xref: /freebsd/sbin/pfctl/parse.y (revision 4757b351ea9d59d71d4a38b82506d2d16fcd560d)
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 	sa_family_t		 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 *, sa_family_t);
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 pfctl_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 pfctl_pooladdr));
5015 				if (pa == NULL)
5016 					err(1, "binat: calloc");
5017 				pa->addr = $13->host->addr;
5018 				pa->ifname[0] = 0;
5019 				pa->af = $13->host->af;
5020 				TAILQ_INSERT_TAIL(&binat.rdr.list,
5021 				    pa, entries);
5022 
5023 				free($13);
5024 			}
5025 
5026 			pfctl_append_rule(pf, &binat, "");
5027 		}
5028 		;
5029 
5030 tag		: /* empty */		{ $$ = NULL; }
5031 		| TAG STRING		{ $$ = $2; }
5032 		;
5033 
5034 tagged		: /* empty */		{ $$.neg = 0; $$.name = NULL; }
5035 		| not TAGGED string	{ $$.neg = $1; $$.name = $3; }
5036 		;
5037 
5038 rtable		: /* empty */		{ $$ = -1; }
5039 		| RTABLE NUMBER		{
5040 			if ($2 < 0 || $2 > rt_tableid_max()) {
5041 				yyerror("invalid rtable id");
5042 				YYERROR;
5043 			}
5044 			$$ = $2;
5045 		}
5046 		;
5047 
5048 route_host	: STRING			{
5049 			$$ = calloc(1, sizeof(struct node_host));
5050 			if ($$ == NULL)
5051 				err(1, "route_host: calloc");
5052 			if (strlen($1) >= IFNAMSIZ) {
5053 				yyerror("interface name too long");
5054 				YYERROR;
5055 			}
5056 			$$->ifname = strdup($1);
5057 			set_ipmask($$, 128);
5058 			$$->next = NULL;
5059 			$$->tail = $$;
5060 		}
5061 		| '(' STRING host ')'		{
5062 			struct node_host *n;
5063 
5064 			$$ = $3;
5065 			for (n = $3; n != NULL; n = n->next) {
5066 				if (strlen($2) >= IFNAMSIZ) {
5067 					yyerror("interface name too long");
5068 					YYERROR;
5069 				}
5070 				n->ifname = strdup($2);
5071 			}
5072 		}
5073 		;
5074 
5075 route_host_list	: route_host optnl			{ $$ = $1; }
5076 		| route_host_list comma route_host optnl {
5077 			if ($1->af == 0)
5078 				$1->af = $3->af;
5079 			if ($1->af != $3->af) {
5080 				yyerror("all pool addresses must be in the "
5081 				    "same address family");
5082 				YYERROR;
5083 			}
5084 			$1->tail->next = $3;
5085 			$1->tail = $3->tail;
5086 			$$ = $1;
5087 		}
5088 		;
5089 
5090 routespec	: route_host			{ $$ = $1; }
5091 		| '{' optnl route_host_list '}'	{ $$ = $3; }
5092 		;
5093 
5094 route		: /* empty */			{
5095 			$$.rt = PF_NOPFROUTE;
5096 		}
5097 		| FASTROUTE {
5098 			/* backwards-compat */
5099 			$$.rt = PF_NOPFROUTE;
5100 		}
5101 		| ROUTETO route_redirspec {
5102 			$$.rt = PF_ROUTETO;
5103 			$$.redirspec = $2;
5104 		}
5105 		| REPLYTO route_redirspec {
5106 			$$.rt = PF_REPLYTO;
5107 			$$.redirspec = $2;
5108 		}
5109 		| DUPTO route_redirspec {
5110 			$$.rt = PF_DUPTO;
5111 			$$.redirspec = $2;
5112 		}
5113 		;
5114 
5115 timeout_spec	: STRING NUMBER
5116 		{
5117 			if (check_rulestate(PFCTL_STATE_OPTION)) {
5118 				free($1);
5119 				YYERROR;
5120 			}
5121 			if ($2 < 0 || $2 > UINT_MAX) {
5122 				yyerror("only positive values permitted");
5123 				YYERROR;
5124 			}
5125 			if (pfctl_apply_timeout(pf, $1, $2, 0) != 0) {
5126 				yyerror("unknown timeout %s", $1);
5127 				free($1);
5128 				YYERROR;
5129 			}
5130 			free($1);
5131 		}
5132 		| INTERVAL NUMBER		{
5133 			if (check_rulestate(PFCTL_STATE_OPTION))
5134 				YYERROR;
5135 			if ($2 < 0 || $2 > UINT_MAX) {
5136 				yyerror("only positive values permitted");
5137 				YYERROR;
5138 			}
5139 			if (pfctl_apply_timeout(pf, "interval", $2, 0) != 0)
5140 				YYERROR;
5141 		}
5142 		;
5143 
5144 timeout_list	: timeout_list comma timeout_spec optnl
5145 		| timeout_spec optnl
5146 		;
5147 
5148 limit_spec	: STRING NUMBER
5149 		{
5150 			if (check_rulestate(PFCTL_STATE_OPTION)) {
5151 				free($1);
5152 				YYERROR;
5153 			}
5154 			if ($2 < 0 || $2 > UINT_MAX) {
5155 				yyerror("only positive values permitted");
5156 				YYERROR;
5157 			}
5158 			if (pfctl_apply_limit(pf, $1, $2) != 0) {
5159 				yyerror("unable to set limit %s %u", $1, $2);
5160 				free($1);
5161 				YYERROR;
5162 			}
5163 			free($1);
5164 		}
5165 		;
5166 
5167 limit_list	: limit_list comma limit_spec optnl
5168 		| limit_spec optnl
5169 		;
5170 
5171 comma		: ','
5172 		| /* empty */
5173 		;
5174 
5175 yesno		: NO			{ $$ = 0; }
5176 		| STRING		{
5177 			if (!strcmp($1, "yes"))
5178 				$$ = 1;
5179 			else {
5180 				yyerror("invalid value '%s', expected 'yes' "
5181 				    "or 'no'", $1);
5182 				free($1);
5183 				YYERROR;
5184 			}
5185 			free($1);
5186 		}
5187 		;
5188 
5189 unaryop		: '='		{ $$ = PF_OP_EQ; }
5190 		| NE			{ $$ = PF_OP_NE; }
5191 		| LE			{ $$ = PF_OP_LE; }
5192 		| '<'		{ $$ = PF_OP_LT; }
5193 		| GE		{ $$ = PF_OP_GE; }
5194 		| '>'		{ $$ = PF_OP_GT; }
5195 		;
5196 
5197 %%
5198 
5199 int
5200 yyerror(const char *fmt, ...)
5201 {
5202 	va_list		 ap;
5203 
5204 	file->errors++;
5205 	va_start(ap, fmt);
5206 	fprintf(stderr, "%s:%d: ", file->name, yylval.lineno);
5207 	vfprintf(stderr, fmt, ap);
5208 	fprintf(stderr, "\n");
5209 	va_end(ap);
5210 	return (0);
5211 }
5212 
5213 int
5214 validate_range(uint8_t op, uint16_t p1, uint16_t p2)
5215 {
5216 	uint16_t a = ntohs(p1);
5217 	uint16_t b = ntohs(p2);
5218 
5219 	if ((op == PF_OP_RRG && a > b) ||  /* 34:12,  i.e. none */
5220 	    (op == PF_OP_IRG && a >= b) || /* 34><12, i.e. none */
5221 	    (op == PF_OP_XRG && a > b))    /* 34<>22, i.e. all */
5222 		return 1;
5223 	return 0;
5224 }
5225 
5226 int
5227 disallow_table(struct node_host *h, const char *fmt)
5228 {
5229 	for (; h != NULL; h = h->next)
5230 		if (h->addr.type == PF_ADDR_TABLE) {
5231 			yyerror(fmt, h->addr.v.tblname);
5232 			return (1);
5233 		}
5234 	return (0);
5235 }
5236 
5237 int
5238 disallow_urpf_failed(struct node_host *h, const char *fmt)
5239 {
5240 	for (; h != NULL; h = h->next)
5241 		if (h->addr.type == PF_ADDR_URPFFAILED) {
5242 			yyerror(fmt);
5243 			return (1);
5244 		}
5245 	return (0);
5246 }
5247 
5248 int
5249 disallow_alias(struct node_host *h, const char *fmt)
5250 {
5251 	for (; h != NULL; h = h->next)
5252 		if (DYNIF_MULTIADDR(h->addr)) {
5253 			yyerror(fmt, h->addr.v.tblname);
5254 			return (1);
5255 		}
5256 	return (0);
5257 }
5258 
5259 int
5260 rule_consistent(struct pfctl_rule *r, int anchor_call)
5261 {
5262 	int	problems = 0;
5263 
5264 	switch (r->action) {
5265 	case PF_PASS:
5266 	case PF_MATCH:
5267 	case PF_DROP:
5268 	case PF_SCRUB:
5269 	case PF_NOSCRUB:
5270 		problems = filter_consistent(r, anchor_call);
5271 		break;
5272 	case PF_NAT:
5273 	case PF_NONAT:
5274 		problems = nat_consistent(r);
5275 		break;
5276 	case PF_RDR:
5277 	case PF_NORDR:
5278 		problems = rdr_consistent(r);
5279 		break;
5280 	case PF_BINAT:
5281 	case PF_NOBINAT:
5282 	default:
5283 		break;
5284 	}
5285 	return (problems);
5286 }
5287 
5288 int
5289 filter_consistent(struct pfctl_rule *r, int anchor_call)
5290 {
5291 	int	problems = 0;
5292 
5293 	if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP &&
5294 	    r->proto != IPPROTO_SCTP &&
5295 	    (r->src.port_op || r->dst.port_op)) {
5296 		yyerror("port only applies to tcp/udp/sctp");
5297 		problems++;
5298 	}
5299 	if (r->proto != IPPROTO_ICMP && r->proto != IPPROTO_ICMPV6 &&
5300 	    (r->type || r->code)) {
5301 		yyerror("icmp-type/code only applies to icmp");
5302 		problems++;
5303 	}
5304 	if (!r->af && (r->type || r->code)) {
5305 		yyerror("must indicate address family with icmp-type/code");
5306 		problems++;
5307 	}
5308 	if (r->rule_flag & PFRULE_AFTO && r->af == r->naf) {
5309 		yyerror("must indicate different address family with af-to");
5310 		problems++;
5311 	}
5312 	if (r->overload_tblname[0] &&
5313 	    r->max_src_conn == 0 && r->max_src_conn_rate.seconds == 0) {
5314 		yyerror("'overload' requires 'max-src-conn' "
5315 		    "or 'max-src-conn-rate'");
5316 		problems++;
5317 	}
5318 	if ((r->proto == IPPROTO_ICMP && r->af == AF_INET6) ||
5319 	    (r->proto == IPPROTO_ICMPV6 && r->af == AF_INET)) {
5320 		yyerror("proto %s doesn't match address family %s",
5321 		    r->proto == IPPROTO_ICMP ? "icmp" : "icmp6",
5322 		    r->af == AF_INET ? "inet" : "inet6");
5323 		problems++;
5324 	}
5325 	if (r->allow_opts && r->action != PF_PASS && r->action != PF_MATCH) {
5326 		yyerror("allow-opts can only be specified for pass or "
5327 		    "match rules");
5328 		problems++;
5329 	}
5330 	if (r->rule_flag & PFRULE_FRAGMENT && (r->src.port_op ||
5331 	    r->dst.port_op || r->flagset || r->type || r->code)) {
5332 		yyerror("fragments can be filtered only on IP header fields");
5333 		problems++;
5334 	}
5335 	if (r->rule_flag & PFRULE_RETURNRST && r->proto != IPPROTO_TCP) {
5336 		yyerror("return-rst can only be applied to TCP rules");
5337 		problems++;
5338 	}
5339 	if (r->max_src_nodes && !(r->rule_flag & PFRULE_RULESRCTRACK)) {
5340 		yyerror("max-src-nodes requires 'source-track rule'");
5341 		problems++;
5342 	}
5343 	if (r->action != PF_PASS && r->keep_state) {
5344 		yyerror("keep state is great, but only for pass rules");
5345 		problems++;
5346 	}
5347 	if (r->rule_flag & PFRULE_STATESLOPPY &&
5348 	    (r->keep_state == PF_STATE_MODULATE ||
5349 	    r->keep_state == PF_STATE_SYNPROXY)) {
5350 		yyerror("sloppy state matching cannot be used with "
5351 		    "synproxy state or modulate state");
5352 		problems++;
5353 	}
5354 	if ((r->keep_state == PF_STATE_SYNPROXY) && (r->direction != PF_IN))
5355 		fprintf(stderr, "%s:%d: warning: "
5356 		    "synproxy used for inbound rules only, "
5357 		    "ignored for outbound\n", file->name, yylval.lineno);
5358 	if (r->rule_flag & PFRULE_AFTO && r->rt) {
5359 		if (r->rt != PF_ROUTETO && r->rt != PF_REPLYTO) {
5360 			yyerror("dup-to "
5361 			   "must not be used on af-to rules");
5362 			problems++;
5363 		}
5364 	}
5365 	/* Basic rule sanity check. */
5366 	switch (r->action) {
5367 	case PF_MATCH:
5368 		if (r->divert.port) {
5369 			yyerror("divert is not supported on match rules");
5370 			problems++;
5371 		}
5372 		if (r->rt) {
5373 			yyerror("route-to, reply-to, dup-to and fastroute "
5374 			   "must not be used on match rules");
5375 			problems++;
5376 		}
5377 		if (r->rule_flag & PFRULE_AFTO) {
5378 			yyerror("af-to is not supported on match rules");
5379 			problems++;
5380 		}
5381 		break;
5382 	case PF_DROP:
5383 		if (r->rt) {
5384 			yyerror("route-to, reply-to and dup-to "
5385 			    "are not supported on block rules");
5386 			problems++;
5387 		}
5388 		break;
5389 	default:;
5390 	}
5391 	if (!TAILQ_EMPTY(&(r->nat.list)) || !TAILQ_EMPTY(&(r->rdr.list))) {
5392 		if (r->action != PF_MATCH && !r->keep_state) {
5393 			yyerror("nat-to and rdr-to require keep state");
5394 			problems++;
5395 		}
5396 		if (r->direction == PF_INOUT) {
5397 			yyerror("nat-to and rdr-to require a direction");
5398 			problems++;
5399 		}
5400 	}
5401 	if (r->route.opts & PF_POOL_STICKYADDR && !r->keep_state) {
5402 		yyerror("'sticky-address' requires 'keep state'");
5403 	}
5404 	return (-problems);
5405 }
5406 
5407 int
5408 nat_consistent(struct pfctl_rule *r)
5409 {
5410 	return (0);	/* yeah! */
5411 }
5412 
5413 int
5414 rdr_consistent(struct pfctl_rule *r)
5415 {
5416 	int			 problems = 0;
5417 
5418 	if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP &&
5419 	    r->proto != IPPROTO_SCTP) {
5420 		if (r->src.port_op) {
5421 			yyerror("src port only applies to tcp/udp/sctp");
5422 			problems++;
5423 		}
5424 		if (r->dst.port_op) {
5425 			yyerror("dst port only applies to tcp/udp/sctp");
5426 			problems++;
5427 		}
5428 		if (r->rdr.proxy_port[0]) {
5429 			yyerror("rdr port only applies to tcp/udp/sctp");
5430 			problems++;
5431 		}
5432 	}
5433 	if (r->dst.port_op &&
5434 	    r->dst.port_op != PF_OP_EQ && r->dst.port_op != PF_OP_RRG) {
5435 		yyerror("invalid port operator for rdr destination port");
5436 		problems++;
5437 	}
5438 	return (-problems);
5439 }
5440 
5441 int
5442 process_tabledef(char *name, struct table_opts *opts, int popts)
5443 {
5444 	struct pfr_buffer	 ab;
5445 	struct node_tinit	*ti;
5446 	unsigned long		 maxcount;
5447 	size_t			 s = sizeof(maxcount);
5448 
5449 	bzero(&ab, sizeof(ab));
5450 	ab.pfrb_type = PFRB_ADDRS;
5451 	SIMPLEQ_FOREACH(ti, &opts->init_nodes, entries) {
5452 		if (ti->file)
5453 			if (pfr_buf_load(&ab, ti->file, 0, append_addr, popts)) {
5454 				if (errno)
5455 					yyerror("cannot load \"%s\": %s",
5456 					    ti->file, strerror(errno));
5457 				else
5458 					yyerror("file \"%s\" contains bad data",
5459 					    ti->file);
5460 				goto _error;
5461 			}
5462 		if (ti->host)
5463 			if (append_addr_host(&ab, ti->host, 0, 0)) {
5464 				yyerror("cannot create address buffer: %s",
5465 				    strerror(errno));
5466 				goto _error;
5467 			}
5468 	}
5469 	if (pf->opts & PF_OPT_VERBOSE)
5470 		print_tabledef(name, opts->flags, opts->init_addr,
5471 		    &opts->init_nodes);
5472 	if (!(pf->opts & PF_OPT_NOACTION) ||
5473 	    (pf->opts & PF_OPT_DUMMYACTION))
5474 		warn_duplicate_tables(name, pf->anchor->path);
5475 	else if (pf->opts & PF_OPT_VERBOSE)
5476 		fprintf(stderr, "%s:%d: skipping duplicate table checks"
5477 		    " for <%s>\n", file->name, yylval.lineno, name);
5478 	if (!(pf->opts & PF_OPT_NOACTION) &&
5479 	    pfctl_define_table(name, opts->flags, opts->init_addr,
5480 	    pf->anchor->path, &ab, pf->anchor->ruleset.tticket)) {
5481 
5482 		if (sysctlbyname("net.pf.request_maxcount", &maxcount, &s,
5483 		    NULL, 0) == -1)
5484 			maxcount = 65535;
5485 
5486 		if (ab.pfrb_size > maxcount)
5487 			yyerror("cannot define table %s: too many elements.\n"
5488 			    "Consider increasing net.pf.request_maxcount.",
5489 			    name);
5490 		else
5491 			yyerror("cannot define table %s: %s", name,
5492 			    pf_strerror(errno));
5493 
5494 		goto _error;
5495 	}
5496 	pf->tdirty = 1;
5497 	pfr_buf_clear(&ab);
5498 	return (0);
5499 _error:
5500 	pfr_buf_clear(&ab);
5501 	return (-1);
5502 }
5503 
5504 struct keywords {
5505 	const char	*k_name;
5506 	int		 k_val;
5507 };
5508 
5509 /* macro gore, but you should've seen the prior indentation nightmare... */
5510 
5511 #define FREE_LIST(T,r) \
5512 	do { \
5513 		T *p, *node = r; \
5514 		while (node != NULL) { \
5515 			p = node; \
5516 			node = node->next; \
5517 			free(p); \
5518 		} \
5519 	} while (0)
5520 
5521 #define LOOP_THROUGH(T,n,r,C) \
5522 	do { \
5523 		T *n; \
5524 		if (r == NULL) { \
5525 			r = calloc(1, sizeof(T)); \
5526 			if (r == NULL) \
5527 				err(1, "LOOP: calloc"); \
5528 			r->next = NULL; \
5529 		} \
5530 		n = r; \
5531 		while (n != NULL) { \
5532 			do { \
5533 				C; \
5534 			} while (0); \
5535 			n = n->next; \
5536 		} \
5537 	} while (0)
5538 
5539 void
5540 expand_label_str(char *label, size_t len, const char *srch, const char *repl)
5541 {
5542 	char *tmp;
5543 	char *p, *q;
5544 
5545 	if ((tmp = calloc(1, len)) == NULL)
5546 		err(1, "%s: calloc", __func__);
5547 	p = q = label;
5548 	while ((q = strstr(p, srch)) != NULL) {
5549 		*q = '\0';
5550 		if ((strlcat(tmp, p, len) >= len) ||
5551 		    (strlcat(tmp, repl, len) >= len))
5552 			errx(1, "%s: label too long", __func__);
5553 		q += strlen(srch);
5554 		p = q;
5555 	}
5556 	if (strlcat(tmp, p, len) >= len)
5557 		errx(1, "%s: label too long", __func__);
5558 	strlcpy(label, tmp, len);	/* always fits */
5559 	free(tmp);
5560 }
5561 
5562 void
5563 expand_label_if(const char *name, char *label, size_t len, const char *ifname)
5564 {
5565 	if (strstr(label, name) != NULL) {
5566 		if (!*ifname)
5567 			expand_label_str(label, len, name, "any");
5568 		else
5569 			expand_label_str(label, len, name, ifname);
5570 	}
5571 }
5572 
5573 void
5574 expand_label_addr(const char *name, char *label, size_t len, sa_family_t af,
5575     struct pf_rule_addr *addr)
5576 {
5577 	char tmp[64], tmp_not[66];
5578 
5579 	if (strstr(label, name) != NULL) {
5580 		switch (addr->addr.type) {
5581 		case PF_ADDR_DYNIFTL:
5582 			snprintf(tmp, sizeof(tmp), "(%s)", addr->addr.v.ifname);
5583 			break;
5584 		case PF_ADDR_TABLE:
5585 			snprintf(tmp, sizeof(tmp), "<%s>", addr->addr.v.tblname);
5586 			break;
5587 		case PF_ADDR_NOROUTE:
5588 			snprintf(tmp, sizeof(tmp), "no-route");
5589 			break;
5590 		case PF_ADDR_URPFFAILED:
5591 			snprintf(tmp, sizeof(tmp), "urpf-failed");
5592 			break;
5593 		case PF_ADDR_ADDRMASK:
5594 			if (!af || (PF_AZERO(&addr->addr.v.a.addr, af) &&
5595 			    PF_AZERO(&addr->addr.v.a.mask, af)))
5596 				snprintf(tmp, sizeof(tmp), "any");
5597 			else {
5598 				char	a[48];
5599 				int	bits;
5600 
5601 				if (inet_ntop(af, &addr->addr.v.a.addr, a,
5602 				    sizeof(a)) == NULL)
5603 					snprintf(tmp, sizeof(tmp), "?");
5604 				else {
5605 					bits = unmask(&addr->addr.v.a.mask);
5606 					if ((af == AF_INET && bits < 32) ||
5607 					    (af == AF_INET6 && bits < 128))
5608 						snprintf(tmp, sizeof(tmp),
5609 						    "%s/%d", a, bits);
5610 					else
5611 						snprintf(tmp, sizeof(tmp),
5612 						    "%s", a);
5613 				}
5614 			}
5615 			break;
5616 		default:
5617 			snprintf(tmp, sizeof(tmp), "?");
5618 			break;
5619 		}
5620 
5621 		if (addr->neg) {
5622 			snprintf(tmp_not, sizeof(tmp_not), "! %s", tmp);
5623 			expand_label_str(label, len, name, tmp_not);
5624 		} else
5625 			expand_label_str(label, len, name, tmp);
5626 	}
5627 }
5628 
5629 void
5630 expand_label_port(const char *name, char *label, size_t len,
5631     struct pf_rule_addr *addr)
5632 {
5633 	char	 a1[6], a2[6], op[13] = "";
5634 
5635 	if (strstr(label, name) != NULL) {
5636 		snprintf(a1, sizeof(a1), "%u", ntohs(addr->port[0]));
5637 		snprintf(a2, sizeof(a2), "%u", ntohs(addr->port[1]));
5638 		if (!addr->port_op)
5639 			;
5640 		else if (addr->port_op == PF_OP_IRG)
5641 			snprintf(op, sizeof(op), "%s><%s", a1, a2);
5642 		else if (addr->port_op == PF_OP_XRG)
5643 			snprintf(op, sizeof(op), "%s<>%s", a1, a2);
5644 		else if (addr->port_op == PF_OP_EQ)
5645 			snprintf(op, sizeof(op), "%s", a1);
5646 		else if (addr->port_op == PF_OP_NE)
5647 			snprintf(op, sizeof(op), "!=%s", a1);
5648 		else if (addr->port_op == PF_OP_LT)
5649 			snprintf(op, sizeof(op), "<%s", a1);
5650 		else if (addr->port_op == PF_OP_LE)
5651 			snprintf(op, sizeof(op), "<=%s", a1);
5652 		else if (addr->port_op == PF_OP_GT)
5653 			snprintf(op, sizeof(op), ">%s", a1);
5654 		else if (addr->port_op == PF_OP_GE)
5655 			snprintf(op, sizeof(op), ">=%s", a1);
5656 		expand_label_str(label, len, name, op);
5657 	}
5658 }
5659 
5660 void
5661 expand_label_proto(const char *name, char *label, size_t len, u_int8_t proto)
5662 {
5663 	const char *protoname;
5664 	char n[4];
5665 
5666 	if (strstr(label, name) != NULL) {
5667 		protoname = pfctl_proto2name(proto);
5668 		if (protoname != NULL)
5669 			expand_label_str(label, len, name, protoname);
5670 		else {
5671 			snprintf(n, sizeof(n), "%u", proto);
5672 			expand_label_str(label, len, name, n);
5673 		}
5674 	}
5675 }
5676 
5677 void
5678 expand_label_nr(const char *name, char *label, size_t len,
5679     struct pfctl_rule *r)
5680 {
5681 	char n[11];
5682 
5683 	if (strstr(label, name) != NULL) {
5684 		snprintf(n, sizeof(n), "%u", r->nr);
5685 		expand_label_str(label, len, name, n);
5686 	}
5687 }
5688 
5689 void
5690 expand_label(char *label, size_t len, struct pfctl_rule *r)
5691 {
5692 	expand_label_if("$if", label, len, r->ifname);
5693 	expand_label_addr("$srcaddr", label, len, r->af, &r->src);
5694 	expand_label_addr("$dstaddr", label, len, r->af, &r->dst);
5695 	expand_label_port("$srcport", label, len, &r->src);
5696 	expand_label_port("$dstport", label, len, &r->dst);
5697 	expand_label_proto("$proto", label, len, r->proto);
5698 	expand_label_nr("$nr", label, len, r);
5699 }
5700 
5701 int
5702 expand_altq(struct pf_altq *a, struct node_if *interfaces,
5703     struct node_queue *nqueues, struct node_queue_bw bwspec,
5704     struct node_queue_opt *opts)
5705 {
5706 	struct pf_altq		 pa, pb;
5707 	char			 qname[PF_QNAME_SIZE];
5708 	struct node_queue	*n;
5709 	struct node_queue_bw	 bw;
5710 	int			 errs = 0;
5711 
5712 	if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
5713 		FREE_LIST(struct node_if, interfaces);
5714 		if (nqueues)
5715 			FREE_LIST(struct node_queue, nqueues);
5716 		return (0);
5717 	}
5718 
5719 	LOOP_THROUGH(struct node_if, interface, interfaces,
5720 		memcpy(&pa, a, sizeof(struct pf_altq));
5721 		if (strlcpy(pa.ifname, interface->ifname,
5722 		    sizeof(pa.ifname)) >= sizeof(pa.ifname))
5723 			errx(1, "%s: strlcpy", __func__);
5724 
5725 		if (interface->not) {
5726 			yyerror("altq on ! <interface> is not supported");
5727 			errs++;
5728 		} else {
5729 			if (eval_pfaltq(pf, &pa, &bwspec, opts))
5730 				errs++;
5731 			else
5732 				if (pfctl_add_altq(pf, &pa))
5733 					errs++;
5734 
5735 			if (pf->opts & PF_OPT_VERBOSE) {
5736 				print_altq(&pf->paltq->altq, 0,
5737 				    &bwspec, opts);
5738 				if (nqueues && nqueues->tail) {
5739 					printf("queue { ");
5740 					LOOP_THROUGH(struct node_queue, queue,
5741 					    nqueues,
5742 						printf("%s ",
5743 						    queue->queue);
5744 					);
5745 					printf("}");
5746 				}
5747 				printf("\n");
5748 			}
5749 
5750 			if (pa.scheduler == ALTQT_CBQ ||
5751 			    pa.scheduler == ALTQT_HFSC ||
5752 			    pa.scheduler == ALTQT_FAIRQ) {
5753 				/* now create a root queue */
5754 				memset(&pb, 0, sizeof(struct pf_altq));
5755 				if (strlcpy(qname, "root_", sizeof(qname)) >=
5756 				    sizeof(qname))
5757 					errx(1, "%s: strlcpy", __func__);
5758 				if (strlcat(qname, interface->ifname,
5759 				    sizeof(qname)) >= sizeof(qname))
5760 					errx(1, "%s: strlcat", __func__);
5761 				if (strlcpy(pb.qname, qname,
5762 				    sizeof(pb.qname)) >= sizeof(pb.qname))
5763 					errx(1, "%s: strlcpy", __func__);
5764 				if (strlcpy(pb.ifname, interface->ifname,
5765 				    sizeof(pb.ifname)) >= sizeof(pb.ifname))
5766 					errx(1, "%s: strlcpy", __func__);
5767 				pb.qlimit = pa.qlimit;
5768 				pb.scheduler = pa.scheduler;
5769 				bw.bw_absolute = pa.ifbandwidth;
5770 				bw.bw_percent = 0;
5771 				if (eval_pfqueue(pf, &pb, &bw, opts))
5772 					errs++;
5773 				else
5774 					if (pfctl_add_altq(pf, &pb))
5775 						errs++;
5776 			}
5777 
5778 			LOOP_THROUGH(struct node_queue, queue, nqueues,
5779 				n = calloc(1, sizeof(struct node_queue));
5780 				if (n == NULL)
5781 					err(1, "%s: calloc", __func__);
5782 				if (pa.scheduler == ALTQT_CBQ ||
5783 				    pa.scheduler == ALTQT_HFSC ||
5784 				    pa.scheduler == ALTQT_FAIRQ)
5785 					if (strlcpy(n->parent, qname,
5786 					    sizeof(n->parent)) >=
5787 					    sizeof(n->parent))
5788 						errx(1, "%s: strlcpy", __func__);
5789 				if (strlcpy(n->queue, queue->queue,
5790 				    sizeof(n->queue)) >= sizeof(n->queue))
5791 					errx(1, "%s: strlcpy", __func__);
5792 				if (strlcpy(n->ifname, interface->ifname,
5793 				    sizeof(n->ifname)) >= sizeof(n->ifname))
5794 					errx(1, "%s: strlcpy", __func__);
5795 				n->scheduler = pa.scheduler;
5796 				n->next = NULL;
5797 				n->tail = n;
5798 				if (queues == NULL)
5799 					queues = n;
5800 				else {
5801 					queues->tail->next = n;
5802 					queues->tail = n;
5803 				}
5804 			);
5805 		}
5806 	);
5807 	FREE_LIST(struct node_if, interfaces);
5808 	if (nqueues)
5809 		FREE_LIST(struct node_queue, nqueues);
5810 
5811 	return (errs);
5812 }
5813 
5814 int
5815 expand_queue(struct pf_altq *a, struct node_if *interfaces,
5816     struct node_queue *nqueues, struct node_queue_bw bwspec,
5817     struct node_queue_opt *opts)
5818 {
5819 	struct node_queue	*n, *nq;
5820 	struct pf_altq		 pa;
5821 	u_int8_t		 found = 0;
5822 	u_int8_t		 errs = 0;
5823 
5824 	if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
5825 		FREE_LIST(struct node_queue, nqueues);
5826 		return (0);
5827 	}
5828 
5829 	if (queues == NULL) {
5830 		yyerror("queue %s has no parent", a->qname);
5831 		FREE_LIST(struct node_queue, nqueues);
5832 		return (1);
5833 	}
5834 
5835 	LOOP_THROUGH(struct node_if, interface, interfaces,
5836 		LOOP_THROUGH(struct node_queue, tqueue, queues,
5837 			if (!strncmp(a->qname, tqueue->queue, PF_QNAME_SIZE) &&
5838 			    (interface->ifname[0] == 0 ||
5839 			    (!interface->not && !strncmp(interface->ifname,
5840 			    tqueue->ifname, IFNAMSIZ)) ||
5841 			    (interface->not && strncmp(interface->ifname,
5842 			    tqueue->ifname, IFNAMSIZ)))) {
5843 				/* found ourself in queues */
5844 				found++;
5845 
5846 				memcpy(&pa, a, sizeof(struct pf_altq));
5847 
5848 				if (pa.scheduler != ALTQT_NONE &&
5849 				    pa.scheduler != tqueue->scheduler) {
5850 					yyerror("exactly one scheduler type "
5851 					    "per interface allowed");
5852 					return (1);
5853 				}
5854 				pa.scheduler = tqueue->scheduler;
5855 
5856 				/* scheduler dependent error checking */
5857 				switch (pa.scheduler) {
5858 				case ALTQT_PRIQ:
5859 					if (nqueues != NULL) {
5860 						yyerror("priq queues cannot "
5861 						    "have child queues");
5862 						return (1);
5863 					}
5864 					if (bwspec.bw_absolute > 0 ||
5865 					    bwspec.bw_percent < 100) {
5866 						yyerror("priq doesn't take "
5867 						    "bandwidth");
5868 						return (1);
5869 					}
5870 					break;
5871 				default:
5872 					break;
5873 				}
5874 
5875 				if (strlcpy(pa.ifname, tqueue->ifname,
5876 				    sizeof(pa.ifname)) >= sizeof(pa.ifname))
5877 					errx(1, "%s: strlcpy", __func__);
5878 				if (strlcpy(pa.parent, tqueue->parent,
5879 				    sizeof(pa.parent)) >= sizeof(pa.parent))
5880 					errx(1, "%s: strlcpy", __func__);
5881 
5882 				if (eval_pfqueue(pf, &pa, &bwspec, opts))
5883 					errs++;
5884 				else
5885 					if (pfctl_add_altq(pf, &pa))
5886 						errs++;
5887 
5888 				for (nq = nqueues; nq != NULL; nq = nq->next) {
5889 					if (!strcmp(a->qname, nq->queue)) {
5890 						yyerror("queue cannot have "
5891 						    "itself as child");
5892 						errs++;
5893 						continue;
5894 					}
5895 					n = calloc(1,
5896 					    sizeof(struct node_queue));
5897 					if (n == NULL)
5898 						err(1, "%s: calloc", __func__);
5899 					if (strlcpy(n->parent, a->qname,
5900 					    sizeof(n->parent)) >=
5901 					    sizeof(n->parent))
5902 						errx(1, "%s strlcpy", __func__);
5903 					if (strlcpy(n->queue, nq->queue,
5904 					    sizeof(n->queue)) >=
5905 					    sizeof(n->queue))
5906 						errx(1, "%s strlcpy", __func__);
5907 					if (strlcpy(n->ifname, tqueue->ifname,
5908 					    sizeof(n->ifname)) >=
5909 					    sizeof(n->ifname))
5910 						errx(1, "%s strlcpy", __func__);
5911 					n->scheduler = tqueue->scheduler;
5912 					n->next = NULL;
5913 					n->tail = n;
5914 					if (queues == NULL)
5915 						queues = n;
5916 					else {
5917 						queues->tail->next = n;
5918 						queues->tail = n;
5919 					}
5920 				}
5921 				if ((pf->opts & PF_OPT_VERBOSE) && (
5922 				    (found == 1 && interface->ifname[0] == 0) ||
5923 				    (found > 0 && interface->ifname[0] != 0))) {
5924 					print_queue(&pf->paltq->altq, 0,
5925 					    &bwspec, interface->ifname[0] != 0,
5926 					    opts);
5927 					if (nqueues && nqueues->tail) {
5928 						printf("{ ");
5929 						LOOP_THROUGH(struct node_queue,
5930 						    queue, nqueues,
5931 							printf("%s ",
5932 							    queue->queue);
5933 						);
5934 						printf("}");
5935 					}
5936 					printf("\n");
5937 				}
5938 			}
5939 		);
5940 	);
5941 
5942 	FREE_LIST(struct node_queue, nqueues);
5943 	FREE_LIST(struct node_if, interfaces);
5944 
5945 	if (!found) {
5946 		yyerror("queue %s has no parent", a->qname);
5947 		errs++;
5948 	}
5949 
5950 	if (errs)
5951 		return (1);
5952 	else
5953 		return (0);
5954 }
5955 
5956 static int
5957 pf_af_to_proto(sa_family_t af)
5958 {
5959 	if (af == AF_INET)
5960 		return (ETHERTYPE_IP);
5961 	if (af == AF_INET6)
5962 		return (ETHERTYPE_IPV6);
5963 
5964 	return (0);
5965 }
5966 
5967 void
5968 expand_eth_rule(struct pfctl_eth_rule *r,
5969     struct node_if *interfaces, struct node_etherproto *protos,
5970     struct node_mac *srcs, struct node_mac *dsts,
5971     struct node_host *ipsrcs, struct node_host *ipdsts,
5972     const char *bridge_to, const char *anchor_call)
5973 {
5974 	char tagname[PF_TAG_NAME_SIZE];
5975 	char match_tagname[PF_TAG_NAME_SIZE];
5976 	char qname[PF_QNAME_SIZE];
5977 
5978 	if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname))
5979 		errx(1, "%s: tagname", __func__);
5980 	if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >=
5981 	    sizeof(match_tagname))
5982 		errx(1, "%s: match_tagname", __func__);
5983 	if (strlcpy(qname, r->qname, sizeof(qname)) >= sizeof(qname))
5984 		errx(1, "%s: qname", __func__);
5985 
5986 	LOOP_THROUGH(struct node_if, interface, interfaces,
5987 	LOOP_THROUGH(struct node_etherproto, proto, protos,
5988 	LOOP_THROUGH(struct node_mac, src, srcs,
5989 	LOOP_THROUGH(struct node_mac, dst, dsts,
5990 	LOOP_THROUGH(struct node_host, ipsrc, ipsrcs,
5991 	LOOP_THROUGH(struct node_host, ipdst, ipdsts,
5992 		strlcpy(r->ifname, interface->ifname,
5993 		    sizeof(r->ifname));
5994 		r->ifnot = interface->not;
5995 		r->proto = proto->proto;
5996 		if (!r->proto && ipsrc->af)
5997 			r->proto = pf_af_to_proto(ipsrc->af);
5998 		else if (!r->proto && ipdst->af)
5999 			r->proto = pf_af_to_proto(ipdst->af);
6000 		bcopy(src->mac, r->src.addr, ETHER_ADDR_LEN);
6001 		bcopy(src->mask, r->src.mask, ETHER_ADDR_LEN);
6002 		r->src.neg = src->neg;
6003 		r->src.isset = src->isset;
6004 		r->ipsrc.addr = ipsrc->addr;
6005 		r->ipsrc.neg = ipsrc->not;
6006 		r->ipdst.addr = ipdst->addr;
6007 		r->ipdst.neg = ipdst->not;
6008 		bcopy(dst->mac, r->dst.addr, ETHER_ADDR_LEN);
6009 		bcopy(dst->mask, r->dst.mask, ETHER_ADDR_LEN);
6010 		r->dst.neg = dst->neg;
6011 		r->dst.isset = dst->isset;
6012 		r->nr = pf->eastack[pf->asd]->match++;
6013 
6014 		if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >=
6015 		    sizeof(r->tagname))
6016 			errx(1, "%s: r->tagname", __func__);
6017 		if (strlcpy(r->match_tagname, match_tagname,
6018 		    sizeof(r->match_tagname)) >= sizeof(r->match_tagname))
6019 			errx(1, "%s: r->match_tagname", __func__);
6020 		if (strlcpy(r->qname, qname, sizeof(r->qname)) >= sizeof(r->qname))
6021 			errx(1, "%s: r->qname", __func__);
6022 
6023 		if (bridge_to)
6024 			strlcpy(r->bridge_to, bridge_to, sizeof(r->bridge_to));
6025 
6026 		pfctl_append_eth_rule(pf, r, anchor_call);
6027 	))))));
6028 
6029 	FREE_LIST(struct node_if, interfaces);
6030 	FREE_LIST(struct node_etherproto, protos);
6031 	FREE_LIST(struct node_mac, srcs);
6032 	FREE_LIST(struct node_mac, dsts);
6033 	FREE_LIST(struct node_host, ipsrcs);
6034 	FREE_LIST(struct node_host, ipdsts);
6035 }
6036 
6037 int
6038 apply_rdr_ports(struct pfctl_rule *r, struct pfctl_pool *rpool, struct redirspec *rs)
6039 {
6040 	if (rs == NULL)
6041 		return 0;
6042 
6043 	rpool->proxy_port[0] = ntohs(rs->rport.a);
6044 
6045 	if (!rs->rport.b && rs->rport.t) {
6046 		rpool->proxy_port[1] = ntohs(rs->rport.a) +
6047 		    (ntohs(r->dst.port[1]) - ntohs(r->dst.port[0]));
6048 	} else {
6049 		if (validate_range(rs->rport.t, rs->rport.a,
6050 		    rs->rport.b)) {
6051 			yyerror("invalid rdr-to port range");
6052 			return (1);
6053 		}
6054 		r->rdr.proxy_port[1] = ntohs(rs->rport.b);
6055 	}
6056 
6057 	if (rs->pool_opts.staticport) {
6058 		yyerror("the 'static-port' option is only valid with nat rules");
6059 		return 1;
6060 	}
6061 
6062 	if (rs->pool_opts.mape.offset) {
6063 		yyerror("the 'map-e-portset' option is only valid with nat rules");
6064 		return 1;
6065 	}
6066 
6067 	return 0;
6068 }
6069 
6070 int
6071 apply_nat_ports(struct pfctl_pool *rpool, struct redirspec *rs)
6072 {
6073 	if (rs == NULL)
6074 		return 0;
6075 
6076 	rpool->proxy_port[0] = ntohs(rs->rport.a);
6077 	rpool->proxy_port[1] = ntohs(rs->rport.b);
6078 	if (!rpool->proxy_port[0] && !rpool->proxy_port[1]) {
6079 		rpool->proxy_port[0] =  PF_NAT_PROXY_PORT_LOW;
6080 		rpool->proxy_port[1] =  PF_NAT_PROXY_PORT_HIGH;
6081 	} else if (!rpool->proxy_port[1])
6082 		rpool->proxy_port[1] = rpool->proxy_port[0];
6083 
6084 	if (rs->pool_opts.staticport) {
6085 		if (rpool->proxy_port[0] != PF_NAT_PROXY_PORT_LOW &&
6086 		    rpool->proxy_port[1] != PF_NAT_PROXY_PORT_HIGH) {
6087 			yyerror("the 'static-port' option can't"
6088 			    " be used when specifying a port"
6089 			    " range");
6090 			return 1;
6091 		}
6092 		rpool->proxy_port[0] = 0;
6093 		rpool->proxy_port[1] = 0;
6094 	}
6095 
6096 	if (rs->pool_opts.mape.offset) {
6097 		if (rs->pool_opts.staticport) {
6098 			yyerror("the 'map-e-portset' option"
6099 			    " can't be used 'static-port'");
6100 			return 1;
6101 		}
6102 		if (rpool->proxy_port[0] != PF_NAT_PROXY_PORT_LOW &&
6103 		    rpool->proxy_port[1] != PF_NAT_PROXY_PORT_HIGH) {
6104 			yyerror("the 'map-e-portset' option"
6105 			    " can't be used when specifying"
6106 			    " a port range");
6107 			return 1;
6108 		}
6109 		rpool->mape = rs->pool_opts.mape;
6110 	}
6111 
6112 	return 0;
6113 }
6114 
6115 int
6116 apply_redirspec(struct pfctl_pool *rpool, struct redirspec *rs)
6117 {
6118 	struct node_host	*h;
6119 	struct pfctl_pooladdr	*pa;
6120 
6121 	if (rs == NULL)
6122 		return 0;
6123 
6124 	rpool->opts = rs->pool_opts.type;
6125 
6126 	if ((rpool->opts & PF_POOL_TYPEMASK) == PF_POOL_NONE &&
6127 	    (rs->host->next != NULL ||
6128 	    rs->host->addr.type == PF_ADDR_TABLE ||
6129 	    DYNIF_MULTIADDR(rs->host->addr)))
6130 		rpool->opts = PF_POOL_ROUNDROBIN;
6131 
6132 	if (!PF_POOL_DYNTYPE(rpool->opts) &&
6133 	    (disallow_table(rs->host, "tables are not supported by pool type") ||
6134 	    disallow_alias(rs->host, "interface (%s) is not supported by pool type")))
6135 		return 1;
6136 
6137 	if (rs->host->next != NULL &&
6138 	    ((rpool->opts & PF_POOL_TYPEMASK) != PF_POOL_ROUNDROBIN)) {
6139 		yyerror("r.route.opts must be PF_POOL_ROUNDROBIN");
6140 		return 1;
6141 	}
6142 
6143 	if (rs->host->next != NULL) {
6144 		if ((rpool->opts & PF_POOL_TYPEMASK) !=
6145 		    PF_POOL_ROUNDROBIN) {
6146 			yyerror("only round-robin valid for multiple "
6147 			    "redirection addresses");
6148 			return 1;
6149 		}
6150 	}
6151 
6152 	rpool->opts |= rs->pool_opts.opts;
6153 
6154 	if (rs->pool_opts.key != NULL)
6155 		memcpy(&(rpool->key), rs->pool_opts.key,
6156 		    sizeof(struct pf_poolhashkey));
6157 
6158 	for (h = rs->host; h != NULL; h = h->next) {
6159 		pa = calloc(1, sizeof(struct pfctl_pooladdr));
6160 		if (pa == NULL)
6161 			err(1, "%s: calloc", __func__);
6162 		pa->addr = h->addr;
6163 		pa->af = h->af;
6164 		if (h->ifname != NULL) {
6165 			if (strlcpy(pa->ifname, h->ifname,
6166 			    sizeof(pa->ifname)) >= sizeof(pa->ifname))
6167 				errx(1, "%s: strlcpy", __func__);
6168 		} else
6169 			pa->ifname[0] = 0;
6170 		TAILQ_INSERT_TAIL(&(rpool->list), pa, entries);
6171 	}
6172 
6173 	return 0;
6174 }
6175 
6176 int
6177 check_binat_redirspec(struct node_host *src_host, struct pfctl_rule *r,
6178     sa_family_t af)
6179 {
6180 	struct pfctl_pooladdr	*nat_pool = TAILQ_FIRST(&(r->nat.list));
6181 	int			error = 0;
6182 
6183 	/* XXX: FreeBSD allows syntax like "{ host1 host2 }" for redirection
6184 	 * pools but does not covert them to tables automatically, because
6185 	 * syntax "{ (iface1 host1), (iface2 iface2) }" is allowed for route-to
6186 	 * redirection. Add a FreeBSD-specific guard against using multiple
6187 	 * hosts for source and redirection.
6188 	 */
6189 	if (src_host->next) {
6190 		yyerror("invalid use of table as the source address "
6191 		    "of a binat-to rule");
6192 		error++;
6193 	}
6194 	if (TAILQ_NEXT(nat_pool, entries)) {
6195 		yyerror ("tables cannot be used as the redirect "
6196 		    "address of a binat-to rule");
6197 		error++;
6198 	}
6199 
6200 	if (disallow_table(src_host, "invalid use of table "
6201 	    "<%s> as the source address of a binat-to rule") ||
6202 	    disallow_alias(src_host, "invalid use of interface "
6203 	    "(%s) as the source address of a binat-to rule")) {
6204 		error++;
6205 	} else if ((r->src.addr.type != PF_ADDR_ADDRMASK &&
6206 	    r->src.addr.type != PF_ADDR_DYNIFTL) ||
6207 	    (nat_pool->addr.type != PF_ADDR_ADDRMASK &&
6208 	    nat_pool->addr.type != PF_ADDR_DYNIFTL)) {
6209 		yyerror("binat-to requires a specified "
6210 		    "source and redirect address");
6211 		error++;
6212 	}
6213 	if (DYNIF_MULTIADDR(r->src.addr) ||
6214 	    DYNIF_MULTIADDR(nat_pool->addr)) {
6215 		yyerror ("dynamic interfaces must be "
6216 		    "used with:0 in a binat-to rule");
6217 		error++;
6218 	}
6219 	if (PF_AZERO(&r->src.addr.v.a.mask, af) ||
6220 	    PF_AZERO(&(nat_pool->addr.v.a.mask), af)) {
6221 		yyerror ("source and redir addresess must have "
6222 		    "a matching network mask in binat-rule");
6223 		error++;
6224 	}
6225 	if (nat_pool->addr.type == PF_ADDR_TABLE) {
6226 		yyerror ("tables cannot be used as the redirect "
6227 		    "address of a binat-to rule");
6228 		error++;
6229 	}
6230 	if (r->direction != PF_INOUT) {
6231 		yyerror("binat-to cannot be specified "
6232 		    "with a direction");
6233 		error++;
6234 	}
6235 
6236 	/* first specify outbound NAT rule */
6237 	r->direction = PF_OUT;
6238 
6239 	return (error);
6240 }
6241 
6242 void
6243 add_binat_rdr_rule(
6244     struct pfctl_rule *binat_rule,
6245     struct redirspec *binat_nat_redirspec, struct node_host *binat_src_host,
6246     struct pfctl_rule *rdr_rule, struct redirspec **rdr_redirspec,
6247     struct node_host **rdr_dst_host)
6248 {
6249 	struct node_host	*rdr_src_host;
6250 
6251 	/*
6252 	 * We're copying the whole rule, but we must re-init redir pools.
6253 	 * FreeBSD uses lists of pfctl_pooladdr, we can't just overwrite them.
6254 	 */
6255 	bcopy(binat_rule, rdr_rule, sizeof(struct pfctl_rule));
6256 	TAILQ_INIT(&(rdr_rule->rdr.list));
6257 	TAILQ_INIT(&(rdr_rule->nat.list));
6258 
6259 	/* now specify inbound rdr rule */
6260 	rdr_rule->direction = PF_IN;
6261 
6262 	if ((rdr_src_host = calloc(1, sizeof(*rdr_src_host))) == NULL)
6263 		err(1, "%s", __func__);
6264 	bcopy(binat_src_host, rdr_src_host, sizeof(*rdr_src_host));
6265 	rdr_src_host->ifname = NULL;
6266 	rdr_src_host->next = NULL;
6267 	rdr_src_host->tail = NULL;
6268 
6269 	if (((*rdr_dst_host) = calloc(1, sizeof(**rdr_dst_host))) == NULL)
6270 		err(1, "%s", __func__);
6271 	bcopy(&(binat_nat_redirspec->host->addr), &((*rdr_dst_host)->addr),
6272 	    sizeof((*rdr_dst_host)->addr));
6273 	(*rdr_dst_host)->ifname = NULL;
6274 	(*rdr_dst_host)->next = NULL;
6275 	(*rdr_dst_host)->tail = NULL;
6276 
6277 	if (((*rdr_redirspec) = calloc(1, sizeof(**rdr_redirspec))) == NULL)
6278 		err(1, "%s", __func__);
6279 	bcopy(binat_nat_redirspec, (*rdr_redirspec), sizeof(**rdr_redirspec));
6280 	(*rdr_redirspec)->pool_opts.staticport = 0;
6281 	(*rdr_redirspec)->host = rdr_src_host;
6282 }
6283 
6284 void
6285 expand_rule(struct pfctl_rule *r, bool keeprule,
6286     struct node_if *interfaces, struct redirspec *nat,
6287     struct redirspec *rdr, struct redirspec *route,
6288     struct node_proto *protos,
6289     struct node_os *src_oses, struct node_host *src_hosts,
6290     struct node_port *src_ports, struct node_host *dst_hosts,
6291     struct node_port *dst_ports, struct node_uid *uids, struct node_gid *gids,
6292     struct node_if *rcv, struct node_icmp *icmp_types, const char *anchor_call)
6293 {
6294 	sa_family_t		 af = r->af;
6295 	int			 added = 0, error = 0;
6296 	char			 ifname[IF_NAMESIZE];
6297 	char			 label[PF_RULE_MAX_LABEL_COUNT][PF_RULE_LABEL_SIZE];
6298 	char			 tagname[PF_TAG_NAME_SIZE];
6299 	char			 match_tagname[PF_TAG_NAME_SIZE];
6300 	struct node_host	*osrch, *odsth;
6301 	u_int8_t		 flags, flagset, keep_state;
6302 
6303 	memcpy(label, r->label, sizeof(r->label));
6304 	assert(sizeof(r->label) == sizeof(label));
6305 	if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname))
6306 		errx(1, "%s: strlcpy", __func__);
6307 	if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >=
6308 	    sizeof(match_tagname))
6309 		errx(1, "%s: strlcpy", __func__);
6310 	flags = r->flags;
6311 	flagset = r->flagset;
6312 	keep_state = r->keep_state;
6313 
6314 	LOOP_THROUGH(struct node_if, interface, interfaces,
6315 	LOOP_THROUGH(struct node_proto, proto, protos,
6316 	LOOP_THROUGH(struct node_icmp, icmp_type, icmp_types,
6317 	LOOP_THROUGH(struct node_host, src_host, src_hosts,
6318 	LOOP_THROUGH(struct node_host, dst_host, dst_hosts,
6319 	LOOP_THROUGH(struct node_port, src_port, src_ports,
6320 	LOOP_THROUGH(struct node_port, dst_port, dst_ports,
6321 	LOOP_THROUGH(struct node_os, src_os, src_oses,
6322 	LOOP_THROUGH(struct node_uid, uid, uids,
6323 	LOOP_THROUGH(struct node_gid, gid, gids,
6324 
6325 		r->af = af;
6326 
6327 		if (r->rule_flag & PFRULE_AFTO) {
6328 			assert(nat != NULL);
6329 			r->naf = nat->af;
6330 		}
6331 
6332 		/* for link-local IPv6 address, interface must match up */
6333 		if ((r->af && src_host->af && r->af != src_host->af) ||
6334 		    (r->af && dst_host->af && r->af != dst_host->af) ||
6335 		    (src_host->af && dst_host->af &&
6336 		    src_host->af != dst_host->af) ||
6337 		    (src_host->ifindex && dst_host->ifindex &&
6338 		    src_host->ifindex != dst_host->ifindex) ||
6339 		    (src_host->ifindex && *interface->ifname &&
6340 		    src_host->ifindex != ifa_nametoindex(interface->ifname)) ||
6341 		    (dst_host->ifindex && *interface->ifname &&
6342 		    dst_host->ifindex != ifa_nametoindex(interface->ifname)))
6343 			continue;
6344 		if (!r->af && src_host->af)
6345 			r->af = src_host->af;
6346 		else if (!r->af && dst_host->af)
6347 			r->af = dst_host->af;
6348 
6349 		if (*interface->ifname)
6350 			strlcpy(r->ifname, interface->ifname,
6351 			    sizeof(r->ifname));
6352 		else if (ifa_indextoname(src_host->ifindex, ifname))
6353 			strlcpy(r->ifname, ifname, sizeof(r->ifname));
6354 		else if (ifa_indextoname(dst_host->ifindex, ifname))
6355 			strlcpy(r->ifname, ifname, sizeof(r->ifname));
6356 		else
6357 			memset(r->ifname, '\0', sizeof(r->ifname));
6358 
6359 		memcpy(r->label, label, sizeof(r->label));
6360 		if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >=
6361 		    sizeof(r->tagname))
6362 			errx(1, "%s: strlcpy", __func__);
6363 		if (strlcpy(r->match_tagname, match_tagname,
6364 		    sizeof(r->match_tagname)) >= sizeof(r->match_tagname))
6365 			errx(1, "%s: strlcpy", __func__);
6366 
6367 		osrch = odsth = NULL;
6368 		if (src_host->addr.type == PF_ADDR_DYNIFTL) {
6369 			osrch = src_host;
6370 			if ((src_host = gen_dynnode(src_host, r->af)) == NULL)
6371 				err(1, "%s: calloc", __func__);
6372 		}
6373 		if (dst_host->addr.type == PF_ADDR_DYNIFTL) {
6374 			odsth = dst_host;
6375 			if ((dst_host = gen_dynnode(dst_host, r->af)) == NULL)
6376 				err(1, "%s: calloc", __func__);
6377 		}
6378 
6379 		error += check_netmask(src_host, r->af);
6380 		error += check_netmask(dst_host, r->af);
6381 
6382 		r->ifnot = interface->not;
6383 		r->proto = proto->proto;
6384 		r->src.addr = src_host->addr;
6385 		r->src.neg = src_host->not;
6386 		r->src.port[0] = src_port->port[0];
6387 		r->src.port[1] = src_port->port[1];
6388 		r->src.port_op = src_port->op;
6389 		r->dst.addr = dst_host->addr;
6390 		r->dst.neg = dst_host->not;
6391 		r->dst.port[0] = dst_port->port[0];
6392 		r->dst.port[1] = dst_port->port[1];
6393 		r->dst.port_op = dst_port->op;
6394 		r->uid.op = uid->op;
6395 		r->uid.uid[0] = uid->uid[0];
6396 		r->uid.uid[1] = uid->uid[1];
6397 		r->gid.op = gid->op;
6398 		r->gid.gid[0] = gid->gid[0];
6399 		r->gid.gid[1] = gid->gid[1];
6400 		if (rcv) {
6401 			strlcpy(r->rcv_ifname, rcv->ifname,
6402 			    sizeof(r->rcv_ifname));
6403 			r->rcvifnot = rcv->not;
6404 		}
6405 		r->type = icmp_type->type;
6406 		r->code = icmp_type->code;
6407 
6408 		if ((keep_state == PF_STATE_MODULATE ||
6409 		    keep_state == PF_STATE_SYNPROXY) &&
6410 		    r->proto && r->proto != IPPROTO_TCP)
6411 			r->keep_state = PF_STATE_NORMAL;
6412 		else
6413 			r->keep_state = keep_state;
6414 
6415 		if (r->proto && r->proto != IPPROTO_TCP) {
6416 			r->flags = 0;
6417 			r->flagset = 0;
6418 		} else {
6419 			r->flags = flags;
6420 			r->flagset = flagset;
6421 		}
6422 		if (icmp_type->proto && r->proto != icmp_type->proto) {
6423 			yyerror("icmp-type mismatch");
6424 			error++;
6425 		}
6426 
6427 		if (src_os && src_os->os) {
6428 			r->os_fingerprint = pfctl_get_fingerprint(src_os->os);
6429 			if ((pf->opts & PF_OPT_VERBOSE2) &&
6430 			    r->os_fingerprint == PF_OSFP_NOMATCH)
6431 				fprintf(stderr,
6432 				    "warning: unknown '%s' OS fingerprint\n",
6433 				    src_os->os);
6434 		} else {
6435 			r->os_fingerprint = PF_OSFP_ANY;
6436 		}
6437 
6438 		if (r->action == PF_RDR) {
6439 			/* Pre-FreeBSD 15 "rdr" rule */
6440 			error += apply_rdr_ports(r, &(r->rdr), rdr);
6441 			error += apply_redirspec(&(r->rdr), rdr);
6442 		} else if (r->action == PF_NAT) {
6443 			/* Pre-FreeBSD 15 "nat" rule */
6444 			error += apply_nat_ports(&(r->rdr), rdr);
6445 			error += apply_redirspec(&(r->rdr), rdr);
6446 		} else {
6447 			/* Modern rule with optional NAT, BINAT, RDR or ROUTE*/
6448 			error += apply_redirspec(&(r->route), route);
6449 
6450 			error += apply_nat_ports(&(r->nat), nat);
6451 			error += apply_redirspec(&(r->nat), nat);
6452 			error += apply_rdr_ports(r, &(r->rdr), rdr);
6453 			error += apply_redirspec(&(r->rdr), rdr);
6454 
6455 			if (nat && nat->binat)
6456 				error += check_binat_redirspec(src_host, r, af);
6457 		}
6458 
6459 		if (rule_consistent(r, anchor_call[0]) < 0 || error)
6460 			yyerror("skipping rule due to errors");
6461 		else {
6462 			r->nr = pf->astack[pf->asd]->match++;
6463 			pfctl_append_rule(pf, r, anchor_call);
6464 			added++;
6465 		}
6466 
6467 		/* Generate binat's matching inbound rule */
6468 		if (!error && nat && nat->binat) {
6469 			struct pfctl_rule	rdr_rule;
6470 			struct redirspec	*rdr_redirspec;
6471 			struct node_host	*rdr_dst_host;
6472 
6473 			add_binat_rdr_rule(
6474 			    r, nat, src_hosts,
6475 			    &rdr_rule, &rdr_redirspec, &rdr_dst_host);
6476 
6477 			expand_rule(&rdr_rule, true, interface, NULL, rdr_redirspec,
6478 			    NULL, proto, src_os, dst_host, dst_port,
6479 			    rdr_dst_host, src_port, uid, gid, rcv, icmp_type,
6480 			    "");
6481 		}
6482 
6483 		if (osrch && src_host->addr.type == PF_ADDR_DYNIFTL) {
6484 			free(src_host);
6485 			src_host = osrch;
6486 		}
6487 		if (odsth && dst_host->addr.type == PF_ADDR_DYNIFTL) {
6488 			free(dst_host);
6489 			dst_host = odsth;
6490 		}
6491 
6492 	))))))))));
6493 
6494 	if (!keeprule) {
6495 		FREE_LIST(struct node_if, interfaces);
6496 		FREE_LIST(struct node_proto, protos);
6497 		FREE_LIST(struct node_host, src_hosts);
6498 		FREE_LIST(struct node_port, src_ports);
6499 		FREE_LIST(struct node_os, src_oses);
6500 		FREE_LIST(struct node_host, dst_hosts);
6501 		FREE_LIST(struct node_port, dst_ports);
6502 		FREE_LIST(struct node_uid, uids);
6503 		FREE_LIST(struct node_gid, gids);
6504 		FREE_LIST(struct node_icmp, icmp_types);
6505 		if (nat) {
6506 			FREE_LIST(struct node_host, nat->host);
6507 			free(nat);
6508 		}
6509 		if (rdr) {
6510 			FREE_LIST(struct node_host, rdr->host);
6511 			free(rdr);
6512 		}
6513 		if (route) {
6514 			FREE_LIST(struct node_host, route->host);
6515 			free(route);
6516 		}
6517 	}
6518 
6519 	if (!added)
6520 		yyerror("rule expands to no valid combination");
6521 }
6522 
6523 int
6524 expand_skip_interface(struct node_if *interfaces)
6525 {
6526 	int	errs = 0;
6527 
6528 	if (!interfaces || (!interfaces->next && !interfaces->not &&
6529 	    !strcmp(interfaces->ifname, "none"))) {
6530 		if (pf->opts & PF_OPT_VERBOSE)
6531 			printf("set skip on none\n");
6532 		errs = pfctl_set_interface_flags(pf, "", PFI_IFLAG_SKIP, 0);
6533 		return (errs);
6534 	}
6535 
6536 	if (pf->opts & PF_OPT_VERBOSE)
6537 		printf("set skip on {");
6538 	LOOP_THROUGH(struct node_if, interface, interfaces,
6539 		if (pf->opts & PF_OPT_VERBOSE)
6540 			printf(" %s", interface->ifname);
6541 		if (interface->not) {
6542 			yyerror("skip on ! <interface> is not supported");
6543 			errs++;
6544 		} else
6545 			errs += pfctl_set_interface_flags(pf,
6546 			    interface->ifname, PFI_IFLAG_SKIP, 1);
6547 	);
6548 	if (pf->opts & PF_OPT_VERBOSE)
6549 		printf(" }\n");
6550 
6551 	FREE_LIST(struct node_if, interfaces);
6552 
6553 	if (errs)
6554 		return (1);
6555 	else
6556 		return (0);
6557 }
6558 
6559 void
6560 freehostlist(struct node_host *h)
6561 {
6562 	FREE_LIST(struct node_host, h);
6563 }
6564 
6565 #undef FREE_LIST
6566 #undef LOOP_THROUGH
6567 
6568 int
6569 check_rulestate(int desired_state)
6570 {
6571 	if (require_order && (rulestate > desired_state)) {
6572 		yyerror("Rules must be in order: options, ethernet, "
6573 		    "normalization, queueing, translation, filtering");
6574 		return (1);
6575 	}
6576 	rulestate = desired_state;
6577 	return (0);
6578 }
6579 
6580 int
6581 kw_cmp(const void *k, const void *e)
6582 {
6583 	return (strcmp(k, ((const struct keywords *)e)->k_name));
6584 }
6585 
6586 int
6587 lookup(char *s)
6588 {
6589 	/* this has to be sorted always */
6590 	static const struct keywords keywords[] = {
6591 		{ "af-to",		AFTO},
6592 		{ "all",		ALL},
6593 		{ "allow-opts",		ALLOWOPTS},
6594 		{ "allow-related",	ALLOW_RELATED},
6595 		{ "altq",		ALTQ},
6596 		{ "anchor",		ANCHOR},
6597 		{ "antispoof",		ANTISPOOF},
6598 		{ "any",		ANY},
6599 		{ "bandwidth",		BANDWIDTH},
6600 		{ "binat",		BINAT},
6601 		{ "binat-anchor",	BINATANCHOR},
6602 		{ "binat-to",		BINATTO},
6603 		{ "bitmask",		BITMASK},
6604 		{ "block",		BLOCK},
6605 		{ "block-policy",	BLOCKPOLICY},
6606 		{ "bridge-to",		BRIDGE_TO},
6607 		{ "buckets",		BUCKETS},
6608 		{ "cbq",		CBQ},
6609 		{ "code",		CODE},
6610 		{ "codelq",		CODEL},
6611 		{ "debug",		DEBUG},
6612 		{ "divert-reply",	DIVERTREPLY},
6613 		{ "divert-to",		DIVERTTO},
6614 		{ "dnpipe",		DNPIPE},
6615 		{ "dnqueue",		DNQUEUE},
6616 		{ "drop",		DROP},
6617 		{ "dup-to",		DUPTO},
6618 		{ "endpoint-independent", ENDPI},
6619 		{ "ether",		ETHER},
6620 		{ "fail-policy",	FAILPOLICY},
6621 		{ "fairq",		FAIRQ},
6622 		{ "fastroute",		FASTROUTE},
6623 		{ "file",		FILENAME},
6624 		{ "fingerprints",	FINGERPRINTS},
6625 		{ "flags",		FLAGS},
6626 		{ "floating",		FLOATING},
6627 		{ "flush",		FLUSH},
6628 		{ "for",		FOR},
6629 		{ "fragment",		FRAGMENT},
6630 		{ "from",		FROM},
6631 		{ "global",		GLOBAL},
6632 		{ "group",		GROUP},
6633 		{ "hfsc",		HFSC},
6634 		{ "hogs",		HOGS},
6635 		{ "hostid",		HOSTID},
6636 		{ "icmp-type",		ICMPTYPE},
6637 		{ "icmp6-type",		ICMP6TYPE},
6638 		{ "if-bound",		IFBOUND},
6639 		{ "in",			IN},
6640 		{ "include",		INCLUDE},
6641 		{ "inet",		INET},
6642 		{ "inet6",		INET6},
6643 		{ "interval",		INTERVAL},
6644 		{ "keep",		KEEP},
6645 		{ "keepcounters",	KEEPCOUNTERS},
6646 		{ "l3",			L3},
6647 		{ "label",		LABEL},
6648 		{ "limit",		LIMIT},
6649 		{ "linkshare",		LINKSHARE},
6650 		{ "load",		LOAD},
6651 		{ "log",		LOG},
6652 		{ "loginterface",	LOGINTERFACE},
6653 		{ "map-e-portset",	MAPEPORTSET},
6654 		{ "match",		MATCH},
6655 		{ "matches",	MATCHES},
6656 		{ "max",		MAXIMUM},
6657 		{ "max-mss",		MAXMSS},
6658 		{ "max-pkt-rate",       MAXPKTRATE},
6659 		{ "max-pkt-size",	MAXPKTSIZE},
6660 		{ "max-src-conn",	MAXSRCCONN},
6661 		{ "max-src-conn-rate",	MAXSRCCONNRATE},
6662 		{ "max-src-nodes",	MAXSRCNODES},
6663 		{ "max-src-states",	MAXSRCSTATES},
6664 		{ "min-ttl",		MINTTL},
6665 		{ "modulate",		MODULATE},
6666 		{ "nat",		NAT},
6667 		{ "nat-anchor",		NATANCHOR},
6668 		{ "nat-to",		NATTO},
6669 		{ "no",			NO},
6670 		{ "no-df",		NODF},
6671 		{ "no-route",		NOROUTE},
6672 		{ "no-sync",		NOSYNC},
6673 		{ "on",			ON},
6674 		{ "optimization",	OPTIMIZATION},
6675 		{ "os",			OS},
6676 		{ "out",		OUT},
6677 		{ "overload",		OVERLOAD},
6678 		{ "pass",		PASS},
6679 		{ "pflow",		PFLOW},
6680 		{ "port",		PORT},
6681 		{ "prio",		PRIO},
6682 		{ "priority",		PRIORITY},
6683 		{ "priq",		PRIQ},
6684 		{ "probability",	PROBABILITY},
6685 		{ "proto",		PROTO},
6686 		{ "qlimit",		QLIMIT},
6687 		{ "queue",		QUEUE},
6688 		{ "quick",		QUICK},
6689 		{ "random",		RANDOM},
6690 		{ "random-id",		RANDOMID},
6691 		{ "rdr",		RDR},
6692 		{ "rdr-anchor",		RDRANCHOR},
6693 		{ "rdr-to",		RDRTO},
6694 		{ "realtime",		REALTIME},
6695 		{ "reassemble",		REASSEMBLE},
6696 		{ "received-on",	RECEIVEDON},
6697 		{ "reply-to",		REPLYTO},
6698 		{ "require-order",	REQUIREORDER},
6699 		{ "return",		RETURN},
6700 		{ "return-icmp",	RETURNICMP},
6701 		{ "return-icmp6",	RETURNICMP6},
6702 		{ "return-rst",		RETURNRST},
6703 		{ "ridentifier",	RIDENTIFIER},
6704 		{ "round-robin",	ROUNDROBIN},
6705 		{ "route",		ROUTE},
6706 		{ "route-to",		ROUTETO},
6707 		{ "rtable",		RTABLE},
6708 		{ "rule",		RULE},
6709 		{ "ruleset-optimization",	RULESET_OPTIMIZATION},
6710 		{ "scrub",		SCRUB},
6711 		{ "set",		SET},
6712 		{ "set-tos",		SETTOS},
6713 		{ "skip",		SKIP},
6714 		{ "sloppy",		SLOPPY},
6715 		{ "source-hash",	SOURCEHASH},
6716 		{ "source-track",	SOURCETRACK},
6717 		{ "state",		STATE},
6718 		{ "state-defaults",	STATEDEFAULTS},
6719 		{ "state-policy",	STATEPOLICY},
6720 		{ "static-port",	STATICPORT},
6721 		{ "sticky-address",	STICKYADDRESS},
6722 		{ "syncookies",         SYNCOOKIES},
6723 		{ "synproxy",		SYNPROXY},
6724 		{ "table",		TABLE},
6725 		{ "tag",		TAG},
6726 		{ "tagged",		TAGGED},
6727 		{ "target",		TARGET},
6728 		{ "tbrsize",		TBRSIZE},
6729 		{ "timeout",		TIMEOUT},
6730 		{ "to",			TO},
6731 		{ "tos",		TOS},
6732 		{ "ttl",		TTL},
6733 		{ "upperlimit",		UPPERLIMIT},
6734 		{ "urpf-failed",	URPFFAILED},
6735 		{ "user",		USER},
6736 	};
6737 	const struct keywords	*p;
6738 
6739 	p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
6740 	    sizeof(keywords[0]), kw_cmp);
6741 
6742 	if (p) {
6743 		if (debug > 1)
6744 			fprintf(stderr, "%s: %d\n", s, p->k_val);
6745 		return (p->k_val);
6746 	} else {
6747 		if (debug > 1)
6748 			fprintf(stderr, "string: %s\n", s);
6749 		return (STRING);
6750 	}
6751 }
6752 
6753 #define	START_EXPAND	1
6754 #define	DONE_EXPAND		2
6755 
6756 static int expanding;
6757 
6758 int
6759 igetc(void)
6760 {
6761 	int c;
6762 	while (1) {
6763 		if (file->ungetpos > 0)
6764 			c = file->ungetbuf[--file->ungetpos];
6765 		else
6766 			c = getc(file->stream);
6767 		if (c == START_EXPAND)
6768 			expanding = 1;
6769 		else if (c == DONE_EXPAND)
6770 			expanding = 0;
6771 		else
6772 			break;
6773 	}
6774 	return (c);
6775 }
6776 
6777 int
6778 lgetc(int quotec)
6779 {
6780 	int	c, next;
6781 
6782 	if (quotec) {
6783 		if ((c = igetc()) == EOF) {
6784 			yyerror("reached end of file while parsing quoted string");
6785 			if (file == topfile || popfile() == EOF)
6786 				return (EOF);
6787 			return (quotec);
6788 		}
6789 		return (c);
6790 	}
6791 
6792 	while ((c = igetc()) == '\\') {
6793 		next = igetc();
6794 		if (next != '\n') {
6795 			c = next;
6796 			break;
6797 		}
6798 		yylval.lineno = file->lineno;
6799 		file->lineno++;
6800 	}
6801 
6802 	if (c == EOF) {
6803 		/*
6804 		 * Fake EOL when hit EOF for the first time. This gets line
6805 		 * count right if last line in included file is syntactically
6806 		 * invalid and has no newline.
6807 		 */
6808 		if (file->eof_reached == 0) {
6809 			file->eof_reached = 1;
6810 			return ('\n');
6811 		}
6812 		while (c == EOF) {
6813 			if (file == topfile || popfile() == EOF)
6814 				return (EOF);
6815 			c = igetc();
6816 		}
6817 	}
6818 	return (c);
6819 }
6820 
6821 void
6822 lungetc(int c)
6823 {
6824 	if (c == EOF)
6825 		return;
6826 	if (file->ungetpos >= file->ungetsize) {
6827 		void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
6828 		if (p == NULL)
6829 			err(1, "%s", __func__);
6830 		file->ungetbuf = p;
6831 		file->ungetsize *= 2;
6832 	}
6833 	file->ungetbuf[file->ungetpos++] = c;
6834 }
6835 
6836 int
6837 findeol(void)
6838 {
6839 	int	c;
6840 
6841 	/* skip to either EOF or the first real EOL */
6842 	while (1) {
6843 		c = lgetc(0);
6844 		if (c == '\n') {
6845 			file->lineno++;
6846 			break;
6847 		}
6848 		if (c == EOF)
6849 			break;
6850 	}
6851 	return (ERROR);
6852 }
6853 
6854 int
6855 yylex(void)
6856 {
6857 	char	 buf[8096];
6858 	char	*p, *val;
6859 	int	 quotec, next, c;
6860 	int	 token;
6861 
6862 top:
6863 	p = buf;
6864 	while ((c = lgetc(0)) == ' ' || c == '\t')
6865 		; /* nothing */
6866 
6867 	yylval.lineno = file->lineno;
6868 	if (c == '#')
6869 		while ((c = lgetc(0)) != '\n' && c != EOF)
6870 			; /* nothing */
6871 	if (c == '$' && !expanding) {
6872 		while (1) {
6873 			if ((c = lgetc(0)) == EOF)
6874 				return (0);
6875 
6876 			if (p + 1 >= buf + sizeof(buf) - 1) {
6877 				yyerror("string too long");
6878 				return (findeol());
6879 			}
6880 			if (isalnum(c) || c == '_') {
6881 				*p++ = (char)c;
6882 				continue;
6883 			}
6884 			*p = '\0';
6885 			lungetc(c);
6886 			break;
6887 		}
6888 		val = symget(buf);
6889 		if (val == NULL) {
6890 			yyerror("macro '%s' not defined", buf);
6891 			return (findeol());
6892 		}
6893 		p = val + strlen(val) - 1;
6894 		lungetc(DONE_EXPAND);
6895 		while (p >= val) {
6896 			lungetc(*p);
6897 			p--;
6898 		}
6899 		lungetc(START_EXPAND);
6900 		goto top;
6901 	}
6902 
6903 	switch (c) {
6904 	case '\'':
6905 	case '"':
6906 		quotec = c;
6907 		while (1) {
6908 			if ((c = lgetc(quotec)) == EOF)
6909 				return (0);
6910 			if (c == '\n') {
6911 				file->lineno++;
6912 				continue;
6913 			} else if (c == '\\') {
6914 				if ((next = lgetc(quotec)) == EOF)
6915 					return (0);
6916 				if (next == quotec || next == ' ' ||
6917 				    next == '\t')
6918 					c = next;
6919 				else if (next == '\n') {
6920 					file->lineno++;
6921 					continue;
6922 				}
6923 				else
6924 					lungetc(next);
6925 			} else if (c == quotec) {
6926 				*p = '\0';
6927 				break;
6928 			} else if (c == '\0') {
6929 				yyerror("syntax error");
6930 				return (findeol());
6931 			}
6932 			if (p + 1 >= buf + sizeof(buf) - 1) {
6933 				yyerror("string too long");
6934 				return (findeol());
6935 			}
6936 			*p++ = (char)c;
6937 		}
6938 		yylval.v.string = strdup(buf);
6939 		if (yylval.v.string == NULL)
6940 			err(1, "%s: strdup", __func__);
6941 		return (STRING);
6942 		case '!':
6943 			next = lgetc(0);
6944 			if (next == '=')
6945 				return (NE);
6946 			lungetc(next);
6947 		break;
6948 	case '<':
6949 		next = lgetc(0);
6950 		if (next == '>') {
6951 			yylval.v.i = PF_OP_XRG;
6952 			return (PORTBINARY);
6953 		} else if (next == '=')
6954 			return (LE);
6955 		lungetc(next);
6956 		break;
6957 	case '>':
6958 		next = lgetc(0);
6959 		if (next == '<') {
6960 			yylval.v.i = PF_OP_IRG;
6961 			return (PORTBINARY);
6962 		} else if (next == '=')
6963 			return (GE);
6964 		lungetc(next);
6965 		break;
6966 	case '-':
6967 		next = lgetc(0);
6968 		if (next == '>')
6969 			return (ARROW);
6970 		lungetc(next);
6971 		break;
6972 	}
6973 
6974 #define allowed_to_end_number(x) \
6975 	(isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
6976 
6977 	if (c == '-' || isdigit(c)) {
6978 		do {
6979 			*p++ = c;
6980 			if ((size_t)(p-buf) >= sizeof(buf)) {
6981 				yyerror("string too long");
6982 				return (findeol());
6983 			}
6984 		} while ((c = lgetc(0)) != EOF && isdigit(c));
6985 		lungetc(c);
6986 		if (p == buf + 1 && buf[0] == '-')
6987 			goto nodigits;
6988 		if (c == EOF || allowed_to_end_number(c)) {
6989 			const char *errstr = NULL;
6990 
6991 			*p = '\0';
6992 			yylval.v.number = strtonum(buf, LLONG_MIN,
6993 			    LLONG_MAX, &errstr);
6994 			if (errstr) {
6995 				yyerror("\"%s\" invalid number: %s",
6996 				    buf, errstr);
6997 				return (findeol());
6998 			}
6999 			return (NUMBER);
7000 		} else {
7001 nodigits:
7002 			while (p > buf + 1)
7003 				lungetc(*--p);
7004 			c = *--p;
7005 			if (c == '-')
7006 				return (c);
7007 		}
7008 	}
7009 
7010 #define allowed_in_string(x) \
7011 	(isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
7012 	x != '{' && x != '}' && x != '<' && x != '>' && \
7013 	x != '!' && x != '=' && x != '/' && x != '#' && \
7014 	x != ','))
7015 
7016 	if (isalnum(c) || c == ':' || c == '_') {
7017 		do {
7018 			*p++ = c;
7019 			if ((size_t)(p-buf) >= sizeof(buf)) {
7020 				yyerror("string too long");
7021 				return (findeol());
7022 			}
7023 		} while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
7024 		lungetc(c);
7025 		*p = '\0';
7026 		if ((token = lookup(buf)) == STRING)
7027 			if ((yylval.v.string = strdup(buf)) == NULL)
7028 				err(1, "%s: strdup", __func__);
7029 		return (token);
7030 	}
7031 	if (c == '\n') {
7032 		yylval.lineno = file->lineno;
7033 		file->lineno++;
7034 	}
7035 	if (c == EOF)
7036 		return (0);
7037 	return (c);
7038 }
7039 
7040 int
7041 check_file_secrecy(int fd, const char *fname)
7042 {
7043 	struct stat	st;
7044 
7045 	if (fstat(fd, &st)) {
7046 		warn("cannot stat %s", fname);
7047 		return (-1);
7048 	}
7049 	if (st.st_uid != 0 && st.st_uid != getuid()) {
7050 		warnx("%s: owner not root or current user", fname);
7051 		return (-1);
7052 	}
7053 	if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
7054 		warnx("%s: group writable or world read/writable", fname);
7055 		return (-1);
7056 	}
7057 	return (0);
7058 }
7059 
7060 struct file *
7061 pushfile(const char *name, int secret)
7062 {
7063 	struct file	*nfile;
7064 
7065 	if ((nfile = calloc(1, sizeof(struct file))) == NULL ||
7066 	    (nfile->name = strdup(name)) == NULL) {
7067 		warn("%s", __func__);
7068 		if (nfile)
7069 			free(nfile);
7070 		return (NULL);
7071 	}
7072 	if (TAILQ_FIRST(&files) == NULL && strcmp(nfile->name, "-") == 0) {
7073 		nfile->stream = stdin;
7074 		free(nfile->name);
7075 		if ((nfile->name = strdup("stdin")) == NULL) {
7076 			warn("%s", __func__);
7077 			free(nfile);
7078 			return (NULL);
7079 		}
7080 	} else if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
7081 		warn("%s: %s", __func__, nfile->name);
7082 		free(nfile->name);
7083 		free(nfile);
7084 		return (NULL);
7085 	} else if (secret &&
7086 	    check_file_secrecy(fileno(nfile->stream), nfile->name)) {
7087 		fclose(nfile->stream);
7088 		free(nfile->name);
7089 		free(nfile);
7090 		return (NULL);
7091 	}
7092 	nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
7093 	nfile->ungetsize = 16;
7094 	nfile->ungetbuf = malloc(nfile->ungetsize);
7095 	if (nfile->ungetbuf == NULL) {
7096 		warn("%s", __func__);
7097 		fclose(nfile->stream);
7098 		free(nfile->name);
7099 		free(nfile);
7100 		return (NULL);
7101 	}
7102 	TAILQ_INSERT_TAIL(&files, nfile, entry);
7103 	return (nfile);
7104 }
7105 
7106 int
7107 popfile(void)
7108 {
7109 	struct file	*prev;
7110 
7111 	if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
7112 		prev->errors += file->errors;
7113 
7114 	TAILQ_REMOVE(&files, file, entry);
7115 	fclose(file->stream);
7116 	free(file->name);
7117 	free(file->ungetbuf);
7118 	free(file);
7119 	file = prev;
7120 
7121 	return (file ? 0 : EOF);
7122 }
7123 
7124 int
7125 parse_config(char *filename, struct pfctl *xpf)
7126 {
7127 	int		 errors = 0;
7128 	struct sym	*sym;
7129 
7130 	pf = xpf;
7131 	errors = 0;
7132 	rulestate = PFCTL_STATE_NONE;
7133 	returnicmpdefault = (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
7134 	returnicmp6default =
7135 	    (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
7136 	blockpolicy = PFRULE_DROP;
7137 	failpolicy = PFRULE_DROP;
7138 	require_order = 1;
7139 
7140 	if ((file = pushfile(filename, 0)) == NULL) {
7141 		warn("cannot open the main config file!");
7142 		return (-1);
7143 	}
7144 	topfile = file;
7145 
7146 	yyparse();
7147 	errors = file->errors;
7148 	popfile();
7149 
7150 	/* Free macros and check which have not been used. */
7151 	while ((sym = TAILQ_FIRST(&symhead))) {
7152 		if ((pf->opts & PF_OPT_VERBOSE2) && !sym->used)
7153 			fprintf(stderr, "warning: macro '%s' not "
7154 			    "used\n", sym->nam);
7155 		free(sym->nam);
7156 		free(sym->val);
7157 		TAILQ_REMOVE(&symhead, sym, entry);
7158 		free(sym);
7159 	}
7160 
7161 	return (errors ? -1 : 0);
7162 }
7163 
7164 int
7165 symset(const char *nam, const char *val, int persist)
7166 {
7167 	struct sym	*sym;
7168 
7169 	TAILQ_FOREACH(sym, &symhead, entry) {
7170 		if (strcmp(nam, sym->nam) == 0)
7171 			break;
7172 	}
7173 
7174 	if (sym != NULL) {
7175 		if (sym->persist == 1)
7176 			return (0);
7177 		else {
7178 			free(sym->nam);
7179 			free(sym->val);
7180 			TAILQ_REMOVE(&symhead, sym, entry);
7181 			free(sym);
7182 		}
7183 	}
7184 	if ((sym = calloc(1, sizeof(*sym))) == NULL)
7185 		return (-1);
7186 
7187 	sym->nam = strdup(nam);
7188 	if (sym->nam == NULL) {
7189 		free(sym);
7190 		return (-1);
7191 	}
7192 	sym->val = strdup(val);
7193 	if (sym->val == NULL) {
7194 		free(sym->nam);
7195 		free(sym);
7196 		return (-1);
7197 	}
7198 	sym->used = 0;
7199 	sym->persist = persist;
7200 	TAILQ_INSERT_TAIL(&symhead, sym, entry);
7201 	return (0);
7202 }
7203 
7204 int
7205 pfctl_cmdline_symset(char *s)
7206 {
7207 	char	*sym, *val;
7208 	int	 ret;
7209 
7210 	if ((val = strrchr(s, '=')) == NULL)
7211 		return (-1);
7212 
7213 	sym = strndup(s, val - s);
7214 	if (sym == NULL)
7215 		err(1, "%s: malloc", __func__);
7216 
7217 	ret = symset(sym, val + 1, 1);
7218 	free(sym);
7219 
7220 	return (ret);
7221 }
7222 
7223 char *
7224 symget(const char *nam)
7225 {
7226 	struct sym	*sym;
7227 
7228 	TAILQ_FOREACH(sym, &symhead, entry) {
7229 		if (strcmp(nam, sym->nam) == 0) {
7230 			sym->used = 1;
7231 			return (sym->val);
7232 		}
7233 	}
7234 	return (NULL);
7235 }
7236 
7237 void
7238 mv_rules(struct pfctl_ruleset *src, struct pfctl_ruleset *dst)
7239 {
7240 	int i;
7241 	struct pfctl_rule *r;
7242 
7243 	for (i = 0; i < PF_RULESET_MAX; ++i) {
7244 		TAILQ_FOREACH(r, src->rules[i].active.ptr, entries)
7245 			dst->anchor->match++;
7246 		TAILQ_CONCAT(dst->rules[i].active.ptr, src->rules[i].active.ptr, entries);
7247 		src->anchor->match = 0;
7248 		TAILQ_CONCAT(dst->rules[i].inactive.ptr, src->rules[i].inactive.ptr, entries);
7249 	}
7250 }
7251 
7252 void
7253 mv_eth_rules(struct pfctl_eth_ruleset *src, struct pfctl_eth_ruleset *dst)
7254 {
7255 	struct pfctl_eth_rule *r;
7256 
7257 	while ((r = TAILQ_FIRST(&src->rules)) != NULL) {
7258 		TAILQ_REMOVE(&src->rules, r, entries);
7259 		TAILQ_INSERT_TAIL(&dst->rules, r, entries);
7260 		dst->anchor->match++;
7261 	}
7262 	src->anchor->match = 0;
7263 }
7264 
7265 void
7266 decide_address_family(struct node_host *n, sa_family_t *af)
7267 {
7268 	if (*af != 0 || n == NULL)
7269 		return;
7270 	*af = n->af;
7271 	while ((n = n->next) != NULL) {
7272 		if (n->af != *af) {
7273 			*af = 0;
7274 			return;
7275 		}
7276 	}
7277 }
7278 
7279 void
7280 remove_invalid_hosts(struct node_host **nh, sa_family_t *af)
7281 {
7282 	struct node_host	*n = *nh, *prev = NULL;
7283 
7284 	while (n != NULL) {
7285 		if (*af && n->af && n->af != *af) {
7286 			/* unlink and free n */
7287 			struct node_host *next = n->next;
7288 
7289 			/* adjust tail pointer */
7290 			if (n == (*nh)->tail)
7291 				(*nh)->tail = prev;
7292 			/* adjust previous node's next pointer */
7293 			if (prev == NULL)
7294 				*nh = next;
7295 			else
7296 				prev->next = next;
7297 			/* free node */
7298 			if (n->ifname != NULL)
7299 				free(n->ifname);
7300 			free(n);
7301 			n = next;
7302 		} else {
7303 			if (n->af && !*af)
7304 				*af = n->af;
7305 			prev = n;
7306 			n = n->next;
7307 		}
7308 	}
7309 }
7310 
7311 int
7312 invalid_redirect(struct node_host *nh, sa_family_t af)
7313 {
7314 	if (!af) {
7315 		struct node_host *n;
7316 
7317 		/* tables and dyniftl are ok without an address family */
7318 		for (n = nh; n != NULL; n = n->next) {
7319 			if (n->addr.type != PF_ADDR_TABLE &&
7320 			    n->addr.type != PF_ADDR_DYNIFTL) {
7321 				yyerror("address family not given and "
7322 				    "translation address expands to multiple "
7323 				    "address families");
7324 				return (1);
7325 			}
7326 		}
7327 	}
7328 	if (nh == NULL) {
7329 		yyerror("no translation address with matching address family "
7330 		    "found.");
7331 		return (1);
7332 	}
7333 	return (0);
7334 }
7335 
7336 int
7337 atoul(char *s, u_long *ulvalp)
7338 {
7339 	u_long	 ulval;
7340 	char	*ep;
7341 
7342 	errno = 0;
7343 	ulval = strtoul(s, &ep, 0);
7344 	if (s[0] == '\0' || *ep != '\0')
7345 		return (-1);
7346 	if (errno == ERANGE && ulval == ULONG_MAX)
7347 		return (-1);
7348 	*ulvalp = ulval;
7349 	return (0);
7350 }
7351 
7352 int
7353 getservice(char *n)
7354 {
7355 	struct servent	*s;
7356 	u_long		 ulval;
7357 
7358 	if (atoul(n, &ulval) == 0) {
7359 		if (ulval > 65535) {
7360 			yyerror("illegal port value %lu", ulval);
7361 			return (-1);
7362 		}
7363 		return (htons(ulval));
7364 	} else {
7365 		s = getservbyname(n, "tcp");
7366 		if (s == NULL)
7367 			s = getservbyname(n, "udp");
7368 		if (s == NULL)
7369 			s = getservbyname(n, "sctp");
7370 		if (s == NULL) {
7371 			yyerror("unknown port %s", n);
7372 			return (-1);
7373 		}
7374 		return (s->s_port);
7375 	}
7376 }
7377 
7378 int
7379 rule_label(struct pfctl_rule *r, char *s[PF_RULE_MAX_LABEL_COUNT])
7380 {
7381 	for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++) {
7382 		if (s[i] == NULL)
7383 			return (0);
7384 
7385 		if (strlcpy(r->label[i], s[i], sizeof(r->label[0])) >=
7386 		    sizeof(r->label[0])) {
7387 			yyerror("rule label too long (max %d chars)",
7388 			    sizeof(r->label[0])-1);
7389 			return (-1);
7390 		}
7391 	}
7392 	return (0);
7393 }
7394 
7395 int
7396 eth_rule_label(struct pfctl_eth_rule *r, char *s[PF_RULE_MAX_LABEL_COUNT])
7397 {
7398 	for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++) {
7399 		if (s[i] == NULL)
7400 			return (0);
7401 
7402 		if (strlcpy(r->label[i], s[i], sizeof(r->label[0])) >=
7403 		    sizeof(r->label[0])) {
7404 			yyerror("rule label too long (max %d chars)",
7405 			    sizeof(r->label[0])-1);
7406 			return (-1);
7407 		}
7408 	}
7409 	return (0);
7410 }
7411 
7412 u_int16_t
7413 parseicmpspec(char *w, sa_family_t af)
7414 {
7415 	const struct icmpcodeent	*p;
7416 	u_long				 ulval;
7417 	u_int8_t			 icmptype;
7418 
7419 	if (af == AF_INET)
7420 		icmptype = returnicmpdefault >> 8;
7421 	else
7422 		icmptype = returnicmp6default >> 8;
7423 
7424 	if (atoul(w, &ulval) == -1) {
7425 		if ((p = geticmpcodebyname(icmptype, w, af)) == NULL) {
7426 			yyerror("unknown icmp code %s", w);
7427 			return (0);
7428 		}
7429 		ulval = p->code;
7430 	}
7431 	if (ulval > 255) {
7432 		yyerror("invalid icmp code %lu", ulval);
7433 		return (0);
7434 	}
7435 	return (icmptype << 8 | ulval);
7436 }
7437 
7438 int
7439 parseport(char *port, struct range *r, int extensions)
7440 {
7441 	char	*p = strchr(port, ':');
7442 
7443 	if (p == NULL) {
7444 		if ((r->a = getservice(port)) == -1)
7445 			return (-1);
7446 		r->b = 0;
7447 		r->t = PF_OP_NONE;
7448 		return (0);
7449 	}
7450 	if ((extensions & PPORT_STAR) && !strcmp(p+1, "*")) {
7451 		*p = 0;
7452 		if ((r->a = getservice(port)) == -1)
7453 			return (-1);
7454 		r->b = 0;
7455 		r->t = PF_OP_IRG;
7456 		return (0);
7457 	}
7458 	if ((extensions & PPORT_RANGE)) {
7459 		*p++ = 0;
7460 		if ((r->a = getservice(port)) == -1 ||
7461 		    (r->b = getservice(p)) == -1)
7462 			return (-1);
7463 		if (r->a == r->b) {
7464 			r->b = 0;
7465 			r->t = PF_OP_NONE;
7466 		} else
7467 			r->t = PF_OP_RRG;
7468 		return (0);
7469 	}
7470 	return (-1);
7471 }
7472 
7473 int
7474 pfctl_load_anchors(int dev, struct pfctl *pf, struct pfr_buffer *trans)
7475 {
7476 	struct loadanchors	*la;
7477 
7478 	TAILQ_FOREACH(la, &loadanchorshead, entries) {
7479 		if (pf->opts & PF_OPT_VERBOSE)
7480 			fprintf(stderr, "\nLoading anchor %s from %s\n",
7481 			    la->anchorname, la->filename);
7482 		if (pfctl_rules(dev, la->filename, pf->opts, pf->optimize,
7483 		    la->anchorname, trans) == -1)
7484 			return (-1);
7485 	}
7486 
7487 	return (0);
7488 }
7489 
7490 int
7491 kw_casecmp(const void *k, const void *e)
7492 {
7493 	return (strcasecmp(k, ((const struct keywords *)e)->k_name));
7494 }
7495 
7496 int
7497 map_tos(char *s, int *val)
7498 {
7499 	/* DiffServ Codepoints and other TOS mappings */
7500 	const struct keywords	 toswords[] = {
7501 		{ "af11",		IPTOS_DSCP_AF11 },
7502 		{ "af12",		IPTOS_DSCP_AF12 },
7503 		{ "af13",		IPTOS_DSCP_AF13 },
7504 		{ "af21",		IPTOS_DSCP_AF21 },
7505 		{ "af22",		IPTOS_DSCP_AF22 },
7506 		{ "af23",		IPTOS_DSCP_AF23 },
7507 		{ "af31",		IPTOS_DSCP_AF31 },
7508 		{ "af32",		IPTOS_DSCP_AF32 },
7509 		{ "af33",		IPTOS_DSCP_AF33 },
7510 		{ "af41",		IPTOS_DSCP_AF41 },
7511 		{ "af42",		IPTOS_DSCP_AF42 },
7512 		{ "af43",		IPTOS_DSCP_AF43 },
7513 		{ "critical",		IPTOS_PREC_CRITIC_ECP },
7514 		{ "cs0",		IPTOS_DSCP_CS0 },
7515 		{ "cs1",		IPTOS_DSCP_CS1 },
7516 		{ "cs2",		IPTOS_DSCP_CS2 },
7517 		{ "cs3",		IPTOS_DSCP_CS3 },
7518 		{ "cs4",		IPTOS_DSCP_CS4 },
7519 		{ "cs5",		IPTOS_DSCP_CS5 },
7520 		{ "cs6",		IPTOS_DSCP_CS6 },
7521 		{ "cs7",		IPTOS_DSCP_CS7 },
7522 		{ "ef",			IPTOS_DSCP_EF },
7523 		{ "inetcontrol",	IPTOS_PREC_INTERNETCONTROL },
7524 		{ "lowdelay",		IPTOS_LOWDELAY },
7525 		{ "netcontrol",		IPTOS_PREC_NETCONTROL },
7526 		{ "reliability",	IPTOS_RELIABILITY },
7527 		{ "throughput",		IPTOS_THROUGHPUT },
7528 		{ "va",			IPTOS_DSCP_VA }
7529 	};
7530 	const struct keywords	*p;
7531 
7532 	p = bsearch(s, toswords, sizeof(toswords)/sizeof(toswords[0]),
7533 	    sizeof(toswords[0]), kw_casecmp);
7534 
7535 	if (p) {
7536 		*val = p->k_val;
7537 		return (1);
7538 	}
7539 	return (0);
7540 }
7541 
7542 int
7543 rt_tableid_max(void)
7544 {
7545 #ifdef __FreeBSD__
7546 	int fibs;
7547 	size_t l = sizeof(fibs);
7548 
7549         if (sysctlbyname("net.fibs", &fibs, &l, NULL, 0) == -1)
7550 		fibs = 16;	/* XXX RT_MAXFIBS, at least limit it some. */
7551 	/*
7552 	 * As the OpenBSD code only compares > and not >= we need to adjust
7553 	 * here given we only accept values of 0..n and want to avoid #ifdefs
7554 	 * in the grammar.
7555 	 */
7556 	return (fibs - 1);
7557 #else
7558 	return (RT_TABLEID_MAX);
7559 #endif
7560 }
7561 
7562 struct node_mac*
7563 node_mac_from_string(const char *str)
7564 {
7565 	struct node_mac *m;
7566 
7567 	m = calloc(1, sizeof(struct node_mac));
7568 	if (m == NULL)
7569 		err(1, "%s: calloc", __func__);
7570 
7571 	if (sscanf(str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
7572 	    &m->mac[0], &m->mac[1], &m->mac[2], &m->mac[3], &m->mac[4],
7573 	    &m->mac[5]) != 6) {
7574 		free(m);
7575 		yyerror("invalid MAC address");
7576 		return (NULL);
7577 	}
7578 
7579 	memset(m->mask, 0xff, ETHER_ADDR_LEN);
7580 	m->isset = true;
7581 	m->next = NULL;
7582 	m->tail = m;
7583 
7584 	return (m);
7585 }
7586 
7587 struct node_mac*
7588 node_mac_from_string_masklen(const char *str, int masklen)
7589 {
7590 	struct node_mac *m;
7591 
7592 	if (masklen < 0 || masklen > (ETHER_ADDR_LEN * 8)) {
7593 		yyerror("invalid MAC mask length");
7594 		return (NULL);
7595 	}
7596 
7597 	m = node_mac_from_string(str);
7598 	if (m == NULL)
7599 		return (NULL);
7600 
7601 	memset(m->mask, 0, ETHER_ADDR_LEN);
7602 	for (int i = 0; i < masklen; i++)
7603 		m->mask[i / 8] |= 1 << (i % 8);
7604 
7605 	return (m);
7606 }
7607 
7608 struct node_mac*
7609 node_mac_from_string_mask(const char *str, const char *mask)
7610 {
7611 	struct node_mac *m;
7612 
7613 	m = node_mac_from_string(str);
7614 	if (m == NULL)
7615 		return (NULL);
7616 
7617 	if (sscanf(mask, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
7618 	    &m->mask[0], &m->mask[1], &m->mask[2], &m->mask[3], &m->mask[4],
7619 	    &m->mask[5]) != 6) {
7620 		free(m);
7621 		yyerror("invalid MAC mask");
7622 		return (NULL);
7623 	}
7624 
7625 	return (m);
7626 }
7627 
7628 int
7629 filteropts_to_rule(struct pfctl_rule *r, struct filter_opts *opts)
7630 {
7631 	r->keep_state = opts->keep.action;
7632 	r->pktrate.limit = opts->pktrate.limit;
7633 	r->pktrate.seconds = opts->pktrate.seconds;
7634 	r->prob = opts->prob;
7635 	r->rtableid = opts->rtableid;
7636 	r->ridentifier = opts->ridentifier;
7637 	r->max_pkt_size = opts->max_pkt_size;
7638 	r->tos = opts->tos;
7639 
7640 	if (opts->nodf)
7641 		r->scrub_flags |= PFSTATE_NODF;
7642 	if (opts->randomid)
7643 		r->scrub_flags |= PFSTATE_RANDOMID;
7644 	if (opts->minttl)
7645 		r->min_ttl = opts->minttl;
7646 	if (opts->max_mss)
7647 		r->max_mss = opts->max_mss;
7648 
7649 	if (opts->tag)
7650 		if (strlcpy(r->tagname, opts->tag,
7651 		    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
7652 			yyerror("tag too long, max %u chars",
7653 			    PF_TAG_NAME_SIZE - 1);
7654 			return (1);
7655 		}
7656 	if (opts->match_tag)
7657 		if (strlcpy(r->match_tagname, opts->match_tag,
7658 		    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
7659 			yyerror("tag too long, max %u chars",
7660 			    PF_TAG_NAME_SIZE - 1);
7661 			return (1);
7662 		}
7663 	r->match_tag_not = opts->match_tag_not;
7664 
7665 	if (rule_label(r, opts->label))
7666 		return (1);
7667 	for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++)
7668 		free(opts->label[i]);
7669 
7670 	if (opts->marker & FOM_AFTO)
7671 		r->rule_flag |= PFRULE_AFTO;
7672 	if (opts->marker & FOM_SCRUB_TCP)
7673 		r->scrub_flags |= PFSTATE_SCRUB_TCP;
7674 	if (opts->marker & FOM_PRIO)
7675 		r->prio = opts->prio ? opts->prio : PF_PRIO_ZERO;
7676 	if (opts->marker & FOM_SETPRIO) {
7677 		r->set_prio[0] = opts->set_prio[0];
7678 		r->set_prio[1] = opts->set_prio[1];
7679 		r->scrub_flags |= PFSTATE_SETPRIO;
7680 	}
7681 	if (opts->marker & FOM_SETTOS) {
7682 		r->scrub_flags |= PFSTATE_SETTOS;
7683 		r->set_tos = opts->settos;
7684 	}
7685 
7686 	r->flags = opts->flags.b1;
7687 	r->flagset = opts->flags.b2;
7688 	if ((opts->flags.b1 & opts->flags.b2) != opts->flags.b1) {
7689 		yyerror("flags always false");
7690 		return (1);
7691 	}
7692 
7693 	if (opts->queues.qname != NULL) {
7694 		if (strlcpy(r->qname, opts->queues.qname,
7695 		    sizeof(r->qname)) >= sizeof(r->qname)) {
7696 			yyerror("rule qname too long (max "
7697 			    "%d chars)", sizeof(r->qname)-1);
7698 			return (1);
7699 		}
7700 		free(opts->queues.qname);
7701 	}
7702 	if (opts->queues.pqname != NULL) {
7703 		if (strlcpy(r->pqname, opts->queues.pqname,
7704 		    sizeof(r->pqname)) >= sizeof(r->pqname)) {
7705 			yyerror("rule pqname too long (max "
7706 			    "%d chars)", sizeof(r->pqname)-1);
7707 			return (1);
7708 		}
7709 		free(opts->queues.pqname);
7710 	}
7711 
7712 	if (opts->fragment)
7713 		r->rule_flag |= PFRULE_FRAGMENT;
7714 	r->allow_opts = opts->allowopts;
7715 
7716 	return (0);
7717 }
7718