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