xref: /freebsd/sbin/pfctl/parse.y (revision 88e858e57c499f996963bd92e5aac4bace3c4fd3)
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 *,
3548a8af942SKristof Provost 		    struct node_host *, struct node_host *, const char *,
3558a8af942SKristof Provost 		    const char *);
356e9eb0941SKristof Provost void		 expand_rule(struct pfctl_rule *, struct node_if *,
3573b3a8eb9SGleb Smirnoff 		    struct node_host *, struct node_proto *, struct node_os *,
3583b3a8eb9SGleb Smirnoff 		    struct node_host *, struct node_port *, struct node_host *,
3593b3a8eb9SGleb Smirnoff 		    struct node_port *, struct node_uid *, struct node_gid *,
3603b3a8eb9SGleb Smirnoff 		    struct node_icmp *, const char *);
3613b3a8eb9SGleb Smirnoff int		 expand_altq(struct pf_altq *, struct node_if *,
3623b3a8eb9SGleb Smirnoff 		    struct node_queue *, struct node_queue_bw bwspec,
3633b3a8eb9SGleb Smirnoff 		    struct node_queue_opt *);
3643b3a8eb9SGleb Smirnoff int		 expand_queue(struct pf_altq *, struct node_if *,
3653b3a8eb9SGleb Smirnoff 		    struct node_queue *, struct node_queue_bw,
3663b3a8eb9SGleb Smirnoff 		    struct node_queue_opt *);
3673b3a8eb9SGleb Smirnoff int		 expand_skip_interface(struct node_if *);
3683b3a8eb9SGleb Smirnoff 
3693b3a8eb9SGleb Smirnoff int	 check_rulestate(int);
3703b3a8eb9SGleb Smirnoff int	 getservice(char *);
3716fcc8e04SKristof Provost int	 rule_label(struct pfctl_rule *, char *s[PF_RULE_MAX_LABEL_COUNT]);
3723b3a8eb9SGleb Smirnoff int	 rt_tableid_max(void);
3733b3a8eb9SGleb Smirnoff 
374e9eb0941SKristof Provost void	 mv_rules(struct pfctl_ruleset *, struct pfctl_ruleset *);
375c5131afeSKristof Provost void	 mv_eth_rules(struct pfctl_eth_ruleset *, struct pfctl_eth_ruleset *);
3763b3a8eb9SGleb Smirnoff void	 decide_address_family(struct node_host *, sa_family_t *);
3773b3a8eb9SGleb Smirnoff void	 remove_invalid_hosts(struct node_host **, sa_family_t *);
3783b3a8eb9SGleb Smirnoff int	 invalid_redirect(struct node_host *, sa_family_t);
3793b3a8eb9SGleb Smirnoff u_int16_t parseicmpspec(char *, sa_family_t);
3801f495578SKristof Provost int	 kw_casecmp(const void *, const void *);
3811f495578SKristof Provost int	 map_tos(char *string, int *);
382b590f17aSKristof Provost struct node_mac* node_mac_from_string(const char *);
383b590f17aSKristof Provost struct node_mac* node_mac_from_string_masklen(const char *, int);
384b590f17aSKristof Provost struct node_mac* node_mac_from_string_mask(const char *, const char *);
3853b3a8eb9SGleb Smirnoff 
38613cfafabSKristof Provost static TAILQ_HEAD(loadanchorshead, loadanchors)
3873b3a8eb9SGleb Smirnoff     loadanchorshead = TAILQ_HEAD_INITIALIZER(loadanchorshead);
3883b3a8eb9SGleb Smirnoff 
3893b3a8eb9SGleb Smirnoff struct loadanchors {
3903b3a8eb9SGleb Smirnoff 	TAILQ_ENTRY(loadanchors)	 entries;
3913b3a8eb9SGleb Smirnoff 	char				*anchorname;
3923b3a8eb9SGleb Smirnoff 	char				*filename;
3933b3a8eb9SGleb Smirnoff };
3943b3a8eb9SGleb Smirnoff 
3953b3a8eb9SGleb Smirnoff typedef struct {
3963b3a8eb9SGleb Smirnoff 	union {
3973b3a8eb9SGleb Smirnoff 		int64_t			 number;
3983b3a8eb9SGleb Smirnoff 		double			 probability;
3993b3a8eb9SGleb Smirnoff 		int			 i;
4003b3a8eb9SGleb Smirnoff 		char			*string;
4013b3a8eb9SGleb Smirnoff 		u_int			 rtableid;
4023b3a8eb9SGleb Smirnoff 		struct {
4033b3a8eb9SGleb Smirnoff 			u_int8_t	 b1;
4043b3a8eb9SGleb Smirnoff 			u_int8_t	 b2;
4053b3a8eb9SGleb Smirnoff 			u_int16_t	 w;
4063b3a8eb9SGleb Smirnoff 			u_int16_t	 w2;
4073b3a8eb9SGleb Smirnoff 		}			 b;
4083b3a8eb9SGleb Smirnoff 		struct range {
4093b3a8eb9SGleb Smirnoff 			int		 a;
4103b3a8eb9SGleb Smirnoff 			int		 b;
4113b3a8eb9SGleb Smirnoff 			int		 t;
4123b3a8eb9SGleb Smirnoff 		}			 range;
4133b3a8eb9SGleb Smirnoff 		struct node_if		*interface;
4143b3a8eb9SGleb Smirnoff 		struct node_proto	*proto;
4152b29ceb8SKristof Provost 		struct node_etherproto	*etherproto;
4163b3a8eb9SGleb Smirnoff 		struct node_icmp	*icmp;
4173b3a8eb9SGleb Smirnoff 		struct node_host	*host;
4183b3a8eb9SGleb Smirnoff 		struct node_os		*os;
4193b3a8eb9SGleb Smirnoff 		struct node_port	*port;
4203b3a8eb9SGleb Smirnoff 		struct node_uid		*uid;
4213b3a8eb9SGleb Smirnoff 		struct node_gid		*gid;
4223b3a8eb9SGleb Smirnoff 		struct node_state_opt	*state_opt;
4233b3a8eb9SGleb Smirnoff 		struct peer		 peer;
4243b3a8eb9SGleb Smirnoff 		struct {
4253b3a8eb9SGleb Smirnoff 			struct peer	 src, dst;
4263b3a8eb9SGleb Smirnoff 			struct node_os	*src_os;
4273b3a8eb9SGleb Smirnoff 		}			 fromto;
4283b3a8eb9SGleb Smirnoff 		struct {
42987a89d6eSKristof Provost 			struct node_mac	*src;
43087a89d6eSKristof Provost 			struct node_mac	*dst;
4312b29ceb8SKristof Provost 		}			 etherfromto;
43287a89d6eSKristof Provost 		struct node_mac		*mac;
4332b29ceb8SKristof Provost 		struct {
43487a89d6eSKristof Provost 			struct node_mac	*mac;
4352b29ceb8SKristof Provost 		} etheraddr;
4368a8af942SKristof Provost 		char			*bridge_to;
4372b29ceb8SKristof Provost 		struct {
4383b3a8eb9SGleb Smirnoff 			struct node_host	*host;
4393b3a8eb9SGleb Smirnoff 			u_int8_t		 rt;
4403b3a8eb9SGleb Smirnoff 			u_int8_t		 pool_opts;
4413b3a8eb9SGleb Smirnoff 			sa_family_t		 af;
4423b3a8eb9SGleb Smirnoff 			struct pf_poolhashkey	*key;
4433b3a8eb9SGleb Smirnoff 		}			 route;
4443b3a8eb9SGleb Smirnoff 		struct redirection {
4453b3a8eb9SGleb Smirnoff 			struct node_host	*host;
4463b3a8eb9SGleb Smirnoff 			struct range		 rport;
4473b3a8eb9SGleb Smirnoff 		}			*redirection;
4483b3a8eb9SGleb Smirnoff 		struct {
4493b3a8eb9SGleb Smirnoff 			int			 action;
4503b3a8eb9SGleb Smirnoff 			struct node_state_opt	*options;
4513b3a8eb9SGleb Smirnoff 		}			 keep_state;
4523b3a8eb9SGleb Smirnoff 		struct {
4533b3a8eb9SGleb Smirnoff 			u_int8_t	 log;
4543b3a8eb9SGleb Smirnoff 			u_int8_t	 logif;
4553b3a8eb9SGleb Smirnoff 			u_int8_t	 quick;
4563b3a8eb9SGleb Smirnoff 		}			 logquick;
4573b3a8eb9SGleb Smirnoff 		struct {
4583b3a8eb9SGleb Smirnoff 			int		 neg;
4593b3a8eb9SGleb Smirnoff 			char		*name;
4603b3a8eb9SGleb Smirnoff 		}			 tagged;
4613b3a8eb9SGleb Smirnoff 		struct pf_poolhashkey	*hashkey;
4623b3a8eb9SGleb Smirnoff 		struct node_queue	*queue;
4633b3a8eb9SGleb Smirnoff 		struct node_queue_opt	 queue_options;
4643b3a8eb9SGleb Smirnoff 		struct node_queue_bw	 queue_bwspec;
4653b3a8eb9SGleb Smirnoff 		struct node_qassign	 qassign;
4663b3a8eb9SGleb Smirnoff 		struct filter_opts	 filter_opts;
4673b3a8eb9SGleb Smirnoff 		struct antispoof_opts	 antispoof_opts;
4683b3a8eb9SGleb Smirnoff 		struct queue_opts	 queue_opts;
4693b3a8eb9SGleb Smirnoff 		struct scrub_opts	 scrub_opts;
4703b3a8eb9SGleb Smirnoff 		struct table_opts	 table_opts;
4713b3a8eb9SGleb Smirnoff 		struct pool_opts	 pool_opts;
4723b3a8eb9SGleb Smirnoff 		struct node_hfsc_opts	 hfsc_opts;
473a5b789f6SErmal Luçi 		struct node_fairq_opts	 fairq_opts;
4740a70aaf8SLuiz Otavio O Souza 		struct codel_opts	 codel_opts;
4755062afffSKristof Provost 		struct pfctl_watermarks	*watermarks;
4763b3a8eb9SGleb Smirnoff 	} v;
4773b3a8eb9SGleb Smirnoff 	int lineno;
4783b3a8eb9SGleb Smirnoff } YYSTYPE;
4793b3a8eb9SGleb Smirnoff 
4803b3a8eb9SGleb Smirnoff #define PPORT_RANGE	1
4813b3a8eb9SGleb Smirnoff #define PPORT_STAR	2
4823b3a8eb9SGleb Smirnoff int	parseport(char *, struct range *r, int);
4833b3a8eb9SGleb Smirnoff 
4843b3a8eb9SGleb Smirnoff #define DYNIF_MULTIADDR(addr) ((addr).type == PF_ADDR_DYNIFTL && \
4853b3a8eb9SGleb Smirnoff 	(!((addr).iflags & PFI_AFLAG_NOALIAS) ||		 \
4863b3a8eb9SGleb Smirnoff 	!isdigit((addr).v.ifname[strlen((addr).v.ifname)-1])))
4873b3a8eb9SGleb Smirnoff 
4883b3a8eb9SGleb Smirnoff %}
4893b3a8eb9SGleb Smirnoff 
490ef950daaSKristof Provost %token	PASS BLOCK MATCH SCRUB RETURN IN OS OUT LOG QUICK ON FROM TO FLAGS
4913b3a8eb9SGleb Smirnoff %token	RETURNRST RETURNICMP RETURNICMP6 PROTO INET INET6 ALL ANY ICMPTYPE
4923b3a8eb9SGleb Smirnoff %token	ICMP6TYPE CODE KEEP MODULATE STATE PORT RDR NAT BINAT ARROW NODF
4933b3a8eb9SGleb Smirnoff %token	MINTTL ERROR ALLOWOPTS FASTROUTE FILENAME ROUTETO DUPTO REPLYTO NO LABEL
4943b3a8eb9SGleb Smirnoff %token	NOROUTE URPFFAILED FRAGMENT USER GROUP MAXMSS MAXIMUM TTL TOS DROP TABLE
495*88e858e5SKristof Provost %token	REASSEMBLE ANCHOR NATANCHOR RDRANCHOR BINATANCHOR
496150182e3SKristof Provost %token	SET OPTIMIZATION TIMEOUT LIMIT LOGINTERFACE BLOCKPOLICY FAILPOLICY
497150182e3SKristof Provost %token	RANDOMID REQUIREORDER SYNPROXY FINGERPRINTS NOSYNC DEBUG SKIP HOSTID
4988a42005dSKristof Provost %token	ANTISPOOF FOR INCLUDE KEEPCOUNTERS SYNCOOKIES L3
4992b29ceb8SKristof Provost %token	ETHER
5002aa21096SKurosawa Takahiro %token	BITMASK RANDOM SOURCEHASH ROUNDROBIN STATICPORT PROBABILITY MAPEPORTSET
5010a70aaf8SLuiz Otavio O Souza %token	ALTQ CBQ CODEL PRIQ HFSC FAIRQ BANDWIDTH TBRSIZE LINKSHARE REALTIME
5020a70aaf8SLuiz Otavio O Souza %token	UPPERLIMIT QUEUE PRIORITY QLIMIT HOGS BUCKETS RTABLE TARGET INTERVAL
50376c5eeccSKristof Provost %token	DNPIPE DNQUEUE RIDENTIFIER
5043e248e0fSKristof Provost %token	LOAD RULESET_OPTIMIZATION PRIO
5053b3a8eb9SGleb Smirnoff %token	STICKYADDRESS MAXSRCSTATES MAXSRCNODES SOURCETRACK GLOBAL RULE
5063b3a8eb9SGleb Smirnoff %token	MAXSRCCONN MAXSRCCONNRATE OVERLOAD FLUSH SLOPPY
5073b3a8eb9SGleb Smirnoff %token	TAGGED TAG IFBOUND FLOATING STATEPOLICY STATEDEFAULTS ROUTE SETTOS
5088a8af942SKristof Provost %token	DIVERTTO DIVERTREPLY BRIDGE_TO
5093b3a8eb9SGleb Smirnoff %token	<v.string>		STRING
5103b3a8eb9SGleb Smirnoff %token	<v.number>		NUMBER
5113b3a8eb9SGleb Smirnoff %token	<v.i>			PORTBINARY
5123b3a8eb9SGleb Smirnoff %type	<v.interface>		interface if_list if_item_not if_item
5133b3a8eb9SGleb Smirnoff %type	<v.number>		number icmptype icmp6type uid gid
5143b3a8eb9SGleb Smirnoff %type	<v.number>		tos not yesno
5153b3a8eb9SGleb Smirnoff %type	<v.probability>		probability
516c69121c4SKristof Provost %type	<v.i>			no dir af fragcache optimizer syncookie_val
5173b3a8eb9SGleb Smirnoff %type	<v.i>			sourcetrack flush unaryop statelock
5182b29ceb8SKristof Provost %type	<v.i>			etherprotoval
5193b3a8eb9SGleb Smirnoff %type	<v.b>			action nataction natpasslog scrubaction
5203e248e0fSKristof Provost %type	<v.b>			flags flag blockspec prio
5213b3a8eb9SGleb Smirnoff %type	<v.range>		portplain portstar portrange
5223b3a8eb9SGleb Smirnoff %type	<v.hashkey>		hashkey
5233b3a8eb9SGleb Smirnoff %type	<v.proto>		proto proto_list proto_item
5243b3a8eb9SGleb Smirnoff %type	<v.number>		protoval
5253b3a8eb9SGleb Smirnoff %type	<v.icmp>		icmpspec
5263b3a8eb9SGleb Smirnoff %type	<v.icmp>		icmp_list icmp_item
5273b3a8eb9SGleb Smirnoff %type	<v.icmp>		icmp6_list icmp6_item
5283b3a8eb9SGleb Smirnoff %type	<v.number>		reticmpspec reticmp6spec
5298a42005dSKristof Provost %type	<v.fromto>		fromto l3fromto
5303b3a8eb9SGleb Smirnoff %type	<v.peer>		ipportspec from to
5313b3a8eb9SGleb Smirnoff %type	<v.host>		ipspec toipspec xhost host dynaddr host_list
5323b3a8eb9SGleb Smirnoff %type	<v.host>		redir_host_list redirspec
5333b3a8eb9SGleb Smirnoff %type	<v.host>		route_host route_host_list routespec
5343b3a8eb9SGleb Smirnoff %type	<v.os>			os xos os_list
5353b3a8eb9SGleb Smirnoff %type	<v.port>		portspec port_list port_item
5363b3a8eb9SGleb Smirnoff %type	<v.uid>			uids uid_list uid_item
5373b3a8eb9SGleb Smirnoff %type	<v.gid>			gids gid_list gid_item
5383b3a8eb9SGleb Smirnoff %type	<v.route>		route
5393b3a8eb9SGleb Smirnoff %type	<v.redirection>		redirection redirpool
5403b3a8eb9SGleb Smirnoff %type	<v.string>		label stringall tag anchorname
5413b3a8eb9SGleb Smirnoff %type	<v.string>		string varstring numberstring
5423b3a8eb9SGleb Smirnoff %type	<v.keep_state>		keep
5433b3a8eb9SGleb Smirnoff %type	<v.state_opt>		state_opt_spec state_opt_list state_opt_item
5443b3a8eb9SGleb Smirnoff %type	<v.logquick>		logquick quick log logopts logopt
5453b3a8eb9SGleb Smirnoff %type	<v.interface>		antispoof_ifspc antispoof_iflst antispoof_if
5462b29ceb8SKristof Provost %type	<v.qassign>		qname etherqname
5473b3a8eb9SGleb Smirnoff %type	<v.queue>		qassign qassign_list qassign_item
5483b3a8eb9SGleb Smirnoff %type	<v.queue_options>	scheduler
5493b3a8eb9SGleb Smirnoff %type	<v.number>		cbqflags_list cbqflags_item
5503b3a8eb9SGleb Smirnoff %type	<v.number>		priqflags_list priqflags_item
5513b3a8eb9SGleb Smirnoff %type	<v.hfsc_opts>		hfscopts_list hfscopts_item hfsc_opts
552a5b789f6SErmal Luçi %type	<v.fairq_opts>		fairqopts_list fairqopts_item fairq_opts
5530a70aaf8SLuiz Otavio O Souza %type	<v.codel_opts>		codelopts_list codelopts_item codel_opts
5543b3a8eb9SGleb Smirnoff %type	<v.queue_bwspec>	bandwidth
5552b29ceb8SKristof Provost %type	<v.filter_opts>		filter_opts filter_opt filter_opts_l etherfilter_opts etherfilter_opt etherfilter_opts_l
5563e248e0fSKristof Provost %type	<v.filter_opts>		filter_sets filter_set filter_sets_l
5573b3a8eb9SGleb Smirnoff %type	<v.antispoof_opts>	antispoof_opts antispoof_opt antispoof_opts_l
5583b3a8eb9SGleb Smirnoff %type	<v.queue_opts>		queue_opts queue_opt queue_opts_l
5593b3a8eb9SGleb Smirnoff %type	<v.scrub_opts>		scrub_opts scrub_opt scrub_opts_l
5603b3a8eb9SGleb Smirnoff %type	<v.table_opts>		table_opts table_opt table_opts_l
5613b3a8eb9SGleb Smirnoff %type	<v.pool_opts>		pool_opts pool_opt pool_opts_l
5623b3a8eb9SGleb Smirnoff %type	<v.tagged>		tagged
5633b3a8eb9SGleb Smirnoff %type	<v.rtableid>		rtable
5645062afffSKristof Provost %type	<v.watermarks>		syncookie_opts
5652b29ceb8SKristof Provost %type	<v.etherproto>		etherproto etherproto_list etherproto_item
5662b29ceb8SKristof Provost %type	<v.etherfromto>		etherfromto
5672b29ceb8SKristof Provost %type	<v.etheraddr>		etherfrom etherto
5688a8af942SKristof Provost %type	<v.bridge_to>		bridge
56987a89d6eSKristof Provost %type	<v.mac>			xmac mac mac_list macspec
5703b3a8eb9SGleb Smirnoff %%
5713b3a8eb9SGleb Smirnoff 
5723b3a8eb9SGleb Smirnoff ruleset		: /* empty */
5733b3a8eb9SGleb Smirnoff 		| ruleset include '\n'
5743b3a8eb9SGleb Smirnoff 		| ruleset '\n'
5753b3a8eb9SGleb Smirnoff 		| ruleset option '\n'
5762b29ceb8SKristof Provost 		| ruleset etherrule '\n'
577c5131afeSKristof Provost 		| ruleset etheranchorrule '\n'
5783b3a8eb9SGleb Smirnoff 		| ruleset scrubrule '\n'
5793b3a8eb9SGleb Smirnoff 		| ruleset natrule '\n'
5803b3a8eb9SGleb Smirnoff 		| ruleset binatrule '\n'
5813b3a8eb9SGleb Smirnoff 		| ruleset pfrule '\n'
5823b3a8eb9SGleb Smirnoff 		| ruleset anchorrule '\n'
5833b3a8eb9SGleb Smirnoff 		| ruleset loadrule '\n'
5843b3a8eb9SGleb Smirnoff 		| ruleset altqif '\n'
5853b3a8eb9SGleb Smirnoff 		| ruleset queuespec '\n'
5863b3a8eb9SGleb Smirnoff 		| ruleset varset '\n'
5873b3a8eb9SGleb Smirnoff 		| ruleset antispoof '\n'
5883b3a8eb9SGleb Smirnoff 		| ruleset tabledef '\n'
5893b3a8eb9SGleb Smirnoff 		| '{' fakeanchor '}' '\n';
5903b3a8eb9SGleb Smirnoff 		| ruleset error '\n'		{ file->errors++; }
5913b3a8eb9SGleb Smirnoff 		;
5923b3a8eb9SGleb Smirnoff 
5933b3a8eb9SGleb Smirnoff include		: INCLUDE STRING		{
5943b3a8eb9SGleb Smirnoff 			struct file	*nfile;
5953b3a8eb9SGleb Smirnoff 
5963b3a8eb9SGleb Smirnoff 			if ((nfile = pushfile($2, 0)) == NULL) {
5973b3a8eb9SGleb Smirnoff 				yyerror("failed to include file %s", $2);
5983b3a8eb9SGleb Smirnoff 				free($2);
5993b3a8eb9SGleb Smirnoff 				YYERROR;
6003b3a8eb9SGleb Smirnoff 			}
6013b3a8eb9SGleb Smirnoff 			free($2);
6023b3a8eb9SGleb Smirnoff 
6033b3a8eb9SGleb Smirnoff 			file = nfile;
6043b3a8eb9SGleb Smirnoff 			lungetc('\n');
6053b3a8eb9SGleb Smirnoff 		}
6063b3a8eb9SGleb Smirnoff 		;
6073b3a8eb9SGleb Smirnoff 
6083b3a8eb9SGleb Smirnoff /*
6093b3a8eb9SGleb Smirnoff  * apply to previouslys specified rule: must be careful to note
6103b3a8eb9SGleb Smirnoff  * what that is: pf or nat or binat or rdr
6113b3a8eb9SGleb Smirnoff  */
6123b3a8eb9SGleb Smirnoff fakeanchor	: fakeanchor '\n'
6133b3a8eb9SGleb Smirnoff 		| fakeanchor anchorrule '\n'
6143b3a8eb9SGleb Smirnoff 		| fakeanchor binatrule '\n'
6153b3a8eb9SGleb Smirnoff 		| fakeanchor natrule '\n'
6163b3a8eb9SGleb Smirnoff 		| fakeanchor pfrule '\n'
6173b3a8eb9SGleb Smirnoff 		| fakeanchor error '\n'
6183b3a8eb9SGleb Smirnoff 		;
6193b3a8eb9SGleb Smirnoff 
6203b3a8eb9SGleb Smirnoff optimizer	: string	{
6213b3a8eb9SGleb Smirnoff 			if (!strcmp($1, "none"))
6223b3a8eb9SGleb Smirnoff 				$$ = 0;
6233b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "basic"))
6243b3a8eb9SGleb Smirnoff 				$$ = PF_OPTIMIZE_BASIC;
6253b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "profile"))
6263b3a8eb9SGleb Smirnoff 				$$ = PF_OPTIMIZE_BASIC | PF_OPTIMIZE_PROFILE;
6273b3a8eb9SGleb Smirnoff 			else {
6283b3a8eb9SGleb Smirnoff 				yyerror("unknown ruleset-optimization %s", $1);
6293b3a8eb9SGleb Smirnoff 				YYERROR;
6303b3a8eb9SGleb Smirnoff 			}
6313b3a8eb9SGleb Smirnoff 		}
6323b3a8eb9SGleb Smirnoff 		;
6333b3a8eb9SGleb Smirnoff 
6343b3a8eb9SGleb Smirnoff option		: SET OPTIMIZATION STRING		{
6353b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_OPTION)) {
6363b3a8eb9SGleb Smirnoff 				free($3);
6373b3a8eb9SGleb Smirnoff 				YYERROR;
6383b3a8eb9SGleb Smirnoff 			}
6393b3a8eb9SGleb Smirnoff 			if (pfctl_set_optimization(pf, $3) != 0) {
6403b3a8eb9SGleb Smirnoff 				yyerror("unknown optimization %s", $3);
6413b3a8eb9SGleb Smirnoff 				free($3);
6423b3a8eb9SGleb Smirnoff 				YYERROR;
6433b3a8eb9SGleb Smirnoff 			}
6443b3a8eb9SGleb Smirnoff 			free($3);
6453b3a8eb9SGleb Smirnoff 		}
6463b3a8eb9SGleb Smirnoff 		| SET RULESET_OPTIMIZATION optimizer {
6473b3a8eb9SGleb Smirnoff 			if (!(pf->opts & PF_OPT_OPTIMIZE)) {
6483b3a8eb9SGleb Smirnoff 				pf->opts |= PF_OPT_OPTIMIZE;
6493b3a8eb9SGleb Smirnoff 				pf->optimize = $3;
6503b3a8eb9SGleb Smirnoff 			}
6513b3a8eb9SGleb Smirnoff 		}
6523b3a8eb9SGleb Smirnoff 		| SET TIMEOUT timeout_spec
6533b3a8eb9SGleb Smirnoff 		| SET TIMEOUT '{' optnl timeout_list '}'
6543b3a8eb9SGleb Smirnoff 		| SET LIMIT limit_spec
6553b3a8eb9SGleb Smirnoff 		| SET LIMIT '{' optnl limit_list '}'
6563b3a8eb9SGleb Smirnoff 		| SET LOGINTERFACE stringall		{
6573b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_OPTION)) {
6583b3a8eb9SGleb Smirnoff 				free($3);
6593b3a8eb9SGleb Smirnoff 				YYERROR;
6603b3a8eb9SGleb Smirnoff 			}
6613b3a8eb9SGleb Smirnoff 			if (pfctl_set_logif(pf, $3) != 0) {
6623b3a8eb9SGleb Smirnoff 				yyerror("error setting loginterface %s", $3);
6633b3a8eb9SGleb Smirnoff 				free($3);
6643b3a8eb9SGleb Smirnoff 				YYERROR;
6653b3a8eb9SGleb Smirnoff 			}
6663b3a8eb9SGleb Smirnoff 			free($3);
6673b3a8eb9SGleb Smirnoff 		}
6683b3a8eb9SGleb Smirnoff 		| SET HOSTID number {
6693b3a8eb9SGleb Smirnoff 			if ($3 == 0 || $3 > UINT_MAX) {
6703b3a8eb9SGleb Smirnoff 				yyerror("hostid must be non-zero");
6713b3a8eb9SGleb Smirnoff 				YYERROR;
6723b3a8eb9SGleb Smirnoff 			}
6733b3a8eb9SGleb Smirnoff 			if (pfctl_set_hostid(pf, $3) != 0) {
6743b3a8eb9SGleb Smirnoff 				yyerror("error setting hostid %08x", $3);
6753b3a8eb9SGleb Smirnoff 				YYERROR;
6763b3a8eb9SGleb Smirnoff 			}
6773b3a8eb9SGleb Smirnoff 		}
6783b3a8eb9SGleb Smirnoff 		| SET BLOCKPOLICY DROP	{
6793b3a8eb9SGleb Smirnoff 			if (pf->opts & PF_OPT_VERBOSE)
6803b3a8eb9SGleb Smirnoff 				printf("set block-policy drop\n");
6813b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_OPTION))
6823b3a8eb9SGleb Smirnoff 				YYERROR;
6833b3a8eb9SGleb Smirnoff 			blockpolicy = PFRULE_DROP;
6843b3a8eb9SGleb Smirnoff 		}
6853b3a8eb9SGleb Smirnoff 		| SET BLOCKPOLICY RETURN {
6863b3a8eb9SGleb Smirnoff 			if (pf->opts & PF_OPT_VERBOSE)
6873b3a8eb9SGleb Smirnoff 				printf("set block-policy return\n");
6883b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_OPTION))
6893b3a8eb9SGleb Smirnoff 				YYERROR;
6903b3a8eb9SGleb Smirnoff 			blockpolicy = PFRULE_RETURN;
6913b3a8eb9SGleb Smirnoff 		}
692150182e3SKristof Provost 		| SET FAILPOLICY DROP	{
693150182e3SKristof Provost 			if (pf->opts & PF_OPT_VERBOSE)
694150182e3SKristof Provost 				printf("set fail-policy drop\n");
695150182e3SKristof Provost 			if (check_rulestate(PFCTL_STATE_OPTION))
696150182e3SKristof Provost 				YYERROR;
697150182e3SKristof Provost 			failpolicy = PFRULE_DROP;
698150182e3SKristof Provost 		}
699150182e3SKristof Provost 		| SET FAILPOLICY RETURN {
700150182e3SKristof Provost 			if (pf->opts & PF_OPT_VERBOSE)
701150182e3SKristof Provost 				printf("set fail-policy return\n");
702150182e3SKristof Provost 			if (check_rulestate(PFCTL_STATE_OPTION))
703150182e3SKristof Provost 				YYERROR;
704150182e3SKristof Provost 			failpolicy = PFRULE_RETURN;
705150182e3SKristof Provost 		}
7063b3a8eb9SGleb Smirnoff 		| SET REQUIREORDER yesno {
7073b3a8eb9SGleb Smirnoff 			if (pf->opts & PF_OPT_VERBOSE)
7083b3a8eb9SGleb Smirnoff 				printf("set require-order %s\n",
7093b3a8eb9SGleb Smirnoff 				    $3 == 1 ? "yes" : "no");
7103b3a8eb9SGleb Smirnoff 			require_order = $3;
7113b3a8eb9SGleb Smirnoff 		}
7123b3a8eb9SGleb Smirnoff 		| SET FINGERPRINTS STRING {
7133b3a8eb9SGleb Smirnoff 			if (pf->opts & PF_OPT_VERBOSE)
7143b3a8eb9SGleb Smirnoff 				printf("set fingerprints \"%s\"\n", $3);
7153b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_OPTION)) {
7163b3a8eb9SGleb Smirnoff 				free($3);
7173b3a8eb9SGleb Smirnoff 				YYERROR;
7183b3a8eb9SGleb Smirnoff 			}
7193b3a8eb9SGleb Smirnoff 			if (!pf->anchor->name[0]) {
7203b3a8eb9SGleb Smirnoff 				if (pfctl_file_fingerprints(pf->dev,
7213b3a8eb9SGleb Smirnoff 				    pf->opts, $3)) {
7223b3a8eb9SGleb Smirnoff 					yyerror("error loading "
7233b3a8eb9SGleb Smirnoff 					    "fingerprints %s", $3);
7243b3a8eb9SGleb Smirnoff 					free($3);
7253b3a8eb9SGleb Smirnoff 					YYERROR;
7263b3a8eb9SGleb Smirnoff 				}
7273b3a8eb9SGleb Smirnoff 			}
7283b3a8eb9SGleb Smirnoff 			free($3);
7293b3a8eb9SGleb Smirnoff 		}
7303b3a8eb9SGleb Smirnoff 		| SET STATEPOLICY statelock {
7313b3a8eb9SGleb Smirnoff 			if (pf->opts & PF_OPT_VERBOSE)
7323b3a8eb9SGleb Smirnoff 				switch ($3) {
7333b3a8eb9SGleb Smirnoff 				case 0:
7343b3a8eb9SGleb Smirnoff 					printf("set state-policy floating\n");
7353b3a8eb9SGleb Smirnoff 					break;
7363b3a8eb9SGleb Smirnoff 				case PFRULE_IFBOUND:
7373b3a8eb9SGleb Smirnoff 					printf("set state-policy if-bound\n");
7383b3a8eb9SGleb Smirnoff 					break;
7393b3a8eb9SGleb Smirnoff 				}
7403b3a8eb9SGleb Smirnoff 			default_statelock = $3;
7413b3a8eb9SGleb Smirnoff 		}
7423b3a8eb9SGleb Smirnoff 		| SET DEBUG STRING {
7433b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_OPTION)) {
7443b3a8eb9SGleb Smirnoff 				free($3);
7453b3a8eb9SGleb Smirnoff 				YYERROR;
7463b3a8eb9SGleb Smirnoff 			}
7473b3a8eb9SGleb Smirnoff 			if (pfctl_set_debug(pf, $3) != 0) {
7483b3a8eb9SGleb Smirnoff 				yyerror("error setting debuglevel %s", $3);
7493b3a8eb9SGleb Smirnoff 				free($3);
7503b3a8eb9SGleb Smirnoff 				YYERROR;
7513b3a8eb9SGleb Smirnoff 			}
7523b3a8eb9SGleb Smirnoff 			free($3);
7533b3a8eb9SGleb Smirnoff 		}
7543b3a8eb9SGleb Smirnoff 		| SET SKIP interface {
7553b3a8eb9SGleb Smirnoff 			if (expand_skip_interface($3) != 0) {
7563b3a8eb9SGleb Smirnoff 				yyerror("error setting skip interface(s)");
7573b3a8eb9SGleb Smirnoff 				YYERROR;
7583b3a8eb9SGleb Smirnoff 			}
7593b3a8eb9SGleb Smirnoff 		}
7603b3a8eb9SGleb Smirnoff 		| SET STATEDEFAULTS state_opt_list {
7613b3a8eb9SGleb Smirnoff 			if (keep_state_defaults != NULL) {
7623b3a8eb9SGleb Smirnoff 				yyerror("cannot redefine state-defaults");
7633b3a8eb9SGleb Smirnoff 				YYERROR;
7643b3a8eb9SGleb Smirnoff 			}
7653b3a8eb9SGleb Smirnoff 			keep_state_defaults = $3;
7663b3a8eb9SGleb Smirnoff 		}
76742ec75f8SKristof Provost 		| SET KEEPCOUNTERS {
76842ec75f8SKristof Provost 			pf->keep_counters = true;
76942ec75f8SKristof Provost 		}
7705062afffSKristof Provost 		| SET SYNCOOKIES syncookie_val syncookie_opts {
7715062afffSKristof Provost 			if (pfctl_cfg_syncookies(pf, $3, $4)) {
7725062afffSKristof Provost 				yyerror("error setting syncookies");
7735062afffSKristof Provost 				YYERROR;
7745062afffSKristof Provost 			}
775c69121c4SKristof Provost 		}
776c69121c4SKristof Provost 		;
777c69121c4SKristof Provost 
778c69121c4SKristof Provost syncookie_val  : STRING        {
779c69121c4SKristof Provost 			if (!strcmp($1, "never"))
780c69121c4SKristof Provost 				$$ = PFCTL_SYNCOOKIES_NEVER;
7815062afffSKristof Provost 			else if (!strcmp($1, "adaptive"))
7825062afffSKristof Provost 				$$ = PFCTL_SYNCOOKIES_ADAPTIVE;
783c69121c4SKristof Provost 			else if (!strcmp($1, "always"))
784c69121c4SKristof Provost 				$$ = PFCTL_SYNCOOKIES_ALWAYS;
785c69121c4SKristof Provost 			else {
786c69121c4SKristof Provost 				yyerror("illegal value for syncookies");
787c69121c4SKristof Provost 				YYERROR;
788c69121c4SKristof Provost 			}
789c69121c4SKristof Provost 		}
7903b3a8eb9SGleb Smirnoff 		;
7915062afffSKristof Provost syncookie_opts  : /* empty */                   { $$ = NULL; }
7925062afffSKristof Provost 		| {
7935062afffSKristof Provost 			memset(&syncookie_opts, 0, sizeof(syncookie_opts));
7945062afffSKristof Provost 		  } '(' syncookie_opt_l ')'     { $$ = &syncookie_opts; }
7955062afffSKristof Provost 		;
7965062afffSKristof Provost 
7975062afffSKristof Provost syncookie_opt_l : syncookie_opt_l comma syncookie_opt
7985062afffSKristof Provost 		| syncookie_opt
7995062afffSKristof Provost 		;
8005062afffSKristof Provost 
8015062afffSKristof Provost syncookie_opt   : STRING STRING {
8025062afffSKristof Provost 			double   val;
8035062afffSKristof Provost 			char    *cp;
8045062afffSKristof Provost 
8055062afffSKristof Provost 			val = strtod($2, &cp);
8065062afffSKristof Provost 			if (cp == NULL || strcmp(cp, "%"))
8075062afffSKristof Provost 				YYERROR;
8085062afffSKristof Provost 			if (val <= 0 || val > 100) {
8095062afffSKristof Provost 				yyerror("illegal percentage value");
8105062afffSKristof Provost 				YYERROR;
8115062afffSKristof Provost 			}
8125062afffSKristof Provost 			if (!strcmp($1, "start")) {
8135062afffSKristof Provost 				syncookie_opts.hi = val;
8145062afffSKristof Provost 			} else if (!strcmp($1, "end")) {
8155062afffSKristof Provost 				syncookie_opts.lo = val;
8165062afffSKristof Provost 			} else {
8175062afffSKristof Provost 				yyerror("illegal syncookie option");
8185062afffSKristof Provost 				YYERROR;
8195062afffSKristof Provost 			}
8205062afffSKristof Provost 		}
8215062afffSKristof Provost 		;
8223b3a8eb9SGleb Smirnoff 
8233b3a8eb9SGleb Smirnoff stringall	: STRING	{ $$ = $1; }
8243b3a8eb9SGleb Smirnoff 		| ALL		{
8253b3a8eb9SGleb Smirnoff 			if (($$ = strdup("all")) == NULL) {
8263b3a8eb9SGleb Smirnoff 				err(1, "stringall: strdup");
8273b3a8eb9SGleb Smirnoff 			}
8283b3a8eb9SGleb Smirnoff 		}
8293b3a8eb9SGleb Smirnoff 		;
8303b3a8eb9SGleb Smirnoff 
8313b3a8eb9SGleb Smirnoff string		: STRING string				{
8323b3a8eb9SGleb Smirnoff 			if (asprintf(&$$, "%s %s", $1, $2) == -1)
8333b3a8eb9SGleb Smirnoff 				err(1, "string: asprintf");
8343b3a8eb9SGleb Smirnoff 			free($1);
8353b3a8eb9SGleb Smirnoff 			free($2);
8363b3a8eb9SGleb Smirnoff 		}
8373b3a8eb9SGleb Smirnoff 		| STRING
8383b3a8eb9SGleb Smirnoff 		;
8393b3a8eb9SGleb Smirnoff 
8403b3a8eb9SGleb Smirnoff varstring	: numberstring varstring 		{
8413b3a8eb9SGleb Smirnoff 			if (asprintf(&$$, "%s %s", $1, $2) == -1)
8423b3a8eb9SGleb Smirnoff 				err(1, "string: asprintf");
8433b3a8eb9SGleb Smirnoff 			free($1);
8443b3a8eb9SGleb Smirnoff 			free($2);
8453b3a8eb9SGleb Smirnoff 		}
8463b3a8eb9SGleb Smirnoff 		| numberstring
8473b3a8eb9SGleb Smirnoff 		;
8483b3a8eb9SGleb Smirnoff 
8493b3a8eb9SGleb Smirnoff numberstring	: NUMBER				{
8503b3a8eb9SGleb Smirnoff 			char	*s;
8513b3a8eb9SGleb Smirnoff 			if (asprintf(&s, "%lld", (long long)$1) == -1) {
8523b3a8eb9SGleb Smirnoff 				yyerror("string: asprintf");
8533b3a8eb9SGleb Smirnoff 				YYERROR;
8543b3a8eb9SGleb Smirnoff 			}
8553b3a8eb9SGleb Smirnoff 			$$ = s;
8563b3a8eb9SGleb Smirnoff 		}
8573b3a8eb9SGleb Smirnoff 		| STRING
8583b3a8eb9SGleb Smirnoff 		;
8593b3a8eb9SGleb Smirnoff 
8603b3a8eb9SGleb Smirnoff varset		: STRING '=' varstring	{
861d3f65324SKristof Provost 			char *s = $1;
8623b3a8eb9SGleb Smirnoff 			if (pf->opts & PF_OPT_VERBOSE)
8633b3a8eb9SGleb Smirnoff 				printf("%s = \"%s\"\n", $1, $3);
864d3f65324SKristof Provost 			while (*s++) {
865d3f65324SKristof Provost 				if (isspace((unsigned char)*s)) {
866d3f65324SKristof Provost 					yyerror("macro name cannot contain "
867d3f65324SKristof Provost 					   "whitespace");
868d3f65324SKristof Provost 					YYERROR;
869d3f65324SKristof Provost 				}
870d3f65324SKristof Provost 			}
8713b3a8eb9SGleb Smirnoff 			if (symset($1, $3, 0) == -1)
8723b3a8eb9SGleb Smirnoff 				err(1, "cannot store variable %s", $1);
8733b3a8eb9SGleb Smirnoff 			free($1);
8743b3a8eb9SGleb Smirnoff 			free($3);
8753b3a8eb9SGleb Smirnoff 		}
8763b3a8eb9SGleb Smirnoff 		;
8773b3a8eb9SGleb Smirnoff 
8783b3a8eb9SGleb Smirnoff anchorname	: STRING			{ $$ = $1; }
8793b3a8eb9SGleb Smirnoff 		| /* empty */			{ $$ = NULL; }
8803b3a8eb9SGleb Smirnoff 		;
8813b3a8eb9SGleb Smirnoff 
8823b3a8eb9SGleb Smirnoff pfa_anchorlist	: /* empty */
8833b3a8eb9SGleb Smirnoff 		| pfa_anchorlist '\n'
8843b3a8eb9SGleb Smirnoff 		| pfa_anchorlist pfrule '\n'
8853b3a8eb9SGleb Smirnoff 		| pfa_anchorlist anchorrule '\n'
8863b3a8eb9SGleb Smirnoff 		;
8873b3a8eb9SGleb Smirnoff 
8883b3a8eb9SGleb Smirnoff pfa_anchor	: '{'
8893b3a8eb9SGleb Smirnoff 		{
8903b3a8eb9SGleb Smirnoff 			char ta[PF_ANCHOR_NAME_SIZE];
891e9eb0941SKristof Provost 			struct pfctl_ruleset *rs;
8923b3a8eb9SGleb Smirnoff 
8932fa6223aSGordon Bergling 			/* stepping into a brace anchor */
8943b3a8eb9SGleb Smirnoff 			pf->asd++;
8953b3a8eb9SGleb Smirnoff 			pf->bn++;
8963b3a8eb9SGleb Smirnoff 
897585a5ed0SKristof Provost 			/*
898585a5ed0SKristof Provost 			* Anchor contents are parsed before the anchor rule
899585a5ed0SKristof Provost 			* production completes, so we don't know the real
900585a5ed0SKristof Provost 			* location yet. Create a holding ruleset in the root;
901585a5ed0SKristof Provost 			* contents will be moved afterwards.
902585a5ed0SKristof Provost 			*/
9033b3a8eb9SGleb Smirnoff 			snprintf(ta, PF_ANCHOR_NAME_SIZE, "_%d", pf->bn);
9043b3a8eb9SGleb Smirnoff 			rs = pf_find_or_create_ruleset(ta);
9053b3a8eb9SGleb Smirnoff 			if (rs == NULL)
9063b3a8eb9SGleb Smirnoff 				err(1, "pfa_anchor: pf_find_or_create_ruleset");
9073b3a8eb9SGleb Smirnoff 			pf->astack[pf->asd] = rs->anchor;
9083b3a8eb9SGleb Smirnoff 			pf->anchor = rs->anchor;
9093b3a8eb9SGleb Smirnoff 		} '\n' pfa_anchorlist '}'
9103b3a8eb9SGleb Smirnoff 		{
9113b3a8eb9SGleb Smirnoff 			pf->alast = pf->anchor;
9123b3a8eb9SGleb Smirnoff 			pf->asd--;
9133b3a8eb9SGleb Smirnoff 			pf->anchor = pf->astack[pf->asd];
9143b3a8eb9SGleb Smirnoff 		}
9153b3a8eb9SGleb Smirnoff 		| /* empty */
9163b3a8eb9SGleb Smirnoff 		;
9173b3a8eb9SGleb Smirnoff 
9183b3a8eb9SGleb Smirnoff anchorrule	: ANCHOR anchorname dir quick interface af proto fromto
9193b3a8eb9SGleb Smirnoff 		    filter_opts pfa_anchor
9203b3a8eb9SGleb Smirnoff 		{
921e9eb0941SKristof Provost 			struct pfctl_rule	r;
9223b3a8eb9SGleb Smirnoff 			struct node_proto	*proto;
9233b3a8eb9SGleb Smirnoff 
9243b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_FILTER)) {
9253b3a8eb9SGleb Smirnoff 				if ($2)
9263b3a8eb9SGleb Smirnoff 					free($2);
9273b3a8eb9SGleb Smirnoff 				YYERROR;
9283b3a8eb9SGleb Smirnoff 			}
9293b3a8eb9SGleb Smirnoff 
9303b3a8eb9SGleb Smirnoff 			if ($2 && ($2[0] == '_' || strstr($2, "/_") != NULL)) {
9313b3a8eb9SGleb Smirnoff 				free($2);
9323b3a8eb9SGleb Smirnoff 				yyerror("anchor names beginning with '_' "
9333b3a8eb9SGleb Smirnoff 				    "are reserved for internal use");
9343b3a8eb9SGleb Smirnoff 				YYERROR;
9353b3a8eb9SGleb Smirnoff 			}
9363b3a8eb9SGleb Smirnoff 
9373b3a8eb9SGleb Smirnoff 			memset(&r, 0, sizeof(r));
9383b3a8eb9SGleb Smirnoff 			if (pf->astack[pf->asd + 1]) {
939585a5ed0SKristof Provost 				if ($2 && strchr($2, '/') != NULL) {
940585a5ed0SKristof Provost 					free($2);
941585a5ed0SKristof Provost 					yyerror("anchor paths containing '/' "
942585a5ed0SKristof Provost 					   "cannot be used for inline anchors.");
943585a5ed0SKristof Provost 					YYERROR;
944585a5ed0SKristof Provost 				}
945585a5ed0SKristof Provost 
946585a5ed0SKristof Provost 				/* Move inline rules into relative location. */
947e9eb0941SKristof Provost 				pfctl_anchor_setup(&r,
9483b3a8eb9SGleb Smirnoff 				    &pf->astack[pf->asd]->ruleset,
9493b3a8eb9SGleb Smirnoff 				    $2 ? $2 : pf->alast->name);
9503b3a8eb9SGleb Smirnoff 
9513b3a8eb9SGleb Smirnoff 				if (r.anchor == NULL)
9523b3a8eb9SGleb Smirnoff 					err(1, "anchorrule: unable to "
9533b3a8eb9SGleb Smirnoff 					    "create ruleset");
9543b3a8eb9SGleb Smirnoff 
9553b3a8eb9SGleb Smirnoff 				if (pf->alast != r.anchor) {
9563b3a8eb9SGleb Smirnoff 					if (r.anchor->match) {
9573b3a8eb9SGleb Smirnoff 						yyerror("inline anchor '%s' "
9583b3a8eb9SGleb Smirnoff 						    "already exists",
9593b3a8eb9SGleb Smirnoff 						    r.anchor->name);
9603b3a8eb9SGleb Smirnoff 						YYERROR;
9613b3a8eb9SGleb Smirnoff 					}
9623b3a8eb9SGleb Smirnoff 					mv_rules(&pf->alast->ruleset,
9633b3a8eb9SGleb Smirnoff 					    &r.anchor->ruleset);
9643b3a8eb9SGleb Smirnoff 				}
9653b3a8eb9SGleb Smirnoff 				pf_remove_if_empty_ruleset(&pf->alast->ruleset);
9663b3a8eb9SGleb Smirnoff 				pf->alast = r.anchor;
9673b3a8eb9SGleb Smirnoff 			} else {
9683b3a8eb9SGleb Smirnoff 				if (!$2) {
9693b3a8eb9SGleb Smirnoff 					yyerror("anchors without explicit "
9703b3a8eb9SGleb Smirnoff 					    "rules must specify a name");
9713b3a8eb9SGleb Smirnoff 					YYERROR;
9723b3a8eb9SGleb Smirnoff 				}
9733b3a8eb9SGleb Smirnoff 			}
9743b3a8eb9SGleb Smirnoff 			r.direction = $3;
9753b3a8eb9SGleb Smirnoff 			r.quick = $4.quick;
9763b3a8eb9SGleb Smirnoff 			r.af = $6;
9773b3a8eb9SGleb Smirnoff 			r.prob = $9.prob;
9783b3a8eb9SGleb Smirnoff 			r.rtableid = $9.rtableid;
97976c5eeccSKristof Provost 			r.ridentifier = $9.ridentifier;
9803b3a8eb9SGleb Smirnoff 
9813b3a8eb9SGleb Smirnoff 			if ($9.tag)
9823b3a8eb9SGleb Smirnoff 				if (strlcpy(r.tagname, $9.tag,
9833b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
9843b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
9853b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
9863b3a8eb9SGleb Smirnoff 					YYERROR;
9873b3a8eb9SGleb Smirnoff 				}
9883b3a8eb9SGleb Smirnoff 			if ($9.match_tag)
9893b3a8eb9SGleb Smirnoff 				if (strlcpy(r.match_tagname, $9.match_tag,
9903b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
9913b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
9923b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
9933b3a8eb9SGleb Smirnoff 					YYERROR;
9943b3a8eb9SGleb Smirnoff 				}
9953b3a8eb9SGleb Smirnoff 			r.match_tag_not = $9.match_tag_not;
9963b3a8eb9SGleb Smirnoff 			if (rule_label(&r, $9.label))
9973b3a8eb9SGleb Smirnoff 				YYERROR;
9986fcc8e04SKristof Provost 			for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++)
9996fcc8e04SKristof Provost 				free($9.label[i]);
10003b3a8eb9SGleb Smirnoff 			r.flags = $9.flags.b1;
10013b3a8eb9SGleb Smirnoff 			r.flagset = $9.flags.b2;
10023b3a8eb9SGleb Smirnoff 			if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
10033b3a8eb9SGleb Smirnoff 				yyerror("flags always false");
10043b3a8eb9SGleb Smirnoff 				YYERROR;
10053b3a8eb9SGleb Smirnoff 			}
10063b3a8eb9SGleb Smirnoff 			if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
10073b3a8eb9SGleb Smirnoff 				for (proto = $7; proto != NULL &&
10083b3a8eb9SGleb Smirnoff 				    proto->proto != IPPROTO_TCP;
10093b3a8eb9SGleb Smirnoff 				    proto = proto->next)
10103b3a8eb9SGleb Smirnoff 					;	/* nothing */
10113b3a8eb9SGleb Smirnoff 				if (proto == NULL && $7 != NULL) {
10123b3a8eb9SGleb Smirnoff 					if ($9.flags.b1 || $9.flags.b2)
10133b3a8eb9SGleb Smirnoff 						yyerror(
10143b3a8eb9SGleb Smirnoff 						    "flags only apply to tcp");
10153b3a8eb9SGleb Smirnoff 					if ($8.src_os)
10163b3a8eb9SGleb Smirnoff 						yyerror(
10173b3a8eb9SGleb Smirnoff 						    "OS fingerprinting only "
10183b3a8eb9SGleb Smirnoff 						    "applies to tcp");
10193b3a8eb9SGleb Smirnoff 					YYERROR;
10203b3a8eb9SGleb Smirnoff 				}
10213b3a8eb9SGleb Smirnoff 			}
10223b3a8eb9SGleb Smirnoff 
10233b3a8eb9SGleb Smirnoff 			r.tos = $9.tos;
10243b3a8eb9SGleb Smirnoff 
10253b3a8eb9SGleb Smirnoff 			if ($9.keep.action) {
10263b3a8eb9SGleb Smirnoff 				yyerror("cannot specify state handling "
10273b3a8eb9SGleb Smirnoff 				    "on anchors");
10283b3a8eb9SGleb Smirnoff 				YYERROR;
10293b3a8eb9SGleb Smirnoff 			}
10303b3a8eb9SGleb Smirnoff 
10313b3a8eb9SGleb Smirnoff 			if ($9.match_tag)
10323b3a8eb9SGleb Smirnoff 				if (strlcpy(r.match_tagname, $9.match_tag,
10333b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
10343b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
10353b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
10363b3a8eb9SGleb Smirnoff 					YYERROR;
10373b3a8eb9SGleb Smirnoff 				}
10383b3a8eb9SGleb Smirnoff 			r.match_tag_not = $9.match_tag_not;
10393e248e0fSKristof Provost 			if ($9.marker & FOM_PRIO) {
10403e248e0fSKristof Provost 				if ($9.prio == 0)
10413e248e0fSKristof Provost 					r.prio = PF_PRIO_ZERO;
10423e248e0fSKristof Provost 				else
10433e248e0fSKristof Provost 					r.prio = $9.prio;
10443e248e0fSKristof Provost 			}
10453e248e0fSKristof Provost 			if ($9.marker & FOM_SETPRIO) {
10463e248e0fSKristof Provost 				r.set_prio[0] = $9.set_prio[0];
10473e248e0fSKristof Provost 				r.set_prio[1] = $9.set_prio[1];
10483e248e0fSKristof Provost 				r.scrub_flags |= PFSTATE_SETPRIO;
10493e248e0fSKristof Provost 			}
10503b3a8eb9SGleb Smirnoff 
10513b3a8eb9SGleb Smirnoff 			decide_address_family($8.src.host, &r.af);
10523b3a8eb9SGleb Smirnoff 			decide_address_family($8.dst.host, &r.af);
10533b3a8eb9SGleb Smirnoff 
10543b3a8eb9SGleb Smirnoff 			expand_rule(&r, $5, NULL, $7, $8.src_os,
10553b3a8eb9SGleb Smirnoff 			    $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
10563b3a8eb9SGleb Smirnoff 			    $9.uid, $9.gid, $9.icmpspec,
10573b3a8eb9SGleb Smirnoff 			    pf->astack[pf->asd + 1] ? pf->alast->name : $2);
10583b3a8eb9SGleb Smirnoff 			free($2);
10593b3a8eb9SGleb Smirnoff 			pf->astack[pf->asd + 1] = NULL;
10603b3a8eb9SGleb Smirnoff 		}
10613b3a8eb9SGleb Smirnoff 		| NATANCHOR string interface af proto fromto rtable {
1062e9eb0941SKristof Provost 			struct pfctl_rule	r;
10633b3a8eb9SGleb Smirnoff 
10643b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_NAT)) {
10653b3a8eb9SGleb Smirnoff 				free($2);
10663b3a8eb9SGleb Smirnoff 				YYERROR;
10673b3a8eb9SGleb Smirnoff 			}
10683b3a8eb9SGleb Smirnoff 
10693b3a8eb9SGleb Smirnoff 			memset(&r, 0, sizeof(r));
10703b3a8eb9SGleb Smirnoff 			r.action = PF_NAT;
10713b3a8eb9SGleb Smirnoff 			r.af = $4;
10723b3a8eb9SGleb Smirnoff 			r.rtableid = $7;
10733b3a8eb9SGleb Smirnoff 
10743b3a8eb9SGleb Smirnoff 			decide_address_family($6.src.host, &r.af);
10753b3a8eb9SGleb Smirnoff 			decide_address_family($6.dst.host, &r.af);
10763b3a8eb9SGleb Smirnoff 
10773b3a8eb9SGleb Smirnoff 			expand_rule(&r, $3, NULL, $5, $6.src_os,
10783b3a8eb9SGleb Smirnoff 			    $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
10793b3a8eb9SGleb Smirnoff 			    0, 0, 0, $2);
10803b3a8eb9SGleb Smirnoff 			free($2);
10813b3a8eb9SGleb Smirnoff 		}
10823b3a8eb9SGleb Smirnoff 		| RDRANCHOR string interface af proto fromto rtable {
1083e9eb0941SKristof Provost 			struct pfctl_rule	r;
10843b3a8eb9SGleb Smirnoff 
10853b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_NAT)) {
10863b3a8eb9SGleb Smirnoff 				free($2);
10873b3a8eb9SGleb Smirnoff 				YYERROR;
10883b3a8eb9SGleb Smirnoff 			}
10893b3a8eb9SGleb Smirnoff 
10903b3a8eb9SGleb Smirnoff 			memset(&r, 0, sizeof(r));
10913b3a8eb9SGleb Smirnoff 			r.action = PF_RDR;
10923b3a8eb9SGleb Smirnoff 			r.af = $4;
10933b3a8eb9SGleb Smirnoff 			r.rtableid = $7;
10943b3a8eb9SGleb Smirnoff 
10953b3a8eb9SGleb Smirnoff 			decide_address_family($6.src.host, &r.af);
10963b3a8eb9SGleb Smirnoff 			decide_address_family($6.dst.host, &r.af);
10973b3a8eb9SGleb Smirnoff 
10983b3a8eb9SGleb Smirnoff 			if ($6.src.port != NULL) {
10993b3a8eb9SGleb Smirnoff 				yyerror("source port parameter not supported"
11003b3a8eb9SGleb Smirnoff 				    " in rdr-anchor");
11013b3a8eb9SGleb Smirnoff 				YYERROR;
11023b3a8eb9SGleb Smirnoff 			}
11033b3a8eb9SGleb Smirnoff 			if ($6.dst.port != NULL) {
11043b3a8eb9SGleb Smirnoff 				if ($6.dst.port->next != NULL) {
11053b3a8eb9SGleb Smirnoff 					yyerror("destination port list "
11063b3a8eb9SGleb Smirnoff 					    "expansion not supported in "
11073b3a8eb9SGleb Smirnoff 					    "rdr-anchor");
11083b3a8eb9SGleb Smirnoff 					YYERROR;
11093b3a8eb9SGleb Smirnoff 				} else if ($6.dst.port->op != PF_OP_EQ) {
11103b3a8eb9SGleb Smirnoff 					yyerror("destination port operators"
11113b3a8eb9SGleb Smirnoff 					    " not supported in rdr-anchor");
11123b3a8eb9SGleb Smirnoff 					YYERROR;
11133b3a8eb9SGleb Smirnoff 				}
11143b3a8eb9SGleb Smirnoff 				r.dst.port[0] = $6.dst.port->port[0];
11153b3a8eb9SGleb Smirnoff 				r.dst.port[1] = $6.dst.port->port[1];
11163b3a8eb9SGleb Smirnoff 				r.dst.port_op = $6.dst.port->op;
11173b3a8eb9SGleb Smirnoff 			}
11183b3a8eb9SGleb Smirnoff 
11193b3a8eb9SGleb Smirnoff 			expand_rule(&r, $3, NULL, $5, $6.src_os,
11203b3a8eb9SGleb Smirnoff 			    $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
11213b3a8eb9SGleb Smirnoff 			    0, 0, 0, $2);
11223b3a8eb9SGleb Smirnoff 			free($2);
11233b3a8eb9SGleb Smirnoff 		}
11243b3a8eb9SGleb Smirnoff 		| BINATANCHOR string interface af proto fromto rtable {
1125e9eb0941SKristof Provost 			struct pfctl_rule	r;
11263b3a8eb9SGleb Smirnoff 
11273b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_NAT)) {
11283b3a8eb9SGleb Smirnoff 				free($2);
11293b3a8eb9SGleb Smirnoff 				YYERROR;
11303b3a8eb9SGleb Smirnoff 			}
11313b3a8eb9SGleb Smirnoff 
11323b3a8eb9SGleb Smirnoff 			memset(&r, 0, sizeof(r));
11333b3a8eb9SGleb Smirnoff 			r.action = PF_BINAT;
11343b3a8eb9SGleb Smirnoff 			r.af = $4;
11353b3a8eb9SGleb Smirnoff 			r.rtableid = $7;
11363b3a8eb9SGleb Smirnoff 			if ($5 != NULL) {
11373b3a8eb9SGleb Smirnoff 				if ($5->next != NULL) {
11383b3a8eb9SGleb Smirnoff 					yyerror("proto list expansion"
11393b3a8eb9SGleb Smirnoff 					    " not supported in binat-anchor");
11403b3a8eb9SGleb Smirnoff 					YYERROR;
11413b3a8eb9SGleb Smirnoff 				}
11423b3a8eb9SGleb Smirnoff 				r.proto = $5->proto;
11433b3a8eb9SGleb Smirnoff 				free($5);
11443b3a8eb9SGleb Smirnoff 			}
11453b3a8eb9SGleb Smirnoff 
11463b3a8eb9SGleb Smirnoff 			if ($6.src.host != NULL || $6.src.port != NULL ||
11473b3a8eb9SGleb Smirnoff 			    $6.dst.host != NULL || $6.dst.port != NULL) {
11483b3a8eb9SGleb Smirnoff 				yyerror("fromto parameter not supported"
11493b3a8eb9SGleb Smirnoff 				    " in binat-anchor");
11503b3a8eb9SGleb Smirnoff 				YYERROR;
11513b3a8eb9SGleb Smirnoff 			}
11523b3a8eb9SGleb Smirnoff 
11533b3a8eb9SGleb Smirnoff 			decide_address_family($6.src.host, &r.af);
11543b3a8eb9SGleb Smirnoff 			decide_address_family($6.dst.host, &r.af);
11553b3a8eb9SGleb Smirnoff 
11560d71f9f3SKristof Provost 			pfctl_append_rule(pf, &r, $2);
11573b3a8eb9SGleb Smirnoff 			free($2);
11583b3a8eb9SGleb Smirnoff 		}
11593b3a8eb9SGleb Smirnoff 		;
11603b3a8eb9SGleb Smirnoff 
11613b3a8eb9SGleb Smirnoff loadrule	: LOAD ANCHOR string FROM string	{
11623b3a8eb9SGleb Smirnoff 			struct loadanchors	*loadanchor;
11633b3a8eb9SGleb Smirnoff 
11643b3a8eb9SGleb Smirnoff 			if (strlen(pf->anchor->name) + 1 +
11653b3a8eb9SGleb Smirnoff 			    strlen($3) >= MAXPATHLEN) {
11663b3a8eb9SGleb Smirnoff 				yyerror("anchorname %s too long, max %u\n",
11673b3a8eb9SGleb Smirnoff 				    $3, MAXPATHLEN - 1);
11683b3a8eb9SGleb Smirnoff 				free($3);
11693b3a8eb9SGleb Smirnoff 				YYERROR;
11703b3a8eb9SGleb Smirnoff 			}
11713b3a8eb9SGleb Smirnoff 			loadanchor = calloc(1, sizeof(struct loadanchors));
11723b3a8eb9SGleb Smirnoff 			if (loadanchor == NULL)
11733b3a8eb9SGleb Smirnoff 				err(1, "loadrule: calloc");
11743b3a8eb9SGleb Smirnoff 			if ((loadanchor->anchorname = malloc(MAXPATHLEN)) ==
11753b3a8eb9SGleb Smirnoff 			    NULL)
11763b3a8eb9SGleb Smirnoff 				err(1, "loadrule: malloc");
11773b3a8eb9SGleb Smirnoff 			if (pf->anchor->name[0])
11783b3a8eb9SGleb Smirnoff 				snprintf(loadanchor->anchorname, MAXPATHLEN,
11793b3a8eb9SGleb Smirnoff 				    "%s/%s", pf->anchor->name, $3);
11803b3a8eb9SGleb Smirnoff 			else
11813b3a8eb9SGleb Smirnoff 				strlcpy(loadanchor->anchorname, $3, MAXPATHLEN);
11823b3a8eb9SGleb Smirnoff 			if ((loadanchor->filename = strdup($5)) == NULL)
11833b3a8eb9SGleb Smirnoff 				err(1, "loadrule: strdup");
11843b3a8eb9SGleb Smirnoff 
11853b3a8eb9SGleb Smirnoff 			TAILQ_INSERT_TAIL(&loadanchorshead, loadanchor,
11863b3a8eb9SGleb Smirnoff 			    entries);
11873b3a8eb9SGleb Smirnoff 
11883b3a8eb9SGleb Smirnoff 			free($3);
11893b3a8eb9SGleb Smirnoff 			free($5);
11903b3a8eb9SGleb Smirnoff 		};
11913b3a8eb9SGleb Smirnoff 
11923b3a8eb9SGleb Smirnoff scrubaction	: no SCRUB {
11933b3a8eb9SGleb Smirnoff 			$$.b2 = $$.w = 0;
11943b3a8eb9SGleb Smirnoff 			if ($1)
11953b3a8eb9SGleb Smirnoff 				$$.b1 = PF_NOSCRUB;
11963b3a8eb9SGleb Smirnoff 			else
11973b3a8eb9SGleb Smirnoff 				$$.b1 = PF_SCRUB;
11983b3a8eb9SGleb Smirnoff 		}
11993b3a8eb9SGleb Smirnoff 		;
12003b3a8eb9SGleb Smirnoff 
12018a8af942SKristof Provost etherrule	: ETHER action dir quick interface bridge etherproto etherfromto l3fromto etherfilter_opts
12022b29ceb8SKristof Provost 		{
12032b29ceb8SKristof Provost 			struct pfctl_eth_rule	r;
12042b29ceb8SKristof Provost 
12052b29ceb8SKristof Provost 			bzero(&r, sizeof(r));
12062b29ceb8SKristof Provost 
12072b29ceb8SKristof Provost 			if (check_rulestate(PFCTL_STATE_ETHER))
12082b29ceb8SKristof Provost 				YYERROR;
12092b29ceb8SKristof Provost 
12102b29ceb8SKristof Provost 			r.action = $2.b1;
12112b29ceb8SKristof Provost 			r.direction = $3;
12122b29ceb8SKristof Provost 			r.quick = $4.quick;
12138a8af942SKristof Provost 			if ($10.tag != NULL)
12148a8af942SKristof Provost 				memcpy(&r.tagname, $10.tag, sizeof(r.tagname));
12158a8af942SKristof Provost 			if ($10.match_tag)
12168a8af942SKristof Provost 				if (strlcpy(r.match_tagname, $10.match_tag,
12171f61367fSKristof Provost 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
12181f61367fSKristof Provost 					yyerror("tag too long, max %u chars",
12191f61367fSKristof Provost 					    PF_TAG_NAME_SIZE - 1);
12201f61367fSKristof Provost 					YYERROR;
12211f61367fSKristof Provost 				}
12228a8af942SKristof Provost 			r.match_tag_not = $10.match_tag_not;
12238a8af942SKristof Provost 			if ($10.queues.qname != NULL)
12248a8af942SKristof Provost 				memcpy(&r.qname, $10.queues.qname, sizeof(r.qname));
12258a8af942SKristof Provost 			r.dnpipe = $10.dnpipe;
12268a8af942SKristof Provost 			r.dnflags = $10.free_flags;
12272b29ceb8SKristof Provost 
12288a8af942SKristof Provost 			expand_eth_rule(&r, $5, $7, $8.src, $8.dst,
12298a8af942SKristof Provost 			    $9.src.host, $9.dst.host, $6, "");
1230c5131afeSKristof Provost 		}
1231c5131afeSKristof Provost 		;
1232c5131afeSKristof Provost 
1233c5131afeSKristof Provost etherpfa_anchorlist	: /* empty */
1234c5131afeSKristof Provost 		| etherpfa_anchorlist '\n'
1235c5131afeSKristof Provost 		| etherpfa_anchorlist etherrule '\n'
1236c5131afeSKristof Provost 		| etherpfa_anchorlist etheranchorrule '\n'
1237c5131afeSKristof Provost 		;
1238c5131afeSKristof Provost 
1239c5131afeSKristof Provost etherpfa_anchor	: '{'
1240c5131afeSKristof Provost 		{
1241c5131afeSKristof Provost 			char ta[PF_ANCHOR_NAME_SIZE];
1242c5131afeSKristof Provost 			struct pfctl_eth_ruleset *rs;
1243c5131afeSKristof Provost 
1244c5131afeSKristof Provost 			/* steping into a brace anchor */
1245c5131afeSKristof Provost 			pf->asd++;
1246c5131afeSKristof Provost 			pf->bn++;
1247c5131afeSKristof Provost 
1248c5131afeSKristof Provost 			/* create a holding ruleset in the root */
1249c5131afeSKristof Provost 			snprintf(ta, PF_ANCHOR_NAME_SIZE, "_%d", pf->bn);
1250c5131afeSKristof Provost 			rs = pf_find_or_create_eth_ruleset(ta);
1251c5131afeSKristof Provost 			if (rs == NULL)
1252c5131afeSKristof Provost 				err(1, "etherpfa_anchor: pf_find_or_create_eth_ruleset");
1253c5131afeSKristof Provost 			pf->eastack[pf->asd] = rs->anchor;
1254c5131afeSKristof Provost 			pf->eanchor = rs->anchor;
1255c5131afeSKristof Provost 		} '\n' etherpfa_anchorlist '}'
1256c5131afeSKristof Provost 		{
1257c5131afeSKristof Provost 			pf->ealast = pf->eanchor;
1258c5131afeSKristof Provost 			pf->asd--;
1259c5131afeSKristof Provost 			pf->eanchor = pf->eastack[pf->asd];
1260c5131afeSKristof Provost 		}
1261c5131afeSKristof Provost 		| /* empty */
1262c5131afeSKristof Provost 		;
1263c5131afeSKristof Provost 
12648a42005dSKristof Provost etheranchorrule	: ETHER ANCHOR anchorname dir quick interface etherproto etherfromto l3fromto etherpfa_anchor
1265c5131afeSKristof Provost 		{
1266c5131afeSKristof Provost 			struct pfctl_eth_rule	r;
1267c5131afeSKristof Provost 
1268c5131afeSKristof Provost 			if (check_rulestate(PFCTL_STATE_ETHER)) {
1269c5131afeSKristof Provost 				free($3);
1270c5131afeSKristof Provost 				YYERROR;
1271c5131afeSKristof Provost 			}
1272c5131afeSKristof Provost 
1273c5131afeSKristof Provost 			if ($3 && ($3[0] == '_' || strstr($3, "/_") != NULL)) {
1274c5131afeSKristof Provost 				free($3);
1275c5131afeSKristof Provost 				yyerror("anchor names beginning with '_' "
1276c5131afeSKristof Provost 				    "are reserved for internal use");
1277c5131afeSKristof Provost 				YYERROR;
1278c5131afeSKristof Provost 			}
1279c5131afeSKristof Provost 
1280c5131afeSKristof Provost 			memset(&r, 0, sizeof(r));
1281c5131afeSKristof Provost 			if (pf->eastack[pf->asd + 1]) {
1282cfa1a130SKristof Provost 				if ($3 && strchr($3, '/') != NULL) {
1283cfa1a130SKristof Provost 					free($3);
1284cfa1a130SKristof Provost 					yyerror("anchor paths containing '/' "
1285cfa1a130SKristof Provost 					   "cannot be used for inline anchors.");
1286cfa1a130SKristof Provost 					YYERROR;
1287cfa1a130SKristof Provost 				}
1288cfa1a130SKristof Provost 
1289cfa1a130SKristof Provost 				/* Move inline rules into relative location. */
1290c5131afeSKristof Provost 				pfctl_eth_anchor_setup(pf, &r,
1291c5131afeSKristof Provost 				    &pf->eastack[pf->asd]->ruleset,
1292c5131afeSKristof Provost 				    $3 ? $3 : pf->ealast->name);
1293c5131afeSKristof Provost 				if (r.anchor == NULL)
1294c5131afeSKristof Provost 					err(1, "etheranchorrule: unable to "
1295c5131afeSKristof Provost 					    "create ruleset");
1296c5131afeSKristof Provost 
1297c5131afeSKristof Provost 				if (pf->ealast != r.anchor) {
1298c5131afeSKristof Provost 					if (r.anchor->match) {
1299c5131afeSKristof Provost 						yyerror("inline anchor '%s' "
1300c5131afeSKristof Provost 						    "already exists",
1301c5131afeSKristof Provost 						    r.anchor->name);
1302c5131afeSKristof Provost 						YYERROR;
1303c5131afeSKristof Provost 					}
1304c5131afeSKristof Provost 					mv_eth_rules(&pf->ealast->ruleset,
1305c5131afeSKristof Provost 					    &r.anchor->ruleset);
1306c5131afeSKristof Provost 				}
1307c5131afeSKristof Provost 				pf_remove_if_empty_eth_ruleset(&pf->ealast->ruleset);
1308c5131afeSKristof Provost 				pf->ealast = r.anchor;
1309c5131afeSKristof Provost 			} else {
1310c5131afeSKristof Provost 				if (!$3) {
1311c5131afeSKristof Provost 					yyerror("anchors without explicit "
1312c5131afeSKristof Provost 					    "rules must specify a name");
1313c5131afeSKristof Provost 					YYERROR;
1314c5131afeSKristof Provost 				}
1315c5131afeSKristof Provost 			}
1316c5131afeSKristof Provost 
1317c5131afeSKristof Provost 			r.direction = $4;
1318c5131afeSKristof Provost 			r.quick = $5.quick;
1319c5131afeSKristof Provost 
1320c5131afeSKristof Provost 			expand_eth_rule(&r, $6, $7, $8.src, $8.dst,
13218a8af942SKristof Provost 			    $9.src.host, $9.dst.host, NULL,
1322c5131afeSKristof Provost 			    pf->eastack[pf->asd + 1] ? pf->ealast->name : $3);
1323c5131afeSKristof Provost 
1324c5131afeSKristof Provost 			free($3);
1325c5131afeSKristof Provost 			pf->eastack[pf->asd + 1] = NULL;
13262b29ceb8SKristof Provost 		}
13272b29ceb8SKristof Provost 		;
13282b29ceb8SKristof Provost 
13292b29ceb8SKristof Provost etherfilter_opts	:	{
13302b29ceb8SKristof Provost 				bzero(&filter_opts, sizeof filter_opts);
13312b29ceb8SKristof Provost 			}
13322b29ceb8SKristof Provost 		    etherfilter_opts_l
13332b29ceb8SKristof Provost 			{ $$ = filter_opts; }
13342b29ceb8SKristof Provost 		| /* empty */	{
13352b29ceb8SKristof Provost 			bzero(&filter_opts, sizeof filter_opts);
13362b29ceb8SKristof Provost 			$$ = filter_opts;
13372b29ceb8SKristof Provost 		}
13382b29ceb8SKristof Provost 		;
13392b29ceb8SKristof Provost 
13402b29ceb8SKristof Provost etherfilter_opts_l	: etherfilter_opts_l etherfilter_opt
13412b29ceb8SKristof Provost 			| etherfilter_opt
13422b29ceb8SKristof Provost 
13432b29ceb8SKristof Provost etherfilter_opt	: etherqname	{
13442b29ceb8SKristof Provost 			if (filter_opts.queues.qname) {
13452b29ceb8SKristof Provost 				yyerror("queue cannot be redefined");
13462b29ceb8SKristof Provost 				YYERROR;
13472b29ceb8SKristof Provost 			}
13482b29ceb8SKristof Provost 			filter_opts.queues = $1;
13492b29ceb8SKristof Provost 		}
13502b29ceb8SKristof Provost 		| TAG string				{
13512b29ceb8SKristof Provost 			filter_opts.tag = $2;
13522b29ceb8SKristof Provost 		}
13531f61367fSKristof Provost 		| not TAGGED string			{
13541f61367fSKristof Provost 			filter_opts.match_tag = $3;
13551f61367fSKristof Provost 			filter_opts.match_tag_not = $1;
13561f61367fSKristof Provost 		}
1357fb330f39SKristof Provost 		| DNPIPE number {
1358fb330f39SKristof Provost 			filter_opts.dnpipe = $2;
1359fb330f39SKristof Provost 			filter_opts.free_flags |= PFRULE_DN_IS_PIPE;
1360fb330f39SKristof Provost 		}
1361fb330f39SKristof Provost 		| DNQUEUE number {
1362fb330f39SKristof Provost 			filter_opts.dnpipe = $2;
1363fb330f39SKristof Provost 			filter_opts.free_flags |= PFRULE_DN_IS_QUEUE;
1364fb330f39SKristof Provost 		}
13652b29ceb8SKristof Provost 		;
13662b29ceb8SKristof Provost 
13678a8af942SKristof Provost bridge		:	/* empty */		{
13688a8af942SKristof Provost 			$$ = NULL;
13698a8af942SKristof Provost 		}
13708a8af942SKristof Provost 		| BRIDGE_TO STRING {
13718a8af942SKristof Provost 			$$ = strdup($2);
13728a8af942SKristof Provost 		}
13738a8af942SKristof Provost 		;
13748a8af942SKristof Provost 
13753b3a8eb9SGleb Smirnoff scrubrule	: scrubaction dir logquick interface af proto fromto scrub_opts
13763b3a8eb9SGleb Smirnoff 		{
1377e9eb0941SKristof Provost 			struct pfctl_rule	r;
13783b3a8eb9SGleb Smirnoff 
13793b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_SCRUB))
13803b3a8eb9SGleb Smirnoff 				YYERROR;
13813b3a8eb9SGleb Smirnoff 
13823b3a8eb9SGleb Smirnoff 			memset(&r, 0, sizeof(r));
13833b3a8eb9SGleb Smirnoff 
13843b3a8eb9SGleb Smirnoff 			r.action = $1.b1;
13853b3a8eb9SGleb Smirnoff 			r.direction = $2;
13863b3a8eb9SGleb Smirnoff 
13873b3a8eb9SGleb Smirnoff 			r.log = $3.log;
13883b3a8eb9SGleb Smirnoff 			r.logif = $3.logif;
13893b3a8eb9SGleb Smirnoff 			if ($3.quick) {
13903b3a8eb9SGleb Smirnoff 				yyerror("scrub rules do not support 'quick'");
13913b3a8eb9SGleb Smirnoff 				YYERROR;
13923b3a8eb9SGleb Smirnoff 			}
13933b3a8eb9SGleb Smirnoff 
13943b3a8eb9SGleb Smirnoff 			r.af = $5;
13953b3a8eb9SGleb Smirnoff 			if ($8.nodf)
13963b3a8eb9SGleb Smirnoff 				r.rule_flag |= PFRULE_NODF;
13973b3a8eb9SGleb Smirnoff 			if ($8.randomid)
13983b3a8eb9SGleb Smirnoff 				r.rule_flag |= PFRULE_RANDOMID;
13993b3a8eb9SGleb Smirnoff 			if ($8.reassemble_tcp) {
14003b3a8eb9SGleb Smirnoff 				if (r.direction != PF_INOUT) {
14013b3a8eb9SGleb Smirnoff 					yyerror("reassemble tcp rules can not "
14023b3a8eb9SGleb Smirnoff 					    "specify direction");
14033b3a8eb9SGleb Smirnoff 					YYERROR;
14043b3a8eb9SGleb Smirnoff 				}
14053b3a8eb9SGleb Smirnoff 				r.rule_flag |= PFRULE_REASSEMBLE_TCP;
14063b3a8eb9SGleb Smirnoff 			}
14073b3a8eb9SGleb Smirnoff 			if ($8.minttl)
14083b3a8eb9SGleb Smirnoff 				r.min_ttl = $8.minttl;
14093b3a8eb9SGleb Smirnoff 			if ($8.maxmss)
14103b3a8eb9SGleb Smirnoff 				r.max_mss = $8.maxmss;
14113b3a8eb9SGleb Smirnoff 			if ($8.marker & SOM_SETTOS) {
14123b3a8eb9SGleb Smirnoff 				r.rule_flag |= PFRULE_SET_TOS;
14133b3a8eb9SGleb Smirnoff 				r.set_tos = $8.settos;
14143b3a8eb9SGleb Smirnoff 			}
14153b3a8eb9SGleb Smirnoff 			if ($8.fragcache)
14163b3a8eb9SGleb Smirnoff 				r.rule_flag |= $8.fragcache;
14173b3a8eb9SGleb Smirnoff 			if ($8.match_tag)
14183b3a8eb9SGleb Smirnoff 				if (strlcpy(r.match_tagname, $8.match_tag,
14193b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
14203b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
14213b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
14223b3a8eb9SGleb Smirnoff 					YYERROR;
14233b3a8eb9SGleb Smirnoff 				}
14243b3a8eb9SGleb Smirnoff 			r.match_tag_not = $8.match_tag_not;
14253b3a8eb9SGleb Smirnoff 			r.rtableid = $8.rtableid;
14263b3a8eb9SGleb Smirnoff 
14273b3a8eb9SGleb Smirnoff 			expand_rule(&r, $4, NULL, $6, $7.src_os,
14283b3a8eb9SGleb Smirnoff 			    $7.src.host, $7.src.port, $7.dst.host, $7.dst.port,
14293b3a8eb9SGleb Smirnoff 			    NULL, NULL, NULL, "");
14303b3a8eb9SGleb Smirnoff 		}
14313b3a8eb9SGleb Smirnoff 		;
14323b3a8eb9SGleb Smirnoff 
14333b3a8eb9SGleb Smirnoff scrub_opts	:	{
14343b3a8eb9SGleb Smirnoff 				bzero(&scrub_opts, sizeof scrub_opts);
14353b3a8eb9SGleb Smirnoff 				scrub_opts.rtableid = -1;
14363b3a8eb9SGleb Smirnoff 			}
14373b3a8eb9SGleb Smirnoff 		    scrub_opts_l
14383b3a8eb9SGleb Smirnoff 			{ $$ = scrub_opts; }
14393b3a8eb9SGleb Smirnoff 		| /* empty */ {
14403b3a8eb9SGleb Smirnoff 			bzero(&scrub_opts, sizeof scrub_opts);
14413b3a8eb9SGleb Smirnoff 			scrub_opts.rtableid = -1;
14423b3a8eb9SGleb Smirnoff 			$$ = scrub_opts;
14433b3a8eb9SGleb Smirnoff 		}
14443b3a8eb9SGleb Smirnoff 		;
14453b3a8eb9SGleb Smirnoff 
14463b3a8eb9SGleb Smirnoff scrub_opts_l	: scrub_opts_l scrub_opt
14473b3a8eb9SGleb Smirnoff 		| scrub_opt
14483b3a8eb9SGleb Smirnoff 		;
14493b3a8eb9SGleb Smirnoff 
14503b3a8eb9SGleb Smirnoff scrub_opt	: NODF	{
14513b3a8eb9SGleb Smirnoff 			if (scrub_opts.nodf) {
14523b3a8eb9SGleb Smirnoff 				yyerror("no-df cannot be respecified");
14533b3a8eb9SGleb Smirnoff 				YYERROR;
14543b3a8eb9SGleb Smirnoff 			}
14553b3a8eb9SGleb Smirnoff 			scrub_opts.nodf = 1;
14563b3a8eb9SGleb Smirnoff 		}
14573b3a8eb9SGleb Smirnoff 		| MINTTL NUMBER {
14583b3a8eb9SGleb Smirnoff 			if (scrub_opts.marker & SOM_MINTTL) {
14593b3a8eb9SGleb Smirnoff 				yyerror("min-ttl cannot be respecified");
14603b3a8eb9SGleb Smirnoff 				YYERROR;
14613b3a8eb9SGleb Smirnoff 			}
14623b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > 255) {
14633b3a8eb9SGleb Smirnoff 				yyerror("illegal min-ttl value %d", $2);
14643b3a8eb9SGleb Smirnoff 				YYERROR;
14653b3a8eb9SGleb Smirnoff 			}
14663b3a8eb9SGleb Smirnoff 			scrub_opts.marker |= SOM_MINTTL;
14673b3a8eb9SGleb Smirnoff 			scrub_opts.minttl = $2;
14683b3a8eb9SGleb Smirnoff 		}
14693b3a8eb9SGleb Smirnoff 		| MAXMSS NUMBER {
14703b3a8eb9SGleb Smirnoff 			if (scrub_opts.marker & SOM_MAXMSS) {
14713b3a8eb9SGleb Smirnoff 				yyerror("max-mss cannot be respecified");
14723b3a8eb9SGleb Smirnoff 				YYERROR;
14733b3a8eb9SGleb Smirnoff 			}
14743b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > 65535) {
14753b3a8eb9SGleb Smirnoff 				yyerror("illegal max-mss value %d", $2);
14763b3a8eb9SGleb Smirnoff 				YYERROR;
14773b3a8eb9SGleb Smirnoff 			}
14783b3a8eb9SGleb Smirnoff 			scrub_opts.marker |= SOM_MAXMSS;
14793b3a8eb9SGleb Smirnoff 			scrub_opts.maxmss = $2;
14803b3a8eb9SGleb Smirnoff 		}
14813b3a8eb9SGleb Smirnoff 		| SETTOS tos {
14823b3a8eb9SGleb Smirnoff 			if (scrub_opts.marker & SOM_SETTOS) {
14833b3a8eb9SGleb Smirnoff 				yyerror("set-tos cannot be respecified");
14843b3a8eb9SGleb Smirnoff 				YYERROR;
14853b3a8eb9SGleb Smirnoff 			}
14863b3a8eb9SGleb Smirnoff 			scrub_opts.marker |= SOM_SETTOS;
14873b3a8eb9SGleb Smirnoff 			scrub_opts.settos = $2;
14883b3a8eb9SGleb Smirnoff 		}
14893b3a8eb9SGleb Smirnoff 		| fragcache {
14903b3a8eb9SGleb Smirnoff 			if (scrub_opts.marker & SOM_FRAGCACHE) {
14913b3a8eb9SGleb Smirnoff 				yyerror("fragcache cannot be respecified");
14923b3a8eb9SGleb Smirnoff 				YYERROR;
14933b3a8eb9SGleb Smirnoff 			}
14943b3a8eb9SGleb Smirnoff 			scrub_opts.marker |= SOM_FRAGCACHE;
14953b3a8eb9SGleb Smirnoff 			scrub_opts.fragcache = $1;
14963b3a8eb9SGleb Smirnoff 		}
14973b3a8eb9SGleb Smirnoff 		| REASSEMBLE STRING {
14983b3a8eb9SGleb Smirnoff 			if (strcasecmp($2, "tcp") != 0) {
14993b3a8eb9SGleb Smirnoff 				yyerror("scrub reassemble supports only tcp, "
15003b3a8eb9SGleb Smirnoff 				    "not '%s'", $2);
15013b3a8eb9SGleb Smirnoff 				free($2);
15023b3a8eb9SGleb Smirnoff 				YYERROR;
15033b3a8eb9SGleb Smirnoff 			}
15043b3a8eb9SGleb Smirnoff 			free($2);
15053b3a8eb9SGleb Smirnoff 			if (scrub_opts.reassemble_tcp) {
15063b3a8eb9SGleb Smirnoff 				yyerror("reassemble tcp cannot be respecified");
15073b3a8eb9SGleb Smirnoff 				YYERROR;
15083b3a8eb9SGleb Smirnoff 			}
15093b3a8eb9SGleb Smirnoff 			scrub_opts.reassemble_tcp = 1;
15103b3a8eb9SGleb Smirnoff 		}
15113b3a8eb9SGleb Smirnoff 		| RANDOMID {
15123b3a8eb9SGleb Smirnoff 			if (scrub_opts.randomid) {
15133b3a8eb9SGleb Smirnoff 				yyerror("random-id cannot be respecified");
15143b3a8eb9SGleb Smirnoff 				YYERROR;
15153b3a8eb9SGleb Smirnoff 			}
15163b3a8eb9SGleb Smirnoff 			scrub_opts.randomid = 1;
15173b3a8eb9SGleb Smirnoff 		}
15183b3a8eb9SGleb Smirnoff 		| RTABLE NUMBER				{
15193b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > rt_tableid_max()) {
15203b3a8eb9SGleb Smirnoff 				yyerror("invalid rtable id");
15213b3a8eb9SGleb Smirnoff 				YYERROR;
15223b3a8eb9SGleb Smirnoff 			}
15233b3a8eb9SGleb Smirnoff 			scrub_opts.rtableid = $2;
15243b3a8eb9SGleb Smirnoff 		}
15253b3a8eb9SGleb Smirnoff 		| not TAGGED string			{
15263b3a8eb9SGleb Smirnoff 			scrub_opts.match_tag = $3;
15273b3a8eb9SGleb Smirnoff 			scrub_opts.match_tag_not = $1;
15283b3a8eb9SGleb Smirnoff 		}
15293b3a8eb9SGleb Smirnoff 		;
15303b3a8eb9SGleb Smirnoff 
15313b3a8eb9SGleb Smirnoff fragcache	: FRAGMENT REASSEMBLE		{ $$ = 0; /* default */ }
153257e047e5SKristof Provost 		| FRAGMENT NO REASSEMBLE	{ $$ = PFRULE_FRAGMENT_NOREASS; }
15333b3a8eb9SGleb Smirnoff 		;
15343b3a8eb9SGleb Smirnoff 
15353b3a8eb9SGleb Smirnoff antispoof	: ANTISPOOF logquick antispoof_ifspc af antispoof_opts {
1536e9eb0941SKristof Provost 			struct pfctl_rule	 r;
15373b3a8eb9SGleb Smirnoff 			struct node_host	*h = NULL, *hh;
15383b3a8eb9SGleb Smirnoff 			struct node_if		*i, *j;
15393b3a8eb9SGleb Smirnoff 
15403b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_FILTER))
15413b3a8eb9SGleb Smirnoff 				YYERROR;
15423b3a8eb9SGleb Smirnoff 
15433b3a8eb9SGleb Smirnoff 			for (i = $3; i; i = i->next) {
15443b3a8eb9SGleb Smirnoff 				bzero(&r, sizeof(r));
15453b3a8eb9SGleb Smirnoff 
15463b3a8eb9SGleb Smirnoff 				r.action = PF_DROP;
15473b3a8eb9SGleb Smirnoff 				r.direction = PF_IN;
15483b3a8eb9SGleb Smirnoff 				r.log = $2.log;
15493b3a8eb9SGleb Smirnoff 				r.logif = $2.logif;
15503b3a8eb9SGleb Smirnoff 				r.quick = $2.quick;
15513b3a8eb9SGleb Smirnoff 				r.af = $4;
155276c5eeccSKristof Provost 				r.ridentifier = $5.ridentifier;
15533b3a8eb9SGleb Smirnoff 				if (rule_label(&r, $5.label))
15543b3a8eb9SGleb Smirnoff 					YYERROR;
15553b3a8eb9SGleb Smirnoff 				r.rtableid = $5.rtableid;
15563b3a8eb9SGleb Smirnoff 				j = calloc(1, sizeof(struct node_if));
15573b3a8eb9SGleb Smirnoff 				if (j == NULL)
15583b3a8eb9SGleb Smirnoff 					err(1, "antispoof: calloc");
15593b3a8eb9SGleb Smirnoff 				if (strlcpy(j->ifname, i->ifname,
15603b3a8eb9SGleb Smirnoff 				    sizeof(j->ifname)) >= sizeof(j->ifname)) {
15613b3a8eb9SGleb Smirnoff 					free(j);
15623b3a8eb9SGleb Smirnoff 					yyerror("interface name too long");
15633b3a8eb9SGleb Smirnoff 					YYERROR;
15643b3a8eb9SGleb Smirnoff 				}
15653b3a8eb9SGleb Smirnoff 				j->not = 1;
15663b3a8eb9SGleb Smirnoff 				if (i->dynamic) {
15673b3a8eb9SGleb Smirnoff 					h = calloc(1, sizeof(*h));
15683b3a8eb9SGleb Smirnoff 					if (h == NULL)
15693b3a8eb9SGleb Smirnoff 						err(1, "address: calloc");
15703b3a8eb9SGleb Smirnoff 					h->addr.type = PF_ADDR_DYNIFTL;
15713b3a8eb9SGleb Smirnoff 					set_ipmask(h, 128);
15723b3a8eb9SGleb Smirnoff 					if (strlcpy(h->addr.v.ifname, i->ifname,
15733b3a8eb9SGleb Smirnoff 					    sizeof(h->addr.v.ifname)) >=
15743b3a8eb9SGleb Smirnoff 					    sizeof(h->addr.v.ifname)) {
15753b3a8eb9SGleb Smirnoff 						free(h);
15763b3a8eb9SGleb Smirnoff 						yyerror(
15773b3a8eb9SGleb Smirnoff 						    "interface name too long");
15783b3a8eb9SGleb Smirnoff 						YYERROR;
15793b3a8eb9SGleb Smirnoff 					}
15803b3a8eb9SGleb Smirnoff 					hh = malloc(sizeof(*hh));
15813b3a8eb9SGleb Smirnoff 					if (hh == NULL)
15823b3a8eb9SGleb Smirnoff 						 err(1, "address: malloc");
15833b3a8eb9SGleb Smirnoff 					bcopy(h, hh, sizeof(*hh));
15843b3a8eb9SGleb Smirnoff 					h->addr.iflags = PFI_AFLAG_NETWORK;
15853b3a8eb9SGleb Smirnoff 				} else {
15863b3a8eb9SGleb Smirnoff 					h = ifa_lookup(j->ifname,
15873b3a8eb9SGleb Smirnoff 					    PFI_AFLAG_NETWORK);
15883b3a8eb9SGleb Smirnoff 					hh = NULL;
15893b3a8eb9SGleb Smirnoff 				}
15903b3a8eb9SGleb Smirnoff 
15913b3a8eb9SGleb Smirnoff 				if (h != NULL)
15923b3a8eb9SGleb Smirnoff 					expand_rule(&r, j, NULL, NULL, NULL, h,
15933b3a8eb9SGleb Smirnoff 					    NULL, NULL, NULL, NULL, NULL,
15943b3a8eb9SGleb Smirnoff 					    NULL, "");
15953b3a8eb9SGleb Smirnoff 
15963b3a8eb9SGleb Smirnoff 				if ((i->ifa_flags & IFF_LOOPBACK) == 0) {
15973b3a8eb9SGleb Smirnoff 					bzero(&r, sizeof(r));
15983b3a8eb9SGleb Smirnoff 
15993b3a8eb9SGleb Smirnoff 					r.action = PF_DROP;
16003b3a8eb9SGleb Smirnoff 					r.direction = PF_IN;
16013b3a8eb9SGleb Smirnoff 					r.log = $2.log;
16023b3a8eb9SGleb Smirnoff 					r.logif = $2.logif;
16033b3a8eb9SGleb Smirnoff 					r.quick = $2.quick;
16043b3a8eb9SGleb Smirnoff 					r.af = $4;
160576c5eeccSKristof Provost 					r.ridentifier = $5.ridentifier;
16063b3a8eb9SGleb Smirnoff 					if (rule_label(&r, $5.label))
16073b3a8eb9SGleb Smirnoff 						YYERROR;
16083b3a8eb9SGleb Smirnoff 					r.rtableid = $5.rtableid;
16093b3a8eb9SGleb Smirnoff 					if (hh != NULL)
16103b3a8eb9SGleb Smirnoff 						h = hh;
16113b3a8eb9SGleb Smirnoff 					else
16123b3a8eb9SGleb Smirnoff 						h = ifa_lookup(i->ifname, 0);
16133b3a8eb9SGleb Smirnoff 					if (h != NULL)
16143b3a8eb9SGleb Smirnoff 						expand_rule(&r, NULL, NULL,
16153b3a8eb9SGleb Smirnoff 						    NULL, NULL, h, NULL, NULL,
16163b3a8eb9SGleb Smirnoff 						    NULL, NULL, NULL, NULL, "");
16173b3a8eb9SGleb Smirnoff 				} else
16183b3a8eb9SGleb Smirnoff 					free(hh);
16193b3a8eb9SGleb Smirnoff 			}
16206fcc8e04SKristof Provost 			for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++)
16216fcc8e04SKristof Provost 				free($5.label[i]);
16223b3a8eb9SGleb Smirnoff 		}
16233b3a8eb9SGleb Smirnoff 		;
16243b3a8eb9SGleb Smirnoff 
16253b3a8eb9SGleb Smirnoff antispoof_ifspc	: FOR antispoof_if			{ $$ = $2; }
16263b3a8eb9SGleb Smirnoff 		| FOR '{' optnl antispoof_iflst '}'	{ $$ = $4; }
16273b3a8eb9SGleb Smirnoff 		;
16283b3a8eb9SGleb Smirnoff 
16293b3a8eb9SGleb Smirnoff antispoof_iflst	: antispoof_if optnl			{ $$ = $1; }
16303b3a8eb9SGleb Smirnoff 		| antispoof_iflst comma antispoof_if optnl {
16313b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
16323b3a8eb9SGleb Smirnoff 			$1->tail = $3;
16333b3a8eb9SGleb Smirnoff 			$$ = $1;
16343b3a8eb9SGleb Smirnoff 		}
16353b3a8eb9SGleb Smirnoff 		;
16363b3a8eb9SGleb Smirnoff 
16373b3a8eb9SGleb Smirnoff antispoof_if	: if_item				{ $$ = $1; }
16383b3a8eb9SGleb Smirnoff 		| '(' if_item ')'			{
16393b3a8eb9SGleb Smirnoff 			$2->dynamic = 1;
16403b3a8eb9SGleb Smirnoff 			$$ = $2;
16413b3a8eb9SGleb Smirnoff 		}
16423b3a8eb9SGleb Smirnoff 		;
16433b3a8eb9SGleb Smirnoff 
16443b3a8eb9SGleb Smirnoff antispoof_opts	:	{
16453b3a8eb9SGleb Smirnoff 				bzero(&antispoof_opts, sizeof antispoof_opts);
16463b3a8eb9SGleb Smirnoff 				antispoof_opts.rtableid = -1;
16473b3a8eb9SGleb Smirnoff 			}
16483b3a8eb9SGleb Smirnoff 		    antispoof_opts_l
16493b3a8eb9SGleb Smirnoff 			{ $$ = antispoof_opts; }
16503b3a8eb9SGleb Smirnoff 		| /* empty */	{
16513b3a8eb9SGleb Smirnoff 			bzero(&antispoof_opts, sizeof antispoof_opts);
16523b3a8eb9SGleb Smirnoff 			antispoof_opts.rtableid = -1;
16533b3a8eb9SGleb Smirnoff 			$$ = antispoof_opts;
16543b3a8eb9SGleb Smirnoff 		}
16553b3a8eb9SGleb Smirnoff 		;
16563b3a8eb9SGleb Smirnoff 
16573b3a8eb9SGleb Smirnoff antispoof_opts_l	: antispoof_opts_l antispoof_opt
16583b3a8eb9SGleb Smirnoff 			| antispoof_opt
16593b3a8eb9SGleb Smirnoff 			;
16603b3a8eb9SGleb Smirnoff 
16613b3a8eb9SGleb Smirnoff antispoof_opt	: label	{
16626fcc8e04SKristof Provost 			if (antispoof_opts.labelcount >= PF_RULE_MAX_LABEL_COUNT) {
16636fcc8e04SKristof Provost 				yyerror("label can only be used %d times", PF_RULE_MAX_LABEL_COUNT);
16643b3a8eb9SGleb Smirnoff 				YYERROR;
16653b3a8eb9SGleb Smirnoff 			}
16666fcc8e04SKristof Provost 			antispoof_opts.label[antispoof_opts.labelcount++] = $1;
16673b3a8eb9SGleb Smirnoff 		}
166876c5eeccSKristof Provost 		| RIDENTIFIER number {
166976c5eeccSKristof Provost 			antispoof_opts.ridentifier = $2;
167076c5eeccSKristof Provost 		}
16713b3a8eb9SGleb Smirnoff 		| RTABLE NUMBER				{
16723b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > rt_tableid_max()) {
16733b3a8eb9SGleb Smirnoff 				yyerror("invalid rtable id");
16743b3a8eb9SGleb Smirnoff 				YYERROR;
16753b3a8eb9SGleb Smirnoff 			}
16763b3a8eb9SGleb Smirnoff 			antispoof_opts.rtableid = $2;
16773b3a8eb9SGleb Smirnoff 		}
16783b3a8eb9SGleb Smirnoff 		;
16793b3a8eb9SGleb Smirnoff 
16803b3a8eb9SGleb Smirnoff not		: '!'		{ $$ = 1; }
16813b3a8eb9SGleb Smirnoff 		| /* empty */	{ $$ = 0; }
16823b3a8eb9SGleb Smirnoff 		;
16833b3a8eb9SGleb Smirnoff 
16843b3a8eb9SGleb Smirnoff tabledef	: TABLE '<' STRING '>' table_opts {
16853b3a8eb9SGleb Smirnoff 			struct node_host	 *h, *nh;
16863b3a8eb9SGleb Smirnoff 			struct node_tinit	 *ti, *nti;
16873b3a8eb9SGleb Smirnoff 
16883b3a8eb9SGleb Smirnoff 			if (strlen($3) >= PF_TABLE_NAME_SIZE) {
16893b3a8eb9SGleb Smirnoff 				yyerror("table name too long, max %d chars",
16903b3a8eb9SGleb Smirnoff 				    PF_TABLE_NAME_SIZE - 1);
16913b3a8eb9SGleb Smirnoff 				free($3);
16923b3a8eb9SGleb Smirnoff 				YYERROR;
16933b3a8eb9SGleb Smirnoff 			}
16943b3a8eb9SGleb Smirnoff 			if (pf->loadopt & PFCTL_FLAG_TABLE)
16953b3a8eb9SGleb Smirnoff 				if (process_tabledef($3, &$5)) {
16963b3a8eb9SGleb Smirnoff 					free($3);
16973b3a8eb9SGleb Smirnoff 					YYERROR;
16983b3a8eb9SGleb Smirnoff 				}
16993b3a8eb9SGleb Smirnoff 			free($3);
17003b3a8eb9SGleb Smirnoff 			for (ti = SIMPLEQ_FIRST(&$5.init_nodes);
17013b3a8eb9SGleb Smirnoff 			    ti != SIMPLEQ_END(&$5.init_nodes); ti = nti) {
17023b3a8eb9SGleb Smirnoff 				if (ti->file)
17033b3a8eb9SGleb Smirnoff 					free(ti->file);
17043b3a8eb9SGleb Smirnoff 				for (h = ti->host; h != NULL; h = nh) {
17053b3a8eb9SGleb Smirnoff 					nh = h->next;
17063b3a8eb9SGleb Smirnoff 					free(h);
17073b3a8eb9SGleb Smirnoff 				}
17083b3a8eb9SGleb Smirnoff 				nti = SIMPLEQ_NEXT(ti, entries);
17093b3a8eb9SGleb Smirnoff 				free(ti);
17103b3a8eb9SGleb Smirnoff 			}
17113b3a8eb9SGleb Smirnoff 		}
17123b3a8eb9SGleb Smirnoff 		;
17133b3a8eb9SGleb Smirnoff 
17143b3a8eb9SGleb Smirnoff table_opts	:	{
17153b3a8eb9SGleb Smirnoff 			bzero(&table_opts, sizeof table_opts);
17163b3a8eb9SGleb Smirnoff 			SIMPLEQ_INIT(&table_opts.init_nodes);
17173b3a8eb9SGleb Smirnoff 		}
17183b3a8eb9SGleb Smirnoff 		    table_opts_l
17193b3a8eb9SGleb Smirnoff 			{ $$ = table_opts; }
17203b3a8eb9SGleb Smirnoff 		| /* empty */
17213b3a8eb9SGleb Smirnoff 			{
17223b3a8eb9SGleb Smirnoff 			bzero(&table_opts, sizeof table_opts);
17233b3a8eb9SGleb Smirnoff 			SIMPLEQ_INIT(&table_opts.init_nodes);
17243b3a8eb9SGleb Smirnoff 			$$ = table_opts;
17253b3a8eb9SGleb Smirnoff 		}
17263b3a8eb9SGleb Smirnoff 		;
17273b3a8eb9SGleb Smirnoff 
17283b3a8eb9SGleb Smirnoff table_opts_l	: table_opts_l table_opt
17293b3a8eb9SGleb Smirnoff 		| table_opt
17303b3a8eb9SGleb Smirnoff 		;
17313b3a8eb9SGleb Smirnoff 
17323b3a8eb9SGleb Smirnoff table_opt	: STRING		{
17333b3a8eb9SGleb Smirnoff 			if (!strcmp($1, "const"))
17343b3a8eb9SGleb Smirnoff 				table_opts.flags |= PFR_TFLAG_CONST;
17353b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "persist"))
17363b3a8eb9SGleb Smirnoff 				table_opts.flags |= PFR_TFLAG_PERSIST;
17373b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "counters"))
17383b3a8eb9SGleb Smirnoff 				table_opts.flags |= PFR_TFLAG_COUNTERS;
17393b3a8eb9SGleb Smirnoff 			else {
17403b3a8eb9SGleb Smirnoff 				yyerror("invalid table option '%s'", $1);
17413b3a8eb9SGleb Smirnoff 				free($1);
17423b3a8eb9SGleb Smirnoff 				YYERROR;
17433b3a8eb9SGleb Smirnoff 			}
17443b3a8eb9SGleb Smirnoff 			free($1);
17453b3a8eb9SGleb Smirnoff 		}
17463b3a8eb9SGleb Smirnoff 		| '{' optnl '}'		{ table_opts.init_addr = 1; }
17473b3a8eb9SGleb Smirnoff 		| '{' optnl host_list '}'	{
17483b3a8eb9SGleb Smirnoff 			struct node_host	*n;
17493b3a8eb9SGleb Smirnoff 			struct node_tinit	*ti;
17503b3a8eb9SGleb Smirnoff 
17513b3a8eb9SGleb Smirnoff 			for (n = $3; n != NULL; n = n->next) {
17523b3a8eb9SGleb Smirnoff 				switch (n->addr.type) {
17533b3a8eb9SGleb Smirnoff 				case PF_ADDR_ADDRMASK:
17543b3a8eb9SGleb Smirnoff 					continue; /* ok */
17553b3a8eb9SGleb Smirnoff 				case PF_ADDR_RANGE:
17563b3a8eb9SGleb Smirnoff 					yyerror("address ranges are not "
17573b3a8eb9SGleb Smirnoff 					    "permitted inside tables");
17583b3a8eb9SGleb Smirnoff 					break;
17593b3a8eb9SGleb Smirnoff 				case PF_ADDR_DYNIFTL:
17603b3a8eb9SGleb Smirnoff 					yyerror("dynamic addresses are not "
17613b3a8eb9SGleb Smirnoff 					    "permitted inside tables");
17623b3a8eb9SGleb Smirnoff 					break;
17633b3a8eb9SGleb Smirnoff 				case PF_ADDR_TABLE:
17643b3a8eb9SGleb Smirnoff 					yyerror("tables cannot contain tables");
17653b3a8eb9SGleb Smirnoff 					break;
17663b3a8eb9SGleb Smirnoff 				case PF_ADDR_NOROUTE:
17673b3a8eb9SGleb Smirnoff 					yyerror("\"no-route\" is not permitted "
17683b3a8eb9SGleb Smirnoff 					    "inside tables");
17693b3a8eb9SGleb Smirnoff 					break;
17703b3a8eb9SGleb Smirnoff 				case PF_ADDR_URPFFAILED:
17713b3a8eb9SGleb Smirnoff 					yyerror("\"urpf-failed\" is not "
17723b3a8eb9SGleb Smirnoff 					    "permitted inside tables");
17733b3a8eb9SGleb Smirnoff 					break;
17743b3a8eb9SGleb Smirnoff 				default:
17753b3a8eb9SGleb Smirnoff 					yyerror("unknown address type %d",
17763b3a8eb9SGleb Smirnoff 					    n->addr.type);
17773b3a8eb9SGleb Smirnoff 				}
17783b3a8eb9SGleb Smirnoff 				YYERROR;
17793b3a8eb9SGleb Smirnoff 			}
17803b3a8eb9SGleb Smirnoff 			if (!(ti = calloc(1, sizeof(*ti))))
17813b3a8eb9SGleb Smirnoff 				err(1, "table_opt: calloc");
17823b3a8eb9SGleb Smirnoff 			ti->host = $3;
17833b3a8eb9SGleb Smirnoff 			SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
17843b3a8eb9SGleb Smirnoff 			    entries);
17853b3a8eb9SGleb Smirnoff 			table_opts.init_addr = 1;
17863b3a8eb9SGleb Smirnoff 		}
17873b3a8eb9SGleb Smirnoff 		| FILENAME STRING	{
17883b3a8eb9SGleb Smirnoff 			struct node_tinit	*ti;
17893b3a8eb9SGleb Smirnoff 
17903b3a8eb9SGleb Smirnoff 			if (!(ti = calloc(1, sizeof(*ti))))
17913b3a8eb9SGleb Smirnoff 				err(1, "table_opt: calloc");
17923b3a8eb9SGleb Smirnoff 			ti->file = $2;
17933b3a8eb9SGleb Smirnoff 			SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
17943b3a8eb9SGleb Smirnoff 			    entries);
17953b3a8eb9SGleb Smirnoff 			table_opts.init_addr = 1;
17963b3a8eb9SGleb Smirnoff 		}
17973b3a8eb9SGleb Smirnoff 		;
17983b3a8eb9SGleb Smirnoff 
17993b3a8eb9SGleb Smirnoff altqif		: ALTQ interface queue_opts QUEUE qassign {
18003b3a8eb9SGleb Smirnoff 			struct pf_altq	a;
18013b3a8eb9SGleb Smirnoff 
18023b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_QUEUE))
18033b3a8eb9SGleb Smirnoff 				YYERROR;
18043b3a8eb9SGleb Smirnoff 
18053b3a8eb9SGleb Smirnoff 			memset(&a, 0, sizeof(a));
18063b3a8eb9SGleb Smirnoff 			if ($3.scheduler.qtype == ALTQT_NONE) {
18073b3a8eb9SGleb Smirnoff 				yyerror("no scheduler specified!");
18083b3a8eb9SGleb Smirnoff 				YYERROR;
18093b3a8eb9SGleb Smirnoff 			}
18103b3a8eb9SGleb Smirnoff 			a.scheduler = $3.scheduler.qtype;
18113b3a8eb9SGleb Smirnoff 			a.qlimit = $3.qlimit;
18123b3a8eb9SGleb Smirnoff 			a.tbrsize = $3.tbrsize;
18130a70aaf8SLuiz Otavio O Souza 			if ($5 == NULL && $3.scheduler.qtype != ALTQT_CODEL) {
18143b3a8eb9SGleb Smirnoff 				yyerror("no child queues specified");
18153b3a8eb9SGleb Smirnoff 				YYERROR;
18163b3a8eb9SGleb Smirnoff 			}
18173b3a8eb9SGleb Smirnoff 			if (expand_altq(&a, $2, $5, $3.queue_bwspec,
18183b3a8eb9SGleb Smirnoff 			    &$3.scheduler))
18193b3a8eb9SGleb Smirnoff 				YYERROR;
18203b3a8eb9SGleb Smirnoff 		}
18213b3a8eb9SGleb Smirnoff 		;
18223b3a8eb9SGleb Smirnoff 
18233b3a8eb9SGleb Smirnoff queuespec	: QUEUE STRING interface queue_opts qassign {
18243b3a8eb9SGleb Smirnoff 			struct pf_altq	a;
18253b3a8eb9SGleb Smirnoff 
18263b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_QUEUE)) {
18273b3a8eb9SGleb Smirnoff 				free($2);
18283b3a8eb9SGleb Smirnoff 				YYERROR;
18293b3a8eb9SGleb Smirnoff 			}
18303b3a8eb9SGleb Smirnoff 
18313b3a8eb9SGleb Smirnoff 			memset(&a, 0, sizeof(a));
18323b3a8eb9SGleb Smirnoff 
18333b3a8eb9SGleb Smirnoff 			if (strlcpy(a.qname, $2, sizeof(a.qname)) >=
18343b3a8eb9SGleb Smirnoff 			    sizeof(a.qname)) {
18353b3a8eb9SGleb Smirnoff 				yyerror("queue name too long (max "
18363b3a8eb9SGleb Smirnoff 				    "%d chars)", PF_QNAME_SIZE-1);
18373b3a8eb9SGleb Smirnoff 				free($2);
18383b3a8eb9SGleb Smirnoff 				YYERROR;
18393b3a8eb9SGleb Smirnoff 			}
18403b3a8eb9SGleb Smirnoff 			free($2);
18413b3a8eb9SGleb Smirnoff 			if ($4.tbrsize) {
18423b3a8eb9SGleb Smirnoff 				yyerror("cannot specify tbrsize for queue");
18433b3a8eb9SGleb Smirnoff 				YYERROR;
18443b3a8eb9SGleb Smirnoff 			}
18453b3a8eb9SGleb Smirnoff 			if ($4.priority > 255) {
18463b3a8eb9SGleb Smirnoff 				yyerror("priority out of range: max 255");
18473b3a8eb9SGleb Smirnoff 				YYERROR;
18483b3a8eb9SGleb Smirnoff 			}
18493b3a8eb9SGleb Smirnoff 			a.priority = $4.priority;
18503b3a8eb9SGleb Smirnoff 			a.qlimit = $4.qlimit;
18513b3a8eb9SGleb Smirnoff 			a.scheduler = $4.scheduler.qtype;
18523b3a8eb9SGleb Smirnoff 			if (expand_queue(&a, $3, $5, $4.queue_bwspec,
18533b3a8eb9SGleb Smirnoff 			    &$4.scheduler)) {
18543b3a8eb9SGleb Smirnoff 				yyerror("errors in queue definition");
18553b3a8eb9SGleb Smirnoff 				YYERROR;
18563b3a8eb9SGleb Smirnoff 			}
18573b3a8eb9SGleb Smirnoff 		}
18583b3a8eb9SGleb Smirnoff 		;
18593b3a8eb9SGleb Smirnoff 
18603b3a8eb9SGleb Smirnoff queue_opts	:	{
18613b3a8eb9SGleb Smirnoff 			bzero(&queue_opts, sizeof queue_opts);
18623b3a8eb9SGleb Smirnoff 			queue_opts.priority = DEFAULT_PRIORITY;
18633b3a8eb9SGleb Smirnoff 			queue_opts.qlimit = DEFAULT_QLIMIT;
18643b3a8eb9SGleb Smirnoff 			queue_opts.scheduler.qtype = ALTQT_NONE;
18653b3a8eb9SGleb Smirnoff 			queue_opts.queue_bwspec.bw_percent = 100;
18663b3a8eb9SGleb Smirnoff 		}
18673b3a8eb9SGleb Smirnoff 		    queue_opts_l
18683b3a8eb9SGleb Smirnoff 			{ $$ = queue_opts; }
18693b3a8eb9SGleb Smirnoff 		| /* empty */ {
18703b3a8eb9SGleb Smirnoff 			bzero(&queue_opts, sizeof queue_opts);
18713b3a8eb9SGleb Smirnoff 			queue_opts.priority = DEFAULT_PRIORITY;
18723b3a8eb9SGleb Smirnoff 			queue_opts.qlimit = DEFAULT_QLIMIT;
18733b3a8eb9SGleb Smirnoff 			queue_opts.scheduler.qtype = ALTQT_NONE;
18743b3a8eb9SGleb Smirnoff 			queue_opts.queue_bwspec.bw_percent = 100;
18753b3a8eb9SGleb Smirnoff 			$$ = queue_opts;
18763b3a8eb9SGleb Smirnoff 		}
18773b3a8eb9SGleb Smirnoff 		;
18783b3a8eb9SGleb Smirnoff 
18793b3a8eb9SGleb Smirnoff queue_opts_l	: queue_opts_l queue_opt
18803b3a8eb9SGleb Smirnoff 		| queue_opt
18813b3a8eb9SGleb Smirnoff 		;
18823b3a8eb9SGleb Smirnoff 
18833b3a8eb9SGleb Smirnoff queue_opt	: BANDWIDTH bandwidth	{
18843b3a8eb9SGleb Smirnoff 			if (queue_opts.marker & QOM_BWSPEC) {
18853b3a8eb9SGleb Smirnoff 				yyerror("bandwidth cannot be respecified");
18863b3a8eb9SGleb Smirnoff 				YYERROR;
18873b3a8eb9SGleb Smirnoff 			}
18883b3a8eb9SGleb Smirnoff 			queue_opts.marker |= QOM_BWSPEC;
18893b3a8eb9SGleb Smirnoff 			queue_opts.queue_bwspec = $2;
18903b3a8eb9SGleb Smirnoff 		}
18913b3a8eb9SGleb Smirnoff 		| PRIORITY NUMBER	{
18923b3a8eb9SGleb Smirnoff 			if (queue_opts.marker & QOM_PRIORITY) {
18933b3a8eb9SGleb Smirnoff 				yyerror("priority cannot be respecified");
18943b3a8eb9SGleb Smirnoff 				YYERROR;
18953b3a8eb9SGleb Smirnoff 			}
18963b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > 255) {
18973b3a8eb9SGleb Smirnoff 				yyerror("priority out of range: max 255");
18983b3a8eb9SGleb Smirnoff 				YYERROR;
18993b3a8eb9SGleb Smirnoff 			}
19003b3a8eb9SGleb Smirnoff 			queue_opts.marker |= QOM_PRIORITY;
19013b3a8eb9SGleb Smirnoff 			queue_opts.priority = $2;
19023b3a8eb9SGleb Smirnoff 		}
19033b3a8eb9SGleb Smirnoff 		| QLIMIT NUMBER	{
19043b3a8eb9SGleb Smirnoff 			if (queue_opts.marker & QOM_QLIMIT) {
19053b3a8eb9SGleb Smirnoff 				yyerror("qlimit cannot be respecified");
19063b3a8eb9SGleb Smirnoff 				YYERROR;
19073b3a8eb9SGleb Smirnoff 			}
19083b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > 65535) {
19093b3a8eb9SGleb Smirnoff 				yyerror("qlimit out of range: max 65535");
19103b3a8eb9SGleb Smirnoff 				YYERROR;
19113b3a8eb9SGleb Smirnoff 			}
19123b3a8eb9SGleb Smirnoff 			queue_opts.marker |= QOM_QLIMIT;
19133b3a8eb9SGleb Smirnoff 			queue_opts.qlimit = $2;
19143b3a8eb9SGleb Smirnoff 		}
19153b3a8eb9SGleb Smirnoff 		| scheduler	{
19163b3a8eb9SGleb Smirnoff 			if (queue_opts.marker & QOM_SCHEDULER) {
19173b3a8eb9SGleb Smirnoff 				yyerror("scheduler cannot be respecified");
19183b3a8eb9SGleb Smirnoff 				YYERROR;
19193b3a8eb9SGleb Smirnoff 			}
19203b3a8eb9SGleb Smirnoff 			queue_opts.marker |= QOM_SCHEDULER;
19213b3a8eb9SGleb Smirnoff 			queue_opts.scheduler = $1;
19223b3a8eb9SGleb Smirnoff 		}
19233b3a8eb9SGleb Smirnoff 		| TBRSIZE NUMBER	{
19243b3a8eb9SGleb Smirnoff 			if (queue_opts.marker & QOM_TBRSIZE) {
19253b3a8eb9SGleb Smirnoff 				yyerror("tbrsize cannot be respecified");
19263b3a8eb9SGleb Smirnoff 				YYERROR;
19273b3a8eb9SGleb Smirnoff 			}
1928249cc75fSPatrick Kelsey 			if ($2 < 0 || $2 > UINT_MAX) {
1929249cc75fSPatrick Kelsey 				yyerror("tbrsize too big: max %u", UINT_MAX);
19303b3a8eb9SGleb Smirnoff 				YYERROR;
19313b3a8eb9SGleb Smirnoff 			}
19323b3a8eb9SGleb Smirnoff 			queue_opts.marker |= QOM_TBRSIZE;
19333b3a8eb9SGleb Smirnoff 			queue_opts.tbrsize = $2;
19343b3a8eb9SGleb Smirnoff 		}
19353b3a8eb9SGleb Smirnoff 		;
19363b3a8eb9SGleb Smirnoff 
19373b3a8eb9SGleb Smirnoff bandwidth	: STRING {
19383b3a8eb9SGleb Smirnoff 			double	 bps;
19393b3a8eb9SGleb Smirnoff 			char	*cp;
19403b3a8eb9SGleb Smirnoff 
19413b3a8eb9SGleb Smirnoff 			$$.bw_percent = 0;
19423b3a8eb9SGleb Smirnoff 
19433b3a8eb9SGleb Smirnoff 			bps = strtod($1, &cp);
19443b3a8eb9SGleb Smirnoff 			if (cp != NULL) {
1945db1bbde6SLuiz Otavio O Souza 				if (strlen(cp) > 1) {
1946db1bbde6SLuiz Otavio O Souza 					char *cu = cp + 1;
1947db1bbde6SLuiz Otavio O Souza 					if (!strcmp(cu, "Bit") ||
1948db1bbde6SLuiz Otavio O Souza 					    !strcmp(cu, "B") ||
1949db1bbde6SLuiz Otavio O Souza 					    !strcmp(cu, "bit") ||
1950db1bbde6SLuiz Otavio O Souza 					    !strcmp(cu, "b")) {
1951db1bbde6SLuiz Otavio O Souza 						*cu = 0;
1952db1bbde6SLuiz Otavio O Souza 					}
1953db1bbde6SLuiz Otavio O Souza 				}
19543b3a8eb9SGleb Smirnoff 				if (!strcmp(cp, "b"))
19553b3a8eb9SGleb Smirnoff 					; /* nothing */
1956db1bbde6SLuiz Otavio O Souza 				else if (!strcmp(cp, "K"))
19573b3a8eb9SGleb Smirnoff 					bps *= 1000;
1958db1bbde6SLuiz Otavio O Souza 				else if (!strcmp(cp, "M"))
19593b3a8eb9SGleb Smirnoff 					bps *= 1000 * 1000;
1960db1bbde6SLuiz Otavio O Souza 				else if (!strcmp(cp, "G"))
19613b3a8eb9SGleb Smirnoff 					bps *= 1000 * 1000 * 1000;
19623b3a8eb9SGleb Smirnoff 				else if (!strcmp(cp, "%")) {
19633b3a8eb9SGleb Smirnoff 					if (bps < 0 || bps > 100) {
19643b3a8eb9SGleb Smirnoff 						yyerror("bandwidth spec "
19653b3a8eb9SGleb Smirnoff 						    "out of range");
19663b3a8eb9SGleb Smirnoff 						free($1);
19673b3a8eb9SGleb Smirnoff 						YYERROR;
19683b3a8eb9SGleb Smirnoff 					}
19693b3a8eb9SGleb Smirnoff 					$$.bw_percent = bps;
19703b3a8eb9SGleb Smirnoff 					bps = 0;
19713b3a8eb9SGleb Smirnoff 				} else {
19723b3a8eb9SGleb Smirnoff 					yyerror("unknown unit %s", cp);
19733b3a8eb9SGleb Smirnoff 					free($1);
19743b3a8eb9SGleb Smirnoff 					YYERROR;
19753b3a8eb9SGleb Smirnoff 				}
19763b3a8eb9SGleb Smirnoff 			}
19773b3a8eb9SGleb Smirnoff 			free($1);
1978249cc75fSPatrick Kelsey 			$$.bw_absolute = (u_int64_t)bps;
19793b3a8eb9SGleb Smirnoff 		}
19803b3a8eb9SGleb Smirnoff 		| NUMBER {
1981249cc75fSPatrick Kelsey 			if ($1 < 0 || $1 >= LLONG_MAX) {
19823b3a8eb9SGleb Smirnoff 				yyerror("bandwidth number too big");
19833b3a8eb9SGleb Smirnoff 				YYERROR;
19843b3a8eb9SGleb Smirnoff 			}
19853b3a8eb9SGleb Smirnoff 			$$.bw_percent = 0;
19863b3a8eb9SGleb Smirnoff 			$$.bw_absolute = $1;
19873b3a8eb9SGleb Smirnoff 		}
19883b3a8eb9SGleb Smirnoff 		;
19893b3a8eb9SGleb Smirnoff 
19903b3a8eb9SGleb Smirnoff scheduler	: CBQ				{
19913b3a8eb9SGleb Smirnoff 			$$.qtype = ALTQT_CBQ;
19923b3a8eb9SGleb Smirnoff 			$$.data.cbq_opts.flags = 0;
19933b3a8eb9SGleb Smirnoff 		}
19943b3a8eb9SGleb Smirnoff 		| CBQ '(' cbqflags_list ')'	{
19953b3a8eb9SGleb Smirnoff 			$$.qtype = ALTQT_CBQ;
19963b3a8eb9SGleb Smirnoff 			$$.data.cbq_opts.flags = $3;
19973b3a8eb9SGleb Smirnoff 		}
19983b3a8eb9SGleb Smirnoff 		| PRIQ				{
19993b3a8eb9SGleb Smirnoff 			$$.qtype = ALTQT_PRIQ;
20003b3a8eb9SGleb Smirnoff 			$$.data.priq_opts.flags = 0;
20013b3a8eb9SGleb Smirnoff 		}
20023b3a8eb9SGleb Smirnoff 		| PRIQ '(' priqflags_list ')'	{
20033b3a8eb9SGleb Smirnoff 			$$.qtype = ALTQT_PRIQ;
20043b3a8eb9SGleb Smirnoff 			$$.data.priq_opts.flags = $3;
20053b3a8eb9SGleb Smirnoff 		}
20063b3a8eb9SGleb Smirnoff 		| HFSC				{
20073b3a8eb9SGleb Smirnoff 			$$.qtype = ALTQT_HFSC;
20083b3a8eb9SGleb Smirnoff 			bzero(&$$.data.hfsc_opts,
20093b3a8eb9SGleb Smirnoff 			    sizeof(struct node_hfsc_opts));
20103b3a8eb9SGleb Smirnoff 		}
20113b3a8eb9SGleb Smirnoff 		| HFSC '(' hfsc_opts ')'	{
20123b3a8eb9SGleb Smirnoff 			$$.qtype = ALTQT_HFSC;
20133b3a8eb9SGleb Smirnoff 			$$.data.hfsc_opts = $3;
20143b3a8eb9SGleb Smirnoff 		}
2015a5b789f6SErmal Luçi 		| FAIRQ				{
2016a5b789f6SErmal Luçi 			$$.qtype = ALTQT_FAIRQ;
2017a5b789f6SErmal Luçi 			bzero(&$$.data.fairq_opts,
2018a5b789f6SErmal Luçi 				sizeof(struct node_fairq_opts));
2019a5b789f6SErmal Luçi 		}
2020a5b789f6SErmal Luçi 		| FAIRQ '(' fairq_opts ')'      {
2021a5b789f6SErmal Luçi 			$$.qtype = ALTQT_FAIRQ;
2022a5b789f6SErmal Luçi 			$$.data.fairq_opts = $3;
2023a5b789f6SErmal Luçi 		}
20240a70aaf8SLuiz Otavio O Souza 		| CODEL				{
20250a70aaf8SLuiz Otavio O Souza 			$$.qtype = ALTQT_CODEL;
20260a70aaf8SLuiz Otavio O Souza 			bzero(&$$.data.codel_opts,
20270a70aaf8SLuiz Otavio O Souza 				sizeof(struct codel_opts));
20280a70aaf8SLuiz Otavio O Souza 		}
20290a70aaf8SLuiz Otavio O Souza 		| CODEL '(' codel_opts ')'	{
20300a70aaf8SLuiz Otavio O Souza 			$$.qtype = ALTQT_CODEL;
20310a70aaf8SLuiz Otavio O Souza 			$$.data.codel_opts = $3;
20320a70aaf8SLuiz Otavio O Souza 		}
20333b3a8eb9SGleb Smirnoff 		;
20343b3a8eb9SGleb Smirnoff 
20353b3a8eb9SGleb Smirnoff cbqflags_list	: cbqflags_item				{ $$ |= $1; }
20363b3a8eb9SGleb Smirnoff 		| cbqflags_list comma cbqflags_item	{ $$ |= $3; }
20373b3a8eb9SGleb Smirnoff 		;
20383b3a8eb9SGleb Smirnoff 
20393b3a8eb9SGleb Smirnoff cbqflags_item	: STRING	{
20403b3a8eb9SGleb Smirnoff 			if (!strcmp($1, "default"))
20413b3a8eb9SGleb Smirnoff 				$$ = CBQCLF_DEFCLASS;
20423b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "borrow"))
20433b3a8eb9SGleb Smirnoff 				$$ = CBQCLF_BORROW;
20443b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "red"))
20453b3a8eb9SGleb Smirnoff 				$$ = CBQCLF_RED;
20463b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "ecn"))
20473b3a8eb9SGleb Smirnoff 				$$ = CBQCLF_RED|CBQCLF_ECN;
20483b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "rio"))
20493b3a8eb9SGleb Smirnoff 				$$ = CBQCLF_RIO;
20500a70aaf8SLuiz Otavio O Souza 			else if (!strcmp($1, "codel"))
20510a70aaf8SLuiz Otavio O Souza 				$$ = CBQCLF_CODEL;
20523b3a8eb9SGleb Smirnoff 			else {
20533b3a8eb9SGleb Smirnoff 				yyerror("unknown cbq flag \"%s\"", $1);
20543b3a8eb9SGleb Smirnoff 				free($1);
20553b3a8eb9SGleb Smirnoff 				YYERROR;
20563b3a8eb9SGleb Smirnoff 			}
20573b3a8eb9SGleb Smirnoff 			free($1);
20583b3a8eb9SGleb Smirnoff 		}
20593b3a8eb9SGleb Smirnoff 		;
20603b3a8eb9SGleb Smirnoff 
20613b3a8eb9SGleb Smirnoff priqflags_list	: priqflags_item			{ $$ |= $1; }
20623b3a8eb9SGleb Smirnoff 		| priqflags_list comma priqflags_item	{ $$ |= $3; }
20633b3a8eb9SGleb Smirnoff 		;
20643b3a8eb9SGleb Smirnoff 
20653b3a8eb9SGleb Smirnoff priqflags_item	: STRING	{
20663b3a8eb9SGleb Smirnoff 			if (!strcmp($1, "default"))
20673b3a8eb9SGleb Smirnoff 				$$ = PRCF_DEFAULTCLASS;
20683b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "red"))
20693b3a8eb9SGleb Smirnoff 				$$ = PRCF_RED;
20703b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "ecn"))
20713b3a8eb9SGleb Smirnoff 				$$ = PRCF_RED|PRCF_ECN;
20723b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "rio"))
20733b3a8eb9SGleb Smirnoff 				$$ = PRCF_RIO;
20740a70aaf8SLuiz Otavio O Souza 			else if (!strcmp($1, "codel"))
20750a70aaf8SLuiz Otavio O Souza 				$$ = PRCF_CODEL;
20763b3a8eb9SGleb Smirnoff 			else {
20773b3a8eb9SGleb Smirnoff 				yyerror("unknown priq flag \"%s\"", $1);
20783b3a8eb9SGleb Smirnoff 				free($1);
20793b3a8eb9SGleb Smirnoff 				YYERROR;
20803b3a8eb9SGleb Smirnoff 			}
20813b3a8eb9SGleb Smirnoff 			free($1);
20823b3a8eb9SGleb Smirnoff 		}
20833b3a8eb9SGleb Smirnoff 		;
20843b3a8eb9SGleb Smirnoff 
20853b3a8eb9SGleb Smirnoff hfsc_opts	:	{
20863b3a8eb9SGleb Smirnoff 				bzero(&hfsc_opts,
20873b3a8eb9SGleb Smirnoff 				    sizeof(struct node_hfsc_opts));
20883b3a8eb9SGleb Smirnoff 			}
20893b3a8eb9SGleb Smirnoff 		    hfscopts_list				{
20903b3a8eb9SGleb Smirnoff 			$$ = hfsc_opts;
20913b3a8eb9SGleb Smirnoff 		}
20923b3a8eb9SGleb Smirnoff 		;
20933b3a8eb9SGleb Smirnoff 
20943b3a8eb9SGleb Smirnoff hfscopts_list	: hfscopts_item
20953b3a8eb9SGleb Smirnoff 		| hfscopts_list comma hfscopts_item
20963b3a8eb9SGleb Smirnoff 		;
20973b3a8eb9SGleb Smirnoff 
20983b3a8eb9SGleb Smirnoff hfscopts_item	: LINKSHARE bandwidth				{
20993b3a8eb9SGleb Smirnoff 			if (hfsc_opts.linkshare.used) {
21003b3a8eb9SGleb Smirnoff 				yyerror("linkshare already specified");
21013b3a8eb9SGleb Smirnoff 				YYERROR;
21023b3a8eb9SGleb Smirnoff 			}
21033b3a8eb9SGleb Smirnoff 			hfsc_opts.linkshare.m2 = $2;
21043b3a8eb9SGleb Smirnoff 			hfsc_opts.linkshare.used = 1;
21053b3a8eb9SGleb Smirnoff 		}
21063b3a8eb9SGleb Smirnoff 		| LINKSHARE '(' bandwidth comma NUMBER comma bandwidth ')'
21073b3a8eb9SGleb Smirnoff 		    {
21083b3a8eb9SGleb Smirnoff 			if ($5 < 0 || $5 > INT_MAX) {
21093b3a8eb9SGleb Smirnoff 				yyerror("timing in curve out of range");
21103b3a8eb9SGleb Smirnoff 				YYERROR;
21113b3a8eb9SGleb Smirnoff 			}
21123b3a8eb9SGleb Smirnoff 			if (hfsc_opts.linkshare.used) {
21133b3a8eb9SGleb Smirnoff 				yyerror("linkshare already specified");
21143b3a8eb9SGleb Smirnoff 				YYERROR;
21153b3a8eb9SGleb Smirnoff 			}
21163b3a8eb9SGleb Smirnoff 			hfsc_opts.linkshare.m1 = $3;
21173b3a8eb9SGleb Smirnoff 			hfsc_opts.linkshare.d = $5;
21183b3a8eb9SGleb Smirnoff 			hfsc_opts.linkshare.m2 = $7;
21193b3a8eb9SGleb Smirnoff 			hfsc_opts.linkshare.used = 1;
21203b3a8eb9SGleb Smirnoff 		}
21213b3a8eb9SGleb Smirnoff 		| REALTIME bandwidth				{
21223b3a8eb9SGleb Smirnoff 			if (hfsc_opts.realtime.used) {
21233b3a8eb9SGleb Smirnoff 				yyerror("realtime already specified");
21243b3a8eb9SGleb Smirnoff 				YYERROR;
21253b3a8eb9SGleb Smirnoff 			}
21263b3a8eb9SGleb Smirnoff 			hfsc_opts.realtime.m2 = $2;
21273b3a8eb9SGleb Smirnoff 			hfsc_opts.realtime.used = 1;
21283b3a8eb9SGleb Smirnoff 		}
21293b3a8eb9SGleb Smirnoff 		| REALTIME '(' bandwidth comma NUMBER comma bandwidth ')'
21303b3a8eb9SGleb Smirnoff 		    {
21313b3a8eb9SGleb Smirnoff 			if ($5 < 0 || $5 > INT_MAX) {
21323b3a8eb9SGleb Smirnoff 				yyerror("timing in curve out of range");
21333b3a8eb9SGleb Smirnoff 				YYERROR;
21343b3a8eb9SGleb Smirnoff 			}
21353b3a8eb9SGleb Smirnoff 			if (hfsc_opts.realtime.used) {
21363b3a8eb9SGleb Smirnoff 				yyerror("realtime already specified");
21373b3a8eb9SGleb Smirnoff 				YYERROR;
21383b3a8eb9SGleb Smirnoff 			}
21393b3a8eb9SGleb Smirnoff 			hfsc_opts.realtime.m1 = $3;
21403b3a8eb9SGleb Smirnoff 			hfsc_opts.realtime.d = $5;
21413b3a8eb9SGleb Smirnoff 			hfsc_opts.realtime.m2 = $7;
21423b3a8eb9SGleb Smirnoff 			hfsc_opts.realtime.used = 1;
21433b3a8eb9SGleb Smirnoff 		}
21443b3a8eb9SGleb Smirnoff 		| UPPERLIMIT bandwidth				{
21453b3a8eb9SGleb Smirnoff 			if (hfsc_opts.upperlimit.used) {
21463b3a8eb9SGleb Smirnoff 				yyerror("upperlimit already specified");
21473b3a8eb9SGleb Smirnoff 				YYERROR;
21483b3a8eb9SGleb Smirnoff 			}
21493b3a8eb9SGleb Smirnoff 			hfsc_opts.upperlimit.m2 = $2;
21503b3a8eb9SGleb Smirnoff 			hfsc_opts.upperlimit.used = 1;
21513b3a8eb9SGleb Smirnoff 		}
21523b3a8eb9SGleb Smirnoff 		| UPPERLIMIT '(' bandwidth comma NUMBER comma bandwidth ')'
21533b3a8eb9SGleb Smirnoff 		    {
21543b3a8eb9SGleb Smirnoff 			if ($5 < 0 || $5 > INT_MAX) {
21553b3a8eb9SGleb Smirnoff 				yyerror("timing in curve out of range");
21563b3a8eb9SGleb Smirnoff 				YYERROR;
21573b3a8eb9SGleb Smirnoff 			}
21583b3a8eb9SGleb Smirnoff 			if (hfsc_opts.upperlimit.used) {
21593b3a8eb9SGleb Smirnoff 				yyerror("upperlimit already specified");
21603b3a8eb9SGleb Smirnoff 				YYERROR;
21613b3a8eb9SGleb Smirnoff 			}
21623b3a8eb9SGleb Smirnoff 			hfsc_opts.upperlimit.m1 = $3;
21633b3a8eb9SGleb Smirnoff 			hfsc_opts.upperlimit.d = $5;
21643b3a8eb9SGleb Smirnoff 			hfsc_opts.upperlimit.m2 = $7;
21653b3a8eb9SGleb Smirnoff 			hfsc_opts.upperlimit.used = 1;
21663b3a8eb9SGleb Smirnoff 		}
21673b3a8eb9SGleb Smirnoff 		| STRING	{
21683b3a8eb9SGleb Smirnoff 			if (!strcmp($1, "default"))
21693b3a8eb9SGleb Smirnoff 				hfsc_opts.flags |= HFCF_DEFAULTCLASS;
21703b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "red"))
21713b3a8eb9SGleb Smirnoff 				hfsc_opts.flags |= HFCF_RED;
21723b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "ecn"))
21733b3a8eb9SGleb Smirnoff 				hfsc_opts.flags |= HFCF_RED|HFCF_ECN;
21743b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "rio"))
21753b3a8eb9SGleb Smirnoff 				hfsc_opts.flags |= HFCF_RIO;
21760a70aaf8SLuiz Otavio O Souza 			else if (!strcmp($1, "codel"))
21770a70aaf8SLuiz Otavio O Souza 				hfsc_opts.flags |= HFCF_CODEL;
21783b3a8eb9SGleb Smirnoff 			else {
21793b3a8eb9SGleb Smirnoff 				yyerror("unknown hfsc flag \"%s\"", $1);
21803b3a8eb9SGleb Smirnoff 				free($1);
21813b3a8eb9SGleb Smirnoff 				YYERROR;
21823b3a8eb9SGleb Smirnoff 			}
21833b3a8eb9SGleb Smirnoff 			free($1);
21843b3a8eb9SGleb Smirnoff 		}
21853b3a8eb9SGleb Smirnoff 		;
21863b3a8eb9SGleb Smirnoff 
2187a5b789f6SErmal Luçi fairq_opts	:	{
2188a5b789f6SErmal Luçi 				bzero(&fairq_opts,
2189a5b789f6SErmal Luçi 				    sizeof(struct node_fairq_opts));
2190a5b789f6SErmal Luçi 			}
2191a5b789f6SErmal Luçi 		    fairqopts_list				{
2192a5b789f6SErmal Luçi 			$$ = fairq_opts;
2193a5b789f6SErmal Luçi 		}
2194a5b789f6SErmal Luçi 		;
2195a5b789f6SErmal Luçi 
2196a5b789f6SErmal Luçi fairqopts_list	: fairqopts_item
2197a5b789f6SErmal Luçi 		| fairqopts_list comma fairqopts_item
2198a5b789f6SErmal Luçi 		;
2199a5b789f6SErmal Luçi 
2200a5b789f6SErmal Luçi fairqopts_item	: LINKSHARE bandwidth				{
2201a5b789f6SErmal Luçi 			if (fairq_opts.linkshare.used) {
2202a5b789f6SErmal Luçi 				yyerror("linkshare already specified");
2203a5b789f6SErmal Luçi 				YYERROR;
2204a5b789f6SErmal Luçi 			}
2205a5b789f6SErmal Luçi 			fairq_opts.linkshare.m2 = $2;
2206a5b789f6SErmal Luçi 			fairq_opts.linkshare.used = 1;
2207a5b789f6SErmal Luçi 		}
2208a5b789f6SErmal Luçi 		| LINKSHARE '(' bandwidth number bandwidth ')'	{
2209a5b789f6SErmal Luçi 			if (fairq_opts.linkshare.used) {
2210a5b789f6SErmal Luçi 				yyerror("linkshare already specified");
2211a5b789f6SErmal Luçi 				YYERROR;
2212a5b789f6SErmal Luçi 			}
2213a5b789f6SErmal Luçi 			fairq_opts.linkshare.m1 = $3;
2214a5b789f6SErmal Luçi 			fairq_opts.linkshare.d = $4;
2215a5b789f6SErmal Luçi 			fairq_opts.linkshare.m2 = $5;
2216a5b789f6SErmal Luçi 			fairq_opts.linkshare.used = 1;
2217a5b789f6SErmal Luçi 		}
2218a5b789f6SErmal Luçi 		| HOGS bandwidth {
2219a5b789f6SErmal Luçi 			fairq_opts.hogs_bw = $2;
2220a5b789f6SErmal Luçi 		}
2221a5b789f6SErmal Luçi 		| BUCKETS number {
2222a5b789f6SErmal Luçi 			fairq_opts.nbuckets = $2;
2223a5b789f6SErmal Luçi 		}
2224a5b789f6SErmal Luçi 		| STRING	{
2225a5b789f6SErmal Luçi 			if (!strcmp($1, "default"))
2226a5b789f6SErmal Luçi 				fairq_opts.flags |= FARF_DEFAULTCLASS;
2227a5b789f6SErmal Luçi 			else if (!strcmp($1, "red"))
2228a5b789f6SErmal Luçi 				fairq_opts.flags |= FARF_RED;
2229a5b789f6SErmal Luçi 			else if (!strcmp($1, "ecn"))
2230a5b789f6SErmal Luçi 				fairq_opts.flags |= FARF_RED|FARF_ECN;
2231a5b789f6SErmal Luçi 			else if (!strcmp($1, "rio"))
2232a5b789f6SErmal Luçi 				fairq_opts.flags |= FARF_RIO;
22330a70aaf8SLuiz Otavio O Souza 			else if (!strcmp($1, "codel"))
22340a70aaf8SLuiz Otavio O Souza 				fairq_opts.flags |= FARF_CODEL;
2235a5b789f6SErmal Luçi 			else {
2236a5b789f6SErmal Luçi 				yyerror("unknown fairq flag \"%s\"", $1);
2237a5b789f6SErmal Luçi 				free($1);
2238a5b789f6SErmal Luçi 				YYERROR;
2239a5b789f6SErmal Luçi 			}
2240a5b789f6SErmal Luçi 			free($1);
2241a5b789f6SErmal Luçi 		}
2242a5b789f6SErmal Luçi 		;
2243a5b789f6SErmal Luçi 
22440a70aaf8SLuiz Otavio O Souza codel_opts	:	{
22450a70aaf8SLuiz Otavio O Souza 				bzero(&codel_opts,
22460a70aaf8SLuiz Otavio O Souza 				    sizeof(struct codel_opts));
22470a70aaf8SLuiz Otavio O Souza 			}
22480a70aaf8SLuiz Otavio O Souza 		    codelopts_list				{
22490a70aaf8SLuiz Otavio O Souza 			$$ = codel_opts;
22500a70aaf8SLuiz Otavio O Souza 		}
22510a70aaf8SLuiz Otavio O Souza 		;
22520a70aaf8SLuiz Otavio O Souza 
22530a70aaf8SLuiz Otavio O Souza codelopts_list	: codelopts_item
22540a70aaf8SLuiz Otavio O Souza 		| codelopts_list comma codelopts_item
22550a70aaf8SLuiz Otavio O Souza 		;
22560a70aaf8SLuiz Otavio O Souza 
22570a70aaf8SLuiz Otavio O Souza codelopts_item	: INTERVAL number				{
22580a70aaf8SLuiz Otavio O Souza 			if (codel_opts.interval) {
22590a70aaf8SLuiz Otavio O Souza 				yyerror("interval already specified");
22600a70aaf8SLuiz Otavio O Souza 				YYERROR;
22610a70aaf8SLuiz Otavio O Souza 			}
22620a70aaf8SLuiz Otavio O Souza 			codel_opts.interval = $2;
22630a70aaf8SLuiz Otavio O Souza 		}
22640a70aaf8SLuiz Otavio O Souza 		| TARGET number					{
22650a70aaf8SLuiz Otavio O Souza 			if (codel_opts.target) {
22660a70aaf8SLuiz Otavio O Souza 				yyerror("target already specified");
22670a70aaf8SLuiz Otavio O Souza 				YYERROR;
22680a70aaf8SLuiz Otavio O Souza 			}
22690a70aaf8SLuiz Otavio O Souza 			codel_opts.target = $2;
22700a70aaf8SLuiz Otavio O Souza 		}
22710a70aaf8SLuiz Otavio O Souza 		| STRING					{
22720a70aaf8SLuiz Otavio O Souza 			if (!strcmp($1, "ecn"))
22730a70aaf8SLuiz Otavio O Souza 				codel_opts.ecn = 1;
22740a70aaf8SLuiz Otavio O Souza 			else {
22750a70aaf8SLuiz Otavio O Souza 				yyerror("unknown codel option \"%s\"", $1);
22760a70aaf8SLuiz Otavio O Souza 				free($1);
22770a70aaf8SLuiz Otavio O Souza 				YYERROR;
22780a70aaf8SLuiz Otavio O Souza 			}
22790a70aaf8SLuiz Otavio O Souza 			free($1);
22800a70aaf8SLuiz Otavio O Souza 		}
22810a70aaf8SLuiz Otavio O Souza 		;
22820a70aaf8SLuiz Otavio O Souza 
22833b3a8eb9SGleb Smirnoff qassign		: /* empty */		{ $$ = NULL; }
22843b3a8eb9SGleb Smirnoff 		| qassign_item		{ $$ = $1; }
22853b3a8eb9SGleb Smirnoff 		| '{' optnl qassign_list '}'	{ $$ = $3; }
22863b3a8eb9SGleb Smirnoff 		;
22873b3a8eb9SGleb Smirnoff 
22883b3a8eb9SGleb Smirnoff qassign_list	: qassign_item optnl		{ $$ = $1; }
22893b3a8eb9SGleb Smirnoff 		| qassign_list comma qassign_item optnl	{
22903b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
22913b3a8eb9SGleb Smirnoff 			$1->tail = $3;
22923b3a8eb9SGleb Smirnoff 			$$ = $1;
22933b3a8eb9SGleb Smirnoff 		}
22943b3a8eb9SGleb Smirnoff 		;
22953b3a8eb9SGleb Smirnoff 
22963b3a8eb9SGleb Smirnoff qassign_item	: STRING			{
22973b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_queue));
22983b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
22993b3a8eb9SGleb Smirnoff 				err(1, "qassign_item: calloc");
23003b3a8eb9SGleb Smirnoff 			if (strlcpy($$->queue, $1, sizeof($$->queue)) >=
23013b3a8eb9SGleb Smirnoff 			    sizeof($$->queue)) {
23023b3a8eb9SGleb Smirnoff 				yyerror("queue name '%s' too long (max "
23033b3a8eb9SGleb Smirnoff 				    "%d chars)", $1, sizeof($$->queue)-1);
23043b3a8eb9SGleb Smirnoff 				free($1);
23053b3a8eb9SGleb Smirnoff 				free($$);
23063b3a8eb9SGleb Smirnoff 				YYERROR;
23073b3a8eb9SGleb Smirnoff 			}
23083b3a8eb9SGleb Smirnoff 			free($1);
23093b3a8eb9SGleb Smirnoff 			$$->next = NULL;
23103b3a8eb9SGleb Smirnoff 			$$->tail = $$;
23113b3a8eb9SGleb Smirnoff 		}
23123b3a8eb9SGleb Smirnoff 		;
23133b3a8eb9SGleb Smirnoff 
23143b3a8eb9SGleb Smirnoff pfrule		: action dir logquick interface route af proto fromto
23153b3a8eb9SGleb Smirnoff 		    filter_opts
23163b3a8eb9SGleb Smirnoff 		{
2317e9eb0941SKristof Provost 			struct pfctl_rule	 r;
23183b3a8eb9SGleb Smirnoff 			struct node_state_opt	*o;
23193b3a8eb9SGleb Smirnoff 			struct node_proto	*proto;
23203b3a8eb9SGleb Smirnoff 			int			 srctrack = 0;
23213b3a8eb9SGleb Smirnoff 			int			 statelock = 0;
23223b3a8eb9SGleb Smirnoff 			int			 adaptive = 0;
23233b3a8eb9SGleb Smirnoff 			int			 defaults = 0;
23243b3a8eb9SGleb Smirnoff 
23253b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_FILTER))
23263b3a8eb9SGleb Smirnoff 				YYERROR;
23273b3a8eb9SGleb Smirnoff 
23283b3a8eb9SGleb Smirnoff 			memset(&r, 0, sizeof(r));
23293b3a8eb9SGleb Smirnoff 
23303b3a8eb9SGleb Smirnoff 			r.action = $1.b1;
23313b3a8eb9SGleb Smirnoff 			switch ($1.b2) {
23323b3a8eb9SGleb Smirnoff 			case PFRULE_RETURNRST:
23333b3a8eb9SGleb Smirnoff 				r.rule_flag |= PFRULE_RETURNRST;
23343b3a8eb9SGleb Smirnoff 				r.return_ttl = $1.w;
23353b3a8eb9SGleb Smirnoff 				break;
23363b3a8eb9SGleb Smirnoff 			case PFRULE_RETURNICMP:
23373b3a8eb9SGleb Smirnoff 				r.rule_flag |= PFRULE_RETURNICMP;
23383b3a8eb9SGleb Smirnoff 				r.return_icmp = $1.w;
23393b3a8eb9SGleb Smirnoff 				r.return_icmp6 = $1.w2;
23403b3a8eb9SGleb Smirnoff 				break;
23413b3a8eb9SGleb Smirnoff 			case PFRULE_RETURN:
23423b3a8eb9SGleb Smirnoff 				r.rule_flag |= PFRULE_RETURN;
23433b3a8eb9SGleb Smirnoff 				r.return_icmp = $1.w;
23443b3a8eb9SGleb Smirnoff 				r.return_icmp6 = $1.w2;
23453b3a8eb9SGleb Smirnoff 				break;
23463b3a8eb9SGleb Smirnoff 			}
23473b3a8eb9SGleb Smirnoff 			r.direction = $2;
23483b3a8eb9SGleb Smirnoff 			r.log = $3.log;
23493b3a8eb9SGleb Smirnoff 			r.logif = $3.logif;
23503b3a8eb9SGleb Smirnoff 			r.quick = $3.quick;
23513b3a8eb9SGleb Smirnoff 			r.prob = $9.prob;
23523b3a8eb9SGleb Smirnoff 			r.rtableid = $9.rtableid;
23533b3a8eb9SGleb Smirnoff 
23543e248e0fSKristof Provost 			if ($9.marker & FOM_PRIO) {
23553e248e0fSKristof Provost 				if ($9.prio == 0)
23563e248e0fSKristof Provost 					r.prio = PF_PRIO_ZERO;
23573e248e0fSKristof Provost 				else
23583e248e0fSKristof Provost 					r.prio = $9.prio;
23593e248e0fSKristof Provost 			}
23603e248e0fSKristof Provost 			if ($9.marker & FOM_SETPRIO) {
23613e248e0fSKristof Provost 				r.set_prio[0] = $9.set_prio[0];
23623e248e0fSKristof Provost 				r.set_prio[1] = $9.set_prio[1];
23633e248e0fSKristof Provost 				r.scrub_flags |= PFSTATE_SETPRIO;
23643e248e0fSKristof Provost 			}
23653e248e0fSKristof Provost 
23663b3a8eb9SGleb Smirnoff 			r.af = $6;
23673b3a8eb9SGleb Smirnoff 			if ($9.tag)
23683b3a8eb9SGleb Smirnoff 				if (strlcpy(r.tagname, $9.tag,
23693b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
23703b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
23713b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
23723b3a8eb9SGleb Smirnoff 					YYERROR;
23733b3a8eb9SGleb Smirnoff 				}
23743b3a8eb9SGleb Smirnoff 			if ($9.match_tag)
23753b3a8eb9SGleb Smirnoff 				if (strlcpy(r.match_tagname, $9.match_tag,
23763b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
23773b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
23783b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
23793b3a8eb9SGleb Smirnoff 					YYERROR;
23803b3a8eb9SGleb Smirnoff 				}
23813b3a8eb9SGleb Smirnoff 			r.match_tag_not = $9.match_tag_not;
23823b3a8eb9SGleb Smirnoff 			if (rule_label(&r, $9.label))
23833b3a8eb9SGleb Smirnoff 				YYERROR;
23846fcc8e04SKristof Provost 			for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++)
23856fcc8e04SKristof Provost 				free($9.label[i]);
238676c5eeccSKristof Provost 			r.ridentifier = $9.ridentifier;
23873b3a8eb9SGleb Smirnoff 			r.flags = $9.flags.b1;
23883b3a8eb9SGleb Smirnoff 			r.flagset = $9.flags.b2;
23893b3a8eb9SGleb Smirnoff 			if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
23903b3a8eb9SGleb Smirnoff 				yyerror("flags always false");
23913b3a8eb9SGleb Smirnoff 				YYERROR;
23923b3a8eb9SGleb Smirnoff 			}
23933b3a8eb9SGleb Smirnoff 			if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
23943b3a8eb9SGleb Smirnoff 				for (proto = $7; proto != NULL &&
23953b3a8eb9SGleb Smirnoff 				    proto->proto != IPPROTO_TCP;
23963b3a8eb9SGleb Smirnoff 				    proto = proto->next)
23973b3a8eb9SGleb Smirnoff 					;	/* nothing */
23983b3a8eb9SGleb Smirnoff 				if (proto == NULL && $7 != NULL) {
23993b3a8eb9SGleb Smirnoff 					if ($9.flags.b1 || $9.flags.b2)
24003b3a8eb9SGleb Smirnoff 						yyerror(
24013b3a8eb9SGleb Smirnoff 						    "flags only apply to tcp");
24023b3a8eb9SGleb Smirnoff 					if ($8.src_os)
24033b3a8eb9SGleb Smirnoff 						yyerror(
24043b3a8eb9SGleb Smirnoff 						    "OS fingerprinting only "
24053b3a8eb9SGleb Smirnoff 						    "apply to tcp");
24063b3a8eb9SGleb Smirnoff 					YYERROR;
24073b3a8eb9SGleb Smirnoff 				}
24083b3a8eb9SGleb Smirnoff #if 0
24093b3a8eb9SGleb Smirnoff 				if (($9.flags.b1 & parse_flags("S")) == 0 &&
24103b3a8eb9SGleb Smirnoff 				    $8.src_os) {
24113b3a8eb9SGleb Smirnoff 					yyerror("OS fingerprinting requires "
24123b3a8eb9SGleb Smirnoff 					    "the SYN TCP flag (flags S/SA)");
24133b3a8eb9SGleb Smirnoff 					YYERROR;
24143b3a8eb9SGleb Smirnoff 				}
24153b3a8eb9SGleb Smirnoff #endif
24163b3a8eb9SGleb Smirnoff 			}
24173b3a8eb9SGleb Smirnoff 
24183b3a8eb9SGleb Smirnoff 			r.tos = $9.tos;
24193b3a8eb9SGleb Smirnoff 			r.keep_state = $9.keep.action;
24203b3a8eb9SGleb Smirnoff 			o = $9.keep.options;
24213b3a8eb9SGleb Smirnoff 
24223b3a8eb9SGleb Smirnoff 			/* 'keep state' by default on pass rules. */
24233b3a8eb9SGleb Smirnoff 			if (!r.keep_state && !r.action &&
24243b3a8eb9SGleb Smirnoff 			    !($9.marker & FOM_KEEP)) {
24253b3a8eb9SGleb Smirnoff 				r.keep_state = PF_STATE_NORMAL;
24263b3a8eb9SGleb Smirnoff 				o = keep_state_defaults;
24273b3a8eb9SGleb Smirnoff 				defaults = 1;
24283b3a8eb9SGleb Smirnoff 			}
24293b3a8eb9SGleb Smirnoff 
24303b3a8eb9SGleb Smirnoff 			while (o) {
24313b3a8eb9SGleb Smirnoff 				struct node_state_opt	*p = o;
24323b3a8eb9SGleb Smirnoff 
24333b3a8eb9SGleb Smirnoff 				switch (o->type) {
24343b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_MAX:
24353b3a8eb9SGleb Smirnoff 					if (r.max_states) {
24363b3a8eb9SGleb Smirnoff 						yyerror("state option 'max' "
24373b3a8eb9SGleb Smirnoff 						    "multiple definitions");
24383b3a8eb9SGleb Smirnoff 						YYERROR;
24393b3a8eb9SGleb Smirnoff 					}
24403b3a8eb9SGleb Smirnoff 					r.max_states = o->data.max_states;
24413b3a8eb9SGleb Smirnoff 					break;
24423b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_NOSYNC:
24433b3a8eb9SGleb Smirnoff 					if (r.rule_flag & PFRULE_NOSYNC) {
24443b3a8eb9SGleb Smirnoff 						yyerror("state option 'sync' "
24453b3a8eb9SGleb Smirnoff 						    "multiple definitions");
24463b3a8eb9SGleb Smirnoff 						YYERROR;
24473b3a8eb9SGleb Smirnoff 					}
24483b3a8eb9SGleb Smirnoff 					r.rule_flag |= PFRULE_NOSYNC;
24493b3a8eb9SGleb Smirnoff 					break;
24503b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_SRCTRACK:
24513b3a8eb9SGleb Smirnoff 					if (srctrack) {
24523b3a8eb9SGleb Smirnoff 						yyerror("state option "
24533b3a8eb9SGleb Smirnoff 						    "'source-track' "
24543b3a8eb9SGleb Smirnoff 						    "multiple definitions");
24553b3a8eb9SGleb Smirnoff 						YYERROR;
24563b3a8eb9SGleb Smirnoff 					}
24573b3a8eb9SGleb Smirnoff 					srctrack =  o->data.src_track;
24583b3a8eb9SGleb Smirnoff 					r.rule_flag |= PFRULE_SRCTRACK;
24593b3a8eb9SGleb Smirnoff 					break;
24603b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_MAX_SRC_STATES:
24613b3a8eb9SGleb Smirnoff 					if (r.max_src_states) {
24623b3a8eb9SGleb Smirnoff 						yyerror("state option "
24633b3a8eb9SGleb Smirnoff 						    "'max-src-states' "
24643b3a8eb9SGleb Smirnoff 						    "multiple definitions");
24653b3a8eb9SGleb Smirnoff 						YYERROR;
24663b3a8eb9SGleb Smirnoff 					}
24673b3a8eb9SGleb Smirnoff 					if (o->data.max_src_states == 0) {
24683b3a8eb9SGleb Smirnoff 						yyerror("'max-src-states' must "
24693b3a8eb9SGleb Smirnoff 						    "be > 0");
24703b3a8eb9SGleb Smirnoff 						YYERROR;
24713b3a8eb9SGleb Smirnoff 					}
24723b3a8eb9SGleb Smirnoff 					r.max_src_states =
24733b3a8eb9SGleb Smirnoff 					    o->data.max_src_states;
24743b3a8eb9SGleb Smirnoff 					r.rule_flag |= PFRULE_SRCTRACK;
24753b3a8eb9SGleb Smirnoff 					break;
24763b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_OVERLOAD:
24773b3a8eb9SGleb Smirnoff 					if (r.overload_tblname[0]) {
24783b3a8eb9SGleb Smirnoff 						yyerror("multiple 'overload' "
24793b3a8eb9SGleb Smirnoff 						    "table definitions");
24803b3a8eb9SGleb Smirnoff 						YYERROR;
24813b3a8eb9SGleb Smirnoff 					}
24823b3a8eb9SGleb Smirnoff 					if (strlcpy(r.overload_tblname,
24833b3a8eb9SGleb Smirnoff 					    o->data.overload.tblname,
24843b3a8eb9SGleb Smirnoff 					    PF_TABLE_NAME_SIZE) >=
24853b3a8eb9SGleb Smirnoff 					    PF_TABLE_NAME_SIZE) {
24863b3a8eb9SGleb Smirnoff 						yyerror("state option: "
24873b3a8eb9SGleb Smirnoff 						    "strlcpy");
24883b3a8eb9SGleb Smirnoff 						YYERROR;
24893b3a8eb9SGleb Smirnoff 					}
24903b3a8eb9SGleb Smirnoff 					r.flush = o->data.overload.flush;
24913b3a8eb9SGleb Smirnoff 					break;
24923b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_MAX_SRC_CONN:
24933b3a8eb9SGleb Smirnoff 					if (r.max_src_conn) {
24943b3a8eb9SGleb Smirnoff 						yyerror("state option "
24953b3a8eb9SGleb Smirnoff 						    "'max-src-conn' "
24963b3a8eb9SGleb Smirnoff 						    "multiple definitions");
24973b3a8eb9SGleb Smirnoff 						YYERROR;
24983b3a8eb9SGleb Smirnoff 					}
24993b3a8eb9SGleb Smirnoff 					if (o->data.max_src_conn == 0) {
25003b3a8eb9SGleb Smirnoff 						yyerror("'max-src-conn' "
25013b3a8eb9SGleb Smirnoff 						    "must be > 0");
25023b3a8eb9SGleb Smirnoff 						YYERROR;
25033b3a8eb9SGleb Smirnoff 					}
25043b3a8eb9SGleb Smirnoff 					r.max_src_conn =
25053b3a8eb9SGleb Smirnoff 					    o->data.max_src_conn;
25063b3a8eb9SGleb Smirnoff 					r.rule_flag |= PFRULE_SRCTRACK |
25073b3a8eb9SGleb Smirnoff 					    PFRULE_RULESRCTRACK;
25083b3a8eb9SGleb Smirnoff 					break;
25093b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_MAX_SRC_CONN_RATE:
25103b3a8eb9SGleb Smirnoff 					if (r.max_src_conn_rate.limit) {
25113b3a8eb9SGleb Smirnoff 						yyerror("state option "
25123b3a8eb9SGleb Smirnoff 						    "'max-src-conn-rate' "
25133b3a8eb9SGleb Smirnoff 						    "multiple definitions");
25143b3a8eb9SGleb Smirnoff 						YYERROR;
25153b3a8eb9SGleb Smirnoff 					}
25163b3a8eb9SGleb Smirnoff 					if (!o->data.max_src_conn_rate.limit ||
25173b3a8eb9SGleb Smirnoff 					    !o->data.max_src_conn_rate.seconds) {
25183b3a8eb9SGleb Smirnoff 						yyerror("'max-src-conn-rate' "
25193b3a8eb9SGleb Smirnoff 						    "values must be > 0");
25203b3a8eb9SGleb Smirnoff 						YYERROR;
25213b3a8eb9SGleb Smirnoff 					}
25223b3a8eb9SGleb Smirnoff 					if (o->data.max_src_conn_rate.limit >
25233b3a8eb9SGleb Smirnoff 					    PF_THRESHOLD_MAX) {
25243b3a8eb9SGleb Smirnoff 						yyerror("'max-src-conn-rate' "
25253b3a8eb9SGleb Smirnoff 						    "maximum rate must be < %u",
25263b3a8eb9SGleb Smirnoff 						    PF_THRESHOLD_MAX);
25273b3a8eb9SGleb Smirnoff 						YYERROR;
25283b3a8eb9SGleb Smirnoff 					}
25293b3a8eb9SGleb Smirnoff 					r.max_src_conn_rate.limit =
25303b3a8eb9SGleb Smirnoff 					    o->data.max_src_conn_rate.limit;
25313b3a8eb9SGleb Smirnoff 					r.max_src_conn_rate.seconds =
25323b3a8eb9SGleb Smirnoff 					    o->data.max_src_conn_rate.seconds;
25333b3a8eb9SGleb Smirnoff 					r.rule_flag |= PFRULE_SRCTRACK |
25343b3a8eb9SGleb Smirnoff 					    PFRULE_RULESRCTRACK;
25353b3a8eb9SGleb Smirnoff 					break;
25363b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_MAX_SRC_NODES:
25373b3a8eb9SGleb Smirnoff 					if (r.max_src_nodes) {
25383b3a8eb9SGleb Smirnoff 						yyerror("state option "
25393b3a8eb9SGleb Smirnoff 						    "'max-src-nodes' "
25403b3a8eb9SGleb Smirnoff 						    "multiple definitions");
25413b3a8eb9SGleb Smirnoff 						YYERROR;
25423b3a8eb9SGleb Smirnoff 					}
25433b3a8eb9SGleb Smirnoff 					if (o->data.max_src_nodes == 0) {
25443b3a8eb9SGleb Smirnoff 						yyerror("'max-src-nodes' must "
25453b3a8eb9SGleb Smirnoff 						    "be > 0");
25463b3a8eb9SGleb Smirnoff 						YYERROR;
25473b3a8eb9SGleb Smirnoff 					}
25483b3a8eb9SGleb Smirnoff 					r.max_src_nodes =
25493b3a8eb9SGleb Smirnoff 					    o->data.max_src_nodes;
25503b3a8eb9SGleb Smirnoff 					r.rule_flag |= PFRULE_SRCTRACK |
25513b3a8eb9SGleb Smirnoff 					    PFRULE_RULESRCTRACK;
25523b3a8eb9SGleb Smirnoff 					break;
25533b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_STATELOCK:
25543b3a8eb9SGleb Smirnoff 					if (statelock) {
25553b3a8eb9SGleb Smirnoff 						yyerror("state locking option: "
25563b3a8eb9SGleb Smirnoff 						    "multiple definitions");
25573b3a8eb9SGleb Smirnoff 						YYERROR;
25583b3a8eb9SGleb Smirnoff 					}
25593b3a8eb9SGleb Smirnoff 					statelock = 1;
25603b3a8eb9SGleb Smirnoff 					r.rule_flag |= o->data.statelock;
25613b3a8eb9SGleb Smirnoff 					break;
25623b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_SLOPPY:
25633b3a8eb9SGleb Smirnoff 					if (r.rule_flag & PFRULE_STATESLOPPY) {
25643b3a8eb9SGleb Smirnoff 						yyerror("state sloppy option: "
25653b3a8eb9SGleb Smirnoff 						    "multiple definitions");
25663b3a8eb9SGleb Smirnoff 						YYERROR;
25673b3a8eb9SGleb Smirnoff 					}
25683b3a8eb9SGleb Smirnoff 					r.rule_flag |= PFRULE_STATESLOPPY;
25693b3a8eb9SGleb Smirnoff 					break;
25703b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_TIMEOUT:
25713b3a8eb9SGleb Smirnoff 					if (o->data.timeout.number ==
25723b3a8eb9SGleb Smirnoff 					    PFTM_ADAPTIVE_START ||
25733b3a8eb9SGleb Smirnoff 					    o->data.timeout.number ==
25743b3a8eb9SGleb Smirnoff 					    PFTM_ADAPTIVE_END)
25753b3a8eb9SGleb Smirnoff 						adaptive = 1;
25763b3a8eb9SGleb Smirnoff 					if (r.timeout[o->data.timeout.number]) {
25773b3a8eb9SGleb Smirnoff 						yyerror("state timeout %s "
25783b3a8eb9SGleb Smirnoff 						    "multiple definitions",
25793b3a8eb9SGleb Smirnoff 						    pf_timeouts[o->data.
25803b3a8eb9SGleb Smirnoff 						    timeout.number].name);
25813b3a8eb9SGleb Smirnoff 						YYERROR;
25823b3a8eb9SGleb Smirnoff 					}
25833b3a8eb9SGleb Smirnoff 					r.timeout[o->data.timeout.number] =
25843b3a8eb9SGleb Smirnoff 					    o->data.timeout.seconds;
25853b3a8eb9SGleb Smirnoff 				}
25863b3a8eb9SGleb Smirnoff 				o = o->next;
25873b3a8eb9SGleb Smirnoff 				if (!defaults)
25883b3a8eb9SGleb Smirnoff 					free(p);
25893b3a8eb9SGleb Smirnoff 			}
25903b3a8eb9SGleb Smirnoff 
25913b3a8eb9SGleb Smirnoff 			/* 'flags S/SA' by default on stateful rules */
25923b3a8eb9SGleb Smirnoff 			if (!r.action && !r.flags && !r.flagset &&
25933b3a8eb9SGleb Smirnoff 			    !$9.fragment && !($9.marker & FOM_FLAGS) &&
25943b3a8eb9SGleb Smirnoff 			    r.keep_state) {
25953b3a8eb9SGleb Smirnoff 				r.flags = parse_flags("S");
25963b3a8eb9SGleb Smirnoff 				r.flagset =  parse_flags("SA");
25973b3a8eb9SGleb Smirnoff 			}
25983b3a8eb9SGleb Smirnoff 			if (!adaptive && r.max_states) {
25993b3a8eb9SGleb Smirnoff 				r.timeout[PFTM_ADAPTIVE_START] =
26003b3a8eb9SGleb Smirnoff 				    (r.max_states / 10) * 6;
26013b3a8eb9SGleb Smirnoff 				r.timeout[PFTM_ADAPTIVE_END] =
26023b3a8eb9SGleb Smirnoff 				    (r.max_states / 10) * 12;
26033b3a8eb9SGleb Smirnoff 			}
26043b3a8eb9SGleb Smirnoff 			if (r.rule_flag & PFRULE_SRCTRACK) {
26053b3a8eb9SGleb Smirnoff 				if (srctrack == PF_SRCTRACK_GLOBAL &&
26063b3a8eb9SGleb Smirnoff 				    r.max_src_nodes) {
26073b3a8eb9SGleb Smirnoff 					yyerror("'max-src-nodes' is "
26083b3a8eb9SGleb Smirnoff 					    "incompatible with "
26093b3a8eb9SGleb Smirnoff 					    "'source-track global'");
26103b3a8eb9SGleb Smirnoff 					YYERROR;
26113b3a8eb9SGleb Smirnoff 				}
26123b3a8eb9SGleb Smirnoff 				if (srctrack == PF_SRCTRACK_GLOBAL &&
26133b3a8eb9SGleb Smirnoff 				    r.max_src_conn) {
26143b3a8eb9SGleb Smirnoff 					yyerror("'max-src-conn' is "
26153b3a8eb9SGleb Smirnoff 					    "incompatible with "
26163b3a8eb9SGleb Smirnoff 					    "'source-track global'");
26173b3a8eb9SGleb Smirnoff 					YYERROR;
26183b3a8eb9SGleb Smirnoff 				}
26193b3a8eb9SGleb Smirnoff 				if (srctrack == PF_SRCTRACK_GLOBAL &&
26203b3a8eb9SGleb Smirnoff 				    r.max_src_conn_rate.seconds) {
26213b3a8eb9SGleb Smirnoff 					yyerror("'max-src-conn-rate' is "
26223b3a8eb9SGleb Smirnoff 					    "incompatible with "
26233b3a8eb9SGleb Smirnoff 					    "'source-track global'");
26243b3a8eb9SGleb Smirnoff 					YYERROR;
26253b3a8eb9SGleb Smirnoff 				}
26263b3a8eb9SGleb Smirnoff 				if (r.timeout[PFTM_SRC_NODE] <
26273b3a8eb9SGleb Smirnoff 				    r.max_src_conn_rate.seconds)
26283b3a8eb9SGleb Smirnoff 					r.timeout[PFTM_SRC_NODE] =
26293b3a8eb9SGleb Smirnoff 					    r.max_src_conn_rate.seconds;
26303b3a8eb9SGleb Smirnoff 				r.rule_flag |= PFRULE_SRCTRACK;
26313b3a8eb9SGleb Smirnoff 				if (srctrack == PF_SRCTRACK_RULE)
26323b3a8eb9SGleb Smirnoff 					r.rule_flag |= PFRULE_RULESRCTRACK;
26333b3a8eb9SGleb Smirnoff 			}
26343b3a8eb9SGleb Smirnoff 			if (r.keep_state && !statelock)
26353b3a8eb9SGleb Smirnoff 				r.rule_flag |= default_statelock;
26363b3a8eb9SGleb Smirnoff 
26373b3a8eb9SGleb Smirnoff 			if ($9.fragment)
26383b3a8eb9SGleb Smirnoff 				r.rule_flag |= PFRULE_FRAGMENT;
26393b3a8eb9SGleb Smirnoff 			r.allow_opts = $9.allowopts;
26403b3a8eb9SGleb Smirnoff 
26413b3a8eb9SGleb Smirnoff 			decide_address_family($8.src.host, &r.af);
26423b3a8eb9SGleb Smirnoff 			decide_address_family($8.dst.host, &r.af);
26433b3a8eb9SGleb Smirnoff 
26443b3a8eb9SGleb Smirnoff 			if ($5.rt) {
26453b3a8eb9SGleb Smirnoff 				if (!r.direction) {
26463b3a8eb9SGleb Smirnoff 					yyerror("direction must be explicit "
26473b3a8eb9SGleb Smirnoff 					    "with rules that specify routing");
26483b3a8eb9SGleb Smirnoff 					YYERROR;
26493b3a8eb9SGleb Smirnoff 				}
26503b3a8eb9SGleb Smirnoff 				r.rt = $5.rt;
26513b3a8eb9SGleb Smirnoff 				r.rpool.opts = $5.pool_opts;
26523b3a8eb9SGleb Smirnoff 				if ($5.key != NULL)
26533b3a8eb9SGleb Smirnoff 					memcpy(&r.rpool.key, $5.key,
26543b3a8eb9SGleb Smirnoff 					    sizeof(struct pf_poolhashkey));
26553b3a8eb9SGleb Smirnoff 			}
2656813196a1SKristof Provost 			if (r.rt) {
26573b3a8eb9SGleb Smirnoff 				decide_address_family($5.host, &r.af);
26583b3a8eb9SGleb Smirnoff 				remove_invalid_hosts(&$5.host, &r.af);
26593b3a8eb9SGleb Smirnoff 				if ($5.host == NULL) {
26603b3a8eb9SGleb Smirnoff 					yyerror("no routing address with "
26613b3a8eb9SGleb Smirnoff 					    "matching address family found.");
26623b3a8eb9SGleb Smirnoff 					YYERROR;
26633b3a8eb9SGleb Smirnoff 				}
26643b3a8eb9SGleb Smirnoff 				if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
26653b3a8eb9SGleb Smirnoff 				    PF_POOL_NONE && ($5.host->next != NULL ||
26663b3a8eb9SGleb Smirnoff 				    $5.host->addr.type == PF_ADDR_TABLE ||
26673b3a8eb9SGleb Smirnoff 				    DYNIF_MULTIADDR($5.host->addr)))
26683b3a8eb9SGleb Smirnoff 					r.rpool.opts |= PF_POOL_ROUNDROBIN;
26693b3a8eb9SGleb Smirnoff 				if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
26703b3a8eb9SGleb Smirnoff 				    PF_POOL_ROUNDROBIN &&
26713b3a8eb9SGleb Smirnoff 				    disallow_table($5.host, "tables are only "
26723b3a8eb9SGleb Smirnoff 				    "supported in round-robin routing pools"))
26733b3a8eb9SGleb Smirnoff 					YYERROR;
26743b3a8eb9SGleb Smirnoff 				if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
26753b3a8eb9SGleb Smirnoff 				    PF_POOL_ROUNDROBIN &&
26763b3a8eb9SGleb Smirnoff 				    disallow_alias($5.host, "interface (%s) "
26773b3a8eb9SGleb Smirnoff 				    "is only supported in round-robin "
26783b3a8eb9SGleb Smirnoff 				    "routing pools"))
26793b3a8eb9SGleb Smirnoff 					YYERROR;
26803b3a8eb9SGleb Smirnoff 				if ($5.host->next != NULL) {
26813b3a8eb9SGleb Smirnoff 					if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
26823b3a8eb9SGleb Smirnoff 					    PF_POOL_ROUNDROBIN) {
26833b3a8eb9SGleb Smirnoff 						yyerror("r.rpool.opts must "
26843b3a8eb9SGleb Smirnoff 						    "be PF_POOL_ROUNDROBIN");
26853b3a8eb9SGleb Smirnoff 						YYERROR;
26863b3a8eb9SGleb Smirnoff 					}
26873b3a8eb9SGleb Smirnoff 				}
26883b3a8eb9SGleb Smirnoff 			}
26893b3a8eb9SGleb Smirnoff 			if ($9.queues.qname != NULL) {
26903b3a8eb9SGleb Smirnoff 				if (strlcpy(r.qname, $9.queues.qname,
26913b3a8eb9SGleb Smirnoff 				    sizeof(r.qname)) >= sizeof(r.qname)) {
26923b3a8eb9SGleb Smirnoff 					yyerror("rule qname too long (max "
26933b3a8eb9SGleb Smirnoff 					    "%d chars)", sizeof(r.qname)-1);
26943b3a8eb9SGleb Smirnoff 					YYERROR;
26953b3a8eb9SGleb Smirnoff 				}
26963b3a8eb9SGleb Smirnoff 				free($9.queues.qname);
26973b3a8eb9SGleb Smirnoff 			}
26983b3a8eb9SGleb Smirnoff 			if ($9.queues.pqname != NULL) {
26993b3a8eb9SGleb Smirnoff 				if (strlcpy(r.pqname, $9.queues.pqname,
27003b3a8eb9SGleb Smirnoff 				    sizeof(r.pqname)) >= sizeof(r.pqname)) {
27013b3a8eb9SGleb Smirnoff 					yyerror("rule pqname too long (max "
27023b3a8eb9SGleb Smirnoff 					    "%d chars)", sizeof(r.pqname)-1);
27033b3a8eb9SGleb Smirnoff 					YYERROR;
27043b3a8eb9SGleb Smirnoff 				}
27053b3a8eb9SGleb Smirnoff 				free($9.queues.pqname);
27063b3a8eb9SGleb Smirnoff 			}
27073b3a8eb9SGleb Smirnoff #ifdef __FreeBSD__
27083b3a8eb9SGleb Smirnoff 			r.divert.port = $9.divert.port;
27093b3a8eb9SGleb Smirnoff #else
27103b3a8eb9SGleb Smirnoff 			if ((r.divert.port = $9.divert.port)) {
27113b3a8eb9SGleb Smirnoff 				if (r.direction == PF_OUT) {
27123b3a8eb9SGleb Smirnoff 					if ($9.divert.addr) {
27133b3a8eb9SGleb Smirnoff 						yyerror("address specified "
27143b3a8eb9SGleb Smirnoff 						    "for outgoing divert");
27153b3a8eb9SGleb Smirnoff 						YYERROR;
27163b3a8eb9SGleb Smirnoff 					}
27173b3a8eb9SGleb Smirnoff 					bzero(&r.divert.addr,
27183b3a8eb9SGleb Smirnoff 					    sizeof(r.divert.addr));
27193b3a8eb9SGleb Smirnoff 				} else {
27203b3a8eb9SGleb Smirnoff 					if (!$9.divert.addr) {
27213b3a8eb9SGleb Smirnoff 						yyerror("no address specified "
27223b3a8eb9SGleb Smirnoff 						    "for incoming divert");
27233b3a8eb9SGleb Smirnoff 						YYERROR;
27243b3a8eb9SGleb Smirnoff 					}
27253b3a8eb9SGleb Smirnoff 					if ($9.divert.addr->af != r.af) {
27263b3a8eb9SGleb Smirnoff 						yyerror("address family "
27273b3a8eb9SGleb Smirnoff 						    "mismatch for divert");
27283b3a8eb9SGleb Smirnoff 						YYERROR;
27293b3a8eb9SGleb Smirnoff 					}
27303b3a8eb9SGleb Smirnoff 					r.divert.addr =
27313b3a8eb9SGleb Smirnoff 					    $9.divert.addr->addr.v.a.addr;
27323b3a8eb9SGleb Smirnoff 				}
27333b3a8eb9SGleb Smirnoff 			}
27343b3a8eb9SGleb Smirnoff #endif
27353b3a8eb9SGleb Smirnoff 
273663b3c1c7SKristof Provost 			if ($9.dnpipe || $9.dnrpipe) {
273763b3c1c7SKristof Provost 				r.dnpipe = $9.dnpipe;
273863b3c1c7SKristof Provost 				r.dnrpipe = $9.dnrpipe;
273963b3c1c7SKristof Provost 				if ($9.free_flags & PFRULE_DN_IS_PIPE)
274063b3c1c7SKristof Provost 					r.free_flags |= PFRULE_DN_IS_PIPE;
274163b3c1c7SKristof Provost 				else
274263b3c1c7SKristof Provost 					r.free_flags |= PFRULE_DN_IS_QUEUE;
274363b3c1c7SKristof Provost 			}
274463b3c1c7SKristof Provost 
27453b3a8eb9SGleb Smirnoff 			expand_rule(&r, $4, $5.host, $7, $8.src_os,
27463b3a8eb9SGleb Smirnoff 			    $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
27473b3a8eb9SGleb Smirnoff 			    $9.uid, $9.gid, $9.icmpspec, "");
27483b3a8eb9SGleb Smirnoff 		}
27493b3a8eb9SGleb Smirnoff 		;
27503b3a8eb9SGleb Smirnoff 
27513b3a8eb9SGleb Smirnoff filter_opts	:	{
27523b3a8eb9SGleb Smirnoff 				bzero(&filter_opts, sizeof filter_opts);
27533b3a8eb9SGleb Smirnoff 				filter_opts.rtableid = -1;
27543b3a8eb9SGleb Smirnoff 			}
27553b3a8eb9SGleb Smirnoff 		    filter_opts_l
27563b3a8eb9SGleb Smirnoff 			{ $$ = filter_opts; }
27573b3a8eb9SGleb Smirnoff 		| /* empty */	{
27583b3a8eb9SGleb Smirnoff 			bzero(&filter_opts, sizeof filter_opts);
27593b3a8eb9SGleb Smirnoff 			filter_opts.rtableid = -1;
27603b3a8eb9SGleb Smirnoff 			$$ = filter_opts;
27613b3a8eb9SGleb Smirnoff 		}
27623b3a8eb9SGleb Smirnoff 		;
27633b3a8eb9SGleb Smirnoff 
27643b3a8eb9SGleb Smirnoff filter_opts_l	: filter_opts_l filter_opt
27653b3a8eb9SGleb Smirnoff 		| filter_opt
27663b3a8eb9SGleb Smirnoff 		;
27673b3a8eb9SGleb Smirnoff 
27683b3a8eb9SGleb Smirnoff filter_opt	: USER uids {
27693b3a8eb9SGleb Smirnoff 			if (filter_opts.uid)
27703b3a8eb9SGleb Smirnoff 				$2->tail->next = filter_opts.uid;
27713b3a8eb9SGleb Smirnoff 			filter_opts.uid = $2;
27723b3a8eb9SGleb Smirnoff 		}
27733b3a8eb9SGleb Smirnoff 		| GROUP gids {
27743b3a8eb9SGleb Smirnoff 			if (filter_opts.gid)
27753b3a8eb9SGleb Smirnoff 				$2->tail->next = filter_opts.gid;
27763b3a8eb9SGleb Smirnoff 			filter_opts.gid = $2;
27773b3a8eb9SGleb Smirnoff 		}
27783b3a8eb9SGleb Smirnoff 		| flags {
27793b3a8eb9SGleb Smirnoff 			if (filter_opts.marker & FOM_FLAGS) {
27803b3a8eb9SGleb Smirnoff 				yyerror("flags cannot be redefined");
27813b3a8eb9SGleb Smirnoff 				YYERROR;
27823b3a8eb9SGleb Smirnoff 			}
27833b3a8eb9SGleb Smirnoff 			filter_opts.marker |= FOM_FLAGS;
27843b3a8eb9SGleb Smirnoff 			filter_opts.flags.b1 |= $1.b1;
27853b3a8eb9SGleb Smirnoff 			filter_opts.flags.b2 |= $1.b2;
27863b3a8eb9SGleb Smirnoff 			filter_opts.flags.w |= $1.w;
27873b3a8eb9SGleb Smirnoff 			filter_opts.flags.w2 |= $1.w2;
27883b3a8eb9SGleb Smirnoff 		}
27893b3a8eb9SGleb Smirnoff 		| icmpspec {
27903b3a8eb9SGleb Smirnoff 			if (filter_opts.marker & FOM_ICMP) {
27913b3a8eb9SGleb Smirnoff 				yyerror("icmp-type cannot be redefined");
27923b3a8eb9SGleb Smirnoff 				YYERROR;
27933b3a8eb9SGleb Smirnoff 			}
27943b3a8eb9SGleb Smirnoff 			filter_opts.marker |= FOM_ICMP;
27953b3a8eb9SGleb Smirnoff 			filter_opts.icmpspec = $1;
27963b3a8eb9SGleb Smirnoff 		}
27973e248e0fSKristof Provost 		| PRIO NUMBER {
27983e248e0fSKristof Provost 			if (filter_opts.marker & FOM_PRIO) {
27993e248e0fSKristof Provost 				yyerror("prio cannot be redefined");
28003e248e0fSKristof Provost 				YYERROR;
28013e248e0fSKristof Provost 			}
28023e248e0fSKristof Provost 			if ($2 < 0 || $2 > PF_PRIO_MAX) {
28033e248e0fSKristof Provost 				yyerror("prio must be 0 - %u", PF_PRIO_MAX);
28043e248e0fSKristof Provost 				YYERROR;
28053e248e0fSKristof Provost 			}
28063e248e0fSKristof Provost 			filter_opts.marker |= FOM_PRIO;
28073e248e0fSKristof Provost 			filter_opts.prio = $2;
28083e248e0fSKristof Provost 		}
28093b3a8eb9SGleb Smirnoff 		| TOS tos {
28103b3a8eb9SGleb Smirnoff 			if (filter_opts.marker & FOM_TOS) {
28113b3a8eb9SGleb Smirnoff 				yyerror("tos cannot be redefined");
28123b3a8eb9SGleb Smirnoff 				YYERROR;
28133b3a8eb9SGleb Smirnoff 			}
28143b3a8eb9SGleb Smirnoff 			filter_opts.marker |= FOM_TOS;
28153b3a8eb9SGleb Smirnoff 			filter_opts.tos = $2;
28163b3a8eb9SGleb Smirnoff 		}
28173b3a8eb9SGleb Smirnoff 		| keep {
28183b3a8eb9SGleb Smirnoff 			if (filter_opts.marker & FOM_KEEP) {
28193b3a8eb9SGleb Smirnoff 				yyerror("modulate or keep cannot be redefined");
28203b3a8eb9SGleb Smirnoff 				YYERROR;
28213b3a8eb9SGleb Smirnoff 			}
28223b3a8eb9SGleb Smirnoff 			filter_opts.marker |= FOM_KEEP;
28233b3a8eb9SGleb Smirnoff 			filter_opts.keep.action = $1.action;
28243b3a8eb9SGleb Smirnoff 			filter_opts.keep.options = $1.options;
28253b3a8eb9SGleb Smirnoff 		}
282676c5eeccSKristof Provost 		| RIDENTIFIER number {
282776c5eeccSKristof Provost 			filter_opts.ridentifier = $2;
282876c5eeccSKristof Provost 		}
28293b3a8eb9SGleb Smirnoff 		| FRAGMENT {
28303b3a8eb9SGleb Smirnoff 			filter_opts.fragment = 1;
28313b3a8eb9SGleb Smirnoff 		}
28323b3a8eb9SGleb Smirnoff 		| ALLOWOPTS {
28333b3a8eb9SGleb Smirnoff 			filter_opts.allowopts = 1;
28343b3a8eb9SGleb Smirnoff 		}
28353b3a8eb9SGleb Smirnoff 		| label	{
28366fcc8e04SKristof Provost 			if (filter_opts.labelcount >= PF_RULE_MAX_LABEL_COUNT) {
28376fcc8e04SKristof Provost 				yyerror("label can only be used %d times", PF_RULE_MAX_LABEL_COUNT);
28383b3a8eb9SGleb Smirnoff 				YYERROR;
28393b3a8eb9SGleb Smirnoff 			}
28406fcc8e04SKristof Provost 			filter_opts.label[filter_opts.labelcount++] = $1;
28413b3a8eb9SGleb Smirnoff 		}
28423b3a8eb9SGleb Smirnoff 		| qname	{
28433b3a8eb9SGleb Smirnoff 			if (filter_opts.queues.qname) {
28443b3a8eb9SGleb Smirnoff 				yyerror("queue cannot be redefined");
28453b3a8eb9SGleb Smirnoff 				YYERROR;
28463b3a8eb9SGleb Smirnoff 			}
28473b3a8eb9SGleb Smirnoff 			filter_opts.queues = $1;
28483b3a8eb9SGleb Smirnoff 		}
284963b3c1c7SKristof Provost 		| DNPIPE number {
285063b3c1c7SKristof Provost 			filter_opts.dnpipe = $2;
285163b3c1c7SKristof Provost 			filter_opts.free_flags |= PFRULE_DN_IS_PIPE;
285263b3c1c7SKristof Provost 		}
285363b3c1c7SKristof Provost 		| DNPIPE '(' number ')' {
285463b3c1c7SKristof Provost 			filter_opts.dnpipe = $3;
285563b3c1c7SKristof Provost 			filter_opts.free_flags |= PFRULE_DN_IS_PIPE;
285663b3c1c7SKristof Provost 		}
285763b3c1c7SKristof Provost 		| DNPIPE '(' number comma number ')' {
285863b3c1c7SKristof Provost 			filter_opts.dnrpipe = $5;
285963b3c1c7SKristof Provost 			filter_opts.dnpipe = $3;
286063b3c1c7SKristof Provost 			filter_opts.free_flags |= PFRULE_DN_IS_PIPE;
286163b3c1c7SKristof Provost 		}
286263b3c1c7SKristof Provost 		| DNQUEUE number {
286363b3c1c7SKristof Provost 			filter_opts.dnpipe = $2;
286463b3c1c7SKristof Provost 			filter_opts.free_flags |= PFRULE_DN_IS_QUEUE;
286563b3c1c7SKristof Provost 		}
286663b3c1c7SKristof Provost 		| DNQUEUE '(' number comma number ')' {
286763b3c1c7SKristof Provost 			filter_opts.dnrpipe = $5;
286863b3c1c7SKristof Provost 			filter_opts.dnpipe = $3;
286963b3c1c7SKristof Provost 			filter_opts.free_flags |= PFRULE_DN_IS_QUEUE;
287063b3c1c7SKristof Provost 		}
287163b3c1c7SKristof Provost 		| DNQUEUE '(' number ')' {
287263b3c1c7SKristof Provost 			filter_opts.dnpipe = $3;
287363b3c1c7SKristof Provost 			filter_opts.free_flags |= PFRULE_DN_IS_QUEUE;
287463b3c1c7SKristof Provost 		}
28753b3a8eb9SGleb Smirnoff 		| TAG string				{
28763b3a8eb9SGleb Smirnoff 			filter_opts.tag = $2;
28773b3a8eb9SGleb Smirnoff 		}
28783b3a8eb9SGleb Smirnoff 		| not TAGGED string			{
28793b3a8eb9SGleb Smirnoff 			filter_opts.match_tag = $3;
28803b3a8eb9SGleb Smirnoff 			filter_opts.match_tag_not = $1;
28813b3a8eb9SGleb Smirnoff 		}
28823b3a8eb9SGleb Smirnoff 		| PROBABILITY probability		{
28833b3a8eb9SGleb Smirnoff 			double	p;
28843b3a8eb9SGleb Smirnoff 
28853b3a8eb9SGleb Smirnoff 			p = floor($2 * UINT_MAX + 0.5);
28863b3a8eb9SGleb Smirnoff 			if (p < 0.0 || p > UINT_MAX) {
28873b3a8eb9SGleb Smirnoff 				yyerror("invalid probability: %lf", p);
28883b3a8eb9SGleb Smirnoff 				YYERROR;
28893b3a8eb9SGleb Smirnoff 			}
28903b3a8eb9SGleb Smirnoff 			filter_opts.prob = (u_int32_t)p;
28913b3a8eb9SGleb Smirnoff 			if (filter_opts.prob == 0)
28923b3a8eb9SGleb Smirnoff 				filter_opts.prob = 1;
28933b3a8eb9SGleb Smirnoff 		}
28943b3a8eb9SGleb Smirnoff 		| RTABLE NUMBER				{
28953b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > rt_tableid_max()) {
28963b3a8eb9SGleb Smirnoff 				yyerror("invalid rtable id");
28973b3a8eb9SGleb Smirnoff 				YYERROR;
28983b3a8eb9SGleb Smirnoff 			}
28993b3a8eb9SGleb Smirnoff 			filter_opts.rtableid = $2;
29003b3a8eb9SGleb Smirnoff 		}
29013b3a8eb9SGleb Smirnoff 		| DIVERTTO portplain {
29023b3a8eb9SGleb Smirnoff #ifdef __FreeBSD__
29033b3a8eb9SGleb Smirnoff 			filter_opts.divert.port = $2.a;
29043b3a8eb9SGleb Smirnoff 			if (!filter_opts.divert.port) {
29053b3a8eb9SGleb Smirnoff 				yyerror("invalid divert port: %u", ntohs($2.a));
29063b3a8eb9SGleb Smirnoff 				YYERROR;
29073b3a8eb9SGleb Smirnoff 			}
29083b3a8eb9SGleb Smirnoff #endif
29093b3a8eb9SGleb Smirnoff 		}
29103b3a8eb9SGleb Smirnoff 		| DIVERTTO STRING PORT portplain {
29113b3a8eb9SGleb Smirnoff #ifndef __FreeBSD__
29123b3a8eb9SGleb Smirnoff 			if ((filter_opts.divert.addr = host($2)) == NULL) {
29133b3a8eb9SGleb Smirnoff 				yyerror("could not parse divert address: %s",
29143b3a8eb9SGleb Smirnoff 				    $2);
29153b3a8eb9SGleb Smirnoff 				free($2);
29163b3a8eb9SGleb Smirnoff 				YYERROR;
29173b3a8eb9SGleb Smirnoff 			}
29183b3a8eb9SGleb Smirnoff #else
29193b3a8eb9SGleb Smirnoff 			if ($2)
29203b3a8eb9SGleb Smirnoff #endif
29213b3a8eb9SGleb Smirnoff 			free($2);
29223b3a8eb9SGleb Smirnoff 			filter_opts.divert.port = $4.a;
29233b3a8eb9SGleb Smirnoff 			if (!filter_opts.divert.port) {
29243b3a8eb9SGleb Smirnoff 				yyerror("invalid divert port: %u", ntohs($4.a));
29253b3a8eb9SGleb Smirnoff 				YYERROR;
29263b3a8eb9SGleb Smirnoff 			}
29273b3a8eb9SGleb Smirnoff 		}
29283b3a8eb9SGleb Smirnoff 		| DIVERTREPLY {
29293b3a8eb9SGleb Smirnoff #ifdef __FreeBSD__
29303b3a8eb9SGleb Smirnoff 			yyerror("divert-reply has no meaning in FreeBSD pf(4)");
29313b3a8eb9SGleb Smirnoff 			YYERROR;
29323b3a8eb9SGleb Smirnoff #else
29333b3a8eb9SGleb Smirnoff 			filter_opts.divert.port = 1;	/* some random value */
29343b3a8eb9SGleb Smirnoff #endif
29353b3a8eb9SGleb Smirnoff 		}
29363e248e0fSKristof Provost 		| filter_sets
29373e248e0fSKristof Provost 		;
29383e248e0fSKristof Provost 
29393e248e0fSKristof Provost filter_sets	: SET '(' filter_sets_l ')'	{ $$ = filter_opts; }
29403e248e0fSKristof Provost 		| SET filter_set		{ $$ = filter_opts; }
29413e248e0fSKristof Provost 		;
29423e248e0fSKristof Provost 
29433e248e0fSKristof Provost filter_sets_l	: filter_sets_l comma filter_set
29443e248e0fSKristof Provost 		| filter_set
29453e248e0fSKristof Provost 		;
29463e248e0fSKristof Provost 
29473e248e0fSKristof Provost filter_set	: prio {
29483e248e0fSKristof Provost 			if (filter_opts.marker & FOM_SETPRIO) {
29493e248e0fSKristof Provost 				yyerror("prio cannot be redefined");
29503e248e0fSKristof Provost 				YYERROR;
29513e248e0fSKristof Provost 			}
29523e248e0fSKristof Provost 			filter_opts.marker |= FOM_SETPRIO;
29533e248e0fSKristof Provost 			filter_opts.set_prio[0] = $1.b1;
29543e248e0fSKristof Provost 			filter_opts.set_prio[1] = $1.b2;
29553e248e0fSKristof Provost 		}
29563e248e0fSKristof Provost prio		: PRIO NUMBER {
29573e248e0fSKristof Provost 			if ($2 < 0 || $2 > PF_PRIO_MAX) {
29583e248e0fSKristof Provost 				yyerror("prio must be 0 - %u", PF_PRIO_MAX);
29593e248e0fSKristof Provost 				YYERROR;
29603e248e0fSKristof Provost 			}
29613e248e0fSKristof Provost 			$$.b1 = $$.b2 = $2;
29623e248e0fSKristof Provost 		}
29633e248e0fSKristof Provost 		| PRIO '(' NUMBER comma NUMBER ')' {
29643e248e0fSKristof Provost 			if ($3 < 0 || $3 > PF_PRIO_MAX ||
29653e248e0fSKristof Provost 			    $5 < 0 || $5 > PF_PRIO_MAX) {
29663e248e0fSKristof Provost 				yyerror("prio must be 0 - %u", PF_PRIO_MAX);
29673e248e0fSKristof Provost 				YYERROR;
29683e248e0fSKristof Provost 			}
29693e248e0fSKristof Provost 			$$.b1 = $3;
29703e248e0fSKristof Provost 			$$.b2 = $5;
29713e248e0fSKristof Provost 		}
29723b3a8eb9SGleb Smirnoff 		;
29733b3a8eb9SGleb Smirnoff 
29743b3a8eb9SGleb Smirnoff probability	: STRING				{
29753b3a8eb9SGleb Smirnoff 			char	*e;
29763b3a8eb9SGleb Smirnoff 			double	 p = strtod($1, &e);
29773b3a8eb9SGleb Smirnoff 
29783b3a8eb9SGleb Smirnoff 			if (*e == '%') {
29793b3a8eb9SGleb Smirnoff 				p *= 0.01;
29803b3a8eb9SGleb Smirnoff 				e++;
29813b3a8eb9SGleb Smirnoff 			}
29823b3a8eb9SGleb Smirnoff 			if (*e) {
29833b3a8eb9SGleb Smirnoff 				yyerror("invalid probability: %s", $1);
29843b3a8eb9SGleb Smirnoff 				free($1);
29853b3a8eb9SGleb Smirnoff 				YYERROR;
29863b3a8eb9SGleb Smirnoff 			}
29873b3a8eb9SGleb Smirnoff 			free($1);
29883b3a8eb9SGleb Smirnoff 			$$ = p;
29893b3a8eb9SGleb Smirnoff 		}
29903b3a8eb9SGleb Smirnoff 		| NUMBER				{
29913b3a8eb9SGleb Smirnoff 			$$ = (double)$1;
29923b3a8eb9SGleb Smirnoff 		}
29933b3a8eb9SGleb Smirnoff 		;
29943b3a8eb9SGleb Smirnoff 
29953b3a8eb9SGleb Smirnoff 
2996150182e3SKristof Provost action		: PASS 			{
2997150182e3SKristof Provost 			$$.b1 = PF_PASS;
2998150182e3SKristof Provost 			$$.b2 = failpolicy;
2999150182e3SKristof Provost 			$$.w = returnicmpdefault;
3000150182e3SKristof Provost 			$$.w2 = returnicmp6default;
3001150182e3SKristof Provost 		}
3002ef950daaSKristof Provost 		| MATCH			{ $$.b1 = PF_MATCH; $$.b2 = $$.w = 0; }
30033b3a8eb9SGleb Smirnoff 		| BLOCK blockspec	{ $$ = $2; $$.b1 = PF_DROP; }
30043b3a8eb9SGleb Smirnoff 		;
30053b3a8eb9SGleb Smirnoff 
30063b3a8eb9SGleb Smirnoff blockspec	: /* empty */		{
30073b3a8eb9SGleb Smirnoff 			$$.b2 = blockpolicy;
30083b3a8eb9SGleb Smirnoff 			$$.w = returnicmpdefault;
30093b3a8eb9SGleb Smirnoff 			$$.w2 = returnicmp6default;
30103b3a8eb9SGleb Smirnoff 		}
30113b3a8eb9SGleb Smirnoff 		| DROP			{
30123b3a8eb9SGleb Smirnoff 			$$.b2 = PFRULE_DROP;
30133b3a8eb9SGleb Smirnoff 			$$.w = 0;
30143b3a8eb9SGleb Smirnoff 			$$.w2 = 0;
30153b3a8eb9SGleb Smirnoff 		}
30163b3a8eb9SGleb Smirnoff 		| RETURNRST		{
30173b3a8eb9SGleb Smirnoff 			$$.b2 = PFRULE_RETURNRST;
30183b3a8eb9SGleb Smirnoff 			$$.w = 0;
30193b3a8eb9SGleb Smirnoff 			$$.w2 = 0;
30203b3a8eb9SGleb Smirnoff 		}
30213b3a8eb9SGleb Smirnoff 		| RETURNRST '(' TTL NUMBER ')'	{
30223b3a8eb9SGleb Smirnoff 			if ($4 < 0 || $4 > 255) {
30233b3a8eb9SGleb Smirnoff 				yyerror("illegal ttl value %d", $4);
30243b3a8eb9SGleb Smirnoff 				YYERROR;
30253b3a8eb9SGleb Smirnoff 			}
30263b3a8eb9SGleb Smirnoff 			$$.b2 = PFRULE_RETURNRST;
30273b3a8eb9SGleb Smirnoff 			$$.w = $4;
30283b3a8eb9SGleb Smirnoff 			$$.w2 = 0;
30293b3a8eb9SGleb Smirnoff 		}
30303b3a8eb9SGleb Smirnoff 		| RETURNICMP		{
30313b3a8eb9SGleb Smirnoff 			$$.b2 = PFRULE_RETURNICMP;
30323b3a8eb9SGleb Smirnoff 			$$.w = returnicmpdefault;
30333b3a8eb9SGleb Smirnoff 			$$.w2 = returnicmp6default;
30343b3a8eb9SGleb Smirnoff 		}
30353b3a8eb9SGleb Smirnoff 		| RETURNICMP6		{
30363b3a8eb9SGleb Smirnoff 			$$.b2 = PFRULE_RETURNICMP;
30373b3a8eb9SGleb Smirnoff 			$$.w = returnicmpdefault;
30383b3a8eb9SGleb Smirnoff 			$$.w2 = returnicmp6default;
30393b3a8eb9SGleb Smirnoff 		}
30403b3a8eb9SGleb Smirnoff 		| RETURNICMP '(' reticmpspec ')'	{
30413b3a8eb9SGleb Smirnoff 			$$.b2 = PFRULE_RETURNICMP;
30423b3a8eb9SGleb Smirnoff 			$$.w = $3;
30433b3a8eb9SGleb Smirnoff 			$$.w2 = returnicmpdefault;
30443b3a8eb9SGleb Smirnoff 		}
30453b3a8eb9SGleb Smirnoff 		| RETURNICMP6 '(' reticmp6spec ')'	{
30463b3a8eb9SGleb Smirnoff 			$$.b2 = PFRULE_RETURNICMP;
30473b3a8eb9SGleb Smirnoff 			$$.w = returnicmpdefault;
30483b3a8eb9SGleb Smirnoff 			$$.w2 = $3;
30493b3a8eb9SGleb Smirnoff 		}
30503b3a8eb9SGleb Smirnoff 		| RETURNICMP '(' reticmpspec comma reticmp6spec ')' {
30513b3a8eb9SGleb Smirnoff 			$$.b2 = PFRULE_RETURNICMP;
30523b3a8eb9SGleb Smirnoff 			$$.w = $3;
30533b3a8eb9SGleb Smirnoff 			$$.w2 = $5;
30543b3a8eb9SGleb Smirnoff 		}
30553b3a8eb9SGleb Smirnoff 		| RETURN {
30563b3a8eb9SGleb Smirnoff 			$$.b2 = PFRULE_RETURN;
30573b3a8eb9SGleb Smirnoff 			$$.w = returnicmpdefault;
30583b3a8eb9SGleb Smirnoff 			$$.w2 = returnicmp6default;
30593b3a8eb9SGleb Smirnoff 		}
30603b3a8eb9SGleb Smirnoff 		;
30613b3a8eb9SGleb Smirnoff 
30623b3a8eb9SGleb Smirnoff reticmpspec	: STRING			{
30633b3a8eb9SGleb Smirnoff 			if (!($$ = parseicmpspec($1, AF_INET))) {
30643b3a8eb9SGleb Smirnoff 				free($1);
30653b3a8eb9SGleb Smirnoff 				YYERROR;
30663b3a8eb9SGleb Smirnoff 			}
30673b3a8eb9SGleb Smirnoff 			free($1);
30683b3a8eb9SGleb Smirnoff 		}
30693b3a8eb9SGleb Smirnoff 		| NUMBER			{
30703b3a8eb9SGleb Smirnoff 			u_int8_t		icmptype;
30713b3a8eb9SGleb Smirnoff 
30723b3a8eb9SGleb Smirnoff 			if ($1 < 0 || $1 > 255) {
30733b3a8eb9SGleb Smirnoff 				yyerror("invalid icmp code %lu", $1);
30743b3a8eb9SGleb Smirnoff 				YYERROR;
30753b3a8eb9SGleb Smirnoff 			}
30763b3a8eb9SGleb Smirnoff 			icmptype = returnicmpdefault >> 8;
30773b3a8eb9SGleb Smirnoff 			$$ = (icmptype << 8 | $1);
30783b3a8eb9SGleb Smirnoff 		}
30793b3a8eb9SGleb Smirnoff 		;
30803b3a8eb9SGleb Smirnoff 
30813b3a8eb9SGleb Smirnoff reticmp6spec	: STRING			{
30823b3a8eb9SGleb Smirnoff 			if (!($$ = parseicmpspec($1, AF_INET6))) {
30833b3a8eb9SGleb Smirnoff 				free($1);
30843b3a8eb9SGleb Smirnoff 				YYERROR;
30853b3a8eb9SGleb Smirnoff 			}
30863b3a8eb9SGleb Smirnoff 			free($1);
30873b3a8eb9SGleb Smirnoff 		}
30883b3a8eb9SGleb Smirnoff 		| NUMBER			{
30893b3a8eb9SGleb Smirnoff 			u_int8_t		icmptype;
30903b3a8eb9SGleb Smirnoff 
30913b3a8eb9SGleb Smirnoff 			if ($1 < 0 || $1 > 255) {
30923b3a8eb9SGleb Smirnoff 				yyerror("invalid icmp code %lu", $1);
30933b3a8eb9SGleb Smirnoff 				YYERROR;
30943b3a8eb9SGleb Smirnoff 			}
30953b3a8eb9SGleb Smirnoff 			icmptype = returnicmp6default >> 8;
30963b3a8eb9SGleb Smirnoff 			$$ = (icmptype << 8 | $1);
30973b3a8eb9SGleb Smirnoff 		}
30983b3a8eb9SGleb Smirnoff 		;
30993b3a8eb9SGleb Smirnoff 
31003b3a8eb9SGleb Smirnoff dir		: /* empty */			{ $$ = PF_INOUT; }
31013b3a8eb9SGleb Smirnoff 		| IN				{ $$ = PF_IN; }
31023b3a8eb9SGleb Smirnoff 		| OUT				{ $$ = PF_OUT; }
31033b3a8eb9SGleb Smirnoff 		;
31043b3a8eb9SGleb Smirnoff 
31053b3a8eb9SGleb Smirnoff quick		: /* empty */			{ $$.quick = 0; }
31063b3a8eb9SGleb Smirnoff 		| QUICK				{ $$.quick = 1; }
31073b3a8eb9SGleb Smirnoff 		;
31083b3a8eb9SGleb Smirnoff 
31093b3a8eb9SGleb Smirnoff logquick	: /* empty */	{ $$.log = 0; $$.quick = 0; $$.logif = 0; }
31103b3a8eb9SGleb Smirnoff 		| log		{ $$ = $1; $$.quick = 0; }
31113b3a8eb9SGleb Smirnoff 		| QUICK		{ $$.quick = 1; $$.log = 0; $$.logif = 0; }
31123b3a8eb9SGleb Smirnoff 		| log QUICK	{ $$ = $1; $$.quick = 1; }
31133b3a8eb9SGleb Smirnoff 		| QUICK log	{ $$ = $2; $$.quick = 1; }
31143b3a8eb9SGleb Smirnoff 		;
31153b3a8eb9SGleb Smirnoff 
31163b3a8eb9SGleb Smirnoff log		: LOG			{ $$.log = PF_LOG; $$.logif = 0; }
31173b3a8eb9SGleb Smirnoff 		| LOG '(' logopts ')'	{
31183b3a8eb9SGleb Smirnoff 			$$.log = PF_LOG | $3.log;
31193b3a8eb9SGleb Smirnoff 			$$.logif = $3.logif;
31203b3a8eb9SGleb Smirnoff 		}
31213b3a8eb9SGleb Smirnoff 		;
31223b3a8eb9SGleb Smirnoff 
31233b3a8eb9SGleb Smirnoff logopts		: logopt			{ $$ = $1; }
31243b3a8eb9SGleb Smirnoff 		| logopts comma logopt		{
31253b3a8eb9SGleb Smirnoff 			$$.log = $1.log | $3.log;
31263b3a8eb9SGleb Smirnoff 			$$.logif = $3.logif;
31273b3a8eb9SGleb Smirnoff 			if ($$.logif == 0)
31283b3a8eb9SGleb Smirnoff 				$$.logif = $1.logif;
31293b3a8eb9SGleb Smirnoff 		}
31303b3a8eb9SGleb Smirnoff 		;
31313b3a8eb9SGleb Smirnoff 
31323b3a8eb9SGleb Smirnoff logopt		: ALL		{ $$.log = PF_LOG_ALL; $$.logif = 0; }
31333b3a8eb9SGleb Smirnoff 		| USER		{ $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
31343b3a8eb9SGleb Smirnoff 		| GROUP		{ $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
31353b3a8eb9SGleb Smirnoff 		| TO string	{
31363b3a8eb9SGleb Smirnoff 			const char	*errstr;
31373b3a8eb9SGleb Smirnoff 			u_int		 i;
31383b3a8eb9SGleb Smirnoff 
31393b3a8eb9SGleb Smirnoff 			$$.log = 0;
31403b3a8eb9SGleb Smirnoff 			if (strncmp($2, "pflog", 5)) {
31413b3a8eb9SGleb Smirnoff 				yyerror("%s: should be a pflog interface", $2);
31423b3a8eb9SGleb Smirnoff 				free($2);
31433b3a8eb9SGleb Smirnoff 				YYERROR;
31443b3a8eb9SGleb Smirnoff 			}
31453b3a8eb9SGleb Smirnoff 			i = strtonum($2 + 5, 0, 255, &errstr);
31463b3a8eb9SGleb Smirnoff 			if (errstr) {
31473b3a8eb9SGleb Smirnoff 				yyerror("%s: %s", $2, errstr);
31483b3a8eb9SGleb Smirnoff 				free($2);
31493b3a8eb9SGleb Smirnoff 				YYERROR;
31503b3a8eb9SGleb Smirnoff 			}
31513b3a8eb9SGleb Smirnoff 			free($2);
31523b3a8eb9SGleb Smirnoff 			$$.logif = i;
31533b3a8eb9SGleb Smirnoff 		}
31543b3a8eb9SGleb Smirnoff 		;
31553b3a8eb9SGleb Smirnoff 
31563b3a8eb9SGleb Smirnoff interface	: /* empty */			{ $$ = NULL; }
31573b3a8eb9SGleb Smirnoff 		| ON if_item_not		{ $$ = $2; }
31583b3a8eb9SGleb Smirnoff 		| ON '{' optnl if_list '}'	{ $$ = $4; }
31593b3a8eb9SGleb Smirnoff 		;
31603b3a8eb9SGleb Smirnoff 
31613b3a8eb9SGleb Smirnoff if_list		: if_item_not optnl		{ $$ = $1; }
31623b3a8eb9SGleb Smirnoff 		| if_list comma if_item_not optnl	{
31633b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
31643b3a8eb9SGleb Smirnoff 			$1->tail = $3;
31653b3a8eb9SGleb Smirnoff 			$$ = $1;
31663b3a8eb9SGleb Smirnoff 		}
31673b3a8eb9SGleb Smirnoff 		;
31683b3a8eb9SGleb Smirnoff 
31693b3a8eb9SGleb Smirnoff if_item_not	: not if_item			{ $$ = $2; $$->not = $1; }
31703b3a8eb9SGleb Smirnoff 		;
31713b3a8eb9SGleb Smirnoff 
31723b3a8eb9SGleb Smirnoff if_item		: STRING			{
31733b3a8eb9SGleb Smirnoff 			struct node_host	*n;
31743b3a8eb9SGleb Smirnoff 
31753b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_if));
31763b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
31773b3a8eb9SGleb Smirnoff 				err(1, "if_item: calloc");
31783b3a8eb9SGleb Smirnoff 			if (strlcpy($$->ifname, $1, sizeof($$->ifname)) >=
31793b3a8eb9SGleb Smirnoff 			    sizeof($$->ifname)) {
31803b3a8eb9SGleb Smirnoff 				free($1);
31813b3a8eb9SGleb Smirnoff 				free($$);
31823b3a8eb9SGleb Smirnoff 				yyerror("interface name too long");
31833b3a8eb9SGleb Smirnoff 				YYERROR;
31843b3a8eb9SGleb Smirnoff 			}
31853b3a8eb9SGleb Smirnoff 
31863b3a8eb9SGleb Smirnoff 			if ((n = ifa_exists($1)) != NULL)
31873b3a8eb9SGleb Smirnoff 				$$->ifa_flags = n->ifa_flags;
31883b3a8eb9SGleb Smirnoff 
31893b3a8eb9SGleb Smirnoff 			free($1);
31903b3a8eb9SGleb Smirnoff 			$$->not = 0;
31913b3a8eb9SGleb Smirnoff 			$$->next = NULL;
31923b3a8eb9SGleb Smirnoff 			$$->tail = $$;
31933b3a8eb9SGleb Smirnoff 		}
31943b3a8eb9SGleb Smirnoff 		;
31953b3a8eb9SGleb Smirnoff 
31963b3a8eb9SGleb Smirnoff af		: /* empty */			{ $$ = 0; }
31973b3a8eb9SGleb Smirnoff 		| INET				{ $$ = AF_INET; }
31983b3a8eb9SGleb Smirnoff 		| INET6				{ $$ = AF_INET6; }
31993b3a8eb9SGleb Smirnoff 		;
32003b3a8eb9SGleb Smirnoff 
32012b29ceb8SKristof Provost etherproto	: /* empty */				{ $$ = NULL; }
32022b29ceb8SKristof Provost 		| PROTO etherproto_item			{ $$ = $2; }
32032b29ceb8SKristof Provost 		| PROTO '{' optnl etherproto_list '}'	{ $$ = $4; }
32042b29ceb8SKristof Provost 		;
32052b29ceb8SKristof Provost 
32062b29ceb8SKristof Provost etherproto_list	: etherproto_item optnl			{ $$ = $1; }
32072b29ceb8SKristof Provost 		| etherproto_list comma etherproto_item optnl	{
32082b29ceb8SKristof Provost 			$1->tail->next = $3;
32092b29ceb8SKristof Provost 			$1->tail = $3;
32102b29ceb8SKristof Provost 			$$ = $1;
32112b29ceb8SKristof Provost 		}
32122b29ceb8SKristof Provost 		;
32132b29ceb8SKristof Provost 
32142b29ceb8SKristof Provost etherproto_item	: etherprotoval		{
32152b29ceb8SKristof Provost 			u_int16_t	pr;
32162b29ceb8SKristof Provost 
32172b29ceb8SKristof Provost 			pr = (u_int16_t)$1;
32182b29ceb8SKristof Provost 			if (pr == 0) {
32192b29ceb8SKristof Provost 				yyerror("proto 0 cannot be used");
32202b29ceb8SKristof Provost 				YYERROR;
32212b29ceb8SKristof Provost 			}
32222b29ceb8SKristof Provost 			$$ = calloc(1, sizeof(struct node_proto));
32232b29ceb8SKristof Provost 			if ($$ == NULL)
32242b29ceb8SKristof Provost 				err(1, "proto_item: calloc");
32252b29ceb8SKristof Provost 			$$->proto = pr;
32262b29ceb8SKristof Provost 			$$->next = NULL;
32272b29ceb8SKristof Provost 			$$->tail = $$;
32282b29ceb8SKristof Provost 		}
32292b29ceb8SKristof Provost 		;
32302b29ceb8SKristof Provost 
32312b29ceb8SKristof Provost etherprotoval	: NUMBER			{
32322b29ceb8SKristof Provost 			if ($1 < 0 || $1 > 65565) {
32332b29ceb8SKristof Provost 				yyerror("protocol outside range");
32342b29ceb8SKristof Provost 				YYERROR;
32352b29ceb8SKristof Provost 			}
32362b29ceb8SKristof Provost 		}
32372b29ceb8SKristof Provost 		| STRING
32382b29ceb8SKristof Provost 		{
32392b29ceb8SKristof Provost 			if (!strncmp($1, "0x", 2)) {
32402b29ceb8SKristof Provost 				if (sscanf($1, "0x%4x", &$$) != 1) {
32412b29ceb8SKristof Provost 					free($1);
32422b29ceb8SKristof Provost 					yyerror("invalid EtherType hex");
32432b29ceb8SKristof Provost 					YYERROR;
32442b29ceb8SKristof Provost 				}
32452b29ceb8SKristof Provost 			} else {
32462b29ceb8SKristof Provost 				yyerror("Symbolic EtherType not yet supported");
32472b29ceb8SKristof Provost 			}
32482b29ceb8SKristof Provost 		}
32492b29ceb8SKristof Provost 		;
32502b29ceb8SKristof Provost 
32513b3a8eb9SGleb Smirnoff proto		: /* empty */				{ $$ = NULL; }
32523b3a8eb9SGleb Smirnoff 		| PROTO proto_item			{ $$ = $2; }
32533b3a8eb9SGleb Smirnoff 		| PROTO '{' optnl proto_list '}'	{ $$ = $4; }
32543b3a8eb9SGleb Smirnoff 		;
32553b3a8eb9SGleb Smirnoff 
32563b3a8eb9SGleb Smirnoff proto_list	: proto_item optnl		{ $$ = $1; }
32573b3a8eb9SGleb Smirnoff 		| proto_list comma proto_item optnl	{
32583b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
32593b3a8eb9SGleb Smirnoff 			$1->tail = $3;
32603b3a8eb9SGleb Smirnoff 			$$ = $1;
32613b3a8eb9SGleb Smirnoff 		}
32623b3a8eb9SGleb Smirnoff 		;
32633b3a8eb9SGleb Smirnoff 
32643b3a8eb9SGleb Smirnoff proto_item	: protoval			{
32653b3a8eb9SGleb Smirnoff 			u_int8_t	pr;
32663b3a8eb9SGleb Smirnoff 
32673b3a8eb9SGleb Smirnoff 			pr = (u_int8_t)$1;
32683b3a8eb9SGleb Smirnoff 			if (pr == 0) {
32693b3a8eb9SGleb Smirnoff 				yyerror("proto 0 cannot be used");
32703b3a8eb9SGleb Smirnoff 				YYERROR;
32713b3a8eb9SGleb Smirnoff 			}
32723b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_proto));
32733b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
32743b3a8eb9SGleb Smirnoff 				err(1, "proto_item: calloc");
32753b3a8eb9SGleb Smirnoff 			$$->proto = pr;
32763b3a8eb9SGleb Smirnoff 			$$->next = NULL;
32773b3a8eb9SGleb Smirnoff 			$$->tail = $$;
32783b3a8eb9SGleb Smirnoff 		}
32793b3a8eb9SGleb Smirnoff 		;
32803b3a8eb9SGleb Smirnoff 
32813b3a8eb9SGleb Smirnoff protoval	: STRING			{
32823b3a8eb9SGleb Smirnoff 			struct protoent	*p;
32833b3a8eb9SGleb Smirnoff 
32843b3a8eb9SGleb Smirnoff 			p = getprotobyname($1);
32853b3a8eb9SGleb Smirnoff 			if (p == NULL) {
32863b3a8eb9SGleb Smirnoff 				yyerror("unknown protocol %s", $1);
32873b3a8eb9SGleb Smirnoff 				free($1);
32883b3a8eb9SGleb Smirnoff 				YYERROR;
32893b3a8eb9SGleb Smirnoff 			}
32903b3a8eb9SGleb Smirnoff 			$$ = p->p_proto;
32913b3a8eb9SGleb Smirnoff 			free($1);
32923b3a8eb9SGleb Smirnoff 		}
32933b3a8eb9SGleb Smirnoff 		| NUMBER			{
32943b3a8eb9SGleb Smirnoff 			if ($1 < 0 || $1 > 255) {
32953b3a8eb9SGleb Smirnoff 				yyerror("protocol outside range");
32963b3a8eb9SGleb Smirnoff 				YYERROR;
32973b3a8eb9SGleb Smirnoff 			}
32983b3a8eb9SGleb Smirnoff 		}
32993b3a8eb9SGleb Smirnoff 		;
33003b3a8eb9SGleb Smirnoff 
33018a42005dSKristof Provost l3fromto	: /* empty */			{
33028a42005dSKristof Provost 			bzero(&$$, sizeof($$));
33038a42005dSKristof Provost 		}
33048a42005dSKristof Provost 		| L3 fromto			{
33053468cd95SKristof Provost 			if ($2.src.host != NULL &&
3306812839e5SKristof Provost 			    $2.src.host->addr.type != PF_ADDR_ADDRMASK &&
3307812839e5SKristof Provost 			    $2.src.host->addr.type != PF_ADDR_TABLE) {
3308812839e5SKristof Provost 				yyerror("from must be an address or table");
33093468cd95SKristof Provost 				YYERROR;
33103468cd95SKristof Provost 			}
33113468cd95SKristof Provost 			if ($2.dst.host != NULL &&
3312812839e5SKristof Provost 			    $2.dst.host->addr.type != PF_ADDR_ADDRMASK &&
3313812839e5SKristof Provost 			    $2.dst.host->addr.type != PF_ADDR_TABLE) {
3314812839e5SKristof Provost 				yyerror("to must be an address or table");
33153468cd95SKristof Provost 				YYERROR;
33163468cd95SKristof Provost 			}
33178a42005dSKristof Provost 			$$ = $2;
33188a42005dSKristof Provost 		}
33198a42005dSKristof Provost 		;
33202b29ceb8SKristof Provost etherfromto	: ALL				{
332187a89d6eSKristof Provost 			$$.src = NULL;
332287a89d6eSKristof Provost 			$$.dst = NULL;
33232b29ceb8SKristof Provost 		}
33242b29ceb8SKristof Provost 		| etherfrom etherto		{
332587a89d6eSKristof Provost 			$$.src = $1.mac;
332687a89d6eSKristof Provost 			$$.dst = $2.mac;
33272b29ceb8SKristof Provost 		}
33282b29ceb8SKristof Provost 		;
33292b29ceb8SKristof Provost 
33302b29ceb8SKristof Provost etherfrom	: /* emtpy */			{
33312b29ceb8SKristof Provost 			bzero(&$$, sizeof($$));
33322b29ceb8SKristof Provost 		}
333387a89d6eSKristof Provost 		| FROM macspec			{
333487a89d6eSKristof Provost 			$$.mac = $2;
33352b29ceb8SKristof Provost 		}
33362b29ceb8SKristof Provost 		;
33372b29ceb8SKristof Provost 
33382b29ceb8SKristof Provost etherto		: /* empty */			{
33392b29ceb8SKristof Provost 			bzero(&$$, sizeof($$));
33402b29ceb8SKristof Provost 		}
334187a89d6eSKristof Provost 		| TO macspec			{
334287a89d6eSKristof Provost 			$$.mac = $2;
33432b29ceb8SKristof Provost 		}
33442b29ceb8SKristof Provost 		;
33452b29ceb8SKristof Provost 
3346b590f17aSKristof Provost mac		: string '/' NUMBER		{
3347b590f17aSKristof Provost 			$$ = node_mac_from_string_masklen($1, $3);
33482b29ceb8SKristof Provost 			free($1);
3349b590f17aSKristof Provost 			if ($$ == NULL)
33502b29ceb8SKristof Provost 				YYERROR;
33512b29ceb8SKristof Provost 		}
3352b590f17aSKristof Provost 		| string			{
3353b590f17aSKristof Provost 			if (strchr($1, '&')) {
3354b590f17aSKristof Provost 				/* mac&mask */
3355b590f17aSKristof Provost 				char *mac = strtok($1, "&");
3356b590f17aSKristof Provost 				char *mask = strtok(NULL, "&");
3357b590f17aSKristof Provost 				$$ = node_mac_from_string_mask(mac, mask);
3358b590f17aSKristof Provost 			} else {
3359b590f17aSKristof Provost 				$$ = node_mac_from_string($1);
3360b590f17aSKristof Provost 			}
336187a89d6eSKristof Provost 			free($1);
3362b590f17aSKristof Provost 			if ($$ == NULL)
3363b590f17aSKristof Provost 				YYERROR;
3364b590f17aSKristof Provost 
336587a89d6eSKristof Provost 		}
336687a89d6eSKristof Provost xmac		: not mac {
336787a89d6eSKristof Provost 			struct node_mac	*n;
336887a89d6eSKristof Provost 
336987a89d6eSKristof Provost 			for (n = $2; n != NULL; n = n->next)
337087a89d6eSKristof Provost 				n->neg = $1;
337187a89d6eSKristof Provost 			$$ = $2;
33722b29ceb8SKristof Provost 		}
33732b29ceb8SKristof Provost 		;
337487a89d6eSKristof Provost macspec		: xmac {
337587a89d6eSKristof Provost 			$$ = $1;
337687a89d6eSKristof Provost 		}
337787a89d6eSKristof Provost 		| '{' optnl mac_list '}'
337887a89d6eSKristof Provost 		{
337987a89d6eSKristof Provost 			$$ = $3;
338087a89d6eSKristof Provost 		}
338187a89d6eSKristof Provost 		;
338287a89d6eSKristof Provost mac_list	: xmac optnl {
338387a89d6eSKristof Provost 			$$ = $1;
338487a89d6eSKristof Provost 		}
338587a89d6eSKristof Provost 		| mac_list comma xmac {
338687a89d6eSKristof Provost 			if ($3 == NULL)
338787a89d6eSKristof Provost 				$$ = $1;
338887a89d6eSKristof Provost 			else if ($1 == NULL)
338987a89d6eSKristof Provost 				$$ = $3;
339087a89d6eSKristof Provost 			else {
339187a89d6eSKristof Provost 				$1->tail->next = $3;
339287a89d6eSKristof Provost 				$1->tail = $3->tail;
339387a89d6eSKristof Provost 				$$ = $1;
339487a89d6eSKristof Provost 			}
339587a89d6eSKristof Provost 		}
33962b29ceb8SKristof Provost 
33973b3a8eb9SGleb Smirnoff fromto		: ALL				{
33983b3a8eb9SGleb Smirnoff 			$$.src.host = NULL;
33993b3a8eb9SGleb Smirnoff 			$$.src.port = NULL;
34003b3a8eb9SGleb Smirnoff 			$$.dst.host = NULL;
34013b3a8eb9SGleb Smirnoff 			$$.dst.port = NULL;
34023b3a8eb9SGleb Smirnoff 			$$.src_os = NULL;
34033b3a8eb9SGleb Smirnoff 		}
34043b3a8eb9SGleb Smirnoff 		| from os to			{
34053b3a8eb9SGleb Smirnoff 			$$.src = $1;
34063b3a8eb9SGleb Smirnoff 			$$.src_os = $2;
34073b3a8eb9SGleb Smirnoff 			$$.dst = $3;
34083b3a8eb9SGleb Smirnoff 		}
34093b3a8eb9SGleb Smirnoff 		;
34103b3a8eb9SGleb Smirnoff 
34113b3a8eb9SGleb Smirnoff os		: /* empty */			{ $$ = NULL; }
34123b3a8eb9SGleb Smirnoff 		| OS xos			{ $$ = $2; }
34133b3a8eb9SGleb Smirnoff 		| OS '{' optnl os_list '}'	{ $$ = $4; }
34143b3a8eb9SGleb Smirnoff 		;
34153b3a8eb9SGleb Smirnoff 
34163b3a8eb9SGleb Smirnoff xos		: STRING {
34173b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_os));
34183b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
34193b3a8eb9SGleb Smirnoff 				err(1, "os: calloc");
34203b3a8eb9SGleb Smirnoff 			$$->os = $1;
34213b3a8eb9SGleb Smirnoff 			$$->tail = $$;
34223b3a8eb9SGleb Smirnoff 		}
34233b3a8eb9SGleb Smirnoff 		;
34243b3a8eb9SGleb Smirnoff 
34253b3a8eb9SGleb Smirnoff os_list		: xos optnl 			{ $$ = $1; }
34263b3a8eb9SGleb Smirnoff 		| os_list comma xos optnl	{
34273b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
34283b3a8eb9SGleb Smirnoff 			$1->tail = $3;
34293b3a8eb9SGleb Smirnoff 			$$ = $1;
34303b3a8eb9SGleb Smirnoff 		}
34313b3a8eb9SGleb Smirnoff 		;
34323b3a8eb9SGleb Smirnoff 
34333b3a8eb9SGleb Smirnoff from		: /* empty */			{
34343b3a8eb9SGleb Smirnoff 			$$.host = NULL;
34353b3a8eb9SGleb Smirnoff 			$$.port = NULL;
34363b3a8eb9SGleb Smirnoff 		}
34373b3a8eb9SGleb Smirnoff 		| FROM ipportspec		{
34383b3a8eb9SGleb Smirnoff 			$$ = $2;
34393b3a8eb9SGleb Smirnoff 		}
34403b3a8eb9SGleb Smirnoff 		;
34413b3a8eb9SGleb Smirnoff 
34423b3a8eb9SGleb Smirnoff to		: /* empty */			{
34433b3a8eb9SGleb Smirnoff 			$$.host = NULL;
34443b3a8eb9SGleb Smirnoff 			$$.port = NULL;
34453b3a8eb9SGleb Smirnoff 		}
34463b3a8eb9SGleb Smirnoff 		| TO ipportspec		{
34473b3a8eb9SGleb Smirnoff 			if (disallow_urpf_failed($2.host, "\"urpf-failed\" is "
34483b3a8eb9SGleb Smirnoff 			    "not permitted in a destination address"))
34493b3a8eb9SGleb Smirnoff 				YYERROR;
34503b3a8eb9SGleb Smirnoff 			$$ = $2;
34513b3a8eb9SGleb Smirnoff 		}
34523b3a8eb9SGleb Smirnoff 		;
34533b3a8eb9SGleb Smirnoff 
34543b3a8eb9SGleb Smirnoff ipportspec	: ipspec			{
34553b3a8eb9SGleb Smirnoff 			$$.host = $1;
34563b3a8eb9SGleb Smirnoff 			$$.port = NULL;
34573b3a8eb9SGleb Smirnoff 		}
34583b3a8eb9SGleb Smirnoff 		| ipspec PORT portspec		{
34593b3a8eb9SGleb Smirnoff 			$$.host = $1;
34603b3a8eb9SGleb Smirnoff 			$$.port = $3;
34613b3a8eb9SGleb Smirnoff 		}
34623b3a8eb9SGleb Smirnoff 		| PORT portspec			{
34633b3a8eb9SGleb Smirnoff 			$$.host = NULL;
34643b3a8eb9SGleb Smirnoff 			$$.port = $2;
34653b3a8eb9SGleb Smirnoff 		}
34663b3a8eb9SGleb Smirnoff 		;
34673b3a8eb9SGleb Smirnoff 
34683b3a8eb9SGleb Smirnoff optnl		: '\n' optnl
34693b3a8eb9SGleb Smirnoff 		|
34703b3a8eb9SGleb Smirnoff 		;
34713b3a8eb9SGleb Smirnoff 
34723b3a8eb9SGleb Smirnoff ipspec		: ANY				{ $$ = NULL; }
34733b3a8eb9SGleb Smirnoff 		| xhost				{ $$ = $1; }
34743b3a8eb9SGleb Smirnoff 		| '{' optnl host_list '}'	{ $$ = $3; }
34753b3a8eb9SGleb Smirnoff 		;
34763b3a8eb9SGleb Smirnoff 
34773b3a8eb9SGleb Smirnoff toipspec	: TO ipspec			{ $$ = $2; }
34783b3a8eb9SGleb Smirnoff 		| /* empty */			{ $$ = NULL; }
34793b3a8eb9SGleb Smirnoff 		;
34803b3a8eb9SGleb Smirnoff 
34813b3a8eb9SGleb Smirnoff host_list	: ipspec optnl			{ $$ = $1; }
34823b3a8eb9SGleb Smirnoff 		| host_list comma ipspec optnl	{
34833b3a8eb9SGleb Smirnoff 			if ($3 == NULL)
34843b3a8eb9SGleb Smirnoff 				$$ = $1;
34853b3a8eb9SGleb Smirnoff 			else if ($1 == NULL)
34863b3a8eb9SGleb Smirnoff 				$$ = $3;
34873b3a8eb9SGleb Smirnoff 			else {
34883b3a8eb9SGleb Smirnoff 				$1->tail->next = $3;
34893b3a8eb9SGleb Smirnoff 				$1->tail = $3->tail;
34903b3a8eb9SGleb Smirnoff 				$$ = $1;
34913b3a8eb9SGleb Smirnoff 			}
34923b3a8eb9SGleb Smirnoff 		}
34933b3a8eb9SGleb Smirnoff 		;
34943b3a8eb9SGleb Smirnoff 
34953b3a8eb9SGleb Smirnoff xhost		: not host			{
34963b3a8eb9SGleb Smirnoff 			struct node_host	*n;
34973b3a8eb9SGleb Smirnoff 
34983b3a8eb9SGleb Smirnoff 			for (n = $2; n != NULL; n = n->next)
34993b3a8eb9SGleb Smirnoff 				n->not = $1;
35003b3a8eb9SGleb Smirnoff 			$$ = $2;
35013b3a8eb9SGleb Smirnoff 		}
35023b3a8eb9SGleb Smirnoff 		| not NOROUTE			{
35033b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_host));
35043b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
35053b3a8eb9SGleb Smirnoff 				err(1, "xhost: calloc");
35063b3a8eb9SGleb Smirnoff 			$$->addr.type = PF_ADDR_NOROUTE;
35073b3a8eb9SGleb Smirnoff 			$$->next = NULL;
35083b3a8eb9SGleb Smirnoff 			$$->not = $1;
35093b3a8eb9SGleb Smirnoff 			$$->tail = $$;
35103b3a8eb9SGleb Smirnoff 		}
35113b3a8eb9SGleb Smirnoff 		| not URPFFAILED		{
35123b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_host));
35133b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
35143b3a8eb9SGleb Smirnoff 				err(1, "xhost: calloc");
35153b3a8eb9SGleb Smirnoff 			$$->addr.type = PF_ADDR_URPFFAILED;
35163b3a8eb9SGleb Smirnoff 			$$->next = NULL;
35173b3a8eb9SGleb Smirnoff 			$$->not = $1;
35183b3a8eb9SGleb Smirnoff 			$$->tail = $$;
35193b3a8eb9SGleb Smirnoff 		}
35203b3a8eb9SGleb Smirnoff 		;
35213b3a8eb9SGleb Smirnoff 
35223b3a8eb9SGleb Smirnoff host		: STRING			{
35233b3a8eb9SGleb Smirnoff 			if (($$ = host($1)) == NULL)	{
35243b3a8eb9SGleb Smirnoff 				/* error. "any" is handled elsewhere */
35253b3a8eb9SGleb Smirnoff 				free($1);
35263b3a8eb9SGleb Smirnoff 				yyerror("could not parse host specification");
35273b3a8eb9SGleb Smirnoff 				YYERROR;
35283b3a8eb9SGleb Smirnoff 			}
35293b3a8eb9SGleb Smirnoff 			free($1);
35303b3a8eb9SGleb Smirnoff 
35313b3a8eb9SGleb Smirnoff 		}
35323b3a8eb9SGleb Smirnoff 		| STRING '-' STRING		{
35333b3a8eb9SGleb Smirnoff 			struct node_host *b, *e;
35343b3a8eb9SGleb Smirnoff 
35353b3a8eb9SGleb Smirnoff 			if ((b = host($1)) == NULL || (e = host($3)) == NULL) {
35363b3a8eb9SGleb Smirnoff 				free($1);
35373b3a8eb9SGleb Smirnoff 				free($3);
35383b3a8eb9SGleb Smirnoff 				yyerror("could not parse host specification");
35393b3a8eb9SGleb Smirnoff 				YYERROR;
35403b3a8eb9SGleb Smirnoff 			}
35413b3a8eb9SGleb Smirnoff 			if (b->af != e->af ||
35423b3a8eb9SGleb Smirnoff 			    b->addr.type != PF_ADDR_ADDRMASK ||
35433b3a8eb9SGleb Smirnoff 			    e->addr.type != PF_ADDR_ADDRMASK ||
35443b3a8eb9SGleb Smirnoff 			    unmask(&b->addr.v.a.mask, b->af) !=
35453b3a8eb9SGleb Smirnoff 			    (b->af == AF_INET ? 32 : 128) ||
35463b3a8eb9SGleb Smirnoff 			    unmask(&e->addr.v.a.mask, e->af) !=
35473b3a8eb9SGleb Smirnoff 			    (e->af == AF_INET ? 32 : 128) ||
35483b3a8eb9SGleb Smirnoff 			    b->next != NULL || b->not ||
35493b3a8eb9SGleb Smirnoff 			    e->next != NULL || e->not) {
35503b3a8eb9SGleb Smirnoff 				free(b);
35513b3a8eb9SGleb Smirnoff 				free(e);
35523b3a8eb9SGleb Smirnoff 				free($1);
35533b3a8eb9SGleb Smirnoff 				free($3);
35543b3a8eb9SGleb Smirnoff 				yyerror("invalid address range");
35553b3a8eb9SGleb Smirnoff 				YYERROR;
35563b3a8eb9SGleb Smirnoff 			}
35573b3a8eb9SGleb Smirnoff 			memcpy(&b->addr.v.a.mask, &e->addr.v.a.addr,
35583b3a8eb9SGleb Smirnoff 			    sizeof(b->addr.v.a.mask));
35593b3a8eb9SGleb Smirnoff 			b->addr.type = PF_ADDR_RANGE;
35603b3a8eb9SGleb Smirnoff 			$$ = b;
35613b3a8eb9SGleb Smirnoff 			free(e);
35623b3a8eb9SGleb Smirnoff 			free($1);
35633b3a8eb9SGleb Smirnoff 			free($3);
35643b3a8eb9SGleb Smirnoff 		}
35653b3a8eb9SGleb Smirnoff 		| STRING '/' NUMBER		{
35663b3a8eb9SGleb Smirnoff 			char	*buf;
35673b3a8eb9SGleb Smirnoff 
35683b3a8eb9SGleb Smirnoff 			if (asprintf(&buf, "%s/%lld", $1, (long long)$3) == -1)
35693b3a8eb9SGleb Smirnoff 				err(1, "host: asprintf");
35703b3a8eb9SGleb Smirnoff 			free($1);
35713b3a8eb9SGleb Smirnoff 			if (($$ = host(buf)) == NULL)	{
35723b3a8eb9SGleb Smirnoff 				/* error. "any" is handled elsewhere */
35733b3a8eb9SGleb Smirnoff 				free(buf);
35743b3a8eb9SGleb Smirnoff 				yyerror("could not parse host specification");
35753b3a8eb9SGleb Smirnoff 				YYERROR;
35763b3a8eb9SGleb Smirnoff 			}
35773b3a8eb9SGleb Smirnoff 			free(buf);
35783b3a8eb9SGleb Smirnoff 		}
35793b3a8eb9SGleb Smirnoff 		| NUMBER '/' NUMBER		{
35803b3a8eb9SGleb Smirnoff 			char	*buf;
35813b3a8eb9SGleb Smirnoff 
35823b3a8eb9SGleb Smirnoff 			/* ie. for 10/8 parsing */
35833b3a8eb9SGleb Smirnoff #ifdef __FreeBSD__
35843b3a8eb9SGleb Smirnoff 			if (asprintf(&buf, "%lld/%lld", (long long)$1, (long long)$3) == -1)
35853b3a8eb9SGleb Smirnoff #else
35863b3a8eb9SGleb Smirnoff 			if (asprintf(&buf, "%lld/%lld", $1, $3) == -1)
35873b3a8eb9SGleb Smirnoff #endif
35883b3a8eb9SGleb Smirnoff 				err(1, "host: asprintf");
35893b3a8eb9SGleb Smirnoff 			if (($$ = host(buf)) == NULL)	{
35903b3a8eb9SGleb Smirnoff 				/* error. "any" is handled elsewhere */
35913b3a8eb9SGleb Smirnoff 				free(buf);
35923b3a8eb9SGleb Smirnoff 				yyerror("could not parse host specification");
35933b3a8eb9SGleb Smirnoff 				YYERROR;
35943b3a8eb9SGleb Smirnoff 			}
35953b3a8eb9SGleb Smirnoff 			free(buf);
35963b3a8eb9SGleb Smirnoff 		}
35973b3a8eb9SGleb Smirnoff 		| dynaddr
35983b3a8eb9SGleb Smirnoff 		| dynaddr '/' NUMBER		{
35993b3a8eb9SGleb Smirnoff 			struct node_host	*n;
36003b3a8eb9SGleb Smirnoff 
36013b3a8eb9SGleb Smirnoff 			if ($3 < 0 || $3 > 128) {
36023b3a8eb9SGleb Smirnoff 				yyerror("bit number too big");
36033b3a8eb9SGleb Smirnoff 				YYERROR;
36043b3a8eb9SGleb Smirnoff 			}
36053b3a8eb9SGleb Smirnoff 			$$ = $1;
36063b3a8eb9SGleb Smirnoff 			for (n = $1; n != NULL; n = n->next)
36073b3a8eb9SGleb Smirnoff 				set_ipmask(n, $3);
36083b3a8eb9SGleb Smirnoff 		}
36093b3a8eb9SGleb Smirnoff 		| '<' STRING '>'	{
36103b3a8eb9SGleb Smirnoff 			if (strlen($2) >= PF_TABLE_NAME_SIZE) {
36113b3a8eb9SGleb Smirnoff 				yyerror("table name '%s' too long", $2);
36123b3a8eb9SGleb Smirnoff 				free($2);
36133b3a8eb9SGleb Smirnoff 				YYERROR;
36143b3a8eb9SGleb Smirnoff 			}
36153b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_host));
36163b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
36173b3a8eb9SGleb Smirnoff 				err(1, "host: calloc");
36183b3a8eb9SGleb Smirnoff 			$$->addr.type = PF_ADDR_TABLE;
36193b3a8eb9SGleb Smirnoff 			if (strlcpy($$->addr.v.tblname, $2,
36203b3a8eb9SGleb Smirnoff 			    sizeof($$->addr.v.tblname)) >=
36213b3a8eb9SGleb Smirnoff 			    sizeof($$->addr.v.tblname))
36223b3a8eb9SGleb Smirnoff 				errx(1, "host: strlcpy");
36233b3a8eb9SGleb Smirnoff 			free($2);
36243b3a8eb9SGleb Smirnoff 			$$->next = NULL;
36253b3a8eb9SGleb Smirnoff 			$$->tail = $$;
36263b3a8eb9SGleb Smirnoff 		}
36273b3a8eb9SGleb Smirnoff 		;
36283b3a8eb9SGleb Smirnoff 
36293b3a8eb9SGleb Smirnoff number		: NUMBER
36303b3a8eb9SGleb Smirnoff 		| STRING		{
36313b3a8eb9SGleb Smirnoff 			u_long	ulval;
36323b3a8eb9SGleb Smirnoff 
36333b3a8eb9SGleb Smirnoff 			if (atoul($1, &ulval) == -1) {
36343b3a8eb9SGleb Smirnoff 				yyerror("%s is not a number", $1);
36353b3a8eb9SGleb Smirnoff 				free($1);
36363b3a8eb9SGleb Smirnoff 				YYERROR;
36373b3a8eb9SGleb Smirnoff 			} else
36383b3a8eb9SGleb Smirnoff 				$$ = ulval;
36393b3a8eb9SGleb Smirnoff 			free($1);
36403b3a8eb9SGleb Smirnoff 		}
36413b3a8eb9SGleb Smirnoff 		;
36423b3a8eb9SGleb Smirnoff 
36433b3a8eb9SGleb Smirnoff dynaddr		: '(' STRING ')'		{
36443b3a8eb9SGleb Smirnoff 			int	 flags = 0;
36453b3a8eb9SGleb Smirnoff 			char	*p, *op;
36463b3a8eb9SGleb Smirnoff 
36473b3a8eb9SGleb Smirnoff 			op = $2;
36483b3a8eb9SGleb Smirnoff 			if (!isalpha(op[0])) {
36493b3a8eb9SGleb Smirnoff 				yyerror("invalid interface name '%s'", op);
36503b3a8eb9SGleb Smirnoff 				free(op);
36513b3a8eb9SGleb Smirnoff 				YYERROR;
36523b3a8eb9SGleb Smirnoff 			}
36533b3a8eb9SGleb Smirnoff 			while ((p = strrchr($2, ':')) != NULL) {
36543b3a8eb9SGleb Smirnoff 				if (!strcmp(p+1, "network"))
36553b3a8eb9SGleb Smirnoff 					flags |= PFI_AFLAG_NETWORK;
36563b3a8eb9SGleb Smirnoff 				else if (!strcmp(p+1, "broadcast"))
36573b3a8eb9SGleb Smirnoff 					flags |= PFI_AFLAG_BROADCAST;
36583b3a8eb9SGleb Smirnoff 				else if (!strcmp(p+1, "peer"))
36593b3a8eb9SGleb Smirnoff 					flags |= PFI_AFLAG_PEER;
36603b3a8eb9SGleb Smirnoff 				else if (!strcmp(p+1, "0"))
36613b3a8eb9SGleb Smirnoff 					flags |= PFI_AFLAG_NOALIAS;
36623b3a8eb9SGleb Smirnoff 				else {
36633b3a8eb9SGleb Smirnoff 					yyerror("interface %s has bad modifier",
36643b3a8eb9SGleb Smirnoff 					    $2);
36653b3a8eb9SGleb Smirnoff 					free(op);
36663b3a8eb9SGleb Smirnoff 					YYERROR;
36673b3a8eb9SGleb Smirnoff 				}
36683b3a8eb9SGleb Smirnoff 				*p = '\0';
36693b3a8eb9SGleb Smirnoff 			}
36703b3a8eb9SGleb Smirnoff 			if (flags & (flags - 1) & PFI_AFLAG_MODEMASK) {
36713b3a8eb9SGleb Smirnoff 				free(op);
36723b3a8eb9SGleb Smirnoff 				yyerror("illegal combination of "
36733b3a8eb9SGleb Smirnoff 				    "interface modifiers");
36743b3a8eb9SGleb Smirnoff 				YYERROR;
36753b3a8eb9SGleb Smirnoff 			}
36763b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_host));
36773b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
36783b3a8eb9SGleb Smirnoff 				err(1, "address: calloc");
36793b3a8eb9SGleb Smirnoff 			$$->af = 0;
36803b3a8eb9SGleb Smirnoff 			set_ipmask($$, 128);
36813b3a8eb9SGleb Smirnoff 			$$->addr.type = PF_ADDR_DYNIFTL;
36823b3a8eb9SGleb Smirnoff 			$$->addr.iflags = flags;
36833b3a8eb9SGleb Smirnoff 			if (strlcpy($$->addr.v.ifname, $2,
36843b3a8eb9SGleb Smirnoff 			    sizeof($$->addr.v.ifname)) >=
36853b3a8eb9SGleb Smirnoff 			    sizeof($$->addr.v.ifname)) {
36863b3a8eb9SGleb Smirnoff 				free(op);
36873b3a8eb9SGleb Smirnoff 				free($$);
36883b3a8eb9SGleb Smirnoff 				yyerror("interface name too long");
36893b3a8eb9SGleb Smirnoff 				YYERROR;
36903b3a8eb9SGleb Smirnoff 			}
36913b3a8eb9SGleb Smirnoff 			free(op);
36923b3a8eb9SGleb Smirnoff 			$$->next = NULL;
36933b3a8eb9SGleb Smirnoff 			$$->tail = $$;
36943b3a8eb9SGleb Smirnoff 		}
36953b3a8eb9SGleb Smirnoff 		;
36963b3a8eb9SGleb Smirnoff 
36973b3a8eb9SGleb Smirnoff portspec	: port_item			{ $$ = $1; }
36983b3a8eb9SGleb Smirnoff 		| '{' optnl port_list '}'	{ $$ = $3; }
36993b3a8eb9SGleb Smirnoff 		;
37003b3a8eb9SGleb Smirnoff 
37013b3a8eb9SGleb Smirnoff port_list	: port_item optnl		{ $$ = $1; }
37023b3a8eb9SGleb Smirnoff 		| port_list comma port_item optnl	{
37033b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
37043b3a8eb9SGleb Smirnoff 			$1->tail = $3;
37053b3a8eb9SGleb Smirnoff 			$$ = $1;
37063b3a8eb9SGleb Smirnoff 		}
37073b3a8eb9SGleb Smirnoff 		;
37083b3a8eb9SGleb Smirnoff 
37093b3a8eb9SGleb Smirnoff port_item	: portrange			{
37103b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_port));
37113b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
37123b3a8eb9SGleb Smirnoff 				err(1, "port_item: calloc");
37133b3a8eb9SGleb Smirnoff 			$$->port[0] = $1.a;
37143b3a8eb9SGleb Smirnoff 			$$->port[1] = $1.b;
37153b3a8eb9SGleb Smirnoff 			if ($1.t)
37163b3a8eb9SGleb Smirnoff 				$$->op = PF_OP_RRG;
37173b3a8eb9SGleb Smirnoff 			else
37183b3a8eb9SGleb Smirnoff 				$$->op = PF_OP_EQ;
37193b3a8eb9SGleb Smirnoff 			$$->next = NULL;
37203b3a8eb9SGleb Smirnoff 			$$->tail = $$;
37213b3a8eb9SGleb Smirnoff 		}
37223b3a8eb9SGleb Smirnoff 		| unaryop portrange	{
37233b3a8eb9SGleb Smirnoff 			if ($2.t) {
37243b3a8eb9SGleb Smirnoff 				yyerror("':' cannot be used with an other "
37253b3a8eb9SGleb Smirnoff 				    "port operator");
37263b3a8eb9SGleb Smirnoff 				YYERROR;
37273b3a8eb9SGleb Smirnoff 			}
37283b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_port));
37293b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
37303b3a8eb9SGleb Smirnoff 				err(1, "port_item: calloc");
37313b3a8eb9SGleb Smirnoff 			$$->port[0] = $2.a;
37323b3a8eb9SGleb Smirnoff 			$$->port[1] = $2.b;
37333b3a8eb9SGleb Smirnoff 			$$->op = $1;
37343b3a8eb9SGleb Smirnoff 			$$->next = NULL;
37353b3a8eb9SGleb Smirnoff 			$$->tail = $$;
37363b3a8eb9SGleb Smirnoff 		}
37373b3a8eb9SGleb Smirnoff 		| portrange PORTBINARY portrange	{
37383b3a8eb9SGleb Smirnoff 			if ($1.t || $3.t) {
37393b3a8eb9SGleb Smirnoff 				yyerror("':' cannot be used with an other "
37403b3a8eb9SGleb Smirnoff 				    "port operator");
37413b3a8eb9SGleb Smirnoff 				YYERROR;
37423b3a8eb9SGleb Smirnoff 			}
37433b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_port));
37443b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
37453b3a8eb9SGleb Smirnoff 				err(1, "port_item: calloc");
37463b3a8eb9SGleb Smirnoff 			$$->port[0] = $1.a;
37473b3a8eb9SGleb Smirnoff 			$$->port[1] = $3.a;
37483b3a8eb9SGleb Smirnoff 			$$->op = $2;
37493b3a8eb9SGleb Smirnoff 			$$->next = NULL;
37503b3a8eb9SGleb Smirnoff 			$$->tail = $$;
37513b3a8eb9SGleb Smirnoff 		}
37523b3a8eb9SGleb Smirnoff 		;
37533b3a8eb9SGleb Smirnoff 
37543b3a8eb9SGleb Smirnoff portplain	: numberstring			{
37553b3a8eb9SGleb Smirnoff 			if (parseport($1, &$$, 0) == -1) {
37563b3a8eb9SGleb Smirnoff 				free($1);
37573b3a8eb9SGleb Smirnoff 				YYERROR;
37583b3a8eb9SGleb Smirnoff 			}
37593b3a8eb9SGleb Smirnoff 			free($1);
37603b3a8eb9SGleb Smirnoff 		}
37613b3a8eb9SGleb Smirnoff 		;
37623b3a8eb9SGleb Smirnoff 
37633b3a8eb9SGleb Smirnoff portrange	: numberstring			{
37643b3a8eb9SGleb Smirnoff 			if (parseport($1, &$$, PPORT_RANGE) == -1) {
37653b3a8eb9SGleb Smirnoff 				free($1);
37663b3a8eb9SGleb Smirnoff 				YYERROR;
37673b3a8eb9SGleb Smirnoff 			}
37683b3a8eb9SGleb Smirnoff 			free($1);
37693b3a8eb9SGleb Smirnoff 		}
37703b3a8eb9SGleb Smirnoff 		;
37713b3a8eb9SGleb Smirnoff 
37723b3a8eb9SGleb Smirnoff uids		: uid_item			{ $$ = $1; }
37733b3a8eb9SGleb Smirnoff 		| '{' optnl uid_list '}'	{ $$ = $3; }
37743b3a8eb9SGleb Smirnoff 		;
37753b3a8eb9SGleb Smirnoff 
37763b3a8eb9SGleb Smirnoff uid_list	: uid_item optnl		{ $$ = $1; }
37773b3a8eb9SGleb Smirnoff 		| uid_list comma uid_item optnl	{
37783b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
37793b3a8eb9SGleb Smirnoff 			$1->tail = $3;
37803b3a8eb9SGleb Smirnoff 			$$ = $1;
37813b3a8eb9SGleb Smirnoff 		}
37823b3a8eb9SGleb Smirnoff 		;
37833b3a8eb9SGleb Smirnoff 
37843b3a8eb9SGleb Smirnoff uid_item	: uid				{
37853b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_uid));
37863b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
37873b3a8eb9SGleb Smirnoff 				err(1, "uid_item: calloc");
37883b3a8eb9SGleb Smirnoff 			$$->uid[0] = $1;
37893b3a8eb9SGleb Smirnoff 			$$->uid[1] = $1;
37903b3a8eb9SGleb Smirnoff 			$$->op = PF_OP_EQ;
37913b3a8eb9SGleb Smirnoff 			$$->next = NULL;
37923b3a8eb9SGleb Smirnoff 			$$->tail = $$;
37933b3a8eb9SGleb Smirnoff 		}
37943b3a8eb9SGleb Smirnoff 		| unaryop uid			{
37953b3a8eb9SGleb Smirnoff 			if ($2 == UID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
37963b3a8eb9SGleb Smirnoff 				yyerror("user unknown requires operator = or "
37973b3a8eb9SGleb Smirnoff 				    "!=");
37983b3a8eb9SGleb Smirnoff 				YYERROR;
37993b3a8eb9SGleb Smirnoff 			}
38003b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_uid));
38013b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
38023b3a8eb9SGleb Smirnoff 				err(1, "uid_item: calloc");
38033b3a8eb9SGleb Smirnoff 			$$->uid[0] = $2;
38043b3a8eb9SGleb Smirnoff 			$$->uid[1] = $2;
38053b3a8eb9SGleb Smirnoff 			$$->op = $1;
38063b3a8eb9SGleb Smirnoff 			$$->next = NULL;
38073b3a8eb9SGleb Smirnoff 			$$->tail = $$;
38083b3a8eb9SGleb Smirnoff 		}
38093b3a8eb9SGleb Smirnoff 		| uid PORTBINARY uid		{
38103b3a8eb9SGleb Smirnoff 			if ($1 == UID_MAX || $3 == UID_MAX) {
38113b3a8eb9SGleb Smirnoff 				yyerror("user unknown requires operator = or "
38123b3a8eb9SGleb Smirnoff 				    "!=");
38133b3a8eb9SGleb Smirnoff 				YYERROR;
38143b3a8eb9SGleb Smirnoff 			}
38153b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_uid));
38163b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
38173b3a8eb9SGleb Smirnoff 				err(1, "uid_item: calloc");
38183b3a8eb9SGleb Smirnoff 			$$->uid[0] = $1;
38193b3a8eb9SGleb Smirnoff 			$$->uid[1] = $3;
38203b3a8eb9SGleb Smirnoff 			$$->op = $2;
38213b3a8eb9SGleb Smirnoff 			$$->next = NULL;
38223b3a8eb9SGleb Smirnoff 			$$->tail = $$;
38233b3a8eb9SGleb Smirnoff 		}
38243b3a8eb9SGleb Smirnoff 		;
38253b3a8eb9SGleb Smirnoff 
38263b3a8eb9SGleb Smirnoff uid		: STRING			{
38273b3a8eb9SGleb Smirnoff 			if (!strcmp($1, "unknown"))
38283b3a8eb9SGleb Smirnoff 				$$ = UID_MAX;
38293b3a8eb9SGleb Smirnoff 			else {
38303b3a8eb9SGleb Smirnoff 				struct passwd	*pw;
38313b3a8eb9SGleb Smirnoff 
38323b3a8eb9SGleb Smirnoff 				if ((pw = getpwnam($1)) == NULL) {
38333b3a8eb9SGleb Smirnoff 					yyerror("unknown user %s", $1);
38343b3a8eb9SGleb Smirnoff 					free($1);
38353b3a8eb9SGleb Smirnoff 					YYERROR;
38363b3a8eb9SGleb Smirnoff 				}
38373b3a8eb9SGleb Smirnoff 				$$ = pw->pw_uid;
38383b3a8eb9SGleb Smirnoff 			}
38393b3a8eb9SGleb Smirnoff 			free($1);
38403b3a8eb9SGleb Smirnoff 		}
38413b3a8eb9SGleb Smirnoff 		| NUMBER			{
38423b3a8eb9SGleb Smirnoff 			if ($1 < 0 || $1 >= UID_MAX) {
38433b3a8eb9SGleb Smirnoff 				yyerror("illegal uid value %lu", $1);
38443b3a8eb9SGleb Smirnoff 				YYERROR;
38453b3a8eb9SGleb Smirnoff 			}
38463b3a8eb9SGleb Smirnoff 			$$ = $1;
38473b3a8eb9SGleb Smirnoff 		}
38483b3a8eb9SGleb Smirnoff 		;
38493b3a8eb9SGleb Smirnoff 
38503b3a8eb9SGleb Smirnoff gids		: gid_item			{ $$ = $1; }
38513b3a8eb9SGleb Smirnoff 		| '{' optnl gid_list '}'	{ $$ = $3; }
38523b3a8eb9SGleb Smirnoff 		;
38533b3a8eb9SGleb Smirnoff 
38543b3a8eb9SGleb Smirnoff gid_list	: gid_item optnl		{ $$ = $1; }
38553b3a8eb9SGleb Smirnoff 		| gid_list comma gid_item optnl	{
38563b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
38573b3a8eb9SGleb Smirnoff 			$1->tail = $3;
38583b3a8eb9SGleb Smirnoff 			$$ = $1;
38593b3a8eb9SGleb Smirnoff 		}
38603b3a8eb9SGleb Smirnoff 		;
38613b3a8eb9SGleb Smirnoff 
38623b3a8eb9SGleb Smirnoff gid_item	: gid				{
38633b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_gid));
38643b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
38653b3a8eb9SGleb Smirnoff 				err(1, "gid_item: calloc");
38663b3a8eb9SGleb Smirnoff 			$$->gid[0] = $1;
38673b3a8eb9SGleb Smirnoff 			$$->gid[1] = $1;
38683b3a8eb9SGleb Smirnoff 			$$->op = PF_OP_EQ;
38693b3a8eb9SGleb Smirnoff 			$$->next = NULL;
38703b3a8eb9SGleb Smirnoff 			$$->tail = $$;
38713b3a8eb9SGleb Smirnoff 		}
38723b3a8eb9SGleb Smirnoff 		| unaryop gid			{
38733b3a8eb9SGleb Smirnoff 			if ($2 == GID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
38743b3a8eb9SGleb Smirnoff 				yyerror("group unknown requires operator = or "
38753b3a8eb9SGleb Smirnoff 				    "!=");
38763b3a8eb9SGleb Smirnoff 				YYERROR;
38773b3a8eb9SGleb Smirnoff 			}
38783b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_gid));
38793b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
38803b3a8eb9SGleb Smirnoff 				err(1, "gid_item: calloc");
38813b3a8eb9SGleb Smirnoff 			$$->gid[0] = $2;
38823b3a8eb9SGleb Smirnoff 			$$->gid[1] = $2;
38833b3a8eb9SGleb Smirnoff 			$$->op = $1;
38843b3a8eb9SGleb Smirnoff 			$$->next = NULL;
38853b3a8eb9SGleb Smirnoff 			$$->tail = $$;
38863b3a8eb9SGleb Smirnoff 		}
38873b3a8eb9SGleb Smirnoff 		| gid PORTBINARY gid		{
38883b3a8eb9SGleb Smirnoff 			if ($1 == GID_MAX || $3 == GID_MAX) {
38893b3a8eb9SGleb Smirnoff 				yyerror("group unknown requires operator = or "
38903b3a8eb9SGleb Smirnoff 				    "!=");
38913b3a8eb9SGleb Smirnoff 				YYERROR;
38923b3a8eb9SGleb Smirnoff 			}
38933b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_gid));
38943b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
38953b3a8eb9SGleb Smirnoff 				err(1, "gid_item: calloc");
38963b3a8eb9SGleb Smirnoff 			$$->gid[0] = $1;
38973b3a8eb9SGleb Smirnoff 			$$->gid[1] = $3;
38983b3a8eb9SGleb Smirnoff 			$$->op = $2;
38993b3a8eb9SGleb Smirnoff 			$$->next = NULL;
39003b3a8eb9SGleb Smirnoff 			$$->tail = $$;
39013b3a8eb9SGleb Smirnoff 		}
39023b3a8eb9SGleb Smirnoff 		;
39033b3a8eb9SGleb Smirnoff 
39043b3a8eb9SGleb Smirnoff gid		: STRING			{
39053b3a8eb9SGleb Smirnoff 			if (!strcmp($1, "unknown"))
39063b3a8eb9SGleb Smirnoff 				$$ = GID_MAX;
39073b3a8eb9SGleb Smirnoff 			else {
39083b3a8eb9SGleb Smirnoff 				struct group	*grp;
39093b3a8eb9SGleb Smirnoff 
39103b3a8eb9SGleb Smirnoff 				if ((grp = getgrnam($1)) == NULL) {
39113b3a8eb9SGleb Smirnoff 					yyerror("unknown group %s", $1);
39123b3a8eb9SGleb Smirnoff 					free($1);
39133b3a8eb9SGleb Smirnoff 					YYERROR;
39143b3a8eb9SGleb Smirnoff 				}
39153b3a8eb9SGleb Smirnoff 				$$ = grp->gr_gid;
39163b3a8eb9SGleb Smirnoff 			}
39173b3a8eb9SGleb Smirnoff 			free($1);
39183b3a8eb9SGleb Smirnoff 		}
39193b3a8eb9SGleb Smirnoff 		| NUMBER			{
39203b3a8eb9SGleb Smirnoff 			if ($1 < 0 || $1 >= GID_MAX) {
39213b3a8eb9SGleb Smirnoff 				yyerror("illegal gid value %lu", $1);
39223b3a8eb9SGleb Smirnoff 				YYERROR;
39233b3a8eb9SGleb Smirnoff 			}
39243b3a8eb9SGleb Smirnoff 			$$ = $1;
39253b3a8eb9SGleb Smirnoff 		}
39263b3a8eb9SGleb Smirnoff 		;
39273b3a8eb9SGleb Smirnoff 
39283b3a8eb9SGleb Smirnoff flag		: STRING			{
39293b3a8eb9SGleb Smirnoff 			int	f;
39303b3a8eb9SGleb Smirnoff 
39313b3a8eb9SGleb Smirnoff 			if ((f = parse_flags($1)) < 0) {
39323b3a8eb9SGleb Smirnoff 				yyerror("bad flags %s", $1);
39333b3a8eb9SGleb Smirnoff 				free($1);
39343b3a8eb9SGleb Smirnoff 				YYERROR;
39353b3a8eb9SGleb Smirnoff 			}
39363b3a8eb9SGleb Smirnoff 			free($1);
39373b3a8eb9SGleb Smirnoff 			$$.b1 = f;
39383b3a8eb9SGleb Smirnoff 		}
39393b3a8eb9SGleb Smirnoff 		;
39403b3a8eb9SGleb Smirnoff 
39413b3a8eb9SGleb Smirnoff flags		: FLAGS flag '/' flag	{ $$.b1 = $2.b1; $$.b2 = $4.b1; }
39423b3a8eb9SGleb Smirnoff 		| FLAGS '/' flag	{ $$.b1 = 0; $$.b2 = $3.b1; }
39433b3a8eb9SGleb Smirnoff 		| FLAGS ANY		{ $$.b1 = 0; $$.b2 = 0; }
39443b3a8eb9SGleb Smirnoff 		;
39453b3a8eb9SGleb Smirnoff 
39463b3a8eb9SGleb Smirnoff icmpspec	: ICMPTYPE icmp_item			{ $$ = $2; }
39473b3a8eb9SGleb Smirnoff 		| ICMPTYPE '{' optnl icmp_list '}'	{ $$ = $4; }
39483b3a8eb9SGleb Smirnoff 		| ICMP6TYPE icmp6_item			{ $$ = $2; }
39493b3a8eb9SGleb Smirnoff 		| ICMP6TYPE '{' optnl icmp6_list '}'	{ $$ = $4; }
39503b3a8eb9SGleb Smirnoff 		;
39513b3a8eb9SGleb Smirnoff 
39523b3a8eb9SGleb Smirnoff icmp_list	: icmp_item optnl		{ $$ = $1; }
39533b3a8eb9SGleb Smirnoff 		| icmp_list comma icmp_item optnl {
39543b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
39553b3a8eb9SGleb Smirnoff 			$1->tail = $3;
39563b3a8eb9SGleb Smirnoff 			$$ = $1;
39573b3a8eb9SGleb Smirnoff 		}
39583b3a8eb9SGleb Smirnoff 		;
39593b3a8eb9SGleb Smirnoff 
39603b3a8eb9SGleb Smirnoff icmp6_list	: icmp6_item optnl		{ $$ = $1; }
39613b3a8eb9SGleb Smirnoff 		| icmp6_list comma icmp6_item optnl {
39623b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
39633b3a8eb9SGleb Smirnoff 			$1->tail = $3;
39643b3a8eb9SGleb Smirnoff 			$$ = $1;
39653b3a8eb9SGleb Smirnoff 		}
39663b3a8eb9SGleb Smirnoff 		;
39673b3a8eb9SGleb Smirnoff 
39683b3a8eb9SGleb Smirnoff icmp_item	: icmptype		{
39693b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_icmp));
39703b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
39713b3a8eb9SGleb Smirnoff 				err(1, "icmp_item: calloc");
39723b3a8eb9SGleb Smirnoff 			$$->type = $1;
39733b3a8eb9SGleb Smirnoff 			$$->code = 0;
39743b3a8eb9SGleb Smirnoff 			$$->proto = IPPROTO_ICMP;
39753b3a8eb9SGleb Smirnoff 			$$->next = NULL;
39763b3a8eb9SGleb Smirnoff 			$$->tail = $$;
39773b3a8eb9SGleb Smirnoff 		}
39783b3a8eb9SGleb Smirnoff 		| icmptype CODE STRING	{
39793b3a8eb9SGleb Smirnoff 			const struct icmpcodeent	*p;
39803b3a8eb9SGleb Smirnoff 
39813b3a8eb9SGleb Smirnoff 			if ((p = geticmpcodebyname($1-1, $3, AF_INET)) == NULL) {
39823b3a8eb9SGleb Smirnoff 				yyerror("unknown icmp-code %s", $3);
39833b3a8eb9SGleb Smirnoff 				free($3);
39843b3a8eb9SGleb Smirnoff 				YYERROR;
39853b3a8eb9SGleb Smirnoff 			}
39863b3a8eb9SGleb Smirnoff 
39873b3a8eb9SGleb Smirnoff 			free($3);
39883b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_icmp));
39893b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
39903b3a8eb9SGleb Smirnoff 				err(1, "icmp_item: calloc");
39913b3a8eb9SGleb Smirnoff 			$$->type = $1;
39923b3a8eb9SGleb Smirnoff 			$$->code = p->code + 1;
39933b3a8eb9SGleb Smirnoff 			$$->proto = IPPROTO_ICMP;
39943b3a8eb9SGleb Smirnoff 			$$->next = NULL;
39953b3a8eb9SGleb Smirnoff 			$$->tail = $$;
39963b3a8eb9SGleb Smirnoff 		}
39973b3a8eb9SGleb Smirnoff 		| icmptype CODE NUMBER	{
39983b3a8eb9SGleb Smirnoff 			if ($3 < 0 || $3 > 255) {
39993b3a8eb9SGleb Smirnoff 				yyerror("illegal icmp-code %lu", $3);
40003b3a8eb9SGleb Smirnoff 				YYERROR;
40013b3a8eb9SGleb Smirnoff 			}
40023b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_icmp));
40033b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
40043b3a8eb9SGleb Smirnoff 				err(1, "icmp_item: calloc");
40053b3a8eb9SGleb Smirnoff 			$$->type = $1;
40063b3a8eb9SGleb Smirnoff 			$$->code = $3 + 1;
40073b3a8eb9SGleb Smirnoff 			$$->proto = IPPROTO_ICMP;
40083b3a8eb9SGleb Smirnoff 			$$->next = NULL;
40093b3a8eb9SGleb Smirnoff 			$$->tail = $$;
40103b3a8eb9SGleb Smirnoff 		}
40113b3a8eb9SGleb Smirnoff 		;
40123b3a8eb9SGleb Smirnoff 
40133b3a8eb9SGleb Smirnoff icmp6_item	: icmp6type		{
40143b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_icmp));
40153b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
40163b3a8eb9SGleb Smirnoff 				err(1, "icmp_item: calloc");
40173b3a8eb9SGleb Smirnoff 			$$->type = $1;
40183b3a8eb9SGleb Smirnoff 			$$->code = 0;
40193b3a8eb9SGleb Smirnoff 			$$->proto = IPPROTO_ICMPV6;
40203b3a8eb9SGleb Smirnoff 			$$->next = NULL;
40213b3a8eb9SGleb Smirnoff 			$$->tail = $$;
40223b3a8eb9SGleb Smirnoff 		}
40233b3a8eb9SGleb Smirnoff 		| icmp6type CODE STRING	{
40243b3a8eb9SGleb Smirnoff 			const struct icmpcodeent	*p;
40253b3a8eb9SGleb Smirnoff 
40263b3a8eb9SGleb Smirnoff 			if ((p = geticmpcodebyname($1-1, $3, AF_INET6)) == NULL) {
40273b3a8eb9SGleb Smirnoff 				yyerror("unknown icmp6-code %s", $3);
40283b3a8eb9SGleb Smirnoff 				free($3);
40293b3a8eb9SGleb Smirnoff 				YYERROR;
40303b3a8eb9SGleb Smirnoff 			}
40313b3a8eb9SGleb Smirnoff 			free($3);
40323b3a8eb9SGleb Smirnoff 
40333b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_icmp));
40343b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
40353b3a8eb9SGleb Smirnoff 				err(1, "icmp_item: calloc");
40363b3a8eb9SGleb Smirnoff 			$$->type = $1;
40373b3a8eb9SGleb Smirnoff 			$$->code = p->code + 1;
40383b3a8eb9SGleb Smirnoff 			$$->proto = IPPROTO_ICMPV6;
40393b3a8eb9SGleb Smirnoff 			$$->next = NULL;
40403b3a8eb9SGleb Smirnoff 			$$->tail = $$;
40413b3a8eb9SGleb Smirnoff 		}
40423b3a8eb9SGleb Smirnoff 		| icmp6type CODE NUMBER	{
40433b3a8eb9SGleb Smirnoff 			if ($3 < 0 || $3 > 255) {
40443b3a8eb9SGleb Smirnoff 				yyerror("illegal icmp-code %lu", $3);
40453b3a8eb9SGleb Smirnoff 				YYERROR;
40463b3a8eb9SGleb Smirnoff 			}
40473b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_icmp));
40483b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
40493b3a8eb9SGleb Smirnoff 				err(1, "icmp_item: calloc");
40503b3a8eb9SGleb Smirnoff 			$$->type = $1;
40513b3a8eb9SGleb Smirnoff 			$$->code = $3 + 1;
40523b3a8eb9SGleb Smirnoff 			$$->proto = IPPROTO_ICMPV6;
40533b3a8eb9SGleb Smirnoff 			$$->next = NULL;
40543b3a8eb9SGleb Smirnoff 			$$->tail = $$;
40553b3a8eb9SGleb Smirnoff 		}
40563b3a8eb9SGleb Smirnoff 		;
40573b3a8eb9SGleb Smirnoff 
40583b3a8eb9SGleb Smirnoff icmptype	: STRING			{
40593b3a8eb9SGleb Smirnoff 			const struct icmptypeent	*p;
40603b3a8eb9SGleb Smirnoff 
40613b3a8eb9SGleb Smirnoff 			if ((p = geticmptypebyname($1, AF_INET)) == NULL) {
40623b3a8eb9SGleb Smirnoff 				yyerror("unknown icmp-type %s", $1);
40633b3a8eb9SGleb Smirnoff 				free($1);
40643b3a8eb9SGleb Smirnoff 				YYERROR;
40653b3a8eb9SGleb Smirnoff 			}
40663b3a8eb9SGleb Smirnoff 			$$ = p->type + 1;
40673b3a8eb9SGleb Smirnoff 			free($1);
40683b3a8eb9SGleb Smirnoff 		}
40693b3a8eb9SGleb Smirnoff 		| NUMBER			{
40703b3a8eb9SGleb Smirnoff 			if ($1 < 0 || $1 > 255) {
40713b3a8eb9SGleb Smirnoff 				yyerror("illegal icmp-type %lu", $1);
40723b3a8eb9SGleb Smirnoff 				YYERROR;
40733b3a8eb9SGleb Smirnoff 			}
40743b3a8eb9SGleb Smirnoff 			$$ = $1 + 1;
40753b3a8eb9SGleb Smirnoff 		}
40763b3a8eb9SGleb Smirnoff 		;
40773b3a8eb9SGleb Smirnoff 
40783b3a8eb9SGleb Smirnoff icmp6type	: STRING			{
40793b3a8eb9SGleb Smirnoff 			const struct icmptypeent	*p;
40803b3a8eb9SGleb Smirnoff 
40813b3a8eb9SGleb Smirnoff 			if ((p = geticmptypebyname($1, AF_INET6)) ==
40823b3a8eb9SGleb Smirnoff 			    NULL) {
40833b3a8eb9SGleb Smirnoff 				yyerror("unknown icmp6-type %s", $1);
40843b3a8eb9SGleb Smirnoff 				free($1);
40853b3a8eb9SGleb Smirnoff 				YYERROR;
40863b3a8eb9SGleb Smirnoff 			}
40873b3a8eb9SGleb Smirnoff 			$$ = p->type + 1;
40883b3a8eb9SGleb Smirnoff 			free($1);
40893b3a8eb9SGleb Smirnoff 		}
40903b3a8eb9SGleb Smirnoff 		| NUMBER			{
40913b3a8eb9SGleb Smirnoff 			if ($1 < 0 || $1 > 255) {
40923b3a8eb9SGleb Smirnoff 				yyerror("illegal icmp6-type %lu", $1);
40933b3a8eb9SGleb Smirnoff 				YYERROR;
40943b3a8eb9SGleb Smirnoff 			}
40953b3a8eb9SGleb Smirnoff 			$$ = $1 + 1;
40963b3a8eb9SGleb Smirnoff 		}
40973b3a8eb9SGleb Smirnoff 		;
40983b3a8eb9SGleb Smirnoff 
40993b3a8eb9SGleb Smirnoff tos	: STRING			{
41001f495578SKristof Provost 			int val;
41011f495578SKristof Provost 			char *end;
41021f495578SKristof Provost 
41031f495578SKristof Provost 			if (map_tos($1, &val))
41041f495578SKristof Provost 				$$ = val;
41051f495578SKristof Provost 			else if ($1[0] == '0' && $1[1] == 'x') {
41061f495578SKristof Provost 				errno = 0;
41071f495578SKristof Provost 				$$ = strtoul($1, &end, 16);
41081f495578SKristof Provost 				if (errno || *end != '\0')
41091f495578SKristof Provost 					$$ = 256;
41101f495578SKristof Provost 			} else
41110cd7a91aSKristof Provost 				$$ = 256;		/* flag bad argument */
41120cd7a91aSKristof Provost 			if ($$ < 0 || $$ > 255) {
41133b3a8eb9SGleb Smirnoff 				yyerror("illegal tos value %s", $1);
41143b3a8eb9SGleb Smirnoff 				free($1);
41153b3a8eb9SGleb Smirnoff 				YYERROR;
41163b3a8eb9SGleb Smirnoff 			}
41173b3a8eb9SGleb Smirnoff 			free($1);
41183b3a8eb9SGleb Smirnoff 		}
41193b3a8eb9SGleb Smirnoff 		| NUMBER			{
41203b3a8eb9SGleb Smirnoff 			$$ = $1;
41210cd7a91aSKristof Provost 			if ($$ < 0 || $$ > 255) {
41223b3a8eb9SGleb Smirnoff 				yyerror("illegal tos value %s", $1);
41233b3a8eb9SGleb Smirnoff 				YYERROR;
41243b3a8eb9SGleb Smirnoff 			}
41253b3a8eb9SGleb Smirnoff 		}
41263b3a8eb9SGleb Smirnoff 		;
41273b3a8eb9SGleb Smirnoff 
41283b3a8eb9SGleb Smirnoff sourcetrack	: SOURCETRACK		{ $$ = PF_SRCTRACK; }
41293b3a8eb9SGleb Smirnoff 		| SOURCETRACK GLOBAL	{ $$ = PF_SRCTRACK_GLOBAL; }
41303b3a8eb9SGleb Smirnoff 		| SOURCETRACK RULE	{ $$ = PF_SRCTRACK_RULE; }
41313b3a8eb9SGleb Smirnoff 		;
41323b3a8eb9SGleb Smirnoff 
41333b3a8eb9SGleb Smirnoff statelock	: IFBOUND {
41343b3a8eb9SGleb Smirnoff 			$$ = PFRULE_IFBOUND;
41353b3a8eb9SGleb Smirnoff 		}
41363b3a8eb9SGleb Smirnoff 		| FLOATING {
41373b3a8eb9SGleb Smirnoff 			$$ = 0;
41383b3a8eb9SGleb Smirnoff 		}
41393b3a8eb9SGleb Smirnoff 		;
41403b3a8eb9SGleb Smirnoff 
41413b3a8eb9SGleb Smirnoff keep		: NO STATE			{
41423b3a8eb9SGleb Smirnoff 			$$.action = 0;
41433b3a8eb9SGleb Smirnoff 			$$.options = NULL;
41443b3a8eb9SGleb Smirnoff 		}
41453b3a8eb9SGleb Smirnoff 		| KEEP STATE state_opt_spec	{
41463b3a8eb9SGleb Smirnoff 			$$.action = PF_STATE_NORMAL;
41473b3a8eb9SGleb Smirnoff 			$$.options = $3;
41483b3a8eb9SGleb Smirnoff 		}
41493b3a8eb9SGleb Smirnoff 		| MODULATE STATE state_opt_spec {
41503b3a8eb9SGleb Smirnoff 			$$.action = PF_STATE_MODULATE;
41513b3a8eb9SGleb Smirnoff 			$$.options = $3;
41523b3a8eb9SGleb Smirnoff 		}
41533b3a8eb9SGleb Smirnoff 		| SYNPROXY STATE state_opt_spec {
41543b3a8eb9SGleb Smirnoff 			$$.action = PF_STATE_SYNPROXY;
41553b3a8eb9SGleb Smirnoff 			$$.options = $3;
41563b3a8eb9SGleb Smirnoff 		}
41573b3a8eb9SGleb Smirnoff 		;
41583b3a8eb9SGleb Smirnoff 
41593b3a8eb9SGleb Smirnoff flush		: /* empty */			{ $$ = 0; }
41603b3a8eb9SGleb Smirnoff 		| FLUSH				{ $$ = PF_FLUSH; }
41613b3a8eb9SGleb Smirnoff 		| FLUSH GLOBAL			{
41623b3a8eb9SGleb Smirnoff 			$$ = PF_FLUSH | PF_FLUSH_GLOBAL;
41633b3a8eb9SGleb Smirnoff 		}
41643b3a8eb9SGleb Smirnoff 		;
41653b3a8eb9SGleb Smirnoff 
41663b3a8eb9SGleb Smirnoff state_opt_spec	: '(' state_opt_list ')'	{ $$ = $2; }
41673b3a8eb9SGleb Smirnoff 		| /* empty */			{ $$ = NULL; }
41683b3a8eb9SGleb Smirnoff 		;
41693b3a8eb9SGleb Smirnoff 
41703b3a8eb9SGleb Smirnoff state_opt_list	: state_opt_item		{ $$ = $1; }
41713b3a8eb9SGleb Smirnoff 		| state_opt_list comma state_opt_item {
41723b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
41733b3a8eb9SGleb Smirnoff 			$1->tail = $3;
41743b3a8eb9SGleb Smirnoff 			$$ = $1;
41753b3a8eb9SGleb Smirnoff 		}
41763b3a8eb9SGleb Smirnoff 		;
41773b3a8eb9SGleb Smirnoff 
41783b3a8eb9SGleb Smirnoff state_opt_item	: MAXIMUM NUMBER		{
41793b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > UINT_MAX) {
41803b3a8eb9SGleb Smirnoff 				yyerror("only positive values permitted");
41813b3a8eb9SGleb Smirnoff 				YYERROR;
41823b3a8eb9SGleb Smirnoff 			}
41833b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
41843b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
41853b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
41863b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_MAX;
41873b3a8eb9SGleb Smirnoff 			$$->data.max_states = $2;
41883b3a8eb9SGleb Smirnoff 			$$->next = NULL;
41893b3a8eb9SGleb Smirnoff 			$$->tail = $$;
41903b3a8eb9SGleb Smirnoff 		}
41913b3a8eb9SGleb Smirnoff 		| NOSYNC				{
41923b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
41933b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
41943b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
41953b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_NOSYNC;
41963b3a8eb9SGleb Smirnoff 			$$->next = NULL;
41973b3a8eb9SGleb Smirnoff 			$$->tail = $$;
41983b3a8eb9SGleb Smirnoff 		}
41993b3a8eb9SGleb Smirnoff 		| MAXSRCSTATES NUMBER			{
42003b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > UINT_MAX) {
42013b3a8eb9SGleb Smirnoff 				yyerror("only positive values permitted");
42023b3a8eb9SGleb Smirnoff 				YYERROR;
42033b3a8eb9SGleb Smirnoff 			}
42043b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
42053b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
42063b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
42073b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_MAX_SRC_STATES;
42083b3a8eb9SGleb Smirnoff 			$$->data.max_src_states = $2;
42093b3a8eb9SGleb Smirnoff 			$$->next = NULL;
42103b3a8eb9SGleb Smirnoff 			$$->tail = $$;
42113b3a8eb9SGleb Smirnoff 		}
42123b3a8eb9SGleb Smirnoff 		| MAXSRCCONN NUMBER			{
42133b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > UINT_MAX) {
42143b3a8eb9SGleb Smirnoff 				yyerror("only positive values permitted");
42153b3a8eb9SGleb Smirnoff 				YYERROR;
42163b3a8eb9SGleb Smirnoff 			}
42173b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
42183b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
42193b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
42203b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_MAX_SRC_CONN;
42213b3a8eb9SGleb Smirnoff 			$$->data.max_src_conn = $2;
42223b3a8eb9SGleb Smirnoff 			$$->next = NULL;
42233b3a8eb9SGleb Smirnoff 			$$->tail = $$;
42243b3a8eb9SGleb Smirnoff 		}
42253b3a8eb9SGleb Smirnoff 		| MAXSRCCONNRATE NUMBER '/' NUMBER	{
42263b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > UINT_MAX ||
42273b3a8eb9SGleb Smirnoff 			    $4 < 0 || $4 > UINT_MAX) {
42283b3a8eb9SGleb Smirnoff 				yyerror("only positive values permitted");
42293b3a8eb9SGleb Smirnoff 				YYERROR;
42303b3a8eb9SGleb Smirnoff 			}
42313b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
42323b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
42333b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
42343b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_MAX_SRC_CONN_RATE;
42353b3a8eb9SGleb Smirnoff 			$$->data.max_src_conn_rate.limit = $2;
42363b3a8eb9SGleb Smirnoff 			$$->data.max_src_conn_rate.seconds = $4;
42373b3a8eb9SGleb Smirnoff 			$$->next = NULL;
42383b3a8eb9SGleb Smirnoff 			$$->tail = $$;
42393b3a8eb9SGleb Smirnoff 		}
42403b3a8eb9SGleb Smirnoff 		| OVERLOAD '<' STRING '>' flush		{
42413b3a8eb9SGleb Smirnoff 			if (strlen($3) >= PF_TABLE_NAME_SIZE) {
42423b3a8eb9SGleb Smirnoff 				yyerror("table name '%s' too long", $3);
42433b3a8eb9SGleb Smirnoff 				free($3);
42443b3a8eb9SGleb Smirnoff 				YYERROR;
42453b3a8eb9SGleb Smirnoff 			}
42463b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
42473b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
42483b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
42493b3a8eb9SGleb Smirnoff 			if (strlcpy($$->data.overload.tblname, $3,
42503b3a8eb9SGleb Smirnoff 			    PF_TABLE_NAME_SIZE) >= PF_TABLE_NAME_SIZE)
42513b3a8eb9SGleb Smirnoff 				errx(1, "state_opt_item: strlcpy");
42523b3a8eb9SGleb Smirnoff 			free($3);
42533b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_OVERLOAD;
42543b3a8eb9SGleb Smirnoff 			$$->data.overload.flush = $5;
42553b3a8eb9SGleb Smirnoff 			$$->next = NULL;
42563b3a8eb9SGleb Smirnoff 			$$->tail = $$;
42573b3a8eb9SGleb Smirnoff 		}
42583b3a8eb9SGleb Smirnoff 		| MAXSRCNODES NUMBER			{
42593b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > UINT_MAX) {
42603b3a8eb9SGleb Smirnoff 				yyerror("only positive values permitted");
42613b3a8eb9SGleb Smirnoff 				YYERROR;
42623b3a8eb9SGleb Smirnoff 			}
42633b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
42643b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
42653b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
42663b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_MAX_SRC_NODES;
42673b3a8eb9SGleb Smirnoff 			$$->data.max_src_nodes = $2;
42683b3a8eb9SGleb Smirnoff 			$$->next = NULL;
42693b3a8eb9SGleb Smirnoff 			$$->tail = $$;
42703b3a8eb9SGleb Smirnoff 		}
42713b3a8eb9SGleb Smirnoff 		| sourcetrack {
42723b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
42733b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
42743b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
42753b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_SRCTRACK;
42763b3a8eb9SGleb Smirnoff 			$$->data.src_track = $1;
42773b3a8eb9SGleb Smirnoff 			$$->next = NULL;
42783b3a8eb9SGleb Smirnoff 			$$->tail = $$;
42793b3a8eb9SGleb Smirnoff 		}
42803b3a8eb9SGleb Smirnoff 		| statelock {
42813b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
42823b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
42833b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
42843b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_STATELOCK;
42853b3a8eb9SGleb Smirnoff 			$$->data.statelock = $1;
42863b3a8eb9SGleb Smirnoff 			$$->next = NULL;
42873b3a8eb9SGleb Smirnoff 			$$->tail = $$;
42883b3a8eb9SGleb Smirnoff 		}
42893b3a8eb9SGleb Smirnoff 		| SLOPPY {
42903b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
42913b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
42923b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
42933b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_SLOPPY;
42943b3a8eb9SGleb Smirnoff 			$$->next = NULL;
42953b3a8eb9SGleb Smirnoff 			$$->tail = $$;
42963b3a8eb9SGleb Smirnoff 		}
42973b3a8eb9SGleb Smirnoff 		| STRING NUMBER			{
42983b3a8eb9SGleb Smirnoff 			int	i;
42993b3a8eb9SGleb Smirnoff 
43003b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > UINT_MAX) {
43013b3a8eb9SGleb Smirnoff 				yyerror("only positive values permitted");
43023b3a8eb9SGleb Smirnoff 				YYERROR;
43033b3a8eb9SGleb Smirnoff 			}
43043b3a8eb9SGleb Smirnoff 			for (i = 0; pf_timeouts[i].name &&
43053b3a8eb9SGleb Smirnoff 			    strcmp(pf_timeouts[i].name, $1); ++i)
43063b3a8eb9SGleb Smirnoff 				;	/* nothing */
43073b3a8eb9SGleb Smirnoff 			if (!pf_timeouts[i].name) {
43083b3a8eb9SGleb Smirnoff 				yyerror("illegal timeout name %s", $1);
43093b3a8eb9SGleb Smirnoff 				free($1);
43103b3a8eb9SGleb Smirnoff 				YYERROR;
43113b3a8eb9SGleb Smirnoff 			}
43123b3a8eb9SGleb Smirnoff 			if (strchr(pf_timeouts[i].name, '.') == NULL) {
43133b3a8eb9SGleb Smirnoff 				yyerror("illegal state timeout %s", $1);
43143b3a8eb9SGleb Smirnoff 				free($1);
43153b3a8eb9SGleb Smirnoff 				YYERROR;
43163b3a8eb9SGleb Smirnoff 			}
43173b3a8eb9SGleb Smirnoff 			free($1);
43183b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
43193b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
43203b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
43213b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_TIMEOUT;
43223b3a8eb9SGleb Smirnoff 			$$->data.timeout.number = pf_timeouts[i].timeout;
43233b3a8eb9SGleb Smirnoff 			$$->data.timeout.seconds = $2;
43243b3a8eb9SGleb Smirnoff 			$$->next = NULL;
43253b3a8eb9SGleb Smirnoff 			$$->tail = $$;
43263b3a8eb9SGleb Smirnoff 		}
43273b3a8eb9SGleb Smirnoff 		;
43283b3a8eb9SGleb Smirnoff 
43293b3a8eb9SGleb Smirnoff label		: LABEL STRING			{
43303b3a8eb9SGleb Smirnoff 			$$ = $2;
43313b3a8eb9SGleb Smirnoff 		}
43323b3a8eb9SGleb Smirnoff 		;
43333b3a8eb9SGleb Smirnoff 
43342b29ceb8SKristof Provost etherqname	: QUEUE STRING				{
43352b29ceb8SKristof Provost 			$$.qname = $2;
43362b29ceb8SKristof Provost 		}
43372b29ceb8SKristof Provost 		| QUEUE '(' STRING ')'			{
43382b29ceb8SKristof Provost 			$$.qname = $3;
43392b29ceb8SKristof Provost 		}
43402b29ceb8SKristof Provost 		;
43412b29ceb8SKristof Provost 
43423b3a8eb9SGleb Smirnoff qname		: QUEUE STRING				{
43433b3a8eb9SGleb Smirnoff 			$$.qname = $2;
43443b3a8eb9SGleb Smirnoff 			$$.pqname = NULL;
43453b3a8eb9SGleb Smirnoff 		}
43463b3a8eb9SGleb Smirnoff 		| QUEUE '(' STRING ')'			{
43473b3a8eb9SGleb Smirnoff 			$$.qname = $3;
43483b3a8eb9SGleb Smirnoff 			$$.pqname = NULL;
43493b3a8eb9SGleb Smirnoff 		}
43503b3a8eb9SGleb Smirnoff 		| QUEUE '(' STRING comma STRING ')'	{
43513b3a8eb9SGleb Smirnoff 			$$.qname = $3;
43523b3a8eb9SGleb Smirnoff 			$$.pqname = $5;
43533b3a8eb9SGleb Smirnoff 		}
43543b3a8eb9SGleb Smirnoff 		;
43553b3a8eb9SGleb Smirnoff 
43563b3a8eb9SGleb Smirnoff no		: /* empty */			{ $$ = 0; }
43573b3a8eb9SGleb Smirnoff 		| NO				{ $$ = 1; }
43583b3a8eb9SGleb Smirnoff 		;
43593b3a8eb9SGleb Smirnoff 
43603b3a8eb9SGleb Smirnoff portstar	: numberstring			{
43613b3a8eb9SGleb Smirnoff 			if (parseport($1, &$$, PPORT_RANGE|PPORT_STAR) == -1) {
43623b3a8eb9SGleb Smirnoff 				free($1);
43633b3a8eb9SGleb Smirnoff 				YYERROR;
43643b3a8eb9SGleb Smirnoff 			}
43653b3a8eb9SGleb Smirnoff 			free($1);
43663b3a8eb9SGleb Smirnoff 		}
43673b3a8eb9SGleb Smirnoff 		;
43683b3a8eb9SGleb Smirnoff 
43693b3a8eb9SGleb Smirnoff redirspec	: host				{ $$ = $1; }
43703b3a8eb9SGleb Smirnoff 		| '{' optnl redir_host_list '}'	{ $$ = $3; }
43713b3a8eb9SGleb Smirnoff 		;
43723b3a8eb9SGleb Smirnoff 
43733b3a8eb9SGleb Smirnoff redir_host_list	: host optnl			{ $$ = $1; }
43743b3a8eb9SGleb Smirnoff 		| redir_host_list comma host optnl {
43753b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
43763b3a8eb9SGleb Smirnoff 			$1->tail = $3->tail;
43773b3a8eb9SGleb Smirnoff 			$$ = $1;
43783b3a8eb9SGleb Smirnoff 		}
43793b3a8eb9SGleb Smirnoff 		;
43803b3a8eb9SGleb Smirnoff 
43813b3a8eb9SGleb Smirnoff redirpool	: /* empty */			{ $$ = NULL; }
43823b3a8eb9SGleb Smirnoff 		| ARROW redirspec		{
43833b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct redirection));
43843b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
43853b3a8eb9SGleb Smirnoff 				err(1, "redirection: calloc");
43863b3a8eb9SGleb Smirnoff 			$$->host = $2;
43873b3a8eb9SGleb Smirnoff 			$$->rport.a = $$->rport.b = $$->rport.t = 0;
43883b3a8eb9SGleb Smirnoff 		}
43893b3a8eb9SGleb Smirnoff 		| ARROW redirspec PORT portstar	{
43903b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct redirection));
43913b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
43923b3a8eb9SGleb Smirnoff 				err(1, "redirection: calloc");
43933b3a8eb9SGleb Smirnoff 			$$->host = $2;
43943b3a8eb9SGleb Smirnoff 			$$->rport = $4;
43953b3a8eb9SGleb Smirnoff 		}
43963b3a8eb9SGleb Smirnoff 		;
43973b3a8eb9SGleb Smirnoff 
43983b3a8eb9SGleb Smirnoff hashkey		: /* empty */
43993b3a8eb9SGleb Smirnoff 		{
44003b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct pf_poolhashkey));
44013b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
44023b3a8eb9SGleb Smirnoff 				err(1, "hashkey: calloc");
44033b3a8eb9SGleb Smirnoff 			$$->key32[0] = arc4random();
44043b3a8eb9SGleb Smirnoff 			$$->key32[1] = arc4random();
44053b3a8eb9SGleb Smirnoff 			$$->key32[2] = arc4random();
44063b3a8eb9SGleb Smirnoff 			$$->key32[3] = arc4random();
44073b3a8eb9SGleb Smirnoff 		}
44083b3a8eb9SGleb Smirnoff 		| string
44093b3a8eb9SGleb Smirnoff 		{
44103b3a8eb9SGleb Smirnoff 			if (!strncmp($1, "0x", 2)) {
44113b3a8eb9SGleb Smirnoff 				if (strlen($1) != 34) {
44123b3a8eb9SGleb Smirnoff 					free($1);
44133b3a8eb9SGleb Smirnoff 					yyerror("hex key must be 128 bits "
44143b3a8eb9SGleb Smirnoff 						"(32 hex digits) long");
44153b3a8eb9SGleb Smirnoff 					YYERROR;
44163b3a8eb9SGleb Smirnoff 				}
44173b3a8eb9SGleb Smirnoff 				$$ = calloc(1, sizeof(struct pf_poolhashkey));
44183b3a8eb9SGleb Smirnoff 				if ($$ == NULL)
44193b3a8eb9SGleb Smirnoff 					err(1, "hashkey: calloc");
44203b3a8eb9SGleb Smirnoff 
44213b3a8eb9SGleb Smirnoff 				if (sscanf($1, "0x%8x%8x%8x%8x",
44223b3a8eb9SGleb Smirnoff 				    &$$->key32[0], &$$->key32[1],
44233b3a8eb9SGleb Smirnoff 				    &$$->key32[2], &$$->key32[3]) != 4) {
44243b3a8eb9SGleb Smirnoff 					free($$);
44253b3a8eb9SGleb Smirnoff 					free($1);
44263b3a8eb9SGleb Smirnoff 					yyerror("invalid hex key");
44273b3a8eb9SGleb Smirnoff 					YYERROR;
44283b3a8eb9SGleb Smirnoff 				}
44293b3a8eb9SGleb Smirnoff 			} else {
44303b3a8eb9SGleb Smirnoff 				MD5_CTX	context;
44313b3a8eb9SGleb Smirnoff 
44323b3a8eb9SGleb Smirnoff 				$$ = calloc(1, sizeof(struct pf_poolhashkey));
44333b3a8eb9SGleb Smirnoff 				if ($$ == NULL)
44343b3a8eb9SGleb Smirnoff 					err(1, "hashkey: calloc");
44353b3a8eb9SGleb Smirnoff 				MD5Init(&context);
44363b3a8eb9SGleb Smirnoff 				MD5Update(&context, (unsigned char *)$1,
44373b3a8eb9SGleb Smirnoff 				    strlen($1));
44383b3a8eb9SGleb Smirnoff 				MD5Final((unsigned char *)$$, &context);
44393b3a8eb9SGleb Smirnoff 				HTONL($$->key32[0]);
44403b3a8eb9SGleb Smirnoff 				HTONL($$->key32[1]);
44413b3a8eb9SGleb Smirnoff 				HTONL($$->key32[2]);
44423b3a8eb9SGleb Smirnoff 				HTONL($$->key32[3]);
44433b3a8eb9SGleb Smirnoff 			}
44443b3a8eb9SGleb Smirnoff 			free($1);
44453b3a8eb9SGleb Smirnoff 		}
44463b3a8eb9SGleb Smirnoff 		;
44473b3a8eb9SGleb Smirnoff 
44483b3a8eb9SGleb Smirnoff pool_opts	:	{ bzero(&pool_opts, sizeof pool_opts); }
44493b3a8eb9SGleb Smirnoff 		    pool_opts_l
44503b3a8eb9SGleb Smirnoff 			{ $$ = pool_opts; }
44513b3a8eb9SGleb Smirnoff 		| /* empty */	{
44523b3a8eb9SGleb Smirnoff 			bzero(&pool_opts, sizeof pool_opts);
44533b3a8eb9SGleb Smirnoff 			$$ = pool_opts;
44543b3a8eb9SGleb Smirnoff 		}
44553b3a8eb9SGleb Smirnoff 		;
44563b3a8eb9SGleb Smirnoff 
44573b3a8eb9SGleb Smirnoff pool_opts_l	: pool_opts_l pool_opt
44583b3a8eb9SGleb Smirnoff 		| pool_opt
44593b3a8eb9SGleb Smirnoff 		;
44603b3a8eb9SGleb Smirnoff 
44613b3a8eb9SGleb Smirnoff pool_opt	: BITMASK	{
44623b3a8eb9SGleb Smirnoff 			if (pool_opts.type) {
44633b3a8eb9SGleb Smirnoff 				yyerror("pool type cannot be redefined");
44643b3a8eb9SGleb Smirnoff 				YYERROR;
44653b3a8eb9SGleb Smirnoff 			}
44663b3a8eb9SGleb Smirnoff 			pool_opts.type =  PF_POOL_BITMASK;
44673b3a8eb9SGleb Smirnoff 		}
44683b3a8eb9SGleb Smirnoff 		| RANDOM	{
44693b3a8eb9SGleb Smirnoff 			if (pool_opts.type) {
44703b3a8eb9SGleb Smirnoff 				yyerror("pool type cannot be redefined");
44713b3a8eb9SGleb Smirnoff 				YYERROR;
44723b3a8eb9SGleb Smirnoff 			}
44733b3a8eb9SGleb Smirnoff 			pool_opts.type = PF_POOL_RANDOM;
44743b3a8eb9SGleb Smirnoff 		}
44753b3a8eb9SGleb Smirnoff 		| SOURCEHASH hashkey {
44763b3a8eb9SGleb Smirnoff 			if (pool_opts.type) {
44773b3a8eb9SGleb Smirnoff 				yyerror("pool type cannot be redefined");
44783b3a8eb9SGleb Smirnoff 				YYERROR;
44793b3a8eb9SGleb Smirnoff 			}
44803b3a8eb9SGleb Smirnoff 			pool_opts.type = PF_POOL_SRCHASH;
44813b3a8eb9SGleb Smirnoff 			pool_opts.key = $2;
44823b3a8eb9SGleb Smirnoff 		}
44833b3a8eb9SGleb Smirnoff 		| ROUNDROBIN	{
44843b3a8eb9SGleb Smirnoff 			if (pool_opts.type) {
44853b3a8eb9SGleb Smirnoff 				yyerror("pool type cannot be redefined");
44863b3a8eb9SGleb Smirnoff 				YYERROR;
44873b3a8eb9SGleb Smirnoff 			}
44883b3a8eb9SGleb Smirnoff 			pool_opts.type = PF_POOL_ROUNDROBIN;
44893b3a8eb9SGleb Smirnoff 		}
44903b3a8eb9SGleb Smirnoff 		| STATICPORT	{
44913b3a8eb9SGleb Smirnoff 			if (pool_opts.staticport) {
44923b3a8eb9SGleb Smirnoff 				yyerror("static-port cannot be redefined");
44933b3a8eb9SGleb Smirnoff 				YYERROR;
44943b3a8eb9SGleb Smirnoff 			}
44953b3a8eb9SGleb Smirnoff 			pool_opts.staticport = 1;
44963b3a8eb9SGleb Smirnoff 		}
44973b3a8eb9SGleb Smirnoff 		| STICKYADDRESS	{
44981e73fbd8SFranco Fichtner 			if (pool_opts.marker & POM_STICKYADDRESS) {
44993b3a8eb9SGleb Smirnoff 				yyerror("sticky-address cannot be redefined");
45003b3a8eb9SGleb Smirnoff 				YYERROR;
45013b3a8eb9SGleb Smirnoff 			}
45023b3a8eb9SGleb Smirnoff 			pool_opts.marker |= POM_STICKYADDRESS;
45033b3a8eb9SGleb Smirnoff 			pool_opts.opts |= PF_POOL_STICKYADDR;
45043b3a8eb9SGleb Smirnoff 		}
45052aa21096SKurosawa Takahiro 		| MAPEPORTSET number '/' number '/' number {
45062aa21096SKurosawa Takahiro 			if (pool_opts.mape.offset) {
45072aa21096SKurosawa Takahiro 				yyerror("map-e-portset cannot be redefined");
45082aa21096SKurosawa Takahiro 				YYERROR;
45092aa21096SKurosawa Takahiro 			}
45102aa21096SKurosawa Takahiro 			if (pool_opts.type) {
45112aa21096SKurosawa Takahiro 				yyerror("map-e-portset cannot be used with "
45122aa21096SKurosawa Takahiro 					"address pools");
45132aa21096SKurosawa Takahiro 				YYERROR;
45142aa21096SKurosawa Takahiro 			}
45152aa21096SKurosawa Takahiro 			if ($2 <= 0 || $2 >= 16) {
45162aa21096SKurosawa Takahiro 				yyerror("MAP-E PSID offset must be 1-15");
45172aa21096SKurosawa Takahiro 				YYERROR;
45182aa21096SKurosawa Takahiro 			}
45192aa21096SKurosawa Takahiro 			if ($4 < 0 || $4 >= 16 || $2 + $4 > 16) {
45202aa21096SKurosawa Takahiro 				yyerror("Invalid MAP-E PSID length");
45212aa21096SKurosawa Takahiro 				YYERROR;
45222aa21096SKurosawa Takahiro 			} else if ($4 == 0) {
45232aa21096SKurosawa Takahiro 				yyerror("PSID Length = 0: this means"
45242aa21096SKurosawa Takahiro 				    " you do not need MAP-E");
45252aa21096SKurosawa Takahiro 				YYERROR;
45262aa21096SKurosawa Takahiro 			}
45272aa21096SKurosawa Takahiro 			if ($6 < 0 || $6 > 65535) {
45282aa21096SKurosawa Takahiro 				yyerror("Invalid MAP-E PSID");
45292aa21096SKurosawa Takahiro 				YYERROR;
45302aa21096SKurosawa Takahiro 			}
45312aa21096SKurosawa Takahiro 			pool_opts.mape.offset = $2;
45322aa21096SKurosawa Takahiro 			pool_opts.mape.psidlen = $4;
45332aa21096SKurosawa Takahiro 			pool_opts.mape.psid = $6;
45342aa21096SKurosawa Takahiro 		}
45353b3a8eb9SGleb Smirnoff 		;
45363b3a8eb9SGleb Smirnoff 
45373b3a8eb9SGleb Smirnoff redirection	: /* empty */			{ $$ = NULL; }
45383b3a8eb9SGleb Smirnoff 		| ARROW host			{
45393b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct redirection));
45403b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
45413b3a8eb9SGleb Smirnoff 				err(1, "redirection: calloc");
45423b3a8eb9SGleb Smirnoff 			$$->host = $2;
45433b3a8eb9SGleb Smirnoff 			$$->rport.a = $$->rport.b = $$->rport.t = 0;
45443b3a8eb9SGleb Smirnoff 		}
45453b3a8eb9SGleb Smirnoff 		| ARROW host PORT portstar	{
45463b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct redirection));
45473b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
45483b3a8eb9SGleb Smirnoff 				err(1, "redirection: calloc");
45493b3a8eb9SGleb Smirnoff 			$$->host = $2;
45503b3a8eb9SGleb Smirnoff 			$$->rport = $4;
45513b3a8eb9SGleb Smirnoff 		}
45523b3a8eb9SGleb Smirnoff 		;
45533b3a8eb9SGleb Smirnoff 
45543b3a8eb9SGleb Smirnoff natpasslog	: /* empty */	{ $$.b1 = $$.b2 = 0; $$.w2 = 0; }
45553b3a8eb9SGleb Smirnoff 		| PASS		{ $$.b1 = 1; $$.b2 = 0; $$.w2 = 0; }
45563b3a8eb9SGleb Smirnoff 		| PASS log	{ $$.b1 = 1; $$.b2 = $2.log; $$.w2 = $2.logif; }
45573b3a8eb9SGleb Smirnoff 		| log		{ $$.b1 = 0; $$.b2 = $1.log; $$.w2 = $1.logif; }
45583b3a8eb9SGleb Smirnoff 		;
45593b3a8eb9SGleb Smirnoff 
45603b3a8eb9SGleb Smirnoff nataction	: no NAT natpasslog {
45613b3a8eb9SGleb Smirnoff 			if ($1 && $3.b1) {
45623b3a8eb9SGleb Smirnoff 				yyerror("\"pass\" not valid with \"no\"");
45633b3a8eb9SGleb Smirnoff 				YYERROR;
45643b3a8eb9SGleb Smirnoff 			}
45653b3a8eb9SGleb Smirnoff 			if ($1)
45663b3a8eb9SGleb Smirnoff 				$$.b1 = PF_NONAT;
45673b3a8eb9SGleb Smirnoff 			else
45683b3a8eb9SGleb Smirnoff 				$$.b1 = PF_NAT;
45693b3a8eb9SGleb Smirnoff 			$$.b2 = $3.b1;
45703b3a8eb9SGleb Smirnoff 			$$.w = $3.b2;
45713b3a8eb9SGleb Smirnoff 			$$.w2 = $3.w2;
45723b3a8eb9SGleb Smirnoff 		}
45733b3a8eb9SGleb Smirnoff 		| no RDR natpasslog {
45743b3a8eb9SGleb Smirnoff 			if ($1 && $3.b1) {
45753b3a8eb9SGleb Smirnoff 				yyerror("\"pass\" not valid with \"no\"");
45763b3a8eb9SGleb Smirnoff 				YYERROR;
45773b3a8eb9SGleb Smirnoff 			}
45783b3a8eb9SGleb Smirnoff 			if ($1)
45793b3a8eb9SGleb Smirnoff 				$$.b1 = PF_NORDR;
45803b3a8eb9SGleb Smirnoff 			else
45813b3a8eb9SGleb Smirnoff 				$$.b1 = PF_RDR;
45823b3a8eb9SGleb Smirnoff 			$$.b2 = $3.b1;
45833b3a8eb9SGleb Smirnoff 			$$.w = $3.b2;
45843b3a8eb9SGleb Smirnoff 			$$.w2 = $3.w2;
45853b3a8eb9SGleb Smirnoff 		}
45863b3a8eb9SGleb Smirnoff 		;
45873b3a8eb9SGleb Smirnoff 
45883b3a8eb9SGleb Smirnoff natrule		: nataction interface af proto fromto tag tagged rtable
45893b3a8eb9SGleb Smirnoff 		    redirpool pool_opts
45903b3a8eb9SGleb Smirnoff 		{
4591e9eb0941SKristof Provost 			struct pfctl_rule	r;
45923b3a8eb9SGleb Smirnoff 
45933b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_NAT))
45943b3a8eb9SGleb Smirnoff 				YYERROR;
45953b3a8eb9SGleb Smirnoff 
45963b3a8eb9SGleb Smirnoff 			memset(&r, 0, sizeof(r));
45973b3a8eb9SGleb Smirnoff 
45983b3a8eb9SGleb Smirnoff 			r.action = $1.b1;
45993b3a8eb9SGleb Smirnoff 			r.natpass = $1.b2;
46003b3a8eb9SGleb Smirnoff 			r.log = $1.w;
46013b3a8eb9SGleb Smirnoff 			r.logif = $1.w2;
46023b3a8eb9SGleb Smirnoff 			r.af = $3;
46033b3a8eb9SGleb Smirnoff 
46043b3a8eb9SGleb Smirnoff 			if (!r.af) {
46053b3a8eb9SGleb Smirnoff 				if ($5.src.host && $5.src.host->af &&
46063b3a8eb9SGleb Smirnoff 				    !$5.src.host->ifindex)
46073b3a8eb9SGleb Smirnoff 					r.af = $5.src.host->af;
46083b3a8eb9SGleb Smirnoff 				else if ($5.dst.host && $5.dst.host->af &&
46093b3a8eb9SGleb Smirnoff 				    !$5.dst.host->ifindex)
46103b3a8eb9SGleb Smirnoff 					r.af = $5.dst.host->af;
46113b3a8eb9SGleb Smirnoff 			}
46123b3a8eb9SGleb Smirnoff 
46133b3a8eb9SGleb Smirnoff 			if ($6 != NULL)
46143b3a8eb9SGleb Smirnoff 				if (strlcpy(r.tagname, $6, PF_TAG_NAME_SIZE) >=
46153b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) {
46163b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
46173b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
46183b3a8eb9SGleb Smirnoff 					YYERROR;
46193b3a8eb9SGleb Smirnoff 				}
46203b3a8eb9SGleb Smirnoff 
46213b3a8eb9SGleb Smirnoff 			if ($7.name)
46223b3a8eb9SGleb Smirnoff 				if (strlcpy(r.match_tagname, $7.name,
46233b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
46243b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
46253b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
46263b3a8eb9SGleb Smirnoff 					YYERROR;
46273b3a8eb9SGleb Smirnoff 				}
46283b3a8eb9SGleb Smirnoff 			r.match_tag_not = $7.neg;
46293b3a8eb9SGleb Smirnoff 			r.rtableid = $8;
46303b3a8eb9SGleb Smirnoff 
46313b3a8eb9SGleb Smirnoff 			if (r.action == PF_NONAT || r.action == PF_NORDR) {
46323b3a8eb9SGleb Smirnoff 				if ($9 != NULL) {
46333b3a8eb9SGleb Smirnoff 					yyerror("translation rule with 'no' "
46343b3a8eb9SGleb Smirnoff 					    "does not need '->'");
46353b3a8eb9SGleb Smirnoff 					YYERROR;
46363b3a8eb9SGleb Smirnoff 				}
46373b3a8eb9SGleb Smirnoff 			} else {
46383b3a8eb9SGleb Smirnoff 				if ($9 == NULL || $9->host == NULL) {
46393b3a8eb9SGleb Smirnoff 					yyerror("translation rule requires '-> "
46403b3a8eb9SGleb Smirnoff 					    "address'");
46413b3a8eb9SGleb Smirnoff 					YYERROR;
46423b3a8eb9SGleb Smirnoff 				}
46433b3a8eb9SGleb Smirnoff 				if (!r.af && ! $9->host->ifindex)
46443b3a8eb9SGleb Smirnoff 					r.af = $9->host->af;
46453b3a8eb9SGleb Smirnoff 
46463b3a8eb9SGleb Smirnoff 				remove_invalid_hosts(&$9->host, &r.af);
46473b3a8eb9SGleb Smirnoff 				if (invalid_redirect($9->host, r.af))
46483b3a8eb9SGleb Smirnoff 					YYERROR;
46493b3a8eb9SGleb Smirnoff 				if (check_netmask($9->host, r.af))
46503b3a8eb9SGleb Smirnoff 					YYERROR;
46513b3a8eb9SGleb Smirnoff 
46523b3a8eb9SGleb Smirnoff 				r.rpool.proxy_port[0] = ntohs($9->rport.a);
46533b3a8eb9SGleb Smirnoff 
46543b3a8eb9SGleb Smirnoff 				switch (r.action) {
46553b3a8eb9SGleb Smirnoff 				case PF_RDR:
46563b3a8eb9SGleb Smirnoff 					if (!$9->rport.b && $9->rport.t &&
46573b3a8eb9SGleb Smirnoff 					    $5.dst.port != NULL) {
46583b3a8eb9SGleb Smirnoff 						r.rpool.proxy_port[1] =
46593b3a8eb9SGleb Smirnoff 						    ntohs($9->rport.a) +
46603b3a8eb9SGleb Smirnoff 						    (ntohs(
46613b3a8eb9SGleb Smirnoff 						    $5.dst.port->port[1]) -
46623b3a8eb9SGleb Smirnoff 						    ntohs(
46633b3a8eb9SGleb Smirnoff 						    $5.dst.port->port[0]));
46643b3a8eb9SGleb Smirnoff 					} else
46653b3a8eb9SGleb Smirnoff 						r.rpool.proxy_port[1] =
46663b3a8eb9SGleb Smirnoff 						    ntohs($9->rport.b);
46673b3a8eb9SGleb Smirnoff 					break;
46683b3a8eb9SGleb Smirnoff 				case PF_NAT:
46693b3a8eb9SGleb Smirnoff 					r.rpool.proxy_port[1] =
46703b3a8eb9SGleb Smirnoff 					    ntohs($9->rport.b);
46713b3a8eb9SGleb Smirnoff 					if (!r.rpool.proxy_port[0] &&
46723b3a8eb9SGleb Smirnoff 					    !r.rpool.proxy_port[1]) {
46733b3a8eb9SGleb Smirnoff 						r.rpool.proxy_port[0] =
46743b3a8eb9SGleb Smirnoff 						    PF_NAT_PROXY_PORT_LOW;
46753b3a8eb9SGleb Smirnoff 						r.rpool.proxy_port[1] =
46763b3a8eb9SGleb Smirnoff 						    PF_NAT_PROXY_PORT_HIGH;
46773b3a8eb9SGleb Smirnoff 					} else if (!r.rpool.proxy_port[1])
46783b3a8eb9SGleb Smirnoff 						r.rpool.proxy_port[1] =
46793b3a8eb9SGleb Smirnoff 						    r.rpool.proxy_port[0];
46803b3a8eb9SGleb Smirnoff 					break;
46813b3a8eb9SGleb Smirnoff 				default:
46823b3a8eb9SGleb Smirnoff 					break;
46833b3a8eb9SGleb Smirnoff 				}
46843b3a8eb9SGleb Smirnoff 
46853b3a8eb9SGleb Smirnoff 				r.rpool.opts = $10.type;
46863b3a8eb9SGleb Smirnoff 				if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
46873b3a8eb9SGleb Smirnoff 				    PF_POOL_NONE && ($9->host->next != NULL ||
46883b3a8eb9SGleb Smirnoff 				    $9->host->addr.type == PF_ADDR_TABLE ||
46893b3a8eb9SGleb Smirnoff 				    DYNIF_MULTIADDR($9->host->addr)))
46903b3a8eb9SGleb Smirnoff 					r.rpool.opts = PF_POOL_ROUNDROBIN;
46913b3a8eb9SGleb Smirnoff 				if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
46923b3a8eb9SGleb Smirnoff 				    PF_POOL_ROUNDROBIN &&
46933b3a8eb9SGleb Smirnoff 				    disallow_table($9->host, "tables are only "
46943b3a8eb9SGleb Smirnoff 				    "supported in round-robin redirection "
46953b3a8eb9SGleb Smirnoff 				    "pools"))
46963b3a8eb9SGleb Smirnoff 					YYERROR;
46973b3a8eb9SGleb Smirnoff 				if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
46983b3a8eb9SGleb Smirnoff 				    PF_POOL_ROUNDROBIN &&
46993b3a8eb9SGleb Smirnoff 				    disallow_alias($9->host, "interface (%s) "
47003b3a8eb9SGleb Smirnoff 				    "is only supported in round-robin "
47013b3a8eb9SGleb Smirnoff 				    "redirection pools"))
47023b3a8eb9SGleb Smirnoff 					YYERROR;
47033b3a8eb9SGleb Smirnoff 				if ($9->host->next != NULL) {
47043b3a8eb9SGleb Smirnoff 					if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
47053b3a8eb9SGleb Smirnoff 					    PF_POOL_ROUNDROBIN) {
47063b3a8eb9SGleb Smirnoff 						yyerror("only round-robin "
47073b3a8eb9SGleb Smirnoff 						    "valid for multiple "
47083b3a8eb9SGleb Smirnoff 						    "redirection addresses");
47093b3a8eb9SGleb Smirnoff 						YYERROR;
47103b3a8eb9SGleb Smirnoff 					}
47113b3a8eb9SGleb Smirnoff 				}
47123b3a8eb9SGleb Smirnoff 			}
47133b3a8eb9SGleb Smirnoff 
47143b3a8eb9SGleb Smirnoff 			if ($10.key != NULL)
47153b3a8eb9SGleb Smirnoff 				memcpy(&r.rpool.key, $10.key,
47163b3a8eb9SGleb Smirnoff 				    sizeof(struct pf_poolhashkey));
47173b3a8eb9SGleb Smirnoff 
47183b3a8eb9SGleb Smirnoff 			 if ($10.opts)
47193b3a8eb9SGleb Smirnoff 				r.rpool.opts |= $10.opts;
47203b3a8eb9SGleb Smirnoff 
47213b3a8eb9SGleb Smirnoff 			if ($10.staticport) {
47223b3a8eb9SGleb Smirnoff 				if (r.action != PF_NAT) {
47233b3a8eb9SGleb Smirnoff 					yyerror("the 'static-port' option is "
47243b3a8eb9SGleb Smirnoff 					    "only valid with nat rules");
47253b3a8eb9SGleb Smirnoff 					YYERROR;
47263b3a8eb9SGleb Smirnoff 				}
47273b3a8eb9SGleb Smirnoff 				if (r.rpool.proxy_port[0] !=
47283b3a8eb9SGleb Smirnoff 				    PF_NAT_PROXY_PORT_LOW &&
47293b3a8eb9SGleb Smirnoff 				    r.rpool.proxy_port[1] !=
47303b3a8eb9SGleb Smirnoff 				    PF_NAT_PROXY_PORT_HIGH) {
47313b3a8eb9SGleb Smirnoff 					yyerror("the 'static-port' option can't"
47323b3a8eb9SGleb Smirnoff 					    " be used when specifying a port"
47333b3a8eb9SGleb Smirnoff 					    " range");
47343b3a8eb9SGleb Smirnoff 					YYERROR;
47353b3a8eb9SGleb Smirnoff 				}
47363b3a8eb9SGleb Smirnoff 				r.rpool.proxy_port[0] = 0;
47373b3a8eb9SGleb Smirnoff 				r.rpool.proxy_port[1] = 0;
47383b3a8eb9SGleb Smirnoff 			}
47393b3a8eb9SGleb Smirnoff 
47402aa21096SKurosawa Takahiro 			if ($10.mape.offset) {
47412aa21096SKurosawa Takahiro 				if (r.action != PF_NAT) {
47422aa21096SKurosawa Takahiro 					yyerror("the 'map-e-portset' option is"
47432aa21096SKurosawa Takahiro 					    " only valid with nat rules");
47442aa21096SKurosawa Takahiro 					YYERROR;
47452aa21096SKurosawa Takahiro 				}
47462aa21096SKurosawa Takahiro 				if ($10.staticport) {
47472aa21096SKurosawa Takahiro 					yyerror("the 'map-e-portset' option"
47482aa21096SKurosawa Takahiro 					    " can't be used 'static-port'");
47492aa21096SKurosawa Takahiro 					YYERROR;
47502aa21096SKurosawa Takahiro 				}
47512aa21096SKurosawa Takahiro 				if (r.rpool.proxy_port[0] !=
47522aa21096SKurosawa Takahiro 				    PF_NAT_PROXY_PORT_LOW &&
47532aa21096SKurosawa Takahiro 				    r.rpool.proxy_port[1] !=
47542aa21096SKurosawa Takahiro 				    PF_NAT_PROXY_PORT_HIGH) {
47552aa21096SKurosawa Takahiro 					yyerror("the 'map-e-portset' option"
47562aa21096SKurosawa Takahiro 					    " can't be used when specifying"
47572aa21096SKurosawa Takahiro 					    " a port range");
47582aa21096SKurosawa Takahiro 					YYERROR;
47592aa21096SKurosawa Takahiro 				}
47602aa21096SKurosawa Takahiro 				r.rpool.mape = $10.mape;
47612aa21096SKurosawa Takahiro 			}
47622aa21096SKurosawa Takahiro 
47633b3a8eb9SGleb Smirnoff 			expand_rule(&r, $2, $9 == NULL ? NULL : $9->host, $4,
47643b3a8eb9SGleb Smirnoff 			    $5.src_os, $5.src.host, $5.src.port, $5.dst.host,
47653b3a8eb9SGleb Smirnoff 			    $5.dst.port, 0, 0, 0, "");
47663b3a8eb9SGleb Smirnoff 			free($9);
47673b3a8eb9SGleb Smirnoff 		}
47683b3a8eb9SGleb Smirnoff 		;
47693b3a8eb9SGleb Smirnoff 
47701e93588bSLuiz Otavio O Souza binatrule	: no BINAT natpasslog interface af proto FROM ipspec toipspec tag
47713b3a8eb9SGleb Smirnoff 		    tagged rtable redirection
47723b3a8eb9SGleb Smirnoff 		{
4773e9eb0941SKristof Provost 			struct pfctl_rule	binat;
47743b3a8eb9SGleb Smirnoff 			struct pf_pooladdr	*pa;
47753b3a8eb9SGleb Smirnoff 
47763b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_NAT))
47773b3a8eb9SGleb Smirnoff 				YYERROR;
47783b3a8eb9SGleb Smirnoff 			if (disallow_urpf_failed($9, "\"urpf-failed\" is not "
47793b3a8eb9SGleb Smirnoff 			    "permitted as a binat destination"))
47803b3a8eb9SGleb Smirnoff 				YYERROR;
47813b3a8eb9SGleb Smirnoff 
47823b3a8eb9SGleb Smirnoff 			memset(&binat, 0, sizeof(binat));
47833b3a8eb9SGleb Smirnoff 
47843b3a8eb9SGleb Smirnoff 			if ($1 && $3.b1) {
47853b3a8eb9SGleb Smirnoff 				yyerror("\"pass\" not valid with \"no\"");
47863b3a8eb9SGleb Smirnoff 				YYERROR;
47873b3a8eb9SGleb Smirnoff 			}
47883b3a8eb9SGleb Smirnoff 			if ($1)
47893b3a8eb9SGleb Smirnoff 				binat.action = PF_NOBINAT;
47903b3a8eb9SGleb Smirnoff 			else
47913b3a8eb9SGleb Smirnoff 				binat.action = PF_BINAT;
47923b3a8eb9SGleb Smirnoff 			binat.natpass = $3.b1;
47933b3a8eb9SGleb Smirnoff 			binat.log = $3.b2;
47943b3a8eb9SGleb Smirnoff 			binat.logif = $3.w2;
47953b3a8eb9SGleb Smirnoff 			binat.af = $5;
47963b3a8eb9SGleb Smirnoff 			if (!binat.af && $8 != NULL && $8->af)
47973b3a8eb9SGleb Smirnoff 				binat.af = $8->af;
47983b3a8eb9SGleb Smirnoff 			if (!binat.af && $9 != NULL && $9->af)
47993b3a8eb9SGleb Smirnoff 				binat.af = $9->af;
48003b3a8eb9SGleb Smirnoff 
48013b3a8eb9SGleb Smirnoff 			if (!binat.af && $13 != NULL && $13->host)
48023b3a8eb9SGleb Smirnoff 				binat.af = $13->host->af;
48033b3a8eb9SGleb Smirnoff 			if (!binat.af) {
48043b3a8eb9SGleb Smirnoff 				yyerror("address family (inet/inet6) "
48053b3a8eb9SGleb Smirnoff 				    "undefined");
48063b3a8eb9SGleb Smirnoff 				YYERROR;
48073b3a8eb9SGleb Smirnoff 			}
48083b3a8eb9SGleb Smirnoff 
48093b3a8eb9SGleb Smirnoff 			if ($4 != NULL) {
48103b3a8eb9SGleb Smirnoff 				memcpy(binat.ifname, $4->ifname,
48113b3a8eb9SGleb Smirnoff 				    sizeof(binat.ifname));
48123b3a8eb9SGleb Smirnoff 				binat.ifnot = $4->not;
48133b3a8eb9SGleb Smirnoff 				free($4);
48143b3a8eb9SGleb Smirnoff 			}
48153b3a8eb9SGleb Smirnoff 
48163b3a8eb9SGleb Smirnoff 			if ($10 != NULL)
48173b3a8eb9SGleb Smirnoff 				if (strlcpy(binat.tagname, $10,
48183b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
48193b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
48203b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
48213b3a8eb9SGleb Smirnoff 					YYERROR;
48223b3a8eb9SGleb Smirnoff 				}
48233b3a8eb9SGleb Smirnoff 			if ($11.name)
48243b3a8eb9SGleb Smirnoff 				if (strlcpy(binat.match_tagname, $11.name,
48253b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
48263b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
48273b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
48283b3a8eb9SGleb Smirnoff 					YYERROR;
48293b3a8eb9SGleb Smirnoff 				}
48303b3a8eb9SGleb Smirnoff 			binat.match_tag_not = $11.neg;
48313b3a8eb9SGleb Smirnoff 			binat.rtableid = $12;
48323b3a8eb9SGleb Smirnoff 
48333b3a8eb9SGleb Smirnoff 			if ($6 != NULL) {
48343b3a8eb9SGleb Smirnoff 				binat.proto = $6->proto;
48353b3a8eb9SGleb Smirnoff 				free($6);
48363b3a8eb9SGleb Smirnoff 			}
48373b3a8eb9SGleb Smirnoff 
48383b3a8eb9SGleb Smirnoff 			if ($8 != NULL && disallow_table($8, "invalid use of "
48393b3a8eb9SGleb Smirnoff 			    "table <%s> as the source address of a binat rule"))
48403b3a8eb9SGleb Smirnoff 				YYERROR;
48413b3a8eb9SGleb Smirnoff 			if ($8 != NULL && disallow_alias($8, "invalid use of "
48423b3a8eb9SGleb Smirnoff 			    "interface (%s) as the source address of a binat "
48433b3a8eb9SGleb Smirnoff 			    "rule"))
48443b3a8eb9SGleb Smirnoff 				YYERROR;
48453b3a8eb9SGleb Smirnoff 			if ($13 != NULL && $13->host != NULL && disallow_table(
48463b3a8eb9SGleb Smirnoff 			    $13->host, "invalid use of table <%s> as the "
48473b3a8eb9SGleb Smirnoff 			    "redirect address of a binat rule"))
48483b3a8eb9SGleb Smirnoff 				YYERROR;
48493b3a8eb9SGleb Smirnoff 			if ($13 != NULL && $13->host != NULL && disallow_alias(
48503b3a8eb9SGleb Smirnoff 			    $13->host, "invalid use of interface (%s) as the "
48513b3a8eb9SGleb Smirnoff 			    "redirect address of a binat rule"))
48523b3a8eb9SGleb Smirnoff 				YYERROR;
48533b3a8eb9SGleb Smirnoff 
48543b3a8eb9SGleb Smirnoff 			if ($8 != NULL) {
48553b3a8eb9SGleb Smirnoff 				if ($8->next) {
48563b3a8eb9SGleb Smirnoff 					yyerror("multiple binat ip addresses");
48573b3a8eb9SGleb Smirnoff 					YYERROR;
48583b3a8eb9SGleb Smirnoff 				}
48593b3a8eb9SGleb Smirnoff 				if ($8->addr.type == PF_ADDR_DYNIFTL)
48603b3a8eb9SGleb Smirnoff 					$8->af = binat.af;
48613b3a8eb9SGleb Smirnoff 				if ($8->af != binat.af) {
48623b3a8eb9SGleb Smirnoff 					yyerror("binat ip versions must match");
48633b3a8eb9SGleb Smirnoff 					YYERROR;
48643b3a8eb9SGleb Smirnoff 				}
48653b3a8eb9SGleb Smirnoff 				if (check_netmask($8, binat.af))
48663b3a8eb9SGleb Smirnoff 					YYERROR;
48673b3a8eb9SGleb Smirnoff 				memcpy(&binat.src.addr, &$8->addr,
48683b3a8eb9SGleb Smirnoff 				    sizeof(binat.src.addr));
48693b3a8eb9SGleb Smirnoff 				free($8);
48703b3a8eb9SGleb Smirnoff 			}
48713b3a8eb9SGleb Smirnoff 			if ($9 != NULL) {
48723b3a8eb9SGleb Smirnoff 				if ($9->next) {
48733b3a8eb9SGleb Smirnoff 					yyerror("multiple binat ip addresses");
48743b3a8eb9SGleb Smirnoff 					YYERROR;
48753b3a8eb9SGleb Smirnoff 				}
48763b3a8eb9SGleb Smirnoff 				if ($9->af != binat.af && $9->af) {
48773b3a8eb9SGleb Smirnoff 					yyerror("binat ip versions must match");
48783b3a8eb9SGleb Smirnoff 					YYERROR;
48793b3a8eb9SGleb Smirnoff 				}
48803b3a8eb9SGleb Smirnoff 				if (check_netmask($9, binat.af))
48813b3a8eb9SGleb Smirnoff 					YYERROR;
48823b3a8eb9SGleb Smirnoff 				memcpy(&binat.dst.addr, &$9->addr,
48833b3a8eb9SGleb Smirnoff 				    sizeof(binat.dst.addr));
48843b3a8eb9SGleb Smirnoff 				binat.dst.neg = $9->not;
48853b3a8eb9SGleb Smirnoff 				free($9);
48863b3a8eb9SGleb Smirnoff 			}
48873b3a8eb9SGleb Smirnoff 
48883b3a8eb9SGleb Smirnoff 			if (binat.action == PF_NOBINAT) {
48893b3a8eb9SGleb Smirnoff 				if ($13 != NULL) {
48903b3a8eb9SGleb Smirnoff 					yyerror("'no binat' rule does not need"
48913b3a8eb9SGleb Smirnoff 					    " '->'");
48923b3a8eb9SGleb Smirnoff 					YYERROR;
48933b3a8eb9SGleb Smirnoff 				}
48943b3a8eb9SGleb Smirnoff 			} else {
48953b3a8eb9SGleb Smirnoff 				if ($13 == NULL || $13->host == NULL) {
48963b3a8eb9SGleb Smirnoff 					yyerror("'binat' rule requires"
48973b3a8eb9SGleb Smirnoff 					    " '-> address'");
48983b3a8eb9SGleb Smirnoff 					YYERROR;
48993b3a8eb9SGleb Smirnoff 				}
49003b3a8eb9SGleb Smirnoff 
49013b3a8eb9SGleb Smirnoff 				remove_invalid_hosts(&$13->host, &binat.af);
49023b3a8eb9SGleb Smirnoff 				if (invalid_redirect($13->host, binat.af))
49033b3a8eb9SGleb Smirnoff 					YYERROR;
49043b3a8eb9SGleb Smirnoff 				if ($13->host->next != NULL) {
49053b3a8eb9SGleb Smirnoff 					yyerror("binat rule must redirect to "
49063b3a8eb9SGleb Smirnoff 					    "a single address");
49073b3a8eb9SGleb Smirnoff 					YYERROR;
49083b3a8eb9SGleb Smirnoff 				}
49093b3a8eb9SGleb Smirnoff 				if (check_netmask($13->host, binat.af))
49103b3a8eb9SGleb Smirnoff 					YYERROR;
49113b3a8eb9SGleb Smirnoff 
49123b3a8eb9SGleb Smirnoff 				if (!PF_AZERO(&binat.src.addr.v.a.mask,
49133b3a8eb9SGleb Smirnoff 				    binat.af) &&
49143b3a8eb9SGleb Smirnoff 				    !PF_AEQ(&binat.src.addr.v.a.mask,
49153b3a8eb9SGleb Smirnoff 				    &$13->host->addr.v.a.mask, binat.af)) {
49163b3a8eb9SGleb Smirnoff 					yyerror("'binat' source mask and "
49173b3a8eb9SGleb Smirnoff 					    "redirect mask must be the same");
49183b3a8eb9SGleb Smirnoff 					YYERROR;
49193b3a8eb9SGleb Smirnoff 				}
49203b3a8eb9SGleb Smirnoff 
49213b3a8eb9SGleb Smirnoff 				TAILQ_INIT(&binat.rpool.list);
49223b3a8eb9SGleb Smirnoff 				pa = calloc(1, sizeof(struct pf_pooladdr));
49233b3a8eb9SGleb Smirnoff 				if (pa == NULL)
49243b3a8eb9SGleb Smirnoff 					err(1, "binat: calloc");
49253b3a8eb9SGleb Smirnoff 				pa->addr = $13->host->addr;
49263b3a8eb9SGleb Smirnoff 				pa->ifname[0] = 0;
49273b3a8eb9SGleb Smirnoff 				TAILQ_INSERT_TAIL(&binat.rpool.list,
49283b3a8eb9SGleb Smirnoff 				    pa, entries);
49293b3a8eb9SGleb Smirnoff 
49303b3a8eb9SGleb Smirnoff 				free($13);
49313b3a8eb9SGleb Smirnoff 			}
49323b3a8eb9SGleb Smirnoff 
49330d71f9f3SKristof Provost 			pfctl_append_rule(pf, &binat, "");
49343b3a8eb9SGleb Smirnoff 		}
49353b3a8eb9SGleb Smirnoff 		;
49363b3a8eb9SGleb Smirnoff 
49373b3a8eb9SGleb Smirnoff tag		: /* empty */		{ $$ = NULL; }
49383b3a8eb9SGleb Smirnoff 		| TAG STRING		{ $$ = $2; }
49393b3a8eb9SGleb Smirnoff 		;
49403b3a8eb9SGleb Smirnoff 
49413b3a8eb9SGleb Smirnoff tagged		: /* empty */		{ $$.neg = 0; $$.name = NULL; }
49423b3a8eb9SGleb Smirnoff 		| not TAGGED string	{ $$.neg = $1; $$.name = $3; }
49433b3a8eb9SGleb Smirnoff 		;
49443b3a8eb9SGleb Smirnoff 
49453b3a8eb9SGleb Smirnoff rtable		: /* empty */		{ $$ = -1; }
49463b3a8eb9SGleb Smirnoff 		| RTABLE NUMBER		{
49473b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > rt_tableid_max()) {
49483b3a8eb9SGleb Smirnoff 				yyerror("invalid rtable id");
49493b3a8eb9SGleb Smirnoff 				YYERROR;
49503b3a8eb9SGleb Smirnoff 			}
49513b3a8eb9SGleb Smirnoff 			$$ = $2;
49523b3a8eb9SGleb Smirnoff 		}
49533b3a8eb9SGleb Smirnoff 		;
49543b3a8eb9SGleb Smirnoff 
49553b3a8eb9SGleb Smirnoff route_host	: STRING			{
49563b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_host));
49573b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
49583b3a8eb9SGleb Smirnoff 				err(1, "route_host: calloc");
4959e68de669SKristof Provost 			if (strlen($1) >= IFNAMSIZ) {
4960e68de669SKristof Provost 				yyerror("interface name too long");
4961e68de669SKristof Provost 				YYERROR;
4962e68de669SKristof Provost 			}
4963a2a90d6eSKristof Provost 			$$->ifname = strdup($1);
49643b3a8eb9SGleb Smirnoff 			set_ipmask($$, 128);
49653b3a8eb9SGleb Smirnoff 			$$->next = NULL;
49663b3a8eb9SGleb Smirnoff 			$$->tail = $$;
49673b3a8eb9SGleb Smirnoff 		}
49683b3a8eb9SGleb Smirnoff 		| '(' STRING host ')'		{
496958c8430aSKristof Provost 			struct node_host *n;
497058c8430aSKristof Provost 
49713b3a8eb9SGleb Smirnoff 			$$ = $3;
4972e68de669SKristof Provost 			for (n = $3; n != NULL; n = n->next) {
4973e68de669SKristof Provost 				if (strlen($2) >= IFNAMSIZ) {
4974e68de669SKristof Provost 					yyerror("interface name too long");
4975e68de669SKristof Provost 					YYERROR;
4976e68de669SKristof Provost 				}
4977a2a90d6eSKristof Provost 				n->ifname = strdup($2);
49783b3a8eb9SGleb Smirnoff 			}
4979e68de669SKristof Provost 		}
49803b3a8eb9SGleb Smirnoff 		;
49813b3a8eb9SGleb Smirnoff 
49823b3a8eb9SGleb Smirnoff route_host_list	: route_host optnl			{ $$ = $1; }
49833b3a8eb9SGleb Smirnoff 		| route_host_list comma route_host optnl {
49843b3a8eb9SGleb Smirnoff 			if ($1->af == 0)
49853b3a8eb9SGleb Smirnoff 				$1->af = $3->af;
49863b3a8eb9SGleb Smirnoff 			if ($1->af != $3->af) {
49873b3a8eb9SGleb Smirnoff 				yyerror("all pool addresses must be in the "
49883b3a8eb9SGleb Smirnoff 				    "same address family");
49893b3a8eb9SGleb Smirnoff 				YYERROR;
49903b3a8eb9SGleb Smirnoff 			}
49913b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
49923b3a8eb9SGleb Smirnoff 			$1->tail = $3->tail;
49933b3a8eb9SGleb Smirnoff 			$$ = $1;
49943b3a8eb9SGleb Smirnoff 		}
49953b3a8eb9SGleb Smirnoff 		;
49963b3a8eb9SGleb Smirnoff 
49973b3a8eb9SGleb Smirnoff routespec	: route_host			{ $$ = $1; }
49983b3a8eb9SGleb Smirnoff 		| '{' optnl route_host_list '}'	{ $$ = $3; }
49993b3a8eb9SGleb Smirnoff 		;
50003b3a8eb9SGleb Smirnoff 
50013b3a8eb9SGleb Smirnoff route		: /* empty */			{
50023b3a8eb9SGleb Smirnoff 			$$.host = NULL;
50033b3a8eb9SGleb Smirnoff 			$$.rt = 0;
50043b3a8eb9SGleb Smirnoff 			$$.pool_opts = 0;
50053b3a8eb9SGleb Smirnoff 		}
50063b3a8eb9SGleb Smirnoff 		| FASTROUTE {
5007813196a1SKristof Provost 			/* backwards-compat */
50083b3a8eb9SGleb Smirnoff 			$$.host = NULL;
5009813196a1SKristof Provost 			$$.rt = 0;
50103b3a8eb9SGleb Smirnoff 			$$.pool_opts = 0;
50113b3a8eb9SGleb Smirnoff 		}
50123b3a8eb9SGleb Smirnoff 		| ROUTETO routespec pool_opts {
50133b3a8eb9SGleb Smirnoff 			$$.host = $2;
50143b3a8eb9SGleb Smirnoff 			$$.rt = PF_ROUTETO;
50153b3a8eb9SGleb Smirnoff 			$$.pool_opts = $3.type | $3.opts;
50163b3a8eb9SGleb Smirnoff 			if ($3.key != NULL)
50173b3a8eb9SGleb Smirnoff 				$$.key = $3.key;
50183b3a8eb9SGleb Smirnoff 		}
50193b3a8eb9SGleb Smirnoff 		| REPLYTO routespec pool_opts {
50203b3a8eb9SGleb Smirnoff 			$$.host = $2;
50213b3a8eb9SGleb Smirnoff 			$$.rt = PF_REPLYTO;
50223b3a8eb9SGleb Smirnoff 			$$.pool_opts = $3.type | $3.opts;
50233b3a8eb9SGleb Smirnoff 			if ($3.key != NULL)
50243b3a8eb9SGleb Smirnoff 				$$.key = $3.key;
50253b3a8eb9SGleb Smirnoff 		}
50263b3a8eb9SGleb Smirnoff 		| DUPTO routespec pool_opts {
50273b3a8eb9SGleb Smirnoff 			$$.host = $2;
50283b3a8eb9SGleb Smirnoff 			$$.rt = PF_DUPTO;
50293b3a8eb9SGleb Smirnoff 			$$.pool_opts = $3.type | $3.opts;
50303b3a8eb9SGleb Smirnoff 			if ($3.key != NULL)
50313b3a8eb9SGleb Smirnoff 				$$.key = $3.key;
50323b3a8eb9SGleb Smirnoff 		}
50333b3a8eb9SGleb Smirnoff 		;
50343b3a8eb9SGleb Smirnoff 
50353b3a8eb9SGleb Smirnoff timeout_spec	: STRING NUMBER
50363b3a8eb9SGleb Smirnoff 		{
50373b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_OPTION)) {
50383b3a8eb9SGleb Smirnoff 				free($1);
50393b3a8eb9SGleb Smirnoff 				YYERROR;
50403b3a8eb9SGleb Smirnoff 			}
50413b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > UINT_MAX) {
50423b3a8eb9SGleb Smirnoff 				yyerror("only positive values permitted");
50433b3a8eb9SGleb Smirnoff 				YYERROR;
50443b3a8eb9SGleb Smirnoff 			}
50453b3a8eb9SGleb Smirnoff 			if (pfctl_set_timeout(pf, $1, $2, 0) != 0) {
50463b3a8eb9SGleb Smirnoff 				yyerror("unknown timeout %s", $1);
50473b3a8eb9SGleb Smirnoff 				free($1);
50483b3a8eb9SGleb Smirnoff 				YYERROR;
50493b3a8eb9SGleb Smirnoff 			}
50503b3a8eb9SGleb Smirnoff 			free($1);
50513b3a8eb9SGleb Smirnoff 		}
50527f8af000SLuiz Otavio O Souza 		| INTERVAL NUMBER		{
50537f8af000SLuiz Otavio O Souza 			if (check_rulestate(PFCTL_STATE_OPTION))
50547f8af000SLuiz Otavio O Souza 				YYERROR;
50557f8af000SLuiz Otavio O Souza 			if ($2 < 0 || $2 > UINT_MAX) {
50567f8af000SLuiz Otavio O Souza 				yyerror("only positive values permitted");
50577f8af000SLuiz Otavio O Souza 				YYERROR;
50587f8af000SLuiz Otavio O Souza 			}
50597f8af000SLuiz Otavio O Souza 			if (pfctl_set_timeout(pf, "interval", $2, 0) != 0)
50607f8af000SLuiz Otavio O Souza 				YYERROR;
50617f8af000SLuiz Otavio O Souza 		}
50623b3a8eb9SGleb Smirnoff 		;
50633b3a8eb9SGleb Smirnoff 
50643b3a8eb9SGleb Smirnoff timeout_list	: timeout_list comma timeout_spec optnl
50653b3a8eb9SGleb Smirnoff 		| timeout_spec optnl
50663b3a8eb9SGleb Smirnoff 		;
50673b3a8eb9SGleb Smirnoff 
50683b3a8eb9SGleb Smirnoff limit_spec	: STRING NUMBER
50693b3a8eb9SGleb Smirnoff 		{
50703b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_OPTION)) {
50713b3a8eb9SGleb Smirnoff 				free($1);
50723b3a8eb9SGleb Smirnoff 				YYERROR;
50733b3a8eb9SGleb Smirnoff 			}
50743b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > UINT_MAX) {
50753b3a8eb9SGleb Smirnoff 				yyerror("only positive values permitted");
50763b3a8eb9SGleb Smirnoff 				YYERROR;
50773b3a8eb9SGleb Smirnoff 			}
50783b3a8eb9SGleb Smirnoff 			if (pfctl_set_limit(pf, $1, $2) != 0) {
50793b3a8eb9SGleb Smirnoff 				yyerror("unable to set limit %s %u", $1, $2);
50803b3a8eb9SGleb Smirnoff 				free($1);
50813b3a8eb9SGleb Smirnoff 				YYERROR;
50823b3a8eb9SGleb Smirnoff 			}
50833b3a8eb9SGleb Smirnoff 			free($1);
50843b3a8eb9SGleb Smirnoff 		}
50853b3a8eb9SGleb Smirnoff 		;
50863b3a8eb9SGleb Smirnoff 
50873b3a8eb9SGleb Smirnoff limit_list	: limit_list comma limit_spec optnl
50883b3a8eb9SGleb Smirnoff 		| limit_spec optnl
50893b3a8eb9SGleb Smirnoff 		;
50903b3a8eb9SGleb Smirnoff 
50913b3a8eb9SGleb Smirnoff comma		: ','
50923b3a8eb9SGleb Smirnoff 		| /* empty */
50933b3a8eb9SGleb Smirnoff 		;
50943b3a8eb9SGleb Smirnoff 
50953b3a8eb9SGleb Smirnoff yesno		: NO			{ $$ = 0; }
50963b3a8eb9SGleb Smirnoff 		| STRING		{
50973b3a8eb9SGleb Smirnoff 			if (!strcmp($1, "yes"))
50983b3a8eb9SGleb Smirnoff 				$$ = 1;
50993b3a8eb9SGleb Smirnoff 			else {
51003b3a8eb9SGleb Smirnoff 				yyerror("invalid value '%s', expected 'yes' "
51013b3a8eb9SGleb Smirnoff 				    "or 'no'", $1);
51023b3a8eb9SGleb Smirnoff 				free($1);
51033b3a8eb9SGleb Smirnoff 				YYERROR;
51043b3a8eb9SGleb Smirnoff 			}
51053b3a8eb9SGleb Smirnoff 			free($1);
51063b3a8eb9SGleb Smirnoff 		}
51073b3a8eb9SGleb Smirnoff 		;
51083b3a8eb9SGleb Smirnoff 
51093b3a8eb9SGleb Smirnoff unaryop		: '='		{ $$ = PF_OP_EQ; }
51103b3a8eb9SGleb Smirnoff 		| '!' '='	{ $$ = PF_OP_NE; }
51113b3a8eb9SGleb Smirnoff 		| '<' '='	{ $$ = PF_OP_LE; }
51123b3a8eb9SGleb Smirnoff 		| '<'		{ $$ = PF_OP_LT; }
51133b3a8eb9SGleb Smirnoff 		| '>' '='	{ $$ = PF_OP_GE; }
51143b3a8eb9SGleb Smirnoff 		| '>'		{ $$ = PF_OP_GT; }
51153b3a8eb9SGleb Smirnoff 		;
51163b3a8eb9SGleb Smirnoff 
51173b3a8eb9SGleb Smirnoff %%
51183b3a8eb9SGleb Smirnoff 
51193b3a8eb9SGleb Smirnoff int
51203b3a8eb9SGleb Smirnoff yyerror(const char *fmt, ...)
51213b3a8eb9SGleb Smirnoff {
51223b3a8eb9SGleb Smirnoff 	va_list		 ap;
51233b3a8eb9SGleb Smirnoff 
51243b3a8eb9SGleb Smirnoff 	file->errors++;
51253b3a8eb9SGleb Smirnoff 	va_start(ap, fmt);
51263b3a8eb9SGleb Smirnoff 	fprintf(stderr, "%s:%d: ", file->name, yylval.lineno);
51273b3a8eb9SGleb Smirnoff 	vfprintf(stderr, fmt, ap);
51283b3a8eb9SGleb Smirnoff 	fprintf(stderr, "\n");
51293b3a8eb9SGleb Smirnoff 	va_end(ap);
51303b3a8eb9SGleb Smirnoff 	return (0);
51313b3a8eb9SGleb Smirnoff }
51323b3a8eb9SGleb Smirnoff 
51333b3a8eb9SGleb Smirnoff int
51343b3a8eb9SGleb Smirnoff disallow_table(struct node_host *h, const char *fmt)
51353b3a8eb9SGleb Smirnoff {
51363b3a8eb9SGleb Smirnoff 	for (; h != NULL; h = h->next)
51373b3a8eb9SGleb Smirnoff 		if (h->addr.type == PF_ADDR_TABLE) {
51383b3a8eb9SGleb Smirnoff 			yyerror(fmt, h->addr.v.tblname);
51393b3a8eb9SGleb Smirnoff 			return (1);
51403b3a8eb9SGleb Smirnoff 		}
51413b3a8eb9SGleb Smirnoff 	return (0);
51423b3a8eb9SGleb Smirnoff }
51433b3a8eb9SGleb Smirnoff 
51443b3a8eb9SGleb Smirnoff int
51453b3a8eb9SGleb Smirnoff disallow_urpf_failed(struct node_host *h, const char *fmt)
51463b3a8eb9SGleb Smirnoff {
51473b3a8eb9SGleb Smirnoff 	for (; h != NULL; h = h->next)
51483b3a8eb9SGleb Smirnoff 		if (h->addr.type == PF_ADDR_URPFFAILED) {
51493b3a8eb9SGleb Smirnoff 			yyerror(fmt);
51503b3a8eb9SGleb Smirnoff 			return (1);
51513b3a8eb9SGleb Smirnoff 		}
51523b3a8eb9SGleb Smirnoff 	return (0);
51533b3a8eb9SGleb Smirnoff }
51543b3a8eb9SGleb Smirnoff 
51553b3a8eb9SGleb Smirnoff int
51563b3a8eb9SGleb Smirnoff disallow_alias(struct node_host *h, const char *fmt)
51573b3a8eb9SGleb Smirnoff {
51583b3a8eb9SGleb Smirnoff 	for (; h != NULL; h = h->next)
51593b3a8eb9SGleb Smirnoff 		if (DYNIF_MULTIADDR(h->addr)) {
51603b3a8eb9SGleb Smirnoff 			yyerror(fmt, h->addr.v.tblname);
51613b3a8eb9SGleb Smirnoff 			return (1);
51623b3a8eb9SGleb Smirnoff 		}
51633b3a8eb9SGleb Smirnoff 	return (0);
51643b3a8eb9SGleb Smirnoff }
51653b3a8eb9SGleb Smirnoff 
51663b3a8eb9SGleb Smirnoff int
5167e9eb0941SKristof Provost rule_consistent(struct pfctl_rule *r, int anchor_call)
51683b3a8eb9SGleb Smirnoff {
51693b3a8eb9SGleb Smirnoff 	int	problems = 0;
51703b3a8eb9SGleb Smirnoff 
51713b3a8eb9SGleb Smirnoff 	switch (r->action) {
51723b3a8eb9SGleb Smirnoff 	case PF_PASS:
51733b3a8eb9SGleb Smirnoff 	case PF_DROP:
51743b3a8eb9SGleb Smirnoff 	case PF_SCRUB:
51753b3a8eb9SGleb Smirnoff 	case PF_NOSCRUB:
51763b3a8eb9SGleb Smirnoff 		problems = filter_consistent(r, anchor_call);
51773b3a8eb9SGleb Smirnoff 		break;
51783b3a8eb9SGleb Smirnoff 	case PF_NAT:
51793b3a8eb9SGleb Smirnoff 	case PF_NONAT:
51803b3a8eb9SGleb Smirnoff 		problems = nat_consistent(r);
51813b3a8eb9SGleb Smirnoff 		break;
51823b3a8eb9SGleb Smirnoff 	case PF_RDR:
51833b3a8eb9SGleb Smirnoff 	case PF_NORDR:
51843b3a8eb9SGleb Smirnoff 		problems = rdr_consistent(r);
51853b3a8eb9SGleb Smirnoff 		break;
51863b3a8eb9SGleb Smirnoff 	case PF_BINAT:
51873b3a8eb9SGleb Smirnoff 	case PF_NOBINAT:
51883b3a8eb9SGleb Smirnoff 	default:
51893b3a8eb9SGleb Smirnoff 		break;
51903b3a8eb9SGleb Smirnoff 	}
51913b3a8eb9SGleb Smirnoff 	return (problems);
51923b3a8eb9SGleb Smirnoff }
51933b3a8eb9SGleb Smirnoff 
51943b3a8eb9SGleb Smirnoff int
5195e9eb0941SKristof Provost filter_consistent(struct pfctl_rule *r, int anchor_call)
51963b3a8eb9SGleb Smirnoff {
51973b3a8eb9SGleb Smirnoff 	int	problems = 0;
51983b3a8eb9SGleb Smirnoff 
51993b3a8eb9SGleb Smirnoff 	if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP &&
52003b3a8eb9SGleb Smirnoff 	    (r->src.port_op || r->dst.port_op)) {
52013b3a8eb9SGleb Smirnoff 		yyerror("port only applies to tcp/udp");
52023b3a8eb9SGleb Smirnoff 		problems++;
52033b3a8eb9SGleb Smirnoff 	}
52043b3a8eb9SGleb Smirnoff 	if (r->proto != IPPROTO_ICMP && r->proto != IPPROTO_ICMPV6 &&
52053b3a8eb9SGleb Smirnoff 	    (r->type || r->code)) {
52063b3a8eb9SGleb Smirnoff 		yyerror("icmp-type/code only applies to icmp");
52073b3a8eb9SGleb Smirnoff 		problems++;
52083b3a8eb9SGleb Smirnoff 	}
52093b3a8eb9SGleb Smirnoff 	if (!r->af && (r->type || r->code)) {
52103b3a8eb9SGleb Smirnoff 		yyerror("must indicate address family with icmp-type/code");
52113b3a8eb9SGleb Smirnoff 		problems++;
52123b3a8eb9SGleb Smirnoff 	}
52133b3a8eb9SGleb Smirnoff 	if (r->overload_tblname[0] &&
52143b3a8eb9SGleb Smirnoff 	    r->max_src_conn == 0 && r->max_src_conn_rate.seconds == 0) {
52153b3a8eb9SGleb Smirnoff 		yyerror("'overload' requires 'max-src-conn' "
52163b3a8eb9SGleb Smirnoff 		    "or 'max-src-conn-rate'");
52173b3a8eb9SGleb Smirnoff 		problems++;
52183b3a8eb9SGleb Smirnoff 	}
52193b3a8eb9SGleb Smirnoff 	if ((r->proto == IPPROTO_ICMP && r->af == AF_INET6) ||
52203b3a8eb9SGleb Smirnoff 	    (r->proto == IPPROTO_ICMPV6 && r->af == AF_INET)) {
52213b3a8eb9SGleb Smirnoff 		yyerror("proto %s doesn't match address family %s",
52223b3a8eb9SGleb Smirnoff 		    r->proto == IPPROTO_ICMP ? "icmp" : "icmp6",
52233b3a8eb9SGleb Smirnoff 		    r->af == AF_INET ? "inet" : "inet6");
52243b3a8eb9SGleb Smirnoff 		problems++;
52253b3a8eb9SGleb Smirnoff 	}
52263b3a8eb9SGleb Smirnoff 	if (r->allow_opts && r->action != PF_PASS) {
52273b3a8eb9SGleb Smirnoff 		yyerror("allow-opts can only be specified for pass rules");
52283b3a8eb9SGleb Smirnoff 		problems++;
52293b3a8eb9SGleb Smirnoff 	}
52303b3a8eb9SGleb Smirnoff 	if (r->rule_flag & PFRULE_FRAGMENT && (r->src.port_op ||
52313b3a8eb9SGleb Smirnoff 	    r->dst.port_op || r->flagset || r->type || r->code)) {
52323b3a8eb9SGleb Smirnoff 		yyerror("fragments can be filtered only on IP header fields");
52333b3a8eb9SGleb Smirnoff 		problems++;
52343b3a8eb9SGleb Smirnoff 	}
52353b3a8eb9SGleb Smirnoff 	if (r->rule_flag & PFRULE_RETURNRST && r->proto != IPPROTO_TCP) {
52363b3a8eb9SGleb Smirnoff 		yyerror("return-rst can only be applied to TCP rules");
52373b3a8eb9SGleb Smirnoff 		problems++;
52383b3a8eb9SGleb Smirnoff 	}
52393b3a8eb9SGleb Smirnoff 	if (r->max_src_nodes && !(r->rule_flag & PFRULE_RULESRCTRACK)) {
52403b3a8eb9SGleb Smirnoff 		yyerror("max-src-nodes requires 'source-track rule'");
52413b3a8eb9SGleb Smirnoff 		problems++;
52423b3a8eb9SGleb Smirnoff 	}
52433b3a8eb9SGleb Smirnoff 	if (r->action == PF_DROP && r->keep_state) {
52443b3a8eb9SGleb Smirnoff 		yyerror("keep state on block rules doesn't make sense");
52453b3a8eb9SGleb Smirnoff 		problems++;
52463b3a8eb9SGleb Smirnoff 	}
52473b3a8eb9SGleb Smirnoff 	if (r->rule_flag & PFRULE_STATESLOPPY &&
52483b3a8eb9SGleb Smirnoff 	    (r->keep_state == PF_STATE_MODULATE ||
52493b3a8eb9SGleb Smirnoff 	    r->keep_state == PF_STATE_SYNPROXY)) {
52503b3a8eb9SGleb Smirnoff 		yyerror("sloppy state matching cannot be used with "
52513b3a8eb9SGleb Smirnoff 		    "synproxy state or modulate state");
52523b3a8eb9SGleb Smirnoff 		problems++;
52533b3a8eb9SGleb Smirnoff 	}
52543b3a8eb9SGleb Smirnoff 	return (-problems);
52553b3a8eb9SGleb Smirnoff }
52563b3a8eb9SGleb Smirnoff 
52573b3a8eb9SGleb Smirnoff int
5258e9eb0941SKristof Provost nat_consistent(struct pfctl_rule *r)
52593b3a8eb9SGleb Smirnoff {
52603b3a8eb9SGleb Smirnoff 	return (0);	/* yeah! */
52613b3a8eb9SGleb Smirnoff }
52623b3a8eb9SGleb Smirnoff 
52633b3a8eb9SGleb Smirnoff int
5264e9eb0941SKristof Provost rdr_consistent(struct pfctl_rule *r)
52653b3a8eb9SGleb Smirnoff {
52663b3a8eb9SGleb Smirnoff 	int			 problems = 0;
52673b3a8eb9SGleb Smirnoff 
52683b3a8eb9SGleb Smirnoff 	if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP) {
52693b3a8eb9SGleb Smirnoff 		if (r->src.port_op) {
52703b3a8eb9SGleb Smirnoff 			yyerror("src port only applies to tcp/udp");
52713b3a8eb9SGleb Smirnoff 			problems++;
52723b3a8eb9SGleb Smirnoff 		}
52733b3a8eb9SGleb Smirnoff 		if (r->dst.port_op) {
52743b3a8eb9SGleb Smirnoff 			yyerror("dst port only applies to tcp/udp");
52753b3a8eb9SGleb Smirnoff 			problems++;
52763b3a8eb9SGleb Smirnoff 		}
52773b3a8eb9SGleb Smirnoff 		if (r->rpool.proxy_port[0]) {
52783b3a8eb9SGleb Smirnoff 			yyerror("rpool port only applies to tcp/udp");
52793b3a8eb9SGleb Smirnoff 			problems++;
52803b3a8eb9SGleb Smirnoff 		}
52813b3a8eb9SGleb Smirnoff 	}
52823b3a8eb9SGleb Smirnoff 	if (r->dst.port_op &&
52833b3a8eb9SGleb Smirnoff 	    r->dst.port_op != PF_OP_EQ && r->dst.port_op != PF_OP_RRG) {
52843b3a8eb9SGleb Smirnoff 		yyerror("invalid port operator for rdr destination port");
52853b3a8eb9SGleb Smirnoff 		problems++;
52863b3a8eb9SGleb Smirnoff 	}
52873b3a8eb9SGleb Smirnoff 	return (-problems);
52883b3a8eb9SGleb Smirnoff }
52893b3a8eb9SGleb Smirnoff 
52903b3a8eb9SGleb Smirnoff int
52913b3a8eb9SGleb Smirnoff process_tabledef(char *name, struct table_opts *opts)
52923b3a8eb9SGleb Smirnoff {
52933b3a8eb9SGleb Smirnoff 	struct pfr_buffer	 ab;
52943b3a8eb9SGleb Smirnoff 	struct node_tinit	*ti;
5295542feeffSKristof Provost 	unsigned long		 maxcount;
5296542feeffSKristof Provost 	size_t			 s = sizeof(maxcount);
52973b3a8eb9SGleb Smirnoff 
52983b3a8eb9SGleb Smirnoff 	bzero(&ab, sizeof(ab));
52993b3a8eb9SGleb Smirnoff 	ab.pfrb_type = PFRB_ADDRS;
53003b3a8eb9SGleb Smirnoff 	SIMPLEQ_FOREACH(ti, &opts->init_nodes, entries) {
53013b3a8eb9SGleb Smirnoff 		if (ti->file)
53023b3a8eb9SGleb Smirnoff 			if (pfr_buf_load(&ab, ti->file, 0, append_addr)) {
53033b3a8eb9SGleb Smirnoff 				if (errno)
53043b3a8eb9SGleb Smirnoff 					yyerror("cannot load \"%s\": %s",
53053b3a8eb9SGleb Smirnoff 					    ti->file, strerror(errno));
53063b3a8eb9SGleb Smirnoff 				else
53073b3a8eb9SGleb Smirnoff 					yyerror("file \"%s\" contains bad data",
53083b3a8eb9SGleb Smirnoff 					    ti->file);
53093b3a8eb9SGleb Smirnoff 				goto _error;
53103b3a8eb9SGleb Smirnoff 			}
53113b3a8eb9SGleb Smirnoff 		if (ti->host)
53123b3a8eb9SGleb Smirnoff 			if (append_addr_host(&ab, ti->host, 0, 0)) {
53133b3a8eb9SGleb Smirnoff 				yyerror("cannot create address buffer: %s",
53143b3a8eb9SGleb Smirnoff 				    strerror(errno));
53153b3a8eb9SGleb Smirnoff 				goto _error;
53163b3a8eb9SGleb Smirnoff 			}
53173b3a8eb9SGleb Smirnoff 	}
53183b3a8eb9SGleb Smirnoff 	if (pf->opts & PF_OPT_VERBOSE)
53193b3a8eb9SGleb Smirnoff 		print_tabledef(name, opts->flags, opts->init_addr,
53203b3a8eb9SGleb Smirnoff 		    &opts->init_nodes);
53213b3a8eb9SGleb Smirnoff 	if (!(pf->opts & PF_OPT_NOACTION) &&
53223b3a8eb9SGleb Smirnoff 	    pfctl_define_table(name, opts->flags, opts->init_addr,
53233b3a8eb9SGleb Smirnoff 	    pf->anchor->name, &ab, pf->anchor->ruleset.tticket)) {
5324542feeffSKristof Provost 
5325542feeffSKristof Provost 		if (sysctlbyname("net.pf.request_maxcount", &maxcount, &s,
5326542feeffSKristof Provost 		    NULL, 0) == -1)
5327542feeffSKristof Provost 			maxcount = 65535;
5328542feeffSKristof Provost 
5329542feeffSKristof Provost 		if (ab.pfrb_size > maxcount)
5330542feeffSKristof Provost 			yyerror("cannot define table %s: too many elements.\n"
5331542feeffSKristof Provost 			    "Consider increasing net.pf.request_maxcount.",
5332542feeffSKristof Provost 			    name);
5333542feeffSKristof Provost 		else
53343b3a8eb9SGleb Smirnoff 			yyerror("cannot define table %s: %s", name,
53353b3a8eb9SGleb Smirnoff 			    pfr_strerror(errno));
5336542feeffSKristof Provost 
53373b3a8eb9SGleb Smirnoff 		goto _error;
53383b3a8eb9SGleb Smirnoff 	}
53393b3a8eb9SGleb Smirnoff 	pf->tdirty = 1;
53403b3a8eb9SGleb Smirnoff 	pfr_buf_clear(&ab);
53413b3a8eb9SGleb Smirnoff 	return (0);
53423b3a8eb9SGleb Smirnoff _error:
53433b3a8eb9SGleb Smirnoff 	pfr_buf_clear(&ab);
53443b3a8eb9SGleb Smirnoff 	return (-1);
53453b3a8eb9SGleb Smirnoff }
53463b3a8eb9SGleb Smirnoff 
53473b3a8eb9SGleb Smirnoff struct keywords {
53483b3a8eb9SGleb Smirnoff 	const char	*k_name;
53493b3a8eb9SGleb Smirnoff 	int		 k_val;
53503b3a8eb9SGleb Smirnoff };
53513b3a8eb9SGleb Smirnoff 
53523b3a8eb9SGleb Smirnoff /* macro gore, but you should've seen the prior indentation nightmare... */
53533b3a8eb9SGleb Smirnoff 
53543b3a8eb9SGleb Smirnoff #define FREE_LIST(T,r) \
53553b3a8eb9SGleb Smirnoff 	do { \
53563b3a8eb9SGleb Smirnoff 		T *p, *node = r; \
53573b3a8eb9SGleb Smirnoff 		while (node != NULL) { \
53583b3a8eb9SGleb Smirnoff 			p = node; \
53593b3a8eb9SGleb Smirnoff 			node = node->next; \
53603b3a8eb9SGleb Smirnoff 			free(p); \
53613b3a8eb9SGleb Smirnoff 		} \
53623b3a8eb9SGleb Smirnoff 	} while (0)
53633b3a8eb9SGleb Smirnoff 
53643b3a8eb9SGleb Smirnoff #define LOOP_THROUGH(T,n,r,C) \
53653b3a8eb9SGleb Smirnoff 	do { \
53663b3a8eb9SGleb Smirnoff 		T *n; \
53673b3a8eb9SGleb Smirnoff 		if (r == NULL) { \
53683b3a8eb9SGleb Smirnoff 			r = calloc(1, sizeof(T)); \
53693b3a8eb9SGleb Smirnoff 			if (r == NULL) \
53703b3a8eb9SGleb Smirnoff 				err(1, "LOOP: calloc"); \
53713b3a8eb9SGleb Smirnoff 			r->next = NULL; \
53723b3a8eb9SGleb Smirnoff 		} \
53733b3a8eb9SGleb Smirnoff 		n = r; \
53743b3a8eb9SGleb Smirnoff 		while (n != NULL) { \
53753b3a8eb9SGleb Smirnoff 			do { \
53763b3a8eb9SGleb Smirnoff 				C; \
53773b3a8eb9SGleb Smirnoff 			} while (0); \
53783b3a8eb9SGleb Smirnoff 			n = n->next; \
53793b3a8eb9SGleb Smirnoff 		} \
53803b3a8eb9SGleb Smirnoff 	} while (0)
53813b3a8eb9SGleb Smirnoff 
53823b3a8eb9SGleb Smirnoff void
53833b3a8eb9SGleb Smirnoff expand_label_str(char *label, size_t len, const char *srch, const char *repl)
53843b3a8eb9SGleb Smirnoff {
53853b3a8eb9SGleb Smirnoff 	char *tmp;
53863b3a8eb9SGleb Smirnoff 	char *p, *q;
53873b3a8eb9SGleb Smirnoff 
53883b3a8eb9SGleb Smirnoff 	if ((tmp = calloc(1, len)) == NULL)
53893b3a8eb9SGleb Smirnoff 		err(1, "expand_label_str: calloc");
53903b3a8eb9SGleb Smirnoff 	p = q = label;
53913b3a8eb9SGleb Smirnoff 	while ((q = strstr(p, srch)) != NULL) {
53923b3a8eb9SGleb Smirnoff 		*q = '\0';
53933b3a8eb9SGleb Smirnoff 		if ((strlcat(tmp, p, len) >= len) ||
53943b3a8eb9SGleb Smirnoff 		    (strlcat(tmp, repl, len) >= len))
53953b3a8eb9SGleb Smirnoff 			errx(1, "expand_label: label too long");
53963b3a8eb9SGleb Smirnoff 		q += strlen(srch);
53973b3a8eb9SGleb Smirnoff 		p = q;
53983b3a8eb9SGleb Smirnoff 	}
53993b3a8eb9SGleb Smirnoff 	if (strlcat(tmp, p, len) >= len)
54003b3a8eb9SGleb Smirnoff 		errx(1, "expand_label: label too long");
54013b3a8eb9SGleb Smirnoff 	strlcpy(label, tmp, len);	/* always fits */
54023b3a8eb9SGleb Smirnoff 	free(tmp);
54033b3a8eb9SGleb Smirnoff }
54043b3a8eb9SGleb Smirnoff 
54053b3a8eb9SGleb Smirnoff void
54063b3a8eb9SGleb Smirnoff expand_label_if(const char *name, char *label, size_t len, const char *ifname)
54073b3a8eb9SGleb Smirnoff {
54083b3a8eb9SGleb Smirnoff 	if (strstr(label, name) != NULL) {
54093b3a8eb9SGleb Smirnoff 		if (!*ifname)
54103b3a8eb9SGleb Smirnoff 			expand_label_str(label, len, name, "any");
54113b3a8eb9SGleb Smirnoff 		else
54123b3a8eb9SGleb Smirnoff 			expand_label_str(label, len, name, ifname);
54133b3a8eb9SGleb Smirnoff 	}
54143b3a8eb9SGleb Smirnoff }
54153b3a8eb9SGleb Smirnoff 
54163b3a8eb9SGleb Smirnoff void
54173b3a8eb9SGleb Smirnoff expand_label_addr(const char *name, char *label, size_t len, sa_family_t af,
541809c7f238SKristof Provost     struct pf_rule_addr *addr)
54193b3a8eb9SGleb Smirnoff {
54203b3a8eb9SGleb Smirnoff 	char tmp[64], tmp_not[66];
54213b3a8eb9SGleb Smirnoff 
54223b3a8eb9SGleb Smirnoff 	if (strstr(label, name) != NULL) {
542309c7f238SKristof Provost 		switch (addr->addr.type) {
54243b3a8eb9SGleb Smirnoff 		case PF_ADDR_DYNIFTL:
542509c7f238SKristof Provost 			snprintf(tmp, sizeof(tmp), "(%s)", addr->addr.v.ifname);
54263b3a8eb9SGleb Smirnoff 			break;
54273b3a8eb9SGleb Smirnoff 		case PF_ADDR_TABLE:
542809c7f238SKristof Provost 			snprintf(tmp, sizeof(tmp), "<%s>", addr->addr.v.tblname);
54293b3a8eb9SGleb Smirnoff 			break;
54303b3a8eb9SGleb Smirnoff 		case PF_ADDR_NOROUTE:
54313b3a8eb9SGleb Smirnoff 			snprintf(tmp, sizeof(tmp), "no-route");
54323b3a8eb9SGleb Smirnoff 			break;
54333b3a8eb9SGleb Smirnoff 		case PF_ADDR_URPFFAILED:
54343b3a8eb9SGleb Smirnoff 			snprintf(tmp, sizeof(tmp), "urpf-failed");
54353b3a8eb9SGleb Smirnoff 			break;
54363b3a8eb9SGleb Smirnoff 		case PF_ADDR_ADDRMASK:
543709c7f238SKristof Provost 			if (!af || (PF_AZERO(&addr->addr.v.a.addr, af) &&
543809c7f238SKristof Provost 			    PF_AZERO(&addr->addr.v.a.mask, af)))
54393b3a8eb9SGleb Smirnoff 				snprintf(tmp, sizeof(tmp), "any");
54403b3a8eb9SGleb Smirnoff 			else {
54413b3a8eb9SGleb Smirnoff 				char	a[48];
54423b3a8eb9SGleb Smirnoff 				int	bits;
54433b3a8eb9SGleb Smirnoff 
544409c7f238SKristof Provost 				if (inet_ntop(af, &addr->addr.v.a.addr, a,
54453b3a8eb9SGleb Smirnoff 				    sizeof(a)) == NULL)
54463b3a8eb9SGleb Smirnoff 					snprintf(tmp, sizeof(tmp), "?");
54473b3a8eb9SGleb Smirnoff 				else {
544809c7f238SKristof Provost 					bits = unmask(&addr->addr.v.a.mask, af);
54493b3a8eb9SGleb Smirnoff 					if ((af == AF_INET && bits < 32) ||
54503b3a8eb9SGleb Smirnoff 					    (af == AF_INET6 && bits < 128))
54513b3a8eb9SGleb Smirnoff 						snprintf(tmp, sizeof(tmp),
54523b3a8eb9SGleb Smirnoff 						    "%s/%d", a, bits);
54533b3a8eb9SGleb Smirnoff 					else
54543b3a8eb9SGleb Smirnoff 						snprintf(tmp, sizeof(tmp),
54553b3a8eb9SGleb Smirnoff 						    "%s", a);
54563b3a8eb9SGleb Smirnoff 				}
54573b3a8eb9SGleb Smirnoff 			}
54583b3a8eb9SGleb Smirnoff 			break;
54593b3a8eb9SGleb Smirnoff 		default:
54603b3a8eb9SGleb Smirnoff 			snprintf(tmp, sizeof(tmp), "?");
54613b3a8eb9SGleb Smirnoff 			break;
54623b3a8eb9SGleb Smirnoff 		}
54633b3a8eb9SGleb Smirnoff 
546409c7f238SKristof Provost 		if (addr->neg) {
54653b3a8eb9SGleb Smirnoff 			snprintf(tmp_not, sizeof(tmp_not), "! %s", tmp);
54663b3a8eb9SGleb Smirnoff 			expand_label_str(label, len, name, tmp_not);
54673b3a8eb9SGleb Smirnoff 		} else
54683b3a8eb9SGleb Smirnoff 			expand_label_str(label, len, name, tmp);
54693b3a8eb9SGleb Smirnoff 	}
54703b3a8eb9SGleb Smirnoff }
54713b3a8eb9SGleb Smirnoff 
54723b3a8eb9SGleb Smirnoff void
54733b3a8eb9SGleb Smirnoff expand_label_port(const char *name, char *label, size_t len,
547409c7f238SKristof Provost     struct pf_rule_addr *addr)
54753b3a8eb9SGleb Smirnoff {
54763b3a8eb9SGleb Smirnoff 	char	 a1[6], a2[6], op[13] = "";
54773b3a8eb9SGleb Smirnoff 
54783b3a8eb9SGleb Smirnoff 	if (strstr(label, name) != NULL) {
547909c7f238SKristof Provost 		snprintf(a1, sizeof(a1), "%u", ntohs(addr->port[0]));
548009c7f238SKristof Provost 		snprintf(a2, sizeof(a2), "%u", ntohs(addr->port[1]));
548109c7f238SKristof Provost 		if (!addr->port_op)
54823b3a8eb9SGleb Smirnoff 			;
548309c7f238SKristof Provost 		else if (addr->port_op == PF_OP_IRG)
54843b3a8eb9SGleb Smirnoff 			snprintf(op, sizeof(op), "%s><%s", a1, a2);
548509c7f238SKristof Provost 		else if (addr->port_op == PF_OP_XRG)
54863b3a8eb9SGleb Smirnoff 			snprintf(op, sizeof(op), "%s<>%s", a1, a2);
548709c7f238SKristof Provost 		else if (addr->port_op == PF_OP_EQ)
54883b3a8eb9SGleb Smirnoff 			snprintf(op, sizeof(op), "%s", a1);
548909c7f238SKristof Provost 		else if (addr->port_op == PF_OP_NE)
54903b3a8eb9SGleb Smirnoff 			snprintf(op, sizeof(op), "!=%s", a1);
549109c7f238SKristof Provost 		else if (addr->port_op == PF_OP_LT)
54923b3a8eb9SGleb Smirnoff 			snprintf(op, sizeof(op), "<%s", a1);
549309c7f238SKristof Provost 		else if (addr->port_op == PF_OP_LE)
54943b3a8eb9SGleb Smirnoff 			snprintf(op, sizeof(op), "<=%s", a1);
549509c7f238SKristof Provost 		else if (addr->port_op == PF_OP_GT)
54963b3a8eb9SGleb Smirnoff 			snprintf(op, sizeof(op), ">%s", a1);
549709c7f238SKristof Provost 		else if (addr->port_op == PF_OP_GE)
54983b3a8eb9SGleb Smirnoff 			snprintf(op, sizeof(op), ">=%s", a1);
54993b3a8eb9SGleb Smirnoff 		expand_label_str(label, len, name, op);
55003b3a8eb9SGleb Smirnoff 	}
55013b3a8eb9SGleb Smirnoff }
55023b3a8eb9SGleb Smirnoff 
55033b3a8eb9SGleb Smirnoff void
55043b3a8eb9SGleb Smirnoff expand_label_proto(const char *name, char *label, size_t len, u_int8_t proto)
55053b3a8eb9SGleb Smirnoff {
5506858937beSMateusz Guzik 	const char *protoname;
55073b3a8eb9SGleb Smirnoff 	char n[4];
55083b3a8eb9SGleb Smirnoff 
55093b3a8eb9SGleb Smirnoff 	if (strstr(label, name) != NULL) {
5510858937beSMateusz Guzik 		protoname = pfctl_proto2name(proto);
5511858937beSMateusz Guzik 		if (protoname != NULL)
5512858937beSMateusz Guzik 			expand_label_str(label, len, name, protoname);
55133b3a8eb9SGleb Smirnoff 		else {
55143b3a8eb9SGleb Smirnoff 			snprintf(n, sizeof(n), "%u", proto);
55153b3a8eb9SGleb Smirnoff 			expand_label_str(label, len, name, n);
55163b3a8eb9SGleb Smirnoff 		}
55173b3a8eb9SGleb Smirnoff 	}
55183b3a8eb9SGleb Smirnoff }
55193b3a8eb9SGleb Smirnoff 
55203b3a8eb9SGleb Smirnoff void
552109c7f238SKristof Provost expand_label_nr(const char *name, char *label, size_t len,
552209c7f238SKristof Provost     struct pfctl_rule *r)
55233b3a8eb9SGleb Smirnoff {
55243b3a8eb9SGleb Smirnoff 	char n[11];
55253b3a8eb9SGleb Smirnoff 
55263b3a8eb9SGleb Smirnoff 	if (strstr(label, name) != NULL) {
552709c7f238SKristof Provost 		snprintf(n, sizeof(n), "%u", r->nr);
55283b3a8eb9SGleb Smirnoff 		expand_label_str(label, len, name, n);
55293b3a8eb9SGleb Smirnoff 	}
55303b3a8eb9SGleb Smirnoff }
55313b3a8eb9SGleb Smirnoff 
55323b3a8eb9SGleb Smirnoff void
553309c7f238SKristof Provost expand_label(char *label, size_t len, struct pfctl_rule *r)
55343b3a8eb9SGleb Smirnoff {
553509c7f238SKristof Provost 	expand_label_if("$if", label, len, r->ifname);
553609c7f238SKristof Provost 	expand_label_addr("$srcaddr", label, len, r->af, &r->src);
553709c7f238SKristof Provost 	expand_label_addr("$dstaddr", label, len, r->af, &r->dst);
553809c7f238SKristof Provost 	expand_label_port("$srcport", label, len, &r->src);
553909c7f238SKristof Provost 	expand_label_port("$dstport", label, len, &r->dst);
554009c7f238SKristof Provost 	expand_label_proto("$proto", label, len, r->proto);
554109c7f238SKristof Provost 	expand_label_nr("$nr", label, len, r);
55423b3a8eb9SGleb Smirnoff }
55433b3a8eb9SGleb Smirnoff 
55443b3a8eb9SGleb Smirnoff int
55453b3a8eb9SGleb Smirnoff expand_altq(struct pf_altq *a, struct node_if *interfaces,
55463b3a8eb9SGleb Smirnoff     struct node_queue *nqueues, struct node_queue_bw bwspec,
55473b3a8eb9SGleb Smirnoff     struct node_queue_opt *opts)
55483b3a8eb9SGleb Smirnoff {
55493b3a8eb9SGleb Smirnoff 	struct pf_altq		 pa, pb;
55503b3a8eb9SGleb Smirnoff 	char			 qname[PF_QNAME_SIZE];
55513b3a8eb9SGleb Smirnoff 	struct node_queue	*n;
55523b3a8eb9SGleb Smirnoff 	struct node_queue_bw	 bw;
55533b3a8eb9SGleb Smirnoff 	int			 errs = 0;
55543b3a8eb9SGleb Smirnoff 
55553b3a8eb9SGleb Smirnoff 	if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
55563b3a8eb9SGleb Smirnoff 		FREE_LIST(struct node_if, interfaces);
55570a70aaf8SLuiz Otavio O Souza 		if (nqueues)
55583b3a8eb9SGleb Smirnoff 			FREE_LIST(struct node_queue, nqueues);
55593b3a8eb9SGleb Smirnoff 		return (0);
55603b3a8eb9SGleb Smirnoff 	}
55613b3a8eb9SGleb Smirnoff 
55623b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_if, interface, interfaces,
55633b3a8eb9SGleb Smirnoff 		memcpy(&pa, a, sizeof(struct pf_altq));
55643b3a8eb9SGleb Smirnoff 		if (strlcpy(pa.ifname, interface->ifname,
55653b3a8eb9SGleb Smirnoff 		    sizeof(pa.ifname)) >= sizeof(pa.ifname))
55663b3a8eb9SGleb Smirnoff 			errx(1, "expand_altq: strlcpy");
55673b3a8eb9SGleb Smirnoff 
55683b3a8eb9SGleb Smirnoff 		if (interface->not) {
55693b3a8eb9SGleb Smirnoff 			yyerror("altq on ! <interface> is not supported");
55703b3a8eb9SGleb Smirnoff 			errs++;
55713b3a8eb9SGleb Smirnoff 		} else {
55723b3a8eb9SGleb Smirnoff 			if (eval_pfaltq(pf, &pa, &bwspec, opts))
55733b3a8eb9SGleb Smirnoff 				errs++;
55743b3a8eb9SGleb Smirnoff 			else
55753b3a8eb9SGleb Smirnoff 				if (pfctl_add_altq(pf, &pa))
55763b3a8eb9SGleb Smirnoff 					errs++;
55773b3a8eb9SGleb Smirnoff 
55783b3a8eb9SGleb Smirnoff 			if (pf->opts & PF_OPT_VERBOSE) {
55793b3a8eb9SGleb Smirnoff 				print_altq(&pf->paltq->altq, 0,
55803b3a8eb9SGleb Smirnoff 				    &bwspec, opts);
55813b3a8eb9SGleb Smirnoff 				if (nqueues && nqueues->tail) {
55823b3a8eb9SGleb Smirnoff 					printf("queue { ");
55833b3a8eb9SGleb Smirnoff 					LOOP_THROUGH(struct node_queue, queue,
55843b3a8eb9SGleb Smirnoff 					    nqueues,
55853b3a8eb9SGleb Smirnoff 						printf("%s ",
55863b3a8eb9SGleb Smirnoff 						    queue->queue);
55873b3a8eb9SGleb Smirnoff 					);
55883b3a8eb9SGleb Smirnoff 					printf("}");
55893b3a8eb9SGleb Smirnoff 				}
55903b3a8eb9SGleb Smirnoff 				printf("\n");
55913b3a8eb9SGleb Smirnoff 			}
55923b3a8eb9SGleb Smirnoff 
55933b3a8eb9SGleb Smirnoff 			if (pa.scheduler == ALTQT_CBQ ||
5594dc784287SKristof Provost 			    pa.scheduler == ALTQT_HFSC ||
5595dc784287SKristof Provost 			    pa.scheduler == ALTQT_FAIRQ) {
55963b3a8eb9SGleb Smirnoff 				/* now create a root queue */
55973b3a8eb9SGleb Smirnoff 				memset(&pb, 0, sizeof(struct pf_altq));
55983b3a8eb9SGleb Smirnoff 				if (strlcpy(qname, "root_", sizeof(qname)) >=
55993b3a8eb9SGleb Smirnoff 				    sizeof(qname))
56003b3a8eb9SGleb Smirnoff 					errx(1, "expand_altq: strlcpy");
56013b3a8eb9SGleb Smirnoff 				if (strlcat(qname, interface->ifname,
56023b3a8eb9SGleb Smirnoff 				    sizeof(qname)) >= sizeof(qname))
56033b3a8eb9SGleb Smirnoff 					errx(1, "expand_altq: strlcat");
56043b3a8eb9SGleb Smirnoff 				if (strlcpy(pb.qname, qname,
56053b3a8eb9SGleb Smirnoff 				    sizeof(pb.qname)) >= sizeof(pb.qname))
56063b3a8eb9SGleb Smirnoff 					errx(1, "expand_altq: strlcpy");
56073b3a8eb9SGleb Smirnoff 				if (strlcpy(pb.ifname, interface->ifname,
56083b3a8eb9SGleb Smirnoff 				    sizeof(pb.ifname)) >= sizeof(pb.ifname))
56093b3a8eb9SGleb Smirnoff 					errx(1, "expand_altq: strlcpy");
56103b3a8eb9SGleb Smirnoff 				pb.qlimit = pa.qlimit;
56113b3a8eb9SGleb Smirnoff 				pb.scheduler = pa.scheduler;
56123b3a8eb9SGleb Smirnoff 				bw.bw_absolute = pa.ifbandwidth;
56133b3a8eb9SGleb Smirnoff 				bw.bw_percent = 0;
56143b3a8eb9SGleb Smirnoff 				if (eval_pfqueue(pf, &pb, &bw, opts))
56153b3a8eb9SGleb Smirnoff 					errs++;
56163b3a8eb9SGleb Smirnoff 				else
56173b3a8eb9SGleb Smirnoff 					if (pfctl_add_altq(pf, &pb))
56183b3a8eb9SGleb Smirnoff 						errs++;
56193b3a8eb9SGleb Smirnoff 			}
56203b3a8eb9SGleb Smirnoff 
56213b3a8eb9SGleb Smirnoff 			LOOP_THROUGH(struct node_queue, queue, nqueues,
56223b3a8eb9SGleb Smirnoff 				n = calloc(1, sizeof(struct node_queue));
56233b3a8eb9SGleb Smirnoff 				if (n == NULL)
56243b3a8eb9SGleb Smirnoff 					err(1, "expand_altq: calloc");
56253b3a8eb9SGleb Smirnoff 				if (pa.scheduler == ALTQT_CBQ ||
5626dc784287SKristof Provost 				    pa.scheduler == ALTQT_HFSC ||
5627dc784287SKristof Provost 				    pa.scheduler == ALTQT_FAIRQ)
56283b3a8eb9SGleb Smirnoff 					if (strlcpy(n->parent, qname,
56293b3a8eb9SGleb Smirnoff 					    sizeof(n->parent)) >=
56303b3a8eb9SGleb Smirnoff 					    sizeof(n->parent))
56313b3a8eb9SGleb Smirnoff 						errx(1, "expand_altq: strlcpy");
56323b3a8eb9SGleb Smirnoff 				if (strlcpy(n->queue, queue->queue,
56333b3a8eb9SGleb Smirnoff 				    sizeof(n->queue)) >= sizeof(n->queue))
56343b3a8eb9SGleb Smirnoff 					errx(1, "expand_altq: strlcpy");
56353b3a8eb9SGleb Smirnoff 				if (strlcpy(n->ifname, interface->ifname,
56363b3a8eb9SGleb Smirnoff 				    sizeof(n->ifname)) >= sizeof(n->ifname))
56373b3a8eb9SGleb Smirnoff 					errx(1, "expand_altq: strlcpy");
56383b3a8eb9SGleb Smirnoff 				n->scheduler = pa.scheduler;
56393b3a8eb9SGleb Smirnoff 				n->next = NULL;
56403b3a8eb9SGleb Smirnoff 				n->tail = n;
56413b3a8eb9SGleb Smirnoff 				if (queues == NULL)
56423b3a8eb9SGleb Smirnoff 					queues = n;
56433b3a8eb9SGleb Smirnoff 				else {
56443b3a8eb9SGleb Smirnoff 					queues->tail->next = n;
56453b3a8eb9SGleb Smirnoff 					queues->tail = n;
56463b3a8eb9SGleb Smirnoff 				}
56473b3a8eb9SGleb Smirnoff 			);
56483b3a8eb9SGleb Smirnoff 		}
56493b3a8eb9SGleb Smirnoff 	);
56503b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_if, interfaces);
56510a70aaf8SLuiz Otavio O Souza 	if (nqueues)
56523b3a8eb9SGleb Smirnoff 		FREE_LIST(struct node_queue, nqueues);
56533b3a8eb9SGleb Smirnoff 
56543b3a8eb9SGleb Smirnoff 	return (errs);
56553b3a8eb9SGleb Smirnoff }
56563b3a8eb9SGleb Smirnoff 
56573b3a8eb9SGleb Smirnoff int
56583b3a8eb9SGleb Smirnoff expand_queue(struct pf_altq *a, struct node_if *interfaces,
56593b3a8eb9SGleb Smirnoff     struct node_queue *nqueues, struct node_queue_bw bwspec,
56603b3a8eb9SGleb Smirnoff     struct node_queue_opt *opts)
56613b3a8eb9SGleb Smirnoff {
56623b3a8eb9SGleb Smirnoff 	struct node_queue	*n, *nq;
56633b3a8eb9SGleb Smirnoff 	struct pf_altq		 pa;
56643b3a8eb9SGleb Smirnoff 	u_int8_t		 found = 0;
56653b3a8eb9SGleb Smirnoff 	u_int8_t		 errs = 0;
56663b3a8eb9SGleb Smirnoff 
56673b3a8eb9SGleb Smirnoff 	if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
56683b3a8eb9SGleb Smirnoff 		FREE_LIST(struct node_queue, nqueues);
56693b3a8eb9SGleb Smirnoff 		return (0);
56703b3a8eb9SGleb Smirnoff 	}
56713b3a8eb9SGleb Smirnoff 
56723b3a8eb9SGleb Smirnoff 	if (queues == NULL) {
56733b3a8eb9SGleb Smirnoff 		yyerror("queue %s has no parent", a->qname);
56743b3a8eb9SGleb Smirnoff 		FREE_LIST(struct node_queue, nqueues);
56753b3a8eb9SGleb Smirnoff 		return (1);
56763b3a8eb9SGleb Smirnoff 	}
56773b3a8eb9SGleb Smirnoff 
56783b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_if, interface, interfaces,
56793b3a8eb9SGleb Smirnoff 		LOOP_THROUGH(struct node_queue, tqueue, queues,
56803b3a8eb9SGleb Smirnoff 			if (!strncmp(a->qname, tqueue->queue, PF_QNAME_SIZE) &&
56813b3a8eb9SGleb Smirnoff 			    (interface->ifname[0] == 0 ||
56823b3a8eb9SGleb Smirnoff 			    (!interface->not && !strncmp(interface->ifname,
56833b3a8eb9SGleb Smirnoff 			    tqueue->ifname, IFNAMSIZ)) ||
56843b3a8eb9SGleb Smirnoff 			    (interface->not && strncmp(interface->ifname,
56853b3a8eb9SGleb Smirnoff 			    tqueue->ifname, IFNAMSIZ)))) {
56863b3a8eb9SGleb Smirnoff 				/* found ourself in queues */
56873b3a8eb9SGleb Smirnoff 				found++;
56883b3a8eb9SGleb Smirnoff 
56893b3a8eb9SGleb Smirnoff 				memcpy(&pa, a, sizeof(struct pf_altq));
56903b3a8eb9SGleb Smirnoff 
56913b3a8eb9SGleb Smirnoff 				if (pa.scheduler != ALTQT_NONE &&
56923b3a8eb9SGleb Smirnoff 				    pa.scheduler != tqueue->scheduler) {
56933b3a8eb9SGleb Smirnoff 					yyerror("exactly one scheduler type "
56943b3a8eb9SGleb Smirnoff 					    "per interface allowed");
56953b3a8eb9SGleb Smirnoff 					return (1);
56963b3a8eb9SGleb Smirnoff 				}
56973b3a8eb9SGleb Smirnoff 				pa.scheduler = tqueue->scheduler;
56983b3a8eb9SGleb Smirnoff 
56993b3a8eb9SGleb Smirnoff 				/* scheduler dependent error checking */
57003b3a8eb9SGleb Smirnoff 				switch (pa.scheduler) {
57013b3a8eb9SGleb Smirnoff 				case ALTQT_PRIQ:
57023b3a8eb9SGleb Smirnoff 					if (nqueues != NULL) {
57033b3a8eb9SGleb Smirnoff 						yyerror("priq queues cannot "
57043b3a8eb9SGleb Smirnoff 						    "have child queues");
57053b3a8eb9SGleb Smirnoff 						return (1);
57063b3a8eb9SGleb Smirnoff 					}
57073b3a8eb9SGleb Smirnoff 					if (bwspec.bw_absolute > 0 ||
57083b3a8eb9SGleb Smirnoff 					    bwspec.bw_percent < 100) {
57093b3a8eb9SGleb Smirnoff 						yyerror("priq doesn't take "
57103b3a8eb9SGleb Smirnoff 						    "bandwidth");
57113b3a8eb9SGleb Smirnoff 						return (1);
57123b3a8eb9SGleb Smirnoff 					}
57133b3a8eb9SGleb Smirnoff 					break;
57143b3a8eb9SGleb Smirnoff 				default:
57153b3a8eb9SGleb Smirnoff 					break;
57163b3a8eb9SGleb Smirnoff 				}
57173b3a8eb9SGleb Smirnoff 
57183b3a8eb9SGleb Smirnoff 				if (strlcpy(pa.ifname, tqueue->ifname,
57193b3a8eb9SGleb Smirnoff 				    sizeof(pa.ifname)) >= sizeof(pa.ifname))
57203b3a8eb9SGleb Smirnoff 					errx(1, "expand_queue: strlcpy");
57213b3a8eb9SGleb Smirnoff 				if (strlcpy(pa.parent, tqueue->parent,
57223b3a8eb9SGleb Smirnoff 				    sizeof(pa.parent)) >= sizeof(pa.parent))
57233b3a8eb9SGleb Smirnoff 					errx(1, "expand_queue: strlcpy");
57243b3a8eb9SGleb Smirnoff 
57253b3a8eb9SGleb Smirnoff 				if (eval_pfqueue(pf, &pa, &bwspec, opts))
57263b3a8eb9SGleb Smirnoff 					errs++;
57273b3a8eb9SGleb Smirnoff 				else
57283b3a8eb9SGleb Smirnoff 					if (pfctl_add_altq(pf, &pa))
57293b3a8eb9SGleb Smirnoff 						errs++;
57303b3a8eb9SGleb Smirnoff 
57313b3a8eb9SGleb Smirnoff 				for (nq = nqueues; nq != NULL; nq = nq->next) {
57323b3a8eb9SGleb Smirnoff 					if (!strcmp(a->qname, nq->queue)) {
57333b3a8eb9SGleb Smirnoff 						yyerror("queue cannot have "
57343b3a8eb9SGleb Smirnoff 						    "itself as child");
57353b3a8eb9SGleb Smirnoff 						errs++;
57363b3a8eb9SGleb Smirnoff 						continue;
57373b3a8eb9SGleb Smirnoff 					}
57383b3a8eb9SGleb Smirnoff 					n = calloc(1,
57393b3a8eb9SGleb Smirnoff 					    sizeof(struct node_queue));
57403b3a8eb9SGleb Smirnoff 					if (n == NULL)
57413b3a8eb9SGleb Smirnoff 						err(1, "expand_queue: calloc");
57423b3a8eb9SGleb Smirnoff 					if (strlcpy(n->parent, a->qname,
57433b3a8eb9SGleb Smirnoff 					    sizeof(n->parent)) >=
57443b3a8eb9SGleb Smirnoff 					    sizeof(n->parent))
57453b3a8eb9SGleb Smirnoff 						errx(1, "expand_queue strlcpy");
57463b3a8eb9SGleb Smirnoff 					if (strlcpy(n->queue, nq->queue,
57473b3a8eb9SGleb Smirnoff 					    sizeof(n->queue)) >=
57483b3a8eb9SGleb Smirnoff 					    sizeof(n->queue))
57493b3a8eb9SGleb Smirnoff 						errx(1, "expand_queue strlcpy");
57503b3a8eb9SGleb Smirnoff 					if (strlcpy(n->ifname, tqueue->ifname,
57513b3a8eb9SGleb Smirnoff 					    sizeof(n->ifname)) >=
57523b3a8eb9SGleb Smirnoff 					    sizeof(n->ifname))
57533b3a8eb9SGleb Smirnoff 						errx(1, "expand_queue strlcpy");
57543b3a8eb9SGleb Smirnoff 					n->scheduler = tqueue->scheduler;
57553b3a8eb9SGleb Smirnoff 					n->next = NULL;
57563b3a8eb9SGleb Smirnoff 					n->tail = n;
57573b3a8eb9SGleb Smirnoff 					if (queues == NULL)
57583b3a8eb9SGleb Smirnoff 						queues = n;
57593b3a8eb9SGleb Smirnoff 					else {
57603b3a8eb9SGleb Smirnoff 						queues->tail->next = n;
57613b3a8eb9SGleb Smirnoff 						queues->tail = n;
57623b3a8eb9SGleb Smirnoff 					}
57633b3a8eb9SGleb Smirnoff 				}
57643b3a8eb9SGleb Smirnoff 				if ((pf->opts & PF_OPT_VERBOSE) && (
57653b3a8eb9SGleb Smirnoff 				    (found == 1 && interface->ifname[0] == 0) ||
57663b3a8eb9SGleb Smirnoff 				    (found > 0 && interface->ifname[0] != 0))) {
57673b3a8eb9SGleb Smirnoff 					print_queue(&pf->paltq->altq, 0,
57683b3a8eb9SGleb Smirnoff 					    &bwspec, interface->ifname[0] != 0,
57693b3a8eb9SGleb Smirnoff 					    opts);
57703b3a8eb9SGleb Smirnoff 					if (nqueues && nqueues->tail) {
57713b3a8eb9SGleb Smirnoff 						printf("{ ");
57723b3a8eb9SGleb Smirnoff 						LOOP_THROUGH(struct node_queue,
57733b3a8eb9SGleb Smirnoff 						    queue, nqueues,
57743b3a8eb9SGleb Smirnoff 							printf("%s ",
57753b3a8eb9SGleb Smirnoff 							    queue->queue);
57763b3a8eb9SGleb Smirnoff 						);
57773b3a8eb9SGleb Smirnoff 						printf("}");
57783b3a8eb9SGleb Smirnoff 					}
57793b3a8eb9SGleb Smirnoff 					printf("\n");
57803b3a8eb9SGleb Smirnoff 				}
57813b3a8eb9SGleb Smirnoff 			}
57823b3a8eb9SGleb Smirnoff 		);
57833b3a8eb9SGleb Smirnoff 	);
57843b3a8eb9SGleb Smirnoff 
57853b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_queue, nqueues);
57863b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_if, interfaces);
57873b3a8eb9SGleb Smirnoff 
57883b3a8eb9SGleb Smirnoff 	if (!found) {
57893b3a8eb9SGleb Smirnoff 		yyerror("queue %s has no parent", a->qname);
57903b3a8eb9SGleb Smirnoff 		errs++;
57913b3a8eb9SGleb Smirnoff 	}
57923b3a8eb9SGleb Smirnoff 
57933b3a8eb9SGleb Smirnoff 	if (errs)
57943b3a8eb9SGleb Smirnoff 		return (1);
57953b3a8eb9SGleb Smirnoff 	else
57963b3a8eb9SGleb Smirnoff 		return (0);
57973b3a8eb9SGleb Smirnoff }
57983b3a8eb9SGleb Smirnoff 
57998a42005dSKristof Provost static int
58008a42005dSKristof Provost pf_af_to_proto(sa_family_t af)
58018a42005dSKristof Provost {
58028a42005dSKristof Provost 	if (af == AF_INET)
58038a42005dSKristof Provost 		return (ETHERTYPE_IP);
58048a42005dSKristof Provost 	if (af == AF_INET6)
58058a42005dSKristof Provost 		return (ETHERTYPE_IPV6);
58068a42005dSKristof Provost 
58078a42005dSKristof Provost 	return (0);
58088a42005dSKristof Provost }
58098a42005dSKristof Provost 
58103b3a8eb9SGleb Smirnoff void
58112b29ceb8SKristof Provost expand_eth_rule(struct pfctl_eth_rule *r,
581287a89d6eSKristof Provost     struct node_if *interfaces, struct node_etherproto *protos,
58138a42005dSKristof Provost     struct node_mac *srcs, struct node_mac *dsts,
58148a8af942SKristof Provost     struct node_host *ipsrcs, struct node_host *ipdsts,
58158a8af942SKristof Provost     const char *bridge_to, const char *anchor_call)
58162b29ceb8SKristof Provost {
58171f61367fSKristof Provost 	char tagname[PF_TAG_NAME_SIZE];
58181f61367fSKristof Provost 	char match_tagname[PF_TAG_NAME_SIZE];
58191f61367fSKristof Provost 	char qname[PF_QNAME_SIZE];
58201f61367fSKristof Provost 
58211f61367fSKristof Provost 	if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname))
58221f61367fSKristof Provost 		errx(1, "expand_eth_rule: tagname");
58231f61367fSKristof Provost 	if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >=
58241f61367fSKristof Provost 	    sizeof(match_tagname))
58251f61367fSKristof Provost 		errx(1, "expand_eth_rule: match_tagname");
58261f61367fSKristof Provost 	if (strlcpy(qname, r->qname, sizeof(qname)) >= sizeof(qname))
58271f61367fSKristof Provost 		errx(1, "expand_eth_rule: qname");
58281f61367fSKristof Provost 
58292b29ceb8SKristof Provost 	LOOP_THROUGH(struct node_if, interface, interfaces,
58302b29ceb8SKristof Provost 	LOOP_THROUGH(struct node_etherproto, proto, protos,
583187a89d6eSKristof Provost 	LOOP_THROUGH(struct node_mac, src, srcs,
583287a89d6eSKristof Provost 	LOOP_THROUGH(struct node_mac, dst, dsts,
58338a42005dSKristof Provost 	LOOP_THROUGH(struct node_host, ipsrc, ipsrcs,
58348a42005dSKristof Provost 	LOOP_THROUGH(struct node_host, ipdst, ipdsts,
58352b29ceb8SKristof Provost 		strlcpy(r->ifname, interface->ifname,
58362b29ceb8SKristof Provost 		    sizeof(r->ifname));
58372b29ceb8SKristof Provost 		r->ifnot = interface->not;
58382b29ceb8SKristof Provost 		r->proto = proto->proto;
58398a42005dSKristof Provost 		if (!r->proto && ipsrc->af)
58408a42005dSKristof Provost 			r->proto = pf_af_to_proto(ipsrc->af);
58418a42005dSKristof Provost 		else if (!r->proto && ipdst->af)
58428a42005dSKristof Provost 			r->proto = pf_af_to_proto(ipdst->af);
584387a89d6eSKristof Provost 		bcopy(src->mac, r->src.addr, ETHER_ADDR_LEN);
5844b590f17aSKristof Provost 		bcopy(src->mask, r->src.mask, ETHER_ADDR_LEN);
584587a89d6eSKristof Provost 		r->src.neg = src->neg;
5846c32cd180SKristof Provost 		r->src.isset = src->isset;
58478a42005dSKristof Provost 		r->ipsrc.addr = ipsrc->addr;
58488a42005dSKristof Provost 		r->ipsrc.neg = ipsrc->not;
58498a42005dSKristof Provost 		r->ipdst.addr = ipdst->addr;
58508a42005dSKristof Provost 		r->ipdst.neg = ipdst->not;
585187a89d6eSKristof Provost 		bcopy(dst->mac, r->dst.addr, ETHER_ADDR_LEN);
5852b590f17aSKristof Provost 		bcopy(dst->mask, r->dst.mask, ETHER_ADDR_LEN);
585387a89d6eSKristof Provost 		r->dst.neg = dst->neg;
5854c32cd180SKristof Provost 		r->dst.isset = dst->isset;
5855c5131afeSKristof Provost 		r->nr = pf->eastack[pf->asd]->match++;
58562b29ceb8SKristof Provost 
58571f61367fSKristof Provost 		if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >=
58581f61367fSKristof Provost 		    sizeof(r->tagname))
58591f61367fSKristof Provost 			errx(1, "expand_eth_rule: r->tagname");
58601f61367fSKristof Provost 		if (strlcpy(r->match_tagname, match_tagname,
58611f61367fSKristof Provost 		    sizeof(r->match_tagname)) >= sizeof(r->match_tagname))
58621f61367fSKristof Provost 			errx(1, "expand_eth_rule: r->match_tagname");
58631f61367fSKristof Provost 		if (strlcpy(r->qname, qname, sizeof(r->qname)) >= sizeof(r->qname))
58641f61367fSKristof Provost 			errx(1, "expand_eth_rule: r->qname");
58651f61367fSKristof Provost 
58668a8af942SKristof Provost 		if (bridge_to)
58678a8af942SKristof Provost 			strlcpy(r->bridge_to, bridge_to, sizeof(r->bridge_to));
58688a8af942SKristof Provost 
5869c5131afeSKristof Provost 		pfctl_append_eth_rule(pf, r, anchor_call);
58708a42005dSKristof Provost 	))))));
58712b29ceb8SKristof Provost 
58722b29ceb8SKristof Provost 	FREE_LIST(struct node_if, interfaces);
58732b29ceb8SKristof Provost 	FREE_LIST(struct node_etherproto, protos);
587487a89d6eSKristof Provost 	FREE_LIST(struct node_mac, srcs);
587587a89d6eSKristof Provost 	FREE_LIST(struct node_mac, dsts);
58768a42005dSKristof Provost 	FREE_LIST(struct node_host, ipsrcs);
58778a42005dSKristof Provost 	FREE_LIST(struct node_host, ipdsts);
58782b29ceb8SKristof Provost }
58792b29ceb8SKristof Provost 
58802b29ceb8SKristof Provost void
5881e9eb0941SKristof Provost expand_rule(struct pfctl_rule *r,
58823b3a8eb9SGleb Smirnoff     struct node_if *interfaces, struct node_host *rpool_hosts,
58833b3a8eb9SGleb Smirnoff     struct node_proto *protos, struct node_os *src_oses,
58843b3a8eb9SGleb Smirnoff     struct node_host *src_hosts, struct node_port *src_ports,
58853b3a8eb9SGleb Smirnoff     struct node_host *dst_hosts, struct node_port *dst_ports,
58863b3a8eb9SGleb Smirnoff     struct node_uid *uids, struct node_gid *gids, struct node_icmp *icmp_types,
58873b3a8eb9SGleb Smirnoff     const char *anchor_call)
58883b3a8eb9SGleb Smirnoff {
58893b3a8eb9SGleb Smirnoff 	sa_family_t		 af = r->af;
58903b3a8eb9SGleb Smirnoff 	int			 added = 0, error = 0;
58913b3a8eb9SGleb Smirnoff 	char			 ifname[IF_NAMESIZE];
58926fcc8e04SKristof Provost 	char			 label[PF_RULE_MAX_LABEL_COUNT][PF_RULE_LABEL_SIZE];
58933b3a8eb9SGleb Smirnoff 	char			 tagname[PF_TAG_NAME_SIZE];
58943b3a8eb9SGleb Smirnoff 	char			 match_tagname[PF_TAG_NAME_SIZE];
58953b3a8eb9SGleb Smirnoff 	struct pf_pooladdr	*pa;
58963b3a8eb9SGleb Smirnoff 	struct node_host	*h;
58973b3a8eb9SGleb Smirnoff 	u_int8_t		 flags, flagset, keep_state;
58983b3a8eb9SGleb Smirnoff 
58996fcc8e04SKristof Provost 	memcpy(label, r->label, sizeof(r->label));
59006fcc8e04SKristof Provost 	assert(sizeof(r->label) == sizeof(label));
59013b3a8eb9SGleb Smirnoff 	if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname))
59023b3a8eb9SGleb Smirnoff 		errx(1, "expand_rule: strlcpy");
59033b3a8eb9SGleb Smirnoff 	if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >=
59043b3a8eb9SGleb Smirnoff 	    sizeof(match_tagname))
59053b3a8eb9SGleb Smirnoff 		errx(1, "expand_rule: strlcpy");
59063b3a8eb9SGleb Smirnoff 	flags = r->flags;
59073b3a8eb9SGleb Smirnoff 	flagset = r->flagset;
59083b3a8eb9SGleb Smirnoff 	keep_state = r->keep_state;
59093b3a8eb9SGleb Smirnoff 
59103b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_if, interface, interfaces,
59113b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_proto, proto, protos,
59123b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_icmp, icmp_type, icmp_types,
59133b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_host, src_host, src_hosts,
59143b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_port, src_port, src_ports,
59153b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_os, src_os, src_oses,
59163b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_host, dst_host, dst_hosts,
59173b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_port, dst_port, dst_ports,
59183b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_uid, uid, uids,
59193b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_gid, gid, gids,
59203b3a8eb9SGleb Smirnoff 
59213b3a8eb9SGleb Smirnoff 		r->af = af;
59223b3a8eb9SGleb Smirnoff 		/* for link-local IPv6 address, interface must match up */
59233b3a8eb9SGleb Smirnoff 		if ((r->af && src_host->af && r->af != src_host->af) ||
59243b3a8eb9SGleb Smirnoff 		    (r->af && dst_host->af && r->af != dst_host->af) ||
59253b3a8eb9SGleb Smirnoff 		    (src_host->af && dst_host->af &&
59263b3a8eb9SGleb Smirnoff 		    src_host->af != dst_host->af) ||
59273b3a8eb9SGleb Smirnoff 		    (src_host->ifindex && dst_host->ifindex &&
59283b3a8eb9SGleb Smirnoff 		    src_host->ifindex != dst_host->ifindex) ||
59293b3a8eb9SGleb Smirnoff 		    (src_host->ifindex && *interface->ifname &&
59303b3a8eb9SGleb Smirnoff 		    src_host->ifindex != if_nametoindex(interface->ifname)) ||
59313b3a8eb9SGleb Smirnoff 		    (dst_host->ifindex && *interface->ifname &&
59323b3a8eb9SGleb Smirnoff 		    dst_host->ifindex != if_nametoindex(interface->ifname)))
59333b3a8eb9SGleb Smirnoff 			continue;
59343b3a8eb9SGleb Smirnoff 		if (!r->af && src_host->af)
59353b3a8eb9SGleb Smirnoff 			r->af = src_host->af;
59363b3a8eb9SGleb Smirnoff 		else if (!r->af && dst_host->af)
59373b3a8eb9SGleb Smirnoff 			r->af = dst_host->af;
59383b3a8eb9SGleb Smirnoff 
59393b3a8eb9SGleb Smirnoff 		if (*interface->ifname)
59403b3a8eb9SGleb Smirnoff 			strlcpy(r->ifname, interface->ifname,
59413b3a8eb9SGleb Smirnoff 			    sizeof(r->ifname));
59423b3a8eb9SGleb Smirnoff 		else if (if_indextoname(src_host->ifindex, ifname))
59433b3a8eb9SGleb Smirnoff 			strlcpy(r->ifname, ifname, sizeof(r->ifname));
59443b3a8eb9SGleb Smirnoff 		else if (if_indextoname(dst_host->ifindex, ifname))
59453b3a8eb9SGleb Smirnoff 			strlcpy(r->ifname, ifname, sizeof(r->ifname));
59463b3a8eb9SGleb Smirnoff 		else
59473b3a8eb9SGleb Smirnoff 			memset(r->ifname, '\0', sizeof(r->ifname));
59483b3a8eb9SGleb Smirnoff 
59496fcc8e04SKristof Provost 		memcpy(r->label, label, sizeof(r->label));
59503b3a8eb9SGleb Smirnoff 		if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >=
59513b3a8eb9SGleb Smirnoff 		    sizeof(r->tagname))
59523b3a8eb9SGleb Smirnoff 			errx(1, "expand_rule: strlcpy");
59533b3a8eb9SGleb Smirnoff 		if (strlcpy(r->match_tagname, match_tagname,
59543b3a8eb9SGleb Smirnoff 		    sizeof(r->match_tagname)) >= sizeof(r->match_tagname))
59553b3a8eb9SGleb Smirnoff 			errx(1, "expand_rule: strlcpy");
59563b3a8eb9SGleb Smirnoff 
59573b3a8eb9SGleb Smirnoff 		error += check_netmask(src_host, r->af);
59583b3a8eb9SGleb Smirnoff 		error += check_netmask(dst_host, r->af);
59593b3a8eb9SGleb Smirnoff 
59603b3a8eb9SGleb Smirnoff 		r->ifnot = interface->not;
59613b3a8eb9SGleb Smirnoff 		r->proto = proto->proto;
59623b3a8eb9SGleb Smirnoff 		r->src.addr = src_host->addr;
59633b3a8eb9SGleb Smirnoff 		r->src.neg = src_host->not;
59643b3a8eb9SGleb Smirnoff 		r->src.port[0] = src_port->port[0];
59653b3a8eb9SGleb Smirnoff 		r->src.port[1] = src_port->port[1];
59663b3a8eb9SGleb Smirnoff 		r->src.port_op = src_port->op;
59673b3a8eb9SGleb Smirnoff 		r->dst.addr = dst_host->addr;
59683b3a8eb9SGleb Smirnoff 		r->dst.neg = dst_host->not;
59693b3a8eb9SGleb Smirnoff 		r->dst.port[0] = dst_port->port[0];
59703b3a8eb9SGleb Smirnoff 		r->dst.port[1] = dst_port->port[1];
59713b3a8eb9SGleb Smirnoff 		r->dst.port_op = dst_port->op;
59723b3a8eb9SGleb Smirnoff 		r->uid.op = uid->op;
59733b3a8eb9SGleb Smirnoff 		r->uid.uid[0] = uid->uid[0];
59743b3a8eb9SGleb Smirnoff 		r->uid.uid[1] = uid->uid[1];
59753b3a8eb9SGleb Smirnoff 		r->gid.op = gid->op;
59763b3a8eb9SGleb Smirnoff 		r->gid.gid[0] = gid->gid[0];
59773b3a8eb9SGleb Smirnoff 		r->gid.gid[1] = gid->gid[1];
59783b3a8eb9SGleb Smirnoff 		r->type = icmp_type->type;
59793b3a8eb9SGleb Smirnoff 		r->code = icmp_type->code;
59803b3a8eb9SGleb Smirnoff 
59813b3a8eb9SGleb Smirnoff 		if ((keep_state == PF_STATE_MODULATE ||
59823b3a8eb9SGleb Smirnoff 		    keep_state == PF_STATE_SYNPROXY) &&
59833b3a8eb9SGleb Smirnoff 		    r->proto && r->proto != IPPROTO_TCP)
59843b3a8eb9SGleb Smirnoff 			r->keep_state = PF_STATE_NORMAL;
59853b3a8eb9SGleb Smirnoff 		else
59863b3a8eb9SGleb Smirnoff 			r->keep_state = keep_state;
59873b3a8eb9SGleb Smirnoff 
59883b3a8eb9SGleb Smirnoff 		if (r->proto && r->proto != IPPROTO_TCP) {
59893b3a8eb9SGleb Smirnoff 			r->flags = 0;
59903b3a8eb9SGleb Smirnoff 			r->flagset = 0;
59913b3a8eb9SGleb Smirnoff 		} else {
59923b3a8eb9SGleb Smirnoff 			r->flags = flags;
59933b3a8eb9SGleb Smirnoff 			r->flagset = flagset;
59943b3a8eb9SGleb Smirnoff 		}
59953b3a8eb9SGleb Smirnoff 		if (icmp_type->proto && r->proto != icmp_type->proto) {
59963b3a8eb9SGleb Smirnoff 			yyerror("icmp-type mismatch");
59973b3a8eb9SGleb Smirnoff 			error++;
59983b3a8eb9SGleb Smirnoff 		}
59993b3a8eb9SGleb Smirnoff 
60003b3a8eb9SGleb Smirnoff 		if (src_os && src_os->os) {
60013b3a8eb9SGleb Smirnoff 			r->os_fingerprint = pfctl_get_fingerprint(src_os->os);
60023b3a8eb9SGleb Smirnoff 			if ((pf->opts & PF_OPT_VERBOSE2) &&
60033b3a8eb9SGleb Smirnoff 			    r->os_fingerprint == PF_OSFP_NOMATCH)
60043b3a8eb9SGleb Smirnoff 				fprintf(stderr,
60053b3a8eb9SGleb Smirnoff 				    "warning: unknown '%s' OS fingerprint\n",
60063b3a8eb9SGleb Smirnoff 				    src_os->os);
60073b3a8eb9SGleb Smirnoff 		} else {
60083b3a8eb9SGleb Smirnoff 			r->os_fingerprint = PF_OSFP_ANY;
60093b3a8eb9SGleb Smirnoff 		}
60103b3a8eb9SGleb Smirnoff 
60113b3a8eb9SGleb Smirnoff 		TAILQ_INIT(&r->rpool.list);
60123b3a8eb9SGleb Smirnoff 		for (h = rpool_hosts; h != NULL; h = h->next) {
60133b3a8eb9SGleb Smirnoff 			pa = calloc(1, sizeof(struct pf_pooladdr));
60143b3a8eb9SGleb Smirnoff 			if (pa == NULL)
60153b3a8eb9SGleb Smirnoff 				err(1, "expand_rule: calloc");
60163b3a8eb9SGleb Smirnoff 			pa->addr = h->addr;
60173b3a8eb9SGleb Smirnoff 			if (h->ifname != NULL) {
60183b3a8eb9SGleb Smirnoff 				if (strlcpy(pa->ifname, h->ifname,
60193b3a8eb9SGleb Smirnoff 				    sizeof(pa->ifname)) >=
60203b3a8eb9SGleb Smirnoff 				    sizeof(pa->ifname))
60213b3a8eb9SGleb Smirnoff 					errx(1, "expand_rule: strlcpy");
60223b3a8eb9SGleb Smirnoff 			} else
60233b3a8eb9SGleb Smirnoff 				pa->ifname[0] = 0;
60243b3a8eb9SGleb Smirnoff 			TAILQ_INSERT_TAIL(&r->rpool.list, pa, entries);
60253b3a8eb9SGleb Smirnoff 		}
60263b3a8eb9SGleb Smirnoff 
60273b3a8eb9SGleb Smirnoff 		if (rule_consistent(r, anchor_call[0]) < 0 || error)
60283b3a8eb9SGleb Smirnoff 			yyerror("skipping rule due to errors");
60293b3a8eb9SGleb Smirnoff 		else {
60303b3a8eb9SGleb Smirnoff 			r->nr = pf->astack[pf->asd]->match++;
60310d71f9f3SKristof Provost 			pfctl_append_rule(pf, r, anchor_call);
60323b3a8eb9SGleb Smirnoff 			added++;
60333b3a8eb9SGleb Smirnoff 		}
60343b3a8eb9SGleb Smirnoff 
60353b3a8eb9SGleb Smirnoff 	))))))))));
60363b3a8eb9SGleb Smirnoff 
60373b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_if, interfaces);
60383b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_proto, protos);
60393b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_host, src_hosts);
60403b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_port, src_ports);
60413b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_os, src_oses);
60423b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_host, dst_hosts);
60433b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_port, dst_ports);
60443b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_uid, uids);
60453b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_gid, gids);
60463b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_icmp, icmp_types);
60473b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_host, rpool_hosts);
60483b3a8eb9SGleb Smirnoff 
60493b3a8eb9SGleb Smirnoff 	if (!added)
60503b3a8eb9SGleb Smirnoff 		yyerror("rule expands to no valid combination");
60513b3a8eb9SGleb Smirnoff }
60523b3a8eb9SGleb Smirnoff 
60533b3a8eb9SGleb Smirnoff int
60543b3a8eb9SGleb Smirnoff expand_skip_interface(struct node_if *interfaces)
60553b3a8eb9SGleb Smirnoff {
60563b3a8eb9SGleb Smirnoff 	int	errs = 0;
60573b3a8eb9SGleb Smirnoff 
60583b3a8eb9SGleb Smirnoff 	if (!interfaces || (!interfaces->next && !interfaces->not &&
60593b3a8eb9SGleb Smirnoff 	    !strcmp(interfaces->ifname, "none"))) {
60603b3a8eb9SGleb Smirnoff 		if (pf->opts & PF_OPT_VERBOSE)
60613b3a8eb9SGleb Smirnoff 			printf("set skip on none\n");
60623b3a8eb9SGleb Smirnoff 		errs = pfctl_set_interface_flags(pf, "", PFI_IFLAG_SKIP, 0);
60633b3a8eb9SGleb Smirnoff 		return (errs);
60643b3a8eb9SGleb Smirnoff 	}
60653b3a8eb9SGleb Smirnoff 
60663b3a8eb9SGleb Smirnoff 	if (pf->opts & PF_OPT_VERBOSE)
60673b3a8eb9SGleb Smirnoff 		printf("set skip on {");
60683b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_if, interface, interfaces,
60693b3a8eb9SGleb Smirnoff 		if (pf->opts & PF_OPT_VERBOSE)
60703b3a8eb9SGleb Smirnoff 			printf(" %s", interface->ifname);
60713b3a8eb9SGleb Smirnoff 		if (interface->not) {
60723b3a8eb9SGleb Smirnoff 			yyerror("skip on ! <interface> is not supported");
60733b3a8eb9SGleb Smirnoff 			errs++;
60743b3a8eb9SGleb Smirnoff 		} else
60753b3a8eb9SGleb Smirnoff 			errs += pfctl_set_interface_flags(pf,
60763b3a8eb9SGleb Smirnoff 			    interface->ifname, PFI_IFLAG_SKIP, 1);
60773b3a8eb9SGleb Smirnoff 	);
60783b3a8eb9SGleb Smirnoff 	if (pf->opts & PF_OPT_VERBOSE)
60793b3a8eb9SGleb Smirnoff 		printf(" }\n");
60803b3a8eb9SGleb Smirnoff 
60813b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_if, interfaces);
60823b3a8eb9SGleb Smirnoff 
60833b3a8eb9SGleb Smirnoff 	if (errs)
60843b3a8eb9SGleb Smirnoff 		return (1);
60853b3a8eb9SGleb Smirnoff 	else
60863b3a8eb9SGleb Smirnoff 		return (0);
60873b3a8eb9SGleb Smirnoff }
60883b3a8eb9SGleb Smirnoff 
60893b3a8eb9SGleb Smirnoff #undef FREE_LIST
60903b3a8eb9SGleb Smirnoff #undef LOOP_THROUGH
60913b3a8eb9SGleb Smirnoff 
60923b3a8eb9SGleb Smirnoff int
60933b3a8eb9SGleb Smirnoff check_rulestate(int desired_state)
60943b3a8eb9SGleb Smirnoff {
60953b3a8eb9SGleb Smirnoff 	if (require_order && (rulestate > desired_state)) {
60962b29ceb8SKristof Provost 		yyerror("Rules must be in order: options, ethernet, "
60972b29ceb8SKristof Provost 		    "normalization, queueing, translation, filtering");
60983b3a8eb9SGleb Smirnoff 		return (1);
60993b3a8eb9SGleb Smirnoff 	}
61003b3a8eb9SGleb Smirnoff 	rulestate = desired_state;
61013b3a8eb9SGleb Smirnoff 	return (0);
61023b3a8eb9SGleb Smirnoff }
61033b3a8eb9SGleb Smirnoff 
61043b3a8eb9SGleb Smirnoff int
61053b3a8eb9SGleb Smirnoff kw_cmp(const void *k, const void *e)
61063b3a8eb9SGleb Smirnoff {
61073b3a8eb9SGleb Smirnoff 	return (strcmp(k, ((const struct keywords *)e)->k_name));
61083b3a8eb9SGleb Smirnoff }
61093b3a8eb9SGleb Smirnoff 
61103b3a8eb9SGleb Smirnoff int
61113b3a8eb9SGleb Smirnoff lookup(char *s)
61123b3a8eb9SGleb Smirnoff {
61133b3a8eb9SGleb Smirnoff 	/* this has to be sorted always */
61143b3a8eb9SGleb Smirnoff 	static const struct keywords keywords[] = {
61153b3a8eb9SGleb Smirnoff 		{ "all",		ALL},
61163b3a8eb9SGleb Smirnoff 		{ "allow-opts",		ALLOWOPTS},
61173b3a8eb9SGleb Smirnoff 		{ "altq",		ALTQ},
61183b3a8eb9SGleb Smirnoff 		{ "anchor",		ANCHOR},
61193b3a8eb9SGleb Smirnoff 		{ "antispoof",		ANTISPOOF},
61203b3a8eb9SGleb Smirnoff 		{ "any",		ANY},
61213b3a8eb9SGleb Smirnoff 		{ "bandwidth",		BANDWIDTH},
61223b3a8eb9SGleb Smirnoff 		{ "binat",		BINAT},
61233b3a8eb9SGleb Smirnoff 		{ "binat-anchor",	BINATANCHOR},
61243b3a8eb9SGleb Smirnoff 		{ "bitmask",		BITMASK},
61253b3a8eb9SGleb Smirnoff 		{ "block",		BLOCK},
61263b3a8eb9SGleb Smirnoff 		{ "block-policy",	BLOCKPOLICY},
61278a8af942SKristof Provost 		{ "bridge-to",		BRIDGE_TO},
6128a5b789f6SErmal Luçi 		{ "buckets",		BUCKETS},
61293b3a8eb9SGleb Smirnoff 		{ "cbq",		CBQ},
61303b3a8eb9SGleb Smirnoff 		{ "code",		CODE},
61310a70aaf8SLuiz Otavio O Souza 		{ "codelq",		CODEL},
61323b3a8eb9SGleb Smirnoff 		{ "debug",		DEBUG},
61333b3a8eb9SGleb Smirnoff 		{ "divert-reply",	DIVERTREPLY},
61343b3a8eb9SGleb Smirnoff 		{ "divert-to",		DIVERTTO},
613563b3c1c7SKristof Provost 		{ "dnpipe",		DNPIPE},
613663b3c1c7SKristof Provost 		{ "dnqueue",		DNQUEUE},
61373b3a8eb9SGleb Smirnoff 		{ "drop",		DROP},
61383b3a8eb9SGleb Smirnoff 		{ "dup-to",		DUPTO},
61392b29ceb8SKristof Provost 		{ "ether",		ETHER},
6140150182e3SKristof Provost 		{ "fail-policy",	FAILPOLICY},
6141a5b789f6SErmal Luçi 		{ "fairq",		FAIRQ},
61423b3a8eb9SGleb Smirnoff 		{ "fastroute",		FASTROUTE},
61433b3a8eb9SGleb Smirnoff 		{ "file",		FILENAME},
61443b3a8eb9SGleb Smirnoff 		{ "fingerprints",	FINGERPRINTS},
61453b3a8eb9SGleb Smirnoff 		{ "flags",		FLAGS},
61463b3a8eb9SGleb Smirnoff 		{ "floating",		FLOATING},
61473b3a8eb9SGleb Smirnoff 		{ "flush",		FLUSH},
61483b3a8eb9SGleb Smirnoff 		{ "for",		FOR},
61493b3a8eb9SGleb Smirnoff 		{ "fragment",		FRAGMENT},
61503b3a8eb9SGleb Smirnoff 		{ "from",		FROM},
61513b3a8eb9SGleb Smirnoff 		{ "global",		GLOBAL},
61523b3a8eb9SGleb Smirnoff 		{ "group",		GROUP},
61533b3a8eb9SGleb Smirnoff 		{ "hfsc",		HFSC},
6154a5b789f6SErmal Luçi 		{ "hogs",		HOGS},
61553b3a8eb9SGleb Smirnoff 		{ "hostid",		HOSTID},
61563b3a8eb9SGleb Smirnoff 		{ "icmp-type",		ICMPTYPE},
61573b3a8eb9SGleb Smirnoff 		{ "icmp6-type",		ICMP6TYPE},
61583b3a8eb9SGleb Smirnoff 		{ "if-bound",		IFBOUND},
61593b3a8eb9SGleb Smirnoff 		{ "in",			IN},
61603b3a8eb9SGleb Smirnoff 		{ "include",		INCLUDE},
61613b3a8eb9SGleb Smirnoff 		{ "inet",		INET},
61623b3a8eb9SGleb Smirnoff 		{ "inet6",		INET6},
61630a70aaf8SLuiz Otavio O Souza 		{ "interval",		INTERVAL},
61643b3a8eb9SGleb Smirnoff 		{ "keep",		KEEP},
616542ec75f8SKristof Provost 		{ "keepcounters",	KEEPCOUNTERS},
61668a42005dSKristof Provost 		{ "l3",			L3},
61673b3a8eb9SGleb Smirnoff 		{ "label",		LABEL},
61683b3a8eb9SGleb Smirnoff 		{ "limit",		LIMIT},
61693b3a8eb9SGleb Smirnoff 		{ "linkshare",		LINKSHARE},
61703b3a8eb9SGleb Smirnoff 		{ "load",		LOAD},
61713b3a8eb9SGleb Smirnoff 		{ "log",		LOG},
61723b3a8eb9SGleb Smirnoff 		{ "loginterface",	LOGINTERFACE},
61732aa21096SKurosawa Takahiro 		{ "map-e-portset",	MAPEPORTSET},
6174ef950daaSKristof Provost 		{ "match",		MATCH},
61753b3a8eb9SGleb Smirnoff 		{ "max",		MAXIMUM},
61763b3a8eb9SGleb Smirnoff 		{ "max-mss",		MAXMSS},
61773b3a8eb9SGleb Smirnoff 		{ "max-src-conn",	MAXSRCCONN},
61783b3a8eb9SGleb Smirnoff 		{ "max-src-conn-rate",	MAXSRCCONNRATE},
61793b3a8eb9SGleb Smirnoff 		{ "max-src-nodes",	MAXSRCNODES},
61803b3a8eb9SGleb Smirnoff 		{ "max-src-states",	MAXSRCSTATES},
61813b3a8eb9SGleb Smirnoff 		{ "min-ttl",		MINTTL},
61823b3a8eb9SGleb Smirnoff 		{ "modulate",		MODULATE},
61833b3a8eb9SGleb Smirnoff 		{ "nat",		NAT},
61843b3a8eb9SGleb Smirnoff 		{ "nat-anchor",		NATANCHOR},
61853b3a8eb9SGleb Smirnoff 		{ "no",			NO},
61863b3a8eb9SGleb Smirnoff 		{ "no-df",		NODF},
61873b3a8eb9SGleb Smirnoff 		{ "no-route",		NOROUTE},
61883b3a8eb9SGleb Smirnoff 		{ "no-sync",		NOSYNC},
61893b3a8eb9SGleb Smirnoff 		{ "on",			ON},
61903b3a8eb9SGleb Smirnoff 		{ "optimization",	OPTIMIZATION},
61913b3a8eb9SGleb Smirnoff 		{ "os",			OS},
61923b3a8eb9SGleb Smirnoff 		{ "out",		OUT},
61933b3a8eb9SGleb Smirnoff 		{ "overload",		OVERLOAD},
61943b3a8eb9SGleb Smirnoff 		{ "pass",		PASS},
61953b3a8eb9SGleb Smirnoff 		{ "port",		PORT},
61963e248e0fSKristof Provost 		{ "prio",		PRIO},
61973b3a8eb9SGleb Smirnoff 		{ "priority",		PRIORITY},
61983b3a8eb9SGleb Smirnoff 		{ "priq",		PRIQ},
61993b3a8eb9SGleb Smirnoff 		{ "probability",	PROBABILITY},
62003b3a8eb9SGleb Smirnoff 		{ "proto",		PROTO},
62013b3a8eb9SGleb Smirnoff 		{ "qlimit",		QLIMIT},
62023b3a8eb9SGleb Smirnoff 		{ "queue",		QUEUE},
62033b3a8eb9SGleb Smirnoff 		{ "quick",		QUICK},
62043b3a8eb9SGleb Smirnoff 		{ "random",		RANDOM},
62053b3a8eb9SGleb Smirnoff 		{ "random-id",		RANDOMID},
62063b3a8eb9SGleb Smirnoff 		{ "rdr",		RDR},
62073b3a8eb9SGleb Smirnoff 		{ "rdr-anchor",		RDRANCHOR},
62083b3a8eb9SGleb Smirnoff 		{ "realtime",		REALTIME},
62093b3a8eb9SGleb Smirnoff 		{ "reassemble",		REASSEMBLE},
62103b3a8eb9SGleb Smirnoff 		{ "reply-to",		REPLYTO},
62113b3a8eb9SGleb Smirnoff 		{ "require-order",	REQUIREORDER},
62123b3a8eb9SGleb Smirnoff 		{ "return",		RETURN},
62133b3a8eb9SGleb Smirnoff 		{ "return-icmp",	RETURNICMP},
62143b3a8eb9SGleb Smirnoff 		{ "return-icmp6",	RETURNICMP6},
62153b3a8eb9SGleb Smirnoff 		{ "return-rst",		RETURNRST},
621676c5eeccSKristof Provost 		{ "ridentifier",	RIDENTIFIER},
62173b3a8eb9SGleb Smirnoff 		{ "round-robin",	ROUNDROBIN},
62183b3a8eb9SGleb Smirnoff 		{ "route",		ROUTE},
62193b3a8eb9SGleb Smirnoff 		{ "route-to",		ROUTETO},
62203b3a8eb9SGleb Smirnoff 		{ "rtable",		RTABLE},
62213b3a8eb9SGleb Smirnoff 		{ "rule",		RULE},
62223b3a8eb9SGleb Smirnoff 		{ "ruleset-optimization",	RULESET_OPTIMIZATION},
62233b3a8eb9SGleb Smirnoff 		{ "scrub",		SCRUB},
62243b3a8eb9SGleb Smirnoff 		{ "set",		SET},
62253b3a8eb9SGleb Smirnoff 		{ "set-tos",		SETTOS},
62263b3a8eb9SGleb Smirnoff 		{ "skip",		SKIP},
62273b3a8eb9SGleb Smirnoff 		{ "sloppy",		SLOPPY},
62283b3a8eb9SGleb Smirnoff 		{ "source-hash",	SOURCEHASH},
62293b3a8eb9SGleb Smirnoff 		{ "source-track",	SOURCETRACK},
62303b3a8eb9SGleb Smirnoff 		{ "state",		STATE},
62313b3a8eb9SGleb Smirnoff 		{ "state-defaults",	STATEDEFAULTS},
62323b3a8eb9SGleb Smirnoff 		{ "state-policy",	STATEPOLICY},
62333b3a8eb9SGleb Smirnoff 		{ "static-port",	STATICPORT},
62343b3a8eb9SGleb Smirnoff 		{ "sticky-address",	STICKYADDRESS},
6235c69121c4SKristof Provost 		{ "syncookies",         SYNCOOKIES},
62363b3a8eb9SGleb Smirnoff 		{ "synproxy",		SYNPROXY},
62373b3a8eb9SGleb Smirnoff 		{ "table",		TABLE},
62383b3a8eb9SGleb Smirnoff 		{ "tag",		TAG},
62393b3a8eb9SGleb Smirnoff 		{ "tagged",		TAGGED},
62400a70aaf8SLuiz Otavio O Souza 		{ "target",		TARGET},
62413b3a8eb9SGleb Smirnoff 		{ "tbrsize",		TBRSIZE},
62423b3a8eb9SGleb Smirnoff 		{ "timeout",		TIMEOUT},
62433b3a8eb9SGleb Smirnoff 		{ "to",			TO},
62443b3a8eb9SGleb Smirnoff 		{ "tos",		TOS},
62453b3a8eb9SGleb Smirnoff 		{ "ttl",		TTL},
62463b3a8eb9SGleb Smirnoff 		{ "upperlimit",		UPPERLIMIT},
62473b3a8eb9SGleb Smirnoff 		{ "urpf-failed",	URPFFAILED},
62483b3a8eb9SGleb Smirnoff 		{ "user",		USER},
62493b3a8eb9SGleb Smirnoff 	};
62503b3a8eb9SGleb Smirnoff 	const struct keywords	*p;
62513b3a8eb9SGleb Smirnoff 
62523b3a8eb9SGleb Smirnoff 	p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
62533b3a8eb9SGleb Smirnoff 	    sizeof(keywords[0]), kw_cmp);
62543b3a8eb9SGleb Smirnoff 
62553b3a8eb9SGleb Smirnoff 	if (p) {
62563b3a8eb9SGleb Smirnoff 		if (debug > 1)
62573b3a8eb9SGleb Smirnoff 			fprintf(stderr, "%s: %d\n", s, p->k_val);
62583b3a8eb9SGleb Smirnoff 		return (p->k_val);
62593b3a8eb9SGleb Smirnoff 	} else {
62603b3a8eb9SGleb Smirnoff 		if (debug > 1)
62613b3a8eb9SGleb Smirnoff 			fprintf(stderr, "string: %s\n", s);
62623b3a8eb9SGleb Smirnoff 		return (STRING);
62633b3a8eb9SGleb Smirnoff 	}
62643b3a8eb9SGleb Smirnoff }
62653b3a8eb9SGleb Smirnoff 
62663b3a8eb9SGleb Smirnoff #define MAXPUSHBACK	128
62673b3a8eb9SGleb Smirnoff 
626813cfafabSKristof Provost static char	*parsebuf;
626913cfafabSKristof Provost static int	 parseindex;
627013cfafabSKristof Provost static char	 pushback_buffer[MAXPUSHBACK];
627113cfafabSKristof Provost static int	 pushback_index = 0;
62723b3a8eb9SGleb Smirnoff 
62733b3a8eb9SGleb Smirnoff int
62743b3a8eb9SGleb Smirnoff lgetc(int quotec)
62753b3a8eb9SGleb Smirnoff {
62763b3a8eb9SGleb Smirnoff 	int		c, next;
62773b3a8eb9SGleb Smirnoff 
62783b3a8eb9SGleb Smirnoff 	if (parsebuf) {
62793b3a8eb9SGleb Smirnoff 		/* Read character from the parsebuffer instead of input. */
62803b3a8eb9SGleb Smirnoff 		if (parseindex >= 0) {
62813b3a8eb9SGleb Smirnoff 			c = parsebuf[parseindex++];
62823b3a8eb9SGleb Smirnoff 			if (c != '\0')
62833b3a8eb9SGleb Smirnoff 				return (c);
62843b3a8eb9SGleb Smirnoff 			parsebuf = NULL;
62853b3a8eb9SGleb Smirnoff 		} else
62863b3a8eb9SGleb Smirnoff 			parseindex++;
62873b3a8eb9SGleb Smirnoff 	}
62883b3a8eb9SGleb Smirnoff 
62893b3a8eb9SGleb Smirnoff 	if (pushback_index)
62903b3a8eb9SGleb Smirnoff 		return (pushback_buffer[--pushback_index]);
62913b3a8eb9SGleb Smirnoff 
62923b3a8eb9SGleb Smirnoff 	if (quotec) {
62933b3a8eb9SGleb Smirnoff 		if ((c = getc(file->stream)) == EOF) {
62943b3a8eb9SGleb Smirnoff 			yyerror("reached end of file while parsing quoted string");
62953b3a8eb9SGleb Smirnoff 			if (popfile() == EOF)
62963b3a8eb9SGleb Smirnoff 				return (EOF);
62973b3a8eb9SGleb Smirnoff 			return (quotec);
62983b3a8eb9SGleb Smirnoff 		}
62993b3a8eb9SGleb Smirnoff 		return (c);
63003b3a8eb9SGleb Smirnoff 	}
63013b3a8eb9SGleb Smirnoff 
63023b3a8eb9SGleb Smirnoff 	while ((c = getc(file->stream)) == '\\') {
63033b3a8eb9SGleb Smirnoff 		next = getc(file->stream);
63043b3a8eb9SGleb Smirnoff 		if (next != '\n') {
63053b3a8eb9SGleb Smirnoff 			c = next;
63063b3a8eb9SGleb Smirnoff 			break;
63073b3a8eb9SGleb Smirnoff 		}
63083b3a8eb9SGleb Smirnoff 		yylval.lineno = file->lineno;
63093b3a8eb9SGleb Smirnoff 		file->lineno++;
63103b3a8eb9SGleb Smirnoff 	}
63113b3a8eb9SGleb Smirnoff 
63123b3a8eb9SGleb Smirnoff 	while (c == EOF) {
63133b3a8eb9SGleb Smirnoff 		if (popfile() == EOF)
63143b3a8eb9SGleb Smirnoff 			return (EOF);
63153b3a8eb9SGleb Smirnoff 		c = getc(file->stream);
63163b3a8eb9SGleb Smirnoff 	}
63173b3a8eb9SGleb Smirnoff 	return (c);
63183b3a8eb9SGleb Smirnoff }
63193b3a8eb9SGleb Smirnoff 
63203b3a8eb9SGleb Smirnoff int
63213b3a8eb9SGleb Smirnoff lungetc(int c)
63223b3a8eb9SGleb Smirnoff {
63233b3a8eb9SGleb Smirnoff 	if (c == EOF)
63243b3a8eb9SGleb Smirnoff 		return (EOF);
63253b3a8eb9SGleb Smirnoff 	if (parsebuf) {
63263b3a8eb9SGleb Smirnoff 		parseindex--;
63273b3a8eb9SGleb Smirnoff 		if (parseindex >= 0)
63283b3a8eb9SGleb Smirnoff 			return (c);
63293b3a8eb9SGleb Smirnoff 	}
63303b3a8eb9SGleb Smirnoff 	if (pushback_index < MAXPUSHBACK-1)
63313b3a8eb9SGleb Smirnoff 		return (pushback_buffer[pushback_index++] = c);
63323b3a8eb9SGleb Smirnoff 	else
63333b3a8eb9SGleb Smirnoff 		return (EOF);
63343b3a8eb9SGleb Smirnoff }
63353b3a8eb9SGleb Smirnoff 
63363b3a8eb9SGleb Smirnoff int
63373b3a8eb9SGleb Smirnoff findeol(void)
63383b3a8eb9SGleb Smirnoff {
63393b3a8eb9SGleb Smirnoff 	int	c;
63403b3a8eb9SGleb Smirnoff 
63413b3a8eb9SGleb Smirnoff 	parsebuf = NULL;
63423b3a8eb9SGleb Smirnoff 
63433b3a8eb9SGleb Smirnoff 	/* skip to either EOF or the first real EOL */
63443b3a8eb9SGleb Smirnoff 	while (1) {
63453b3a8eb9SGleb Smirnoff 		if (pushback_index)
63463b3a8eb9SGleb Smirnoff 			c = pushback_buffer[--pushback_index];
63473b3a8eb9SGleb Smirnoff 		else
63483b3a8eb9SGleb Smirnoff 			c = lgetc(0);
63493b3a8eb9SGleb Smirnoff 		if (c == '\n') {
63503b3a8eb9SGleb Smirnoff 			file->lineno++;
63513b3a8eb9SGleb Smirnoff 			break;
63523b3a8eb9SGleb Smirnoff 		}
63533b3a8eb9SGleb Smirnoff 		if (c == EOF)
63543b3a8eb9SGleb Smirnoff 			break;
63553b3a8eb9SGleb Smirnoff 	}
63563b3a8eb9SGleb Smirnoff 	return (ERROR);
63573b3a8eb9SGleb Smirnoff }
63583b3a8eb9SGleb Smirnoff 
63593b3a8eb9SGleb Smirnoff int
63603b3a8eb9SGleb Smirnoff yylex(void)
63613b3a8eb9SGleb Smirnoff {
63623b3a8eb9SGleb Smirnoff 	char	 buf[8096];
63633b3a8eb9SGleb Smirnoff 	char	*p, *val;
63643b3a8eb9SGleb Smirnoff 	int	 quotec, next, c;
63653b3a8eb9SGleb Smirnoff 	int	 token;
63663b3a8eb9SGleb Smirnoff 
63673b3a8eb9SGleb Smirnoff top:
63683b3a8eb9SGleb Smirnoff 	p = buf;
63693b3a8eb9SGleb Smirnoff 	while ((c = lgetc(0)) == ' ' || c == '\t')
63703b3a8eb9SGleb Smirnoff 		; /* nothing */
63713b3a8eb9SGleb Smirnoff 
63723b3a8eb9SGleb Smirnoff 	yylval.lineno = file->lineno;
63733b3a8eb9SGleb Smirnoff 	if (c == '#')
63743b3a8eb9SGleb Smirnoff 		while ((c = lgetc(0)) != '\n' && c != EOF)
63753b3a8eb9SGleb Smirnoff 			; /* nothing */
63763b3a8eb9SGleb Smirnoff 	if (c == '$' && parsebuf == NULL) {
63773b3a8eb9SGleb Smirnoff 		while (1) {
63783b3a8eb9SGleb Smirnoff 			if ((c = lgetc(0)) == EOF)
63793b3a8eb9SGleb Smirnoff 				return (0);
63803b3a8eb9SGleb Smirnoff 
63813b3a8eb9SGleb Smirnoff 			if (p + 1 >= buf + sizeof(buf) - 1) {
63823b3a8eb9SGleb Smirnoff 				yyerror("string too long");
63833b3a8eb9SGleb Smirnoff 				return (findeol());
63843b3a8eb9SGleb Smirnoff 			}
63853b3a8eb9SGleb Smirnoff 			if (isalnum(c) || c == '_') {
63863b3a8eb9SGleb Smirnoff 				*p++ = (char)c;
63873b3a8eb9SGleb Smirnoff 				continue;
63883b3a8eb9SGleb Smirnoff 			}
63893b3a8eb9SGleb Smirnoff 			*p = '\0';
63903b3a8eb9SGleb Smirnoff 			lungetc(c);
63913b3a8eb9SGleb Smirnoff 			break;
63923b3a8eb9SGleb Smirnoff 		}
63933b3a8eb9SGleb Smirnoff 		val = symget(buf);
63943b3a8eb9SGleb Smirnoff 		if (val == NULL) {
63953b3a8eb9SGleb Smirnoff 			yyerror("macro '%s' not defined", buf);
63963b3a8eb9SGleb Smirnoff 			return (findeol());
63973b3a8eb9SGleb Smirnoff 		}
63983b3a8eb9SGleb Smirnoff 		parsebuf = val;
63993b3a8eb9SGleb Smirnoff 		parseindex = 0;
64003b3a8eb9SGleb Smirnoff 		goto top;
64013b3a8eb9SGleb Smirnoff 	}
64023b3a8eb9SGleb Smirnoff 
64033b3a8eb9SGleb Smirnoff 	switch (c) {
64043b3a8eb9SGleb Smirnoff 	case '\'':
64053b3a8eb9SGleb Smirnoff 	case '"':
64063b3a8eb9SGleb Smirnoff 		quotec = c;
64073b3a8eb9SGleb Smirnoff 		while (1) {
64083b3a8eb9SGleb Smirnoff 			if ((c = lgetc(quotec)) == EOF)
64093b3a8eb9SGleb Smirnoff 				return (0);
64103b3a8eb9SGleb Smirnoff 			if (c == '\n') {
64113b3a8eb9SGleb Smirnoff 				file->lineno++;
64123b3a8eb9SGleb Smirnoff 				continue;
64133b3a8eb9SGleb Smirnoff 			} else if (c == '\\') {
64143b3a8eb9SGleb Smirnoff 				if ((next = lgetc(quotec)) == EOF)
64153b3a8eb9SGleb Smirnoff 					return (0);
64163b3a8eb9SGleb Smirnoff 				if (next == quotec || c == ' ' || c == '\t')
64173b3a8eb9SGleb Smirnoff 					c = next;
64184a8e4793SKristof Provost 				else if (next == '\n') {
64194a8e4793SKristof Provost 					file->lineno++;
64203b3a8eb9SGleb Smirnoff 					continue;
64214a8e4793SKristof Provost 				}
64223b3a8eb9SGleb Smirnoff 				else
64233b3a8eb9SGleb Smirnoff 					lungetc(next);
64243b3a8eb9SGleb Smirnoff 			} else if (c == quotec) {
64253b3a8eb9SGleb Smirnoff 				*p = '\0';
64263b3a8eb9SGleb Smirnoff 				break;
64273b3a8eb9SGleb Smirnoff 			}
64283b3a8eb9SGleb Smirnoff 			if (p + 1 >= buf + sizeof(buf) - 1) {
64293b3a8eb9SGleb Smirnoff 				yyerror("string too long");
64303b3a8eb9SGleb Smirnoff 				return (findeol());
64313b3a8eb9SGleb Smirnoff 			}
64323b3a8eb9SGleb Smirnoff 			*p++ = (char)c;
64333b3a8eb9SGleb Smirnoff 		}
64343b3a8eb9SGleb Smirnoff 		yylval.v.string = strdup(buf);
64353b3a8eb9SGleb Smirnoff 		if (yylval.v.string == NULL)
64363b3a8eb9SGleb Smirnoff 			err(1, "yylex: strdup");
64373b3a8eb9SGleb Smirnoff 		return (STRING);
64383b3a8eb9SGleb Smirnoff 	case '<':
64393b3a8eb9SGleb Smirnoff 		next = lgetc(0);
64403b3a8eb9SGleb Smirnoff 		if (next == '>') {
64413b3a8eb9SGleb Smirnoff 			yylval.v.i = PF_OP_XRG;
64423b3a8eb9SGleb Smirnoff 			return (PORTBINARY);
64433b3a8eb9SGleb Smirnoff 		}
64443b3a8eb9SGleb Smirnoff 		lungetc(next);
64453b3a8eb9SGleb Smirnoff 		break;
64463b3a8eb9SGleb Smirnoff 	case '>':
64473b3a8eb9SGleb Smirnoff 		next = lgetc(0);
64483b3a8eb9SGleb Smirnoff 		if (next == '<') {
64493b3a8eb9SGleb Smirnoff 			yylval.v.i = PF_OP_IRG;
64503b3a8eb9SGleb Smirnoff 			return (PORTBINARY);
64513b3a8eb9SGleb Smirnoff 		}
64523b3a8eb9SGleb Smirnoff 		lungetc(next);
64533b3a8eb9SGleb Smirnoff 		break;
64543b3a8eb9SGleb Smirnoff 	case '-':
64553b3a8eb9SGleb Smirnoff 		next = lgetc(0);
64563b3a8eb9SGleb Smirnoff 		if (next == '>')
64573b3a8eb9SGleb Smirnoff 			return (ARROW);
64583b3a8eb9SGleb Smirnoff 		lungetc(next);
64593b3a8eb9SGleb Smirnoff 		break;
64603b3a8eb9SGleb Smirnoff 	}
64613b3a8eb9SGleb Smirnoff 
64623b3a8eb9SGleb Smirnoff #define allowed_to_end_number(x) \
64633b3a8eb9SGleb Smirnoff 	(isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
64643b3a8eb9SGleb Smirnoff 
64653b3a8eb9SGleb Smirnoff 	if (c == '-' || isdigit(c)) {
64663b3a8eb9SGleb Smirnoff 		do {
64673b3a8eb9SGleb Smirnoff 			*p++ = c;
64683b3a8eb9SGleb Smirnoff 			if ((unsigned)(p-buf) >= sizeof(buf)) {
64693b3a8eb9SGleb Smirnoff 				yyerror("string too long");
64703b3a8eb9SGleb Smirnoff 				return (findeol());
64713b3a8eb9SGleb Smirnoff 			}
64723b3a8eb9SGleb Smirnoff 		} while ((c = lgetc(0)) != EOF && isdigit(c));
64733b3a8eb9SGleb Smirnoff 		lungetc(c);
64743b3a8eb9SGleb Smirnoff 		if (p == buf + 1 && buf[0] == '-')
64753b3a8eb9SGleb Smirnoff 			goto nodigits;
64763b3a8eb9SGleb Smirnoff 		if (c == EOF || allowed_to_end_number(c)) {
64773b3a8eb9SGleb Smirnoff 			const char *errstr = NULL;
64783b3a8eb9SGleb Smirnoff 
64793b3a8eb9SGleb Smirnoff 			*p = '\0';
64803b3a8eb9SGleb Smirnoff 			yylval.v.number = strtonum(buf, LLONG_MIN,
64813b3a8eb9SGleb Smirnoff 			    LLONG_MAX, &errstr);
64823b3a8eb9SGleb Smirnoff 			if (errstr) {
64833b3a8eb9SGleb Smirnoff 				yyerror("\"%s\" invalid number: %s",
64843b3a8eb9SGleb Smirnoff 				    buf, errstr);
64853b3a8eb9SGleb Smirnoff 				return (findeol());
64863b3a8eb9SGleb Smirnoff 			}
64873b3a8eb9SGleb Smirnoff 			return (NUMBER);
64883b3a8eb9SGleb Smirnoff 		} else {
64893b3a8eb9SGleb Smirnoff nodigits:
64903b3a8eb9SGleb Smirnoff 			while (p > buf + 1)
64913b3a8eb9SGleb Smirnoff 				lungetc(*--p);
64923b3a8eb9SGleb Smirnoff 			c = *--p;
64933b3a8eb9SGleb Smirnoff 			if (c == '-')
64943b3a8eb9SGleb Smirnoff 				return (c);
64953b3a8eb9SGleb Smirnoff 		}
64963b3a8eb9SGleb Smirnoff 	}
64973b3a8eb9SGleb Smirnoff 
64983b3a8eb9SGleb Smirnoff #define allowed_in_string(x) \
64993b3a8eb9SGleb Smirnoff 	(isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
65003b3a8eb9SGleb Smirnoff 	x != '{' && x != '}' && x != '<' && x != '>' && \
65013b3a8eb9SGleb Smirnoff 	x != '!' && x != '=' && x != '/' && x != '#' && \
65023b3a8eb9SGleb Smirnoff 	x != ','))
65033b3a8eb9SGleb Smirnoff 
65043b3a8eb9SGleb Smirnoff 	if (isalnum(c) || c == ':' || c == '_') {
65053b3a8eb9SGleb Smirnoff 		do {
65063b3a8eb9SGleb Smirnoff 			*p++ = c;
65073b3a8eb9SGleb Smirnoff 			if ((unsigned)(p-buf) >= sizeof(buf)) {
65083b3a8eb9SGleb Smirnoff 				yyerror("string too long");
65093b3a8eb9SGleb Smirnoff 				return (findeol());
65103b3a8eb9SGleb Smirnoff 			}
65113b3a8eb9SGleb Smirnoff 		} while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
65123b3a8eb9SGleb Smirnoff 		lungetc(c);
65133b3a8eb9SGleb Smirnoff 		*p = '\0';
65143b3a8eb9SGleb Smirnoff 		if ((token = lookup(buf)) == STRING)
65153b3a8eb9SGleb Smirnoff 			if ((yylval.v.string = strdup(buf)) == NULL)
65163b3a8eb9SGleb Smirnoff 				err(1, "yylex: strdup");
65173b3a8eb9SGleb Smirnoff 		return (token);
65183b3a8eb9SGleb Smirnoff 	}
65193b3a8eb9SGleb Smirnoff 	if (c == '\n') {
65203b3a8eb9SGleb Smirnoff 		yylval.lineno = file->lineno;
65213b3a8eb9SGleb Smirnoff 		file->lineno++;
65223b3a8eb9SGleb Smirnoff 	}
65233b3a8eb9SGleb Smirnoff 	if (c == EOF)
65243b3a8eb9SGleb Smirnoff 		return (0);
65253b3a8eb9SGleb Smirnoff 	return (c);
65263b3a8eb9SGleb Smirnoff }
65273b3a8eb9SGleb Smirnoff 
65283b3a8eb9SGleb Smirnoff int
65293b3a8eb9SGleb Smirnoff check_file_secrecy(int fd, const char *fname)
65303b3a8eb9SGleb Smirnoff {
65313b3a8eb9SGleb Smirnoff 	struct stat	st;
65323b3a8eb9SGleb Smirnoff 
65333b3a8eb9SGleb Smirnoff 	if (fstat(fd, &st)) {
65343b3a8eb9SGleb Smirnoff 		warn("cannot stat %s", fname);
65353b3a8eb9SGleb Smirnoff 		return (-1);
65363b3a8eb9SGleb Smirnoff 	}
65373b3a8eb9SGleb Smirnoff 	if (st.st_uid != 0 && st.st_uid != getuid()) {
65383b3a8eb9SGleb Smirnoff 		warnx("%s: owner not root or current user", fname);
65393b3a8eb9SGleb Smirnoff 		return (-1);
65403b3a8eb9SGleb Smirnoff 	}
65413b3a8eb9SGleb Smirnoff 	if (st.st_mode & (S_IRWXG | S_IRWXO)) {
65423b3a8eb9SGleb Smirnoff 		warnx("%s: group/world readable/writeable", fname);
65433b3a8eb9SGleb Smirnoff 		return (-1);
65443b3a8eb9SGleb Smirnoff 	}
65453b3a8eb9SGleb Smirnoff 	return (0);
65463b3a8eb9SGleb Smirnoff }
65473b3a8eb9SGleb Smirnoff 
65483b3a8eb9SGleb Smirnoff struct file *
65493b3a8eb9SGleb Smirnoff pushfile(const char *name, int secret)
65503b3a8eb9SGleb Smirnoff {
65513b3a8eb9SGleb Smirnoff 	struct file	*nfile;
65523b3a8eb9SGleb Smirnoff 
65533b3a8eb9SGleb Smirnoff 	if ((nfile = calloc(1, sizeof(struct file))) == NULL ||
65543b3a8eb9SGleb Smirnoff 	    (nfile->name = strdup(name)) == NULL) {
65553b3a8eb9SGleb Smirnoff 		warn("malloc");
65563b3a8eb9SGleb Smirnoff 		return (NULL);
65573b3a8eb9SGleb Smirnoff 	}
65583b3a8eb9SGleb Smirnoff 	if (TAILQ_FIRST(&files) == NULL && strcmp(nfile->name, "-") == 0) {
65593b3a8eb9SGleb Smirnoff 		nfile->stream = stdin;
65603b3a8eb9SGleb Smirnoff 		free(nfile->name);
65613b3a8eb9SGleb Smirnoff 		if ((nfile->name = strdup("stdin")) == NULL) {
65623b3a8eb9SGleb Smirnoff 			warn("strdup");
65633b3a8eb9SGleb Smirnoff 			free(nfile);
65643b3a8eb9SGleb Smirnoff 			return (NULL);
65653b3a8eb9SGleb Smirnoff 		}
65663b3a8eb9SGleb Smirnoff 	} else if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
65673b3a8eb9SGleb Smirnoff 		warn("%s", nfile->name);
65683b3a8eb9SGleb Smirnoff 		free(nfile->name);
65693b3a8eb9SGleb Smirnoff 		free(nfile);
65703b3a8eb9SGleb Smirnoff 		return (NULL);
65713b3a8eb9SGleb Smirnoff 	} else if (secret &&
65723b3a8eb9SGleb Smirnoff 	    check_file_secrecy(fileno(nfile->stream), nfile->name)) {
65733b3a8eb9SGleb Smirnoff 		fclose(nfile->stream);
65743b3a8eb9SGleb Smirnoff 		free(nfile->name);
65753b3a8eb9SGleb Smirnoff 		free(nfile);
65763b3a8eb9SGleb Smirnoff 		return (NULL);
65773b3a8eb9SGleb Smirnoff 	}
65783b3a8eb9SGleb Smirnoff 	nfile->lineno = 1;
65793b3a8eb9SGleb Smirnoff 	TAILQ_INSERT_TAIL(&files, nfile, entry);
65803b3a8eb9SGleb Smirnoff 	return (nfile);
65813b3a8eb9SGleb Smirnoff }
65823b3a8eb9SGleb Smirnoff 
65833b3a8eb9SGleb Smirnoff int
65843b3a8eb9SGleb Smirnoff popfile(void)
65853b3a8eb9SGleb Smirnoff {
65863b3a8eb9SGleb Smirnoff 	struct file	*prev;
65873b3a8eb9SGleb Smirnoff 
65883b3a8eb9SGleb Smirnoff 	if ((prev = TAILQ_PREV(file, files, entry)) != NULL) {
65893b3a8eb9SGleb Smirnoff 		prev->errors += file->errors;
65903b3a8eb9SGleb Smirnoff 		TAILQ_REMOVE(&files, file, entry);
65913b3a8eb9SGleb Smirnoff 		fclose(file->stream);
65923b3a8eb9SGleb Smirnoff 		free(file->name);
65933b3a8eb9SGleb Smirnoff 		free(file);
65943b3a8eb9SGleb Smirnoff 		file = prev;
65953b3a8eb9SGleb Smirnoff 		return (0);
65963b3a8eb9SGleb Smirnoff 	}
65973b3a8eb9SGleb Smirnoff 	return (EOF);
65983b3a8eb9SGleb Smirnoff }
65993b3a8eb9SGleb Smirnoff 
66003b3a8eb9SGleb Smirnoff int
66013b3a8eb9SGleb Smirnoff parse_config(char *filename, struct pfctl *xpf)
66023b3a8eb9SGleb Smirnoff {
66033b3a8eb9SGleb Smirnoff 	int		 errors = 0;
66043b3a8eb9SGleb Smirnoff 	struct sym	*sym;
66053b3a8eb9SGleb Smirnoff 
66063b3a8eb9SGleb Smirnoff 	pf = xpf;
66073b3a8eb9SGleb Smirnoff 	errors = 0;
66083b3a8eb9SGleb Smirnoff 	rulestate = PFCTL_STATE_NONE;
66093b3a8eb9SGleb Smirnoff 	returnicmpdefault = (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
66103b3a8eb9SGleb Smirnoff 	returnicmp6default =
66113b3a8eb9SGleb Smirnoff 	    (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
66123b3a8eb9SGleb Smirnoff 	blockpolicy = PFRULE_DROP;
6613150182e3SKristof Provost 	failpolicy = PFRULE_DROP;
66143b3a8eb9SGleb Smirnoff 	require_order = 1;
66153b3a8eb9SGleb Smirnoff 
66163b3a8eb9SGleb Smirnoff 	if ((file = pushfile(filename, 0)) == NULL) {
66173b3a8eb9SGleb Smirnoff 		warn("cannot open the main config file!");
66183b3a8eb9SGleb Smirnoff 		return (-1);
66193b3a8eb9SGleb Smirnoff 	}
66203b3a8eb9SGleb Smirnoff 
66213b3a8eb9SGleb Smirnoff 	yyparse();
66223b3a8eb9SGleb Smirnoff 	errors = file->errors;
66233b3a8eb9SGleb Smirnoff 	popfile();
66243b3a8eb9SGleb Smirnoff 
66253b3a8eb9SGleb Smirnoff 	/* Free macros and check which have not been used. */
66263b3a8eb9SGleb Smirnoff 	while ((sym = TAILQ_FIRST(&symhead))) {
66273b3a8eb9SGleb Smirnoff 		if ((pf->opts & PF_OPT_VERBOSE2) && !sym->used)
66283b3a8eb9SGleb Smirnoff 			fprintf(stderr, "warning: macro '%s' not "
66293b3a8eb9SGleb Smirnoff 			    "used\n", sym->nam);
66303b3a8eb9SGleb Smirnoff 		free(sym->nam);
66313b3a8eb9SGleb Smirnoff 		free(sym->val);
66323b3a8eb9SGleb Smirnoff 		TAILQ_REMOVE(&symhead, sym, entry);
66333b3a8eb9SGleb Smirnoff 		free(sym);
66343b3a8eb9SGleb Smirnoff 	}
66353b3a8eb9SGleb Smirnoff 
66363b3a8eb9SGleb Smirnoff 	return (errors ? -1 : 0);
66373b3a8eb9SGleb Smirnoff }
66383b3a8eb9SGleb Smirnoff 
66393b3a8eb9SGleb Smirnoff int
66403b3a8eb9SGleb Smirnoff symset(const char *nam, const char *val, int persist)
66413b3a8eb9SGleb Smirnoff {
66423b3a8eb9SGleb Smirnoff 	struct sym	*sym;
66433b3a8eb9SGleb Smirnoff 
66443b3a8eb9SGleb Smirnoff 	for (sym = TAILQ_FIRST(&symhead); sym && strcmp(nam, sym->nam);
66453b3a8eb9SGleb Smirnoff 	    sym = TAILQ_NEXT(sym, entry))
66463b3a8eb9SGleb Smirnoff 		;	/* nothing */
66473b3a8eb9SGleb Smirnoff 
66483b3a8eb9SGleb Smirnoff 	if (sym != NULL) {
66493b3a8eb9SGleb Smirnoff 		if (sym->persist == 1)
66503b3a8eb9SGleb Smirnoff 			return (0);
66513b3a8eb9SGleb Smirnoff 		else {
66523b3a8eb9SGleb Smirnoff 			free(sym->nam);
66533b3a8eb9SGleb Smirnoff 			free(sym->val);
66543b3a8eb9SGleb Smirnoff 			TAILQ_REMOVE(&symhead, sym, entry);
66553b3a8eb9SGleb Smirnoff 			free(sym);
66563b3a8eb9SGleb Smirnoff 		}
66573b3a8eb9SGleb Smirnoff 	}
66583b3a8eb9SGleb Smirnoff 	if ((sym = calloc(1, sizeof(*sym))) == NULL)
66593b3a8eb9SGleb Smirnoff 		return (-1);
66603b3a8eb9SGleb Smirnoff 
66613b3a8eb9SGleb Smirnoff 	sym->nam = strdup(nam);
66623b3a8eb9SGleb Smirnoff 	if (sym->nam == NULL) {
66633b3a8eb9SGleb Smirnoff 		free(sym);
66643b3a8eb9SGleb Smirnoff 		return (-1);
66653b3a8eb9SGleb Smirnoff 	}
66663b3a8eb9SGleb Smirnoff 	sym->val = strdup(val);
66673b3a8eb9SGleb Smirnoff 	if (sym->val == NULL) {
66683b3a8eb9SGleb Smirnoff 		free(sym->nam);
66693b3a8eb9SGleb Smirnoff 		free(sym);
66703b3a8eb9SGleb Smirnoff 		return (-1);
66713b3a8eb9SGleb Smirnoff 	}
66723b3a8eb9SGleb Smirnoff 	sym->used = 0;
66733b3a8eb9SGleb Smirnoff 	sym->persist = persist;
66743b3a8eb9SGleb Smirnoff 	TAILQ_INSERT_TAIL(&symhead, sym, entry);
66753b3a8eb9SGleb Smirnoff 	return (0);
66763b3a8eb9SGleb Smirnoff }
66773b3a8eb9SGleb Smirnoff 
66783b3a8eb9SGleb Smirnoff int
66793b3a8eb9SGleb Smirnoff pfctl_cmdline_symset(char *s)
66803b3a8eb9SGleb Smirnoff {
66813b3a8eb9SGleb Smirnoff 	char	*sym, *val;
66823b3a8eb9SGleb Smirnoff 	int	 ret;
66833b3a8eb9SGleb Smirnoff 
66843b3a8eb9SGleb Smirnoff 	if ((val = strrchr(s, '=')) == NULL)
66853b3a8eb9SGleb Smirnoff 		return (-1);
66863b3a8eb9SGleb Smirnoff 
66873b3a8eb9SGleb Smirnoff 	if ((sym = malloc(strlen(s) - strlen(val) + 1)) == NULL)
66883b3a8eb9SGleb Smirnoff 		err(1, "pfctl_cmdline_symset: malloc");
66893b3a8eb9SGleb Smirnoff 
66903b3a8eb9SGleb Smirnoff 	strlcpy(sym, s, strlen(s) - strlen(val) + 1);
66913b3a8eb9SGleb Smirnoff 
66923b3a8eb9SGleb Smirnoff 	ret = symset(sym, val + 1, 1);
66933b3a8eb9SGleb Smirnoff 	free(sym);
66943b3a8eb9SGleb Smirnoff 
66953b3a8eb9SGleb Smirnoff 	return (ret);
66963b3a8eb9SGleb Smirnoff }
66973b3a8eb9SGleb Smirnoff 
66983b3a8eb9SGleb Smirnoff char *
66993b3a8eb9SGleb Smirnoff symget(const char *nam)
67003b3a8eb9SGleb Smirnoff {
67013b3a8eb9SGleb Smirnoff 	struct sym	*sym;
67023b3a8eb9SGleb Smirnoff 
67033b3a8eb9SGleb Smirnoff 	TAILQ_FOREACH(sym, &symhead, entry)
67043b3a8eb9SGleb Smirnoff 		if (strcmp(nam, sym->nam) == 0) {
67053b3a8eb9SGleb Smirnoff 			sym->used = 1;
67063b3a8eb9SGleb Smirnoff 			return (sym->val);
67073b3a8eb9SGleb Smirnoff 		}
67083b3a8eb9SGleb Smirnoff 	return (NULL);
67093b3a8eb9SGleb Smirnoff }
67103b3a8eb9SGleb Smirnoff 
67113b3a8eb9SGleb Smirnoff void
6712e9eb0941SKristof Provost mv_rules(struct pfctl_ruleset *src, struct pfctl_ruleset *dst)
67133b3a8eb9SGleb Smirnoff {
67143b3a8eb9SGleb Smirnoff 	int i;
6715e9eb0941SKristof Provost 	struct pfctl_rule *r;
67163b3a8eb9SGleb Smirnoff 
67173b3a8eb9SGleb Smirnoff 	for (i = 0; i < PF_RULESET_MAX; ++i) {
67183b3a8eb9SGleb Smirnoff 		while ((r = TAILQ_FIRST(src->rules[i].active.ptr))
67193b3a8eb9SGleb Smirnoff 		    != NULL) {
67203b3a8eb9SGleb Smirnoff 			TAILQ_REMOVE(src->rules[i].active.ptr, r, entries);
67213b3a8eb9SGleb Smirnoff 			TAILQ_INSERT_TAIL(dst->rules[i].active.ptr, r, entries);
67223b3a8eb9SGleb Smirnoff 			dst->anchor->match++;
67233b3a8eb9SGleb Smirnoff 		}
67243b3a8eb9SGleb Smirnoff 		src->anchor->match = 0;
67253b3a8eb9SGleb Smirnoff 		while ((r = TAILQ_FIRST(src->rules[i].inactive.ptr))
67263b3a8eb9SGleb Smirnoff 		    != NULL) {
67273b3a8eb9SGleb Smirnoff 			TAILQ_REMOVE(src->rules[i].inactive.ptr, r, entries);
67283b3a8eb9SGleb Smirnoff 			TAILQ_INSERT_TAIL(dst->rules[i].inactive.ptr,
67293b3a8eb9SGleb Smirnoff 				r, entries);
67303b3a8eb9SGleb Smirnoff 		}
67313b3a8eb9SGleb Smirnoff 	}
67323b3a8eb9SGleb Smirnoff }
67333b3a8eb9SGleb Smirnoff 
67343b3a8eb9SGleb Smirnoff void
6735c5131afeSKristof Provost mv_eth_rules(struct pfctl_eth_ruleset *src, struct pfctl_eth_ruleset *dst)
6736c5131afeSKristof Provost {
6737c5131afeSKristof Provost 	struct pfctl_eth_rule *r;
6738c5131afeSKristof Provost 
6739c5131afeSKristof Provost 	while ((r = TAILQ_FIRST(&src->rules)) != NULL) {
6740c5131afeSKristof Provost 		TAILQ_REMOVE(&src->rules, r, entries);
6741c5131afeSKristof Provost 		TAILQ_INSERT_TAIL(&dst->rules, r, entries);
6742c5131afeSKristof Provost 		dst->anchor->match++;
6743c5131afeSKristof Provost 	}
6744c5131afeSKristof Provost 	src->anchor->match = 0;
6745c5131afeSKristof Provost }
6746c5131afeSKristof Provost 
6747c5131afeSKristof Provost void
67483b3a8eb9SGleb Smirnoff decide_address_family(struct node_host *n, sa_family_t *af)
67493b3a8eb9SGleb Smirnoff {
67503b3a8eb9SGleb Smirnoff 	if (*af != 0 || n == NULL)
67513b3a8eb9SGleb Smirnoff 		return;
67523b3a8eb9SGleb Smirnoff 	*af = n->af;
67533b3a8eb9SGleb Smirnoff 	while ((n = n->next) != NULL) {
67543b3a8eb9SGleb Smirnoff 		if (n->af != *af) {
67553b3a8eb9SGleb Smirnoff 			*af = 0;
67563b3a8eb9SGleb Smirnoff 			return;
67573b3a8eb9SGleb Smirnoff 		}
67583b3a8eb9SGleb Smirnoff 	}
67593b3a8eb9SGleb Smirnoff }
67603b3a8eb9SGleb Smirnoff 
67613b3a8eb9SGleb Smirnoff void
67623b3a8eb9SGleb Smirnoff remove_invalid_hosts(struct node_host **nh, sa_family_t *af)
67633b3a8eb9SGleb Smirnoff {
67643b3a8eb9SGleb Smirnoff 	struct node_host	*n = *nh, *prev = NULL;
67653b3a8eb9SGleb Smirnoff 
67663b3a8eb9SGleb Smirnoff 	while (n != NULL) {
67673b3a8eb9SGleb Smirnoff 		if (*af && n->af && n->af != *af) {
67683b3a8eb9SGleb Smirnoff 			/* unlink and free n */
67693b3a8eb9SGleb Smirnoff 			struct node_host *next = n->next;
67703b3a8eb9SGleb Smirnoff 
67713b3a8eb9SGleb Smirnoff 			/* adjust tail pointer */
67723b3a8eb9SGleb Smirnoff 			if (n == (*nh)->tail)
67733b3a8eb9SGleb Smirnoff 				(*nh)->tail = prev;
67743b3a8eb9SGleb Smirnoff 			/* adjust previous node's next pointer */
67753b3a8eb9SGleb Smirnoff 			if (prev == NULL)
67763b3a8eb9SGleb Smirnoff 				*nh = next;
67773b3a8eb9SGleb Smirnoff 			else
67783b3a8eb9SGleb Smirnoff 				prev->next = next;
67793b3a8eb9SGleb Smirnoff 			/* free node */
67803b3a8eb9SGleb Smirnoff 			if (n->ifname != NULL)
67813b3a8eb9SGleb Smirnoff 				free(n->ifname);
67823b3a8eb9SGleb Smirnoff 			free(n);
67833b3a8eb9SGleb Smirnoff 			n = next;
67843b3a8eb9SGleb Smirnoff 		} else {
67853b3a8eb9SGleb Smirnoff 			if (n->af && !*af)
67863b3a8eb9SGleb Smirnoff 				*af = n->af;
67873b3a8eb9SGleb Smirnoff 			prev = n;
67883b3a8eb9SGleb Smirnoff 			n = n->next;
67893b3a8eb9SGleb Smirnoff 		}
67903b3a8eb9SGleb Smirnoff 	}
67913b3a8eb9SGleb Smirnoff }
67923b3a8eb9SGleb Smirnoff 
67933b3a8eb9SGleb Smirnoff int
67943b3a8eb9SGleb Smirnoff invalid_redirect(struct node_host *nh, sa_family_t af)
67953b3a8eb9SGleb Smirnoff {
67963b3a8eb9SGleb Smirnoff 	if (!af) {
67973b3a8eb9SGleb Smirnoff 		struct node_host *n;
67983b3a8eb9SGleb Smirnoff 
67993b3a8eb9SGleb Smirnoff 		/* tables and dyniftl are ok without an address family */
68003b3a8eb9SGleb Smirnoff 		for (n = nh; n != NULL; n = n->next) {
68013b3a8eb9SGleb Smirnoff 			if (n->addr.type != PF_ADDR_TABLE &&
68023b3a8eb9SGleb Smirnoff 			    n->addr.type != PF_ADDR_DYNIFTL) {
68033b3a8eb9SGleb Smirnoff 				yyerror("address family not given and "
68043b3a8eb9SGleb Smirnoff 				    "translation address expands to multiple "
68053b3a8eb9SGleb Smirnoff 				    "address families");
68063b3a8eb9SGleb Smirnoff 				return (1);
68073b3a8eb9SGleb Smirnoff 			}
68083b3a8eb9SGleb Smirnoff 		}
68093b3a8eb9SGleb Smirnoff 	}
68103b3a8eb9SGleb Smirnoff 	if (nh == NULL) {
68113b3a8eb9SGleb Smirnoff 		yyerror("no translation address with matching address family "
68123b3a8eb9SGleb Smirnoff 		    "found.");
68133b3a8eb9SGleb Smirnoff 		return (1);
68143b3a8eb9SGleb Smirnoff 	}
68153b3a8eb9SGleb Smirnoff 	return (0);
68163b3a8eb9SGleb Smirnoff }
68173b3a8eb9SGleb Smirnoff 
68183b3a8eb9SGleb Smirnoff int
68193b3a8eb9SGleb Smirnoff atoul(char *s, u_long *ulvalp)
68203b3a8eb9SGleb Smirnoff {
68213b3a8eb9SGleb Smirnoff 	u_long	 ulval;
68223b3a8eb9SGleb Smirnoff 	char	*ep;
68233b3a8eb9SGleb Smirnoff 
68243b3a8eb9SGleb Smirnoff 	errno = 0;
68253b3a8eb9SGleb Smirnoff 	ulval = strtoul(s, &ep, 0);
68263b3a8eb9SGleb Smirnoff 	if (s[0] == '\0' || *ep != '\0')
68273b3a8eb9SGleb Smirnoff 		return (-1);
68283b3a8eb9SGleb Smirnoff 	if (errno == ERANGE && ulval == ULONG_MAX)
68293b3a8eb9SGleb Smirnoff 		return (-1);
68303b3a8eb9SGleb Smirnoff 	*ulvalp = ulval;
68313b3a8eb9SGleb Smirnoff 	return (0);
68323b3a8eb9SGleb Smirnoff }
68333b3a8eb9SGleb Smirnoff 
68343b3a8eb9SGleb Smirnoff int
68353b3a8eb9SGleb Smirnoff getservice(char *n)
68363b3a8eb9SGleb Smirnoff {
68373b3a8eb9SGleb Smirnoff 	struct servent	*s;
68383b3a8eb9SGleb Smirnoff 	u_long		 ulval;
68393b3a8eb9SGleb Smirnoff 
68403b3a8eb9SGleb Smirnoff 	if (atoul(n, &ulval) == 0) {
68413b3a8eb9SGleb Smirnoff 		if (ulval > 65535) {
68423b3a8eb9SGleb Smirnoff 			yyerror("illegal port value %lu", ulval);
68433b3a8eb9SGleb Smirnoff 			return (-1);
68443b3a8eb9SGleb Smirnoff 		}
68453b3a8eb9SGleb Smirnoff 		return (htons(ulval));
68463b3a8eb9SGleb Smirnoff 	} else {
68473b3a8eb9SGleb Smirnoff 		s = getservbyname(n, "tcp");
68483b3a8eb9SGleb Smirnoff 		if (s == NULL)
68493b3a8eb9SGleb Smirnoff 			s = getservbyname(n, "udp");
68503b3a8eb9SGleb Smirnoff 		if (s == NULL) {
68513b3a8eb9SGleb Smirnoff 			yyerror("unknown port %s", n);
68523b3a8eb9SGleb Smirnoff 			return (-1);
68533b3a8eb9SGleb Smirnoff 		}
68543b3a8eb9SGleb Smirnoff 		return (s->s_port);
68553b3a8eb9SGleb Smirnoff 	}
68563b3a8eb9SGleb Smirnoff }
68573b3a8eb9SGleb Smirnoff 
68583b3a8eb9SGleb Smirnoff int
68596fcc8e04SKristof Provost rule_label(struct pfctl_rule *r, char *s[PF_RULE_MAX_LABEL_COUNT])
68603b3a8eb9SGleb Smirnoff {
68616fcc8e04SKristof Provost 	for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++) {
68626fcc8e04SKristof Provost 		if (s[i] == NULL)
68636fcc8e04SKristof Provost 			return (0);
68646fcc8e04SKristof Provost 
68656fcc8e04SKristof Provost 		if (strlcpy(r->label[i], s[i], sizeof(r->label[0])) >=
68666fcc8e04SKristof Provost 		    sizeof(r->label[0])) {
68673b3a8eb9SGleb Smirnoff 			yyerror("rule label too long (max %d chars)",
68686fcc8e04SKristof Provost 			    sizeof(r->label[0])-1);
68693b3a8eb9SGleb Smirnoff 			return (-1);
68703b3a8eb9SGleb Smirnoff 		}
68713b3a8eb9SGleb Smirnoff 	}
68723b3a8eb9SGleb Smirnoff 	return (0);
68733b3a8eb9SGleb Smirnoff }
68743b3a8eb9SGleb Smirnoff 
68753b3a8eb9SGleb Smirnoff u_int16_t
68763b3a8eb9SGleb Smirnoff parseicmpspec(char *w, sa_family_t af)
68773b3a8eb9SGleb Smirnoff {
68783b3a8eb9SGleb Smirnoff 	const struct icmpcodeent	*p;
68793b3a8eb9SGleb Smirnoff 	u_long				 ulval;
68803b3a8eb9SGleb Smirnoff 	u_int8_t			 icmptype;
68813b3a8eb9SGleb Smirnoff 
68823b3a8eb9SGleb Smirnoff 	if (af == AF_INET)
68833b3a8eb9SGleb Smirnoff 		icmptype = returnicmpdefault >> 8;
68843b3a8eb9SGleb Smirnoff 	else
68853b3a8eb9SGleb Smirnoff 		icmptype = returnicmp6default >> 8;
68863b3a8eb9SGleb Smirnoff 
68873b3a8eb9SGleb Smirnoff 	if (atoul(w, &ulval) == -1) {
68883b3a8eb9SGleb Smirnoff 		if ((p = geticmpcodebyname(icmptype, w, af)) == NULL) {
68893b3a8eb9SGleb Smirnoff 			yyerror("unknown icmp code %s", w);
68903b3a8eb9SGleb Smirnoff 			return (0);
68913b3a8eb9SGleb Smirnoff 		}
68923b3a8eb9SGleb Smirnoff 		ulval = p->code;
68933b3a8eb9SGleb Smirnoff 	}
68943b3a8eb9SGleb Smirnoff 	if (ulval > 255) {
68953b3a8eb9SGleb Smirnoff 		yyerror("invalid icmp code %lu", ulval);
68963b3a8eb9SGleb Smirnoff 		return (0);
68973b3a8eb9SGleb Smirnoff 	}
68983b3a8eb9SGleb Smirnoff 	return (icmptype << 8 | ulval);
68993b3a8eb9SGleb Smirnoff }
69003b3a8eb9SGleb Smirnoff 
69013b3a8eb9SGleb Smirnoff int
69023b3a8eb9SGleb Smirnoff parseport(char *port, struct range *r, int extensions)
69033b3a8eb9SGleb Smirnoff {
69043b3a8eb9SGleb Smirnoff 	char	*p = strchr(port, ':');
69053b3a8eb9SGleb Smirnoff 
69063b3a8eb9SGleb Smirnoff 	if (p == NULL) {
69073b3a8eb9SGleb Smirnoff 		if ((r->a = getservice(port)) == -1)
69083b3a8eb9SGleb Smirnoff 			return (-1);
69093b3a8eb9SGleb Smirnoff 		r->b = 0;
69103b3a8eb9SGleb Smirnoff 		r->t = PF_OP_NONE;
69113b3a8eb9SGleb Smirnoff 		return (0);
69123b3a8eb9SGleb Smirnoff 	}
69133b3a8eb9SGleb Smirnoff 	if ((extensions & PPORT_STAR) && !strcmp(p+1, "*")) {
69143b3a8eb9SGleb Smirnoff 		*p = 0;
69153b3a8eb9SGleb Smirnoff 		if ((r->a = getservice(port)) == -1)
69163b3a8eb9SGleb Smirnoff 			return (-1);
69173b3a8eb9SGleb Smirnoff 		r->b = 0;
69183b3a8eb9SGleb Smirnoff 		r->t = PF_OP_IRG;
69193b3a8eb9SGleb Smirnoff 		return (0);
69203b3a8eb9SGleb Smirnoff 	}
69213b3a8eb9SGleb Smirnoff 	if ((extensions & PPORT_RANGE)) {
69223b3a8eb9SGleb Smirnoff 		*p++ = 0;
69233b3a8eb9SGleb Smirnoff 		if ((r->a = getservice(port)) == -1 ||
69243b3a8eb9SGleb Smirnoff 		    (r->b = getservice(p)) == -1)
69253b3a8eb9SGleb Smirnoff 			return (-1);
69263b3a8eb9SGleb Smirnoff 		if (r->a == r->b) {
69273b3a8eb9SGleb Smirnoff 			r->b = 0;
69283b3a8eb9SGleb Smirnoff 			r->t = PF_OP_NONE;
69293b3a8eb9SGleb Smirnoff 		} else
69303b3a8eb9SGleb Smirnoff 			r->t = PF_OP_RRG;
69313b3a8eb9SGleb Smirnoff 		return (0);
69323b3a8eb9SGleb Smirnoff 	}
69333b3a8eb9SGleb Smirnoff 	return (-1);
69343b3a8eb9SGleb Smirnoff }
69353b3a8eb9SGleb Smirnoff 
69363b3a8eb9SGleb Smirnoff int
69373b3a8eb9SGleb Smirnoff pfctl_load_anchors(int dev, struct pfctl *pf, struct pfr_buffer *trans)
69383b3a8eb9SGleb Smirnoff {
69393b3a8eb9SGleb Smirnoff 	struct loadanchors	*la;
69403b3a8eb9SGleb Smirnoff 
69413b3a8eb9SGleb Smirnoff 	TAILQ_FOREACH(la, &loadanchorshead, entries) {
69423b3a8eb9SGleb Smirnoff 		if (pf->opts & PF_OPT_VERBOSE)
69433b3a8eb9SGleb Smirnoff 			fprintf(stderr, "\nLoading anchor %s from %s\n",
69443b3a8eb9SGleb Smirnoff 			    la->anchorname, la->filename);
69453b3a8eb9SGleb Smirnoff 		if (pfctl_rules(dev, la->filename, pf->opts, pf->optimize,
69463b3a8eb9SGleb Smirnoff 		    la->anchorname, trans) == -1)
69473b3a8eb9SGleb Smirnoff 			return (-1);
69483b3a8eb9SGleb Smirnoff 	}
69493b3a8eb9SGleb Smirnoff 
69503b3a8eb9SGleb Smirnoff 	return (0);
69513b3a8eb9SGleb Smirnoff }
69523b3a8eb9SGleb Smirnoff 
69533b3a8eb9SGleb Smirnoff int
69541f495578SKristof Provost kw_casecmp(const void *k, const void *e)
69551f495578SKristof Provost {
69561f495578SKristof Provost 	return (strcasecmp(k, ((const struct keywords *)e)->k_name));
69571f495578SKristof Provost }
69581f495578SKristof Provost 
69591f495578SKristof Provost int
69601f495578SKristof Provost map_tos(char *s, int *val)
69611f495578SKristof Provost {
69621f495578SKristof Provost 	/* DiffServ Codepoints and other TOS mappings */
69631f495578SKristof Provost 	const struct keywords	 toswords[] = {
69641f495578SKristof Provost 		{ "af11",		IPTOS_DSCP_AF11 },
69651f495578SKristof Provost 		{ "af12",		IPTOS_DSCP_AF12 },
69661f495578SKristof Provost 		{ "af13",		IPTOS_DSCP_AF13 },
69671f495578SKristof Provost 		{ "af21",		IPTOS_DSCP_AF21 },
69681f495578SKristof Provost 		{ "af22",		IPTOS_DSCP_AF22 },
69691f495578SKristof Provost 		{ "af23",		IPTOS_DSCP_AF23 },
69701f495578SKristof Provost 		{ "af31",		IPTOS_DSCP_AF31 },
69711f495578SKristof Provost 		{ "af32",		IPTOS_DSCP_AF32 },
69721f495578SKristof Provost 		{ "af33",		IPTOS_DSCP_AF33 },
69731f495578SKristof Provost 		{ "af41",		IPTOS_DSCP_AF41 },
69741f495578SKristof Provost 		{ "af42",		IPTOS_DSCP_AF42 },
69751f495578SKristof Provost 		{ "af43",		IPTOS_DSCP_AF43 },
69761f495578SKristof Provost 		{ "critical",		IPTOS_PREC_CRITIC_ECP },
69771f495578SKristof Provost 		{ "cs0",		IPTOS_DSCP_CS0 },
69781f495578SKristof Provost 		{ "cs1",		IPTOS_DSCP_CS1 },
69791f495578SKristof Provost 		{ "cs2",		IPTOS_DSCP_CS2 },
69801f495578SKristof Provost 		{ "cs3",		IPTOS_DSCP_CS3 },
69811f495578SKristof Provost 		{ "cs4",		IPTOS_DSCP_CS4 },
69821f495578SKristof Provost 		{ "cs5",		IPTOS_DSCP_CS5 },
69831f495578SKristof Provost 		{ "cs6",		IPTOS_DSCP_CS6 },
69841f495578SKristof Provost 		{ "cs7",		IPTOS_DSCP_CS7 },
69851f495578SKristof Provost 		{ "ef",			IPTOS_DSCP_EF },
69861f495578SKristof Provost 		{ "inetcontrol",	IPTOS_PREC_INTERNETCONTROL },
69871f495578SKristof Provost 		{ "lowdelay",		IPTOS_LOWDELAY },
69881f495578SKristof Provost 		{ "netcontrol",		IPTOS_PREC_NETCONTROL },
69891f495578SKristof Provost 		{ "reliability",	IPTOS_RELIABILITY },
6990b4e3f3c2SKristof Provost 		{ "throughput",		IPTOS_THROUGHPUT },
6991b4e3f3c2SKristof Provost 		{ "va",			IPTOS_DSCP_VA }
69921f495578SKristof Provost 	};
69931f495578SKristof Provost 	const struct keywords	*p;
69941f495578SKristof Provost 
69951f495578SKristof Provost 	p = bsearch(s, toswords, sizeof(toswords)/sizeof(toswords[0]),
69961f495578SKristof Provost 	    sizeof(toswords[0]), kw_casecmp);
69971f495578SKristof Provost 
69981f495578SKristof Provost 	if (p) {
69991f495578SKristof Provost 		*val = p->k_val;
70001f495578SKristof Provost 		return (1);
70011f495578SKristof Provost 	}
70021f495578SKristof Provost 	return (0);
70031f495578SKristof Provost }
70041f495578SKristof Provost 
70051f495578SKristof Provost int
70063b3a8eb9SGleb Smirnoff rt_tableid_max(void)
70073b3a8eb9SGleb Smirnoff {
70083b3a8eb9SGleb Smirnoff #ifdef __FreeBSD__
70093b3a8eb9SGleb Smirnoff 	int fibs;
70103b3a8eb9SGleb Smirnoff 	size_t l = sizeof(fibs);
70113b3a8eb9SGleb Smirnoff 
70123b3a8eb9SGleb Smirnoff         if (sysctlbyname("net.fibs", &fibs, &l, NULL, 0) == -1)
70133b3a8eb9SGleb Smirnoff 		fibs = 16;	/* XXX RT_MAXFIBS, at least limit it some. */
70143b3a8eb9SGleb Smirnoff 	/*
70153b3a8eb9SGleb Smirnoff 	 * As the OpenBSD code only compares > and not >= we need to adjust
70163b3a8eb9SGleb Smirnoff 	 * here given we only accept values of 0..n and want to avoid #ifdefs
7017b68ac800SPedro F. Giffuni 	 * in the grammar.
70183b3a8eb9SGleb Smirnoff 	 */
70193b3a8eb9SGleb Smirnoff 	return (fibs - 1);
70203b3a8eb9SGleb Smirnoff #else
70213b3a8eb9SGleb Smirnoff 	return (RT_TABLEID_MAX);
70223b3a8eb9SGleb Smirnoff #endif
70233b3a8eb9SGleb Smirnoff }
7024b590f17aSKristof Provost 
7025b590f17aSKristof Provost struct node_mac*
7026b590f17aSKristof Provost node_mac_from_string(const char *str)
7027b590f17aSKristof Provost {
7028b590f17aSKristof Provost 	struct node_mac *m;
7029b590f17aSKristof Provost 
7030b590f17aSKristof Provost 	m = calloc(1, sizeof(struct node_mac));
7031b590f17aSKristof Provost 	if (m == NULL)
7032b590f17aSKristof Provost 		err(1, "mac: calloc");
7033b590f17aSKristof Provost 
7034b590f17aSKristof Provost 	if (sscanf(str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
7035b590f17aSKristof Provost 	    &m->mac[0], &m->mac[1], &m->mac[2], &m->mac[3], &m->mac[4],
7036b590f17aSKristof Provost 	    &m->mac[5]) != 6) {
7037b590f17aSKristof Provost 		free(m);
7038b590f17aSKristof Provost 		yyerror("invalid MAC address");
7039b590f17aSKristof Provost 		return (NULL);
7040b590f17aSKristof Provost 	}
7041b590f17aSKristof Provost 
7042b590f17aSKristof Provost 	memset(m->mask, 0xff, ETHER_ADDR_LEN);
7043c32cd180SKristof Provost 	m->isset = true;
7044b590f17aSKristof Provost 	m->next = NULL;
7045b590f17aSKristof Provost 	m->tail = m;
7046b590f17aSKristof Provost 
7047b590f17aSKristof Provost 	return (m);
7048b590f17aSKristof Provost }
7049b590f17aSKristof Provost 
7050b590f17aSKristof Provost struct node_mac*
7051b590f17aSKristof Provost node_mac_from_string_masklen(const char *str, int masklen)
7052b590f17aSKristof Provost {
7053b590f17aSKristof Provost 	struct node_mac *m;
7054b590f17aSKristof Provost 
7055b590f17aSKristof Provost 	if (masklen < 0 || masklen > (ETHER_ADDR_LEN * 8)) {
7056b590f17aSKristof Provost 		yyerror("invalid MAC mask length");
7057b590f17aSKristof Provost 		return (NULL);
7058b590f17aSKristof Provost 	}
7059b590f17aSKristof Provost 
7060b590f17aSKristof Provost 	m = node_mac_from_string(str);
7061b590f17aSKristof Provost 	if (m == NULL)
7062b590f17aSKristof Provost 		return (NULL);
7063b590f17aSKristof Provost 
7064b590f17aSKristof Provost 	memset(m->mask, 0, ETHER_ADDR_LEN);
7065b590f17aSKristof Provost 	for (int i = 0; i < masklen; i++)
7066b590f17aSKristof Provost 		m->mask[i / 8] |= 1 << (i % 8);
7067b590f17aSKristof Provost 
7068b590f17aSKristof Provost 	return (m);
7069b590f17aSKristof Provost }
7070b590f17aSKristof Provost 
7071b590f17aSKristof Provost struct node_mac*
7072b590f17aSKristof Provost node_mac_from_string_mask(const char *str, const char *mask)
7073b590f17aSKristof Provost {
7074b590f17aSKristof Provost 	struct node_mac *m;
7075b590f17aSKristof Provost 
7076b590f17aSKristof Provost 	m = node_mac_from_string(str);
7077b590f17aSKristof Provost 	if (m == NULL)
7078b590f17aSKristof Provost 		return (NULL);
7079b590f17aSKristof Provost 
7080b590f17aSKristof Provost 	if (sscanf(mask, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
7081b590f17aSKristof Provost 	    &m->mask[0], &m->mask[1], &m->mask[2], &m->mask[3], &m->mask[4],
7082b590f17aSKristof Provost 	    &m->mask[5]) != 6) {
7083b590f17aSKristof Provost 		free(m);
7084b590f17aSKristof Provost 		yyerror("invalid MAC mask");
7085b590f17aSKristof Provost 		return (NULL);
7086b590f17aSKristof Provost 	}
7087b590f17aSKristof Provost 
7088b590f17aSKristof Provost 	return (m);
7089b590f17aSKristof Provost }
7090