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