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