xref: /freebsd/sbin/pfctl/parse.y (revision 3468cd95ca1249946bace931f2035e76b070711e)
13b3a8eb9SGleb Smirnoff /*	$OpenBSD: parse.y,v 1.554 2008/10/17 12:59:53 henning Exp $	*/
23b3a8eb9SGleb Smirnoff 
31de7b4b8SPedro F. Giffuni /*-
41de7b4b8SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause
51de7b4b8SPedro F. Giffuni  *
63b3a8eb9SGleb Smirnoff  * Copyright (c) 2001 Markus Friedl.  All rights reserved.
73b3a8eb9SGleb Smirnoff  * Copyright (c) 2001 Daniel Hartmeier.  All rights reserved.
83b3a8eb9SGleb Smirnoff  * Copyright (c) 2001 Theo de Raadt.  All rights reserved.
93b3a8eb9SGleb Smirnoff  * Copyright (c) 2002,2003 Henning Brauer. All rights reserved.
103b3a8eb9SGleb Smirnoff  *
113b3a8eb9SGleb Smirnoff  * Redistribution and use in source and binary forms, with or without
123b3a8eb9SGleb Smirnoff  * modification, are permitted provided that the following conditions
133b3a8eb9SGleb Smirnoff  * are met:
143b3a8eb9SGleb Smirnoff  * 1. Redistributions of source code must retain the above copyright
153b3a8eb9SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer.
163b3a8eb9SGleb Smirnoff  * 2. Redistributions in binary form must reproduce the above copyright
173b3a8eb9SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer in the
183b3a8eb9SGleb Smirnoff  *    documentation and/or other materials provided with the distribution.
193b3a8eb9SGleb Smirnoff  *
203b3a8eb9SGleb Smirnoff  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
213b3a8eb9SGleb Smirnoff  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
223b3a8eb9SGleb Smirnoff  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
233b3a8eb9SGleb Smirnoff  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
243b3a8eb9SGleb Smirnoff  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
253b3a8eb9SGleb Smirnoff  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
263b3a8eb9SGleb Smirnoff  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
273b3a8eb9SGleb Smirnoff  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
283b3a8eb9SGleb Smirnoff  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
293b3a8eb9SGleb Smirnoff  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
303b3a8eb9SGleb Smirnoff  */
313b3a8eb9SGleb Smirnoff %{
323b3a8eb9SGleb Smirnoff #include <sys/cdefs.h>
333b3a8eb9SGleb Smirnoff __FBSDID("$FreeBSD$");
343b3a8eb9SGleb Smirnoff 
35249cc75fSPatrick Kelsey #define PFIOC_USE_LATEST
36249cc75fSPatrick Kelsey 
373b3a8eb9SGleb Smirnoff #include <sys/types.h>
383b3a8eb9SGleb Smirnoff #include <sys/socket.h>
393b3a8eb9SGleb Smirnoff #include <sys/stat.h>
403b3a8eb9SGleb Smirnoff #ifdef __FreeBSD__
413b3a8eb9SGleb Smirnoff #include <sys/sysctl.h>
423b3a8eb9SGleb Smirnoff #endif
433b3a8eb9SGleb Smirnoff #include <net/if.h>
443b3a8eb9SGleb Smirnoff #include <netinet/in.h>
453b3a8eb9SGleb Smirnoff #include <netinet/in_systm.h>
463b3a8eb9SGleb Smirnoff #include <netinet/ip.h>
473b3a8eb9SGleb Smirnoff #include <netinet/ip_icmp.h>
483b3a8eb9SGleb Smirnoff #include <netinet/icmp6.h>
493b3a8eb9SGleb Smirnoff #include <net/pfvar.h>
503b3a8eb9SGleb Smirnoff #include <arpa/inet.h>
51772e66a6SGleb Smirnoff #include <net/altq/altq.h>
52772e66a6SGleb Smirnoff #include <net/altq/altq_cbq.h>
530a70aaf8SLuiz Otavio O Souza #include <net/altq/altq_codel.h>
54772e66a6SGleb Smirnoff #include <net/altq/altq_priq.h>
55772e66a6SGleb Smirnoff #include <net/altq/altq_hfsc.h>
56a5b789f6SErmal Luçi #include <net/altq/altq_fairq.h>
573b3a8eb9SGleb Smirnoff 
586fcc8e04SKristof Provost #include <assert.h>
593b3a8eb9SGleb Smirnoff #include <stdio.h>
603b3a8eb9SGleb Smirnoff #include <unistd.h>
613b3a8eb9SGleb Smirnoff #include <stdlib.h>
623b3a8eb9SGleb Smirnoff #include <netdb.h>
633b3a8eb9SGleb Smirnoff #include <stdarg.h>
643b3a8eb9SGleb Smirnoff #include <errno.h>
653b3a8eb9SGleb Smirnoff #include <string.h>
663b3a8eb9SGleb Smirnoff #include <ctype.h>
673b3a8eb9SGleb Smirnoff #include <math.h>
683b3a8eb9SGleb Smirnoff #include <err.h>
693b3a8eb9SGleb Smirnoff #include <limits.h>
703b3a8eb9SGleb Smirnoff #include <pwd.h>
713b3a8eb9SGleb Smirnoff #include <grp.h>
723b3a8eb9SGleb Smirnoff #include <md5.h>
733b3a8eb9SGleb Smirnoff 
743b3a8eb9SGleb Smirnoff #include "pfctl_parser.h"
753b3a8eb9SGleb Smirnoff #include "pfctl.h"
763b3a8eb9SGleb Smirnoff 
773b3a8eb9SGleb Smirnoff static struct pfctl	*pf = NULL;
783b3a8eb9SGleb Smirnoff static int		 debug = 0;
793b3a8eb9SGleb Smirnoff static int		 rulestate = 0;
803b3a8eb9SGleb Smirnoff static u_int16_t	 returnicmpdefault =
813b3a8eb9SGleb Smirnoff 			    (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
823b3a8eb9SGleb Smirnoff static u_int16_t	 returnicmp6default =
833b3a8eb9SGleb Smirnoff 			    (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
843b3a8eb9SGleb Smirnoff static int		 blockpolicy = PFRULE_DROP;
85150182e3SKristof Provost static int		 failpolicy = PFRULE_DROP;
863b3a8eb9SGleb Smirnoff static int		 require_order = 1;
873b3a8eb9SGleb Smirnoff static int		 default_statelock;
883b3a8eb9SGleb Smirnoff 
8913cfafabSKristof Provost static TAILQ_HEAD(files, file)	 files = TAILQ_HEAD_INITIALIZER(files);
903b3a8eb9SGleb Smirnoff static struct file {
913b3a8eb9SGleb Smirnoff 	TAILQ_ENTRY(file)	 entry;
923b3a8eb9SGleb Smirnoff 	FILE			*stream;
933b3a8eb9SGleb Smirnoff 	char			*name;
943b3a8eb9SGleb Smirnoff 	int			 lineno;
953b3a8eb9SGleb Smirnoff 	int			 errors;
963b3a8eb9SGleb Smirnoff } *file;
973b3a8eb9SGleb Smirnoff struct file	*pushfile(const char *, int);
983b3a8eb9SGleb Smirnoff int		 popfile(void);
993b3a8eb9SGleb Smirnoff int		 check_file_secrecy(int, const char *);
1003b3a8eb9SGleb Smirnoff int		 yyparse(void);
1013b3a8eb9SGleb Smirnoff int		 yylex(void);
1023b3a8eb9SGleb Smirnoff int		 yyerror(const char *, ...);
1033b3a8eb9SGleb Smirnoff int		 kw_cmp(const void *, const void *);
1043b3a8eb9SGleb Smirnoff int		 lookup(char *);
1053b3a8eb9SGleb Smirnoff int		 lgetc(int);
1063b3a8eb9SGleb Smirnoff int		 lungetc(int);
1073b3a8eb9SGleb Smirnoff int		 findeol(void);
1083b3a8eb9SGleb Smirnoff 
10913cfafabSKristof Provost static TAILQ_HEAD(symhead, sym)	 symhead = TAILQ_HEAD_INITIALIZER(symhead);
1103b3a8eb9SGleb Smirnoff struct sym {
1113b3a8eb9SGleb Smirnoff 	TAILQ_ENTRY(sym)	 entry;
1123b3a8eb9SGleb Smirnoff 	int			 used;
1133b3a8eb9SGleb Smirnoff 	int			 persist;
1143b3a8eb9SGleb Smirnoff 	char			*nam;
1153b3a8eb9SGleb Smirnoff 	char			*val;
1163b3a8eb9SGleb Smirnoff };
1173b3a8eb9SGleb Smirnoff int		 symset(const char *, const char *, int);
1183b3a8eb9SGleb Smirnoff char		*symget(const char *);
1193b3a8eb9SGleb Smirnoff 
1203b3a8eb9SGleb Smirnoff int		 atoul(char *, u_long *);
1213b3a8eb9SGleb Smirnoff 
1223b3a8eb9SGleb Smirnoff enum {
1233b3a8eb9SGleb Smirnoff 	PFCTL_STATE_NONE,
1243b3a8eb9SGleb Smirnoff 	PFCTL_STATE_OPTION,
1252b29ceb8SKristof Provost 	PFCTL_STATE_ETHER,
1263b3a8eb9SGleb Smirnoff 	PFCTL_STATE_SCRUB,
1273b3a8eb9SGleb Smirnoff 	PFCTL_STATE_QUEUE,
1283b3a8eb9SGleb Smirnoff 	PFCTL_STATE_NAT,
1293b3a8eb9SGleb Smirnoff 	PFCTL_STATE_FILTER
1303b3a8eb9SGleb Smirnoff };
1313b3a8eb9SGleb Smirnoff 
1322b29ceb8SKristof Provost struct node_etherproto {
1332b29ceb8SKristof Provost 	u_int16_t		 proto;
1342b29ceb8SKristof Provost 	struct node_etherproto	*next;
1352b29ceb8SKristof Provost 	struct node_etherproto	*tail;
1362b29ceb8SKristof Provost };
1372b29ceb8SKristof Provost 
1383b3a8eb9SGleb Smirnoff struct node_proto {
1393b3a8eb9SGleb Smirnoff 	u_int8_t		 proto;
1403b3a8eb9SGleb Smirnoff 	struct node_proto	*next;
1413b3a8eb9SGleb Smirnoff 	struct node_proto	*tail;
1423b3a8eb9SGleb Smirnoff };
1433b3a8eb9SGleb Smirnoff 
1443b3a8eb9SGleb Smirnoff struct node_port {
1453b3a8eb9SGleb Smirnoff 	u_int16_t		 port[2];
1463b3a8eb9SGleb Smirnoff 	u_int8_t		 op;
1473b3a8eb9SGleb Smirnoff 	struct node_port	*next;
1483b3a8eb9SGleb Smirnoff 	struct node_port	*tail;
1493b3a8eb9SGleb Smirnoff };
1503b3a8eb9SGleb Smirnoff 
1513b3a8eb9SGleb Smirnoff struct node_uid {
1523b3a8eb9SGleb Smirnoff 	uid_t			 uid[2];
1533b3a8eb9SGleb Smirnoff 	u_int8_t		 op;
1543b3a8eb9SGleb Smirnoff 	struct node_uid		*next;
1553b3a8eb9SGleb Smirnoff 	struct node_uid		*tail;
1563b3a8eb9SGleb Smirnoff };
1573b3a8eb9SGleb Smirnoff 
1583b3a8eb9SGleb Smirnoff struct node_gid {
1593b3a8eb9SGleb Smirnoff 	gid_t			 gid[2];
1603b3a8eb9SGleb Smirnoff 	u_int8_t		 op;
1613b3a8eb9SGleb Smirnoff 	struct node_gid		*next;
1623b3a8eb9SGleb Smirnoff 	struct node_gid		*tail;
1633b3a8eb9SGleb Smirnoff };
1643b3a8eb9SGleb Smirnoff 
1653b3a8eb9SGleb Smirnoff struct node_icmp {
1663b3a8eb9SGleb Smirnoff 	u_int8_t		 code;
1673b3a8eb9SGleb Smirnoff 	u_int8_t		 type;
1683b3a8eb9SGleb Smirnoff 	u_int8_t		 proto;
1693b3a8eb9SGleb Smirnoff 	struct node_icmp	*next;
1703b3a8eb9SGleb Smirnoff 	struct node_icmp	*tail;
1713b3a8eb9SGleb Smirnoff };
1723b3a8eb9SGleb Smirnoff 
1733b3a8eb9SGleb Smirnoff enum	{ PF_STATE_OPT_MAX, PF_STATE_OPT_NOSYNC, PF_STATE_OPT_SRCTRACK,
1743b3a8eb9SGleb Smirnoff 	    PF_STATE_OPT_MAX_SRC_STATES, PF_STATE_OPT_MAX_SRC_CONN,
1753b3a8eb9SGleb Smirnoff 	    PF_STATE_OPT_MAX_SRC_CONN_RATE, PF_STATE_OPT_MAX_SRC_NODES,
1763b3a8eb9SGleb Smirnoff 	    PF_STATE_OPT_OVERLOAD, PF_STATE_OPT_STATELOCK,
1773b3a8eb9SGleb Smirnoff 	    PF_STATE_OPT_TIMEOUT, PF_STATE_OPT_SLOPPY, };
1783b3a8eb9SGleb Smirnoff 
1793b3a8eb9SGleb Smirnoff enum	{ PF_SRCTRACK_NONE, PF_SRCTRACK, PF_SRCTRACK_GLOBAL, PF_SRCTRACK_RULE };
1803b3a8eb9SGleb Smirnoff 
1813b3a8eb9SGleb Smirnoff struct node_state_opt {
1823b3a8eb9SGleb Smirnoff 	int			 type;
1833b3a8eb9SGleb Smirnoff 	union {
1843b3a8eb9SGleb Smirnoff 		u_int32_t	 max_states;
1853b3a8eb9SGleb Smirnoff 		u_int32_t	 max_src_states;
1863b3a8eb9SGleb Smirnoff 		u_int32_t	 max_src_conn;
1873b3a8eb9SGleb Smirnoff 		struct {
1883b3a8eb9SGleb Smirnoff 			u_int32_t	limit;
1893b3a8eb9SGleb Smirnoff 			u_int32_t	seconds;
1903b3a8eb9SGleb Smirnoff 		}		 max_src_conn_rate;
1913b3a8eb9SGleb Smirnoff 		struct {
1923b3a8eb9SGleb Smirnoff 			u_int8_t	flush;
1933b3a8eb9SGleb Smirnoff 			char		tblname[PF_TABLE_NAME_SIZE];
1943b3a8eb9SGleb Smirnoff 		}		 overload;
1953b3a8eb9SGleb Smirnoff 		u_int32_t	 max_src_nodes;
1963b3a8eb9SGleb Smirnoff 		u_int8_t	 src_track;
1973b3a8eb9SGleb Smirnoff 		u_int32_t	 statelock;
1983b3a8eb9SGleb Smirnoff 		struct {
1993b3a8eb9SGleb Smirnoff 			int		number;
2003b3a8eb9SGleb Smirnoff 			u_int32_t	seconds;
2013b3a8eb9SGleb Smirnoff 		}		 timeout;
2023b3a8eb9SGleb Smirnoff 	}			 data;
2033b3a8eb9SGleb Smirnoff 	struct node_state_opt	*next;
2043b3a8eb9SGleb Smirnoff 	struct node_state_opt	*tail;
2053b3a8eb9SGleb Smirnoff };
2063b3a8eb9SGleb Smirnoff 
2073b3a8eb9SGleb Smirnoff struct peer {
2083b3a8eb9SGleb Smirnoff 	struct node_host	*host;
2093b3a8eb9SGleb Smirnoff 	struct node_port	*port;
2103b3a8eb9SGleb Smirnoff };
2113b3a8eb9SGleb Smirnoff 
21213cfafabSKristof Provost static struct node_queue {
2133b3a8eb9SGleb Smirnoff 	char			 queue[PF_QNAME_SIZE];
2143b3a8eb9SGleb Smirnoff 	char			 parent[PF_QNAME_SIZE];
2153b3a8eb9SGleb Smirnoff 	char			 ifname[IFNAMSIZ];
2163b3a8eb9SGleb Smirnoff 	int			 scheduler;
2173b3a8eb9SGleb Smirnoff 	struct node_queue	*next;
2183b3a8eb9SGleb Smirnoff 	struct node_queue	*tail;
2193b3a8eb9SGleb Smirnoff }	*queues = NULL;
2203b3a8eb9SGleb Smirnoff 
2213b3a8eb9SGleb Smirnoff struct node_qassign {
2223b3a8eb9SGleb Smirnoff 	char		*qname;
2233b3a8eb9SGleb Smirnoff 	char		*pqname;
2243b3a8eb9SGleb Smirnoff };
2253b3a8eb9SGleb Smirnoff 
22613cfafabSKristof Provost static struct filter_opts {
2273b3a8eb9SGleb Smirnoff 	int			 marker;
2283b3a8eb9SGleb Smirnoff #define FOM_FLAGS	0x01
2293b3a8eb9SGleb Smirnoff #define FOM_ICMP	0x02
2303b3a8eb9SGleb Smirnoff #define FOM_TOS		0x04
2313b3a8eb9SGleb Smirnoff #define FOM_KEEP	0x08
2323b3a8eb9SGleb Smirnoff #define FOM_SRCTRACK	0x10
2333e248e0fSKristof Provost #define FOM_SETPRIO	0x0400
2343e248e0fSKristof Provost #define FOM_PRIO	0x2000
2353b3a8eb9SGleb Smirnoff 	struct node_uid		*uid;
2363b3a8eb9SGleb Smirnoff 	struct node_gid		*gid;
2373b3a8eb9SGleb Smirnoff 	struct {
2383b3a8eb9SGleb Smirnoff 		u_int8_t	 b1;
2393b3a8eb9SGleb Smirnoff 		u_int8_t	 b2;
2403b3a8eb9SGleb Smirnoff 		u_int16_t	 w;
2413b3a8eb9SGleb Smirnoff 		u_int16_t	 w2;
2423b3a8eb9SGleb Smirnoff 	} flags;
2433b3a8eb9SGleb Smirnoff 	struct node_icmp	*icmpspec;
2443b3a8eb9SGleb Smirnoff 	u_int32_t		 tos;
2453b3a8eb9SGleb Smirnoff 	u_int32_t		 prob;
24676c5eeccSKristof Provost 	u_int32_t		 ridentifier;
2473b3a8eb9SGleb Smirnoff 	struct {
2483b3a8eb9SGleb Smirnoff 		int			 action;
2493b3a8eb9SGleb Smirnoff 		struct node_state_opt	*options;
2503b3a8eb9SGleb Smirnoff 	} keep;
2513b3a8eb9SGleb Smirnoff 	int			 fragment;
2523b3a8eb9SGleb Smirnoff 	int			 allowopts;
2536fcc8e04SKristof Provost 	char			*label[PF_RULE_MAX_LABEL_COUNT];
2546fcc8e04SKristof Provost 	int			 labelcount;
2553b3a8eb9SGleb Smirnoff 	struct node_qassign	 queues;
2563b3a8eb9SGleb Smirnoff 	char			*tag;
2573b3a8eb9SGleb Smirnoff 	char			*match_tag;
2583b3a8eb9SGleb Smirnoff 	u_int8_t		 match_tag_not;
25963b3c1c7SKristof Provost 	u_int16_t		 dnpipe;
26063b3c1c7SKristof Provost 	u_int16_t		 dnrpipe;
26163b3c1c7SKristof Provost 	u_int32_t		 free_flags;
2623b3a8eb9SGleb Smirnoff 	u_int			 rtableid;
2633e248e0fSKristof Provost 	u_int8_t		 prio;
2643e248e0fSKristof Provost 	u_int8_t		 set_prio[2];
2653b3a8eb9SGleb Smirnoff 	struct {
2663b3a8eb9SGleb Smirnoff 		struct node_host	*addr;
2673b3a8eb9SGleb Smirnoff 		u_int16_t		port;
2683b3a8eb9SGleb Smirnoff 	}			 divert;
2693b3a8eb9SGleb Smirnoff } filter_opts;
2703b3a8eb9SGleb Smirnoff 
27113cfafabSKristof Provost static struct antispoof_opts {
2726fcc8e04SKristof Provost 	char			*label[PF_RULE_MAX_LABEL_COUNT];
2736fcc8e04SKristof Provost 	int			 labelcount;
27476c5eeccSKristof Provost 	u_int32_t		 ridentifier;
2753b3a8eb9SGleb Smirnoff 	u_int			 rtableid;
2763b3a8eb9SGleb Smirnoff } antispoof_opts;
2773b3a8eb9SGleb Smirnoff 
27813cfafabSKristof Provost static struct scrub_opts {
2793b3a8eb9SGleb Smirnoff 	int			 marker;
2803b3a8eb9SGleb Smirnoff #define SOM_MINTTL	0x01
2813b3a8eb9SGleb Smirnoff #define SOM_MAXMSS	0x02
2823b3a8eb9SGleb Smirnoff #define SOM_FRAGCACHE	0x04
2833b3a8eb9SGleb Smirnoff #define SOM_SETTOS	0x08
2843b3a8eb9SGleb Smirnoff 	int			 nodf;
2853b3a8eb9SGleb Smirnoff 	int			 minttl;
2863b3a8eb9SGleb Smirnoff 	int			 maxmss;
2873b3a8eb9SGleb Smirnoff 	int			 settos;
2883b3a8eb9SGleb Smirnoff 	int			 fragcache;
2893b3a8eb9SGleb Smirnoff 	int			 randomid;
2903b3a8eb9SGleb Smirnoff 	int			 reassemble_tcp;
2913b3a8eb9SGleb Smirnoff 	char			*match_tag;
2923b3a8eb9SGleb Smirnoff 	u_int8_t		 match_tag_not;
2933b3a8eb9SGleb Smirnoff 	u_int			 rtableid;
2943b3a8eb9SGleb Smirnoff } scrub_opts;
2953b3a8eb9SGleb Smirnoff 
29613cfafabSKristof Provost static struct queue_opts {
2973b3a8eb9SGleb Smirnoff 	int			marker;
2983b3a8eb9SGleb Smirnoff #define QOM_BWSPEC	0x01
2993b3a8eb9SGleb Smirnoff #define QOM_SCHEDULER	0x02
3003b3a8eb9SGleb Smirnoff #define QOM_PRIORITY	0x04
3013b3a8eb9SGleb Smirnoff #define QOM_TBRSIZE	0x08
3023b3a8eb9SGleb Smirnoff #define QOM_QLIMIT	0x10
3033b3a8eb9SGleb Smirnoff 	struct node_queue_bw	queue_bwspec;
3043b3a8eb9SGleb Smirnoff 	struct node_queue_opt	scheduler;
3053b3a8eb9SGleb Smirnoff 	int			priority;
306249cc75fSPatrick Kelsey 	unsigned int		tbrsize;
3073b3a8eb9SGleb Smirnoff 	int			qlimit;
3083b3a8eb9SGleb Smirnoff } queue_opts;
3093b3a8eb9SGleb Smirnoff 
31013cfafabSKristof Provost static struct table_opts {
3113b3a8eb9SGleb Smirnoff 	int			flags;
3123b3a8eb9SGleb Smirnoff 	int			init_addr;
3133b3a8eb9SGleb Smirnoff 	struct node_tinithead	init_nodes;
3143b3a8eb9SGleb Smirnoff } table_opts;
3153b3a8eb9SGleb Smirnoff 
31613cfafabSKristof Provost static struct pool_opts {
3173b3a8eb9SGleb Smirnoff 	int			 marker;
3183b3a8eb9SGleb Smirnoff #define POM_TYPE		0x01
3193b3a8eb9SGleb Smirnoff #define POM_STICKYADDRESS	0x02
3203b3a8eb9SGleb Smirnoff 	u_int8_t		 opts;
3213b3a8eb9SGleb Smirnoff 	int			 type;
3223b3a8eb9SGleb Smirnoff 	int			 staticport;
3233b3a8eb9SGleb Smirnoff 	struct pf_poolhashkey	*key;
3242aa21096SKurosawa Takahiro 	struct pf_mape_portset	 mape;
3253b3a8eb9SGleb Smirnoff 
3263b3a8eb9SGleb Smirnoff } pool_opts;
3273b3a8eb9SGleb Smirnoff 
32813cfafabSKristof Provost static struct codel_opts	 codel_opts;
32913cfafabSKristof Provost static struct node_hfsc_opts	 hfsc_opts;
33013cfafabSKristof Provost static struct node_fairq_opts	 fairq_opts;
33113cfafabSKristof Provost static struct node_state_opt	*keep_state_defaults = NULL;
3325062afffSKristof Provost static struct pfctl_watermarks	 syncookie_opts;
3333b3a8eb9SGleb Smirnoff 
3343b3a8eb9SGleb Smirnoff int		 disallow_table(struct node_host *, const char *);
3353b3a8eb9SGleb Smirnoff int		 disallow_urpf_failed(struct node_host *, const char *);
3363b3a8eb9SGleb Smirnoff int		 disallow_alias(struct node_host *, const char *);
337e9eb0941SKristof Provost int		 rule_consistent(struct pfctl_rule *, int);
338e9eb0941SKristof Provost int		 filter_consistent(struct pfctl_rule *, int);
339e9eb0941SKristof Provost int		 nat_consistent(struct pfctl_rule *);
340e9eb0941SKristof Provost int		 rdr_consistent(struct pfctl_rule *);
3413b3a8eb9SGleb Smirnoff int		 process_tabledef(char *, struct table_opts *);
3423b3a8eb9SGleb Smirnoff void		 expand_label_str(char *, size_t, const char *, const char *);
3433b3a8eb9SGleb Smirnoff void		 expand_label_if(const char *, char *, size_t, const char *);
3443b3a8eb9SGleb Smirnoff void		 expand_label_addr(const char *, char *, size_t, u_int8_t,
34509c7f238SKristof Provost 		    struct pf_rule_addr *);
3463b3a8eb9SGleb Smirnoff void		 expand_label_port(const char *, char *, size_t,
34709c7f238SKristof Provost 		    struct pf_rule_addr *);
3483b3a8eb9SGleb Smirnoff void		 expand_label_proto(const char *, char *, size_t, u_int8_t);
34909c7f238SKristof Provost void		 expand_label_nr(const char *, char *, size_t,
35009c7f238SKristof Provost 		    struct pfctl_rule *);
3512b29ceb8SKristof Provost void		 expand_eth_rule(struct pfctl_eth_rule *,
35287a89d6eSKristof Provost 		    struct node_if *, struct node_etherproto *,
3538a42005dSKristof Provost 		    struct node_mac *, struct node_mac *,
3548a42005dSKristof Provost 		    struct node_host *, struct node_host *, const char *);
355e9eb0941SKristof Provost void		 expand_rule(struct pfctl_rule *, struct node_if *,
3563b3a8eb9SGleb Smirnoff 		    struct node_host *, struct node_proto *, struct node_os *,
3573b3a8eb9SGleb Smirnoff 		    struct node_host *, struct node_port *, struct node_host *,
3583b3a8eb9SGleb Smirnoff 		    struct node_port *, struct node_uid *, struct node_gid *,
3593b3a8eb9SGleb Smirnoff 		    struct node_icmp *, const char *);
3603b3a8eb9SGleb Smirnoff int		 expand_altq(struct pf_altq *, struct node_if *,
3613b3a8eb9SGleb Smirnoff 		    struct node_queue *, struct node_queue_bw bwspec,
3623b3a8eb9SGleb Smirnoff 		    struct node_queue_opt *);
3633b3a8eb9SGleb Smirnoff int		 expand_queue(struct pf_altq *, struct node_if *,
3643b3a8eb9SGleb Smirnoff 		    struct node_queue *, struct node_queue_bw,
3653b3a8eb9SGleb Smirnoff 		    struct node_queue_opt *);
3663b3a8eb9SGleb Smirnoff int		 expand_skip_interface(struct node_if *);
3673b3a8eb9SGleb Smirnoff 
3683b3a8eb9SGleb Smirnoff int	 check_rulestate(int);
3693b3a8eb9SGleb Smirnoff int	 getservice(char *);
3706fcc8e04SKristof Provost int	 rule_label(struct pfctl_rule *, char *s[PF_RULE_MAX_LABEL_COUNT]);
3713b3a8eb9SGleb Smirnoff int	 rt_tableid_max(void);
3723b3a8eb9SGleb Smirnoff 
373e9eb0941SKristof Provost void	 mv_rules(struct pfctl_ruleset *, struct pfctl_ruleset *);
374c5131afeSKristof Provost void	 mv_eth_rules(struct pfctl_eth_ruleset *, struct pfctl_eth_ruleset *);
3753b3a8eb9SGleb Smirnoff void	 decide_address_family(struct node_host *, sa_family_t *);
3763b3a8eb9SGleb Smirnoff void	 remove_invalid_hosts(struct node_host **, sa_family_t *);
3773b3a8eb9SGleb Smirnoff int	 invalid_redirect(struct node_host *, sa_family_t);
3783b3a8eb9SGleb Smirnoff u_int16_t parseicmpspec(char *, sa_family_t);
3791f495578SKristof Provost int	 kw_casecmp(const void *, const void *);
3801f495578SKristof Provost int	 map_tos(char *string, int *);
381b590f17aSKristof Provost struct node_mac* node_mac_from_string(const char *);
382b590f17aSKristof Provost struct node_mac* node_mac_from_string_masklen(const char *, int);
383b590f17aSKristof Provost struct node_mac* node_mac_from_string_mask(const char *, const char *);
3843b3a8eb9SGleb Smirnoff 
38513cfafabSKristof Provost static TAILQ_HEAD(loadanchorshead, loadanchors)
3863b3a8eb9SGleb Smirnoff     loadanchorshead = TAILQ_HEAD_INITIALIZER(loadanchorshead);
3873b3a8eb9SGleb Smirnoff 
3883b3a8eb9SGleb Smirnoff struct loadanchors {
3893b3a8eb9SGleb Smirnoff 	TAILQ_ENTRY(loadanchors)	 entries;
3903b3a8eb9SGleb Smirnoff 	char				*anchorname;
3913b3a8eb9SGleb Smirnoff 	char				*filename;
3923b3a8eb9SGleb Smirnoff };
3933b3a8eb9SGleb Smirnoff 
3943b3a8eb9SGleb Smirnoff typedef struct {
3953b3a8eb9SGleb Smirnoff 	union {
3963b3a8eb9SGleb Smirnoff 		int64_t			 number;
3973b3a8eb9SGleb Smirnoff 		double			 probability;
3983b3a8eb9SGleb Smirnoff 		int			 i;
3993b3a8eb9SGleb Smirnoff 		char			*string;
4003b3a8eb9SGleb Smirnoff 		u_int			 rtableid;
4013b3a8eb9SGleb Smirnoff 		struct {
4023b3a8eb9SGleb Smirnoff 			u_int8_t	 b1;
4033b3a8eb9SGleb Smirnoff 			u_int8_t	 b2;
4043b3a8eb9SGleb Smirnoff 			u_int16_t	 w;
4053b3a8eb9SGleb Smirnoff 			u_int16_t	 w2;
4063b3a8eb9SGleb Smirnoff 		}			 b;
4073b3a8eb9SGleb Smirnoff 		struct range {
4083b3a8eb9SGleb Smirnoff 			int		 a;
4093b3a8eb9SGleb Smirnoff 			int		 b;
4103b3a8eb9SGleb Smirnoff 			int		 t;
4113b3a8eb9SGleb Smirnoff 		}			 range;
4123b3a8eb9SGleb Smirnoff 		struct node_if		*interface;
4133b3a8eb9SGleb Smirnoff 		struct node_proto	*proto;
4142b29ceb8SKristof Provost 		struct node_etherproto	*etherproto;
4153b3a8eb9SGleb Smirnoff 		struct node_icmp	*icmp;
4163b3a8eb9SGleb Smirnoff 		struct node_host	*host;
4173b3a8eb9SGleb Smirnoff 		struct node_os		*os;
4183b3a8eb9SGleb Smirnoff 		struct node_port	*port;
4193b3a8eb9SGleb Smirnoff 		struct node_uid		*uid;
4203b3a8eb9SGleb Smirnoff 		struct node_gid		*gid;
4213b3a8eb9SGleb Smirnoff 		struct node_state_opt	*state_opt;
4223b3a8eb9SGleb Smirnoff 		struct peer		 peer;
4233b3a8eb9SGleb Smirnoff 		struct {
4243b3a8eb9SGleb Smirnoff 			struct peer	 src, dst;
4253b3a8eb9SGleb Smirnoff 			struct node_os	*src_os;
4263b3a8eb9SGleb Smirnoff 		}			 fromto;
4273b3a8eb9SGleb Smirnoff 		struct {
42887a89d6eSKristof Provost 			struct node_mac	*src;
42987a89d6eSKristof Provost 			struct node_mac	*dst;
4302b29ceb8SKristof Provost 		}			 etherfromto;
43187a89d6eSKristof Provost 		struct node_mac		*mac;
4322b29ceb8SKristof Provost 		struct {
43387a89d6eSKristof Provost 			struct node_mac	*mac;
4342b29ceb8SKristof Provost 		} etheraddr;
4352b29ceb8SKristof Provost 		struct {
4363b3a8eb9SGleb Smirnoff 			struct node_host	*host;
4373b3a8eb9SGleb Smirnoff 			u_int8_t		 rt;
4383b3a8eb9SGleb Smirnoff 			u_int8_t		 pool_opts;
4393b3a8eb9SGleb Smirnoff 			sa_family_t		 af;
4403b3a8eb9SGleb Smirnoff 			struct pf_poolhashkey	*key;
4413b3a8eb9SGleb Smirnoff 		}			 route;
4423b3a8eb9SGleb Smirnoff 		struct redirection {
4433b3a8eb9SGleb Smirnoff 			struct node_host	*host;
4443b3a8eb9SGleb Smirnoff 			struct range		 rport;
4453b3a8eb9SGleb Smirnoff 		}			*redirection;
4463b3a8eb9SGleb Smirnoff 		struct {
4473b3a8eb9SGleb Smirnoff 			int			 action;
4483b3a8eb9SGleb Smirnoff 			struct node_state_opt	*options;
4493b3a8eb9SGleb Smirnoff 		}			 keep_state;
4503b3a8eb9SGleb Smirnoff 		struct {
4513b3a8eb9SGleb Smirnoff 			u_int8_t	 log;
4523b3a8eb9SGleb Smirnoff 			u_int8_t	 logif;
4533b3a8eb9SGleb Smirnoff 			u_int8_t	 quick;
4543b3a8eb9SGleb Smirnoff 		}			 logquick;
4553b3a8eb9SGleb Smirnoff 		struct {
4563b3a8eb9SGleb Smirnoff 			int		 neg;
4573b3a8eb9SGleb Smirnoff 			char		*name;
4583b3a8eb9SGleb Smirnoff 		}			 tagged;
4593b3a8eb9SGleb Smirnoff 		struct pf_poolhashkey	*hashkey;
4603b3a8eb9SGleb Smirnoff 		struct node_queue	*queue;
4613b3a8eb9SGleb Smirnoff 		struct node_queue_opt	 queue_options;
4623b3a8eb9SGleb Smirnoff 		struct node_queue_bw	 queue_bwspec;
4633b3a8eb9SGleb Smirnoff 		struct node_qassign	 qassign;
4643b3a8eb9SGleb Smirnoff 		struct filter_opts	 filter_opts;
4653b3a8eb9SGleb Smirnoff 		struct antispoof_opts	 antispoof_opts;
4663b3a8eb9SGleb Smirnoff 		struct queue_opts	 queue_opts;
4673b3a8eb9SGleb Smirnoff 		struct scrub_opts	 scrub_opts;
4683b3a8eb9SGleb Smirnoff 		struct table_opts	 table_opts;
4693b3a8eb9SGleb Smirnoff 		struct pool_opts	 pool_opts;
4703b3a8eb9SGleb Smirnoff 		struct node_hfsc_opts	 hfsc_opts;
471a5b789f6SErmal Luçi 		struct node_fairq_opts	 fairq_opts;
4720a70aaf8SLuiz Otavio O Souza 		struct codel_opts	 codel_opts;
4735062afffSKristof Provost 		struct pfctl_watermarks	*watermarks;
4743b3a8eb9SGleb Smirnoff 	} v;
4753b3a8eb9SGleb Smirnoff 	int lineno;
4763b3a8eb9SGleb Smirnoff } YYSTYPE;
4773b3a8eb9SGleb Smirnoff 
4783b3a8eb9SGleb Smirnoff #define PPORT_RANGE	1
4793b3a8eb9SGleb Smirnoff #define PPORT_STAR	2
4803b3a8eb9SGleb Smirnoff int	parseport(char *, struct range *r, int);
4813b3a8eb9SGleb Smirnoff 
4823b3a8eb9SGleb Smirnoff #define DYNIF_MULTIADDR(addr) ((addr).type == PF_ADDR_DYNIFTL && \
4833b3a8eb9SGleb Smirnoff 	(!((addr).iflags & PFI_AFLAG_NOALIAS) ||		 \
4843b3a8eb9SGleb Smirnoff 	!isdigit((addr).v.ifname[strlen((addr).v.ifname)-1])))
4853b3a8eb9SGleb Smirnoff 
4863b3a8eb9SGleb Smirnoff %}
4873b3a8eb9SGleb Smirnoff 
488ef950daaSKristof Provost %token	PASS BLOCK MATCH SCRUB RETURN IN OS OUT LOG QUICK ON FROM TO FLAGS
4893b3a8eb9SGleb Smirnoff %token	RETURNRST RETURNICMP RETURNICMP6 PROTO INET INET6 ALL ANY ICMPTYPE
4903b3a8eb9SGleb Smirnoff %token	ICMP6TYPE CODE KEEP MODULATE STATE PORT RDR NAT BINAT ARROW NODF
4913b3a8eb9SGleb Smirnoff %token	MINTTL ERROR ALLOWOPTS FASTROUTE FILENAME ROUTETO DUPTO REPLYTO NO LABEL
4923b3a8eb9SGleb Smirnoff %token	NOROUTE URPFFAILED FRAGMENT USER GROUP MAXMSS MAXIMUM TTL TOS DROP TABLE
4933b3a8eb9SGleb Smirnoff %token	REASSEMBLE FRAGDROP FRAGCROP ANCHOR NATANCHOR RDRANCHOR BINATANCHOR
494150182e3SKristof Provost %token	SET OPTIMIZATION TIMEOUT LIMIT LOGINTERFACE BLOCKPOLICY FAILPOLICY
495150182e3SKristof Provost %token	RANDOMID REQUIREORDER SYNPROXY FINGERPRINTS NOSYNC DEBUG SKIP HOSTID
4968a42005dSKristof Provost %token	ANTISPOOF FOR INCLUDE KEEPCOUNTERS SYNCOOKIES L3
4972b29ceb8SKristof Provost %token	ETHER
4982aa21096SKurosawa Takahiro %token	BITMASK RANDOM SOURCEHASH ROUNDROBIN STATICPORT PROBABILITY MAPEPORTSET
4990a70aaf8SLuiz Otavio O Souza %token	ALTQ CBQ CODEL PRIQ HFSC FAIRQ BANDWIDTH TBRSIZE LINKSHARE REALTIME
5000a70aaf8SLuiz Otavio O Souza %token	UPPERLIMIT QUEUE PRIORITY QLIMIT HOGS BUCKETS RTABLE TARGET INTERVAL
50176c5eeccSKristof Provost %token	DNPIPE DNQUEUE RIDENTIFIER
5023e248e0fSKristof Provost %token	LOAD RULESET_OPTIMIZATION PRIO
5033b3a8eb9SGleb Smirnoff %token	STICKYADDRESS MAXSRCSTATES MAXSRCNODES SOURCETRACK GLOBAL RULE
5043b3a8eb9SGleb Smirnoff %token	MAXSRCCONN MAXSRCCONNRATE OVERLOAD FLUSH SLOPPY
5053b3a8eb9SGleb Smirnoff %token	TAGGED TAG IFBOUND FLOATING STATEPOLICY STATEDEFAULTS ROUTE SETTOS
5063b3a8eb9SGleb Smirnoff %token	DIVERTTO DIVERTREPLY
5073b3a8eb9SGleb Smirnoff %token	<v.string>		STRING
5083b3a8eb9SGleb Smirnoff %token	<v.number>		NUMBER
5093b3a8eb9SGleb Smirnoff %token	<v.i>			PORTBINARY
5103b3a8eb9SGleb Smirnoff %type	<v.interface>		interface if_list if_item_not if_item
5113b3a8eb9SGleb Smirnoff %type	<v.number>		number icmptype icmp6type uid gid
5123b3a8eb9SGleb Smirnoff %type	<v.number>		tos not yesno
5133b3a8eb9SGleb Smirnoff %type	<v.probability>		probability
514c69121c4SKristof Provost %type	<v.i>			no dir af fragcache optimizer syncookie_val
5153b3a8eb9SGleb Smirnoff %type	<v.i>			sourcetrack flush unaryop statelock
5162b29ceb8SKristof Provost %type	<v.i>			etherprotoval
5173b3a8eb9SGleb Smirnoff %type	<v.b>			action nataction natpasslog scrubaction
5183e248e0fSKristof Provost %type	<v.b>			flags flag blockspec prio
5193b3a8eb9SGleb Smirnoff %type	<v.range>		portplain portstar portrange
5203b3a8eb9SGleb Smirnoff %type	<v.hashkey>		hashkey
5213b3a8eb9SGleb Smirnoff %type	<v.proto>		proto proto_list proto_item
5223b3a8eb9SGleb Smirnoff %type	<v.number>		protoval
5233b3a8eb9SGleb Smirnoff %type	<v.icmp>		icmpspec
5243b3a8eb9SGleb Smirnoff %type	<v.icmp>		icmp_list icmp_item
5253b3a8eb9SGleb Smirnoff %type	<v.icmp>		icmp6_list icmp6_item
5263b3a8eb9SGleb Smirnoff %type	<v.number>		reticmpspec reticmp6spec
5278a42005dSKristof Provost %type	<v.fromto>		fromto l3fromto
5283b3a8eb9SGleb Smirnoff %type	<v.peer>		ipportspec from to
5293b3a8eb9SGleb Smirnoff %type	<v.host>		ipspec toipspec xhost host dynaddr host_list
5303b3a8eb9SGleb Smirnoff %type	<v.host>		redir_host_list redirspec
5313b3a8eb9SGleb Smirnoff %type	<v.host>		route_host route_host_list routespec
5323b3a8eb9SGleb Smirnoff %type	<v.os>			os xos os_list
5333b3a8eb9SGleb Smirnoff %type	<v.port>		portspec port_list port_item
5343b3a8eb9SGleb Smirnoff %type	<v.uid>			uids uid_list uid_item
5353b3a8eb9SGleb Smirnoff %type	<v.gid>			gids gid_list gid_item
5363b3a8eb9SGleb Smirnoff %type	<v.route>		route
5373b3a8eb9SGleb Smirnoff %type	<v.redirection>		redirection redirpool
5383b3a8eb9SGleb Smirnoff %type	<v.string>		label stringall tag anchorname
5393b3a8eb9SGleb Smirnoff %type	<v.string>		string varstring numberstring
5403b3a8eb9SGleb Smirnoff %type	<v.keep_state>		keep
5413b3a8eb9SGleb Smirnoff %type	<v.state_opt>		state_opt_spec state_opt_list state_opt_item
5423b3a8eb9SGleb Smirnoff %type	<v.logquick>		logquick quick log logopts logopt
5433b3a8eb9SGleb Smirnoff %type	<v.interface>		antispoof_ifspc antispoof_iflst antispoof_if
5442b29ceb8SKristof Provost %type	<v.qassign>		qname etherqname
5453b3a8eb9SGleb Smirnoff %type	<v.queue>		qassign qassign_list qassign_item
5463b3a8eb9SGleb Smirnoff %type	<v.queue_options>	scheduler
5473b3a8eb9SGleb Smirnoff %type	<v.number>		cbqflags_list cbqflags_item
5483b3a8eb9SGleb Smirnoff %type	<v.number>		priqflags_list priqflags_item
5493b3a8eb9SGleb Smirnoff %type	<v.hfsc_opts>		hfscopts_list hfscopts_item hfsc_opts
550a5b789f6SErmal Luçi %type	<v.fairq_opts>		fairqopts_list fairqopts_item fairq_opts
5510a70aaf8SLuiz Otavio O Souza %type	<v.codel_opts>		codelopts_list codelopts_item codel_opts
5523b3a8eb9SGleb Smirnoff %type	<v.queue_bwspec>	bandwidth
5532b29ceb8SKristof Provost %type	<v.filter_opts>		filter_opts filter_opt filter_opts_l etherfilter_opts etherfilter_opt etherfilter_opts_l
5543e248e0fSKristof Provost %type	<v.filter_opts>		filter_sets filter_set filter_sets_l
5553b3a8eb9SGleb Smirnoff %type	<v.antispoof_opts>	antispoof_opts antispoof_opt antispoof_opts_l
5563b3a8eb9SGleb Smirnoff %type	<v.queue_opts>		queue_opts queue_opt queue_opts_l
5573b3a8eb9SGleb Smirnoff %type	<v.scrub_opts>		scrub_opts scrub_opt scrub_opts_l
5583b3a8eb9SGleb Smirnoff %type	<v.table_opts>		table_opts table_opt table_opts_l
5593b3a8eb9SGleb Smirnoff %type	<v.pool_opts>		pool_opts pool_opt pool_opts_l
5603b3a8eb9SGleb Smirnoff %type	<v.tagged>		tagged
5613b3a8eb9SGleb Smirnoff %type	<v.rtableid>		rtable
5625062afffSKristof Provost %type	<v.watermarks>		syncookie_opts
5632b29ceb8SKristof Provost %type	<v.etherproto>		etherproto etherproto_list etherproto_item
5642b29ceb8SKristof Provost %type	<v.etherfromto>		etherfromto
5652b29ceb8SKristof Provost %type	<v.etheraddr>		etherfrom etherto
56687a89d6eSKristof Provost %type	<v.mac>			xmac mac mac_list macspec
5673b3a8eb9SGleb Smirnoff %%
5683b3a8eb9SGleb Smirnoff 
5693b3a8eb9SGleb Smirnoff ruleset		: /* empty */
5703b3a8eb9SGleb Smirnoff 		| ruleset include '\n'
5713b3a8eb9SGleb Smirnoff 		| ruleset '\n'
5723b3a8eb9SGleb Smirnoff 		| ruleset option '\n'
5732b29ceb8SKristof Provost 		| ruleset etherrule '\n'
574c5131afeSKristof Provost 		| ruleset etheranchorrule '\n'
5753b3a8eb9SGleb Smirnoff 		| ruleset scrubrule '\n'
5763b3a8eb9SGleb Smirnoff 		| ruleset natrule '\n'
5773b3a8eb9SGleb Smirnoff 		| ruleset binatrule '\n'
5783b3a8eb9SGleb Smirnoff 		| ruleset pfrule '\n'
5793b3a8eb9SGleb Smirnoff 		| ruleset anchorrule '\n'
5803b3a8eb9SGleb Smirnoff 		| ruleset loadrule '\n'
5813b3a8eb9SGleb Smirnoff 		| ruleset altqif '\n'
5823b3a8eb9SGleb Smirnoff 		| ruleset queuespec '\n'
5833b3a8eb9SGleb Smirnoff 		| ruleset varset '\n'
5843b3a8eb9SGleb Smirnoff 		| ruleset antispoof '\n'
5853b3a8eb9SGleb Smirnoff 		| ruleset tabledef '\n'
5863b3a8eb9SGleb Smirnoff 		| '{' fakeanchor '}' '\n';
5873b3a8eb9SGleb Smirnoff 		| ruleset error '\n'		{ file->errors++; }
5883b3a8eb9SGleb Smirnoff 		;
5893b3a8eb9SGleb Smirnoff 
5903b3a8eb9SGleb Smirnoff include		: INCLUDE STRING		{
5913b3a8eb9SGleb Smirnoff 			struct file	*nfile;
5923b3a8eb9SGleb Smirnoff 
5933b3a8eb9SGleb Smirnoff 			if ((nfile = pushfile($2, 0)) == NULL) {
5943b3a8eb9SGleb Smirnoff 				yyerror("failed to include file %s", $2);
5953b3a8eb9SGleb Smirnoff 				free($2);
5963b3a8eb9SGleb Smirnoff 				YYERROR;
5973b3a8eb9SGleb Smirnoff 			}
5983b3a8eb9SGleb Smirnoff 			free($2);
5993b3a8eb9SGleb Smirnoff 
6003b3a8eb9SGleb Smirnoff 			file = nfile;
6013b3a8eb9SGleb Smirnoff 			lungetc('\n');
6023b3a8eb9SGleb Smirnoff 		}
6033b3a8eb9SGleb Smirnoff 		;
6043b3a8eb9SGleb Smirnoff 
6053b3a8eb9SGleb Smirnoff /*
6063b3a8eb9SGleb Smirnoff  * apply to previouslys specified rule: must be careful to note
6073b3a8eb9SGleb Smirnoff  * what that is: pf or nat or binat or rdr
6083b3a8eb9SGleb Smirnoff  */
6093b3a8eb9SGleb Smirnoff fakeanchor	: fakeanchor '\n'
6103b3a8eb9SGleb Smirnoff 		| fakeanchor anchorrule '\n'
6113b3a8eb9SGleb Smirnoff 		| fakeanchor binatrule '\n'
6123b3a8eb9SGleb Smirnoff 		| fakeanchor natrule '\n'
6133b3a8eb9SGleb Smirnoff 		| fakeanchor pfrule '\n'
6143b3a8eb9SGleb Smirnoff 		| fakeanchor error '\n'
6153b3a8eb9SGleb Smirnoff 		;
6163b3a8eb9SGleb Smirnoff 
6173b3a8eb9SGleb Smirnoff optimizer	: string	{
6183b3a8eb9SGleb Smirnoff 			if (!strcmp($1, "none"))
6193b3a8eb9SGleb Smirnoff 				$$ = 0;
6203b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "basic"))
6213b3a8eb9SGleb Smirnoff 				$$ = PF_OPTIMIZE_BASIC;
6223b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "profile"))
6233b3a8eb9SGleb Smirnoff 				$$ = PF_OPTIMIZE_BASIC | PF_OPTIMIZE_PROFILE;
6243b3a8eb9SGleb Smirnoff 			else {
6253b3a8eb9SGleb Smirnoff 				yyerror("unknown ruleset-optimization %s", $1);
6263b3a8eb9SGleb Smirnoff 				YYERROR;
6273b3a8eb9SGleb Smirnoff 			}
6283b3a8eb9SGleb Smirnoff 		}
6293b3a8eb9SGleb Smirnoff 		;
6303b3a8eb9SGleb Smirnoff 
6313b3a8eb9SGleb Smirnoff option		: SET OPTIMIZATION STRING		{
6323b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_OPTION)) {
6333b3a8eb9SGleb Smirnoff 				free($3);
6343b3a8eb9SGleb Smirnoff 				YYERROR;
6353b3a8eb9SGleb Smirnoff 			}
6363b3a8eb9SGleb Smirnoff 			if (pfctl_set_optimization(pf, $3) != 0) {
6373b3a8eb9SGleb Smirnoff 				yyerror("unknown optimization %s", $3);
6383b3a8eb9SGleb Smirnoff 				free($3);
6393b3a8eb9SGleb Smirnoff 				YYERROR;
6403b3a8eb9SGleb Smirnoff 			}
6413b3a8eb9SGleb Smirnoff 			free($3);
6423b3a8eb9SGleb Smirnoff 		}
6433b3a8eb9SGleb Smirnoff 		| SET RULESET_OPTIMIZATION optimizer {
6443b3a8eb9SGleb Smirnoff 			if (!(pf->opts & PF_OPT_OPTIMIZE)) {
6453b3a8eb9SGleb Smirnoff 				pf->opts |= PF_OPT_OPTIMIZE;
6463b3a8eb9SGleb Smirnoff 				pf->optimize = $3;
6473b3a8eb9SGleb Smirnoff 			}
6483b3a8eb9SGleb Smirnoff 		}
6493b3a8eb9SGleb Smirnoff 		| SET TIMEOUT timeout_spec
6503b3a8eb9SGleb Smirnoff 		| SET TIMEOUT '{' optnl timeout_list '}'
6513b3a8eb9SGleb Smirnoff 		| SET LIMIT limit_spec
6523b3a8eb9SGleb Smirnoff 		| SET LIMIT '{' optnl limit_list '}'
6533b3a8eb9SGleb Smirnoff 		| SET LOGINTERFACE stringall		{
6543b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_OPTION)) {
6553b3a8eb9SGleb Smirnoff 				free($3);
6563b3a8eb9SGleb Smirnoff 				YYERROR;
6573b3a8eb9SGleb Smirnoff 			}
6583b3a8eb9SGleb Smirnoff 			if (pfctl_set_logif(pf, $3) != 0) {
6593b3a8eb9SGleb Smirnoff 				yyerror("error setting loginterface %s", $3);
6603b3a8eb9SGleb Smirnoff 				free($3);
6613b3a8eb9SGleb Smirnoff 				YYERROR;
6623b3a8eb9SGleb Smirnoff 			}
6633b3a8eb9SGleb Smirnoff 			free($3);
6643b3a8eb9SGleb Smirnoff 		}
6653b3a8eb9SGleb Smirnoff 		| SET HOSTID number {
6663b3a8eb9SGleb Smirnoff 			if ($3 == 0 || $3 > UINT_MAX) {
6673b3a8eb9SGleb Smirnoff 				yyerror("hostid must be non-zero");
6683b3a8eb9SGleb Smirnoff 				YYERROR;
6693b3a8eb9SGleb Smirnoff 			}
6703b3a8eb9SGleb Smirnoff 			if (pfctl_set_hostid(pf, $3) != 0) {
6713b3a8eb9SGleb Smirnoff 				yyerror("error setting hostid %08x", $3);
6723b3a8eb9SGleb Smirnoff 				YYERROR;
6733b3a8eb9SGleb Smirnoff 			}
6743b3a8eb9SGleb Smirnoff 		}
6753b3a8eb9SGleb Smirnoff 		| SET BLOCKPOLICY DROP	{
6763b3a8eb9SGleb Smirnoff 			if (pf->opts & PF_OPT_VERBOSE)
6773b3a8eb9SGleb Smirnoff 				printf("set block-policy drop\n");
6783b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_OPTION))
6793b3a8eb9SGleb Smirnoff 				YYERROR;
6803b3a8eb9SGleb Smirnoff 			blockpolicy = PFRULE_DROP;
6813b3a8eb9SGleb Smirnoff 		}
6823b3a8eb9SGleb Smirnoff 		| SET BLOCKPOLICY RETURN {
6833b3a8eb9SGleb Smirnoff 			if (pf->opts & PF_OPT_VERBOSE)
6843b3a8eb9SGleb Smirnoff 				printf("set block-policy return\n");
6853b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_OPTION))
6863b3a8eb9SGleb Smirnoff 				YYERROR;
6873b3a8eb9SGleb Smirnoff 			blockpolicy = PFRULE_RETURN;
6883b3a8eb9SGleb Smirnoff 		}
689150182e3SKristof Provost 		| SET FAILPOLICY DROP	{
690150182e3SKristof Provost 			if (pf->opts & PF_OPT_VERBOSE)
691150182e3SKristof Provost 				printf("set fail-policy drop\n");
692150182e3SKristof Provost 			if (check_rulestate(PFCTL_STATE_OPTION))
693150182e3SKristof Provost 				YYERROR;
694150182e3SKristof Provost 			failpolicy = PFRULE_DROP;
695150182e3SKristof Provost 		}
696150182e3SKristof Provost 		| SET FAILPOLICY RETURN {
697150182e3SKristof Provost 			if (pf->opts & PF_OPT_VERBOSE)
698150182e3SKristof Provost 				printf("set fail-policy return\n");
699150182e3SKristof Provost 			if (check_rulestate(PFCTL_STATE_OPTION))
700150182e3SKristof Provost 				YYERROR;
701150182e3SKristof Provost 			failpolicy = PFRULE_RETURN;
702150182e3SKristof Provost 		}
7033b3a8eb9SGleb Smirnoff 		| SET REQUIREORDER yesno {
7043b3a8eb9SGleb Smirnoff 			if (pf->opts & PF_OPT_VERBOSE)
7053b3a8eb9SGleb Smirnoff 				printf("set require-order %s\n",
7063b3a8eb9SGleb Smirnoff 				    $3 == 1 ? "yes" : "no");
7073b3a8eb9SGleb Smirnoff 			require_order = $3;
7083b3a8eb9SGleb Smirnoff 		}
7093b3a8eb9SGleb Smirnoff 		| SET FINGERPRINTS STRING {
7103b3a8eb9SGleb Smirnoff 			if (pf->opts & PF_OPT_VERBOSE)
7113b3a8eb9SGleb Smirnoff 				printf("set fingerprints \"%s\"\n", $3);
7123b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_OPTION)) {
7133b3a8eb9SGleb Smirnoff 				free($3);
7143b3a8eb9SGleb Smirnoff 				YYERROR;
7153b3a8eb9SGleb Smirnoff 			}
7163b3a8eb9SGleb Smirnoff 			if (!pf->anchor->name[0]) {
7173b3a8eb9SGleb Smirnoff 				if (pfctl_file_fingerprints(pf->dev,
7183b3a8eb9SGleb Smirnoff 				    pf->opts, $3)) {
7193b3a8eb9SGleb Smirnoff 					yyerror("error loading "
7203b3a8eb9SGleb Smirnoff 					    "fingerprints %s", $3);
7213b3a8eb9SGleb Smirnoff 					free($3);
7223b3a8eb9SGleb Smirnoff 					YYERROR;
7233b3a8eb9SGleb Smirnoff 				}
7243b3a8eb9SGleb Smirnoff 			}
7253b3a8eb9SGleb Smirnoff 			free($3);
7263b3a8eb9SGleb Smirnoff 		}
7273b3a8eb9SGleb Smirnoff 		| SET STATEPOLICY statelock {
7283b3a8eb9SGleb Smirnoff 			if (pf->opts & PF_OPT_VERBOSE)
7293b3a8eb9SGleb Smirnoff 				switch ($3) {
7303b3a8eb9SGleb Smirnoff 				case 0:
7313b3a8eb9SGleb Smirnoff 					printf("set state-policy floating\n");
7323b3a8eb9SGleb Smirnoff 					break;
7333b3a8eb9SGleb Smirnoff 				case PFRULE_IFBOUND:
7343b3a8eb9SGleb Smirnoff 					printf("set state-policy if-bound\n");
7353b3a8eb9SGleb Smirnoff 					break;
7363b3a8eb9SGleb Smirnoff 				}
7373b3a8eb9SGleb Smirnoff 			default_statelock = $3;
7383b3a8eb9SGleb Smirnoff 		}
7393b3a8eb9SGleb Smirnoff 		| SET DEBUG STRING {
7403b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_OPTION)) {
7413b3a8eb9SGleb Smirnoff 				free($3);
7423b3a8eb9SGleb Smirnoff 				YYERROR;
7433b3a8eb9SGleb Smirnoff 			}
7443b3a8eb9SGleb Smirnoff 			if (pfctl_set_debug(pf, $3) != 0) {
7453b3a8eb9SGleb Smirnoff 				yyerror("error setting debuglevel %s", $3);
7463b3a8eb9SGleb Smirnoff 				free($3);
7473b3a8eb9SGleb Smirnoff 				YYERROR;
7483b3a8eb9SGleb Smirnoff 			}
7493b3a8eb9SGleb Smirnoff 			free($3);
7503b3a8eb9SGleb Smirnoff 		}
7513b3a8eb9SGleb Smirnoff 		| SET SKIP interface {
7523b3a8eb9SGleb Smirnoff 			if (expand_skip_interface($3) != 0) {
7533b3a8eb9SGleb Smirnoff 				yyerror("error setting skip interface(s)");
7543b3a8eb9SGleb Smirnoff 				YYERROR;
7553b3a8eb9SGleb Smirnoff 			}
7563b3a8eb9SGleb Smirnoff 		}
7573b3a8eb9SGleb Smirnoff 		| SET STATEDEFAULTS state_opt_list {
7583b3a8eb9SGleb Smirnoff 			if (keep_state_defaults != NULL) {
7593b3a8eb9SGleb Smirnoff 				yyerror("cannot redefine state-defaults");
7603b3a8eb9SGleb Smirnoff 				YYERROR;
7613b3a8eb9SGleb Smirnoff 			}
7623b3a8eb9SGleb Smirnoff 			keep_state_defaults = $3;
7633b3a8eb9SGleb Smirnoff 		}
76442ec75f8SKristof Provost 		| SET KEEPCOUNTERS {
76542ec75f8SKristof Provost 			pf->keep_counters = true;
76642ec75f8SKristof Provost 		}
7675062afffSKristof Provost 		| SET SYNCOOKIES syncookie_val syncookie_opts {
7685062afffSKristof Provost 			if (pfctl_cfg_syncookies(pf, $3, $4)) {
7695062afffSKristof Provost 				yyerror("error setting syncookies");
7705062afffSKristof Provost 				YYERROR;
7715062afffSKristof Provost 			}
772c69121c4SKristof Provost 		}
773c69121c4SKristof Provost 		;
774c69121c4SKristof Provost 
775c69121c4SKristof Provost syncookie_val  : STRING        {
776c69121c4SKristof Provost 			if (!strcmp($1, "never"))
777c69121c4SKristof Provost 				$$ = PFCTL_SYNCOOKIES_NEVER;
7785062afffSKristof Provost 			else if (!strcmp($1, "adaptive"))
7795062afffSKristof Provost 				$$ = PFCTL_SYNCOOKIES_ADAPTIVE;
780c69121c4SKristof Provost 			else if (!strcmp($1, "always"))
781c69121c4SKristof Provost 				$$ = PFCTL_SYNCOOKIES_ALWAYS;
782c69121c4SKristof Provost 			else {
783c69121c4SKristof Provost 				yyerror("illegal value for syncookies");
784c69121c4SKristof Provost 				YYERROR;
785c69121c4SKristof Provost 			}
786c69121c4SKristof Provost 		}
7873b3a8eb9SGleb Smirnoff 		;
7885062afffSKristof Provost syncookie_opts  : /* empty */                   { $$ = NULL; }
7895062afffSKristof Provost 		| {
7905062afffSKristof Provost 			memset(&syncookie_opts, 0, sizeof(syncookie_opts));
7915062afffSKristof Provost 		  } '(' syncookie_opt_l ')'     { $$ = &syncookie_opts; }
7925062afffSKristof Provost 		;
7935062afffSKristof Provost 
7945062afffSKristof Provost syncookie_opt_l : syncookie_opt_l comma syncookie_opt
7955062afffSKristof Provost 		| syncookie_opt
7965062afffSKristof Provost 		;
7975062afffSKristof Provost 
7985062afffSKristof Provost syncookie_opt   : STRING STRING {
7995062afffSKristof Provost 			double   val;
8005062afffSKristof Provost 			char    *cp;
8015062afffSKristof Provost 
8025062afffSKristof Provost 			val = strtod($2, &cp);
8035062afffSKristof Provost 			if (cp == NULL || strcmp(cp, "%"))
8045062afffSKristof Provost 				YYERROR;
8055062afffSKristof Provost 			if (val <= 0 || val > 100) {
8065062afffSKristof Provost 				yyerror("illegal percentage value");
8075062afffSKristof Provost 				YYERROR;
8085062afffSKristof Provost 			}
8095062afffSKristof Provost 			if (!strcmp($1, "start")) {
8105062afffSKristof Provost 				syncookie_opts.hi = val;
8115062afffSKristof Provost 			} else if (!strcmp($1, "end")) {
8125062afffSKristof Provost 				syncookie_opts.lo = val;
8135062afffSKristof Provost 			} else {
8145062afffSKristof Provost 				yyerror("illegal syncookie option");
8155062afffSKristof Provost 				YYERROR;
8165062afffSKristof Provost 			}
8175062afffSKristof Provost 		}
8185062afffSKristof Provost 		;
8193b3a8eb9SGleb Smirnoff 
8203b3a8eb9SGleb Smirnoff stringall	: STRING	{ $$ = $1; }
8213b3a8eb9SGleb Smirnoff 		| ALL		{
8223b3a8eb9SGleb Smirnoff 			if (($$ = strdup("all")) == NULL) {
8233b3a8eb9SGleb Smirnoff 				err(1, "stringall: strdup");
8243b3a8eb9SGleb Smirnoff 			}
8253b3a8eb9SGleb Smirnoff 		}
8263b3a8eb9SGleb Smirnoff 		;
8273b3a8eb9SGleb Smirnoff 
8283b3a8eb9SGleb Smirnoff string		: STRING string				{
8293b3a8eb9SGleb Smirnoff 			if (asprintf(&$$, "%s %s", $1, $2) == -1)
8303b3a8eb9SGleb Smirnoff 				err(1, "string: asprintf");
8313b3a8eb9SGleb Smirnoff 			free($1);
8323b3a8eb9SGleb Smirnoff 			free($2);
8333b3a8eb9SGleb Smirnoff 		}
8343b3a8eb9SGleb Smirnoff 		| STRING
8353b3a8eb9SGleb Smirnoff 		;
8363b3a8eb9SGleb Smirnoff 
8373b3a8eb9SGleb Smirnoff varstring	: numberstring varstring 		{
8383b3a8eb9SGleb Smirnoff 			if (asprintf(&$$, "%s %s", $1, $2) == -1)
8393b3a8eb9SGleb Smirnoff 				err(1, "string: asprintf");
8403b3a8eb9SGleb Smirnoff 			free($1);
8413b3a8eb9SGleb Smirnoff 			free($2);
8423b3a8eb9SGleb Smirnoff 		}
8433b3a8eb9SGleb Smirnoff 		| numberstring
8443b3a8eb9SGleb Smirnoff 		;
8453b3a8eb9SGleb Smirnoff 
8463b3a8eb9SGleb Smirnoff numberstring	: NUMBER				{
8473b3a8eb9SGleb Smirnoff 			char	*s;
8483b3a8eb9SGleb Smirnoff 			if (asprintf(&s, "%lld", (long long)$1) == -1) {
8493b3a8eb9SGleb Smirnoff 				yyerror("string: asprintf");
8503b3a8eb9SGleb Smirnoff 				YYERROR;
8513b3a8eb9SGleb Smirnoff 			}
8523b3a8eb9SGleb Smirnoff 			$$ = s;
8533b3a8eb9SGleb Smirnoff 		}
8543b3a8eb9SGleb Smirnoff 		| STRING
8553b3a8eb9SGleb Smirnoff 		;
8563b3a8eb9SGleb Smirnoff 
8573b3a8eb9SGleb Smirnoff varset		: STRING '=' varstring	{
858d3f65324SKristof Provost 			char *s = $1;
8593b3a8eb9SGleb Smirnoff 			if (pf->opts & PF_OPT_VERBOSE)
8603b3a8eb9SGleb Smirnoff 				printf("%s = \"%s\"\n", $1, $3);
861d3f65324SKristof Provost 			while (*s++) {
862d3f65324SKristof Provost 				if (isspace((unsigned char)*s)) {
863d3f65324SKristof Provost 					yyerror("macro name cannot contain "
864d3f65324SKristof Provost 					   "whitespace");
865d3f65324SKristof Provost 					YYERROR;
866d3f65324SKristof Provost 				}
867d3f65324SKristof Provost 			}
8683b3a8eb9SGleb Smirnoff 			if (symset($1, $3, 0) == -1)
8693b3a8eb9SGleb Smirnoff 				err(1, "cannot store variable %s", $1);
8703b3a8eb9SGleb Smirnoff 			free($1);
8713b3a8eb9SGleb Smirnoff 			free($3);
8723b3a8eb9SGleb Smirnoff 		}
8733b3a8eb9SGleb Smirnoff 		;
8743b3a8eb9SGleb Smirnoff 
8753b3a8eb9SGleb Smirnoff anchorname	: STRING			{ $$ = $1; }
8763b3a8eb9SGleb Smirnoff 		| /* empty */			{ $$ = NULL; }
8773b3a8eb9SGleb Smirnoff 		;
8783b3a8eb9SGleb Smirnoff 
8793b3a8eb9SGleb Smirnoff pfa_anchorlist	: /* empty */
8803b3a8eb9SGleb Smirnoff 		| pfa_anchorlist '\n'
8813b3a8eb9SGleb Smirnoff 		| pfa_anchorlist pfrule '\n'
8823b3a8eb9SGleb Smirnoff 		| pfa_anchorlist anchorrule '\n'
8833b3a8eb9SGleb Smirnoff 		;
8843b3a8eb9SGleb Smirnoff 
8853b3a8eb9SGleb Smirnoff pfa_anchor	: '{'
8863b3a8eb9SGleb Smirnoff 		{
8873b3a8eb9SGleb Smirnoff 			char ta[PF_ANCHOR_NAME_SIZE];
888e9eb0941SKristof Provost 			struct pfctl_ruleset *rs;
8893b3a8eb9SGleb Smirnoff 
8903b3a8eb9SGleb Smirnoff 			/* steping into a brace anchor */
8913b3a8eb9SGleb Smirnoff 			pf->asd++;
8923b3a8eb9SGleb Smirnoff 			pf->bn++;
8933b3a8eb9SGleb Smirnoff 
8943b3a8eb9SGleb Smirnoff 			/* create a holding ruleset in the root */
8953b3a8eb9SGleb Smirnoff 			snprintf(ta, PF_ANCHOR_NAME_SIZE, "_%d", pf->bn);
8963b3a8eb9SGleb Smirnoff 			rs = pf_find_or_create_ruleset(ta);
8973b3a8eb9SGleb Smirnoff 			if (rs == NULL)
8983b3a8eb9SGleb Smirnoff 				err(1, "pfa_anchor: pf_find_or_create_ruleset");
8993b3a8eb9SGleb Smirnoff 			pf->astack[pf->asd] = rs->anchor;
9003b3a8eb9SGleb Smirnoff 			pf->anchor = rs->anchor;
9013b3a8eb9SGleb Smirnoff 		} '\n' pfa_anchorlist '}'
9023b3a8eb9SGleb Smirnoff 		{
9033b3a8eb9SGleb Smirnoff 			pf->alast = pf->anchor;
9043b3a8eb9SGleb Smirnoff 			pf->asd--;
9053b3a8eb9SGleb Smirnoff 			pf->anchor = pf->astack[pf->asd];
9063b3a8eb9SGleb Smirnoff 		}
9073b3a8eb9SGleb Smirnoff 		| /* empty */
9083b3a8eb9SGleb Smirnoff 		;
9093b3a8eb9SGleb Smirnoff 
9103b3a8eb9SGleb Smirnoff anchorrule	: ANCHOR anchorname dir quick interface af proto fromto
9113b3a8eb9SGleb Smirnoff 		    filter_opts pfa_anchor
9123b3a8eb9SGleb Smirnoff 		{
913e9eb0941SKristof Provost 			struct pfctl_rule	r;
9143b3a8eb9SGleb Smirnoff 			struct node_proto	*proto;
9153b3a8eb9SGleb Smirnoff 
9163b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_FILTER)) {
9173b3a8eb9SGleb Smirnoff 				if ($2)
9183b3a8eb9SGleb Smirnoff 					free($2);
9193b3a8eb9SGleb Smirnoff 				YYERROR;
9203b3a8eb9SGleb Smirnoff 			}
9213b3a8eb9SGleb Smirnoff 
9223b3a8eb9SGleb Smirnoff 			if ($2 && ($2[0] == '_' || strstr($2, "/_") != NULL)) {
9233b3a8eb9SGleb Smirnoff 				free($2);
9243b3a8eb9SGleb Smirnoff 				yyerror("anchor names beginning with '_' "
9253b3a8eb9SGleb Smirnoff 				    "are reserved for internal use");
9263b3a8eb9SGleb Smirnoff 				YYERROR;
9273b3a8eb9SGleb Smirnoff 			}
9283b3a8eb9SGleb Smirnoff 
9293b3a8eb9SGleb Smirnoff 			memset(&r, 0, sizeof(r));
9303b3a8eb9SGleb Smirnoff 			if (pf->astack[pf->asd + 1]) {
9313b3a8eb9SGleb Smirnoff 				/* move inline rules into relative location */
932e9eb0941SKristof Provost 				pfctl_anchor_setup(&r,
9333b3a8eb9SGleb Smirnoff 				    &pf->astack[pf->asd]->ruleset,
9343b3a8eb9SGleb Smirnoff 				    $2 ? $2 : pf->alast->name);
9353b3a8eb9SGleb Smirnoff 
9363b3a8eb9SGleb Smirnoff 				if (r.anchor == NULL)
9373b3a8eb9SGleb Smirnoff 					err(1, "anchorrule: unable to "
9383b3a8eb9SGleb Smirnoff 					    "create ruleset");
9393b3a8eb9SGleb Smirnoff 
9403b3a8eb9SGleb Smirnoff 				if (pf->alast != r.anchor) {
9413b3a8eb9SGleb Smirnoff 					if (r.anchor->match) {
9423b3a8eb9SGleb Smirnoff 						yyerror("inline anchor '%s' "
9433b3a8eb9SGleb Smirnoff 						    "already exists",
9443b3a8eb9SGleb Smirnoff 						    r.anchor->name);
9453b3a8eb9SGleb Smirnoff 						YYERROR;
9463b3a8eb9SGleb Smirnoff 					}
9473b3a8eb9SGleb Smirnoff 					mv_rules(&pf->alast->ruleset,
9483b3a8eb9SGleb Smirnoff 					    &r.anchor->ruleset);
9493b3a8eb9SGleb Smirnoff 				}
9503b3a8eb9SGleb Smirnoff 				pf_remove_if_empty_ruleset(&pf->alast->ruleset);
9513b3a8eb9SGleb Smirnoff 				pf->alast = r.anchor;
9523b3a8eb9SGleb Smirnoff 			} else {
9533b3a8eb9SGleb Smirnoff 				if (!$2) {
9543b3a8eb9SGleb Smirnoff 					yyerror("anchors without explicit "
9553b3a8eb9SGleb Smirnoff 					    "rules must specify a name");
9563b3a8eb9SGleb Smirnoff 					YYERROR;
9573b3a8eb9SGleb Smirnoff 				}
9583b3a8eb9SGleb Smirnoff 			}
9593b3a8eb9SGleb Smirnoff 			r.direction = $3;
9603b3a8eb9SGleb Smirnoff 			r.quick = $4.quick;
9613b3a8eb9SGleb Smirnoff 			r.af = $6;
9623b3a8eb9SGleb Smirnoff 			r.prob = $9.prob;
9633b3a8eb9SGleb Smirnoff 			r.rtableid = $9.rtableid;
96476c5eeccSKristof Provost 			r.ridentifier = $9.ridentifier;
9653b3a8eb9SGleb Smirnoff 
9663b3a8eb9SGleb Smirnoff 			if ($9.tag)
9673b3a8eb9SGleb Smirnoff 				if (strlcpy(r.tagname, $9.tag,
9683b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
9693b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
9703b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
9713b3a8eb9SGleb Smirnoff 					YYERROR;
9723b3a8eb9SGleb Smirnoff 				}
9733b3a8eb9SGleb Smirnoff 			if ($9.match_tag)
9743b3a8eb9SGleb Smirnoff 				if (strlcpy(r.match_tagname, $9.match_tag,
9753b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
9763b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
9773b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
9783b3a8eb9SGleb Smirnoff 					YYERROR;
9793b3a8eb9SGleb Smirnoff 				}
9803b3a8eb9SGleb Smirnoff 			r.match_tag_not = $9.match_tag_not;
9813b3a8eb9SGleb Smirnoff 			if (rule_label(&r, $9.label))
9823b3a8eb9SGleb Smirnoff 				YYERROR;
9836fcc8e04SKristof Provost 			for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++)
9846fcc8e04SKristof Provost 				free($9.label[i]);
9853b3a8eb9SGleb Smirnoff 			r.flags = $9.flags.b1;
9863b3a8eb9SGleb Smirnoff 			r.flagset = $9.flags.b2;
9873b3a8eb9SGleb Smirnoff 			if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
9883b3a8eb9SGleb Smirnoff 				yyerror("flags always false");
9893b3a8eb9SGleb Smirnoff 				YYERROR;
9903b3a8eb9SGleb Smirnoff 			}
9913b3a8eb9SGleb Smirnoff 			if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
9923b3a8eb9SGleb Smirnoff 				for (proto = $7; proto != NULL &&
9933b3a8eb9SGleb Smirnoff 				    proto->proto != IPPROTO_TCP;
9943b3a8eb9SGleb Smirnoff 				    proto = proto->next)
9953b3a8eb9SGleb Smirnoff 					;	/* nothing */
9963b3a8eb9SGleb Smirnoff 				if (proto == NULL && $7 != NULL) {
9973b3a8eb9SGleb Smirnoff 					if ($9.flags.b1 || $9.flags.b2)
9983b3a8eb9SGleb Smirnoff 						yyerror(
9993b3a8eb9SGleb Smirnoff 						    "flags only apply to tcp");
10003b3a8eb9SGleb Smirnoff 					if ($8.src_os)
10013b3a8eb9SGleb Smirnoff 						yyerror(
10023b3a8eb9SGleb Smirnoff 						    "OS fingerprinting only "
10033b3a8eb9SGleb Smirnoff 						    "applies to tcp");
10043b3a8eb9SGleb Smirnoff 					YYERROR;
10053b3a8eb9SGleb Smirnoff 				}
10063b3a8eb9SGleb Smirnoff 			}
10073b3a8eb9SGleb Smirnoff 
10083b3a8eb9SGleb Smirnoff 			r.tos = $9.tos;
10093b3a8eb9SGleb Smirnoff 
10103b3a8eb9SGleb Smirnoff 			if ($9.keep.action) {
10113b3a8eb9SGleb Smirnoff 				yyerror("cannot specify state handling "
10123b3a8eb9SGleb Smirnoff 				    "on anchors");
10133b3a8eb9SGleb Smirnoff 				YYERROR;
10143b3a8eb9SGleb Smirnoff 			}
10153b3a8eb9SGleb Smirnoff 
10163b3a8eb9SGleb Smirnoff 			if ($9.match_tag)
10173b3a8eb9SGleb Smirnoff 				if (strlcpy(r.match_tagname, $9.match_tag,
10183b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
10193b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
10203b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
10213b3a8eb9SGleb Smirnoff 					YYERROR;
10223b3a8eb9SGleb Smirnoff 				}
10233b3a8eb9SGleb Smirnoff 			r.match_tag_not = $9.match_tag_not;
10243e248e0fSKristof Provost 			if ($9.marker & FOM_PRIO) {
10253e248e0fSKristof Provost 				if ($9.prio == 0)
10263e248e0fSKristof Provost 					r.prio = PF_PRIO_ZERO;
10273e248e0fSKristof Provost 				else
10283e248e0fSKristof Provost 					r.prio = $9.prio;
10293e248e0fSKristof Provost 			}
10303e248e0fSKristof Provost 			if ($9.marker & FOM_SETPRIO) {
10313e248e0fSKristof Provost 				r.set_prio[0] = $9.set_prio[0];
10323e248e0fSKristof Provost 				r.set_prio[1] = $9.set_prio[1];
10333e248e0fSKristof Provost 				r.scrub_flags |= PFSTATE_SETPRIO;
10343e248e0fSKristof Provost 			}
10353b3a8eb9SGleb Smirnoff 
10363b3a8eb9SGleb Smirnoff 			decide_address_family($8.src.host, &r.af);
10373b3a8eb9SGleb Smirnoff 			decide_address_family($8.dst.host, &r.af);
10383b3a8eb9SGleb Smirnoff 
10393b3a8eb9SGleb Smirnoff 			expand_rule(&r, $5, NULL, $7, $8.src_os,
10403b3a8eb9SGleb Smirnoff 			    $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
10413b3a8eb9SGleb Smirnoff 			    $9.uid, $9.gid, $9.icmpspec,
10423b3a8eb9SGleb Smirnoff 			    pf->astack[pf->asd + 1] ? pf->alast->name : $2);
10433b3a8eb9SGleb Smirnoff 			free($2);
10443b3a8eb9SGleb Smirnoff 			pf->astack[pf->asd + 1] = NULL;
10453b3a8eb9SGleb Smirnoff 		}
10463b3a8eb9SGleb Smirnoff 		| NATANCHOR string interface af proto fromto rtable {
1047e9eb0941SKristof Provost 			struct pfctl_rule	r;
10483b3a8eb9SGleb Smirnoff 
10493b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_NAT)) {
10503b3a8eb9SGleb Smirnoff 				free($2);
10513b3a8eb9SGleb Smirnoff 				YYERROR;
10523b3a8eb9SGleb Smirnoff 			}
10533b3a8eb9SGleb Smirnoff 
10543b3a8eb9SGleb Smirnoff 			memset(&r, 0, sizeof(r));
10553b3a8eb9SGleb Smirnoff 			r.action = PF_NAT;
10563b3a8eb9SGleb Smirnoff 			r.af = $4;
10573b3a8eb9SGleb Smirnoff 			r.rtableid = $7;
10583b3a8eb9SGleb Smirnoff 
10593b3a8eb9SGleb Smirnoff 			decide_address_family($6.src.host, &r.af);
10603b3a8eb9SGleb Smirnoff 			decide_address_family($6.dst.host, &r.af);
10613b3a8eb9SGleb Smirnoff 
10623b3a8eb9SGleb Smirnoff 			expand_rule(&r, $3, NULL, $5, $6.src_os,
10633b3a8eb9SGleb Smirnoff 			    $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
10643b3a8eb9SGleb Smirnoff 			    0, 0, 0, $2);
10653b3a8eb9SGleb Smirnoff 			free($2);
10663b3a8eb9SGleb Smirnoff 		}
10673b3a8eb9SGleb Smirnoff 		| RDRANCHOR string interface af proto fromto rtable {
1068e9eb0941SKristof Provost 			struct pfctl_rule	r;
10693b3a8eb9SGleb Smirnoff 
10703b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_NAT)) {
10713b3a8eb9SGleb Smirnoff 				free($2);
10723b3a8eb9SGleb Smirnoff 				YYERROR;
10733b3a8eb9SGleb Smirnoff 			}
10743b3a8eb9SGleb Smirnoff 
10753b3a8eb9SGleb Smirnoff 			memset(&r, 0, sizeof(r));
10763b3a8eb9SGleb Smirnoff 			r.action = PF_RDR;
10773b3a8eb9SGleb Smirnoff 			r.af = $4;
10783b3a8eb9SGleb Smirnoff 			r.rtableid = $7;
10793b3a8eb9SGleb Smirnoff 
10803b3a8eb9SGleb Smirnoff 			decide_address_family($6.src.host, &r.af);
10813b3a8eb9SGleb Smirnoff 			decide_address_family($6.dst.host, &r.af);
10823b3a8eb9SGleb Smirnoff 
10833b3a8eb9SGleb Smirnoff 			if ($6.src.port != NULL) {
10843b3a8eb9SGleb Smirnoff 				yyerror("source port parameter not supported"
10853b3a8eb9SGleb Smirnoff 				    " in rdr-anchor");
10863b3a8eb9SGleb Smirnoff 				YYERROR;
10873b3a8eb9SGleb Smirnoff 			}
10883b3a8eb9SGleb Smirnoff 			if ($6.dst.port != NULL) {
10893b3a8eb9SGleb Smirnoff 				if ($6.dst.port->next != NULL) {
10903b3a8eb9SGleb Smirnoff 					yyerror("destination port list "
10913b3a8eb9SGleb Smirnoff 					    "expansion not supported in "
10923b3a8eb9SGleb Smirnoff 					    "rdr-anchor");
10933b3a8eb9SGleb Smirnoff 					YYERROR;
10943b3a8eb9SGleb Smirnoff 				} else if ($6.dst.port->op != PF_OP_EQ) {
10953b3a8eb9SGleb Smirnoff 					yyerror("destination port operators"
10963b3a8eb9SGleb Smirnoff 					    " not supported in rdr-anchor");
10973b3a8eb9SGleb Smirnoff 					YYERROR;
10983b3a8eb9SGleb Smirnoff 				}
10993b3a8eb9SGleb Smirnoff 				r.dst.port[0] = $6.dst.port->port[0];
11003b3a8eb9SGleb Smirnoff 				r.dst.port[1] = $6.dst.port->port[1];
11013b3a8eb9SGleb Smirnoff 				r.dst.port_op = $6.dst.port->op;
11023b3a8eb9SGleb Smirnoff 			}
11033b3a8eb9SGleb Smirnoff 
11043b3a8eb9SGleb Smirnoff 			expand_rule(&r, $3, NULL, $5, $6.src_os,
11053b3a8eb9SGleb Smirnoff 			    $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
11063b3a8eb9SGleb Smirnoff 			    0, 0, 0, $2);
11073b3a8eb9SGleb Smirnoff 			free($2);
11083b3a8eb9SGleb Smirnoff 		}
11093b3a8eb9SGleb Smirnoff 		| BINATANCHOR string interface af proto fromto rtable {
1110e9eb0941SKristof Provost 			struct pfctl_rule	r;
11113b3a8eb9SGleb Smirnoff 
11123b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_NAT)) {
11133b3a8eb9SGleb Smirnoff 				free($2);
11143b3a8eb9SGleb Smirnoff 				YYERROR;
11153b3a8eb9SGleb Smirnoff 			}
11163b3a8eb9SGleb Smirnoff 
11173b3a8eb9SGleb Smirnoff 			memset(&r, 0, sizeof(r));
11183b3a8eb9SGleb Smirnoff 			r.action = PF_BINAT;
11193b3a8eb9SGleb Smirnoff 			r.af = $4;
11203b3a8eb9SGleb Smirnoff 			r.rtableid = $7;
11213b3a8eb9SGleb Smirnoff 			if ($5 != NULL) {
11223b3a8eb9SGleb Smirnoff 				if ($5->next != NULL) {
11233b3a8eb9SGleb Smirnoff 					yyerror("proto list expansion"
11243b3a8eb9SGleb Smirnoff 					    " not supported in binat-anchor");
11253b3a8eb9SGleb Smirnoff 					YYERROR;
11263b3a8eb9SGleb Smirnoff 				}
11273b3a8eb9SGleb Smirnoff 				r.proto = $5->proto;
11283b3a8eb9SGleb Smirnoff 				free($5);
11293b3a8eb9SGleb Smirnoff 			}
11303b3a8eb9SGleb Smirnoff 
11313b3a8eb9SGleb Smirnoff 			if ($6.src.host != NULL || $6.src.port != NULL ||
11323b3a8eb9SGleb Smirnoff 			    $6.dst.host != NULL || $6.dst.port != NULL) {
11333b3a8eb9SGleb Smirnoff 				yyerror("fromto parameter not supported"
11343b3a8eb9SGleb Smirnoff 				    " in binat-anchor");
11353b3a8eb9SGleb Smirnoff 				YYERROR;
11363b3a8eb9SGleb Smirnoff 			}
11373b3a8eb9SGleb Smirnoff 
11383b3a8eb9SGleb Smirnoff 			decide_address_family($6.src.host, &r.af);
11393b3a8eb9SGleb Smirnoff 			decide_address_family($6.dst.host, &r.af);
11403b3a8eb9SGleb Smirnoff 
11410d71f9f3SKristof Provost 			pfctl_append_rule(pf, &r, $2);
11423b3a8eb9SGleb Smirnoff 			free($2);
11433b3a8eb9SGleb Smirnoff 		}
11443b3a8eb9SGleb Smirnoff 		;
11453b3a8eb9SGleb Smirnoff 
11463b3a8eb9SGleb Smirnoff loadrule	: LOAD ANCHOR string FROM string	{
11473b3a8eb9SGleb Smirnoff 			struct loadanchors	*loadanchor;
11483b3a8eb9SGleb Smirnoff 
11493b3a8eb9SGleb Smirnoff 			if (strlen(pf->anchor->name) + 1 +
11503b3a8eb9SGleb Smirnoff 			    strlen($3) >= MAXPATHLEN) {
11513b3a8eb9SGleb Smirnoff 				yyerror("anchorname %s too long, max %u\n",
11523b3a8eb9SGleb Smirnoff 				    $3, MAXPATHLEN - 1);
11533b3a8eb9SGleb Smirnoff 				free($3);
11543b3a8eb9SGleb Smirnoff 				YYERROR;
11553b3a8eb9SGleb Smirnoff 			}
11563b3a8eb9SGleb Smirnoff 			loadanchor = calloc(1, sizeof(struct loadanchors));
11573b3a8eb9SGleb Smirnoff 			if (loadanchor == NULL)
11583b3a8eb9SGleb Smirnoff 				err(1, "loadrule: calloc");
11593b3a8eb9SGleb Smirnoff 			if ((loadanchor->anchorname = malloc(MAXPATHLEN)) ==
11603b3a8eb9SGleb Smirnoff 			    NULL)
11613b3a8eb9SGleb Smirnoff 				err(1, "loadrule: malloc");
11623b3a8eb9SGleb Smirnoff 			if (pf->anchor->name[0])
11633b3a8eb9SGleb Smirnoff 				snprintf(loadanchor->anchorname, MAXPATHLEN,
11643b3a8eb9SGleb Smirnoff 				    "%s/%s", pf->anchor->name, $3);
11653b3a8eb9SGleb Smirnoff 			else
11663b3a8eb9SGleb Smirnoff 				strlcpy(loadanchor->anchorname, $3, MAXPATHLEN);
11673b3a8eb9SGleb Smirnoff 			if ((loadanchor->filename = strdup($5)) == NULL)
11683b3a8eb9SGleb Smirnoff 				err(1, "loadrule: strdup");
11693b3a8eb9SGleb Smirnoff 
11703b3a8eb9SGleb Smirnoff 			TAILQ_INSERT_TAIL(&loadanchorshead, loadanchor,
11713b3a8eb9SGleb Smirnoff 			    entries);
11723b3a8eb9SGleb Smirnoff 
11733b3a8eb9SGleb Smirnoff 			free($3);
11743b3a8eb9SGleb Smirnoff 			free($5);
11753b3a8eb9SGleb Smirnoff 		};
11763b3a8eb9SGleb Smirnoff 
11773b3a8eb9SGleb Smirnoff scrubaction	: no SCRUB {
11783b3a8eb9SGleb Smirnoff 			$$.b2 = $$.w = 0;
11793b3a8eb9SGleb Smirnoff 			if ($1)
11803b3a8eb9SGleb Smirnoff 				$$.b1 = PF_NOSCRUB;
11813b3a8eb9SGleb Smirnoff 			else
11823b3a8eb9SGleb Smirnoff 				$$.b1 = PF_SCRUB;
11833b3a8eb9SGleb Smirnoff 		}
11843b3a8eb9SGleb Smirnoff 		;
11853b3a8eb9SGleb Smirnoff 
11868a42005dSKristof Provost etherrule	: ETHER action dir quick interface etherproto etherfromto l3fromto etherfilter_opts
11872b29ceb8SKristof Provost 		{
11882b29ceb8SKristof Provost 			struct pfctl_eth_rule	r;
11892b29ceb8SKristof Provost 
11902b29ceb8SKristof Provost 			bzero(&r, sizeof(r));
11912b29ceb8SKristof Provost 
11922b29ceb8SKristof Provost 			if (check_rulestate(PFCTL_STATE_ETHER))
11932b29ceb8SKristof Provost 				YYERROR;
11942b29ceb8SKristof Provost 
11952b29ceb8SKristof Provost 			r.action = $2.b1;
11962b29ceb8SKristof Provost 			r.direction = $3;
11972b29ceb8SKristof Provost 			r.quick = $4.quick;
11988a42005dSKristof Provost 			if ($9.tag != NULL)
11998a42005dSKristof Provost 				memcpy(&r.tagname, $9.tag, sizeof(r.tagname));
12008a42005dSKristof Provost 			if ($9.queues.qname != NULL)
12018a42005dSKristof Provost 				memcpy(&r.qname, $9.queues.qname, sizeof(r.qname));
12028a42005dSKristof Provost 			r.dnpipe = $9.dnpipe;
12038a42005dSKristof Provost 			r.dnflags = $9.free_flags;
12042b29ceb8SKristof Provost 
12058a42005dSKristof Provost 			expand_eth_rule(&r, $5, $6, $7.src, $7.dst,
12068a42005dSKristof Provost 			    $8.src.host, $8.dst.host, "");
1207c5131afeSKristof Provost 		}
1208c5131afeSKristof Provost 		;
1209c5131afeSKristof Provost 
1210c5131afeSKristof Provost etherpfa_anchorlist	: /* empty */
1211c5131afeSKristof Provost 		| etherpfa_anchorlist '\n'
1212c5131afeSKristof Provost 		| etherpfa_anchorlist etherrule '\n'
1213c5131afeSKristof Provost 		| etherpfa_anchorlist etheranchorrule '\n'
1214c5131afeSKristof Provost 		;
1215c5131afeSKristof Provost 
1216c5131afeSKristof Provost etherpfa_anchor	: '{'
1217c5131afeSKristof Provost 		{
1218c5131afeSKristof Provost 			char ta[PF_ANCHOR_NAME_SIZE];
1219c5131afeSKristof Provost 			struct pfctl_eth_ruleset *rs;
1220c5131afeSKristof Provost 
1221c5131afeSKristof Provost 			/* steping into a brace anchor */
1222c5131afeSKristof Provost 			pf->asd++;
1223c5131afeSKristof Provost 			pf->bn++;
1224c5131afeSKristof Provost 
1225c5131afeSKristof Provost 			/* create a holding ruleset in the root */
1226c5131afeSKristof Provost 			snprintf(ta, PF_ANCHOR_NAME_SIZE, "_%d", pf->bn);
1227c5131afeSKristof Provost 			rs = pf_find_or_create_eth_ruleset(ta);
1228c5131afeSKristof Provost 			if (rs == NULL)
1229c5131afeSKristof Provost 				err(1, "etherpfa_anchor: pf_find_or_create_eth_ruleset");
1230c5131afeSKristof Provost 			pf->eastack[pf->asd] = rs->anchor;
1231c5131afeSKristof Provost 			pf->eanchor = rs->anchor;
1232c5131afeSKristof Provost 		} '\n' etherpfa_anchorlist '}'
1233c5131afeSKristof Provost 		{
1234c5131afeSKristof Provost 			pf->ealast = pf->eanchor;
1235c5131afeSKristof Provost 			pf->asd--;
1236c5131afeSKristof Provost 			pf->eanchor = pf->eastack[pf->asd];
1237c5131afeSKristof Provost 		}
1238c5131afeSKristof Provost 		| /* empty */
1239c5131afeSKristof Provost 		;
1240c5131afeSKristof Provost 
12418a42005dSKristof Provost etheranchorrule	: ETHER ANCHOR anchorname dir quick interface etherproto etherfromto l3fromto etherpfa_anchor
1242c5131afeSKristof Provost 		{
1243c5131afeSKristof Provost 			struct pfctl_eth_rule	r;
1244c5131afeSKristof Provost 
1245c5131afeSKristof Provost 			if (check_rulestate(PFCTL_STATE_ETHER)) {
1246c5131afeSKristof Provost 				free($3);
1247c5131afeSKristof Provost 				YYERROR;
1248c5131afeSKristof Provost 			}
1249c5131afeSKristof Provost 
1250c5131afeSKristof Provost 			if ($3 && ($3[0] == '_' || strstr($3, "/_") != NULL)) {
1251c5131afeSKristof Provost 				free($3);
1252c5131afeSKristof Provost 				yyerror("anchor names beginning with '_' "
1253c5131afeSKristof Provost 				    "are reserved for internal use");
1254c5131afeSKristof Provost 				YYERROR;
1255c5131afeSKristof Provost 			}
1256c5131afeSKristof Provost 
1257c5131afeSKristof Provost 			memset(&r, 0, sizeof(r));
1258c5131afeSKristof Provost 			if (pf->eastack[pf->asd + 1]) {
1259c5131afeSKristof Provost 				/* move inline rules into relative location */
1260c5131afeSKristof Provost 				pfctl_eth_anchor_setup(pf, &r,
1261c5131afeSKristof Provost 				    &pf->eastack[pf->asd]->ruleset,
1262c5131afeSKristof Provost 				    $3 ? $3 : pf->ealast->name);
1263c5131afeSKristof Provost 				if (r.anchor == NULL)
1264c5131afeSKristof Provost 					err(1, "etheranchorrule: unable to "
1265c5131afeSKristof Provost 					    "create ruleset");
1266c5131afeSKristof Provost 
1267c5131afeSKristof Provost 				if (pf->ealast != r.anchor) {
1268c5131afeSKristof Provost 					if (r.anchor->match) {
1269c5131afeSKristof Provost 						yyerror("inline anchor '%s' "
1270c5131afeSKristof Provost 						    "already exists",
1271c5131afeSKristof Provost 						    r.anchor->name);
1272c5131afeSKristof Provost 						YYERROR;
1273c5131afeSKristof Provost 					}
1274c5131afeSKristof Provost 					mv_eth_rules(&pf->ealast->ruleset,
1275c5131afeSKristof Provost 					    &r.anchor->ruleset);
1276c5131afeSKristof Provost 				}
1277c5131afeSKristof Provost 				pf_remove_if_empty_eth_ruleset(&pf->ealast->ruleset);
1278c5131afeSKristof Provost 				pf->ealast = r.anchor;
1279c5131afeSKristof Provost 			} else {
1280c5131afeSKristof Provost 				if (!$3) {
1281c5131afeSKristof Provost 					yyerror("anchors without explicit "
1282c5131afeSKristof Provost 					    "rules must specify a name");
1283c5131afeSKristof Provost 					YYERROR;
1284c5131afeSKristof Provost 				}
1285c5131afeSKristof Provost 			}
1286c5131afeSKristof Provost 
1287c5131afeSKristof Provost 			r.direction = $4;
1288c5131afeSKristof Provost 			r.quick = $5.quick;
1289c5131afeSKristof Provost 
1290c5131afeSKristof Provost 			expand_eth_rule(&r, $6, $7, $8.src, $8.dst,
12918a42005dSKristof Provost 			    $9.src.host, $9.dst.host,
1292c5131afeSKristof Provost 			    pf->eastack[pf->asd + 1] ? pf->ealast->name : $3);
1293c5131afeSKristof Provost 
1294c5131afeSKristof Provost 			free($3);
1295c5131afeSKristof Provost 			pf->eastack[pf->asd + 1] = NULL;
12962b29ceb8SKristof Provost 		}
12972b29ceb8SKristof Provost 		;
12982b29ceb8SKristof Provost 
12992b29ceb8SKristof Provost etherfilter_opts	:	{
13002b29ceb8SKristof Provost 				bzero(&filter_opts, sizeof filter_opts);
13012b29ceb8SKristof Provost 			}
13022b29ceb8SKristof Provost 		    etherfilter_opts_l
13032b29ceb8SKristof Provost 			{ $$ = filter_opts; }
13042b29ceb8SKristof Provost 		| /* empty */	{
13052b29ceb8SKristof Provost 			bzero(&filter_opts, sizeof filter_opts);
13062b29ceb8SKristof Provost 			$$ = filter_opts;
13072b29ceb8SKristof Provost 		}
13082b29ceb8SKristof Provost 		;
13092b29ceb8SKristof Provost 
13102b29ceb8SKristof Provost etherfilter_opts_l	: etherfilter_opts_l etherfilter_opt
13112b29ceb8SKristof Provost 			| etherfilter_opt
13122b29ceb8SKristof Provost 
13132b29ceb8SKristof Provost etherfilter_opt	: etherqname	{
13142b29ceb8SKristof Provost 			if (filter_opts.queues.qname) {
13152b29ceb8SKristof Provost 				yyerror("queue cannot be redefined");
13162b29ceb8SKristof Provost 				YYERROR;
13172b29ceb8SKristof Provost 			}
13182b29ceb8SKristof Provost 			filter_opts.queues = $1;
13192b29ceb8SKristof Provost 		}
13202b29ceb8SKristof Provost 		| TAG string				{
13212b29ceb8SKristof Provost 			filter_opts.tag = $2;
13222b29ceb8SKristof Provost 		}
1323fb330f39SKristof Provost 		| DNPIPE number {
1324fb330f39SKristof Provost 			filter_opts.dnpipe = $2;
1325fb330f39SKristof Provost 			filter_opts.free_flags |= PFRULE_DN_IS_PIPE;
1326fb330f39SKristof Provost 		}
1327fb330f39SKristof Provost 		| DNQUEUE number {
1328fb330f39SKristof Provost 			filter_opts.dnpipe = $2;
1329fb330f39SKristof Provost 			filter_opts.free_flags |= PFRULE_DN_IS_QUEUE;
1330fb330f39SKristof Provost 		}
13312b29ceb8SKristof Provost 		;
13322b29ceb8SKristof Provost 
13333b3a8eb9SGleb Smirnoff scrubrule	: scrubaction dir logquick interface af proto fromto scrub_opts
13343b3a8eb9SGleb Smirnoff 		{
1335e9eb0941SKristof Provost 			struct pfctl_rule	r;
13363b3a8eb9SGleb Smirnoff 
13373b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_SCRUB))
13383b3a8eb9SGleb Smirnoff 				YYERROR;
13393b3a8eb9SGleb Smirnoff 
13403b3a8eb9SGleb Smirnoff 			memset(&r, 0, sizeof(r));
13413b3a8eb9SGleb Smirnoff 
13423b3a8eb9SGleb Smirnoff 			r.action = $1.b1;
13433b3a8eb9SGleb Smirnoff 			r.direction = $2;
13443b3a8eb9SGleb Smirnoff 
13453b3a8eb9SGleb Smirnoff 			r.log = $3.log;
13463b3a8eb9SGleb Smirnoff 			r.logif = $3.logif;
13473b3a8eb9SGleb Smirnoff 			if ($3.quick) {
13483b3a8eb9SGleb Smirnoff 				yyerror("scrub rules do not support 'quick'");
13493b3a8eb9SGleb Smirnoff 				YYERROR;
13503b3a8eb9SGleb Smirnoff 			}
13513b3a8eb9SGleb Smirnoff 
13523b3a8eb9SGleb Smirnoff 			r.af = $5;
13533b3a8eb9SGleb Smirnoff 			if ($8.nodf)
13543b3a8eb9SGleb Smirnoff 				r.rule_flag |= PFRULE_NODF;
13553b3a8eb9SGleb Smirnoff 			if ($8.randomid)
13563b3a8eb9SGleb Smirnoff 				r.rule_flag |= PFRULE_RANDOMID;
13573b3a8eb9SGleb Smirnoff 			if ($8.reassemble_tcp) {
13583b3a8eb9SGleb Smirnoff 				if (r.direction != PF_INOUT) {
13593b3a8eb9SGleb Smirnoff 					yyerror("reassemble tcp rules can not "
13603b3a8eb9SGleb Smirnoff 					    "specify direction");
13613b3a8eb9SGleb Smirnoff 					YYERROR;
13623b3a8eb9SGleb Smirnoff 				}
13633b3a8eb9SGleb Smirnoff 				r.rule_flag |= PFRULE_REASSEMBLE_TCP;
13643b3a8eb9SGleb Smirnoff 			}
13653b3a8eb9SGleb Smirnoff 			if ($8.minttl)
13663b3a8eb9SGleb Smirnoff 				r.min_ttl = $8.minttl;
13673b3a8eb9SGleb Smirnoff 			if ($8.maxmss)
13683b3a8eb9SGleb Smirnoff 				r.max_mss = $8.maxmss;
13693b3a8eb9SGleb Smirnoff 			if ($8.marker & SOM_SETTOS) {
13703b3a8eb9SGleb Smirnoff 				r.rule_flag |= PFRULE_SET_TOS;
13713b3a8eb9SGleb Smirnoff 				r.set_tos = $8.settos;
13723b3a8eb9SGleb Smirnoff 			}
13733b3a8eb9SGleb Smirnoff 			if ($8.fragcache)
13743b3a8eb9SGleb Smirnoff 				r.rule_flag |= $8.fragcache;
13753b3a8eb9SGleb Smirnoff 			if ($8.match_tag)
13763b3a8eb9SGleb Smirnoff 				if (strlcpy(r.match_tagname, $8.match_tag,
13773b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
13783b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
13793b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
13803b3a8eb9SGleb Smirnoff 					YYERROR;
13813b3a8eb9SGleb Smirnoff 				}
13823b3a8eb9SGleb Smirnoff 			r.match_tag_not = $8.match_tag_not;
13833b3a8eb9SGleb Smirnoff 			r.rtableid = $8.rtableid;
13843b3a8eb9SGleb Smirnoff 
13853b3a8eb9SGleb Smirnoff 			expand_rule(&r, $4, NULL, $6, $7.src_os,
13863b3a8eb9SGleb Smirnoff 			    $7.src.host, $7.src.port, $7.dst.host, $7.dst.port,
13873b3a8eb9SGleb Smirnoff 			    NULL, NULL, NULL, "");
13883b3a8eb9SGleb Smirnoff 		}
13893b3a8eb9SGleb Smirnoff 		;
13903b3a8eb9SGleb Smirnoff 
13913b3a8eb9SGleb Smirnoff scrub_opts	:	{
13923b3a8eb9SGleb Smirnoff 				bzero(&scrub_opts, sizeof scrub_opts);
13933b3a8eb9SGleb Smirnoff 				scrub_opts.rtableid = -1;
13943b3a8eb9SGleb Smirnoff 			}
13953b3a8eb9SGleb Smirnoff 		    scrub_opts_l
13963b3a8eb9SGleb Smirnoff 			{ $$ = scrub_opts; }
13973b3a8eb9SGleb Smirnoff 		| /* empty */ {
13983b3a8eb9SGleb Smirnoff 			bzero(&scrub_opts, sizeof scrub_opts);
13993b3a8eb9SGleb Smirnoff 			scrub_opts.rtableid = -1;
14003b3a8eb9SGleb Smirnoff 			$$ = scrub_opts;
14013b3a8eb9SGleb Smirnoff 		}
14023b3a8eb9SGleb Smirnoff 		;
14033b3a8eb9SGleb Smirnoff 
14043b3a8eb9SGleb Smirnoff scrub_opts_l	: scrub_opts_l scrub_opt
14053b3a8eb9SGleb Smirnoff 		| scrub_opt
14063b3a8eb9SGleb Smirnoff 		;
14073b3a8eb9SGleb Smirnoff 
14083b3a8eb9SGleb Smirnoff scrub_opt	: NODF	{
14093b3a8eb9SGleb Smirnoff 			if (scrub_opts.nodf) {
14103b3a8eb9SGleb Smirnoff 				yyerror("no-df cannot be respecified");
14113b3a8eb9SGleb Smirnoff 				YYERROR;
14123b3a8eb9SGleb Smirnoff 			}
14133b3a8eb9SGleb Smirnoff 			scrub_opts.nodf = 1;
14143b3a8eb9SGleb Smirnoff 		}
14153b3a8eb9SGleb Smirnoff 		| MINTTL NUMBER {
14163b3a8eb9SGleb Smirnoff 			if (scrub_opts.marker & SOM_MINTTL) {
14173b3a8eb9SGleb Smirnoff 				yyerror("min-ttl cannot be respecified");
14183b3a8eb9SGleb Smirnoff 				YYERROR;
14193b3a8eb9SGleb Smirnoff 			}
14203b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > 255) {
14213b3a8eb9SGleb Smirnoff 				yyerror("illegal min-ttl value %d", $2);
14223b3a8eb9SGleb Smirnoff 				YYERROR;
14233b3a8eb9SGleb Smirnoff 			}
14243b3a8eb9SGleb Smirnoff 			scrub_opts.marker |= SOM_MINTTL;
14253b3a8eb9SGleb Smirnoff 			scrub_opts.minttl = $2;
14263b3a8eb9SGleb Smirnoff 		}
14273b3a8eb9SGleb Smirnoff 		| MAXMSS NUMBER {
14283b3a8eb9SGleb Smirnoff 			if (scrub_opts.marker & SOM_MAXMSS) {
14293b3a8eb9SGleb Smirnoff 				yyerror("max-mss cannot be respecified");
14303b3a8eb9SGleb Smirnoff 				YYERROR;
14313b3a8eb9SGleb Smirnoff 			}
14323b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > 65535) {
14333b3a8eb9SGleb Smirnoff 				yyerror("illegal max-mss value %d", $2);
14343b3a8eb9SGleb Smirnoff 				YYERROR;
14353b3a8eb9SGleb Smirnoff 			}
14363b3a8eb9SGleb Smirnoff 			scrub_opts.marker |= SOM_MAXMSS;
14373b3a8eb9SGleb Smirnoff 			scrub_opts.maxmss = $2;
14383b3a8eb9SGleb Smirnoff 		}
14393b3a8eb9SGleb Smirnoff 		| SETTOS tos {
14403b3a8eb9SGleb Smirnoff 			if (scrub_opts.marker & SOM_SETTOS) {
14413b3a8eb9SGleb Smirnoff 				yyerror("set-tos cannot be respecified");
14423b3a8eb9SGleb Smirnoff 				YYERROR;
14433b3a8eb9SGleb Smirnoff 			}
14443b3a8eb9SGleb Smirnoff 			scrub_opts.marker |= SOM_SETTOS;
14453b3a8eb9SGleb Smirnoff 			scrub_opts.settos = $2;
14463b3a8eb9SGleb Smirnoff 		}
14473b3a8eb9SGleb Smirnoff 		| fragcache {
14483b3a8eb9SGleb Smirnoff 			if (scrub_opts.marker & SOM_FRAGCACHE) {
14493b3a8eb9SGleb Smirnoff 				yyerror("fragcache cannot be respecified");
14503b3a8eb9SGleb Smirnoff 				YYERROR;
14513b3a8eb9SGleb Smirnoff 			}
14523b3a8eb9SGleb Smirnoff 			scrub_opts.marker |= SOM_FRAGCACHE;
14533b3a8eb9SGleb Smirnoff 			scrub_opts.fragcache = $1;
14543b3a8eb9SGleb Smirnoff 		}
14553b3a8eb9SGleb Smirnoff 		| REASSEMBLE STRING {
14563b3a8eb9SGleb Smirnoff 			if (strcasecmp($2, "tcp") != 0) {
14573b3a8eb9SGleb Smirnoff 				yyerror("scrub reassemble supports only tcp, "
14583b3a8eb9SGleb Smirnoff 				    "not '%s'", $2);
14593b3a8eb9SGleb Smirnoff 				free($2);
14603b3a8eb9SGleb Smirnoff 				YYERROR;
14613b3a8eb9SGleb Smirnoff 			}
14623b3a8eb9SGleb Smirnoff 			free($2);
14633b3a8eb9SGleb Smirnoff 			if (scrub_opts.reassemble_tcp) {
14643b3a8eb9SGleb Smirnoff 				yyerror("reassemble tcp cannot be respecified");
14653b3a8eb9SGleb Smirnoff 				YYERROR;
14663b3a8eb9SGleb Smirnoff 			}
14673b3a8eb9SGleb Smirnoff 			scrub_opts.reassemble_tcp = 1;
14683b3a8eb9SGleb Smirnoff 		}
14693b3a8eb9SGleb Smirnoff 		| RANDOMID {
14703b3a8eb9SGleb Smirnoff 			if (scrub_opts.randomid) {
14713b3a8eb9SGleb Smirnoff 				yyerror("random-id cannot be respecified");
14723b3a8eb9SGleb Smirnoff 				YYERROR;
14733b3a8eb9SGleb Smirnoff 			}
14743b3a8eb9SGleb Smirnoff 			scrub_opts.randomid = 1;
14753b3a8eb9SGleb Smirnoff 		}
14763b3a8eb9SGleb Smirnoff 		| RTABLE NUMBER				{
14773b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > rt_tableid_max()) {
14783b3a8eb9SGleb Smirnoff 				yyerror("invalid rtable id");
14793b3a8eb9SGleb Smirnoff 				YYERROR;
14803b3a8eb9SGleb Smirnoff 			}
14813b3a8eb9SGleb Smirnoff 			scrub_opts.rtableid = $2;
14823b3a8eb9SGleb Smirnoff 		}
14833b3a8eb9SGleb Smirnoff 		| not TAGGED string			{
14843b3a8eb9SGleb Smirnoff 			scrub_opts.match_tag = $3;
14853b3a8eb9SGleb Smirnoff 			scrub_opts.match_tag_not = $1;
14863b3a8eb9SGleb Smirnoff 		}
14873b3a8eb9SGleb Smirnoff 		;
14883b3a8eb9SGleb Smirnoff 
14893b3a8eb9SGleb Smirnoff fragcache	: FRAGMENT REASSEMBLE	{ $$ = 0; /* default */ }
149064b3b4d6SKristof Provost 		| FRAGMENT FRAGCROP	{ $$ = 0; }
149164b3b4d6SKristof Provost 		| FRAGMENT FRAGDROP	{ $$ = 0; }
14923b3a8eb9SGleb Smirnoff 		;
14933b3a8eb9SGleb Smirnoff 
14943b3a8eb9SGleb Smirnoff antispoof	: ANTISPOOF logquick antispoof_ifspc af antispoof_opts {
1495e9eb0941SKristof Provost 			struct pfctl_rule	 r;
14963b3a8eb9SGleb Smirnoff 			struct node_host	*h = NULL, *hh;
14973b3a8eb9SGleb Smirnoff 			struct node_if		*i, *j;
14983b3a8eb9SGleb Smirnoff 
14993b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_FILTER))
15003b3a8eb9SGleb Smirnoff 				YYERROR;
15013b3a8eb9SGleb Smirnoff 
15023b3a8eb9SGleb Smirnoff 			for (i = $3; i; i = i->next) {
15033b3a8eb9SGleb Smirnoff 				bzero(&r, sizeof(r));
15043b3a8eb9SGleb Smirnoff 
15053b3a8eb9SGleb Smirnoff 				r.action = PF_DROP;
15063b3a8eb9SGleb Smirnoff 				r.direction = PF_IN;
15073b3a8eb9SGleb Smirnoff 				r.log = $2.log;
15083b3a8eb9SGleb Smirnoff 				r.logif = $2.logif;
15093b3a8eb9SGleb Smirnoff 				r.quick = $2.quick;
15103b3a8eb9SGleb Smirnoff 				r.af = $4;
151176c5eeccSKristof Provost 				r.ridentifier = $5.ridentifier;
15123b3a8eb9SGleb Smirnoff 				if (rule_label(&r, $5.label))
15133b3a8eb9SGleb Smirnoff 					YYERROR;
15143b3a8eb9SGleb Smirnoff 				r.rtableid = $5.rtableid;
15153b3a8eb9SGleb Smirnoff 				j = calloc(1, sizeof(struct node_if));
15163b3a8eb9SGleb Smirnoff 				if (j == NULL)
15173b3a8eb9SGleb Smirnoff 					err(1, "antispoof: calloc");
15183b3a8eb9SGleb Smirnoff 				if (strlcpy(j->ifname, i->ifname,
15193b3a8eb9SGleb Smirnoff 				    sizeof(j->ifname)) >= sizeof(j->ifname)) {
15203b3a8eb9SGleb Smirnoff 					free(j);
15213b3a8eb9SGleb Smirnoff 					yyerror("interface name too long");
15223b3a8eb9SGleb Smirnoff 					YYERROR;
15233b3a8eb9SGleb Smirnoff 				}
15243b3a8eb9SGleb Smirnoff 				j->not = 1;
15253b3a8eb9SGleb Smirnoff 				if (i->dynamic) {
15263b3a8eb9SGleb Smirnoff 					h = calloc(1, sizeof(*h));
15273b3a8eb9SGleb Smirnoff 					if (h == NULL)
15283b3a8eb9SGleb Smirnoff 						err(1, "address: calloc");
15293b3a8eb9SGleb Smirnoff 					h->addr.type = PF_ADDR_DYNIFTL;
15303b3a8eb9SGleb Smirnoff 					set_ipmask(h, 128);
15313b3a8eb9SGleb Smirnoff 					if (strlcpy(h->addr.v.ifname, i->ifname,
15323b3a8eb9SGleb Smirnoff 					    sizeof(h->addr.v.ifname)) >=
15333b3a8eb9SGleb Smirnoff 					    sizeof(h->addr.v.ifname)) {
15343b3a8eb9SGleb Smirnoff 						free(h);
15353b3a8eb9SGleb Smirnoff 						yyerror(
15363b3a8eb9SGleb Smirnoff 						    "interface name too long");
15373b3a8eb9SGleb Smirnoff 						YYERROR;
15383b3a8eb9SGleb Smirnoff 					}
15393b3a8eb9SGleb Smirnoff 					hh = malloc(sizeof(*hh));
15403b3a8eb9SGleb Smirnoff 					if (hh == NULL)
15413b3a8eb9SGleb Smirnoff 						 err(1, "address: malloc");
15423b3a8eb9SGleb Smirnoff 					bcopy(h, hh, sizeof(*hh));
15433b3a8eb9SGleb Smirnoff 					h->addr.iflags = PFI_AFLAG_NETWORK;
15443b3a8eb9SGleb Smirnoff 				} else {
15453b3a8eb9SGleb Smirnoff 					h = ifa_lookup(j->ifname,
15463b3a8eb9SGleb Smirnoff 					    PFI_AFLAG_NETWORK);
15473b3a8eb9SGleb Smirnoff 					hh = NULL;
15483b3a8eb9SGleb Smirnoff 				}
15493b3a8eb9SGleb Smirnoff 
15503b3a8eb9SGleb Smirnoff 				if (h != NULL)
15513b3a8eb9SGleb Smirnoff 					expand_rule(&r, j, NULL, NULL, NULL, h,
15523b3a8eb9SGleb Smirnoff 					    NULL, NULL, NULL, NULL, NULL,
15533b3a8eb9SGleb Smirnoff 					    NULL, "");
15543b3a8eb9SGleb Smirnoff 
15553b3a8eb9SGleb Smirnoff 				if ((i->ifa_flags & IFF_LOOPBACK) == 0) {
15563b3a8eb9SGleb Smirnoff 					bzero(&r, sizeof(r));
15573b3a8eb9SGleb Smirnoff 
15583b3a8eb9SGleb Smirnoff 					r.action = PF_DROP;
15593b3a8eb9SGleb Smirnoff 					r.direction = PF_IN;
15603b3a8eb9SGleb Smirnoff 					r.log = $2.log;
15613b3a8eb9SGleb Smirnoff 					r.logif = $2.logif;
15623b3a8eb9SGleb Smirnoff 					r.quick = $2.quick;
15633b3a8eb9SGleb Smirnoff 					r.af = $4;
156476c5eeccSKristof Provost 					r.ridentifier = $5.ridentifier;
15653b3a8eb9SGleb Smirnoff 					if (rule_label(&r, $5.label))
15663b3a8eb9SGleb Smirnoff 						YYERROR;
15673b3a8eb9SGleb Smirnoff 					r.rtableid = $5.rtableid;
15683b3a8eb9SGleb Smirnoff 					if (hh != NULL)
15693b3a8eb9SGleb Smirnoff 						h = hh;
15703b3a8eb9SGleb Smirnoff 					else
15713b3a8eb9SGleb Smirnoff 						h = ifa_lookup(i->ifname, 0);
15723b3a8eb9SGleb Smirnoff 					if (h != NULL)
15733b3a8eb9SGleb Smirnoff 						expand_rule(&r, NULL, NULL,
15743b3a8eb9SGleb Smirnoff 						    NULL, NULL, h, NULL, NULL,
15753b3a8eb9SGleb Smirnoff 						    NULL, NULL, NULL, NULL, "");
15763b3a8eb9SGleb Smirnoff 				} else
15773b3a8eb9SGleb Smirnoff 					free(hh);
15783b3a8eb9SGleb Smirnoff 			}
15796fcc8e04SKristof Provost 			for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++)
15806fcc8e04SKristof Provost 				free($5.label[i]);
15813b3a8eb9SGleb Smirnoff 		}
15823b3a8eb9SGleb Smirnoff 		;
15833b3a8eb9SGleb Smirnoff 
15843b3a8eb9SGleb Smirnoff antispoof_ifspc	: FOR antispoof_if			{ $$ = $2; }
15853b3a8eb9SGleb Smirnoff 		| FOR '{' optnl antispoof_iflst '}'	{ $$ = $4; }
15863b3a8eb9SGleb Smirnoff 		;
15873b3a8eb9SGleb Smirnoff 
15883b3a8eb9SGleb Smirnoff antispoof_iflst	: antispoof_if optnl			{ $$ = $1; }
15893b3a8eb9SGleb Smirnoff 		| antispoof_iflst comma antispoof_if optnl {
15903b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
15913b3a8eb9SGleb Smirnoff 			$1->tail = $3;
15923b3a8eb9SGleb Smirnoff 			$$ = $1;
15933b3a8eb9SGleb Smirnoff 		}
15943b3a8eb9SGleb Smirnoff 		;
15953b3a8eb9SGleb Smirnoff 
15963b3a8eb9SGleb Smirnoff antispoof_if	: if_item				{ $$ = $1; }
15973b3a8eb9SGleb Smirnoff 		| '(' if_item ')'			{
15983b3a8eb9SGleb Smirnoff 			$2->dynamic = 1;
15993b3a8eb9SGleb Smirnoff 			$$ = $2;
16003b3a8eb9SGleb Smirnoff 		}
16013b3a8eb9SGleb Smirnoff 		;
16023b3a8eb9SGleb Smirnoff 
16033b3a8eb9SGleb Smirnoff antispoof_opts	:	{
16043b3a8eb9SGleb Smirnoff 				bzero(&antispoof_opts, sizeof antispoof_opts);
16053b3a8eb9SGleb Smirnoff 				antispoof_opts.rtableid = -1;
16063b3a8eb9SGleb Smirnoff 			}
16073b3a8eb9SGleb Smirnoff 		    antispoof_opts_l
16083b3a8eb9SGleb Smirnoff 			{ $$ = antispoof_opts; }
16093b3a8eb9SGleb Smirnoff 		| /* empty */	{
16103b3a8eb9SGleb Smirnoff 			bzero(&antispoof_opts, sizeof antispoof_opts);
16113b3a8eb9SGleb Smirnoff 			antispoof_opts.rtableid = -1;
16123b3a8eb9SGleb Smirnoff 			$$ = antispoof_opts;
16133b3a8eb9SGleb Smirnoff 		}
16143b3a8eb9SGleb Smirnoff 		;
16153b3a8eb9SGleb Smirnoff 
16163b3a8eb9SGleb Smirnoff antispoof_opts_l	: antispoof_opts_l antispoof_opt
16173b3a8eb9SGleb Smirnoff 			| antispoof_opt
16183b3a8eb9SGleb Smirnoff 			;
16193b3a8eb9SGleb Smirnoff 
16203b3a8eb9SGleb Smirnoff antispoof_opt	: label	{
16216fcc8e04SKristof Provost 			if (antispoof_opts.labelcount >= PF_RULE_MAX_LABEL_COUNT) {
16226fcc8e04SKristof Provost 				yyerror("label can only be used %d times", PF_RULE_MAX_LABEL_COUNT);
16233b3a8eb9SGleb Smirnoff 				YYERROR;
16243b3a8eb9SGleb Smirnoff 			}
16256fcc8e04SKristof Provost 			antispoof_opts.label[antispoof_opts.labelcount++] = $1;
16263b3a8eb9SGleb Smirnoff 		}
162776c5eeccSKristof Provost 		| RIDENTIFIER number {
162876c5eeccSKristof Provost 			antispoof_opts.ridentifier = $2;
162976c5eeccSKristof Provost 		}
16303b3a8eb9SGleb Smirnoff 		| RTABLE NUMBER				{
16313b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > rt_tableid_max()) {
16323b3a8eb9SGleb Smirnoff 				yyerror("invalid rtable id");
16333b3a8eb9SGleb Smirnoff 				YYERROR;
16343b3a8eb9SGleb Smirnoff 			}
16353b3a8eb9SGleb Smirnoff 			antispoof_opts.rtableid = $2;
16363b3a8eb9SGleb Smirnoff 		}
16373b3a8eb9SGleb Smirnoff 		;
16383b3a8eb9SGleb Smirnoff 
16393b3a8eb9SGleb Smirnoff not		: '!'		{ $$ = 1; }
16403b3a8eb9SGleb Smirnoff 		| /* empty */	{ $$ = 0; }
16413b3a8eb9SGleb Smirnoff 		;
16423b3a8eb9SGleb Smirnoff 
16433b3a8eb9SGleb Smirnoff tabledef	: TABLE '<' STRING '>' table_opts {
16443b3a8eb9SGleb Smirnoff 			struct node_host	 *h, *nh;
16453b3a8eb9SGleb Smirnoff 			struct node_tinit	 *ti, *nti;
16463b3a8eb9SGleb Smirnoff 
16473b3a8eb9SGleb Smirnoff 			if (strlen($3) >= PF_TABLE_NAME_SIZE) {
16483b3a8eb9SGleb Smirnoff 				yyerror("table name too long, max %d chars",
16493b3a8eb9SGleb Smirnoff 				    PF_TABLE_NAME_SIZE - 1);
16503b3a8eb9SGleb Smirnoff 				free($3);
16513b3a8eb9SGleb Smirnoff 				YYERROR;
16523b3a8eb9SGleb Smirnoff 			}
16533b3a8eb9SGleb Smirnoff 			if (pf->loadopt & PFCTL_FLAG_TABLE)
16543b3a8eb9SGleb Smirnoff 				if (process_tabledef($3, &$5)) {
16553b3a8eb9SGleb Smirnoff 					free($3);
16563b3a8eb9SGleb Smirnoff 					YYERROR;
16573b3a8eb9SGleb Smirnoff 				}
16583b3a8eb9SGleb Smirnoff 			free($3);
16593b3a8eb9SGleb Smirnoff 			for (ti = SIMPLEQ_FIRST(&$5.init_nodes);
16603b3a8eb9SGleb Smirnoff 			    ti != SIMPLEQ_END(&$5.init_nodes); ti = nti) {
16613b3a8eb9SGleb Smirnoff 				if (ti->file)
16623b3a8eb9SGleb Smirnoff 					free(ti->file);
16633b3a8eb9SGleb Smirnoff 				for (h = ti->host; h != NULL; h = nh) {
16643b3a8eb9SGleb Smirnoff 					nh = h->next;
16653b3a8eb9SGleb Smirnoff 					free(h);
16663b3a8eb9SGleb Smirnoff 				}
16673b3a8eb9SGleb Smirnoff 				nti = SIMPLEQ_NEXT(ti, entries);
16683b3a8eb9SGleb Smirnoff 				free(ti);
16693b3a8eb9SGleb Smirnoff 			}
16703b3a8eb9SGleb Smirnoff 		}
16713b3a8eb9SGleb Smirnoff 		;
16723b3a8eb9SGleb Smirnoff 
16733b3a8eb9SGleb Smirnoff table_opts	:	{
16743b3a8eb9SGleb Smirnoff 			bzero(&table_opts, sizeof table_opts);
16753b3a8eb9SGleb Smirnoff 			SIMPLEQ_INIT(&table_opts.init_nodes);
16763b3a8eb9SGleb Smirnoff 		}
16773b3a8eb9SGleb Smirnoff 		    table_opts_l
16783b3a8eb9SGleb Smirnoff 			{ $$ = table_opts; }
16793b3a8eb9SGleb Smirnoff 		| /* empty */
16803b3a8eb9SGleb Smirnoff 			{
16813b3a8eb9SGleb Smirnoff 			bzero(&table_opts, sizeof table_opts);
16823b3a8eb9SGleb Smirnoff 			SIMPLEQ_INIT(&table_opts.init_nodes);
16833b3a8eb9SGleb Smirnoff 			$$ = table_opts;
16843b3a8eb9SGleb Smirnoff 		}
16853b3a8eb9SGleb Smirnoff 		;
16863b3a8eb9SGleb Smirnoff 
16873b3a8eb9SGleb Smirnoff table_opts_l	: table_opts_l table_opt
16883b3a8eb9SGleb Smirnoff 		| table_opt
16893b3a8eb9SGleb Smirnoff 		;
16903b3a8eb9SGleb Smirnoff 
16913b3a8eb9SGleb Smirnoff table_opt	: STRING		{
16923b3a8eb9SGleb Smirnoff 			if (!strcmp($1, "const"))
16933b3a8eb9SGleb Smirnoff 				table_opts.flags |= PFR_TFLAG_CONST;
16943b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "persist"))
16953b3a8eb9SGleb Smirnoff 				table_opts.flags |= PFR_TFLAG_PERSIST;
16963b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "counters"))
16973b3a8eb9SGleb Smirnoff 				table_opts.flags |= PFR_TFLAG_COUNTERS;
16983b3a8eb9SGleb Smirnoff 			else {
16993b3a8eb9SGleb Smirnoff 				yyerror("invalid table option '%s'", $1);
17003b3a8eb9SGleb Smirnoff 				free($1);
17013b3a8eb9SGleb Smirnoff 				YYERROR;
17023b3a8eb9SGleb Smirnoff 			}
17033b3a8eb9SGleb Smirnoff 			free($1);
17043b3a8eb9SGleb Smirnoff 		}
17053b3a8eb9SGleb Smirnoff 		| '{' optnl '}'		{ table_opts.init_addr = 1; }
17063b3a8eb9SGleb Smirnoff 		| '{' optnl host_list '}'	{
17073b3a8eb9SGleb Smirnoff 			struct node_host	*n;
17083b3a8eb9SGleb Smirnoff 			struct node_tinit	*ti;
17093b3a8eb9SGleb Smirnoff 
17103b3a8eb9SGleb Smirnoff 			for (n = $3; n != NULL; n = n->next) {
17113b3a8eb9SGleb Smirnoff 				switch (n->addr.type) {
17123b3a8eb9SGleb Smirnoff 				case PF_ADDR_ADDRMASK:
17133b3a8eb9SGleb Smirnoff 					continue; /* ok */
17143b3a8eb9SGleb Smirnoff 				case PF_ADDR_RANGE:
17153b3a8eb9SGleb Smirnoff 					yyerror("address ranges are not "
17163b3a8eb9SGleb Smirnoff 					    "permitted inside tables");
17173b3a8eb9SGleb Smirnoff 					break;
17183b3a8eb9SGleb Smirnoff 				case PF_ADDR_DYNIFTL:
17193b3a8eb9SGleb Smirnoff 					yyerror("dynamic addresses are not "
17203b3a8eb9SGleb Smirnoff 					    "permitted inside tables");
17213b3a8eb9SGleb Smirnoff 					break;
17223b3a8eb9SGleb Smirnoff 				case PF_ADDR_TABLE:
17233b3a8eb9SGleb Smirnoff 					yyerror("tables cannot contain tables");
17243b3a8eb9SGleb Smirnoff 					break;
17253b3a8eb9SGleb Smirnoff 				case PF_ADDR_NOROUTE:
17263b3a8eb9SGleb Smirnoff 					yyerror("\"no-route\" is not permitted "
17273b3a8eb9SGleb Smirnoff 					    "inside tables");
17283b3a8eb9SGleb Smirnoff 					break;
17293b3a8eb9SGleb Smirnoff 				case PF_ADDR_URPFFAILED:
17303b3a8eb9SGleb Smirnoff 					yyerror("\"urpf-failed\" is not "
17313b3a8eb9SGleb Smirnoff 					    "permitted inside tables");
17323b3a8eb9SGleb Smirnoff 					break;
17333b3a8eb9SGleb Smirnoff 				default:
17343b3a8eb9SGleb Smirnoff 					yyerror("unknown address type %d",
17353b3a8eb9SGleb Smirnoff 					    n->addr.type);
17363b3a8eb9SGleb Smirnoff 				}
17373b3a8eb9SGleb Smirnoff 				YYERROR;
17383b3a8eb9SGleb Smirnoff 			}
17393b3a8eb9SGleb Smirnoff 			if (!(ti = calloc(1, sizeof(*ti))))
17403b3a8eb9SGleb Smirnoff 				err(1, "table_opt: calloc");
17413b3a8eb9SGleb Smirnoff 			ti->host = $3;
17423b3a8eb9SGleb Smirnoff 			SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
17433b3a8eb9SGleb Smirnoff 			    entries);
17443b3a8eb9SGleb Smirnoff 			table_opts.init_addr = 1;
17453b3a8eb9SGleb Smirnoff 		}
17463b3a8eb9SGleb Smirnoff 		| FILENAME STRING	{
17473b3a8eb9SGleb Smirnoff 			struct node_tinit	*ti;
17483b3a8eb9SGleb Smirnoff 
17493b3a8eb9SGleb Smirnoff 			if (!(ti = calloc(1, sizeof(*ti))))
17503b3a8eb9SGleb Smirnoff 				err(1, "table_opt: calloc");
17513b3a8eb9SGleb Smirnoff 			ti->file = $2;
17523b3a8eb9SGleb Smirnoff 			SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
17533b3a8eb9SGleb Smirnoff 			    entries);
17543b3a8eb9SGleb Smirnoff 			table_opts.init_addr = 1;
17553b3a8eb9SGleb Smirnoff 		}
17563b3a8eb9SGleb Smirnoff 		;
17573b3a8eb9SGleb Smirnoff 
17583b3a8eb9SGleb Smirnoff altqif		: ALTQ interface queue_opts QUEUE qassign {
17593b3a8eb9SGleb Smirnoff 			struct pf_altq	a;
17603b3a8eb9SGleb Smirnoff 
17613b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_QUEUE))
17623b3a8eb9SGleb Smirnoff 				YYERROR;
17633b3a8eb9SGleb Smirnoff 
17643b3a8eb9SGleb Smirnoff 			memset(&a, 0, sizeof(a));
17653b3a8eb9SGleb Smirnoff 			if ($3.scheduler.qtype == ALTQT_NONE) {
17663b3a8eb9SGleb Smirnoff 				yyerror("no scheduler specified!");
17673b3a8eb9SGleb Smirnoff 				YYERROR;
17683b3a8eb9SGleb Smirnoff 			}
17693b3a8eb9SGleb Smirnoff 			a.scheduler = $3.scheduler.qtype;
17703b3a8eb9SGleb Smirnoff 			a.qlimit = $3.qlimit;
17713b3a8eb9SGleb Smirnoff 			a.tbrsize = $3.tbrsize;
17720a70aaf8SLuiz Otavio O Souza 			if ($5 == NULL && $3.scheduler.qtype != ALTQT_CODEL) {
17733b3a8eb9SGleb Smirnoff 				yyerror("no child queues specified");
17743b3a8eb9SGleb Smirnoff 				YYERROR;
17753b3a8eb9SGleb Smirnoff 			}
17763b3a8eb9SGleb Smirnoff 			if (expand_altq(&a, $2, $5, $3.queue_bwspec,
17773b3a8eb9SGleb Smirnoff 			    &$3.scheduler))
17783b3a8eb9SGleb Smirnoff 				YYERROR;
17793b3a8eb9SGleb Smirnoff 		}
17803b3a8eb9SGleb Smirnoff 		;
17813b3a8eb9SGleb Smirnoff 
17823b3a8eb9SGleb Smirnoff queuespec	: QUEUE STRING interface queue_opts qassign {
17833b3a8eb9SGleb Smirnoff 			struct pf_altq	a;
17843b3a8eb9SGleb Smirnoff 
17853b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_QUEUE)) {
17863b3a8eb9SGleb Smirnoff 				free($2);
17873b3a8eb9SGleb Smirnoff 				YYERROR;
17883b3a8eb9SGleb Smirnoff 			}
17893b3a8eb9SGleb Smirnoff 
17903b3a8eb9SGleb Smirnoff 			memset(&a, 0, sizeof(a));
17913b3a8eb9SGleb Smirnoff 
17923b3a8eb9SGleb Smirnoff 			if (strlcpy(a.qname, $2, sizeof(a.qname)) >=
17933b3a8eb9SGleb Smirnoff 			    sizeof(a.qname)) {
17943b3a8eb9SGleb Smirnoff 				yyerror("queue name too long (max "
17953b3a8eb9SGleb Smirnoff 				    "%d chars)", PF_QNAME_SIZE-1);
17963b3a8eb9SGleb Smirnoff 				free($2);
17973b3a8eb9SGleb Smirnoff 				YYERROR;
17983b3a8eb9SGleb Smirnoff 			}
17993b3a8eb9SGleb Smirnoff 			free($2);
18003b3a8eb9SGleb Smirnoff 			if ($4.tbrsize) {
18013b3a8eb9SGleb Smirnoff 				yyerror("cannot specify tbrsize for queue");
18023b3a8eb9SGleb Smirnoff 				YYERROR;
18033b3a8eb9SGleb Smirnoff 			}
18043b3a8eb9SGleb Smirnoff 			if ($4.priority > 255) {
18053b3a8eb9SGleb Smirnoff 				yyerror("priority out of range: max 255");
18063b3a8eb9SGleb Smirnoff 				YYERROR;
18073b3a8eb9SGleb Smirnoff 			}
18083b3a8eb9SGleb Smirnoff 			a.priority = $4.priority;
18093b3a8eb9SGleb Smirnoff 			a.qlimit = $4.qlimit;
18103b3a8eb9SGleb Smirnoff 			a.scheduler = $4.scheduler.qtype;
18113b3a8eb9SGleb Smirnoff 			if (expand_queue(&a, $3, $5, $4.queue_bwspec,
18123b3a8eb9SGleb Smirnoff 			    &$4.scheduler)) {
18133b3a8eb9SGleb Smirnoff 				yyerror("errors in queue definition");
18143b3a8eb9SGleb Smirnoff 				YYERROR;
18153b3a8eb9SGleb Smirnoff 			}
18163b3a8eb9SGleb Smirnoff 		}
18173b3a8eb9SGleb Smirnoff 		;
18183b3a8eb9SGleb Smirnoff 
18193b3a8eb9SGleb Smirnoff queue_opts	:	{
18203b3a8eb9SGleb Smirnoff 			bzero(&queue_opts, sizeof queue_opts);
18213b3a8eb9SGleb Smirnoff 			queue_opts.priority = DEFAULT_PRIORITY;
18223b3a8eb9SGleb Smirnoff 			queue_opts.qlimit = DEFAULT_QLIMIT;
18233b3a8eb9SGleb Smirnoff 			queue_opts.scheduler.qtype = ALTQT_NONE;
18243b3a8eb9SGleb Smirnoff 			queue_opts.queue_bwspec.bw_percent = 100;
18253b3a8eb9SGleb Smirnoff 		}
18263b3a8eb9SGleb Smirnoff 		    queue_opts_l
18273b3a8eb9SGleb Smirnoff 			{ $$ = queue_opts; }
18283b3a8eb9SGleb Smirnoff 		| /* empty */ {
18293b3a8eb9SGleb Smirnoff 			bzero(&queue_opts, sizeof queue_opts);
18303b3a8eb9SGleb Smirnoff 			queue_opts.priority = DEFAULT_PRIORITY;
18313b3a8eb9SGleb Smirnoff 			queue_opts.qlimit = DEFAULT_QLIMIT;
18323b3a8eb9SGleb Smirnoff 			queue_opts.scheduler.qtype = ALTQT_NONE;
18333b3a8eb9SGleb Smirnoff 			queue_opts.queue_bwspec.bw_percent = 100;
18343b3a8eb9SGleb Smirnoff 			$$ = queue_opts;
18353b3a8eb9SGleb Smirnoff 		}
18363b3a8eb9SGleb Smirnoff 		;
18373b3a8eb9SGleb Smirnoff 
18383b3a8eb9SGleb Smirnoff queue_opts_l	: queue_opts_l queue_opt
18393b3a8eb9SGleb Smirnoff 		| queue_opt
18403b3a8eb9SGleb Smirnoff 		;
18413b3a8eb9SGleb Smirnoff 
18423b3a8eb9SGleb Smirnoff queue_opt	: BANDWIDTH bandwidth	{
18433b3a8eb9SGleb Smirnoff 			if (queue_opts.marker & QOM_BWSPEC) {
18443b3a8eb9SGleb Smirnoff 				yyerror("bandwidth cannot be respecified");
18453b3a8eb9SGleb Smirnoff 				YYERROR;
18463b3a8eb9SGleb Smirnoff 			}
18473b3a8eb9SGleb Smirnoff 			queue_opts.marker |= QOM_BWSPEC;
18483b3a8eb9SGleb Smirnoff 			queue_opts.queue_bwspec = $2;
18493b3a8eb9SGleb Smirnoff 		}
18503b3a8eb9SGleb Smirnoff 		| PRIORITY NUMBER	{
18513b3a8eb9SGleb Smirnoff 			if (queue_opts.marker & QOM_PRIORITY) {
18523b3a8eb9SGleb Smirnoff 				yyerror("priority cannot be respecified");
18533b3a8eb9SGleb Smirnoff 				YYERROR;
18543b3a8eb9SGleb Smirnoff 			}
18553b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > 255) {
18563b3a8eb9SGleb Smirnoff 				yyerror("priority out of range: max 255");
18573b3a8eb9SGleb Smirnoff 				YYERROR;
18583b3a8eb9SGleb Smirnoff 			}
18593b3a8eb9SGleb Smirnoff 			queue_opts.marker |= QOM_PRIORITY;
18603b3a8eb9SGleb Smirnoff 			queue_opts.priority = $2;
18613b3a8eb9SGleb Smirnoff 		}
18623b3a8eb9SGleb Smirnoff 		| QLIMIT NUMBER	{
18633b3a8eb9SGleb Smirnoff 			if (queue_opts.marker & QOM_QLIMIT) {
18643b3a8eb9SGleb Smirnoff 				yyerror("qlimit cannot be respecified");
18653b3a8eb9SGleb Smirnoff 				YYERROR;
18663b3a8eb9SGleb Smirnoff 			}
18673b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > 65535) {
18683b3a8eb9SGleb Smirnoff 				yyerror("qlimit out of range: max 65535");
18693b3a8eb9SGleb Smirnoff 				YYERROR;
18703b3a8eb9SGleb Smirnoff 			}
18713b3a8eb9SGleb Smirnoff 			queue_opts.marker |= QOM_QLIMIT;
18723b3a8eb9SGleb Smirnoff 			queue_opts.qlimit = $2;
18733b3a8eb9SGleb Smirnoff 		}
18743b3a8eb9SGleb Smirnoff 		| scheduler	{
18753b3a8eb9SGleb Smirnoff 			if (queue_opts.marker & QOM_SCHEDULER) {
18763b3a8eb9SGleb Smirnoff 				yyerror("scheduler cannot be respecified");
18773b3a8eb9SGleb Smirnoff 				YYERROR;
18783b3a8eb9SGleb Smirnoff 			}
18793b3a8eb9SGleb Smirnoff 			queue_opts.marker |= QOM_SCHEDULER;
18803b3a8eb9SGleb Smirnoff 			queue_opts.scheduler = $1;
18813b3a8eb9SGleb Smirnoff 		}
18823b3a8eb9SGleb Smirnoff 		| TBRSIZE NUMBER	{
18833b3a8eb9SGleb Smirnoff 			if (queue_opts.marker & QOM_TBRSIZE) {
18843b3a8eb9SGleb Smirnoff 				yyerror("tbrsize cannot be respecified");
18853b3a8eb9SGleb Smirnoff 				YYERROR;
18863b3a8eb9SGleb Smirnoff 			}
1887249cc75fSPatrick Kelsey 			if ($2 < 0 || $2 > UINT_MAX) {
1888249cc75fSPatrick Kelsey 				yyerror("tbrsize too big: max %u", UINT_MAX);
18893b3a8eb9SGleb Smirnoff 				YYERROR;
18903b3a8eb9SGleb Smirnoff 			}
18913b3a8eb9SGleb Smirnoff 			queue_opts.marker |= QOM_TBRSIZE;
18923b3a8eb9SGleb Smirnoff 			queue_opts.tbrsize = $2;
18933b3a8eb9SGleb Smirnoff 		}
18943b3a8eb9SGleb Smirnoff 		;
18953b3a8eb9SGleb Smirnoff 
18963b3a8eb9SGleb Smirnoff bandwidth	: STRING {
18973b3a8eb9SGleb Smirnoff 			double	 bps;
18983b3a8eb9SGleb Smirnoff 			char	*cp;
18993b3a8eb9SGleb Smirnoff 
19003b3a8eb9SGleb Smirnoff 			$$.bw_percent = 0;
19013b3a8eb9SGleb Smirnoff 
19023b3a8eb9SGleb Smirnoff 			bps = strtod($1, &cp);
19033b3a8eb9SGleb Smirnoff 			if (cp != NULL) {
1904db1bbde6SLuiz Otavio O Souza 				if (strlen(cp) > 1) {
1905db1bbde6SLuiz Otavio O Souza 					char *cu = cp + 1;
1906db1bbde6SLuiz Otavio O Souza 					if (!strcmp(cu, "Bit") ||
1907db1bbde6SLuiz Otavio O Souza 					    !strcmp(cu, "B") ||
1908db1bbde6SLuiz Otavio O Souza 					    !strcmp(cu, "bit") ||
1909db1bbde6SLuiz Otavio O Souza 					    !strcmp(cu, "b")) {
1910db1bbde6SLuiz Otavio O Souza 						*cu = 0;
1911db1bbde6SLuiz Otavio O Souza 					}
1912db1bbde6SLuiz Otavio O Souza 				}
19133b3a8eb9SGleb Smirnoff 				if (!strcmp(cp, "b"))
19143b3a8eb9SGleb Smirnoff 					; /* nothing */
1915db1bbde6SLuiz Otavio O Souza 				else if (!strcmp(cp, "K"))
19163b3a8eb9SGleb Smirnoff 					bps *= 1000;
1917db1bbde6SLuiz Otavio O Souza 				else if (!strcmp(cp, "M"))
19183b3a8eb9SGleb Smirnoff 					bps *= 1000 * 1000;
1919db1bbde6SLuiz Otavio O Souza 				else if (!strcmp(cp, "G"))
19203b3a8eb9SGleb Smirnoff 					bps *= 1000 * 1000 * 1000;
19213b3a8eb9SGleb Smirnoff 				else if (!strcmp(cp, "%")) {
19223b3a8eb9SGleb Smirnoff 					if (bps < 0 || bps > 100) {
19233b3a8eb9SGleb Smirnoff 						yyerror("bandwidth spec "
19243b3a8eb9SGleb Smirnoff 						    "out of range");
19253b3a8eb9SGleb Smirnoff 						free($1);
19263b3a8eb9SGleb Smirnoff 						YYERROR;
19273b3a8eb9SGleb Smirnoff 					}
19283b3a8eb9SGleb Smirnoff 					$$.bw_percent = bps;
19293b3a8eb9SGleb Smirnoff 					bps = 0;
19303b3a8eb9SGleb Smirnoff 				} else {
19313b3a8eb9SGleb Smirnoff 					yyerror("unknown unit %s", cp);
19323b3a8eb9SGleb Smirnoff 					free($1);
19333b3a8eb9SGleb Smirnoff 					YYERROR;
19343b3a8eb9SGleb Smirnoff 				}
19353b3a8eb9SGleb Smirnoff 			}
19363b3a8eb9SGleb Smirnoff 			free($1);
1937249cc75fSPatrick Kelsey 			$$.bw_absolute = (u_int64_t)bps;
19383b3a8eb9SGleb Smirnoff 		}
19393b3a8eb9SGleb Smirnoff 		| NUMBER {
1940249cc75fSPatrick Kelsey 			if ($1 < 0 || $1 >= LLONG_MAX) {
19413b3a8eb9SGleb Smirnoff 				yyerror("bandwidth number too big");
19423b3a8eb9SGleb Smirnoff 				YYERROR;
19433b3a8eb9SGleb Smirnoff 			}
19443b3a8eb9SGleb Smirnoff 			$$.bw_percent = 0;
19453b3a8eb9SGleb Smirnoff 			$$.bw_absolute = $1;
19463b3a8eb9SGleb Smirnoff 		}
19473b3a8eb9SGleb Smirnoff 		;
19483b3a8eb9SGleb Smirnoff 
19493b3a8eb9SGleb Smirnoff scheduler	: CBQ				{
19503b3a8eb9SGleb Smirnoff 			$$.qtype = ALTQT_CBQ;
19513b3a8eb9SGleb Smirnoff 			$$.data.cbq_opts.flags = 0;
19523b3a8eb9SGleb Smirnoff 		}
19533b3a8eb9SGleb Smirnoff 		| CBQ '(' cbqflags_list ')'	{
19543b3a8eb9SGleb Smirnoff 			$$.qtype = ALTQT_CBQ;
19553b3a8eb9SGleb Smirnoff 			$$.data.cbq_opts.flags = $3;
19563b3a8eb9SGleb Smirnoff 		}
19573b3a8eb9SGleb Smirnoff 		| PRIQ				{
19583b3a8eb9SGleb Smirnoff 			$$.qtype = ALTQT_PRIQ;
19593b3a8eb9SGleb Smirnoff 			$$.data.priq_opts.flags = 0;
19603b3a8eb9SGleb Smirnoff 		}
19613b3a8eb9SGleb Smirnoff 		| PRIQ '(' priqflags_list ')'	{
19623b3a8eb9SGleb Smirnoff 			$$.qtype = ALTQT_PRIQ;
19633b3a8eb9SGleb Smirnoff 			$$.data.priq_opts.flags = $3;
19643b3a8eb9SGleb Smirnoff 		}
19653b3a8eb9SGleb Smirnoff 		| HFSC				{
19663b3a8eb9SGleb Smirnoff 			$$.qtype = ALTQT_HFSC;
19673b3a8eb9SGleb Smirnoff 			bzero(&$$.data.hfsc_opts,
19683b3a8eb9SGleb Smirnoff 			    sizeof(struct node_hfsc_opts));
19693b3a8eb9SGleb Smirnoff 		}
19703b3a8eb9SGleb Smirnoff 		| HFSC '(' hfsc_opts ')'	{
19713b3a8eb9SGleb Smirnoff 			$$.qtype = ALTQT_HFSC;
19723b3a8eb9SGleb Smirnoff 			$$.data.hfsc_opts = $3;
19733b3a8eb9SGleb Smirnoff 		}
1974a5b789f6SErmal Luçi 		| FAIRQ				{
1975a5b789f6SErmal Luçi 			$$.qtype = ALTQT_FAIRQ;
1976a5b789f6SErmal Luçi 			bzero(&$$.data.fairq_opts,
1977a5b789f6SErmal Luçi 				sizeof(struct node_fairq_opts));
1978a5b789f6SErmal Luçi 		}
1979a5b789f6SErmal Luçi 		| FAIRQ '(' fairq_opts ')'      {
1980a5b789f6SErmal Luçi 			$$.qtype = ALTQT_FAIRQ;
1981a5b789f6SErmal Luçi 			$$.data.fairq_opts = $3;
1982a5b789f6SErmal Luçi 		}
19830a70aaf8SLuiz Otavio O Souza 		| CODEL				{
19840a70aaf8SLuiz Otavio O Souza 			$$.qtype = ALTQT_CODEL;
19850a70aaf8SLuiz Otavio O Souza 			bzero(&$$.data.codel_opts,
19860a70aaf8SLuiz Otavio O Souza 				sizeof(struct codel_opts));
19870a70aaf8SLuiz Otavio O Souza 		}
19880a70aaf8SLuiz Otavio O Souza 		| CODEL '(' codel_opts ')'	{
19890a70aaf8SLuiz Otavio O Souza 			$$.qtype = ALTQT_CODEL;
19900a70aaf8SLuiz Otavio O Souza 			$$.data.codel_opts = $3;
19910a70aaf8SLuiz Otavio O Souza 		}
19923b3a8eb9SGleb Smirnoff 		;
19933b3a8eb9SGleb Smirnoff 
19943b3a8eb9SGleb Smirnoff cbqflags_list	: cbqflags_item				{ $$ |= $1; }
19953b3a8eb9SGleb Smirnoff 		| cbqflags_list comma cbqflags_item	{ $$ |= $3; }
19963b3a8eb9SGleb Smirnoff 		;
19973b3a8eb9SGleb Smirnoff 
19983b3a8eb9SGleb Smirnoff cbqflags_item	: STRING	{
19993b3a8eb9SGleb Smirnoff 			if (!strcmp($1, "default"))
20003b3a8eb9SGleb Smirnoff 				$$ = CBQCLF_DEFCLASS;
20013b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "borrow"))
20023b3a8eb9SGleb Smirnoff 				$$ = CBQCLF_BORROW;
20033b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "red"))
20043b3a8eb9SGleb Smirnoff 				$$ = CBQCLF_RED;
20053b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "ecn"))
20063b3a8eb9SGleb Smirnoff 				$$ = CBQCLF_RED|CBQCLF_ECN;
20073b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "rio"))
20083b3a8eb9SGleb Smirnoff 				$$ = CBQCLF_RIO;
20090a70aaf8SLuiz Otavio O Souza 			else if (!strcmp($1, "codel"))
20100a70aaf8SLuiz Otavio O Souza 				$$ = CBQCLF_CODEL;
20113b3a8eb9SGleb Smirnoff 			else {
20123b3a8eb9SGleb Smirnoff 				yyerror("unknown cbq flag \"%s\"", $1);
20133b3a8eb9SGleb Smirnoff 				free($1);
20143b3a8eb9SGleb Smirnoff 				YYERROR;
20153b3a8eb9SGleb Smirnoff 			}
20163b3a8eb9SGleb Smirnoff 			free($1);
20173b3a8eb9SGleb Smirnoff 		}
20183b3a8eb9SGleb Smirnoff 		;
20193b3a8eb9SGleb Smirnoff 
20203b3a8eb9SGleb Smirnoff priqflags_list	: priqflags_item			{ $$ |= $1; }
20213b3a8eb9SGleb Smirnoff 		| priqflags_list comma priqflags_item	{ $$ |= $3; }
20223b3a8eb9SGleb Smirnoff 		;
20233b3a8eb9SGleb Smirnoff 
20243b3a8eb9SGleb Smirnoff priqflags_item	: STRING	{
20253b3a8eb9SGleb Smirnoff 			if (!strcmp($1, "default"))
20263b3a8eb9SGleb Smirnoff 				$$ = PRCF_DEFAULTCLASS;
20273b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "red"))
20283b3a8eb9SGleb Smirnoff 				$$ = PRCF_RED;
20293b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "ecn"))
20303b3a8eb9SGleb Smirnoff 				$$ = PRCF_RED|PRCF_ECN;
20313b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "rio"))
20323b3a8eb9SGleb Smirnoff 				$$ = PRCF_RIO;
20330a70aaf8SLuiz Otavio O Souza 			else if (!strcmp($1, "codel"))
20340a70aaf8SLuiz Otavio O Souza 				$$ = PRCF_CODEL;
20353b3a8eb9SGleb Smirnoff 			else {
20363b3a8eb9SGleb Smirnoff 				yyerror("unknown priq flag \"%s\"", $1);
20373b3a8eb9SGleb Smirnoff 				free($1);
20383b3a8eb9SGleb Smirnoff 				YYERROR;
20393b3a8eb9SGleb Smirnoff 			}
20403b3a8eb9SGleb Smirnoff 			free($1);
20413b3a8eb9SGleb Smirnoff 		}
20423b3a8eb9SGleb Smirnoff 		;
20433b3a8eb9SGleb Smirnoff 
20443b3a8eb9SGleb Smirnoff hfsc_opts	:	{
20453b3a8eb9SGleb Smirnoff 				bzero(&hfsc_opts,
20463b3a8eb9SGleb Smirnoff 				    sizeof(struct node_hfsc_opts));
20473b3a8eb9SGleb Smirnoff 			}
20483b3a8eb9SGleb Smirnoff 		    hfscopts_list				{
20493b3a8eb9SGleb Smirnoff 			$$ = hfsc_opts;
20503b3a8eb9SGleb Smirnoff 		}
20513b3a8eb9SGleb Smirnoff 		;
20523b3a8eb9SGleb Smirnoff 
20533b3a8eb9SGleb Smirnoff hfscopts_list	: hfscopts_item
20543b3a8eb9SGleb Smirnoff 		| hfscopts_list comma hfscopts_item
20553b3a8eb9SGleb Smirnoff 		;
20563b3a8eb9SGleb Smirnoff 
20573b3a8eb9SGleb Smirnoff hfscopts_item	: LINKSHARE bandwidth				{
20583b3a8eb9SGleb Smirnoff 			if (hfsc_opts.linkshare.used) {
20593b3a8eb9SGleb Smirnoff 				yyerror("linkshare already specified");
20603b3a8eb9SGleb Smirnoff 				YYERROR;
20613b3a8eb9SGleb Smirnoff 			}
20623b3a8eb9SGleb Smirnoff 			hfsc_opts.linkshare.m2 = $2;
20633b3a8eb9SGleb Smirnoff 			hfsc_opts.linkshare.used = 1;
20643b3a8eb9SGleb Smirnoff 		}
20653b3a8eb9SGleb Smirnoff 		| LINKSHARE '(' bandwidth comma NUMBER comma bandwidth ')'
20663b3a8eb9SGleb Smirnoff 		    {
20673b3a8eb9SGleb Smirnoff 			if ($5 < 0 || $5 > INT_MAX) {
20683b3a8eb9SGleb Smirnoff 				yyerror("timing in curve out of range");
20693b3a8eb9SGleb Smirnoff 				YYERROR;
20703b3a8eb9SGleb Smirnoff 			}
20713b3a8eb9SGleb Smirnoff 			if (hfsc_opts.linkshare.used) {
20723b3a8eb9SGleb Smirnoff 				yyerror("linkshare already specified");
20733b3a8eb9SGleb Smirnoff 				YYERROR;
20743b3a8eb9SGleb Smirnoff 			}
20753b3a8eb9SGleb Smirnoff 			hfsc_opts.linkshare.m1 = $3;
20763b3a8eb9SGleb Smirnoff 			hfsc_opts.linkshare.d = $5;
20773b3a8eb9SGleb Smirnoff 			hfsc_opts.linkshare.m2 = $7;
20783b3a8eb9SGleb Smirnoff 			hfsc_opts.linkshare.used = 1;
20793b3a8eb9SGleb Smirnoff 		}
20803b3a8eb9SGleb Smirnoff 		| REALTIME bandwidth				{
20813b3a8eb9SGleb Smirnoff 			if (hfsc_opts.realtime.used) {
20823b3a8eb9SGleb Smirnoff 				yyerror("realtime already specified");
20833b3a8eb9SGleb Smirnoff 				YYERROR;
20843b3a8eb9SGleb Smirnoff 			}
20853b3a8eb9SGleb Smirnoff 			hfsc_opts.realtime.m2 = $2;
20863b3a8eb9SGleb Smirnoff 			hfsc_opts.realtime.used = 1;
20873b3a8eb9SGleb Smirnoff 		}
20883b3a8eb9SGleb Smirnoff 		| REALTIME '(' bandwidth comma NUMBER comma bandwidth ')'
20893b3a8eb9SGleb Smirnoff 		    {
20903b3a8eb9SGleb Smirnoff 			if ($5 < 0 || $5 > INT_MAX) {
20913b3a8eb9SGleb Smirnoff 				yyerror("timing in curve out of range");
20923b3a8eb9SGleb Smirnoff 				YYERROR;
20933b3a8eb9SGleb Smirnoff 			}
20943b3a8eb9SGleb Smirnoff 			if (hfsc_opts.realtime.used) {
20953b3a8eb9SGleb Smirnoff 				yyerror("realtime already specified");
20963b3a8eb9SGleb Smirnoff 				YYERROR;
20973b3a8eb9SGleb Smirnoff 			}
20983b3a8eb9SGleb Smirnoff 			hfsc_opts.realtime.m1 = $3;
20993b3a8eb9SGleb Smirnoff 			hfsc_opts.realtime.d = $5;
21003b3a8eb9SGleb Smirnoff 			hfsc_opts.realtime.m2 = $7;
21013b3a8eb9SGleb Smirnoff 			hfsc_opts.realtime.used = 1;
21023b3a8eb9SGleb Smirnoff 		}
21033b3a8eb9SGleb Smirnoff 		| UPPERLIMIT bandwidth				{
21043b3a8eb9SGleb Smirnoff 			if (hfsc_opts.upperlimit.used) {
21053b3a8eb9SGleb Smirnoff 				yyerror("upperlimit already specified");
21063b3a8eb9SGleb Smirnoff 				YYERROR;
21073b3a8eb9SGleb Smirnoff 			}
21083b3a8eb9SGleb Smirnoff 			hfsc_opts.upperlimit.m2 = $2;
21093b3a8eb9SGleb Smirnoff 			hfsc_opts.upperlimit.used = 1;
21103b3a8eb9SGleb Smirnoff 		}
21113b3a8eb9SGleb Smirnoff 		| UPPERLIMIT '(' bandwidth comma NUMBER comma bandwidth ')'
21123b3a8eb9SGleb Smirnoff 		    {
21133b3a8eb9SGleb Smirnoff 			if ($5 < 0 || $5 > INT_MAX) {
21143b3a8eb9SGleb Smirnoff 				yyerror("timing in curve out of range");
21153b3a8eb9SGleb Smirnoff 				YYERROR;
21163b3a8eb9SGleb Smirnoff 			}
21173b3a8eb9SGleb Smirnoff 			if (hfsc_opts.upperlimit.used) {
21183b3a8eb9SGleb Smirnoff 				yyerror("upperlimit already specified");
21193b3a8eb9SGleb Smirnoff 				YYERROR;
21203b3a8eb9SGleb Smirnoff 			}
21213b3a8eb9SGleb Smirnoff 			hfsc_opts.upperlimit.m1 = $3;
21223b3a8eb9SGleb Smirnoff 			hfsc_opts.upperlimit.d = $5;
21233b3a8eb9SGleb Smirnoff 			hfsc_opts.upperlimit.m2 = $7;
21243b3a8eb9SGleb Smirnoff 			hfsc_opts.upperlimit.used = 1;
21253b3a8eb9SGleb Smirnoff 		}
21263b3a8eb9SGleb Smirnoff 		| STRING	{
21273b3a8eb9SGleb Smirnoff 			if (!strcmp($1, "default"))
21283b3a8eb9SGleb Smirnoff 				hfsc_opts.flags |= HFCF_DEFAULTCLASS;
21293b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "red"))
21303b3a8eb9SGleb Smirnoff 				hfsc_opts.flags |= HFCF_RED;
21313b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "ecn"))
21323b3a8eb9SGleb Smirnoff 				hfsc_opts.flags |= HFCF_RED|HFCF_ECN;
21333b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "rio"))
21343b3a8eb9SGleb Smirnoff 				hfsc_opts.flags |= HFCF_RIO;
21350a70aaf8SLuiz Otavio O Souza 			else if (!strcmp($1, "codel"))
21360a70aaf8SLuiz Otavio O Souza 				hfsc_opts.flags |= HFCF_CODEL;
21373b3a8eb9SGleb Smirnoff 			else {
21383b3a8eb9SGleb Smirnoff 				yyerror("unknown hfsc flag \"%s\"", $1);
21393b3a8eb9SGleb Smirnoff 				free($1);
21403b3a8eb9SGleb Smirnoff 				YYERROR;
21413b3a8eb9SGleb Smirnoff 			}
21423b3a8eb9SGleb Smirnoff 			free($1);
21433b3a8eb9SGleb Smirnoff 		}
21443b3a8eb9SGleb Smirnoff 		;
21453b3a8eb9SGleb Smirnoff 
2146a5b789f6SErmal Luçi fairq_opts	:	{
2147a5b789f6SErmal Luçi 				bzero(&fairq_opts,
2148a5b789f6SErmal Luçi 				    sizeof(struct node_fairq_opts));
2149a5b789f6SErmal Luçi 			}
2150a5b789f6SErmal Luçi 		    fairqopts_list				{
2151a5b789f6SErmal Luçi 			$$ = fairq_opts;
2152a5b789f6SErmal Luçi 		}
2153a5b789f6SErmal Luçi 		;
2154a5b789f6SErmal Luçi 
2155a5b789f6SErmal Luçi fairqopts_list	: fairqopts_item
2156a5b789f6SErmal Luçi 		| fairqopts_list comma fairqopts_item
2157a5b789f6SErmal Luçi 		;
2158a5b789f6SErmal Luçi 
2159a5b789f6SErmal Luçi fairqopts_item	: LINKSHARE bandwidth				{
2160a5b789f6SErmal Luçi 			if (fairq_opts.linkshare.used) {
2161a5b789f6SErmal Luçi 				yyerror("linkshare already specified");
2162a5b789f6SErmal Luçi 				YYERROR;
2163a5b789f6SErmal Luçi 			}
2164a5b789f6SErmal Luçi 			fairq_opts.linkshare.m2 = $2;
2165a5b789f6SErmal Luçi 			fairq_opts.linkshare.used = 1;
2166a5b789f6SErmal Luçi 		}
2167a5b789f6SErmal Luçi 		| LINKSHARE '(' bandwidth number bandwidth ')'	{
2168a5b789f6SErmal Luçi 			if (fairq_opts.linkshare.used) {
2169a5b789f6SErmal Luçi 				yyerror("linkshare already specified");
2170a5b789f6SErmal Luçi 				YYERROR;
2171a5b789f6SErmal Luçi 			}
2172a5b789f6SErmal Luçi 			fairq_opts.linkshare.m1 = $3;
2173a5b789f6SErmal Luçi 			fairq_opts.linkshare.d = $4;
2174a5b789f6SErmal Luçi 			fairq_opts.linkshare.m2 = $5;
2175a5b789f6SErmal Luçi 			fairq_opts.linkshare.used = 1;
2176a5b789f6SErmal Luçi 		}
2177a5b789f6SErmal Luçi 		| HOGS bandwidth {
2178a5b789f6SErmal Luçi 			fairq_opts.hogs_bw = $2;
2179a5b789f6SErmal Luçi 		}
2180a5b789f6SErmal Luçi 		| BUCKETS number {
2181a5b789f6SErmal Luçi 			fairq_opts.nbuckets = $2;
2182a5b789f6SErmal Luçi 		}
2183a5b789f6SErmal Luçi 		| STRING	{
2184a5b789f6SErmal Luçi 			if (!strcmp($1, "default"))
2185a5b789f6SErmal Luçi 				fairq_opts.flags |= FARF_DEFAULTCLASS;
2186a5b789f6SErmal Luçi 			else if (!strcmp($1, "red"))
2187a5b789f6SErmal Luçi 				fairq_opts.flags |= FARF_RED;
2188a5b789f6SErmal Luçi 			else if (!strcmp($1, "ecn"))
2189a5b789f6SErmal Luçi 				fairq_opts.flags |= FARF_RED|FARF_ECN;
2190a5b789f6SErmal Luçi 			else if (!strcmp($1, "rio"))
2191a5b789f6SErmal Luçi 				fairq_opts.flags |= FARF_RIO;
21920a70aaf8SLuiz Otavio O Souza 			else if (!strcmp($1, "codel"))
21930a70aaf8SLuiz Otavio O Souza 				fairq_opts.flags |= FARF_CODEL;
2194a5b789f6SErmal Luçi 			else {
2195a5b789f6SErmal Luçi 				yyerror("unknown fairq flag \"%s\"", $1);
2196a5b789f6SErmal Luçi 				free($1);
2197a5b789f6SErmal Luçi 				YYERROR;
2198a5b789f6SErmal Luçi 			}
2199a5b789f6SErmal Luçi 			free($1);
2200a5b789f6SErmal Luçi 		}
2201a5b789f6SErmal Luçi 		;
2202a5b789f6SErmal Luçi 
22030a70aaf8SLuiz Otavio O Souza codel_opts	:	{
22040a70aaf8SLuiz Otavio O Souza 				bzero(&codel_opts,
22050a70aaf8SLuiz Otavio O Souza 				    sizeof(struct codel_opts));
22060a70aaf8SLuiz Otavio O Souza 			}
22070a70aaf8SLuiz Otavio O Souza 		    codelopts_list				{
22080a70aaf8SLuiz Otavio O Souza 			$$ = codel_opts;
22090a70aaf8SLuiz Otavio O Souza 		}
22100a70aaf8SLuiz Otavio O Souza 		;
22110a70aaf8SLuiz Otavio O Souza 
22120a70aaf8SLuiz Otavio O Souza codelopts_list	: codelopts_item
22130a70aaf8SLuiz Otavio O Souza 		| codelopts_list comma codelopts_item
22140a70aaf8SLuiz Otavio O Souza 		;
22150a70aaf8SLuiz Otavio O Souza 
22160a70aaf8SLuiz Otavio O Souza codelopts_item	: INTERVAL number				{
22170a70aaf8SLuiz Otavio O Souza 			if (codel_opts.interval) {
22180a70aaf8SLuiz Otavio O Souza 				yyerror("interval already specified");
22190a70aaf8SLuiz Otavio O Souza 				YYERROR;
22200a70aaf8SLuiz Otavio O Souza 			}
22210a70aaf8SLuiz Otavio O Souza 			codel_opts.interval = $2;
22220a70aaf8SLuiz Otavio O Souza 		}
22230a70aaf8SLuiz Otavio O Souza 		| TARGET number					{
22240a70aaf8SLuiz Otavio O Souza 			if (codel_opts.target) {
22250a70aaf8SLuiz Otavio O Souza 				yyerror("target already specified");
22260a70aaf8SLuiz Otavio O Souza 				YYERROR;
22270a70aaf8SLuiz Otavio O Souza 			}
22280a70aaf8SLuiz Otavio O Souza 			codel_opts.target = $2;
22290a70aaf8SLuiz Otavio O Souza 		}
22300a70aaf8SLuiz Otavio O Souza 		| STRING					{
22310a70aaf8SLuiz Otavio O Souza 			if (!strcmp($1, "ecn"))
22320a70aaf8SLuiz Otavio O Souza 				codel_opts.ecn = 1;
22330a70aaf8SLuiz Otavio O Souza 			else {
22340a70aaf8SLuiz Otavio O Souza 				yyerror("unknown codel option \"%s\"", $1);
22350a70aaf8SLuiz Otavio O Souza 				free($1);
22360a70aaf8SLuiz Otavio O Souza 				YYERROR;
22370a70aaf8SLuiz Otavio O Souza 			}
22380a70aaf8SLuiz Otavio O Souza 			free($1);
22390a70aaf8SLuiz Otavio O Souza 		}
22400a70aaf8SLuiz Otavio O Souza 		;
22410a70aaf8SLuiz Otavio O Souza 
22423b3a8eb9SGleb Smirnoff qassign		: /* empty */		{ $$ = NULL; }
22433b3a8eb9SGleb Smirnoff 		| qassign_item		{ $$ = $1; }
22443b3a8eb9SGleb Smirnoff 		| '{' optnl qassign_list '}'	{ $$ = $3; }
22453b3a8eb9SGleb Smirnoff 		;
22463b3a8eb9SGleb Smirnoff 
22473b3a8eb9SGleb Smirnoff qassign_list	: qassign_item optnl		{ $$ = $1; }
22483b3a8eb9SGleb Smirnoff 		| qassign_list comma qassign_item optnl	{
22493b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
22503b3a8eb9SGleb Smirnoff 			$1->tail = $3;
22513b3a8eb9SGleb Smirnoff 			$$ = $1;
22523b3a8eb9SGleb Smirnoff 		}
22533b3a8eb9SGleb Smirnoff 		;
22543b3a8eb9SGleb Smirnoff 
22553b3a8eb9SGleb Smirnoff qassign_item	: STRING			{
22563b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_queue));
22573b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
22583b3a8eb9SGleb Smirnoff 				err(1, "qassign_item: calloc");
22593b3a8eb9SGleb Smirnoff 			if (strlcpy($$->queue, $1, sizeof($$->queue)) >=
22603b3a8eb9SGleb Smirnoff 			    sizeof($$->queue)) {
22613b3a8eb9SGleb Smirnoff 				yyerror("queue name '%s' too long (max "
22623b3a8eb9SGleb Smirnoff 				    "%d chars)", $1, sizeof($$->queue)-1);
22633b3a8eb9SGleb Smirnoff 				free($1);
22643b3a8eb9SGleb Smirnoff 				free($$);
22653b3a8eb9SGleb Smirnoff 				YYERROR;
22663b3a8eb9SGleb Smirnoff 			}
22673b3a8eb9SGleb Smirnoff 			free($1);
22683b3a8eb9SGleb Smirnoff 			$$->next = NULL;
22693b3a8eb9SGleb Smirnoff 			$$->tail = $$;
22703b3a8eb9SGleb Smirnoff 		}
22713b3a8eb9SGleb Smirnoff 		;
22723b3a8eb9SGleb Smirnoff 
22733b3a8eb9SGleb Smirnoff pfrule		: action dir logquick interface route af proto fromto
22743b3a8eb9SGleb Smirnoff 		    filter_opts
22753b3a8eb9SGleb Smirnoff 		{
2276e9eb0941SKristof Provost 			struct pfctl_rule	 r;
22773b3a8eb9SGleb Smirnoff 			struct node_state_opt	*o;
22783b3a8eb9SGleb Smirnoff 			struct node_proto	*proto;
22793b3a8eb9SGleb Smirnoff 			int			 srctrack = 0;
22803b3a8eb9SGleb Smirnoff 			int			 statelock = 0;
22813b3a8eb9SGleb Smirnoff 			int			 adaptive = 0;
22823b3a8eb9SGleb Smirnoff 			int			 defaults = 0;
22833b3a8eb9SGleb Smirnoff 
22843b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_FILTER))
22853b3a8eb9SGleb Smirnoff 				YYERROR;
22863b3a8eb9SGleb Smirnoff 
22873b3a8eb9SGleb Smirnoff 			memset(&r, 0, sizeof(r));
22883b3a8eb9SGleb Smirnoff 
22893b3a8eb9SGleb Smirnoff 			r.action = $1.b1;
22903b3a8eb9SGleb Smirnoff 			switch ($1.b2) {
22913b3a8eb9SGleb Smirnoff 			case PFRULE_RETURNRST:
22923b3a8eb9SGleb Smirnoff 				r.rule_flag |= PFRULE_RETURNRST;
22933b3a8eb9SGleb Smirnoff 				r.return_ttl = $1.w;
22943b3a8eb9SGleb Smirnoff 				break;
22953b3a8eb9SGleb Smirnoff 			case PFRULE_RETURNICMP:
22963b3a8eb9SGleb Smirnoff 				r.rule_flag |= PFRULE_RETURNICMP;
22973b3a8eb9SGleb Smirnoff 				r.return_icmp = $1.w;
22983b3a8eb9SGleb Smirnoff 				r.return_icmp6 = $1.w2;
22993b3a8eb9SGleb Smirnoff 				break;
23003b3a8eb9SGleb Smirnoff 			case PFRULE_RETURN:
23013b3a8eb9SGleb Smirnoff 				r.rule_flag |= PFRULE_RETURN;
23023b3a8eb9SGleb Smirnoff 				r.return_icmp = $1.w;
23033b3a8eb9SGleb Smirnoff 				r.return_icmp6 = $1.w2;
23043b3a8eb9SGleb Smirnoff 				break;
23053b3a8eb9SGleb Smirnoff 			}
23063b3a8eb9SGleb Smirnoff 			r.direction = $2;
23073b3a8eb9SGleb Smirnoff 			r.log = $3.log;
23083b3a8eb9SGleb Smirnoff 			r.logif = $3.logif;
23093b3a8eb9SGleb Smirnoff 			r.quick = $3.quick;
23103b3a8eb9SGleb Smirnoff 			r.prob = $9.prob;
23113b3a8eb9SGleb Smirnoff 			r.rtableid = $9.rtableid;
23123b3a8eb9SGleb Smirnoff 
23133e248e0fSKristof Provost 			if ($9.marker & FOM_PRIO) {
23143e248e0fSKristof Provost 				if ($9.prio == 0)
23153e248e0fSKristof Provost 					r.prio = PF_PRIO_ZERO;
23163e248e0fSKristof Provost 				else
23173e248e0fSKristof Provost 					r.prio = $9.prio;
23183e248e0fSKristof Provost 			}
23193e248e0fSKristof Provost 			if ($9.marker & FOM_SETPRIO) {
23203e248e0fSKristof Provost 				r.set_prio[0] = $9.set_prio[0];
23213e248e0fSKristof Provost 				r.set_prio[1] = $9.set_prio[1];
23223e248e0fSKristof Provost 				r.scrub_flags |= PFSTATE_SETPRIO;
23233e248e0fSKristof Provost 			}
23243e248e0fSKristof Provost 
23253b3a8eb9SGleb Smirnoff 			r.af = $6;
23263b3a8eb9SGleb Smirnoff 			if ($9.tag)
23273b3a8eb9SGleb Smirnoff 				if (strlcpy(r.tagname, $9.tag,
23283b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
23293b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
23303b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
23313b3a8eb9SGleb Smirnoff 					YYERROR;
23323b3a8eb9SGleb Smirnoff 				}
23333b3a8eb9SGleb Smirnoff 			if ($9.match_tag)
23343b3a8eb9SGleb Smirnoff 				if (strlcpy(r.match_tagname, $9.match_tag,
23353b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
23363b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
23373b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
23383b3a8eb9SGleb Smirnoff 					YYERROR;
23393b3a8eb9SGleb Smirnoff 				}
23403b3a8eb9SGleb Smirnoff 			r.match_tag_not = $9.match_tag_not;
23413b3a8eb9SGleb Smirnoff 			if (rule_label(&r, $9.label))
23423b3a8eb9SGleb Smirnoff 				YYERROR;
23436fcc8e04SKristof Provost 			for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++)
23446fcc8e04SKristof Provost 				free($9.label[i]);
234576c5eeccSKristof Provost 			r.ridentifier = $9.ridentifier;
23463b3a8eb9SGleb Smirnoff 			r.flags = $9.flags.b1;
23473b3a8eb9SGleb Smirnoff 			r.flagset = $9.flags.b2;
23483b3a8eb9SGleb Smirnoff 			if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
23493b3a8eb9SGleb Smirnoff 				yyerror("flags always false");
23503b3a8eb9SGleb Smirnoff 				YYERROR;
23513b3a8eb9SGleb Smirnoff 			}
23523b3a8eb9SGleb Smirnoff 			if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
23533b3a8eb9SGleb Smirnoff 				for (proto = $7; proto != NULL &&
23543b3a8eb9SGleb Smirnoff 				    proto->proto != IPPROTO_TCP;
23553b3a8eb9SGleb Smirnoff 				    proto = proto->next)
23563b3a8eb9SGleb Smirnoff 					;	/* nothing */
23573b3a8eb9SGleb Smirnoff 				if (proto == NULL && $7 != NULL) {
23583b3a8eb9SGleb Smirnoff 					if ($9.flags.b1 || $9.flags.b2)
23593b3a8eb9SGleb Smirnoff 						yyerror(
23603b3a8eb9SGleb Smirnoff 						    "flags only apply to tcp");
23613b3a8eb9SGleb Smirnoff 					if ($8.src_os)
23623b3a8eb9SGleb Smirnoff 						yyerror(
23633b3a8eb9SGleb Smirnoff 						    "OS fingerprinting only "
23643b3a8eb9SGleb Smirnoff 						    "apply to tcp");
23653b3a8eb9SGleb Smirnoff 					YYERROR;
23663b3a8eb9SGleb Smirnoff 				}
23673b3a8eb9SGleb Smirnoff #if 0
23683b3a8eb9SGleb Smirnoff 				if (($9.flags.b1 & parse_flags("S")) == 0 &&
23693b3a8eb9SGleb Smirnoff 				    $8.src_os) {
23703b3a8eb9SGleb Smirnoff 					yyerror("OS fingerprinting requires "
23713b3a8eb9SGleb Smirnoff 					    "the SYN TCP flag (flags S/SA)");
23723b3a8eb9SGleb Smirnoff 					YYERROR;
23733b3a8eb9SGleb Smirnoff 				}
23743b3a8eb9SGleb Smirnoff #endif
23753b3a8eb9SGleb Smirnoff 			}
23763b3a8eb9SGleb Smirnoff 
23773b3a8eb9SGleb Smirnoff 			r.tos = $9.tos;
23783b3a8eb9SGleb Smirnoff 			r.keep_state = $9.keep.action;
23793b3a8eb9SGleb Smirnoff 			o = $9.keep.options;
23803b3a8eb9SGleb Smirnoff 
23813b3a8eb9SGleb Smirnoff 			/* 'keep state' by default on pass rules. */
23823b3a8eb9SGleb Smirnoff 			if (!r.keep_state && !r.action &&
23833b3a8eb9SGleb Smirnoff 			    !($9.marker & FOM_KEEP)) {
23843b3a8eb9SGleb Smirnoff 				r.keep_state = PF_STATE_NORMAL;
23853b3a8eb9SGleb Smirnoff 				o = keep_state_defaults;
23863b3a8eb9SGleb Smirnoff 				defaults = 1;
23873b3a8eb9SGleb Smirnoff 			}
23883b3a8eb9SGleb Smirnoff 
23893b3a8eb9SGleb Smirnoff 			while (o) {
23903b3a8eb9SGleb Smirnoff 				struct node_state_opt	*p = o;
23913b3a8eb9SGleb Smirnoff 
23923b3a8eb9SGleb Smirnoff 				switch (o->type) {
23933b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_MAX:
23943b3a8eb9SGleb Smirnoff 					if (r.max_states) {
23953b3a8eb9SGleb Smirnoff 						yyerror("state option 'max' "
23963b3a8eb9SGleb Smirnoff 						    "multiple definitions");
23973b3a8eb9SGleb Smirnoff 						YYERROR;
23983b3a8eb9SGleb Smirnoff 					}
23993b3a8eb9SGleb Smirnoff 					r.max_states = o->data.max_states;
24003b3a8eb9SGleb Smirnoff 					break;
24013b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_NOSYNC:
24023b3a8eb9SGleb Smirnoff 					if (r.rule_flag & PFRULE_NOSYNC) {
24033b3a8eb9SGleb Smirnoff 						yyerror("state option 'sync' "
24043b3a8eb9SGleb Smirnoff 						    "multiple definitions");
24053b3a8eb9SGleb Smirnoff 						YYERROR;
24063b3a8eb9SGleb Smirnoff 					}
24073b3a8eb9SGleb Smirnoff 					r.rule_flag |= PFRULE_NOSYNC;
24083b3a8eb9SGleb Smirnoff 					break;
24093b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_SRCTRACK:
24103b3a8eb9SGleb Smirnoff 					if (srctrack) {
24113b3a8eb9SGleb Smirnoff 						yyerror("state option "
24123b3a8eb9SGleb Smirnoff 						    "'source-track' "
24133b3a8eb9SGleb Smirnoff 						    "multiple definitions");
24143b3a8eb9SGleb Smirnoff 						YYERROR;
24153b3a8eb9SGleb Smirnoff 					}
24163b3a8eb9SGleb Smirnoff 					srctrack =  o->data.src_track;
24173b3a8eb9SGleb Smirnoff 					r.rule_flag |= PFRULE_SRCTRACK;
24183b3a8eb9SGleb Smirnoff 					break;
24193b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_MAX_SRC_STATES:
24203b3a8eb9SGleb Smirnoff 					if (r.max_src_states) {
24213b3a8eb9SGleb Smirnoff 						yyerror("state option "
24223b3a8eb9SGleb Smirnoff 						    "'max-src-states' "
24233b3a8eb9SGleb Smirnoff 						    "multiple definitions");
24243b3a8eb9SGleb Smirnoff 						YYERROR;
24253b3a8eb9SGleb Smirnoff 					}
24263b3a8eb9SGleb Smirnoff 					if (o->data.max_src_states == 0) {
24273b3a8eb9SGleb Smirnoff 						yyerror("'max-src-states' must "
24283b3a8eb9SGleb Smirnoff 						    "be > 0");
24293b3a8eb9SGleb Smirnoff 						YYERROR;
24303b3a8eb9SGleb Smirnoff 					}
24313b3a8eb9SGleb Smirnoff 					r.max_src_states =
24323b3a8eb9SGleb Smirnoff 					    o->data.max_src_states;
24333b3a8eb9SGleb Smirnoff 					r.rule_flag |= PFRULE_SRCTRACK;
24343b3a8eb9SGleb Smirnoff 					break;
24353b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_OVERLOAD:
24363b3a8eb9SGleb Smirnoff 					if (r.overload_tblname[0]) {
24373b3a8eb9SGleb Smirnoff 						yyerror("multiple 'overload' "
24383b3a8eb9SGleb Smirnoff 						    "table definitions");
24393b3a8eb9SGleb Smirnoff 						YYERROR;
24403b3a8eb9SGleb Smirnoff 					}
24413b3a8eb9SGleb Smirnoff 					if (strlcpy(r.overload_tblname,
24423b3a8eb9SGleb Smirnoff 					    o->data.overload.tblname,
24433b3a8eb9SGleb Smirnoff 					    PF_TABLE_NAME_SIZE) >=
24443b3a8eb9SGleb Smirnoff 					    PF_TABLE_NAME_SIZE) {
24453b3a8eb9SGleb Smirnoff 						yyerror("state option: "
24463b3a8eb9SGleb Smirnoff 						    "strlcpy");
24473b3a8eb9SGleb Smirnoff 						YYERROR;
24483b3a8eb9SGleb Smirnoff 					}
24493b3a8eb9SGleb Smirnoff 					r.flush = o->data.overload.flush;
24503b3a8eb9SGleb Smirnoff 					break;
24513b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_MAX_SRC_CONN:
24523b3a8eb9SGleb Smirnoff 					if (r.max_src_conn) {
24533b3a8eb9SGleb Smirnoff 						yyerror("state option "
24543b3a8eb9SGleb Smirnoff 						    "'max-src-conn' "
24553b3a8eb9SGleb Smirnoff 						    "multiple definitions");
24563b3a8eb9SGleb Smirnoff 						YYERROR;
24573b3a8eb9SGleb Smirnoff 					}
24583b3a8eb9SGleb Smirnoff 					if (o->data.max_src_conn == 0) {
24593b3a8eb9SGleb Smirnoff 						yyerror("'max-src-conn' "
24603b3a8eb9SGleb Smirnoff 						    "must be > 0");
24613b3a8eb9SGleb Smirnoff 						YYERROR;
24623b3a8eb9SGleb Smirnoff 					}
24633b3a8eb9SGleb Smirnoff 					r.max_src_conn =
24643b3a8eb9SGleb Smirnoff 					    o->data.max_src_conn;
24653b3a8eb9SGleb Smirnoff 					r.rule_flag |= PFRULE_SRCTRACK |
24663b3a8eb9SGleb Smirnoff 					    PFRULE_RULESRCTRACK;
24673b3a8eb9SGleb Smirnoff 					break;
24683b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_MAX_SRC_CONN_RATE:
24693b3a8eb9SGleb Smirnoff 					if (r.max_src_conn_rate.limit) {
24703b3a8eb9SGleb Smirnoff 						yyerror("state option "
24713b3a8eb9SGleb Smirnoff 						    "'max-src-conn-rate' "
24723b3a8eb9SGleb Smirnoff 						    "multiple definitions");
24733b3a8eb9SGleb Smirnoff 						YYERROR;
24743b3a8eb9SGleb Smirnoff 					}
24753b3a8eb9SGleb Smirnoff 					if (!o->data.max_src_conn_rate.limit ||
24763b3a8eb9SGleb Smirnoff 					    !o->data.max_src_conn_rate.seconds) {
24773b3a8eb9SGleb Smirnoff 						yyerror("'max-src-conn-rate' "
24783b3a8eb9SGleb Smirnoff 						    "values must be > 0");
24793b3a8eb9SGleb Smirnoff 						YYERROR;
24803b3a8eb9SGleb Smirnoff 					}
24813b3a8eb9SGleb Smirnoff 					if (o->data.max_src_conn_rate.limit >
24823b3a8eb9SGleb Smirnoff 					    PF_THRESHOLD_MAX) {
24833b3a8eb9SGleb Smirnoff 						yyerror("'max-src-conn-rate' "
24843b3a8eb9SGleb Smirnoff 						    "maximum rate must be < %u",
24853b3a8eb9SGleb Smirnoff 						    PF_THRESHOLD_MAX);
24863b3a8eb9SGleb Smirnoff 						YYERROR;
24873b3a8eb9SGleb Smirnoff 					}
24883b3a8eb9SGleb Smirnoff 					r.max_src_conn_rate.limit =
24893b3a8eb9SGleb Smirnoff 					    o->data.max_src_conn_rate.limit;
24903b3a8eb9SGleb Smirnoff 					r.max_src_conn_rate.seconds =
24913b3a8eb9SGleb Smirnoff 					    o->data.max_src_conn_rate.seconds;
24923b3a8eb9SGleb Smirnoff 					r.rule_flag |= PFRULE_SRCTRACK |
24933b3a8eb9SGleb Smirnoff 					    PFRULE_RULESRCTRACK;
24943b3a8eb9SGleb Smirnoff 					break;
24953b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_MAX_SRC_NODES:
24963b3a8eb9SGleb Smirnoff 					if (r.max_src_nodes) {
24973b3a8eb9SGleb Smirnoff 						yyerror("state option "
24983b3a8eb9SGleb Smirnoff 						    "'max-src-nodes' "
24993b3a8eb9SGleb Smirnoff 						    "multiple definitions");
25003b3a8eb9SGleb Smirnoff 						YYERROR;
25013b3a8eb9SGleb Smirnoff 					}
25023b3a8eb9SGleb Smirnoff 					if (o->data.max_src_nodes == 0) {
25033b3a8eb9SGleb Smirnoff 						yyerror("'max-src-nodes' must "
25043b3a8eb9SGleb Smirnoff 						    "be > 0");
25053b3a8eb9SGleb Smirnoff 						YYERROR;
25063b3a8eb9SGleb Smirnoff 					}
25073b3a8eb9SGleb Smirnoff 					r.max_src_nodes =
25083b3a8eb9SGleb Smirnoff 					    o->data.max_src_nodes;
25093b3a8eb9SGleb Smirnoff 					r.rule_flag |= PFRULE_SRCTRACK |
25103b3a8eb9SGleb Smirnoff 					    PFRULE_RULESRCTRACK;
25113b3a8eb9SGleb Smirnoff 					break;
25123b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_STATELOCK:
25133b3a8eb9SGleb Smirnoff 					if (statelock) {
25143b3a8eb9SGleb Smirnoff 						yyerror("state locking option: "
25153b3a8eb9SGleb Smirnoff 						    "multiple definitions");
25163b3a8eb9SGleb Smirnoff 						YYERROR;
25173b3a8eb9SGleb Smirnoff 					}
25183b3a8eb9SGleb Smirnoff 					statelock = 1;
25193b3a8eb9SGleb Smirnoff 					r.rule_flag |= o->data.statelock;
25203b3a8eb9SGleb Smirnoff 					break;
25213b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_SLOPPY:
25223b3a8eb9SGleb Smirnoff 					if (r.rule_flag & PFRULE_STATESLOPPY) {
25233b3a8eb9SGleb Smirnoff 						yyerror("state sloppy option: "
25243b3a8eb9SGleb Smirnoff 						    "multiple definitions");
25253b3a8eb9SGleb Smirnoff 						YYERROR;
25263b3a8eb9SGleb Smirnoff 					}
25273b3a8eb9SGleb Smirnoff 					r.rule_flag |= PFRULE_STATESLOPPY;
25283b3a8eb9SGleb Smirnoff 					break;
25293b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_TIMEOUT:
25303b3a8eb9SGleb Smirnoff 					if (o->data.timeout.number ==
25313b3a8eb9SGleb Smirnoff 					    PFTM_ADAPTIVE_START ||
25323b3a8eb9SGleb Smirnoff 					    o->data.timeout.number ==
25333b3a8eb9SGleb Smirnoff 					    PFTM_ADAPTIVE_END)
25343b3a8eb9SGleb Smirnoff 						adaptive = 1;
25353b3a8eb9SGleb Smirnoff 					if (r.timeout[o->data.timeout.number]) {
25363b3a8eb9SGleb Smirnoff 						yyerror("state timeout %s "
25373b3a8eb9SGleb Smirnoff 						    "multiple definitions",
25383b3a8eb9SGleb Smirnoff 						    pf_timeouts[o->data.
25393b3a8eb9SGleb Smirnoff 						    timeout.number].name);
25403b3a8eb9SGleb Smirnoff 						YYERROR;
25413b3a8eb9SGleb Smirnoff 					}
25423b3a8eb9SGleb Smirnoff 					r.timeout[o->data.timeout.number] =
25433b3a8eb9SGleb Smirnoff 					    o->data.timeout.seconds;
25443b3a8eb9SGleb Smirnoff 				}
25453b3a8eb9SGleb Smirnoff 				o = o->next;
25463b3a8eb9SGleb Smirnoff 				if (!defaults)
25473b3a8eb9SGleb Smirnoff 					free(p);
25483b3a8eb9SGleb Smirnoff 			}
25493b3a8eb9SGleb Smirnoff 
25503b3a8eb9SGleb Smirnoff 			/* 'flags S/SA' by default on stateful rules */
25513b3a8eb9SGleb Smirnoff 			if (!r.action && !r.flags && !r.flagset &&
25523b3a8eb9SGleb Smirnoff 			    !$9.fragment && !($9.marker & FOM_FLAGS) &&
25533b3a8eb9SGleb Smirnoff 			    r.keep_state) {
25543b3a8eb9SGleb Smirnoff 				r.flags = parse_flags("S");
25553b3a8eb9SGleb Smirnoff 				r.flagset =  parse_flags("SA");
25563b3a8eb9SGleb Smirnoff 			}
25573b3a8eb9SGleb Smirnoff 			if (!adaptive && r.max_states) {
25583b3a8eb9SGleb Smirnoff 				r.timeout[PFTM_ADAPTIVE_START] =
25593b3a8eb9SGleb Smirnoff 				    (r.max_states / 10) * 6;
25603b3a8eb9SGleb Smirnoff 				r.timeout[PFTM_ADAPTIVE_END] =
25613b3a8eb9SGleb Smirnoff 				    (r.max_states / 10) * 12;
25623b3a8eb9SGleb Smirnoff 			}
25633b3a8eb9SGleb Smirnoff 			if (r.rule_flag & PFRULE_SRCTRACK) {
25643b3a8eb9SGleb Smirnoff 				if (srctrack == PF_SRCTRACK_GLOBAL &&
25653b3a8eb9SGleb Smirnoff 				    r.max_src_nodes) {
25663b3a8eb9SGleb Smirnoff 					yyerror("'max-src-nodes' is "
25673b3a8eb9SGleb Smirnoff 					    "incompatible with "
25683b3a8eb9SGleb Smirnoff 					    "'source-track global'");
25693b3a8eb9SGleb Smirnoff 					YYERROR;
25703b3a8eb9SGleb Smirnoff 				}
25713b3a8eb9SGleb Smirnoff 				if (srctrack == PF_SRCTRACK_GLOBAL &&
25723b3a8eb9SGleb Smirnoff 				    r.max_src_conn) {
25733b3a8eb9SGleb Smirnoff 					yyerror("'max-src-conn' is "
25743b3a8eb9SGleb Smirnoff 					    "incompatible with "
25753b3a8eb9SGleb Smirnoff 					    "'source-track global'");
25763b3a8eb9SGleb Smirnoff 					YYERROR;
25773b3a8eb9SGleb Smirnoff 				}
25783b3a8eb9SGleb Smirnoff 				if (srctrack == PF_SRCTRACK_GLOBAL &&
25793b3a8eb9SGleb Smirnoff 				    r.max_src_conn_rate.seconds) {
25803b3a8eb9SGleb Smirnoff 					yyerror("'max-src-conn-rate' is "
25813b3a8eb9SGleb Smirnoff 					    "incompatible with "
25823b3a8eb9SGleb Smirnoff 					    "'source-track global'");
25833b3a8eb9SGleb Smirnoff 					YYERROR;
25843b3a8eb9SGleb Smirnoff 				}
25853b3a8eb9SGleb Smirnoff 				if (r.timeout[PFTM_SRC_NODE] <
25863b3a8eb9SGleb Smirnoff 				    r.max_src_conn_rate.seconds)
25873b3a8eb9SGleb Smirnoff 					r.timeout[PFTM_SRC_NODE] =
25883b3a8eb9SGleb Smirnoff 					    r.max_src_conn_rate.seconds;
25893b3a8eb9SGleb Smirnoff 				r.rule_flag |= PFRULE_SRCTRACK;
25903b3a8eb9SGleb Smirnoff 				if (srctrack == PF_SRCTRACK_RULE)
25913b3a8eb9SGleb Smirnoff 					r.rule_flag |= PFRULE_RULESRCTRACK;
25923b3a8eb9SGleb Smirnoff 			}
25933b3a8eb9SGleb Smirnoff 			if (r.keep_state && !statelock)
25943b3a8eb9SGleb Smirnoff 				r.rule_flag |= default_statelock;
25953b3a8eb9SGleb Smirnoff 
25963b3a8eb9SGleb Smirnoff 			if ($9.fragment)
25973b3a8eb9SGleb Smirnoff 				r.rule_flag |= PFRULE_FRAGMENT;
25983b3a8eb9SGleb Smirnoff 			r.allow_opts = $9.allowopts;
25993b3a8eb9SGleb Smirnoff 
26003b3a8eb9SGleb Smirnoff 			decide_address_family($8.src.host, &r.af);
26013b3a8eb9SGleb Smirnoff 			decide_address_family($8.dst.host, &r.af);
26023b3a8eb9SGleb Smirnoff 
26033b3a8eb9SGleb Smirnoff 			if ($5.rt) {
26043b3a8eb9SGleb Smirnoff 				if (!r.direction) {
26053b3a8eb9SGleb Smirnoff 					yyerror("direction must be explicit "
26063b3a8eb9SGleb Smirnoff 					    "with rules that specify routing");
26073b3a8eb9SGleb Smirnoff 					YYERROR;
26083b3a8eb9SGleb Smirnoff 				}
26093b3a8eb9SGleb Smirnoff 				r.rt = $5.rt;
26103b3a8eb9SGleb Smirnoff 				r.rpool.opts = $5.pool_opts;
26113b3a8eb9SGleb Smirnoff 				if ($5.key != NULL)
26123b3a8eb9SGleb Smirnoff 					memcpy(&r.rpool.key, $5.key,
26133b3a8eb9SGleb Smirnoff 					    sizeof(struct pf_poolhashkey));
26143b3a8eb9SGleb Smirnoff 			}
2615813196a1SKristof Provost 			if (r.rt) {
26163b3a8eb9SGleb Smirnoff 				decide_address_family($5.host, &r.af);
26173b3a8eb9SGleb Smirnoff 				remove_invalid_hosts(&$5.host, &r.af);
26183b3a8eb9SGleb Smirnoff 				if ($5.host == NULL) {
26193b3a8eb9SGleb Smirnoff 					yyerror("no routing address with "
26203b3a8eb9SGleb Smirnoff 					    "matching address family found.");
26213b3a8eb9SGleb Smirnoff 					YYERROR;
26223b3a8eb9SGleb Smirnoff 				}
26233b3a8eb9SGleb Smirnoff 				if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
26243b3a8eb9SGleb Smirnoff 				    PF_POOL_NONE && ($5.host->next != NULL ||
26253b3a8eb9SGleb Smirnoff 				    $5.host->addr.type == PF_ADDR_TABLE ||
26263b3a8eb9SGleb Smirnoff 				    DYNIF_MULTIADDR($5.host->addr)))
26273b3a8eb9SGleb Smirnoff 					r.rpool.opts |= PF_POOL_ROUNDROBIN;
26283b3a8eb9SGleb Smirnoff 				if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
26293b3a8eb9SGleb Smirnoff 				    PF_POOL_ROUNDROBIN &&
26303b3a8eb9SGleb Smirnoff 				    disallow_table($5.host, "tables are only "
26313b3a8eb9SGleb Smirnoff 				    "supported in round-robin routing pools"))
26323b3a8eb9SGleb Smirnoff 					YYERROR;
26333b3a8eb9SGleb Smirnoff 				if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
26343b3a8eb9SGleb Smirnoff 				    PF_POOL_ROUNDROBIN &&
26353b3a8eb9SGleb Smirnoff 				    disallow_alias($5.host, "interface (%s) "
26363b3a8eb9SGleb Smirnoff 				    "is only supported in round-robin "
26373b3a8eb9SGleb Smirnoff 				    "routing pools"))
26383b3a8eb9SGleb Smirnoff 					YYERROR;
26393b3a8eb9SGleb Smirnoff 				if ($5.host->next != NULL) {
26403b3a8eb9SGleb Smirnoff 					if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
26413b3a8eb9SGleb Smirnoff 					    PF_POOL_ROUNDROBIN) {
26423b3a8eb9SGleb Smirnoff 						yyerror("r.rpool.opts must "
26433b3a8eb9SGleb Smirnoff 						    "be PF_POOL_ROUNDROBIN");
26443b3a8eb9SGleb Smirnoff 						YYERROR;
26453b3a8eb9SGleb Smirnoff 					}
26463b3a8eb9SGleb Smirnoff 				}
26473b3a8eb9SGleb Smirnoff 			}
26483b3a8eb9SGleb Smirnoff 			if ($9.queues.qname != NULL) {
26493b3a8eb9SGleb Smirnoff 				if (strlcpy(r.qname, $9.queues.qname,
26503b3a8eb9SGleb Smirnoff 				    sizeof(r.qname)) >= sizeof(r.qname)) {
26513b3a8eb9SGleb Smirnoff 					yyerror("rule qname too long (max "
26523b3a8eb9SGleb Smirnoff 					    "%d chars)", sizeof(r.qname)-1);
26533b3a8eb9SGleb Smirnoff 					YYERROR;
26543b3a8eb9SGleb Smirnoff 				}
26553b3a8eb9SGleb Smirnoff 				free($9.queues.qname);
26563b3a8eb9SGleb Smirnoff 			}
26573b3a8eb9SGleb Smirnoff 			if ($9.queues.pqname != NULL) {
26583b3a8eb9SGleb Smirnoff 				if (strlcpy(r.pqname, $9.queues.pqname,
26593b3a8eb9SGleb Smirnoff 				    sizeof(r.pqname)) >= sizeof(r.pqname)) {
26603b3a8eb9SGleb Smirnoff 					yyerror("rule pqname too long (max "
26613b3a8eb9SGleb Smirnoff 					    "%d chars)", sizeof(r.pqname)-1);
26623b3a8eb9SGleb Smirnoff 					YYERROR;
26633b3a8eb9SGleb Smirnoff 				}
26643b3a8eb9SGleb Smirnoff 				free($9.queues.pqname);
26653b3a8eb9SGleb Smirnoff 			}
26663b3a8eb9SGleb Smirnoff #ifdef __FreeBSD__
26673b3a8eb9SGleb Smirnoff 			r.divert.port = $9.divert.port;
26683b3a8eb9SGleb Smirnoff #else
26693b3a8eb9SGleb Smirnoff 			if ((r.divert.port = $9.divert.port)) {
26703b3a8eb9SGleb Smirnoff 				if (r.direction == PF_OUT) {
26713b3a8eb9SGleb Smirnoff 					if ($9.divert.addr) {
26723b3a8eb9SGleb Smirnoff 						yyerror("address specified "
26733b3a8eb9SGleb Smirnoff 						    "for outgoing divert");
26743b3a8eb9SGleb Smirnoff 						YYERROR;
26753b3a8eb9SGleb Smirnoff 					}
26763b3a8eb9SGleb Smirnoff 					bzero(&r.divert.addr,
26773b3a8eb9SGleb Smirnoff 					    sizeof(r.divert.addr));
26783b3a8eb9SGleb Smirnoff 				} else {
26793b3a8eb9SGleb Smirnoff 					if (!$9.divert.addr) {
26803b3a8eb9SGleb Smirnoff 						yyerror("no address specified "
26813b3a8eb9SGleb Smirnoff 						    "for incoming divert");
26823b3a8eb9SGleb Smirnoff 						YYERROR;
26833b3a8eb9SGleb Smirnoff 					}
26843b3a8eb9SGleb Smirnoff 					if ($9.divert.addr->af != r.af) {
26853b3a8eb9SGleb Smirnoff 						yyerror("address family "
26863b3a8eb9SGleb Smirnoff 						    "mismatch for divert");
26873b3a8eb9SGleb Smirnoff 						YYERROR;
26883b3a8eb9SGleb Smirnoff 					}
26893b3a8eb9SGleb Smirnoff 					r.divert.addr =
26903b3a8eb9SGleb Smirnoff 					    $9.divert.addr->addr.v.a.addr;
26913b3a8eb9SGleb Smirnoff 				}
26923b3a8eb9SGleb Smirnoff 			}
26933b3a8eb9SGleb Smirnoff #endif
26943b3a8eb9SGleb Smirnoff 
269563b3c1c7SKristof Provost 			if ($9.dnpipe || $9.dnrpipe) {
269663b3c1c7SKristof Provost 				r.dnpipe = $9.dnpipe;
269763b3c1c7SKristof Provost 				r.dnrpipe = $9.dnrpipe;
269863b3c1c7SKristof Provost 				if ($9.free_flags & PFRULE_DN_IS_PIPE)
269963b3c1c7SKristof Provost 					r.free_flags |= PFRULE_DN_IS_PIPE;
270063b3c1c7SKristof Provost 				else
270163b3c1c7SKristof Provost 					r.free_flags |= PFRULE_DN_IS_QUEUE;
270263b3c1c7SKristof Provost 			}
270363b3c1c7SKristof Provost 
27043b3a8eb9SGleb Smirnoff 			expand_rule(&r, $4, $5.host, $7, $8.src_os,
27053b3a8eb9SGleb Smirnoff 			    $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
27063b3a8eb9SGleb Smirnoff 			    $9.uid, $9.gid, $9.icmpspec, "");
27073b3a8eb9SGleb Smirnoff 		}
27083b3a8eb9SGleb Smirnoff 		;
27093b3a8eb9SGleb Smirnoff 
27103b3a8eb9SGleb Smirnoff filter_opts	:	{
27113b3a8eb9SGleb Smirnoff 				bzero(&filter_opts, sizeof filter_opts);
27123b3a8eb9SGleb Smirnoff 				filter_opts.rtableid = -1;
27133b3a8eb9SGleb Smirnoff 			}
27143b3a8eb9SGleb Smirnoff 		    filter_opts_l
27153b3a8eb9SGleb Smirnoff 			{ $$ = filter_opts; }
27163b3a8eb9SGleb Smirnoff 		| /* empty */	{
27173b3a8eb9SGleb Smirnoff 			bzero(&filter_opts, sizeof filter_opts);
27183b3a8eb9SGleb Smirnoff 			filter_opts.rtableid = -1;
27193b3a8eb9SGleb Smirnoff 			$$ = filter_opts;
27203b3a8eb9SGleb Smirnoff 		}
27213b3a8eb9SGleb Smirnoff 		;
27223b3a8eb9SGleb Smirnoff 
27233b3a8eb9SGleb Smirnoff filter_opts_l	: filter_opts_l filter_opt
27243b3a8eb9SGleb Smirnoff 		| filter_opt
27253b3a8eb9SGleb Smirnoff 		;
27263b3a8eb9SGleb Smirnoff 
27273b3a8eb9SGleb Smirnoff filter_opt	: USER uids {
27283b3a8eb9SGleb Smirnoff 			if (filter_opts.uid)
27293b3a8eb9SGleb Smirnoff 				$2->tail->next = filter_opts.uid;
27303b3a8eb9SGleb Smirnoff 			filter_opts.uid = $2;
27313b3a8eb9SGleb Smirnoff 		}
27323b3a8eb9SGleb Smirnoff 		| GROUP gids {
27333b3a8eb9SGleb Smirnoff 			if (filter_opts.gid)
27343b3a8eb9SGleb Smirnoff 				$2->tail->next = filter_opts.gid;
27353b3a8eb9SGleb Smirnoff 			filter_opts.gid = $2;
27363b3a8eb9SGleb Smirnoff 		}
27373b3a8eb9SGleb Smirnoff 		| flags {
27383b3a8eb9SGleb Smirnoff 			if (filter_opts.marker & FOM_FLAGS) {
27393b3a8eb9SGleb Smirnoff 				yyerror("flags cannot be redefined");
27403b3a8eb9SGleb Smirnoff 				YYERROR;
27413b3a8eb9SGleb Smirnoff 			}
27423b3a8eb9SGleb Smirnoff 			filter_opts.marker |= FOM_FLAGS;
27433b3a8eb9SGleb Smirnoff 			filter_opts.flags.b1 |= $1.b1;
27443b3a8eb9SGleb Smirnoff 			filter_opts.flags.b2 |= $1.b2;
27453b3a8eb9SGleb Smirnoff 			filter_opts.flags.w |= $1.w;
27463b3a8eb9SGleb Smirnoff 			filter_opts.flags.w2 |= $1.w2;
27473b3a8eb9SGleb Smirnoff 		}
27483b3a8eb9SGleb Smirnoff 		| icmpspec {
27493b3a8eb9SGleb Smirnoff 			if (filter_opts.marker & FOM_ICMP) {
27503b3a8eb9SGleb Smirnoff 				yyerror("icmp-type cannot be redefined");
27513b3a8eb9SGleb Smirnoff 				YYERROR;
27523b3a8eb9SGleb Smirnoff 			}
27533b3a8eb9SGleb Smirnoff 			filter_opts.marker |= FOM_ICMP;
27543b3a8eb9SGleb Smirnoff 			filter_opts.icmpspec = $1;
27553b3a8eb9SGleb Smirnoff 		}
27563e248e0fSKristof Provost 		| PRIO NUMBER {
27573e248e0fSKristof Provost 			if (filter_opts.marker & FOM_PRIO) {
27583e248e0fSKristof Provost 				yyerror("prio cannot be redefined");
27593e248e0fSKristof Provost 				YYERROR;
27603e248e0fSKristof Provost 			}
27613e248e0fSKristof Provost 			if ($2 < 0 || $2 > PF_PRIO_MAX) {
27623e248e0fSKristof Provost 				yyerror("prio must be 0 - %u", PF_PRIO_MAX);
27633e248e0fSKristof Provost 				YYERROR;
27643e248e0fSKristof Provost 			}
27653e248e0fSKristof Provost 			filter_opts.marker |= FOM_PRIO;
27663e248e0fSKristof Provost 			filter_opts.prio = $2;
27673e248e0fSKristof Provost 		}
27683b3a8eb9SGleb Smirnoff 		| TOS tos {
27693b3a8eb9SGleb Smirnoff 			if (filter_opts.marker & FOM_TOS) {
27703b3a8eb9SGleb Smirnoff 				yyerror("tos cannot be redefined");
27713b3a8eb9SGleb Smirnoff 				YYERROR;
27723b3a8eb9SGleb Smirnoff 			}
27733b3a8eb9SGleb Smirnoff 			filter_opts.marker |= FOM_TOS;
27743b3a8eb9SGleb Smirnoff 			filter_opts.tos = $2;
27753b3a8eb9SGleb Smirnoff 		}
27763b3a8eb9SGleb Smirnoff 		| keep {
27773b3a8eb9SGleb Smirnoff 			if (filter_opts.marker & FOM_KEEP) {
27783b3a8eb9SGleb Smirnoff 				yyerror("modulate or keep cannot be redefined");
27793b3a8eb9SGleb Smirnoff 				YYERROR;
27803b3a8eb9SGleb Smirnoff 			}
27813b3a8eb9SGleb Smirnoff 			filter_opts.marker |= FOM_KEEP;
27823b3a8eb9SGleb Smirnoff 			filter_opts.keep.action = $1.action;
27833b3a8eb9SGleb Smirnoff 			filter_opts.keep.options = $1.options;
27843b3a8eb9SGleb Smirnoff 		}
278576c5eeccSKristof Provost 		| RIDENTIFIER number {
278676c5eeccSKristof Provost 			filter_opts.ridentifier = $2;
278776c5eeccSKristof Provost 		}
27883b3a8eb9SGleb Smirnoff 		| FRAGMENT {
27893b3a8eb9SGleb Smirnoff 			filter_opts.fragment = 1;
27903b3a8eb9SGleb Smirnoff 		}
27913b3a8eb9SGleb Smirnoff 		| ALLOWOPTS {
27923b3a8eb9SGleb Smirnoff 			filter_opts.allowopts = 1;
27933b3a8eb9SGleb Smirnoff 		}
27943b3a8eb9SGleb Smirnoff 		| label	{
27956fcc8e04SKristof Provost 			if (filter_opts.labelcount >= PF_RULE_MAX_LABEL_COUNT) {
27966fcc8e04SKristof Provost 				yyerror("label can only be used %d times", PF_RULE_MAX_LABEL_COUNT);
27973b3a8eb9SGleb Smirnoff 				YYERROR;
27983b3a8eb9SGleb Smirnoff 			}
27996fcc8e04SKristof Provost 			filter_opts.label[filter_opts.labelcount++] = $1;
28003b3a8eb9SGleb Smirnoff 		}
28013b3a8eb9SGleb Smirnoff 		| qname	{
28023b3a8eb9SGleb Smirnoff 			if (filter_opts.queues.qname) {
28033b3a8eb9SGleb Smirnoff 				yyerror("queue cannot be redefined");
28043b3a8eb9SGleb Smirnoff 				YYERROR;
28053b3a8eb9SGleb Smirnoff 			}
28063b3a8eb9SGleb Smirnoff 			filter_opts.queues = $1;
28073b3a8eb9SGleb Smirnoff 		}
280863b3c1c7SKristof Provost 		| DNPIPE number {
280963b3c1c7SKristof Provost 			filter_opts.dnpipe = $2;
281063b3c1c7SKristof Provost 			filter_opts.free_flags |= PFRULE_DN_IS_PIPE;
281163b3c1c7SKristof Provost 		}
281263b3c1c7SKristof Provost 		| DNPIPE '(' number ')' {
281363b3c1c7SKristof Provost 			filter_opts.dnpipe = $3;
281463b3c1c7SKristof Provost 			filter_opts.free_flags |= PFRULE_DN_IS_PIPE;
281563b3c1c7SKristof Provost 		}
281663b3c1c7SKristof Provost 		| DNPIPE '(' number comma number ')' {
281763b3c1c7SKristof Provost 			filter_opts.dnrpipe = $5;
281863b3c1c7SKristof Provost 			filter_opts.dnpipe = $3;
281963b3c1c7SKristof Provost 			filter_opts.free_flags |= PFRULE_DN_IS_PIPE;
282063b3c1c7SKristof Provost 		}
282163b3c1c7SKristof Provost 		| DNQUEUE number {
282263b3c1c7SKristof Provost 			filter_opts.dnpipe = $2;
282363b3c1c7SKristof Provost 			filter_opts.free_flags |= PFRULE_DN_IS_QUEUE;
282463b3c1c7SKristof Provost 		}
282563b3c1c7SKristof Provost 		| DNQUEUE '(' number comma number ')' {
282663b3c1c7SKristof Provost 			filter_opts.dnrpipe = $5;
282763b3c1c7SKristof Provost 			filter_opts.dnpipe = $3;
282863b3c1c7SKristof Provost 			filter_opts.free_flags |= PFRULE_DN_IS_QUEUE;
282963b3c1c7SKristof Provost 		}
283063b3c1c7SKristof Provost 		| DNQUEUE '(' number ')' {
283163b3c1c7SKristof Provost 			filter_opts.dnpipe = $3;
283263b3c1c7SKristof Provost 			filter_opts.free_flags |= PFRULE_DN_IS_QUEUE;
283363b3c1c7SKristof Provost 		}
28343b3a8eb9SGleb Smirnoff 		| TAG string				{
28353b3a8eb9SGleb Smirnoff 			filter_opts.tag = $2;
28363b3a8eb9SGleb Smirnoff 		}
28373b3a8eb9SGleb Smirnoff 		| not TAGGED string			{
28383b3a8eb9SGleb Smirnoff 			filter_opts.match_tag = $3;
28393b3a8eb9SGleb Smirnoff 			filter_opts.match_tag_not = $1;
28403b3a8eb9SGleb Smirnoff 		}
28413b3a8eb9SGleb Smirnoff 		| PROBABILITY probability		{
28423b3a8eb9SGleb Smirnoff 			double	p;
28433b3a8eb9SGleb Smirnoff 
28443b3a8eb9SGleb Smirnoff 			p = floor($2 * UINT_MAX + 0.5);
28453b3a8eb9SGleb Smirnoff 			if (p < 0.0 || p > UINT_MAX) {
28463b3a8eb9SGleb Smirnoff 				yyerror("invalid probability: %lf", p);
28473b3a8eb9SGleb Smirnoff 				YYERROR;
28483b3a8eb9SGleb Smirnoff 			}
28493b3a8eb9SGleb Smirnoff 			filter_opts.prob = (u_int32_t)p;
28503b3a8eb9SGleb Smirnoff 			if (filter_opts.prob == 0)
28513b3a8eb9SGleb Smirnoff 				filter_opts.prob = 1;
28523b3a8eb9SGleb Smirnoff 		}
28533b3a8eb9SGleb Smirnoff 		| RTABLE NUMBER				{
28543b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > rt_tableid_max()) {
28553b3a8eb9SGleb Smirnoff 				yyerror("invalid rtable id");
28563b3a8eb9SGleb Smirnoff 				YYERROR;
28573b3a8eb9SGleb Smirnoff 			}
28583b3a8eb9SGleb Smirnoff 			filter_opts.rtableid = $2;
28593b3a8eb9SGleb Smirnoff 		}
28603b3a8eb9SGleb Smirnoff 		| DIVERTTO portplain {
28613b3a8eb9SGleb Smirnoff #ifdef __FreeBSD__
28623b3a8eb9SGleb Smirnoff 			filter_opts.divert.port = $2.a;
28633b3a8eb9SGleb Smirnoff 			if (!filter_opts.divert.port) {
28643b3a8eb9SGleb Smirnoff 				yyerror("invalid divert port: %u", ntohs($2.a));
28653b3a8eb9SGleb Smirnoff 				YYERROR;
28663b3a8eb9SGleb Smirnoff 			}
28673b3a8eb9SGleb Smirnoff #endif
28683b3a8eb9SGleb Smirnoff 		}
28693b3a8eb9SGleb Smirnoff 		| DIVERTTO STRING PORT portplain {
28703b3a8eb9SGleb Smirnoff #ifndef __FreeBSD__
28713b3a8eb9SGleb Smirnoff 			if ((filter_opts.divert.addr = host($2)) == NULL) {
28723b3a8eb9SGleb Smirnoff 				yyerror("could not parse divert address: %s",
28733b3a8eb9SGleb Smirnoff 				    $2);
28743b3a8eb9SGleb Smirnoff 				free($2);
28753b3a8eb9SGleb Smirnoff 				YYERROR;
28763b3a8eb9SGleb Smirnoff 			}
28773b3a8eb9SGleb Smirnoff #else
28783b3a8eb9SGleb Smirnoff 			if ($2)
28793b3a8eb9SGleb Smirnoff #endif
28803b3a8eb9SGleb Smirnoff 			free($2);
28813b3a8eb9SGleb Smirnoff 			filter_opts.divert.port = $4.a;
28823b3a8eb9SGleb Smirnoff 			if (!filter_opts.divert.port) {
28833b3a8eb9SGleb Smirnoff 				yyerror("invalid divert port: %u", ntohs($4.a));
28843b3a8eb9SGleb Smirnoff 				YYERROR;
28853b3a8eb9SGleb Smirnoff 			}
28863b3a8eb9SGleb Smirnoff 		}
28873b3a8eb9SGleb Smirnoff 		| DIVERTREPLY {
28883b3a8eb9SGleb Smirnoff #ifdef __FreeBSD__
28893b3a8eb9SGleb Smirnoff 			yyerror("divert-reply has no meaning in FreeBSD pf(4)");
28903b3a8eb9SGleb Smirnoff 			YYERROR;
28913b3a8eb9SGleb Smirnoff #else
28923b3a8eb9SGleb Smirnoff 			filter_opts.divert.port = 1;	/* some random value */
28933b3a8eb9SGleb Smirnoff #endif
28943b3a8eb9SGleb Smirnoff 		}
28953e248e0fSKristof Provost 		| filter_sets
28963e248e0fSKristof Provost 		;
28973e248e0fSKristof Provost 
28983e248e0fSKristof Provost filter_sets	: SET '(' filter_sets_l ')'	{ $$ = filter_opts; }
28993e248e0fSKristof Provost 		| SET filter_set		{ $$ = filter_opts; }
29003e248e0fSKristof Provost 		;
29013e248e0fSKristof Provost 
29023e248e0fSKristof Provost filter_sets_l	: filter_sets_l comma filter_set
29033e248e0fSKristof Provost 		| filter_set
29043e248e0fSKristof Provost 		;
29053e248e0fSKristof Provost 
29063e248e0fSKristof Provost filter_set	: prio {
29073e248e0fSKristof Provost 			if (filter_opts.marker & FOM_SETPRIO) {
29083e248e0fSKristof Provost 				yyerror("prio cannot be redefined");
29093e248e0fSKristof Provost 				YYERROR;
29103e248e0fSKristof Provost 			}
29113e248e0fSKristof Provost 			filter_opts.marker |= FOM_SETPRIO;
29123e248e0fSKristof Provost 			filter_opts.set_prio[0] = $1.b1;
29133e248e0fSKristof Provost 			filter_opts.set_prio[1] = $1.b2;
29143e248e0fSKristof Provost 		}
29153e248e0fSKristof Provost prio		: PRIO NUMBER {
29163e248e0fSKristof Provost 			if ($2 < 0 || $2 > PF_PRIO_MAX) {
29173e248e0fSKristof Provost 				yyerror("prio must be 0 - %u", PF_PRIO_MAX);
29183e248e0fSKristof Provost 				YYERROR;
29193e248e0fSKristof Provost 			}
29203e248e0fSKristof Provost 			$$.b1 = $$.b2 = $2;
29213e248e0fSKristof Provost 		}
29223e248e0fSKristof Provost 		| PRIO '(' NUMBER comma NUMBER ')' {
29233e248e0fSKristof Provost 			if ($3 < 0 || $3 > PF_PRIO_MAX ||
29243e248e0fSKristof Provost 			    $5 < 0 || $5 > PF_PRIO_MAX) {
29253e248e0fSKristof Provost 				yyerror("prio must be 0 - %u", PF_PRIO_MAX);
29263e248e0fSKristof Provost 				YYERROR;
29273e248e0fSKristof Provost 			}
29283e248e0fSKristof Provost 			$$.b1 = $3;
29293e248e0fSKristof Provost 			$$.b2 = $5;
29303e248e0fSKristof Provost 		}
29313b3a8eb9SGleb Smirnoff 		;
29323b3a8eb9SGleb Smirnoff 
29333b3a8eb9SGleb Smirnoff probability	: STRING				{
29343b3a8eb9SGleb Smirnoff 			char	*e;
29353b3a8eb9SGleb Smirnoff 			double	 p = strtod($1, &e);
29363b3a8eb9SGleb Smirnoff 
29373b3a8eb9SGleb Smirnoff 			if (*e == '%') {
29383b3a8eb9SGleb Smirnoff 				p *= 0.01;
29393b3a8eb9SGleb Smirnoff 				e++;
29403b3a8eb9SGleb Smirnoff 			}
29413b3a8eb9SGleb Smirnoff 			if (*e) {
29423b3a8eb9SGleb Smirnoff 				yyerror("invalid probability: %s", $1);
29433b3a8eb9SGleb Smirnoff 				free($1);
29443b3a8eb9SGleb Smirnoff 				YYERROR;
29453b3a8eb9SGleb Smirnoff 			}
29463b3a8eb9SGleb Smirnoff 			free($1);
29473b3a8eb9SGleb Smirnoff 			$$ = p;
29483b3a8eb9SGleb Smirnoff 		}
29493b3a8eb9SGleb Smirnoff 		| NUMBER				{
29503b3a8eb9SGleb Smirnoff 			$$ = (double)$1;
29513b3a8eb9SGleb Smirnoff 		}
29523b3a8eb9SGleb Smirnoff 		;
29533b3a8eb9SGleb Smirnoff 
29543b3a8eb9SGleb Smirnoff 
2955150182e3SKristof Provost action		: PASS 			{
2956150182e3SKristof Provost 			$$.b1 = PF_PASS;
2957150182e3SKristof Provost 			$$.b2 = failpolicy;
2958150182e3SKristof Provost 			$$.w = returnicmpdefault;
2959150182e3SKristof Provost 			$$.w2 = returnicmp6default;
2960150182e3SKristof Provost 		}
2961ef950daaSKristof Provost 		| MATCH			{ $$.b1 = PF_MATCH; $$.b2 = $$.w = 0; }
29623b3a8eb9SGleb Smirnoff 		| BLOCK blockspec	{ $$ = $2; $$.b1 = PF_DROP; }
29633b3a8eb9SGleb Smirnoff 		;
29643b3a8eb9SGleb Smirnoff 
29653b3a8eb9SGleb Smirnoff blockspec	: /* empty */		{
29663b3a8eb9SGleb Smirnoff 			$$.b2 = blockpolicy;
29673b3a8eb9SGleb Smirnoff 			$$.w = returnicmpdefault;
29683b3a8eb9SGleb Smirnoff 			$$.w2 = returnicmp6default;
29693b3a8eb9SGleb Smirnoff 		}
29703b3a8eb9SGleb Smirnoff 		| DROP			{
29713b3a8eb9SGleb Smirnoff 			$$.b2 = PFRULE_DROP;
29723b3a8eb9SGleb Smirnoff 			$$.w = 0;
29733b3a8eb9SGleb Smirnoff 			$$.w2 = 0;
29743b3a8eb9SGleb Smirnoff 		}
29753b3a8eb9SGleb Smirnoff 		| RETURNRST		{
29763b3a8eb9SGleb Smirnoff 			$$.b2 = PFRULE_RETURNRST;
29773b3a8eb9SGleb Smirnoff 			$$.w = 0;
29783b3a8eb9SGleb Smirnoff 			$$.w2 = 0;
29793b3a8eb9SGleb Smirnoff 		}
29803b3a8eb9SGleb Smirnoff 		| RETURNRST '(' TTL NUMBER ')'	{
29813b3a8eb9SGleb Smirnoff 			if ($4 < 0 || $4 > 255) {
29823b3a8eb9SGleb Smirnoff 				yyerror("illegal ttl value %d", $4);
29833b3a8eb9SGleb Smirnoff 				YYERROR;
29843b3a8eb9SGleb Smirnoff 			}
29853b3a8eb9SGleb Smirnoff 			$$.b2 = PFRULE_RETURNRST;
29863b3a8eb9SGleb Smirnoff 			$$.w = $4;
29873b3a8eb9SGleb Smirnoff 			$$.w2 = 0;
29883b3a8eb9SGleb Smirnoff 		}
29893b3a8eb9SGleb Smirnoff 		| RETURNICMP		{
29903b3a8eb9SGleb Smirnoff 			$$.b2 = PFRULE_RETURNICMP;
29913b3a8eb9SGleb Smirnoff 			$$.w = returnicmpdefault;
29923b3a8eb9SGleb Smirnoff 			$$.w2 = returnicmp6default;
29933b3a8eb9SGleb Smirnoff 		}
29943b3a8eb9SGleb Smirnoff 		| RETURNICMP6		{
29953b3a8eb9SGleb Smirnoff 			$$.b2 = PFRULE_RETURNICMP;
29963b3a8eb9SGleb Smirnoff 			$$.w = returnicmpdefault;
29973b3a8eb9SGleb Smirnoff 			$$.w2 = returnicmp6default;
29983b3a8eb9SGleb Smirnoff 		}
29993b3a8eb9SGleb Smirnoff 		| RETURNICMP '(' reticmpspec ')'	{
30003b3a8eb9SGleb Smirnoff 			$$.b2 = PFRULE_RETURNICMP;
30013b3a8eb9SGleb Smirnoff 			$$.w = $3;
30023b3a8eb9SGleb Smirnoff 			$$.w2 = returnicmpdefault;
30033b3a8eb9SGleb Smirnoff 		}
30043b3a8eb9SGleb Smirnoff 		| RETURNICMP6 '(' reticmp6spec ')'	{
30053b3a8eb9SGleb Smirnoff 			$$.b2 = PFRULE_RETURNICMP;
30063b3a8eb9SGleb Smirnoff 			$$.w = returnicmpdefault;
30073b3a8eb9SGleb Smirnoff 			$$.w2 = $3;
30083b3a8eb9SGleb Smirnoff 		}
30093b3a8eb9SGleb Smirnoff 		| RETURNICMP '(' reticmpspec comma reticmp6spec ')' {
30103b3a8eb9SGleb Smirnoff 			$$.b2 = PFRULE_RETURNICMP;
30113b3a8eb9SGleb Smirnoff 			$$.w = $3;
30123b3a8eb9SGleb Smirnoff 			$$.w2 = $5;
30133b3a8eb9SGleb Smirnoff 		}
30143b3a8eb9SGleb Smirnoff 		| RETURN {
30153b3a8eb9SGleb Smirnoff 			$$.b2 = PFRULE_RETURN;
30163b3a8eb9SGleb Smirnoff 			$$.w = returnicmpdefault;
30173b3a8eb9SGleb Smirnoff 			$$.w2 = returnicmp6default;
30183b3a8eb9SGleb Smirnoff 		}
30193b3a8eb9SGleb Smirnoff 		;
30203b3a8eb9SGleb Smirnoff 
30213b3a8eb9SGleb Smirnoff reticmpspec	: STRING			{
30223b3a8eb9SGleb Smirnoff 			if (!($$ = parseicmpspec($1, AF_INET))) {
30233b3a8eb9SGleb Smirnoff 				free($1);
30243b3a8eb9SGleb Smirnoff 				YYERROR;
30253b3a8eb9SGleb Smirnoff 			}
30263b3a8eb9SGleb Smirnoff 			free($1);
30273b3a8eb9SGleb Smirnoff 		}
30283b3a8eb9SGleb Smirnoff 		| NUMBER			{
30293b3a8eb9SGleb Smirnoff 			u_int8_t		icmptype;
30303b3a8eb9SGleb Smirnoff 
30313b3a8eb9SGleb Smirnoff 			if ($1 < 0 || $1 > 255) {
30323b3a8eb9SGleb Smirnoff 				yyerror("invalid icmp code %lu", $1);
30333b3a8eb9SGleb Smirnoff 				YYERROR;
30343b3a8eb9SGleb Smirnoff 			}
30353b3a8eb9SGleb Smirnoff 			icmptype = returnicmpdefault >> 8;
30363b3a8eb9SGleb Smirnoff 			$$ = (icmptype << 8 | $1);
30373b3a8eb9SGleb Smirnoff 		}
30383b3a8eb9SGleb Smirnoff 		;
30393b3a8eb9SGleb Smirnoff 
30403b3a8eb9SGleb Smirnoff reticmp6spec	: STRING			{
30413b3a8eb9SGleb Smirnoff 			if (!($$ = parseicmpspec($1, AF_INET6))) {
30423b3a8eb9SGleb Smirnoff 				free($1);
30433b3a8eb9SGleb Smirnoff 				YYERROR;
30443b3a8eb9SGleb Smirnoff 			}
30453b3a8eb9SGleb Smirnoff 			free($1);
30463b3a8eb9SGleb Smirnoff 		}
30473b3a8eb9SGleb Smirnoff 		| NUMBER			{
30483b3a8eb9SGleb Smirnoff 			u_int8_t		icmptype;
30493b3a8eb9SGleb Smirnoff 
30503b3a8eb9SGleb Smirnoff 			if ($1 < 0 || $1 > 255) {
30513b3a8eb9SGleb Smirnoff 				yyerror("invalid icmp code %lu", $1);
30523b3a8eb9SGleb Smirnoff 				YYERROR;
30533b3a8eb9SGleb Smirnoff 			}
30543b3a8eb9SGleb Smirnoff 			icmptype = returnicmp6default >> 8;
30553b3a8eb9SGleb Smirnoff 			$$ = (icmptype << 8 | $1);
30563b3a8eb9SGleb Smirnoff 		}
30573b3a8eb9SGleb Smirnoff 		;
30583b3a8eb9SGleb Smirnoff 
30593b3a8eb9SGleb Smirnoff dir		: /* empty */			{ $$ = PF_INOUT; }
30603b3a8eb9SGleb Smirnoff 		| IN				{ $$ = PF_IN; }
30613b3a8eb9SGleb Smirnoff 		| OUT				{ $$ = PF_OUT; }
30623b3a8eb9SGleb Smirnoff 		;
30633b3a8eb9SGleb Smirnoff 
30643b3a8eb9SGleb Smirnoff quick		: /* empty */			{ $$.quick = 0; }
30653b3a8eb9SGleb Smirnoff 		| QUICK				{ $$.quick = 1; }
30663b3a8eb9SGleb Smirnoff 		;
30673b3a8eb9SGleb Smirnoff 
30683b3a8eb9SGleb Smirnoff logquick	: /* empty */	{ $$.log = 0; $$.quick = 0; $$.logif = 0; }
30693b3a8eb9SGleb Smirnoff 		| log		{ $$ = $1; $$.quick = 0; }
30703b3a8eb9SGleb Smirnoff 		| QUICK		{ $$.quick = 1; $$.log = 0; $$.logif = 0; }
30713b3a8eb9SGleb Smirnoff 		| log QUICK	{ $$ = $1; $$.quick = 1; }
30723b3a8eb9SGleb Smirnoff 		| QUICK log	{ $$ = $2; $$.quick = 1; }
30733b3a8eb9SGleb Smirnoff 		;
30743b3a8eb9SGleb Smirnoff 
30753b3a8eb9SGleb Smirnoff log		: LOG			{ $$.log = PF_LOG; $$.logif = 0; }
30763b3a8eb9SGleb Smirnoff 		| LOG '(' logopts ')'	{
30773b3a8eb9SGleb Smirnoff 			$$.log = PF_LOG | $3.log;
30783b3a8eb9SGleb Smirnoff 			$$.logif = $3.logif;
30793b3a8eb9SGleb Smirnoff 		}
30803b3a8eb9SGleb Smirnoff 		;
30813b3a8eb9SGleb Smirnoff 
30823b3a8eb9SGleb Smirnoff logopts		: logopt			{ $$ = $1; }
30833b3a8eb9SGleb Smirnoff 		| logopts comma logopt		{
30843b3a8eb9SGleb Smirnoff 			$$.log = $1.log | $3.log;
30853b3a8eb9SGleb Smirnoff 			$$.logif = $3.logif;
30863b3a8eb9SGleb Smirnoff 			if ($$.logif == 0)
30873b3a8eb9SGleb Smirnoff 				$$.logif = $1.logif;
30883b3a8eb9SGleb Smirnoff 		}
30893b3a8eb9SGleb Smirnoff 		;
30903b3a8eb9SGleb Smirnoff 
30913b3a8eb9SGleb Smirnoff logopt		: ALL		{ $$.log = PF_LOG_ALL; $$.logif = 0; }
30923b3a8eb9SGleb Smirnoff 		| USER		{ $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
30933b3a8eb9SGleb Smirnoff 		| GROUP		{ $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
30943b3a8eb9SGleb Smirnoff 		| TO string	{
30953b3a8eb9SGleb Smirnoff 			const char	*errstr;
30963b3a8eb9SGleb Smirnoff 			u_int		 i;
30973b3a8eb9SGleb Smirnoff 
30983b3a8eb9SGleb Smirnoff 			$$.log = 0;
30993b3a8eb9SGleb Smirnoff 			if (strncmp($2, "pflog", 5)) {
31003b3a8eb9SGleb Smirnoff 				yyerror("%s: should be a pflog interface", $2);
31013b3a8eb9SGleb Smirnoff 				free($2);
31023b3a8eb9SGleb Smirnoff 				YYERROR;
31033b3a8eb9SGleb Smirnoff 			}
31043b3a8eb9SGleb Smirnoff 			i = strtonum($2 + 5, 0, 255, &errstr);
31053b3a8eb9SGleb Smirnoff 			if (errstr) {
31063b3a8eb9SGleb Smirnoff 				yyerror("%s: %s", $2, errstr);
31073b3a8eb9SGleb Smirnoff 				free($2);
31083b3a8eb9SGleb Smirnoff 				YYERROR;
31093b3a8eb9SGleb Smirnoff 			}
31103b3a8eb9SGleb Smirnoff 			free($2);
31113b3a8eb9SGleb Smirnoff 			$$.logif = i;
31123b3a8eb9SGleb Smirnoff 		}
31133b3a8eb9SGleb Smirnoff 		;
31143b3a8eb9SGleb Smirnoff 
31153b3a8eb9SGleb Smirnoff interface	: /* empty */			{ $$ = NULL; }
31163b3a8eb9SGleb Smirnoff 		| ON if_item_not		{ $$ = $2; }
31173b3a8eb9SGleb Smirnoff 		| ON '{' optnl if_list '}'	{ $$ = $4; }
31183b3a8eb9SGleb Smirnoff 		;
31193b3a8eb9SGleb Smirnoff 
31203b3a8eb9SGleb Smirnoff if_list		: if_item_not optnl		{ $$ = $1; }
31213b3a8eb9SGleb Smirnoff 		| if_list comma if_item_not optnl	{
31223b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
31233b3a8eb9SGleb Smirnoff 			$1->tail = $3;
31243b3a8eb9SGleb Smirnoff 			$$ = $1;
31253b3a8eb9SGleb Smirnoff 		}
31263b3a8eb9SGleb Smirnoff 		;
31273b3a8eb9SGleb Smirnoff 
31283b3a8eb9SGleb Smirnoff if_item_not	: not if_item			{ $$ = $2; $$->not = $1; }
31293b3a8eb9SGleb Smirnoff 		;
31303b3a8eb9SGleb Smirnoff 
31313b3a8eb9SGleb Smirnoff if_item		: STRING			{
31323b3a8eb9SGleb Smirnoff 			struct node_host	*n;
31333b3a8eb9SGleb Smirnoff 
31343b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_if));
31353b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
31363b3a8eb9SGleb Smirnoff 				err(1, "if_item: calloc");
31373b3a8eb9SGleb Smirnoff 			if (strlcpy($$->ifname, $1, sizeof($$->ifname)) >=
31383b3a8eb9SGleb Smirnoff 			    sizeof($$->ifname)) {
31393b3a8eb9SGleb Smirnoff 				free($1);
31403b3a8eb9SGleb Smirnoff 				free($$);
31413b3a8eb9SGleb Smirnoff 				yyerror("interface name too long");
31423b3a8eb9SGleb Smirnoff 				YYERROR;
31433b3a8eb9SGleb Smirnoff 			}
31443b3a8eb9SGleb Smirnoff 
31453b3a8eb9SGleb Smirnoff 			if ((n = ifa_exists($1)) != NULL)
31463b3a8eb9SGleb Smirnoff 				$$->ifa_flags = n->ifa_flags;
31473b3a8eb9SGleb Smirnoff 
31483b3a8eb9SGleb Smirnoff 			free($1);
31493b3a8eb9SGleb Smirnoff 			$$->not = 0;
31503b3a8eb9SGleb Smirnoff 			$$->next = NULL;
31513b3a8eb9SGleb Smirnoff 			$$->tail = $$;
31523b3a8eb9SGleb Smirnoff 		}
31533b3a8eb9SGleb Smirnoff 		;
31543b3a8eb9SGleb Smirnoff 
31553b3a8eb9SGleb Smirnoff af		: /* empty */			{ $$ = 0; }
31563b3a8eb9SGleb Smirnoff 		| INET				{ $$ = AF_INET; }
31573b3a8eb9SGleb Smirnoff 		| INET6				{ $$ = AF_INET6; }
31583b3a8eb9SGleb Smirnoff 		;
31593b3a8eb9SGleb Smirnoff 
31602b29ceb8SKristof Provost etherproto	: /* empty */				{ $$ = NULL; }
31612b29ceb8SKristof Provost 		| PROTO etherproto_item			{ $$ = $2; }
31622b29ceb8SKristof Provost 		| PROTO '{' optnl etherproto_list '}'	{ $$ = $4; }
31632b29ceb8SKristof Provost 		;
31642b29ceb8SKristof Provost 
31652b29ceb8SKristof Provost etherproto_list	: etherproto_item optnl			{ $$ = $1; }
31662b29ceb8SKristof Provost 		| etherproto_list comma etherproto_item optnl	{
31672b29ceb8SKristof Provost 			$1->tail->next = $3;
31682b29ceb8SKristof Provost 			$1->tail = $3;
31692b29ceb8SKristof Provost 			$$ = $1;
31702b29ceb8SKristof Provost 		}
31712b29ceb8SKristof Provost 		;
31722b29ceb8SKristof Provost 
31732b29ceb8SKristof Provost etherproto_item	: etherprotoval		{
31742b29ceb8SKristof Provost 			u_int16_t	pr;
31752b29ceb8SKristof Provost 
31762b29ceb8SKristof Provost 			pr = (u_int16_t)$1;
31772b29ceb8SKristof Provost 			if (pr == 0) {
31782b29ceb8SKristof Provost 				yyerror("proto 0 cannot be used");
31792b29ceb8SKristof Provost 				YYERROR;
31802b29ceb8SKristof Provost 			}
31812b29ceb8SKristof Provost 			$$ = calloc(1, sizeof(struct node_proto));
31822b29ceb8SKristof Provost 			if ($$ == NULL)
31832b29ceb8SKristof Provost 				err(1, "proto_item: calloc");
31842b29ceb8SKristof Provost 			$$->proto = pr;
31852b29ceb8SKristof Provost 			$$->next = NULL;
31862b29ceb8SKristof Provost 			$$->tail = $$;
31872b29ceb8SKristof Provost 		}
31882b29ceb8SKristof Provost 		;
31892b29ceb8SKristof Provost 
31902b29ceb8SKristof Provost etherprotoval	: NUMBER			{
31912b29ceb8SKristof Provost 			if ($1 < 0 || $1 > 65565) {
31922b29ceb8SKristof Provost 				yyerror("protocol outside range");
31932b29ceb8SKristof Provost 				YYERROR;
31942b29ceb8SKristof Provost 			}
31952b29ceb8SKristof Provost 		}
31962b29ceb8SKristof Provost 		| STRING
31972b29ceb8SKristof Provost 		{
31982b29ceb8SKristof Provost 			if (!strncmp($1, "0x", 2)) {
31992b29ceb8SKristof Provost 				if (sscanf($1, "0x%4x", &$$) != 1) {
32002b29ceb8SKristof Provost 					free($1);
32012b29ceb8SKristof Provost 					yyerror("invalid EtherType hex");
32022b29ceb8SKristof Provost 					YYERROR;
32032b29ceb8SKristof Provost 				}
32042b29ceb8SKristof Provost 			} else {
32052b29ceb8SKristof Provost 				yyerror("Symbolic EtherType not yet supported");
32062b29ceb8SKristof Provost 			}
32072b29ceb8SKristof Provost 		}
32082b29ceb8SKristof Provost 		;
32092b29ceb8SKristof Provost 
32103b3a8eb9SGleb Smirnoff proto		: /* empty */				{ $$ = NULL; }
32113b3a8eb9SGleb Smirnoff 		| PROTO proto_item			{ $$ = $2; }
32123b3a8eb9SGleb Smirnoff 		| PROTO '{' optnl proto_list '}'	{ $$ = $4; }
32133b3a8eb9SGleb Smirnoff 		;
32143b3a8eb9SGleb Smirnoff 
32153b3a8eb9SGleb Smirnoff proto_list	: proto_item optnl		{ $$ = $1; }
32163b3a8eb9SGleb Smirnoff 		| proto_list comma proto_item optnl	{
32173b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
32183b3a8eb9SGleb Smirnoff 			$1->tail = $3;
32193b3a8eb9SGleb Smirnoff 			$$ = $1;
32203b3a8eb9SGleb Smirnoff 		}
32213b3a8eb9SGleb Smirnoff 		;
32223b3a8eb9SGleb Smirnoff 
32233b3a8eb9SGleb Smirnoff proto_item	: protoval			{
32243b3a8eb9SGleb Smirnoff 			u_int8_t	pr;
32253b3a8eb9SGleb Smirnoff 
32263b3a8eb9SGleb Smirnoff 			pr = (u_int8_t)$1;
32273b3a8eb9SGleb Smirnoff 			if (pr == 0) {
32283b3a8eb9SGleb Smirnoff 				yyerror("proto 0 cannot be used");
32293b3a8eb9SGleb Smirnoff 				YYERROR;
32303b3a8eb9SGleb Smirnoff 			}
32313b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_proto));
32323b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
32333b3a8eb9SGleb Smirnoff 				err(1, "proto_item: calloc");
32343b3a8eb9SGleb Smirnoff 			$$->proto = pr;
32353b3a8eb9SGleb Smirnoff 			$$->next = NULL;
32363b3a8eb9SGleb Smirnoff 			$$->tail = $$;
32373b3a8eb9SGleb Smirnoff 		}
32383b3a8eb9SGleb Smirnoff 		;
32393b3a8eb9SGleb Smirnoff 
32403b3a8eb9SGleb Smirnoff protoval	: STRING			{
32413b3a8eb9SGleb Smirnoff 			struct protoent	*p;
32423b3a8eb9SGleb Smirnoff 
32433b3a8eb9SGleb Smirnoff 			p = getprotobyname($1);
32443b3a8eb9SGleb Smirnoff 			if (p == NULL) {
32453b3a8eb9SGleb Smirnoff 				yyerror("unknown protocol %s", $1);
32463b3a8eb9SGleb Smirnoff 				free($1);
32473b3a8eb9SGleb Smirnoff 				YYERROR;
32483b3a8eb9SGleb Smirnoff 			}
32493b3a8eb9SGleb Smirnoff 			$$ = p->p_proto;
32503b3a8eb9SGleb Smirnoff 			free($1);
32513b3a8eb9SGleb Smirnoff 		}
32523b3a8eb9SGleb Smirnoff 		| NUMBER			{
32533b3a8eb9SGleb Smirnoff 			if ($1 < 0 || $1 > 255) {
32543b3a8eb9SGleb Smirnoff 				yyerror("protocol outside range");
32553b3a8eb9SGleb Smirnoff 				YYERROR;
32563b3a8eb9SGleb Smirnoff 			}
32573b3a8eb9SGleb Smirnoff 		}
32583b3a8eb9SGleb Smirnoff 		;
32593b3a8eb9SGleb Smirnoff 
32608a42005dSKristof Provost l3fromto	: /* empty */			{
32618a42005dSKristof Provost 			bzero(&$$, sizeof($$));
32628a42005dSKristof Provost 		}
32638a42005dSKristof Provost 		| L3 fromto			{
3264*3468cd95SKristof Provost 			if ($2.src.host != NULL &&
3265*3468cd95SKristof Provost 			    $2.src.host->addr.type != PF_ADDR_ADDRMASK) {
3266*3468cd95SKristof Provost 				yyerror("from must be an address");
3267*3468cd95SKristof Provost 				YYERROR;
3268*3468cd95SKristof Provost 			}
3269*3468cd95SKristof Provost 			if ($2.dst.host != NULL &&
3270*3468cd95SKristof Provost 			    $2.dst.host->addr.type != PF_ADDR_ADDRMASK) {
3271*3468cd95SKristof Provost 				yyerror("to must be an address");
3272*3468cd95SKristof Provost 				YYERROR;
3273*3468cd95SKristof Provost 			}
32748a42005dSKristof Provost 			$$ = $2;
32758a42005dSKristof Provost 		}
32768a42005dSKristof Provost 		;
32772b29ceb8SKristof Provost etherfromto	: ALL				{
327887a89d6eSKristof Provost 			$$.src = NULL;
327987a89d6eSKristof Provost 			$$.dst = NULL;
32802b29ceb8SKristof Provost 		}
32812b29ceb8SKristof Provost 		| etherfrom etherto		{
328287a89d6eSKristof Provost 			$$.src = $1.mac;
328387a89d6eSKristof Provost 			$$.dst = $2.mac;
32842b29ceb8SKristof Provost 		}
32852b29ceb8SKristof Provost 		;
32862b29ceb8SKristof Provost 
32872b29ceb8SKristof Provost etherfrom	: /* emtpy */			{
32882b29ceb8SKristof Provost 			bzero(&$$, sizeof($$));
32892b29ceb8SKristof Provost 		}
329087a89d6eSKristof Provost 		| FROM macspec			{
329187a89d6eSKristof Provost 			$$.mac = $2;
32922b29ceb8SKristof Provost 		}
32932b29ceb8SKristof Provost 		;
32942b29ceb8SKristof Provost 
32952b29ceb8SKristof Provost etherto		: /* empty */			{
32962b29ceb8SKristof Provost 			bzero(&$$, sizeof($$));
32972b29ceb8SKristof Provost 		}
329887a89d6eSKristof Provost 		| TO macspec			{
329987a89d6eSKristof Provost 			$$.mac = $2;
33002b29ceb8SKristof Provost 		}
33012b29ceb8SKristof Provost 		;
33022b29ceb8SKristof Provost 
3303b590f17aSKristof Provost mac		: string '/' NUMBER		{
3304b590f17aSKristof Provost 			$$ = node_mac_from_string_masklen($1, $3);
33052b29ceb8SKristof Provost 			free($1);
3306b590f17aSKristof Provost 			if ($$ == NULL)
33072b29ceb8SKristof Provost 				YYERROR;
33082b29ceb8SKristof Provost 		}
3309b590f17aSKristof Provost 		| string			{
3310b590f17aSKristof Provost 			if (strchr($1, '&')) {
3311b590f17aSKristof Provost 				/* mac&mask */
3312b590f17aSKristof Provost 				char *mac = strtok($1, "&");
3313b590f17aSKristof Provost 				char *mask = strtok(NULL, "&");
3314b590f17aSKristof Provost 				$$ = node_mac_from_string_mask(mac, mask);
3315b590f17aSKristof Provost 			} else {
3316b590f17aSKristof Provost 				$$ = node_mac_from_string($1);
3317b590f17aSKristof Provost 			}
331887a89d6eSKristof Provost 			free($1);
3319b590f17aSKristof Provost 			if ($$ == NULL)
3320b590f17aSKristof Provost 				YYERROR;
3321b590f17aSKristof Provost 
332287a89d6eSKristof Provost 		}
332387a89d6eSKristof Provost xmac		: not mac {
332487a89d6eSKristof Provost 			struct node_mac	*n;
332587a89d6eSKristof Provost 
332687a89d6eSKristof Provost 			for (n = $2; n != NULL; n = n->next)
332787a89d6eSKristof Provost 				n->neg = $1;
332887a89d6eSKristof Provost 			$$ = $2;
33292b29ceb8SKristof Provost 		}
33302b29ceb8SKristof Provost 		;
333187a89d6eSKristof Provost macspec		: xmac {
333287a89d6eSKristof Provost 			$$ = $1;
333387a89d6eSKristof Provost 		}
333487a89d6eSKristof Provost 		| '{' optnl mac_list '}'
333587a89d6eSKristof Provost 		{
333687a89d6eSKristof Provost 			$$ = $3;
333787a89d6eSKristof Provost 		}
333887a89d6eSKristof Provost 		;
333987a89d6eSKristof Provost mac_list	: xmac optnl {
334087a89d6eSKristof Provost 			$$ = $1;
334187a89d6eSKristof Provost 		}
334287a89d6eSKristof Provost 		| mac_list comma xmac {
334387a89d6eSKristof Provost 			if ($3 == NULL)
334487a89d6eSKristof Provost 				$$ = $1;
334587a89d6eSKristof Provost 			else if ($1 == NULL)
334687a89d6eSKristof Provost 				$$ = $3;
334787a89d6eSKristof Provost 			else {
334887a89d6eSKristof Provost 				$1->tail->next = $3;
334987a89d6eSKristof Provost 				$1->tail = $3->tail;
335087a89d6eSKristof Provost 				$$ = $1;
335187a89d6eSKristof Provost 			}
335287a89d6eSKristof Provost 		}
33532b29ceb8SKristof Provost 
33543b3a8eb9SGleb Smirnoff fromto		: ALL				{
33553b3a8eb9SGleb Smirnoff 			$$.src.host = NULL;
33563b3a8eb9SGleb Smirnoff 			$$.src.port = NULL;
33573b3a8eb9SGleb Smirnoff 			$$.dst.host = NULL;
33583b3a8eb9SGleb Smirnoff 			$$.dst.port = NULL;
33593b3a8eb9SGleb Smirnoff 			$$.src_os = NULL;
33603b3a8eb9SGleb Smirnoff 		}
33613b3a8eb9SGleb Smirnoff 		| from os to			{
33623b3a8eb9SGleb Smirnoff 			$$.src = $1;
33633b3a8eb9SGleb Smirnoff 			$$.src_os = $2;
33643b3a8eb9SGleb Smirnoff 			$$.dst = $3;
33653b3a8eb9SGleb Smirnoff 		}
33663b3a8eb9SGleb Smirnoff 		;
33673b3a8eb9SGleb Smirnoff 
33683b3a8eb9SGleb Smirnoff os		: /* empty */			{ $$ = NULL; }
33693b3a8eb9SGleb Smirnoff 		| OS xos			{ $$ = $2; }
33703b3a8eb9SGleb Smirnoff 		| OS '{' optnl os_list '}'	{ $$ = $4; }
33713b3a8eb9SGleb Smirnoff 		;
33723b3a8eb9SGleb Smirnoff 
33733b3a8eb9SGleb Smirnoff xos		: STRING {
33743b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_os));
33753b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
33763b3a8eb9SGleb Smirnoff 				err(1, "os: calloc");
33773b3a8eb9SGleb Smirnoff 			$$->os = $1;
33783b3a8eb9SGleb Smirnoff 			$$->tail = $$;
33793b3a8eb9SGleb Smirnoff 		}
33803b3a8eb9SGleb Smirnoff 		;
33813b3a8eb9SGleb Smirnoff 
33823b3a8eb9SGleb Smirnoff os_list		: xos optnl 			{ $$ = $1; }
33833b3a8eb9SGleb Smirnoff 		| os_list comma xos optnl	{
33843b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
33853b3a8eb9SGleb Smirnoff 			$1->tail = $3;
33863b3a8eb9SGleb Smirnoff 			$$ = $1;
33873b3a8eb9SGleb Smirnoff 		}
33883b3a8eb9SGleb Smirnoff 		;
33893b3a8eb9SGleb Smirnoff 
33903b3a8eb9SGleb Smirnoff from		: /* empty */			{
33913b3a8eb9SGleb Smirnoff 			$$.host = NULL;
33923b3a8eb9SGleb Smirnoff 			$$.port = NULL;
33933b3a8eb9SGleb Smirnoff 		}
33943b3a8eb9SGleb Smirnoff 		| FROM ipportspec		{
33953b3a8eb9SGleb Smirnoff 			$$ = $2;
33963b3a8eb9SGleb Smirnoff 		}
33973b3a8eb9SGleb Smirnoff 		;
33983b3a8eb9SGleb Smirnoff 
33993b3a8eb9SGleb Smirnoff to		: /* empty */			{
34003b3a8eb9SGleb Smirnoff 			$$.host = NULL;
34013b3a8eb9SGleb Smirnoff 			$$.port = NULL;
34023b3a8eb9SGleb Smirnoff 		}
34033b3a8eb9SGleb Smirnoff 		| TO ipportspec		{
34043b3a8eb9SGleb Smirnoff 			if (disallow_urpf_failed($2.host, "\"urpf-failed\" is "
34053b3a8eb9SGleb Smirnoff 			    "not permitted in a destination address"))
34063b3a8eb9SGleb Smirnoff 				YYERROR;
34073b3a8eb9SGleb Smirnoff 			$$ = $2;
34083b3a8eb9SGleb Smirnoff 		}
34093b3a8eb9SGleb Smirnoff 		;
34103b3a8eb9SGleb Smirnoff 
34113b3a8eb9SGleb Smirnoff ipportspec	: ipspec			{
34123b3a8eb9SGleb Smirnoff 			$$.host = $1;
34133b3a8eb9SGleb Smirnoff 			$$.port = NULL;
34143b3a8eb9SGleb Smirnoff 		}
34153b3a8eb9SGleb Smirnoff 		| ipspec PORT portspec		{
34163b3a8eb9SGleb Smirnoff 			$$.host = $1;
34173b3a8eb9SGleb Smirnoff 			$$.port = $3;
34183b3a8eb9SGleb Smirnoff 		}
34193b3a8eb9SGleb Smirnoff 		| PORT portspec			{
34203b3a8eb9SGleb Smirnoff 			$$.host = NULL;
34213b3a8eb9SGleb Smirnoff 			$$.port = $2;
34223b3a8eb9SGleb Smirnoff 		}
34233b3a8eb9SGleb Smirnoff 		;
34243b3a8eb9SGleb Smirnoff 
34253b3a8eb9SGleb Smirnoff optnl		: '\n' optnl
34263b3a8eb9SGleb Smirnoff 		|
34273b3a8eb9SGleb Smirnoff 		;
34283b3a8eb9SGleb Smirnoff 
34293b3a8eb9SGleb Smirnoff ipspec		: ANY				{ $$ = NULL; }
34303b3a8eb9SGleb Smirnoff 		| xhost				{ $$ = $1; }
34313b3a8eb9SGleb Smirnoff 		| '{' optnl host_list '}'	{ $$ = $3; }
34323b3a8eb9SGleb Smirnoff 		;
34333b3a8eb9SGleb Smirnoff 
34343b3a8eb9SGleb Smirnoff toipspec	: TO ipspec			{ $$ = $2; }
34353b3a8eb9SGleb Smirnoff 		| /* empty */			{ $$ = NULL; }
34363b3a8eb9SGleb Smirnoff 		;
34373b3a8eb9SGleb Smirnoff 
34383b3a8eb9SGleb Smirnoff host_list	: ipspec optnl			{ $$ = $1; }
34393b3a8eb9SGleb Smirnoff 		| host_list comma ipspec optnl	{
34403b3a8eb9SGleb Smirnoff 			if ($3 == NULL)
34413b3a8eb9SGleb Smirnoff 				$$ = $1;
34423b3a8eb9SGleb Smirnoff 			else if ($1 == NULL)
34433b3a8eb9SGleb Smirnoff 				$$ = $3;
34443b3a8eb9SGleb Smirnoff 			else {
34453b3a8eb9SGleb Smirnoff 				$1->tail->next = $3;
34463b3a8eb9SGleb Smirnoff 				$1->tail = $3->tail;
34473b3a8eb9SGleb Smirnoff 				$$ = $1;
34483b3a8eb9SGleb Smirnoff 			}
34493b3a8eb9SGleb Smirnoff 		}
34503b3a8eb9SGleb Smirnoff 		;
34513b3a8eb9SGleb Smirnoff 
34523b3a8eb9SGleb Smirnoff xhost		: not host			{
34533b3a8eb9SGleb Smirnoff 			struct node_host	*n;
34543b3a8eb9SGleb Smirnoff 
34553b3a8eb9SGleb Smirnoff 			for (n = $2; n != NULL; n = n->next)
34563b3a8eb9SGleb Smirnoff 				n->not = $1;
34573b3a8eb9SGleb Smirnoff 			$$ = $2;
34583b3a8eb9SGleb Smirnoff 		}
34593b3a8eb9SGleb Smirnoff 		| not NOROUTE			{
34603b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_host));
34613b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
34623b3a8eb9SGleb Smirnoff 				err(1, "xhost: calloc");
34633b3a8eb9SGleb Smirnoff 			$$->addr.type = PF_ADDR_NOROUTE;
34643b3a8eb9SGleb Smirnoff 			$$->next = NULL;
34653b3a8eb9SGleb Smirnoff 			$$->not = $1;
34663b3a8eb9SGleb Smirnoff 			$$->tail = $$;
34673b3a8eb9SGleb Smirnoff 		}
34683b3a8eb9SGleb Smirnoff 		| not URPFFAILED		{
34693b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_host));
34703b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
34713b3a8eb9SGleb Smirnoff 				err(1, "xhost: calloc");
34723b3a8eb9SGleb Smirnoff 			$$->addr.type = PF_ADDR_URPFFAILED;
34733b3a8eb9SGleb Smirnoff 			$$->next = NULL;
34743b3a8eb9SGleb Smirnoff 			$$->not = $1;
34753b3a8eb9SGleb Smirnoff 			$$->tail = $$;
34763b3a8eb9SGleb Smirnoff 		}
34773b3a8eb9SGleb Smirnoff 		;
34783b3a8eb9SGleb Smirnoff 
34793b3a8eb9SGleb Smirnoff host		: STRING			{
34803b3a8eb9SGleb Smirnoff 			if (($$ = host($1)) == NULL)	{
34813b3a8eb9SGleb Smirnoff 				/* error. "any" is handled elsewhere */
34823b3a8eb9SGleb Smirnoff 				free($1);
34833b3a8eb9SGleb Smirnoff 				yyerror("could not parse host specification");
34843b3a8eb9SGleb Smirnoff 				YYERROR;
34853b3a8eb9SGleb Smirnoff 			}
34863b3a8eb9SGleb Smirnoff 			free($1);
34873b3a8eb9SGleb Smirnoff 
34883b3a8eb9SGleb Smirnoff 		}
34893b3a8eb9SGleb Smirnoff 		| STRING '-' STRING		{
34903b3a8eb9SGleb Smirnoff 			struct node_host *b, *e;
34913b3a8eb9SGleb Smirnoff 
34923b3a8eb9SGleb Smirnoff 			if ((b = host($1)) == NULL || (e = host($3)) == NULL) {
34933b3a8eb9SGleb Smirnoff 				free($1);
34943b3a8eb9SGleb Smirnoff 				free($3);
34953b3a8eb9SGleb Smirnoff 				yyerror("could not parse host specification");
34963b3a8eb9SGleb Smirnoff 				YYERROR;
34973b3a8eb9SGleb Smirnoff 			}
34983b3a8eb9SGleb Smirnoff 			if (b->af != e->af ||
34993b3a8eb9SGleb Smirnoff 			    b->addr.type != PF_ADDR_ADDRMASK ||
35003b3a8eb9SGleb Smirnoff 			    e->addr.type != PF_ADDR_ADDRMASK ||
35013b3a8eb9SGleb Smirnoff 			    unmask(&b->addr.v.a.mask, b->af) !=
35023b3a8eb9SGleb Smirnoff 			    (b->af == AF_INET ? 32 : 128) ||
35033b3a8eb9SGleb Smirnoff 			    unmask(&e->addr.v.a.mask, e->af) !=
35043b3a8eb9SGleb Smirnoff 			    (e->af == AF_INET ? 32 : 128) ||
35053b3a8eb9SGleb Smirnoff 			    b->next != NULL || b->not ||
35063b3a8eb9SGleb Smirnoff 			    e->next != NULL || e->not) {
35073b3a8eb9SGleb Smirnoff 				free(b);
35083b3a8eb9SGleb Smirnoff 				free(e);
35093b3a8eb9SGleb Smirnoff 				free($1);
35103b3a8eb9SGleb Smirnoff 				free($3);
35113b3a8eb9SGleb Smirnoff 				yyerror("invalid address range");
35123b3a8eb9SGleb Smirnoff 				YYERROR;
35133b3a8eb9SGleb Smirnoff 			}
35143b3a8eb9SGleb Smirnoff 			memcpy(&b->addr.v.a.mask, &e->addr.v.a.addr,
35153b3a8eb9SGleb Smirnoff 			    sizeof(b->addr.v.a.mask));
35163b3a8eb9SGleb Smirnoff 			b->addr.type = PF_ADDR_RANGE;
35173b3a8eb9SGleb Smirnoff 			$$ = b;
35183b3a8eb9SGleb Smirnoff 			free(e);
35193b3a8eb9SGleb Smirnoff 			free($1);
35203b3a8eb9SGleb Smirnoff 			free($3);
35213b3a8eb9SGleb Smirnoff 		}
35223b3a8eb9SGleb Smirnoff 		| STRING '/' NUMBER		{
35233b3a8eb9SGleb Smirnoff 			char	*buf;
35243b3a8eb9SGleb Smirnoff 
35253b3a8eb9SGleb Smirnoff 			if (asprintf(&buf, "%s/%lld", $1, (long long)$3) == -1)
35263b3a8eb9SGleb Smirnoff 				err(1, "host: asprintf");
35273b3a8eb9SGleb Smirnoff 			free($1);
35283b3a8eb9SGleb Smirnoff 			if (($$ = host(buf)) == NULL)	{
35293b3a8eb9SGleb Smirnoff 				/* error. "any" is handled elsewhere */
35303b3a8eb9SGleb Smirnoff 				free(buf);
35313b3a8eb9SGleb Smirnoff 				yyerror("could not parse host specification");
35323b3a8eb9SGleb Smirnoff 				YYERROR;
35333b3a8eb9SGleb Smirnoff 			}
35343b3a8eb9SGleb Smirnoff 			free(buf);
35353b3a8eb9SGleb Smirnoff 		}
35363b3a8eb9SGleb Smirnoff 		| NUMBER '/' NUMBER		{
35373b3a8eb9SGleb Smirnoff 			char	*buf;
35383b3a8eb9SGleb Smirnoff 
35393b3a8eb9SGleb Smirnoff 			/* ie. for 10/8 parsing */
35403b3a8eb9SGleb Smirnoff #ifdef __FreeBSD__
35413b3a8eb9SGleb Smirnoff 			if (asprintf(&buf, "%lld/%lld", (long long)$1, (long long)$3) == -1)
35423b3a8eb9SGleb Smirnoff #else
35433b3a8eb9SGleb Smirnoff 			if (asprintf(&buf, "%lld/%lld", $1, $3) == -1)
35443b3a8eb9SGleb Smirnoff #endif
35453b3a8eb9SGleb Smirnoff 				err(1, "host: asprintf");
35463b3a8eb9SGleb Smirnoff 			if (($$ = host(buf)) == NULL)	{
35473b3a8eb9SGleb Smirnoff 				/* error. "any" is handled elsewhere */
35483b3a8eb9SGleb Smirnoff 				free(buf);
35493b3a8eb9SGleb Smirnoff 				yyerror("could not parse host specification");
35503b3a8eb9SGleb Smirnoff 				YYERROR;
35513b3a8eb9SGleb Smirnoff 			}
35523b3a8eb9SGleb Smirnoff 			free(buf);
35533b3a8eb9SGleb Smirnoff 		}
35543b3a8eb9SGleb Smirnoff 		| dynaddr
35553b3a8eb9SGleb Smirnoff 		| dynaddr '/' NUMBER		{
35563b3a8eb9SGleb Smirnoff 			struct node_host	*n;
35573b3a8eb9SGleb Smirnoff 
35583b3a8eb9SGleb Smirnoff 			if ($3 < 0 || $3 > 128) {
35593b3a8eb9SGleb Smirnoff 				yyerror("bit number too big");
35603b3a8eb9SGleb Smirnoff 				YYERROR;
35613b3a8eb9SGleb Smirnoff 			}
35623b3a8eb9SGleb Smirnoff 			$$ = $1;
35633b3a8eb9SGleb Smirnoff 			for (n = $1; n != NULL; n = n->next)
35643b3a8eb9SGleb Smirnoff 				set_ipmask(n, $3);
35653b3a8eb9SGleb Smirnoff 		}
35663b3a8eb9SGleb Smirnoff 		| '<' STRING '>'	{
35673b3a8eb9SGleb Smirnoff 			if (strlen($2) >= PF_TABLE_NAME_SIZE) {
35683b3a8eb9SGleb Smirnoff 				yyerror("table name '%s' too long", $2);
35693b3a8eb9SGleb Smirnoff 				free($2);
35703b3a8eb9SGleb Smirnoff 				YYERROR;
35713b3a8eb9SGleb Smirnoff 			}
35723b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_host));
35733b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
35743b3a8eb9SGleb Smirnoff 				err(1, "host: calloc");
35753b3a8eb9SGleb Smirnoff 			$$->addr.type = PF_ADDR_TABLE;
35763b3a8eb9SGleb Smirnoff 			if (strlcpy($$->addr.v.tblname, $2,
35773b3a8eb9SGleb Smirnoff 			    sizeof($$->addr.v.tblname)) >=
35783b3a8eb9SGleb Smirnoff 			    sizeof($$->addr.v.tblname))
35793b3a8eb9SGleb Smirnoff 				errx(1, "host: strlcpy");
35803b3a8eb9SGleb Smirnoff 			free($2);
35813b3a8eb9SGleb Smirnoff 			$$->next = NULL;
35823b3a8eb9SGleb Smirnoff 			$$->tail = $$;
35833b3a8eb9SGleb Smirnoff 		}
35843b3a8eb9SGleb Smirnoff 		;
35853b3a8eb9SGleb Smirnoff 
35863b3a8eb9SGleb Smirnoff number		: NUMBER
35873b3a8eb9SGleb Smirnoff 		| STRING		{
35883b3a8eb9SGleb Smirnoff 			u_long	ulval;
35893b3a8eb9SGleb Smirnoff 
35903b3a8eb9SGleb Smirnoff 			if (atoul($1, &ulval) == -1) {
35913b3a8eb9SGleb Smirnoff 				yyerror("%s is not a number", $1);
35923b3a8eb9SGleb Smirnoff 				free($1);
35933b3a8eb9SGleb Smirnoff 				YYERROR;
35943b3a8eb9SGleb Smirnoff 			} else
35953b3a8eb9SGleb Smirnoff 				$$ = ulval;
35963b3a8eb9SGleb Smirnoff 			free($1);
35973b3a8eb9SGleb Smirnoff 		}
35983b3a8eb9SGleb Smirnoff 		;
35993b3a8eb9SGleb Smirnoff 
36003b3a8eb9SGleb Smirnoff dynaddr		: '(' STRING ')'		{
36013b3a8eb9SGleb Smirnoff 			int	 flags = 0;
36023b3a8eb9SGleb Smirnoff 			char	*p, *op;
36033b3a8eb9SGleb Smirnoff 
36043b3a8eb9SGleb Smirnoff 			op = $2;
36053b3a8eb9SGleb Smirnoff 			if (!isalpha(op[0])) {
36063b3a8eb9SGleb Smirnoff 				yyerror("invalid interface name '%s'", op);
36073b3a8eb9SGleb Smirnoff 				free(op);
36083b3a8eb9SGleb Smirnoff 				YYERROR;
36093b3a8eb9SGleb Smirnoff 			}
36103b3a8eb9SGleb Smirnoff 			while ((p = strrchr($2, ':')) != NULL) {
36113b3a8eb9SGleb Smirnoff 				if (!strcmp(p+1, "network"))
36123b3a8eb9SGleb Smirnoff 					flags |= PFI_AFLAG_NETWORK;
36133b3a8eb9SGleb Smirnoff 				else if (!strcmp(p+1, "broadcast"))
36143b3a8eb9SGleb Smirnoff 					flags |= PFI_AFLAG_BROADCAST;
36153b3a8eb9SGleb Smirnoff 				else if (!strcmp(p+1, "peer"))
36163b3a8eb9SGleb Smirnoff 					flags |= PFI_AFLAG_PEER;
36173b3a8eb9SGleb Smirnoff 				else if (!strcmp(p+1, "0"))
36183b3a8eb9SGleb Smirnoff 					flags |= PFI_AFLAG_NOALIAS;
36193b3a8eb9SGleb Smirnoff 				else {
36203b3a8eb9SGleb Smirnoff 					yyerror("interface %s has bad modifier",
36213b3a8eb9SGleb Smirnoff 					    $2);
36223b3a8eb9SGleb Smirnoff 					free(op);
36233b3a8eb9SGleb Smirnoff 					YYERROR;
36243b3a8eb9SGleb Smirnoff 				}
36253b3a8eb9SGleb Smirnoff 				*p = '\0';
36263b3a8eb9SGleb Smirnoff 			}
36273b3a8eb9SGleb Smirnoff 			if (flags & (flags - 1) & PFI_AFLAG_MODEMASK) {
36283b3a8eb9SGleb Smirnoff 				free(op);
36293b3a8eb9SGleb Smirnoff 				yyerror("illegal combination of "
36303b3a8eb9SGleb Smirnoff 				    "interface modifiers");
36313b3a8eb9SGleb Smirnoff 				YYERROR;
36323b3a8eb9SGleb Smirnoff 			}
36333b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_host));
36343b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
36353b3a8eb9SGleb Smirnoff 				err(1, "address: calloc");
36363b3a8eb9SGleb Smirnoff 			$$->af = 0;
36373b3a8eb9SGleb Smirnoff 			set_ipmask($$, 128);
36383b3a8eb9SGleb Smirnoff 			$$->addr.type = PF_ADDR_DYNIFTL;
36393b3a8eb9SGleb Smirnoff 			$$->addr.iflags = flags;
36403b3a8eb9SGleb Smirnoff 			if (strlcpy($$->addr.v.ifname, $2,
36413b3a8eb9SGleb Smirnoff 			    sizeof($$->addr.v.ifname)) >=
36423b3a8eb9SGleb Smirnoff 			    sizeof($$->addr.v.ifname)) {
36433b3a8eb9SGleb Smirnoff 				free(op);
36443b3a8eb9SGleb Smirnoff 				free($$);
36453b3a8eb9SGleb Smirnoff 				yyerror("interface name too long");
36463b3a8eb9SGleb Smirnoff 				YYERROR;
36473b3a8eb9SGleb Smirnoff 			}
36483b3a8eb9SGleb Smirnoff 			free(op);
36493b3a8eb9SGleb Smirnoff 			$$->next = NULL;
36503b3a8eb9SGleb Smirnoff 			$$->tail = $$;
36513b3a8eb9SGleb Smirnoff 		}
36523b3a8eb9SGleb Smirnoff 		;
36533b3a8eb9SGleb Smirnoff 
36543b3a8eb9SGleb Smirnoff portspec	: port_item			{ $$ = $1; }
36553b3a8eb9SGleb Smirnoff 		| '{' optnl port_list '}'	{ $$ = $3; }
36563b3a8eb9SGleb Smirnoff 		;
36573b3a8eb9SGleb Smirnoff 
36583b3a8eb9SGleb Smirnoff port_list	: port_item optnl		{ $$ = $1; }
36593b3a8eb9SGleb Smirnoff 		| port_list comma port_item optnl	{
36603b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
36613b3a8eb9SGleb Smirnoff 			$1->tail = $3;
36623b3a8eb9SGleb Smirnoff 			$$ = $1;
36633b3a8eb9SGleb Smirnoff 		}
36643b3a8eb9SGleb Smirnoff 		;
36653b3a8eb9SGleb Smirnoff 
36663b3a8eb9SGleb Smirnoff port_item	: portrange			{
36673b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_port));
36683b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
36693b3a8eb9SGleb Smirnoff 				err(1, "port_item: calloc");
36703b3a8eb9SGleb Smirnoff 			$$->port[0] = $1.a;
36713b3a8eb9SGleb Smirnoff 			$$->port[1] = $1.b;
36723b3a8eb9SGleb Smirnoff 			if ($1.t)
36733b3a8eb9SGleb Smirnoff 				$$->op = PF_OP_RRG;
36743b3a8eb9SGleb Smirnoff 			else
36753b3a8eb9SGleb Smirnoff 				$$->op = PF_OP_EQ;
36763b3a8eb9SGleb Smirnoff 			$$->next = NULL;
36773b3a8eb9SGleb Smirnoff 			$$->tail = $$;
36783b3a8eb9SGleb Smirnoff 		}
36793b3a8eb9SGleb Smirnoff 		| unaryop portrange	{
36803b3a8eb9SGleb Smirnoff 			if ($2.t) {
36813b3a8eb9SGleb Smirnoff 				yyerror("':' cannot be used with an other "
36823b3a8eb9SGleb Smirnoff 				    "port operator");
36833b3a8eb9SGleb Smirnoff 				YYERROR;
36843b3a8eb9SGleb Smirnoff 			}
36853b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_port));
36863b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
36873b3a8eb9SGleb Smirnoff 				err(1, "port_item: calloc");
36883b3a8eb9SGleb Smirnoff 			$$->port[0] = $2.a;
36893b3a8eb9SGleb Smirnoff 			$$->port[1] = $2.b;
36903b3a8eb9SGleb Smirnoff 			$$->op = $1;
36913b3a8eb9SGleb Smirnoff 			$$->next = NULL;
36923b3a8eb9SGleb Smirnoff 			$$->tail = $$;
36933b3a8eb9SGleb Smirnoff 		}
36943b3a8eb9SGleb Smirnoff 		| portrange PORTBINARY portrange	{
36953b3a8eb9SGleb Smirnoff 			if ($1.t || $3.t) {
36963b3a8eb9SGleb Smirnoff 				yyerror("':' cannot be used with an other "
36973b3a8eb9SGleb Smirnoff 				    "port operator");
36983b3a8eb9SGleb Smirnoff 				YYERROR;
36993b3a8eb9SGleb Smirnoff 			}
37003b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_port));
37013b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
37023b3a8eb9SGleb Smirnoff 				err(1, "port_item: calloc");
37033b3a8eb9SGleb Smirnoff 			$$->port[0] = $1.a;
37043b3a8eb9SGleb Smirnoff 			$$->port[1] = $3.a;
37053b3a8eb9SGleb Smirnoff 			$$->op = $2;
37063b3a8eb9SGleb Smirnoff 			$$->next = NULL;
37073b3a8eb9SGleb Smirnoff 			$$->tail = $$;
37083b3a8eb9SGleb Smirnoff 		}
37093b3a8eb9SGleb Smirnoff 		;
37103b3a8eb9SGleb Smirnoff 
37113b3a8eb9SGleb Smirnoff portplain	: numberstring			{
37123b3a8eb9SGleb Smirnoff 			if (parseport($1, &$$, 0) == -1) {
37133b3a8eb9SGleb Smirnoff 				free($1);
37143b3a8eb9SGleb Smirnoff 				YYERROR;
37153b3a8eb9SGleb Smirnoff 			}
37163b3a8eb9SGleb Smirnoff 			free($1);
37173b3a8eb9SGleb Smirnoff 		}
37183b3a8eb9SGleb Smirnoff 		;
37193b3a8eb9SGleb Smirnoff 
37203b3a8eb9SGleb Smirnoff portrange	: numberstring			{
37213b3a8eb9SGleb Smirnoff 			if (parseport($1, &$$, PPORT_RANGE) == -1) {
37223b3a8eb9SGleb Smirnoff 				free($1);
37233b3a8eb9SGleb Smirnoff 				YYERROR;
37243b3a8eb9SGleb Smirnoff 			}
37253b3a8eb9SGleb Smirnoff 			free($1);
37263b3a8eb9SGleb Smirnoff 		}
37273b3a8eb9SGleb Smirnoff 		;
37283b3a8eb9SGleb Smirnoff 
37293b3a8eb9SGleb Smirnoff uids		: uid_item			{ $$ = $1; }
37303b3a8eb9SGleb Smirnoff 		| '{' optnl uid_list '}'	{ $$ = $3; }
37313b3a8eb9SGleb Smirnoff 		;
37323b3a8eb9SGleb Smirnoff 
37333b3a8eb9SGleb Smirnoff uid_list	: uid_item optnl		{ $$ = $1; }
37343b3a8eb9SGleb Smirnoff 		| uid_list comma uid_item optnl	{
37353b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
37363b3a8eb9SGleb Smirnoff 			$1->tail = $3;
37373b3a8eb9SGleb Smirnoff 			$$ = $1;
37383b3a8eb9SGleb Smirnoff 		}
37393b3a8eb9SGleb Smirnoff 		;
37403b3a8eb9SGleb Smirnoff 
37413b3a8eb9SGleb Smirnoff uid_item	: uid				{
37423b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_uid));
37433b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
37443b3a8eb9SGleb Smirnoff 				err(1, "uid_item: calloc");
37453b3a8eb9SGleb Smirnoff 			$$->uid[0] = $1;
37463b3a8eb9SGleb Smirnoff 			$$->uid[1] = $1;
37473b3a8eb9SGleb Smirnoff 			$$->op = PF_OP_EQ;
37483b3a8eb9SGleb Smirnoff 			$$->next = NULL;
37493b3a8eb9SGleb Smirnoff 			$$->tail = $$;
37503b3a8eb9SGleb Smirnoff 		}
37513b3a8eb9SGleb Smirnoff 		| unaryop uid			{
37523b3a8eb9SGleb Smirnoff 			if ($2 == UID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
37533b3a8eb9SGleb Smirnoff 				yyerror("user unknown requires operator = or "
37543b3a8eb9SGleb Smirnoff 				    "!=");
37553b3a8eb9SGleb Smirnoff 				YYERROR;
37563b3a8eb9SGleb Smirnoff 			}
37573b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_uid));
37583b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
37593b3a8eb9SGleb Smirnoff 				err(1, "uid_item: calloc");
37603b3a8eb9SGleb Smirnoff 			$$->uid[0] = $2;
37613b3a8eb9SGleb Smirnoff 			$$->uid[1] = $2;
37623b3a8eb9SGleb Smirnoff 			$$->op = $1;
37633b3a8eb9SGleb Smirnoff 			$$->next = NULL;
37643b3a8eb9SGleb Smirnoff 			$$->tail = $$;
37653b3a8eb9SGleb Smirnoff 		}
37663b3a8eb9SGleb Smirnoff 		| uid PORTBINARY uid		{
37673b3a8eb9SGleb Smirnoff 			if ($1 == UID_MAX || $3 == UID_MAX) {
37683b3a8eb9SGleb Smirnoff 				yyerror("user unknown requires operator = or "
37693b3a8eb9SGleb Smirnoff 				    "!=");
37703b3a8eb9SGleb Smirnoff 				YYERROR;
37713b3a8eb9SGleb Smirnoff 			}
37723b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_uid));
37733b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
37743b3a8eb9SGleb Smirnoff 				err(1, "uid_item: calloc");
37753b3a8eb9SGleb Smirnoff 			$$->uid[0] = $1;
37763b3a8eb9SGleb Smirnoff 			$$->uid[1] = $3;
37773b3a8eb9SGleb Smirnoff 			$$->op = $2;
37783b3a8eb9SGleb Smirnoff 			$$->next = NULL;
37793b3a8eb9SGleb Smirnoff 			$$->tail = $$;
37803b3a8eb9SGleb Smirnoff 		}
37813b3a8eb9SGleb Smirnoff 		;
37823b3a8eb9SGleb Smirnoff 
37833b3a8eb9SGleb Smirnoff uid		: STRING			{
37843b3a8eb9SGleb Smirnoff 			if (!strcmp($1, "unknown"))
37853b3a8eb9SGleb Smirnoff 				$$ = UID_MAX;
37863b3a8eb9SGleb Smirnoff 			else {
37873b3a8eb9SGleb Smirnoff 				struct passwd	*pw;
37883b3a8eb9SGleb Smirnoff 
37893b3a8eb9SGleb Smirnoff 				if ((pw = getpwnam($1)) == NULL) {
37903b3a8eb9SGleb Smirnoff 					yyerror("unknown user %s", $1);
37913b3a8eb9SGleb Smirnoff 					free($1);
37923b3a8eb9SGleb Smirnoff 					YYERROR;
37933b3a8eb9SGleb Smirnoff 				}
37943b3a8eb9SGleb Smirnoff 				$$ = pw->pw_uid;
37953b3a8eb9SGleb Smirnoff 			}
37963b3a8eb9SGleb Smirnoff 			free($1);
37973b3a8eb9SGleb Smirnoff 		}
37983b3a8eb9SGleb Smirnoff 		| NUMBER			{
37993b3a8eb9SGleb Smirnoff 			if ($1 < 0 || $1 >= UID_MAX) {
38003b3a8eb9SGleb Smirnoff 				yyerror("illegal uid value %lu", $1);
38013b3a8eb9SGleb Smirnoff 				YYERROR;
38023b3a8eb9SGleb Smirnoff 			}
38033b3a8eb9SGleb Smirnoff 			$$ = $1;
38043b3a8eb9SGleb Smirnoff 		}
38053b3a8eb9SGleb Smirnoff 		;
38063b3a8eb9SGleb Smirnoff 
38073b3a8eb9SGleb Smirnoff gids		: gid_item			{ $$ = $1; }
38083b3a8eb9SGleb Smirnoff 		| '{' optnl gid_list '}'	{ $$ = $3; }
38093b3a8eb9SGleb Smirnoff 		;
38103b3a8eb9SGleb Smirnoff 
38113b3a8eb9SGleb Smirnoff gid_list	: gid_item optnl		{ $$ = $1; }
38123b3a8eb9SGleb Smirnoff 		| gid_list comma gid_item optnl	{
38133b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
38143b3a8eb9SGleb Smirnoff 			$1->tail = $3;
38153b3a8eb9SGleb Smirnoff 			$$ = $1;
38163b3a8eb9SGleb Smirnoff 		}
38173b3a8eb9SGleb Smirnoff 		;
38183b3a8eb9SGleb Smirnoff 
38193b3a8eb9SGleb Smirnoff gid_item	: gid				{
38203b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_gid));
38213b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
38223b3a8eb9SGleb Smirnoff 				err(1, "gid_item: calloc");
38233b3a8eb9SGleb Smirnoff 			$$->gid[0] = $1;
38243b3a8eb9SGleb Smirnoff 			$$->gid[1] = $1;
38253b3a8eb9SGleb Smirnoff 			$$->op = PF_OP_EQ;
38263b3a8eb9SGleb Smirnoff 			$$->next = NULL;
38273b3a8eb9SGleb Smirnoff 			$$->tail = $$;
38283b3a8eb9SGleb Smirnoff 		}
38293b3a8eb9SGleb Smirnoff 		| unaryop gid			{
38303b3a8eb9SGleb Smirnoff 			if ($2 == GID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
38313b3a8eb9SGleb Smirnoff 				yyerror("group unknown requires operator = or "
38323b3a8eb9SGleb Smirnoff 				    "!=");
38333b3a8eb9SGleb Smirnoff 				YYERROR;
38343b3a8eb9SGleb Smirnoff 			}
38353b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_gid));
38363b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
38373b3a8eb9SGleb Smirnoff 				err(1, "gid_item: calloc");
38383b3a8eb9SGleb Smirnoff 			$$->gid[0] = $2;
38393b3a8eb9SGleb Smirnoff 			$$->gid[1] = $2;
38403b3a8eb9SGleb Smirnoff 			$$->op = $1;
38413b3a8eb9SGleb Smirnoff 			$$->next = NULL;
38423b3a8eb9SGleb Smirnoff 			$$->tail = $$;
38433b3a8eb9SGleb Smirnoff 		}
38443b3a8eb9SGleb Smirnoff 		| gid PORTBINARY gid		{
38453b3a8eb9SGleb Smirnoff 			if ($1 == GID_MAX || $3 == GID_MAX) {
38463b3a8eb9SGleb Smirnoff 				yyerror("group unknown requires operator = or "
38473b3a8eb9SGleb Smirnoff 				    "!=");
38483b3a8eb9SGleb Smirnoff 				YYERROR;
38493b3a8eb9SGleb Smirnoff 			}
38503b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_gid));
38513b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
38523b3a8eb9SGleb Smirnoff 				err(1, "gid_item: calloc");
38533b3a8eb9SGleb Smirnoff 			$$->gid[0] = $1;
38543b3a8eb9SGleb Smirnoff 			$$->gid[1] = $3;
38553b3a8eb9SGleb Smirnoff 			$$->op = $2;
38563b3a8eb9SGleb Smirnoff 			$$->next = NULL;
38573b3a8eb9SGleb Smirnoff 			$$->tail = $$;
38583b3a8eb9SGleb Smirnoff 		}
38593b3a8eb9SGleb Smirnoff 		;
38603b3a8eb9SGleb Smirnoff 
38613b3a8eb9SGleb Smirnoff gid		: STRING			{
38623b3a8eb9SGleb Smirnoff 			if (!strcmp($1, "unknown"))
38633b3a8eb9SGleb Smirnoff 				$$ = GID_MAX;
38643b3a8eb9SGleb Smirnoff 			else {
38653b3a8eb9SGleb Smirnoff 				struct group	*grp;
38663b3a8eb9SGleb Smirnoff 
38673b3a8eb9SGleb Smirnoff 				if ((grp = getgrnam($1)) == NULL) {
38683b3a8eb9SGleb Smirnoff 					yyerror("unknown group %s", $1);
38693b3a8eb9SGleb Smirnoff 					free($1);
38703b3a8eb9SGleb Smirnoff 					YYERROR;
38713b3a8eb9SGleb Smirnoff 				}
38723b3a8eb9SGleb Smirnoff 				$$ = grp->gr_gid;
38733b3a8eb9SGleb Smirnoff 			}
38743b3a8eb9SGleb Smirnoff 			free($1);
38753b3a8eb9SGleb Smirnoff 		}
38763b3a8eb9SGleb Smirnoff 		| NUMBER			{
38773b3a8eb9SGleb Smirnoff 			if ($1 < 0 || $1 >= GID_MAX) {
38783b3a8eb9SGleb Smirnoff 				yyerror("illegal gid value %lu", $1);
38793b3a8eb9SGleb Smirnoff 				YYERROR;
38803b3a8eb9SGleb Smirnoff 			}
38813b3a8eb9SGleb Smirnoff 			$$ = $1;
38823b3a8eb9SGleb Smirnoff 		}
38833b3a8eb9SGleb Smirnoff 		;
38843b3a8eb9SGleb Smirnoff 
38853b3a8eb9SGleb Smirnoff flag		: STRING			{
38863b3a8eb9SGleb Smirnoff 			int	f;
38873b3a8eb9SGleb Smirnoff 
38883b3a8eb9SGleb Smirnoff 			if ((f = parse_flags($1)) < 0) {
38893b3a8eb9SGleb Smirnoff 				yyerror("bad flags %s", $1);
38903b3a8eb9SGleb Smirnoff 				free($1);
38913b3a8eb9SGleb Smirnoff 				YYERROR;
38923b3a8eb9SGleb Smirnoff 			}
38933b3a8eb9SGleb Smirnoff 			free($1);
38943b3a8eb9SGleb Smirnoff 			$$.b1 = f;
38953b3a8eb9SGleb Smirnoff 		}
38963b3a8eb9SGleb Smirnoff 		;
38973b3a8eb9SGleb Smirnoff 
38983b3a8eb9SGleb Smirnoff flags		: FLAGS flag '/' flag	{ $$.b1 = $2.b1; $$.b2 = $4.b1; }
38993b3a8eb9SGleb Smirnoff 		| FLAGS '/' flag	{ $$.b1 = 0; $$.b2 = $3.b1; }
39003b3a8eb9SGleb Smirnoff 		| FLAGS ANY		{ $$.b1 = 0; $$.b2 = 0; }
39013b3a8eb9SGleb Smirnoff 		;
39023b3a8eb9SGleb Smirnoff 
39033b3a8eb9SGleb Smirnoff icmpspec	: ICMPTYPE icmp_item			{ $$ = $2; }
39043b3a8eb9SGleb Smirnoff 		| ICMPTYPE '{' optnl icmp_list '}'	{ $$ = $4; }
39053b3a8eb9SGleb Smirnoff 		| ICMP6TYPE icmp6_item			{ $$ = $2; }
39063b3a8eb9SGleb Smirnoff 		| ICMP6TYPE '{' optnl icmp6_list '}'	{ $$ = $4; }
39073b3a8eb9SGleb Smirnoff 		;
39083b3a8eb9SGleb Smirnoff 
39093b3a8eb9SGleb Smirnoff icmp_list	: icmp_item optnl		{ $$ = $1; }
39103b3a8eb9SGleb Smirnoff 		| icmp_list comma icmp_item optnl {
39113b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
39123b3a8eb9SGleb Smirnoff 			$1->tail = $3;
39133b3a8eb9SGleb Smirnoff 			$$ = $1;
39143b3a8eb9SGleb Smirnoff 		}
39153b3a8eb9SGleb Smirnoff 		;
39163b3a8eb9SGleb Smirnoff 
39173b3a8eb9SGleb Smirnoff icmp6_list	: icmp6_item optnl		{ $$ = $1; }
39183b3a8eb9SGleb Smirnoff 		| icmp6_list comma icmp6_item optnl {
39193b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
39203b3a8eb9SGleb Smirnoff 			$1->tail = $3;
39213b3a8eb9SGleb Smirnoff 			$$ = $1;
39223b3a8eb9SGleb Smirnoff 		}
39233b3a8eb9SGleb Smirnoff 		;
39243b3a8eb9SGleb Smirnoff 
39253b3a8eb9SGleb Smirnoff icmp_item	: icmptype		{
39263b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_icmp));
39273b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
39283b3a8eb9SGleb Smirnoff 				err(1, "icmp_item: calloc");
39293b3a8eb9SGleb Smirnoff 			$$->type = $1;
39303b3a8eb9SGleb Smirnoff 			$$->code = 0;
39313b3a8eb9SGleb Smirnoff 			$$->proto = IPPROTO_ICMP;
39323b3a8eb9SGleb Smirnoff 			$$->next = NULL;
39333b3a8eb9SGleb Smirnoff 			$$->tail = $$;
39343b3a8eb9SGleb Smirnoff 		}
39353b3a8eb9SGleb Smirnoff 		| icmptype CODE STRING	{
39363b3a8eb9SGleb Smirnoff 			const struct icmpcodeent	*p;
39373b3a8eb9SGleb Smirnoff 
39383b3a8eb9SGleb Smirnoff 			if ((p = geticmpcodebyname($1-1, $3, AF_INET)) == NULL) {
39393b3a8eb9SGleb Smirnoff 				yyerror("unknown icmp-code %s", $3);
39403b3a8eb9SGleb Smirnoff 				free($3);
39413b3a8eb9SGleb Smirnoff 				YYERROR;
39423b3a8eb9SGleb Smirnoff 			}
39433b3a8eb9SGleb Smirnoff 
39443b3a8eb9SGleb Smirnoff 			free($3);
39453b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_icmp));
39463b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
39473b3a8eb9SGleb Smirnoff 				err(1, "icmp_item: calloc");
39483b3a8eb9SGleb Smirnoff 			$$->type = $1;
39493b3a8eb9SGleb Smirnoff 			$$->code = p->code + 1;
39503b3a8eb9SGleb Smirnoff 			$$->proto = IPPROTO_ICMP;
39513b3a8eb9SGleb Smirnoff 			$$->next = NULL;
39523b3a8eb9SGleb Smirnoff 			$$->tail = $$;
39533b3a8eb9SGleb Smirnoff 		}
39543b3a8eb9SGleb Smirnoff 		| icmptype CODE NUMBER	{
39553b3a8eb9SGleb Smirnoff 			if ($3 < 0 || $3 > 255) {
39563b3a8eb9SGleb Smirnoff 				yyerror("illegal icmp-code %lu", $3);
39573b3a8eb9SGleb Smirnoff 				YYERROR;
39583b3a8eb9SGleb Smirnoff 			}
39593b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_icmp));
39603b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
39613b3a8eb9SGleb Smirnoff 				err(1, "icmp_item: calloc");
39623b3a8eb9SGleb Smirnoff 			$$->type = $1;
39633b3a8eb9SGleb Smirnoff 			$$->code = $3 + 1;
39643b3a8eb9SGleb Smirnoff 			$$->proto = IPPROTO_ICMP;
39653b3a8eb9SGleb Smirnoff 			$$->next = NULL;
39663b3a8eb9SGleb Smirnoff 			$$->tail = $$;
39673b3a8eb9SGleb Smirnoff 		}
39683b3a8eb9SGleb Smirnoff 		;
39693b3a8eb9SGleb Smirnoff 
39703b3a8eb9SGleb Smirnoff icmp6_item	: icmp6type		{
39713b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_icmp));
39723b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
39733b3a8eb9SGleb Smirnoff 				err(1, "icmp_item: calloc");
39743b3a8eb9SGleb Smirnoff 			$$->type = $1;
39753b3a8eb9SGleb Smirnoff 			$$->code = 0;
39763b3a8eb9SGleb Smirnoff 			$$->proto = IPPROTO_ICMPV6;
39773b3a8eb9SGleb Smirnoff 			$$->next = NULL;
39783b3a8eb9SGleb Smirnoff 			$$->tail = $$;
39793b3a8eb9SGleb Smirnoff 		}
39803b3a8eb9SGleb Smirnoff 		| icmp6type CODE STRING	{
39813b3a8eb9SGleb Smirnoff 			const struct icmpcodeent	*p;
39823b3a8eb9SGleb Smirnoff 
39833b3a8eb9SGleb Smirnoff 			if ((p = geticmpcodebyname($1-1, $3, AF_INET6)) == NULL) {
39843b3a8eb9SGleb Smirnoff 				yyerror("unknown icmp6-code %s", $3);
39853b3a8eb9SGleb Smirnoff 				free($3);
39863b3a8eb9SGleb Smirnoff 				YYERROR;
39873b3a8eb9SGleb Smirnoff 			}
39883b3a8eb9SGleb Smirnoff 			free($3);
39893b3a8eb9SGleb Smirnoff 
39903b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_icmp));
39913b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
39923b3a8eb9SGleb Smirnoff 				err(1, "icmp_item: calloc");
39933b3a8eb9SGleb Smirnoff 			$$->type = $1;
39943b3a8eb9SGleb Smirnoff 			$$->code = p->code + 1;
39953b3a8eb9SGleb Smirnoff 			$$->proto = IPPROTO_ICMPV6;
39963b3a8eb9SGleb Smirnoff 			$$->next = NULL;
39973b3a8eb9SGleb Smirnoff 			$$->tail = $$;
39983b3a8eb9SGleb Smirnoff 		}
39993b3a8eb9SGleb Smirnoff 		| icmp6type CODE NUMBER	{
40003b3a8eb9SGleb Smirnoff 			if ($3 < 0 || $3 > 255) {
40013b3a8eb9SGleb Smirnoff 				yyerror("illegal icmp-code %lu", $3);
40023b3a8eb9SGleb Smirnoff 				YYERROR;
40033b3a8eb9SGleb Smirnoff 			}
40043b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_icmp));
40053b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
40063b3a8eb9SGleb Smirnoff 				err(1, "icmp_item: calloc");
40073b3a8eb9SGleb Smirnoff 			$$->type = $1;
40083b3a8eb9SGleb Smirnoff 			$$->code = $3 + 1;
40093b3a8eb9SGleb Smirnoff 			$$->proto = IPPROTO_ICMPV6;
40103b3a8eb9SGleb Smirnoff 			$$->next = NULL;
40113b3a8eb9SGleb Smirnoff 			$$->tail = $$;
40123b3a8eb9SGleb Smirnoff 		}
40133b3a8eb9SGleb Smirnoff 		;
40143b3a8eb9SGleb Smirnoff 
40153b3a8eb9SGleb Smirnoff icmptype	: STRING			{
40163b3a8eb9SGleb Smirnoff 			const struct icmptypeent	*p;
40173b3a8eb9SGleb Smirnoff 
40183b3a8eb9SGleb Smirnoff 			if ((p = geticmptypebyname($1, AF_INET)) == NULL) {
40193b3a8eb9SGleb Smirnoff 				yyerror("unknown icmp-type %s", $1);
40203b3a8eb9SGleb Smirnoff 				free($1);
40213b3a8eb9SGleb Smirnoff 				YYERROR;
40223b3a8eb9SGleb Smirnoff 			}
40233b3a8eb9SGleb Smirnoff 			$$ = p->type + 1;
40243b3a8eb9SGleb Smirnoff 			free($1);
40253b3a8eb9SGleb Smirnoff 		}
40263b3a8eb9SGleb Smirnoff 		| NUMBER			{
40273b3a8eb9SGleb Smirnoff 			if ($1 < 0 || $1 > 255) {
40283b3a8eb9SGleb Smirnoff 				yyerror("illegal icmp-type %lu", $1);
40293b3a8eb9SGleb Smirnoff 				YYERROR;
40303b3a8eb9SGleb Smirnoff 			}
40313b3a8eb9SGleb Smirnoff 			$$ = $1 + 1;
40323b3a8eb9SGleb Smirnoff 		}
40333b3a8eb9SGleb Smirnoff 		;
40343b3a8eb9SGleb Smirnoff 
40353b3a8eb9SGleb Smirnoff icmp6type	: STRING			{
40363b3a8eb9SGleb Smirnoff 			const struct icmptypeent	*p;
40373b3a8eb9SGleb Smirnoff 
40383b3a8eb9SGleb Smirnoff 			if ((p = geticmptypebyname($1, AF_INET6)) ==
40393b3a8eb9SGleb Smirnoff 			    NULL) {
40403b3a8eb9SGleb Smirnoff 				yyerror("unknown icmp6-type %s", $1);
40413b3a8eb9SGleb Smirnoff 				free($1);
40423b3a8eb9SGleb Smirnoff 				YYERROR;
40433b3a8eb9SGleb Smirnoff 			}
40443b3a8eb9SGleb Smirnoff 			$$ = p->type + 1;
40453b3a8eb9SGleb Smirnoff 			free($1);
40463b3a8eb9SGleb Smirnoff 		}
40473b3a8eb9SGleb Smirnoff 		| NUMBER			{
40483b3a8eb9SGleb Smirnoff 			if ($1 < 0 || $1 > 255) {
40493b3a8eb9SGleb Smirnoff 				yyerror("illegal icmp6-type %lu", $1);
40503b3a8eb9SGleb Smirnoff 				YYERROR;
40513b3a8eb9SGleb Smirnoff 			}
40523b3a8eb9SGleb Smirnoff 			$$ = $1 + 1;
40533b3a8eb9SGleb Smirnoff 		}
40543b3a8eb9SGleb Smirnoff 		;
40553b3a8eb9SGleb Smirnoff 
40563b3a8eb9SGleb Smirnoff tos	: STRING			{
40571f495578SKristof Provost 			int val;
40581f495578SKristof Provost 			char *end;
40591f495578SKristof Provost 
40601f495578SKristof Provost 			if (map_tos($1, &val))
40611f495578SKristof Provost 				$$ = val;
40621f495578SKristof Provost 			else if ($1[0] == '0' && $1[1] == 'x') {
40631f495578SKristof Provost 				errno = 0;
40641f495578SKristof Provost 				$$ = strtoul($1, &end, 16);
40651f495578SKristof Provost 				if (errno || *end != '\0')
40661f495578SKristof Provost 					$$ = 256;
40671f495578SKristof Provost 			} else
40680cd7a91aSKristof Provost 				$$ = 256;		/* flag bad argument */
40690cd7a91aSKristof Provost 			if ($$ < 0 || $$ > 255) {
40703b3a8eb9SGleb Smirnoff 				yyerror("illegal tos value %s", $1);
40713b3a8eb9SGleb Smirnoff 				free($1);
40723b3a8eb9SGleb Smirnoff 				YYERROR;
40733b3a8eb9SGleb Smirnoff 			}
40743b3a8eb9SGleb Smirnoff 			free($1);
40753b3a8eb9SGleb Smirnoff 		}
40763b3a8eb9SGleb Smirnoff 		| NUMBER			{
40773b3a8eb9SGleb Smirnoff 			$$ = $1;
40780cd7a91aSKristof Provost 			if ($$ < 0 || $$ > 255) {
40793b3a8eb9SGleb Smirnoff 				yyerror("illegal tos value %s", $1);
40803b3a8eb9SGleb Smirnoff 				YYERROR;
40813b3a8eb9SGleb Smirnoff 			}
40823b3a8eb9SGleb Smirnoff 		}
40833b3a8eb9SGleb Smirnoff 		;
40843b3a8eb9SGleb Smirnoff 
40853b3a8eb9SGleb Smirnoff sourcetrack	: SOURCETRACK		{ $$ = PF_SRCTRACK; }
40863b3a8eb9SGleb Smirnoff 		| SOURCETRACK GLOBAL	{ $$ = PF_SRCTRACK_GLOBAL; }
40873b3a8eb9SGleb Smirnoff 		| SOURCETRACK RULE	{ $$ = PF_SRCTRACK_RULE; }
40883b3a8eb9SGleb Smirnoff 		;
40893b3a8eb9SGleb Smirnoff 
40903b3a8eb9SGleb Smirnoff statelock	: IFBOUND {
40913b3a8eb9SGleb Smirnoff 			$$ = PFRULE_IFBOUND;
40923b3a8eb9SGleb Smirnoff 		}
40933b3a8eb9SGleb Smirnoff 		| FLOATING {
40943b3a8eb9SGleb Smirnoff 			$$ = 0;
40953b3a8eb9SGleb Smirnoff 		}
40963b3a8eb9SGleb Smirnoff 		;
40973b3a8eb9SGleb Smirnoff 
40983b3a8eb9SGleb Smirnoff keep		: NO STATE			{
40993b3a8eb9SGleb Smirnoff 			$$.action = 0;
41003b3a8eb9SGleb Smirnoff 			$$.options = NULL;
41013b3a8eb9SGleb Smirnoff 		}
41023b3a8eb9SGleb Smirnoff 		| KEEP STATE state_opt_spec	{
41033b3a8eb9SGleb Smirnoff 			$$.action = PF_STATE_NORMAL;
41043b3a8eb9SGleb Smirnoff 			$$.options = $3;
41053b3a8eb9SGleb Smirnoff 		}
41063b3a8eb9SGleb Smirnoff 		| MODULATE STATE state_opt_spec {
41073b3a8eb9SGleb Smirnoff 			$$.action = PF_STATE_MODULATE;
41083b3a8eb9SGleb Smirnoff 			$$.options = $3;
41093b3a8eb9SGleb Smirnoff 		}
41103b3a8eb9SGleb Smirnoff 		| SYNPROXY STATE state_opt_spec {
41113b3a8eb9SGleb Smirnoff 			$$.action = PF_STATE_SYNPROXY;
41123b3a8eb9SGleb Smirnoff 			$$.options = $3;
41133b3a8eb9SGleb Smirnoff 		}
41143b3a8eb9SGleb Smirnoff 		;
41153b3a8eb9SGleb Smirnoff 
41163b3a8eb9SGleb Smirnoff flush		: /* empty */			{ $$ = 0; }
41173b3a8eb9SGleb Smirnoff 		| FLUSH				{ $$ = PF_FLUSH; }
41183b3a8eb9SGleb Smirnoff 		| FLUSH GLOBAL			{
41193b3a8eb9SGleb Smirnoff 			$$ = PF_FLUSH | PF_FLUSH_GLOBAL;
41203b3a8eb9SGleb Smirnoff 		}
41213b3a8eb9SGleb Smirnoff 		;
41223b3a8eb9SGleb Smirnoff 
41233b3a8eb9SGleb Smirnoff state_opt_spec	: '(' state_opt_list ')'	{ $$ = $2; }
41243b3a8eb9SGleb Smirnoff 		| /* empty */			{ $$ = NULL; }
41253b3a8eb9SGleb Smirnoff 		;
41263b3a8eb9SGleb Smirnoff 
41273b3a8eb9SGleb Smirnoff state_opt_list	: state_opt_item		{ $$ = $1; }
41283b3a8eb9SGleb Smirnoff 		| state_opt_list comma state_opt_item {
41293b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
41303b3a8eb9SGleb Smirnoff 			$1->tail = $3;
41313b3a8eb9SGleb Smirnoff 			$$ = $1;
41323b3a8eb9SGleb Smirnoff 		}
41333b3a8eb9SGleb Smirnoff 		;
41343b3a8eb9SGleb Smirnoff 
41353b3a8eb9SGleb Smirnoff state_opt_item	: MAXIMUM NUMBER		{
41363b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > UINT_MAX) {
41373b3a8eb9SGleb Smirnoff 				yyerror("only positive values permitted");
41383b3a8eb9SGleb Smirnoff 				YYERROR;
41393b3a8eb9SGleb Smirnoff 			}
41403b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
41413b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
41423b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
41433b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_MAX;
41443b3a8eb9SGleb Smirnoff 			$$->data.max_states = $2;
41453b3a8eb9SGleb Smirnoff 			$$->next = NULL;
41463b3a8eb9SGleb Smirnoff 			$$->tail = $$;
41473b3a8eb9SGleb Smirnoff 		}
41483b3a8eb9SGleb Smirnoff 		| NOSYNC				{
41493b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
41503b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
41513b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
41523b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_NOSYNC;
41533b3a8eb9SGleb Smirnoff 			$$->next = NULL;
41543b3a8eb9SGleb Smirnoff 			$$->tail = $$;
41553b3a8eb9SGleb Smirnoff 		}
41563b3a8eb9SGleb Smirnoff 		| MAXSRCSTATES NUMBER			{
41573b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > UINT_MAX) {
41583b3a8eb9SGleb Smirnoff 				yyerror("only positive values permitted");
41593b3a8eb9SGleb Smirnoff 				YYERROR;
41603b3a8eb9SGleb Smirnoff 			}
41613b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
41623b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
41633b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
41643b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_MAX_SRC_STATES;
41653b3a8eb9SGleb Smirnoff 			$$->data.max_src_states = $2;
41663b3a8eb9SGleb Smirnoff 			$$->next = NULL;
41673b3a8eb9SGleb Smirnoff 			$$->tail = $$;
41683b3a8eb9SGleb Smirnoff 		}
41693b3a8eb9SGleb Smirnoff 		| MAXSRCCONN NUMBER			{
41703b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > UINT_MAX) {
41713b3a8eb9SGleb Smirnoff 				yyerror("only positive values permitted");
41723b3a8eb9SGleb Smirnoff 				YYERROR;
41733b3a8eb9SGleb Smirnoff 			}
41743b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
41753b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
41763b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
41773b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_MAX_SRC_CONN;
41783b3a8eb9SGleb Smirnoff 			$$->data.max_src_conn = $2;
41793b3a8eb9SGleb Smirnoff 			$$->next = NULL;
41803b3a8eb9SGleb Smirnoff 			$$->tail = $$;
41813b3a8eb9SGleb Smirnoff 		}
41823b3a8eb9SGleb Smirnoff 		| MAXSRCCONNRATE NUMBER '/' NUMBER	{
41833b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > UINT_MAX ||
41843b3a8eb9SGleb Smirnoff 			    $4 < 0 || $4 > UINT_MAX) {
41853b3a8eb9SGleb Smirnoff 				yyerror("only positive values permitted");
41863b3a8eb9SGleb Smirnoff 				YYERROR;
41873b3a8eb9SGleb Smirnoff 			}
41883b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
41893b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
41903b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
41913b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_MAX_SRC_CONN_RATE;
41923b3a8eb9SGleb Smirnoff 			$$->data.max_src_conn_rate.limit = $2;
41933b3a8eb9SGleb Smirnoff 			$$->data.max_src_conn_rate.seconds = $4;
41943b3a8eb9SGleb Smirnoff 			$$->next = NULL;
41953b3a8eb9SGleb Smirnoff 			$$->tail = $$;
41963b3a8eb9SGleb Smirnoff 		}
41973b3a8eb9SGleb Smirnoff 		| OVERLOAD '<' STRING '>' flush		{
41983b3a8eb9SGleb Smirnoff 			if (strlen($3) >= PF_TABLE_NAME_SIZE) {
41993b3a8eb9SGleb Smirnoff 				yyerror("table name '%s' too long", $3);
42003b3a8eb9SGleb Smirnoff 				free($3);
42013b3a8eb9SGleb Smirnoff 				YYERROR;
42023b3a8eb9SGleb Smirnoff 			}
42033b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
42043b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
42053b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
42063b3a8eb9SGleb Smirnoff 			if (strlcpy($$->data.overload.tblname, $3,
42073b3a8eb9SGleb Smirnoff 			    PF_TABLE_NAME_SIZE) >= PF_TABLE_NAME_SIZE)
42083b3a8eb9SGleb Smirnoff 				errx(1, "state_opt_item: strlcpy");
42093b3a8eb9SGleb Smirnoff 			free($3);
42103b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_OVERLOAD;
42113b3a8eb9SGleb Smirnoff 			$$->data.overload.flush = $5;
42123b3a8eb9SGleb Smirnoff 			$$->next = NULL;
42133b3a8eb9SGleb Smirnoff 			$$->tail = $$;
42143b3a8eb9SGleb Smirnoff 		}
42153b3a8eb9SGleb Smirnoff 		| MAXSRCNODES NUMBER			{
42163b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > UINT_MAX) {
42173b3a8eb9SGleb Smirnoff 				yyerror("only positive values permitted");
42183b3a8eb9SGleb Smirnoff 				YYERROR;
42193b3a8eb9SGleb Smirnoff 			}
42203b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
42213b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
42223b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
42233b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_MAX_SRC_NODES;
42243b3a8eb9SGleb Smirnoff 			$$->data.max_src_nodes = $2;
42253b3a8eb9SGleb Smirnoff 			$$->next = NULL;
42263b3a8eb9SGleb Smirnoff 			$$->tail = $$;
42273b3a8eb9SGleb Smirnoff 		}
42283b3a8eb9SGleb Smirnoff 		| sourcetrack {
42293b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
42303b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
42313b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
42323b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_SRCTRACK;
42333b3a8eb9SGleb Smirnoff 			$$->data.src_track = $1;
42343b3a8eb9SGleb Smirnoff 			$$->next = NULL;
42353b3a8eb9SGleb Smirnoff 			$$->tail = $$;
42363b3a8eb9SGleb Smirnoff 		}
42373b3a8eb9SGleb Smirnoff 		| statelock {
42383b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
42393b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
42403b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
42413b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_STATELOCK;
42423b3a8eb9SGleb Smirnoff 			$$->data.statelock = $1;
42433b3a8eb9SGleb Smirnoff 			$$->next = NULL;
42443b3a8eb9SGleb Smirnoff 			$$->tail = $$;
42453b3a8eb9SGleb Smirnoff 		}
42463b3a8eb9SGleb Smirnoff 		| SLOPPY {
42473b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
42483b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
42493b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
42503b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_SLOPPY;
42513b3a8eb9SGleb Smirnoff 			$$->next = NULL;
42523b3a8eb9SGleb Smirnoff 			$$->tail = $$;
42533b3a8eb9SGleb Smirnoff 		}
42543b3a8eb9SGleb Smirnoff 		| STRING NUMBER			{
42553b3a8eb9SGleb Smirnoff 			int	i;
42563b3a8eb9SGleb Smirnoff 
42573b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > UINT_MAX) {
42583b3a8eb9SGleb Smirnoff 				yyerror("only positive values permitted");
42593b3a8eb9SGleb Smirnoff 				YYERROR;
42603b3a8eb9SGleb Smirnoff 			}
42613b3a8eb9SGleb Smirnoff 			for (i = 0; pf_timeouts[i].name &&
42623b3a8eb9SGleb Smirnoff 			    strcmp(pf_timeouts[i].name, $1); ++i)
42633b3a8eb9SGleb Smirnoff 				;	/* nothing */
42643b3a8eb9SGleb Smirnoff 			if (!pf_timeouts[i].name) {
42653b3a8eb9SGleb Smirnoff 				yyerror("illegal timeout name %s", $1);
42663b3a8eb9SGleb Smirnoff 				free($1);
42673b3a8eb9SGleb Smirnoff 				YYERROR;
42683b3a8eb9SGleb Smirnoff 			}
42693b3a8eb9SGleb Smirnoff 			if (strchr(pf_timeouts[i].name, '.') == NULL) {
42703b3a8eb9SGleb Smirnoff 				yyerror("illegal state timeout %s", $1);
42713b3a8eb9SGleb Smirnoff 				free($1);
42723b3a8eb9SGleb Smirnoff 				YYERROR;
42733b3a8eb9SGleb Smirnoff 			}
42743b3a8eb9SGleb Smirnoff 			free($1);
42753b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
42763b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
42773b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
42783b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_TIMEOUT;
42793b3a8eb9SGleb Smirnoff 			$$->data.timeout.number = pf_timeouts[i].timeout;
42803b3a8eb9SGleb Smirnoff 			$$->data.timeout.seconds = $2;
42813b3a8eb9SGleb Smirnoff 			$$->next = NULL;
42823b3a8eb9SGleb Smirnoff 			$$->tail = $$;
42833b3a8eb9SGleb Smirnoff 		}
42843b3a8eb9SGleb Smirnoff 		;
42853b3a8eb9SGleb Smirnoff 
42863b3a8eb9SGleb Smirnoff label		: LABEL STRING			{
42873b3a8eb9SGleb Smirnoff 			$$ = $2;
42883b3a8eb9SGleb Smirnoff 		}
42893b3a8eb9SGleb Smirnoff 		;
42903b3a8eb9SGleb Smirnoff 
42912b29ceb8SKristof Provost etherqname	: QUEUE STRING				{
42922b29ceb8SKristof Provost 			$$.qname = $2;
42932b29ceb8SKristof Provost 		}
42942b29ceb8SKristof Provost 		| QUEUE '(' STRING ')'			{
42952b29ceb8SKristof Provost 			$$.qname = $3;
42962b29ceb8SKristof Provost 		}
42972b29ceb8SKristof Provost 		;
42982b29ceb8SKristof Provost 
42993b3a8eb9SGleb Smirnoff qname		: QUEUE STRING				{
43003b3a8eb9SGleb Smirnoff 			$$.qname = $2;
43013b3a8eb9SGleb Smirnoff 			$$.pqname = NULL;
43023b3a8eb9SGleb Smirnoff 		}
43033b3a8eb9SGleb Smirnoff 		| QUEUE '(' STRING ')'			{
43043b3a8eb9SGleb Smirnoff 			$$.qname = $3;
43053b3a8eb9SGleb Smirnoff 			$$.pqname = NULL;
43063b3a8eb9SGleb Smirnoff 		}
43073b3a8eb9SGleb Smirnoff 		| QUEUE '(' STRING comma STRING ')'	{
43083b3a8eb9SGleb Smirnoff 			$$.qname = $3;
43093b3a8eb9SGleb Smirnoff 			$$.pqname = $5;
43103b3a8eb9SGleb Smirnoff 		}
43113b3a8eb9SGleb Smirnoff 		;
43123b3a8eb9SGleb Smirnoff 
43133b3a8eb9SGleb Smirnoff no		: /* empty */			{ $$ = 0; }
43143b3a8eb9SGleb Smirnoff 		| NO				{ $$ = 1; }
43153b3a8eb9SGleb Smirnoff 		;
43163b3a8eb9SGleb Smirnoff 
43173b3a8eb9SGleb Smirnoff portstar	: numberstring			{
43183b3a8eb9SGleb Smirnoff 			if (parseport($1, &$$, PPORT_RANGE|PPORT_STAR) == -1) {
43193b3a8eb9SGleb Smirnoff 				free($1);
43203b3a8eb9SGleb Smirnoff 				YYERROR;
43213b3a8eb9SGleb Smirnoff 			}
43223b3a8eb9SGleb Smirnoff 			free($1);
43233b3a8eb9SGleb Smirnoff 		}
43243b3a8eb9SGleb Smirnoff 		;
43253b3a8eb9SGleb Smirnoff 
43263b3a8eb9SGleb Smirnoff redirspec	: host				{ $$ = $1; }
43273b3a8eb9SGleb Smirnoff 		| '{' optnl redir_host_list '}'	{ $$ = $3; }
43283b3a8eb9SGleb Smirnoff 		;
43293b3a8eb9SGleb Smirnoff 
43303b3a8eb9SGleb Smirnoff redir_host_list	: host optnl			{ $$ = $1; }
43313b3a8eb9SGleb Smirnoff 		| redir_host_list comma host optnl {
43323b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
43333b3a8eb9SGleb Smirnoff 			$1->tail = $3->tail;
43343b3a8eb9SGleb Smirnoff 			$$ = $1;
43353b3a8eb9SGleb Smirnoff 		}
43363b3a8eb9SGleb Smirnoff 		;
43373b3a8eb9SGleb Smirnoff 
43383b3a8eb9SGleb Smirnoff redirpool	: /* empty */			{ $$ = NULL; }
43393b3a8eb9SGleb Smirnoff 		| ARROW redirspec		{
43403b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct redirection));
43413b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
43423b3a8eb9SGleb Smirnoff 				err(1, "redirection: calloc");
43433b3a8eb9SGleb Smirnoff 			$$->host = $2;
43443b3a8eb9SGleb Smirnoff 			$$->rport.a = $$->rport.b = $$->rport.t = 0;
43453b3a8eb9SGleb Smirnoff 		}
43463b3a8eb9SGleb Smirnoff 		| ARROW redirspec PORT portstar	{
43473b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct redirection));
43483b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
43493b3a8eb9SGleb Smirnoff 				err(1, "redirection: calloc");
43503b3a8eb9SGleb Smirnoff 			$$->host = $2;
43513b3a8eb9SGleb Smirnoff 			$$->rport = $4;
43523b3a8eb9SGleb Smirnoff 		}
43533b3a8eb9SGleb Smirnoff 		;
43543b3a8eb9SGleb Smirnoff 
43553b3a8eb9SGleb Smirnoff hashkey		: /* empty */
43563b3a8eb9SGleb Smirnoff 		{
43573b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct pf_poolhashkey));
43583b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
43593b3a8eb9SGleb Smirnoff 				err(1, "hashkey: calloc");
43603b3a8eb9SGleb Smirnoff 			$$->key32[0] = arc4random();
43613b3a8eb9SGleb Smirnoff 			$$->key32[1] = arc4random();
43623b3a8eb9SGleb Smirnoff 			$$->key32[2] = arc4random();
43633b3a8eb9SGleb Smirnoff 			$$->key32[3] = arc4random();
43643b3a8eb9SGleb Smirnoff 		}
43653b3a8eb9SGleb Smirnoff 		| string
43663b3a8eb9SGleb Smirnoff 		{
43673b3a8eb9SGleb Smirnoff 			if (!strncmp($1, "0x", 2)) {
43683b3a8eb9SGleb Smirnoff 				if (strlen($1) != 34) {
43693b3a8eb9SGleb Smirnoff 					free($1);
43703b3a8eb9SGleb Smirnoff 					yyerror("hex key must be 128 bits "
43713b3a8eb9SGleb Smirnoff 						"(32 hex digits) long");
43723b3a8eb9SGleb Smirnoff 					YYERROR;
43733b3a8eb9SGleb Smirnoff 				}
43743b3a8eb9SGleb Smirnoff 				$$ = calloc(1, sizeof(struct pf_poolhashkey));
43753b3a8eb9SGleb Smirnoff 				if ($$ == NULL)
43763b3a8eb9SGleb Smirnoff 					err(1, "hashkey: calloc");
43773b3a8eb9SGleb Smirnoff 
43783b3a8eb9SGleb Smirnoff 				if (sscanf($1, "0x%8x%8x%8x%8x",
43793b3a8eb9SGleb Smirnoff 				    &$$->key32[0], &$$->key32[1],
43803b3a8eb9SGleb Smirnoff 				    &$$->key32[2], &$$->key32[3]) != 4) {
43813b3a8eb9SGleb Smirnoff 					free($$);
43823b3a8eb9SGleb Smirnoff 					free($1);
43833b3a8eb9SGleb Smirnoff 					yyerror("invalid hex key");
43843b3a8eb9SGleb Smirnoff 					YYERROR;
43853b3a8eb9SGleb Smirnoff 				}
43863b3a8eb9SGleb Smirnoff 			} else {
43873b3a8eb9SGleb Smirnoff 				MD5_CTX	context;
43883b3a8eb9SGleb Smirnoff 
43893b3a8eb9SGleb Smirnoff 				$$ = calloc(1, sizeof(struct pf_poolhashkey));
43903b3a8eb9SGleb Smirnoff 				if ($$ == NULL)
43913b3a8eb9SGleb Smirnoff 					err(1, "hashkey: calloc");
43923b3a8eb9SGleb Smirnoff 				MD5Init(&context);
43933b3a8eb9SGleb Smirnoff 				MD5Update(&context, (unsigned char *)$1,
43943b3a8eb9SGleb Smirnoff 				    strlen($1));
43953b3a8eb9SGleb Smirnoff 				MD5Final((unsigned char *)$$, &context);
43963b3a8eb9SGleb Smirnoff 				HTONL($$->key32[0]);
43973b3a8eb9SGleb Smirnoff 				HTONL($$->key32[1]);
43983b3a8eb9SGleb Smirnoff 				HTONL($$->key32[2]);
43993b3a8eb9SGleb Smirnoff 				HTONL($$->key32[3]);
44003b3a8eb9SGleb Smirnoff 			}
44013b3a8eb9SGleb Smirnoff 			free($1);
44023b3a8eb9SGleb Smirnoff 		}
44033b3a8eb9SGleb Smirnoff 		;
44043b3a8eb9SGleb Smirnoff 
44053b3a8eb9SGleb Smirnoff pool_opts	:	{ bzero(&pool_opts, sizeof pool_opts); }
44063b3a8eb9SGleb Smirnoff 		    pool_opts_l
44073b3a8eb9SGleb Smirnoff 			{ $$ = pool_opts; }
44083b3a8eb9SGleb Smirnoff 		| /* empty */	{
44093b3a8eb9SGleb Smirnoff 			bzero(&pool_opts, sizeof pool_opts);
44103b3a8eb9SGleb Smirnoff 			$$ = pool_opts;
44113b3a8eb9SGleb Smirnoff 		}
44123b3a8eb9SGleb Smirnoff 		;
44133b3a8eb9SGleb Smirnoff 
44143b3a8eb9SGleb Smirnoff pool_opts_l	: pool_opts_l pool_opt
44153b3a8eb9SGleb Smirnoff 		| pool_opt
44163b3a8eb9SGleb Smirnoff 		;
44173b3a8eb9SGleb Smirnoff 
44183b3a8eb9SGleb Smirnoff pool_opt	: BITMASK	{
44193b3a8eb9SGleb Smirnoff 			if (pool_opts.type) {
44203b3a8eb9SGleb Smirnoff 				yyerror("pool type cannot be redefined");
44213b3a8eb9SGleb Smirnoff 				YYERROR;
44223b3a8eb9SGleb Smirnoff 			}
44233b3a8eb9SGleb Smirnoff 			pool_opts.type =  PF_POOL_BITMASK;
44243b3a8eb9SGleb Smirnoff 		}
44253b3a8eb9SGleb Smirnoff 		| RANDOM	{
44263b3a8eb9SGleb Smirnoff 			if (pool_opts.type) {
44273b3a8eb9SGleb Smirnoff 				yyerror("pool type cannot be redefined");
44283b3a8eb9SGleb Smirnoff 				YYERROR;
44293b3a8eb9SGleb Smirnoff 			}
44303b3a8eb9SGleb Smirnoff 			pool_opts.type = PF_POOL_RANDOM;
44313b3a8eb9SGleb Smirnoff 		}
44323b3a8eb9SGleb Smirnoff 		| SOURCEHASH hashkey {
44333b3a8eb9SGleb Smirnoff 			if (pool_opts.type) {
44343b3a8eb9SGleb Smirnoff 				yyerror("pool type cannot be redefined");
44353b3a8eb9SGleb Smirnoff 				YYERROR;
44363b3a8eb9SGleb Smirnoff 			}
44373b3a8eb9SGleb Smirnoff 			pool_opts.type = PF_POOL_SRCHASH;
44383b3a8eb9SGleb Smirnoff 			pool_opts.key = $2;
44393b3a8eb9SGleb Smirnoff 		}
44403b3a8eb9SGleb Smirnoff 		| ROUNDROBIN	{
44413b3a8eb9SGleb Smirnoff 			if (pool_opts.type) {
44423b3a8eb9SGleb Smirnoff 				yyerror("pool type cannot be redefined");
44433b3a8eb9SGleb Smirnoff 				YYERROR;
44443b3a8eb9SGleb Smirnoff 			}
44453b3a8eb9SGleb Smirnoff 			pool_opts.type = PF_POOL_ROUNDROBIN;
44463b3a8eb9SGleb Smirnoff 		}
44473b3a8eb9SGleb Smirnoff 		| STATICPORT	{
44483b3a8eb9SGleb Smirnoff 			if (pool_opts.staticport) {
44493b3a8eb9SGleb Smirnoff 				yyerror("static-port cannot be redefined");
44503b3a8eb9SGleb Smirnoff 				YYERROR;
44513b3a8eb9SGleb Smirnoff 			}
44523b3a8eb9SGleb Smirnoff 			pool_opts.staticport = 1;
44533b3a8eb9SGleb Smirnoff 		}
44543b3a8eb9SGleb Smirnoff 		| STICKYADDRESS	{
44553b3a8eb9SGleb Smirnoff 			if (filter_opts.marker & POM_STICKYADDRESS) {
44563b3a8eb9SGleb Smirnoff 				yyerror("sticky-address cannot be redefined");
44573b3a8eb9SGleb Smirnoff 				YYERROR;
44583b3a8eb9SGleb Smirnoff 			}
44593b3a8eb9SGleb Smirnoff 			pool_opts.marker |= POM_STICKYADDRESS;
44603b3a8eb9SGleb Smirnoff 			pool_opts.opts |= PF_POOL_STICKYADDR;
44613b3a8eb9SGleb Smirnoff 		}
44622aa21096SKurosawa Takahiro 		| MAPEPORTSET number '/' number '/' number {
44632aa21096SKurosawa Takahiro 			if (pool_opts.mape.offset) {
44642aa21096SKurosawa Takahiro 				yyerror("map-e-portset cannot be redefined");
44652aa21096SKurosawa Takahiro 				YYERROR;
44662aa21096SKurosawa Takahiro 			}
44672aa21096SKurosawa Takahiro 			if (pool_opts.type) {
44682aa21096SKurosawa Takahiro 				yyerror("map-e-portset cannot be used with "
44692aa21096SKurosawa Takahiro 					"address pools");
44702aa21096SKurosawa Takahiro 				YYERROR;
44712aa21096SKurosawa Takahiro 			}
44722aa21096SKurosawa Takahiro 			if ($2 <= 0 || $2 >= 16) {
44732aa21096SKurosawa Takahiro 				yyerror("MAP-E PSID offset must be 1-15");
44742aa21096SKurosawa Takahiro 				YYERROR;
44752aa21096SKurosawa Takahiro 			}
44762aa21096SKurosawa Takahiro 			if ($4 < 0 || $4 >= 16 || $2 + $4 > 16) {
44772aa21096SKurosawa Takahiro 				yyerror("Invalid MAP-E PSID length");
44782aa21096SKurosawa Takahiro 				YYERROR;
44792aa21096SKurosawa Takahiro 			} else if ($4 == 0) {
44802aa21096SKurosawa Takahiro 				yyerror("PSID Length = 0: this means"
44812aa21096SKurosawa Takahiro 				    " you do not need MAP-E");
44822aa21096SKurosawa Takahiro 				YYERROR;
44832aa21096SKurosawa Takahiro 			}
44842aa21096SKurosawa Takahiro 			if ($6 < 0 || $6 > 65535) {
44852aa21096SKurosawa Takahiro 				yyerror("Invalid MAP-E PSID");
44862aa21096SKurosawa Takahiro 				YYERROR;
44872aa21096SKurosawa Takahiro 			}
44882aa21096SKurosawa Takahiro 			pool_opts.mape.offset = $2;
44892aa21096SKurosawa Takahiro 			pool_opts.mape.psidlen = $4;
44902aa21096SKurosawa Takahiro 			pool_opts.mape.psid = $6;
44912aa21096SKurosawa Takahiro 		}
44923b3a8eb9SGleb Smirnoff 		;
44933b3a8eb9SGleb Smirnoff 
44943b3a8eb9SGleb Smirnoff redirection	: /* empty */			{ $$ = NULL; }
44953b3a8eb9SGleb Smirnoff 		| ARROW host			{
44963b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct redirection));
44973b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
44983b3a8eb9SGleb Smirnoff 				err(1, "redirection: calloc");
44993b3a8eb9SGleb Smirnoff 			$$->host = $2;
45003b3a8eb9SGleb Smirnoff 			$$->rport.a = $$->rport.b = $$->rport.t = 0;
45013b3a8eb9SGleb Smirnoff 		}
45023b3a8eb9SGleb Smirnoff 		| ARROW host PORT portstar	{
45033b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct redirection));
45043b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
45053b3a8eb9SGleb Smirnoff 				err(1, "redirection: calloc");
45063b3a8eb9SGleb Smirnoff 			$$->host = $2;
45073b3a8eb9SGleb Smirnoff 			$$->rport = $4;
45083b3a8eb9SGleb Smirnoff 		}
45093b3a8eb9SGleb Smirnoff 		;
45103b3a8eb9SGleb Smirnoff 
45113b3a8eb9SGleb Smirnoff natpasslog	: /* empty */	{ $$.b1 = $$.b2 = 0; $$.w2 = 0; }
45123b3a8eb9SGleb Smirnoff 		| PASS		{ $$.b1 = 1; $$.b2 = 0; $$.w2 = 0; }
45133b3a8eb9SGleb Smirnoff 		| PASS log	{ $$.b1 = 1; $$.b2 = $2.log; $$.w2 = $2.logif; }
45143b3a8eb9SGleb Smirnoff 		| log		{ $$.b1 = 0; $$.b2 = $1.log; $$.w2 = $1.logif; }
45153b3a8eb9SGleb Smirnoff 		;
45163b3a8eb9SGleb Smirnoff 
45173b3a8eb9SGleb Smirnoff nataction	: no NAT natpasslog {
45183b3a8eb9SGleb Smirnoff 			if ($1 && $3.b1) {
45193b3a8eb9SGleb Smirnoff 				yyerror("\"pass\" not valid with \"no\"");
45203b3a8eb9SGleb Smirnoff 				YYERROR;
45213b3a8eb9SGleb Smirnoff 			}
45223b3a8eb9SGleb Smirnoff 			if ($1)
45233b3a8eb9SGleb Smirnoff 				$$.b1 = PF_NONAT;
45243b3a8eb9SGleb Smirnoff 			else
45253b3a8eb9SGleb Smirnoff 				$$.b1 = PF_NAT;
45263b3a8eb9SGleb Smirnoff 			$$.b2 = $3.b1;
45273b3a8eb9SGleb Smirnoff 			$$.w = $3.b2;
45283b3a8eb9SGleb Smirnoff 			$$.w2 = $3.w2;
45293b3a8eb9SGleb Smirnoff 		}
45303b3a8eb9SGleb Smirnoff 		| no RDR natpasslog {
45313b3a8eb9SGleb Smirnoff 			if ($1 && $3.b1) {
45323b3a8eb9SGleb Smirnoff 				yyerror("\"pass\" not valid with \"no\"");
45333b3a8eb9SGleb Smirnoff 				YYERROR;
45343b3a8eb9SGleb Smirnoff 			}
45353b3a8eb9SGleb Smirnoff 			if ($1)
45363b3a8eb9SGleb Smirnoff 				$$.b1 = PF_NORDR;
45373b3a8eb9SGleb Smirnoff 			else
45383b3a8eb9SGleb Smirnoff 				$$.b1 = PF_RDR;
45393b3a8eb9SGleb Smirnoff 			$$.b2 = $3.b1;
45403b3a8eb9SGleb Smirnoff 			$$.w = $3.b2;
45413b3a8eb9SGleb Smirnoff 			$$.w2 = $3.w2;
45423b3a8eb9SGleb Smirnoff 		}
45433b3a8eb9SGleb Smirnoff 		;
45443b3a8eb9SGleb Smirnoff 
45453b3a8eb9SGleb Smirnoff natrule		: nataction interface af proto fromto tag tagged rtable
45463b3a8eb9SGleb Smirnoff 		    redirpool pool_opts
45473b3a8eb9SGleb Smirnoff 		{
4548e9eb0941SKristof Provost 			struct pfctl_rule	r;
45493b3a8eb9SGleb Smirnoff 
45503b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_NAT))
45513b3a8eb9SGleb Smirnoff 				YYERROR;
45523b3a8eb9SGleb Smirnoff 
45533b3a8eb9SGleb Smirnoff 			memset(&r, 0, sizeof(r));
45543b3a8eb9SGleb Smirnoff 
45553b3a8eb9SGleb Smirnoff 			r.action = $1.b1;
45563b3a8eb9SGleb Smirnoff 			r.natpass = $1.b2;
45573b3a8eb9SGleb Smirnoff 			r.log = $1.w;
45583b3a8eb9SGleb Smirnoff 			r.logif = $1.w2;
45593b3a8eb9SGleb Smirnoff 			r.af = $3;
45603b3a8eb9SGleb Smirnoff 
45613b3a8eb9SGleb Smirnoff 			if (!r.af) {
45623b3a8eb9SGleb Smirnoff 				if ($5.src.host && $5.src.host->af &&
45633b3a8eb9SGleb Smirnoff 				    !$5.src.host->ifindex)
45643b3a8eb9SGleb Smirnoff 					r.af = $5.src.host->af;
45653b3a8eb9SGleb Smirnoff 				else if ($5.dst.host && $5.dst.host->af &&
45663b3a8eb9SGleb Smirnoff 				    !$5.dst.host->ifindex)
45673b3a8eb9SGleb Smirnoff 					r.af = $5.dst.host->af;
45683b3a8eb9SGleb Smirnoff 			}
45693b3a8eb9SGleb Smirnoff 
45703b3a8eb9SGleb Smirnoff 			if ($6 != NULL)
45713b3a8eb9SGleb Smirnoff 				if (strlcpy(r.tagname, $6, PF_TAG_NAME_SIZE) >=
45723b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) {
45733b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
45743b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
45753b3a8eb9SGleb Smirnoff 					YYERROR;
45763b3a8eb9SGleb Smirnoff 				}
45773b3a8eb9SGleb Smirnoff 
45783b3a8eb9SGleb Smirnoff 			if ($7.name)
45793b3a8eb9SGleb Smirnoff 				if (strlcpy(r.match_tagname, $7.name,
45803b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
45813b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
45823b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
45833b3a8eb9SGleb Smirnoff 					YYERROR;
45843b3a8eb9SGleb Smirnoff 				}
45853b3a8eb9SGleb Smirnoff 			r.match_tag_not = $7.neg;
45863b3a8eb9SGleb Smirnoff 			r.rtableid = $8;
45873b3a8eb9SGleb Smirnoff 
45883b3a8eb9SGleb Smirnoff 			if (r.action == PF_NONAT || r.action == PF_NORDR) {
45893b3a8eb9SGleb Smirnoff 				if ($9 != NULL) {
45903b3a8eb9SGleb Smirnoff 					yyerror("translation rule with 'no' "
45913b3a8eb9SGleb Smirnoff 					    "does not need '->'");
45923b3a8eb9SGleb Smirnoff 					YYERROR;
45933b3a8eb9SGleb Smirnoff 				}
45943b3a8eb9SGleb Smirnoff 			} else {
45953b3a8eb9SGleb Smirnoff 				if ($9 == NULL || $9->host == NULL) {
45963b3a8eb9SGleb Smirnoff 					yyerror("translation rule requires '-> "
45973b3a8eb9SGleb Smirnoff 					    "address'");
45983b3a8eb9SGleb Smirnoff 					YYERROR;
45993b3a8eb9SGleb Smirnoff 				}
46003b3a8eb9SGleb Smirnoff 				if (!r.af && ! $9->host->ifindex)
46013b3a8eb9SGleb Smirnoff 					r.af = $9->host->af;
46023b3a8eb9SGleb Smirnoff 
46033b3a8eb9SGleb Smirnoff 				remove_invalid_hosts(&$9->host, &r.af);
46043b3a8eb9SGleb Smirnoff 				if (invalid_redirect($9->host, r.af))
46053b3a8eb9SGleb Smirnoff 					YYERROR;
46063b3a8eb9SGleb Smirnoff 				if (check_netmask($9->host, r.af))
46073b3a8eb9SGleb Smirnoff 					YYERROR;
46083b3a8eb9SGleb Smirnoff 
46093b3a8eb9SGleb Smirnoff 				r.rpool.proxy_port[0] = ntohs($9->rport.a);
46103b3a8eb9SGleb Smirnoff 
46113b3a8eb9SGleb Smirnoff 				switch (r.action) {
46123b3a8eb9SGleb Smirnoff 				case PF_RDR:
46133b3a8eb9SGleb Smirnoff 					if (!$9->rport.b && $9->rport.t &&
46143b3a8eb9SGleb Smirnoff 					    $5.dst.port != NULL) {
46153b3a8eb9SGleb Smirnoff 						r.rpool.proxy_port[1] =
46163b3a8eb9SGleb Smirnoff 						    ntohs($9->rport.a) +
46173b3a8eb9SGleb Smirnoff 						    (ntohs(
46183b3a8eb9SGleb Smirnoff 						    $5.dst.port->port[1]) -
46193b3a8eb9SGleb Smirnoff 						    ntohs(
46203b3a8eb9SGleb Smirnoff 						    $5.dst.port->port[0]));
46213b3a8eb9SGleb Smirnoff 					} else
46223b3a8eb9SGleb Smirnoff 						r.rpool.proxy_port[1] =
46233b3a8eb9SGleb Smirnoff 						    ntohs($9->rport.b);
46243b3a8eb9SGleb Smirnoff 					break;
46253b3a8eb9SGleb Smirnoff 				case PF_NAT:
46263b3a8eb9SGleb Smirnoff 					r.rpool.proxy_port[1] =
46273b3a8eb9SGleb Smirnoff 					    ntohs($9->rport.b);
46283b3a8eb9SGleb Smirnoff 					if (!r.rpool.proxy_port[0] &&
46293b3a8eb9SGleb Smirnoff 					    !r.rpool.proxy_port[1]) {
46303b3a8eb9SGleb Smirnoff 						r.rpool.proxy_port[0] =
46313b3a8eb9SGleb Smirnoff 						    PF_NAT_PROXY_PORT_LOW;
46323b3a8eb9SGleb Smirnoff 						r.rpool.proxy_port[1] =
46333b3a8eb9SGleb Smirnoff 						    PF_NAT_PROXY_PORT_HIGH;
46343b3a8eb9SGleb Smirnoff 					} else if (!r.rpool.proxy_port[1])
46353b3a8eb9SGleb Smirnoff 						r.rpool.proxy_port[1] =
46363b3a8eb9SGleb Smirnoff 						    r.rpool.proxy_port[0];
46373b3a8eb9SGleb Smirnoff 					break;
46383b3a8eb9SGleb Smirnoff 				default:
46393b3a8eb9SGleb Smirnoff 					break;
46403b3a8eb9SGleb Smirnoff 				}
46413b3a8eb9SGleb Smirnoff 
46423b3a8eb9SGleb Smirnoff 				r.rpool.opts = $10.type;
46433b3a8eb9SGleb Smirnoff 				if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
46443b3a8eb9SGleb Smirnoff 				    PF_POOL_NONE && ($9->host->next != NULL ||
46453b3a8eb9SGleb Smirnoff 				    $9->host->addr.type == PF_ADDR_TABLE ||
46463b3a8eb9SGleb Smirnoff 				    DYNIF_MULTIADDR($9->host->addr)))
46473b3a8eb9SGleb Smirnoff 					r.rpool.opts = PF_POOL_ROUNDROBIN;
46483b3a8eb9SGleb Smirnoff 				if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
46493b3a8eb9SGleb Smirnoff 				    PF_POOL_ROUNDROBIN &&
46503b3a8eb9SGleb Smirnoff 				    disallow_table($9->host, "tables are only "
46513b3a8eb9SGleb Smirnoff 				    "supported in round-robin redirection "
46523b3a8eb9SGleb Smirnoff 				    "pools"))
46533b3a8eb9SGleb Smirnoff 					YYERROR;
46543b3a8eb9SGleb Smirnoff 				if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
46553b3a8eb9SGleb Smirnoff 				    PF_POOL_ROUNDROBIN &&
46563b3a8eb9SGleb Smirnoff 				    disallow_alias($9->host, "interface (%s) "
46573b3a8eb9SGleb Smirnoff 				    "is only supported in round-robin "
46583b3a8eb9SGleb Smirnoff 				    "redirection pools"))
46593b3a8eb9SGleb Smirnoff 					YYERROR;
46603b3a8eb9SGleb Smirnoff 				if ($9->host->next != NULL) {
46613b3a8eb9SGleb Smirnoff 					if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
46623b3a8eb9SGleb Smirnoff 					    PF_POOL_ROUNDROBIN) {
46633b3a8eb9SGleb Smirnoff 						yyerror("only round-robin "
46643b3a8eb9SGleb Smirnoff 						    "valid for multiple "
46653b3a8eb9SGleb Smirnoff 						    "redirection addresses");
46663b3a8eb9SGleb Smirnoff 						YYERROR;
46673b3a8eb9SGleb Smirnoff 					}
46683b3a8eb9SGleb Smirnoff 				}
46693b3a8eb9SGleb Smirnoff 			}
46703b3a8eb9SGleb Smirnoff 
46713b3a8eb9SGleb Smirnoff 			if ($10.key != NULL)
46723b3a8eb9SGleb Smirnoff 				memcpy(&r.rpool.key, $10.key,
46733b3a8eb9SGleb Smirnoff 				    sizeof(struct pf_poolhashkey));
46743b3a8eb9SGleb Smirnoff 
46753b3a8eb9SGleb Smirnoff 			 if ($10.opts)
46763b3a8eb9SGleb Smirnoff 				r.rpool.opts |= $10.opts;
46773b3a8eb9SGleb Smirnoff 
46783b3a8eb9SGleb Smirnoff 			if ($10.staticport) {
46793b3a8eb9SGleb Smirnoff 				if (r.action != PF_NAT) {
46803b3a8eb9SGleb Smirnoff 					yyerror("the 'static-port' option is "
46813b3a8eb9SGleb Smirnoff 					    "only valid with nat rules");
46823b3a8eb9SGleb Smirnoff 					YYERROR;
46833b3a8eb9SGleb Smirnoff 				}
46843b3a8eb9SGleb Smirnoff 				if (r.rpool.proxy_port[0] !=
46853b3a8eb9SGleb Smirnoff 				    PF_NAT_PROXY_PORT_LOW &&
46863b3a8eb9SGleb Smirnoff 				    r.rpool.proxy_port[1] !=
46873b3a8eb9SGleb Smirnoff 				    PF_NAT_PROXY_PORT_HIGH) {
46883b3a8eb9SGleb Smirnoff 					yyerror("the 'static-port' option can't"
46893b3a8eb9SGleb Smirnoff 					    " be used when specifying a port"
46903b3a8eb9SGleb Smirnoff 					    " range");
46913b3a8eb9SGleb Smirnoff 					YYERROR;
46923b3a8eb9SGleb Smirnoff 				}
46933b3a8eb9SGleb Smirnoff 				r.rpool.proxy_port[0] = 0;
46943b3a8eb9SGleb Smirnoff 				r.rpool.proxy_port[1] = 0;
46953b3a8eb9SGleb Smirnoff 			}
46963b3a8eb9SGleb Smirnoff 
46972aa21096SKurosawa Takahiro 			if ($10.mape.offset) {
46982aa21096SKurosawa Takahiro 				if (r.action != PF_NAT) {
46992aa21096SKurosawa Takahiro 					yyerror("the 'map-e-portset' option is"
47002aa21096SKurosawa Takahiro 					    " only valid with nat rules");
47012aa21096SKurosawa Takahiro 					YYERROR;
47022aa21096SKurosawa Takahiro 				}
47032aa21096SKurosawa Takahiro 				if ($10.staticport) {
47042aa21096SKurosawa Takahiro 					yyerror("the 'map-e-portset' option"
47052aa21096SKurosawa Takahiro 					    " can't be used 'static-port'");
47062aa21096SKurosawa Takahiro 					YYERROR;
47072aa21096SKurosawa Takahiro 				}
47082aa21096SKurosawa Takahiro 				if (r.rpool.proxy_port[0] !=
47092aa21096SKurosawa Takahiro 				    PF_NAT_PROXY_PORT_LOW &&
47102aa21096SKurosawa Takahiro 				    r.rpool.proxy_port[1] !=
47112aa21096SKurosawa Takahiro 				    PF_NAT_PROXY_PORT_HIGH) {
47122aa21096SKurosawa Takahiro 					yyerror("the 'map-e-portset' option"
47132aa21096SKurosawa Takahiro 					    " can't be used when specifying"
47142aa21096SKurosawa Takahiro 					    " a port range");
47152aa21096SKurosawa Takahiro 					YYERROR;
47162aa21096SKurosawa Takahiro 				}
47172aa21096SKurosawa Takahiro 				r.rpool.mape = $10.mape;
47182aa21096SKurosawa Takahiro 			}
47192aa21096SKurosawa Takahiro 
47203b3a8eb9SGleb Smirnoff 			expand_rule(&r, $2, $9 == NULL ? NULL : $9->host, $4,
47213b3a8eb9SGleb Smirnoff 			    $5.src_os, $5.src.host, $5.src.port, $5.dst.host,
47223b3a8eb9SGleb Smirnoff 			    $5.dst.port, 0, 0, 0, "");
47233b3a8eb9SGleb Smirnoff 			free($9);
47243b3a8eb9SGleb Smirnoff 		}
47253b3a8eb9SGleb Smirnoff 		;
47263b3a8eb9SGleb Smirnoff 
47271e93588bSLuiz Otavio O Souza binatrule	: no BINAT natpasslog interface af proto FROM ipspec toipspec tag
47283b3a8eb9SGleb Smirnoff 		    tagged rtable redirection
47293b3a8eb9SGleb Smirnoff 		{
4730e9eb0941SKristof Provost 			struct pfctl_rule	binat;
47313b3a8eb9SGleb Smirnoff 			struct pf_pooladdr	*pa;
47323b3a8eb9SGleb Smirnoff 
47333b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_NAT))
47343b3a8eb9SGleb Smirnoff 				YYERROR;
47353b3a8eb9SGleb Smirnoff 			if (disallow_urpf_failed($9, "\"urpf-failed\" is not "
47363b3a8eb9SGleb Smirnoff 			    "permitted as a binat destination"))
47373b3a8eb9SGleb Smirnoff 				YYERROR;
47383b3a8eb9SGleb Smirnoff 
47393b3a8eb9SGleb Smirnoff 			memset(&binat, 0, sizeof(binat));
47403b3a8eb9SGleb Smirnoff 
47413b3a8eb9SGleb Smirnoff 			if ($1 && $3.b1) {
47423b3a8eb9SGleb Smirnoff 				yyerror("\"pass\" not valid with \"no\"");
47433b3a8eb9SGleb Smirnoff 				YYERROR;
47443b3a8eb9SGleb Smirnoff 			}
47453b3a8eb9SGleb Smirnoff 			if ($1)
47463b3a8eb9SGleb Smirnoff 				binat.action = PF_NOBINAT;
47473b3a8eb9SGleb Smirnoff 			else
47483b3a8eb9SGleb Smirnoff 				binat.action = PF_BINAT;
47493b3a8eb9SGleb Smirnoff 			binat.natpass = $3.b1;
47503b3a8eb9SGleb Smirnoff 			binat.log = $3.b2;
47513b3a8eb9SGleb Smirnoff 			binat.logif = $3.w2;
47523b3a8eb9SGleb Smirnoff 			binat.af = $5;
47533b3a8eb9SGleb Smirnoff 			if (!binat.af && $8 != NULL && $8->af)
47543b3a8eb9SGleb Smirnoff 				binat.af = $8->af;
47553b3a8eb9SGleb Smirnoff 			if (!binat.af && $9 != NULL && $9->af)
47563b3a8eb9SGleb Smirnoff 				binat.af = $9->af;
47573b3a8eb9SGleb Smirnoff 
47583b3a8eb9SGleb Smirnoff 			if (!binat.af && $13 != NULL && $13->host)
47593b3a8eb9SGleb Smirnoff 				binat.af = $13->host->af;
47603b3a8eb9SGleb Smirnoff 			if (!binat.af) {
47613b3a8eb9SGleb Smirnoff 				yyerror("address family (inet/inet6) "
47623b3a8eb9SGleb Smirnoff 				    "undefined");
47633b3a8eb9SGleb Smirnoff 				YYERROR;
47643b3a8eb9SGleb Smirnoff 			}
47653b3a8eb9SGleb Smirnoff 
47663b3a8eb9SGleb Smirnoff 			if ($4 != NULL) {
47673b3a8eb9SGleb Smirnoff 				memcpy(binat.ifname, $4->ifname,
47683b3a8eb9SGleb Smirnoff 				    sizeof(binat.ifname));
47693b3a8eb9SGleb Smirnoff 				binat.ifnot = $4->not;
47703b3a8eb9SGleb Smirnoff 				free($4);
47713b3a8eb9SGleb Smirnoff 			}
47723b3a8eb9SGleb Smirnoff 
47733b3a8eb9SGleb Smirnoff 			if ($10 != NULL)
47743b3a8eb9SGleb Smirnoff 				if (strlcpy(binat.tagname, $10,
47753b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
47763b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
47773b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
47783b3a8eb9SGleb Smirnoff 					YYERROR;
47793b3a8eb9SGleb Smirnoff 				}
47803b3a8eb9SGleb Smirnoff 			if ($11.name)
47813b3a8eb9SGleb Smirnoff 				if (strlcpy(binat.match_tagname, $11.name,
47823b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
47833b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
47843b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
47853b3a8eb9SGleb Smirnoff 					YYERROR;
47863b3a8eb9SGleb Smirnoff 				}
47873b3a8eb9SGleb Smirnoff 			binat.match_tag_not = $11.neg;
47883b3a8eb9SGleb Smirnoff 			binat.rtableid = $12;
47893b3a8eb9SGleb Smirnoff 
47903b3a8eb9SGleb Smirnoff 			if ($6 != NULL) {
47913b3a8eb9SGleb Smirnoff 				binat.proto = $6->proto;
47923b3a8eb9SGleb Smirnoff 				free($6);
47933b3a8eb9SGleb Smirnoff 			}
47943b3a8eb9SGleb Smirnoff 
47953b3a8eb9SGleb Smirnoff 			if ($8 != NULL && disallow_table($8, "invalid use of "
47963b3a8eb9SGleb Smirnoff 			    "table <%s> as the source address of a binat rule"))
47973b3a8eb9SGleb Smirnoff 				YYERROR;
47983b3a8eb9SGleb Smirnoff 			if ($8 != NULL && disallow_alias($8, "invalid use of "
47993b3a8eb9SGleb Smirnoff 			    "interface (%s) as the source address of a binat "
48003b3a8eb9SGleb Smirnoff 			    "rule"))
48013b3a8eb9SGleb Smirnoff 				YYERROR;
48023b3a8eb9SGleb Smirnoff 			if ($13 != NULL && $13->host != NULL && disallow_table(
48033b3a8eb9SGleb Smirnoff 			    $13->host, "invalid use of table <%s> as the "
48043b3a8eb9SGleb Smirnoff 			    "redirect address of a binat rule"))
48053b3a8eb9SGleb Smirnoff 				YYERROR;
48063b3a8eb9SGleb Smirnoff 			if ($13 != NULL && $13->host != NULL && disallow_alias(
48073b3a8eb9SGleb Smirnoff 			    $13->host, "invalid use of interface (%s) as the "
48083b3a8eb9SGleb Smirnoff 			    "redirect address of a binat rule"))
48093b3a8eb9SGleb Smirnoff 				YYERROR;
48103b3a8eb9SGleb Smirnoff 
48113b3a8eb9SGleb Smirnoff 			if ($8 != NULL) {
48123b3a8eb9SGleb Smirnoff 				if ($8->next) {
48133b3a8eb9SGleb Smirnoff 					yyerror("multiple binat ip addresses");
48143b3a8eb9SGleb Smirnoff 					YYERROR;
48153b3a8eb9SGleb Smirnoff 				}
48163b3a8eb9SGleb Smirnoff 				if ($8->addr.type == PF_ADDR_DYNIFTL)
48173b3a8eb9SGleb Smirnoff 					$8->af = binat.af;
48183b3a8eb9SGleb Smirnoff 				if ($8->af != binat.af) {
48193b3a8eb9SGleb Smirnoff 					yyerror("binat ip versions must match");
48203b3a8eb9SGleb Smirnoff 					YYERROR;
48213b3a8eb9SGleb Smirnoff 				}
48223b3a8eb9SGleb Smirnoff 				if (check_netmask($8, binat.af))
48233b3a8eb9SGleb Smirnoff 					YYERROR;
48243b3a8eb9SGleb Smirnoff 				memcpy(&binat.src.addr, &$8->addr,
48253b3a8eb9SGleb Smirnoff 				    sizeof(binat.src.addr));
48263b3a8eb9SGleb Smirnoff 				free($8);
48273b3a8eb9SGleb Smirnoff 			}
48283b3a8eb9SGleb Smirnoff 			if ($9 != NULL) {
48293b3a8eb9SGleb Smirnoff 				if ($9->next) {
48303b3a8eb9SGleb Smirnoff 					yyerror("multiple binat ip addresses");
48313b3a8eb9SGleb Smirnoff 					YYERROR;
48323b3a8eb9SGleb Smirnoff 				}
48333b3a8eb9SGleb Smirnoff 				if ($9->af != binat.af && $9->af) {
48343b3a8eb9SGleb Smirnoff 					yyerror("binat ip versions must match");
48353b3a8eb9SGleb Smirnoff 					YYERROR;
48363b3a8eb9SGleb Smirnoff 				}
48373b3a8eb9SGleb Smirnoff 				if (check_netmask($9, binat.af))
48383b3a8eb9SGleb Smirnoff 					YYERROR;
48393b3a8eb9SGleb Smirnoff 				memcpy(&binat.dst.addr, &$9->addr,
48403b3a8eb9SGleb Smirnoff 				    sizeof(binat.dst.addr));
48413b3a8eb9SGleb Smirnoff 				binat.dst.neg = $9->not;
48423b3a8eb9SGleb Smirnoff 				free($9);
48433b3a8eb9SGleb Smirnoff 			}
48443b3a8eb9SGleb Smirnoff 
48453b3a8eb9SGleb Smirnoff 			if (binat.action == PF_NOBINAT) {
48463b3a8eb9SGleb Smirnoff 				if ($13 != NULL) {
48473b3a8eb9SGleb Smirnoff 					yyerror("'no binat' rule does not need"
48483b3a8eb9SGleb Smirnoff 					    " '->'");
48493b3a8eb9SGleb Smirnoff 					YYERROR;
48503b3a8eb9SGleb Smirnoff 				}
48513b3a8eb9SGleb Smirnoff 			} else {
48523b3a8eb9SGleb Smirnoff 				if ($13 == NULL || $13->host == NULL) {
48533b3a8eb9SGleb Smirnoff 					yyerror("'binat' rule requires"
48543b3a8eb9SGleb Smirnoff 					    " '-> address'");
48553b3a8eb9SGleb Smirnoff 					YYERROR;
48563b3a8eb9SGleb Smirnoff 				}
48573b3a8eb9SGleb Smirnoff 
48583b3a8eb9SGleb Smirnoff 				remove_invalid_hosts(&$13->host, &binat.af);
48593b3a8eb9SGleb Smirnoff 				if (invalid_redirect($13->host, binat.af))
48603b3a8eb9SGleb Smirnoff 					YYERROR;
48613b3a8eb9SGleb Smirnoff 				if ($13->host->next != NULL) {
48623b3a8eb9SGleb Smirnoff 					yyerror("binat rule must redirect to "
48633b3a8eb9SGleb Smirnoff 					    "a single address");
48643b3a8eb9SGleb Smirnoff 					YYERROR;
48653b3a8eb9SGleb Smirnoff 				}
48663b3a8eb9SGleb Smirnoff 				if (check_netmask($13->host, binat.af))
48673b3a8eb9SGleb Smirnoff 					YYERROR;
48683b3a8eb9SGleb Smirnoff 
48693b3a8eb9SGleb Smirnoff 				if (!PF_AZERO(&binat.src.addr.v.a.mask,
48703b3a8eb9SGleb Smirnoff 				    binat.af) &&
48713b3a8eb9SGleb Smirnoff 				    !PF_AEQ(&binat.src.addr.v.a.mask,
48723b3a8eb9SGleb Smirnoff 				    &$13->host->addr.v.a.mask, binat.af)) {
48733b3a8eb9SGleb Smirnoff 					yyerror("'binat' source mask and "
48743b3a8eb9SGleb Smirnoff 					    "redirect mask must be the same");
48753b3a8eb9SGleb Smirnoff 					YYERROR;
48763b3a8eb9SGleb Smirnoff 				}
48773b3a8eb9SGleb Smirnoff 
48783b3a8eb9SGleb Smirnoff 				TAILQ_INIT(&binat.rpool.list);
48793b3a8eb9SGleb Smirnoff 				pa = calloc(1, sizeof(struct pf_pooladdr));
48803b3a8eb9SGleb Smirnoff 				if (pa == NULL)
48813b3a8eb9SGleb Smirnoff 					err(1, "binat: calloc");
48823b3a8eb9SGleb Smirnoff 				pa->addr = $13->host->addr;
48833b3a8eb9SGleb Smirnoff 				pa->ifname[0] = 0;
48843b3a8eb9SGleb Smirnoff 				TAILQ_INSERT_TAIL(&binat.rpool.list,
48853b3a8eb9SGleb Smirnoff 				    pa, entries);
48863b3a8eb9SGleb Smirnoff 
48873b3a8eb9SGleb Smirnoff 				free($13);
48883b3a8eb9SGleb Smirnoff 			}
48893b3a8eb9SGleb Smirnoff 
48900d71f9f3SKristof Provost 			pfctl_append_rule(pf, &binat, "");
48913b3a8eb9SGleb Smirnoff 		}
48923b3a8eb9SGleb Smirnoff 		;
48933b3a8eb9SGleb Smirnoff 
48943b3a8eb9SGleb Smirnoff tag		: /* empty */		{ $$ = NULL; }
48953b3a8eb9SGleb Smirnoff 		| TAG STRING		{ $$ = $2; }
48963b3a8eb9SGleb Smirnoff 		;
48973b3a8eb9SGleb Smirnoff 
48983b3a8eb9SGleb Smirnoff tagged		: /* empty */		{ $$.neg = 0; $$.name = NULL; }
48993b3a8eb9SGleb Smirnoff 		| not TAGGED string	{ $$.neg = $1; $$.name = $3; }
49003b3a8eb9SGleb Smirnoff 		;
49013b3a8eb9SGleb Smirnoff 
49023b3a8eb9SGleb Smirnoff rtable		: /* empty */		{ $$ = -1; }
49033b3a8eb9SGleb Smirnoff 		| RTABLE NUMBER		{
49043b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > rt_tableid_max()) {
49053b3a8eb9SGleb Smirnoff 				yyerror("invalid rtable id");
49063b3a8eb9SGleb Smirnoff 				YYERROR;
49073b3a8eb9SGleb Smirnoff 			}
49083b3a8eb9SGleb Smirnoff 			$$ = $2;
49093b3a8eb9SGleb Smirnoff 		}
49103b3a8eb9SGleb Smirnoff 		;
49113b3a8eb9SGleb Smirnoff 
49123b3a8eb9SGleb Smirnoff route_host	: STRING			{
49133b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_host));
49143b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
49153b3a8eb9SGleb Smirnoff 				err(1, "route_host: calloc");
4916e68de669SKristof Provost 			if (strlen($1) >= IFNAMSIZ) {
4917e68de669SKristof Provost 				yyerror("interface name too long");
4918e68de669SKristof Provost 				YYERROR;
4919e68de669SKristof Provost 			}
4920a2a90d6eSKristof Provost 			$$->ifname = strdup($1);
49213b3a8eb9SGleb Smirnoff 			set_ipmask($$, 128);
49223b3a8eb9SGleb Smirnoff 			$$->next = NULL;
49233b3a8eb9SGleb Smirnoff 			$$->tail = $$;
49243b3a8eb9SGleb Smirnoff 		}
49253b3a8eb9SGleb Smirnoff 		| '(' STRING host ')'		{
492658c8430aSKristof Provost 			struct node_host *n;
492758c8430aSKristof Provost 
49283b3a8eb9SGleb Smirnoff 			$$ = $3;
4929e68de669SKristof Provost 			for (n = $3; n != NULL; n = n->next) {
4930e68de669SKristof Provost 				if (strlen($2) >= IFNAMSIZ) {
4931e68de669SKristof Provost 					yyerror("interface name too long");
4932e68de669SKristof Provost 					YYERROR;
4933e68de669SKristof Provost 				}
4934a2a90d6eSKristof Provost 				n->ifname = strdup($2);
49353b3a8eb9SGleb Smirnoff 			}
4936e68de669SKristof Provost 		}
49373b3a8eb9SGleb Smirnoff 		;
49383b3a8eb9SGleb Smirnoff 
49393b3a8eb9SGleb Smirnoff route_host_list	: route_host optnl			{ $$ = $1; }
49403b3a8eb9SGleb Smirnoff 		| route_host_list comma route_host optnl {
49413b3a8eb9SGleb Smirnoff 			if ($1->af == 0)
49423b3a8eb9SGleb Smirnoff 				$1->af = $3->af;
49433b3a8eb9SGleb Smirnoff 			if ($1->af != $3->af) {
49443b3a8eb9SGleb Smirnoff 				yyerror("all pool addresses must be in the "
49453b3a8eb9SGleb Smirnoff 				    "same address family");
49463b3a8eb9SGleb Smirnoff 				YYERROR;
49473b3a8eb9SGleb Smirnoff 			}
49483b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
49493b3a8eb9SGleb Smirnoff 			$1->tail = $3->tail;
49503b3a8eb9SGleb Smirnoff 			$$ = $1;
49513b3a8eb9SGleb Smirnoff 		}
49523b3a8eb9SGleb Smirnoff 		;
49533b3a8eb9SGleb Smirnoff 
49543b3a8eb9SGleb Smirnoff routespec	: route_host			{ $$ = $1; }
49553b3a8eb9SGleb Smirnoff 		| '{' optnl route_host_list '}'	{ $$ = $3; }
49563b3a8eb9SGleb Smirnoff 		;
49573b3a8eb9SGleb Smirnoff 
49583b3a8eb9SGleb Smirnoff route		: /* empty */			{
49593b3a8eb9SGleb Smirnoff 			$$.host = NULL;
49603b3a8eb9SGleb Smirnoff 			$$.rt = 0;
49613b3a8eb9SGleb Smirnoff 			$$.pool_opts = 0;
49623b3a8eb9SGleb Smirnoff 		}
49633b3a8eb9SGleb Smirnoff 		| FASTROUTE {
4964813196a1SKristof Provost 			/* backwards-compat */
49653b3a8eb9SGleb Smirnoff 			$$.host = NULL;
4966813196a1SKristof Provost 			$$.rt = 0;
49673b3a8eb9SGleb Smirnoff 			$$.pool_opts = 0;
49683b3a8eb9SGleb Smirnoff 		}
49693b3a8eb9SGleb Smirnoff 		| ROUTETO routespec pool_opts {
49703b3a8eb9SGleb Smirnoff 			$$.host = $2;
49713b3a8eb9SGleb Smirnoff 			$$.rt = PF_ROUTETO;
49723b3a8eb9SGleb Smirnoff 			$$.pool_opts = $3.type | $3.opts;
49733b3a8eb9SGleb Smirnoff 			if ($3.key != NULL)
49743b3a8eb9SGleb Smirnoff 				$$.key = $3.key;
49753b3a8eb9SGleb Smirnoff 		}
49763b3a8eb9SGleb Smirnoff 		| REPLYTO routespec pool_opts {
49773b3a8eb9SGleb Smirnoff 			$$.host = $2;
49783b3a8eb9SGleb Smirnoff 			$$.rt = PF_REPLYTO;
49793b3a8eb9SGleb Smirnoff 			$$.pool_opts = $3.type | $3.opts;
49803b3a8eb9SGleb Smirnoff 			if ($3.key != NULL)
49813b3a8eb9SGleb Smirnoff 				$$.key = $3.key;
49823b3a8eb9SGleb Smirnoff 		}
49833b3a8eb9SGleb Smirnoff 		| DUPTO routespec pool_opts {
49843b3a8eb9SGleb Smirnoff 			$$.host = $2;
49853b3a8eb9SGleb Smirnoff 			$$.rt = PF_DUPTO;
49863b3a8eb9SGleb Smirnoff 			$$.pool_opts = $3.type | $3.opts;
49873b3a8eb9SGleb Smirnoff 			if ($3.key != NULL)
49883b3a8eb9SGleb Smirnoff 				$$.key = $3.key;
49893b3a8eb9SGleb Smirnoff 		}
49903b3a8eb9SGleb Smirnoff 		;
49913b3a8eb9SGleb Smirnoff 
49923b3a8eb9SGleb Smirnoff timeout_spec	: STRING NUMBER
49933b3a8eb9SGleb Smirnoff 		{
49943b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_OPTION)) {
49953b3a8eb9SGleb Smirnoff 				free($1);
49963b3a8eb9SGleb Smirnoff 				YYERROR;
49973b3a8eb9SGleb Smirnoff 			}
49983b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > UINT_MAX) {
49993b3a8eb9SGleb Smirnoff 				yyerror("only positive values permitted");
50003b3a8eb9SGleb Smirnoff 				YYERROR;
50013b3a8eb9SGleb Smirnoff 			}
50023b3a8eb9SGleb Smirnoff 			if (pfctl_set_timeout(pf, $1, $2, 0) != 0) {
50033b3a8eb9SGleb Smirnoff 				yyerror("unknown timeout %s", $1);
50043b3a8eb9SGleb Smirnoff 				free($1);
50053b3a8eb9SGleb Smirnoff 				YYERROR;
50063b3a8eb9SGleb Smirnoff 			}
50073b3a8eb9SGleb Smirnoff 			free($1);
50083b3a8eb9SGleb Smirnoff 		}
50097f8af000SLuiz Otavio O Souza 		| INTERVAL NUMBER		{
50107f8af000SLuiz Otavio O Souza 			if (check_rulestate(PFCTL_STATE_OPTION))
50117f8af000SLuiz Otavio O Souza 				YYERROR;
50127f8af000SLuiz Otavio O Souza 			if ($2 < 0 || $2 > UINT_MAX) {
50137f8af000SLuiz Otavio O Souza 				yyerror("only positive values permitted");
50147f8af000SLuiz Otavio O Souza 				YYERROR;
50157f8af000SLuiz Otavio O Souza 			}
50167f8af000SLuiz Otavio O Souza 			if (pfctl_set_timeout(pf, "interval", $2, 0) != 0)
50177f8af000SLuiz Otavio O Souza 				YYERROR;
50187f8af000SLuiz Otavio O Souza 		}
50193b3a8eb9SGleb Smirnoff 		;
50203b3a8eb9SGleb Smirnoff 
50213b3a8eb9SGleb Smirnoff timeout_list	: timeout_list comma timeout_spec optnl
50223b3a8eb9SGleb Smirnoff 		| timeout_spec optnl
50233b3a8eb9SGleb Smirnoff 		;
50243b3a8eb9SGleb Smirnoff 
50253b3a8eb9SGleb Smirnoff limit_spec	: STRING NUMBER
50263b3a8eb9SGleb Smirnoff 		{
50273b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_OPTION)) {
50283b3a8eb9SGleb Smirnoff 				free($1);
50293b3a8eb9SGleb Smirnoff 				YYERROR;
50303b3a8eb9SGleb Smirnoff 			}
50313b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > UINT_MAX) {
50323b3a8eb9SGleb Smirnoff 				yyerror("only positive values permitted");
50333b3a8eb9SGleb Smirnoff 				YYERROR;
50343b3a8eb9SGleb Smirnoff 			}
50353b3a8eb9SGleb Smirnoff 			if (pfctl_set_limit(pf, $1, $2) != 0) {
50363b3a8eb9SGleb Smirnoff 				yyerror("unable to set limit %s %u", $1, $2);
50373b3a8eb9SGleb Smirnoff 				free($1);
50383b3a8eb9SGleb Smirnoff 				YYERROR;
50393b3a8eb9SGleb Smirnoff 			}
50403b3a8eb9SGleb Smirnoff 			free($1);
50413b3a8eb9SGleb Smirnoff 		}
50423b3a8eb9SGleb Smirnoff 		;
50433b3a8eb9SGleb Smirnoff 
50443b3a8eb9SGleb Smirnoff limit_list	: limit_list comma limit_spec optnl
50453b3a8eb9SGleb Smirnoff 		| limit_spec optnl
50463b3a8eb9SGleb Smirnoff 		;
50473b3a8eb9SGleb Smirnoff 
50483b3a8eb9SGleb Smirnoff comma		: ','
50493b3a8eb9SGleb Smirnoff 		| /* empty */
50503b3a8eb9SGleb Smirnoff 		;
50513b3a8eb9SGleb Smirnoff 
50523b3a8eb9SGleb Smirnoff yesno		: NO			{ $$ = 0; }
50533b3a8eb9SGleb Smirnoff 		| STRING		{
50543b3a8eb9SGleb Smirnoff 			if (!strcmp($1, "yes"))
50553b3a8eb9SGleb Smirnoff 				$$ = 1;
50563b3a8eb9SGleb Smirnoff 			else {
50573b3a8eb9SGleb Smirnoff 				yyerror("invalid value '%s', expected 'yes' "
50583b3a8eb9SGleb Smirnoff 				    "or 'no'", $1);
50593b3a8eb9SGleb Smirnoff 				free($1);
50603b3a8eb9SGleb Smirnoff 				YYERROR;
50613b3a8eb9SGleb Smirnoff 			}
50623b3a8eb9SGleb Smirnoff 			free($1);
50633b3a8eb9SGleb Smirnoff 		}
50643b3a8eb9SGleb Smirnoff 		;
50653b3a8eb9SGleb Smirnoff 
50663b3a8eb9SGleb Smirnoff unaryop		: '='		{ $$ = PF_OP_EQ; }
50673b3a8eb9SGleb Smirnoff 		| '!' '='	{ $$ = PF_OP_NE; }
50683b3a8eb9SGleb Smirnoff 		| '<' '='	{ $$ = PF_OP_LE; }
50693b3a8eb9SGleb Smirnoff 		| '<'		{ $$ = PF_OP_LT; }
50703b3a8eb9SGleb Smirnoff 		| '>' '='	{ $$ = PF_OP_GE; }
50713b3a8eb9SGleb Smirnoff 		| '>'		{ $$ = PF_OP_GT; }
50723b3a8eb9SGleb Smirnoff 		;
50733b3a8eb9SGleb Smirnoff 
50743b3a8eb9SGleb Smirnoff %%
50753b3a8eb9SGleb Smirnoff 
50763b3a8eb9SGleb Smirnoff int
50773b3a8eb9SGleb Smirnoff yyerror(const char *fmt, ...)
50783b3a8eb9SGleb Smirnoff {
50793b3a8eb9SGleb Smirnoff 	va_list		 ap;
50803b3a8eb9SGleb Smirnoff 
50813b3a8eb9SGleb Smirnoff 	file->errors++;
50823b3a8eb9SGleb Smirnoff 	va_start(ap, fmt);
50833b3a8eb9SGleb Smirnoff 	fprintf(stderr, "%s:%d: ", file->name, yylval.lineno);
50843b3a8eb9SGleb Smirnoff 	vfprintf(stderr, fmt, ap);
50853b3a8eb9SGleb Smirnoff 	fprintf(stderr, "\n");
50863b3a8eb9SGleb Smirnoff 	va_end(ap);
50873b3a8eb9SGleb Smirnoff 	return (0);
50883b3a8eb9SGleb Smirnoff }
50893b3a8eb9SGleb Smirnoff 
50903b3a8eb9SGleb Smirnoff int
50913b3a8eb9SGleb Smirnoff disallow_table(struct node_host *h, const char *fmt)
50923b3a8eb9SGleb Smirnoff {
50933b3a8eb9SGleb Smirnoff 	for (; h != NULL; h = h->next)
50943b3a8eb9SGleb Smirnoff 		if (h->addr.type == PF_ADDR_TABLE) {
50953b3a8eb9SGleb Smirnoff 			yyerror(fmt, h->addr.v.tblname);
50963b3a8eb9SGleb Smirnoff 			return (1);
50973b3a8eb9SGleb Smirnoff 		}
50983b3a8eb9SGleb Smirnoff 	return (0);
50993b3a8eb9SGleb Smirnoff }
51003b3a8eb9SGleb Smirnoff 
51013b3a8eb9SGleb Smirnoff int
51023b3a8eb9SGleb Smirnoff disallow_urpf_failed(struct node_host *h, const char *fmt)
51033b3a8eb9SGleb Smirnoff {
51043b3a8eb9SGleb Smirnoff 	for (; h != NULL; h = h->next)
51053b3a8eb9SGleb Smirnoff 		if (h->addr.type == PF_ADDR_URPFFAILED) {
51063b3a8eb9SGleb Smirnoff 			yyerror(fmt);
51073b3a8eb9SGleb Smirnoff 			return (1);
51083b3a8eb9SGleb Smirnoff 		}
51093b3a8eb9SGleb Smirnoff 	return (0);
51103b3a8eb9SGleb Smirnoff }
51113b3a8eb9SGleb Smirnoff 
51123b3a8eb9SGleb Smirnoff int
51133b3a8eb9SGleb Smirnoff disallow_alias(struct node_host *h, const char *fmt)
51143b3a8eb9SGleb Smirnoff {
51153b3a8eb9SGleb Smirnoff 	for (; h != NULL; h = h->next)
51163b3a8eb9SGleb Smirnoff 		if (DYNIF_MULTIADDR(h->addr)) {
51173b3a8eb9SGleb Smirnoff 			yyerror(fmt, h->addr.v.tblname);
51183b3a8eb9SGleb Smirnoff 			return (1);
51193b3a8eb9SGleb Smirnoff 		}
51203b3a8eb9SGleb Smirnoff 	return (0);
51213b3a8eb9SGleb Smirnoff }
51223b3a8eb9SGleb Smirnoff 
51233b3a8eb9SGleb Smirnoff int
5124e9eb0941SKristof Provost rule_consistent(struct pfctl_rule *r, int anchor_call)
51253b3a8eb9SGleb Smirnoff {
51263b3a8eb9SGleb Smirnoff 	int	problems = 0;
51273b3a8eb9SGleb Smirnoff 
51283b3a8eb9SGleb Smirnoff 	switch (r->action) {
51293b3a8eb9SGleb Smirnoff 	case PF_PASS:
51303b3a8eb9SGleb Smirnoff 	case PF_DROP:
51313b3a8eb9SGleb Smirnoff 	case PF_SCRUB:
51323b3a8eb9SGleb Smirnoff 	case PF_NOSCRUB:
51333b3a8eb9SGleb Smirnoff 		problems = filter_consistent(r, anchor_call);
51343b3a8eb9SGleb Smirnoff 		break;
51353b3a8eb9SGleb Smirnoff 	case PF_NAT:
51363b3a8eb9SGleb Smirnoff 	case PF_NONAT:
51373b3a8eb9SGleb Smirnoff 		problems = nat_consistent(r);
51383b3a8eb9SGleb Smirnoff 		break;
51393b3a8eb9SGleb Smirnoff 	case PF_RDR:
51403b3a8eb9SGleb Smirnoff 	case PF_NORDR:
51413b3a8eb9SGleb Smirnoff 		problems = rdr_consistent(r);
51423b3a8eb9SGleb Smirnoff 		break;
51433b3a8eb9SGleb Smirnoff 	case PF_BINAT:
51443b3a8eb9SGleb Smirnoff 	case PF_NOBINAT:
51453b3a8eb9SGleb Smirnoff 	default:
51463b3a8eb9SGleb Smirnoff 		break;
51473b3a8eb9SGleb Smirnoff 	}
51483b3a8eb9SGleb Smirnoff 	return (problems);
51493b3a8eb9SGleb Smirnoff }
51503b3a8eb9SGleb Smirnoff 
51513b3a8eb9SGleb Smirnoff int
5152e9eb0941SKristof Provost filter_consistent(struct pfctl_rule *r, int anchor_call)
51533b3a8eb9SGleb Smirnoff {
51543b3a8eb9SGleb Smirnoff 	int	problems = 0;
51553b3a8eb9SGleb Smirnoff 
51563b3a8eb9SGleb Smirnoff 	if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP &&
51573b3a8eb9SGleb Smirnoff 	    (r->src.port_op || r->dst.port_op)) {
51583b3a8eb9SGleb Smirnoff 		yyerror("port only applies to tcp/udp");
51593b3a8eb9SGleb Smirnoff 		problems++;
51603b3a8eb9SGleb Smirnoff 	}
51613b3a8eb9SGleb Smirnoff 	if (r->proto != IPPROTO_ICMP && r->proto != IPPROTO_ICMPV6 &&
51623b3a8eb9SGleb Smirnoff 	    (r->type || r->code)) {
51633b3a8eb9SGleb Smirnoff 		yyerror("icmp-type/code only applies to icmp");
51643b3a8eb9SGleb Smirnoff 		problems++;
51653b3a8eb9SGleb Smirnoff 	}
51663b3a8eb9SGleb Smirnoff 	if (!r->af && (r->type || r->code)) {
51673b3a8eb9SGleb Smirnoff 		yyerror("must indicate address family with icmp-type/code");
51683b3a8eb9SGleb Smirnoff 		problems++;
51693b3a8eb9SGleb Smirnoff 	}
51703b3a8eb9SGleb Smirnoff 	if (r->overload_tblname[0] &&
51713b3a8eb9SGleb Smirnoff 	    r->max_src_conn == 0 && r->max_src_conn_rate.seconds == 0) {
51723b3a8eb9SGleb Smirnoff 		yyerror("'overload' requires 'max-src-conn' "
51733b3a8eb9SGleb Smirnoff 		    "or 'max-src-conn-rate'");
51743b3a8eb9SGleb Smirnoff 		problems++;
51753b3a8eb9SGleb Smirnoff 	}
51763b3a8eb9SGleb Smirnoff 	if ((r->proto == IPPROTO_ICMP && r->af == AF_INET6) ||
51773b3a8eb9SGleb Smirnoff 	    (r->proto == IPPROTO_ICMPV6 && r->af == AF_INET)) {
51783b3a8eb9SGleb Smirnoff 		yyerror("proto %s doesn't match address family %s",
51793b3a8eb9SGleb Smirnoff 		    r->proto == IPPROTO_ICMP ? "icmp" : "icmp6",
51803b3a8eb9SGleb Smirnoff 		    r->af == AF_INET ? "inet" : "inet6");
51813b3a8eb9SGleb Smirnoff 		problems++;
51823b3a8eb9SGleb Smirnoff 	}
51833b3a8eb9SGleb Smirnoff 	if (r->allow_opts && r->action != PF_PASS) {
51843b3a8eb9SGleb Smirnoff 		yyerror("allow-opts can only be specified for pass rules");
51853b3a8eb9SGleb Smirnoff 		problems++;
51863b3a8eb9SGleb Smirnoff 	}
51873b3a8eb9SGleb Smirnoff 	if (r->rule_flag & PFRULE_FRAGMENT && (r->src.port_op ||
51883b3a8eb9SGleb Smirnoff 	    r->dst.port_op || r->flagset || r->type || r->code)) {
51893b3a8eb9SGleb Smirnoff 		yyerror("fragments can be filtered only on IP header fields");
51903b3a8eb9SGleb Smirnoff 		problems++;
51913b3a8eb9SGleb Smirnoff 	}
51923b3a8eb9SGleb Smirnoff 	if (r->rule_flag & PFRULE_RETURNRST && r->proto != IPPROTO_TCP) {
51933b3a8eb9SGleb Smirnoff 		yyerror("return-rst can only be applied to TCP rules");
51943b3a8eb9SGleb Smirnoff 		problems++;
51953b3a8eb9SGleb Smirnoff 	}
51963b3a8eb9SGleb Smirnoff 	if (r->max_src_nodes && !(r->rule_flag & PFRULE_RULESRCTRACK)) {
51973b3a8eb9SGleb Smirnoff 		yyerror("max-src-nodes requires 'source-track rule'");
51983b3a8eb9SGleb Smirnoff 		problems++;
51993b3a8eb9SGleb Smirnoff 	}
52003b3a8eb9SGleb Smirnoff 	if (r->action == PF_DROP && r->keep_state) {
52013b3a8eb9SGleb Smirnoff 		yyerror("keep state on block rules doesn't make sense");
52023b3a8eb9SGleb Smirnoff 		problems++;
52033b3a8eb9SGleb Smirnoff 	}
52043b3a8eb9SGleb Smirnoff 	if (r->rule_flag & PFRULE_STATESLOPPY &&
52053b3a8eb9SGleb Smirnoff 	    (r->keep_state == PF_STATE_MODULATE ||
52063b3a8eb9SGleb Smirnoff 	    r->keep_state == PF_STATE_SYNPROXY)) {
52073b3a8eb9SGleb Smirnoff 		yyerror("sloppy state matching cannot be used with "
52083b3a8eb9SGleb Smirnoff 		    "synproxy state or modulate state");
52093b3a8eb9SGleb Smirnoff 		problems++;
52103b3a8eb9SGleb Smirnoff 	}
52113b3a8eb9SGleb Smirnoff 	return (-problems);
52123b3a8eb9SGleb Smirnoff }
52133b3a8eb9SGleb Smirnoff 
52143b3a8eb9SGleb Smirnoff int
5215e9eb0941SKristof Provost nat_consistent(struct pfctl_rule *r)
52163b3a8eb9SGleb Smirnoff {
52173b3a8eb9SGleb Smirnoff 	return (0);	/* yeah! */
52183b3a8eb9SGleb Smirnoff }
52193b3a8eb9SGleb Smirnoff 
52203b3a8eb9SGleb Smirnoff int
5221e9eb0941SKristof Provost rdr_consistent(struct pfctl_rule *r)
52223b3a8eb9SGleb Smirnoff {
52233b3a8eb9SGleb Smirnoff 	int			 problems = 0;
52243b3a8eb9SGleb Smirnoff 
52253b3a8eb9SGleb Smirnoff 	if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP) {
52263b3a8eb9SGleb Smirnoff 		if (r->src.port_op) {
52273b3a8eb9SGleb Smirnoff 			yyerror("src port only applies to tcp/udp");
52283b3a8eb9SGleb Smirnoff 			problems++;
52293b3a8eb9SGleb Smirnoff 		}
52303b3a8eb9SGleb Smirnoff 		if (r->dst.port_op) {
52313b3a8eb9SGleb Smirnoff 			yyerror("dst port only applies to tcp/udp");
52323b3a8eb9SGleb Smirnoff 			problems++;
52333b3a8eb9SGleb Smirnoff 		}
52343b3a8eb9SGleb Smirnoff 		if (r->rpool.proxy_port[0]) {
52353b3a8eb9SGleb Smirnoff 			yyerror("rpool port only applies to tcp/udp");
52363b3a8eb9SGleb Smirnoff 			problems++;
52373b3a8eb9SGleb Smirnoff 		}
52383b3a8eb9SGleb Smirnoff 	}
52393b3a8eb9SGleb Smirnoff 	if (r->dst.port_op &&
52403b3a8eb9SGleb Smirnoff 	    r->dst.port_op != PF_OP_EQ && r->dst.port_op != PF_OP_RRG) {
52413b3a8eb9SGleb Smirnoff 		yyerror("invalid port operator for rdr destination port");
52423b3a8eb9SGleb Smirnoff 		problems++;
52433b3a8eb9SGleb Smirnoff 	}
52443b3a8eb9SGleb Smirnoff 	return (-problems);
52453b3a8eb9SGleb Smirnoff }
52463b3a8eb9SGleb Smirnoff 
52473b3a8eb9SGleb Smirnoff int
52483b3a8eb9SGleb Smirnoff process_tabledef(char *name, struct table_opts *opts)
52493b3a8eb9SGleb Smirnoff {
52503b3a8eb9SGleb Smirnoff 	struct pfr_buffer	 ab;
52513b3a8eb9SGleb Smirnoff 	struct node_tinit	*ti;
5252542feeffSKristof Provost 	unsigned long		 maxcount;
5253542feeffSKristof Provost 	size_t			 s = sizeof(maxcount);
52543b3a8eb9SGleb Smirnoff 
52553b3a8eb9SGleb Smirnoff 	bzero(&ab, sizeof(ab));
52563b3a8eb9SGleb Smirnoff 	ab.pfrb_type = PFRB_ADDRS;
52573b3a8eb9SGleb Smirnoff 	SIMPLEQ_FOREACH(ti, &opts->init_nodes, entries) {
52583b3a8eb9SGleb Smirnoff 		if (ti->file)
52593b3a8eb9SGleb Smirnoff 			if (pfr_buf_load(&ab, ti->file, 0, append_addr)) {
52603b3a8eb9SGleb Smirnoff 				if (errno)
52613b3a8eb9SGleb Smirnoff 					yyerror("cannot load \"%s\": %s",
52623b3a8eb9SGleb Smirnoff 					    ti->file, strerror(errno));
52633b3a8eb9SGleb Smirnoff 				else
52643b3a8eb9SGleb Smirnoff 					yyerror("file \"%s\" contains bad data",
52653b3a8eb9SGleb Smirnoff 					    ti->file);
52663b3a8eb9SGleb Smirnoff 				goto _error;
52673b3a8eb9SGleb Smirnoff 			}
52683b3a8eb9SGleb Smirnoff 		if (ti->host)
52693b3a8eb9SGleb Smirnoff 			if (append_addr_host(&ab, ti->host, 0, 0)) {
52703b3a8eb9SGleb Smirnoff 				yyerror("cannot create address buffer: %s",
52713b3a8eb9SGleb Smirnoff 				    strerror(errno));
52723b3a8eb9SGleb Smirnoff 				goto _error;
52733b3a8eb9SGleb Smirnoff 			}
52743b3a8eb9SGleb Smirnoff 	}
52753b3a8eb9SGleb Smirnoff 	if (pf->opts & PF_OPT_VERBOSE)
52763b3a8eb9SGleb Smirnoff 		print_tabledef(name, opts->flags, opts->init_addr,
52773b3a8eb9SGleb Smirnoff 		    &opts->init_nodes);
52783b3a8eb9SGleb Smirnoff 	if (!(pf->opts & PF_OPT_NOACTION) &&
52793b3a8eb9SGleb Smirnoff 	    pfctl_define_table(name, opts->flags, opts->init_addr,
52803b3a8eb9SGleb Smirnoff 	    pf->anchor->name, &ab, pf->anchor->ruleset.tticket)) {
5281542feeffSKristof Provost 
5282542feeffSKristof Provost 		if (sysctlbyname("net.pf.request_maxcount", &maxcount, &s,
5283542feeffSKristof Provost 		    NULL, 0) == -1)
5284542feeffSKristof Provost 			maxcount = 65535;
5285542feeffSKristof Provost 
5286542feeffSKristof Provost 		if (ab.pfrb_size > maxcount)
5287542feeffSKristof Provost 			yyerror("cannot define table %s: too many elements.\n"
5288542feeffSKristof Provost 			    "Consider increasing net.pf.request_maxcount.",
5289542feeffSKristof Provost 			    name);
5290542feeffSKristof Provost 		else
52913b3a8eb9SGleb Smirnoff 			yyerror("cannot define table %s: %s", name,
52923b3a8eb9SGleb Smirnoff 			    pfr_strerror(errno));
5293542feeffSKristof Provost 
52943b3a8eb9SGleb Smirnoff 		goto _error;
52953b3a8eb9SGleb Smirnoff 	}
52963b3a8eb9SGleb Smirnoff 	pf->tdirty = 1;
52973b3a8eb9SGleb Smirnoff 	pfr_buf_clear(&ab);
52983b3a8eb9SGleb Smirnoff 	return (0);
52993b3a8eb9SGleb Smirnoff _error:
53003b3a8eb9SGleb Smirnoff 	pfr_buf_clear(&ab);
53013b3a8eb9SGleb Smirnoff 	return (-1);
53023b3a8eb9SGleb Smirnoff }
53033b3a8eb9SGleb Smirnoff 
53043b3a8eb9SGleb Smirnoff struct keywords {
53053b3a8eb9SGleb Smirnoff 	const char	*k_name;
53063b3a8eb9SGleb Smirnoff 	int		 k_val;
53073b3a8eb9SGleb Smirnoff };
53083b3a8eb9SGleb Smirnoff 
53093b3a8eb9SGleb Smirnoff /* macro gore, but you should've seen the prior indentation nightmare... */
53103b3a8eb9SGleb Smirnoff 
53113b3a8eb9SGleb Smirnoff #define FREE_LIST(T,r) \
53123b3a8eb9SGleb Smirnoff 	do { \
53133b3a8eb9SGleb Smirnoff 		T *p, *node = r; \
53143b3a8eb9SGleb Smirnoff 		while (node != NULL) { \
53153b3a8eb9SGleb Smirnoff 			p = node; \
53163b3a8eb9SGleb Smirnoff 			node = node->next; \
53173b3a8eb9SGleb Smirnoff 			free(p); \
53183b3a8eb9SGleb Smirnoff 		} \
53193b3a8eb9SGleb Smirnoff 	} while (0)
53203b3a8eb9SGleb Smirnoff 
53213b3a8eb9SGleb Smirnoff #define LOOP_THROUGH(T,n,r,C) \
53223b3a8eb9SGleb Smirnoff 	do { \
53233b3a8eb9SGleb Smirnoff 		T *n; \
53243b3a8eb9SGleb Smirnoff 		if (r == NULL) { \
53253b3a8eb9SGleb Smirnoff 			r = calloc(1, sizeof(T)); \
53263b3a8eb9SGleb Smirnoff 			if (r == NULL) \
53273b3a8eb9SGleb Smirnoff 				err(1, "LOOP: calloc"); \
53283b3a8eb9SGleb Smirnoff 			r->next = NULL; \
53293b3a8eb9SGleb Smirnoff 		} \
53303b3a8eb9SGleb Smirnoff 		n = r; \
53313b3a8eb9SGleb Smirnoff 		while (n != NULL) { \
53323b3a8eb9SGleb Smirnoff 			do { \
53333b3a8eb9SGleb Smirnoff 				C; \
53343b3a8eb9SGleb Smirnoff 			} while (0); \
53353b3a8eb9SGleb Smirnoff 			n = n->next; \
53363b3a8eb9SGleb Smirnoff 		} \
53373b3a8eb9SGleb Smirnoff 	} while (0)
53383b3a8eb9SGleb Smirnoff 
53393b3a8eb9SGleb Smirnoff void
53403b3a8eb9SGleb Smirnoff expand_label_str(char *label, size_t len, const char *srch, const char *repl)
53413b3a8eb9SGleb Smirnoff {
53423b3a8eb9SGleb Smirnoff 	char *tmp;
53433b3a8eb9SGleb Smirnoff 	char *p, *q;
53443b3a8eb9SGleb Smirnoff 
53453b3a8eb9SGleb Smirnoff 	if ((tmp = calloc(1, len)) == NULL)
53463b3a8eb9SGleb Smirnoff 		err(1, "expand_label_str: calloc");
53473b3a8eb9SGleb Smirnoff 	p = q = label;
53483b3a8eb9SGleb Smirnoff 	while ((q = strstr(p, srch)) != NULL) {
53493b3a8eb9SGleb Smirnoff 		*q = '\0';
53503b3a8eb9SGleb Smirnoff 		if ((strlcat(tmp, p, len) >= len) ||
53513b3a8eb9SGleb Smirnoff 		    (strlcat(tmp, repl, len) >= len))
53523b3a8eb9SGleb Smirnoff 			errx(1, "expand_label: label too long");
53533b3a8eb9SGleb Smirnoff 		q += strlen(srch);
53543b3a8eb9SGleb Smirnoff 		p = q;
53553b3a8eb9SGleb Smirnoff 	}
53563b3a8eb9SGleb Smirnoff 	if (strlcat(tmp, p, len) >= len)
53573b3a8eb9SGleb Smirnoff 		errx(1, "expand_label: label too long");
53583b3a8eb9SGleb Smirnoff 	strlcpy(label, tmp, len);	/* always fits */
53593b3a8eb9SGleb Smirnoff 	free(tmp);
53603b3a8eb9SGleb Smirnoff }
53613b3a8eb9SGleb Smirnoff 
53623b3a8eb9SGleb Smirnoff void
53633b3a8eb9SGleb Smirnoff expand_label_if(const char *name, char *label, size_t len, const char *ifname)
53643b3a8eb9SGleb Smirnoff {
53653b3a8eb9SGleb Smirnoff 	if (strstr(label, name) != NULL) {
53663b3a8eb9SGleb Smirnoff 		if (!*ifname)
53673b3a8eb9SGleb Smirnoff 			expand_label_str(label, len, name, "any");
53683b3a8eb9SGleb Smirnoff 		else
53693b3a8eb9SGleb Smirnoff 			expand_label_str(label, len, name, ifname);
53703b3a8eb9SGleb Smirnoff 	}
53713b3a8eb9SGleb Smirnoff }
53723b3a8eb9SGleb Smirnoff 
53733b3a8eb9SGleb Smirnoff void
53743b3a8eb9SGleb Smirnoff expand_label_addr(const char *name, char *label, size_t len, sa_family_t af,
537509c7f238SKristof Provost     struct pf_rule_addr *addr)
53763b3a8eb9SGleb Smirnoff {
53773b3a8eb9SGleb Smirnoff 	char tmp[64], tmp_not[66];
53783b3a8eb9SGleb Smirnoff 
53793b3a8eb9SGleb Smirnoff 	if (strstr(label, name) != NULL) {
538009c7f238SKristof Provost 		switch (addr->addr.type) {
53813b3a8eb9SGleb Smirnoff 		case PF_ADDR_DYNIFTL:
538209c7f238SKristof Provost 			snprintf(tmp, sizeof(tmp), "(%s)", addr->addr.v.ifname);
53833b3a8eb9SGleb Smirnoff 			break;
53843b3a8eb9SGleb Smirnoff 		case PF_ADDR_TABLE:
538509c7f238SKristof Provost 			snprintf(tmp, sizeof(tmp), "<%s>", addr->addr.v.tblname);
53863b3a8eb9SGleb Smirnoff 			break;
53873b3a8eb9SGleb Smirnoff 		case PF_ADDR_NOROUTE:
53883b3a8eb9SGleb Smirnoff 			snprintf(tmp, sizeof(tmp), "no-route");
53893b3a8eb9SGleb Smirnoff 			break;
53903b3a8eb9SGleb Smirnoff 		case PF_ADDR_URPFFAILED:
53913b3a8eb9SGleb Smirnoff 			snprintf(tmp, sizeof(tmp), "urpf-failed");
53923b3a8eb9SGleb Smirnoff 			break;
53933b3a8eb9SGleb Smirnoff 		case PF_ADDR_ADDRMASK:
539409c7f238SKristof Provost 			if (!af || (PF_AZERO(&addr->addr.v.a.addr, af) &&
539509c7f238SKristof Provost 			    PF_AZERO(&addr->addr.v.a.mask, af)))
53963b3a8eb9SGleb Smirnoff 				snprintf(tmp, sizeof(tmp), "any");
53973b3a8eb9SGleb Smirnoff 			else {
53983b3a8eb9SGleb Smirnoff 				char	a[48];
53993b3a8eb9SGleb Smirnoff 				int	bits;
54003b3a8eb9SGleb Smirnoff 
540109c7f238SKristof Provost 				if (inet_ntop(af, &addr->addr.v.a.addr, a,
54023b3a8eb9SGleb Smirnoff 				    sizeof(a)) == NULL)
54033b3a8eb9SGleb Smirnoff 					snprintf(tmp, sizeof(tmp), "?");
54043b3a8eb9SGleb Smirnoff 				else {
540509c7f238SKristof Provost 					bits = unmask(&addr->addr.v.a.mask, af);
54063b3a8eb9SGleb Smirnoff 					if ((af == AF_INET && bits < 32) ||
54073b3a8eb9SGleb Smirnoff 					    (af == AF_INET6 && bits < 128))
54083b3a8eb9SGleb Smirnoff 						snprintf(tmp, sizeof(tmp),
54093b3a8eb9SGleb Smirnoff 						    "%s/%d", a, bits);
54103b3a8eb9SGleb Smirnoff 					else
54113b3a8eb9SGleb Smirnoff 						snprintf(tmp, sizeof(tmp),
54123b3a8eb9SGleb Smirnoff 						    "%s", a);
54133b3a8eb9SGleb Smirnoff 				}
54143b3a8eb9SGleb Smirnoff 			}
54153b3a8eb9SGleb Smirnoff 			break;
54163b3a8eb9SGleb Smirnoff 		default:
54173b3a8eb9SGleb Smirnoff 			snprintf(tmp, sizeof(tmp), "?");
54183b3a8eb9SGleb Smirnoff 			break;
54193b3a8eb9SGleb Smirnoff 		}
54203b3a8eb9SGleb Smirnoff 
542109c7f238SKristof Provost 		if (addr->neg) {
54223b3a8eb9SGleb Smirnoff 			snprintf(tmp_not, sizeof(tmp_not), "! %s", tmp);
54233b3a8eb9SGleb Smirnoff 			expand_label_str(label, len, name, tmp_not);
54243b3a8eb9SGleb Smirnoff 		} else
54253b3a8eb9SGleb Smirnoff 			expand_label_str(label, len, name, tmp);
54263b3a8eb9SGleb Smirnoff 	}
54273b3a8eb9SGleb Smirnoff }
54283b3a8eb9SGleb Smirnoff 
54293b3a8eb9SGleb Smirnoff void
54303b3a8eb9SGleb Smirnoff expand_label_port(const char *name, char *label, size_t len,
543109c7f238SKristof Provost     struct pf_rule_addr *addr)
54323b3a8eb9SGleb Smirnoff {
54333b3a8eb9SGleb Smirnoff 	char	 a1[6], a2[6], op[13] = "";
54343b3a8eb9SGleb Smirnoff 
54353b3a8eb9SGleb Smirnoff 	if (strstr(label, name) != NULL) {
543609c7f238SKristof Provost 		snprintf(a1, sizeof(a1), "%u", ntohs(addr->port[0]));
543709c7f238SKristof Provost 		snprintf(a2, sizeof(a2), "%u", ntohs(addr->port[1]));
543809c7f238SKristof Provost 		if (!addr->port_op)
54393b3a8eb9SGleb Smirnoff 			;
544009c7f238SKristof Provost 		else if (addr->port_op == PF_OP_IRG)
54413b3a8eb9SGleb Smirnoff 			snprintf(op, sizeof(op), "%s><%s", a1, a2);
544209c7f238SKristof Provost 		else if (addr->port_op == PF_OP_XRG)
54433b3a8eb9SGleb Smirnoff 			snprintf(op, sizeof(op), "%s<>%s", a1, a2);
544409c7f238SKristof Provost 		else if (addr->port_op == PF_OP_EQ)
54453b3a8eb9SGleb Smirnoff 			snprintf(op, sizeof(op), "%s", a1);
544609c7f238SKristof Provost 		else if (addr->port_op == PF_OP_NE)
54473b3a8eb9SGleb Smirnoff 			snprintf(op, sizeof(op), "!=%s", a1);
544809c7f238SKristof Provost 		else if (addr->port_op == PF_OP_LT)
54493b3a8eb9SGleb Smirnoff 			snprintf(op, sizeof(op), "<%s", a1);
545009c7f238SKristof Provost 		else if (addr->port_op == PF_OP_LE)
54513b3a8eb9SGleb Smirnoff 			snprintf(op, sizeof(op), "<=%s", a1);
545209c7f238SKristof Provost 		else if (addr->port_op == PF_OP_GT)
54533b3a8eb9SGleb Smirnoff 			snprintf(op, sizeof(op), ">%s", a1);
545409c7f238SKristof Provost 		else if (addr->port_op == PF_OP_GE)
54553b3a8eb9SGleb Smirnoff 			snprintf(op, sizeof(op), ">=%s", a1);
54563b3a8eb9SGleb Smirnoff 		expand_label_str(label, len, name, op);
54573b3a8eb9SGleb Smirnoff 	}
54583b3a8eb9SGleb Smirnoff }
54593b3a8eb9SGleb Smirnoff 
54603b3a8eb9SGleb Smirnoff void
54613b3a8eb9SGleb Smirnoff expand_label_proto(const char *name, char *label, size_t len, u_int8_t proto)
54623b3a8eb9SGleb Smirnoff {
5463858937beSMateusz Guzik 	const char *protoname;
54643b3a8eb9SGleb Smirnoff 	char n[4];
54653b3a8eb9SGleb Smirnoff 
54663b3a8eb9SGleb Smirnoff 	if (strstr(label, name) != NULL) {
5467858937beSMateusz Guzik 		protoname = pfctl_proto2name(proto);
5468858937beSMateusz Guzik 		if (protoname != NULL)
5469858937beSMateusz Guzik 			expand_label_str(label, len, name, protoname);
54703b3a8eb9SGleb Smirnoff 		else {
54713b3a8eb9SGleb Smirnoff 			snprintf(n, sizeof(n), "%u", proto);
54723b3a8eb9SGleb Smirnoff 			expand_label_str(label, len, name, n);
54733b3a8eb9SGleb Smirnoff 		}
54743b3a8eb9SGleb Smirnoff 	}
54753b3a8eb9SGleb Smirnoff }
54763b3a8eb9SGleb Smirnoff 
54773b3a8eb9SGleb Smirnoff void
547809c7f238SKristof Provost expand_label_nr(const char *name, char *label, size_t len,
547909c7f238SKristof Provost     struct pfctl_rule *r)
54803b3a8eb9SGleb Smirnoff {
54813b3a8eb9SGleb Smirnoff 	char n[11];
54823b3a8eb9SGleb Smirnoff 
54833b3a8eb9SGleb Smirnoff 	if (strstr(label, name) != NULL) {
548409c7f238SKristof Provost 		snprintf(n, sizeof(n), "%u", r->nr);
54853b3a8eb9SGleb Smirnoff 		expand_label_str(label, len, name, n);
54863b3a8eb9SGleb Smirnoff 	}
54873b3a8eb9SGleb Smirnoff }
54883b3a8eb9SGleb Smirnoff 
54893b3a8eb9SGleb Smirnoff void
549009c7f238SKristof Provost expand_label(char *label, size_t len, struct pfctl_rule *r)
54913b3a8eb9SGleb Smirnoff {
549209c7f238SKristof Provost 	expand_label_if("$if", label, len, r->ifname);
549309c7f238SKristof Provost 	expand_label_addr("$srcaddr", label, len, r->af, &r->src);
549409c7f238SKristof Provost 	expand_label_addr("$dstaddr", label, len, r->af, &r->dst);
549509c7f238SKristof Provost 	expand_label_port("$srcport", label, len, &r->src);
549609c7f238SKristof Provost 	expand_label_port("$dstport", label, len, &r->dst);
549709c7f238SKristof Provost 	expand_label_proto("$proto", label, len, r->proto);
549809c7f238SKristof Provost 	expand_label_nr("$nr", label, len, r);
54993b3a8eb9SGleb Smirnoff }
55003b3a8eb9SGleb Smirnoff 
55013b3a8eb9SGleb Smirnoff int
55023b3a8eb9SGleb Smirnoff expand_altq(struct pf_altq *a, struct node_if *interfaces,
55033b3a8eb9SGleb Smirnoff     struct node_queue *nqueues, struct node_queue_bw bwspec,
55043b3a8eb9SGleb Smirnoff     struct node_queue_opt *opts)
55053b3a8eb9SGleb Smirnoff {
55063b3a8eb9SGleb Smirnoff 	struct pf_altq		 pa, pb;
55073b3a8eb9SGleb Smirnoff 	char			 qname[PF_QNAME_SIZE];
55083b3a8eb9SGleb Smirnoff 	struct node_queue	*n;
55093b3a8eb9SGleb Smirnoff 	struct node_queue_bw	 bw;
55103b3a8eb9SGleb Smirnoff 	int			 errs = 0;
55113b3a8eb9SGleb Smirnoff 
55123b3a8eb9SGleb Smirnoff 	if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
55133b3a8eb9SGleb Smirnoff 		FREE_LIST(struct node_if, interfaces);
55140a70aaf8SLuiz Otavio O Souza 		if (nqueues)
55153b3a8eb9SGleb Smirnoff 			FREE_LIST(struct node_queue, nqueues);
55163b3a8eb9SGleb Smirnoff 		return (0);
55173b3a8eb9SGleb Smirnoff 	}
55183b3a8eb9SGleb Smirnoff 
55193b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_if, interface, interfaces,
55203b3a8eb9SGleb Smirnoff 		memcpy(&pa, a, sizeof(struct pf_altq));
55213b3a8eb9SGleb Smirnoff 		if (strlcpy(pa.ifname, interface->ifname,
55223b3a8eb9SGleb Smirnoff 		    sizeof(pa.ifname)) >= sizeof(pa.ifname))
55233b3a8eb9SGleb Smirnoff 			errx(1, "expand_altq: strlcpy");
55243b3a8eb9SGleb Smirnoff 
55253b3a8eb9SGleb Smirnoff 		if (interface->not) {
55263b3a8eb9SGleb Smirnoff 			yyerror("altq on ! <interface> is not supported");
55273b3a8eb9SGleb Smirnoff 			errs++;
55283b3a8eb9SGleb Smirnoff 		} else {
55293b3a8eb9SGleb Smirnoff 			if (eval_pfaltq(pf, &pa, &bwspec, opts))
55303b3a8eb9SGleb Smirnoff 				errs++;
55313b3a8eb9SGleb Smirnoff 			else
55323b3a8eb9SGleb Smirnoff 				if (pfctl_add_altq(pf, &pa))
55333b3a8eb9SGleb Smirnoff 					errs++;
55343b3a8eb9SGleb Smirnoff 
55353b3a8eb9SGleb Smirnoff 			if (pf->opts & PF_OPT_VERBOSE) {
55363b3a8eb9SGleb Smirnoff 				print_altq(&pf->paltq->altq, 0,
55373b3a8eb9SGleb Smirnoff 				    &bwspec, opts);
55383b3a8eb9SGleb Smirnoff 				if (nqueues && nqueues->tail) {
55393b3a8eb9SGleb Smirnoff 					printf("queue { ");
55403b3a8eb9SGleb Smirnoff 					LOOP_THROUGH(struct node_queue, queue,
55413b3a8eb9SGleb Smirnoff 					    nqueues,
55423b3a8eb9SGleb Smirnoff 						printf("%s ",
55433b3a8eb9SGleb Smirnoff 						    queue->queue);
55443b3a8eb9SGleb Smirnoff 					);
55453b3a8eb9SGleb Smirnoff 					printf("}");
55463b3a8eb9SGleb Smirnoff 				}
55473b3a8eb9SGleb Smirnoff 				printf("\n");
55483b3a8eb9SGleb Smirnoff 			}
55493b3a8eb9SGleb Smirnoff 
55503b3a8eb9SGleb Smirnoff 			if (pa.scheduler == ALTQT_CBQ ||
5551dc784287SKristof Provost 			    pa.scheduler == ALTQT_HFSC ||
5552dc784287SKristof Provost 			    pa.scheduler == ALTQT_FAIRQ) {
55533b3a8eb9SGleb Smirnoff 				/* now create a root queue */
55543b3a8eb9SGleb Smirnoff 				memset(&pb, 0, sizeof(struct pf_altq));
55553b3a8eb9SGleb Smirnoff 				if (strlcpy(qname, "root_", sizeof(qname)) >=
55563b3a8eb9SGleb Smirnoff 				    sizeof(qname))
55573b3a8eb9SGleb Smirnoff 					errx(1, "expand_altq: strlcpy");
55583b3a8eb9SGleb Smirnoff 				if (strlcat(qname, interface->ifname,
55593b3a8eb9SGleb Smirnoff 				    sizeof(qname)) >= sizeof(qname))
55603b3a8eb9SGleb Smirnoff 					errx(1, "expand_altq: strlcat");
55613b3a8eb9SGleb Smirnoff 				if (strlcpy(pb.qname, qname,
55623b3a8eb9SGleb Smirnoff 				    sizeof(pb.qname)) >= sizeof(pb.qname))
55633b3a8eb9SGleb Smirnoff 					errx(1, "expand_altq: strlcpy");
55643b3a8eb9SGleb Smirnoff 				if (strlcpy(pb.ifname, interface->ifname,
55653b3a8eb9SGleb Smirnoff 				    sizeof(pb.ifname)) >= sizeof(pb.ifname))
55663b3a8eb9SGleb Smirnoff 					errx(1, "expand_altq: strlcpy");
55673b3a8eb9SGleb Smirnoff 				pb.qlimit = pa.qlimit;
55683b3a8eb9SGleb Smirnoff 				pb.scheduler = pa.scheduler;
55693b3a8eb9SGleb Smirnoff 				bw.bw_absolute = pa.ifbandwidth;
55703b3a8eb9SGleb Smirnoff 				bw.bw_percent = 0;
55713b3a8eb9SGleb Smirnoff 				if (eval_pfqueue(pf, &pb, &bw, opts))
55723b3a8eb9SGleb Smirnoff 					errs++;
55733b3a8eb9SGleb Smirnoff 				else
55743b3a8eb9SGleb Smirnoff 					if (pfctl_add_altq(pf, &pb))
55753b3a8eb9SGleb Smirnoff 						errs++;
55763b3a8eb9SGleb Smirnoff 			}
55773b3a8eb9SGleb Smirnoff 
55783b3a8eb9SGleb Smirnoff 			LOOP_THROUGH(struct node_queue, queue, nqueues,
55793b3a8eb9SGleb Smirnoff 				n = calloc(1, sizeof(struct node_queue));
55803b3a8eb9SGleb Smirnoff 				if (n == NULL)
55813b3a8eb9SGleb Smirnoff 					err(1, "expand_altq: calloc");
55823b3a8eb9SGleb Smirnoff 				if (pa.scheduler == ALTQT_CBQ ||
5583dc784287SKristof Provost 				    pa.scheduler == ALTQT_HFSC ||
5584dc784287SKristof Provost 				    pa.scheduler == ALTQT_FAIRQ)
55853b3a8eb9SGleb Smirnoff 					if (strlcpy(n->parent, qname,
55863b3a8eb9SGleb Smirnoff 					    sizeof(n->parent)) >=
55873b3a8eb9SGleb Smirnoff 					    sizeof(n->parent))
55883b3a8eb9SGleb Smirnoff 						errx(1, "expand_altq: strlcpy");
55893b3a8eb9SGleb Smirnoff 				if (strlcpy(n->queue, queue->queue,
55903b3a8eb9SGleb Smirnoff 				    sizeof(n->queue)) >= sizeof(n->queue))
55913b3a8eb9SGleb Smirnoff 					errx(1, "expand_altq: strlcpy");
55923b3a8eb9SGleb Smirnoff 				if (strlcpy(n->ifname, interface->ifname,
55933b3a8eb9SGleb Smirnoff 				    sizeof(n->ifname)) >= sizeof(n->ifname))
55943b3a8eb9SGleb Smirnoff 					errx(1, "expand_altq: strlcpy");
55953b3a8eb9SGleb Smirnoff 				n->scheduler = pa.scheduler;
55963b3a8eb9SGleb Smirnoff 				n->next = NULL;
55973b3a8eb9SGleb Smirnoff 				n->tail = n;
55983b3a8eb9SGleb Smirnoff 				if (queues == NULL)
55993b3a8eb9SGleb Smirnoff 					queues = n;
56003b3a8eb9SGleb Smirnoff 				else {
56013b3a8eb9SGleb Smirnoff 					queues->tail->next = n;
56023b3a8eb9SGleb Smirnoff 					queues->tail = n;
56033b3a8eb9SGleb Smirnoff 				}
56043b3a8eb9SGleb Smirnoff 			);
56053b3a8eb9SGleb Smirnoff 		}
56063b3a8eb9SGleb Smirnoff 	);
56073b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_if, interfaces);
56080a70aaf8SLuiz Otavio O Souza 	if (nqueues)
56093b3a8eb9SGleb Smirnoff 		FREE_LIST(struct node_queue, nqueues);
56103b3a8eb9SGleb Smirnoff 
56113b3a8eb9SGleb Smirnoff 	return (errs);
56123b3a8eb9SGleb Smirnoff }
56133b3a8eb9SGleb Smirnoff 
56143b3a8eb9SGleb Smirnoff int
56153b3a8eb9SGleb Smirnoff expand_queue(struct pf_altq *a, struct node_if *interfaces,
56163b3a8eb9SGleb Smirnoff     struct node_queue *nqueues, struct node_queue_bw bwspec,
56173b3a8eb9SGleb Smirnoff     struct node_queue_opt *opts)
56183b3a8eb9SGleb Smirnoff {
56193b3a8eb9SGleb Smirnoff 	struct node_queue	*n, *nq;
56203b3a8eb9SGleb Smirnoff 	struct pf_altq		 pa;
56213b3a8eb9SGleb Smirnoff 	u_int8_t		 found = 0;
56223b3a8eb9SGleb Smirnoff 	u_int8_t		 errs = 0;
56233b3a8eb9SGleb Smirnoff 
56243b3a8eb9SGleb Smirnoff 	if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
56253b3a8eb9SGleb Smirnoff 		FREE_LIST(struct node_queue, nqueues);
56263b3a8eb9SGleb Smirnoff 		return (0);
56273b3a8eb9SGleb Smirnoff 	}
56283b3a8eb9SGleb Smirnoff 
56293b3a8eb9SGleb Smirnoff 	if (queues == NULL) {
56303b3a8eb9SGleb Smirnoff 		yyerror("queue %s has no parent", a->qname);
56313b3a8eb9SGleb Smirnoff 		FREE_LIST(struct node_queue, nqueues);
56323b3a8eb9SGleb Smirnoff 		return (1);
56333b3a8eb9SGleb Smirnoff 	}
56343b3a8eb9SGleb Smirnoff 
56353b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_if, interface, interfaces,
56363b3a8eb9SGleb Smirnoff 		LOOP_THROUGH(struct node_queue, tqueue, queues,
56373b3a8eb9SGleb Smirnoff 			if (!strncmp(a->qname, tqueue->queue, PF_QNAME_SIZE) &&
56383b3a8eb9SGleb Smirnoff 			    (interface->ifname[0] == 0 ||
56393b3a8eb9SGleb Smirnoff 			    (!interface->not && !strncmp(interface->ifname,
56403b3a8eb9SGleb Smirnoff 			    tqueue->ifname, IFNAMSIZ)) ||
56413b3a8eb9SGleb Smirnoff 			    (interface->not && strncmp(interface->ifname,
56423b3a8eb9SGleb Smirnoff 			    tqueue->ifname, IFNAMSIZ)))) {
56433b3a8eb9SGleb Smirnoff 				/* found ourself in queues */
56443b3a8eb9SGleb Smirnoff 				found++;
56453b3a8eb9SGleb Smirnoff 
56463b3a8eb9SGleb Smirnoff 				memcpy(&pa, a, sizeof(struct pf_altq));
56473b3a8eb9SGleb Smirnoff 
56483b3a8eb9SGleb Smirnoff 				if (pa.scheduler != ALTQT_NONE &&
56493b3a8eb9SGleb Smirnoff 				    pa.scheduler != tqueue->scheduler) {
56503b3a8eb9SGleb Smirnoff 					yyerror("exactly one scheduler type "
56513b3a8eb9SGleb Smirnoff 					    "per interface allowed");
56523b3a8eb9SGleb Smirnoff 					return (1);
56533b3a8eb9SGleb Smirnoff 				}
56543b3a8eb9SGleb Smirnoff 				pa.scheduler = tqueue->scheduler;
56553b3a8eb9SGleb Smirnoff 
56563b3a8eb9SGleb Smirnoff 				/* scheduler dependent error checking */
56573b3a8eb9SGleb Smirnoff 				switch (pa.scheduler) {
56583b3a8eb9SGleb Smirnoff 				case ALTQT_PRIQ:
56593b3a8eb9SGleb Smirnoff 					if (nqueues != NULL) {
56603b3a8eb9SGleb Smirnoff 						yyerror("priq queues cannot "
56613b3a8eb9SGleb Smirnoff 						    "have child queues");
56623b3a8eb9SGleb Smirnoff 						return (1);
56633b3a8eb9SGleb Smirnoff 					}
56643b3a8eb9SGleb Smirnoff 					if (bwspec.bw_absolute > 0 ||
56653b3a8eb9SGleb Smirnoff 					    bwspec.bw_percent < 100) {
56663b3a8eb9SGleb Smirnoff 						yyerror("priq doesn't take "
56673b3a8eb9SGleb Smirnoff 						    "bandwidth");
56683b3a8eb9SGleb Smirnoff 						return (1);
56693b3a8eb9SGleb Smirnoff 					}
56703b3a8eb9SGleb Smirnoff 					break;
56713b3a8eb9SGleb Smirnoff 				default:
56723b3a8eb9SGleb Smirnoff 					break;
56733b3a8eb9SGleb Smirnoff 				}
56743b3a8eb9SGleb Smirnoff 
56753b3a8eb9SGleb Smirnoff 				if (strlcpy(pa.ifname, tqueue->ifname,
56763b3a8eb9SGleb Smirnoff 				    sizeof(pa.ifname)) >= sizeof(pa.ifname))
56773b3a8eb9SGleb Smirnoff 					errx(1, "expand_queue: strlcpy");
56783b3a8eb9SGleb Smirnoff 				if (strlcpy(pa.parent, tqueue->parent,
56793b3a8eb9SGleb Smirnoff 				    sizeof(pa.parent)) >= sizeof(pa.parent))
56803b3a8eb9SGleb Smirnoff 					errx(1, "expand_queue: strlcpy");
56813b3a8eb9SGleb Smirnoff 
56823b3a8eb9SGleb Smirnoff 				if (eval_pfqueue(pf, &pa, &bwspec, opts))
56833b3a8eb9SGleb Smirnoff 					errs++;
56843b3a8eb9SGleb Smirnoff 				else
56853b3a8eb9SGleb Smirnoff 					if (pfctl_add_altq(pf, &pa))
56863b3a8eb9SGleb Smirnoff 						errs++;
56873b3a8eb9SGleb Smirnoff 
56883b3a8eb9SGleb Smirnoff 				for (nq = nqueues; nq != NULL; nq = nq->next) {
56893b3a8eb9SGleb Smirnoff 					if (!strcmp(a->qname, nq->queue)) {
56903b3a8eb9SGleb Smirnoff 						yyerror("queue cannot have "
56913b3a8eb9SGleb Smirnoff 						    "itself as child");
56923b3a8eb9SGleb Smirnoff 						errs++;
56933b3a8eb9SGleb Smirnoff 						continue;
56943b3a8eb9SGleb Smirnoff 					}
56953b3a8eb9SGleb Smirnoff 					n = calloc(1,
56963b3a8eb9SGleb Smirnoff 					    sizeof(struct node_queue));
56973b3a8eb9SGleb Smirnoff 					if (n == NULL)
56983b3a8eb9SGleb Smirnoff 						err(1, "expand_queue: calloc");
56993b3a8eb9SGleb Smirnoff 					if (strlcpy(n->parent, a->qname,
57003b3a8eb9SGleb Smirnoff 					    sizeof(n->parent)) >=
57013b3a8eb9SGleb Smirnoff 					    sizeof(n->parent))
57023b3a8eb9SGleb Smirnoff 						errx(1, "expand_queue strlcpy");
57033b3a8eb9SGleb Smirnoff 					if (strlcpy(n->queue, nq->queue,
57043b3a8eb9SGleb Smirnoff 					    sizeof(n->queue)) >=
57053b3a8eb9SGleb Smirnoff 					    sizeof(n->queue))
57063b3a8eb9SGleb Smirnoff 						errx(1, "expand_queue strlcpy");
57073b3a8eb9SGleb Smirnoff 					if (strlcpy(n->ifname, tqueue->ifname,
57083b3a8eb9SGleb Smirnoff 					    sizeof(n->ifname)) >=
57093b3a8eb9SGleb Smirnoff 					    sizeof(n->ifname))
57103b3a8eb9SGleb Smirnoff 						errx(1, "expand_queue strlcpy");
57113b3a8eb9SGleb Smirnoff 					n->scheduler = tqueue->scheduler;
57123b3a8eb9SGleb Smirnoff 					n->next = NULL;
57133b3a8eb9SGleb Smirnoff 					n->tail = n;
57143b3a8eb9SGleb Smirnoff 					if (queues == NULL)
57153b3a8eb9SGleb Smirnoff 						queues = n;
57163b3a8eb9SGleb Smirnoff 					else {
57173b3a8eb9SGleb Smirnoff 						queues->tail->next = n;
57183b3a8eb9SGleb Smirnoff 						queues->tail = n;
57193b3a8eb9SGleb Smirnoff 					}
57203b3a8eb9SGleb Smirnoff 				}
57213b3a8eb9SGleb Smirnoff 				if ((pf->opts & PF_OPT_VERBOSE) && (
57223b3a8eb9SGleb Smirnoff 				    (found == 1 && interface->ifname[0] == 0) ||
57233b3a8eb9SGleb Smirnoff 				    (found > 0 && interface->ifname[0] != 0))) {
57243b3a8eb9SGleb Smirnoff 					print_queue(&pf->paltq->altq, 0,
57253b3a8eb9SGleb Smirnoff 					    &bwspec, interface->ifname[0] != 0,
57263b3a8eb9SGleb Smirnoff 					    opts);
57273b3a8eb9SGleb Smirnoff 					if (nqueues && nqueues->tail) {
57283b3a8eb9SGleb Smirnoff 						printf("{ ");
57293b3a8eb9SGleb Smirnoff 						LOOP_THROUGH(struct node_queue,
57303b3a8eb9SGleb Smirnoff 						    queue, nqueues,
57313b3a8eb9SGleb Smirnoff 							printf("%s ",
57323b3a8eb9SGleb Smirnoff 							    queue->queue);
57333b3a8eb9SGleb Smirnoff 						);
57343b3a8eb9SGleb Smirnoff 						printf("}");
57353b3a8eb9SGleb Smirnoff 					}
57363b3a8eb9SGleb Smirnoff 					printf("\n");
57373b3a8eb9SGleb Smirnoff 				}
57383b3a8eb9SGleb Smirnoff 			}
57393b3a8eb9SGleb Smirnoff 		);
57403b3a8eb9SGleb Smirnoff 	);
57413b3a8eb9SGleb Smirnoff 
57423b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_queue, nqueues);
57433b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_if, interfaces);
57443b3a8eb9SGleb Smirnoff 
57453b3a8eb9SGleb Smirnoff 	if (!found) {
57463b3a8eb9SGleb Smirnoff 		yyerror("queue %s has no parent", a->qname);
57473b3a8eb9SGleb Smirnoff 		errs++;
57483b3a8eb9SGleb Smirnoff 	}
57493b3a8eb9SGleb Smirnoff 
57503b3a8eb9SGleb Smirnoff 	if (errs)
57513b3a8eb9SGleb Smirnoff 		return (1);
57523b3a8eb9SGleb Smirnoff 	else
57533b3a8eb9SGleb Smirnoff 		return (0);
57543b3a8eb9SGleb Smirnoff }
57553b3a8eb9SGleb Smirnoff 
57568a42005dSKristof Provost static int
57578a42005dSKristof Provost pf_af_to_proto(sa_family_t af)
57588a42005dSKristof Provost {
57598a42005dSKristof Provost 	if (af == AF_INET)
57608a42005dSKristof Provost 		return (ETHERTYPE_IP);
57618a42005dSKristof Provost 	if (af == AF_INET6)
57628a42005dSKristof Provost 		return (ETHERTYPE_IPV6);
57638a42005dSKristof Provost 
57648a42005dSKristof Provost 	return (0);
57658a42005dSKristof Provost }
57668a42005dSKristof Provost 
57673b3a8eb9SGleb Smirnoff void
57682b29ceb8SKristof Provost expand_eth_rule(struct pfctl_eth_rule *r,
576987a89d6eSKristof Provost     struct node_if *interfaces, struct node_etherproto *protos,
57708a42005dSKristof Provost     struct node_mac *srcs, struct node_mac *dsts,
57718a42005dSKristof Provost     struct node_host *ipsrcs, struct node_host *ipdsts, const char *anchor_call)
57722b29ceb8SKristof Provost {
57732b29ceb8SKristof Provost 	LOOP_THROUGH(struct node_if, interface, interfaces,
57742b29ceb8SKristof Provost 	LOOP_THROUGH(struct node_etherproto, proto, protos,
577587a89d6eSKristof Provost 	LOOP_THROUGH(struct node_mac, src, srcs,
577687a89d6eSKristof Provost 	LOOP_THROUGH(struct node_mac, dst, dsts,
57778a42005dSKristof Provost 	LOOP_THROUGH(struct node_host, ipsrc, ipsrcs,
57788a42005dSKristof Provost 	LOOP_THROUGH(struct node_host, ipdst, ipdsts,
57792b29ceb8SKristof Provost 		strlcpy(r->ifname, interface->ifname,
57802b29ceb8SKristof Provost 		    sizeof(r->ifname));
57812b29ceb8SKristof Provost 		r->ifnot = interface->not;
57822b29ceb8SKristof Provost 		r->proto = proto->proto;
57838a42005dSKristof Provost 		if (!r->proto && ipsrc->af)
57848a42005dSKristof Provost 			r->proto = pf_af_to_proto(ipsrc->af);
57858a42005dSKristof Provost 		else if (!r->proto && ipdst->af)
57868a42005dSKristof Provost 			r->proto = pf_af_to_proto(ipdst->af);
578787a89d6eSKristof Provost 		bcopy(src->mac, r->src.addr, ETHER_ADDR_LEN);
5788b590f17aSKristof Provost 		bcopy(src->mask, r->src.mask, ETHER_ADDR_LEN);
578987a89d6eSKristof Provost 		r->src.neg = src->neg;
5790c32cd180SKristof Provost 		r->src.isset = src->isset;
57918a42005dSKristof Provost 		r->ipsrc.addr = ipsrc->addr;
57928a42005dSKristof Provost 		r->ipsrc.neg = ipsrc->not;
57938a42005dSKristof Provost 		r->ipdst.addr = ipdst->addr;
57948a42005dSKristof Provost 		r->ipdst.neg = ipdst->not;
579587a89d6eSKristof Provost 		bcopy(dst->mac, r->dst.addr, ETHER_ADDR_LEN);
5796b590f17aSKristof Provost 		bcopy(dst->mask, r->dst.mask, ETHER_ADDR_LEN);
579787a89d6eSKristof Provost 		r->dst.neg = dst->neg;
5798c32cd180SKristof Provost 		r->dst.isset = dst->isset;
5799c5131afeSKristof Provost 		r->nr = pf->eastack[pf->asd]->match++;
58002b29ceb8SKristof Provost 
5801c5131afeSKristof Provost 		pfctl_append_eth_rule(pf, r, anchor_call);
58028a42005dSKristof Provost 	))))));
58032b29ceb8SKristof Provost 
58042b29ceb8SKristof Provost 	FREE_LIST(struct node_if, interfaces);
58052b29ceb8SKristof Provost 	FREE_LIST(struct node_etherproto, protos);
580687a89d6eSKristof Provost 	FREE_LIST(struct node_mac, srcs);
580787a89d6eSKristof Provost 	FREE_LIST(struct node_mac, dsts);
58088a42005dSKristof Provost 	FREE_LIST(struct node_host, ipsrcs);
58098a42005dSKristof Provost 	FREE_LIST(struct node_host, ipdsts);
58102b29ceb8SKristof Provost }
58112b29ceb8SKristof Provost 
58122b29ceb8SKristof Provost void
5813e9eb0941SKristof Provost expand_rule(struct pfctl_rule *r,
58143b3a8eb9SGleb Smirnoff     struct node_if *interfaces, struct node_host *rpool_hosts,
58153b3a8eb9SGleb Smirnoff     struct node_proto *protos, struct node_os *src_oses,
58163b3a8eb9SGleb Smirnoff     struct node_host *src_hosts, struct node_port *src_ports,
58173b3a8eb9SGleb Smirnoff     struct node_host *dst_hosts, struct node_port *dst_ports,
58183b3a8eb9SGleb Smirnoff     struct node_uid *uids, struct node_gid *gids, struct node_icmp *icmp_types,
58193b3a8eb9SGleb Smirnoff     const char *anchor_call)
58203b3a8eb9SGleb Smirnoff {
58213b3a8eb9SGleb Smirnoff 	sa_family_t		 af = r->af;
58223b3a8eb9SGleb Smirnoff 	int			 added = 0, error = 0;
58233b3a8eb9SGleb Smirnoff 	char			 ifname[IF_NAMESIZE];
58246fcc8e04SKristof Provost 	char			 label[PF_RULE_MAX_LABEL_COUNT][PF_RULE_LABEL_SIZE];
58253b3a8eb9SGleb Smirnoff 	char			 tagname[PF_TAG_NAME_SIZE];
58263b3a8eb9SGleb Smirnoff 	char			 match_tagname[PF_TAG_NAME_SIZE];
58273b3a8eb9SGleb Smirnoff 	struct pf_pooladdr	*pa;
58283b3a8eb9SGleb Smirnoff 	struct node_host	*h;
58293b3a8eb9SGleb Smirnoff 	u_int8_t		 flags, flagset, keep_state;
58303b3a8eb9SGleb Smirnoff 
58316fcc8e04SKristof Provost 	memcpy(label, r->label, sizeof(r->label));
58326fcc8e04SKristof Provost 	assert(sizeof(r->label) == sizeof(label));
58333b3a8eb9SGleb Smirnoff 	if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname))
58343b3a8eb9SGleb Smirnoff 		errx(1, "expand_rule: strlcpy");
58353b3a8eb9SGleb Smirnoff 	if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >=
58363b3a8eb9SGleb Smirnoff 	    sizeof(match_tagname))
58373b3a8eb9SGleb Smirnoff 		errx(1, "expand_rule: strlcpy");
58383b3a8eb9SGleb Smirnoff 	flags = r->flags;
58393b3a8eb9SGleb Smirnoff 	flagset = r->flagset;
58403b3a8eb9SGleb Smirnoff 	keep_state = r->keep_state;
58413b3a8eb9SGleb Smirnoff 
58423b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_if, interface, interfaces,
58433b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_proto, proto, protos,
58443b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_icmp, icmp_type, icmp_types,
58453b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_host, src_host, src_hosts,
58463b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_port, src_port, src_ports,
58473b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_os, src_os, src_oses,
58483b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_host, dst_host, dst_hosts,
58493b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_port, dst_port, dst_ports,
58503b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_uid, uid, uids,
58513b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_gid, gid, gids,
58523b3a8eb9SGleb Smirnoff 
58533b3a8eb9SGleb Smirnoff 		r->af = af;
58543b3a8eb9SGleb Smirnoff 		/* for link-local IPv6 address, interface must match up */
58553b3a8eb9SGleb Smirnoff 		if ((r->af && src_host->af && r->af != src_host->af) ||
58563b3a8eb9SGleb Smirnoff 		    (r->af && dst_host->af && r->af != dst_host->af) ||
58573b3a8eb9SGleb Smirnoff 		    (src_host->af && dst_host->af &&
58583b3a8eb9SGleb Smirnoff 		    src_host->af != dst_host->af) ||
58593b3a8eb9SGleb Smirnoff 		    (src_host->ifindex && dst_host->ifindex &&
58603b3a8eb9SGleb Smirnoff 		    src_host->ifindex != dst_host->ifindex) ||
58613b3a8eb9SGleb Smirnoff 		    (src_host->ifindex && *interface->ifname &&
58623b3a8eb9SGleb Smirnoff 		    src_host->ifindex != if_nametoindex(interface->ifname)) ||
58633b3a8eb9SGleb Smirnoff 		    (dst_host->ifindex && *interface->ifname &&
58643b3a8eb9SGleb Smirnoff 		    dst_host->ifindex != if_nametoindex(interface->ifname)))
58653b3a8eb9SGleb Smirnoff 			continue;
58663b3a8eb9SGleb Smirnoff 		if (!r->af && src_host->af)
58673b3a8eb9SGleb Smirnoff 			r->af = src_host->af;
58683b3a8eb9SGleb Smirnoff 		else if (!r->af && dst_host->af)
58693b3a8eb9SGleb Smirnoff 			r->af = dst_host->af;
58703b3a8eb9SGleb Smirnoff 
58713b3a8eb9SGleb Smirnoff 		if (*interface->ifname)
58723b3a8eb9SGleb Smirnoff 			strlcpy(r->ifname, interface->ifname,
58733b3a8eb9SGleb Smirnoff 			    sizeof(r->ifname));
58743b3a8eb9SGleb Smirnoff 		else if (if_indextoname(src_host->ifindex, ifname))
58753b3a8eb9SGleb Smirnoff 			strlcpy(r->ifname, ifname, sizeof(r->ifname));
58763b3a8eb9SGleb Smirnoff 		else if (if_indextoname(dst_host->ifindex, ifname))
58773b3a8eb9SGleb Smirnoff 			strlcpy(r->ifname, ifname, sizeof(r->ifname));
58783b3a8eb9SGleb Smirnoff 		else
58793b3a8eb9SGleb Smirnoff 			memset(r->ifname, '\0', sizeof(r->ifname));
58803b3a8eb9SGleb Smirnoff 
58816fcc8e04SKristof Provost 		memcpy(r->label, label, sizeof(r->label));
58823b3a8eb9SGleb Smirnoff 		if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >=
58833b3a8eb9SGleb Smirnoff 		    sizeof(r->tagname))
58843b3a8eb9SGleb Smirnoff 			errx(1, "expand_rule: strlcpy");
58853b3a8eb9SGleb Smirnoff 		if (strlcpy(r->match_tagname, match_tagname,
58863b3a8eb9SGleb Smirnoff 		    sizeof(r->match_tagname)) >= sizeof(r->match_tagname))
58873b3a8eb9SGleb Smirnoff 			errx(1, "expand_rule: strlcpy");
58883b3a8eb9SGleb Smirnoff 
58893b3a8eb9SGleb Smirnoff 		error += check_netmask(src_host, r->af);
58903b3a8eb9SGleb Smirnoff 		error += check_netmask(dst_host, r->af);
58913b3a8eb9SGleb Smirnoff 
58923b3a8eb9SGleb Smirnoff 		r->ifnot = interface->not;
58933b3a8eb9SGleb Smirnoff 		r->proto = proto->proto;
58943b3a8eb9SGleb Smirnoff 		r->src.addr = src_host->addr;
58953b3a8eb9SGleb Smirnoff 		r->src.neg = src_host->not;
58963b3a8eb9SGleb Smirnoff 		r->src.port[0] = src_port->port[0];
58973b3a8eb9SGleb Smirnoff 		r->src.port[1] = src_port->port[1];
58983b3a8eb9SGleb Smirnoff 		r->src.port_op = src_port->op;
58993b3a8eb9SGleb Smirnoff 		r->dst.addr = dst_host->addr;
59003b3a8eb9SGleb Smirnoff 		r->dst.neg = dst_host->not;
59013b3a8eb9SGleb Smirnoff 		r->dst.port[0] = dst_port->port[0];
59023b3a8eb9SGleb Smirnoff 		r->dst.port[1] = dst_port->port[1];
59033b3a8eb9SGleb Smirnoff 		r->dst.port_op = dst_port->op;
59043b3a8eb9SGleb Smirnoff 		r->uid.op = uid->op;
59053b3a8eb9SGleb Smirnoff 		r->uid.uid[0] = uid->uid[0];
59063b3a8eb9SGleb Smirnoff 		r->uid.uid[1] = uid->uid[1];
59073b3a8eb9SGleb Smirnoff 		r->gid.op = gid->op;
59083b3a8eb9SGleb Smirnoff 		r->gid.gid[0] = gid->gid[0];
59093b3a8eb9SGleb Smirnoff 		r->gid.gid[1] = gid->gid[1];
59103b3a8eb9SGleb Smirnoff 		r->type = icmp_type->type;
59113b3a8eb9SGleb Smirnoff 		r->code = icmp_type->code;
59123b3a8eb9SGleb Smirnoff 
59133b3a8eb9SGleb Smirnoff 		if ((keep_state == PF_STATE_MODULATE ||
59143b3a8eb9SGleb Smirnoff 		    keep_state == PF_STATE_SYNPROXY) &&
59153b3a8eb9SGleb Smirnoff 		    r->proto && r->proto != IPPROTO_TCP)
59163b3a8eb9SGleb Smirnoff 			r->keep_state = PF_STATE_NORMAL;
59173b3a8eb9SGleb Smirnoff 		else
59183b3a8eb9SGleb Smirnoff 			r->keep_state = keep_state;
59193b3a8eb9SGleb Smirnoff 
59203b3a8eb9SGleb Smirnoff 		if (r->proto && r->proto != IPPROTO_TCP) {
59213b3a8eb9SGleb Smirnoff 			r->flags = 0;
59223b3a8eb9SGleb Smirnoff 			r->flagset = 0;
59233b3a8eb9SGleb Smirnoff 		} else {
59243b3a8eb9SGleb Smirnoff 			r->flags = flags;
59253b3a8eb9SGleb Smirnoff 			r->flagset = flagset;
59263b3a8eb9SGleb Smirnoff 		}
59273b3a8eb9SGleb Smirnoff 		if (icmp_type->proto && r->proto != icmp_type->proto) {
59283b3a8eb9SGleb Smirnoff 			yyerror("icmp-type mismatch");
59293b3a8eb9SGleb Smirnoff 			error++;
59303b3a8eb9SGleb Smirnoff 		}
59313b3a8eb9SGleb Smirnoff 
59323b3a8eb9SGleb Smirnoff 		if (src_os && src_os->os) {
59333b3a8eb9SGleb Smirnoff 			r->os_fingerprint = pfctl_get_fingerprint(src_os->os);
59343b3a8eb9SGleb Smirnoff 			if ((pf->opts & PF_OPT_VERBOSE2) &&
59353b3a8eb9SGleb Smirnoff 			    r->os_fingerprint == PF_OSFP_NOMATCH)
59363b3a8eb9SGleb Smirnoff 				fprintf(stderr,
59373b3a8eb9SGleb Smirnoff 				    "warning: unknown '%s' OS fingerprint\n",
59383b3a8eb9SGleb Smirnoff 				    src_os->os);
59393b3a8eb9SGleb Smirnoff 		} else {
59403b3a8eb9SGleb Smirnoff 			r->os_fingerprint = PF_OSFP_ANY;
59413b3a8eb9SGleb Smirnoff 		}
59423b3a8eb9SGleb Smirnoff 
59433b3a8eb9SGleb Smirnoff 		TAILQ_INIT(&r->rpool.list);
59443b3a8eb9SGleb Smirnoff 		for (h = rpool_hosts; h != NULL; h = h->next) {
59453b3a8eb9SGleb Smirnoff 			pa = calloc(1, sizeof(struct pf_pooladdr));
59463b3a8eb9SGleb Smirnoff 			if (pa == NULL)
59473b3a8eb9SGleb Smirnoff 				err(1, "expand_rule: calloc");
59483b3a8eb9SGleb Smirnoff 			pa->addr = h->addr;
59493b3a8eb9SGleb Smirnoff 			if (h->ifname != NULL) {
59503b3a8eb9SGleb Smirnoff 				if (strlcpy(pa->ifname, h->ifname,
59513b3a8eb9SGleb Smirnoff 				    sizeof(pa->ifname)) >=
59523b3a8eb9SGleb Smirnoff 				    sizeof(pa->ifname))
59533b3a8eb9SGleb Smirnoff 					errx(1, "expand_rule: strlcpy");
59543b3a8eb9SGleb Smirnoff 			} else
59553b3a8eb9SGleb Smirnoff 				pa->ifname[0] = 0;
59563b3a8eb9SGleb Smirnoff 			TAILQ_INSERT_TAIL(&r->rpool.list, pa, entries);
59573b3a8eb9SGleb Smirnoff 		}
59583b3a8eb9SGleb Smirnoff 
59593b3a8eb9SGleb Smirnoff 		if (rule_consistent(r, anchor_call[0]) < 0 || error)
59603b3a8eb9SGleb Smirnoff 			yyerror("skipping rule due to errors");
59613b3a8eb9SGleb Smirnoff 		else {
59623b3a8eb9SGleb Smirnoff 			r->nr = pf->astack[pf->asd]->match++;
59630d71f9f3SKristof Provost 			pfctl_append_rule(pf, r, anchor_call);
59643b3a8eb9SGleb Smirnoff 			added++;
59653b3a8eb9SGleb Smirnoff 		}
59663b3a8eb9SGleb Smirnoff 
59673b3a8eb9SGleb Smirnoff 	))))))))));
59683b3a8eb9SGleb Smirnoff 
59693b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_if, interfaces);
59703b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_proto, protos);
59713b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_host, src_hosts);
59723b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_port, src_ports);
59733b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_os, src_oses);
59743b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_host, dst_hosts);
59753b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_port, dst_ports);
59763b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_uid, uids);
59773b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_gid, gids);
59783b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_icmp, icmp_types);
59793b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_host, rpool_hosts);
59803b3a8eb9SGleb Smirnoff 
59813b3a8eb9SGleb Smirnoff 	if (!added)
59823b3a8eb9SGleb Smirnoff 		yyerror("rule expands to no valid combination");
59833b3a8eb9SGleb Smirnoff }
59843b3a8eb9SGleb Smirnoff 
59853b3a8eb9SGleb Smirnoff int
59863b3a8eb9SGleb Smirnoff expand_skip_interface(struct node_if *interfaces)
59873b3a8eb9SGleb Smirnoff {
59883b3a8eb9SGleb Smirnoff 	int	errs = 0;
59893b3a8eb9SGleb Smirnoff 
59903b3a8eb9SGleb Smirnoff 	if (!interfaces || (!interfaces->next && !interfaces->not &&
59913b3a8eb9SGleb Smirnoff 	    !strcmp(interfaces->ifname, "none"))) {
59923b3a8eb9SGleb Smirnoff 		if (pf->opts & PF_OPT_VERBOSE)
59933b3a8eb9SGleb Smirnoff 			printf("set skip on none\n");
59943b3a8eb9SGleb Smirnoff 		errs = pfctl_set_interface_flags(pf, "", PFI_IFLAG_SKIP, 0);
59953b3a8eb9SGleb Smirnoff 		return (errs);
59963b3a8eb9SGleb Smirnoff 	}
59973b3a8eb9SGleb Smirnoff 
59983b3a8eb9SGleb Smirnoff 	if (pf->opts & PF_OPT_VERBOSE)
59993b3a8eb9SGleb Smirnoff 		printf("set skip on {");
60003b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_if, interface, interfaces,
60013b3a8eb9SGleb Smirnoff 		if (pf->opts & PF_OPT_VERBOSE)
60023b3a8eb9SGleb Smirnoff 			printf(" %s", interface->ifname);
60033b3a8eb9SGleb Smirnoff 		if (interface->not) {
60043b3a8eb9SGleb Smirnoff 			yyerror("skip on ! <interface> is not supported");
60053b3a8eb9SGleb Smirnoff 			errs++;
60063b3a8eb9SGleb Smirnoff 		} else
60073b3a8eb9SGleb Smirnoff 			errs += pfctl_set_interface_flags(pf,
60083b3a8eb9SGleb Smirnoff 			    interface->ifname, PFI_IFLAG_SKIP, 1);
60093b3a8eb9SGleb Smirnoff 	);
60103b3a8eb9SGleb Smirnoff 	if (pf->opts & PF_OPT_VERBOSE)
60113b3a8eb9SGleb Smirnoff 		printf(" }\n");
60123b3a8eb9SGleb Smirnoff 
60133b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_if, interfaces);
60143b3a8eb9SGleb Smirnoff 
60153b3a8eb9SGleb Smirnoff 	if (errs)
60163b3a8eb9SGleb Smirnoff 		return (1);
60173b3a8eb9SGleb Smirnoff 	else
60183b3a8eb9SGleb Smirnoff 		return (0);
60193b3a8eb9SGleb Smirnoff }
60203b3a8eb9SGleb Smirnoff 
60213b3a8eb9SGleb Smirnoff #undef FREE_LIST
60223b3a8eb9SGleb Smirnoff #undef LOOP_THROUGH
60233b3a8eb9SGleb Smirnoff 
60243b3a8eb9SGleb Smirnoff int
60253b3a8eb9SGleb Smirnoff check_rulestate(int desired_state)
60263b3a8eb9SGleb Smirnoff {
60273b3a8eb9SGleb Smirnoff 	if (require_order && (rulestate > desired_state)) {
60282b29ceb8SKristof Provost 		yyerror("Rules must be in order: options, ethernet, "
60292b29ceb8SKristof Provost 		    "normalization, queueing, translation, filtering");
60303b3a8eb9SGleb Smirnoff 		return (1);
60313b3a8eb9SGleb Smirnoff 	}
60323b3a8eb9SGleb Smirnoff 	rulestate = desired_state;
60333b3a8eb9SGleb Smirnoff 	return (0);
60343b3a8eb9SGleb Smirnoff }
60353b3a8eb9SGleb Smirnoff 
60363b3a8eb9SGleb Smirnoff int
60373b3a8eb9SGleb Smirnoff kw_cmp(const void *k, const void *e)
60383b3a8eb9SGleb Smirnoff {
60393b3a8eb9SGleb Smirnoff 	return (strcmp(k, ((const struct keywords *)e)->k_name));
60403b3a8eb9SGleb Smirnoff }
60413b3a8eb9SGleb Smirnoff 
60423b3a8eb9SGleb Smirnoff int
60433b3a8eb9SGleb Smirnoff lookup(char *s)
60443b3a8eb9SGleb Smirnoff {
60453b3a8eb9SGleb Smirnoff 	/* this has to be sorted always */
60463b3a8eb9SGleb Smirnoff 	static const struct keywords keywords[] = {
60473b3a8eb9SGleb Smirnoff 		{ "all",		ALL},
60483b3a8eb9SGleb Smirnoff 		{ "allow-opts",		ALLOWOPTS},
60493b3a8eb9SGleb Smirnoff 		{ "altq",		ALTQ},
60503b3a8eb9SGleb Smirnoff 		{ "anchor",		ANCHOR},
60513b3a8eb9SGleb Smirnoff 		{ "antispoof",		ANTISPOOF},
60523b3a8eb9SGleb Smirnoff 		{ "any",		ANY},
60533b3a8eb9SGleb Smirnoff 		{ "bandwidth",		BANDWIDTH},
60543b3a8eb9SGleb Smirnoff 		{ "binat",		BINAT},
60553b3a8eb9SGleb Smirnoff 		{ "binat-anchor",	BINATANCHOR},
60563b3a8eb9SGleb Smirnoff 		{ "bitmask",		BITMASK},
60573b3a8eb9SGleb Smirnoff 		{ "block",		BLOCK},
60583b3a8eb9SGleb Smirnoff 		{ "block-policy",	BLOCKPOLICY},
6059a5b789f6SErmal Luçi 		{ "buckets",		BUCKETS},
60603b3a8eb9SGleb Smirnoff 		{ "cbq",		CBQ},
60613b3a8eb9SGleb Smirnoff 		{ "code",		CODE},
60620a70aaf8SLuiz Otavio O Souza 		{ "codelq",		CODEL},
60633b3a8eb9SGleb Smirnoff 		{ "crop",		FRAGCROP},
60643b3a8eb9SGleb Smirnoff 		{ "debug",		DEBUG},
60653b3a8eb9SGleb Smirnoff 		{ "divert-reply",	DIVERTREPLY},
60663b3a8eb9SGleb Smirnoff 		{ "divert-to",		DIVERTTO},
606763b3c1c7SKristof Provost 		{ "dnpipe",		DNPIPE},
606863b3c1c7SKristof Provost 		{ "dnqueue",		DNQUEUE},
60693b3a8eb9SGleb Smirnoff 		{ "drop",		DROP},
60703b3a8eb9SGleb Smirnoff 		{ "drop-ovl",		FRAGDROP},
60713b3a8eb9SGleb Smirnoff 		{ "dup-to",		DUPTO},
60722b29ceb8SKristof Provost 		{ "ether",		ETHER},
6073150182e3SKristof Provost 		{ "fail-policy",	FAILPOLICY},
6074a5b789f6SErmal Luçi 		{ "fairq",		FAIRQ},
60753b3a8eb9SGleb Smirnoff 		{ "fastroute",		FASTROUTE},
60763b3a8eb9SGleb Smirnoff 		{ "file",		FILENAME},
60773b3a8eb9SGleb Smirnoff 		{ "fingerprints",	FINGERPRINTS},
60783b3a8eb9SGleb Smirnoff 		{ "flags",		FLAGS},
60793b3a8eb9SGleb Smirnoff 		{ "floating",		FLOATING},
60803b3a8eb9SGleb Smirnoff 		{ "flush",		FLUSH},
60813b3a8eb9SGleb Smirnoff 		{ "for",		FOR},
60823b3a8eb9SGleb Smirnoff 		{ "fragment",		FRAGMENT},
60833b3a8eb9SGleb Smirnoff 		{ "from",		FROM},
60843b3a8eb9SGleb Smirnoff 		{ "global",		GLOBAL},
60853b3a8eb9SGleb Smirnoff 		{ "group",		GROUP},
60863b3a8eb9SGleb Smirnoff 		{ "hfsc",		HFSC},
6087a5b789f6SErmal Luçi 		{ "hogs",		HOGS},
60883b3a8eb9SGleb Smirnoff 		{ "hostid",		HOSTID},
60893b3a8eb9SGleb Smirnoff 		{ "icmp-type",		ICMPTYPE},
60903b3a8eb9SGleb Smirnoff 		{ "icmp6-type",		ICMP6TYPE},
60913b3a8eb9SGleb Smirnoff 		{ "if-bound",		IFBOUND},
60923b3a8eb9SGleb Smirnoff 		{ "in",			IN},
60933b3a8eb9SGleb Smirnoff 		{ "include",		INCLUDE},
60943b3a8eb9SGleb Smirnoff 		{ "inet",		INET},
60953b3a8eb9SGleb Smirnoff 		{ "inet6",		INET6},
60960a70aaf8SLuiz Otavio O Souza 		{ "interval",		INTERVAL},
60973b3a8eb9SGleb Smirnoff 		{ "keep",		KEEP},
609842ec75f8SKristof Provost 		{ "keepcounters",	KEEPCOUNTERS},
60998a42005dSKristof Provost 		{ "l3",			L3},
61003b3a8eb9SGleb Smirnoff 		{ "label",		LABEL},
61013b3a8eb9SGleb Smirnoff 		{ "limit",		LIMIT},
61023b3a8eb9SGleb Smirnoff 		{ "linkshare",		LINKSHARE},
61033b3a8eb9SGleb Smirnoff 		{ "load",		LOAD},
61043b3a8eb9SGleb Smirnoff 		{ "log",		LOG},
61053b3a8eb9SGleb Smirnoff 		{ "loginterface",	LOGINTERFACE},
61062aa21096SKurosawa Takahiro 		{ "map-e-portset",	MAPEPORTSET},
6107ef950daaSKristof Provost 		{ "match",		MATCH},
61083b3a8eb9SGleb Smirnoff 		{ "max",		MAXIMUM},
61093b3a8eb9SGleb Smirnoff 		{ "max-mss",		MAXMSS},
61103b3a8eb9SGleb Smirnoff 		{ "max-src-conn",	MAXSRCCONN},
61113b3a8eb9SGleb Smirnoff 		{ "max-src-conn-rate",	MAXSRCCONNRATE},
61123b3a8eb9SGleb Smirnoff 		{ "max-src-nodes",	MAXSRCNODES},
61133b3a8eb9SGleb Smirnoff 		{ "max-src-states",	MAXSRCSTATES},
61143b3a8eb9SGleb Smirnoff 		{ "min-ttl",		MINTTL},
61153b3a8eb9SGleb Smirnoff 		{ "modulate",		MODULATE},
61163b3a8eb9SGleb Smirnoff 		{ "nat",		NAT},
61173b3a8eb9SGleb Smirnoff 		{ "nat-anchor",		NATANCHOR},
61183b3a8eb9SGleb Smirnoff 		{ "no",			NO},
61193b3a8eb9SGleb Smirnoff 		{ "no-df",		NODF},
61203b3a8eb9SGleb Smirnoff 		{ "no-route",		NOROUTE},
61213b3a8eb9SGleb Smirnoff 		{ "no-sync",		NOSYNC},
61223b3a8eb9SGleb Smirnoff 		{ "on",			ON},
61233b3a8eb9SGleb Smirnoff 		{ "optimization",	OPTIMIZATION},
61243b3a8eb9SGleb Smirnoff 		{ "os",			OS},
61253b3a8eb9SGleb Smirnoff 		{ "out",		OUT},
61263b3a8eb9SGleb Smirnoff 		{ "overload",		OVERLOAD},
61273b3a8eb9SGleb Smirnoff 		{ "pass",		PASS},
61283b3a8eb9SGleb Smirnoff 		{ "port",		PORT},
61293e248e0fSKristof Provost 		{ "prio",		PRIO},
61303b3a8eb9SGleb Smirnoff 		{ "priority",		PRIORITY},
61313b3a8eb9SGleb Smirnoff 		{ "priq",		PRIQ},
61323b3a8eb9SGleb Smirnoff 		{ "probability",	PROBABILITY},
61333b3a8eb9SGleb Smirnoff 		{ "proto",		PROTO},
61343b3a8eb9SGleb Smirnoff 		{ "qlimit",		QLIMIT},
61353b3a8eb9SGleb Smirnoff 		{ "queue",		QUEUE},
61363b3a8eb9SGleb Smirnoff 		{ "quick",		QUICK},
61373b3a8eb9SGleb Smirnoff 		{ "random",		RANDOM},
61383b3a8eb9SGleb Smirnoff 		{ "random-id",		RANDOMID},
61393b3a8eb9SGleb Smirnoff 		{ "rdr",		RDR},
61403b3a8eb9SGleb Smirnoff 		{ "rdr-anchor",		RDRANCHOR},
61413b3a8eb9SGleb Smirnoff 		{ "realtime",		REALTIME},
61423b3a8eb9SGleb Smirnoff 		{ "reassemble",		REASSEMBLE},
61433b3a8eb9SGleb Smirnoff 		{ "reply-to",		REPLYTO},
61443b3a8eb9SGleb Smirnoff 		{ "require-order",	REQUIREORDER},
61453b3a8eb9SGleb Smirnoff 		{ "return",		RETURN},
61463b3a8eb9SGleb Smirnoff 		{ "return-icmp",	RETURNICMP},
61473b3a8eb9SGleb Smirnoff 		{ "return-icmp6",	RETURNICMP6},
61483b3a8eb9SGleb Smirnoff 		{ "return-rst",		RETURNRST},
614976c5eeccSKristof Provost 		{ "ridentifier",	RIDENTIFIER},
61503b3a8eb9SGleb Smirnoff 		{ "round-robin",	ROUNDROBIN},
61513b3a8eb9SGleb Smirnoff 		{ "route",		ROUTE},
61523b3a8eb9SGleb Smirnoff 		{ "route-to",		ROUTETO},
61533b3a8eb9SGleb Smirnoff 		{ "rtable",		RTABLE},
61543b3a8eb9SGleb Smirnoff 		{ "rule",		RULE},
61553b3a8eb9SGleb Smirnoff 		{ "ruleset-optimization",	RULESET_OPTIMIZATION},
61563b3a8eb9SGleb Smirnoff 		{ "scrub",		SCRUB},
61573b3a8eb9SGleb Smirnoff 		{ "set",		SET},
61583b3a8eb9SGleb Smirnoff 		{ "set-tos",		SETTOS},
61593b3a8eb9SGleb Smirnoff 		{ "skip",		SKIP},
61603b3a8eb9SGleb Smirnoff 		{ "sloppy",		SLOPPY},
61613b3a8eb9SGleb Smirnoff 		{ "source-hash",	SOURCEHASH},
61623b3a8eb9SGleb Smirnoff 		{ "source-track",	SOURCETRACK},
61633b3a8eb9SGleb Smirnoff 		{ "state",		STATE},
61643b3a8eb9SGleb Smirnoff 		{ "state-defaults",	STATEDEFAULTS},
61653b3a8eb9SGleb Smirnoff 		{ "state-policy",	STATEPOLICY},
61663b3a8eb9SGleb Smirnoff 		{ "static-port",	STATICPORT},
61673b3a8eb9SGleb Smirnoff 		{ "sticky-address",	STICKYADDRESS},
6168c69121c4SKristof Provost 		{ "syncookies",         SYNCOOKIES},
61693b3a8eb9SGleb Smirnoff 		{ "synproxy",		SYNPROXY},
61703b3a8eb9SGleb Smirnoff 		{ "table",		TABLE},
61713b3a8eb9SGleb Smirnoff 		{ "tag",		TAG},
61723b3a8eb9SGleb Smirnoff 		{ "tagged",		TAGGED},
61730a70aaf8SLuiz Otavio O Souza 		{ "target",		TARGET},
61743b3a8eb9SGleb Smirnoff 		{ "tbrsize",		TBRSIZE},
61753b3a8eb9SGleb Smirnoff 		{ "timeout",		TIMEOUT},
61763b3a8eb9SGleb Smirnoff 		{ "to",			TO},
61773b3a8eb9SGleb Smirnoff 		{ "tos",		TOS},
61783b3a8eb9SGleb Smirnoff 		{ "ttl",		TTL},
61793b3a8eb9SGleb Smirnoff 		{ "upperlimit",		UPPERLIMIT},
61803b3a8eb9SGleb Smirnoff 		{ "urpf-failed",	URPFFAILED},
61813b3a8eb9SGleb Smirnoff 		{ "user",		USER},
61823b3a8eb9SGleb Smirnoff 	};
61833b3a8eb9SGleb Smirnoff 	const struct keywords	*p;
61843b3a8eb9SGleb Smirnoff 
61853b3a8eb9SGleb Smirnoff 	p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
61863b3a8eb9SGleb Smirnoff 	    sizeof(keywords[0]), kw_cmp);
61873b3a8eb9SGleb Smirnoff 
61883b3a8eb9SGleb Smirnoff 	if (p) {
61893b3a8eb9SGleb Smirnoff 		if (debug > 1)
61903b3a8eb9SGleb Smirnoff 			fprintf(stderr, "%s: %d\n", s, p->k_val);
61913b3a8eb9SGleb Smirnoff 		return (p->k_val);
61923b3a8eb9SGleb Smirnoff 	} else {
61933b3a8eb9SGleb Smirnoff 		if (debug > 1)
61943b3a8eb9SGleb Smirnoff 			fprintf(stderr, "string: %s\n", s);
61953b3a8eb9SGleb Smirnoff 		return (STRING);
61963b3a8eb9SGleb Smirnoff 	}
61973b3a8eb9SGleb Smirnoff }
61983b3a8eb9SGleb Smirnoff 
61993b3a8eb9SGleb Smirnoff #define MAXPUSHBACK	128
62003b3a8eb9SGleb Smirnoff 
620113cfafabSKristof Provost static char	*parsebuf;
620213cfafabSKristof Provost static int	 parseindex;
620313cfafabSKristof Provost static char	 pushback_buffer[MAXPUSHBACK];
620413cfafabSKristof Provost static int	 pushback_index = 0;
62053b3a8eb9SGleb Smirnoff 
62063b3a8eb9SGleb Smirnoff int
62073b3a8eb9SGleb Smirnoff lgetc(int quotec)
62083b3a8eb9SGleb Smirnoff {
62093b3a8eb9SGleb Smirnoff 	int		c, next;
62103b3a8eb9SGleb Smirnoff 
62113b3a8eb9SGleb Smirnoff 	if (parsebuf) {
62123b3a8eb9SGleb Smirnoff 		/* Read character from the parsebuffer instead of input. */
62133b3a8eb9SGleb Smirnoff 		if (parseindex >= 0) {
62143b3a8eb9SGleb Smirnoff 			c = parsebuf[parseindex++];
62153b3a8eb9SGleb Smirnoff 			if (c != '\0')
62163b3a8eb9SGleb Smirnoff 				return (c);
62173b3a8eb9SGleb Smirnoff 			parsebuf = NULL;
62183b3a8eb9SGleb Smirnoff 		} else
62193b3a8eb9SGleb Smirnoff 			parseindex++;
62203b3a8eb9SGleb Smirnoff 	}
62213b3a8eb9SGleb Smirnoff 
62223b3a8eb9SGleb Smirnoff 	if (pushback_index)
62233b3a8eb9SGleb Smirnoff 		return (pushback_buffer[--pushback_index]);
62243b3a8eb9SGleb Smirnoff 
62253b3a8eb9SGleb Smirnoff 	if (quotec) {
62263b3a8eb9SGleb Smirnoff 		if ((c = getc(file->stream)) == EOF) {
62273b3a8eb9SGleb Smirnoff 			yyerror("reached end of file while parsing quoted string");
62283b3a8eb9SGleb Smirnoff 			if (popfile() == EOF)
62293b3a8eb9SGleb Smirnoff 				return (EOF);
62303b3a8eb9SGleb Smirnoff 			return (quotec);
62313b3a8eb9SGleb Smirnoff 		}
62323b3a8eb9SGleb Smirnoff 		return (c);
62333b3a8eb9SGleb Smirnoff 	}
62343b3a8eb9SGleb Smirnoff 
62353b3a8eb9SGleb Smirnoff 	while ((c = getc(file->stream)) == '\\') {
62363b3a8eb9SGleb Smirnoff 		next = getc(file->stream);
62373b3a8eb9SGleb Smirnoff 		if (next != '\n') {
62383b3a8eb9SGleb Smirnoff 			c = next;
62393b3a8eb9SGleb Smirnoff 			break;
62403b3a8eb9SGleb Smirnoff 		}
62413b3a8eb9SGleb Smirnoff 		yylval.lineno = file->lineno;
62423b3a8eb9SGleb Smirnoff 		file->lineno++;
62433b3a8eb9SGleb Smirnoff 	}
62443b3a8eb9SGleb Smirnoff 
62453b3a8eb9SGleb Smirnoff 	while (c == EOF) {
62463b3a8eb9SGleb Smirnoff 		if (popfile() == EOF)
62473b3a8eb9SGleb Smirnoff 			return (EOF);
62483b3a8eb9SGleb Smirnoff 		c = getc(file->stream);
62493b3a8eb9SGleb Smirnoff 	}
62503b3a8eb9SGleb Smirnoff 	return (c);
62513b3a8eb9SGleb Smirnoff }
62523b3a8eb9SGleb Smirnoff 
62533b3a8eb9SGleb Smirnoff int
62543b3a8eb9SGleb Smirnoff lungetc(int c)
62553b3a8eb9SGleb Smirnoff {
62563b3a8eb9SGleb Smirnoff 	if (c == EOF)
62573b3a8eb9SGleb Smirnoff 		return (EOF);
62583b3a8eb9SGleb Smirnoff 	if (parsebuf) {
62593b3a8eb9SGleb Smirnoff 		parseindex--;
62603b3a8eb9SGleb Smirnoff 		if (parseindex >= 0)
62613b3a8eb9SGleb Smirnoff 			return (c);
62623b3a8eb9SGleb Smirnoff 	}
62633b3a8eb9SGleb Smirnoff 	if (pushback_index < MAXPUSHBACK-1)
62643b3a8eb9SGleb Smirnoff 		return (pushback_buffer[pushback_index++] = c);
62653b3a8eb9SGleb Smirnoff 	else
62663b3a8eb9SGleb Smirnoff 		return (EOF);
62673b3a8eb9SGleb Smirnoff }
62683b3a8eb9SGleb Smirnoff 
62693b3a8eb9SGleb Smirnoff int
62703b3a8eb9SGleb Smirnoff findeol(void)
62713b3a8eb9SGleb Smirnoff {
62723b3a8eb9SGleb Smirnoff 	int	c;
62733b3a8eb9SGleb Smirnoff 
62743b3a8eb9SGleb Smirnoff 	parsebuf = NULL;
62753b3a8eb9SGleb Smirnoff 
62763b3a8eb9SGleb Smirnoff 	/* skip to either EOF or the first real EOL */
62773b3a8eb9SGleb Smirnoff 	while (1) {
62783b3a8eb9SGleb Smirnoff 		if (pushback_index)
62793b3a8eb9SGleb Smirnoff 			c = pushback_buffer[--pushback_index];
62803b3a8eb9SGleb Smirnoff 		else
62813b3a8eb9SGleb Smirnoff 			c = lgetc(0);
62823b3a8eb9SGleb Smirnoff 		if (c == '\n') {
62833b3a8eb9SGleb Smirnoff 			file->lineno++;
62843b3a8eb9SGleb Smirnoff 			break;
62853b3a8eb9SGleb Smirnoff 		}
62863b3a8eb9SGleb Smirnoff 		if (c == EOF)
62873b3a8eb9SGleb Smirnoff 			break;
62883b3a8eb9SGleb Smirnoff 	}
62893b3a8eb9SGleb Smirnoff 	return (ERROR);
62903b3a8eb9SGleb Smirnoff }
62913b3a8eb9SGleb Smirnoff 
62923b3a8eb9SGleb Smirnoff int
62933b3a8eb9SGleb Smirnoff yylex(void)
62943b3a8eb9SGleb Smirnoff {
62953b3a8eb9SGleb Smirnoff 	char	 buf[8096];
62963b3a8eb9SGleb Smirnoff 	char	*p, *val;
62973b3a8eb9SGleb Smirnoff 	int	 quotec, next, c;
62983b3a8eb9SGleb Smirnoff 	int	 token;
62993b3a8eb9SGleb Smirnoff 
63003b3a8eb9SGleb Smirnoff top:
63013b3a8eb9SGleb Smirnoff 	p = buf;
63023b3a8eb9SGleb Smirnoff 	while ((c = lgetc(0)) == ' ' || c == '\t')
63033b3a8eb9SGleb Smirnoff 		; /* nothing */
63043b3a8eb9SGleb Smirnoff 
63053b3a8eb9SGleb Smirnoff 	yylval.lineno = file->lineno;
63063b3a8eb9SGleb Smirnoff 	if (c == '#')
63073b3a8eb9SGleb Smirnoff 		while ((c = lgetc(0)) != '\n' && c != EOF)
63083b3a8eb9SGleb Smirnoff 			; /* nothing */
63093b3a8eb9SGleb Smirnoff 	if (c == '$' && parsebuf == NULL) {
63103b3a8eb9SGleb Smirnoff 		while (1) {
63113b3a8eb9SGleb Smirnoff 			if ((c = lgetc(0)) == EOF)
63123b3a8eb9SGleb Smirnoff 				return (0);
63133b3a8eb9SGleb Smirnoff 
63143b3a8eb9SGleb Smirnoff 			if (p + 1 >= buf + sizeof(buf) - 1) {
63153b3a8eb9SGleb Smirnoff 				yyerror("string too long");
63163b3a8eb9SGleb Smirnoff 				return (findeol());
63173b3a8eb9SGleb Smirnoff 			}
63183b3a8eb9SGleb Smirnoff 			if (isalnum(c) || c == '_') {
63193b3a8eb9SGleb Smirnoff 				*p++ = (char)c;
63203b3a8eb9SGleb Smirnoff 				continue;
63213b3a8eb9SGleb Smirnoff 			}
63223b3a8eb9SGleb Smirnoff 			*p = '\0';
63233b3a8eb9SGleb Smirnoff 			lungetc(c);
63243b3a8eb9SGleb Smirnoff 			break;
63253b3a8eb9SGleb Smirnoff 		}
63263b3a8eb9SGleb Smirnoff 		val = symget(buf);
63273b3a8eb9SGleb Smirnoff 		if (val == NULL) {
63283b3a8eb9SGleb Smirnoff 			yyerror("macro '%s' not defined", buf);
63293b3a8eb9SGleb Smirnoff 			return (findeol());
63303b3a8eb9SGleb Smirnoff 		}
63313b3a8eb9SGleb Smirnoff 		parsebuf = val;
63323b3a8eb9SGleb Smirnoff 		parseindex = 0;
63333b3a8eb9SGleb Smirnoff 		goto top;
63343b3a8eb9SGleb Smirnoff 	}
63353b3a8eb9SGleb Smirnoff 
63363b3a8eb9SGleb Smirnoff 	switch (c) {
63373b3a8eb9SGleb Smirnoff 	case '\'':
63383b3a8eb9SGleb Smirnoff 	case '"':
63393b3a8eb9SGleb Smirnoff 		quotec = c;
63403b3a8eb9SGleb Smirnoff 		while (1) {
63413b3a8eb9SGleb Smirnoff 			if ((c = lgetc(quotec)) == EOF)
63423b3a8eb9SGleb Smirnoff 				return (0);
63433b3a8eb9SGleb Smirnoff 			if (c == '\n') {
63443b3a8eb9SGleb Smirnoff 				file->lineno++;
63453b3a8eb9SGleb Smirnoff 				continue;
63463b3a8eb9SGleb Smirnoff 			} else if (c == '\\') {
63473b3a8eb9SGleb Smirnoff 				if ((next = lgetc(quotec)) == EOF)
63483b3a8eb9SGleb Smirnoff 					return (0);
63493b3a8eb9SGleb Smirnoff 				if (next == quotec || c == ' ' || c == '\t')
63503b3a8eb9SGleb Smirnoff 					c = next;
63514a8e4793SKristof Provost 				else if (next == '\n') {
63524a8e4793SKristof Provost 					file->lineno++;
63533b3a8eb9SGleb Smirnoff 					continue;
63544a8e4793SKristof Provost 				}
63553b3a8eb9SGleb Smirnoff 				else
63563b3a8eb9SGleb Smirnoff 					lungetc(next);
63573b3a8eb9SGleb Smirnoff 			} else if (c == quotec) {
63583b3a8eb9SGleb Smirnoff 				*p = '\0';
63593b3a8eb9SGleb Smirnoff 				break;
63603b3a8eb9SGleb Smirnoff 			}
63613b3a8eb9SGleb Smirnoff 			if (p + 1 >= buf + sizeof(buf) - 1) {
63623b3a8eb9SGleb Smirnoff 				yyerror("string too long");
63633b3a8eb9SGleb Smirnoff 				return (findeol());
63643b3a8eb9SGleb Smirnoff 			}
63653b3a8eb9SGleb Smirnoff 			*p++ = (char)c;
63663b3a8eb9SGleb Smirnoff 		}
63673b3a8eb9SGleb Smirnoff 		yylval.v.string = strdup(buf);
63683b3a8eb9SGleb Smirnoff 		if (yylval.v.string == NULL)
63693b3a8eb9SGleb Smirnoff 			err(1, "yylex: strdup");
63703b3a8eb9SGleb Smirnoff 		return (STRING);
63713b3a8eb9SGleb Smirnoff 	case '<':
63723b3a8eb9SGleb Smirnoff 		next = lgetc(0);
63733b3a8eb9SGleb Smirnoff 		if (next == '>') {
63743b3a8eb9SGleb Smirnoff 			yylval.v.i = PF_OP_XRG;
63753b3a8eb9SGleb Smirnoff 			return (PORTBINARY);
63763b3a8eb9SGleb Smirnoff 		}
63773b3a8eb9SGleb Smirnoff 		lungetc(next);
63783b3a8eb9SGleb Smirnoff 		break;
63793b3a8eb9SGleb Smirnoff 	case '>':
63803b3a8eb9SGleb Smirnoff 		next = lgetc(0);
63813b3a8eb9SGleb Smirnoff 		if (next == '<') {
63823b3a8eb9SGleb Smirnoff 			yylval.v.i = PF_OP_IRG;
63833b3a8eb9SGleb Smirnoff 			return (PORTBINARY);
63843b3a8eb9SGleb Smirnoff 		}
63853b3a8eb9SGleb Smirnoff 		lungetc(next);
63863b3a8eb9SGleb Smirnoff 		break;
63873b3a8eb9SGleb Smirnoff 	case '-':
63883b3a8eb9SGleb Smirnoff 		next = lgetc(0);
63893b3a8eb9SGleb Smirnoff 		if (next == '>')
63903b3a8eb9SGleb Smirnoff 			return (ARROW);
63913b3a8eb9SGleb Smirnoff 		lungetc(next);
63923b3a8eb9SGleb Smirnoff 		break;
63933b3a8eb9SGleb Smirnoff 	}
63943b3a8eb9SGleb Smirnoff 
63953b3a8eb9SGleb Smirnoff #define allowed_to_end_number(x) \
63963b3a8eb9SGleb Smirnoff 	(isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
63973b3a8eb9SGleb Smirnoff 
63983b3a8eb9SGleb Smirnoff 	if (c == '-' || isdigit(c)) {
63993b3a8eb9SGleb Smirnoff 		do {
64003b3a8eb9SGleb Smirnoff 			*p++ = c;
64013b3a8eb9SGleb Smirnoff 			if ((unsigned)(p-buf) >= sizeof(buf)) {
64023b3a8eb9SGleb Smirnoff 				yyerror("string too long");
64033b3a8eb9SGleb Smirnoff 				return (findeol());
64043b3a8eb9SGleb Smirnoff 			}
64053b3a8eb9SGleb Smirnoff 		} while ((c = lgetc(0)) != EOF && isdigit(c));
64063b3a8eb9SGleb Smirnoff 		lungetc(c);
64073b3a8eb9SGleb Smirnoff 		if (p == buf + 1 && buf[0] == '-')
64083b3a8eb9SGleb Smirnoff 			goto nodigits;
64093b3a8eb9SGleb Smirnoff 		if (c == EOF || allowed_to_end_number(c)) {
64103b3a8eb9SGleb Smirnoff 			const char *errstr = NULL;
64113b3a8eb9SGleb Smirnoff 
64123b3a8eb9SGleb Smirnoff 			*p = '\0';
64133b3a8eb9SGleb Smirnoff 			yylval.v.number = strtonum(buf, LLONG_MIN,
64143b3a8eb9SGleb Smirnoff 			    LLONG_MAX, &errstr);
64153b3a8eb9SGleb Smirnoff 			if (errstr) {
64163b3a8eb9SGleb Smirnoff 				yyerror("\"%s\" invalid number: %s",
64173b3a8eb9SGleb Smirnoff 				    buf, errstr);
64183b3a8eb9SGleb Smirnoff 				return (findeol());
64193b3a8eb9SGleb Smirnoff 			}
64203b3a8eb9SGleb Smirnoff 			return (NUMBER);
64213b3a8eb9SGleb Smirnoff 		} else {
64223b3a8eb9SGleb Smirnoff nodigits:
64233b3a8eb9SGleb Smirnoff 			while (p > buf + 1)
64243b3a8eb9SGleb Smirnoff 				lungetc(*--p);
64253b3a8eb9SGleb Smirnoff 			c = *--p;
64263b3a8eb9SGleb Smirnoff 			if (c == '-')
64273b3a8eb9SGleb Smirnoff 				return (c);
64283b3a8eb9SGleb Smirnoff 		}
64293b3a8eb9SGleb Smirnoff 	}
64303b3a8eb9SGleb Smirnoff 
64313b3a8eb9SGleb Smirnoff #define allowed_in_string(x) \
64323b3a8eb9SGleb Smirnoff 	(isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
64333b3a8eb9SGleb Smirnoff 	x != '{' && x != '}' && x != '<' && x != '>' && \
64343b3a8eb9SGleb Smirnoff 	x != '!' && x != '=' && x != '/' && x != '#' && \
64353b3a8eb9SGleb Smirnoff 	x != ','))
64363b3a8eb9SGleb Smirnoff 
64373b3a8eb9SGleb Smirnoff 	if (isalnum(c) || c == ':' || c == '_') {
64383b3a8eb9SGleb Smirnoff 		do {
64393b3a8eb9SGleb Smirnoff 			*p++ = c;
64403b3a8eb9SGleb Smirnoff 			if ((unsigned)(p-buf) >= sizeof(buf)) {
64413b3a8eb9SGleb Smirnoff 				yyerror("string too long");
64423b3a8eb9SGleb Smirnoff 				return (findeol());
64433b3a8eb9SGleb Smirnoff 			}
64443b3a8eb9SGleb Smirnoff 		} while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
64453b3a8eb9SGleb Smirnoff 		lungetc(c);
64463b3a8eb9SGleb Smirnoff 		*p = '\0';
64473b3a8eb9SGleb Smirnoff 		if ((token = lookup(buf)) == STRING)
64483b3a8eb9SGleb Smirnoff 			if ((yylval.v.string = strdup(buf)) == NULL)
64493b3a8eb9SGleb Smirnoff 				err(1, "yylex: strdup");
64503b3a8eb9SGleb Smirnoff 		return (token);
64513b3a8eb9SGleb Smirnoff 	}
64523b3a8eb9SGleb Smirnoff 	if (c == '\n') {
64533b3a8eb9SGleb Smirnoff 		yylval.lineno = file->lineno;
64543b3a8eb9SGleb Smirnoff 		file->lineno++;
64553b3a8eb9SGleb Smirnoff 	}
64563b3a8eb9SGleb Smirnoff 	if (c == EOF)
64573b3a8eb9SGleb Smirnoff 		return (0);
64583b3a8eb9SGleb Smirnoff 	return (c);
64593b3a8eb9SGleb Smirnoff }
64603b3a8eb9SGleb Smirnoff 
64613b3a8eb9SGleb Smirnoff int
64623b3a8eb9SGleb Smirnoff check_file_secrecy(int fd, const char *fname)
64633b3a8eb9SGleb Smirnoff {
64643b3a8eb9SGleb Smirnoff 	struct stat	st;
64653b3a8eb9SGleb Smirnoff 
64663b3a8eb9SGleb Smirnoff 	if (fstat(fd, &st)) {
64673b3a8eb9SGleb Smirnoff 		warn("cannot stat %s", fname);
64683b3a8eb9SGleb Smirnoff 		return (-1);
64693b3a8eb9SGleb Smirnoff 	}
64703b3a8eb9SGleb Smirnoff 	if (st.st_uid != 0 && st.st_uid != getuid()) {
64713b3a8eb9SGleb Smirnoff 		warnx("%s: owner not root or current user", fname);
64723b3a8eb9SGleb Smirnoff 		return (-1);
64733b3a8eb9SGleb Smirnoff 	}
64743b3a8eb9SGleb Smirnoff 	if (st.st_mode & (S_IRWXG | S_IRWXO)) {
64753b3a8eb9SGleb Smirnoff 		warnx("%s: group/world readable/writeable", fname);
64763b3a8eb9SGleb Smirnoff 		return (-1);
64773b3a8eb9SGleb Smirnoff 	}
64783b3a8eb9SGleb Smirnoff 	return (0);
64793b3a8eb9SGleb Smirnoff }
64803b3a8eb9SGleb Smirnoff 
64813b3a8eb9SGleb Smirnoff struct file *
64823b3a8eb9SGleb Smirnoff pushfile(const char *name, int secret)
64833b3a8eb9SGleb Smirnoff {
64843b3a8eb9SGleb Smirnoff 	struct file	*nfile;
64853b3a8eb9SGleb Smirnoff 
64863b3a8eb9SGleb Smirnoff 	if ((nfile = calloc(1, sizeof(struct file))) == NULL ||
64873b3a8eb9SGleb Smirnoff 	    (nfile->name = strdup(name)) == NULL) {
64883b3a8eb9SGleb Smirnoff 		warn("malloc");
64893b3a8eb9SGleb Smirnoff 		return (NULL);
64903b3a8eb9SGleb Smirnoff 	}
64913b3a8eb9SGleb Smirnoff 	if (TAILQ_FIRST(&files) == NULL && strcmp(nfile->name, "-") == 0) {
64923b3a8eb9SGleb Smirnoff 		nfile->stream = stdin;
64933b3a8eb9SGleb Smirnoff 		free(nfile->name);
64943b3a8eb9SGleb Smirnoff 		if ((nfile->name = strdup("stdin")) == NULL) {
64953b3a8eb9SGleb Smirnoff 			warn("strdup");
64963b3a8eb9SGleb Smirnoff 			free(nfile);
64973b3a8eb9SGleb Smirnoff 			return (NULL);
64983b3a8eb9SGleb Smirnoff 		}
64993b3a8eb9SGleb Smirnoff 	} else if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
65003b3a8eb9SGleb Smirnoff 		warn("%s", nfile->name);
65013b3a8eb9SGleb Smirnoff 		free(nfile->name);
65023b3a8eb9SGleb Smirnoff 		free(nfile);
65033b3a8eb9SGleb Smirnoff 		return (NULL);
65043b3a8eb9SGleb Smirnoff 	} else if (secret &&
65053b3a8eb9SGleb Smirnoff 	    check_file_secrecy(fileno(nfile->stream), nfile->name)) {
65063b3a8eb9SGleb Smirnoff 		fclose(nfile->stream);
65073b3a8eb9SGleb Smirnoff 		free(nfile->name);
65083b3a8eb9SGleb Smirnoff 		free(nfile);
65093b3a8eb9SGleb Smirnoff 		return (NULL);
65103b3a8eb9SGleb Smirnoff 	}
65113b3a8eb9SGleb Smirnoff 	nfile->lineno = 1;
65123b3a8eb9SGleb Smirnoff 	TAILQ_INSERT_TAIL(&files, nfile, entry);
65133b3a8eb9SGleb Smirnoff 	return (nfile);
65143b3a8eb9SGleb Smirnoff }
65153b3a8eb9SGleb Smirnoff 
65163b3a8eb9SGleb Smirnoff int
65173b3a8eb9SGleb Smirnoff popfile(void)
65183b3a8eb9SGleb Smirnoff {
65193b3a8eb9SGleb Smirnoff 	struct file	*prev;
65203b3a8eb9SGleb Smirnoff 
65213b3a8eb9SGleb Smirnoff 	if ((prev = TAILQ_PREV(file, files, entry)) != NULL) {
65223b3a8eb9SGleb Smirnoff 		prev->errors += file->errors;
65233b3a8eb9SGleb Smirnoff 		TAILQ_REMOVE(&files, file, entry);
65243b3a8eb9SGleb Smirnoff 		fclose(file->stream);
65253b3a8eb9SGleb Smirnoff 		free(file->name);
65263b3a8eb9SGleb Smirnoff 		free(file);
65273b3a8eb9SGleb Smirnoff 		file = prev;
65283b3a8eb9SGleb Smirnoff 		return (0);
65293b3a8eb9SGleb Smirnoff 	}
65303b3a8eb9SGleb Smirnoff 	return (EOF);
65313b3a8eb9SGleb Smirnoff }
65323b3a8eb9SGleb Smirnoff 
65333b3a8eb9SGleb Smirnoff int
65343b3a8eb9SGleb Smirnoff parse_config(char *filename, struct pfctl *xpf)
65353b3a8eb9SGleb Smirnoff {
65363b3a8eb9SGleb Smirnoff 	int		 errors = 0;
65373b3a8eb9SGleb Smirnoff 	struct sym	*sym;
65383b3a8eb9SGleb Smirnoff 
65393b3a8eb9SGleb Smirnoff 	pf = xpf;
65403b3a8eb9SGleb Smirnoff 	errors = 0;
65413b3a8eb9SGleb Smirnoff 	rulestate = PFCTL_STATE_NONE;
65423b3a8eb9SGleb Smirnoff 	returnicmpdefault = (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
65433b3a8eb9SGleb Smirnoff 	returnicmp6default =
65443b3a8eb9SGleb Smirnoff 	    (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
65453b3a8eb9SGleb Smirnoff 	blockpolicy = PFRULE_DROP;
6546150182e3SKristof Provost 	failpolicy = PFRULE_DROP;
65473b3a8eb9SGleb Smirnoff 	require_order = 1;
65483b3a8eb9SGleb Smirnoff 
65493b3a8eb9SGleb Smirnoff 	if ((file = pushfile(filename, 0)) == NULL) {
65503b3a8eb9SGleb Smirnoff 		warn("cannot open the main config file!");
65513b3a8eb9SGleb Smirnoff 		return (-1);
65523b3a8eb9SGleb Smirnoff 	}
65533b3a8eb9SGleb Smirnoff 
65543b3a8eb9SGleb Smirnoff 	yyparse();
65553b3a8eb9SGleb Smirnoff 	errors = file->errors;
65563b3a8eb9SGleb Smirnoff 	popfile();
65573b3a8eb9SGleb Smirnoff 
65583b3a8eb9SGleb Smirnoff 	/* Free macros and check which have not been used. */
65593b3a8eb9SGleb Smirnoff 	while ((sym = TAILQ_FIRST(&symhead))) {
65603b3a8eb9SGleb Smirnoff 		if ((pf->opts & PF_OPT_VERBOSE2) && !sym->used)
65613b3a8eb9SGleb Smirnoff 			fprintf(stderr, "warning: macro '%s' not "
65623b3a8eb9SGleb Smirnoff 			    "used\n", sym->nam);
65633b3a8eb9SGleb Smirnoff 		free(sym->nam);
65643b3a8eb9SGleb Smirnoff 		free(sym->val);
65653b3a8eb9SGleb Smirnoff 		TAILQ_REMOVE(&symhead, sym, entry);
65663b3a8eb9SGleb Smirnoff 		free(sym);
65673b3a8eb9SGleb Smirnoff 	}
65683b3a8eb9SGleb Smirnoff 
65693b3a8eb9SGleb Smirnoff 	return (errors ? -1 : 0);
65703b3a8eb9SGleb Smirnoff }
65713b3a8eb9SGleb Smirnoff 
65723b3a8eb9SGleb Smirnoff int
65733b3a8eb9SGleb Smirnoff symset(const char *nam, const char *val, int persist)
65743b3a8eb9SGleb Smirnoff {
65753b3a8eb9SGleb Smirnoff 	struct sym	*sym;
65763b3a8eb9SGleb Smirnoff 
65773b3a8eb9SGleb Smirnoff 	for (sym = TAILQ_FIRST(&symhead); sym && strcmp(nam, sym->nam);
65783b3a8eb9SGleb Smirnoff 	    sym = TAILQ_NEXT(sym, entry))
65793b3a8eb9SGleb Smirnoff 		;	/* nothing */
65803b3a8eb9SGleb Smirnoff 
65813b3a8eb9SGleb Smirnoff 	if (sym != NULL) {
65823b3a8eb9SGleb Smirnoff 		if (sym->persist == 1)
65833b3a8eb9SGleb Smirnoff 			return (0);
65843b3a8eb9SGleb Smirnoff 		else {
65853b3a8eb9SGleb Smirnoff 			free(sym->nam);
65863b3a8eb9SGleb Smirnoff 			free(sym->val);
65873b3a8eb9SGleb Smirnoff 			TAILQ_REMOVE(&symhead, sym, entry);
65883b3a8eb9SGleb Smirnoff 			free(sym);
65893b3a8eb9SGleb Smirnoff 		}
65903b3a8eb9SGleb Smirnoff 	}
65913b3a8eb9SGleb Smirnoff 	if ((sym = calloc(1, sizeof(*sym))) == NULL)
65923b3a8eb9SGleb Smirnoff 		return (-1);
65933b3a8eb9SGleb Smirnoff 
65943b3a8eb9SGleb Smirnoff 	sym->nam = strdup(nam);
65953b3a8eb9SGleb Smirnoff 	if (sym->nam == NULL) {
65963b3a8eb9SGleb Smirnoff 		free(sym);
65973b3a8eb9SGleb Smirnoff 		return (-1);
65983b3a8eb9SGleb Smirnoff 	}
65993b3a8eb9SGleb Smirnoff 	sym->val = strdup(val);
66003b3a8eb9SGleb Smirnoff 	if (sym->val == NULL) {
66013b3a8eb9SGleb Smirnoff 		free(sym->nam);
66023b3a8eb9SGleb Smirnoff 		free(sym);
66033b3a8eb9SGleb Smirnoff 		return (-1);
66043b3a8eb9SGleb Smirnoff 	}
66053b3a8eb9SGleb Smirnoff 	sym->used = 0;
66063b3a8eb9SGleb Smirnoff 	sym->persist = persist;
66073b3a8eb9SGleb Smirnoff 	TAILQ_INSERT_TAIL(&symhead, sym, entry);
66083b3a8eb9SGleb Smirnoff 	return (0);
66093b3a8eb9SGleb Smirnoff }
66103b3a8eb9SGleb Smirnoff 
66113b3a8eb9SGleb Smirnoff int
66123b3a8eb9SGleb Smirnoff pfctl_cmdline_symset(char *s)
66133b3a8eb9SGleb Smirnoff {
66143b3a8eb9SGleb Smirnoff 	char	*sym, *val;
66153b3a8eb9SGleb Smirnoff 	int	 ret;
66163b3a8eb9SGleb Smirnoff 
66173b3a8eb9SGleb Smirnoff 	if ((val = strrchr(s, '=')) == NULL)
66183b3a8eb9SGleb Smirnoff 		return (-1);
66193b3a8eb9SGleb Smirnoff 
66203b3a8eb9SGleb Smirnoff 	if ((sym = malloc(strlen(s) - strlen(val) + 1)) == NULL)
66213b3a8eb9SGleb Smirnoff 		err(1, "pfctl_cmdline_symset: malloc");
66223b3a8eb9SGleb Smirnoff 
66233b3a8eb9SGleb Smirnoff 	strlcpy(sym, s, strlen(s) - strlen(val) + 1);
66243b3a8eb9SGleb Smirnoff 
66253b3a8eb9SGleb Smirnoff 	ret = symset(sym, val + 1, 1);
66263b3a8eb9SGleb Smirnoff 	free(sym);
66273b3a8eb9SGleb Smirnoff 
66283b3a8eb9SGleb Smirnoff 	return (ret);
66293b3a8eb9SGleb Smirnoff }
66303b3a8eb9SGleb Smirnoff 
66313b3a8eb9SGleb Smirnoff char *
66323b3a8eb9SGleb Smirnoff symget(const char *nam)
66333b3a8eb9SGleb Smirnoff {
66343b3a8eb9SGleb Smirnoff 	struct sym	*sym;
66353b3a8eb9SGleb Smirnoff 
66363b3a8eb9SGleb Smirnoff 	TAILQ_FOREACH(sym, &symhead, entry)
66373b3a8eb9SGleb Smirnoff 		if (strcmp(nam, sym->nam) == 0) {
66383b3a8eb9SGleb Smirnoff 			sym->used = 1;
66393b3a8eb9SGleb Smirnoff 			return (sym->val);
66403b3a8eb9SGleb Smirnoff 		}
66413b3a8eb9SGleb Smirnoff 	return (NULL);
66423b3a8eb9SGleb Smirnoff }
66433b3a8eb9SGleb Smirnoff 
66443b3a8eb9SGleb Smirnoff void
6645e9eb0941SKristof Provost mv_rules(struct pfctl_ruleset *src, struct pfctl_ruleset *dst)
66463b3a8eb9SGleb Smirnoff {
66473b3a8eb9SGleb Smirnoff 	int i;
6648e9eb0941SKristof Provost 	struct pfctl_rule *r;
66493b3a8eb9SGleb Smirnoff 
66503b3a8eb9SGleb Smirnoff 	for (i = 0; i < PF_RULESET_MAX; ++i) {
66513b3a8eb9SGleb Smirnoff 		while ((r = TAILQ_FIRST(src->rules[i].active.ptr))
66523b3a8eb9SGleb Smirnoff 		    != NULL) {
66533b3a8eb9SGleb Smirnoff 			TAILQ_REMOVE(src->rules[i].active.ptr, r, entries);
66543b3a8eb9SGleb Smirnoff 			TAILQ_INSERT_TAIL(dst->rules[i].active.ptr, r, entries);
66553b3a8eb9SGleb Smirnoff 			dst->anchor->match++;
66563b3a8eb9SGleb Smirnoff 		}
66573b3a8eb9SGleb Smirnoff 		src->anchor->match = 0;
66583b3a8eb9SGleb Smirnoff 		while ((r = TAILQ_FIRST(src->rules[i].inactive.ptr))
66593b3a8eb9SGleb Smirnoff 		    != NULL) {
66603b3a8eb9SGleb Smirnoff 			TAILQ_REMOVE(src->rules[i].inactive.ptr, r, entries);
66613b3a8eb9SGleb Smirnoff 			TAILQ_INSERT_TAIL(dst->rules[i].inactive.ptr,
66623b3a8eb9SGleb Smirnoff 				r, entries);
66633b3a8eb9SGleb Smirnoff 		}
66643b3a8eb9SGleb Smirnoff 	}
66653b3a8eb9SGleb Smirnoff }
66663b3a8eb9SGleb Smirnoff 
66673b3a8eb9SGleb Smirnoff void
6668c5131afeSKristof Provost mv_eth_rules(struct pfctl_eth_ruleset *src, struct pfctl_eth_ruleset *dst)
6669c5131afeSKristof Provost {
6670c5131afeSKristof Provost 	struct pfctl_eth_rule *r;
6671c5131afeSKristof Provost 
6672c5131afeSKristof Provost 	while ((r = TAILQ_FIRST(&src->rules)) != NULL) {
6673c5131afeSKristof Provost 		TAILQ_REMOVE(&src->rules, r, entries);
6674c5131afeSKristof Provost 		TAILQ_INSERT_TAIL(&dst->rules, r, entries);
6675c5131afeSKristof Provost 		dst->anchor->match++;
6676c5131afeSKristof Provost 	}
6677c5131afeSKristof Provost 	src->anchor->match = 0;
6678c5131afeSKristof Provost }
6679c5131afeSKristof Provost 
6680c5131afeSKristof Provost void
66813b3a8eb9SGleb Smirnoff decide_address_family(struct node_host *n, sa_family_t *af)
66823b3a8eb9SGleb Smirnoff {
66833b3a8eb9SGleb Smirnoff 	if (*af != 0 || n == NULL)
66843b3a8eb9SGleb Smirnoff 		return;
66853b3a8eb9SGleb Smirnoff 	*af = n->af;
66863b3a8eb9SGleb Smirnoff 	while ((n = n->next) != NULL) {
66873b3a8eb9SGleb Smirnoff 		if (n->af != *af) {
66883b3a8eb9SGleb Smirnoff 			*af = 0;
66893b3a8eb9SGleb Smirnoff 			return;
66903b3a8eb9SGleb Smirnoff 		}
66913b3a8eb9SGleb Smirnoff 	}
66923b3a8eb9SGleb Smirnoff }
66933b3a8eb9SGleb Smirnoff 
66943b3a8eb9SGleb Smirnoff void
66953b3a8eb9SGleb Smirnoff remove_invalid_hosts(struct node_host **nh, sa_family_t *af)
66963b3a8eb9SGleb Smirnoff {
66973b3a8eb9SGleb Smirnoff 	struct node_host	*n = *nh, *prev = NULL;
66983b3a8eb9SGleb Smirnoff 
66993b3a8eb9SGleb Smirnoff 	while (n != NULL) {
67003b3a8eb9SGleb Smirnoff 		if (*af && n->af && n->af != *af) {
67013b3a8eb9SGleb Smirnoff 			/* unlink and free n */
67023b3a8eb9SGleb Smirnoff 			struct node_host *next = n->next;
67033b3a8eb9SGleb Smirnoff 
67043b3a8eb9SGleb Smirnoff 			/* adjust tail pointer */
67053b3a8eb9SGleb Smirnoff 			if (n == (*nh)->tail)
67063b3a8eb9SGleb Smirnoff 				(*nh)->tail = prev;
67073b3a8eb9SGleb Smirnoff 			/* adjust previous node's next pointer */
67083b3a8eb9SGleb Smirnoff 			if (prev == NULL)
67093b3a8eb9SGleb Smirnoff 				*nh = next;
67103b3a8eb9SGleb Smirnoff 			else
67113b3a8eb9SGleb Smirnoff 				prev->next = next;
67123b3a8eb9SGleb Smirnoff 			/* free node */
67133b3a8eb9SGleb Smirnoff 			if (n->ifname != NULL)
67143b3a8eb9SGleb Smirnoff 				free(n->ifname);
67153b3a8eb9SGleb Smirnoff 			free(n);
67163b3a8eb9SGleb Smirnoff 			n = next;
67173b3a8eb9SGleb Smirnoff 		} else {
67183b3a8eb9SGleb Smirnoff 			if (n->af && !*af)
67193b3a8eb9SGleb Smirnoff 				*af = n->af;
67203b3a8eb9SGleb Smirnoff 			prev = n;
67213b3a8eb9SGleb Smirnoff 			n = n->next;
67223b3a8eb9SGleb Smirnoff 		}
67233b3a8eb9SGleb Smirnoff 	}
67243b3a8eb9SGleb Smirnoff }
67253b3a8eb9SGleb Smirnoff 
67263b3a8eb9SGleb Smirnoff int
67273b3a8eb9SGleb Smirnoff invalid_redirect(struct node_host *nh, sa_family_t af)
67283b3a8eb9SGleb Smirnoff {
67293b3a8eb9SGleb Smirnoff 	if (!af) {
67303b3a8eb9SGleb Smirnoff 		struct node_host *n;
67313b3a8eb9SGleb Smirnoff 
67323b3a8eb9SGleb Smirnoff 		/* tables and dyniftl are ok without an address family */
67333b3a8eb9SGleb Smirnoff 		for (n = nh; n != NULL; n = n->next) {
67343b3a8eb9SGleb Smirnoff 			if (n->addr.type != PF_ADDR_TABLE &&
67353b3a8eb9SGleb Smirnoff 			    n->addr.type != PF_ADDR_DYNIFTL) {
67363b3a8eb9SGleb Smirnoff 				yyerror("address family not given and "
67373b3a8eb9SGleb Smirnoff 				    "translation address expands to multiple "
67383b3a8eb9SGleb Smirnoff 				    "address families");
67393b3a8eb9SGleb Smirnoff 				return (1);
67403b3a8eb9SGleb Smirnoff 			}
67413b3a8eb9SGleb Smirnoff 		}
67423b3a8eb9SGleb Smirnoff 	}
67433b3a8eb9SGleb Smirnoff 	if (nh == NULL) {
67443b3a8eb9SGleb Smirnoff 		yyerror("no translation address with matching address family "
67453b3a8eb9SGleb Smirnoff 		    "found.");
67463b3a8eb9SGleb Smirnoff 		return (1);
67473b3a8eb9SGleb Smirnoff 	}
67483b3a8eb9SGleb Smirnoff 	return (0);
67493b3a8eb9SGleb Smirnoff }
67503b3a8eb9SGleb Smirnoff 
67513b3a8eb9SGleb Smirnoff int
67523b3a8eb9SGleb Smirnoff atoul(char *s, u_long *ulvalp)
67533b3a8eb9SGleb Smirnoff {
67543b3a8eb9SGleb Smirnoff 	u_long	 ulval;
67553b3a8eb9SGleb Smirnoff 	char	*ep;
67563b3a8eb9SGleb Smirnoff 
67573b3a8eb9SGleb Smirnoff 	errno = 0;
67583b3a8eb9SGleb Smirnoff 	ulval = strtoul(s, &ep, 0);
67593b3a8eb9SGleb Smirnoff 	if (s[0] == '\0' || *ep != '\0')
67603b3a8eb9SGleb Smirnoff 		return (-1);
67613b3a8eb9SGleb Smirnoff 	if (errno == ERANGE && ulval == ULONG_MAX)
67623b3a8eb9SGleb Smirnoff 		return (-1);
67633b3a8eb9SGleb Smirnoff 	*ulvalp = ulval;
67643b3a8eb9SGleb Smirnoff 	return (0);
67653b3a8eb9SGleb Smirnoff }
67663b3a8eb9SGleb Smirnoff 
67673b3a8eb9SGleb Smirnoff int
67683b3a8eb9SGleb Smirnoff getservice(char *n)
67693b3a8eb9SGleb Smirnoff {
67703b3a8eb9SGleb Smirnoff 	struct servent	*s;
67713b3a8eb9SGleb Smirnoff 	u_long		 ulval;
67723b3a8eb9SGleb Smirnoff 
67733b3a8eb9SGleb Smirnoff 	if (atoul(n, &ulval) == 0) {
67743b3a8eb9SGleb Smirnoff 		if (ulval > 65535) {
67753b3a8eb9SGleb Smirnoff 			yyerror("illegal port value %lu", ulval);
67763b3a8eb9SGleb Smirnoff 			return (-1);
67773b3a8eb9SGleb Smirnoff 		}
67783b3a8eb9SGleb Smirnoff 		return (htons(ulval));
67793b3a8eb9SGleb Smirnoff 	} else {
67803b3a8eb9SGleb Smirnoff 		s = getservbyname(n, "tcp");
67813b3a8eb9SGleb Smirnoff 		if (s == NULL)
67823b3a8eb9SGleb Smirnoff 			s = getservbyname(n, "udp");
67833b3a8eb9SGleb Smirnoff 		if (s == NULL) {
67843b3a8eb9SGleb Smirnoff 			yyerror("unknown port %s", n);
67853b3a8eb9SGleb Smirnoff 			return (-1);
67863b3a8eb9SGleb Smirnoff 		}
67873b3a8eb9SGleb Smirnoff 		return (s->s_port);
67883b3a8eb9SGleb Smirnoff 	}
67893b3a8eb9SGleb Smirnoff }
67903b3a8eb9SGleb Smirnoff 
67913b3a8eb9SGleb Smirnoff int
67926fcc8e04SKristof Provost rule_label(struct pfctl_rule *r, char *s[PF_RULE_MAX_LABEL_COUNT])
67933b3a8eb9SGleb Smirnoff {
67946fcc8e04SKristof Provost 	for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++) {
67956fcc8e04SKristof Provost 		if (s[i] == NULL)
67966fcc8e04SKristof Provost 			return (0);
67976fcc8e04SKristof Provost 
67986fcc8e04SKristof Provost 		if (strlcpy(r->label[i], s[i], sizeof(r->label[0])) >=
67996fcc8e04SKristof Provost 		    sizeof(r->label[0])) {
68003b3a8eb9SGleb Smirnoff 			yyerror("rule label too long (max %d chars)",
68016fcc8e04SKristof Provost 			    sizeof(r->label[0])-1);
68023b3a8eb9SGleb Smirnoff 			return (-1);
68033b3a8eb9SGleb Smirnoff 		}
68043b3a8eb9SGleb Smirnoff 	}
68053b3a8eb9SGleb Smirnoff 	return (0);
68063b3a8eb9SGleb Smirnoff }
68073b3a8eb9SGleb Smirnoff 
68083b3a8eb9SGleb Smirnoff u_int16_t
68093b3a8eb9SGleb Smirnoff parseicmpspec(char *w, sa_family_t af)
68103b3a8eb9SGleb Smirnoff {
68113b3a8eb9SGleb Smirnoff 	const struct icmpcodeent	*p;
68123b3a8eb9SGleb Smirnoff 	u_long				 ulval;
68133b3a8eb9SGleb Smirnoff 	u_int8_t			 icmptype;
68143b3a8eb9SGleb Smirnoff 
68153b3a8eb9SGleb Smirnoff 	if (af == AF_INET)
68163b3a8eb9SGleb Smirnoff 		icmptype = returnicmpdefault >> 8;
68173b3a8eb9SGleb Smirnoff 	else
68183b3a8eb9SGleb Smirnoff 		icmptype = returnicmp6default >> 8;
68193b3a8eb9SGleb Smirnoff 
68203b3a8eb9SGleb Smirnoff 	if (atoul(w, &ulval) == -1) {
68213b3a8eb9SGleb Smirnoff 		if ((p = geticmpcodebyname(icmptype, w, af)) == NULL) {
68223b3a8eb9SGleb Smirnoff 			yyerror("unknown icmp code %s", w);
68233b3a8eb9SGleb Smirnoff 			return (0);
68243b3a8eb9SGleb Smirnoff 		}
68253b3a8eb9SGleb Smirnoff 		ulval = p->code;
68263b3a8eb9SGleb Smirnoff 	}
68273b3a8eb9SGleb Smirnoff 	if (ulval > 255) {
68283b3a8eb9SGleb Smirnoff 		yyerror("invalid icmp code %lu", ulval);
68293b3a8eb9SGleb Smirnoff 		return (0);
68303b3a8eb9SGleb Smirnoff 	}
68313b3a8eb9SGleb Smirnoff 	return (icmptype << 8 | ulval);
68323b3a8eb9SGleb Smirnoff }
68333b3a8eb9SGleb Smirnoff 
68343b3a8eb9SGleb Smirnoff int
68353b3a8eb9SGleb Smirnoff parseport(char *port, struct range *r, int extensions)
68363b3a8eb9SGleb Smirnoff {
68373b3a8eb9SGleb Smirnoff 	char	*p = strchr(port, ':');
68383b3a8eb9SGleb Smirnoff 
68393b3a8eb9SGleb Smirnoff 	if (p == NULL) {
68403b3a8eb9SGleb Smirnoff 		if ((r->a = getservice(port)) == -1)
68413b3a8eb9SGleb Smirnoff 			return (-1);
68423b3a8eb9SGleb Smirnoff 		r->b = 0;
68433b3a8eb9SGleb Smirnoff 		r->t = PF_OP_NONE;
68443b3a8eb9SGleb Smirnoff 		return (0);
68453b3a8eb9SGleb Smirnoff 	}
68463b3a8eb9SGleb Smirnoff 	if ((extensions & PPORT_STAR) && !strcmp(p+1, "*")) {
68473b3a8eb9SGleb Smirnoff 		*p = 0;
68483b3a8eb9SGleb Smirnoff 		if ((r->a = getservice(port)) == -1)
68493b3a8eb9SGleb Smirnoff 			return (-1);
68503b3a8eb9SGleb Smirnoff 		r->b = 0;
68513b3a8eb9SGleb Smirnoff 		r->t = PF_OP_IRG;
68523b3a8eb9SGleb Smirnoff 		return (0);
68533b3a8eb9SGleb Smirnoff 	}
68543b3a8eb9SGleb Smirnoff 	if ((extensions & PPORT_RANGE)) {
68553b3a8eb9SGleb Smirnoff 		*p++ = 0;
68563b3a8eb9SGleb Smirnoff 		if ((r->a = getservice(port)) == -1 ||
68573b3a8eb9SGleb Smirnoff 		    (r->b = getservice(p)) == -1)
68583b3a8eb9SGleb Smirnoff 			return (-1);
68593b3a8eb9SGleb Smirnoff 		if (r->a == r->b) {
68603b3a8eb9SGleb Smirnoff 			r->b = 0;
68613b3a8eb9SGleb Smirnoff 			r->t = PF_OP_NONE;
68623b3a8eb9SGleb Smirnoff 		} else
68633b3a8eb9SGleb Smirnoff 			r->t = PF_OP_RRG;
68643b3a8eb9SGleb Smirnoff 		return (0);
68653b3a8eb9SGleb Smirnoff 	}
68663b3a8eb9SGleb Smirnoff 	return (-1);
68673b3a8eb9SGleb Smirnoff }
68683b3a8eb9SGleb Smirnoff 
68693b3a8eb9SGleb Smirnoff int
68703b3a8eb9SGleb Smirnoff pfctl_load_anchors(int dev, struct pfctl *pf, struct pfr_buffer *trans)
68713b3a8eb9SGleb Smirnoff {
68723b3a8eb9SGleb Smirnoff 	struct loadanchors	*la;
68733b3a8eb9SGleb Smirnoff 
68743b3a8eb9SGleb Smirnoff 	TAILQ_FOREACH(la, &loadanchorshead, entries) {
68753b3a8eb9SGleb Smirnoff 		if (pf->opts & PF_OPT_VERBOSE)
68763b3a8eb9SGleb Smirnoff 			fprintf(stderr, "\nLoading anchor %s from %s\n",
68773b3a8eb9SGleb Smirnoff 			    la->anchorname, la->filename);
68783b3a8eb9SGleb Smirnoff 		if (pfctl_rules(dev, la->filename, pf->opts, pf->optimize,
68793b3a8eb9SGleb Smirnoff 		    la->anchorname, trans) == -1)
68803b3a8eb9SGleb Smirnoff 			return (-1);
68813b3a8eb9SGleb Smirnoff 	}
68823b3a8eb9SGleb Smirnoff 
68833b3a8eb9SGleb Smirnoff 	return (0);
68843b3a8eb9SGleb Smirnoff }
68853b3a8eb9SGleb Smirnoff 
68863b3a8eb9SGleb Smirnoff int
68871f495578SKristof Provost kw_casecmp(const void *k, const void *e)
68881f495578SKristof Provost {
68891f495578SKristof Provost 	return (strcasecmp(k, ((const struct keywords *)e)->k_name));
68901f495578SKristof Provost }
68911f495578SKristof Provost 
68921f495578SKristof Provost int
68931f495578SKristof Provost map_tos(char *s, int *val)
68941f495578SKristof Provost {
68951f495578SKristof Provost 	/* DiffServ Codepoints and other TOS mappings */
68961f495578SKristof Provost 	const struct keywords	 toswords[] = {
68971f495578SKristof Provost 		{ "af11",		IPTOS_DSCP_AF11 },
68981f495578SKristof Provost 		{ "af12",		IPTOS_DSCP_AF12 },
68991f495578SKristof Provost 		{ "af13",		IPTOS_DSCP_AF13 },
69001f495578SKristof Provost 		{ "af21",		IPTOS_DSCP_AF21 },
69011f495578SKristof Provost 		{ "af22",		IPTOS_DSCP_AF22 },
69021f495578SKristof Provost 		{ "af23",		IPTOS_DSCP_AF23 },
69031f495578SKristof Provost 		{ "af31",		IPTOS_DSCP_AF31 },
69041f495578SKristof Provost 		{ "af32",		IPTOS_DSCP_AF32 },
69051f495578SKristof Provost 		{ "af33",		IPTOS_DSCP_AF33 },
69061f495578SKristof Provost 		{ "af41",		IPTOS_DSCP_AF41 },
69071f495578SKristof Provost 		{ "af42",		IPTOS_DSCP_AF42 },
69081f495578SKristof Provost 		{ "af43",		IPTOS_DSCP_AF43 },
69091f495578SKristof Provost 		{ "critical",		IPTOS_PREC_CRITIC_ECP },
69101f495578SKristof Provost 		{ "cs0",		IPTOS_DSCP_CS0 },
69111f495578SKristof Provost 		{ "cs1",		IPTOS_DSCP_CS1 },
69121f495578SKristof Provost 		{ "cs2",		IPTOS_DSCP_CS2 },
69131f495578SKristof Provost 		{ "cs3",		IPTOS_DSCP_CS3 },
69141f495578SKristof Provost 		{ "cs4",		IPTOS_DSCP_CS4 },
69151f495578SKristof Provost 		{ "cs5",		IPTOS_DSCP_CS5 },
69161f495578SKristof Provost 		{ "cs6",		IPTOS_DSCP_CS6 },
69171f495578SKristof Provost 		{ "cs7",		IPTOS_DSCP_CS7 },
69181f495578SKristof Provost 		{ "ef",			IPTOS_DSCP_EF },
69191f495578SKristof Provost 		{ "inetcontrol",	IPTOS_PREC_INTERNETCONTROL },
69201f495578SKristof Provost 		{ "lowdelay",		IPTOS_LOWDELAY },
69211f495578SKristof Provost 		{ "netcontrol",		IPTOS_PREC_NETCONTROL },
69221f495578SKristof Provost 		{ "reliability",	IPTOS_RELIABILITY },
6923b4e3f3c2SKristof Provost 		{ "throughput",		IPTOS_THROUGHPUT },
6924b4e3f3c2SKristof Provost 		{ "va",			IPTOS_DSCP_VA }
69251f495578SKristof Provost 	};
69261f495578SKristof Provost 	const struct keywords	*p;
69271f495578SKristof Provost 
69281f495578SKristof Provost 	p = bsearch(s, toswords, sizeof(toswords)/sizeof(toswords[0]),
69291f495578SKristof Provost 	    sizeof(toswords[0]), kw_casecmp);
69301f495578SKristof Provost 
69311f495578SKristof Provost 	if (p) {
69321f495578SKristof Provost 		*val = p->k_val;
69331f495578SKristof Provost 		return (1);
69341f495578SKristof Provost 	}
69351f495578SKristof Provost 	return (0);
69361f495578SKristof Provost }
69371f495578SKristof Provost 
69381f495578SKristof Provost int
69393b3a8eb9SGleb Smirnoff rt_tableid_max(void)
69403b3a8eb9SGleb Smirnoff {
69413b3a8eb9SGleb Smirnoff #ifdef __FreeBSD__
69423b3a8eb9SGleb Smirnoff 	int fibs;
69433b3a8eb9SGleb Smirnoff 	size_t l = sizeof(fibs);
69443b3a8eb9SGleb Smirnoff 
69453b3a8eb9SGleb Smirnoff         if (sysctlbyname("net.fibs", &fibs, &l, NULL, 0) == -1)
69463b3a8eb9SGleb Smirnoff 		fibs = 16;	/* XXX RT_MAXFIBS, at least limit it some. */
69473b3a8eb9SGleb Smirnoff 	/*
69483b3a8eb9SGleb Smirnoff 	 * As the OpenBSD code only compares > and not >= we need to adjust
69493b3a8eb9SGleb Smirnoff 	 * here given we only accept values of 0..n and want to avoid #ifdefs
6950b68ac800SPedro F. Giffuni 	 * in the grammar.
69513b3a8eb9SGleb Smirnoff 	 */
69523b3a8eb9SGleb Smirnoff 	return (fibs - 1);
69533b3a8eb9SGleb Smirnoff #else
69543b3a8eb9SGleb Smirnoff 	return (RT_TABLEID_MAX);
69553b3a8eb9SGleb Smirnoff #endif
69563b3a8eb9SGleb Smirnoff }
6957b590f17aSKristof Provost 
6958b590f17aSKristof Provost struct node_mac*
6959b590f17aSKristof Provost node_mac_from_string(const char *str)
6960b590f17aSKristof Provost {
6961b590f17aSKristof Provost 	struct node_mac *m;
6962b590f17aSKristof Provost 
6963b590f17aSKristof Provost 	m = calloc(1, sizeof(struct node_mac));
6964b590f17aSKristof Provost 	if (m == NULL)
6965b590f17aSKristof Provost 		err(1, "mac: calloc");
6966b590f17aSKristof Provost 
6967b590f17aSKristof Provost 	if (sscanf(str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
6968b590f17aSKristof Provost 	    &m->mac[0], &m->mac[1], &m->mac[2], &m->mac[3], &m->mac[4],
6969b590f17aSKristof Provost 	    &m->mac[5]) != 6) {
6970b590f17aSKristof Provost 		free(m);
6971b590f17aSKristof Provost 		yyerror("invalid MAC address");
6972b590f17aSKristof Provost 		return (NULL);
6973b590f17aSKristof Provost 	}
6974b590f17aSKristof Provost 
6975b590f17aSKristof Provost 	memset(m->mask, 0xff, ETHER_ADDR_LEN);
6976c32cd180SKristof Provost 	m->isset = true;
6977b590f17aSKristof Provost 	m->next = NULL;
6978b590f17aSKristof Provost 	m->tail = m;
6979b590f17aSKristof Provost 
6980b590f17aSKristof Provost 	return (m);
6981b590f17aSKristof Provost }
6982b590f17aSKristof Provost 
6983b590f17aSKristof Provost struct node_mac*
6984b590f17aSKristof Provost node_mac_from_string_masklen(const char *str, int masklen)
6985b590f17aSKristof Provost {
6986b590f17aSKristof Provost 	struct node_mac *m;
6987b590f17aSKristof Provost 
6988b590f17aSKristof Provost 	if (masklen < 0 || masklen > (ETHER_ADDR_LEN * 8)) {
6989b590f17aSKristof Provost 		yyerror("invalid MAC mask length");
6990b590f17aSKristof Provost 		return (NULL);
6991b590f17aSKristof Provost 	}
6992b590f17aSKristof Provost 
6993b590f17aSKristof Provost 	m = node_mac_from_string(str);
6994b590f17aSKristof Provost 	if (m == NULL)
6995b590f17aSKristof Provost 		return (NULL);
6996b590f17aSKristof Provost 
6997b590f17aSKristof Provost 	memset(m->mask, 0, ETHER_ADDR_LEN);
6998b590f17aSKristof Provost 	for (int i = 0; i < masklen; i++)
6999b590f17aSKristof Provost 		m->mask[i / 8] |= 1 << (i % 8);
7000b590f17aSKristof Provost 
7001b590f17aSKristof Provost 	return (m);
7002b590f17aSKristof Provost }
7003b590f17aSKristof Provost 
7004b590f17aSKristof Provost struct node_mac*
7005b590f17aSKristof Provost node_mac_from_string_mask(const char *str, const char *mask)
7006b590f17aSKristof Provost {
7007b590f17aSKristof Provost 	struct node_mac *m;
7008b590f17aSKristof Provost 
7009b590f17aSKristof Provost 	m = node_mac_from_string(str);
7010b590f17aSKristof Provost 	if (m == NULL)
7011b590f17aSKristof Provost 		return (NULL);
7012b590f17aSKristof Provost 
7013b590f17aSKristof Provost 	if (sscanf(mask, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
7014b590f17aSKristof Provost 	    &m->mask[0], &m->mask[1], &m->mask[2], &m->mask[3], &m->mask[4],
7015b590f17aSKristof Provost 	    &m->mask[5]) != 6) {
7016b590f17aSKristof Provost 		free(m);
7017b590f17aSKristof Provost 		yyerror("invalid MAC mask");
7018b590f17aSKristof Provost 		return (NULL);
7019b590f17aSKristof Provost 	}
7020b590f17aSKristof Provost 
7021b590f17aSKristof Provost 	return (m);
7022b590f17aSKristof Provost }
7023