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