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