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