xref: /freebsd/sbin/pfctl/parse.y (revision aa69fdf1542db0247e9b991002603fc2046bcfbc)
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>
33249cc75fSPatrick Kelsey #define PFIOC_USE_LATEST
34249cc75fSPatrick Kelsey 
353b3a8eb9SGleb Smirnoff #include <sys/types.h>
363b3a8eb9SGleb Smirnoff #include <sys/socket.h>
373b3a8eb9SGleb Smirnoff #include <sys/stat.h>
383b3a8eb9SGleb Smirnoff #ifdef __FreeBSD__
393b3a8eb9SGleb Smirnoff #include <sys/sysctl.h>
403b3a8eb9SGleb Smirnoff #endif
413b3a8eb9SGleb Smirnoff #include <net/if.h>
423b3a8eb9SGleb Smirnoff #include <netinet/in.h>
433b3a8eb9SGleb Smirnoff #include <netinet/in_systm.h>
443b3a8eb9SGleb Smirnoff #include <netinet/ip.h>
453b3a8eb9SGleb Smirnoff #include <netinet/ip_icmp.h>
463b3a8eb9SGleb Smirnoff #include <netinet/icmp6.h>
473b3a8eb9SGleb Smirnoff #include <net/pfvar.h>
483b3a8eb9SGleb Smirnoff #include <arpa/inet.h>
49772e66a6SGleb Smirnoff #include <net/altq/altq.h>
50772e66a6SGleb Smirnoff #include <net/altq/altq_cbq.h>
510a70aaf8SLuiz Otavio O Souza #include <net/altq/altq_codel.h>
52772e66a6SGleb Smirnoff #include <net/altq/altq_priq.h>
53772e66a6SGleb Smirnoff #include <net/altq/altq_hfsc.h>
54a5b789f6SErmal Luçi #include <net/altq/altq_fairq.h>
553b3a8eb9SGleb Smirnoff 
566fcc8e04SKristof Provost #include <assert.h>
573b3a8eb9SGleb Smirnoff #include <stdio.h>
583b3a8eb9SGleb Smirnoff #include <unistd.h>
593b3a8eb9SGleb Smirnoff #include <stdlib.h>
603b3a8eb9SGleb Smirnoff #include <netdb.h>
613b3a8eb9SGleb Smirnoff #include <stdarg.h>
623b3a8eb9SGleb Smirnoff #include <errno.h>
633b3a8eb9SGleb Smirnoff #include <string.h>
643b3a8eb9SGleb Smirnoff #include <ctype.h>
653b3a8eb9SGleb Smirnoff #include <math.h>
663b3a8eb9SGleb Smirnoff #include <err.h>
673b3a8eb9SGleb Smirnoff #include <limits.h>
683b3a8eb9SGleb Smirnoff #include <pwd.h>
693b3a8eb9SGleb Smirnoff #include <grp.h>
703b3a8eb9SGleb Smirnoff #include <md5.h>
713b3a8eb9SGleb Smirnoff 
723b3a8eb9SGleb Smirnoff #include "pfctl_parser.h"
733b3a8eb9SGleb Smirnoff #include "pfctl.h"
743b3a8eb9SGleb Smirnoff 
753b3a8eb9SGleb Smirnoff static struct pfctl	*pf = NULL;
763b3a8eb9SGleb Smirnoff static int		 debug = 0;
773b3a8eb9SGleb Smirnoff static int		 rulestate = 0;
783b3a8eb9SGleb Smirnoff static u_int16_t	 returnicmpdefault =
793b3a8eb9SGleb Smirnoff 			    (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
803b3a8eb9SGleb Smirnoff static u_int16_t	 returnicmp6default =
813b3a8eb9SGleb Smirnoff 			    (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
823b3a8eb9SGleb Smirnoff static int		 blockpolicy = PFRULE_DROP;
83150182e3SKristof Provost static int		 failpolicy = PFRULE_DROP;
843b3a8eb9SGleb Smirnoff static int		 require_order = 1;
853b3a8eb9SGleb Smirnoff static int		 default_statelock;
863b3a8eb9SGleb Smirnoff 
8713cfafabSKristof Provost static TAILQ_HEAD(files, file)	 files = TAILQ_HEAD_INITIALIZER(files);
883b3a8eb9SGleb Smirnoff static struct file {
893b3a8eb9SGleb Smirnoff 	TAILQ_ENTRY(file)	 entry;
903b3a8eb9SGleb Smirnoff 	FILE			*stream;
913b3a8eb9SGleb Smirnoff 	char			*name;
923b3a8eb9SGleb Smirnoff 	int			 lineno;
933b3a8eb9SGleb Smirnoff 	int			 errors;
943b3a8eb9SGleb Smirnoff } *file;
953b3a8eb9SGleb Smirnoff struct file	*pushfile(const char *, int);
963b3a8eb9SGleb Smirnoff int		 popfile(void);
973b3a8eb9SGleb Smirnoff int		 check_file_secrecy(int, const char *);
983b3a8eb9SGleb Smirnoff int		 yyparse(void);
993b3a8eb9SGleb Smirnoff int		 yylex(void);
1003b3a8eb9SGleb Smirnoff int		 yyerror(const char *, ...);
1013b3a8eb9SGleb Smirnoff int		 kw_cmp(const void *, const void *);
1023b3a8eb9SGleb Smirnoff int		 lookup(char *);
1033b3a8eb9SGleb Smirnoff int		 lgetc(int);
1043b3a8eb9SGleb Smirnoff int		 lungetc(int);
1053b3a8eb9SGleb Smirnoff int		 findeol(void);
1063b3a8eb9SGleb Smirnoff 
10713cfafabSKristof Provost static TAILQ_HEAD(symhead, sym)	 symhead = TAILQ_HEAD_INITIALIZER(symhead);
1083b3a8eb9SGleb Smirnoff struct sym {
1093b3a8eb9SGleb Smirnoff 	TAILQ_ENTRY(sym)	 entry;
1103b3a8eb9SGleb Smirnoff 	int			 used;
1113b3a8eb9SGleb Smirnoff 	int			 persist;
1123b3a8eb9SGleb Smirnoff 	char			*nam;
1133b3a8eb9SGleb Smirnoff 	char			*val;
1143b3a8eb9SGleb Smirnoff };
1153b3a8eb9SGleb Smirnoff int		 symset(const char *, const char *, int);
1163b3a8eb9SGleb Smirnoff char		*symget(const char *);
1173b3a8eb9SGleb Smirnoff 
1183b3a8eb9SGleb Smirnoff int		 atoul(char *, u_long *);
1193b3a8eb9SGleb Smirnoff 
1203b3a8eb9SGleb Smirnoff enum {
1213b3a8eb9SGleb Smirnoff 	PFCTL_STATE_NONE,
1223b3a8eb9SGleb Smirnoff 	PFCTL_STATE_OPTION,
1232b29ceb8SKristof Provost 	PFCTL_STATE_ETHER,
1243b3a8eb9SGleb Smirnoff 	PFCTL_STATE_SCRUB,
1253b3a8eb9SGleb Smirnoff 	PFCTL_STATE_QUEUE,
1263b3a8eb9SGleb Smirnoff 	PFCTL_STATE_NAT,
1273b3a8eb9SGleb Smirnoff 	PFCTL_STATE_FILTER
1283b3a8eb9SGleb Smirnoff };
1293b3a8eb9SGleb Smirnoff 
1302b29ceb8SKristof Provost struct node_etherproto {
1312b29ceb8SKristof Provost 	u_int16_t		 proto;
1322b29ceb8SKristof Provost 	struct node_etherproto	*next;
1332b29ceb8SKristof Provost 	struct node_etherproto	*tail;
1342b29ceb8SKristof Provost };
1352b29ceb8SKristof Provost 
1363b3a8eb9SGleb Smirnoff struct node_proto {
1373b3a8eb9SGleb Smirnoff 	u_int8_t		 proto;
1383b3a8eb9SGleb Smirnoff 	struct node_proto	*next;
1393b3a8eb9SGleb Smirnoff 	struct node_proto	*tail;
1403b3a8eb9SGleb Smirnoff };
1413b3a8eb9SGleb Smirnoff 
1423b3a8eb9SGleb Smirnoff struct node_port {
1433b3a8eb9SGleb Smirnoff 	u_int16_t		 port[2];
1443b3a8eb9SGleb Smirnoff 	u_int8_t		 op;
1453b3a8eb9SGleb Smirnoff 	struct node_port	*next;
1463b3a8eb9SGleb Smirnoff 	struct node_port	*tail;
1473b3a8eb9SGleb Smirnoff };
1483b3a8eb9SGleb Smirnoff 
1493b3a8eb9SGleb Smirnoff struct node_uid {
1503b3a8eb9SGleb Smirnoff 	uid_t			 uid[2];
1513b3a8eb9SGleb Smirnoff 	u_int8_t		 op;
1523b3a8eb9SGleb Smirnoff 	struct node_uid		*next;
1533b3a8eb9SGleb Smirnoff 	struct node_uid		*tail;
1543b3a8eb9SGleb Smirnoff };
1553b3a8eb9SGleb Smirnoff 
1563b3a8eb9SGleb Smirnoff struct node_gid {
1573b3a8eb9SGleb Smirnoff 	gid_t			 gid[2];
1583b3a8eb9SGleb Smirnoff 	u_int8_t		 op;
1593b3a8eb9SGleb Smirnoff 	struct node_gid		*next;
1603b3a8eb9SGleb Smirnoff 	struct node_gid		*tail;
1613b3a8eb9SGleb Smirnoff };
1623b3a8eb9SGleb Smirnoff 
1633b3a8eb9SGleb Smirnoff struct node_icmp {
1643b3a8eb9SGleb Smirnoff 	u_int8_t		 code;
1653b3a8eb9SGleb Smirnoff 	u_int8_t		 type;
1663b3a8eb9SGleb Smirnoff 	u_int8_t		 proto;
1673b3a8eb9SGleb Smirnoff 	struct node_icmp	*next;
1683b3a8eb9SGleb Smirnoff 	struct node_icmp	*tail;
1693b3a8eb9SGleb Smirnoff };
1703b3a8eb9SGleb Smirnoff 
1713b3a8eb9SGleb Smirnoff enum	{ PF_STATE_OPT_MAX, PF_STATE_OPT_NOSYNC, PF_STATE_OPT_SRCTRACK,
1723b3a8eb9SGleb Smirnoff 	    PF_STATE_OPT_MAX_SRC_STATES, PF_STATE_OPT_MAX_SRC_CONN,
1733b3a8eb9SGleb Smirnoff 	    PF_STATE_OPT_MAX_SRC_CONN_RATE, PF_STATE_OPT_MAX_SRC_NODES,
1743b3a8eb9SGleb Smirnoff 	    PF_STATE_OPT_OVERLOAD, PF_STATE_OPT_STATELOCK,
175baf9b6d0SKristof Provost 	    PF_STATE_OPT_TIMEOUT, PF_STATE_OPT_SLOPPY,
176baf9b6d0SKristof Provost 	    PF_STATE_OPT_PFLOW };
1773b3a8eb9SGleb Smirnoff 
1783b3a8eb9SGleb Smirnoff enum	{ PF_SRCTRACK_NONE, PF_SRCTRACK, PF_SRCTRACK_GLOBAL, PF_SRCTRACK_RULE };
1793b3a8eb9SGleb Smirnoff 
1803b3a8eb9SGleb Smirnoff struct node_state_opt {
1813b3a8eb9SGleb Smirnoff 	int			 type;
1823b3a8eb9SGleb Smirnoff 	union {
1833b3a8eb9SGleb Smirnoff 		u_int32_t	 max_states;
1843b3a8eb9SGleb Smirnoff 		u_int32_t	 max_src_states;
1853b3a8eb9SGleb Smirnoff 		u_int32_t	 max_src_conn;
1863b3a8eb9SGleb Smirnoff 		struct {
1873b3a8eb9SGleb Smirnoff 			u_int32_t	limit;
1883b3a8eb9SGleb Smirnoff 			u_int32_t	seconds;
1893b3a8eb9SGleb Smirnoff 		}		 max_src_conn_rate;
1903b3a8eb9SGleb Smirnoff 		struct {
1913b3a8eb9SGleb Smirnoff 			u_int8_t	flush;
1923b3a8eb9SGleb Smirnoff 			char		tblname[PF_TABLE_NAME_SIZE];
1933b3a8eb9SGleb Smirnoff 		}		 overload;
1943b3a8eb9SGleb Smirnoff 		u_int32_t	 max_src_nodes;
1953b3a8eb9SGleb Smirnoff 		u_int8_t	 src_track;
1963b3a8eb9SGleb Smirnoff 		u_int32_t	 statelock;
1973b3a8eb9SGleb Smirnoff 		struct {
1983b3a8eb9SGleb Smirnoff 			int		number;
1993b3a8eb9SGleb Smirnoff 			u_int32_t	seconds;
2003b3a8eb9SGleb Smirnoff 		}		 timeout;
2013b3a8eb9SGleb Smirnoff 	}			 data;
2023b3a8eb9SGleb Smirnoff 	struct node_state_opt	*next;
2033b3a8eb9SGleb Smirnoff 	struct node_state_opt	*tail;
2043b3a8eb9SGleb Smirnoff };
2053b3a8eb9SGleb Smirnoff 
2063b3a8eb9SGleb Smirnoff struct peer {
2073b3a8eb9SGleb Smirnoff 	struct node_host	*host;
2083b3a8eb9SGleb Smirnoff 	struct node_port	*port;
2093b3a8eb9SGleb Smirnoff };
2103b3a8eb9SGleb Smirnoff 
21113cfafabSKristof Provost static struct node_queue {
2123b3a8eb9SGleb Smirnoff 	char			 queue[PF_QNAME_SIZE];
2133b3a8eb9SGleb Smirnoff 	char			 parent[PF_QNAME_SIZE];
2143b3a8eb9SGleb Smirnoff 	char			 ifname[IFNAMSIZ];
2153b3a8eb9SGleb Smirnoff 	int			 scheduler;
2163b3a8eb9SGleb Smirnoff 	struct node_queue	*next;
2173b3a8eb9SGleb Smirnoff 	struct node_queue	*tail;
2183b3a8eb9SGleb Smirnoff }	*queues = NULL;
2193b3a8eb9SGleb Smirnoff 
2203b3a8eb9SGleb Smirnoff struct node_qassign {
2213b3a8eb9SGleb Smirnoff 	char		*qname;
2223b3a8eb9SGleb Smirnoff 	char		*pqname;
2233b3a8eb9SGleb Smirnoff };
2243b3a8eb9SGleb Smirnoff 
225*aa69fdf1SKristof Provost struct range {
226*aa69fdf1SKristof Provost 	int		 a;
227*aa69fdf1SKristof Provost 	int		 b;
228*aa69fdf1SKristof Provost 	int		 t;
229*aa69fdf1SKristof Provost };
230*aa69fdf1SKristof Provost struct redirection {
231*aa69fdf1SKristof Provost 	struct node_host	*host;
232*aa69fdf1SKristof Provost 	struct range		 rport;
233*aa69fdf1SKristof Provost };
234*aa69fdf1SKristof Provost 
235*aa69fdf1SKristof Provost static struct pool_opts {
236*aa69fdf1SKristof Provost 	int			 marker;
237*aa69fdf1SKristof Provost #define POM_TYPE		0x01
238*aa69fdf1SKristof Provost #define POM_STICKYADDRESS	0x02
239*aa69fdf1SKristof Provost #define POM_ENDPI		0x04
240*aa69fdf1SKristof Provost 	u_int8_t		 opts;
241*aa69fdf1SKristof Provost 	int			 type;
242*aa69fdf1SKristof Provost 	int			 staticport;
243*aa69fdf1SKristof Provost 	struct pf_poolhashkey	*key;
244*aa69fdf1SKristof Provost 	struct pf_mape_portset	 mape;
245*aa69fdf1SKristof Provost } pool_opts;
246*aa69fdf1SKristof Provost 
247*aa69fdf1SKristof Provost struct redirspec {
248*aa69fdf1SKristof Provost 	struct redirection	 *rdr;
249*aa69fdf1SKristof Provost 	struct pool_opts	 pool_opts;
250*aa69fdf1SKristof Provost 	int		 af;
251*aa69fdf1SKristof Provost };
252*aa69fdf1SKristof Provost 
25313cfafabSKristof Provost static struct filter_opts {
2543b3a8eb9SGleb Smirnoff 	int			 marker;
25539282ef3SKajetan Staszkiewicz #define FOM_FLAGS	0x0001
25639282ef3SKajetan Staszkiewicz #define FOM_ICMP	0x0002
25739282ef3SKajetan Staszkiewicz #define FOM_TOS		0x0004
25839282ef3SKajetan Staszkiewicz #define FOM_KEEP	0x0008
25939282ef3SKajetan Staszkiewicz #define FOM_SRCTRACK	0x0010
26039282ef3SKajetan Staszkiewicz #define FOM_MINTTL	0x0020
26139282ef3SKajetan Staszkiewicz #define FOM_MAXMSS	0x0040
262*aa69fdf1SKristof Provost #define FOM_AFTO	0x0080
26339282ef3SKajetan Staszkiewicz #define FOM_SETTOS	0x0100
26439282ef3SKajetan Staszkiewicz #define FOM_SCRUB_TCP	0x0200
2653e248e0fSKristof Provost #define FOM_SETPRIO	0x0400
26639282ef3SKajetan Staszkiewicz #define FOM_ONCE	0x1000 /* not yet implemmented */
2673e248e0fSKristof Provost #define FOM_PRIO	0x2000
26839282ef3SKajetan Staszkiewicz #define FOM_SETDELAY	0x4000
26939282ef3SKajetan Staszkiewicz #define FOM_FRAGCACHE	0x8000 /* does not exist in OpenBSD */
2703b3a8eb9SGleb Smirnoff 	struct node_uid		*uid;
2713b3a8eb9SGleb Smirnoff 	struct node_gid		*gid;
2722339ead6SKristof Provost 	struct node_if		*rcv;
2733b3a8eb9SGleb Smirnoff 	struct {
2743b3a8eb9SGleb Smirnoff 		u_int8_t	 b1;
2753b3a8eb9SGleb Smirnoff 		u_int8_t	 b2;
2763b3a8eb9SGleb Smirnoff 		u_int16_t	 w;
2773b3a8eb9SGleb Smirnoff 		u_int16_t	 w2;
2783b3a8eb9SGleb Smirnoff 	} flags;
2793b3a8eb9SGleb Smirnoff 	struct node_icmp	*icmpspec;
2803b3a8eb9SGleb Smirnoff 	u_int32_t		 tos;
2813b3a8eb9SGleb Smirnoff 	u_int32_t		 prob;
28276c5eeccSKristof Provost 	u_int32_t		 ridentifier;
2833b3a8eb9SGleb Smirnoff 	struct {
2843b3a8eb9SGleb Smirnoff 		int			 action;
2853b3a8eb9SGleb Smirnoff 		struct node_state_opt	*options;
2863b3a8eb9SGleb Smirnoff 	} keep;
2873b3a8eb9SGleb Smirnoff 	int			 fragment;
2883b3a8eb9SGleb Smirnoff 	int			 allowopts;
2896fcc8e04SKristof Provost 	char			*label[PF_RULE_MAX_LABEL_COUNT];
2906fcc8e04SKristof Provost 	int			 labelcount;
2913b3a8eb9SGleb Smirnoff 	struct node_qassign	 queues;
2923b3a8eb9SGleb Smirnoff 	char			*tag;
2933b3a8eb9SGleb Smirnoff 	char			*match_tag;
2943b3a8eb9SGleb Smirnoff 	u_int8_t		 match_tag_not;
29563b3c1c7SKristof Provost 	u_int16_t		 dnpipe;
29663b3c1c7SKristof Provost 	u_int16_t		 dnrpipe;
29763b3c1c7SKristof Provost 	u_int32_t		 free_flags;
2983b3a8eb9SGleb Smirnoff 	u_int			 rtableid;
2993e248e0fSKristof Provost 	u_int8_t		 prio;
3003e248e0fSKristof Provost 	u_int8_t		 set_prio[2];
3013b3a8eb9SGleb Smirnoff 	struct {
3023b3a8eb9SGleb Smirnoff 		struct node_host	*addr;
3033b3a8eb9SGleb Smirnoff 		u_int16_t		port;
3043b3a8eb9SGleb Smirnoff 	}			 divert;
305*aa69fdf1SKristof Provost 	struct redirspec	 nat;
306*aa69fdf1SKristof Provost 	struct redirspec	 rdr;
30739282ef3SKajetan Staszkiewicz 	/* new-style scrub opts */
30839282ef3SKajetan Staszkiewicz 	int			 nodf;
30939282ef3SKajetan Staszkiewicz 	int			 minttl;
31039282ef3SKajetan Staszkiewicz 	int			 settos;
31139282ef3SKajetan Staszkiewicz 	int			 randomid;
31239282ef3SKajetan Staszkiewicz 	int			 max_mss;
3133b3a8eb9SGleb Smirnoff } filter_opts;
3143b3a8eb9SGleb Smirnoff 
31513cfafabSKristof Provost static struct antispoof_opts {
3166fcc8e04SKristof Provost 	char			*label[PF_RULE_MAX_LABEL_COUNT];
3176fcc8e04SKristof Provost 	int			 labelcount;
31876c5eeccSKristof Provost 	u_int32_t		 ridentifier;
3193b3a8eb9SGleb Smirnoff 	u_int			 rtableid;
3203b3a8eb9SGleb Smirnoff } antispoof_opts;
3213b3a8eb9SGleb Smirnoff 
32213cfafabSKristof Provost static struct scrub_opts {
3233b3a8eb9SGleb Smirnoff 	int			 marker;
3243b3a8eb9SGleb Smirnoff 	int			 nodf;
3253b3a8eb9SGleb Smirnoff 	int			 minttl;
3263b3a8eb9SGleb Smirnoff 	int			 maxmss;
3273b3a8eb9SGleb Smirnoff 	int			 settos;
3283b3a8eb9SGleb Smirnoff 	int			 fragcache;
3293b3a8eb9SGleb Smirnoff 	int			 randomid;
3303b3a8eb9SGleb Smirnoff 	int			 reassemble_tcp;
3313b3a8eb9SGleb Smirnoff 	char			*match_tag;
3323b3a8eb9SGleb Smirnoff 	u_int8_t		 match_tag_not;
3333b3a8eb9SGleb Smirnoff 	u_int			 rtableid;
3343b3a8eb9SGleb Smirnoff } scrub_opts;
3353b3a8eb9SGleb Smirnoff 
33613cfafabSKristof Provost static struct queue_opts {
3373b3a8eb9SGleb Smirnoff 	int			marker;
3383b3a8eb9SGleb Smirnoff #define QOM_BWSPEC	0x01
3393b3a8eb9SGleb Smirnoff #define QOM_SCHEDULER	0x02
3403b3a8eb9SGleb Smirnoff #define QOM_PRIORITY	0x04
3413b3a8eb9SGleb Smirnoff #define QOM_TBRSIZE	0x08
3423b3a8eb9SGleb Smirnoff #define QOM_QLIMIT	0x10
3433b3a8eb9SGleb Smirnoff 	struct node_queue_bw	queue_bwspec;
3443b3a8eb9SGleb Smirnoff 	struct node_queue_opt	scheduler;
3453b3a8eb9SGleb Smirnoff 	int			priority;
346249cc75fSPatrick Kelsey 	unsigned int		tbrsize;
3473b3a8eb9SGleb Smirnoff 	int			qlimit;
3483b3a8eb9SGleb Smirnoff } queue_opts;
3493b3a8eb9SGleb Smirnoff 
35013cfafabSKristof Provost static struct table_opts {
3513b3a8eb9SGleb Smirnoff 	int			flags;
3523b3a8eb9SGleb Smirnoff 	int			init_addr;
3533b3a8eb9SGleb Smirnoff 	struct node_tinithead	init_nodes;
3543b3a8eb9SGleb Smirnoff } table_opts;
3553b3a8eb9SGleb Smirnoff 
35613cfafabSKristof Provost static struct codel_opts	 codel_opts;
35713cfafabSKristof Provost static struct node_hfsc_opts	 hfsc_opts;
35813cfafabSKristof Provost static struct node_fairq_opts	 fairq_opts;
35913cfafabSKristof Provost static struct node_state_opt	*keep_state_defaults = NULL;
3605062afffSKristof Provost static struct pfctl_watermarks	 syncookie_opts;
3613b3a8eb9SGleb Smirnoff 
3623b3a8eb9SGleb Smirnoff int		 disallow_table(struct node_host *, const char *);
3633b3a8eb9SGleb Smirnoff int		 disallow_urpf_failed(struct node_host *, const char *);
3643b3a8eb9SGleb Smirnoff int		 disallow_alias(struct node_host *, const char *);
365e9eb0941SKristof Provost int		 rule_consistent(struct pfctl_rule *, int);
366e9eb0941SKristof Provost int		 filter_consistent(struct pfctl_rule *, int);
367e9eb0941SKristof Provost int		 nat_consistent(struct pfctl_rule *);
368e9eb0941SKristof Provost int		 rdr_consistent(struct pfctl_rule *);
3693b3a8eb9SGleb Smirnoff int		 process_tabledef(char *, struct table_opts *);
3703b3a8eb9SGleb Smirnoff void		 expand_label_str(char *, size_t, const char *, const char *);
3713b3a8eb9SGleb Smirnoff void		 expand_label_if(const char *, char *, size_t, const char *);
3729ec48bc3SKristof Provost void		 expand_label_addr(const char *, char *, size_t, sa_family_t,
37309c7f238SKristof Provost 		    struct pf_rule_addr *);
3743b3a8eb9SGleb Smirnoff void		 expand_label_port(const char *, char *, size_t,
37509c7f238SKristof Provost 		    struct pf_rule_addr *);
3763b3a8eb9SGleb Smirnoff void		 expand_label_proto(const char *, char *, size_t, u_int8_t);
37709c7f238SKristof Provost void		 expand_label_nr(const char *, char *, size_t,
37809c7f238SKristof Provost 		    struct pfctl_rule *);
3792b29ceb8SKristof Provost void		 expand_eth_rule(struct pfctl_eth_rule *,
38087a89d6eSKristof Provost 		    struct node_if *, struct node_etherproto *,
3818a42005dSKristof Provost 		    struct node_mac *, struct node_mac *,
3828a8af942SKristof Provost 		    struct node_host *, struct node_host *, const char *,
3838a8af942SKristof Provost 		    const char *);
384e9eb0941SKristof Provost void		 expand_rule(struct pfctl_rule *, struct node_if *,
385*aa69fdf1SKristof Provost 		    struct node_host *,
3863b3a8eb9SGleb Smirnoff 		    struct node_host *, struct node_proto *, struct node_os *,
3873b3a8eb9SGleb Smirnoff 		    struct node_host *, struct node_port *, struct node_host *,
3883b3a8eb9SGleb Smirnoff 		    struct node_port *, struct node_uid *, struct node_gid *,
3892339ead6SKristof Provost 		    struct node_if *, struct node_icmp *, const char *);
3903b3a8eb9SGleb Smirnoff int		 expand_altq(struct pf_altq *, struct node_if *,
3913b3a8eb9SGleb Smirnoff 		    struct node_queue *, struct node_queue_bw bwspec,
3923b3a8eb9SGleb Smirnoff 		    struct node_queue_opt *);
3933b3a8eb9SGleb Smirnoff int		 expand_queue(struct pf_altq *, struct node_if *,
3943b3a8eb9SGleb Smirnoff 		    struct node_queue *, struct node_queue_bw,
3953b3a8eb9SGleb Smirnoff 		    struct node_queue_opt *);
3963b3a8eb9SGleb Smirnoff int		 expand_skip_interface(struct node_if *);
3973b3a8eb9SGleb Smirnoff 
3983b3a8eb9SGleb Smirnoff int	 check_rulestate(int);
3993b3a8eb9SGleb Smirnoff int	 getservice(char *);
4006fcc8e04SKristof Provost int	 rule_label(struct pfctl_rule *, char *s[PF_RULE_MAX_LABEL_COUNT]);
401ef661d4aSChristian McDonald int	 eth_rule_label(struct pfctl_eth_rule *, char *s[PF_RULE_MAX_LABEL_COUNT]);
4023b3a8eb9SGleb Smirnoff int	 rt_tableid_max(void);
4033b3a8eb9SGleb Smirnoff 
404e9eb0941SKristof Provost void	 mv_rules(struct pfctl_ruleset *, struct pfctl_ruleset *);
405c5131afeSKristof Provost void	 mv_eth_rules(struct pfctl_eth_ruleset *, struct pfctl_eth_ruleset *);
4063b3a8eb9SGleb Smirnoff void	 decide_address_family(struct node_host *, sa_family_t *);
4073b3a8eb9SGleb Smirnoff void	 remove_invalid_hosts(struct node_host **, sa_family_t *);
4083b3a8eb9SGleb Smirnoff int	 invalid_redirect(struct node_host *, sa_family_t);
4093b3a8eb9SGleb Smirnoff u_int16_t parseicmpspec(char *, sa_family_t);
4101f495578SKristof Provost int	 kw_casecmp(const void *, const void *);
4111f495578SKristof Provost int	 map_tos(char *string, int *);
412b590f17aSKristof Provost struct node_mac* node_mac_from_string(const char *);
413b590f17aSKristof Provost struct node_mac* node_mac_from_string_masklen(const char *, int);
414b590f17aSKristof Provost struct node_mac* node_mac_from_string_mask(const char *, const char *);
4153b3a8eb9SGleb Smirnoff 
41613cfafabSKristof Provost static TAILQ_HEAD(loadanchorshead, loadanchors)
4173b3a8eb9SGleb Smirnoff     loadanchorshead = TAILQ_HEAD_INITIALIZER(loadanchorshead);
4183b3a8eb9SGleb Smirnoff 
4193b3a8eb9SGleb Smirnoff struct loadanchors {
4203b3a8eb9SGleb Smirnoff 	TAILQ_ENTRY(loadanchors)	 entries;
4213b3a8eb9SGleb Smirnoff 	char				*anchorname;
4223b3a8eb9SGleb Smirnoff 	char				*filename;
4233b3a8eb9SGleb Smirnoff };
4243b3a8eb9SGleb Smirnoff 
4253b3a8eb9SGleb Smirnoff typedef struct {
4263b3a8eb9SGleb Smirnoff 	union {
4273b3a8eb9SGleb Smirnoff 		int64_t			 number;
4283b3a8eb9SGleb Smirnoff 		double			 probability;
4293b3a8eb9SGleb Smirnoff 		int			 i;
4303b3a8eb9SGleb Smirnoff 		char			*string;
4313b3a8eb9SGleb Smirnoff 		u_int			 rtableid;
4323b3a8eb9SGleb Smirnoff 		struct {
4333b3a8eb9SGleb Smirnoff 			u_int8_t	 b1;
4343b3a8eb9SGleb Smirnoff 			u_int8_t	 b2;
4353b3a8eb9SGleb Smirnoff 			u_int16_t	 w;
4363b3a8eb9SGleb Smirnoff 			u_int16_t	 w2;
4373b3a8eb9SGleb Smirnoff 		}			 b;
438*aa69fdf1SKristof Provost 		struct range		 range;
4393b3a8eb9SGleb Smirnoff 		struct node_if		*interface;
4403b3a8eb9SGleb Smirnoff 		struct node_proto	*proto;
4412b29ceb8SKristof Provost 		struct node_etherproto	*etherproto;
4423b3a8eb9SGleb Smirnoff 		struct node_icmp	*icmp;
4433b3a8eb9SGleb Smirnoff 		struct node_host	*host;
4443b3a8eb9SGleb Smirnoff 		struct node_os		*os;
4453b3a8eb9SGleb Smirnoff 		struct node_port	*port;
4463b3a8eb9SGleb Smirnoff 		struct node_uid		*uid;
4473b3a8eb9SGleb Smirnoff 		struct node_gid		*gid;
4483b3a8eb9SGleb Smirnoff 		struct node_state_opt	*state_opt;
4493b3a8eb9SGleb Smirnoff 		struct peer		 peer;
4503b3a8eb9SGleb Smirnoff 		struct {
4513b3a8eb9SGleb Smirnoff 			struct peer	 src, dst;
4523b3a8eb9SGleb Smirnoff 			struct node_os	*src_os;
4533b3a8eb9SGleb Smirnoff 		}			 fromto;
4543b3a8eb9SGleb Smirnoff 		struct {
45587a89d6eSKristof Provost 			struct node_mac	*src;
45687a89d6eSKristof Provost 			struct node_mac	*dst;
4572b29ceb8SKristof Provost 		}			 etherfromto;
45887a89d6eSKristof Provost 		struct node_mac		*mac;
4592b29ceb8SKristof Provost 		struct {
46087a89d6eSKristof Provost 			struct node_mac	*mac;
4612b29ceb8SKristof Provost 		} etheraddr;
4628a8af942SKristof Provost 		char			*bridge_to;
4632b29ceb8SKristof Provost 		struct {
4643b3a8eb9SGleb Smirnoff 			struct node_host	*host;
4653b3a8eb9SGleb Smirnoff 			u_int8_t		 rt;
4663b3a8eb9SGleb Smirnoff 			u_int8_t		 pool_opts;
4673b3a8eb9SGleb Smirnoff 			sa_family_t		 af;
4683b3a8eb9SGleb Smirnoff 			struct pf_poolhashkey	*key;
4693b3a8eb9SGleb Smirnoff 		}			 route;
470*aa69fdf1SKristof Provost 		struct redirection	*redirection;
4713b3a8eb9SGleb Smirnoff 		struct {
4723b3a8eb9SGleb Smirnoff 			int			 action;
4733b3a8eb9SGleb Smirnoff 			struct node_state_opt	*options;
4743b3a8eb9SGleb Smirnoff 		}			 keep_state;
4753b3a8eb9SGleb Smirnoff 		struct {
4763b3a8eb9SGleb Smirnoff 			u_int8_t	 log;
4773b3a8eb9SGleb Smirnoff 			u_int8_t	 logif;
4783b3a8eb9SGleb Smirnoff 			u_int8_t	 quick;
4793b3a8eb9SGleb Smirnoff 		}			 logquick;
4803b3a8eb9SGleb Smirnoff 		struct {
4813b3a8eb9SGleb Smirnoff 			int		 neg;
4823b3a8eb9SGleb Smirnoff 			char		*name;
4833b3a8eb9SGleb Smirnoff 		}			 tagged;
4843b3a8eb9SGleb Smirnoff 		struct pf_poolhashkey	*hashkey;
4853b3a8eb9SGleb Smirnoff 		struct node_queue	*queue;
4863b3a8eb9SGleb Smirnoff 		struct node_queue_opt	 queue_options;
4873b3a8eb9SGleb Smirnoff 		struct node_queue_bw	 queue_bwspec;
4883b3a8eb9SGleb Smirnoff 		struct node_qassign	 qassign;
4893b3a8eb9SGleb Smirnoff 		struct filter_opts	 filter_opts;
4903b3a8eb9SGleb Smirnoff 		struct antispoof_opts	 antispoof_opts;
4913b3a8eb9SGleb Smirnoff 		struct queue_opts	 queue_opts;
4923b3a8eb9SGleb Smirnoff 		struct scrub_opts	 scrub_opts;
4933b3a8eb9SGleb Smirnoff 		struct table_opts	 table_opts;
4943b3a8eb9SGleb Smirnoff 		struct pool_opts	 pool_opts;
4953b3a8eb9SGleb Smirnoff 		struct node_hfsc_opts	 hfsc_opts;
496a5b789f6SErmal Luçi 		struct node_fairq_opts	 fairq_opts;
4970a70aaf8SLuiz Otavio O Souza 		struct codel_opts	 codel_opts;
4985062afffSKristof Provost 		struct pfctl_watermarks	*watermarks;
4993b3a8eb9SGleb Smirnoff 	} v;
5003b3a8eb9SGleb Smirnoff 	int lineno;
5013b3a8eb9SGleb Smirnoff } YYSTYPE;
5023b3a8eb9SGleb Smirnoff 
5033b3a8eb9SGleb Smirnoff #define PPORT_RANGE	1
5043b3a8eb9SGleb Smirnoff #define PPORT_STAR	2
5053b3a8eb9SGleb Smirnoff int	parseport(char *, struct range *r, int);
5063b3a8eb9SGleb Smirnoff 
5073b3a8eb9SGleb Smirnoff #define DYNIF_MULTIADDR(addr) ((addr).type == PF_ADDR_DYNIFTL && \
5083b3a8eb9SGleb Smirnoff 	(!((addr).iflags & PFI_AFLAG_NOALIAS) ||		 \
5093b3a8eb9SGleb Smirnoff 	!isdigit((addr).v.ifname[strlen((addr).v.ifname)-1])))
5103b3a8eb9SGleb Smirnoff 
5113b3a8eb9SGleb Smirnoff %}
5123b3a8eb9SGleb Smirnoff 
513ef950daaSKristof Provost %token	PASS BLOCK MATCH SCRUB RETURN IN OS OUT LOG QUICK ON FROM TO FLAGS
5143b3a8eb9SGleb Smirnoff %token	RETURNRST RETURNICMP RETURNICMP6 PROTO INET INET6 ALL ANY ICMPTYPE
5153b3a8eb9SGleb Smirnoff %token	ICMP6TYPE CODE KEEP MODULATE STATE PORT RDR NAT BINAT ARROW NODF
5163b3a8eb9SGleb Smirnoff %token	MINTTL ERROR ALLOWOPTS FASTROUTE FILENAME ROUTETO DUPTO REPLYTO NO LABEL
5173b3a8eb9SGleb Smirnoff %token	NOROUTE URPFFAILED FRAGMENT USER GROUP MAXMSS MAXIMUM TTL TOS DROP TABLE
51888e858e5SKristof Provost %token	REASSEMBLE ANCHOR NATANCHOR RDRANCHOR BINATANCHOR
519150182e3SKristof Provost %token	SET OPTIMIZATION TIMEOUT LIMIT LOGINTERFACE BLOCKPOLICY FAILPOLICY
520150182e3SKristof Provost %token	RANDOMID REQUIREORDER SYNPROXY FINGERPRINTS NOSYNC DEBUG SKIP HOSTID
521f3ab00c2SKristof Provost %token	ANTISPOOF FOR INCLUDE KEEPCOUNTERS SYNCOOKIES L3 MATCHES
5222b29ceb8SKristof Provost %token	ETHER
5232aa21096SKurosawa Takahiro %token	BITMASK RANDOM SOURCEHASH ROUNDROBIN STATICPORT PROBABILITY MAPEPORTSET
5240a70aaf8SLuiz Otavio O Souza %token	ALTQ CBQ CODEL PRIQ HFSC FAIRQ BANDWIDTH TBRSIZE LINKSHARE REALTIME
5250a70aaf8SLuiz Otavio O Souza %token	UPPERLIMIT QUEUE PRIORITY QLIMIT HOGS BUCKETS RTABLE TARGET INTERVAL
52676c5eeccSKristof Provost %token	DNPIPE DNQUEUE RIDENTIFIER
5273e248e0fSKristof Provost %token	LOAD RULESET_OPTIMIZATION PRIO
528390dc369STom Jones %token	STICKYADDRESS ENDPI MAXSRCSTATES MAXSRCNODES SOURCETRACK GLOBAL RULE
529baf9b6d0SKristof Provost %token	MAXSRCCONN MAXSRCCONNRATE OVERLOAD FLUSH SLOPPY PFLOW
5303b3a8eb9SGleb Smirnoff %token	TAGGED TAG IFBOUND FLOATING STATEPOLICY STATEDEFAULTS ROUTE SETTOS
531*aa69fdf1SKristof Provost %token	DIVERTTO DIVERTREPLY BRIDGE_TO RECEIVEDON NE LE GE AFTO
5323b3a8eb9SGleb Smirnoff %token	<v.string>		STRING
5333b3a8eb9SGleb Smirnoff %token	<v.number>		NUMBER
5343b3a8eb9SGleb Smirnoff %token	<v.i>			PORTBINARY
5353b3a8eb9SGleb Smirnoff %type	<v.interface>		interface if_list if_item_not if_item
5363b3a8eb9SGleb Smirnoff %type	<v.number>		number icmptype icmp6type uid gid
53739282ef3SKajetan Staszkiewicz %type	<v.number>		tos not yesno optnodf
5383b3a8eb9SGleb Smirnoff %type	<v.probability>		probability
539c69121c4SKristof Provost %type	<v.i>			no dir af fragcache optimizer syncookie_val
5403b3a8eb9SGleb Smirnoff %type	<v.i>			sourcetrack flush unaryop statelock
5412b29ceb8SKristof Provost %type	<v.i>			etherprotoval
5423b3a8eb9SGleb Smirnoff %type	<v.b>			action nataction natpasslog scrubaction
5433e248e0fSKristof Provost %type	<v.b>			flags flag blockspec prio
5443b3a8eb9SGleb Smirnoff %type	<v.range>		portplain portstar portrange
5453b3a8eb9SGleb Smirnoff %type	<v.hashkey>		hashkey
5463b3a8eb9SGleb Smirnoff %type	<v.proto>		proto proto_list proto_item
5473b3a8eb9SGleb Smirnoff %type	<v.number>		protoval
5483b3a8eb9SGleb Smirnoff %type	<v.icmp>		icmpspec
5493b3a8eb9SGleb Smirnoff %type	<v.icmp>		icmp_list icmp_item
5503b3a8eb9SGleb Smirnoff %type	<v.icmp>		icmp6_list icmp6_item
5513b3a8eb9SGleb Smirnoff %type	<v.number>		reticmpspec reticmp6spec
5528a42005dSKristof Provost %type	<v.fromto>		fromto l3fromto
5533b3a8eb9SGleb Smirnoff %type	<v.peer>		ipportspec from to
5543b3a8eb9SGleb Smirnoff %type	<v.host>		ipspec toipspec xhost host dynaddr host_list
5553b3a8eb9SGleb Smirnoff %type	<v.host>		redir_host_list redirspec
5563b3a8eb9SGleb Smirnoff %type	<v.host>		route_host route_host_list routespec
5573b3a8eb9SGleb Smirnoff %type	<v.os>			os xos os_list
5583b3a8eb9SGleb Smirnoff %type	<v.port>		portspec port_list port_item
5593b3a8eb9SGleb Smirnoff %type	<v.uid>			uids uid_list uid_item
5603b3a8eb9SGleb Smirnoff %type	<v.gid>			gids gid_list gid_item
5613b3a8eb9SGleb Smirnoff %type	<v.route>		route
5623b3a8eb9SGleb Smirnoff %type	<v.redirection>		redirection redirpool
5633b3a8eb9SGleb Smirnoff %type	<v.string>		label stringall tag anchorname
5643b3a8eb9SGleb Smirnoff %type	<v.string>		string varstring numberstring
5653b3a8eb9SGleb Smirnoff %type	<v.keep_state>		keep
5663b3a8eb9SGleb Smirnoff %type	<v.state_opt>		state_opt_spec state_opt_list state_opt_item
5673b3a8eb9SGleb Smirnoff %type	<v.logquick>		logquick quick log logopts logopt
5683b3a8eb9SGleb Smirnoff %type	<v.interface>		antispoof_ifspc antispoof_iflst antispoof_if
5692b29ceb8SKristof Provost %type	<v.qassign>		qname etherqname
5703b3a8eb9SGleb Smirnoff %type	<v.queue>		qassign qassign_list qassign_item
5713b3a8eb9SGleb Smirnoff %type	<v.queue_options>	scheduler
5723b3a8eb9SGleb Smirnoff %type	<v.number>		cbqflags_list cbqflags_item
5733b3a8eb9SGleb Smirnoff %type	<v.number>		priqflags_list priqflags_item
5743b3a8eb9SGleb Smirnoff %type	<v.hfsc_opts>		hfscopts_list hfscopts_item hfsc_opts
575a5b789f6SErmal Luçi %type	<v.fairq_opts>		fairqopts_list fairqopts_item fairq_opts
5760a70aaf8SLuiz Otavio O Souza %type	<v.codel_opts>		codelopts_list codelopts_item codel_opts
5773b3a8eb9SGleb Smirnoff %type	<v.queue_bwspec>	bandwidth
5782b29ceb8SKristof Provost %type	<v.filter_opts>		filter_opts filter_opt filter_opts_l etherfilter_opts etherfilter_opt etherfilter_opts_l
5793e248e0fSKristof Provost %type	<v.filter_opts>		filter_sets filter_set filter_sets_l
5803b3a8eb9SGleb Smirnoff %type	<v.antispoof_opts>	antispoof_opts antispoof_opt antispoof_opts_l
5813b3a8eb9SGleb Smirnoff %type	<v.queue_opts>		queue_opts queue_opt queue_opts_l
5823b3a8eb9SGleb Smirnoff %type	<v.scrub_opts>		scrub_opts scrub_opt scrub_opts_l
5833b3a8eb9SGleb Smirnoff %type	<v.table_opts>		table_opts table_opt table_opts_l
5843b3a8eb9SGleb Smirnoff %type	<v.pool_opts>		pool_opts pool_opt pool_opts_l
5853b3a8eb9SGleb Smirnoff %type	<v.tagged>		tagged
5863b3a8eb9SGleb Smirnoff %type	<v.rtableid>		rtable
5875062afffSKristof Provost %type	<v.watermarks>		syncookie_opts
5882b29ceb8SKristof Provost %type	<v.etherproto>		etherproto etherproto_list etherproto_item
5892b29ceb8SKristof Provost %type	<v.etherfromto>		etherfromto
5902b29ceb8SKristof Provost %type	<v.etheraddr>		etherfrom etherto
5918a8af942SKristof Provost %type	<v.bridge_to>		bridge
59287a89d6eSKristof Provost %type	<v.mac>			xmac mac mac_list macspec
5933b3a8eb9SGleb Smirnoff %%
5943b3a8eb9SGleb Smirnoff 
5953b3a8eb9SGleb Smirnoff ruleset		: /* empty */
5963b3a8eb9SGleb Smirnoff 		| ruleset include '\n'
5973b3a8eb9SGleb Smirnoff 		| ruleset '\n'
5983b3a8eb9SGleb Smirnoff 		| ruleset option '\n'
5992b29ceb8SKristof Provost 		| ruleset etherrule '\n'
600c5131afeSKristof Provost 		| ruleset etheranchorrule '\n'
6013b3a8eb9SGleb Smirnoff 		| ruleset scrubrule '\n'
6023b3a8eb9SGleb Smirnoff 		| ruleset natrule '\n'
6033b3a8eb9SGleb Smirnoff 		| ruleset binatrule '\n'
6043b3a8eb9SGleb Smirnoff 		| ruleset pfrule '\n'
6053b3a8eb9SGleb Smirnoff 		| ruleset anchorrule '\n'
6063b3a8eb9SGleb Smirnoff 		| ruleset loadrule '\n'
6073b3a8eb9SGleb Smirnoff 		| ruleset altqif '\n'
6083b3a8eb9SGleb Smirnoff 		| ruleset queuespec '\n'
6093b3a8eb9SGleb Smirnoff 		| ruleset varset '\n'
6103b3a8eb9SGleb Smirnoff 		| ruleset antispoof '\n'
6113b3a8eb9SGleb Smirnoff 		| ruleset tabledef '\n'
6123b3a8eb9SGleb Smirnoff 		| '{' fakeanchor '}' '\n';
6133b3a8eb9SGleb Smirnoff 		| ruleset error '\n'		{ file->errors++; }
6143b3a8eb9SGleb Smirnoff 		;
6153b3a8eb9SGleb Smirnoff 
6163b3a8eb9SGleb Smirnoff include		: INCLUDE STRING		{
6173b3a8eb9SGleb Smirnoff 			struct file	*nfile;
6183b3a8eb9SGleb Smirnoff 
6193b3a8eb9SGleb Smirnoff 			if ((nfile = pushfile($2, 0)) == NULL) {
6203b3a8eb9SGleb Smirnoff 				yyerror("failed to include file %s", $2);
6213b3a8eb9SGleb Smirnoff 				free($2);
6223b3a8eb9SGleb Smirnoff 				YYERROR;
6233b3a8eb9SGleb Smirnoff 			}
6243b3a8eb9SGleb Smirnoff 			free($2);
6253b3a8eb9SGleb Smirnoff 
6263b3a8eb9SGleb Smirnoff 			file = nfile;
6273b3a8eb9SGleb Smirnoff 			lungetc('\n');
6283b3a8eb9SGleb Smirnoff 		}
6293b3a8eb9SGleb Smirnoff 		;
6303b3a8eb9SGleb Smirnoff 
6313b3a8eb9SGleb Smirnoff /*
6323b3a8eb9SGleb Smirnoff  * apply to previouslys specified rule: must be careful to note
6333b3a8eb9SGleb Smirnoff  * what that is: pf or nat or binat or rdr
6343b3a8eb9SGleb Smirnoff  */
6353b3a8eb9SGleb Smirnoff fakeanchor	: fakeanchor '\n'
6363b3a8eb9SGleb Smirnoff 		| fakeanchor anchorrule '\n'
6373b3a8eb9SGleb Smirnoff 		| fakeanchor binatrule '\n'
6383b3a8eb9SGleb Smirnoff 		| fakeanchor natrule '\n'
6393b3a8eb9SGleb Smirnoff 		| fakeanchor pfrule '\n'
6403b3a8eb9SGleb Smirnoff 		| fakeanchor error '\n'
6413b3a8eb9SGleb Smirnoff 		;
6423b3a8eb9SGleb Smirnoff 
6433b3a8eb9SGleb Smirnoff optimizer	: string	{
6443b3a8eb9SGleb Smirnoff 			if (!strcmp($1, "none"))
6453b3a8eb9SGleb Smirnoff 				$$ = 0;
6463b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "basic"))
6473b3a8eb9SGleb Smirnoff 				$$ = PF_OPTIMIZE_BASIC;
6483b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "profile"))
6493b3a8eb9SGleb Smirnoff 				$$ = PF_OPTIMIZE_BASIC | PF_OPTIMIZE_PROFILE;
6503b3a8eb9SGleb Smirnoff 			else {
6513b3a8eb9SGleb Smirnoff 				yyerror("unknown ruleset-optimization %s", $1);
6523b3a8eb9SGleb Smirnoff 				YYERROR;
6533b3a8eb9SGleb Smirnoff 			}
6543b3a8eb9SGleb Smirnoff 		}
6553b3a8eb9SGleb Smirnoff 		;
6563b3a8eb9SGleb Smirnoff 
65739282ef3SKajetan Staszkiewicz optnodf		: /* empty */	{ $$ = 0; }
65839282ef3SKajetan Staszkiewicz 		| NODF		{ $$ = 1; }
65939282ef3SKajetan Staszkiewicz 		;
66039282ef3SKajetan Staszkiewicz 
66139282ef3SKajetan Staszkiewicz option		: SET REASSEMBLE yesno optnodf		{
66239282ef3SKajetan Staszkiewicz 			if (check_rulestate(PFCTL_STATE_OPTION))
66339282ef3SKajetan Staszkiewicz 				YYERROR;
66439282ef3SKajetan Staszkiewicz 			pfctl_set_reassembly(pf, $3, $4);
66539282ef3SKajetan Staszkiewicz 		}
66639282ef3SKajetan Staszkiewicz 		| SET OPTIMIZATION STRING		{
6673b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_OPTION)) {
6683b3a8eb9SGleb Smirnoff 				free($3);
6693b3a8eb9SGleb Smirnoff 				YYERROR;
6703b3a8eb9SGleb Smirnoff 			}
6713b3a8eb9SGleb Smirnoff 			if (pfctl_set_optimization(pf, $3) != 0) {
6723b3a8eb9SGleb Smirnoff 				yyerror("unknown optimization %s", $3);
6733b3a8eb9SGleb Smirnoff 				free($3);
6743b3a8eb9SGleb Smirnoff 				YYERROR;
6753b3a8eb9SGleb Smirnoff 			}
6763b3a8eb9SGleb Smirnoff 			free($3);
6773b3a8eb9SGleb Smirnoff 		}
6783b3a8eb9SGleb Smirnoff 		| SET RULESET_OPTIMIZATION optimizer {
6793b3a8eb9SGleb Smirnoff 			if (!(pf->opts & PF_OPT_OPTIMIZE)) {
6803b3a8eb9SGleb Smirnoff 				pf->opts |= PF_OPT_OPTIMIZE;
6813b3a8eb9SGleb Smirnoff 				pf->optimize = $3;
6823b3a8eb9SGleb Smirnoff 			}
6833b3a8eb9SGleb Smirnoff 		}
6843b3a8eb9SGleb Smirnoff 		| SET TIMEOUT timeout_spec
6853b3a8eb9SGleb Smirnoff 		| SET TIMEOUT '{' optnl timeout_list '}'
6863b3a8eb9SGleb Smirnoff 		| SET LIMIT limit_spec
6873b3a8eb9SGleb Smirnoff 		| SET LIMIT '{' optnl limit_list '}'
6883b3a8eb9SGleb Smirnoff 		| SET LOGINTERFACE stringall		{
6893b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_OPTION)) {
6903b3a8eb9SGleb Smirnoff 				free($3);
6913b3a8eb9SGleb Smirnoff 				YYERROR;
6923b3a8eb9SGleb Smirnoff 			}
6933b3a8eb9SGleb Smirnoff 			if (pfctl_set_logif(pf, $3) != 0) {
6943b3a8eb9SGleb Smirnoff 				yyerror("error setting loginterface %s", $3);
6953b3a8eb9SGleb Smirnoff 				free($3);
6963b3a8eb9SGleb Smirnoff 				YYERROR;
6973b3a8eb9SGleb Smirnoff 			}
6983b3a8eb9SGleb Smirnoff 			free($3);
6993b3a8eb9SGleb Smirnoff 		}
7003b3a8eb9SGleb Smirnoff 		| SET HOSTID number {
7013b3a8eb9SGleb Smirnoff 			if ($3 == 0 || $3 > UINT_MAX) {
7023b3a8eb9SGleb Smirnoff 				yyerror("hostid must be non-zero");
7033b3a8eb9SGleb Smirnoff 				YYERROR;
7043b3a8eb9SGleb Smirnoff 			}
7053b3a8eb9SGleb Smirnoff 			if (pfctl_set_hostid(pf, $3) != 0) {
7063b3a8eb9SGleb Smirnoff 				yyerror("error setting hostid %08x", $3);
7073b3a8eb9SGleb Smirnoff 				YYERROR;
7083b3a8eb9SGleb Smirnoff 			}
7093b3a8eb9SGleb Smirnoff 		}
7103b3a8eb9SGleb Smirnoff 		| SET BLOCKPOLICY DROP	{
7113b3a8eb9SGleb Smirnoff 			if (pf->opts & PF_OPT_VERBOSE)
7123b3a8eb9SGleb Smirnoff 				printf("set block-policy drop\n");
7133b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_OPTION))
7143b3a8eb9SGleb Smirnoff 				YYERROR;
7153b3a8eb9SGleb Smirnoff 			blockpolicy = PFRULE_DROP;
7163b3a8eb9SGleb Smirnoff 		}
7173b3a8eb9SGleb Smirnoff 		| SET BLOCKPOLICY RETURN {
7183b3a8eb9SGleb Smirnoff 			if (pf->opts & PF_OPT_VERBOSE)
7193b3a8eb9SGleb Smirnoff 				printf("set block-policy return\n");
7203b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_OPTION))
7213b3a8eb9SGleb Smirnoff 				YYERROR;
7223b3a8eb9SGleb Smirnoff 			blockpolicy = PFRULE_RETURN;
7233b3a8eb9SGleb Smirnoff 		}
724150182e3SKristof Provost 		| SET FAILPOLICY DROP	{
725150182e3SKristof Provost 			if (pf->opts & PF_OPT_VERBOSE)
726150182e3SKristof Provost 				printf("set fail-policy drop\n");
727150182e3SKristof Provost 			if (check_rulestate(PFCTL_STATE_OPTION))
728150182e3SKristof Provost 				YYERROR;
729150182e3SKristof Provost 			failpolicy = PFRULE_DROP;
730150182e3SKristof Provost 		}
731150182e3SKristof Provost 		| SET FAILPOLICY RETURN {
732150182e3SKristof Provost 			if (pf->opts & PF_OPT_VERBOSE)
733150182e3SKristof Provost 				printf("set fail-policy return\n");
734150182e3SKristof Provost 			if (check_rulestate(PFCTL_STATE_OPTION))
735150182e3SKristof Provost 				YYERROR;
736150182e3SKristof Provost 			failpolicy = PFRULE_RETURN;
737150182e3SKristof Provost 		}
7383b3a8eb9SGleb Smirnoff 		| SET REQUIREORDER yesno {
7393b3a8eb9SGleb Smirnoff 			if (pf->opts & PF_OPT_VERBOSE)
7403b3a8eb9SGleb Smirnoff 				printf("set require-order %s\n",
7413b3a8eb9SGleb Smirnoff 				    $3 == 1 ? "yes" : "no");
7423b3a8eb9SGleb Smirnoff 			require_order = $3;
7433b3a8eb9SGleb Smirnoff 		}
7443b3a8eb9SGleb Smirnoff 		| SET FINGERPRINTS STRING {
7453b3a8eb9SGleb Smirnoff 			if (pf->opts & PF_OPT_VERBOSE)
7463b3a8eb9SGleb Smirnoff 				printf("set fingerprints \"%s\"\n", $3);
7473b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_OPTION)) {
7483b3a8eb9SGleb Smirnoff 				free($3);
7493b3a8eb9SGleb Smirnoff 				YYERROR;
7503b3a8eb9SGleb Smirnoff 			}
7513b3a8eb9SGleb Smirnoff 			if (!pf->anchor->name[0]) {
7523b3a8eb9SGleb Smirnoff 				if (pfctl_file_fingerprints(pf->dev,
7533b3a8eb9SGleb Smirnoff 				    pf->opts, $3)) {
7543b3a8eb9SGleb Smirnoff 					yyerror("error loading "
7553b3a8eb9SGleb Smirnoff 					    "fingerprints %s", $3);
7563b3a8eb9SGleb Smirnoff 					free($3);
7573b3a8eb9SGleb Smirnoff 					YYERROR;
7583b3a8eb9SGleb Smirnoff 				}
7593b3a8eb9SGleb Smirnoff 			}
7603b3a8eb9SGleb Smirnoff 			free($3);
7613b3a8eb9SGleb Smirnoff 		}
7623b3a8eb9SGleb Smirnoff 		| SET STATEPOLICY statelock {
7633b3a8eb9SGleb Smirnoff 			if (pf->opts & PF_OPT_VERBOSE)
7643b3a8eb9SGleb Smirnoff 				switch ($3) {
7653b3a8eb9SGleb Smirnoff 				case 0:
7663b3a8eb9SGleb Smirnoff 					printf("set state-policy floating\n");
7673b3a8eb9SGleb Smirnoff 					break;
7683b3a8eb9SGleb Smirnoff 				case PFRULE_IFBOUND:
7693b3a8eb9SGleb Smirnoff 					printf("set state-policy if-bound\n");
7703b3a8eb9SGleb Smirnoff 					break;
7713b3a8eb9SGleb Smirnoff 				}
7723b3a8eb9SGleb Smirnoff 			default_statelock = $3;
7733b3a8eb9SGleb Smirnoff 		}
7743b3a8eb9SGleb Smirnoff 		| SET DEBUG STRING {
7753b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_OPTION)) {
7763b3a8eb9SGleb Smirnoff 				free($3);
7773b3a8eb9SGleb Smirnoff 				YYERROR;
7783b3a8eb9SGleb Smirnoff 			}
779c36c90a2SKristof Provost 			if (pfctl_do_set_debug(pf, $3) != 0) {
7803b3a8eb9SGleb Smirnoff 				yyerror("error setting debuglevel %s", $3);
7813b3a8eb9SGleb Smirnoff 				free($3);
7823b3a8eb9SGleb Smirnoff 				YYERROR;
7833b3a8eb9SGleb Smirnoff 			}
7843b3a8eb9SGleb Smirnoff 			free($3);
7853b3a8eb9SGleb Smirnoff 		}
7863b3a8eb9SGleb Smirnoff 		| SET SKIP interface {
7873b3a8eb9SGleb Smirnoff 			if (expand_skip_interface($3) != 0) {
7883b3a8eb9SGleb Smirnoff 				yyerror("error setting skip interface(s)");
7893b3a8eb9SGleb Smirnoff 				YYERROR;
7903b3a8eb9SGleb Smirnoff 			}
7913b3a8eb9SGleb Smirnoff 		}
7923b3a8eb9SGleb Smirnoff 		| SET STATEDEFAULTS state_opt_list {
7933b3a8eb9SGleb Smirnoff 			if (keep_state_defaults != NULL) {
7943b3a8eb9SGleb Smirnoff 				yyerror("cannot redefine state-defaults");
7953b3a8eb9SGleb Smirnoff 				YYERROR;
7963b3a8eb9SGleb Smirnoff 			}
7973b3a8eb9SGleb Smirnoff 			keep_state_defaults = $3;
7983b3a8eb9SGleb Smirnoff 		}
79942ec75f8SKristof Provost 		| SET KEEPCOUNTERS {
80042ec75f8SKristof Provost 			pf->keep_counters = true;
80142ec75f8SKristof Provost 		}
8025062afffSKristof Provost 		| SET SYNCOOKIES syncookie_val syncookie_opts {
8035062afffSKristof Provost 			if (pfctl_cfg_syncookies(pf, $3, $4)) {
8045062afffSKristof Provost 				yyerror("error setting syncookies");
8055062afffSKristof Provost 				YYERROR;
8065062afffSKristof Provost 			}
807c69121c4SKristof Provost 		}
808c69121c4SKristof Provost 		;
809c69121c4SKristof Provost 
810c69121c4SKristof Provost syncookie_val  : STRING        {
811c69121c4SKristof Provost 			if (!strcmp($1, "never"))
812c69121c4SKristof Provost 				$$ = PFCTL_SYNCOOKIES_NEVER;
8135062afffSKristof Provost 			else if (!strcmp($1, "adaptive"))
8145062afffSKristof Provost 				$$ = PFCTL_SYNCOOKIES_ADAPTIVE;
815c69121c4SKristof Provost 			else if (!strcmp($1, "always"))
816c69121c4SKristof Provost 				$$ = PFCTL_SYNCOOKIES_ALWAYS;
817c69121c4SKristof Provost 			else {
818c69121c4SKristof Provost 				yyerror("illegal value for syncookies");
819c69121c4SKristof Provost 				YYERROR;
820c69121c4SKristof Provost 			}
821c69121c4SKristof Provost 		}
8223b3a8eb9SGleb Smirnoff 		;
8235062afffSKristof Provost syncookie_opts  : /* empty */                   { $$ = NULL; }
8245062afffSKristof Provost 		| {
8255062afffSKristof Provost 			memset(&syncookie_opts, 0, sizeof(syncookie_opts));
8265062afffSKristof Provost 		  } '(' syncookie_opt_l ')'     { $$ = &syncookie_opts; }
8275062afffSKristof Provost 		;
8285062afffSKristof Provost 
8295062afffSKristof Provost syncookie_opt_l : syncookie_opt_l comma syncookie_opt
8305062afffSKristof Provost 		| syncookie_opt
8315062afffSKristof Provost 		;
8325062afffSKristof Provost 
8335062afffSKristof Provost syncookie_opt   : STRING STRING {
8345062afffSKristof Provost 			double   val;
8355062afffSKristof Provost 			char    *cp;
8365062afffSKristof Provost 
8375062afffSKristof Provost 			val = strtod($2, &cp);
8385062afffSKristof Provost 			if (cp == NULL || strcmp(cp, "%"))
8395062afffSKristof Provost 				YYERROR;
8405062afffSKristof Provost 			if (val <= 0 || val > 100) {
8415062afffSKristof Provost 				yyerror("illegal percentage value");
8425062afffSKristof Provost 				YYERROR;
8435062afffSKristof Provost 			}
8445062afffSKristof Provost 			if (!strcmp($1, "start")) {
8455062afffSKristof Provost 				syncookie_opts.hi = val;
8465062afffSKristof Provost 			} else if (!strcmp($1, "end")) {
8475062afffSKristof Provost 				syncookie_opts.lo = val;
8485062afffSKristof Provost 			} else {
8495062afffSKristof Provost 				yyerror("illegal syncookie option");
8505062afffSKristof Provost 				YYERROR;
8515062afffSKristof Provost 			}
8525062afffSKristof Provost 		}
8535062afffSKristof Provost 		;
8543b3a8eb9SGleb Smirnoff 
8553b3a8eb9SGleb Smirnoff stringall	: STRING	{ $$ = $1; }
8563b3a8eb9SGleb Smirnoff 		| ALL		{
8573b3a8eb9SGleb Smirnoff 			if (($$ = strdup("all")) == NULL) {
8583b3a8eb9SGleb Smirnoff 				err(1, "stringall: strdup");
8593b3a8eb9SGleb Smirnoff 			}
8603b3a8eb9SGleb Smirnoff 		}
8613b3a8eb9SGleb Smirnoff 		;
8623b3a8eb9SGleb Smirnoff 
8633b3a8eb9SGleb Smirnoff string		: STRING string				{
8643b3a8eb9SGleb Smirnoff 			if (asprintf(&$$, "%s %s", $1, $2) == -1)
8653b3a8eb9SGleb Smirnoff 				err(1, "string: asprintf");
8663b3a8eb9SGleb Smirnoff 			free($1);
8673b3a8eb9SGleb Smirnoff 			free($2);
8683b3a8eb9SGleb Smirnoff 		}
8693b3a8eb9SGleb Smirnoff 		| STRING
8703b3a8eb9SGleb Smirnoff 		;
8713b3a8eb9SGleb Smirnoff 
8723b3a8eb9SGleb Smirnoff varstring	: numberstring varstring 		{
8733b3a8eb9SGleb Smirnoff 			if (asprintf(&$$, "%s %s", $1, $2) == -1)
8743b3a8eb9SGleb Smirnoff 				err(1, "string: asprintf");
8753b3a8eb9SGleb Smirnoff 			free($1);
8763b3a8eb9SGleb Smirnoff 			free($2);
8773b3a8eb9SGleb Smirnoff 		}
8783b3a8eb9SGleb Smirnoff 		| numberstring
8793b3a8eb9SGleb Smirnoff 		;
8803b3a8eb9SGleb Smirnoff 
8813b3a8eb9SGleb Smirnoff numberstring	: NUMBER				{
8823b3a8eb9SGleb Smirnoff 			char	*s;
8833b3a8eb9SGleb Smirnoff 			if (asprintf(&s, "%lld", (long long)$1) == -1) {
8843b3a8eb9SGleb Smirnoff 				yyerror("string: asprintf");
8853b3a8eb9SGleb Smirnoff 				YYERROR;
8863b3a8eb9SGleb Smirnoff 			}
8873b3a8eb9SGleb Smirnoff 			$$ = s;
8883b3a8eb9SGleb Smirnoff 		}
8893b3a8eb9SGleb Smirnoff 		| STRING
8903b3a8eb9SGleb Smirnoff 		;
8913b3a8eb9SGleb Smirnoff 
8923b3a8eb9SGleb Smirnoff varset		: STRING '=' varstring	{
893d3f65324SKristof Provost 			char *s = $1;
8943b3a8eb9SGleb Smirnoff 			if (pf->opts & PF_OPT_VERBOSE)
8953b3a8eb9SGleb Smirnoff 				printf("%s = \"%s\"\n", $1, $3);
896d3f65324SKristof Provost 			while (*s++) {
897d3f65324SKristof Provost 				if (isspace((unsigned char)*s)) {
898d3f65324SKristof Provost 					yyerror("macro name cannot contain "
899d3f65324SKristof Provost 					   "whitespace");
900d3f65324SKristof Provost 					YYERROR;
901d3f65324SKristof Provost 				}
902d3f65324SKristof Provost 			}
9033b3a8eb9SGleb Smirnoff 			if (symset($1, $3, 0) == -1)
9043b3a8eb9SGleb Smirnoff 				err(1, "cannot store variable %s", $1);
9053b3a8eb9SGleb Smirnoff 			free($1);
9063b3a8eb9SGleb Smirnoff 			free($3);
9073b3a8eb9SGleb Smirnoff 		}
9083b3a8eb9SGleb Smirnoff 		;
9093b3a8eb9SGleb Smirnoff 
9103b3a8eb9SGleb Smirnoff anchorname	: STRING			{ $$ = $1; }
9113b3a8eb9SGleb Smirnoff 		| /* empty */			{ $$ = NULL; }
9123b3a8eb9SGleb Smirnoff 		;
9133b3a8eb9SGleb Smirnoff 
9143b3a8eb9SGleb Smirnoff pfa_anchorlist	: /* empty */
9153b3a8eb9SGleb Smirnoff 		| pfa_anchorlist '\n'
9163b3a8eb9SGleb Smirnoff 		| pfa_anchorlist pfrule '\n'
9173b3a8eb9SGleb Smirnoff 		| pfa_anchorlist anchorrule '\n'
9183b3a8eb9SGleb Smirnoff 		;
9193b3a8eb9SGleb Smirnoff 
9203b3a8eb9SGleb Smirnoff pfa_anchor	: '{'
9213b3a8eb9SGleb Smirnoff 		{
9223b3a8eb9SGleb Smirnoff 			char ta[PF_ANCHOR_NAME_SIZE];
923e9eb0941SKristof Provost 			struct pfctl_ruleset *rs;
9243b3a8eb9SGleb Smirnoff 
9252fa6223aSGordon Bergling 			/* stepping into a brace anchor */
9263b3a8eb9SGleb Smirnoff 			pf->asd++;
9273b3a8eb9SGleb Smirnoff 			pf->bn++;
9283b3a8eb9SGleb Smirnoff 
929585a5ed0SKristof Provost 			/*
930585a5ed0SKristof Provost 			* Anchor contents are parsed before the anchor rule
931585a5ed0SKristof Provost 			* production completes, so we don't know the real
932585a5ed0SKristof Provost 			* location yet. Create a holding ruleset in the root;
933585a5ed0SKristof Provost 			* contents will be moved afterwards.
934585a5ed0SKristof Provost 			*/
9353b3a8eb9SGleb Smirnoff 			snprintf(ta, PF_ANCHOR_NAME_SIZE, "_%d", pf->bn);
9363b3a8eb9SGleb Smirnoff 			rs = pf_find_or_create_ruleset(ta);
9373b3a8eb9SGleb Smirnoff 			if (rs == NULL)
9383b3a8eb9SGleb Smirnoff 				err(1, "pfa_anchor: pf_find_or_create_ruleset");
9393b3a8eb9SGleb Smirnoff 			pf->astack[pf->asd] = rs->anchor;
9403b3a8eb9SGleb Smirnoff 			pf->anchor = rs->anchor;
9413b3a8eb9SGleb Smirnoff 		} '\n' pfa_anchorlist '}'
9423b3a8eb9SGleb Smirnoff 		{
9433b3a8eb9SGleb Smirnoff 			pf->alast = pf->anchor;
9443b3a8eb9SGleb Smirnoff 			pf->asd--;
9453b3a8eb9SGleb Smirnoff 			pf->anchor = pf->astack[pf->asd];
9463b3a8eb9SGleb Smirnoff 		}
9473b3a8eb9SGleb Smirnoff 		| /* empty */
9483b3a8eb9SGleb Smirnoff 		;
9493b3a8eb9SGleb Smirnoff 
9503b3a8eb9SGleb Smirnoff anchorrule	: ANCHOR anchorname dir quick interface af proto fromto
9513b3a8eb9SGleb Smirnoff 		    filter_opts pfa_anchor
9523b3a8eb9SGleb Smirnoff 		{
953e9eb0941SKristof Provost 			struct pfctl_rule	r;
9543b3a8eb9SGleb Smirnoff 			struct node_proto	*proto;
9553b3a8eb9SGleb Smirnoff 
9563b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_FILTER)) {
9573b3a8eb9SGleb Smirnoff 				if ($2)
9583b3a8eb9SGleb Smirnoff 					free($2);
9593b3a8eb9SGleb Smirnoff 				YYERROR;
9603b3a8eb9SGleb Smirnoff 			}
9613b3a8eb9SGleb Smirnoff 
9623b3a8eb9SGleb Smirnoff 			if ($2 && ($2[0] == '_' || strstr($2, "/_") != NULL)) {
9633b3a8eb9SGleb Smirnoff 				free($2);
9643b3a8eb9SGleb Smirnoff 				yyerror("anchor names beginning with '_' "
9653b3a8eb9SGleb Smirnoff 				    "are reserved for internal use");
9663b3a8eb9SGleb Smirnoff 				YYERROR;
9673b3a8eb9SGleb Smirnoff 			}
9683b3a8eb9SGleb Smirnoff 
9693b3a8eb9SGleb Smirnoff 			memset(&r, 0, sizeof(r));
9703b3a8eb9SGleb Smirnoff 			if (pf->astack[pf->asd + 1]) {
971585a5ed0SKristof Provost 				if ($2 && strchr($2, '/') != NULL) {
972585a5ed0SKristof Provost 					free($2);
973585a5ed0SKristof Provost 					yyerror("anchor paths containing '/' "
974585a5ed0SKristof Provost 					   "cannot be used for inline anchors.");
975585a5ed0SKristof Provost 					YYERROR;
976585a5ed0SKristof Provost 				}
977585a5ed0SKristof Provost 
978585a5ed0SKristof Provost 				/* Move inline rules into relative location. */
979e9eb0941SKristof Provost 				pfctl_anchor_setup(&r,
9803b3a8eb9SGleb Smirnoff 				    &pf->astack[pf->asd]->ruleset,
9813b3a8eb9SGleb Smirnoff 				    $2 ? $2 : pf->alast->name);
9823b3a8eb9SGleb Smirnoff 
9833b3a8eb9SGleb Smirnoff 				if (r.anchor == NULL)
9843b3a8eb9SGleb Smirnoff 					err(1, "anchorrule: unable to "
9853b3a8eb9SGleb Smirnoff 					    "create ruleset");
9863b3a8eb9SGleb Smirnoff 
9873b3a8eb9SGleb Smirnoff 				if (pf->alast != r.anchor) {
9883b3a8eb9SGleb Smirnoff 					if (r.anchor->match) {
9893b3a8eb9SGleb Smirnoff 						yyerror("inline anchor '%s' "
9903b3a8eb9SGleb Smirnoff 						    "already exists",
9913b3a8eb9SGleb Smirnoff 						    r.anchor->name);
9923b3a8eb9SGleb Smirnoff 						YYERROR;
9933b3a8eb9SGleb Smirnoff 					}
9943b3a8eb9SGleb Smirnoff 					mv_rules(&pf->alast->ruleset,
9953b3a8eb9SGleb Smirnoff 					    &r.anchor->ruleset);
9963b3a8eb9SGleb Smirnoff 				}
9973b3a8eb9SGleb Smirnoff 				pf_remove_if_empty_ruleset(&pf->alast->ruleset);
9983b3a8eb9SGleb Smirnoff 				pf->alast = r.anchor;
9993b3a8eb9SGleb Smirnoff 			} else {
10003b3a8eb9SGleb Smirnoff 				if (!$2) {
10013b3a8eb9SGleb Smirnoff 					yyerror("anchors without explicit "
10023b3a8eb9SGleb Smirnoff 					    "rules must specify a name");
10033b3a8eb9SGleb Smirnoff 					YYERROR;
10043b3a8eb9SGleb Smirnoff 				}
10053b3a8eb9SGleb Smirnoff 			}
10063b3a8eb9SGleb Smirnoff 			r.direction = $3;
10073b3a8eb9SGleb Smirnoff 			r.quick = $4.quick;
10083b3a8eb9SGleb Smirnoff 			r.af = $6;
10093b3a8eb9SGleb Smirnoff 			r.prob = $9.prob;
10103b3a8eb9SGleb Smirnoff 			r.rtableid = $9.rtableid;
101176c5eeccSKristof Provost 			r.ridentifier = $9.ridentifier;
10123b3a8eb9SGleb Smirnoff 
10133b3a8eb9SGleb Smirnoff 			if ($9.tag)
10143b3a8eb9SGleb Smirnoff 				if (strlcpy(r.tagname, $9.tag,
10153b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
10163b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
10173b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
10183b3a8eb9SGleb Smirnoff 					YYERROR;
10193b3a8eb9SGleb Smirnoff 				}
10203b3a8eb9SGleb Smirnoff 			if ($9.match_tag)
10213b3a8eb9SGleb Smirnoff 				if (strlcpy(r.match_tagname, $9.match_tag,
10223b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
10233b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
10243b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
10253b3a8eb9SGleb Smirnoff 					YYERROR;
10263b3a8eb9SGleb Smirnoff 				}
10273b3a8eb9SGleb Smirnoff 			r.match_tag_not = $9.match_tag_not;
10283b3a8eb9SGleb Smirnoff 			if (rule_label(&r, $9.label))
10293b3a8eb9SGleb Smirnoff 				YYERROR;
10306fcc8e04SKristof Provost 			for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++)
10316fcc8e04SKristof Provost 				free($9.label[i]);
10323b3a8eb9SGleb Smirnoff 			r.flags = $9.flags.b1;
10333b3a8eb9SGleb Smirnoff 			r.flagset = $9.flags.b2;
10343b3a8eb9SGleb Smirnoff 			if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
10353b3a8eb9SGleb Smirnoff 				yyerror("flags always false");
10363b3a8eb9SGleb Smirnoff 				YYERROR;
10373b3a8eb9SGleb Smirnoff 			}
10383b3a8eb9SGleb Smirnoff 			if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
10393b3a8eb9SGleb Smirnoff 				for (proto = $7; proto != NULL &&
10403b3a8eb9SGleb Smirnoff 				    proto->proto != IPPROTO_TCP;
10413b3a8eb9SGleb Smirnoff 				    proto = proto->next)
10423b3a8eb9SGleb Smirnoff 					;	/* nothing */
10433b3a8eb9SGleb Smirnoff 				if (proto == NULL && $7 != NULL) {
10443b3a8eb9SGleb Smirnoff 					if ($9.flags.b1 || $9.flags.b2)
10453b3a8eb9SGleb Smirnoff 						yyerror(
10463b3a8eb9SGleb Smirnoff 						    "flags only apply to tcp");
10473b3a8eb9SGleb Smirnoff 					if ($8.src_os)
10483b3a8eb9SGleb Smirnoff 						yyerror(
10493b3a8eb9SGleb Smirnoff 						    "OS fingerprinting only "
10503b3a8eb9SGleb Smirnoff 						    "applies to tcp");
10513b3a8eb9SGleb Smirnoff 					YYERROR;
10523b3a8eb9SGleb Smirnoff 				}
10533b3a8eb9SGleb Smirnoff 			}
10543b3a8eb9SGleb Smirnoff 
10553b3a8eb9SGleb Smirnoff 			r.tos = $9.tos;
10563b3a8eb9SGleb Smirnoff 
10573b3a8eb9SGleb Smirnoff 			if ($9.keep.action) {
10583b3a8eb9SGleb Smirnoff 				yyerror("cannot specify state handling "
10593b3a8eb9SGleb Smirnoff 				    "on anchors");
10603b3a8eb9SGleb Smirnoff 				YYERROR;
10613b3a8eb9SGleb Smirnoff 			}
10623b3a8eb9SGleb Smirnoff 
10633b3a8eb9SGleb Smirnoff 			if ($9.match_tag)
10643b3a8eb9SGleb Smirnoff 				if (strlcpy(r.match_tagname, $9.match_tag,
10653b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
10663b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
10673b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
10683b3a8eb9SGleb Smirnoff 					YYERROR;
10693b3a8eb9SGleb Smirnoff 				}
10703b3a8eb9SGleb Smirnoff 			r.match_tag_not = $9.match_tag_not;
10713e248e0fSKristof Provost 			if ($9.marker & FOM_PRIO) {
10723e248e0fSKristof Provost 				if ($9.prio == 0)
10733e248e0fSKristof Provost 					r.prio = PF_PRIO_ZERO;
10743e248e0fSKristof Provost 				else
10753e248e0fSKristof Provost 					r.prio = $9.prio;
10763e248e0fSKristof Provost 			}
10773e248e0fSKristof Provost 			if ($9.marker & FOM_SETPRIO) {
10783e248e0fSKristof Provost 				r.set_prio[0] = $9.set_prio[0];
10793e248e0fSKristof Provost 				r.set_prio[1] = $9.set_prio[1];
10803e248e0fSKristof Provost 				r.scrub_flags |= PFSTATE_SETPRIO;
10813e248e0fSKristof Provost 			}
10823b3a8eb9SGleb Smirnoff 
10833b3a8eb9SGleb Smirnoff 			decide_address_family($8.src.host, &r.af);
10843b3a8eb9SGleb Smirnoff 			decide_address_family($8.dst.host, &r.af);
1085*aa69fdf1SKristof Provost 			r.naf = r.af;
10863b3a8eb9SGleb Smirnoff 
1087*aa69fdf1SKristof Provost 			expand_rule(&r, $5, NULL, NULL, $7, $8.src_os,
10883b3a8eb9SGleb Smirnoff 			    $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
10892339ead6SKristof Provost 			    $9.uid, $9.gid, $9.rcv, $9.icmpspec,
10903b3a8eb9SGleb Smirnoff 			    pf->astack[pf->asd + 1] ? pf->alast->name : $2);
10913b3a8eb9SGleb Smirnoff 			free($2);
10923b3a8eb9SGleb Smirnoff 			pf->astack[pf->asd + 1] = NULL;
10933b3a8eb9SGleb Smirnoff 		}
10943b3a8eb9SGleb Smirnoff 		| NATANCHOR string interface af proto fromto rtable {
1095e9eb0941SKristof Provost 			struct pfctl_rule	r;
10963b3a8eb9SGleb Smirnoff 
10973b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_NAT)) {
10983b3a8eb9SGleb Smirnoff 				free($2);
10993b3a8eb9SGleb Smirnoff 				YYERROR;
11003b3a8eb9SGleb Smirnoff 			}
11013b3a8eb9SGleb Smirnoff 
11023b3a8eb9SGleb Smirnoff 			memset(&r, 0, sizeof(r));
11033b3a8eb9SGleb Smirnoff 			r.action = PF_NAT;
11043b3a8eb9SGleb Smirnoff 			r.af = $4;
11053b3a8eb9SGleb Smirnoff 			r.rtableid = $7;
11063b3a8eb9SGleb Smirnoff 
11073b3a8eb9SGleb Smirnoff 			decide_address_family($6.src.host, &r.af);
11083b3a8eb9SGleb Smirnoff 			decide_address_family($6.dst.host, &r.af);
11093b3a8eb9SGleb Smirnoff 
1110*aa69fdf1SKristof Provost 			expand_rule(&r, $3, NULL, NULL, $5, $6.src_os,
11113b3a8eb9SGleb Smirnoff 			    $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
11122339ead6SKristof Provost 			    0, 0, 0, 0, $2);
11133b3a8eb9SGleb Smirnoff 			free($2);
11143b3a8eb9SGleb Smirnoff 		}
11153b3a8eb9SGleb Smirnoff 		| RDRANCHOR string interface af proto fromto rtable {
1116e9eb0941SKristof Provost 			struct pfctl_rule	r;
11173b3a8eb9SGleb Smirnoff 
11183b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_NAT)) {
11193b3a8eb9SGleb Smirnoff 				free($2);
11203b3a8eb9SGleb Smirnoff 				YYERROR;
11213b3a8eb9SGleb Smirnoff 			}
11223b3a8eb9SGleb Smirnoff 
11233b3a8eb9SGleb Smirnoff 			memset(&r, 0, sizeof(r));
11243b3a8eb9SGleb Smirnoff 			r.action = PF_RDR;
11253b3a8eb9SGleb Smirnoff 			r.af = $4;
11263b3a8eb9SGleb Smirnoff 			r.rtableid = $7;
11273b3a8eb9SGleb Smirnoff 
11283b3a8eb9SGleb Smirnoff 			decide_address_family($6.src.host, &r.af);
11293b3a8eb9SGleb Smirnoff 			decide_address_family($6.dst.host, &r.af);
11303b3a8eb9SGleb Smirnoff 
11313b3a8eb9SGleb Smirnoff 			if ($6.src.port != NULL) {
11323b3a8eb9SGleb Smirnoff 				yyerror("source port parameter not supported"
11333b3a8eb9SGleb Smirnoff 				    " in rdr-anchor");
11343b3a8eb9SGleb Smirnoff 				YYERROR;
11353b3a8eb9SGleb Smirnoff 			}
11363b3a8eb9SGleb Smirnoff 			if ($6.dst.port != NULL) {
11373b3a8eb9SGleb Smirnoff 				if ($6.dst.port->next != NULL) {
11383b3a8eb9SGleb Smirnoff 					yyerror("destination port list "
11393b3a8eb9SGleb Smirnoff 					    "expansion not supported in "
11403b3a8eb9SGleb Smirnoff 					    "rdr-anchor");
11413b3a8eb9SGleb Smirnoff 					YYERROR;
11423b3a8eb9SGleb Smirnoff 				} else if ($6.dst.port->op != PF_OP_EQ) {
11433b3a8eb9SGleb Smirnoff 					yyerror("destination port operators"
11443b3a8eb9SGleb Smirnoff 					    " not supported in rdr-anchor");
11453b3a8eb9SGleb Smirnoff 					YYERROR;
11463b3a8eb9SGleb Smirnoff 				}
11473b3a8eb9SGleb Smirnoff 				r.dst.port[0] = $6.dst.port->port[0];
11483b3a8eb9SGleb Smirnoff 				r.dst.port[1] = $6.dst.port->port[1];
11493b3a8eb9SGleb Smirnoff 				r.dst.port_op = $6.dst.port->op;
11503b3a8eb9SGleb Smirnoff 			}
11513b3a8eb9SGleb Smirnoff 
1152*aa69fdf1SKristof Provost 			expand_rule(&r, $3, NULL, NULL, $5, $6.src_os,
11533b3a8eb9SGleb Smirnoff 			    $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
11542339ead6SKristof Provost 			    0, 0, 0, 0, $2);
11553b3a8eb9SGleb Smirnoff 			free($2);
11563b3a8eb9SGleb Smirnoff 		}
11573b3a8eb9SGleb Smirnoff 		| BINATANCHOR string interface af proto fromto rtable {
1158e9eb0941SKristof Provost 			struct pfctl_rule	r;
11593b3a8eb9SGleb Smirnoff 
11603b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_NAT)) {
11613b3a8eb9SGleb Smirnoff 				free($2);
11623b3a8eb9SGleb Smirnoff 				YYERROR;
11633b3a8eb9SGleb Smirnoff 			}
11643b3a8eb9SGleb Smirnoff 
11653b3a8eb9SGleb Smirnoff 			memset(&r, 0, sizeof(r));
11663b3a8eb9SGleb Smirnoff 			r.action = PF_BINAT;
11673b3a8eb9SGleb Smirnoff 			r.af = $4;
11683b3a8eb9SGleb Smirnoff 			r.rtableid = $7;
11693b3a8eb9SGleb Smirnoff 			if ($5 != NULL) {
11703b3a8eb9SGleb Smirnoff 				if ($5->next != NULL) {
11713b3a8eb9SGleb Smirnoff 					yyerror("proto list expansion"
11723b3a8eb9SGleb Smirnoff 					    " not supported in binat-anchor");
11733b3a8eb9SGleb Smirnoff 					YYERROR;
11743b3a8eb9SGleb Smirnoff 				}
11753b3a8eb9SGleb Smirnoff 				r.proto = $5->proto;
11763b3a8eb9SGleb Smirnoff 				free($5);
11773b3a8eb9SGleb Smirnoff 			}
11783b3a8eb9SGleb Smirnoff 
11793b3a8eb9SGleb Smirnoff 			if ($6.src.host != NULL || $6.src.port != NULL ||
11803b3a8eb9SGleb Smirnoff 			    $6.dst.host != NULL || $6.dst.port != NULL) {
11813b3a8eb9SGleb Smirnoff 				yyerror("fromto parameter not supported"
11823b3a8eb9SGleb Smirnoff 				    " in binat-anchor");
11833b3a8eb9SGleb Smirnoff 				YYERROR;
11843b3a8eb9SGleb Smirnoff 			}
11853b3a8eb9SGleb Smirnoff 
11863b3a8eb9SGleb Smirnoff 			decide_address_family($6.src.host, &r.af);
11873b3a8eb9SGleb Smirnoff 			decide_address_family($6.dst.host, &r.af);
11883b3a8eb9SGleb Smirnoff 
11890d71f9f3SKristof Provost 			pfctl_append_rule(pf, &r, $2);
11903b3a8eb9SGleb Smirnoff 			free($2);
11913b3a8eb9SGleb Smirnoff 		}
11923b3a8eb9SGleb Smirnoff 		;
11933b3a8eb9SGleb Smirnoff 
11943b3a8eb9SGleb Smirnoff loadrule	: LOAD ANCHOR string FROM string	{
11953b3a8eb9SGleb Smirnoff 			struct loadanchors	*loadanchor;
11963b3a8eb9SGleb Smirnoff 
11973b3a8eb9SGleb Smirnoff 			if (strlen(pf->anchor->name) + 1 +
11983b3a8eb9SGleb Smirnoff 			    strlen($3) >= MAXPATHLEN) {
11993b3a8eb9SGleb Smirnoff 				yyerror("anchorname %s too long, max %u\n",
12003b3a8eb9SGleb Smirnoff 				    $3, MAXPATHLEN - 1);
12013b3a8eb9SGleb Smirnoff 				free($3);
12023b3a8eb9SGleb Smirnoff 				YYERROR;
12033b3a8eb9SGleb Smirnoff 			}
12043b3a8eb9SGleb Smirnoff 			loadanchor = calloc(1, sizeof(struct loadanchors));
12053b3a8eb9SGleb Smirnoff 			if (loadanchor == NULL)
12063b3a8eb9SGleb Smirnoff 				err(1, "loadrule: calloc");
12073b3a8eb9SGleb Smirnoff 			if ((loadanchor->anchorname = malloc(MAXPATHLEN)) ==
12083b3a8eb9SGleb Smirnoff 			    NULL)
12093b3a8eb9SGleb Smirnoff 				err(1, "loadrule: malloc");
12103b3a8eb9SGleb Smirnoff 			if (pf->anchor->name[0])
12113b3a8eb9SGleb Smirnoff 				snprintf(loadanchor->anchorname, MAXPATHLEN,
12123b3a8eb9SGleb Smirnoff 				    "%s/%s", pf->anchor->name, $3);
12133b3a8eb9SGleb Smirnoff 			else
12143b3a8eb9SGleb Smirnoff 				strlcpy(loadanchor->anchorname, $3, MAXPATHLEN);
12153b3a8eb9SGleb Smirnoff 			if ((loadanchor->filename = strdup($5)) == NULL)
12163b3a8eb9SGleb Smirnoff 				err(1, "loadrule: strdup");
12173b3a8eb9SGleb Smirnoff 
12183b3a8eb9SGleb Smirnoff 			TAILQ_INSERT_TAIL(&loadanchorshead, loadanchor,
12193b3a8eb9SGleb Smirnoff 			    entries);
12203b3a8eb9SGleb Smirnoff 
12213b3a8eb9SGleb Smirnoff 			free($3);
12223b3a8eb9SGleb Smirnoff 			free($5);
12233b3a8eb9SGleb Smirnoff 		};
12243b3a8eb9SGleb Smirnoff 
12253b3a8eb9SGleb Smirnoff scrubaction	: no SCRUB {
12263b3a8eb9SGleb Smirnoff 			$$.b2 = $$.w = 0;
12273b3a8eb9SGleb Smirnoff 			if ($1)
12283b3a8eb9SGleb Smirnoff 				$$.b1 = PF_NOSCRUB;
12293b3a8eb9SGleb Smirnoff 			else
12303b3a8eb9SGleb Smirnoff 				$$.b1 = PF_SCRUB;
12313b3a8eb9SGleb Smirnoff 		}
12323b3a8eb9SGleb Smirnoff 		;
12333b3a8eb9SGleb Smirnoff 
12348a8af942SKristof Provost etherrule	: ETHER action dir quick interface bridge etherproto etherfromto l3fromto etherfilter_opts
12352b29ceb8SKristof Provost 		{
12362b29ceb8SKristof Provost 			struct pfctl_eth_rule	r;
12372b29ceb8SKristof Provost 
12382b29ceb8SKristof Provost 			bzero(&r, sizeof(r));
12392b29ceb8SKristof Provost 
12402b29ceb8SKristof Provost 			if (check_rulestate(PFCTL_STATE_ETHER))
12412b29ceb8SKristof Provost 				YYERROR;
12422b29ceb8SKristof Provost 
12432b29ceb8SKristof Provost 			r.action = $2.b1;
12442b29ceb8SKristof Provost 			r.direction = $3;
12452b29ceb8SKristof Provost 			r.quick = $4.quick;
12468a8af942SKristof Provost 			if ($10.tag != NULL)
1247dc3ee89cSKristof Provost 				strlcpy(r.tagname, $10.tag, sizeof(r.tagname));
12488a8af942SKristof Provost 			if ($10.match_tag)
12498a8af942SKristof Provost 				if (strlcpy(r.match_tagname, $10.match_tag,
12501f61367fSKristof Provost 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
12511f61367fSKristof Provost 					yyerror("tag too long, max %u chars",
12521f61367fSKristof Provost 					    PF_TAG_NAME_SIZE - 1);
12531f61367fSKristof Provost 					YYERROR;
12541f61367fSKristof Provost 				}
12558a8af942SKristof Provost 			r.match_tag_not = $10.match_tag_not;
12568a8af942SKristof Provost 			if ($10.queues.qname != NULL)
1257dc3ee89cSKristof Provost 				strlcpy(r.qname, $10.queues.qname, sizeof(r.qname));
12588a8af942SKristof Provost 			r.dnpipe = $10.dnpipe;
12598a8af942SKristof Provost 			r.dnflags = $10.free_flags;
1260ef661d4aSChristian McDonald 			if (eth_rule_label(&r, $10.label))
1261ef661d4aSChristian McDonald 				YYERROR;
1262ef661d4aSChristian McDonald 			for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++)
1263ef661d4aSChristian McDonald 				free($10.label[i]);
1264ef661d4aSChristian McDonald 			r.ridentifier = $10.ridentifier;
12652b29ceb8SKristof Provost 
12668a8af942SKristof Provost 			expand_eth_rule(&r, $5, $7, $8.src, $8.dst,
12678a8af942SKristof Provost 			    $9.src.host, $9.dst.host, $6, "");
1268c5131afeSKristof Provost 		}
1269c5131afeSKristof Provost 		;
1270c5131afeSKristof Provost 
1271c5131afeSKristof Provost etherpfa_anchorlist	: /* empty */
1272c5131afeSKristof Provost 		| etherpfa_anchorlist '\n'
1273c5131afeSKristof Provost 		| etherpfa_anchorlist etherrule '\n'
1274c5131afeSKristof Provost 		| etherpfa_anchorlist etheranchorrule '\n'
1275c5131afeSKristof Provost 		;
1276c5131afeSKristof Provost 
1277c5131afeSKristof Provost etherpfa_anchor	: '{'
1278c5131afeSKristof Provost 		{
1279c5131afeSKristof Provost 			char ta[PF_ANCHOR_NAME_SIZE];
1280c5131afeSKristof Provost 			struct pfctl_eth_ruleset *rs;
1281c5131afeSKristof Provost 
1282c5131afeSKristof Provost 			/* steping into a brace anchor */
1283c5131afeSKristof Provost 			pf->asd++;
1284c5131afeSKristof Provost 			pf->bn++;
1285c5131afeSKristof Provost 
1286c5131afeSKristof Provost 			/* create a holding ruleset in the root */
1287c5131afeSKristof Provost 			snprintf(ta, PF_ANCHOR_NAME_SIZE, "_%d", pf->bn);
1288c5131afeSKristof Provost 			rs = pf_find_or_create_eth_ruleset(ta);
1289c5131afeSKristof Provost 			if (rs == NULL)
1290c5131afeSKristof Provost 				err(1, "etherpfa_anchor: pf_find_or_create_eth_ruleset");
1291c5131afeSKristof Provost 			pf->eastack[pf->asd] = rs->anchor;
1292c5131afeSKristof Provost 			pf->eanchor = rs->anchor;
1293c5131afeSKristof Provost 		} '\n' etherpfa_anchorlist '}'
1294c5131afeSKristof Provost 		{
1295c5131afeSKristof Provost 			pf->ealast = pf->eanchor;
1296c5131afeSKristof Provost 			pf->asd--;
1297c5131afeSKristof Provost 			pf->eanchor = pf->eastack[pf->asd];
1298c5131afeSKristof Provost 		}
1299c5131afeSKristof Provost 		| /* empty */
1300c5131afeSKristof Provost 		;
1301c5131afeSKristof Provost 
13028a42005dSKristof Provost etheranchorrule	: ETHER ANCHOR anchorname dir quick interface etherproto etherfromto l3fromto etherpfa_anchor
1303c5131afeSKristof Provost 		{
1304c5131afeSKristof Provost 			struct pfctl_eth_rule	r;
1305c5131afeSKristof Provost 
1306c5131afeSKristof Provost 			if (check_rulestate(PFCTL_STATE_ETHER)) {
1307c5131afeSKristof Provost 				free($3);
1308c5131afeSKristof Provost 				YYERROR;
1309c5131afeSKristof Provost 			}
1310c5131afeSKristof Provost 
1311c5131afeSKristof Provost 			if ($3 && ($3[0] == '_' || strstr($3, "/_") != NULL)) {
1312c5131afeSKristof Provost 				free($3);
1313c5131afeSKristof Provost 				yyerror("anchor names beginning with '_' "
1314c5131afeSKristof Provost 				    "are reserved for internal use");
1315c5131afeSKristof Provost 				YYERROR;
1316c5131afeSKristof Provost 			}
1317c5131afeSKristof Provost 
1318c5131afeSKristof Provost 			memset(&r, 0, sizeof(r));
1319c5131afeSKristof Provost 			if (pf->eastack[pf->asd + 1]) {
1320cfa1a130SKristof Provost 				if ($3 && strchr($3, '/') != NULL) {
1321cfa1a130SKristof Provost 					free($3);
1322cfa1a130SKristof Provost 					yyerror("anchor paths containing '/' "
1323cfa1a130SKristof Provost 					   "cannot be used for inline anchors.");
1324cfa1a130SKristof Provost 					YYERROR;
1325cfa1a130SKristof Provost 				}
1326cfa1a130SKristof Provost 
1327cfa1a130SKristof Provost 				/* Move inline rules into relative location. */
1328c5131afeSKristof Provost 				pfctl_eth_anchor_setup(pf, &r,
1329c5131afeSKristof Provost 				    &pf->eastack[pf->asd]->ruleset,
1330c5131afeSKristof Provost 				    $3 ? $3 : pf->ealast->name);
1331c5131afeSKristof Provost 				if (r.anchor == NULL)
1332c5131afeSKristof Provost 					err(1, "etheranchorrule: unable to "
1333c5131afeSKristof Provost 					    "create ruleset");
1334c5131afeSKristof Provost 
1335c5131afeSKristof Provost 				if (pf->ealast != r.anchor) {
1336c5131afeSKristof Provost 					if (r.anchor->match) {
1337c5131afeSKristof Provost 						yyerror("inline anchor '%s' "
1338c5131afeSKristof Provost 						    "already exists",
1339c5131afeSKristof Provost 						    r.anchor->name);
1340c5131afeSKristof Provost 						YYERROR;
1341c5131afeSKristof Provost 					}
1342c5131afeSKristof Provost 					mv_eth_rules(&pf->ealast->ruleset,
1343c5131afeSKristof Provost 					    &r.anchor->ruleset);
1344c5131afeSKristof Provost 				}
1345c5131afeSKristof Provost 				pf_remove_if_empty_eth_ruleset(&pf->ealast->ruleset);
1346c5131afeSKristof Provost 				pf->ealast = r.anchor;
1347c5131afeSKristof Provost 			} else {
1348c5131afeSKristof Provost 				if (!$3) {
1349c5131afeSKristof Provost 					yyerror("anchors without explicit "
1350c5131afeSKristof Provost 					    "rules must specify a name");
1351c5131afeSKristof Provost 					YYERROR;
1352c5131afeSKristof Provost 				}
1353c5131afeSKristof Provost 			}
1354c5131afeSKristof Provost 
1355c5131afeSKristof Provost 			r.direction = $4;
1356c5131afeSKristof Provost 			r.quick = $5.quick;
1357c5131afeSKristof Provost 
1358c5131afeSKristof Provost 			expand_eth_rule(&r, $6, $7, $8.src, $8.dst,
13598a8af942SKristof Provost 			    $9.src.host, $9.dst.host, NULL,
1360c5131afeSKristof Provost 			    pf->eastack[pf->asd + 1] ? pf->ealast->name : $3);
1361c5131afeSKristof Provost 
1362c5131afeSKristof Provost 			free($3);
1363c5131afeSKristof Provost 			pf->eastack[pf->asd + 1] = NULL;
13642b29ceb8SKristof Provost 		}
13652b29ceb8SKristof Provost 		;
13662b29ceb8SKristof Provost 
13672b29ceb8SKristof Provost etherfilter_opts	:	{
13682b29ceb8SKristof Provost 				bzero(&filter_opts, sizeof filter_opts);
13692b29ceb8SKristof Provost 			}
13702b29ceb8SKristof Provost 		    etherfilter_opts_l
13712b29ceb8SKristof Provost 			{ $$ = filter_opts; }
13722b29ceb8SKristof Provost 		| /* empty */	{
13732b29ceb8SKristof Provost 			bzero(&filter_opts, sizeof filter_opts);
13742b29ceb8SKristof Provost 			$$ = filter_opts;
13752b29ceb8SKristof Provost 		}
13762b29ceb8SKristof Provost 		;
13772b29ceb8SKristof Provost 
13782b29ceb8SKristof Provost etherfilter_opts_l	: etherfilter_opts_l etherfilter_opt
13792b29ceb8SKristof Provost 			| etherfilter_opt
13802b29ceb8SKristof Provost 
13812b29ceb8SKristof Provost etherfilter_opt	: etherqname	{
13822b29ceb8SKristof Provost 			if (filter_opts.queues.qname) {
13832b29ceb8SKristof Provost 				yyerror("queue cannot be redefined");
13842b29ceb8SKristof Provost 				YYERROR;
13852b29ceb8SKristof Provost 			}
13862b29ceb8SKristof Provost 			filter_opts.queues = $1;
13872b29ceb8SKristof Provost 		}
1388ef661d4aSChristian McDonald 		| RIDENTIFIER number {
1389ef661d4aSChristian McDonald 			filter_opts.ridentifier = $2;
1390ef661d4aSChristian McDonald 		}
1391ef661d4aSChristian McDonald 		| label	{
1392ef661d4aSChristian McDonald 			if (filter_opts.labelcount >= PF_RULE_MAX_LABEL_COUNT) {
1393ef661d4aSChristian McDonald 				yyerror("label can only be used %d times", PF_RULE_MAX_LABEL_COUNT);
1394ef661d4aSChristian McDonald 				YYERROR;
1395ef661d4aSChristian McDonald 			}
1396ef661d4aSChristian McDonald 			filter_opts.label[filter_opts.labelcount++] = $1;
1397ef661d4aSChristian McDonald 		}
13982b29ceb8SKristof Provost 		| TAG string				{
13992b29ceb8SKristof Provost 			filter_opts.tag = $2;
14002b29ceb8SKristof Provost 		}
14011f61367fSKristof Provost 		| not TAGGED string			{
14021f61367fSKristof Provost 			filter_opts.match_tag = $3;
14031f61367fSKristof Provost 			filter_opts.match_tag_not = $1;
14041f61367fSKristof Provost 		}
1405fb330f39SKristof Provost 		| DNPIPE number {
1406fb330f39SKristof Provost 			filter_opts.dnpipe = $2;
1407fb330f39SKristof Provost 			filter_opts.free_flags |= PFRULE_DN_IS_PIPE;
1408fb330f39SKristof Provost 		}
1409fb330f39SKristof Provost 		| DNQUEUE number {
1410fb330f39SKristof Provost 			filter_opts.dnpipe = $2;
1411fb330f39SKristof Provost 			filter_opts.free_flags |= PFRULE_DN_IS_QUEUE;
1412fb330f39SKristof Provost 		}
14132b29ceb8SKristof Provost 		;
14142b29ceb8SKristof Provost 
14158a8af942SKristof Provost bridge		:	/* empty */		{
14168a8af942SKristof Provost 			$$ = NULL;
14178a8af942SKristof Provost 		}
14188a8af942SKristof Provost 		| BRIDGE_TO STRING {
14198a8af942SKristof Provost 			$$ = strdup($2);
14208a8af942SKristof Provost 		}
14218a8af942SKristof Provost 		;
14228a8af942SKristof Provost 
14233b3a8eb9SGleb Smirnoff scrubrule	: scrubaction dir logquick interface af proto fromto scrub_opts
14243b3a8eb9SGleb Smirnoff 		{
1425e9eb0941SKristof Provost 			struct pfctl_rule	r;
14263b3a8eb9SGleb Smirnoff 
14273b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_SCRUB))
14283b3a8eb9SGleb Smirnoff 				YYERROR;
14293b3a8eb9SGleb Smirnoff 
14303b3a8eb9SGleb Smirnoff 			memset(&r, 0, sizeof(r));
14313b3a8eb9SGleb Smirnoff 
14323b3a8eb9SGleb Smirnoff 			r.action = $1.b1;
14333b3a8eb9SGleb Smirnoff 			r.direction = $2;
14343b3a8eb9SGleb Smirnoff 
14353b3a8eb9SGleb Smirnoff 			r.log = $3.log;
14363b3a8eb9SGleb Smirnoff 			r.logif = $3.logif;
14373b3a8eb9SGleb Smirnoff 			if ($3.quick) {
14383b3a8eb9SGleb Smirnoff 				yyerror("scrub rules do not support 'quick'");
14393b3a8eb9SGleb Smirnoff 				YYERROR;
14403b3a8eb9SGleb Smirnoff 			}
14413b3a8eb9SGleb Smirnoff 
14423b3a8eb9SGleb Smirnoff 			r.af = $5;
14433b3a8eb9SGleb Smirnoff 			if ($8.nodf)
14443b3a8eb9SGleb Smirnoff 				r.rule_flag |= PFRULE_NODF;
14453b3a8eb9SGleb Smirnoff 			if ($8.randomid)
14463b3a8eb9SGleb Smirnoff 				r.rule_flag |= PFRULE_RANDOMID;
14473b3a8eb9SGleb Smirnoff 			if ($8.reassemble_tcp) {
14483b3a8eb9SGleb Smirnoff 				if (r.direction != PF_INOUT) {
14493b3a8eb9SGleb Smirnoff 					yyerror("reassemble tcp rules can not "
14503b3a8eb9SGleb Smirnoff 					    "specify direction");
14513b3a8eb9SGleb Smirnoff 					YYERROR;
14523b3a8eb9SGleb Smirnoff 				}
14533b3a8eb9SGleb Smirnoff 				r.rule_flag |= PFRULE_REASSEMBLE_TCP;
14543b3a8eb9SGleb Smirnoff 			}
14553b3a8eb9SGleb Smirnoff 			if ($8.minttl)
14563b3a8eb9SGleb Smirnoff 				r.min_ttl = $8.minttl;
14573b3a8eb9SGleb Smirnoff 			if ($8.maxmss)
14583b3a8eb9SGleb Smirnoff 				r.max_mss = $8.maxmss;
145939282ef3SKajetan Staszkiewicz 			if ($8.marker & FOM_SETTOS) {
14603b3a8eb9SGleb Smirnoff 				r.rule_flag |= PFRULE_SET_TOS;
14613b3a8eb9SGleb Smirnoff 				r.set_tos = $8.settos;
14623b3a8eb9SGleb Smirnoff 			}
14633b3a8eb9SGleb Smirnoff 			if ($8.fragcache)
14643b3a8eb9SGleb Smirnoff 				r.rule_flag |= $8.fragcache;
14653b3a8eb9SGleb Smirnoff 			if ($8.match_tag)
14663b3a8eb9SGleb Smirnoff 				if (strlcpy(r.match_tagname, $8.match_tag,
14673b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
14683b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
14693b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
14703b3a8eb9SGleb Smirnoff 					YYERROR;
14713b3a8eb9SGleb Smirnoff 				}
14723b3a8eb9SGleb Smirnoff 			r.match_tag_not = $8.match_tag_not;
14733b3a8eb9SGleb Smirnoff 			r.rtableid = $8.rtableid;
14743b3a8eb9SGleb Smirnoff 
1475*aa69fdf1SKristof Provost 			expand_rule(&r, $4, NULL, NULL, $6, $7.src_os,
14763b3a8eb9SGleb Smirnoff 			    $7.src.host, $7.src.port, $7.dst.host, $7.dst.port,
14772339ead6SKristof Provost 			    NULL, NULL, NULL, NULL, "");
14783b3a8eb9SGleb Smirnoff 		}
14793b3a8eb9SGleb Smirnoff 		;
14803b3a8eb9SGleb Smirnoff 
14813b3a8eb9SGleb Smirnoff scrub_opts	:	{
14823b3a8eb9SGleb Smirnoff 				bzero(&scrub_opts, sizeof scrub_opts);
14833b3a8eb9SGleb Smirnoff 				scrub_opts.rtableid = -1;
14843b3a8eb9SGleb Smirnoff 			}
14853b3a8eb9SGleb Smirnoff 		    scrub_opts_l
14863b3a8eb9SGleb Smirnoff 			{ $$ = scrub_opts; }
14873b3a8eb9SGleb Smirnoff 		| /* empty */ {
14883b3a8eb9SGleb Smirnoff 			bzero(&scrub_opts, sizeof scrub_opts);
14893b3a8eb9SGleb Smirnoff 			scrub_opts.rtableid = -1;
14903b3a8eb9SGleb Smirnoff 			$$ = scrub_opts;
14913b3a8eb9SGleb Smirnoff 		}
14923b3a8eb9SGleb Smirnoff 		;
14933b3a8eb9SGleb Smirnoff 
149439282ef3SKajetan Staszkiewicz scrub_opts_l	: scrub_opts_l comma scrub_opt
14953b3a8eb9SGleb Smirnoff 		| scrub_opt
14963b3a8eb9SGleb Smirnoff 		;
14973b3a8eb9SGleb Smirnoff 
14983b3a8eb9SGleb Smirnoff scrub_opt	: NODF	{
14993b3a8eb9SGleb Smirnoff 			if (scrub_opts.nodf) {
15003b3a8eb9SGleb Smirnoff 				yyerror("no-df cannot be respecified");
15013b3a8eb9SGleb Smirnoff 				YYERROR;
15023b3a8eb9SGleb Smirnoff 			}
15033b3a8eb9SGleb Smirnoff 			scrub_opts.nodf = 1;
15043b3a8eb9SGleb Smirnoff 		}
15053b3a8eb9SGleb Smirnoff 		| MINTTL NUMBER {
150639282ef3SKajetan Staszkiewicz 			if (scrub_opts.marker & FOM_MINTTL) {
15073b3a8eb9SGleb Smirnoff 				yyerror("min-ttl cannot be respecified");
15083b3a8eb9SGleb Smirnoff 				YYERROR;
15093b3a8eb9SGleb Smirnoff 			}
15103b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > 255) {
15113b3a8eb9SGleb Smirnoff 				yyerror("illegal min-ttl value %d", $2);
15123b3a8eb9SGleb Smirnoff 				YYERROR;
15133b3a8eb9SGleb Smirnoff 			}
151439282ef3SKajetan Staszkiewicz 			scrub_opts.marker |= FOM_MINTTL;
15153b3a8eb9SGleb Smirnoff 			scrub_opts.minttl = $2;
15163b3a8eb9SGleb Smirnoff 		}
15173b3a8eb9SGleb Smirnoff 		| MAXMSS NUMBER {
151839282ef3SKajetan Staszkiewicz 			if (scrub_opts.marker & FOM_MAXMSS) {
15193b3a8eb9SGleb Smirnoff 				yyerror("max-mss cannot be respecified");
15203b3a8eb9SGleb Smirnoff 				YYERROR;
15213b3a8eb9SGleb Smirnoff 			}
15223b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > 65535) {
15233b3a8eb9SGleb Smirnoff 				yyerror("illegal max-mss value %d", $2);
15243b3a8eb9SGleb Smirnoff 				YYERROR;
15253b3a8eb9SGleb Smirnoff 			}
152639282ef3SKajetan Staszkiewicz 			scrub_opts.marker |= FOM_MAXMSS;
15273b3a8eb9SGleb Smirnoff 			scrub_opts.maxmss = $2;
15283b3a8eb9SGleb Smirnoff 		}
15293b3a8eb9SGleb Smirnoff 		| SETTOS tos {
153039282ef3SKajetan Staszkiewicz 			if (scrub_opts.marker & FOM_SETTOS) {
15313b3a8eb9SGleb Smirnoff 				yyerror("set-tos cannot be respecified");
15323b3a8eb9SGleb Smirnoff 				YYERROR;
15333b3a8eb9SGleb Smirnoff 			}
153439282ef3SKajetan Staszkiewicz 			scrub_opts.marker |= FOM_SETTOS;
15353b3a8eb9SGleb Smirnoff 			scrub_opts.settos = $2;
15363b3a8eb9SGleb Smirnoff 		}
15373b3a8eb9SGleb Smirnoff 		| fragcache {
153839282ef3SKajetan Staszkiewicz 			if (scrub_opts.marker & FOM_FRAGCACHE) {
15393b3a8eb9SGleb Smirnoff 				yyerror("fragcache cannot be respecified");
15403b3a8eb9SGleb Smirnoff 				YYERROR;
15413b3a8eb9SGleb Smirnoff 			}
154239282ef3SKajetan Staszkiewicz 			scrub_opts.marker |= FOM_FRAGCACHE;
15433b3a8eb9SGleb Smirnoff 			scrub_opts.fragcache = $1;
15443b3a8eb9SGleb Smirnoff 		}
15453b3a8eb9SGleb Smirnoff 		| REASSEMBLE STRING {
15463b3a8eb9SGleb Smirnoff 			if (strcasecmp($2, "tcp") != 0) {
15473b3a8eb9SGleb Smirnoff 				yyerror("scrub reassemble supports only tcp, "
15483b3a8eb9SGleb Smirnoff 				    "not '%s'", $2);
15493b3a8eb9SGleb Smirnoff 				free($2);
15503b3a8eb9SGleb Smirnoff 				YYERROR;
15513b3a8eb9SGleb Smirnoff 			}
15523b3a8eb9SGleb Smirnoff 			free($2);
15533b3a8eb9SGleb Smirnoff 			if (scrub_opts.reassemble_tcp) {
15543b3a8eb9SGleb Smirnoff 				yyerror("reassemble tcp cannot be respecified");
15553b3a8eb9SGleb Smirnoff 				YYERROR;
15563b3a8eb9SGleb Smirnoff 			}
15573b3a8eb9SGleb Smirnoff 			scrub_opts.reassemble_tcp = 1;
15583b3a8eb9SGleb Smirnoff 		}
15593b3a8eb9SGleb Smirnoff 		| RANDOMID {
15603b3a8eb9SGleb Smirnoff 			if (scrub_opts.randomid) {
15613b3a8eb9SGleb Smirnoff 				yyerror("random-id cannot be respecified");
15623b3a8eb9SGleb Smirnoff 				YYERROR;
15633b3a8eb9SGleb Smirnoff 			}
15643b3a8eb9SGleb Smirnoff 			scrub_opts.randomid = 1;
15653b3a8eb9SGleb Smirnoff 		}
15663b3a8eb9SGleb Smirnoff 		| RTABLE NUMBER				{
15673b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > rt_tableid_max()) {
15683b3a8eb9SGleb Smirnoff 				yyerror("invalid rtable id");
15693b3a8eb9SGleb Smirnoff 				YYERROR;
15703b3a8eb9SGleb Smirnoff 			}
15713b3a8eb9SGleb Smirnoff 			scrub_opts.rtableid = $2;
15723b3a8eb9SGleb Smirnoff 		}
15733b3a8eb9SGleb Smirnoff 		| not TAGGED string			{
15743b3a8eb9SGleb Smirnoff 			scrub_opts.match_tag = $3;
15753b3a8eb9SGleb Smirnoff 			scrub_opts.match_tag_not = $1;
15763b3a8eb9SGleb Smirnoff 		}
15773b3a8eb9SGleb Smirnoff 		;
15783b3a8eb9SGleb Smirnoff 
15793b3a8eb9SGleb Smirnoff fragcache	: FRAGMENT REASSEMBLE		{ $$ = 0; /* default */ }
158057e047e5SKristof Provost 		| FRAGMENT NO REASSEMBLE	{ $$ = PFRULE_FRAGMENT_NOREASS; }
15813b3a8eb9SGleb Smirnoff 		;
15823b3a8eb9SGleb Smirnoff 
15833b3a8eb9SGleb Smirnoff antispoof	: ANTISPOOF logquick antispoof_ifspc af antispoof_opts {
1584e9eb0941SKristof Provost 			struct pfctl_rule	 r;
15853b3a8eb9SGleb Smirnoff 			struct node_host	*h = NULL, *hh;
15863b3a8eb9SGleb Smirnoff 			struct node_if		*i, *j;
15873b3a8eb9SGleb Smirnoff 
15883b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_FILTER))
15893b3a8eb9SGleb Smirnoff 				YYERROR;
15903b3a8eb9SGleb Smirnoff 
15913b3a8eb9SGleb Smirnoff 			for (i = $3; i; i = i->next) {
15923b3a8eb9SGleb Smirnoff 				bzero(&r, sizeof(r));
15933b3a8eb9SGleb Smirnoff 
15943b3a8eb9SGleb Smirnoff 				r.action = PF_DROP;
15953b3a8eb9SGleb Smirnoff 				r.direction = PF_IN;
15963b3a8eb9SGleb Smirnoff 				r.log = $2.log;
15973b3a8eb9SGleb Smirnoff 				r.logif = $2.logif;
15983b3a8eb9SGleb Smirnoff 				r.quick = $2.quick;
15993b3a8eb9SGleb Smirnoff 				r.af = $4;
160076c5eeccSKristof Provost 				r.ridentifier = $5.ridentifier;
16013b3a8eb9SGleb Smirnoff 				if (rule_label(&r, $5.label))
16023b3a8eb9SGleb Smirnoff 					YYERROR;
16033b3a8eb9SGleb Smirnoff 				r.rtableid = $5.rtableid;
16043b3a8eb9SGleb Smirnoff 				j = calloc(1, sizeof(struct node_if));
16053b3a8eb9SGleb Smirnoff 				if (j == NULL)
16063b3a8eb9SGleb Smirnoff 					err(1, "antispoof: calloc");
16073b3a8eb9SGleb Smirnoff 				if (strlcpy(j->ifname, i->ifname,
16083b3a8eb9SGleb Smirnoff 				    sizeof(j->ifname)) >= sizeof(j->ifname)) {
16093b3a8eb9SGleb Smirnoff 					free(j);
16103b3a8eb9SGleb Smirnoff 					yyerror("interface name too long");
16113b3a8eb9SGleb Smirnoff 					YYERROR;
16123b3a8eb9SGleb Smirnoff 				}
16133b3a8eb9SGleb Smirnoff 				j->not = 1;
16143b3a8eb9SGleb Smirnoff 				if (i->dynamic) {
16153b3a8eb9SGleb Smirnoff 					h = calloc(1, sizeof(*h));
16163b3a8eb9SGleb Smirnoff 					if (h == NULL)
16173b3a8eb9SGleb Smirnoff 						err(1, "address: calloc");
16183b3a8eb9SGleb Smirnoff 					h->addr.type = PF_ADDR_DYNIFTL;
16193b3a8eb9SGleb Smirnoff 					set_ipmask(h, 128);
16203b3a8eb9SGleb Smirnoff 					if (strlcpy(h->addr.v.ifname, i->ifname,
16213b3a8eb9SGleb Smirnoff 					    sizeof(h->addr.v.ifname)) >=
16223b3a8eb9SGleb Smirnoff 					    sizeof(h->addr.v.ifname)) {
16233b3a8eb9SGleb Smirnoff 						free(h);
16243b3a8eb9SGleb Smirnoff 						yyerror(
16253b3a8eb9SGleb Smirnoff 						    "interface name too long");
16263b3a8eb9SGleb Smirnoff 						YYERROR;
16273b3a8eb9SGleb Smirnoff 					}
16283b3a8eb9SGleb Smirnoff 					hh = malloc(sizeof(*hh));
16293b3a8eb9SGleb Smirnoff 					if (hh == NULL)
16303b3a8eb9SGleb Smirnoff 						 err(1, "address: malloc");
16313b3a8eb9SGleb Smirnoff 					bcopy(h, hh, sizeof(*hh));
16323b3a8eb9SGleb Smirnoff 					h->addr.iflags = PFI_AFLAG_NETWORK;
16333b3a8eb9SGleb Smirnoff 				} else {
16343b3a8eb9SGleb Smirnoff 					h = ifa_lookup(j->ifname,
16353b3a8eb9SGleb Smirnoff 					    PFI_AFLAG_NETWORK);
16363b3a8eb9SGleb Smirnoff 					hh = NULL;
16373b3a8eb9SGleb Smirnoff 				}
16383b3a8eb9SGleb Smirnoff 
16393b3a8eb9SGleb Smirnoff 				if (h != NULL)
1640*aa69fdf1SKristof Provost 					expand_rule(&r, j, NULL, NULL, NULL, NULL, h,
16413b3a8eb9SGleb Smirnoff 					    NULL, NULL, NULL, NULL, NULL,
16422339ead6SKristof Provost 					    NULL, NULL, "");
16433b3a8eb9SGleb Smirnoff 
16443b3a8eb9SGleb Smirnoff 				if ((i->ifa_flags & IFF_LOOPBACK) == 0) {
16453b3a8eb9SGleb Smirnoff 					bzero(&r, sizeof(r));
16463b3a8eb9SGleb Smirnoff 
16473b3a8eb9SGleb Smirnoff 					r.action = PF_DROP;
16483b3a8eb9SGleb Smirnoff 					r.direction = PF_IN;
16493b3a8eb9SGleb Smirnoff 					r.log = $2.log;
16503b3a8eb9SGleb Smirnoff 					r.logif = $2.logif;
16513b3a8eb9SGleb Smirnoff 					r.quick = $2.quick;
16523b3a8eb9SGleb Smirnoff 					r.af = $4;
165376c5eeccSKristof Provost 					r.ridentifier = $5.ridentifier;
16543b3a8eb9SGleb Smirnoff 					if (rule_label(&r, $5.label))
16553b3a8eb9SGleb Smirnoff 						YYERROR;
16563b3a8eb9SGleb Smirnoff 					r.rtableid = $5.rtableid;
16573b3a8eb9SGleb Smirnoff 					if (hh != NULL)
16583b3a8eb9SGleb Smirnoff 						h = hh;
16593b3a8eb9SGleb Smirnoff 					else
16603b3a8eb9SGleb Smirnoff 						h = ifa_lookup(i->ifname, 0);
16613b3a8eb9SGleb Smirnoff 					if (h != NULL)
1662*aa69fdf1SKristof Provost 						expand_rule(&r, NULL, NULL, NULL,
16633b3a8eb9SGleb Smirnoff 						    NULL, NULL, h, NULL, NULL,
16642339ead6SKristof Provost 						    NULL, NULL, NULL, NULL, NULL, "");
16653b3a8eb9SGleb Smirnoff 				} else
16663b3a8eb9SGleb Smirnoff 					free(hh);
16673b3a8eb9SGleb Smirnoff 			}
16686fcc8e04SKristof Provost 			for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++)
16696fcc8e04SKristof Provost 				free($5.label[i]);
16703b3a8eb9SGleb Smirnoff 		}
16713b3a8eb9SGleb Smirnoff 		;
16723b3a8eb9SGleb Smirnoff 
16733b3a8eb9SGleb Smirnoff antispoof_ifspc	: FOR antispoof_if			{ $$ = $2; }
16743b3a8eb9SGleb Smirnoff 		| FOR '{' optnl antispoof_iflst '}'	{ $$ = $4; }
16753b3a8eb9SGleb Smirnoff 		;
16763b3a8eb9SGleb Smirnoff 
16773b3a8eb9SGleb Smirnoff antispoof_iflst	: antispoof_if optnl			{ $$ = $1; }
16783b3a8eb9SGleb Smirnoff 		| antispoof_iflst comma antispoof_if optnl {
16793b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
16803b3a8eb9SGleb Smirnoff 			$1->tail = $3;
16813b3a8eb9SGleb Smirnoff 			$$ = $1;
16823b3a8eb9SGleb Smirnoff 		}
16833b3a8eb9SGleb Smirnoff 		;
16843b3a8eb9SGleb Smirnoff 
16853b3a8eb9SGleb Smirnoff antispoof_if	: if_item				{ $$ = $1; }
16863b3a8eb9SGleb Smirnoff 		| '(' if_item ')'			{
16873b3a8eb9SGleb Smirnoff 			$2->dynamic = 1;
16883b3a8eb9SGleb Smirnoff 			$$ = $2;
16893b3a8eb9SGleb Smirnoff 		}
16903b3a8eb9SGleb Smirnoff 		;
16913b3a8eb9SGleb Smirnoff 
16923b3a8eb9SGleb Smirnoff antispoof_opts	:	{
16933b3a8eb9SGleb Smirnoff 				bzero(&antispoof_opts, sizeof antispoof_opts);
16943b3a8eb9SGleb Smirnoff 				antispoof_opts.rtableid = -1;
16953b3a8eb9SGleb Smirnoff 			}
16963b3a8eb9SGleb Smirnoff 		    antispoof_opts_l
16973b3a8eb9SGleb Smirnoff 			{ $$ = antispoof_opts; }
16983b3a8eb9SGleb Smirnoff 		| /* empty */	{
16993b3a8eb9SGleb Smirnoff 			bzero(&antispoof_opts, sizeof antispoof_opts);
17003b3a8eb9SGleb Smirnoff 			antispoof_opts.rtableid = -1;
17013b3a8eb9SGleb Smirnoff 			$$ = antispoof_opts;
17023b3a8eb9SGleb Smirnoff 		}
17033b3a8eb9SGleb Smirnoff 		;
17043b3a8eb9SGleb Smirnoff 
17053b3a8eb9SGleb Smirnoff antispoof_opts_l	: antispoof_opts_l antispoof_opt
17063b3a8eb9SGleb Smirnoff 			| antispoof_opt
17073b3a8eb9SGleb Smirnoff 			;
17083b3a8eb9SGleb Smirnoff 
17093b3a8eb9SGleb Smirnoff antispoof_opt	: label	{
17106fcc8e04SKristof Provost 			if (antispoof_opts.labelcount >= PF_RULE_MAX_LABEL_COUNT) {
17116fcc8e04SKristof Provost 				yyerror("label can only be used %d times", PF_RULE_MAX_LABEL_COUNT);
17123b3a8eb9SGleb Smirnoff 				YYERROR;
17133b3a8eb9SGleb Smirnoff 			}
17146fcc8e04SKristof Provost 			antispoof_opts.label[antispoof_opts.labelcount++] = $1;
17153b3a8eb9SGleb Smirnoff 		}
171676c5eeccSKristof Provost 		| RIDENTIFIER number {
171776c5eeccSKristof Provost 			antispoof_opts.ridentifier = $2;
171876c5eeccSKristof Provost 		}
17193b3a8eb9SGleb Smirnoff 		| RTABLE NUMBER				{
17203b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > rt_tableid_max()) {
17213b3a8eb9SGleb Smirnoff 				yyerror("invalid rtable id");
17223b3a8eb9SGleb Smirnoff 				YYERROR;
17233b3a8eb9SGleb Smirnoff 			}
17243b3a8eb9SGleb Smirnoff 			antispoof_opts.rtableid = $2;
17253b3a8eb9SGleb Smirnoff 		}
17263b3a8eb9SGleb Smirnoff 		;
17273b3a8eb9SGleb Smirnoff 
17283b3a8eb9SGleb Smirnoff not		: '!'		{ $$ = 1; }
17293b3a8eb9SGleb Smirnoff 		| /* empty */	{ $$ = 0; }
17303b3a8eb9SGleb Smirnoff 		;
17313b3a8eb9SGleb Smirnoff 
17323b3a8eb9SGleb Smirnoff tabledef	: TABLE '<' STRING '>' table_opts {
17333b3a8eb9SGleb Smirnoff 			struct node_host	 *h, *nh;
17343b3a8eb9SGleb Smirnoff 			struct node_tinit	 *ti, *nti;
17353b3a8eb9SGleb Smirnoff 
17363b3a8eb9SGleb Smirnoff 			if (strlen($3) >= PF_TABLE_NAME_SIZE) {
17373b3a8eb9SGleb Smirnoff 				yyerror("table name too long, max %d chars",
17383b3a8eb9SGleb Smirnoff 				    PF_TABLE_NAME_SIZE - 1);
17393b3a8eb9SGleb Smirnoff 				free($3);
17403b3a8eb9SGleb Smirnoff 				YYERROR;
17413b3a8eb9SGleb Smirnoff 			}
17423b3a8eb9SGleb Smirnoff 			if (pf->loadopt & PFCTL_FLAG_TABLE)
17433b3a8eb9SGleb Smirnoff 				if (process_tabledef($3, &$5)) {
17443b3a8eb9SGleb Smirnoff 					free($3);
17453b3a8eb9SGleb Smirnoff 					YYERROR;
17463b3a8eb9SGleb Smirnoff 				}
17473b3a8eb9SGleb Smirnoff 			free($3);
17483b3a8eb9SGleb Smirnoff 			for (ti = SIMPLEQ_FIRST(&$5.init_nodes);
17493b3a8eb9SGleb Smirnoff 			    ti != SIMPLEQ_END(&$5.init_nodes); ti = nti) {
17503b3a8eb9SGleb Smirnoff 				if (ti->file)
17513b3a8eb9SGleb Smirnoff 					free(ti->file);
17523b3a8eb9SGleb Smirnoff 				for (h = ti->host; h != NULL; h = nh) {
17533b3a8eb9SGleb Smirnoff 					nh = h->next;
17543b3a8eb9SGleb Smirnoff 					free(h);
17553b3a8eb9SGleb Smirnoff 				}
17563b3a8eb9SGleb Smirnoff 				nti = SIMPLEQ_NEXT(ti, entries);
17573b3a8eb9SGleb Smirnoff 				free(ti);
17583b3a8eb9SGleb Smirnoff 			}
17593b3a8eb9SGleb Smirnoff 		}
17603b3a8eb9SGleb Smirnoff 		;
17613b3a8eb9SGleb Smirnoff 
17623b3a8eb9SGleb Smirnoff table_opts	:	{
17633b3a8eb9SGleb Smirnoff 			bzero(&table_opts, sizeof table_opts);
17643b3a8eb9SGleb Smirnoff 			SIMPLEQ_INIT(&table_opts.init_nodes);
17653b3a8eb9SGleb Smirnoff 		}
17663b3a8eb9SGleb Smirnoff 		    table_opts_l
17673b3a8eb9SGleb Smirnoff 			{ $$ = table_opts; }
17683b3a8eb9SGleb Smirnoff 		| /* empty */
17693b3a8eb9SGleb Smirnoff 			{
17703b3a8eb9SGleb Smirnoff 			bzero(&table_opts, sizeof table_opts);
17713b3a8eb9SGleb Smirnoff 			SIMPLEQ_INIT(&table_opts.init_nodes);
17723b3a8eb9SGleb Smirnoff 			$$ = table_opts;
17733b3a8eb9SGleb Smirnoff 		}
17743b3a8eb9SGleb Smirnoff 		;
17753b3a8eb9SGleb Smirnoff 
17763b3a8eb9SGleb Smirnoff table_opts_l	: table_opts_l table_opt
17773b3a8eb9SGleb Smirnoff 		| table_opt
17783b3a8eb9SGleb Smirnoff 		;
17793b3a8eb9SGleb Smirnoff 
17803b3a8eb9SGleb Smirnoff table_opt	: STRING		{
17813b3a8eb9SGleb Smirnoff 			if (!strcmp($1, "const"))
17823b3a8eb9SGleb Smirnoff 				table_opts.flags |= PFR_TFLAG_CONST;
17833b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "persist"))
17843b3a8eb9SGleb Smirnoff 				table_opts.flags |= PFR_TFLAG_PERSIST;
17853b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "counters"))
17863b3a8eb9SGleb Smirnoff 				table_opts.flags |= PFR_TFLAG_COUNTERS;
17873b3a8eb9SGleb Smirnoff 			else {
17883b3a8eb9SGleb Smirnoff 				yyerror("invalid table option '%s'", $1);
17893b3a8eb9SGleb Smirnoff 				free($1);
17903b3a8eb9SGleb Smirnoff 				YYERROR;
17913b3a8eb9SGleb Smirnoff 			}
17923b3a8eb9SGleb Smirnoff 			free($1);
17933b3a8eb9SGleb Smirnoff 		}
17943b3a8eb9SGleb Smirnoff 		| '{' optnl '}'		{ table_opts.init_addr = 1; }
17953b3a8eb9SGleb Smirnoff 		| '{' optnl host_list '}'	{
17963b3a8eb9SGleb Smirnoff 			struct node_host	*n;
17973b3a8eb9SGleb Smirnoff 			struct node_tinit	*ti;
17983b3a8eb9SGleb Smirnoff 
17993b3a8eb9SGleb Smirnoff 			for (n = $3; n != NULL; n = n->next) {
18003b3a8eb9SGleb Smirnoff 				switch (n->addr.type) {
18013b3a8eb9SGleb Smirnoff 				case PF_ADDR_ADDRMASK:
18023b3a8eb9SGleb Smirnoff 					continue; /* ok */
18033b3a8eb9SGleb Smirnoff 				case PF_ADDR_RANGE:
18043b3a8eb9SGleb Smirnoff 					yyerror("address ranges are not "
18053b3a8eb9SGleb Smirnoff 					    "permitted inside tables");
18063b3a8eb9SGleb Smirnoff 					break;
18073b3a8eb9SGleb Smirnoff 				case PF_ADDR_DYNIFTL:
18083b3a8eb9SGleb Smirnoff 					yyerror("dynamic addresses are not "
18093b3a8eb9SGleb Smirnoff 					    "permitted inside tables");
18103b3a8eb9SGleb Smirnoff 					break;
18113b3a8eb9SGleb Smirnoff 				case PF_ADDR_TABLE:
18123b3a8eb9SGleb Smirnoff 					yyerror("tables cannot contain tables");
18133b3a8eb9SGleb Smirnoff 					break;
18143b3a8eb9SGleb Smirnoff 				case PF_ADDR_NOROUTE:
18153b3a8eb9SGleb Smirnoff 					yyerror("\"no-route\" is not permitted "
18163b3a8eb9SGleb Smirnoff 					    "inside tables");
18173b3a8eb9SGleb Smirnoff 					break;
18183b3a8eb9SGleb Smirnoff 				case PF_ADDR_URPFFAILED:
18193b3a8eb9SGleb Smirnoff 					yyerror("\"urpf-failed\" is not "
18203b3a8eb9SGleb Smirnoff 					    "permitted inside tables");
18213b3a8eb9SGleb Smirnoff 					break;
18223b3a8eb9SGleb Smirnoff 				default:
18233b3a8eb9SGleb Smirnoff 					yyerror("unknown address type %d",
18243b3a8eb9SGleb Smirnoff 					    n->addr.type);
18253b3a8eb9SGleb Smirnoff 				}
18263b3a8eb9SGleb Smirnoff 				YYERROR;
18273b3a8eb9SGleb Smirnoff 			}
18283b3a8eb9SGleb Smirnoff 			if (!(ti = calloc(1, sizeof(*ti))))
18293b3a8eb9SGleb Smirnoff 				err(1, "table_opt: calloc");
18303b3a8eb9SGleb Smirnoff 			ti->host = $3;
18313b3a8eb9SGleb Smirnoff 			SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
18323b3a8eb9SGleb Smirnoff 			    entries);
18333b3a8eb9SGleb Smirnoff 			table_opts.init_addr = 1;
18343b3a8eb9SGleb Smirnoff 		}
18353b3a8eb9SGleb Smirnoff 		| FILENAME STRING	{
18363b3a8eb9SGleb Smirnoff 			struct node_tinit	*ti;
18373b3a8eb9SGleb Smirnoff 
18383b3a8eb9SGleb Smirnoff 			if (!(ti = calloc(1, sizeof(*ti))))
18393b3a8eb9SGleb Smirnoff 				err(1, "table_opt: calloc");
18403b3a8eb9SGleb Smirnoff 			ti->file = $2;
18413b3a8eb9SGleb Smirnoff 			SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
18423b3a8eb9SGleb Smirnoff 			    entries);
18433b3a8eb9SGleb Smirnoff 			table_opts.init_addr = 1;
18443b3a8eb9SGleb Smirnoff 		}
18453b3a8eb9SGleb Smirnoff 		;
18463b3a8eb9SGleb Smirnoff 
18473b3a8eb9SGleb Smirnoff altqif		: ALTQ interface queue_opts QUEUE qassign {
18483b3a8eb9SGleb Smirnoff 			struct pf_altq	a;
18493b3a8eb9SGleb Smirnoff 
18503b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_QUEUE))
18513b3a8eb9SGleb Smirnoff 				YYERROR;
18523b3a8eb9SGleb Smirnoff 
18533b3a8eb9SGleb Smirnoff 			memset(&a, 0, sizeof(a));
18543b3a8eb9SGleb Smirnoff 			if ($3.scheduler.qtype == ALTQT_NONE) {
18553b3a8eb9SGleb Smirnoff 				yyerror("no scheduler specified!");
18563b3a8eb9SGleb Smirnoff 				YYERROR;
18573b3a8eb9SGleb Smirnoff 			}
18583b3a8eb9SGleb Smirnoff 			a.scheduler = $3.scheduler.qtype;
18593b3a8eb9SGleb Smirnoff 			a.qlimit = $3.qlimit;
18603b3a8eb9SGleb Smirnoff 			a.tbrsize = $3.tbrsize;
18610a70aaf8SLuiz Otavio O Souza 			if ($5 == NULL && $3.scheduler.qtype != ALTQT_CODEL) {
18623b3a8eb9SGleb Smirnoff 				yyerror("no child queues specified");
18633b3a8eb9SGleb Smirnoff 				YYERROR;
18643b3a8eb9SGleb Smirnoff 			}
18653b3a8eb9SGleb Smirnoff 			if (expand_altq(&a, $2, $5, $3.queue_bwspec,
18663b3a8eb9SGleb Smirnoff 			    &$3.scheduler))
18673b3a8eb9SGleb Smirnoff 				YYERROR;
18683b3a8eb9SGleb Smirnoff 		}
18693b3a8eb9SGleb Smirnoff 		;
18703b3a8eb9SGleb Smirnoff 
18713b3a8eb9SGleb Smirnoff queuespec	: QUEUE STRING interface queue_opts qassign {
18723b3a8eb9SGleb Smirnoff 			struct pf_altq	a;
18733b3a8eb9SGleb Smirnoff 
18743b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_QUEUE)) {
18753b3a8eb9SGleb Smirnoff 				free($2);
18763b3a8eb9SGleb Smirnoff 				YYERROR;
18773b3a8eb9SGleb Smirnoff 			}
18783b3a8eb9SGleb Smirnoff 
18793b3a8eb9SGleb Smirnoff 			memset(&a, 0, sizeof(a));
18803b3a8eb9SGleb Smirnoff 
18813b3a8eb9SGleb Smirnoff 			if (strlcpy(a.qname, $2, sizeof(a.qname)) >=
18823b3a8eb9SGleb Smirnoff 			    sizeof(a.qname)) {
18833b3a8eb9SGleb Smirnoff 				yyerror("queue name too long (max "
18843b3a8eb9SGleb Smirnoff 				    "%d chars)", PF_QNAME_SIZE-1);
18853b3a8eb9SGleb Smirnoff 				free($2);
18863b3a8eb9SGleb Smirnoff 				YYERROR;
18873b3a8eb9SGleb Smirnoff 			}
18883b3a8eb9SGleb Smirnoff 			free($2);
18893b3a8eb9SGleb Smirnoff 			if ($4.tbrsize) {
18903b3a8eb9SGleb Smirnoff 				yyerror("cannot specify tbrsize for queue");
18913b3a8eb9SGleb Smirnoff 				YYERROR;
18923b3a8eb9SGleb Smirnoff 			}
18933b3a8eb9SGleb Smirnoff 			if ($4.priority > 255) {
18943b3a8eb9SGleb Smirnoff 				yyerror("priority out of range: max 255");
18953b3a8eb9SGleb Smirnoff 				YYERROR;
18963b3a8eb9SGleb Smirnoff 			}
18973b3a8eb9SGleb Smirnoff 			a.priority = $4.priority;
18983b3a8eb9SGleb Smirnoff 			a.qlimit = $4.qlimit;
18993b3a8eb9SGleb Smirnoff 			a.scheduler = $4.scheduler.qtype;
19003b3a8eb9SGleb Smirnoff 			if (expand_queue(&a, $3, $5, $4.queue_bwspec,
19013b3a8eb9SGleb Smirnoff 			    &$4.scheduler)) {
19023b3a8eb9SGleb Smirnoff 				yyerror("errors in queue definition");
19033b3a8eb9SGleb Smirnoff 				YYERROR;
19043b3a8eb9SGleb Smirnoff 			}
19053b3a8eb9SGleb Smirnoff 		}
19063b3a8eb9SGleb Smirnoff 		;
19073b3a8eb9SGleb Smirnoff 
19083b3a8eb9SGleb Smirnoff queue_opts	:	{
19093b3a8eb9SGleb Smirnoff 			bzero(&queue_opts, sizeof queue_opts);
19103b3a8eb9SGleb Smirnoff 			queue_opts.priority = DEFAULT_PRIORITY;
19113b3a8eb9SGleb Smirnoff 			queue_opts.qlimit = DEFAULT_QLIMIT;
19123b3a8eb9SGleb Smirnoff 			queue_opts.scheduler.qtype = ALTQT_NONE;
19133b3a8eb9SGleb Smirnoff 			queue_opts.queue_bwspec.bw_percent = 100;
19143b3a8eb9SGleb Smirnoff 		}
19153b3a8eb9SGleb Smirnoff 		    queue_opts_l
19163b3a8eb9SGleb Smirnoff 			{ $$ = queue_opts; }
19173b3a8eb9SGleb Smirnoff 		| /* empty */ {
19183b3a8eb9SGleb Smirnoff 			bzero(&queue_opts, sizeof queue_opts);
19193b3a8eb9SGleb Smirnoff 			queue_opts.priority = DEFAULT_PRIORITY;
19203b3a8eb9SGleb Smirnoff 			queue_opts.qlimit = DEFAULT_QLIMIT;
19213b3a8eb9SGleb Smirnoff 			queue_opts.scheduler.qtype = ALTQT_NONE;
19223b3a8eb9SGleb Smirnoff 			queue_opts.queue_bwspec.bw_percent = 100;
19233b3a8eb9SGleb Smirnoff 			$$ = queue_opts;
19243b3a8eb9SGleb Smirnoff 		}
19253b3a8eb9SGleb Smirnoff 		;
19263b3a8eb9SGleb Smirnoff 
19273b3a8eb9SGleb Smirnoff queue_opts_l	: queue_opts_l queue_opt
19283b3a8eb9SGleb Smirnoff 		| queue_opt
19293b3a8eb9SGleb Smirnoff 		;
19303b3a8eb9SGleb Smirnoff 
19313b3a8eb9SGleb Smirnoff queue_opt	: BANDWIDTH bandwidth	{
19323b3a8eb9SGleb Smirnoff 			if (queue_opts.marker & QOM_BWSPEC) {
19333b3a8eb9SGleb Smirnoff 				yyerror("bandwidth cannot be respecified");
19343b3a8eb9SGleb Smirnoff 				YYERROR;
19353b3a8eb9SGleb Smirnoff 			}
19363b3a8eb9SGleb Smirnoff 			queue_opts.marker |= QOM_BWSPEC;
19373b3a8eb9SGleb Smirnoff 			queue_opts.queue_bwspec = $2;
19383b3a8eb9SGleb Smirnoff 		}
19393b3a8eb9SGleb Smirnoff 		| PRIORITY NUMBER	{
19403b3a8eb9SGleb Smirnoff 			if (queue_opts.marker & QOM_PRIORITY) {
19413b3a8eb9SGleb Smirnoff 				yyerror("priority cannot be respecified");
19423b3a8eb9SGleb Smirnoff 				YYERROR;
19433b3a8eb9SGleb Smirnoff 			}
19443b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > 255) {
19453b3a8eb9SGleb Smirnoff 				yyerror("priority out of range: max 255");
19463b3a8eb9SGleb Smirnoff 				YYERROR;
19473b3a8eb9SGleb Smirnoff 			}
19483b3a8eb9SGleb Smirnoff 			queue_opts.marker |= QOM_PRIORITY;
19493b3a8eb9SGleb Smirnoff 			queue_opts.priority = $2;
19503b3a8eb9SGleb Smirnoff 		}
19513b3a8eb9SGleb Smirnoff 		| QLIMIT NUMBER	{
19523b3a8eb9SGleb Smirnoff 			if (queue_opts.marker & QOM_QLIMIT) {
19533b3a8eb9SGleb Smirnoff 				yyerror("qlimit cannot be respecified");
19543b3a8eb9SGleb Smirnoff 				YYERROR;
19553b3a8eb9SGleb Smirnoff 			}
19563b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > 65535) {
19573b3a8eb9SGleb Smirnoff 				yyerror("qlimit out of range: max 65535");
19583b3a8eb9SGleb Smirnoff 				YYERROR;
19593b3a8eb9SGleb Smirnoff 			}
19603b3a8eb9SGleb Smirnoff 			queue_opts.marker |= QOM_QLIMIT;
19613b3a8eb9SGleb Smirnoff 			queue_opts.qlimit = $2;
19623b3a8eb9SGleb Smirnoff 		}
19633b3a8eb9SGleb Smirnoff 		| scheduler	{
19643b3a8eb9SGleb Smirnoff 			if (queue_opts.marker & QOM_SCHEDULER) {
19653b3a8eb9SGleb Smirnoff 				yyerror("scheduler cannot be respecified");
19663b3a8eb9SGleb Smirnoff 				YYERROR;
19673b3a8eb9SGleb Smirnoff 			}
19683b3a8eb9SGleb Smirnoff 			queue_opts.marker |= QOM_SCHEDULER;
19693b3a8eb9SGleb Smirnoff 			queue_opts.scheduler = $1;
19703b3a8eb9SGleb Smirnoff 		}
19713b3a8eb9SGleb Smirnoff 		| TBRSIZE NUMBER	{
19723b3a8eb9SGleb Smirnoff 			if (queue_opts.marker & QOM_TBRSIZE) {
19733b3a8eb9SGleb Smirnoff 				yyerror("tbrsize cannot be respecified");
19743b3a8eb9SGleb Smirnoff 				YYERROR;
19753b3a8eb9SGleb Smirnoff 			}
1976249cc75fSPatrick Kelsey 			if ($2 < 0 || $2 > UINT_MAX) {
1977249cc75fSPatrick Kelsey 				yyerror("tbrsize too big: max %u", UINT_MAX);
19783b3a8eb9SGleb Smirnoff 				YYERROR;
19793b3a8eb9SGleb Smirnoff 			}
19803b3a8eb9SGleb Smirnoff 			queue_opts.marker |= QOM_TBRSIZE;
19813b3a8eb9SGleb Smirnoff 			queue_opts.tbrsize = $2;
19823b3a8eb9SGleb Smirnoff 		}
19833b3a8eb9SGleb Smirnoff 		;
19843b3a8eb9SGleb Smirnoff 
19853b3a8eb9SGleb Smirnoff bandwidth	: STRING {
19863b3a8eb9SGleb Smirnoff 			double	 bps;
19873b3a8eb9SGleb Smirnoff 			char	*cp;
19883b3a8eb9SGleb Smirnoff 
19893b3a8eb9SGleb Smirnoff 			$$.bw_percent = 0;
19903b3a8eb9SGleb Smirnoff 
19913b3a8eb9SGleb Smirnoff 			bps = strtod($1, &cp);
19923b3a8eb9SGleb Smirnoff 			if (cp != NULL) {
1993db1bbde6SLuiz Otavio O Souza 				if (strlen(cp) > 1) {
1994db1bbde6SLuiz Otavio O Souza 					char *cu = cp + 1;
1995db1bbde6SLuiz Otavio O Souza 					if (!strcmp(cu, "Bit") ||
1996db1bbde6SLuiz Otavio O Souza 					    !strcmp(cu, "B") ||
1997db1bbde6SLuiz Otavio O Souza 					    !strcmp(cu, "bit") ||
1998db1bbde6SLuiz Otavio O Souza 					    !strcmp(cu, "b")) {
1999db1bbde6SLuiz Otavio O Souza 						*cu = 0;
2000db1bbde6SLuiz Otavio O Souza 					}
2001db1bbde6SLuiz Otavio O Souza 				}
20023b3a8eb9SGleb Smirnoff 				if (!strcmp(cp, "b"))
20033b3a8eb9SGleb Smirnoff 					; /* nothing */
2004db1bbde6SLuiz Otavio O Souza 				else if (!strcmp(cp, "K"))
20053b3a8eb9SGleb Smirnoff 					bps *= 1000;
2006db1bbde6SLuiz Otavio O Souza 				else if (!strcmp(cp, "M"))
20073b3a8eb9SGleb Smirnoff 					bps *= 1000 * 1000;
2008db1bbde6SLuiz Otavio O Souza 				else if (!strcmp(cp, "G"))
20093b3a8eb9SGleb Smirnoff 					bps *= 1000 * 1000 * 1000;
20103b3a8eb9SGleb Smirnoff 				else if (!strcmp(cp, "%")) {
20113b3a8eb9SGleb Smirnoff 					if (bps < 0 || bps > 100) {
20123b3a8eb9SGleb Smirnoff 						yyerror("bandwidth spec "
20133b3a8eb9SGleb Smirnoff 						    "out of range");
20143b3a8eb9SGleb Smirnoff 						free($1);
20153b3a8eb9SGleb Smirnoff 						YYERROR;
20163b3a8eb9SGleb Smirnoff 					}
20173b3a8eb9SGleb Smirnoff 					$$.bw_percent = bps;
20183b3a8eb9SGleb Smirnoff 					bps = 0;
20193b3a8eb9SGleb Smirnoff 				} else {
20203b3a8eb9SGleb Smirnoff 					yyerror("unknown unit %s", cp);
20213b3a8eb9SGleb Smirnoff 					free($1);
20223b3a8eb9SGleb Smirnoff 					YYERROR;
20233b3a8eb9SGleb Smirnoff 				}
20243b3a8eb9SGleb Smirnoff 			}
20253b3a8eb9SGleb Smirnoff 			free($1);
2026249cc75fSPatrick Kelsey 			$$.bw_absolute = (u_int64_t)bps;
20273b3a8eb9SGleb Smirnoff 		}
20283b3a8eb9SGleb Smirnoff 		| NUMBER {
2029249cc75fSPatrick Kelsey 			if ($1 < 0 || $1 >= LLONG_MAX) {
20303b3a8eb9SGleb Smirnoff 				yyerror("bandwidth number too big");
20313b3a8eb9SGleb Smirnoff 				YYERROR;
20323b3a8eb9SGleb Smirnoff 			}
20333b3a8eb9SGleb Smirnoff 			$$.bw_percent = 0;
20343b3a8eb9SGleb Smirnoff 			$$.bw_absolute = $1;
20353b3a8eb9SGleb Smirnoff 		}
20363b3a8eb9SGleb Smirnoff 		;
20373b3a8eb9SGleb Smirnoff 
20383b3a8eb9SGleb Smirnoff scheduler	: CBQ				{
20393b3a8eb9SGleb Smirnoff 			$$.qtype = ALTQT_CBQ;
20403b3a8eb9SGleb Smirnoff 			$$.data.cbq_opts.flags = 0;
20413b3a8eb9SGleb Smirnoff 		}
20423b3a8eb9SGleb Smirnoff 		| CBQ '(' cbqflags_list ')'	{
20433b3a8eb9SGleb Smirnoff 			$$.qtype = ALTQT_CBQ;
20443b3a8eb9SGleb Smirnoff 			$$.data.cbq_opts.flags = $3;
20453b3a8eb9SGleb Smirnoff 		}
20463b3a8eb9SGleb Smirnoff 		| PRIQ				{
20473b3a8eb9SGleb Smirnoff 			$$.qtype = ALTQT_PRIQ;
20483b3a8eb9SGleb Smirnoff 			$$.data.priq_opts.flags = 0;
20493b3a8eb9SGleb Smirnoff 		}
20503b3a8eb9SGleb Smirnoff 		| PRIQ '(' priqflags_list ')'	{
20513b3a8eb9SGleb Smirnoff 			$$.qtype = ALTQT_PRIQ;
20523b3a8eb9SGleb Smirnoff 			$$.data.priq_opts.flags = $3;
20533b3a8eb9SGleb Smirnoff 		}
20543b3a8eb9SGleb Smirnoff 		| HFSC				{
20553b3a8eb9SGleb Smirnoff 			$$.qtype = ALTQT_HFSC;
20563b3a8eb9SGleb Smirnoff 			bzero(&$$.data.hfsc_opts,
20573b3a8eb9SGleb Smirnoff 			    sizeof(struct node_hfsc_opts));
20583b3a8eb9SGleb Smirnoff 		}
20593b3a8eb9SGleb Smirnoff 		| HFSC '(' hfsc_opts ')'	{
20603b3a8eb9SGleb Smirnoff 			$$.qtype = ALTQT_HFSC;
20613b3a8eb9SGleb Smirnoff 			$$.data.hfsc_opts = $3;
20623b3a8eb9SGleb Smirnoff 		}
2063a5b789f6SErmal Luçi 		| FAIRQ				{
2064a5b789f6SErmal Luçi 			$$.qtype = ALTQT_FAIRQ;
2065a5b789f6SErmal Luçi 			bzero(&$$.data.fairq_opts,
2066a5b789f6SErmal Luçi 				sizeof(struct node_fairq_opts));
2067a5b789f6SErmal Luçi 		}
2068a5b789f6SErmal Luçi 		| FAIRQ '(' fairq_opts ')'      {
2069a5b789f6SErmal Luçi 			$$.qtype = ALTQT_FAIRQ;
2070a5b789f6SErmal Luçi 			$$.data.fairq_opts = $3;
2071a5b789f6SErmal Luçi 		}
20720a70aaf8SLuiz Otavio O Souza 		| CODEL				{
20730a70aaf8SLuiz Otavio O Souza 			$$.qtype = ALTQT_CODEL;
20740a70aaf8SLuiz Otavio O Souza 			bzero(&$$.data.codel_opts,
20750a70aaf8SLuiz Otavio O Souza 				sizeof(struct codel_opts));
20760a70aaf8SLuiz Otavio O Souza 		}
20770a70aaf8SLuiz Otavio O Souza 		| CODEL '(' codel_opts ')'	{
20780a70aaf8SLuiz Otavio O Souza 			$$.qtype = ALTQT_CODEL;
20790a70aaf8SLuiz Otavio O Souza 			$$.data.codel_opts = $3;
20800a70aaf8SLuiz Otavio O Souza 		}
20813b3a8eb9SGleb Smirnoff 		;
20823b3a8eb9SGleb Smirnoff 
20833b3a8eb9SGleb Smirnoff cbqflags_list	: cbqflags_item				{ $$ |= $1; }
20843b3a8eb9SGleb Smirnoff 		| cbqflags_list comma cbqflags_item	{ $$ |= $3; }
20853b3a8eb9SGleb Smirnoff 		;
20863b3a8eb9SGleb Smirnoff 
20873b3a8eb9SGleb Smirnoff cbqflags_item	: STRING	{
20883b3a8eb9SGleb Smirnoff 			if (!strcmp($1, "default"))
20893b3a8eb9SGleb Smirnoff 				$$ = CBQCLF_DEFCLASS;
20903b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "borrow"))
20913b3a8eb9SGleb Smirnoff 				$$ = CBQCLF_BORROW;
20923b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "red"))
20933b3a8eb9SGleb Smirnoff 				$$ = CBQCLF_RED;
20943b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "ecn"))
20953b3a8eb9SGleb Smirnoff 				$$ = CBQCLF_RED|CBQCLF_ECN;
20963b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "rio"))
20973b3a8eb9SGleb Smirnoff 				$$ = CBQCLF_RIO;
20980a70aaf8SLuiz Otavio O Souza 			else if (!strcmp($1, "codel"))
20990a70aaf8SLuiz Otavio O Souza 				$$ = CBQCLF_CODEL;
21003b3a8eb9SGleb Smirnoff 			else {
21013b3a8eb9SGleb Smirnoff 				yyerror("unknown cbq flag \"%s\"", $1);
21023b3a8eb9SGleb Smirnoff 				free($1);
21033b3a8eb9SGleb Smirnoff 				YYERROR;
21043b3a8eb9SGleb Smirnoff 			}
21053b3a8eb9SGleb Smirnoff 			free($1);
21063b3a8eb9SGleb Smirnoff 		}
21073b3a8eb9SGleb Smirnoff 		;
21083b3a8eb9SGleb Smirnoff 
21093b3a8eb9SGleb Smirnoff priqflags_list	: priqflags_item			{ $$ |= $1; }
21103b3a8eb9SGleb Smirnoff 		| priqflags_list comma priqflags_item	{ $$ |= $3; }
21113b3a8eb9SGleb Smirnoff 		;
21123b3a8eb9SGleb Smirnoff 
21133b3a8eb9SGleb Smirnoff priqflags_item	: STRING	{
21143b3a8eb9SGleb Smirnoff 			if (!strcmp($1, "default"))
21153b3a8eb9SGleb Smirnoff 				$$ = PRCF_DEFAULTCLASS;
21163b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "red"))
21173b3a8eb9SGleb Smirnoff 				$$ = PRCF_RED;
21183b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "ecn"))
21193b3a8eb9SGleb Smirnoff 				$$ = PRCF_RED|PRCF_ECN;
21203b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "rio"))
21213b3a8eb9SGleb Smirnoff 				$$ = PRCF_RIO;
21220a70aaf8SLuiz Otavio O Souza 			else if (!strcmp($1, "codel"))
21230a70aaf8SLuiz Otavio O Souza 				$$ = PRCF_CODEL;
21243b3a8eb9SGleb Smirnoff 			else {
21253b3a8eb9SGleb Smirnoff 				yyerror("unknown priq flag \"%s\"", $1);
21263b3a8eb9SGleb Smirnoff 				free($1);
21273b3a8eb9SGleb Smirnoff 				YYERROR;
21283b3a8eb9SGleb Smirnoff 			}
21293b3a8eb9SGleb Smirnoff 			free($1);
21303b3a8eb9SGleb Smirnoff 		}
21313b3a8eb9SGleb Smirnoff 		;
21323b3a8eb9SGleb Smirnoff 
21333b3a8eb9SGleb Smirnoff hfsc_opts	:	{
21343b3a8eb9SGleb Smirnoff 				bzero(&hfsc_opts,
21353b3a8eb9SGleb Smirnoff 				    sizeof(struct node_hfsc_opts));
21363b3a8eb9SGleb Smirnoff 			}
21373b3a8eb9SGleb Smirnoff 		    hfscopts_list				{
21383b3a8eb9SGleb Smirnoff 			$$ = hfsc_opts;
21393b3a8eb9SGleb Smirnoff 		}
21403b3a8eb9SGleb Smirnoff 		;
21413b3a8eb9SGleb Smirnoff 
21423b3a8eb9SGleb Smirnoff hfscopts_list	: hfscopts_item
21433b3a8eb9SGleb Smirnoff 		| hfscopts_list comma hfscopts_item
21443b3a8eb9SGleb Smirnoff 		;
21453b3a8eb9SGleb Smirnoff 
21463b3a8eb9SGleb Smirnoff hfscopts_item	: LINKSHARE bandwidth				{
21473b3a8eb9SGleb Smirnoff 			if (hfsc_opts.linkshare.used) {
21483b3a8eb9SGleb Smirnoff 				yyerror("linkshare already specified");
21493b3a8eb9SGleb Smirnoff 				YYERROR;
21503b3a8eb9SGleb Smirnoff 			}
21513b3a8eb9SGleb Smirnoff 			hfsc_opts.linkshare.m2 = $2;
21523b3a8eb9SGleb Smirnoff 			hfsc_opts.linkshare.used = 1;
21533b3a8eb9SGleb Smirnoff 		}
21543b3a8eb9SGleb Smirnoff 		| LINKSHARE '(' bandwidth comma NUMBER comma bandwidth ')'
21553b3a8eb9SGleb Smirnoff 		    {
21563b3a8eb9SGleb Smirnoff 			if ($5 < 0 || $5 > INT_MAX) {
21573b3a8eb9SGleb Smirnoff 				yyerror("timing in curve out of range");
21583b3a8eb9SGleb Smirnoff 				YYERROR;
21593b3a8eb9SGleb Smirnoff 			}
21603b3a8eb9SGleb Smirnoff 			if (hfsc_opts.linkshare.used) {
21613b3a8eb9SGleb Smirnoff 				yyerror("linkshare already specified");
21623b3a8eb9SGleb Smirnoff 				YYERROR;
21633b3a8eb9SGleb Smirnoff 			}
21643b3a8eb9SGleb Smirnoff 			hfsc_opts.linkshare.m1 = $3;
21653b3a8eb9SGleb Smirnoff 			hfsc_opts.linkshare.d = $5;
21663b3a8eb9SGleb Smirnoff 			hfsc_opts.linkshare.m2 = $7;
21673b3a8eb9SGleb Smirnoff 			hfsc_opts.linkshare.used = 1;
21683b3a8eb9SGleb Smirnoff 		}
21693b3a8eb9SGleb Smirnoff 		| REALTIME bandwidth				{
21703b3a8eb9SGleb Smirnoff 			if (hfsc_opts.realtime.used) {
21713b3a8eb9SGleb Smirnoff 				yyerror("realtime already specified");
21723b3a8eb9SGleb Smirnoff 				YYERROR;
21733b3a8eb9SGleb Smirnoff 			}
21743b3a8eb9SGleb Smirnoff 			hfsc_opts.realtime.m2 = $2;
21753b3a8eb9SGleb Smirnoff 			hfsc_opts.realtime.used = 1;
21763b3a8eb9SGleb Smirnoff 		}
21773b3a8eb9SGleb Smirnoff 		| REALTIME '(' bandwidth comma NUMBER comma bandwidth ')'
21783b3a8eb9SGleb Smirnoff 		    {
21793b3a8eb9SGleb Smirnoff 			if ($5 < 0 || $5 > INT_MAX) {
21803b3a8eb9SGleb Smirnoff 				yyerror("timing in curve out of range");
21813b3a8eb9SGleb Smirnoff 				YYERROR;
21823b3a8eb9SGleb Smirnoff 			}
21833b3a8eb9SGleb Smirnoff 			if (hfsc_opts.realtime.used) {
21843b3a8eb9SGleb Smirnoff 				yyerror("realtime already specified");
21853b3a8eb9SGleb Smirnoff 				YYERROR;
21863b3a8eb9SGleb Smirnoff 			}
21873b3a8eb9SGleb Smirnoff 			hfsc_opts.realtime.m1 = $3;
21883b3a8eb9SGleb Smirnoff 			hfsc_opts.realtime.d = $5;
21893b3a8eb9SGleb Smirnoff 			hfsc_opts.realtime.m2 = $7;
21903b3a8eb9SGleb Smirnoff 			hfsc_opts.realtime.used = 1;
21913b3a8eb9SGleb Smirnoff 		}
21923b3a8eb9SGleb Smirnoff 		| UPPERLIMIT bandwidth				{
21933b3a8eb9SGleb Smirnoff 			if (hfsc_opts.upperlimit.used) {
21943b3a8eb9SGleb Smirnoff 				yyerror("upperlimit already specified");
21953b3a8eb9SGleb Smirnoff 				YYERROR;
21963b3a8eb9SGleb Smirnoff 			}
21973b3a8eb9SGleb Smirnoff 			hfsc_opts.upperlimit.m2 = $2;
21983b3a8eb9SGleb Smirnoff 			hfsc_opts.upperlimit.used = 1;
21993b3a8eb9SGleb Smirnoff 		}
22003b3a8eb9SGleb Smirnoff 		| UPPERLIMIT '(' bandwidth comma NUMBER comma bandwidth ')'
22013b3a8eb9SGleb Smirnoff 		    {
22023b3a8eb9SGleb Smirnoff 			if ($5 < 0 || $5 > INT_MAX) {
22033b3a8eb9SGleb Smirnoff 				yyerror("timing in curve out of range");
22043b3a8eb9SGleb Smirnoff 				YYERROR;
22053b3a8eb9SGleb Smirnoff 			}
22063b3a8eb9SGleb Smirnoff 			if (hfsc_opts.upperlimit.used) {
22073b3a8eb9SGleb Smirnoff 				yyerror("upperlimit already specified");
22083b3a8eb9SGleb Smirnoff 				YYERROR;
22093b3a8eb9SGleb Smirnoff 			}
22103b3a8eb9SGleb Smirnoff 			hfsc_opts.upperlimit.m1 = $3;
22113b3a8eb9SGleb Smirnoff 			hfsc_opts.upperlimit.d = $5;
22123b3a8eb9SGleb Smirnoff 			hfsc_opts.upperlimit.m2 = $7;
22133b3a8eb9SGleb Smirnoff 			hfsc_opts.upperlimit.used = 1;
22143b3a8eb9SGleb Smirnoff 		}
22153b3a8eb9SGleb Smirnoff 		| STRING	{
22163b3a8eb9SGleb Smirnoff 			if (!strcmp($1, "default"))
22173b3a8eb9SGleb Smirnoff 				hfsc_opts.flags |= HFCF_DEFAULTCLASS;
22183b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "red"))
22193b3a8eb9SGleb Smirnoff 				hfsc_opts.flags |= HFCF_RED;
22203b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "ecn"))
22213b3a8eb9SGleb Smirnoff 				hfsc_opts.flags |= HFCF_RED|HFCF_ECN;
22223b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "rio"))
22233b3a8eb9SGleb Smirnoff 				hfsc_opts.flags |= HFCF_RIO;
22240a70aaf8SLuiz Otavio O Souza 			else if (!strcmp($1, "codel"))
22250a70aaf8SLuiz Otavio O Souza 				hfsc_opts.flags |= HFCF_CODEL;
22263b3a8eb9SGleb Smirnoff 			else {
22273b3a8eb9SGleb Smirnoff 				yyerror("unknown hfsc flag \"%s\"", $1);
22283b3a8eb9SGleb Smirnoff 				free($1);
22293b3a8eb9SGleb Smirnoff 				YYERROR;
22303b3a8eb9SGleb Smirnoff 			}
22313b3a8eb9SGleb Smirnoff 			free($1);
22323b3a8eb9SGleb Smirnoff 		}
22333b3a8eb9SGleb Smirnoff 		;
22343b3a8eb9SGleb Smirnoff 
2235a5b789f6SErmal Luçi fairq_opts	:	{
2236a5b789f6SErmal Luçi 				bzero(&fairq_opts,
2237a5b789f6SErmal Luçi 				    sizeof(struct node_fairq_opts));
2238a5b789f6SErmal Luçi 			}
2239a5b789f6SErmal Luçi 		    fairqopts_list				{
2240a5b789f6SErmal Luçi 			$$ = fairq_opts;
2241a5b789f6SErmal Luçi 		}
2242a5b789f6SErmal Luçi 		;
2243a5b789f6SErmal Luçi 
2244a5b789f6SErmal Luçi fairqopts_list	: fairqopts_item
2245a5b789f6SErmal Luçi 		| fairqopts_list comma fairqopts_item
2246a5b789f6SErmal Luçi 		;
2247a5b789f6SErmal Luçi 
2248a5b789f6SErmal Luçi fairqopts_item	: LINKSHARE bandwidth				{
2249a5b789f6SErmal Luçi 			if (fairq_opts.linkshare.used) {
2250a5b789f6SErmal Luçi 				yyerror("linkshare already specified");
2251a5b789f6SErmal Luçi 				YYERROR;
2252a5b789f6SErmal Luçi 			}
2253a5b789f6SErmal Luçi 			fairq_opts.linkshare.m2 = $2;
2254a5b789f6SErmal Luçi 			fairq_opts.linkshare.used = 1;
2255a5b789f6SErmal Luçi 		}
2256a5b789f6SErmal Luçi 		| LINKSHARE '(' bandwidth number bandwidth ')'	{
2257a5b789f6SErmal Luçi 			if (fairq_opts.linkshare.used) {
2258a5b789f6SErmal Luçi 				yyerror("linkshare already specified");
2259a5b789f6SErmal Luçi 				YYERROR;
2260a5b789f6SErmal Luçi 			}
2261a5b789f6SErmal Luçi 			fairq_opts.linkshare.m1 = $3;
2262a5b789f6SErmal Luçi 			fairq_opts.linkshare.d = $4;
2263a5b789f6SErmal Luçi 			fairq_opts.linkshare.m2 = $5;
2264a5b789f6SErmal Luçi 			fairq_opts.linkshare.used = 1;
2265a5b789f6SErmal Luçi 		}
2266a5b789f6SErmal Luçi 		| HOGS bandwidth {
2267a5b789f6SErmal Luçi 			fairq_opts.hogs_bw = $2;
2268a5b789f6SErmal Luçi 		}
2269a5b789f6SErmal Luçi 		| BUCKETS number {
2270a5b789f6SErmal Luçi 			fairq_opts.nbuckets = $2;
2271a5b789f6SErmal Luçi 		}
2272a5b789f6SErmal Luçi 		| STRING	{
2273a5b789f6SErmal Luçi 			if (!strcmp($1, "default"))
2274a5b789f6SErmal Luçi 				fairq_opts.flags |= FARF_DEFAULTCLASS;
2275a5b789f6SErmal Luçi 			else if (!strcmp($1, "red"))
2276a5b789f6SErmal Luçi 				fairq_opts.flags |= FARF_RED;
2277a5b789f6SErmal Luçi 			else if (!strcmp($1, "ecn"))
2278a5b789f6SErmal Luçi 				fairq_opts.flags |= FARF_RED|FARF_ECN;
2279a5b789f6SErmal Luçi 			else if (!strcmp($1, "rio"))
2280a5b789f6SErmal Luçi 				fairq_opts.flags |= FARF_RIO;
22810a70aaf8SLuiz Otavio O Souza 			else if (!strcmp($1, "codel"))
22820a70aaf8SLuiz Otavio O Souza 				fairq_opts.flags |= FARF_CODEL;
2283a5b789f6SErmal Luçi 			else {
2284a5b789f6SErmal Luçi 				yyerror("unknown fairq flag \"%s\"", $1);
2285a5b789f6SErmal Luçi 				free($1);
2286a5b789f6SErmal Luçi 				YYERROR;
2287a5b789f6SErmal Luçi 			}
2288a5b789f6SErmal Luçi 			free($1);
2289a5b789f6SErmal Luçi 		}
2290a5b789f6SErmal Luçi 		;
2291a5b789f6SErmal Luçi 
22920a70aaf8SLuiz Otavio O Souza codel_opts	:	{
22930a70aaf8SLuiz Otavio O Souza 				bzero(&codel_opts,
22940a70aaf8SLuiz Otavio O Souza 				    sizeof(struct codel_opts));
22950a70aaf8SLuiz Otavio O Souza 			}
22960a70aaf8SLuiz Otavio O Souza 		    codelopts_list				{
22970a70aaf8SLuiz Otavio O Souza 			$$ = codel_opts;
22980a70aaf8SLuiz Otavio O Souza 		}
22990a70aaf8SLuiz Otavio O Souza 		;
23000a70aaf8SLuiz Otavio O Souza 
23010a70aaf8SLuiz Otavio O Souza codelopts_list	: codelopts_item
23020a70aaf8SLuiz Otavio O Souza 		| codelopts_list comma codelopts_item
23030a70aaf8SLuiz Otavio O Souza 		;
23040a70aaf8SLuiz Otavio O Souza 
23050a70aaf8SLuiz Otavio O Souza codelopts_item	: INTERVAL number				{
23060a70aaf8SLuiz Otavio O Souza 			if (codel_opts.interval) {
23070a70aaf8SLuiz Otavio O Souza 				yyerror("interval already specified");
23080a70aaf8SLuiz Otavio O Souza 				YYERROR;
23090a70aaf8SLuiz Otavio O Souza 			}
23100a70aaf8SLuiz Otavio O Souza 			codel_opts.interval = $2;
23110a70aaf8SLuiz Otavio O Souza 		}
23120a70aaf8SLuiz Otavio O Souza 		| TARGET number					{
23130a70aaf8SLuiz Otavio O Souza 			if (codel_opts.target) {
23140a70aaf8SLuiz Otavio O Souza 				yyerror("target already specified");
23150a70aaf8SLuiz Otavio O Souza 				YYERROR;
23160a70aaf8SLuiz Otavio O Souza 			}
23170a70aaf8SLuiz Otavio O Souza 			codel_opts.target = $2;
23180a70aaf8SLuiz Otavio O Souza 		}
23190a70aaf8SLuiz Otavio O Souza 		| STRING					{
23200a70aaf8SLuiz Otavio O Souza 			if (!strcmp($1, "ecn"))
23210a70aaf8SLuiz Otavio O Souza 				codel_opts.ecn = 1;
23220a70aaf8SLuiz Otavio O Souza 			else {
23230a70aaf8SLuiz Otavio O Souza 				yyerror("unknown codel option \"%s\"", $1);
23240a70aaf8SLuiz Otavio O Souza 				free($1);
23250a70aaf8SLuiz Otavio O Souza 				YYERROR;
23260a70aaf8SLuiz Otavio O Souza 			}
23270a70aaf8SLuiz Otavio O Souza 			free($1);
23280a70aaf8SLuiz Otavio O Souza 		}
23290a70aaf8SLuiz Otavio O Souza 		;
23300a70aaf8SLuiz Otavio O Souza 
23313b3a8eb9SGleb Smirnoff qassign		: /* empty */		{ $$ = NULL; }
23323b3a8eb9SGleb Smirnoff 		| qassign_item		{ $$ = $1; }
23333b3a8eb9SGleb Smirnoff 		| '{' optnl qassign_list '}'	{ $$ = $3; }
23343b3a8eb9SGleb Smirnoff 		;
23353b3a8eb9SGleb Smirnoff 
23363b3a8eb9SGleb Smirnoff qassign_list	: qassign_item optnl		{ $$ = $1; }
23373b3a8eb9SGleb Smirnoff 		| qassign_list comma qassign_item optnl	{
23383b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
23393b3a8eb9SGleb Smirnoff 			$1->tail = $3;
23403b3a8eb9SGleb Smirnoff 			$$ = $1;
23413b3a8eb9SGleb Smirnoff 		}
23423b3a8eb9SGleb Smirnoff 		;
23433b3a8eb9SGleb Smirnoff 
23443b3a8eb9SGleb Smirnoff qassign_item	: STRING			{
23453b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_queue));
23463b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
23473b3a8eb9SGleb Smirnoff 				err(1, "qassign_item: calloc");
23483b3a8eb9SGleb Smirnoff 			if (strlcpy($$->queue, $1, sizeof($$->queue)) >=
23493b3a8eb9SGleb Smirnoff 			    sizeof($$->queue)) {
23503b3a8eb9SGleb Smirnoff 				yyerror("queue name '%s' too long (max "
23513b3a8eb9SGleb Smirnoff 				    "%d chars)", $1, sizeof($$->queue)-1);
23523b3a8eb9SGleb Smirnoff 				free($1);
23533b3a8eb9SGleb Smirnoff 				free($$);
23543b3a8eb9SGleb Smirnoff 				YYERROR;
23553b3a8eb9SGleb Smirnoff 			}
23563b3a8eb9SGleb Smirnoff 			free($1);
23573b3a8eb9SGleb Smirnoff 			$$->next = NULL;
23583b3a8eb9SGleb Smirnoff 			$$->tail = $$;
23593b3a8eb9SGleb Smirnoff 		}
23603b3a8eb9SGleb Smirnoff 		;
23613b3a8eb9SGleb Smirnoff 
23623b3a8eb9SGleb Smirnoff pfrule		: action dir logquick interface route af proto fromto
23633b3a8eb9SGleb Smirnoff 		    filter_opts
23643b3a8eb9SGleb Smirnoff 		{
2365e9eb0941SKristof Provost 			struct pfctl_rule	 r;
23663b3a8eb9SGleb Smirnoff 			struct node_state_opt	*o;
23673b3a8eb9SGleb Smirnoff 			struct node_proto	*proto;
23683b3a8eb9SGleb Smirnoff 			int			 srctrack = 0;
23693b3a8eb9SGleb Smirnoff 			int			 statelock = 0;
23703b3a8eb9SGleb Smirnoff 			int			 adaptive = 0;
23713b3a8eb9SGleb Smirnoff 			int			 defaults = 0;
23723b3a8eb9SGleb Smirnoff 
23733b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_FILTER))
23743b3a8eb9SGleb Smirnoff 				YYERROR;
23753b3a8eb9SGleb Smirnoff 
23763b3a8eb9SGleb Smirnoff 			memset(&r, 0, sizeof(r));
23773b3a8eb9SGleb Smirnoff 
23783b3a8eb9SGleb Smirnoff 			r.action = $1.b1;
23793b3a8eb9SGleb Smirnoff 			switch ($1.b2) {
23803b3a8eb9SGleb Smirnoff 			case PFRULE_RETURNRST:
23813b3a8eb9SGleb Smirnoff 				r.rule_flag |= PFRULE_RETURNRST;
23823b3a8eb9SGleb Smirnoff 				r.return_ttl = $1.w;
23833b3a8eb9SGleb Smirnoff 				break;
23843b3a8eb9SGleb Smirnoff 			case PFRULE_RETURNICMP:
23853b3a8eb9SGleb Smirnoff 				r.rule_flag |= PFRULE_RETURNICMP;
23863b3a8eb9SGleb Smirnoff 				r.return_icmp = $1.w;
23873b3a8eb9SGleb Smirnoff 				r.return_icmp6 = $1.w2;
23883b3a8eb9SGleb Smirnoff 				break;
23893b3a8eb9SGleb Smirnoff 			case PFRULE_RETURN:
23903b3a8eb9SGleb Smirnoff 				r.rule_flag |= PFRULE_RETURN;
23913b3a8eb9SGleb Smirnoff 				r.return_icmp = $1.w;
23923b3a8eb9SGleb Smirnoff 				r.return_icmp6 = $1.w2;
23933b3a8eb9SGleb Smirnoff 				break;
23943b3a8eb9SGleb Smirnoff 			}
23953b3a8eb9SGleb Smirnoff 			r.direction = $2;
23963b3a8eb9SGleb Smirnoff 			r.log = $3.log;
23973b3a8eb9SGleb Smirnoff 			r.logif = $3.logif;
23983b3a8eb9SGleb Smirnoff 			r.quick = $3.quick;
23993b3a8eb9SGleb Smirnoff 			r.prob = $9.prob;
24003b3a8eb9SGleb Smirnoff 			r.rtableid = $9.rtableid;
24013b3a8eb9SGleb Smirnoff 
240239282ef3SKajetan Staszkiewicz 			if ($9.nodf)
240339282ef3SKajetan Staszkiewicz 				r.scrub_flags |= PFSTATE_NODF;
240439282ef3SKajetan Staszkiewicz 			if ($9.randomid)
240539282ef3SKajetan Staszkiewicz 				r.scrub_flags |= PFSTATE_RANDOMID;
240639282ef3SKajetan Staszkiewicz 			if ($9.minttl)
240739282ef3SKajetan Staszkiewicz 				r.min_ttl = $9.minttl;
240839282ef3SKajetan Staszkiewicz 			if ($9.max_mss)
240939282ef3SKajetan Staszkiewicz 				r.max_mss = $9.max_mss;
241039282ef3SKajetan Staszkiewicz 			if ($9.marker & FOM_SETTOS) {
241139282ef3SKajetan Staszkiewicz 				r.scrub_flags |= PFSTATE_SETTOS;
241239282ef3SKajetan Staszkiewicz 				r.set_tos = $9.settos;
241339282ef3SKajetan Staszkiewicz 			}
241439282ef3SKajetan Staszkiewicz 			if ($9.marker & FOM_SCRUB_TCP)
241539282ef3SKajetan Staszkiewicz 				r.scrub_flags |= PFSTATE_SCRUB_TCP;
241639282ef3SKajetan Staszkiewicz 
24173e248e0fSKristof Provost 			if ($9.marker & FOM_PRIO) {
24183e248e0fSKristof Provost 				if ($9.prio == 0)
24193e248e0fSKristof Provost 					r.prio = PF_PRIO_ZERO;
24203e248e0fSKristof Provost 				else
24213e248e0fSKristof Provost 					r.prio = $9.prio;
24223e248e0fSKristof Provost 			}
24233e248e0fSKristof Provost 			if ($9.marker & FOM_SETPRIO) {
24243e248e0fSKristof Provost 				r.set_prio[0] = $9.set_prio[0];
24253e248e0fSKristof Provost 				r.set_prio[1] = $9.set_prio[1];
24263e248e0fSKristof Provost 				r.scrub_flags |= PFSTATE_SETPRIO;
24273e248e0fSKristof Provost 			}
24283e248e0fSKristof Provost 
2429*aa69fdf1SKristof Provost 			if ($9.marker & FOM_AFTO) {
2430*aa69fdf1SKristof Provost 				if (!$6) {
2431*aa69fdf1SKristof Provost 					yyerror("must indicate source address "
2432*aa69fdf1SKristof Provost 					    "family with af-to");
2433*aa69fdf1SKristof Provost 					YYERROR;
2434*aa69fdf1SKristof Provost 				}
2435*aa69fdf1SKristof Provost 				if ($6 == $9.nat.af) {
2436*aa69fdf1SKristof Provost 					yyerror("incorrect address family "
2437*aa69fdf1SKristof Provost 					    "translation");
2438*aa69fdf1SKristof Provost 					YYERROR;
2439*aa69fdf1SKristof Provost 				}
2440*aa69fdf1SKristof Provost 			}
2441*aa69fdf1SKristof Provost 
24423b3a8eb9SGleb Smirnoff 			r.af = $6;
24433b3a8eb9SGleb Smirnoff 			if ($9.tag)
24443b3a8eb9SGleb Smirnoff 				if (strlcpy(r.tagname, $9.tag,
24453b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
24463b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
24473b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
24483b3a8eb9SGleb Smirnoff 					YYERROR;
24493b3a8eb9SGleb Smirnoff 				}
24503b3a8eb9SGleb Smirnoff 			if ($9.match_tag)
24513b3a8eb9SGleb Smirnoff 				if (strlcpy(r.match_tagname, $9.match_tag,
24523b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
24533b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
24543b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
24553b3a8eb9SGleb Smirnoff 					YYERROR;
24563b3a8eb9SGleb Smirnoff 				}
24573b3a8eb9SGleb Smirnoff 			r.match_tag_not = $9.match_tag_not;
24583b3a8eb9SGleb Smirnoff 			if (rule_label(&r, $9.label))
24593b3a8eb9SGleb Smirnoff 				YYERROR;
24606fcc8e04SKristof Provost 			for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++)
24616fcc8e04SKristof Provost 				free($9.label[i]);
246276c5eeccSKristof Provost 			r.ridentifier = $9.ridentifier;
24633b3a8eb9SGleb Smirnoff 			r.flags = $9.flags.b1;
24643b3a8eb9SGleb Smirnoff 			r.flagset = $9.flags.b2;
24653b3a8eb9SGleb Smirnoff 			if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
24663b3a8eb9SGleb Smirnoff 				yyerror("flags always false");
24673b3a8eb9SGleb Smirnoff 				YYERROR;
24683b3a8eb9SGleb Smirnoff 			}
24693b3a8eb9SGleb Smirnoff 			if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
24703b3a8eb9SGleb Smirnoff 				for (proto = $7; proto != NULL &&
24713b3a8eb9SGleb Smirnoff 				    proto->proto != IPPROTO_TCP;
24723b3a8eb9SGleb Smirnoff 				    proto = proto->next)
24733b3a8eb9SGleb Smirnoff 					;	/* nothing */
24743b3a8eb9SGleb Smirnoff 				if (proto == NULL && $7 != NULL) {
24753b3a8eb9SGleb Smirnoff 					if ($9.flags.b1 || $9.flags.b2)
24763b3a8eb9SGleb Smirnoff 						yyerror(
24773b3a8eb9SGleb Smirnoff 						    "flags only apply to tcp");
24783b3a8eb9SGleb Smirnoff 					if ($8.src_os)
24793b3a8eb9SGleb Smirnoff 						yyerror(
24803b3a8eb9SGleb Smirnoff 						    "OS fingerprinting only "
24813b3a8eb9SGleb Smirnoff 						    "apply to tcp");
24823b3a8eb9SGleb Smirnoff 					YYERROR;
24833b3a8eb9SGleb Smirnoff 				}
24843b3a8eb9SGleb Smirnoff #if 0
24853b3a8eb9SGleb Smirnoff 				if (($9.flags.b1 & parse_flags("S")) == 0 &&
24863b3a8eb9SGleb Smirnoff 				    $8.src_os) {
24873b3a8eb9SGleb Smirnoff 					yyerror("OS fingerprinting requires "
24883b3a8eb9SGleb Smirnoff 					    "the SYN TCP flag (flags S/SA)");
24893b3a8eb9SGleb Smirnoff 					YYERROR;
24903b3a8eb9SGleb Smirnoff 				}
24913b3a8eb9SGleb Smirnoff #endif
24923b3a8eb9SGleb Smirnoff 			}
24933b3a8eb9SGleb Smirnoff 
24943b3a8eb9SGleb Smirnoff 			r.tos = $9.tos;
24953b3a8eb9SGleb Smirnoff 			r.keep_state = $9.keep.action;
24963b3a8eb9SGleb Smirnoff 			o = $9.keep.options;
24973b3a8eb9SGleb Smirnoff 
24983b3a8eb9SGleb Smirnoff 			/* 'keep state' by default on pass rules. */
24993b3a8eb9SGleb Smirnoff 			if (!r.keep_state && !r.action &&
25003b3a8eb9SGleb Smirnoff 			    !($9.marker & FOM_KEEP)) {
25013b3a8eb9SGleb Smirnoff 				r.keep_state = PF_STATE_NORMAL;
25023b3a8eb9SGleb Smirnoff 				o = keep_state_defaults;
25033b3a8eb9SGleb Smirnoff 				defaults = 1;
25043b3a8eb9SGleb Smirnoff 			}
25053b3a8eb9SGleb Smirnoff 
25063b3a8eb9SGleb Smirnoff 			while (o) {
25073b3a8eb9SGleb Smirnoff 				struct node_state_opt	*p = o;
25083b3a8eb9SGleb Smirnoff 
25093b3a8eb9SGleb Smirnoff 				switch (o->type) {
25103b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_MAX:
25113b3a8eb9SGleb Smirnoff 					if (r.max_states) {
25123b3a8eb9SGleb Smirnoff 						yyerror("state option 'max' "
25133b3a8eb9SGleb Smirnoff 						    "multiple definitions");
25143b3a8eb9SGleb Smirnoff 						YYERROR;
25153b3a8eb9SGleb Smirnoff 					}
25163b3a8eb9SGleb Smirnoff 					r.max_states = o->data.max_states;
25173b3a8eb9SGleb Smirnoff 					break;
25183b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_NOSYNC:
25193b3a8eb9SGleb Smirnoff 					if (r.rule_flag & PFRULE_NOSYNC) {
25203b3a8eb9SGleb Smirnoff 						yyerror("state option 'sync' "
25213b3a8eb9SGleb Smirnoff 						    "multiple definitions");
25223b3a8eb9SGleb Smirnoff 						YYERROR;
25233b3a8eb9SGleb Smirnoff 					}
25243b3a8eb9SGleb Smirnoff 					r.rule_flag |= PFRULE_NOSYNC;
25253b3a8eb9SGleb Smirnoff 					break;
25263b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_SRCTRACK:
25273b3a8eb9SGleb Smirnoff 					if (srctrack) {
25283b3a8eb9SGleb Smirnoff 						yyerror("state option "
25293b3a8eb9SGleb Smirnoff 						    "'source-track' "
25303b3a8eb9SGleb Smirnoff 						    "multiple definitions");
25313b3a8eb9SGleb Smirnoff 						YYERROR;
25323b3a8eb9SGleb Smirnoff 					}
25333b3a8eb9SGleb Smirnoff 					srctrack =  o->data.src_track;
25343b3a8eb9SGleb Smirnoff 					r.rule_flag |= PFRULE_SRCTRACK;
25353b3a8eb9SGleb Smirnoff 					break;
25363b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_MAX_SRC_STATES:
25373b3a8eb9SGleb Smirnoff 					if (r.max_src_states) {
25383b3a8eb9SGleb Smirnoff 						yyerror("state option "
25393b3a8eb9SGleb Smirnoff 						    "'max-src-states' "
25403b3a8eb9SGleb Smirnoff 						    "multiple definitions");
25413b3a8eb9SGleb Smirnoff 						YYERROR;
25423b3a8eb9SGleb Smirnoff 					}
25433b3a8eb9SGleb Smirnoff 					if (o->data.max_src_states == 0) {
25443b3a8eb9SGleb Smirnoff 						yyerror("'max-src-states' must "
25453b3a8eb9SGleb Smirnoff 						    "be > 0");
25463b3a8eb9SGleb Smirnoff 						YYERROR;
25473b3a8eb9SGleb Smirnoff 					}
25483b3a8eb9SGleb Smirnoff 					r.max_src_states =
25493b3a8eb9SGleb Smirnoff 					    o->data.max_src_states;
25503b3a8eb9SGleb Smirnoff 					r.rule_flag |= PFRULE_SRCTRACK;
25513b3a8eb9SGleb Smirnoff 					break;
25523b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_OVERLOAD:
25533b3a8eb9SGleb Smirnoff 					if (r.overload_tblname[0]) {
25543b3a8eb9SGleb Smirnoff 						yyerror("multiple 'overload' "
25553b3a8eb9SGleb Smirnoff 						    "table definitions");
25563b3a8eb9SGleb Smirnoff 						YYERROR;
25573b3a8eb9SGleb Smirnoff 					}
25583b3a8eb9SGleb Smirnoff 					if (strlcpy(r.overload_tblname,
25593b3a8eb9SGleb Smirnoff 					    o->data.overload.tblname,
25603b3a8eb9SGleb Smirnoff 					    PF_TABLE_NAME_SIZE) >=
25613b3a8eb9SGleb Smirnoff 					    PF_TABLE_NAME_SIZE) {
25623b3a8eb9SGleb Smirnoff 						yyerror("state option: "
25633b3a8eb9SGleb Smirnoff 						    "strlcpy");
25643b3a8eb9SGleb Smirnoff 						YYERROR;
25653b3a8eb9SGleb Smirnoff 					}
25663b3a8eb9SGleb Smirnoff 					r.flush = o->data.overload.flush;
25673b3a8eb9SGleb Smirnoff 					break;
25683b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_MAX_SRC_CONN:
25693b3a8eb9SGleb Smirnoff 					if (r.max_src_conn) {
25703b3a8eb9SGleb Smirnoff 						yyerror("state option "
25713b3a8eb9SGleb Smirnoff 						    "'max-src-conn' "
25723b3a8eb9SGleb Smirnoff 						    "multiple definitions");
25733b3a8eb9SGleb Smirnoff 						YYERROR;
25743b3a8eb9SGleb Smirnoff 					}
25753b3a8eb9SGleb Smirnoff 					if (o->data.max_src_conn == 0) {
25763b3a8eb9SGleb Smirnoff 						yyerror("'max-src-conn' "
25773b3a8eb9SGleb Smirnoff 						    "must be > 0");
25783b3a8eb9SGleb Smirnoff 						YYERROR;
25793b3a8eb9SGleb Smirnoff 					}
25803b3a8eb9SGleb Smirnoff 					r.max_src_conn =
25813b3a8eb9SGleb Smirnoff 					    o->data.max_src_conn;
25823b3a8eb9SGleb Smirnoff 					r.rule_flag |= PFRULE_SRCTRACK |
25833b3a8eb9SGleb Smirnoff 					    PFRULE_RULESRCTRACK;
25843b3a8eb9SGleb Smirnoff 					break;
25853b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_MAX_SRC_CONN_RATE:
25863b3a8eb9SGleb Smirnoff 					if (r.max_src_conn_rate.limit) {
25873b3a8eb9SGleb Smirnoff 						yyerror("state option "
25883b3a8eb9SGleb Smirnoff 						    "'max-src-conn-rate' "
25893b3a8eb9SGleb Smirnoff 						    "multiple definitions");
25903b3a8eb9SGleb Smirnoff 						YYERROR;
25913b3a8eb9SGleb Smirnoff 					}
25923b3a8eb9SGleb Smirnoff 					if (!o->data.max_src_conn_rate.limit ||
25933b3a8eb9SGleb Smirnoff 					    !o->data.max_src_conn_rate.seconds) {
25943b3a8eb9SGleb Smirnoff 						yyerror("'max-src-conn-rate' "
25953b3a8eb9SGleb Smirnoff 						    "values must be > 0");
25963b3a8eb9SGleb Smirnoff 						YYERROR;
25973b3a8eb9SGleb Smirnoff 					}
25983b3a8eb9SGleb Smirnoff 					if (o->data.max_src_conn_rate.limit >
25993b3a8eb9SGleb Smirnoff 					    PF_THRESHOLD_MAX) {
26003b3a8eb9SGleb Smirnoff 						yyerror("'max-src-conn-rate' "
26013b3a8eb9SGleb Smirnoff 						    "maximum rate must be < %u",
26023b3a8eb9SGleb Smirnoff 						    PF_THRESHOLD_MAX);
26033b3a8eb9SGleb Smirnoff 						YYERROR;
26043b3a8eb9SGleb Smirnoff 					}
26053b3a8eb9SGleb Smirnoff 					r.max_src_conn_rate.limit =
26063b3a8eb9SGleb Smirnoff 					    o->data.max_src_conn_rate.limit;
26073b3a8eb9SGleb Smirnoff 					r.max_src_conn_rate.seconds =
26083b3a8eb9SGleb Smirnoff 					    o->data.max_src_conn_rate.seconds;
26093b3a8eb9SGleb Smirnoff 					r.rule_flag |= PFRULE_SRCTRACK |
26103b3a8eb9SGleb Smirnoff 					    PFRULE_RULESRCTRACK;
26113b3a8eb9SGleb Smirnoff 					break;
26123b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_MAX_SRC_NODES:
26133b3a8eb9SGleb Smirnoff 					if (r.max_src_nodes) {
26143b3a8eb9SGleb Smirnoff 						yyerror("state option "
26153b3a8eb9SGleb Smirnoff 						    "'max-src-nodes' "
26163b3a8eb9SGleb Smirnoff 						    "multiple definitions");
26173b3a8eb9SGleb Smirnoff 						YYERROR;
26183b3a8eb9SGleb Smirnoff 					}
26193b3a8eb9SGleb Smirnoff 					if (o->data.max_src_nodes == 0) {
26203b3a8eb9SGleb Smirnoff 						yyerror("'max-src-nodes' must "
26213b3a8eb9SGleb Smirnoff 						    "be > 0");
26223b3a8eb9SGleb Smirnoff 						YYERROR;
26233b3a8eb9SGleb Smirnoff 					}
26243b3a8eb9SGleb Smirnoff 					r.max_src_nodes =
26253b3a8eb9SGleb Smirnoff 					    o->data.max_src_nodes;
26263b3a8eb9SGleb Smirnoff 					r.rule_flag |= PFRULE_SRCTRACK |
26273b3a8eb9SGleb Smirnoff 					    PFRULE_RULESRCTRACK;
26283b3a8eb9SGleb Smirnoff 					break;
26293b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_STATELOCK:
26303b3a8eb9SGleb Smirnoff 					if (statelock) {
26313b3a8eb9SGleb Smirnoff 						yyerror("state locking option: "
26323b3a8eb9SGleb Smirnoff 						    "multiple definitions");
26333b3a8eb9SGleb Smirnoff 						YYERROR;
26343b3a8eb9SGleb Smirnoff 					}
26353b3a8eb9SGleb Smirnoff 					statelock = 1;
26363b3a8eb9SGleb Smirnoff 					r.rule_flag |= o->data.statelock;
26373b3a8eb9SGleb Smirnoff 					break;
26383b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_SLOPPY:
26393b3a8eb9SGleb Smirnoff 					if (r.rule_flag & PFRULE_STATESLOPPY) {
26403b3a8eb9SGleb Smirnoff 						yyerror("state sloppy option: "
26413b3a8eb9SGleb Smirnoff 						    "multiple definitions");
26423b3a8eb9SGleb Smirnoff 						YYERROR;
26433b3a8eb9SGleb Smirnoff 					}
26443b3a8eb9SGleb Smirnoff 					r.rule_flag |= PFRULE_STATESLOPPY;
26453b3a8eb9SGleb Smirnoff 					break;
2646baf9b6d0SKristof Provost 				case PF_STATE_OPT_PFLOW:
2647baf9b6d0SKristof Provost 					if (r.rule_flag & PFRULE_PFLOW) {
2648baf9b6d0SKristof Provost 						yyerror("state pflow option: "
2649baf9b6d0SKristof Provost 						    "multiple definitions");
2650baf9b6d0SKristof Provost 						YYERROR;
2651baf9b6d0SKristof Provost 					}
2652baf9b6d0SKristof Provost 					r.rule_flag |= PFRULE_PFLOW;
2653baf9b6d0SKristof Provost 					break;
26543b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_TIMEOUT:
26553b3a8eb9SGleb Smirnoff 					if (o->data.timeout.number ==
26563b3a8eb9SGleb Smirnoff 					    PFTM_ADAPTIVE_START ||
26573b3a8eb9SGleb Smirnoff 					    o->data.timeout.number ==
26583b3a8eb9SGleb Smirnoff 					    PFTM_ADAPTIVE_END)
26593b3a8eb9SGleb Smirnoff 						adaptive = 1;
26603b3a8eb9SGleb Smirnoff 					if (r.timeout[o->data.timeout.number]) {
26613b3a8eb9SGleb Smirnoff 						yyerror("state timeout %s "
26623b3a8eb9SGleb Smirnoff 						    "multiple definitions",
26633b3a8eb9SGleb Smirnoff 						    pf_timeouts[o->data.
26643b3a8eb9SGleb Smirnoff 						    timeout.number].name);
26653b3a8eb9SGleb Smirnoff 						YYERROR;
26663b3a8eb9SGleb Smirnoff 					}
26673b3a8eb9SGleb Smirnoff 					r.timeout[o->data.timeout.number] =
26683b3a8eb9SGleb Smirnoff 					    o->data.timeout.seconds;
26693b3a8eb9SGleb Smirnoff 				}
26703b3a8eb9SGleb Smirnoff 				o = o->next;
26713b3a8eb9SGleb Smirnoff 				if (!defaults)
26723b3a8eb9SGleb Smirnoff 					free(p);
26733b3a8eb9SGleb Smirnoff 			}
26743b3a8eb9SGleb Smirnoff 
26753b3a8eb9SGleb Smirnoff 			/* 'flags S/SA' by default on stateful rules */
26763b3a8eb9SGleb Smirnoff 			if (!r.action && !r.flags && !r.flagset &&
26773b3a8eb9SGleb Smirnoff 			    !$9.fragment && !($9.marker & FOM_FLAGS) &&
26783b3a8eb9SGleb Smirnoff 			    r.keep_state) {
26793b3a8eb9SGleb Smirnoff 				r.flags = parse_flags("S");
26803b3a8eb9SGleb Smirnoff 				r.flagset =  parse_flags("SA");
26813b3a8eb9SGleb Smirnoff 			}
26823b3a8eb9SGleb Smirnoff 			if (!adaptive && r.max_states) {
26833b3a8eb9SGleb Smirnoff 				r.timeout[PFTM_ADAPTIVE_START] =
26843b3a8eb9SGleb Smirnoff 				    (r.max_states / 10) * 6;
26853b3a8eb9SGleb Smirnoff 				r.timeout[PFTM_ADAPTIVE_END] =
26863b3a8eb9SGleb Smirnoff 				    (r.max_states / 10) * 12;
26873b3a8eb9SGleb Smirnoff 			}
26883b3a8eb9SGleb Smirnoff 			if (r.rule_flag & PFRULE_SRCTRACK) {
26893b3a8eb9SGleb Smirnoff 				if (srctrack == PF_SRCTRACK_GLOBAL &&
26903b3a8eb9SGleb Smirnoff 				    r.max_src_nodes) {
26913b3a8eb9SGleb Smirnoff 					yyerror("'max-src-nodes' is "
26923b3a8eb9SGleb Smirnoff 					    "incompatible with "
26933b3a8eb9SGleb Smirnoff 					    "'source-track global'");
26943b3a8eb9SGleb Smirnoff 					YYERROR;
26953b3a8eb9SGleb Smirnoff 				}
26963b3a8eb9SGleb Smirnoff 				if (srctrack == PF_SRCTRACK_GLOBAL &&
26973b3a8eb9SGleb Smirnoff 				    r.max_src_conn) {
26983b3a8eb9SGleb Smirnoff 					yyerror("'max-src-conn' is "
26993b3a8eb9SGleb Smirnoff 					    "incompatible with "
27003b3a8eb9SGleb Smirnoff 					    "'source-track global'");
27013b3a8eb9SGleb Smirnoff 					YYERROR;
27023b3a8eb9SGleb Smirnoff 				}
27033b3a8eb9SGleb Smirnoff 				if (srctrack == PF_SRCTRACK_GLOBAL &&
27043b3a8eb9SGleb Smirnoff 				    r.max_src_conn_rate.seconds) {
27053b3a8eb9SGleb Smirnoff 					yyerror("'max-src-conn-rate' is "
27063b3a8eb9SGleb Smirnoff 					    "incompatible with "
27073b3a8eb9SGleb Smirnoff 					    "'source-track global'");
27083b3a8eb9SGleb Smirnoff 					YYERROR;
27093b3a8eb9SGleb Smirnoff 				}
27103b3a8eb9SGleb Smirnoff 				if (r.timeout[PFTM_SRC_NODE] <
27113b3a8eb9SGleb Smirnoff 				    r.max_src_conn_rate.seconds)
27123b3a8eb9SGleb Smirnoff 					r.timeout[PFTM_SRC_NODE] =
27133b3a8eb9SGleb Smirnoff 					    r.max_src_conn_rate.seconds;
27143b3a8eb9SGleb Smirnoff 				r.rule_flag |= PFRULE_SRCTRACK;
27153b3a8eb9SGleb Smirnoff 				if (srctrack == PF_SRCTRACK_RULE)
27163b3a8eb9SGleb Smirnoff 					r.rule_flag |= PFRULE_RULESRCTRACK;
27173b3a8eb9SGleb Smirnoff 			}
27183b3a8eb9SGleb Smirnoff 			if (r.keep_state && !statelock)
27193b3a8eb9SGleb Smirnoff 				r.rule_flag |= default_statelock;
27203b3a8eb9SGleb Smirnoff 
27213b3a8eb9SGleb Smirnoff 			if ($9.fragment)
27223b3a8eb9SGleb Smirnoff 				r.rule_flag |= PFRULE_FRAGMENT;
27233b3a8eb9SGleb Smirnoff 			r.allow_opts = $9.allowopts;
27243b3a8eb9SGleb Smirnoff 
27253b3a8eb9SGleb Smirnoff 			decide_address_family($8.src.host, &r.af);
27263b3a8eb9SGleb Smirnoff 			decide_address_family($8.dst.host, &r.af);
2727*aa69fdf1SKristof Provost 			r.naf = r.af;
27283b3a8eb9SGleb Smirnoff 
27293b3a8eb9SGleb Smirnoff 			if ($5.rt) {
27303b3a8eb9SGleb Smirnoff 				if (!r.direction) {
27313b3a8eb9SGleb Smirnoff 					yyerror("direction must be explicit "
27323b3a8eb9SGleb Smirnoff 					    "with rules that specify routing");
27333b3a8eb9SGleb Smirnoff 					YYERROR;
27343b3a8eb9SGleb Smirnoff 				}
27353b3a8eb9SGleb Smirnoff 				r.rt = $5.rt;
27363b3a8eb9SGleb Smirnoff 				r.rpool.opts = $5.pool_opts;
27373b3a8eb9SGleb Smirnoff 				if ($5.key != NULL)
27383b3a8eb9SGleb Smirnoff 					memcpy(&r.rpool.key, $5.key,
27393b3a8eb9SGleb Smirnoff 					    sizeof(struct pf_poolhashkey));
27403b3a8eb9SGleb Smirnoff 			}
2741813196a1SKristof Provost 			if (r.rt) {
27423b3a8eb9SGleb Smirnoff 				decide_address_family($5.host, &r.af);
27433b3a8eb9SGleb Smirnoff 				remove_invalid_hosts(&$5.host, &r.af);
27443b3a8eb9SGleb Smirnoff 				if ($5.host == NULL) {
27453b3a8eb9SGleb Smirnoff 					yyerror("no routing address with "
27463b3a8eb9SGleb Smirnoff 					    "matching address family found.");
27473b3a8eb9SGleb Smirnoff 					YYERROR;
27483b3a8eb9SGleb Smirnoff 				}
27493b3a8eb9SGleb Smirnoff 				if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
27503b3a8eb9SGleb Smirnoff 				    PF_POOL_NONE && ($5.host->next != NULL ||
27513b3a8eb9SGleb Smirnoff 				    $5.host->addr.type == PF_ADDR_TABLE ||
27523b3a8eb9SGleb Smirnoff 				    DYNIF_MULTIADDR($5.host->addr)))
27533b3a8eb9SGleb Smirnoff 					r.rpool.opts |= PF_POOL_ROUNDROBIN;
27543b3a8eb9SGleb Smirnoff 				if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
27553b3a8eb9SGleb Smirnoff 				    PF_POOL_ROUNDROBIN &&
27563b3a8eb9SGleb Smirnoff 				    disallow_table($5.host, "tables are only "
27573b3a8eb9SGleb Smirnoff 				    "supported in round-robin routing pools"))
27583b3a8eb9SGleb Smirnoff 					YYERROR;
27593b3a8eb9SGleb Smirnoff 				if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
27603b3a8eb9SGleb Smirnoff 				    PF_POOL_ROUNDROBIN &&
27613b3a8eb9SGleb Smirnoff 				    disallow_alias($5.host, "interface (%s) "
27623b3a8eb9SGleb Smirnoff 				    "is only supported in round-robin "
27633b3a8eb9SGleb Smirnoff 				    "routing pools"))
27643b3a8eb9SGleb Smirnoff 					YYERROR;
27653b3a8eb9SGleb Smirnoff 				if ($5.host->next != NULL) {
27663b3a8eb9SGleb Smirnoff 					if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
27673b3a8eb9SGleb Smirnoff 					    PF_POOL_ROUNDROBIN) {
27683b3a8eb9SGleb Smirnoff 						yyerror("r.rpool.opts must "
27693b3a8eb9SGleb Smirnoff 						    "be PF_POOL_ROUNDROBIN");
27703b3a8eb9SGleb Smirnoff 						YYERROR;
27713b3a8eb9SGleb Smirnoff 					}
27723b3a8eb9SGleb Smirnoff 				}
27733b3a8eb9SGleb Smirnoff 			}
27743b3a8eb9SGleb Smirnoff 			if ($9.queues.qname != NULL) {
27753b3a8eb9SGleb Smirnoff 				if (strlcpy(r.qname, $9.queues.qname,
27763b3a8eb9SGleb Smirnoff 				    sizeof(r.qname)) >= sizeof(r.qname)) {
27773b3a8eb9SGleb Smirnoff 					yyerror("rule qname too long (max "
27783b3a8eb9SGleb Smirnoff 					    "%d chars)", sizeof(r.qname)-1);
27793b3a8eb9SGleb Smirnoff 					YYERROR;
27803b3a8eb9SGleb Smirnoff 				}
27813b3a8eb9SGleb Smirnoff 				free($9.queues.qname);
27823b3a8eb9SGleb Smirnoff 			}
27833b3a8eb9SGleb Smirnoff 			if ($9.queues.pqname != NULL) {
27843b3a8eb9SGleb Smirnoff 				if (strlcpy(r.pqname, $9.queues.pqname,
27853b3a8eb9SGleb Smirnoff 				    sizeof(r.pqname)) >= sizeof(r.pqname)) {
27863b3a8eb9SGleb Smirnoff 					yyerror("rule pqname too long (max "
27873b3a8eb9SGleb Smirnoff 					    "%d chars)", sizeof(r.pqname)-1);
27883b3a8eb9SGleb Smirnoff 					YYERROR;
27893b3a8eb9SGleb Smirnoff 				}
27903b3a8eb9SGleb Smirnoff 				free($9.queues.pqname);
27913b3a8eb9SGleb Smirnoff 			}
27923b3a8eb9SGleb Smirnoff #ifdef __FreeBSD__
27933b3a8eb9SGleb Smirnoff 			r.divert.port = $9.divert.port;
27943b3a8eb9SGleb Smirnoff #else
27953b3a8eb9SGleb Smirnoff 			if ((r.divert.port = $9.divert.port)) {
27963b3a8eb9SGleb Smirnoff 				if (r.direction == PF_OUT) {
27973b3a8eb9SGleb Smirnoff 					if ($9.divert.addr) {
27983b3a8eb9SGleb Smirnoff 						yyerror("address specified "
27993b3a8eb9SGleb Smirnoff 						    "for outgoing divert");
28003b3a8eb9SGleb Smirnoff 						YYERROR;
28013b3a8eb9SGleb Smirnoff 					}
28023b3a8eb9SGleb Smirnoff 					bzero(&r.divert.addr,
28033b3a8eb9SGleb Smirnoff 					    sizeof(r.divert.addr));
28043b3a8eb9SGleb Smirnoff 				} else {
28053b3a8eb9SGleb Smirnoff 					if (!$9.divert.addr) {
28063b3a8eb9SGleb Smirnoff 						yyerror("no address specified "
28073b3a8eb9SGleb Smirnoff 						    "for incoming divert");
28083b3a8eb9SGleb Smirnoff 						YYERROR;
28093b3a8eb9SGleb Smirnoff 					}
28103b3a8eb9SGleb Smirnoff 					if ($9.divert.addr->af != r.af) {
28113b3a8eb9SGleb Smirnoff 						yyerror("address family "
28123b3a8eb9SGleb Smirnoff 						    "mismatch for divert");
28133b3a8eb9SGleb Smirnoff 						YYERROR;
28143b3a8eb9SGleb Smirnoff 					}
28153b3a8eb9SGleb Smirnoff 					r.divert.addr =
28163b3a8eb9SGleb Smirnoff 					    $9.divert.addr->addr.v.a.addr;
28173b3a8eb9SGleb Smirnoff 				}
28183b3a8eb9SGleb Smirnoff 			}
28193b3a8eb9SGleb Smirnoff #endif
28203b3a8eb9SGleb Smirnoff 
282163b3c1c7SKristof Provost 			if ($9.dnpipe || $9.dnrpipe) {
282263b3c1c7SKristof Provost 				r.dnpipe = $9.dnpipe;
282363b3c1c7SKristof Provost 				r.dnrpipe = $9.dnrpipe;
282463b3c1c7SKristof Provost 				if ($9.free_flags & PFRULE_DN_IS_PIPE)
282563b3c1c7SKristof Provost 					r.free_flags |= PFRULE_DN_IS_PIPE;
282663b3c1c7SKristof Provost 				else
282763b3c1c7SKristof Provost 					r.free_flags |= PFRULE_DN_IS_QUEUE;
282863b3c1c7SKristof Provost 			}
282963b3c1c7SKristof Provost 
2830*aa69fdf1SKristof Provost 			if ($9.marker & FOM_AFTO)
2831*aa69fdf1SKristof Provost 				r.naf = $9.nat.af;
2832*aa69fdf1SKristof Provost 
2833*aa69fdf1SKristof Provost 			r.nat.opts = $9.nat.pool_opts.type;
2834*aa69fdf1SKristof Provost 			r.nat.opts |= $9.nat.pool_opts.opts;
2835*aa69fdf1SKristof Provost 			expand_rule(&r, $4, $5.host, $9.nat.rdr ? $9.nat.rdr->host : NULL,
2836*aa69fdf1SKristof Provost 			    $7, $8.src_os, $8.src.host, $8.src.port, $8.dst.host,
2837*aa69fdf1SKristof Provost 			    $8.dst.port, $9.uid, $9.gid, $9.rcv, $9.icmpspec, "");
28383b3a8eb9SGleb Smirnoff 		}
28393b3a8eb9SGleb Smirnoff 		;
28403b3a8eb9SGleb Smirnoff 
28413b3a8eb9SGleb Smirnoff filter_opts	:	{
28423b3a8eb9SGleb Smirnoff 				bzero(&filter_opts, sizeof filter_opts);
28433b3a8eb9SGleb Smirnoff 				filter_opts.rtableid = -1;
28443b3a8eb9SGleb Smirnoff 			}
28453b3a8eb9SGleb Smirnoff 		    filter_opts_l
28463b3a8eb9SGleb Smirnoff 			{ $$ = filter_opts; }
28473b3a8eb9SGleb Smirnoff 		| /* empty */	{
28483b3a8eb9SGleb Smirnoff 			bzero(&filter_opts, sizeof filter_opts);
28493b3a8eb9SGleb Smirnoff 			filter_opts.rtableid = -1;
28503b3a8eb9SGleb Smirnoff 			$$ = filter_opts;
28513b3a8eb9SGleb Smirnoff 		}
28523b3a8eb9SGleb Smirnoff 		;
28533b3a8eb9SGleb Smirnoff 
28543b3a8eb9SGleb Smirnoff filter_opts_l	: filter_opts_l filter_opt
28553b3a8eb9SGleb Smirnoff 		| filter_opt
28563b3a8eb9SGleb Smirnoff 		;
28573b3a8eb9SGleb Smirnoff 
28583b3a8eb9SGleb Smirnoff filter_opt	: USER uids {
28593b3a8eb9SGleb Smirnoff 			if (filter_opts.uid)
28603b3a8eb9SGleb Smirnoff 				$2->tail->next = filter_opts.uid;
28613b3a8eb9SGleb Smirnoff 			filter_opts.uid = $2;
28623b3a8eb9SGleb Smirnoff 		}
28633b3a8eb9SGleb Smirnoff 		| GROUP gids {
28643b3a8eb9SGleb Smirnoff 			if (filter_opts.gid)
28653b3a8eb9SGleb Smirnoff 				$2->tail->next = filter_opts.gid;
28663b3a8eb9SGleb Smirnoff 			filter_opts.gid = $2;
28673b3a8eb9SGleb Smirnoff 		}
28683b3a8eb9SGleb Smirnoff 		| flags {
28693b3a8eb9SGleb Smirnoff 			if (filter_opts.marker & FOM_FLAGS) {
28703b3a8eb9SGleb Smirnoff 				yyerror("flags cannot be redefined");
28713b3a8eb9SGleb Smirnoff 				YYERROR;
28723b3a8eb9SGleb Smirnoff 			}
28733b3a8eb9SGleb Smirnoff 			filter_opts.marker |= FOM_FLAGS;
28743b3a8eb9SGleb Smirnoff 			filter_opts.flags.b1 |= $1.b1;
28753b3a8eb9SGleb Smirnoff 			filter_opts.flags.b2 |= $1.b2;
28763b3a8eb9SGleb Smirnoff 			filter_opts.flags.w |= $1.w;
28773b3a8eb9SGleb Smirnoff 			filter_opts.flags.w2 |= $1.w2;
28783b3a8eb9SGleb Smirnoff 		}
28793b3a8eb9SGleb Smirnoff 		| icmpspec {
28803b3a8eb9SGleb Smirnoff 			if (filter_opts.marker & FOM_ICMP) {
28813b3a8eb9SGleb Smirnoff 				yyerror("icmp-type cannot be redefined");
28823b3a8eb9SGleb Smirnoff 				YYERROR;
28833b3a8eb9SGleb Smirnoff 			}
28843b3a8eb9SGleb Smirnoff 			filter_opts.marker |= FOM_ICMP;
28853b3a8eb9SGleb Smirnoff 			filter_opts.icmpspec = $1;
28863b3a8eb9SGleb Smirnoff 		}
28873e248e0fSKristof Provost 		| PRIO NUMBER {
28883e248e0fSKristof Provost 			if (filter_opts.marker & FOM_PRIO) {
28893e248e0fSKristof Provost 				yyerror("prio cannot be redefined");
28903e248e0fSKristof Provost 				YYERROR;
28913e248e0fSKristof Provost 			}
28923e248e0fSKristof Provost 			if ($2 < 0 || $2 > PF_PRIO_MAX) {
28933e248e0fSKristof Provost 				yyerror("prio must be 0 - %u", PF_PRIO_MAX);
28943e248e0fSKristof Provost 				YYERROR;
28953e248e0fSKristof Provost 			}
28963e248e0fSKristof Provost 			filter_opts.marker |= FOM_PRIO;
28973e248e0fSKristof Provost 			filter_opts.prio = $2;
28983e248e0fSKristof Provost 		}
28993b3a8eb9SGleb Smirnoff 		| TOS tos {
29003b3a8eb9SGleb Smirnoff 			if (filter_opts.marker & FOM_TOS) {
29013b3a8eb9SGleb Smirnoff 				yyerror("tos cannot be redefined");
29023b3a8eb9SGleb Smirnoff 				YYERROR;
29033b3a8eb9SGleb Smirnoff 			}
29043b3a8eb9SGleb Smirnoff 			filter_opts.marker |= FOM_TOS;
29053b3a8eb9SGleb Smirnoff 			filter_opts.tos = $2;
29063b3a8eb9SGleb Smirnoff 		}
29073b3a8eb9SGleb Smirnoff 		| keep {
29083b3a8eb9SGleb Smirnoff 			if (filter_opts.marker & FOM_KEEP) {
29093b3a8eb9SGleb Smirnoff 				yyerror("modulate or keep cannot be redefined");
29103b3a8eb9SGleb Smirnoff 				YYERROR;
29113b3a8eb9SGleb Smirnoff 			}
29123b3a8eb9SGleb Smirnoff 			filter_opts.marker |= FOM_KEEP;
29133b3a8eb9SGleb Smirnoff 			filter_opts.keep.action = $1.action;
29143b3a8eb9SGleb Smirnoff 			filter_opts.keep.options = $1.options;
29153b3a8eb9SGleb Smirnoff 		}
291676c5eeccSKristof Provost 		| RIDENTIFIER number {
291776c5eeccSKristof Provost 			filter_opts.ridentifier = $2;
291876c5eeccSKristof Provost 		}
29193b3a8eb9SGleb Smirnoff 		| FRAGMENT {
29203b3a8eb9SGleb Smirnoff 			filter_opts.fragment = 1;
29213b3a8eb9SGleb Smirnoff 		}
29223b3a8eb9SGleb Smirnoff 		| ALLOWOPTS {
29233b3a8eb9SGleb Smirnoff 			filter_opts.allowopts = 1;
29243b3a8eb9SGleb Smirnoff 		}
29253b3a8eb9SGleb Smirnoff 		| label	{
29266fcc8e04SKristof Provost 			if (filter_opts.labelcount >= PF_RULE_MAX_LABEL_COUNT) {
29276fcc8e04SKristof Provost 				yyerror("label can only be used %d times", PF_RULE_MAX_LABEL_COUNT);
29283b3a8eb9SGleb Smirnoff 				YYERROR;
29293b3a8eb9SGleb Smirnoff 			}
29306fcc8e04SKristof Provost 			filter_opts.label[filter_opts.labelcount++] = $1;
29313b3a8eb9SGleb Smirnoff 		}
29323b3a8eb9SGleb Smirnoff 		| qname	{
29333b3a8eb9SGleb Smirnoff 			if (filter_opts.queues.qname) {
29343b3a8eb9SGleb Smirnoff 				yyerror("queue cannot be redefined");
29353b3a8eb9SGleb Smirnoff 				YYERROR;
29363b3a8eb9SGleb Smirnoff 			}
29373b3a8eb9SGleb Smirnoff 			filter_opts.queues = $1;
29383b3a8eb9SGleb Smirnoff 		}
293963b3c1c7SKristof Provost 		| DNPIPE number {
294063b3c1c7SKristof Provost 			filter_opts.dnpipe = $2;
294163b3c1c7SKristof Provost 			filter_opts.free_flags |= PFRULE_DN_IS_PIPE;
294263b3c1c7SKristof Provost 		}
294363b3c1c7SKristof Provost 		| DNPIPE '(' number ')' {
294463b3c1c7SKristof Provost 			filter_opts.dnpipe = $3;
294563b3c1c7SKristof Provost 			filter_opts.free_flags |= PFRULE_DN_IS_PIPE;
294663b3c1c7SKristof Provost 		}
294763b3c1c7SKristof Provost 		| DNPIPE '(' number comma number ')' {
294863b3c1c7SKristof Provost 			filter_opts.dnrpipe = $5;
294963b3c1c7SKristof Provost 			filter_opts.dnpipe = $3;
295063b3c1c7SKristof Provost 			filter_opts.free_flags |= PFRULE_DN_IS_PIPE;
295163b3c1c7SKristof Provost 		}
295263b3c1c7SKristof Provost 		| DNQUEUE number {
295363b3c1c7SKristof Provost 			filter_opts.dnpipe = $2;
295463b3c1c7SKristof Provost 			filter_opts.free_flags |= PFRULE_DN_IS_QUEUE;
295563b3c1c7SKristof Provost 		}
295663b3c1c7SKristof Provost 		| DNQUEUE '(' number comma number ')' {
295763b3c1c7SKristof Provost 			filter_opts.dnrpipe = $5;
295863b3c1c7SKristof Provost 			filter_opts.dnpipe = $3;
295963b3c1c7SKristof Provost 			filter_opts.free_flags |= PFRULE_DN_IS_QUEUE;
296063b3c1c7SKristof Provost 		}
296163b3c1c7SKristof Provost 		| DNQUEUE '(' number ')' {
296263b3c1c7SKristof Provost 			filter_opts.dnpipe = $3;
296363b3c1c7SKristof Provost 			filter_opts.free_flags |= PFRULE_DN_IS_QUEUE;
296463b3c1c7SKristof Provost 		}
29653b3a8eb9SGleb Smirnoff 		| TAG string				{
29663b3a8eb9SGleb Smirnoff 			filter_opts.tag = $2;
29673b3a8eb9SGleb Smirnoff 		}
29683b3a8eb9SGleb Smirnoff 		| not TAGGED string			{
29693b3a8eb9SGleb Smirnoff 			filter_opts.match_tag = $3;
29703b3a8eb9SGleb Smirnoff 			filter_opts.match_tag_not = $1;
29713b3a8eb9SGleb Smirnoff 		}
29722339ead6SKristof Provost 		| RECEIVEDON if_item {
29732339ead6SKristof Provost 			if (filter_opts.rcv) {
29742339ead6SKristof Provost 				yyerror("cannot respecify received-on");
29752339ead6SKristof Provost 				YYERROR;
29762339ead6SKristof Provost 			}
29772339ead6SKristof Provost 			filter_opts.rcv = $2;
29782339ead6SKristof Provost 		}
29793b3a8eb9SGleb Smirnoff 		| PROBABILITY probability		{
29803b3a8eb9SGleb Smirnoff 			double	p;
29813b3a8eb9SGleb Smirnoff 
29823b3a8eb9SGleb Smirnoff 			p = floor($2 * UINT_MAX + 0.5);
29833b3a8eb9SGleb Smirnoff 			if (p < 0.0 || p > UINT_MAX) {
29843b3a8eb9SGleb Smirnoff 				yyerror("invalid probability: %lf", p);
29853b3a8eb9SGleb Smirnoff 				YYERROR;
29863b3a8eb9SGleb Smirnoff 			}
29873b3a8eb9SGleb Smirnoff 			filter_opts.prob = (u_int32_t)p;
29883b3a8eb9SGleb Smirnoff 			if (filter_opts.prob == 0)
29893b3a8eb9SGleb Smirnoff 				filter_opts.prob = 1;
29903b3a8eb9SGleb Smirnoff 		}
29913b3a8eb9SGleb Smirnoff 		| RTABLE NUMBER				{
29923b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > rt_tableid_max()) {
29933b3a8eb9SGleb Smirnoff 				yyerror("invalid rtable id");
29943b3a8eb9SGleb Smirnoff 				YYERROR;
29953b3a8eb9SGleb Smirnoff 			}
29963b3a8eb9SGleb Smirnoff 			filter_opts.rtableid = $2;
29973b3a8eb9SGleb Smirnoff 		}
29983b3a8eb9SGleb Smirnoff 		| DIVERTTO portplain {
29993b3a8eb9SGleb Smirnoff #ifdef __FreeBSD__
30003b3a8eb9SGleb Smirnoff 			filter_opts.divert.port = $2.a;
30013b3a8eb9SGleb Smirnoff 			if (!filter_opts.divert.port) {
30023b3a8eb9SGleb Smirnoff 				yyerror("invalid divert port: %u", ntohs($2.a));
30033b3a8eb9SGleb Smirnoff 				YYERROR;
30043b3a8eb9SGleb Smirnoff 			}
30053b3a8eb9SGleb Smirnoff #endif
30063b3a8eb9SGleb Smirnoff 		}
30073b3a8eb9SGleb Smirnoff 		| DIVERTTO STRING PORT portplain {
30083b3a8eb9SGleb Smirnoff #ifndef __FreeBSD__
30093b3a8eb9SGleb Smirnoff 			if ((filter_opts.divert.addr = host($2)) == NULL) {
30103b3a8eb9SGleb Smirnoff 				yyerror("could not parse divert address: %s",
30113b3a8eb9SGleb Smirnoff 				    $2);
30123b3a8eb9SGleb Smirnoff 				free($2);
30133b3a8eb9SGleb Smirnoff 				YYERROR;
30143b3a8eb9SGleb Smirnoff 			}
30153b3a8eb9SGleb Smirnoff #else
30163b3a8eb9SGleb Smirnoff 			if ($2)
30173b3a8eb9SGleb Smirnoff #endif
30183b3a8eb9SGleb Smirnoff 			free($2);
30193b3a8eb9SGleb Smirnoff 			filter_opts.divert.port = $4.a;
30203b3a8eb9SGleb Smirnoff 			if (!filter_opts.divert.port) {
30213b3a8eb9SGleb Smirnoff 				yyerror("invalid divert port: %u", ntohs($4.a));
30223b3a8eb9SGleb Smirnoff 				YYERROR;
30233b3a8eb9SGleb Smirnoff 			}
30243b3a8eb9SGleb Smirnoff 		}
30253b3a8eb9SGleb Smirnoff 		| DIVERTREPLY {
30263b3a8eb9SGleb Smirnoff #ifdef __FreeBSD__
30273b3a8eb9SGleb Smirnoff 			yyerror("divert-reply has no meaning in FreeBSD pf(4)");
30283b3a8eb9SGleb Smirnoff 			YYERROR;
30293b3a8eb9SGleb Smirnoff #else
30303b3a8eb9SGleb Smirnoff 			filter_opts.divert.port = 1;	/* some random value */
30313b3a8eb9SGleb Smirnoff #endif
30323b3a8eb9SGleb Smirnoff 		}
303339282ef3SKajetan Staszkiewicz 		| SCRUB '(' scrub_opts ')' {
303439282ef3SKajetan Staszkiewicz 			filter_opts.nodf = $3.nodf;
303539282ef3SKajetan Staszkiewicz 			filter_opts.minttl = $3.minttl;
303639282ef3SKajetan Staszkiewicz 			if ($3.marker & FOM_SETTOS) {
303739282ef3SKajetan Staszkiewicz 				/* Old style rules are "scrub set-tos 0x42"
303839282ef3SKajetan Staszkiewicz 				 * New style are "set tos 0x42 scrub (...)"
303939282ef3SKajetan Staszkiewicz 				 * What is in "scrub(...)"" is unfortunately the
304039282ef3SKajetan Staszkiewicz 				 * original scrub syntax so it would overwrite
304139282ef3SKajetan Staszkiewicz 				 * "set tos" of a pass/match rule.
304239282ef3SKajetan Staszkiewicz 				 */
304339282ef3SKajetan Staszkiewicz 				filter_opts.settos = $3.settos;
304439282ef3SKajetan Staszkiewicz 			}
304539282ef3SKajetan Staszkiewicz 			filter_opts.randomid = $3.randomid;
304639282ef3SKajetan Staszkiewicz 			filter_opts.max_mss = $3.maxmss;
304739282ef3SKajetan Staszkiewicz 			if ($3.reassemble_tcp)
304839282ef3SKajetan Staszkiewicz 				filter_opts.marker |= FOM_SCRUB_TCP;
304939282ef3SKajetan Staszkiewicz 			filter_opts.marker |= $3.marker;
305039282ef3SKajetan Staszkiewicz 		}
3051*aa69fdf1SKristof Provost 		| AFTO af FROM redirspec pool_opts {
3052*aa69fdf1SKristof Provost 			if (filter_opts.nat.rdr) {
3053*aa69fdf1SKristof Provost 				yyerror("cannot respecify af-to");
3054*aa69fdf1SKristof Provost 				YYERROR;
3055*aa69fdf1SKristof Provost 			}
3056*aa69fdf1SKristof Provost 			if ($2 == 0) {
3057*aa69fdf1SKristof Provost 				yyerror("no address family specified");
3058*aa69fdf1SKristof Provost 				YYERROR;
3059*aa69fdf1SKristof Provost 			}
3060*aa69fdf1SKristof Provost 			if ($4->af && $4->af != $2) {
3061*aa69fdf1SKristof Provost 				yyerror("af-to addresses must be in the "
3062*aa69fdf1SKristof Provost 				   "target address family");
3063*aa69fdf1SKristof Provost 				YYERROR;
3064*aa69fdf1SKristof Provost 			}
3065*aa69fdf1SKristof Provost 			filter_opts.nat.af = $2;
3066*aa69fdf1SKristof Provost 			filter_opts.nat.rdr = calloc(1, sizeof(struct redirection));
3067*aa69fdf1SKristof Provost 			if (filter_opts.nat.rdr == NULL)
3068*aa69fdf1SKristof Provost 				err(1, "af-to: calloc");
3069*aa69fdf1SKristof Provost 			filter_opts.nat.rdr->host = $4;
3070*aa69fdf1SKristof Provost 			memcpy(&filter_opts.nat.pool_opts, &$5,
3071*aa69fdf1SKristof Provost 			    sizeof(filter_opts.nat.pool_opts));
3072*aa69fdf1SKristof Provost 			filter_opts.rdr.rdr =
3073*aa69fdf1SKristof Provost 			    calloc(1, sizeof(struct redirection));
3074*aa69fdf1SKristof Provost 			bzero(&filter_opts.rdr.pool_opts,
3075*aa69fdf1SKristof Provost 			    sizeof(filter_opts.rdr.pool_opts));
3076*aa69fdf1SKristof Provost 			filter_opts.marker |= FOM_AFTO;
3077*aa69fdf1SKristof Provost 		}
3078*aa69fdf1SKristof Provost 		| AFTO af FROM redirspec pool_opts TO redirspec pool_opts {
3079*aa69fdf1SKristof Provost 			if (filter_opts.nat.rdr) {
3080*aa69fdf1SKristof Provost 				yyerror("cannot respecify af-to");
3081*aa69fdf1SKristof Provost 				YYERROR;
3082*aa69fdf1SKristof Provost 			}
3083*aa69fdf1SKristof Provost 			if ($2 == 0) {
3084*aa69fdf1SKristof Provost 				yyerror("no address family specified");
3085*aa69fdf1SKristof Provost 				YYERROR;
3086*aa69fdf1SKristof Provost 			}
3087*aa69fdf1SKristof Provost 				if (($4->af && $4->af != $2) ||
3088*aa69fdf1SKristof Provost 				($7->af && $7->af != $2)) {
3089*aa69fdf1SKristof Provost 				yyerror("af-to addresses must be in the "
3090*aa69fdf1SKristof Provost 				   "target address family");
3091*aa69fdf1SKristof Provost 				YYERROR;
3092*aa69fdf1SKristof Provost 			}
3093*aa69fdf1SKristof Provost 			filter_opts.nat.af = $2;
3094*aa69fdf1SKristof Provost 			filter_opts.nat.rdr = calloc(1, sizeof(struct redirection));
3095*aa69fdf1SKristof Provost 			if (filter_opts.nat.rdr == NULL)
3096*aa69fdf1SKristof Provost 				err(1, "af-to: calloc");
3097*aa69fdf1SKristof Provost 			filter_opts.nat.rdr->host = $4;
3098*aa69fdf1SKristof Provost 			memcpy(&filter_opts.nat.pool_opts, &$5,
3099*aa69fdf1SKristof Provost 			    sizeof(filter_opts.nat.pool_opts));
3100*aa69fdf1SKristof Provost 			filter_opts.rdr.af = $2;
3101*aa69fdf1SKristof Provost 			filter_opts.rdr.rdr = calloc(1, sizeof(struct redirection));
3102*aa69fdf1SKristof Provost 			if (filter_opts.rdr.rdr == NULL)
3103*aa69fdf1SKristof Provost 				err(1, "af-to: calloc");
3104*aa69fdf1SKristof Provost 			filter_opts.rdr.rdr->host = $7;
3105*aa69fdf1SKristof Provost 			memcpy(&filter_opts.nat.pool_opts, &$8,
3106*aa69fdf1SKristof Provost 			    sizeof(filter_opts.nat.pool_opts));
3107*aa69fdf1SKristof Provost 			filter_opts.marker |= FOM_AFTO;
3108*aa69fdf1SKristof Provost 		}
31093e248e0fSKristof Provost 		| filter_sets
31103e248e0fSKristof Provost 		;
31113e248e0fSKristof Provost 
31123e248e0fSKristof Provost filter_sets	: SET '(' filter_sets_l ')'	{ $$ = filter_opts; }
31133e248e0fSKristof Provost 		| SET filter_set		{ $$ = filter_opts; }
31143e248e0fSKristof Provost 		;
31153e248e0fSKristof Provost 
31163e248e0fSKristof Provost filter_sets_l	: filter_sets_l comma filter_set
31173e248e0fSKristof Provost 		| filter_set
31183e248e0fSKristof Provost 		;
31193e248e0fSKristof Provost 
31203e248e0fSKristof Provost filter_set	: prio {
31213e248e0fSKristof Provost 			if (filter_opts.marker & FOM_SETPRIO) {
31223e248e0fSKristof Provost 				yyerror("prio cannot be redefined");
31233e248e0fSKristof Provost 				YYERROR;
31243e248e0fSKristof Provost 			}
31253e248e0fSKristof Provost 			filter_opts.marker |= FOM_SETPRIO;
31263e248e0fSKristof Provost 			filter_opts.set_prio[0] = $1.b1;
31273e248e0fSKristof Provost 			filter_opts.set_prio[1] = $1.b2;
31283e248e0fSKristof Provost 		}
312939282ef3SKajetan Staszkiewicz 		| TOS tos {
313039282ef3SKajetan Staszkiewicz 			if (filter_opts.marker & FOM_SETTOS) {
313139282ef3SKajetan Staszkiewicz 				yyerror("tos cannot be respecified");
313239282ef3SKajetan Staszkiewicz 				YYERROR;
313339282ef3SKajetan Staszkiewicz 			}
313439282ef3SKajetan Staszkiewicz 			filter_opts.marker |= FOM_SETTOS;
313539282ef3SKajetan Staszkiewicz 			filter_opts.settos = $2;
313639282ef3SKajetan Staszkiewicz 		}
31373e248e0fSKristof Provost prio		: PRIO NUMBER {
31383e248e0fSKristof Provost 			if ($2 < 0 || $2 > PF_PRIO_MAX) {
31393e248e0fSKristof Provost 				yyerror("prio must be 0 - %u", PF_PRIO_MAX);
31403e248e0fSKristof Provost 				YYERROR;
31413e248e0fSKristof Provost 			}
31423e248e0fSKristof Provost 			$$.b1 = $$.b2 = $2;
31433e248e0fSKristof Provost 		}
31443e248e0fSKristof Provost 		| PRIO '(' NUMBER comma NUMBER ')' {
31453e248e0fSKristof Provost 			if ($3 < 0 || $3 > PF_PRIO_MAX ||
31463e248e0fSKristof Provost 			    $5 < 0 || $5 > PF_PRIO_MAX) {
31473e248e0fSKristof Provost 				yyerror("prio must be 0 - %u", PF_PRIO_MAX);
31483e248e0fSKristof Provost 				YYERROR;
31493e248e0fSKristof Provost 			}
31503e248e0fSKristof Provost 			$$.b1 = $3;
31513e248e0fSKristof Provost 			$$.b2 = $5;
31523e248e0fSKristof Provost 		}
31533b3a8eb9SGleb Smirnoff 		;
31543b3a8eb9SGleb Smirnoff 
31553b3a8eb9SGleb Smirnoff probability	: STRING				{
31563b3a8eb9SGleb Smirnoff 			char	*e;
31573b3a8eb9SGleb Smirnoff 			double	 p = strtod($1, &e);
31583b3a8eb9SGleb Smirnoff 
31593b3a8eb9SGleb Smirnoff 			if (*e == '%') {
31603b3a8eb9SGleb Smirnoff 				p *= 0.01;
31613b3a8eb9SGleb Smirnoff 				e++;
31623b3a8eb9SGleb Smirnoff 			}
31633b3a8eb9SGleb Smirnoff 			if (*e) {
31643b3a8eb9SGleb Smirnoff 				yyerror("invalid probability: %s", $1);
31653b3a8eb9SGleb Smirnoff 				free($1);
31663b3a8eb9SGleb Smirnoff 				YYERROR;
31673b3a8eb9SGleb Smirnoff 			}
31683b3a8eb9SGleb Smirnoff 			free($1);
31693b3a8eb9SGleb Smirnoff 			$$ = p;
31703b3a8eb9SGleb Smirnoff 		}
31713b3a8eb9SGleb Smirnoff 		| NUMBER				{
31723b3a8eb9SGleb Smirnoff 			$$ = (double)$1;
31733b3a8eb9SGleb Smirnoff 		}
31743b3a8eb9SGleb Smirnoff 		;
31753b3a8eb9SGleb Smirnoff 
31763b3a8eb9SGleb Smirnoff 
3177150182e3SKristof Provost action		: PASS 			{
3178150182e3SKristof Provost 			$$.b1 = PF_PASS;
3179150182e3SKristof Provost 			$$.b2 = failpolicy;
3180150182e3SKristof Provost 			$$.w = returnicmpdefault;
3181150182e3SKristof Provost 			$$.w2 = returnicmp6default;
3182150182e3SKristof Provost 		}
3183ef950daaSKristof Provost 		| MATCH			{ $$.b1 = PF_MATCH; $$.b2 = $$.w = 0; }
31843b3a8eb9SGleb Smirnoff 		| BLOCK blockspec	{ $$ = $2; $$.b1 = PF_DROP; }
31853b3a8eb9SGleb Smirnoff 		;
31863b3a8eb9SGleb Smirnoff 
31873b3a8eb9SGleb Smirnoff blockspec	: /* empty */		{
31883b3a8eb9SGleb Smirnoff 			$$.b2 = blockpolicy;
31893b3a8eb9SGleb Smirnoff 			$$.w = returnicmpdefault;
31903b3a8eb9SGleb Smirnoff 			$$.w2 = returnicmp6default;
31913b3a8eb9SGleb Smirnoff 		}
31923b3a8eb9SGleb Smirnoff 		| DROP			{
31933b3a8eb9SGleb Smirnoff 			$$.b2 = PFRULE_DROP;
31943b3a8eb9SGleb Smirnoff 			$$.w = 0;
31953b3a8eb9SGleb Smirnoff 			$$.w2 = 0;
31963b3a8eb9SGleb Smirnoff 		}
31973b3a8eb9SGleb Smirnoff 		| RETURNRST		{
31983b3a8eb9SGleb Smirnoff 			$$.b2 = PFRULE_RETURNRST;
31993b3a8eb9SGleb Smirnoff 			$$.w = 0;
32003b3a8eb9SGleb Smirnoff 			$$.w2 = 0;
32013b3a8eb9SGleb Smirnoff 		}
32023b3a8eb9SGleb Smirnoff 		| RETURNRST '(' TTL NUMBER ')'	{
32033b3a8eb9SGleb Smirnoff 			if ($4 < 0 || $4 > 255) {
32043b3a8eb9SGleb Smirnoff 				yyerror("illegal ttl value %d", $4);
32053b3a8eb9SGleb Smirnoff 				YYERROR;
32063b3a8eb9SGleb Smirnoff 			}
32073b3a8eb9SGleb Smirnoff 			$$.b2 = PFRULE_RETURNRST;
32083b3a8eb9SGleb Smirnoff 			$$.w = $4;
32093b3a8eb9SGleb Smirnoff 			$$.w2 = 0;
32103b3a8eb9SGleb Smirnoff 		}
32113b3a8eb9SGleb Smirnoff 		| RETURNICMP		{
32123b3a8eb9SGleb Smirnoff 			$$.b2 = PFRULE_RETURNICMP;
32133b3a8eb9SGleb Smirnoff 			$$.w = returnicmpdefault;
32143b3a8eb9SGleb Smirnoff 			$$.w2 = returnicmp6default;
32153b3a8eb9SGleb Smirnoff 		}
32163b3a8eb9SGleb Smirnoff 		| RETURNICMP6		{
32173b3a8eb9SGleb Smirnoff 			$$.b2 = PFRULE_RETURNICMP;
32183b3a8eb9SGleb Smirnoff 			$$.w = returnicmpdefault;
32193b3a8eb9SGleb Smirnoff 			$$.w2 = returnicmp6default;
32203b3a8eb9SGleb Smirnoff 		}
32213b3a8eb9SGleb Smirnoff 		| RETURNICMP '(' reticmpspec ')'	{
32223b3a8eb9SGleb Smirnoff 			$$.b2 = PFRULE_RETURNICMP;
32233b3a8eb9SGleb Smirnoff 			$$.w = $3;
32243b3a8eb9SGleb Smirnoff 			$$.w2 = returnicmpdefault;
32253b3a8eb9SGleb Smirnoff 		}
32263b3a8eb9SGleb Smirnoff 		| RETURNICMP6 '(' reticmp6spec ')'	{
32273b3a8eb9SGleb Smirnoff 			$$.b2 = PFRULE_RETURNICMP;
32283b3a8eb9SGleb Smirnoff 			$$.w = returnicmpdefault;
32293b3a8eb9SGleb Smirnoff 			$$.w2 = $3;
32303b3a8eb9SGleb Smirnoff 		}
32313b3a8eb9SGleb Smirnoff 		| RETURNICMP '(' reticmpspec comma reticmp6spec ')' {
32323b3a8eb9SGleb Smirnoff 			$$.b2 = PFRULE_RETURNICMP;
32333b3a8eb9SGleb Smirnoff 			$$.w = $3;
32343b3a8eb9SGleb Smirnoff 			$$.w2 = $5;
32353b3a8eb9SGleb Smirnoff 		}
32363b3a8eb9SGleb Smirnoff 		| RETURN {
32373b3a8eb9SGleb Smirnoff 			$$.b2 = PFRULE_RETURN;
32383b3a8eb9SGleb Smirnoff 			$$.w = returnicmpdefault;
32393b3a8eb9SGleb Smirnoff 			$$.w2 = returnicmp6default;
32403b3a8eb9SGleb Smirnoff 		}
32413b3a8eb9SGleb Smirnoff 		;
32423b3a8eb9SGleb Smirnoff 
32433b3a8eb9SGleb Smirnoff reticmpspec	: STRING			{
32443b3a8eb9SGleb Smirnoff 			if (!($$ = parseicmpspec($1, AF_INET))) {
32453b3a8eb9SGleb Smirnoff 				free($1);
32463b3a8eb9SGleb Smirnoff 				YYERROR;
32473b3a8eb9SGleb Smirnoff 			}
32483b3a8eb9SGleb Smirnoff 			free($1);
32493b3a8eb9SGleb Smirnoff 		}
32503b3a8eb9SGleb Smirnoff 		| NUMBER			{
32513b3a8eb9SGleb Smirnoff 			u_int8_t		icmptype;
32523b3a8eb9SGleb Smirnoff 
32533b3a8eb9SGleb Smirnoff 			if ($1 < 0 || $1 > 255) {
32543b3a8eb9SGleb Smirnoff 				yyerror("invalid icmp code %lu", $1);
32553b3a8eb9SGleb Smirnoff 				YYERROR;
32563b3a8eb9SGleb Smirnoff 			}
32573b3a8eb9SGleb Smirnoff 			icmptype = returnicmpdefault >> 8;
32583b3a8eb9SGleb Smirnoff 			$$ = (icmptype << 8 | $1);
32593b3a8eb9SGleb Smirnoff 		}
32603b3a8eb9SGleb Smirnoff 		;
32613b3a8eb9SGleb Smirnoff 
32623b3a8eb9SGleb Smirnoff reticmp6spec	: STRING			{
32633b3a8eb9SGleb Smirnoff 			if (!($$ = parseicmpspec($1, AF_INET6))) {
32643b3a8eb9SGleb Smirnoff 				free($1);
32653b3a8eb9SGleb Smirnoff 				YYERROR;
32663b3a8eb9SGleb Smirnoff 			}
32673b3a8eb9SGleb Smirnoff 			free($1);
32683b3a8eb9SGleb Smirnoff 		}
32693b3a8eb9SGleb Smirnoff 		| NUMBER			{
32703b3a8eb9SGleb Smirnoff 			u_int8_t		icmptype;
32713b3a8eb9SGleb Smirnoff 
32723b3a8eb9SGleb Smirnoff 			if ($1 < 0 || $1 > 255) {
32733b3a8eb9SGleb Smirnoff 				yyerror("invalid icmp code %lu", $1);
32743b3a8eb9SGleb Smirnoff 				YYERROR;
32753b3a8eb9SGleb Smirnoff 			}
32763b3a8eb9SGleb Smirnoff 			icmptype = returnicmp6default >> 8;
32773b3a8eb9SGleb Smirnoff 			$$ = (icmptype << 8 | $1);
32783b3a8eb9SGleb Smirnoff 		}
32793b3a8eb9SGleb Smirnoff 		;
32803b3a8eb9SGleb Smirnoff 
32813b3a8eb9SGleb Smirnoff dir		: /* empty */			{ $$ = PF_INOUT; }
32823b3a8eb9SGleb Smirnoff 		| IN				{ $$ = PF_IN; }
32833b3a8eb9SGleb Smirnoff 		| OUT				{ $$ = PF_OUT; }
32843b3a8eb9SGleb Smirnoff 		;
32853b3a8eb9SGleb Smirnoff 
32863b3a8eb9SGleb Smirnoff quick		: /* empty */			{ $$.quick = 0; }
32873b3a8eb9SGleb Smirnoff 		| QUICK				{ $$.quick = 1; }
32883b3a8eb9SGleb Smirnoff 		;
32893b3a8eb9SGleb Smirnoff 
32903b3a8eb9SGleb Smirnoff logquick	: /* empty */	{ $$.log = 0; $$.quick = 0; $$.logif = 0; }
32913b3a8eb9SGleb Smirnoff 		| log		{ $$ = $1; $$.quick = 0; }
32923b3a8eb9SGleb Smirnoff 		| QUICK		{ $$.quick = 1; $$.log = 0; $$.logif = 0; }
32933b3a8eb9SGleb Smirnoff 		| log QUICK	{ $$ = $1; $$.quick = 1; }
32943b3a8eb9SGleb Smirnoff 		| QUICK log	{ $$ = $2; $$.quick = 1; }
32953b3a8eb9SGleb Smirnoff 		;
32963b3a8eb9SGleb Smirnoff 
32973b3a8eb9SGleb Smirnoff log		: LOG			{ $$.log = PF_LOG; $$.logif = 0; }
32983b3a8eb9SGleb Smirnoff 		| LOG '(' logopts ')'	{
32993b3a8eb9SGleb Smirnoff 			$$.log = PF_LOG | $3.log;
33003b3a8eb9SGleb Smirnoff 			$$.logif = $3.logif;
33013b3a8eb9SGleb Smirnoff 		}
33023b3a8eb9SGleb Smirnoff 		;
33033b3a8eb9SGleb Smirnoff 
33043b3a8eb9SGleb Smirnoff logopts		: logopt			{ $$ = $1; }
33053b3a8eb9SGleb Smirnoff 		| logopts comma logopt		{
33063b3a8eb9SGleb Smirnoff 			$$.log = $1.log | $3.log;
33073b3a8eb9SGleb Smirnoff 			$$.logif = $3.logif;
33083b3a8eb9SGleb Smirnoff 			if ($$.logif == 0)
33093b3a8eb9SGleb Smirnoff 				$$.logif = $1.logif;
33103b3a8eb9SGleb Smirnoff 		}
33113b3a8eb9SGleb Smirnoff 		;
33123b3a8eb9SGleb Smirnoff 
33133b3a8eb9SGleb Smirnoff logopt		: ALL		{ $$.log = PF_LOG_ALL; $$.logif = 0; }
3314f3ab00c2SKristof Provost 		| MATCHES		{ $$.log = PF_LOG_MATCHES; $$.logif = 0; }
33153b3a8eb9SGleb Smirnoff 		| USER		{ $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
33163b3a8eb9SGleb Smirnoff 		| GROUP		{ $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
33173b3a8eb9SGleb Smirnoff 		| TO string	{
33183b3a8eb9SGleb Smirnoff 			const char	*errstr;
33193b3a8eb9SGleb Smirnoff 			u_int		 i;
33203b3a8eb9SGleb Smirnoff 
33213b3a8eb9SGleb Smirnoff 			$$.log = 0;
33223b3a8eb9SGleb Smirnoff 			if (strncmp($2, "pflog", 5)) {
33233b3a8eb9SGleb Smirnoff 				yyerror("%s: should be a pflog interface", $2);
33243b3a8eb9SGleb Smirnoff 				free($2);
33253b3a8eb9SGleb Smirnoff 				YYERROR;
33263b3a8eb9SGleb Smirnoff 			}
33273b3a8eb9SGleb Smirnoff 			i = strtonum($2 + 5, 0, 255, &errstr);
33283b3a8eb9SGleb Smirnoff 			if (errstr) {
33293b3a8eb9SGleb Smirnoff 				yyerror("%s: %s", $2, errstr);
33303b3a8eb9SGleb Smirnoff 				free($2);
33313b3a8eb9SGleb Smirnoff 				YYERROR;
33323b3a8eb9SGleb Smirnoff 			}
33333b3a8eb9SGleb Smirnoff 			free($2);
33343b3a8eb9SGleb Smirnoff 			$$.logif = i;
33353b3a8eb9SGleb Smirnoff 		}
33363b3a8eb9SGleb Smirnoff 		;
33373b3a8eb9SGleb Smirnoff 
33383b3a8eb9SGleb Smirnoff interface	: /* empty */			{ $$ = NULL; }
33393b3a8eb9SGleb Smirnoff 		| ON if_item_not		{ $$ = $2; }
33403b3a8eb9SGleb Smirnoff 		| ON '{' optnl if_list '}'	{ $$ = $4; }
33413b3a8eb9SGleb Smirnoff 		;
33423b3a8eb9SGleb Smirnoff 
33433b3a8eb9SGleb Smirnoff if_list		: if_item_not optnl		{ $$ = $1; }
33443b3a8eb9SGleb Smirnoff 		| if_list comma if_item_not optnl	{
33453b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
33463b3a8eb9SGleb Smirnoff 			$1->tail = $3;
33473b3a8eb9SGleb Smirnoff 			$$ = $1;
33483b3a8eb9SGleb Smirnoff 		}
33493b3a8eb9SGleb Smirnoff 		;
33503b3a8eb9SGleb Smirnoff 
33513b3a8eb9SGleb Smirnoff if_item_not	: not if_item			{ $$ = $2; $$->not = $1; }
33523b3a8eb9SGleb Smirnoff 		;
33533b3a8eb9SGleb Smirnoff 
33543b3a8eb9SGleb Smirnoff if_item		: STRING			{
33553b3a8eb9SGleb Smirnoff 			struct node_host	*n;
33563b3a8eb9SGleb Smirnoff 
33573b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_if));
33583b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
33593b3a8eb9SGleb Smirnoff 				err(1, "if_item: calloc");
33603b3a8eb9SGleb Smirnoff 			if (strlcpy($$->ifname, $1, sizeof($$->ifname)) >=
33613b3a8eb9SGleb Smirnoff 			    sizeof($$->ifname)) {
33623b3a8eb9SGleb Smirnoff 				free($1);
33633b3a8eb9SGleb Smirnoff 				free($$);
33643b3a8eb9SGleb Smirnoff 				yyerror("interface name too long");
33653b3a8eb9SGleb Smirnoff 				YYERROR;
33663b3a8eb9SGleb Smirnoff 			}
33673b3a8eb9SGleb Smirnoff 
33683b3a8eb9SGleb Smirnoff 			if ((n = ifa_exists($1)) != NULL)
33693b3a8eb9SGleb Smirnoff 				$$->ifa_flags = n->ifa_flags;
33703b3a8eb9SGleb Smirnoff 
33713b3a8eb9SGleb Smirnoff 			free($1);
33723b3a8eb9SGleb Smirnoff 			$$->not = 0;
33733b3a8eb9SGleb Smirnoff 			$$->next = NULL;
33743b3a8eb9SGleb Smirnoff 			$$->tail = $$;
33753b3a8eb9SGleb Smirnoff 		}
33763b3a8eb9SGleb Smirnoff 		;
33773b3a8eb9SGleb Smirnoff 
33783b3a8eb9SGleb Smirnoff af		: /* empty */			{ $$ = 0; }
33793b3a8eb9SGleb Smirnoff 		| INET				{ $$ = AF_INET; }
33803b3a8eb9SGleb Smirnoff 		| INET6				{ $$ = AF_INET6; }
33813b3a8eb9SGleb Smirnoff 		;
33823b3a8eb9SGleb Smirnoff 
33832b29ceb8SKristof Provost etherproto	: /* empty */				{ $$ = NULL; }
33842b29ceb8SKristof Provost 		| PROTO etherproto_item			{ $$ = $2; }
33852b29ceb8SKristof Provost 		| PROTO '{' optnl etherproto_list '}'	{ $$ = $4; }
33862b29ceb8SKristof Provost 		;
33872b29ceb8SKristof Provost 
33882b29ceb8SKristof Provost etherproto_list	: etherproto_item optnl			{ $$ = $1; }
33892b29ceb8SKristof Provost 		| etherproto_list comma etherproto_item optnl	{
33902b29ceb8SKristof Provost 			$1->tail->next = $3;
33912b29ceb8SKristof Provost 			$1->tail = $3;
33922b29ceb8SKristof Provost 			$$ = $1;
33932b29ceb8SKristof Provost 		}
33942b29ceb8SKristof Provost 		;
33952b29ceb8SKristof Provost 
33962b29ceb8SKristof Provost etherproto_item	: etherprotoval		{
33972b29ceb8SKristof Provost 			u_int16_t	pr;
33982b29ceb8SKristof Provost 
33992b29ceb8SKristof Provost 			pr = (u_int16_t)$1;
34002b29ceb8SKristof Provost 			if (pr == 0) {
34012b29ceb8SKristof Provost 				yyerror("proto 0 cannot be used");
34022b29ceb8SKristof Provost 				YYERROR;
34032b29ceb8SKristof Provost 			}
34042b29ceb8SKristof Provost 			$$ = calloc(1, sizeof(struct node_proto));
34052b29ceb8SKristof Provost 			if ($$ == NULL)
34062b29ceb8SKristof Provost 				err(1, "proto_item: calloc");
34072b29ceb8SKristof Provost 			$$->proto = pr;
34082b29ceb8SKristof Provost 			$$->next = NULL;
34092b29ceb8SKristof Provost 			$$->tail = $$;
34102b29ceb8SKristof Provost 		}
34112b29ceb8SKristof Provost 		;
34122b29ceb8SKristof Provost 
34132b29ceb8SKristof Provost etherprotoval	: NUMBER			{
34142b29ceb8SKristof Provost 			if ($1 < 0 || $1 > 65565) {
34152b29ceb8SKristof Provost 				yyerror("protocol outside range");
34162b29ceb8SKristof Provost 				YYERROR;
34172b29ceb8SKristof Provost 			}
34182b29ceb8SKristof Provost 		}
34192b29ceb8SKristof Provost 		| STRING
34202b29ceb8SKristof Provost 		{
34212b29ceb8SKristof Provost 			if (!strncmp($1, "0x", 2)) {
34222b29ceb8SKristof Provost 				if (sscanf($1, "0x%4x", &$$) != 1) {
34232b29ceb8SKristof Provost 					free($1);
34242b29ceb8SKristof Provost 					yyerror("invalid EtherType hex");
34252b29ceb8SKristof Provost 					YYERROR;
34262b29ceb8SKristof Provost 				}
34272b29ceb8SKristof Provost 			} else {
34282b29ceb8SKristof Provost 				yyerror("Symbolic EtherType not yet supported");
34292b29ceb8SKristof Provost 			}
34302b29ceb8SKristof Provost 		}
34312b29ceb8SKristof Provost 		;
34322b29ceb8SKristof Provost 
34333b3a8eb9SGleb Smirnoff proto		: /* empty */				{ $$ = NULL; }
34343b3a8eb9SGleb Smirnoff 		| PROTO proto_item			{ $$ = $2; }
34353b3a8eb9SGleb Smirnoff 		| PROTO '{' optnl proto_list '}'	{ $$ = $4; }
34363b3a8eb9SGleb Smirnoff 		;
34373b3a8eb9SGleb Smirnoff 
34383b3a8eb9SGleb Smirnoff proto_list	: proto_item optnl		{ $$ = $1; }
34393b3a8eb9SGleb Smirnoff 		| proto_list comma proto_item optnl	{
34403b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
34413b3a8eb9SGleb Smirnoff 			$1->tail = $3;
34423b3a8eb9SGleb Smirnoff 			$$ = $1;
34433b3a8eb9SGleb Smirnoff 		}
34443b3a8eb9SGleb Smirnoff 		;
34453b3a8eb9SGleb Smirnoff 
34463b3a8eb9SGleb Smirnoff proto_item	: protoval			{
34473b3a8eb9SGleb Smirnoff 			u_int8_t	pr;
34483b3a8eb9SGleb Smirnoff 
34493b3a8eb9SGleb Smirnoff 			pr = (u_int8_t)$1;
34503b3a8eb9SGleb Smirnoff 			if (pr == 0) {
34513b3a8eb9SGleb Smirnoff 				yyerror("proto 0 cannot be used");
34523b3a8eb9SGleb Smirnoff 				YYERROR;
34533b3a8eb9SGleb Smirnoff 			}
34543b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_proto));
34553b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
34563b3a8eb9SGleb Smirnoff 				err(1, "proto_item: calloc");
34573b3a8eb9SGleb Smirnoff 			$$->proto = pr;
34583b3a8eb9SGleb Smirnoff 			$$->next = NULL;
34593b3a8eb9SGleb Smirnoff 			$$->tail = $$;
34603b3a8eb9SGleb Smirnoff 		}
34613b3a8eb9SGleb Smirnoff 		;
34623b3a8eb9SGleb Smirnoff 
34633b3a8eb9SGleb Smirnoff protoval	: STRING			{
34643b3a8eb9SGleb Smirnoff 			struct protoent	*p;
34653b3a8eb9SGleb Smirnoff 
34663b3a8eb9SGleb Smirnoff 			p = getprotobyname($1);
34673b3a8eb9SGleb Smirnoff 			if (p == NULL) {
34683b3a8eb9SGleb Smirnoff 				yyerror("unknown protocol %s", $1);
34693b3a8eb9SGleb Smirnoff 				free($1);
34703b3a8eb9SGleb Smirnoff 				YYERROR;
34713b3a8eb9SGleb Smirnoff 			}
34723b3a8eb9SGleb Smirnoff 			$$ = p->p_proto;
34733b3a8eb9SGleb Smirnoff 			free($1);
34743b3a8eb9SGleb Smirnoff 		}
34753b3a8eb9SGleb Smirnoff 		| NUMBER			{
34763b3a8eb9SGleb Smirnoff 			if ($1 < 0 || $1 > 255) {
34773b3a8eb9SGleb Smirnoff 				yyerror("protocol outside range");
34783b3a8eb9SGleb Smirnoff 				YYERROR;
34793b3a8eb9SGleb Smirnoff 			}
34803b3a8eb9SGleb Smirnoff 		}
34813b3a8eb9SGleb Smirnoff 		;
34823b3a8eb9SGleb Smirnoff 
34838a42005dSKristof Provost l3fromto	: /* empty */			{
34848a42005dSKristof Provost 			bzero(&$$, sizeof($$));
34858a42005dSKristof Provost 		}
34868a42005dSKristof Provost 		| L3 fromto			{
34873468cd95SKristof Provost 			if ($2.src.host != NULL &&
3488812839e5SKristof Provost 			    $2.src.host->addr.type != PF_ADDR_ADDRMASK &&
3489812839e5SKristof Provost 			    $2.src.host->addr.type != PF_ADDR_TABLE) {
3490812839e5SKristof Provost 				yyerror("from must be an address or table");
34913468cd95SKristof Provost 				YYERROR;
34923468cd95SKristof Provost 			}
34933468cd95SKristof Provost 			if ($2.dst.host != NULL &&
3494812839e5SKristof Provost 			    $2.dst.host->addr.type != PF_ADDR_ADDRMASK &&
3495812839e5SKristof Provost 			    $2.dst.host->addr.type != PF_ADDR_TABLE) {
3496812839e5SKristof Provost 				yyerror("to must be an address or table");
34973468cd95SKristof Provost 				YYERROR;
34983468cd95SKristof Provost 			}
34998a42005dSKristof Provost 			$$ = $2;
35008a42005dSKristof Provost 		}
35018a42005dSKristof Provost 		;
35022b29ceb8SKristof Provost etherfromto	: ALL				{
350387a89d6eSKristof Provost 			$$.src = NULL;
350487a89d6eSKristof Provost 			$$.dst = NULL;
35052b29ceb8SKristof Provost 		}
35062b29ceb8SKristof Provost 		| etherfrom etherto		{
350787a89d6eSKristof Provost 			$$.src = $1.mac;
350887a89d6eSKristof Provost 			$$.dst = $2.mac;
35092b29ceb8SKristof Provost 		}
35102b29ceb8SKristof Provost 		;
35112b29ceb8SKristof Provost 
35122b29ceb8SKristof Provost etherfrom	: /* emtpy */			{
35132b29ceb8SKristof Provost 			bzero(&$$, sizeof($$));
35142b29ceb8SKristof Provost 		}
351587a89d6eSKristof Provost 		| FROM macspec			{
351687a89d6eSKristof Provost 			$$.mac = $2;
35172b29ceb8SKristof Provost 		}
35182b29ceb8SKristof Provost 		;
35192b29ceb8SKristof Provost 
35202b29ceb8SKristof Provost etherto		: /* empty */			{
35212b29ceb8SKristof Provost 			bzero(&$$, sizeof($$));
35222b29ceb8SKristof Provost 		}
352387a89d6eSKristof Provost 		| TO macspec			{
352487a89d6eSKristof Provost 			$$.mac = $2;
35252b29ceb8SKristof Provost 		}
35262b29ceb8SKristof Provost 		;
35272b29ceb8SKristof Provost 
3528b590f17aSKristof Provost mac		: string '/' NUMBER		{
3529b590f17aSKristof Provost 			$$ = node_mac_from_string_masklen($1, $3);
35302b29ceb8SKristof Provost 			free($1);
3531b590f17aSKristof Provost 			if ($$ == NULL)
35322b29ceb8SKristof Provost 				YYERROR;
35332b29ceb8SKristof Provost 		}
3534b590f17aSKristof Provost 		| string			{
3535b590f17aSKristof Provost 			if (strchr($1, '&')) {
3536b590f17aSKristof Provost 				/* mac&mask */
3537b590f17aSKristof Provost 				char *mac = strtok($1, "&");
3538b590f17aSKristof Provost 				char *mask = strtok(NULL, "&");
3539b590f17aSKristof Provost 				$$ = node_mac_from_string_mask(mac, mask);
3540b590f17aSKristof Provost 			} else {
3541b590f17aSKristof Provost 				$$ = node_mac_from_string($1);
3542b590f17aSKristof Provost 			}
354387a89d6eSKristof Provost 			free($1);
3544b590f17aSKristof Provost 			if ($$ == NULL)
3545b590f17aSKristof Provost 				YYERROR;
3546b590f17aSKristof Provost 
354787a89d6eSKristof Provost 		}
354887a89d6eSKristof Provost xmac		: not mac {
354987a89d6eSKristof Provost 			struct node_mac	*n;
355087a89d6eSKristof Provost 
355187a89d6eSKristof Provost 			for (n = $2; n != NULL; n = n->next)
355287a89d6eSKristof Provost 				n->neg = $1;
355387a89d6eSKristof Provost 			$$ = $2;
35542b29ceb8SKristof Provost 		}
35552b29ceb8SKristof Provost 		;
355687a89d6eSKristof Provost macspec		: xmac {
355787a89d6eSKristof Provost 			$$ = $1;
355887a89d6eSKristof Provost 		}
355987a89d6eSKristof Provost 		| '{' optnl mac_list '}'
356087a89d6eSKristof Provost 		{
356187a89d6eSKristof Provost 			$$ = $3;
356287a89d6eSKristof Provost 		}
356387a89d6eSKristof Provost 		;
356487a89d6eSKristof Provost mac_list	: xmac optnl {
356587a89d6eSKristof Provost 			$$ = $1;
356687a89d6eSKristof Provost 		}
356787a89d6eSKristof Provost 		| mac_list comma xmac {
356887a89d6eSKristof Provost 			if ($3 == NULL)
356987a89d6eSKristof Provost 				$$ = $1;
357087a89d6eSKristof Provost 			else if ($1 == NULL)
357187a89d6eSKristof Provost 				$$ = $3;
357287a89d6eSKristof Provost 			else {
357387a89d6eSKristof Provost 				$1->tail->next = $3;
357487a89d6eSKristof Provost 				$1->tail = $3->tail;
357587a89d6eSKristof Provost 				$$ = $1;
357687a89d6eSKristof Provost 			}
357787a89d6eSKristof Provost 		}
35782b29ceb8SKristof Provost 
35793b3a8eb9SGleb Smirnoff fromto		: ALL				{
35803b3a8eb9SGleb Smirnoff 			$$.src.host = NULL;
35813b3a8eb9SGleb Smirnoff 			$$.src.port = NULL;
35823b3a8eb9SGleb Smirnoff 			$$.dst.host = NULL;
35833b3a8eb9SGleb Smirnoff 			$$.dst.port = NULL;
35843b3a8eb9SGleb Smirnoff 			$$.src_os = NULL;
35853b3a8eb9SGleb Smirnoff 		}
35863b3a8eb9SGleb Smirnoff 		| from os to			{
35873b3a8eb9SGleb Smirnoff 			$$.src = $1;
35883b3a8eb9SGleb Smirnoff 			$$.src_os = $2;
35893b3a8eb9SGleb Smirnoff 			$$.dst = $3;
35903b3a8eb9SGleb Smirnoff 		}
35913b3a8eb9SGleb Smirnoff 		;
35923b3a8eb9SGleb Smirnoff 
35933b3a8eb9SGleb Smirnoff os		: /* empty */			{ $$ = NULL; }
35943b3a8eb9SGleb Smirnoff 		| OS xos			{ $$ = $2; }
35953b3a8eb9SGleb Smirnoff 		| OS '{' optnl os_list '}'	{ $$ = $4; }
35963b3a8eb9SGleb Smirnoff 		;
35973b3a8eb9SGleb Smirnoff 
35983b3a8eb9SGleb Smirnoff xos		: STRING {
35993b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_os));
36003b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
36013b3a8eb9SGleb Smirnoff 				err(1, "os: calloc");
36023b3a8eb9SGleb Smirnoff 			$$->os = $1;
36033b3a8eb9SGleb Smirnoff 			$$->tail = $$;
36043b3a8eb9SGleb Smirnoff 		}
36053b3a8eb9SGleb Smirnoff 		;
36063b3a8eb9SGleb Smirnoff 
36073b3a8eb9SGleb Smirnoff os_list		: xos optnl 			{ $$ = $1; }
36083b3a8eb9SGleb Smirnoff 		| os_list comma xos optnl	{
36093b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
36103b3a8eb9SGleb Smirnoff 			$1->tail = $3;
36113b3a8eb9SGleb Smirnoff 			$$ = $1;
36123b3a8eb9SGleb Smirnoff 		}
36133b3a8eb9SGleb Smirnoff 		;
36143b3a8eb9SGleb Smirnoff 
36153b3a8eb9SGleb Smirnoff from		: /* empty */			{
36163b3a8eb9SGleb Smirnoff 			$$.host = NULL;
36173b3a8eb9SGleb Smirnoff 			$$.port = NULL;
36183b3a8eb9SGleb Smirnoff 		}
36193b3a8eb9SGleb Smirnoff 		| FROM ipportspec		{
36203b3a8eb9SGleb Smirnoff 			$$ = $2;
36213b3a8eb9SGleb Smirnoff 		}
36223b3a8eb9SGleb Smirnoff 		;
36233b3a8eb9SGleb Smirnoff 
36243b3a8eb9SGleb Smirnoff to		: /* empty */			{
36253b3a8eb9SGleb Smirnoff 			$$.host = NULL;
36263b3a8eb9SGleb Smirnoff 			$$.port = NULL;
36273b3a8eb9SGleb Smirnoff 		}
36283b3a8eb9SGleb Smirnoff 		| TO ipportspec		{
36293b3a8eb9SGleb Smirnoff 			if (disallow_urpf_failed($2.host, "\"urpf-failed\" is "
36303b3a8eb9SGleb Smirnoff 			    "not permitted in a destination address"))
36313b3a8eb9SGleb Smirnoff 				YYERROR;
36323b3a8eb9SGleb Smirnoff 			$$ = $2;
36333b3a8eb9SGleb Smirnoff 		}
36343b3a8eb9SGleb Smirnoff 		;
36353b3a8eb9SGleb Smirnoff 
36363b3a8eb9SGleb Smirnoff ipportspec	: ipspec			{
36373b3a8eb9SGleb Smirnoff 			$$.host = $1;
36383b3a8eb9SGleb Smirnoff 			$$.port = NULL;
36393b3a8eb9SGleb Smirnoff 		}
36403b3a8eb9SGleb Smirnoff 		| ipspec PORT portspec		{
36413b3a8eb9SGleb Smirnoff 			$$.host = $1;
36423b3a8eb9SGleb Smirnoff 			$$.port = $3;
36433b3a8eb9SGleb Smirnoff 		}
36443b3a8eb9SGleb Smirnoff 		| PORT portspec			{
36453b3a8eb9SGleb Smirnoff 			$$.host = NULL;
36463b3a8eb9SGleb Smirnoff 			$$.port = $2;
36473b3a8eb9SGleb Smirnoff 		}
36483b3a8eb9SGleb Smirnoff 		;
36493b3a8eb9SGleb Smirnoff 
36503b3a8eb9SGleb Smirnoff optnl		: '\n' optnl
36513b3a8eb9SGleb Smirnoff 		|
36523b3a8eb9SGleb Smirnoff 		;
36533b3a8eb9SGleb Smirnoff 
36543b3a8eb9SGleb Smirnoff ipspec		: ANY				{ $$ = NULL; }
36553b3a8eb9SGleb Smirnoff 		| xhost				{ $$ = $1; }
36563b3a8eb9SGleb Smirnoff 		| '{' optnl host_list '}'	{ $$ = $3; }
36573b3a8eb9SGleb Smirnoff 		;
36583b3a8eb9SGleb Smirnoff 
36593b3a8eb9SGleb Smirnoff toipspec	: TO ipspec			{ $$ = $2; }
36603b3a8eb9SGleb Smirnoff 		| /* empty */			{ $$ = NULL; }
36613b3a8eb9SGleb Smirnoff 		;
36623b3a8eb9SGleb Smirnoff 
36633b3a8eb9SGleb Smirnoff host_list	: ipspec optnl			{ $$ = $1; }
36643b3a8eb9SGleb Smirnoff 		| host_list comma ipspec optnl	{
3665637d81c5SKristof Provost 			if ($1 == NULL) {
3666637d81c5SKristof Provost 				freehostlist($3);
36673b3a8eb9SGleb Smirnoff 				$$ = $1;
3668637d81c5SKristof Provost 			} else if ($3 == NULL) {
3669637d81c5SKristof Provost 				freehostlist($1);
36703b3a8eb9SGleb Smirnoff 				$$ = $3;
3671637d81c5SKristof Provost 			} else {
36723b3a8eb9SGleb Smirnoff 				$1->tail->next = $3;
36733b3a8eb9SGleb Smirnoff 				$1->tail = $3->tail;
36743b3a8eb9SGleb Smirnoff 				$$ = $1;
36753b3a8eb9SGleb Smirnoff 			}
36763b3a8eb9SGleb Smirnoff 		}
36773b3a8eb9SGleb Smirnoff 		;
36783b3a8eb9SGleb Smirnoff 
36793b3a8eb9SGleb Smirnoff xhost		: not host			{
36803b3a8eb9SGleb Smirnoff 			struct node_host	*n;
36813b3a8eb9SGleb Smirnoff 
36823b3a8eb9SGleb Smirnoff 			for (n = $2; n != NULL; n = n->next)
36833b3a8eb9SGleb Smirnoff 				n->not = $1;
36843b3a8eb9SGleb Smirnoff 			$$ = $2;
36853b3a8eb9SGleb Smirnoff 		}
36863b3a8eb9SGleb Smirnoff 		| not NOROUTE			{
36873b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_host));
36883b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
36893b3a8eb9SGleb Smirnoff 				err(1, "xhost: calloc");
36903b3a8eb9SGleb Smirnoff 			$$->addr.type = PF_ADDR_NOROUTE;
36913b3a8eb9SGleb Smirnoff 			$$->next = NULL;
36923b3a8eb9SGleb Smirnoff 			$$->not = $1;
36933b3a8eb9SGleb Smirnoff 			$$->tail = $$;
36943b3a8eb9SGleb Smirnoff 		}
36953b3a8eb9SGleb Smirnoff 		| not URPFFAILED		{
36963b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_host));
36973b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
36983b3a8eb9SGleb Smirnoff 				err(1, "xhost: calloc");
36993b3a8eb9SGleb Smirnoff 			$$->addr.type = PF_ADDR_URPFFAILED;
37003b3a8eb9SGleb Smirnoff 			$$->next = NULL;
37013b3a8eb9SGleb Smirnoff 			$$->not = $1;
37023b3a8eb9SGleb Smirnoff 			$$->tail = $$;
37033b3a8eb9SGleb Smirnoff 		}
37043b3a8eb9SGleb Smirnoff 		;
37053b3a8eb9SGleb Smirnoff 
37063b3a8eb9SGleb Smirnoff host		: STRING			{
37073b3a8eb9SGleb Smirnoff 			if (($$ = host($1)) == NULL)	{
37083b3a8eb9SGleb Smirnoff 				/* error. "any" is handled elsewhere */
37093b3a8eb9SGleb Smirnoff 				free($1);
37103b3a8eb9SGleb Smirnoff 				yyerror("could not parse host specification");
37113b3a8eb9SGleb Smirnoff 				YYERROR;
37123b3a8eb9SGleb Smirnoff 			}
37133b3a8eb9SGleb Smirnoff 			free($1);
37143b3a8eb9SGleb Smirnoff 
37153b3a8eb9SGleb Smirnoff 		}
37163b3a8eb9SGleb Smirnoff 		| STRING '-' STRING		{
37173b3a8eb9SGleb Smirnoff 			struct node_host *b, *e;
37183b3a8eb9SGleb Smirnoff 
37193b3a8eb9SGleb Smirnoff 			if ((b = host($1)) == NULL || (e = host($3)) == NULL) {
37203b3a8eb9SGleb Smirnoff 				free($1);
37213b3a8eb9SGleb Smirnoff 				free($3);
37223b3a8eb9SGleb Smirnoff 				yyerror("could not parse host specification");
37233b3a8eb9SGleb Smirnoff 				YYERROR;
37243b3a8eb9SGleb Smirnoff 			}
37253b3a8eb9SGleb Smirnoff 			if (b->af != e->af ||
37263b3a8eb9SGleb Smirnoff 			    b->addr.type != PF_ADDR_ADDRMASK ||
37273b3a8eb9SGleb Smirnoff 			    e->addr.type != PF_ADDR_ADDRMASK ||
37283b3a8eb9SGleb Smirnoff 			    unmask(&b->addr.v.a.mask, b->af) !=
37293b3a8eb9SGleb Smirnoff 			    (b->af == AF_INET ? 32 : 128) ||
37303b3a8eb9SGleb Smirnoff 			    unmask(&e->addr.v.a.mask, e->af) !=
37313b3a8eb9SGleb Smirnoff 			    (e->af == AF_INET ? 32 : 128) ||
37323b3a8eb9SGleb Smirnoff 			    b->next != NULL || b->not ||
37333b3a8eb9SGleb Smirnoff 			    e->next != NULL || e->not) {
37343b3a8eb9SGleb Smirnoff 				free(b);
37353b3a8eb9SGleb Smirnoff 				free(e);
37363b3a8eb9SGleb Smirnoff 				free($1);
37373b3a8eb9SGleb Smirnoff 				free($3);
37383b3a8eb9SGleb Smirnoff 				yyerror("invalid address range");
37393b3a8eb9SGleb Smirnoff 				YYERROR;
37403b3a8eb9SGleb Smirnoff 			}
37413b3a8eb9SGleb Smirnoff 			memcpy(&b->addr.v.a.mask, &e->addr.v.a.addr,
37423b3a8eb9SGleb Smirnoff 			    sizeof(b->addr.v.a.mask));
37433b3a8eb9SGleb Smirnoff 			b->addr.type = PF_ADDR_RANGE;
37443b3a8eb9SGleb Smirnoff 			$$ = b;
37453b3a8eb9SGleb Smirnoff 			free(e);
37463b3a8eb9SGleb Smirnoff 			free($1);
37473b3a8eb9SGleb Smirnoff 			free($3);
37483b3a8eb9SGleb Smirnoff 		}
37493b3a8eb9SGleb Smirnoff 		| STRING '/' NUMBER		{
37503b3a8eb9SGleb Smirnoff 			char	*buf;
37513b3a8eb9SGleb Smirnoff 
37523b3a8eb9SGleb Smirnoff 			if (asprintf(&buf, "%s/%lld", $1, (long long)$3) == -1)
37533b3a8eb9SGleb Smirnoff 				err(1, "host: asprintf");
37543b3a8eb9SGleb Smirnoff 			free($1);
37553b3a8eb9SGleb Smirnoff 			if (($$ = host(buf)) == NULL)	{
37563b3a8eb9SGleb Smirnoff 				/* error. "any" is handled elsewhere */
37573b3a8eb9SGleb Smirnoff 				free(buf);
37583b3a8eb9SGleb Smirnoff 				yyerror("could not parse host specification");
37593b3a8eb9SGleb Smirnoff 				YYERROR;
37603b3a8eb9SGleb Smirnoff 			}
37613b3a8eb9SGleb Smirnoff 			free(buf);
37623b3a8eb9SGleb Smirnoff 		}
37633b3a8eb9SGleb Smirnoff 		| NUMBER '/' NUMBER		{
37643b3a8eb9SGleb Smirnoff 			char	*buf;
37653b3a8eb9SGleb Smirnoff 
37663b3a8eb9SGleb Smirnoff 			/* ie. for 10/8 parsing */
37673b3a8eb9SGleb Smirnoff #ifdef __FreeBSD__
37683b3a8eb9SGleb Smirnoff 			if (asprintf(&buf, "%lld/%lld", (long long)$1, (long long)$3) == -1)
37693b3a8eb9SGleb Smirnoff #else
37703b3a8eb9SGleb Smirnoff 			if (asprintf(&buf, "%lld/%lld", $1, $3) == -1)
37713b3a8eb9SGleb Smirnoff #endif
37723b3a8eb9SGleb Smirnoff 				err(1, "host: asprintf");
37733b3a8eb9SGleb Smirnoff 			if (($$ = host(buf)) == NULL)	{
37743b3a8eb9SGleb Smirnoff 				/* error. "any" is handled elsewhere */
37753b3a8eb9SGleb Smirnoff 				free(buf);
37763b3a8eb9SGleb Smirnoff 				yyerror("could not parse host specification");
37773b3a8eb9SGleb Smirnoff 				YYERROR;
37783b3a8eb9SGleb Smirnoff 			}
37793b3a8eb9SGleb Smirnoff 			free(buf);
37803b3a8eb9SGleb Smirnoff 		}
37813b3a8eb9SGleb Smirnoff 		| dynaddr
37823b3a8eb9SGleb Smirnoff 		| dynaddr '/' NUMBER		{
37833b3a8eb9SGleb Smirnoff 			struct node_host	*n;
37843b3a8eb9SGleb Smirnoff 
37853b3a8eb9SGleb Smirnoff 			if ($3 < 0 || $3 > 128) {
37863b3a8eb9SGleb Smirnoff 				yyerror("bit number too big");
37873b3a8eb9SGleb Smirnoff 				YYERROR;
37883b3a8eb9SGleb Smirnoff 			}
37893b3a8eb9SGleb Smirnoff 			$$ = $1;
37903b3a8eb9SGleb Smirnoff 			for (n = $1; n != NULL; n = n->next)
37913b3a8eb9SGleb Smirnoff 				set_ipmask(n, $3);
37923b3a8eb9SGleb Smirnoff 		}
37933b3a8eb9SGleb Smirnoff 		| '<' STRING '>'	{
37943b3a8eb9SGleb Smirnoff 			if (strlen($2) >= PF_TABLE_NAME_SIZE) {
37953b3a8eb9SGleb Smirnoff 				yyerror("table name '%s' too long", $2);
37963b3a8eb9SGleb Smirnoff 				free($2);
37973b3a8eb9SGleb Smirnoff 				YYERROR;
37983b3a8eb9SGleb Smirnoff 			}
37993b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_host));
38003b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
38013b3a8eb9SGleb Smirnoff 				err(1, "host: calloc");
38023b3a8eb9SGleb Smirnoff 			$$->addr.type = PF_ADDR_TABLE;
38033b3a8eb9SGleb Smirnoff 			if (strlcpy($$->addr.v.tblname, $2,
38043b3a8eb9SGleb Smirnoff 			    sizeof($$->addr.v.tblname)) >=
38053b3a8eb9SGleb Smirnoff 			    sizeof($$->addr.v.tblname))
38063b3a8eb9SGleb Smirnoff 				errx(1, "host: strlcpy");
38073b3a8eb9SGleb Smirnoff 			free($2);
38083b3a8eb9SGleb Smirnoff 			$$->next = NULL;
38093b3a8eb9SGleb Smirnoff 			$$->tail = $$;
38103b3a8eb9SGleb Smirnoff 		}
38113b3a8eb9SGleb Smirnoff 		;
38123b3a8eb9SGleb Smirnoff 
38133b3a8eb9SGleb Smirnoff number		: NUMBER
38143b3a8eb9SGleb Smirnoff 		| STRING		{
38153b3a8eb9SGleb Smirnoff 			u_long	ulval;
38163b3a8eb9SGleb Smirnoff 
38173b3a8eb9SGleb Smirnoff 			if (atoul($1, &ulval) == -1) {
38183b3a8eb9SGleb Smirnoff 				yyerror("%s is not a number", $1);
38193b3a8eb9SGleb Smirnoff 				free($1);
38203b3a8eb9SGleb Smirnoff 				YYERROR;
38213b3a8eb9SGleb Smirnoff 			} else
38223b3a8eb9SGleb Smirnoff 				$$ = ulval;
38233b3a8eb9SGleb Smirnoff 			free($1);
38243b3a8eb9SGleb Smirnoff 		}
38253b3a8eb9SGleb Smirnoff 		;
38263b3a8eb9SGleb Smirnoff 
38273b3a8eb9SGleb Smirnoff dynaddr		: '(' STRING ')'		{
38283b3a8eb9SGleb Smirnoff 			int	 flags = 0;
38293b3a8eb9SGleb Smirnoff 			char	*p, *op;
38303b3a8eb9SGleb Smirnoff 
38313b3a8eb9SGleb Smirnoff 			op = $2;
38323b3a8eb9SGleb Smirnoff 			if (!isalpha(op[0])) {
38333b3a8eb9SGleb Smirnoff 				yyerror("invalid interface name '%s'", op);
38343b3a8eb9SGleb Smirnoff 				free(op);
38353b3a8eb9SGleb Smirnoff 				YYERROR;
38363b3a8eb9SGleb Smirnoff 			}
38373b3a8eb9SGleb Smirnoff 			while ((p = strrchr($2, ':')) != NULL) {
38383b3a8eb9SGleb Smirnoff 				if (!strcmp(p+1, "network"))
38393b3a8eb9SGleb Smirnoff 					flags |= PFI_AFLAG_NETWORK;
38403b3a8eb9SGleb Smirnoff 				else if (!strcmp(p+1, "broadcast"))
38413b3a8eb9SGleb Smirnoff 					flags |= PFI_AFLAG_BROADCAST;
38423b3a8eb9SGleb Smirnoff 				else if (!strcmp(p+1, "peer"))
38433b3a8eb9SGleb Smirnoff 					flags |= PFI_AFLAG_PEER;
38443b3a8eb9SGleb Smirnoff 				else if (!strcmp(p+1, "0"))
38453b3a8eb9SGleb Smirnoff 					flags |= PFI_AFLAG_NOALIAS;
38463b3a8eb9SGleb Smirnoff 				else {
38473b3a8eb9SGleb Smirnoff 					yyerror("interface %s has bad modifier",
38483b3a8eb9SGleb Smirnoff 					    $2);
38493b3a8eb9SGleb Smirnoff 					free(op);
38503b3a8eb9SGleb Smirnoff 					YYERROR;
38513b3a8eb9SGleb Smirnoff 				}
38523b3a8eb9SGleb Smirnoff 				*p = '\0';
38533b3a8eb9SGleb Smirnoff 			}
38543b3a8eb9SGleb Smirnoff 			if (flags & (flags - 1) & PFI_AFLAG_MODEMASK) {
38553b3a8eb9SGleb Smirnoff 				free(op);
38563b3a8eb9SGleb Smirnoff 				yyerror("illegal combination of "
38573b3a8eb9SGleb Smirnoff 				    "interface modifiers");
38583b3a8eb9SGleb Smirnoff 				YYERROR;
38593b3a8eb9SGleb Smirnoff 			}
38603b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_host));
38613b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
38623b3a8eb9SGleb Smirnoff 				err(1, "address: calloc");
38633b3a8eb9SGleb Smirnoff 			$$->af = 0;
38643b3a8eb9SGleb Smirnoff 			set_ipmask($$, 128);
38653b3a8eb9SGleb Smirnoff 			$$->addr.type = PF_ADDR_DYNIFTL;
38663b3a8eb9SGleb Smirnoff 			$$->addr.iflags = flags;
38673b3a8eb9SGleb Smirnoff 			if (strlcpy($$->addr.v.ifname, $2,
38683b3a8eb9SGleb Smirnoff 			    sizeof($$->addr.v.ifname)) >=
38693b3a8eb9SGleb Smirnoff 			    sizeof($$->addr.v.ifname)) {
38703b3a8eb9SGleb Smirnoff 				free(op);
38713b3a8eb9SGleb Smirnoff 				free($$);
38723b3a8eb9SGleb Smirnoff 				yyerror("interface name too long");
38733b3a8eb9SGleb Smirnoff 				YYERROR;
38743b3a8eb9SGleb Smirnoff 			}
38753b3a8eb9SGleb Smirnoff 			free(op);
38763b3a8eb9SGleb Smirnoff 			$$->next = NULL;
38773b3a8eb9SGleb Smirnoff 			$$->tail = $$;
38783b3a8eb9SGleb Smirnoff 		}
38793b3a8eb9SGleb Smirnoff 		;
38803b3a8eb9SGleb Smirnoff 
38813b3a8eb9SGleb Smirnoff portspec	: port_item			{ $$ = $1; }
38823b3a8eb9SGleb Smirnoff 		| '{' optnl port_list '}'	{ $$ = $3; }
38833b3a8eb9SGleb Smirnoff 		;
38843b3a8eb9SGleb Smirnoff 
38853b3a8eb9SGleb Smirnoff port_list	: port_item optnl		{ $$ = $1; }
38863b3a8eb9SGleb Smirnoff 		| port_list comma port_item optnl	{
38873b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
38883b3a8eb9SGleb Smirnoff 			$1->tail = $3;
38893b3a8eb9SGleb Smirnoff 			$$ = $1;
38903b3a8eb9SGleb Smirnoff 		}
38913b3a8eb9SGleb Smirnoff 		;
38923b3a8eb9SGleb Smirnoff 
38933b3a8eb9SGleb Smirnoff port_item	: portrange			{
38943b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_port));
38953b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
38963b3a8eb9SGleb Smirnoff 				err(1, "port_item: calloc");
38973b3a8eb9SGleb Smirnoff 			$$->port[0] = $1.a;
38983b3a8eb9SGleb Smirnoff 			$$->port[1] = $1.b;
38993b3a8eb9SGleb Smirnoff 			if ($1.t)
39003b3a8eb9SGleb Smirnoff 				$$->op = PF_OP_RRG;
39013b3a8eb9SGleb Smirnoff 			else
39023b3a8eb9SGleb Smirnoff 				$$->op = PF_OP_EQ;
39033b3a8eb9SGleb Smirnoff 			$$->next = NULL;
39043b3a8eb9SGleb Smirnoff 			$$->tail = $$;
39053b3a8eb9SGleb Smirnoff 		}
39063b3a8eb9SGleb Smirnoff 		| unaryop portrange	{
39073b3a8eb9SGleb Smirnoff 			if ($2.t) {
39083b3a8eb9SGleb Smirnoff 				yyerror("':' cannot be used with an other "
39093b3a8eb9SGleb Smirnoff 				    "port operator");
39103b3a8eb9SGleb Smirnoff 				YYERROR;
39113b3a8eb9SGleb Smirnoff 			}
39123b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_port));
39133b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
39143b3a8eb9SGleb Smirnoff 				err(1, "port_item: calloc");
39153b3a8eb9SGleb Smirnoff 			$$->port[0] = $2.a;
39163b3a8eb9SGleb Smirnoff 			$$->port[1] = $2.b;
39173b3a8eb9SGleb Smirnoff 			$$->op = $1;
39183b3a8eb9SGleb Smirnoff 			$$->next = NULL;
39193b3a8eb9SGleb Smirnoff 			$$->tail = $$;
39203b3a8eb9SGleb Smirnoff 		}
39213b3a8eb9SGleb Smirnoff 		| portrange PORTBINARY portrange	{
39223b3a8eb9SGleb Smirnoff 			if ($1.t || $3.t) {
39233b3a8eb9SGleb Smirnoff 				yyerror("':' cannot be used with an other "
39243b3a8eb9SGleb Smirnoff 				    "port operator");
39253b3a8eb9SGleb Smirnoff 				YYERROR;
39263b3a8eb9SGleb Smirnoff 			}
39273b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_port));
39283b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
39293b3a8eb9SGleb Smirnoff 				err(1, "port_item: calloc");
39303b3a8eb9SGleb Smirnoff 			$$->port[0] = $1.a;
39313b3a8eb9SGleb Smirnoff 			$$->port[1] = $3.a;
39323b3a8eb9SGleb Smirnoff 			$$->op = $2;
39333b3a8eb9SGleb Smirnoff 			$$->next = NULL;
39343b3a8eb9SGleb Smirnoff 			$$->tail = $$;
39353b3a8eb9SGleb Smirnoff 		}
39363b3a8eb9SGleb Smirnoff 		;
39373b3a8eb9SGleb Smirnoff 
39383b3a8eb9SGleb Smirnoff portplain	: numberstring			{
39393b3a8eb9SGleb Smirnoff 			if (parseport($1, &$$, 0) == -1) {
39403b3a8eb9SGleb Smirnoff 				free($1);
39413b3a8eb9SGleb Smirnoff 				YYERROR;
39423b3a8eb9SGleb Smirnoff 			}
39433b3a8eb9SGleb Smirnoff 			free($1);
39443b3a8eb9SGleb Smirnoff 		}
39453b3a8eb9SGleb Smirnoff 		;
39463b3a8eb9SGleb Smirnoff 
39473b3a8eb9SGleb Smirnoff portrange	: numberstring			{
39483b3a8eb9SGleb Smirnoff 			if (parseport($1, &$$, PPORT_RANGE) == -1) {
39493b3a8eb9SGleb Smirnoff 				free($1);
39503b3a8eb9SGleb Smirnoff 				YYERROR;
39513b3a8eb9SGleb Smirnoff 			}
39523b3a8eb9SGleb Smirnoff 			free($1);
39533b3a8eb9SGleb Smirnoff 		}
39543b3a8eb9SGleb Smirnoff 		;
39553b3a8eb9SGleb Smirnoff 
39563b3a8eb9SGleb Smirnoff uids		: uid_item			{ $$ = $1; }
39573b3a8eb9SGleb Smirnoff 		| '{' optnl uid_list '}'	{ $$ = $3; }
39583b3a8eb9SGleb Smirnoff 		;
39593b3a8eb9SGleb Smirnoff 
39603b3a8eb9SGleb Smirnoff uid_list	: uid_item optnl		{ $$ = $1; }
39613b3a8eb9SGleb Smirnoff 		| uid_list comma uid_item optnl	{
39623b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
39633b3a8eb9SGleb Smirnoff 			$1->tail = $3;
39643b3a8eb9SGleb Smirnoff 			$$ = $1;
39653b3a8eb9SGleb Smirnoff 		}
39663b3a8eb9SGleb Smirnoff 		;
39673b3a8eb9SGleb Smirnoff 
39683b3a8eb9SGleb Smirnoff uid_item	: uid				{
39693b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_uid));
39703b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
39713b3a8eb9SGleb Smirnoff 				err(1, "uid_item: calloc");
39723b3a8eb9SGleb Smirnoff 			$$->uid[0] = $1;
39733b3a8eb9SGleb Smirnoff 			$$->uid[1] = $1;
39743b3a8eb9SGleb Smirnoff 			$$->op = PF_OP_EQ;
39753b3a8eb9SGleb Smirnoff 			$$->next = NULL;
39763b3a8eb9SGleb Smirnoff 			$$->tail = $$;
39773b3a8eb9SGleb Smirnoff 		}
39783b3a8eb9SGleb Smirnoff 		| unaryop uid			{
39793b3a8eb9SGleb Smirnoff 			if ($2 == UID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
39803b3a8eb9SGleb Smirnoff 				yyerror("user unknown requires operator = or "
39813b3a8eb9SGleb Smirnoff 				    "!=");
39823b3a8eb9SGleb Smirnoff 				YYERROR;
39833b3a8eb9SGleb Smirnoff 			}
39843b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_uid));
39853b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
39863b3a8eb9SGleb Smirnoff 				err(1, "uid_item: calloc");
39873b3a8eb9SGleb Smirnoff 			$$->uid[0] = $2;
39883b3a8eb9SGleb Smirnoff 			$$->uid[1] = $2;
39893b3a8eb9SGleb Smirnoff 			$$->op = $1;
39903b3a8eb9SGleb Smirnoff 			$$->next = NULL;
39913b3a8eb9SGleb Smirnoff 			$$->tail = $$;
39923b3a8eb9SGleb Smirnoff 		}
39933b3a8eb9SGleb Smirnoff 		| uid PORTBINARY uid		{
39943b3a8eb9SGleb Smirnoff 			if ($1 == UID_MAX || $3 == UID_MAX) {
39953b3a8eb9SGleb Smirnoff 				yyerror("user unknown requires operator = or "
39963b3a8eb9SGleb Smirnoff 				    "!=");
39973b3a8eb9SGleb Smirnoff 				YYERROR;
39983b3a8eb9SGleb Smirnoff 			}
39993b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_uid));
40003b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
40013b3a8eb9SGleb Smirnoff 				err(1, "uid_item: calloc");
40023b3a8eb9SGleb Smirnoff 			$$->uid[0] = $1;
40033b3a8eb9SGleb Smirnoff 			$$->uid[1] = $3;
40043b3a8eb9SGleb Smirnoff 			$$->op = $2;
40053b3a8eb9SGleb Smirnoff 			$$->next = NULL;
40063b3a8eb9SGleb Smirnoff 			$$->tail = $$;
40073b3a8eb9SGleb Smirnoff 		}
40083b3a8eb9SGleb Smirnoff 		;
40093b3a8eb9SGleb Smirnoff 
40103b3a8eb9SGleb Smirnoff uid		: STRING			{
40113b3a8eb9SGleb Smirnoff 			if (!strcmp($1, "unknown"))
40123b3a8eb9SGleb Smirnoff 				$$ = UID_MAX;
40133b3a8eb9SGleb Smirnoff 			else {
40143b3a8eb9SGleb Smirnoff 				struct passwd	*pw;
40153b3a8eb9SGleb Smirnoff 
40163b3a8eb9SGleb Smirnoff 				if ((pw = getpwnam($1)) == NULL) {
40173b3a8eb9SGleb Smirnoff 					yyerror("unknown user %s", $1);
40183b3a8eb9SGleb Smirnoff 					free($1);
40193b3a8eb9SGleb Smirnoff 					YYERROR;
40203b3a8eb9SGleb Smirnoff 				}
40213b3a8eb9SGleb Smirnoff 				$$ = pw->pw_uid;
40223b3a8eb9SGleb Smirnoff 			}
40233b3a8eb9SGleb Smirnoff 			free($1);
40243b3a8eb9SGleb Smirnoff 		}
40253b3a8eb9SGleb Smirnoff 		| NUMBER			{
40263b3a8eb9SGleb Smirnoff 			if ($1 < 0 || $1 >= UID_MAX) {
40273b3a8eb9SGleb Smirnoff 				yyerror("illegal uid value %lu", $1);
40283b3a8eb9SGleb Smirnoff 				YYERROR;
40293b3a8eb9SGleb Smirnoff 			}
40303b3a8eb9SGleb Smirnoff 			$$ = $1;
40313b3a8eb9SGleb Smirnoff 		}
40323b3a8eb9SGleb Smirnoff 		;
40333b3a8eb9SGleb Smirnoff 
40343b3a8eb9SGleb Smirnoff gids		: gid_item			{ $$ = $1; }
40353b3a8eb9SGleb Smirnoff 		| '{' optnl gid_list '}'	{ $$ = $3; }
40363b3a8eb9SGleb Smirnoff 		;
40373b3a8eb9SGleb Smirnoff 
40383b3a8eb9SGleb Smirnoff gid_list	: gid_item optnl		{ $$ = $1; }
40393b3a8eb9SGleb Smirnoff 		| gid_list comma gid_item optnl	{
40403b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
40413b3a8eb9SGleb Smirnoff 			$1->tail = $3;
40423b3a8eb9SGleb Smirnoff 			$$ = $1;
40433b3a8eb9SGleb Smirnoff 		}
40443b3a8eb9SGleb Smirnoff 		;
40453b3a8eb9SGleb Smirnoff 
40463b3a8eb9SGleb Smirnoff gid_item	: gid				{
40473b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_gid));
40483b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
40493b3a8eb9SGleb Smirnoff 				err(1, "gid_item: calloc");
40503b3a8eb9SGleb Smirnoff 			$$->gid[0] = $1;
40513b3a8eb9SGleb Smirnoff 			$$->gid[1] = $1;
40523b3a8eb9SGleb Smirnoff 			$$->op = PF_OP_EQ;
40533b3a8eb9SGleb Smirnoff 			$$->next = NULL;
40543b3a8eb9SGleb Smirnoff 			$$->tail = $$;
40553b3a8eb9SGleb Smirnoff 		}
40563b3a8eb9SGleb Smirnoff 		| unaryop gid			{
40573b3a8eb9SGleb Smirnoff 			if ($2 == GID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
40583b3a8eb9SGleb Smirnoff 				yyerror("group unknown requires operator = or "
40593b3a8eb9SGleb Smirnoff 				    "!=");
40603b3a8eb9SGleb Smirnoff 				YYERROR;
40613b3a8eb9SGleb Smirnoff 			}
40623b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_gid));
40633b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
40643b3a8eb9SGleb Smirnoff 				err(1, "gid_item: calloc");
40653b3a8eb9SGleb Smirnoff 			$$->gid[0] = $2;
40663b3a8eb9SGleb Smirnoff 			$$->gid[1] = $2;
40673b3a8eb9SGleb Smirnoff 			$$->op = $1;
40683b3a8eb9SGleb Smirnoff 			$$->next = NULL;
40693b3a8eb9SGleb Smirnoff 			$$->tail = $$;
40703b3a8eb9SGleb Smirnoff 		}
40713b3a8eb9SGleb Smirnoff 		| gid PORTBINARY gid		{
40723b3a8eb9SGleb Smirnoff 			if ($1 == GID_MAX || $3 == GID_MAX) {
40733b3a8eb9SGleb Smirnoff 				yyerror("group unknown requires operator = or "
40743b3a8eb9SGleb Smirnoff 				    "!=");
40753b3a8eb9SGleb Smirnoff 				YYERROR;
40763b3a8eb9SGleb Smirnoff 			}
40773b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_gid));
40783b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
40793b3a8eb9SGleb Smirnoff 				err(1, "gid_item: calloc");
40803b3a8eb9SGleb Smirnoff 			$$->gid[0] = $1;
40813b3a8eb9SGleb Smirnoff 			$$->gid[1] = $3;
40823b3a8eb9SGleb Smirnoff 			$$->op = $2;
40833b3a8eb9SGleb Smirnoff 			$$->next = NULL;
40843b3a8eb9SGleb Smirnoff 			$$->tail = $$;
40853b3a8eb9SGleb Smirnoff 		}
40863b3a8eb9SGleb Smirnoff 		;
40873b3a8eb9SGleb Smirnoff 
40883b3a8eb9SGleb Smirnoff gid		: STRING			{
40893b3a8eb9SGleb Smirnoff 			if (!strcmp($1, "unknown"))
40903b3a8eb9SGleb Smirnoff 				$$ = GID_MAX;
40913b3a8eb9SGleb Smirnoff 			else {
40923b3a8eb9SGleb Smirnoff 				struct group	*grp;
40933b3a8eb9SGleb Smirnoff 
40943b3a8eb9SGleb Smirnoff 				if ((grp = getgrnam($1)) == NULL) {
40953b3a8eb9SGleb Smirnoff 					yyerror("unknown group %s", $1);
40963b3a8eb9SGleb Smirnoff 					free($1);
40973b3a8eb9SGleb Smirnoff 					YYERROR;
40983b3a8eb9SGleb Smirnoff 				}
40993b3a8eb9SGleb Smirnoff 				$$ = grp->gr_gid;
41003b3a8eb9SGleb Smirnoff 			}
41013b3a8eb9SGleb Smirnoff 			free($1);
41023b3a8eb9SGleb Smirnoff 		}
41033b3a8eb9SGleb Smirnoff 		| NUMBER			{
41043b3a8eb9SGleb Smirnoff 			if ($1 < 0 || $1 >= GID_MAX) {
41053b3a8eb9SGleb Smirnoff 				yyerror("illegal gid value %lu", $1);
41063b3a8eb9SGleb Smirnoff 				YYERROR;
41073b3a8eb9SGleb Smirnoff 			}
41083b3a8eb9SGleb Smirnoff 			$$ = $1;
41093b3a8eb9SGleb Smirnoff 		}
41103b3a8eb9SGleb Smirnoff 		;
41113b3a8eb9SGleb Smirnoff 
41123b3a8eb9SGleb Smirnoff flag		: STRING			{
41133b3a8eb9SGleb Smirnoff 			int	f;
41143b3a8eb9SGleb Smirnoff 
41153b3a8eb9SGleb Smirnoff 			if ((f = parse_flags($1)) < 0) {
41163b3a8eb9SGleb Smirnoff 				yyerror("bad flags %s", $1);
41173b3a8eb9SGleb Smirnoff 				free($1);
41183b3a8eb9SGleb Smirnoff 				YYERROR;
41193b3a8eb9SGleb Smirnoff 			}
41203b3a8eb9SGleb Smirnoff 			free($1);
41213b3a8eb9SGleb Smirnoff 			$$.b1 = f;
41223b3a8eb9SGleb Smirnoff 		}
41233b3a8eb9SGleb Smirnoff 		;
41243b3a8eb9SGleb Smirnoff 
41253b3a8eb9SGleb Smirnoff flags		: FLAGS flag '/' flag	{ $$.b1 = $2.b1; $$.b2 = $4.b1; }
41263b3a8eb9SGleb Smirnoff 		| FLAGS '/' flag	{ $$.b1 = 0; $$.b2 = $3.b1; }
41273b3a8eb9SGleb Smirnoff 		| FLAGS ANY		{ $$.b1 = 0; $$.b2 = 0; }
41283b3a8eb9SGleb Smirnoff 		;
41293b3a8eb9SGleb Smirnoff 
41303b3a8eb9SGleb Smirnoff icmpspec	: ICMPTYPE icmp_item			{ $$ = $2; }
41313b3a8eb9SGleb Smirnoff 		| ICMPTYPE '{' optnl icmp_list '}'	{ $$ = $4; }
41323b3a8eb9SGleb Smirnoff 		| ICMP6TYPE icmp6_item			{ $$ = $2; }
41333b3a8eb9SGleb Smirnoff 		| ICMP6TYPE '{' optnl icmp6_list '}'	{ $$ = $4; }
41343b3a8eb9SGleb Smirnoff 		;
41353b3a8eb9SGleb Smirnoff 
41363b3a8eb9SGleb Smirnoff icmp_list	: icmp_item optnl		{ $$ = $1; }
41373b3a8eb9SGleb Smirnoff 		| icmp_list comma icmp_item optnl {
41383b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
41393b3a8eb9SGleb Smirnoff 			$1->tail = $3;
41403b3a8eb9SGleb Smirnoff 			$$ = $1;
41413b3a8eb9SGleb Smirnoff 		}
41423b3a8eb9SGleb Smirnoff 		;
41433b3a8eb9SGleb Smirnoff 
41443b3a8eb9SGleb Smirnoff icmp6_list	: icmp6_item optnl		{ $$ = $1; }
41453b3a8eb9SGleb Smirnoff 		| icmp6_list comma icmp6_item optnl {
41463b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
41473b3a8eb9SGleb Smirnoff 			$1->tail = $3;
41483b3a8eb9SGleb Smirnoff 			$$ = $1;
41493b3a8eb9SGleb Smirnoff 		}
41503b3a8eb9SGleb Smirnoff 		;
41513b3a8eb9SGleb Smirnoff 
41523b3a8eb9SGleb Smirnoff icmp_item	: icmptype		{
41533b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_icmp));
41543b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
41553b3a8eb9SGleb Smirnoff 				err(1, "icmp_item: calloc");
41563b3a8eb9SGleb Smirnoff 			$$->type = $1;
41573b3a8eb9SGleb Smirnoff 			$$->code = 0;
41583b3a8eb9SGleb Smirnoff 			$$->proto = IPPROTO_ICMP;
41593b3a8eb9SGleb Smirnoff 			$$->next = NULL;
41603b3a8eb9SGleb Smirnoff 			$$->tail = $$;
41613b3a8eb9SGleb Smirnoff 		}
41623b3a8eb9SGleb Smirnoff 		| icmptype CODE STRING	{
41633b3a8eb9SGleb Smirnoff 			const struct icmpcodeent	*p;
41643b3a8eb9SGleb Smirnoff 
41653b3a8eb9SGleb Smirnoff 			if ((p = geticmpcodebyname($1-1, $3, AF_INET)) == NULL) {
41663b3a8eb9SGleb Smirnoff 				yyerror("unknown icmp-code %s", $3);
41673b3a8eb9SGleb Smirnoff 				free($3);
41683b3a8eb9SGleb Smirnoff 				YYERROR;
41693b3a8eb9SGleb Smirnoff 			}
41703b3a8eb9SGleb Smirnoff 
41713b3a8eb9SGleb Smirnoff 			free($3);
41723b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_icmp));
41733b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
41743b3a8eb9SGleb Smirnoff 				err(1, "icmp_item: calloc");
41753b3a8eb9SGleb Smirnoff 			$$->type = $1;
41763b3a8eb9SGleb Smirnoff 			$$->code = p->code + 1;
41773b3a8eb9SGleb Smirnoff 			$$->proto = IPPROTO_ICMP;
41783b3a8eb9SGleb Smirnoff 			$$->next = NULL;
41793b3a8eb9SGleb Smirnoff 			$$->tail = $$;
41803b3a8eb9SGleb Smirnoff 		}
41813b3a8eb9SGleb Smirnoff 		| icmptype CODE NUMBER	{
41823b3a8eb9SGleb Smirnoff 			if ($3 < 0 || $3 > 255) {
41833b3a8eb9SGleb Smirnoff 				yyerror("illegal icmp-code %lu", $3);
41843b3a8eb9SGleb Smirnoff 				YYERROR;
41853b3a8eb9SGleb Smirnoff 			}
41863b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_icmp));
41873b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
41883b3a8eb9SGleb Smirnoff 				err(1, "icmp_item: calloc");
41893b3a8eb9SGleb Smirnoff 			$$->type = $1;
41903b3a8eb9SGleb Smirnoff 			$$->code = $3 + 1;
41913b3a8eb9SGleb Smirnoff 			$$->proto = IPPROTO_ICMP;
41923b3a8eb9SGleb Smirnoff 			$$->next = NULL;
41933b3a8eb9SGleb Smirnoff 			$$->tail = $$;
41943b3a8eb9SGleb Smirnoff 		}
41953b3a8eb9SGleb Smirnoff 		;
41963b3a8eb9SGleb Smirnoff 
41973b3a8eb9SGleb Smirnoff icmp6_item	: icmp6type		{
41983b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_icmp));
41993b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
42003b3a8eb9SGleb Smirnoff 				err(1, "icmp_item: calloc");
42013b3a8eb9SGleb Smirnoff 			$$->type = $1;
42023b3a8eb9SGleb Smirnoff 			$$->code = 0;
42033b3a8eb9SGleb Smirnoff 			$$->proto = IPPROTO_ICMPV6;
42043b3a8eb9SGleb Smirnoff 			$$->next = NULL;
42053b3a8eb9SGleb Smirnoff 			$$->tail = $$;
42063b3a8eb9SGleb Smirnoff 		}
42073b3a8eb9SGleb Smirnoff 		| icmp6type CODE STRING	{
42083b3a8eb9SGleb Smirnoff 			const struct icmpcodeent	*p;
42093b3a8eb9SGleb Smirnoff 
42103b3a8eb9SGleb Smirnoff 			if ((p = geticmpcodebyname($1-1, $3, AF_INET6)) == NULL) {
42113b3a8eb9SGleb Smirnoff 				yyerror("unknown icmp6-code %s", $3);
42123b3a8eb9SGleb Smirnoff 				free($3);
42133b3a8eb9SGleb Smirnoff 				YYERROR;
42143b3a8eb9SGleb Smirnoff 			}
42153b3a8eb9SGleb Smirnoff 			free($3);
42163b3a8eb9SGleb Smirnoff 
42173b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_icmp));
42183b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
42193b3a8eb9SGleb Smirnoff 				err(1, "icmp_item: calloc");
42203b3a8eb9SGleb Smirnoff 			$$->type = $1;
42213b3a8eb9SGleb Smirnoff 			$$->code = p->code + 1;
42223b3a8eb9SGleb Smirnoff 			$$->proto = IPPROTO_ICMPV6;
42233b3a8eb9SGleb Smirnoff 			$$->next = NULL;
42243b3a8eb9SGleb Smirnoff 			$$->tail = $$;
42253b3a8eb9SGleb Smirnoff 		}
42263b3a8eb9SGleb Smirnoff 		| icmp6type CODE NUMBER	{
42273b3a8eb9SGleb Smirnoff 			if ($3 < 0 || $3 > 255) {
42283b3a8eb9SGleb Smirnoff 				yyerror("illegal icmp-code %lu", $3);
42293b3a8eb9SGleb Smirnoff 				YYERROR;
42303b3a8eb9SGleb Smirnoff 			}
42313b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_icmp));
42323b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
42333b3a8eb9SGleb Smirnoff 				err(1, "icmp_item: calloc");
42343b3a8eb9SGleb Smirnoff 			$$->type = $1;
42353b3a8eb9SGleb Smirnoff 			$$->code = $3 + 1;
42363b3a8eb9SGleb Smirnoff 			$$->proto = IPPROTO_ICMPV6;
42373b3a8eb9SGleb Smirnoff 			$$->next = NULL;
42383b3a8eb9SGleb Smirnoff 			$$->tail = $$;
42393b3a8eb9SGleb Smirnoff 		}
42403b3a8eb9SGleb Smirnoff 		;
42413b3a8eb9SGleb Smirnoff 
42423b3a8eb9SGleb Smirnoff icmptype	: STRING			{
42433b3a8eb9SGleb Smirnoff 			const struct icmptypeent	*p;
42443b3a8eb9SGleb Smirnoff 
42453b3a8eb9SGleb Smirnoff 			if ((p = geticmptypebyname($1, AF_INET)) == NULL) {
42463b3a8eb9SGleb Smirnoff 				yyerror("unknown icmp-type %s", $1);
42473b3a8eb9SGleb Smirnoff 				free($1);
42483b3a8eb9SGleb Smirnoff 				YYERROR;
42493b3a8eb9SGleb Smirnoff 			}
42503b3a8eb9SGleb Smirnoff 			$$ = p->type + 1;
42513b3a8eb9SGleb Smirnoff 			free($1);
42523b3a8eb9SGleb Smirnoff 		}
42533b3a8eb9SGleb Smirnoff 		| NUMBER			{
42543b3a8eb9SGleb Smirnoff 			if ($1 < 0 || $1 > 255) {
42553b3a8eb9SGleb Smirnoff 				yyerror("illegal icmp-type %lu", $1);
42563b3a8eb9SGleb Smirnoff 				YYERROR;
42573b3a8eb9SGleb Smirnoff 			}
42583b3a8eb9SGleb Smirnoff 			$$ = $1 + 1;
42593b3a8eb9SGleb Smirnoff 		}
42603b3a8eb9SGleb Smirnoff 		;
42613b3a8eb9SGleb Smirnoff 
42623b3a8eb9SGleb Smirnoff icmp6type	: STRING			{
42633b3a8eb9SGleb Smirnoff 			const struct icmptypeent	*p;
42643b3a8eb9SGleb Smirnoff 
42653b3a8eb9SGleb Smirnoff 			if ((p = geticmptypebyname($1, AF_INET6)) ==
42663b3a8eb9SGleb Smirnoff 			    NULL) {
42673b3a8eb9SGleb Smirnoff 				yyerror("unknown icmp6-type %s", $1);
42683b3a8eb9SGleb Smirnoff 				free($1);
42693b3a8eb9SGleb Smirnoff 				YYERROR;
42703b3a8eb9SGleb Smirnoff 			}
42713b3a8eb9SGleb Smirnoff 			$$ = p->type + 1;
42723b3a8eb9SGleb Smirnoff 			free($1);
42733b3a8eb9SGleb Smirnoff 		}
42743b3a8eb9SGleb Smirnoff 		| NUMBER			{
42753b3a8eb9SGleb Smirnoff 			if ($1 < 0 || $1 > 255) {
42763b3a8eb9SGleb Smirnoff 				yyerror("illegal icmp6-type %lu", $1);
42773b3a8eb9SGleb Smirnoff 				YYERROR;
42783b3a8eb9SGleb Smirnoff 			}
42793b3a8eb9SGleb Smirnoff 			$$ = $1 + 1;
42803b3a8eb9SGleb Smirnoff 		}
42813b3a8eb9SGleb Smirnoff 		;
42823b3a8eb9SGleb Smirnoff 
42833b3a8eb9SGleb Smirnoff tos	: STRING			{
42841f495578SKristof Provost 			int val;
42851f495578SKristof Provost 			char *end;
42861f495578SKristof Provost 
42871f495578SKristof Provost 			if (map_tos($1, &val))
42881f495578SKristof Provost 				$$ = val;
42891f495578SKristof Provost 			else if ($1[0] == '0' && $1[1] == 'x') {
42901f495578SKristof Provost 				errno = 0;
42911f495578SKristof Provost 				$$ = strtoul($1, &end, 16);
42921f495578SKristof Provost 				if (errno || *end != '\0')
42931f495578SKristof Provost 					$$ = 256;
42941f495578SKristof Provost 			} else
42950cd7a91aSKristof Provost 				$$ = 256;		/* flag bad argument */
42960cd7a91aSKristof Provost 			if ($$ < 0 || $$ > 255) {
42973b3a8eb9SGleb Smirnoff 				yyerror("illegal tos value %s", $1);
42983b3a8eb9SGleb Smirnoff 				free($1);
42993b3a8eb9SGleb Smirnoff 				YYERROR;
43003b3a8eb9SGleb Smirnoff 			}
43013b3a8eb9SGleb Smirnoff 			free($1);
43023b3a8eb9SGleb Smirnoff 		}
43033b3a8eb9SGleb Smirnoff 		| NUMBER			{
43043b3a8eb9SGleb Smirnoff 			$$ = $1;
43050cd7a91aSKristof Provost 			if ($$ < 0 || $$ > 255) {
43066562157dSKristof Provost 				yyerror("illegal tos value %lu", $1);
43073b3a8eb9SGleb Smirnoff 				YYERROR;
43083b3a8eb9SGleb Smirnoff 			}
43093b3a8eb9SGleb Smirnoff 		}
43103b3a8eb9SGleb Smirnoff 		;
43113b3a8eb9SGleb Smirnoff 
43123b3a8eb9SGleb Smirnoff sourcetrack	: SOURCETRACK		{ $$ = PF_SRCTRACK; }
43133b3a8eb9SGleb Smirnoff 		| SOURCETRACK GLOBAL	{ $$ = PF_SRCTRACK_GLOBAL; }
43143b3a8eb9SGleb Smirnoff 		| SOURCETRACK RULE	{ $$ = PF_SRCTRACK_RULE; }
43153b3a8eb9SGleb Smirnoff 		;
43163b3a8eb9SGleb Smirnoff 
43173b3a8eb9SGleb Smirnoff statelock	: IFBOUND {
43183b3a8eb9SGleb Smirnoff 			$$ = PFRULE_IFBOUND;
43193b3a8eb9SGleb Smirnoff 		}
43203b3a8eb9SGleb Smirnoff 		| FLOATING {
43213b3a8eb9SGleb Smirnoff 			$$ = 0;
43223b3a8eb9SGleb Smirnoff 		}
43233b3a8eb9SGleb Smirnoff 		;
43243b3a8eb9SGleb Smirnoff 
43253b3a8eb9SGleb Smirnoff keep		: NO STATE			{
43263b3a8eb9SGleb Smirnoff 			$$.action = 0;
43273b3a8eb9SGleb Smirnoff 			$$.options = NULL;
43283b3a8eb9SGleb Smirnoff 		}
43293b3a8eb9SGleb Smirnoff 		| KEEP STATE state_opt_spec	{
43303b3a8eb9SGleb Smirnoff 			$$.action = PF_STATE_NORMAL;
43313b3a8eb9SGleb Smirnoff 			$$.options = $3;
43323b3a8eb9SGleb Smirnoff 		}
43333b3a8eb9SGleb Smirnoff 		| MODULATE STATE state_opt_spec {
43343b3a8eb9SGleb Smirnoff 			$$.action = PF_STATE_MODULATE;
43353b3a8eb9SGleb Smirnoff 			$$.options = $3;
43363b3a8eb9SGleb Smirnoff 		}
43373b3a8eb9SGleb Smirnoff 		| SYNPROXY STATE state_opt_spec {
43383b3a8eb9SGleb Smirnoff 			$$.action = PF_STATE_SYNPROXY;
43393b3a8eb9SGleb Smirnoff 			$$.options = $3;
43403b3a8eb9SGleb Smirnoff 		}
43413b3a8eb9SGleb Smirnoff 		;
43423b3a8eb9SGleb Smirnoff 
43433b3a8eb9SGleb Smirnoff flush		: /* empty */			{ $$ = 0; }
43443b3a8eb9SGleb Smirnoff 		| FLUSH				{ $$ = PF_FLUSH; }
43453b3a8eb9SGleb Smirnoff 		| FLUSH GLOBAL			{
43463b3a8eb9SGleb Smirnoff 			$$ = PF_FLUSH | PF_FLUSH_GLOBAL;
43473b3a8eb9SGleb Smirnoff 		}
43483b3a8eb9SGleb Smirnoff 		;
43493b3a8eb9SGleb Smirnoff 
43503b3a8eb9SGleb Smirnoff state_opt_spec	: '(' state_opt_list ')'	{ $$ = $2; }
43513b3a8eb9SGleb Smirnoff 		| /* empty */			{ $$ = NULL; }
43523b3a8eb9SGleb Smirnoff 		;
43533b3a8eb9SGleb Smirnoff 
43543b3a8eb9SGleb Smirnoff state_opt_list	: state_opt_item		{ $$ = $1; }
43553b3a8eb9SGleb Smirnoff 		| state_opt_list comma state_opt_item {
43563b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
43573b3a8eb9SGleb Smirnoff 			$1->tail = $3;
43583b3a8eb9SGleb Smirnoff 			$$ = $1;
43593b3a8eb9SGleb Smirnoff 		}
43603b3a8eb9SGleb Smirnoff 		;
43613b3a8eb9SGleb Smirnoff 
43623b3a8eb9SGleb Smirnoff state_opt_item	: MAXIMUM NUMBER		{
43633b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > UINT_MAX) {
43643b3a8eb9SGleb Smirnoff 				yyerror("only positive values permitted");
43653b3a8eb9SGleb Smirnoff 				YYERROR;
43663b3a8eb9SGleb Smirnoff 			}
43673b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
43683b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
43693b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
43703b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_MAX;
43713b3a8eb9SGleb Smirnoff 			$$->data.max_states = $2;
43723b3a8eb9SGleb Smirnoff 			$$->next = NULL;
43733b3a8eb9SGleb Smirnoff 			$$->tail = $$;
43743b3a8eb9SGleb Smirnoff 		}
43753b3a8eb9SGleb Smirnoff 		| NOSYNC				{
43763b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
43773b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
43783b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
43793b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_NOSYNC;
43803b3a8eb9SGleb Smirnoff 			$$->next = NULL;
43813b3a8eb9SGleb Smirnoff 			$$->tail = $$;
43823b3a8eb9SGleb Smirnoff 		}
43833b3a8eb9SGleb Smirnoff 		| MAXSRCSTATES NUMBER			{
43843b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > UINT_MAX) {
43853b3a8eb9SGleb Smirnoff 				yyerror("only positive values permitted");
43863b3a8eb9SGleb Smirnoff 				YYERROR;
43873b3a8eb9SGleb Smirnoff 			}
43883b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
43893b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
43903b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
43913b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_MAX_SRC_STATES;
43923b3a8eb9SGleb Smirnoff 			$$->data.max_src_states = $2;
43933b3a8eb9SGleb Smirnoff 			$$->next = NULL;
43943b3a8eb9SGleb Smirnoff 			$$->tail = $$;
43953b3a8eb9SGleb Smirnoff 		}
43963b3a8eb9SGleb Smirnoff 		| MAXSRCCONN NUMBER			{
43973b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > UINT_MAX) {
43983b3a8eb9SGleb Smirnoff 				yyerror("only positive values permitted");
43993b3a8eb9SGleb Smirnoff 				YYERROR;
44003b3a8eb9SGleb Smirnoff 			}
44013b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
44023b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
44033b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
44043b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_MAX_SRC_CONN;
44053b3a8eb9SGleb Smirnoff 			$$->data.max_src_conn = $2;
44063b3a8eb9SGleb Smirnoff 			$$->next = NULL;
44073b3a8eb9SGleb Smirnoff 			$$->tail = $$;
44083b3a8eb9SGleb Smirnoff 		}
44093b3a8eb9SGleb Smirnoff 		| MAXSRCCONNRATE NUMBER '/' NUMBER	{
44103b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > UINT_MAX ||
44113b3a8eb9SGleb Smirnoff 			    $4 < 0 || $4 > UINT_MAX) {
44123b3a8eb9SGleb Smirnoff 				yyerror("only positive values permitted");
44133b3a8eb9SGleb Smirnoff 				YYERROR;
44143b3a8eb9SGleb Smirnoff 			}
44153b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
44163b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
44173b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
44183b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_MAX_SRC_CONN_RATE;
44193b3a8eb9SGleb Smirnoff 			$$->data.max_src_conn_rate.limit = $2;
44203b3a8eb9SGleb Smirnoff 			$$->data.max_src_conn_rate.seconds = $4;
44213b3a8eb9SGleb Smirnoff 			$$->next = NULL;
44223b3a8eb9SGleb Smirnoff 			$$->tail = $$;
44233b3a8eb9SGleb Smirnoff 		}
44243b3a8eb9SGleb Smirnoff 		| OVERLOAD '<' STRING '>' flush		{
44253b3a8eb9SGleb Smirnoff 			if (strlen($3) >= PF_TABLE_NAME_SIZE) {
44263b3a8eb9SGleb Smirnoff 				yyerror("table name '%s' too long", $3);
44273b3a8eb9SGleb Smirnoff 				free($3);
44283b3a8eb9SGleb Smirnoff 				YYERROR;
44293b3a8eb9SGleb Smirnoff 			}
44303b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
44313b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
44323b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
44333b3a8eb9SGleb Smirnoff 			if (strlcpy($$->data.overload.tblname, $3,
44343b3a8eb9SGleb Smirnoff 			    PF_TABLE_NAME_SIZE) >= PF_TABLE_NAME_SIZE)
44353b3a8eb9SGleb Smirnoff 				errx(1, "state_opt_item: strlcpy");
44363b3a8eb9SGleb Smirnoff 			free($3);
44373b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_OVERLOAD;
44383b3a8eb9SGleb Smirnoff 			$$->data.overload.flush = $5;
44393b3a8eb9SGleb Smirnoff 			$$->next = NULL;
44403b3a8eb9SGleb Smirnoff 			$$->tail = $$;
44413b3a8eb9SGleb Smirnoff 		}
44423b3a8eb9SGleb Smirnoff 		| MAXSRCNODES NUMBER			{
44433b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > UINT_MAX) {
44443b3a8eb9SGleb Smirnoff 				yyerror("only positive values permitted");
44453b3a8eb9SGleb Smirnoff 				YYERROR;
44463b3a8eb9SGleb Smirnoff 			}
44473b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
44483b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
44493b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
44503b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_MAX_SRC_NODES;
44513b3a8eb9SGleb Smirnoff 			$$->data.max_src_nodes = $2;
44523b3a8eb9SGleb Smirnoff 			$$->next = NULL;
44533b3a8eb9SGleb Smirnoff 			$$->tail = $$;
44543b3a8eb9SGleb Smirnoff 		}
44553b3a8eb9SGleb Smirnoff 		| sourcetrack {
44563b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
44573b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
44583b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
44593b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_SRCTRACK;
44603b3a8eb9SGleb Smirnoff 			$$->data.src_track = $1;
44613b3a8eb9SGleb Smirnoff 			$$->next = NULL;
44623b3a8eb9SGleb Smirnoff 			$$->tail = $$;
44633b3a8eb9SGleb Smirnoff 		}
44643b3a8eb9SGleb Smirnoff 		| statelock {
44653b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
44663b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
44673b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
44683b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_STATELOCK;
44693b3a8eb9SGleb Smirnoff 			$$->data.statelock = $1;
44703b3a8eb9SGleb Smirnoff 			$$->next = NULL;
44713b3a8eb9SGleb Smirnoff 			$$->tail = $$;
44723b3a8eb9SGleb Smirnoff 		}
44733b3a8eb9SGleb Smirnoff 		| SLOPPY {
44743b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
44753b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
44763b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
44773b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_SLOPPY;
44783b3a8eb9SGleb Smirnoff 			$$->next = NULL;
44793b3a8eb9SGleb Smirnoff 			$$->tail = $$;
44803b3a8eb9SGleb Smirnoff 		}
4481baf9b6d0SKristof Provost 		| PFLOW {
4482baf9b6d0SKristof Provost 			$$ = calloc(1, sizeof(struct node_state_opt));
4483baf9b6d0SKristof Provost 			if ($$ == NULL)
4484baf9b6d0SKristof Provost 				err(1, "state_opt_item: calloc");
4485baf9b6d0SKristof Provost 			$$->type = PF_STATE_OPT_PFLOW;
4486baf9b6d0SKristof Provost 			$$->next = NULL;
4487baf9b6d0SKristof Provost 			$$->tail = $$;
4488baf9b6d0SKristof Provost 		}
44893b3a8eb9SGleb Smirnoff 		| STRING NUMBER			{
44903b3a8eb9SGleb Smirnoff 			int	i;
44913b3a8eb9SGleb Smirnoff 
44923b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > UINT_MAX) {
44933b3a8eb9SGleb Smirnoff 				yyerror("only positive values permitted");
44943b3a8eb9SGleb Smirnoff 				YYERROR;
44953b3a8eb9SGleb Smirnoff 			}
44963b3a8eb9SGleb Smirnoff 			for (i = 0; pf_timeouts[i].name &&
44973b3a8eb9SGleb Smirnoff 			    strcmp(pf_timeouts[i].name, $1); ++i)
44983b3a8eb9SGleb Smirnoff 				;	/* nothing */
44993b3a8eb9SGleb Smirnoff 			if (!pf_timeouts[i].name) {
45003b3a8eb9SGleb Smirnoff 				yyerror("illegal timeout name %s", $1);
45013b3a8eb9SGleb Smirnoff 				free($1);
45023b3a8eb9SGleb Smirnoff 				YYERROR;
45033b3a8eb9SGleb Smirnoff 			}
45043b3a8eb9SGleb Smirnoff 			if (strchr(pf_timeouts[i].name, '.') == NULL) {
45053b3a8eb9SGleb Smirnoff 				yyerror("illegal state timeout %s", $1);
45063b3a8eb9SGleb Smirnoff 				free($1);
45073b3a8eb9SGleb Smirnoff 				YYERROR;
45083b3a8eb9SGleb Smirnoff 			}
45093b3a8eb9SGleb Smirnoff 			free($1);
45103b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
45113b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
45123b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
45133b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_TIMEOUT;
45143b3a8eb9SGleb Smirnoff 			$$->data.timeout.number = pf_timeouts[i].timeout;
45153b3a8eb9SGleb Smirnoff 			$$->data.timeout.seconds = $2;
45163b3a8eb9SGleb Smirnoff 			$$->next = NULL;
45173b3a8eb9SGleb Smirnoff 			$$->tail = $$;
45183b3a8eb9SGleb Smirnoff 		}
45193b3a8eb9SGleb Smirnoff 		;
45203b3a8eb9SGleb Smirnoff 
45213b3a8eb9SGleb Smirnoff label		: LABEL STRING			{
45223b3a8eb9SGleb Smirnoff 			$$ = $2;
45233b3a8eb9SGleb Smirnoff 		}
45243b3a8eb9SGleb Smirnoff 		;
45253b3a8eb9SGleb Smirnoff 
45262b29ceb8SKristof Provost etherqname	: QUEUE STRING				{
45272b29ceb8SKristof Provost 			$$.qname = $2;
45282b29ceb8SKristof Provost 		}
45292b29ceb8SKristof Provost 		| QUEUE '(' STRING ')'			{
45302b29ceb8SKristof Provost 			$$.qname = $3;
45312b29ceb8SKristof Provost 		}
45322b29ceb8SKristof Provost 		;
45332b29ceb8SKristof Provost 
45343b3a8eb9SGleb Smirnoff qname		: QUEUE STRING				{
45353b3a8eb9SGleb Smirnoff 			$$.qname = $2;
45363b3a8eb9SGleb Smirnoff 			$$.pqname = NULL;
45373b3a8eb9SGleb Smirnoff 		}
45383b3a8eb9SGleb Smirnoff 		| QUEUE '(' STRING ')'			{
45393b3a8eb9SGleb Smirnoff 			$$.qname = $3;
45403b3a8eb9SGleb Smirnoff 			$$.pqname = NULL;
45413b3a8eb9SGleb Smirnoff 		}
45423b3a8eb9SGleb Smirnoff 		| QUEUE '(' STRING comma STRING ')'	{
45433b3a8eb9SGleb Smirnoff 			$$.qname = $3;
45443b3a8eb9SGleb Smirnoff 			$$.pqname = $5;
45453b3a8eb9SGleb Smirnoff 		}
45463b3a8eb9SGleb Smirnoff 		;
45473b3a8eb9SGleb Smirnoff 
45483b3a8eb9SGleb Smirnoff no		: /* empty */			{ $$ = 0; }
45493b3a8eb9SGleb Smirnoff 		| NO				{ $$ = 1; }
45503b3a8eb9SGleb Smirnoff 		;
45513b3a8eb9SGleb Smirnoff 
45523b3a8eb9SGleb Smirnoff portstar	: numberstring			{
45533b3a8eb9SGleb Smirnoff 			if (parseport($1, &$$, PPORT_RANGE|PPORT_STAR) == -1) {
45543b3a8eb9SGleb Smirnoff 				free($1);
45553b3a8eb9SGleb Smirnoff 				YYERROR;
45563b3a8eb9SGleb Smirnoff 			}
45573b3a8eb9SGleb Smirnoff 			free($1);
45583b3a8eb9SGleb Smirnoff 		}
45593b3a8eb9SGleb Smirnoff 		;
45603b3a8eb9SGleb Smirnoff 
45613b3a8eb9SGleb Smirnoff redirspec	: host				{ $$ = $1; }
45623b3a8eb9SGleb Smirnoff 		| '{' optnl redir_host_list '}'	{ $$ = $3; }
45633b3a8eb9SGleb Smirnoff 		;
45643b3a8eb9SGleb Smirnoff 
45653b3a8eb9SGleb Smirnoff redir_host_list	: host optnl			{ $$ = $1; }
45663b3a8eb9SGleb Smirnoff 		| redir_host_list comma host optnl {
45673b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
45683b3a8eb9SGleb Smirnoff 			$1->tail = $3->tail;
45693b3a8eb9SGleb Smirnoff 			$$ = $1;
45703b3a8eb9SGleb Smirnoff 		}
45713b3a8eb9SGleb Smirnoff 		;
45723b3a8eb9SGleb Smirnoff 
45733b3a8eb9SGleb Smirnoff redirpool	: /* empty */			{ $$ = NULL; }
45743b3a8eb9SGleb Smirnoff 		| ARROW redirspec		{
45753b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct redirection));
45763b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
45773b3a8eb9SGleb Smirnoff 				err(1, "redirection: calloc");
45783b3a8eb9SGleb Smirnoff 			$$->host = $2;
45793b3a8eb9SGleb Smirnoff 			$$->rport.a = $$->rport.b = $$->rport.t = 0;
45803b3a8eb9SGleb Smirnoff 		}
45813b3a8eb9SGleb Smirnoff 		| ARROW redirspec PORT portstar	{
45823b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct redirection));
45833b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
45843b3a8eb9SGleb Smirnoff 				err(1, "redirection: calloc");
45853b3a8eb9SGleb Smirnoff 			$$->host = $2;
45863b3a8eb9SGleb Smirnoff 			$$->rport = $4;
45873b3a8eb9SGleb Smirnoff 		}
45883b3a8eb9SGleb Smirnoff 		;
45893b3a8eb9SGleb Smirnoff 
45903b3a8eb9SGleb Smirnoff hashkey		: /* empty */
45913b3a8eb9SGleb Smirnoff 		{
45923b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct pf_poolhashkey));
45933b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
45943b3a8eb9SGleb Smirnoff 				err(1, "hashkey: calloc");
45953b3a8eb9SGleb Smirnoff 			$$->key32[0] = arc4random();
45963b3a8eb9SGleb Smirnoff 			$$->key32[1] = arc4random();
45973b3a8eb9SGleb Smirnoff 			$$->key32[2] = arc4random();
45983b3a8eb9SGleb Smirnoff 			$$->key32[3] = arc4random();
45993b3a8eb9SGleb Smirnoff 		}
46003b3a8eb9SGleb Smirnoff 		| string
46013b3a8eb9SGleb Smirnoff 		{
46023b3a8eb9SGleb Smirnoff 			if (!strncmp($1, "0x", 2)) {
46033b3a8eb9SGleb Smirnoff 				if (strlen($1) != 34) {
46043b3a8eb9SGleb Smirnoff 					free($1);
46053b3a8eb9SGleb Smirnoff 					yyerror("hex key must be 128 bits "
46063b3a8eb9SGleb Smirnoff 						"(32 hex digits) long");
46073b3a8eb9SGleb Smirnoff 					YYERROR;
46083b3a8eb9SGleb Smirnoff 				}
46093b3a8eb9SGleb Smirnoff 				$$ = calloc(1, sizeof(struct pf_poolhashkey));
46103b3a8eb9SGleb Smirnoff 				if ($$ == NULL)
46113b3a8eb9SGleb Smirnoff 					err(1, "hashkey: calloc");
46123b3a8eb9SGleb Smirnoff 
46133b3a8eb9SGleb Smirnoff 				if (sscanf($1, "0x%8x%8x%8x%8x",
46143b3a8eb9SGleb Smirnoff 				    &$$->key32[0], &$$->key32[1],
46153b3a8eb9SGleb Smirnoff 				    &$$->key32[2], &$$->key32[3]) != 4) {
46163b3a8eb9SGleb Smirnoff 					free($$);
46173b3a8eb9SGleb Smirnoff 					free($1);
46183b3a8eb9SGleb Smirnoff 					yyerror("invalid hex key");
46193b3a8eb9SGleb Smirnoff 					YYERROR;
46203b3a8eb9SGleb Smirnoff 				}
46213b3a8eb9SGleb Smirnoff 			} else {
46223b3a8eb9SGleb Smirnoff 				MD5_CTX	context;
46233b3a8eb9SGleb Smirnoff 
46243b3a8eb9SGleb Smirnoff 				$$ = calloc(1, sizeof(struct pf_poolhashkey));
46253b3a8eb9SGleb Smirnoff 				if ($$ == NULL)
46263b3a8eb9SGleb Smirnoff 					err(1, "hashkey: calloc");
46273b3a8eb9SGleb Smirnoff 				MD5Init(&context);
46283b3a8eb9SGleb Smirnoff 				MD5Update(&context, (unsigned char *)$1,
46293b3a8eb9SGleb Smirnoff 				    strlen($1));
46303b3a8eb9SGleb Smirnoff 				MD5Final((unsigned char *)$$, &context);
46313b3a8eb9SGleb Smirnoff 				HTONL($$->key32[0]);
46323b3a8eb9SGleb Smirnoff 				HTONL($$->key32[1]);
46333b3a8eb9SGleb Smirnoff 				HTONL($$->key32[2]);
46343b3a8eb9SGleb Smirnoff 				HTONL($$->key32[3]);
46353b3a8eb9SGleb Smirnoff 			}
46363b3a8eb9SGleb Smirnoff 			free($1);
46373b3a8eb9SGleb Smirnoff 		}
46383b3a8eb9SGleb Smirnoff 		;
46393b3a8eb9SGleb Smirnoff 
46403b3a8eb9SGleb Smirnoff pool_opts	:	{ bzero(&pool_opts, sizeof pool_opts); }
46413b3a8eb9SGleb Smirnoff 		    pool_opts_l
46423b3a8eb9SGleb Smirnoff 			{ $$ = pool_opts; }
46433b3a8eb9SGleb Smirnoff 		| /* empty */	{
46443b3a8eb9SGleb Smirnoff 			bzero(&pool_opts, sizeof pool_opts);
46453b3a8eb9SGleb Smirnoff 			$$ = pool_opts;
46463b3a8eb9SGleb Smirnoff 		}
46473b3a8eb9SGleb Smirnoff 		;
46483b3a8eb9SGleb Smirnoff 
46493b3a8eb9SGleb Smirnoff pool_opts_l	: pool_opts_l pool_opt
46503b3a8eb9SGleb Smirnoff 		| pool_opt
46513b3a8eb9SGleb Smirnoff 		;
46523b3a8eb9SGleb Smirnoff 
46533b3a8eb9SGleb Smirnoff pool_opt	: BITMASK	{
46543b3a8eb9SGleb Smirnoff 			if (pool_opts.type) {
46553b3a8eb9SGleb Smirnoff 				yyerror("pool type cannot be redefined");
46563b3a8eb9SGleb Smirnoff 				YYERROR;
46573b3a8eb9SGleb Smirnoff 			}
46583b3a8eb9SGleb Smirnoff 			pool_opts.type =  PF_POOL_BITMASK;
46593b3a8eb9SGleb Smirnoff 		}
46603b3a8eb9SGleb Smirnoff 		| RANDOM	{
46613b3a8eb9SGleb Smirnoff 			if (pool_opts.type) {
46623b3a8eb9SGleb Smirnoff 				yyerror("pool type cannot be redefined");
46633b3a8eb9SGleb Smirnoff 				YYERROR;
46643b3a8eb9SGleb Smirnoff 			}
46653b3a8eb9SGleb Smirnoff 			pool_opts.type = PF_POOL_RANDOM;
46663b3a8eb9SGleb Smirnoff 		}
46673b3a8eb9SGleb Smirnoff 		| SOURCEHASH hashkey {
46683b3a8eb9SGleb Smirnoff 			if (pool_opts.type) {
46693b3a8eb9SGleb Smirnoff 				yyerror("pool type cannot be redefined");
46703b3a8eb9SGleb Smirnoff 				YYERROR;
46713b3a8eb9SGleb Smirnoff 			}
46723b3a8eb9SGleb Smirnoff 			pool_opts.type = PF_POOL_SRCHASH;
46733b3a8eb9SGleb Smirnoff 			pool_opts.key = $2;
46743b3a8eb9SGleb Smirnoff 		}
46753b3a8eb9SGleb Smirnoff 		| ROUNDROBIN	{
46763b3a8eb9SGleb Smirnoff 			if (pool_opts.type) {
46773b3a8eb9SGleb Smirnoff 				yyerror("pool type cannot be redefined");
46783b3a8eb9SGleb Smirnoff 				YYERROR;
46793b3a8eb9SGleb Smirnoff 			}
46803b3a8eb9SGleb Smirnoff 			pool_opts.type = PF_POOL_ROUNDROBIN;
46813b3a8eb9SGleb Smirnoff 		}
46823b3a8eb9SGleb Smirnoff 		| STATICPORT	{
46833b3a8eb9SGleb Smirnoff 			if (pool_opts.staticport) {
46843b3a8eb9SGleb Smirnoff 				yyerror("static-port cannot be redefined");
46853b3a8eb9SGleb Smirnoff 				YYERROR;
46863b3a8eb9SGleb Smirnoff 			}
46873b3a8eb9SGleb Smirnoff 			pool_opts.staticport = 1;
46883b3a8eb9SGleb Smirnoff 		}
46893b3a8eb9SGleb Smirnoff 		| STICKYADDRESS	{
46901e73fbd8SFranco Fichtner 			if (pool_opts.marker & POM_STICKYADDRESS) {
46913b3a8eb9SGleb Smirnoff 				yyerror("sticky-address cannot be redefined");
46923b3a8eb9SGleb Smirnoff 				YYERROR;
46933b3a8eb9SGleb Smirnoff 			}
46943b3a8eb9SGleb Smirnoff 			pool_opts.marker |= POM_STICKYADDRESS;
46953b3a8eb9SGleb Smirnoff 			pool_opts.opts |= PF_POOL_STICKYADDR;
46963b3a8eb9SGleb Smirnoff 		}
4697390dc369STom Jones 		| ENDPI {
4698390dc369STom Jones 			if (pool_opts.marker & POM_ENDPI) {
4699390dc369STom Jones 				yyerror("endpoint-independent cannot be redefined");
4700390dc369STom Jones 				YYERROR;
4701390dc369STom Jones 			}
4702390dc369STom Jones 			pool_opts.marker |= POM_ENDPI;
4703390dc369STom Jones 			pool_opts.opts |= PF_POOL_ENDPI;
4704390dc369STom Jones 		}
47052aa21096SKurosawa Takahiro 		| MAPEPORTSET number '/' number '/' number {
47062aa21096SKurosawa Takahiro 			if (pool_opts.mape.offset) {
47072aa21096SKurosawa Takahiro 				yyerror("map-e-portset cannot be redefined");
47082aa21096SKurosawa Takahiro 				YYERROR;
47092aa21096SKurosawa Takahiro 			}
47102aa21096SKurosawa Takahiro 			if (pool_opts.type) {
47112aa21096SKurosawa Takahiro 				yyerror("map-e-portset cannot be used with "
47122aa21096SKurosawa Takahiro 					"address pools");
47132aa21096SKurosawa Takahiro 				YYERROR;
47142aa21096SKurosawa Takahiro 			}
47152aa21096SKurosawa Takahiro 			if ($2 <= 0 || $2 >= 16) {
47162aa21096SKurosawa Takahiro 				yyerror("MAP-E PSID offset must be 1-15");
47172aa21096SKurosawa Takahiro 				YYERROR;
47182aa21096SKurosawa Takahiro 			}
47192aa21096SKurosawa Takahiro 			if ($4 < 0 || $4 >= 16 || $2 + $4 > 16) {
47202aa21096SKurosawa Takahiro 				yyerror("Invalid MAP-E PSID length");
47212aa21096SKurosawa Takahiro 				YYERROR;
47222aa21096SKurosawa Takahiro 			} else if ($4 == 0) {
47232aa21096SKurosawa Takahiro 				yyerror("PSID Length = 0: this means"
47242aa21096SKurosawa Takahiro 				    " you do not need MAP-E");
47252aa21096SKurosawa Takahiro 				YYERROR;
47262aa21096SKurosawa Takahiro 			}
47272aa21096SKurosawa Takahiro 			if ($6 < 0 || $6 > 65535) {
47282aa21096SKurosawa Takahiro 				yyerror("Invalid MAP-E PSID");
47292aa21096SKurosawa Takahiro 				YYERROR;
47302aa21096SKurosawa Takahiro 			}
47312aa21096SKurosawa Takahiro 			pool_opts.mape.offset = $2;
47322aa21096SKurosawa Takahiro 			pool_opts.mape.psidlen = $4;
47332aa21096SKurosawa Takahiro 			pool_opts.mape.psid = $6;
47342aa21096SKurosawa Takahiro 		}
47353b3a8eb9SGleb Smirnoff 		;
47363b3a8eb9SGleb Smirnoff 
47373b3a8eb9SGleb Smirnoff redirection	: /* empty */			{ $$ = NULL; }
47383b3a8eb9SGleb Smirnoff 		| ARROW host			{
47393b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct redirection));
47403b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
47413b3a8eb9SGleb Smirnoff 				err(1, "redirection: calloc");
47423b3a8eb9SGleb Smirnoff 			$$->host = $2;
47433b3a8eb9SGleb Smirnoff 			$$->rport.a = $$->rport.b = $$->rport.t = 0;
47443b3a8eb9SGleb Smirnoff 		}
47453b3a8eb9SGleb Smirnoff 		| ARROW host PORT portstar	{
47463b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct redirection));
47473b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
47483b3a8eb9SGleb Smirnoff 				err(1, "redirection: calloc");
47493b3a8eb9SGleb Smirnoff 			$$->host = $2;
47503b3a8eb9SGleb Smirnoff 			$$->rport = $4;
47513b3a8eb9SGleb Smirnoff 		}
47523b3a8eb9SGleb Smirnoff 		;
47533b3a8eb9SGleb Smirnoff 
47543b3a8eb9SGleb Smirnoff natpasslog	: /* empty */	{ $$.b1 = $$.b2 = 0; $$.w2 = 0; }
47553b3a8eb9SGleb Smirnoff 		| PASS		{ $$.b1 = 1; $$.b2 = 0; $$.w2 = 0; }
47563b3a8eb9SGleb Smirnoff 		| PASS log	{ $$.b1 = 1; $$.b2 = $2.log; $$.w2 = $2.logif; }
47573b3a8eb9SGleb Smirnoff 		| log		{ $$.b1 = 0; $$.b2 = $1.log; $$.w2 = $1.logif; }
47583b3a8eb9SGleb Smirnoff 		;
47593b3a8eb9SGleb Smirnoff 
47603b3a8eb9SGleb Smirnoff nataction	: no NAT natpasslog {
47613b3a8eb9SGleb Smirnoff 			if ($1 && $3.b1) {
47623b3a8eb9SGleb Smirnoff 				yyerror("\"pass\" not valid with \"no\"");
47633b3a8eb9SGleb Smirnoff 				YYERROR;
47643b3a8eb9SGleb Smirnoff 			}
47653b3a8eb9SGleb Smirnoff 			if ($1)
47663b3a8eb9SGleb Smirnoff 				$$.b1 = PF_NONAT;
47673b3a8eb9SGleb Smirnoff 			else
47683b3a8eb9SGleb Smirnoff 				$$.b1 = PF_NAT;
47693b3a8eb9SGleb Smirnoff 			$$.b2 = $3.b1;
47703b3a8eb9SGleb Smirnoff 			$$.w = $3.b2;
47713b3a8eb9SGleb Smirnoff 			$$.w2 = $3.w2;
47723b3a8eb9SGleb Smirnoff 		}
47733b3a8eb9SGleb Smirnoff 		| no RDR natpasslog {
47743b3a8eb9SGleb Smirnoff 			if ($1 && $3.b1) {
47753b3a8eb9SGleb Smirnoff 				yyerror("\"pass\" not valid with \"no\"");
47763b3a8eb9SGleb Smirnoff 				YYERROR;
47773b3a8eb9SGleb Smirnoff 			}
47783b3a8eb9SGleb Smirnoff 			if ($1)
47793b3a8eb9SGleb Smirnoff 				$$.b1 = PF_NORDR;
47803b3a8eb9SGleb Smirnoff 			else
47813b3a8eb9SGleb Smirnoff 				$$.b1 = PF_RDR;
47823b3a8eb9SGleb Smirnoff 			$$.b2 = $3.b1;
47833b3a8eb9SGleb Smirnoff 			$$.w = $3.b2;
47843b3a8eb9SGleb Smirnoff 			$$.w2 = $3.w2;
47853b3a8eb9SGleb Smirnoff 		}
47863b3a8eb9SGleb Smirnoff 		;
47873b3a8eb9SGleb Smirnoff 
47883b3a8eb9SGleb Smirnoff natrule		: nataction interface af proto fromto tag tagged rtable
47893b3a8eb9SGleb Smirnoff 		    redirpool pool_opts
47903b3a8eb9SGleb Smirnoff 		{
4791e9eb0941SKristof Provost 			struct pfctl_rule	r;
4792fc6e5069SKristof Provost 			struct node_state_opt	*o;
47933b3a8eb9SGleb Smirnoff 
47943b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_NAT))
47953b3a8eb9SGleb Smirnoff 				YYERROR;
47963b3a8eb9SGleb Smirnoff 
47973b3a8eb9SGleb Smirnoff 			memset(&r, 0, sizeof(r));
47983b3a8eb9SGleb Smirnoff 
47993b3a8eb9SGleb Smirnoff 			r.action = $1.b1;
48003b3a8eb9SGleb Smirnoff 			r.natpass = $1.b2;
48013b3a8eb9SGleb Smirnoff 			r.log = $1.w;
48023b3a8eb9SGleb Smirnoff 			r.logif = $1.w2;
48033b3a8eb9SGleb Smirnoff 			r.af = $3;
48043b3a8eb9SGleb Smirnoff 
48053b3a8eb9SGleb Smirnoff 			if (!r.af) {
48063b3a8eb9SGleb Smirnoff 				if ($5.src.host && $5.src.host->af &&
48073b3a8eb9SGleb Smirnoff 				    !$5.src.host->ifindex)
48083b3a8eb9SGleb Smirnoff 					r.af = $5.src.host->af;
48093b3a8eb9SGleb Smirnoff 				else if ($5.dst.host && $5.dst.host->af &&
48103b3a8eb9SGleb Smirnoff 				    !$5.dst.host->ifindex)
48113b3a8eb9SGleb Smirnoff 					r.af = $5.dst.host->af;
48123b3a8eb9SGleb Smirnoff 			}
48133b3a8eb9SGleb Smirnoff 
48143b3a8eb9SGleb Smirnoff 			if ($6 != NULL)
48153b3a8eb9SGleb Smirnoff 				if (strlcpy(r.tagname, $6, PF_TAG_NAME_SIZE) >=
48163b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) {
48173b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
48183b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
48193b3a8eb9SGleb Smirnoff 					YYERROR;
48203b3a8eb9SGleb Smirnoff 				}
48213b3a8eb9SGleb Smirnoff 
48223b3a8eb9SGleb Smirnoff 			if ($7.name)
48233b3a8eb9SGleb Smirnoff 				if (strlcpy(r.match_tagname, $7.name,
48243b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
48253b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
48263b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
48273b3a8eb9SGleb Smirnoff 					YYERROR;
48283b3a8eb9SGleb Smirnoff 				}
48293b3a8eb9SGleb Smirnoff 			r.match_tag_not = $7.neg;
48303b3a8eb9SGleb Smirnoff 			r.rtableid = $8;
48313b3a8eb9SGleb Smirnoff 
48323b3a8eb9SGleb Smirnoff 			if (r.action == PF_NONAT || r.action == PF_NORDR) {
48333b3a8eb9SGleb Smirnoff 				if ($9 != NULL) {
48343b3a8eb9SGleb Smirnoff 					yyerror("translation rule with 'no' "
48353b3a8eb9SGleb Smirnoff 					    "does not need '->'");
48363b3a8eb9SGleb Smirnoff 					YYERROR;
48373b3a8eb9SGleb Smirnoff 				}
48383b3a8eb9SGleb Smirnoff 			} else {
48393b3a8eb9SGleb Smirnoff 				if ($9 == NULL || $9->host == NULL) {
48403b3a8eb9SGleb Smirnoff 					yyerror("translation rule requires '-> "
48413b3a8eb9SGleb Smirnoff 					    "address'");
48423b3a8eb9SGleb Smirnoff 					YYERROR;
48433b3a8eb9SGleb Smirnoff 				}
48443b3a8eb9SGleb Smirnoff 				if (!r.af && ! $9->host->ifindex)
48453b3a8eb9SGleb Smirnoff 					r.af = $9->host->af;
48463b3a8eb9SGleb Smirnoff 
48473b3a8eb9SGleb Smirnoff 				remove_invalid_hosts(&$9->host, &r.af);
48483b3a8eb9SGleb Smirnoff 				if (invalid_redirect($9->host, r.af))
48493b3a8eb9SGleb Smirnoff 					YYERROR;
48507ce98cf2SKristof Provost 				if ($9->host->addr.type == PF_ADDR_DYNIFTL) {
48517ce98cf2SKristof Provost 					if (($9->host = gen_dynnode($9->host, r.af)) == NULL)
48527ce98cf2SKristof Provost 						err(1, "calloc");
48537ce98cf2SKristof Provost 				}
48543b3a8eb9SGleb Smirnoff 				if (check_netmask($9->host, r.af))
48553b3a8eb9SGleb Smirnoff 					YYERROR;
48563b3a8eb9SGleb Smirnoff 
48573b3a8eb9SGleb Smirnoff 				r.rpool.proxy_port[0] = ntohs($9->rport.a);
48583b3a8eb9SGleb Smirnoff 
48593b3a8eb9SGleb Smirnoff 				switch (r.action) {
48603b3a8eb9SGleb Smirnoff 				case PF_RDR:
48613b3a8eb9SGleb Smirnoff 					if (!$9->rport.b && $9->rport.t &&
48623b3a8eb9SGleb Smirnoff 					    $5.dst.port != NULL) {
48633b3a8eb9SGleb Smirnoff 						r.rpool.proxy_port[1] =
48643b3a8eb9SGleb Smirnoff 						    ntohs($9->rport.a) +
48653b3a8eb9SGleb Smirnoff 						    (ntohs(
48663b3a8eb9SGleb Smirnoff 						    $5.dst.port->port[1]) -
48673b3a8eb9SGleb Smirnoff 						    ntohs(
48683b3a8eb9SGleb Smirnoff 						    $5.dst.port->port[0]));
48693b3a8eb9SGleb Smirnoff 					} else
48703b3a8eb9SGleb Smirnoff 						r.rpool.proxy_port[1] =
48713b3a8eb9SGleb Smirnoff 						    ntohs($9->rport.b);
48723b3a8eb9SGleb Smirnoff 					break;
48733b3a8eb9SGleb Smirnoff 				case PF_NAT:
48743b3a8eb9SGleb Smirnoff 					r.rpool.proxy_port[1] =
48753b3a8eb9SGleb Smirnoff 					    ntohs($9->rport.b);
48763b3a8eb9SGleb Smirnoff 					if (!r.rpool.proxy_port[0] &&
48773b3a8eb9SGleb Smirnoff 					    !r.rpool.proxy_port[1]) {
48783b3a8eb9SGleb Smirnoff 						r.rpool.proxy_port[0] =
48793b3a8eb9SGleb Smirnoff 						    PF_NAT_PROXY_PORT_LOW;
48803b3a8eb9SGleb Smirnoff 						r.rpool.proxy_port[1] =
48813b3a8eb9SGleb Smirnoff 						    PF_NAT_PROXY_PORT_HIGH;
48823b3a8eb9SGleb Smirnoff 					} else if (!r.rpool.proxy_port[1])
48833b3a8eb9SGleb Smirnoff 						r.rpool.proxy_port[1] =
48843b3a8eb9SGleb Smirnoff 						    r.rpool.proxy_port[0];
48853b3a8eb9SGleb Smirnoff 					break;
48863b3a8eb9SGleb Smirnoff 				default:
48873b3a8eb9SGleb Smirnoff 					break;
48883b3a8eb9SGleb Smirnoff 				}
48893b3a8eb9SGleb Smirnoff 
48903b3a8eb9SGleb Smirnoff 				r.rpool.opts = $10.type;
48913b3a8eb9SGleb Smirnoff 				if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
48923b3a8eb9SGleb Smirnoff 				    PF_POOL_NONE && ($9->host->next != NULL ||
48933b3a8eb9SGleb Smirnoff 				    $9->host->addr.type == PF_ADDR_TABLE ||
48943b3a8eb9SGleb Smirnoff 				    DYNIF_MULTIADDR($9->host->addr)))
48953b3a8eb9SGleb Smirnoff 					r.rpool.opts = PF_POOL_ROUNDROBIN;
48963b3a8eb9SGleb Smirnoff 				if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
48973b3a8eb9SGleb Smirnoff 				    PF_POOL_ROUNDROBIN &&
48983b3a8eb9SGleb Smirnoff 				    disallow_table($9->host, "tables are only "
48993b3a8eb9SGleb Smirnoff 				    "supported in round-robin redirection "
49003b3a8eb9SGleb Smirnoff 				    "pools"))
49013b3a8eb9SGleb Smirnoff 					YYERROR;
49023b3a8eb9SGleb Smirnoff 				if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
49033b3a8eb9SGleb Smirnoff 				    PF_POOL_ROUNDROBIN &&
49043b3a8eb9SGleb Smirnoff 				    disallow_alias($9->host, "interface (%s) "
49053b3a8eb9SGleb Smirnoff 				    "is only supported in round-robin "
49063b3a8eb9SGleb Smirnoff 				    "redirection pools"))
49073b3a8eb9SGleb Smirnoff 					YYERROR;
49083b3a8eb9SGleb Smirnoff 				if ($9->host->next != NULL) {
49093b3a8eb9SGleb Smirnoff 					if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
49103b3a8eb9SGleb Smirnoff 					    PF_POOL_ROUNDROBIN) {
49113b3a8eb9SGleb Smirnoff 						yyerror("only round-robin "
49123b3a8eb9SGleb Smirnoff 						    "valid for multiple "
49133b3a8eb9SGleb Smirnoff 						    "redirection addresses");
49143b3a8eb9SGleb Smirnoff 						YYERROR;
49153b3a8eb9SGleb Smirnoff 					}
49163b3a8eb9SGleb Smirnoff 				}
49173b3a8eb9SGleb Smirnoff 			}
49183b3a8eb9SGleb Smirnoff 
49193b3a8eb9SGleb Smirnoff 			if ($10.key != NULL)
49203b3a8eb9SGleb Smirnoff 				memcpy(&r.rpool.key, $10.key,
49213b3a8eb9SGleb Smirnoff 				    sizeof(struct pf_poolhashkey));
49223b3a8eb9SGleb Smirnoff 
49233b3a8eb9SGleb Smirnoff 			 if ($10.opts)
49243b3a8eb9SGleb Smirnoff 				r.rpool.opts |= $10.opts;
49253b3a8eb9SGleb Smirnoff 
49263b3a8eb9SGleb Smirnoff 			if ($10.staticport) {
49273b3a8eb9SGleb Smirnoff 				if (r.action != PF_NAT) {
49283b3a8eb9SGleb Smirnoff 					yyerror("the 'static-port' option is "
49293b3a8eb9SGleb Smirnoff 					    "only valid with nat rules");
49303b3a8eb9SGleb Smirnoff 					YYERROR;
49313b3a8eb9SGleb Smirnoff 				}
49323b3a8eb9SGleb Smirnoff 				if (r.rpool.proxy_port[0] !=
49333b3a8eb9SGleb Smirnoff 				    PF_NAT_PROXY_PORT_LOW &&
49343b3a8eb9SGleb Smirnoff 				    r.rpool.proxy_port[1] !=
49353b3a8eb9SGleb Smirnoff 				    PF_NAT_PROXY_PORT_HIGH) {
49363b3a8eb9SGleb Smirnoff 					yyerror("the 'static-port' option can't"
49373b3a8eb9SGleb Smirnoff 					    " be used when specifying a port"
49383b3a8eb9SGleb Smirnoff 					    " range");
49393b3a8eb9SGleb Smirnoff 					YYERROR;
49403b3a8eb9SGleb Smirnoff 				}
49413b3a8eb9SGleb Smirnoff 				r.rpool.proxy_port[0] = 0;
49423b3a8eb9SGleb Smirnoff 				r.rpool.proxy_port[1] = 0;
49433b3a8eb9SGleb Smirnoff 			}
49443b3a8eb9SGleb Smirnoff 
49452aa21096SKurosawa Takahiro 			if ($10.mape.offset) {
49462aa21096SKurosawa Takahiro 				if (r.action != PF_NAT) {
49472aa21096SKurosawa Takahiro 					yyerror("the 'map-e-portset' option is"
49482aa21096SKurosawa Takahiro 					    " only valid with nat rules");
49492aa21096SKurosawa Takahiro 					YYERROR;
49502aa21096SKurosawa Takahiro 				}
49512aa21096SKurosawa Takahiro 				if ($10.staticport) {
49522aa21096SKurosawa Takahiro 					yyerror("the 'map-e-portset' option"
49532aa21096SKurosawa Takahiro 					    " can't be used 'static-port'");
49542aa21096SKurosawa Takahiro 					YYERROR;
49552aa21096SKurosawa Takahiro 				}
49562aa21096SKurosawa Takahiro 				if (r.rpool.proxy_port[0] !=
49572aa21096SKurosawa Takahiro 				    PF_NAT_PROXY_PORT_LOW &&
49582aa21096SKurosawa Takahiro 				    r.rpool.proxy_port[1] !=
49592aa21096SKurosawa Takahiro 				    PF_NAT_PROXY_PORT_HIGH) {
49602aa21096SKurosawa Takahiro 					yyerror("the 'map-e-portset' option"
49612aa21096SKurosawa Takahiro 					    " can't be used when specifying"
49622aa21096SKurosawa Takahiro 					    " a port range");
49632aa21096SKurosawa Takahiro 					YYERROR;
49642aa21096SKurosawa Takahiro 				}
49652aa21096SKurosawa Takahiro 				r.rpool.mape = $10.mape;
49662aa21096SKurosawa Takahiro 			}
49672aa21096SKurosawa Takahiro 
4968fc6e5069SKristof Provost 			o = keep_state_defaults;
4969fc6e5069SKristof Provost 			while (o) {
4970fc6e5069SKristof Provost 				switch (o->type) {
4971fc6e5069SKristof Provost 				case PF_STATE_OPT_PFLOW:
4972fc6e5069SKristof Provost 					if (r.rule_flag & PFRULE_PFLOW) {
4973fc6e5069SKristof Provost 						yyerror("state pflow option: "
4974fc6e5069SKristof Provost 						    "multiple definitions");
4975fc6e5069SKristof Provost 						YYERROR;
4976fc6e5069SKristof Provost 					}
4977fc6e5069SKristof Provost 					r.rule_flag |= PFRULE_PFLOW;
4978fc6e5069SKristof Provost 					break;
4979fc6e5069SKristof Provost 				}
4980fc6e5069SKristof Provost 				o = o->next;
4981fc6e5069SKristof Provost 			}
4982fc6e5069SKristof Provost 
4983*aa69fdf1SKristof Provost 			expand_rule(&r, $2, $9 == NULL ? NULL : $9->host, NULL, $4,
49843b3a8eb9SGleb Smirnoff 			    $5.src_os, $5.src.host, $5.src.port, $5.dst.host,
49852339ead6SKristof Provost 			    $5.dst.port, 0, 0, 0, 0, "");
49863b3a8eb9SGleb Smirnoff 			free($9);
49873b3a8eb9SGleb Smirnoff 		}
49883b3a8eb9SGleb Smirnoff 		;
49893b3a8eb9SGleb Smirnoff 
49901e93588bSLuiz Otavio O Souza binatrule	: no BINAT natpasslog interface af proto FROM ipspec toipspec tag
49913b3a8eb9SGleb Smirnoff 		    tagged rtable redirection
49923b3a8eb9SGleb Smirnoff 		{
4993e9eb0941SKristof Provost 			struct pfctl_rule	binat;
49943b3a8eb9SGleb Smirnoff 			struct pf_pooladdr	*pa;
49953b3a8eb9SGleb Smirnoff 
49963b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_NAT))
49973b3a8eb9SGleb Smirnoff 				YYERROR;
49983b3a8eb9SGleb Smirnoff 			if (disallow_urpf_failed($9, "\"urpf-failed\" is not "
49993b3a8eb9SGleb Smirnoff 			    "permitted as a binat destination"))
50003b3a8eb9SGleb Smirnoff 				YYERROR;
50013b3a8eb9SGleb Smirnoff 
50023b3a8eb9SGleb Smirnoff 			memset(&binat, 0, sizeof(binat));
50033b3a8eb9SGleb Smirnoff 
50043b3a8eb9SGleb Smirnoff 			if ($1 && $3.b1) {
50053b3a8eb9SGleb Smirnoff 				yyerror("\"pass\" not valid with \"no\"");
50063b3a8eb9SGleb Smirnoff 				YYERROR;
50073b3a8eb9SGleb Smirnoff 			}
50083b3a8eb9SGleb Smirnoff 			if ($1)
50093b3a8eb9SGleb Smirnoff 				binat.action = PF_NOBINAT;
50103b3a8eb9SGleb Smirnoff 			else
50113b3a8eb9SGleb Smirnoff 				binat.action = PF_BINAT;
50123b3a8eb9SGleb Smirnoff 			binat.natpass = $3.b1;
50133b3a8eb9SGleb Smirnoff 			binat.log = $3.b2;
50143b3a8eb9SGleb Smirnoff 			binat.logif = $3.w2;
50153b3a8eb9SGleb Smirnoff 			binat.af = $5;
50163b3a8eb9SGleb Smirnoff 			if (!binat.af && $8 != NULL && $8->af)
50173b3a8eb9SGleb Smirnoff 				binat.af = $8->af;
50183b3a8eb9SGleb Smirnoff 			if (!binat.af && $9 != NULL && $9->af)
50193b3a8eb9SGleb Smirnoff 				binat.af = $9->af;
50203b3a8eb9SGleb Smirnoff 
50213b3a8eb9SGleb Smirnoff 			if (!binat.af && $13 != NULL && $13->host)
50223b3a8eb9SGleb Smirnoff 				binat.af = $13->host->af;
50233b3a8eb9SGleb Smirnoff 			if (!binat.af) {
50243b3a8eb9SGleb Smirnoff 				yyerror("address family (inet/inet6) "
50253b3a8eb9SGleb Smirnoff 				    "undefined");
50263b3a8eb9SGleb Smirnoff 				YYERROR;
50273b3a8eb9SGleb Smirnoff 			}
50283b3a8eb9SGleb Smirnoff 
50293b3a8eb9SGleb Smirnoff 			if ($4 != NULL) {
50303b3a8eb9SGleb Smirnoff 				memcpy(binat.ifname, $4->ifname,
50313b3a8eb9SGleb Smirnoff 				    sizeof(binat.ifname));
50323b3a8eb9SGleb Smirnoff 				binat.ifnot = $4->not;
50333b3a8eb9SGleb Smirnoff 				free($4);
50343b3a8eb9SGleb Smirnoff 			}
50353b3a8eb9SGleb Smirnoff 
50363b3a8eb9SGleb Smirnoff 			if ($10 != NULL)
50373b3a8eb9SGleb Smirnoff 				if (strlcpy(binat.tagname, $10,
50383b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
50393b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
50403b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
50413b3a8eb9SGleb Smirnoff 					YYERROR;
50423b3a8eb9SGleb Smirnoff 				}
50433b3a8eb9SGleb Smirnoff 			if ($11.name)
50443b3a8eb9SGleb Smirnoff 				if (strlcpy(binat.match_tagname, $11.name,
50453b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
50463b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
50473b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
50483b3a8eb9SGleb Smirnoff 					YYERROR;
50493b3a8eb9SGleb Smirnoff 				}
50503b3a8eb9SGleb Smirnoff 			binat.match_tag_not = $11.neg;
50513b3a8eb9SGleb Smirnoff 			binat.rtableid = $12;
50523b3a8eb9SGleb Smirnoff 
50533b3a8eb9SGleb Smirnoff 			if ($6 != NULL) {
50543b3a8eb9SGleb Smirnoff 				binat.proto = $6->proto;
50553b3a8eb9SGleb Smirnoff 				free($6);
50563b3a8eb9SGleb Smirnoff 			}
50573b3a8eb9SGleb Smirnoff 
50583b3a8eb9SGleb Smirnoff 			if ($8 != NULL && disallow_table($8, "invalid use of "
50593b3a8eb9SGleb Smirnoff 			    "table <%s> as the source address of a binat rule"))
50603b3a8eb9SGleb Smirnoff 				YYERROR;
50613b3a8eb9SGleb Smirnoff 			if ($8 != NULL && disallow_alias($8, "invalid use of "
50623b3a8eb9SGleb Smirnoff 			    "interface (%s) as the source address of a binat "
50633b3a8eb9SGleb Smirnoff 			    "rule"))
50643b3a8eb9SGleb Smirnoff 				YYERROR;
50653b3a8eb9SGleb Smirnoff 			if ($13 != NULL && $13->host != NULL && disallow_table(
50663b3a8eb9SGleb Smirnoff 			    $13->host, "invalid use of table <%s> as the "
50673b3a8eb9SGleb Smirnoff 			    "redirect address of a binat rule"))
50683b3a8eb9SGleb Smirnoff 				YYERROR;
50693b3a8eb9SGleb Smirnoff 			if ($13 != NULL && $13->host != NULL && disallow_alias(
50703b3a8eb9SGleb Smirnoff 			    $13->host, "invalid use of interface (%s) as the "
50713b3a8eb9SGleb Smirnoff 			    "redirect address of a binat rule"))
50723b3a8eb9SGleb Smirnoff 				YYERROR;
50733b3a8eb9SGleb Smirnoff 
50743b3a8eb9SGleb Smirnoff 			if ($8 != NULL) {
50753b3a8eb9SGleb Smirnoff 				if ($8->next) {
50763b3a8eb9SGleb Smirnoff 					yyerror("multiple binat ip addresses");
50773b3a8eb9SGleb Smirnoff 					YYERROR;
50783b3a8eb9SGleb Smirnoff 				}
50793b3a8eb9SGleb Smirnoff 				if ($8->addr.type == PF_ADDR_DYNIFTL)
50803b3a8eb9SGleb Smirnoff 					$8->af = binat.af;
50813b3a8eb9SGleb Smirnoff 				if ($8->af != binat.af) {
50823b3a8eb9SGleb Smirnoff 					yyerror("binat ip versions must match");
50833b3a8eb9SGleb Smirnoff 					YYERROR;
50843b3a8eb9SGleb Smirnoff 				}
50857ce98cf2SKristof Provost 				if ($8->addr.type == PF_ADDR_DYNIFTL) {
50867ce98cf2SKristof Provost 					if (($8 = gen_dynnode($8, binat.af)) == NULL)
50877ce98cf2SKristof Provost 						err(1, "calloc");
50887ce98cf2SKristof Provost 				}
50893b3a8eb9SGleb Smirnoff 				if (check_netmask($8, binat.af))
50903b3a8eb9SGleb Smirnoff 					YYERROR;
50913b3a8eb9SGleb Smirnoff 				memcpy(&binat.src.addr, &$8->addr,
50923b3a8eb9SGleb Smirnoff 				    sizeof(binat.src.addr));
50933b3a8eb9SGleb Smirnoff 				free($8);
50943b3a8eb9SGleb Smirnoff 			}
50953b3a8eb9SGleb Smirnoff 			if ($9 != NULL) {
50963b3a8eb9SGleb Smirnoff 				if ($9->next) {
50973b3a8eb9SGleb Smirnoff 					yyerror("multiple binat ip addresses");
50983b3a8eb9SGleb Smirnoff 					YYERROR;
50993b3a8eb9SGleb Smirnoff 				}
51003b3a8eb9SGleb Smirnoff 				if ($9->af != binat.af && $9->af) {
51013b3a8eb9SGleb Smirnoff 					yyerror("binat ip versions must match");
51023b3a8eb9SGleb Smirnoff 					YYERROR;
51033b3a8eb9SGleb Smirnoff 				}
51047ce98cf2SKristof Provost 				if ($9->addr.type == PF_ADDR_DYNIFTL) {
51057ce98cf2SKristof Provost 					if (($9 = gen_dynnode($9, binat.af)) == NULL)
51067ce98cf2SKristof Provost 						err(1, "calloc");
51077ce98cf2SKristof Provost 				}
51083b3a8eb9SGleb Smirnoff 				if (check_netmask($9, binat.af))
51093b3a8eb9SGleb Smirnoff 					YYERROR;
51103b3a8eb9SGleb Smirnoff 				memcpy(&binat.dst.addr, &$9->addr,
51113b3a8eb9SGleb Smirnoff 				    sizeof(binat.dst.addr));
51123b3a8eb9SGleb Smirnoff 				binat.dst.neg = $9->not;
51133b3a8eb9SGleb Smirnoff 				free($9);
51143b3a8eb9SGleb Smirnoff 			}
51153b3a8eb9SGleb Smirnoff 
51163b3a8eb9SGleb Smirnoff 			if (binat.action == PF_NOBINAT) {
51173b3a8eb9SGleb Smirnoff 				if ($13 != NULL) {
51183b3a8eb9SGleb Smirnoff 					yyerror("'no binat' rule does not need"
51193b3a8eb9SGleb Smirnoff 					    " '->'");
51203b3a8eb9SGleb Smirnoff 					YYERROR;
51213b3a8eb9SGleb Smirnoff 				}
51223b3a8eb9SGleb Smirnoff 			} else {
51233b3a8eb9SGleb Smirnoff 				if ($13 == NULL || $13->host == NULL) {
51243b3a8eb9SGleb Smirnoff 					yyerror("'binat' rule requires"
51253b3a8eb9SGleb Smirnoff 					    " '-> address'");
51263b3a8eb9SGleb Smirnoff 					YYERROR;
51273b3a8eb9SGleb Smirnoff 				}
51283b3a8eb9SGleb Smirnoff 
51293b3a8eb9SGleb Smirnoff 				remove_invalid_hosts(&$13->host, &binat.af);
51303b3a8eb9SGleb Smirnoff 				if (invalid_redirect($13->host, binat.af))
51313b3a8eb9SGleb Smirnoff 					YYERROR;
51323b3a8eb9SGleb Smirnoff 				if ($13->host->next != NULL) {
51333b3a8eb9SGleb Smirnoff 					yyerror("binat rule must redirect to "
51343b3a8eb9SGleb Smirnoff 					    "a single address");
51353b3a8eb9SGleb Smirnoff 					YYERROR;
51363b3a8eb9SGleb Smirnoff 				}
51377ce98cf2SKristof Provost 				if ($13->host->addr.type == PF_ADDR_DYNIFTL) {
51387ce98cf2SKristof Provost 					if (($13->host = gen_dynnode($13->host, binat.af)) == NULL)
51397ce98cf2SKristof Provost 						err(1, "calloc");
51407ce98cf2SKristof Provost 				}
51413b3a8eb9SGleb Smirnoff 				if (check_netmask($13->host, binat.af))
51423b3a8eb9SGleb Smirnoff 					YYERROR;
51433b3a8eb9SGleb Smirnoff 
51443b3a8eb9SGleb Smirnoff 				if (!PF_AZERO(&binat.src.addr.v.a.mask,
51453b3a8eb9SGleb Smirnoff 				    binat.af) &&
51463b3a8eb9SGleb Smirnoff 				    !PF_AEQ(&binat.src.addr.v.a.mask,
51473b3a8eb9SGleb Smirnoff 				    &$13->host->addr.v.a.mask, binat.af)) {
51483b3a8eb9SGleb Smirnoff 					yyerror("'binat' source mask and "
51493b3a8eb9SGleb Smirnoff 					    "redirect mask must be the same");
51503b3a8eb9SGleb Smirnoff 					YYERROR;
51513b3a8eb9SGleb Smirnoff 				}
51523b3a8eb9SGleb Smirnoff 
51533b3a8eb9SGleb Smirnoff 				TAILQ_INIT(&binat.rpool.list);
51543b3a8eb9SGleb Smirnoff 				pa = calloc(1, sizeof(struct pf_pooladdr));
51553b3a8eb9SGleb Smirnoff 				if (pa == NULL)
51563b3a8eb9SGleb Smirnoff 					err(1, "binat: calloc");
51573b3a8eb9SGleb Smirnoff 				pa->addr = $13->host->addr;
51583b3a8eb9SGleb Smirnoff 				pa->ifname[0] = 0;
51593b3a8eb9SGleb Smirnoff 				TAILQ_INSERT_TAIL(&binat.rpool.list,
51603b3a8eb9SGleb Smirnoff 				    pa, entries);
51613b3a8eb9SGleb Smirnoff 
51623b3a8eb9SGleb Smirnoff 				free($13);
51633b3a8eb9SGleb Smirnoff 			}
51643b3a8eb9SGleb Smirnoff 
51650d71f9f3SKristof Provost 			pfctl_append_rule(pf, &binat, "");
51663b3a8eb9SGleb Smirnoff 		}
51673b3a8eb9SGleb Smirnoff 		;
51683b3a8eb9SGleb Smirnoff 
51693b3a8eb9SGleb Smirnoff tag		: /* empty */		{ $$ = NULL; }
51703b3a8eb9SGleb Smirnoff 		| TAG STRING		{ $$ = $2; }
51713b3a8eb9SGleb Smirnoff 		;
51723b3a8eb9SGleb Smirnoff 
51733b3a8eb9SGleb Smirnoff tagged		: /* empty */		{ $$.neg = 0; $$.name = NULL; }
51743b3a8eb9SGleb Smirnoff 		| not TAGGED string	{ $$.neg = $1; $$.name = $3; }
51753b3a8eb9SGleb Smirnoff 		;
51763b3a8eb9SGleb Smirnoff 
51773b3a8eb9SGleb Smirnoff rtable		: /* empty */		{ $$ = -1; }
51783b3a8eb9SGleb Smirnoff 		| RTABLE NUMBER		{
51793b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > rt_tableid_max()) {
51803b3a8eb9SGleb Smirnoff 				yyerror("invalid rtable id");
51813b3a8eb9SGleb Smirnoff 				YYERROR;
51823b3a8eb9SGleb Smirnoff 			}
51833b3a8eb9SGleb Smirnoff 			$$ = $2;
51843b3a8eb9SGleb Smirnoff 		}
51853b3a8eb9SGleb Smirnoff 		;
51863b3a8eb9SGleb Smirnoff 
51873b3a8eb9SGleb Smirnoff route_host	: STRING			{
51883b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_host));
51893b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
51903b3a8eb9SGleb Smirnoff 				err(1, "route_host: calloc");
5191e68de669SKristof Provost 			if (strlen($1) >= IFNAMSIZ) {
5192e68de669SKristof Provost 				yyerror("interface name too long");
5193e68de669SKristof Provost 				YYERROR;
5194e68de669SKristof Provost 			}
5195a2a90d6eSKristof Provost 			$$->ifname = strdup($1);
51963b3a8eb9SGleb Smirnoff 			set_ipmask($$, 128);
51973b3a8eb9SGleb Smirnoff 			$$->next = NULL;
51983b3a8eb9SGleb Smirnoff 			$$->tail = $$;
51993b3a8eb9SGleb Smirnoff 		}
52003b3a8eb9SGleb Smirnoff 		| '(' STRING host ')'		{
520158c8430aSKristof Provost 			struct node_host *n;
520258c8430aSKristof Provost 
52033b3a8eb9SGleb Smirnoff 			$$ = $3;
5204e68de669SKristof Provost 			for (n = $3; n != NULL; n = n->next) {
5205e68de669SKristof Provost 				if (strlen($2) >= IFNAMSIZ) {
5206e68de669SKristof Provost 					yyerror("interface name too long");
5207e68de669SKristof Provost 					YYERROR;
5208e68de669SKristof Provost 				}
5209a2a90d6eSKristof Provost 				n->ifname = strdup($2);
52103b3a8eb9SGleb Smirnoff 			}
5211e68de669SKristof Provost 		}
52123b3a8eb9SGleb Smirnoff 		;
52133b3a8eb9SGleb Smirnoff 
52143b3a8eb9SGleb Smirnoff route_host_list	: route_host optnl			{ $$ = $1; }
52153b3a8eb9SGleb Smirnoff 		| route_host_list comma route_host optnl {
52163b3a8eb9SGleb Smirnoff 			if ($1->af == 0)
52173b3a8eb9SGleb Smirnoff 				$1->af = $3->af;
52183b3a8eb9SGleb Smirnoff 			if ($1->af != $3->af) {
52193b3a8eb9SGleb Smirnoff 				yyerror("all pool addresses must be in the "
52203b3a8eb9SGleb Smirnoff 				    "same address family");
52213b3a8eb9SGleb Smirnoff 				YYERROR;
52223b3a8eb9SGleb Smirnoff 			}
52233b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
52243b3a8eb9SGleb Smirnoff 			$1->tail = $3->tail;
52253b3a8eb9SGleb Smirnoff 			$$ = $1;
52263b3a8eb9SGleb Smirnoff 		}
52273b3a8eb9SGleb Smirnoff 		;
52283b3a8eb9SGleb Smirnoff 
52293b3a8eb9SGleb Smirnoff routespec	: route_host			{ $$ = $1; }
52303b3a8eb9SGleb Smirnoff 		| '{' optnl route_host_list '}'	{ $$ = $3; }
52313b3a8eb9SGleb Smirnoff 		;
52323b3a8eb9SGleb Smirnoff 
52333b3a8eb9SGleb Smirnoff route		: /* empty */			{
52343b3a8eb9SGleb Smirnoff 			$$.host = NULL;
52353b3a8eb9SGleb Smirnoff 			$$.rt = 0;
52363b3a8eb9SGleb Smirnoff 			$$.pool_opts = 0;
52373b3a8eb9SGleb Smirnoff 		}
52383b3a8eb9SGleb Smirnoff 		| FASTROUTE {
5239813196a1SKristof Provost 			/* backwards-compat */
52403b3a8eb9SGleb Smirnoff 			$$.host = NULL;
5241813196a1SKristof Provost 			$$.rt = 0;
52423b3a8eb9SGleb Smirnoff 			$$.pool_opts = 0;
52433b3a8eb9SGleb Smirnoff 		}
52443b3a8eb9SGleb Smirnoff 		| ROUTETO routespec pool_opts {
52453b3a8eb9SGleb Smirnoff 			$$.host = $2;
52463b3a8eb9SGleb Smirnoff 			$$.rt = PF_ROUTETO;
52473b3a8eb9SGleb Smirnoff 			$$.pool_opts = $3.type | $3.opts;
52483b3a8eb9SGleb Smirnoff 			if ($3.key != NULL)
52493b3a8eb9SGleb Smirnoff 				$$.key = $3.key;
52503b3a8eb9SGleb Smirnoff 		}
52513b3a8eb9SGleb Smirnoff 		| REPLYTO routespec pool_opts {
52523b3a8eb9SGleb Smirnoff 			$$.host = $2;
52533b3a8eb9SGleb Smirnoff 			$$.rt = PF_REPLYTO;
52543b3a8eb9SGleb Smirnoff 			$$.pool_opts = $3.type | $3.opts;
52553b3a8eb9SGleb Smirnoff 			if ($3.key != NULL)
52563b3a8eb9SGleb Smirnoff 				$$.key = $3.key;
52573b3a8eb9SGleb Smirnoff 		}
52583b3a8eb9SGleb Smirnoff 		| DUPTO routespec pool_opts {
52593b3a8eb9SGleb Smirnoff 			$$.host = $2;
52603b3a8eb9SGleb Smirnoff 			$$.rt = PF_DUPTO;
52613b3a8eb9SGleb Smirnoff 			$$.pool_opts = $3.type | $3.opts;
52623b3a8eb9SGleb Smirnoff 			if ($3.key != NULL)
52633b3a8eb9SGleb Smirnoff 				$$.key = $3.key;
52643b3a8eb9SGleb Smirnoff 		}
52653b3a8eb9SGleb Smirnoff 		;
52663b3a8eb9SGleb Smirnoff 
52673b3a8eb9SGleb Smirnoff timeout_spec	: STRING NUMBER
52683b3a8eb9SGleb Smirnoff 		{
52693b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_OPTION)) {
52703b3a8eb9SGleb Smirnoff 				free($1);
52713b3a8eb9SGleb Smirnoff 				YYERROR;
52723b3a8eb9SGleb Smirnoff 			}
52733b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > UINT_MAX) {
52743b3a8eb9SGleb Smirnoff 				yyerror("only positive values permitted");
52753b3a8eb9SGleb Smirnoff 				YYERROR;
52763b3a8eb9SGleb Smirnoff 			}
527730bad751SKristof Provost 			if (pfctl_apply_timeout(pf, $1, $2, 0) != 0) {
52783b3a8eb9SGleb Smirnoff 				yyerror("unknown timeout %s", $1);
52793b3a8eb9SGleb Smirnoff 				free($1);
52803b3a8eb9SGleb Smirnoff 				YYERROR;
52813b3a8eb9SGleb Smirnoff 			}
52823b3a8eb9SGleb Smirnoff 			free($1);
52833b3a8eb9SGleb Smirnoff 		}
52847f8af000SLuiz Otavio O Souza 		| INTERVAL NUMBER		{
52857f8af000SLuiz Otavio O Souza 			if (check_rulestate(PFCTL_STATE_OPTION))
52867f8af000SLuiz Otavio O Souza 				YYERROR;
52877f8af000SLuiz Otavio O Souza 			if ($2 < 0 || $2 > UINT_MAX) {
52887f8af000SLuiz Otavio O Souza 				yyerror("only positive values permitted");
52897f8af000SLuiz Otavio O Souza 				YYERROR;
52907f8af000SLuiz Otavio O Souza 			}
529130bad751SKristof Provost 			if (pfctl_apply_timeout(pf, "interval", $2, 0) != 0)
52927f8af000SLuiz Otavio O Souza 				YYERROR;
52937f8af000SLuiz Otavio O Souza 		}
52943b3a8eb9SGleb Smirnoff 		;
52953b3a8eb9SGleb Smirnoff 
52963b3a8eb9SGleb Smirnoff timeout_list	: timeout_list comma timeout_spec optnl
52973b3a8eb9SGleb Smirnoff 		| timeout_spec optnl
52983b3a8eb9SGleb Smirnoff 		;
52993b3a8eb9SGleb Smirnoff 
53003b3a8eb9SGleb Smirnoff limit_spec	: STRING NUMBER
53013b3a8eb9SGleb Smirnoff 		{
53023b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_OPTION)) {
53033b3a8eb9SGleb Smirnoff 				free($1);
53043b3a8eb9SGleb Smirnoff 				YYERROR;
53053b3a8eb9SGleb Smirnoff 			}
53063b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > UINT_MAX) {
53073b3a8eb9SGleb Smirnoff 				yyerror("only positive values permitted");
53083b3a8eb9SGleb Smirnoff 				YYERROR;
53093b3a8eb9SGleb Smirnoff 			}
5310d9ab8999SKristof Provost 			if (pfctl_apply_limit(pf, $1, $2) != 0) {
53113b3a8eb9SGleb Smirnoff 				yyerror("unable to set limit %s %u", $1, $2);
53123b3a8eb9SGleb Smirnoff 				free($1);
53133b3a8eb9SGleb Smirnoff 				YYERROR;
53143b3a8eb9SGleb Smirnoff 			}
53153b3a8eb9SGleb Smirnoff 			free($1);
53163b3a8eb9SGleb Smirnoff 		}
53173b3a8eb9SGleb Smirnoff 		;
53183b3a8eb9SGleb Smirnoff 
53193b3a8eb9SGleb Smirnoff limit_list	: limit_list comma limit_spec optnl
53203b3a8eb9SGleb Smirnoff 		| limit_spec optnl
53213b3a8eb9SGleb Smirnoff 		;
53223b3a8eb9SGleb Smirnoff 
53233b3a8eb9SGleb Smirnoff comma		: ','
53243b3a8eb9SGleb Smirnoff 		| /* empty */
53253b3a8eb9SGleb Smirnoff 		;
53263b3a8eb9SGleb Smirnoff 
53273b3a8eb9SGleb Smirnoff yesno		: NO			{ $$ = 0; }
53283b3a8eb9SGleb Smirnoff 		| STRING		{
53293b3a8eb9SGleb Smirnoff 			if (!strcmp($1, "yes"))
53303b3a8eb9SGleb Smirnoff 				$$ = 1;
53313b3a8eb9SGleb Smirnoff 			else {
53323b3a8eb9SGleb Smirnoff 				yyerror("invalid value '%s', expected 'yes' "
53333b3a8eb9SGleb Smirnoff 				    "or 'no'", $1);
53343b3a8eb9SGleb Smirnoff 				free($1);
53353b3a8eb9SGleb Smirnoff 				YYERROR;
53363b3a8eb9SGleb Smirnoff 			}
53373b3a8eb9SGleb Smirnoff 			free($1);
53383b3a8eb9SGleb Smirnoff 		}
53393b3a8eb9SGleb Smirnoff 		;
53403b3a8eb9SGleb Smirnoff 
53413b3a8eb9SGleb Smirnoff unaryop		: '='		{ $$ = PF_OP_EQ; }
534280eb861dSKristof Provost 		| NE			{ $$ = PF_OP_NE; }
534380eb861dSKristof Provost 		| LE			{ $$ = PF_OP_LE; }
53443b3a8eb9SGleb Smirnoff 		| '<'		{ $$ = PF_OP_LT; }
534580eb861dSKristof Provost 		| GE		{ $$ = PF_OP_GE; }
53463b3a8eb9SGleb Smirnoff 		| '>'		{ $$ = PF_OP_GT; }
53473b3a8eb9SGleb Smirnoff 		;
53483b3a8eb9SGleb Smirnoff 
53493b3a8eb9SGleb Smirnoff %%
53503b3a8eb9SGleb Smirnoff 
53513b3a8eb9SGleb Smirnoff int
53523b3a8eb9SGleb Smirnoff yyerror(const char *fmt, ...)
53533b3a8eb9SGleb Smirnoff {
53543b3a8eb9SGleb Smirnoff 	va_list		 ap;
53553b3a8eb9SGleb Smirnoff 
53563b3a8eb9SGleb Smirnoff 	file->errors++;
53573b3a8eb9SGleb Smirnoff 	va_start(ap, fmt);
53583b3a8eb9SGleb Smirnoff 	fprintf(stderr, "%s:%d: ", file->name, yylval.lineno);
53593b3a8eb9SGleb Smirnoff 	vfprintf(stderr, fmt, ap);
53603b3a8eb9SGleb Smirnoff 	fprintf(stderr, "\n");
53613b3a8eb9SGleb Smirnoff 	va_end(ap);
53623b3a8eb9SGleb Smirnoff 	return (0);
53633b3a8eb9SGleb Smirnoff }
53643b3a8eb9SGleb Smirnoff 
53653b3a8eb9SGleb Smirnoff int
53663b3a8eb9SGleb Smirnoff disallow_table(struct node_host *h, const char *fmt)
53673b3a8eb9SGleb Smirnoff {
53683b3a8eb9SGleb Smirnoff 	for (; h != NULL; h = h->next)
53693b3a8eb9SGleb Smirnoff 		if (h->addr.type == PF_ADDR_TABLE) {
53703b3a8eb9SGleb Smirnoff 			yyerror(fmt, h->addr.v.tblname);
53713b3a8eb9SGleb Smirnoff 			return (1);
53723b3a8eb9SGleb Smirnoff 		}
53733b3a8eb9SGleb Smirnoff 	return (0);
53743b3a8eb9SGleb Smirnoff }
53753b3a8eb9SGleb Smirnoff 
53763b3a8eb9SGleb Smirnoff int
53773b3a8eb9SGleb Smirnoff disallow_urpf_failed(struct node_host *h, const char *fmt)
53783b3a8eb9SGleb Smirnoff {
53793b3a8eb9SGleb Smirnoff 	for (; h != NULL; h = h->next)
53803b3a8eb9SGleb Smirnoff 		if (h->addr.type == PF_ADDR_URPFFAILED) {
53813b3a8eb9SGleb Smirnoff 			yyerror(fmt);
53823b3a8eb9SGleb Smirnoff 			return (1);
53833b3a8eb9SGleb Smirnoff 		}
53843b3a8eb9SGleb Smirnoff 	return (0);
53853b3a8eb9SGleb Smirnoff }
53863b3a8eb9SGleb Smirnoff 
53873b3a8eb9SGleb Smirnoff int
53883b3a8eb9SGleb Smirnoff disallow_alias(struct node_host *h, const char *fmt)
53893b3a8eb9SGleb Smirnoff {
53903b3a8eb9SGleb Smirnoff 	for (; h != NULL; h = h->next)
53913b3a8eb9SGleb Smirnoff 		if (DYNIF_MULTIADDR(h->addr)) {
53923b3a8eb9SGleb Smirnoff 			yyerror(fmt, h->addr.v.tblname);
53933b3a8eb9SGleb Smirnoff 			return (1);
53943b3a8eb9SGleb Smirnoff 		}
53953b3a8eb9SGleb Smirnoff 	return (0);
53963b3a8eb9SGleb Smirnoff }
53973b3a8eb9SGleb Smirnoff 
53983b3a8eb9SGleb Smirnoff int
5399e9eb0941SKristof Provost rule_consistent(struct pfctl_rule *r, int anchor_call)
54003b3a8eb9SGleb Smirnoff {
54013b3a8eb9SGleb Smirnoff 	int	problems = 0;
54023b3a8eb9SGleb Smirnoff 
54033b3a8eb9SGleb Smirnoff 	switch (r->action) {
54043b3a8eb9SGleb Smirnoff 	case PF_PASS:
540539282ef3SKajetan Staszkiewicz 	case PF_MATCH:
54063b3a8eb9SGleb Smirnoff 	case PF_DROP:
54073b3a8eb9SGleb Smirnoff 	case PF_SCRUB:
54083b3a8eb9SGleb Smirnoff 	case PF_NOSCRUB:
54093b3a8eb9SGleb Smirnoff 		problems = filter_consistent(r, anchor_call);
54103b3a8eb9SGleb Smirnoff 		break;
54113b3a8eb9SGleb Smirnoff 	case PF_NAT:
54123b3a8eb9SGleb Smirnoff 	case PF_NONAT:
54133b3a8eb9SGleb Smirnoff 		problems = nat_consistent(r);
54143b3a8eb9SGleb Smirnoff 		break;
54153b3a8eb9SGleb Smirnoff 	case PF_RDR:
54163b3a8eb9SGleb Smirnoff 	case PF_NORDR:
54173b3a8eb9SGleb Smirnoff 		problems = rdr_consistent(r);
54183b3a8eb9SGleb Smirnoff 		break;
54193b3a8eb9SGleb Smirnoff 	case PF_BINAT:
54203b3a8eb9SGleb Smirnoff 	case PF_NOBINAT:
54213b3a8eb9SGleb Smirnoff 	default:
54223b3a8eb9SGleb Smirnoff 		break;
54233b3a8eb9SGleb Smirnoff 	}
54243b3a8eb9SGleb Smirnoff 	return (problems);
54253b3a8eb9SGleb Smirnoff }
54263b3a8eb9SGleb Smirnoff 
54273b3a8eb9SGleb Smirnoff int
5428e9eb0941SKristof Provost filter_consistent(struct pfctl_rule *r, int anchor_call)
54293b3a8eb9SGleb Smirnoff {
54303b3a8eb9SGleb Smirnoff 	int	problems = 0;
54313b3a8eb9SGleb Smirnoff 
54323b3a8eb9SGleb Smirnoff 	if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP &&
54330bd4a683SKristof Provost 	    r->proto != IPPROTO_SCTP &&
54343b3a8eb9SGleb Smirnoff 	    (r->src.port_op || r->dst.port_op)) {
54350bd4a683SKristof Provost 		yyerror("port only applies to tcp/udp/sctp");
54363b3a8eb9SGleb Smirnoff 		problems++;
54373b3a8eb9SGleb Smirnoff 	}
54383b3a8eb9SGleb Smirnoff 	if (r->proto != IPPROTO_ICMP && r->proto != IPPROTO_ICMPV6 &&
54393b3a8eb9SGleb Smirnoff 	    (r->type || r->code)) {
54403b3a8eb9SGleb Smirnoff 		yyerror("icmp-type/code only applies to icmp");
54413b3a8eb9SGleb Smirnoff 		problems++;
54423b3a8eb9SGleb Smirnoff 	}
54433b3a8eb9SGleb Smirnoff 	if (!r->af && (r->type || r->code)) {
54443b3a8eb9SGleb Smirnoff 		yyerror("must indicate address family with icmp-type/code");
54453b3a8eb9SGleb Smirnoff 		problems++;
54463b3a8eb9SGleb Smirnoff 	}
54473b3a8eb9SGleb Smirnoff 	if (r->overload_tblname[0] &&
54483b3a8eb9SGleb Smirnoff 	    r->max_src_conn == 0 && r->max_src_conn_rate.seconds == 0) {
54493b3a8eb9SGleb Smirnoff 		yyerror("'overload' requires 'max-src-conn' "
54503b3a8eb9SGleb Smirnoff 		    "or 'max-src-conn-rate'");
54513b3a8eb9SGleb Smirnoff 		problems++;
54523b3a8eb9SGleb Smirnoff 	}
54533b3a8eb9SGleb Smirnoff 	if ((r->proto == IPPROTO_ICMP && r->af == AF_INET6) ||
54543b3a8eb9SGleb Smirnoff 	    (r->proto == IPPROTO_ICMPV6 && r->af == AF_INET)) {
54553b3a8eb9SGleb Smirnoff 		yyerror("proto %s doesn't match address family %s",
54563b3a8eb9SGleb Smirnoff 		    r->proto == IPPROTO_ICMP ? "icmp" : "icmp6",
54573b3a8eb9SGleb Smirnoff 		    r->af == AF_INET ? "inet" : "inet6");
54583b3a8eb9SGleb Smirnoff 		problems++;
54593b3a8eb9SGleb Smirnoff 	}
54603b3a8eb9SGleb Smirnoff 	if (r->allow_opts && r->action != PF_PASS) {
54613b3a8eb9SGleb Smirnoff 		yyerror("allow-opts can only be specified for pass rules");
54623b3a8eb9SGleb Smirnoff 		problems++;
54633b3a8eb9SGleb Smirnoff 	}
54643b3a8eb9SGleb Smirnoff 	if (r->rule_flag & PFRULE_FRAGMENT && (r->src.port_op ||
54653b3a8eb9SGleb Smirnoff 	    r->dst.port_op || r->flagset || r->type || r->code)) {
54663b3a8eb9SGleb Smirnoff 		yyerror("fragments can be filtered only on IP header fields");
54673b3a8eb9SGleb Smirnoff 		problems++;
54683b3a8eb9SGleb Smirnoff 	}
54693b3a8eb9SGleb Smirnoff 	if (r->rule_flag & PFRULE_RETURNRST && r->proto != IPPROTO_TCP) {
54703b3a8eb9SGleb Smirnoff 		yyerror("return-rst can only be applied to TCP rules");
54713b3a8eb9SGleb Smirnoff 		problems++;
54723b3a8eb9SGleb Smirnoff 	}
54733b3a8eb9SGleb Smirnoff 	if (r->max_src_nodes && !(r->rule_flag & PFRULE_RULESRCTRACK)) {
54743b3a8eb9SGleb Smirnoff 		yyerror("max-src-nodes requires 'source-track rule'");
54753b3a8eb9SGleb Smirnoff 		problems++;
54763b3a8eb9SGleb Smirnoff 	}
547739282ef3SKajetan Staszkiewicz 	if (r->action != PF_PASS && r->keep_state) {
547839282ef3SKajetan Staszkiewicz 		yyerror("keep state is great, but only for pass rules");
54793b3a8eb9SGleb Smirnoff 		problems++;
54803b3a8eb9SGleb Smirnoff 	}
54813b3a8eb9SGleb Smirnoff 	if (r->rule_flag & PFRULE_STATESLOPPY &&
54823b3a8eb9SGleb Smirnoff 	    (r->keep_state == PF_STATE_MODULATE ||
54833b3a8eb9SGleb Smirnoff 	    r->keep_state == PF_STATE_SYNPROXY)) {
54843b3a8eb9SGleb Smirnoff 		yyerror("sloppy state matching cannot be used with "
54853b3a8eb9SGleb Smirnoff 		    "synproxy state or modulate state");
54863b3a8eb9SGleb Smirnoff 		problems++;
54873b3a8eb9SGleb Smirnoff 	}
548839282ef3SKajetan Staszkiewicz 	/* match rules rules */
548939282ef3SKajetan Staszkiewicz 	if (r->action == PF_MATCH) {
549039282ef3SKajetan Staszkiewicz 		if (r->divert.port) {
549139282ef3SKajetan Staszkiewicz 			yyerror("divert is not supported on match rules");
549239282ef3SKajetan Staszkiewicz 			problems++;
549339282ef3SKajetan Staszkiewicz 		}
549439282ef3SKajetan Staszkiewicz 		if (r->rt) {
549539282ef3SKajetan Staszkiewicz 			yyerror("route-to, reply-to, dup-to and fastroute "
549639282ef3SKajetan Staszkiewicz 			   "must not be used on match rules");
549739282ef3SKajetan Staszkiewicz 			problems++;
549839282ef3SKajetan Staszkiewicz 		}
5499*aa69fdf1SKristof Provost 		if (r->naf != r->af) {
5500*aa69fdf1SKristof Provost 			yyerror("af-to is not supported on match rules");
5501*aa69fdf1SKristof Provost 			problems++;
5502*aa69fdf1SKristof Provost 		}
550339282ef3SKajetan Staszkiewicz 	}
5504788f194fSKajetan Staszkiewicz 	if (r->rpool.opts & PF_POOL_STICKYADDR && !r->keep_state) {
5505788f194fSKajetan Staszkiewicz 		yyerror("'sticky-address' requires 'keep state'");
5506788f194fSKajetan Staszkiewicz 		problems++;
5507788f194fSKajetan Staszkiewicz 	}
55083b3a8eb9SGleb Smirnoff 	return (-problems);
55093b3a8eb9SGleb Smirnoff }
55103b3a8eb9SGleb Smirnoff 
55113b3a8eb9SGleb Smirnoff int
5512e9eb0941SKristof Provost nat_consistent(struct pfctl_rule *r)
55133b3a8eb9SGleb Smirnoff {
55143b3a8eb9SGleb Smirnoff 	return (0);	/* yeah! */
55153b3a8eb9SGleb Smirnoff }
55163b3a8eb9SGleb Smirnoff 
55173b3a8eb9SGleb Smirnoff int
5518e9eb0941SKristof Provost rdr_consistent(struct pfctl_rule *r)
55193b3a8eb9SGleb Smirnoff {
55203b3a8eb9SGleb Smirnoff 	int			 problems = 0;
55213b3a8eb9SGleb Smirnoff 
55220bd4a683SKristof Provost 	if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP &&
55230bd4a683SKristof Provost 	    r->proto != IPPROTO_SCTP) {
55243b3a8eb9SGleb Smirnoff 		if (r->src.port_op) {
55250bd4a683SKristof Provost 			yyerror("src port only applies to tcp/udp/sctp");
55263b3a8eb9SGleb Smirnoff 			problems++;
55273b3a8eb9SGleb Smirnoff 		}
55283b3a8eb9SGleb Smirnoff 		if (r->dst.port_op) {
55290bd4a683SKristof Provost 			yyerror("dst port only applies to tcp/udp/sctp");
55303b3a8eb9SGleb Smirnoff 			problems++;
55313b3a8eb9SGleb Smirnoff 		}
55323b3a8eb9SGleb Smirnoff 		if (r->rpool.proxy_port[0]) {
55330bd4a683SKristof Provost 			yyerror("rpool port only applies to tcp/udp/sctp");
55343b3a8eb9SGleb Smirnoff 			problems++;
55353b3a8eb9SGleb Smirnoff 		}
55363b3a8eb9SGleb Smirnoff 	}
55373b3a8eb9SGleb Smirnoff 	if (r->dst.port_op &&
55383b3a8eb9SGleb Smirnoff 	    r->dst.port_op != PF_OP_EQ && r->dst.port_op != PF_OP_RRG) {
55393b3a8eb9SGleb Smirnoff 		yyerror("invalid port operator for rdr destination port");
55403b3a8eb9SGleb Smirnoff 		problems++;
55413b3a8eb9SGleb Smirnoff 	}
55423b3a8eb9SGleb Smirnoff 	return (-problems);
55433b3a8eb9SGleb Smirnoff }
55443b3a8eb9SGleb Smirnoff 
55453b3a8eb9SGleb Smirnoff int
55463b3a8eb9SGleb Smirnoff process_tabledef(char *name, struct table_opts *opts)
55473b3a8eb9SGleb Smirnoff {
55483b3a8eb9SGleb Smirnoff 	struct pfr_buffer	 ab;
55493b3a8eb9SGleb Smirnoff 	struct node_tinit	*ti;
5550542feeffSKristof Provost 	unsigned long		 maxcount;
5551542feeffSKristof Provost 	size_t			 s = sizeof(maxcount);
55523b3a8eb9SGleb Smirnoff 
55533b3a8eb9SGleb Smirnoff 	bzero(&ab, sizeof(ab));
55543b3a8eb9SGleb Smirnoff 	ab.pfrb_type = PFRB_ADDRS;
55553b3a8eb9SGleb Smirnoff 	SIMPLEQ_FOREACH(ti, &opts->init_nodes, entries) {
55563b3a8eb9SGleb Smirnoff 		if (ti->file)
55573b3a8eb9SGleb Smirnoff 			if (pfr_buf_load(&ab, ti->file, 0, append_addr)) {
55583b3a8eb9SGleb Smirnoff 				if (errno)
55593b3a8eb9SGleb Smirnoff 					yyerror("cannot load \"%s\": %s",
55603b3a8eb9SGleb Smirnoff 					    ti->file, strerror(errno));
55613b3a8eb9SGleb Smirnoff 				else
55623b3a8eb9SGleb Smirnoff 					yyerror("file \"%s\" contains bad data",
55633b3a8eb9SGleb Smirnoff 					    ti->file);
55643b3a8eb9SGleb Smirnoff 				goto _error;
55653b3a8eb9SGleb Smirnoff 			}
55663b3a8eb9SGleb Smirnoff 		if (ti->host)
55673b3a8eb9SGleb Smirnoff 			if (append_addr_host(&ab, ti->host, 0, 0)) {
55683b3a8eb9SGleb Smirnoff 				yyerror("cannot create address buffer: %s",
55693b3a8eb9SGleb Smirnoff 				    strerror(errno));
55703b3a8eb9SGleb Smirnoff 				goto _error;
55713b3a8eb9SGleb Smirnoff 			}
55723b3a8eb9SGleb Smirnoff 	}
55733b3a8eb9SGleb Smirnoff 	if (pf->opts & PF_OPT_VERBOSE)
55743b3a8eb9SGleb Smirnoff 		print_tabledef(name, opts->flags, opts->init_addr,
55753b3a8eb9SGleb Smirnoff 		    &opts->init_nodes);
55763b3a8eb9SGleb Smirnoff 	if (!(pf->opts & PF_OPT_NOACTION) &&
55773b3a8eb9SGleb Smirnoff 	    pfctl_define_table(name, opts->flags, opts->init_addr,
55783b3a8eb9SGleb Smirnoff 	    pf->anchor->name, &ab, pf->anchor->ruleset.tticket)) {
5579542feeffSKristof Provost 
5580542feeffSKristof Provost 		if (sysctlbyname("net.pf.request_maxcount", &maxcount, &s,
5581542feeffSKristof Provost 		    NULL, 0) == -1)
5582542feeffSKristof Provost 			maxcount = 65535;
5583542feeffSKristof Provost 
5584542feeffSKristof Provost 		if (ab.pfrb_size > maxcount)
5585542feeffSKristof Provost 			yyerror("cannot define table %s: too many elements.\n"
5586542feeffSKristof Provost 			    "Consider increasing net.pf.request_maxcount.",
5587542feeffSKristof Provost 			    name);
5588542feeffSKristof Provost 		else
55893b3a8eb9SGleb Smirnoff 			yyerror("cannot define table %s: %s", name,
55903b3a8eb9SGleb Smirnoff 			    pfr_strerror(errno));
5591542feeffSKristof Provost 
55923b3a8eb9SGleb Smirnoff 		goto _error;
55933b3a8eb9SGleb Smirnoff 	}
55943b3a8eb9SGleb Smirnoff 	pf->tdirty = 1;
55953b3a8eb9SGleb Smirnoff 	pfr_buf_clear(&ab);
55963b3a8eb9SGleb Smirnoff 	return (0);
55973b3a8eb9SGleb Smirnoff _error:
55983b3a8eb9SGleb Smirnoff 	pfr_buf_clear(&ab);
55993b3a8eb9SGleb Smirnoff 	return (-1);
56003b3a8eb9SGleb Smirnoff }
56013b3a8eb9SGleb Smirnoff 
56023b3a8eb9SGleb Smirnoff struct keywords {
56033b3a8eb9SGleb Smirnoff 	const char	*k_name;
56043b3a8eb9SGleb Smirnoff 	int		 k_val;
56053b3a8eb9SGleb Smirnoff };
56063b3a8eb9SGleb Smirnoff 
56073b3a8eb9SGleb Smirnoff /* macro gore, but you should've seen the prior indentation nightmare... */
56083b3a8eb9SGleb Smirnoff 
56093b3a8eb9SGleb Smirnoff #define FREE_LIST(T,r) \
56103b3a8eb9SGleb Smirnoff 	do { \
56113b3a8eb9SGleb Smirnoff 		T *p, *node = r; \
56123b3a8eb9SGleb Smirnoff 		while (node != NULL) { \
56133b3a8eb9SGleb Smirnoff 			p = node; \
56143b3a8eb9SGleb Smirnoff 			node = node->next; \
56153b3a8eb9SGleb Smirnoff 			free(p); \
56163b3a8eb9SGleb Smirnoff 		} \
56173b3a8eb9SGleb Smirnoff 	} while (0)
56183b3a8eb9SGleb Smirnoff 
56193b3a8eb9SGleb Smirnoff #define LOOP_THROUGH(T,n,r,C) \
56203b3a8eb9SGleb Smirnoff 	do { \
56213b3a8eb9SGleb Smirnoff 		T *n; \
56223b3a8eb9SGleb Smirnoff 		if (r == NULL) { \
56233b3a8eb9SGleb Smirnoff 			r = calloc(1, sizeof(T)); \
56243b3a8eb9SGleb Smirnoff 			if (r == NULL) \
56253b3a8eb9SGleb Smirnoff 				err(1, "LOOP: calloc"); \
56263b3a8eb9SGleb Smirnoff 			r->next = NULL; \
56273b3a8eb9SGleb Smirnoff 		} \
56283b3a8eb9SGleb Smirnoff 		n = r; \
56293b3a8eb9SGleb Smirnoff 		while (n != NULL) { \
56303b3a8eb9SGleb Smirnoff 			do { \
56313b3a8eb9SGleb Smirnoff 				C; \
56323b3a8eb9SGleb Smirnoff 			} while (0); \
56333b3a8eb9SGleb Smirnoff 			n = n->next; \
56343b3a8eb9SGleb Smirnoff 		} \
56353b3a8eb9SGleb Smirnoff 	} while (0)
56363b3a8eb9SGleb Smirnoff 
56373b3a8eb9SGleb Smirnoff void
56383b3a8eb9SGleb Smirnoff expand_label_str(char *label, size_t len, const char *srch, const char *repl)
56393b3a8eb9SGleb Smirnoff {
56403b3a8eb9SGleb Smirnoff 	char *tmp;
56413b3a8eb9SGleb Smirnoff 	char *p, *q;
56423b3a8eb9SGleb Smirnoff 
56433b3a8eb9SGleb Smirnoff 	if ((tmp = calloc(1, len)) == NULL)
56443b3a8eb9SGleb Smirnoff 		err(1, "expand_label_str: calloc");
56453b3a8eb9SGleb Smirnoff 	p = q = label;
56463b3a8eb9SGleb Smirnoff 	while ((q = strstr(p, srch)) != NULL) {
56473b3a8eb9SGleb Smirnoff 		*q = '\0';
56483b3a8eb9SGleb Smirnoff 		if ((strlcat(tmp, p, len) >= len) ||
56493b3a8eb9SGleb Smirnoff 		    (strlcat(tmp, repl, len) >= len))
56503b3a8eb9SGleb Smirnoff 			errx(1, "expand_label: label too long");
56513b3a8eb9SGleb Smirnoff 		q += strlen(srch);
56523b3a8eb9SGleb Smirnoff 		p = q;
56533b3a8eb9SGleb Smirnoff 	}
56543b3a8eb9SGleb Smirnoff 	if (strlcat(tmp, p, len) >= len)
56553b3a8eb9SGleb Smirnoff 		errx(1, "expand_label: label too long");
56563b3a8eb9SGleb Smirnoff 	strlcpy(label, tmp, len);	/* always fits */
56573b3a8eb9SGleb Smirnoff 	free(tmp);
56583b3a8eb9SGleb Smirnoff }
56593b3a8eb9SGleb Smirnoff 
56603b3a8eb9SGleb Smirnoff void
56613b3a8eb9SGleb Smirnoff expand_label_if(const char *name, char *label, size_t len, const char *ifname)
56623b3a8eb9SGleb Smirnoff {
56633b3a8eb9SGleb Smirnoff 	if (strstr(label, name) != NULL) {
56643b3a8eb9SGleb Smirnoff 		if (!*ifname)
56653b3a8eb9SGleb Smirnoff 			expand_label_str(label, len, name, "any");
56663b3a8eb9SGleb Smirnoff 		else
56673b3a8eb9SGleb Smirnoff 			expand_label_str(label, len, name, ifname);
56683b3a8eb9SGleb Smirnoff 	}
56693b3a8eb9SGleb Smirnoff }
56703b3a8eb9SGleb Smirnoff 
56713b3a8eb9SGleb Smirnoff void
56723b3a8eb9SGleb Smirnoff expand_label_addr(const char *name, char *label, size_t len, sa_family_t af,
567309c7f238SKristof Provost     struct pf_rule_addr *addr)
56743b3a8eb9SGleb Smirnoff {
56753b3a8eb9SGleb Smirnoff 	char tmp[64], tmp_not[66];
56763b3a8eb9SGleb Smirnoff 
56773b3a8eb9SGleb Smirnoff 	if (strstr(label, name) != NULL) {
567809c7f238SKristof Provost 		switch (addr->addr.type) {
56793b3a8eb9SGleb Smirnoff 		case PF_ADDR_DYNIFTL:
568009c7f238SKristof Provost 			snprintf(tmp, sizeof(tmp), "(%s)", addr->addr.v.ifname);
56813b3a8eb9SGleb Smirnoff 			break;
56823b3a8eb9SGleb Smirnoff 		case PF_ADDR_TABLE:
568309c7f238SKristof Provost 			snprintf(tmp, sizeof(tmp), "<%s>", addr->addr.v.tblname);
56843b3a8eb9SGleb Smirnoff 			break;
56853b3a8eb9SGleb Smirnoff 		case PF_ADDR_NOROUTE:
56863b3a8eb9SGleb Smirnoff 			snprintf(tmp, sizeof(tmp), "no-route");
56873b3a8eb9SGleb Smirnoff 			break;
56883b3a8eb9SGleb Smirnoff 		case PF_ADDR_URPFFAILED:
56893b3a8eb9SGleb Smirnoff 			snprintf(tmp, sizeof(tmp), "urpf-failed");
56903b3a8eb9SGleb Smirnoff 			break;
56913b3a8eb9SGleb Smirnoff 		case PF_ADDR_ADDRMASK:
569209c7f238SKristof Provost 			if (!af || (PF_AZERO(&addr->addr.v.a.addr, af) &&
569309c7f238SKristof Provost 			    PF_AZERO(&addr->addr.v.a.mask, af)))
56943b3a8eb9SGleb Smirnoff 				snprintf(tmp, sizeof(tmp), "any");
56953b3a8eb9SGleb Smirnoff 			else {
56963b3a8eb9SGleb Smirnoff 				char	a[48];
56973b3a8eb9SGleb Smirnoff 				int	bits;
56983b3a8eb9SGleb Smirnoff 
569909c7f238SKristof Provost 				if (inet_ntop(af, &addr->addr.v.a.addr, a,
57003b3a8eb9SGleb Smirnoff 				    sizeof(a)) == NULL)
57013b3a8eb9SGleb Smirnoff 					snprintf(tmp, sizeof(tmp), "?");
57023b3a8eb9SGleb Smirnoff 				else {
570309c7f238SKristof Provost 					bits = unmask(&addr->addr.v.a.mask, af);
57043b3a8eb9SGleb Smirnoff 					if ((af == AF_INET && bits < 32) ||
57053b3a8eb9SGleb Smirnoff 					    (af == AF_INET6 && bits < 128))
57063b3a8eb9SGleb Smirnoff 						snprintf(tmp, sizeof(tmp),
57073b3a8eb9SGleb Smirnoff 						    "%s/%d", a, bits);
57083b3a8eb9SGleb Smirnoff 					else
57093b3a8eb9SGleb Smirnoff 						snprintf(tmp, sizeof(tmp),
57103b3a8eb9SGleb Smirnoff 						    "%s", a);
57113b3a8eb9SGleb Smirnoff 				}
57123b3a8eb9SGleb Smirnoff 			}
57133b3a8eb9SGleb Smirnoff 			break;
57143b3a8eb9SGleb Smirnoff 		default:
57153b3a8eb9SGleb Smirnoff 			snprintf(tmp, sizeof(tmp), "?");
57163b3a8eb9SGleb Smirnoff 			break;
57173b3a8eb9SGleb Smirnoff 		}
57183b3a8eb9SGleb Smirnoff 
571909c7f238SKristof Provost 		if (addr->neg) {
57203b3a8eb9SGleb Smirnoff 			snprintf(tmp_not, sizeof(tmp_not), "! %s", tmp);
57213b3a8eb9SGleb Smirnoff 			expand_label_str(label, len, name, tmp_not);
57223b3a8eb9SGleb Smirnoff 		} else
57233b3a8eb9SGleb Smirnoff 			expand_label_str(label, len, name, tmp);
57243b3a8eb9SGleb Smirnoff 	}
57253b3a8eb9SGleb Smirnoff }
57263b3a8eb9SGleb Smirnoff 
57273b3a8eb9SGleb Smirnoff void
57283b3a8eb9SGleb Smirnoff expand_label_port(const char *name, char *label, size_t len,
572909c7f238SKristof Provost     struct pf_rule_addr *addr)
57303b3a8eb9SGleb Smirnoff {
57313b3a8eb9SGleb Smirnoff 	char	 a1[6], a2[6], op[13] = "";
57323b3a8eb9SGleb Smirnoff 
57333b3a8eb9SGleb Smirnoff 	if (strstr(label, name) != NULL) {
573409c7f238SKristof Provost 		snprintf(a1, sizeof(a1), "%u", ntohs(addr->port[0]));
573509c7f238SKristof Provost 		snprintf(a2, sizeof(a2), "%u", ntohs(addr->port[1]));
573609c7f238SKristof Provost 		if (!addr->port_op)
57373b3a8eb9SGleb Smirnoff 			;
573809c7f238SKristof Provost 		else if (addr->port_op == PF_OP_IRG)
57393b3a8eb9SGleb Smirnoff 			snprintf(op, sizeof(op), "%s><%s", a1, a2);
574009c7f238SKristof Provost 		else if (addr->port_op == PF_OP_XRG)
57413b3a8eb9SGleb Smirnoff 			snprintf(op, sizeof(op), "%s<>%s", a1, a2);
574209c7f238SKristof Provost 		else if (addr->port_op == PF_OP_EQ)
57433b3a8eb9SGleb Smirnoff 			snprintf(op, sizeof(op), "%s", a1);
574409c7f238SKristof Provost 		else if (addr->port_op == PF_OP_NE)
57453b3a8eb9SGleb Smirnoff 			snprintf(op, sizeof(op), "!=%s", a1);
574609c7f238SKristof Provost 		else if (addr->port_op == PF_OP_LT)
57473b3a8eb9SGleb Smirnoff 			snprintf(op, sizeof(op), "<%s", a1);
574809c7f238SKristof Provost 		else if (addr->port_op == PF_OP_LE)
57493b3a8eb9SGleb Smirnoff 			snprintf(op, sizeof(op), "<=%s", a1);
575009c7f238SKristof Provost 		else if (addr->port_op == PF_OP_GT)
57513b3a8eb9SGleb Smirnoff 			snprintf(op, sizeof(op), ">%s", a1);
575209c7f238SKristof Provost 		else if (addr->port_op == PF_OP_GE)
57533b3a8eb9SGleb Smirnoff 			snprintf(op, sizeof(op), ">=%s", a1);
57543b3a8eb9SGleb Smirnoff 		expand_label_str(label, len, name, op);
57553b3a8eb9SGleb Smirnoff 	}
57563b3a8eb9SGleb Smirnoff }
57573b3a8eb9SGleb Smirnoff 
57583b3a8eb9SGleb Smirnoff void
57593b3a8eb9SGleb Smirnoff expand_label_proto(const char *name, char *label, size_t len, u_int8_t proto)
57603b3a8eb9SGleb Smirnoff {
5761858937beSMateusz Guzik 	const char *protoname;
57623b3a8eb9SGleb Smirnoff 	char n[4];
57633b3a8eb9SGleb Smirnoff 
57643b3a8eb9SGleb Smirnoff 	if (strstr(label, name) != NULL) {
5765858937beSMateusz Guzik 		protoname = pfctl_proto2name(proto);
5766858937beSMateusz Guzik 		if (protoname != NULL)
5767858937beSMateusz Guzik 			expand_label_str(label, len, name, protoname);
57683b3a8eb9SGleb Smirnoff 		else {
57693b3a8eb9SGleb Smirnoff 			snprintf(n, sizeof(n), "%u", proto);
57703b3a8eb9SGleb Smirnoff 			expand_label_str(label, len, name, n);
57713b3a8eb9SGleb Smirnoff 		}
57723b3a8eb9SGleb Smirnoff 	}
57733b3a8eb9SGleb Smirnoff }
57743b3a8eb9SGleb Smirnoff 
57753b3a8eb9SGleb Smirnoff void
577609c7f238SKristof Provost expand_label_nr(const char *name, char *label, size_t len,
577709c7f238SKristof Provost     struct pfctl_rule *r)
57783b3a8eb9SGleb Smirnoff {
57793b3a8eb9SGleb Smirnoff 	char n[11];
57803b3a8eb9SGleb Smirnoff 
57813b3a8eb9SGleb Smirnoff 	if (strstr(label, name) != NULL) {
578209c7f238SKristof Provost 		snprintf(n, sizeof(n), "%u", r->nr);
57833b3a8eb9SGleb Smirnoff 		expand_label_str(label, len, name, n);
57843b3a8eb9SGleb Smirnoff 	}
57853b3a8eb9SGleb Smirnoff }
57863b3a8eb9SGleb Smirnoff 
57873b3a8eb9SGleb Smirnoff void
578809c7f238SKristof Provost expand_label(char *label, size_t len, struct pfctl_rule *r)
57893b3a8eb9SGleb Smirnoff {
579009c7f238SKristof Provost 	expand_label_if("$if", label, len, r->ifname);
579109c7f238SKristof Provost 	expand_label_addr("$srcaddr", label, len, r->af, &r->src);
579209c7f238SKristof Provost 	expand_label_addr("$dstaddr", label, len, r->af, &r->dst);
579309c7f238SKristof Provost 	expand_label_port("$srcport", label, len, &r->src);
579409c7f238SKristof Provost 	expand_label_port("$dstport", label, len, &r->dst);
579509c7f238SKristof Provost 	expand_label_proto("$proto", label, len, r->proto);
579609c7f238SKristof Provost 	expand_label_nr("$nr", label, len, r);
57973b3a8eb9SGleb Smirnoff }
57983b3a8eb9SGleb Smirnoff 
57993b3a8eb9SGleb Smirnoff int
58003b3a8eb9SGleb Smirnoff expand_altq(struct pf_altq *a, struct node_if *interfaces,
58013b3a8eb9SGleb Smirnoff     struct node_queue *nqueues, struct node_queue_bw bwspec,
58023b3a8eb9SGleb Smirnoff     struct node_queue_opt *opts)
58033b3a8eb9SGleb Smirnoff {
58043b3a8eb9SGleb Smirnoff 	struct pf_altq		 pa, pb;
58053b3a8eb9SGleb Smirnoff 	char			 qname[PF_QNAME_SIZE];
58063b3a8eb9SGleb Smirnoff 	struct node_queue	*n;
58073b3a8eb9SGleb Smirnoff 	struct node_queue_bw	 bw;
58083b3a8eb9SGleb Smirnoff 	int			 errs = 0;
58093b3a8eb9SGleb Smirnoff 
58103b3a8eb9SGleb Smirnoff 	if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
58113b3a8eb9SGleb Smirnoff 		FREE_LIST(struct node_if, interfaces);
58120a70aaf8SLuiz Otavio O Souza 		if (nqueues)
58133b3a8eb9SGleb Smirnoff 			FREE_LIST(struct node_queue, nqueues);
58143b3a8eb9SGleb Smirnoff 		return (0);
58153b3a8eb9SGleb Smirnoff 	}
58163b3a8eb9SGleb Smirnoff 
58173b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_if, interface, interfaces,
58183b3a8eb9SGleb Smirnoff 		memcpy(&pa, a, sizeof(struct pf_altq));
58193b3a8eb9SGleb Smirnoff 		if (strlcpy(pa.ifname, interface->ifname,
58203b3a8eb9SGleb Smirnoff 		    sizeof(pa.ifname)) >= sizeof(pa.ifname))
58213b3a8eb9SGleb Smirnoff 			errx(1, "expand_altq: strlcpy");
58223b3a8eb9SGleb Smirnoff 
58233b3a8eb9SGleb Smirnoff 		if (interface->not) {
58243b3a8eb9SGleb Smirnoff 			yyerror("altq on ! <interface> is not supported");
58253b3a8eb9SGleb Smirnoff 			errs++;
58263b3a8eb9SGleb Smirnoff 		} else {
58273b3a8eb9SGleb Smirnoff 			if (eval_pfaltq(pf, &pa, &bwspec, opts))
58283b3a8eb9SGleb Smirnoff 				errs++;
58293b3a8eb9SGleb Smirnoff 			else
58303b3a8eb9SGleb Smirnoff 				if (pfctl_add_altq(pf, &pa))
58313b3a8eb9SGleb Smirnoff 					errs++;
58323b3a8eb9SGleb Smirnoff 
58333b3a8eb9SGleb Smirnoff 			if (pf->opts & PF_OPT_VERBOSE) {
58343b3a8eb9SGleb Smirnoff 				print_altq(&pf->paltq->altq, 0,
58353b3a8eb9SGleb Smirnoff 				    &bwspec, opts);
58363b3a8eb9SGleb Smirnoff 				if (nqueues && nqueues->tail) {
58373b3a8eb9SGleb Smirnoff 					printf("queue { ");
58383b3a8eb9SGleb Smirnoff 					LOOP_THROUGH(struct node_queue, queue,
58393b3a8eb9SGleb Smirnoff 					    nqueues,
58403b3a8eb9SGleb Smirnoff 						printf("%s ",
58413b3a8eb9SGleb Smirnoff 						    queue->queue);
58423b3a8eb9SGleb Smirnoff 					);
58433b3a8eb9SGleb Smirnoff 					printf("}");
58443b3a8eb9SGleb Smirnoff 				}
58453b3a8eb9SGleb Smirnoff 				printf("\n");
58463b3a8eb9SGleb Smirnoff 			}
58473b3a8eb9SGleb Smirnoff 
58483b3a8eb9SGleb Smirnoff 			if (pa.scheduler == ALTQT_CBQ ||
5849dc784287SKristof Provost 			    pa.scheduler == ALTQT_HFSC ||
5850dc784287SKristof Provost 			    pa.scheduler == ALTQT_FAIRQ) {
58513b3a8eb9SGleb Smirnoff 				/* now create a root queue */
58523b3a8eb9SGleb Smirnoff 				memset(&pb, 0, sizeof(struct pf_altq));
58533b3a8eb9SGleb Smirnoff 				if (strlcpy(qname, "root_", sizeof(qname)) >=
58543b3a8eb9SGleb Smirnoff 				    sizeof(qname))
58553b3a8eb9SGleb Smirnoff 					errx(1, "expand_altq: strlcpy");
58563b3a8eb9SGleb Smirnoff 				if (strlcat(qname, interface->ifname,
58573b3a8eb9SGleb Smirnoff 				    sizeof(qname)) >= sizeof(qname))
58583b3a8eb9SGleb Smirnoff 					errx(1, "expand_altq: strlcat");
58593b3a8eb9SGleb Smirnoff 				if (strlcpy(pb.qname, qname,
58603b3a8eb9SGleb Smirnoff 				    sizeof(pb.qname)) >= sizeof(pb.qname))
58613b3a8eb9SGleb Smirnoff 					errx(1, "expand_altq: strlcpy");
58623b3a8eb9SGleb Smirnoff 				if (strlcpy(pb.ifname, interface->ifname,
58633b3a8eb9SGleb Smirnoff 				    sizeof(pb.ifname)) >= sizeof(pb.ifname))
58643b3a8eb9SGleb Smirnoff 					errx(1, "expand_altq: strlcpy");
58653b3a8eb9SGleb Smirnoff 				pb.qlimit = pa.qlimit;
58663b3a8eb9SGleb Smirnoff 				pb.scheduler = pa.scheduler;
58673b3a8eb9SGleb Smirnoff 				bw.bw_absolute = pa.ifbandwidth;
58683b3a8eb9SGleb Smirnoff 				bw.bw_percent = 0;
58693b3a8eb9SGleb Smirnoff 				if (eval_pfqueue(pf, &pb, &bw, opts))
58703b3a8eb9SGleb Smirnoff 					errs++;
58713b3a8eb9SGleb Smirnoff 				else
58723b3a8eb9SGleb Smirnoff 					if (pfctl_add_altq(pf, &pb))
58733b3a8eb9SGleb Smirnoff 						errs++;
58743b3a8eb9SGleb Smirnoff 			}
58753b3a8eb9SGleb Smirnoff 
58763b3a8eb9SGleb Smirnoff 			LOOP_THROUGH(struct node_queue, queue, nqueues,
58773b3a8eb9SGleb Smirnoff 				n = calloc(1, sizeof(struct node_queue));
58783b3a8eb9SGleb Smirnoff 				if (n == NULL)
58793b3a8eb9SGleb Smirnoff 					err(1, "expand_altq: calloc");
58803b3a8eb9SGleb Smirnoff 				if (pa.scheduler == ALTQT_CBQ ||
5881dc784287SKristof Provost 				    pa.scheduler == ALTQT_HFSC ||
5882dc784287SKristof Provost 				    pa.scheduler == ALTQT_FAIRQ)
58833b3a8eb9SGleb Smirnoff 					if (strlcpy(n->parent, qname,
58843b3a8eb9SGleb Smirnoff 					    sizeof(n->parent)) >=
58853b3a8eb9SGleb Smirnoff 					    sizeof(n->parent))
58863b3a8eb9SGleb Smirnoff 						errx(1, "expand_altq: strlcpy");
58873b3a8eb9SGleb Smirnoff 				if (strlcpy(n->queue, queue->queue,
58883b3a8eb9SGleb Smirnoff 				    sizeof(n->queue)) >= sizeof(n->queue))
58893b3a8eb9SGleb Smirnoff 					errx(1, "expand_altq: strlcpy");
58903b3a8eb9SGleb Smirnoff 				if (strlcpy(n->ifname, interface->ifname,
58913b3a8eb9SGleb Smirnoff 				    sizeof(n->ifname)) >= sizeof(n->ifname))
58923b3a8eb9SGleb Smirnoff 					errx(1, "expand_altq: strlcpy");
58933b3a8eb9SGleb Smirnoff 				n->scheduler = pa.scheduler;
58943b3a8eb9SGleb Smirnoff 				n->next = NULL;
58953b3a8eb9SGleb Smirnoff 				n->tail = n;
58963b3a8eb9SGleb Smirnoff 				if (queues == NULL)
58973b3a8eb9SGleb Smirnoff 					queues = n;
58983b3a8eb9SGleb Smirnoff 				else {
58993b3a8eb9SGleb Smirnoff 					queues->tail->next = n;
59003b3a8eb9SGleb Smirnoff 					queues->tail = n;
59013b3a8eb9SGleb Smirnoff 				}
59023b3a8eb9SGleb Smirnoff 			);
59033b3a8eb9SGleb Smirnoff 		}
59043b3a8eb9SGleb Smirnoff 	);
59053b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_if, interfaces);
59060a70aaf8SLuiz Otavio O Souza 	if (nqueues)
59073b3a8eb9SGleb Smirnoff 		FREE_LIST(struct node_queue, nqueues);
59083b3a8eb9SGleb Smirnoff 
59093b3a8eb9SGleb Smirnoff 	return (errs);
59103b3a8eb9SGleb Smirnoff }
59113b3a8eb9SGleb Smirnoff 
59123b3a8eb9SGleb Smirnoff int
59133b3a8eb9SGleb Smirnoff expand_queue(struct pf_altq *a, struct node_if *interfaces,
59143b3a8eb9SGleb Smirnoff     struct node_queue *nqueues, struct node_queue_bw bwspec,
59153b3a8eb9SGleb Smirnoff     struct node_queue_opt *opts)
59163b3a8eb9SGleb Smirnoff {
59173b3a8eb9SGleb Smirnoff 	struct node_queue	*n, *nq;
59183b3a8eb9SGleb Smirnoff 	struct pf_altq		 pa;
59193b3a8eb9SGleb Smirnoff 	u_int8_t		 found = 0;
59203b3a8eb9SGleb Smirnoff 	u_int8_t		 errs = 0;
59213b3a8eb9SGleb Smirnoff 
59223b3a8eb9SGleb Smirnoff 	if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
59233b3a8eb9SGleb Smirnoff 		FREE_LIST(struct node_queue, nqueues);
59243b3a8eb9SGleb Smirnoff 		return (0);
59253b3a8eb9SGleb Smirnoff 	}
59263b3a8eb9SGleb Smirnoff 
59273b3a8eb9SGleb Smirnoff 	if (queues == NULL) {
59283b3a8eb9SGleb Smirnoff 		yyerror("queue %s has no parent", a->qname);
59293b3a8eb9SGleb Smirnoff 		FREE_LIST(struct node_queue, nqueues);
59303b3a8eb9SGleb Smirnoff 		return (1);
59313b3a8eb9SGleb Smirnoff 	}
59323b3a8eb9SGleb Smirnoff 
59333b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_if, interface, interfaces,
59343b3a8eb9SGleb Smirnoff 		LOOP_THROUGH(struct node_queue, tqueue, queues,
59353b3a8eb9SGleb Smirnoff 			if (!strncmp(a->qname, tqueue->queue, PF_QNAME_SIZE) &&
59363b3a8eb9SGleb Smirnoff 			    (interface->ifname[0] == 0 ||
59373b3a8eb9SGleb Smirnoff 			    (!interface->not && !strncmp(interface->ifname,
59383b3a8eb9SGleb Smirnoff 			    tqueue->ifname, IFNAMSIZ)) ||
59393b3a8eb9SGleb Smirnoff 			    (interface->not && strncmp(interface->ifname,
59403b3a8eb9SGleb Smirnoff 			    tqueue->ifname, IFNAMSIZ)))) {
59413b3a8eb9SGleb Smirnoff 				/* found ourself in queues */
59423b3a8eb9SGleb Smirnoff 				found++;
59433b3a8eb9SGleb Smirnoff 
59443b3a8eb9SGleb Smirnoff 				memcpy(&pa, a, sizeof(struct pf_altq));
59453b3a8eb9SGleb Smirnoff 
59463b3a8eb9SGleb Smirnoff 				if (pa.scheduler != ALTQT_NONE &&
59473b3a8eb9SGleb Smirnoff 				    pa.scheduler != tqueue->scheduler) {
59483b3a8eb9SGleb Smirnoff 					yyerror("exactly one scheduler type "
59493b3a8eb9SGleb Smirnoff 					    "per interface allowed");
59503b3a8eb9SGleb Smirnoff 					return (1);
59513b3a8eb9SGleb Smirnoff 				}
59523b3a8eb9SGleb Smirnoff 				pa.scheduler = tqueue->scheduler;
59533b3a8eb9SGleb Smirnoff 
59543b3a8eb9SGleb Smirnoff 				/* scheduler dependent error checking */
59553b3a8eb9SGleb Smirnoff 				switch (pa.scheduler) {
59563b3a8eb9SGleb Smirnoff 				case ALTQT_PRIQ:
59573b3a8eb9SGleb Smirnoff 					if (nqueues != NULL) {
59583b3a8eb9SGleb Smirnoff 						yyerror("priq queues cannot "
59593b3a8eb9SGleb Smirnoff 						    "have child queues");
59603b3a8eb9SGleb Smirnoff 						return (1);
59613b3a8eb9SGleb Smirnoff 					}
59623b3a8eb9SGleb Smirnoff 					if (bwspec.bw_absolute > 0 ||
59633b3a8eb9SGleb Smirnoff 					    bwspec.bw_percent < 100) {
59643b3a8eb9SGleb Smirnoff 						yyerror("priq doesn't take "
59653b3a8eb9SGleb Smirnoff 						    "bandwidth");
59663b3a8eb9SGleb Smirnoff 						return (1);
59673b3a8eb9SGleb Smirnoff 					}
59683b3a8eb9SGleb Smirnoff 					break;
59693b3a8eb9SGleb Smirnoff 				default:
59703b3a8eb9SGleb Smirnoff 					break;
59713b3a8eb9SGleb Smirnoff 				}
59723b3a8eb9SGleb Smirnoff 
59733b3a8eb9SGleb Smirnoff 				if (strlcpy(pa.ifname, tqueue->ifname,
59743b3a8eb9SGleb Smirnoff 				    sizeof(pa.ifname)) >= sizeof(pa.ifname))
59753b3a8eb9SGleb Smirnoff 					errx(1, "expand_queue: strlcpy");
59763b3a8eb9SGleb Smirnoff 				if (strlcpy(pa.parent, tqueue->parent,
59773b3a8eb9SGleb Smirnoff 				    sizeof(pa.parent)) >= sizeof(pa.parent))
59783b3a8eb9SGleb Smirnoff 					errx(1, "expand_queue: strlcpy");
59793b3a8eb9SGleb Smirnoff 
59803b3a8eb9SGleb Smirnoff 				if (eval_pfqueue(pf, &pa, &bwspec, opts))
59813b3a8eb9SGleb Smirnoff 					errs++;
59823b3a8eb9SGleb Smirnoff 				else
59833b3a8eb9SGleb Smirnoff 					if (pfctl_add_altq(pf, &pa))
59843b3a8eb9SGleb Smirnoff 						errs++;
59853b3a8eb9SGleb Smirnoff 
59863b3a8eb9SGleb Smirnoff 				for (nq = nqueues; nq != NULL; nq = nq->next) {
59873b3a8eb9SGleb Smirnoff 					if (!strcmp(a->qname, nq->queue)) {
59883b3a8eb9SGleb Smirnoff 						yyerror("queue cannot have "
59893b3a8eb9SGleb Smirnoff 						    "itself as child");
59903b3a8eb9SGleb Smirnoff 						errs++;
59913b3a8eb9SGleb Smirnoff 						continue;
59923b3a8eb9SGleb Smirnoff 					}
59933b3a8eb9SGleb Smirnoff 					n = calloc(1,
59943b3a8eb9SGleb Smirnoff 					    sizeof(struct node_queue));
59953b3a8eb9SGleb Smirnoff 					if (n == NULL)
59963b3a8eb9SGleb Smirnoff 						err(1, "expand_queue: calloc");
59973b3a8eb9SGleb Smirnoff 					if (strlcpy(n->parent, a->qname,
59983b3a8eb9SGleb Smirnoff 					    sizeof(n->parent)) >=
59993b3a8eb9SGleb Smirnoff 					    sizeof(n->parent))
60003b3a8eb9SGleb Smirnoff 						errx(1, "expand_queue strlcpy");
60013b3a8eb9SGleb Smirnoff 					if (strlcpy(n->queue, nq->queue,
60023b3a8eb9SGleb Smirnoff 					    sizeof(n->queue)) >=
60033b3a8eb9SGleb Smirnoff 					    sizeof(n->queue))
60043b3a8eb9SGleb Smirnoff 						errx(1, "expand_queue strlcpy");
60053b3a8eb9SGleb Smirnoff 					if (strlcpy(n->ifname, tqueue->ifname,
60063b3a8eb9SGleb Smirnoff 					    sizeof(n->ifname)) >=
60073b3a8eb9SGleb Smirnoff 					    sizeof(n->ifname))
60083b3a8eb9SGleb Smirnoff 						errx(1, "expand_queue strlcpy");
60093b3a8eb9SGleb Smirnoff 					n->scheduler = tqueue->scheduler;
60103b3a8eb9SGleb Smirnoff 					n->next = NULL;
60113b3a8eb9SGleb Smirnoff 					n->tail = n;
60123b3a8eb9SGleb Smirnoff 					if (queues == NULL)
60133b3a8eb9SGleb Smirnoff 						queues = n;
60143b3a8eb9SGleb Smirnoff 					else {
60153b3a8eb9SGleb Smirnoff 						queues->tail->next = n;
60163b3a8eb9SGleb Smirnoff 						queues->tail = n;
60173b3a8eb9SGleb Smirnoff 					}
60183b3a8eb9SGleb Smirnoff 				}
60193b3a8eb9SGleb Smirnoff 				if ((pf->opts & PF_OPT_VERBOSE) && (
60203b3a8eb9SGleb Smirnoff 				    (found == 1 && interface->ifname[0] == 0) ||
60213b3a8eb9SGleb Smirnoff 				    (found > 0 && interface->ifname[0] != 0))) {
60223b3a8eb9SGleb Smirnoff 					print_queue(&pf->paltq->altq, 0,
60233b3a8eb9SGleb Smirnoff 					    &bwspec, interface->ifname[0] != 0,
60243b3a8eb9SGleb Smirnoff 					    opts);
60253b3a8eb9SGleb Smirnoff 					if (nqueues && nqueues->tail) {
60263b3a8eb9SGleb Smirnoff 						printf("{ ");
60273b3a8eb9SGleb Smirnoff 						LOOP_THROUGH(struct node_queue,
60283b3a8eb9SGleb Smirnoff 						    queue, nqueues,
60293b3a8eb9SGleb Smirnoff 							printf("%s ",
60303b3a8eb9SGleb Smirnoff 							    queue->queue);
60313b3a8eb9SGleb Smirnoff 						);
60323b3a8eb9SGleb Smirnoff 						printf("}");
60333b3a8eb9SGleb Smirnoff 					}
60343b3a8eb9SGleb Smirnoff 					printf("\n");
60353b3a8eb9SGleb Smirnoff 				}
60363b3a8eb9SGleb Smirnoff 			}
60373b3a8eb9SGleb Smirnoff 		);
60383b3a8eb9SGleb Smirnoff 	);
60393b3a8eb9SGleb Smirnoff 
60403b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_queue, nqueues);
60413b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_if, interfaces);
60423b3a8eb9SGleb Smirnoff 
60433b3a8eb9SGleb Smirnoff 	if (!found) {
60443b3a8eb9SGleb Smirnoff 		yyerror("queue %s has no parent", a->qname);
60453b3a8eb9SGleb Smirnoff 		errs++;
60463b3a8eb9SGleb Smirnoff 	}
60473b3a8eb9SGleb Smirnoff 
60483b3a8eb9SGleb Smirnoff 	if (errs)
60493b3a8eb9SGleb Smirnoff 		return (1);
60503b3a8eb9SGleb Smirnoff 	else
60513b3a8eb9SGleb Smirnoff 		return (0);
60523b3a8eb9SGleb Smirnoff }
60533b3a8eb9SGleb Smirnoff 
60548a42005dSKristof Provost static int
60558a42005dSKristof Provost pf_af_to_proto(sa_family_t af)
60568a42005dSKristof Provost {
60578a42005dSKristof Provost 	if (af == AF_INET)
60588a42005dSKristof Provost 		return (ETHERTYPE_IP);
60598a42005dSKristof Provost 	if (af == AF_INET6)
60608a42005dSKristof Provost 		return (ETHERTYPE_IPV6);
60618a42005dSKristof Provost 
60628a42005dSKristof Provost 	return (0);
60638a42005dSKristof Provost }
60648a42005dSKristof Provost 
60653b3a8eb9SGleb Smirnoff void
60662b29ceb8SKristof Provost expand_eth_rule(struct pfctl_eth_rule *r,
606787a89d6eSKristof Provost     struct node_if *interfaces, struct node_etherproto *protos,
60688a42005dSKristof Provost     struct node_mac *srcs, struct node_mac *dsts,
60698a8af942SKristof Provost     struct node_host *ipsrcs, struct node_host *ipdsts,
60708a8af942SKristof Provost     const char *bridge_to, const char *anchor_call)
60712b29ceb8SKristof Provost {
60721f61367fSKristof Provost 	char tagname[PF_TAG_NAME_SIZE];
60731f61367fSKristof Provost 	char match_tagname[PF_TAG_NAME_SIZE];
60741f61367fSKristof Provost 	char qname[PF_QNAME_SIZE];
60751f61367fSKristof Provost 
60761f61367fSKristof Provost 	if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname))
60771f61367fSKristof Provost 		errx(1, "expand_eth_rule: tagname");
60781f61367fSKristof Provost 	if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >=
60791f61367fSKristof Provost 	    sizeof(match_tagname))
60801f61367fSKristof Provost 		errx(1, "expand_eth_rule: match_tagname");
60811f61367fSKristof Provost 	if (strlcpy(qname, r->qname, sizeof(qname)) >= sizeof(qname))
60821f61367fSKristof Provost 		errx(1, "expand_eth_rule: qname");
60831f61367fSKristof Provost 
60842b29ceb8SKristof Provost 	LOOP_THROUGH(struct node_if, interface, interfaces,
60852b29ceb8SKristof Provost 	LOOP_THROUGH(struct node_etherproto, proto, protos,
608687a89d6eSKristof Provost 	LOOP_THROUGH(struct node_mac, src, srcs,
608787a89d6eSKristof Provost 	LOOP_THROUGH(struct node_mac, dst, dsts,
60888a42005dSKristof Provost 	LOOP_THROUGH(struct node_host, ipsrc, ipsrcs,
60898a42005dSKristof Provost 	LOOP_THROUGH(struct node_host, ipdst, ipdsts,
60902b29ceb8SKristof Provost 		strlcpy(r->ifname, interface->ifname,
60912b29ceb8SKristof Provost 		    sizeof(r->ifname));
60922b29ceb8SKristof Provost 		r->ifnot = interface->not;
60932b29ceb8SKristof Provost 		r->proto = proto->proto;
60948a42005dSKristof Provost 		if (!r->proto && ipsrc->af)
60958a42005dSKristof Provost 			r->proto = pf_af_to_proto(ipsrc->af);
60968a42005dSKristof Provost 		else if (!r->proto && ipdst->af)
60978a42005dSKristof Provost 			r->proto = pf_af_to_proto(ipdst->af);
609887a89d6eSKristof Provost 		bcopy(src->mac, r->src.addr, ETHER_ADDR_LEN);
6099b590f17aSKristof Provost 		bcopy(src->mask, r->src.mask, ETHER_ADDR_LEN);
610087a89d6eSKristof Provost 		r->src.neg = src->neg;
6101c32cd180SKristof Provost 		r->src.isset = src->isset;
61028a42005dSKristof Provost 		r->ipsrc.addr = ipsrc->addr;
61038a42005dSKristof Provost 		r->ipsrc.neg = ipsrc->not;
61048a42005dSKristof Provost 		r->ipdst.addr = ipdst->addr;
61058a42005dSKristof Provost 		r->ipdst.neg = ipdst->not;
610687a89d6eSKristof Provost 		bcopy(dst->mac, r->dst.addr, ETHER_ADDR_LEN);
6107b590f17aSKristof Provost 		bcopy(dst->mask, r->dst.mask, ETHER_ADDR_LEN);
610887a89d6eSKristof Provost 		r->dst.neg = dst->neg;
6109c32cd180SKristof Provost 		r->dst.isset = dst->isset;
6110c5131afeSKristof Provost 		r->nr = pf->eastack[pf->asd]->match++;
61112b29ceb8SKristof Provost 
61121f61367fSKristof Provost 		if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >=
61131f61367fSKristof Provost 		    sizeof(r->tagname))
61141f61367fSKristof Provost 			errx(1, "expand_eth_rule: r->tagname");
61151f61367fSKristof Provost 		if (strlcpy(r->match_tagname, match_tagname,
61161f61367fSKristof Provost 		    sizeof(r->match_tagname)) >= sizeof(r->match_tagname))
61171f61367fSKristof Provost 			errx(1, "expand_eth_rule: r->match_tagname");
61181f61367fSKristof Provost 		if (strlcpy(r->qname, qname, sizeof(r->qname)) >= sizeof(r->qname))
61191f61367fSKristof Provost 			errx(1, "expand_eth_rule: r->qname");
61201f61367fSKristof Provost 
61218a8af942SKristof Provost 		if (bridge_to)
61228a8af942SKristof Provost 			strlcpy(r->bridge_to, bridge_to, sizeof(r->bridge_to));
61238a8af942SKristof Provost 
6124c5131afeSKristof Provost 		pfctl_append_eth_rule(pf, r, anchor_call);
61258a42005dSKristof Provost 	))))));
61262b29ceb8SKristof Provost 
61272b29ceb8SKristof Provost 	FREE_LIST(struct node_if, interfaces);
61282b29ceb8SKristof Provost 	FREE_LIST(struct node_etherproto, protos);
612987a89d6eSKristof Provost 	FREE_LIST(struct node_mac, srcs);
613087a89d6eSKristof Provost 	FREE_LIST(struct node_mac, dsts);
61318a42005dSKristof Provost 	FREE_LIST(struct node_host, ipsrcs);
61328a42005dSKristof Provost 	FREE_LIST(struct node_host, ipdsts);
61332b29ceb8SKristof Provost }
61342b29ceb8SKristof Provost 
61352b29ceb8SKristof Provost void
6136e9eb0941SKristof Provost expand_rule(struct pfctl_rule *r,
6137*aa69fdf1SKristof Provost     struct node_if *interfaces, struct node_host *rdr_hosts,
6138*aa69fdf1SKristof Provost     struct node_host *nat_hosts,
61393b3a8eb9SGleb Smirnoff     struct node_proto *protos, struct node_os *src_oses,
61403b3a8eb9SGleb Smirnoff     struct node_host *src_hosts, struct node_port *src_ports,
61413b3a8eb9SGleb Smirnoff     struct node_host *dst_hosts, struct node_port *dst_ports,
61422339ead6SKristof Provost     struct node_uid *uids, struct node_gid *gids, struct node_if *rcv,
61432339ead6SKristof Provost     struct node_icmp *icmp_types, const char *anchor_call)
61443b3a8eb9SGleb Smirnoff {
61453b3a8eb9SGleb Smirnoff 	sa_family_t		 af = r->af;
61463b3a8eb9SGleb Smirnoff 	int			 added = 0, error = 0;
61473b3a8eb9SGleb Smirnoff 	char			 ifname[IF_NAMESIZE];
61486fcc8e04SKristof Provost 	char			 label[PF_RULE_MAX_LABEL_COUNT][PF_RULE_LABEL_SIZE];
61493b3a8eb9SGleb Smirnoff 	char			 tagname[PF_TAG_NAME_SIZE];
61503b3a8eb9SGleb Smirnoff 	char			 match_tagname[PF_TAG_NAME_SIZE];
61513b3a8eb9SGleb Smirnoff 	struct pf_pooladdr	*pa;
61527ce98cf2SKristof Provost 	struct node_host	*h, *osrch, *odsth;
61533b3a8eb9SGleb Smirnoff 	u_int8_t		 flags, flagset, keep_state;
61543b3a8eb9SGleb Smirnoff 
61556fcc8e04SKristof Provost 	memcpy(label, r->label, sizeof(r->label));
61566fcc8e04SKristof Provost 	assert(sizeof(r->label) == sizeof(label));
61573b3a8eb9SGleb Smirnoff 	if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname))
61583b3a8eb9SGleb Smirnoff 		errx(1, "expand_rule: strlcpy");
61593b3a8eb9SGleb Smirnoff 	if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >=
61603b3a8eb9SGleb Smirnoff 	    sizeof(match_tagname))
61613b3a8eb9SGleb Smirnoff 		errx(1, "expand_rule: strlcpy");
61623b3a8eb9SGleb Smirnoff 	flags = r->flags;
61633b3a8eb9SGleb Smirnoff 	flagset = r->flagset;
61643b3a8eb9SGleb Smirnoff 	keep_state = r->keep_state;
61653b3a8eb9SGleb Smirnoff 
61663b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_if, interface, interfaces,
61673b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_proto, proto, protos,
61683b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_icmp, icmp_type, icmp_types,
61693b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_host, src_host, src_hosts,
61703b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_host, dst_host, dst_hosts,
6171288bec2bSKristof Provost 	LOOP_THROUGH(struct node_port, src_port, src_ports,
61723b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_port, dst_port, dst_ports,
6173288bec2bSKristof Provost 	LOOP_THROUGH(struct node_os, src_os, src_oses,
61743b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_uid, uid, uids,
61753b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_gid, gid, gids,
61763b3a8eb9SGleb Smirnoff 
61773b3a8eb9SGleb Smirnoff 		r->af = af;
61783b3a8eb9SGleb Smirnoff 		/* for link-local IPv6 address, interface must match up */
61793b3a8eb9SGleb Smirnoff 		if ((r->af && src_host->af && r->af != src_host->af) ||
61803b3a8eb9SGleb Smirnoff 		    (r->af && dst_host->af && r->af != dst_host->af) ||
61813b3a8eb9SGleb Smirnoff 		    (src_host->af && dst_host->af &&
61823b3a8eb9SGleb Smirnoff 		    src_host->af != dst_host->af) ||
61833b3a8eb9SGleb Smirnoff 		    (src_host->ifindex && dst_host->ifindex &&
61843b3a8eb9SGleb Smirnoff 		    src_host->ifindex != dst_host->ifindex) ||
61853b3a8eb9SGleb Smirnoff 		    (src_host->ifindex && *interface->ifname &&
61863b3a8eb9SGleb Smirnoff 		    src_host->ifindex != if_nametoindex(interface->ifname)) ||
61873b3a8eb9SGleb Smirnoff 		    (dst_host->ifindex && *interface->ifname &&
61883b3a8eb9SGleb Smirnoff 		    dst_host->ifindex != if_nametoindex(interface->ifname)))
61893b3a8eb9SGleb Smirnoff 			continue;
61903b3a8eb9SGleb Smirnoff 		if (!r->af && src_host->af)
61913b3a8eb9SGleb Smirnoff 			r->af = src_host->af;
61923b3a8eb9SGleb Smirnoff 		else if (!r->af && dst_host->af)
61933b3a8eb9SGleb Smirnoff 			r->af = dst_host->af;
61943b3a8eb9SGleb Smirnoff 
61953b3a8eb9SGleb Smirnoff 		if (*interface->ifname)
61963b3a8eb9SGleb Smirnoff 			strlcpy(r->ifname, interface->ifname,
61973b3a8eb9SGleb Smirnoff 			    sizeof(r->ifname));
61983b3a8eb9SGleb Smirnoff 		else if (if_indextoname(src_host->ifindex, ifname))
61993b3a8eb9SGleb Smirnoff 			strlcpy(r->ifname, ifname, sizeof(r->ifname));
62003b3a8eb9SGleb Smirnoff 		else if (if_indextoname(dst_host->ifindex, ifname))
62013b3a8eb9SGleb Smirnoff 			strlcpy(r->ifname, ifname, sizeof(r->ifname));
62023b3a8eb9SGleb Smirnoff 		else
62033b3a8eb9SGleb Smirnoff 			memset(r->ifname, '\0', sizeof(r->ifname));
62043b3a8eb9SGleb Smirnoff 
62056fcc8e04SKristof Provost 		memcpy(r->label, label, sizeof(r->label));
62063b3a8eb9SGleb Smirnoff 		if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >=
62073b3a8eb9SGleb Smirnoff 		    sizeof(r->tagname))
62083b3a8eb9SGleb Smirnoff 			errx(1, "expand_rule: strlcpy");
62093b3a8eb9SGleb Smirnoff 		if (strlcpy(r->match_tagname, match_tagname,
62103b3a8eb9SGleb Smirnoff 		    sizeof(r->match_tagname)) >= sizeof(r->match_tagname))
62113b3a8eb9SGleb Smirnoff 			errx(1, "expand_rule: strlcpy");
62123b3a8eb9SGleb Smirnoff 
62137ce98cf2SKristof Provost 		osrch = odsth = NULL;
62147ce98cf2SKristof Provost 		if (src_host->addr.type == PF_ADDR_DYNIFTL) {
62157ce98cf2SKristof Provost 			osrch = src_host;
62167ce98cf2SKristof Provost 			if ((src_host = gen_dynnode(src_host, r->af)) == NULL)
62177ce98cf2SKristof Provost 				err(1, "expand_rule: calloc");
62187ce98cf2SKristof Provost 		}
62197ce98cf2SKristof Provost 		if (dst_host->addr.type == PF_ADDR_DYNIFTL) {
62207ce98cf2SKristof Provost 			odsth = dst_host;
62217ce98cf2SKristof Provost 			if ((dst_host = gen_dynnode(dst_host, r->af)) == NULL)
62227ce98cf2SKristof Provost 				err(1, "expand_rule: calloc");
62237ce98cf2SKristof Provost 		}
62247ce98cf2SKristof Provost 
62253b3a8eb9SGleb Smirnoff 		error += check_netmask(src_host, r->af);
62263b3a8eb9SGleb Smirnoff 		error += check_netmask(dst_host, r->af);
62273b3a8eb9SGleb Smirnoff 
62283b3a8eb9SGleb Smirnoff 		r->ifnot = interface->not;
62293b3a8eb9SGleb Smirnoff 		r->proto = proto->proto;
62303b3a8eb9SGleb Smirnoff 		r->src.addr = src_host->addr;
62313b3a8eb9SGleb Smirnoff 		r->src.neg = src_host->not;
62323b3a8eb9SGleb Smirnoff 		r->src.port[0] = src_port->port[0];
62333b3a8eb9SGleb Smirnoff 		r->src.port[1] = src_port->port[1];
62343b3a8eb9SGleb Smirnoff 		r->src.port_op = src_port->op;
62353b3a8eb9SGleb Smirnoff 		r->dst.addr = dst_host->addr;
62363b3a8eb9SGleb Smirnoff 		r->dst.neg = dst_host->not;
62373b3a8eb9SGleb Smirnoff 		r->dst.port[0] = dst_port->port[0];
62383b3a8eb9SGleb Smirnoff 		r->dst.port[1] = dst_port->port[1];
62393b3a8eb9SGleb Smirnoff 		r->dst.port_op = dst_port->op;
62403b3a8eb9SGleb Smirnoff 		r->uid.op = uid->op;
62413b3a8eb9SGleb Smirnoff 		r->uid.uid[0] = uid->uid[0];
62423b3a8eb9SGleb Smirnoff 		r->uid.uid[1] = uid->uid[1];
62433b3a8eb9SGleb Smirnoff 		r->gid.op = gid->op;
62443b3a8eb9SGleb Smirnoff 		r->gid.gid[0] = gid->gid[0];
62453b3a8eb9SGleb Smirnoff 		r->gid.gid[1] = gid->gid[1];
62462339ead6SKristof Provost 		if (rcv) {
62472339ead6SKristof Provost 			strlcpy(r->rcv_ifname, rcv->ifname,
62482339ead6SKristof Provost 			    sizeof(r->rcv_ifname));
62492339ead6SKristof Provost 		}
62503b3a8eb9SGleb Smirnoff 		r->type = icmp_type->type;
62513b3a8eb9SGleb Smirnoff 		r->code = icmp_type->code;
62523b3a8eb9SGleb Smirnoff 
62533b3a8eb9SGleb Smirnoff 		if ((keep_state == PF_STATE_MODULATE ||
62543b3a8eb9SGleb Smirnoff 		    keep_state == PF_STATE_SYNPROXY) &&
62553b3a8eb9SGleb Smirnoff 		    r->proto && r->proto != IPPROTO_TCP)
62563b3a8eb9SGleb Smirnoff 			r->keep_state = PF_STATE_NORMAL;
62573b3a8eb9SGleb Smirnoff 		else
62583b3a8eb9SGleb Smirnoff 			r->keep_state = keep_state;
62593b3a8eb9SGleb Smirnoff 
62603b3a8eb9SGleb Smirnoff 		if (r->proto && r->proto != IPPROTO_TCP) {
62613b3a8eb9SGleb Smirnoff 			r->flags = 0;
62623b3a8eb9SGleb Smirnoff 			r->flagset = 0;
62633b3a8eb9SGleb Smirnoff 		} else {
62643b3a8eb9SGleb Smirnoff 			r->flags = flags;
62653b3a8eb9SGleb Smirnoff 			r->flagset = flagset;
62663b3a8eb9SGleb Smirnoff 		}
62673b3a8eb9SGleb Smirnoff 		if (icmp_type->proto && r->proto != icmp_type->proto) {
62683b3a8eb9SGleb Smirnoff 			yyerror("icmp-type mismatch");
62693b3a8eb9SGleb Smirnoff 			error++;
62703b3a8eb9SGleb Smirnoff 		}
62713b3a8eb9SGleb Smirnoff 
62723b3a8eb9SGleb Smirnoff 		if (src_os && src_os->os) {
62733b3a8eb9SGleb Smirnoff 			r->os_fingerprint = pfctl_get_fingerprint(src_os->os);
62743b3a8eb9SGleb Smirnoff 			if ((pf->opts & PF_OPT_VERBOSE2) &&
62753b3a8eb9SGleb Smirnoff 			    r->os_fingerprint == PF_OSFP_NOMATCH)
62763b3a8eb9SGleb Smirnoff 				fprintf(stderr,
62773b3a8eb9SGleb Smirnoff 				    "warning: unknown '%s' OS fingerprint\n",
62783b3a8eb9SGleb Smirnoff 				    src_os->os);
62793b3a8eb9SGleb Smirnoff 		} else {
62803b3a8eb9SGleb Smirnoff 			r->os_fingerprint = PF_OSFP_ANY;
62813b3a8eb9SGleb Smirnoff 		}
62823b3a8eb9SGleb Smirnoff 
6283*aa69fdf1SKristof Provost 		TAILQ_INIT(&r->rdr.list);
6284*aa69fdf1SKristof Provost 		for (h = rdr_hosts; h != NULL; h = h->next) {
62853b3a8eb9SGleb Smirnoff 			pa = calloc(1, sizeof(struct pf_pooladdr));
62863b3a8eb9SGleb Smirnoff 			if (pa == NULL)
62873b3a8eb9SGleb Smirnoff 				err(1, "expand_rule: calloc");
62883b3a8eb9SGleb Smirnoff 			pa->addr = h->addr;
62893b3a8eb9SGleb Smirnoff 			if (h->ifname != NULL) {
62903b3a8eb9SGleb Smirnoff 				if (strlcpy(pa->ifname, h->ifname,
62913b3a8eb9SGleb Smirnoff 				    sizeof(pa->ifname)) >=
62923b3a8eb9SGleb Smirnoff 				    sizeof(pa->ifname))
62933b3a8eb9SGleb Smirnoff 					errx(1, "expand_rule: strlcpy");
62943b3a8eb9SGleb Smirnoff 			} else
62953b3a8eb9SGleb Smirnoff 				pa->ifname[0] = 0;
62963b3a8eb9SGleb Smirnoff 			TAILQ_INSERT_TAIL(&r->rpool.list, pa, entries);
62973b3a8eb9SGleb Smirnoff 		}
6298*aa69fdf1SKristof Provost 		TAILQ_INIT(&r->nat.list);
6299*aa69fdf1SKristof Provost 		for (h = nat_hosts; h != NULL; h = h->next) {
6300*aa69fdf1SKristof Provost 			pa = calloc(1, sizeof(struct pf_pooladdr));
6301*aa69fdf1SKristof Provost 			if (pa == NULL)
6302*aa69fdf1SKristof Provost 				err(1, "expand_rule: calloc");
6303*aa69fdf1SKristof Provost 			pa->addr = h->addr;
6304*aa69fdf1SKristof Provost 			if (h->ifname != NULL) {
6305*aa69fdf1SKristof Provost 				if (strlcpy(pa->ifname, h->ifname,
6306*aa69fdf1SKristof Provost 				    sizeof(pa->ifname)) >=
6307*aa69fdf1SKristof Provost 				    sizeof(pa->ifname))
6308*aa69fdf1SKristof Provost 					errx(1, "expand_rule: strlcpy");
6309*aa69fdf1SKristof Provost 			} else
6310*aa69fdf1SKristof Provost 				pa->ifname[0] = 0;
6311*aa69fdf1SKristof Provost 			TAILQ_INSERT_TAIL(&r->nat.list, pa, entries);
6312*aa69fdf1SKristof Provost 		}
6313*aa69fdf1SKristof Provost 
6314*aa69fdf1SKristof Provost 		r->nat.proxy_port[0] = PF_NAT_PROXY_PORT_LOW;
6315*aa69fdf1SKristof Provost 		r->nat.proxy_port[1] = PF_NAT_PROXY_PORT_HIGH;
63163b3a8eb9SGleb Smirnoff 
63173b3a8eb9SGleb Smirnoff 		if (rule_consistent(r, anchor_call[0]) < 0 || error)
63183b3a8eb9SGleb Smirnoff 			yyerror("skipping rule due to errors");
63193b3a8eb9SGleb Smirnoff 		else {
63203b3a8eb9SGleb Smirnoff 			r->nr = pf->astack[pf->asd]->match++;
63210d71f9f3SKristof Provost 			pfctl_append_rule(pf, r, anchor_call);
63223b3a8eb9SGleb Smirnoff 			added++;
63233b3a8eb9SGleb Smirnoff 		}
63243b3a8eb9SGleb Smirnoff 
63257ce98cf2SKristof Provost 		if (osrch && src_host->addr.type == PF_ADDR_DYNIFTL) {
63267ce98cf2SKristof Provost 			free(src_host);
63277ce98cf2SKristof Provost 			src_host = osrch;
63287ce98cf2SKristof Provost 		}
63297ce98cf2SKristof Provost 		if (odsth && dst_host->addr.type == PF_ADDR_DYNIFTL) {
63307ce98cf2SKristof Provost 			free(dst_host);
63317ce98cf2SKristof Provost 			dst_host = odsth;
63327ce98cf2SKristof Provost 		}
63337ce98cf2SKristof Provost 
63343b3a8eb9SGleb Smirnoff 	))))))))));
63353b3a8eb9SGleb Smirnoff 
63363b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_if, interfaces);
63373b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_proto, protos);
63383b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_host, src_hosts);
63393b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_port, src_ports);
63403b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_os, src_oses);
63413b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_host, dst_hosts);
63423b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_port, dst_ports);
63433b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_uid, uids);
63443b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_gid, gids);
63453b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_icmp, icmp_types);
6346*aa69fdf1SKristof Provost 	FREE_LIST(struct node_host, rdr_hosts);
6347*aa69fdf1SKristof Provost 	FREE_LIST(struct node_host, nat_hosts);
63483b3a8eb9SGleb Smirnoff 
63493b3a8eb9SGleb Smirnoff 	if (!added)
63503b3a8eb9SGleb Smirnoff 		yyerror("rule expands to no valid combination");
63513b3a8eb9SGleb Smirnoff }
63523b3a8eb9SGleb Smirnoff 
63533b3a8eb9SGleb Smirnoff int
63543b3a8eb9SGleb Smirnoff expand_skip_interface(struct node_if *interfaces)
63553b3a8eb9SGleb Smirnoff {
63563b3a8eb9SGleb Smirnoff 	int	errs = 0;
63573b3a8eb9SGleb Smirnoff 
63583b3a8eb9SGleb Smirnoff 	if (!interfaces || (!interfaces->next && !interfaces->not &&
63593b3a8eb9SGleb Smirnoff 	    !strcmp(interfaces->ifname, "none"))) {
63603b3a8eb9SGleb Smirnoff 		if (pf->opts & PF_OPT_VERBOSE)
63613b3a8eb9SGleb Smirnoff 			printf("set skip on none\n");
63623b3a8eb9SGleb Smirnoff 		errs = pfctl_set_interface_flags(pf, "", PFI_IFLAG_SKIP, 0);
63633b3a8eb9SGleb Smirnoff 		return (errs);
63643b3a8eb9SGleb Smirnoff 	}
63653b3a8eb9SGleb Smirnoff 
63663b3a8eb9SGleb Smirnoff 	if (pf->opts & PF_OPT_VERBOSE)
63673b3a8eb9SGleb Smirnoff 		printf("set skip on {");
63683b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_if, interface, interfaces,
63693b3a8eb9SGleb Smirnoff 		if (pf->opts & PF_OPT_VERBOSE)
63703b3a8eb9SGleb Smirnoff 			printf(" %s", interface->ifname);
63713b3a8eb9SGleb Smirnoff 		if (interface->not) {
63723b3a8eb9SGleb Smirnoff 			yyerror("skip on ! <interface> is not supported");
63733b3a8eb9SGleb Smirnoff 			errs++;
63743b3a8eb9SGleb Smirnoff 		} else
63753b3a8eb9SGleb Smirnoff 			errs += pfctl_set_interface_flags(pf,
63763b3a8eb9SGleb Smirnoff 			    interface->ifname, PFI_IFLAG_SKIP, 1);
63773b3a8eb9SGleb Smirnoff 	);
63783b3a8eb9SGleb Smirnoff 	if (pf->opts & PF_OPT_VERBOSE)
63793b3a8eb9SGleb Smirnoff 		printf(" }\n");
63803b3a8eb9SGleb Smirnoff 
63813b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_if, interfaces);
63823b3a8eb9SGleb Smirnoff 
63833b3a8eb9SGleb Smirnoff 	if (errs)
63843b3a8eb9SGleb Smirnoff 		return (1);
63853b3a8eb9SGleb Smirnoff 	else
63863b3a8eb9SGleb Smirnoff 		return (0);
63873b3a8eb9SGleb Smirnoff }
63883b3a8eb9SGleb Smirnoff 
6389637d81c5SKristof Provost void
6390637d81c5SKristof Provost freehostlist(struct node_host *h)
6391637d81c5SKristof Provost {
6392637d81c5SKristof Provost 	FREE_LIST(struct node_host, h);
6393637d81c5SKristof Provost }
6394637d81c5SKristof Provost 
63953b3a8eb9SGleb Smirnoff #undef FREE_LIST
63963b3a8eb9SGleb Smirnoff #undef LOOP_THROUGH
63973b3a8eb9SGleb Smirnoff 
63983b3a8eb9SGleb Smirnoff int
63993b3a8eb9SGleb Smirnoff check_rulestate(int desired_state)
64003b3a8eb9SGleb Smirnoff {
64013b3a8eb9SGleb Smirnoff 	if (require_order && (rulestate > desired_state)) {
64022b29ceb8SKristof Provost 		yyerror("Rules must be in order: options, ethernet, "
64032b29ceb8SKristof Provost 		    "normalization, queueing, translation, filtering");
64043b3a8eb9SGleb Smirnoff 		return (1);
64053b3a8eb9SGleb Smirnoff 	}
64063b3a8eb9SGleb Smirnoff 	rulestate = desired_state;
64073b3a8eb9SGleb Smirnoff 	return (0);
64083b3a8eb9SGleb Smirnoff }
64093b3a8eb9SGleb Smirnoff 
64103b3a8eb9SGleb Smirnoff int
64113b3a8eb9SGleb Smirnoff kw_cmp(const void *k, const void *e)
64123b3a8eb9SGleb Smirnoff {
64133b3a8eb9SGleb Smirnoff 	return (strcmp(k, ((const struct keywords *)e)->k_name));
64143b3a8eb9SGleb Smirnoff }
64153b3a8eb9SGleb Smirnoff 
64163b3a8eb9SGleb Smirnoff int
64173b3a8eb9SGleb Smirnoff lookup(char *s)
64183b3a8eb9SGleb Smirnoff {
64193b3a8eb9SGleb Smirnoff 	/* this has to be sorted always */
64203b3a8eb9SGleb Smirnoff 	static const struct keywords keywords[] = {
6421*aa69fdf1SKristof Provost 		{ "af-to",		AFTO},
64223b3a8eb9SGleb Smirnoff 		{ "all",		ALL},
64233b3a8eb9SGleb Smirnoff 		{ "allow-opts",		ALLOWOPTS},
64243b3a8eb9SGleb Smirnoff 		{ "altq",		ALTQ},
64253b3a8eb9SGleb Smirnoff 		{ "anchor",		ANCHOR},
64263b3a8eb9SGleb Smirnoff 		{ "antispoof",		ANTISPOOF},
64273b3a8eb9SGleb Smirnoff 		{ "any",		ANY},
64283b3a8eb9SGleb Smirnoff 		{ "bandwidth",		BANDWIDTH},
64293b3a8eb9SGleb Smirnoff 		{ "binat",		BINAT},
64303b3a8eb9SGleb Smirnoff 		{ "binat-anchor",	BINATANCHOR},
64313b3a8eb9SGleb Smirnoff 		{ "bitmask",		BITMASK},
64323b3a8eb9SGleb Smirnoff 		{ "block",		BLOCK},
64333b3a8eb9SGleb Smirnoff 		{ "block-policy",	BLOCKPOLICY},
64348a8af942SKristof Provost 		{ "bridge-to",		BRIDGE_TO},
6435a5b789f6SErmal Luçi 		{ "buckets",		BUCKETS},
64363b3a8eb9SGleb Smirnoff 		{ "cbq",		CBQ},
64373b3a8eb9SGleb Smirnoff 		{ "code",		CODE},
64380a70aaf8SLuiz Otavio O Souza 		{ "codelq",		CODEL},
64393b3a8eb9SGleb Smirnoff 		{ "debug",		DEBUG},
64403b3a8eb9SGleb Smirnoff 		{ "divert-reply",	DIVERTREPLY},
64413b3a8eb9SGleb Smirnoff 		{ "divert-to",		DIVERTTO},
644263b3c1c7SKristof Provost 		{ "dnpipe",		DNPIPE},
644363b3c1c7SKristof Provost 		{ "dnqueue",		DNQUEUE},
64443b3a8eb9SGleb Smirnoff 		{ "drop",		DROP},
64453b3a8eb9SGleb Smirnoff 		{ "dup-to",		DUPTO},
6446390dc369STom Jones 		{ "endpoint-independent", ENDPI},
64472b29ceb8SKristof Provost 		{ "ether",		ETHER},
6448150182e3SKristof Provost 		{ "fail-policy",	FAILPOLICY},
6449a5b789f6SErmal Luçi 		{ "fairq",		FAIRQ},
64503b3a8eb9SGleb Smirnoff 		{ "fastroute",		FASTROUTE},
64513b3a8eb9SGleb Smirnoff 		{ "file",		FILENAME},
64523b3a8eb9SGleb Smirnoff 		{ "fingerprints",	FINGERPRINTS},
64533b3a8eb9SGleb Smirnoff 		{ "flags",		FLAGS},
64543b3a8eb9SGleb Smirnoff 		{ "floating",		FLOATING},
64553b3a8eb9SGleb Smirnoff 		{ "flush",		FLUSH},
64563b3a8eb9SGleb Smirnoff 		{ "for",		FOR},
64573b3a8eb9SGleb Smirnoff 		{ "fragment",		FRAGMENT},
64583b3a8eb9SGleb Smirnoff 		{ "from",		FROM},
64593b3a8eb9SGleb Smirnoff 		{ "global",		GLOBAL},
64603b3a8eb9SGleb Smirnoff 		{ "group",		GROUP},
64613b3a8eb9SGleb Smirnoff 		{ "hfsc",		HFSC},
6462a5b789f6SErmal Luçi 		{ "hogs",		HOGS},
64633b3a8eb9SGleb Smirnoff 		{ "hostid",		HOSTID},
64643b3a8eb9SGleb Smirnoff 		{ "icmp-type",		ICMPTYPE},
64653b3a8eb9SGleb Smirnoff 		{ "icmp6-type",		ICMP6TYPE},
64663b3a8eb9SGleb Smirnoff 		{ "if-bound",		IFBOUND},
64673b3a8eb9SGleb Smirnoff 		{ "in",			IN},
64683b3a8eb9SGleb Smirnoff 		{ "include",		INCLUDE},
64693b3a8eb9SGleb Smirnoff 		{ "inet",		INET},
64703b3a8eb9SGleb Smirnoff 		{ "inet6",		INET6},
64710a70aaf8SLuiz Otavio O Souza 		{ "interval",		INTERVAL},
64723b3a8eb9SGleb Smirnoff 		{ "keep",		KEEP},
647342ec75f8SKristof Provost 		{ "keepcounters",	KEEPCOUNTERS},
64748a42005dSKristof Provost 		{ "l3",			L3},
64753b3a8eb9SGleb Smirnoff 		{ "label",		LABEL},
64763b3a8eb9SGleb Smirnoff 		{ "limit",		LIMIT},
64773b3a8eb9SGleb Smirnoff 		{ "linkshare",		LINKSHARE},
64783b3a8eb9SGleb Smirnoff 		{ "load",		LOAD},
64793b3a8eb9SGleb Smirnoff 		{ "log",		LOG},
64803b3a8eb9SGleb Smirnoff 		{ "loginterface",	LOGINTERFACE},
64812aa21096SKurosawa Takahiro 		{ "map-e-portset",	MAPEPORTSET},
6482ef950daaSKristof Provost 		{ "match",		MATCH},
6483f3ab00c2SKristof Provost 		{ "matches",	MATCHES},
64843b3a8eb9SGleb Smirnoff 		{ "max",		MAXIMUM},
64853b3a8eb9SGleb Smirnoff 		{ "max-mss",		MAXMSS},
64863b3a8eb9SGleb Smirnoff 		{ "max-src-conn",	MAXSRCCONN},
64873b3a8eb9SGleb Smirnoff 		{ "max-src-conn-rate",	MAXSRCCONNRATE},
64883b3a8eb9SGleb Smirnoff 		{ "max-src-nodes",	MAXSRCNODES},
64893b3a8eb9SGleb Smirnoff 		{ "max-src-states",	MAXSRCSTATES},
64903b3a8eb9SGleb Smirnoff 		{ "min-ttl",		MINTTL},
64913b3a8eb9SGleb Smirnoff 		{ "modulate",		MODULATE},
64923b3a8eb9SGleb Smirnoff 		{ "nat",		NAT},
64933b3a8eb9SGleb Smirnoff 		{ "nat-anchor",		NATANCHOR},
64943b3a8eb9SGleb Smirnoff 		{ "no",			NO},
64953b3a8eb9SGleb Smirnoff 		{ "no-df",		NODF},
64963b3a8eb9SGleb Smirnoff 		{ "no-route",		NOROUTE},
64973b3a8eb9SGleb Smirnoff 		{ "no-sync",		NOSYNC},
64983b3a8eb9SGleb Smirnoff 		{ "on",			ON},
64993b3a8eb9SGleb Smirnoff 		{ "optimization",	OPTIMIZATION},
65003b3a8eb9SGleb Smirnoff 		{ "os",			OS},
65013b3a8eb9SGleb Smirnoff 		{ "out",		OUT},
65023b3a8eb9SGleb Smirnoff 		{ "overload",		OVERLOAD},
65033b3a8eb9SGleb Smirnoff 		{ "pass",		PASS},
6504baf9b6d0SKristof Provost 		{ "pflow",		PFLOW},
65053b3a8eb9SGleb Smirnoff 		{ "port",		PORT},
65063e248e0fSKristof Provost 		{ "prio",		PRIO},
65073b3a8eb9SGleb Smirnoff 		{ "priority",		PRIORITY},
65083b3a8eb9SGleb Smirnoff 		{ "priq",		PRIQ},
65093b3a8eb9SGleb Smirnoff 		{ "probability",	PROBABILITY},
65103b3a8eb9SGleb Smirnoff 		{ "proto",		PROTO},
65113b3a8eb9SGleb Smirnoff 		{ "qlimit",		QLIMIT},
65123b3a8eb9SGleb Smirnoff 		{ "queue",		QUEUE},
65133b3a8eb9SGleb Smirnoff 		{ "quick",		QUICK},
65143b3a8eb9SGleb Smirnoff 		{ "random",		RANDOM},
65153b3a8eb9SGleb Smirnoff 		{ "random-id",		RANDOMID},
65163b3a8eb9SGleb Smirnoff 		{ "rdr",		RDR},
65173b3a8eb9SGleb Smirnoff 		{ "rdr-anchor",		RDRANCHOR},
65183b3a8eb9SGleb Smirnoff 		{ "realtime",		REALTIME},
65193b3a8eb9SGleb Smirnoff 		{ "reassemble",		REASSEMBLE},
65202339ead6SKristof Provost 		{ "received-on",	RECEIVEDON},
65213b3a8eb9SGleb Smirnoff 		{ "reply-to",		REPLYTO},
65223b3a8eb9SGleb Smirnoff 		{ "require-order",	REQUIREORDER},
65233b3a8eb9SGleb Smirnoff 		{ "return",		RETURN},
65243b3a8eb9SGleb Smirnoff 		{ "return-icmp",	RETURNICMP},
65253b3a8eb9SGleb Smirnoff 		{ "return-icmp6",	RETURNICMP6},
65263b3a8eb9SGleb Smirnoff 		{ "return-rst",		RETURNRST},
652776c5eeccSKristof Provost 		{ "ridentifier",	RIDENTIFIER},
65283b3a8eb9SGleb Smirnoff 		{ "round-robin",	ROUNDROBIN},
65293b3a8eb9SGleb Smirnoff 		{ "route",		ROUTE},
65303b3a8eb9SGleb Smirnoff 		{ "route-to",		ROUTETO},
65313b3a8eb9SGleb Smirnoff 		{ "rtable",		RTABLE},
65323b3a8eb9SGleb Smirnoff 		{ "rule",		RULE},
65333b3a8eb9SGleb Smirnoff 		{ "ruleset-optimization",	RULESET_OPTIMIZATION},
65343b3a8eb9SGleb Smirnoff 		{ "scrub",		SCRUB},
65353b3a8eb9SGleb Smirnoff 		{ "set",		SET},
65363b3a8eb9SGleb Smirnoff 		{ "set-tos",		SETTOS},
65373b3a8eb9SGleb Smirnoff 		{ "skip",		SKIP},
65383b3a8eb9SGleb Smirnoff 		{ "sloppy",		SLOPPY},
65393b3a8eb9SGleb Smirnoff 		{ "source-hash",	SOURCEHASH},
65403b3a8eb9SGleb Smirnoff 		{ "source-track",	SOURCETRACK},
65413b3a8eb9SGleb Smirnoff 		{ "state",		STATE},
65423b3a8eb9SGleb Smirnoff 		{ "state-defaults",	STATEDEFAULTS},
65433b3a8eb9SGleb Smirnoff 		{ "state-policy",	STATEPOLICY},
65443b3a8eb9SGleb Smirnoff 		{ "static-port",	STATICPORT},
65453b3a8eb9SGleb Smirnoff 		{ "sticky-address",	STICKYADDRESS},
6546c69121c4SKristof Provost 		{ "syncookies",         SYNCOOKIES},
65473b3a8eb9SGleb Smirnoff 		{ "synproxy",		SYNPROXY},
65483b3a8eb9SGleb Smirnoff 		{ "table",		TABLE},
65493b3a8eb9SGleb Smirnoff 		{ "tag",		TAG},
65503b3a8eb9SGleb Smirnoff 		{ "tagged",		TAGGED},
65510a70aaf8SLuiz Otavio O Souza 		{ "target",		TARGET},
65523b3a8eb9SGleb Smirnoff 		{ "tbrsize",		TBRSIZE},
65533b3a8eb9SGleb Smirnoff 		{ "timeout",		TIMEOUT},
65543b3a8eb9SGleb Smirnoff 		{ "to",			TO},
65553b3a8eb9SGleb Smirnoff 		{ "tos",		TOS},
65563b3a8eb9SGleb Smirnoff 		{ "ttl",		TTL},
65573b3a8eb9SGleb Smirnoff 		{ "upperlimit",		UPPERLIMIT},
65583b3a8eb9SGleb Smirnoff 		{ "urpf-failed",	URPFFAILED},
65593b3a8eb9SGleb Smirnoff 		{ "user",		USER},
65603b3a8eb9SGleb Smirnoff 	};
65613b3a8eb9SGleb Smirnoff 	const struct keywords	*p;
65623b3a8eb9SGleb Smirnoff 
65633b3a8eb9SGleb Smirnoff 	p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
65643b3a8eb9SGleb Smirnoff 	    sizeof(keywords[0]), kw_cmp);
65653b3a8eb9SGleb Smirnoff 
65663b3a8eb9SGleb Smirnoff 	if (p) {
65673b3a8eb9SGleb Smirnoff 		if (debug > 1)
65683b3a8eb9SGleb Smirnoff 			fprintf(stderr, "%s: %d\n", s, p->k_val);
65693b3a8eb9SGleb Smirnoff 		return (p->k_val);
65703b3a8eb9SGleb Smirnoff 	} else {
65713b3a8eb9SGleb Smirnoff 		if (debug > 1)
65723b3a8eb9SGleb Smirnoff 			fprintf(stderr, "string: %s\n", s);
65733b3a8eb9SGleb Smirnoff 		return (STRING);
65743b3a8eb9SGleb Smirnoff 	}
65753b3a8eb9SGleb Smirnoff }
65763b3a8eb9SGleb Smirnoff 
65773b3a8eb9SGleb Smirnoff #define MAXPUSHBACK	128
65783b3a8eb9SGleb Smirnoff 
657913cfafabSKristof Provost static char	*parsebuf;
658013cfafabSKristof Provost static int	 parseindex;
658113cfafabSKristof Provost static char	 pushback_buffer[MAXPUSHBACK];
658213cfafabSKristof Provost static int	 pushback_index = 0;
65833b3a8eb9SGleb Smirnoff 
65843b3a8eb9SGleb Smirnoff int
65853b3a8eb9SGleb Smirnoff lgetc(int quotec)
65863b3a8eb9SGleb Smirnoff {
65873b3a8eb9SGleb Smirnoff 	int		c, next;
65883b3a8eb9SGleb Smirnoff 
65893b3a8eb9SGleb Smirnoff 	if (parsebuf) {
65903b3a8eb9SGleb Smirnoff 		/* Read character from the parsebuffer instead of input. */
65913b3a8eb9SGleb Smirnoff 		if (parseindex >= 0) {
65923b3a8eb9SGleb Smirnoff 			c = parsebuf[parseindex++];
65933b3a8eb9SGleb Smirnoff 			if (c != '\0')
65943b3a8eb9SGleb Smirnoff 				return (c);
65953b3a8eb9SGleb Smirnoff 			parsebuf = NULL;
65963b3a8eb9SGleb Smirnoff 		} else
65973b3a8eb9SGleb Smirnoff 			parseindex++;
65983b3a8eb9SGleb Smirnoff 	}
65993b3a8eb9SGleb Smirnoff 
66003b3a8eb9SGleb Smirnoff 	if (pushback_index)
66013b3a8eb9SGleb Smirnoff 		return (pushback_buffer[--pushback_index]);
66023b3a8eb9SGleb Smirnoff 
66033b3a8eb9SGleb Smirnoff 	if (quotec) {
66043b3a8eb9SGleb Smirnoff 		if ((c = getc(file->stream)) == EOF) {
66053b3a8eb9SGleb Smirnoff 			yyerror("reached end of file while parsing quoted string");
66063b3a8eb9SGleb Smirnoff 			if (popfile() == EOF)
66073b3a8eb9SGleb Smirnoff 				return (EOF);
66083b3a8eb9SGleb Smirnoff 			return (quotec);
66093b3a8eb9SGleb Smirnoff 		}
66103b3a8eb9SGleb Smirnoff 		return (c);
66113b3a8eb9SGleb Smirnoff 	}
66123b3a8eb9SGleb Smirnoff 
66133b3a8eb9SGleb Smirnoff 	while ((c = getc(file->stream)) == '\\') {
66143b3a8eb9SGleb Smirnoff 		next = getc(file->stream);
66153b3a8eb9SGleb Smirnoff 		if (next != '\n') {
66163b3a8eb9SGleb Smirnoff 			c = next;
66173b3a8eb9SGleb Smirnoff 			break;
66183b3a8eb9SGleb Smirnoff 		}
66193b3a8eb9SGleb Smirnoff 		yylval.lineno = file->lineno;
66203b3a8eb9SGleb Smirnoff 		file->lineno++;
66213b3a8eb9SGleb Smirnoff 	}
66223b3a8eb9SGleb Smirnoff 
66233b3a8eb9SGleb Smirnoff 	while (c == EOF) {
66243b3a8eb9SGleb Smirnoff 		if (popfile() == EOF)
66253b3a8eb9SGleb Smirnoff 			return (EOF);
66263b3a8eb9SGleb Smirnoff 		c = getc(file->stream);
66273b3a8eb9SGleb Smirnoff 	}
66283b3a8eb9SGleb Smirnoff 	return (c);
66293b3a8eb9SGleb Smirnoff }
66303b3a8eb9SGleb Smirnoff 
66313b3a8eb9SGleb Smirnoff int
66323b3a8eb9SGleb Smirnoff lungetc(int c)
66333b3a8eb9SGleb Smirnoff {
66343b3a8eb9SGleb Smirnoff 	if (c == EOF)
66353b3a8eb9SGleb Smirnoff 		return (EOF);
66363b3a8eb9SGleb Smirnoff 	if (parsebuf) {
66373b3a8eb9SGleb Smirnoff 		parseindex--;
66383b3a8eb9SGleb Smirnoff 		if (parseindex >= 0)
66393b3a8eb9SGleb Smirnoff 			return (c);
66403b3a8eb9SGleb Smirnoff 	}
66413b3a8eb9SGleb Smirnoff 	if (pushback_index < MAXPUSHBACK-1)
66423b3a8eb9SGleb Smirnoff 		return (pushback_buffer[pushback_index++] = c);
66433b3a8eb9SGleb Smirnoff 	else
66443b3a8eb9SGleb Smirnoff 		return (EOF);
66453b3a8eb9SGleb Smirnoff }
66463b3a8eb9SGleb Smirnoff 
66473b3a8eb9SGleb Smirnoff int
66483b3a8eb9SGleb Smirnoff findeol(void)
66493b3a8eb9SGleb Smirnoff {
66503b3a8eb9SGleb Smirnoff 	int	c;
66513b3a8eb9SGleb Smirnoff 
66523b3a8eb9SGleb Smirnoff 	parsebuf = NULL;
66533b3a8eb9SGleb Smirnoff 
66543b3a8eb9SGleb Smirnoff 	/* skip to either EOF or the first real EOL */
66553b3a8eb9SGleb Smirnoff 	while (1) {
66563b3a8eb9SGleb Smirnoff 		if (pushback_index)
66573b3a8eb9SGleb Smirnoff 			c = pushback_buffer[--pushback_index];
66583b3a8eb9SGleb Smirnoff 		else
66593b3a8eb9SGleb Smirnoff 			c = lgetc(0);
66603b3a8eb9SGleb Smirnoff 		if (c == '\n') {
66613b3a8eb9SGleb Smirnoff 			file->lineno++;
66623b3a8eb9SGleb Smirnoff 			break;
66633b3a8eb9SGleb Smirnoff 		}
66643b3a8eb9SGleb Smirnoff 		if (c == EOF)
66653b3a8eb9SGleb Smirnoff 			break;
66663b3a8eb9SGleb Smirnoff 	}
66673b3a8eb9SGleb Smirnoff 	return (ERROR);
66683b3a8eb9SGleb Smirnoff }
66693b3a8eb9SGleb Smirnoff 
66703b3a8eb9SGleb Smirnoff int
66713b3a8eb9SGleb Smirnoff yylex(void)
66723b3a8eb9SGleb Smirnoff {
66733b3a8eb9SGleb Smirnoff 	char	 buf[8096];
66743b3a8eb9SGleb Smirnoff 	char	*p, *val;
66753b3a8eb9SGleb Smirnoff 	int	 quotec, next, c;
66763b3a8eb9SGleb Smirnoff 	int	 token;
66773b3a8eb9SGleb Smirnoff 
66783b3a8eb9SGleb Smirnoff top:
66793b3a8eb9SGleb Smirnoff 	p = buf;
66803b3a8eb9SGleb Smirnoff 	while ((c = lgetc(0)) == ' ' || c == '\t')
66813b3a8eb9SGleb Smirnoff 		; /* nothing */
66823b3a8eb9SGleb Smirnoff 
66833b3a8eb9SGleb Smirnoff 	yylval.lineno = file->lineno;
66843b3a8eb9SGleb Smirnoff 	if (c == '#')
66853b3a8eb9SGleb Smirnoff 		while ((c = lgetc(0)) != '\n' && c != EOF)
66863b3a8eb9SGleb Smirnoff 			; /* nothing */
66873b3a8eb9SGleb Smirnoff 	if (c == '$' && parsebuf == NULL) {
66883b3a8eb9SGleb Smirnoff 		while (1) {
66893b3a8eb9SGleb Smirnoff 			if ((c = lgetc(0)) == EOF)
66903b3a8eb9SGleb Smirnoff 				return (0);
66913b3a8eb9SGleb Smirnoff 
66923b3a8eb9SGleb Smirnoff 			if (p + 1 >= buf + sizeof(buf) - 1) {
66933b3a8eb9SGleb Smirnoff 				yyerror("string too long");
66943b3a8eb9SGleb Smirnoff 				return (findeol());
66953b3a8eb9SGleb Smirnoff 			}
66963b3a8eb9SGleb Smirnoff 			if (isalnum(c) || c == '_') {
66973b3a8eb9SGleb Smirnoff 				*p++ = (char)c;
66983b3a8eb9SGleb Smirnoff 				continue;
66993b3a8eb9SGleb Smirnoff 			}
67003b3a8eb9SGleb Smirnoff 			*p = '\0';
67013b3a8eb9SGleb Smirnoff 			lungetc(c);
67023b3a8eb9SGleb Smirnoff 			break;
67033b3a8eb9SGleb Smirnoff 		}
67043b3a8eb9SGleb Smirnoff 		val = symget(buf);
67053b3a8eb9SGleb Smirnoff 		if (val == NULL) {
67063b3a8eb9SGleb Smirnoff 			yyerror("macro '%s' not defined", buf);
67073b3a8eb9SGleb Smirnoff 			return (findeol());
67083b3a8eb9SGleb Smirnoff 		}
67093b3a8eb9SGleb Smirnoff 		parsebuf = val;
67103b3a8eb9SGleb Smirnoff 		parseindex = 0;
67113b3a8eb9SGleb Smirnoff 		goto top;
67123b3a8eb9SGleb Smirnoff 	}
67133b3a8eb9SGleb Smirnoff 
67143b3a8eb9SGleb Smirnoff 	switch (c) {
67153b3a8eb9SGleb Smirnoff 	case '\'':
67163b3a8eb9SGleb Smirnoff 	case '"':
67173b3a8eb9SGleb Smirnoff 		quotec = c;
67183b3a8eb9SGleb Smirnoff 		while (1) {
67193b3a8eb9SGleb Smirnoff 			if ((c = lgetc(quotec)) == EOF)
67203b3a8eb9SGleb Smirnoff 				return (0);
67213b3a8eb9SGleb Smirnoff 			if (c == '\n') {
67223b3a8eb9SGleb Smirnoff 				file->lineno++;
67233b3a8eb9SGleb Smirnoff 				continue;
67243b3a8eb9SGleb Smirnoff 			} else if (c == '\\') {
67253b3a8eb9SGleb Smirnoff 				if ((next = lgetc(quotec)) == EOF)
67263b3a8eb9SGleb Smirnoff 					return (0);
67273b3a8eb9SGleb Smirnoff 				if (next == quotec || c == ' ' || c == '\t')
67283b3a8eb9SGleb Smirnoff 					c = next;
67294a8e4793SKristof Provost 				else if (next == '\n') {
67304a8e4793SKristof Provost 					file->lineno++;
67313b3a8eb9SGleb Smirnoff 					continue;
67324a8e4793SKristof Provost 				}
67333b3a8eb9SGleb Smirnoff 				else
67343b3a8eb9SGleb Smirnoff 					lungetc(next);
67353b3a8eb9SGleb Smirnoff 			} else if (c == quotec) {
67363b3a8eb9SGleb Smirnoff 				*p = '\0';
67373b3a8eb9SGleb Smirnoff 				break;
67383b3a8eb9SGleb Smirnoff 			}
67393b3a8eb9SGleb Smirnoff 			if (p + 1 >= buf + sizeof(buf) - 1) {
67403b3a8eb9SGleb Smirnoff 				yyerror("string too long");
67413b3a8eb9SGleb Smirnoff 				return (findeol());
67423b3a8eb9SGleb Smirnoff 			}
67433b3a8eb9SGleb Smirnoff 			*p++ = (char)c;
67443b3a8eb9SGleb Smirnoff 		}
67453b3a8eb9SGleb Smirnoff 		yylval.v.string = strdup(buf);
67463b3a8eb9SGleb Smirnoff 		if (yylval.v.string == NULL)
67473b3a8eb9SGleb Smirnoff 			err(1, "yylex: strdup");
67483b3a8eb9SGleb Smirnoff 		return (STRING);
674980eb861dSKristof Provost 		case '!':
675080eb861dSKristof Provost 			next = lgetc(0);
675180eb861dSKristof Provost 			if (next == '=')
675280eb861dSKristof Provost 				return (NE);
675380eb861dSKristof Provost 			lungetc(next);
675480eb861dSKristof Provost 		break;
67553b3a8eb9SGleb Smirnoff 	case '<':
67563b3a8eb9SGleb Smirnoff 		next = lgetc(0);
67573b3a8eb9SGleb Smirnoff 		if (next == '>') {
67583b3a8eb9SGleb Smirnoff 			yylval.v.i = PF_OP_XRG;
67593b3a8eb9SGleb Smirnoff 			return (PORTBINARY);
676080eb861dSKristof Provost 		} else if (next == '=')
676180eb861dSKristof Provost 			return (LE);
67623b3a8eb9SGleb Smirnoff 		lungetc(next);
67633b3a8eb9SGleb Smirnoff 		break;
67643b3a8eb9SGleb Smirnoff 	case '>':
67653b3a8eb9SGleb Smirnoff 		next = lgetc(0);
67663b3a8eb9SGleb Smirnoff 		if (next == '<') {
67673b3a8eb9SGleb Smirnoff 			yylval.v.i = PF_OP_IRG;
67683b3a8eb9SGleb Smirnoff 			return (PORTBINARY);
676980eb861dSKristof Provost 		} else if (next == '=')
677080eb861dSKristof Provost 			return (GE);
67713b3a8eb9SGleb Smirnoff 		lungetc(next);
67723b3a8eb9SGleb Smirnoff 		break;
67733b3a8eb9SGleb Smirnoff 	case '-':
67743b3a8eb9SGleb Smirnoff 		next = lgetc(0);
67753b3a8eb9SGleb Smirnoff 		if (next == '>')
67763b3a8eb9SGleb Smirnoff 			return (ARROW);
67773b3a8eb9SGleb Smirnoff 		lungetc(next);
67783b3a8eb9SGleb Smirnoff 		break;
67793b3a8eb9SGleb Smirnoff 	}
67803b3a8eb9SGleb Smirnoff 
67813b3a8eb9SGleb Smirnoff #define allowed_to_end_number(x) \
67823b3a8eb9SGleb Smirnoff 	(isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
67833b3a8eb9SGleb Smirnoff 
67843b3a8eb9SGleb Smirnoff 	if (c == '-' || isdigit(c)) {
67853b3a8eb9SGleb Smirnoff 		do {
67863b3a8eb9SGleb Smirnoff 			*p++ = c;
67873b3a8eb9SGleb Smirnoff 			if ((unsigned)(p-buf) >= sizeof(buf)) {
67883b3a8eb9SGleb Smirnoff 				yyerror("string too long");
67893b3a8eb9SGleb Smirnoff 				return (findeol());
67903b3a8eb9SGleb Smirnoff 			}
67913b3a8eb9SGleb Smirnoff 		} while ((c = lgetc(0)) != EOF && isdigit(c));
67923b3a8eb9SGleb Smirnoff 		lungetc(c);
67933b3a8eb9SGleb Smirnoff 		if (p == buf + 1 && buf[0] == '-')
67943b3a8eb9SGleb Smirnoff 			goto nodigits;
67953b3a8eb9SGleb Smirnoff 		if (c == EOF || allowed_to_end_number(c)) {
67963b3a8eb9SGleb Smirnoff 			const char *errstr = NULL;
67973b3a8eb9SGleb Smirnoff 
67983b3a8eb9SGleb Smirnoff 			*p = '\0';
67993b3a8eb9SGleb Smirnoff 			yylval.v.number = strtonum(buf, LLONG_MIN,
68003b3a8eb9SGleb Smirnoff 			    LLONG_MAX, &errstr);
68013b3a8eb9SGleb Smirnoff 			if (errstr) {
68023b3a8eb9SGleb Smirnoff 				yyerror("\"%s\" invalid number: %s",
68033b3a8eb9SGleb Smirnoff 				    buf, errstr);
68043b3a8eb9SGleb Smirnoff 				return (findeol());
68053b3a8eb9SGleb Smirnoff 			}
68063b3a8eb9SGleb Smirnoff 			return (NUMBER);
68073b3a8eb9SGleb Smirnoff 		} else {
68083b3a8eb9SGleb Smirnoff nodigits:
68093b3a8eb9SGleb Smirnoff 			while (p > buf + 1)
68103b3a8eb9SGleb Smirnoff 				lungetc(*--p);
68113b3a8eb9SGleb Smirnoff 			c = *--p;
68123b3a8eb9SGleb Smirnoff 			if (c == '-')
68133b3a8eb9SGleb Smirnoff 				return (c);
68143b3a8eb9SGleb Smirnoff 		}
68153b3a8eb9SGleb Smirnoff 	}
68163b3a8eb9SGleb Smirnoff 
68173b3a8eb9SGleb Smirnoff #define allowed_in_string(x) \
68183b3a8eb9SGleb Smirnoff 	(isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
68193b3a8eb9SGleb Smirnoff 	x != '{' && x != '}' && x != '<' && x != '>' && \
68203b3a8eb9SGleb Smirnoff 	x != '!' && x != '=' && x != '/' && x != '#' && \
68213b3a8eb9SGleb Smirnoff 	x != ','))
68223b3a8eb9SGleb Smirnoff 
68233b3a8eb9SGleb Smirnoff 	if (isalnum(c) || c == ':' || c == '_') {
68243b3a8eb9SGleb Smirnoff 		do {
68253b3a8eb9SGleb Smirnoff 			*p++ = c;
68263b3a8eb9SGleb Smirnoff 			if ((unsigned)(p-buf) >= sizeof(buf)) {
68273b3a8eb9SGleb Smirnoff 				yyerror("string too long");
68283b3a8eb9SGleb Smirnoff 				return (findeol());
68293b3a8eb9SGleb Smirnoff 			}
68303b3a8eb9SGleb Smirnoff 		} while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
68313b3a8eb9SGleb Smirnoff 		lungetc(c);
68323b3a8eb9SGleb Smirnoff 		*p = '\0';
68333b3a8eb9SGleb Smirnoff 		if ((token = lookup(buf)) == STRING)
68343b3a8eb9SGleb Smirnoff 			if ((yylval.v.string = strdup(buf)) == NULL)
68353b3a8eb9SGleb Smirnoff 				err(1, "yylex: strdup");
68363b3a8eb9SGleb Smirnoff 		return (token);
68373b3a8eb9SGleb Smirnoff 	}
68383b3a8eb9SGleb Smirnoff 	if (c == '\n') {
68393b3a8eb9SGleb Smirnoff 		yylval.lineno = file->lineno;
68403b3a8eb9SGleb Smirnoff 		file->lineno++;
68413b3a8eb9SGleb Smirnoff 	}
68423b3a8eb9SGleb Smirnoff 	if (c == EOF)
68433b3a8eb9SGleb Smirnoff 		return (0);
68443b3a8eb9SGleb Smirnoff 	return (c);
68453b3a8eb9SGleb Smirnoff }
68463b3a8eb9SGleb Smirnoff 
68473b3a8eb9SGleb Smirnoff int
68483b3a8eb9SGleb Smirnoff check_file_secrecy(int fd, const char *fname)
68493b3a8eb9SGleb Smirnoff {
68503b3a8eb9SGleb Smirnoff 	struct stat	st;
68513b3a8eb9SGleb Smirnoff 
68523b3a8eb9SGleb Smirnoff 	if (fstat(fd, &st)) {
68533b3a8eb9SGleb Smirnoff 		warn("cannot stat %s", fname);
68543b3a8eb9SGleb Smirnoff 		return (-1);
68553b3a8eb9SGleb Smirnoff 	}
68563b3a8eb9SGleb Smirnoff 	if (st.st_uid != 0 && st.st_uid != getuid()) {
68573b3a8eb9SGleb Smirnoff 		warnx("%s: owner not root or current user", fname);
68583b3a8eb9SGleb Smirnoff 		return (-1);
68593b3a8eb9SGleb Smirnoff 	}
68603b3a8eb9SGleb Smirnoff 	if (st.st_mode & (S_IRWXG | S_IRWXO)) {
68613b3a8eb9SGleb Smirnoff 		warnx("%s: group/world readable/writeable", fname);
68623b3a8eb9SGleb Smirnoff 		return (-1);
68633b3a8eb9SGleb Smirnoff 	}
68643b3a8eb9SGleb Smirnoff 	return (0);
68653b3a8eb9SGleb Smirnoff }
68663b3a8eb9SGleb Smirnoff 
68673b3a8eb9SGleb Smirnoff struct file *
68683b3a8eb9SGleb Smirnoff pushfile(const char *name, int secret)
68693b3a8eb9SGleb Smirnoff {
68703b3a8eb9SGleb Smirnoff 	struct file	*nfile;
68713b3a8eb9SGleb Smirnoff 
68723b3a8eb9SGleb Smirnoff 	if ((nfile = calloc(1, sizeof(struct file))) == NULL ||
68733b3a8eb9SGleb Smirnoff 	    (nfile->name = strdup(name)) == NULL) {
68743b3a8eb9SGleb Smirnoff 		warn("malloc");
68753b3a8eb9SGleb Smirnoff 		return (NULL);
68763b3a8eb9SGleb Smirnoff 	}
68773b3a8eb9SGleb Smirnoff 	if (TAILQ_FIRST(&files) == NULL && strcmp(nfile->name, "-") == 0) {
68783b3a8eb9SGleb Smirnoff 		nfile->stream = stdin;
68793b3a8eb9SGleb Smirnoff 		free(nfile->name);
68803b3a8eb9SGleb Smirnoff 		if ((nfile->name = strdup("stdin")) == NULL) {
68813b3a8eb9SGleb Smirnoff 			warn("strdup");
68823b3a8eb9SGleb Smirnoff 			free(nfile);
68833b3a8eb9SGleb Smirnoff 			return (NULL);
68843b3a8eb9SGleb Smirnoff 		}
68853b3a8eb9SGleb Smirnoff 	} else if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
68863b3a8eb9SGleb Smirnoff 		warn("%s", nfile->name);
68873b3a8eb9SGleb Smirnoff 		free(nfile->name);
68883b3a8eb9SGleb Smirnoff 		free(nfile);
68893b3a8eb9SGleb Smirnoff 		return (NULL);
68903b3a8eb9SGleb Smirnoff 	} else if (secret &&
68913b3a8eb9SGleb Smirnoff 	    check_file_secrecy(fileno(nfile->stream), nfile->name)) {
68923b3a8eb9SGleb Smirnoff 		fclose(nfile->stream);
68933b3a8eb9SGleb Smirnoff 		free(nfile->name);
68943b3a8eb9SGleb Smirnoff 		free(nfile);
68953b3a8eb9SGleb Smirnoff 		return (NULL);
68963b3a8eb9SGleb Smirnoff 	}
68973b3a8eb9SGleb Smirnoff 	nfile->lineno = 1;
68983b3a8eb9SGleb Smirnoff 	TAILQ_INSERT_TAIL(&files, nfile, entry);
68993b3a8eb9SGleb Smirnoff 	return (nfile);
69003b3a8eb9SGleb Smirnoff }
69013b3a8eb9SGleb Smirnoff 
69023b3a8eb9SGleb Smirnoff int
69033b3a8eb9SGleb Smirnoff popfile(void)
69043b3a8eb9SGleb Smirnoff {
69053b3a8eb9SGleb Smirnoff 	struct file	*prev;
69063b3a8eb9SGleb Smirnoff 
69073b3a8eb9SGleb Smirnoff 	if ((prev = TAILQ_PREV(file, files, entry)) != NULL) {
69083b3a8eb9SGleb Smirnoff 		prev->errors += file->errors;
69093b3a8eb9SGleb Smirnoff 		TAILQ_REMOVE(&files, file, entry);
69103b3a8eb9SGleb Smirnoff 		fclose(file->stream);
69113b3a8eb9SGleb Smirnoff 		free(file->name);
69123b3a8eb9SGleb Smirnoff 		free(file);
69133b3a8eb9SGleb Smirnoff 		file = prev;
69143b3a8eb9SGleb Smirnoff 		return (0);
69153b3a8eb9SGleb Smirnoff 	}
69163b3a8eb9SGleb Smirnoff 	return (EOF);
69173b3a8eb9SGleb Smirnoff }
69183b3a8eb9SGleb Smirnoff 
69193b3a8eb9SGleb Smirnoff int
69203b3a8eb9SGleb Smirnoff parse_config(char *filename, struct pfctl *xpf)
69213b3a8eb9SGleb Smirnoff {
69223b3a8eb9SGleb Smirnoff 	int		 errors = 0;
69233b3a8eb9SGleb Smirnoff 	struct sym	*sym;
69243b3a8eb9SGleb Smirnoff 
69253b3a8eb9SGleb Smirnoff 	pf = xpf;
69263b3a8eb9SGleb Smirnoff 	errors = 0;
69273b3a8eb9SGleb Smirnoff 	rulestate = PFCTL_STATE_NONE;
69283b3a8eb9SGleb Smirnoff 	returnicmpdefault = (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
69293b3a8eb9SGleb Smirnoff 	returnicmp6default =
69303b3a8eb9SGleb Smirnoff 	    (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
69313b3a8eb9SGleb Smirnoff 	blockpolicy = PFRULE_DROP;
6932150182e3SKristof Provost 	failpolicy = PFRULE_DROP;
69333b3a8eb9SGleb Smirnoff 	require_order = 1;
69343b3a8eb9SGleb Smirnoff 
69353b3a8eb9SGleb Smirnoff 	if ((file = pushfile(filename, 0)) == NULL) {
69363b3a8eb9SGleb Smirnoff 		warn("cannot open the main config file!");
69373b3a8eb9SGleb Smirnoff 		return (-1);
69383b3a8eb9SGleb Smirnoff 	}
69393b3a8eb9SGleb Smirnoff 
69403b3a8eb9SGleb Smirnoff 	yyparse();
69413b3a8eb9SGleb Smirnoff 	errors = file->errors;
69423b3a8eb9SGleb Smirnoff 	popfile();
69433b3a8eb9SGleb Smirnoff 
69443b3a8eb9SGleb Smirnoff 	/* Free macros and check which have not been used. */
69453b3a8eb9SGleb Smirnoff 	while ((sym = TAILQ_FIRST(&symhead))) {
69463b3a8eb9SGleb Smirnoff 		if ((pf->opts & PF_OPT_VERBOSE2) && !sym->used)
69473b3a8eb9SGleb Smirnoff 			fprintf(stderr, "warning: macro '%s' not "
69483b3a8eb9SGleb Smirnoff 			    "used\n", sym->nam);
69493b3a8eb9SGleb Smirnoff 		free(sym->nam);
69503b3a8eb9SGleb Smirnoff 		free(sym->val);
69513b3a8eb9SGleb Smirnoff 		TAILQ_REMOVE(&symhead, sym, entry);
69523b3a8eb9SGleb Smirnoff 		free(sym);
69533b3a8eb9SGleb Smirnoff 	}
69543b3a8eb9SGleb Smirnoff 
69553b3a8eb9SGleb Smirnoff 	return (errors ? -1 : 0);
69563b3a8eb9SGleb Smirnoff }
69573b3a8eb9SGleb Smirnoff 
69583b3a8eb9SGleb Smirnoff int
69593b3a8eb9SGleb Smirnoff symset(const char *nam, const char *val, int persist)
69603b3a8eb9SGleb Smirnoff {
69613b3a8eb9SGleb Smirnoff 	struct sym	*sym;
69623b3a8eb9SGleb Smirnoff 
69633b3a8eb9SGleb Smirnoff 	for (sym = TAILQ_FIRST(&symhead); sym && strcmp(nam, sym->nam);
69643b3a8eb9SGleb Smirnoff 	    sym = TAILQ_NEXT(sym, entry))
69653b3a8eb9SGleb Smirnoff 		;	/* nothing */
69663b3a8eb9SGleb Smirnoff 
69673b3a8eb9SGleb Smirnoff 	if (sym != NULL) {
69683b3a8eb9SGleb Smirnoff 		if (sym->persist == 1)
69693b3a8eb9SGleb Smirnoff 			return (0);
69703b3a8eb9SGleb Smirnoff 		else {
69713b3a8eb9SGleb Smirnoff 			free(sym->nam);
69723b3a8eb9SGleb Smirnoff 			free(sym->val);
69733b3a8eb9SGleb Smirnoff 			TAILQ_REMOVE(&symhead, sym, entry);
69743b3a8eb9SGleb Smirnoff 			free(sym);
69753b3a8eb9SGleb Smirnoff 		}
69763b3a8eb9SGleb Smirnoff 	}
69773b3a8eb9SGleb Smirnoff 	if ((sym = calloc(1, sizeof(*sym))) == NULL)
69783b3a8eb9SGleb Smirnoff 		return (-1);
69793b3a8eb9SGleb Smirnoff 
69803b3a8eb9SGleb Smirnoff 	sym->nam = strdup(nam);
69813b3a8eb9SGleb Smirnoff 	if (sym->nam == NULL) {
69823b3a8eb9SGleb Smirnoff 		free(sym);
69833b3a8eb9SGleb Smirnoff 		return (-1);
69843b3a8eb9SGleb Smirnoff 	}
69853b3a8eb9SGleb Smirnoff 	sym->val = strdup(val);
69863b3a8eb9SGleb Smirnoff 	if (sym->val == NULL) {
69873b3a8eb9SGleb Smirnoff 		free(sym->nam);
69883b3a8eb9SGleb Smirnoff 		free(sym);
69893b3a8eb9SGleb Smirnoff 		return (-1);
69903b3a8eb9SGleb Smirnoff 	}
69913b3a8eb9SGleb Smirnoff 	sym->used = 0;
69923b3a8eb9SGleb Smirnoff 	sym->persist = persist;
69933b3a8eb9SGleb Smirnoff 	TAILQ_INSERT_TAIL(&symhead, sym, entry);
69943b3a8eb9SGleb Smirnoff 	return (0);
69953b3a8eb9SGleb Smirnoff }
69963b3a8eb9SGleb Smirnoff 
69973b3a8eb9SGleb Smirnoff int
69983b3a8eb9SGleb Smirnoff pfctl_cmdline_symset(char *s)
69993b3a8eb9SGleb Smirnoff {
70003b3a8eb9SGleb Smirnoff 	char	*sym, *val;
70013b3a8eb9SGleb Smirnoff 	int	 ret;
70023b3a8eb9SGleb Smirnoff 
70033b3a8eb9SGleb Smirnoff 	if ((val = strrchr(s, '=')) == NULL)
70043b3a8eb9SGleb Smirnoff 		return (-1);
70053b3a8eb9SGleb Smirnoff 
70063b3a8eb9SGleb Smirnoff 	if ((sym = malloc(strlen(s) - strlen(val) + 1)) == NULL)
70073b3a8eb9SGleb Smirnoff 		err(1, "pfctl_cmdline_symset: malloc");
70083b3a8eb9SGleb Smirnoff 
70093b3a8eb9SGleb Smirnoff 	strlcpy(sym, s, strlen(s) - strlen(val) + 1);
70103b3a8eb9SGleb Smirnoff 
70113b3a8eb9SGleb Smirnoff 	ret = symset(sym, val + 1, 1);
70123b3a8eb9SGleb Smirnoff 	free(sym);
70133b3a8eb9SGleb Smirnoff 
70143b3a8eb9SGleb Smirnoff 	return (ret);
70153b3a8eb9SGleb Smirnoff }
70163b3a8eb9SGleb Smirnoff 
70173b3a8eb9SGleb Smirnoff char *
70183b3a8eb9SGleb Smirnoff symget(const char *nam)
70193b3a8eb9SGleb Smirnoff {
70203b3a8eb9SGleb Smirnoff 	struct sym	*sym;
70213b3a8eb9SGleb Smirnoff 
70223b3a8eb9SGleb Smirnoff 	TAILQ_FOREACH(sym, &symhead, entry)
70233b3a8eb9SGleb Smirnoff 		if (strcmp(nam, sym->nam) == 0) {
70243b3a8eb9SGleb Smirnoff 			sym->used = 1;
70253b3a8eb9SGleb Smirnoff 			return (sym->val);
70263b3a8eb9SGleb Smirnoff 		}
70273b3a8eb9SGleb Smirnoff 	return (NULL);
70283b3a8eb9SGleb Smirnoff }
70293b3a8eb9SGleb Smirnoff 
70303b3a8eb9SGleb Smirnoff void
7031e9eb0941SKristof Provost mv_rules(struct pfctl_ruleset *src, struct pfctl_ruleset *dst)
70323b3a8eb9SGleb Smirnoff {
70333b3a8eb9SGleb Smirnoff 	int i;
7034e9eb0941SKristof Provost 	struct pfctl_rule *r;
70353b3a8eb9SGleb Smirnoff 
70363b3a8eb9SGleb Smirnoff 	for (i = 0; i < PF_RULESET_MAX; ++i) {
70373b3a8eb9SGleb Smirnoff 		while ((r = TAILQ_FIRST(src->rules[i].active.ptr))
70383b3a8eb9SGleb Smirnoff 		    != NULL) {
70393b3a8eb9SGleb Smirnoff 			TAILQ_REMOVE(src->rules[i].active.ptr, r, entries);
70403b3a8eb9SGleb Smirnoff 			TAILQ_INSERT_TAIL(dst->rules[i].active.ptr, r, entries);
70413b3a8eb9SGleb Smirnoff 			dst->anchor->match++;
70423b3a8eb9SGleb Smirnoff 		}
70433b3a8eb9SGleb Smirnoff 		src->anchor->match = 0;
70443b3a8eb9SGleb Smirnoff 		while ((r = TAILQ_FIRST(src->rules[i].inactive.ptr))
70453b3a8eb9SGleb Smirnoff 		    != NULL) {
70463b3a8eb9SGleb Smirnoff 			TAILQ_REMOVE(src->rules[i].inactive.ptr, r, entries);
70473b3a8eb9SGleb Smirnoff 			TAILQ_INSERT_TAIL(dst->rules[i].inactive.ptr,
70483b3a8eb9SGleb Smirnoff 				r, entries);
70493b3a8eb9SGleb Smirnoff 		}
70503b3a8eb9SGleb Smirnoff 	}
70513b3a8eb9SGleb Smirnoff }
70523b3a8eb9SGleb Smirnoff 
70533b3a8eb9SGleb Smirnoff void
7054c5131afeSKristof Provost mv_eth_rules(struct pfctl_eth_ruleset *src, struct pfctl_eth_ruleset *dst)
7055c5131afeSKristof Provost {
7056c5131afeSKristof Provost 	struct pfctl_eth_rule *r;
7057c5131afeSKristof Provost 
7058c5131afeSKristof Provost 	while ((r = TAILQ_FIRST(&src->rules)) != NULL) {
7059c5131afeSKristof Provost 		TAILQ_REMOVE(&src->rules, r, entries);
7060c5131afeSKristof Provost 		TAILQ_INSERT_TAIL(&dst->rules, r, entries);
7061c5131afeSKristof Provost 		dst->anchor->match++;
7062c5131afeSKristof Provost 	}
7063c5131afeSKristof Provost 	src->anchor->match = 0;
7064c5131afeSKristof Provost }
7065c5131afeSKristof Provost 
7066c5131afeSKristof Provost void
70673b3a8eb9SGleb Smirnoff decide_address_family(struct node_host *n, sa_family_t *af)
70683b3a8eb9SGleb Smirnoff {
70693b3a8eb9SGleb Smirnoff 	if (*af != 0 || n == NULL)
70703b3a8eb9SGleb Smirnoff 		return;
70713b3a8eb9SGleb Smirnoff 	*af = n->af;
70723b3a8eb9SGleb Smirnoff 	while ((n = n->next) != NULL) {
70733b3a8eb9SGleb Smirnoff 		if (n->af != *af) {
70743b3a8eb9SGleb Smirnoff 			*af = 0;
70753b3a8eb9SGleb Smirnoff 			return;
70763b3a8eb9SGleb Smirnoff 		}
70773b3a8eb9SGleb Smirnoff 	}
70783b3a8eb9SGleb Smirnoff }
70793b3a8eb9SGleb Smirnoff 
70803b3a8eb9SGleb Smirnoff void
70813b3a8eb9SGleb Smirnoff remove_invalid_hosts(struct node_host **nh, sa_family_t *af)
70823b3a8eb9SGleb Smirnoff {
70833b3a8eb9SGleb Smirnoff 	struct node_host	*n = *nh, *prev = NULL;
70843b3a8eb9SGleb Smirnoff 
70853b3a8eb9SGleb Smirnoff 	while (n != NULL) {
70863b3a8eb9SGleb Smirnoff 		if (*af && n->af && n->af != *af) {
70873b3a8eb9SGleb Smirnoff 			/* unlink and free n */
70883b3a8eb9SGleb Smirnoff 			struct node_host *next = n->next;
70893b3a8eb9SGleb Smirnoff 
70903b3a8eb9SGleb Smirnoff 			/* adjust tail pointer */
70913b3a8eb9SGleb Smirnoff 			if (n == (*nh)->tail)
70923b3a8eb9SGleb Smirnoff 				(*nh)->tail = prev;
70933b3a8eb9SGleb Smirnoff 			/* adjust previous node's next pointer */
70943b3a8eb9SGleb Smirnoff 			if (prev == NULL)
70953b3a8eb9SGleb Smirnoff 				*nh = next;
70963b3a8eb9SGleb Smirnoff 			else
70973b3a8eb9SGleb Smirnoff 				prev->next = next;
70983b3a8eb9SGleb Smirnoff 			/* free node */
70993b3a8eb9SGleb Smirnoff 			if (n->ifname != NULL)
71003b3a8eb9SGleb Smirnoff 				free(n->ifname);
71013b3a8eb9SGleb Smirnoff 			free(n);
71023b3a8eb9SGleb Smirnoff 			n = next;
71033b3a8eb9SGleb Smirnoff 		} else {
71043b3a8eb9SGleb Smirnoff 			if (n->af && !*af)
71053b3a8eb9SGleb Smirnoff 				*af = n->af;
71063b3a8eb9SGleb Smirnoff 			prev = n;
71073b3a8eb9SGleb Smirnoff 			n = n->next;
71083b3a8eb9SGleb Smirnoff 		}
71093b3a8eb9SGleb Smirnoff 	}
71103b3a8eb9SGleb Smirnoff }
71113b3a8eb9SGleb Smirnoff 
71123b3a8eb9SGleb Smirnoff int
71133b3a8eb9SGleb Smirnoff invalid_redirect(struct node_host *nh, sa_family_t af)
71143b3a8eb9SGleb Smirnoff {
71153b3a8eb9SGleb Smirnoff 	if (!af) {
71163b3a8eb9SGleb Smirnoff 		struct node_host *n;
71173b3a8eb9SGleb Smirnoff 
71183b3a8eb9SGleb Smirnoff 		/* tables and dyniftl are ok without an address family */
71193b3a8eb9SGleb Smirnoff 		for (n = nh; n != NULL; n = n->next) {
71203b3a8eb9SGleb Smirnoff 			if (n->addr.type != PF_ADDR_TABLE &&
71213b3a8eb9SGleb Smirnoff 			    n->addr.type != PF_ADDR_DYNIFTL) {
71223b3a8eb9SGleb Smirnoff 				yyerror("address family not given and "
71233b3a8eb9SGleb Smirnoff 				    "translation address expands to multiple "
71243b3a8eb9SGleb Smirnoff 				    "address families");
71253b3a8eb9SGleb Smirnoff 				return (1);
71263b3a8eb9SGleb Smirnoff 			}
71273b3a8eb9SGleb Smirnoff 		}
71283b3a8eb9SGleb Smirnoff 	}
71293b3a8eb9SGleb Smirnoff 	if (nh == NULL) {
71303b3a8eb9SGleb Smirnoff 		yyerror("no translation address with matching address family "
71313b3a8eb9SGleb Smirnoff 		    "found.");
71323b3a8eb9SGleb Smirnoff 		return (1);
71333b3a8eb9SGleb Smirnoff 	}
71343b3a8eb9SGleb Smirnoff 	return (0);
71353b3a8eb9SGleb Smirnoff }
71363b3a8eb9SGleb Smirnoff 
71373b3a8eb9SGleb Smirnoff int
71383b3a8eb9SGleb Smirnoff atoul(char *s, u_long *ulvalp)
71393b3a8eb9SGleb Smirnoff {
71403b3a8eb9SGleb Smirnoff 	u_long	 ulval;
71413b3a8eb9SGleb Smirnoff 	char	*ep;
71423b3a8eb9SGleb Smirnoff 
71433b3a8eb9SGleb Smirnoff 	errno = 0;
71443b3a8eb9SGleb Smirnoff 	ulval = strtoul(s, &ep, 0);
71453b3a8eb9SGleb Smirnoff 	if (s[0] == '\0' || *ep != '\0')
71463b3a8eb9SGleb Smirnoff 		return (-1);
71473b3a8eb9SGleb Smirnoff 	if (errno == ERANGE && ulval == ULONG_MAX)
71483b3a8eb9SGleb Smirnoff 		return (-1);
71493b3a8eb9SGleb Smirnoff 	*ulvalp = ulval;
71503b3a8eb9SGleb Smirnoff 	return (0);
71513b3a8eb9SGleb Smirnoff }
71523b3a8eb9SGleb Smirnoff 
71533b3a8eb9SGleb Smirnoff int
71543b3a8eb9SGleb Smirnoff getservice(char *n)
71553b3a8eb9SGleb Smirnoff {
71563b3a8eb9SGleb Smirnoff 	struct servent	*s;
71573b3a8eb9SGleb Smirnoff 	u_long		 ulval;
71583b3a8eb9SGleb Smirnoff 
71593b3a8eb9SGleb Smirnoff 	if (atoul(n, &ulval) == 0) {
71603b3a8eb9SGleb Smirnoff 		if (ulval > 65535) {
71613b3a8eb9SGleb Smirnoff 			yyerror("illegal port value %lu", ulval);
71623b3a8eb9SGleb Smirnoff 			return (-1);
71633b3a8eb9SGleb Smirnoff 		}
71643b3a8eb9SGleb Smirnoff 		return (htons(ulval));
71653b3a8eb9SGleb Smirnoff 	} else {
71663b3a8eb9SGleb Smirnoff 		s = getservbyname(n, "tcp");
71673b3a8eb9SGleb Smirnoff 		if (s == NULL)
71683b3a8eb9SGleb Smirnoff 			s = getservbyname(n, "udp");
71690bd4a683SKristof Provost 		if (s == NULL)
71700bd4a683SKristof Provost 			s = getservbyname(n, "sctp");
71713b3a8eb9SGleb Smirnoff 		if (s == NULL) {
71723b3a8eb9SGleb Smirnoff 			yyerror("unknown port %s", n);
71733b3a8eb9SGleb Smirnoff 			return (-1);
71743b3a8eb9SGleb Smirnoff 		}
71753b3a8eb9SGleb Smirnoff 		return (s->s_port);
71763b3a8eb9SGleb Smirnoff 	}
71773b3a8eb9SGleb Smirnoff }
71783b3a8eb9SGleb Smirnoff 
71793b3a8eb9SGleb Smirnoff int
71806fcc8e04SKristof Provost rule_label(struct pfctl_rule *r, char *s[PF_RULE_MAX_LABEL_COUNT])
71813b3a8eb9SGleb Smirnoff {
71826fcc8e04SKristof Provost 	for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++) {
71836fcc8e04SKristof Provost 		if (s[i] == NULL)
71846fcc8e04SKristof Provost 			return (0);
71856fcc8e04SKristof Provost 
71866fcc8e04SKristof Provost 		if (strlcpy(r->label[i], s[i], sizeof(r->label[0])) >=
71876fcc8e04SKristof Provost 		    sizeof(r->label[0])) {
71883b3a8eb9SGleb Smirnoff 			yyerror("rule label too long (max %d chars)",
71896fcc8e04SKristof Provost 			    sizeof(r->label[0])-1);
71903b3a8eb9SGleb Smirnoff 			return (-1);
71913b3a8eb9SGleb Smirnoff 		}
71923b3a8eb9SGleb Smirnoff 	}
71933b3a8eb9SGleb Smirnoff 	return (0);
71943b3a8eb9SGleb Smirnoff }
71953b3a8eb9SGleb Smirnoff 
7196ef661d4aSChristian McDonald int
7197ef661d4aSChristian McDonald eth_rule_label(struct pfctl_eth_rule *r, char *s[PF_RULE_MAX_LABEL_COUNT])
7198ef661d4aSChristian McDonald {
7199ef661d4aSChristian McDonald 	for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++) {
7200ef661d4aSChristian McDonald 		if (s[i] == NULL)
7201ef661d4aSChristian McDonald 			return (0);
7202ef661d4aSChristian McDonald 
7203ef661d4aSChristian McDonald 		if (strlcpy(r->label[i], s[i], sizeof(r->label[0])) >=
7204ef661d4aSChristian McDonald 		    sizeof(r->label[0])) {
7205ef661d4aSChristian McDonald 			yyerror("rule label too long (max %d chars)",
7206ef661d4aSChristian McDonald 			    sizeof(r->label[0])-1);
7207ef661d4aSChristian McDonald 			return (-1);
7208ef661d4aSChristian McDonald 		}
7209ef661d4aSChristian McDonald 	}
7210ef661d4aSChristian McDonald 	return (0);
7211ef661d4aSChristian McDonald }
7212ef661d4aSChristian McDonald 
72133b3a8eb9SGleb Smirnoff u_int16_t
72143b3a8eb9SGleb Smirnoff parseicmpspec(char *w, sa_family_t af)
72153b3a8eb9SGleb Smirnoff {
72163b3a8eb9SGleb Smirnoff 	const struct icmpcodeent	*p;
72173b3a8eb9SGleb Smirnoff 	u_long				 ulval;
72183b3a8eb9SGleb Smirnoff 	u_int8_t			 icmptype;
72193b3a8eb9SGleb Smirnoff 
72203b3a8eb9SGleb Smirnoff 	if (af == AF_INET)
72213b3a8eb9SGleb Smirnoff 		icmptype = returnicmpdefault >> 8;
72223b3a8eb9SGleb Smirnoff 	else
72233b3a8eb9SGleb Smirnoff 		icmptype = returnicmp6default >> 8;
72243b3a8eb9SGleb Smirnoff 
72253b3a8eb9SGleb Smirnoff 	if (atoul(w, &ulval) == -1) {
72263b3a8eb9SGleb Smirnoff 		if ((p = geticmpcodebyname(icmptype, w, af)) == NULL) {
72273b3a8eb9SGleb Smirnoff 			yyerror("unknown icmp code %s", w);
72283b3a8eb9SGleb Smirnoff 			return (0);
72293b3a8eb9SGleb Smirnoff 		}
72303b3a8eb9SGleb Smirnoff 		ulval = p->code;
72313b3a8eb9SGleb Smirnoff 	}
72323b3a8eb9SGleb Smirnoff 	if (ulval > 255) {
72333b3a8eb9SGleb Smirnoff 		yyerror("invalid icmp code %lu", ulval);
72343b3a8eb9SGleb Smirnoff 		return (0);
72353b3a8eb9SGleb Smirnoff 	}
72363b3a8eb9SGleb Smirnoff 	return (icmptype << 8 | ulval);
72373b3a8eb9SGleb Smirnoff }
72383b3a8eb9SGleb Smirnoff 
72393b3a8eb9SGleb Smirnoff int
72403b3a8eb9SGleb Smirnoff parseport(char *port, struct range *r, int extensions)
72413b3a8eb9SGleb Smirnoff {
72423b3a8eb9SGleb Smirnoff 	char	*p = strchr(port, ':');
72433b3a8eb9SGleb Smirnoff 
72443b3a8eb9SGleb Smirnoff 	if (p == NULL) {
72453b3a8eb9SGleb Smirnoff 		if ((r->a = getservice(port)) == -1)
72463b3a8eb9SGleb Smirnoff 			return (-1);
72473b3a8eb9SGleb Smirnoff 		r->b = 0;
72483b3a8eb9SGleb Smirnoff 		r->t = PF_OP_NONE;
72493b3a8eb9SGleb Smirnoff 		return (0);
72503b3a8eb9SGleb Smirnoff 	}
72513b3a8eb9SGleb Smirnoff 	if ((extensions & PPORT_STAR) && !strcmp(p+1, "*")) {
72523b3a8eb9SGleb Smirnoff 		*p = 0;
72533b3a8eb9SGleb Smirnoff 		if ((r->a = getservice(port)) == -1)
72543b3a8eb9SGleb Smirnoff 			return (-1);
72553b3a8eb9SGleb Smirnoff 		r->b = 0;
72563b3a8eb9SGleb Smirnoff 		r->t = PF_OP_IRG;
72573b3a8eb9SGleb Smirnoff 		return (0);
72583b3a8eb9SGleb Smirnoff 	}
72593b3a8eb9SGleb Smirnoff 	if ((extensions & PPORT_RANGE)) {
72603b3a8eb9SGleb Smirnoff 		*p++ = 0;
72613b3a8eb9SGleb Smirnoff 		if ((r->a = getservice(port)) == -1 ||
72623b3a8eb9SGleb Smirnoff 		    (r->b = getservice(p)) == -1)
72633b3a8eb9SGleb Smirnoff 			return (-1);
72643b3a8eb9SGleb Smirnoff 		if (r->a == r->b) {
72653b3a8eb9SGleb Smirnoff 			r->b = 0;
72663b3a8eb9SGleb Smirnoff 			r->t = PF_OP_NONE;
72673b3a8eb9SGleb Smirnoff 		} else
72683b3a8eb9SGleb Smirnoff 			r->t = PF_OP_RRG;
72693b3a8eb9SGleb Smirnoff 		return (0);
72703b3a8eb9SGleb Smirnoff 	}
72713b3a8eb9SGleb Smirnoff 	return (-1);
72723b3a8eb9SGleb Smirnoff }
72733b3a8eb9SGleb Smirnoff 
72743b3a8eb9SGleb Smirnoff int
72753b3a8eb9SGleb Smirnoff pfctl_load_anchors(int dev, struct pfctl *pf, struct pfr_buffer *trans)
72763b3a8eb9SGleb Smirnoff {
72773b3a8eb9SGleb Smirnoff 	struct loadanchors	*la;
72783b3a8eb9SGleb Smirnoff 
72793b3a8eb9SGleb Smirnoff 	TAILQ_FOREACH(la, &loadanchorshead, entries) {
72803b3a8eb9SGleb Smirnoff 		if (pf->opts & PF_OPT_VERBOSE)
72813b3a8eb9SGleb Smirnoff 			fprintf(stderr, "\nLoading anchor %s from %s\n",
72823b3a8eb9SGleb Smirnoff 			    la->anchorname, la->filename);
72833b3a8eb9SGleb Smirnoff 		if (pfctl_rules(dev, la->filename, pf->opts, pf->optimize,
72843b3a8eb9SGleb Smirnoff 		    la->anchorname, trans) == -1)
72853b3a8eb9SGleb Smirnoff 			return (-1);
72863b3a8eb9SGleb Smirnoff 	}
72873b3a8eb9SGleb Smirnoff 
72883b3a8eb9SGleb Smirnoff 	return (0);
72893b3a8eb9SGleb Smirnoff }
72903b3a8eb9SGleb Smirnoff 
72913b3a8eb9SGleb Smirnoff int
72921f495578SKristof Provost kw_casecmp(const void *k, const void *e)
72931f495578SKristof Provost {
72941f495578SKristof Provost 	return (strcasecmp(k, ((const struct keywords *)e)->k_name));
72951f495578SKristof Provost }
72961f495578SKristof Provost 
72971f495578SKristof Provost int
72981f495578SKristof Provost map_tos(char *s, int *val)
72991f495578SKristof Provost {
73001f495578SKristof Provost 	/* DiffServ Codepoints and other TOS mappings */
73011f495578SKristof Provost 	const struct keywords	 toswords[] = {
73021f495578SKristof Provost 		{ "af11",		IPTOS_DSCP_AF11 },
73031f495578SKristof Provost 		{ "af12",		IPTOS_DSCP_AF12 },
73041f495578SKristof Provost 		{ "af13",		IPTOS_DSCP_AF13 },
73051f495578SKristof Provost 		{ "af21",		IPTOS_DSCP_AF21 },
73061f495578SKristof Provost 		{ "af22",		IPTOS_DSCP_AF22 },
73071f495578SKristof Provost 		{ "af23",		IPTOS_DSCP_AF23 },
73081f495578SKristof Provost 		{ "af31",		IPTOS_DSCP_AF31 },
73091f495578SKristof Provost 		{ "af32",		IPTOS_DSCP_AF32 },
73101f495578SKristof Provost 		{ "af33",		IPTOS_DSCP_AF33 },
73111f495578SKristof Provost 		{ "af41",		IPTOS_DSCP_AF41 },
73121f495578SKristof Provost 		{ "af42",		IPTOS_DSCP_AF42 },
73131f495578SKristof Provost 		{ "af43",		IPTOS_DSCP_AF43 },
73141f495578SKristof Provost 		{ "critical",		IPTOS_PREC_CRITIC_ECP },
73151f495578SKristof Provost 		{ "cs0",		IPTOS_DSCP_CS0 },
73161f495578SKristof Provost 		{ "cs1",		IPTOS_DSCP_CS1 },
73171f495578SKristof Provost 		{ "cs2",		IPTOS_DSCP_CS2 },
73181f495578SKristof Provost 		{ "cs3",		IPTOS_DSCP_CS3 },
73191f495578SKristof Provost 		{ "cs4",		IPTOS_DSCP_CS4 },
73201f495578SKristof Provost 		{ "cs5",		IPTOS_DSCP_CS5 },
73211f495578SKristof Provost 		{ "cs6",		IPTOS_DSCP_CS6 },
73221f495578SKristof Provost 		{ "cs7",		IPTOS_DSCP_CS7 },
73231f495578SKristof Provost 		{ "ef",			IPTOS_DSCP_EF },
73241f495578SKristof Provost 		{ "inetcontrol",	IPTOS_PREC_INTERNETCONTROL },
73251f495578SKristof Provost 		{ "lowdelay",		IPTOS_LOWDELAY },
73261f495578SKristof Provost 		{ "netcontrol",		IPTOS_PREC_NETCONTROL },
73271f495578SKristof Provost 		{ "reliability",	IPTOS_RELIABILITY },
7328b4e3f3c2SKristof Provost 		{ "throughput",		IPTOS_THROUGHPUT },
7329b4e3f3c2SKristof Provost 		{ "va",			IPTOS_DSCP_VA }
73301f495578SKristof Provost 	};
73311f495578SKristof Provost 	const struct keywords	*p;
73321f495578SKristof Provost 
73331f495578SKristof Provost 	p = bsearch(s, toswords, sizeof(toswords)/sizeof(toswords[0]),
73341f495578SKristof Provost 	    sizeof(toswords[0]), kw_casecmp);
73351f495578SKristof Provost 
73361f495578SKristof Provost 	if (p) {
73371f495578SKristof Provost 		*val = p->k_val;
73381f495578SKristof Provost 		return (1);
73391f495578SKristof Provost 	}
73401f495578SKristof Provost 	return (0);
73411f495578SKristof Provost }
73421f495578SKristof Provost 
73431f495578SKristof Provost int
73443b3a8eb9SGleb Smirnoff rt_tableid_max(void)
73453b3a8eb9SGleb Smirnoff {
73463b3a8eb9SGleb Smirnoff #ifdef __FreeBSD__
73473b3a8eb9SGleb Smirnoff 	int fibs;
73483b3a8eb9SGleb Smirnoff 	size_t l = sizeof(fibs);
73493b3a8eb9SGleb Smirnoff 
73503b3a8eb9SGleb Smirnoff         if (sysctlbyname("net.fibs", &fibs, &l, NULL, 0) == -1)
73513b3a8eb9SGleb Smirnoff 		fibs = 16;	/* XXX RT_MAXFIBS, at least limit it some. */
73523b3a8eb9SGleb Smirnoff 	/*
73533b3a8eb9SGleb Smirnoff 	 * As the OpenBSD code only compares > and not >= we need to adjust
73543b3a8eb9SGleb Smirnoff 	 * here given we only accept values of 0..n and want to avoid #ifdefs
7355b68ac800SPedro F. Giffuni 	 * in the grammar.
73563b3a8eb9SGleb Smirnoff 	 */
73573b3a8eb9SGleb Smirnoff 	return (fibs - 1);
73583b3a8eb9SGleb Smirnoff #else
73593b3a8eb9SGleb Smirnoff 	return (RT_TABLEID_MAX);
73603b3a8eb9SGleb Smirnoff #endif
73613b3a8eb9SGleb Smirnoff }
7362b590f17aSKristof Provost 
7363b590f17aSKristof Provost struct node_mac*
7364b590f17aSKristof Provost node_mac_from_string(const char *str)
7365b590f17aSKristof Provost {
7366b590f17aSKristof Provost 	struct node_mac *m;
7367b590f17aSKristof Provost 
7368b590f17aSKristof Provost 	m = calloc(1, sizeof(struct node_mac));
7369b590f17aSKristof Provost 	if (m == NULL)
7370b590f17aSKristof Provost 		err(1, "mac: calloc");
7371b590f17aSKristof Provost 
7372b590f17aSKristof Provost 	if (sscanf(str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
7373b590f17aSKristof Provost 	    &m->mac[0], &m->mac[1], &m->mac[2], &m->mac[3], &m->mac[4],
7374b590f17aSKristof Provost 	    &m->mac[5]) != 6) {
7375b590f17aSKristof Provost 		free(m);
7376b590f17aSKristof Provost 		yyerror("invalid MAC address");
7377b590f17aSKristof Provost 		return (NULL);
7378b590f17aSKristof Provost 	}
7379b590f17aSKristof Provost 
7380b590f17aSKristof Provost 	memset(m->mask, 0xff, ETHER_ADDR_LEN);
7381c32cd180SKristof Provost 	m->isset = true;
7382b590f17aSKristof Provost 	m->next = NULL;
7383b590f17aSKristof Provost 	m->tail = m;
7384b590f17aSKristof Provost 
7385b590f17aSKristof Provost 	return (m);
7386b590f17aSKristof Provost }
7387b590f17aSKristof Provost 
7388b590f17aSKristof Provost struct node_mac*
7389b590f17aSKristof Provost node_mac_from_string_masklen(const char *str, int masklen)
7390b590f17aSKristof Provost {
7391b590f17aSKristof Provost 	struct node_mac *m;
7392b590f17aSKristof Provost 
7393b590f17aSKristof Provost 	if (masklen < 0 || masklen > (ETHER_ADDR_LEN * 8)) {
7394b590f17aSKristof Provost 		yyerror("invalid MAC mask length");
7395b590f17aSKristof Provost 		return (NULL);
7396b590f17aSKristof Provost 	}
7397b590f17aSKristof Provost 
7398b590f17aSKristof Provost 	m = node_mac_from_string(str);
7399b590f17aSKristof Provost 	if (m == NULL)
7400b590f17aSKristof Provost 		return (NULL);
7401b590f17aSKristof Provost 
7402b590f17aSKristof Provost 	memset(m->mask, 0, ETHER_ADDR_LEN);
7403b590f17aSKristof Provost 	for (int i = 0; i < masklen; i++)
7404b590f17aSKristof Provost 		m->mask[i / 8] |= 1 << (i % 8);
7405b590f17aSKristof Provost 
7406b590f17aSKristof Provost 	return (m);
7407b590f17aSKristof Provost }
7408b590f17aSKristof Provost 
7409b590f17aSKristof Provost struct node_mac*
7410b590f17aSKristof Provost node_mac_from_string_mask(const char *str, const char *mask)
7411b590f17aSKristof Provost {
7412b590f17aSKristof Provost 	struct node_mac *m;
7413b590f17aSKristof Provost 
7414b590f17aSKristof Provost 	m = node_mac_from_string(str);
7415b590f17aSKristof Provost 	if (m == NULL)
7416b590f17aSKristof Provost 		return (NULL);
7417b590f17aSKristof Provost 
7418b590f17aSKristof Provost 	if (sscanf(mask, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
7419b590f17aSKristof Provost 	    &m->mask[0], &m->mask[1], &m->mask[2], &m->mask[3], &m->mask[4],
7420b590f17aSKristof Provost 	    &m->mask[5]) != 6) {
7421b590f17aSKristof Provost 		free(m);
7422b590f17aSKristof Provost 		yyerror("invalid MAC mask");
7423b590f17aSKristof Provost 		return (NULL);
7424b590f17aSKristof Provost 	}
7425b590f17aSKristof Provost 
7426b590f17aSKristof Provost 	return (m);
7427b590f17aSKristof Provost }
7428