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