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