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