parse.y revision 178894
1171172Smlaier/*	$OpenBSD: parse.y,v 1.517 2007/02/03 23:26:40 dhartmei Exp $	*/
2126353Smlaier
3126353Smlaier/*
4126353Smlaier * Copyright (c) 2001 Markus Friedl.  All rights reserved.
5126353Smlaier * Copyright (c) 2001 Daniel Hartmeier.  All rights reserved.
6130617Smlaier * Copyright (c) 2001 Theo de Raadt.  All rights reserved.
7130617Smlaier * Copyright (c) 2002,2003 Henning Brauer. All rights reserved.
8126353Smlaier *
9126353Smlaier * Redistribution and use in source and binary forms, with or without
10126353Smlaier * modification, are permitted provided that the following conditions
11126353Smlaier * are met:
12126353Smlaier * 1. Redistributions of source code must retain the above copyright
13126353Smlaier *    notice, this list of conditions and the following disclaimer.
14126353Smlaier * 2. Redistributions in binary form must reproduce the above copyright
15126353Smlaier *    notice, this list of conditions and the following disclaimer in the
16126353Smlaier *    documentation and/or other materials provided with the distribution.
17126353Smlaier *
18126353Smlaier * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19126353Smlaier * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20126353Smlaier * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21126353Smlaier * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22126353Smlaier * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23126353Smlaier * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24126353Smlaier * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25126353Smlaier * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26126353Smlaier * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27126353Smlaier * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28126353Smlaier */
29126353Smlaier%{
30127082Sobrien#include <sys/cdefs.h>
31127082Sobrien__FBSDID("$FreeBSD: head/contrib/pf/pfctl/parse.y 178894 2008-05-09 23:53:01Z julian $");
32127082Sobrien
33126353Smlaier#include <sys/types.h>
34126353Smlaier#include <sys/socket.h>
35126353Smlaier#include <net/if.h>
36126353Smlaier#include <netinet/in.h>
37126353Smlaier#include <netinet/in_systm.h>
38126353Smlaier#include <netinet/ip.h>
39126353Smlaier#include <netinet/ip_icmp.h>
40126353Smlaier#include <netinet/icmp6.h>
41126353Smlaier#include <net/pfvar.h>
42126353Smlaier#include <arpa/inet.h>
43126353Smlaier#include <altq/altq.h>
44126353Smlaier#include <altq/altq_cbq.h>
45126353Smlaier#include <altq/altq_priq.h>
46126353Smlaier#include <altq/altq_hfsc.h>
47126353Smlaier
48126353Smlaier#include <stdio.h>
49126353Smlaier#include <stdlib.h>
50126353Smlaier#include <netdb.h>
51126353Smlaier#include <stdarg.h>
52126353Smlaier#include <errno.h>
53126353Smlaier#include <string.h>
54126353Smlaier#include <ctype.h>
55145840Smlaier#include <math.h>
56126353Smlaier#include <err.h>
57127024Smlaier#include <limits.h>
58126353Smlaier#include <pwd.h>
59126353Smlaier#include <grp.h>
60126353Smlaier#include <md5.h>
61126353Smlaier
62126353Smlaier#include "pfctl_parser.h"
63126353Smlaier#include "pfctl.h"
64126353Smlaier
65127024Smlaier#ifdef __FreeBSD__
66126361Smlaier#define	HTONL(x)	(x) = htonl((__uint32_t)(x))
67126361Smlaier#endif
68126361Smlaier
69126353Smlaierstatic struct pfctl	*pf = NULL;
70126353Smlaierstatic FILE		*fin = NULL;
71126353Smlaierstatic int		 debug = 0;
72126353Smlaierstatic int		 lineno = 1;
73126353Smlaierstatic int		 errors = 0;
74126353Smlaierstatic int		 rulestate = 0;
75126353Smlaierstatic u_int16_t	 returnicmpdefault =
76126353Smlaier			    (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
77126353Smlaierstatic u_int16_t	 returnicmp6default =
78126353Smlaier			    (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
79126353Smlaierstatic int		 blockpolicy = PFRULE_DROP;
80126353Smlaierstatic int		 require_order = 1;
81130617Smlaierstatic int		 default_statelock;
82126353Smlaier
83126353Smlaierenum {
84126353Smlaier	PFCTL_STATE_NONE,
85126353Smlaier	PFCTL_STATE_OPTION,
86126353Smlaier	PFCTL_STATE_SCRUB,
87126353Smlaier	PFCTL_STATE_QUEUE,
88126353Smlaier	PFCTL_STATE_NAT,
89126353Smlaier	PFCTL_STATE_FILTER
90126353Smlaier};
91126353Smlaier
92126353Smlaierstruct node_proto {
93126353Smlaier	u_int8_t		 proto;
94126353Smlaier	struct node_proto	*next;
95126353Smlaier	struct node_proto	*tail;
96126353Smlaier};
97126353Smlaier
98126353Smlaierstruct node_port {
99126353Smlaier	u_int16_t		 port[2];
100126353Smlaier	u_int8_t		 op;
101126353Smlaier	struct node_port	*next;
102126353Smlaier	struct node_port	*tail;
103126353Smlaier};
104126353Smlaier
105126353Smlaierstruct node_uid {
106126353Smlaier	uid_t			 uid[2];
107126353Smlaier	u_int8_t		 op;
108126353Smlaier	struct node_uid		*next;
109126353Smlaier	struct node_uid		*tail;
110126353Smlaier};
111126353Smlaier
112126353Smlaierstruct node_gid {
113126353Smlaier	gid_t			 gid[2];
114126353Smlaier	u_int8_t		 op;
115126353Smlaier	struct node_gid		*next;
116126353Smlaier	struct node_gid		*tail;
117126353Smlaier};
118126353Smlaier
119126353Smlaierstruct node_icmp {
120126353Smlaier	u_int8_t		 code;
121126353Smlaier	u_int8_t		 type;
122126353Smlaier	u_int8_t		 proto;
123126353Smlaier	struct node_icmp	*next;
124126353Smlaier	struct node_icmp	*tail;
125126353Smlaier};
126126353Smlaier
127130617Smlaierenum	{ PF_STATE_OPT_MAX, PF_STATE_OPT_NOSYNC, PF_STATE_OPT_SRCTRACK,
128145840Smlaier	    PF_STATE_OPT_MAX_SRC_STATES, PF_STATE_OPT_MAX_SRC_CONN,
129145840Smlaier	    PF_STATE_OPT_MAX_SRC_CONN_RATE, PF_STATE_OPT_MAX_SRC_NODES,
130145840Smlaier	    PF_STATE_OPT_OVERLOAD, PF_STATE_OPT_STATELOCK,
131145840Smlaier	    PF_STATE_OPT_TIMEOUT };
132130617Smlaier
133130617Smlaierenum	{ PF_SRCTRACK_NONE, PF_SRCTRACK, PF_SRCTRACK_GLOBAL, PF_SRCTRACK_RULE };
134130617Smlaier
135126353Smlaierstruct node_state_opt {
136126353Smlaier	int			 type;
137126353Smlaier	union {
138126353Smlaier		u_int32_t	 max_states;
139130617Smlaier		u_int32_t	 max_src_states;
140145840Smlaier		u_int32_t	 max_src_conn;
141145840Smlaier		struct {
142145840Smlaier			u_int32_t	limit;
143145840Smlaier			u_int32_t	seconds;
144145840Smlaier		}		 max_src_conn_rate;
145145840Smlaier		struct {
146145840Smlaier			u_int8_t	flush;
147145840Smlaier			char		tblname[PF_TABLE_NAME_SIZE];
148145840Smlaier		}		 overload;
149130617Smlaier		u_int32_t	 max_src_nodes;
150130617Smlaier		u_int8_t	 src_track;
151130617Smlaier		u_int32_t	 statelock;
152126353Smlaier		struct {
153126353Smlaier			int		number;
154126353Smlaier			u_int32_t	seconds;
155126353Smlaier		}		 timeout;
156126353Smlaier	}			 data;
157126353Smlaier	struct node_state_opt	*next;
158126353Smlaier	struct node_state_opt	*tail;
159126353Smlaier};
160126353Smlaier
161126353Smlaierstruct peer {
162126353Smlaier	struct node_host	*host;
163126353Smlaier	struct node_port	*port;
164126353Smlaier};
165126353Smlaier
166126353Smlaierstruct node_queue {
167126353Smlaier	char			 queue[PF_QNAME_SIZE];
168126353Smlaier	char			 parent[PF_QNAME_SIZE];
169126353Smlaier	char			 ifname[IFNAMSIZ];
170126353Smlaier	int			 scheduler;
171126353Smlaier	struct node_queue	*next;
172126353Smlaier	struct node_queue	*tail;
173126353Smlaier}	*queues = NULL;
174126353Smlaier
175126353Smlaierstruct node_qassign {
176126353Smlaier	char		*qname;
177126353Smlaier	char		*pqname;
178126353Smlaier};
179126353Smlaier
180126353Smlaierstruct filter_opts {
181126353Smlaier	int			 marker;
182126353Smlaier#define FOM_FLAGS	0x01
183126353Smlaier#define FOM_ICMP	0x02
184126353Smlaier#define FOM_TOS		0x04
185126353Smlaier#define FOM_KEEP	0x08
186130617Smlaier#define FOM_SRCTRACK	0x10
187126353Smlaier	struct node_uid		*uid;
188126353Smlaier	struct node_gid		*gid;
189126353Smlaier	struct {
190126353Smlaier		u_int8_t	 b1;
191126353Smlaier		u_int8_t	 b2;
192126353Smlaier		u_int16_t	 w;
193126353Smlaier		u_int16_t	 w2;
194126353Smlaier	} flags;
195126353Smlaier	struct node_icmp	*icmpspec;
196126353Smlaier	u_int32_t		 tos;
197145840Smlaier	u_int32_t		 prob;
198126353Smlaier	struct {
199126353Smlaier		int			 action;
200126353Smlaier		struct node_state_opt	*options;
201126353Smlaier	} keep;
202126353Smlaier	int			 fragment;
203126353Smlaier	int			 allowopts;
204126353Smlaier	char			*label;
205126353Smlaier	struct node_qassign	 queues;
206126353Smlaier	char			*tag;
207126353Smlaier	char			*match_tag;
208126353Smlaier	u_int8_t		 match_tag_not;
209171172Smlaier	int			 rtableid;
210126353Smlaier} filter_opts;
211126353Smlaier
212126353Smlaierstruct antispoof_opts {
213126353Smlaier	char			*label;
214171172Smlaier	int			 rtableid;
215126353Smlaier} antispoof_opts;
216126353Smlaier
217126353Smlaierstruct scrub_opts {
218126353Smlaier	int			marker;
219126353Smlaier#define SOM_MINTTL	0x01
220126353Smlaier#define SOM_MAXMSS	0x02
221126353Smlaier#define SOM_FRAGCACHE	0x04
222126353Smlaier	int			nodf;
223126353Smlaier	int			minttl;
224126353Smlaier	int			maxmss;
225126353Smlaier	int			fragcache;
226126353Smlaier	int			randomid;
227126353Smlaier	int			reassemble_tcp;
228171172Smlaier	int			rtableid;
229126353Smlaier} scrub_opts;
230126353Smlaier
231126353Smlaierstruct queue_opts {
232126353Smlaier	int			marker;
233126353Smlaier#define QOM_BWSPEC	0x01
234126353Smlaier#define QOM_SCHEDULER	0x02
235126353Smlaier#define QOM_PRIORITY	0x04
236126353Smlaier#define QOM_TBRSIZE	0x08
237126353Smlaier#define QOM_QLIMIT	0x10
238126353Smlaier	struct node_queue_bw	queue_bwspec;
239126353Smlaier	struct node_queue_opt	scheduler;
240126353Smlaier	int			priority;
241126353Smlaier	int			tbrsize;
242126353Smlaier	int			qlimit;
243126353Smlaier} queue_opts;
244126353Smlaier
245126353Smlaierstruct table_opts {
246126353Smlaier	int			flags;
247126353Smlaier	int			init_addr;
248126353Smlaier	struct node_tinithead	init_nodes;
249126353Smlaier} table_opts;
250126353Smlaier
251130617Smlaierstruct pool_opts {
252130617Smlaier	int			 marker;
253130617Smlaier#define POM_TYPE		0x01
254130617Smlaier#define POM_STICKYADDRESS	0x02
255130617Smlaier	u_int8_t		 opts;
256130617Smlaier	int			 type;
257130617Smlaier	int			 staticport;
258130617Smlaier	struct pf_poolhashkey	*key;
259130617Smlaier
260130617Smlaier} pool_opts;
261130617Smlaier
262130617Smlaier
263126353Smlaierstruct node_hfsc_opts	hfsc_opts;
264126353Smlaier
265126353Smlaierint	yyerror(const char *, ...);
266126353Smlaierint	disallow_table(struct node_host *, const char *);
267171172Smlaierint	disallow_urpf_failed(struct node_host *, const char *);
268130617Smlaierint	disallow_alias(struct node_host *, const char *);
269171172Smlaierint	rule_consistent(struct pf_rule *, int);
270171172Smlaierint	filter_consistent(struct pf_rule *, int);
271126353Smlaierint	nat_consistent(struct pf_rule *);
272126353Smlaierint	rdr_consistent(struct pf_rule *);
273126353Smlaierint	process_tabledef(char *, struct table_opts *);
274126353Smlaierint	yyparse(void);
275130617Smlaiervoid	expand_label_str(char *, size_t, const char *, const char *);
276130617Smlaiervoid	expand_label_if(const char *, char *, size_t, const char *);
277130617Smlaiervoid	expand_label_addr(const char *, char *, size_t, u_int8_t,
278130617Smlaier	    struct node_host *);
279130617Smlaiervoid	expand_label_port(const char *, char *, size_t, struct node_port *);
280130617Smlaiervoid	expand_label_proto(const char *, char *, size_t, u_int8_t);
281130617Smlaiervoid	expand_label_nr(const char *, char *, size_t);
282130617Smlaiervoid	expand_label(char *, size_t, const char *, u_int8_t, struct node_host *,
283126353Smlaier	    struct node_port *, struct node_host *, struct node_port *,
284126353Smlaier	    u_int8_t);
285126353Smlaiervoid	expand_rule(struct pf_rule *, struct node_if *, struct node_host *,
286126353Smlaier	    struct node_proto *, struct node_os*, struct node_host *,
287126353Smlaier	    struct node_port *, struct node_host *, struct node_port *,
288145840Smlaier	    struct node_uid *, struct node_gid *, struct node_icmp *,
289145840Smlaier	    const char *);
290126353Smlaierint	expand_altq(struct pf_altq *, struct node_if *, struct node_queue *,
291126353Smlaier	    struct node_queue_bw bwspec, struct node_queue_opt *);
292126353Smlaierint	expand_queue(struct pf_altq *, struct node_if *, struct node_queue *,
293126353Smlaier	    struct node_queue_bw, struct node_queue_opt *);
294145840Smlaierint	expand_skip_interface(struct node_if *);
295126353Smlaier
296126353Smlaierint	 check_rulestate(int);
297126353Smlaierint	 kw_cmp(const void *, const void *);
298126353Smlaierint	 lookup(char *);
299126353Smlaierint	 lgetc(FILE *);
300126353Smlaierint	 lungetc(int);
301126353Smlaierint	 findeol(void);
302126353Smlaierint	 yylex(void);
303126353Smlaierint	 atoul(char *, u_long *);
304126353Smlaierint	 getservice(char *);
305126353Smlaierint	 rule_label(struct pf_rule *, char *);
306126353Smlaier
307126353SmlaierTAILQ_HEAD(symhead, sym)	 symhead = TAILQ_HEAD_INITIALIZER(symhead);
308126353Smlaierstruct sym {
309126353Smlaier	TAILQ_ENTRY(sym)	 entries;
310126353Smlaier	int			 used;
311126353Smlaier	int			 persist;
312126353Smlaier	char			*nam;
313126353Smlaier	char			*val;
314126353Smlaier};
315126353Smlaier
316126353Smlaier
317126353Smlaierint	 symset(const char *, const char *, int);
318126353Smlaierchar	*symget(const char *);
319126353Smlaier
320171172Smlaiervoid	 mv_rules(struct pf_ruleset *, struct pf_ruleset *);
321126353Smlaiervoid	 decide_address_family(struct node_host *, sa_family_t *);
322126353Smlaiervoid	 remove_invalid_hosts(struct node_host **, sa_family_t *);
323126353Smlaierint	 invalid_redirect(struct node_host *, sa_family_t);
324126353Smlaieru_int16_t parseicmpspec(char *, sa_family_t);
325126353Smlaier
326130617SmlaierTAILQ_HEAD(loadanchorshead, loadanchors)
327130617Smlaier    loadanchorshead = TAILQ_HEAD_INITIALIZER(loadanchorshead);
328130617Smlaier
329126353Smlaierstruct loadanchors {
330126353Smlaier	TAILQ_ENTRY(loadanchors)	 entries;
331126353Smlaier	char				*anchorname;
332126353Smlaier	char				*filename;
333126353Smlaier};
334126353Smlaier
335126353Smlaiertypedef struct {
336126353Smlaier	union {
337126353Smlaier		u_int32_t		 number;
338126353Smlaier		int			 i;
339126353Smlaier		char			*string;
340171172Smlaier		int			 rtableid;
341126353Smlaier		struct {
342126353Smlaier			u_int8_t	 b1;
343126353Smlaier			u_int8_t	 b2;
344126353Smlaier			u_int16_t	 w;
345126353Smlaier			u_int16_t	 w2;
346126353Smlaier		}			 b;
347126353Smlaier		struct range {
348126353Smlaier			int		 a;
349126353Smlaier			int		 b;
350126353Smlaier			int		 t;
351126353Smlaier		}			 range;
352126353Smlaier		struct node_if		*interface;
353126353Smlaier		struct node_proto	*proto;
354126353Smlaier		struct node_icmp	*icmp;
355126353Smlaier		struct node_host	*host;
356126353Smlaier		struct node_os		*os;
357126353Smlaier		struct node_port	*port;
358126353Smlaier		struct node_uid		*uid;
359126353Smlaier		struct node_gid		*gid;
360126353Smlaier		struct node_state_opt	*state_opt;
361126353Smlaier		struct peer		 peer;
362126353Smlaier		struct {
363126353Smlaier			struct peer	 src, dst;
364126353Smlaier			struct node_os	*src_os;
365126353Smlaier		}			 fromto;
366126353Smlaier		struct {
367126353Smlaier			struct node_host	*host;
368126353Smlaier			u_int8_t		 rt;
369126353Smlaier			u_int8_t		 pool_opts;
370126353Smlaier			sa_family_t		 af;
371126353Smlaier			struct pf_poolhashkey	*key;
372126353Smlaier		}			 route;
373126353Smlaier		struct redirection {
374126353Smlaier			struct node_host	*host;
375126353Smlaier			struct range		 rport;
376126353Smlaier		}			*redirection;
377126353Smlaier		struct {
378126353Smlaier			int			 action;
379126353Smlaier			struct node_state_opt	*options;
380126353Smlaier		}			 keep_state;
381126353Smlaier		struct {
382126353Smlaier			u_int8_t	 log;
383171172Smlaier			u_int8_t	 logif;
384126353Smlaier			u_int8_t	 quick;
385126353Smlaier		}			 logquick;
386145840Smlaier		struct {
387145840Smlaier			int		 neg;
388145840Smlaier			char		*name;
389145840Smlaier		}			 tagged;
390130617Smlaier		struct pf_poolhashkey	*hashkey;
391126353Smlaier		struct node_queue	*queue;
392126353Smlaier		struct node_queue_opt	 queue_options;
393126353Smlaier		struct node_queue_bw	 queue_bwspec;
394126353Smlaier		struct node_qassign	 qassign;
395126353Smlaier		struct filter_opts	 filter_opts;
396126353Smlaier		struct antispoof_opts	 antispoof_opts;
397126353Smlaier		struct queue_opts	 queue_opts;
398126353Smlaier		struct scrub_opts	 scrub_opts;
399126353Smlaier		struct table_opts	 table_opts;
400130617Smlaier		struct pool_opts	 pool_opts;
401126353Smlaier		struct node_hfsc_opts	 hfsc_opts;
402126353Smlaier	} v;
403126353Smlaier	int lineno;
404126353Smlaier} YYSTYPE;
405126353Smlaier
406130617Smlaier#define DYNIF_MULTIADDR(addr) ((addr).type == PF_ADDR_DYNIFTL && \
407130617Smlaier	(!((addr).iflags & PFI_AFLAG_NOALIAS) ||		 \
408130617Smlaier	!isdigit((addr).v.ifname[strlen((addr).v.ifname)-1])))
409130617Smlaier
410126353Smlaier%}
411126353Smlaier
412171172Smlaier%token	PASS BLOCK SCRUB RETURN IN OS OUT LOG QUICK ON FROM TO FLAGS
413126353Smlaier%token	RETURNRST RETURNICMP RETURNICMP6 PROTO INET INET6 ALL ANY ICMPTYPE
414126353Smlaier%token	ICMP6TYPE CODE KEEP MODULATE STATE PORT RDR NAT BINAT ARROW NODF
415126353Smlaier%token	MINTTL ERROR ALLOWOPTS FASTROUTE FILENAME ROUTETO DUPTO REPLYTO NO LABEL
416171172Smlaier%token	NOROUTE URPFFAILED FRAGMENT USER GROUP MAXMSS MAXIMUM TTL TOS DROP TABLE
417126353Smlaier%token	REASSEMBLE FRAGDROP FRAGCROP ANCHOR NATANCHOR RDRANCHOR BINATANCHOR
418126353Smlaier%token	SET OPTIMIZATION TIMEOUT LIMIT LOGINTERFACE BLOCKPOLICY RANDOMID
419145840Smlaier%token	REQUIREORDER SYNPROXY FINGERPRINTS NOSYNC DEBUG SKIP HOSTID
420126353Smlaier%token	ANTISPOOF FOR
421145840Smlaier%token	BITMASK RANDOM SOURCEHASH ROUNDROBIN STATICPORT PROBABILITY
422126353Smlaier%token	ALTQ CBQ PRIQ HFSC BANDWIDTH TBRSIZE LINKSHARE REALTIME UPPERLIMIT
423171172Smlaier%token	QUEUE PRIORITY QLIMIT RTABLE
424171172Smlaier%token	LOAD RULESET_OPTIMIZATION
425130617Smlaier%token	STICKYADDRESS MAXSRCSTATES MAXSRCNODES SOURCETRACK GLOBAL RULE
426145840Smlaier%token	MAXSRCCONN MAXSRCCONNRATE OVERLOAD FLUSH
427171172Smlaier%token	TAGGED TAG IFBOUND FLOATING STATEPOLICY ROUTE
428126353Smlaier%token	<v.string>		STRING
429126353Smlaier%token	<v.i>			PORTBINARY
430126353Smlaier%type	<v.interface>		interface if_list if_item_not if_item
431126353Smlaier%type	<v.number>		number icmptype icmp6type uid gid
432171172Smlaier%type	<v.number>		tos not yesno
433171172Smlaier%type	<v.i>			no dir af fragcache optimizer
434171172Smlaier%type	<v.i>			sourcetrack flush unaryop statelock
435171172Smlaier%type	<v.b>			action nataction natpass scrubaction
436145840Smlaier%type	<v.b>			flags flag blockspec
437126353Smlaier%type	<v.range>		port rport
438126353Smlaier%type	<v.hashkey>		hashkey
439126353Smlaier%type	<v.proto>		proto proto_list proto_item
440126353Smlaier%type	<v.icmp>		icmpspec
441126353Smlaier%type	<v.icmp>		icmp_list icmp_item
442126353Smlaier%type	<v.icmp>		icmp6_list icmp6_item
443126353Smlaier%type	<v.fromto>		fromto
444126353Smlaier%type	<v.peer>		ipportspec from to
445126353Smlaier%type	<v.host>		ipspec xhost host dynaddr host_list
446126353Smlaier%type	<v.host>		redir_host_list redirspec
447126353Smlaier%type	<v.host>		route_host route_host_list routespec
448126353Smlaier%type	<v.os>			os xos os_list
449126353Smlaier%type	<v.port>		portspec port_list port_item
450126353Smlaier%type	<v.uid>			uids uid_list uid_item
451126353Smlaier%type	<v.gid>			gids gid_list gid_item
452126353Smlaier%type	<v.route>		route
453126353Smlaier%type	<v.redirection>		redirection redirpool
454171172Smlaier%type	<v.string>		label string tag anchorname
455126353Smlaier%type	<v.keep_state>		keep
456126353Smlaier%type	<v.state_opt>		state_opt_spec state_opt_list state_opt_item
457171172Smlaier%type	<v.logquick>		logquick quick log logopts logopt
458145840Smlaier%type	<v.interface>		antispoof_ifspc antispoof_iflst antispoof_if
459126353Smlaier%type	<v.qassign>		qname
460126353Smlaier%type	<v.queue>		qassign qassign_list qassign_item
461126353Smlaier%type	<v.queue_options>	scheduler
462126353Smlaier%type	<v.number>		cbqflags_list cbqflags_item
463126353Smlaier%type	<v.number>		priqflags_list priqflags_item
464126353Smlaier%type	<v.hfsc_opts>		hfscopts_list hfscopts_item hfsc_opts
465126353Smlaier%type	<v.queue_bwspec>	bandwidth
466126353Smlaier%type	<v.filter_opts>		filter_opts filter_opt filter_opts_l
467126353Smlaier%type	<v.antispoof_opts>	antispoof_opts antispoof_opt antispoof_opts_l
468126353Smlaier%type	<v.queue_opts>		queue_opts queue_opt queue_opts_l
469126353Smlaier%type	<v.scrub_opts>		scrub_opts scrub_opt scrub_opts_l
470126353Smlaier%type	<v.table_opts>		table_opts table_opt table_opts_l
471130617Smlaier%type	<v.pool_opts>		pool_opts pool_opt pool_opts_l
472145840Smlaier%type	<v.tagged>		tagged
473171172Smlaier%type	<v.rtableid>		rtable
474126353Smlaier%%
475126353Smlaier
476126353Smlaierruleset		: /* empty */
477126353Smlaier		| ruleset '\n'
478126353Smlaier		| ruleset option '\n'
479126353Smlaier		| ruleset scrubrule '\n'
480126353Smlaier		| ruleset natrule '\n'
481126353Smlaier		| ruleset binatrule '\n'
482126353Smlaier		| ruleset pfrule '\n'
483126353Smlaier		| ruleset anchorrule '\n'
484126353Smlaier		| ruleset loadrule '\n'
485126353Smlaier		| ruleset altqif '\n'
486126353Smlaier		| ruleset queuespec '\n'
487126353Smlaier		| ruleset varset '\n'
488126353Smlaier		| ruleset antispoof '\n'
489126353Smlaier		| ruleset tabledef '\n'
490171172Smlaier		| '{' fakeanchor '}' '\n';
491126353Smlaier		| ruleset error '\n'		{ errors++; }
492126353Smlaier		;
493126353Smlaier
494171172Smlaier/*
495171172Smlaier * apply to previouslys specified rule: must be careful to note
496171172Smlaier * what that is: pf or nat or binat or rdr
497171172Smlaier */
498171172Smlaierfakeanchor	: fakeanchor '\n'
499171172Smlaier		| fakeanchor anchorrule '\n'
500171172Smlaier		| fakeanchor binatrule '\n'
501171172Smlaier		| fakeanchor natrule '\n'
502171172Smlaier		| fakeanchor pfrule '\n'
503171172Smlaier		| fakeanchor error '\n'
504171172Smlaier		;
505171172Smlaier
506171172Smlaieroptimizer	: string	{
507171172Smlaier			if (!strcmp($1, "none"))
508171172Smlaier				$$ = 0;
509171172Smlaier			else if (!strcmp($1, "basic"))
510171172Smlaier				$$ = PF_OPTIMIZE_BASIC;
511171172Smlaier			else if (!strcmp($1, "profile"))
512171172Smlaier				$$ = PF_OPTIMIZE_BASIC | PF_OPTIMIZE_PROFILE;
513171172Smlaier			else {
514171172Smlaier				yyerror("unknown ruleset-optimization %s", $$);
515171172Smlaier				YYERROR;
516171172Smlaier			}
517171172Smlaier		}
518171172Smlaier		;
519171172Smlaier
520126353Smlaieroption		: SET OPTIMIZATION STRING		{
521130617Smlaier			if (check_rulestate(PFCTL_STATE_OPTION)) {
522130617Smlaier				free($3);
523126353Smlaier				YYERROR;
524130617Smlaier			}
525126353Smlaier			if (pfctl_set_optimization(pf, $3) != 0) {
526126353Smlaier				yyerror("unknown optimization %s", $3);
527130617Smlaier				free($3);
528126353Smlaier				YYERROR;
529126353Smlaier			}
530171172Smlaier			free($3);
531126353Smlaier		}
532171172Smlaier		| SET RULESET_OPTIMIZATION optimizer {
533171172Smlaier			if (!(pf->opts & PF_OPT_OPTIMIZE)) {
534171172Smlaier				pf->opts |= PF_OPT_OPTIMIZE;
535171172Smlaier				pf->optimize = $3;
536171172Smlaier			}
537171172Smlaier		}
538126353Smlaier		| SET TIMEOUT timeout_spec
539126353Smlaier		| SET TIMEOUT '{' timeout_list '}'
540126353Smlaier		| SET LIMIT limit_spec
541126353Smlaier		| SET LIMIT '{' limit_list '}'
542126353Smlaier		| SET LOGINTERFACE STRING		{
543130617Smlaier			if (check_rulestate(PFCTL_STATE_OPTION)) {
544130617Smlaier				free($3);
545126353Smlaier				YYERROR;
546130617Smlaier			}
547126353Smlaier			if (pfctl_set_logif(pf, $3) != 0) {
548126353Smlaier				yyerror("error setting loginterface %s", $3);
549130617Smlaier				free($3);
550126353Smlaier				YYERROR;
551126353Smlaier			}
552130617Smlaier			free($3);
553126353Smlaier		}
554130617Smlaier		| SET HOSTID number {
555130617Smlaier			if ($3 == 0) {
556130617Smlaier				yyerror("hostid must be non-zero");
557130617Smlaier				YYERROR;
558130617Smlaier			}
559130617Smlaier			if (pfctl_set_hostid(pf, $3) != 0) {
560145840Smlaier				yyerror("error setting hostid %08x", $3);
561130617Smlaier				YYERROR;
562130617Smlaier			}
563130617Smlaier		}
564126353Smlaier		| SET BLOCKPOLICY DROP	{
565126353Smlaier			if (pf->opts & PF_OPT_VERBOSE)
566126353Smlaier				printf("set block-policy drop\n");
567126353Smlaier			if (check_rulestate(PFCTL_STATE_OPTION))
568126353Smlaier				YYERROR;
569126353Smlaier			blockpolicy = PFRULE_DROP;
570126353Smlaier		}
571126353Smlaier		| SET BLOCKPOLICY RETURN {
572126353Smlaier			if (pf->opts & PF_OPT_VERBOSE)
573126353Smlaier				printf("set block-policy return\n");
574126353Smlaier			if (check_rulestate(PFCTL_STATE_OPTION))
575126353Smlaier				YYERROR;
576126353Smlaier			blockpolicy = PFRULE_RETURN;
577126353Smlaier		}
578126353Smlaier		| SET REQUIREORDER yesno {
579126353Smlaier			if (pf->opts & PF_OPT_VERBOSE)
580126353Smlaier				printf("set require-order %s\n",
581126353Smlaier				    $3 == 1 ? "yes" : "no");
582126353Smlaier			require_order = $3;
583126353Smlaier		}
584126353Smlaier		| SET FINGERPRINTS STRING {
585126353Smlaier			if (pf->opts & PF_OPT_VERBOSE)
586171172Smlaier				printf("set fingerprints \"%s\"\n", $3);
587130617Smlaier			if (check_rulestate(PFCTL_STATE_OPTION)) {
588130617Smlaier				free($3);
589126353Smlaier				YYERROR;
590130617Smlaier			}
591171172Smlaier			if (!pf->anchor->name[0]) {
592145840Smlaier				if (pfctl_file_fingerprints(pf->dev,
593145840Smlaier				    pf->opts, $3)) {
594145840Smlaier					yyerror("error loading "
595145840Smlaier					    "fingerprints %s", $3);
596145840Smlaier					free($3);
597145840Smlaier					YYERROR;
598145840Smlaier				}
599126353Smlaier			}
600130617Smlaier			free($3);
601126353Smlaier		}
602130617Smlaier		| SET STATEPOLICY statelock {
603130617Smlaier			if (pf->opts & PF_OPT_VERBOSE)
604130617Smlaier				switch ($3) {
605130617Smlaier				case 0:
606130617Smlaier					printf("set state-policy floating\n");
607130617Smlaier					break;
608130617Smlaier				case PFRULE_IFBOUND:
609130617Smlaier					printf("set state-policy if-bound\n");
610130617Smlaier					break;
611130617Smlaier				}
612130617Smlaier			default_statelock = $3;
613130617Smlaier		}
614130617Smlaier		| SET DEBUG STRING {
615130617Smlaier			if (check_rulestate(PFCTL_STATE_OPTION)) {
616130617Smlaier				free($3);
617130617Smlaier				YYERROR;
618130617Smlaier			}
619130617Smlaier			if (pfctl_set_debug(pf, $3) != 0) {
620130617Smlaier				yyerror("error setting debuglevel %s", $3);
621130617Smlaier				free($3);
622130617Smlaier				YYERROR;
623130617Smlaier			}
624130617Smlaier			free($3);
625130617Smlaier		}
626145840Smlaier		| SET SKIP interface {
627145840Smlaier			if (expand_skip_interface($3) != 0) {
628145840Smlaier				yyerror("error setting skip interface(s)");
629145840Smlaier				YYERROR;
630145840Smlaier			}
631145840Smlaier		}
632126353Smlaier		;
633126353Smlaier
634126353Smlaierstring		: string STRING				{
635126353Smlaier			if (asprintf(&$$, "%s %s", $1, $2) == -1)
636126353Smlaier				err(1, "string: asprintf");
637126353Smlaier			free($1);
638126353Smlaier			free($2);
639126353Smlaier		}
640126353Smlaier		| STRING
641126353Smlaier		;
642126353Smlaier
643126353Smlaiervarset		: STRING '=' string		{
644126353Smlaier			if (pf->opts & PF_OPT_VERBOSE)
645126353Smlaier				printf("%s = \"%s\"\n", $1, $3);
646126353Smlaier			if (symset($1, $3, 0) == -1)
647126353Smlaier				err(1, "cannot store variable %s", $1);
648130617Smlaier			free($1);
649130617Smlaier			free($3);
650126353Smlaier		}
651126353Smlaier		;
652126353Smlaier
653171172Smlaieranchorname	: STRING			{ $$ = $1; }
654171172Smlaier		| /* empty */			{ $$ = NULL; }
655171172Smlaier		;
656171172Smlaier
657171172Smlaieroptnl		: optnl '\n'
658171172Smlaier		|
659171172Smlaier		;
660171172Smlaier
661171172Smlaierpfa_anchorlist	: pfrule optnl
662171172Smlaier		| anchorrule optnl
663171172Smlaier		| pfa_anchorlist pfrule optnl
664171172Smlaier		| pfa_anchorlist anchorrule optnl
665171172Smlaier		;
666171172Smlaier
667171172Smlaierpfa_anchor	: '{'
668171172Smlaier		{
669171172Smlaier			char ta[PF_ANCHOR_NAME_SIZE];
670171172Smlaier			struct pf_ruleset *rs;
671171172Smlaier
672171172Smlaier			/* steping into a brace anchor */
673171172Smlaier			pf->asd++;
674171172Smlaier			pf->bn++;
675171172Smlaier			pf->brace = 1;
676171172Smlaier
677171172Smlaier			/* create a holding ruleset in the root */
678171172Smlaier			snprintf(ta, PF_ANCHOR_NAME_SIZE, "_%d", pf->bn);
679171172Smlaier			rs = pf_find_or_create_ruleset(ta);
680171172Smlaier			if (rs == NULL)
681171172Smlaier				err(1, "pfa_anchor: pf_find_or_create_ruleset");
682171172Smlaier			pf->astack[pf->asd] = rs->anchor;
683171172Smlaier			pf->anchor = rs->anchor;
684171172Smlaier		} '\n' pfa_anchorlist '}'
685171172Smlaier		{
686171172Smlaier			pf->alast = pf->anchor;
687171172Smlaier			pf->asd--;
688171172Smlaier			pf->anchor = pf->astack[pf->asd];
689171172Smlaier		}
690171172Smlaier		| /* empty */
691171172Smlaier		;
692171172Smlaier
693171172Smlaieranchorrule	: ANCHOR anchorname dir quick interface af proto fromto
694171172Smlaier		    filter_opts pfa_anchor
695171172Smlaier		{
696126353Smlaier			struct pf_rule	r;
697126353Smlaier
698130617Smlaier			if (check_rulestate(PFCTL_STATE_FILTER)) {
699171172Smlaier				if ($2)
700171172Smlaier					free($2);
701171172Smlaier				YYERROR;
702171172Smlaier			}
703171172Smlaier
704171172Smlaier			if ($2 && ($2[0] == '_' || strstr($2, "/_") != NULL)) {
705130617Smlaier				free($2);
706171172Smlaier				yyerror("anchor names beginning with '_' "
707171172Smlaier				    "are reserved for internal use");
708126353Smlaier				YYERROR;
709130617Smlaier			}
710126353Smlaier
711145840Smlaier			memset(&r, 0, sizeof(r));
712171172Smlaier			if (pf->astack[pf->asd + 1]) {
713171172Smlaier				/* move inline rules into relative location */
714171172Smlaier				pf_anchor_setup(&r,
715171172Smlaier				    &pf->astack[pf->asd]->ruleset,
716171172Smlaier				    $2 ? $2 : pf->alast->name);
717171172Smlaier
718171172Smlaier				if (r.anchor == NULL)
719171172Smlaier					err(1, "anchorrule: unable to "
720171172Smlaier					    "create ruleset");
721171172Smlaier
722171172Smlaier				if (pf->alast != r.anchor) {
723171172Smlaier					if (r.anchor->match) {
724171172Smlaier						yyerror("inline anchor '%s' "
725171172Smlaier						    "already exists",
726171172Smlaier						    r.anchor->name);
727171172Smlaier						YYERROR;
728171172Smlaier					}
729171172Smlaier					mv_rules(&pf->alast->ruleset,
730171172Smlaier					    &r.anchor->ruleset);
731171172Smlaier				}
732171172Smlaier				pf_remove_if_empty_ruleset(&pf->alast->ruleset);
733171172Smlaier				pf->alast = r.anchor;
734171172Smlaier			} else {
735171172Smlaier				if (!$2) {
736171172Smlaier					yyerror("anchors without explicit "
737171172Smlaier					    "rules must specify a name");
738171172Smlaier					YYERROR;
739171172Smlaier				}
740171172Smlaier			}
741126353Smlaier			r.direction = $3;
742171172Smlaier			r.quick = $4.quick;
743171172Smlaier			r.af = $6;
744171172Smlaier			r.prob = $9.prob;
745171172Smlaier			r.rtableid = $9.rtableid;
746126353Smlaier
747171172Smlaier			if ($9.match_tag)
748171172Smlaier				if (strlcpy(r.match_tagname, $9.match_tag,
749130617Smlaier				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
750130617Smlaier					yyerror("tag too long, max %u chars",
751130617Smlaier					    PF_TAG_NAME_SIZE - 1);
752130617Smlaier					YYERROR;
753130617Smlaier				}
754171172Smlaier			r.match_tag_not = $9.match_tag_not;
755130617Smlaier
756171172Smlaier			decide_address_family($8.src.host, &r.af);
757171172Smlaier			decide_address_family($8.dst.host, &r.af);
758126353Smlaier
759171172Smlaier			expand_rule(&r, $5, NULL, $7, $8.src_os,
760171172Smlaier			    $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
761171172Smlaier			    0, 0, 0, pf->astack[pf->asd + 1] ?
762171172Smlaier			    pf->alast->name : $2);
763145840Smlaier			free($2);
764171172Smlaier			pf->astack[pf->asd + 1] = NULL;
765126353Smlaier		}
766171172Smlaier		| NATANCHOR string interface af proto fromto rtable {
767126353Smlaier			struct pf_rule	r;
768126353Smlaier
769130617Smlaier			if (check_rulestate(PFCTL_STATE_NAT)) {
770130617Smlaier				free($2);
771126353Smlaier				YYERROR;
772130617Smlaier			}
773126353Smlaier
774145840Smlaier			memset(&r, 0, sizeof(r));
775126353Smlaier			r.action = PF_NAT;
776126353Smlaier			r.af = $4;
777171172Smlaier			r.rtableid = $7;
778126353Smlaier
779126353Smlaier			decide_address_family($6.src.host, &r.af);
780126353Smlaier			decide_address_family($6.dst.host, &r.af);
781126353Smlaier
782126353Smlaier			expand_rule(&r, $3, NULL, $5, $6.src_os,
783126353Smlaier			    $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
784145840Smlaier			    0, 0, 0, $2);
785145840Smlaier			free($2);
786126353Smlaier		}
787171172Smlaier		| RDRANCHOR string interface af proto fromto rtable {
788126353Smlaier			struct pf_rule	r;
789126353Smlaier
790130617Smlaier			if (check_rulestate(PFCTL_STATE_NAT)) {
791130617Smlaier				free($2);
792126353Smlaier				YYERROR;
793130617Smlaier			}
794126353Smlaier
795145840Smlaier			memset(&r, 0, sizeof(r));
796126353Smlaier			r.action = PF_RDR;
797126353Smlaier			r.af = $4;
798171172Smlaier			r.rtableid = $7;
799126353Smlaier
800126353Smlaier			decide_address_family($6.src.host, &r.af);
801126353Smlaier			decide_address_family($6.dst.host, &r.af);
802126353Smlaier
803126353Smlaier			if ($6.src.port != NULL) {
804126353Smlaier				yyerror("source port parameter not supported"
805126353Smlaier				    " in rdr-anchor");
806126353Smlaier				YYERROR;
807126353Smlaier			}
808126353Smlaier			if ($6.dst.port != NULL) {
809126353Smlaier				if ($6.dst.port->next != NULL) {
810126353Smlaier					yyerror("destination port list "
811126353Smlaier					    "expansion not supported in "
812126353Smlaier					    "rdr-anchor");
813126353Smlaier					YYERROR;
814126353Smlaier				} else if ($6.dst.port->op != PF_OP_EQ) {
815126353Smlaier					yyerror("destination port operators"
816126353Smlaier					    " not supported in rdr-anchor");
817126353Smlaier					YYERROR;
818126353Smlaier				}
819126353Smlaier				r.dst.port[0] = $6.dst.port->port[0];
820126353Smlaier				r.dst.port[1] = $6.dst.port->port[1];
821126353Smlaier				r.dst.port_op = $6.dst.port->op;
822126353Smlaier			}
823126353Smlaier
824126353Smlaier			expand_rule(&r, $3, NULL, $5, $6.src_os,
825126353Smlaier			    $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
826145840Smlaier			    0, 0, 0, $2);
827145840Smlaier			free($2);
828126353Smlaier		}
829171172Smlaier		| BINATANCHOR string interface af proto fromto rtable {
830126353Smlaier			struct pf_rule	r;
831126353Smlaier
832130617Smlaier			if (check_rulestate(PFCTL_STATE_NAT)) {
833130617Smlaier				free($2);
834126353Smlaier				YYERROR;
835130617Smlaier			}
836126353Smlaier
837145840Smlaier			memset(&r, 0, sizeof(r));
838126353Smlaier			r.action = PF_BINAT;
839126353Smlaier			r.af = $4;
840171172Smlaier			r.rtableid = $7;
841126353Smlaier			if ($5 != NULL) {
842126353Smlaier				if ($5->next != NULL) {
843126353Smlaier					yyerror("proto list expansion"
844126353Smlaier					    " not supported in binat-anchor");
845126353Smlaier					YYERROR;
846126353Smlaier				}
847126353Smlaier				r.proto = $5->proto;
848126353Smlaier				free($5);
849126353Smlaier			}
850126353Smlaier
851126353Smlaier			if ($6.src.host != NULL || $6.src.port != NULL ||
852126353Smlaier			    $6.dst.host != NULL || $6.dst.port != NULL) {
853126353Smlaier				yyerror("fromto parameter not supported"
854126353Smlaier				    " in binat-anchor");
855126353Smlaier				YYERROR;
856126353Smlaier			}
857126353Smlaier
858126353Smlaier			decide_address_family($6.src.host, &r.af);
859126353Smlaier			decide_address_family($6.dst.host, &r.af);
860126353Smlaier
861145840Smlaier			pfctl_add_rule(pf, &r, $2);
862145840Smlaier			free($2);
863126353Smlaier		}
864126353Smlaier		;
865126353Smlaier
866126353Smlaierloadrule	: LOAD ANCHOR string FROM string	{
867126353Smlaier			struct loadanchors	*loadanchor;
868126353Smlaier
869171172Smlaier			if (strlen(pf->anchor->name) + 1 +
870171172Smlaier			    strlen($3) >= MAXPATHLEN) {
871126353Smlaier				yyerror("anchorname %s too long, max %u\n",
872145840Smlaier				    $3, MAXPATHLEN - 1);
873145840Smlaier				free($3);
874126353Smlaier				YYERROR;
875126353Smlaier			}
876126353Smlaier			loadanchor = calloc(1, sizeof(struct loadanchors));
877126353Smlaier			if (loadanchor == NULL)
878126353Smlaier				err(1, "loadrule: calloc");
879171172Smlaier			if ((loadanchor->anchorname = malloc(MAXPATHLEN)) ==
880171172Smlaier			    NULL)
881171172Smlaier				err(1, "loadrule: malloc");
882171172Smlaier			if (pf->anchor->name[0])
883171172Smlaier				snprintf(loadanchor->anchorname, MAXPATHLEN,
884171172Smlaier				    "%s/%s", pf->anchor->name, $3);
885171172Smlaier			else
886171172Smlaier				strlcpy(loadanchor->anchorname, $3, MAXPATHLEN);
887126353Smlaier			if ((loadanchor->filename = strdup($5)) == NULL)
888126353Smlaier				err(1, "loadrule: strdup");
889126353Smlaier
890126353Smlaier			TAILQ_INSERT_TAIL(&loadanchorshead, loadanchor,
891126353Smlaier			    entries);
892126353Smlaier
893145840Smlaier			free($3);
894126353Smlaier			free($5);
895126353Smlaier		};
896126353Smlaier
897145840Smlaierscrubaction	: no SCRUB {
898145840Smlaier			$$.b2 = $$.w = 0;
899145840Smlaier			if ($1)
900145840Smlaier				$$.b1 = PF_NOSCRUB;
901145840Smlaier			else
902145840Smlaier				$$.b1 = PF_SCRUB;
903145840Smlaier		}
904145840Smlaier		;
905145840Smlaier
906145840Smlaierscrubrule	: scrubaction dir logquick interface af proto fromto scrub_opts
907126353Smlaier		{
908126353Smlaier			struct pf_rule	r;
909126353Smlaier
910126353Smlaier			if (check_rulestate(PFCTL_STATE_SCRUB))
911126353Smlaier				YYERROR;
912126353Smlaier
913126353Smlaier			memset(&r, 0, sizeof(r));
914126353Smlaier
915145840Smlaier			r.action = $1.b1;
916126353Smlaier			r.direction = $2;
917126353Smlaier
918126353Smlaier			r.log = $3.log;
919171172Smlaier			r.logif = $3.logif;
920126353Smlaier			if ($3.quick) {
921126353Smlaier				yyerror("scrub rules do not support 'quick'");
922126353Smlaier				YYERROR;
923126353Smlaier			}
924126353Smlaier
925126353Smlaier			r.af = $5;
926126353Smlaier			if ($8.nodf)
927126353Smlaier				r.rule_flag |= PFRULE_NODF;
928126353Smlaier			if ($8.randomid)
929126353Smlaier				r.rule_flag |= PFRULE_RANDOMID;
930126353Smlaier			if ($8.reassemble_tcp) {
931126353Smlaier				if (r.direction != PF_INOUT) {
932126353Smlaier					yyerror("reassemble tcp rules can not "
933126353Smlaier					    "specify direction");
934126353Smlaier					YYERROR;
935126353Smlaier				}
936126353Smlaier				r.rule_flag |= PFRULE_REASSEMBLE_TCP;
937126353Smlaier			}
938126353Smlaier			if ($8.minttl)
939126353Smlaier				r.min_ttl = $8.minttl;
940126353Smlaier			if ($8.maxmss)
941126353Smlaier				r.max_mss = $8.maxmss;
942126353Smlaier			if ($8.fragcache)
943126353Smlaier				r.rule_flag |= $8.fragcache;
944171172Smlaier			r.rtableid = $8.rtableid;
945126353Smlaier
946126353Smlaier			expand_rule(&r, $4, NULL, $6, $7.src_os,
947126353Smlaier			    $7.src.host, $7.src.port, $7.dst.host, $7.dst.port,
948145840Smlaier			    NULL, NULL, NULL, "");
949126353Smlaier		}
950126353Smlaier		;
951126353Smlaier
952126353Smlaierscrub_opts	:	{
953171172Smlaier				bzero(&scrub_opts, sizeof scrub_opts);
954171172Smlaier				scrub_opts.rtableid = -1;
955171172Smlaier			}
956130617Smlaier		    scrub_opts_l
957126353Smlaier			{ $$ = scrub_opts; }
958126353Smlaier		| /* empty */ {
959126353Smlaier			bzero(&scrub_opts, sizeof scrub_opts);
960171172Smlaier			scrub_opts.rtableid = -1;
961126353Smlaier			$$ = scrub_opts;
962126353Smlaier		}
963126353Smlaier		;
964126353Smlaier
965126353Smlaierscrub_opts_l	: scrub_opts_l scrub_opt
966126353Smlaier		| scrub_opt
967126353Smlaier		;
968126353Smlaier
969126353Smlaierscrub_opt	: NODF	{
970126353Smlaier			if (scrub_opts.nodf) {
971126353Smlaier				yyerror("no-df cannot be respecified");
972126353Smlaier				YYERROR;
973126353Smlaier			}
974126353Smlaier			scrub_opts.nodf = 1;
975126353Smlaier		}
976126353Smlaier		| MINTTL number {
977126353Smlaier			if (scrub_opts.marker & SOM_MINTTL) {
978126353Smlaier				yyerror("min-ttl cannot be respecified");
979126353Smlaier				YYERROR;
980126353Smlaier			}
981126353Smlaier			if ($2 > 255) {
982126353Smlaier				yyerror("illegal min-ttl value %d", $2);
983126353Smlaier				YYERROR;
984126353Smlaier			}
985126353Smlaier			scrub_opts.marker |= SOM_MINTTL;
986126353Smlaier			scrub_opts.minttl = $2;
987126353Smlaier		}
988126353Smlaier		| MAXMSS number {
989126353Smlaier			if (scrub_opts.marker & SOM_MAXMSS) {
990126353Smlaier				yyerror("max-mss cannot be respecified");
991126353Smlaier				YYERROR;
992126353Smlaier			}
993126353Smlaier			if ($2 > 65535) {
994126353Smlaier				yyerror("illegal max-mss value %d", $2);
995126353Smlaier				YYERROR;
996126353Smlaier			}
997126353Smlaier			scrub_opts.marker |= SOM_MAXMSS;
998126353Smlaier			scrub_opts.maxmss = $2;
999126353Smlaier		}
1000126353Smlaier		| fragcache {
1001126353Smlaier			if (scrub_opts.marker & SOM_FRAGCACHE) {
1002126353Smlaier				yyerror("fragcache cannot be respecified");
1003126353Smlaier				YYERROR;
1004126353Smlaier			}
1005126353Smlaier			scrub_opts.marker |= SOM_FRAGCACHE;
1006126353Smlaier			scrub_opts.fragcache = $1;
1007126353Smlaier		}
1008126353Smlaier		| REASSEMBLE STRING {
1009130617Smlaier			if (strcasecmp($2, "tcp") != 0) {
1010145840Smlaier				yyerror("scrub reassemble supports only tcp, "
1011145840Smlaier				    "not '%s'", $2);
1012130617Smlaier				free($2);
1013126353Smlaier				YYERROR;
1014130617Smlaier			}
1015130617Smlaier			free($2);
1016126353Smlaier			if (scrub_opts.reassemble_tcp) {
1017126353Smlaier				yyerror("reassemble tcp cannot be respecified");
1018126353Smlaier				YYERROR;
1019126353Smlaier			}
1020126353Smlaier			scrub_opts.reassemble_tcp = 1;
1021126353Smlaier		}
1022126353Smlaier		| RANDOMID {
1023126353Smlaier			if (scrub_opts.randomid) {
1024126353Smlaier				yyerror("random-id cannot be respecified");
1025126353Smlaier				YYERROR;
1026126353Smlaier			}
1027126353Smlaier			scrub_opts.randomid = 1;
1028126353Smlaier		}
1029171172Smlaier		| RTABLE number				{
1030178894Sjulian#ifndef __FreeBSD__
1031171172Smlaier			if ($2 > RT_TABLEID_MAX || $2 < 0) {
1032171172Smlaier				yyerror("invalid rtable id");
1033171172Smlaier				YYERROR;
1034171172Smlaier			}
1035178894Sjulian#endif
1036171172Smlaier			scrub_opts.rtableid = $2;
1037171172Smlaier		}
1038126353Smlaier		;
1039126353Smlaier
1040126353Smlaierfragcache	: FRAGMENT REASSEMBLE	{ $$ = 0; /* default */ }
1041126353Smlaier		| FRAGMENT FRAGCROP	{ $$ = PFRULE_FRAGCROP; }
1042126353Smlaier		| FRAGMENT FRAGDROP	{ $$ = PFRULE_FRAGDROP; }
1043126353Smlaier		;
1044126353Smlaier
1045126353Smlaierantispoof	: ANTISPOOF logquick antispoof_ifspc af antispoof_opts {
1046126353Smlaier			struct pf_rule		 r;
1047145840Smlaier			struct node_host	*h = NULL, *hh;
1048126353Smlaier			struct node_if		*i, *j;
1049126353Smlaier
1050126353Smlaier			if (check_rulestate(PFCTL_STATE_FILTER))
1051126353Smlaier				YYERROR;
1052126353Smlaier
1053126353Smlaier			for (i = $3; i; i = i->next) {
1054126353Smlaier				bzero(&r, sizeof(r));
1055126353Smlaier
1056126353Smlaier				r.action = PF_DROP;
1057126353Smlaier				r.direction = PF_IN;
1058126353Smlaier				r.log = $2.log;
1059171172Smlaier				r.logif = $2.logif;
1060126353Smlaier				r.quick = $2.quick;
1061126353Smlaier				r.af = $4;
1062126353Smlaier				if (rule_label(&r, $5.label))
1063126353Smlaier					YYERROR;
1064171172Smlaier				r.rtableid = $5.rtableid;
1065126353Smlaier				j = calloc(1, sizeof(struct node_if));
1066126353Smlaier				if (j == NULL)
1067126353Smlaier					err(1, "antispoof: calloc");
1068126353Smlaier				if (strlcpy(j->ifname, i->ifname,
1069126353Smlaier				    sizeof(j->ifname)) >= sizeof(j->ifname)) {
1070126353Smlaier					free(j);
1071126353Smlaier					yyerror("interface name too long");
1072126353Smlaier					YYERROR;
1073126353Smlaier				}
1074126353Smlaier				j->not = 1;
1075145840Smlaier				if (i->dynamic) {
1076145840Smlaier					h = calloc(1, sizeof(*h));
1077145840Smlaier					if (h == NULL)
1078145840Smlaier						err(1, "address: calloc");
1079145840Smlaier					h->addr.type = PF_ADDR_DYNIFTL;
1080145840Smlaier					set_ipmask(h, 128);
1081145840Smlaier					if (strlcpy(h->addr.v.ifname, i->ifname,
1082145840Smlaier					    sizeof(h->addr.v.ifname)) >=
1083145840Smlaier					    sizeof(h->addr.v.ifname)) {
1084145840Smlaier						free(h);
1085145840Smlaier						yyerror(
1086145840Smlaier						    "interface name too long");
1087145840Smlaier						YYERROR;
1088145840Smlaier					}
1089145840Smlaier					hh = malloc(sizeof(*hh));
1090145840Smlaier					if (hh == NULL)
1091145840Smlaier						 err(1, "address: malloc");
1092145840Smlaier					bcopy(h, hh, sizeof(*hh));
1093145840Smlaier					h->addr.iflags = PFI_AFLAG_NETWORK;
1094145840Smlaier				} else {
1095145840Smlaier					h = ifa_lookup(j->ifname,
1096145840Smlaier					    PFI_AFLAG_NETWORK);
1097145840Smlaier					hh = NULL;
1098145840Smlaier				}
1099126353Smlaier
1100130617Smlaier				if (h != NULL)
1101130617Smlaier					expand_rule(&r, j, NULL, NULL, NULL, h,
1102145840Smlaier					    NULL, NULL, NULL, NULL, NULL,
1103145840Smlaier					    NULL, "");
1104126353Smlaier
1105126353Smlaier				if ((i->ifa_flags & IFF_LOOPBACK) == 0) {
1106126353Smlaier					bzero(&r, sizeof(r));
1107126353Smlaier
1108126353Smlaier					r.action = PF_DROP;
1109126353Smlaier					r.direction = PF_IN;
1110126353Smlaier					r.log = $2.log;
1111126353Smlaier					r.quick = $2.quick;
1112126353Smlaier					r.af = $4;
1113126353Smlaier					if (rule_label(&r, $5.label))
1114126353Smlaier						YYERROR;
1115171172Smlaier					r.rtableid = $5.rtableid;
1116145840Smlaier					if (hh != NULL)
1117145840Smlaier						h = hh;
1118145840Smlaier					else
1119145840Smlaier						h = ifa_lookup(i->ifname, 0);
1120130617Smlaier					if (h != NULL)
1121130617Smlaier						expand_rule(&r, NULL, NULL,
1122130617Smlaier						    NULL, NULL, h, NULL, NULL,
1123145840Smlaier						    NULL, NULL, NULL, NULL, "");
1124145840Smlaier				} else
1125145840Smlaier					free(hh);
1126126353Smlaier			}
1127126353Smlaier			free($5.label);
1128126353Smlaier		}
1129126353Smlaier		;
1130126353Smlaier
1131145840Smlaierantispoof_ifspc	: FOR antispoof_if		{ $$ = $2; }
1132126353Smlaier		| FOR '{' antispoof_iflst '}'	{ $$ = $3; }
1133126353Smlaier		;
1134126353Smlaier
1135145840Smlaierantispoof_iflst	: antispoof_if				{ $$ = $1; }
1136145840Smlaier		| antispoof_iflst comma antispoof_if	{
1137126353Smlaier			$1->tail->next = $3;
1138126353Smlaier			$1->tail = $3;
1139126353Smlaier			$$ = $1;
1140126353Smlaier		}
1141126353Smlaier		;
1142126353Smlaier
1143145840Smlaierantispoof_if  : if_item				{ $$ = $1; }
1144145840Smlaier		| '(' if_item ')'		{
1145145840Smlaier			$2->dynamic = 1;
1146145840Smlaier			$$ = $2;
1147145840Smlaier		}
1148145840Smlaier		;
1149145840Smlaier
1150171172Smlaierantispoof_opts	:	{
1151171172Smlaier				bzero(&antispoof_opts, sizeof antispoof_opts);
1152171172Smlaier				antispoof_opts.rtableid = -1;
1153171172Smlaier			}
1154130617Smlaier		    antispoof_opts_l
1155126353Smlaier			{ $$ = antispoof_opts; }
1156126353Smlaier		| /* empty */	{
1157126353Smlaier			bzero(&antispoof_opts, sizeof antispoof_opts);
1158171172Smlaier			antispoof_opts.rtableid = -1;
1159126353Smlaier			$$ = antispoof_opts;
1160126353Smlaier		}
1161126353Smlaier		;
1162126353Smlaier
1163126353Smlaierantispoof_opts_l	: antispoof_opts_l antispoof_opt
1164126353Smlaier			| antispoof_opt
1165126353Smlaier			;
1166126353Smlaier
1167126353Smlaierantispoof_opt	: label	{
1168126353Smlaier			if (antispoof_opts.label) {
1169126353Smlaier				yyerror("label cannot be redefined");
1170126353Smlaier				YYERROR;
1171126353Smlaier			}
1172126353Smlaier			antispoof_opts.label = $1;
1173126353Smlaier		}
1174171172Smlaier		| RTABLE number				{
1175178894Sjulian#ifndef __FreeBSD__
1176171172Smlaier			if ($2 > RT_TABLEID_MAX || $2 < 0) {
1177171172Smlaier				yyerror("invalid rtable id");
1178171172Smlaier				YYERROR;
1179171172Smlaier			}
1180178894Sjulian#endif
1181171172Smlaier			antispoof_opts.rtableid = $2;
1182171172Smlaier		}
1183126353Smlaier		;
1184126353Smlaier
1185126353Smlaiernot		: '!'		{ $$ = 1; }
1186126353Smlaier		| /* empty */	{ $$ = 0; }
1187130617Smlaier		;
1188126353Smlaier
1189126353Smlaiertabledef	: TABLE '<' STRING '>' table_opts {
1190126353Smlaier			struct node_host	 *h, *nh;
1191126353Smlaier			struct node_tinit	 *ti, *nti;
1192126353Smlaier
1193126353Smlaier			if (strlen($3) >= PF_TABLE_NAME_SIZE) {
1194126353Smlaier				yyerror("table name too long, max %d chars",
1195126353Smlaier				    PF_TABLE_NAME_SIZE - 1);
1196130617Smlaier				free($3);
1197126353Smlaier				YYERROR;
1198126353Smlaier			}
1199126353Smlaier			if (pf->loadopt & PFCTL_FLAG_TABLE)
1200130617Smlaier				if (process_tabledef($3, &$5)) {
1201130617Smlaier					free($3);
1202126353Smlaier					YYERROR;
1203130617Smlaier				}
1204130617Smlaier			free($3);
1205126353Smlaier			for (ti = SIMPLEQ_FIRST(&$5.init_nodes);
1206126353Smlaier			    ti != SIMPLEQ_END(&$5.init_nodes); ti = nti) {
1207126353Smlaier				if (ti->file)
1208126353Smlaier					free(ti->file);
1209126353Smlaier				for (h = ti->host; h != NULL; h = nh) {
1210126353Smlaier					nh = h->next;
1211126353Smlaier					free(h);
1212126353Smlaier				}
1213126353Smlaier				nti = SIMPLEQ_NEXT(ti, entries);
1214130617Smlaier				free(ti);
1215126353Smlaier			}
1216126353Smlaier		}
1217126353Smlaier		;
1218126353Smlaier
1219126353Smlaiertable_opts	:	{
1220126353Smlaier			bzero(&table_opts, sizeof table_opts);
1221126353Smlaier			SIMPLEQ_INIT(&table_opts.init_nodes);
1222126353Smlaier		}
1223130617Smlaier		    table_opts_l
1224126353Smlaier			{ $$ = table_opts; }
1225126353Smlaier		| /* empty */
1226126353Smlaier			{
1227126353Smlaier			bzero(&table_opts, sizeof table_opts);
1228126353Smlaier			SIMPLEQ_INIT(&table_opts.init_nodes);
1229126353Smlaier			$$ = table_opts;
1230126353Smlaier		}
1231126353Smlaier		;
1232126353Smlaier
1233126353Smlaiertable_opts_l	: table_opts_l table_opt
1234126353Smlaier		| table_opt
1235126353Smlaier		;
1236126353Smlaier
1237126353Smlaiertable_opt	: STRING		{
1238126353Smlaier			if (!strcmp($1, "const"))
1239126353Smlaier				table_opts.flags |= PFR_TFLAG_CONST;
1240126353Smlaier			else if (!strcmp($1, "persist"))
1241126353Smlaier				table_opts.flags |= PFR_TFLAG_PERSIST;
1242130617Smlaier			else {
1243145840Smlaier				yyerror("invalid table option '%s'", $1);
1244130617Smlaier				free($1);
1245126353Smlaier				YYERROR;
1246130617Smlaier			}
1247130617Smlaier			free($1);
1248126353Smlaier		}
1249126353Smlaier		| '{' '}'		{ table_opts.init_addr = 1; }
1250126353Smlaier		| '{' host_list '}'	{
1251126353Smlaier			struct node_host	*n;
1252126353Smlaier			struct node_tinit	*ti;
1253126353Smlaier
1254126353Smlaier			for (n = $2; n != NULL; n = n->next) {
1255130617Smlaier				switch (n->addr.type) {
1256126353Smlaier				case PF_ADDR_ADDRMASK:
1257126353Smlaier					continue; /* ok */
1258126353Smlaier				case PF_ADDR_DYNIFTL:
1259126353Smlaier					yyerror("dynamic addresses are not "
1260126353Smlaier					    "permitted inside tables");
1261126353Smlaier					break;
1262126353Smlaier				case PF_ADDR_TABLE:
1263126353Smlaier					yyerror("tables cannot contain tables");
1264126353Smlaier					break;
1265126353Smlaier				case PF_ADDR_NOROUTE:
1266126353Smlaier					yyerror("\"no-route\" is not permitted "
1267126353Smlaier					    "inside tables");
1268126353Smlaier					break;
1269171172Smlaier				case PF_ADDR_URPFFAILED:
1270171172Smlaier					yyerror("\"urpf-failed\" is not "
1271171172Smlaier					    "permitted inside tables");
1272171172Smlaier					break;
1273126353Smlaier				default:
1274126353Smlaier					yyerror("unknown address type %d",
1275126353Smlaier					    n->addr.type);
1276126353Smlaier				}
1277126353Smlaier				YYERROR;
1278126353Smlaier			}
1279126353Smlaier			if (!(ti = calloc(1, sizeof(*ti))))
1280126353Smlaier				err(1, "table_opt: calloc");
1281126353Smlaier			ti->host = $2;
1282126353Smlaier			SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1283126353Smlaier			    entries);
1284126353Smlaier			table_opts.init_addr = 1;
1285126353Smlaier		}
1286126353Smlaier		| FILENAME STRING	{
1287126353Smlaier			struct node_tinit	*ti;
1288126353Smlaier
1289126353Smlaier			if (!(ti = calloc(1, sizeof(*ti))))
1290126353Smlaier				err(1, "table_opt: calloc");
1291126353Smlaier			ti->file = $2;
1292126353Smlaier			SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1293126353Smlaier			    entries);
1294126353Smlaier			table_opts.init_addr = 1;
1295126353Smlaier		}
1296126353Smlaier		;
1297126353Smlaier
1298126353Smlaieraltqif		: ALTQ interface queue_opts QUEUE qassign {
1299126353Smlaier			struct pf_altq	a;
1300126353Smlaier
1301126353Smlaier			if (check_rulestate(PFCTL_STATE_QUEUE))
1302126353Smlaier				YYERROR;
1303126353Smlaier
1304126353Smlaier			memset(&a, 0, sizeof(a));
1305126353Smlaier			if ($3.scheduler.qtype == ALTQT_NONE) {
1306126353Smlaier				yyerror("no scheduler specified!");
1307126353Smlaier				YYERROR;
1308126353Smlaier			}
1309126353Smlaier			a.scheduler = $3.scheduler.qtype;
1310126353Smlaier			a.qlimit = $3.qlimit;
1311126353Smlaier			a.tbrsize = $3.tbrsize;
1312126353Smlaier			if ($5 == NULL) {
1313126353Smlaier				yyerror("no child queues specified");
1314126353Smlaier				YYERROR;
1315126353Smlaier			}
1316126353Smlaier			if (expand_altq(&a, $2, $5, $3.queue_bwspec,
1317126353Smlaier			    &$3.scheduler))
1318126353Smlaier				YYERROR;
1319126353Smlaier		}
1320126353Smlaier		;
1321126353Smlaier
1322126353Smlaierqueuespec	: QUEUE STRING interface queue_opts qassign {
1323126353Smlaier			struct pf_altq	a;
1324126353Smlaier
1325130617Smlaier			if (check_rulestate(PFCTL_STATE_QUEUE)) {
1326130617Smlaier				free($2);
1327126353Smlaier				YYERROR;
1328130617Smlaier			}
1329126353Smlaier
1330126353Smlaier			memset(&a, 0, sizeof(a));
1331126353Smlaier
1332126353Smlaier			if (strlcpy(a.qname, $2, sizeof(a.qname)) >=
1333126353Smlaier			    sizeof(a.qname)) {
1334126353Smlaier				yyerror("queue name too long (max "
1335126353Smlaier				    "%d chars)", PF_QNAME_SIZE-1);
1336130617Smlaier				free($2);
1337126353Smlaier				YYERROR;
1338126353Smlaier			}
1339130617Smlaier			free($2);
1340126353Smlaier			if ($4.tbrsize) {
1341126353Smlaier				yyerror("cannot specify tbrsize for queue");
1342126353Smlaier				YYERROR;
1343126353Smlaier			}
1344126353Smlaier			if ($4.priority > 255) {
1345126353Smlaier				yyerror("priority out of range: max 255");
1346126353Smlaier				YYERROR;
1347126353Smlaier			}
1348126353Smlaier			a.priority = $4.priority;
1349126353Smlaier			a.qlimit = $4.qlimit;
1350126353Smlaier			a.scheduler = $4.scheduler.qtype;
1351126353Smlaier			if (expand_queue(&a, $3, $5, $4.queue_bwspec,
1352126353Smlaier			    &$4.scheduler)) {
1353126353Smlaier				yyerror("errors in queue definition");
1354126353Smlaier				YYERROR;
1355126353Smlaier			}
1356126353Smlaier		}
1357126353Smlaier		;
1358126353Smlaier
1359126353Smlaierqueue_opts	:	{
1360126353Smlaier			bzero(&queue_opts, sizeof queue_opts);
1361126353Smlaier			queue_opts.priority = DEFAULT_PRIORITY;
1362126353Smlaier			queue_opts.qlimit = DEFAULT_QLIMIT;
1363126353Smlaier			queue_opts.scheduler.qtype = ALTQT_NONE;
1364126353Smlaier			queue_opts.queue_bwspec.bw_percent = 100;
1365126353Smlaier		}
1366130617Smlaier		    queue_opts_l
1367126353Smlaier			{ $$ = queue_opts; }
1368126353Smlaier		| /* empty */ {
1369126353Smlaier			bzero(&queue_opts, sizeof queue_opts);
1370126353Smlaier			queue_opts.priority = DEFAULT_PRIORITY;
1371126353Smlaier			queue_opts.qlimit = DEFAULT_QLIMIT;
1372126353Smlaier			queue_opts.scheduler.qtype = ALTQT_NONE;
1373126353Smlaier			queue_opts.queue_bwspec.bw_percent = 100;
1374126353Smlaier			$$ = queue_opts;
1375126353Smlaier		}
1376126353Smlaier		;
1377126353Smlaier
1378126353Smlaierqueue_opts_l	: queue_opts_l queue_opt
1379126353Smlaier		| queue_opt
1380126353Smlaier		;
1381126353Smlaier
1382126353Smlaierqueue_opt	: BANDWIDTH bandwidth	{
1383126353Smlaier			if (queue_opts.marker & QOM_BWSPEC) {
1384126353Smlaier				yyerror("bandwidth cannot be respecified");
1385126353Smlaier				YYERROR;
1386126353Smlaier			}
1387126353Smlaier			queue_opts.marker |= QOM_BWSPEC;
1388126353Smlaier			queue_opts.queue_bwspec = $2;
1389126353Smlaier		}
1390126353Smlaier		| PRIORITY number	{
1391126353Smlaier			if (queue_opts.marker & QOM_PRIORITY) {
1392126353Smlaier				yyerror("priority cannot be respecified");
1393126353Smlaier				YYERROR;
1394126353Smlaier			}
1395126353Smlaier			if ($2 > 255) {
1396126353Smlaier				yyerror("priority out of range: max 255");
1397126353Smlaier				YYERROR;
1398126353Smlaier			}
1399126353Smlaier			queue_opts.marker |= QOM_PRIORITY;
1400126353Smlaier			queue_opts.priority = $2;
1401126353Smlaier		}
1402126353Smlaier		| QLIMIT number	{
1403126353Smlaier			if (queue_opts.marker & QOM_QLIMIT) {
1404126353Smlaier				yyerror("qlimit cannot be respecified");
1405126353Smlaier				YYERROR;
1406126353Smlaier			}
1407126353Smlaier			if ($2 > 65535) {
1408126353Smlaier				yyerror("qlimit out of range: max 65535");
1409126353Smlaier				YYERROR;
1410126353Smlaier			}
1411126353Smlaier			queue_opts.marker |= QOM_QLIMIT;
1412126353Smlaier			queue_opts.qlimit = $2;
1413126353Smlaier		}
1414126353Smlaier		| scheduler	{
1415126353Smlaier			if (queue_opts.marker & QOM_SCHEDULER) {
1416126353Smlaier				yyerror("scheduler cannot be respecified");
1417126353Smlaier				YYERROR;
1418126353Smlaier			}
1419126353Smlaier			queue_opts.marker |= QOM_SCHEDULER;
1420126353Smlaier			queue_opts.scheduler = $1;
1421126353Smlaier		}
1422126353Smlaier		| TBRSIZE number	{
1423126353Smlaier			if (queue_opts.marker & QOM_TBRSIZE) {
1424126353Smlaier				yyerror("tbrsize cannot be respecified");
1425126353Smlaier				YYERROR;
1426126353Smlaier			}
1427126353Smlaier			if ($2 > 65535) {
1428126353Smlaier				yyerror("tbrsize too big: max 65535");
1429126353Smlaier				YYERROR;
1430126353Smlaier			}
1431126353Smlaier			queue_opts.marker |= QOM_TBRSIZE;
1432126353Smlaier			queue_opts.tbrsize = $2;
1433126353Smlaier		}
1434126353Smlaier		;
1435126353Smlaier
1436126353Smlaierbandwidth	: STRING {
1437126353Smlaier			double	 bps;
1438126353Smlaier			char	*cp;
1439126353Smlaier
1440126353Smlaier			$$.bw_percent = 0;
1441126353Smlaier
1442126353Smlaier			bps = strtod($1, &cp);
1443126353Smlaier			if (cp != NULL) {
1444126353Smlaier				if (!strcmp(cp, "b"))
1445126353Smlaier					; /* nothing */
1446126353Smlaier				else if (!strcmp(cp, "Kb"))
1447126353Smlaier					bps *= 1000;
1448126353Smlaier				else if (!strcmp(cp, "Mb"))
1449126353Smlaier					bps *= 1000 * 1000;
1450126353Smlaier				else if (!strcmp(cp, "Gb"))
1451126353Smlaier					bps *= 1000 * 1000 * 1000;
1452126353Smlaier				else if (!strcmp(cp, "%")) {
1453126353Smlaier					if (bps < 0 || bps > 100) {
1454126353Smlaier						yyerror("bandwidth spec "
1455126353Smlaier						    "out of range");
1456130617Smlaier						free($1);
1457126353Smlaier						YYERROR;
1458126353Smlaier					}
1459126353Smlaier					$$.bw_percent = bps;
1460126353Smlaier					bps = 0;
1461126353Smlaier				} else {
1462126353Smlaier					yyerror("unknown unit %s", cp);
1463130617Smlaier					free($1);
1464126353Smlaier					YYERROR;
1465126353Smlaier				}
1466126353Smlaier			}
1467130617Smlaier			free($1);
1468126353Smlaier			$$.bw_absolute = (u_int32_t)bps;
1469126353Smlaier		}
1470130617Smlaier		;
1471126353Smlaier
1472126353Smlaierscheduler	: CBQ				{
1473126353Smlaier			$$.qtype = ALTQT_CBQ;
1474126353Smlaier			$$.data.cbq_opts.flags = 0;
1475126353Smlaier		}
1476126353Smlaier		| CBQ '(' cbqflags_list ')'	{
1477126353Smlaier			$$.qtype = ALTQT_CBQ;
1478126353Smlaier			$$.data.cbq_opts.flags = $3;
1479126353Smlaier		}
1480126353Smlaier		| PRIQ				{
1481126353Smlaier			$$.qtype = ALTQT_PRIQ;
1482126353Smlaier			$$.data.priq_opts.flags = 0;
1483126353Smlaier		}
1484126353Smlaier		| PRIQ '(' priqflags_list ')'	{
1485126353Smlaier			$$.qtype = ALTQT_PRIQ;
1486126353Smlaier			$$.data.priq_opts.flags = $3;
1487126353Smlaier		}
1488126353Smlaier		| HFSC				{
1489126353Smlaier			$$.qtype = ALTQT_HFSC;
1490126353Smlaier			bzero(&$$.data.hfsc_opts,
1491126353Smlaier			    sizeof(struct node_hfsc_opts));
1492126353Smlaier		}
1493126353Smlaier		| HFSC '(' hfsc_opts ')'	{
1494126353Smlaier			$$.qtype = ALTQT_HFSC;
1495126353Smlaier			$$.data.hfsc_opts = $3;
1496126353Smlaier		}
1497126353Smlaier		;
1498126353Smlaier
1499126353Smlaiercbqflags_list	: cbqflags_item				{ $$ |= $1; }
1500126353Smlaier		| cbqflags_list comma cbqflags_item	{ $$ |= $3; }
1501126353Smlaier		;
1502126353Smlaier
1503126353Smlaiercbqflags_item	: STRING	{
1504126353Smlaier			if (!strcmp($1, "default"))
1505126353Smlaier				$$ = CBQCLF_DEFCLASS;
1506126353Smlaier			else if (!strcmp($1, "borrow"))
1507126353Smlaier				$$ = CBQCLF_BORROW;
1508126353Smlaier			else if (!strcmp($1, "red"))
1509126353Smlaier				$$ = CBQCLF_RED;
1510126353Smlaier			else if (!strcmp($1, "ecn"))
1511126353Smlaier				$$ = CBQCLF_RED|CBQCLF_ECN;
1512126353Smlaier			else if (!strcmp($1, "rio"))
1513126353Smlaier				$$ = CBQCLF_RIO;
1514126353Smlaier			else {
1515126353Smlaier				yyerror("unknown cbq flag \"%s\"", $1);
1516130617Smlaier				free($1);
1517126353Smlaier				YYERROR;
1518126353Smlaier			}
1519130617Smlaier			free($1);
1520126353Smlaier		}
1521126353Smlaier		;
1522126353Smlaier
1523126353Smlaierpriqflags_list	: priqflags_item			{ $$ |= $1; }
1524126353Smlaier		| priqflags_list comma priqflags_item	{ $$ |= $3; }
1525126353Smlaier		;
1526126353Smlaier
1527126353Smlaierpriqflags_item	: STRING	{
1528126353Smlaier			if (!strcmp($1, "default"))
1529126353Smlaier				$$ = PRCF_DEFAULTCLASS;
1530126353Smlaier			else if (!strcmp($1, "red"))
1531126353Smlaier				$$ = PRCF_RED;
1532126353Smlaier			else if (!strcmp($1, "ecn"))
1533126353Smlaier				$$ = PRCF_RED|PRCF_ECN;
1534126353Smlaier			else if (!strcmp($1, "rio"))
1535126353Smlaier				$$ = PRCF_RIO;
1536126353Smlaier			else {
1537126353Smlaier				yyerror("unknown priq flag \"%s\"", $1);
1538130617Smlaier				free($1);
1539126353Smlaier				YYERROR;
1540126353Smlaier			}
1541130617Smlaier			free($1);
1542126353Smlaier		}
1543126353Smlaier		;
1544126353Smlaier
1545126353Smlaierhfsc_opts	:	{
1546126353Smlaier				bzero(&hfsc_opts,
1547126353Smlaier				    sizeof(struct node_hfsc_opts));
1548126353Smlaier			}
1549130617Smlaier		    hfscopts_list				{
1550126353Smlaier			$$ = hfsc_opts;
1551126353Smlaier		}
1552126353Smlaier		;
1553126353Smlaier
1554126353Smlaierhfscopts_list	: hfscopts_item
1555126353Smlaier		| hfscopts_list comma hfscopts_item
1556126353Smlaier		;
1557126353Smlaier
1558126353Smlaierhfscopts_item	: LINKSHARE bandwidth				{
1559126353Smlaier			if (hfsc_opts.linkshare.used) {
1560126353Smlaier				yyerror("linkshare already specified");
1561126353Smlaier				YYERROR;
1562126353Smlaier			}
1563126353Smlaier			hfsc_opts.linkshare.m2 = $2;
1564126353Smlaier			hfsc_opts.linkshare.used = 1;
1565126353Smlaier		}
1566145840Smlaier		| LINKSHARE '(' bandwidth comma number comma bandwidth ')'
1567145840Smlaier		    {
1568126353Smlaier			if (hfsc_opts.linkshare.used) {
1569126353Smlaier				yyerror("linkshare already specified");
1570126353Smlaier				YYERROR;
1571126353Smlaier			}
1572126353Smlaier			hfsc_opts.linkshare.m1 = $3;
1573145840Smlaier			hfsc_opts.linkshare.d = $5;
1574145840Smlaier			hfsc_opts.linkshare.m2 = $7;
1575126353Smlaier			hfsc_opts.linkshare.used = 1;
1576126353Smlaier		}
1577126353Smlaier		| REALTIME bandwidth				{
1578126353Smlaier			if (hfsc_opts.realtime.used) {
1579126353Smlaier				yyerror("realtime already specified");
1580126353Smlaier				YYERROR;
1581126353Smlaier			}
1582126353Smlaier			hfsc_opts.realtime.m2 = $2;
1583126353Smlaier			hfsc_opts.realtime.used = 1;
1584126353Smlaier		}
1585145840Smlaier		| REALTIME '(' bandwidth comma number comma bandwidth ')'
1586145840Smlaier		    {
1587126353Smlaier			if (hfsc_opts.realtime.used) {
1588126353Smlaier				yyerror("realtime already specified");
1589126353Smlaier				YYERROR;
1590126353Smlaier			}
1591126353Smlaier			hfsc_opts.realtime.m1 = $3;
1592145840Smlaier			hfsc_opts.realtime.d = $5;
1593145840Smlaier			hfsc_opts.realtime.m2 = $7;
1594126353Smlaier			hfsc_opts.realtime.used = 1;
1595126353Smlaier		}
1596126353Smlaier		| UPPERLIMIT bandwidth				{
1597126353Smlaier			if (hfsc_opts.upperlimit.used) {
1598126353Smlaier				yyerror("upperlimit already specified");
1599126353Smlaier				YYERROR;
1600126353Smlaier			}
1601126353Smlaier			hfsc_opts.upperlimit.m2 = $2;
1602126353Smlaier			hfsc_opts.upperlimit.used = 1;
1603126353Smlaier		}
1604145840Smlaier		| UPPERLIMIT '(' bandwidth comma number comma bandwidth ')'
1605145840Smlaier		    {
1606126353Smlaier			if (hfsc_opts.upperlimit.used) {
1607126353Smlaier				yyerror("upperlimit already specified");
1608126353Smlaier				YYERROR;
1609126353Smlaier			}
1610126353Smlaier			hfsc_opts.upperlimit.m1 = $3;
1611145840Smlaier			hfsc_opts.upperlimit.d = $5;
1612145840Smlaier			hfsc_opts.upperlimit.m2 = $7;
1613126353Smlaier			hfsc_opts.upperlimit.used = 1;
1614126353Smlaier		}
1615126353Smlaier		| STRING	{
1616126353Smlaier			if (!strcmp($1, "default"))
1617126353Smlaier				hfsc_opts.flags |= HFCF_DEFAULTCLASS;
1618126353Smlaier			else if (!strcmp($1, "red"))
1619126353Smlaier				hfsc_opts.flags |= HFCF_RED;
1620126353Smlaier			else if (!strcmp($1, "ecn"))
1621126353Smlaier				hfsc_opts.flags |= HFCF_RED|HFCF_ECN;
1622126353Smlaier			else if (!strcmp($1, "rio"))
1623126353Smlaier				hfsc_opts.flags |= HFCF_RIO;
1624126353Smlaier			else {
1625126353Smlaier				yyerror("unknown hfsc flag \"%s\"", $1);
1626130617Smlaier				free($1);
1627126353Smlaier				YYERROR;
1628126353Smlaier			}
1629130617Smlaier			free($1);
1630126353Smlaier		}
1631126353Smlaier		;
1632126353Smlaier
1633126353Smlaierqassign		: /* empty */		{ $$ = NULL; }
1634126353Smlaier		| qassign_item		{ $$ = $1; }
1635126353Smlaier		| '{' qassign_list '}'	{ $$ = $2; }
1636126353Smlaier		;
1637126353Smlaier
1638126353Smlaierqassign_list	: qassign_item			{ $$ = $1; }
1639126353Smlaier		| qassign_list comma qassign_item	{
1640126353Smlaier			$1->tail->next = $3;
1641126353Smlaier			$1->tail = $3;
1642126353Smlaier			$$ = $1;
1643126353Smlaier		}
1644126353Smlaier		;
1645126353Smlaier
1646126353Smlaierqassign_item	: STRING			{
1647126353Smlaier			$$ = calloc(1, sizeof(struct node_queue));
1648126353Smlaier			if ($$ == NULL)
1649126353Smlaier				err(1, "qassign_item: calloc");
1650126353Smlaier			if (strlcpy($$->queue, $1, sizeof($$->queue)) >=
1651126353Smlaier			    sizeof($$->queue)) {
1652126353Smlaier				yyerror("queue name '%s' too long (max "
1653126353Smlaier				    "%d chars)", $1, sizeof($$->queue)-1);
1654130617Smlaier				free($1);
1655130617Smlaier				free($$);
1656126353Smlaier				YYERROR;
1657126353Smlaier			}
1658130617Smlaier			free($1);
1659126353Smlaier			$$->next = NULL;
1660126353Smlaier			$$->tail = $$;
1661126353Smlaier		}
1662126353Smlaier		;
1663126353Smlaier
1664126353Smlaierpfrule		: action dir logquick interface route af proto fromto
1665130617Smlaier		    filter_opts
1666126353Smlaier		{
1667126353Smlaier			struct pf_rule		 r;
1668126353Smlaier			struct node_state_opt	*o;
1669126353Smlaier			struct node_proto	*proto;
1670130617Smlaier			int			 srctrack = 0;
1671130617Smlaier			int			 statelock = 0;
1672171172Smlaier			int			 adaptive = 0;
1673126353Smlaier
1674126353Smlaier			if (check_rulestate(PFCTL_STATE_FILTER))
1675126353Smlaier				YYERROR;
1676126353Smlaier
1677126353Smlaier			memset(&r, 0, sizeof(r));
1678126353Smlaier
1679126353Smlaier			r.action = $1.b1;
1680126353Smlaier			switch ($1.b2) {
1681126353Smlaier			case PFRULE_RETURNRST:
1682126353Smlaier				r.rule_flag |= PFRULE_RETURNRST;
1683126353Smlaier				r.return_ttl = $1.w;
1684126353Smlaier				break;
1685126353Smlaier			case PFRULE_RETURNICMP:
1686126353Smlaier				r.rule_flag |= PFRULE_RETURNICMP;
1687126353Smlaier				r.return_icmp = $1.w;
1688126353Smlaier				r.return_icmp6 = $1.w2;
1689126353Smlaier				break;
1690126353Smlaier			case PFRULE_RETURN:
1691126353Smlaier				r.rule_flag |= PFRULE_RETURN;
1692126353Smlaier				r.return_icmp = $1.w;
1693126353Smlaier				r.return_icmp6 = $1.w2;
1694126353Smlaier				break;
1695126353Smlaier			}
1696126353Smlaier			r.direction = $2;
1697126353Smlaier			r.log = $3.log;
1698171172Smlaier			r.logif = $3.logif;
1699126353Smlaier			r.quick = $3.quick;
1700145840Smlaier			r.prob = $9.prob;
1701171172Smlaier			r.rtableid = $9.rtableid;
1702126353Smlaier
1703126353Smlaier			r.af = $6;
1704126353Smlaier			if ($9.tag)
1705126353Smlaier				if (strlcpy(r.tagname, $9.tag,
1706130617Smlaier				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1707126353Smlaier					yyerror("tag too long, max %u chars",
1708126353Smlaier					    PF_TAG_NAME_SIZE - 1);
1709126353Smlaier					YYERROR;
1710126353Smlaier				}
1711126353Smlaier			if ($9.match_tag)
1712126353Smlaier				if (strlcpy(r.match_tagname, $9.match_tag,
1713130617Smlaier				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1714126353Smlaier					yyerror("tag too long, max %u chars",
1715126353Smlaier					    PF_TAG_NAME_SIZE - 1);
1716126353Smlaier					YYERROR;
1717126353Smlaier				}
1718126353Smlaier			r.match_tag_not = $9.match_tag_not;
1719171172Smlaier			if (rule_label(&r, $9.label))
1720171172Smlaier				YYERROR;
1721171172Smlaier			free($9.label);
1722126353Smlaier			r.flags = $9.flags.b1;
1723126353Smlaier			r.flagset = $9.flags.b2;
1724171172Smlaier			if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
1725171172Smlaier				yyerror("flags always false");
1726126353Smlaier				YYERROR;
1727171172Smlaier			}
1728126353Smlaier			if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
1729126353Smlaier				for (proto = $7; proto != NULL &&
1730126353Smlaier				    proto->proto != IPPROTO_TCP;
1731126353Smlaier				    proto = proto->next)
1732126353Smlaier					;	/* nothing */
1733126353Smlaier				if (proto == NULL && $7 != NULL) {
1734126353Smlaier					if ($9.flags.b1 || $9.flags.b2)
1735126353Smlaier						yyerror(
1736126353Smlaier						    "flags only apply to tcp");
1737126353Smlaier					if ($8.src_os)
1738126353Smlaier						yyerror(
1739126353Smlaier						    "OS fingerprinting only "
1740126353Smlaier						    "apply to tcp");
1741126353Smlaier					YYERROR;
1742126353Smlaier				}
1743126353Smlaier#if 0
1744126353Smlaier				if (($9.flags.b1 & parse_flags("S")) == 0 &&
1745126353Smlaier				    $8.src_os) {
1746126353Smlaier					yyerror("OS fingerprinting requires "
1747130617Smlaier					    "the SYN TCP flag (flags S/SA)");
1748126353Smlaier					YYERROR;
1749126353Smlaier				}
1750126353Smlaier#endif
1751126353Smlaier			}
1752126353Smlaier
1753126353Smlaier			r.tos = $9.tos;
1754126353Smlaier			r.keep_state = $9.keep.action;
1755171172Smlaier
1756171172Smlaier			/* 'keep state' by default on pass rules. */
1757171172Smlaier			if (!r.keep_state && !r.action &&
1758171172Smlaier			    !($9.marker & FOM_KEEP))
1759171172Smlaier				r.keep_state = PF_STATE_NORMAL;
1760171172Smlaier
1761126353Smlaier			o = $9.keep.options;
1762126353Smlaier			while (o) {
1763126353Smlaier				struct node_state_opt	*p = o;
1764126353Smlaier
1765126353Smlaier				switch (o->type) {
1766126353Smlaier				case PF_STATE_OPT_MAX:
1767126353Smlaier					if (r.max_states) {
1768126353Smlaier						yyerror("state option 'max' "
1769126353Smlaier						    "multiple definitions");
1770126353Smlaier						YYERROR;
1771126353Smlaier					}
1772126353Smlaier					r.max_states = o->data.max_states;
1773126353Smlaier					break;
1774130617Smlaier				case PF_STATE_OPT_NOSYNC:
1775130617Smlaier					if (r.rule_flag & PFRULE_NOSYNC) {
1776130617Smlaier						yyerror("state option 'sync' "
1777130617Smlaier						    "multiple definitions");
1778130617Smlaier						YYERROR;
1779130617Smlaier					}
1780130617Smlaier					r.rule_flag |= PFRULE_NOSYNC;
1781130617Smlaier					break;
1782130617Smlaier				case PF_STATE_OPT_SRCTRACK:
1783130617Smlaier					if (srctrack) {
1784130617Smlaier						yyerror("state option "
1785130617Smlaier						    "'source-track' "
1786130617Smlaier						    "multiple definitions");
1787130617Smlaier						YYERROR;
1788130617Smlaier					}
1789130617Smlaier					srctrack =  o->data.src_track;
1790145840Smlaier					r.rule_flag |= PFRULE_SRCTRACK;
1791130617Smlaier					break;
1792130617Smlaier				case PF_STATE_OPT_MAX_SRC_STATES:
1793130617Smlaier					if (r.max_src_states) {
1794130617Smlaier						yyerror("state option "
1795130617Smlaier						    "'max-src-states' "
1796130617Smlaier						    "multiple definitions");
1797130617Smlaier						YYERROR;
1798130617Smlaier					}
1799145840Smlaier					if (o->data.max_src_states == 0) {
1800130617Smlaier						yyerror("'max-src-states' must "
1801130617Smlaier						    "be > 0");
1802130617Smlaier						YYERROR;
1803130617Smlaier					}
1804130617Smlaier					r.max_src_states =
1805130617Smlaier					    o->data.max_src_states;
1806130617Smlaier					r.rule_flag |= PFRULE_SRCTRACK;
1807130617Smlaier					break;
1808145840Smlaier				case PF_STATE_OPT_OVERLOAD:
1809145840Smlaier					if (r.overload_tblname[0]) {
1810145840Smlaier						yyerror("multiple 'overload' "
1811145840Smlaier						    "table definitions");
1812145840Smlaier						YYERROR;
1813145840Smlaier					}
1814145840Smlaier					if (strlcpy(r.overload_tblname,
1815145840Smlaier					    o->data.overload.tblname,
1816145840Smlaier					    PF_TABLE_NAME_SIZE) >=
1817145840Smlaier					    PF_TABLE_NAME_SIZE) {
1818145840Smlaier						yyerror("state option: "
1819145840Smlaier						    "strlcpy");
1820145840Smlaier						YYERROR;
1821145840Smlaier					}
1822145840Smlaier					r.flush = o->data.overload.flush;
1823145840Smlaier					break;
1824145840Smlaier				case PF_STATE_OPT_MAX_SRC_CONN:
1825145840Smlaier					if (r.max_src_conn) {
1826145840Smlaier						yyerror("state option "
1827145840Smlaier						    "'max-src-conn' "
1828145840Smlaier						    "multiple definitions");
1829145840Smlaier						YYERROR;
1830145840Smlaier					}
1831145840Smlaier					if (o->data.max_src_conn == 0) {
1832145840Smlaier						yyerror("'max-src-conn' "
1833145840Smlaier						    "must be > 0");
1834145840Smlaier						YYERROR;
1835145840Smlaier					}
1836145840Smlaier					r.max_src_conn =
1837145840Smlaier					    o->data.max_src_conn;
1838145840Smlaier					r.rule_flag |= PFRULE_SRCTRACK |
1839145840Smlaier					    PFRULE_RULESRCTRACK;
1840145840Smlaier					break;
1841145840Smlaier				case PF_STATE_OPT_MAX_SRC_CONN_RATE:
1842145840Smlaier					if (r.max_src_conn_rate.limit) {
1843145840Smlaier						yyerror("state option "
1844145840Smlaier						    "'max-src-conn-rate' "
1845145840Smlaier						    "multiple definitions");
1846145840Smlaier						YYERROR;
1847145840Smlaier					}
1848145840Smlaier					if (!o->data.max_src_conn_rate.limit ||
1849145840Smlaier					    !o->data.max_src_conn_rate.seconds) {
1850145840Smlaier						yyerror("'max-src-conn-rate' "
1851145840Smlaier						    "values must be > 0");
1852145840Smlaier						YYERROR;
1853145840Smlaier					}
1854145840Smlaier					if (o->data.max_src_conn_rate.limit >
1855145840Smlaier					    PF_THRESHOLD_MAX) {
1856145840Smlaier						yyerror("'max-src-conn-rate' "
1857171172Smlaier						    "maximum rate must be < %u",
1858171172Smlaier						    PF_THRESHOLD_MAX);
1859145840Smlaier						YYERROR;
1860145840Smlaier					}
1861145840Smlaier					r.max_src_conn_rate.limit =
1862145840Smlaier					    o->data.max_src_conn_rate.limit;
1863145840Smlaier					r.max_src_conn_rate.seconds =
1864145840Smlaier					    o->data.max_src_conn_rate.seconds;
1865145840Smlaier					r.rule_flag |= PFRULE_SRCTRACK |
1866145840Smlaier					    PFRULE_RULESRCTRACK;
1867145840Smlaier					break;
1868130617Smlaier				case PF_STATE_OPT_MAX_SRC_NODES:
1869130617Smlaier					if (r.max_src_nodes) {
1870130617Smlaier						yyerror("state option "
1871130617Smlaier						    "'max-src-nodes' "
1872130617Smlaier						    "multiple definitions");
1873130617Smlaier						YYERROR;
1874130617Smlaier					}
1875130617Smlaier					if (o->data.max_src_nodes == 0) {
1876130617Smlaier						yyerror("'max-src-nodes' must "
1877130617Smlaier						    "be > 0");
1878130617Smlaier						YYERROR;
1879130617Smlaier					}
1880130617Smlaier					r.max_src_nodes =
1881130617Smlaier					    o->data.max_src_nodes;
1882130617Smlaier					r.rule_flag |= PFRULE_SRCTRACK |
1883130617Smlaier					    PFRULE_RULESRCTRACK;
1884130617Smlaier					break;
1885130617Smlaier				case PF_STATE_OPT_STATELOCK:
1886130617Smlaier					if (statelock) {
1887130617Smlaier						yyerror("state locking option: "
1888130617Smlaier						    "multiple definitions");
1889130617Smlaier						YYERROR;
1890130617Smlaier					}
1891130617Smlaier					statelock = 1;
1892130617Smlaier					r.rule_flag |= o->data.statelock;
1893130617Smlaier					break;
1894126353Smlaier				case PF_STATE_OPT_TIMEOUT:
1895171172Smlaier					if (o->data.timeout.number ==
1896171172Smlaier					    PFTM_ADAPTIVE_START ||
1897171172Smlaier					    o->data.timeout.number ==
1898171172Smlaier					    PFTM_ADAPTIVE_END)
1899171172Smlaier						adaptive = 1;
1900126353Smlaier					if (r.timeout[o->data.timeout.number]) {
1901126353Smlaier						yyerror("state timeout %s "
1902126353Smlaier						    "multiple definitions",
1903126353Smlaier						    pf_timeouts[o->data.
1904126353Smlaier						    timeout.number].name);
1905126353Smlaier						YYERROR;
1906126353Smlaier					}
1907126353Smlaier					r.timeout[o->data.timeout.number] =
1908126353Smlaier					    o->data.timeout.seconds;
1909126353Smlaier				}
1910126353Smlaier				o = o->next;
1911126353Smlaier				free(p);
1912126353Smlaier			}
1913171172Smlaier
1914171172Smlaier			/* 'flags S/SA' by default on stateful rules */
1915171172Smlaier			if (!r.action && !r.flags && !r.flagset &&
1916171172Smlaier			    !$9.fragment && !($9.marker & FOM_FLAGS) &&
1917171172Smlaier			    r.keep_state) {
1918171172Smlaier				r.flags = parse_flags("S");
1919171172Smlaier				r.flagset =  parse_flags("SA");
1920171172Smlaier			}
1921171172Smlaier			if (!adaptive && r.max_states) {
1922171172Smlaier				r.timeout[PFTM_ADAPTIVE_START] =
1923171172Smlaier				    (r.max_states / 10) * 6;
1924171172Smlaier				r.timeout[PFTM_ADAPTIVE_END] =
1925171172Smlaier				    (r.max_states / 10) * 12;
1926171172Smlaier			}
1927145840Smlaier			if (r.rule_flag & PFRULE_SRCTRACK) {
1928130617Smlaier				if (srctrack == PF_SRCTRACK_GLOBAL &&
1929130617Smlaier				    r.max_src_nodes) {
1930130617Smlaier					yyerror("'max-src-nodes' is "
1931130617Smlaier					    "incompatible with "
1932130617Smlaier					    "'source-track global'");
1933130617Smlaier					YYERROR;
1934130617Smlaier				}
1935145840Smlaier				if (srctrack == PF_SRCTRACK_GLOBAL &&
1936145840Smlaier				    r.max_src_conn) {
1937145840Smlaier					yyerror("'max-src-conn' is "
1938145840Smlaier					    "incompatible with "
1939145840Smlaier					    "'source-track global'");
1940145840Smlaier					YYERROR;
1941145840Smlaier				}
1942145840Smlaier				if (srctrack == PF_SRCTRACK_GLOBAL &&
1943145840Smlaier				    r.max_src_conn_rate.seconds) {
1944145840Smlaier					yyerror("'max-src-conn-rate' is "
1945145840Smlaier					    "incompatible with "
1946145840Smlaier					    "'source-track global'");
1947145840Smlaier					YYERROR;
1948145840Smlaier				}
1949145840Smlaier				if (r.timeout[PFTM_SRC_NODE] <
1950145840Smlaier				    r.max_src_conn_rate.seconds)
1951145840Smlaier					r.timeout[PFTM_SRC_NODE] =
1952145840Smlaier					    r.max_src_conn_rate.seconds;
1953130617Smlaier				r.rule_flag |= PFRULE_SRCTRACK;
1954130617Smlaier				if (srctrack == PF_SRCTRACK_RULE)
1955130617Smlaier					r.rule_flag |= PFRULE_RULESRCTRACK;
1956130617Smlaier			}
1957130617Smlaier			if (r.keep_state && !statelock)
1958130617Smlaier				r.rule_flag |= default_statelock;
1959126353Smlaier
1960126353Smlaier			if ($9.fragment)
1961126353Smlaier				r.rule_flag |= PFRULE_FRAGMENT;
1962126353Smlaier			r.allow_opts = $9.allowopts;
1963126353Smlaier
1964126353Smlaier			decide_address_family($8.src.host, &r.af);
1965126353Smlaier			decide_address_family($8.dst.host, &r.af);
1966126353Smlaier
1967126353Smlaier			if ($5.rt) {
1968126353Smlaier				if (!r.direction) {
1969126353Smlaier					yyerror("direction must be explicit "
1970126353Smlaier					    "with rules that specify routing");
1971126353Smlaier					YYERROR;
1972126353Smlaier				}
1973126353Smlaier				r.rt = $5.rt;
1974126353Smlaier				r.rpool.opts = $5.pool_opts;
1975126353Smlaier				if ($5.key != NULL)
1976126353Smlaier					memcpy(&r.rpool.key, $5.key,
1977126353Smlaier					    sizeof(struct pf_poolhashkey));
1978126353Smlaier			}
1979126353Smlaier			if (r.rt && r.rt != PF_FASTROUTE) {
1980126353Smlaier				decide_address_family($5.host, &r.af);
1981126353Smlaier				remove_invalid_hosts(&$5.host, &r.af);
1982126353Smlaier				if ($5.host == NULL) {
1983126353Smlaier					yyerror("no routing address with "
1984126353Smlaier					    "matching address family found.");
1985126353Smlaier					YYERROR;
1986126353Smlaier				}
1987130617Smlaier				if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
1988130617Smlaier				    PF_POOL_NONE && ($5.host->next != NULL ||
1989130617Smlaier				    $5.host->addr.type == PF_ADDR_TABLE ||
1990130617Smlaier				    DYNIF_MULTIADDR($5.host->addr)))
1991130617Smlaier					r.rpool.opts |= PF_POOL_ROUNDROBIN;
1992130617Smlaier				if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
1993130617Smlaier				    PF_POOL_ROUNDROBIN &&
1994130617Smlaier				    disallow_table($5.host, "tables are only "
1995130617Smlaier				    "supported in round-robin routing pools"))
1996130617Smlaier					YYERROR;
1997130617Smlaier				if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
1998130617Smlaier				    PF_POOL_ROUNDROBIN &&
1999130617Smlaier				    disallow_alias($5.host, "interface (%s) "
2000130617Smlaier				    "is only supported in round-robin "
2001130617Smlaier				    "routing pools"))
2002130617Smlaier					YYERROR;
2003126353Smlaier				if ($5.host->next != NULL) {
2004130617Smlaier					if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2005126353Smlaier					    PF_POOL_ROUNDROBIN) {
2006126353Smlaier						yyerror("r.rpool.opts must "
2007126353Smlaier						    "be PF_POOL_ROUNDROBIN");
2008126353Smlaier						YYERROR;
2009126353Smlaier					}
2010126353Smlaier				}
2011126353Smlaier			}
2012126353Smlaier			if ($9.queues.qname != NULL) {
2013126353Smlaier				if (strlcpy(r.qname, $9.queues.qname,
2014126353Smlaier				    sizeof(r.qname)) >= sizeof(r.qname)) {
2015126353Smlaier					yyerror("rule qname too long (max "
2016126353Smlaier					    "%d chars)", sizeof(r.qname)-1);
2017126353Smlaier					YYERROR;
2018126353Smlaier				}
2019126353Smlaier				free($9.queues.qname);
2020126353Smlaier			}
2021126353Smlaier			if ($9.queues.pqname != NULL) {
2022126353Smlaier				if (strlcpy(r.pqname, $9.queues.pqname,
2023126353Smlaier				    sizeof(r.pqname)) >= sizeof(r.pqname)) {
2024126353Smlaier					yyerror("rule pqname too long (max "
2025126353Smlaier					    "%d chars)", sizeof(r.pqname)-1);
2026126353Smlaier					YYERROR;
2027126353Smlaier				}
2028126353Smlaier				free($9.queues.pqname);
2029126353Smlaier			}
2030126353Smlaier
2031126353Smlaier			expand_rule(&r, $4, $5.host, $7, $8.src_os,
2032126353Smlaier			    $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
2033145840Smlaier			    $9.uid, $9.gid, $9.icmpspec, "");
2034126353Smlaier		}
2035126353Smlaier		;
2036126353Smlaier
2037171172Smlaierfilter_opts	:	{
2038171172Smlaier				bzero(&filter_opts, sizeof filter_opts);
2039171172Smlaier				filter_opts.rtableid = -1;
2040171172Smlaier			}
2041130617Smlaier		    filter_opts_l
2042126353Smlaier			{ $$ = filter_opts; }
2043126353Smlaier		| /* empty */	{
2044126353Smlaier			bzero(&filter_opts, sizeof filter_opts);
2045171172Smlaier			filter_opts.rtableid = -1;
2046126353Smlaier			$$ = filter_opts;
2047126353Smlaier		}
2048126353Smlaier		;
2049126353Smlaier
2050126353Smlaierfilter_opts_l	: filter_opts_l filter_opt
2051126353Smlaier		| filter_opt
2052126353Smlaier		;
2053126353Smlaier
2054126353Smlaierfilter_opt	: USER uids {
2055126353Smlaier			if (filter_opts.uid)
2056126353Smlaier				$2->tail->next = filter_opts.uid;
2057126353Smlaier			filter_opts.uid = $2;
2058126353Smlaier		}
2059126353Smlaier		| GROUP gids {
2060126353Smlaier			if (filter_opts.gid)
2061126353Smlaier				$2->tail->next = filter_opts.gid;
2062126353Smlaier			filter_opts.gid = $2;
2063126353Smlaier		}
2064126353Smlaier		| flags {
2065126353Smlaier			if (filter_opts.marker & FOM_FLAGS) {
2066126353Smlaier				yyerror("flags cannot be redefined");
2067126353Smlaier				YYERROR;
2068126353Smlaier			}
2069126353Smlaier			filter_opts.marker |= FOM_FLAGS;
2070126353Smlaier			filter_opts.flags.b1 |= $1.b1;
2071126353Smlaier			filter_opts.flags.b2 |= $1.b2;
2072126353Smlaier			filter_opts.flags.w |= $1.w;
2073126353Smlaier			filter_opts.flags.w2 |= $1.w2;
2074126353Smlaier		}
2075126353Smlaier		| icmpspec {
2076126353Smlaier			if (filter_opts.marker & FOM_ICMP) {
2077126353Smlaier				yyerror("icmp-type cannot be redefined");
2078126353Smlaier				YYERROR;
2079126353Smlaier			}
2080126353Smlaier			filter_opts.marker |= FOM_ICMP;
2081126353Smlaier			filter_opts.icmpspec = $1;
2082126353Smlaier		}
2083126353Smlaier		| tos {
2084126353Smlaier			if (filter_opts.marker & FOM_TOS) {
2085126353Smlaier				yyerror("tos cannot be redefined");
2086126353Smlaier				YYERROR;
2087126353Smlaier			}
2088126353Smlaier			filter_opts.marker |= FOM_TOS;
2089126353Smlaier			filter_opts.tos = $1;
2090126353Smlaier		}
2091126353Smlaier		| keep {
2092126353Smlaier			if (filter_opts.marker & FOM_KEEP) {
2093126353Smlaier				yyerror("modulate or keep cannot be redefined");
2094126353Smlaier				YYERROR;
2095126353Smlaier			}
2096126353Smlaier			filter_opts.marker |= FOM_KEEP;
2097126353Smlaier			filter_opts.keep.action = $1.action;
2098126353Smlaier			filter_opts.keep.options = $1.options;
2099126353Smlaier		}
2100126353Smlaier		| FRAGMENT {
2101126353Smlaier			filter_opts.fragment = 1;
2102126353Smlaier		}
2103126353Smlaier		| ALLOWOPTS {
2104126353Smlaier			filter_opts.allowopts = 1;
2105126353Smlaier		}
2106126353Smlaier		| label	{
2107126353Smlaier			if (filter_opts.label) {
2108126353Smlaier				yyerror("label cannot be redefined");
2109126353Smlaier				YYERROR;
2110126353Smlaier			}
2111126353Smlaier			filter_opts.label = $1;
2112126353Smlaier		}
2113126353Smlaier		| qname	{
2114126353Smlaier			if (filter_opts.queues.qname) {
2115126353Smlaier				yyerror("queue cannot be redefined");
2116126353Smlaier				YYERROR;
2117126353Smlaier			}
2118126353Smlaier			filter_opts.queues = $1;
2119126353Smlaier		}
2120126353Smlaier		| TAG string				{
2121126353Smlaier			filter_opts.tag = $2;
2122126353Smlaier		}
2123126353Smlaier		| not TAGGED string			{
2124126353Smlaier			filter_opts.match_tag = $3;
2125126353Smlaier			filter_opts.match_tag_not = $1;
2126126353Smlaier		}
2127145840Smlaier		| PROBABILITY STRING			{
2128145840Smlaier			char	*e;
2129145840Smlaier			double	 p = strtod($2, &e);
2130145840Smlaier
2131145840Smlaier			if (*e == '%') {
2132145840Smlaier				p *= 0.01;
2133145840Smlaier				e++;
2134145840Smlaier			}
2135145840Smlaier			if (*e) {
2136145840Smlaier				yyerror("invalid probability: %s", $2);
2137145840Smlaier				free($2);
2138145840Smlaier				YYERROR;
2139145840Smlaier			}
2140145840Smlaier			p = floor(p * (UINT_MAX+1.0) + 0.5);
2141145840Smlaier			if (p < 1.0 || p >= (UINT_MAX+1.0)) {
2142145840Smlaier				yyerror("invalid probability: %s", $2);
2143145840Smlaier				free($2);
2144145840Smlaier				YYERROR;
2145145840Smlaier			}
2146145840Smlaier			filter_opts.prob = (u_int32_t)p;
2147145840Smlaier			free($2);
2148145840Smlaier		}
2149171172Smlaier		| RTABLE number				{
2150178894Sjulian#ifndef __FreeBSD__
2151171172Smlaier			if ($2 > RT_TABLEID_MAX || $2 < 0) {
2152171172Smlaier				yyerror("invalid rtable id");
2153171172Smlaier				YYERROR;
2154171172Smlaier			}
2155178894Sjulian#endif
2156171172Smlaier			filter_opts.rtableid = $2;
2157171172Smlaier		}
2158126353Smlaier		;
2159126353Smlaier
2160126353Smlaieraction		: PASS			{ $$.b1 = PF_PASS; $$.b2 = $$.w = 0; }
2161126353Smlaier		| BLOCK blockspec	{ $$ = $2; $$.b1 = PF_DROP; }
2162126353Smlaier		;
2163126353Smlaier
2164126353Smlaierblockspec	: /* empty */		{
2165126353Smlaier			$$.b2 = blockpolicy;
2166126353Smlaier			$$.w = returnicmpdefault;
2167126353Smlaier			$$.w2 = returnicmp6default;
2168126353Smlaier		}
2169126353Smlaier		| DROP			{
2170126353Smlaier			$$.b2 = PFRULE_DROP;
2171126353Smlaier			$$.w = 0;
2172126353Smlaier			$$.w2 = 0;
2173126353Smlaier		}
2174126353Smlaier		| RETURNRST		{
2175126353Smlaier			$$.b2 = PFRULE_RETURNRST;
2176126353Smlaier			$$.w = 0;
2177126353Smlaier			$$.w2 = 0;
2178126353Smlaier		}
2179126353Smlaier		| RETURNRST '(' TTL number ')'	{
2180126353Smlaier			if ($4 > 255) {
2181126353Smlaier				yyerror("illegal ttl value %d", $4);
2182126353Smlaier				YYERROR;
2183126353Smlaier			}
2184126353Smlaier			$$.b2 = PFRULE_RETURNRST;
2185126353Smlaier			$$.w = $4;
2186126353Smlaier			$$.w2 = 0;
2187126353Smlaier		}
2188126353Smlaier		| RETURNICMP		{
2189126353Smlaier			$$.b2 = PFRULE_RETURNICMP;
2190126353Smlaier			$$.w = returnicmpdefault;
2191126353Smlaier			$$.w2 = returnicmp6default;
2192126353Smlaier		}
2193126353Smlaier		| RETURNICMP6		{
2194126353Smlaier			$$.b2 = PFRULE_RETURNICMP;
2195126353Smlaier			$$.w = returnicmpdefault;
2196126353Smlaier			$$.w2 = returnicmp6default;
2197126353Smlaier		}
2198126353Smlaier		| RETURNICMP '(' STRING ')'	{
2199126353Smlaier			$$.b2 = PFRULE_RETURNICMP;
2200130617Smlaier			if (!($$.w = parseicmpspec($3, AF_INET))) {
2201130617Smlaier				free($3);
2202126353Smlaier				YYERROR;
2203130617Smlaier			}
2204130617Smlaier			free($3);
2205126353Smlaier			$$.w2 = returnicmp6default;
2206126353Smlaier		}
2207126353Smlaier		| RETURNICMP6 '(' STRING ')'	{
2208126353Smlaier			$$.b2 = PFRULE_RETURNICMP;
2209126353Smlaier			$$.w = returnicmpdefault;
2210130617Smlaier			if (!($$.w2 = parseicmpspec($3, AF_INET6))) {
2211130617Smlaier				free($3);
2212126353Smlaier				YYERROR;
2213130617Smlaier			}
2214130617Smlaier			free($3);
2215126353Smlaier		}
2216126353Smlaier		| RETURNICMP '(' STRING comma STRING ')' {
2217126353Smlaier			$$.b2 = PFRULE_RETURNICMP;
2218130617Smlaier			if (!($$.w = parseicmpspec($3, AF_INET)) ||
2219130617Smlaier			    !($$.w2 = parseicmpspec($5, AF_INET6))) {
2220130617Smlaier				free($3);
2221130617Smlaier				free($5);
2222126353Smlaier				YYERROR;
2223130617Smlaier			}
2224130617Smlaier			free($3);
2225130617Smlaier			free($5);
2226126353Smlaier		}
2227126353Smlaier		| RETURN {
2228126353Smlaier			$$.b2 = PFRULE_RETURN;
2229126353Smlaier			$$.w = returnicmpdefault;
2230126353Smlaier			$$.w2 = returnicmp6default;
2231126353Smlaier		}
2232126353Smlaier		;
2233126353Smlaier
2234126353Smlaierdir		: /* empty */			{ $$ = 0; }
2235126353Smlaier		| IN				{ $$ = PF_IN; }
2236126353Smlaier		| OUT				{ $$ = PF_OUT; }
2237126353Smlaier		;
2238126353Smlaier
2239171172Smlaierquick		: /* empty */			{ $$.quick = 0; }
2240171172Smlaier		| QUICK				{ $$.quick = 1; }
2241126353Smlaier		;
2242126353Smlaier
2243171172Smlaierlogquick	: /* empty */	{ $$.log = 0; $$.quick = 0; $$.logif = 0; }
2244171172Smlaier		| log		{ $$ = $1; $$.quick = 0; }
2245171172Smlaier		| QUICK		{ $$.quick = 1; $$.log = 0; $$.logif = 0; }
2246171172Smlaier		| log QUICK	{ $$ = $1; $$.quick = 1; }
2247171172Smlaier		| QUICK log	{ $$ = $2; $$.quick = 1; }
2248126353Smlaier		;
2249126353Smlaier
2250171172Smlaierlog		: LOG			{ $$.log = PF_LOG; $$.logif = 0; }
2251171172Smlaier		| LOG '(' logopts ')'	{
2252171172Smlaier			$$.log = PF_LOG | $3.log;
2253171172Smlaier			$$.logif = $3.logif;
2254171172Smlaier		}
2255171172Smlaier		;
2256171172Smlaier
2257171172Smlaierlogopts		: logopt			{ $$ = $1; }
2258171172Smlaier		| logopts comma logopt		{
2259171172Smlaier			$$.log = $1.log | $3.log;
2260171172Smlaier			$$.logif = $3.logif;
2261171172Smlaier			if ($$.logif == 0)
2262171172Smlaier				$$.logif = $1.logif;
2263171172Smlaier		}
2264171172Smlaier		;
2265171172Smlaier
2266171172Smlaierlogopt		: ALL		{ $$.log = PF_LOG_ALL; $$.logif = 0; }
2267171172Smlaier		| USER		{ $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
2268171172Smlaier		| GROUP		{ $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
2269171172Smlaier		| TO string	{
2270171172Smlaier			const char	*errstr;
2271171172Smlaier			u_int		 i;
2272171172Smlaier
2273171172Smlaier			$$.log = 0;
2274171172Smlaier			if (strncmp($2, "pflog", 5)) {
2275171172Smlaier				yyerror("%s: should be a pflog interface", $2);
2276171172Smlaier				free($2);
2277171172Smlaier				YYERROR;
2278171172Smlaier			}
2279171172Smlaier			i = strtonum($2 + 5, 0, 255, &errstr);
2280171172Smlaier			if (errstr) {
2281171172Smlaier				yyerror("%s: %s", $2, errstr);
2282171172Smlaier				free($2);
2283171172Smlaier				YYERROR;
2284171172Smlaier			}
2285171172Smlaier			free($2);
2286171172Smlaier			$$.logif = i;
2287171172Smlaier		}
2288171172Smlaier		;
2289171172Smlaier
2290126353Smlaierinterface	: /* empty */			{ $$ = NULL; }
2291126353Smlaier		| ON if_item_not		{ $$ = $2; }
2292126353Smlaier		| ON '{' if_list '}'		{ $$ = $3; }
2293126353Smlaier		;
2294126353Smlaier
2295126353Smlaierif_list		: if_item_not			{ $$ = $1; }
2296126353Smlaier		| if_list comma if_item_not	{
2297126353Smlaier			$1->tail->next = $3;
2298126353Smlaier			$1->tail = $3;
2299126353Smlaier			$$ = $1;
2300126353Smlaier		}
2301126353Smlaier		;
2302126353Smlaier
2303126353Smlaierif_item_not	: not if_item			{ $$ = $2; $$->not = $1; }
2304126353Smlaier		;
2305126353Smlaier
2306126353Smlaierif_item		: STRING			{
2307126353Smlaier			struct node_host	*n;
2308126353Smlaier
2309126353Smlaier			$$ = calloc(1, sizeof(struct node_if));
2310126353Smlaier			if ($$ == NULL)
2311126353Smlaier				err(1, "if_item: calloc");
2312126353Smlaier			if (strlcpy($$->ifname, $1, sizeof($$->ifname)) >=
2313126353Smlaier			    sizeof($$->ifname)) {
2314130617Smlaier				free($1);
2315126353Smlaier				free($$);
2316126353Smlaier				yyerror("interface name too long");
2317126353Smlaier				YYERROR;
2318126353Smlaier			}
2319145840Smlaier
2320171172Smlaier			if ((n = ifa_exists($1)) != NULL)
2321145840Smlaier				$$->ifa_flags = n->ifa_flags;
2322145840Smlaier
2323130617Smlaier			free($1);
2324126353Smlaier			$$->not = 0;
2325126353Smlaier			$$->next = NULL;
2326126353Smlaier			$$->tail = $$;
2327126353Smlaier		}
2328126353Smlaier		;
2329126353Smlaier
2330126353Smlaieraf		: /* empty */			{ $$ = 0; }
2331126353Smlaier		| INET				{ $$ = AF_INET; }
2332126353Smlaier		| INET6				{ $$ = AF_INET6; }
2333130617Smlaier		;
2334126353Smlaier
2335126353Smlaierproto		: /* empty */			{ $$ = NULL; }
2336126353Smlaier		| PROTO proto_item		{ $$ = $2; }
2337126353Smlaier		| PROTO '{' proto_list '}'	{ $$ = $3; }
2338126353Smlaier		;
2339126353Smlaier
2340126353Smlaierproto_list	: proto_item			{ $$ = $1; }
2341126353Smlaier		| proto_list comma proto_item	{
2342126353Smlaier			$1->tail->next = $3;
2343126353Smlaier			$1->tail = $3;
2344126353Smlaier			$$ = $1;
2345126353Smlaier		}
2346126353Smlaier		;
2347126353Smlaier
2348126353Smlaierproto_item	: STRING			{
2349126353Smlaier			u_int8_t	pr;
2350126353Smlaier			u_long		ulval;
2351126353Smlaier
2352126353Smlaier			if (atoul($1, &ulval) == 0) {
2353126353Smlaier				if (ulval > 255) {
2354126353Smlaier					yyerror("protocol outside range");
2355130617Smlaier					free($1);
2356126353Smlaier					YYERROR;
2357126353Smlaier				}
2358126353Smlaier				pr = (u_int8_t)ulval;
2359126353Smlaier			} else {
2360126353Smlaier				struct protoent	*p;
2361126353Smlaier
2362126353Smlaier				p = getprotobyname($1);
2363126353Smlaier				if (p == NULL) {
2364126353Smlaier					yyerror("unknown protocol %s", $1);
2365130617Smlaier					free($1);
2366126353Smlaier					YYERROR;
2367126353Smlaier				}
2368126353Smlaier				pr = p->p_proto;
2369126353Smlaier			}
2370130617Smlaier			free($1);
2371126353Smlaier			if (pr == 0) {
2372126353Smlaier				yyerror("proto 0 cannot be used");
2373126353Smlaier				YYERROR;
2374126353Smlaier			}
2375126353Smlaier			$$ = calloc(1, sizeof(struct node_proto));
2376126353Smlaier			if ($$ == NULL)
2377126353Smlaier				err(1, "proto_item: calloc");
2378126353Smlaier			$$->proto = pr;
2379126353Smlaier			$$->next = NULL;
2380126353Smlaier			$$->tail = $$;
2381126353Smlaier		}
2382126353Smlaier		;
2383126353Smlaier
2384126353Smlaierfromto		: ALL				{
2385126353Smlaier			$$.src.host = NULL;
2386126353Smlaier			$$.src.port = NULL;
2387126353Smlaier			$$.dst.host = NULL;
2388126353Smlaier			$$.dst.port = NULL;
2389126353Smlaier			$$.src_os = NULL;
2390126353Smlaier		}
2391126353Smlaier		| from os to			{
2392126353Smlaier			$$.src = $1;
2393126353Smlaier			$$.src_os = $2;
2394126353Smlaier			$$.dst = $3;
2395126353Smlaier		}
2396126353Smlaier		;
2397126353Smlaier
2398126353Smlaieros		: /* empty */			{ $$ = NULL; }
2399126353Smlaier		| OS xos			{ $$ = $2; }
2400126353Smlaier		| OS '{' os_list '}'		{ $$ = $3; }
2401126353Smlaier		;
2402126353Smlaier
2403126353Smlaierxos		: STRING {
2404126353Smlaier			$$ = calloc(1, sizeof(struct node_os));
2405126353Smlaier			if ($$ == NULL)
2406126353Smlaier				err(1, "os: calloc");
2407126353Smlaier			$$->os = $1;
2408126353Smlaier			$$->tail = $$;
2409126353Smlaier		}
2410126353Smlaier		;
2411126353Smlaier
2412126353Smlaieros_list		: xos				{ $$ = $1; }
2413126353Smlaier		| os_list comma xos		{
2414126353Smlaier			$1->tail->next = $3;
2415126353Smlaier			$1->tail = $3;
2416126353Smlaier			$$ = $1;
2417126353Smlaier		}
2418126353Smlaier		;
2419126353Smlaier
2420126353Smlaierfrom		: /* empty */			{
2421126353Smlaier			$$.host = NULL;
2422126353Smlaier			$$.port = NULL;
2423126353Smlaier		}
2424126353Smlaier		| FROM ipportspec		{
2425126353Smlaier			$$ = $2;
2426126353Smlaier		}
2427126353Smlaier		;
2428126353Smlaier
2429126353Smlaierto		: /* empty */			{
2430126353Smlaier			$$.host = NULL;
2431126353Smlaier			$$.port = NULL;
2432126353Smlaier		}
2433126353Smlaier		| TO ipportspec		{
2434171172Smlaier			if (disallow_urpf_failed($2.host, "\"urpf-failed\" is "
2435171172Smlaier			    "not permitted in a destination address"))
2436171172Smlaier				YYERROR;
2437126353Smlaier			$$ = $2;
2438126353Smlaier		}
2439126353Smlaier		;
2440126353Smlaier
2441126353Smlaieripportspec	: ipspec			{
2442126353Smlaier			$$.host = $1;
2443126353Smlaier			$$.port = NULL;
2444126353Smlaier		}
2445126353Smlaier		| ipspec PORT portspec		{
2446126353Smlaier			$$.host = $1;
2447126353Smlaier			$$.port = $3;
2448126353Smlaier		}
2449126353Smlaier		| PORT portspec			{
2450126353Smlaier			$$.host = NULL;
2451126353Smlaier			$$.port = $2;
2452126353Smlaier		}
2453126353Smlaier		;
2454126353Smlaier
2455126353Smlaieripspec		: ANY				{ $$ = NULL; }
2456126353Smlaier		| xhost				{ $$ = $1; }
2457126353Smlaier		| '{' host_list '}'		{ $$ = $2; }
2458126353Smlaier		;
2459126353Smlaier
2460171172Smlaierhost_list	: ipspec			{ $$ = $1; }
2461171172Smlaier		| host_list comma ipspec	{
2462126353Smlaier			if ($3 == NULL)
2463126353Smlaier				$$ = $1;
2464126353Smlaier			else if ($1 == NULL)
2465126353Smlaier				$$ = $3;
2466126353Smlaier			else {
2467126353Smlaier				$1->tail->next = $3;
2468126353Smlaier				$1->tail = $3->tail;
2469126353Smlaier				$$ = $1;
2470126353Smlaier			}
2471126353Smlaier		}
2472126353Smlaier		;
2473126353Smlaier
2474126353Smlaierxhost		: not host			{
2475126353Smlaier			struct node_host	*n;
2476126353Smlaier
2477126353Smlaier			for (n = $2; n != NULL; n = n->next)
2478126353Smlaier				n->not = $1;
2479126353Smlaier			$$ = $2;
2480126353Smlaier		}
2481171172Smlaier		| not NOROUTE			{
2482126353Smlaier			$$ = calloc(1, sizeof(struct node_host));
2483126353Smlaier			if ($$ == NULL)
2484126353Smlaier				err(1, "xhost: calloc");
2485126353Smlaier			$$->addr.type = PF_ADDR_NOROUTE;
2486126353Smlaier			$$->next = NULL;
2487171172Smlaier			$$->not = $1;
2488126353Smlaier			$$->tail = $$;
2489126353Smlaier		}
2490171172Smlaier		| not URPFFAILED		{
2491171172Smlaier			$$ = calloc(1, sizeof(struct node_host));
2492171172Smlaier			if ($$ == NULL)
2493171172Smlaier				err(1, "xhost: calloc");
2494171172Smlaier			$$->addr.type = PF_ADDR_URPFFAILED;
2495171172Smlaier			$$->next = NULL;
2496171172Smlaier			$$->not = $1;
2497171172Smlaier			$$->tail = $$;
2498171172Smlaier		}
2499126353Smlaier		;
2500126353Smlaier
2501126353Smlaierhost		: STRING			{
2502126353Smlaier			if (($$ = host($1)) == NULL)	{
2503126353Smlaier				/* error. "any" is handled elsewhere */
2504130617Smlaier				free($1);
2505126353Smlaier				yyerror("could not parse host specification");
2506126353Smlaier				YYERROR;
2507126353Smlaier			}
2508130617Smlaier			free($1);
2509126353Smlaier
2510126353Smlaier		}
2511126353Smlaier		| STRING '/' number		{
2512126353Smlaier			char	*buf;
2513126353Smlaier
2514126353Smlaier			if (asprintf(&buf, "%s/%u", $1, $3) == -1)
2515126353Smlaier				err(1, "host: asprintf");
2516130617Smlaier			free($1);
2517126353Smlaier			if (($$ = host(buf)) == NULL)	{
2518126353Smlaier				/* error. "any" is handled elsewhere */
2519126353Smlaier				free(buf);
2520126353Smlaier				yyerror("could not parse host specification");
2521126353Smlaier				YYERROR;
2522126353Smlaier			}
2523126353Smlaier			free(buf);
2524126353Smlaier		}
2525126353Smlaier		| dynaddr
2526126353Smlaier		| dynaddr '/' number		{
2527126353Smlaier			struct node_host	*n;
2528126353Smlaier
2529126353Smlaier			$$ = $1;
2530126353Smlaier			for (n = $1; n != NULL; n = n->next)
2531126353Smlaier				set_ipmask(n, $3);
2532126353Smlaier		}
2533126353Smlaier		| '<' STRING '>'	{
2534126353Smlaier			if (strlen($2) >= PF_TABLE_NAME_SIZE) {
2535130617Smlaier				yyerror("table name '%s' too long", $2);
2536130617Smlaier				free($2);
2537126353Smlaier				YYERROR;
2538126353Smlaier			}
2539126353Smlaier			$$ = calloc(1, sizeof(struct node_host));
2540126353Smlaier			if ($$ == NULL)
2541126353Smlaier				err(1, "host: calloc");
2542126353Smlaier			$$->addr.type = PF_ADDR_TABLE;
2543126353Smlaier			if (strlcpy($$->addr.v.tblname, $2,
2544126353Smlaier			    sizeof($$->addr.v.tblname)) >=
2545126353Smlaier			    sizeof($$->addr.v.tblname))
2546126353Smlaier				errx(1, "host: strlcpy");
2547130617Smlaier			free($2);
2548126353Smlaier			$$->next = NULL;
2549126353Smlaier			$$->tail = $$;
2550126353Smlaier		}
2551145840Smlaier		| ROUTE	STRING		{
2552145840Smlaier			$$ = calloc(1, sizeof(struct node_host));
2553145840Smlaier			if ($$ == NULL) {
2554145840Smlaier				free($2);
2555145840Smlaier				err(1, "host: calloc");
2556145840Smlaier			}
2557145840Smlaier			$$->addr.type = PF_ADDR_RTLABEL;
2558145840Smlaier			if (strlcpy($$->addr.v.rtlabelname, $2,
2559145840Smlaier			    sizeof($$->addr.v.rtlabelname)) >=
2560145840Smlaier			    sizeof($$->addr.v.rtlabelname)) {
2561145840Smlaier				yyerror("route label too long, max %u chars",
2562145840Smlaier				    sizeof($$->addr.v.rtlabelname) - 1);
2563145840Smlaier				free($2);
2564145840Smlaier				free($$);
2565145840Smlaier				YYERROR;
2566145840Smlaier			}
2567145840Smlaier			$$->next = NULL;
2568145840Smlaier			$$->tail = $$;
2569145840Smlaier			free($2);
2570145840Smlaier		}
2571126353Smlaier		;
2572126353Smlaier
2573126353Smlaiernumber		: STRING			{
2574126353Smlaier			u_long	ulval;
2575126353Smlaier
2576126353Smlaier			if (atoul($1, &ulval) == -1) {
2577126353Smlaier				yyerror("%s is not a number", $1);
2578130617Smlaier				free($1);
2579126353Smlaier				YYERROR;
2580126353Smlaier			} else
2581126353Smlaier				$$ = ulval;
2582130617Smlaier			free($1);
2583126353Smlaier		}
2584126353Smlaier		;
2585126353Smlaier
2586126353Smlaierdynaddr		: '(' STRING ')'		{
2587130617Smlaier			int	 flags = 0;
2588130617Smlaier			char	*p, *op;
2589130617Smlaier
2590130617Smlaier			op = $2;
2591145840Smlaier			if (!isalpha(op[0])) {
2592145840Smlaier				yyerror("invalid interface name '%s'", op);
2593145840Smlaier				free(op);
2594145840Smlaier				YYERROR;
2595145840Smlaier			}
2596130617Smlaier			while ((p = strrchr($2, ':')) != NULL) {
2597130617Smlaier				if (!strcmp(p+1, "network"))
2598130617Smlaier					flags |= PFI_AFLAG_NETWORK;
2599130617Smlaier				else if (!strcmp(p+1, "broadcast"))
2600130617Smlaier					flags |= PFI_AFLAG_BROADCAST;
2601130617Smlaier				else if (!strcmp(p+1, "peer"))
2602130617Smlaier					flags |= PFI_AFLAG_PEER;
2603130617Smlaier				else if (!strcmp(p+1, "0"))
2604130617Smlaier					flags |= PFI_AFLAG_NOALIAS;
2605130617Smlaier				else {
2606130617Smlaier					yyerror("interface %s has bad modifier",
2607130617Smlaier					    $2);
2608130617Smlaier					free(op);
2609130617Smlaier					YYERROR;
2610130617Smlaier				}
2611130617Smlaier				*p = '\0';
2612130617Smlaier			}
2613130617Smlaier			if (flags & (flags - 1) & PFI_AFLAG_MODEMASK) {
2614130617Smlaier				free(op);
2615130617Smlaier				yyerror("illegal combination of "
2616130617Smlaier				    "interface modifiers");
2617130617Smlaier				YYERROR;
2618130617Smlaier			}
2619126353Smlaier			$$ = calloc(1, sizeof(struct node_host));
2620126353Smlaier			if ($$ == NULL)
2621126353Smlaier				err(1, "address: calloc");
2622126353Smlaier			$$->af = 0;
2623126353Smlaier			set_ipmask($$, 128);
2624126353Smlaier			$$->addr.type = PF_ADDR_DYNIFTL;
2625130617Smlaier			$$->addr.iflags = flags;
2626126353Smlaier			if (strlcpy($$->addr.v.ifname, $2,
2627126353Smlaier			    sizeof($$->addr.v.ifname)) >=
2628126353Smlaier			    sizeof($$->addr.v.ifname)) {
2629130617Smlaier				free(op);
2630126353Smlaier				free($$);
2631126353Smlaier				yyerror("interface name too long");
2632126353Smlaier				YYERROR;
2633126353Smlaier			}
2634130617Smlaier			free(op);
2635126353Smlaier			$$->next = NULL;
2636126353Smlaier			$$->tail = $$;
2637126353Smlaier		}
2638126353Smlaier		;
2639126353Smlaier
2640126353Smlaierportspec	: port_item			{ $$ = $1; }
2641126353Smlaier		| '{' port_list '}'		{ $$ = $2; }
2642126353Smlaier		;
2643126353Smlaier
2644126353Smlaierport_list	: port_item			{ $$ = $1; }
2645126353Smlaier		| port_list comma port_item	{
2646126353Smlaier			$1->tail->next = $3;
2647126353Smlaier			$1->tail = $3;
2648126353Smlaier			$$ = $1;
2649126353Smlaier		}
2650126353Smlaier		;
2651126353Smlaier
2652126353Smlaierport_item	: port				{
2653126353Smlaier			$$ = calloc(1, sizeof(struct node_port));
2654126353Smlaier			if ($$ == NULL)
2655126353Smlaier				err(1, "port_item: calloc");
2656126353Smlaier			$$->port[0] = $1.a;
2657126353Smlaier			$$->port[1] = $1.b;
2658126353Smlaier			if ($1.t)
2659126353Smlaier				$$->op = PF_OP_RRG;
2660126353Smlaier			else
2661126353Smlaier				$$->op = PF_OP_EQ;
2662126353Smlaier			$$->next = NULL;
2663126353Smlaier			$$->tail = $$;
2664126353Smlaier		}
2665126353Smlaier		| unaryop port		{
2666126353Smlaier			if ($2.t) {
2667126353Smlaier				yyerror("':' cannot be used with an other "
2668126353Smlaier				    "port operator");
2669126353Smlaier				YYERROR;
2670126353Smlaier			}
2671126353Smlaier			$$ = calloc(1, sizeof(struct node_port));
2672126353Smlaier			if ($$ == NULL)
2673126353Smlaier				err(1, "port_item: calloc");
2674126353Smlaier			$$->port[0] = $2.a;
2675126353Smlaier			$$->port[1] = $2.b;
2676126353Smlaier			$$->op = $1;
2677126353Smlaier			$$->next = NULL;
2678126353Smlaier			$$->tail = $$;
2679126353Smlaier		}
2680126353Smlaier		| port PORTBINARY port		{
2681126353Smlaier			if ($1.t || $3.t) {
2682126353Smlaier				yyerror("':' cannot be used with an other "
2683126353Smlaier				    "port operator");
2684126353Smlaier				YYERROR;
2685126353Smlaier			}
2686126353Smlaier			$$ = calloc(1, sizeof(struct node_port));
2687126353Smlaier			if ($$ == NULL)
2688126353Smlaier				err(1, "port_item: calloc");
2689126353Smlaier			$$->port[0] = $1.a;
2690126353Smlaier			$$->port[1] = $3.a;
2691126353Smlaier			$$->op = $2;
2692126353Smlaier			$$->next = NULL;
2693126353Smlaier			$$->tail = $$;
2694126353Smlaier		}
2695126353Smlaier		;
2696126353Smlaier
2697126353Smlaierport		: STRING			{
2698126353Smlaier			char	*p = strchr($1, ':');
2699126353Smlaier
2700126353Smlaier			if (p == NULL) {
2701171172Smlaier				if (($$.a = getservice($1)) == -1) {
2702171172Smlaier					free($1);
2703171172Smlaier					YYERROR;
2704126353Smlaier				}
2705171172Smlaier				$$.b = $$.t = 0;
2706126353Smlaier			} else {
2707126353Smlaier				int port[2];
2708126353Smlaier
2709126353Smlaier				*p++ = 0;
2710126353Smlaier				if ((port[0] = getservice($1)) == -1 ||
2711130617Smlaier				    (port[1] = getservice(p)) == -1) {
2712130617Smlaier					free($1);
2713126353Smlaier					YYERROR;
2714130617Smlaier				}
2715126353Smlaier				$$.a = port[0];
2716126353Smlaier				$$.b = port[1];
2717126353Smlaier				$$.t = PF_OP_RRG;
2718126353Smlaier			}
2719130617Smlaier			free($1);
2720126353Smlaier		}
2721126353Smlaier		;
2722126353Smlaier
2723126353Smlaieruids		: uid_item			{ $$ = $1; }
2724126353Smlaier		| '{' uid_list '}'		{ $$ = $2; }
2725126353Smlaier		;
2726126353Smlaier
2727126353Smlaieruid_list	: uid_item			{ $$ = $1; }
2728126353Smlaier		| uid_list comma uid_item	{
2729126353Smlaier			$1->tail->next = $3;
2730126353Smlaier			$1->tail = $3;
2731126353Smlaier			$$ = $1;
2732126353Smlaier		}
2733126353Smlaier		;
2734126353Smlaier
2735126353Smlaieruid_item	: uid				{
2736126353Smlaier			$$ = calloc(1, sizeof(struct node_uid));
2737126353Smlaier			if ($$ == NULL)
2738126353Smlaier				err(1, "uid_item: calloc");
2739126353Smlaier			$$->uid[0] = $1;
2740126353Smlaier			$$->uid[1] = $1;
2741126353Smlaier			$$->op = PF_OP_EQ;
2742126353Smlaier			$$->next = NULL;
2743126353Smlaier			$$->tail = $$;
2744126353Smlaier		}
2745126353Smlaier		| unaryop uid			{
2746126353Smlaier			if ($2 == UID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
2747126353Smlaier				yyerror("user unknown requires operator = or "
2748126353Smlaier				    "!=");
2749126353Smlaier				YYERROR;
2750126353Smlaier			}
2751126353Smlaier			$$ = calloc(1, sizeof(struct node_uid));
2752126353Smlaier			if ($$ == NULL)
2753126353Smlaier				err(1, "uid_item: calloc");
2754126353Smlaier			$$->uid[0] = $2;
2755126353Smlaier			$$->uid[1] = $2;
2756126353Smlaier			$$->op = $1;
2757126353Smlaier			$$->next = NULL;
2758126353Smlaier			$$->tail = $$;
2759126353Smlaier		}
2760126353Smlaier		| uid PORTBINARY uid		{
2761126353Smlaier			if ($1 == UID_MAX || $3 == UID_MAX) {
2762126353Smlaier				yyerror("user unknown requires operator = or "
2763126353Smlaier				    "!=");
2764126353Smlaier				YYERROR;
2765126353Smlaier			}
2766126353Smlaier			$$ = calloc(1, sizeof(struct node_uid));
2767126353Smlaier			if ($$ == NULL)
2768126353Smlaier				err(1, "uid_item: calloc");
2769126353Smlaier			$$->uid[0] = $1;
2770126353Smlaier			$$->uid[1] = $3;
2771126353Smlaier			$$->op = $2;
2772126353Smlaier			$$->next = NULL;
2773126353Smlaier			$$->tail = $$;
2774126353Smlaier		}
2775126353Smlaier		;
2776126353Smlaier
2777126353Smlaieruid		: STRING			{
2778126353Smlaier			u_long	ulval;
2779126353Smlaier
2780126353Smlaier			if (atoul($1, &ulval) == -1) {
2781126353Smlaier				if (!strcmp($1, "unknown"))
2782126353Smlaier					$$ = UID_MAX;
2783126353Smlaier				else {
2784126353Smlaier					struct passwd	*pw;
2785126353Smlaier
2786126353Smlaier					if ((pw = getpwnam($1)) == NULL) {
2787126353Smlaier						yyerror("unknown user %s", $1);
2788130617Smlaier						free($1);
2789126353Smlaier						YYERROR;
2790126353Smlaier					}
2791126353Smlaier					$$ = pw->pw_uid;
2792126353Smlaier				}
2793126353Smlaier			} else {
2794126353Smlaier				if (ulval >= UID_MAX) {
2795130617Smlaier					free($1);
2796126353Smlaier					yyerror("illegal uid value %lu", ulval);
2797126353Smlaier					YYERROR;
2798126353Smlaier				}
2799126353Smlaier				$$ = ulval;
2800126353Smlaier			}
2801130617Smlaier			free($1);
2802126353Smlaier		}
2803126353Smlaier		;
2804126353Smlaier
2805126353Smlaiergids		: gid_item			{ $$ = $1; }
2806126353Smlaier		| '{' gid_list '}'		{ $$ = $2; }
2807126353Smlaier		;
2808126353Smlaier
2809126353Smlaiergid_list	: gid_item			{ $$ = $1; }
2810126353Smlaier		| gid_list comma gid_item	{
2811126353Smlaier			$1->tail->next = $3;
2812126353Smlaier			$1->tail = $3;
2813126353Smlaier			$$ = $1;
2814126353Smlaier		}
2815126353Smlaier		;
2816126353Smlaier
2817126353Smlaiergid_item	: gid				{
2818126353Smlaier			$$ = calloc(1, sizeof(struct node_gid));
2819126353Smlaier			if ($$ == NULL)
2820126353Smlaier				err(1, "gid_item: calloc");
2821126353Smlaier			$$->gid[0] = $1;
2822126353Smlaier			$$->gid[1] = $1;
2823126353Smlaier			$$->op = PF_OP_EQ;
2824126353Smlaier			$$->next = NULL;
2825126353Smlaier			$$->tail = $$;
2826126353Smlaier		}
2827126353Smlaier		| unaryop gid			{
2828126353Smlaier			if ($2 == GID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
2829126353Smlaier				yyerror("group unknown requires operator = or "
2830126353Smlaier				    "!=");
2831126353Smlaier				YYERROR;
2832126353Smlaier			}
2833126353Smlaier			$$ = calloc(1, sizeof(struct node_gid));
2834126353Smlaier			if ($$ == NULL)
2835126353Smlaier				err(1, "gid_item: calloc");
2836126353Smlaier			$$->gid[0] = $2;
2837126353Smlaier			$$->gid[1] = $2;
2838126353Smlaier			$$->op = $1;
2839126353Smlaier			$$->next = NULL;
2840126353Smlaier			$$->tail = $$;
2841126353Smlaier		}
2842126353Smlaier		| gid PORTBINARY gid		{
2843126353Smlaier			if ($1 == GID_MAX || $3 == GID_MAX) {
2844126353Smlaier				yyerror("group unknown requires operator = or "
2845126353Smlaier				    "!=");
2846126353Smlaier				YYERROR;
2847126353Smlaier			}
2848126353Smlaier			$$ = calloc(1, sizeof(struct node_gid));
2849126353Smlaier			if ($$ == NULL)
2850126353Smlaier				err(1, "gid_item: calloc");
2851126353Smlaier			$$->gid[0] = $1;
2852126353Smlaier			$$->gid[1] = $3;
2853126353Smlaier			$$->op = $2;
2854126353Smlaier			$$->next = NULL;
2855126353Smlaier			$$->tail = $$;
2856126353Smlaier		}
2857126353Smlaier		;
2858126353Smlaier
2859126353Smlaiergid		: STRING			{
2860126353Smlaier			u_long	ulval;
2861126353Smlaier
2862126353Smlaier			if (atoul($1, &ulval) == -1) {
2863126353Smlaier				if (!strcmp($1, "unknown"))
2864126353Smlaier					$$ = GID_MAX;
2865126353Smlaier				else {
2866126353Smlaier					struct group	*grp;
2867126353Smlaier
2868126353Smlaier					if ((grp = getgrnam($1)) == NULL) {
2869126353Smlaier						yyerror("unknown group %s", $1);
2870130617Smlaier						free($1);
2871126353Smlaier						YYERROR;
2872126353Smlaier					}
2873126353Smlaier					$$ = grp->gr_gid;
2874126353Smlaier				}
2875126353Smlaier			} else {
2876126353Smlaier				if (ulval >= GID_MAX) {
2877126353Smlaier					yyerror("illegal gid value %lu", ulval);
2878130617Smlaier					free($1);
2879126353Smlaier					YYERROR;
2880126353Smlaier				}
2881126353Smlaier				$$ = ulval;
2882126353Smlaier			}
2883130617Smlaier			free($1);
2884126353Smlaier		}
2885126353Smlaier		;
2886126353Smlaier
2887126353Smlaierflag		: STRING			{
2888126353Smlaier			int	f;
2889126353Smlaier
2890126353Smlaier			if ((f = parse_flags($1)) < 0) {
2891126353Smlaier				yyerror("bad flags %s", $1);
2892130617Smlaier				free($1);
2893126353Smlaier				YYERROR;
2894126353Smlaier			}
2895130617Smlaier			free($1);
2896126353Smlaier			$$.b1 = f;
2897126353Smlaier		}
2898126353Smlaier		;
2899126353Smlaier
2900126353Smlaierflags		: FLAGS flag '/' flag	{ $$.b1 = $2.b1; $$.b2 = $4.b1; }
2901126353Smlaier		| FLAGS '/' flag	{ $$.b1 = 0; $$.b2 = $3.b1; }
2902171172Smlaier		| FLAGS ANY		{ $$.b1 = 0; $$.b2 = 0; }
2903126353Smlaier		;
2904126353Smlaier
2905126353Smlaiericmpspec	: ICMPTYPE icmp_item		{ $$ = $2; }
2906126353Smlaier		| ICMPTYPE '{' icmp_list '}'	{ $$ = $3; }
2907126353Smlaier		| ICMP6TYPE icmp6_item		{ $$ = $2; }
2908126353Smlaier		| ICMP6TYPE '{' icmp6_list '}'	{ $$ = $3; }
2909126353Smlaier		;
2910126353Smlaier
2911126353Smlaiericmp_list	: icmp_item			{ $$ = $1; }
2912126353Smlaier		| icmp_list comma icmp_item	{
2913126353Smlaier			$1->tail->next = $3;
2914126353Smlaier			$1->tail = $3;
2915126353Smlaier			$$ = $1;
2916126353Smlaier		}
2917126353Smlaier		;
2918126353Smlaier
2919126353Smlaiericmp6_list	: icmp6_item			{ $$ = $1; }
2920126353Smlaier		| icmp6_list comma icmp6_item	{
2921126353Smlaier			$1->tail->next = $3;
2922126353Smlaier			$1->tail = $3;
2923126353Smlaier			$$ = $1;
2924126353Smlaier		}
2925126353Smlaier		;
2926126353Smlaier
2927126353Smlaiericmp_item	: icmptype		{
2928126353Smlaier			$$ = calloc(1, sizeof(struct node_icmp));
2929126353Smlaier			if ($$ == NULL)
2930126353Smlaier				err(1, "icmp_item: calloc");
2931126353Smlaier			$$->type = $1;
2932126353Smlaier			$$->code = 0;
2933126353Smlaier			$$->proto = IPPROTO_ICMP;
2934126353Smlaier			$$->next = NULL;
2935126353Smlaier			$$->tail = $$;
2936126353Smlaier		}
2937126353Smlaier		| icmptype CODE STRING	{
2938126353Smlaier			const struct icmpcodeent	*p;
2939126353Smlaier			u_long				 ulval;
2940126353Smlaier
2941126353Smlaier			if (atoul($3, &ulval) == 0) {
2942126353Smlaier				if (ulval > 255) {
2943130617Smlaier					free($3);
2944145840Smlaier					yyerror("illegal icmp-code %lu", ulval);
2945126353Smlaier					YYERROR;
2946126353Smlaier				}
2947126353Smlaier			} else {
2948126353Smlaier				if ((p = geticmpcodebyname($1-1, $3,
2949126353Smlaier				    AF_INET)) == NULL) {
2950126353Smlaier					yyerror("unknown icmp-code %s", $3);
2951130617Smlaier					free($3);
2952126353Smlaier					YYERROR;
2953126353Smlaier				}
2954126353Smlaier				ulval = p->code;
2955126353Smlaier			}
2956130617Smlaier			free($3);
2957126353Smlaier			$$ = calloc(1, sizeof(struct node_icmp));
2958126353Smlaier			if ($$ == NULL)
2959126353Smlaier				err(1, "icmp_item: calloc");
2960126353Smlaier			$$->type = $1;
2961126353Smlaier			$$->code = ulval + 1;
2962126353Smlaier			$$->proto = IPPROTO_ICMP;
2963126353Smlaier			$$->next = NULL;
2964126353Smlaier			$$->tail = $$;
2965126353Smlaier		}
2966126353Smlaier		;
2967126353Smlaier
2968126353Smlaiericmp6_item	: icmp6type		{
2969126353Smlaier			$$ = calloc(1, sizeof(struct node_icmp));
2970126353Smlaier			if ($$ == NULL)
2971126353Smlaier				err(1, "icmp_item: calloc");
2972126353Smlaier			$$->type = $1;
2973126353Smlaier			$$->code = 0;
2974126353Smlaier			$$->proto = IPPROTO_ICMPV6;
2975126353Smlaier			$$->next = NULL;
2976126353Smlaier			$$->tail = $$;
2977126353Smlaier		}
2978126353Smlaier		| icmp6type CODE STRING	{
2979126353Smlaier			const struct icmpcodeent	*p;
2980126353Smlaier			u_long				 ulval;
2981126353Smlaier
2982126353Smlaier			if (atoul($3, &ulval) == 0) {
2983126353Smlaier				if (ulval > 255) {
2984145840Smlaier					yyerror("illegal icmp6-code %lu",
2985126353Smlaier					    ulval);
2986130617Smlaier					free($3);
2987126353Smlaier					YYERROR;
2988126353Smlaier				}
2989126353Smlaier			} else {
2990126353Smlaier				if ((p = geticmpcodebyname($1-1, $3,
2991126353Smlaier				    AF_INET6)) == NULL) {
2992126353Smlaier					yyerror("unknown icmp6-code %s", $3);
2993130617Smlaier					free($3);
2994126353Smlaier					YYERROR;
2995126353Smlaier				}
2996126353Smlaier				ulval = p->code;
2997126353Smlaier			}
2998130617Smlaier			free($3);
2999126353Smlaier			$$ = calloc(1, sizeof(struct node_icmp));
3000126353Smlaier			if ($$ == NULL)
3001126353Smlaier				err(1, "icmp_item: calloc");
3002126353Smlaier			$$->type = $1;
3003126353Smlaier			$$->code = ulval + 1;
3004126353Smlaier			$$->proto = IPPROTO_ICMPV6;
3005126353Smlaier			$$->next = NULL;
3006126353Smlaier			$$->tail = $$;
3007126353Smlaier		}
3008126353Smlaier		;
3009126353Smlaier
3010126353Smlaiericmptype	: STRING			{
3011126353Smlaier			const struct icmptypeent	*p;
3012126353Smlaier			u_long				 ulval;
3013126353Smlaier
3014126353Smlaier			if (atoul($1, &ulval) == 0) {
3015126353Smlaier				if (ulval > 255) {
3016145840Smlaier					yyerror("illegal icmp-type %lu", ulval);
3017130617Smlaier					free($1);
3018126353Smlaier					YYERROR;
3019126353Smlaier				}
3020126353Smlaier				$$ = ulval + 1;
3021126353Smlaier			} else {
3022126353Smlaier				if ((p = geticmptypebyname($1, AF_INET)) ==
3023126353Smlaier				    NULL) {
3024126353Smlaier					yyerror("unknown icmp-type %s", $1);
3025130617Smlaier					free($1);
3026126353Smlaier					YYERROR;
3027126353Smlaier				}
3028126353Smlaier				$$ = p->type + 1;
3029126353Smlaier			}
3030130617Smlaier			free($1);
3031126353Smlaier		}
3032126353Smlaier		;
3033126353Smlaier
3034126353Smlaiericmp6type	: STRING			{
3035126353Smlaier			const struct icmptypeent	*p;
3036126353Smlaier			u_long				 ulval;
3037126353Smlaier
3038126353Smlaier			if (atoul($1, &ulval) == 0) {
3039126353Smlaier				if (ulval > 255) {
3040171172Smlaier					yyerror("illegal icmp6-type %lu",
3041171172Smlaier					    ulval);
3042130617Smlaier					free($1);
3043126353Smlaier					YYERROR;
3044126353Smlaier				}
3045126353Smlaier				$$ = ulval + 1;
3046126353Smlaier			} else {
3047126353Smlaier				if ((p = geticmptypebyname($1, AF_INET6)) ==
3048126353Smlaier				    NULL) {
3049126353Smlaier					yyerror("unknown icmp6-type %s", $1);
3050130617Smlaier					free($1);
3051126353Smlaier					YYERROR;
3052126353Smlaier				}
3053126353Smlaier				$$ = p->type + 1;
3054126353Smlaier			}
3055130617Smlaier			free($1);
3056126353Smlaier		}
3057126353Smlaier		;
3058126353Smlaier
3059126353Smlaiertos		: TOS STRING			{
3060126353Smlaier			if (!strcmp($2, "lowdelay"))
3061126353Smlaier				$$ = IPTOS_LOWDELAY;
3062126353Smlaier			else if (!strcmp($2, "throughput"))
3063126353Smlaier				$$ = IPTOS_THROUGHPUT;
3064126353Smlaier			else if (!strcmp($2, "reliability"))
3065126353Smlaier				$$ = IPTOS_RELIABILITY;
3066126353Smlaier			else if ($2[0] == '0' && $2[1] == 'x')
3067126353Smlaier				$$ = strtoul($2, NULL, 16);
3068126353Smlaier			else
3069126353Smlaier				$$ = strtoul($2, NULL, 10);
3070126353Smlaier			if (!$$ || $$ > 255) {
3071126353Smlaier				yyerror("illegal tos value %s", $2);
3072130617Smlaier				free($2);
3073126353Smlaier				YYERROR;
3074126353Smlaier			}
3075130617Smlaier			free($2);
3076126353Smlaier		}
3077126353Smlaier		;
3078126353Smlaier
3079130617Smlaiersourcetrack	: SOURCETRACK		{ $$ = PF_SRCTRACK; }
3080130617Smlaier		| SOURCETRACK GLOBAL	{ $$ = PF_SRCTRACK_GLOBAL; }
3081130617Smlaier		| SOURCETRACK RULE	{ $$ = PF_SRCTRACK_RULE; }
3082130617Smlaier		;
3083130617Smlaier
3084130617Smlaierstatelock	: IFBOUND {
3085130617Smlaier			$$ = PFRULE_IFBOUND;
3086130617Smlaier		}
3087130617Smlaier		| FLOATING {
3088130617Smlaier			$$ = 0;
3089130617Smlaier		}
3090130617Smlaier		;
3091130617Smlaier
3092171172Smlaierkeep		: NO STATE			{
3093171172Smlaier			$$.action = 0;
3094171172Smlaier			$$.options = NULL;
3095171172Smlaier		}
3096171172Smlaier		| KEEP STATE state_opt_spec	{
3097126353Smlaier			$$.action = PF_STATE_NORMAL;
3098126353Smlaier			$$.options = $3;
3099126353Smlaier		}
3100130617Smlaier		| MODULATE STATE state_opt_spec {
3101126353Smlaier			$$.action = PF_STATE_MODULATE;
3102126353Smlaier			$$.options = $3;
3103126353Smlaier		}
3104126353Smlaier		| SYNPROXY STATE state_opt_spec {
3105126353Smlaier			$$.action = PF_STATE_SYNPROXY;
3106126353Smlaier			$$.options = $3;
3107126353Smlaier		}
3108126353Smlaier		;
3109126353Smlaier
3110145840Smlaierflush		: /* empty */			{ $$ = 0; }
3111145840Smlaier		| FLUSH				{ $$ = PF_FLUSH; }
3112145840Smlaier		| FLUSH GLOBAL			{
3113145840Smlaier			$$ = PF_FLUSH | PF_FLUSH_GLOBAL;
3114145840Smlaier		}
3115145840Smlaier		;
3116145840Smlaier
3117126353Smlaierstate_opt_spec	: '(' state_opt_list ')'	{ $$ = $2; }
3118126353Smlaier		| /* empty */			{ $$ = NULL; }
3119126353Smlaier		;
3120126353Smlaier
3121126353Smlaierstate_opt_list	: state_opt_item		{ $$ = $1; }
3122126353Smlaier		| state_opt_list comma state_opt_item {
3123126353Smlaier			$1->tail->next = $3;
3124126353Smlaier			$1->tail = $3;
3125126353Smlaier			$$ = $1;
3126126353Smlaier		}
3127126353Smlaier		;
3128126353Smlaier
3129126353Smlaierstate_opt_item	: MAXIMUM number		{
3130126353Smlaier			$$ = calloc(1, sizeof(struct node_state_opt));
3131126353Smlaier			if ($$ == NULL)
3132126353Smlaier				err(1, "state_opt_item: calloc");
3133126353Smlaier			$$->type = PF_STATE_OPT_MAX;
3134126353Smlaier			$$->data.max_states = $2;
3135126353Smlaier			$$->next = NULL;
3136126353Smlaier			$$->tail = $$;
3137126353Smlaier		}
3138130617Smlaier		| NOSYNC				{
3139130617Smlaier			$$ = calloc(1, sizeof(struct node_state_opt));
3140130617Smlaier			if ($$ == NULL)
3141130617Smlaier				err(1, "state_opt_item: calloc");
3142130617Smlaier			$$->type = PF_STATE_OPT_NOSYNC;
3143130617Smlaier			$$->next = NULL;
3144130617Smlaier			$$->tail = $$;
3145130617Smlaier		}
3146130617Smlaier		| MAXSRCSTATES number			{
3147130617Smlaier			$$ = calloc(1, sizeof(struct node_state_opt));
3148130617Smlaier			if ($$ == NULL)
3149130617Smlaier				err(1, "state_opt_item: calloc");
3150130617Smlaier			$$->type = PF_STATE_OPT_MAX_SRC_STATES;
3151130617Smlaier			$$->data.max_src_states = $2;
3152130617Smlaier			$$->next = NULL;
3153130617Smlaier			$$->tail = $$;
3154130617Smlaier		}
3155145840Smlaier		| MAXSRCCONN number			{
3156145840Smlaier			$$ = calloc(1, sizeof(struct node_state_opt));
3157145840Smlaier			if ($$ == NULL)
3158145840Smlaier				err(1, "state_opt_item: calloc");
3159145840Smlaier			$$->type = PF_STATE_OPT_MAX_SRC_CONN;
3160145840Smlaier			$$->data.max_src_conn = $2;
3161145840Smlaier			$$->next = NULL;
3162145840Smlaier			$$->tail = $$;
3163145840Smlaier		}
3164145840Smlaier		| MAXSRCCONNRATE number '/' number	{
3165145840Smlaier			$$ = calloc(1, sizeof(struct node_state_opt));
3166145840Smlaier			if ($$ == NULL)
3167145840Smlaier				err(1, "state_opt_item: calloc");
3168145840Smlaier			$$->type = PF_STATE_OPT_MAX_SRC_CONN_RATE;
3169145840Smlaier			$$->data.max_src_conn_rate.limit = $2;
3170145840Smlaier			$$->data.max_src_conn_rate.seconds = $4;
3171145840Smlaier			$$->next = NULL;
3172145840Smlaier			$$->tail = $$;
3173145840Smlaier		}
3174145840Smlaier		| OVERLOAD '<' STRING '>' flush		{
3175145840Smlaier			if (strlen($3) >= PF_TABLE_NAME_SIZE) {
3176145840Smlaier				yyerror("table name '%s' too long", $3);
3177145840Smlaier				free($3);
3178145840Smlaier				YYERROR;
3179145840Smlaier			}
3180145840Smlaier			$$ = calloc(1, sizeof(struct node_state_opt));
3181145840Smlaier			if ($$ == NULL)
3182145840Smlaier				err(1, "state_opt_item: calloc");
3183145840Smlaier			if (strlcpy($$->data.overload.tblname, $3,
3184145840Smlaier			    PF_TABLE_NAME_SIZE) >= PF_TABLE_NAME_SIZE)
3185145840Smlaier				errx(1, "state_opt_item: strlcpy");
3186145840Smlaier			free($3);
3187145840Smlaier			$$->type = PF_STATE_OPT_OVERLOAD;
3188145840Smlaier			$$->data.overload.flush = $5;
3189145840Smlaier			$$->next = NULL;
3190145840Smlaier			$$->tail = $$;
3191145840Smlaier		}
3192130617Smlaier		| MAXSRCNODES number			{
3193130617Smlaier			$$ = calloc(1, sizeof(struct node_state_opt));
3194130617Smlaier			if ($$ == NULL)
3195130617Smlaier				err(1, "state_opt_item: calloc");
3196130617Smlaier			$$->type = PF_STATE_OPT_MAX_SRC_NODES;
3197130617Smlaier			$$->data.max_src_nodes = $2;
3198130617Smlaier			$$->next = NULL;
3199130617Smlaier			$$->tail = $$;
3200130617Smlaier		}
3201130617Smlaier		| sourcetrack {
3202130617Smlaier			$$ = calloc(1, sizeof(struct node_state_opt));
3203130617Smlaier			if ($$ == NULL)
3204130617Smlaier				err(1, "state_opt_item: calloc");
3205130617Smlaier			$$->type = PF_STATE_OPT_SRCTRACK;
3206130617Smlaier			$$->data.src_track = $1;
3207130617Smlaier			$$->next = NULL;
3208130617Smlaier			$$->tail = $$;
3209130617Smlaier		}
3210130617Smlaier		| statelock {
3211130617Smlaier			$$ = calloc(1, sizeof(struct node_state_opt));
3212130617Smlaier			if ($$ == NULL)
3213130617Smlaier				err(1, "state_opt_item: calloc");
3214130617Smlaier			$$->type = PF_STATE_OPT_STATELOCK;
3215130617Smlaier			$$->data.statelock = $1;
3216130617Smlaier			$$->next = NULL;
3217130617Smlaier			$$->tail = $$;
3218130617Smlaier		}
3219126353Smlaier		| STRING number			{
3220126353Smlaier			int	i;
3221126353Smlaier
3222126353Smlaier			for (i = 0; pf_timeouts[i].name &&
3223126353Smlaier			    strcmp(pf_timeouts[i].name, $1); ++i)
3224126353Smlaier				;	/* nothing */
3225126353Smlaier			if (!pf_timeouts[i].name) {
3226126353Smlaier				yyerror("illegal timeout name %s", $1);
3227130617Smlaier				free($1);
3228126353Smlaier				YYERROR;
3229126353Smlaier			}
3230126353Smlaier			if (strchr(pf_timeouts[i].name, '.') == NULL) {
3231126353Smlaier				yyerror("illegal state timeout %s", $1);
3232130617Smlaier				free($1);
3233126353Smlaier				YYERROR;
3234126353Smlaier			}
3235130617Smlaier			free($1);
3236126353Smlaier			$$ = calloc(1, sizeof(struct node_state_opt));
3237126353Smlaier			if ($$ == NULL)
3238126353Smlaier				err(1, "state_opt_item: calloc");
3239126353Smlaier			$$->type = PF_STATE_OPT_TIMEOUT;
3240126353Smlaier			$$->data.timeout.number = pf_timeouts[i].timeout;
3241126353Smlaier			$$->data.timeout.seconds = $2;
3242126353Smlaier			$$->next = NULL;
3243126353Smlaier			$$->tail = $$;
3244126353Smlaier		}
3245126353Smlaier		;
3246126353Smlaier
3247126353Smlaierlabel		: LABEL STRING			{
3248130617Smlaier			$$ = $2;
3249126353Smlaier		}
3250126353Smlaier		;
3251126353Smlaier
3252126353Smlaierqname		: QUEUE STRING				{
3253130617Smlaier			$$.qname = $2;
3254126353Smlaier		}
3255126353Smlaier		| QUEUE '(' STRING ')'			{
3256130617Smlaier			$$.qname = $3;
3257126353Smlaier		}
3258126353Smlaier		| QUEUE '(' STRING comma STRING ')'	{
3259130617Smlaier			$$.qname = $3;
3260130617Smlaier			$$.pqname = $5;
3261126353Smlaier		}
3262126353Smlaier		;
3263126353Smlaier
3264126353Smlaierno		: /* empty */			{ $$ = 0; }
3265126353Smlaier		| NO				{ $$ = 1; }
3266126353Smlaier		;
3267126353Smlaier
3268126353Smlaierrport		: STRING			{
3269126353Smlaier			char	*p = strchr($1, ':');
3270126353Smlaier
3271126353Smlaier			if (p == NULL) {
3272130617Smlaier				if (($$.a = getservice($1)) == -1) {
3273130617Smlaier					free($1);
3274126353Smlaier					YYERROR;
3275130617Smlaier				}
3276126353Smlaier				$$.b = $$.t = 0;
3277126353Smlaier			} else if (!strcmp(p+1, "*")) {
3278126353Smlaier				*p = 0;
3279130617Smlaier				if (($$.a = getservice($1)) == -1) {
3280130617Smlaier					free($1);
3281126353Smlaier					YYERROR;
3282130617Smlaier				}
3283126353Smlaier				$$.b = 0;
3284126353Smlaier				$$.t = 1;
3285126353Smlaier			} else {
3286126353Smlaier				*p++ = 0;
3287126353Smlaier				if (($$.a = getservice($1)) == -1 ||
3288130617Smlaier				    ($$.b = getservice(p)) == -1) {
3289130617Smlaier					free($1);
3290126353Smlaier					YYERROR;
3291130617Smlaier				}
3292126353Smlaier				if ($$.a == $$.b)
3293126353Smlaier					$$.b = 0;
3294126353Smlaier				$$.t = 0;
3295126353Smlaier			}
3296130617Smlaier			free($1);
3297126353Smlaier		}
3298126353Smlaier		;
3299126353Smlaier
3300126353Smlaierredirspec	: host				{ $$ = $1; }
3301126353Smlaier		| '{' redir_host_list '}'	{ $$ = $2; }
3302126353Smlaier		;
3303126353Smlaier
3304126353Smlaierredir_host_list	: host				{ $$ = $1; }
3305126353Smlaier		| redir_host_list comma host	{
3306126353Smlaier			$1->tail->next = $3;
3307126353Smlaier			$1->tail = $3->tail;
3308126353Smlaier			$$ = $1;
3309126353Smlaier		}
3310126353Smlaier		;
3311126353Smlaier
3312126353Smlaierredirpool	: /* empty */			{ $$ = NULL; }
3313126353Smlaier		| ARROW redirspec		{
3314126353Smlaier			$$ = calloc(1, sizeof(struct redirection));
3315126353Smlaier			if ($$ == NULL)
3316126353Smlaier				err(1, "redirection: calloc");
3317126353Smlaier			$$->host = $2;
3318126353Smlaier			$$->rport.a = $$->rport.b = $$->rport.t = 0;
3319126353Smlaier		}
3320126353Smlaier		| ARROW redirspec PORT rport	{
3321126353Smlaier			$$ = calloc(1, sizeof(struct redirection));
3322126353Smlaier			if ($$ == NULL)
3323126353Smlaier				err(1, "redirection: calloc");
3324126353Smlaier			$$->host = $2;
3325126353Smlaier			$$->rport = $4;
3326126353Smlaier		}
3327126353Smlaier		;
3328126353Smlaier
3329126353Smlaierhashkey		: /* empty */
3330126353Smlaier		{
3331126353Smlaier			$$ = calloc(1, sizeof(struct pf_poolhashkey));
3332126353Smlaier			if ($$ == NULL)
3333126353Smlaier				err(1, "hashkey: calloc");
3334126353Smlaier			$$->key32[0] = arc4random();
3335126353Smlaier			$$->key32[1] = arc4random();
3336126353Smlaier			$$->key32[2] = arc4random();
3337126353Smlaier			$$->key32[3] = arc4random();
3338126353Smlaier		}
3339126353Smlaier		| string
3340126353Smlaier		{
3341126353Smlaier			if (!strncmp($1, "0x", 2)) {
3342126353Smlaier				if (strlen($1) != 34) {
3343130617Smlaier					free($1);
3344126353Smlaier					yyerror("hex key must be 128 bits "
3345126353Smlaier						"(32 hex digits) long");
3346126353Smlaier					YYERROR;
3347126353Smlaier				}
3348126353Smlaier				$$ = calloc(1, sizeof(struct pf_poolhashkey));
3349126353Smlaier				if ($$ == NULL)
3350126353Smlaier					err(1, "hashkey: calloc");
3351126353Smlaier
3352126353Smlaier				if (sscanf($1, "0x%8x%8x%8x%8x",
3353126353Smlaier				    &$$->key32[0], &$$->key32[1],
3354126353Smlaier				    &$$->key32[2], &$$->key32[3]) != 4) {
3355126353Smlaier					free($$);
3356130617Smlaier					free($1);
3357126353Smlaier					yyerror("invalid hex key");
3358126353Smlaier					YYERROR;
3359126353Smlaier				}
3360126353Smlaier			} else {
3361126353Smlaier				MD5_CTX	context;
3362126353Smlaier
3363126353Smlaier				$$ = calloc(1, sizeof(struct pf_poolhashkey));
3364126353Smlaier				if ($$ == NULL)
3365126353Smlaier					err(1, "hashkey: calloc");
3366126353Smlaier				MD5Init(&context);
3367126353Smlaier				MD5Update(&context, (unsigned char *)$1,
3368126353Smlaier				    strlen($1));
3369126353Smlaier				MD5Final((unsigned char *)$$, &context);
3370126353Smlaier				HTONL($$->key32[0]);
3371126353Smlaier				HTONL($$->key32[1]);
3372126353Smlaier				HTONL($$->key32[2]);
3373126353Smlaier				HTONL($$->key32[3]);
3374126353Smlaier			}
3375130617Smlaier			free($1);
3376126353Smlaier		}
3377126353Smlaier		;
3378126353Smlaier
3379130617Smlaierpool_opts	:	{ bzero(&pool_opts, sizeof pool_opts); }
3380130617Smlaier		    pool_opts_l
3381130617Smlaier			{ $$ = pool_opts; }
3382130617Smlaier		| /* empty */	{
3383130617Smlaier			bzero(&pool_opts, sizeof pool_opts);
3384130617Smlaier			$$ = pool_opts;
3385126353Smlaier		}
3386130617Smlaier		;
3387130617Smlaier
3388130617Smlaierpool_opts_l	: pool_opts_l pool_opt
3389130617Smlaier		| pool_opt
3390130617Smlaier		;
3391130617Smlaier
3392130617Smlaierpool_opt	: BITMASK	{
3393130617Smlaier			if (pool_opts.type) {
3394130617Smlaier				yyerror("pool type cannot be redefined");
3395130617Smlaier				YYERROR;
3396130617Smlaier			}
3397130617Smlaier			pool_opts.type =  PF_POOL_BITMASK;
3398126353Smlaier		}
3399130617Smlaier		| RANDOM	{
3400130617Smlaier			if (pool_opts.type) {
3401130617Smlaier				yyerror("pool type cannot be redefined");
3402130617Smlaier				YYERROR;
3403130617Smlaier			}
3404130617Smlaier			pool_opts.type = PF_POOL_RANDOM;
3405126353Smlaier		}
3406130617Smlaier		| SOURCEHASH hashkey {
3407130617Smlaier			if (pool_opts.type) {
3408130617Smlaier				yyerror("pool type cannot be redefined");
3409130617Smlaier				YYERROR;
3410130617Smlaier			}
3411130617Smlaier			pool_opts.type = PF_POOL_SRCHASH;
3412130617Smlaier			pool_opts.key = $2;
3413126353Smlaier		}
3414130617Smlaier		| ROUNDROBIN	{
3415130617Smlaier			if (pool_opts.type) {
3416130617Smlaier				yyerror("pool type cannot be redefined");
3417130617Smlaier				YYERROR;
3418130617Smlaier			}
3419130617Smlaier			pool_opts.type = PF_POOL_ROUNDROBIN;
3420126353Smlaier		}
3421130617Smlaier		| STATICPORT	{
3422130617Smlaier			if (pool_opts.staticport) {
3423130617Smlaier				yyerror("static-port cannot be redefined");
3424130617Smlaier				YYERROR;
3425130617Smlaier			}
3426130617Smlaier			pool_opts.staticport = 1;
3427130617Smlaier		}
3428130617Smlaier		| STICKYADDRESS	{
3429130617Smlaier			if (filter_opts.marker & POM_STICKYADDRESS) {
3430130617Smlaier				yyerror("sticky-address cannot be redefined");
3431130617Smlaier				YYERROR;
3432130617Smlaier			}
3433130617Smlaier			pool_opts.marker |= POM_STICKYADDRESS;
3434130617Smlaier			pool_opts.opts |= PF_POOL_STICKYADDR;
3435130617Smlaier		}
3436126353Smlaier		;
3437126353Smlaier
3438126353Smlaierredirection	: /* empty */			{ $$ = NULL; }
3439126353Smlaier		| ARROW host			{
3440126353Smlaier			$$ = calloc(1, sizeof(struct redirection));
3441126353Smlaier			if ($$ == NULL)
3442126353Smlaier				err(1, "redirection: calloc");
3443126353Smlaier			$$->host = $2;
3444126353Smlaier			$$->rport.a = $$->rport.b = $$->rport.t = 0;
3445126353Smlaier		}
3446126353Smlaier		| ARROW host PORT rport	{
3447126353Smlaier			$$ = calloc(1, sizeof(struct redirection));
3448126353Smlaier			if ($$ == NULL)
3449126353Smlaier				err(1, "redirection: calloc");
3450126353Smlaier			$$->host = $2;
3451126353Smlaier			$$->rport = $4;
3452126353Smlaier		}
3453126353Smlaier		;
3454126353Smlaier
3455171172Smlaier/* ifdef __FreeBSD__ */
3456171172Smlaiernatpass		: /* empty */	{ $$.b1 = $$.b2 = 0; $$.w2 = 0; }
3457171172Smlaier		| PASS		{ $$.b1 = 1; $$.b2 = 0; $$.w2 = 0; }
3458171172Smlaier/* else
3459171172Smlaiernatpass		:  empty 	{ $$.b1 = $$.b2 = 0; }
3460171172Smlaier		| PASS		{ $$.b1 = 1; $$.b2 = 0; }
3461171172Smlaier * endif */
3462171172Smlaier		| PASS log	{ $$.b1 = 1; $$.b2 = $2.log; $$.w2 = $2.logif; }
3463126353Smlaier		;
3464126353Smlaier
3465126353Smlaiernataction	: no NAT natpass {
3466171172Smlaier			if ($1 && $3.b1) {
3467171172Smlaier				yyerror("\"pass\" not valid with \"no\"");
3468171172Smlaier				YYERROR;
3469171172Smlaier			}
3470126353Smlaier			if ($1)
3471126353Smlaier				$$.b1 = PF_NONAT;
3472126353Smlaier			else
3473126353Smlaier				$$.b1 = PF_NAT;
3474171172Smlaier			$$.b2 = $3.b1;
3475171172Smlaier			$$.w = $3.b2;
3476171172Smlaier			$$.w2 = $3.w2;
3477126353Smlaier		}
3478126353Smlaier		| no RDR natpass {
3479171172Smlaier			if ($1 && $3.b1) {
3480171172Smlaier				yyerror("\"pass\" not valid with \"no\"");
3481171172Smlaier				YYERROR;
3482171172Smlaier			}
3483126353Smlaier			if ($1)
3484126353Smlaier				$$.b1 = PF_NORDR;
3485126353Smlaier			else
3486126353Smlaier				$$.b1 = PF_RDR;
3487171172Smlaier			$$.b2 = $3.b1;
3488171172Smlaier			$$.w = $3.b2;
3489171172Smlaier			$$.w2 = $3.w2;
3490126353Smlaier		}
3491126353Smlaier		;
3492126353Smlaier
3493171172Smlaiernatrule		: nataction interface af proto fromto tag tagged rtable
3494171172Smlaier		    redirpool pool_opts
3495126353Smlaier		{
3496126353Smlaier			struct pf_rule	r;
3497126353Smlaier
3498126353Smlaier			if (check_rulestate(PFCTL_STATE_NAT))
3499126353Smlaier				YYERROR;
3500126353Smlaier
3501126353Smlaier			memset(&r, 0, sizeof(r));
3502126353Smlaier
3503126353Smlaier			r.action = $1.b1;
3504126353Smlaier			r.natpass = $1.b2;
3505171172Smlaier			r.log = $1.w;
3506171172Smlaier			r.logif = $1.w2;
3507126353Smlaier			r.af = $3;
3508126353Smlaier
3509126353Smlaier			if (!r.af) {
3510126353Smlaier				if ($5.src.host && $5.src.host->af &&
3511126353Smlaier				    !$5.src.host->ifindex)
3512126353Smlaier					r.af = $5.src.host->af;
3513126353Smlaier				else if ($5.dst.host && $5.dst.host->af &&
3514126353Smlaier				    !$5.dst.host->ifindex)
3515126353Smlaier					r.af = $5.dst.host->af;
3516126353Smlaier			}
3517126353Smlaier
3518126353Smlaier			if ($6 != NULL)
3519130617Smlaier				if (strlcpy(r.tagname, $6, PF_TAG_NAME_SIZE) >=
3520126353Smlaier				    PF_TAG_NAME_SIZE) {
3521126353Smlaier					yyerror("tag too long, max %u chars",
3522126353Smlaier					    PF_TAG_NAME_SIZE - 1);
3523126353Smlaier					YYERROR;
3524126353Smlaier				}
3525126353Smlaier
3526145840Smlaier			if ($7.name)
3527145840Smlaier				if (strlcpy(r.match_tagname, $7.name,
3528145840Smlaier				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
3529145840Smlaier					yyerror("tag too long, max %u chars",
3530145840Smlaier					    PF_TAG_NAME_SIZE - 1);
3531145840Smlaier					YYERROR;
3532145840Smlaier				}
3533145840Smlaier			r.match_tag_not = $7.neg;
3534171172Smlaier			r.rtableid = $8;
3535145840Smlaier
3536126353Smlaier			if (r.action == PF_NONAT || r.action == PF_NORDR) {
3537171172Smlaier				if ($9 != NULL) {
3538126353Smlaier					yyerror("translation rule with 'no' "
3539126353Smlaier					    "does not need '->'");
3540126353Smlaier					YYERROR;
3541126353Smlaier				}
3542126353Smlaier			} else {
3543171172Smlaier				if ($9 == NULL || $9->host == NULL) {
3544126353Smlaier					yyerror("translation rule requires '-> "
3545126353Smlaier					    "address'");
3546126353Smlaier					YYERROR;
3547126353Smlaier				}
3548171172Smlaier				if (!r.af && ! $9->host->ifindex)
3549171172Smlaier					r.af = $9->host->af;
3550126353Smlaier
3551171172Smlaier				remove_invalid_hosts(&$9->host, &r.af);
3552171172Smlaier				if (invalid_redirect($9->host, r.af))
3553126353Smlaier					YYERROR;
3554171172Smlaier				if (check_netmask($9->host, r.af))
3555126353Smlaier					YYERROR;
3556126353Smlaier
3557171172Smlaier				r.rpool.proxy_port[0] = ntohs($9->rport.a);
3558126353Smlaier
3559126353Smlaier				switch (r.action) {
3560126353Smlaier				case PF_RDR:
3561171172Smlaier					if (!$9->rport.b && $9->rport.t &&
3562126353Smlaier					    $5.dst.port != NULL) {
3563126353Smlaier						r.rpool.proxy_port[1] =
3564171172Smlaier						    ntohs($9->rport.a) +
3565130617Smlaier						    (ntohs(
3566130617Smlaier						    $5.dst.port->port[1]) -
3567130617Smlaier						    ntohs(
3568130617Smlaier						    $5.dst.port->port[0]));
3569126353Smlaier					} else
3570126353Smlaier						r.rpool.proxy_port[1] =
3571171172Smlaier						    ntohs($9->rport.b);
3572126353Smlaier					break;
3573126353Smlaier				case PF_NAT:
3574130617Smlaier					r.rpool.proxy_port[1] =
3575171172Smlaier					    ntohs($9->rport.b);
3576126353Smlaier					if (!r.rpool.proxy_port[0] &&
3577126353Smlaier					    !r.rpool.proxy_port[1]) {
3578126353Smlaier						r.rpool.proxy_port[0] =
3579126353Smlaier						    PF_NAT_PROXY_PORT_LOW;
3580126353Smlaier						r.rpool.proxy_port[1] =
3581126353Smlaier						    PF_NAT_PROXY_PORT_HIGH;
3582126353Smlaier					} else if (!r.rpool.proxy_port[1])
3583126353Smlaier						r.rpool.proxy_port[1] =
3584126353Smlaier						    r.rpool.proxy_port[0];
3585126353Smlaier					break;
3586126353Smlaier				default:
3587126353Smlaier					break;
3588126353Smlaier				}
3589126353Smlaier
3590171172Smlaier				r.rpool.opts = $10.type;
3591130617Smlaier				if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
3592171172Smlaier				    PF_POOL_NONE && ($9->host->next != NULL ||
3593171172Smlaier				    $9->host->addr.type == PF_ADDR_TABLE ||
3594171172Smlaier				    DYNIF_MULTIADDR($9->host->addr)))
3595126353Smlaier					r.rpool.opts = PF_POOL_ROUNDROBIN;
3596130617Smlaier				if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
3597130617Smlaier				    PF_POOL_ROUNDROBIN &&
3598171172Smlaier				    disallow_table($9->host, "tables are only "
3599130617Smlaier				    "supported in round-robin redirection "
3600130617Smlaier				    "pools"))
3601130617Smlaier					YYERROR;
3602130617Smlaier				if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
3603130617Smlaier				    PF_POOL_ROUNDROBIN &&
3604171172Smlaier				    disallow_alias($9->host, "interface (%s) "
3605130617Smlaier				    "is only supported in round-robin "
3606130617Smlaier				    "redirection pools"))
3607130617Smlaier					YYERROR;
3608171172Smlaier				if ($9->host->next != NULL) {
3609130617Smlaier					if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
3610126353Smlaier					    PF_POOL_ROUNDROBIN) {
3611126353Smlaier						yyerror("only round-robin "
3612126353Smlaier						    "valid for multiple "
3613126353Smlaier						    "redirection addresses");
3614126353Smlaier						YYERROR;
3615126353Smlaier					}
3616126353Smlaier				}
3617126353Smlaier			}
3618126353Smlaier
3619171172Smlaier			if ($10.key != NULL)
3620171172Smlaier				memcpy(&r.rpool.key, $10.key,
3621126353Smlaier				    sizeof(struct pf_poolhashkey));
3622126353Smlaier
3623171172Smlaier			 if ($10.opts)
3624171172Smlaier				r.rpool.opts |= $10.opts;
3625130617Smlaier
3626171172Smlaier			if ($10.staticport) {
3627126353Smlaier				if (r.action != PF_NAT) {
3628126353Smlaier					yyerror("the 'static-port' option is "
3629126353Smlaier					    "only valid with nat rules");
3630126353Smlaier					YYERROR;
3631126353Smlaier				}
3632126353Smlaier				if (r.rpool.proxy_port[0] !=
3633126353Smlaier				    PF_NAT_PROXY_PORT_LOW &&
3634126353Smlaier				    r.rpool.proxy_port[1] !=
3635126353Smlaier				    PF_NAT_PROXY_PORT_HIGH) {
3636126353Smlaier					yyerror("the 'static-port' option can't"
3637126353Smlaier					    " be used when specifying a port"
3638126353Smlaier					    " range");
3639126353Smlaier					YYERROR;
3640126353Smlaier				}
3641126353Smlaier				r.rpool.proxy_port[0] = 0;
3642126353Smlaier				r.rpool.proxy_port[1] = 0;
3643126353Smlaier			}
3644126353Smlaier
3645171172Smlaier			expand_rule(&r, $2, $9 == NULL ? NULL : $9->host, $4,
3646126353Smlaier			    $5.src_os, $5.src.host, $5.src.port, $5.dst.host,
3647145840Smlaier			    $5.dst.port, 0, 0, 0, "");
3648171172Smlaier			free($9);
3649126353Smlaier		}
3650126353Smlaier		;
3651126353Smlaier
3652171172Smlaierbinatrule	: no BINAT natpass interface af proto FROM host TO ipspec tag
3653171172Smlaier		    tagged rtable redirection
3654126353Smlaier		{
3655126353Smlaier			struct pf_rule		binat;
3656126353Smlaier			struct pf_pooladdr	*pa;
3657126353Smlaier
3658126353Smlaier			if (check_rulestate(PFCTL_STATE_NAT))
3659126353Smlaier				YYERROR;
3660171172Smlaier			if (disallow_urpf_failed($10, "\"urpf-failed\" is not "
3661171172Smlaier			    "permitted as a binat destination"))
3662171172Smlaier				YYERROR;
3663126353Smlaier
3664126353Smlaier			memset(&binat, 0, sizeof(binat));
3665126353Smlaier
3666171172Smlaier			if ($1 && $3.b1) {
3667171172Smlaier				yyerror("\"pass\" not valid with \"no\"");
3668171172Smlaier				YYERROR;
3669171172Smlaier			}
3670126353Smlaier			if ($1)
3671126353Smlaier				binat.action = PF_NOBINAT;
3672126353Smlaier			else
3673126353Smlaier				binat.action = PF_BINAT;
3674171172Smlaier			binat.natpass = $3.b1;
3675171172Smlaier			binat.log = $3.b2;
3676171172Smlaier			binat.logif = $3.w2;
3677126353Smlaier			binat.af = $5;
3678126353Smlaier			if (!binat.af && $8 != NULL && $8->af)
3679126353Smlaier				binat.af = $8->af;
3680126353Smlaier			if (!binat.af && $10 != NULL && $10->af)
3681126353Smlaier				binat.af = $10->af;
3682145840Smlaier
3683171172Smlaier			if (!binat.af && $14 != NULL && $14->host)
3684171172Smlaier				binat.af = $14->host->af;
3685126353Smlaier			if (!binat.af) {
3686126353Smlaier				yyerror("address family (inet/inet6) "
3687126353Smlaier				    "undefined");
3688126353Smlaier				YYERROR;
3689126353Smlaier			}
3690126353Smlaier
3691126353Smlaier			if ($4 != NULL) {
3692126353Smlaier				memcpy(binat.ifname, $4->ifname,
3693126353Smlaier				    sizeof(binat.ifname));
3694130617Smlaier				binat.ifnot = $4->not;
3695126353Smlaier				free($4);
3696126353Smlaier			}
3697145840Smlaier
3698126353Smlaier			if ($11 != NULL)
3699126353Smlaier				if (strlcpy(binat.tagname, $11,
3700130617Smlaier				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
3701126353Smlaier					yyerror("tag too long, max %u chars",
3702126353Smlaier					    PF_TAG_NAME_SIZE - 1);
3703126353Smlaier					YYERROR;
3704126353Smlaier				}
3705145840Smlaier			if ($12.name)
3706145840Smlaier				if (strlcpy(binat.match_tagname, $12.name,
3707145840Smlaier				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
3708145840Smlaier					yyerror("tag too long, max %u chars",
3709145840Smlaier					    PF_TAG_NAME_SIZE - 1);
3710145840Smlaier					YYERROR;
3711145840Smlaier				}
3712145840Smlaier			binat.match_tag_not = $12.neg;
3713171172Smlaier			binat.rtableid = $13;
3714126353Smlaier
3715126353Smlaier			if ($6 != NULL) {
3716126353Smlaier				binat.proto = $6->proto;
3717126353Smlaier				free($6);
3718126353Smlaier			}
3719126353Smlaier
3720126353Smlaier			if ($8 != NULL && disallow_table($8, "invalid use of "
3721126353Smlaier			    "table <%s> as the source address of a binat rule"))
3722126353Smlaier				YYERROR;
3723130617Smlaier			if ($8 != NULL && disallow_alias($8, "invalid use of "
3724130617Smlaier			    "interface (%s) as the source address of a binat "
3725130617Smlaier			    "rule"))
3726130617Smlaier				YYERROR;
3727171172Smlaier			if ($14 != NULL && $14->host != NULL && disallow_table(
3728171172Smlaier			    $14->host, "invalid use of table <%s> as the "
3729126353Smlaier			    "redirect address of a binat rule"))
3730126353Smlaier				YYERROR;
3731171172Smlaier			if ($14 != NULL && $14->host != NULL && disallow_alias(
3732171172Smlaier			    $14->host, "invalid use of interface (%s) as the "
3733130617Smlaier			    "redirect address of a binat rule"))
3734130617Smlaier				YYERROR;
3735126353Smlaier
3736126353Smlaier			if ($8 != NULL) {
3737126353Smlaier				if ($8->next) {
3738126353Smlaier					yyerror("multiple binat ip addresses");
3739126353Smlaier					YYERROR;
3740126353Smlaier				}
3741126353Smlaier				if ($8->addr.type == PF_ADDR_DYNIFTL)
3742126353Smlaier					$8->af = binat.af;
3743126353Smlaier				if ($8->af != binat.af) {
3744126353Smlaier					yyerror("binat ip versions must match");
3745126353Smlaier					YYERROR;
3746126353Smlaier				}
3747126353Smlaier				if (check_netmask($8, binat.af))
3748126353Smlaier					YYERROR;
3749126353Smlaier				memcpy(&binat.src.addr, &$8->addr,
3750126353Smlaier				    sizeof(binat.src.addr));
3751126353Smlaier				free($8);
3752126353Smlaier			}
3753126353Smlaier			if ($10 != NULL) {
3754126353Smlaier				if ($10->next) {
3755126353Smlaier					yyerror("multiple binat ip addresses");
3756126353Smlaier					YYERROR;
3757126353Smlaier				}
3758126353Smlaier				if ($10->af != binat.af && $10->af) {
3759126353Smlaier					yyerror("binat ip versions must match");
3760126353Smlaier					YYERROR;
3761126353Smlaier				}
3762126353Smlaier				if (check_netmask($10, binat.af))
3763126353Smlaier					YYERROR;
3764126353Smlaier				memcpy(&binat.dst.addr, &$10->addr,
3765126353Smlaier				    sizeof(binat.dst.addr));
3766145840Smlaier				binat.dst.neg = $10->not;
3767126353Smlaier				free($10);
3768126353Smlaier			}
3769126353Smlaier
3770126353Smlaier			if (binat.action == PF_NOBINAT) {
3771171172Smlaier				if ($14 != NULL) {
3772126353Smlaier					yyerror("'no binat' rule does not need"
3773126353Smlaier					    " '->'");
3774126353Smlaier					YYERROR;
3775126353Smlaier				}
3776126353Smlaier			} else {
3777171172Smlaier				if ($14 == NULL || $14->host == NULL) {
3778126353Smlaier					yyerror("'binat' rule requires"
3779126353Smlaier					    " '-> address'");
3780126353Smlaier					YYERROR;
3781126353Smlaier				}
3782126353Smlaier
3783171172Smlaier				remove_invalid_hosts(&$14->host, &binat.af);
3784171172Smlaier				if (invalid_redirect($14->host, binat.af))
3785126353Smlaier					YYERROR;
3786171172Smlaier				if ($14->host->next != NULL) {
3787126353Smlaier					yyerror("binat rule must redirect to "
3788126353Smlaier					    "a single address");
3789126353Smlaier					YYERROR;
3790126353Smlaier				}
3791171172Smlaier				if (check_netmask($14->host, binat.af))
3792126353Smlaier					YYERROR;
3793126353Smlaier
3794126353Smlaier				if (!PF_AZERO(&binat.src.addr.v.a.mask,
3795126353Smlaier				    binat.af) &&
3796126353Smlaier				    !PF_AEQ(&binat.src.addr.v.a.mask,
3797171172Smlaier				    &$14->host->addr.v.a.mask, binat.af)) {
3798126353Smlaier					yyerror("'binat' source mask and "
3799126353Smlaier					    "redirect mask must be the same");
3800126353Smlaier					YYERROR;
3801126353Smlaier				}
3802126353Smlaier
3803126353Smlaier				TAILQ_INIT(&binat.rpool.list);
3804126353Smlaier				pa = calloc(1, sizeof(struct pf_pooladdr));
3805126353Smlaier				if (pa == NULL)
3806126353Smlaier					err(1, "binat: calloc");
3807171172Smlaier				pa->addr = $14->host->addr;
3808126353Smlaier				pa->ifname[0] = 0;
3809126353Smlaier				TAILQ_INSERT_TAIL(&binat.rpool.list,
3810126353Smlaier				    pa, entries);
3811126353Smlaier
3812171172Smlaier				free($14);
3813126353Smlaier			}
3814126353Smlaier
3815145840Smlaier			pfctl_add_rule(pf, &binat, "");
3816126353Smlaier		}
3817126353Smlaier		;
3818126353Smlaier
3819126353Smlaiertag		: /* empty */		{ $$ = NULL; }
3820126353Smlaier		| TAG STRING		{ $$ = $2; }
3821130617Smlaier		;
3822126353Smlaier
3823145840Smlaiertagged		: /* empty */		{ $$.neg = 0; $$.name = NULL; }
3824145840Smlaier		| not TAGGED string	{ $$.neg = $1; $$.name = $3; }
3825145840Smlaier		;
3826145840Smlaier
3827171172Smlaierrtable		: /* empty */		{ $$ = -1; }
3828171172Smlaier		| RTABLE number		{
3829171172Smlaier#ifdef __FreeBSD__
3830171172Smlaier			yyerror("rtable id not supported in FreeBSD, yet");
3831171172Smlaier			YYERROR;
3832171172Smlaier#else
3833171172Smlaier			if ($2 > RT_TABLEID_MAX || $2 < 0) {
3834171172Smlaier				yyerror("invalid rtable id");
3835171172Smlaier				YYERROR;
3836171172Smlaier			}
3837171172Smlaier			$$ = $2;
3838171172Smlaier#endif
3839171172Smlaier		}
3840171172Smlaier		;
3841171172Smlaier
3842126353Smlaierroute_host	: STRING			{
3843126353Smlaier			$$ = calloc(1, sizeof(struct node_host));
3844126353Smlaier			if ($$ == NULL)
3845126353Smlaier				err(1, "route_host: calloc");
3846130617Smlaier			$$->ifname = $1;
3847126353Smlaier			set_ipmask($$, 128);
3848126353Smlaier			$$->next = NULL;
3849126353Smlaier			$$->tail = $$;
3850126353Smlaier		}
3851126353Smlaier		| '(' STRING host ')'		{
3852126353Smlaier			$$ = $3;
3853130617Smlaier			$$->ifname = $2;
3854126353Smlaier		}
3855126353Smlaier		;
3856126353Smlaier
3857126353Smlaierroute_host_list	: route_host				{ $$ = $1; }
3858126353Smlaier		| route_host_list comma route_host	{
3859126353Smlaier			if ($1->af == 0)
3860126353Smlaier				$1->af = $3->af;
3861126353Smlaier			if ($1->af != $3->af) {
3862126353Smlaier				yyerror("all pool addresses must be in the "
3863126353Smlaier				    "same address family");
3864126353Smlaier				YYERROR;
3865126353Smlaier			}
3866126353Smlaier			$1->tail->next = $3;
3867126353Smlaier			$1->tail = $3->tail;
3868126353Smlaier			$$ = $1;
3869126353Smlaier		}
3870126353Smlaier		;
3871126353Smlaier
3872126353Smlaierroutespec	: route_host			{ $$ = $1; }
3873126353Smlaier		| '{' route_host_list '}'	{ $$ = $2; }
3874126353Smlaier		;
3875126353Smlaier
3876126353Smlaierroute		: /* empty */			{
3877126353Smlaier			$$.host = NULL;
3878126353Smlaier			$$.rt = 0;
3879126353Smlaier			$$.pool_opts = 0;
3880126353Smlaier		}
3881126353Smlaier		| FASTROUTE {
3882126353Smlaier			$$.host = NULL;
3883126353Smlaier			$$.rt = PF_FASTROUTE;
3884126353Smlaier			$$.pool_opts = 0;
3885126353Smlaier		}
3886130617Smlaier		| ROUTETO routespec pool_opts {
3887126353Smlaier			$$.host = $2;
3888126353Smlaier			$$.rt = PF_ROUTETO;
3889130617Smlaier			$$.pool_opts = $3.type | $3.opts;
3890126353Smlaier			if ($3.key != NULL)
3891126353Smlaier				$$.key = $3.key;
3892126353Smlaier		}
3893130617Smlaier		| REPLYTO routespec pool_opts {
3894126353Smlaier			$$.host = $2;
3895126353Smlaier			$$.rt = PF_REPLYTO;
3896130617Smlaier			$$.pool_opts = $3.type | $3.opts;
3897126353Smlaier			if ($3.key != NULL)
3898126353Smlaier				$$.key = $3.key;
3899126353Smlaier		}
3900130617Smlaier		| DUPTO routespec pool_opts {
3901126353Smlaier			$$.host = $2;
3902126353Smlaier			$$.rt = PF_DUPTO;
3903130617Smlaier			$$.pool_opts = $3.type | $3.opts;
3904126353Smlaier			if ($3.key != NULL)
3905126353Smlaier				$$.key = $3.key;
3906126353Smlaier		}
3907126353Smlaier		;
3908126353Smlaier
3909126353Smlaiertimeout_spec	: STRING number
3910126353Smlaier		{
3911130617Smlaier			if (check_rulestate(PFCTL_STATE_OPTION)) {
3912130617Smlaier				free($1);
3913126353Smlaier				YYERROR;
3914130617Smlaier			}
3915126353Smlaier			if (pfctl_set_timeout(pf, $1, $2, 0) != 0) {
3916126353Smlaier				yyerror("unknown timeout %s", $1);
3917130617Smlaier				free($1);
3918126353Smlaier				YYERROR;
3919126353Smlaier			}
3920130617Smlaier			free($1);
3921126353Smlaier		}
3922126353Smlaier		;
3923126353Smlaier
3924126353Smlaiertimeout_list	: timeout_list comma timeout_spec
3925126353Smlaier		| timeout_spec
3926126353Smlaier		;
3927126353Smlaier
3928126353Smlaierlimit_spec	: STRING number
3929126353Smlaier		{
3930130617Smlaier			if (check_rulestate(PFCTL_STATE_OPTION)) {
3931130617Smlaier				free($1);
3932126353Smlaier				YYERROR;
3933130617Smlaier			}
3934126353Smlaier			if (pfctl_set_limit(pf, $1, $2) != 0) {
3935126353Smlaier				yyerror("unable to set limit %s %u", $1, $2);
3936130617Smlaier				free($1);
3937126353Smlaier				YYERROR;
3938126353Smlaier			}
3939130617Smlaier			free($1);
3940126353Smlaier		}
3941130617Smlaier		;
3942126353Smlaier
3943126353Smlaierlimit_list	: limit_list comma limit_spec
3944126353Smlaier		| limit_spec
3945126353Smlaier		;
3946126353Smlaier
3947126353Smlaiercomma		: ','
3948126353Smlaier		| /* empty */
3949126353Smlaier		;
3950126353Smlaier
3951126353Smlaieryesno		: NO			{ $$ = 0; }
3952126353Smlaier		| STRING		{
3953126353Smlaier			if (!strcmp($1, "yes"))
3954126353Smlaier				$$ = 1;
3955130617Smlaier			else {
3956145840Smlaier				yyerror("invalid value '%s', expected 'yes' "
3957145840Smlaier				    "or 'no'", $1);
3958130617Smlaier				free($1);
3959126353Smlaier				YYERROR;
3960130617Smlaier			}
3961130617Smlaier			free($1);
3962126353Smlaier		}
3963130617Smlaier		;
3964126353Smlaier
3965126353Smlaierunaryop		: '='		{ $$ = PF_OP_EQ; }
3966126353Smlaier		| '!' '='	{ $$ = PF_OP_NE; }
3967126353Smlaier		| '<' '='	{ $$ = PF_OP_LE; }
3968126353Smlaier		| '<'		{ $$ = PF_OP_LT; }
3969126353Smlaier		| '>' '='	{ $$ = PF_OP_GE; }
3970126353Smlaier		| '>'		{ $$ = PF_OP_GT; }
3971126353Smlaier		;
3972126353Smlaier
3973126353Smlaier%%
3974126353Smlaier
3975126353Smlaierint
3976126353Smlaieryyerror(const char *fmt, ...)
3977126353Smlaier{
3978126353Smlaier	va_list		 ap;
3979126353Smlaier	extern char	*infile;
3980126353Smlaier
3981126353Smlaier	errors = 1;
3982126353Smlaier	va_start(ap, fmt);
3983126353Smlaier	fprintf(stderr, "%s:%d: ", infile, yylval.lineno);
3984126353Smlaier	vfprintf(stderr, fmt, ap);
3985126353Smlaier	fprintf(stderr, "\n");
3986126353Smlaier	va_end(ap);
3987126353Smlaier	return (0);
3988126353Smlaier}
3989126353Smlaier
3990126353Smlaierint
3991126353Smlaierdisallow_table(struct node_host *h, const char *fmt)
3992126353Smlaier{
3993126353Smlaier	for (; h != NULL; h = h->next)
3994126353Smlaier		if (h->addr.type == PF_ADDR_TABLE) {
3995126353Smlaier			yyerror(fmt, h->addr.v.tblname);
3996126353Smlaier			return (1);
3997126353Smlaier		}
3998126353Smlaier	return (0);
3999126353Smlaier}
4000126353Smlaier
4001126353Smlaierint
4002171172Smlaierdisallow_urpf_failed(struct node_host *h, const char *fmt)
4003171172Smlaier{
4004171172Smlaier	for (; h != NULL; h = h->next)
4005171172Smlaier		if (h->addr.type == PF_ADDR_URPFFAILED) {
4006171172Smlaier			yyerror(fmt);
4007171172Smlaier			return (1);
4008171172Smlaier		}
4009171172Smlaier	return (0);
4010171172Smlaier}
4011171172Smlaier
4012171172Smlaierint
4013130617Smlaierdisallow_alias(struct node_host *h, const char *fmt)
4014130617Smlaier{
4015130617Smlaier	for (; h != NULL; h = h->next)
4016130617Smlaier		if (DYNIF_MULTIADDR(h->addr)) {
4017130617Smlaier			yyerror(fmt, h->addr.v.tblname);
4018130617Smlaier			return (1);
4019130617Smlaier		}
4020130617Smlaier	return (0);
4021130617Smlaier}
4022130617Smlaier
4023130617Smlaierint
4024171172Smlaierrule_consistent(struct pf_rule *r, int anchor_call)
4025126353Smlaier{
4026126353Smlaier	int	problems = 0;
4027126353Smlaier
4028126353Smlaier	switch (r->action) {
4029126353Smlaier	case PF_PASS:
4030126353Smlaier	case PF_DROP:
4031126353Smlaier	case PF_SCRUB:
4032145840Smlaier	case PF_NOSCRUB:
4033171172Smlaier		problems = filter_consistent(r, anchor_call);
4034126353Smlaier		break;
4035126353Smlaier	case PF_NAT:
4036126353Smlaier	case PF_NONAT:
4037126353Smlaier		problems = nat_consistent(r);
4038126353Smlaier		break;
4039126353Smlaier	case PF_RDR:
4040126353Smlaier	case PF_NORDR:
4041126353Smlaier		problems = rdr_consistent(r);
4042126353Smlaier		break;
4043126353Smlaier	case PF_BINAT:
4044126353Smlaier	case PF_NOBINAT:
4045126353Smlaier	default:
4046126353Smlaier		break;
4047126353Smlaier	}
4048126353Smlaier	return (problems);
4049126353Smlaier}
4050126353Smlaier
4051126353Smlaierint
4052171172Smlaierfilter_consistent(struct pf_rule *r, int anchor_call)
4053126353Smlaier{
4054126353Smlaier	int	problems = 0;
4055126353Smlaier
4056126353Smlaier	if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP &&
4057126353Smlaier	    (r->src.port_op || r->dst.port_op)) {
4058126353Smlaier		yyerror("port only applies to tcp/udp");
4059126353Smlaier		problems++;
4060126353Smlaier	}
4061126353Smlaier	if (r->proto != IPPROTO_ICMP && r->proto != IPPROTO_ICMPV6 &&
4062126353Smlaier	    (r->type || r->code)) {
4063126353Smlaier		yyerror("icmp-type/code only applies to icmp");
4064126353Smlaier		problems++;
4065126353Smlaier	}
4066126353Smlaier	if (!r->af && (r->type || r->code)) {
4067126353Smlaier		yyerror("must indicate address family with icmp-type/code");
4068126353Smlaier		problems++;
4069126353Smlaier	}
4070145840Smlaier	if (r->overload_tblname[0] &&
4071145840Smlaier	    r->max_src_conn == 0 && r->max_src_conn_rate.seconds == 0) {
4072145840Smlaier		yyerror("'overload' requires 'max-src-conn' "
4073145840Smlaier		    "or 'max-src-conn-rate'");
4074145840Smlaier		problems++;
4075145840Smlaier	}
4076126353Smlaier	if ((r->proto == IPPROTO_ICMP && r->af == AF_INET6) ||
4077126353Smlaier	    (r->proto == IPPROTO_ICMPV6 && r->af == AF_INET)) {
4078126353Smlaier		yyerror("proto %s doesn't match address family %s",
4079126353Smlaier		    r->proto == IPPROTO_ICMP ? "icmp" : "icmp6",
4080126353Smlaier		    r->af == AF_INET ? "inet" : "inet6");
4081126353Smlaier		problems++;
4082126353Smlaier	}
4083126353Smlaier	if (r->allow_opts && r->action != PF_PASS) {
4084126353Smlaier		yyerror("allow-opts can only be specified for pass rules");
4085126353Smlaier		problems++;
4086126353Smlaier	}
4087126353Smlaier	if (r->rule_flag & PFRULE_FRAGMENT && (r->src.port_op ||
4088126353Smlaier	    r->dst.port_op || r->flagset || r->type || r->code)) {
4089126353Smlaier		yyerror("fragments can be filtered only on IP header fields");
4090126353Smlaier		problems++;
4091126353Smlaier	}
4092126353Smlaier	if (r->rule_flag & PFRULE_RETURNRST && r->proto != IPPROTO_TCP) {
4093126353Smlaier		yyerror("return-rst can only be applied to TCP rules");
4094126353Smlaier		problems++;
4095126353Smlaier	}
4096130617Smlaier	if (r->max_src_nodes && !(r->rule_flag & PFRULE_RULESRCTRACK)) {
4097130617Smlaier		yyerror("max-src-nodes requires 'source-track rule'");
4098130617Smlaier		problems++;
4099130617Smlaier	}
4100126353Smlaier	if (r->action == PF_DROP && r->keep_state) {
4101126353Smlaier		yyerror("keep state on block rules doesn't make sense");
4102126353Smlaier		problems++;
4103126353Smlaier	}
4104126353Smlaier	return (-problems);
4105126353Smlaier}
4106126353Smlaier
4107126353Smlaierint
4108126353Smlaiernat_consistent(struct pf_rule *r)
4109126353Smlaier{
4110130617Smlaier	return (0);	/* yeah! */
4111126353Smlaier}
4112126353Smlaier
4113126353Smlaierint
4114126353Smlaierrdr_consistent(struct pf_rule *r)
4115126353Smlaier{
4116126353Smlaier	int			 problems = 0;
4117126353Smlaier
4118126353Smlaier	if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP) {
4119126353Smlaier		if (r->src.port_op) {
4120126353Smlaier			yyerror("src port only applies to tcp/udp");
4121126353Smlaier			problems++;
4122126353Smlaier		}
4123126353Smlaier		if (r->dst.port_op) {
4124126353Smlaier			yyerror("dst port only applies to tcp/udp");
4125126353Smlaier			problems++;
4126126353Smlaier		}
4127126353Smlaier		if (r->rpool.proxy_port[0]) {
4128126353Smlaier			yyerror("rpool port only applies to tcp/udp");
4129126353Smlaier			problems++;
4130126353Smlaier		}
4131126353Smlaier	}
4132126353Smlaier	if (r->dst.port_op &&
4133126353Smlaier	    r->dst.port_op != PF_OP_EQ && r->dst.port_op != PF_OP_RRG) {
4134126353Smlaier		yyerror("invalid port operator for rdr destination port");
4135126353Smlaier		problems++;
4136126353Smlaier	}
4137126353Smlaier	return (-problems);
4138126353Smlaier}
4139126353Smlaier
4140126353Smlaierint
4141126353Smlaierprocess_tabledef(char *name, struct table_opts *opts)
4142126353Smlaier{
4143126353Smlaier	struct pfr_buffer	 ab;
4144126353Smlaier	struct node_tinit	*ti;
4145126353Smlaier
4146126353Smlaier	bzero(&ab, sizeof(ab));
4147126353Smlaier	ab.pfrb_type = PFRB_ADDRS;
4148126353Smlaier	SIMPLEQ_FOREACH(ti, &opts->init_nodes, entries) {
4149126353Smlaier		if (ti->file)
4150126353Smlaier			if (pfr_buf_load(&ab, ti->file, 0, append_addr)) {
4151126353Smlaier				if (errno)
4152126353Smlaier					yyerror("cannot load \"%s\": %s",
4153126353Smlaier					    ti->file, strerror(errno));
4154126353Smlaier				else
4155126353Smlaier					yyerror("file \"%s\" contains bad data",
4156126353Smlaier					    ti->file);
4157126353Smlaier				goto _error;
4158126353Smlaier			}
4159126353Smlaier		if (ti->host)
4160126353Smlaier			if (append_addr_host(&ab, ti->host, 0, 0)) {
4161126353Smlaier				yyerror("cannot create address buffer: %s",
4162126353Smlaier				    strerror(errno));
4163126353Smlaier				goto _error;
4164126353Smlaier			}
4165126353Smlaier	}
4166126353Smlaier	if (pf->opts & PF_OPT_VERBOSE)
4167126353Smlaier		print_tabledef(name, opts->flags, opts->init_addr,
4168126353Smlaier		    &opts->init_nodes);
4169126353Smlaier	if (!(pf->opts & PF_OPT_NOACTION) &&
4170126353Smlaier	    pfctl_define_table(name, opts->flags, opts->init_addr,
4171171172Smlaier	    pf->anchor->name, &ab, pf->anchor->ruleset.tticket)) {
4172126353Smlaier		yyerror("cannot define table %s: %s", name,
4173126353Smlaier		    pfr_strerror(errno));
4174126353Smlaier		goto _error;
4175126353Smlaier	}
4176126353Smlaier	pf->tdirty = 1;
4177126353Smlaier	pfr_buf_clear(&ab);
4178126353Smlaier	return (0);
4179126353Smlaier_error:
4180126353Smlaier	pfr_buf_clear(&ab);
4181126353Smlaier	return (-1);
4182126353Smlaier}
4183126353Smlaier
4184126353Smlaierstruct keywords {
4185126353Smlaier	const char	*k_name;
4186126353Smlaier	int		 k_val;
4187126353Smlaier};
4188126353Smlaier
4189126353Smlaier/* macro gore, but you should've seen the prior indentation nightmare... */
4190126353Smlaier
4191126353Smlaier#define FREE_LIST(T,r) \
4192126353Smlaier	do { \
4193126353Smlaier		T *p, *node = r; \
4194126353Smlaier		while (node != NULL) { \
4195126353Smlaier			p = node; \
4196126353Smlaier			node = node->next; \
4197126353Smlaier			free(p); \
4198126353Smlaier		} \
4199126353Smlaier	} while (0)
4200126353Smlaier
4201126353Smlaier#define LOOP_THROUGH(T,n,r,C) \
4202126353Smlaier	do { \
4203126353Smlaier		T *n; \
4204126353Smlaier		if (r == NULL) { \
4205126353Smlaier			r = calloc(1, sizeof(T)); \
4206126353Smlaier			if (r == NULL) \
4207126353Smlaier				err(1, "LOOP: calloc"); \
4208126353Smlaier			r->next = NULL; \
4209126353Smlaier		} \
4210126353Smlaier		n = r; \
4211126353Smlaier		while (n != NULL) { \
4212126353Smlaier			do { \
4213126353Smlaier				C; \
4214126353Smlaier			} while (0); \
4215126353Smlaier			n = n->next; \
4216126353Smlaier		} \
4217126353Smlaier	} while (0)
4218126353Smlaier
4219126353Smlaiervoid
4220130617Smlaierexpand_label_str(char *label, size_t len, const char *srch, const char *repl)
4221126353Smlaier{
4222130617Smlaier	char *tmp;
4223126353Smlaier	char *p, *q;
4224126353Smlaier
4225130617Smlaier	if ((tmp = calloc(1, len)) == NULL)
4226130617Smlaier		err(1, "expand_label_str: calloc");
4227126353Smlaier	p = q = label;
4228126353Smlaier	while ((q = strstr(p, srch)) != NULL) {
4229126353Smlaier		*q = '\0';
4230130617Smlaier		if ((strlcat(tmp, p, len) >= len) ||
4231130617Smlaier		    (strlcat(tmp, repl, len) >= len))
4232130617Smlaier			errx(1, "expand_label: label too long");
4233126353Smlaier		q += strlen(srch);
4234126353Smlaier		p = q;
4235126353Smlaier	}
4236130617Smlaier	if (strlcat(tmp, p, len) >= len)
4237130617Smlaier		errx(1, "expand_label: label too long");
4238130617Smlaier	strlcpy(label, tmp, len);	/* always fits */
4239130617Smlaier	free(tmp);
4240126353Smlaier}
4241126353Smlaier
4242126353Smlaiervoid
4243130617Smlaierexpand_label_if(const char *name, char *label, size_t len, const char *ifname)
4244126353Smlaier{
4245126353Smlaier	if (strstr(label, name) != NULL) {
4246126353Smlaier		if (!*ifname)
4247130617Smlaier			expand_label_str(label, len, name, "any");
4248126353Smlaier		else
4249130617Smlaier			expand_label_str(label, len, name, ifname);
4250126353Smlaier	}
4251126353Smlaier}
4252126353Smlaier
4253126353Smlaiervoid
4254130617Smlaierexpand_label_addr(const char *name, char *label, size_t len, sa_family_t af,
4255126353Smlaier    struct node_host *h)
4256126353Smlaier{
4257126353Smlaier	char tmp[64], tmp_not[66];
4258126353Smlaier
4259126353Smlaier	if (strstr(label, name) != NULL) {
4260126353Smlaier		switch (h->addr.type) {
4261126353Smlaier		case PF_ADDR_DYNIFTL:
4262126353Smlaier			snprintf(tmp, sizeof(tmp), "(%s)", h->addr.v.ifname);
4263126353Smlaier			break;
4264126353Smlaier		case PF_ADDR_TABLE:
4265126353Smlaier			snprintf(tmp, sizeof(tmp), "<%s>", h->addr.v.tblname);
4266126353Smlaier			break;
4267126353Smlaier		case PF_ADDR_NOROUTE:
4268126353Smlaier			snprintf(tmp, sizeof(tmp), "no-route");
4269126353Smlaier			break;
4270171172Smlaier		case PF_ADDR_URPFFAILED:
4271171172Smlaier			snprintf(tmp, sizeof(tmp), "urpf-failed");
4272171172Smlaier			break;
4273126353Smlaier		case PF_ADDR_ADDRMASK:
4274126353Smlaier			if (!af || (PF_AZERO(&h->addr.v.a.addr, af) &&
4275126353Smlaier			    PF_AZERO(&h->addr.v.a.mask, af)))
4276126353Smlaier				snprintf(tmp, sizeof(tmp), "any");
4277126353Smlaier			else {
4278126353Smlaier				char	a[48];
4279126353Smlaier				int	bits;
4280126353Smlaier
4281126353Smlaier				if (inet_ntop(af, &h->addr.v.a.addr, a,
4282126353Smlaier				    sizeof(a)) == NULL)
4283126353Smlaier					snprintf(tmp, sizeof(tmp), "?");
4284126353Smlaier				else {
4285126353Smlaier					bits = unmask(&h->addr.v.a.mask, af);
4286126353Smlaier					if ((af == AF_INET && bits < 32) ||
4287126353Smlaier					    (af == AF_INET6 && bits < 128))
4288126353Smlaier						snprintf(tmp, sizeof(tmp),
4289130617Smlaier						    "%s/%d", a, bits);
4290126353Smlaier					else
4291126353Smlaier						snprintf(tmp, sizeof(tmp),
4292126353Smlaier						    "%s", a);
4293126353Smlaier				}
4294126353Smlaier			}
4295126353Smlaier			break;
4296126353Smlaier		default:
4297126353Smlaier			snprintf(tmp, sizeof(tmp), "?");
4298126353Smlaier			break;
4299126353Smlaier		}
4300126353Smlaier
4301126353Smlaier		if (h->not) {
4302126353Smlaier			snprintf(tmp_not, sizeof(tmp_not), "! %s", tmp);
4303130617Smlaier			expand_label_str(label, len, name, tmp_not);
4304126353Smlaier		} else
4305130617Smlaier			expand_label_str(label, len, name, tmp);
4306126353Smlaier	}
4307126353Smlaier}
4308126353Smlaier
4309126353Smlaiervoid
4310130617Smlaierexpand_label_port(const char *name, char *label, size_t len,
4311130617Smlaier    struct node_port *port)
4312126353Smlaier{
4313126353Smlaier	char	 a1[6], a2[6], op[13] = "";
4314126353Smlaier
4315126353Smlaier	if (strstr(label, name) != NULL) {
4316126353Smlaier		snprintf(a1, sizeof(a1), "%u", ntohs(port->port[0]));
4317126353Smlaier		snprintf(a2, sizeof(a2), "%u", ntohs(port->port[1]));
4318126353Smlaier		if (!port->op)
4319126353Smlaier			;
4320126353Smlaier		else if (port->op == PF_OP_IRG)
4321126353Smlaier			snprintf(op, sizeof(op), "%s><%s", a1, a2);
4322126353Smlaier		else if (port->op == PF_OP_XRG)
4323126353Smlaier			snprintf(op, sizeof(op), "%s<>%s", a1, a2);
4324126353Smlaier		else if (port->op == PF_OP_EQ)
4325126353Smlaier			snprintf(op, sizeof(op), "%s", a1);
4326126353Smlaier		else if (port->op == PF_OP_NE)
4327126353Smlaier			snprintf(op, sizeof(op), "!=%s", a1);
4328126353Smlaier		else if (port->op == PF_OP_LT)
4329126353Smlaier			snprintf(op, sizeof(op), "<%s", a1);
4330126353Smlaier		else if (port->op == PF_OP_LE)
4331126353Smlaier			snprintf(op, sizeof(op), "<=%s", a1);
4332126353Smlaier		else if (port->op == PF_OP_GT)
4333126353Smlaier			snprintf(op, sizeof(op), ">%s", a1);
4334126353Smlaier		else if (port->op == PF_OP_GE)
4335126353Smlaier			snprintf(op, sizeof(op), ">=%s", a1);
4336130617Smlaier		expand_label_str(label, len, name, op);
4337126353Smlaier	}
4338126353Smlaier}
4339126353Smlaier
4340126353Smlaiervoid
4341130617Smlaierexpand_label_proto(const char *name, char *label, size_t len, u_int8_t proto)
4342126353Smlaier{
4343126353Smlaier	struct protoent *pe;
4344126353Smlaier	char n[4];
4345126353Smlaier
4346126353Smlaier	if (strstr(label, name) != NULL) {
4347126353Smlaier		pe = getprotobynumber(proto);
4348126353Smlaier		if (pe != NULL)
4349130617Smlaier			expand_label_str(label, len, name, pe->p_name);
4350126353Smlaier		else {
4351126353Smlaier			snprintf(n, sizeof(n), "%u", proto);
4352130617Smlaier			expand_label_str(label, len, name, n);
4353126353Smlaier		}
4354126353Smlaier	}
4355126353Smlaier}
4356126353Smlaier
4357126353Smlaiervoid
4358130617Smlaierexpand_label_nr(const char *name, char *label, size_t len)
4359126353Smlaier{
4360126353Smlaier	char n[11];
4361126353Smlaier
4362126353Smlaier	if (strstr(label, name) != NULL) {
4363171172Smlaier		snprintf(n, sizeof(n), "%u", pf->anchor->match);
4364130617Smlaier		expand_label_str(label, len, name, n);
4365126353Smlaier	}
4366126353Smlaier}
4367126353Smlaier
4368126353Smlaiervoid
4369130617Smlaierexpand_label(char *label, size_t len, const char *ifname, sa_family_t af,
4370126353Smlaier    struct node_host *src_host, struct node_port *src_port,
4371126353Smlaier    struct node_host *dst_host, struct node_port *dst_port,
4372126353Smlaier    u_int8_t proto)
4373126353Smlaier{
4374130617Smlaier	expand_label_if("$if", label, len, ifname);
4375130617Smlaier	expand_label_addr("$srcaddr", label, len, af, src_host);
4376130617Smlaier	expand_label_addr("$dstaddr", label, len, af, dst_host);
4377130617Smlaier	expand_label_port("$srcport", label, len, src_port);
4378130617Smlaier	expand_label_port("$dstport", label, len, dst_port);
4379130617Smlaier	expand_label_proto("$proto", label, len, proto);
4380130617Smlaier	expand_label_nr("$nr", label, len);
4381126353Smlaier}
4382126353Smlaier
4383126353Smlaierint
4384126353Smlaierexpand_altq(struct pf_altq *a, struct node_if *interfaces,
4385126353Smlaier    struct node_queue *nqueues, struct node_queue_bw bwspec,
4386126353Smlaier    struct node_queue_opt *opts)
4387126353Smlaier{
4388126353Smlaier	struct pf_altq		 pa, pb;
4389126353Smlaier	char			 qname[PF_QNAME_SIZE];
4390126353Smlaier	struct node_queue	*n;
4391126353Smlaier	struct node_queue_bw	 bw;
4392126353Smlaier	int			 errs = 0;
4393126353Smlaier
4394126353Smlaier	if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
4395126353Smlaier		FREE_LIST(struct node_if, interfaces);
4396126353Smlaier		FREE_LIST(struct node_queue, nqueues);
4397126353Smlaier		return (0);
4398126353Smlaier	}
4399126353Smlaier
4400126353Smlaier	LOOP_THROUGH(struct node_if, interface, interfaces,
4401126353Smlaier		memcpy(&pa, a, sizeof(struct pf_altq));
4402126353Smlaier		if (strlcpy(pa.ifname, interface->ifname,
4403126353Smlaier		    sizeof(pa.ifname)) >= sizeof(pa.ifname))
4404126353Smlaier			errx(1, "expand_altq: strlcpy");
4405126353Smlaier
4406126353Smlaier		if (interface->not) {
4407126353Smlaier			yyerror("altq on ! <interface> is not supported");
4408126353Smlaier			errs++;
4409126353Smlaier		} else {
4410126353Smlaier			if (eval_pfaltq(pf, &pa, &bwspec, opts))
4411126353Smlaier				errs++;
4412126353Smlaier			else
4413126353Smlaier				if (pfctl_add_altq(pf, &pa))
4414126353Smlaier					errs++;
4415126353Smlaier
4416126353Smlaier			if (pf->opts & PF_OPT_VERBOSE) {
4417126353Smlaier				print_altq(&pf->paltq->altq, 0,
4418126353Smlaier				    &bwspec, opts);
4419126353Smlaier				if (nqueues && nqueues->tail) {
4420126353Smlaier					printf("queue { ");
4421126353Smlaier					LOOP_THROUGH(struct node_queue, queue,
4422126353Smlaier					    nqueues,
4423126353Smlaier						printf("%s ",
4424126353Smlaier						    queue->queue);
4425126353Smlaier					);
4426126353Smlaier					printf("}");
4427126353Smlaier				}
4428126353Smlaier				printf("\n");
4429126353Smlaier			}
4430126353Smlaier
4431126353Smlaier			if (pa.scheduler == ALTQT_CBQ ||
4432126353Smlaier			    pa.scheduler == ALTQT_HFSC) {
4433126353Smlaier				/* now create a root queue */
4434126353Smlaier				memset(&pb, 0, sizeof(struct pf_altq));
4435126353Smlaier				if (strlcpy(qname, "root_", sizeof(qname)) >=
4436126353Smlaier				    sizeof(qname))
4437126353Smlaier					errx(1, "expand_altq: strlcpy");
4438126353Smlaier				if (strlcat(qname, interface->ifname,
4439126353Smlaier				    sizeof(qname)) >= sizeof(qname))
4440126353Smlaier					errx(1, "expand_altq: strlcat");
4441126353Smlaier				if (strlcpy(pb.qname, qname,
4442126353Smlaier				    sizeof(pb.qname)) >= sizeof(pb.qname))
4443126353Smlaier					errx(1, "expand_altq: strlcpy");
4444126353Smlaier				if (strlcpy(pb.ifname, interface->ifname,
4445126353Smlaier				    sizeof(pb.ifname)) >= sizeof(pb.ifname))
4446126353Smlaier					errx(1, "expand_altq: strlcpy");
4447126353Smlaier				pb.qlimit = pa.qlimit;
4448126353Smlaier				pb.scheduler = pa.scheduler;
4449126353Smlaier				bw.bw_absolute = pa.ifbandwidth;
4450126353Smlaier				bw.bw_percent = 0;
4451126353Smlaier				if (eval_pfqueue(pf, &pb, &bw, opts))
4452126353Smlaier					errs++;
4453126353Smlaier				else
4454126353Smlaier					if (pfctl_add_altq(pf, &pb))
4455126353Smlaier						errs++;
4456126353Smlaier			}
4457126353Smlaier
4458126353Smlaier			LOOP_THROUGH(struct node_queue, queue, nqueues,
4459126353Smlaier				n = calloc(1, sizeof(struct node_queue));
4460126353Smlaier				if (n == NULL)
4461126353Smlaier					err(1, "expand_altq: calloc");
4462126353Smlaier				if (pa.scheduler == ALTQT_CBQ ||
4463126353Smlaier				    pa.scheduler == ALTQT_HFSC)
4464126353Smlaier					if (strlcpy(n->parent, qname,
4465126353Smlaier					    sizeof(n->parent)) >=
4466126353Smlaier					    sizeof(n->parent))
4467126353Smlaier						errx(1, "expand_altq: strlcpy");
4468126353Smlaier				if (strlcpy(n->queue, queue->queue,
4469126353Smlaier				    sizeof(n->queue)) >= sizeof(n->queue))
4470126353Smlaier					errx(1, "expand_altq: strlcpy");
4471126353Smlaier				if (strlcpy(n->ifname, interface->ifname,
4472126353Smlaier				    sizeof(n->ifname)) >= sizeof(n->ifname))
4473126353Smlaier					errx(1, "expand_altq: strlcpy");
4474126353Smlaier				n->scheduler = pa.scheduler;
4475126353Smlaier				n->next = NULL;
4476126353Smlaier				n->tail = n;
4477126353Smlaier				if (queues == NULL)
4478126353Smlaier					queues = n;
4479126353Smlaier				else {
4480126353Smlaier					queues->tail->next = n;
4481126353Smlaier					queues->tail = n;
4482126353Smlaier				}
4483126353Smlaier			);
4484126353Smlaier		}
4485126353Smlaier	);
4486126353Smlaier	FREE_LIST(struct node_if, interfaces);
4487126353Smlaier	FREE_LIST(struct node_queue, nqueues);
4488126353Smlaier
4489126353Smlaier	return (errs);
4490126353Smlaier}
4491126353Smlaier
4492126353Smlaierint
4493126353Smlaierexpand_queue(struct pf_altq *a, struct node_if *interfaces,
4494126353Smlaier    struct node_queue *nqueues, struct node_queue_bw bwspec,
4495126353Smlaier    struct node_queue_opt *opts)
4496126353Smlaier{
4497126353Smlaier	struct node_queue	*n, *nq;
4498126353Smlaier	struct pf_altq		 pa;
4499126353Smlaier	u_int8_t		 found = 0;
4500126353Smlaier	u_int8_t		 errs = 0;
4501126353Smlaier
4502126353Smlaier	if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
4503126353Smlaier		FREE_LIST(struct node_queue, nqueues);
4504126353Smlaier		return (0);
4505126353Smlaier	}
4506126353Smlaier
4507126353Smlaier	if (queues == NULL) {
4508126353Smlaier		yyerror("queue %s has no parent", a->qname);
4509126353Smlaier		FREE_LIST(struct node_queue, nqueues);
4510126353Smlaier		return (1);
4511126353Smlaier	}
4512126353Smlaier
4513126353Smlaier	LOOP_THROUGH(struct node_if, interface, interfaces,
4514126353Smlaier		LOOP_THROUGH(struct node_queue, tqueue, queues,
4515126353Smlaier			if (!strncmp(a->qname, tqueue->queue, PF_QNAME_SIZE) &&
4516126353Smlaier			    (interface->ifname[0] == 0 ||
4517126353Smlaier			    (!interface->not && !strncmp(interface->ifname,
4518126353Smlaier			    tqueue->ifname, IFNAMSIZ)) ||
4519126353Smlaier			    (interface->not && strncmp(interface->ifname,
4520126353Smlaier			    tqueue->ifname, IFNAMSIZ)))) {
4521126353Smlaier				/* found ourself in queues */
4522126353Smlaier				found++;
4523126353Smlaier
4524126353Smlaier				memcpy(&pa, a, sizeof(struct pf_altq));
4525126353Smlaier
4526126353Smlaier				if (pa.scheduler != ALTQT_NONE &&
4527126353Smlaier				    pa.scheduler != tqueue->scheduler) {
4528126353Smlaier					yyerror("exactly one scheduler type "
4529126353Smlaier					    "per interface allowed");
4530126353Smlaier					return (1);
4531126353Smlaier				}
4532126353Smlaier				pa.scheduler = tqueue->scheduler;
4533126353Smlaier
4534126353Smlaier				/* scheduler dependent error checking */
4535126353Smlaier				switch (pa.scheduler) {
4536126353Smlaier				case ALTQT_PRIQ:
4537126353Smlaier					if (nqueues != NULL) {
4538126353Smlaier						yyerror("priq queues cannot "
4539126353Smlaier						    "have child queues");
4540126353Smlaier						return (1);
4541126353Smlaier					}
4542126353Smlaier					if (bwspec.bw_absolute > 0 ||
4543126353Smlaier					    bwspec.bw_percent < 100) {
4544126353Smlaier						yyerror("priq doesn't take "
4545126353Smlaier						    "bandwidth");
4546126353Smlaier						return (1);
4547126353Smlaier					}
4548126353Smlaier					break;
4549126353Smlaier				default:
4550126353Smlaier					break;
4551126353Smlaier				}
4552126353Smlaier
4553126353Smlaier				if (strlcpy(pa.ifname, tqueue->ifname,
4554126353Smlaier				    sizeof(pa.ifname)) >= sizeof(pa.ifname))
4555126353Smlaier					errx(1, "expand_queue: strlcpy");
4556126353Smlaier				if (strlcpy(pa.parent, tqueue->parent,
4557126353Smlaier				    sizeof(pa.parent)) >= sizeof(pa.parent))
4558126353Smlaier					errx(1, "expand_queue: strlcpy");
4559126353Smlaier
4560126353Smlaier				if (eval_pfqueue(pf, &pa, &bwspec, opts))
4561126353Smlaier					errs++;
4562126353Smlaier				else
4563126353Smlaier					if (pfctl_add_altq(pf, &pa))
4564126353Smlaier						errs++;
4565126353Smlaier
4566126353Smlaier				for (nq = nqueues; nq != NULL; nq = nq->next) {
4567126353Smlaier					if (!strcmp(a->qname, nq->queue)) {
4568126353Smlaier						yyerror("queue cannot have "
4569126353Smlaier						    "itself as child");
4570126353Smlaier						errs++;
4571126353Smlaier						continue;
4572126353Smlaier					}
4573126353Smlaier					n = calloc(1,
4574126353Smlaier					    sizeof(struct node_queue));
4575126353Smlaier					if (n == NULL)
4576126353Smlaier						err(1, "expand_queue: calloc");
4577126353Smlaier					if (strlcpy(n->parent, a->qname,
4578126353Smlaier					    sizeof(n->parent)) >=
4579126353Smlaier					    sizeof(n->parent))
4580126353Smlaier						errx(1, "expand_queue strlcpy");
4581126353Smlaier					if (strlcpy(n->queue, nq->queue,
4582126353Smlaier					    sizeof(n->queue)) >=
4583126353Smlaier					    sizeof(n->queue))
4584126353Smlaier						errx(1, "expand_queue strlcpy");
4585126353Smlaier					if (strlcpy(n->ifname, tqueue->ifname,
4586126353Smlaier					    sizeof(n->ifname)) >=
4587126353Smlaier					    sizeof(n->ifname))
4588126353Smlaier						errx(1, "expand_queue strlcpy");
4589126353Smlaier					n->scheduler = tqueue->scheduler;
4590126353Smlaier					n->next = NULL;
4591126353Smlaier					n->tail = n;
4592126353Smlaier					if (queues == NULL)
4593126353Smlaier						queues = n;
4594126353Smlaier					else {
4595126353Smlaier						queues->tail->next = n;
4596126353Smlaier						queues->tail = n;
4597126353Smlaier					}
4598126353Smlaier				}
4599126353Smlaier				if ((pf->opts & PF_OPT_VERBOSE) && (
4600126353Smlaier				    (found == 1 && interface->ifname[0] == 0) ||
4601126353Smlaier				    (found > 0 && interface->ifname[0] != 0))) {
4602126353Smlaier					print_queue(&pf->paltq->altq, 0,
4603126353Smlaier					    &bwspec, interface->ifname[0] != 0,
4604126353Smlaier					    opts);
4605126353Smlaier					if (nqueues && nqueues->tail) {
4606126353Smlaier						printf("{ ");
4607126353Smlaier						LOOP_THROUGH(struct node_queue,
4608126353Smlaier						    queue, nqueues,
4609126353Smlaier							printf("%s ",
4610126353Smlaier							    queue->queue);
4611126353Smlaier						);
4612126353Smlaier						printf("}");
4613126353Smlaier					}
4614126353Smlaier					printf("\n");
4615126353Smlaier				}
4616126353Smlaier			}
4617126353Smlaier		);
4618126353Smlaier	);
4619126353Smlaier
4620126353Smlaier	FREE_LIST(struct node_queue, nqueues);
4621126353Smlaier	FREE_LIST(struct node_if, interfaces);
4622126353Smlaier
4623126353Smlaier	if (!found) {
4624126353Smlaier		yyerror("queue %s has no parent", a->qname);
4625126353Smlaier		errs++;
4626126353Smlaier	}
4627126353Smlaier
4628126353Smlaier	if (errs)
4629126353Smlaier		return (1);
4630126353Smlaier	else
4631126353Smlaier		return (0);
4632126353Smlaier}
4633126353Smlaier
4634126353Smlaiervoid
4635126353Smlaierexpand_rule(struct pf_rule *r,
4636126353Smlaier    struct node_if *interfaces, struct node_host *rpool_hosts,
4637126353Smlaier    struct node_proto *protos, struct node_os *src_oses,
4638126353Smlaier    struct node_host *src_hosts, struct node_port *src_ports,
4639126353Smlaier    struct node_host *dst_hosts, struct node_port *dst_ports,
4640145840Smlaier    struct node_uid *uids, struct node_gid *gids, struct node_icmp *icmp_types,
4641145840Smlaier    const char *anchor_call)
4642126353Smlaier{
4643126353Smlaier	sa_family_t		 af = r->af;
4644126353Smlaier	int			 added = 0, error = 0;
4645126353Smlaier	char			 ifname[IF_NAMESIZE];
4646126353Smlaier	char			 label[PF_RULE_LABEL_SIZE];
4647130617Smlaier	char			 tagname[PF_TAG_NAME_SIZE];
4648130617Smlaier	char			 match_tagname[PF_TAG_NAME_SIZE];
4649126353Smlaier	struct pf_pooladdr	*pa;
4650126353Smlaier	struct node_host	*h;
4651130617Smlaier	u_int8_t		 flags, flagset, keep_state;
4652126353Smlaier
4653126353Smlaier	if (strlcpy(label, r->label, sizeof(label)) >= sizeof(label))
4654126353Smlaier		errx(1, "expand_rule: strlcpy");
4655130617Smlaier	if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname))
4656130617Smlaier		errx(1, "expand_rule: strlcpy");
4657130617Smlaier	if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >=
4658130617Smlaier	    sizeof(match_tagname))
4659130617Smlaier		errx(1, "expand_rule: strlcpy");
4660126353Smlaier	flags = r->flags;
4661126353Smlaier	flagset = r->flagset;
4662130617Smlaier	keep_state = r->keep_state;
4663126353Smlaier
4664126353Smlaier	LOOP_THROUGH(struct node_if, interface, interfaces,
4665126353Smlaier	LOOP_THROUGH(struct node_proto, proto, protos,
4666126353Smlaier	LOOP_THROUGH(struct node_icmp, icmp_type, icmp_types,
4667126353Smlaier	LOOP_THROUGH(struct node_host, src_host, src_hosts,
4668126353Smlaier	LOOP_THROUGH(struct node_port, src_port, src_ports,
4669126353Smlaier	LOOP_THROUGH(struct node_os, src_os, src_oses,
4670126353Smlaier	LOOP_THROUGH(struct node_host, dst_host, dst_hosts,
4671126353Smlaier	LOOP_THROUGH(struct node_port, dst_port, dst_ports,
4672126353Smlaier	LOOP_THROUGH(struct node_uid, uid, uids,
4673126353Smlaier	LOOP_THROUGH(struct node_gid, gid, gids,
4674126353Smlaier
4675126353Smlaier		r->af = af;
4676126353Smlaier		/* for link-local IPv6 address, interface must match up */
4677126353Smlaier		if ((r->af && src_host->af && r->af != src_host->af) ||
4678126353Smlaier		    (r->af && dst_host->af && r->af != dst_host->af) ||
4679126353Smlaier		    (src_host->af && dst_host->af &&
4680126353Smlaier		    src_host->af != dst_host->af) ||
4681126353Smlaier		    (src_host->ifindex && dst_host->ifindex &&
4682126353Smlaier		    src_host->ifindex != dst_host->ifindex) ||
4683130617Smlaier		    (src_host->ifindex && *interface->ifname &&
4684126353Smlaier		    src_host->ifindex != if_nametoindex(interface->ifname)) ||
4685130617Smlaier		    (dst_host->ifindex && *interface->ifname &&
4686126353Smlaier		    dst_host->ifindex != if_nametoindex(interface->ifname)))
4687126353Smlaier			continue;
4688126353Smlaier		if (!r->af && src_host->af)
4689126353Smlaier			r->af = src_host->af;
4690126353Smlaier		else if (!r->af && dst_host->af)
4691126353Smlaier			r->af = dst_host->af;
4692126353Smlaier
4693130617Smlaier		if (*interface->ifname)
4694145840Smlaier			strlcpy(r->ifname, interface->ifname,
4695145840Smlaier			    sizeof(r->ifname));
4696130617Smlaier		else if (if_indextoname(src_host->ifindex, ifname))
4697145840Smlaier			strlcpy(r->ifname, ifname, sizeof(r->ifname));
4698126353Smlaier		else if (if_indextoname(dst_host->ifindex, ifname))
4699145840Smlaier			strlcpy(r->ifname, ifname, sizeof(r->ifname));
4700126353Smlaier		else
4701130617Smlaier			memset(r->ifname, '\0', sizeof(r->ifname));
4702126353Smlaier
4703126353Smlaier		if (strlcpy(r->label, label, sizeof(r->label)) >=
4704126353Smlaier		    sizeof(r->label))
4705126353Smlaier			errx(1, "expand_rule: strlcpy");
4706130617Smlaier		if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >=
4707130617Smlaier		    sizeof(r->tagname))
4708130617Smlaier			errx(1, "expand_rule: strlcpy");
4709130617Smlaier		if (strlcpy(r->match_tagname, match_tagname,
4710130617Smlaier		    sizeof(r->match_tagname)) >= sizeof(r->match_tagname))
4711130617Smlaier			errx(1, "expand_rule: strlcpy");
4712130617Smlaier		expand_label(r->label, PF_RULE_LABEL_SIZE, r->ifname, r->af,
4713130617Smlaier		    src_host, src_port, dst_host, dst_port, proto->proto);
4714130617Smlaier		expand_label(r->tagname, PF_TAG_NAME_SIZE, r->ifname, r->af,
4715130617Smlaier		    src_host, src_port, dst_host, dst_port, proto->proto);
4716130617Smlaier		expand_label(r->match_tagname, PF_TAG_NAME_SIZE, r->ifname,
4717130617Smlaier		    r->af, src_host, src_port, dst_host, dst_port,
4718130617Smlaier		    proto->proto);
4719126353Smlaier
4720126353Smlaier		error += check_netmask(src_host, r->af);
4721126353Smlaier		error += check_netmask(dst_host, r->af);
4722126353Smlaier
4723126353Smlaier		r->ifnot = interface->not;
4724126353Smlaier		r->proto = proto->proto;
4725126353Smlaier		r->src.addr = src_host->addr;
4726145840Smlaier		r->src.neg = src_host->not;
4727126353Smlaier		r->src.port[0] = src_port->port[0];
4728126353Smlaier		r->src.port[1] = src_port->port[1];
4729126353Smlaier		r->src.port_op = src_port->op;
4730126353Smlaier		r->dst.addr = dst_host->addr;
4731145840Smlaier		r->dst.neg = dst_host->not;
4732126353Smlaier		r->dst.port[0] = dst_port->port[0];
4733126353Smlaier		r->dst.port[1] = dst_port->port[1];
4734126353Smlaier		r->dst.port_op = dst_port->op;
4735126353Smlaier		r->uid.op = uid->op;
4736126353Smlaier		r->uid.uid[0] = uid->uid[0];
4737126353Smlaier		r->uid.uid[1] = uid->uid[1];
4738126353Smlaier		r->gid.op = gid->op;
4739126353Smlaier		r->gid.gid[0] = gid->gid[0];
4740126353Smlaier		r->gid.gid[1] = gid->gid[1];
4741126353Smlaier		r->type = icmp_type->type;
4742126353Smlaier		r->code = icmp_type->code;
4743126353Smlaier
4744130617Smlaier		if ((keep_state == PF_STATE_MODULATE ||
4745130617Smlaier		    keep_state == PF_STATE_SYNPROXY) &&
4746130617Smlaier		    r->proto && r->proto != IPPROTO_TCP)
4747130617Smlaier			r->keep_state = PF_STATE_NORMAL;
4748130617Smlaier		else
4749130617Smlaier			r->keep_state = keep_state;
4750130617Smlaier
4751126353Smlaier		if (r->proto && r->proto != IPPROTO_TCP) {
4752126353Smlaier			r->flags = 0;
4753126353Smlaier			r->flagset = 0;
4754126353Smlaier		} else {
4755126353Smlaier			r->flags = flags;
4756126353Smlaier			r->flagset = flagset;
4757126353Smlaier		}
4758126353Smlaier		if (icmp_type->proto && r->proto != icmp_type->proto) {
4759126353Smlaier			yyerror("icmp-type mismatch");
4760126353Smlaier			error++;
4761126353Smlaier		}
4762126353Smlaier
4763126353Smlaier		if (src_os && src_os->os) {
4764126353Smlaier			r->os_fingerprint = pfctl_get_fingerprint(src_os->os);
4765126353Smlaier			if ((pf->opts & PF_OPT_VERBOSE2) &&
4766126353Smlaier			    r->os_fingerprint == PF_OSFP_NOMATCH)
4767126353Smlaier				fprintf(stderr,
4768126353Smlaier				    "warning: unknown '%s' OS fingerprint\n",
4769126353Smlaier				    src_os->os);
4770126353Smlaier		} else {
4771126353Smlaier			r->os_fingerprint = PF_OSFP_ANY;
4772126353Smlaier		}
4773126353Smlaier
4774126353Smlaier		TAILQ_INIT(&r->rpool.list);
4775126353Smlaier		for (h = rpool_hosts; h != NULL; h = h->next) {
4776126353Smlaier			pa = calloc(1, sizeof(struct pf_pooladdr));
4777126353Smlaier			if (pa == NULL)
4778126353Smlaier				err(1, "expand_rule: calloc");
4779126353Smlaier			pa->addr = h->addr;
4780126353Smlaier			if (h->ifname != NULL) {
4781126353Smlaier				if (strlcpy(pa->ifname, h->ifname,
4782126353Smlaier				    sizeof(pa->ifname)) >=
4783126353Smlaier				    sizeof(pa->ifname))
4784126353Smlaier					errx(1, "expand_rule: strlcpy");
4785126353Smlaier			} else
4786126353Smlaier				pa->ifname[0] = 0;
4787126353Smlaier			TAILQ_INSERT_TAIL(&r->rpool.list, pa, entries);
4788126353Smlaier		}
4789126353Smlaier
4790171172Smlaier		if (rule_consistent(r, anchor_call[0]) < 0 || error)
4791126353Smlaier			yyerror("skipping rule due to errors");
4792126353Smlaier		else {
4793171172Smlaier			r->nr = pf->astack[pf->asd]->match++;
4794145840Smlaier			pfctl_add_rule(pf, r, anchor_call);
4795126353Smlaier			added++;
4796126353Smlaier		}
4797126353Smlaier
4798126353Smlaier	))))))))));
4799126353Smlaier
4800126353Smlaier	FREE_LIST(struct node_if, interfaces);
4801126353Smlaier	FREE_LIST(struct node_proto, protos);
4802126353Smlaier	FREE_LIST(struct node_host, src_hosts);
4803126353Smlaier	FREE_LIST(struct node_port, src_ports);
4804126353Smlaier	FREE_LIST(struct node_os, src_oses);
4805126353Smlaier	FREE_LIST(struct node_host, dst_hosts);
4806126353Smlaier	FREE_LIST(struct node_port, dst_ports);
4807126353Smlaier	FREE_LIST(struct node_uid, uids);
4808126353Smlaier	FREE_LIST(struct node_gid, gids);
4809126353Smlaier	FREE_LIST(struct node_icmp, icmp_types);
4810126353Smlaier	FREE_LIST(struct node_host, rpool_hosts);
4811126353Smlaier
4812126353Smlaier	if (!added)
4813126353Smlaier		yyerror("rule expands to no valid combination");
4814126353Smlaier}
4815126353Smlaier
4816145840Smlaierint
4817145840Smlaierexpand_skip_interface(struct node_if *interfaces)
4818145840Smlaier{
4819145840Smlaier	int	errs = 0;
4820145840Smlaier
4821145840Smlaier	if (!interfaces || (!interfaces->next && !interfaces->not &&
4822145840Smlaier	    !strcmp(interfaces->ifname, "none"))) {
4823145840Smlaier		if (pf->opts & PF_OPT_VERBOSE)
4824145840Smlaier			printf("set skip on none\n");
4825145840Smlaier		errs = pfctl_set_interface_flags(pf, "", PFI_IFLAG_SKIP, 0);
4826145840Smlaier		return (errs);
4827145840Smlaier	}
4828145840Smlaier
4829145840Smlaier	if (pf->opts & PF_OPT_VERBOSE)
4830145840Smlaier		printf("set skip on {");
4831145840Smlaier	LOOP_THROUGH(struct node_if, interface, interfaces,
4832145840Smlaier		if (pf->opts & PF_OPT_VERBOSE)
4833145840Smlaier			printf(" %s", interface->ifname);
4834145840Smlaier		if (interface->not) {
4835145840Smlaier			yyerror("skip on ! <interface> is not supported");
4836145840Smlaier			errs++;
4837145840Smlaier		} else
4838145840Smlaier			errs += pfctl_set_interface_flags(pf,
4839145840Smlaier			    interface->ifname, PFI_IFLAG_SKIP, 1);
4840145840Smlaier	);
4841145840Smlaier	if (pf->opts & PF_OPT_VERBOSE)
4842145840Smlaier		printf(" }\n");
4843145840Smlaier
4844145840Smlaier	FREE_LIST(struct node_if, interfaces);
4845145840Smlaier
4846145840Smlaier	if (errs)
4847145840Smlaier		return (1);
4848145840Smlaier	else
4849145840Smlaier		return (0);
4850145840Smlaier}
4851145840Smlaier
4852126353Smlaier#undef FREE_LIST
4853126353Smlaier#undef LOOP_THROUGH
4854126353Smlaier
4855126353Smlaierint
4856126353Smlaiercheck_rulestate(int desired_state)
4857126353Smlaier{
4858126353Smlaier	if (require_order && (rulestate > desired_state)) {
4859126353Smlaier		yyerror("Rules must be in order: options, normalization, "
4860126353Smlaier		    "queueing, translation, filtering");
4861126353Smlaier		return (1);
4862126353Smlaier	}
4863126353Smlaier	rulestate = desired_state;
4864126353Smlaier	return (0);
4865126353Smlaier}
4866126353Smlaier
4867126353Smlaierint
4868126353Smlaierkw_cmp(const void *k, const void *e)
4869126353Smlaier{
4870126353Smlaier	return (strcmp(k, ((const struct keywords *)e)->k_name));
4871126353Smlaier}
4872126353Smlaier
4873126353Smlaierint
4874126353Smlaierlookup(char *s)
4875126353Smlaier{
4876126353Smlaier	/* this has to be sorted always */
4877126353Smlaier	static const struct keywords keywords[] = {
4878126353Smlaier		{ "all",		ALL},
4879126353Smlaier		{ "allow-opts",		ALLOWOPTS},
4880126353Smlaier		{ "altq",		ALTQ},
4881126353Smlaier		{ "anchor",		ANCHOR},
4882126353Smlaier		{ "antispoof",		ANTISPOOF},
4883126353Smlaier		{ "any",		ANY},
4884126353Smlaier		{ "bandwidth",		BANDWIDTH},
4885126353Smlaier		{ "binat",		BINAT},
4886126353Smlaier		{ "binat-anchor",	BINATANCHOR},
4887126353Smlaier		{ "bitmask",		BITMASK},
4888126353Smlaier		{ "block",		BLOCK},
4889126353Smlaier		{ "block-policy",	BLOCKPOLICY},
4890126353Smlaier		{ "cbq",		CBQ},
4891126353Smlaier		{ "code",		CODE},
4892126353Smlaier		{ "crop",		FRAGCROP},
4893130617Smlaier		{ "debug",		DEBUG},
4894126353Smlaier		{ "drop",		DROP},
4895126353Smlaier		{ "drop-ovl",		FRAGDROP},
4896126353Smlaier		{ "dup-to",		DUPTO},
4897126353Smlaier		{ "fastroute",		FASTROUTE},
4898126353Smlaier		{ "file",		FILENAME},
4899126353Smlaier		{ "fingerprints",	FINGERPRINTS},
4900126353Smlaier		{ "flags",		FLAGS},
4901130617Smlaier		{ "floating",		FLOATING},
4902145840Smlaier		{ "flush",		FLUSH},
4903126353Smlaier		{ "for",		FOR},
4904126353Smlaier		{ "fragment",		FRAGMENT},
4905126353Smlaier		{ "from",		FROM},
4906130617Smlaier		{ "global",		GLOBAL},
4907126353Smlaier		{ "group",		GROUP},
4908126353Smlaier		{ "hfsc",		HFSC},
4909130617Smlaier		{ "hostid",		HOSTID},
4910126353Smlaier		{ "icmp-type",		ICMPTYPE},
4911126353Smlaier		{ "icmp6-type",		ICMP6TYPE},
4912130617Smlaier		{ "if-bound",		IFBOUND},
4913126353Smlaier		{ "in",			IN},
4914126353Smlaier		{ "inet",		INET},
4915126353Smlaier		{ "inet6",		INET6},
4916126353Smlaier		{ "keep",		KEEP},
4917126353Smlaier		{ "label",		LABEL},
4918126353Smlaier		{ "limit",		LIMIT},
4919126353Smlaier		{ "linkshare",		LINKSHARE},
4920126353Smlaier		{ "load",		LOAD},
4921126353Smlaier		{ "log",		LOG},
4922126353Smlaier		{ "loginterface",	LOGINTERFACE},
4923126353Smlaier		{ "max",		MAXIMUM},
4924126353Smlaier		{ "max-mss",		MAXMSS},
4925145840Smlaier		{ "max-src-conn",	MAXSRCCONN},
4926145840Smlaier		{ "max-src-conn-rate",	MAXSRCCONNRATE},
4927130617Smlaier		{ "max-src-nodes",	MAXSRCNODES},
4928130617Smlaier		{ "max-src-states",	MAXSRCSTATES},
4929126353Smlaier		{ "min-ttl",		MINTTL},
4930126353Smlaier		{ "modulate",		MODULATE},
4931126353Smlaier		{ "nat",		NAT},
4932126353Smlaier		{ "nat-anchor",		NATANCHOR},
4933126353Smlaier		{ "no",			NO},
4934126353Smlaier		{ "no-df",		NODF},
4935126353Smlaier		{ "no-route",		NOROUTE},
4936130617Smlaier		{ "no-sync",		NOSYNC},
4937126353Smlaier		{ "on",			ON},
4938126353Smlaier		{ "optimization",	OPTIMIZATION},
4939126353Smlaier		{ "os",			OS},
4940126353Smlaier		{ "out",		OUT},
4941145840Smlaier		{ "overload",		OVERLOAD},
4942126353Smlaier		{ "pass",		PASS},
4943126353Smlaier		{ "port",		PORT},
4944126353Smlaier		{ "priority",		PRIORITY},
4945126353Smlaier		{ "priq",		PRIQ},
4946145840Smlaier		{ "probability",	PROBABILITY},
4947126353Smlaier		{ "proto",		PROTO},
4948126353Smlaier		{ "qlimit",		QLIMIT},
4949126353Smlaier		{ "queue",		QUEUE},
4950126353Smlaier		{ "quick",		QUICK},
4951126353Smlaier		{ "random",		RANDOM},
4952126353Smlaier		{ "random-id",		RANDOMID},
4953126353Smlaier		{ "rdr",		RDR},
4954126353Smlaier		{ "rdr-anchor",		RDRANCHOR},
4955126353Smlaier		{ "realtime",		REALTIME},
4956126353Smlaier		{ "reassemble",		REASSEMBLE},
4957126353Smlaier		{ "reply-to",		REPLYTO},
4958126353Smlaier		{ "require-order",	REQUIREORDER},
4959126353Smlaier		{ "return",		RETURN},
4960126353Smlaier		{ "return-icmp",	RETURNICMP},
4961126353Smlaier		{ "return-icmp6",	RETURNICMP6},
4962126353Smlaier		{ "return-rst",		RETURNRST},
4963126353Smlaier		{ "round-robin",	ROUNDROBIN},
4964145840Smlaier		{ "route",		ROUTE},
4965126353Smlaier		{ "route-to",		ROUTETO},
4966171172Smlaier		{ "rtable",		RTABLE},
4967130617Smlaier		{ "rule",		RULE},
4968171172Smlaier		{ "ruleset-optimization",	RULESET_OPTIMIZATION},
4969126353Smlaier		{ "scrub",		SCRUB},
4970126353Smlaier		{ "set",		SET},
4971145840Smlaier		{ "skip",		SKIP},
4972126353Smlaier		{ "source-hash",	SOURCEHASH},
4973130617Smlaier		{ "source-track",	SOURCETRACK},
4974126353Smlaier		{ "state",		STATE},
4975130617Smlaier		{ "state-policy",	STATEPOLICY},
4976126353Smlaier		{ "static-port",	STATICPORT},
4977130617Smlaier		{ "sticky-address",	STICKYADDRESS},
4978126353Smlaier		{ "synproxy",		SYNPROXY},
4979126353Smlaier		{ "table",		TABLE},
4980126353Smlaier		{ "tag",		TAG},
4981126353Smlaier		{ "tagged",		TAGGED},
4982126353Smlaier		{ "tbrsize",		TBRSIZE},
4983126353Smlaier		{ "timeout",		TIMEOUT},
4984126353Smlaier		{ "to",			TO},
4985126353Smlaier		{ "tos",		TOS},
4986126353Smlaier		{ "ttl",		TTL},
4987126353Smlaier		{ "upperlimit",		UPPERLIMIT},
4988171172Smlaier		{ "urpf-failed",	URPFFAILED},
4989126353Smlaier		{ "user",		USER},
4990126353Smlaier	};
4991126353Smlaier	const struct keywords	*p;
4992126353Smlaier
4993126353Smlaier	p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
4994126353Smlaier	    sizeof(keywords[0]), kw_cmp);
4995126353Smlaier
4996126353Smlaier	if (p) {
4997126353Smlaier		if (debug > 1)
4998126353Smlaier			fprintf(stderr, "%s: %d\n", s, p->k_val);
4999126353Smlaier		return (p->k_val);
5000126353Smlaier	} else {
5001126353Smlaier		if (debug > 1)
5002126353Smlaier			fprintf(stderr, "string: %s\n", s);
5003126353Smlaier		return (STRING);
5004126353Smlaier	}
5005126353Smlaier}
5006126353Smlaier
5007126353Smlaier#define MAXPUSHBACK	128
5008126353Smlaier
5009126353Smlaierchar	*parsebuf;
5010126353Smlaierint	 parseindex;
5011126353Smlaierchar	 pushback_buffer[MAXPUSHBACK];
5012126353Smlaierint	 pushback_index = 0;
5013126353Smlaier
5014126353Smlaierint
5015126353Smlaierlgetc(FILE *f)
5016126353Smlaier{
5017126353Smlaier	int	c, next;
5018126353Smlaier
5019126353Smlaier	if (parsebuf) {
5020126353Smlaier		/* Read character from the parsebuffer instead of input. */
5021126353Smlaier		if (parseindex >= 0) {
5022126353Smlaier			c = parsebuf[parseindex++];
5023126353Smlaier			if (c != '\0')
5024126353Smlaier				return (c);
5025126353Smlaier			parsebuf = NULL;
5026126353Smlaier		} else
5027126353Smlaier			parseindex++;
5028126353Smlaier	}
5029126353Smlaier
5030126353Smlaier	if (pushback_index)
5031126353Smlaier		return (pushback_buffer[--pushback_index]);
5032126353Smlaier
5033126353Smlaier	while ((c = getc(f)) == '\\') {
5034126353Smlaier		next = getc(f);
5035126353Smlaier		if (next != '\n') {
5036171172Smlaier			c = next;
5037126353Smlaier			break;
5038126353Smlaier		}
5039126353Smlaier		yylval.lineno = lineno;
5040126353Smlaier		lineno++;
5041126353Smlaier	}
5042126353Smlaier	if (c == '\t' || c == ' ') {
5043126353Smlaier		/* Compress blanks to a single space. */
5044126353Smlaier		do {
5045126353Smlaier			c = getc(f);
5046126353Smlaier		} while (c == '\t' || c == ' ');
5047126353Smlaier		ungetc(c, f);
5048126353Smlaier		c = ' ';
5049126353Smlaier	}
5050126353Smlaier
5051126353Smlaier	return (c);
5052126353Smlaier}
5053126353Smlaier
5054126353Smlaierint
5055126353Smlaierlungetc(int c)
5056126353Smlaier{
5057126353Smlaier	if (c == EOF)
5058126353Smlaier		return (EOF);
5059126353Smlaier	if (parsebuf) {
5060126353Smlaier		parseindex--;
5061126353Smlaier		if (parseindex >= 0)
5062126353Smlaier			return (c);
5063126353Smlaier	}
5064126353Smlaier	if (pushback_index < MAXPUSHBACK-1)
5065126353Smlaier		return (pushback_buffer[pushback_index++] = c);
5066126353Smlaier	else
5067126353Smlaier		return (EOF);
5068126353Smlaier}
5069126353Smlaier
5070126353Smlaierint
5071126353Smlaierfindeol(void)
5072126353Smlaier{
5073126353Smlaier	int	c;
5074126353Smlaier
5075126353Smlaier	parsebuf = NULL;
5076126353Smlaier	pushback_index = 0;
5077126353Smlaier
5078126353Smlaier	/* skip to either EOF or the first real EOL */
5079126353Smlaier	while (1) {
5080126353Smlaier		c = lgetc(fin);
5081126353Smlaier		if (c == '\n') {
5082126353Smlaier			lineno++;
5083126353Smlaier			break;
5084126353Smlaier		}
5085126353Smlaier		if (c == EOF)
5086126353Smlaier			break;
5087126353Smlaier	}
5088126353Smlaier	return (ERROR);
5089126353Smlaier}
5090126353Smlaier
5091126353Smlaierint
5092126353Smlaieryylex(void)
5093126353Smlaier{
5094126353Smlaier	char	 buf[8096];
5095126353Smlaier	char	*p, *val;
5096126353Smlaier	int	 endc, c, next;
5097126353Smlaier	int	 token;
5098126353Smlaier
5099126353Smlaiertop:
5100126353Smlaier	p = buf;
5101126353Smlaier	while ((c = lgetc(fin)) == ' ')
5102126353Smlaier		; /* nothing */
5103126353Smlaier
5104126353Smlaier	yylval.lineno = lineno;
5105126353Smlaier	if (c == '#')
5106126353Smlaier		while ((c = lgetc(fin)) != '\n' && c != EOF)
5107126353Smlaier			; /* nothing */
5108126353Smlaier	if (c == '$' && parsebuf == NULL) {
5109126353Smlaier		while (1) {
5110126353Smlaier			if ((c = lgetc(fin)) == EOF)
5111126353Smlaier				return (0);
5112126353Smlaier
5113126353Smlaier			if (p + 1 >= buf + sizeof(buf) - 1) {
5114126353Smlaier				yyerror("string too long");
5115126353Smlaier				return (findeol());
5116126353Smlaier			}
5117126353Smlaier			if (isalnum(c) || c == '_') {
5118126353Smlaier				*p++ = (char)c;
5119126353Smlaier				continue;
5120126353Smlaier			}
5121126353Smlaier			*p = '\0';
5122126353Smlaier			lungetc(c);
5123126353Smlaier			break;
5124126353Smlaier		}
5125126353Smlaier		val = symget(buf);
5126126353Smlaier		if (val == NULL) {
5127126353Smlaier			yyerror("macro '%s' not defined", buf);
5128126353Smlaier			return (findeol());
5129126353Smlaier		}
5130126353Smlaier		parsebuf = val;
5131126353Smlaier		parseindex = 0;
5132126353Smlaier		goto top;
5133126353Smlaier	}
5134126353Smlaier
5135126353Smlaier	switch (c) {
5136126353Smlaier	case '\'':
5137126353Smlaier	case '"':
5138126353Smlaier		endc = c;
5139126353Smlaier		while (1) {
5140126353Smlaier			if ((c = lgetc(fin)) == EOF)
5141126353Smlaier				return (0);
5142126353Smlaier			if (c == endc) {
5143126353Smlaier				*p = '\0';
5144126353Smlaier				break;
5145126353Smlaier			}
5146126353Smlaier			if (c == '\n') {
5147126353Smlaier				lineno++;
5148126353Smlaier				continue;
5149126353Smlaier			}
5150126353Smlaier			if (p + 1 >= buf + sizeof(buf) - 1) {
5151126353Smlaier				yyerror("string too long");
5152126353Smlaier				return (findeol());
5153126353Smlaier			}
5154126353Smlaier			*p++ = (char)c;
5155126353Smlaier		}
5156126353Smlaier		yylval.v.string = strdup(buf);
5157126353Smlaier		if (yylval.v.string == NULL)
5158126353Smlaier			err(1, "yylex: strdup");
5159126353Smlaier		return (STRING);
5160126353Smlaier	case '<':
5161126353Smlaier		next = lgetc(fin);
5162126353Smlaier		if (next == '>') {
5163126353Smlaier			yylval.v.i = PF_OP_XRG;
5164126353Smlaier			return (PORTBINARY);
5165126353Smlaier		}
5166126353Smlaier		lungetc(next);
5167126353Smlaier		break;
5168126353Smlaier	case '>':
5169126353Smlaier		next = lgetc(fin);
5170126353Smlaier		if (next == '<') {
5171126353Smlaier			yylval.v.i = PF_OP_IRG;
5172126353Smlaier			return (PORTBINARY);
5173126353Smlaier		}
5174126353Smlaier		lungetc(next);
5175126353Smlaier		break;
5176126353Smlaier	case '-':
5177126353Smlaier		next = lgetc(fin);
5178126353Smlaier		if (next == '>')
5179126353Smlaier			return (ARROW);
5180126353Smlaier		lungetc(next);
5181126353Smlaier		break;
5182126353Smlaier	}
5183126353Smlaier
5184126353Smlaier#define allowed_in_string(x) \
5185126353Smlaier	(isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
5186126353Smlaier	x != '{' && x != '}' && x != '<' && x != '>' && \
5187126353Smlaier	x != '!' && x != '=' && x != '/' && x != '#' && \
5188126353Smlaier	x != ','))
5189126353Smlaier
5190126353Smlaier	if (isalnum(c) || c == ':' || c == '_') {
5191126353Smlaier		do {
5192126353Smlaier			*p++ = c;
5193126353Smlaier			if ((unsigned)(p-buf) >= sizeof(buf)) {
5194126353Smlaier				yyerror("string too long");
5195126353Smlaier				return (findeol());
5196126353Smlaier			}
5197126353Smlaier		} while ((c = lgetc(fin)) != EOF && (allowed_in_string(c)));
5198126353Smlaier		lungetc(c);
5199126353Smlaier		*p = '\0';
5200130617Smlaier		if ((token = lookup(buf)) == STRING)
5201130617Smlaier			if ((yylval.v.string = strdup(buf)) == NULL)
5202130617Smlaier				err(1, "yylex: strdup");
5203126353Smlaier		return (token);
5204126353Smlaier	}
5205126353Smlaier	if (c == '\n') {
5206126353Smlaier		yylval.lineno = lineno;
5207126353Smlaier		lineno++;
5208126353Smlaier	}
5209126353Smlaier	if (c == EOF)
5210126353Smlaier		return (0);
5211126353Smlaier	return (c);
5212126353Smlaier}
5213126353Smlaier
5214126353Smlaierint
5215126353Smlaierparse_rules(FILE *input, struct pfctl *xpf)
5216126353Smlaier{
5217130617Smlaier	struct sym	*sym, *next;
5218126353Smlaier
5219126353Smlaier	fin = input;
5220126353Smlaier	pf = xpf;
5221126353Smlaier	lineno = 1;
5222126353Smlaier	errors = 0;
5223126353Smlaier	rulestate = PFCTL_STATE_NONE;
5224126353Smlaier	returnicmpdefault = (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
5225126353Smlaier	returnicmp6default =
5226126353Smlaier	    (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
5227126353Smlaier	blockpolicy = PFRULE_DROP;
5228126353Smlaier	require_order = 1;
5229126353Smlaier
5230126353Smlaier	yyparse();
5231126353Smlaier
5232126353Smlaier	/* Free macros and check which have not been used. */
5233130617Smlaier	for (sym = TAILQ_FIRST(&symhead); sym != NULL; sym = next) {
5234130617Smlaier		next = TAILQ_NEXT(sym, entries);
5235126353Smlaier		if ((pf->opts & PF_OPT_VERBOSE2) && !sym->used)
5236126353Smlaier			fprintf(stderr, "warning: macro '%s' not "
5237126353Smlaier			    "used\n", sym->nam);
5238126353Smlaier		free(sym->nam);
5239126353Smlaier		free(sym->val);
5240126353Smlaier		TAILQ_REMOVE(&symhead, sym, entries);
5241130617Smlaier		free(sym);
5242126353Smlaier	}
5243126353Smlaier
5244126353Smlaier	return (errors ? -1 : 0);
5245126353Smlaier}
5246126353Smlaier
5247126353Smlaier/*
5248126353Smlaier * Over-designed efficiency is a French and German concept, so how about
5249126353Smlaier * we wait until they discover this ugliness and make it all fancy.
5250126353Smlaier */
5251126353Smlaierint
5252126353Smlaiersymset(const char *nam, const char *val, int persist)
5253126353Smlaier{
5254126353Smlaier	struct sym	*sym;
5255126353Smlaier
5256126353Smlaier	for (sym = TAILQ_FIRST(&symhead); sym && strcmp(nam, sym->nam);
5257126353Smlaier	    sym = TAILQ_NEXT(sym, entries))
5258126353Smlaier		;	/* nothing */
5259126353Smlaier
5260126353Smlaier	if (sym != NULL) {
5261126353Smlaier		if (sym->persist == 1)
5262126353Smlaier			return (0);
5263126353Smlaier		else {
5264126353Smlaier			free(sym->nam);
5265126353Smlaier			free(sym->val);
5266126353Smlaier			TAILQ_REMOVE(&symhead, sym, entries);
5267126353Smlaier			free(sym);
5268126353Smlaier		}
5269126353Smlaier	}
5270126353Smlaier	if ((sym = calloc(1, sizeof(*sym))) == NULL)
5271126353Smlaier		return (-1);
5272126353Smlaier
5273126353Smlaier	sym->nam = strdup(nam);
5274126353Smlaier	if (sym->nam == NULL) {
5275126353Smlaier		free(sym);
5276126353Smlaier		return (-1);
5277126353Smlaier	}
5278126353Smlaier	sym->val = strdup(val);
5279126353Smlaier	if (sym->val == NULL) {
5280126353Smlaier		free(sym->nam);
5281126353Smlaier		free(sym);
5282126353Smlaier		return (-1);
5283126353Smlaier	}
5284126353Smlaier	sym->used = 0;
5285126353Smlaier	sym->persist = persist;
5286126353Smlaier	TAILQ_INSERT_TAIL(&symhead, sym, entries);
5287126353Smlaier	return (0);
5288126353Smlaier}
5289126353Smlaier
5290126353Smlaierint
5291126353Smlaierpfctl_cmdline_symset(char *s)
5292126353Smlaier{
5293126353Smlaier	char	*sym, *val;
5294126353Smlaier	int	 ret;
5295126353Smlaier
5296126353Smlaier	if ((val = strrchr(s, '=')) == NULL)
5297126353Smlaier		return (-1);
5298126353Smlaier
5299126353Smlaier	if ((sym = malloc(strlen(s) - strlen(val) + 1)) == NULL)
5300126353Smlaier		err(1, "pfctl_cmdline_symset: malloc");
5301126353Smlaier
5302126353Smlaier	strlcpy(sym, s, strlen(s) - strlen(val) + 1);
5303126353Smlaier
5304126353Smlaier	ret = symset(sym, val + 1, 1);
5305126353Smlaier	free(sym);
5306126353Smlaier
5307126353Smlaier	return (ret);
5308126353Smlaier}
5309126353Smlaier
5310126353Smlaierchar *
5311126353Smlaiersymget(const char *nam)
5312126353Smlaier{
5313126353Smlaier	struct sym	*sym;
5314126353Smlaier
5315126353Smlaier	TAILQ_FOREACH(sym, &symhead, entries)
5316126353Smlaier		if (strcmp(nam, sym->nam) == 0) {
5317126353Smlaier			sym->used = 1;
5318126353Smlaier			return (sym->val);
5319126353Smlaier		}
5320126353Smlaier	return (NULL);
5321126353Smlaier}
5322126353Smlaier
5323126353Smlaiervoid
5324171172Smlaiermv_rules(struct pf_ruleset *src, struct pf_ruleset *dst)
5325126353Smlaier{
5326171172Smlaier	int i;
5327171172Smlaier	struct pf_rule *r;
5328126353Smlaier
5329171172Smlaier	for (i = 0; i < PF_RULESET_MAX; ++i) {
5330171172Smlaier		while ((r = TAILQ_FIRST(src->rules[i].active.ptr))
5331171172Smlaier		    != NULL) {
5332171172Smlaier			TAILQ_REMOVE(src->rules[i].active.ptr, r, entries);
5333171172Smlaier			TAILQ_INSERT_TAIL(dst->rules[i].active.ptr, r, entries);
5334171172Smlaier			dst->anchor->match++;
5335126353Smlaier		}
5336171172Smlaier		src->anchor->match = 0;
5337171172Smlaier		while ((r = TAILQ_FIRST(src->rules[i].inactive.ptr))
5338171172Smlaier		    != NULL) {
5339171172Smlaier			TAILQ_REMOVE(src->rules[i].inactive.ptr, r, entries);
5340171172Smlaier			TAILQ_INSERT_TAIL(dst->rules[i].inactive.ptr,
5341171172Smlaier				r, entries);
5342171172Smlaier		}
5343126353Smlaier	}
5344126353Smlaier}
5345126353Smlaier
5346126353Smlaiervoid
5347171172Smlaierdecide_address_family(struct node_host *n, sa_family_t *af)
5348171172Smlaier{
5349171172Smlaier	if (*af != 0 || n == NULL)
5350171172Smlaier		return;
5351171172Smlaier	*af = n->af;
5352171172Smlaier	while ((n = n->next) != NULL) {
5353171172Smlaier		if (n->af != *af) {
5354171172Smlaier			*af = 0;
5355171172Smlaier			return;
5356171172Smlaier		}
5357171172Smlaier	}
5358171172Smlaier}
5359171172Smlaier
5360171172Smlaiervoid
5361126353Smlaierremove_invalid_hosts(struct node_host **nh, sa_family_t *af)
5362126353Smlaier{
5363126353Smlaier	struct node_host	*n = *nh, *prev = NULL;
5364126353Smlaier
5365126353Smlaier	while (n != NULL) {
5366126353Smlaier		if (*af && n->af && n->af != *af) {
5367126353Smlaier			/* unlink and free n */
5368126353Smlaier			struct node_host *next = n->next;
5369126353Smlaier
5370126353Smlaier			/* adjust tail pointer */
5371126353Smlaier			if (n == (*nh)->tail)
5372126353Smlaier				(*nh)->tail = prev;
5373126353Smlaier			/* adjust previous node's next pointer */
5374126353Smlaier			if (prev == NULL)
5375126353Smlaier				*nh = next;
5376126353Smlaier			else
5377126353Smlaier				prev->next = next;
5378126353Smlaier			/* free node */
5379126353Smlaier			if (n->ifname != NULL)
5380126353Smlaier				free(n->ifname);
5381126353Smlaier			free(n);
5382126353Smlaier			n = next;
5383126353Smlaier		} else {
5384126353Smlaier			if (n->af && !*af)
5385126353Smlaier				*af = n->af;
5386126353Smlaier			prev = n;
5387126353Smlaier			n = n->next;
5388126353Smlaier		}
5389126353Smlaier	}
5390126353Smlaier}
5391126353Smlaier
5392126353Smlaierint
5393126353Smlaierinvalid_redirect(struct node_host *nh, sa_family_t af)
5394126353Smlaier{
5395126353Smlaier	if (!af) {
5396126353Smlaier		struct node_host *n;
5397126353Smlaier
5398130617Smlaier		/* tables and dyniftl are ok without an address family */
5399126353Smlaier		for (n = nh; n != NULL; n = n->next) {
5400130617Smlaier			if (n->addr.type != PF_ADDR_TABLE &&
5401130617Smlaier			    n->addr.type != PF_ADDR_DYNIFTL) {
5402126353Smlaier				yyerror("address family not given and "
5403126353Smlaier				    "translation address expands to multiple "
5404126353Smlaier				    "address families");
5405126353Smlaier				return (1);
5406126353Smlaier			}
5407126353Smlaier		}
5408126353Smlaier	}
5409126353Smlaier	if (nh == NULL) {
5410126353Smlaier		yyerror("no translation address with matching address family "
5411126353Smlaier		    "found.");
5412126353Smlaier		return (1);
5413126353Smlaier	}
5414126353Smlaier	return (0);
5415126353Smlaier}
5416126353Smlaier
5417126353Smlaierint
5418126353Smlaieratoul(char *s, u_long *ulvalp)
5419126353Smlaier{
5420126353Smlaier	u_long	 ulval;
5421126353Smlaier	char	*ep;
5422126353Smlaier
5423126353Smlaier	errno = 0;
5424126353Smlaier	ulval = strtoul(s, &ep, 0);
5425126353Smlaier	if (s[0] == '\0' || *ep != '\0')
5426126353Smlaier		return (-1);
5427126353Smlaier	if (errno == ERANGE && ulval == ULONG_MAX)
5428126353Smlaier		return (-1);
5429126353Smlaier	*ulvalp = ulval;
5430126353Smlaier	return (0);
5431126353Smlaier}
5432126353Smlaier
5433126353Smlaierint
5434126353Smlaiergetservice(char *n)
5435126353Smlaier{
5436126353Smlaier	struct servent	*s;
5437126353Smlaier	u_long		 ulval;
5438126353Smlaier
5439126353Smlaier	if (atoul(n, &ulval) == 0) {
5440126353Smlaier		if (ulval > 65535) {
5441145840Smlaier			yyerror("illegal port value %lu", ulval);
5442126353Smlaier			return (-1);
5443126353Smlaier		}
5444126353Smlaier		return (htons(ulval));
5445126353Smlaier	} else {
5446126353Smlaier		s = getservbyname(n, "tcp");
5447126353Smlaier		if (s == NULL)
5448126353Smlaier			s = getservbyname(n, "udp");
5449126353Smlaier		if (s == NULL) {
5450126353Smlaier			yyerror("unknown port %s", n);
5451126353Smlaier			return (-1);
5452126353Smlaier		}
5453126353Smlaier		return (s->s_port);
5454126353Smlaier	}
5455126353Smlaier}
5456126353Smlaier
5457126353Smlaierint
5458126353Smlaierrule_label(struct pf_rule *r, char *s)
5459126353Smlaier{
5460126353Smlaier	if (s) {
5461126353Smlaier		if (strlcpy(r->label, s, sizeof(r->label)) >=
5462126353Smlaier		    sizeof(r->label)) {
5463126353Smlaier			yyerror("rule label too long (max %d chars)",
5464126353Smlaier			    sizeof(r->label)-1);
5465126353Smlaier			return (-1);
5466126353Smlaier		}
5467126353Smlaier	}
5468126353Smlaier	return (0);
5469126353Smlaier}
5470126353Smlaier
5471126353Smlaieru_int16_t
5472126353Smlaierparseicmpspec(char *w, sa_family_t af)
5473126353Smlaier{
5474126353Smlaier	const struct icmpcodeent	*p;
5475126353Smlaier	u_long				 ulval;
5476126353Smlaier	u_int8_t			 icmptype;
5477126353Smlaier
5478126353Smlaier	if (af == AF_INET)
5479126353Smlaier		icmptype = returnicmpdefault >> 8;
5480126353Smlaier	else
5481126353Smlaier		icmptype = returnicmp6default >> 8;
5482126353Smlaier
5483126353Smlaier	if (atoul(w, &ulval) == -1) {
5484126353Smlaier		if ((p = geticmpcodebyname(icmptype, w, af)) == NULL) {
5485126353Smlaier			yyerror("unknown icmp code %s", w);
5486126353Smlaier			return (0);
5487126353Smlaier		}
5488126353Smlaier		ulval = p->code;
5489126353Smlaier	}
5490126353Smlaier	if (ulval > 255) {
5491145840Smlaier		yyerror("invalid icmp code %lu", ulval);
5492126353Smlaier		return (0);
5493126353Smlaier	}
5494126353Smlaier	return (icmptype << 8 | ulval);
5495126353Smlaier}
5496126353Smlaier
5497126353Smlaierint
5498171172Smlaierpfctl_load_anchors(int dev, struct pfctl *pf, struct pfr_buffer *trans)
5499126353Smlaier{
5500126353Smlaier	struct loadanchors	*la;
5501171172Smlaier	FILE			*fin;
5502126353Smlaier
5503126353Smlaier	TAILQ_FOREACH(la, &loadanchorshead, entries) {
5504171172Smlaier		if (pf->opts & PF_OPT_VERBOSE)
5505145840Smlaier			fprintf(stderr, "\nLoading anchor %s from %s\n",
5506145840Smlaier			    la->anchorname, la->filename);
5507171172Smlaier		if ((fin = pfctl_fopen(la->filename, "r")) == NULL) {
5508171172Smlaier			warn("%s", la->filename);
5509171172Smlaier			continue;
5510171172Smlaier		}
5511171172Smlaier		if (pfctl_rules(dev, la->filename, fin, pf->opts, pf->optimize,
5512171172Smlaier		    la->anchorname, trans) == -1)
5513126353Smlaier			return (-1);
5514126353Smlaier	}
5515126353Smlaier
5516126353Smlaier	return (0);
5517126353Smlaier}
5518