xref: /freebsd/sbin/pfctl/parse.y (revision 6562157dfad0d18447cfcac08435f7ffdb8fa46c)
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 MATCHES
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 NE LE GE
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 		| MATCHES		{ $$.log = PF_LOG_MATCHES; $$.logif = 0; }
3226 		| USER		{ $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
3227 		| GROUP		{ $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
3228 		| TO string	{
3229 			const char	*errstr;
3230 			u_int		 i;
3231 
3232 			$$.log = 0;
3233 			if (strncmp($2, "pflog", 5)) {
3234 				yyerror("%s: should be a pflog interface", $2);
3235 				free($2);
3236 				YYERROR;
3237 			}
3238 			i = strtonum($2 + 5, 0, 255, &errstr);
3239 			if (errstr) {
3240 				yyerror("%s: %s", $2, errstr);
3241 				free($2);
3242 				YYERROR;
3243 			}
3244 			free($2);
3245 			$$.logif = i;
3246 		}
3247 		;
3248 
3249 interface	: /* empty */			{ $$ = NULL; }
3250 		| ON if_item_not		{ $$ = $2; }
3251 		| ON '{' optnl if_list '}'	{ $$ = $4; }
3252 		;
3253 
3254 if_list		: if_item_not optnl		{ $$ = $1; }
3255 		| if_list comma if_item_not optnl	{
3256 			$1->tail->next = $3;
3257 			$1->tail = $3;
3258 			$$ = $1;
3259 		}
3260 		;
3261 
3262 if_item_not	: not if_item			{ $$ = $2; $$->not = $1; }
3263 		;
3264 
3265 if_item		: STRING			{
3266 			struct node_host	*n;
3267 
3268 			$$ = calloc(1, sizeof(struct node_if));
3269 			if ($$ == NULL)
3270 				err(1, "if_item: calloc");
3271 			if (strlcpy($$->ifname, $1, sizeof($$->ifname)) >=
3272 			    sizeof($$->ifname)) {
3273 				free($1);
3274 				free($$);
3275 				yyerror("interface name too long");
3276 				YYERROR;
3277 			}
3278 
3279 			if ((n = ifa_exists($1)) != NULL)
3280 				$$->ifa_flags = n->ifa_flags;
3281 
3282 			free($1);
3283 			$$->not = 0;
3284 			$$->next = NULL;
3285 			$$->tail = $$;
3286 		}
3287 		;
3288 
3289 af		: /* empty */			{ $$ = 0; }
3290 		| INET				{ $$ = AF_INET; }
3291 		| INET6				{ $$ = AF_INET6; }
3292 		;
3293 
3294 etherproto	: /* empty */				{ $$ = NULL; }
3295 		| PROTO etherproto_item			{ $$ = $2; }
3296 		| PROTO '{' optnl etherproto_list '}'	{ $$ = $4; }
3297 		;
3298 
3299 etherproto_list	: etherproto_item optnl			{ $$ = $1; }
3300 		| etherproto_list comma etherproto_item optnl	{
3301 			$1->tail->next = $3;
3302 			$1->tail = $3;
3303 			$$ = $1;
3304 		}
3305 		;
3306 
3307 etherproto_item	: etherprotoval		{
3308 			u_int16_t	pr;
3309 
3310 			pr = (u_int16_t)$1;
3311 			if (pr == 0) {
3312 				yyerror("proto 0 cannot be used");
3313 				YYERROR;
3314 			}
3315 			$$ = calloc(1, sizeof(struct node_proto));
3316 			if ($$ == NULL)
3317 				err(1, "proto_item: calloc");
3318 			$$->proto = pr;
3319 			$$->next = NULL;
3320 			$$->tail = $$;
3321 		}
3322 		;
3323 
3324 etherprotoval	: NUMBER			{
3325 			if ($1 < 0 || $1 > 65565) {
3326 				yyerror("protocol outside range");
3327 				YYERROR;
3328 			}
3329 		}
3330 		| STRING
3331 		{
3332 			if (!strncmp($1, "0x", 2)) {
3333 				if (sscanf($1, "0x%4x", &$$) != 1) {
3334 					free($1);
3335 					yyerror("invalid EtherType hex");
3336 					YYERROR;
3337 				}
3338 			} else {
3339 				yyerror("Symbolic EtherType not yet supported");
3340 			}
3341 		}
3342 		;
3343 
3344 proto		: /* empty */				{ $$ = NULL; }
3345 		| PROTO proto_item			{ $$ = $2; }
3346 		| PROTO '{' optnl proto_list '}'	{ $$ = $4; }
3347 		;
3348 
3349 proto_list	: proto_item optnl		{ $$ = $1; }
3350 		| proto_list comma proto_item optnl	{
3351 			$1->tail->next = $3;
3352 			$1->tail = $3;
3353 			$$ = $1;
3354 		}
3355 		;
3356 
3357 proto_item	: protoval			{
3358 			u_int8_t	pr;
3359 
3360 			pr = (u_int8_t)$1;
3361 			if (pr == 0) {
3362 				yyerror("proto 0 cannot be used");
3363 				YYERROR;
3364 			}
3365 			$$ = calloc(1, sizeof(struct node_proto));
3366 			if ($$ == NULL)
3367 				err(1, "proto_item: calloc");
3368 			$$->proto = pr;
3369 			$$->next = NULL;
3370 			$$->tail = $$;
3371 		}
3372 		;
3373 
3374 protoval	: STRING			{
3375 			struct protoent	*p;
3376 
3377 			p = getprotobyname($1);
3378 			if (p == NULL) {
3379 				yyerror("unknown protocol %s", $1);
3380 				free($1);
3381 				YYERROR;
3382 			}
3383 			$$ = p->p_proto;
3384 			free($1);
3385 		}
3386 		| NUMBER			{
3387 			if ($1 < 0 || $1 > 255) {
3388 				yyerror("protocol outside range");
3389 				YYERROR;
3390 			}
3391 		}
3392 		;
3393 
3394 l3fromto	: /* empty */			{
3395 			bzero(&$$, sizeof($$));
3396 		}
3397 		| L3 fromto			{
3398 			if ($2.src.host != NULL &&
3399 			    $2.src.host->addr.type != PF_ADDR_ADDRMASK &&
3400 			    $2.src.host->addr.type != PF_ADDR_TABLE) {
3401 				yyerror("from must be an address or table");
3402 				YYERROR;
3403 			}
3404 			if ($2.dst.host != NULL &&
3405 			    $2.dst.host->addr.type != PF_ADDR_ADDRMASK &&
3406 			    $2.dst.host->addr.type != PF_ADDR_TABLE) {
3407 				yyerror("to must be an address or table");
3408 				YYERROR;
3409 			}
3410 			$$ = $2;
3411 		}
3412 		;
3413 etherfromto	: ALL				{
3414 			$$.src = NULL;
3415 			$$.dst = NULL;
3416 		}
3417 		| etherfrom etherto		{
3418 			$$.src = $1.mac;
3419 			$$.dst = $2.mac;
3420 		}
3421 		;
3422 
3423 etherfrom	: /* emtpy */			{
3424 			bzero(&$$, sizeof($$));
3425 		}
3426 		| FROM macspec			{
3427 			$$.mac = $2;
3428 		}
3429 		;
3430 
3431 etherto		: /* empty */			{
3432 			bzero(&$$, sizeof($$));
3433 		}
3434 		| TO macspec			{
3435 			$$.mac = $2;
3436 		}
3437 		;
3438 
3439 mac		: string '/' NUMBER		{
3440 			$$ = node_mac_from_string_masklen($1, $3);
3441 			free($1);
3442 			if ($$ == NULL)
3443 				YYERROR;
3444 		}
3445 		| string			{
3446 			if (strchr($1, '&')) {
3447 				/* mac&mask */
3448 				char *mac = strtok($1, "&");
3449 				char *mask = strtok(NULL, "&");
3450 				$$ = node_mac_from_string_mask(mac, mask);
3451 			} else {
3452 				$$ = node_mac_from_string($1);
3453 			}
3454 			free($1);
3455 			if ($$ == NULL)
3456 				YYERROR;
3457 
3458 		}
3459 xmac		: not mac {
3460 			struct node_mac	*n;
3461 
3462 			for (n = $2; n != NULL; n = n->next)
3463 				n->neg = $1;
3464 			$$ = $2;
3465 		}
3466 		;
3467 macspec		: xmac {
3468 			$$ = $1;
3469 		}
3470 		| '{' optnl mac_list '}'
3471 		{
3472 			$$ = $3;
3473 		}
3474 		;
3475 mac_list	: xmac optnl {
3476 			$$ = $1;
3477 		}
3478 		| mac_list comma xmac {
3479 			if ($3 == NULL)
3480 				$$ = $1;
3481 			else if ($1 == NULL)
3482 				$$ = $3;
3483 			else {
3484 				$1->tail->next = $3;
3485 				$1->tail = $3->tail;
3486 				$$ = $1;
3487 			}
3488 		}
3489 
3490 fromto		: ALL				{
3491 			$$.src.host = NULL;
3492 			$$.src.port = NULL;
3493 			$$.dst.host = NULL;
3494 			$$.dst.port = NULL;
3495 			$$.src_os = NULL;
3496 		}
3497 		| from os to			{
3498 			$$.src = $1;
3499 			$$.src_os = $2;
3500 			$$.dst = $3;
3501 		}
3502 		;
3503 
3504 os		: /* empty */			{ $$ = NULL; }
3505 		| OS xos			{ $$ = $2; }
3506 		| OS '{' optnl os_list '}'	{ $$ = $4; }
3507 		;
3508 
3509 xos		: STRING {
3510 			$$ = calloc(1, sizeof(struct node_os));
3511 			if ($$ == NULL)
3512 				err(1, "os: calloc");
3513 			$$->os = $1;
3514 			$$->tail = $$;
3515 		}
3516 		;
3517 
3518 os_list		: xos optnl 			{ $$ = $1; }
3519 		| os_list comma xos optnl	{
3520 			$1->tail->next = $3;
3521 			$1->tail = $3;
3522 			$$ = $1;
3523 		}
3524 		;
3525 
3526 from		: /* empty */			{
3527 			$$.host = NULL;
3528 			$$.port = NULL;
3529 		}
3530 		| FROM ipportspec		{
3531 			$$ = $2;
3532 		}
3533 		;
3534 
3535 to		: /* empty */			{
3536 			$$.host = NULL;
3537 			$$.port = NULL;
3538 		}
3539 		| TO ipportspec		{
3540 			if (disallow_urpf_failed($2.host, "\"urpf-failed\" is "
3541 			    "not permitted in a destination address"))
3542 				YYERROR;
3543 			$$ = $2;
3544 		}
3545 		;
3546 
3547 ipportspec	: ipspec			{
3548 			$$.host = $1;
3549 			$$.port = NULL;
3550 		}
3551 		| ipspec PORT portspec		{
3552 			$$.host = $1;
3553 			$$.port = $3;
3554 		}
3555 		| PORT portspec			{
3556 			$$.host = NULL;
3557 			$$.port = $2;
3558 		}
3559 		;
3560 
3561 optnl		: '\n' optnl
3562 		|
3563 		;
3564 
3565 ipspec		: ANY				{ $$ = NULL; }
3566 		| xhost				{ $$ = $1; }
3567 		| '{' optnl host_list '}'	{ $$ = $3; }
3568 		;
3569 
3570 toipspec	: TO ipspec			{ $$ = $2; }
3571 		| /* empty */			{ $$ = NULL; }
3572 		;
3573 
3574 host_list	: ipspec optnl			{ $$ = $1; }
3575 		| host_list comma ipspec optnl	{
3576 			if ($1 == NULL) {
3577 				freehostlist($3);
3578 				$$ = $1;
3579 			} else if ($3 == NULL) {
3580 				freehostlist($1);
3581 				$$ = $3;
3582 			} else {
3583 				$1->tail->next = $3;
3584 				$1->tail = $3->tail;
3585 				$$ = $1;
3586 			}
3587 		}
3588 		;
3589 
3590 xhost		: not host			{
3591 			struct node_host	*n;
3592 
3593 			for (n = $2; n != NULL; n = n->next)
3594 				n->not = $1;
3595 			$$ = $2;
3596 		}
3597 		| not NOROUTE			{
3598 			$$ = calloc(1, sizeof(struct node_host));
3599 			if ($$ == NULL)
3600 				err(1, "xhost: calloc");
3601 			$$->addr.type = PF_ADDR_NOROUTE;
3602 			$$->next = NULL;
3603 			$$->not = $1;
3604 			$$->tail = $$;
3605 		}
3606 		| not URPFFAILED		{
3607 			$$ = calloc(1, sizeof(struct node_host));
3608 			if ($$ == NULL)
3609 				err(1, "xhost: calloc");
3610 			$$->addr.type = PF_ADDR_URPFFAILED;
3611 			$$->next = NULL;
3612 			$$->not = $1;
3613 			$$->tail = $$;
3614 		}
3615 		;
3616 
3617 host		: STRING			{
3618 			if (($$ = host($1)) == NULL)	{
3619 				/* error. "any" is handled elsewhere */
3620 				free($1);
3621 				yyerror("could not parse host specification");
3622 				YYERROR;
3623 			}
3624 			free($1);
3625 
3626 		}
3627 		| STRING '-' STRING		{
3628 			struct node_host *b, *e;
3629 
3630 			if ((b = host($1)) == NULL || (e = host($3)) == NULL) {
3631 				free($1);
3632 				free($3);
3633 				yyerror("could not parse host specification");
3634 				YYERROR;
3635 			}
3636 			if (b->af != e->af ||
3637 			    b->addr.type != PF_ADDR_ADDRMASK ||
3638 			    e->addr.type != PF_ADDR_ADDRMASK ||
3639 			    unmask(&b->addr.v.a.mask, b->af) !=
3640 			    (b->af == AF_INET ? 32 : 128) ||
3641 			    unmask(&e->addr.v.a.mask, e->af) !=
3642 			    (e->af == AF_INET ? 32 : 128) ||
3643 			    b->next != NULL || b->not ||
3644 			    e->next != NULL || e->not) {
3645 				free(b);
3646 				free(e);
3647 				free($1);
3648 				free($3);
3649 				yyerror("invalid address range");
3650 				YYERROR;
3651 			}
3652 			memcpy(&b->addr.v.a.mask, &e->addr.v.a.addr,
3653 			    sizeof(b->addr.v.a.mask));
3654 			b->addr.type = PF_ADDR_RANGE;
3655 			$$ = b;
3656 			free(e);
3657 			free($1);
3658 			free($3);
3659 		}
3660 		| STRING '/' NUMBER		{
3661 			char	*buf;
3662 
3663 			if (asprintf(&buf, "%s/%lld", $1, (long long)$3) == -1)
3664 				err(1, "host: asprintf");
3665 			free($1);
3666 			if (($$ = host(buf)) == NULL)	{
3667 				/* error. "any" is handled elsewhere */
3668 				free(buf);
3669 				yyerror("could not parse host specification");
3670 				YYERROR;
3671 			}
3672 			free(buf);
3673 		}
3674 		| NUMBER '/' NUMBER		{
3675 			char	*buf;
3676 
3677 			/* ie. for 10/8 parsing */
3678 #ifdef __FreeBSD__
3679 			if (asprintf(&buf, "%lld/%lld", (long long)$1, (long long)$3) == -1)
3680 #else
3681 			if (asprintf(&buf, "%lld/%lld", $1, $3) == -1)
3682 #endif
3683 				err(1, "host: asprintf");
3684 			if (($$ = host(buf)) == NULL)	{
3685 				/* error. "any" is handled elsewhere */
3686 				free(buf);
3687 				yyerror("could not parse host specification");
3688 				YYERROR;
3689 			}
3690 			free(buf);
3691 		}
3692 		| dynaddr
3693 		| dynaddr '/' NUMBER		{
3694 			struct node_host	*n;
3695 
3696 			if ($3 < 0 || $3 > 128) {
3697 				yyerror("bit number too big");
3698 				YYERROR;
3699 			}
3700 			$$ = $1;
3701 			for (n = $1; n != NULL; n = n->next)
3702 				set_ipmask(n, $3);
3703 		}
3704 		| '<' STRING '>'	{
3705 			if (strlen($2) >= PF_TABLE_NAME_SIZE) {
3706 				yyerror("table name '%s' too long", $2);
3707 				free($2);
3708 				YYERROR;
3709 			}
3710 			$$ = calloc(1, sizeof(struct node_host));
3711 			if ($$ == NULL)
3712 				err(1, "host: calloc");
3713 			$$->addr.type = PF_ADDR_TABLE;
3714 			if (strlcpy($$->addr.v.tblname, $2,
3715 			    sizeof($$->addr.v.tblname)) >=
3716 			    sizeof($$->addr.v.tblname))
3717 				errx(1, "host: strlcpy");
3718 			free($2);
3719 			$$->next = NULL;
3720 			$$->tail = $$;
3721 		}
3722 		;
3723 
3724 number		: NUMBER
3725 		| STRING		{
3726 			u_long	ulval;
3727 
3728 			if (atoul($1, &ulval) == -1) {
3729 				yyerror("%s is not a number", $1);
3730 				free($1);
3731 				YYERROR;
3732 			} else
3733 				$$ = ulval;
3734 			free($1);
3735 		}
3736 		;
3737 
3738 dynaddr		: '(' STRING ')'		{
3739 			int	 flags = 0;
3740 			char	*p, *op;
3741 
3742 			op = $2;
3743 			if (!isalpha(op[0])) {
3744 				yyerror("invalid interface name '%s'", op);
3745 				free(op);
3746 				YYERROR;
3747 			}
3748 			while ((p = strrchr($2, ':')) != NULL) {
3749 				if (!strcmp(p+1, "network"))
3750 					flags |= PFI_AFLAG_NETWORK;
3751 				else if (!strcmp(p+1, "broadcast"))
3752 					flags |= PFI_AFLAG_BROADCAST;
3753 				else if (!strcmp(p+1, "peer"))
3754 					flags |= PFI_AFLAG_PEER;
3755 				else if (!strcmp(p+1, "0"))
3756 					flags |= PFI_AFLAG_NOALIAS;
3757 				else {
3758 					yyerror("interface %s has bad modifier",
3759 					    $2);
3760 					free(op);
3761 					YYERROR;
3762 				}
3763 				*p = '\0';
3764 			}
3765 			if (flags & (flags - 1) & PFI_AFLAG_MODEMASK) {
3766 				free(op);
3767 				yyerror("illegal combination of "
3768 				    "interface modifiers");
3769 				YYERROR;
3770 			}
3771 			$$ = calloc(1, sizeof(struct node_host));
3772 			if ($$ == NULL)
3773 				err(1, "address: calloc");
3774 			$$->af = 0;
3775 			set_ipmask($$, 128);
3776 			$$->addr.type = PF_ADDR_DYNIFTL;
3777 			$$->addr.iflags = flags;
3778 			if (strlcpy($$->addr.v.ifname, $2,
3779 			    sizeof($$->addr.v.ifname)) >=
3780 			    sizeof($$->addr.v.ifname)) {
3781 				free(op);
3782 				free($$);
3783 				yyerror("interface name too long");
3784 				YYERROR;
3785 			}
3786 			free(op);
3787 			$$->next = NULL;
3788 			$$->tail = $$;
3789 		}
3790 		;
3791 
3792 portspec	: port_item			{ $$ = $1; }
3793 		| '{' optnl port_list '}'	{ $$ = $3; }
3794 		;
3795 
3796 port_list	: port_item optnl		{ $$ = $1; }
3797 		| port_list comma port_item optnl	{
3798 			$1->tail->next = $3;
3799 			$1->tail = $3;
3800 			$$ = $1;
3801 		}
3802 		;
3803 
3804 port_item	: portrange			{
3805 			$$ = calloc(1, sizeof(struct node_port));
3806 			if ($$ == NULL)
3807 				err(1, "port_item: calloc");
3808 			$$->port[0] = $1.a;
3809 			$$->port[1] = $1.b;
3810 			if ($1.t)
3811 				$$->op = PF_OP_RRG;
3812 			else
3813 				$$->op = PF_OP_EQ;
3814 			$$->next = NULL;
3815 			$$->tail = $$;
3816 		}
3817 		| unaryop portrange	{
3818 			if ($2.t) {
3819 				yyerror("':' cannot be used with an other "
3820 				    "port operator");
3821 				YYERROR;
3822 			}
3823 			$$ = calloc(1, sizeof(struct node_port));
3824 			if ($$ == NULL)
3825 				err(1, "port_item: calloc");
3826 			$$->port[0] = $2.a;
3827 			$$->port[1] = $2.b;
3828 			$$->op = $1;
3829 			$$->next = NULL;
3830 			$$->tail = $$;
3831 		}
3832 		| portrange PORTBINARY portrange	{
3833 			if ($1.t || $3.t) {
3834 				yyerror("':' cannot be used with an other "
3835 				    "port operator");
3836 				YYERROR;
3837 			}
3838 			$$ = calloc(1, sizeof(struct node_port));
3839 			if ($$ == NULL)
3840 				err(1, "port_item: calloc");
3841 			$$->port[0] = $1.a;
3842 			$$->port[1] = $3.a;
3843 			$$->op = $2;
3844 			$$->next = NULL;
3845 			$$->tail = $$;
3846 		}
3847 		;
3848 
3849 portplain	: numberstring			{
3850 			if (parseport($1, &$$, 0) == -1) {
3851 				free($1);
3852 				YYERROR;
3853 			}
3854 			free($1);
3855 		}
3856 		;
3857 
3858 portrange	: numberstring			{
3859 			if (parseport($1, &$$, PPORT_RANGE) == -1) {
3860 				free($1);
3861 				YYERROR;
3862 			}
3863 			free($1);
3864 		}
3865 		;
3866 
3867 uids		: uid_item			{ $$ = $1; }
3868 		| '{' optnl uid_list '}'	{ $$ = $3; }
3869 		;
3870 
3871 uid_list	: uid_item optnl		{ $$ = $1; }
3872 		| uid_list comma uid_item optnl	{
3873 			$1->tail->next = $3;
3874 			$1->tail = $3;
3875 			$$ = $1;
3876 		}
3877 		;
3878 
3879 uid_item	: uid				{
3880 			$$ = calloc(1, sizeof(struct node_uid));
3881 			if ($$ == NULL)
3882 				err(1, "uid_item: calloc");
3883 			$$->uid[0] = $1;
3884 			$$->uid[1] = $1;
3885 			$$->op = PF_OP_EQ;
3886 			$$->next = NULL;
3887 			$$->tail = $$;
3888 		}
3889 		| unaryop uid			{
3890 			if ($2 == UID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
3891 				yyerror("user unknown requires operator = or "
3892 				    "!=");
3893 				YYERROR;
3894 			}
3895 			$$ = calloc(1, sizeof(struct node_uid));
3896 			if ($$ == NULL)
3897 				err(1, "uid_item: calloc");
3898 			$$->uid[0] = $2;
3899 			$$->uid[1] = $2;
3900 			$$->op = $1;
3901 			$$->next = NULL;
3902 			$$->tail = $$;
3903 		}
3904 		| uid PORTBINARY uid		{
3905 			if ($1 == UID_MAX || $3 == UID_MAX) {
3906 				yyerror("user unknown requires operator = or "
3907 				    "!=");
3908 				YYERROR;
3909 			}
3910 			$$ = calloc(1, sizeof(struct node_uid));
3911 			if ($$ == NULL)
3912 				err(1, "uid_item: calloc");
3913 			$$->uid[0] = $1;
3914 			$$->uid[1] = $3;
3915 			$$->op = $2;
3916 			$$->next = NULL;
3917 			$$->tail = $$;
3918 		}
3919 		;
3920 
3921 uid		: STRING			{
3922 			if (!strcmp($1, "unknown"))
3923 				$$ = UID_MAX;
3924 			else {
3925 				struct passwd	*pw;
3926 
3927 				if ((pw = getpwnam($1)) == NULL) {
3928 					yyerror("unknown user %s", $1);
3929 					free($1);
3930 					YYERROR;
3931 				}
3932 				$$ = pw->pw_uid;
3933 			}
3934 			free($1);
3935 		}
3936 		| NUMBER			{
3937 			if ($1 < 0 || $1 >= UID_MAX) {
3938 				yyerror("illegal uid value %lu", $1);
3939 				YYERROR;
3940 			}
3941 			$$ = $1;
3942 		}
3943 		;
3944 
3945 gids		: gid_item			{ $$ = $1; }
3946 		| '{' optnl gid_list '}'	{ $$ = $3; }
3947 		;
3948 
3949 gid_list	: gid_item optnl		{ $$ = $1; }
3950 		| gid_list comma gid_item optnl	{
3951 			$1->tail->next = $3;
3952 			$1->tail = $3;
3953 			$$ = $1;
3954 		}
3955 		;
3956 
3957 gid_item	: gid				{
3958 			$$ = calloc(1, sizeof(struct node_gid));
3959 			if ($$ == NULL)
3960 				err(1, "gid_item: calloc");
3961 			$$->gid[0] = $1;
3962 			$$->gid[1] = $1;
3963 			$$->op = PF_OP_EQ;
3964 			$$->next = NULL;
3965 			$$->tail = $$;
3966 		}
3967 		| unaryop gid			{
3968 			if ($2 == GID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
3969 				yyerror("group unknown requires operator = or "
3970 				    "!=");
3971 				YYERROR;
3972 			}
3973 			$$ = calloc(1, sizeof(struct node_gid));
3974 			if ($$ == NULL)
3975 				err(1, "gid_item: calloc");
3976 			$$->gid[0] = $2;
3977 			$$->gid[1] = $2;
3978 			$$->op = $1;
3979 			$$->next = NULL;
3980 			$$->tail = $$;
3981 		}
3982 		| gid PORTBINARY gid		{
3983 			if ($1 == GID_MAX || $3 == GID_MAX) {
3984 				yyerror("group unknown requires operator = or "
3985 				    "!=");
3986 				YYERROR;
3987 			}
3988 			$$ = calloc(1, sizeof(struct node_gid));
3989 			if ($$ == NULL)
3990 				err(1, "gid_item: calloc");
3991 			$$->gid[0] = $1;
3992 			$$->gid[1] = $3;
3993 			$$->op = $2;
3994 			$$->next = NULL;
3995 			$$->tail = $$;
3996 		}
3997 		;
3998 
3999 gid		: STRING			{
4000 			if (!strcmp($1, "unknown"))
4001 				$$ = GID_MAX;
4002 			else {
4003 				struct group	*grp;
4004 
4005 				if ((grp = getgrnam($1)) == NULL) {
4006 					yyerror("unknown group %s", $1);
4007 					free($1);
4008 					YYERROR;
4009 				}
4010 				$$ = grp->gr_gid;
4011 			}
4012 			free($1);
4013 		}
4014 		| NUMBER			{
4015 			if ($1 < 0 || $1 >= GID_MAX) {
4016 				yyerror("illegal gid value %lu", $1);
4017 				YYERROR;
4018 			}
4019 			$$ = $1;
4020 		}
4021 		;
4022 
4023 flag		: STRING			{
4024 			int	f;
4025 
4026 			if ((f = parse_flags($1)) < 0) {
4027 				yyerror("bad flags %s", $1);
4028 				free($1);
4029 				YYERROR;
4030 			}
4031 			free($1);
4032 			$$.b1 = f;
4033 		}
4034 		;
4035 
4036 flags		: FLAGS flag '/' flag	{ $$.b1 = $2.b1; $$.b2 = $4.b1; }
4037 		| FLAGS '/' flag	{ $$.b1 = 0; $$.b2 = $3.b1; }
4038 		| FLAGS ANY		{ $$.b1 = 0; $$.b2 = 0; }
4039 		;
4040 
4041 icmpspec	: ICMPTYPE icmp_item			{ $$ = $2; }
4042 		| ICMPTYPE '{' optnl icmp_list '}'	{ $$ = $4; }
4043 		| ICMP6TYPE icmp6_item			{ $$ = $2; }
4044 		| ICMP6TYPE '{' optnl icmp6_list '}'	{ $$ = $4; }
4045 		;
4046 
4047 icmp_list	: icmp_item optnl		{ $$ = $1; }
4048 		| icmp_list comma icmp_item optnl {
4049 			$1->tail->next = $3;
4050 			$1->tail = $3;
4051 			$$ = $1;
4052 		}
4053 		;
4054 
4055 icmp6_list	: icmp6_item optnl		{ $$ = $1; }
4056 		| icmp6_list comma icmp6_item optnl {
4057 			$1->tail->next = $3;
4058 			$1->tail = $3;
4059 			$$ = $1;
4060 		}
4061 		;
4062 
4063 icmp_item	: icmptype		{
4064 			$$ = calloc(1, sizeof(struct node_icmp));
4065 			if ($$ == NULL)
4066 				err(1, "icmp_item: calloc");
4067 			$$->type = $1;
4068 			$$->code = 0;
4069 			$$->proto = IPPROTO_ICMP;
4070 			$$->next = NULL;
4071 			$$->tail = $$;
4072 		}
4073 		| icmptype CODE STRING	{
4074 			const struct icmpcodeent	*p;
4075 
4076 			if ((p = geticmpcodebyname($1-1, $3, AF_INET)) == NULL) {
4077 				yyerror("unknown icmp-code %s", $3);
4078 				free($3);
4079 				YYERROR;
4080 			}
4081 
4082 			free($3);
4083 			$$ = calloc(1, sizeof(struct node_icmp));
4084 			if ($$ == NULL)
4085 				err(1, "icmp_item: calloc");
4086 			$$->type = $1;
4087 			$$->code = p->code + 1;
4088 			$$->proto = IPPROTO_ICMP;
4089 			$$->next = NULL;
4090 			$$->tail = $$;
4091 		}
4092 		| icmptype CODE NUMBER	{
4093 			if ($3 < 0 || $3 > 255) {
4094 				yyerror("illegal icmp-code %lu", $3);
4095 				YYERROR;
4096 			}
4097 			$$ = calloc(1, sizeof(struct node_icmp));
4098 			if ($$ == NULL)
4099 				err(1, "icmp_item: calloc");
4100 			$$->type = $1;
4101 			$$->code = $3 + 1;
4102 			$$->proto = IPPROTO_ICMP;
4103 			$$->next = NULL;
4104 			$$->tail = $$;
4105 		}
4106 		;
4107 
4108 icmp6_item	: icmp6type		{
4109 			$$ = calloc(1, sizeof(struct node_icmp));
4110 			if ($$ == NULL)
4111 				err(1, "icmp_item: calloc");
4112 			$$->type = $1;
4113 			$$->code = 0;
4114 			$$->proto = IPPROTO_ICMPV6;
4115 			$$->next = NULL;
4116 			$$->tail = $$;
4117 		}
4118 		| icmp6type CODE STRING	{
4119 			const struct icmpcodeent	*p;
4120 
4121 			if ((p = geticmpcodebyname($1-1, $3, AF_INET6)) == NULL) {
4122 				yyerror("unknown icmp6-code %s", $3);
4123 				free($3);
4124 				YYERROR;
4125 			}
4126 			free($3);
4127 
4128 			$$ = calloc(1, sizeof(struct node_icmp));
4129 			if ($$ == NULL)
4130 				err(1, "icmp_item: calloc");
4131 			$$->type = $1;
4132 			$$->code = p->code + 1;
4133 			$$->proto = IPPROTO_ICMPV6;
4134 			$$->next = NULL;
4135 			$$->tail = $$;
4136 		}
4137 		| icmp6type CODE NUMBER	{
4138 			if ($3 < 0 || $3 > 255) {
4139 				yyerror("illegal icmp-code %lu", $3);
4140 				YYERROR;
4141 			}
4142 			$$ = calloc(1, sizeof(struct node_icmp));
4143 			if ($$ == NULL)
4144 				err(1, "icmp_item: calloc");
4145 			$$->type = $1;
4146 			$$->code = $3 + 1;
4147 			$$->proto = IPPROTO_ICMPV6;
4148 			$$->next = NULL;
4149 			$$->tail = $$;
4150 		}
4151 		;
4152 
4153 icmptype	: STRING			{
4154 			const struct icmptypeent	*p;
4155 
4156 			if ((p = geticmptypebyname($1, AF_INET)) == NULL) {
4157 				yyerror("unknown icmp-type %s", $1);
4158 				free($1);
4159 				YYERROR;
4160 			}
4161 			$$ = p->type + 1;
4162 			free($1);
4163 		}
4164 		| NUMBER			{
4165 			if ($1 < 0 || $1 > 255) {
4166 				yyerror("illegal icmp-type %lu", $1);
4167 				YYERROR;
4168 			}
4169 			$$ = $1 + 1;
4170 		}
4171 		;
4172 
4173 icmp6type	: STRING			{
4174 			const struct icmptypeent	*p;
4175 
4176 			if ((p = geticmptypebyname($1, AF_INET6)) ==
4177 			    NULL) {
4178 				yyerror("unknown icmp6-type %s", $1);
4179 				free($1);
4180 				YYERROR;
4181 			}
4182 			$$ = p->type + 1;
4183 			free($1);
4184 		}
4185 		| NUMBER			{
4186 			if ($1 < 0 || $1 > 255) {
4187 				yyerror("illegal icmp6-type %lu", $1);
4188 				YYERROR;
4189 			}
4190 			$$ = $1 + 1;
4191 		}
4192 		;
4193 
4194 tos	: STRING			{
4195 			int val;
4196 			char *end;
4197 
4198 			if (map_tos($1, &val))
4199 				$$ = val;
4200 			else if ($1[0] == '0' && $1[1] == 'x') {
4201 				errno = 0;
4202 				$$ = strtoul($1, &end, 16);
4203 				if (errno || *end != '\0')
4204 					$$ = 256;
4205 			} else
4206 				$$ = 256;		/* flag bad argument */
4207 			if ($$ < 0 || $$ > 255) {
4208 				yyerror("illegal tos value %s", $1);
4209 				free($1);
4210 				YYERROR;
4211 			}
4212 			free($1);
4213 		}
4214 		| NUMBER			{
4215 			$$ = $1;
4216 			if ($$ < 0 || $$ > 255) {
4217 				yyerror("illegal tos value %lu", $1);
4218 				YYERROR;
4219 			}
4220 		}
4221 		;
4222 
4223 sourcetrack	: SOURCETRACK		{ $$ = PF_SRCTRACK; }
4224 		| SOURCETRACK GLOBAL	{ $$ = PF_SRCTRACK_GLOBAL; }
4225 		| SOURCETRACK RULE	{ $$ = PF_SRCTRACK_RULE; }
4226 		;
4227 
4228 statelock	: IFBOUND {
4229 			$$ = PFRULE_IFBOUND;
4230 		}
4231 		| FLOATING {
4232 			$$ = 0;
4233 		}
4234 		;
4235 
4236 keep		: NO STATE			{
4237 			$$.action = 0;
4238 			$$.options = NULL;
4239 		}
4240 		| KEEP STATE state_opt_spec	{
4241 			$$.action = PF_STATE_NORMAL;
4242 			$$.options = $3;
4243 		}
4244 		| MODULATE STATE state_opt_spec {
4245 			$$.action = PF_STATE_MODULATE;
4246 			$$.options = $3;
4247 		}
4248 		| SYNPROXY STATE state_opt_spec {
4249 			$$.action = PF_STATE_SYNPROXY;
4250 			$$.options = $3;
4251 		}
4252 		;
4253 
4254 flush		: /* empty */			{ $$ = 0; }
4255 		| FLUSH				{ $$ = PF_FLUSH; }
4256 		| FLUSH GLOBAL			{
4257 			$$ = PF_FLUSH | PF_FLUSH_GLOBAL;
4258 		}
4259 		;
4260 
4261 state_opt_spec	: '(' state_opt_list ')'	{ $$ = $2; }
4262 		| /* empty */			{ $$ = NULL; }
4263 		;
4264 
4265 state_opt_list	: state_opt_item		{ $$ = $1; }
4266 		| state_opt_list comma state_opt_item {
4267 			$1->tail->next = $3;
4268 			$1->tail = $3;
4269 			$$ = $1;
4270 		}
4271 		;
4272 
4273 state_opt_item	: MAXIMUM NUMBER		{
4274 			if ($2 < 0 || $2 > UINT_MAX) {
4275 				yyerror("only positive values permitted");
4276 				YYERROR;
4277 			}
4278 			$$ = calloc(1, sizeof(struct node_state_opt));
4279 			if ($$ == NULL)
4280 				err(1, "state_opt_item: calloc");
4281 			$$->type = PF_STATE_OPT_MAX;
4282 			$$->data.max_states = $2;
4283 			$$->next = NULL;
4284 			$$->tail = $$;
4285 		}
4286 		| NOSYNC				{
4287 			$$ = calloc(1, sizeof(struct node_state_opt));
4288 			if ($$ == NULL)
4289 				err(1, "state_opt_item: calloc");
4290 			$$->type = PF_STATE_OPT_NOSYNC;
4291 			$$->next = NULL;
4292 			$$->tail = $$;
4293 		}
4294 		| MAXSRCSTATES NUMBER			{
4295 			if ($2 < 0 || $2 > UINT_MAX) {
4296 				yyerror("only positive values permitted");
4297 				YYERROR;
4298 			}
4299 			$$ = calloc(1, sizeof(struct node_state_opt));
4300 			if ($$ == NULL)
4301 				err(1, "state_opt_item: calloc");
4302 			$$->type = PF_STATE_OPT_MAX_SRC_STATES;
4303 			$$->data.max_src_states = $2;
4304 			$$->next = NULL;
4305 			$$->tail = $$;
4306 		}
4307 		| MAXSRCCONN NUMBER			{
4308 			if ($2 < 0 || $2 > UINT_MAX) {
4309 				yyerror("only positive values permitted");
4310 				YYERROR;
4311 			}
4312 			$$ = calloc(1, sizeof(struct node_state_opt));
4313 			if ($$ == NULL)
4314 				err(1, "state_opt_item: calloc");
4315 			$$->type = PF_STATE_OPT_MAX_SRC_CONN;
4316 			$$->data.max_src_conn = $2;
4317 			$$->next = NULL;
4318 			$$->tail = $$;
4319 		}
4320 		| MAXSRCCONNRATE NUMBER '/' NUMBER	{
4321 			if ($2 < 0 || $2 > UINT_MAX ||
4322 			    $4 < 0 || $4 > UINT_MAX) {
4323 				yyerror("only positive values permitted");
4324 				YYERROR;
4325 			}
4326 			$$ = calloc(1, sizeof(struct node_state_opt));
4327 			if ($$ == NULL)
4328 				err(1, "state_opt_item: calloc");
4329 			$$->type = PF_STATE_OPT_MAX_SRC_CONN_RATE;
4330 			$$->data.max_src_conn_rate.limit = $2;
4331 			$$->data.max_src_conn_rate.seconds = $4;
4332 			$$->next = NULL;
4333 			$$->tail = $$;
4334 		}
4335 		| OVERLOAD '<' STRING '>' flush		{
4336 			if (strlen($3) >= PF_TABLE_NAME_SIZE) {
4337 				yyerror("table name '%s' too long", $3);
4338 				free($3);
4339 				YYERROR;
4340 			}
4341 			$$ = calloc(1, sizeof(struct node_state_opt));
4342 			if ($$ == NULL)
4343 				err(1, "state_opt_item: calloc");
4344 			if (strlcpy($$->data.overload.tblname, $3,
4345 			    PF_TABLE_NAME_SIZE) >= PF_TABLE_NAME_SIZE)
4346 				errx(1, "state_opt_item: strlcpy");
4347 			free($3);
4348 			$$->type = PF_STATE_OPT_OVERLOAD;
4349 			$$->data.overload.flush = $5;
4350 			$$->next = NULL;
4351 			$$->tail = $$;
4352 		}
4353 		| MAXSRCNODES NUMBER			{
4354 			if ($2 < 0 || $2 > UINT_MAX) {
4355 				yyerror("only positive values permitted");
4356 				YYERROR;
4357 			}
4358 			$$ = calloc(1, sizeof(struct node_state_opt));
4359 			if ($$ == NULL)
4360 				err(1, "state_opt_item: calloc");
4361 			$$->type = PF_STATE_OPT_MAX_SRC_NODES;
4362 			$$->data.max_src_nodes = $2;
4363 			$$->next = NULL;
4364 			$$->tail = $$;
4365 		}
4366 		| sourcetrack {
4367 			$$ = calloc(1, sizeof(struct node_state_opt));
4368 			if ($$ == NULL)
4369 				err(1, "state_opt_item: calloc");
4370 			$$->type = PF_STATE_OPT_SRCTRACK;
4371 			$$->data.src_track = $1;
4372 			$$->next = NULL;
4373 			$$->tail = $$;
4374 		}
4375 		| statelock {
4376 			$$ = calloc(1, sizeof(struct node_state_opt));
4377 			if ($$ == NULL)
4378 				err(1, "state_opt_item: calloc");
4379 			$$->type = PF_STATE_OPT_STATELOCK;
4380 			$$->data.statelock = $1;
4381 			$$->next = NULL;
4382 			$$->tail = $$;
4383 		}
4384 		| SLOPPY {
4385 			$$ = calloc(1, sizeof(struct node_state_opt));
4386 			if ($$ == NULL)
4387 				err(1, "state_opt_item: calloc");
4388 			$$->type = PF_STATE_OPT_SLOPPY;
4389 			$$->next = NULL;
4390 			$$->tail = $$;
4391 		}
4392 		| PFLOW {
4393 			$$ = calloc(1, sizeof(struct node_state_opt));
4394 			if ($$ == NULL)
4395 				err(1, "state_opt_item: calloc");
4396 			$$->type = PF_STATE_OPT_PFLOW;
4397 			$$->next = NULL;
4398 			$$->tail = $$;
4399 		}
4400 		| STRING NUMBER			{
4401 			int	i;
4402 
4403 			if ($2 < 0 || $2 > UINT_MAX) {
4404 				yyerror("only positive values permitted");
4405 				YYERROR;
4406 			}
4407 			for (i = 0; pf_timeouts[i].name &&
4408 			    strcmp(pf_timeouts[i].name, $1); ++i)
4409 				;	/* nothing */
4410 			if (!pf_timeouts[i].name) {
4411 				yyerror("illegal timeout name %s", $1);
4412 				free($1);
4413 				YYERROR;
4414 			}
4415 			if (strchr(pf_timeouts[i].name, '.') == NULL) {
4416 				yyerror("illegal state timeout %s", $1);
4417 				free($1);
4418 				YYERROR;
4419 			}
4420 			free($1);
4421 			$$ = calloc(1, sizeof(struct node_state_opt));
4422 			if ($$ == NULL)
4423 				err(1, "state_opt_item: calloc");
4424 			$$->type = PF_STATE_OPT_TIMEOUT;
4425 			$$->data.timeout.number = pf_timeouts[i].timeout;
4426 			$$->data.timeout.seconds = $2;
4427 			$$->next = NULL;
4428 			$$->tail = $$;
4429 		}
4430 		;
4431 
4432 label		: LABEL STRING			{
4433 			$$ = $2;
4434 		}
4435 		;
4436 
4437 etherqname	: QUEUE STRING				{
4438 			$$.qname = $2;
4439 		}
4440 		| QUEUE '(' STRING ')'			{
4441 			$$.qname = $3;
4442 		}
4443 		;
4444 
4445 qname		: QUEUE STRING				{
4446 			$$.qname = $2;
4447 			$$.pqname = NULL;
4448 		}
4449 		| QUEUE '(' STRING ')'			{
4450 			$$.qname = $3;
4451 			$$.pqname = NULL;
4452 		}
4453 		| QUEUE '(' STRING comma STRING ')'	{
4454 			$$.qname = $3;
4455 			$$.pqname = $5;
4456 		}
4457 		;
4458 
4459 no		: /* empty */			{ $$ = 0; }
4460 		| NO				{ $$ = 1; }
4461 		;
4462 
4463 portstar	: numberstring			{
4464 			if (parseport($1, &$$, PPORT_RANGE|PPORT_STAR) == -1) {
4465 				free($1);
4466 				YYERROR;
4467 			}
4468 			free($1);
4469 		}
4470 		;
4471 
4472 redirspec	: host				{ $$ = $1; }
4473 		| '{' optnl redir_host_list '}'	{ $$ = $3; }
4474 		;
4475 
4476 redir_host_list	: host optnl			{ $$ = $1; }
4477 		| redir_host_list comma host optnl {
4478 			$1->tail->next = $3;
4479 			$1->tail = $3->tail;
4480 			$$ = $1;
4481 		}
4482 		;
4483 
4484 redirpool	: /* empty */			{ $$ = NULL; }
4485 		| ARROW redirspec		{
4486 			$$ = calloc(1, sizeof(struct redirection));
4487 			if ($$ == NULL)
4488 				err(1, "redirection: calloc");
4489 			$$->host = $2;
4490 			$$->rport.a = $$->rport.b = $$->rport.t = 0;
4491 		}
4492 		| ARROW redirspec PORT portstar	{
4493 			$$ = calloc(1, sizeof(struct redirection));
4494 			if ($$ == NULL)
4495 				err(1, "redirection: calloc");
4496 			$$->host = $2;
4497 			$$->rport = $4;
4498 		}
4499 		;
4500 
4501 hashkey		: /* empty */
4502 		{
4503 			$$ = calloc(1, sizeof(struct pf_poolhashkey));
4504 			if ($$ == NULL)
4505 				err(1, "hashkey: calloc");
4506 			$$->key32[0] = arc4random();
4507 			$$->key32[1] = arc4random();
4508 			$$->key32[2] = arc4random();
4509 			$$->key32[3] = arc4random();
4510 		}
4511 		| string
4512 		{
4513 			if (!strncmp($1, "0x", 2)) {
4514 				if (strlen($1) != 34) {
4515 					free($1);
4516 					yyerror("hex key must be 128 bits "
4517 						"(32 hex digits) long");
4518 					YYERROR;
4519 				}
4520 				$$ = calloc(1, sizeof(struct pf_poolhashkey));
4521 				if ($$ == NULL)
4522 					err(1, "hashkey: calloc");
4523 
4524 				if (sscanf($1, "0x%8x%8x%8x%8x",
4525 				    &$$->key32[0], &$$->key32[1],
4526 				    &$$->key32[2], &$$->key32[3]) != 4) {
4527 					free($$);
4528 					free($1);
4529 					yyerror("invalid hex key");
4530 					YYERROR;
4531 				}
4532 			} else {
4533 				MD5_CTX	context;
4534 
4535 				$$ = calloc(1, sizeof(struct pf_poolhashkey));
4536 				if ($$ == NULL)
4537 					err(1, "hashkey: calloc");
4538 				MD5Init(&context);
4539 				MD5Update(&context, (unsigned char *)$1,
4540 				    strlen($1));
4541 				MD5Final((unsigned char *)$$, &context);
4542 				HTONL($$->key32[0]);
4543 				HTONL($$->key32[1]);
4544 				HTONL($$->key32[2]);
4545 				HTONL($$->key32[3]);
4546 			}
4547 			free($1);
4548 		}
4549 		;
4550 
4551 pool_opts	:	{ bzero(&pool_opts, sizeof pool_opts); }
4552 		    pool_opts_l
4553 			{ $$ = pool_opts; }
4554 		| /* empty */	{
4555 			bzero(&pool_opts, sizeof pool_opts);
4556 			$$ = pool_opts;
4557 		}
4558 		;
4559 
4560 pool_opts_l	: pool_opts_l pool_opt
4561 		| pool_opt
4562 		;
4563 
4564 pool_opt	: BITMASK	{
4565 			if (pool_opts.type) {
4566 				yyerror("pool type cannot be redefined");
4567 				YYERROR;
4568 			}
4569 			pool_opts.type =  PF_POOL_BITMASK;
4570 		}
4571 		| RANDOM	{
4572 			if (pool_opts.type) {
4573 				yyerror("pool type cannot be redefined");
4574 				YYERROR;
4575 			}
4576 			pool_opts.type = PF_POOL_RANDOM;
4577 		}
4578 		| SOURCEHASH hashkey {
4579 			if (pool_opts.type) {
4580 				yyerror("pool type cannot be redefined");
4581 				YYERROR;
4582 			}
4583 			pool_opts.type = PF_POOL_SRCHASH;
4584 			pool_opts.key = $2;
4585 		}
4586 		| ROUNDROBIN	{
4587 			if (pool_opts.type) {
4588 				yyerror("pool type cannot be redefined");
4589 				YYERROR;
4590 			}
4591 			pool_opts.type = PF_POOL_ROUNDROBIN;
4592 		}
4593 		| STATICPORT	{
4594 			if (pool_opts.staticport) {
4595 				yyerror("static-port cannot be redefined");
4596 				YYERROR;
4597 			}
4598 			pool_opts.staticport = 1;
4599 		}
4600 		| STICKYADDRESS	{
4601 			if (pool_opts.marker & POM_STICKYADDRESS) {
4602 				yyerror("sticky-address cannot be redefined");
4603 				YYERROR;
4604 			}
4605 			pool_opts.marker |= POM_STICKYADDRESS;
4606 			pool_opts.opts |= PF_POOL_STICKYADDR;
4607 		}
4608 		| ENDPI {
4609 			if (pool_opts.marker & POM_ENDPI) {
4610 				yyerror("endpoint-independent cannot be redefined");
4611 				YYERROR;
4612 			}
4613 			pool_opts.marker |= POM_ENDPI;
4614 			pool_opts.opts |= PF_POOL_ENDPI;
4615 		}
4616 		| MAPEPORTSET number '/' number '/' number {
4617 			if (pool_opts.mape.offset) {
4618 				yyerror("map-e-portset cannot be redefined");
4619 				YYERROR;
4620 			}
4621 			if (pool_opts.type) {
4622 				yyerror("map-e-portset cannot be used with "
4623 					"address pools");
4624 				YYERROR;
4625 			}
4626 			if ($2 <= 0 || $2 >= 16) {
4627 				yyerror("MAP-E PSID offset must be 1-15");
4628 				YYERROR;
4629 			}
4630 			if ($4 < 0 || $4 >= 16 || $2 + $4 > 16) {
4631 				yyerror("Invalid MAP-E PSID length");
4632 				YYERROR;
4633 			} else if ($4 == 0) {
4634 				yyerror("PSID Length = 0: this means"
4635 				    " you do not need MAP-E");
4636 				YYERROR;
4637 			}
4638 			if ($6 < 0 || $6 > 65535) {
4639 				yyerror("Invalid MAP-E PSID");
4640 				YYERROR;
4641 			}
4642 			pool_opts.mape.offset = $2;
4643 			pool_opts.mape.psidlen = $4;
4644 			pool_opts.mape.psid = $6;
4645 		}
4646 		;
4647 
4648 redirection	: /* empty */			{ $$ = NULL; }
4649 		| ARROW host			{
4650 			$$ = calloc(1, sizeof(struct redirection));
4651 			if ($$ == NULL)
4652 				err(1, "redirection: calloc");
4653 			$$->host = $2;
4654 			$$->rport.a = $$->rport.b = $$->rport.t = 0;
4655 		}
4656 		| ARROW host PORT portstar	{
4657 			$$ = calloc(1, sizeof(struct redirection));
4658 			if ($$ == NULL)
4659 				err(1, "redirection: calloc");
4660 			$$->host = $2;
4661 			$$->rport = $4;
4662 		}
4663 		;
4664 
4665 natpasslog	: /* empty */	{ $$.b1 = $$.b2 = 0; $$.w2 = 0; }
4666 		| PASS		{ $$.b1 = 1; $$.b2 = 0; $$.w2 = 0; }
4667 		| PASS log	{ $$.b1 = 1; $$.b2 = $2.log; $$.w2 = $2.logif; }
4668 		| log		{ $$.b1 = 0; $$.b2 = $1.log; $$.w2 = $1.logif; }
4669 		;
4670 
4671 nataction	: no NAT natpasslog {
4672 			if ($1 && $3.b1) {
4673 				yyerror("\"pass\" not valid with \"no\"");
4674 				YYERROR;
4675 			}
4676 			if ($1)
4677 				$$.b1 = PF_NONAT;
4678 			else
4679 				$$.b1 = PF_NAT;
4680 			$$.b2 = $3.b1;
4681 			$$.w = $3.b2;
4682 			$$.w2 = $3.w2;
4683 		}
4684 		| no RDR natpasslog {
4685 			if ($1 && $3.b1) {
4686 				yyerror("\"pass\" not valid with \"no\"");
4687 				YYERROR;
4688 			}
4689 			if ($1)
4690 				$$.b1 = PF_NORDR;
4691 			else
4692 				$$.b1 = PF_RDR;
4693 			$$.b2 = $3.b1;
4694 			$$.w = $3.b2;
4695 			$$.w2 = $3.w2;
4696 		}
4697 		;
4698 
4699 natrule		: nataction interface af proto fromto tag tagged rtable
4700 		    redirpool pool_opts
4701 		{
4702 			struct pfctl_rule	r;
4703 			struct node_state_opt	*o;
4704 
4705 			if (check_rulestate(PFCTL_STATE_NAT))
4706 				YYERROR;
4707 
4708 			memset(&r, 0, sizeof(r));
4709 
4710 			r.action = $1.b1;
4711 			r.natpass = $1.b2;
4712 			r.log = $1.w;
4713 			r.logif = $1.w2;
4714 			r.af = $3;
4715 
4716 			if (!r.af) {
4717 				if ($5.src.host && $5.src.host->af &&
4718 				    !$5.src.host->ifindex)
4719 					r.af = $5.src.host->af;
4720 				else if ($5.dst.host && $5.dst.host->af &&
4721 				    !$5.dst.host->ifindex)
4722 					r.af = $5.dst.host->af;
4723 			}
4724 
4725 			if ($6 != NULL)
4726 				if (strlcpy(r.tagname, $6, PF_TAG_NAME_SIZE) >=
4727 				    PF_TAG_NAME_SIZE) {
4728 					yyerror("tag too long, max %u chars",
4729 					    PF_TAG_NAME_SIZE - 1);
4730 					YYERROR;
4731 				}
4732 
4733 			if ($7.name)
4734 				if (strlcpy(r.match_tagname, $7.name,
4735 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4736 					yyerror("tag too long, max %u chars",
4737 					    PF_TAG_NAME_SIZE - 1);
4738 					YYERROR;
4739 				}
4740 			r.match_tag_not = $7.neg;
4741 			r.rtableid = $8;
4742 
4743 			if (r.action == PF_NONAT || r.action == PF_NORDR) {
4744 				if ($9 != NULL) {
4745 					yyerror("translation rule with 'no' "
4746 					    "does not need '->'");
4747 					YYERROR;
4748 				}
4749 			} else {
4750 				if ($9 == NULL || $9->host == NULL) {
4751 					yyerror("translation rule requires '-> "
4752 					    "address'");
4753 					YYERROR;
4754 				}
4755 				if (!r.af && ! $9->host->ifindex)
4756 					r.af = $9->host->af;
4757 
4758 				remove_invalid_hosts(&$9->host, &r.af);
4759 				if (invalid_redirect($9->host, r.af))
4760 					YYERROR;
4761 				if ($9->host->addr.type == PF_ADDR_DYNIFTL) {
4762 					if (($9->host = gen_dynnode($9->host, r.af)) == NULL)
4763 						err(1, "calloc");
4764 				}
4765 				if (check_netmask($9->host, r.af))
4766 					YYERROR;
4767 
4768 				r.rpool.proxy_port[0] = ntohs($9->rport.a);
4769 
4770 				switch (r.action) {
4771 				case PF_RDR:
4772 					if (!$9->rport.b && $9->rport.t &&
4773 					    $5.dst.port != NULL) {
4774 						r.rpool.proxy_port[1] =
4775 						    ntohs($9->rport.a) +
4776 						    (ntohs(
4777 						    $5.dst.port->port[1]) -
4778 						    ntohs(
4779 						    $5.dst.port->port[0]));
4780 					} else
4781 						r.rpool.proxy_port[1] =
4782 						    ntohs($9->rport.b);
4783 					break;
4784 				case PF_NAT:
4785 					r.rpool.proxy_port[1] =
4786 					    ntohs($9->rport.b);
4787 					if (!r.rpool.proxy_port[0] &&
4788 					    !r.rpool.proxy_port[1]) {
4789 						r.rpool.proxy_port[0] =
4790 						    PF_NAT_PROXY_PORT_LOW;
4791 						r.rpool.proxy_port[1] =
4792 						    PF_NAT_PROXY_PORT_HIGH;
4793 					} else if (!r.rpool.proxy_port[1])
4794 						r.rpool.proxy_port[1] =
4795 						    r.rpool.proxy_port[0];
4796 					break;
4797 				default:
4798 					break;
4799 				}
4800 
4801 				r.rpool.opts = $10.type;
4802 				if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
4803 				    PF_POOL_NONE && ($9->host->next != NULL ||
4804 				    $9->host->addr.type == PF_ADDR_TABLE ||
4805 				    DYNIF_MULTIADDR($9->host->addr)))
4806 					r.rpool.opts = PF_POOL_ROUNDROBIN;
4807 				if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4808 				    PF_POOL_ROUNDROBIN &&
4809 				    disallow_table($9->host, "tables are only "
4810 				    "supported in round-robin redirection "
4811 				    "pools"))
4812 					YYERROR;
4813 				if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4814 				    PF_POOL_ROUNDROBIN &&
4815 				    disallow_alias($9->host, "interface (%s) "
4816 				    "is only supported in round-robin "
4817 				    "redirection pools"))
4818 					YYERROR;
4819 				if ($9->host->next != NULL) {
4820 					if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4821 					    PF_POOL_ROUNDROBIN) {
4822 						yyerror("only round-robin "
4823 						    "valid for multiple "
4824 						    "redirection addresses");
4825 						YYERROR;
4826 					}
4827 				}
4828 			}
4829 
4830 			if ($10.key != NULL)
4831 				memcpy(&r.rpool.key, $10.key,
4832 				    sizeof(struct pf_poolhashkey));
4833 
4834 			 if ($10.opts)
4835 				r.rpool.opts |= $10.opts;
4836 
4837 			if ($10.staticport) {
4838 				if (r.action != PF_NAT) {
4839 					yyerror("the 'static-port' option is "
4840 					    "only valid with nat rules");
4841 					YYERROR;
4842 				}
4843 				if (r.rpool.proxy_port[0] !=
4844 				    PF_NAT_PROXY_PORT_LOW &&
4845 				    r.rpool.proxy_port[1] !=
4846 				    PF_NAT_PROXY_PORT_HIGH) {
4847 					yyerror("the 'static-port' option can't"
4848 					    " be used when specifying a port"
4849 					    " range");
4850 					YYERROR;
4851 				}
4852 				r.rpool.proxy_port[0] = 0;
4853 				r.rpool.proxy_port[1] = 0;
4854 			}
4855 
4856 			if ($10.mape.offset) {
4857 				if (r.action != PF_NAT) {
4858 					yyerror("the 'map-e-portset' option is"
4859 					    " only valid with nat rules");
4860 					YYERROR;
4861 				}
4862 				if ($10.staticport) {
4863 					yyerror("the 'map-e-portset' option"
4864 					    " can't be used 'static-port'");
4865 					YYERROR;
4866 				}
4867 				if (r.rpool.proxy_port[0] !=
4868 				    PF_NAT_PROXY_PORT_LOW &&
4869 				    r.rpool.proxy_port[1] !=
4870 				    PF_NAT_PROXY_PORT_HIGH) {
4871 					yyerror("the 'map-e-portset' option"
4872 					    " can't be used when specifying"
4873 					    " a port range");
4874 					YYERROR;
4875 				}
4876 				r.rpool.mape = $10.mape;
4877 			}
4878 
4879 			o = keep_state_defaults;
4880 			while (o) {
4881 				switch (o->type) {
4882 				case PF_STATE_OPT_PFLOW:
4883 					if (r.rule_flag & PFRULE_PFLOW) {
4884 						yyerror("state pflow option: "
4885 						    "multiple definitions");
4886 						YYERROR;
4887 					}
4888 					r.rule_flag |= PFRULE_PFLOW;
4889 					break;
4890 				}
4891 				o = o->next;
4892 			}
4893 
4894 			expand_rule(&r, $2, $9 == NULL ? NULL : $9->host, $4,
4895 			    $5.src_os, $5.src.host, $5.src.port, $5.dst.host,
4896 			    $5.dst.port, 0, 0, 0, 0, "");
4897 			free($9);
4898 		}
4899 		;
4900 
4901 binatrule	: no BINAT natpasslog interface af proto FROM ipspec toipspec tag
4902 		    tagged rtable redirection
4903 		{
4904 			struct pfctl_rule	binat;
4905 			struct pf_pooladdr	*pa;
4906 
4907 			if (check_rulestate(PFCTL_STATE_NAT))
4908 				YYERROR;
4909 			if (disallow_urpf_failed($9, "\"urpf-failed\" is not "
4910 			    "permitted as a binat destination"))
4911 				YYERROR;
4912 
4913 			memset(&binat, 0, sizeof(binat));
4914 
4915 			if ($1 && $3.b1) {
4916 				yyerror("\"pass\" not valid with \"no\"");
4917 				YYERROR;
4918 			}
4919 			if ($1)
4920 				binat.action = PF_NOBINAT;
4921 			else
4922 				binat.action = PF_BINAT;
4923 			binat.natpass = $3.b1;
4924 			binat.log = $3.b2;
4925 			binat.logif = $3.w2;
4926 			binat.af = $5;
4927 			if (!binat.af && $8 != NULL && $8->af)
4928 				binat.af = $8->af;
4929 			if (!binat.af && $9 != NULL && $9->af)
4930 				binat.af = $9->af;
4931 
4932 			if (!binat.af && $13 != NULL && $13->host)
4933 				binat.af = $13->host->af;
4934 			if (!binat.af) {
4935 				yyerror("address family (inet/inet6) "
4936 				    "undefined");
4937 				YYERROR;
4938 			}
4939 
4940 			if ($4 != NULL) {
4941 				memcpy(binat.ifname, $4->ifname,
4942 				    sizeof(binat.ifname));
4943 				binat.ifnot = $4->not;
4944 				free($4);
4945 			}
4946 
4947 			if ($10 != NULL)
4948 				if (strlcpy(binat.tagname, $10,
4949 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4950 					yyerror("tag too long, max %u chars",
4951 					    PF_TAG_NAME_SIZE - 1);
4952 					YYERROR;
4953 				}
4954 			if ($11.name)
4955 				if (strlcpy(binat.match_tagname, $11.name,
4956 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4957 					yyerror("tag too long, max %u chars",
4958 					    PF_TAG_NAME_SIZE - 1);
4959 					YYERROR;
4960 				}
4961 			binat.match_tag_not = $11.neg;
4962 			binat.rtableid = $12;
4963 
4964 			if ($6 != NULL) {
4965 				binat.proto = $6->proto;
4966 				free($6);
4967 			}
4968 
4969 			if ($8 != NULL && disallow_table($8, "invalid use of "
4970 			    "table <%s> as the source address of a binat rule"))
4971 				YYERROR;
4972 			if ($8 != NULL && disallow_alias($8, "invalid use of "
4973 			    "interface (%s) as the source address of a binat "
4974 			    "rule"))
4975 				YYERROR;
4976 			if ($13 != NULL && $13->host != NULL && disallow_table(
4977 			    $13->host, "invalid use of table <%s> as the "
4978 			    "redirect address of a binat rule"))
4979 				YYERROR;
4980 			if ($13 != NULL && $13->host != NULL && disallow_alias(
4981 			    $13->host, "invalid use of interface (%s) as the "
4982 			    "redirect address of a binat rule"))
4983 				YYERROR;
4984 
4985 			if ($8 != NULL) {
4986 				if ($8->next) {
4987 					yyerror("multiple binat ip addresses");
4988 					YYERROR;
4989 				}
4990 				if ($8->addr.type == PF_ADDR_DYNIFTL)
4991 					$8->af = binat.af;
4992 				if ($8->af != binat.af) {
4993 					yyerror("binat ip versions must match");
4994 					YYERROR;
4995 				}
4996 				if ($8->addr.type == PF_ADDR_DYNIFTL) {
4997 					if (($8 = gen_dynnode($8, binat.af)) == NULL)
4998 						err(1, "calloc");
4999 				}
5000 				if (check_netmask($8, binat.af))
5001 					YYERROR;
5002 				memcpy(&binat.src.addr, &$8->addr,
5003 				    sizeof(binat.src.addr));
5004 				free($8);
5005 			}
5006 			if ($9 != NULL) {
5007 				if ($9->next) {
5008 					yyerror("multiple binat ip addresses");
5009 					YYERROR;
5010 				}
5011 				if ($9->af != binat.af && $9->af) {
5012 					yyerror("binat ip versions must match");
5013 					YYERROR;
5014 				}
5015 				if ($9->addr.type == PF_ADDR_DYNIFTL) {
5016 					if (($9 = gen_dynnode($9, binat.af)) == NULL)
5017 						err(1, "calloc");
5018 				}
5019 				if (check_netmask($9, binat.af))
5020 					YYERROR;
5021 				memcpy(&binat.dst.addr, &$9->addr,
5022 				    sizeof(binat.dst.addr));
5023 				binat.dst.neg = $9->not;
5024 				free($9);
5025 			}
5026 
5027 			if (binat.action == PF_NOBINAT) {
5028 				if ($13 != NULL) {
5029 					yyerror("'no binat' rule does not need"
5030 					    " '->'");
5031 					YYERROR;
5032 				}
5033 			} else {
5034 				if ($13 == NULL || $13->host == NULL) {
5035 					yyerror("'binat' rule requires"
5036 					    " '-> address'");
5037 					YYERROR;
5038 				}
5039 
5040 				remove_invalid_hosts(&$13->host, &binat.af);
5041 				if (invalid_redirect($13->host, binat.af))
5042 					YYERROR;
5043 				if ($13->host->next != NULL) {
5044 					yyerror("binat rule must redirect to "
5045 					    "a single address");
5046 					YYERROR;
5047 				}
5048 				if ($13->host->addr.type == PF_ADDR_DYNIFTL) {
5049 					if (($13->host = gen_dynnode($13->host, binat.af)) == NULL)
5050 						err(1, "calloc");
5051 				}
5052 				if (check_netmask($13->host, binat.af))
5053 					YYERROR;
5054 
5055 				if (!PF_AZERO(&binat.src.addr.v.a.mask,
5056 				    binat.af) &&
5057 				    !PF_AEQ(&binat.src.addr.v.a.mask,
5058 				    &$13->host->addr.v.a.mask, binat.af)) {
5059 					yyerror("'binat' source mask and "
5060 					    "redirect mask must be the same");
5061 					YYERROR;
5062 				}
5063 
5064 				TAILQ_INIT(&binat.rpool.list);
5065 				pa = calloc(1, sizeof(struct pf_pooladdr));
5066 				if (pa == NULL)
5067 					err(1, "binat: calloc");
5068 				pa->addr = $13->host->addr;
5069 				pa->ifname[0] = 0;
5070 				TAILQ_INSERT_TAIL(&binat.rpool.list,
5071 				    pa, entries);
5072 
5073 				free($13);
5074 			}
5075 
5076 			pfctl_append_rule(pf, &binat, "");
5077 		}
5078 		;
5079 
5080 tag		: /* empty */		{ $$ = NULL; }
5081 		| TAG STRING		{ $$ = $2; }
5082 		;
5083 
5084 tagged		: /* empty */		{ $$.neg = 0; $$.name = NULL; }
5085 		| not TAGGED string	{ $$.neg = $1; $$.name = $3; }
5086 		;
5087 
5088 rtable		: /* empty */		{ $$ = -1; }
5089 		| RTABLE NUMBER		{
5090 			if ($2 < 0 || $2 > rt_tableid_max()) {
5091 				yyerror("invalid rtable id");
5092 				YYERROR;
5093 			}
5094 			$$ = $2;
5095 		}
5096 		;
5097 
5098 route_host	: STRING			{
5099 			$$ = calloc(1, sizeof(struct node_host));
5100 			if ($$ == NULL)
5101 				err(1, "route_host: calloc");
5102 			if (strlen($1) >= IFNAMSIZ) {
5103 				yyerror("interface name too long");
5104 				YYERROR;
5105 			}
5106 			$$->ifname = strdup($1);
5107 			set_ipmask($$, 128);
5108 			$$->next = NULL;
5109 			$$->tail = $$;
5110 		}
5111 		| '(' STRING host ')'		{
5112 			struct node_host *n;
5113 
5114 			$$ = $3;
5115 			for (n = $3; n != NULL; n = n->next) {
5116 				if (strlen($2) >= IFNAMSIZ) {
5117 					yyerror("interface name too long");
5118 					YYERROR;
5119 				}
5120 				n->ifname = strdup($2);
5121 			}
5122 		}
5123 		;
5124 
5125 route_host_list	: route_host optnl			{ $$ = $1; }
5126 		| route_host_list comma route_host optnl {
5127 			if ($1->af == 0)
5128 				$1->af = $3->af;
5129 			if ($1->af != $3->af) {
5130 				yyerror("all pool addresses must be in the "
5131 				    "same address family");
5132 				YYERROR;
5133 			}
5134 			$1->tail->next = $3;
5135 			$1->tail = $3->tail;
5136 			$$ = $1;
5137 		}
5138 		;
5139 
5140 routespec	: route_host			{ $$ = $1; }
5141 		| '{' optnl route_host_list '}'	{ $$ = $3; }
5142 		;
5143 
5144 route		: /* empty */			{
5145 			$$.host = NULL;
5146 			$$.rt = 0;
5147 			$$.pool_opts = 0;
5148 		}
5149 		| FASTROUTE {
5150 			/* backwards-compat */
5151 			$$.host = NULL;
5152 			$$.rt = 0;
5153 			$$.pool_opts = 0;
5154 		}
5155 		| ROUTETO routespec pool_opts {
5156 			$$.host = $2;
5157 			$$.rt = PF_ROUTETO;
5158 			$$.pool_opts = $3.type | $3.opts;
5159 			if ($3.key != NULL)
5160 				$$.key = $3.key;
5161 		}
5162 		| REPLYTO routespec pool_opts {
5163 			$$.host = $2;
5164 			$$.rt = PF_REPLYTO;
5165 			$$.pool_opts = $3.type | $3.opts;
5166 			if ($3.key != NULL)
5167 				$$.key = $3.key;
5168 		}
5169 		| DUPTO routespec pool_opts {
5170 			$$.host = $2;
5171 			$$.rt = PF_DUPTO;
5172 			$$.pool_opts = $3.type | $3.opts;
5173 			if ($3.key != NULL)
5174 				$$.key = $3.key;
5175 		}
5176 		;
5177 
5178 timeout_spec	: STRING NUMBER
5179 		{
5180 			if (check_rulestate(PFCTL_STATE_OPTION)) {
5181 				free($1);
5182 				YYERROR;
5183 			}
5184 			if ($2 < 0 || $2 > UINT_MAX) {
5185 				yyerror("only positive values permitted");
5186 				YYERROR;
5187 			}
5188 			if (pfctl_apply_timeout(pf, $1, $2, 0) != 0) {
5189 				yyerror("unknown timeout %s", $1);
5190 				free($1);
5191 				YYERROR;
5192 			}
5193 			free($1);
5194 		}
5195 		| INTERVAL NUMBER		{
5196 			if (check_rulestate(PFCTL_STATE_OPTION))
5197 				YYERROR;
5198 			if ($2 < 0 || $2 > UINT_MAX) {
5199 				yyerror("only positive values permitted");
5200 				YYERROR;
5201 			}
5202 			if (pfctl_apply_timeout(pf, "interval", $2, 0) != 0)
5203 				YYERROR;
5204 		}
5205 		;
5206 
5207 timeout_list	: timeout_list comma timeout_spec optnl
5208 		| timeout_spec optnl
5209 		;
5210 
5211 limit_spec	: STRING NUMBER
5212 		{
5213 			if (check_rulestate(PFCTL_STATE_OPTION)) {
5214 				free($1);
5215 				YYERROR;
5216 			}
5217 			if ($2 < 0 || $2 > UINT_MAX) {
5218 				yyerror("only positive values permitted");
5219 				YYERROR;
5220 			}
5221 			if (pfctl_apply_limit(pf, $1, $2) != 0) {
5222 				yyerror("unable to set limit %s %u", $1, $2);
5223 				free($1);
5224 				YYERROR;
5225 			}
5226 			free($1);
5227 		}
5228 		;
5229 
5230 limit_list	: limit_list comma limit_spec optnl
5231 		| limit_spec optnl
5232 		;
5233 
5234 comma		: ','
5235 		| /* empty */
5236 		;
5237 
5238 yesno		: NO			{ $$ = 0; }
5239 		| STRING		{
5240 			if (!strcmp($1, "yes"))
5241 				$$ = 1;
5242 			else {
5243 				yyerror("invalid value '%s', expected 'yes' "
5244 				    "or 'no'", $1);
5245 				free($1);
5246 				YYERROR;
5247 			}
5248 			free($1);
5249 		}
5250 		;
5251 
5252 unaryop		: '='		{ $$ = PF_OP_EQ; }
5253 		| NE			{ $$ = PF_OP_NE; }
5254 		| LE			{ $$ = PF_OP_LE; }
5255 		| '<'		{ $$ = PF_OP_LT; }
5256 		| GE		{ $$ = PF_OP_GE; }
5257 		| '>'		{ $$ = PF_OP_GT; }
5258 		;
5259 
5260 %%
5261 
5262 int
5263 yyerror(const char *fmt, ...)
5264 {
5265 	va_list		 ap;
5266 
5267 	file->errors++;
5268 	va_start(ap, fmt);
5269 	fprintf(stderr, "%s:%d: ", file->name, yylval.lineno);
5270 	vfprintf(stderr, fmt, ap);
5271 	fprintf(stderr, "\n");
5272 	va_end(ap);
5273 	return (0);
5274 }
5275 
5276 int
disallow_table(struct node_host * h,const char * fmt)5277 disallow_table(struct node_host *h, const char *fmt)
5278 {
5279 	for (; h != NULL; h = h->next)
5280 		if (h->addr.type == PF_ADDR_TABLE) {
5281 			yyerror(fmt, h->addr.v.tblname);
5282 			return (1);
5283 		}
5284 	return (0);
5285 }
5286 
5287 int
disallow_urpf_failed(struct node_host * h,const char * fmt)5288 disallow_urpf_failed(struct node_host *h, const char *fmt)
5289 {
5290 	for (; h != NULL; h = h->next)
5291 		if (h->addr.type == PF_ADDR_URPFFAILED) {
5292 			yyerror(fmt);
5293 			return (1);
5294 		}
5295 	return (0);
5296 }
5297 
5298 int
disallow_alias(struct node_host * h,const char * fmt)5299 disallow_alias(struct node_host *h, const char *fmt)
5300 {
5301 	for (; h != NULL; h = h->next)
5302 		if (DYNIF_MULTIADDR(h->addr)) {
5303 			yyerror(fmt, h->addr.v.tblname);
5304 			return (1);
5305 		}
5306 	return (0);
5307 }
5308 
5309 int
rule_consistent(struct pfctl_rule * r,int anchor_call)5310 rule_consistent(struct pfctl_rule *r, int anchor_call)
5311 {
5312 	int	problems = 0;
5313 
5314 	switch (r->action) {
5315 	case PF_PASS:
5316 	case PF_MATCH:
5317 	case PF_DROP:
5318 	case PF_SCRUB:
5319 	case PF_NOSCRUB:
5320 		problems = filter_consistent(r, anchor_call);
5321 		break;
5322 	case PF_NAT:
5323 	case PF_NONAT:
5324 		problems = nat_consistent(r);
5325 		break;
5326 	case PF_RDR:
5327 	case PF_NORDR:
5328 		problems = rdr_consistent(r);
5329 		break;
5330 	case PF_BINAT:
5331 	case PF_NOBINAT:
5332 	default:
5333 		break;
5334 	}
5335 	return (problems);
5336 }
5337 
5338 int
filter_consistent(struct pfctl_rule * r,int anchor_call)5339 filter_consistent(struct pfctl_rule *r, int anchor_call)
5340 {
5341 	int	problems = 0;
5342 
5343 	if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP &&
5344 	    r->proto != IPPROTO_SCTP &&
5345 	    (r->src.port_op || r->dst.port_op)) {
5346 		yyerror("port only applies to tcp/udp/sctp");
5347 		problems++;
5348 	}
5349 	if (r->proto != IPPROTO_ICMP && r->proto != IPPROTO_ICMPV6 &&
5350 	    (r->type || r->code)) {
5351 		yyerror("icmp-type/code only applies to icmp");
5352 		problems++;
5353 	}
5354 	if (!r->af && (r->type || r->code)) {
5355 		yyerror("must indicate address family with icmp-type/code");
5356 		problems++;
5357 	}
5358 	if (r->overload_tblname[0] &&
5359 	    r->max_src_conn == 0 && r->max_src_conn_rate.seconds == 0) {
5360 		yyerror("'overload' requires 'max-src-conn' "
5361 		    "or 'max-src-conn-rate'");
5362 		problems++;
5363 	}
5364 	if ((r->proto == IPPROTO_ICMP && r->af == AF_INET6) ||
5365 	    (r->proto == IPPROTO_ICMPV6 && r->af == AF_INET)) {
5366 		yyerror("proto %s doesn't match address family %s",
5367 		    r->proto == IPPROTO_ICMP ? "icmp" : "icmp6",
5368 		    r->af == AF_INET ? "inet" : "inet6");
5369 		problems++;
5370 	}
5371 	if (r->allow_opts && r->action != PF_PASS) {
5372 		yyerror("allow-opts can only be specified for pass rules");
5373 		problems++;
5374 	}
5375 	if (r->rule_flag & PFRULE_FRAGMENT && (r->src.port_op ||
5376 	    r->dst.port_op || r->flagset || r->type || r->code)) {
5377 		yyerror("fragments can be filtered only on IP header fields");
5378 		problems++;
5379 	}
5380 	if (r->rule_flag & PFRULE_RETURNRST && r->proto != IPPROTO_TCP) {
5381 		yyerror("return-rst can only be applied to TCP rules");
5382 		problems++;
5383 	}
5384 	if (r->max_src_nodes && !(r->rule_flag & PFRULE_RULESRCTRACK)) {
5385 		yyerror("max-src-nodes requires 'source-track rule'");
5386 		problems++;
5387 	}
5388 	if (r->action != PF_PASS && r->keep_state) {
5389 		yyerror("keep state is great, but only for pass rules");
5390 		problems++;
5391 	}
5392 	if (r->rule_flag & PFRULE_STATESLOPPY &&
5393 	    (r->keep_state == PF_STATE_MODULATE ||
5394 	    r->keep_state == PF_STATE_SYNPROXY)) {
5395 		yyerror("sloppy state matching cannot be used with "
5396 		    "synproxy state or modulate state");
5397 		problems++;
5398 	}
5399 	/* match rules rules */
5400 	if (r->action == PF_MATCH) {
5401 		if (r->divert.port) {
5402 			yyerror("divert is not supported on match rules");
5403 			problems++;
5404 		}
5405 		if (r->rt) {
5406 			yyerror("route-to, reply-to, dup-to and fastroute "
5407 			   "must not be used on match rules");
5408 			problems++;
5409 		}
5410 	}
5411 	if (r->rpool.opts & PF_POOL_STICKYADDR && !r->keep_state) {
5412 		yyerror("'sticky-address' requires 'keep state'");
5413 		problems++;
5414 	}
5415 	return (-problems);
5416 }
5417 
5418 int
nat_consistent(struct pfctl_rule * r)5419 nat_consistent(struct pfctl_rule *r)
5420 {
5421 	return (0);	/* yeah! */
5422 }
5423 
5424 int
rdr_consistent(struct pfctl_rule * r)5425 rdr_consistent(struct pfctl_rule *r)
5426 {
5427 	int			 problems = 0;
5428 
5429 	if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP &&
5430 	    r->proto != IPPROTO_SCTP) {
5431 		if (r->src.port_op) {
5432 			yyerror("src port only applies to tcp/udp/sctp");
5433 			problems++;
5434 		}
5435 		if (r->dst.port_op) {
5436 			yyerror("dst port only applies to tcp/udp/sctp");
5437 			problems++;
5438 		}
5439 		if (r->rpool.proxy_port[0]) {
5440 			yyerror("rpool port only applies to tcp/udp/sctp");
5441 			problems++;
5442 		}
5443 	}
5444 	if (r->dst.port_op &&
5445 	    r->dst.port_op != PF_OP_EQ && r->dst.port_op != PF_OP_RRG) {
5446 		yyerror("invalid port operator for rdr destination port");
5447 		problems++;
5448 	}
5449 	return (-problems);
5450 }
5451 
5452 int
process_tabledef(char * name,struct table_opts * opts)5453 process_tabledef(char *name, struct table_opts *opts)
5454 {
5455 	struct pfr_buffer	 ab;
5456 	struct node_tinit	*ti;
5457 	unsigned long		 maxcount;
5458 	size_t			 s = sizeof(maxcount);
5459 
5460 	bzero(&ab, sizeof(ab));
5461 	ab.pfrb_type = PFRB_ADDRS;
5462 	SIMPLEQ_FOREACH(ti, &opts->init_nodes, entries) {
5463 		if (ti->file)
5464 			if (pfr_buf_load(&ab, ti->file, 0, append_addr)) {
5465 				if (errno)
5466 					yyerror("cannot load \"%s\": %s",
5467 					    ti->file, strerror(errno));
5468 				else
5469 					yyerror("file \"%s\" contains bad data",
5470 					    ti->file);
5471 				goto _error;
5472 			}
5473 		if (ti->host)
5474 			if (append_addr_host(&ab, ti->host, 0, 0)) {
5475 				yyerror("cannot create address buffer: %s",
5476 				    strerror(errno));
5477 				goto _error;
5478 			}
5479 	}
5480 	if (pf->opts & PF_OPT_VERBOSE)
5481 		print_tabledef(name, opts->flags, opts->init_addr,
5482 		    &opts->init_nodes);
5483 	if (!(pf->opts & PF_OPT_NOACTION) &&
5484 	    pfctl_define_table(name, opts->flags, opts->init_addr,
5485 	    pf->anchor->name, &ab, pf->anchor->ruleset.tticket)) {
5486 
5487 		if (sysctlbyname("net.pf.request_maxcount", &maxcount, &s,
5488 		    NULL, 0) == -1)
5489 			maxcount = 65535;
5490 
5491 		if (ab.pfrb_size > maxcount)
5492 			yyerror("cannot define table %s: too many elements.\n"
5493 			    "Consider increasing net.pf.request_maxcount.",
5494 			    name);
5495 		else
5496 			yyerror("cannot define table %s: %s", name,
5497 			    pfr_strerror(errno));
5498 
5499 		goto _error;
5500 	}
5501 	pf->tdirty = 1;
5502 	pfr_buf_clear(&ab);
5503 	return (0);
5504 _error:
5505 	pfr_buf_clear(&ab);
5506 	return (-1);
5507 }
5508 
5509 struct keywords {
5510 	const char	*k_name;
5511 	int		 k_val;
5512 };
5513 
5514 /* macro gore, but you should've seen the prior indentation nightmare... */
5515 
5516 #define FREE_LIST(T,r) \
5517 	do { \
5518 		T *p, *node = r; \
5519 		while (node != NULL) { \
5520 			p = node; \
5521 			node = node->next; \
5522 			free(p); \
5523 		} \
5524 	} while (0)
5525 
5526 #define LOOP_THROUGH(T,n,r,C) \
5527 	do { \
5528 		T *n; \
5529 		if (r == NULL) { \
5530 			r = calloc(1, sizeof(T)); \
5531 			if (r == NULL) \
5532 				err(1, "LOOP: calloc"); \
5533 			r->next = NULL; \
5534 		} \
5535 		n = r; \
5536 		while (n != NULL) { \
5537 			do { \
5538 				C; \
5539 			} while (0); \
5540 			n = n->next; \
5541 		} \
5542 	} while (0)
5543 
5544 void
expand_label_str(char * label,size_t len,const char * srch,const char * repl)5545 expand_label_str(char *label, size_t len, const char *srch, const char *repl)
5546 {
5547 	char *tmp;
5548 	char *p, *q;
5549 
5550 	if ((tmp = calloc(1, len)) == NULL)
5551 		err(1, "expand_label_str: calloc");
5552 	p = q = label;
5553 	while ((q = strstr(p, srch)) != NULL) {
5554 		*q = '\0';
5555 		if ((strlcat(tmp, p, len) >= len) ||
5556 		    (strlcat(tmp, repl, len) >= len))
5557 			errx(1, "expand_label: label too long");
5558 		q += strlen(srch);
5559 		p = q;
5560 	}
5561 	if (strlcat(tmp, p, len) >= len)
5562 		errx(1, "expand_label: label too long");
5563 	strlcpy(label, tmp, len);	/* always fits */
5564 	free(tmp);
5565 }
5566 
5567 void
expand_label_if(const char * name,char * label,size_t len,const char * ifname)5568 expand_label_if(const char *name, char *label, size_t len, const char *ifname)
5569 {
5570 	if (strstr(label, name) != NULL) {
5571 		if (!*ifname)
5572 			expand_label_str(label, len, name, "any");
5573 		else
5574 			expand_label_str(label, len, name, ifname);
5575 	}
5576 }
5577 
5578 void
expand_label_addr(const char * name,char * label,size_t len,sa_family_t af,struct pf_rule_addr * addr)5579 expand_label_addr(const char *name, char *label, size_t len, sa_family_t af,
5580     struct pf_rule_addr *addr)
5581 {
5582 	char tmp[64], tmp_not[66];
5583 
5584 	if (strstr(label, name) != NULL) {
5585 		switch (addr->addr.type) {
5586 		case PF_ADDR_DYNIFTL:
5587 			snprintf(tmp, sizeof(tmp), "(%s)", addr->addr.v.ifname);
5588 			break;
5589 		case PF_ADDR_TABLE:
5590 			snprintf(tmp, sizeof(tmp), "<%s>", addr->addr.v.tblname);
5591 			break;
5592 		case PF_ADDR_NOROUTE:
5593 			snprintf(tmp, sizeof(tmp), "no-route");
5594 			break;
5595 		case PF_ADDR_URPFFAILED:
5596 			snprintf(tmp, sizeof(tmp), "urpf-failed");
5597 			break;
5598 		case PF_ADDR_ADDRMASK:
5599 			if (!af || (PF_AZERO(&addr->addr.v.a.addr, af) &&
5600 			    PF_AZERO(&addr->addr.v.a.mask, af)))
5601 				snprintf(tmp, sizeof(tmp), "any");
5602 			else {
5603 				char	a[48];
5604 				int	bits;
5605 
5606 				if (inet_ntop(af, &addr->addr.v.a.addr, a,
5607 				    sizeof(a)) == NULL)
5608 					snprintf(tmp, sizeof(tmp), "?");
5609 				else {
5610 					bits = unmask(&addr->addr.v.a.mask, af);
5611 					if ((af == AF_INET && bits < 32) ||
5612 					    (af == AF_INET6 && bits < 128))
5613 						snprintf(tmp, sizeof(tmp),
5614 						    "%s/%d", a, bits);
5615 					else
5616 						snprintf(tmp, sizeof(tmp),
5617 						    "%s", a);
5618 				}
5619 			}
5620 			break;
5621 		default:
5622 			snprintf(tmp, sizeof(tmp), "?");
5623 			break;
5624 		}
5625 
5626 		if (addr->neg) {
5627 			snprintf(tmp_not, sizeof(tmp_not), "! %s", tmp);
5628 			expand_label_str(label, len, name, tmp_not);
5629 		} else
5630 			expand_label_str(label, len, name, tmp);
5631 	}
5632 }
5633 
5634 void
expand_label_port(const char * name,char * label,size_t len,struct pf_rule_addr * addr)5635 expand_label_port(const char *name, char *label, size_t len,
5636     struct pf_rule_addr *addr)
5637 {
5638 	char	 a1[6], a2[6], op[13] = "";
5639 
5640 	if (strstr(label, name) != NULL) {
5641 		snprintf(a1, sizeof(a1), "%u", ntohs(addr->port[0]));
5642 		snprintf(a2, sizeof(a2), "%u", ntohs(addr->port[1]));
5643 		if (!addr->port_op)
5644 			;
5645 		else if (addr->port_op == PF_OP_IRG)
5646 			snprintf(op, sizeof(op), "%s><%s", a1, a2);
5647 		else if (addr->port_op == PF_OP_XRG)
5648 			snprintf(op, sizeof(op), "%s<>%s", a1, a2);
5649 		else if (addr->port_op == PF_OP_EQ)
5650 			snprintf(op, sizeof(op), "%s", a1);
5651 		else if (addr->port_op == PF_OP_NE)
5652 			snprintf(op, sizeof(op), "!=%s", a1);
5653 		else if (addr->port_op == PF_OP_LT)
5654 			snprintf(op, sizeof(op), "<%s", a1);
5655 		else if (addr->port_op == PF_OP_LE)
5656 			snprintf(op, sizeof(op), "<=%s", a1);
5657 		else if (addr->port_op == PF_OP_GT)
5658 			snprintf(op, sizeof(op), ">%s", a1);
5659 		else if (addr->port_op == PF_OP_GE)
5660 			snprintf(op, sizeof(op), ">=%s", a1);
5661 		expand_label_str(label, len, name, op);
5662 	}
5663 }
5664 
5665 void
expand_label_proto(const char * name,char * label,size_t len,u_int8_t proto)5666 expand_label_proto(const char *name, char *label, size_t len, u_int8_t proto)
5667 {
5668 	const char *protoname;
5669 	char n[4];
5670 
5671 	if (strstr(label, name) != NULL) {
5672 		protoname = pfctl_proto2name(proto);
5673 		if (protoname != NULL)
5674 			expand_label_str(label, len, name, protoname);
5675 		else {
5676 			snprintf(n, sizeof(n), "%u", proto);
5677 			expand_label_str(label, len, name, n);
5678 		}
5679 	}
5680 }
5681 
5682 void
expand_label_nr(const char * name,char * label,size_t len,struct pfctl_rule * r)5683 expand_label_nr(const char *name, char *label, size_t len,
5684     struct pfctl_rule *r)
5685 {
5686 	char n[11];
5687 
5688 	if (strstr(label, name) != NULL) {
5689 		snprintf(n, sizeof(n), "%u", r->nr);
5690 		expand_label_str(label, len, name, n);
5691 	}
5692 }
5693 
5694 void
expand_label(char * label,size_t len,struct pfctl_rule * r)5695 expand_label(char *label, size_t len, struct pfctl_rule *r)
5696 {
5697 	expand_label_if("$if", label, len, r->ifname);
5698 	expand_label_addr("$srcaddr", label, len, r->af, &r->src);
5699 	expand_label_addr("$dstaddr", label, len, r->af, &r->dst);
5700 	expand_label_port("$srcport", label, len, &r->src);
5701 	expand_label_port("$dstport", label, len, &r->dst);
5702 	expand_label_proto("$proto", label, len, r->proto);
5703 	expand_label_nr("$nr", label, len, r);
5704 }
5705 
5706 int
expand_altq(struct pf_altq * a,struct node_if * interfaces,struct node_queue * nqueues,struct node_queue_bw bwspec,struct node_queue_opt * opts)5707 expand_altq(struct pf_altq *a, struct node_if *interfaces,
5708     struct node_queue *nqueues, struct node_queue_bw bwspec,
5709     struct node_queue_opt *opts)
5710 {
5711 	struct pf_altq		 pa, pb;
5712 	char			 qname[PF_QNAME_SIZE];
5713 	struct node_queue	*n;
5714 	struct node_queue_bw	 bw;
5715 	int			 errs = 0;
5716 
5717 	if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
5718 		FREE_LIST(struct node_if, interfaces);
5719 		if (nqueues)
5720 			FREE_LIST(struct node_queue, nqueues);
5721 		return (0);
5722 	}
5723 
5724 	LOOP_THROUGH(struct node_if, interface, interfaces,
5725 		memcpy(&pa, a, sizeof(struct pf_altq));
5726 		if (strlcpy(pa.ifname, interface->ifname,
5727 		    sizeof(pa.ifname)) >= sizeof(pa.ifname))
5728 			errx(1, "expand_altq: strlcpy");
5729 
5730 		if (interface->not) {
5731 			yyerror("altq on ! <interface> is not supported");
5732 			errs++;
5733 		} else {
5734 			if (eval_pfaltq(pf, &pa, &bwspec, opts))
5735 				errs++;
5736 			else
5737 				if (pfctl_add_altq(pf, &pa))
5738 					errs++;
5739 
5740 			if (pf->opts & PF_OPT_VERBOSE) {
5741 				print_altq(&pf->paltq->altq, 0,
5742 				    &bwspec, opts);
5743 				if (nqueues && nqueues->tail) {
5744 					printf("queue { ");
5745 					LOOP_THROUGH(struct node_queue, queue,
5746 					    nqueues,
5747 						printf("%s ",
5748 						    queue->queue);
5749 					);
5750 					printf("}");
5751 				}
5752 				printf("\n");
5753 			}
5754 
5755 			if (pa.scheduler == ALTQT_CBQ ||
5756 			    pa.scheduler == ALTQT_HFSC ||
5757 			    pa.scheduler == ALTQT_FAIRQ) {
5758 				/* now create a root queue */
5759 				memset(&pb, 0, sizeof(struct pf_altq));
5760 				if (strlcpy(qname, "root_", sizeof(qname)) >=
5761 				    sizeof(qname))
5762 					errx(1, "expand_altq: strlcpy");
5763 				if (strlcat(qname, interface->ifname,
5764 				    sizeof(qname)) >= sizeof(qname))
5765 					errx(1, "expand_altq: strlcat");
5766 				if (strlcpy(pb.qname, qname,
5767 				    sizeof(pb.qname)) >= sizeof(pb.qname))
5768 					errx(1, "expand_altq: strlcpy");
5769 				if (strlcpy(pb.ifname, interface->ifname,
5770 				    sizeof(pb.ifname)) >= sizeof(pb.ifname))
5771 					errx(1, "expand_altq: strlcpy");
5772 				pb.qlimit = pa.qlimit;
5773 				pb.scheduler = pa.scheduler;
5774 				bw.bw_absolute = pa.ifbandwidth;
5775 				bw.bw_percent = 0;
5776 				if (eval_pfqueue(pf, &pb, &bw, opts))
5777 					errs++;
5778 				else
5779 					if (pfctl_add_altq(pf, &pb))
5780 						errs++;
5781 			}
5782 
5783 			LOOP_THROUGH(struct node_queue, queue, nqueues,
5784 				n = calloc(1, sizeof(struct node_queue));
5785 				if (n == NULL)
5786 					err(1, "expand_altq: calloc");
5787 				if (pa.scheduler == ALTQT_CBQ ||
5788 				    pa.scheduler == ALTQT_HFSC ||
5789 				    pa.scheduler == ALTQT_FAIRQ)
5790 					if (strlcpy(n->parent, qname,
5791 					    sizeof(n->parent)) >=
5792 					    sizeof(n->parent))
5793 						errx(1, "expand_altq: strlcpy");
5794 				if (strlcpy(n->queue, queue->queue,
5795 				    sizeof(n->queue)) >= sizeof(n->queue))
5796 					errx(1, "expand_altq: strlcpy");
5797 				if (strlcpy(n->ifname, interface->ifname,
5798 				    sizeof(n->ifname)) >= sizeof(n->ifname))
5799 					errx(1, "expand_altq: strlcpy");
5800 				n->scheduler = pa.scheduler;
5801 				n->next = NULL;
5802 				n->tail = n;
5803 				if (queues == NULL)
5804 					queues = n;
5805 				else {
5806 					queues->tail->next = n;
5807 					queues->tail = n;
5808 				}
5809 			);
5810 		}
5811 	);
5812 	FREE_LIST(struct node_if, interfaces);
5813 	if (nqueues)
5814 		FREE_LIST(struct node_queue, nqueues);
5815 
5816 	return (errs);
5817 }
5818 
5819 int
expand_queue(struct pf_altq * a,struct node_if * interfaces,struct node_queue * nqueues,struct node_queue_bw bwspec,struct node_queue_opt * opts)5820 expand_queue(struct pf_altq *a, struct node_if *interfaces,
5821     struct node_queue *nqueues, struct node_queue_bw bwspec,
5822     struct node_queue_opt *opts)
5823 {
5824 	struct node_queue	*n, *nq;
5825 	struct pf_altq		 pa;
5826 	u_int8_t		 found = 0;
5827 	u_int8_t		 errs = 0;
5828 
5829 	if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
5830 		FREE_LIST(struct node_queue, nqueues);
5831 		return (0);
5832 	}
5833 
5834 	if (queues == NULL) {
5835 		yyerror("queue %s has no parent", a->qname);
5836 		FREE_LIST(struct node_queue, nqueues);
5837 		return (1);
5838 	}
5839 
5840 	LOOP_THROUGH(struct node_if, interface, interfaces,
5841 		LOOP_THROUGH(struct node_queue, tqueue, queues,
5842 			if (!strncmp(a->qname, tqueue->queue, PF_QNAME_SIZE) &&
5843 			    (interface->ifname[0] == 0 ||
5844 			    (!interface->not && !strncmp(interface->ifname,
5845 			    tqueue->ifname, IFNAMSIZ)) ||
5846 			    (interface->not && strncmp(interface->ifname,
5847 			    tqueue->ifname, IFNAMSIZ)))) {
5848 				/* found ourself in queues */
5849 				found++;
5850 
5851 				memcpy(&pa, a, sizeof(struct pf_altq));
5852 
5853 				if (pa.scheduler != ALTQT_NONE &&
5854 				    pa.scheduler != tqueue->scheduler) {
5855 					yyerror("exactly one scheduler type "
5856 					    "per interface allowed");
5857 					return (1);
5858 				}
5859 				pa.scheduler = tqueue->scheduler;
5860 
5861 				/* scheduler dependent error checking */
5862 				switch (pa.scheduler) {
5863 				case ALTQT_PRIQ:
5864 					if (nqueues != NULL) {
5865 						yyerror("priq queues cannot "
5866 						    "have child queues");
5867 						return (1);
5868 					}
5869 					if (bwspec.bw_absolute > 0 ||
5870 					    bwspec.bw_percent < 100) {
5871 						yyerror("priq doesn't take "
5872 						    "bandwidth");
5873 						return (1);
5874 					}
5875 					break;
5876 				default:
5877 					break;
5878 				}
5879 
5880 				if (strlcpy(pa.ifname, tqueue->ifname,
5881 				    sizeof(pa.ifname)) >= sizeof(pa.ifname))
5882 					errx(1, "expand_queue: strlcpy");
5883 				if (strlcpy(pa.parent, tqueue->parent,
5884 				    sizeof(pa.parent)) >= sizeof(pa.parent))
5885 					errx(1, "expand_queue: strlcpy");
5886 
5887 				if (eval_pfqueue(pf, &pa, &bwspec, opts))
5888 					errs++;
5889 				else
5890 					if (pfctl_add_altq(pf, &pa))
5891 						errs++;
5892 
5893 				for (nq = nqueues; nq != NULL; nq = nq->next) {
5894 					if (!strcmp(a->qname, nq->queue)) {
5895 						yyerror("queue cannot have "
5896 						    "itself as child");
5897 						errs++;
5898 						continue;
5899 					}
5900 					n = calloc(1,
5901 					    sizeof(struct node_queue));
5902 					if (n == NULL)
5903 						err(1, "expand_queue: calloc");
5904 					if (strlcpy(n->parent, a->qname,
5905 					    sizeof(n->parent)) >=
5906 					    sizeof(n->parent))
5907 						errx(1, "expand_queue strlcpy");
5908 					if (strlcpy(n->queue, nq->queue,
5909 					    sizeof(n->queue)) >=
5910 					    sizeof(n->queue))
5911 						errx(1, "expand_queue strlcpy");
5912 					if (strlcpy(n->ifname, tqueue->ifname,
5913 					    sizeof(n->ifname)) >=
5914 					    sizeof(n->ifname))
5915 						errx(1, "expand_queue strlcpy");
5916 					n->scheduler = tqueue->scheduler;
5917 					n->next = NULL;
5918 					n->tail = n;
5919 					if (queues == NULL)
5920 						queues = n;
5921 					else {
5922 						queues->tail->next = n;
5923 						queues->tail = n;
5924 					}
5925 				}
5926 				if ((pf->opts & PF_OPT_VERBOSE) && (
5927 				    (found == 1 && interface->ifname[0] == 0) ||
5928 				    (found > 0 && interface->ifname[0] != 0))) {
5929 					print_queue(&pf->paltq->altq, 0,
5930 					    &bwspec, interface->ifname[0] != 0,
5931 					    opts);
5932 					if (nqueues && nqueues->tail) {
5933 						printf("{ ");
5934 						LOOP_THROUGH(struct node_queue,
5935 						    queue, nqueues,
5936 							printf("%s ",
5937 							    queue->queue);
5938 						);
5939 						printf("}");
5940 					}
5941 					printf("\n");
5942 				}
5943 			}
5944 		);
5945 	);
5946 
5947 	FREE_LIST(struct node_queue, nqueues);
5948 	FREE_LIST(struct node_if, interfaces);
5949 
5950 	if (!found) {
5951 		yyerror("queue %s has no parent", a->qname);
5952 		errs++;
5953 	}
5954 
5955 	if (errs)
5956 		return (1);
5957 	else
5958 		return (0);
5959 }
5960 
5961 static int
pf_af_to_proto(sa_family_t af)5962 pf_af_to_proto(sa_family_t af)
5963 {
5964 	if (af == AF_INET)
5965 		return (ETHERTYPE_IP);
5966 	if (af == AF_INET6)
5967 		return (ETHERTYPE_IPV6);
5968 
5969 	return (0);
5970 }
5971 
5972 void
expand_eth_rule(struct pfctl_eth_rule * r,struct node_if * interfaces,struct node_etherproto * protos,struct node_mac * srcs,struct node_mac * dsts,struct node_host * ipsrcs,struct node_host * ipdsts,const char * bridge_to,const char * anchor_call)5973 expand_eth_rule(struct pfctl_eth_rule *r,
5974     struct node_if *interfaces, struct node_etherproto *protos,
5975     struct node_mac *srcs, struct node_mac *dsts,
5976     struct node_host *ipsrcs, struct node_host *ipdsts,
5977     const char *bridge_to, const char *anchor_call)
5978 {
5979 	char tagname[PF_TAG_NAME_SIZE];
5980 	char match_tagname[PF_TAG_NAME_SIZE];
5981 	char qname[PF_QNAME_SIZE];
5982 
5983 	if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname))
5984 		errx(1, "expand_eth_rule: tagname");
5985 	if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >=
5986 	    sizeof(match_tagname))
5987 		errx(1, "expand_eth_rule: match_tagname");
5988 	if (strlcpy(qname, r->qname, sizeof(qname)) >= sizeof(qname))
5989 		errx(1, "expand_eth_rule: qname");
5990 
5991 	LOOP_THROUGH(struct node_if, interface, interfaces,
5992 	LOOP_THROUGH(struct node_etherproto, proto, protos,
5993 	LOOP_THROUGH(struct node_mac, src, srcs,
5994 	LOOP_THROUGH(struct node_mac, dst, dsts,
5995 	LOOP_THROUGH(struct node_host, ipsrc, ipsrcs,
5996 	LOOP_THROUGH(struct node_host, ipdst, ipdsts,
5997 		strlcpy(r->ifname, interface->ifname,
5998 		    sizeof(r->ifname));
5999 		r->ifnot = interface->not;
6000 		r->proto = proto->proto;
6001 		if (!r->proto && ipsrc->af)
6002 			r->proto = pf_af_to_proto(ipsrc->af);
6003 		else if (!r->proto && ipdst->af)
6004 			r->proto = pf_af_to_proto(ipdst->af);
6005 		bcopy(src->mac, r->src.addr, ETHER_ADDR_LEN);
6006 		bcopy(src->mask, r->src.mask, ETHER_ADDR_LEN);
6007 		r->src.neg = src->neg;
6008 		r->src.isset = src->isset;
6009 		r->ipsrc.addr = ipsrc->addr;
6010 		r->ipsrc.neg = ipsrc->not;
6011 		r->ipdst.addr = ipdst->addr;
6012 		r->ipdst.neg = ipdst->not;
6013 		bcopy(dst->mac, r->dst.addr, ETHER_ADDR_LEN);
6014 		bcopy(dst->mask, r->dst.mask, ETHER_ADDR_LEN);
6015 		r->dst.neg = dst->neg;
6016 		r->dst.isset = dst->isset;
6017 		r->nr = pf->eastack[pf->asd]->match++;
6018 
6019 		if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >=
6020 		    sizeof(r->tagname))
6021 			errx(1, "expand_eth_rule: r->tagname");
6022 		if (strlcpy(r->match_tagname, match_tagname,
6023 		    sizeof(r->match_tagname)) >= sizeof(r->match_tagname))
6024 			errx(1, "expand_eth_rule: r->match_tagname");
6025 		if (strlcpy(r->qname, qname, sizeof(r->qname)) >= sizeof(r->qname))
6026 			errx(1, "expand_eth_rule: r->qname");
6027 
6028 		if (bridge_to)
6029 			strlcpy(r->bridge_to, bridge_to, sizeof(r->bridge_to));
6030 
6031 		pfctl_append_eth_rule(pf, r, anchor_call);
6032 	))))));
6033 
6034 	FREE_LIST(struct node_if, interfaces);
6035 	FREE_LIST(struct node_etherproto, protos);
6036 	FREE_LIST(struct node_mac, srcs);
6037 	FREE_LIST(struct node_mac, dsts);
6038 	FREE_LIST(struct node_host, ipsrcs);
6039 	FREE_LIST(struct node_host, ipdsts);
6040 }
6041 
6042 void
expand_rule(struct pfctl_rule * r,struct node_if * interfaces,struct node_host * rpool_hosts,struct node_proto * protos,struct node_os * src_oses,struct node_host * src_hosts,struct node_port * src_ports,struct node_host * dst_hosts,struct node_port * dst_ports,struct node_uid * uids,struct node_gid * gids,struct node_if * rcv,struct node_icmp * icmp_types,const char * anchor_call)6043 expand_rule(struct pfctl_rule *r,
6044     struct node_if *interfaces, struct node_host *rpool_hosts,
6045     struct node_proto *protos, struct node_os *src_oses,
6046     struct node_host *src_hosts, struct node_port *src_ports,
6047     struct node_host *dst_hosts, struct node_port *dst_ports,
6048     struct node_uid *uids, struct node_gid *gids, struct node_if *rcv,
6049     struct node_icmp *icmp_types, const char *anchor_call)
6050 {
6051 	sa_family_t		 af = r->af;
6052 	int			 added = 0, error = 0;
6053 	char			 ifname[IF_NAMESIZE];
6054 	char			 label[PF_RULE_MAX_LABEL_COUNT][PF_RULE_LABEL_SIZE];
6055 	char			 tagname[PF_TAG_NAME_SIZE];
6056 	char			 match_tagname[PF_TAG_NAME_SIZE];
6057 	struct pf_pooladdr	*pa;
6058 	struct node_host	*h, *osrch, *odsth;
6059 	u_int8_t		 flags, flagset, keep_state;
6060 
6061 	memcpy(label, r->label, sizeof(r->label));
6062 	assert(sizeof(r->label) == sizeof(label));
6063 	if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname))
6064 		errx(1, "expand_rule: strlcpy");
6065 	if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >=
6066 	    sizeof(match_tagname))
6067 		errx(1, "expand_rule: strlcpy");
6068 	flags = r->flags;
6069 	flagset = r->flagset;
6070 	keep_state = r->keep_state;
6071 
6072 	LOOP_THROUGH(struct node_if, interface, interfaces,
6073 	LOOP_THROUGH(struct node_proto, proto, protos,
6074 	LOOP_THROUGH(struct node_icmp, icmp_type, icmp_types,
6075 	LOOP_THROUGH(struct node_host, src_host, src_hosts,
6076 	LOOP_THROUGH(struct node_host, dst_host, dst_hosts,
6077 	LOOP_THROUGH(struct node_port, src_port, src_ports,
6078 	LOOP_THROUGH(struct node_port, dst_port, dst_ports,
6079 	LOOP_THROUGH(struct node_os, src_os, src_oses,
6080 	LOOP_THROUGH(struct node_uid, uid, uids,
6081 	LOOP_THROUGH(struct node_gid, gid, gids,
6082 
6083 		r->af = af;
6084 		/* for link-local IPv6 address, interface must match up */
6085 		if ((r->af && src_host->af && r->af != src_host->af) ||
6086 		    (r->af && dst_host->af && r->af != dst_host->af) ||
6087 		    (src_host->af && dst_host->af &&
6088 		    src_host->af != dst_host->af) ||
6089 		    (src_host->ifindex && dst_host->ifindex &&
6090 		    src_host->ifindex != dst_host->ifindex) ||
6091 		    (src_host->ifindex && *interface->ifname &&
6092 		    src_host->ifindex != if_nametoindex(interface->ifname)) ||
6093 		    (dst_host->ifindex && *interface->ifname &&
6094 		    dst_host->ifindex != if_nametoindex(interface->ifname)))
6095 			continue;
6096 		if (!r->af && src_host->af)
6097 			r->af = src_host->af;
6098 		else if (!r->af && dst_host->af)
6099 			r->af = dst_host->af;
6100 
6101 		if (*interface->ifname)
6102 			strlcpy(r->ifname, interface->ifname,
6103 			    sizeof(r->ifname));
6104 		else if (if_indextoname(src_host->ifindex, ifname))
6105 			strlcpy(r->ifname, ifname, sizeof(r->ifname));
6106 		else if (if_indextoname(dst_host->ifindex, ifname))
6107 			strlcpy(r->ifname, ifname, sizeof(r->ifname));
6108 		else
6109 			memset(r->ifname, '\0', sizeof(r->ifname));
6110 
6111 		memcpy(r->label, label, sizeof(r->label));
6112 		if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >=
6113 		    sizeof(r->tagname))
6114 			errx(1, "expand_rule: strlcpy");
6115 		if (strlcpy(r->match_tagname, match_tagname,
6116 		    sizeof(r->match_tagname)) >= sizeof(r->match_tagname))
6117 			errx(1, "expand_rule: strlcpy");
6118 
6119 		osrch = odsth = NULL;
6120 		if (src_host->addr.type == PF_ADDR_DYNIFTL) {
6121 			osrch = src_host;
6122 			if ((src_host = gen_dynnode(src_host, r->af)) == NULL)
6123 				err(1, "expand_rule: calloc");
6124 		}
6125 		if (dst_host->addr.type == PF_ADDR_DYNIFTL) {
6126 			odsth = dst_host;
6127 			if ((dst_host = gen_dynnode(dst_host, r->af)) == NULL)
6128 				err(1, "expand_rule: calloc");
6129 		}
6130 
6131 		error += check_netmask(src_host, r->af);
6132 		error += check_netmask(dst_host, r->af);
6133 
6134 		r->ifnot = interface->not;
6135 		r->proto = proto->proto;
6136 		r->src.addr = src_host->addr;
6137 		r->src.neg = src_host->not;
6138 		r->src.port[0] = src_port->port[0];
6139 		r->src.port[1] = src_port->port[1];
6140 		r->src.port_op = src_port->op;
6141 		r->dst.addr = dst_host->addr;
6142 		r->dst.neg = dst_host->not;
6143 		r->dst.port[0] = dst_port->port[0];
6144 		r->dst.port[1] = dst_port->port[1];
6145 		r->dst.port_op = dst_port->op;
6146 		r->uid.op = uid->op;
6147 		r->uid.uid[0] = uid->uid[0];
6148 		r->uid.uid[1] = uid->uid[1];
6149 		r->gid.op = gid->op;
6150 		r->gid.gid[0] = gid->gid[0];
6151 		r->gid.gid[1] = gid->gid[1];
6152 		if (rcv) {
6153 			strlcpy(r->rcv_ifname, rcv->ifname,
6154 			    sizeof(r->rcv_ifname));
6155 		}
6156 		r->type = icmp_type->type;
6157 		r->code = icmp_type->code;
6158 
6159 		if ((keep_state == PF_STATE_MODULATE ||
6160 		    keep_state == PF_STATE_SYNPROXY) &&
6161 		    r->proto && r->proto != IPPROTO_TCP)
6162 			r->keep_state = PF_STATE_NORMAL;
6163 		else
6164 			r->keep_state = keep_state;
6165 
6166 		if (r->proto && r->proto != IPPROTO_TCP) {
6167 			r->flags = 0;
6168 			r->flagset = 0;
6169 		} else {
6170 			r->flags = flags;
6171 			r->flagset = flagset;
6172 		}
6173 		if (icmp_type->proto && r->proto != icmp_type->proto) {
6174 			yyerror("icmp-type mismatch");
6175 			error++;
6176 		}
6177 
6178 		if (src_os && src_os->os) {
6179 			r->os_fingerprint = pfctl_get_fingerprint(src_os->os);
6180 			if ((pf->opts & PF_OPT_VERBOSE2) &&
6181 			    r->os_fingerprint == PF_OSFP_NOMATCH)
6182 				fprintf(stderr,
6183 				    "warning: unknown '%s' OS fingerprint\n",
6184 				    src_os->os);
6185 		} else {
6186 			r->os_fingerprint = PF_OSFP_ANY;
6187 		}
6188 
6189 		TAILQ_INIT(&r->rpool.list);
6190 		for (h = rpool_hosts; h != NULL; h = h->next) {
6191 			pa = calloc(1, sizeof(struct pf_pooladdr));
6192 			if (pa == NULL)
6193 				err(1, "expand_rule: calloc");
6194 			pa->addr = h->addr;
6195 			if (h->ifname != NULL) {
6196 				if (strlcpy(pa->ifname, h->ifname,
6197 				    sizeof(pa->ifname)) >=
6198 				    sizeof(pa->ifname))
6199 					errx(1, "expand_rule: strlcpy");
6200 			} else
6201 				pa->ifname[0] = 0;
6202 			TAILQ_INSERT_TAIL(&r->rpool.list, pa, entries);
6203 		}
6204 
6205 		if (rule_consistent(r, anchor_call[0]) < 0 || error)
6206 			yyerror("skipping rule due to errors");
6207 		else {
6208 			r->nr = pf->astack[pf->asd]->match++;
6209 			pfctl_append_rule(pf, r, anchor_call);
6210 			added++;
6211 		}
6212 
6213 		if (osrch && src_host->addr.type == PF_ADDR_DYNIFTL) {
6214 			free(src_host);
6215 			src_host = osrch;
6216 		}
6217 		if (odsth && dst_host->addr.type == PF_ADDR_DYNIFTL) {
6218 			free(dst_host);
6219 			dst_host = odsth;
6220 		}
6221 
6222 	))))))))));
6223 
6224 	FREE_LIST(struct node_if, interfaces);
6225 	FREE_LIST(struct node_proto, protos);
6226 	FREE_LIST(struct node_host, src_hosts);
6227 	FREE_LIST(struct node_port, src_ports);
6228 	FREE_LIST(struct node_os, src_oses);
6229 	FREE_LIST(struct node_host, dst_hosts);
6230 	FREE_LIST(struct node_port, dst_ports);
6231 	FREE_LIST(struct node_uid, uids);
6232 	FREE_LIST(struct node_gid, gids);
6233 	FREE_LIST(struct node_icmp, icmp_types);
6234 	FREE_LIST(struct node_host, rpool_hosts);
6235 
6236 	if (!added)
6237 		yyerror("rule expands to no valid combination");
6238 }
6239 
6240 int
expand_skip_interface(struct node_if * interfaces)6241 expand_skip_interface(struct node_if *interfaces)
6242 {
6243 	int	errs = 0;
6244 
6245 	if (!interfaces || (!interfaces->next && !interfaces->not &&
6246 	    !strcmp(interfaces->ifname, "none"))) {
6247 		if (pf->opts & PF_OPT_VERBOSE)
6248 			printf("set skip on none\n");
6249 		errs = pfctl_set_interface_flags(pf, "", PFI_IFLAG_SKIP, 0);
6250 		return (errs);
6251 	}
6252 
6253 	if (pf->opts & PF_OPT_VERBOSE)
6254 		printf("set skip on {");
6255 	LOOP_THROUGH(struct node_if, interface, interfaces,
6256 		if (pf->opts & PF_OPT_VERBOSE)
6257 			printf(" %s", interface->ifname);
6258 		if (interface->not) {
6259 			yyerror("skip on ! <interface> is not supported");
6260 			errs++;
6261 		} else
6262 			errs += pfctl_set_interface_flags(pf,
6263 			    interface->ifname, PFI_IFLAG_SKIP, 1);
6264 	);
6265 	if (pf->opts & PF_OPT_VERBOSE)
6266 		printf(" }\n");
6267 
6268 	FREE_LIST(struct node_if, interfaces);
6269 
6270 	if (errs)
6271 		return (1);
6272 	else
6273 		return (0);
6274 }
6275 
6276 void
freehostlist(struct node_host * h)6277 freehostlist(struct node_host *h)
6278 {
6279 	FREE_LIST(struct node_host, h);
6280 }
6281 
6282 #undef FREE_LIST
6283 #undef LOOP_THROUGH
6284 
6285 int
check_rulestate(int desired_state)6286 check_rulestate(int desired_state)
6287 {
6288 	if (require_order && (rulestate > desired_state)) {
6289 		yyerror("Rules must be in order: options, ethernet, "
6290 		    "normalization, queueing, translation, filtering");
6291 		return (1);
6292 	}
6293 	rulestate = desired_state;
6294 	return (0);
6295 }
6296 
6297 int
kw_cmp(const void * k,const void * e)6298 kw_cmp(const void *k, const void *e)
6299 {
6300 	return (strcmp(k, ((const struct keywords *)e)->k_name));
6301 }
6302 
6303 int
lookup(char * s)6304 lookup(char *s)
6305 {
6306 	/* this has to be sorted always */
6307 	static const struct keywords keywords[] = {
6308 		{ "all",		ALL},
6309 		{ "allow-opts",		ALLOWOPTS},
6310 		{ "altq",		ALTQ},
6311 		{ "anchor",		ANCHOR},
6312 		{ "antispoof",		ANTISPOOF},
6313 		{ "any",		ANY},
6314 		{ "bandwidth",		BANDWIDTH},
6315 		{ "binat",		BINAT},
6316 		{ "binat-anchor",	BINATANCHOR},
6317 		{ "bitmask",		BITMASK},
6318 		{ "block",		BLOCK},
6319 		{ "block-policy",	BLOCKPOLICY},
6320 		{ "bridge-to",		BRIDGE_TO},
6321 		{ "buckets",		BUCKETS},
6322 		{ "cbq",		CBQ},
6323 		{ "code",		CODE},
6324 		{ "codelq",		CODEL},
6325 		{ "debug",		DEBUG},
6326 		{ "divert-reply",	DIVERTREPLY},
6327 		{ "divert-to",		DIVERTTO},
6328 		{ "dnpipe",		DNPIPE},
6329 		{ "dnqueue",		DNQUEUE},
6330 		{ "drop",		DROP},
6331 		{ "dup-to",		DUPTO},
6332 		{ "endpoint-independent", ENDPI},
6333 		{ "ether",		ETHER},
6334 		{ "fail-policy",	FAILPOLICY},
6335 		{ "fairq",		FAIRQ},
6336 		{ "fastroute",		FASTROUTE},
6337 		{ "file",		FILENAME},
6338 		{ "fingerprints",	FINGERPRINTS},
6339 		{ "flags",		FLAGS},
6340 		{ "floating",		FLOATING},
6341 		{ "flush",		FLUSH},
6342 		{ "for",		FOR},
6343 		{ "fragment",		FRAGMENT},
6344 		{ "from",		FROM},
6345 		{ "global",		GLOBAL},
6346 		{ "group",		GROUP},
6347 		{ "hfsc",		HFSC},
6348 		{ "hogs",		HOGS},
6349 		{ "hostid",		HOSTID},
6350 		{ "icmp-type",		ICMPTYPE},
6351 		{ "icmp6-type",		ICMP6TYPE},
6352 		{ "if-bound",		IFBOUND},
6353 		{ "in",			IN},
6354 		{ "include",		INCLUDE},
6355 		{ "inet",		INET},
6356 		{ "inet6",		INET6},
6357 		{ "interval",		INTERVAL},
6358 		{ "keep",		KEEP},
6359 		{ "keepcounters",	KEEPCOUNTERS},
6360 		{ "l3",			L3},
6361 		{ "label",		LABEL},
6362 		{ "limit",		LIMIT},
6363 		{ "linkshare",		LINKSHARE},
6364 		{ "load",		LOAD},
6365 		{ "log",		LOG},
6366 		{ "loginterface",	LOGINTERFACE},
6367 		{ "map-e-portset",	MAPEPORTSET},
6368 		{ "match",		MATCH},
6369 		{ "matches",	MATCHES},
6370 		{ "max",		MAXIMUM},
6371 		{ "max-mss",		MAXMSS},
6372 		{ "max-src-conn",	MAXSRCCONN},
6373 		{ "max-src-conn-rate",	MAXSRCCONNRATE},
6374 		{ "max-src-nodes",	MAXSRCNODES},
6375 		{ "max-src-states",	MAXSRCSTATES},
6376 		{ "min-ttl",		MINTTL},
6377 		{ "modulate",		MODULATE},
6378 		{ "nat",		NAT},
6379 		{ "nat-anchor",		NATANCHOR},
6380 		{ "no",			NO},
6381 		{ "no-df",		NODF},
6382 		{ "no-route",		NOROUTE},
6383 		{ "no-sync",		NOSYNC},
6384 		{ "on",			ON},
6385 		{ "optimization",	OPTIMIZATION},
6386 		{ "os",			OS},
6387 		{ "out",		OUT},
6388 		{ "overload",		OVERLOAD},
6389 		{ "pass",		PASS},
6390 		{ "pflow",		PFLOW},
6391 		{ "port",		PORT},
6392 		{ "prio",		PRIO},
6393 		{ "priority",		PRIORITY},
6394 		{ "priq",		PRIQ},
6395 		{ "probability",	PROBABILITY},
6396 		{ "proto",		PROTO},
6397 		{ "qlimit",		QLIMIT},
6398 		{ "queue",		QUEUE},
6399 		{ "quick",		QUICK},
6400 		{ "random",		RANDOM},
6401 		{ "random-id",		RANDOMID},
6402 		{ "rdr",		RDR},
6403 		{ "rdr-anchor",		RDRANCHOR},
6404 		{ "realtime",		REALTIME},
6405 		{ "reassemble",		REASSEMBLE},
6406 		{ "received-on",	RECEIVEDON},
6407 		{ "reply-to",		REPLYTO},
6408 		{ "require-order",	REQUIREORDER},
6409 		{ "return",		RETURN},
6410 		{ "return-icmp",	RETURNICMP},
6411 		{ "return-icmp6",	RETURNICMP6},
6412 		{ "return-rst",		RETURNRST},
6413 		{ "ridentifier",	RIDENTIFIER},
6414 		{ "round-robin",	ROUNDROBIN},
6415 		{ "route",		ROUTE},
6416 		{ "route-to",		ROUTETO},
6417 		{ "rtable",		RTABLE},
6418 		{ "rule",		RULE},
6419 		{ "ruleset-optimization",	RULESET_OPTIMIZATION},
6420 		{ "scrub",		SCRUB},
6421 		{ "set",		SET},
6422 		{ "set-tos",		SETTOS},
6423 		{ "skip",		SKIP},
6424 		{ "sloppy",		SLOPPY},
6425 		{ "source-hash",	SOURCEHASH},
6426 		{ "source-track",	SOURCETRACK},
6427 		{ "state",		STATE},
6428 		{ "state-defaults",	STATEDEFAULTS},
6429 		{ "state-policy",	STATEPOLICY},
6430 		{ "static-port",	STATICPORT},
6431 		{ "sticky-address",	STICKYADDRESS},
6432 		{ "syncookies",         SYNCOOKIES},
6433 		{ "synproxy",		SYNPROXY},
6434 		{ "table",		TABLE},
6435 		{ "tag",		TAG},
6436 		{ "tagged",		TAGGED},
6437 		{ "target",		TARGET},
6438 		{ "tbrsize",		TBRSIZE},
6439 		{ "timeout",		TIMEOUT},
6440 		{ "to",			TO},
6441 		{ "tos",		TOS},
6442 		{ "ttl",		TTL},
6443 		{ "upperlimit",		UPPERLIMIT},
6444 		{ "urpf-failed",	URPFFAILED},
6445 		{ "user",		USER},
6446 	};
6447 	const struct keywords	*p;
6448 
6449 	p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
6450 	    sizeof(keywords[0]), kw_cmp);
6451 
6452 	if (p) {
6453 		if (debug > 1)
6454 			fprintf(stderr, "%s: %d\n", s, p->k_val);
6455 		return (p->k_val);
6456 	} else {
6457 		if (debug > 1)
6458 			fprintf(stderr, "string: %s\n", s);
6459 		return (STRING);
6460 	}
6461 }
6462 
6463 #define MAXPUSHBACK	128
6464 
6465 static char	*parsebuf;
6466 static int	 parseindex;
6467 static char	 pushback_buffer[MAXPUSHBACK];
6468 static int	 pushback_index = 0;
6469 
6470 int
lgetc(int quotec)6471 lgetc(int quotec)
6472 {
6473 	int		c, next;
6474 
6475 	if (parsebuf) {
6476 		/* Read character from the parsebuffer instead of input. */
6477 		if (parseindex >= 0) {
6478 			c = parsebuf[parseindex++];
6479 			if (c != '\0')
6480 				return (c);
6481 			parsebuf = NULL;
6482 		} else
6483 			parseindex++;
6484 	}
6485 
6486 	if (pushback_index)
6487 		return (pushback_buffer[--pushback_index]);
6488 
6489 	if (quotec) {
6490 		if ((c = getc(file->stream)) == EOF) {
6491 			yyerror("reached end of file while parsing quoted string");
6492 			if (popfile() == EOF)
6493 				return (EOF);
6494 			return (quotec);
6495 		}
6496 		return (c);
6497 	}
6498 
6499 	while ((c = getc(file->stream)) == '\\') {
6500 		next = getc(file->stream);
6501 		if (next != '\n') {
6502 			c = next;
6503 			break;
6504 		}
6505 		yylval.lineno = file->lineno;
6506 		file->lineno++;
6507 	}
6508 
6509 	while (c == EOF) {
6510 		if (popfile() == EOF)
6511 			return (EOF);
6512 		c = getc(file->stream);
6513 	}
6514 	return (c);
6515 }
6516 
6517 int
lungetc(int c)6518 lungetc(int c)
6519 {
6520 	if (c == EOF)
6521 		return (EOF);
6522 	if (parsebuf) {
6523 		parseindex--;
6524 		if (parseindex >= 0)
6525 			return (c);
6526 	}
6527 	if (pushback_index < MAXPUSHBACK-1)
6528 		return (pushback_buffer[pushback_index++] = c);
6529 	else
6530 		return (EOF);
6531 }
6532 
6533 int
findeol(void)6534 findeol(void)
6535 {
6536 	int	c;
6537 
6538 	parsebuf = NULL;
6539 
6540 	/* skip to either EOF or the first real EOL */
6541 	while (1) {
6542 		if (pushback_index)
6543 			c = pushback_buffer[--pushback_index];
6544 		else
6545 			c = lgetc(0);
6546 		if (c == '\n') {
6547 			file->lineno++;
6548 			break;
6549 		}
6550 		if (c == EOF)
6551 			break;
6552 	}
6553 	return (ERROR);
6554 }
6555 
6556 int
yylex(void)6557 yylex(void)
6558 {
6559 	char	 buf[8096];
6560 	char	*p, *val;
6561 	int	 quotec, next, c;
6562 	int	 token;
6563 
6564 top:
6565 	p = buf;
6566 	while ((c = lgetc(0)) == ' ' || c == '\t')
6567 		; /* nothing */
6568 
6569 	yylval.lineno = file->lineno;
6570 	if (c == '#')
6571 		while ((c = lgetc(0)) != '\n' && c != EOF)
6572 			; /* nothing */
6573 	if (c == '$' && parsebuf == NULL) {
6574 		while (1) {
6575 			if ((c = lgetc(0)) == EOF)
6576 				return (0);
6577 
6578 			if (p + 1 >= buf + sizeof(buf) - 1) {
6579 				yyerror("string too long");
6580 				return (findeol());
6581 			}
6582 			if (isalnum(c) || c == '_') {
6583 				*p++ = (char)c;
6584 				continue;
6585 			}
6586 			*p = '\0';
6587 			lungetc(c);
6588 			break;
6589 		}
6590 		val = symget(buf);
6591 		if (val == NULL) {
6592 			yyerror("macro '%s' not defined", buf);
6593 			return (findeol());
6594 		}
6595 		parsebuf = val;
6596 		parseindex = 0;
6597 		goto top;
6598 	}
6599 
6600 	switch (c) {
6601 	case '\'':
6602 	case '"':
6603 		quotec = c;
6604 		while (1) {
6605 			if ((c = lgetc(quotec)) == EOF)
6606 				return (0);
6607 			if (c == '\n') {
6608 				file->lineno++;
6609 				continue;
6610 			} else if (c == '\\') {
6611 				if ((next = lgetc(quotec)) == EOF)
6612 					return (0);
6613 				if (next == quotec || c == ' ' || c == '\t')
6614 					c = next;
6615 				else if (next == '\n') {
6616 					file->lineno++;
6617 					continue;
6618 				}
6619 				else
6620 					lungetc(next);
6621 			} else if (c == quotec) {
6622 				*p = '\0';
6623 				break;
6624 			}
6625 			if (p + 1 >= buf + sizeof(buf) - 1) {
6626 				yyerror("string too long");
6627 				return (findeol());
6628 			}
6629 			*p++ = (char)c;
6630 		}
6631 		yylval.v.string = strdup(buf);
6632 		if (yylval.v.string == NULL)
6633 			err(1, "yylex: strdup");
6634 		return (STRING);
6635 		case '!':
6636 			next = lgetc(0);
6637 			if (next == '=')
6638 				return (NE);
6639 			lungetc(next);
6640 		break;
6641 	case '<':
6642 		next = lgetc(0);
6643 		if (next == '>') {
6644 			yylval.v.i = PF_OP_XRG;
6645 			return (PORTBINARY);
6646 		} else if (next == '=')
6647 			return (LE);
6648 		lungetc(next);
6649 		break;
6650 	case '>':
6651 		next = lgetc(0);
6652 		if (next == '<') {
6653 			yylval.v.i = PF_OP_IRG;
6654 			return (PORTBINARY);
6655 		} else if (next == '=')
6656 			return (GE);
6657 		lungetc(next);
6658 		break;
6659 	case '-':
6660 		next = lgetc(0);
6661 		if (next == '>')
6662 			return (ARROW);
6663 		lungetc(next);
6664 		break;
6665 	}
6666 
6667 #define allowed_to_end_number(x) \
6668 	(isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
6669 
6670 	if (c == '-' || isdigit(c)) {
6671 		do {
6672 			*p++ = c;
6673 			if ((unsigned)(p-buf) >= sizeof(buf)) {
6674 				yyerror("string too long");
6675 				return (findeol());
6676 			}
6677 		} while ((c = lgetc(0)) != EOF && isdigit(c));
6678 		lungetc(c);
6679 		if (p == buf + 1 && buf[0] == '-')
6680 			goto nodigits;
6681 		if (c == EOF || allowed_to_end_number(c)) {
6682 			const char *errstr = NULL;
6683 
6684 			*p = '\0';
6685 			yylval.v.number = strtonum(buf, LLONG_MIN,
6686 			    LLONG_MAX, &errstr);
6687 			if (errstr) {
6688 				yyerror("\"%s\" invalid number: %s",
6689 				    buf, errstr);
6690 				return (findeol());
6691 			}
6692 			return (NUMBER);
6693 		} else {
6694 nodigits:
6695 			while (p > buf + 1)
6696 				lungetc(*--p);
6697 			c = *--p;
6698 			if (c == '-')
6699 				return (c);
6700 		}
6701 	}
6702 
6703 #define allowed_in_string(x) \
6704 	(isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
6705 	x != '{' && x != '}' && x != '<' && x != '>' && \
6706 	x != '!' && x != '=' && x != '/' && x != '#' && \
6707 	x != ','))
6708 
6709 	if (isalnum(c) || c == ':' || c == '_') {
6710 		do {
6711 			*p++ = c;
6712 			if ((unsigned)(p-buf) >= sizeof(buf)) {
6713 				yyerror("string too long");
6714 				return (findeol());
6715 			}
6716 		} while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
6717 		lungetc(c);
6718 		*p = '\0';
6719 		if ((token = lookup(buf)) == STRING)
6720 			if ((yylval.v.string = strdup(buf)) == NULL)
6721 				err(1, "yylex: strdup");
6722 		return (token);
6723 	}
6724 	if (c == '\n') {
6725 		yylval.lineno = file->lineno;
6726 		file->lineno++;
6727 	}
6728 	if (c == EOF)
6729 		return (0);
6730 	return (c);
6731 }
6732 
6733 int
check_file_secrecy(int fd,const char * fname)6734 check_file_secrecy(int fd, const char *fname)
6735 {
6736 	struct stat	st;
6737 
6738 	if (fstat(fd, &st)) {
6739 		warn("cannot stat %s", fname);
6740 		return (-1);
6741 	}
6742 	if (st.st_uid != 0 && st.st_uid != getuid()) {
6743 		warnx("%s: owner not root or current user", fname);
6744 		return (-1);
6745 	}
6746 	if (st.st_mode & (S_IRWXG | S_IRWXO)) {
6747 		warnx("%s: group/world readable/writeable", fname);
6748 		return (-1);
6749 	}
6750 	return (0);
6751 }
6752 
6753 struct file *
pushfile(const char * name,int secret)6754 pushfile(const char *name, int secret)
6755 {
6756 	struct file	*nfile;
6757 
6758 	if ((nfile = calloc(1, sizeof(struct file))) == NULL ||
6759 	    (nfile->name = strdup(name)) == NULL) {
6760 		warn("malloc");
6761 		return (NULL);
6762 	}
6763 	if (TAILQ_FIRST(&files) == NULL && strcmp(nfile->name, "-") == 0) {
6764 		nfile->stream = stdin;
6765 		free(nfile->name);
6766 		if ((nfile->name = strdup("stdin")) == NULL) {
6767 			warn("strdup");
6768 			free(nfile);
6769 			return (NULL);
6770 		}
6771 	} else if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
6772 		warn("%s", nfile->name);
6773 		free(nfile->name);
6774 		free(nfile);
6775 		return (NULL);
6776 	} else if (secret &&
6777 	    check_file_secrecy(fileno(nfile->stream), nfile->name)) {
6778 		fclose(nfile->stream);
6779 		free(nfile->name);
6780 		free(nfile);
6781 		return (NULL);
6782 	}
6783 	nfile->lineno = 1;
6784 	TAILQ_INSERT_TAIL(&files, nfile, entry);
6785 	return (nfile);
6786 }
6787 
6788 int
popfile(void)6789 popfile(void)
6790 {
6791 	struct file	*prev;
6792 
6793 	if ((prev = TAILQ_PREV(file, files, entry)) != NULL) {
6794 		prev->errors += file->errors;
6795 		TAILQ_REMOVE(&files, file, entry);
6796 		fclose(file->stream);
6797 		free(file->name);
6798 		free(file);
6799 		file = prev;
6800 		return (0);
6801 	}
6802 	return (EOF);
6803 }
6804 
6805 int
parse_config(char * filename,struct pfctl * xpf)6806 parse_config(char *filename, struct pfctl *xpf)
6807 {
6808 	int		 errors = 0;
6809 	struct sym	*sym;
6810 
6811 	pf = xpf;
6812 	errors = 0;
6813 	rulestate = PFCTL_STATE_NONE;
6814 	returnicmpdefault = (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
6815 	returnicmp6default =
6816 	    (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
6817 	blockpolicy = PFRULE_DROP;
6818 	failpolicy = PFRULE_DROP;
6819 	require_order = 1;
6820 
6821 	if ((file = pushfile(filename, 0)) == NULL) {
6822 		warn("cannot open the main config file!");
6823 		return (-1);
6824 	}
6825 
6826 	yyparse();
6827 	errors = file->errors;
6828 	popfile();
6829 
6830 	/* Free macros and check which have not been used. */
6831 	while ((sym = TAILQ_FIRST(&symhead))) {
6832 		if ((pf->opts & PF_OPT_VERBOSE2) && !sym->used)
6833 			fprintf(stderr, "warning: macro '%s' not "
6834 			    "used\n", sym->nam);
6835 		free(sym->nam);
6836 		free(sym->val);
6837 		TAILQ_REMOVE(&symhead, sym, entry);
6838 		free(sym);
6839 	}
6840 
6841 	return (errors ? -1 : 0);
6842 }
6843 
6844 int
symset(const char * nam,const char * val,int persist)6845 symset(const char *nam, const char *val, int persist)
6846 {
6847 	struct sym	*sym;
6848 
6849 	for (sym = TAILQ_FIRST(&symhead); sym && strcmp(nam, sym->nam);
6850 	    sym = TAILQ_NEXT(sym, entry))
6851 		;	/* nothing */
6852 
6853 	if (sym != NULL) {
6854 		if (sym->persist == 1)
6855 			return (0);
6856 		else {
6857 			free(sym->nam);
6858 			free(sym->val);
6859 			TAILQ_REMOVE(&symhead, sym, entry);
6860 			free(sym);
6861 		}
6862 	}
6863 	if ((sym = calloc(1, sizeof(*sym))) == NULL)
6864 		return (-1);
6865 
6866 	sym->nam = strdup(nam);
6867 	if (sym->nam == NULL) {
6868 		free(sym);
6869 		return (-1);
6870 	}
6871 	sym->val = strdup(val);
6872 	if (sym->val == NULL) {
6873 		free(sym->nam);
6874 		free(sym);
6875 		return (-1);
6876 	}
6877 	sym->used = 0;
6878 	sym->persist = persist;
6879 	TAILQ_INSERT_TAIL(&symhead, sym, entry);
6880 	return (0);
6881 }
6882 
6883 int
pfctl_cmdline_symset(char * s)6884 pfctl_cmdline_symset(char *s)
6885 {
6886 	char	*sym, *val;
6887 	int	 ret;
6888 
6889 	if ((val = strrchr(s, '=')) == NULL)
6890 		return (-1);
6891 
6892 	if ((sym = malloc(strlen(s) - strlen(val) + 1)) == NULL)
6893 		err(1, "pfctl_cmdline_symset: malloc");
6894 
6895 	strlcpy(sym, s, strlen(s) - strlen(val) + 1);
6896 
6897 	ret = symset(sym, val + 1, 1);
6898 	free(sym);
6899 
6900 	return (ret);
6901 }
6902 
6903 char *
symget(const char * nam)6904 symget(const char *nam)
6905 {
6906 	struct sym	*sym;
6907 
6908 	TAILQ_FOREACH(sym, &symhead, entry)
6909 		if (strcmp(nam, sym->nam) == 0) {
6910 			sym->used = 1;
6911 			return (sym->val);
6912 		}
6913 	return (NULL);
6914 }
6915 
6916 void
mv_rules(struct pfctl_ruleset * src,struct pfctl_ruleset * dst)6917 mv_rules(struct pfctl_ruleset *src, struct pfctl_ruleset *dst)
6918 {
6919 	int i;
6920 	struct pfctl_rule *r;
6921 
6922 	for (i = 0; i < PF_RULESET_MAX; ++i) {
6923 		while ((r = TAILQ_FIRST(src->rules[i].active.ptr))
6924 		    != NULL) {
6925 			TAILQ_REMOVE(src->rules[i].active.ptr, r, entries);
6926 			TAILQ_INSERT_TAIL(dst->rules[i].active.ptr, r, entries);
6927 			dst->anchor->match++;
6928 		}
6929 		src->anchor->match = 0;
6930 		while ((r = TAILQ_FIRST(src->rules[i].inactive.ptr))
6931 		    != NULL) {
6932 			TAILQ_REMOVE(src->rules[i].inactive.ptr, r, entries);
6933 			TAILQ_INSERT_TAIL(dst->rules[i].inactive.ptr,
6934 				r, entries);
6935 		}
6936 	}
6937 }
6938 
6939 void
mv_eth_rules(struct pfctl_eth_ruleset * src,struct pfctl_eth_ruleset * dst)6940 mv_eth_rules(struct pfctl_eth_ruleset *src, struct pfctl_eth_ruleset *dst)
6941 {
6942 	struct pfctl_eth_rule *r;
6943 
6944 	while ((r = TAILQ_FIRST(&src->rules)) != NULL) {
6945 		TAILQ_REMOVE(&src->rules, r, entries);
6946 		TAILQ_INSERT_TAIL(&dst->rules, r, entries);
6947 		dst->anchor->match++;
6948 	}
6949 	src->anchor->match = 0;
6950 }
6951 
6952 void
decide_address_family(struct node_host * n,sa_family_t * af)6953 decide_address_family(struct node_host *n, sa_family_t *af)
6954 {
6955 	if (*af != 0 || n == NULL)
6956 		return;
6957 	*af = n->af;
6958 	while ((n = n->next) != NULL) {
6959 		if (n->af != *af) {
6960 			*af = 0;
6961 			return;
6962 		}
6963 	}
6964 }
6965 
6966 void
remove_invalid_hosts(struct node_host ** nh,sa_family_t * af)6967 remove_invalid_hosts(struct node_host **nh, sa_family_t *af)
6968 {
6969 	struct node_host	*n = *nh, *prev = NULL;
6970 
6971 	while (n != NULL) {
6972 		if (*af && n->af && n->af != *af) {
6973 			/* unlink and free n */
6974 			struct node_host *next = n->next;
6975 
6976 			/* adjust tail pointer */
6977 			if (n == (*nh)->tail)
6978 				(*nh)->tail = prev;
6979 			/* adjust previous node's next pointer */
6980 			if (prev == NULL)
6981 				*nh = next;
6982 			else
6983 				prev->next = next;
6984 			/* free node */
6985 			if (n->ifname != NULL)
6986 				free(n->ifname);
6987 			free(n);
6988 			n = next;
6989 		} else {
6990 			if (n->af && !*af)
6991 				*af = n->af;
6992 			prev = n;
6993 			n = n->next;
6994 		}
6995 	}
6996 }
6997 
6998 int
invalid_redirect(struct node_host * nh,sa_family_t af)6999 invalid_redirect(struct node_host *nh, sa_family_t af)
7000 {
7001 	if (!af) {
7002 		struct node_host *n;
7003 
7004 		/* tables and dyniftl are ok without an address family */
7005 		for (n = nh; n != NULL; n = n->next) {
7006 			if (n->addr.type != PF_ADDR_TABLE &&
7007 			    n->addr.type != PF_ADDR_DYNIFTL) {
7008 				yyerror("address family not given and "
7009 				    "translation address expands to multiple "
7010 				    "address families");
7011 				return (1);
7012 			}
7013 		}
7014 	}
7015 	if (nh == NULL) {
7016 		yyerror("no translation address with matching address family "
7017 		    "found.");
7018 		return (1);
7019 	}
7020 	return (0);
7021 }
7022 
7023 int
atoul(char * s,u_long * ulvalp)7024 atoul(char *s, u_long *ulvalp)
7025 {
7026 	u_long	 ulval;
7027 	char	*ep;
7028 
7029 	errno = 0;
7030 	ulval = strtoul(s, &ep, 0);
7031 	if (s[0] == '\0' || *ep != '\0')
7032 		return (-1);
7033 	if (errno == ERANGE && ulval == ULONG_MAX)
7034 		return (-1);
7035 	*ulvalp = ulval;
7036 	return (0);
7037 }
7038 
7039 int
getservice(char * n)7040 getservice(char *n)
7041 {
7042 	struct servent	*s;
7043 	u_long		 ulval;
7044 
7045 	if (atoul(n, &ulval) == 0) {
7046 		if (ulval > 65535) {
7047 			yyerror("illegal port value %lu", ulval);
7048 			return (-1);
7049 		}
7050 		return (htons(ulval));
7051 	} else {
7052 		s = getservbyname(n, "tcp");
7053 		if (s == NULL)
7054 			s = getservbyname(n, "udp");
7055 		if (s == NULL)
7056 			s = getservbyname(n, "sctp");
7057 		if (s == NULL) {
7058 			yyerror("unknown port %s", n);
7059 			return (-1);
7060 		}
7061 		return (s->s_port);
7062 	}
7063 }
7064 
7065 int
rule_label(struct pfctl_rule * r,char * s[PF_RULE_MAX_LABEL_COUNT])7066 rule_label(struct pfctl_rule *r, char *s[PF_RULE_MAX_LABEL_COUNT])
7067 {
7068 	for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++) {
7069 		if (s[i] == NULL)
7070 			return (0);
7071 
7072 		if (strlcpy(r->label[i], s[i], sizeof(r->label[0])) >=
7073 		    sizeof(r->label[0])) {
7074 			yyerror("rule label too long (max %d chars)",
7075 			    sizeof(r->label[0])-1);
7076 			return (-1);
7077 		}
7078 	}
7079 	return (0);
7080 }
7081 
7082 int
eth_rule_label(struct pfctl_eth_rule * r,char * s[PF_RULE_MAX_LABEL_COUNT])7083 eth_rule_label(struct pfctl_eth_rule *r, char *s[PF_RULE_MAX_LABEL_COUNT])
7084 {
7085 	for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++) {
7086 		if (s[i] == NULL)
7087 			return (0);
7088 
7089 		if (strlcpy(r->label[i], s[i], sizeof(r->label[0])) >=
7090 		    sizeof(r->label[0])) {
7091 			yyerror("rule label too long (max %d chars)",
7092 			    sizeof(r->label[0])-1);
7093 			return (-1);
7094 		}
7095 	}
7096 	return (0);
7097 }
7098 
7099 u_int16_t
parseicmpspec(char * w,sa_family_t af)7100 parseicmpspec(char *w, sa_family_t af)
7101 {
7102 	const struct icmpcodeent	*p;
7103 	u_long				 ulval;
7104 	u_int8_t			 icmptype;
7105 
7106 	if (af == AF_INET)
7107 		icmptype = returnicmpdefault >> 8;
7108 	else
7109 		icmptype = returnicmp6default >> 8;
7110 
7111 	if (atoul(w, &ulval) == -1) {
7112 		if ((p = geticmpcodebyname(icmptype, w, af)) == NULL) {
7113 			yyerror("unknown icmp code %s", w);
7114 			return (0);
7115 		}
7116 		ulval = p->code;
7117 	}
7118 	if (ulval > 255) {
7119 		yyerror("invalid icmp code %lu", ulval);
7120 		return (0);
7121 	}
7122 	return (icmptype << 8 | ulval);
7123 }
7124 
7125 int
parseport(char * port,struct range * r,int extensions)7126 parseport(char *port, struct range *r, int extensions)
7127 {
7128 	char	*p = strchr(port, ':');
7129 
7130 	if (p == NULL) {
7131 		if ((r->a = getservice(port)) == -1)
7132 			return (-1);
7133 		r->b = 0;
7134 		r->t = PF_OP_NONE;
7135 		return (0);
7136 	}
7137 	if ((extensions & PPORT_STAR) && !strcmp(p+1, "*")) {
7138 		*p = 0;
7139 		if ((r->a = getservice(port)) == -1)
7140 			return (-1);
7141 		r->b = 0;
7142 		r->t = PF_OP_IRG;
7143 		return (0);
7144 	}
7145 	if ((extensions & PPORT_RANGE)) {
7146 		*p++ = 0;
7147 		if ((r->a = getservice(port)) == -1 ||
7148 		    (r->b = getservice(p)) == -1)
7149 			return (-1);
7150 		if (r->a == r->b) {
7151 			r->b = 0;
7152 			r->t = PF_OP_NONE;
7153 		} else
7154 			r->t = PF_OP_RRG;
7155 		return (0);
7156 	}
7157 	return (-1);
7158 }
7159 
7160 int
pfctl_load_anchors(int dev,struct pfctl * pf,struct pfr_buffer * trans)7161 pfctl_load_anchors(int dev, struct pfctl *pf, struct pfr_buffer *trans)
7162 {
7163 	struct loadanchors	*la;
7164 
7165 	TAILQ_FOREACH(la, &loadanchorshead, entries) {
7166 		if (pf->opts & PF_OPT_VERBOSE)
7167 			fprintf(stderr, "\nLoading anchor %s from %s\n",
7168 			    la->anchorname, la->filename);
7169 		if (pfctl_rules(dev, la->filename, pf->opts, pf->optimize,
7170 		    la->anchorname, trans) == -1)
7171 			return (-1);
7172 	}
7173 
7174 	return (0);
7175 }
7176 
7177 int
kw_casecmp(const void * k,const void * e)7178 kw_casecmp(const void *k, const void *e)
7179 {
7180 	return (strcasecmp(k, ((const struct keywords *)e)->k_name));
7181 }
7182 
7183 int
map_tos(char * s,int * val)7184 map_tos(char *s, int *val)
7185 {
7186 	/* DiffServ Codepoints and other TOS mappings */
7187 	const struct keywords	 toswords[] = {
7188 		{ "af11",		IPTOS_DSCP_AF11 },
7189 		{ "af12",		IPTOS_DSCP_AF12 },
7190 		{ "af13",		IPTOS_DSCP_AF13 },
7191 		{ "af21",		IPTOS_DSCP_AF21 },
7192 		{ "af22",		IPTOS_DSCP_AF22 },
7193 		{ "af23",		IPTOS_DSCP_AF23 },
7194 		{ "af31",		IPTOS_DSCP_AF31 },
7195 		{ "af32",		IPTOS_DSCP_AF32 },
7196 		{ "af33",		IPTOS_DSCP_AF33 },
7197 		{ "af41",		IPTOS_DSCP_AF41 },
7198 		{ "af42",		IPTOS_DSCP_AF42 },
7199 		{ "af43",		IPTOS_DSCP_AF43 },
7200 		{ "critical",		IPTOS_PREC_CRITIC_ECP },
7201 		{ "cs0",		IPTOS_DSCP_CS0 },
7202 		{ "cs1",		IPTOS_DSCP_CS1 },
7203 		{ "cs2",		IPTOS_DSCP_CS2 },
7204 		{ "cs3",		IPTOS_DSCP_CS3 },
7205 		{ "cs4",		IPTOS_DSCP_CS4 },
7206 		{ "cs5",		IPTOS_DSCP_CS5 },
7207 		{ "cs6",		IPTOS_DSCP_CS6 },
7208 		{ "cs7",		IPTOS_DSCP_CS7 },
7209 		{ "ef",			IPTOS_DSCP_EF },
7210 		{ "inetcontrol",	IPTOS_PREC_INTERNETCONTROL },
7211 		{ "lowdelay",		IPTOS_LOWDELAY },
7212 		{ "netcontrol",		IPTOS_PREC_NETCONTROL },
7213 		{ "reliability",	IPTOS_RELIABILITY },
7214 		{ "throughput",		IPTOS_THROUGHPUT },
7215 		{ "va",			IPTOS_DSCP_VA }
7216 	};
7217 	const struct keywords	*p;
7218 
7219 	p = bsearch(s, toswords, sizeof(toswords)/sizeof(toswords[0]),
7220 	    sizeof(toswords[0]), kw_casecmp);
7221 
7222 	if (p) {
7223 		*val = p->k_val;
7224 		return (1);
7225 	}
7226 	return (0);
7227 }
7228 
7229 int
rt_tableid_max(void)7230 rt_tableid_max(void)
7231 {
7232 #ifdef __FreeBSD__
7233 	int fibs;
7234 	size_t l = sizeof(fibs);
7235 
7236         if (sysctlbyname("net.fibs", &fibs, &l, NULL, 0) == -1)
7237 		fibs = 16;	/* XXX RT_MAXFIBS, at least limit it some. */
7238 	/*
7239 	 * As the OpenBSD code only compares > and not >= we need to adjust
7240 	 * here given we only accept values of 0..n and want to avoid #ifdefs
7241 	 * in the grammar.
7242 	 */
7243 	return (fibs - 1);
7244 #else
7245 	return (RT_TABLEID_MAX);
7246 #endif
7247 }
7248 
7249 struct node_mac*
node_mac_from_string(const char * str)7250 node_mac_from_string(const char *str)
7251 {
7252 	struct node_mac *m;
7253 
7254 	m = calloc(1, sizeof(struct node_mac));
7255 	if (m == NULL)
7256 		err(1, "mac: calloc");
7257 
7258 	if (sscanf(str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
7259 	    &m->mac[0], &m->mac[1], &m->mac[2], &m->mac[3], &m->mac[4],
7260 	    &m->mac[5]) != 6) {
7261 		free(m);
7262 		yyerror("invalid MAC address");
7263 		return (NULL);
7264 	}
7265 
7266 	memset(m->mask, 0xff, ETHER_ADDR_LEN);
7267 	m->isset = true;
7268 	m->next = NULL;
7269 	m->tail = m;
7270 
7271 	return (m);
7272 }
7273 
7274 struct node_mac*
node_mac_from_string_masklen(const char * str,int masklen)7275 node_mac_from_string_masklen(const char *str, int masklen)
7276 {
7277 	struct node_mac *m;
7278 
7279 	if (masklen < 0 || masklen > (ETHER_ADDR_LEN * 8)) {
7280 		yyerror("invalid MAC mask length");
7281 		return (NULL);
7282 	}
7283 
7284 	m = node_mac_from_string(str);
7285 	if (m == NULL)
7286 		return (NULL);
7287 
7288 	memset(m->mask, 0, ETHER_ADDR_LEN);
7289 	for (int i = 0; i < masklen; i++)
7290 		m->mask[i / 8] |= 1 << (i % 8);
7291 
7292 	return (m);
7293 }
7294 
7295 struct node_mac*
node_mac_from_string_mask(const char * str,const char * mask)7296 node_mac_from_string_mask(const char *str, const char *mask)
7297 {
7298 	struct node_mac *m;
7299 
7300 	m = node_mac_from_string(str);
7301 	if (m == NULL)
7302 		return (NULL);
7303 
7304 	if (sscanf(mask, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
7305 	    &m->mask[0], &m->mask[1], &m->mask[2], &m->mask[3], &m->mask[4],
7306 	    &m->mask[5]) != 6) {
7307 		free(m);
7308 		yyerror("invalid MAC mask");
7309 		return (NULL);
7310 	}
7311 
7312 	return (m);
7313 }
7314