xref: /freebsd/sbin/pfctl/parse.y (revision 58c8430a32463db3d29e4c701f2e72653269274c)
13b3a8eb9SGleb Smirnoff /*	$OpenBSD: parse.y,v 1.554 2008/10/17 12:59:53 henning Exp $	*/
23b3a8eb9SGleb Smirnoff 
33b3a8eb9SGleb Smirnoff /*
43b3a8eb9SGleb Smirnoff  * Copyright (c) 2001 Markus Friedl.  All rights reserved.
53b3a8eb9SGleb Smirnoff  * Copyright (c) 2001 Daniel Hartmeier.  All rights reserved.
63b3a8eb9SGleb Smirnoff  * Copyright (c) 2001 Theo de Raadt.  All rights reserved.
73b3a8eb9SGleb Smirnoff  * Copyright (c) 2002,2003 Henning Brauer. All rights reserved.
83b3a8eb9SGleb Smirnoff  *
93b3a8eb9SGleb Smirnoff  * Redistribution and use in source and binary forms, with or without
103b3a8eb9SGleb Smirnoff  * modification, are permitted provided that the following conditions
113b3a8eb9SGleb Smirnoff  * are met:
123b3a8eb9SGleb Smirnoff  * 1. Redistributions of source code must retain the above copyright
133b3a8eb9SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer.
143b3a8eb9SGleb Smirnoff  * 2. Redistributions in binary form must reproduce the above copyright
153b3a8eb9SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer in the
163b3a8eb9SGleb Smirnoff  *    documentation and/or other materials provided with the distribution.
173b3a8eb9SGleb Smirnoff  *
183b3a8eb9SGleb Smirnoff  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
193b3a8eb9SGleb Smirnoff  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
203b3a8eb9SGleb Smirnoff  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
213b3a8eb9SGleb Smirnoff  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
223b3a8eb9SGleb Smirnoff  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
233b3a8eb9SGleb Smirnoff  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
243b3a8eb9SGleb Smirnoff  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
253b3a8eb9SGleb Smirnoff  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
263b3a8eb9SGleb Smirnoff  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
273b3a8eb9SGleb Smirnoff  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
283b3a8eb9SGleb Smirnoff  */
293b3a8eb9SGleb Smirnoff %{
303b3a8eb9SGleb Smirnoff #include <sys/cdefs.h>
313b3a8eb9SGleb Smirnoff __FBSDID("$FreeBSD$");
323b3a8eb9SGleb Smirnoff 
333b3a8eb9SGleb Smirnoff #include <sys/types.h>
343b3a8eb9SGleb Smirnoff #include <sys/socket.h>
353b3a8eb9SGleb Smirnoff #include <sys/stat.h>
363b3a8eb9SGleb Smirnoff #ifdef __FreeBSD__
373b3a8eb9SGleb Smirnoff #include <sys/sysctl.h>
383b3a8eb9SGleb Smirnoff #endif
393b3a8eb9SGleb Smirnoff #include <net/if.h>
403b3a8eb9SGleb Smirnoff #include <netinet/in.h>
413b3a8eb9SGleb Smirnoff #include <netinet/in_systm.h>
423b3a8eb9SGleb Smirnoff #include <netinet/ip.h>
433b3a8eb9SGleb Smirnoff #include <netinet/ip_icmp.h>
443b3a8eb9SGleb Smirnoff #include <netinet/icmp6.h>
453b3a8eb9SGleb Smirnoff #include <net/pfvar.h>
463b3a8eb9SGleb Smirnoff #include <arpa/inet.h>
47772e66a6SGleb Smirnoff #include <net/altq/altq.h>
48772e66a6SGleb Smirnoff #include <net/altq/altq_cbq.h>
490a70aaf8SLuiz Otavio O Souza #include <net/altq/altq_codel.h>
50772e66a6SGleb Smirnoff #include <net/altq/altq_priq.h>
51772e66a6SGleb Smirnoff #include <net/altq/altq_hfsc.h>
52a5b789f6SErmal Luçi #include <net/altq/altq_fairq.h>
533b3a8eb9SGleb Smirnoff 
543b3a8eb9SGleb Smirnoff #include <stdio.h>
553b3a8eb9SGleb Smirnoff #include <unistd.h>
563b3a8eb9SGleb Smirnoff #include <stdlib.h>
573b3a8eb9SGleb Smirnoff #include <netdb.h>
583b3a8eb9SGleb Smirnoff #include <stdarg.h>
593b3a8eb9SGleb Smirnoff #include <errno.h>
603b3a8eb9SGleb Smirnoff #include <string.h>
613b3a8eb9SGleb Smirnoff #include <ctype.h>
623b3a8eb9SGleb Smirnoff #include <math.h>
633b3a8eb9SGleb Smirnoff #include <err.h>
643b3a8eb9SGleb Smirnoff #include <limits.h>
653b3a8eb9SGleb Smirnoff #include <pwd.h>
663b3a8eb9SGleb Smirnoff #include <grp.h>
673b3a8eb9SGleb Smirnoff #include <md5.h>
683b3a8eb9SGleb Smirnoff 
693b3a8eb9SGleb Smirnoff #include "pfctl_parser.h"
703b3a8eb9SGleb Smirnoff #include "pfctl.h"
713b3a8eb9SGleb Smirnoff 
723b3a8eb9SGleb Smirnoff static struct pfctl	*pf = NULL;
733b3a8eb9SGleb Smirnoff static int		 debug = 0;
743b3a8eb9SGleb Smirnoff static int		 rulestate = 0;
753b3a8eb9SGleb Smirnoff static u_int16_t	 returnicmpdefault =
763b3a8eb9SGleb Smirnoff 			    (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
773b3a8eb9SGleb Smirnoff static u_int16_t	 returnicmp6default =
783b3a8eb9SGleb Smirnoff 			    (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
793b3a8eb9SGleb Smirnoff static int		 blockpolicy = PFRULE_DROP;
803b3a8eb9SGleb Smirnoff static int		 require_order = 1;
813b3a8eb9SGleb Smirnoff static int		 default_statelock;
823b3a8eb9SGleb Smirnoff 
8313cfafabSKristof Provost static TAILQ_HEAD(files, file)	 files = TAILQ_HEAD_INITIALIZER(files);
843b3a8eb9SGleb Smirnoff static struct file {
853b3a8eb9SGleb Smirnoff 	TAILQ_ENTRY(file)	 entry;
863b3a8eb9SGleb Smirnoff 	FILE			*stream;
873b3a8eb9SGleb Smirnoff 	char			*name;
883b3a8eb9SGleb Smirnoff 	int			 lineno;
893b3a8eb9SGleb Smirnoff 	int			 errors;
903b3a8eb9SGleb Smirnoff } *file;
913b3a8eb9SGleb Smirnoff struct file	*pushfile(const char *, int);
923b3a8eb9SGleb Smirnoff int		 popfile(void);
933b3a8eb9SGleb Smirnoff int		 check_file_secrecy(int, const char *);
943b3a8eb9SGleb Smirnoff int		 yyparse(void);
953b3a8eb9SGleb Smirnoff int		 yylex(void);
963b3a8eb9SGleb Smirnoff int		 yyerror(const char *, ...);
973b3a8eb9SGleb Smirnoff int		 kw_cmp(const void *, const void *);
983b3a8eb9SGleb Smirnoff int		 lookup(char *);
993b3a8eb9SGleb Smirnoff int		 lgetc(int);
1003b3a8eb9SGleb Smirnoff int		 lungetc(int);
1013b3a8eb9SGleb Smirnoff int		 findeol(void);
1023b3a8eb9SGleb Smirnoff 
10313cfafabSKristof Provost static TAILQ_HEAD(symhead, sym)	 symhead = TAILQ_HEAD_INITIALIZER(symhead);
1043b3a8eb9SGleb Smirnoff struct sym {
1053b3a8eb9SGleb Smirnoff 	TAILQ_ENTRY(sym)	 entry;
1063b3a8eb9SGleb Smirnoff 	int			 used;
1073b3a8eb9SGleb Smirnoff 	int			 persist;
1083b3a8eb9SGleb Smirnoff 	char			*nam;
1093b3a8eb9SGleb Smirnoff 	char			*val;
1103b3a8eb9SGleb Smirnoff };
1113b3a8eb9SGleb Smirnoff int		 symset(const char *, const char *, int);
1123b3a8eb9SGleb Smirnoff char		*symget(const char *);
1133b3a8eb9SGleb Smirnoff 
1143b3a8eb9SGleb Smirnoff int		 atoul(char *, u_long *);
1153b3a8eb9SGleb Smirnoff 
1163b3a8eb9SGleb Smirnoff enum {
1173b3a8eb9SGleb Smirnoff 	PFCTL_STATE_NONE,
1183b3a8eb9SGleb Smirnoff 	PFCTL_STATE_OPTION,
1193b3a8eb9SGleb Smirnoff 	PFCTL_STATE_SCRUB,
1203b3a8eb9SGleb Smirnoff 	PFCTL_STATE_QUEUE,
1213b3a8eb9SGleb Smirnoff 	PFCTL_STATE_NAT,
1223b3a8eb9SGleb Smirnoff 	PFCTL_STATE_FILTER
1233b3a8eb9SGleb Smirnoff };
1243b3a8eb9SGleb Smirnoff 
1253b3a8eb9SGleb Smirnoff struct node_proto {
1263b3a8eb9SGleb Smirnoff 	u_int8_t		 proto;
1273b3a8eb9SGleb Smirnoff 	struct node_proto	*next;
1283b3a8eb9SGleb Smirnoff 	struct node_proto	*tail;
1293b3a8eb9SGleb Smirnoff };
1303b3a8eb9SGleb Smirnoff 
1313b3a8eb9SGleb Smirnoff struct node_port {
1323b3a8eb9SGleb Smirnoff 	u_int16_t		 port[2];
1333b3a8eb9SGleb Smirnoff 	u_int8_t		 op;
1343b3a8eb9SGleb Smirnoff 	struct node_port	*next;
1353b3a8eb9SGleb Smirnoff 	struct node_port	*tail;
1363b3a8eb9SGleb Smirnoff };
1373b3a8eb9SGleb Smirnoff 
1383b3a8eb9SGleb Smirnoff struct node_uid {
1393b3a8eb9SGleb Smirnoff 	uid_t			 uid[2];
1403b3a8eb9SGleb Smirnoff 	u_int8_t		 op;
1413b3a8eb9SGleb Smirnoff 	struct node_uid		*next;
1423b3a8eb9SGleb Smirnoff 	struct node_uid		*tail;
1433b3a8eb9SGleb Smirnoff };
1443b3a8eb9SGleb Smirnoff 
1453b3a8eb9SGleb Smirnoff struct node_gid {
1463b3a8eb9SGleb Smirnoff 	gid_t			 gid[2];
1473b3a8eb9SGleb Smirnoff 	u_int8_t		 op;
1483b3a8eb9SGleb Smirnoff 	struct node_gid		*next;
1493b3a8eb9SGleb Smirnoff 	struct node_gid		*tail;
1503b3a8eb9SGleb Smirnoff };
1513b3a8eb9SGleb Smirnoff 
1523b3a8eb9SGleb Smirnoff struct node_icmp {
1533b3a8eb9SGleb Smirnoff 	u_int8_t		 code;
1543b3a8eb9SGleb Smirnoff 	u_int8_t		 type;
1553b3a8eb9SGleb Smirnoff 	u_int8_t		 proto;
1563b3a8eb9SGleb Smirnoff 	struct node_icmp	*next;
1573b3a8eb9SGleb Smirnoff 	struct node_icmp	*tail;
1583b3a8eb9SGleb Smirnoff };
1593b3a8eb9SGleb Smirnoff 
1603b3a8eb9SGleb Smirnoff enum	{ PF_STATE_OPT_MAX, PF_STATE_OPT_NOSYNC, PF_STATE_OPT_SRCTRACK,
1613b3a8eb9SGleb Smirnoff 	    PF_STATE_OPT_MAX_SRC_STATES, PF_STATE_OPT_MAX_SRC_CONN,
1623b3a8eb9SGleb Smirnoff 	    PF_STATE_OPT_MAX_SRC_CONN_RATE, PF_STATE_OPT_MAX_SRC_NODES,
1633b3a8eb9SGleb Smirnoff 	    PF_STATE_OPT_OVERLOAD, PF_STATE_OPT_STATELOCK,
1643b3a8eb9SGleb Smirnoff 	    PF_STATE_OPT_TIMEOUT, PF_STATE_OPT_SLOPPY, };
1653b3a8eb9SGleb Smirnoff 
1663b3a8eb9SGleb Smirnoff enum	{ PF_SRCTRACK_NONE, PF_SRCTRACK, PF_SRCTRACK_GLOBAL, PF_SRCTRACK_RULE };
1673b3a8eb9SGleb Smirnoff 
1683b3a8eb9SGleb Smirnoff struct node_state_opt {
1693b3a8eb9SGleb Smirnoff 	int			 type;
1703b3a8eb9SGleb Smirnoff 	union {
1713b3a8eb9SGleb Smirnoff 		u_int32_t	 max_states;
1723b3a8eb9SGleb Smirnoff 		u_int32_t	 max_src_states;
1733b3a8eb9SGleb Smirnoff 		u_int32_t	 max_src_conn;
1743b3a8eb9SGleb Smirnoff 		struct {
1753b3a8eb9SGleb Smirnoff 			u_int32_t	limit;
1763b3a8eb9SGleb Smirnoff 			u_int32_t	seconds;
1773b3a8eb9SGleb Smirnoff 		}		 max_src_conn_rate;
1783b3a8eb9SGleb Smirnoff 		struct {
1793b3a8eb9SGleb Smirnoff 			u_int8_t	flush;
1803b3a8eb9SGleb Smirnoff 			char		tblname[PF_TABLE_NAME_SIZE];
1813b3a8eb9SGleb Smirnoff 		}		 overload;
1823b3a8eb9SGleb Smirnoff 		u_int32_t	 max_src_nodes;
1833b3a8eb9SGleb Smirnoff 		u_int8_t	 src_track;
1843b3a8eb9SGleb Smirnoff 		u_int32_t	 statelock;
1853b3a8eb9SGleb Smirnoff 		struct {
1863b3a8eb9SGleb Smirnoff 			int		number;
1873b3a8eb9SGleb Smirnoff 			u_int32_t	seconds;
1883b3a8eb9SGleb Smirnoff 		}		 timeout;
1893b3a8eb9SGleb Smirnoff 	}			 data;
1903b3a8eb9SGleb Smirnoff 	struct node_state_opt	*next;
1913b3a8eb9SGleb Smirnoff 	struct node_state_opt	*tail;
1923b3a8eb9SGleb Smirnoff };
1933b3a8eb9SGleb Smirnoff 
1943b3a8eb9SGleb Smirnoff struct peer {
1953b3a8eb9SGleb Smirnoff 	struct node_host	*host;
1963b3a8eb9SGleb Smirnoff 	struct node_port	*port;
1973b3a8eb9SGleb Smirnoff };
1983b3a8eb9SGleb Smirnoff 
19913cfafabSKristof Provost static struct node_queue {
2003b3a8eb9SGleb Smirnoff 	char			 queue[PF_QNAME_SIZE];
2013b3a8eb9SGleb Smirnoff 	char			 parent[PF_QNAME_SIZE];
2023b3a8eb9SGleb Smirnoff 	char			 ifname[IFNAMSIZ];
2033b3a8eb9SGleb Smirnoff 	int			 scheduler;
2043b3a8eb9SGleb Smirnoff 	struct node_queue	*next;
2053b3a8eb9SGleb Smirnoff 	struct node_queue	*tail;
2063b3a8eb9SGleb Smirnoff }	*queues = NULL;
2073b3a8eb9SGleb Smirnoff 
2083b3a8eb9SGleb Smirnoff struct node_qassign {
2093b3a8eb9SGleb Smirnoff 	char		*qname;
2103b3a8eb9SGleb Smirnoff 	char		*pqname;
2113b3a8eb9SGleb Smirnoff };
2123b3a8eb9SGleb Smirnoff 
21313cfafabSKristof Provost static struct filter_opts {
2143b3a8eb9SGleb Smirnoff 	int			 marker;
2153b3a8eb9SGleb Smirnoff #define FOM_FLAGS	0x01
2163b3a8eb9SGleb Smirnoff #define FOM_ICMP	0x02
2173b3a8eb9SGleb Smirnoff #define FOM_TOS		0x04
2183b3a8eb9SGleb Smirnoff #define FOM_KEEP	0x08
2193b3a8eb9SGleb Smirnoff #define FOM_SRCTRACK	0x10
2203e248e0fSKristof Provost #define FOM_SETPRIO	0x0400
2213e248e0fSKristof Provost #define FOM_PRIO	0x2000
2223b3a8eb9SGleb Smirnoff 	struct node_uid		*uid;
2233b3a8eb9SGleb Smirnoff 	struct node_gid		*gid;
2243b3a8eb9SGleb Smirnoff 	struct {
2253b3a8eb9SGleb Smirnoff 		u_int8_t	 b1;
2263b3a8eb9SGleb Smirnoff 		u_int8_t	 b2;
2273b3a8eb9SGleb Smirnoff 		u_int16_t	 w;
2283b3a8eb9SGleb Smirnoff 		u_int16_t	 w2;
2293b3a8eb9SGleb Smirnoff 	} flags;
2303b3a8eb9SGleb Smirnoff 	struct node_icmp	*icmpspec;
2313b3a8eb9SGleb Smirnoff 	u_int32_t		 tos;
2323b3a8eb9SGleb Smirnoff 	u_int32_t		 prob;
2333b3a8eb9SGleb Smirnoff 	struct {
2343b3a8eb9SGleb Smirnoff 		int			 action;
2353b3a8eb9SGleb Smirnoff 		struct node_state_opt	*options;
2363b3a8eb9SGleb Smirnoff 	} keep;
2373b3a8eb9SGleb Smirnoff 	int			 fragment;
2383b3a8eb9SGleb Smirnoff 	int			 allowopts;
2393b3a8eb9SGleb Smirnoff 	char			*label;
2403b3a8eb9SGleb Smirnoff 	struct node_qassign	 queues;
2413b3a8eb9SGleb Smirnoff 	char			*tag;
2423b3a8eb9SGleb Smirnoff 	char			*match_tag;
2433b3a8eb9SGleb Smirnoff 	u_int8_t		 match_tag_not;
2443b3a8eb9SGleb Smirnoff 	u_int			 rtableid;
2453e248e0fSKristof Provost 	u_int8_t		 prio;
2463e248e0fSKristof Provost 	u_int8_t		 set_prio[2];
2473b3a8eb9SGleb Smirnoff 	struct {
2483b3a8eb9SGleb Smirnoff 		struct node_host	*addr;
2493b3a8eb9SGleb Smirnoff 		u_int16_t		port;
2503b3a8eb9SGleb Smirnoff 	}			 divert;
2513b3a8eb9SGleb Smirnoff } filter_opts;
2523b3a8eb9SGleb Smirnoff 
25313cfafabSKristof Provost static struct antispoof_opts {
2543b3a8eb9SGleb Smirnoff 	char			*label;
2553b3a8eb9SGleb Smirnoff 	u_int			 rtableid;
2563b3a8eb9SGleb Smirnoff } antispoof_opts;
2573b3a8eb9SGleb Smirnoff 
25813cfafabSKristof Provost static struct scrub_opts {
2593b3a8eb9SGleb Smirnoff 	int			 marker;
2603b3a8eb9SGleb Smirnoff #define SOM_MINTTL	0x01
2613b3a8eb9SGleb Smirnoff #define SOM_MAXMSS	0x02
2623b3a8eb9SGleb Smirnoff #define SOM_FRAGCACHE	0x04
2633b3a8eb9SGleb Smirnoff #define SOM_SETTOS	0x08
2643b3a8eb9SGleb Smirnoff 	int			 nodf;
2653b3a8eb9SGleb Smirnoff 	int			 minttl;
2663b3a8eb9SGleb Smirnoff 	int			 maxmss;
2673b3a8eb9SGleb Smirnoff 	int			 settos;
2683b3a8eb9SGleb Smirnoff 	int			 fragcache;
2693b3a8eb9SGleb Smirnoff 	int			 randomid;
2703b3a8eb9SGleb Smirnoff 	int			 reassemble_tcp;
2713b3a8eb9SGleb Smirnoff 	char			*match_tag;
2723b3a8eb9SGleb Smirnoff 	u_int8_t		 match_tag_not;
2733b3a8eb9SGleb Smirnoff 	u_int			 rtableid;
2743b3a8eb9SGleb Smirnoff } scrub_opts;
2753b3a8eb9SGleb Smirnoff 
27613cfafabSKristof Provost static struct queue_opts {
2773b3a8eb9SGleb Smirnoff 	int			marker;
2783b3a8eb9SGleb Smirnoff #define QOM_BWSPEC	0x01
2793b3a8eb9SGleb Smirnoff #define QOM_SCHEDULER	0x02
2803b3a8eb9SGleb Smirnoff #define QOM_PRIORITY	0x04
2813b3a8eb9SGleb Smirnoff #define QOM_TBRSIZE	0x08
2823b3a8eb9SGleb Smirnoff #define QOM_QLIMIT	0x10
2833b3a8eb9SGleb Smirnoff 	struct node_queue_bw	queue_bwspec;
2843b3a8eb9SGleb Smirnoff 	struct node_queue_opt	scheduler;
2853b3a8eb9SGleb Smirnoff 	int			priority;
2863b3a8eb9SGleb Smirnoff 	int			tbrsize;
2873b3a8eb9SGleb Smirnoff 	int			qlimit;
2883b3a8eb9SGleb Smirnoff } queue_opts;
2893b3a8eb9SGleb Smirnoff 
29013cfafabSKristof Provost static struct table_opts {
2913b3a8eb9SGleb Smirnoff 	int			flags;
2923b3a8eb9SGleb Smirnoff 	int			init_addr;
2933b3a8eb9SGleb Smirnoff 	struct node_tinithead	init_nodes;
2943b3a8eb9SGleb Smirnoff } table_opts;
2953b3a8eb9SGleb Smirnoff 
29613cfafabSKristof Provost static struct pool_opts {
2973b3a8eb9SGleb Smirnoff 	int			 marker;
2983b3a8eb9SGleb Smirnoff #define POM_TYPE		0x01
2993b3a8eb9SGleb Smirnoff #define POM_STICKYADDRESS	0x02
3003b3a8eb9SGleb Smirnoff 	u_int8_t		 opts;
3013b3a8eb9SGleb Smirnoff 	int			 type;
3023b3a8eb9SGleb Smirnoff 	int			 staticport;
3033b3a8eb9SGleb Smirnoff 	struct pf_poolhashkey	*key;
3043b3a8eb9SGleb Smirnoff 
3053b3a8eb9SGleb Smirnoff } pool_opts;
3063b3a8eb9SGleb Smirnoff 
30713cfafabSKristof Provost static struct codel_opts	 codel_opts;
30813cfafabSKristof Provost static struct node_hfsc_opts	 hfsc_opts;
30913cfafabSKristof Provost static struct node_fairq_opts	 fairq_opts;
31013cfafabSKristof Provost static struct node_state_opt	*keep_state_defaults = NULL;
3113b3a8eb9SGleb Smirnoff 
3123b3a8eb9SGleb Smirnoff int		 disallow_table(struct node_host *, const char *);
3133b3a8eb9SGleb Smirnoff int		 disallow_urpf_failed(struct node_host *, const char *);
3143b3a8eb9SGleb Smirnoff int		 disallow_alias(struct node_host *, const char *);
3153b3a8eb9SGleb Smirnoff int		 rule_consistent(struct pf_rule *, int);
3163b3a8eb9SGleb Smirnoff int		 filter_consistent(struct pf_rule *, int);
3173b3a8eb9SGleb Smirnoff int		 nat_consistent(struct pf_rule *);
3183b3a8eb9SGleb Smirnoff int		 rdr_consistent(struct pf_rule *);
3193b3a8eb9SGleb Smirnoff int		 process_tabledef(char *, struct table_opts *);
3203b3a8eb9SGleb Smirnoff void		 expand_label_str(char *, size_t, const char *, const char *);
3213b3a8eb9SGleb Smirnoff void		 expand_label_if(const char *, char *, size_t, const char *);
3223b3a8eb9SGleb Smirnoff void		 expand_label_addr(const char *, char *, size_t, u_int8_t,
3233b3a8eb9SGleb Smirnoff 		    struct node_host *);
3243b3a8eb9SGleb Smirnoff void		 expand_label_port(const char *, char *, size_t,
3253b3a8eb9SGleb Smirnoff 		    struct node_port *);
3263b3a8eb9SGleb Smirnoff void		 expand_label_proto(const char *, char *, size_t, u_int8_t);
3273b3a8eb9SGleb Smirnoff void		 expand_label_nr(const char *, char *, size_t);
3283b3a8eb9SGleb Smirnoff void		 expand_label(char *, size_t, const char *, u_int8_t,
3293b3a8eb9SGleb Smirnoff 		    struct node_host *, struct node_port *, struct node_host *,
3303b3a8eb9SGleb Smirnoff 		    struct node_port *, u_int8_t);
3313b3a8eb9SGleb Smirnoff void		 expand_rule(struct pf_rule *, struct node_if *,
3323b3a8eb9SGleb Smirnoff 		    struct node_host *, struct node_proto *, struct node_os *,
3333b3a8eb9SGleb Smirnoff 		    struct node_host *, struct node_port *, struct node_host *,
3343b3a8eb9SGleb Smirnoff 		    struct node_port *, struct node_uid *, struct node_gid *,
3353b3a8eb9SGleb Smirnoff 		    struct node_icmp *, const char *);
3363b3a8eb9SGleb Smirnoff int		 expand_altq(struct pf_altq *, struct node_if *,
3373b3a8eb9SGleb Smirnoff 		    struct node_queue *, struct node_queue_bw bwspec,
3383b3a8eb9SGleb Smirnoff 		    struct node_queue_opt *);
3393b3a8eb9SGleb Smirnoff int		 expand_queue(struct pf_altq *, struct node_if *,
3403b3a8eb9SGleb Smirnoff 		    struct node_queue *, struct node_queue_bw,
3413b3a8eb9SGleb Smirnoff 		    struct node_queue_opt *);
3423b3a8eb9SGleb Smirnoff int		 expand_skip_interface(struct node_if *);
3433b3a8eb9SGleb Smirnoff 
3443b3a8eb9SGleb Smirnoff int	 check_rulestate(int);
3453b3a8eb9SGleb Smirnoff int	 getservice(char *);
3463b3a8eb9SGleb Smirnoff int	 rule_label(struct pf_rule *, char *);
3473b3a8eb9SGleb Smirnoff int	 rt_tableid_max(void);
3483b3a8eb9SGleb Smirnoff 
3493b3a8eb9SGleb Smirnoff void	 mv_rules(struct pf_ruleset *, struct pf_ruleset *);
3503b3a8eb9SGleb Smirnoff void	 decide_address_family(struct node_host *, sa_family_t *);
3513b3a8eb9SGleb Smirnoff void	 remove_invalid_hosts(struct node_host **, sa_family_t *);
3523b3a8eb9SGleb Smirnoff int	 invalid_redirect(struct node_host *, sa_family_t);
3533b3a8eb9SGleb Smirnoff u_int16_t parseicmpspec(char *, sa_family_t);
3541f495578SKristof Provost int	 kw_casecmp(const void *, const void *);
3551f495578SKristof Provost int	 map_tos(char *string, int *);
3563b3a8eb9SGleb Smirnoff 
35713cfafabSKristof Provost static TAILQ_HEAD(loadanchorshead, loadanchors)
3583b3a8eb9SGleb Smirnoff     loadanchorshead = TAILQ_HEAD_INITIALIZER(loadanchorshead);
3593b3a8eb9SGleb Smirnoff 
3603b3a8eb9SGleb Smirnoff struct loadanchors {
3613b3a8eb9SGleb Smirnoff 	TAILQ_ENTRY(loadanchors)	 entries;
3623b3a8eb9SGleb Smirnoff 	char				*anchorname;
3633b3a8eb9SGleb Smirnoff 	char				*filename;
3643b3a8eb9SGleb Smirnoff };
3653b3a8eb9SGleb Smirnoff 
3663b3a8eb9SGleb Smirnoff typedef struct {
3673b3a8eb9SGleb Smirnoff 	union {
3683b3a8eb9SGleb Smirnoff 		int64_t			 number;
3693b3a8eb9SGleb Smirnoff 		double			 probability;
3703b3a8eb9SGleb Smirnoff 		int			 i;
3713b3a8eb9SGleb Smirnoff 		char			*string;
3723b3a8eb9SGleb Smirnoff 		u_int			 rtableid;
3733b3a8eb9SGleb Smirnoff 		struct {
3743b3a8eb9SGleb Smirnoff 			u_int8_t	 b1;
3753b3a8eb9SGleb Smirnoff 			u_int8_t	 b2;
3763b3a8eb9SGleb Smirnoff 			u_int16_t	 w;
3773b3a8eb9SGleb Smirnoff 			u_int16_t	 w2;
3783b3a8eb9SGleb Smirnoff 		}			 b;
3793b3a8eb9SGleb Smirnoff 		struct range {
3803b3a8eb9SGleb Smirnoff 			int		 a;
3813b3a8eb9SGleb Smirnoff 			int		 b;
3823b3a8eb9SGleb Smirnoff 			int		 t;
3833b3a8eb9SGleb Smirnoff 		}			 range;
3843b3a8eb9SGleb Smirnoff 		struct node_if		*interface;
3853b3a8eb9SGleb Smirnoff 		struct node_proto	*proto;
3863b3a8eb9SGleb Smirnoff 		struct node_icmp	*icmp;
3873b3a8eb9SGleb Smirnoff 		struct node_host	*host;
3883b3a8eb9SGleb Smirnoff 		struct node_os		*os;
3893b3a8eb9SGleb Smirnoff 		struct node_port	*port;
3903b3a8eb9SGleb Smirnoff 		struct node_uid		*uid;
3913b3a8eb9SGleb Smirnoff 		struct node_gid		*gid;
3923b3a8eb9SGleb Smirnoff 		struct node_state_opt	*state_opt;
3933b3a8eb9SGleb Smirnoff 		struct peer		 peer;
3943b3a8eb9SGleb Smirnoff 		struct {
3953b3a8eb9SGleb Smirnoff 			struct peer	 src, dst;
3963b3a8eb9SGleb Smirnoff 			struct node_os	*src_os;
3973b3a8eb9SGleb Smirnoff 		}			 fromto;
3983b3a8eb9SGleb Smirnoff 		struct {
3993b3a8eb9SGleb Smirnoff 			struct node_host	*host;
4003b3a8eb9SGleb Smirnoff 			u_int8_t		 rt;
4013b3a8eb9SGleb Smirnoff 			u_int8_t		 pool_opts;
4023b3a8eb9SGleb Smirnoff 			sa_family_t		 af;
4033b3a8eb9SGleb Smirnoff 			struct pf_poolhashkey	*key;
4043b3a8eb9SGleb Smirnoff 		}			 route;
4053b3a8eb9SGleb Smirnoff 		struct redirection {
4063b3a8eb9SGleb Smirnoff 			struct node_host	*host;
4073b3a8eb9SGleb Smirnoff 			struct range		 rport;
4083b3a8eb9SGleb Smirnoff 		}			*redirection;
4093b3a8eb9SGleb Smirnoff 		struct {
4103b3a8eb9SGleb Smirnoff 			int			 action;
4113b3a8eb9SGleb Smirnoff 			struct node_state_opt	*options;
4123b3a8eb9SGleb Smirnoff 		}			 keep_state;
4133b3a8eb9SGleb Smirnoff 		struct {
4143b3a8eb9SGleb Smirnoff 			u_int8_t	 log;
4153b3a8eb9SGleb Smirnoff 			u_int8_t	 logif;
4163b3a8eb9SGleb Smirnoff 			u_int8_t	 quick;
4173b3a8eb9SGleb Smirnoff 		}			 logquick;
4183b3a8eb9SGleb Smirnoff 		struct {
4193b3a8eb9SGleb Smirnoff 			int		 neg;
4203b3a8eb9SGleb Smirnoff 			char		*name;
4213b3a8eb9SGleb Smirnoff 		}			 tagged;
4223b3a8eb9SGleb Smirnoff 		struct pf_poolhashkey	*hashkey;
4233b3a8eb9SGleb Smirnoff 		struct node_queue	*queue;
4243b3a8eb9SGleb Smirnoff 		struct node_queue_opt	 queue_options;
4253b3a8eb9SGleb Smirnoff 		struct node_queue_bw	 queue_bwspec;
4263b3a8eb9SGleb Smirnoff 		struct node_qassign	 qassign;
4273b3a8eb9SGleb Smirnoff 		struct filter_opts	 filter_opts;
4283b3a8eb9SGleb Smirnoff 		struct antispoof_opts	 antispoof_opts;
4293b3a8eb9SGleb Smirnoff 		struct queue_opts	 queue_opts;
4303b3a8eb9SGleb Smirnoff 		struct scrub_opts	 scrub_opts;
4313b3a8eb9SGleb Smirnoff 		struct table_opts	 table_opts;
4323b3a8eb9SGleb Smirnoff 		struct pool_opts	 pool_opts;
4333b3a8eb9SGleb Smirnoff 		struct node_hfsc_opts	 hfsc_opts;
434a5b789f6SErmal Luçi 		struct node_fairq_opts	 fairq_opts;
4350a70aaf8SLuiz Otavio O Souza 		struct codel_opts	 codel_opts;
4363b3a8eb9SGleb Smirnoff 	} v;
4373b3a8eb9SGleb Smirnoff 	int lineno;
4383b3a8eb9SGleb Smirnoff } YYSTYPE;
4393b3a8eb9SGleb Smirnoff 
4403b3a8eb9SGleb Smirnoff #define PPORT_RANGE	1
4413b3a8eb9SGleb Smirnoff #define PPORT_STAR	2
4423b3a8eb9SGleb Smirnoff int	parseport(char *, struct range *r, int);
4433b3a8eb9SGleb Smirnoff 
4443b3a8eb9SGleb Smirnoff #define DYNIF_MULTIADDR(addr) ((addr).type == PF_ADDR_DYNIFTL && \
4453b3a8eb9SGleb Smirnoff 	(!((addr).iflags & PFI_AFLAG_NOALIAS) ||		 \
4463b3a8eb9SGleb Smirnoff 	!isdigit((addr).v.ifname[strlen((addr).v.ifname)-1])))
4473b3a8eb9SGleb Smirnoff 
4483b3a8eb9SGleb Smirnoff %}
4493b3a8eb9SGleb Smirnoff 
4503b3a8eb9SGleb Smirnoff %token	PASS BLOCK SCRUB RETURN IN OS OUT LOG QUICK ON FROM TO FLAGS
4513b3a8eb9SGleb Smirnoff %token	RETURNRST RETURNICMP RETURNICMP6 PROTO INET INET6 ALL ANY ICMPTYPE
4523b3a8eb9SGleb Smirnoff %token	ICMP6TYPE CODE KEEP MODULATE STATE PORT RDR NAT BINAT ARROW NODF
4533b3a8eb9SGleb Smirnoff %token	MINTTL ERROR ALLOWOPTS FASTROUTE FILENAME ROUTETO DUPTO REPLYTO NO LABEL
4543b3a8eb9SGleb Smirnoff %token	NOROUTE URPFFAILED FRAGMENT USER GROUP MAXMSS MAXIMUM TTL TOS DROP TABLE
4553b3a8eb9SGleb Smirnoff %token	REASSEMBLE FRAGDROP FRAGCROP ANCHOR NATANCHOR RDRANCHOR BINATANCHOR
4563b3a8eb9SGleb Smirnoff %token	SET OPTIMIZATION TIMEOUT LIMIT LOGINTERFACE BLOCKPOLICY RANDOMID
4573b3a8eb9SGleb Smirnoff %token	REQUIREORDER SYNPROXY FINGERPRINTS NOSYNC DEBUG SKIP HOSTID
4583b3a8eb9SGleb Smirnoff %token	ANTISPOOF FOR INCLUDE
4593b3a8eb9SGleb Smirnoff %token	BITMASK RANDOM SOURCEHASH ROUNDROBIN STATICPORT PROBABILITY
4600a70aaf8SLuiz Otavio O Souza %token	ALTQ CBQ CODEL PRIQ HFSC FAIRQ BANDWIDTH TBRSIZE LINKSHARE REALTIME
4610a70aaf8SLuiz Otavio O Souza %token	UPPERLIMIT QUEUE PRIORITY QLIMIT HOGS BUCKETS RTABLE TARGET INTERVAL
4623e248e0fSKristof Provost %token	LOAD RULESET_OPTIMIZATION PRIO
4633b3a8eb9SGleb Smirnoff %token	STICKYADDRESS MAXSRCSTATES MAXSRCNODES SOURCETRACK GLOBAL RULE
4643b3a8eb9SGleb Smirnoff %token	MAXSRCCONN MAXSRCCONNRATE OVERLOAD FLUSH SLOPPY
4653b3a8eb9SGleb Smirnoff %token	TAGGED TAG IFBOUND FLOATING STATEPOLICY STATEDEFAULTS ROUTE SETTOS
4663b3a8eb9SGleb Smirnoff %token	DIVERTTO DIVERTREPLY
4673b3a8eb9SGleb Smirnoff %token	<v.string>		STRING
4683b3a8eb9SGleb Smirnoff %token	<v.number>		NUMBER
4693b3a8eb9SGleb Smirnoff %token	<v.i>			PORTBINARY
4703b3a8eb9SGleb Smirnoff %type	<v.interface>		interface if_list if_item_not if_item
4713b3a8eb9SGleb Smirnoff %type	<v.number>		number icmptype icmp6type uid gid
4723b3a8eb9SGleb Smirnoff %type	<v.number>		tos not yesno
4733b3a8eb9SGleb Smirnoff %type	<v.probability>		probability
4743b3a8eb9SGleb Smirnoff %type	<v.i>			no dir af fragcache optimizer
4753b3a8eb9SGleb Smirnoff %type	<v.i>			sourcetrack flush unaryop statelock
4763b3a8eb9SGleb Smirnoff %type	<v.b>			action nataction natpasslog scrubaction
4773e248e0fSKristof Provost %type	<v.b>			flags flag blockspec prio
4783b3a8eb9SGleb Smirnoff %type	<v.range>		portplain portstar portrange
4793b3a8eb9SGleb Smirnoff %type	<v.hashkey>		hashkey
4803b3a8eb9SGleb Smirnoff %type	<v.proto>		proto proto_list proto_item
4813b3a8eb9SGleb Smirnoff %type	<v.number>		protoval
4823b3a8eb9SGleb Smirnoff %type	<v.icmp>		icmpspec
4833b3a8eb9SGleb Smirnoff %type	<v.icmp>		icmp_list icmp_item
4843b3a8eb9SGleb Smirnoff %type	<v.icmp>		icmp6_list icmp6_item
4853b3a8eb9SGleb Smirnoff %type	<v.number>		reticmpspec reticmp6spec
4863b3a8eb9SGleb Smirnoff %type	<v.fromto>		fromto
4873b3a8eb9SGleb Smirnoff %type	<v.peer>		ipportspec from to
4883b3a8eb9SGleb Smirnoff %type	<v.host>		ipspec toipspec xhost host dynaddr host_list
4893b3a8eb9SGleb Smirnoff %type	<v.host>		redir_host_list redirspec
4903b3a8eb9SGleb Smirnoff %type	<v.host>		route_host route_host_list routespec
4913b3a8eb9SGleb Smirnoff %type	<v.os>			os xos os_list
4923b3a8eb9SGleb Smirnoff %type	<v.port>		portspec port_list port_item
4933b3a8eb9SGleb Smirnoff %type	<v.uid>			uids uid_list uid_item
4943b3a8eb9SGleb Smirnoff %type	<v.gid>			gids gid_list gid_item
4953b3a8eb9SGleb Smirnoff %type	<v.route>		route
4963b3a8eb9SGleb Smirnoff %type	<v.redirection>		redirection redirpool
4973b3a8eb9SGleb Smirnoff %type	<v.string>		label stringall tag anchorname
4983b3a8eb9SGleb Smirnoff %type	<v.string>		string varstring numberstring
4993b3a8eb9SGleb Smirnoff %type	<v.keep_state>		keep
5003b3a8eb9SGleb Smirnoff %type	<v.state_opt>		state_opt_spec state_opt_list state_opt_item
5013b3a8eb9SGleb Smirnoff %type	<v.logquick>		logquick quick log logopts logopt
5023b3a8eb9SGleb Smirnoff %type	<v.interface>		antispoof_ifspc antispoof_iflst antispoof_if
5033b3a8eb9SGleb Smirnoff %type	<v.qassign>		qname
5043b3a8eb9SGleb Smirnoff %type	<v.queue>		qassign qassign_list qassign_item
5053b3a8eb9SGleb Smirnoff %type	<v.queue_options>	scheduler
5063b3a8eb9SGleb Smirnoff %type	<v.number>		cbqflags_list cbqflags_item
5073b3a8eb9SGleb Smirnoff %type	<v.number>		priqflags_list priqflags_item
5083b3a8eb9SGleb Smirnoff %type	<v.hfsc_opts>		hfscopts_list hfscopts_item hfsc_opts
509a5b789f6SErmal Luçi %type	<v.fairq_opts>		fairqopts_list fairqopts_item fairq_opts
5100a70aaf8SLuiz Otavio O Souza %type	<v.codel_opts>		codelopts_list codelopts_item codel_opts
5113b3a8eb9SGleb Smirnoff %type	<v.queue_bwspec>	bandwidth
5123b3a8eb9SGleb Smirnoff %type	<v.filter_opts>		filter_opts filter_opt filter_opts_l
5133e248e0fSKristof Provost %type	<v.filter_opts>		filter_sets filter_set filter_sets_l
5143b3a8eb9SGleb Smirnoff %type	<v.antispoof_opts>	antispoof_opts antispoof_opt antispoof_opts_l
5153b3a8eb9SGleb Smirnoff %type	<v.queue_opts>		queue_opts queue_opt queue_opts_l
5163b3a8eb9SGleb Smirnoff %type	<v.scrub_opts>		scrub_opts scrub_opt scrub_opts_l
5173b3a8eb9SGleb Smirnoff %type	<v.table_opts>		table_opts table_opt table_opts_l
5183b3a8eb9SGleb Smirnoff %type	<v.pool_opts>		pool_opts pool_opt pool_opts_l
5193b3a8eb9SGleb Smirnoff %type	<v.tagged>		tagged
5203b3a8eb9SGleb Smirnoff %type	<v.rtableid>		rtable
5213b3a8eb9SGleb Smirnoff %%
5223b3a8eb9SGleb Smirnoff 
5233b3a8eb9SGleb Smirnoff ruleset		: /* empty */
5243b3a8eb9SGleb Smirnoff 		| ruleset include '\n'
5253b3a8eb9SGleb Smirnoff 		| ruleset '\n'
5263b3a8eb9SGleb Smirnoff 		| ruleset option '\n'
5273b3a8eb9SGleb Smirnoff 		| ruleset scrubrule '\n'
5283b3a8eb9SGleb Smirnoff 		| ruleset natrule '\n'
5293b3a8eb9SGleb Smirnoff 		| ruleset binatrule '\n'
5303b3a8eb9SGleb Smirnoff 		| ruleset pfrule '\n'
5313b3a8eb9SGleb Smirnoff 		| ruleset anchorrule '\n'
5323b3a8eb9SGleb Smirnoff 		| ruleset loadrule '\n'
5333b3a8eb9SGleb Smirnoff 		| ruleset altqif '\n'
5343b3a8eb9SGleb Smirnoff 		| ruleset queuespec '\n'
5353b3a8eb9SGleb Smirnoff 		| ruleset varset '\n'
5363b3a8eb9SGleb Smirnoff 		| ruleset antispoof '\n'
5373b3a8eb9SGleb Smirnoff 		| ruleset tabledef '\n'
5383b3a8eb9SGleb Smirnoff 		| '{' fakeanchor '}' '\n';
5393b3a8eb9SGleb Smirnoff 		| ruleset error '\n'		{ file->errors++; }
5403b3a8eb9SGleb Smirnoff 		;
5413b3a8eb9SGleb Smirnoff 
5423b3a8eb9SGleb Smirnoff include		: INCLUDE STRING		{
5433b3a8eb9SGleb Smirnoff 			struct file	*nfile;
5443b3a8eb9SGleb Smirnoff 
5453b3a8eb9SGleb Smirnoff 			if ((nfile = pushfile($2, 0)) == NULL) {
5463b3a8eb9SGleb Smirnoff 				yyerror("failed to include file %s", $2);
5473b3a8eb9SGleb Smirnoff 				free($2);
5483b3a8eb9SGleb Smirnoff 				YYERROR;
5493b3a8eb9SGleb Smirnoff 			}
5503b3a8eb9SGleb Smirnoff 			free($2);
5513b3a8eb9SGleb Smirnoff 
5523b3a8eb9SGleb Smirnoff 			file = nfile;
5533b3a8eb9SGleb Smirnoff 			lungetc('\n');
5543b3a8eb9SGleb Smirnoff 		}
5553b3a8eb9SGleb Smirnoff 		;
5563b3a8eb9SGleb Smirnoff 
5573b3a8eb9SGleb Smirnoff /*
5583b3a8eb9SGleb Smirnoff  * apply to previouslys specified rule: must be careful to note
5593b3a8eb9SGleb Smirnoff  * what that is: pf or nat or binat or rdr
5603b3a8eb9SGleb Smirnoff  */
5613b3a8eb9SGleb Smirnoff fakeanchor	: fakeanchor '\n'
5623b3a8eb9SGleb Smirnoff 		| fakeanchor anchorrule '\n'
5633b3a8eb9SGleb Smirnoff 		| fakeanchor binatrule '\n'
5643b3a8eb9SGleb Smirnoff 		| fakeanchor natrule '\n'
5653b3a8eb9SGleb Smirnoff 		| fakeanchor pfrule '\n'
5663b3a8eb9SGleb Smirnoff 		| fakeanchor error '\n'
5673b3a8eb9SGleb Smirnoff 		;
5683b3a8eb9SGleb Smirnoff 
5693b3a8eb9SGleb Smirnoff optimizer	: string	{
5703b3a8eb9SGleb Smirnoff 			if (!strcmp($1, "none"))
5713b3a8eb9SGleb Smirnoff 				$$ = 0;
5723b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "basic"))
5733b3a8eb9SGleb Smirnoff 				$$ = PF_OPTIMIZE_BASIC;
5743b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "profile"))
5753b3a8eb9SGleb Smirnoff 				$$ = PF_OPTIMIZE_BASIC | PF_OPTIMIZE_PROFILE;
5763b3a8eb9SGleb Smirnoff 			else {
5773b3a8eb9SGleb Smirnoff 				yyerror("unknown ruleset-optimization %s", $1);
5783b3a8eb9SGleb Smirnoff 				YYERROR;
5793b3a8eb9SGleb Smirnoff 			}
5803b3a8eb9SGleb Smirnoff 		}
5813b3a8eb9SGleb Smirnoff 		;
5823b3a8eb9SGleb Smirnoff 
5833b3a8eb9SGleb Smirnoff option		: SET OPTIMIZATION STRING		{
5843b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_OPTION)) {
5853b3a8eb9SGleb Smirnoff 				free($3);
5863b3a8eb9SGleb Smirnoff 				YYERROR;
5873b3a8eb9SGleb Smirnoff 			}
5883b3a8eb9SGleb Smirnoff 			if (pfctl_set_optimization(pf, $3) != 0) {
5893b3a8eb9SGleb Smirnoff 				yyerror("unknown optimization %s", $3);
5903b3a8eb9SGleb Smirnoff 				free($3);
5913b3a8eb9SGleb Smirnoff 				YYERROR;
5923b3a8eb9SGleb Smirnoff 			}
5933b3a8eb9SGleb Smirnoff 			free($3);
5943b3a8eb9SGleb Smirnoff 		}
5953b3a8eb9SGleb Smirnoff 		| SET RULESET_OPTIMIZATION optimizer {
5963b3a8eb9SGleb Smirnoff 			if (!(pf->opts & PF_OPT_OPTIMIZE)) {
5973b3a8eb9SGleb Smirnoff 				pf->opts |= PF_OPT_OPTIMIZE;
5983b3a8eb9SGleb Smirnoff 				pf->optimize = $3;
5993b3a8eb9SGleb Smirnoff 			}
6003b3a8eb9SGleb Smirnoff 		}
6013b3a8eb9SGleb Smirnoff 		| SET TIMEOUT timeout_spec
6023b3a8eb9SGleb Smirnoff 		| SET TIMEOUT '{' optnl timeout_list '}'
6033b3a8eb9SGleb Smirnoff 		| SET LIMIT limit_spec
6043b3a8eb9SGleb Smirnoff 		| SET LIMIT '{' optnl limit_list '}'
6053b3a8eb9SGleb Smirnoff 		| SET LOGINTERFACE stringall		{
6063b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_OPTION)) {
6073b3a8eb9SGleb Smirnoff 				free($3);
6083b3a8eb9SGleb Smirnoff 				YYERROR;
6093b3a8eb9SGleb Smirnoff 			}
6103b3a8eb9SGleb Smirnoff 			if (pfctl_set_logif(pf, $3) != 0) {
6113b3a8eb9SGleb Smirnoff 				yyerror("error setting loginterface %s", $3);
6123b3a8eb9SGleb Smirnoff 				free($3);
6133b3a8eb9SGleb Smirnoff 				YYERROR;
6143b3a8eb9SGleb Smirnoff 			}
6153b3a8eb9SGleb Smirnoff 			free($3);
6163b3a8eb9SGleb Smirnoff 		}
6173b3a8eb9SGleb Smirnoff 		| SET HOSTID number {
6183b3a8eb9SGleb Smirnoff 			if ($3 == 0 || $3 > UINT_MAX) {
6193b3a8eb9SGleb Smirnoff 				yyerror("hostid must be non-zero");
6203b3a8eb9SGleb Smirnoff 				YYERROR;
6213b3a8eb9SGleb Smirnoff 			}
6223b3a8eb9SGleb Smirnoff 			if (pfctl_set_hostid(pf, $3) != 0) {
6233b3a8eb9SGleb Smirnoff 				yyerror("error setting hostid %08x", $3);
6243b3a8eb9SGleb Smirnoff 				YYERROR;
6253b3a8eb9SGleb Smirnoff 			}
6263b3a8eb9SGleb Smirnoff 		}
6273b3a8eb9SGleb Smirnoff 		| SET BLOCKPOLICY DROP	{
6283b3a8eb9SGleb Smirnoff 			if (pf->opts & PF_OPT_VERBOSE)
6293b3a8eb9SGleb Smirnoff 				printf("set block-policy drop\n");
6303b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_OPTION))
6313b3a8eb9SGleb Smirnoff 				YYERROR;
6323b3a8eb9SGleb Smirnoff 			blockpolicy = PFRULE_DROP;
6333b3a8eb9SGleb Smirnoff 		}
6343b3a8eb9SGleb Smirnoff 		| SET BLOCKPOLICY RETURN {
6353b3a8eb9SGleb Smirnoff 			if (pf->opts & PF_OPT_VERBOSE)
6363b3a8eb9SGleb Smirnoff 				printf("set block-policy return\n");
6373b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_OPTION))
6383b3a8eb9SGleb Smirnoff 				YYERROR;
6393b3a8eb9SGleb Smirnoff 			blockpolicy = PFRULE_RETURN;
6403b3a8eb9SGleb Smirnoff 		}
6413b3a8eb9SGleb Smirnoff 		| SET REQUIREORDER yesno {
6423b3a8eb9SGleb Smirnoff 			if (pf->opts & PF_OPT_VERBOSE)
6433b3a8eb9SGleb Smirnoff 				printf("set require-order %s\n",
6443b3a8eb9SGleb Smirnoff 				    $3 == 1 ? "yes" : "no");
6453b3a8eb9SGleb Smirnoff 			require_order = $3;
6463b3a8eb9SGleb Smirnoff 		}
6473b3a8eb9SGleb Smirnoff 		| SET FINGERPRINTS STRING {
6483b3a8eb9SGleb Smirnoff 			if (pf->opts & PF_OPT_VERBOSE)
6493b3a8eb9SGleb Smirnoff 				printf("set fingerprints \"%s\"\n", $3);
6503b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_OPTION)) {
6513b3a8eb9SGleb Smirnoff 				free($3);
6523b3a8eb9SGleb Smirnoff 				YYERROR;
6533b3a8eb9SGleb Smirnoff 			}
6543b3a8eb9SGleb Smirnoff 			if (!pf->anchor->name[0]) {
6553b3a8eb9SGleb Smirnoff 				if (pfctl_file_fingerprints(pf->dev,
6563b3a8eb9SGleb Smirnoff 				    pf->opts, $3)) {
6573b3a8eb9SGleb Smirnoff 					yyerror("error loading "
6583b3a8eb9SGleb Smirnoff 					    "fingerprints %s", $3);
6593b3a8eb9SGleb Smirnoff 					free($3);
6603b3a8eb9SGleb Smirnoff 					YYERROR;
6613b3a8eb9SGleb Smirnoff 				}
6623b3a8eb9SGleb Smirnoff 			}
6633b3a8eb9SGleb Smirnoff 			free($3);
6643b3a8eb9SGleb Smirnoff 		}
6653b3a8eb9SGleb Smirnoff 		| SET STATEPOLICY statelock {
6663b3a8eb9SGleb Smirnoff 			if (pf->opts & PF_OPT_VERBOSE)
6673b3a8eb9SGleb Smirnoff 				switch ($3) {
6683b3a8eb9SGleb Smirnoff 				case 0:
6693b3a8eb9SGleb Smirnoff 					printf("set state-policy floating\n");
6703b3a8eb9SGleb Smirnoff 					break;
6713b3a8eb9SGleb Smirnoff 				case PFRULE_IFBOUND:
6723b3a8eb9SGleb Smirnoff 					printf("set state-policy if-bound\n");
6733b3a8eb9SGleb Smirnoff 					break;
6743b3a8eb9SGleb Smirnoff 				}
6753b3a8eb9SGleb Smirnoff 			default_statelock = $3;
6763b3a8eb9SGleb Smirnoff 		}
6773b3a8eb9SGleb Smirnoff 		| SET DEBUG STRING {
6783b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_OPTION)) {
6793b3a8eb9SGleb Smirnoff 				free($3);
6803b3a8eb9SGleb Smirnoff 				YYERROR;
6813b3a8eb9SGleb Smirnoff 			}
6823b3a8eb9SGleb Smirnoff 			if (pfctl_set_debug(pf, $3) != 0) {
6833b3a8eb9SGleb Smirnoff 				yyerror("error setting debuglevel %s", $3);
6843b3a8eb9SGleb Smirnoff 				free($3);
6853b3a8eb9SGleb Smirnoff 				YYERROR;
6863b3a8eb9SGleb Smirnoff 			}
6873b3a8eb9SGleb Smirnoff 			free($3);
6883b3a8eb9SGleb Smirnoff 		}
6893b3a8eb9SGleb Smirnoff 		| SET SKIP interface {
6903b3a8eb9SGleb Smirnoff 			if (expand_skip_interface($3) != 0) {
6913b3a8eb9SGleb Smirnoff 				yyerror("error setting skip interface(s)");
6923b3a8eb9SGleb Smirnoff 				YYERROR;
6933b3a8eb9SGleb Smirnoff 			}
6943b3a8eb9SGleb Smirnoff 		}
6953b3a8eb9SGleb Smirnoff 		| SET STATEDEFAULTS state_opt_list {
6963b3a8eb9SGleb Smirnoff 			if (keep_state_defaults != NULL) {
6973b3a8eb9SGleb Smirnoff 				yyerror("cannot redefine state-defaults");
6983b3a8eb9SGleb Smirnoff 				YYERROR;
6993b3a8eb9SGleb Smirnoff 			}
7003b3a8eb9SGleb Smirnoff 			keep_state_defaults = $3;
7013b3a8eb9SGleb Smirnoff 		}
7023b3a8eb9SGleb Smirnoff 		;
7033b3a8eb9SGleb Smirnoff 
7043b3a8eb9SGleb Smirnoff stringall	: STRING	{ $$ = $1; }
7053b3a8eb9SGleb Smirnoff 		| ALL		{
7063b3a8eb9SGleb Smirnoff 			if (($$ = strdup("all")) == NULL) {
7073b3a8eb9SGleb Smirnoff 				err(1, "stringall: strdup");
7083b3a8eb9SGleb Smirnoff 			}
7093b3a8eb9SGleb Smirnoff 		}
7103b3a8eb9SGleb Smirnoff 		;
7113b3a8eb9SGleb Smirnoff 
7123b3a8eb9SGleb Smirnoff string		: STRING string				{
7133b3a8eb9SGleb Smirnoff 			if (asprintf(&$$, "%s %s", $1, $2) == -1)
7143b3a8eb9SGleb Smirnoff 				err(1, "string: asprintf");
7153b3a8eb9SGleb Smirnoff 			free($1);
7163b3a8eb9SGleb Smirnoff 			free($2);
7173b3a8eb9SGleb Smirnoff 		}
7183b3a8eb9SGleb Smirnoff 		| STRING
7193b3a8eb9SGleb Smirnoff 		;
7203b3a8eb9SGleb Smirnoff 
7213b3a8eb9SGleb Smirnoff varstring	: numberstring varstring 		{
7223b3a8eb9SGleb Smirnoff 			if (asprintf(&$$, "%s %s", $1, $2) == -1)
7233b3a8eb9SGleb Smirnoff 				err(1, "string: asprintf");
7243b3a8eb9SGleb Smirnoff 			free($1);
7253b3a8eb9SGleb Smirnoff 			free($2);
7263b3a8eb9SGleb Smirnoff 		}
7273b3a8eb9SGleb Smirnoff 		| numberstring
7283b3a8eb9SGleb Smirnoff 		;
7293b3a8eb9SGleb Smirnoff 
7303b3a8eb9SGleb Smirnoff numberstring	: NUMBER				{
7313b3a8eb9SGleb Smirnoff 			char	*s;
7323b3a8eb9SGleb Smirnoff 			if (asprintf(&s, "%lld", (long long)$1) == -1) {
7333b3a8eb9SGleb Smirnoff 				yyerror("string: asprintf");
7343b3a8eb9SGleb Smirnoff 				YYERROR;
7353b3a8eb9SGleb Smirnoff 			}
7363b3a8eb9SGleb Smirnoff 			$$ = s;
7373b3a8eb9SGleb Smirnoff 		}
7383b3a8eb9SGleb Smirnoff 		| STRING
7393b3a8eb9SGleb Smirnoff 		;
7403b3a8eb9SGleb Smirnoff 
7413b3a8eb9SGleb Smirnoff varset		: STRING '=' varstring	{
7423b3a8eb9SGleb Smirnoff 			if (pf->opts & PF_OPT_VERBOSE)
7433b3a8eb9SGleb Smirnoff 				printf("%s = \"%s\"\n", $1, $3);
7443b3a8eb9SGleb Smirnoff 			if (symset($1, $3, 0) == -1)
7453b3a8eb9SGleb Smirnoff 				err(1, "cannot store variable %s", $1);
7463b3a8eb9SGleb Smirnoff 			free($1);
7473b3a8eb9SGleb Smirnoff 			free($3);
7483b3a8eb9SGleb Smirnoff 		}
7493b3a8eb9SGleb Smirnoff 		;
7503b3a8eb9SGleb Smirnoff 
7513b3a8eb9SGleb Smirnoff anchorname	: STRING			{ $$ = $1; }
7523b3a8eb9SGleb Smirnoff 		| /* empty */			{ $$ = NULL; }
7533b3a8eb9SGleb Smirnoff 		;
7543b3a8eb9SGleb Smirnoff 
7553b3a8eb9SGleb Smirnoff pfa_anchorlist	: /* empty */
7563b3a8eb9SGleb Smirnoff 		| pfa_anchorlist '\n'
7573b3a8eb9SGleb Smirnoff 		| pfa_anchorlist pfrule '\n'
7583b3a8eb9SGleb Smirnoff 		| pfa_anchorlist anchorrule '\n'
7593b3a8eb9SGleb Smirnoff 		;
7603b3a8eb9SGleb Smirnoff 
7613b3a8eb9SGleb Smirnoff pfa_anchor	: '{'
7623b3a8eb9SGleb Smirnoff 		{
7633b3a8eb9SGleb Smirnoff 			char ta[PF_ANCHOR_NAME_SIZE];
7643b3a8eb9SGleb Smirnoff 			struct pf_ruleset *rs;
7653b3a8eb9SGleb Smirnoff 
7663b3a8eb9SGleb Smirnoff 			/* steping into a brace anchor */
7673b3a8eb9SGleb Smirnoff 			pf->asd++;
7683b3a8eb9SGleb Smirnoff 			pf->bn++;
7693b3a8eb9SGleb Smirnoff 			pf->brace = 1;
7703b3a8eb9SGleb Smirnoff 
7713b3a8eb9SGleb Smirnoff 			/* create a holding ruleset in the root */
7723b3a8eb9SGleb Smirnoff 			snprintf(ta, PF_ANCHOR_NAME_SIZE, "_%d", pf->bn);
7733b3a8eb9SGleb Smirnoff 			rs = pf_find_or_create_ruleset(ta);
7743b3a8eb9SGleb Smirnoff 			if (rs == NULL)
7753b3a8eb9SGleb Smirnoff 				err(1, "pfa_anchor: pf_find_or_create_ruleset");
7763b3a8eb9SGleb Smirnoff 			pf->astack[pf->asd] = rs->anchor;
7773b3a8eb9SGleb Smirnoff 			pf->anchor = rs->anchor;
7783b3a8eb9SGleb Smirnoff 		} '\n' pfa_anchorlist '}'
7793b3a8eb9SGleb Smirnoff 		{
7803b3a8eb9SGleb Smirnoff 			pf->alast = pf->anchor;
7813b3a8eb9SGleb Smirnoff 			pf->asd--;
7823b3a8eb9SGleb Smirnoff 			pf->anchor = pf->astack[pf->asd];
7833b3a8eb9SGleb Smirnoff 		}
7843b3a8eb9SGleb Smirnoff 		| /* empty */
7853b3a8eb9SGleb Smirnoff 		;
7863b3a8eb9SGleb Smirnoff 
7873b3a8eb9SGleb Smirnoff anchorrule	: ANCHOR anchorname dir quick interface af proto fromto
7883b3a8eb9SGleb Smirnoff 		    filter_opts pfa_anchor
7893b3a8eb9SGleb Smirnoff 		{
7903b3a8eb9SGleb Smirnoff 			struct pf_rule	r;
7913b3a8eb9SGleb Smirnoff 			struct node_proto	*proto;
7923b3a8eb9SGleb Smirnoff 
7933b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_FILTER)) {
7943b3a8eb9SGleb Smirnoff 				if ($2)
7953b3a8eb9SGleb Smirnoff 					free($2);
7963b3a8eb9SGleb Smirnoff 				YYERROR;
7973b3a8eb9SGleb Smirnoff 			}
7983b3a8eb9SGleb Smirnoff 
7993b3a8eb9SGleb Smirnoff 			if ($2 && ($2[0] == '_' || strstr($2, "/_") != NULL)) {
8003b3a8eb9SGleb Smirnoff 				free($2);
8013b3a8eb9SGleb Smirnoff 				yyerror("anchor names beginning with '_' "
8023b3a8eb9SGleb Smirnoff 				    "are reserved for internal use");
8033b3a8eb9SGleb Smirnoff 				YYERROR;
8043b3a8eb9SGleb Smirnoff 			}
8053b3a8eb9SGleb Smirnoff 
8063b3a8eb9SGleb Smirnoff 			memset(&r, 0, sizeof(r));
8073b3a8eb9SGleb Smirnoff 			if (pf->astack[pf->asd + 1]) {
8083b3a8eb9SGleb Smirnoff 				/* move inline rules into relative location */
8093b3a8eb9SGleb Smirnoff 				pf_anchor_setup(&r,
8103b3a8eb9SGleb Smirnoff 				    &pf->astack[pf->asd]->ruleset,
8113b3a8eb9SGleb Smirnoff 				    $2 ? $2 : pf->alast->name);
8123b3a8eb9SGleb Smirnoff 
8133b3a8eb9SGleb Smirnoff 				if (r.anchor == NULL)
8143b3a8eb9SGleb Smirnoff 					err(1, "anchorrule: unable to "
8153b3a8eb9SGleb Smirnoff 					    "create ruleset");
8163b3a8eb9SGleb Smirnoff 
8173b3a8eb9SGleb Smirnoff 				if (pf->alast != r.anchor) {
8183b3a8eb9SGleb Smirnoff 					if (r.anchor->match) {
8193b3a8eb9SGleb Smirnoff 						yyerror("inline anchor '%s' "
8203b3a8eb9SGleb Smirnoff 						    "already exists",
8213b3a8eb9SGleb Smirnoff 						    r.anchor->name);
8223b3a8eb9SGleb Smirnoff 						YYERROR;
8233b3a8eb9SGleb Smirnoff 					}
8243b3a8eb9SGleb Smirnoff 					mv_rules(&pf->alast->ruleset,
8253b3a8eb9SGleb Smirnoff 					    &r.anchor->ruleset);
8263b3a8eb9SGleb Smirnoff 				}
8273b3a8eb9SGleb Smirnoff 				pf_remove_if_empty_ruleset(&pf->alast->ruleset);
8283b3a8eb9SGleb Smirnoff 				pf->alast = r.anchor;
8293b3a8eb9SGleb Smirnoff 			} else {
8303b3a8eb9SGleb Smirnoff 				if (!$2) {
8313b3a8eb9SGleb Smirnoff 					yyerror("anchors without explicit "
8323b3a8eb9SGleb Smirnoff 					    "rules must specify a name");
8333b3a8eb9SGleb Smirnoff 					YYERROR;
8343b3a8eb9SGleb Smirnoff 				}
8353b3a8eb9SGleb Smirnoff 			}
8363b3a8eb9SGleb Smirnoff 			r.direction = $3;
8373b3a8eb9SGleb Smirnoff 			r.quick = $4.quick;
8383b3a8eb9SGleb Smirnoff 			r.af = $6;
8393b3a8eb9SGleb Smirnoff 			r.prob = $9.prob;
8403b3a8eb9SGleb Smirnoff 			r.rtableid = $9.rtableid;
8413b3a8eb9SGleb Smirnoff 
8423b3a8eb9SGleb Smirnoff 			if ($9.tag)
8433b3a8eb9SGleb Smirnoff 				if (strlcpy(r.tagname, $9.tag,
8443b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
8453b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
8463b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
8473b3a8eb9SGleb Smirnoff 					YYERROR;
8483b3a8eb9SGleb Smirnoff 				}
8493b3a8eb9SGleb Smirnoff 			if ($9.match_tag)
8503b3a8eb9SGleb Smirnoff 				if (strlcpy(r.match_tagname, $9.match_tag,
8513b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
8523b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
8533b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
8543b3a8eb9SGleb Smirnoff 					YYERROR;
8553b3a8eb9SGleb Smirnoff 				}
8563b3a8eb9SGleb Smirnoff 			r.match_tag_not = $9.match_tag_not;
8573b3a8eb9SGleb Smirnoff 			if (rule_label(&r, $9.label))
8583b3a8eb9SGleb Smirnoff 				YYERROR;
8593b3a8eb9SGleb Smirnoff 			free($9.label);
8603b3a8eb9SGleb Smirnoff 			r.flags = $9.flags.b1;
8613b3a8eb9SGleb Smirnoff 			r.flagset = $9.flags.b2;
8623b3a8eb9SGleb Smirnoff 			if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
8633b3a8eb9SGleb Smirnoff 				yyerror("flags always false");
8643b3a8eb9SGleb Smirnoff 				YYERROR;
8653b3a8eb9SGleb Smirnoff 			}
8663b3a8eb9SGleb Smirnoff 			if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
8673b3a8eb9SGleb Smirnoff 				for (proto = $7; proto != NULL &&
8683b3a8eb9SGleb Smirnoff 				    proto->proto != IPPROTO_TCP;
8693b3a8eb9SGleb Smirnoff 				    proto = proto->next)
8703b3a8eb9SGleb Smirnoff 					;	/* nothing */
8713b3a8eb9SGleb Smirnoff 				if (proto == NULL && $7 != NULL) {
8723b3a8eb9SGleb Smirnoff 					if ($9.flags.b1 || $9.flags.b2)
8733b3a8eb9SGleb Smirnoff 						yyerror(
8743b3a8eb9SGleb Smirnoff 						    "flags only apply to tcp");
8753b3a8eb9SGleb Smirnoff 					if ($8.src_os)
8763b3a8eb9SGleb Smirnoff 						yyerror(
8773b3a8eb9SGleb Smirnoff 						    "OS fingerprinting only "
8783b3a8eb9SGleb Smirnoff 						    "applies to tcp");
8793b3a8eb9SGleb Smirnoff 					YYERROR;
8803b3a8eb9SGleb Smirnoff 				}
8813b3a8eb9SGleb Smirnoff 			}
8823b3a8eb9SGleb Smirnoff 
8833b3a8eb9SGleb Smirnoff 			r.tos = $9.tos;
8843b3a8eb9SGleb Smirnoff 
8853b3a8eb9SGleb Smirnoff 			if ($9.keep.action) {
8863b3a8eb9SGleb Smirnoff 				yyerror("cannot specify state handling "
8873b3a8eb9SGleb Smirnoff 				    "on anchors");
8883b3a8eb9SGleb Smirnoff 				YYERROR;
8893b3a8eb9SGleb Smirnoff 			}
8903b3a8eb9SGleb Smirnoff 
8913b3a8eb9SGleb Smirnoff 			if ($9.match_tag)
8923b3a8eb9SGleb Smirnoff 				if (strlcpy(r.match_tagname, $9.match_tag,
8933b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
8943b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
8953b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
8963b3a8eb9SGleb Smirnoff 					YYERROR;
8973b3a8eb9SGleb Smirnoff 				}
8983b3a8eb9SGleb Smirnoff 			r.match_tag_not = $9.match_tag_not;
8993e248e0fSKristof Provost 			if ($9.marker & FOM_PRIO) {
9003e248e0fSKristof Provost 				if ($9.prio == 0)
9013e248e0fSKristof Provost 					r.prio = PF_PRIO_ZERO;
9023e248e0fSKristof Provost 				else
9033e248e0fSKristof Provost 					r.prio = $9.prio;
9043e248e0fSKristof Provost 			}
9053e248e0fSKristof Provost 			if ($9.marker & FOM_SETPRIO) {
9063e248e0fSKristof Provost 				r.set_prio[0] = $9.set_prio[0];
9073e248e0fSKristof Provost 				r.set_prio[1] = $9.set_prio[1];
9083e248e0fSKristof Provost 				r.scrub_flags |= PFSTATE_SETPRIO;
9093e248e0fSKristof Provost 			}
9103b3a8eb9SGleb Smirnoff 
9113b3a8eb9SGleb Smirnoff 			decide_address_family($8.src.host, &r.af);
9123b3a8eb9SGleb Smirnoff 			decide_address_family($8.dst.host, &r.af);
9133b3a8eb9SGleb Smirnoff 
9143b3a8eb9SGleb Smirnoff 			expand_rule(&r, $5, NULL, $7, $8.src_os,
9153b3a8eb9SGleb Smirnoff 			    $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
9163b3a8eb9SGleb Smirnoff 			    $9.uid, $9.gid, $9.icmpspec,
9173b3a8eb9SGleb Smirnoff 			    pf->astack[pf->asd + 1] ? pf->alast->name : $2);
9183b3a8eb9SGleb Smirnoff 			free($2);
9193b3a8eb9SGleb Smirnoff 			pf->astack[pf->asd + 1] = NULL;
9203b3a8eb9SGleb Smirnoff 		}
9213b3a8eb9SGleb Smirnoff 		| NATANCHOR string interface af proto fromto rtable {
9223b3a8eb9SGleb Smirnoff 			struct pf_rule	r;
9233b3a8eb9SGleb Smirnoff 
9243b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_NAT)) {
9253b3a8eb9SGleb Smirnoff 				free($2);
9263b3a8eb9SGleb Smirnoff 				YYERROR;
9273b3a8eb9SGleb Smirnoff 			}
9283b3a8eb9SGleb Smirnoff 
9293b3a8eb9SGleb Smirnoff 			memset(&r, 0, sizeof(r));
9303b3a8eb9SGleb Smirnoff 			r.action = PF_NAT;
9313b3a8eb9SGleb Smirnoff 			r.af = $4;
9323b3a8eb9SGleb Smirnoff 			r.rtableid = $7;
9333b3a8eb9SGleb Smirnoff 
9343b3a8eb9SGleb Smirnoff 			decide_address_family($6.src.host, &r.af);
9353b3a8eb9SGleb Smirnoff 			decide_address_family($6.dst.host, &r.af);
9363b3a8eb9SGleb Smirnoff 
9373b3a8eb9SGleb Smirnoff 			expand_rule(&r, $3, NULL, $5, $6.src_os,
9383b3a8eb9SGleb Smirnoff 			    $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
9393b3a8eb9SGleb Smirnoff 			    0, 0, 0, $2);
9403b3a8eb9SGleb Smirnoff 			free($2);
9413b3a8eb9SGleb Smirnoff 		}
9423b3a8eb9SGleb Smirnoff 		| RDRANCHOR string interface af proto fromto rtable {
9433b3a8eb9SGleb Smirnoff 			struct pf_rule	r;
9443b3a8eb9SGleb Smirnoff 
9453b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_NAT)) {
9463b3a8eb9SGleb Smirnoff 				free($2);
9473b3a8eb9SGleb Smirnoff 				YYERROR;
9483b3a8eb9SGleb Smirnoff 			}
9493b3a8eb9SGleb Smirnoff 
9503b3a8eb9SGleb Smirnoff 			memset(&r, 0, sizeof(r));
9513b3a8eb9SGleb Smirnoff 			r.action = PF_RDR;
9523b3a8eb9SGleb Smirnoff 			r.af = $4;
9533b3a8eb9SGleb Smirnoff 			r.rtableid = $7;
9543b3a8eb9SGleb Smirnoff 
9553b3a8eb9SGleb Smirnoff 			decide_address_family($6.src.host, &r.af);
9563b3a8eb9SGleb Smirnoff 			decide_address_family($6.dst.host, &r.af);
9573b3a8eb9SGleb Smirnoff 
9583b3a8eb9SGleb Smirnoff 			if ($6.src.port != NULL) {
9593b3a8eb9SGleb Smirnoff 				yyerror("source port parameter not supported"
9603b3a8eb9SGleb Smirnoff 				    " in rdr-anchor");
9613b3a8eb9SGleb Smirnoff 				YYERROR;
9623b3a8eb9SGleb Smirnoff 			}
9633b3a8eb9SGleb Smirnoff 			if ($6.dst.port != NULL) {
9643b3a8eb9SGleb Smirnoff 				if ($6.dst.port->next != NULL) {
9653b3a8eb9SGleb Smirnoff 					yyerror("destination port list "
9663b3a8eb9SGleb Smirnoff 					    "expansion not supported in "
9673b3a8eb9SGleb Smirnoff 					    "rdr-anchor");
9683b3a8eb9SGleb Smirnoff 					YYERROR;
9693b3a8eb9SGleb Smirnoff 				} else if ($6.dst.port->op != PF_OP_EQ) {
9703b3a8eb9SGleb Smirnoff 					yyerror("destination port operators"
9713b3a8eb9SGleb Smirnoff 					    " not supported in rdr-anchor");
9723b3a8eb9SGleb Smirnoff 					YYERROR;
9733b3a8eb9SGleb Smirnoff 				}
9743b3a8eb9SGleb Smirnoff 				r.dst.port[0] = $6.dst.port->port[0];
9753b3a8eb9SGleb Smirnoff 				r.dst.port[1] = $6.dst.port->port[1];
9763b3a8eb9SGleb Smirnoff 				r.dst.port_op = $6.dst.port->op;
9773b3a8eb9SGleb Smirnoff 			}
9783b3a8eb9SGleb Smirnoff 
9793b3a8eb9SGleb Smirnoff 			expand_rule(&r, $3, NULL, $5, $6.src_os,
9803b3a8eb9SGleb Smirnoff 			    $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
9813b3a8eb9SGleb Smirnoff 			    0, 0, 0, $2);
9823b3a8eb9SGleb Smirnoff 			free($2);
9833b3a8eb9SGleb Smirnoff 		}
9843b3a8eb9SGleb Smirnoff 		| BINATANCHOR string interface af proto fromto rtable {
9853b3a8eb9SGleb Smirnoff 			struct pf_rule	r;
9863b3a8eb9SGleb Smirnoff 
9873b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_NAT)) {
9883b3a8eb9SGleb Smirnoff 				free($2);
9893b3a8eb9SGleb Smirnoff 				YYERROR;
9903b3a8eb9SGleb Smirnoff 			}
9913b3a8eb9SGleb Smirnoff 
9923b3a8eb9SGleb Smirnoff 			memset(&r, 0, sizeof(r));
9933b3a8eb9SGleb Smirnoff 			r.action = PF_BINAT;
9943b3a8eb9SGleb Smirnoff 			r.af = $4;
9953b3a8eb9SGleb Smirnoff 			r.rtableid = $7;
9963b3a8eb9SGleb Smirnoff 			if ($5 != NULL) {
9973b3a8eb9SGleb Smirnoff 				if ($5->next != NULL) {
9983b3a8eb9SGleb Smirnoff 					yyerror("proto list expansion"
9993b3a8eb9SGleb Smirnoff 					    " not supported in binat-anchor");
10003b3a8eb9SGleb Smirnoff 					YYERROR;
10013b3a8eb9SGleb Smirnoff 				}
10023b3a8eb9SGleb Smirnoff 				r.proto = $5->proto;
10033b3a8eb9SGleb Smirnoff 				free($5);
10043b3a8eb9SGleb Smirnoff 			}
10053b3a8eb9SGleb Smirnoff 
10063b3a8eb9SGleb Smirnoff 			if ($6.src.host != NULL || $6.src.port != NULL ||
10073b3a8eb9SGleb Smirnoff 			    $6.dst.host != NULL || $6.dst.port != NULL) {
10083b3a8eb9SGleb Smirnoff 				yyerror("fromto parameter not supported"
10093b3a8eb9SGleb Smirnoff 				    " in binat-anchor");
10103b3a8eb9SGleb Smirnoff 				YYERROR;
10113b3a8eb9SGleb Smirnoff 			}
10123b3a8eb9SGleb Smirnoff 
10133b3a8eb9SGleb Smirnoff 			decide_address_family($6.src.host, &r.af);
10143b3a8eb9SGleb Smirnoff 			decide_address_family($6.dst.host, &r.af);
10153b3a8eb9SGleb Smirnoff 
10163b3a8eb9SGleb Smirnoff 			pfctl_add_rule(pf, &r, $2);
10173b3a8eb9SGleb Smirnoff 			free($2);
10183b3a8eb9SGleb Smirnoff 		}
10193b3a8eb9SGleb Smirnoff 		;
10203b3a8eb9SGleb Smirnoff 
10213b3a8eb9SGleb Smirnoff loadrule	: LOAD ANCHOR string FROM string	{
10223b3a8eb9SGleb Smirnoff 			struct loadanchors	*loadanchor;
10233b3a8eb9SGleb Smirnoff 
10243b3a8eb9SGleb Smirnoff 			if (strlen(pf->anchor->name) + 1 +
10253b3a8eb9SGleb Smirnoff 			    strlen($3) >= MAXPATHLEN) {
10263b3a8eb9SGleb Smirnoff 				yyerror("anchorname %s too long, max %u\n",
10273b3a8eb9SGleb Smirnoff 				    $3, MAXPATHLEN - 1);
10283b3a8eb9SGleb Smirnoff 				free($3);
10293b3a8eb9SGleb Smirnoff 				YYERROR;
10303b3a8eb9SGleb Smirnoff 			}
10313b3a8eb9SGleb Smirnoff 			loadanchor = calloc(1, sizeof(struct loadanchors));
10323b3a8eb9SGleb Smirnoff 			if (loadanchor == NULL)
10333b3a8eb9SGleb Smirnoff 				err(1, "loadrule: calloc");
10343b3a8eb9SGleb Smirnoff 			if ((loadanchor->anchorname = malloc(MAXPATHLEN)) ==
10353b3a8eb9SGleb Smirnoff 			    NULL)
10363b3a8eb9SGleb Smirnoff 				err(1, "loadrule: malloc");
10373b3a8eb9SGleb Smirnoff 			if (pf->anchor->name[0])
10383b3a8eb9SGleb Smirnoff 				snprintf(loadanchor->anchorname, MAXPATHLEN,
10393b3a8eb9SGleb Smirnoff 				    "%s/%s", pf->anchor->name, $3);
10403b3a8eb9SGleb Smirnoff 			else
10413b3a8eb9SGleb Smirnoff 				strlcpy(loadanchor->anchorname, $3, MAXPATHLEN);
10423b3a8eb9SGleb Smirnoff 			if ((loadanchor->filename = strdup($5)) == NULL)
10433b3a8eb9SGleb Smirnoff 				err(1, "loadrule: strdup");
10443b3a8eb9SGleb Smirnoff 
10453b3a8eb9SGleb Smirnoff 			TAILQ_INSERT_TAIL(&loadanchorshead, loadanchor,
10463b3a8eb9SGleb Smirnoff 			    entries);
10473b3a8eb9SGleb Smirnoff 
10483b3a8eb9SGleb Smirnoff 			free($3);
10493b3a8eb9SGleb Smirnoff 			free($5);
10503b3a8eb9SGleb Smirnoff 		};
10513b3a8eb9SGleb Smirnoff 
10523b3a8eb9SGleb Smirnoff scrubaction	: no SCRUB {
10533b3a8eb9SGleb Smirnoff 			$$.b2 = $$.w = 0;
10543b3a8eb9SGleb Smirnoff 			if ($1)
10553b3a8eb9SGleb Smirnoff 				$$.b1 = PF_NOSCRUB;
10563b3a8eb9SGleb Smirnoff 			else
10573b3a8eb9SGleb Smirnoff 				$$.b1 = PF_SCRUB;
10583b3a8eb9SGleb Smirnoff 		}
10593b3a8eb9SGleb Smirnoff 		;
10603b3a8eb9SGleb Smirnoff 
10613b3a8eb9SGleb Smirnoff scrubrule	: scrubaction dir logquick interface af proto fromto scrub_opts
10623b3a8eb9SGleb Smirnoff 		{
10633b3a8eb9SGleb Smirnoff 			struct pf_rule	r;
10643b3a8eb9SGleb Smirnoff 
10653b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_SCRUB))
10663b3a8eb9SGleb Smirnoff 				YYERROR;
10673b3a8eb9SGleb Smirnoff 
10683b3a8eb9SGleb Smirnoff 			memset(&r, 0, sizeof(r));
10693b3a8eb9SGleb Smirnoff 
10703b3a8eb9SGleb Smirnoff 			r.action = $1.b1;
10713b3a8eb9SGleb Smirnoff 			r.direction = $2;
10723b3a8eb9SGleb Smirnoff 
10733b3a8eb9SGleb Smirnoff 			r.log = $3.log;
10743b3a8eb9SGleb Smirnoff 			r.logif = $3.logif;
10753b3a8eb9SGleb Smirnoff 			if ($3.quick) {
10763b3a8eb9SGleb Smirnoff 				yyerror("scrub rules do not support 'quick'");
10773b3a8eb9SGleb Smirnoff 				YYERROR;
10783b3a8eb9SGleb Smirnoff 			}
10793b3a8eb9SGleb Smirnoff 
10803b3a8eb9SGleb Smirnoff 			r.af = $5;
10813b3a8eb9SGleb Smirnoff 			if ($8.nodf)
10823b3a8eb9SGleb Smirnoff 				r.rule_flag |= PFRULE_NODF;
10833b3a8eb9SGleb Smirnoff 			if ($8.randomid)
10843b3a8eb9SGleb Smirnoff 				r.rule_flag |= PFRULE_RANDOMID;
10853b3a8eb9SGleb Smirnoff 			if ($8.reassemble_tcp) {
10863b3a8eb9SGleb Smirnoff 				if (r.direction != PF_INOUT) {
10873b3a8eb9SGleb Smirnoff 					yyerror("reassemble tcp rules can not "
10883b3a8eb9SGleb Smirnoff 					    "specify direction");
10893b3a8eb9SGleb Smirnoff 					YYERROR;
10903b3a8eb9SGleb Smirnoff 				}
10913b3a8eb9SGleb Smirnoff 				r.rule_flag |= PFRULE_REASSEMBLE_TCP;
10923b3a8eb9SGleb Smirnoff 			}
10933b3a8eb9SGleb Smirnoff 			if ($8.minttl)
10943b3a8eb9SGleb Smirnoff 				r.min_ttl = $8.minttl;
10953b3a8eb9SGleb Smirnoff 			if ($8.maxmss)
10963b3a8eb9SGleb Smirnoff 				r.max_mss = $8.maxmss;
10973b3a8eb9SGleb Smirnoff 			if ($8.marker & SOM_SETTOS) {
10983b3a8eb9SGleb Smirnoff 				r.rule_flag |= PFRULE_SET_TOS;
10993b3a8eb9SGleb Smirnoff 				r.set_tos = $8.settos;
11003b3a8eb9SGleb Smirnoff 			}
11013b3a8eb9SGleb Smirnoff 			if ($8.fragcache)
11023b3a8eb9SGleb Smirnoff 				r.rule_flag |= $8.fragcache;
11033b3a8eb9SGleb Smirnoff 			if ($8.match_tag)
11043b3a8eb9SGleb Smirnoff 				if (strlcpy(r.match_tagname, $8.match_tag,
11053b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
11063b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
11073b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
11083b3a8eb9SGleb Smirnoff 					YYERROR;
11093b3a8eb9SGleb Smirnoff 				}
11103b3a8eb9SGleb Smirnoff 			r.match_tag_not = $8.match_tag_not;
11113b3a8eb9SGleb Smirnoff 			r.rtableid = $8.rtableid;
11123b3a8eb9SGleb Smirnoff 
11133b3a8eb9SGleb Smirnoff 			expand_rule(&r, $4, NULL, $6, $7.src_os,
11143b3a8eb9SGleb Smirnoff 			    $7.src.host, $7.src.port, $7.dst.host, $7.dst.port,
11153b3a8eb9SGleb Smirnoff 			    NULL, NULL, NULL, "");
11163b3a8eb9SGleb Smirnoff 		}
11173b3a8eb9SGleb Smirnoff 		;
11183b3a8eb9SGleb Smirnoff 
11193b3a8eb9SGleb Smirnoff scrub_opts	:	{
11203b3a8eb9SGleb Smirnoff 				bzero(&scrub_opts, sizeof scrub_opts);
11213b3a8eb9SGleb Smirnoff 				scrub_opts.rtableid = -1;
11223b3a8eb9SGleb Smirnoff 			}
11233b3a8eb9SGleb Smirnoff 		    scrub_opts_l
11243b3a8eb9SGleb Smirnoff 			{ $$ = scrub_opts; }
11253b3a8eb9SGleb Smirnoff 		| /* empty */ {
11263b3a8eb9SGleb Smirnoff 			bzero(&scrub_opts, sizeof scrub_opts);
11273b3a8eb9SGleb Smirnoff 			scrub_opts.rtableid = -1;
11283b3a8eb9SGleb Smirnoff 			$$ = scrub_opts;
11293b3a8eb9SGleb Smirnoff 		}
11303b3a8eb9SGleb Smirnoff 		;
11313b3a8eb9SGleb Smirnoff 
11323b3a8eb9SGleb Smirnoff scrub_opts_l	: scrub_opts_l scrub_opt
11333b3a8eb9SGleb Smirnoff 		| scrub_opt
11343b3a8eb9SGleb Smirnoff 		;
11353b3a8eb9SGleb Smirnoff 
11363b3a8eb9SGleb Smirnoff scrub_opt	: NODF	{
11373b3a8eb9SGleb Smirnoff 			if (scrub_opts.nodf) {
11383b3a8eb9SGleb Smirnoff 				yyerror("no-df cannot be respecified");
11393b3a8eb9SGleb Smirnoff 				YYERROR;
11403b3a8eb9SGleb Smirnoff 			}
11413b3a8eb9SGleb Smirnoff 			scrub_opts.nodf = 1;
11423b3a8eb9SGleb Smirnoff 		}
11433b3a8eb9SGleb Smirnoff 		| MINTTL NUMBER {
11443b3a8eb9SGleb Smirnoff 			if (scrub_opts.marker & SOM_MINTTL) {
11453b3a8eb9SGleb Smirnoff 				yyerror("min-ttl cannot be respecified");
11463b3a8eb9SGleb Smirnoff 				YYERROR;
11473b3a8eb9SGleb Smirnoff 			}
11483b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > 255) {
11493b3a8eb9SGleb Smirnoff 				yyerror("illegal min-ttl value %d", $2);
11503b3a8eb9SGleb Smirnoff 				YYERROR;
11513b3a8eb9SGleb Smirnoff 			}
11523b3a8eb9SGleb Smirnoff 			scrub_opts.marker |= SOM_MINTTL;
11533b3a8eb9SGleb Smirnoff 			scrub_opts.minttl = $2;
11543b3a8eb9SGleb Smirnoff 		}
11553b3a8eb9SGleb Smirnoff 		| MAXMSS NUMBER {
11563b3a8eb9SGleb Smirnoff 			if (scrub_opts.marker & SOM_MAXMSS) {
11573b3a8eb9SGleb Smirnoff 				yyerror("max-mss cannot be respecified");
11583b3a8eb9SGleb Smirnoff 				YYERROR;
11593b3a8eb9SGleb Smirnoff 			}
11603b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > 65535) {
11613b3a8eb9SGleb Smirnoff 				yyerror("illegal max-mss value %d", $2);
11623b3a8eb9SGleb Smirnoff 				YYERROR;
11633b3a8eb9SGleb Smirnoff 			}
11643b3a8eb9SGleb Smirnoff 			scrub_opts.marker |= SOM_MAXMSS;
11653b3a8eb9SGleb Smirnoff 			scrub_opts.maxmss = $2;
11663b3a8eb9SGleb Smirnoff 		}
11673b3a8eb9SGleb Smirnoff 		| SETTOS tos {
11683b3a8eb9SGleb Smirnoff 			if (scrub_opts.marker & SOM_SETTOS) {
11693b3a8eb9SGleb Smirnoff 				yyerror("set-tos cannot be respecified");
11703b3a8eb9SGleb Smirnoff 				YYERROR;
11713b3a8eb9SGleb Smirnoff 			}
11723b3a8eb9SGleb Smirnoff 			scrub_opts.marker |= SOM_SETTOS;
11733b3a8eb9SGleb Smirnoff 			scrub_opts.settos = $2;
11743b3a8eb9SGleb Smirnoff 		}
11753b3a8eb9SGleb Smirnoff 		| fragcache {
11763b3a8eb9SGleb Smirnoff 			if (scrub_opts.marker & SOM_FRAGCACHE) {
11773b3a8eb9SGleb Smirnoff 				yyerror("fragcache cannot be respecified");
11783b3a8eb9SGleb Smirnoff 				YYERROR;
11793b3a8eb9SGleb Smirnoff 			}
11803b3a8eb9SGleb Smirnoff 			scrub_opts.marker |= SOM_FRAGCACHE;
11813b3a8eb9SGleb Smirnoff 			scrub_opts.fragcache = $1;
11823b3a8eb9SGleb Smirnoff 		}
11833b3a8eb9SGleb Smirnoff 		| REASSEMBLE STRING {
11843b3a8eb9SGleb Smirnoff 			if (strcasecmp($2, "tcp") != 0) {
11853b3a8eb9SGleb Smirnoff 				yyerror("scrub reassemble supports only tcp, "
11863b3a8eb9SGleb Smirnoff 				    "not '%s'", $2);
11873b3a8eb9SGleb Smirnoff 				free($2);
11883b3a8eb9SGleb Smirnoff 				YYERROR;
11893b3a8eb9SGleb Smirnoff 			}
11903b3a8eb9SGleb Smirnoff 			free($2);
11913b3a8eb9SGleb Smirnoff 			if (scrub_opts.reassemble_tcp) {
11923b3a8eb9SGleb Smirnoff 				yyerror("reassemble tcp cannot be respecified");
11933b3a8eb9SGleb Smirnoff 				YYERROR;
11943b3a8eb9SGleb Smirnoff 			}
11953b3a8eb9SGleb Smirnoff 			scrub_opts.reassemble_tcp = 1;
11963b3a8eb9SGleb Smirnoff 		}
11973b3a8eb9SGleb Smirnoff 		| RANDOMID {
11983b3a8eb9SGleb Smirnoff 			if (scrub_opts.randomid) {
11993b3a8eb9SGleb Smirnoff 				yyerror("random-id cannot be respecified");
12003b3a8eb9SGleb Smirnoff 				YYERROR;
12013b3a8eb9SGleb Smirnoff 			}
12023b3a8eb9SGleb Smirnoff 			scrub_opts.randomid = 1;
12033b3a8eb9SGleb Smirnoff 		}
12043b3a8eb9SGleb Smirnoff 		| RTABLE NUMBER				{
12053b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > rt_tableid_max()) {
12063b3a8eb9SGleb Smirnoff 				yyerror("invalid rtable id");
12073b3a8eb9SGleb Smirnoff 				YYERROR;
12083b3a8eb9SGleb Smirnoff 			}
12093b3a8eb9SGleb Smirnoff 			scrub_opts.rtableid = $2;
12103b3a8eb9SGleb Smirnoff 		}
12113b3a8eb9SGleb Smirnoff 		| not TAGGED string			{
12123b3a8eb9SGleb Smirnoff 			scrub_opts.match_tag = $3;
12133b3a8eb9SGleb Smirnoff 			scrub_opts.match_tag_not = $1;
12143b3a8eb9SGleb Smirnoff 		}
12153b3a8eb9SGleb Smirnoff 		;
12163b3a8eb9SGleb Smirnoff 
12173b3a8eb9SGleb Smirnoff fragcache	: FRAGMENT REASSEMBLE	{ $$ = 0; /* default */ }
121864b3b4d6SKristof Provost 		| FRAGMENT FRAGCROP	{ $$ = 0; }
121964b3b4d6SKristof Provost 		| FRAGMENT FRAGDROP	{ $$ = 0; }
12203b3a8eb9SGleb Smirnoff 		;
12213b3a8eb9SGleb Smirnoff 
12223b3a8eb9SGleb Smirnoff antispoof	: ANTISPOOF logquick antispoof_ifspc af antispoof_opts {
12233b3a8eb9SGleb Smirnoff 			struct pf_rule		 r;
12243b3a8eb9SGleb Smirnoff 			struct node_host	*h = NULL, *hh;
12253b3a8eb9SGleb Smirnoff 			struct node_if		*i, *j;
12263b3a8eb9SGleb Smirnoff 
12273b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_FILTER))
12283b3a8eb9SGleb Smirnoff 				YYERROR;
12293b3a8eb9SGleb Smirnoff 
12303b3a8eb9SGleb Smirnoff 			for (i = $3; i; i = i->next) {
12313b3a8eb9SGleb Smirnoff 				bzero(&r, sizeof(r));
12323b3a8eb9SGleb Smirnoff 
12333b3a8eb9SGleb Smirnoff 				r.action = PF_DROP;
12343b3a8eb9SGleb Smirnoff 				r.direction = PF_IN;
12353b3a8eb9SGleb Smirnoff 				r.log = $2.log;
12363b3a8eb9SGleb Smirnoff 				r.logif = $2.logif;
12373b3a8eb9SGleb Smirnoff 				r.quick = $2.quick;
12383b3a8eb9SGleb Smirnoff 				r.af = $4;
12393b3a8eb9SGleb Smirnoff 				if (rule_label(&r, $5.label))
12403b3a8eb9SGleb Smirnoff 					YYERROR;
12413b3a8eb9SGleb Smirnoff 				r.rtableid = $5.rtableid;
12423b3a8eb9SGleb Smirnoff 				j = calloc(1, sizeof(struct node_if));
12433b3a8eb9SGleb Smirnoff 				if (j == NULL)
12443b3a8eb9SGleb Smirnoff 					err(1, "antispoof: calloc");
12453b3a8eb9SGleb Smirnoff 				if (strlcpy(j->ifname, i->ifname,
12463b3a8eb9SGleb Smirnoff 				    sizeof(j->ifname)) >= sizeof(j->ifname)) {
12473b3a8eb9SGleb Smirnoff 					free(j);
12483b3a8eb9SGleb Smirnoff 					yyerror("interface name too long");
12493b3a8eb9SGleb Smirnoff 					YYERROR;
12503b3a8eb9SGleb Smirnoff 				}
12513b3a8eb9SGleb Smirnoff 				j->not = 1;
12523b3a8eb9SGleb Smirnoff 				if (i->dynamic) {
12533b3a8eb9SGleb Smirnoff 					h = calloc(1, sizeof(*h));
12543b3a8eb9SGleb Smirnoff 					if (h == NULL)
12553b3a8eb9SGleb Smirnoff 						err(1, "address: calloc");
12563b3a8eb9SGleb Smirnoff 					h->addr.type = PF_ADDR_DYNIFTL;
12573b3a8eb9SGleb Smirnoff 					set_ipmask(h, 128);
12583b3a8eb9SGleb Smirnoff 					if (strlcpy(h->addr.v.ifname, i->ifname,
12593b3a8eb9SGleb Smirnoff 					    sizeof(h->addr.v.ifname)) >=
12603b3a8eb9SGleb Smirnoff 					    sizeof(h->addr.v.ifname)) {
12613b3a8eb9SGleb Smirnoff 						free(h);
12623b3a8eb9SGleb Smirnoff 						yyerror(
12633b3a8eb9SGleb Smirnoff 						    "interface name too long");
12643b3a8eb9SGleb Smirnoff 						YYERROR;
12653b3a8eb9SGleb Smirnoff 					}
12663b3a8eb9SGleb Smirnoff 					hh = malloc(sizeof(*hh));
12673b3a8eb9SGleb Smirnoff 					if (hh == NULL)
12683b3a8eb9SGleb Smirnoff 						 err(1, "address: malloc");
12693b3a8eb9SGleb Smirnoff 					bcopy(h, hh, sizeof(*hh));
12703b3a8eb9SGleb Smirnoff 					h->addr.iflags = PFI_AFLAG_NETWORK;
12713b3a8eb9SGleb Smirnoff 				} else {
12723b3a8eb9SGleb Smirnoff 					h = ifa_lookup(j->ifname,
12733b3a8eb9SGleb Smirnoff 					    PFI_AFLAG_NETWORK);
12743b3a8eb9SGleb Smirnoff 					hh = NULL;
12753b3a8eb9SGleb Smirnoff 				}
12763b3a8eb9SGleb Smirnoff 
12773b3a8eb9SGleb Smirnoff 				if (h != NULL)
12783b3a8eb9SGleb Smirnoff 					expand_rule(&r, j, NULL, NULL, NULL, h,
12793b3a8eb9SGleb Smirnoff 					    NULL, NULL, NULL, NULL, NULL,
12803b3a8eb9SGleb Smirnoff 					    NULL, "");
12813b3a8eb9SGleb Smirnoff 
12823b3a8eb9SGleb Smirnoff 				if ((i->ifa_flags & IFF_LOOPBACK) == 0) {
12833b3a8eb9SGleb Smirnoff 					bzero(&r, sizeof(r));
12843b3a8eb9SGleb Smirnoff 
12853b3a8eb9SGleb Smirnoff 					r.action = PF_DROP;
12863b3a8eb9SGleb Smirnoff 					r.direction = PF_IN;
12873b3a8eb9SGleb Smirnoff 					r.log = $2.log;
12883b3a8eb9SGleb Smirnoff 					r.logif = $2.logif;
12893b3a8eb9SGleb Smirnoff 					r.quick = $2.quick;
12903b3a8eb9SGleb Smirnoff 					r.af = $4;
12913b3a8eb9SGleb Smirnoff 					if (rule_label(&r, $5.label))
12923b3a8eb9SGleb Smirnoff 						YYERROR;
12933b3a8eb9SGleb Smirnoff 					r.rtableid = $5.rtableid;
12943b3a8eb9SGleb Smirnoff 					if (hh != NULL)
12953b3a8eb9SGleb Smirnoff 						h = hh;
12963b3a8eb9SGleb Smirnoff 					else
12973b3a8eb9SGleb Smirnoff 						h = ifa_lookup(i->ifname, 0);
12983b3a8eb9SGleb Smirnoff 					if (h != NULL)
12993b3a8eb9SGleb Smirnoff 						expand_rule(&r, NULL, NULL,
13003b3a8eb9SGleb Smirnoff 						    NULL, NULL, h, NULL, NULL,
13013b3a8eb9SGleb Smirnoff 						    NULL, NULL, NULL, NULL, "");
13023b3a8eb9SGleb Smirnoff 				} else
13033b3a8eb9SGleb Smirnoff 					free(hh);
13043b3a8eb9SGleb Smirnoff 			}
13053b3a8eb9SGleb Smirnoff 			free($5.label);
13063b3a8eb9SGleb Smirnoff 		}
13073b3a8eb9SGleb Smirnoff 		;
13083b3a8eb9SGleb Smirnoff 
13093b3a8eb9SGleb Smirnoff antispoof_ifspc	: FOR antispoof_if			{ $$ = $2; }
13103b3a8eb9SGleb Smirnoff 		| FOR '{' optnl antispoof_iflst '}'	{ $$ = $4; }
13113b3a8eb9SGleb Smirnoff 		;
13123b3a8eb9SGleb Smirnoff 
13133b3a8eb9SGleb Smirnoff antispoof_iflst	: antispoof_if optnl			{ $$ = $1; }
13143b3a8eb9SGleb Smirnoff 		| antispoof_iflst comma antispoof_if optnl {
13153b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
13163b3a8eb9SGleb Smirnoff 			$1->tail = $3;
13173b3a8eb9SGleb Smirnoff 			$$ = $1;
13183b3a8eb9SGleb Smirnoff 		}
13193b3a8eb9SGleb Smirnoff 		;
13203b3a8eb9SGleb Smirnoff 
13213b3a8eb9SGleb Smirnoff antispoof_if	: if_item				{ $$ = $1; }
13223b3a8eb9SGleb Smirnoff 		| '(' if_item ')'			{
13233b3a8eb9SGleb Smirnoff 			$2->dynamic = 1;
13243b3a8eb9SGleb Smirnoff 			$$ = $2;
13253b3a8eb9SGleb Smirnoff 		}
13263b3a8eb9SGleb Smirnoff 		;
13273b3a8eb9SGleb Smirnoff 
13283b3a8eb9SGleb Smirnoff antispoof_opts	:	{
13293b3a8eb9SGleb Smirnoff 				bzero(&antispoof_opts, sizeof antispoof_opts);
13303b3a8eb9SGleb Smirnoff 				antispoof_opts.rtableid = -1;
13313b3a8eb9SGleb Smirnoff 			}
13323b3a8eb9SGleb Smirnoff 		    antispoof_opts_l
13333b3a8eb9SGleb Smirnoff 			{ $$ = antispoof_opts; }
13343b3a8eb9SGleb Smirnoff 		| /* empty */	{
13353b3a8eb9SGleb Smirnoff 			bzero(&antispoof_opts, sizeof antispoof_opts);
13363b3a8eb9SGleb Smirnoff 			antispoof_opts.rtableid = -1;
13373b3a8eb9SGleb Smirnoff 			$$ = antispoof_opts;
13383b3a8eb9SGleb Smirnoff 		}
13393b3a8eb9SGleb Smirnoff 		;
13403b3a8eb9SGleb Smirnoff 
13413b3a8eb9SGleb Smirnoff antispoof_opts_l	: antispoof_opts_l antispoof_opt
13423b3a8eb9SGleb Smirnoff 			| antispoof_opt
13433b3a8eb9SGleb Smirnoff 			;
13443b3a8eb9SGleb Smirnoff 
13453b3a8eb9SGleb Smirnoff antispoof_opt	: label	{
13463b3a8eb9SGleb Smirnoff 			if (antispoof_opts.label) {
13473b3a8eb9SGleb Smirnoff 				yyerror("label cannot be redefined");
13483b3a8eb9SGleb Smirnoff 				YYERROR;
13493b3a8eb9SGleb Smirnoff 			}
13503b3a8eb9SGleb Smirnoff 			antispoof_opts.label = $1;
13513b3a8eb9SGleb Smirnoff 		}
13523b3a8eb9SGleb Smirnoff 		| RTABLE NUMBER				{
13533b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > rt_tableid_max()) {
13543b3a8eb9SGleb Smirnoff 				yyerror("invalid rtable id");
13553b3a8eb9SGleb Smirnoff 				YYERROR;
13563b3a8eb9SGleb Smirnoff 			}
13573b3a8eb9SGleb Smirnoff 			antispoof_opts.rtableid = $2;
13583b3a8eb9SGleb Smirnoff 		}
13593b3a8eb9SGleb Smirnoff 		;
13603b3a8eb9SGleb Smirnoff 
13613b3a8eb9SGleb Smirnoff not		: '!'		{ $$ = 1; }
13623b3a8eb9SGleb Smirnoff 		| /* empty */	{ $$ = 0; }
13633b3a8eb9SGleb Smirnoff 		;
13643b3a8eb9SGleb Smirnoff 
13653b3a8eb9SGleb Smirnoff tabledef	: TABLE '<' STRING '>' table_opts {
13663b3a8eb9SGleb Smirnoff 			struct node_host	 *h, *nh;
13673b3a8eb9SGleb Smirnoff 			struct node_tinit	 *ti, *nti;
13683b3a8eb9SGleb Smirnoff 
13693b3a8eb9SGleb Smirnoff 			if (strlen($3) >= PF_TABLE_NAME_SIZE) {
13703b3a8eb9SGleb Smirnoff 				yyerror("table name too long, max %d chars",
13713b3a8eb9SGleb Smirnoff 				    PF_TABLE_NAME_SIZE - 1);
13723b3a8eb9SGleb Smirnoff 				free($3);
13733b3a8eb9SGleb Smirnoff 				YYERROR;
13743b3a8eb9SGleb Smirnoff 			}
13753b3a8eb9SGleb Smirnoff 			if (pf->loadopt & PFCTL_FLAG_TABLE)
13763b3a8eb9SGleb Smirnoff 				if (process_tabledef($3, &$5)) {
13773b3a8eb9SGleb Smirnoff 					free($3);
13783b3a8eb9SGleb Smirnoff 					YYERROR;
13793b3a8eb9SGleb Smirnoff 				}
13803b3a8eb9SGleb Smirnoff 			free($3);
13813b3a8eb9SGleb Smirnoff 			for (ti = SIMPLEQ_FIRST(&$5.init_nodes);
13823b3a8eb9SGleb Smirnoff 			    ti != SIMPLEQ_END(&$5.init_nodes); ti = nti) {
13833b3a8eb9SGleb Smirnoff 				if (ti->file)
13843b3a8eb9SGleb Smirnoff 					free(ti->file);
13853b3a8eb9SGleb Smirnoff 				for (h = ti->host; h != NULL; h = nh) {
13863b3a8eb9SGleb Smirnoff 					nh = h->next;
13873b3a8eb9SGleb Smirnoff 					free(h);
13883b3a8eb9SGleb Smirnoff 				}
13893b3a8eb9SGleb Smirnoff 				nti = SIMPLEQ_NEXT(ti, entries);
13903b3a8eb9SGleb Smirnoff 				free(ti);
13913b3a8eb9SGleb Smirnoff 			}
13923b3a8eb9SGleb Smirnoff 		}
13933b3a8eb9SGleb Smirnoff 		;
13943b3a8eb9SGleb Smirnoff 
13953b3a8eb9SGleb Smirnoff table_opts	:	{
13963b3a8eb9SGleb Smirnoff 			bzero(&table_opts, sizeof table_opts);
13973b3a8eb9SGleb Smirnoff 			SIMPLEQ_INIT(&table_opts.init_nodes);
13983b3a8eb9SGleb Smirnoff 		}
13993b3a8eb9SGleb Smirnoff 		    table_opts_l
14003b3a8eb9SGleb Smirnoff 			{ $$ = table_opts; }
14013b3a8eb9SGleb Smirnoff 		| /* empty */
14023b3a8eb9SGleb Smirnoff 			{
14033b3a8eb9SGleb Smirnoff 			bzero(&table_opts, sizeof table_opts);
14043b3a8eb9SGleb Smirnoff 			SIMPLEQ_INIT(&table_opts.init_nodes);
14053b3a8eb9SGleb Smirnoff 			$$ = table_opts;
14063b3a8eb9SGleb Smirnoff 		}
14073b3a8eb9SGleb Smirnoff 		;
14083b3a8eb9SGleb Smirnoff 
14093b3a8eb9SGleb Smirnoff table_opts_l	: table_opts_l table_opt
14103b3a8eb9SGleb Smirnoff 		| table_opt
14113b3a8eb9SGleb Smirnoff 		;
14123b3a8eb9SGleb Smirnoff 
14133b3a8eb9SGleb Smirnoff table_opt	: STRING		{
14143b3a8eb9SGleb Smirnoff 			if (!strcmp($1, "const"))
14153b3a8eb9SGleb Smirnoff 				table_opts.flags |= PFR_TFLAG_CONST;
14163b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "persist"))
14173b3a8eb9SGleb Smirnoff 				table_opts.flags |= PFR_TFLAG_PERSIST;
14183b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "counters"))
14193b3a8eb9SGleb Smirnoff 				table_opts.flags |= PFR_TFLAG_COUNTERS;
14203b3a8eb9SGleb Smirnoff 			else {
14213b3a8eb9SGleb Smirnoff 				yyerror("invalid table option '%s'", $1);
14223b3a8eb9SGleb Smirnoff 				free($1);
14233b3a8eb9SGleb Smirnoff 				YYERROR;
14243b3a8eb9SGleb Smirnoff 			}
14253b3a8eb9SGleb Smirnoff 			free($1);
14263b3a8eb9SGleb Smirnoff 		}
14273b3a8eb9SGleb Smirnoff 		| '{' optnl '}'		{ table_opts.init_addr = 1; }
14283b3a8eb9SGleb Smirnoff 		| '{' optnl host_list '}'	{
14293b3a8eb9SGleb Smirnoff 			struct node_host	*n;
14303b3a8eb9SGleb Smirnoff 			struct node_tinit	*ti;
14313b3a8eb9SGleb Smirnoff 
14323b3a8eb9SGleb Smirnoff 			for (n = $3; n != NULL; n = n->next) {
14333b3a8eb9SGleb Smirnoff 				switch (n->addr.type) {
14343b3a8eb9SGleb Smirnoff 				case PF_ADDR_ADDRMASK:
14353b3a8eb9SGleb Smirnoff 					continue; /* ok */
14363b3a8eb9SGleb Smirnoff 				case PF_ADDR_RANGE:
14373b3a8eb9SGleb Smirnoff 					yyerror("address ranges are not "
14383b3a8eb9SGleb Smirnoff 					    "permitted inside tables");
14393b3a8eb9SGleb Smirnoff 					break;
14403b3a8eb9SGleb Smirnoff 				case PF_ADDR_DYNIFTL:
14413b3a8eb9SGleb Smirnoff 					yyerror("dynamic addresses are not "
14423b3a8eb9SGleb Smirnoff 					    "permitted inside tables");
14433b3a8eb9SGleb Smirnoff 					break;
14443b3a8eb9SGleb Smirnoff 				case PF_ADDR_TABLE:
14453b3a8eb9SGleb Smirnoff 					yyerror("tables cannot contain tables");
14463b3a8eb9SGleb Smirnoff 					break;
14473b3a8eb9SGleb Smirnoff 				case PF_ADDR_NOROUTE:
14483b3a8eb9SGleb Smirnoff 					yyerror("\"no-route\" is not permitted "
14493b3a8eb9SGleb Smirnoff 					    "inside tables");
14503b3a8eb9SGleb Smirnoff 					break;
14513b3a8eb9SGleb Smirnoff 				case PF_ADDR_URPFFAILED:
14523b3a8eb9SGleb Smirnoff 					yyerror("\"urpf-failed\" is not "
14533b3a8eb9SGleb Smirnoff 					    "permitted inside tables");
14543b3a8eb9SGleb Smirnoff 					break;
14553b3a8eb9SGleb Smirnoff 				default:
14563b3a8eb9SGleb Smirnoff 					yyerror("unknown address type %d",
14573b3a8eb9SGleb Smirnoff 					    n->addr.type);
14583b3a8eb9SGleb Smirnoff 				}
14593b3a8eb9SGleb Smirnoff 				YYERROR;
14603b3a8eb9SGleb Smirnoff 			}
14613b3a8eb9SGleb Smirnoff 			if (!(ti = calloc(1, sizeof(*ti))))
14623b3a8eb9SGleb Smirnoff 				err(1, "table_opt: calloc");
14633b3a8eb9SGleb Smirnoff 			ti->host = $3;
14643b3a8eb9SGleb Smirnoff 			SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
14653b3a8eb9SGleb Smirnoff 			    entries);
14663b3a8eb9SGleb Smirnoff 			table_opts.init_addr = 1;
14673b3a8eb9SGleb Smirnoff 		}
14683b3a8eb9SGleb Smirnoff 		| FILENAME STRING	{
14693b3a8eb9SGleb Smirnoff 			struct node_tinit	*ti;
14703b3a8eb9SGleb Smirnoff 
14713b3a8eb9SGleb Smirnoff 			if (!(ti = calloc(1, sizeof(*ti))))
14723b3a8eb9SGleb Smirnoff 				err(1, "table_opt: calloc");
14733b3a8eb9SGleb Smirnoff 			ti->file = $2;
14743b3a8eb9SGleb Smirnoff 			SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
14753b3a8eb9SGleb Smirnoff 			    entries);
14763b3a8eb9SGleb Smirnoff 			table_opts.init_addr = 1;
14773b3a8eb9SGleb Smirnoff 		}
14783b3a8eb9SGleb Smirnoff 		;
14793b3a8eb9SGleb Smirnoff 
14803b3a8eb9SGleb Smirnoff altqif		: ALTQ interface queue_opts QUEUE qassign {
14813b3a8eb9SGleb Smirnoff 			struct pf_altq	a;
14823b3a8eb9SGleb Smirnoff 
14833b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_QUEUE))
14843b3a8eb9SGleb Smirnoff 				YYERROR;
14853b3a8eb9SGleb Smirnoff 
14863b3a8eb9SGleb Smirnoff 			memset(&a, 0, sizeof(a));
14873b3a8eb9SGleb Smirnoff 			if ($3.scheduler.qtype == ALTQT_NONE) {
14883b3a8eb9SGleb Smirnoff 				yyerror("no scheduler specified!");
14893b3a8eb9SGleb Smirnoff 				YYERROR;
14903b3a8eb9SGleb Smirnoff 			}
14913b3a8eb9SGleb Smirnoff 			a.scheduler = $3.scheduler.qtype;
14923b3a8eb9SGleb Smirnoff 			a.qlimit = $3.qlimit;
14933b3a8eb9SGleb Smirnoff 			a.tbrsize = $3.tbrsize;
14940a70aaf8SLuiz Otavio O Souza 			if ($5 == NULL && $3.scheduler.qtype != ALTQT_CODEL) {
14953b3a8eb9SGleb Smirnoff 				yyerror("no child queues specified");
14963b3a8eb9SGleb Smirnoff 				YYERROR;
14973b3a8eb9SGleb Smirnoff 			}
14983b3a8eb9SGleb Smirnoff 			if (expand_altq(&a, $2, $5, $3.queue_bwspec,
14993b3a8eb9SGleb Smirnoff 			    &$3.scheduler))
15003b3a8eb9SGleb Smirnoff 				YYERROR;
15013b3a8eb9SGleb Smirnoff 		}
15023b3a8eb9SGleb Smirnoff 		;
15033b3a8eb9SGleb Smirnoff 
15043b3a8eb9SGleb Smirnoff queuespec	: QUEUE STRING interface queue_opts qassign {
15053b3a8eb9SGleb Smirnoff 			struct pf_altq	a;
15063b3a8eb9SGleb Smirnoff 
15073b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_QUEUE)) {
15083b3a8eb9SGleb Smirnoff 				free($2);
15093b3a8eb9SGleb Smirnoff 				YYERROR;
15103b3a8eb9SGleb Smirnoff 			}
15113b3a8eb9SGleb Smirnoff 
15123b3a8eb9SGleb Smirnoff 			memset(&a, 0, sizeof(a));
15133b3a8eb9SGleb Smirnoff 
15143b3a8eb9SGleb Smirnoff 			if (strlcpy(a.qname, $2, sizeof(a.qname)) >=
15153b3a8eb9SGleb Smirnoff 			    sizeof(a.qname)) {
15163b3a8eb9SGleb Smirnoff 				yyerror("queue name too long (max "
15173b3a8eb9SGleb Smirnoff 				    "%d chars)", PF_QNAME_SIZE-1);
15183b3a8eb9SGleb Smirnoff 				free($2);
15193b3a8eb9SGleb Smirnoff 				YYERROR;
15203b3a8eb9SGleb Smirnoff 			}
15213b3a8eb9SGleb Smirnoff 			free($2);
15223b3a8eb9SGleb Smirnoff 			if ($4.tbrsize) {
15233b3a8eb9SGleb Smirnoff 				yyerror("cannot specify tbrsize for queue");
15243b3a8eb9SGleb Smirnoff 				YYERROR;
15253b3a8eb9SGleb Smirnoff 			}
15263b3a8eb9SGleb Smirnoff 			if ($4.priority > 255) {
15273b3a8eb9SGleb Smirnoff 				yyerror("priority out of range: max 255");
15283b3a8eb9SGleb Smirnoff 				YYERROR;
15293b3a8eb9SGleb Smirnoff 			}
15303b3a8eb9SGleb Smirnoff 			a.priority = $4.priority;
15313b3a8eb9SGleb Smirnoff 			a.qlimit = $4.qlimit;
15323b3a8eb9SGleb Smirnoff 			a.scheduler = $4.scheduler.qtype;
15333b3a8eb9SGleb Smirnoff 			if (expand_queue(&a, $3, $5, $4.queue_bwspec,
15343b3a8eb9SGleb Smirnoff 			    &$4.scheduler)) {
15353b3a8eb9SGleb Smirnoff 				yyerror("errors in queue definition");
15363b3a8eb9SGleb Smirnoff 				YYERROR;
15373b3a8eb9SGleb Smirnoff 			}
15383b3a8eb9SGleb Smirnoff 		}
15393b3a8eb9SGleb Smirnoff 		;
15403b3a8eb9SGleb Smirnoff 
15413b3a8eb9SGleb Smirnoff queue_opts	:	{
15423b3a8eb9SGleb Smirnoff 			bzero(&queue_opts, sizeof queue_opts);
15433b3a8eb9SGleb Smirnoff 			queue_opts.priority = DEFAULT_PRIORITY;
15443b3a8eb9SGleb Smirnoff 			queue_opts.qlimit = DEFAULT_QLIMIT;
15453b3a8eb9SGleb Smirnoff 			queue_opts.scheduler.qtype = ALTQT_NONE;
15463b3a8eb9SGleb Smirnoff 			queue_opts.queue_bwspec.bw_percent = 100;
15473b3a8eb9SGleb Smirnoff 		}
15483b3a8eb9SGleb Smirnoff 		    queue_opts_l
15493b3a8eb9SGleb Smirnoff 			{ $$ = queue_opts; }
15503b3a8eb9SGleb Smirnoff 		| /* empty */ {
15513b3a8eb9SGleb Smirnoff 			bzero(&queue_opts, sizeof queue_opts);
15523b3a8eb9SGleb Smirnoff 			queue_opts.priority = DEFAULT_PRIORITY;
15533b3a8eb9SGleb Smirnoff 			queue_opts.qlimit = DEFAULT_QLIMIT;
15543b3a8eb9SGleb Smirnoff 			queue_opts.scheduler.qtype = ALTQT_NONE;
15553b3a8eb9SGleb Smirnoff 			queue_opts.queue_bwspec.bw_percent = 100;
15563b3a8eb9SGleb Smirnoff 			$$ = queue_opts;
15573b3a8eb9SGleb Smirnoff 		}
15583b3a8eb9SGleb Smirnoff 		;
15593b3a8eb9SGleb Smirnoff 
15603b3a8eb9SGleb Smirnoff queue_opts_l	: queue_opts_l queue_opt
15613b3a8eb9SGleb Smirnoff 		| queue_opt
15623b3a8eb9SGleb Smirnoff 		;
15633b3a8eb9SGleb Smirnoff 
15643b3a8eb9SGleb Smirnoff queue_opt	: BANDWIDTH bandwidth	{
15653b3a8eb9SGleb Smirnoff 			if (queue_opts.marker & QOM_BWSPEC) {
15663b3a8eb9SGleb Smirnoff 				yyerror("bandwidth cannot be respecified");
15673b3a8eb9SGleb Smirnoff 				YYERROR;
15683b3a8eb9SGleb Smirnoff 			}
15693b3a8eb9SGleb Smirnoff 			queue_opts.marker |= QOM_BWSPEC;
15703b3a8eb9SGleb Smirnoff 			queue_opts.queue_bwspec = $2;
15713b3a8eb9SGleb Smirnoff 		}
15723b3a8eb9SGleb Smirnoff 		| PRIORITY NUMBER	{
15733b3a8eb9SGleb Smirnoff 			if (queue_opts.marker & QOM_PRIORITY) {
15743b3a8eb9SGleb Smirnoff 				yyerror("priority cannot be respecified");
15753b3a8eb9SGleb Smirnoff 				YYERROR;
15763b3a8eb9SGleb Smirnoff 			}
15773b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > 255) {
15783b3a8eb9SGleb Smirnoff 				yyerror("priority out of range: max 255");
15793b3a8eb9SGleb Smirnoff 				YYERROR;
15803b3a8eb9SGleb Smirnoff 			}
15813b3a8eb9SGleb Smirnoff 			queue_opts.marker |= QOM_PRIORITY;
15823b3a8eb9SGleb Smirnoff 			queue_opts.priority = $2;
15833b3a8eb9SGleb Smirnoff 		}
15843b3a8eb9SGleb Smirnoff 		| QLIMIT NUMBER	{
15853b3a8eb9SGleb Smirnoff 			if (queue_opts.marker & QOM_QLIMIT) {
15863b3a8eb9SGleb Smirnoff 				yyerror("qlimit cannot be respecified");
15873b3a8eb9SGleb Smirnoff 				YYERROR;
15883b3a8eb9SGleb Smirnoff 			}
15893b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > 65535) {
15903b3a8eb9SGleb Smirnoff 				yyerror("qlimit out of range: max 65535");
15913b3a8eb9SGleb Smirnoff 				YYERROR;
15923b3a8eb9SGleb Smirnoff 			}
15933b3a8eb9SGleb Smirnoff 			queue_opts.marker |= QOM_QLIMIT;
15943b3a8eb9SGleb Smirnoff 			queue_opts.qlimit = $2;
15953b3a8eb9SGleb Smirnoff 		}
15963b3a8eb9SGleb Smirnoff 		| scheduler	{
15973b3a8eb9SGleb Smirnoff 			if (queue_opts.marker & QOM_SCHEDULER) {
15983b3a8eb9SGleb Smirnoff 				yyerror("scheduler cannot be respecified");
15993b3a8eb9SGleb Smirnoff 				YYERROR;
16003b3a8eb9SGleb Smirnoff 			}
16013b3a8eb9SGleb Smirnoff 			queue_opts.marker |= QOM_SCHEDULER;
16023b3a8eb9SGleb Smirnoff 			queue_opts.scheduler = $1;
16033b3a8eb9SGleb Smirnoff 		}
16043b3a8eb9SGleb Smirnoff 		| TBRSIZE NUMBER	{
16053b3a8eb9SGleb Smirnoff 			if (queue_opts.marker & QOM_TBRSIZE) {
16063b3a8eb9SGleb Smirnoff 				yyerror("tbrsize cannot be respecified");
16073b3a8eb9SGleb Smirnoff 				YYERROR;
16083b3a8eb9SGleb Smirnoff 			}
16093b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > 65535) {
16103b3a8eb9SGleb Smirnoff 				yyerror("tbrsize too big: max 65535");
16113b3a8eb9SGleb Smirnoff 				YYERROR;
16123b3a8eb9SGleb Smirnoff 			}
16133b3a8eb9SGleb Smirnoff 			queue_opts.marker |= QOM_TBRSIZE;
16143b3a8eb9SGleb Smirnoff 			queue_opts.tbrsize = $2;
16153b3a8eb9SGleb Smirnoff 		}
16163b3a8eb9SGleb Smirnoff 		;
16173b3a8eb9SGleb Smirnoff 
16183b3a8eb9SGleb Smirnoff bandwidth	: STRING {
16193b3a8eb9SGleb Smirnoff 			double	 bps;
16203b3a8eb9SGleb Smirnoff 			char	*cp;
16213b3a8eb9SGleb Smirnoff 
16223b3a8eb9SGleb Smirnoff 			$$.bw_percent = 0;
16233b3a8eb9SGleb Smirnoff 
16243b3a8eb9SGleb Smirnoff 			bps = strtod($1, &cp);
16253b3a8eb9SGleb Smirnoff 			if (cp != NULL) {
1626db1bbde6SLuiz Otavio O Souza 				if (strlen(cp) > 1) {
1627db1bbde6SLuiz Otavio O Souza 					char *cu = cp + 1;
1628db1bbde6SLuiz Otavio O Souza 					if (!strcmp(cu, "Bit") ||
1629db1bbde6SLuiz Otavio O Souza 					    !strcmp(cu, "B") ||
1630db1bbde6SLuiz Otavio O Souza 					    !strcmp(cu, "bit") ||
1631db1bbde6SLuiz Otavio O Souza 					    !strcmp(cu, "b")) {
1632db1bbde6SLuiz Otavio O Souza 						*cu = 0;
1633db1bbde6SLuiz Otavio O Souza 					}
1634db1bbde6SLuiz Otavio O Souza 				}
16353b3a8eb9SGleb Smirnoff 				if (!strcmp(cp, "b"))
16363b3a8eb9SGleb Smirnoff 					; /* nothing */
1637db1bbde6SLuiz Otavio O Souza 				else if (!strcmp(cp, "K"))
16383b3a8eb9SGleb Smirnoff 					bps *= 1000;
1639db1bbde6SLuiz Otavio O Souza 				else if (!strcmp(cp, "M"))
16403b3a8eb9SGleb Smirnoff 					bps *= 1000 * 1000;
1641db1bbde6SLuiz Otavio O Souza 				else if (!strcmp(cp, "G"))
16423b3a8eb9SGleb Smirnoff 					bps *= 1000 * 1000 * 1000;
16433b3a8eb9SGleb Smirnoff 				else if (!strcmp(cp, "%")) {
16443b3a8eb9SGleb Smirnoff 					if (bps < 0 || bps > 100) {
16453b3a8eb9SGleb Smirnoff 						yyerror("bandwidth spec "
16463b3a8eb9SGleb Smirnoff 						    "out of range");
16473b3a8eb9SGleb Smirnoff 						free($1);
16483b3a8eb9SGleb Smirnoff 						YYERROR;
16493b3a8eb9SGleb Smirnoff 					}
16503b3a8eb9SGleb Smirnoff 					$$.bw_percent = bps;
16513b3a8eb9SGleb Smirnoff 					bps = 0;
16523b3a8eb9SGleb Smirnoff 				} else {
16533b3a8eb9SGleb Smirnoff 					yyerror("unknown unit %s", cp);
16543b3a8eb9SGleb Smirnoff 					free($1);
16553b3a8eb9SGleb Smirnoff 					YYERROR;
16563b3a8eb9SGleb Smirnoff 				}
16573b3a8eb9SGleb Smirnoff 			}
16583b3a8eb9SGleb Smirnoff 			free($1);
16593b3a8eb9SGleb Smirnoff 			$$.bw_absolute = (u_int32_t)bps;
16603b3a8eb9SGleb Smirnoff 		}
16613b3a8eb9SGleb Smirnoff 		| NUMBER {
16623b3a8eb9SGleb Smirnoff 			if ($1 < 0 || $1 > UINT_MAX) {
16633b3a8eb9SGleb Smirnoff 				yyerror("bandwidth number too big");
16643b3a8eb9SGleb Smirnoff 				YYERROR;
16653b3a8eb9SGleb Smirnoff 			}
16663b3a8eb9SGleb Smirnoff 			$$.bw_percent = 0;
16673b3a8eb9SGleb Smirnoff 			$$.bw_absolute = $1;
16683b3a8eb9SGleb Smirnoff 		}
16693b3a8eb9SGleb Smirnoff 		;
16703b3a8eb9SGleb Smirnoff 
16713b3a8eb9SGleb Smirnoff scheduler	: CBQ				{
16723b3a8eb9SGleb Smirnoff 			$$.qtype = ALTQT_CBQ;
16733b3a8eb9SGleb Smirnoff 			$$.data.cbq_opts.flags = 0;
16743b3a8eb9SGleb Smirnoff 		}
16753b3a8eb9SGleb Smirnoff 		| CBQ '(' cbqflags_list ')'	{
16763b3a8eb9SGleb Smirnoff 			$$.qtype = ALTQT_CBQ;
16773b3a8eb9SGleb Smirnoff 			$$.data.cbq_opts.flags = $3;
16783b3a8eb9SGleb Smirnoff 		}
16793b3a8eb9SGleb Smirnoff 		| PRIQ				{
16803b3a8eb9SGleb Smirnoff 			$$.qtype = ALTQT_PRIQ;
16813b3a8eb9SGleb Smirnoff 			$$.data.priq_opts.flags = 0;
16823b3a8eb9SGleb Smirnoff 		}
16833b3a8eb9SGleb Smirnoff 		| PRIQ '(' priqflags_list ')'	{
16843b3a8eb9SGleb Smirnoff 			$$.qtype = ALTQT_PRIQ;
16853b3a8eb9SGleb Smirnoff 			$$.data.priq_opts.flags = $3;
16863b3a8eb9SGleb Smirnoff 		}
16873b3a8eb9SGleb Smirnoff 		| HFSC				{
16883b3a8eb9SGleb Smirnoff 			$$.qtype = ALTQT_HFSC;
16893b3a8eb9SGleb Smirnoff 			bzero(&$$.data.hfsc_opts,
16903b3a8eb9SGleb Smirnoff 			    sizeof(struct node_hfsc_opts));
16913b3a8eb9SGleb Smirnoff 		}
16923b3a8eb9SGleb Smirnoff 		| HFSC '(' hfsc_opts ')'	{
16933b3a8eb9SGleb Smirnoff 			$$.qtype = ALTQT_HFSC;
16943b3a8eb9SGleb Smirnoff 			$$.data.hfsc_opts = $3;
16953b3a8eb9SGleb Smirnoff 		}
1696a5b789f6SErmal Luçi 		| FAIRQ				{
1697a5b789f6SErmal Luçi 			$$.qtype = ALTQT_FAIRQ;
1698a5b789f6SErmal Luçi 			bzero(&$$.data.fairq_opts,
1699a5b789f6SErmal Luçi 				sizeof(struct node_fairq_opts));
1700a5b789f6SErmal Luçi 		}
1701a5b789f6SErmal Luçi 		| FAIRQ '(' fairq_opts ')'      {
1702a5b789f6SErmal Luçi 			$$.qtype = ALTQT_FAIRQ;
1703a5b789f6SErmal Luçi 			$$.data.fairq_opts = $3;
1704a5b789f6SErmal Luçi 		}
17050a70aaf8SLuiz Otavio O Souza 		| CODEL				{
17060a70aaf8SLuiz Otavio O Souza 			$$.qtype = ALTQT_CODEL;
17070a70aaf8SLuiz Otavio O Souza 			bzero(&$$.data.codel_opts,
17080a70aaf8SLuiz Otavio O Souza 				sizeof(struct codel_opts));
17090a70aaf8SLuiz Otavio O Souza 		}
17100a70aaf8SLuiz Otavio O Souza 		| CODEL '(' codel_opts ')'	{
17110a70aaf8SLuiz Otavio O Souza 			$$.qtype = ALTQT_CODEL;
17120a70aaf8SLuiz Otavio O Souza 			$$.data.codel_opts = $3;
17130a70aaf8SLuiz Otavio O Souza 		}
17143b3a8eb9SGleb Smirnoff 		;
17153b3a8eb9SGleb Smirnoff 
17163b3a8eb9SGleb Smirnoff cbqflags_list	: cbqflags_item				{ $$ |= $1; }
17173b3a8eb9SGleb Smirnoff 		| cbqflags_list comma cbqflags_item	{ $$ |= $3; }
17183b3a8eb9SGleb Smirnoff 		;
17193b3a8eb9SGleb Smirnoff 
17203b3a8eb9SGleb Smirnoff cbqflags_item	: STRING	{
17213b3a8eb9SGleb Smirnoff 			if (!strcmp($1, "default"))
17223b3a8eb9SGleb Smirnoff 				$$ = CBQCLF_DEFCLASS;
17233b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "borrow"))
17243b3a8eb9SGleb Smirnoff 				$$ = CBQCLF_BORROW;
17253b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "red"))
17263b3a8eb9SGleb Smirnoff 				$$ = CBQCLF_RED;
17273b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "ecn"))
17283b3a8eb9SGleb Smirnoff 				$$ = CBQCLF_RED|CBQCLF_ECN;
17293b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "rio"))
17303b3a8eb9SGleb Smirnoff 				$$ = CBQCLF_RIO;
17310a70aaf8SLuiz Otavio O Souza 			else if (!strcmp($1, "codel"))
17320a70aaf8SLuiz Otavio O Souza 				$$ = CBQCLF_CODEL;
17333b3a8eb9SGleb Smirnoff 			else {
17343b3a8eb9SGleb Smirnoff 				yyerror("unknown cbq flag \"%s\"", $1);
17353b3a8eb9SGleb Smirnoff 				free($1);
17363b3a8eb9SGleb Smirnoff 				YYERROR;
17373b3a8eb9SGleb Smirnoff 			}
17383b3a8eb9SGleb Smirnoff 			free($1);
17393b3a8eb9SGleb Smirnoff 		}
17403b3a8eb9SGleb Smirnoff 		;
17413b3a8eb9SGleb Smirnoff 
17423b3a8eb9SGleb Smirnoff priqflags_list	: priqflags_item			{ $$ |= $1; }
17433b3a8eb9SGleb Smirnoff 		| priqflags_list comma priqflags_item	{ $$ |= $3; }
17443b3a8eb9SGleb Smirnoff 		;
17453b3a8eb9SGleb Smirnoff 
17463b3a8eb9SGleb Smirnoff priqflags_item	: STRING	{
17473b3a8eb9SGleb Smirnoff 			if (!strcmp($1, "default"))
17483b3a8eb9SGleb Smirnoff 				$$ = PRCF_DEFAULTCLASS;
17493b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "red"))
17503b3a8eb9SGleb Smirnoff 				$$ = PRCF_RED;
17513b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "ecn"))
17523b3a8eb9SGleb Smirnoff 				$$ = PRCF_RED|PRCF_ECN;
17533b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "rio"))
17543b3a8eb9SGleb Smirnoff 				$$ = PRCF_RIO;
17550a70aaf8SLuiz Otavio O Souza 			else if (!strcmp($1, "codel"))
17560a70aaf8SLuiz Otavio O Souza 				$$ = PRCF_CODEL;
17573b3a8eb9SGleb Smirnoff 			else {
17583b3a8eb9SGleb Smirnoff 				yyerror("unknown priq flag \"%s\"", $1);
17593b3a8eb9SGleb Smirnoff 				free($1);
17603b3a8eb9SGleb Smirnoff 				YYERROR;
17613b3a8eb9SGleb Smirnoff 			}
17623b3a8eb9SGleb Smirnoff 			free($1);
17633b3a8eb9SGleb Smirnoff 		}
17643b3a8eb9SGleb Smirnoff 		;
17653b3a8eb9SGleb Smirnoff 
17663b3a8eb9SGleb Smirnoff hfsc_opts	:	{
17673b3a8eb9SGleb Smirnoff 				bzero(&hfsc_opts,
17683b3a8eb9SGleb Smirnoff 				    sizeof(struct node_hfsc_opts));
17693b3a8eb9SGleb Smirnoff 			}
17703b3a8eb9SGleb Smirnoff 		    hfscopts_list				{
17713b3a8eb9SGleb Smirnoff 			$$ = hfsc_opts;
17723b3a8eb9SGleb Smirnoff 		}
17733b3a8eb9SGleb Smirnoff 		;
17743b3a8eb9SGleb Smirnoff 
17753b3a8eb9SGleb Smirnoff hfscopts_list	: hfscopts_item
17763b3a8eb9SGleb Smirnoff 		| hfscopts_list comma hfscopts_item
17773b3a8eb9SGleb Smirnoff 		;
17783b3a8eb9SGleb Smirnoff 
17793b3a8eb9SGleb Smirnoff hfscopts_item	: LINKSHARE bandwidth				{
17803b3a8eb9SGleb Smirnoff 			if (hfsc_opts.linkshare.used) {
17813b3a8eb9SGleb Smirnoff 				yyerror("linkshare already specified");
17823b3a8eb9SGleb Smirnoff 				YYERROR;
17833b3a8eb9SGleb Smirnoff 			}
17843b3a8eb9SGleb Smirnoff 			hfsc_opts.linkshare.m2 = $2;
17853b3a8eb9SGleb Smirnoff 			hfsc_opts.linkshare.used = 1;
17863b3a8eb9SGleb Smirnoff 		}
17873b3a8eb9SGleb Smirnoff 		| LINKSHARE '(' bandwidth comma NUMBER comma bandwidth ')'
17883b3a8eb9SGleb Smirnoff 		    {
17893b3a8eb9SGleb Smirnoff 			if ($5 < 0 || $5 > INT_MAX) {
17903b3a8eb9SGleb Smirnoff 				yyerror("timing in curve out of range");
17913b3a8eb9SGleb Smirnoff 				YYERROR;
17923b3a8eb9SGleb Smirnoff 			}
17933b3a8eb9SGleb Smirnoff 			if (hfsc_opts.linkshare.used) {
17943b3a8eb9SGleb Smirnoff 				yyerror("linkshare already specified");
17953b3a8eb9SGleb Smirnoff 				YYERROR;
17963b3a8eb9SGleb Smirnoff 			}
17973b3a8eb9SGleb Smirnoff 			hfsc_opts.linkshare.m1 = $3;
17983b3a8eb9SGleb Smirnoff 			hfsc_opts.linkshare.d = $5;
17993b3a8eb9SGleb Smirnoff 			hfsc_opts.linkshare.m2 = $7;
18003b3a8eb9SGleb Smirnoff 			hfsc_opts.linkshare.used = 1;
18013b3a8eb9SGleb Smirnoff 		}
18023b3a8eb9SGleb Smirnoff 		| REALTIME bandwidth				{
18033b3a8eb9SGleb Smirnoff 			if (hfsc_opts.realtime.used) {
18043b3a8eb9SGleb Smirnoff 				yyerror("realtime already specified");
18053b3a8eb9SGleb Smirnoff 				YYERROR;
18063b3a8eb9SGleb Smirnoff 			}
18073b3a8eb9SGleb Smirnoff 			hfsc_opts.realtime.m2 = $2;
18083b3a8eb9SGleb Smirnoff 			hfsc_opts.realtime.used = 1;
18093b3a8eb9SGleb Smirnoff 		}
18103b3a8eb9SGleb Smirnoff 		| REALTIME '(' bandwidth comma NUMBER comma bandwidth ')'
18113b3a8eb9SGleb Smirnoff 		    {
18123b3a8eb9SGleb Smirnoff 			if ($5 < 0 || $5 > INT_MAX) {
18133b3a8eb9SGleb Smirnoff 				yyerror("timing in curve out of range");
18143b3a8eb9SGleb Smirnoff 				YYERROR;
18153b3a8eb9SGleb Smirnoff 			}
18163b3a8eb9SGleb Smirnoff 			if (hfsc_opts.realtime.used) {
18173b3a8eb9SGleb Smirnoff 				yyerror("realtime already specified");
18183b3a8eb9SGleb Smirnoff 				YYERROR;
18193b3a8eb9SGleb Smirnoff 			}
18203b3a8eb9SGleb Smirnoff 			hfsc_opts.realtime.m1 = $3;
18213b3a8eb9SGleb Smirnoff 			hfsc_opts.realtime.d = $5;
18223b3a8eb9SGleb Smirnoff 			hfsc_opts.realtime.m2 = $7;
18233b3a8eb9SGleb Smirnoff 			hfsc_opts.realtime.used = 1;
18243b3a8eb9SGleb Smirnoff 		}
18253b3a8eb9SGleb Smirnoff 		| UPPERLIMIT bandwidth				{
18263b3a8eb9SGleb Smirnoff 			if (hfsc_opts.upperlimit.used) {
18273b3a8eb9SGleb Smirnoff 				yyerror("upperlimit already specified");
18283b3a8eb9SGleb Smirnoff 				YYERROR;
18293b3a8eb9SGleb Smirnoff 			}
18303b3a8eb9SGleb Smirnoff 			hfsc_opts.upperlimit.m2 = $2;
18313b3a8eb9SGleb Smirnoff 			hfsc_opts.upperlimit.used = 1;
18323b3a8eb9SGleb Smirnoff 		}
18333b3a8eb9SGleb Smirnoff 		| UPPERLIMIT '(' bandwidth comma NUMBER comma bandwidth ')'
18343b3a8eb9SGleb Smirnoff 		    {
18353b3a8eb9SGleb Smirnoff 			if ($5 < 0 || $5 > INT_MAX) {
18363b3a8eb9SGleb Smirnoff 				yyerror("timing in curve out of range");
18373b3a8eb9SGleb Smirnoff 				YYERROR;
18383b3a8eb9SGleb Smirnoff 			}
18393b3a8eb9SGleb Smirnoff 			if (hfsc_opts.upperlimit.used) {
18403b3a8eb9SGleb Smirnoff 				yyerror("upperlimit already specified");
18413b3a8eb9SGleb Smirnoff 				YYERROR;
18423b3a8eb9SGleb Smirnoff 			}
18433b3a8eb9SGleb Smirnoff 			hfsc_opts.upperlimit.m1 = $3;
18443b3a8eb9SGleb Smirnoff 			hfsc_opts.upperlimit.d = $5;
18453b3a8eb9SGleb Smirnoff 			hfsc_opts.upperlimit.m2 = $7;
18463b3a8eb9SGleb Smirnoff 			hfsc_opts.upperlimit.used = 1;
18473b3a8eb9SGleb Smirnoff 		}
18483b3a8eb9SGleb Smirnoff 		| STRING	{
18493b3a8eb9SGleb Smirnoff 			if (!strcmp($1, "default"))
18503b3a8eb9SGleb Smirnoff 				hfsc_opts.flags |= HFCF_DEFAULTCLASS;
18513b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "red"))
18523b3a8eb9SGleb Smirnoff 				hfsc_opts.flags |= HFCF_RED;
18533b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "ecn"))
18543b3a8eb9SGleb Smirnoff 				hfsc_opts.flags |= HFCF_RED|HFCF_ECN;
18553b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "rio"))
18563b3a8eb9SGleb Smirnoff 				hfsc_opts.flags |= HFCF_RIO;
18570a70aaf8SLuiz Otavio O Souza 			else if (!strcmp($1, "codel"))
18580a70aaf8SLuiz Otavio O Souza 				hfsc_opts.flags |= HFCF_CODEL;
18593b3a8eb9SGleb Smirnoff 			else {
18603b3a8eb9SGleb Smirnoff 				yyerror("unknown hfsc flag \"%s\"", $1);
18613b3a8eb9SGleb Smirnoff 				free($1);
18623b3a8eb9SGleb Smirnoff 				YYERROR;
18633b3a8eb9SGleb Smirnoff 			}
18643b3a8eb9SGleb Smirnoff 			free($1);
18653b3a8eb9SGleb Smirnoff 		}
18663b3a8eb9SGleb Smirnoff 		;
18673b3a8eb9SGleb Smirnoff 
1868a5b789f6SErmal Luçi fairq_opts	:	{
1869a5b789f6SErmal Luçi 				bzero(&fairq_opts,
1870a5b789f6SErmal Luçi 				    sizeof(struct node_fairq_opts));
1871a5b789f6SErmal Luçi 			}
1872a5b789f6SErmal Luçi 		    fairqopts_list				{
1873a5b789f6SErmal Luçi 			$$ = fairq_opts;
1874a5b789f6SErmal Luçi 		}
1875a5b789f6SErmal Luçi 		;
1876a5b789f6SErmal Luçi 
1877a5b789f6SErmal Luçi fairqopts_list	: fairqopts_item
1878a5b789f6SErmal Luçi 		| fairqopts_list comma fairqopts_item
1879a5b789f6SErmal Luçi 		;
1880a5b789f6SErmal Luçi 
1881a5b789f6SErmal Luçi fairqopts_item	: LINKSHARE bandwidth				{
1882a5b789f6SErmal Luçi 			if (fairq_opts.linkshare.used) {
1883a5b789f6SErmal Luçi 				yyerror("linkshare already specified");
1884a5b789f6SErmal Luçi 				YYERROR;
1885a5b789f6SErmal Luçi 			}
1886a5b789f6SErmal Luçi 			fairq_opts.linkshare.m2 = $2;
1887a5b789f6SErmal Luçi 			fairq_opts.linkshare.used = 1;
1888a5b789f6SErmal Luçi 		}
1889a5b789f6SErmal Luçi 		| LINKSHARE '(' bandwidth number bandwidth ')'	{
1890a5b789f6SErmal Luçi 			if (fairq_opts.linkshare.used) {
1891a5b789f6SErmal Luçi 				yyerror("linkshare already specified");
1892a5b789f6SErmal Luçi 				YYERROR;
1893a5b789f6SErmal Luçi 			}
1894a5b789f6SErmal Luçi 			fairq_opts.linkshare.m1 = $3;
1895a5b789f6SErmal Luçi 			fairq_opts.linkshare.d = $4;
1896a5b789f6SErmal Luçi 			fairq_opts.linkshare.m2 = $5;
1897a5b789f6SErmal Luçi 			fairq_opts.linkshare.used = 1;
1898a5b789f6SErmal Luçi 		}
1899a5b789f6SErmal Luçi 		| HOGS bandwidth {
1900a5b789f6SErmal Luçi 			fairq_opts.hogs_bw = $2;
1901a5b789f6SErmal Luçi 		}
1902a5b789f6SErmal Luçi 		| BUCKETS number {
1903a5b789f6SErmal Luçi 			fairq_opts.nbuckets = $2;
1904a5b789f6SErmal Luçi 		}
1905a5b789f6SErmal Luçi 		| STRING	{
1906a5b789f6SErmal Luçi 			if (!strcmp($1, "default"))
1907a5b789f6SErmal Luçi 				fairq_opts.flags |= FARF_DEFAULTCLASS;
1908a5b789f6SErmal Luçi 			else if (!strcmp($1, "red"))
1909a5b789f6SErmal Luçi 				fairq_opts.flags |= FARF_RED;
1910a5b789f6SErmal Luçi 			else if (!strcmp($1, "ecn"))
1911a5b789f6SErmal Luçi 				fairq_opts.flags |= FARF_RED|FARF_ECN;
1912a5b789f6SErmal Luçi 			else if (!strcmp($1, "rio"))
1913a5b789f6SErmal Luçi 				fairq_opts.flags |= FARF_RIO;
19140a70aaf8SLuiz Otavio O Souza 			else if (!strcmp($1, "codel"))
19150a70aaf8SLuiz Otavio O Souza 				fairq_opts.flags |= FARF_CODEL;
1916a5b789f6SErmal Luçi 			else {
1917a5b789f6SErmal Luçi 				yyerror("unknown fairq flag \"%s\"", $1);
1918a5b789f6SErmal Luçi 				free($1);
1919a5b789f6SErmal Luçi 				YYERROR;
1920a5b789f6SErmal Luçi 			}
1921a5b789f6SErmal Luçi 			free($1);
1922a5b789f6SErmal Luçi 		}
1923a5b789f6SErmal Luçi 		;
1924a5b789f6SErmal Luçi 
19250a70aaf8SLuiz Otavio O Souza codel_opts	:	{
19260a70aaf8SLuiz Otavio O Souza 				bzero(&codel_opts,
19270a70aaf8SLuiz Otavio O Souza 				    sizeof(struct codel_opts));
19280a70aaf8SLuiz Otavio O Souza 			}
19290a70aaf8SLuiz Otavio O Souza 		    codelopts_list				{
19300a70aaf8SLuiz Otavio O Souza 			$$ = codel_opts;
19310a70aaf8SLuiz Otavio O Souza 		}
19320a70aaf8SLuiz Otavio O Souza 		;
19330a70aaf8SLuiz Otavio O Souza 
19340a70aaf8SLuiz Otavio O Souza codelopts_list	: codelopts_item
19350a70aaf8SLuiz Otavio O Souza 		| codelopts_list comma codelopts_item
19360a70aaf8SLuiz Otavio O Souza 		;
19370a70aaf8SLuiz Otavio O Souza 
19380a70aaf8SLuiz Otavio O Souza codelopts_item	: INTERVAL number				{
19390a70aaf8SLuiz Otavio O Souza 			if (codel_opts.interval) {
19400a70aaf8SLuiz Otavio O Souza 				yyerror("interval already specified");
19410a70aaf8SLuiz Otavio O Souza 				YYERROR;
19420a70aaf8SLuiz Otavio O Souza 			}
19430a70aaf8SLuiz Otavio O Souza 			codel_opts.interval = $2;
19440a70aaf8SLuiz Otavio O Souza 		}
19450a70aaf8SLuiz Otavio O Souza 		| TARGET number					{
19460a70aaf8SLuiz Otavio O Souza 			if (codel_opts.target) {
19470a70aaf8SLuiz Otavio O Souza 				yyerror("target already specified");
19480a70aaf8SLuiz Otavio O Souza 				YYERROR;
19490a70aaf8SLuiz Otavio O Souza 			}
19500a70aaf8SLuiz Otavio O Souza 			codel_opts.target = $2;
19510a70aaf8SLuiz Otavio O Souza 		}
19520a70aaf8SLuiz Otavio O Souza 		| STRING					{
19530a70aaf8SLuiz Otavio O Souza 			if (!strcmp($1, "ecn"))
19540a70aaf8SLuiz Otavio O Souza 				codel_opts.ecn = 1;
19550a70aaf8SLuiz Otavio O Souza 			else {
19560a70aaf8SLuiz Otavio O Souza 				yyerror("unknown codel option \"%s\"", $1);
19570a70aaf8SLuiz Otavio O Souza 				free($1);
19580a70aaf8SLuiz Otavio O Souza 				YYERROR;
19590a70aaf8SLuiz Otavio O Souza 			}
19600a70aaf8SLuiz Otavio O Souza 			free($1);
19610a70aaf8SLuiz Otavio O Souza 		}
19620a70aaf8SLuiz Otavio O Souza 		;
19630a70aaf8SLuiz Otavio O Souza 
19643b3a8eb9SGleb Smirnoff qassign		: /* empty */		{ $$ = NULL; }
19653b3a8eb9SGleb Smirnoff 		| qassign_item		{ $$ = $1; }
19663b3a8eb9SGleb Smirnoff 		| '{' optnl qassign_list '}'	{ $$ = $3; }
19673b3a8eb9SGleb Smirnoff 		;
19683b3a8eb9SGleb Smirnoff 
19693b3a8eb9SGleb Smirnoff qassign_list	: qassign_item optnl		{ $$ = $1; }
19703b3a8eb9SGleb Smirnoff 		| qassign_list comma qassign_item optnl	{
19713b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
19723b3a8eb9SGleb Smirnoff 			$1->tail = $3;
19733b3a8eb9SGleb Smirnoff 			$$ = $1;
19743b3a8eb9SGleb Smirnoff 		}
19753b3a8eb9SGleb Smirnoff 		;
19763b3a8eb9SGleb Smirnoff 
19773b3a8eb9SGleb Smirnoff qassign_item	: STRING			{
19783b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_queue));
19793b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
19803b3a8eb9SGleb Smirnoff 				err(1, "qassign_item: calloc");
19813b3a8eb9SGleb Smirnoff 			if (strlcpy($$->queue, $1, sizeof($$->queue)) >=
19823b3a8eb9SGleb Smirnoff 			    sizeof($$->queue)) {
19833b3a8eb9SGleb Smirnoff 				yyerror("queue name '%s' too long (max "
19843b3a8eb9SGleb Smirnoff 				    "%d chars)", $1, sizeof($$->queue)-1);
19853b3a8eb9SGleb Smirnoff 				free($1);
19863b3a8eb9SGleb Smirnoff 				free($$);
19873b3a8eb9SGleb Smirnoff 				YYERROR;
19883b3a8eb9SGleb Smirnoff 			}
19893b3a8eb9SGleb Smirnoff 			free($1);
19903b3a8eb9SGleb Smirnoff 			$$->next = NULL;
19913b3a8eb9SGleb Smirnoff 			$$->tail = $$;
19923b3a8eb9SGleb Smirnoff 		}
19933b3a8eb9SGleb Smirnoff 		;
19943b3a8eb9SGleb Smirnoff 
19953b3a8eb9SGleb Smirnoff pfrule		: action dir logquick interface route af proto fromto
19963b3a8eb9SGleb Smirnoff 		    filter_opts
19973b3a8eb9SGleb Smirnoff 		{
19983b3a8eb9SGleb Smirnoff 			struct pf_rule		 r;
19993b3a8eb9SGleb Smirnoff 			struct node_state_opt	*o;
20003b3a8eb9SGleb Smirnoff 			struct node_proto	*proto;
20013b3a8eb9SGleb Smirnoff 			int			 srctrack = 0;
20023b3a8eb9SGleb Smirnoff 			int			 statelock = 0;
20033b3a8eb9SGleb Smirnoff 			int			 adaptive = 0;
20043b3a8eb9SGleb Smirnoff 			int			 defaults = 0;
20053b3a8eb9SGleb Smirnoff 
20063b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_FILTER))
20073b3a8eb9SGleb Smirnoff 				YYERROR;
20083b3a8eb9SGleb Smirnoff 
20093b3a8eb9SGleb Smirnoff 			memset(&r, 0, sizeof(r));
20103b3a8eb9SGleb Smirnoff 
20113b3a8eb9SGleb Smirnoff 			r.action = $1.b1;
20123b3a8eb9SGleb Smirnoff 			switch ($1.b2) {
20133b3a8eb9SGleb Smirnoff 			case PFRULE_RETURNRST:
20143b3a8eb9SGleb Smirnoff 				r.rule_flag |= PFRULE_RETURNRST;
20153b3a8eb9SGleb Smirnoff 				r.return_ttl = $1.w;
20163b3a8eb9SGleb Smirnoff 				break;
20173b3a8eb9SGleb Smirnoff 			case PFRULE_RETURNICMP:
20183b3a8eb9SGleb Smirnoff 				r.rule_flag |= PFRULE_RETURNICMP;
20193b3a8eb9SGleb Smirnoff 				r.return_icmp = $1.w;
20203b3a8eb9SGleb Smirnoff 				r.return_icmp6 = $1.w2;
20213b3a8eb9SGleb Smirnoff 				break;
20223b3a8eb9SGleb Smirnoff 			case PFRULE_RETURN:
20233b3a8eb9SGleb Smirnoff 				r.rule_flag |= PFRULE_RETURN;
20243b3a8eb9SGleb Smirnoff 				r.return_icmp = $1.w;
20253b3a8eb9SGleb Smirnoff 				r.return_icmp6 = $1.w2;
20263b3a8eb9SGleb Smirnoff 				break;
20273b3a8eb9SGleb Smirnoff 			}
20283b3a8eb9SGleb Smirnoff 			r.direction = $2;
20293b3a8eb9SGleb Smirnoff 			r.log = $3.log;
20303b3a8eb9SGleb Smirnoff 			r.logif = $3.logif;
20313b3a8eb9SGleb Smirnoff 			r.quick = $3.quick;
20323b3a8eb9SGleb Smirnoff 			r.prob = $9.prob;
20333b3a8eb9SGleb Smirnoff 			r.rtableid = $9.rtableid;
20343b3a8eb9SGleb Smirnoff 
20353e248e0fSKristof Provost 			if ($9.marker & FOM_PRIO) {
20363e248e0fSKristof Provost 				if ($9.prio == 0)
20373e248e0fSKristof Provost 					r.prio = PF_PRIO_ZERO;
20383e248e0fSKristof Provost 				else
20393e248e0fSKristof Provost 					r.prio = $9.prio;
20403e248e0fSKristof Provost 			}
20413e248e0fSKristof Provost 			if ($9.marker & FOM_SETPRIO) {
20423e248e0fSKristof Provost 				r.set_prio[0] = $9.set_prio[0];
20433e248e0fSKristof Provost 				r.set_prio[1] = $9.set_prio[1];
20443e248e0fSKristof Provost 				r.scrub_flags |= PFSTATE_SETPRIO;
20453e248e0fSKristof Provost 			}
20463e248e0fSKristof Provost 
20473b3a8eb9SGleb Smirnoff 			r.af = $6;
20483b3a8eb9SGleb Smirnoff 			if ($9.tag)
20493b3a8eb9SGleb Smirnoff 				if (strlcpy(r.tagname, $9.tag,
20503b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
20513b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
20523b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
20533b3a8eb9SGleb Smirnoff 					YYERROR;
20543b3a8eb9SGleb Smirnoff 				}
20553b3a8eb9SGleb Smirnoff 			if ($9.match_tag)
20563b3a8eb9SGleb Smirnoff 				if (strlcpy(r.match_tagname, $9.match_tag,
20573b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
20583b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
20593b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
20603b3a8eb9SGleb Smirnoff 					YYERROR;
20613b3a8eb9SGleb Smirnoff 				}
20623b3a8eb9SGleb Smirnoff 			r.match_tag_not = $9.match_tag_not;
20633b3a8eb9SGleb Smirnoff 			if (rule_label(&r, $9.label))
20643b3a8eb9SGleb Smirnoff 				YYERROR;
20653b3a8eb9SGleb Smirnoff 			free($9.label);
20663b3a8eb9SGleb Smirnoff 			r.flags = $9.flags.b1;
20673b3a8eb9SGleb Smirnoff 			r.flagset = $9.flags.b2;
20683b3a8eb9SGleb Smirnoff 			if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
20693b3a8eb9SGleb Smirnoff 				yyerror("flags always false");
20703b3a8eb9SGleb Smirnoff 				YYERROR;
20713b3a8eb9SGleb Smirnoff 			}
20723b3a8eb9SGleb Smirnoff 			if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
20733b3a8eb9SGleb Smirnoff 				for (proto = $7; proto != NULL &&
20743b3a8eb9SGleb Smirnoff 				    proto->proto != IPPROTO_TCP;
20753b3a8eb9SGleb Smirnoff 				    proto = proto->next)
20763b3a8eb9SGleb Smirnoff 					;	/* nothing */
20773b3a8eb9SGleb Smirnoff 				if (proto == NULL && $7 != NULL) {
20783b3a8eb9SGleb Smirnoff 					if ($9.flags.b1 || $9.flags.b2)
20793b3a8eb9SGleb Smirnoff 						yyerror(
20803b3a8eb9SGleb Smirnoff 						    "flags only apply to tcp");
20813b3a8eb9SGleb Smirnoff 					if ($8.src_os)
20823b3a8eb9SGleb Smirnoff 						yyerror(
20833b3a8eb9SGleb Smirnoff 						    "OS fingerprinting only "
20843b3a8eb9SGleb Smirnoff 						    "apply to tcp");
20853b3a8eb9SGleb Smirnoff 					YYERROR;
20863b3a8eb9SGleb Smirnoff 				}
20873b3a8eb9SGleb Smirnoff #if 0
20883b3a8eb9SGleb Smirnoff 				if (($9.flags.b1 & parse_flags("S")) == 0 &&
20893b3a8eb9SGleb Smirnoff 				    $8.src_os) {
20903b3a8eb9SGleb Smirnoff 					yyerror("OS fingerprinting requires "
20913b3a8eb9SGleb Smirnoff 					    "the SYN TCP flag (flags S/SA)");
20923b3a8eb9SGleb Smirnoff 					YYERROR;
20933b3a8eb9SGleb Smirnoff 				}
20943b3a8eb9SGleb Smirnoff #endif
20953b3a8eb9SGleb Smirnoff 			}
20963b3a8eb9SGleb Smirnoff 
20973b3a8eb9SGleb Smirnoff 			r.tos = $9.tos;
20983b3a8eb9SGleb Smirnoff 			r.keep_state = $9.keep.action;
20993b3a8eb9SGleb Smirnoff 			o = $9.keep.options;
21003b3a8eb9SGleb Smirnoff 
21013b3a8eb9SGleb Smirnoff 			/* 'keep state' by default on pass rules. */
21023b3a8eb9SGleb Smirnoff 			if (!r.keep_state && !r.action &&
21033b3a8eb9SGleb Smirnoff 			    !($9.marker & FOM_KEEP)) {
21043b3a8eb9SGleb Smirnoff 				r.keep_state = PF_STATE_NORMAL;
21053b3a8eb9SGleb Smirnoff 				o = keep_state_defaults;
21063b3a8eb9SGleb Smirnoff 				defaults = 1;
21073b3a8eb9SGleb Smirnoff 			}
21083b3a8eb9SGleb Smirnoff 
21093b3a8eb9SGleb Smirnoff 			while (o) {
21103b3a8eb9SGleb Smirnoff 				struct node_state_opt	*p = o;
21113b3a8eb9SGleb Smirnoff 
21123b3a8eb9SGleb Smirnoff 				switch (o->type) {
21133b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_MAX:
21143b3a8eb9SGleb Smirnoff 					if (r.max_states) {
21153b3a8eb9SGleb Smirnoff 						yyerror("state option 'max' "
21163b3a8eb9SGleb Smirnoff 						    "multiple definitions");
21173b3a8eb9SGleb Smirnoff 						YYERROR;
21183b3a8eb9SGleb Smirnoff 					}
21193b3a8eb9SGleb Smirnoff 					r.max_states = o->data.max_states;
21203b3a8eb9SGleb Smirnoff 					break;
21213b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_NOSYNC:
21223b3a8eb9SGleb Smirnoff 					if (r.rule_flag & PFRULE_NOSYNC) {
21233b3a8eb9SGleb Smirnoff 						yyerror("state option 'sync' "
21243b3a8eb9SGleb Smirnoff 						    "multiple definitions");
21253b3a8eb9SGleb Smirnoff 						YYERROR;
21263b3a8eb9SGleb Smirnoff 					}
21273b3a8eb9SGleb Smirnoff 					r.rule_flag |= PFRULE_NOSYNC;
21283b3a8eb9SGleb Smirnoff 					break;
21293b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_SRCTRACK:
21303b3a8eb9SGleb Smirnoff 					if (srctrack) {
21313b3a8eb9SGleb Smirnoff 						yyerror("state option "
21323b3a8eb9SGleb Smirnoff 						    "'source-track' "
21333b3a8eb9SGleb Smirnoff 						    "multiple definitions");
21343b3a8eb9SGleb Smirnoff 						YYERROR;
21353b3a8eb9SGleb Smirnoff 					}
21363b3a8eb9SGleb Smirnoff 					srctrack =  o->data.src_track;
21373b3a8eb9SGleb Smirnoff 					r.rule_flag |= PFRULE_SRCTRACK;
21383b3a8eb9SGleb Smirnoff 					break;
21393b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_MAX_SRC_STATES:
21403b3a8eb9SGleb Smirnoff 					if (r.max_src_states) {
21413b3a8eb9SGleb Smirnoff 						yyerror("state option "
21423b3a8eb9SGleb Smirnoff 						    "'max-src-states' "
21433b3a8eb9SGleb Smirnoff 						    "multiple definitions");
21443b3a8eb9SGleb Smirnoff 						YYERROR;
21453b3a8eb9SGleb Smirnoff 					}
21463b3a8eb9SGleb Smirnoff 					if (o->data.max_src_states == 0) {
21473b3a8eb9SGleb Smirnoff 						yyerror("'max-src-states' must "
21483b3a8eb9SGleb Smirnoff 						    "be > 0");
21493b3a8eb9SGleb Smirnoff 						YYERROR;
21503b3a8eb9SGleb Smirnoff 					}
21513b3a8eb9SGleb Smirnoff 					r.max_src_states =
21523b3a8eb9SGleb Smirnoff 					    o->data.max_src_states;
21533b3a8eb9SGleb Smirnoff 					r.rule_flag |= PFRULE_SRCTRACK;
21543b3a8eb9SGleb Smirnoff 					break;
21553b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_OVERLOAD:
21563b3a8eb9SGleb Smirnoff 					if (r.overload_tblname[0]) {
21573b3a8eb9SGleb Smirnoff 						yyerror("multiple 'overload' "
21583b3a8eb9SGleb Smirnoff 						    "table definitions");
21593b3a8eb9SGleb Smirnoff 						YYERROR;
21603b3a8eb9SGleb Smirnoff 					}
21613b3a8eb9SGleb Smirnoff 					if (strlcpy(r.overload_tblname,
21623b3a8eb9SGleb Smirnoff 					    o->data.overload.tblname,
21633b3a8eb9SGleb Smirnoff 					    PF_TABLE_NAME_SIZE) >=
21643b3a8eb9SGleb Smirnoff 					    PF_TABLE_NAME_SIZE) {
21653b3a8eb9SGleb Smirnoff 						yyerror("state option: "
21663b3a8eb9SGleb Smirnoff 						    "strlcpy");
21673b3a8eb9SGleb Smirnoff 						YYERROR;
21683b3a8eb9SGleb Smirnoff 					}
21693b3a8eb9SGleb Smirnoff 					r.flush = o->data.overload.flush;
21703b3a8eb9SGleb Smirnoff 					break;
21713b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_MAX_SRC_CONN:
21723b3a8eb9SGleb Smirnoff 					if (r.max_src_conn) {
21733b3a8eb9SGleb Smirnoff 						yyerror("state option "
21743b3a8eb9SGleb Smirnoff 						    "'max-src-conn' "
21753b3a8eb9SGleb Smirnoff 						    "multiple definitions");
21763b3a8eb9SGleb Smirnoff 						YYERROR;
21773b3a8eb9SGleb Smirnoff 					}
21783b3a8eb9SGleb Smirnoff 					if (o->data.max_src_conn == 0) {
21793b3a8eb9SGleb Smirnoff 						yyerror("'max-src-conn' "
21803b3a8eb9SGleb Smirnoff 						    "must be > 0");
21813b3a8eb9SGleb Smirnoff 						YYERROR;
21823b3a8eb9SGleb Smirnoff 					}
21833b3a8eb9SGleb Smirnoff 					r.max_src_conn =
21843b3a8eb9SGleb Smirnoff 					    o->data.max_src_conn;
21853b3a8eb9SGleb Smirnoff 					r.rule_flag |= PFRULE_SRCTRACK |
21863b3a8eb9SGleb Smirnoff 					    PFRULE_RULESRCTRACK;
21873b3a8eb9SGleb Smirnoff 					break;
21883b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_MAX_SRC_CONN_RATE:
21893b3a8eb9SGleb Smirnoff 					if (r.max_src_conn_rate.limit) {
21903b3a8eb9SGleb Smirnoff 						yyerror("state option "
21913b3a8eb9SGleb Smirnoff 						    "'max-src-conn-rate' "
21923b3a8eb9SGleb Smirnoff 						    "multiple definitions");
21933b3a8eb9SGleb Smirnoff 						YYERROR;
21943b3a8eb9SGleb Smirnoff 					}
21953b3a8eb9SGleb Smirnoff 					if (!o->data.max_src_conn_rate.limit ||
21963b3a8eb9SGleb Smirnoff 					    !o->data.max_src_conn_rate.seconds) {
21973b3a8eb9SGleb Smirnoff 						yyerror("'max-src-conn-rate' "
21983b3a8eb9SGleb Smirnoff 						    "values must be > 0");
21993b3a8eb9SGleb Smirnoff 						YYERROR;
22003b3a8eb9SGleb Smirnoff 					}
22013b3a8eb9SGleb Smirnoff 					if (o->data.max_src_conn_rate.limit >
22023b3a8eb9SGleb Smirnoff 					    PF_THRESHOLD_MAX) {
22033b3a8eb9SGleb Smirnoff 						yyerror("'max-src-conn-rate' "
22043b3a8eb9SGleb Smirnoff 						    "maximum rate must be < %u",
22053b3a8eb9SGleb Smirnoff 						    PF_THRESHOLD_MAX);
22063b3a8eb9SGleb Smirnoff 						YYERROR;
22073b3a8eb9SGleb Smirnoff 					}
22083b3a8eb9SGleb Smirnoff 					r.max_src_conn_rate.limit =
22093b3a8eb9SGleb Smirnoff 					    o->data.max_src_conn_rate.limit;
22103b3a8eb9SGleb Smirnoff 					r.max_src_conn_rate.seconds =
22113b3a8eb9SGleb Smirnoff 					    o->data.max_src_conn_rate.seconds;
22123b3a8eb9SGleb Smirnoff 					r.rule_flag |= PFRULE_SRCTRACK |
22133b3a8eb9SGleb Smirnoff 					    PFRULE_RULESRCTRACK;
22143b3a8eb9SGleb Smirnoff 					break;
22153b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_MAX_SRC_NODES:
22163b3a8eb9SGleb Smirnoff 					if (r.max_src_nodes) {
22173b3a8eb9SGleb Smirnoff 						yyerror("state option "
22183b3a8eb9SGleb Smirnoff 						    "'max-src-nodes' "
22193b3a8eb9SGleb Smirnoff 						    "multiple definitions");
22203b3a8eb9SGleb Smirnoff 						YYERROR;
22213b3a8eb9SGleb Smirnoff 					}
22223b3a8eb9SGleb Smirnoff 					if (o->data.max_src_nodes == 0) {
22233b3a8eb9SGleb Smirnoff 						yyerror("'max-src-nodes' must "
22243b3a8eb9SGleb Smirnoff 						    "be > 0");
22253b3a8eb9SGleb Smirnoff 						YYERROR;
22263b3a8eb9SGleb Smirnoff 					}
22273b3a8eb9SGleb Smirnoff 					r.max_src_nodes =
22283b3a8eb9SGleb Smirnoff 					    o->data.max_src_nodes;
22293b3a8eb9SGleb Smirnoff 					r.rule_flag |= PFRULE_SRCTRACK |
22303b3a8eb9SGleb Smirnoff 					    PFRULE_RULESRCTRACK;
22313b3a8eb9SGleb Smirnoff 					break;
22323b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_STATELOCK:
22333b3a8eb9SGleb Smirnoff 					if (statelock) {
22343b3a8eb9SGleb Smirnoff 						yyerror("state locking option: "
22353b3a8eb9SGleb Smirnoff 						    "multiple definitions");
22363b3a8eb9SGleb Smirnoff 						YYERROR;
22373b3a8eb9SGleb Smirnoff 					}
22383b3a8eb9SGleb Smirnoff 					statelock = 1;
22393b3a8eb9SGleb Smirnoff 					r.rule_flag |= o->data.statelock;
22403b3a8eb9SGleb Smirnoff 					break;
22413b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_SLOPPY:
22423b3a8eb9SGleb Smirnoff 					if (r.rule_flag & PFRULE_STATESLOPPY) {
22433b3a8eb9SGleb Smirnoff 						yyerror("state sloppy option: "
22443b3a8eb9SGleb Smirnoff 						    "multiple definitions");
22453b3a8eb9SGleb Smirnoff 						YYERROR;
22463b3a8eb9SGleb Smirnoff 					}
22473b3a8eb9SGleb Smirnoff 					r.rule_flag |= PFRULE_STATESLOPPY;
22483b3a8eb9SGleb Smirnoff 					break;
22493b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_TIMEOUT:
22503b3a8eb9SGleb Smirnoff 					if (o->data.timeout.number ==
22513b3a8eb9SGleb Smirnoff 					    PFTM_ADAPTIVE_START ||
22523b3a8eb9SGleb Smirnoff 					    o->data.timeout.number ==
22533b3a8eb9SGleb Smirnoff 					    PFTM_ADAPTIVE_END)
22543b3a8eb9SGleb Smirnoff 						adaptive = 1;
22553b3a8eb9SGleb Smirnoff 					if (r.timeout[o->data.timeout.number]) {
22563b3a8eb9SGleb Smirnoff 						yyerror("state timeout %s "
22573b3a8eb9SGleb Smirnoff 						    "multiple definitions",
22583b3a8eb9SGleb Smirnoff 						    pf_timeouts[o->data.
22593b3a8eb9SGleb Smirnoff 						    timeout.number].name);
22603b3a8eb9SGleb Smirnoff 						YYERROR;
22613b3a8eb9SGleb Smirnoff 					}
22623b3a8eb9SGleb Smirnoff 					r.timeout[o->data.timeout.number] =
22633b3a8eb9SGleb Smirnoff 					    o->data.timeout.seconds;
22643b3a8eb9SGleb Smirnoff 				}
22653b3a8eb9SGleb Smirnoff 				o = o->next;
22663b3a8eb9SGleb Smirnoff 				if (!defaults)
22673b3a8eb9SGleb Smirnoff 					free(p);
22683b3a8eb9SGleb Smirnoff 			}
22693b3a8eb9SGleb Smirnoff 
22703b3a8eb9SGleb Smirnoff 			/* 'flags S/SA' by default on stateful rules */
22713b3a8eb9SGleb Smirnoff 			if (!r.action && !r.flags && !r.flagset &&
22723b3a8eb9SGleb Smirnoff 			    !$9.fragment && !($9.marker & FOM_FLAGS) &&
22733b3a8eb9SGleb Smirnoff 			    r.keep_state) {
22743b3a8eb9SGleb Smirnoff 				r.flags = parse_flags("S");
22753b3a8eb9SGleb Smirnoff 				r.flagset =  parse_flags("SA");
22763b3a8eb9SGleb Smirnoff 			}
22773b3a8eb9SGleb Smirnoff 			if (!adaptive && r.max_states) {
22783b3a8eb9SGleb Smirnoff 				r.timeout[PFTM_ADAPTIVE_START] =
22793b3a8eb9SGleb Smirnoff 				    (r.max_states / 10) * 6;
22803b3a8eb9SGleb Smirnoff 				r.timeout[PFTM_ADAPTIVE_END] =
22813b3a8eb9SGleb Smirnoff 				    (r.max_states / 10) * 12;
22823b3a8eb9SGleb Smirnoff 			}
22833b3a8eb9SGleb Smirnoff 			if (r.rule_flag & PFRULE_SRCTRACK) {
22843b3a8eb9SGleb Smirnoff 				if (srctrack == PF_SRCTRACK_GLOBAL &&
22853b3a8eb9SGleb Smirnoff 				    r.max_src_nodes) {
22863b3a8eb9SGleb Smirnoff 					yyerror("'max-src-nodes' is "
22873b3a8eb9SGleb Smirnoff 					    "incompatible with "
22883b3a8eb9SGleb Smirnoff 					    "'source-track global'");
22893b3a8eb9SGleb Smirnoff 					YYERROR;
22903b3a8eb9SGleb Smirnoff 				}
22913b3a8eb9SGleb Smirnoff 				if (srctrack == PF_SRCTRACK_GLOBAL &&
22923b3a8eb9SGleb Smirnoff 				    r.max_src_conn) {
22933b3a8eb9SGleb Smirnoff 					yyerror("'max-src-conn' is "
22943b3a8eb9SGleb Smirnoff 					    "incompatible with "
22953b3a8eb9SGleb Smirnoff 					    "'source-track global'");
22963b3a8eb9SGleb Smirnoff 					YYERROR;
22973b3a8eb9SGleb Smirnoff 				}
22983b3a8eb9SGleb Smirnoff 				if (srctrack == PF_SRCTRACK_GLOBAL &&
22993b3a8eb9SGleb Smirnoff 				    r.max_src_conn_rate.seconds) {
23003b3a8eb9SGleb Smirnoff 					yyerror("'max-src-conn-rate' is "
23013b3a8eb9SGleb Smirnoff 					    "incompatible with "
23023b3a8eb9SGleb Smirnoff 					    "'source-track global'");
23033b3a8eb9SGleb Smirnoff 					YYERROR;
23043b3a8eb9SGleb Smirnoff 				}
23053b3a8eb9SGleb Smirnoff 				if (r.timeout[PFTM_SRC_NODE] <
23063b3a8eb9SGleb Smirnoff 				    r.max_src_conn_rate.seconds)
23073b3a8eb9SGleb Smirnoff 					r.timeout[PFTM_SRC_NODE] =
23083b3a8eb9SGleb Smirnoff 					    r.max_src_conn_rate.seconds;
23093b3a8eb9SGleb Smirnoff 				r.rule_flag |= PFRULE_SRCTRACK;
23103b3a8eb9SGleb Smirnoff 				if (srctrack == PF_SRCTRACK_RULE)
23113b3a8eb9SGleb Smirnoff 					r.rule_flag |= PFRULE_RULESRCTRACK;
23123b3a8eb9SGleb Smirnoff 			}
23133b3a8eb9SGleb Smirnoff 			if (r.keep_state && !statelock)
23143b3a8eb9SGleb Smirnoff 				r.rule_flag |= default_statelock;
23153b3a8eb9SGleb Smirnoff 
23163b3a8eb9SGleb Smirnoff 			if ($9.fragment)
23173b3a8eb9SGleb Smirnoff 				r.rule_flag |= PFRULE_FRAGMENT;
23183b3a8eb9SGleb Smirnoff 			r.allow_opts = $9.allowopts;
23193b3a8eb9SGleb Smirnoff 
23203b3a8eb9SGleb Smirnoff 			decide_address_family($8.src.host, &r.af);
23213b3a8eb9SGleb Smirnoff 			decide_address_family($8.dst.host, &r.af);
23223b3a8eb9SGleb Smirnoff 
23233b3a8eb9SGleb Smirnoff 			if ($5.rt) {
23243b3a8eb9SGleb Smirnoff 				if (!r.direction) {
23253b3a8eb9SGleb Smirnoff 					yyerror("direction must be explicit "
23263b3a8eb9SGleb Smirnoff 					    "with rules that specify routing");
23273b3a8eb9SGleb Smirnoff 					YYERROR;
23283b3a8eb9SGleb Smirnoff 				}
23293b3a8eb9SGleb Smirnoff 				r.rt = $5.rt;
23303b3a8eb9SGleb Smirnoff 				r.rpool.opts = $5.pool_opts;
23313b3a8eb9SGleb Smirnoff 				if ($5.key != NULL)
23323b3a8eb9SGleb Smirnoff 					memcpy(&r.rpool.key, $5.key,
23333b3a8eb9SGleb Smirnoff 					    sizeof(struct pf_poolhashkey));
23343b3a8eb9SGleb Smirnoff 			}
2335813196a1SKristof Provost 			if (r.rt) {
23363b3a8eb9SGleb Smirnoff 				decide_address_family($5.host, &r.af);
23373b3a8eb9SGleb Smirnoff 				remove_invalid_hosts(&$5.host, &r.af);
23383b3a8eb9SGleb Smirnoff 				if ($5.host == NULL) {
23393b3a8eb9SGleb Smirnoff 					yyerror("no routing address with "
23403b3a8eb9SGleb Smirnoff 					    "matching address family found.");
23413b3a8eb9SGleb Smirnoff 					YYERROR;
23423b3a8eb9SGleb Smirnoff 				}
23433b3a8eb9SGleb Smirnoff 				if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
23443b3a8eb9SGleb Smirnoff 				    PF_POOL_NONE && ($5.host->next != NULL ||
23453b3a8eb9SGleb Smirnoff 				    $5.host->addr.type == PF_ADDR_TABLE ||
23463b3a8eb9SGleb Smirnoff 				    DYNIF_MULTIADDR($5.host->addr)))
23473b3a8eb9SGleb Smirnoff 					r.rpool.opts |= PF_POOL_ROUNDROBIN;
23483b3a8eb9SGleb Smirnoff 				if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
23493b3a8eb9SGleb Smirnoff 				    PF_POOL_ROUNDROBIN &&
23503b3a8eb9SGleb Smirnoff 				    disallow_table($5.host, "tables are only "
23513b3a8eb9SGleb Smirnoff 				    "supported in round-robin routing pools"))
23523b3a8eb9SGleb Smirnoff 					YYERROR;
23533b3a8eb9SGleb Smirnoff 				if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
23543b3a8eb9SGleb Smirnoff 				    PF_POOL_ROUNDROBIN &&
23553b3a8eb9SGleb Smirnoff 				    disallow_alias($5.host, "interface (%s) "
23563b3a8eb9SGleb Smirnoff 				    "is only supported in round-robin "
23573b3a8eb9SGleb Smirnoff 				    "routing pools"))
23583b3a8eb9SGleb Smirnoff 					YYERROR;
23593b3a8eb9SGleb Smirnoff 				if ($5.host->next != NULL) {
23603b3a8eb9SGleb Smirnoff 					if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
23613b3a8eb9SGleb Smirnoff 					    PF_POOL_ROUNDROBIN) {
23623b3a8eb9SGleb Smirnoff 						yyerror("r.rpool.opts must "
23633b3a8eb9SGleb Smirnoff 						    "be PF_POOL_ROUNDROBIN");
23643b3a8eb9SGleb Smirnoff 						YYERROR;
23653b3a8eb9SGleb Smirnoff 					}
23663b3a8eb9SGleb Smirnoff 				}
23673b3a8eb9SGleb Smirnoff 			}
23683b3a8eb9SGleb Smirnoff 			if ($9.queues.qname != NULL) {
23693b3a8eb9SGleb Smirnoff 				if (strlcpy(r.qname, $9.queues.qname,
23703b3a8eb9SGleb Smirnoff 				    sizeof(r.qname)) >= sizeof(r.qname)) {
23713b3a8eb9SGleb Smirnoff 					yyerror("rule qname too long (max "
23723b3a8eb9SGleb Smirnoff 					    "%d chars)", sizeof(r.qname)-1);
23733b3a8eb9SGleb Smirnoff 					YYERROR;
23743b3a8eb9SGleb Smirnoff 				}
23753b3a8eb9SGleb Smirnoff 				free($9.queues.qname);
23763b3a8eb9SGleb Smirnoff 			}
23773b3a8eb9SGleb Smirnoff 			if ($9.queues.pqname != NULL) {
23783b3a8eb9SGleb Smirnoff 				if (strlcpy(r.pqname, $9.queues.pqname,
23793b3a8eb9SGleb Smirnoff 				    sizeof(r.pqname)) >= sizeof(r.pqname)) {
23803b3a8eb9SGleb Smirnoff 					yyerror("rule pqname too long (max "
23813b3a8eb9SGleb Smirnoff 					    "%d chars)", sizeof(r.pqname)-1);
23823b3a8eb9SGleb Smirnoff 					YYERROR;
23833b3a8eb9SGleb Smirnoff 				}
23843b3a8eb9SGleb Smirnoff 				free($9.queues.pqname);
23853b3a8eb9SGleb Smirnoff 			}
23863b3a8eb9SGleb Smirnoff #ifdef __FreeBSD__
23873b3a8eb9SGleb Smirnoff 			r.divert.port = $9.divert.port;
23883b3a8eb9SGleb Smirnoff #else
23893b3a8eb9SGleb Smirnoff 			if ((r.divert.port = $9.divert.port)) {
23903b3a8eb9SGleb Smirnoff 				if (r.direction == PF_OUT) {
23913b3a8eb9SGleb Smirnoff 					if ($9.divert.addr) {
23923b3a8eb9SGleb Smirnoff 						yyerror("address specified "
23933b3a8eb9SGleb Smirnoff 						    "for outgoing divert");
23943b3a8eb9SGleb Smirnoff 						YYERROR;
23953b3a8eb9SGleb Smirnoff 					}
23963b3a8eb9SGleb Smirnoff 					bzero(&r.divert.addr,
23973b3a8eb9SGleb Smirnoff 					    sizeof(r.divert.addr));
23983b3a8eb9SGleb Smirnoff 				} else {
23993b3a8eb9SGleb Smirnoff 					if (!$9.divert.addr) {
24003b3a8eb9SGleb Smirnoff 						yyerror("no address specified "
24013b3a8eb9SGleb Smirnoff 						    "for incoming divert");
24023b3a8eb9SGleb Smirnoff 						YYERROR;
24033b3a8eb9SGleb Smirnoff 					}
24043b3a8eb9SGleb Smirnoff 					if ($9.divert.addr->af != r.af) {
24053b3a8eb9SGleb Smirnoff 						yyerror("address family "
24063b3a8eb9SGleb Smirnoff 						    "mismatch for divert");
24073b3a8eb9SGleb Smirnoff 						YYERROR;
24083b3a8eb9SGleb Smirnoff 					}
24093b3a8eb9SGleb Smirnoff 					r.divert.addr =
24103b3a8eb9SGleb Smirnoff 					    $9.divert.addr->addr.v.a.addr;
24113b3a8eb9SGleb Smirnoff 				}
24123b3a8eb9SGleb Smirnoff 			}
24133b3a8eb9SGleb Smirnoff #endif
24143b3a8eb9SGleb Smirnoff 
24153b3a8eb9SGleb Smirnoff 			expand_rule(&r, $4, $5.host, $7, $8.src_os,
24163b3a8eb9SGleb Smirnoff 			    $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
24173b3a8eb9SGleb Smirnoff 			    $9.uid, $9.gid, $9.icmpspec, "");
24183b3a8eb9SGleb Smirnoff 		}
24193b3a8eb9SGleb Smirnoff 		;
24203b3a8eb9SGleb Smirnoff 
24213b3a8eb9SGleb Smirnoff filter_opts	:	{
24223b3a8eb9SGleb Smirnoff 				bzero(&filter_opts, sizeof filter_opts);
24233b3a8eb9SGleb Smirnoff 				filter_opts.rtableid = -1;
24243b3a8eb9SGleb Smirnoff 			}
24253b3a8eb9SGleb Smirnoff 		    filter_opts_l
24263b3a8eb9SGleb Smirnoff 			{ $$ = filter_opts; }
24273b3a8eb9SGleb Smirnoff 		| /* empty */	{
24283b3a8eb9SGleb Smirnoff 			bzero(&filter_opts, sizeof filter_opts);
24293b3a8eb9SGleb Smirnoff 			filter_opts.rtableid = -1;
24303b3a8eb9SGleb Smirnoff 			$$ = filter_opts;
24313b3a8eb9SGleb Smirnoff 		}
24323b3a8eb9SGleb Smirnoff 		;
24333b3a8eb9SGleb Smirnoff 
24343b3a8eb9SGleb Smirnoff filter_opts_l	: filter_opts_l filter_opt
24353b3a8eb9SGleb Smirnoff 		| filter_opt
24363b3a8eb9SGleb Smirnoff 		;
24373b3a8eb9SGleb Smirnoff 
24383b3a8eb9SGleb Smirnoff filter_opt	: USER uids {
24393b3a8eb9SGleb Smirnoff 			if (filter_opts.uid)
24403b3a8eb9SGleb Smirnoff 				$2->tail->next = filter_opts.uid;
24413b3a8eb9SGleb Smirnoff 			filter_opts.uid = $2;
24423b3a8eb9SGleb Smirnoff 		}
24433b3a8eb9SGleb Smirnoff 		| GROUP gids {
24443b3a8eb9SGleb Smirnoff 			if (filter_opts.gid)
24453b3a8eb9SGleb Smirnoff 				$2->tail->next = filter_opts.gid;
24463b3a8eb9SGleb Smirnoff 			filter_opts.gid = $2;
24473b3a8eb9SGleb Smirnoff 		}
24483b3a8eb9SGleb Smirnoff 		| flags {
24493b3a8eb9SGleb Smirnoff 			if (filter_opts.marker & FOM_FLAGS) {
24503b3a8eb9SGleb Smirnoff 				yyerror("flags cannot be redefined");
24513b3a8eb9SGleb Smirnoff 				YYERROR;
24523b3a8eb9SGleb Smirnoff 			}
24533b3a8eb9SGleb Smirnoff 			filter_opts.marker |= FOM_FLAGS;
24543b3a8eb9SGleb Smirnoff 			filter_opts.flags.b1 |= $1.b1;
24553b3a8eb9SGleb Smirnoff 			filter_opts.flags.b2 |= $1.b2;
24563b3a8eb9SGleb Smirnoff 			filter_opts.flags.w |= $1.w;
24573b3a8eb9SGleb Smirnoff 			filter_opts.flags.w2 |= $1.w2;
24583b3a8eb9SGleb Smirnoff 		}
24593b3a8eb9SGleb Smirnoff 		| icmpspec {
24603b3a8eb9SGleb Smirnoff 			if (filter_opts.marker & FOM_ICMP) {
24613b3a8eb9SGleb Smirnoff 				yyerror("icmp-type cannot be redefined");
24623b3a8eb9SGleb Smirnoff 				YYERROR;
24633b3a8eb9SGleb Smirnoff 			}
24643b3a8eb9SGleb Smirnoff 			filter_opts.marker |= FOM_ICMP;
24653b3a8eb9SGleb Smirnoff 			filter_opts.icmpspec = $1;
24663b3a8eb9SGleb Smirnoff 		}
24673e248e0fSKristof Provost 		| PRIO NUMBER {
24683e248e0fSKristof Provost 			if (filter_opts.marker & FOM_PRIO) {
24693e248e0fSKristof Provost 				yyerror("prio cannot be redefined");
24703e248e0fSKristof Provost 				YYERROR;
24713e248e0fSKristof Provost 			}
24723e248e0fSKristof Provost 			if ($2 < 0 || $2 > PF_PRIO_MAX) {
24733e248e0fSKristof Provost 				yyerror("prio must be 0 - %u", PF_PRIO_MAX);
24743e248e0fSKristof Provost 				YYERROR;
24753e248e0fSKristof Provost 			}
24763e248e0fSKristof Provost 			filter_opts.marker |= FOM_PRIO;
24773e248e0fSKristof Provost 			filter_opts.prio = $2;
24783e248e0fSKristof Provost 		}
24793b3a8eb9SGleb Smirnoff 		| TOS tos {
24803b3a8eb9SGleb Smirnoff 			if (filter_opts.marker & FOM_TOS) {
24813b3a8eb9SGleb Smirnoff 				yyerror("tos cannot be redefined");
24823b3a8eb9SGleb Smirnoff 				YYERROR;
24833b3a8eb9SGleb Smirnoff 			}
24843b3a8eb9SGleb Smirnoff 			filter_opts.marker |= FOM_TOS;
24853b3a8eb9SGleb Smirnoff 			filter_opts.tos = $2;
24863b3a8eb9SGleb Smirnoff 		}
24873b3a8eb9SGleb Smirnoff 		| keep {
24883b3a8eb9SGleb Smirnoff 			if (filter_opts.marker & FOM_KEEP) {
24893b3a8eb9SGleb Smirnoff 				yyerror("modulate or keep cannot be redefined");
24903b3a8eb9SGleb Smirnoff 				YYERROR;
24913b3a8eb9SGleb Smirnoff 			}
24923b3a8eb9SGleb Smirnoff 			filter_opts.marker |= FOM_KEEP;
24933b3a8eb9SGleb Smirnoff 			filter_opts.keep.action = $1.action;
24943b3a8eb9SGleb Smirnoff 			filter_opts.keep.options = $1.options;
24953b3a8eb9SGleb Smirnoff 		}
24963b3a8eb9SGleb Smirnoff 		| FRAGMENT {
24973b3a8eb9SGleb Smirnoff 			filter_opts.fragment = 1;
24983b3a8eb9SGleb Smirnoff 		}
24993b3a8eb9SGleb Smirnoff 		| ALLOWOPTS {
25003b3a8eb9SGleb Smirnoff 			filter_opts.allowopts = 1;
25013b3a8eb9SGleb Smirnoff 		}
25023b3a8eb9SGleb Smirnoff 		| label	{
25033b3a8eb9SGleb Smirnoff 			if (filter_opts.label) {
25043b3a8eb9SGleb Smirnoff 				yyerror("label cannot be redefined");
25053b3a8eb9SGleb Smirnoff 				YYERROR;
25063b3a8eb9SGleb Smirnoff 			}
25073b3a8eb9SGleb Smirnoff 			filter_opts.label = $1;
25083b3a8eb9SGleb Smirnoff 		}
25093b3a8eb9SGleb Smirnoff 		| qname	{
25103b3a8eb9SGleb Smirnoff 			if (filter_opts.queues.qname) {
25113b3a8eb9SGleb Smirnoff 				yyerror("queue cannot be redefined");
25123b3a8eb9SGleb Smirnoff 				YYERROR;
25133b3a8eb9SGleb Smirnoff 			}
25143b3a8eb9SGleb Smirnoff 			filter_opts.queues = $1;
25153b3a8eb9SGleb Smirnoff 		}
25163b3a8eb9SGleb Smirnoff 		| TAG string				{
25173b3a8eb9SGleb Smirnoff 			filter_opts.tag = $2;
25183b3a8eb9SGleb Smirnoff 		}
25193b3a8eb9SGleb Smirnoff 		| not TAGGED string			{
25203b3a8eb9SGleb Smirnoff 			filter_opts.match_tag = $3;
25213b3a8eb9SGleb Smirnoff 			filter_opts.match_tag_not = $1;
25223b3a8eb9SGleb Smirnoff 		}
25233b3a8eb9SGleb Smirnoff 		| PROBABILITY probability		{
25243b3a8eb9SGleb Smirnoff 			double	p;
25253b3a8eb9SGleb Smirnoff 
25263b3a8eb9SGleb Smirnoff 			p = floor($2 * UINT_MAX + 0.5);
25273b3a8eb9SGleb Smirnoff 			if (p < 0.0 || p > UINT_MAX) {
25283b3a8eb9SGleb Smirnoff 				yyerror("invalid probability: %lf", p);
25293b3a8eb9SGleb Smirnoff 				YYERROR;
25303b3a8eb9SGleb Smirnoff 			}
25313b3a8eb9SGleb Smirnoff 			filter_opts.prob = (u_int32_t)p;
25323b3a8eb9SGleb Smirnoff 			if (filter_opts.prob == 0)
25333b3a8eb9SGleb Smirnoff 				filter_opts.prob = 1;
25343b3a8eb9SGleb Smirnoff 		}
25353b3a8eb9SGleb Smirnoff 		| RTABLE NUMBER				{
25363b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > rt_tableid_max()) {
25373b3a8eb9SGleb Smirnoff 				yyerror("invalid rtable id");
25383b3a8eb9SGleb Smirnoff 				YYERROR;
25393b3a8eb9SGleb Smirnoff 			}
25403b3a8eb9SGleb Smirnoff 			filter_opts.rtableid = $2;
25413b3a8eb9SGleb Smirnoff 		}
25423b3a8eb9SGleb Smirnoff 		| DIVERTTO portplain {
25433b3a8eb9SGleb Smirnoff #ifdef __FreeBSD__
25443b3a8eb9SGleb Smirnoff 			filter_opts.divert.port = $2.a;
25453b3a8eb9SGleb Smirnoff 			if (!filter_opts.divert.port) {
25463b3a8eb9SGleb Smirnoff 				yyerror("invalid divert port: %u", ntohs($2.a));
25473b3a8eb9SGleb Smirnoff 				YYERROR;
25483b3a8eb9SGleb Smirnoff 			}
25493b3a8eb9SGleb Smirnoff #endif
25503b3a8eb9SGleb Smirnoff 		}
25513b3a8eb9SGleb Smirnoff 		| DIVERTTO STRING PORT portplain {
25523b3a8eb9SGleb Smirnoff #ifndef __FreeBSD__
25533b3a8eb9SGleb Smirnoff 			if ((filter_opts.divert.addr = host($2)) == NULL) {
25543b3a8eb9SGleb Smirnoff 				yyerror("could not parse divert address: %s",
25553b3a8eb9SGleb Smirnoff 				    $2);
25563b3a8eb9SGleb Smirnoff 				free($2);
25573b3a8eb9SGleb Smirnoff 				YYERROR;
25583b3a8eb9SGleb Smirnoff 			}
25593b3a8eb9SGleb Smirnoff #else
25603b3a8eb9SGleb Smirnoff 			if ($2)
25613b3a8eb9SGleb Smirnoff #endif
25623b3a8eb9SGleb Smirnoff 			free($2);
25633b3a8eb9SGleb Smirnoff 			filter_opts.divert.port = $4.a;
25643b3a8eb9SGleb Smirnoff 			if (!filter_opts.divert.port) {
25653b3a8eb9SGleb Smirnoff 				yyerror("invalid divert port: %u", ntohs($4.a));
25663b3a8eb9SGleb Smirnoff 				YYERROR;
25673b3a8eb9SGleb Smirnoff 			}
25683b3a8eb9SGleb Smirnoff 		}
25693b3a8eb9SGleb Smirnoff 		| DIVERTREPLY {
25703b3a8eb9SGleb Smirnoff #ifdef __FreeBSD__
25713b3a8eb9SGleb Smirnoff 			yyerror("divert-reply has no meaning in FreeBSD pf(4)");
25723b3a8eb9SGleb Smirnoff 			YYERROR;
25733b3a8eb9SGleb Smirnoff #else
25743b3a8eb9SGleb Smirnoff 			filter_opts.divert.port = 1;	/* some random value */
25753b3a8eb9SGleb Smirnoff #endif
25763b3a8eb9SGleb Smirnoff 		}
25773e248e0fSKristof Provost 		| filter_sets
25783e248e0fSKristof Provost 		;
25793e248e0fSKristof Provost 
25803e248e0fSKristof Provost filter_sets	: SET '(' filter_sets_l ')'	{ $$ = filter_opts; }
25813e248e0fSKristof Provost 		| SET filter_set		{ $$ = filter_opts; }
25823e248e0fSKristof Provost 		;
25833e248e0fSKristof Provost 
25843e248e0fSKristof Provost filter_sets_l	: filter_sets_l comma filter_set
25853e248e0fSKristof Provost 		| filter_set
25863e248e0fSKristof Provost 		;
25873e248e0fSKristof Provost 
25883e248e0fSKristof Provost filter_set	: prio {
25893e248e0fSKristof Provost 			if (filter_opts.marker & FOM_SETPRIO) {
25903e248e0fSKristof Provost 				yyerror("prio cannot be redefined");
25913e248e0fSKristof Provost 				YYERROR;
25923e248e0fSKristof Provost 			}
25933e248e0fSKristof Provost 			filter_opts.marker |= FOM_SETPRIO;
25943e248e0fSKristof Provost 			filter_opts.set_prio[0] = $1.b1;
25953e248e0fSKristof Provost 			filter_opts.set_prio[1] = $1.b2;
25963e248e0fSKristof Provost 		}
25973e248e0fSKristof Provost prio		: PRIO NUMBER {
25983e248e0fSKristof Provost 			if ($2 < 0 || $2 > PF_PRIO_MAX) {
25993e248e0fSKristof Provost 				yyerror("prio must be 0 - %u", PF_PRIO_MAX);
26003e248e0fSKristof Provost 				YYERROR;
26013e248e0fSKristof Provost 			}
26023e248e0fSKristof Provost 			$$.b1 = $$.b2 = $2;
26033e248e0fSKristof Provost 		}
26043e248e0fSKristof Provost 		| PRIO '(' NUMBER comma NUMBER ')' {
26053e248e0fSKristof Provost 			if ($3 < 0 || $3 > PF_PRIO_MAX ||
26063e248e0fSKristof Provost 			    $5 < 0 || $5 > PF_PRIO_MAX) {
26073e248e0fSKristof Provost 				yyerror("prio must be 0 - %u", PF_PRIO_MAX);
26083e248e0fSKristof Provost 				YYERROR;
26093e248e0fSKristof Provost 			}
26103e248e0fSKristof Provost 			$$.b1 = $3;
26113e248e0fSKristof Provost 			$$.b2 = $5;
26123e248e0fSKristof Provost 		}
26133b3a8eb9SGleb Smirnoff 		;
26143b3a8eb9SGleb Smirnoff 
26153b3a8eb9SGleb Smirnoff probability	: STRING				{
26163b3a8eb9SGleb Smirnoff 			char	*e;
26173b3a8eb9SGleb Smirnoff 			double	 p = strtod($1, &e);
26183b3a8eb9SGleb Smirnoff 
26193b3a8eb9SGleb Smirnoff 			if (*e == '%') {
26203b3a8eb9SGleb Smirnoff 				p *= 0.01;
26213b3a8eb9SGleb Smirnoff 				e++;
26223b3a8eb9SGleb Smirnoff 			}
26233b3a8eb9SGleb Smirnoff 			if (*e) {
26243b3a8eb9SGleb Smirnoff 				yyerror("invalid probability: %s", $1);
26253b3a8eb9SGleb Smirnoff 				free($1);
26263b3a8eb9SGleb Smirnoff 				YYERROR;
26273b3a8eb9SGleb Smirnoff 			}
26283b3a8eb9SGleb Smirnoff 			free($1);
26293b3a8eb9SGleb Smirnoff 			$$ = p;
26303b3a8eb9SGleb Smirnoff 		}
26313b3a8eb9SGleb Smirnoff 		| NUMBER				{
26323b3a8eb9SGleb Smirnoff 			$$ = (double)$1;
26333b3a8eb9SGleb Smirnoff 		}
26343b3a8eb9SGleb Smirnoff 		;
26353b3a8eb9SGleb Smirnoff 
26363b3a8eb9SGleb Smirnoff 
26373b3a8eb9SGleb Smirnoff action		: PASS			{ $$.b1 = PF_PASS; $$.b2 = $$.w = 0; }
26383b3a8eb9SGleb Smirnoff 		| BLOCK blockspec	{ $$ = $2; $$.b1 = PF_DROP; }
26393b3a8eb9SGleb Smirnoff 		;
26403b3a8eb9SGleb Smirnoff 
26413b3a8eb9SGleb Smirnoff blockspec	: /* empty */		{
26423b3a8eb9SGleb Smirnoff 			$$.b2 = blockpolicy;
26433b3a8eb9SGleb Smirnoff 			$$.w = returnicmpdefault;
26443b3a8eb9SGleb Smirnoff 			$$.w2 = returnicmp6default;
26453b3a8eb9SGleb Smirnoff 		}
26463b3a8eb9SGleb Smirnoff 		| DROP			{
26473b3a8eb9SGleb Smirnoff 			$$.b2 = PFRULE_DROP;
26483b3a8eb9SGleb Smirnoff 			$$.w = 0;
26493b3a8eb9SGleb Smirnoff 			$$.w2 = 0;
26503b3a8eb9SGleb Smirnoff 		}
26513b3a8eb9SGleb Smirnoff 		| RETURNRST		{
26523b3a8eb9SGleb Smirnoff 			$$.b2 = PFRULE_RETURNRST;
26533b3a8eb9SGleb Smirnoff 			$$.w = 0;
26543b3a8eb9SGleb Smirnoff 			$$.w2 = 0;
26553b3a8eb9SGleb Smirnoff 		}
26563b3a8eb9SGleb Smirnoff 		| RETURNRST '(' TTL NUMBER ')'	{
26573b3a8eb9SGleb Smirnoff 			if ($4 < 0 || $4 > 255) {
26583b3a8eb9SGleb Smirnoff 				yyerror("illegal ttl value %d", $4);
26593b3a8eb9SGleb Smirnoff 				YYERROR;
26603b3a8eb9SGleb Smirnoff 			}
26613b3a8eb9SGleb Smirnoff 			$$.b2 = PFRULE_RETURNRST;
26623b3a8eb9SGleb Smirnoff 			$$.w = $4;
26633b3a8eb9SGleb Smirnoff 			$$.w2 = 0;
26643b3a8eb9SGleb Smirnoff 		}
26653b3a8eb9SGleb Smirnoff 		| RETURNICMP		{
26663b3a8eb9SGleb Smirnoff 			$$.b2 = PFRULE_RETURNICMP;
26673b3a8eb9SGleb Smirnoff 			$$.w = returnicmpdefault;
26683b3a8eb9SGleb Smirnoff 			$$.w2 = returnicmp6default;
26693b3a8eb9SGleb Smirnoff 		}
26703b3a8eb9SGleb Smirnoff 		| RETURNICMP6		{
26713b3a8eb9SGleb Smirnoff 			$$.b2 = PFRULE_RETURNICMP;
26723b3a8eb9SGleb Smirnoff 			$$.w = returnicmpdefault;
26733b3a8eb9SGleb Smirnoff 			$$.w2 = returnicmp6default;
26743b3a8eb9SGleb Smirnoff 		}
26753b3a8eb9SGleb Smirnoff 		| RETURNICMP '(' reticmpspec ')'	{
26763b3a8eb9SGleb Smirnoff 			$$.b2 = PFRULE_RETURNICMP;
26773b3a8eb9SGleb Smirnoff 			$$.w = $3;
26783b3a8eb9SGleb Smirnoff 			$$.w2 = returnicmpdefault;
26793b3a8eb9SGleb Smirnoff 		}
26803b3a8eb9SGleb Smirnoff 		| RETURNICMP6 '(' reticmp6spec ')'	{
26813b3a8eb9SGleb Smirnoff 			$$.b2 = PFRULE_RETURNICMP;
26823b3a8eb9SGleb Smirnoff 			$$.w = returnicmpdefault;
26833b3a8eb9SGleb Smirnoff 			$$.w2 = $3;
26843b3a8eb9SGleb Smirnoff 		}
26853b3a8eb9SGleb Smirnoff 		| RETURNICMP '(' reticmpspec comma reticmp6spec ')' {
26863b3a8eb9SGleb Smirnoff 			$$.b2 = PFRULE_RETURNICMP;
26873b3a8eb9SGleb Smirnoff 			$$.w = $3;
26883b3a8eb9SGleb Smirnoff 			$$.w2 = $5;
26893b3a8eb9SGleb Smirnoff 		}
26903b3a8eb9SGleb Smirnoff 		| RETURN {
26913b3a8eb9SGleb Smirnoff 			$$.b2 = PFRULE_RETURN;
26923b3a8eb9SGleb Smirnoff 			$$.w = returnicmpdefault;
26933b3a8eb9SGleb Smirnoff 			$$.w2 = returnicmp6default;
26943b3a8eb9SGleb Smirnoff 		}
26953b3a8eb9SGleb Smirnoff 		;
26963b3a8eb9SGleb Smirnoff 
26973b3a8eb9SGleb Smirnoff reticmpspec	: STRING			{
26983b3a8eb9SGleb Smirnoff 			if (!($$ = parseicmpspec($1, AF_INET))) {
26993b3a8eb9SGleb Smirnoff 				free($1);
27003b3a8eb9SGleb Smirnoff 				YYERROR;
27013b3a8eb9SGleb Smirnoff 			}
27023b3a8eb9SGleb Smirnoff 			free($1);
27033b3a8eb9SGleb Smirnoff 		}
27043b3a8eb9SGleb Smirnoff 		| NUMBER			{
27053b3a8eb9SGleb Smirnoff 			u_int8_t		icmptype;
27063b3a8eb9SGleb Smirnoff 
27073b3a8eb9SGleb Smirnoff 			if ($1 < 0 || $1 > 255) {
27083b3a8eb9SGleb Smirnoff 				yyerror("invalid icmp code %lu", $1);
27093b3a8eb9SGleb Smirnoff 				YYERROR;
27103b3a8eb9SGleb Smirnoff 			}
27113b3a8eb9SGleb Smirnoff 			icmptype = returnicmpdefault >> 8;
27123b3a8eb9SGleb Smirnoff 			$$ = (icmptype << 8 | $1);
27133b3a8eb9SGleb Smirnoff 		}
27143b3a8eb9SGleb Smirnoff 		;
27153b3a8eb9SGleb Smirnoff 
27163b3a8eb9SGleb Smirnoff reticmp6spec	: STRING			{
27173b3a8eb9SGleb Smirnoff 			if (!($$ = parseicmpspec($1, AF_INET6))) {
27183b3a8eb9SGleb Smirnoff 				free($1);
27193b3a8eb9SGleb Smirnoff 				YYERROR;
27203b3a8eb9SGleb Smirnoff 			}
27213b3a8eb9SGleb Smirnoff 			free($1);
27223b3a8eb9SGleb Smirnoff 		}
27233b3a8eb9SGleb Smirnoff 		| NUMBER			{
27243b3a8eb9SGleb Smirnoff 			u_int8_t		icmptype;
27253b3a8eb9SGleb Smirnoff 
27263b3a8eb9SGleb Smirnoff 			if ($1 < 0 || $1 > 255) {
27273b3a8eb9SGleb Smirnoff 				yyerror("invalid icmp code %lu", $1);
27283b3a8eb9SGleb Smirnoff 				YYERROR;
27293b3a8eb9SGleb Smirnoff 			}
27303b3a8eb9SGleb Smirnoff 			icmptype = returnicmp6default >> 8;
27313b3a8eb9SGleb Smirnoff 			$$ = (icmptype << 8 | $1);
27323b3a8eb9SGleb Smirnoff 		}
27333b3a8eb9SGleb Smirnoff 		;
27343b3a8eb9SGleb Smirnoff 
27353b3a8eb9SGleb Smirnoff dir		: /* empty */			{ $$ = PF_INOUT; }
27363b3a8eb9SGleb Smirnoff 		| IN				{ $$ = PF_IN; }
27373b3a8eb9SGleb Smirnoff 		| OUT				{ $$ = PF_OUT; }
27383b3a8eb9SGleb Smirnoff 		;
27393b3a8eb9SGleb Smirnoff 
27403b3a8eb9SGleb Smirnoff quick		: /* empty */			{ $$.quick = 0; }
27413b3a8eb9SGleb Smirnoff 		| QUICK				{ $$.quick = 1; }
27423b3a8eb9SGleb Smirnoff 		;
27433b3a8eb9SGleb Smirnoff 
27443b3a8eb9SGleb Smirnoff logquick	: /* empty */	{ $$.log = 0; $$.quick = 0; $$.logif = 0; }
27453b3a8eb9SGleb Smirnoff 		| log		{ $$ = $1; $$.quick = 0; }
27463b3a8eb9SGleb Smirnoff 		| QUICK		{ $$.quick = 1; $$.log = 0; $$.logif = 0; }
27473b3a8eb9SGleb Smirnoff 		| log QUICK	{ $$ = $1; $$.quick = 1; }
27483b3a8eb9SGleb Smirnoff 		| QUICK log	{ $$ = $2; $$.quick = 1; }
27493b3a8eb9SGleb Smirnoff 		;
27503b3a8eb9SGleb Smirnoff 
27513b3a8eb9SGleb Smirnoff log		: LOG			{ $$.log = PF_LOG; $$.logif = 0; }
27523b3a8eb9SGleb Smirnoff 		| LOG '(' logopts ')'	{
27533b3a8eb9SGleb Smirnoff 			$$.log = PF_LOG | $3.log;
27543b3a8eb9SGleb Smirnoff 			$$.logif = $3.logif;
27553b3a8eb9SGleb Smirnoff 		}
27563b3a8eb9SGleb Smirnoff 		;
27573b3a8eb9SGleb Smirnoff 
27583b3a8eb9SGleb Smirnoff logopts		: logopt			{ $$ = $1; }
27593b3a8eb9SGleb Smirnoff 		| logopts comma logopt		{
27603b3a8eb9SGleb Smirnoff 			$$.log = $1.log | $3.log;
27613b3a8eb9SGleb Smirnoff 			$$.logif = $3.logif;
27623b3a8eb9SGleb Smirnoff 			if ($$.logif == 0)
27633b3a8eb9SGleb Smirnoff 				$$.logif = $1.logif;
27643b3a8eb9SGleb Smirnoff 		}
27653b3a8eb9SGleb Smirnoff 		;
27663b3a8eb9SGleb Smirnoff 
27673b3a8eb9SGleb Smirnoff logopt		: ALL		{ $$.log = PF_LOG_ALL; $$.logif = 0; }
27683b3a8eb9SGleb Smirnoff 		| USER		{ $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
27693b3a8eb9SGleb Smirnoff 		| GROUP		{ $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
27703b3a8eb9SGleb Smirnoff 		| TO string	{
27713b3a8eb9SGleb Smirnoff 			const char	*errstr;
27723b3a8eb9SGleb Smirnoff 			u_int		 i;
27733b3a8eb9SGleb Smirnoff 
27743b3a8eb9SGleb Smirnoff 			$$.log = 0;
27753b3a8eb9SGleb Smirnoff 			if (strncmp($2, "pflog", 5)) {
27763b3a8eb9SGleb Smirnoff 				yyerror("%s: should be a pflog interface", $2);
27773b3a8eb9SGleb Smirnoff 				free($2);
27783b3a8eb9SGleb Smirnoff 				YYERROR;
27793b3a8eb9SGleb Smirnoff 			}
27803b3a8eb9SGleb Smirnoff 			i = strtonum($2 + 5, 0, 255, &errstr);
27813b3a8eb9SGleb Smirnoff 			if (errstr) {
27823b3a8eb9SGleb Smirnoff 				yyerror("%s: %s", $2, errstr);
27833b3a8eb9SGleb Smirnoff 				free($2);
27843b3a8eb9SGleb Smirnoff 				YYERROR;
27853b3a8eb9SGleb Smirnoff 			}
27863b3a8eb9SGleb Smirnoff 			free($2);
27873b3a8eb9SGleb Smirnoff 			$$.logif = i;
27883b3a8eb9SGleb Smirnoff 		}
27893b3a8eb9SGleb Smirnoff 		;
27903b3a8eb9SGleb Smirnoff 
27913b3a8eb9SGleb Smirnoff interface	: /* empty */			{ $$ = NULL; }
27923b3a8eb9SGleb Smirnoff 		| ON if_item_not		{ $$ = $2; }
27933b3a8eb9SGleb Smirnoff 		| ON '{' optnl if_list '}'	{ $$ = $4; }
27943b3a8eb9SGleb Smirnoff 		;
27953b3a8eb9SGleb Smirnoff 
27963b3a8eb9SGleb Smirnoff if_list		: if_item_not optnl		{ $$ = $1; }
27973b3a8eb9SGleb Smirnoff 		| if_list comma if_item_not optnl	{
27983b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
27993b3a8eb9SGleb Smirnoff 			$1->tail = $3;
28003b3a8eb9SGleb Smirnoff 			$$ = $1;
28013b3a8eb9SGleb Smirnoff 		}
28023b3a8eb9SGleb Smirnoff 		;
28033b3a8eb9SGleb Smirnoff 
28043b3a8eb9SGleb Smirnoff if_item_not	: not if_item			{ $$ = $2; $$->not = $1; }
28053b3a8eb9SGleb Smirnoff 		;
28063b3a8eb9SGleb Smirnoff 
28073b3a8eb9SGleb Smirnoff if_item		: STRING			{
28083b3a8eb9SGleb Smirnoff 			struct node_host	*n;
28093b3a8eb9SGleb Smirnoff 
28103b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_if));
28113b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
28123b3a8eb9SGleb Smirnoff 				err(1, "if_item: calloc");
28133b3a8eb9SGleb Smirnoff 			if (strlcpy($$->ifname, $1, sizeof($$->ifname)) >=
28143b3a8eb9SGleb Smirnoff 			    sizeof($$->ifname)) {
28153b3a8eb9SGleb Smirnoff 				free($1);
28163b3a8eb9SGleb Smirnoff 				free($$);
28173b3a8eb9SGleb Smirnoff 				yyerror("interface name too long");
28183b3a8eb9SGleb Smirnoff 				YYERROR;
28193b3a8eb9SGleb Smirnoff 			}
28203b3a8eb9SGleb Smirnoff 
28213b3a8eb9SGleb Smirnoff 			if ((n = ifa_exists($1)) != NULL)
28223b3a8eb9SGleb Smirnoff 				$$->ifa_flags = n->ifa_flags;
28233b3a8eb9SGleb Smirnoff 
28243b3a8eb9SGleb Smirnoff 			free($1);
28253b3a8eb9SGleb Smirnoff 			$$->not = 0;
28263b3a8eb9SGleb Smirnoff 			$$->next = NULL;
28273b3a8eb9SGleb Smirnoff 			$$->tail = $$;
28283b3a8eb9SGleb Smirnoff 		}
28293b3a8eb9SGleb Smirnoff 		;
28303b3a8eb9SGleb Smirnoff 
28313b3a8eb9SGleb Smirnoff af		: /* empty */			{ $$ = 0; }
28323b3a8eb9SGleb Smirnoff 		| INET				{ $$ = AF_INET; }
28333b3a8eb9SGleb Smirnoff 		| INET6				{ $$ = AF_INET6; }
28343b3a8eb9SGleb Smirnoff 		;
28353b3a8eb9SGleb Smirnoff 
28363b3a8eb9SGleb Smirnoff proto		: /* empty */				{ $$ = NULL; }
28373b3a8eb9SGleb Smirnoff 		| PROTO proto_item			{ $$ = $2; }
28383b3a8eb9SGleb Smirnoff 		| PROTO '{' optnl proto_list '}'	{ $$ = $4; }
28393b3a8eb9SGleb Smirnoff 		;
28403b3a8eb9SGleb Smirnoff 
28413b3a8eb9SGleb Smirnoff proto_list	: proto_item optnl		{ $$ = $1; }
28423b3a8eb9SGleb Smirnoff 		| proto_list comma proto_item optnl	{
28433b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
28443b3a8eb9SGleb Smirnoff 			$1->tail = $3;
28453b3a8eb9SGleb Smirnoff 			$$ = $1;
28463b3a8eb9SGleb Smirnoff 		}
28473b3a8eb9SGleb Smirnoff 		;
28483b3a8eb9SGleb Smirnoff 
28493b3a8eb9SGleb Smirnoff proto_item	: protoval			{
28503b3a8eb9SGleb Smirnoff 			u_int8_t	pr;
28513b3a8eb9SGleb Smirnoff 
28523b3a8eb9SGleb Smirnoff 			pr = (u_int8_t)$1;
28533b3a8eb9SGleb Smirnoff 			if (pr == 0) {
28543b3a8eb9SGleb Smirnoff 				yyerror("proto 0 cannot be used");
28553b3a8eb9SGleb Smirnoff 				YYERROR;
28563b3a8eb9SGleb Smirnoff 			}
28573b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_proto));
28583b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
28593b3a8eb9SGleb Smirnoff 				err(1, "proto_item: calloc");
28603b3a8eb9SGleb Smirnoff 			$$->proto = pr;
28613b3a8eb9SGleb Smirnoff 			$$->next = NULL;
28623b3a8eb9SGleb Smirnoff 			$$->tail = $$;
28633b3a8eb9SGleb Smirnoff 		}
28643b3a8eb9SGleb Smirnoff 		;
28653b3a8eb9SGleb Smirnoff 
28663b3a8eb9SGleb Smirnoff protoval	: STRING			{
28673b3a8eb9SGleb Smirnoff 			struct protoent	*p;
28683b3a8eb9SGleb Smirnoff 
28693b3a8eb9SGleb Smirnoff 			p = getprotobyname($1);
28703b3a8eb9SGleb Smirnoff 			if (p == NULL) {
28713b3a8eb9SGleb Smirnoff 				yyerror("unknown protocol %s", $1);
28723b3a8eb9SGleb Smirnoff 				free($1);
28733b3a8eb9SGleb Smirnoff 				YYERROR;
28743b3a8eb9SGleb Smirnoff 			}
28753b3a8eb9SGleb Smirnoff 			$$ = p->p_proto;
28763b3a8eb9SGleb Smirnoff 			free($1);
28773b3a8eb9SGleb Smirnoff 		}
28783b3a8eb9SGleb Smirnoff 		| NUMBER			{
28793b3a8eb9SGleb Smirnoff 			if ($1 < 0 || $1 > 255) {
28803b3a8eb9SGleb Smirnoff 				yyerror("protocol outside range");
28813b3a8eb9SGleb Smirnoff 				YYERROR;
28823b3a8eb9SGleb Smirnoff 			}
28833b3a8eb9SGleb Smirnoff 		}
28843b3a8eb9SGleb Smirnoff 		;
28853b3a8eb9SGleb Smirnoff 
28863b3a8eb9SGleb Smirnoff fromto		: ALL				{
28873b3a8eb9SGleb Smirnoff 			$$.src.host = NULL;
28883b3a8eb9SGleb Smirnoff 			$$.src.port = NULL;
28893b3a8eb9SGleb Smirnoff 			$$.dst.host = NULL;
28903b3a8eb9SGleb Smirnoff 			$$.dst.port = NULL;
28913b3a8eb9SGleb Smirnoff 			$$.src_os = NULL;
28923b3a8eb9SGleb Smirnoff 		}
28933b3a8eb9SGleb Smirnoff 		| from os to			{
28943b3a8eb9SGleb Smirnoff 			$$.src = $1;
28953b3a8eb9SGleb Smirnoff 			$$.src_os = $2;
28963b3a8eb9SGleb Smirnoff 			$$.dst = $3;
28973b3a8eb9SGleb Smirnoff 		}
28983b3a8eb9SGleb Smirnoff 		;
28993b3a8eb9SGleb Smirnoff 
29003b3a8eb9SGleb Smirnoff os		: /* empty */			{ $$ = NULL; }
29013b3a8eb9SGleb Smirnoff 		| OS xos			{ $$ = $2; }
29023b3a8eb9SGleb Smirnoff 		| OS '{' optnl os_list '}'	{ $$ = $4; }
29033b3a8eb9SGleb Smirnoff 		;
29043b3a8eb9SGleb Smirnoff 
29053b3a8eb9SGleb Smirnoff xos		: STRING {
29063b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_os));
29073b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
29083b3a8eb9SGleb Smirnoff 				err(1, "os: calloc");
29093b3a8eb9SGleb Smirnoff 			$$->os = $1;
29103b3a8eb9SGleb Smirnoff 			$$->tail = $$;
29113b3a8eb9SGleb Smirnoff 		}
29123b3a8eb9SGleb Smirnoff 		;
29133b3a8eb9SGleb Smirnoff 
29143b3a8eb9SGleb Smirnoff os_list		: xos optnl 			{ $$ = $1; }
29153b3a8eb9SGleb Smirnoff 		| os_list comma xos optnl	{
29163b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
29173b3a8eb9SGleb Smirnoff 			$1->tail = $3;
29183b3a8eb9SGleb Smirnoff 			$$ = $1;
29193b3a8eb9SGleb Smirnoff 		}
29203b3a8eb9SGleb Smirnoff 		;
29213b3a8eb9SGleb Smirnoff 
29223b3a8eb9SGleb Smirnoff from		: /* empty */			{
29233b3a8eb9SGleb Smirnoff 			$$.host = NULL;
29243b3a8eb9SGleb Smirnoff 			$$.port = NULL;
29253b3a8eb9SGleb Smirnoff 		}
29263b3a8eb9SGleb Smirnoff 		| FROM ipportspec		{
29273b3a8eb9SGleb Smirnoff 			$$ = $2;
29283b3a8eb9SGleb Smirnoff 		}
29293b3a8eb9SGleb Smirnoff 		;
29303b3a8eb9SGleb Smirnoff 
29313b3a8eb9SGleb Smirnoff to		: /* empty */			{
29323b3a8eb9SGleb Smirnoff 			$$.host = NULL;
29333b3a8eb9SGleb Smirnoff 			$$.port = NULL;
29343b3a8eb9SGleb Smirnoff 		}
29353b3a8eb9SGleb Smirnoff 		| TO ipportspec		{
29363b3a8eb9SGleb Smirnoff 			if (disallow_urpf_failed($2.host, "\"urpf-failed\" is "
29373b3a8eb9SGleb Smirnoff 			    "not permitted in a destination address"))
29383b3a8eb9SGleb Smirnoff 				YYERROR;
29393b3a8eb9SGleb Smirnoff 			$$ = $2;
29403b3a8eb9SGleb Smirnoff 		}
29413b3a8eb9SGleb Smirnoff 		;
29423b3a8eb9SGleb Smirnoff 
29433b3a8eb9SGleb Smirnoff ipportspec	: ipspec			{
29443b3a8eb9SGleb Smirnoff 			$$.host = $1;
29453b3a8eb9SGleb Smirnoff 			$$.port = NULL;
29463b3a8eb9SGleb Smirnoff 		}
29473b3a8eb9SGleb Smirnoff 		| ipspec PORT portspec		{
29483b3a8eb9SGleb Smirnoff 			$$.host = $1;
29493b3a8eb9SGleb Smirnoff 			$$.port = $3;
29503b3a8eb9SGleb Smirnoff 		}
29513b3a8eb9SGleb Smirnoff 		| PORT portspec			{
29523b3a8eb9SGleb Smirnoff 			$$.host = NULL;
29533b3a8eb9SGleb Smirnoff 			$$.port = $2;
29543b3a8eb9SGleb Smirnoff 		}
29553b3a8eb9SGleb Smirnoff 		;
29563b3a8eb9SGleb Smirnoff 
29573b3a8eb9SGleb Smirnoff optnl		: '\n' optnl
29583b3a8eb9SGleb Smirnoff 		|
29593b3a8eb9SGleb Smirnoff 		;
29603b3a8eb9SGleb Smirnoff 
29613b3a8eb9SGleb Smirnoff ipspec		: ANY				{ $$ = NULL; }
29623b3a8eb9SGleb Smirnoff 		| xhost				{ $$ = $1; }
29633b3a8eb9SGleb Smirnoff 		| '{' optnl host_list '}'	{ $$ = $3; }
29643b3a8eb9SGleb Smirnoff 		;
29653b3a8eb9SGleb Smirnoff 
29663b3a8eb9SGleb Smirnoff toipspec	: TO ipspec			{ $$ = $2; }
29673b3a8eb9SGleb Smirnoff 		| /* empty */			{ $$ = NULL; }
29683b3a8eb9SGleb Smirnoff 		;
29693b3a8eb9SGleb Smirnoff 
29703b3a8eb9SGleb Smirnoff host_list	: ipspec optnl			{ $$ = $1; }
29713b3a8eb9SGleb Smirnoff 		| host_list comma ipspec optnl	{
29723b3a8eb9SGleb Smirnoff 			if ($3 == NULL)
29733b3a8eb9SGleb Smirnoff 				$$ = $1;
29743b3a8eb9SGleb Smirnoff 			else if ($1 == NULL)
29753b3a8eb9SGleb Smirnoff 				$$ = $3;
29763b3a8eb9SGleb Smirnoff 			else {
29773b3a8eb9SGleb Smirnoff 				$1->tail->next = $3;
29783b3a8eb9SGleb Smirnoff 				$1->tail = $3->tail;
29793b3a8eb9SGleb Smirnoff 				$$ = $1;
29803b3a8eb9SGleb Smirnoff 			}
29813b3a8eb9SGleb Smirnoff 		}
29823b3a8eb9SGleb Smirnoff 		;
29833b3a8eb9SGleb Smirnoff 
29843b3a8eb9SGleb Smirnoff xhost		: not host			{
29853b3a8eb9SGleb Smirnoff 			struct node_host	*n;
29863b3a8eb9SGleb Smirnoff 
29873b3a8eb9SGleb Smirnoff 			for (n = $2; n != NULL; n = n->next)
29883b3a8eb9SGleb Smirnoff 				n->not = $1;
29893b3a8eb9SGleb Smirnoff 			$$ = $2;
29903b3a8eb9SGleb Smirnoff 		}
29913b3a8eb9SGleb Smirnoff 		| not NOROUTE			{
29923b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_host));
29933b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
29943b3a8eb9SGleb Smirnoff 				err(1, "xhost: calloc");
29953b3a8eb9SGleb Smirnoff 			$$->addr.type = PF_ADDR_NOROUTE;
29963b3a8eb9SGleb Smirnoff 			$$->next = NULL;
29973b3a8eb9SGleb Smirnoff 			$$->not = $1;
29983b3a8eb9SGleb Smirnoff 			$$->tail = $$;
29993b3a8eb9SGleb Smirnoff 		}
30003b3a8eb9SGleb Smirnoff 		| not URPFFAILED		{
30013b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_host));
30023b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
30033b3a8eb9SGleb Smirnoff 				err(1, "xhost: calloc");
30043b3a8eb9SGleb Smirnoff 			$$->addr.type = PF_ADDR_URPFFAILED;
30053b3a8eb9SGleb Smirnoff 			$$->next = NULL;
30063b3a8eb9SGleb Smirnoff 			$$->not = $1;
30073b3a8eb9SGleb Smirnoff 			$$->tail = $$;
30083b3a8eb9SGleb Smirnoff 		}
30093b3a8eb9SGleb Smirnoff 		;
30103b3a8eb9SGleb Smirnoff 
30113b3a8eb9SGleb Smirnoff host		: STRING			{
30123b3a8eb9SGleb Smirnoff 			if (($$ = host($1)) == NULL)	{
30133b3a8eb9SGleb Smirnoff 				/* error. "any" is handled elsewhere */
30143b3a8eb9SGleb Smirnoff 				free($1);
30153b3a8eb9SGleb Smirnoff 				yyerror("could not parse host specification");
30163b3a8eb9SGleb Smirnoff 				YYERROR;
30173b3a8eb9SGleb Smirnoff 			}
30183b3a8eb9SGleb Smirnoff 			free($1);
30193b3a8eb9SGleb Smirnoff 
30203b3a8eb9SGleb Smirnoff 		}
30213b3a8eb9SGleb Smirnoff 		| STRING '-' STRING		{
30223b3a8eb9SGleb Smirnoff 			struct node_host *b, *e;
30233b3a8eb9SGleb Smirnoff 
30243b3a8eb9SGleb Smirnoff 			if ((b = host($1)) == NULL || (e = host($3)) == NULL) {
30253b3a8eb9SGleb Smirnoff 				free($1);
30263b3a8eb9SGleb Smirnoff 				free($3);
30273b3a8eb9SGleb Smirnoff 				yyerror("could not parse host specification");
30283b3a8eb9SGleb Smirnoff 				YYERROR;
30293b3a8eb9SGleb Smirnoff 			}
30303b3a8eb9SGleb Smirnoff 			if (b->af != e->af ||
30313b3a8eb9SGleb Smirnoff 			    b->addr.type != PF_ADDR_ADDRMASK ||
30323b3a8eb9SGleb Smirnoff 			    e->addr.type != PF_ADDR_ADDRMASK ||
30333b3a8eb9SGleb Smirnoff 			    unmask(&b->addr.v.a.mask, b->af) !=
30343b3a8eb9SGleb Smirnoff 			    (b->af == AF_INET ? 32 : 128) ||
30353b3a8eb9SGleb Smirnoff 			    unmask(&e->addr.v.a.mask, e->af) !=
30363b3a8eb9SGleb Smirnoff 			    (e->af == AF_INET ? 32 : 128) ||
30373b3a8eb9SGleb Smirnoff 			    b->next != NULL || b->not ||
30383b3a8eb9SGleb Smirnoff 			    e->next != NULL || e->not) {
30393b3a8eb9SGleb Smirnoff 				free(b);
30403b3a8eb9SGleb Smirnoff 				free(e);
30413b3a8eb9SGleb Smirnoff 				free($1);
30423b3a8eb9SGleb Smirnoff 				free($3);
30433b3a8eb9SGleb Smirnoff 				yyerror("invalid address range");
30443b3a8eb9SGleb Smirnoff 				YYERROR;
30453b3a8eb9SGleb Smirnoff 			}
30463b3a8eb9SGleb Smirnoff 			memcpy(&b->addr.v.a.mask, &e->addr.v.a.addr,
30473b3a8eb9SGleb Smirnoff 			    sizeof(b->addr.v.a.mask));
30483b3a8eb9SGleb Smirnoff 			b->addr.type = PF_ADDR_RANGE;
30493b3a8eb9SGleb Smirnoff 			$$ = b;
30503b3a8eb9SGleb Smirnoff 			free(e);
30513b3a8eb9SGleb Smirnoff 			free($1);
30523b3a8eb9SGleb Smirnoff 			free($3);
30533b3a8eb9SGleb Smirnoff 		}
30543b3a8eb9SGleb Smirnoff 		| STRING '/' NUMBER		{
30553b3a8eb9SGleb Smirnoff 			char	*buf;
30563b3a8eb9SGleb Smirnoff 
30573b3a8eb9SGleb Smirnoff 			if (asprintf(&buf, "%s/%lld", $1, (long long)$3) == -1)
30583b3a8eb9SGleb Smirnoff 				err(1, "host: asprintf");
30593b3a8eb9SGleb Smirnoff 			free($1);
30603b3a8eb9SGleb Smirnoff 			if (($$ = host(buf)) == NULL)	{
30613b3a8eb9SGleb Smirnoff 				/* error. "any" is handled elsewhere */
30623b3a8eb9SGleb Smirnoff 				free(buf);
30633b3a8eb9SGleb Smirnoff 				yyerror("could not parse host specification");
30643b3a8eb9SGleb Smirnoff 				YYERROR;
30653b3a8eb9SGleb Smirnoff 			}
30663b3a8eb9SGleb Smirnoff 			free(buf);
30673b3a8eb9SGleb Smirnoff 		}
30683b3a8eb9SGleb Smirnoff 		| NUMBER '/' NUMBER		{
30693b3a8eb9SGleb Smirnoff 			char	*buf;
30703b3a8eb9SGleb Smirnoff 
30713b3a8eb9SGleb Smirnoff 			/* ie. for 10/8 parsing */
30723b3a8eb9SGleb Smirnoff #ifdef __FreeBSD__
30733b3a8eb9SGleb Smirnoff 			if (asprintf(&buf, "%lld/%lld", (long long)$1, (long long)$3) == -1)
30743b3a8eb9SGleb Smirnoff #else
30753b3a8eb9SGleb Smirnoff 			if (asprintf(&buf, "%lld/%lld", $1, $3) == -1)
30763b3a8eb9SGleb Smirnoff #endif
30773b3a8eb9SGleb Smirnoff 				err(1, "host: asprintf");
30783b3a8eb9SGleb Smirnoff 			if (($$ = host(buf)) == NULL)	{
30793b3a8eb9SGleb Smirnoff 				/* error. "any" is handled elsewhere */
30803b3a8eb9SGleb Smirnoff 				free(buf);
30813b3a8eb9SGleb Smirnoff 				yyerror("could not parse host specification");
30823b3a8eb9SGleb Smirnoff 				YYERROR;
30833b3a8eb9SGleb Smirnoff 			}
30843b3a8eb9SGleb Smirnoff 			free(buf);
30853b3a8eb9SGleb Smirnoff 		}
30863b3a8eb9SGleb Smirnoff 		| dynaddr
30873b3a8eb9SGleb Smirnoff 		| dynaddr '/' NUMBER		{
30883b3a8eb9SGleb Smirnoff 			struct node_host	*n;
30893b3a8eb9SGleb Smirnoff 
30903b3a8eb9SGleb Smirnoff 			if ($3 < 0 || $3 > 128) {
30913b3a8eb9SGleb Smirnoff 				yyerror("bit number too big");
30923b3a8eb9SGleb Smirnoff 				YYERROR;
30933b3a8eb9SGleb Smirnoff 			}
30943b3a8eb9SGleb Smirnoff 			$$ = $1;
30953b3a8eb9SGleb Smirnoff 			for (n = $1; n != NULL; n = n->next)
30963b3a8eb9SGleb Smirnoff 				set_ipmask(n, $3);
30973b3a8eb9SGleb Smirnoff 		}
30983b3a8eb9SGleb Smirnoff 		| '<' STRING '>'	{
30993b3a8eb9SGleb Smirnoff 			if (strlen($2) >= PF_TABLE_NAME_SIZE) {
31003b3a8eb9SGleb Smirnoff 				yyerror("table name '%s' too long", $2);
31013b3a8eb9SGleb Smirnoff 				free($2);
31023b3a8eb9SGleb Smirnoff 				YYERROR;
31033b3a8eb9SGleb Smirnoff 			}
31043b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_host));
31053b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
31063b3a8eb9SGleb Smirnoff 				err(1, "host: calloc");
31073b3a8eb9SGleb Smirnoff 			$$->addr.type = PF_ADDR_TABLE;
31083b3a8eb9SGleb Smirnoff 			if (strlcpy($$->addr.v.tblname, $2,
31093b3a8eb9SGleb Smirnoff 			    sizeof($$->addr.v.tblname)) >=
31103b3a8eb9SGleb Smirnoff 			    sizeof($$->addr.v.tblname))
31113b3a8eb9SGleb Smirnoff 				errx(1, "host: strlcpy");
31123b3a8eb9SGleb Smirnoff 			free($2);
31133b3a8eb9SGleb Smirnoff 			$$->next = NULL;
31143b3a8eb9SGleb Smirnoff 			$$->tail = $$;
31153b3a8eb9SGleb Smirnoff 		}
31163b3a8eb9SGleb Smirnoff 		;
31173b3a8eb9SGleb Smirnoff 
31183b3a8eb9SGleb Smirnoff number		: NUMBER
31193b3a8eb9SGleb Smirnoff 		| STRING		{
31203b3a8eb9SGleb Smirnoff 			u_long	ulval;
31213b3a8eb9SGleb Smirnoff 
31223b3a8eb9SGleb Smirnoff 			if (atoul($1, &ulval) == -1) {
31233b3a8eb9SGleb Smirnoff 				yyerror("%s is not a number", $1);
31243b3a8eb9SGleb Smirnoff 				free($1);
31253b3a8eb9SGleb Smirnoff 				YYERROR;
31263b3a8eb9SGleb Smirnoff 			} else
31273b3a8eb9SGleb Smirnoff 				$$ = ulval;
31283b3a8eb9SGleb Smirnoff 			free($1);
31293b3a8eb9SGleb Smirnoff 		}
31303b3a8eb9SGleb Smirnoff 		;
31313b3a8eb9SGleb Smirnoff 
31323b3a8eb9SGleb Smirnoff dynaddr		: '(' STRING ')'		{
31333b3a8eb9SGleb Smirnoff 			int	 flags = 0;
31343b3a8eb9SGleb Smirnoff 			char	*p, *op;
31353b3a8eb9SGleb Smirnoff 
31363b3a8eb9SGleb Smirnoff 			op = $2;
31373b3a8eb9SGleb Smirnoff 			if (!isalpha(op[0])) {
31383b3a8eb9SGleb Smirnoff 				yyerror("invalid interface name '%s'", op);
31393b3a8eb9SGleb Smirnoff 				free(op);
31403b3a8eb9SGleb Smirnoff 				YYERROR;
31413b3a8eb9SGleb Smirnoff 			}
31423b3a8eb9SGleb Smirnoff 			while ((p = strrchr($2, ':')) != NULL) {
31433b3a8eb9SGleb Smirnoff 				if (!strcmp(p+1, "network"))
31443b3a8eb9SGleb Smirnoff 					flags |= PFI_AFLAG_NETWORK;
31453b3a8eb9SGleb Smirnoff 				else if (!strcmp(p+1, "broadcast"))
31463b3a8eb9SGleb Smirnoff 					flags |= PFI_AFLAG_BROADCAST;
31473b3a8eb9SGleb Smirnoff 				else if (!strcmp(p+1, "peer"))
31483b3a8eb9SGleb Smirnoff 					flags |= PFI_AFLAG_PEER;
31493b3a8eb9SGleb Smirnoff 				else if (!strcmp(p+1, "0"))
31503b3a8eb9SGleb Smirnoff 					flags |= PFI_AFLAG_NOALIAS;
31513b3a8eb9SGleb Smirnoff 				else {
31523b3a8eb9SGleb Smirnoff 					yyerror("interface %s has bad modifier",
31533b3a8eb9SGleb Smirnoff 					    $2);
31543b3a8eb9SGleb Smirnoff 					free(op);
31553b3a8eb9SGleb Smirnoff 					YYERROR;
31563b3a8eb9SGleb Smirnoff 				}
31573b3a8eb9SGleb Smirnoff 				*p = '\0';
31583b3a8eb9SGleb Smirnoff 			}
31593b3a8eb9SGleb Smirnoff 			if (flags & (flags - 1) & PFI_AFLAG_MODEMASK) {
31603b3a8eb9SGleb Smirnoff 				free(op);
31613b3a8eb9SGleb Smirnoff 				yyerror("illegal combination of "
31623b3a8eb9SGleb Smirnoff 				    "interface modifiers");
31633b3a8eb9SGleb Smirnoff 				YYERROR;
31643b3a8eb9SGleb Smirnoff 			}
31653b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_host));
31663b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
31673b3a8eb9SGleb Smirnoff 				err(1, "address: calloc");
31683b3a8eb9SGleb Smirnoff 			$$->af = 0;
31693b3a8eb9SGleb Smirnoff 			set_ipmask($$, 128);
31703b3a8eb9SGleb Smirnoff 			$$->addr.type = PF_ADDR_DYNIFTL;
31713b3a8eb9SGleb Smirnoff 			$$->addr.iflags = flags;
31723b3a8eb9SGleb Smirnoff 			if (strlcpy($$->addr.v.ifname, $2,
31733b3a8eb9SGleb Smirnoff 			    sizeof($$->addr.v.ifname)) >=
31743b3a8eb9SGleb Smirnoff 			    sizeof($$->addr.v.ifname)) {
31753b3a8eb9SGleb Smirnoff 				free(op);
31763b3a8eb9SGleb Smirnoff 				free($$);
31773b3a8eb9SGleb Smirnoff 				yyerror("interface name too long");
31783b3a8eb9SGleb Smirnoff 				YYERROR;
31793b3a8eb9SGleb Smirnoff 			}
31803b3a8eb9SGleb Smirnoff 			free(op);
31813b3a8eb9SGleb Smirnoff 			$$->next = NULL;
31823b3a8eb9SGleb Smirnoff 			$$->tail = $$;
31833b3a8eb9SGleb Smirnoff 		}
31843b3a8eb9SGleb Smirnoff 		;
31853b3a8eb9SGleb Smirnoff 
31863b3a8eb9SGleb Smirnoff portspec	: port_item			{ $$ = $1; }
31873b3a8eb9SGleb Smirnoff 		| '{' optnl port_list '}'	{ $$ = $3; }
31883b3a8eb9SGleb Smirnoff 		;
31893b3a8eb9SGleb Smirnoff 
31903b3a8eb9SGleb Smirnoff port_list	: port_item optnl		{ $$ = $1; }
31913b3a8eb9SGleb Smirnoff 		| port_list comma port_item optnl	{
31923b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
31933b3a8eb9SGleb Smirnoff 			$1->tail = $3;
31943b3a8eb9SGleb Smirnoff 			$$ = $1;
31953b3a8eb9SGleb Smirnoff 		}
31963b3a8eb9SGleb Smirnoff 		;
31973b3a8eb9SGleb Smirnoff 
31983b3a8eb9SGleb Smirnoff port_item	: portrange			{
31993b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_port));
32003b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
32013b3a8eb9SGleb Smirnoff 				err(1, "port_item: calloc");
32023b3a8eb9SGleb Smirnoff 			$$->port[0] = $1.a;
32033b3a8eb9SGleb Smirnoff 			$$->port[1] = $1.b;
32043b3a8eb9SGleb Smirnoff 			if ($1.t)
32053b3a8eb9SGleb Smirnoff 				$$->op = PF_OP_RRG;
32063b3a8eb9SGleb Smirnoff 			else
32073b3a8eb9SGleb Smirnoff 				$$->op = PF_OP_EQ;
32083b3a8eb9SGleb Smirnoff 			$$->next = NULL;
32093b3a8eb9SGleb Smirnoff 			$$->tail = $$;
32103b3a8eb9SGleb Smirnoff 		}
32113b3a8eb9SGleb Smirnoff 		| unaryop portrange	{
32123b3a8eb9SGleb Smirnoff 			if ($2.t) {
32133b3a8eb9SGleb Smirnoff 				yyerror("':' cannot be used with an other "
32143b3a8eb9SGleb Smirnoff 				    "port operator");
32153b3a8eb9SGleb Smirnoff 				YYERROR;
32163b3a8eb9SGleb Smirnoff 			}
32173b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_port));
32183b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
32193b3a8eb9SGleb Smirnoff 				err(1, "port_item: calloc");
32203b3a8eb9SGleb Smirnoff 			$$->port[0] = $2.a;
32213b3a8eb9SGleb Smirnoff 			$$->port[1] = $2.b;
32223b3a8eb9SGleb Smirnoff 			$$->op = $1;
32233b3a8eb9SGleb Smirnoff 			$$->next = NULL;
32243b3a8eb9SGleb Smirnoff 			$$->tail = $$;
32253b3a8eb9SGleb Smirnoff 		}
32263b3a8eb9SGleb Smirnoff 		| portrange PORTBINARY portrange	{
32273b3a8eb9SGleb Smirnoff 			if ($1.t || $3.t) {
32283b3a8eb9SGleb Smirnoff 				yyerror("':' cannot be used with an other "
32293b3a8eb9SGleb Smirnoff 				    "port operator");
32303b3a8eb9SGleb Smirnoff 				YYERROR;
32313b3a8eb9SGleb Smirnoff 			}
32323b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_port));
32333b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
32343b3a8eb9SGleb Smirnoff 				err(1, "port_item: calloc");
32353b3a8eb9SGleb Smirnoff 			$$->port[0] = $1.a;
32363b3a8eb9SGleb Smirnoff 			$$->port[1] = $3.a;
32373b3a8eb9SGleb Smirnoff 			$$->op = $2;
32383b3a8eb9SGleb Smirnoff 			$$->next = NULL;
32393b3a8eb9SGleb Smirnoff 			$$->tail = $$;
32403b3a8eb9SGleb Smirnoff 		}
32413b3a8eb9SGleb Smirnoff 		;
32423b3a8eb9SGleb Smirnoff 
32433b3a8eb9SGleb Smirnoff portplain	: numberstring			{
32443b3a8eb9SGleb Smirnoff 			if (parseport($1, &$$, 0) == -1) {
32453b3a8eb9SGleb Smirnoff 				free($1);
32463b3a8eb9SGleb Smirnoff 				YYERROR;
32473b3a8eb9SGleb Smirnoff 			}
32483b3a8eb9SGleb Smirnoff 			free($1);
32493b3a8eb9SGleb Smirnoff 		}
32503b3a8eb9SGleb Smirnoff 		;
32513b3a8eb9SGleb Smirnoff 
32523b3a8eb9SGleb Smirnoff portrange	: numberstring			{
32533b3a8eb9SGleb Smirnoff 			if (parseport($1, &$$, PPORT_RANGE) == -1) {
32543b3a8eb9SGleb Smirnoff 				free($1);
32553b3a8eb9SGleb Smirnoff 				YYERROR;
32563b3a8eb9SGleb Smirnoff 			}
32573b3a8eb9SGleb Smirnoff 			free($1);
32583b3a8eb9SGleb Smirnoff 		}
32593b3a8eb9SGleb Smirnoff 		;
32603b3a8eb9SGleb Smirnoff 
32613b3a8eb9SGleb Smirnoff uids		: uid_item			{ $$ = $1; }
32623b3a8eb9SGleb Smirnoff 		| '{' optnl uid_list '}'	{ $$ = $3; }
32633b3a8eb9SGleb Smirnoff 		;
32643b3a8eb9SGleb Smirnoff 
32653b3a8eb9SGleb Smirnoff uid_list	: uid_item optnl		{ $$ = $1; }
32663b3a8eb9SGleb Smirnoff 		| uid_list comma uid_item optnl	{
32673b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
32683b3a8eb9SGleb Smirnoff 			$1->tail = $3;
32693b3a8eb9SGleb Smirnoff 			$$ = $1;
32703b3a8eb9SGleb Smirnoff 		}
32713b3a8eb9SGleb Smirnoff 		;
32723b3a8eb9SGleb Smirnoff 
32733b3a8eb9SGleb Smirnoff uid_item	: uid				{
32743b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_uid));
32753b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
32763b3a8eb9SGleb Smirnoff 				err(1, "uid_item: calloc");
32773b3a8eb9SGleb Smirnoff 			$$->uid[0] = $1;
32783b3a8eb9SGleb Smirnoff 			$$->uid[1] = $1;
32793b3a8eb9SGleb Smirnoff 			$$->op = PF_OP_EQ;
32803b3a8eb9SGleb Smirnoff 			$$->next = NULL;
32813b3a8eb9SGleb Smirnoff 			$$->tail = $$;
32823b3a8eb9SGleb Smirnoff 		}
32833b3a8eb9SGleb Smirnoff 		| unaryop uid			{
32843b3a8eb9SGleb Smirnoff 			if ($2 == UID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
32853b3a8eb9SGleb Smirnoff 				yyerror("user unknown requires operator = or "
32863b3a8eb9SGleb Smirnoff 				    "!=");
32873b3a8eb9SGleb Smirnoff 				YYERROR;
32883b3a8eb9SGleb Smirnoff 			}
32893b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_uid));
32903b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
32913b3a8eb9SGleb Smirnoff 				err(1, "uid_item: calloc");
32923b3a8eb9SGleb Smirnoff 			$$->uid[0] = $2;
32933b3a8eb9SGleb Smirnoff 			$$->uid[1] = $2;
32943b3a8eb9SGleb Smirnoff 			$$->op = $1;
32953b3a8eb9SGleb Smirnoff 			$$->next = NULL;
32963b3a8eb9SGleb Smirnoff 			$$->tail = $$;
32973b3a8eb9SGleb Smirnoff 		}
32983b3a8eb9SGleb Smirnoff 		| uid PORTBINARY uid		{
32993b3a8eb9SGleb Smirnoff 			if ($1 == UID_MAX || $3 == UID_MAX) {
33003b3a8eb9SGleb Smirnoff 				yyerror("user unknown requires operator = or "
33013b3a8eb9SGleb Smirnoff 				    "!=");
33023b3a8eb9SGleb Smirnoff 				YYERROR;
33033b3a8eb9SGleb Smirnoff 			}
33043b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_uid));
33053b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
33063b3a8eb9SGleb Smirnoff 				err(1, "uid_item: calloc");
33073b3a8eb9SGleb Smirnoff 			$$->uid[0] = $1;
33083b3a8eb9SGleb Smirnoff 			$$->uid[1] = $3;
33093b3a8eb9SGleb Smirnoff 			$$->op = $2;
33103b3a8eb9SGleb Smirnoff 			$$->next = NULL;
33113b3a8eb9SGleb Smirnoff 			$$->tail = $$;
33123b3a8eb9SGleb Smirnoff 		}
33133b3a8eb9SGleb Smirnoff 		;
33143b3a8eb9SGleb Smirnoff 
33153b3a8eb9SGleb Smirnoff uid		: STRING			{
33163b3a8eb9SGleb Smirnoff 			if (!strcmp($1, "unknown"))
33173b3a8eb9SGleb Smirnoff 				$$ = UID_MAX;
33183b3a8eb9SGleb Smirnoff 			else {
33193b3a8eb9SGleb Smirnoff 				struct passwd	*pw;
33203b3a8eb9SGleb Smirnoff 
33213b3a8eb9SGleb Smirnoff 				if ((pw = getpwnam($1)) == NULL) {
33223b3a8eb9SGleb Smirnoff 					yyerror("unknown user %s", $1);
33233b3a8eb9SGleb Smirnoff 					free($1);
33243b3a8eb9SGleb Smirnoff 					YYERROR;
33253b3a8eb9SGleb Smirnoff 				}
33263b3a8eb9SGleb Smirnoff 				$$ = pw->pw_uid;
33273b3a8eb9SGleb Smirnoff 			}
33283b3a8eb9SGleb Smirnoff 			free($1);
33293b3a8eb9SGleb Smirnoff 		}
33303b3a8eb9SGleb Smirnoff 		| NUMBER			{
33313b3a8eb9SGleb Smirnoff 			if ($1 < 0 || $1 >= UID_MAX) {
33323b3a8eb9SGleb Smirnoff 				yyerror("illegal uid value %lu", $1);
33333b3a8eb9SGleb Smirnoff 				YYERROR;
33343b3a8eb9SGleb Smirnoff 			}
33353b3a8eb9SGleb Smirnoff 			$$ = $1;
33363b3a8eb9SGleb Smirnoff 		}
33373b3a8eb9SGleb Smirnoff 		;
33383b3a8eb9SGleb Smirnoff 
33393b3a8eb9SGleb Smirnoff gids		: gid_item			{ $$ = $1; }
33403b3a8eb9SGleb Smirnoff 		| '{' optnl gid_list '}'	{ $$ = $3; }
33413b3a8eb9SGleb Smirnoff 		;
33423b3a8eb9SGleb Smirnoff 
33433b3a8eb9SGleb Smirnoff gid_list	: gid_item optnl		{ $$ = $1; }
33443b3a8eb9SGleb Smirnoff 		| gid_list comma gid_item optnl	{
33453b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
33463b3a8eb9SGleb Smirnoff 			$1->tail = $3;
33473b3a8eb9SGleb Smirnoff 			$$ = $1;
33483b3a8eb9SGleb Smirnoff 		}
33493b3a8eb9SGleb Smirnoff 		;
33503b3a8eb9SGleb Smirnoff 
33513b3a8eb9SGleb Smirnoff gid_item	: gid				{
33523b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_gid));
33533b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
33543b3a8eb9SGleb Smirnoff 				err(1, "gid_item: calloc");
33553b3a8eb9SGleb Smirnoff 			$$->gid[0] = $1;
33563b3a8eb9SGleb Smirnoff 			$$->gid[1] = $1;
33573b3a8eb9SGleb Smirnoff 			$$->op = PF_OP_EQ;
33583b3a8eb9SGleb Smirnoff 			$$->next = NULL;
33593b3a8eb9SGleb Smirnoff 			$$->tail = $$;
33603b3a8eb9SGleb Smirnoff 		}
33613b3a8eb9SGleb Smirnoff 		| unaryop gid			{
33623b3a8eb9SGleb Smirnoff 			if ($2 == GID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
33633b3a8eb9SGleb Smirnoff 				yyerror("group unknown requires operator = or "
33643b3a8eb9SGleb Smirnoff 				    "!=");
33653b3a8eb9SGleb Smirnoff 				YYERROR;
33663b3a8eb9SGleb Smirnoff 			}
33673b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_gid));
33683b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
33693b3a8eb9SGleb Smirnoff 				err(1, "gid_item: calloc");
33703b3a8eb9SGleb Smirnoff 			$$->gid[0] = $2;
33713b3a8eb9SGleb Smirnoff 			$$->gid[1] = $2;
33723b3a8eb9SGleb Smirnoff 			$$->op = $1;
33733b3a8eb9SGleb Smirnoff 			$$->next = NULL;
33743b3a8eb9SGleb Smirnoff 			$$->tail = $$;
33753b3a8eb9SGleb Smirnoff 		}
33763b3a8eb9SGleb Smirnoff 		| gid PORTBINARY gid		{
33773b3a8eb9SGleb Smirnoff 			if ($1 == GID_MAX || $3 == GID_MAX) {
33783b3a8eb9SGleb Smirnoff 				yyerror("group unknown requires operator = or "
33793b3a8eb9SGleb Smirnoff 				    "!=");
33803b3a8eb9SGleb Smirnoff 				YYERROR;
33813b3a8eb9SGleb Smirnoff 			}
33823b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_gid));
33833b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
33843b3a8eb9SGleb Smirnoff 				err(1, "gid_item: calloc");
33853b3a8eb9SGleb Smirnoff 			$$->gid[0] = $1;
33863b3a8eb9SGleb Smirnoff 			$$->gid[1] = $3;
33873b3a8eb9SGleb Smirnoff 			$$->op = $2;
33883b3a8eb9SGleb Smirnoff 			$$->next = NULL;
33893b3a8eb9SGleb Smirnoff 			$$->tail = $$;
33903b3a8eb9SGleb Smirnoff 		}
33913b3a8eb9SGleb Smirnoff 		;
33923b3a8eb9SGleb Smirnoff 
33933b3a8eb9SGleb Smirnoff gid		: STRING			{
33943b3a8eb9SGleb Smirnoff 			if (!strcmp($1, "unknown"))
33953b3a8eb9SGleb Smirnoff 				$$ = GID_MAX;
33963b3a8eb9SGleb Smirnoff 			else {
33973b3a8eb9SGleb Smirnoff 				struct group	*grp;
33983b3a8eb9SGleb Smirnoff 
33993b3a8eb9SGleb Smirnoff 				if ((grp = getgrnam($1)) == NULL) {
34003b3a8eb9SGleb Smirnoff 					yyerror("unknown group %s", $1);
34013b3a8eb9SGleb Smirnoff 					free($1);
34023b3a8eb9SGleb Smirnoff 					YYERROR;
34033b3a8eb9SGleb Smirnoff 				}
34043b3a8eb9SGleb Smirnoff 				$$ = grp->gr_gid;
34053b3a8eb9SGleb Smirnoff 			}
34063b3a8eb9SGleb Smirnoff 			free($1);
34073b3a8eb9SGleb Smirnoff 		}
34083b3a8eb9SGleb Smirnoff 		| NUMBER			{
34093b3a8eb9SGleb Smirnoff 			if ($1 < 0 || $1 >= GID_MAX) {
34103b3a8eb9SGleb Smirnoff 				yyerror("illegal gid value %lu", $1);
34113b3a8eb9SGleb Smirnoff 				YYERROR;
34123b3a8eb9SGleb Smirnoff 			}
34133b3a8eb9SGleb Smirnoff 			$$ = $1;
34143b3a8eb9SGleb Smirnoff 		}
34153b3a8eb9SGleb Smirnoff 		;
34163b3a8eb9SGleb Smirnoff 
34173b3a8eb9SGleb Smirnoff flag		: STRING			{
34183b3a8eb9SGleb Smirnoff 			int	f;
34193b3a8eb9SGleb Smirnoff 
34203b3a8eb9SGleb Smirnoff 			if ((f = parse_flags($1)) < 0) {
34213b3a8eb9SGleb Smirnoff 				yyerror("bad flags %s", $1);
34223b3a8eb9SGleb Smirnoff 				free($1);
34233b3a8eb9SGleb Smirnoff 				YYERROR;
34243b3a8eb9SGleb Smirnoff 			}
34253b3a8eb9SGleb Smirnoff 			free($1);
34263b3a8eb9SGleb Smirnoff 			$$.b1 = f;
34273b3a8eb9SGleb Smirnoff 		}
34283b3a8eb9SGleb Smirnoff 		;
34293b3a8eb9SGleb Smirnoff 
34303b3a8eb9SGleb Smirnoff flags		: FLAGS flag '/' flag	{ $$.b1 = $2.b1; $$.b2 = $4.b1; }
34313b3a8eb9SGleb Smirnoff 		| FLAGS '/' flag	{ $$.b1 = 0; $$.b2 = $3.b1; }
34323b3a8eb9SGleb Smirnoff 		| FLAGS ANY		{ $$.b1 = 0; $$.b2 = 0; }
34333b3a8eb9SGleb Smirnoff 		;
34343b3a8eb9SGleb Smirnoff 
34353b3a8eb9SGleb Smirnoff icmpspec	: ICMPTYPE icmp_item			{ $$ = $2; }
34363b3a8eb9SGleb Smirnoff 		| ICMPTYPE '{' optnl icmp_list '}'	{ $$ = $4; }
34373b3a8eb9SGleb Smirnoff 		| ICMP6TYPE icmp6_item			{ $$ = $2; }
34383b3a8eb9SGleb Smirnoff 		| ICMP6TYPE '{' optnl icmp6_list '}'	{ $$ = $4; }
34393b3a8eb9SGleb Smirnoff 		;
34403b3a8eb9SGleb Smirnoff 
34413b3a8eb9SGleb Smirnoff icmp_list	: icmp_item optnl		{ $$ = $1; }
34423b3a8eb9SGleb Smirnoff 		| icmp_list comma icmp_item optnl {
34433b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
34443b3a8eb9SGleb Smirnoff 			$1->tail = $3;
34453b3a8eb9SGleb Smirnoff 			$$ = $1;
34463b3a8eb9SGleb Smirnoff 		}
34473b3a8eb9SGleb Smirnoff 		;
34483b3a8eb9SGleb Smirnoff 
34493b3a8eb9SGleb Smirnoff icmp6_list	: icmp6_item optnl		{ $$ = $1; }
34503b3a8eb9SGleb Smirnoff 		| icmp6_list comma icmp6_item optnl {
34513b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
34523b3a8eb9SGleb Smirnoff 			$1->tail = $3;
34533b3a8eb9SGleb Smirnoff 			$$ = $1;
34543b3a8eb9SGleb Smirnoff 		}
34553b3a8eb9SGleb Smirnoff 		;
34563b3a8eb9SGleb Smirnoff 
34573b3a8eb9SGleb Smirnoff icmp_item	: icmptype		{
34583b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_icmp));
34593b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
34603b3a8eb9SGleb Smirnoff 				err(1, "icmp_item: calloc");
34613b3a8eb9SGleb Smirnoff 			$$->type = $1;
34623b3a8eb9SGleb Smirnoff 			$$->code = 0;
34633b3a8eb9SGleb Smirnoff 			$$->proto = IPPROTO_ICMP;
34643b3a8eb9SGleb Smirnoff 			$$->next = NULL;
34653b3a8eb9SGleb Smirnoff 			$$->tail = $$;
34663b3a8eb9SGleb Smirnoff 		}
34673b3a8eb9SGleb Smirnoff 		| icmptype CODE STRING	{
34683b3a8eb9SGleb Smirnoff 			const struct icmpcodeent	*p;
34693b3a8eb9SGleb Smirnoff 
34703b3a8eb9SGleb Smirnoff 			if ((p = geticmpcodebyname($1-1, $3, AF_INET)) == NULL) {
34713b3a8eb9SGleb Smirnoff 				yyerror("unknown icmp-code %s", $3);
34723b3a8eb9SGleb Smirnoff 				free($3);
34733b3a8eb9SGleb Smirnoff 				YYERROR;
34743b3a8eb9SGleb Smirnoff 			}
34753b3a8eb9SGleb Smirnoff 
34763b3a8eb9SGleb Smirnoff 			free($3);
34773b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_icmp));
34783b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
34793b3a8eb9SGleb Smirnoff 				err(1, "icmp_item: calloc");
34803b3a8eb9SGleb Smirnoff 			$$->type = $1;
34813b3a8eb9SGleb Smirnoff 			$$->code = p->code + 1;
34823b3a8eb9SGleb Smirnoff 			$$->proto = IPPROTO_ICMP;
34833b3a8eb9SGleb Smirnoff 			$$->next = NULL;
34843b3a8eb9SGleb Smirnoff 			$$->tail = $$;
34853b3a8eb9SGleb Smirnoff 		}
34863b3a8eb9SGleb Smirnoff 		| icmptype CODE NUMBER	{
34873b3a8eb9SGleb Smirnoff 			if ($3 < 0 || $3 > 255) {
34883b3a8eb9SGleb Smirnoff 				yyerror("illegal icmp-code %lu", $3);
34893b3a8eb9SGleb Smirnoff 				YYERROR;
34903b3a8eb9SGleb Smirnoff 			}
34913b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_icmp));
34923b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
34933b3a8eb9SGleb Smirnoff 				err(1, "icmp_item: calloc");
34943b3a8eb9SGleb Smirnoff 			$$->type = $1;
34953b3a8eb9SGleb Smirnoff 			$$->code = $3 + 1;
34963b3a8eb9SGleb Smirnoff 			$$->proto = IPPROTO_ICMP;
34973b3a8eb9SGleb Smirnoff 			$$->next = NULL;
34983b3a8eb9SGleb Smirnoff 			$$->tail = $$;
34993b3a8eb9SGleb Smirnoff 		}
35003b3a8eb9SGleb Smirnoff 		;
35013b3a8eb9SGleb Smirnoff 
35023b3a8eb9SGleb Smirnoff icmp6_item	: icmp6type		{
35033b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_icmp));
35043b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
35053b3a8eb9SGleb Smirnoff 				err(1, "icmp_item: calloc");
35063b3a8eb9SGleb Smirnoff 			$$->type = $1;
35073b3a8eb9SGleb Smirnoff 			$$->code = 0;
35083b3a8eb9SGleb Smirnoff 			$$->proto = IPPROTO_ICMPV6;
35093b3a8eb9SGleb Smirnoff 			$$->next = NULL;
35103b3a8eb9SGleb Smirnoff 			$$->tail = $$;
35113b3a8eb9SGleb Smirnoff 		}
35123b3a8eb9SGleb Smirnoff 		| icmp6type CODE STRING	{
35133b3a8eb9SGleb Smirnoff 			const struct icmpcodeent	*p;
35143b3a8eb9SGleb Smirnoff 
35153b3a8eb9SGleb Smirnoff 			if ((p = geticmpcodebyname($1-1, $3, AF_INET6)) == NULL) {
35163b3a8eb9SGleb Smirnoff 				yyerror("unknown icmp6-code %s", $3);
35173b3a8eb9SGleb Smirnoff 				free($3);
35183b3a8eb9SGleb Smirnoff 				YYERROR;
35193b3a8eb9SGleb Smirnoff 			}
35203b3a8eb9SGleb Smirnoff 			free($3);
35213b3a8eb9SGleb Smirnoff 
35223b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_icmp));
35233b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
35243b3a8eb9SGleb Smirnoff 				err(1, "icmp_item: calloc");
35253b3a8eb9SGleb Smirnoff 			$$->type = $1;
35263b3a8eb9SGleb Smirnoff 			$$->code = p->code + 1;
35273b3a8eb9SGleb Smirnoff 			$$->proto = IPPROTO_ICMPV6;
35283b3a8eb9SGleb Smirnoff 			$$->next = NULL;
35293b3a8eb9SGleb Smirnoff 			$$->tail = $$;
35303b3a8eb9SGleb Smirnoff 		}
35313b3a8eb9SGleb Smirnoff 		| icmp6type CODE NUMBER	{
35323b3a8eb9SGleb Smirnoff 			if ($3 < 0 || $3 > 255) {
35333b3a8eb9SGleb Smirnoff 				yyerror("illegal icmp-code %lu", $3);
35343b3a8eb9SGleb Smirnoff 				YYERROR;
35353b3a8eb9SGleb Smirnoff 			}
35363b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_icmp));
35373b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
35383b3a8eb9SGleb Smirnoff 				err(1, "icmp_item: calloc");
35393b3a8eb9SGleb Smirnoff 			$$->type = $1;
35403b3a8eb9SGleb Smirnoff 			$$->code = $3 + 1;
35413b3a8eb9SGleb Smirnoff 			$$->proto = IPPROTO_ICMPV6;
35423b3a8eb9SGleb Smirnoff 			$$->next = NULL;
35433b3a8eb9SGleb Smirnoff 			$$->tail = $$;
35443b3a8eb9SGleb Smirnoff 		}
35453b3a8eb9SGleb Smirnoff 		;
35463b3a8eb9SGleb Smirnoff 
35473b3a8eb9SGleb Smirnoff icmptype	: STRING			{
35483b3a8eb9SGleb Smirnoff 			const struct icmptypeent	*p;
35493b3a8eb9SGleb Smirnoff 
35503b3a8eb9SGleb Smirnoff 			if ((p = geticmptypebyname($1, AF_INET)) == NULL) {
35513b3a8eb9SGleb Smirnoff 				yyerror("unknown icmp-type %s", $1);
35523b3a8eb9SGleb Smirnoff 				free($1);
35533b3a8eb9SGleb Smirnoff 				YYERROR;
35543b3a8eb9SGleb Smirnoff 			}
35553b3a8eb9SGleb Smirnoff 			$$ = p->type + 1;
35563b3a8eb9SGleb Smirnoff 			free($1);
35573b3a8eb9SGleb Smirnoff 		}
35583b3a8eb9SGleb Smirnoff 		| NUMBER			{
35593b3a8eb9SGleb Smirnoff 			if ($1 < 0 || $1 > 255) {
35603b3a8eb9SGleb Smirnoff 				yyerror("illegal icmp-type %lu", $1);
35613b3a8eb9SGleb Smirnoff 				YYERROR;
35623b3a8eb9SGleb Smirnoff 			}
35633b3a8eb9SGleb Smirnoff 			$$ = $1 + 1;
35643b3a8eb9SGleb Smirnoff 		}
35653b3a8eb9SGleb Smirnoff 		;
35663b3a8eb9SGleb Smirnoff 
35673b3a8eb9SGleb Smirnoff icmp6type	: STRING			{
35683b3a8eb9SGleb Smirnoff 			const struct icmptypeent	*p;
35693b3a8eb9SGleb Smirnoff 
35703b3a8eb9SGleb Smirnoff 			if ((p = geticmptypebyname($1, AF_INET6)) ==
35713b3a8eb9SGleb Smirnoff 			    NULL) {
35723b3a8eb9SGleb Smirnoff 				yyerror("unknown icmp6-type %s", $1);
35733b3a8eb9SGleb Smirnoff 				free($1);
35743b3a8eb9SGleb Smirnoff 				YYERROR;
35753b3a8eb9SGleb Smirnoff 			}
35763b3a8eb9SGleb Smirnoff 			$$ = p->type + 1;
35773b3a8eb9SGleb Smirnoff 			free($1);
35783b3a8eb9SGleb Smirnoff 		}
35793b3a8eb9SGleb Smirnoff 		| NUMBER			{
35803b3a8eb9SGleb Smirnoff 			if ($1 < 0 || $1 > 255) {
35813b3a8eb9SGleb Smirnoff 				yyerror("illegal icmp6-type %lu", $1);
35823b3a8eb9SGleb Smirnoff 				YYERROR;
35833b3a8eb9SGleb Smirnoff 			}
35843b3a8eb9SGleb Smirnoff 			$$ = $1 + 1;
35853b3a8eb9SGleb Smirnoff 		}
35863b3a8eb9SGleb Smirnoff 		;
35873b3a8eb9SGleb Smirnoff 
35883b3a8eb9SGleb Smirnoff tos	: STRING			{
35891f495578SKristof Provost 			int val;
35901f495578SKristof Provost 			char *end;
35911f495578SKristof Provost 
35921f495578SKristof Provost 			if (map_tos($1, &val))
35931f495578SKristof Provost 				$$ = val;
35941f495578SKristof Provost 			else if ($1[0] == '0' && $1[1] == 'x') {
35951f495578SKristof Provost 				errno = 0;
35961f495578SKristof Provost 				$$ = strtoul($1, &end, 16);
35971f495578SKristof Provost 				if (errno || *end != '\0')
35981f495578SKristof Provost 					$$ = 256;
35991f495578SKristof Provost 			} else
36000cd7a91aSKristof Provost 				$$ = 256;		/* flag bad argument */
36010cd7a91aSKristof Provost 			if ($$ < 0 || $$ > 255) {
36023b3a8eb9SGleb Smirnoff 				yyerror("illegal tos value %s", $1);
36033b3a8eb9SGleb Smirnoff 				free($1);
36043b3a8eb9SGleb Smirnoff 				YYERROR;
36053b3a8eb9SGleb Smirnoff 			}
36063b3a8eb9SGleb Smirnoff 			free($1);
36073b3a8eb9SGleb Smirnoff 		}
36083b3a8eb9SGleb Smirnoff 		| NUMBER			{
36093b3a8eb9SGleb Smirnoff 			$$ = $1;
36100cd7a91aSKristof Provost 			if ($$ < 0 || $$ > 255) {
36113b3a8eb9SGleb Smirnoff 				yyerror("illegal tos value %s", $1);
36123b3a8eb9SGleb Smirnoff 				YYERROR;
36133b3a8eb9SGleb Smirnoff 			}
36143b3a8eb9SGleb Smirnoff 		}
36153b3a8eb9SGleb Smirnoff 		;
36163b3a8eb9SGleb Smirnoff 
36173b3a8eb9SGleb Smirnoff sourcetrack	: SOURCETRACK		{ $$ = PF_SRCTRACK; }
36183b3a8eb9SGleb Smirnoff 		| SOURCETRACK GLOBAL	{ $$ = PF_SRCTRACK_GLOBAL; }
36193b3a8eb9SGleb Smirnoff 		| SOURCETRACK RULE	{ $$ = PF_SRCTRACK_RULE; }
36203b3a8eb9SGleb Smirnoff 		;
36213b3a8eb9SGleb Smirnoff 
36223b3a8eb9SGleb Smirnoff statelock	: IFBOUND {
36233b3a8eb9SGleb Smirnoff 			$$ = PFRULE_IFBOUND;
36243b3a8eb9SGleb Smirnoff 		}
36253b3a8eb9SGleb Smirnoff 		| FLOATING {
36263b3a8eb9SGleb Smirnoff 			$$ = 0;
36273b3a8eb9SGleb Smirnoff 		}
36283b3a8eb9SGleb Smirnoff 		;
36293b3a8eb9SGleb Smirnoff 
36303b3a8eb9SGleb Smirnoff keep		: NO STATE			{
36313b3a8eb9SGleb Smirnoff 			$$.action = 0;
36323b3a8eb9SGleb Smirnoff 			$$.options = NULL;
36333b3a8eb9SGleb Smirnoff 		}
36343b3a8eb9SGleb Smirnoff 		| KEEP STATE state_opt_spec	{
36353b3a8eb9SGleb Smirnoff 			$$.action = PF_STATE_NORMAL;
36363b3a8eb9SGleb Smirnoff 			$$.options = $3;
36373b3a8eb9SGleb Smirnoff 		}
36383b3a8eb9SGleb Smirnoff 		| MODULATE STATE state_opt_spec {
36393b3a8eb9SGleb Smirnoff 			$$.action = PF_STATE_MODULATE;
36403b3a8eb9SGleb Smirnoff 			$$.options = $3;
36413b3a8eb9SGleb Smirnoff 		}
36423b3a8eb9SGleb Smirnoff 		| SYNPROXY STATE state_opt_spec {
36433b3a8eb9SGleb Smirnoff 			$$.action = PF_STATE_SYNPROXY;
36443b3a8eb9SGleb Smirnoff 			$$.options = $3;
36453b3a8eb9SGleb Smirnoff 		}
36463b3a8eb9SGleb Smirnoff 		;
36473b3a8eb9SGleb Smirnoff 
36483b3a8eb9SGleb Smirnoff flush		: /* empty */			{ $$ = 0; }
36493b3a8eb9SGleb Smirnoff 		| FLUSH				{ $$ = PF_FLUSH; }
36503b3a8eb9SGleb Smirnoff 		| FLUSH GLOBAL			{
36513b3a8eb9SGleb Smirnoff 			$$ = PF_FLUSH | PF_FLUSH_GLOBAL;
36523b3a8eb9SGleb Smirnoff 		}
36533b3a8eb9SGleb Smirnoff 		;
36543b3a8eb9SGleb Smirnoff 
36553b3a8eb9SGleb Smirnoff state_opt_spec	: '(' state_opt_list ')'	{ $$ = $2; }
36563b3a8eb9SGleb Smirnoff 		| /* empty */			{ $$ = NULL; }
36573b3a8eb9SGleb Smirnoff 		;
36583b3a8eb9SGleb Smirnoff 
36593b3a8eb9SGleb Smirnoff state_opt_list	: state_opt_item		{ $$ = $1; }
36603b3a8eb9SGleb Smirnoff 		| state_opt_list comma state_opt_item {
36613b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
36623b3a8eb9SGleb Smirnoff 			$1->tail = $3;
36633b3a8eb9SGleb Smirnoff 			$$ = $1;
36643b3a8eb9SGleb Smirnoff 		}
36653b3a8eb9SGleb Smirnoff 		;
36663b3a8eb9SGleb Smirnoff 
36673b3a8eb9SGleb Smirnoff state_opt_item	: MAXIMUM NUMBER		{
36683b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > UINT_MAX) {
36693b3a8eb9SGleb Smirnoff 				yyerror("only positive values permitted");
36703b3a8eb9SGleb Smirnoff 				YYERROR;
36713b3a8eb9SGleb Smirnoff 			}
36723b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
36733b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
36743b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
36753b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_MAX;
36763b3a8eb9SGleb Smirnoff 			$$->data.max_states = $2;
36773b3a8eb9SGleb Smirnoff 			$$->next = NULL;
36783b3a8eb9SGleb Smirnoff 			$$->tail = $$;
36793b3a8eb9SGleb Smirnoff 		}
36803b3a8eb9SGleb Smirnoff 		| NOSYNC				{
36813b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
36823b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
36833b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
36843b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_NOSYNC;
36853b3a8eb9SGleb Smirnoff 			$$->next = NULL;
36863b3a8eb9SGleb Smirnoff 			$$->tail = $$;
36873b3a8eb9SGleb Smirnoff 		}
36883b3a8eb9SGleb Smirnoff 		| MAXSRCSTATES NUMBER			{
36893b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > UINT_MAX) {
36903b3a8eb9SGleb Smirnoff 				yyerror("only positive values permitted");
36913b3a8eb9SGleb Smirnoff 				YYERROR;
36923b3a8eb9SGleb Smirnoff 			}
36933b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
36943b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
36953b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
36963b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_MAX_SRC_STATES;
36973b3a8eb9SGleb Smirnoff 			$$->data.max_src_states = $2;
36983b3a8eb9SGleb Smirnoff 			$$->next = NULL;
36993b3a8eb9SGleb Smirnoff 			$$->tail = $$;
37003b3a8eb9SGleb Smirnoff 		}
37013b3a8eb9SGleb Smirnoff 		| MAXSRCCONN NUMBER			{
37023b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > UINT_MAX) {
37033b3a8eb9SGleb Smirnoff 				yyerror("only positive values permitted");
37043b3a8eb9SGleb Smirnoff 				YYERROR;
37053b3a8eb9SGleb Smirnoff 			}
37063b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
37073b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
37083b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
37093b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_MAX_SRC_CONN;
37103b3a8eb9SGleb Smirnoff 			$$->data.max_src_conn = $2;
37113b3a8eb9SGleb Smirnoff 			$$->next = NULL;
37123b3a8eb9SGleb Smirnoff 			$$->tail = $$;
37133b3a8eb9SGleb Smirnoff 		}
37143b3a8eb9SGleb Smirnoff 		| MAXSRCCONNRATE NUMBER '/' NUMBER	{
37153b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > UINT_MAX ||
37163b3a8eb9SGleb Smirnoff 			    $4 < 0 || $4 > UINT_MAX) {
37173b3a8eb9SGleb Smirnoff 				yyerror("only positive values permitted");
37183b3a8eb9SGleb Smirnoff 				YYERROR;
37193b3a8eb9SGleb Smirnoff 			}
37203b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
37213b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
37223b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
37233b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_MAX_SRC_CONN_RATE;
37243b3a8eb9SGleb Smirnoff 			$$->data.max_src_conn_rate.limit = $2;
37253b3a8eb9SGleb Smirnoff 			$$->data.max_src_conn_rate.seconds = $4;
37263b3a8eb9SGleb Smirnoff 			$$->next = NULL;
37273b3a8eb9SGleb Smirnoff 			$$->tail = $$;
37283b3a8eb9SGleb Smirnoff 		}
37293b3a8eb9SGleb Smirnoff 		| OVERLOAD '<' STRING '>' flush		{
37303b3a8eb9SGleb Smirnoff 			if (strlen($3) >= PF_TABLE_NAME_SIZE) {
37313b3a8eb9SGleb Smirnoff 				yyerror("table name '%s' too long", $3);
37323b3a8eb9SGleb Smirnoff 				free($3);
37333b3a8eb9SGleb Smirnoff 				YYERROR;
37343b3a8eb9SGleb Smirnoff 			}
37353b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
37363b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
37373b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
37383b3a8eb9SGleb Smirnoff 			if (strlcpy($$->data.overload.tblname, $3,
37393b3a8eb9SGleb Smirnoff 			    PF_TABLE_NAME_SIZE) >= PF_TABLE_NAME_SIZE)
37403b3a8eb9SGleb Smirnoff 				errx(1, "state_opt_item: strlcpy");
37413b3a8eb9SGleb Smirnoff 			free($3);
37423b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_OVERLOAD;
37433b3a8eb9SGleb Smirnoff 			$$->data.overload.flush = $5;
37443b3a8eb9SGleb Smirnoff 			$$->next = NULL;
37453b3a8eb9SGleb Smirnoff 			$$->tail = $$;
37463b3a8eb9SGleb Smirnoff 		}
37473b3a8eb9SGleb Smirnoff 		| MAXSRCNODES NUMBER			{
37483b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > UINT_MAX) {
37493b3a8eb9SGleb Smirnoff 				yyerror("only positive values permitted");
37503b3a8eb9SGleb Smirnoff 				YYERROR;
37513b3a8eb9SGleb Smirnoff 			}
37523b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
37533b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
37543b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
37553b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_MAX_SRC_NODES;
37563b3a8eb9SGleb Smirnoff 			$$->data.max_src_nodes = $2;
37573b3a8eb9SGleb Smirnoff 			$$->next = NULL;
37583b3a8eb9SGleb Smirnoff 			$$->tail = $$;
37593b3a8eb9SGleb Smirnoff 		}
37603b3a8eb9SGleb Smirnoff 		| sourcetrack {
37613b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
37623b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
37633b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
37643b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_SRCTRACK;
37653b3a8eb9SGleb Smirnoff 			$$->data.src_track = $1;
37663b3a8eb9SGleb Smirnoff 			$$->next = NULL;
37673b3a8eb9SGleb Smirnoff 			$$->tail = $$;
37683b3a8eb9SGleb Smirnoff 		}
37693b3a8eb9SGleb Smirnoff 		| statelock {
37703b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
37713b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
37723b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
37733b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_STATELOCK;
37743b3a8eb9SGleb Smirnoff 			$$->data.statelock = $1;
37753b3a8eb9SGleb Smirnoff 			$$->next = NULL;
37763b3a8eb9SGleb Smirnoff 			$$->tail = $$;
37773b3a8eb9SGleb Smirnoff 		}
37783b3a8eb9SGleb Smirnoff 		| SLOPPY {
37793b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
37803b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
37813b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
37823b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_SLOPPY;
37833b3a8eb9SGleb Smirnoff 			$$->next = NULL;
37843b3a8eb9SGleb Smirnoff 			$$->tail = $$;
37853b3a8eb9SGleb Smirnoff 		}
37863b3a8eb9SGleb Smirnoff 		| STRING NUMBER			{
37873b3a8eb9SGleb Smirnoff 			int	i;
37883b3a8eb9SGleb Smirnoff 
37893b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > UINT_MAX) {
37903b3a8eb9SGleb Smirnoff 				yyerror("only positive values permitted");
37913b3a8eb9SGleb Smirnoff 				YYERROR;
37923b3a8eb9SGleb Smirnoff 			}
37933b3a8eb9SGleb Smirnoff 			for (i = 0; pf_timeouts[i].name &&
37943b3a8eb9SGleb Smirnoff 			    strcmp(pf_timeouts[i].name, $1); ++i)
37953b3a8eb9SGleb Smirnoff 				;	/* nothing */
37963b3a8eb9SGleb Smirnoff 			if (!pf_timeouts[i].name) {
37973b3a8eb9SGleb Smirnoff 				yyerror("illegal timeout name %s", $1);
37983b3a8eb9SGleb Smirnoff 				free($1);
37993b3a8eb9SGleb Smirnoff 				YYERROR;
38003b3a8eb9SGleb Smirnoff 			}
38013b3a8eb9SGleb Smirnoff 			if (strchr(pf_timeouts[i].name, '.') == NULL) {
38023b3a8eb9SGleb Smirnoff 				yyerror("illegal state timeout %s", $1);
38033b3a8eb9SGleb Smirnoff 				free($1);
38043b3a8eb9SGleb Smirnoff 				YYERROR;
38053b3a8eb9SGleb Smirnoff 			}
38063b3a8eb9SGleb Smirnoff 			free($1);
38073b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
38083b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
38093b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
38103b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_TIMEOUT;
38113b3a8eb9SGleb Smirnoff 			$$->data.timeout.number = pf_timeouts[i].timeout;
38123b3a8eb9SGleb Smirnoff 			$$->data.timeout.seconds = $2;
38133b3a8eb9SGleb Smirnoff 			$$->next = NULL;
38143b3a8eb9SGleb Smirnoff 			$$->tail = $$;
38153b3a8eb9SGleb Smirnoff 		}
38163b3a8eb9SGleb Smirnoff 		;
38173b3a8eb9SGleb Smirnoff 
38183b3a8eb9SGleb Smirnoff label		: LABEL STRING			{
38193b3a8eb9SGleb Smirnoff 			$$ = $2;
38203b3a8eb9SGleb Smirnoff 		}
38213b3a8eb9SGleb Smirnoff 		;
38223b3a8eb9SGleb Smirnoff 
38233b3a8eb9SGleb Smirnoff qname		: QUEUE STRING				{
38243b3a8eb9SGleb Smirnoff 			$$.qname = $2;
38253b3a8eb9SGleb Smirnoff 			$$.pqname = NULL;
38263b3a8eb9SGleb Smirnoff 		}
38273b3a8eb9SGleb Smirnoff 		| QUEUE '(' STRING ')'			{
38283b3a8eb9SGleb Smirnoff 			$$.qname = $3;
38293b3a8eb9SGleb Smirnoff 			$$.pqname = NULL;
38303b3a8eb9SGleb Smirnoff 		}
38313b3a8eb9SGleb Smirnoff 		| QUEUE '(' STRING comma STRING ')'	{
38323b3a8eb9SGleb Smirnoff 			$$.qname = $3;
38333b3a8eb9SGleb Smirnoff 			$$.pqname = $5;
38343b3a8eb9SGleb Smirnoff 		}
38353b3a8eb9SGleb Smirnoff 		;
38363b3a8eb9SGleb Smirnoff 
38373b3a8eb9SGleb Smirnoff no		: /* empty */			{ $$ = 0; }
38383b3a8eb9SGleb Smirnoff 		| NO				{ $$ = 1; }
38393b3a8eb9SGleb Smirnoff 		;
38403b3a8eb9SGleb Smirnoff 
38413b3a8eb9SGleb Smirnoff portstar	: numberstring			{
38423b3a8eb9SGleb Smirnoff 			if (parseport($1, &$$, PPORT_RANGE|PPORT_STAR) == -1) {
38433b3a8eb9SGleb Smirnoff 				free($1);
38443b3a8eb9SGleb Smirnoff 				YYERROR;
38453b3a8eb9SGleb Smirnoff 			}
38463b3a8eb9SGleb Smirnoff 			free($1);
38473b3a8eb9SGleb Smirnoff 		}
38483b3a8eb9SGleb Smirnoff 		;
38493b3a8eb9SGleb Smirnoff 
38503b3a8eb9SGleb Smirnoff redirspec	: host				{ $$ = $1; }
38513b3a8eb9SGleb Smirnoff 		| '{' optnl redir_host_list '}'	{ $$ = $3; }
38523b3a8eb9SGleb Smirnoff 		;
38533b3a8eb9SGleb Smirnoff 
38543b3a8eb9SGleb Smirnoff redir_host_list	: host optnl			{ $$ = $1; }
38553b3a8eb9SGleb Smirnoff 		| redir_host_list comma host optnl {
38563b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
38573b3a8eb9SGleb Smirnoff 			$1->tail = $3->tail;
38583b3a8eb9SGleb Smirnoff 			$$ = $1;
38593b3a8eb9SGleb Smirnoff 		}
38603b3a8eb9SGleb Smirnoff 		;
38613b3a8eb9SGleb Smirnoff 
38623b3a8eb9SGleb Smirnoff redirpool	: /* empty */			{ $$ = NULL; }
38633b3a8eb9SGleb Smirnoff 		| ARROW redirspec		{
38643b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct redirection));
38653b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
38663b3a8eb9SGleb Smirnoff 				err(1, "redirection: calloc");
38673b3a8eb9SGleb Smirnoff 			$$->host = $2;
38683b3a8eb9SGleb Smirnoff 			$$->rport.a = $$->rport.b = $$->rport.t = 0;
38693b3a8eb9SGleb Smirnoff 		}
38703b3a8eb9SGleb Smirnoff 		| ARROW redirspec PORT portstar	{
38713b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct redirection));
38723b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
38733b3a8eb9SGleb Smirnoff 				err(1, "redirection: calloc");
38743b3a8eb9SGleb Smirnoff 			$$->host = $2;
38753b3a8eb9SGleb Smirnoff 			$$->rport = $4;
38763b3a8eb9SGleb Smirnoff 		}
38773b3a8eb9SGleb Smirnoff 		;
38783b3a8eb9SGleb Smirnoff 
38793b3a8eb9SGleb Smirnoff hashkey		: /* empty */
38803b3a8eb9SGleb Smirnoff 		{
38813b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct pf_poolhashkey));
38823b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
38833b3a8eb9SGleb Smirnoff 				err(1, "hashkey: calloc");
38843b3a8eb9SGleb Smirnoff 			$$->key32[0] = arc4random();
38853b3a8eb9SGleb Smirnoff 			$$->key32[1] = arc4random();
38863b3a8eb9SGleb Smirnoff 			$$->key32[2] = arc4random();
38873b3a8eb9SGleb Smirnoff 			$$->key32[3] = arc4random();
38883b3a8eb9SGleb Smirnoff 		}
38893b3a8eb9SGleb Smirnoff 		| string
38903b3a8eb9SGleb Smirnoff 		{
38913b3a8eb9SGleb Smirnoff 			if (!strncmp($1, "0x", 2)) {
38923b3a8eb9SGleb Smirnoff 				if (strlen($1) != 34) {
38933b3a8eb9SGleb Smirnoff 					free($1);
38943b3a8eb9SGleb Smirnoff 					yyerror("hex key must be 128 bits "
38953b3a8eb9SGleb Smirnoff 						"(32 hex digits) long");
38963b3a8eb9SGleb Smirnoff 					YYERROR;
38973b3a8eb9SGleb Smirnoff 				}
38983b3a8eb9SGleb Smirnoff 				$$ = calloc(1, sizeof(struct pf_poolhashkey));
38993b3a8eb9SGleb Smirnoff 				if ($$ == NULL)
39003b3a8eb9SGleb Smirnoff 					err(1, "hashkey: calloc");
39013b3a8eb9SGleb Smirnoff 
39023b3a8eb9SGleb Smirnoff 				if (sscanf($1, "0x%8x%8x%8x%8x",
39033b3a8eb9SGleb Smirnoff 				    &$$->key32[0], &$$->key32[1],
39043b3a8eb9SGleb Smirnoff 				    &$$->key32[2], &$$->key32[3]) != 4) {
39053b3a8eb9SGleb Smirnoff 					free($$);
39063b3a8eb9SGleb Smirnoff 					free($1);
39073b3a8eb9SGleb Smirnoff 					yyerror("invalid hex key");
39083b3a8eb9SGleb Smirnoff 					YYERROR;
39093b3a8eb9SGleb Smirnoff 				}
39103b3a8eb9SGleb Smirnoff 			} else {
39113b3a8eb9SGleb Smirnoff 				MD5_CTX	context;
39123b3a8eb9SGleb Smirnoff 
39133b3a8eb9SGleb Smirnoff 				$$ = calloc(1, sizeof(struct pf_poolhashkey));
39143b3a8eb9SGleb Smirnoff 				if ($$ == NULL)
39153b3a8eb9SGleb Smirnoff 					err(1, "hashkey: calloc");
39163b3a8eb9SGleb Smirnoff 				MD5Init(&context);
39173b3a8eb9SGleb Smirnoff 				MD5Update(&context, (unsigned char *)$1,
39183b3a8eb9SGleb Smirnoff 				    strlen($1));
39193b3a8eb9SGleb Smirnoff 				MD5Final((unsigned char *)$$, &context);
39203b3a8eb9SGleb Smirnoff 				HTONL($$->key32[0]);
39213b3a8eb9SGleb Smirnoff 				HTONL($$->key32[1]);
39223b3a8eb9SGleb Smirnoff 				HTONL($$->key32[2]);
39233b3a8eb9SGleb Smirnoff 				HTONL($$->key32[3]);
39243b3a8eb9SGleb Smirnoff 			}
39253b3a8eb9SGleb Smirnoff 			free($1);
39263b3a8eb9SGleb Smirnoff 		}
39273b3a8eb9SGleb Smirnoff 		;
39283b3a8eb9SGleb Smirnoff 
39293b3a8eb9SGleb Smirnoff pool_opts	:	{ bzero(&pool_opts, sizeof pool_opts); }
39303b3a8eb9SGleb Smirnoff 		    pool_opts_l
39313b3a8eb9SGleb Smirnoff 			{ $$ = pool_opts; }
39323b3a8eb9SGleb Smirnoff 		| /* empty */	{
39333b3a8eb9SGleb Smirnoff 			bzero(&pool_opts, sizeof pool_opts);
39343b3a8eb9SGleb Smirnoff 			$$ = pool_opts;
39353b3a8eb9SGleb Smirnoff 		}
39363b3a8eb9SGleb Smirnoff 		;
39373b3a8eb9SGleb Smirnoff 
39383b3a8eb9SGleb Smirnoff pool_opts_l	: pool_opts_l pool_opt
39393b3a8eb9SGleb Smirnoff 		| pool_opt
39403b3a8eb9SGleb Smirnoff 		;
39413b3a8eb9SGleb Smirnoff 
39423b3a8eb9SGleb Smirnoff pool_opt	: BITMASK	{
39433b3a8eb9SGleb Smirnoff 			if (pool_opts.type) {
39443b3a8eb9SGleb Smirnoff 				yyerror("pool type cannot be redefined");
39453b3a8eb9SGleb Smirnoff 				YYERROR;
39463b3a8eb9SGleb Smirnoff 			}
39473b3a8eb9SGleb Smirnoff 			pool_opts.type =  PF_POOL_BITMASK;
39483b3a8eb9SGleb Smirnoff 		}
39493b3a8eb9SGleb Smirnoff 		| RANDOM	{
39503b3a8eb9SGleb Smirnoff 			if (pool_opts.type) {
39513b3a8eb9SGleb Smirnoff 				yyerror("pool type cannot be redefined");
39523b3a8eb9SGleb Smirnoff 				YYERROR;
39533b3a8eb9SGleb Smirnoff 			}
39543b3a8eb9SGleb Smirnoff 			pool_opts.type = PF_POOL_RANDOM;
39553b3a8eb9SGleb Smirnoff 		}
39563b3a8eb9SGleb Smirnoff 		| SOURCEHASH hashkey {
39573b3a8eb9SGleb Smirnoff 			if (pool_opts.type) {
39583b3a8eb9SGleb Smirnoff 				yyerror("pool type cannot be redefined");
39593b3a8eb9SGleb Smirnoff 				YYERROR;
39603b3a8eb9SGleb Smirnoff 			}
39613b3a8eb9SGleb Smirnoff 			pool_opts.type = PF_POOL_SRCHASH;
39623b3a8eb9SGleb Smirnoff 			pool_opts.key = $2;
39633b3a8eb9SGleb Smirnoff 		}
39643b3a8eb9SGleb Smirnoff 		| ROUNDROBIN	{
39653b3a8eb9SGleb Smirnoff 			if (pool_opts.type) {
39663b3a8eb9SGleb Smirnoff 				yyerror("pool type cannot be redefined");
39673b3a8eb9SGleb Smirnoff 				YYERROR;
39683b3a8eb9SGleb Smirnoff 			}
39693b3a8eb9SGleb Smirnoff 			pool_opts.type = PF_POOL_ROUNDROBIN;
39703b3a8eb9SGleb Smirnoff 		}
39713b3a8eb9SGleb Smirnoff 		| STATICPORT	{
39723b3a8eb9SGleb Smirnoff 			if (pool_opts.staticport) {
39733b3a8eb9SGleb Smirnoff 				yyerror("static-port cannot be redefined");
39743b3a8eb9SGleb Smirnoff 				YYERROR;
39753b3a8eb9SGleb Smirnoff 			}
39763b3a8eb9SGleb Smirnoff 			pool_opts.staticport = 1;
39773b3a8eb9SGleb Smirnoff 		}
39783b3a8eb9SGleb Smirnoff 		| STICKYADDRESS	{
39793b3a8eb9SGleb Smirnoff 			if (filter_opts.marker & POM_STICKYADDRESS) {
39803b3a8eb9SGleb Smirnoff 				yyerror("sticky-address cannot be redefined");
39813b3a8eb9SGleb Smirnoff 				YYERROR;
39823b3a8eb9SGleb Smirnoff 			}
39833b3a8eb9SGleb Smirnoff 			pool_opts.marker |= POM_STICKYADDRESS;
39843b3a8eb9SGleb Smirnoff 			pool_opts.opts |= PF_POOL_STICKYADDR;
39853b3a8eb9SGleb Smirnoff 		}
39863b3a8eb9SGleb Smirnoff 		;
39873b3a8eb9SGleb Smirnoff 
39883b3a8eb9SGleb Smirnoff redirection	: /* empty */			{ $$ = NULL; }
39893b3a8eb9SGleb Smirnoff 		| ARROW host			{
39903b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct redirection));
39913b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
39923b3a8eb9SGleb Smirnoff 				err(1, "redirection: calloc");
39933b3a8eb9SGleb Smirnoff 			$$->host = $2;
39943b3a8eb9SGleb Smirnoff 			$$->rport.a = $$->rport.b = $$->rport.t = 0;
39953b3a8eb9SGleb Smirnoff 		}
39963b3a8eb9SGleb Smirnoff 		| ARROW host PORT portstar	{
39973b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct redirection));
39983b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
39993b3a8eb9SGleb Smirnoff 				err(1, "redirection: calloc");
40003b3a8eb9SGleb Smirnoff 			$$->host = $2;
40013b3a8eb9SGleb Smirnoff 			$$->rport = $4;
40023b3a8eb9SGleb Smirnoff 		}
40033b3a8eb9SGleb Smirnoff 		;
40043b3a8eb9SGleb Smirnoff 
40053b3a8eb9SGleb Smirnoff natpasslog	: /* empty */	{ $$.b1 = $$.b2 = 0; $$.w2 = 0; }
40063b3a8eb9SGleb Smirnoff 		| PASS		{ $$.b1 = 1; $$.b2 = 0; $$.w2 = 0; }
40073b3a8eb9SGleb Smirnoff 		| PASS log	{ $$.b1 = 1; $$.b2 = $2.log; $$.w2 = $2.logif; }
40083b3a8eb9SGleb Smirnoff 		| log		{ $$.b1 = 0; $$.b2 = $1.log; $$.w2 = $1.logif; }
40093b3a8eb9SGleb Smirnoff 		;
40103b3a8eb9SGleb Smirnoff 
40113b3a8eb9SGleb Smirnoff nataction	: no NAT natpasslog {
40123b3a8eb9SGleb Smirnoff 			if ($1 && $3.b1) {
40133b3a8eb9SGleb Smirnoff 				yyerror("\"pass\" not valid with \"no\"");
40143b3a8eb9SGleb Smirnoff 				YYERROR;
40153b3a8eb9SGleb Smirnoff 			}
40163b3a8eb9SGleb Smirnoff 			if ($1)
40173b3a8eb9SGleb Smirnoff 				$$.b1 = PF_NONAT;
40183b3a8eb9SGleb Smirnoff 			else
40193b3a8eb9SGleb Smirnoff 				$$.b1 = PF_NAT;
40203b3a8eb9SGleb Smirnoff 			$$.b2 = $3.b1;
40213b3a8eb9SGleb Smirnoff 			$$.w = $3.b2;
40223b3a8eb9SGleb Smirnoff 			$$.w2 = $3.w2;
40233b3a8eb9SGleb Smirnoff 		}
40243b3a8eb9SGleb Smirnoff 		| no RDR natpasslog {
40253b3a8eb9SGleb Smirnoff 			if ($1 && $3.b1) {
40263b3a8eb9SGleb Smirnoff 				yyerror("\"pass\" not valid with \"no\"");
40273b3a8eb9SGleb Smirnoff 				YYERROR;
40283b3a8eb9SGleb Smirnoff 			}
40293b3a8eb9SGleb Smirnoff 			if ($1)
40303b3a8eb9SGleb Smirnoff 				$$.b1 = PF_NORDR;
40313b3a8eb9SGleb Smirnoff 			else
40323b3a8eb9SGleb Smirnoff 				$$.b1 = PF_RDR;
40333b3a8eb9SGleb Smirnoff 			$$.b2 = $3.b1;
40343b3a8eb9SGleb Smirnoff 			$$.w = $3.b2;
40353b3a8eb9SGleb Smirnoff 			$$.w2 = $3.w2;
40363b3a8eb9SGleb Smirnoff 		}
40373b3a8eb9SGleb Smirnoff 		;
40383b3a8eb9SGleb Smirnoff 
40393b3a8eb9SGleb Smirnoff natrule		: nataction interface af proto fromto tag tagged rtable
40403b3a8eb9SGleb Smirnoff 		    redirpool pool_opts
40413b3a8eb9SGleb Smirnoff 		{
40423b3a8eb9SGleb Smirnoff 			struct pf_rule	r;
40433b3a8eb9SGleb Smirnoff 
40443b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_NAT))
40453b3a8eb9SGleb Smirnoff 				YYERROR;
40463b3a8eb9SGleb Smirnoff 
40473b3a8eb9SGleb Smirnoff 			memset(&r, 0, sizeof(r));
40483b3a8eb9SGleb Smirnoff 
40493b3a8eb9SGleb Smirnoff 			r.action = $1.b1;
40503b3a8eb9SGleb Smirnoff 			r.natpass = $1.b2;
40513b3a8eb9SGleb Smirnoff 			r.log = $1.w;
40523b3a8eb9SGleb Smirnoff 			r.logif = $1.w2;
40533b3a8eb9SGleb Smirnoff 			r.af = $3;
40543b3a8eb9SGleb Smirnoff 
40553b3a8eb9SGleb Smirnoff 			if (!r.af) {
40563b3a8eb9SGleb Smirnoff 				if ($5.src.host && $5.src.host->af &&
40573b3a8eb9SGleb Smirnoff 				    !$5.src.host->ifindex)
40583b3a8eb9SGleb Smirnoff 					r.af = $5.src.host->af;
40593b3a8eb9SGleb Smirnoff 				else if ($5.dst.host && $5.dst.host->af &&
40603b3a8eb9SGleb Smirnoff 				    !$5.dst.host->ifindex)
40613b3a8eb9SGleb Smirnoff 					r.af = $5.dst.host->af;
40623b3a8eb9SGleb Smirnoff 			}
40633b3a8eb9SGleb Smirnoff 
40643b3a8eb9SGleb Smirnoff 			if ($6 != NULL)
40653b3a8eb9SGleb Smirnoff 				if (strlcpy(r.tagname, $6, PF_TAG_NAME_SIZE) >=
40663b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) {
40673b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
40683b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
40693b3a8eb9SGleb Smirnoff 					YYERROR;
40703b3a8eb9SGleb Smirnoff 				}
40713b3a8eb9SGleb Smirnoff 
40723b3a8eb9SGleb Smirnoff 			if ($7.name)
40733b3a8eb9SGleb Smirnoff 				if (strlcpy(r.match_tagname, $7.name,
40743b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
40753b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
40763b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
40773b3a8eb9SGleb Smirnoff 					YYERROR;
40783b3a8eb9SGleb Smirnoff 				}
40793b3a8eb9SGleb Smirnoff 			r.match_tag_not = $7.neg;
40803b3a8eb9SGleb Smirnoff 			r.rtableid = $8;
40813b3a8eb9SGleb Smirnoff 
40823b3a8eb9SGleb Smirnoff 			if (r.action == PF_NONAT || r.action == PF_NORDR) {
40833b3a8eb9SGleb Smirnoff 				if ($9 != NULL) {
40843b3a8eb9SGleb Smirnoff 					yyerror("translation rule with 'no' "
40853b3a8eb9SGleb Smirnoff 					    "does not need '->'");
40863b3a8eb9SGleb Smirnoff 					YYERROR;
40873b3a8eb9SGleb Smirnoff 				}
40883b3a8eb9SGleb Smirnoff 			} else {
40893b3a8eb9SGleb Smirnoff 				if ($9 == NULL || $9->host == NULL) {
40903b3a8eb9SGleb Smirnoff 					yyerror("translation rule requires '-> "
40913b3a8eb9SGleb Smirnoff 					    "address'");
40923b3a8eb9SGleb Smirnoff 					YYERROR;
40933b3a8eb9SGleb Smirnoff 				}
40943b3a8eb9SGleb Smirnoff 				if (!r.af && ! $9->host->ifindex)
40953b3a8eb9SGleb Smirnoff 					r.af = $9->host->af;
40963b3a8eb9SGleb Smirnoff 
40973b3a8eb9SGleb Smirnoff 				remove_invalid_hosts(&$9->host, &r.af);
40983b3a8eb9SGleb Smirnoff 				if (invalid_redirect($9->host, r.af))
40993b3a8eb9SGleb Smirnoff 					YYERROR;
41003b3a8eb9SGleb Smirnoff 				if (check_netmask($9->host, r.af))
41013b3a8eb9SGleb Smirnoff 					YYERROR;
41023b3a8eb9SGleb Smirnoff 
41033b3a8eb9SGleb Smirnoff 				r.rpool.proxy_port[0] = ntohs($9->rport.a);
41043b3a8eb9SGleb Smirnoff 
41053b3a8eb9SGleb Smirnoff 				switch (r.action) {
41063b3a8eb9SGleb Smirnoff 				case PF_RDR:
41073b3a8eb9SGleb Smirnoff 					if (!$9->rport.b && $9->rport.t &&
41083b3a8eb9SGleb Smirnoff 					    $5.dst.port != NULL) {
41093b3a8eb9SGleb Smirnoff 						r.rpool.proxy_port[1] =
41103b3a8eb9SGleb Smirnoff 						    ntohs($9->rport.a) +
41113b3a8eb9SGleb Smirnoff 						    (ntohs(
41123b3a8eb9SGleb Smirnoff 						    $5.dst.port->port[1]) -
41133b3a8eb9SGleb Smirnoff 						    ntohs(
41143b3a8eb9SGleb Smirnoff 						    $5.dst.port->port[0]));
41153b3a8eb9SGleb Smirnoff 					} else
41163b3a8eb9SGleb Smirnoff 						r.rpool.proxy_port[1] =
41173b3a8eb9SGleb Smirnoff 						    ntohs($9->rport.b);
41183b3a8eb9SGleb Smirnoff 					break;
41193b3a8eb9SGleb Smirnoff 				case PF_NAT:
41203b3a8eb9SGleb Smirnoff 					r.rpool.proxy_port[1] =
41213b3a8eb9SGleb Smirnoff 					    ntohs($9->rport.b);
41223b3a8eb9SGleb Smirnoff 					if (!r.rpool.proxy_port[0] &&
41233b3a8eb9SGleb Smirnoff 					    !r.rpool.proxy_port[1]) {
41243b3a8eb9SGleb Smirnoff 						r.rpool.proxy_port[0] =
41253b3a8eb9SGleb Smirnoff 						    PF_NAT_PROXY_PORT_LOW;
41263b3a8eb9SGleb Smirnoff 						r.rpool.proxy_port[1] =
41273b3a8eb9SGleb Smirnoff 						    PF_NAT_PROXY_PORT_HIGH;
41283b3a8eb9SGleb Smirnoff 					} else if (!r.rpool.proxy_port[1])
41293b3a8eb9SGleb Smirnoff 						r.rpool.proxy_port[1] =
41303b3a8eb9SGleb Smirnoff 						    r.rpool.proxy_port[0];
41313b3a8eb9SGleb Smirnoff 					break;
41323b3a8eb9SGleb Smirnoff 				default:
41333b3a8eb9SGleb Smirnoff 					break;
41343b3a8eb9SGleb Smirnoff 				}
41353b3a8eb9SGleb Smirnoff 
41363b3a8eb9SGleb Smirnoff 				r.rpool.opts = $10.type;
41373b3a8eb9SGleb Smirnoff 				if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
41383b3a8eb9SGleb Smirnoff 				    PF_POOL_NONE && ($9->host->next != NULL ||
41393b3a8eb9SGleb Smirnoff 				    $9->host->addr.type == PF_ADDR_TABLE ||
41403b3a8eb9SGleb Smirnoff 				    DYNIF_MULTIADDR($9->host->addr)))
41413b3a8eb9SGleb Smirnoff 					r.rpool.opts = PF_POOL_ROUNDROBIN;
41423b3a8eb9SGleb Smirnoff 				if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
41433b3a8eb9SGleb Smirnoff 				    PF_POOL_ROUNDROBIN &&
41443b3a8eb9SGleb Smirnoff 				    disallow_table($9->host, "tables are only "
41453b3a8eb9SGleb Smirnoff 				    "supported in round-robin redirection "
41463b3a8eb9SGleb Smirnoff 				    "pools"))
41473b3a8eb9SGleb Smirnoff 					YYERROR;
41483b3a8eb9SGleb Smirnoff 				if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
41493b3a8eb9SGleb Smirnoff 				    PF_POOL_ROUNDROBIN &&
41503b3a8eb9SGleb Smirnoff 				    disallow_alias($9->host, "interface (%s) "
41513b3a8eb9SGleb Smirnoff 				    "is only supported in round-robin "
41523b3a8eb9SGleb Smirnoff 				    "redirection pools"))
41533b3a8eb9SGleb Smirnoff 					YYERROR;
41543b3a8eb9SGleb Smirnoff 				if ($9->host->next != NULL) {
41553b3a8eb9SGleb Smirnoff 					if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
41563b3a8eb9SGleb Smirnoff 					    PF_POOL_ROUNDROBIN) {
41573b3a8eb9SGleb Smirnoff 						yyerror("only round-robin "
41583b3a8eb9SGleb Smirnoff 						    "valid for multiple "
41593b3a8eb9SGleb Smirnoff 						    "redirection addresses");
41603b3a8eb9SGleb Smirnoff 						YYERROR;
41613b3a8eb9SGleb Smirnoff 					}
41623b3a8eb9SGleb Smirnoff 				}
41633b3a8eb9SGleb Smirnoff 			}
41643b3a8eb9SGleb Smirnoff 
41653b3a8eb9SGleb Smirnoff 			if ($10.key != NULL)
41663b3a8eb9SGleb Smirnoff 				memcpy(&r.rpool.key, $10.key,
41673b3a8eb9SGleb Smirnoff 				    sizeof(struct pf_poolhashkey));
41683b3a8eb9SGleb Smirnoff 
41693b3a8eb9SGleb Smirnoff 			 if ($10.opts)
41703b3a8eb9SGleb Smirnoff 				r.rpool.opts |= $10.opts;
41713b3a8eb9SGleb Smirnoff 
41723b3a8eb9SGleb Smirnoff 			if ($10.staticport) {
41733b3a8eb9SGleb Smirnoff 				if (r.action != PF_NAT) {
41743b3a8eb9SGleb Smirnoff 					yyerror("the 'static-port' option is "
41753b3a8eb9SGleb Smirnoff 					    "only valid with nat rules");
41763b3a8eb9SGleb Smirnoff 					YYERROR;
41773b3a8eb9SGleb Smirnoff 				}
41783b3a8eb9SGleb Smirnoff 				if (r.rpool.proxy_port[0] !=
41793b3a8eb9SGleb Smirnoff 				    PF_NAT_PROXY_PORT_LOW &&
41803b3a8eb9SGleb Smirnoff 				    r.rpool.proxy_port[1] !=
41813b3a8eb9SGleb Smirnoff 				    PF_NAT_PROXY_PORT_HIGH) {
41823b3a8eb9SGleb Smirnoff 					yyerror("the 'static-port' option can't"
41833b3a8eb9SGleb Smirnoff 					    " be used when specifying a port"
41843b3a8eb9SGleb Smirnoff 					    " range");
41853b3a8eb9SGleb Smirnoff 					YYERROR;
41863b3a8eb9SGleb Smirnoff 				}
41873b3a8eb9SGleb Smirnoff 				r.rpool.proxy_port[0] = 0;
41883b3a8eb9SGleb Smirnoff 				r.rpool.proxy_port[1] = 0;
41893b3a8eb9SGleb Smirnoff 			}
41903b3a8eb9SGleb Smirnoff 
41913b3a8eb9SGleb Smirnoff 			expand_rule(&r, $2, $9 == NULL ? NULL : $9->host, $4,
41923b3a8eb9SGleb Smirnoff 			    $5.src_os, $5.src.host, $5.src.port, $5.dst.host,
41933b3a8eb9SGleb Smirnoff 			    $5.dst.port, 0, 0, 0, "");
41943b3a8eb9SGleb Smirnoff 			free($9);
41953b3a8eb9SGleb Smirnoff 		}
41963b3a8eb9SGleb Smirnoff 		;
41973b3a8eb9SGleb Smirnoff 
41981e93588bSLuiz Otavio O Souza binatrule	: no BINAT natpasslog interface af proto FROM ipspec toipspec tag
41993b3a8eb9SGleb Smirnoff 		    tagged rtable redirection
42003b3a8eb9SGleb Smirnoff 		{
42013b3a8eb9SGleb Smirnoff 			struct pf_rule		binat;
42023b3a8eb9SGleb Smirnoff 			struct pf_pooladdr	*pa;
42033b3a8eb9SGleb Smirnoff 
42043b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_NAT))
42053b3a8eb9SGleb Smirnoff 				YYERROR;
42063b3a8eb9SGleb Smirnoff 			if (disallow_urpf_failed($9, "\"urpf-failed\" is not "
42073b3a8eb9SGleb Smirnoff 			    "permitted as a binat destination"))
42083b3a8eb9SGleb Smirnoff 				YYERROR;
42093b3a8eb9SGleb Smirnoff 
42103b3a8eb9SGleb Smirnoff 			memset(&binat, 0, sizeof(binat));
42113b3a8eb9SGleb Smirnoff 
42123b3a8eb9SGleb Smirnoff 			if ($1 && $3.b1) {
42133b3a8eb9SGleb Smirnoff 				yyerror("\"pass\" not valid with \"no\"");
42143b3a8eb9SGleb Smirnoff 				YYERROR;
42153b3a8eb9SGleb Smirnoff 			}
42163b3a8eb9SGleb Smirnoff 			if ($1)
42173b3a8eb9SGleb Smirnoff 				binat.action = PF_NOBINAT;
42183b3a8eb9SGleb Smirnoff 			else
42193b3a8eb9SGleb Smirnoff 				binat.action = PF_BINAT;
42203b3a8eb9SGleb Smirnoff 			binat.natpass = $3.b1;
42213b3a8eb9SGleb Smirnoff 			binat.log = $3.b2;
42223b3a8eb9SGleb Smirnoff 			binat.logif = $3.w2;
42233b3a8eb9SGleb Smirnoff 			binat.af = $5;
42243b3a8eb9SGleb Smirnoff 			if (!binat.af && $8 != NULL && $8->af)
42253b3a8eb9SGleb Smirnoff 				binat.af = $8->af;
42263b3a8eb9SGleb Smirnoff 			if (!binat.af && $9 != NULL && $9->af)
42273b3a8eb9SGleb Smirnoff 				binat.af = $9->af;
42283b3a8eb9SGleb Smirnoff 
42293b3a8eb9SGleb Smirnoff 			if (!binat.af && $13 != NULL && $13->host)
42303b3a8eb9SGleb Smirnoff 				binat.af = $13->host->af;
42313b3a8eb9SGleb Smirnoff 			if (!binat.af) {
42323b3a8eb9SGleb Smirnoff 				yyerror("address family (inet/inet6) "
42333b3a8eb9SGleb Smirnoff 				    "undefined");
42343b3a8eb9SGleb Smirnoff 				YYERROR;
42353b3a8eb9SGleb Smirnoff 			}
42363b3a8eb9SGleb Smirnoff 
42373b3a8eb9SGleb Smirnoff 			if ($4 != NULL) {
42383b3a8eb9SGleb Smirnoff 				memcpy(binat.ifname, $4->ifname,
42393b3a8eb9SGleb Smirnoff 				    sizeof(binat.ifname));
42403b3a8eb9SGleb Smirnoff 				binat.ifnot = $4->not;
42413b3a8eb9SGleb Smirnoff 				free($4);
42423b3a8eb9SGleb Smirnoff 			}
42433b3a8eb9SGleb Smirnoff 
42443b3a8eb9SGleb Smirnoff 			if ($10 != NULL)
42453b3a8eb9SGleb Smirnoff 				if (strlcpy(binat.tagname, $10,
42463b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
42473b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
42483b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
42493b3a8eb9SGleb Smirnoff 					YYERROR;
42503b3a8eb9SGleb Smirnoff 				}
42513b3a8eb9SGleb Smirnoff 			if ($11.name)
42523b3a8eb9SGleb Smirnoff 				if (strlcpy(binat.match_tagname, $11.name,
42533b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
42543b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
42553b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
42563b3a8eb9SGleb Smirnoff 					YYERROR;
42573b3a8eb9SGleb Smirnoff 				}
42583b3a8eb9SGleb Smirnoff 			binat.match_tag_not = $11.neg;
42593b3a8eb9SGleb Smirnoff 			binat.rtableid = $12;
42603b3a8eb9SGleb Smirnoff 
42613b3a8eb9SGleb Smirnoff 			if ($6 != NULL) {
42623b3a8eb9SGleb Smirnoff 				binat.proto = $6->proto;
42633b3a8eb9SGleb Smirnoff 				free($6);
42643b3a8eb9SGleb Smirnoff 			}
42653b3a8eb9SGleb Smirnoff 
42663b3a8eb9SGleb Smirnoff 			if ($8 != NULL && disallow_table($8, "invalid use of "
42673b3a8eb9SGleb Smirnoff 			    "table <%s> as the source address of a binat rule"))
42683b3a8eb9SGleb Smirnoff 				YYERROR;
42693b3a8eb9SGleb Smirnoff 			if ($8 != NULL && disallow_alias($8, "invalid use of "
42703b3a8eb9SGleb Smirnoff 			    "interface (%s) as the source address of a binat "
42713b3a8eb9SGleb Smirnoff 			    "rule"))
42723b3a8eb9SGleb Smirnoff 				YYERROR;
42733b3a8eb9SGleb Smirnoff 			if ($13 != NULL && $13->host != NULL && disallow_table(
42743b3a8eb9SGleb Smirnoff 			    $13->host, "invalid use of table <%s> as the "
42753b3a8eb9SGleb Smirnoff 			    "redirect address of a binat rule"))
42763b3a8eb9SGleb Smirnoff 				YYERROR;
42773b3a8eb9SGleb Smirnoff 			if ($13 != NULL && $13->host != NULL && disallow_alias(
42783b3a8eb9SGleb Smirnoff 			    $13->host, "invalid use of interface (%s) as the "
42793b3a8eb9SGleb Smirnoff 			    "redirect address of a binat rule"))
42803b3a8eb9SGleb Smirnoff 				YYERROR;
42813b3a8eb9SGleb Smirnoff 
42823b3a8eb9SGleb Smirnoff 			if ($8 != NULL) {
42833b3a8eb9SGleb Smirnoff 				if ($8->next) {
42843b3a8eb9SGleb Smirnoff 					yyerror("multiple binat ip addresses");
42853b3a8eb9SGleb Smirnoff 					YYERROR;
42863b3a8eb9SGleb Smirnoff 				}
42873b3a8eb9SGleb Smirnoff 				if ($8->addr.type == PF_ADDR_DYNIFTL)
42883b3a8eb9SGleb Smirnoff 					$8->af = binat.af;
42893b3a8eb9SGleb Smirnoff 				if ($8->af != binat.af) {
42903b3a8eb9SGleb Smirnoff 					yyerror("binat ip versions must match");
42913b3a8eb9SGleb Smirnoff 					YYERROR;
42923b3a8eb9SGleb Smirnoff 				}
42933b3a8eb9SGleb Smirnoff 				if (check_netmask($8, binat.af))
42943b3a8eb9SGleb Smirnoff 					YYERROR;
42953b3a8eb9SGleb Smirnoff 				memcpy(&binat.src.addr, &$8->addr,
42963b3a8eb9SGleb Smirnoff 				    sizeof(binat.src.addr));
42973b3a8eb9SGleb Smirnoff 				free($8);
42983b3a8eb9SGleb Smirnoff 			}
42993b3a8eb9SGleb Smirnoff 			if ($9 != NULL) {
43003b3a8eb9SGleb Smirnoff 				if ($9->next) {
43013b3a8eb9SGleb Smirnoff 					yyerror("multiple binat ip addresses");
43023b3a8eb9SGleb Smirnoff 					YYERROR;
43033b3a8eb9SGleb Smirnoff 				}
43043b3a8eb9SGleb Smirnoff 				if ($9->af != binat.af && $9->af) {
43053b3a8eb9SGleb Smirnoff 					yyerror("binat ip versions must match");
43063b3a8eb9SGleb Smirnoff 					YYERROR;
43073b3a8eb9SGleb Smirnoff 				}
43083b3a8eb9SGleb Smirnoff 				if (check_netmask($9, binat.af))
43093b3a8eb9SGleb Smirnoff 					YYERROR;
43103b3a8eb9SGleb Smirnoff 				memcpy(&binat.dst.addr, &$9->addr,
43113b3a8eb9SGleb Smirnoff 				    sizeof(binat.dst.addr));
43123b3a8eb9SGleb Smirnoff 				binat.dst.neg = $9->not;
43133b3a8eb9SGleb Smirnoff 				free($9);
43143b3a8eb9SGleb Smirnoff 			}
43153b3a8eb9SGleb Smirnoff 
43163b3a8eb9SGleb Smirnoff 			if (binat.action == PF_NOBINAT) {
43173b3a8eb9SGleb Smirnoff 				if ($13 != NULL) {
43183b3a8eb9SGleb Smirnoff 					yyerror("'no binat' rule does not need"
43193b3a8eb9SGleb Smirnoff 					    " '->'");
43203b3a8eb9SGleb Smirnoff 					YYERROR;
43213b3a8eb9SGleb Smirnoff 				}
43223b3a8eb9SGleb Smirnoff 			} else {
43233b3a8eb9SGleb Smirnoff 				if ($13 == NULL || $13->host == NULL) {
43243b3a8eb9SGleb Smirnoff 					yyerror("'binat' rule requires"
43253b3a8eb9SGleb Smirnoff 					    " '-> address'");
43263b3a8eb9SGleb Smirnoff 					YYERROR;
43273b3a8eb9SGleb Smirnoff 				}
43283b3a8eb9SGleb Smirnoff 
43293b3a8eb9SGleb Smirnoff 				remove_invalid_hosts(&$13->host, &binat.af);
43303b3a8eb9SGleb Smirnoff 				if (invalid_redirect($13->host, binat.af))
43313b3a8eb9SGleb Smirnoff 					YYERROR;
43323b3a8eb9SGleb Smirnoff 				if ($13->host->next != NULL) {
43333b3a8eb9SGleb Smirnoff 					yyerror("binat rule must redirect to "
43343b3a8eb9SGleb Smirnoff 					    "a single address");
43353b3a8eb9SGleb Smirnoff 					YYERROR;
43363b3a8eb9SGleb Smirnoff 				}
43373b3a8eb9SGleb Smirnoff 				if (check_netmask($13->host, binat.af))
43383b3a8eb9SGleb Smirnoff 					YYERROR;
43393b3a8eb9SGleb Smirnoff 
43403b3a8eb9SGleb Smirnoff 				if (!PF_AZERO(&binat.src.addr.v.a.mask,
43413b3a8eb9SGleb Smirnoff 				    binat.af) &&
43423b3a8eb9SGleb Smirnoff 				    !PF_AEQ(&binat.src.addr.v.a.mask,
43433b3a8eb9SGleb Smirnoff 				    &$13->host->addr.v.a.mask, binat.af)) {
43443b3a8eb9SGleb Smirnoff 					yyerror("'binat' source mask and "
43453b3a8eb9SGleb Smirnoff 					    "redirect mask must be the same");
43463b3a8eb9SGleb Smirnoff 					YYERROR;
43473b3a8eb9SGleb Smirnoff 				}
43483b3a8eb9SGleb Smirnoff 
43493b3a8eb9SGleb Smirnoff 				TAILQ_INIT(&binat.rpool.list);
43503b3a8eb9SGleb Smirnoff 				pa = calloc(1, sizeof(struct pf_pooladdr));
43513b3a8eb9SGleb Smirnoff 				if (pa == NULL)
43523b3a8eb9SGleb Smirnoff 					err(1, "binat: calloc");
43533b3a8eb9SGleb Smirnoff 				pa->addr = $13->host->addr;
43543b3a8eb9SGleb Smirnoff 				pa->ifname[0] = 0;
43553b3a8eb9SGleb Smirnoff 				TAILQ_INSERT_TAIL(&binat.rpool.list,
43563b3a8eb9SGleb Smirnoff 				    pa, entries);
43573b3a8eb9SGleb Smirnoff 
43583b3a8eb9SGleb Smirnoff 				free($13);
43593b3a8eb9SGleb Smirnoff 			}
43603b3a8eb9SGleb Smirnoff 
43613b3a8eb9SGleb Smirnoff 			pfctl_add_rule(pf, &binat, "");
43623b3a8eb9SGleb Smirnoff 		}
43633b3a8eb9SGleb Smirnoff 		;
43643b3a8eb9SGleb Smirnoff 
43653b3a8eb9SGleb Smirnoff tag		: /* empty */		{ $$ = NULL; }
43663b3a8eb9SGleb Smirnoff 		| TAG STRING		{ $$ = $2; }
43673b3a8eb9SGleb Smirnoff 		;
43683b3a8eb9SGleb Smirnoff 
43693b3a8eb9SGleb Smirnoff tagged		: /* empty */		{ $$.neg = 0; $$.name = NULL; }
43703b3a8eb9SGleb Smirnoff 		| not TAGGED string	{ $$.neg = $1; $$.name = $3; }
43713b3a8eb9SGleb Smirnoff 		;
43723b3a8eb9SGleb Smirnoff 
43733b3a8eb9SGleb Smirnoff rtable		: /* empty */		{ $$ = -1; }
43743b3a8eb9SGleb Smirnoff 		| RTABLE NUMBER		{
43753b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > rt_tableid_max()) {
43763b3a8eb9SGleb Smirnoff 				yyerror("invalid rtable id");
43773b3a8eb9SGleb Smirnoff 				YYERROR;
43783b3a8eb9SGleb Smirnoff 			}
43793b3a8eb9SGleb Smirnoff 			$$ = $2;
43803b3a8eb9SGleb Smirnoff 		}
43813b3a8eb9SGleb Smirnoff 		;
43823b3a8eb9SGleb Smirnoff 
43833b3a8eb9SGleb Smirnoff route_host	: STRING			{
43843b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_host));
43853b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
43863b3a8eb9SGleb Smirnoff 				err(1, "route_host: calloc");
43873b3a8eb9SGleb Smirnoff 			$$->ifname = $1;
43883b3a8eb9SGleb Smirnoff 			set_ipmask($$, 128);
43893b3a8eb9SGleb Smirnoff 			$$->next = NULL;
43903b3a8eb9SGleb Smirnoff 			$$->tail = $$;
43913b3a8eb9SGleb Smirnoff 		}
43923b3a8eb9SGleb Smirnoff 		| '(' STRING host ')'		{
4393*58c8430aSKristof Provost 			struct node_host *n;
4394*58c8430aSKristof Provost 
43953b3a8eb9SGleb Smirnoff 			$$ = $3;
4396*58c8430aSKristof Provost 			for (n = $3; n != NULL; n = n->next)
4397*58c8430aSKristof Provost 				n->ifname = $2;
43983b3a8eb9SGleb Smirnoff 		}
43993b3a8eb9SGleb Smirnoff 		;
44003b3a8eb9SGleb Smirnoff 
44013b3a8eb9SGleb Smirnoff route_host_list	: route_host optnl			{ $$ = $1; }
44023b3a8eb9SGleb Smirnoff 		| route_host_list comma route_host optnl {
44033b3a8eb9SGleb Smirnoff 			if ($1->af == 0)
44043b3a8eb9SGleb Smirnoff 				$1->af = $3->af;
44053b3a8eb9SGleb Smirnoff 			if ($1->af != $3->af) {
44063b3a8eb9SGleb Smirnoff 				yyerror("all pool addresses must be in the "
44073b3a8eb9SGleb Smirnoff 				    "same address family");
44083b3a8eb9SGleb Smirnoff 				YYERROR;
44093b3a8eb9SGleb Smirnoff 			}
44103b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
44113b3a8eb9SGleb Smirnoff 			$1->tail = $3->tail;
44123b3a8eb9SGleb Smirnoff 			$$ = $1;
44133b3a8eb9SGleb Smirnoff 		}
44143b3a8eb9SGleb Smirnoff 		;
44153b3a8eb9SGleb Smirnoff 
44163b3a8eb9SGleb Smirnoff routespec	: route_host			{ $$ = $1; }
44173b3a8eb9SGleb Smirnoff 		| '{' optnl route_host_list '}'	{ $$ = $3; }
44183b3a8eb9SGleb Smirnoff 		;
44193b3a8eb9SGleb Smirnoff 
44203b3a8eb9SGleb Smirnoff route		: /* empty */			{
44213b3a8eb9SGleb Smirnoff 			$$.host = NULL;
44223b3a8eb9SGleb Smirnoff 			$$.rt = 0;
44233b3a8eb9SGleb Smirnoff 			$$.pool_opts = 0;
44243b3a8eb9SGleb Smirnoff 		}
44253b3a8eb9SGleb Smirnoff 		| FASTROUTE {
4426813196a1SKristof Provost 			/* backwards-compat */
44273b3a8eb9SGleb Smirnoff 			$$.host = NULL;
4428813196a1SKristof Provost 			$$.rt = 0;
44293b3a8eb9SGleb Smirnoff 			$$.pool_opts = 0;
44303b3a8eb9SGleb Smirnoff 		}
44313b3a8eb9SGleb Smirnoff 		| ROUTETO routespec pool_opts {
44323b3a8eb9SGleb Smirnoff 			$$.host = $2;
44333b3a8eb9SGleb Smirnoff 			$$.rt = PF_ROUTETO;
44343b3a8eb9SGleb Smirnoff 			$$.pool_opts = $3.type | $3.opts;
44353b3a8eb9SGleb Smirnoff 			if ($3.key != NULL)
44363b3a8eb9SGleb Smirnoff 				$$.key = $3.key;
44373b3a8eb9SGleb Smirnoff 		}
44383b3a8eb9SGleb Smirnoff 		| REPLYTO routespec pool_opts {
44393b3a8eb9SGleb Smirnoff 			$$.host = $2;
44403b3a8eb9SGleb Smirnoff 			$$.rt = PF_REPLYTO;
44413b3a8eb9SGleb Smirnoff 			$$.pool_opts = $3.type | $3.opts;
44423b3a8eb9SGleb Smirnoff 			if ($3.key != NULL)
44433b3a8eb9SGleb Smirnoff 				$$.key = $3.key;
44443b3a8eb9SGleb Smirnoff 		}
44453b3a8eb9SGleb Smirnoff 		| DUPTO routespec pool_opts {
44463b3a8eb9SGleb Smirnoff 			$$.host = $2;
44473b3a8eb9SGleb Smirnoff 			$$.rt = PF_DUPTO;
44483b3a8eb9SGleb Smirnoff 			$$.pool_opts = $3.type | $3.opts;
44493b3a8eb9SGleb Smirnoff 			if ($3.key != NULL)
44503b3a8eb9SGleb Smirnoff 				$$.key = $3.key;
44513b3a8eb9SGleb Smirnoff 		}
44523b3a8eb9SGleb Smirnoff 		;
44533b3a8eb9SGleb Smirnoff 
44543b3a8eb9SGleb Smirnoff timeout_spec	: STRING NUMBER
44553b3a8eb9SGleb Smirnoff 		{
44563b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_OPTION)) {
44573b3a8eb9SGleb Smirnoff 				free($1);
44583b3a8eb9SGleb Smirnoff 				YYERROR;
44593b3a8eb9SGleb Smirnoff 			}
44603b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > UINT_MAX) {
44613b3a8eb9SGleb Smirnoff 				yyerror("only positive values permitted");
44623b3a8eb9SGleb Smirnoff 				YYERROR;
44633b3a8eb9SGleb Smirnoff 			}
44643b3a8eb9SGleb Smirnoff 			if (pfctl_set_timeout(pf, $1, $2, 0) != 0) {
44653b3a8eb9SGleb Smirnoff 				yyerror("unknown timeout %s", $1);
44663b3a8eb9SGleb Smirnoff 				free($1);
44673b3a8eb9SGleb Smirnoff 				YYERROR;
44683b3a8eb9SGleb Smirnoff 			}
44693b3a8eb9SGleb Smirnoff 			free($1);
44703b3a8eb9SGleb Smirnoff 		}
44717f8af000SLuiz Otavio O Souza 		| INTERVAL NUMBER		{
44727f8af000SLuiz Otavio O Souza 			if (check_rulestate(PFCTL_STATE_OPTION))
44737f8af000SLuiz Otavio O Souza 				YYERROR;
44747f8af000SLuiz Otavio O Souza 			if ($2 < 0 || $2 > UINT_MAX) {
44757f8af000SLuiz Otavio O Souza 				yyerror("only positive values permitted");
44767f8af000SLuiz Otavio O Souza 				YYERROR;
44777f8af000SLuiz Otavio O Souza 			}
44787f8af000SLuiz Otavio O Souza 			if (pfctl_set_timeout(pf, "interval", $2, 0) != 0)
44797f8af000SLuiz Otavio O Souza 				YYERROR;
44807f8af000SLuiz Otavio O Souza 		}
44813b3a8eb9SGleb Smirnoff 		;
44823b3a8eb9SGleb Smirnoff 
44833b3a8eb9SGleb Smirnoff timeout_list	: timeout_list comma timeout_spec optnl
44843b3a8eb9SGleb Smirnoff 		| timeout_spec optnl
44853b3a8eb9SGleb Smirnoff 		;
44863b3a8eb9SGleb Smirnoff 
44873b3a8eb9SGleb Smirnoff limit_spec	: STRING NUMBER
44883b3a8eb9SGleb Smirnoff 		{
44893b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_OPTION)) {
44903b3a8eb9SGleb Smirnoff 				free($1);
44913b3a8eb9SGleb Smirnoff 				YYERROR;
44923b3a8eb9SGleb Smirnoff 			}
44933b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > UINT_MAX) {
44943b3a8eb9SGleb Smirnoff 				yyerror("only positive values permitted");
44953b3a8eb9SGleb Smirnoff 				YYERROR;
44963b3a8eb9SGleb Smirnoff 			}
44973b3a8eb9SGleb Smirnoff 			if (pfctl_set_limit(pf, $1, $2) != 0) {
44983b3a8eb9SGleb Smirnoff 				yyerror("unable to set limit %s %u", $1, $2);
44993b3a8eb9SGleb Smirnoff 				free($1);
45003b3a8eb9SGleb Smirnoff 				YYERROR;
45013b3a8eb9SGleb Smirnoff 			}
45023b3a8eb9SGleb Smirnoff 			free($1);
45033b3a8eb9SGleb Smirnoff 		}
45043b3a8eb9SGleb Smirnoff 		;
45053b3a8eb9SGleb Smirnoff 
45063b3a8eb9SGleb Smirnoff limit_list	: limit_list comma limit_spec optnl
45073b3a8eb9SGleb Smirnoff 		| limit_spec optnl
45083b3a8eb9SGleb Smirnoff 		;
45093b3a8eb9SGleb Smirnoff 
45103b3a8eb9SGleb Smirnoff comma		: ','
45113b3a8eb9SGleb Smirnoff 		| /* empty */
45123b3a8eb9SGleb Smirnoff 		;
45133b3a8eb9SGleb Smirnoff 
45143b3a8eb9SGleb Smirnoff yesno		: NO			{ $$ = 0; }
45153b3a8eb9SGleb Smirnoff 		| STRING		{
45163b3a8eb9SGleb Smirnoff 			if (!strcmp($1, "yes"))
45173b3a8eb9SGleb Smirnoff 				$$ = 1;
45183b3a8eb9SGleb Smirnoff 			else {
45193b3a8eb9SGleb Smirnoff 				yyerror("invalid value '%s', expected 'yes' "
45203b3a8eb9SGleb Smirnoff 				    "or 'no'", $1);
45213b3a8eb9SGleb Smirnoff 				free($1);
45223b3a8eb9SGleb Smirnoff 				YYERROR;
45233b3a8eb9SGleb Smirnoff 			}
45243b3a8eb9SGleb Smirnoff 			free($1);
45253b3a8eb9SGleb Smirnoff 		}
45263b3a8eb9SGleb Smirnoff 		;
45273b3a8eb9SGleb Smirnoff 
45283b3a8eb9SGleb Smirnoff unaryop		: '='		{ $$ = PF_OP_EQ; }
45293b3a8eb9SGleb Smirnoff 		| '!' '='	{ $$ = PF_OP_NE; }
45303b3a8eb9SGleb Smirnoff 		| '<' '='	{ $$ = PF_OP_LE; }
45313b3a8eb9SGleb Smirnoff 		| '<'		{ $$ = PF_OP_LT; }
45323b3a8eb9SGleb Smirnoff 		| '>' '='	{ $$ = PF_OP_GE; }
45333b3a8eb9SGleb Smirnoff 		| '>'		{ $$ = PF_OP_GT; }
45343b3a8eb9SGleb Smirnoff 		;
45353b3a8eb9SGleb Smirnoff 
45363b3a8eb9SGleb Smirnoff %%
45373b3a8eb9SGleb Smirnoff 
45383b3a8eb9SGleb Smirnoff int
45393b3a8eb9SGleb Smirnoff yyerror(const char *fmt, ...)
45403b3a8eb9SGleb Smirnoff {
45413b3a8eb9SGleb Smirnoff 	va_list		 ap;
45423b3a8eb9SGleb Smirnoff 
45433b3a8eb9SGleb Smirnoff 	file->errors++;
45443b3a8eb9SGleb Smirnoff 	va_start(ap, fmt);
45453b3a8eb9SGleb Smirnoff 	fprintf(stderr, "%s:%d: ", file->name, yylval.lineno);
45463b3a8eb9SGleb Smirnoff 	vfprintf(stderr, fmt, ap);
45473b3a8eb9SGleb Smirnoff 	fprintf(stderr, "\n");
45483b3a8eb9SGleb Smirnoff 	va_end(ap);
45493b3a8eb9SGleb Smirnoff 	return (0);
45503b3a8eb9SGleb Smirnoff }
45513b3a8eb9SGleb Smirnoff 
45523b3a8eb9SGleb Smirnoff int
45533b3a8eb9SGleb Smirnoff disallow_table(struct node_host *h, const char *fmt)
45543b3a8eb9SGleb Smirnoff {
45553b3a8eb9SGleb Smirnoff 	for (; h != NULL; h = h->next)
45563b3a8eb9SGleb Smirnoff 		if (h->addr.type == PF_ADDR_TABLE) {
45573b3a8eb9SGleb Smirnoff 			yyerror(fmt, h->addr.v.tblname);
45583b3a8eb9SGleb Smirnoff 			return (1);
45593b3a8eb9SGleb Smirnoff 		}
45603b3a8eb9SGleb Smirnoff 	return (0);
45613b3a8eb9SGleb Smirnoff }
45623b3a8eb9SGleb Smirnoff 
45633b3a8eb9SGleb Smirnoff int
45643b3a8eb9SGleb Smirnoff disallow_urpf_failed(struct node_host *h, const char *fmt)
45653b3a8eb9SGleb Smirnoff {
45663b3a8eb9SGleb Smirnoff 	for (; h != NULL; h = h->next)
45673b3a8eb9SGleb Smirnoff 		if (h->addr.type == PF_ADDR_URPFFAILED) {
45683b3a8eb9SGleb Smirnoff 			yyerror(fmt);
45693b3a8eb9SGleb Smirnoff 			return (1);
45703b3a8eb9SGleb Smirnoff 		}
45713b3a8eb9SGleb Smirnoff 	return (0);
45723b3a8eb9SGleb Smirnoff }
45733b3a8eb9SGleb Smirnoff 
45743b3a8eb9SGleb Smirnoff int
45753b3a8eb9SGleb Smirnoff disallow_alias(struct node_host *h, const char *fmt)
45763b3a8eb9SGleb Smirnoff {
45773b3a8eb9SGleb Smirnoff 	for (; h != NULL; h = h->next)
45783b3a8eb9SGleb Smirnoff 		if (DYNIF_MULTIADDR(h->addr)) {
45793b3a8eb9SGleb Smirnoff 			yyerror(fmt, h->addr.v.tblname);
45803b3a8eb9SGleb Smirnoff 			return (1);
45813b3a8eb9SGleb Smirnoff 		}
45823b3a8eb9SGleb Smirnoff 	return (0);
45833b3a8eb9SGleb Smirnoff }
45843b3a8eb9SGleb Smirnoff 
45853b3a8eb9SGleb Smirnoff int
45863b3a8eb9SGleb Smirnoff rule_consistent(struct pf_rule *r, int anchor_call)
45873b3a8eb9SGleb Smirnoff {
45883b3a8eb9SGleb Smirnoff 	int	problems = 0;
45893b3a8eb9SGleb Smirnoff 
45903b3a8eb9SGleb Smirnoff 	switch (r->action) {
45913b3a8eb9SGleb Smirnoff 	case PF_PASS:
45923b3a8eb9SGleb Smirnoff 	case PF_DROP:
45933b3a8eb9SGleb Smirnoff 	case PF_SCRUB:
45943b3a8eb9SGleb Smirnoff 	case PF_NOSCRUB:
45953b3a8eb9SGleb Smirnoff 		problems = filter_consistent(r, anchor_call);
45963b3a8eb9SGleb Smirnoff 		break;
45973b3a8eb9SGleb Smirnoff 	case PF_NAT:
45983b3a8eb9SGleb Smirnoff 	case PF_NONAT:
45993b3a8eb9SGleb Smirnoff 		problems = nat_consistent(r);
46003b3a8eb9SGleb Smirnoff 		break;
46013b3a8eb9SGleb Smirnoff 	case PF_RDR:
46023b3a8eb9SGleb Smirnoff 	case PF_NORDR:
46033b3a8eb9SGleb Smirnoff 		problems = rdr_consistent(r);
46043b3a8eb9SGleb Smirnoff 		break;
46053b3a8eb9SGleb Smirnoff 	case PF_BINAT:
46063b3a8eb9SGleb Smirnoff 	case PF_NOBINAT:
46073b3a8eb9SGleb Smirnoff 	default:
46083b3a8eb9SGleb Smirnoff 		break;
46093b3a8eb9SGleb Smirnoff 	}
46103b3a8eb9SGleb Smirnoff 	return (problems);
46113b3a8eb9SGleb Smirnoff }
46123b3a8eb9SGleb Smirnoff 
46133b3a8eb9SGleb Smirnoff int
46143b3a8eb9SGleb Smirnoff filter_consistent(struct pf_rule *r, int anchor_call)
46153b3a8eb9SGleb Smirnoff {
46163b3a8eb9SGleb Smirnoff 	int	problems = 0;
46173b3a8eb9SGleb Smirnoff 
46183b3a8eb9SGleb Smirnoff 	if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP &&
46193b3a8eb9SGleb Smirnoff 	    (r->src.port_op || r->dst.port_op)) {
46203b3a8eb9SGleb Smirnoff 		yyerror("port only applies to tcp/udp");
46213b3a8eb9SGleb Smirnoff 		problems++;
46223b3a8eb9SGleb Smirnoff 	}
46233b3a8eb9SGleb Smirnoff 	if (r->proto != IPPROTO_ICMP && r->proto != IPPROTO_ICMPV6 &&
46243b3a8eb9SGleb Smirnoff 	    (r->type || r->code)) {
46253b3a8eb9SGleb Smirnoff 		yyerror("icmp-type/code only applies to icmp");
46263b3a8eb9SGleb Smirnoff 		problems++;
46273b3a8eb9SGleb Smirnoff 	}
46283b3a8eb9SGleb Smirnoff 	if (!r->af && (r->type || r->code)) {
46293b3a8eb9SGleb Smirnoff 		yyerror("must indicate address family with icmp-type/code");
46303b3a8eb9SGleb Smirnoff 		problems++;
46313b3a8eb9SGleb Smirnoff 	}
46323b3a8eb9SGleb Smirnoff 	if (r->overload_tblname[0] &&
46333b3a8eb9SGleb Smirnoff 	    r->max_src_conn == 0 && r->max_src_conn_rate.seconds == 0) {
46343b3a8eb9SGleb Smirnoff 		yyerror("'overload' requires 'max-src-conn' "
46353b3a8eb9SGleb Smirnoff 		    "or 'max-src-conn-rate'");
46363b3a8eb9SGleb Smirnoff 		problems++;
46373b3a8eb9SGleb Smirnoff 	}
46383b3a8eb9SGleb Smirnoff 	if ((r->proto == IPPROTO_ICMP && r->af == AF_INET6) ||
46393b3a8eb9SGleb Smirnoff 	    (r->proto == IPPROTO_ICMPV6 && r->af == AF_INET)) {
46403b3a8eb9SGleb Smirnoff 		yyerror("proto %s doesn't match address family %s",
46413b3a8eb9SGleb Smirnoff 		    r->proto == IPPROTO_ICMP ? "icmp" : "icmp6",
46423b3a8eb9SGleb Smirnoff 		    r->af == AF_INET ? "inet" : "inet6");
46433b3a8eb9SGleb Smirnoff 		problems++;
46443b3a8eb9SGleb Smirnoff 	}
46453b3a8eb9SGleb Smirnoff 	if (r->allow_opts && r->action != PF_PASS) {
46463b3a8eb9SGleb Smirnoff 		yyerror("allow-opts can only be specified for pass rules");
46473b3a8eb9SGleb Smirnoff 		problems++;
46483b3a8eb9SGleb Smirnoff 	}
46493b3a8eb9SGleb Smirnoff 	if (r->rule_flag & PFRULE_FRAGMENT && (r->src.port_op ||
46503b3a8eb9SGleb Smirnoff 	    r->dst.port_op || r->flagset || r->type || r->code)) {
46513b3a8eb9SGleb Smirnoff 		yyerror("fragments can be filtered only on IP header fields");
46523b3a8eb9SGleb Smirnoff 		problems++;
46533b3a8eb9SGleb Smirnoff 	}
46543b3a8eb9SGleb Smirnoff 	if (r->rule_flag & PFRULE_RETURNRST && r->proto != IPPROTO_TCP) {
46553b3a8eb9SGleb Smirnoff 		yyerror("return-rst can only be applied to TCP rules");
46563b3a8eb9SGleb Smirnoff 		problems++;
46573b3a8eb9SGleb Smirnoff 	}
46583b3a8eb9SGleb Smirnoff 	if (r->max_src_nodes && !(r->rule_flag & PFRULE_RULESRCTRACK)) {
46593b3a8eb9SGleb Smirnoff 		yyerror("max-src-nodes requires 'source-track rule'");
46603b3a8eb9SGleb Smirnoff 		problems++;
46613b3a8eb9SGleb Smirnoff 	}
46623b3a8eb9SGleb Smirnoff 	if (r->action == PF_DROP && r->keep_state) {
46633b3a8eb9SGleb Smirnoff 		yyerror("keep state on block rules doesn't make sense");
46643b3a8eb9SGleb Smirnoff 		problems++;
46653b3a8eb9SGleb Smirnoff 	}
46663b3a8eb9SGleb Smirnoff 	if (r->rule_flag & PFRULE_STATESLOPPY &&
46673b3a8eb9SGleb Smirnoff 	    (r->keep_state == PF_STATE_MODULATE ||
46683b3a8eb9SGleb Smirnoff 	    r->keep_state == PF_STATE_SYNPROXY)) {
46693b3a8eb9SGleb Smirnoff 		yyerror("sloppy state matching cannot be used with "
46703b3a8eb9SGleb Smirnoff 		    "synproxy state or modulate state");
46713b3a8eb9SGleb Smirnoff 		problems++;
46723b3a8eb9SGleb Smirnoff 	}
46733b3a8eb9SGleb Smirnoff 	return (-problems);
46743b3a8eb9SGleb Smirnoff }
46753b3a8eb9SGleb Smirnoff 
46763b3a8eb9SGleb Smirnoff int
46773b3a8eb9SGleb Smirnoff nat_consistent(struct pf_rule *r)
46783b3a8eb9SGleb Smirnoff {
46793b3a8eb9SGleb Smirnoff 	return (0);	/* yeah! */
46803b3a8eb9SGleb Smirnoff }
46813b3a8eb9SGleb Smirnoff 
46823b3a8eb9SGleb Smirnoff int
46833b3a8eb9SGleb Smirnoff rdr_consistent(struct pf_rule *r)
46843b3a8eb9SGleb Smirnoff {
46853b3a8eb9SGleb Smirnoff 	int			 problems = 0;
46863b3a8eb9SGleb Smirnoff 
46873b3a8eb9SGleb Smirnoff 	if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP) {
46883b3a8eb9SGleb Smirnoff 		if (r->src.port_op) {
46893b3a8eb9SGleb Smirnoff 			yyerror("src port only applies to tcp/udp");
46903b3a8eb9SGleb Smirnoff 			problems++;
46913b3a8eb9SGleb Smirnoff 		}
46923b3a8eb9SGleb Smirnoff 		if (r->dst.port_op) {
46933b3a8eb9SGleb Smirnoff 			yyerror("dst port only applies to tcp/udp");
46943b3a8eb9SGleb Smirnoff 			problems++;
46953b3a8eb9SGleb Smirnoff 		}
46963b3a8eb9SGleb Smirnoff 		if (r->rpool.proxy_port[0]) {
46973b3a8eb9SGleb Smirnoff 			yyerror("rpool port only applies to tcp/udp");
46983b3a8eb9SGleb Smirnoff 			problems++;
46993b3a8eb9SGleb Smirnoff 		}
47003b3a8eb9SGleb Smirnoff 	}
47013b3a8eb9SGleb Smirnoff 	if (r->dst.port_op &&
47023b3a8eb9SGleb Smirnoff 	    r->dst.port_op != PF_OP_EQ && r->dst.port_op != PF_OP_RRG) {
47033b3a8eb9SGleb Smirnoff 		yyerror("invalid port operator for rdr destination port");
47043b3a8eb9SGleb Smirnoff 		problems++;
47053b3a8eb9SGleb Smirnoff 	}
47063b3a8eb9SGleb Smirnoff 	return (-problems);
47073b3a8eb9SGleb Smirnoff }
47083b3a8eb9SGleb Smirnoff 
47093b3a8eb9SGleb Smirnoff int
47103b3a8eb9SGleb Smirnoff process_tabledef(char *name, struct table_opts *opts)
47113b3a8eb9SGleb Smirnoff {
47123b3a8eb9SGleb Smirnoff 	struct pfr_buffer	 ab;
47133b3a8eb9SGleb Smirnoff 	struct node_tinit	*ti;
47143b3a8eb9SGleb Smirnoff 
47153b3a8eb9SGleb Smirnoff 	bzero(&ab, sizeof(ab));
47163b3a8eb9SGleb Smirnoff 	ab.pfrb_type = PFRB_ADDRS;
47173b3a8eb9SGleb Smirnoff 	SIMPLEQ_FOREACH(ti, &opts->init_nodes, entries) {
47183b3a8eb9SGleb Smirnoff 		if (ti->file)
47193b3a8eb9SGleb Smirnoff 			if (pfr_buf_load(&ab, ti->file, 0, append_addr)) {
47203b3a8eb9SGleb Smirnoff 				if (errno)
47213b3a8eb9SGleb Smirnoff 					yyerror("cannot load \"%s\": %s",
47223b3a8eb9SGleb Smirnoff 					    ti->file, strerror(errno));
47233b3a8eb9SGleb Smirnoff 				else
47243b3a8eb9SGleb Smirnoff 					yyerror("file \"%s\" contains bad data",
47253b3a8eb9SGleb Smirnoff 					    ti->file);
47263b3a8eb9SGleb Smirnoff 				goto _error;
47273b3a8eb9SGleb Smirnoff 			}
47283b3a8eb9SGleb Smirnoff 		if (ti->host)
47293b3a8eb9SGleb Smirnoff 			if (append_addr_host(&ab, ti->host, 0, 0)) {
47303b3a8eb9SGleb Smirnoff 				yyerror("cannot create address buffer: %s",
47313b3a8eb9SGleb Smirnoff 				    strerror(errno));
47323b3a8eb9SGleb Smirnoff 				goto _error;
47333b3a8eb9SGleb Smirnoff 			}
47343b3a8eb9SGleb Smirnoff 	}
47353b3a8eb9SGleb Smirnoff 	if (pf->opts & PF_OPT_VERBOSE)
47363b3a8eb9SGleb Smirnoff 		print_tabledef(name, opts->flags, opts->init_addr,
47373b3a8eb9SGleb Smirnoff 		    &opts->init_nodes);
47383b3a8eb9SGleb Smirnoff 	if (!(pf->opts & PF_OPT_NOACTION) &&
47393b3a8eb9SGleb Smirnoff 	    pfctl_define_table(name, opts->flags, opts->init_addr,
47403b3a8eb9SGleb Smirnoff 	    pf->anchor->name, &ab, pf->anchor->ruleset.tticket)) {
47413b3a8eb9SGleb Smirnoff 		yyerror("cannot define table %s: %s", name,
47423b3a8eb9SGleb Smirnoff 		    pfr_strerror(errno));
47433b3a8eb9SGleb Smirnoff 		goto _error;
47443b3a8eb9SGleb Smirnoff 	}
47453b3a8eb9SGleb Smirnoff 	pf->tdirty = 1;
47463b3a8eb9SGleb Smirnoff 	pfr_buf_clear(&ab);
47473b3a8eb9SGleb Smirnoff 	return (0);
47483b3a8eb9SGleb Smirnoff _error:
47493b3a8eb9SGleb Smirnoff 	pfr_buf_clear(&ab);
47503b3a8eb9SGleb Smirnoff 	return (-1);
47513b3a8eb9SGleb Smirnoff }
47523b3a8eb9SGleb Smirnoff 
47533b3a8eb9SGleb Smirnoff struct keywords {
47543b3a8eb9SGleb Smirnoff 	const char	*k_name;
47553b3a8eb9SGleb Smirnoff 	int		 k_val;
47563b3a8eb9SGleb Smirnoff };
47573b3a8eb9SGleb Smirnoff 
47583b3a8eb9SGleb Smirnoff /* macro gore, but you should've seen the prior indentation nightmare... */
47593b3a8eb9SGleb Smirnoff 
47603b3a8eb9SGleb Smirnoff #define FREE_LIST(T,r) \
47613b3a8eb9SGleb Smirnoff 	do { \
47623b3a8eb9SGleb Smirnoff 		T *p, *node = r; \
47633b3a8eb9SGleb Smirnoff 		while (node != NULL) { \
47643b3a8eb9SGleb Smirnoff 			p = node; \
47653b3a8eb9SGleb Smirnoff 			node = node->next; \
47663b3a8eb9SGleb Smirnoff 			free(p); \
47673b3a8eb9SGleb Smirnoff 		} \
47683b3a8eb9SGleb Smirnoff 	} while (0)
47693b3a8eb9SGleb Smirnoff 
47703b3a8eb9SGleb Smirnoff #define LOOP_THROUGH(T,n,r,C) \
47713b3a8eb9SGleb Smirnoff 	do { \
47723b3a8eb9SGleb Smirnoff 		T *n; \
47733b3a8eb9SGleb Smirnoff 		if (r == NULL) { \
47743b3a8eb9SGleb Smirnoff 			r = calloc(1, sizeof(T)); \
47753b3a8eb9SGleb Smirnoff 			if (r == NULL) \
47763b3a8eb9SGleb Smirnoff 				err(1, "LOOP: calloc"); \
47773b3a8eb9SGleb Smirnoff 			r->next = NULL; \
47783b3a8eb9SGleb Smirnoff 		} \
47793b3a8eb9SGleb Smirnoff 		n = r; \
47803b3a8eb9SGleb Smirnoff 		while (n != NULL) { \
47813b3a8eb9SGleb Smirnoff 			do { \
47823b3a8eb9SGleb Smirnoff 				C; \
47833b3a8eb9SGleb Smirnoff 			} while (0); \
47843b3a8eb9SGleb Smirnoff 			n = n->next; \
47853b3a8eb9SGleb Smirnoff 		} \
47863b3a8eb9SGleb Smirnoff 	} while (0)
47873b3a8eb9SGleb Smirnoff 
47883b3a8eb9SGleb Smirnoff void
47893b3a8eb9SGleb Smirnoff expand_label_str(char *label, size_t len, const char *srch, const char *repl)
47903b3a8eb9SGleb Smirnoff {
47913b3a8eb9SGleb Smirnoff 	char *tmp;
47923b3a8eb9SGleb Smirnoff 	char *p, *q;
47933b3a8eb9SGleb Smirnoff 
47943b3a8eb9SGleb Smirnoff 	if ((tmp = calloc(1, len)) == NULL)
47953b3a8eb9SGleb Smirnoff 		err(1, "expand_label_str: calloc");
47963b3a8eb9SGleb Smirnoff 	p = q = label;
47973b3a8eb9SGleb Smirnoff 	while ((q = strstr(p, srch)) != NULL) {
47983b3a8eb9SGleb Smirnoff 		*q = '\0';
47993b3a8eb9SGleb Smirnoff 		if ((strlcat(tmp, p, len) >= len) ||
48003b3a8eb9SGleb Smirnoff 		    (strlcat(tmp, repl, len) >= len))
48013b3a8eb9SGleb Smirnoff 			errx(1, "expand_label: label too long");
48023b3a8eb9SGleb Smirnoff 		q += strlen(srch);
48033b3a8eb9SGleb Smirnoff 		p = q;
48043b3a8eb9SGleb Smirnoff 	}
48053b3a8eb9SGleb Smirnoff 	if (strlcat(tmp, p, len) >= len)
48063b3a8eb9SGleb Smirnoff 		errx(1, "expand_label: label too long");
48073b3a8eb9SGleb Smirnoff 	strlcpy(label, tmp, len);	/* always fits */
48083b3a8eb9SGleb Smirnoff 	free(tmp);
48093b3a8eb9SGleb Smirnoff }
48103b3a8eb9SGleb Smirnoff 
48113b3a8eb9SGleb Smirnoff void
48123b3a8eb9SGleb Smirnoff expand_label_if(const char *name, char *label, size_t len, const char *ifname)
48133b3a8eb9SGleb Smirnoff {
48143b3a8eb9SGleb Smirnoff 	if (strstr(label, name) != NULL) {
48153b3a8eb9SGleb Smirnoff 		if (!*ifname)
48163b3a8eb9SGleb Smirnoff 			expand_label_str(label, len, name, "any");
48173b3a8eb9SGleb Smirnoff 		else
48183b3a8eb9SGleb Smirnoff 			expand_label_str(label, len, name, ifname);
48193b3a8eb9SGleb Smirnoff 	}
48203b3a8eb9SGleb Smirnoff }
48213b3a8eb9SGleb Smirnoff 
48223b3a8eb9SGleb Smirnoff void
48233b3a8eb9SGleb Smirnoff expand_label_addr(const char *name, char *label, size_t len, sa_family_t af,
48243b3a8eb9SGleb Smirnoff     struct node_host *h)
48253b3a8eb9SGleb Smirnoff {
48263b3a8eb9SGleb Smirnoff 	char tmp[64], tmp_not[66];
48273b3a8eb9SGleb Smirnoff 
48283b3a8eb9SGleb Smirnoff 	if (strstr(label, name) != NULL) {
48293b3a8eb9SGleb Smirnoff 		switch (h->addr.type) {
48303b3a8eb9SGleb Smirnoff 		case PF_ADDR_DYNIFTL:
48313b3a8eb9SGleb Smirnoff 			snprintf(tmp, sizeof(tmp), "(%s)", h->addr.v.ifname);
48323b3a8eb9SGleb Smirnoff 			break;
48333b3a8eb9SGleb Smirnoff 		case PF_ADDR_TABLE:
48343b3a8eb9SGleb Smirnoff 			snprintf(tmp, sizeof(tmp), "<%s>", h->addr.v.tblname);
48353b3a8eb9SGleb Smirnoff 			break;
48363b3a8eb9SGleb Smirnoff 		case PF_ADDR_NOROUTE:
48373b3a8eb9SGleb Smirnoff 			snprintf(tmp, sizeof(tmp), "no-route");
48383b3a8eb9SGleb Smirnoff 			break;
48393b3a8eb9SGleb Smirnoff 		case PF_ADDR_URPFFAILED:
48403b3a8eb9SGleb Smirnoff 			snprintf(tmp, sizeof(tmp), "urpf-failed");
48413b3a8eb9SGleb Smirnoff 			break;
48423b3a8eb9SGleb Smirnoff 		case PF_ADDR_ADDRMASK:
48433b3a8eb9SGleb Smirnoff 			if (!af || (PF_AZERO(&h->addr.v.a.addr, af) &&
48443b3a8eb9SGleb Smirnoff 			    PF_AZERO(&h->addr.v.a.mask, af)))
48453b3a8eb9SGleb Smirnoff 				snprintf(tmp, sizeof(tmp), "any");
48463b3a8eb9SGleb Smirnoff 			else {
48473b3a8eb9SGleb Smirnoff 				char	a[48];
48483b3a8eb9SGleb Smirnoff 				int	bits;
48493b3a8eb9SGleb Smirnoff 
48503b3a8eb9SGleb Smirnoff 				if (inet_ntop(af, &h->addr.v.a.addr, a,
48513b3a8eb9SGleb Smirnoff 				    sizeof(a)) == NULL)
48523b3a8eb9SGleb Smirnoff 					snprintf(tmp, sizeof(tmp), "?");
48533b3a8eb9SGleb Smirnoff 				else {
48543b3a8eb9SGleb Smirnoff 					bits = unmask(&h->addr.v.a.mask, af);
48553b3a8eb9SGleb Smirnoff 					if ((af == AF_INET && bits < 32) ||
48563b3a8eb9SGleb Smirnoff 					    (af == AF_INET6 && bits < 128))
48573b3a8eb9SGleb Smirnoff 						snprintf(tmp, sizeof(tmp),
48583b3a8eb9SGleb Smirnoff 						    "%s/%d", a, bits);
48593b3a8eb9SGleb Smirnoff 					else
48603b3a8eb9SGleb Smirnoff 						snprintf(tmp, sizeof(tmp),
48613b3a8eb9SGleb Smirnoff 						    "%s", a);
48623b3a8eb9SGleb Smirnoff 				}
48633b3a8eb9SGleb Smirnoff 			}
48643b3a8eb9SGleb Smirnoff 			break;
48653b3a8eb9SGleb Smirnoff 		default:
48663b3a8eb9SGleb Smirnoff 			snprintf(tmp, sizeof(tmp), "?");
48673b3a8eb9SGleb Smirnoff 			break;
48683b3a8eb9SGleb Smirnoff 		}
48693b3a8eb9SGleb Smirnoff 
48703b3a8eb9SGleb Smirnoff 		if (h->not) {
48713b3a8eb9SGleb Smirnoff 			snprintf(tmp_not, sizeof(tmp_not), "! %s", tmp);
48723b3a8eb9SGleb Smirnoff 			expand_label_str(label, len, name, tmp_not);
48733b3a8eb9SGleb Smirnoff 		} else
48743b3a8eb9SGleb Smirnoff 			expand_label_str(label, len, name, tmp);
48753b3a8eb9SGleb Smirnoff 	}
48763b3a8eb9SGleb Smirnoff }
48773b3a8eb9SGleb Smirnoff 
48783b3a8eb9SGleb Smirnoff void
48793b3a8eb9SGleb Smirnoff expand_label_port(const char *name, char *label, size_t len,
48803b3a8eb9SGleb Smirnoff     struct node_port *port)
48813b3a8eb9SGleb Smirnoff {
48823b3a8eb9SGleb Smirnoff 	char	 a1[6], a2[6], op[13] = "";
48833b3a8eb9SGleb Smirnoff 
48843b3a8eb9SGleb Smirnoff 	if (strstr(label, name) != NULL) {
48853b3a8eb9SGleb Smirnoff 		snprintf(a1, sizeof(a1), "%u", ntohs(port->port[0]));
48863b3a8eb9SGleb Smirnoff 		snprintf(a2, sizeof(a2), "%u", ntohs(port->port[1]));
48873b3a8eb9SGleb Smirnoff 		if (!port->op)
48883b3a8eb9SGleb Smirnoff 			;
48893b3a8eb9SGleb Smirnoff 		else if (port->op == PF_OP_IRG)
48903b3a8eb9SGleb Smirnoff 			snprintf(op, sizeof(op), "%s><%s", a1, a2);
48913b3a8eb9SGleb Smirnoff 		else if (port->op == PF_OP_XRG)
48923b3a8eb9SGleb Smirnoff 			snprintf(op, sizeof(op), "%s<>%s", a1, a2);
48933b3a8eb9SGleb Smirnoff 		else if (port->op == PF_OP_EQ)
48943b3a8eb9SGleb Smirnoff 			snprintf(op, sizeof(op), "%s", a1);
48953b3a8eb9SGleb Smirnoff 		else if (port->op == PF_OP_NE)
48963b3a8eb9SGleb Smirnoff 			snprintf(op, sizeof(op), "!=%s", a1);
48973b3a8eb9SGleb Smirnoff 		else if (port->op == PF_OP_LT)
48983b3a8eb9SGleb Smirnoff 			snprintf(op, sizeof(op), "<%s", a1);
48993b3a8eb9SGleb Smirnoff 		else if (port->op == PF_OP_LE)
49003b3a8eb9SGleb Smirnoff 			snprintf(op, sizeof(op), "<=%s", a1);
49013b3a8eb9SGleb Smirnoff 		else if (port->op == PF_OP_GT)
49023b3a8eb9SGleb Smirnoff 			snprintf(op, sizeof(op), ">%s", a1);
49033b3a8eb9SGleb Smirnoff 		else if (port->op == PF_OP_GE)
49043b3a8eb9SGleb Smirnoff 			snprintf(op, sizeof(op), ">=%s", a1);
49053b3a8eb9SGleb Smirnoff 		expand_label_str(label, len, name, op);
49063b3a8eb9SGleb Smirnoff 	}
49073b3a8eb9SGleb Smirnoff }
49083b3a8eb9SGleb Smirnoff 
49093b3a8eb9SGleb Smirnoff void
49103b3a8eb9SGleb Smirnoff expand_label_proto(const char *name, char *label, size_t len, u_int8_t proto)
49113b3a8eb9SGleb Smirnoff {
49123b3a8eb9SGleb Smirnoff 	struct protoent *pe;
49133b3a8eb9SGleb Smirnoff 	char n[4];
49143b3a8eb9SGleb Smirnoff 
49153b3a8eb9SGleb Smirnoff 	if (strstr(label, name) != NULL) {
49163b3a8eb9SGleb Smirnoff 		pe = getprotobynumber(proto);
49173b3a8eb9SGleb Smirnoff 		if (pe != NULL)
49183b3a8eb9SGleb Smirnoff 			expand_label_str(label, len, name, pe->p_name);
49193b3a8eb9SGleb Smirnoff 		else {
49203b3a8eb9SGleb Smirnoff 			snprintf(n, sizeof(n), "%u", proto);
49213b3a8eb9SGleb Smirnoff 			expand_label_str(label, len, name, n);
49223b3a8eb9SGleb Smirnoff 		}
49233b3a8eb9SGleb Smirnoff 	}
49243b3a8eb9SGleb Smirnoff }
49253b3a8eb9SGleb Smirnoff 
49263b3a8eb9SGleb Smirnoff void
49273b3a8eb9SGleb Smirnoff expand_label_nr(const char *name, char *label, size_t len)
49283b3a8eb9SGleb Smirnoff {
49293b3a8eb9SGleb Smirnoff 	char n[11];
49303b3a8eb9SGleb Smirnoff 
49313b3a8eb9SGleb Smirnoff 	if (strstr(label, name) != NULL) {
49323b3a8eb9SGleb Smirnoff 		snprintf(n, sizeof(n), "%u", pf->anchor->match);
49333b3a8eb9SGleb Smirnoff 		expand_label_str(label, len, name, n);
49343b3a8eb9SGleb Smirnoff 	}
49353b3a8eb9SGleb Smirnoff }
49363b3a8eb9SGleb Smirnoff 
49373b3a8eb9SGleb Smirnoff void
49383b3a8eb9SGleb Smirnoff expand_label(char *label, size_t len, const char *ifname, sa_family_t af,
49393b3a8eb9SGleb Smirnoff     struct node_host *src_host, struct node_port *src_port,
49403b3a8eb9SGleb Smirnoff     struct node_host *dst_host, struct node_port *dst_port,
49413b3a8eb9SGleb Smirnoff     u_int8_t proto)
49423b3a8eb9SGleb Smirnoff {
49433b3a8eb9SGleb Smirnoff 	expand_label_if("$if", label, len, ifname);
49443b3a8eb9SGleb Smirnoff 	expand_label_addr("$srcaddr", label, len, af, src_host);
49453b3a8eb9SGleb Smirnoff 	expand_label_addr("$dstaddr", label, len, af, dst_host);
49463b3a8eb9SGleb Smirnoff 	expand_label_port("$srcport", label, len, src_port);
49473b3a8eb9SGleb Smirnoff 	expand_label_port("$dstport", label, len, dst_port);
49483b3a8eb9SGleb Smirnoff 	expand_label_proto("$proto", label, len, proto);
49493b3a8eb9SGleb Smirnoff 	expand_label_nr("$nr", label, len);
49503b3a8eb9SGleb Smirnoff }
49513b3a8eb9SGleb Smirnoff 
49523b3a8eb9SGleb Smirnoff int
49533b3a8eb9SGleb Smirnoff expand_altq(struct pf_altq *a, struct node_if *interfaces,
49543b3a8eb9SGleb Smirnoff     struct node_queue *nqueues, struct node_queue_bw bwspec,
49553b3a8eb9SGleb Smirnoff     struct node_queue_opt *opts)
49563b3a8eb9SGleb Smirnoff {
49573b3a8eb9SGleb Smirnoff 	struct pf_altq		 pa, pb;
49583b3a8eb9SGleb Smirnoff 	char			 qname[PF_QNAME_SIZE];
49593b3a8eb9SGleb Smirnoff 	struct node_queue	*n;
49603b3a8eb9SGleb Smirnoff 	struct node_queue_bw	 bw;
49613b3a8eb9SGleb Smirnoff 	int			 errs = 0;
49623b3a8eb9SGleb Smirnoff 
49633b3a8eb9SGleb Smirnoff 	if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
49643b3a8eb9SGleb Smirnoff 		FREE_LIST(struct node_if, interfaces);
49650a70aaf8SLuiz Otavio O Souza 		if (nqueues)
49663b3a8eb9SGleb Smirnoff 			FREE_LIST(struct node_queue, nqueues);
49673b3a8eb9SGleb Smirnoff 		return (0);
49683b3a8eb9SGleb Smirnoff 	}
49693b3a8eb9SGleb Smirnoff 
49703b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_if, interface, interfaces,
49713b3a8eb9SGleb Smirnoff 		memcpy(&pa, a, sizeof(struct pf_altq));
49723b3a8eb9SGleb Smirnoff 		if (strlcpy(pa.ifname, interface->ifname,
49733b3a8eb9SGleb Smirnoff 		    sizeof(pa.ifname)) >= sizeof(pa.ifname))
49743b3a8eb9SGleb Smirnoff 			errx(1, "expand_altq: strlcpy");
49753b3a8eb9SGleb Smirnoff 
49763b3a8eb9SGleb Smirnoff 		if (interface->not) {
49773b3a8eb9SGleb Smirnoff 			yyerror("altq on ! <interface> is not supported");
49783b3a8eb9SGleb Smirnoff 			errs++;
49793b3a8eb9SGleb Smirnoff 		} else {
49803b3a8eb9SGleb Smirnoff 			if (eval_pfaltq(pf, &pa, &bwspec, opts))
49813b3a8eb9SGleb Smirnoff 				errs++;
49823b3a8eb9SGleb Smirnoff 			else
49833b3a8eb9SGleb Smirnoff 				if (pfctl_add_altq(pf, &pa))
49843b3a8eb9SGleb Smirnoff 					errs++;
49853b3a8eb9SGleb Smirnoff 
49863b3a8eb9SGleb Smirnoff 			if (pf->opts & PF_OPT_VERBOSE) {
49873b3a8eb9SGleb Smirnoff 				print_altq(&pf->paltq->altq, 0,
49883b3a8eb9SGleb Smirnoff 				    &bwspec, opts);
49893b3a8eb9SGleb Smirnoff 				if (nqueues && nqueues->tail) {
49903b3a8eb9SGleb Smirnoff 					printf("queue { ");
49913b3a8eb9SGleb Smirnoff 					LOOP_THROUGH(struct node_queue, queue,
49923b3a8eb9SGleb Smirnoff 					    nqueues,
49933b3a8eb9SGleb Smirnoff 						printf("%s ",
49943b3a8eb9SGleb Smirnoff 						    queue->queue);
49953b3a8eb9SGleb Smirnoff 					);
49963b3a8eb9SGleb Smirnoff 					printf("}");
49973b3a8eb9SGleb Smirnoff 				}
49983b3a8eb9SGleb Smirnoff 				printf("\n");
49993b3a8eb9SGleb Smirnoff 			}
50003b3a8eb9SGleb Smirnoff 
50013b3a8eb9SGleb Smirnoff 			if (pa.scheduler == ALTQT_CBQ ||
50023b3a8eb9SGleb Smirnoff 			    pa.scheduler == ALTQT_HFSC) {
50033b3a8eb9SGleb Smirnoff 				/* now create a root queue */
50043b3a8eb9SGleb Smirnoff 				memset(&pb, 0, sizeof(struct pf_altq));
50053b3a8eb9SGleb Smirnoff 				if (strlcpy(qname, "root_", sizeof(qname)) >=
50063b3a8eb9SGleb Smirnoff 				    sizeof(qname))
50073b3a8eb9SGleb Smirnoff 					errx(1, "expand_altq: strlcpy");
50083b3a8eb9SGleb Smirnoff 				if (strlcat(qname, interface->ifname,
50093b3a8eb9SGleb Smirnoff 				    sizeof(qname)) >= sizeof(qname))
50103b3a8eb9SGleb Smirnoff 					errx(1, "expand_altq: strlcat");
50113b3a8eb9SGleb Smirnoff 				if (strlcpy(pb.qname, qname,
50123b3a8eb9SGleb Smirnoff 				    sizeof(pb.qname)) >= sizeof(pb.qname))
50133b3a8eb9SGleb Smirnoff 					errx(1, "expand_altq: strlcpy");
50143b3a8eb9SGleb Smirnoff 				if (strlcpy(pb.ifname, interface->ifname,
50153b3a8eb9SGleb Smirnoff 				    sizeof(pb.ifname)) >= sizeof(pb.ifname))
50163b3a8eb9SGleb Smirnoff 					errx(1, "expand_altq: strlcpy");
50173b3a8eb9SGleb Smirnoff 				pb.qlimit = pa.qlimit;
50183b3a8eb9SGleb Smirnoff 				pb.scheduler = pa.scheduler;
50193b3a8eb9SGleb Smirnoff 				bw.bw_absolute = pa.ifbandwidth;
50203b3a8eb9SGleb Smirnoff 				bw.bw_percent = 0;
50213b3a8eb9SGleb Smirnoff 				if (eval_pfqueue(pf, &pb, &bw, opts))
50223b3a8eb9SGleb Smirnoff 					errs++;
50233b3a8eb9SGleb Smirnoff 				else
50243b3a8eb9SGleb Smirnoff 					if (pfctl_add_altq(pf, &pb))
50253b3a8eb9SGleb Smirnoff 						errs++;
50263b3a8eb9SGleb Smirnoff 			}
50273b3a8eb9SGleb Smirnoff 
50283b3a8eb9SGleb Smirnoff 			LOOP_THROUGH(struct node_queue, queue, nqueues,
50293b3a8eb9SGleb Smirnoff 				n = calloc(1, sizeof(struct node_queue));
50303b3a8eb9SGleb Smirnoff 				if (n == NULL)
50313b3a8eb9SGleb Smirnoff 					err(1, "expand_altq: calloc");
50323b3a8eb9SGleb Smirnoff 				if (pa.scheduler == ALTQT_CBQ ||
50333b3a8eb9SGleb Smirnoff 				    pa.scheduler == ALTQT_HFSC)
50343b3a8eb9SGleb Smirnoff 					if (strlcpy(n->parent, qname,
50353b3a8eb9SGleb Smirnoff 					    sizeof(n->parent)) >=
50363b3a8eb9SGleb Smirnoff 					    sizeof(n->parent))
50373b3a8eb9SGleb Smirnoff 						errx(1, "expand_altq: strlcpy");
50383b3a8eb9SGleb Smirnoff 				if (strlcpy(n->queue, queue->queue,
50393b3a8eb9SGleb Smirnoff 				    sizeof(n->queue)) >= sizeof(n->queue))
50403b3a8eb9SGleb Smirnoff 					errx(1, "expand_altq: strlcpy");
50413b3a8eb9SGleb Smirnoff 				if (strlcpy(n->ifname, interface->ifname,
50423b3a8eb9SGleb Smirnoff 				    sizeof(n->ifname)) >= sizeof(n->ifname))
50433b3a8eb9SGleb Smirnoff 					errx(1, "expand_altq: strlcpy");
50443b3a8eb9SGleb Smirnoff 				n->scheduler = pa.scheduler;
50453b3a8eb9SGleb Smirnoff 				n->next = NULL;
50463b3a8eb9SGleb Smirnoff 				n->tail = n;
50473b3a8eb9SGleb Smirnoff 				if (queues == NULL)
50483b3a8eb9SGleb Smirnoff 					queues = n;
50493b3a8eb9SGleb Smirnoff 				else {
50503b3a8eb9SGleb Smirnoff 					queues->tail->next = n;
50513b3a8eb9SGleb Smirnoff 					queues->tail = n;
50523b3a8eb9SGleb Smirnoff 				}
50533b3a8eb9SGleb Smirnoff 			);
50543b3a8eb9SGleb Smirnoff 		}
50553b3a8eb9SGleb Smirnoff 	);
50563b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_if, interfaces);
50570a70aaf8SLuiz Otavio O Souza 	if (nqueues)
50583b3a8eb9SGleb Smirnoff 		FREE_LIST(struct node_queue, nqueues);
50593b3a8eb9SGleb Smirnoff 
50603b3a8eb9SGleb Smirnoff 	return (errs);
50613b3a8eb9SGleb Smirnoff }
50623b3a8eb9SGleb Smirnoff 
50633b3a8eb9SGleb Smirnoff int
50643b3a8eb9SGleb Smirnoff expand_queue(struct pf_altq *a, struct node_if *interfaces,
50653b3a8eb9SGleb Smirnoff     struct node_queue *nqueues, struct node_queue_bw bwspec,
50663b3a8eb9SGleb Smirnoff     struct node_queue_opt *opts)
50673b3a8eb9SGleb Smirnoff {
50683b3a8eb9SGleb Smirnoff 	struct node_queue	*n, *nq;
50693b3a8eb9SGleb Smirnoff 	struct pf_altq		 pa;
50703b3a8eb9SGleb Smirnoff 	u_int8_t		 found = 0;
50713b3a8eb9SGleb Smirnoff 	u_int8_t		 errs = 0;
50723b3a8eb9SGleb Smirnoff 
50733b3a8eb9SGleb Smirnoff 	if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
50743b3a8eb9SGleb Smirnoff 		FREE_LIST(struct node_queue, nqueues);
50753b3a8eb9SGleb Smirnoff 		return (0);
50763b3a8eb9SGleb Smirnoff 	}
50773b3a8eb9SGleb Smirnoff 
50783b3a8eb9SGleb Smirnoff 	if (queues == NULL) {
50793b3a8eb9SGleb Smirnoff 		yyerror("queue %s has no parent", a->qname);
50803b3a8eb9SGleb Smirnoff 		FREE_LIST(struct node_queue, nqueues);
50813b3a8eb9SGleb Smirnoff 		return (1);
50823b3a8eb9SGleb Smirnoff 	}
50833b3a8eb9SGleb Smirnoff 
50843b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_if, interface, interfaces,
50853b3a8eb9SGleb Smirnoff 		LOOP_THROUGH(struct node_queue, tqueue, queues,
50863b3a8eb9SGleb Smirnoff 			if (!strncmp(a->qname, tqueue->queue, PF_QNAME_SIZE) &&
50873b3a8eb9SGleb Smirnoff 			    (interface->ifname[0] == 0 ||
50883b3a8eb9SGleb Smirnoff 			    (!interface->not && !strncmp(interface->ifname,
50893b3a8eb9SGleb Smirnoff 			    tqueue->ifname, IFNAMSIZ)) ||
50903b3a8eb9SGleb Smirnoff 			    (interface->not && strncmp(interface->ifname,
50913b3a8eb9SGleb Smirnoff 			    tqueue->ifname, IFNAMSIZ)))) {
50923b3a8eb9SGleb Smirnoff 				/* found ourself in queues */
50933b3a8eb9SGleb Smirnoff 				found++;
50943b3a8eb9SGleb Smirnoff 
50953b3a8eb9SGleb Smirnoff 				memcpy(&pa, a, sizeof(struct pf_altq));
50963b3a8eb9SGleb Smirnoff 
50973b3a8eb9SGleb Smirnoff 				if (pa.scheduler != ALTQT_NONE &&
50983b3a8eb9SGleb Smirnoff 				    pa.scheduler != tqueue->scheduler) {
50993b3a8eb9SGleb Smirnoff 					yyerror("exactly one scheduler type "
51003b3a8eb9SGleb Smirnoff 					    "per interface allowed");
51013b3a8eb9SGleb Smirnoff 					return (1);
51023b3a8eb9SGleb Smirnoff 				}
51033b3a8eb9SGleb Smirnoff 				pa.scheduler = tqueue->scheduler;
51043b3a8eb9SGleb Smirnoff 
51053b3a8eb9SGleb Smirnoff 				/* scheduler dependent error checking */
51063b3a8eb9SGleb Smirnoff 				switch (pa.scheduler) {
51073b3a8eb9SGleb Smirnoff 				case ALTQT_PRIQ:
51083b3a8eb9SGleb Smirnoff 					if (nqueues != NULL) {
51093b3a8eb9SGleb Smirnoff 						yyerror("priq queues cannot "
51103b3a8eb9SGleb Smirnoff 						    "have child queues");
51113b3a8eb9SGleb Smirnoff 						return (1);
51123b3a8eb9SGleb Smirnoff 					}
51133b3a8eb9SGleb Smirnoff 					if (bwspec.bw_absolute > 0 ||
51143b3a8eb9SGleb Smirnoff 					    bwspec.bw_percent < 100) {
51153b3a8eb9SGleb Smirnoff 						yyerror("priq doesn't take "
51163b3a8eb9SGleb Smirnoff 						    "bandwidth");
51173b3a8eb9SGleb Smirnoff 						return (1);
51183b3a8eb9SGleb Smirnoff 					}
51193b3a8eb9SGleb Smirnoff 					break;
51203b3a8eb9SGleb Smirnoff 				default:
51213b3a8eb9SGleb Smirnoff 					break;
51223b3a8eb9SGleb Smirnoff 				}
51233b3a8eb9SGleb Smirnoff 
51243b3a8eb9SGleb Smirnoff 				if (strlcpy(pa.ifname, tqueue->ifname,
51253b3a8eb9SGleb Smirnoff 				    sizeof(pa.ifname)) >= sizeof(pa.ifname))
51263b3a8eb9SGleb Smirnoff 					errx(1, "expand_queue: strlcpy");
51273b3a8eb9SGleb Smirnoff 				if (strlcpy(pa.parent, tqueue->parent,
51283b3a8eb9SGleb Smirnoff 				    sizeof(pa.parent)) >= sizeof(pa.parent))
51293b3a8eb9SGleb Smirnoff 					errx(1, "expand_queue: strlcpy");
51303b3a8eb9SGleb Smirnoff 
51313b3a8eb9SGleb Smirnoff 				if (eval_pfqueue(pf, &pa, &bwspec, opts))
51323b3a8eb9SGleb Smirnoff 					errs++;
51333b3a8eb9SGleb Smirnoff 				else
51343b3a8eb9SGleb Smirnoff 					if (pfctl_add_altq(pf, &pa))
51353b3a8eb9SGleb Smirnoff 						errs++;
51363b3a8eb9SGleb Smirnoff 
51373b3a8eb9SGleb Smirnoff 				for (nq = nqueues; nq != NULL; nq = nq->next) {
51383b3a8eb9SGleb Smirnoff 					if (!strcmp(a->qname, nq->queue)) {
51393b3a8eb9SGleb Smirnoff 						yyerror("queue cannot have "
51403b3a8eb9SGleb Smirnoff 						    "itself as child");
51413b3a8eb9SGleb Smirnoff 						errs++;
51423b3a8eb9SGleb Smirnoff 						continue;
51433b3a8eb9SGleb Smirnoff 					}
51443b3a8eb9SGleb Smirnoff 					n = calloc(1,
51453b3a8eb9SGleb Smirnoff 					    sizeof(struct node_queue));
51463b3a8eb9SGleb Smirnoff 					if (n == NULL)
51473b3a8eb9SGleb Smirnoff 						err(1, "expand_queue: calloc");
51483b3a8eb9SGleb Smirnoff 					if (strlcpy(n->parent, a->qname,
51493b3a8eb9SGleb Smirnoff 					    sizeof(n->parent)) >=
51503b3a8eb9SGleb Smirnoff 					    sizeof(n->parent))
51513b3a8eb9SGleb Smirnoff 						errx(1, "expand_queue strlcpy");
51523b3a8eb9SGleb Smirnoff 					if (strlcpy(n->queue, nq->queue,
51533b3a8eb9SGleb Smirnoff 					    sizeof(n->queue)) >=
51543b3a8eb9SGleb Smirnoff 					    sizeof(n->queue))
51553b3a8eb9SGleb Smirnoff 						errx(1, "expand_queue strlcpy");
51563b3a8eb9SGleb Smirnoff 					if (strlcpy(n->ifname, tqueue->ifname,
51573b3a8eb9SGleb Smirnoff 					    sizeof(n->ifname)) >=
51583b3a8eb9SGleb Smirnoff 					    sizeof(n->ifname))
51593b3a8eb9SGleb Smirnoff 						errx(1, "expand_queue strlcpy");
51603b3a8eb9SGleb Smirnoff 					n->scheduler = tqueue->scheduler;
51613b3a8eb9SGleb Smirnoff 					n->next = NULL;
51623b3a8eb9SGleb Smirnoff 					n->tail = n;
51633b3a8eb9SGleb Smirnoff 					if (queues == NULL)
51643b3a8eb9SGleb Smirnoff 						queues = n;
51653b3a8eb9SGleb Smirnoff 					else {
51663b3a8eb9SGleb Smirnoff 						queues->tail->next = n;
51673b3a8eb9SGleb Smirnoff 						queues->tail = n;
51683b3a8eb9SGleb Smirnoff 					}
51693b3a8eb9SGleb Smirnoff 				}
51703b3a8eb9SGleb Smirnoff 				if ((pf->opts & PF_OPT_VERBOSE) && (
51713b3a8eb9SGleb Smirnoff 				    (found == 1 && interface->ifname[0] == 0) ||
51723b3a8eb9SGleb Smirnoff 				    (found > 0 && interface->ifname[0] != 0))) {
51733b3a8eb9SGleb Smirnoff 					print_queue(&pf->paltq->altq, 0,
51743b3a8eb9SGleb Smirnoff 					    &bwspec, interface->ifname[0] != 0,
51753b3a8eb9SGleb Smirnoff 					    opts);
51763b3a8eb9SGleb Smirnoff 					if (nqueues && nqueues->tail) {
51773b3a8eb9SGleb Smirnoff 						printf("{ ");
51783b3a8eb9SGleb Smirnoff 						LOOP_THROUGH(struct node_queue,
51793b3a8eb9SGleb Smirnoff 						    queue, nqueues,
51803b3a8eb9SGleb Smirnoff 							printf("%s ",
51813b3a8eb9SGleb Smirnoff 							    queue->queue);
51823b3a8eb9SGleb Smirnoff 						);
51833b3a8eb9SGleb Smirnoff 						printf("}");
51843b3a8eb9SGleb Smirnoff 					}
51853b3a8eb9SGleb Smirnoff 					printf("\n");
51863b3a8eb9SGleb Smirnoff 				}
51873b3a8eb9SGleb Smirnoff 			}
51883b3a8eb9SGleb Smirnoff 		);
51893b3a8eb9SGleb Smirnoff 	);
51903b3a8eb9SGleb Smirnoff 
51913b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_queue, nqueues);
51923b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_if, interfaces);
51933b3a8eb9SGleb Smirnoff 
51943b3a8eb9SGleb Smirnoff 	if (!found) {
51953b3a8eb9SGleb Smirnoff 		yyerror("queue %s has no parent", a->qname);
51963b3a8eb9SGleb Smirnoff 		errs++;
51973b3a8eb9SGleb Smirnoff 	}
51983b3a8eb9SGleb Smirnoff 
51993b3a8eb9SGleb Smirnoff 	if (errs)
52003b3a8eb9SGleb Smirnoff 		return (1);
52013b3a8eb9SGleb Smirnoff 	else
52023b3a8eb9SGleb Smirnoff 		return (0);
52033b3a8eb9SGleb Smirnoff }
52043b3a8eb9SGleb Smirnoff 
52053b3a8eb9SGleb Smirnoff void
52063b3a8eb9SGleb Smirnoff expand_rule(struct pf_rule *r,
52073b3a8eb9SGleb Smirnoff     struct node_if *interfaces, struct node_host *rpool_hosts,
52083b3a8eb9SGleb Smirnoff     struct node_proto *protos, struct node_os *src_oses,
52093b3a8eb9SGleb Smirnoff     struct node_host *src_hosts, struct node_port *src_ports,
52103b3a8eb9SGleb Smirnoff     struct node_host *dst_hosts, struct node_port *dst_ports,
52113b3a8eb9SGleb Smirnoff     struct node_uid *uids, struct node_gid *gids, struct node_icmp *icmp_types,
52123b3a8eb9SGleb Smirnoff     const char *anchor_call)
52133b3a8eb9SGleb Smirnoff {
52143b3a8eb9SGleb Smirnoff 	sa_family_t		 af = r->af;
52153b3a8eb9SGleb Smirnoff 	int			 added = 0, error = 0;
52163b3a8eb9SGleb Smirnoff 	char			 ifname[IF_NAMESIZE];
52173b3a8eb9SGleb Smirnoff 	char			 label[PF_RULE_LABEL_SIZE];
52183b3a8eb9SGleb Smirnoff 	char			 tagname[PF_TAG_NAME_SIZE];
52193b3a8eb9SGleb Smirnoff 	char			 match_tagname[PF_TAG_NAME_SIZE];
52203b3a8eb9SGleb Smirnoff 	struct pf_pooladdr	*pa;
52213b3a8eb9SGleb Smirnoff 	struct node_host	*h;
52223b3a8eb9SGleb Smirnoff 	u_int8_t		 flags, flagset, keep_state;
52233b3a8eb9SGleb Smirnoff 
52243b3a8eb9SGleb Smirnoff 	if (strlcpy(label, r->label, sizeof(label)) >= sizeof(label))
52253b3a8eb9SGleb Smirnoff 		errx(1, "expand_rule: strlcpy");
52263b3a8eb9SGleb Smirnoff 	if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname))
52273b3a8eb9SGleb Smirnoff 		errx(1, "expand_rule: strlcpy");
52283b3a8eb9SGleb Smirnoff 	if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >=
52293b3a8eb9SGleb Smirnoff 	    sizeof(match_tagname))
52303b3a8eb9SGleb Smirnoff 		errx(1, "expand_rule: strlcpy");
52313b3a8eb9SGleb Smirnoff 	flags = r->flags;
52323b3a8eb9SGleb Smirnoff 	flagset = r->flagset;
52333b3a8eb9SGleb Smirnoff 	keep_state = r->keep_state;
52343b3a8eb9SGleb Smirnoff 
52353b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_if, interface, interfaces,
52363b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_proto, proto, protos,
52373b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_icmp, icmp_type, icmp_types,
52383b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_host, src_host, src_hosts,
52393b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_port, src_port, src_ports,
52403b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_os, src_os, src_oses,
52413b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_host, dst_host, dst_hosts,
52423b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_port, dst_port, dst_ports,
52433b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_uid, uid, uids,
52443b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_gid, gid, gids,
52453b3a8eb9SGleb Smirnoff 
52463b3a8eb9SGleb Smirnoff 		r->af = af;
52473b3a8eb9SGleb Smirnoff 		/* for link-local IPv6 address, interface must match up */
52483b3a8eb9SGleb Smirnoff 		if ((r->af && src_host->af && r->af != src_host->af) ||
52493b3a8eb9SGleb Smirnoff 		    (r->af && dst_host->af && r->af != dst_host->af) ||
52503b3a8eb9SGleb Smirnoff 		    (src_host->af && dst_host->af &&
52513b3a8eb9SGleb Smirnoff 		    src_host->af != dst_host->af) ||
52523b3a8eb9SGleb Smirnoff 		    (src_host->ifindex && dst_host->ifindex &&
52533b3a8eb9SGleb Smirnoff 		    src_host->ifindex != dst_host->ifindex) ||
52543b3a8eb9SGleb Smirnoff 		    (src_host->ifindex && *interface->ifname &&
52553b3a8eb9SGleb Smirnoff 		    src_host->ifindex != if_nametoindex(interface->ifname)) ||
52563b3a8eb9SGleb Smirnoff 		    (dst_host->ifindex && *interface->ifname &&
52573b3a8eb9SGleb Smirnoff 		    dst_host->ifindex != if_nametoindex(interface->ifname)))
52583b3a8eb9SGleb Smirnoff 			continue;
52593b3a8eb9SGleb Smirnoff 		if (!r->af && src_host->af)
52603b3a8eb9SGleb Smirnoff 			r->af = src_host->af;
52613b3a8eb9SGleb Smirnoff 		else if (!r->af && dst_host->af)
52623b3a8eb9SGleb Smirnoff 			r->af = dst_host->af;
52633b3a8eb9SGleb Smirnoff 
52643b3a8eb9SGleb Smirnoff 		if (*interface->ifname)
52653b3a8eb9SGleb Smirnoff 			strlcpy(r->ifname, interface->ifname,
52663b3a8eb9SGleb Smirnoff 			    sizeof(r->ifname));
52673b3a8eb9SGleb Smirnoff 		else if (if_indextoname(src_host->ifindex, ifname))
52683b3a8eb9SGleb Smirnoff 			strlcpy(r->ifname, ifname, sizeof(r->ifname));
52693b3a8eb9SGleb Smirnoff 		else if (if_indextoname(dst_host->ifindex, ifname))
52703b3a8eb9SGleb Smirnoff 			strlcpy(r->ifname, ifname, sizeof(r->ifname));
52713b3a8eb9SGleb Smirnoff 		else
52723b3a8eb9SGleb Smirnoff 			memset(r->ifname, '\0', sizeof(r->ifname));
52733b3a8eb9SGleb Smirnoff 
52743b3a8eb9SGleb Smirnoff 		if (strlcpy(r->label, label, sizeof(r->label)) >=
52753b3a8eb9SGleb Smirnoff 		    sizeof(r->label))
52763b3a8eb9SGleb Smirnoff 			errx(1, "expand_rule: strlcpy");
52773b3a8eb9SGleb Smirnoff 		if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >=
52783b3a8eb9SGleb Smirnoff 		    sizeof(r->tagname))
52793b3a8eb9SGleb Smirnoff 			errx(1, "expand_rule: strlcpy");
52803b3a8eb9SGleb Smirnoff 		if (strlcpy(r->match_tagname, match_tagname,
52813b3a8eb9SGleb Smirnoff 		    sizeof(r->match_tagname)) >= sizeof(r->match_tagname))
52823b3a8eb9SGleb Smirnoff 			errx(1, "expand_rule: strlcpy");
52833b3a8eb9SGleb Smirnoff 		expand_label(r->label, PF_RULE_LABEL_SIZE, r->ifname, r->af,
52843b3a8eb9SGleb Smirnoff 		    src_host, src_port, dst_host, dst_port, proto->proto);
52853b3a8eb9SGleb Smirnoff 		expand_label(r->tagname, PF_TAG_NAME_SIZE, r->ifname, r->af,
52863b3a8eb9SGleb Smirnoff 		    src_host, src_port, dst_host, dst_port, proto->proto);
52873b3a8eb9SGleb Smirnoff 		expand_label(r->match_tagname, PF_TAG_NAME_SIZE, r->ifname,
52883b3a8eb9SGleb Smirnoff 		    r->af, src_host, src_port, dst_host, dst_port,
52893b3a8eb9SGleb Smirnoff 		    proto->proto);
52903b3a8eb9SGleb Smirnoff 
52913b3a8eb9SGleb Smirnoff 		error += check_netmask(src_host, r->af);
52923b3a8eb9SGleb Smirnoff 		error += check_netmask(dst_host, r->af);
52933b3a8eb9SGleb Smirnoff 
52943b3a8eb9SGleb Smirnoff 		r->ifnot = interface->not;
52953b3a8eb9SGleb Smirnoff 		r->proto = proto->proto;
52963b3a8eb9SGleb Smirnoff 		r->src.addr = src_host->addr;
52973b3a8eb9SGleb Smirnoff 		r->src.neg = src_host->not;
52983b3a8eb9SGleb Smirnoff 		r->src.port[0] = src_port->port[0];
52993b3a8eb9SGleb Smirnoff 		r->src.port[1] = src_port->port[1];
53003b3a8eb9SGleb Smirnoff 		r->src.port_op = src_port->op;
53013b3a8eb9SGleb Smirnoff 		r->dst.addr = dst_host->addr;
53023b3a8eb9SGleb Smirnoff 		r->dst.neg = dst_host->not;
53033b3a8eb9SGleb Smirnoff 		r->dst.port[0] = dst_port->port[0];
53043b3a8eb9SGleb Smirnoff 		r->dst.port[1] = dst_port->port[1];
53053b3a8eb9SGleb Smirnoff 		r->dst.port_op = dst_port->op;
53063b3a8eb9SGleb Smirnoff 		r->uid.op = uid->op;
53073b3a8eb9SGleb Smirnoff 		r->uid.uid[0] = uid->uid[0];
53083b3a8eb9SGleb Smirnoff 		r->uid.uid[1] = uid->uid[1];
53093b3a8eb9SGleb Smirnoff 		r->gid.op = gid->op;
53103b3a8eb9SGleb Smirnoff 		r->gid.gid[0] = gid->gid[0];
53113b3a8eb9SGleb Smirnoff 		r->gid.gid[1] = gid->gid[1];
53123b3a8eb9SGleb Smirnoff 		r->type = icmp_type->type;
53133b3a8eb9SGleb Smirnoff 		r->code = icmp_type->code;
53143b3a8eb9SGleb Smirnoff 
53153b3a8eb9SGleb Smirnoff 		if ((keep_state == PF_STATE_MODULATE ||
53163b3a8eb9SGleb Smirnoff 		    keep_state == PF_STATE_SYNPROXY) &&
53173b3a8eb9SGleb Smirnoff 		    r->proto && r->proto != IPPROTO_TCP)
53183b3a8eb9SGleb Smirnoff 			r->keep_state = PF_STATE_NORMAL;
53193b3a8eb9SGleb Smirnoff 		else
53203b3a8eb9SGleb Smirnoff 			r->keep_state = keep_state;
53213b3a8eb9SGleb Smirnoff 
53223b3a8eb9SGleb Smirnoff 		if (r->proto && r->proto != IPPROTO_TCP) {
53233b3a8eb9SGleb Smirnoff 			r->flags = 0;
53243b3a8eb9SGleb Smirnoff 			r->flagset = 0;
53253b3a8eb9SGleb Smirnoff 		} else {
53263b3a8eb9SGleb Smirnoff 			r->flags = flags;
53273b3a8eb9SGleb Smirnoff 			r->flagset = flagset;
53283b3a8eb9SGleb Smirnoff 		}
53293b3a8eb9SGleb Smirnoff 		if (icmp_type->proto && r->proto != icmp_type->proto) {
53303b3a8eb9SGleb Smirnoff 			yyerror("icmp-type mismatch");
53313b3a8eb9SGleb Smirnoff 			error++;
53323b3a8eb9SGleb Smirnoff 		}
53333b3a8eb9SGleb Smirnoff 
53343b3a8eb9SGleb Smirnoff 		if (src_os && src_os->os) {
53353b3a8eb9SGleb Smirnoff 			r->os_fingerprint = pfctl_get_fingerprint(src_os->os);
53363b3a8eb9SGleb Smirnoff 			if ((pf->opts & PF_OPT_VERBOSE2) &&
53373b3a8eb9SGleb Smirnoff 			    r->os_fingerprint == PF_OSFP_NOMATCH)
53383b3a8eb9SGleb Smirnoff 				fprintf(stderr,
53393b3a8eb9SGleb Smirnoff 				    "warning: unknown '%s' OS fingerprint\n",
53403b3a8eb9SGleb Smirnoff 				    src_os->os);
53413b3a8eb9SGleb Smirnoff 		} else {
53423b3a8eb9SGleb Smirnoff 			r->os_fingerprint = PF_OSFP_ANY;
53433b3a8eb9SGleb Smirnoff 		}
53443b3a8eb9SGleb Smirnoff 
53453b3a8eb9SGleb Smirnoff 		TAILQ_INIT(&r->rpool.list);
53463b3a8eb9SGleb Smirnoff 		for (h = rpool_hosts; h != NULL; h = h->next) {
53473b3a8eb9SGleb Smirnoff 			pa = calloc(1, sizeof(struct pf_pooladdr));
53483b3a8eb9SGleb Smirnoff 			if (pa == NULL)
53493b3a8eb9SGleb Smirnoff 				err(1, "expand_rule: calloc");
53503b3a8eb9SGleb Smirnoff 			pa->addr = h->addr;
53513b3a8eb9SGleb Smirnoff 			if (h->ifname != NULL) {
53523b3a8eb9SGleb Smirnoff 				if (strlcpy(pa->ifname, h->ifname,
53533b3a8eb9SGleb Smirnoff 				    sizeof(pa->ifname)) >=
53543b3a8eb9SGleb Smirnoff 				    sizeof(pa->ifname))
53553b3a8eb9SGleb Smirnoff 					errx(1, "expand_rule: strlcpy");
53563b3a8eb9SGleb Smirnoff 			} else
53573b3a8eb9SGleb Smirnoff 				pa->ifname[0] = 0;
53583b3a8eb9SGleb Smirnoff 			TAILQ_INSERT_TAIL(&r->rpool.list, pa, entries);
53593b3a8eb9SGleb Smirnoff 		}
53603b3a8eb9SGleb Smirnoff 
53613b3a8eb9SGleb Smirnoff 		if (rule_consistent(r, anchor_call[0]) < 0 || error)
53623b3a8eb9SGleb Smirnoff 			yyerror("skipping rule due to errors");
53633b3a8eb9SGleb Smirnoff 		else {
53643b3a8eb9SGleb Smirnoff 			r->nr = pf->astack[pf->asd]->match++;
53653b3a8eb9SGleb Smirnoff 			pfctl_add_rule(pf, r, anchor_call);
53663b3a8eb9SGleb Smirnoff 			added++;
53673b3a8eb9SGleb Smirnoff 		}
53683b3a8eb9SGleb Smirnoff 
53693b3a8eb9SGleb Smirnoff 	))))))))));
53703b3a8eb9SGleb Smirnoff 
53713b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_if, interfaces);
53723b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_proto, protos);
53733b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_host, src_hosts);
53743b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_port, src_ports);
53753b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_os, src_oses);
53763b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_host, dst_hosts);
53773b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_port, dst_ports);
53783b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_uid, uids);
53793b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_gid, gids);
53803b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_icmp, icmp_types);
53813b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_host, rpool_hosts);
53823b3a8eb9SGleb Smirnoff 
53833b3a8eb9SGleb Smirnoff 	if (!added)
53843b3a8eb9SGleb Smirnoff 		yyerror("rule expands to no valid combination");
53853b3a8eb9SGleb Smirnoff }
53863b3a8eb9SGleb Smirnoff 
53873b3a8eb9SGleb Smirnoff int
53883b3a8eb9SGleb Smirnoff expand_skip_interface(struct node_if *interfaces)
53893b3a8eb9SGleb Smirnoff {
53903b3a8eb9SGleb Smirnoff 	int	errs = 0;
53913b3a8eb9SGleb Smirnoff 
53923b3a8eb9SGleb Smirnoff 	if (!interfaces || (!interfaces->next && !interfaces->not &&
53933b3a8eb9SGleb Smirnoff 	    !strcmp(interfaces->ifname, "none"))) {
53943b3a8eb9SGleb Smirnoff 		if (pf->opts & PF_OPT_VERBOSE)
53953b3a8eb9SGleb Smirnoff 			printf("set skip on none\n");
53963b3a8eb9SGleb Smirnoff 		errs = pfctl_set_interface_flags(pf, "", PFI_IFLAG_SKIP, 0);
53973b3a8eb9SGleb Smirnoff 		return (errs);
53983b3a8eb9SGleb Smirnoff 	}
53993b3a8eb9SGleb Smirnoff 
54003b3a8eb9SGleb Smirnoff 	if (pf->opts & PF_OPT_VERBOSE)
54013b3a8eb9SGleb Smirnoff 		printf("set skip on {");
54023b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_if, interface, interfaces,
54033b3a8eb9SGleb Smirnoff 		if (pf->opts & PF_OPT_VERBOSE)
54043b3a8eb9SGleb Smirnoff 			printf(" %s", interface->ifname);
54053b3a8eb9SGleb Smirnoff 		if (interface->not) {
54063b3a8eb9SGleb Smirnoff 			yyerror("skip on ! <interface> is not supported");
54073b3a8eb9SGleb Smirnoff 			errs++;
54083b3a8eb9SGleb Smirnoff 		} else
54093b3a8eb9SGleb Smirnoff 			errs += pfctl_set_interface_flags(pf,
54103b3a8eb9SGleb Smirnoff 			    interface->ifname, PFI_IFLAG_SKIP, 1);
54113b3a8eb9SGleb Smirnoff 	);
54123b3a8eb9SGleb Smirnoff 	if (pf->opts & PF_OPT_VERBOSE)
54133b3a8eb9SGleb Smirnoff 		printf(" }\n");
54143b3a8eb9SGleb Smirnoff 
54153b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_if, interfaces);
54163b3a8eb9SGleb Smirnoff 
54173b3a8eb9SGleb Smirnoff 	if (errs)
54183b3a8eb9SGleb Smirnoff 		return (1);
54193b3a8eb9SGleb Smirnoff 	else
54203b3a8eb9SGleb Smirnoff 		return (0);
54213b3a8eb9SGleb Smirnoff }
54223b3a8eb9SGleb Smirnoff 
54233b3a8eb9SGleb Smirnoff #undef FREE_LIST
54243b3a8eb9SGleb Smirnoff #undef LOOP_THROUGH
54253b3a8eb9SGleb Smirnoff 
54263b3a8eb9SGleb Smirnoff int
54273b3a8eb9SGleb Smirnoff check_rulestate(int desired_state)
54283b3a8eb9SGleb Smirnoff {
54293b3a8eb9SGleb Smirnoff 	if (require_order && (rulestate > desired_state)) {
54303b3a8eb9SGleb Smirnoff 		yyerror("Rules must be in order: options, normalization, "
54313b3a8eb9SGleb Smirnoff 		    "queueing, translation, filtering");
54323b3a8eb9SGleb Smirnoff 		return (1);
54333b3a8eb9SGleb Smirnoff 	}
54343b3a8eb9SGleb Smirnoff 	rulestate = desired_state;
54353b3a8eb9SGleb Smirnoff 	return (0);
54363b3a8eb9SGleb Smirnoff }
54373b3a8eb9SGleb Smirnoff 
54383b3a8eb9SGleb Smirnoff int
54393b3a8eb9SGleb Smirnoff kw_cmp(const void *k, const void *e)
54403b3a8eb9SGleb Smirnoff {
54413b3a8eb9SGleb Smirnoff 	return (strcmp(k, ((const struct keywords *)e)->k_name));
54423b3a8eb9SGleb Smirnoff }
54433b3a8eb9SGleb Smirnoff 
54443b3a8eb9SGleb Smirnoff int
54453b3a8eb9SGleb Smirnoff lookup(char *s)
54463b3a8eb9SGleb Smirnoff {
54473b3a8eb9SGleb Smirnoff 	/* this has to be sorted always */
54483b3a8eb9SGleb Smirnoff 	static const struct keywords keywords[] = {
54493b3a8eb9SGleb Smirnoff 		{ "all",		ALL},
54503b3a8eb9SGleb Smirnoff 		{ "allow-opts",		ALLOWOPTS},
54513b3a8eb9SGleb Smirnoff 		{ "altq",		ALTQ},
54523b3a8eb9SGleb Smirnoff 		{ "anchor",		ANCHOR},
54533b3a8eb9SGleb Smirnoff 		{ "antispoof",		ANTISPOOF},
54543b3a8eb9SGleb Smirnoff 		{ "any",		ANY},
54553b3a8eb9SGleb Smirnoff 		{ "bandwidth",		BANDWIDTH},
54563b3a8eb9SGleb Smirnoff 		{ "binat",		BINAT},
54573b3a8eb9SGleb Smirnoff 		{ "binat-anchor",	BINATANCHOR},
54583b3a8eb9SGleb Smirnoff 		{ "bitmask",		BITMASK},
54593b3a8eb9SGleb Smirnoff 		{ "block",		BLOCK},
54603b3a8eb9SGleb Smirnoff 		{ "block-policy",	BLOCKPOLICY},
5461a5b789f6SErmal Luçi 		{ "buckets",		BUCKETS},
54623b3a8eb9SGleb Smirnoff 		{ "cbq",		CBQ},
54633b3a8eb9SGleb Smirnoff 		{ "code",		CODE},
54640a70aaf8SLuiz Otavio O Souza 		{ "codelq",		CODEL},
54653b3a8eb9SGleb Smirnoff 		{ "crop",		FRAGCROP},
54663b3a8eb9SGleb Smirnoff 		{ "debug",		DEBUG},
54673b3a8eb9SGleb Smirnoff 		{ "divert-reply",	DIVERTREPLY},
54683b3a8eb9SGleb Smirnoff 		{ "divert-to",		DIVERTTO},
54693b3a8eb9SGleb Smirnoff 		{ "drop",		DROP},
54703b3a8eb9SGleb Smirnoff 		{ "drop-ovl",		FRAGDROP},
54713b3a8eb9SGleb Smirnoff 		{ "dup-to",		DUPTO},
5472a5b789f6SErmal Luçi 		{ "fairq",		FAIRQ},
54733b3a8eb9SGleb Smirnoff 		{ "fastroute",		FASTROUTE},
54743b3a8eb9SGleb Smirnoff 		{ "file",		FILENAME},
54753b3a8eb9SGleb Smirnoff 		{ "fingerprints",	FINGERPRINTS},
54763b3a8eb9SGleb Smirnoff 		{ "flags",		FLAGS},
54773b3a8eb9SGleb Smirnoff 		{ "floating",		FLOATING},
54783b3a8eb9SGleb Smirnoff 		{ "flush",		FLUSH},
54793b3a8eb9SGleb Smirnoff 		{ "for",		FOR},
54803b3a8eb9SGleb Smirnoff 		{ "fragment",		FRAGMENT},
54813b3a8eb9SGleb Smirnoff 		{ "from",		FROM},
54823b3a8eb9SGleb Smirnoff 		{ "global",		GLOBAL},
54833b3a8eb9SGleb Smirnoff 		{ "group",		GROUP},
54843b3a8eb9SGleb Smirnoff 		{ "hfsc",		HFSC},
5485a5b789f6SErmal Luçi 		{ "hogs",		HOGS},
54863b3a8eb9SGleb Smirnoff 		{ "hostid",		HOSTID},
54873b3a8eb9SGleb Smirnoff 		{ "icmp-type",		ICMPTYPE},
54883b3a8eb9SGleb Smirnoff 		{ "icmp6-type",		ICMP6TYPE},
54893b3a8eb9SGleb Smirnoff 		{ "if-bound",		IFBOUND},
54903b3a8eb9SGleb Smirnoff 		{ "in",			IN},
54913b3a8eb9SGleb Smirnoff 		{ "include",		INCLUDE},
54923b3a8eb9SGleb Smirnoff 		{ "inet",		INET},
54933b3a8eb9SGleb Smirnoff 		{ "inet6",		INET6},
54940a70aaf8SLuiz Otavio O Souza 		{ "interval",		INTERVAL},
54953b3a8eb9SGleb Smirnoff 		{ "keep",		KEEP},
54963b3a8eb9SGleb Smirnoff 		{ "label",		LABEL},
54973b3a8eb9SGleb Smirnoff 		{ "limit",		LIMIT},
54983b3a8eb9SGleb Smirnoff 		{ "linkshare",		LINKSHARE},
54993b3a8eb9SGleb Smirnoff 		{ "load",		LOAD},
55003b3a8eb9SGleb Smirnoff 		{ "log",		LOG},
55013b3a8eb9SGleb Smirnoff 		{ "loginterface",	LOGINTERFACE},
55023b3a8eb9SGleb Smirnoff 		{ "max",		MAXIMUM},
55033b3a8eb9SGleb Smirnoff 		{ "max-mss",		MAXMSS},
55043b3a8eb9SGleb Smirnoff 		{ "max-src-conn",	MAXSRCCONN},
55053b3a8eb9SGleb Smirnoff 		{ "max-src-conn-rate",	MAXSRCCONNRATE},
55063b3a8eb9SGleb Smirnoff 		{ "max-src-nodes",	MAXSRCNODES},
55073b3a8eb9SGleb Smirnoff 		{ "max-src-states",	MAXSRCSTATES},
55083b3a8eb9SGleb Smirnoff 		{ "min-ttl",		MINTTL},
55093b3a8eb9SGleb Smirnoff 		{ "modulate",		MODULATE},
55103b3a8eb9SGleb Smirnoff 		{ "nat",		NAT},
55113b3a8eb9SGleb Smirnoff 		{ "nat-anchor",		NATANCHOR},
55123b3a8eb9SGleb Smirnoff 		{ "no",			NO},
55133b3a8eb9SGleb Smirnoff 		{ "no-df",		NODF},
55143b3a8eb9SGleb Smirnoff 		{ "no-route",		NOROUTE},
55153b3a8eb9SGleb Smirnoff 		{ "no-sync",		NOSYNC},
55163b3a8eb9SGleb Smirnoff 		{ "on",			ON},
55173b3a8eb9SGleb Smirnoff 		{ "optimization",	OPTIMIZATION},
55183b3a8eb9SGleb Smirnoff 		{ "os",			OS},
55193b3a8eb9SGleb Smirnoff 		{ "out",		OUT},
55203b3a8eb9SGleb Smirnoff 		{ "overload",		OVERLOAD},
55213b3a8eb9SGleb Smirnoff 		{ "pass",		PASS},
55223b3a8eb9SGleb Smirnoff 		{ "port",		PORT},
55233e248e0fSKristof Provost 		{ "prio",		PRIO},
55243b3a8eb9SGleb Smirnoff 		{ "priority",		PRIORITY},
55253b3a8eb9SGleb Smirnoff 		{ "priq",		PRIQ},
55263b3a8eb9SGleb Smirnoff 		{ "probability",	PROBABILITY},
55273b3a8eb9SGleb Smirnoff 		{ "proto",		PROTO},
55283b3a8eb9SGleb Smirnoff 		{ "qlimit",		QLIMIT},
55293b3a8eb9SGleb Smirnoff 		{ "queue",		QUEUE},
55303b3a8eb9SGleb Smirnoff 		{ "quick",		QUICK},
55313b3a8eb9SGleb Smirnoff 		{ "random",		RANDOM},
55323b3a8eb9SGleb Smirnoff 		{ "random-id",		RANDOMID},
55333b3a8eb9SGleb Smirnoff 		{ "rdr",		RDR},
55343b3a8eb9SGleb Smirnoff 		{ "rdr-anchor",		RDRANCHOR},
55353b3a8eb9SGleb Smirnoff 		{ "realtime",		REALTIME},
55363b3a8eb9SGleb Smirnoff 		{ "reassemble",		REASSEMBLE},
55373b3a8eb9SGleb Smirnoff 		{ "reply-to",		REPLYTO},
55383b3a8eb9SGleb Smirnoff 		{ "require-order",	REQUIREORDER},
55393b3a8eb9SGleb Smirnoff 		{ "return",		RETURN},
55403b3a8eb9SGleb Smirnoff 		{ "return-icmp",	RETURNICMP},
55413b3a8eb9SGleb Smirnoff 		{ "return-icmp6",	RETURNICMP6},
55423b3a8eb9SGleb Smirnoff 		{ "return-rst",		RETURNRST},
55433b3a8eb9SGleb Smirnoff 		{ "round-robin",	ROUNDROBIN},
55443b3a8eb9SGleb Smirnoff 		{ "route",		ROUTE},
55453b3a8eb9SGleb Smirnoff 		{ "route-to",		ROUTETO},
55463b3a8eb9SGleb Smirnoff 		{ "rtable",		RTABLE},
55473b3a8eb9SGleb Smirnoff 		{ "rule",		RULE},
55483b3a8eb9SGleb Smirnoff 		{ "ruleset-optimization",	RULESET_OPTIMIZATION},
55493b3a8eb9SGleb Smirnoff 		{ "scrub",		SCRUB},
55503b3a8eb9SGleb Smirnoff 		{ "set",		SET},
55513b3a8eb9SGleb Smirnoff 		{ "set-tos",		SETTOS},
55523b3a8eb9SGleb Smirnoff 		{ "skip",		SKIP},
55533b3a8eb9SGleb Smirnoff 		{ "sloppy",		SLOPPY},
55543b3a8eb9SGleb Smirnoff 		{ "source-hash",	SOURCEHASH},
55553b3a8eb9SGleb Smirnoff 		{ "source-track",	SOURCETRACK},
55563b3a8eb9SGleb Smirnoff 		{ "state",		STATE},
55573b3a8eb9SGleb Smirnoff 		{ "state-defaults",	STATEDEFAULTS},
55583b3a8eb9SGleb Smirnoff 		{ "state-policy",	STATEPOLICY},
55593b3a8eb9SGleb Smirnoff 		{ "static-port",	STATICPORT},
55603b3a8eb9SGleb Smirnoff 		{ "sticky-address",	STICKYADDRESS},
55613b3a8eb9SGleb Smirnoff 		{ "synproxy",		SYNPROXY},
55623b3a8eb9SGleb Smirnoff 		{ "table",		TABLE},
55633b3a8eb9SGleb Smirnoff 		{ "tag",		TAG},
55643b3a8eb9SGleb Smirnoff 		{ "tagged",		TAGGED},
55650a70aaf8SLuiz Otavio O Souza 		{ "target",		TARGET},
55663b3a8eb9SGleb Smirnoff 		{ "tbrsize",		TBRSIZE},
55673b3a8eb9SGleb Smirnoff 		{ "timeout",		TIMEOUT},
55683b3a8eb9SGleb Smirnoff 		{ "to",			TO},
55693b3a8eb9SGleb Smirnoff 		{ "tos",		TOS},
55703b3a8eb9SGleb Smirnoff 		{ "ttl",		TTL},
55713b3a8eb9SGleb Smirnoff 		{ "upperlimit",		UPPERLIMIT},
55723b3a8eb9SGleb Smirnoff 		{ "urpf-failed",	URPFFAILED},
55733b3a8eb9SGleb Smirnoff 		{ "user",		USER},
55743b3a8eb9SGleb Smirnoff 	};
55753b3a8eb9SGleb Smirnoff 	const struct keywords	*p;
55763b3a8eb9SGleb Smirnoff 
55773b3a8eb9SGleb Smirnoff 	p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
55783b3a8eb9SGleb Smirnoff 	    sizeof(keywords[0]), kw_cmp);
55793b3a8eb9SGleb Smirnoff 
55803b3a8eb9SGleb Smirnoff 	if (p) {
55813b3a8eb9SGleb Smirnoff 		if (debug > 1)
55823b3a8eb9SGleb Smirnoff 			fprintf(stderr, "%s: %d\n", s, p->k_val);
55833b3a8eb9SGleb Smirnoff 		return (p->k_val);
55843b3a8eb9SGleb Smirnoff 	} else {
55853b3a8eb9SGleb Smirnoff 		if (debug > 1)
55863b3a8eb9SGleb Smirnoff 			fprintf(stderr, "string: %s\n", s);
55873b3a8eb9SGleb Smirnoff 		return (STRING);
55883b3a8eb9SGleb Smirnoff 	}
55893b3a8eb9SGleb Smirnoff }
55903b3a8eb9SGleb Smirnoff 
55913b3a8eb9SGleb Smirnoff #define MAXPUSHBACK	128
55923b3a8eb9SGleb Smirnoff 
559313cfafabSKristof Provost static char	*parsebuf;
559413cfafabSKristof Provost static int	 parseindex;
559513cfafabSKristof Provost static char	 pushback_buffer[MAXPUSHBACK];
559613cfafabSKristof Provost static int	 pushback_index = 0;
55973b3a8eb9SGleb Smirnoff 
55983b3a8eb9SGleb Smirnoff int
55993b3a8eb9SGleb Smirnoff lgetc(int quotec)
56003b3a8eb9SGleb Smirnoff {
56013b3a8eb9SGleb Smirnoff 	int		c, next;
56023b3a8eb9SGleb Smirnoff 
56033b3a8eb9SGleb Smirnoff 	if (parsebuf) {
56043b3a8eb9SGleb Smirnoff 		/* Read character from the parsebuffer instead of input. */
56053b3a8eb9SGleb Smirnoff 		if (parseindex >= 0) {
56063b3a8eb9SGleb Smirnoff 			c = parsebuf[parseindex++];
56073b3a8eb9SGleb Smirnoff 			if (c != '\0')
56083b3a8eb9SGleb Smirnoff 				return (c);
56093b3a8eb9SGleb Smirnoff 			parsebuf = NULL;
56103b3a8eb9SGleb Smirnoff 		} else
56113b3a8eb9SGleb Smirnoff 			parseindex++;
56123b3a8eb9SGleb Smirnoff 	}
56133b3a8eb9SGleb Smirnoff 
56143b3a8eb9SGleb Smirnoff 	if (pushback_index)
56153b3a8eb9SGleb Smirnoff 		return (pushback_buffer[--pushback_index]);
56163b3a8eb9SGleb Smirnoff 
56173b3a8eb9SGleb Smirnoff 	if (quotec) {
56183b3a8eb9SGleb Smirnoff 		if ((c = getc(file->stream)) == EOF) {
56193b3a8eb9SGleb Smirnoff 			yyerror("reached end of file while parsing quoted string");
56203b3a8eb9SGleb Smirnoff 			if (popfile() == EOF)
56213b3a8eb9SGleb Smirnoff 				return (EOF);
56223b3a8eb9SGleb Smirnoff 			return (quotec);
56233b3a8eb9SGleb Smirnoff 		}
56243b3a8eb9SGleb Smirnoff 		return (c);
56253b3a8eb9SGleb Smirnoff 	}
56263b3a8eb9SGleb Smirnoff 
56273b3a8eb9SGleb Smirnoff 	while ((c = getc(file->stream)) == '\\') {
56283b3a8eb9SGleb Smirnoff 		next = getc(file->stream);
56293b3a8eb9SGleb Smirnoff 		if (next != '\n') {
56303b3a8eb9SGleb Smirnoff 			c = next;
56313b3a8eb9SGleb Smirnoff 			break;
56323b3a8eb9SGleb Smirnoff 		}
56333b3a8eb9SGleb Smirnoff 		yylval.lineno = file->lineno;
56343b3a8eb9SGleb Smirnoff 		file->lineno++;
56353b3a8eb9SGleb Smirnoff 	}
56363b3a8eb9SGleb Smirnoff 
56373b3a8eb9SGleb Smirnoff 	while (c == EOF) {
56383b3a8eb9SGleb Smirnoff 		if (popfile() == EOF)
56393b3a8eb9SGleb Smirnoff 			return (EOF);
56403b3a8eb9SGleb Smirnoff 		c = getc(file->stream);
56413b3a8eb9SGleb Smirnoff 	}
56423b3a8eb9SGleb Smirnoff 	return (c);
56433b3a8eb9SGleb Smirnoff }
56443b3a8eb9SGleb Smirnoff 
56453b3a8eb9SGleb Smirnoff int
56463b3a8eb9SGleb Smirnoff lungetc(int c)
56473b3a8eb9SGleb Smirnoff {
56483b3a8eb9SGleb Smirnoff 	if (c == EOF)
56493b3a8eb9SGleb Smirnoff 		return (EOF);
56503b3a8eb9SGleb Smirnoff 	if (parsebuf) {
56513b3a8eb9SGleb Smirnoff 		parseindex--;
56523b3a8eb9SGleb Smirnoff 		if (parseindex >= 0)
56533b3a8eb9SGleb Smirnoff 			return (c);
56543b3a8eb9SGleb Smirnoff 	}
56553b3a8eb9SGleb Smirnoff 	if (pushback_index < MAXPUSHBACK-1)
56563b3a8eb9SGleb Smirnoff 		return (pushback_buffer[pushback_index++] = c);
56573b3a8eb9SGleb Smirnoff 	else
56583b3a8eb9SGleb Smirnoff 		return (EOF);
56593b3a8eb9SGleb Smirnoff }
56603b3a8eb9SGleb Smirnoff 
56613b3a8eb9SGleb Smirnoff int
56623b3a8eb9SGleb Smirnoff findeol(void)
56633b3a8eb9SGleb Smirnoff {
56643b3a8eb9SGleb Smirnoff 	int	c;
56653b3a8eb9SGleb Smirnoff 
56663b3a8eb9SGleb Smirnoff 	parsebuf = NULL;
56673b3a8eb9SGleb Smirnoff 
56683b3a8eb9SGleb Smirnoff 	/* skip to either EOF or the first real EOL */
56693b3a8eb9SGleb Smirnoff 	while (1) {
56703b3a8eb9SGleb Smirnoff 		if (pushback_index)
56713b3a8eb9SGleb Smirnoff 			c = pushback_buffer[--pushback_index];
56723b3a8eb9SGleb Smirnoff 		else
56733b3a8eb9SGleb Smirnoff 			c = lgetc(0);
56743b3a8eb9SGleb Smirnoff 		if (c == '\n') {
56753b3a8eb9SGleb Smirnoff 			file->lineno++;
56763b3a8eb9SGleb Smirnoff 			break;
56773b3a8eb9SGleb Smirnoff 		}
56783b3a8eb9SGleb Smirnoff 		if (c == EOF)
56793b3a8eb9SGleb Smirnoff 			break;
56803b3a8eb9SGleb Smirnoff 	}
56813b3a8eb9SGleb Smirnoff 	return (ERROR);
56823b3a8eb9SGleb Smirnoff }
56833b3a8eb9SGleb Smirnoff 
56843b3a8eb9SGleb Smirnoff int
56853b3a8eb9SGleb Smirnoff yylex(void)
56863b3a8eb9SGleb Smirnoff {
56873b3a8eb9SGleb Smirnoff 	char	 buf[8096];
56883b3a8eb9SGleb Smirnoff 	char	*p, *val;
56893b3a8eb9SGleb Smirnoff 	int	 quotec, next, c;
56903b3a8eb9SGleb Smirnoff 	int	 token;
56913b3a8eb9SGleb Smirnoff 
56923b3a8eb9SGleb Smirnoff top:
56933b3a8eb9SGleb Smirnoff 	p = buf;
56943b3a8eb9SGleb Smirnoff 	while ((c = lgetc(0)) == ' ' || c == '\t')
56953b3a8eb9SGleb Smirnoff 		; /* nothing */
56963b3a8eb9SGleb Smirnoff 
56973b3a8eb9SGleb Smirnoff 	yylval.lineno = file->lineno;
56983b3a8eb9SGleb Smirnoff 	if (c == '#')
56993b3a8eb9SGleb Smirnoff 		while ((c = lgetc(0)) != '\n' && c != EOF)
57003b3a8eb9SGleb Smirnoff 			; /* nothing */
57013b3a8eb9SGleb Smirnoff 	if (c == '$' && parsebuf == NULL) {
57023b3a8eb9SGleb Smirnoff 		while (1) {
57033b3a8eb9SGleb Smirnoff 			if ((c = lgetc(0)) == EOF)
57043b3a8eb9SGleb Smirnoff 				return (0);
57053b3a8eb9SGleb Smirnoff 
57063b3a8eb9SGleb Smirnoff 			if (p + 1 >= buf + sizeof(buf) - 1) {
57073b3a8eb9SGleb Smirnoff 				yyerror("string too long");
57083b3a8eb9SGleb Smirnoff 				return (findeol());
57093b3a8eb9SGleb Smirnoff 			}
57103b3a8eb9SGleb Smirnoff 			if (isalnum(c) || c == '_') {
57113b3a8eb9SGleb Smirnoff 				*p++ = (char)c;
57123b3a8eb9SGleb Smirnoff 				continue;
57133b3a8eb9SGleb Smirnoff 			}
57143b3a8eb9SGleb Smirnoff 			*p = '\0';
57153b3a8eb9SGleb Smirnoff 			lungetc(c);
57163b3a8eb9SGleb Smirnoff 			break;
57173b3a8eb9SGleb Smirnoff 		}
57183b3a8eb9SGleb Smirnoff 		val = symget(buf);
57193b3a8eb9SGleb Smirnoff 		if (val == NULL) {
57203b3a8eb9SGleb Smirnoff 			yyerror("macro '%s' not defined", buf);
57213b3a8eb9SGleb Smirnoff 			return (findeol());
57223b3a8eb9SGleb Smirnoff 		}
57233b3a8eb9SGleb Smirnoff 		parsebuf = val;
57243b3a8eb9SGleb Smirnoff 		parseindex = 0;
57253b3a8eb9SGleb Smirnoff 		goto top;
57263b3a8eb9SGleb Smirnoff 	}
57273b3a8eb9SGleb Smirnoff 
57283b3a8eb9SGleb Smirnoff 	switch (c) {
57293b3a8eb9SGleb Smirnoff 	case '\'':
57303b3a8eb9SGleb Smirnoff 	case '"':
57313b3a8eb9SGleb Smirnoff 		quotec = c;
57323b3a8eb9SGleb Smirnoff 		while (1) {
57333b3a8eb9SGleb Smirnoff 			if ((c = lgetc(quotec)) == EOF)
57343b3a8eb9SGleb Smirnoff 				return (0);
57353b3a8eb9SGleb Smirnoff 			if (c == '\n') {
57363b3a8eb9SGleb Smirnoff 				file->lineno++;
57373b3a8eb9SGleb Smirnoff 				continue;
57383b3a8eb9SGleb Smirnoff 			} else if (c == '\\') {
57393b3a8eb9SGleb Smirnoff 				if ((next = lgetc(quotec)) == EOF)
57403b3a8eb9SGleb Smirnoff 					return (0);
57413b3a8eb9SGleb Smirnoff 				if (next == quotec || c == ' ' || c == '\t')
57423b3a8eb9SGleb Smirnoff 					c = next;
57433b3a8eb9SGleb Smirnoff 				else if (next == '\n')
57443b3a8eb9SGleb Smirnoff 					continue;
57453b3a8eb9SGleb Smirnoff 				else
57463b3a8eb9SGleb Smirnoff 					lungetc(next);
57473b3a8eb9SGleb Smirnoff 			} else if (c == quotec) {
57483b3a8eb9SGleb Smirnoff 				*p = '\0';
57493b3a8eb9SGleb Smirnoff 				break;
57503b3a8eb9SGleb Smirnoff 			}
57513b3a8eb9SGleb Smirnoff 			if (p + 1 >= buf + sizeof(buf) - 1) {
57523b3a8eb9SGleb Smirnoff 				yyerror("string too long");
57533b3a8eb9SGleb Smirnoff 				return (findeol());
57543b3a8eb9SGleb Smirnoff 			}
57553b3a8eb9SGleb Smirnoff 			*p++ = (char)c;
57563b3a8eb9SGleb Smirnoff 		}
57573b3a8eb9SGleb Smirnoff 		yylval.v.string = strdup(buf);
57583b3a8eb9SGleb Smirnoff 		if (yylval.v.string == NULL)
57593b3a8eb9SGleb Smirnoff 			err(1, "yylex: strdup");
57603b3a8eb9SGleb Smirnoff 		return (STRING);
57613b3a8eb9SGleb Smirnoff 	case '<':
57623b3a8eb9SGleb Smirnoff 		next = lgetc(0);
57633b3a8eb9SGleb Smirnoff 		if (next == '>') {
57643b3a8eb9SGleb Smirnoff 			yylval.v.i = PF_OP_XRG;
57653b3a8eb9SGleb Smirnoff 			return (PORTBINARY);
57663b3a8eb9SGleb Smirnoff 		}
57673b3a8eb9SGleb Smirnoff 		lungetc(next);
57683b3a8eb9SGleb Smirnoff 		break;
57693b3a8eb9SGleb Smirnoff 	case '>':
57703b3a8eb9SGleb Smirnoff 		next = lgetc(0);
57713b3a8eb9SGleb Smirnoff 		if (next == '<') {
57723b3a8eb9SGleb Smirnoff 			yylval.v.i = PF_OP_IRG;
57733b3a8eb9SGleb Smirnoff 			return (PORTBINARY);
57743b3a8eb9SGleb Smirnoff 		}
57753b3a8eb9SGleb Smirnoff 		lungetc(next);
57763b3a8eb9SGleb Smirnoff 		break;
57773b3a8eb9SGleb Smirnoff 	case '-':
57783b3a8eb9SGleb Smirnoff 		next = lgetc(0);
57793b3a8eb9SGleb Smirnoff 		if (next == '>')
57803b3a8eb9SGleb Smirnoff 			return (ARROW);
57813b3a8eb9SGleb Smirnoff 		lungetc(next);
57823b3a8eb9SGleb Smirnoff 		break;
57833b3a8eb9SGleb Smirnoff 	}
57843b3a8eb9SGleb Smirnoff 
57853b3a8eb9SGleb Smirnoff #define allowed_to_end_number(x) \
57863b3a8eb9SGleb Smirnoff 	(isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
57873b3a8eb9SGleb Smirnoff 
57883b3a8eb9SGleb Smirnoff 	if (c == '-' || isdigit(c)) {
57893b3a8eb9SGleb Smirnoff 		do {
57903b3a8eb9SGleb Smirnoff 			*p++ = c;
57913b3a8eb9SGleb Smirnoff 			if ((unsigned)(p-buf) >= sizeof(buf)) {
57923b3a8eb9SGleb Smirnoff 				yyerror("string too long");
57933b3a8eb9SGleb Smirnoff 				return (findeol());
57943b3a8eb9SGleb Smirnoff 			}
57953b3a8eb9SGleb Smirnoff 		} while ((c = lgetc(0)) != EOF && isdigit(c));
57963b3a8eb9SGleb Smirnoff 		lungetc(c);
57973b3a8eb9SGleb Smirnoff 		if (p == buf + 1 && buf[0] == '-')
57983b3a8eb9SGleb Smirnoff 			goto nodigits;
57993b3a8eb9SGleb Smirnoff 		if (c == EOF || allowed_to_end_number(c)) {
58003b3a8eb9SGleb Smirnoff 			const char *errstr = NULL;
58013b3a8eb9SGleb Smirnoff 
58023b3a8eb9SGleb Smirnoff 			*p = '\0';
58033b3a8eb9SGleb Smirnoff 			yylval.v.number = strtonum(buf, LLONG_MIN,
58043b3a8eb9SGleb Smirnoff 			    LLONG_MAX, &errstr);
58053b3a8eb9SGleb Smirnoff 			if (errstr) {
58063b3a8eb9SGleb Smirnoff 				yyerror("\"%s\" invalid number: %s",
58073b3a8eb9SGleb Smirnoff 				    buf, errstr);
58083b3a8eb9SGleb Smirnoff 				return (findeol());
58093b3a8eb9SGleb Smirnoff 			}
58103b3a8eb9SGleb Smirnoff 			return (NUMBER);
58113b3a8eb9SGleb Smirnoff 		} else {
58123b3a8eb9SGleb Smirnoff nodigits:
58133b3a8eb9SGleb Smirnoff 			while (p > buf + 1)
58143b3a8eb9SGleb Smirnoff 				lungetc(*--p);
58153b3a8eb9SGleb Smirnoff 			c = *--p;
58163b3a8eb9SGleb Smirnoff 			if (c == '-')
58173b3a8eb9SGleb Smirnoff 				return (c);
58183b3a8eb9SGleb Smirnoff 		}
58193b3a8eb9SGleb Smirnoff 	}
58203b3a8eb9SGleb Smirnoff 
58213b3a8eb9SGleb Smirnoff #define allowed_in_string(x) \
58223b3a8eb9SGleb Smirnoff 	(isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
58233b3a8eb9SGleb Smirnoff 	x != '{' && x != '}' && x != '<' && x != '>' && \
58243b3a8eb9SGleb Smirnoff 	x != '!' && x != '=' && x != '/' && x != '#' && \
58253b3a8eb9SGleb Smirnoff 	x != ','))
58263b3a8eb9SGleb Smirnoff 
58273b3a8eb9SGleb Smirnoff 	if (isalnum(c) || c == ':' || c == '_') {
58283b3a8eb9SGleb Smirnoff 		do {
58293b3a8eb9SGleb Smirnoff 			*p++ = c;
58303b3a8eb9SGleb Smirnoff 			if ((unsigned)(p-buf) >= sizeof(buf)) {
58313b3a8eb9SGleb Smirnoff 				yyerror("string too long");
58323b3a8eb9SGleb Smirnoff 				return (findeol());
58333b3a8eb9SGleb Smirnoff 			}
58343b3a8eb9SGleb Smirnoff 		} while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
58353b3a8eb9SGleb Smirnoff 		lungetc(c);
58363b3a8eb9SGleb Smirnoff 		*p = '\0';
58373b3a8eb9SGleb Smirnoff 		if ((token = lookup(buf)) == STRING)
58383b3a8eb9SGleb Smirnoff 			if ((yylval.v.string = strdup(buf)) == NULL)
58393b3a8eb9SGleb Smirnoff 				err(1, "yylex: strdup");
58403b3a8eb9SGleb Smirnoff 		return (token);
58413b3a8eb9SGleb Smirnoff 	}
58423b3a8eb9SGleb Smirnoff 	if (c == '\n') {
58433b3a8eb9SGleb Smirnoff 		yylval.lineno = file->lineno;
58443b3a8eb9SGleb Smirnoff 		file->lineno++;
58453b3a8eb9SGleb Smirnoff 	}
58463b3a8eb9SGleb Smirnoff 	if (c == EOF)
58473b3a8eb9SGleb Smirnoff 		return (0);
58483b3a8eb9SGleb Smirnoff 	return (c);
58493b3a8eb9SGleb Smirnoff }
58503b3a8eb9SGleb Smirnoff 
58513b3a8eb9SGleb Smirnoff int
58523b3a8eb9SGleb Smirnoff check_file_secrecy(int fd, const char *fname)
58533b3a8eb9SGleb Smirnoff {
58543b3a8eb9SGleb Smirnoff 	struct stat	st;
58553b3a8eb9SGleb Smirnoff 
58563b3a8eb9SGleb Smirnoff 	if (fstat(fd, &st)) {
58573b3a8eb9SGleb Smirnoff 		warn("cannot stat %s", fname);
58583b3a8eb9SGleb Smirnoff 		return (-1);
58593b3a8eb9SGleb Smirnoff 	}
58603b3a8eb9SGleb Smirnoff 	if (st.st_uid != 0 && st.st_uid != getuid()) {
58613b3a8eb9SGleb Smirnoff 		warnx("%s: owner not root or current user", fname);
58623b3a8eb9SGleb Smirnoff 		return (-1);
58633b3a8eb9SGleb Smirnoff 	}
58643b3a8eb9SGleb Smirnoff 	if (st.st_mode & (S_IRWXG | S_IRWXO)) {
58653b3a8eb9SGleb Smirnoff 		warnx("%s: group/world readable/writeable", fname);
58663b3a8eb9SGleb Smirnoff 		return (-1);
58673b3a8eb9SGleb Smirnoff 	}
58683b3a8eb9SGleb Smirnoff 	return (0);
58693b3a8eb9SGleb Smirnoff }
58703b3a8eb9SGleb Smirnoff 
58713b3a8eb9SGleb Smirnoff struct file *
58723b3a8eb9SGleb Smirnoff pushfile(const char *name, int secret)
58733b3a8eb9SGleb Smirnoff {
58743b3a8eb9SGleb Smirnoff 	struct file	*nfile;
58753b3a8eb9SGleb Smirnoff 
58763b3a8eb9SGleb Smirnoff 	if ((nfile = calloc(1, sizeof(struct file))) == NULL ||
58773b3a8eb9SGleb Smirnoff 	    (nfile->name = strdup(name)) == NULL) {
58783b3a8eb9SGleb Smirnoff 		warn("malloc");
58793b3a8eb9SGleb Smirnoff 		return (NULL);
58803b3a8eb9SGleb Smirnoff 	}
58813b3a8eb9SGleb Smirnoff 	if (TAILQ_FIRST(&files) == NULL && strcmp(nfile->name, "-") == 0) {
58823b3a8eb9SGleb Smirnoff 		nfile->stream = stdin;
58833b3a8eb9SGleb Smirnoff 		free(nfile->name);
58843b3a8eb9SGleb Smirnoff 		if ((nfile->name = strdup("stdin")) == NULL) {
58853b3a8eb9SGleb Smirnoff 			warn("strdup");
58863b3a8eb9SGleb Smirnoff 			free(nfile);
58873b3a8eb9SGleb Smirnoff 			return (NULL);
58883b3a8eb9SGleb Smirnoff 		}
58893b3a8eb9SGleb Smirnoff 	} else if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
58903b3a8eb9SGleb Smirnoff 		warn("%s", nfile->name);
58913b3a8eb9SGleb Smirnoff 		free(nfile->name);
58923b3a8eb9SGleb Smirnoff 		free(nfile);
58933b3a8eb9SGleb Smirnoff 		return (NULL);
58943b3a8eb9SGleb Smirnoff 	} else if (secret &&
58953b3a8eb9SGleb Smirnoff 	    check_file_secrecy(fileno(nfile->stream), nfile->name)) {
58963b3a8eb9SGleb Smirnoff 		fclose(nfile->stream);
58973b3a8eb9SGleb Smirnoff 		free(nfile->name);
58983b3a8eb9SGleb Smirnoff 		free(nfile);
58993b3a8eb9SGleb Smirnoff 		return (NULL);
59003b3a8eb9SGleb Smirnoff 	}
59013b3a8eb9SGleb Smirnoff 	nfile->lineno = 1;
59023b3a8eb9SGleb Smirnoff 	TAILQ_INSERT_TAIL(&files, nfile, entry);
59033b3a8eb9SGleb Smirnoff 	return (nfile);
59043b3a8eb9SGleb Smirnoff }
59053b3a8eb9SGleb Smirnoff 
59063b3a8eb9SGleb Smirnoff int
59073b3a8eb9SGleb Smirnoff popfile(void)
59083b3a8eb9SGleb Smirnoff {
59093b3a8eb9SGleb Smirnoff 	struct file	*prev;
59103b3a8eb9SGleb Smirnoff 
59113b3a8eb9SGleb Smirnoff 	if ((prev = TAILQ_PREV(file, files, entry)) != NULL) {
59123b3a8eb9SGleb Smirnoff 		prev->errors += file->errors;
59133b3a8eb9SGleb Smirnoff 		TAILQ_REMOVE(&files, file, entry);
59143b3a8eb9SGleb Smirnoff 		fclose(file->stream);
59153b3a8eb9SGleb Smirnoff 		free(file->name);
59163b3a8eb9SGleb Smirnoff 		free(file);
59173b3a8eb9SGleb Smirnoff 		file = prev;
59183b3a8eb9SGleb Smirnoff 		return (0);
59193b3a8eb9SGleb Smirnoff 	}
59203b3a8eb9SGleb Smirnoff 	return (EOF);
59213b3a8eb9SGleb Smirnoff }
59223b3a8eb9SGleb Smirnoff 
59233b3a8eb9SGleb Smirnoff int
59243b3a8eb9SGleb Smirnoff parse_config(char *filename, struct pfctl *xpf)
59253b3a8eb9SGleb Smirnoff {
59263b3a8eb9SGleb Smirnoff 	int		 errors = 0;
59273b3a8eb9SGleb Smirnoff 	struct sym	*sym;
59283b3a8eb9SGleb Smirnoff 
59293b3a8eb9SGleb Smirnoff 	pf = xpf;
59303b3a8eb9SGleb Smirnoff 	errors = 0;
59313b3a8eb9SGleb Smirnoff 	rulestate = PFCTL_STATE_NONE;
59323b3a8eb9SGleb Smirnoff 	returnicmpdefault = (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
59333b3a8eb9SGleb Smirnoff 	returnicmp6default =
59343b3a8eb9SGleb Smirnoff 	    (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
59353b3a8eb9SGleb Smirnoff 	blockpolicy = PFRULE_DROP;
59363b3a8eb9SGleb Smirnoff 	require_order = 1;
59373b3a8eb9SGleb Smirnoff 
59383b3a8eb9SGleb Smirnoff 	if ((file = pushfile(filename, 0)) == NULL) {
59393b3a8eb9SGleb Smirnoff 		warn("cannot open the main config file!");
59403b3a8eb9SGleb Smirnoff 		return (-1);
59413b3a8eb9SGleb Smirnoff 	}
59423b3a8eb9SGleb Smirnoff 
59433b3a8eb9SGleb Smirnoff 	yyparse();
59443b3a8eb9SGleb Smirnoff 	errors = file->errors;
59453b3a8eb9SGleb Smirnoff 	popfile();
59463b3a8eb9SGleb Smirnoff 
59473b3a8eb9SGleb Smirnoff 	/* Free macros and check which have not been used. */
59483b3a8eb9SGleb Smirnoff 	while ((sym = TAILQ_FIRST(&symhead))) {
59493b3a8eb9SGleb Smirnoff 		if ((pf->opts & PF_OPT_VERBOSE2) && !sym->used)
59503b3a8eb9SGleb Smirnoff 			fprintf(stderr, "warning: macro '%s' not "
59513b3a8eb9SGleb Smirnoff 			    "used\n", sym->nam);
59523b3a8eb9SGleb Smirnoff 		free(sym->nam);
59533b3a8eb9SGleb Smirnoff 		free(sym->val);
59543b3a8eb9SGleb Smirnoff 		TAILQ_REMOVE(&symhead, sym, entry);
59553b3a8eb9SGleb Smirnoff 		free(sym);
59563b3a8eb9SGleb Smirnoff 	}
59573b3a8eb9SGleb Smirnoff 
59583b3a8eb9SGleb Smirnoff 	return (errors ? -1 : 0);
59593b3a8eb9SGleb Smirnoff }
59603b3a8eb9SGleb Smirnoff 
59613b3a8eb9SGleb Smirnoff int
59623b3a8eb9SGleb Smirnoff symset(const char *nam, const char *val, int persist)
59633b3a8eb9SGleb Smirnoff {
59643b3a8eb9SGleb Smirnoff 	struct sym	*sym;
59653b3a8eb9SGleb Smirnoff 
59663b3a8eb9SGleb Smirnoff 	for (sym = TAILQ_FIRST(&symhead); sym && strcmp(nam, sym->nam);
59673b3a8eb9SGleb Smirnoff 	    sym = TAILQ_NEXT(sym, entry))
59683b3a8eb9SGleb Smirnoff 		;	/* nothing */
59693b3a8eb9SGleb Smirnoff 
59703b3a8eb9SGleb Smirnoff 	if (sym != NULL) {
59713b3a8eb9SGleb Smirnoff 		if (sym->persist == 1)
59723b3a8eb9SGleb Smirnoff 			return (0);
59733b3a8eb9SGleb Smirnoff 		else {
59743b3a8eb9SGleb Smirnoff 			free(sym->nam);
59753b3a8eb9SGleb Smirnoff 			free(sym->val);
59763b3a8eb9SGleb Smirnoff 			TAILQ_REMOVE(&symhead, sym, entry);
59773b3a8eb9SGleb Smirnoff 			free(sym);
59783b3a8eb9SGleb Smirnoff 		}
59793b3a8eb9SGleb Smirnoff 	}
59803b3a8eb9SGleb Smirnoff 	if ((sym = calloc(1, sizeof(*sym))) == NULL)
59813b3a8eb9SGleb Smirnoff 		return (-1);
59823b3a8eb9SGleb Smirnoff 
59833b3a8eb9SGleb Smirnoff 	sym->nam = strdup(nam);
59843b3a8eb9SGleb Smirnoff 	if (sym->nam == NULL) {
59853b3a8eb9SGleb Smirnoff 		free(sym);
59863b3a8eb9SGleb Smirnoff 		return (-1);
59873b3a8eb9SGleb Smirnoff 	}
59883b3a8eb9SGleb Smirnoff 	sym->val = strdup(val);
59893b3a8eb9SGleb Smirnoff 	if (sym->val == NULL) {
59903b3a8eb9SGleb Smirnoff 		free(sym->nam);
59913b3a8eb9SGleb Smirnoff 		free(sym);
59923b3a8eb9SGleb Smirnoff 		return (-1);
59933b3a8eb9SGleb Smirnoff 	}
59943b3a8eb9SGleb Smirnoff 	sym->used = 0;
59953b3a8eb9SGleb Smirnoff 	sym->persist = persist;
59963b3a8eb9SGleb Smirnoff 	TAILQ_INSERT_TAIL(&symhead, sym, entry);
59973b3a8eb9SGleb Smirnoff 	return (0);
59983b3a8eb9SGleb Smirnoff }
59993b3a8eb9SGleb Smirnoff 
60003b3a8eb9SGleb Smirnoff int
60013b3a8eb9SGleb Smirnoff pfctl_cmdline_symset(char *s)
60023b3a8eb9SGleb Smirnoff {
60033b3a8eb9SGleb Smirnoff 	char	*sym, *val;
60043b3a8eb9SGleb Smirnoff 	int	 ret;
60053b3a8eb9SGleb Smirnoff 
60063b3a8eb9SGleb Smirnoff 	if ((val = strrchr(s, '=')) == NULL)
60073b3a8eb9SGleb Smirnoff 		return (-1);
60083b3a8eb9SGleb Smirnoff 
60093b3a8eb9SGleb Smirnoff 	if ((sym = malloc(strlen(s) - strlen(val) + 1)) == NULL)
60103b3a8eb9SGleb Smirnoff 		err(1, "pfctl_cmdline_symset: malloc");
60113b3a8eb9SGleb Smirnoff 
60123b3a8eb9SGleb Smirnoff 	strlcpy(sym, s, strlen(s) - strlen(val) + 1);
60133b3a8eb9SGleb Smirnoff 
60143b3a8eb9SGleb Smirnoff 	ret = symset(sym, val + 1, 1);
60153b3a8eb9SGleb Smirnoff 	free(sym);
60163b3a8eb9SGleb Smirnoff 
60173b3a8eb9SGleb Smirnoff 	return (ret);
60183b3a8eb9SGleb Smirnoff }
60193b3a8eb9SGleb Smirnoff 
60203b3a8eb9SGleb Smirnoff char *
60213b3a8eb9SGleb Smirnoff symget(const char *nam)
60223b3a8eb9SGleb Smirnoff {
60233b3a8eb9SGleb Smirnoff 	struct sym	*sym;
60243b3a8eb9SGleb Smirnoff 
60253b3a8eb9SGleb Smirnoff 	TAILQ_FOREACH(sym, &symhead, entry)
60263b3a8eb9SGleb Smirnoff 		if (strcmp(nam, sym->nam) == 0) {
60273b3a8eb9SGleb Smirnoff 			sym->used = 1;
60283b3a8eb9SGleb Smirnoff 			return (sym->val);
60293b3a8eb9SGleb Smirnoff 		}
60303b3a8eb9SGleb Smirnoff 	return (NULL);
60313b3a8eb9SGleb Smirnoff }
60323b3a8eb9SGleb Smirnoff 
60333b3a8eb9SGleb Smirnoff void
60343b3a8eb9SGleb Smirnoff mv_rules(struct pf_ruleset *src, struct pf_ruleset *dst)
60353b3a8eb9SGleb Smirnoff {
60363b3a8eb9SGleb Smirnoff 	int i;
60373b3a8eb9SGleb Smirnoff 	struct pf_rule *r;
60383b3a8eb9SGleb Smirnoff 
60393b3a8eb9SGleb Smirnoff 	for (i = 0; i < PF_RULESET_MAX; ++i) {
60403b3a8eb9SGleb Smirnoff 		while ((r = TAILQ_FIRST(src->rules[i].active.ptr))
60413b3a8eb9SGleb Smirnoff 		    != NULL) {
60423b3a8eb9SGleb Smirnoff 			TAILQ_REMOVE(src->rules[i].active.ptr, r, entries);
60433b3a8eb9SGleb Smirnoff 			TAILQ_INSERT_TAIL(dst->rules[i].active.ptr, r, entries);
60443b3a8eb9SGleb Smirnoff 			dst->anchor->match++;
60453b3a8eb9SGleb Smirnoff 		}
60463b3a8eb9SGleb Smirnoff 		src->anchor->match = 0;
60473b3a8eb9SGleb Smirnoff 		while ((r = TAILQ_FIRST(src->rules[i].inactive.ptr))
60483b3a8eb9SGleb Smirnoff 		    != NULL) {
60493b3a8eb9SGleb Smirnoff 			TAILQ_REMOVE(src->rules[i].inactive.ptr, r, entries);
60503b3a8eb9SGleb Smirnoff 			TAILQ_INSERT_TAIL(dst->rules[i].inactive.ptr,
60513b3a8eb9SGleb Smirnoff 				r, entries);
60523b3a8eb9SGleb Smirnoff 		}
60533b3a8eb9SGleb Smirnoff 	}
60543b3a8eb9SGleb Smirnoff }
60553b3a8eb9SGleb Smirnoff 
60563b3a8eb9SGleb Smirnoff void
60573b3a8eb9SGleb Smirnoff decide_address_family(struct node_host *n, sa_family_t *af)
60583b3a8eb9SGleb Smirnoff {
60593b3a8eb9SGleb Smirnoff 	if (*af != 0 || n == NULL)
60603b3a8eb9SGleb Smirnoff 		return;
60613b3a8eb9SGleb Smirnoff 	*af = n->af;
60623b3a8eb9SGleb Smirnoff 	while ((n = n->next) != NULL) {
60633b3a8eb9SGleb Smirnoff 		if (n->af != *af) {
60643b3a8eb9SGleb Smirnoff 			*af = 0;
60653b3a8eb9SGleb Smirnoff 			return;
60663b3a8eb9SGleb Smirnoff 		}
60673b3a8eb9SGleb Smirnoff 	}
60683b3a8eb9SGleb Smirnoff }
60693b3a8eb9SGleb Smirnoff 
60703b3a8eb9SGleb Smirnoff void
60713b3a8eb9SGleb Smirnoff remove_invalid_hosts(struct node_host **nh, sa_family_t *af)
60723b3a8eb9SGleb Smirnoff {
60733b3a8eb9SGleb Smirnoff 	struct node_host	*n = *nh, *prev = NULL;
60743b3a8eb9SGleb Smirnoff 
60753b3a8eb9SGleb Smirnoff 	while (n != NULL) {
60763b3a8eb9SGleb Smirnoff 		if (*af && n->af && n->af != *af) {
60773b3a8eb9SGleb Smirnoff 			/* unlink and free n */
60783b3a8eb9SGleb Smirnoff 			struct node_host *next = n->next;
60793b3a8eb9SGleb Smirnoff 
60803b3a8eb9SGleb Smirnoff 			/* adjust tail pointer */
60813b3a8eb9SGleb Smirnoff 			if (n == (*nh)->tail)
60823b3a8eb9SGleb Smirnoff 				(*nh)->tail = prev;
60833b3a8eb9SGleb Smirnoff 			/* adjust previous node's next pointer */
60843b3a8eb9SGleb Smirnoff 			if (prev == NULL)
60853b3a8eb9SGleb Smirnoff 				*nh = next;
60863b3a8eb9SGleb Smirnoff 			else
60873b3a8eb9SGleb Smirnoff 				prev->next = next;
60883b3a8eb9SGleb Smirnoff 			/* free node */
60893b3a8eb9SGleb Smirnoff 			if (n->ifname != NULL)
60903b3a8eb9SGleb Smirnoff 				free(n->ifname);
60913b3a8eb9SGleb Smirnoff 			free(n);
60923b3a8eb9SGleb Smirnoff 			n = next;
60933b3a8eb9SGleb Smirnoff 		} else {
60943b3a8eb9SGleb Smirnoff 			if (n->af && !*af)
60953b3a8eb9SGleb Smirnoff 				*af = n->af;
60963b3a8eb9SGleb Smirnoff 			prev = n;
60973b3a8eb9SGleb Smirnoff 			n = n->next;
60983b3a8eb9SGleb Smirnoff 		}
60993b3a8eb9SGleb Smirnoff 	}
61003b3a8eb9SGleb Smirnoff }
61013b3a8eb9SGleb Smirnoff 
61023b3a8eb9SGleb Smirnoff int
61033b3a8eb9SGleb Smirnoff invalid_redirect(struct node_host *nh, sa_family_t af)
61043b3a8eb9SGleb Smirnoff {
61053b3a8eb9SGleb Smirnoff 	if (!af) {
61063b3a8eb9SGleb Smirnoff 		struct node_host *n;
61073b3a8eb9SGleb Smirnoff 
61083b3a8eb9SGleb Smirnoff 		/* tables and dyniftl are ok without an address family */
61093b3a8eb9SGleb Smirnoff 		for (n = nh; n != NULL; n = n->next) {
61103b3a8eb9SGleb Smirnoff 			if (n->addr.type != PF_ADDR_TABLE &&
61113b3a8eb9SGleb Smirnoff 			    n->addr.type != PF_ADDR_DYNIFTL) {
61123b3a8eb9SGleb Smirnoff 				yyerror("address family not given and "
61133b3a8eb9SGleb Smirnoff 				    "translation address expands to multiple "
61143b3a8eb9SGleb Smirnoff 				    "address families");
61153b3a8eb9SGleb Smirnoff 				return (1);
61163b3a8eb9SGleb Smirnoff 			}
61173b3a8eb9SGleb Smirnoff 		}
61183b3a8eb9SGleb Smirnoff 	}
61193b3a8eb9SGleb Smirnoff 	if (nh == NULL) {
61203b3a8eb9SGleb Smirnoff 		yyerror("no translation address with matching address family "
61213b3a8eb9SGleb Smirnoff 		    "found.");
61223b3a8eb9SGleb Smirnoff 		return (1);
61233b3a8eb9SGleb Smirnoff 	}
61243b3a8eb9SGleb Smirnoff 	return (0);
61253b3a8eb9SGleb Smirnoff }
61263b3a8eb9SGleb Smirnoff 
61273b3a8eb9SGleb Smirnoff int
61283b3a8eb9SGleb Smirnoff atoul(char *s, u_long *ulvalp)
61293b3a8eb9SGleb Smirnoff {
61303b3a8eb9SGleb Smirnoff 	u_long	 ulval;
61313b3a8eb9SGleb Smirnoff 	char	*ep;
61323b3a8eb9SGleb Smirnoff 
61333b3a8eb9SGleb Smirnoff 	errno = 0;
61343b3a8eb9SGleb Smirnoff 	ulval = strtoul(s, &ep, 0);
61353b3a8eb9SGleb Smirnoff 	if (s[0] == '\0' || *ep != '\0')
61363b3a8eb9SGleb Smirnoff 		return (-1);
61373b3a8eb9SGleb Smirnoff 	if (errno == ERANGE && ulval == ULONG_MAX)
61383b3a8eb9SGleb Smirnoff 		return (-1);
61393b3a8eb9SGleb Smirnoff 	*ulvalp = ulval;
61403b3a8eb9SGleb Smirnoff 	return (0);
61413b3a8eb9SGleb Smirnoff }
61423b3a8eb9SGleb Smirnoff 
61433b3a8eb9SGleb Smirnoff int
61443b3a8eb9SGleb Smirnoff getservice(char *n)
61453b3a8eb9SGleb Smirnoff {
61463b3a8eb9SGleb Smirnoff 	struct servent	*s;
61473b3a8eb9SGleb Smirnoff 	u_long		 ulval;
61483b3a8eb9SGleb Smirnoff 
61493b3a8eb9SGleb Smirnoff 	if (atoul(n, &ulval) == 0) {
61503b3a8eb9SGleb Smirnoff 		if (ulval > 65535) {
61513b3a8eb9SGleb Smirnoff 			yyerror("illegal port value %lu", ulval);
61523b3a8eb9SGleb Smirnoff 			return (-1);
61533b3a8eb9SGleb Smirnoff 		}
61543b3a8eb9SGleb Smirnoff 		return (htons(ulval));
61553b3a8eb9SGleb Smirnoff 	} else {
61563b3a8eb9SGleb Smirnoff 		s = getservbyname(n, "tcp");
61573b3a8eb9SGleb Smirnoff 		if (s == NULL)
61583b3a8eb9SGleb Smirnoff 			s = getservbyname(n, "udp");
61593b3a8eb9SGleb Smirnoff 		if (s == NULL) {
61603b3a8eb9SGleb Smirnoff 			yyerror("unknown port %s", n);
61613b3a8eb9SGleb Smirnoff 			return (-1);
61623b3a8eb9SGleb Smirnoff 		}
61633b3a8eb9SGleb Smirnoff 		return (s->s_port);
61643b3a8eb9SGleb Smirnoff 	}
61653b3a8eb9SGleb Smirnoff }
61663b3a8eb9SGleb Smirnoff 
61673b3a8eb9SGleb Smirnoff int
61683b3a8eb9SGleb Smirnoff rule_label(struct pf_rule *r, char *s)
61693b3a8eb9SGleb Smirnoff {
61703b3a8eb9SGleb Smirnoff 	if (s) {
61713b3a8eb9SGleb Smirnoff 		if (strlcpy(r->label, s, sizeof(r->label)) >=
61723b3a8eb9SGleb Smirnoff 		    sizeof(r->label)) {
61733b3a8eb9SGleb Smirnoff 			yyerror("rule label too long (max %d chars)",
61743b3a8eb9SGleb Smirnoff 			    sizeof(r->label)-1);
61753b3a8eb9SGleb Smirnoff 			return (-1);
61763b3a8eb9SGleb Smirnoff 		}
61773b3a8eb9SGleb Smirnoff 	}
61783b3a8eb9SGleb Smirnoff 	return (0);
61793b3a8eb9SGleb Smirnoff }
61803b3a8eb9SGleb Smirnoff 
61813b3a8eb9SGleb Smirnoff u_int16_t
61823b3a8eb9SGleb Smirnoff parseicmpspec(char *w, sa_family_t af)
61833b3a8eb9SGleb Smirnoff {
61843b3a8eb9SGleb Smirnoff 	const struct icmpcodeent	*p;
61853b3a8eb9SGleb Smirnoff 	u_long				 ulval;
61863b3a8eb9SGleb Smirnoff 	u_int8_t			 icmptype;
61873b3a8eb9SGleb Smirnoff 
61883b3a8eb9SGleb Smirnoff 	if (af == AF_INET)
61893b3a8eb9SGleb Smirnoff 		icmptype = returnicmpdefault >> 8;
61903b3a8eb9SGleb Smirnoff 	else
61913b3a8eb9SGleb Smirnoff 		icmptype = returnicmp6default >> 8;
61923b3a8eb9SGleb Smirnoff 
61933b3a8eb9SGleb Smirnoff 	if (atoul(w, &ulval) == -1) {
61943b3a8eb9SGleb Smirnoff 		if ((p = geticmpcodebyname(icmptype, w, af)) == NULL) {
61953b3a8eb9SGleb Smirnoff 			yyerror("unknown icmp code %s", w);
61963b3a8eb9SGleb Smirnoff 			return (0);
61973b3a8eb9SGleb Smirnoff 		}
61983b3a8eb9SGleb Smirnoff 		ulval = p->code;
61993b3a8eb9SGleb Smirnoff 	}
62003b3a8eb9SGleb Smirnoff 	if (ulval > 255) {
62013b3a8eb9SGleb Smirnoff 		yyerror("invalid icmp code %lu", ulval);
62023b3a8eb9SGleb Smirnoff 		return (0);
62033b3a8eb9SGleb Smirnoff 	}
62043b3a8eb9SGleb Smirnoff 	return (icmptype << 8 | ulval);
62053b3a8eb9SGleb Smirnoff }
62063b3a8eb9SGleb Smirnoff 
62073b3a8eb9SGleb Smirnoff int
62083b3a8eb9SGleb Smirnoff parseport(char *port, struct range *r, int extensions)
62093b3a8eb9SGleb Smirnoff {
62103b3a8eb9SGleb Smirnoff 	char	*p = strchr(port, ':');
62113b3a8eb9SGleb Smirnoff 
62123b3a8eb9SGleb Smirnoff 	if (p == NULL) {
62133b3a8eb9SGleb Smirnoff 		if ((r->a = getservice(port)) == -1)
62143b3a8eb9SGleb Smirnoff 			return (-1);
62153b3a8eb9SGleb Smirnoff 		r->b = 0;
62163b3a8eb9SGleb Smirnoff 		r->t = PF_OP_NONE;
62173b3a8eb9SGleb Smirnoff 		return (0);
62183b3a8eb9SGleb Smirnoff 	}
62193b3a8eb9SGleb Smirnoff 	if ((extensions & PPORT_STAR) && !strcmp(p+1, "*")) {
62203b3a8eb9SGleb Smirnoff 		*p = 0;
62213b3a8eb9SGleb Smirnoff 		if ((r->a = getservice(port)) == -1)
62223b3a8eb9SGleb Smirnoff 			return (-1);
62233b3a8eb9SGleb Smirnoff 		r->b = 0;
62243b3a8eb9SGleb Smirnoff 		r->t = PF_OP_IRG;
62253b3a8eb9SGleb Smirnoff 		return (0);
62263b3a8eb9SGleb Smirnoff 	}
62273b3a8eb9SGleb Smirnoff 	if ((extensions & PPORT_RANGE)) {
62283b3a8eb9SGleb Smirnoff 		*p++ = 0;
62293b3a8eb9SGleb Smirnoff 		if ((r->a = getservice(port)) == -1 ||
62303b3a8eb9SGleb Smirnoff 		    (r->b = getservice(p)) == -1)
62313b3a8eb9SGleb Smirnoff 			return (-1);
62323b3a8eb9SGleb Smirnoff 		if (r->a == r->b) {
62333b3a8eb9SGleb Smirnoff 			r->b = 0;
62343b3a8eb9SGleb Smirnoff 			r->t = PF_OP_NONE;
62353b3a8eb9SGleb Smirnoff 		} else
62363b3a8eb9SGleb Smirnoff 			r->t = PF_OP_RRG;
62373b3a8eb9SGleb Smirnoff 		return (0);
62383b3a8eb9SGleb Smirnoff 	}
62393b3a8eb9SGleb Smirnoff 	return (-1);
62403b3a8eb9SGleb Smirnoff }
62413b3a8eb9SGleb Smirnoff 
62423b3a8eb9SGleb Smirnoff int
62433b3a8eb9SGleb Smirnoff pfctl_load_anchors(int dev, struct pfctl *pf, struct pfr_buffer *trans)
62443b3a8eb9SGleb Smirnoff {
62453b3a8eb9SGleb Smirnoff 	struct loadanchors	*la;
62463b3a8eb9SGleb Smirnoff 
62473b3a8eb9SGleb Smirnoff 	TAILQ_FOREACH(la, &loadanchorshead, entries) {
62483b3a8eb9SGleb Smirnoff 		if (pf->opts & PF_OPT_VERBOSE)
62493b3a8eb9SGleb Smirnoff 			fprintf(stderr, "\nLoading anchor %s from %s\n",
62503b3a8eb9SGleb Smirnoff 			    la->anchorname, la->filename);
62513b3a8eb9SGleb Smirnoff 		if (pfctl_rules(dev, la->filename, pf->opts, pf->optimize,
62523b3a8eb9SGleb Smirnoff 		    la->anchorname, trans) == -1)
62533b3a8eb9SGleb Smirnoff 			return (-1);
62543b3a8eb9SGleb Smirnoff 	}
62553b3a8eb9SGleb Smirnoff 
62563b3a8eb9SGleb Smirnoff 	return (0);
62573b3a8eb9SGleb Smirnoff }
62583b3a8eb9SGleb Smirnoff 
62593b3a8eb9SGleb Smirnoff int
62601f495578SKristof Provost kw_casecmp(const void *k, const void *e)
62611f495578SKristof Provost {
62621f495578SKristof Provost 	return (strcasecmp(k, ((const struct keywords *)e)->k_name));
62631f495578SKristof Provost }
62641f495578SKristof Provost 
62651f495578SKristof Provost int
62661f495578SKristof Provost map_tos(char *s, int *val)
62671f495578SKristof Provost {
62681f495578SKristof Provost 	/* DiffServ Codepoints and other TOS mappings */
62691f495578SKristof Provost 	const struct keywords	 toswords[] = {
62701f495578SKristof Provost 		{ "af11",		IPTOS_DSCP_AF11 },
62711f495578SKristof Provost 		{ "af12",		IPTOS_DSCP_AF12 },
62721f495578SKristof Provost 		{ "af13",		IPTOS_DSCP_AF13 },
62731f495578SKristof Provost 		{ "af21",		IPTOS_DSCP_AF21 },
62741f495578SKristof Provost 		{ "af22",		IPTOS_DSCP_AF22 },
62751f495578SKristof Provost 		{ "af23",		IPTOS_DSCP_AF23 },
62761f495578SKristof Provost 		{ "af31",		IPTOS_DSCP_AF31 },
62771f495578SKristof Provost 		{ "af32",		IPTOS_DSCP_AF32 },
62781f495578SKristof Provost 		{ "af33",		IPTOS_DSCP_AF33 },
62791f495578SKristof Provost 		{ "af41",		IPTOS_DSCP_AF41 },
62801f495578SKristof Provost 		{ "af42",		IPTOS_DSCP_AF42 },
62811f495578SKristof Provost 		{ "af43",		IPTOS_DSCP_AF43 },
62821f495578SKristof Provost 		{ "critical",		IPTOS_PREC_CRITIC_ECP },
62831f495578SKristof Provost 		{ "cs0",		IPTOS_DSCP_CS0 },
62841f495578SKristof Provost 		{ "cs1",		IPTOS_DSCP_CS1 },
62851f495578SKristof Provost 		{ "cs2",		IPTOS_DSCP_CS2 },
62861f495578SKristof Provost 		{ "cs3",		IPTOS_DSCP_CS3 },
62871f495578SKristof Provost 		{ "cs4",		IPTOS_DSCP_CS4 },
62881f495578SKristof Provost 		{ "cs5",		IPTOS_DSCP_CS5 },
62891f495578SKristof Provost 		{ "cs6",		IPTOS_DSCP_CS6 },
62901f495578SKristof Provost 		{ "cs7",		IPTOS_DSCP_CS7 },
62911f495578SKristof Provost 		{ "ef",			IPTOS_DSCP_EF },
62921f495578SKristof Provost 		{ "inetcontrol",	IPTOS_PREC_INTERNETCONTROL },
62931f495578SKristof Provost 		{ "lowdelay",		IPTOS_LOWDELAY },
62941f495578SKristof Provost 		{ "netcontrol",		IPTOS_PREC_NETCONTROL },
62951f495578SKristof Provost 		{ "reliability",	IPTOS_RELIABILITY },
62961f495578SKristof Provost 		{ "throughput",		IPTOS_THROUGHPUT }
62971f495578SKristof Provost 	};
62981f495578SKristof Provost 	const struct keywords	*p;
62991f495578SKristof Provost 
63001f495578SKristof Provost 	p = bsearch(s, toswords, sizeof(toswords)/sizeof(toswords[0]),
63011f495578SKristof Provost 	    sizeof(toswords[0]), kw_casecmp);
63021f495578SKristof Provost 
63031f495578SKristof Provost 	if (p) {
63041f495578SKristof Provost 		*val = p->k_val;
63051f495578SKristof Provost 		return (1);
63061f495578SKristof Provost 	}
63071f495578SKristof Provost 	return (0);
63081f495578SKristof Provost }
63091f495578SKristof Provost 
63101f495578SKristof Provost int
63113b3a8eb9SGleb Smirnoff rt_tableid_max(void)
63123b3a8eb9SGleb Smirnoff {
63133b3a8eb9SGleb Smirnoff #ifdef __FreeBSD__
63143b3a8eb9SGleb Smirnoff 	int fibs;
63153b3a8eb9SGleb Smirnoff 	size_t l = sizeof(fibs);
63163b3a8eb9SGleb Smirnoff 
63173b3a8eb9SGleb Smirnoff         if (sysctlbyname("net.fibs", &fibs, &l, NULL, 0) == -1)
63183b3a8eb9SGleb Smirnoff 		fibs = 16;	/* XXX RT_MAXFIBS, at least limit it some. */
63193b3a8eb9SGleb Smirnoff 	/*
63203b3a8eb9SGleb Smirnoff 	 * As the OpenBSD code only compares > and not >= we need to adjust
63213b3a8eb9SGleb Smirnoff 	 * here given we only accept values of 0..n and want to avoid #ifdefs
6322b68ac800SPedro F. Giffuni 	 * in the grammar.
63233b3a8eb9SGleb Smirnoff 	 */
63243b3a8eb9SGleb Smirnoff 	return (fibs - 1);
63253b3a8eb9SGleb Smirnoff #else
63263b3a8eb9SGleb Smirnoff 	return (RT_TABLEID_MAX);
63273b3a8eb9SGleb Smirnoff #endif
63283b3a8eb9SGleb Smirnoff }
6329