parse.y revision 126353
1/*	$OpenBSD: parse.y,v 1.415 2003/09/01 15:07:40 henning Exp $	*/
2
3/*
4 * Copyright (c) 2001 Markus Friedl.  All rights reserved.
5 * Copyright (c) 2001 Daniel Hartmeier.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27%{
28#include <sys/types.h>
29#include <sys/socket.h>
30#include <net/if.h>
31#include <netinet/in.h>
32#include <netinet/in_systm.h>
33#include <netinet/ip.h>
34#include <netinet/ip_icmp.h>
35#include <netinet/icmp6.h>
36#include <net/pfvar.h>
37#include <arpa/inet.h>
38#include <altq/altq.h>
39#include <altq/altq_cbq.h>
40#include <altq/altq_priq.h>
41#include <altq/altq_hfsc.h>
42
43#include <stdio.h>
44#include <stdlib.h>
45#include <netdb.h>
46#include <stdarg.h>
47#include <errno.h>
48#include <string.h>
49#include <ctype.h>
50#include <err.h>
51#include <pwd.h>
52#include <grp.h>
53#include <md5.h>
54
55#include "pfctl_parser.h"
56#include "pfctl.h"
57
58static struct pfctl	*pf = NULL;
59static FILE		*fin = NULL;
60static int		 debug = 0;
61static int		 lineno = 1;
62static int		 errors = 0;
63static int		 rulestate = 0;
64static u_int16_t	 returnicmpdefault =
65			    (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
66static u_int16_t	 returnicmp6default =
67			    (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
68static int		 blockpolicy = PFRULE_DROP;
69static int		 require_order = 1;
70
71enum {
72	PFCTL_STATE_NONE,
73	PFCTL_STATE_OPTION,
74	PFCTL_STATE_SCRUB,
75	PFCTL_STATE_QUEUE,
76	PFCTL_STATE_NAT,
77	PFCTL_STATE_FILTER
78};
79
80struct node_proto {
81	u_int8_t		 proto;
82	struct node_proto	*next;
83	struct node_proto	*tail;
84};
85
86struct node_port {
87	u_int16_t		 port[2];
88	u_int8_t		 op;
89	struct node_port	*next;
90	struct node_port	*tail;
91};
92
93struct node_uid {
94	uid_t			 uid[2];
95	u_int8_t		 op;
96	struct node_uid		*next;
97	struct node_uid		*tail;
98};
99
100struct node_gid {
101	gid_t			 gid[2];
102	u_int8_t		 op;
103	struct node_gid		*next;
104	struct node_gid		*tail;
105};
106
107struct node_icmp {
108	u_int8_t		 code;
109	u_int8_t		 type;
110	u_int8_t		 proto;
111	struct node_icmp	*next;
112	struct node_icmp	*tail;
113};
114
115enum	{ PF_STATE_OPT_MAX=0, PF_STATE_OPT_TIMEOUT=1 };
116struct node_state_opt {
117	int			 type;
118	union {
119		u_int32_t	 max_states;
120		struct {
121			int		number;
122			u_int32_t	seconds;
123		}		 timeout;
124	}			 data;
125	struct node_state_opt	*next;
126	struct node_state_opt	*tail;
127};
128
129struct peer {
130	struct node_host	*host;
131	struct node_port	*port;
132};
133
134struct node_queue {
135	char			 queue[PF_QNAME_SIZE];
136	char			 parent[PF_QNAME_SIZE];
137	char			 ifname[IFNAMSIZ];
138	int			 scheduler;
139	struct node_queue	*next;
140	struct node_queue	*tail;
141}	*queues = NULL;
142
143struct node_qassign {
144	char		*qname;
145	char		*pqname;
146};
147
148struct filter_opts {
149	int			 marker;
150#define FOM_FLAGS	0x01
151#define FOM_ICMP	0x02
152#define FOM_TOS		0x04
153#define FOM_KEEP	0x08
154	struct node_uid		*uid;
155	struct node_gid		*gid;
156	struct {
157		u_int8_t	 b1;
158		u_int8_t	 b2;
159		u_int16_t	 w;
160		u_int16_t	 w2;
161	} flags;
162	struct node_icmp	*icmpspec;
163	u_int32_t		 tos;
164	struct {
165		int			 action;
166		struct node_state_opt	*options;
167	} keep;
168	int			 fragment;
169	int			 allowopts;
170	char			*label;
171	struct node_qassign	 queues;
172	char			*tag;
173	char			*match_tag;
174	u_int8_t		 match_tag_not;
175} filter_opts;
176
177struct antispoof_opts {
178	char			*label;
179} antispoof_opts;
180
181struct scrub_opts {
182	int			marker;
183#define SOM_MINTTL	0x01
184#define SOM_MAXMSS	0x02
185#define SOM_FRAGCACHE	0x04
186	int			nodf;
187	int			minttl;
188	int			maxmss;
189	int			fragcache;
190	int			randomid;
191	int			reassemble_tcp;
192} scrub_opts;
193
194struct queue_opts {
195	int			marker;
196#define QOM_BWSPEC	0x01
197#define QOM_SCHEDULER	0x02
198#define QOM_PRIORITY	0x04
199#define QOM_TBRSIZE	0x08
200#define QOM_QLIMIT	0x10
201	struct node_queue_bw	queue_bwspec;
202	struct node_queue_opt	scheduler;
203	int			priority;
204	int			tbrsize;
205	int			qlimit;
206} queue_opts;
207
208struct table_opts {
209	int			flags;
210	int			init_addr;
211	struct node_tinithead	init_nodes;
212} table_opts;
213
214struct node_hfsc_opts	hfsc_opts;
215
216int	yyerror(const char *, ...);
217int	disallow_table(struct node_host *, const char *);
218int	rule_consistent(struct pf_rule *);
219int	filter_consistent(struct pf_rule *);
220int	nat_consistent(struct pf_rule *);
221int	rdr_consistent(struct pf_rule *);
222int	process_tabledef(char *, struct table_opts *);
223int	yyparse(void);
224void	expand_label_str(char *, const char *, const char *);
225void	expand_label_if(const char *, char *, const char *);
226void	expand_label_addr(const char *, char *, u_int8_t, struct node_host *);
227void	expand_label_port(const char *, char *, struct node_port *);
228void	expand_label_proto(const char *, char *, u_int8_t);
229void	expand_label_nr(const char *, char *);
230void	expand_label(char *, const char *, u_int8_t, struct node_host *,
231	    struct node_port *, struct node_host *, struct node_port *,
232	    u_int8_t);
233void	expand_rule(struct pf_rule *, struct node_if *, struct node_host *,
234	    struct node_proto *, struct node_os*, struct node_host *,
235	    struct node_port *, struct node_host *, struct node_port *,
236	    struct node_uid *, struct node_gid *, struct node_icmp *);
237int	expand_altq(struct pf_altq *, struct node_if *, struct node_queue *,
238	    struct node_queue_bw bwspec, struct node_queue_opt *);
239int	expand_queue(struct pf_altq *, struct node_if *, struct node_queue *,
240	    struct node_queue_bw, struct node_queue_opt *);
241
242int	 check_rulestate(int);
243int	 kw_cmp(const void *, const void *);
244int	 lookup(char *);
245int	 lgetc(FILE *);
246int	 lungetc(int);
247int	 findeol(void);
248int	 yylex(void);
249int	 atoul(char *, u_long *);
250int	 getservice(char *);
251int	 rule_label(struct pf_rule *, char *);
252
253TAILQ_HEAD(symhead, sym)	 symhead = TAILQ_HEAD_INITIALIZER(symhead);
254struct sym {
255	TAILQ_ENTRY(sym)	 entries;
256	int			 used;
257	int			 persist;
258	char			*nam;
259	char			*val;
260};
261
262
263int	 symset(const char *, const char *, int);
264char	*symget(const char *);
265
266void	 decide_address_family(struct node_host *, sa_family_t *);
267void	 remove_invalid_hosts(struct node_host **, sa_family_t *);
268int	 invalid_redirect(struct node_host *, sa_family_t);
269u_int16_t parseicmpspec(char *, sa_family_t);
270
271TAILQ_HEAD(loadanchorshead, loadanchors)	 loadanchorshead =
272   TAILQ_HEAD_INITIALIZER(loadanchorshead);
273struct loadanchors {
274	TAILQ_ENTRY(loadanchors)	 entries;
275	char				*anchorname;
276	char				*rulesetname;
277	char				*filename;
278};
279
280typedef struct {
281	union {
282		u_int32_t		 number;
283		int			 i;
284		char			*string;
285		struct {
286			u_int8_t	 b1;
287			u_int8_t	 b2;
288			u_int16_t	 w;
289			u_int16_t	 w2;
290		}			 b;
291		struct range {
292			int		 a;
293			int		 b;
294			int		 t;
295		}			 range;
296		struct node_if		*interface;
297		struct node_proto	*proto;
298		struct node_icmp	*icmp;
299		struct node_host	*host;
300		struct node_os		*os;
301		struct node_port	*port;
302		struct node_uid		*uid;
303		struct node_gid		*gid;
304		struct node_state_opt	*state_opt;
305		struct peer		 peer;
306		struct {
307			struct peer	 src, dst;
308			struct node_os	*src_os;
309		}			 fromto;
310		struct pf_poolhashkey	*hashkey;
311		struct {
312			struct node_host	*host;
313			u_int8_t		 rt;
314			u_int8_t		 pool_opts;
315			sa_family_t		 af;
316			struct pf_poolhashkey	*key;
317		}			 route;
318		struct redirection {
319			struct node_host	*host;
320			struct range		 rport;
321		}			*redirection;
322		struct {
323			int			 type;
324			struct pf_poolhashkey	*key;
325		}			 pooltype;
326		struct {
327			int			 action;
328			struct node_state_opt	*options;
329		}			 keep_state;
330		struct {
331			u_int8_t	 log;
332			u_int8_t	 quick;
333		}			 logquick;
334		struct node_queue	*queue;
335		struct node_queue_opt	 queue_options;
336		struct node_queue_bw	 queue_bwspec;
337		struct node_qassign	 qassign;
338		struct filter_opts	 filter_opts;
339		struct antispoof_opts	 antispoof_opts;
340		struct queue_opts	 queue_opts;
341		struct scrub_opts	 scrub_opts;
342		struct table_opts	 table_opts;
343		struct node_hfsc_opts	 hfsc_opts;
344	} v;
345	int lineno;
346} YYSTYPE;
347
348#define PREPARE_ANCHOR_RULE(r, a)				\
349	do {							\
350		memset(&(r), 0, sizeof(r));			\
351		if (strlcpy(r.anchorname, (a),			\
352		    sizeof(r.anchorname)) >=			\
353		    sizeof(r.anchorname)) {			\
354			yyerror("anchor name '%s' too long",	\
355			    (a));				\
356			YYERROR;				\
357		}						\
358	} while (0)
359
360%}
361
362%token	PASS BLOCK SCRUB RETURN IN OS OUT LOG LOGALL QUICK ON FROM TO FLAGS
363%token	RETURNRST RETURNICMP RETURNICMP6 PROTO INET INET6 ALL ANY ICMPTYPE
364%token	ICMP6TYPE CODE KEEP MODULATE STATE PORT RDR NAT BINAT ARROW NODF
365%token	MINTTL ERROR ALLOWOPTS FASTROUTE FILENAME ROUTETO DUPTO REPLYTO NO LABEL
366%token	NOROUTE FRAGMENT USER GROUP MAXMSS MAXIMUM TTL TOS DROP TABLE
367%token	REASSEMBLE FRAGDROP FRAGCROP ANCHOR NATANCHOR RDRANCHOR BINATANCHOR
368%token	SET OPTIMIZATION TIMEOUT LIMIT LOGINTERFACE BLOCKPOLICY RANDOMID
369%token	REQUIREORDER SYNPROXY FINGERPRINTS
370%token	ANTISPOOF FOR
371%token	BITMASK RANDOM SOURCEHASH ROUNDROBIN STATICPORT
372%token	ALTQ CBQ PRIQ HFSC BANDWIDTH TBRSIZE LINKSHARE REALTIME UPPERLIMIT
373%token	QUEUE PRIORITY QLIMIT
374%token	LOAD
375%token	TAGGED TAG
376%token	<v.string>		STRING
377%token	<v.i>			PORTBINARY
378%type	<v.interface>		interface if_list if_item_not if_item
379%type	<v.number>		number icmptype icmp6type uid gid
380%type	<v.number>		tos not yesno natpass
381%type	<v.i>			no dir log af fragcache
382%type	<v.i>			staticport unaryop
383%type	<v.b>			action nataction flags flag blockspec
384%type	<v.range>		port rport
385%type	<v.hashkey>		hashkey
386%type	<v.pooltype>		pooltype
387%type	<v.proto>		proto proto_list proto_item
388%type	<v.icmp>		icmpspec
389%type	<v.icmp>		icmp_list icmp_item
390%type	<v.icmp>		icmp6_list icmp6_item
391%type	<v.fromto>		fromto
392%type	<v.peer>		ipportspec from to
393%type	<v.host>		ipspec xhost host dynaddr host_list
394%type	<v.host>		redir_host_list redirspec
395%type	<v.host>		route_host route_host_list routespec
396%type	<v.os>			os xos os_list
397%type	<v.port>		portspec port_list port_item
398%type	<v.uid>			uids uid_list uid_item
399%type	<v.gid>			gids gid_list gid_item
400%type	<v.route>		route
401%type	<v.redirection>		redirection redirpool
402%type	<v.string>		label string tag
403%type	<v.keep_state>		keep
404%type	<v.state_opt>		state_opt_spec state_opt_list state_opt_item
405%type	<v.logquick>		logquick
406%type	<v.interface>		antispoof_ifspc antispoof_iflst
407%type	<v.qassign>		qname
408%type	<v.queue>		qassign qassign_list qassign_item
409%type	<v.queue_options>	scheduler
410%type	<v.number>		cbqflags_list cbqflags_item
411%type	<v.number>		priqflags_list priqflags_item
412%type	<v.hfsc_opts>		hfscopts_list hfscopts_item hfsc_opts
413%type	<v.queue_bwspec>	bandwidth
414%type	<v.filter_opts>		filter_opts filter_opt filter_opts_l
415%type	<v.antispoof_opts>	antispoof_opts antispoof_opt antispoof_opts_l
416%type	<v.queue_opts>		queue_opts queue_opt queue_opts_l
417%type	<v.scrub_opts>		scrub_opts scrub_opt scrub_opts_l
418%type	<v.table_opts>		table_opts table_opt table_opts_l
419%%
420
421ruleset		: /* empty */
422		| ruleset '\n'
423		| ruleset option '\n'
424		| ruleset scrubrule '\n'
425		| ruleset natrule '\n'
426		| ruleset binatrule '\n'
427		| ruleset pfrule '\n'
428		| ruleset anchorrule '\n'
429		| ruleset loadrule '\n'
430		| ruleset altqif '\n'
431		| ruleset queuespec '\n'
432		| ruleset varset '\n'
433		| ruleset antispoof '\n'
434		| ruleset tabledef '\n'
435		| ruleset error '\n'		{ errors++; }
436		;
437
438option		: SET OPTIMIZATION STRING		{
439			if (check_rulestate(PFCTL_STATE_OPTION))
440				YYERROR;
441			if (pfctl_set_optimization(pf, $3) != 0) {
442				yyerror("unknown optimization %s", $3);
443				YYERROR;
444			}
445		}
446		| SET TIMEOUT timeout_spec
447		| SET TIMEOUT '{' timeout_list '}'
448		| SET LIMIT limit_spec
449		| SET LIMIT '{' limit_list '}'
450		| SET LOGINTERFACE STRING		{
451			if (check_rulestate(PFCTL_STATE_OPTION))
452				YYERROR;
453			if ((ifa_exists($3) == NULL) && strcmp($3, "none")) {
454				yyerror("interface %s doesn't exist", $3);
455				YYERROR;
456			}
457			if (pfctl_set_logif(pf, $3) != 0) {
458				yyerror("error setting loginterface %s", $3);
459				YYERROR;
460			}
461		}
462		| SET BLOCKPOLICY DROP	{
463			if (pf->opts & PF_OPT_VERBOSE)
464				printf("set block-policy drop\n");
465			if (check_rulestate(PFCTL_STATE_OPTION))
466				YYERROR;
467			blockpolicy = PFRULE_DROP;
468		}
469		| SET BLOCKPOLICY RETURN {
470			if (pf->opts & PF_OPT_VERBOSE)
471				printf("set block-policy return\n");
472			if (check_rulestate(PFCTL_STATE_OPTION))
473				YYERROR;
474			blockpolicy = PFRULE_RETURN;
475		}
476		| SET REQUIREORDER yesno {
477			if (pf->opts & PF_OPT_VERBOSE)
478				printf("set require-order %s\n",
479				    $3 == 1 ? "yes" : "no");
480			require_order = $3;
481		}
482		| SET FINGERPRINTS STRING {
483			if (pf->opts & PF_OPT_VERBOSE)
484				printf("fingerprints %s\n", $3);
485			if (check_rulestate(PFCTL_STATE_OPTION))
486				YYERROR;
487			if (pfctl_file_fingerprints(pf->dev, pf->opts, $3)) {
488				yyerror("error loading fingerprints %s", $3);
489				YYERROR;
490			}
491		}
492		;
493
494string		: string STRING				{
495			if (asprintf(&$$, "%s %s", $1, $2) == -1)
496				err(1, "string: asprintf");
497			free($1);
498			free($2);
499		}
500		| STRING
501		;
502
503varset		: STRING '=' string		{
504			if (pf->opts & PF_OPT_VERBOSE)
505				printf("%s = \"%s\"\n", $1, $3);
506			if (symset($1, $3, 0) == -1)
507				err(1, "cannot store variable %s", $1);
508		}
509		;
510
511anchorrule	: ANCHOR string	dir interface af proto fromto {
512			struct pf_rule	r;
513
514			if (check_rulestate(PFCTL_STATE_FILTER))
515				YYERROR;
516
517			PREPARE_ANCHOR_RULE(r, $2);
518			r.direction = $3;
519			r.af = $5;
520
521			decide_address_family($7.src.host, &r.af);
522			decide_address_family($7.dst.host, &r.af);
523
524			expand_rule(&r, $4, NULL, $6, $7.src_os,
525			    $7.src.host, $7.src.port, $7.dst.host, $7.dst.port,
526			    0, 0, 0);
527		}
528		| NATANCHOR string interface af proto fromto {
529			struct pf_rule	r;
530
531			if (check_rulestate(PFCTL_STATE_NAT))
532				YYERROR;
533
534			PREPARE_ANCHOR_RULE(r, $2);
535			r.action = PF_NAT;
536			r.af = $4;
537
538			decide_address_family($6.src.host, &r.af);
539			decide_address_family($6.dst.host, &r.af);
540
541			expand_rule(&r, $3, NULL, $5, $6.src_os,
542			    $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
543			    0, 0, 0);
544		}
545		| RDRANCHOR string interface af proto fromto {
546			struct pf_rule	r;
547
548			if (check_rulestate(PFCTL_STATE_NAT))
549				YYERROR;
550
551			PREPARE_ANCHOR_RULE(r, $2);
552			r.action = PF_RDR;
553			r.af = $4;
554
555			decide_address_family($6.src.host, &r.af);
556			decide_address_family($6.dst.host, &r.af);
557
558			if ($6.src.port != NULL) {
559				yyerror("source port parameter not supported"
560				    " in rdr-anchor");
561				YYERROR;
562			}
563			if ($6.dst.port != NULL) {
564				if ($6.dst.port->next != NULL) {
565					yyerror("destination port list "
566					    "expansion not supported in "
567					    "rdr-anchor");
568					YYERROR;
569				} else if ($6.dst.port->op != PF_OP_EQ) {
570					yyerror("destination port operators"
571					    " not supported in rdr-anchor");
572					YYERROR;
573				}
574				r.dst.port[0] = $6.dst.port->port[0];
575				r.dst.port[1] = $6.dst.port->port[1];
576				r.dst.port_op = $6.dst.port->op;
577			}
578
579			expand_rule(&r, $3, NULL, $5, $6.src_os,
580			    $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
581			    0, 0, 0);
582		}
583		| BINATANCHOR string interface af proto fromto {
584			struct pf_rule	r;
585
586			if (check_rulestate(PFCTL_STATE_NAT))
587				YYERROR;
588
589			PREPARE_ANCHOR_RULE(r, $2);
590			r.action = PF_BINAT;
591			r.af = $4;
592			if ($5 != NULL) {
593				if ($5->next != NULL) {
594					yyerror("proto list expansion"
595					    " not supported in binat-anchor");
596					YYERROR;
597				}
598				r.proto = $5->proto;
599				free($5);
600			}
601
602			if ($6.src.host != NULL || $6.src.port != NULL ||
603			    $6.dst.host != NULL || $6.dst.port != NULL) {
604				yyerror("fromto parameter not supported"
605				    " in binat-anchor");
606				YYERROR;
607			}
608
609			decide_address_family($6.src.host, &r.af);
610			decide_address_family($6.dst.host, &r.af);
611
612			pfctl_add_rule(pf, &r);
613		}
614		;
615
616loadrule	: LOAD ANCHOR string FROM string	{
617			char			*t;
618			struct loadanchors	*loadanchor;
619
620			t = strsep(&$3, ":");
621			if (*t == '\0' || *$3 == '\0') {
622				yyerror("anchor '%s' invalid\n", $3);
623				YYERROR;
624			}
625			if (strlen(t) >= PF_ANCHOR_NAME_SIZE) {
626				yyerror("anchorname %s too long, max %u\n",
627				    t, PF_ANCHOR_NAME_SIZE - 1);
628				YYERROR;
629			}
630			if (strlen($3) >= PF_RULESET_NAME_SIZE) {
631				yyerror("rulesetname %s too long, max %u\n",
632				    $3, PF_RULESET_NAME_SIZE - 1);
633				YYERROR;
634			}
635
636			loadanchor = calloc(1, sizeof(struct loadanchors));
637			if (loadanchor == NULL)
638				err(1, "loadrule: calloc");
639			if ((loadanchor->anchorname = strdup(t)) == NULL)
640				err(1, "loadrule: strdup");
641			if ((loadanchor->rulesetname = strdup($3)) == NULL)
642				err(1, "loadrule: strdup");
643			if ((loadanchor->filename = strdup($5)) == NULL)
644				err(1, "loadrule: strdup");
645
646			TAILQ_INSERT_TAIL(&loadanchorshead, loadanchor,
647			    entries);
648
649			free(t); /* not $3 */
650			free($5);
651		};
652
653scrubrule	: SCRUB dir logquick interface af proto fromto scrub_opts
654		{
655			struct pf_rule	r;
656
657			if (check_rulestate(PFCTL_STATE_SCRUB))
658				YYERROR;
659
660			memset(&r, 0, sizeof(r));
661
662			r.action = PF_SCRUB;
663			r.direction = $2;
664
665			r.log = $3.log;
666			if ($3.quick) {
667				yyerror("scrub rules do not support 'quick'");
668				YYERROR;
669			}
670
671			if ($4) {
672				if ($4->not) {
673					yyerror("scrub rules do not support "
674					    "'! <if>'");
675					YYERROR;
676				}
677			}
678			r.af = $5;
679			if ($8.nodf)
680				r.rule_flag |= PFRULE_NODF;
681			if ($8.randomid)
682				r.rule_flag |= PFRULE_RANDOMID;
683			if ($8.reassemble_tcp) {
684				if (r.direction != PF_INOUT) {
685					yyerror("reassemble tcp rules can not "
686					    "specify direction");
687					YYERROR;
688				}
689				r.rule_flag |= PFRULE_REASSEMBLE_TCP;
690			}
691			if ($8.minttl)
692				r.min_ttl = $8.minttl;
693			if ($8.maxmss)
694				r.max_mss = $8.maxmss;
695			if ($8.fragcache)
696				r.rule_flag |= $8.fragcache;
697
698			expand_rule(&r, $4, NULL, $6, $7.src_os,
699			    $7.src.host, $7.src.port, $7.dst.host, $7.dst.port,
700			    NULL, NULL, NULL);
701		}
702		;
703
704scrub_opts	:	{
705			bzero(&scrub_opts, sizeof scrub_opts);
706		}
707		  scrub_opts_l
708			{ $$ = scrub_opts; }
709		| /* empty */ {
710			bzero(&scrub_opts, sizeof scrub_opts);
711			$$ = scrub_opts;
712		}
713		;
714
715scrub_opts_l	: scrub_opts_l scrub_opt
716		| scrub_opt
717		;
718
719scrub_opt	: NODF	{
720			if (scrub_opts.nodf) {
721				yyerror("no-df cannot be respecified");
722				YYERROR;
723			}
724			scrub_opts.nodf = 1;
725		}
726		| MINTTL number {
727			if (scrub_opts.marker & SOM_MINTTL) {
728				yyerror("min-ttl cannot be respecified");
729				YYERROR;
730			}
731			if ($2 > 255) {
732				yyerror("illegal min-ttl value %d", $2);
733				YYERROR;
734			}
735			scrub_opts.marker |= SOM_MINTTL;
736			scrub_opts.minttl = $2;
737		}
738		| MAXMSS number {
739			if (scrub_opts.marker & SOM_MAXMSS) {
740				yyerror("max-mss cannot be respecified");
741				YYERROR;
742			}
743			if ($2 > 65535) {
744				yyerror("illegal max-mss value %d", $2);
745				YYERROR;
746			}
747			scrub_opts.marker |= SOM_MAXMSS;
748			scrub_opts.maxmss = $2;
749		}
750		| fragcache {
751			if (scrub_opts.marker & SOM_FRAGCACHE) {
752				yyerror("fragcache cannot be respecified");
753				YYERROR;
754			}
755			scrub_opts.marker |= SOM_FRAGCACHE;
756			scrub_opts.fragcache = $1;
757		}
758		| REASSEMBLE STRING {
759			if (strcasecmp($2, "tcp") != 0)
760				YYERROR;
761			if (scrub_opts.reassemble_tcp) {
762				yyerror("reassemble tcp cannot be respecified");
763				YYERROR;
764			}
765			scrub_opts.reassemble_tcp = 1;
766		}
767		| RANDOMID {
768			if (scrub_opts.randomid) {
769				yyerror("random-id cannot be respecified");
770				YYERROR;
771			}
772			scrub_opts.randomid = 1;
773		}
774		;
775
776fragcache	: FRAGMENT REASSEMBLE	{ $$ = 0; /* default */ }
777		| FRAGMENT FRAGCROP	{ $$ = PFRULE_FRAGCROP; }
778		| FRAGMENT FRAGDROP	{ $$ = PFRULE_FRAGDROP; }
779		;
780
781antispoof	: ANTISPOOF logquick antispoof_ifspc af antispoof_opts {
782			struct pf_rule		 r;
783			struct node_host	*h = NULL;
784			struct node_if		*i, *j;
785
786			if (check_rulestate(PFCTL_STATE_FILTER))
787				YYERROR;
788
789			for (i = $3; i; i = i->next) {
790				bzero(&r, sizeof(r));
791
792				r.action = PF_DROP;
793				r.direction = PF_IN;
794				r.log = $2.log;
795				r.quick = $2.quick;
796				r.af = $4;
797				if (rule_label(&r, $5.label))
798					YYERROR;
799				j = calloc(1, sizeof(struct node_if));
800				if (j == NULL)
801					err(1, "antispoof: calloc");
802				if (strlcpy(j->ifname, i->ifname,
803				    sizeof(j->ifname)) >= sizeof(j->ifname)) {
804					free(j);
805					yyerror("interface name too long");
806					YYERROR;
807				}
808				j->not = 1;
809				h = ifa_lookup(j->ifname, PFCTL_IFLOOKUP_NET);
810
811				expand_rule(&r, j, NULL, NULL, NULL, h, NULL,
812				    NULL, NULL, NULL, NULL, NULL);
813
814				if ((i->ifa_flags & IFF_LOOPBACK) == 0) {
815					bzero(&r, sizeof(r));
816
817					r.action = PF_DROP;
818					r.direction = PF_IN;
819					r.log = $2.log;
820					r.quick = $2.quick;
821					r.af = $4;
822					if (rule_label(&r, $5.label))
823						YYERROR;
824					h = ifa_lookup(i->ifname,
825					    PFCTL_IFLOOKUP_HOST);
826					expand_rule(&r, NULL, NULL, NULL, NULL,
827					    h, NULL, NULL, NULL, NULL, NULL,
828					    NULL);
829				}
830			}
831			free($5.label);
832		}
833		;
834
835antispoof_ifspc	: FOR if_item			{ $$ = $2; }
836		| FOR '{' antispoof_iflst '}'	{ $$ = $3; }
837		;
838
839antispoof_iflst	: if_item			{ $$ = $1; }
840		| antispoof_iflst comma if_item	{
841			$1->tail->next = $3;
842			$1->tail = $3;
843			$$ = $1;
844		}
845		;
846
847antispoof_opts	:	{ bzero(&antispoof_opts, sizeof antispoof_opts); }
848		  antispoof_opts_l
849			{ $$ = antispoof_opts; }
850		| /* empty */	{
851			bzero(&antispoof_opts, sizeof antispoof_opts);
852			$$ = antispoof_opts;
853		}
854		;
855
856antispoof_opts_l	: antispoof_opts_l antispoof_opt
857			| antispoof_opt
858			;
859
860antispoof_opt	: label	{
861			if (antispoof_opts.label) {
862				yyerror("label cannot be redefined");
863				YYERROR;
864			}
865			antispoof_opts.label = $1;
866		}
867		;
868
869not		: '!'		{ $$ = 1; }
870		| /* empty */	{ $$ = 0; }
871
872tabledef	: TABLE '<' STRING '>' table_opts {
873			struct node_host	 *h, *nh;
874			struct node_tinit	 *ti, *nti;
875
876			if (strlen($3) >= PF_TABLE_NAME_SIZE) {
877				yyerror("table name too long, max %d chars",
878				    PF_TABLE_NAME_SIZE - 1);
879				YYERROR;
880			}
881			if (pf->loadopt & PFCTL_FLAG_TABLE)
882				if (process_tabledef($3, &$5))
883					YYERROR;
884			for (ti = SIMPLEQ_FIRST(&$5.init_nodes);
885			    ti != SIMPLEQ_END(&$5.init_nodes); ti = nti) {
886				if (ti->file)
887					free(ti->file);
888				for (h = ti->host; h != NULL; h = nh) {
889					nh = h->next;
890					free(h);
891				}
892				nti = SIMPLEQ_NEXT(ti, entries);
893				free (ti);
894			}
895		}
896		;
897
898table_opts	:	{
899			bzero(&table_opts, sizeof table_opts);
900			SIMPLEQ_INIT(&table_opts.init_nodes);
901		}
902		   table_opts_l
903			{ $$ = table_opts; }
904		| /* empty */
905			{
906			bzero(&table_opts, sizeof table_opts);
907			SIMPLEQ_INIT(&table_opts.init_nodes);
908			$$ = table_opts;
909		}
910		;
911
912table_opts_l	: table_opts_l table_opt
913		| table_opt
914		;
915
916table_opt	: STRING		{
917			if (!strcmp($1, "const"))
918				table_opts.flags |= PFR_TFLAG_CONST;
919			else if (!strcmp($1, "persist"))
920				table_opts.flags |= PFR_TFLAG_PERSIST;
921			else
922				YYERROR;
923		}
924		| '{' '}'		{ table_opts.init_addr = 1; }
925		| '{' host_list '}'	{
926			struct node_host	*n;
927			struct node_tinit	*ti;
928
929			for (n = $2; n != NULL; n = n->next) {
930				switch(n->addr.type) {
931				case PF_ADDR_ADDRMASK:
932					continue; /* ok */
933				case PF_ADDR_DYNIFTL:
934					yyerror("dynamic addresses are not "
935					    "permitted inside tables");
936					break;
937				case PF_ADDR_TABLE:
938					yyerror("tables cannot contain tables");
939					break;
940				case PF_ADDR_NOROUTE:
941					yyerror("\"no-route\" is not permitted "
942					    "inside tables");
943					break;
944				default:
945					yyerror("unknown address type %d",
946					    n->addr.type);
947				}
948				YYERROR;
949			}
950			if (!(ti = calloc(1, sizeof(*ti))))
951				err(1, "table_opt: calloc");
952			ti->host = $2;
953			SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
954			    entries);
955			table_opts.init_addr = 1;
956		}
957		| FILENAME STRING	{
958			struct node_tinit	*ti;
959
960			if (!(ti = calloc(1, sizeof(*ti))))
961				err(1, "table_opt: calloc");
962			ti->file = $2;
963			SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
964			    entries);
965			table_opts.init_addr = 1;
966		}
967		;
968
969altqif		: ALTQ interface queue_opts QUEUE qassign {
970			struct pf_altq	a;
971
972			if (check_rulestate(PFCTL_STATE_QUEUE))
973				YYERROR;
974
975			memset(&a, 0, sizeof(a));
976			if ($3.scheduler.qtype == ALTQT_NONE) {
977				yyerror("no scheduler specified!");
978				YYERROR;
979			}
980			a.scheduler = $3.scheduler.qtype;
981			a.qlimit = $3.qlimit;
982			a.tbrsize = $3.tbrsize;
983			if ($5 == NULL) {
984				yyerror("no child queues specified");
985				YYERROR;
986			}
987			if (expand_altq(&a, $2, $5, $3.queue_bwspec,
988			    &$3.scheduler))
989				YYERROR;
990		}
991		;
992
993queuespec	: QUEUE STRING interface queue_opts qassign {
994			struct pf_altq	a;
995
996			if (check_rulestate(PFCTL_STATE_QUEUE))
997				YYERROR;
998
999			memset(&a, 0, sizeof(a));
1000
1001			if (strlcpy(a.qname, $2, sizeof(a.qname)) >=
1002			    sizeof(a.qname)) {
1003				yyerror("queue name too long (max "
1004				    "%d chars)", PF_QNAME_SIZE-1);
1005				YYERROR;
1006			}
1007			if ($4.tbrsize) {
1008				yyerror("cannot specify tbrsize for queue");
1009				YYERROR;
1010			}
1011			if ($4.priority > 255) {
1012				yyerror("priority out of range: max 255");
1013				YYERROR;
1014			}
1015			a.priority = $4.priority;
1016			a.qlimit = $4.qlimit;
1017			a.scheduler = $4.scheduler.qtype;
1018			if (expand_queue(&a, $3, $5, $4.queue_bwspec,
1019			    &$4.scheduler)) {
1020				yyerror("errors in queue definition");
1021				YYERROR;
1022			}
1023		}
1024		;
1025
1026queue_opts	:	{
1027			bzero(&queue_opts, sizeof queue_opts);
1028			queue_opts.priority = DEFAULT_PRIORITY;
1029			queue_opts.qlimit = DEFAULT_QLIMIT;
1030			queue_opts.scheduler.qtype = ALTQT_NONE;
1031			queue_opts.queue_bwspec.bw_percent = 100;
1032		}
1033		  queue_opts_l
1034			{ $$ = queue_opts; }
1035		| /* empty */ {
1036			bzero(&queue_opts, sizeof queue_opts);
1037			queue_opts.priority = DEFAULT_PRIORITY;
1038			queue_opts.qlimit = DEFAULT_QLIMIT;
1039			queue_opts.scheduler.qtype = ALTQT_NONE;
1040			queue_opts.queue_bwspec.bw_percent = 100;
1041			$$ = queue_opts;
1042		}
1043		;
1044
1045queue_opts_l	: queue_opts_l queue_opt
1046		| queue_opt
1047		;
1048
1049queue_opt	: BANDWIDTH bandwidth	{
1050			if (queue_opts.marker & QOM_BWSPEC) {
1051				yyerror("bandwidth cannot be respecified");
1052				YYERROR;
1053			}
1054			queue_opts.marker |= QOM_BWSPEC;
1055			queue_opts.queue_bwspec = $2;
1056		}
1057		| PRIORITY number	{
1058			if (queue_opts.marker & QOM_PRIORITY) {
1059				yyerror("priority cannot be respecified");
1060				YYERROR;
1061			}
1062			if ($2 > 255) {
1063				yyerror("priority out of range: max 255");
1064				YYERROR;
1065			}
1066			queue_opts.marker |= QOM_PRIORITY;
1067			queue_opts.priority = $2;
1068		}
1069		| QLIMIT number	{
1070			if (queue_opts.marker & QOM_QLIMIT) {
1071				yyerror("qlimit cannot be respecified");
1072				YYERROR;
1073			}
1074			if ($2 > 65535) {
1075				yyerror("qlimit out of range: max 65535");
1076				YYERROR;
1077			}
1078			queue_opts.marker |= QOM_QLIMIT;
1079			queue_opts.qlimit = $2;
1080		}
1081		| scheduler	{
1082			if (queue_opts.marker & QOM_SCHEDULER) {
1083				yyerror("scheduler cannot be respecified");
1084				YYERROR;
1085			}
1086			queue_opts.marker |= QOM_SCHEDULER;
1087			queue_opts.scheduler = $1;
1088		}
1089		| TBRSIZE number	{
1090			if (queue_opts.marker & QOM_TBRSIZE) {
1091				yyerror("tbrsize cannot be respecified");
1092				YYERROR;
1093			}
1094			if ($2 > 65535) {
1095				yyerror("tbrsize too big: max 65535");
1096				YYERROR;
1097			}
1098			queue_opts.marker |= QOM_TBRSIZE;
1099			queue_opts.tbrsize = $2;
1100		}
1101		;
1102
1103bandwidth	: STRING {
1104			double	 bps;
1105			char	*cp;
1106
1107			$$.bw_percent = 0;
1108
1109			bps = strtod($1, &cp);
1110			if (cp != NULL) {
1111				if (!strcmp(cp, "b"))
1112					; /* nothing */
1113				else if (!strcmp(cp, "Kb"))
1114					bps *= 1000;
1115				else if (!strcmp(cp, "Mb"))
1116					bps *= 1000 * 1000;
1117				else if (!strcmp(cp, "Gb"))
1118					bps *= 1000 * 1000 * 1000;
1119				else if (!strcmp(cp, "%")) {
1120					if (bps < 0 || bps > 100) {
1121						yyerror("bandwidth spec "
1122						    "out of range");
1123						YYERROR;
1124					}
1125					$$.bw_percent = bps;
1126					bps = 0;
1127				} else {
1128					yyerror("unknown unit %s", cp);
1129					YYERROR;
1130				}
1131			}
1132			$$.bw_absolute = (u_int32_t)bps;
1133		}
1134
1135scheduler	: CBQ				{
1136			$$.qtype = ALTQT_CBQ;
1137			$$.data.cbq_opts.flags = 0;
1138		}
1139		| CBQ '(' cbqflags_list ')'	{
1140			$$.qtype = ALTQT_CBQ;
1141			$$.data.cbq_opts.flags = $3;
1142		}
1143		| PRIQ				{
1144			$$.qtype = ALTQT_PRIQ;
1145			$$.data.priq_opts.flags = 0;
1146		}
1147		| PRIQ '(' priqflags_list ')'	{
1148			$$.qtype = ALTQT_PRIQ;
1149			$$.data.priq_opts.flags = $3;
1150		}
1151		| HFSC				{
1152			$$.qtype = ALTQT_HFSC;
1153			bzero(&$$.data.hfsc_opts,
1154			    sizeof(struct node_hfsc_opts));
1155		}
1156		| HFSC '(' hfsc_opts ')'	{
1157			$$.qtype = ALTQT_HFSC;
1158			$$.data.hfsc_opts = $3;
1159		}
1160		;
1161
1162cbqflags_list	: cbqflags_item				{ $$ |= $1; }
1163		| cbqflags_list comma cbqflags_item	{ $$ |= $3; }
1164		;
1165
1166cbqflags_item	: STRING	{
1167			if (!strcmp($1, "default"))
1168				$$ = CBQCLF_DEFCLASS;
1169			else if (!strcmp($1, "borrow"))
1170				$$ = CBQCLF_BORROW;
1171			else if (!strcmp($1, "red"))
1172				$$ = CBQCLF_RED;
1173			else if (!strcmp($1, "ecn"))
1174				$$ = CBQCLF_RED|CBQCLF_ECN;
1175			else if (!strcmp($1, "rio"))
1176				$$ = CBQCLF_RIO;
1177			else {
1178				yyerror("unknown cbq flag \"%s\"", $1);
1179				YYERROR;
1180			}
1181		}
1182		;
1183
1184priqflags_list	: priqflags_item			{ $$ |= $1; }
1185		| priqflags_list comma priqflags_item	{ $$ |= $3; }
1186		;
1187
1188priqflags_item	: STRING	{
1189			if (!strcmp($1, "default"))
1190				$$ = PRCF_DEFAULTCLASS;
1191			else if (!strcmp($1, "red"))
1192				$$ = PRCF_RED;
1193			else if (!strcmp($1, "ecn"))
1194				$$ = PRCF_RED|PRCF_ECN;
1195			else if (!strcmp($1, "rio"))
1196				$$ = PRCF_RIO;
1197			else {
1198				yyerror("unknown priq flag \"%s\"", $1);
1199				YYERROR;
1200			}
1201		}
1202		;
1203
1204hfsc_opts	:	{
1205				bzero(&hfsc_opts,
1206				    sizeof(struct node_hfsc_opts));
1207			}
1208		  hfscopts_list				{
1209			$$ = hfsc_opts;
1210		}
1211		;
1212
1213hfscopts_list	: hfscopts_item
1214		| hfscopts_list comma hfscopts_item
1215		;
1216
1217hfscopts_item	: LINKSHARE bandwidth				{
1218			if (hfsc_opts.linkshare.used) {
1219				yyerror("linkshare already specified");
1220				YYERROR;
1221			}
1222			hfsc_opts.linkshare.m2 = $2;
1223			hfsc_opts.linkshare.used = 1;
1224		}
1225		| LINKSHARE '(' bandwidth number bandwidth ')'	{
1226			if (hfsc_opts.linkshare.used) {
1227				yyerror("linkshare already specified");
1228				YYERROR;
1229			}
1230			hfsc_opts.linkshare.m1 = $3;
1231			hfsc_opts.linkshare.d = $4;
1232			hfsc_opts.linkshare.m2 = $5;
1233			hfsc_opts.linkshare.used = 1;
1234		}
1235		| REALTIME bandwidth				{
1236			if (hfsc_opts.realtime.used) {
1237				yyerror("realtime already specified");
1238				YYERROR;
1239			}
1240			hfsc_opts.realtime.m2 = $2;
1241			hfsc_opts.realtime.used = 1;
1242		}
1243		| REALTIME '(' bandwidth number bandwidth ')'	{
1244			if (hfsc_opts.realtime.used) {
1245				yyerror("realtime already specified");
1246				YYERROR;
1247			}
1248			hfsc_opts.realtime.m1 = $3;
1249			hfsc_opts.realtime.d = $4;
1250			hfsc_opts.realtime.m2 = $5;
1251			hfsc_opts.realtime.used = 1;
1252		}
1253		| UPPERLIMIT bandwidth				{
1254			if (hfsc_opts.upperlimit.used) {
1255				yyerror("upperlimit already specified");
1256				YYERROR;
1257			}
1258			hfsc_opts.upperlimit.m2 = $2;
1259			hfsc_opts.upperlimit.used = 1;
1260		}
1261		| UPPERLIMIT '(' bandwidth number bandwidth ')'	{
1262			if (hfsc_opts.upperlimit.used) {
1263				yyerror("upperlimit already specified");
1264				YYERROR;
1265			}
1266			hfsc_opts.upperlimit.m1 = $3;
1267			hfsc_opts.upperlimit.d = $4;
1268			hfsc_opts.upperlimit.m2 = $5;
1269			hfsc_opts.upperlimit.used = 1;
1270		}
1271		| STRING	{
1272			if (!strcmp($1, "default"))
1273				hfsc_opts.flags |= HFCF_DEFAULTCLASS;
1274			else if (!strcmp($1, "red"))
1275				hfsc_opts.flags |= HFCF_RED;
1276			else if (!strcmp($1, "ecn"))
1277				hfsc_opts.flags |= HFCF_RED|HFCF_ECN;
1278			else if (!strcmp($1, "rio"))
1279				hfsc_opts.flags |= HFCF_RIO;
1280			else {
1281				yyerror("unknown hfsc flag \"%s\"", $1);
1282				YYERROR;
1283			}
1284		}
1285		;
1286
1287qassign		: /* empty */		{ $$ = NULL; }
1288		| qassign_item		{ $$ = $1; }
1289		| '{' qassign_list '}'	{ $$ = $2; }
1290		;
1291
1292qassign_list	: qassign_item			{ $$ = $1; }
1293		| qassign_list comma qassign_item	{
1294			$1->tail->next = $3;
1295			$1->tail = $3;
1296			$$ = $1;
1297		}
1298		;
1299
1300qassign_item	: STRING			{
1301			$$ = calloc(1, sizeof(struct node_queue));
1302			if ($$ == NULL)
1303				err(1, "qassign_item: calloc");
1304			if (strlcpy($$->queue, $1, sizeof($$->queue)) >=
1305			    sizeof($$->queue)) {
1306				free($$);
1307				yyerror("queue name '%s' too long (max "
1308				    "%d chars)", $1, sizeof($$->queue)-1);
1309				YYERROR;
1310			}
1311			$$->next = NULL;
1312			$$->tail = $$;
1313		}
1314		;
1315
1316pfrule		: action dir logquick interface route af proto fromto
1317		  filter_opts
1318		{
1319			struct pf_rule		 r;
1320			struct node_state_opt	*o;
1321			struct node_proto	*proto;
1322
1323			if (check_rulestate(PFCTL_STATE_FILTER))
1324				YYERROR;
1325
1326			memset(&r, 0, sizeof(r));
1327
1328			r.action = $1.b1;
1329			switch ($1.b2) {
1330			case PFRULE_RETURNRST:
1331				r.rule_flag |= PFRULE_RETURNRST;
1332				r.return_ttl = $1.w;
1333				break;
1334			case PFRULE_RETURNICMP:
1335				r.rule_flag |= PFRULE_RETURNICMP;
1336				r.return_icmp = $1.w;
1337				r.return_icmp6 = $1.w2;
1338				break;
1339			case PFRULE_RETURN:
1340				r.rule_flag |= PFRULE_RETURN;
1341				r.return_icmp = $1.w;
1342				r.return_icmp6 = $1.w2;
1343				break;
1344			}
1345			r.direction = $2;
1346			r.log = $3.log;
1347			r.quick = $3.quick;
1348
1349			r.af = $6;
1350			if ($9.tag)
1351				if (strlcpy(r.tagname, $9.tag,
1352				    PF_TAG_NAME_SIZE) > PF_TAG_NAME_SIZE) {
1353					yyerror("tag too long, max %u chars",
1354					    PF_TAG_NAME_SIZE - 1);
1355					YYERROR;
1356				}
1357			if ($9.match_tag)
1358				if (strlcpy(r.match_tagname, $9.match_tag,
1359				    PF_TAG_NAME_SIZE) > PF_TAG_NAME_SIZE) {
1360					yyerror("tag too long, max %u chars",
1361					    PF_TAG_NAME_SIZE - 1);
1362					YYERROR;
1363				}
1364			r.match_tag_not = $9.match_tag_not;
1365			r.flags = $9.flags.b1;
1366			r.flagset = $9.flags.b2;
1367			if (rule_label(&r, $9.label))
1368				YYERROR;
1369			free($9.label);
1370			if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
1371				for (proto = $7; proto != NULL &&
1372				    proto->proto != IPPROTO_TCP;
1373				    proto = proto->next)
1374					;	/* nothing */
1375				if (proto == NULL && $7 != NULL) {
1376					if ($9.flags.b1 || $9.flags.b2)
1377						yyerror(
1378						    "flags only apply to tcp");
1379					if ($8.src_os)
1380						yyerror(
1381						    "OS fingerprinting only "
1382						    "apply to tcp");
1383					YYERROR;
1384				}
1385#if 0
1386				if (($9.flags.b1 & parse_flags("S")) == 0 &&
1387				    $8.src_os) {
1388					yyerror("OS fingerprinting requires "
1389					     "the SYN TCP flag (flags S/SA)");
1390					YYERROR;
1391				}
1392#endif
1393			}
1394
1395			r.tos = $9.tos;
1396			r.keep_state = $9.keep.action;
1397			o = $9.keep.options;
1398			while (o) {
1399				struct node_state_opt	*p = o;
1400
1401				switch (o->type) {
1402				case PF_STATE_OPT_MAX:
1403					if (r.max_states) {
1404						yyerror("state option 'max' "
1405						    "multiple definitions");
1406						YYERROR;
1407					}
1408					r.max_states = o->data.max_states;
1409					break;
1410				case PF_STATE_OPT_TIMEOUT:
1411					if (r.timeout[o->data.timeout.number]) {
1412						yyerror("state timeout %s "
1413						    "multiple definitions",
1414						    pf_timeouts[o->data.
1415						    timeout.number].name);
1416						YYERROR;
1417					}
1418					r.timeout[o->data.timeout.number] =
1419					    o->data.timeout.seconds;
1420				}
1421				o = o->next;
1422				free(p);
1423			}
1424
1425			if ($9.fragment)
1426				r.rule_flag |= PFRULE_FRAGMENT;
1427			r.allow_opts = $9.allowopts;
1428
1429			decide_address_family($8.src.host, &r.af);
1430			decide_address_family($8.dst.host, &r.af);
1431
1432			if ($5.rt) {
1433				if (!r.direction) {
1434					yyerror("direction must be explicit "
1435					    "with rules that specify routing");
1436					YYERROR;
1437				}
1438				r.rt = $5.rt;
1439				r.rpool.opts = $5.pool_opts;
1440				if ($5.key != NULL)
1441					memcpy(&r.rpool.key, $5.key,
1442					    sizeof(struct pf_poolhashkey));
1443			}
1444			if (r.rt && r.rt != PF_FASTROUTE) {
1445				decide_address_family($5.host, &r.af);
1446				remove_invalid_hosts(&$5.host, &r.af);
1447				if ($5.host == NULL) {
1448					yyerror("no routing address with "
1449					    "matching address family found.");
1450					YYERROR;
1451				}
1452				if (r.rpool.opts == PF_POOL_NONE && (
1453				    $5.host->next != NULL ||
1454				    $5.host->addr.type == PF_ADDR_TABLE))
1455					r.rpool.opts = PF_POOL_ROUNDROBIN;
1456				if (r.rpool.opts != PF_POOL_ROUNDROBIN)
1457					if (disallow_table($5.host, "tables "
1458					    "are only supported in round-robin "
1459					    "routing pools"))
1460						YYERROR;
1461				if ($5.host->next != NULL) {
1462					if (r.rpool.opts !=
1463					    PF_POOL_ROUNDROBIN) {
1464						yyerror("r.rpool.opts must "
1465						    "be PF_POOL_ROUNDROBIN");
1466						YYERROR;
1467					}
1468				}
1469			}
1470			if ($9.queues.qname != NULL) {
1471				if (strlcpy(r.qname, $9.queues.qname,
1472				    sizeof(r.qname)) >= sizeof(r.qname)) {
1473					yyerror("rule qname too long (max "
1474					    "%d chars)", sizeof(r.qname)-1);
1475					YYERROR;
1476				}
1477				free($9.queues.qname);
1478			}
1479			if ($9.queues.pqname != NULL) {
1480				if (strlcpy(r.pqname, $9.queues.pqname,
1481				    sizeof(r.pqname)) >= sizeof(r.pqname)) {
1482					yyerror("rule pqname too long (max "
1483					    "%d chars)", sizeof(r.pqname)-1);
1484					YYERROR;
1485				}
1486				free($9.queues.pqname);
1487			}
1488
1489			expand_rule(&r, $4, $5.host, $7, $8.src_os,
1490			    $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
1491			    $9.uid, $9.gid, $9.icmpspec);
1492		}
1493		;
1494
1495filter_opts	:	{ bzero(&filter_opts, sizeof filter_opts); }
1496		  filter_opts_l
1497			{ $$ = filter_opts; }
1498		| /* empty */	{
1499			bzero(&filter_opts, sizeof filter_opts);
1500			$$ = filter_opts;
1501		}
1502		;
1503
1504filter_opts_l	: filter_opts_l filter_opt
1505		| filter_opt
1506		;
1507
1508filter_opt	: USER uids {
1509			if (filter_opts.uid)
1510				$2->tail->next = filter_opts.uid;
1511			filter_opts.uid = $2;
1512		}
1513		| GROUP gids {
1514			if (filter_opts.gid)
1515				$2->tail->next = filter_opts.gid;
1516			filter_opts.gid = $2;
1517		}
1518		| flags {
1519			if (filter_opts.marker & FOM_FLAGS) {
1520				yyerror("flags cannot be redefined");
1521				YYERROR;
1522			}
1523			filter_opts.marker |= FOM_FLAGS;
1524			filter_opts.flags.b1 |= $1.b1;
1525			filter_opts.flags.b2 |= $1.b2;
1526			filter_opts.flags.w |= $1.w;
1527			filter_opts.flags.w2 |= $1.w2;
1528		}
1529		| icmpspec {
1530			if (filter_opts.marker & FOM_ICMP) {
1531				yyerror("icmp-type cannot be redefined");
1532				YYERROR;
1533			}
1534			filter_opts.marker |= FOM_ICMP;
1535			filter_opts.icmpspec = $1;
1536		}
1537		| tos {
1538			if (filter_opts.marker & FOM_TOS) {
1539				yyerror("tos cannot be redefined");
1540				YYERROR;
1541			}
1542			filter_opts.marker |= FOM_TOS;
1543			filter_opts.tos = $1;
1544		}
1545		| keep {
1546			if (filter_opts.marker & FOM_KEEP) {
1547				yyerror("modulate or keep cannot be redefined");
1548				YYERROR;
1549			}
1550			filter_opts.marker |= FOM_KEEP;
1551			filter_opts.keep.action = $1.action;
1552			filter_opts.keep.options = $1.options;
1553		}
1554		| FRAGMENT {
1555			filter_opts.fragment = 1;
1556		}
1557		| ALLOWOPTS {
1558			filter_opts.allowopts = 1;
1559		}
1560		| label	{
1561			if (filter_opts.label) {
1562				yyerror("label cannot be redefined");
1563				YYERROR;
1564			}
1565			filter_opts.label = $1;
1566		}
1567		| qname	{
1568			if (filter_opts.queues.qname) {
1569				yyerror("queue cannot be redefined");
1570				YYERROR;
1571			}
1572			filter_opts.queues = $1;
1573		}
1574		| TAG string				{
1575			filter_opts.tag = $2;
1576		}
1577		| not TAGGED string			{
1578			filter_opts.match_tag = $3;
1579			filter_opts.match_tag_not = $1;
1580		}
1581		;
1582
1583action		: PASS			{ $$.b1 = PF_PASS; $$.b2 = $$.w = 0; }
1584		| BLOCK blockspec	{ $$ = $2; $$.b1 = PF_DROP; }
1585		;
1586
1587blockspec	: /* empty */		{
1588			$$.b2 = blockpolicy;
1589			$$.w = returnicmpdefault;
1590			$$.w2 = returnicmp6default;
1591		}
1592		| DROP			{
1593			$$.b2 = PFRULE_DROP;
1594			$$.w = 0;
1595			$$.w2 = 0;
1596		}
1597		| RETURNRST		{
1598			$$.b2 = PFRULE_RETURNRST;
1599			$$.w = 0;
1600			$$.w2 = 0;
1601		}
1602		| RETURNRST '(' TTL number ')'	{
1603			if ($4 > 255) {
1604				yyerror("illegal ttl value %d", $4);
1605				YYERROR;
1606			}
1607			$$.b2 = PFRULE_RETURNRST;
1608			$$.w = $4;
1609			$$.w2 = 0;
1610		}
1611		| RETURNICMP		{
1612			$$.b2 = PFRULE_RETURNICMP;
1613			$$.w = returnicmpdefault;
1614			$$.w2 = returnicmp6default;
1615		}
1616		| RETURNICMP6		{
1617			$$.b2 = PFRULE_RETURNICMP;
1618			$$.w = returnicmpdefault;
1619			$$.w2 = returnicmp6default;
1620		}
1621		| RETURNICMP '(' STRING ')'	{
1622			$$.b2 = PFRULE_RETURNICMP;
1623			if (!($$.w = parseicmpspec($3, AF_INET)))
1624				YYERROR;
1625			$$.w2 = returnicmp6default;
1626		}
1627		| RETURNICMP6 '(' STRING ')'	{
1628			$$.b2 = PFRULE_RETURNICMP;
1629			$$.w = returnicmpdefault;
1630			if (!($$.w2 = parseicmpspec($3, AF_INET6)))
1631				YYERROR;
1632		}
1633		| RETURNICMP '(' STRING comma STRING ')' {
1634			$$.b2 = PFRULE_RETURNICMP;
1635			if (!($$.w = parseicmpspec($3, AF_INET)))
1636				YYERROR;
1637			if (!($$.w2 = parseicmpspec($5, AF_INET6)))
1638				YYERROR;
1639		}
1640		| RETURN {
1641			$$.b2 = PFRULE_RETURN;
1642			$$.w = returnicmpdefault;
1643			$$.w2 = returnicmp6default;
1644		}
1645		;
1646
1647dir		: /* empty */			{ $$ = 0; }
1648		| IN				{ $$ = PF_IN; }
1649		| OUT				{ $$ = PF_OUT; }
1650		;
1651
1652logquick	: /* empty */			{ $$.log = 0; $$.quick = 0; }
1653		| log				{ $$.log = $1; $$.quick = 0; }
1654		| QUICK				{ $$.log = 0; $$.quick = 1; }
1655		| log QUICK			{ $$.log = $1; $$.quick = 1; }
1656		| QUICK log			{ $$.log = $2; $$.quick = 1; }
1657		;
1658
1659log		: LOG				{ $$ = 1; }
1660		| LOGALL			{ $$ = 2; }
1661		;
1662
1663interface	: /* empty */			{ $$ = NULL; }
1664		| ON if_item_not		{ $$ = $2; }
1665		| ON '{' if_list '}'		{ $$ = $3; }
1666		;
1667
1668if_list		: if_item_not			{ $$ = $1; }
1669		| if_list comma if_item_not	{
1670			$1->tail->next = $3;
1671			$1->tail = $3;
1672			$$ = $1;
1673		}
1674		;
1675
1676if_item_not	: not if_item			{ $$ = $2; $$->not = $1; }
1677		;
1678
1679if_item		: STRING			{
1680			struct node_host	*n;
1681
1682			if ((n = ifa_exists($1)) == NULL) {
1683				yyerror("unknown interface %s", $1);
1684				YYERROR;
1685			}
1686			$$ = calloc(1, sizeof(struct node_if));
1687			if ($$ == NULL)
1688				err(1, "if_item: calloc");
1689			if (strlcpy($$->ifname, $1, sizeof($$->ifname)) >=
1690			    sizeof($$->ifname)) {
1691				free($$);
1692				yyerror("interface name too long");
1693				YYERROR;
1694			}
1695			$$->ifa_flags = n->ifa_flags;
1696			$$->not = 0;
1697			$$->next = NULL;
1698			$$->tail = $$;
1699		}
1700		;
1701
1702af		: /* empty */			{ $$ = 0; }
1703		| INET				{ $$ = AF_INET; }
1704		| INET6				{ $$ = AF_INET6; }
1705
1706proto		: /* empty */			{ $$ = NULL; }
1707		| PROTO proto_item		{ $$ = $2; }
1708		| PROTO '{' proto_list '}'	{ $$ = $3; }
1709		;
1710
1711proto_list	: proto_item			{ $$ = $1; }
1712		| proto_list comma proto_item	{
1713			$1->tail->next = $3;
1714			$1->tail = $3;
1715			$$ = $1;
1716		}
1717		;
1718
1719proto_item	: STRING			{
1720			u_int8_t	pr;
1721			u_long		ulval;
1722
1723			if (atoul($1, &ulval) == 0) {
1724				if (ulval > 255) {
1725					yyerror("protocol outside range");
1726					YYERROR;
1727				}
1728				pr = (u_int8_t)ulval;
1729			} else {
1730				struct protoent	*p;
1731
1732				p = getprotobyname($1);
1733				if (p == NULL) {
1734					yyerror("unknown protocol %s", $1);
1735					YYERROR;
1736				}
1737				pr = p->p_proto;
1738			}
1739			if (pr == 0) {
1740				yyerror("proto 0 cannot be used");
1741				YYERROR;
1742			}
1743			$$ = calloc(1, sizeof(struct node_proto));
1744			if ($$ == NULL)
1745				err(1, "proto_item: calloc");
1746			$$->proto = pr;
1747			$$->next = NULL;
1748			$$->tail = $$;
1749		}
1750		;
1751
1752fromto		: ALL				{
1753			$$.src.host = NULL;
1754			$$.src.port = NULL;
1755			$$.dst.host = NULL;
1756			$$.dst.port = NULL;
1757			$$.src_os = NULL;
1758		}
1759		| from os to			{
1760			$$.src = $1;
1761			$$.src_os = $2;
1762			$$.dst = $3;
1763		}
1764		;
1765
1766os		: /* empty */			{ $$ = NULL; }
1767		| OS xos			{ $$ = $2; }
1768		| OS '{' os_list '}'		{ $$ = $3; }
1769		;
1770
1771xos		: STRING {
1772			$$ = calloc(1, sizeof(struct node_os));
1773			if ($$ == NULL)
1774				err(1, "os: calloc");
1775			$$->os = $1;
1776			$$->tail = $$;
1777		}
1778		;
1779
1780os_list		: xos				{ $$ = $1; }
1781		| os_list comma xos		{
1782			$1->tail->next = $3;
1783			$1->tail = $3;
1784			$$ = $1;
1785		}
1786		;
1787
1788from		: /* empty */			{
1789			$$.host = NULL;
1790			$$.port = NULL;
1791		}
1792		| FROM ipportspec		{
1793			$$ = $2;
1794		}
1795		;
1796
1797to		: /* empty */			{
1798			$$.host = NULL;
1799			$$.port = NULL;
1800		}
1801		| TO ipportspec		{
1802			$$ = $2;
1803		}
1804		;
1805
1806ipportspec	: ipspec			{
1807			$$.host = $1;
1808			$$.port = NULL;
1809		}
1810		| ipspec PORT portspec		{
1811			$$.host = $1;
1812			$$.port = $3;
1813		}
1814		| PORT portspec			{
1815			$$.host = NULL;
1816			$$.port = $2;
1817		}
1818		;
1819
1820ipspec		: ANY				{ $$ = NULL; }
1821		| xhost				{ $$ = $1; }
1822		| '{' host_list '}'		{ $$ = $2; }
1823		;
1824
1825host_list	: xhost				{ $$ = $1; }
1826		| host_list comma xhost		{
1827			if ($3 == NULL)
1828				$$ = $1;
1829			else if ($1 == NULL)
1830				$$ = $3;
1831			else {
1832				$1->tail->next = $3;
1833				$1->tail = $3->tail;
1834				$$ = $1;
1835			}
1836		}
1837		;
1838
1839xhost		: not host			{
1840			struct node_host	*n;
1841
1842			for (n = $2; n != NULL; n = n->next)
1843				n->not = $1;
1844			$$ = $2;
1845		}
1846		| NOROUTE			{
1847			$$ = calloc(1, sizeof(struct node_host));
1848			if ($$ == NULL)
1849				err(1, "xhost: calloc");
1850			$$->addr.type = PF_ADDR_NOROUTE;
1851			$$->next = NULL;
1852			$$->tail = $$;
1853		}
1854		;
1855
1856host		: STRING			{
1857			if (($$ = host($1)) == NULL)	{
1858				/* error. "any" is handled elsewhere */
1859				yyerror("could not parse host specification");
1860				YYERROR;
1861			}
1862
1863		}
1864		| STRING '/' number		{
1865			char	*buf;
1866
1867			if (asprintf(&buf, "%s/%u", $1, $3) == -1)
1868				err(1, "host: asprintf");
1869			if (($$ = host(buf)) == NULL)	{
1870				/* error. "any" is handled elsewhere */
1871				free(buf);
1872				yyerror("could not parse host specification");
1873				YYERROR;
1874			}
1875			free(buf);
1876		}
1877		| dynaddr
1878		| dynaddr '/' number		{
1879			struct node_host	*n;
1880
1881			$$ = $1;
1882			for (n = $1; n != NULL; n = n->next)
1883				set_ipmask(n, $3);
1884		}
1885		| '<' STRING '>'	{
1886			if (strlen($2) >= PF_TABLE_NAME_SIZE) {
1887				yyerror("table name '%s' too long");
1888				YYERROR;
1889			}
1890			$$ = calloc(1, sizeof(struct node_host));
1891			if ($$ == NULL)
1892				err(1, "host: calloc");
1893			$$->addr.type = PF_ADDR_TABLE;
1894			if (strlcpy($$->addr.v.tblname, $2,
1895			    sizeof($$->addr.v.tblname)) >=
1896			    sizeof($$->addr.v.tblname))
1897				errx(1, "host: strlcpy");
1898			$$->next = NULL;
1899			$$->tail = $$;
1900		}
1901		;
1902
1903number		: STRING			{
1904			u_long	ulval;
1905
1906			if (atoul($1, &ulval) == -1) {
1907				yyerror("%s is not a number", $1);
1908				YYERROR;
1909			} else
1910				$$ = ulval;
1911		}
1912		;
1913
1914dynaddr		: '(' STRING ')'		{
1915			if (ifa_exists($2) == NULL) {
1916				yyerror("interface %s does not exist", $2);
1917				YYERROR;
1918			}
1919			$$ = calloc(1, sizeof(struct node_host));
1920			if ($$ == NULL)
1921				err(1, "address: calloc");
1922			$$->af = 0;
1923			set_ipmask($$, 128);
1924			$$->addr.type = PF_ADDR_DYNIFTL;
1925			if (strlcpy($$->addr.v.ifname, $2,
1926			    sizeof($$->addr.v.ifname)) >=
1927			    sizeof($$->addr.v.ifname)) {
1928				free($$);
1929				yyerror("interface name too long");
1930				YYERROR;
1931			}
1932			$$->next = NULL;
1933			$$->tail = $$;
1934		}
1935		;
1936
1937portspec	: port_item			{ $$ = $1; }
1938		| '{' port_list '}'		{ $$ = $2; }
1939		;
1940
1941port_list	: port_item			{ $$ = $1; }
1942		| port_list comma port_item	{
1943			$1->tail->next = $3;
1944			$1->tail = $3;
1945			$$ = $1;
1946		}
1947		;
1948
1949port_item	: port				{
1950			$$ = calloc(1, sizeof(struct node_port));
1951			if ($$ == NULL)
1952				err(1, "port_item: calloc");
1953			$$->port[0] = $1.a;
1954			$$->port[1] = $1.b;
1955			if ($1.t)
1956				$$->op = PF_OP_RRG;
1957			else
1958				$$->op = PF_OP_EQ;
1959			$$->next = NULL;
1960			$$->tail = $$;
1961		}
1962		| unaryop port		{
1963			if ($2.t) {
1964				yyerror("':' cannot be used with an other "
1965				    "port operator");
1966				YYERROR;
1967			}
1968			$$ = calloc(1, sizeof(struct node_port));
1969			if ($$ == NULL)
1970				err(1, "port_item: calloc");
1971			$$->port[0] = $2.a;
1972			$$->port[1] = $2.b;
1973			$$->op = $1;
1974			$$->next = NULL;
1975			$$->tail = $$;
1976		}
1977		| port PORTBINARY port		{
1978			if ($1.t || $3.t) {
1979				yyerror("':' cannot be used with an other "
1980				    "port operator");
1981				YYERROR;
1982			}
1983			$$ = calloc(1, sizeof(struct node_port));
1984			if ($$ == NULL)
1985				err(1, "port_item: calloc");
1986			$$->port[0] = $1.a;
1987			$$->port[1] = $3.a;
1988			$$->op = $2;
1989			$$->next = NULL;
1990			$$->tail = $$;
1991		}
1992		;
1993
1994port		: STRING			{
1995			char	*p = strchr($1, ':');
1996			struct servent	*s = NULL;
1997			u_long		 ulval;
1998
1999			if (p == NULL) {
2000				if (atoul($1, &ulval) == 0) {
2001					if (ulval > 65535) {
2002						yyerror("illegal port value %d",
2003						    ulval);
2004						YYERROR;
2005					}
2006					$$.a = htons(ulval);
2007				} else {
2008					s = getservbyname($1, "tcp");
2009					if (s == NULL)
2010						s = getservbyname($1, "udp");
2011					if (s == NULL) {
2012						yyerror("unknown port %s", $1);
2013						YYERROR;
2014					}
2015					$$.a = s->s_port;
2016				}
2017				$$.b = 0;
2018				$$.t = 0;
2019			} else {
2020				int port[2];
2021
2022				*p++ = 0;
2023				if ((port[0] = getservice($1)) == -1 ||
2024				    (port[1] = getservice(p)) == -1)
2025					YYERROR;
2026				$$.a = port[0];
2027				$$.b = port[1];
2028				$$.t = PF_OP_RRG;
2029			}
2030		}
2031		;
2032
2033uids		: uid_item			{ $$ = $1; }
2034		| '{' uid_list '}'		{ $$ = $2; }
2035		;
2036
2037uid_list	: uid_item			{ $$ = $1; }
2038		| uid_list comma uid_item	{
2039			$1->tail->next = $3;
2040			$1->tail = $3;
2041			$$ = $1;
2042		}
2043		;
2044
2045uid_item	: uid				{
2046			$$ = calloc(1, sizeof(struct node_uid));
2047			if ($$ == NULL)
2048				err(1, "uid_item: calloc");
2049			$$->uid[0] = $1;
2050			$$->uid[1] = $1;
2051			$$->op = PF_OP_EQ;
2052			$$->next = NULL;
2053			$$->tail = $$;
2054		}
2055		| unaryop uid			{
2056			if ($2 == UID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
2057				yyerror("user unknown requires operator = or "
2058				    "!=");
2059				YYERROR;
2060			}
2061			$$ = calloc(1, sizeof(struct node_uid));
2062			if ($$ == NULL)
2063				err(1, "uid_item: calloc");
2064			$$->uid[0] = $2;
2065			$$->uid[1] = $2;
2066			$$->op = $1;
2067			$$->next = NULL;
2068			$$->tail = $$;
2069		}
2070		| uid PORTBINARY uid		{
2071			if ($1 == UID_MAX || $3 == UID_MAX) {
2072				yyerror("user unknown requires operator = or "
2073				    "!=");
2074				YYERROR;
2075			}
2076			$$ = calloc(1, sizeof(struct node_uid));
2077			if ($$ == NULL)
2078				err(1, "uid_item: calloc");
2079			$$->uid[0] = $1;
2080			$$->uid[1] = $3;
2081			$$->op = $2;
2082			$$->next = NULL;
2083			$$->tail = $$;
2084		}
2085		;
2086
2087uid		: STRING			{
2088			u_long	ulval;
2089
2090			if (atoul($1, &ulval) == -1) {
2091				if (!strcmp($1, "unknown"))
2092					$$ = UID_MAX;
2093				else {
2094					struct passwd	*pw;
2095
2096					if ((pw = getpwnam($1)) == NULL) {
2097						yyerror("unknown user %s", $1);
2098						YYERROR;
2099					}
2100					$$ = pw->pw_uid;
2101				}
2102			} else {
2103				if (ulval >= UID_MAX) {
2104					yyerror("illegal uid value %lu", ulval);
2105					YYERROR;
2106				}
2107				$$ = ulval;
2108			}
2109		}
2110		;
2111
2112gids		: gid_item			{ $$ = $1; }
2113		| '{' gid_list '}'		{ $$ = $2; }
2114		;
2115
2116gid_list	: gid_item			{ $$ = $1; }
2117		| gid_list comma gid_item	{
2118			$1->tail->next = $3;
2119			$1->tail = $3;
2120			$$ = $1;
2121		}
2122		;
2123
2124gid_item	: gid				{
2125			$$ = calloc(1, sizeof(struct node_gid));
2126			if ($$ == NULL)
2127				err(1, "gid_item: calloc");
2128			$$->gid[0] = $1;
2129			$$->gid[1] = $1;
2130			$$->op = PF_OP_EQ;
2131			$$->next = NULL;
2132			$$->tail = $$;
2133		}
2134		| unaryop gid			{
2135			if ($2 == GID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
2136				yyerror("group unknown requires operator = or "
2137				    "!=");
2138				YYERROR;
2139			}
2140			$$ = calloc(1, sizeof(struct node_gid));
2141			if ($$ == NULL)
2142				err(1, "gid_item: calloc");
2143			$$->gid[0] = $2;
2144			$$->gid[1] = $2;
2145			$$->op = $1;
2146			$$->next = NULL;
2147			$$->tail = $$;
2148		}
2149		| gid PORTBINARY gid		{
2150			if ($1 == GID_MAX || $3 == GID_MAX) {
2151				yyerror("group unknown requires operator = or "
2152				    "!=");
2153				YYERROR;
2154			}
2155			$$ = calloc(1, sizeof(struct node_gid));
2156			if ($$ == NULL)
2157				err(1, "gid_item: calloc");
2158			$$->gid[0] = $1;
2159			$$->gid[1] = $3;
2160			$$->op = $2;
2161			$$->next = NULL;
2162			$$->tail = $$;
2163		}
2164		;
2165
2166gid		: STRING			{
2167			u_long	ulval;
2168
2169			if (atoul($1, &ulval) == -1) {
2170				if (!strcmp($1, "unknown"))
2171					$$ = GID_MAX;
2172				else {
2173					struct group	*grp;
2174
2175					if ((grp = getgrnam($1)) == NULL) {
2176						yyerror("unknown group %s", $1);
2177						YYERROR;
2178					}
2179					$$ = grp->gr_gid;
2180				}
2181			} else {
2182				if (ulval >= GID_MAX) {
2183					yyerror("illegal gid value %lu", ulval);
2184					YYERROR;
2185				}
2186				$$ = ulval;
2187			}
2188		}
2189		;
2190
2191flag		: STRING			{
2192			int	f;
2193
2194			if ((f = parse_flags($1)) < 0) {
2195				yyerror("bad flags %s", $1);
2196				YYERROR;
2197			}
2198			$$.b1 = f;
2199		}
2200		;
2201
2202flags		: FLAGS flag '/' flag	{ $$.b1 = $2.b1; $$.b2 = $4.b1; }
2203		| FLAGS '/' flag	{ $$.b1 = 0; $$.b2 = $3.b1; }
2204		;
2205
2206icmpspec	: ICMPTYPE icmp_item		{ $$ = $2; }
2207		| ICMPTYPE '{' icmp_list '}'	{ $$ = $3; }
2208		| ICMP6TYPE icmp6_item		{ $$ = $2; }
2209		| ICMP6TYPE '{' icmp6_list '}'	{ $$ = $3; }
2210		;
2211
2212icmp_list	: icmp_item			{ $$ = $1; }
2213		| icmp_list comma icmp_item	{
2214			$1->tail->next = $3;
2215			$1->tail = $3;
2216			$$ = $1;
2217		}
2218		;
2219
2220icmp6_list	: icmp6_item			{ $$ = $1; }
2221		| icmp6_list comma icmp6_item	{
2222			$1->tail->next = $3;
2223			$1->tail = $3;
2224			$$ = $1;
2225		}
2226		;
2227
2228icmp_item	: icmptype		{
2229			$$ = calloc(1, sizeof(struct node_icmp));
2230			if ($$ == NULL)
2231				err(1, "icmp_item: calloc");
2232			$$->type = $1;
2233			$$->code = 0;
2234			$$->proto = IPPROTO_ICMP;
2235			$$->next = NULL;
2236			$$->tail = $$;
2237		}
2238		| icmptype CODE STRING	{
2239			const struct icmpcodeent	*p;
2240			u_long				 ulval;
2241
2242			if (atoul($3, &ulval) == 0) {
2243				if (ulval > 255) {
2244					yyerror("illegal icmp-code %d", ulval);
2245					YYERROR;
2246				}
2247			} else {
2248				if ((p = geticmpcodebyname($1-1, $3,
2249				    AF_INET)) == NULL) {
2250					yyerror("unknown icmp-code %s", $3);
2251					YYERROR;
2252				}
2253				ulval = p->code;
2254			}
2255			$$ = calloc(1, sizeof(struct node_icmp));
2256			if ($$ == NULL)
2257				err(1, "icmp_item: calloc");
2258			$$->type = $1;
2259			$$->code = ulval + 1;
2260			$$->proto = IPPROTO_ICMP;
2261			$$->next = NULL;
2262			$$->tail = $$;
2263		}
2264		;
2265
2266icmp6_item	: icmp6type		{
2267			$$ = calloc(1, sizeof(struct node_icmp));
2268			if ($$ == NULL)
2269				err(1, "icmp_item: calloc");
2270			$$->type = $1;
2271			$$->code = 0;
2272			$$->proto = IPPROTO_ICMPV6;
2273			$$->next = NULL;
2274			$$->tail = $$;
2275		}
2276		| icmp6type CODE STRING	{
2277			const struct icmpcodeent	*p;
2278			u_long				 ulval;
2279
2280			if (atoul($3, &ulval) == 0) {
2281				if (ulval > 255) {
2282					yyerror("illegal icmp6-code %ld",
2283					    ulval);
2284					YYERROR;
2285				}
2286			} else {
2287				if ((p = geticmpcodebyname($1-1, $3,
2288				    AF_INET6)) == NULL) {
2289					yyerror("unknown icmp6-code %s", $3);
2290					YYERROR;
2291				}
2292				ulval = p->code;
2293			}
2294			$$ = calloc(1, sizeof(struct node_icmp));
2295			if ($$ == NULL)
2296				err(1, "icmp_item: calloc");
2297			$$->type = $1;
2298			$$->code = ulval + 1;
2299			$$->proto = IPPROTO_ICMPV6;
2300			$$->next = NULL;
2301			$$->tail = $$;
2302		}
2303		;
2304
2305icmptype	: STRING			{
2306			const struct icmptypeent	*p;
2307			u_long				 ulval;
2308
2309			if (atoul($1, &ulval) == 0) {
2310				if (ulval > 255) {
2311					yyerror("illegal icmp-type %d", ulval);
2312					YYERROR;
2313				}
2314				$$ = ulval + 1;
2315			} else {
2316				if ((p = geticmptypebyname($1, AF_INET)) ==
2317				    NULL) {
2318					yyerror("unknown icmp-type %s", $1);
2319					YYERROR;
2320				}
2321				$$ = p->type + 1;
2322			}
2323		}
2324		;
2325
2326icmp6type	: STRING			{
2327			const struct icmptypeent	*p;
2328			u_long				 ulval;
2329
2330			if (atoul($1, &ulval) == 0) {
2331				if (ulval > 255) {
2332					yyerror("illegal icmp6-type %d", ulval);
2333					YYERROR;
2334				}
2335				$$ = ulval + 1;
2336			} else {
2337				if ((p = geticmptypebyname($1, AF_INET6)) ==
2338				    NULL) {
2339					yyerror("unknown icmp6-type %s", $1);
2340					YYERROR;
2341				}
2342				$$ = p->type + 1;
2343			}
2344		}
2345		;
2346
2347tos		: TOS STRING			{
2348			if (!strcmp($2, "lowdelay"))
2349				$$ = IPTOS_LOWDELAY;
2350			else if (!strcmp($2, "throughput"))
2351				$$ = IPTOS_THROUGHPUT;
2352			else if (!strcmp($2, "reliability"))
2353				$$ = IPTOS_RELIABILITY;
2354			else if ($2[0] == '0' && $2[1] == 'x')
2355				$$ = strtoul($2, NULL, 16);
2356			else
2357				$$ = strtoul($2, NULL, 10);
2358			if (!$$ || $$ > 255) {
2359				yyerror("illegal tos value %s", $2);
2360				YYERROR;
2361			}
2362		}
2363		;
2364
2365keep		: KEEP STATE state_opt_spec	{
2366			$$.action = PF_STATE_NORMAL;
2367			$$.options = $3;
2368		}
2369		| MODULATE STATE state_opt_spec	{
2370			$$.action = PF_STATE_MODULATE;
2371			$$.options = $3;
2372		}
2373		| SYNPROXY STATE state_opt_spec {
2374			$$.action = PF_STATE_SYNPROXY;
2375			$$.options = $3;
2376		}
2377		;
2378
2379state_opt_spec	: '(' state_opt_list ')'	{ $$ = $2; }
2380		| /* empty */			{ $$ = NULL; }
2381		;
2382
2383state_opt_list	: state_opt_item		{ $$ = $1; }
2384		| state_opt_list comma state_opt_item {
2385			$1->tail->next = $3;
2386			$1->tail = $3;
2387			$$ = $1;
2388		}
2389		;
2390
2391state_opt_item	: MAXIMUM number		{
2392			$$ = calloc(1, sizeof(struct node_state_opt));
2393			if ($$ == NULL)
2394				err(1, "state_opt_item: calloc");
2395			$$->type = PF_STATE_OPT_MAX;
2396			$$->data.max_states = $2;
2397			$$->next = NULL;
2398			$$->tail = $$;
2399		}
2400		| STRING number			{
2401			int	i;
2402
2403			for (i = 0; pf_timeouts[i].name &&
2404			    strcmp(pf_timeouts[i].name, $1); ++i)
2405				;	/* nothing */
2406			if (!pf_timeouts[i].name) {
2407				yyerror("illegal timeout name %s", $1);
2408				YYERROR;
2409			}
2410			if (strchr(pf_timeouts[i].name, '.') == NULL) {
2411				yyerror("illegal state timeout %s", $1);
2412				YYERROR;
2413			}
2414			$$ = calloc(1, sizeof(struct node_state_opt));
2415			if ($$ == NULL)
2416				err(1, "state_opt_item: calloc");
2417			$$->type = PF_STATE_OPT_TIMEOUT;
2418			$$->data.timeout.number = pf_timeouts[i].timeout;
2419			$$->data.timeout.seconds = $2;
2420			$$->next = NULL;
2421			$$->tail = $$;
2422		}
2423		;
2424
2425label		: LABEL STRING			{
2426			if (($$ = strdup($2)) == NULL)
2427				err(1, "rule label strdup() failed");
2428		}
2429		;
2430
2431qname		: QUEUE STRING				{
2432			if (($$.qname = strdup($2)) == NULL)
2433				err(1, "qname strdup() failed");
2434		}
2435		| QUEUE '(' STRING ')'			{
2436			if (($$.qname = strdup($3)) == NULL)
2437				err(1, "qname strdup() failed");
2438		}
2439		| QUEUE '(' STRING comma STRING ')'	{
2440			if (($$.qname = strdup($3)) == NULL ||
2441			    ($$.pqname = strdup($5)) == NULL)
2442				err(1, "qname strdup() failed");
2443		}
2444		;
2445
2446no		: /* empty */			{ $$ = 0; }
2447		| NO				{ $$ = 1; }
2448		;
2449
2450rport		: STRING			{
2451			char	*p = strchr($1, ':');
2452
2453			if (p == NULL) {
2454				if (($$.a = getservice($1)) == -1)
2455					YYERROR;
2456				$$.b = $$.t = 0;
2457			} else if (!strcmp(p+1, "*")) {
2458				*p = 0;
2459				if (($$.a = getservice($1)) == -1)
2460					YYERROR;
2461				$$.b = 0;
2462				$$.t = 1;
2463			} else {
2464				*p++ = 0;
2465				if (($$.a = getservice($1)) == -1 ||
2466				    ($$.b = getservice(p)) == -1)
2467					YYERROR;
2468				if ($$.a == $$.b)
2469					$$.b = 0;
2470				$$.t = 0;
2471			}
2472		}
2473		;
2474
2475redirspec	: host				{ $$ = $1; }
2476		| '{' redir_host_list '}'	{ $$ = $2; }
2477		;
2478
2479redir_host_list	: host				{ $$ = $1; }
2480		| redir_host_list comma host	{
2481			$1->tail->next = $3;
2482			$1->tail = $3->tail;
2483			$$ = $1;
2484		}
2485		;
2486
2487redirpool	: /* empty */			{ $$ = NULL; }
2488		| ARROW redirspec		{
2489			$$ = calloc(1, sizeof(struct redirection));
2490			if ($$ == NULL)
2491				err(1, "redirection: calloc");
2492			$$->host = $2;
2493			$$->rport.a = $$->rport.b = $$->rport.t = 0;
2494		}
2495		| ARROW redirspec PORT rport	{
2496			$$ = calloc(1, sizeof(struct redirection));
2497			if ($$ == NULL)
2498				err(1, "redirection: calloc");
2499			$$->host = $2;
2500			$$->rport = $4;
2501		}
2502		;
2503
2504hashkey		: /* empty */
2505		{
2506			$$ = calloc(1, sizeof(struct pf_poolhashkey));
2507			if ($$ == NULL)
2508				err(1, "hashkey: calloc");
2509			$$->key32[0] = arc4random();
2510			$$->key32[1] = arc4random();
2511			$$->key32[2] = arc4random();
2512			$$->key32[3] = arc4random();
2513		}
2514		| string
2515		{
2516			if (!strncmp($1, "0x", 2)) {
2517				if (strlen($1) != 34) {
2518					yyerror("hex key must be 128 bits "
2519						"(32 hex digits) long");
2520					YYERROR;
2521				}
2522				$$ = calloc(1, sizeof(struct pf_poolhashkey));
2523				if ($$ == NULL)
2524					err(1, "hashkey: calloc");
2525
2526				if (sscanf($1, "0x%8x%8x%8x%8x",
2527				    &$$->key32[0], &$$->key32[1],
2528				    &$$->key32[2], &$$->key32[3]) != 4) {
2529					free($$);
2530					yyerror("invalid hex key");
2531					YYERROR;
2532				}
2533			} else {
2534				MD5_CTX	context;
2535
2536				$$ = calloc(1, sizeof(struct pf_poolhashkey));
2537				if ($$ == NULL)
2538					err(1, "hashkey: calloc");
2539				MD5Init(&context);
2540				MD5Update(&context, (unsigned char *)$1,
2541				    strlen($1));
2542				MD5Final((unsigned char *)$$, &context);
2543				HTONL($$->key32[0]);
2544				HTONL($$->key32[1]);
2545				HTONL($$->key32[2]);
2546				HTONL($$->key32[3]);
2547			}
2548		}
2549		;
2550
2551pooltype	: /* empty */
2552		{
2553			$$.type = PF_POOL_NONE;
2554			$$.key = NULL;
2555		}
2556		| BITMASK
2557		{
2558			$$.type = PF_POOL_BITMASK;
2559			$$.key = NULL;
2560		}
2561		| RANDOM
2562		{
2563			$$.type = PF_POOL_RANDOM;
2564			$$.key = NULL;
2565		}
2566		| SOURCEHASH hashkey
2567		{
2568			$$.type = PF_POOL_SRCHASH;
2569			$$.key = $2;
2570		}
2571		| ROUNDROBIN
2572		{
2573			$$.type = PF_POOL_ROUNDROBIN;
2574			$$.key = NULL;
2575		}
2576		;
2577
2578staticport	: /* empty */			{ $$ = 0; }
2579		| STATICPORT			{ $$ = 1; }
2580		;
2581
2582redirection	: /* empty */			{ $$ = NULL; }
2583		| ARROW host			{
2584			$$ = calloc(1, sizeof(struct redirection));
2585			if ($$ == NULL)
2586				err(1, "redirection: calloc");
2587			$$->host = $2;
2588			$$->rport.a = $$->rport.b = $$->rport.t = 0;
2589		}
2590		| ARROW host PORT rport	{
2591			$$ = calloc(1, sizeof(struct redirection));
2592			if ($$ == NULL)
2593				err(1, "redirection: calloc");
2594			$$->host = $2;
2595			$$->rport = $4;
2596		}
2597		;
2598
2599natpass		: /* empty */	{ $$ = 0; }
2600		| PASS		{ $$ = 1; }
2601		;
2602
2603nataction	: no NAT natpass {
2604			$$.b2 = $$.w = 0;
2605			if ($1)
2606				$$.b1 = PF_NONAT;
2607			else
2608				$$.b1 = PF_NAT;
2609			$$.b2 = $3;
2610		}
2611		| no RDR natpass {
2612			$$.b2 = $$.w = 0;
2613			if ($1)
2614				$$.b1 = PF_NORDR;
2615			else
2616				$$.b1 = PF_RDR;
2617			$$.b2 = $3;
2618		}
2619		;
2620
2621natrule		: nataction interface af proto fromto tag redirpool pooltype
2622		  staticport
2623		{
2624			struct pf_rule	r;
2625
2626			if (check_rulestate(PFCTL_STATE_NAT))
2627				YYERROR;
2628
2629			memset(&r, 0, sizeof(r));
2630
2631			r.action = $1.b1;
2632			r.natpass = $1.b2;
2633			r.af = $3;
2634
2635			if (!r.af) {
2636				if ($5.src.host && $5.src.host->af &&
2637				    !$5.src.host->ifindex)
2638					r.af = $5.src.host->af;
2639				else if ($5.dst.host && $5.dst.host->af &&
2640				    !$5.dst.host->ifindex)
2641					r.af = $5.dst.host->af;
2642			}
2643
2644			if ($6 != NULL)
2645				if (strlcpy(r.tagname, $6, PF_TAG_NAME_SIZE) >
2646				    PF_TAG_NAME_SIZE) {
2647					yyerror("tag too long, max %u chars",
2648					    PF_TAG_NAME_SIZE - 1);
2649					YYERROR;
2650				}
2651
2652			if (r.action == PF_NONAT || r.action == PF_NORDR) {
2653				if ($7 != NULL) {
2654					yyerror("translation rule with 'no' "
2655					    "does not need '->'");
2656					YYERROR;
2657				}
2658			} else {
2659				if ($7 == NULL || $7->host == NULL) {
2660					yyerror("translation rule requires '-> "
2661					    "address'");
2662					YYERROR;
2663				}
2664				if (!r.af && ! $7->host->ifindex)
2665					r.af = $7->host->af;
2666
2667				remove_invalid_hosts(&$7->host, &r.af);
2668				if (invalid_redirect($7->host, r.af))
2669					YYERROR;
2670				if (check_netmask($7->host, r.af))
2671					YYERROR;
2672
2673				r.rpool.proxy_port[0] = ntohs($7->rport.a);
2674
2675				switch (r.action) {
2676				case PF_RDR:
2677					if (!$7->rport.b && $7->rport.t &&
2678					    $5.dst.port != NULL) {
2679						r.rpool.proxy_port[1] =
2680						    ntohs($7->rport.a) +
2681						    (ntohs($5.dst.port->port[1]) -
2682						    ntohs($5.dst.port->port[0]));
2683					} else
2684						r.rpool.proxy_port[1] =
2685						    ntohs($7->rport.b);
2686					break;
2687				case PF_NAT:
2688					r.rpool.proxy_port[1] = ntohs($7->rport.b);
2689					if (!r.rpool.proxy_port[0] &&
2690					    !r.rpool.proxy_port[1]) {
2691						r.rpool.proxy_port[0] =
2692						    PF_NAT_PROXY_PORT_LOW;
2693						r.rpool.proxy_port[1] =
2694						    PF_NAT_PROXY_PORT_HIGH;
2695					} else if (!r.rpool.proxy_port[1])
2696						r.rpool.proxy_port[1] =
2697						    r.rpool.proxy_port[0];
2698					break;
2699				default:
2700					break;
2701				}
2702
2703				r.rpool.opts = $8.type;
2704				if (r.rpool.opts == PF_POOL_NONE)
2705					r.rpool.opts = PF_POOL_ROUNDROBIN;
2706				if (r.rpool.opts != PF_POOL_ROUNDROBIN)
2707					if (disallow_table($7->host, "tables "
2708					    "are only supported in round-robin "
2709					    "redirection pools"))
2710						YYERROR;
2711				if ($7->host->next) {
2712					if (r.rpool.opts !=
2713					    PF_POOL_ROUNDROBIN) {
2714						yyerror("only round-robin "
2715						    "valid for multiple "
2716						    "redirection addresses");
2717						YYERROR;
2718					}
2719				} else {
2720					if ((r.af == AF_INET &&
2721					    unmask(&$7->host->addr.v.a.mask,
2722					    r.af) == 32) ||
2723					    (r.af == AF_INET6 &&
2724					    unmask(&$7->host->addr.v.a.mask,
2725					    r.af) == 128)) {
2726						r.rpool.opts = PF_POOL_NONE;
2727					}
2728				}
2729			}
2730
2731			if ($8.key != NULL)
2732				memcpy(&r.rpool.key, $8.key,
2733				    sizeof(struct pf_poolhashkey));
2734
2735			if ($9 != NULL) {
2736				if (r.action != PF_NAT) {
2737					yyerror("the 'static-port' option is "
2738					    "only valid with nat rules");
2739					YYERROR;
2740				}
2741				if (r.rpool.proxy_port[0] !=
2742				    PF_NAT_PROXY_PORT_LOW &&
2743				    r.rpool.proxy_port[1] !=
2744				    PF_NAT_PROXY_PORT_HIGH) {
2745					yyerror("the 'static-port' option can't"
2746					    " be used when specifying a port"
2747					    " range");
2748					YYERROR;
2749				}
2750				r.rpool.proxy_port[0] = 0;
2751				r.rpool.proxy_port[1] = 0;
2752			}
2753
2754			expand_rule(&r, $2, $7 == NULL ? NULL : $7->host, $4,
2755			    $5.src_os, $5.src.host, $5.src.port, $5.dst.host,
2756			    $5.dst.port, 0, 0, 0);
2757			free($7);
2758		}
2759		;
2760
2761binatrule	: no BINAT natpass interface af proto FROM host TO ipspec tag
2762		  redirection
2763		{
2764			struct pf_rule		binat;
2765			struct pf_pooladdr	*pa;
2766
2767			if (check_rulestate(PFCTL_STATE_NAT))
2768				YYERROR;
2769
2770			memset(&binat, 0, sizeof(binat));
2771
2772			if ($1)
2773				binat.action = PF_NOBINAT;
2774			else
2775				binat.action = PF_BINAT;
2776			binat.natpass = $3;
2777			binat.af = $5;
2778			if (!binat.af && $8 != NULL && $8->af)
2779				binat.af = $8->af;
2780			if (!binat.af && $10 != NULL && $10->af)
2781				binat.af = $10->af;
2782			if (!binat.af && $12 != NULL && $12->host)
2783				binat.af = $12->host->af;
2784			if (!binat.af) {
2785				yyerror("address family (inet/inet6) "
2786				    "undefined");
2787				YYERROR;
2788			}
2789
2790			if ($4 != NULL) {
2791				memcpy(binat.ifname, $4->ifname,
2792				    sizeof(binat.ifname));
2793				free($4);
2794			}
2795			if ($11 != NULL)
2796				if (strlcpy(binat.tagname, $11,
2797				    PF_TAG_NAME_SIZE) > PF_TAG_NAME_SIZE) {
2798					yyerror("tag too long, max %u chars",
2799					    PF_TAG_NAME_SIZE - 1);
2800					YYERROR;
2801				}
2802
2803			if ($6 != NULL) {
2804				binat.proto = $6->proto;
2805				free($6);
2806			}
2807
2808			if ($8 != NULL && disallow_table($8, "invalid use of "
2809			    "table <%s> as the source address of a binat rule"))
2810				YYERROR;
2811			if ($12 != NULL && $12->host != NULL && disallow_table(
2812			    $12->host, "invalid use of table <%s> as the "
2813			    "redirect address of a binat rule"))
2814				YYERROR;
2815
2816			if ($8 != NULL) {
2817				if ($8->next) {
2818					yyerror("multiple binat ip addresses");
2819					YYERROR;
2820				}
2821				if ($8->addr.type == PF_ADDR_DYNIFTL)
2822					$8->af = binat.af;
2823				if ($8->af != binat.af) {
2824					yyerror("binat ip versions must match");
2825					YYERROR;
2826				}
2827				if (check_netmask($8, binat.af))
2828					YYERROR;
2829				memcpy(&binat.src.addr, &$8->addr,
2830				    sizeof(binat.src.addr));
2831				free($8);
2832			}
2833			if ($10 != NULL) {
2834				if ($10->next) {
2835					yyerror("multiple binat ip addresses");
2836					YYERROR;
2837				}
2838				if ($10->af != binat.af && $10->af) {
2839					yyerror("binat ip versions must match");
2840					YYERROR;
2841				}
2842				if (check_netmask($10, binat.af))
2843					YYERROR;
2844				memcpy(&binat.dst.addr, &$10->addr,
2845				    sizeof(binat.dst.addr));
2846				binat.dst.not = $10->not;
2847				free($10);
2848			}
2849
2850			if (binat.action == PF_NOBINAT) {
2851				if ($12 != NULL) {
2852					yyerror("'no binat' rule does not need"
2853					    " '->'");
2854					YYERROR;
2855				}
2856			} else {
2857				if ($12 == NULL || $12->host == NULL) {
2858					yyerror("'binat' rule requires"
2859					    " '-> address'");
2860					YYERROR;
2861				}
2862
2863				remove_invalid_hosts(&$12->host, &binat.af);
2864				if (invalid_redirect($12->host, binat.af))
2865					YYERROR;
2866				if ($12->host->next != NULL) {
2867					yyerror("binat rule must redirect to "
2868					    "a single address");
2869					YYERROR;
2870				}
2871				if (check_netmask($12->host, binat.af))
2872					YYERROR;
2873
2874				if (!PF_AZERO(&binat.src.addr.v.a.mask,
2875				    binat.af) &&
2876				    !PF_AEQ(&binat.src.addr.v.a.mask,
2877				    &$12->host->addr.v.a.mask, binat.af)) {
2878					yyerror("'binat' source mask and "
2879					    "redirect mask must be the same");
2880					YYERROR;
2881				}
2882
2883				TAILQ_INIT(&binat.rpool.list);
2884				pa = calloc(1, sizeof(struct pf_pooladdr));
2885				if (pa == NULL)
2886					err(1, "binat: calloc");
2887				pa->addr = $12->host->addr;
2888				pa->ifname[0] = 0;
2889				TAILQ_INSERT_TAIL(&binat.rpool.list,
2890				    pa, entries);
2891
2892				free($12);
2893			}
2894
2895			pfctl_add_rule(pf, &binat);
2896		}
2897		;
2898
2899tag		: /* empty */		{ $$ = NULL; }
2900		| TAG STRING		{ $$ = $2; }
2901
2902route_host	: STRING			{
2903			struct node_host	*n;
2904
2905			$$ = calloc(1, sizeof(struct node_host));
2906			if ($$ == NULL)
2907				err(1, "route_host: calloc");
2908			if (($$->ifname = strdup($1)) == NULL)
2909				err(1, "routeto: strdup");
2910			if ((n = ifa_exists($$->ifname)) == NULL) {
2911				yyerror("routeto: unknown interface %s",
2912				    $$->ifname);
2913				YYERROR;
2914			}
2915			set_ipmask($$, 128);
2916			$$->next = NULL;
2917			$$->tail = $$;
2918		}
2919		| '(' STRING host ')'		{
2920			struct node_host	*n;
2921
2922			$$ = $3;
2923			if (($$->ifname = strdup($2)) == NULL)
2924				err(1, "routeto: strdup");
2925			if ((n = ifa_exists($$->ifname)) == NULL) {
2926				yyerror("routeto: unknown interface %s",
2927				    $$->ifname);
2928				YYERROR;
2929			}
2930		}
2931		;
2932
2933route_host_list	: route_host				{ $$ = $1; }
2934		| route_host_list comma route_host	{
2935			if ($1->af == 0)
2936				$1->af = $3->af;
2937			if ($1->af != $3->af) {
2938				yyerror("all pool addresses must be in the "
2939				    "same address family");
2940				YYERROR;
2941			}
2942			$1->tail->next = $3;
2943			$1->tail = $3->tail;
2944			$$ = $1;
2945		}
2946		;
2947
2948routespec	: route_host			{ $$ = $1; }
2949		| '{' route_host_list '}'	{ $$ = $2; }
2950		;
2951
2952route		: /* empty */			{
2953			$$.host = NULL;
2954			$$.rt = 0;
2955			$$.pool_opts = 0;
2956		}
2957		| FASTROUTE {
2958			$$.host = NULL;
2959			$$.rt = PF_FASTROUTE;
2960			$$.pool_opts = 0;
2961		}
2962		| ROUTETO routespec pooltype {
2963			$$.host = $2;
2964			$$.rt = PF_ROUTETO;
2965			$$.pool_opts = $3.type;
2966			if ($3.key != NULL)
2967				$$.key = $3.key;
2968		}
2969		| REPLYTO routespec pooltype {
2970			$$.host = $2;
2971			$$.rt = PF_REPLYTO;
2972			$$.pool_opts = $3.type;
2973			if ($3.key != NULL)
2974				$$.key = $3.key;
2975		}
2976		| DUPTO routespec pooltype {
2977			$$.host = $2;
2978			$$.rt = PF_DUPTO;
2979			$$.pool_opts = $3.type;
2980			if ($3.key != NULL)
2981				$$.key = $3.key;
2982		}
2983		;
2984
2985timeout_spec	: STRING number
2986		{
2987			if (check_rulestate(PFCTL_STATE_OPTION))
2988				YYERROR;
2989			if (pfctl_set_timeout(pf, $1, $2, 0) != 0) {
2990				yyerror("unknown timeout %s", $1);
2991				YYERROR;
2992			}
2993		}
2994		;
2995
2996timeout_list	: timeout_list comma timeout_spec
2997		| timeout_spec
2998		;
2999
3000limit_spec	: STRING number
3001		{
3002			if (check_rulestate(PFCTL_STATE_OPTION))
3003				YYERROR;
3004			if (pfctl_set_limit(pf, $1, $2) != 0) {
3005				yyerror("unable to set limit %s %u", $1, $2);
3006				YYERROR;
3007			}
3008		}
3009
3010limit_list	: limit_list comma limit_spec
3011		| limit_spec
3012		;
3013
3014comma		: ','
3015		| /* empty */
3016		;
3017
3018yesno		: NO			{ $$ = 0; }
3019		| STRING		{
3020			if (!strcmp($1, "yes"))
3021				$$ = 1;
3022			else
3023				YYERROR;
3024		}
3025
3026unaryop		: '='		{ $$ = PF_OP_EQ; }
3027		| '!' '='	{ $$ = PF_OP_NE; }
3028		| '<' '='	{ $$ = PF_OP_LE; }
3029		| '<'		{ $$ = PF_OP_LT; }
3030		| '>' '='	{ $$ = PF_OP_GE; }
3031		| '>'		{ $$ = PF_OP_GT; }
3032		;
3033
3034%%
3035
3036int
3037yyerror(const char *fmt, ...)
3038{
3039	va_list		 ap;
3040	extern char	*infile;
3041
3042	errors = 1;
3043	va_start(ap, fmt);
3044	fprintf(stderr, "%s:%d: ", infile, yylval.lineno);
3045	vfprintf(stderr, fmt, ap);
3046	fprintf(stderr, "\n");
3047	va_end(ap);
3048	return (0);
3049}
3050
3051int
3052disallow_table(struct node_host *h, const char *fmt)
3053{
3054	for (; h != NULL; h = h->next)
3055		if (h->addr.type == PF_ADDR_TABLE) {
3056			yyerror(fmt, h->addr.v.tblname);
3057			return (1);
3058		}
3059	return (0);
3060}
3061
3062int
3063rule_consistent(struct pf_rule *r)
3064{
3065	int	problems = 0;
3066
3067	switch (r->action) {
3068	case PF_PASS:
3069	case PF_DROP:
3070	case PF_SCRUB:
3071		problems = filter_consistent(r);
3072		break;
3073	case PF_NAT:
3074	case PF_NONAT:
3075		problems = nat_consistent(r);
3076		break;
3077	case PF_RDR:
3078	case PF_NORDR:
3079		problems = rdr_consistent(r);
3080		break;
3081	case PF_BINAT:
3082	case PF_NOBINAT:
3083	default:
3084		break;
3085	}
3086	return (problems);
3087}
3088
3089int
3090filter_consistent(struct pf_rule *r)
3091{
3092	int	problems = 0;
3093
3094	if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP &&
3095	    (r->src.port_op || r->dst.port_op)) {
3096		yyerror("port only applies to tcp/udp");
3097		problems++;
3098	}
3099	if (r->src.port_op == PF_OP_RRG || r->dst.port_op == PF_OP_RRG) {
3100		yyerror("the ':' port operator only applies to rdr");
3101		problems++;
3102	}
3103	if (r->proto != IPPROTO_ICMP && r->proto != IPPROTO_ICMPV6 &&
3104	    (r->type || r->code)) {
3105		yyerror("icmp-type/code only applies to icmp");
3106		problems++;
3107	}
3108	if (!r->af && (r->type || r->code)) {
3109		yyerror("must indicate address family with icmp-type/code");
3110		problems++;
3111	}
3112	if ((r->proto == IPPROTO_ICMP && r->af == AF_INET6) ||
3113	    (r->proto == IPPROTO_ICMPV6 && r->af == AF_INET)) {
3114		yyerror("proto %s doesn't match address family %s",
3115		    r->proto == IPPROTO_ICMP ? "icmp" : "icmp6",
3116		    r->af == AF_INET ? "inet" : "inet6");
3117		problems++;
3118	}
3119	if ((r->keep_state == PF_STATE_MODULATE || r->keep_state ==
3120	    PF_STATE_SYNPROXY) && r->proto && r->proto != IPPROTO_TCP) {
3121		yyerror("modulate/synproxy state can only be applied to "
3122		    "TCP rules");
3123		problems++;
3124	}
3125	if (r->allow_opts && r->action != PF_PASS) {
3126		yyerror("allow-opts can only be specified for pass rules");
3127		problems++;
3128	}
3129	if (!r->af && (r->src.addr.type == PF_ADDR_DYNIFTL ||
3130	    r->dst.addr.type == PF_ADDR_DYNIFTL)) {
3131		yyerror("dynamic addresses require address family "
3132		    "(inet/inet6)");
3133		problems++;
3134	}
3135	if (r->rule_flag & PFRULE_FRAGMENT && (r->src.port_op ||
3136	    r->dst.port_op || r->flagset || r->type || r->code)) {
3137		yyerror("fragments can be filtered only on IP header fields");
3138		problems++;
3139	}
3140	if (r->rule_flag & PFRULE_RETURNRST && r->proto != IPPROTO_TCP) {
3141		yyerror("return-rst can only be applied to TCP rules");
3142		problems++;
3143	}
3144	if (r->action == PF_DROP && r->keep_state) {
3145		yyerror("keep state on block rules doesn't make sense");
3146		problems++;
3147	}
3148	if ((r->tagname[0] || r->match_tagname[0]) && !r->keep_state &&
3149	    r->action == PF_PASS) {
3150		yyerror("tags cannot be used without keep state");
3151		problems++;
3152	}
3153	return (-problems);
3154}
3155
3156int
3157nat_consistent(struct pf_rule *r)
3158{
3159	int			 problems = 0;
3160	struct pf_pooladdr	*pa;
3161
3162	if (r->src.port_op == PF_OP_RRG || r->dst.port_op == PF_OP_RRG) {
3163		yyerror("the ':' port operator only applies to rdr");
3164		problems++;
3165	}
3166	if (!r->af) {
3167		TAILQ_FOREACH(pa, &r->rpool.list, entries) {
3168			if (pa->addr.type == PF_ADDR_DYNIFTL) {
3169				yyerror("dynamic addresses require "
3170				    "address family (inet/inet6)");
3171				problems++;
3172				break;
3173			}
3174		}
3175	}
3176	return (-problems);
3177}
3178
3179int
3180rdr_consistent(struct pf_rule *r)
3181{
3182	int			 problems = 0;
3183	struct pf_pooladdr	*pa;
3184
3185	if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP) {
3186		if (r->src.port_op) {
3187			yyerror("src port only applies to tcp/udp");
3188			problems++;
3189		}
3190		if (r->dst.port_op) {
3191			yyerror("dst port only applies to tcp/udp");
3192			problems++;
3193		}
3194		if (r->rpool.proxy_port[0]) {
3195			yyerror("rpool port only applies to tcp/udp");
3196			problems++;
3197		}
3198	}
3199	if (r->dst.port_op &&
3200	    r->dst.port_op != PF_OP_EQ && r->dst.port_op != PF_OP_RRG) {
3201		yyerror("invalid port operator for rdr destination port");
3202		problems++;
3203	}
3204	if (r->src.port_op == PF_OP_RRG) {
3205		yyerror("the ':' port operator only applies to rdr "
3206		    "destination port");
3207		problems++;
3208	}
3209	if (!r->af) {
3210		if (r->src.addr.type == PF_ADDR_DYNIFTL ||
3211		    r->dst.addr.type == PF_ADDR_DYNIFTL) {
3212			yyerror("dynamic addresses require address family "
3213			    "(inet/inet6)");
3214			problems++;
3215		} else {
3216			TAILQ_FOREACH(pa, &r->rpool.list, entries) {
3217				if (pa->addr.type == PF_ADDR_DYNIFTL) {
3218					yyerror("dynamic addresses require "
3219					    "address family (inet/inet6)");
3220					problems++;
3221					break;
3222				}
3223			}
3224		}
3225	}
3226	return (-problems);
3227}
3228
3229int
3230process_tabledef(char *name, struct table_opts *opts)
3231{
3232	struct pfr_buffer	 ab;
3233	struct node_tinit	*ti;
3234
3235	bzero(&ab, sizeof(ab));
3236	ab.pfrb_type = PFRB_ADDRS;
3237	SIMPLEQ_FOREACH(ti, &opts->init_nodes, entries) {
3238		if (ti->file)
3239			if (pfr_buf_load(&ab, ti->file, 0, append_addr)) {
3240				if (errno)
3241					yyerror("cannot load \"%s\": %s",
3242					    ti->file, strerror(errno));
3243				else
3244					yyerror("file \"%s\" contains bad data",
3245					    ti->file);
3246				goto _error;
3247			}
3248		if (ti->host)
3249			if (append_addr_host(&ab, ti->host, 0, 0)) {
3250				yyerror("cannot create address buffer: %s",
3251				    strerror(errno));
3252				goto _error;
3253			}
3254	}
3255	if (pf->opts & PF_OPT_VERBOSE)
3256		print_tabledef(name, opts->flags, opts->init_addr,
3257		    &opts->init_nodes);
3258	if (!(pf->opts & PF_OPT_NOACTION) &&
3259	    pfctl_define_table(name, opts->flags, opts->init_addr,
3260	    pf->anchor, pf->ruleset, &ab, pf->tticket)) {
3261		yyerror("cannot define table %s: %s", name,
3262		    pfr_strerror(errno));
3263		goto _error;
3264	}
3265	pf->tdirty = 1;
3266	pfr_buf_clear(&ab);
3267	return (0);
3268_error:
3269	pfr_buf_clear(&ab);
3270	return (-1);
3271}
3272
3273struct keywords {
3274	const char	*k_name;
3275	int		 k_val;
3276};
3277
3278/* macro gore, but you should've seen the prior indentation nightmare... */
3279
3280#define FREE_LIST(T,r) \
3281	do { \
3282		T *p, *node = r; \
3283		while (node != NULL) { \
3284			p = node; \
3285			node = node->next; \
3286			free(p); \
3287		} \
3288	} while (0)
3289
3290#define LOOP_THROUGH(T,n,r,C) \
3291	do { \
3292		T *n; \
3293		if (r == NULL) { \
3294			r = calloc(1, sizeof(T)); \
3295			if (r == NULL) \
3296				err(1, "LOOP: calloc"); \
3297			r->next = NULL; \
3298		} \
3299		n = r; \
3300		while (n != NULL) { \
3301			do { \
3302				C; \
3303			} while (0); \
3304			n = n->next; \
3305		} \
3306	} while (0)
3307
3308void
3309expand_label_str(char *label, const char *srch, const char *repl)
3310{
3311	char tmp[PF_RULE_LABEL_SIZE] = "";
3312	char *p, *q;
3313
3314	p = q = label;
3315	while ((q = strstr(p, srch)) != NULL) {
3316		*q = '\0';
3317		if ((strlcat(tmp, p, sizeof(tmp)) >= sizeof(tmp)) ||
3318		    (strlcat(tmp, repl, sizeof(tmp)) >= sizeof(tmp)))
3319			err(1, "expand_label: label too long");
3320		q += strlen(srch);
3321		p = q;
3322	}
3323	if (strlcat(tmp, p, sizeof(tmp)) >= sizeof(tmp))
3324		err(1, "expand_label: label too long");
3325	strlcpy(label, tmp, PF_RULE_LABEL_SIZE);	/* always fits */
3326}
3327
3328void
3329expand_label_if(const char *name, char *label, const char *ifname)
3330{
3331	if (strstr(label, name) != NULL) {
3332		if (!*ifname)
3333			expand_label_str(label, name, "any");
3334		else
3335			expand_label_str(label, name, ifname);
3336	}
3337}
3338
3339void
3340expand_label_addr(const char *name, char *label, sa_family_t af,
3341    struct node_host *h)
3342{
3343	char tmp[64], tmp_not[66];
3344
3345	if (strstr(label, name) != NULL) {
3346		switch (h->addr.type) {
3347		case PF_ADDR_DYNIFTL:
3348			snprintf(tmp, sizeof(tmp), "(%s)", h->addr.v.ifname);
3349			break;
3350		case PF_ADDR_TABLE:
3351			snprintf(tmp, sizeof(tmp), "<%s>", h->addr.v.tblname);
3352			break;
3353		case PF_ADDR_NOROUTE:
3354			snprintf(tmp, sizeof(tmp), "no-route");
3355			break;
3356		case PF_ADDR_ADDRMASK:
3357			if (!af || (PF_AZERO(&h->addr.v.a.addr, af) &&
3358			    PF_AZERO(&h->addr.v.a.mask, af)))
3359				snprintf(tmp, sizeof(tmp), "any");
3360			else {
3361				char	a[48];
3362				int	bits;
3363
3364				if (inet_ntop(af, &h->addr.v.a.addr, a,
3365				    sizeof(a)) == NULL)
3366					snprintf(tmp, sizeof(tmp), "?");
3367				else {
3368					bits = unmask(&h->addr.v.a.mask, af);
3369					if ((af == AF_INET && bits < 32) ||
3370					    (af == AF_INET6 && bits < 128))
3371						snprintf(tmp, sizeof(tmp),
3372						   "%s/%d", a, bits);
3373					else
3374						snprintf(tmp, sizeof(tmp),
3375						    "%s", a);
3376				}
3377			}
3378			break;
3379		default:
3380			snprintf(tmp, sizeof(tmp), "?");
3381			break;
3382		}
3383
3384		if (h->not) {
3385			snprintf(tmp_not, sizeof(tmp_not), "! %s", tmp);
3386			expand_label_str(label, name, tmp_not);
3387		} else
3388			expand_label_str(label, name, tmp);
3389	}
3390}
3391
3392void
3393expand_label_port(const char *name, char *label, struct node_port *port)
3394{
3395	char	 a1[6], a2[6], op[13] = "";
3396
3397	if (strstr(label, name) != NULL) {
3398		snprintf(a1, sizeof(a1), "%u", ntohs(port->port[0]));
3399		snprintf(a2, sizeof(a2), "%u", ntohs(port->port[1]));
3400		if (!port->op)
3401			;
3402		else if (port->op == PF_OP_IRG)
3403			snprintf(op, sizeof(op), "%s><%s", a1, a2);
3404		else if (port->op == PF_OP_XRG)
3405			snprintf(op, sizeof(op), "%s<>%s", a1, a2);
3406		else if (port->op == PF_OP_EQ)
3407			snprintf(op, sizeof(op), "%s", a1);
3408		else if (port->op == PF_OP_NE)
3409			snprintf(op, sizeof(op), "!=%s", a1);
3410		else if (port->op == PF_OP_LT)
3411			snprintf(op, sizeof(op), "<%s", a1);
3412		else if (port->op == PF_OP_LE)
3413			snprintf(op, sizeof(op), "<=%s", a1);
3414		else if (port->op == PF_OP_GT)
3415			snprintf(op, sizeof(op), ">%s", a1);
3416		else if (port->op == PF_OP_GE)
3417			snprintf(op, sizeof(op), ">=%s", a1);
3418		expand_label_str(label, name, op);
3419	}
3420}
3421
3422void
3423expand_label_proto(const char *name, char *label, u_int8_t proto)
3424{
3425	struct protoent *pe;
3426	char n[4];
3427
3428	if (strstr(label, name) != NULL) {
3429		pe = getprotobynumber(proto);
3430		if (pe != NULL)
3431			expand_label_str(label, name, pe->p_name);
3432		else {
3433			snprintf(n, sizeof(n), "%u", proto);
3434			expand_label_str(label, name, n);
3435		}
3436	}
3437}
3438
3439void
3440expand_label_nr(const char *name, char *label)
3441{
3442	char n[11];
3443
3444	if (strstr(label, name) != NULL) {
3445		snprintf(n, sizeof(n), "%u", pf->rule_nr);
3446		expand_label_str(label, name, n);
3447	}
3448}
3449
3450void
3451expand_label(char *label, const char *ifname, sa_family_t af,
3452    struct node_host *src_host, struct node_port *src_port,
3453    struct node_host *dst_host, struct node_port *dst_port,
3454    u_int8_t proto)
3455{
3456	expand_label_if("$if", label, ifname);
3457	expand_label_addr("$srcaddr", label, af, src_host);
3458	expand_label_addr("$dstaddr", label, af, dst_host);
3459	expand_label_port("$srcport", label, src_port);
3460	expand_label_port("$dstport", label, dst_port);
3461	expand_label_proto("$proto", label, proto);
3462	expand_label_nr("$nr", label);
3463}
3464
3465int
3466expand_altq(struct pf_altq *a, struct node_if *interfaces,
3467    struct node_queue *nqueues, struct node_queue_bw bwspec,
3468    struct node_queue_opt *opts)
3469{
3470	struct pf_altq		 pa, pb;
3471	char			 qname[PF_QNAME_SIZE];
3472	struct node_queue	*n;
3473	struct node_queue_bw	 bw;
3474	int			 errs = 0;
3475
3476	if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
3477		FREE_LIST(struct node_if, interfaces);
3478		FREE_LIST(struct node_queue, nqueues);
3479		return (0);
3480	}
3481
3482	LOOP_THROUGH(struct node_if, interface, interfaces,
3483		memcpy(&pa, a, sizeof(struct pf_altq));
3484		if (strlcpy(pa.ifname, interface->ifname,
3485		    sizeof(pa.ifname)) >= sizeof(pa.ifname))
3486			errx(1, "expand_altq: strlcpy");
3487
3488		if (interface->not) {
3489			yyerror("altq on ! <interface> is not supported");
3490			errs++;
3491		} else {
3492			if (eval_pfaltq(pf, &pa, &bwspec, opts))
3493				errs++;
3494			else
3495				if (pfctl_add_altq(pf, &pa))
3496					errs++;
3497
3498			if (pf->opts & PF_OPT_VERBOSE) {
3499				print_altq(&pf->paltq->altq, 0,
3500				    &bwspec, opts);
3501				if (nqueues && nqueues->tail) {
3502					printf("queue { ");
3503					LOOP_THROUGH(struct node_queue, queue,
3504					    nqueues,
3505						printf("%s ",
3506						    queue->queue);
3507					);
3508					printf("}");
3509				}
3510				printf("\n");
3511			}
3512
3513			if (pa.scheduler == ALTQT_CBQ ||
3514			    pa.scheduler == ALTQT_HFSC) {
3515				/* now create a root queue */
3516				memset(&pb, 0, sizeof(struct pf_altq));
3517				if (strlcpy(qname, "root_", sizeof(qname)) >=
3518				    sizeof(qname))
3519					errx(1, "expand_altq: strlcpy");
3520				if (strlcat(qname, interface->ifname,
3521				    sizeof(qname)) >= sizeof(qname))
3522					errx(1, "expand_altq: strlcat");
3523				if (strlcpy(pb.qname, qname,
3524				    sizeof(pb.qname)) >= sizeof(pb.qname))
3525					errx(1, "expand_altq: strlcpy");
3526				if (strlcpy(pb.ifname, interface->ifname,
3527				    sizeof(pb.ifname)) >= sizeof(pb.ifname))
3528					errx(1, "expand_altq: strlcpy");
3529				pb.qlimit = pa.qlimit;
3530				pb.scheduler = pa.scheduler;
3531				bw.bw_absolute = pa.ifbandwidth;
3532				bw.bw_percent = 0;
3533				if (eval_pfqueue(pf, &pb, &bw, opts))
3534					errs++;
3535				else
3536					if (pfctl_add_altq(pf, &pb))
3537						errs++;
3538			}
3539
3540			LOOP_THROUGH(struct node_queue, queue, nqueues,
3541				n = calloc(1, sizeof(struct node_queue));
3542				if (n == NULL)
3543					err(1, "expand_altq: calloc");
3544				if (pa.scheduler == ALTQT_CBQ ||
3545				    pa.scheduler == ALTQT_HFSC)
3546					if (strlcpy(n->parent, qname,
3547					    sizeof(n->parent)) >=
3548					    sizeof(n->parent))
3549						errx(1, "expand_altq: strlcpy");
3550				if (strlcpy(n->queue, queue->queue,
3551				    sizeof(n->queue)) >= sizeof(n->queue))
3552					errx(1, "expand_altq: strlcpy");
3553				if (strlcpy(n->ifname, interface->ifname,
3554				    sizeof(n->ifname)) >= sizeof(n->ifname))
3555					errx(1, "expand_altq: strlcpy");
3556				n->scheduler = pa.scheduler;
3557				n->next = NULL;
3558				n->tail = n;
3559				if (queues == NULL)
3560					queues = n;
3561				else {
3562					queues->tail->next = n;
3563					queues->tail = n;
3564				}
3565			);
3566		}
3567	);
3568	FREE_LIST(struct node_if, interfaces);
3569	FREE_LIST(struct node_queue, nqueues);
3570
3571	return (errs);
3572}
3573
3574int
3575expand_queue(struct pf_altq *a, struct node_if *interfaces,
3576    struct node_queue *nqueues, struct node_queue_bw bwspec,
3577    struct node_queue_opt *opts)
3578{
3579	struct node_queue	*n, *nq;
3580	struct pf_altq		 pa;
3581	u_int8_t		 found = 0;
3582	u_int8_t		 errs = 0;
3583
3584	if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
3585		FREE_LIST(struct node_queue, nqueues);
3586		return (0);
3587	}
3588
3589	if (queues == NULL) {
3590		yyerror("queue %s has no parent", a->qname);
3591		FREE_LIST(struct node_queue, nqueues);
3592		return (1);
3593	}
3594
3595	LOOP_THROUGH(struct node_if, interface, interfaces,
3596		LOOP_THROUGH(struct node_queue, tqueue, queues,
3597			if (!strncmp(a->qname, tqueue->queue, PF_QNAME_SIZE) &&
3598			    (interface->ifname[0] == 0 ||
3599			    (!interface->not && !strncmp(interface->ifname,
3600			    tqueue->ifname, IFNAMSIZ)) ||
3601			    (interface->not && strncmp(interface->ifname,
3602			    tqueue->ifname, IFNAMSIZ)))) {
3603				/* found ourself in queues */
3604				found++;
3605
3606				memcpy(&pa, a, sizeof(struct pf_altq));
3607
3608				if (pa.scheduler != ALTQT_NONE &&
3609				    pa.scheduler != tqueue->scheduler) {
3610					yyerror("exactly one scheduler type "
3611					    "per interface allowed");
3612					return (1);
3613				}
3614				pa.scheduler = tqueue->scheduler;
3615
3616				/* scheduler dependent error checking */
3617				switch (pa.scheduler) {
3618				case ALTQT_PRIQ:
3619					if (nqueues != NULL) {
3620						yyerror("priq queues cannot "
3621						    "have child queues");
3622						return (1);
3623					}
3624					if (bwspec.bw_absolute > 0 ||
3625					    bwspec.bw_percent < 100) {
3626						yyerror("priq doesn't take "
3627						    "bandwidth");
3628						return (1);
3629					}
3630					break;
3631				default:
3632					break;
3633				}
3634
3635				if (strlcpy(pa.ifname, tqueue->ifname,
3636				    sizeof(pa.ifname)) >= sizeof(pa.ifname))
3637					errx(1, "expand_queue: strlcpy");
3638				if (strlcpy(pa.parent, tqueue->parent,
3639				    sizeof(pa.parent)) >= sizeof(pa.parent))
3640					errx(1, "expand_queue: strlcpy");
3641
3642				if (eval_pfqueue(pf, &pa, &bwspec, opts))
3643					errs++;
3644				else
3645					if (pfctl_add_altq(pf, &pa))
3646						errs++;
3647
3648				for (nq = nqueues; nq != NULL; nq = nq->next) {
3649					if (!strcmp(a->qname, nq->queue)) {
3650						yyerror("queue cannot have "
3651						    "itself as child");
3652						errs++;
3653						continue;
3654					}
3655					n = calloc(1,
3656					    sizeof(struct node_queue));
3657					if (n == NULL)
3658						err(1, "expand_queue: calloc");
3659					if (strlcpy(n->parent, a->qname,
3660					    sizeof(n->parent)) >=
3661					    sizeof(n->parent))
3662						errx(1, "expand_queue strlcpy");
3663					if (strlcpy(n->queue, nq->queue,
3664					    sizeof(n->queue)) >=
3665					    sizeof(n->queue))
3666						errx(1, "expand_queue strlcpy");
3667					if (strlcpy(n->ifname, tqueue->ifname,
3668					    sizeof(n->ifname)) >=
3669					    sizeof(n->ifname))
3670						errx(1, "expand_queue strlcpy");
3671					n->scheduler = tqueue->scheduler;
3672					n->next = NULL;
3673					n->tail = n;
3674					if (queues == NULL)
3675						queues = n;
3676					else {
3677						queues->tail->next = n;
3678						queues->tail = n;
3679					}
3680				}
3681				if ((pf->opts & PF_OPT_VERBOSE) && (
3682				    (found == 1 && interface->ifname[0] == 0) ||
3683				    (found > 0 && interface->ifname[0] != 0))) {
3684					print_queue(&pf->paltq->altq, 0,
3685					    &bwspec, interface->ifname[0] != 0,
3686					    opts);
3687					if (nqueues && nqueues->tail) {
3688						printf("{ ");
3689						LOOP_THROUGH(struct node_queue,
3690						    queue, nqueues,
3691							printf("%s ",
3692							    queue->queue);
3693						);
3694						printf("}");
3695					}
3696					printf("\n");
3697				}
3698			}
3699		);
3700	);
3701
3702	FREE_LIST(struct node_queue, nqueues);
3703	FREE_LIST(struct node_if, interfaces);
3704
3705	if (!found) {
3706		yyerror("queue %s has no parent", a->qname);
3707		errs++;
3708	}
3709
3710	if (errs)
3711		return (1);
3712	else
3713		return (0);
3714}
3715
3716void
3717expand_rule(struct pf_rule *r,
3718    struct node_if *interfaces, struct node_host *rpool_hosts,
3719    struct node_proto *protos, struct node_os *src_oses,
3720    struct node_host *src_hosts, struct node_port *src_ports,
3721    struct node_host *dst_hosts, struct node_port *dst_ports,
3722    struct node_uid *uids, struct node_gid *gids, struct node_icmp *icmp_types)
3723{
3724	sa_family_t		 af = r->af;
3725	int			 added = 0, error = 0;
3726	char			 ifname[IF_NAMESIZE];
3727	char			 label[PF_RULE_LABEL_SIZE];
3728	struct pf_pooladdr	*pa;
3729	struct node_host	*h;
3730	u_int8_t		 flags, flagset;
3731
3732	if (strlcpy(label, r->label, sizeof(label)) >= sizeof(label))
3733		errx(1, "expand_rule: strlcpy");
3734	flags = r->flags;
3735	flagset = r->flagset;
3736
3737	LOOP_THROUGH(struct node_if, interface, interfaces,
3738	LOOP_THROUGH(struct node_proto, proto, protos,
3739	LOOP_THROUGH(struct node_icmp, icmp_type, icmp_types,
3740	LOOP_THROUGH(struct node_host, src_host, src_hosts,
3741	LOOP_THROUGH(struct node_port, src_port, src_ports,
3742	LOOP_THROUGH(struct node_os, src_os, src_oses,
3743	LOOP_THROUGH(struct node_host, dst_host, dst_hosts,
3744	LOOP_THROUGH(struct node_port, dst_port, dst_ports,
3745	LOOP_THROUGH(struct node_uid, uid, uids,
3746	LOOP_THROUGH(struct node_gid, gid, gids,
3747
3748		r->af = af;
3749		/* for link-local IPv6 address, interface must match up */
3750		if ((r->af && src_host->af && r->af != src_host->af) ||
3751		    (r->af && dst_host->af && r->af != dst_host->af) ||
3752		    (src_host->af && dst_host->af &&
3753		    src_host->af != dst_host->af) ||
3754		    (src_host->ifindex && dst_host->ifindex &&
3755		    src_host->ifindex != dst_host->ifindex) ||
3756		    (src_host->ifindex && if_nametoindex(interface->ifname) &&
3757		    src_host->ifindex != if_nametoindex(interface->ifname)) ||
3758		    (dst_host->ifindex && if_nametoindex(interface->ifname) &&
3759		    dst_host->ifindex != if_nametoindex(interface->ifname)))
3760			continue;
3761		if (!r->af && src_host->af)
3762			r->af = src_host->af;
3763		else if (!r->af && dst_host->af)
3764			r->af = dst_host->af;
3765
3766		if (if_indextoname(src_host->ifindex, ifname))
3767			memcpy(r->ifname, ifname, sizeof(r->ifname));
3768		else if (if_indextoname(dst_host->ifindex, ifname))
3769			memcpy(r->ifname, ifname, sizeof(r->ifname));
3770		else
3771			memcpy(r->ifname, interface->ifname, sizeof(r->ifname));
3772
3773		if (strlcpy(r->label, label, sizeof(r->label)) >=
3774		    sizeof(r->label))
3775			errx(1, "expand_rule: strlcpy");
3776		expand_label(r->label, r->ifname, r->af, src_host, src_port,
3777		    dst_host, dst_port, proto->proto);
3778
3779		error += check_netmask(src_host, r->af);
3780		error += check_netmask(dst_host, r->af);
3781
3782		r->ifnot = interface->not;
3783		r->proto = proto->proto;
3784		r->src.addr = src_host->addr;
3785		r->src.not = src_host->not;
3786		r->src.port[0] = src_port->port[0];
3787		r->src.port[1] = src_port->port[1];
3788		r->src.port_op = src_port->op;
3789		r->dst.addr = dst_host->addr;
3790		r->dst.not = dst_host->not;
3791		r->dst.port[0] = dst_port->port[0];
3792		r->dst.port[1] = dst_port->port[1];
3793		r->dst.port_op = dst_port->op;
3794		r->uid.op = uid->op;
3795		r->uid.uid[0] = uid->uid[0];
3796		r->uid.uid[1] = uid->uid[1];
3797		r->gid.op = gid->op;
3798		r->gid.gid[0] = gid->gid[0];
3799		r->gid.gid[1] = gid->gid[1];
3800		r->type = icmp_type->type;
3801		r->code = icmp_type->code;
3802
3803		if (r->proto && r->proto != IPPROTO_TCP) {
3804			r->flags = 0;
3805			r->flagset = 0;
3806		} else {
3807			r->flags = flags;
3808			r->flagset = flagset;
3809		}
3810		if (icmp_type->proto && r->proto != icmp_type->proto) {
3811			yyerror("icmp-type mismatch");
3812			error++;
3813		}
3814
3815		if (src_os && src_os->os) {
3816			r->os_fingerprint = pfctl_get_fingerprint(src_os->os);
3817			if ((pf->opts & PF_OPT_VERBOSE2) &&
3818			    r->os_fingerprint == PF_OSFP_NOMATCH)
3819				fprintf(stderr,
3820				    "warning: unknown '%s' OS fingerprint\n",
3821				    src_os->os);
3822		} else {
3823			r->os_fingerprint = PF_OSFP_ANY;
3824		}
3825
3826		TAILQ_INIT(&r->rpool.list);
3827		for (h = rpool_hosts; h != NULL; h = h->next) {
3828			pa = calloc(1, sizeof(struct pf_pooladdr));
3829			if (pa == NULL)
3830				err(1, "expand_rule: calloc");
3831			pa->addr = h->addr;
3832			if (h->ifname != NULL) {
3833				if (strlcpy(pa->ifname, h->ifname,
3834				    sizeof(pa->ifname)) >=
3835				    sizeof(pa->ifname))
3836					errx(1, "expand_rule: strlcpy");
3837			} else
3838				pa->ifname[0] = 0;
3839			TAILQ_INSERT_TAIL(&r->rpool.list, pa, entries);
3840		}
3841
3842		if (rule_consistent(r) < 0 || error)
3843			yyerror("skipping rule due to errors");
3844		else {
3845			r->nr = pf->rule_nr++;
3846			pfctl_add_rule(pf, r);
3847			added++;
3848		}
3849
3850	))))))))));
3851
3852	FREE_LIST(struct node_if, interfaces);
3853	FREE_LIST(struct node_proto, protos);
3854	FREE_LIST(struct node_host, src_hosts);
3855	FREE_LIST(struct node_port, src_ports);
3856	FREE_LIST(struct node_os, src_oses);
3857	FREE_LIST(struct node_host, dst_hosts);
3858	FREE_LIST(struct node_port, dst_ports);
3859	FREE_LIST(struct node_uid, uids);
3860	FREE_LIST(struct node_gid, gids);
3861	FREE_LIST(struct node_icmp, icmp_types);
3862	FREE_LIST(struct node_host, rpool_hosts);
3863
3864	if (!added)
3865		yyerror("rule expands to no valid combination");
3866}
3867
3868#undef FREE_LIST
3869#undef LOOP_THROUGH
3870
3871int
3872check_rulestate(int desired_state)
3873{
3874	if (require_order && (rulestate > desired_state)) {
3875		yyerror("Rules must be in order: options, normalization, "
3876		    "queueing, translation, filtering");
3877		return (1);
3878	}
3879	rulestate = desired_state;
3880	return (0);
3881}
3882
3883int
3884kw_cmp(const void *k, const void *e)
3885{
3886	return (strcmp(k, ((const struct keywords *)e)->k_name));
3887}
3888
3889int
3890lookup(char *s)
3891{
3892	/* this has to be sorted always */
3893	static const struct keywords keywords[] = {
3894		{ "all",		ALL},
3895		{ "allow-opts",		ALLOWOPTS},
3896		{ "altq",		ALTQ},
3897		{ "anchor",		ANCHOR},
3898		{ "antispoof",		ANTISPOOF},
3899		{ "any",		ANY},
3900		{ "bandwidth",		BANDWIDTH},
3901		{ "binat",		BINAT},
3902		{ "binat-anchor",	BINATANCHOR},
3903		{ "bitmask",		BITMASK},
3904		{ "block",		BLOCK},
3905		{ "block-policy",	BLOCKPOLICY},
3906		{ "cbq",		CBQ},
3907		{ "code",		CODE},
3908		{ "crop",		FRAGCROP},
3909		{ "drop",		DROP},
3910		{ "drop-ovl",		FRAGDROP},
3911		{ "dup-to",		DUPTO},
3912		{ "fastroute",		FASTROUTE},
3913		{ "file",		FILENAME},
3914		{ "fingerprints",	FINGERPRINTS},
3915		{ "flags",		FLAGS},
3916		{ "for",		FOR},
3917		{ "fragment",		FRAGMENT},
3918		{ "from",		FROM},
3919		{ "group",		GROUP},
3920		{ "hfsc",		HFSC},
3921		{ "icmp-type",		ICMPTYPE},
3922		{ "icmp6-type",		ICMP6TYPE},
3923		{ "in",			IN},
3924		{ "inet",		INET},
3925		{ "inet6",		INET6},
3926		{ "keep",		KEEP},
3927		{ "label",		LABEL},
3928		{ "limit",		LIMIT},
3929		{ "linkshare",		LINKSHARE},
3930		{ "load",		LOAD},
3931		{ "log",		LOG},
3932		{ "log-all",		LOGALL},
3933		{ "loginterface",	LOGINTERFACE},
3934		{ "max",		MAXIMUM},
3935		{ "max-mss",		MAXMSS},
3936		{ "min-ttl",		MINTTL},
3937		{ "modulate",		MODULATE},
3938		{ "nat",		NAT},
3939		{ "nat-anchor",		NATANCHOR},
3940		{ "no",			NO},
3941		{ "no-df",		NODF},
3942		{ "no-route",		NOROUTE},
3943		{ "on",			ON},
3944		{ "optimization",	OPTIMIZATION},
3945		{ "os",			OS},
3946		{ "out",		OUT},
3947		{ "pass",		PASS},
3948		{ "port",		PORT},
3949		{ "priority",		PRIORITY},
3950		{ "priq",		PRIQ},
3951		{ "proto",		PROTO},
3952		{ "qlimit",		QLIMIT},
3953		{ "queue",		QUEUE},
3954		{ "quick",		QUICK},
3955		{ "random",		RANDOM},
3956		{ "random-id",		RANDOMID},
3957		{ "rdr",		RDR},
3958		{ "rdr-anchor",		RDRANCHOR},
3959		{ "realtime",		REALTIME},
3960		{ "reassemble",		REASSEMBLE},
3961		{ "reply-to",		REPLYTO},
3962		{ "require-order",	REQUIREORDER},
3963		{ "return",		RETURN},
3964		{ "return-icmp",	RETURNICMP},
3965		{ "return-icmp6",	RETURNICMP6},
3966		{ "return-rst",		RETURNRST},
3967		{ "round-robin",	ROUNDROBIN},
3968		{ "route-to",		ROUTETO},
3969		{ "scrub",		SCRUB},
3970		{ "set",		SET},
3971		{ "source-hash",	SOURCEHASH},
3972		{ "state",		STATE},
3973		{ "static-port",	STATICPORT},
3974		{ "synproxy",		SYNPROXY},
3975		{ "table",		TABLE},
3976		{ "tag",		TAG},
3977		{ "tagged",		TAGGED},
3978		{ "tbrsize",		TBRSIZE},
3979		{ "timeout",		TIMEOUT},
3980		{ "to",			TO},
3981		{ "tos",		TOS},
3982		{ "ttl",		TTL},
3983		{ "upperlimit",		UPPERLIMIT},
3984		{ "user",		USER},
3985	};
3986	const struct keywords	*p;
3987
3988	p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
3989	    sizeof(keywords[0]), kw_cmp);
3990
3991	if (p) {
3992		if (debug > 1)
3993			fprintf(stderr, "%s: %d\n", s, p->k_val);
3994		return (p->k_val);
3995	} else {
3996		if (debug > 1)
3997			fprintf(stderr, "string: %s\n", s);
3998		return (STRING);
3999	}
4000}
4001
4002#define MAXPUSHBACK	128
4003
4004char	*parsebuf;
4005int	 parseindex;
4006char	 pushback_buffer[MAXPUSHBACK];
4007int	 pushback_index = 0;
4008
4009int
4010lgetc(FILE *f)
4011{
4012	int	c, next;
4013
4014	if (parsebuf) {
4015		/* Read character from the parsebuffer instead of input. */
4016		if (parseindex >= 0) {
4017			c = parsebuf[parseindex++];
4018			if (c != '\0')
4019				return (c);
4020			parsebuf = NULL;
4021		} else
4022			parseindex++;
4023	}
4024
4025	if (pushback_index)
4026		return (pushback_buffer[--pushback_index]);
4027
4028	while ((c = getc(f)) == '\\') {
4029		next = getc(f);
4030		if (next != '\n') {
4031			if (isspace(next))
4032				yyerror("whitespace after \\");
4033			ungetc(next, f);
4034			break;
4035		}
4036		yylval.lineno = lineno;
4037		lineno++;
4038	}
4039	if (c == '\t' || c == ' ') {
4040		/* Compress blanks to a single space. */
4041		do {
4042			c = getc(f);
4043		} while (c == '\t' || c == ' ');
4044		ungetc(c, f);
4045		c = ' ';
4046	}
4047
4048	return (c);
4049}
4050
4051int
4052lungetc(int c)
4053{
4054	if (c == EOF)
4055		return (EOF);
4056	if (parsebuf) {
4057		parseindex--;
4058		if (parseindex >= 0)
4059			return (c);
4060	}
4061	if (pushback_index < MAXPUSHBACK-1)
4062		return (pushback_buffer[pushback_index++] = c);
4063	else
4064		return (EOF);
4065}
4066
4067int
4068findeol(void)
4069{
4070	int	c;
4071
4072	parsebuf = NULL;
4073	pushback_index = 0;
4074
4075	/* skip to either EOF or the first real EOL */
4076	while (1) {
4077		c = lgetc(fin);
4078		if (c == '\n') {
4079			lineno++;
4080			break;
4081		}
4082		if (c == EOF)
4083			break;
4084	}
4085	return (ERROR);
4086}
4087
4088int
4089yylex(void)
4090{
4091	char	 buf[8096];
4092	char	*p, *val;
4093	int	 endc, c, next;
4094	int	 token;
4095
4096top:
4097	p = buf;
4098	while ((c = lgetc(fin)) == ' ')
4099		; /* nothing */
4100
4101	yylval.lineno = lineno;
4102	if (c == '#')
4103		while ((c = lgetc(fin)) != '\n' && c != EOF)
4104			; /* nothing */
4105	if (c == '$' && parsebuf == NULL) {
4106		while (1) {
4107			if ((c = lgetc(fin)) == EOF)
4108				return (0);
4109
4110			if (p + 1 >= buf + sizeof(buf) - 1) {
4111				yyerror("string too long");
4112				return (findeol());
4113			}
4114			if (isalnum(c) || c == '_') {
4115				*p++ = (char)c;
4116				continue;
4117			}
4118			*p = '\0';
4119			lungetc(c);
4120			break;
4121		}
4122		val = symget(buf);
4123		if (val == NULL) {
4124			yyerror("macro '%s' not defined", buf);
4125			return (findeol());
4126		}
4127		parsebuf = val;
4128		parseindex = 0;
4129		goto top;
4130	}
4131
4132	switch (c) {
4133	case '\'':
4134	case '"':
4135		endc = c;
4136		while (1) {
4137			if ((c = lgetc(fin)) == EOF)
4138				return (0);
4139			if (c == endc) {
4140				*p = '\0';
4141				break;
4142			}
4143			if (c == '\n') {
4144				lineno++;
4145				continue;
4146			}
4147			if (p + 1 >= buf + sizeof(buf) - 1) {
4148				yyerror("string too long");
4149				return (findeol());
4150			}
4151			*p++ = (char)c;
4152		}
4153		yylval.v.string = strdup(buf);
4154		if (yylval.v.string == NULL)
4155			err(1, "yylex: strdup");
4156		return (STRING);
4157	case '<':
4158		next = lgetc(fin);
4159		if (next == '>') {
4160			yylval.v.i = PF_OP_XRG;
4161			return (PORTBINARY);
4162		}
4163		lungetc(next);
4164		break;
4165	case '>':
4166		next = lgetc(fin);
4167		if (next == '<') {
4168			yylval.v.i = PF_OP_IRG;
4169			return (PORTBINARY);
4170		}
4171		lungetc(next);
4172		break;
4173	case '-':
4174		next = lgetc(fin);
4175		if (next == '>')
4176			return (ARROW);
4177		lungetc(next);
4178		break;
4179	}
4180
4181#define allowed_in_string(x) \
4182	(isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
4183	x != '{' && x != '}' && x != '<' && x != '>' && \
4184	x != '!' && x != '=' && x != '/' && x != '#' && \
4185	x != ','))
4186
4187	if (isalnum(c) || c == ':' || c == '_') {
4188		do {
4189			*p++ = c;
4190			if ((unsigned)(p-buf) >= sizeof(buf)) {
4191				yyerror("string too long");
4192				return (findeol());
4193			}
4194		} while ((c = lgetc(fin)) != EOF && (allowed_in_string(c)));
4195		lungetc(c);
4196		*p = '\0';
4197		token = lookup(buf);
4198		yylval.v.string = strdup(buf);
4199		if (yylval.v.string == NULL)
4200			err(1, "yylex: strdup");
4201		return (token);
4202	}
4203	if (c == '\n') {
4204		yylval.lineno = lineno;
4205		lineno++;
4206	}
4207	if (c == EOF)
4208		return (0);
4209	return (c);
4210}
4211
4212int
4213parse_rules(FILE *input, struct pfctl *xpf)
4214{
4215	struct sym	*sym;
4216
4217	fin = input;
4218	pf = xpf;
4219	lineno = 1;
4220	errors = 0;
4221	rulestate = PFCTL_STATE_NONE;
4222	returnicmpdefault = (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
4223	returnicmp6default =
4224	    (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
4225	blockpolicy = PFRULE_DROP;
4226	require_order = 1;
4227
4228	yyparse();
4229
4230	/* Free macros and check which have not been used. */
4231	TAILQ_FOREACH(sym, &symhead, entries) {
4232		if ((pf->opts & PF_OPT_VERBOSE2) && !sym->used)
4233			fprintf(stderr, "warning: macro '%s' not "
4234			    "used\n", sym->nam);
4235		free(sym->nam);
4236		free(sym->val);
4237		TAILQ_REMOVE(&symhead, sym, entries);
4238	}
4239
4240	return (errors ? -1 : 0);
4241}
4242
4243/*
4244 * Over-designed efficiency is a French and German concept, so how about
4245 * we wait until they discover this ugliness and make it all fancy.
4246 */
4247int
4248symset(const char *nam, const char *val, int persist)
4249{
4250	struct sym	*sym;
4251
4252	for (sym = TAILQ_FIRST(&symhead); sym && strcmp(nam, sym->nam);
4253	    sym = TAILQ_NEXT(sym, entries))
4254		;	/* nothing */
4255
4256	if (sym != NULL) {
4257		if (sym->persist == 1)
4258			return (0);
4259		else {
4260			free(sym->nam);
4261			free(sym->val);
4262			TAILQ_REMOVE(&symhead, sym, entries);
4263			free(sym);
4264		}
4265	}
4266	if ((sym = calloc(1, sizeof(*sym))) == NULL)
4267		return (-1);
4268
4269	sym->nam = strdup(nam);
4270	if (sym->nam == NULL) {
4271		free(sym);
4272		return (-1);
4273	}
4274	sym->val = strdup(val);
4275	if (sym->val == NULL) {
4276		free(sym->nam);
4277		free(sym);
4278		return (-1);
4279	}
4280	sym->used = 0;
4281	sym->persist = persist;
4282	TAILQ_INSERT_TAIL(&symhead, sym, entries);
4283	return (0);
4284}
4285
4286int
4287pfctl_cmdline_symset(char *s)
4288{
4289	char	*sym, *val;
4290	int	 ret;
4291
4292	if ((val = strrchr(s, '=')) == NULL)
4293		return (-1);
4294
4295	if ((sym = malloc(strlen(s) - strlen(val) + 1)) == NULL)
4296		err(1, "pfctl_cmdline_symset: malloc");
4297
4298	strlcpy(sym, s, strlen(s) - strlen(val) + 1);
4299
4300	ret = symset(sym, val + 1, 1);
4301	free(sym);
4302
4303	return (ret);
4304}
4305
4306char *
4307symget(const char *nam)
4308{
4309	struct sym	*sym;
4310
4311	TAILQ_FOREACH(sym, &symhead, entries)
4312		if (strcmp(nam, sym->nam) == 0) {
4313			sym->used = 1;
4314			return (sym->val);
4315		}
4316	return (NULL);
4317}
4318
4319void
4320decide_address_family(struct node_host *n, sa_family_t *af)
4321{
4322	sa_family_t	target_af = 0;
4323
4324	while (!*af && n != NULL) {
4325		if (n->af) {
4326			if (target_af == 0)
4327				target_af = n->af;
4328			if (target_af != n->af)
4329				return;
4330		}
4331		n = n->next;
4332	}
4333	if (!*af && target_af)
4334		*af = target_af;
4335}
4336
4337void
4338remove_invalid_hosts(struct node_host **nh, sa_family_t *af)
4339{
4340	struct node_host	*n = *nh, *prev = NULL;
4341
4342	while (n != NULL) {
4343		if (*af && n->af && n->af != *af) {
4344			/* unlink and free n */
4345			struct node_host *next = n->next;
4346
4347			/* adjust tail pointer */
4348			if (n == (*nh)->tail)
4349				(*nh)->tail = prev;
4350			/* adjust previous node's next pointer */
4351			if (prev == NULL)
4352				*nh = next;
4353			else
4354				prev->next = next;
4355			/* free node */
4356			if (n->ifname != NULL)
4357				free(n->ifname);
4358			free(n);
4359			n = next;
4360		} else {
4361			if (n->af && !*af)
4362				*af = n->af;
4363			prev = n;
4364			n = n->next;
4365		}
4366	}
4367}
4368
4369int
4370invalid_redirect(struct node_host *nh, sa_family_t af)
4371{
4372	if (!af) {
4373		struct node_host *n;
4374
4375		/* only tables are ok without an address family */
4376		for (n = nh; n != NULL; n = n->next) {
4377			if (n->addr.type != PF_ADDR_TABLE) {
4378				yyerror("address family not given and "
4379				    "translation address expands to multiple "
4380				    "address families");
4381				return (1);
4382			}
4383		}
4384	}
4385	if (nh == NULL) {
4386		yyerror("no translation address with matching address family "
4387		    "found.");
4388		return (1);
4389	}
4390	return (0);
4391}
4392
4393int
4394atoul(char *s, u_long *ulvalp)
4395{
4396	u_long	 ulval;
4397	char	*ep;
4398
4399	errno = 0;
4400	ulval = strtoul(s, &ep, 0);
4401	if (s[0] == '\0' || *ep != '\0')
4402		return (-1);
4403	if (errno == ERANGE && ulval == ULONG_MAX)
4404		return (-1);
4405	*ulvalp = ulval;
4406	return (0);
4407}
4408
4409int
4410getservice(char *n)
4411{
4412	struct servent	*s;
4413	u_long		 ulval;
4414
4415	if (atoul(n, &ulval) == 0) {
4416		if (ulval > 65535) {
4417			yyerror("illegal port value %d", ulval);
4418			return (-1);
4419		}
4420		return (htons(ulval));
4421	} else {
4422		s = getservbyname(n, "tcp");
4423		if (s == NULL)
4424			s = getservbyname(n, "udp");
4425		if (s == NULL) {
4426			yyerror("unknown port %s", n);
4427			return (-1);
4428		}
4429		return (s->s_port);
4430	}
4431}
4432
4433int
4434rule_label(struct pf_rule *r, char *s)
4435{
4436	if (s) {
4437		if (strlcpy(r->label, s, sizeof(r->label)) >=
4438		    sizeof(r->label)) {
4439			yyerror("rule label too long (max %d chars)",
4440			    sizeof(r->label)-1);
4441			return (-1);
4442		}
4443	}
4444	return (0);
4445}
4446
4447u_int16_t
4448parseicmpspec(char *w, sa_family_t af)
4449{
4450	const struct icmpcodeent	*p;
4451	u_long				 ulval;
4452	u_int8_t			 icmptype;
4453
4454	if (af == AF_INET)
4455		icmptype = returnicmpdefault >> 8;
4456	else
4457		icmptype = returnicmp6default >> 8;
4458
4459	if (atoul(w, &ulval) == -1) {
4460		if ((p = geticmpcodebyname(icmptype, w, af)) == NULL) {
4461			yyerror("unknown icmp code %s", w);
4462			return (0);
4463		}
4464		ulval = p->code;
4465	}
4466	if (ulval > 255) {
4467		yyerror("invalid icmp code %ld", ulval);
4468		return (0);
4469	}
4470	return (icmptype << 8 | ulval);
4471}
4472
4473int
4474pfctl_load_anchors(int dev, int opts)
4475{
4476	struct loadanchors	*la;
4477
4478	TAILQ_FOREACH(la, &loadanchorshead, entries) {
4479		if (opts & PF_OPT_VERBOSE)
4480			fprintf(stderr, "\nLoading anchor %s:%s from %s\n",
4481			    la->anchorname, la->rulesetname, la->filename);
4482		if (pfctl_rules(dev, la->filename, opts, la->anchorname,
4483		    la->rulesetname) == -1)
4484			return (-1);
4485	}
4486
4487	return (0);
4488}
4489
4490