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