xref: /freebsd/sbin/pfctl/parse.y (revision 6562157dfad0d18447cfcac08435f7ffdb8fa46c)
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 
22513cfafabSKristof Provost static struct filter_opts {
2263b3a8eb9SGleb Smirnoff 	int			 marker;
22739282ef3SKajetan Staszkiewicz #define FOM_FLAGS	0x0001
22839282ef3SKajetan Staszkiewicz #define FOM_ICMP	0x0002
22939282ef3SKajetan Staszkiewicz #define FOM_TOS		0x0004
23039282ef3SKajetan Staszkiewicz #define FOM_KEEP	0x0008
23139282ef3SKajetan Staszkiewicz #define FOM_SRCTRACK	0x0010
23239282ef3SKajetan Staszkiewicz #define FOM_MINTTL	0x0020
23339282ef3SKajetan Staszkiewicz #define FOM_MAXMSS	0x0040
23439282ef3SKajetan Staszkiewicz #define FOM_AFTO	0x0080 /* not yet implemmented */
23539282ef3SKajetan Staszkiewicz #define FOM_SETTOS	0x0100
23639282ef3SKajetan Staszkiewicz #define FOM_SCRUB_TCP	0x0200
2373e248e0fSKristof Provost #define FOM_SETPRIO	0x0400
23839282ef3SKajetan Staszkiewicz #define FOM_ONCE	0x1000 /* not yet implemmented */
2393e248e0fSKristof Provost #define FOM_PRIO	0x2000
24039282ef3SKajetan Staszkiewicz #define FOM_SETDELAY	0x4000
24139282ef3SKajetan Staszkiewicz #define FOM_FRAGCACHE	0x8000 /* does not exist in OpenBSD */
2423b3a8eb9SGleb Smirnoff 	struct node_uid		*uid;
2433b3a8eb9SGleb Smirnoff 	struct node_gid		*gid;
2442339ead6SKristof Provost 	struct node_if		*rcv;
2453b3a8eb9SGleb Smirnoff 	struct {
2463b3a8eb9SGleb Smirnoff 		u_int8_t	 b1;
2473b3a8eb9SGleb Smirnoff 		u_int8_t	 b2;
2483b3a8eb9SGleb Smirnoff 		u_int16_t	 w;
2493b3a8eb9SGleb Smirnoff 		u_int16_t	 w2;
2503b3a8eb9SGleb Smirnoff 	} flags;
2513b3a8eb9SGleb Smirnoff 	struct node_icmp	*icmpspec;
2523b3a8eb9SGleb Smirnoff 	u_int32_t		 tos;
2533b3a8eb9SGleb Smirnoff 	u_int32_t		 prob;
25476c5eeccSKristof Provost 	u_int32_t		 ridentifier;
2553b3a8eb9SGleb Smirnoff 	struct {
2563b3a8eb9SGleb Smirnoff 		int			 action;
2573b3a8eb9SGleb Smirnoff 		struct node_state_opt	*options;
2583b3a8eb9SGleb Smirnoff 	} keep;
2593b3a8eb9SGleb Smirnoff 	int			 fragment;
2603b3a8eb9SGleb Smirnoff 	int			 allowopts;
2616fcc8e04SKristof Provost 	char			*label[PF_RULE_MAX_LABEL_COUNT];
2626fcc8e04SKristof Provost 	int			 labelcount;
2633b3a8eb9SGleb Smirnoff 	struct node_qassign	 queues;
2643b3a8eb9SGleb Smirnoff 	char			*tag;
2653b3a8eb9SGleb Smirnoff 	char			*match_tag;
2663b3a8eb9SGleb Smirnoff 	u_int8_t		 match_tag_not;
26763b3c1c7SKristof Provost 	u_int16_t		 dnpipe;
26863b3c1c7SKristof Provost 	u_int16_t		 dnrpipe;
26963b3c1c7SKristof Provost 	u_int32_t		 free_flags;
2703b3a8eb9SGleb Smirnoff 	u_int			 rtableid;
2713e248e0fSKristof Provost 	u_int8_t		 prio;
2723e248e0fSKristof Provost 	u_int8_t		 set_prio[2];
2733b3a8eb9SGleb Smirnoff 	struct {
2743b3a8eb9SGleb Smirnoff 		struct node_host	*addr;
2753b3a8eb9SGleb Smirnoff 		u_int16_t		port;
2763b3a8eb9SGleb Smirnoff 	}			 divert;
27739282ef3SKajetan Staszkiewicz 	/* new-style scrub opts */
27839282ef3SKajetan Staszkiewicz 	int			 nodf;
27939282ef3SKajetan Staszkiewicz 	int			 minttl;
28039282ef3SKajetan Staszkiewicz 	int			 settos;
28139282ef3SKajetan Staszkiewicz 	int			 randomid;
28239282ef3SKajetan Staszkiewicz 	int			 max_mss;
2833b3a8eb9SGleb Smirnoff } filter_opts;
2843b3a8eb9SGleb Smirnoff 
28513cfafabSKristof Provost static struct antispoof_opts {
2866fcc8e04SKristof Provost 	char			*label[PF_RULE_MAX_LABEL_COUNT];
2876fcc8e04SKristof Provost 	int			 labelcount;
28876c5eeccSKristof Provost 	u_int32_t		 ridentifier;
2893b3a8eb9SGleb Smirnoff 	u_int			 rtableid;
2903b3a8eb9SGleb Smirnoff } antispoof_opts;
2913b3a8eb9SGleb Smirnoff 
29213cfafabSKristof Provost static struct scrub_opts {
2933b3a8eb9SGleb Smirnoff 	int			 marker;
2943b3a8eb9SGleb Smirnoff 	int			 nodf;
2953b3a8eb9SGleb Smirnoff 	int			 minttl;
2963b3a8eb9SGleb Smirnoff 	int			 maxmss;
2973b3a8eb9SGleb Smirnoff 	int			 settos;
2983b3a8eb9SGleb Smirnoff 	int			 fragcache;
2993b3a8eb9SGleb Smirnoff 	int			 randomid;
3003b3a8eb9SGleb Smirnoff 	int			 reassemble_tcp;
3013b3a8eb9SGleb Smirnoff 	char			*match_tag;
3023b3a8eb9SGleb Smirnoff 	u_int8_t		 match_tag_not;
3033b3a8eb9SGleb Smirnoff 	u_int			 rtableid;
3043b3a8eb9SGleb Smirnoff } scrub_opts;
3053b3a8eb9SGleb Smirnoff 
30613cfafabSKristof Provost static struct queue_opts {
3073b3a8eb9SGleb Smirnoff 	int			marker;
3083b3a8eb9SGleb Smirnoff #define QOM_BWSPEC	0x01
3093b3a8eb9SGleb Smirnoff #define QOM_SCHEDULER	0x02
3103b3a8eb9SGleb Smirnoff #define QOM_PRIORITY	0x04
3113b3a8eb9SGleb Smirnoff #define QOM_TBRSIZE	0x08
3123b3a8eb9SGleb Smirnoff #define QOM_QLIMIT	0x10
3133b3a8eb9SGleb Smirnoff 	struct node_queue_bw	queue_bwspec;
3143b3a8eb9SGleb Smirnoff 	struct node_queue_opt	scheduler;
3153b3a8eb9SGleb Smirnoff 	int			priority;
316249cc75fSPatrick Kelsey 	unsigned int		tbrsize;
3173b3a8eb9SGleb Smirnoff 	int			qlimit;
3183b3a8eb9SGleb Smirnoff } queue_opts;
3193b3a8eb9SGleb Smirnoff 
32013cfafabSKristof Provost static struct table_opts {
3213b3a8eb9SGleb Smirnoff 	int			flags;
3223b3a8eb9SGleb Smirnoff 	int			init_addr;
3233b3a8eb9SGleb Smirnoff 	struct node_tinithead	init_nodes;
3243b3a8eb9SGleb Smirnoff } table_opts;
3253b3a8eb9SGleb Smirnoff 
32613cfafabSKristof Provost static struct pool_opts {
3273b3a8eb9SGleb Smirnoff 	int			 marker;
3283b3a8eb9SGleb Smirnoff #define POM_TYPE		0x01
3293b3a8eb9SGleb Smirnoff #define POM_STICKYADDRESS	0x02
330390dc369STom Jones #define POM_ENDPI		0x04
3313b3a8eb9SGleb Smirnoff 	u_int8_t		 opts;
3323b3a8eb9SGleb Smirnoff 	int			 type;
3333b3a8eb9SGleb Smirnoff 	int			 staticport;
3343b3a8eb9SGleb Smirnoff 	struct pf_poolhashkey	*key;
3352aa21096SKurosawa Takahiro 	struct pf_mape_portset	 mape;
3363b3a8eb9SGleb Smirnoff 
3373b3a8eb9SGleb Smirnoff } pool_opts;
3383b3a8eb9SGleb Smirnoff 
33913cfafabSKristof Provost static struct codel_opts	 codel_opts;
34013cfafabSKristof Provost static struct node_hfsc_opts	 hfsc_opts;
34113cfafabSKristof Provost static struct node_fairq_opts	 fairq_opts;
34213cfafabSKristof Provost static struct node_state_opt	*keep_state_defaults = NULL;
3435062afffSKristof Provost static struct pfctl_watermarks	 syncookie_opts;
3443b3a8eb9SGleb Smirnoff 
3453b3a8eb9SGleb Smirnoff int		 disallow_table(struct node_host *, const char *);
3463b3a8eb9SGleb Smirnoff int		 disallow_urpf_failed(struct node_host *, const char *);
3473b3a8eb9SGleb Smirnoff int		 disallow_alias(struct node_host *, const char *);
348e9eb0941SKristof Provost int		 rule_consistent(struct pfctl_rule *, int);
349e9eb0941SKristof Provost int		 filter_consistent(struct pfctl_rule *, int);
350e9eb0941SKristof Provost int		 nat_consistent(struct pfctl_rule *);
351e9eb0941SKristof Provost int		 rdr_consistent(struct pfctl_rule *);
3523b3a8eb9SGleb Smirnoff int		 process_tabledef(char *, struct table_opts *);
3533b3a8eb9SGleb Smirnoff void		 expand_label_str(char *, size_t, const char *, const char *);
3543b3a8eb9SGleb Smirnoff void		 expand_label_if(const char *, char *, size_t, const char *);
3559ec48bc3SKristof Provost void		 expand_label_addr(const char *, char *, size_t, sa_family_t,
35609c7f238SKristof Provost 		    struct pf_rule_addr *);
3573b3a8eb9SGleb Smirnoff void		 expand_label_port(const char *, char *, size_t,
35809c7f238SKristof Provost 		    struct pf_rule_addr *);
3593b3a8eb9SGleb Smirnoff void		 expand_label_proto(const char *, char *, size_t, u_int8_t);
36009c7f238SKristof Provost void		 expand_label_nr(const char *, char *, size_t,
36109c7f238SKristof Provost 		    struct pfctl_rule *);
3622b29ceb8SKristof Provost void		 expand_eth_rule(struct pfctl_eth_rule *,
36387a89d6eSKristof Provost 		    struct node_if *, struct node_etherproto *,
3648a42005dSKristof Provost 		    struct node_mac *, struct node_mac *,
3658a8af942SKristof Provost 		    struct node_host *, struct node_host *, const char *,
3668a8af942SKristof Provost 		    const char *);
367e9eb0941SKristof Provost void		 expand_rule(struct pfctl_rule *, struct node_if *,
3683b3a8eb9SGleb Smirnoff 		    struct node_host *, struct node_proto *, struct node_os *,
3693b3a8eb9SGleb Smirnoff 		    struct node_host *, struct node_port *, struct node_host *,
3703b3a8eb9SGleb Smirnoff 		    struct node_port *, struct node_uid *, struct node_gid *,
3712339ead6SKristof Provost 		    struct node_if *, struct node_icmp *, const char *);
3723b3a8eb9SGleb Smirnoff int		 expand_altq(struct pf_altq *, struct node_if *,
3733b3a8eb9SGleb Smirnoff 		    struct node_queue *, struct node_queue_bw bwspec,
3743b3a8eb9SGleb Smirnoff 		    struct node_queue_opt *);
3753b3a8eb9SGleb Smirnoff int		 expand_queue(struct pf_altq *, struct node_if *,
3763b3a8eb9SGleb Smirnoff 		    struct node_queue *, struct node_queue_bw,
3773b3a8eb9SGleb Smirnoff 		    struct node_queue_opt *);
3783b3a8eb9SGleb Smirnoff int		 expand_skip_interface(struct node_if *);
3793b3a8eb9SGleb Smirnoff 
3803b3a8eb9SGleb Smirnoff int	 check_rulestate(int);
3813b3a8eb9SGleb Smirnoff int	 getservice(char *);
3826fcc8e04SKristof Provost int	 rule_label(struct pfctl_rule *, char *s[PF_RULE_MAX_LABEL_COUNT]);
383ef661d4aSChristian McDonald int	 eth_rule_label(struct pfctl_eth_rule *, char *s[PF_RULE_MAX_LABEL_COUNT]);
3843b3a8eb9SGleb Smirnoff int	 rt_tableid_max(void);
3853b3a8eb9SGleb Smirnoff 
386e9eb0941SKristof Provost void	 mv_rules(struct pfctl_ruleset *, struct pfctl_ruleset *);
387c5131afeSKristof Provost void	 mv_eth_rules(struct pfctl_eth_ruleset *, struct pfctl_eth_ruleset *);
3883b3a8eb9SGleb Smirnoff void	 decide_address_family(struct node_host *, sa_family_t *);
3893b3a8eb9SGleb Smirnoff void	 remove_invalid_hosts(struct node_host **, sa_family_t *);
3903b3a8eb9SGleb Smirnoff int	 invalid_redirect(struct node_host *, sa_family_t);
3913b3a8eb9SGleb Smirnoff u_int16_t parseicmpspec(char *, sa_family_t);
3921f495578SKristof Provost int	 kw_casecmp(const void *, const void *);
3931f495578SKristof Provost int	 map_tos(char *string, int *);
394b590f17aSKristof Provost struct node_mac* node_mac_from_string(const char *);
395b590f17aSKristof Provost struct node_mac* node_mac_from_string_masklen(const char *, int);
396b590f17aSKristof Provost struct node_mac* node_mac_from_string_mask(const char *, const char *);
3973b3a8eb9SGleb Smirnoff 
39813cfafabSKristof Provost static TAILQ_HEAD(loadanchorshead, loadanchors)
3993b3a8eb9SGleb Smirnoff     loadanchorshead = TAILQ_HEAD_INITIALIZER(loadanchorshead);
4003b3a8eb9SGleb Smirnoff 
4013b3a8eb9SGleb Smirnoff struct loadanchors {
4023b3a8eb9SGleb Smirnoff 	TAILQ_ENTRY(loadanchors)	 entries;
4033b3a8eb9SGleb Smirnoff 	char				*anchorname;
4043b3a8eb9SGleb Smirnoff 	char				*filename;
4053b3a8eb9SGleb Smirnoff };
4063b3a8eb9SGleb Smirnoff 
4073b3a8eb9SGleb Smirnoff typedef struct {
4083b3a8eb9SGleb Smirnoff 	union {
4093b3a8eb9SGleb Smirnoff 		int64_t			 number;
4103b3a8eb9SGleb Smirnoff 		double			 probability;
4113b3a8eb9SGleb Smirnoff 		int			 i;
4123b3a8eb9SGleb Smirnoff 		char			*string;
4133b3a8eb9SGleb Smirnoff 		u_int			 rtableid;
4143b3a8eb9SGleb Smirnoff 		struct {
4153b3a8eb9SGleb Smirnoff 			u_int8_t	 b1;
4163b3a8eb9SGleb Smirnoff 			u_int8_t	 b2;
4173b3a8eb9SGleb Smirnoff 			u_int16_t	 w;
4183b3a8eb9SGleb Smirnoff 			u_int16_t	 w2;
4193b3a8eb9SGleb Smirnoff 		}			 b;
4203b3a8eb9SGleb Smirnoff 		struct range {
4213b3a8eb9SGleb Smirnoff 			int		 a;
4223b3a8eb9SGleb Smirnoff 			int		 b;
4233b3a8eb9SGleb Smirnoff 			int		 t;
4243b3a8eb9SGleb Smirnoff 		}			 range;
4253b3a8eb9SGleb Smirnoff 		struct node_if		*interface;
4263b3a8eb9SGleb Smirnoff 		struct node_proto	*proto;
4272b29ceb8SKristof Provost 		struct node_etherproto	*etherproto;
4283b3a8eb9SGleb Smirnoff 		struct node_icmp	*icmp;
4293b3a8eb9SGleb Smirnoff 		struct node_host	*host;
4303b3a8eb9SGleb Smirnoff 		struct node_os		*os;
4313b3a8eb9SGleb Smirnoff 		struct node_port	*port;
4323b3a8eb9SGleb Smirnoff 		struct node_uid		*uid;
4333b3a8eb9SGleb Smirnoff 		struct node_gid		*gid;
4343b3a8eb9SGleb Smirnoff 		struct node_state_opt	*state_opt;
4353b3a8eb9SGleb Smirnoff 		struct peer		 peer;
4363b3a8eb9SGleb Smirnoff 		struct {
4373b3a8eb9SGleb Smirnoff 			struct peer	 src, dst;
4383b3a8eb9SGleb Smirnoff 			struct node_os	*src_os;
4393b3a8eb9SGleb Smirnoff 		}			 fromto;
4403b3a8eb9SGleb Smirnoff 		struct {
44187a89d6eSKristof Provost 			struct node_mac	*src;
44287a89d6eSKristof Provost 			struct node_mac	*dst;
4432b29ceb8SKristof Provost 		}			 etherfromto;
44487a89d6eSKristof Provost 		struct node_mac		*mac;
4452b29ceb8SKristof Provost 		struct {
44687a89d6eSKristof Provost 			struct node_mac	*mac;
4472b29ceb8SKristof Provost 		} etheraddr;
4488a8af942SKristof Provost 		char			*bridge_to;
4492b29ceb8SKristof Provost 		struct {
4503b3a8eb9SGleb Smirnoff 			struct node_host	*host;
4513b3a8eb9SGleb Smirnoff 			u_int8_t		 rt;
4523b3a8eb9SGleb Smirnoff 			u_int8_t		 pool_opts;
4533b3a8eb9SGleb Smirnoff 			sa_family_t		 af;
4543b3a8eb9SGleb Smirnoff 			struct pf_poolhashkey	*key;
4553b3a8eb9SGleb Smirnoff 		}			 route;
4563b3a8eb9SGleb Smirnoff 		struct redirection {
4573b3a8eb9SGleb Smirnoff 			struct node_host	*host;
4583b3a8eb9SGleb Smirnoff 			struct range		 rport;
4593b3a8eb9SGleb Smirnoff 		}			*redirection;
4603b3a8eb9SGleb Smirnoff 		struct {
4613b3a8eb9SGleb Smirnoff 			int			 action;
4623b3a8eb9SGleb Smirnoff 			struct node_state_opt	*options;
4633b3a8eb9SGleb Smirnoff 		}			 keep_state;
4643b3a8eb9SGleb Smirnoff 		struct {
4653b3a8eb9SGleb Smirnoff 			u_int8_t	 log;
4663b3a8eb9SGleb Smirnoff 			u_int8_t	 logif;
4673b3a8eb9SGleb Smirnoff 			u_int8_t	 quick;
4683b3a8eb9SGleb Smirnoff 		}			 logquick;
4693b3a8eb9SGleb Smirnoff 		struct {
4703b3a8eb9SGleb Smirnoff 			int		 neg;
4713b3a8eb9SGleb Smirnoff 			char		*name;
4723b3a8eb9SGleb Smirnoff 		}			 tagged;
4733b3a8eb9SGleb Smirnoff 		struct pf_poolhashkey	*hashkey;
4743b3a8eb9SGleb Smirnoff 		struct node_queue	*queue;
4753b3a8eb9SGleb Smirnoff 		struct node_queue_opt	 queue_options;
4763b3a8eb9SGleb Smirnoff 		struct node_queue_bw	 queue_bwspec;
4773b3a8eb9SGleb Smirnoff 		struct node_qassign	 qassign;
4783b3a8eb9SGleb Smirnoff 		struct filter_opts	 filter_opts;
4793b3a8eb9SGleb Smirnoff 		struct antispoof_opts	 antispoof_opts;
4803b3a8eb9SGleb Smirnoff 		struct queue_opts	 queue_opts;
4813b3a8eb9SGleb Smirnoff 		struct scrub_opts	 scrub_opts;
4823b3a8eb9SGleb Smirnoff 		struct table_opts	 table_opts;
4833b3a8eb9SGleb Smirnoff 		struct pool_opts	 pool_opts;
4843b3a8eb9SGleb Smirnoff 		struct node_hfsc_opts	 hfsc_opts;
485a5b789f6SErmal Luçi 		struct node_fairq_opts	 fairq_opts;
4860a70aaf8SLuiz Otavio O Souza 		struct codel_opts	 codel_opts;
4875062afffSKristof Provost 		struct pfctl_watermarks	*watermarks;
4883b3a8eb9SGleb Smirnoff 	} v;
4893b3a8eb9SGleb Smirnoff 	int lineno;
4903b3a8eb9SGleb Smirnoff } YYSTYPE;
4913b3a8eb9SGleb Smirnoff 
4923b3a8eb9SGleb Smirnoff #define PPORT_RANGE	1
4933b3a8eb9SGleb Smirnoff #define PPORT_STAR	2
4943b3a8eb9SGleb Smirnoff int	parseport(char *, struct range *r, int);
4953b3a8eb9SGleb Smirnoff 
4963b3a8eb9SGleb Smirnoff #define DYNIF_MULTIADDR(addr) ((addr).type == PF_ADDR_DYNIFTL && \
4973b3a8eb9SGleb Smirnoff 	(!((addr).iflags & PFI_AFLAG_NOALIAS) ||		 \
4983b3a8eb9SGleb Smirnoff 	!isdigit((addr).v.ifname[strlen((addr).v.ifname)-1])))
4993b3a8eb9SGleb Smirnoff 
5003b3a8eb9SGleb Smirnoff %}
5013b3a8eb9SGleb Smirnoff 
502ef950daaSKristof Provost %token	PASS BLOCK MATCH SCRUB RETURN IN OS OUT LOG QUICK ON FROM TO FLAGS
5033b3a8eb9SGleb Smirnoff %token	RETURNRST RETURNICMP RETURNICMP6 PROTO INET INET6 ALL ANY ICMPTYPE
5043b3a8eb9SGleb Smirnoff %token	ICMP6TYPE CODE KEEP MODULATE STATE PORT RDR NAT BINAT ARROW NODF
5053b3a8eb9SGleb Smirnoff %token	MINTTL ERROR ALLOWOPTS FASTROUTE FILENAME ROUTETO DUPTO REPLYTO NO LABEL
5063b3a8eb9SGleb Smirnoff %token	NOROUTE URPFFAILED FRAGMENT USER GROUP MAXMSS MAXIMUM TTL TOS DROP TABLE
50788e858e5SKristof Provost %token	REASSEMBLE ANCHOR NATANCHOR RDRANCHOR BINATANCHOR
508150182e3SKristof Provost %token	SET OPTIMIZATION TIMEOUT LIMIT LOGINTERFACE BLOCKPOLICY FAILPOLICY
509150182e3SKristof Provost %token	RANDOMID REQUIREORDER SYNPROXY FINGERPRINTS NOSYNC DEBUG SKIP HOSTID
510f3ab00c2SKristof Provost %token	ANTISPOOF FOR INCLUDE KEEPCOUNTERS SYNCOOKIES L3 MATCHES
5112b29ceb8SKristof Provost %token	ETHER
5122aa21096SKurosawa Takahiro %token	BITMASK RANDOM SOURCEHASH ROUNDROBIN STATICPORT PROBABILITY MAPEPORTSET
5130a70aaf8SLuiz Otavio O Souza %token	ALTQ CBQ CODEL PRIQ HFSC FAIRQ BANDWIDTH TBRSIZE LINKSHARE REALTIME
5140a70aaf8SLuiz Otavio O Souza %token	UPPERLIMIT QUEUE PRIORITY QLIMIT HOGS BUCKETS RTABLE TARGET INTERVAL
51576c5eeccSKristof Provost %token	DNPIPE DNQUEUE RIDENTIFIER
5163e248e0fSKristof Provost %token	LOAD RULESET_OPTIMIZATION PRIO
517390dc369STom Jones %token	STICKYADDRESS ENDPI MAXSRCSTATES MAXSRCNODES SOURCETRACK GLOBAL RULE
518baf9b6d0SKristof Provost %token	MAXSRCCONN MAXSRCCONNRATE OVERLOAD FLUSH SLOPPY PFLOW
5193b3a8eb9SGleb Smirnoff %token	TAGGED TAG IFBOUND FLOATING STATEPOLICY STATEDEFAULTS ROUTE SETTOS
52080eb861dSKristof Provost %token	DIVERTTO DIVERTREPLY BRIDGE_TO RECEIVEDON NE LE GE
5213b3a8eb9SGleb Smirnoff %token	<v.string>		STRING
5223b3a8eb9SGleb Smirnoff %token	<v.number>		NUMBER
5233b3a8eb9SGleb Smirnoff %token	<v.i>			PORTBINARY
5243b3a8eb9SGleb Smirnoff %type	<v.interface>		interface if_list if_item_not if_item
5253b3a8eb9SGleb Smirnoff %type	<v.number>		number icmptype icmp6type uid gid
52639282ef3SKajetan Staszkiewicz %type	<v.number>		tos not yesno optnodf
5273b3a8eb9SGleb Smirnoff %type	<v.probability>		probability
528c69121c4SKristof Provost %type	<v.i>			no dir af fragcache optimizer syncookie_val
5293b3a8eb9SGleb Smirnoff %type	<v.i>			sourcetrack flush unaryop statelock
5302b29ceb8SKristof Provost %type	<v.i>			etherprotoval
5313b3a8eb9SGleb Smirnoff %type	<v.b>			action nataction natpasslog scrubaction
5323e248e0fSKristof Provost %type	<v.b>			flags flag blockspec prio
5333b3a8eb9SGleb Smirnoff %type	<v.range>		portplain portstar portrange
5343b3a8eb9SGleb Smirnoff %type	<v.hashkey>		hashkey
5353b3a8eb9SGleb Smirnoff %type	<v.proto>		proto proto_list proto_item
5363b3a8eb9SGleb Smirnoff %type	<v.number>		protoval
5373b3a8eb9SGleb Smirnoff %type	<v.icmp>		icmpspec
5383b3a8eb9SGleb Smirnoff %type	<v.icmp>		icmp_list icmp_item
5393b3a8eb9SGleb Smirnoff %type	<v.icmp>		icmp6_list icmp6_item
5403b3a8eb9SGleb Smirnoff %type	<v.number>		reticmpspec reticmp6spec
5418a42005dSKristof Provost %type	<v.fromto>		fromto l3fromto
5423b3a8eb9SGleb Smirnoff %type	<v.peer>		ipportspec from to
5433b3a8eb9SGleb Smirnoff %type	<v.host>		ipspec toipspec xhost host dynaddr host_list
5443b3a8eb9SGleb Smirnoff %type	<v.host>		redir_host_list redirspec
5453b3a8eb9SGleb Smirnoff %type	<v.host>		route_host route_host_list routespec
5463b3a8eb9SGleb Smirnoff %type	<v.os>			os xos os_list
5473b3a8eb9SGleb Smirnoff %type	<v.port>		portspec port_list port_item
5483b3a8eb9SGleb Smirnoff %type	<v.uid>			uids uid_list uid_item
5493b3a8eb9SGleb Smirnoff %type	<v.gid>			gids gid_list gid_item
5503b3a8eb9SGleb Smirnoff %type	<v.route>		route
5513b3a8eb9SGleb Smirnoff %type	<v.redirection>		redirection redirpool
5523b3a8eb9SGleb Smirnoff %type	<v.string>		label stringall tag anchorname
5533b3a8eb9SGleb Smirnoff %type	<v.string>		string varstring numberstring
5543b3a8eb9SGleb Smirnoff %type	<v.keep_state>		keep
5553b3a8eb9SGleb Smirnoff %type	<v.state_opt>		state_opt_spec state_opt_list state_opt_item
5563b3a8eb9SGleb Smirnoff %type	<v.logquick>		logquick quick log logopts logopt
5573b3a8eb9SGleb Smirnoff %type	<v.interface>		antispoof_ifspc antispoof_iflst antispoof_if
5582b29ceb8SKristof Provost %type	<v.qassign>		qname etherqname
5593b3a8eb9SGleb Smirnoff %type	<v.queue>		qassign qassign_list qassign_item
5603b3a8eb9SGleb Smirnoff %type	<v.queue_options>	scheduler
5613b3a8eb9SGleb Smirnoff %type	<v.number>		cbqflags_list cbqflags_item
5623b3a8eb9SGleb Smirnoff %type	<v.number>		priqflags_list priqflags_item
5633b3a8eb9SGleb Smirnoff %type	<v.hfsc_opts>		hfscopts_list hfscopts_item hfsc_opts
564a5b789f6SErmal Luçi %type	<v.fairq_opts>		fairqopts_list fairqopts_item fairq_opts
5650a70aaf8SLuiz Otavio O Souza %type	<v.codel_opts>		codelopts_list codelopts_item codel_opts
5663b3a8eb9SGleb Smirnoff %type	<v.queue_bwspec>	bandwidth
5672b29ceb8SKristof Provost %type	<v.filter_opts>		filter_opts filter_opt filter_opts_l etherfilter_opts etherfilter_opt etherfilter_opts_l
5683e248e0fSKristof Provost %type	<v.filter_opts>		filter_sets filter_set filter_sets_l
5693b3a8eb9SGleb Smirnoff %type	<v.antispoof_opts>	antispoof_opts antispoof_opt antispoof_opts_l
5703b3a8eb9SGleb Smirnoff %type	<v.queue_opts>		queue_opts queue_opt queue_opts_l
5713b3a8eb9SGleb Smirnoff %type	<v.scrub_opts>		scrub_opts scrub_opt scrub_opts_l
5723b3a8eb9SGleb Smirnoff %type	<v.table_opts>		table_opts table_opt table_opts_l
5733b3a8eb9SGleb Smirnoff %type	<v.pool_opts>		pool_opts pool_opt pool_opts_l
5743b3a8eb9SGleb Smirnoff %type	<v.tagged>		tagged
5753b3a8eb9SGleb Smirnoff %type	<v.rtableid>		rtable
5765062afffSKristof Provost %type	<v.watermarks>		syncookie_opts
5772b29ceb8SKristof Provost %type	<v.etherproto>		etherproto etherproto_list etherproto_item
5782b29ceb8SKristof Provost %type	<v.etherfromto>		etherfromto
5792b29ceb8SKristof Provost %type	<v.etheraddr>		etherfrom etherto
5808a8af942SKristof Provost %type	<v.bridge_to>		bridge
58187a89d6eSKristof Provost %type	<v.mac>			xmac mac mac_list macspec
5823b3a8eb9SGleb Smirnoff %%
5833b3a8eb9SGleb Smirnoff 
5843b3a8eb9SGleb Smirnoff ruleset		: /* empty */
5853b3a8eb9SGleb Smirnoff 		| ruleset include '\n'
5863b3a8eb9SGleb Smirnoff 		| ruleset '\n'
5873b3a8eb9SGleb Smirnoff 		| ruleset option '\n'
5882b29ceb8SKristof Provost 		| ruleset etherrule '\n'
589c5131afeSKristof Provost 		| ruleset etheranchorrule '\n'
5903b3a8eb9SGleb Smirnoff 		| ruleset scrubrule '\n'
5913b3a8eb9SGleb Smirnoff 		| ruleset natrule '\n'
5923b3a8eb9SGleb Smirnoff 		| ruleset binatrule '\n'
5933b3a8eb9SGleb Smirnoff 		| ruleset pfrule '\n'
5943b3a8eb9SGleb Smirnoff 		| ruleset anchorrule '\n'
5953b3a8eb9SGleb Smirnoff 		| ruleset loadrule '\n'
5963b3a8eb9SGleb Smirnoff 		| ruleset altqif '\n'
5973b3a8eb9SGleb Smirnoff 		| ruleset queuespec '\n'
5983b3a8eb9SGleb Smirnoff 		| ruleset varset '\n'
5993b3a8eb9SGleb Smirnoff 		| ruleset antispoof '\n'
6003b3a8eb9SGleb Smirnoff 		| ruleset tabledef '\n'
6013b3a8eb9SGleb Smirnoff 		| '{' fakeanchor '}' '\n';
6023b3a8eb9SGleb Smirnoff 		| ruleset error '\n'		{ file->errors++; }
6033b3a8eb9SGleb Smirnoff 		;
6043b3a8eb9SGleb Smirnoff 
6053b3a8eb9SGleb Smirnoff include		: INCLUDE STRING		{
6063b3a8eb9SGleb Smirnoff 			struct file	*nfile;
6073b3a8eb9SGleb Smirnoff 
6083b3a8eb9SGleb Smirnoff 			if ((nfile = pushfile($2, 0)) == NULL) {
6093b3a8eb9SGleb Smirnoff 				yyerror("failed to include file %s", $2);
6103b3a8eb9SGleb Smirnoff 				free($2);
6113b3a8eb9SGleb Smirnoff 				YYERROR;
6123b3a8eb9SGleb Smirnoff 			}
6133b3a8eb9SGleb Smirnoff 			free($2);
6143b3a8eb9SGleb Smirnoff 
6153b3a8eb9SGleb Smirnoff 			file = nfile;
6163b3a8eb9SGleb Smirnoff 			lungetc('\n');
6173b3a8eb9SGleb Smirnoff 		}
6183b3a8eb9SGleb Smirnoff 		;
6193b3a8eb9SGleb Smirnoff 
6203b3a8eb9SGleb Smirnoff /*
6213b3a8eb9SGleb Smirnoff  * apply to previouslys specified rule: must be careful to note
6223b3a8eb9SGleb Smirnoff  * what that is: pf or nat or binat or rdr
6233b3a8eb9SGleb Smirnoff  */
6243b3a8eb9SGleb Smirnoff fakeanchor	: fakeanchor '\n'
6253b3a8eb9SGleb Smirnoff 		| fakeanchor anchorrule '\n'
6263b3a8eb9SGleb Smirnoff 		| fakeanchor binatrule '\n'
6273b3a8eb9SGleb Smirnoff 		| fakeanchor natrule '\n'
6283b3a8eb9SGleb Smirnoff 		| fakeanchor pfrule '\n'
6293b3a8eb9SGleb Smirnoff 		| fakeanchor error '\n'
6303b3a8eb9SGleb Smirnoff 		;
6313b3a8eb9SGleb Smirnoff 
6323b3a8eb9SGleb Smirnoff optimizer	: string	{
6333b3a8eb9SGleb Smirnoff 			if (!strcmp($1, "none"))
6343b3a8eb9SGleb Smirnoff 				$$ = 0;
6353b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "basic"))
6363b3a8eb9SGleb Smirnoff 				$$ = PF_OPTIMIZE_BASIC;
6373b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "profile"))
6383b3a8eb9SGleb Smirnoff 				$$ = PF_OPTIMIZE_BASIC | PF_OPTIMIZE_PROFILE;
6393b3a8eb9SGleb Smirnoff 			else {
6403b3a8eb9SGleb Smirnoff 				yyerror("unknown ruleset-optimization %s", $1);
6413b3a8eb9SGleb Smirnoff 				YYERROR;
6423b3a8eb9SGleb Smirnoff 			}
6433b3a8eb9SGleb Smirnoff 		}
6443b3a8eb9SGleb Smirnoff 		;
6453b3a8eb9SGleb Smirnoff 
64639282ef3SKajetan Staszkiewicz optnodf		: /* empty */	{ $$ = 0; }
64739282ef3SKajetan Staszkiewicz 		| NODF		{ $$ = 1; }
64839282ef3SKajetan Staszkiewicz 		;
64939282ef3SKajetan Staszkiewicz 
65039282ef3SKajetan Staszkiewicz option		: SET REASSEMBLE yesno optnodf		{
65139282ef3SKajetan Staszkiewicz 			if (check_rulestate(PFCTL_STATE_OPTION))
65239282ef3SKajetan Staszkiewicz 				YYERROR;
65339282ef3SKajetan Staszkiewicz 			pfctl_set_reassembly(pf, $3, $4);
65439282ef3SKajetan Staszkiewicz 		}
65539282ef3SKajetan Staszkiewicz 		| SET OPTIMIZATION STRING		{
6563b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_OPTION)) {
6573b3a8eb9SGleb Smirnoff 				free($3);
6583b3a8eb9SGleb Smirnoff 				YYERROR;
6593b3a8eb9SGleb Smirnoff 			}
6603b3a8eb9SGleb Smirnoff 			if (pfctl_set_optimization(pf, $3) != 0) {
6613b3a8eb9SGleb Smirnoff 				yyerror("unknown optimization %s", $3);
6623b3a8eb9SGleb Smirnoff 				free($3);
6633b3a8eb9SGleb Smirnoff 				YYERROR;
6643b3a8eb9SGleb Smirnoff 			}
6653b3a8eb9SGleb Smirnoff 			free($3);
6663b3a8eb9SGleb Smirnoff 		}
6673b3a8eb9SGleb Smirnoff 		| SET RULESET_OPTIMIZATION optimizer {
6683b3a8eb9SGleb Smirnoff 			if (!(pf->opts & PF_OPT_OPTIMIZE)) {
6693b3a8eb9SGleb Smirnoff 				pf->opts |= PF_OPT_OPTIMIZE;
6703b3a8eb9SGleb Smirnoff 				pf->optimize = $3;
6713b3a8eb9SGleb Smirnoff 			}
6723b3a8eb9SGleb Smirnoff 		}
6733b3a8eb9SGleb Smirnoff 		| SET TIMEOUT timeout_spec
6743b3a8eb9SGleb Smirnoff 		| SET TIMEOUT '{' optnl timeout_list '}'
6753b3a8eb9SGleb Smirnoff 		| SET LIMIT limit_spec
6763b3a8eb9SGleb Smirnoff 		| SET LIMIT '{' optnl limit_list '}'
6773b3a8eb9SGleb Smirnoff 		| SET LOGINTERFACE stringall		{
6783b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_OPTION)) {
6793b3a8eb9SGleb Smirnoff 				free($3);
6803b3a8eb9SGleb Smirnoff 				YYERROR;
6813b3a8eb9SGleb Smirnoff 			}
6823b3a8eb9SGleb Smirnoff 			if (pfctl_set_logif(pf, $3) != 0) {
6833b3a8eb9SGleb Smirnoff 				yyerror("error setting loginterface %s", $3);
6843b3a8eb9SGleb Smirnoff 				free($3);
6853b3a8eb9SGleb Smirnoff 				YYERROR;
6863b3a8eb9SGleb Smirnoff 			}
6873b3a8eb9SGleb Smirnoff 			free($3);
6883b3a8eb9SGleb Smirnoff 		}
6893b3a8eb9SGleb Smirnoff 		| SET HOSTID number {
6903b3a8eb9SGleb Smirnoff 			if ($3 == 0 || $3 > UINT_MAX) {
6913b3a8eb9SGleb Smirnoff 				yyerror("hostid must be non-zero");
6923b3a8eb9SGleb Smirnoff 				YYERROR;
6933b3a8eb9SGleb Smirnoff 			}
6943b3a8eb9SGleb Smirnoff 			if (pfctl_set_hostid(pf, $3) != 0) {
6953b3a8eb9SGleb Smirnoff 				yyerror("error setting hostid %08x", $3);
6963b3a8eb9SGleb Smirnoff 				YYERROR;
6973b3a8eb9SGleb Smirnoff 			}
6983b3a8eb9SGleb Smirnoff 		}
6993b3a8eb9SGleb Smirnoff 		| SET BLOCKPOLICY DROP	{
7003b3a8eb9SGleb Smirnoff 			if (pf->opts & PF_OPT_VERBOSE)
7013b3a8eb9SGleb Smirnoff 				printf("set block-policy drop\n");
7023b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_OPTION))
7033b3a8eb9SGleb Smirnoff 				YYERROR;
7043b3a8eb9SGleb Smirnoff 			blockpolicy = PFRULE_DROP;
7053b3a8eb9SGleb Smirnoff 		}
7063b3a8eb9SGleb Smirnoff 		| SET BLOCKPOLICY RETURN {
7073b3a8eb9SGleb Smirnoff 			if (pf->opts & PF_OPT_VERBOSE)
7083b3a8eb9SGleb Smirnoff 				printf("set block-policy return\n");
7093b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_OPTION))
7103b3a8eb9SGleb Smirnoff 				YYERROR;
7113b3a8eb9SGleb Smirnoff 			blockpolicy = PFRULE_RETURN;
7123b3a8eb9SGleb Smirnoff 		}
713150182e3SKristof Provost 		| SET FAILPOLICY DROP	{
714150182e3SKristof Provost 			if (pf->opts & PF_OPT_VERBOSE)
715150182e3SKristof Provost 				printf("set fail-policy drop\n");
716150182e3SKristof Provost 			if (check_rulestate(PFCTL_STATE_OPTION))
717150182e3SKristof Provost 				YYERROR;
718150182e3SKristof Provost 			failpolicy = PFRULE_DROP;
719150182e3SKristof Provost 		}
720150182e3SKristof Provost 		| SET FAILPOLICY RETURN {
721150182e3SKristof Provost 			if (pf->opts & PF_OPT_VERBOSE)
722150182e3SKristof Provost 				printf("set fail-policy return\n");
723150182e3SKristof Provost 			if (check_rulestate(PFCTL_STATE_OPTION))
724150182e3SKristof Provost 				YYERROR;
725150182e3SKristof Provost 			failpolicy = PFRULE_RETURN;
726150182e3SKristof Provost 		}
7273b3a8eb9SGleb Smirnoff 		| SET REQUIREORDER yesno {
7283b3a8eb9SGleb Smirnoff 			if (pf->opts & PF_OPT_VERBOSE)
7293b3a8eb9SGleb Smirnoff 				printf("set require-order %s\n",
7303b3a8eb9SGleb Smirnoff 				    $3 == 1 ? "yes" : "no");
7313b3a8eb9SGleb Smirnoff 			require_order = $3;
7323b3a8eb9SGleb Smirnoff 		}
7333b3a8eb9SGleb Smirnoff 		| SET FINGERPRINTS STRING {
7343b3a8eb9SGleb Smirnoff 			if (pf->opts & PF_OPT_VERBOSE)
7353b3a8eb9SGleb Smirnoff 				printf("set fingerprints \"%s\"\n", $3);
7363b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_OPTION)) {
7373b3a8eb9SGleb Smirnoff 				free($3);
7383b3a8eb9SGleb Smirnoff 				YYERROR;
7393b3a8eb9SGleb Smirnoff 			}
7403b3a8eb9SGleb Smirnoff 			if (!pf->anchor->name[0]) {
7413b3a8eb9SGleb Smirnoff 				if (pfctl_file_fingerprints(pf->dev,
7423b3a8eb9SGleb Smirnoff 				    pf->opts, $3)) {
7433b3a8eb9SGleb Smirnoff 					yyerror("error loading "
7443b3a8eb9SGleb Smirnoff 					    "fingerprints %s", $3);
7453b3a8eb9SGleb Smirnoff 					free($3);
7463b3a8eb9SGleb Smirnoff 					YYERROR;
7473b3a8eb9SGleb Smirnoff 				}
7483b3a8eb9SGleb Smirnoff 			}
7493b3a8eb9SGleb Smirnoff 			free($3);
7503b3a8eb9SGleb Smirnoff 		}
7513b3a8eb9SGleb Smirnoff 		| SET STATEPOLICY statelock {
7523b3a8eb9SGleb Smirnoff 			if (pf->opts & PF_OPT_VERBOSE)
7533b3a8eb9SGleb Smirnoff 				switch ($3) {
7543b3a8eb9SGleb Smirnoff 				case 0:
7553b3a8eb9SGleb Smirnoff 					printf("set state-policy floating\n");
7563b3a8eb9SGleb Smirnoff 					break;
7573b3a8eb9SGleb Smirnoff 				case PFRULE_IFBOUND:
7583b3a8eb9SGleb Smirnoff 					printf("set state-policy if-bound\n");
7593b3a8eb9SGleb Smirnoff 					break;
7603b3a8eb9SGleb Smirnoff 				}
7613b3a8eb9SGleb Smirnoff 			default_statelock = $3;
7623b3a8eb9SGleb Smirnoff 		}
7633b3a8eb9SGleb Smirnoff 		| SET DEBUG STRING {
7643b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_OPTION)) {
7653b3a8eb9SGleb Smirnoff 				free($3);
7663b3a8eb9SGleb Smirnoff 				YYERROR;
7673b3a8eb9SGleb Smirnoff 			}
768c36c90a2SKristof Provost 			if (pfctl_do_set_debug(pf, $3) != 0) {
7693b3a8eb9SGleb Smirnoff 				yyerror("error setting debuglevel %s", $3);
7703b3a8eb9SGleb Smirnoff 				free($3);
7713b3a8eb9SGleb Smirnoff 				YYERROR;
7723b3a8eb9SGleb Smirnoff 			}
7733b3a8eb9SGleb Smirnoff 			free($3);
7743b3a8eb9SGleb Smirnoff 		}
7753b3a8eb9SGleb Smirnoff 		| SET SKIP interface {
7763b3a8eb9SGleb Smirnoff 			if (expand_skip_interface($3) != 0) {
7773b3a8eb9SGleb Smirnoff 				yyerror("error setting skip interface(s)");
7783b3a8eb9SGleb Smirnoff 				YYERROR;
7793b3a8eb9SGleb Smirnoff 			}
7803b3a8eb9SGleb Smirnoff 		}
7813b3a8eb9SGleb Smirnoff 		| SET STATEDEFAULTS state_opt_list {
7823b3a8eb9SGleb Smirnoff 			if (keep_state_defaults != NULL) {
7833b3a8eb9SGleb Smirnoff 				yyerror("cannot redefine state-defaults");
7843b3a8eb9SGleb Smirnoff 				YYERROR;
7853b3a8eb9SGleb Smirnoff 			}
7863b3a8eb9SGleb Smirnoff 			keep_state_defaults = $3;
7873b3a8eb9SGleb Smirnoff 		}
78842ec75f8SKristof Provost 		| SET KEEPCOUNTERS {
78942ec75f8SKristof Provost 			pf->keep_counters = true;
79042ec75f8SKristof Provost 		}
7915062afffSKristof Provost 		| SET SYNCOOKIES syncookie_val syncookie_opts {
7925062afffSKristof Provost 			if (pfctl_cfg_syncookies(pf, $3, $4)) {
7935062afffSKristof Provost 				yyerror("error setting syncookies");
7945062afffSKristof Provost 				YYERROR;
7955062afffSKristof Provost 			}
796c69121c4SKristof Provost 		}
797c69121c4SKristof Provost 		;
798c69121c4SKristof Provost 
799c69121c4SKristof Provost syncookie_val  : STRING        {
800c69121c4SKristof Provost 			if (!strcmp($1, "never"))
801c69121c4SKristof Provost 				$$ = PFCTL_SYNCOOKIES_NEVER;
8025062afffSKristof Provost 			else if (!strcmp($1, "adaptive"))
8035062afffSKristof Provost 				$$ = PFCTL_SYNCOOKIES_ADAPTIVE;
804c69121c4SKristof Provost 			else if (!strcmp($1, "always"))
805c69121c4SKristof Provost 				$$ = PFCTL_SYNCOOKIES_ALWAYS;
806c69121c4SKristof Provost 			else {
807c69121c4SKristof Provost 				yyerror("illegal value for syncookies");
808c69121c4SKristof Provost 				YYERROR;
809c69121c4SKristof Provost 			}
810c69121c4SKristof Provost 		}
8113b3a8eb9SGleb Smirnoff 		;
8125062afffSKristof Provost syncookie_opts  : /* empty */                   { $$ = NULL; }
8135062afffSKristof Provost 		| {
8145062afffSKristof Provost 			memset(&syncookie_opts, 0, sizeof(syncookie_opts));
8155062afffSKristof Provost 		  } '(' syncookie_opt_l ')'     { $$ = &syncookie_opts; }
8165062afffSKristof Provost 		;
8175062afffSKristof Provost 
8185062afffSKristof Provost syncookie_opt_l : syncookie_opt_l comma syncookie_opt
8195062afffSKristof Provost 		| syncookie_opt
8205062afffSKristof Provost 		;
8215062afffSKristof Provost 
8225062afffSKristof Provost syncookie_opt   : STRING STRING {
8235062afffSKristof Provost 			double   val;
8245062afffSKristof Provost 			char    *cp;
8255062afffSKristof Provost 
8265062afffSKristof Provost 			val = strtod($2, &cp);
8275062afffSKristof Provost 			if (cp == NULL || strcmp(cp, "%"))
8285062afffSKristof Provost 				YYERROR;
8295062afffSKristof Provost 			if (val <= 0 || val > 100) {
8305062afffSKristof Provost 				yyerror("illegal percentage value");
8315062afffSKristof Provost 				YYERROR;
8325062afffSKristof Provost 			}
8335062afffSKristof Provost 			if (!strcmp($1, "start")) {
8345062afffSKristof Provost 				syncookie_opts.hi = val;
8355062afffSKristof Provost 			} else if (!strcmp($1, "end")) {
8365062afffSKristof Provost 				syncookie_opts.lo = val;
8375062afffSKristof Provost 			} else {
8385062afffSKristof Provost 				yyerror("illegal syncookie option");
8395062afffSKristof Provost 				YYERROR;
8405062afffSKristof Provost 			}
8415062afffSKristof Provost 		}
8425062afffSKristof Provost 		;
8433b3a8eb9SGleb Smirnoff 
8443b3a8eb9SGleb Smirnoff stringall	: STRING	{ $$ = $1; }
8453b3a8eb9SGleb Smirnoff 		| ALL		{
8463b3a8eb9SGleb Smirnoff 			if (($$ = strdup("all")) == NULL) {
8473b3a8eb9SGleb Smirnoff 				err(1, "stringall: strdup");
8483b3a8eb9SGleb Smirnoff 			}
8493b3a8eb9SGleb Smirnoff 		}
8503b3a8eb9SGleb Smirnoff 		;
8513b3a8eb9SGleb Smirnoff 
8523b3a8eb9SGleb Smirnoff string		: STRING string				{
8533b3a8eb9SGleb Smirnoff 			if (asprintf(&$$, "%s %s", $1, $2) == -1)
8543b3a8eb9SGleb Smirnoff 				err(1, "string: asprintf");
8553b3a8eb9SGleb Smirnoff 			free($1);
8563b3a8eb9SGleb Smirnoff 			free($2);
8573b3a8eb9SGleb Smirnoff 		}
8583b3a8eb9SGleb Smirnoff 		| STRING
8593b3a8eb9SGleb Smirnoff 		;
8603b3a8eb9SGleb Smirnoff 
8613b3a8eb9SGleb Smirnoff varstring	: numberstring varstring 		{
8623b3a8eb9SGleb Smirnoff 			if (asprintf(&$$, "%s %s", $1, $2) == -1)
8633b3a8eb9SGleb Smirnoff 				err(1, "string: asprintf");
8643b3a8eb9SGleb Smirnoff 			free($1);
8653b3a8eb9SGleb Smirnoff 			free($2);
8663b3a8eb9SGleb Smirnoff 		}
8673b3a8eb9SGleb Smirnoff 		| numberstring
8683b3a8eb9SGleb Smirnoff 		;
8693b3a8eb9SGleb Smirnoff 
8703b3a8eb9SGleb Smirnoff numberstring	: NUMBER				{
8713b3a8eb9SGleb Smirnoff 			char	*s;
8723b3a8eb9SGleb Smirnoff 			if (asprintf(&s, "%lld", (long long)$1) == -1) {
8733b3a8eb9SGleb Smirnoff 				yyerror("string: asprintf");
8743b3a8eb9SGleb Smirnoff 				YYERROR;
8753b3a8eb9SGleb Smirnoff 			}
8763b3a8eb9SGleb Smirnoff 			$$ = s;
8773b3a8eb9SGleb Smirnoff 		}
8783b3a8eb9SGleb Smirnoff 		| STRING
8793b3a8eb9SGleb Smirnoff 		;
8803b3a8eb9SGleb Smirnoff 
8813b3a8eb9SGleb Smirnoff varset		: STRING '=' varstring	{
882d3f65324SKristof Provost 			char *s = $1;
8833b3a8eb9SGleb Smirnoff 			if (pf->opts & PF_OPT_VERBOSE)
8843b3a8eb9SGleb Smirnoff 				printf("%s = \"%s\"\n", $1, $3);
885d3f65324SKristof Provost 			while (*s++) {
886d3f65324SKristof Provost 				if (isspace((unsigned char)*s)) {
887d3f65324SKristof Provost 					yyerror("macro name cannot contain "
888d3f65324SKristof Provost 					   "whitespace");
889d3f65324SKristof Provost 					YYERROR;
890d3f65324SKristof Provost 				}
891d3f65324SKristof Provost 			}
8923b3a8eb9SGleb Smirnoff 			if (symset($1, $3, 0) == -1)
8933b3a8eb9SGleb Smirnoff 				err(1, "cannot store variable %s", $1);
8943b3a8eb9SGleb Smirnoff 			free($1);
8953b3a8eb9SGleb Smirnoff 			free($3);
8963b3a8eb9SGleb Smirnoff 		}
8973b3a8eb9SGleb Smirnoff 		;
8983b3a8eb9SGleb Smirnoff 
8993b3a8eb9SGleb Smirnoff anchorname	: STRING			{ $$ = $1; }
9003b3a8eb9SGleb Smirnoff 		| /* empty */			{ $$ = NULL; }
9013b3a8eb9SGleb Smirnoff 		;
9023b3a8eb9SGleb Smirnoff 
9033b3a8eb9SGleb Smirnoff pfa_anchorlist	: /* empty */
9043b3a8eb9SGleb Smirnoff 		| pfa_anchorlist '\n'
9053b3a8eb9SGleb Smirnoff 		| pfa_anchorlist pfrule '\n'
9063b3a8eb9SGleb Smirnoff 		| pfa_anchorlist anchorrule '\n'
9073b3a8eb9SGleb Smirnoff 		;
9083b3a8eb9SGleb Smirnoff 
9093b3a8eb9SGleb Smirnoff pfa_anchor	: '{'
9103b3a8eb9SGleb Smirnoff 		{
9113b3a8eb9SGleb Smirnoff 			char ta[PF_ANCHOR_NAME_SIZE];
912e9eb0941SKristof Provost 			struct pfctl_ruleset *rs;
9133b3a8eb9SGleb Smirnoff 
9142fa6223aSGordon Bergling 			/* stepping into a brace anchor */
9153b3a8eb9SGleb Smirnoff 			pf->asd++;
9163b3a8eb9SGleb Smirnoff 			pf->bn++;
9173b3a8eb9SGleb Smirnoff 
918585a5ed0SKristof Provost 			/*
919585a5ed0SKristof Provost 			* Anchor contents are parsed before the anchor rule
920585a5ed0SKristof Provost 			* production completes, so we don't know the real
921585a5ed0SKristof Provost 			* location yet. Create a holding ruleset in the root;
922585a5ed0SKristof Provost 			* contents will be moved afterwards.
923585a5ed0SKristof Provost 			*/
9243b3a8eb9SGleb Smirnoff 			snprintf(ta, PF_ANCHOR_NAME_SIZE, "_%d", pf->bn);
9253b3a8eb9SGleb Smirnoff 			rs = pf_find_or_create_ruleset(ta);
9263b3a8eb9SGleb Smirnoff 			if (rs == NULL)
9273b3a8eb9SGleb Smirnoff 				err(1, "pfa_anchor: pf_find_or_create_ruleset");
9283b3a8eb9SGleb Smirnoff 			pf->astack[pf->asd] = rs->anchor;
9293b3a8eb9SGleb Smirnoff 			pf->anchor = rs->anchor;
9303b3a8eb9SGleb Smirnoff 		} '\n' pfa_anchorlist '}'
9313b3a8eb9SGleb Smirnoff 		{
9323b3a8eb9SGleb Smirnoff 			pf->alast = pf->anchor;
9333b3a8eb9SGleb Smirnoff 			pf->asd--;
9343b3a8eb9SGleb Smirnoff 			pf->anchor = pf->astack[pf->asd];
9353b3a8eb9SGleb Smirnoff 		}
9363b3a8eb9SGleb Smirnoff 		| /* empty */
9373b3a8eb9SGleb Smirnoff 		;
9383b3a8eb9SGleb Smirnoff 
9393b3a8eb9SGleb Smirnoff anchorrule	: ANCHOR anchorname dir quick interface af proto fromto
9403b3a8eb9SGleb Smirnoff 		    filter_opts pfa_anchor
9413b3a8eb9SGleb Smirnoff 		{
942e9eb0941SKristof Provost 			struct pfctl_rule	r;
9433b3a8eb9SGleb Smirnoff 			struct node_proto	*proto;
9443b3a8eb9SGleb Smirnoff 
9453b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_FILTER)) {
9463b3a8eb9SGleb Smirnoff 				if ($2)
9473b3a8eb9SGleb Smirnoff 					free($2);
9483b3a8eb9SGleb Smirnoff 				YYERROR;
9493b3a8eb9SGleb Smirnoff 			}
9503b3a8eb9SGleb Smirnoff 
9513b3a8eb9SGleb Smirnoff 			if ($2 && ($2[0] == '_' || strstr($2, "/_") != NULL)) {
9523b3a8eb9SGleb Smirnoff 				free($2);
9533b3a8eb9SGleb Smirnoff 				yyerror("anchor names beginning with '_' "
9543b3a8eb9SGleb Smirnoff 				    "are reserved for internal use");
9553b3a8eb9SGleb Smirnoff 				YYERROR;
9563b3a8eb9SGleb Smirnoff 			}
9573b3a8eb9SGleb Smirnoff 
9583b3a8eb9SGleb Smirnoff 			memset(&r, 0, sizeof(r));
9593b3a8eb9SGleb Smirnoff 			if (pf->astack[pf->asd + 1]) {
960585a5ed0SKristof Provost 				if ($2 && strchr($2, '/') != NULL) {
961585a5ed0SKristof Provost 					free($2);
962585a5ed0SKristof Provost 					yyerror("anchor paths containing '/' "
963585a5ed0SKristof Provost 					   "cannot be used for inline anchors.");
964585a5ed0SKristof Provost 					YYERROR;
965585a5ed0SKristof Provost 				}
966585a5ed0SKristof Provost 
967585a5ed0SKristof Provost 				/* Move inline rules into relative location. */
968e9eb0941SKristof Provost 				pfctl_anchor_setup(&r,
9693b3a8eb9SGleb Smirnoff 				    &pf->astack[pf->asd]->ruleset,
9703b3a8eb9SGleb Smirnoff 				    $2 ? $2 : pf->alast->name);
9713b3a8eb9SGleb Smirnoff 
9723b3a8eb9SGleb Smirnoff 				if (r.anchor == NULL)
9733b3a8eb9SGleb Smirnoff 					err(1, "anchorrule: unable to "
9743b3a8eb9SGleb Smirnoff 					    "create ruleset");
9753b3a8eb9SGleb Smirnoff 
9763b3a8eb9SGleb Smirnoff 				if (pf->alast != r.anchor) {
9773b3a8eb9SGleb Smirnoff 					if (r.anchor->match) {
9783b3a8eb9SGleb Smirnoff 						yyerror("inline anchor '%s' "
9793b3a8eb9SGleb Smirnoff 						    "already exists",
9803b3a8eb9SGleb Smirnoff 						    r.anchor->name);
9813b3a8eb9SGleb Smirnoff 						YYERROR;
9823b3a8eb9SGleb Smirnoff 					}
9833b3a8eb9SGleb Smirnoff 					mv_rules(&pf->alast->ruleset,
9843b3a8eb9SGleb Smirnoff 					    &r.anchor->ruleset);
9853b3a8eb9SGleb Smirnoff 				}
9863b3a8eb9SGleb Smirnoff 				pf_remove_if_empty_ruleset(&pf->alast->ruleset);
9873b3a8eb9SGleb Smirnoff 				pf->alast = r.anchor;
9883b3a8eb9SGleb Smirnoff 			} else {
9893b3a8eb9SGleb Smirnoff 				if (!$2) {
9903b3a8eb9SGleb Smirnoff 					yyerror("anchors without explicit "
9913b3a8eb9SGleb Smirnoff 					    "rules must specify a name");
9923b3a8eb9SGleb Smirnoff 					YYERROR;
9933b3a8eb9SGleb Smirnoff 				}
9943b3a8eb9SGleb Smirnoff 			}
9953b3a8eb9SGleb Smirnoff 			r.direction = $3;
9963b3a8eb9SGleb Smirnoff 			r.quick = $4.quick;
9973b3a8eb9SGleb Smirnoff 			r.af = $6;
9983b3a8eb9SGleb Smirnoff 			r.prob = $9.prob;
9993b3a8eb9SGleb Smirnoff 			r.rtableid = $9.rtableid;
100076c5eeccSKristof Provost 			r.ridentifier = $9.ridentifier;
10013b3a8eb9SGleb Smirnoff 
10023b3a8eb9SGleb Smirnoff 			if ($9.tag)
10033b3a8eb9SGleb Smirnoff 				if (strlcpy(r.tagname, $9.tag,
10043b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
10053b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
10063b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
10073b3a8eb9SGleb Smirnoff 					YYERROR;
10083b3a8eb9SGleb Smirnoff 				}
10093b3a8eb9SGleb Smirnoff 			if ($9.match_tag)
10103b3a8eb9SGleb Smirnoff 				if (strlcpy(r.match_tagname, $9.match_tag,
10113b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
10123b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
10133b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
10143b3a8eb9SGleb Smirnoff 					YYERROR;
10153b3a8eb9SGleb Smirnoff 				}
10163b3a8eb9SGleb Smirnoff 			r.match_tag_not = $9.match_tag_not;
10173b3a8eb9SGleb Smirnoff 			if (rule_label(&r, $9.label))
10183b3a8eb9SGleb Smirnoff 				YYERROR;
10196fcc8e04SKristof Provost 			for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++)
10206fcc8e04SKristof Provost 				free($9.label[i]);
10213b3a8eb9SGleb Smirnoff 			r.flags = $9.flags.b1;
10223b3a8eb9SGleb Smirnoff 			r.flagset = $9.flags.b2;
10233b3a8eb9SGleb Smirnoff 			if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
10243b3a8eb9SGleb Smirnoff 				yyerror("flags always false");
10253b3a8eb9SGleb Smirnoff 				YYERROR;
10263b3a8eb9SGleb Smirnoff 			}
10273b3a8eb9SGleb Smirnoff 			if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
10283b3a8eb9SGleb Smirnoff 				for (proto = $7; proto != NULL &&
10293b3a8eb9SGleb Smirnoff 				    proto->proto != IPPROTO_TCP;
10303b3a8eb9SGleb Smirnoff 				    proto = proto->next)
10313b3a8eb9SGleb Smirnoff 					;	/* nothing */
10323b3a8eb9SGleb Smirnoff 				if (proto == NULL && $7 != NULL) {
10333b3a8eb9SGleb Smirnoff 					if ($9.flags.b1 || $9.flags.b2)
10343b3a8eb9SGleb Smirnoff 						yyerror(
10353b3a8eb9SGleb Smirnoff 						    "flags only apply to tcp");
10363b3a8eb9SGleb Smirnoff 					if ($8.src_os)
10373b3a8eb9SGleb Smirnoff 						yyerror(
10383b3a8eb9SGleb Smirnoff 						    "OS fingerprinting only "
10393b3a8eb9SGleb Smirnoff 						    "applies to tcp");
10403b3a8eb9SGleb Smirnoff 					YYERROR;
10413b3a8eb9SGleb Smirnoff 				}
10423b3a8eb9SGleb Smirnoff 			}
10433b3a8eb9SGleb Smirnoff 
10443b3a8eb9SGleb Smirnoff 			r.tos = $9.tos;
10453b3a8eb9SGleb Smirnoff 
10463b3a8eb9SGleb Smirnoff 			if ($9.keep.action) {
10473b3a8eb9SGleb Smirnoff 				yyerror("cannot specify state handling "
10483b3a8eb9SGleb Smirnoff 				    "on anchors");
10493b3a8eb9SGleb Smirnoff 				YYERROR;
10503b3a8eb9SGleb Smirnoff 			}
10513b3a8eb9SGleb Smirnoff 
10523b3a8eb9SGleb Smirnoff 			if ($9.match_tag)
10533b3a8eb9SGleb Smirnoff 				if (strlcpy(r.match_tagname, $9.match_tag,
10543b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
10553b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
10563b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
10573b3a8eb9SGleb Smirnoff 					YYERROR;
10583b3a8eb9SGleb Smirnoff 				}
10593b3a8eb9SGleb Smirnoff 			r.match_tag_not = $9.match_tag_not;
10603e248e0fSKristof Provost 			if ($9.marker & FOM_PRIO) {
10613e248e0fSKristof Provost 				if ($9.prio == 0)
10623e248e0fSKristof Provost 					r.prio = PF_PRIO_ZERO;
10633e248e0fSKristof Provost 				else
10643e248e0fSKristof Provost 					r.prio = $9.prio;
10653e248e0fSKristof Provost 			}
10663e248e0fSKristof Provost 			if ($9.marker & FOM_SETPRIO) {
10673e248e0fSKristof Provost 				r.set_prio[0] = $9.set_prio[0];
10683e248e0fSKristof Provost 				r.set_prio[1] = $9.set_prio[1];
10693e248e0fSKristof Provost 				r.scrub_flags |= PFSTATE_SETPRIO;
10703e248e0fSKristof Provost 			}
10713b3a8eb9SGleb Smirnoff 
10723b3a8eb9SGleb Smirnoff 			decide_address_family($8.src.host, &r.af);
10733b3a8eb9SGleb Smirnoff 			decide_address_family($8.dst.host, &r.af);
10743b3a8eb9SGleb Smirnoff 
10753b3a8eb9SGleb Smirnoff 			expand_rule(&r, $5, NULL, $7, $8.src_os,
10763b3a8eb9SGleb Smirnoff 			    $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
10772339ead6SKristof Provost 			    $9.uid, $9.gid, $9.rcv, $9.icmpspec,
10783b3a8eb9SGleb Smirnoff 			    pf->astack[pf->asd + 1] ? pf->alast->name : $2);
10793b3a8eb9SGleb Smirnoff 			free($2);
10803b3a8eb9SGleb Smirnoff 			pf->astack[pf->asd + 1] = NULL;
10813b3a8eb9SGleb Smirnoff 		}
10823b3a8eb9SGleb Smirnoff 		| NATANCHOR string interface af proto fromto rtable {
1083e9eb0941SKristof Provost 			struct pfctl_rule	r;
10843b3a8eb9SGleb Smirnoff 
10853b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_NAT)) {
10863b3a8eb9SGleb Smirnoff 				free($2);
10873b3a8eb9SGleb Smirnoff 				YYERROR;
10883b3a8eb9SGleb Smirnoff 			}
10893b3a8eb9SGleb Smirnoff 
10903b3a8eb9SGleb Smirnoff 			memset(&r, 0, sizeof(r));
10913b3a8eb9SGleb Smirnoff 			r.action = PF_NAT;
10923b3a8eb9SGleb Smirnoff 			r.af = $4;
10933b3a8eb9SGleb Smirnoff 			r.rtableid = $7;
10943b3a8eb9SGleb Smirnoff 
10953b3a8eb9SGleb Smirnoff 			decide_address_family($6.src.host, &r.af);
10963b3a8eb9SGleb Smirnoff 			decide_address_family($6.dst.host, &r.af);
10973b3a8eb9SGleb Smirnoff 
10983b3a8eb9SGleb Smirnoff 			expand_rule(&r, $3, NULL, $5, $6.src_os,
10993b3a8eb9SGleb Smirnoff 			    $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
11002339ead6SKristof Provost 			    0, 0, 0, 0, $2);
11013b3a8eb9SGleb Smirnoff 			free($2);
11023b3a8eb9SGleb Smirnoff 		}
11033b3a8eb9SGleb Smirnoff 		| RDRANCHOR string interface af proto fromto rtable {
1104e9eb0941SKristof Provost 			struct pfctl_rule	r;
11053b3a8eb9SGleb Smirnoff 
11063b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_NAT)) {
11073b3a8eb9SGleb Smirnoff 				free($2);
11083b3a8eb9SGleb Smirnoff 				YYERROR;
11093b3a8eb9SGleb Smirnoff 			}
11103b3a8eb9SGleb Smirnoff 
11113b3a8eb9SGleb Smirnoff 			memset(&r, 0, sizeof(r));
11123b3a8eb9SGleb Smirnoff 			r.action = PF_RDR;
11133b3a8eb9SGleb Smirnoff 			r.af = $4;
11143b3a8eb9SGleb Smirnoff 			r.rtableid = $7;
11153b3a8eb9SGleb Smirnoff 
11163b3a8eb9SGleb Smirnoff 			decide_address_family($6.src.host, &r.af);
11173b3a8eb9SGleb Smirnoff 			decide_address_family($6.dst.host, &r.af);
11183b3a8eb9SGleb Smirnoff 
11193b3a8eb9SGleb Smirnoff 			if ($6.src.port != NULL) {
11203b3a8eb9SGleb Smirnoff 				yyerror("source port parameter not supported"
11213b3a8eb9SGleb Smirnoff 				    " in rdr-anchor");
11223b3a8eb9SGleb Smirnoff 				YYERROR;
11233b3a8eb9SGleb Smirnoff 			}
11243b3a8eb9SGleb Smirnoff 			if ($6.dst.port != NULL) {
11253b3a8eb9SGleb Smirnoff 				if ($6.dst.port->next != NULL) {
11263b3a8eb9SGleb Smirnoff 					yyerror("destination port list "
11273b3a8eb9SGleb Smirnoff 					    "expansion not supported in "
11283b3a8eb9SGleb Smirnoff 					    "rdr-anchor");
11293b3a8eb9SGleb Smirnoff 					YYERROR;
11303b3a8eb9SGleb Smirnoff 				} else if ($6.dst.port->op != PF_OP_EQ) {
11313b3a8eb9SGleb Smirnoff 					yyerror("destination port operators"
11323b3a8eb9SGleb Smirnoff 					    " not supported in rdr-anchor");
11333b3a8eb9SGleb Smirnoff 					YYERROR;
11343b3a8eb9SGleb Smirnoff 				}
11353b3a8eb9SGleb Smirnoff 				r.dst.port[0] = $6.dst.port->port[0];
11363b3a8eb9SGleb Smirnoff 				r.dst.port[1] = $6.dst.port->port[1];
11373b3a8eb9SGleb Smirnoff 				r.dst.port_op = $6.dst.port->op;
11383b3a8eb9SGleb Smirnoff 			}
11393b3a8eb9SGleb Smirnoff 
11403b3a8eb9SGleb Smirnoff 			expand_rule(&r, $3, NULL, $5, $6.src_os,
11413b3a8eb9SGleb Smirnoff 			    $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
11422339ead6SKristof Provost 			    0, 0, 0, 0, $2);
11433b3a8eb9SGleb Smirnoff 			free($2);
11443b3a8eb9SGleb Smirnoff 		}
11453b3a8eb9SGleb Smirnoff 		| BINATANCHOR string interface af proto fromto rtable {
1146e9eb0941SKristof Provost 			struct pfctl_rule	r;
11473b3a8eb9SGleb Smirnoff 
11483b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_NAT)) {
11493b3a8eb9SGleb Smirnoff 				free($2);
11503b3a8eb9SGleb Smirnoff 				YYERROR;
11513b3a8eb9SGleb Smirnoff 			}
11523b3a8eb9SGleb Smirnoff 
11533b3a8eb9SGleb Smirnoff 			memset(&r, 0, sizeof(r));
11543b3a8eb9SGleb Smirnoff 			r.action = PF_BINAT;
11553b3a8eb9SGleb Smirnoff 			r.af = $4;
11563b3a8eb9SGleb Smirnoff 			r.rtableid = $7;
11573b3a8eb9SGleb Smirnoff 			if ($5 != NULL) {
11583b3a8eb9SGleb Smirnoff 				if ($5->next != NULL) {
11593b3a8eb9SGleb Smirnoff 					yyerror("proto list expansion"
11603b3a8eb9SGleb Smirnoff 					    " not supported in binat-anchor");
11613b3a8eb9SGleb Smirnoff 					YYERROR;
11623b3a8eb9SGleb Smirnoff 				}
11633b3a8eb9SGleb Smirnoff 				r.proto = $5->proto;
11643b3a8eb9SGleb Smirnoff 				free($5);
11653b3a8eb9SGleb Smirnoff 			}
11663b3a8eb9SGleb Smirnoff 
11673b3a8eb9SGleb Smirnoff 			if ($6.src.host != NULL || $6.src.port != NULL ||
11683b3a8eb9SGleb Smirnoff 			    $6.dst.host != NULL || $6.dst.port != NULL) {
11693b3a8eb9SGleb Smirnoff 				yyerror("fromto parameter not supported"
11703b3a8eb9SGleb Smirnoff 				    " in binat-anchor");
11713b3a8eb9SGleb Smirnoff 				YYERROR;
11723b3a8eb9SGleb Smirnoff 			}
11733b3a8eb9SGleb Smirnoff 
11743b3a8eb9SGleb Smirnoff 			decide_address_family($6.src.host, &r.af);
11753b3a8eb9SGleb Smirnoff 			decide_address_family($6.dst.host, &r.af);
11763b3a8eb9SGleb Smirnoff 
11770d71f9f3SKristof Provost 			pfctl_append_rule(pf, &r, $2);
11783b3a8eb9SGleb Smirnoff 			free($2);
11793b3a8eb9SGleb Smirnoff 		}
11803b3a8eb9SGleb Smirnoff 		;
11813b3a8eb9SGleb Smirnoff 
11823b3a8eb9SGleb Smirnoff loadrule	: LOAD ANCHOR string FROM string	{
11833b3a8eb9SGleb Smirnoff 			struct loadanchors	*loadanchor;
11843b3a8eb9SGleb Smirnoff 
11853b3a8eb9SGleb Smirnoff 			if (strlen(pf->anchor->name) + 1 +
11863b3a8eb9SGleb Smirnoff 			    strlen($3) >= MAXPATHLEN) {
11873b3a8eb9SGleb Smirnoff 				yyerror("anchorname %s too long, max %u\n",
11883b3a8eb9SGleb Smirnoff 				    $3, MAXPATHLEN - 1);
11893b3a8eb9SGleb Smirnoff 				free($3);
11903b3a8eb9SGleb Smirnoff 				YYERROR;
11913b3a8eb9SGleb Smirnoff 			}
11923b3a8eb9SGleb Smirnoff 			loadanchor = calloc(1, sizeof(struct loadanchors));
11933b3a8eb9SGleb Smirnoff 			if (loadanchor == NULL)
11943b3a8eb9SGleb Smirnoff 				err(1, "loadrule: calloc");
11953b3a8eb9SGleb Smirnoff 			if ((loadanchor->anchorname = malloc(MAXPATHLEN)) ==
11963b3a8eb9SGleb Smirnoff 			    NULL)
11973b3a8eb9SGleb Smirnoff 				err(1, "loadrule: malloc");
11983b3a8eb9SGleb Smirnoff 			if (pf->anchor->name[0])
11993b3a8eb9SGleb Smirnoff 				snprintf(loadanchor->anchorname, MAXPATHLEN,
12003b3a8eb9SGleb Smirnoff 				    "%s/%s", pf->anchor->name, $3);
12013b3a8eb9SGleb Smirnoff 			else
12023b3a8eb9SGleb Smirnoff 				strlcpy(loadanchor->anchorname, $3, MAXPATHLEN);
12033b3a8eb9SGleb Smirnoff 			if ((loadanchor->filename = strdup($5)) == NULL)
12043b3a8eb9SGleb Smirnoff 				err(1, "loadrule: strdup");
12053b3a8eb9SGleb Smirnoff 
12063b3a8eb9SGleb Smirnoff 			TAILQ_INSERT_TAIL(&loadanchorshead, loadanchor,
12073b3a8eb9SGleb Smirnoff 			    entries);
12083b3a8eb9SGleb Smirnoff 
12093b3a8eb9SGleb Smirnoff 			free($3);
12103b3a8eb9SGleb Smirnoff 			free($5);
12113b3a8eb9SGleb Smirnoff 		};
12123b3a8eb9SGleb Smirnoff 
12133b3a8eb9SGleb Smirnoff scrubaction	: no SCRUB {
12143b3a8eb9SGleb Smirnoff 			$$.b2 = $$.w = 0;
12153b3a8eb9SGleb Smirnoff 			if ($1)
12163b3a8eb9SGleb Smirnoff 				$$.b1 = PF_NOSCRUB;
12173b3a8eb9SGleb Smirnoff 			else
12183b3a8eb9SGleb Smirnoff 				$$.b1 = PF_SCRUB;
12193b3a8eb9SGleb Smirnoff 		}
12203b3a8eb9SGleb Smirnoff 		;
12213b3a8eb9SGleb Smirnoff 
12228a8af942SKristof Provost etherrule	: ETHER action dir quick interface bridge etherproto etherfromto l3fromto etherfilter_opts
12232b29ceb8SKristof Provost 		{
12242b29ceb8SKristof Provost 			struct pfctl_eth_rule	r;
12252b29ceb8SKristof Provost 
12262b29ceb8SKristof Provost 			bzero(&r, sizeof(r));
12272b29ceb8SKristof Provost 
12282b29ceb8SKristof Provost 			if (check_rulestate(PFCTL_STATE_ETHER))
12292b29ceb8SKristof Provost 				YYERROR;
12302b29ceb8SKristof Provost 
12312b29ceb8SKristof Provost 			r.action = $2.b1;
12322b29ceb8SKristof Provost 			r.direction = $3;
12332b29ceb8SKristof Provost 			r.quick = $4.quick;
12348a8af942SKristof Provost 			if ($10.tag != NULL)
1235dc3ee89cSKristof Provost 				strlcpy(r.tagname, $10.tag, sizeof(r.tagname));
12368a8af942SKristof Provost 			if ($10.match_tag)
12378a8af942SKristof Provost 				if (strlcpy(r.match_tagname, $10.match_tag,
12381f61367fSKristof Provost 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
12391f61367fSKristof Provost 					yyerror("tag too long, max %u chars",
12401f61367fSKristof Provost 					    PF_TAG_NAME_SIZE - 1);
12411f61367fSKristof Provost 					YYERROR;
12421f61367fSKristof Provost 				}
12438a8af942SKristof Provost 			r.match_tag_not = $10.match_tag_not;
12448a8af942SKristof Provost 			if ($10.queues.qname != NULL)
1245dc3ee89cSKristof Provost 				strlcpy(r.qname, $10.queues.qname, sizeof(r.qname));
12468a8af942SKristof Provost 			r.dnpipe = $10.dnpipe;
12478a8af942SKristof Provost 			r.dnflags = $10.free_flags;
1248ef661d4aSChristian McDonald 			if (eth_rule_label(&r, $10.label))
1249ef661d4aSChristian McDonald 				YYERROR;
1250ef661d4aSChristian McDonald 			for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++)
1251ef661d4aSChristian McDonald 				free($10.label[i]);
1252ef661d4aSChristian McDonald 			r.ridentifier = $10.ridentifier;
12532b29ceb8SKristof Provost 
12548a8af942SKristof Provost 			expand_eth_rule(&r, $5, $7, $8.src, $8.dst,
12558a8af942SKristof Provost 			    $9.src.host, $9.dst.host, $6, "");
1256c5131afeSKristof Provost 		}
1257c5131afeSKristof Provost 		;
1258c5131afeSKristof Provost 
1259c5131afeSKristof Provost etherpfa_anchorlist	: /* empty */
1260c5131afeSKristof Provost 		| etherpfa_anchorlist '\n'
1261c5131afeSKristof Provost 		| etherpfa_anchorlist etherrule '\n'
1262c5131afeSKristof Provost 		| etherpfa_anchorlist etheranchorrule '\n'
1263c5131afeSKristof Provost 		;
1264c5131afeSKristof Provost 
1265c5131afeSKristof Provost etherpfa_anchor	: '{'
1266c5131afeSKristof Provost 		{
1267c5131afeSKristof Provost 			char ta[PF_ANCHOR_NAME_SIZE];
1268c5131afeSKristof Provost 			struct pfctl_eth_ruleset *rs;
1269c5131afeSKristof Provost 
1270c5131afeSKristof Provost 			/* steping into a brace anchor */
1271c5131afeSKristof Provost 			pf->asd++;
1272c5131afeSKristof Provost 			pf->bn++;
1273c5131afeSKristof Provost 
1274c5131afeSKristof Provost 			/* create a holding ruleset in the root */
1275c5131afeSKristof Provost 			snprintf(ta, PF_ANCHOR_NAME_SIZE, "_%d", pf->bn);
1276c5131afeSKristof Provost 			rs = pf_find_or_create_eth_ruleset(ta);
1277c5131afeSKristof Provost 			if (rs == NULL)
1278c5131afeSKristof Provost 				err(1, "etherpfa_anchor: pf_find_or_create_eth_ruleset");
1279c5131afeSKristof Provost 			pf->eastack[pf->asd] = rs->anchor;
1280c5131afeSKristof Provost 			pf->eanchor = rs->anchor;
1281c5131afeSKristof Provost 		} '\n' etherpfa_anchorlist '}'
1282c5131afeSKristof Provost 		{
1283c5131afeSKristof Provost 			pf->ealast = pf->eanchor;
1284c5131afeSKristof Provost 			pf->asd--;
1285c5131afeSKristof Provost 			pf->eanchor = pf->eastack[pf->asd];
1286c5131afeSKristof Provost 		}
1287c5131afeSKristof Provost 		| /* empty */
1288c5131afeSKristof Provost 		;
1289c5131afeSKristof Provost 
12908a42005dSKristof Provost etheranchorrule	: ETHER ANCHOR anchorname dir quick interface etherproto etherfromto l3fromto etherpfa_anchor
1291c5131afeSKristof Provost 		{
1292c5131afeSKristof Provost 			struct pfctl_eth_rule	r;
1293c5131afeSKristof Provost 
1294c5131afeSKristof Provost 			if (check_rulestate(PFCTL_STATE_ETHER)) {
1295c5131afeSKristof Provost 				free($3);
1296c5131afeSKristof Provost 				YYERROR;
1297c5131afeSKristof Provost 			}
1298c5131afeSKristof Provost 
1299c5131afeSKristof Provost 			if ($3 && ($3[0] == '_' || strstr($3, "/_") != NULL)) {
1300c5131afeSKristof Provost 				free($3);
1301c5131afeSKristof Provost 				yyerror("anchor names beginning with '_' "
1302c5131afeSKristof Provost 				    "are reserved for internal use");
1303c5131afeSKristof Provost 				YYERROR;
1304c5131afeSKristof Provost 			}
1305c5131afeSKristof Provost 
1306c5131afeSKristof Provost 			memset(&r, 0, sizeof(r));
1307c5131afeSKristof Provost 			if (pf->eastack[pf->asd + 1]) {
1308cfa1a130SKristof Provost 				if ($3 && strchr($3, '/') != NULL) {
1309cfa1a130SKristof Provost 					free($3);
1310cfa1a130SKristof Provost 					yyerror("anchor paths containing '/' "
1311cfa1a130SKristof Provost 					   "cannot be used for inline anchors.");
1312cfa1a130SKristof Provost 					YYERROR;
1313cfa1a130SKristof Provost 				}
1314cfa1a130SKristof Provost 
1315cfa1a130SKristof Provost 				/* Move inline rules into relative location. */
1316c5131afeSKristof Provost 				pfctl_eth_anchor_setup(pf, &r,
1317c5131afeSKristof Provost 				    &pf->eastack[pf->asd]->ruleset,
1318c5131afeSKristof Provost 				    $3 ? $3 : pf->ealast->name);
1319c5131afeSKristof Provost 				if (r.anchor == NULL)
1320c5131afeSKristof Provost 					err(1, "etheranchorrule: unable to "
1321c5131afeSKristof Provost 					    "create ruleset");
1322c5131afeSKristof Provost 
1323c5131afeSKristof Provost 				if (pf->ealast != r.anchor) {
1324c5131afeSKristof Provost 					if (r.anchor->match) {
1325c5131afeSKristof Provost 						yyerror("inline anchor '%s' "
1326c5131afeSKristof Provost 						    "already exists",
1327c5131afeSKristof Provost 						    r.anchor->name);
1328c5131afeSKristof Provost 						YYERROR;
1329c5131afeSKristof Provost 					}
1330c5131afeSKristof Provost 					mv_eth_rules(&pf->ealast->ruleset,
1331c5131afeSKristof Provost 					    &r.anchor->ruleset);
1332c5131afeSKristof Provost 				}
1333c5131afeSKristof Provost 				pf_remove_if_empty_eth_ruleset(&pf->ealast->ruleset);
1334c5131afeSKristof Provost 				pf->ealast = r.anchor;
1335c5131afeSKristof Provost 			} else {
1336c5131afeSKristof Provost 				if (!$3) {
1337c5131afeSKristof Provost 					yyerror("anchors without explicit "
1338c5131afeSKristof Provost 					    "rules must specify a name");
1339c5131afeSKristof Provost 					YYERROR;
1340c5131afeSKristof Provost 				}
1341c5131afeSKristof Provost 			}
1342c5131afeSKristof Provost 
1343c5131afeSKristof Provost 			r.direction = $4;
1344c5131afeSKristof Provost 			r.quick = $5.quick;
1345c5131afeSKristof Provost 
1346c5131afeSKristof Provost 			expand_eth_rule(&r, $6, $7, $8.src, $8.dst,
13478a8af942SKristof Provost 			    $9.src.host, $9.dst.host, NULL,
1348c5131afeSKristof Provost 			    pf->eastack[pf->asd + 1] ? pf->ealast->name : $3);
1349c5131afeSKristof Provost 
1350c5131afeSKristof Provost 			free($3);
1351c5131afeSKristof Provost 			pf->eastack[pf->asd + 1] = NULL;
13522b29ceb8SKristof Provost 		}
13532b29ceb8SKristof Provost 		;
13542b29ceb8SKristof Provost 
13552b29ceb8SKristof Provost etherfilter_opts	:	{
13562b29ceb8SKristof Provost 				bzero(&filter_opts, sizeof filter_opts);
13572b29ceb8SKristof Provost 			}
13582b29ceb8SKristof Provost 		    etherfilter_opts_l
13592b29ceb8SKristof Provost 			{ $$ = filter_opts; }
13602b29ceb8SKristof Provost 		| /* empty */	{
13612b29ceb8SKristof Provost 			bzero(&filter_opts, sizeof filter_opts);
13622b29ceb8SKristof Provost 			$$ = filter_opts;
13632b29ceb8SKristof Provost 		}
13642b29ceb8SKristof Provost 		;
13652b29ceb8SKristof Provost 
13662b29ceb8SKristof Provost etherfilter_opts_l	: etherfilter_opts_l etherfilter_opt
13672b29ceb8SKristof Provost 			| etherfilter_opt
13682b29ceb8SKristof Provost 
13692b29ceb8SKristof Provost etherfilter_opt	: etherqname	{
13702b29ceb8SKristof Provost 			if (filter_opts.queues.qname) {
13712b29ceb8SKristof Provost 				yyerror("queue cannot be redefined");
13722b29ceb8SKristof Provost 				YYERROR;
13732b29ceb8SKristof Provost 			}
13742b29ceb8SKristof Provost 			filter_opts.queues = $1;
13752b29ceb8SKristof Provost 		}
1376ef661d4aSChristian McDonald 		| RIDENTIFIER number {
1377ef661d4aSChristian McDonald 			filter_opts.ridentifier = $2;
1378ef661d4aSChristian McDonald 		}
1379ef661d4aSChristian McDonald 		| label	{
1380ef661d4aSChristian McDonald 			if (filter_opts.labelcount >= PF_RULE_MAX_LABEL_COUNT) {
1381ef661d4aSChristian McDonald 				yyerror("label can only be used %d times", PF_RULE_MAX_LABEL_COUNT);
1382ef661d4aSChristian McDonald 				YYERROR;
1383ef661d4aSChristian McDonald 			}
1384ef661d4aSChristian McDonald 			filter_opts.label[filter_opts.labelcount++] = $1;
1385ef661d4aSChristian McDonald 		}
13862b29ceb8SKristof Provost 		| TAG string				{
13872b29ceb8SKristof Provost 			filter_opts.tag = $2;
13882b29ceb8SKristof Provost 		}
13891f61367fSKristof Provost 		| not TAGGED string			{
13901f61367fSKristof Provost 			filter_opts.match_tag = $3;
13911f61367fSKristof Provost 			filter_opts.match_tag_not = $1;
13921f61367fSKristof Provost 		}
1393fb330f39SKristof Provost 		| DNPIPE number {
1394fb330f39SKristof Provost 			filter_opts.dnpipe = $2;
1395fb330f39SKristof Provost 			filter_opts.free_flags |= PFRULE_DN_IS_PIPE;
1396fb330f39SKristof Provost 		}
1397fb330f39SKristof Provost 		| DNQUEUE number {
1398fb330f39SKristof Provost 			filter_opts.dnpipe = $2;
1399fb330f39SKristof Provost 			filter_opts.free_flags |= PFRULE_DN_IS_QUEUE;
1400fb330f39SKristof Provost 		}
14012b29ceb8SKristof Provost 		;
14022b29ceb8SKristof Provost 
14038a8af942SKristof Provost bridge		:	/* empty */		{
14048a8af942SKristof Provost 			$$ = NULL;
14058a8af942SKristof Provost 		}
14068a8af942SKristof Provost 		| BRIDGE_TO STRING {
14078a8af942SKristof Provost 			$$ = strdup($2);
14088a8af942SKristof Provost 		}
14098a8af942SKristof Provost 		;
14108a8af942SKristof Provost 
14113b3a8eb9SGleb Smirnoff scrubrule	: scrubaction dir logquick interface af proto fromto scrub_opts
14123b3a8eb9SGleb Smirnoff 		{
1413e9eb0941SKristof Provost 			struct pfctl_rule	r;
14143b3a8eb9SGleb Smirnoff 
14153b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_SCRUB))
14163b3a8eb9SGleb Smirnoff 				YYERROR;
14173b3a8eb9SGleb Smirnoff 
14183b3a8eb9SGleb Smirnoff 			memset(&r, 0, sizeof(r));
14193b3a8eb9SGleb Smirnoff 
14203b3a8eb9SGleb Smirnoff 			r.action = $1.b1;
14213b3a8eb9SGleb Smirnoff 			r.direction = $2;
14223b3a8eb9SGleb Smirnoff 
14233b3a8eb9SGleb Smirnoff 			r.log = $3.log;
14243b3a8eb9SGleb Smirnoff 			r.logif = $3.logif;
14253b3a8eb9SGleb Smirnoff 			if ($3.quick) {
14263b3a8eb9SGleb Smirnoff 				yyerror("scrub rules do not support 'quick'");
14273b3a8eb9SGleb Smirnoff 				YYERROR;
14283b3a8eb9SGleb Smirnoff 			}
14293b3a8eb9SGleb Smirnoff 
14303b3a8eb9SGleb Smirnoff 			r.af = $5;
14313b3a8eb9SGleb Smirnoff 			if ($8.nodf)
14323b3a8eb9SGleb Smirnoff 				r.rule_flag |= PFRULE_NODF;
14333b3a8eb9SGleb Smirnoff 			if ($8.randomid)
14343b3a8eb9SGleb Smirnoff 				r.rule_flag |= PFRULE_RANDOMID;
14353b3a8eb9SGleb Smirnoff 			if ($8.reassemble_tcp) {
14363b3a8eb9SGleb Smirnoff 				if (r.direction != PF_INOUT) {
14373b3a8eb9SGleb Smirnoff 					yyerror("reassemble tcp rules can not "
14383b3a8eb9SGleb Smirnoff 					    "specify direction");
14393b3a8eb9SGleb Smirnoff 					YYERROR;
14403b3a8eb9SGleb Smirnoff 				}
14413b3a8eb9SGleb Smirnoff 				r.rule_flag |= PFRULE_REASSEMBLE_TCP;
14423b3a8eb9SGleb Smirnoff 			}
14433b3a8eb9SGleb Smirnoff 			if ($8.minttl)
14443b3a8eb9SGleb Smirnoff 				r.min_ttl = $8.minttl;
14453b3a8eb9SGleb Smirnoff 			if ($8.maxmss)
14463b3a8eb9SGleb Smirnoff 				r.max_mss = $8.maxmss;
144739282ef3SKajetan Staszkiewicz 			if ($8.marker & FOM_SETTOS) {
14483b3a8eb9SGleb Smirnoff 				r.rule_flag |= PFRULE_SET_TOS;
14493b3a8eb9SGleb Smirnoff 				r.set_tos = $8.settos;
14503b3a8eb9SGleb Smirnoff 			}
14513b3a8eb9SGleb Smirnoff 			if ($8.fragcache)
14523b3a8eb9SGleb Smirnoff 				r.rule_flag |= $8.fragcache;
14533b3a8eb9SGleb Smirnoff 			if ($8.match_tag)
14543b3a8eb9SGleb Smirnoff 				if (strlcpy(r.match_tagname, $8.match_tag,
14553b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
14563b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
14573b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
14583b3a8eb9SGleb Smirnoff 					YYERROR;
14593b3a8eb9SGleb Smirnoff 				}
14603b3a8eb9SGleb Smirnoff 			r.match_tag_not = $8.match_tag_not;
14613b3a8eb9SGleb Smirnoff 			r.rtableid = $8.rtableid;
14623b3a8eb9SGleb Smirnoff 
14633b3a8eb9SGleb Smirnoff 			expand_rule(&r, $4, NULL, $6, $7.src_os,
14643b3a8eb9SGleb Smirnoff 			    $7.src.host, $7.src.port, $7.dst.host, $7.dst.port,
14652339ead6SKristof Provost 			    NULL, NULL, NULL, NULL, "");
14663b3a8eb9SGleb Smirnoff 		}
14673b3a8eb9SGleb Smirnoff 		;
14683b3a8eb9SGleb Smirnoff 
14693b3a8eb9SGleb Smirnoff scrub_opts	:	{
14703b3a8eb9SGleb Smirnoff 				bzero(&scrub_opts, sizeof scrub_opts);
14713b3a8eb9SGleb Smirnoff 				scrub_opts.rtableid = -1;
14723b3a8eb9SGleb Smirnoff 			}
14733b3a8eb9SGleb Smirnoff 		    scrub_opts_l
14743b3a8eb9SGleb Smirnoff 			{ $$ = scrub_opts; }
14753b3a8eb9SGleb Smirnoff 		| /* empty */ {
14763b3a8eb9SGleb Smirnoff 			bzero(&scrub_opts, sizeof scrub_opts);
14773b3a8eb9SGleb Smirnoff 			scrub_opts.rtableid = -1;
14783b3a8eb9SGleb Smirnoff 			$$ = scrub_opts;
14793b3a8eb9SGleb Smirnoff 		}
14803b3a8eb9SGleb Smirnoff 		;
14813b3a8eb9SGleb Smirnoff 
148239282ef3SKajetan Staszkiewicz scrub_opts_l	: scrub_opts_l comma scrub_opt
14833b3a8eb9SGleb Smirnoff 		| scrub_opt
14843b3a8eb9SGleb Smirnoff 		;
14853b3a8eb9SGleb Smirnoff 
14863b3a8eb9SGleb Smirnoff scrub_opt	: NODF	{
14873b3a8eb9SGleb Smirnoff 			if (scrub_opts.nodf) {
14883b3a8eb9SGleb Smirnoff 				yyerror("no-df cannot be respecified");
14893b3a8eb9SGleb Smirnoff 				YYERROR;
14903b3a8eb9SGleb Smirnoff 			}
14913b3a8eb9SGleb Smirnoff 			scrub_opts.nodf = 1;
14923b3a8eb9SGleb Smirnoff 		}
14933b3a8eb9SGleb Smirnoff 		| MINTTL NUMBER {
149439282ef3SKajetan Staszkiewicz 			if (scrub_opts.marker & FOM_MINTTL) {
14953b3a8eb9SGleb Smirnoff 				yyerror("min-ttl cannot be respecified");
14963b3a8eb9SGleb Smirnoff 				YYERROR;
14973b3a8eb9SGleb Smirnoff 			}
14983b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > 255) {
14993b3a8eb9SGleb Smirnoff 				yyerror("illegal min-ttl value %d", $2);
15003b3a8eb9SGleb Smirnoff 				YYERROR;
15013b3a8eb9SGleb Smirnoff 			}
150239282ef3SKajetan Staszkiewicz 			scrub_opts.marker |= FOM_MINTTL;
15033b3a8eb9SGleb Smirnoff 			scrub_opts.minttl = $2;
15043b3a8eb9SGleb Smirnoff 		}
15053b3a8eb9SGleb Smirnoff 		| MAXMSS NUMBER {
150639282ef3SKajetan Staszkiewicz 			if (scrub_opts.marker & FOM_MAXMSS) {
15073b3a8eb9SGleb Smirnoff 				yyerror("max-mss cannot be respecified");
15083b3a8eb9SGleb Smirnoff 				YYERROR;
15093b3a8eb9SGleb Smirnoff 			}
15103b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > 65535) {
15113b3a8eb9SGleb Smirnoff 				yyerror("illegal max-mss value %d", $2);
15123b3a8eb9SGleb Smirnoff 				YYERROR;
15133b3a8eb9SGleb Smirnoff 			}
151439282ef3SKajetan Staszkiewicz 			scrub_opts.marker |= FOM_MAXMSS;
15153b3a8eb9SGleb Smirnoff 			scrub_opts.maxmss = $2;
15163b3a8eb9SGleb Smirnoff 		}
15173b3a8eb9SGleb Smirnoff 		| SETTOS tos {
151839282ef3SKajetan Staszkiewicz 			if (scrub_opts.marker & FOM_SETTOS) {
15193b3a8eb9SGleb Smirnoff 				yyerror("set-tos cannot be respecified");
15203b3a8eb9SGleb Smirnoff 				YYERROR;
15213b3a8eb9SGleb Smirnoff 			}
152239282ef3SKajetan Staszkiewicz 			scrub_opts.marker |= FOM_SETTOS;
15233b3a8eb9SGleb Smirnoff 			scrub_opts.settos = $2;
15243b3a8eb9SGleb Smirnoff 		}
15253b3a8eb9SGleb Smirnoff 		| fragcache {
152639282ef3SKajetan Staszkiewicz 			if (scrub_opts.marker & FOM_FRAGCACHE) {
15273b3a8eb9SGleb Smirnoff 				yyerror("fragcache cannot be respecified");
15283b3a8eb9SGleb Smirnoff 				YYERROR;
15293b3a8eb9SGleb Smirnoff 			}
153039282ef3SKajetan Staszkiewicz 			scrub_opts.marker |= FOM_FRAGCACHE;
15313b3a8eb9SGleb Smirnoff 			scrub_opts.fragcache = $1;
15323b3a8eb9SGleb Smirnoff 		}
15333b3a8eb9SGleb Smirnoff 		| REASSEMBLE STRING {
15343b3a8eb9SGleb Smirnoff 			if (strcasecmp($2, "tcp") != 0) {
15353b3a8eb9SGleb Smirnoff 				yyerror("scrub reassemble supports only tcp, "
15363b3a8eb9SGleb Smirnoff 				    "not '%s'", $2);
15373b3a8eb9SGleb Smirnoff 				free($2);
15383b3a8eb9SGleb Smirnoff 				YYERROR;
15393b3a8eb9SGleb Smirnoff 			}
15403b3a8eb9SGleb Smirnoff 			free($2);
15413b3a8eb9SGleb Smirnoff 			if (scrub_opts.reassemble_tcp) {
15423b3a8eb9SGleb Smirnoff 				yyerror("reassemble tcp cannot be respecified");
15433b3a8eb9SGleb Smirnoff 				YYERROR;
15443b3a8eb9SGleb Smirnoff 			}
15453b3a8eb9SGleb Smirnoff 			scrub_opts.reassemble_tcp = 1;
15463b3a8eb9SGleb Smirnoff 		}
15473b3a8eb9SGleb Smirnoff 		| RANDOMID {
15483b3a8eb9SGleb Smirnoff 			if (scrub_opts.randomid) {
15493b3a8eb9SGleb Smirnoff 				yyerror("random-id cannot be respecified");
15503b3a8eb9SGleb Smirnoff 				YYERROR;
15513b3a8eb9SGleb Smirnoff 			}
15523b3a8eb9SGleb Smirnoff 			scrub_opts.randomid = 1;
15533b3a8eb9SGleb Smirnoff 		}
15543b3a8eb9SGleb Smirnoff 		| RTABLE NUMBER				{
15553b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > rt_tableid_max()) {
15563b3a8eb9SGleb Smirnoff 				yyerror("invalid rtable id");
15573b3a8eb9SGleb Smirnoff 				YYERROR;
15583b3a8eb9SGleb Smirnoff 			}
15593b3a8eb9SGleb Smirnoff 			scrub_opts.rtableid = $2;
15603b3a8eb9SGleb Smirnoff 		}
15613b3a8eb9SGleb Smirnoff 		| not TAGGED string			{
15623b3a8eb9SGleb Smirnoff 			scrub_opts.match_tag = $3;
15633b3a8eb9SGleb Smirnoff 			scrub_opts.match_tag_not = $1;
15643b3a8eb9SGleb Smirnoff 		}
15653b3a8eb9SGleb Smirnoff 		;
15663b3a8eb9SGleb Smirnoff 
15673b3a8eb9SGleb Smirnoff fragcache	: FRAGMENT REASSEMBLE		{ $$ = 0; /* default */ }
156857e047e5SKristof Provost 		| FRAGMENT NO REASSEMBLE	{ $$ = PFRULE_FRAGMENT_NOREASS; }
15693b3a8eb9SGleb Smirnoff 		;
15703b3a8eb9SGleb Smirnoff 
15713b3a8eb9SGleb Smirnoff antispoof	: ANTISPOOF logquick antispoof_ifspc af antispoof_opts {
1572e9eb0941SKristof Provost 			struct pfctl_rule	 r;
15733b3a8eb9SGleb Smirnoff 			struct node_host	*h = NULL, *hh;
15743b3a8eb9SGleb Smirnoff 			struct node_if		*i, *j;
15753b3a8eb9SGleb Smirnoff 
15763b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_FILTER))
15773b3a8eb9SGleb Smirnoff 				YYERROR;
15783b3a8eb9SGleb Smirnoff 
15793b3a8eb9SGleb Smirnoff 			for (i = $3; i; i = i->next) {
15803b3a8eb9SGleb Smirnoff 				bzero(&r, sizeof(r));
15813b3a8eb9SGleb Smirnoff 
15823b3a8eb9SGleb Smirnoff 				r.action = PF_DROP;
15833b3a8eb9SGleb Smirnoff 				r.direction = PF_IN;
15843b3a8eb9SGleb Smirnoff 				r.log = $2.log;
15853b3a8eb9SGleb Smirnoff 				r.logif = $2.logif;
15863b3a8eb9SGleb Smirnoff 				r.quick = $2.quick;
15873b3a8eb9SGleb Smirnoff 				r.af = $4;
158876c5eeccSKristof Provost 				r.ridentifier = $5.ridentifier;
15893b3a8eb9SGleb Smirnoff 				if (rule_label(&r, $5.label))
15903b3a8eb9SGleb Smirnoff 					YYERROR;
15913b3a8eb9SGleb Smirnoff 				r.rtableid = $5.rtableid;
15923b3a8eb9SGleb Smirnoff 				j = calloc(1, sizeof(struct node_if));
15933b3a8eb9SGleb Smirnoff 				if (j == NULL)
15943b3a8eb9SGleb Smirnoff 					err(1, "antispoof: calloc");
15953b3a8eb9SGleb Smirnoff 				if (strlcpy(j->ifname, i->ifname,
15963b3a8eb9SGleb Smirnoff 				    sizeof(j->ifname)) >= sizeof(j->ifname)) {
15973b3a8eb9SGleb Smirnoff 					free(j);
15983b3a8eb9SGleb Smirnoff 					yyerror("interface name too long");
15993b3a8eb9SGleb Smirnoff 					YYERROR;
16003b3a8eb9SGleb Smirnoff 				}
16013b3a8eb9SGleb Smirnoff 				j->not = 1;
16023b3a8eb9SGleb Smirnoff 				if (i->dynamic) {
16033b3a8eb9SGleb Smirnoff 					h = calloc(1, sizeof(*h));
16043b3a8eb9SGleb Smirnoff 					if (h == NULL)
16053b3a8eb9SGleb Smirnoff 						err(1, "address: calloc");
16063b3a8eb9SGleb Smirnoff 					h->addr.type = PF_ADDR_DYNIFTL;
16073b3a8eb9SGleb Smirnoff 					set_ipmask(h, 128);
16083b3a8eb9SGleb Smirnoff 					if (strlcpy(h->addr.v.ifname, i->ifname,
16093b3a8eb9SGleb Smirnoff 					    sizeof(h->addr.v.ifname)) >=
16103b3a8eb9SGleb Smirnoff 					    sizeof(h->addr.v.ifname)) {
16113b3a8eb9SGleb Smirnoff 						free(h);
16123b3a8eb9SGleb Smirnoff 						yyerror(
16133b3a8eb9SGleb Smirnoff 						    "interface name too long");
16143b3a8eb9SGleb Smirnoff 						YYERROR;
16153b3a8eb9SGleb Smirnoff 					}
16163b3a8eb9SGleb Smirnoff 					hh = malloc(sizeof(*hh));
16173b3a8eb9SGleb Smirnoff 					if (hh == NULL)
16183b3a8eb9SGleb Smirnoff 						 err(1, "address: malloc");
16193b3a8eb9SGleb Smirnoff 					bcopy(h, hh, sizeof(*hh));
16203b3a8eb9SGleb Smirnoff 					h->addr.iflags = PFI_AFLAG_NETWORK;
16213b3a8eb9SGleb Smirnoff 				} else {
16223b3a8eb9SGleb Smirnoff 					h = ifa_lookup(j->ifname,
16233b3a8eb9SGleb Smirnoff 					    PFI_AFLAG_NETWORK);
16243b3a8eb9SGleb Smirnoff 					hh = NULL;
16253b3a8eb9SGleb Smirnoff 				}
16263b3a8eb9SGleb Smirnoff 
16273b3a8eb9SGleb Smirnoff 				if (h != NULL)
16283b3a8eb9SGleb Smirnoff 					expand_rule(&r, j, NULL, NULL, NULL, h,
16293b3a8eb9SGleb Smirnoff 					    NULL, NULL, NULL, NULL, NULL,
16302339ead6SKristof Provost 					    NULL, NULL, "");
16313b3a8eb9SGleb Smirnoff 
16323b3a8eb9SGleb Smirnoff 				if ((i->ifa_flags & IFF_LOOPBACK) == 0) {
16333b3a8eb9SGleb Smirnoff 					bzero(&r, sizeof(r));
16343b3a8eb9SGleb Smirnoff 
16353b3a8eb9SGleb Smirnoff 					r.action = PF_DROP;
16363b3a8eb9SGleb Smirnoff 					r.direction = PF_IN;
16373b3a8eb9SGleb Smirnoff 					r.log = $2.log;
16383b3a8eb9SGleb Smirnoff 					r.logif = $2.logif;
16393b3a8eb9SGleb Smirnoff 					r.quick = $2.quick;
16403b3a8eb9SGleb Smirnoff 					r.af = $4;
164176c5eeccSKristof Provost 					r.ridentifier = $5.ridentifier;
16423b3a8eb9SGleb Smirnoff 					if (rule_label(&r, $5.label))
16433b3a8eb9SGleb Smirnoff 						YYERROR;
16443b3a8eb9SGleb Smirnoff 					r.rtableid = $5.rtableid;
16453b3a8eb9SGleb Smirnoff 					if (hh != NULL)
16463b3a8eb9SGleb Smirnoff 						h = hh;
16473b3a8eb9SGleb Smirnoff 					else
16483b3a8eb9SGleb Smirnoff 						h = ifa_lookup(i->ifname, 0);
16493b3a8eb9SGleb Smirnoff 					if (h != NULL)
16503b3a8eb9SGleb Smirnoff 						expand_rule(&r, NULL, NULL,
16513b3a8eb9SGleb Smirnoff 						    NULL, NULL, h, NULL, NULL,
16522339ead6SKristof Provost 						    NULL, NULL, NULL, NULL, NULL, "");
16533b3a8eb9SGleb Smirnoff 				} else
16543b3a8eb9SGleb Smirnoff 					free(hh);
16553b3a8eb9SGleb Smirnoff 			}
16566fcc8e04SKristof Provost 			for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++)
16576fcc8e04SKristof Provost 				free($5.label[i]);
16583b3a8eb9SGleb Smirnoff 		}
16593b3a8eb9SGleb Smirnoff 		;
16603b3a8eb9SGleb Smirnoff 
16613b3a8eb9SGleb Smirnoff antispoof_ifspc	: FOR antispoof_if			{ $$ = $2; }
16623b3a8eb9SGleb Smirnoff 		| FOR '{' optnl antispoof_iflst '}'	{ $$ = $4; }
16633b3a8eb9SGleb Smirnoff 		;
16643b3a8eb9SGleb Smirnoff 
16653b3a8eb9SGleb Smirnoff antispoof_iflst	: antispoof_if optnl			{ $$ = $1; }
16663b3a8eb9SGleb Smirnoff 		| antispoof_iflst comma antispoof_if optnl {
16673b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
16683b3a8eb9SGleb Smirnoff 			$1->tail = $3;
16693b3a8eb9SGleb Smirnoff 			$$ = $1;
16703b3a8eb9SGleb Smirnoff 		}
16713b3a8eb9SGleb Smirnoff 		;
16723b3a8eb9SGleb Smirnoff 
16733b3a8eb9SGleb Smirnoff antispoof_if	: if_item				{ $$ = $1; }
16743b3a8eb9SGleb Smirnoff 		| '(' if_item ')'			{
16753b3a8eb9SGleb Smirnoff 			$2->dynamic = 1;
16763b3a8eb9SGleb Smirnoff 			$$ = $2;
16773b3a8eb9SGleb Smirnoff 		}
16783b3a8eb9SGleb Smirnoff 		;
16793b3a8eb9SGleb Smirnoff 
16803b3a8eb9SGleb Smirnoff antispoof_opts	:	{
16813b3a8eb9SGleb Smirnoff 				bzero(&antispoof_opts, sizeof antispoof_opts);
16823b3a8eb9SGleb Smirnoff 				antispoof_opts.rtableid = -1;
16833b3a8eb9SGleb Smirnoff 			}
16843b3a8eb9SGleb Smirnoff 		    antispoof_opts_l
16853b3a8eb9SGleb Smirnoff 			{ $$ = antispoof_opts; }
16863b3a8eb9SGleb Smirnoff 		| /* empty */	{
16873b3a8eb9SGleb Smirnoff 			bzero(&antispoof_opts, sizeof antispoof_opts);
16883b3a8eb9SGleb Smirnoff 			antispoof_opts.rtableid = -1;
16893b3a8eb9SGleb Smirnoff 			$$ = antispoof_opts;
16903b3a8eb9SGleb Smirnoff 		}
16913b3a8eb9SGleb Smirnoff 		;
16923b3a8eb9SGleb Smirnoff 
16933b3a8eb9SGleb Smirnoff antispoof_opts_l	: antispoof_opts_l antispoof_opt
16943b3a8eb9SGleb Smirnoff 			| antispoof_opt
16953b3a8eb9SGleb Smirnoff 			;
16963b3a8eb9SGleb Smirnoff 
16973b3a8eb9SGleb Smirnoff antispoof_opt	: label	{
16986fcc8e04SKristof Provost 			if (antispoof_opts.labelcount >= PF_RULE_MAX_LABEL_COUNT) {
16996fcc8e04SKristof Provost 				yyerror("label can only be used %d times", PF_RULE_MAX_LABEL_COUNT);
17003b3a8eb9SGleb Smirnoff 				YYERROR;
17013b3a8eb9SGleb Smirnoff 			}
17026fcc8e04SKristof Provost 			antispoof_opts.label[antispoof_opts.labelcount++] = $1;
17033b3a8eb9SGleb Smirnoff 		}
170476c5eeccSKristof Provost 		| RIDENTIFIER number {
170576c5eeccSKristof Provost 			antispoof_opts.ridentifier = $2;
170676c5eeccSKristof Provost 		}
17073b3a8eb9SGleb Smirnoff 		| RTABLE NUMBER				{
17083b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > rt_tableid_max()) {
17093b3a8eb9SGleb Smirnoff 				yyerror("invalid rtable id");
17103b3a8eb9SGleb Smirnoff 				YYERROR;
17113b3a8eb9SGleb Smirnoff 			}
17123b3a8eb9SGleb Smirnoff 			antispoof_opts.rtableid = $2;
17133b3a8eb9SGleb Smirnoff 		}
17143b3a8eb9SGleb Smirnoff 		;
17153b3a8eb9SGleb Smirnoff 
17163b3a8eb9SGleb Smirnoff not		: '!'		{ $$ = 1; }
17173b3a8eb9SGleb Smirnoff 		| /* empty */	{ $$ = 0; }
17183b3a8eb9SGleb Smirnoff 		;
17193b3a8eb9SGleb Smirnoff 
17203b3a8eb9SGleb Smirnoff tabledef	: TABLE '<' STRING '>' table_opts {
17213b3a8eb9SGleb Smirnoff 			struct node_host	 *h, *nh;
17223b3a8eb9SGleb Smirnoff 			struct node_tinit	 *ti, *nti;
17233b3a8eb9SGleb Smirnoff 
17243b3a8eb9SGleb Smirnoff 			if (strlen($3) >= PF_TABLE_NAME_SIZE) {
17253b3a8eb9SGleb Smirnoff 				yyerror("table name too long, max %d chars",
17263b3a8eb9SGleb Smirnoff 				    PF_TABLE_NAME_SIZE - 1);
17273b3a8eb9SGleb Smirnoff 				free($3);
17283b3a8eb9SGleb Smirnoff 				YYERROR;
17293b3a8eb9SGleb Smirnoff 			}
17303b3a8eb9SGleb Smirnoff 			if (pf->loadopt & PFCTL_FLAG_TABLE)
17313b3a8eb9SGleb Smirnoff 				if (process_tabledef($3, &$5)) {
17323b3a8eb9SGleb Smirnoff 					free($3);
17333b3a8eb9SGleb Smirnoff 					YYERROR;
17343b3a8eb9SGleb Smirnoff 				}
17353b3a8eb9SGleb Smirnoff 			free($3);
17363b3a8eb9SGleb Smirnoff 			for (ti = SIMPLEQ_FIRST(&$5.init_nodes);
17373b3a8eb9SGleb Smirnoff 			    ti != SIMPLEQ_END(&$5.init_nodes); ti = nti) {
17383b3a8eb9SGleb Smirnoff 				if (ti->file)
17393b3a8eb9SGleb Smirnoff 					free(ti->file);
17403b3a8eb9SGleb Smirnoff 				for (h = ti->host; h != NULL; h = nh) {
17413b3a8eb9SGleb Smirnoff 					nh = h->next;
17423b3a8eb9SGleb Smirnoff 					free(h);
17433b3a8eb9SGleb Smirnoff 				}
17443b3a8eb9SGleb Smirnoff 				nti = SIMPLEQ_NEXT(ti, entries);
17453b3a8eb9SGleb Smirnoff 				free(ti);
17463b3a8eb9SGleb Smirnoff 			}
17473b3a8eb9SGleb Smirnoff 		}
17483b3a8eb9SGleb Smirnoff 		;
17493b3a8eb9SGleb Smirnoff 
17503b3a8eb9SGleb Smirnoff table_opts	:	{
17513b3a8eb9SGleb Smirnoff 			bzero(&table_opts, sizeof table_opts);
17523b3a8eb9SGleb Smirnoff 			SIMPLEQ_INIT(&table_opts.init_nodes);
17533b3a8eb9SGleb Smirnoff 		}
17543b3a8eb9SGleb Smirnoff 		    table_opts_l
17553b3a8eb9SGleb Smirnoff 			{ $$ = table_opts; }
17563b3a8eb9SGleb Smirnoff 		| /* empty */
17573b3a8eb9SGleb Smirnoff 			{
17583b3a8eb9SGleb Smirnoff 			bzero(&table_opts, sizeof table_opts);
17593b3a8eb9SGleb Smirnoff 			SIMPLEQ_INIT(&table_opts.init_nodes);
17603b3a8eb9SGleb Smirnoff 			$$ = table_opts;
17613b3a8eb9SGleb Smirnoff 		}
17623b3a8eb9SGleb Smirnoff 		;
17633b3a8eb9SGleb Smirnoff 
17643b3a8eb9SGleb Smirnoff table_opts_l	: table_opts_l table_opt
17653b3a8eb9SGleb Smirnoff 		| table_opt
17663b3a8eb9SGleb Smirnoff 		;
17673b3a8eb9SGleb Smirnoff 
17683b3a8eb9SGleb Smirnoff table_opt	: STRING		{
17693b3a8eb9SGleb Smirnoff 			if (!strcmp($1, "const"))
17703b3a8eb9SGleb Smirnoff 				table_opts.flags |= PFR_TFLAG_CONST;
17713b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "persist"))
17723b3a8eb9SGleb Smirnoff 				table_opts.flags |= PFR_TFLAG_PERSIST;
17733b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "counters"))
17743b3a8eb9SGleb Smirnoff 				table_opts.flags |= PFR_TFLAG_COUNTERS;
17753b3a8eb9SGleb Smirnoff 			else {
17763b3a8eb9SGleb Smirnoff 				yyerror("invalid table option '%s'", $1);
17773b3a8eb9SGleb Smirnoff 				free($1);
17783b3a8eb9SGleb Smirnoff 				YYERROR;
17793b3a8eb9SGleb Smirnoff 			}
17803b3a8eb9SGleb Smirnoff 			free($1);
17813b3a8eb9SGleb Smirnoff 		}
17823b3a8eb9SGleb Smirnoff 		| '{' optnl '}'		{ table_opts.init_addr = 1; }
17833b3a8eb9SGleb Smirnoff 		| '{' optnl host_list '}'	{
17843b3a8eb9SGleb Smirnoff 			struct node_host	*n;
17853b3a8eb9SGleb Smirnoff 			struct node_tinit	*ti;
17863b3a8eb9SGleb Smirnoff 
17873b3a8eb9SGleb Smirnoff 			for (n = $3; n != NULL; n = n->next) {
17883b3a8eb9SGleb Smirnoff 				switch (n->addr.type) {
17893b3a8eb9SGleb Smirnoff 				case PF_ADDR_ADDRMASK:
17903b3a8eb9SGleb Smirnoff 					continue; /* ok */
17913b3a8eb9SGleb Smirnoff 				case PF_ADDR_RANGE:
17923b3a8eb9SGleb Smirnoff 					yyerror("address ranges are not "
17933b3a8eb9SGleb Smirnoff 					    "permitted inside tables");
17943b3a8eb9SGleb Smirnoff 					break;
17953b3a8eb9SGleb Smirnoff 				case PF_ADDR_DYNIFTL:
17963b3a8eb9SGleb Smirnoff 					yyerror("dynamic addresses are not "
17973b3a8eb9SGleb Smirnoff 					    "permitted inside tables");
17983b3a8eb9SGleb Smirnoff 					break;
17993b3a8eb9SGleb Smirnoff 				case PF_ADDR_TABLE:
18003b3a8eb9SGleb Smirnoff 					yyerror("tables cannot contain tables");
18013b3a8eb9SGleb Smirnoff 					break;
18023b3a8eb9SGleb Smirnoff 				case PF_ADDR_NOROUTE:
18033b3a8eb9SGleb Smirnoff 					yyerror("\"no-route\" is not permitted "
18043b3a8eb9SGleb Smirnoff 					    "inside tables");
18053b3a8eb9SGleb Smirnoff 					break;
18063b3a8eb9SGleb Smirnoff 				case PF_ADDR_URPFFAILED:
18073b3a8eb9SGleb Smirnoff 					yyerror("\"urpf-failed\" is not "
18083b3a8eb9SGleb Smirnoff 					    "permitted inside tables");
18093b3a8eb9SGleb Smirnoff 					break;
18103b3a8eb9SGleb Smirnoff 				default:
18113b3a8eb9SGleb Smirnoff 					yyerror("unknown address type %d",
18123b3a8eb9SGleb Smirnoff 					    n->addr.type);
18133b3a8eb9SGleb Smirnoff 				}
18143b3a8eb9SGleb Smirnoff 				YYERROR;
18153b3a8eb9SGleb Smirnoff 			}
18163b3a8eb9SGleb Smirnoff 			if (!(ti = calloc(1, sizeof(*ti))))
18173b3a8eb9SGleb Smirnoff 				err(1, "table_opt: calloc");
18183b3a8eb9SGleb Smirnoff 			ti->host = $3;
18193b3a8eb9SGleb Smirnoff 			SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
18203b3a8eb9SGleb Smirnoff 			    entries);
18213b3a8eb9SGleb Smirnoff 			table_opts.init_addr = 1;
18223b3a8eb9SGleb Smirnoff 		}
18233b3a8eb9SGleb Smirnoff 		| FILENAME STRING	{
18243b3a8eb9SGleb Smirnoff 			struct node_tinit	*ti;
18253b3a8eb9SGleb Smirnoff 
18263b3a8eb9SGleb Smirnoff 			if (!(ti = calloc(1, sizeof(*ti))))
18273b3a8eb9SGleb Smirnoff 				err(1, "table_opt: calloc");
18283b3a8eb9SGleb Smirnoff 			ti->file = $2;
18293b3a8eb9SGleb Smirnoff 			SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
18303b3a8eb9SGleb Smirnoff 			    entries);
18313b3a8eb9SGleb Smirnoff 			table_opts.init_addr = 1;
18323b3a8eb9SGleb Smirnoff 		}
18333b3a8eb9SGleb Smirnoff 		;
18343b3a8eb9SGleb Smirnoff 
18353b3a8eb9SGleb Smirnoff altqif		: ALTQ interface queue_opts QUEUE qassign {
18363b3a8eb9SGleb Smirnoff 			struct pf_altq	a;
18373b3a8eb9SGleb Smirnoff 
18383b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_QUEUE))
18393b3a8eb9SGleb Smirnoff 				YYERROR;
18403b3a8eb9SGleb Smirnoff 
18413b3a8eb9SGleb Smirnoff 			memset(&a, 0, sizeof(a));
18423b3a8eb9SGleb Smirnoff 			if ($3.scheduler.qtype == ALTQT_NONE) {
18433b3a8eb9SGleb Smirnoff 				yyerror("no scheduler specified!");
18443b3a8eb9SGleb Smirnoff 				YYERROR;
18453b3a8eb9SGleb Smirnoff 			}
18463b3a8eb9SGleb Smirnoff 			a.scheduler = $3.scheduler.qtype;
18473b3a8eb9SGleb Smirnoff 			a.qlimit = $3.qlimit;
18483b3a8eb9SGleb Smirnoff 			a.tbrsize = $3.tbrsize;
18490a70aaf8SLuiz Otavio O Souza 			if ($5 == NULL && $3.scheduler.qtype != ALTQT_CODEL) {
18503b3a8eb9SGleb Smirnoff 				yyerror("no child queues specified");
18513b3a8eb9SGleb Smirnoff 				YYERROR;
18523b3a8eb9SGleb Smirnoff 			}
18533b3a8eb9SGleb Smirnoff 			if (expand_altq(&a, $2, $5, $3.queue_bwspec,
18543b3a8eb9SGleb Smirnoff 			    &$3.scheduler))
18553b3a8eb9SGleb Smirnoff 				YYERROR;
18563b3a8eb9SGleb Smirnoff 		}
18573b3a8eb9SGleb Smirnoff 		;
18583b3a8eb9SGleb Smirnoff 
18593b3a8eb9SGleb Smirnoff queuespec	: QUEUE STRING interface queue_opts qassign {
18603b3a8eb9SGleb Smirnoff 			struct pf_altq	a;
18613b3a8eb9SGleb Smirnoff 
18623b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_QUEUE)) {
18633b3a8eb9SGleb Smirnoff 				free($2);
18643b3a8eb9SGleb Smirnoff 				YYERROR;
18653b3a8eb9SGleb Smirnoff 			}
18663b3a8eb9SGleb Smirnoff 
18673b3a8eb9SGleb Smirnoff 			memset(&a, 0, sizeof(a));
18683b3a8eb9SGleb Smirnoff 
18693b3a8eb9SGleb Smirnoff 			if (strlcpy(a.qname, $2, sizeof(a.qname)) >=
18703b3a8eb9SGleb Smirnoff 			    sizeof(a.qname)) {
18713b3a8eb9SGleb Smirnoff 				yyerror("queue name too long (max "
18723b3a8eb9SGleb Smirnoff 				    "%d chars)", PF_QNAME_SIZE-1);
18733b3a8eb9SGleb Smirnoff 				free($2);
18743b3a8eb9SGleb Smirnoff 				YYERROR;
18753b3a8eb9SGleb Smirnoff 			}
18763b3a8eb9SGleb Smirnoff 			free($2);
18773b3a8eb9SGleb Smirnoff 			if ($4.tbrsize) {
18783b3a8eb9SGleb Smirnoff 				yyerror("cannot specify tbrsize for queue");
18793b3a8eb9SGleb Smirnoff 				YYERROR;
18803b3a8eb9SGleb Smirnoff 			}
18813b3a8eb9SGleb Smirnoff 			if ($4.priority > 255) {
18823b3a8eb9SGleb Smirnoff 				yyerror("priority out of range: max 255");
18833b3a8eb9SGleb Smirnoff 				YYERROR;
18843b3a8eb9SGleb Smirnoff 			}
18853b3a8eb9SGleb Smirnoff 			a.priority = $4.priority;
18863b3a8eb9SGleb Smirnoff 			a.qlimit = $4.qlimit;
18873b3a8eb9SGleb Smirnoff 			a.scheduler = $4.scheduler.qtype;
18883b3a8eb9SGleb Smirnoff 			if (expand_queue(&a, $3, $5, $4.queue_bwspec,
18893b3a8eb9SGleb Smirnoff 			    &$4.scheduler)) {
18903b3a8eb9SGleb Smirnoff 				yyerror("errors in queue definition");
18913b3a8eb9SGleb Smirnoff 				YYERROR;
18923b3a8eb9SGleb Smirnoff 			}
18933b3a8eb9SGleb Smirnoff 		}
18943b3a8eb9SGleb Smirnoff 		;
18953b3a8eb9SGleb Smirnoff 
18963b3a8eb9SGleb Smirnoff queue_opts	:	{
18973b3a8eb9SGleb Smirnoff 			bzero(&queue_opts, sizeof queue_opts);
18983b3a8eb9SGleb Smirnoff 			queue_opts.priority = DEFAULT_PRIORITY;
18993b3a8eb9SGleb Smirnoff 			queue_opts.qlimit = DEFAULT_QLIMIT;
19003b3a8eb9SGleb Smirnoff 			queue_opts.scheduler.qtype = ALTQT_NONE;
19013b3a8eb9SGleb Smirnoff 			queue_opts.queue_bwspec.bw_percent = 100;
19023b3a8eb9SGleb Smirnoff 		}
19033b3a8eb9SGleb Smirnoff 		    queue_opts_l
19043b3a8eb9SGleb Smirnoff 			{ $$ = queue_opts; }
19053b3a8eb9SGleb Smirnoff 		| /* empty */ {
19063b3a8eb9SGleb Smirnoff 			bzero(&queue_opts, sizeof queue_opts);
19073b3a8eb9SGleb Smirnoff 			queue_opts.priority = DEFAULT_PRIORITY;
19083b3a8eb9SGleb Smirnoff 			queue_opts.qlimit = DEFAULT_QLIMIT;
19093b3a8eb9SGleb Smirnoff 			queue_opts.scheduler.qtype = ALTQT_NONE;
19103b3a8eb9SGleb Smirnoff 			queue_opts.queue_bwspec.bw_percent = 100;
19113b3a8eb9SGleb Smirnoff 			$$ = queue_opts;
19123b3a8eb9SGleb Smirnoff 		}
19133b3a8eb9SGleb Smirnoff 		;
19143b3a8eb9SGleb Smirnoff 
19153b3a8eb9SGleb Smirnoff queue_opts_l	: queue_opts_l queue_opt
19163b3a8eb9SGleb Smirnoff 		| queue_opt
19173b3a8eb9SGleb Smirnoff 		;
19183b3a8eb9SGleb Smirnoff 
19193b3a8eb9SGleb Smirnoff queue_opt	: BANDWIDTH bandwidth	{
19203b3a8eb9SGleb Smirnoff 			if (queue_opts.marker & QOM_BWSPEC) {
19213b3a8eb9SGleb Smirnoff 				yyerror("bandwidth cannot be respecified");
19223b3a8eb9SGleb Smirnoff 				YYERROR;
19233b3a8eb9SGleb Smirnoff 			}
19243b3a8eb9SGleb Smirnoff 			queue_opts.marker |= QOM_BWSPEC;
19253b3a8eb9SGleb Smirnoff 			queue_opts.queue_bwspec = $2;
19263b3a8eb9SGleb Smirnoff 		}
19273b3a8eb9SGleb Smirnoff 		| PRIORITY NUMBER	{
19283b3a8eb9SGleb Smirnoff 			if (queue_opts.marker & QOM_PRIORITY) {
19293b3a8eb9SGleb Smirnoff 				yyerror("priority cannot be respecified");
19303b3a8eb9SGleb Smirnoff 				YYERROR;
19313b3a8eb9SGleb Smirnoff 			}
19323b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > 255) {
19333b3a8eb9SGleb Smirnoff 				yyerror("priority out of range: max 255");
19343b3a8eb9SGleb Smirnoff 				YYERROR;
19353b3a8eb9SGleb Smirnoff 			}
19363b3a8eb9SGleb Smirnoff 			queue_opts.marker |= QOM_PRIORITY;
19373b3a8eb9SGleb Smirnoff 			queue_opts.priority = $2;
19383b3a8eb9SGleb Smirnoff 		}
19393b3a8eb9SGleb Smirnoff 		| QLIMIT NUMBER	{
19403b3a8eb9SGleb Smirnoff 			if (queue_opts.marker & QOM_QLIMIT) {
19413b3a8eb9SGleb Smirnoff 				yyerror("qlimit cannot be respecified");
19423b3a8eb9SGleb Smirnoff 				YYERROR;
19433b3a8eb9SGleb Smirnoff 			}
19443b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > 65535) {
19453b3a8eb9SGleb Smirnoff 				yyerror("qlimit out of range: max 65535");
19463b3a8eb9SGleb Smirnoff 				YYERROR;
19473b3a8eb9SGleb Smirnoff 			}
19483b3a8eb9SGleb Smirnoff 			queue_opts.marker |= QOM_QLIMIT;
19493b3a8eb9SGleb Smirnoff 			queue_opts.qlimit = $2;
19503b3a8eb9SGleb Smirnoff 		}
19513b3a8eb9SGleb Smirnoff 		| scheduler	{
19523b3a8eb9SGleb Smirnoff 			if (queue_opts.marker & QOM_SCHEDULER) {
19533b3a8eb9SGleb Smirnoff 				yyerror("scheduler cannot be respecified");
19543b3a8eb9SGleb Smirnoff 				YYERROR;
19553b3a8eb9SGleb Smirnoff 			}
19563b3a8eb9SGleb Smirnoff 			queue_opts.marker |= QOM_SCHEDULER;
19573b3a8eb9SGleb Smirnoff 			queue_opts.scheduler = $1;
19583b3a8eb9SGleb Smirnoff 		}
19593b3a8eb9SGleb Smirnoff 		| TBRSIZE NUMBER	{
19603b3a8eb9SGleb Smirnoff 			if (queue_opts.marker & QOM_TBRSIZE) {
19613b3a8eb9SGleb Smirnoff 				yyerror("tbrsize cannot be respecified");
19623b3a8eb9SGleb Smirnoff 				YYERROR;
19633b3a8eb9SGleb Smirnoff 			}
1964249cc75fSPatrick Kelsey 			if ($2 < 0 || $2 > UINT_MAX) {
1965249cc75fSPatrick Kelsey 				yyerror("tbrsize too big: max %u", UINT_MAX);
19663b3a8eb9SGleb Smirnoff 				YYERROR;
19673b3a8eb9SGleb Smirnoff 			}
19683b3a8eb9SGleb Smirnoff 			queue_opts.marker |= QOM_TBRSIZE;
19693b3a8eb9SGleb Smirnoff 			queue_opts.tbrsize = $2;
19703b3a8eb9SGleb Smirnoff 		}
19713b3a8eb9SGleb Smirnoff 		;
19723b3a8eb9SGleb Smirnoff 
19733b3a8eb9SGleb Smirnoff bandwidth	: STRING {
19743b3a8eb9SGleb Smirnoff 			double	 bps;
19753b3a8eb9SGleb Smirnoff 			char	*cp;
19763b3a8eb9SGleb Smirnoff 
19773b3a8eb9SGleb Smirnoff 			$$.bw_percent = 0;
19783b3a8eb9SGleb Smirnoff 
19793b3a8eb9SGleb Smirnoff 			bps = strtod($1, &cp);
19803b3a8eb9SGleb Smirnoff 			if (cp != NULL) {
1981db1bbde6SLuiz Otavio O Souza 				if (strlen(cp) > 1) {
1982db1bbde6SLuiz Otavio O Souza 					char *cu = cp + 1;
1983db1bbde6SLuiz Otavio O Souza 					if (!strcmp(cu, "Bit") ||
1984db1bbde6SLuiz Otavio O Souza 					    !strcmp(cu, "B") ||
1985db1bbde6SLuiz Otavio O Souza 					    !strcmp(cu, "bit") ||
1986db1bbde6SLuiz Otavio O Souza 					    !strcmp(cu, "b")) {
1987db1bbde6SLuiz Otavio O Souza 						*cu = 0;
1988db1bbde6SLuiz Otavio O Souza 					}
1989db1bbde6SLuiz Otavio O Souza 				}
19903b3a8eb9SGleb Smirnoff 				if (!strcmp(cp, "b"))
19913b3a8eb9SGleb Smirnoff 					; /* nothing */
1992db1bbde6SLuiz Otavio O Souza 				else if (!strcmp(cp, "K"))
19933b3a8eb9SGleb Smirnoff 					bps *= 1000;
1994db1bbde6SLuiz Otavio O Souza 				else if (!strcmp(cp, "M"))
19953b3a8eb9SGleb Smirnoff 					bps *= 1000 * 1000;
1996db1bbde6SLuiz Otavio O Souza 				else if (!strcmp(cp, "G"))
19973b3a8eb9SGleb Smirnoff 					bps *= 1000 * 1000 * 1000;
19983b3a8eb9SGleb Smirnoff 				else if (!strcmp(cp, "%")) {
19993b3a8eb9SGleb Smirnoff 					if (bps < 0 || bps > 100) {
20003b3a8eb9SGleb Smirnoff 						yyerror("bandwidth spec "
20013b3a8eb9SGleb Smirnoff 						    "out of range");
20023b3a8eb9SGleb Smirnoff 						free($1);
20033b3a8eb9SGleb Smirnoff 						YYERROR;
20043b3a8eb9SGleb Smirnoff 					}
20053b3a8eb9SGleb Smirnoff 					$$.bw_percent = bps;
20063b3a8eb9SGleb Smirnoff 					bps = 0;
20073b3a8eb9SGleb Smirnoff 				} else {
20083b3a8eb9SGleb Smirnoff 					yyerror("unknown unit %s", cp);
20093b3a8eb9SGleb Smirnoff 					free($1);
20103b3a8eb9SGleb Smirnoff 					YYERROR;
20113b3a8eb9SGleb Smirnoff 				}
20123b3a8eb9SGleb Smirnoff 			}
20133b3a8eb9SGleb Smirnoff 			free($1);
2014249cc75fSPatrick Kelsey 			$$.bw_absolute = (u_int64_t)bps;
20153b3a8eb9SGleb Smirnoff 		}
20163b3a8eb9SGleb Smirnoff 		| NUMBER {
2017249cc75fSPatrick Kelsey 			if ($1 < 0 || $1 >= LLONG_MAX) {
20183b3a8eb9SGleb Smirnoff 				yyerror("bandwidth number too big");
20193b3a8eb9SGleb Smirnoff 				YYERROR;
20203b3a8eb9SGleb Smirnoff 			}
20213b3a8eb9SGleb Smirnoff 			$$.bw_percent = 0;
20223b3a8eb9SGleb Smirnoff 			$$.bw_absolute = $1;
20233b3a8eb9SGleb Smirnoff 		}
20243b3a8eb9SGleb Smirnoff 		;
20253b3a8eb9SGleb Smirnoff 
20263b3a8eb9SGleb Smirnoff scheduler	: CBQ				{
20273b3a8eb9SGleb Smirnoff 			$$.qtype = ALTQT_CBQ;
20283b3a8eb9SGleb Smirnoff 			$$.data.cbq_opts.flags = 0;
20293b3a8eb9SGleb Smirnoff 		}
20303b3a8eb9SGleb Smirnoff 		| CBQ '(' cbqflags_list ')'	{
20313b3a8eb9SGleb Smirnoff 			$$.qtype = ALTQT_CBQ;
20323b3a8eb9SGleb Smirnoff 			$$.data.cbq_opts.flags = $3;
20333b3a8eb9SGleb Smirnoff 		}
20343b3a8eb9SGleb Smirnoff 		| PRIQ				{
20353b3a8eb9SGleb Smirnoff 			$$.qtype = ALTQT_PRIQ;
20363b3a8eb9SGleb Smirnoff 			$$.data.priq_opts.flags = 0;
20373b3a8eb9SGleb Smirnoff 		}
20383b3a8eb9SGleb Smirnoff 		| PRIQ '(' priqflags_list ')'	{
20393b3a8eb9SGleb Smirnoff 			$$.qtype = ALTQT_PRIQ;
20403b3a8eb9SGleb Smirnoff 			$$.data.priq_opts.flags = $3;
20413b3a8eb9SGleb Smirnoff 		}
20423b3a8eb9SGleb Smirnoff 		| HFSC				{
20433b3a8eb9SGleb Smirnoff 			$$.qtype = ALTQT_HFSC;
20443b3a8eb9SGleb Smirnoff 			bzero(&$$.data.hfsc_opts,
20453b3a8eb9SGleb Smirnoff 			    sizeof(struct node_hfsc_opts));
20463b3a8eb9SGleb Smirnoff 		}
20473b3a8eb9SGleb Smirnoff 		| HFSC '(' hfsc_opts ')'	{
20483b3a8eb9SGleb Smirnoff 			$$.qtype = ALTQT_HFSC;
20493b3a8eb9SGleb Smirnoff 			$$.data.hfsc_opts = $3;
20503b3a8eb9SGleb Smirnoff 		}
2051a5b789f6SErmal Luçi 		| FAIRQ				{
2052a5b789f6SErmal Luçi 			$$.qtype = ALTQT_FAIRQ;
2053a5b789f6SErmal Luçi 			bzero(&$$.data.fairq_opts,
2054a5b789f6SErmal Luçi 				sizeof(struct node_fairq_opts));
2055a5b789f6SErmal Luçi 		}
2056a5b789f6SErmal Luçi 		| FAIRQ '(' fairq_opts ')'      {
2057a5b789f6SErmal Luçi 			$$.qtype = ALTQT_FAIRQ;
2058a5b789f6SErmal Luçi 			$$.data.fairq_opts = $3;
2059a5b789f6SErmal Luçi 		}
20600a70aaf8SLuiz Otavio O Souza 		| CODEL				{
20610a70aaf8SLuiz Otavio O Souza 			$$.qtype = ALTQT_CODEL;
20620a70aaf8SLuiz Otavio O Souza 			bzero(&$$.data.codel_opts,
20630a70aaf8SLuiz Otavio O Souza 				sizeof(struct codel_opts));
20640a70aaf8SLuiz Otavio O Souza 		}
20650a70aaf8SLuiz Otavio O Souza 		| CODEL '(' codel_opts ')'	{
20660a70aaf8SLuiz Otavio O Souza 			$$.qtype = ALTQT_CODEL;
20670a70aaf8SLuiz Otavio O Souza 			$$.data.codel_opts = $3;
20680a70aaf8SLuiz Otavio O Souza 		}
20693b3a8eb9SGleb Smirnoff 		;
20703b3a8eb9SGleb Smirnoff 
20713b3a8eb9SGleb Smirnoff cbqflags_list	: cbqflags_item				{ $$ |= $1; }
20723b3a8eb9SGleb Smirnoff 		| cbqflags_list comma cbqflags_item	{ $$ |= $3; }
20733b3a8eb9SGleb Smirnoff 		;
20743b3a8eb9SGleb Smirnoff 
20753b3a8eb9SGleb Smirnoff cbqflags_item	: STRING	{
20763b3a8eb9SGleb Smirnoff 			if (!strcmp($1, "default"))
20773b3a8eb9SGleb Smirnoff 				$$ = CBQCLF_DEFCLASS;
20783b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "borrow"))
20793b3a8eb9SGleb Smirnoff 				$$ = CBQCLF_BORROW;
20803b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "red"))
20813b3a8eb9SGleb Smirnoff 				$$ = CBQCLF_RED;
20823b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "ecn"))
20833b3a8eb9SGleb Smirnoff 				$$ = CBQCLF_RED|CBQCLF_ECN;
20843b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "rio"))
20853b3a8eb9SGleb Smirnoff 				$$ = CBQCLF_RIO;
20860a70aaf8SLuiz Otavio O Souza 			else if (!strcmp($1, "codel"))
20870a70aaf8SLuiz Otavio O Souza 				$$ = CBQCLF_CODEL;
20883b3a8eb9SGleb Smirnoff 			else {
20893b3a8eb9SGleb Smirnoff 				yyerror("unknown cbq flag \"%s\"", $1);
20903b3a8eb9SGleb Smirnoff 				free($1);
20913b3a8eb9SGleb Smirnoff 				YYERROR;
20923b3a8eb9SGleb Smirnoff 			}
20933b3a8eb9SGleb Smirnoff 			free($1);
20943b3a8eb9SGleb Smirnoff 		}
20953b3a8eb9SGleb Smirnoff 		;
20963b3a8eb9SGleb Smirnoff 
20973b3a8eb9SGleb Smirnoff priqflags_list	: priqflags_item			{ $$ |= $1; }
20983b3a8eb9SGleb Smirnoff 		| priqflags_list comma priqflags_item	{ $$ |= $3; }
20993b3a8eb9SGleb Smirnoff 		;
21003b3a8eb9SGleb Smirnoff 
21013b3a8eb9SGleb Smirnoff priqflags_item	: STRING	{
21023b3a8eb9SGleb Smirnoff 			if (!strcmp($1, "default"))
21033b3a8eb9SGleb Smirnoff 				$$ = PRCF_DEFAULTCLASS;
21043b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "red"))
21053b3a8eb9SGleb Smirnoff 				$$ = PRCF_RED;
21063b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "ecn"))
21073b3a8eb9SGleb Smirnoff 				$$ = PRCF_RED|PRCF_ECN;
21083b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "rio"))
21093b3a8eb9SGleb Smirnoff 				$$ = PRCF_RIO;
21100a70aaf8SLuiz Otavio O Souza 			else if (!strcmp($1, "codel"))
21110a70aaf8SLuiz Otavio O Souza 				$$ = PRCF_CODEL;
21123b3a8eb9SGleb Smirnoff 			else {
21133b3a8eb9SGleb Smirnoff 				yyerror("unknown priq flag \"%s\"", $1);
21143b3a8eb9SGleb Smirnoff 				free($1);
21153b3a8eb9SGleb Smirnoff 				YYERROR;
21163b3a8eb9SGleb Smirnoff 			}
21173b3a8eb9SGleb Smirnoff 			free($1);
21183b3a8eb9SGleb Smirnoff 		}
21193b3a8eb9SGleb Smirnoff 		;
21203b3a8eb9SGleb Smirnoff 
21213b3a8eb9SGleb Smirnoff hfsc_opts	:	{
21223b3a8eb9SGleb Smirnoff 				bzero(&hfsc_opts,
21233b3a8eb9SGleb Smirnoff 				    sizeof(struct node_hfsc_opts));
21243b3a8eb9SGleb Smirnoff 			}
21253b3a8eb9SGleb Smirnoff 		    hfscopts_list				{
21263b3a8eb9SGleb Smirnoff 			$$ = hfsc_opts;
21273b3a8eb9SGleb Smirnoff 		}
21283b3a8eb9SGleb Smirnoff 		;
21293b3a8eb9SGleb Smirnoff 
21303b3a8eb9SGleb Smirnoff hfscopts_list	: hfscopts_item
21313b3a8eb9SGleb Smirnoff 		| hfscopts_list comma hfscopts_item
21323b3a8eb9SGleb Smirnoff 		;
21333b3a8eb9SGleb Smirnoff 
21343b3a8eb9SGleb Smirnoff hfscopts_item	: LINKSHARE bandwidth				{
21353b3a8eb9SGleb Smirnoff 			if (hfsc_opts.linkshare.used) {
21363b3a8eb9SGleb Smirnoff 				yyerror("linkshare already specified");
21373b3a8eb9SGleb Smirnoff 				YYERROR;
21383b3a8eb9SGleb Smirnoff 			}
21393b3a8eb9SGleb Smirnoff 			hfsc_opts.linkshare.m2 = $2;
21403b3a8eb9SGleb Smirnoff 			hfsc_opts.linkshare.used = 1;
21413b3a8eb9SGleb Smirnoff 		}
21423b3a8eb9SGleb Smirnoff 		| LINKSHARE '(' bandwidth comma NUMBER comma bandwidth ')'
21433b3a8eb9SGleb Smirnoff 		    {
21443b3a8eb9SGleb Smirnoff 			if ($5 < 0 || $5 > INT_MAX) {
21453b3a8eb9SGleb Smirnoff 				yyerror("timing in curve out of range");
21463b3a8eb9SGleb Smirnoff 				YYERROR;
21473b3a8eb9SGleb Smirnoff 			}
21483b3a8eb9SGleb Smirnoff 			if (hfsc_opts.linkshare.used) {
21493b3a8eb9SGleb Smirnoff 				yyerror("linkshare already specified");
21503b3a8eb9SGleb Smirnoff 				YYERROR;
21513b3a8eb9SGleb Smirnoff 			}
21523b3a8eb9SGleb Smirnoff 			hfsc_opts.linkshare.m1 = $3;
21533b3a8eb9SGleb Smirnoff 			hfsc_opts.linkshare.d = $5;
21543b3a8eb9SGleb Smirnoff 			hfsc_opts.linkshare.m2 = $7;
21553b3a8eb9SGleb Smirnoff 			hfsc_opts.linkshare.used = 1;
21563b3a8eb9SGleb Smirnoff 		}
21573b3a8eb9SGleb Smirnoff 		| REALTIME bandwidth				{
21583b3a8eb9SGleb Smirnoff 			if (hfsc_opts.realtime.used) {
21593b3a8eb9SGleb Smirnoff 				yyerror("realtime already specified");
21603b3a8eb9SGleb Smirnoff 				YYERROR;
21613b3a8eb9SGleb Smirnoff 			}
21623b3a8eb9SGleb Smirnoff 			hfsc_opts.realtime.m2 = $2;
21633b3a8eb9SGleb Smirnoff 			hfsc_opts.realtime.used = 1;
21643b3a8eb9SGleb Smirnoff 		}
21653b3a8eb9SGleb Smirnoff 		| REALTIME '(' bandwidth comma NUMBER comma bandwidth ')'
21663b3a8eb9SGleb Smirnoff 		    {
21673b3a8eb9SGleb Smirnoff 			if ($5 < 0 || $5 > INT_MAX) {
21683b3a8eb9SGleb Smirnoff 				yyerror("timing in curve out of range");
21693b3a8eb9SGleb Smirnoff 				YYERROR;
21703b3a8eb9SGleb Smirnoff 			}
21713b3a8eb9SGleb Smirnoff 			if (hfsc_opts.realtime.used) {
21723b3a8eb9SGleb Smirnoff 				yyerror("realtime already specified");
21733b3a8eb9SGleb Smirnoff 				YYERROR;
21743b3a8eb9SGleb Smirnoff 			}
21753b3a8eb9SGleb Smirnoff 			hfsc_opts.realtime.m1 = $3;
21763b3a8eb9SGleb Smirnoff 			hfsc_opts.realtime.d = $5;
21773b3a8eb9SGleb Smirnoff 			hfsc_opts.realtime.m2 = $7;
21783b3a8eb9SGleb Smirnoff 			hfsc_opts.realtime.used = 1;
21793b3a8eb9SGleb Smirnoff 		}
21803b3a8eb9SGleb Smirnoff 		| UPPERLIMIT bandwidth				{
21813b3a8eb9SGleb Smirnoff 			if (hfsc_opts.upperlimit.used) {
21823b3a8eb9SGleb Smirnoff 				yyerror("upperlimit already specified");
21833b3a8eb9SGleb Smirnoff 				YYERROR;
21843b3a8eb9SGleb Smirnoff 			}
21853b3a8eb9SGleb Smirnoff 			hfsc_opts.upperlimit.m2 = $2;
21863b3a8eb9SGleb Smirnoff 			hfsc_opts.upperlimit.used = 1;
21873b3a8eb9SGleb Smirnoff 		}
21883b3a8eb9SGleb Smirnoff 		| UPPERLIMIT '(' bandwidth comma NUMBER comma bandwidth ')'
21893b3a8eb9SGleb Smirnoff 		    {
21903b3a8eb9SGleb Smirnoff 			if ($5 < 0 || $5 > INT_MAX) {
21913b3a8eb9SGleb Smirnoff 				yyerror("timing in curve out of range");
21923b3a8eb9SGleb Smirnoff 				YYERROR;
21933b3a8eb9SGleb Smirnoff 			}
21943b3a8eb9SGleb Smirnoff 			if (hfsc_opts.upperlimit.used) {
21953b3a8eb9SGleb Smirnoff 				yyerror("upperlimit already specified");
21963b3a8eb9SGleb Smirnoff 				YYERROR;
21973b3a8eb9SGleb Smirnoff 			}
21983b3a8eb9SGleb Smirnoff 			hfsc_opts.upperlimit.m1 = $3;
21993b3a8eb9SGleb Smirnoff 			hfsc_opts.upperlimit.d = $5;
22003b3a8eb9SGleb Smirnoff 			hfsc_opts.upperlimit.m2 = $7;
22013b3a8eb9SGleb Smirnoff 			hfsc_opts.upperlimit.used = 1;
22023b3a8eb9SGleb Smirnoff 		}
22033b3a8eb9SGleb Smirnoff 		| STRING	{
22043b3a8eb9SGleb Smirnoff 			if (!strcmp($1, "default"))
22053b3a8eb9SGleb Smirnoff 				hfsc_opts.flags |= HFCF_DEFAULTCLASS;
22063b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "red"))
22073b3a8eb9SGleb Smirnoff 				hfsc_opts.flags |= HFCF_RED;
22083b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "ecn"))
22093b3a8eb9SGleb Smirnoff 				hfsc_opts.flags |= HFCF_RED|HFCF_ECN;
22103b3a8eb9SGleb Smirnoff 			else if (!strcmp($1, "rio"))
22113b3a8eb9SGleb Smirnoff 				hfsc_opts.flags |= HFCF_RIO;
22120a70aaf8SLuiz Otavio O Souza 			else if (!strcmp($1, "codel"))
22130a70aaf8SLuiz Otavio O Souza 				hfsc_opts.flags |= HFCF_CODEL;
22143b3a8eb9SGleb Smirnoff 			else {
22153b3a8eb9SGleb Smirnoff 				yyerror("unknown hfsc flag \"%s\"", $1);
22163b3a8eb9SGleb Smirnoff 				free($1);
22173b3a8eb9SGleb Smirnoff 				YYERROR;
22183b3a8eb9SGleb Smirnoff 			}
22193b3a8eb9SGleb Smirnoff 			free($1);
22203b3a8eb9SGleb Smirnoff 		}
22213b3a8eb9SGleb Smirnoff 		;
22223b3a8eb9SGleb Smirnoff 
2223a5b789f6SErmal Luçi fairq_opts	:	{
2224a5b789f6SErmal Luçi 				bzero(&fairq_opts,
2225a5b789f6SErmal Luçi 				    sizeof(struct node_fairq_opts));
2226a5b789f6SErmal Luçi 			}
2227a5b789f6SErmal Luçi 		    fairqopts_list				{
2228a5b789f6SErmal Luçi 			$$ = fairq_opts;
2229a5b789f6SErmal Luçi 		}
2230a5b789f6SErmal Luçi 		;
2231a5b789f6SErmal Luçi 
2232a5b789f6SErmal Luçi fairqopts_list	: fairqopts_item
2233a5b789f6SErmal Luçi 		| fairqopts_list comma fairqopts_item
2234a5b789f6SErmal Luçi 		;
2235a5b789f6SErmal Luçi 
2236a5b789f6SErmal Luçi fairqopts_item	: LINKSHARE bandwidth				{
2237a5b789f6SErmal Luçi 			if (fairq_opts.linkshare.used) {
2238a5b789f6SErmal Luçi 				yyerror("linkshare already specified");
2239a5b789f6SErmal Luçi 				YYERROR;
2240a5b789f6SErmal Luçi 			}
2241a5b789f6SErmal Luçi 			fairq_opts.linkshare.m2 = $2;
2242a5b789f6SErmal Luçi 			fairq_opts.linkshare.used = 1;
2243a5b789f6SErmal Luçi 		}
2244a5b789f6SErmal Luçi 		| LINKSHARE '(' bandwidth number bandwidth ')'	{
2245a5b789f6SErmal Luçi 			if (fairq_opts.linkshare.used) {
2246a5b789f6SErmal Luçi 				yyerror("linkshare already specified");
2247a5b789f6SErmal Luçi 				YYERROR;
2248a5b789f6SErmal Luçi 			}
2249a5b789f6SErmal Luçi 			fairq_opts.linkshare.m1 = $3;
2250a5b789f6SErmal Luçi 			fairq_opts.linkshare.d = $4;
2251a5b789f6SErmal Luçi 			fairq_opts.linkshare.m2 = $5;
2252a5b789f6SErmal Luçi 			fairq_opts.linkshare.used = 1;
2253a5b789f6SErmal Luçi 		}
2254a5b789f6SErmal Luçi 		| HOGS bandwidth {
2255a5b789f6SErmal Luçi 			fairq_opts.hogs_bw = $2;
2256a5b789f6SErmal Luçi 		}
2257a5b789f6SErmal Luçi 		| BUCKETS number {
2258a5b789f6SErmal Luçi 			fairq_opts.nbuckets = $2;
2259a5b789f6SErmal Luçi 		}
2260a5b789f6SErmal Luçi 		| STRING	{
2261a5b789f6SErmal Luçi 			if (!strcmp($1, "default"))
2262a5b789f6SErmal Luçi 				fairq_opts.flags |= FARF_DEFAULTCLASS;
2263a5b789f6SErmal Luçi 			else if (!strcmp($1, "red"))
2264a5b789f6SErmal Luçi 				fairq_opts.flags |= FARF_RED;
2265a5b789f6SErmal Luçi 			else if (!strcmp($1, "ecn"))
2266a5b789f6SErmal Luçi 				fairq_opts.flags |= FARF_RED|FARF_ECN;
2267a5b789f6SErmal Luçi 			else if (!strcmp($1, "rio"))
2268a5b789f6SErmal Luçi 				fairq_opts.flags |= FARF_RIO;
22690a70aaf8SLuiz Otavio O Souza 			else if (!strcmp($1, "codel"))
22700a70aaf8SLuiz Otavio O Souza 				fairq_opts.flags |= FARF_CODEL;
2271a5b789f6SErmal Luçi 			else {
2272a5b789f6SErmal Luçi 				yyerror("unknown fairq flag \"%s\"", $1);
2273a5b789f6SErmal Luçi 				free($1);
2274a5b789f6SErmal Luçi 				YYERROR;
2275a5b789f6SErmal Luçi 			}
2276a5b789f6SErmal Luçi 			free($1);
2277a5b789f6SErmal Luçi 		}
2278a5b789f6SErmal Luçi 		;
2279a5b789f6SErmal Luçi 
22800a70aaf8SLuiz Otavio O Souza codel_opts	:	{
22810a70aaf8SLuiz Otavio O Souza 				bzero(&codel_opts,
22820a70aaf8SLuiz Otavio O Souza 				    sizeof(struct codel_opts));
22830a70aaf8SLuiz Otavio O Souza 			}
22840a70aaf8SLuiz Otavio O Souza 		    codelopts_list				{
22850a70aaf8SLuiz Otavio O Souza 			$$ = codel_opts;
22860a70aaf8SLuiz Otavio O Souza 		}
22870a70aaf8SLuiz Otavio O Souza 		;
22880a70aaf8SLuiz Otavio O Souza 
22890a70aaf8SLuiz Otavio O Souza codelopts_list	: codelopts_item
22900a70aaf8SLuiz Otavio O Souza 		| codelopts_list comma codelopts_item
22910a70aaf8SLuiz Otavio O Souza 		;
22920a70aaf8SLuiz Otavio O Souza 
22930a70aaf8SLuiz Otavio O Souza codelopts_item	: INTERVAL number				{
22940a70aaf8SLuiz Otavio O Souza 			if (codel_opts.interval) {
22950a70aaf8SLuiz Otavio O Souza 				yyerror("interval already specified");
22960a70aaf8SLuiz Otavio O Souza 				YYERROR;
22970a70aaf8SLuiz Otavio O Souza 			}
22980a70aaf8SLuiz Otavio O Souza 			codel_opts.interval = $2;
22990a70aaf8SLuiz Otavio O Souza 		}
23000a70aaf8SLuiz Otavio O Souza 		| TARGET number					{
23010a70aaf8SLuiz Otavio O Souza 			if (codel_opts.target) {
23020a70aaf8SLuiz Otavio O Souza 				yyerror("target already specified");
23030a70aaf8SLuiz Otavio O Souza 				YYERROR;
23040a70aaf8SLuiz Otavio O Souza 			}
23050a70aaf8SLuiz Otavio O Souza 			codel_opts.target = $2;
23060a70aaf8SLuiz Otavio O Souza 		}
23070a70aaf8SLuiz Otavio O Souza 		| STRING					{
23080a70aaf8SLuiz Otavio O Souza 			if (!strcmp($1, "ecn"))
23090a70aaf8SLuiz Otavio O Souza 				codel_opts.ecn = 1;
23100a70aaf8SLuiz Otavio O Souza 			else {
23110a70aaf8SLuiz Otavio O Souza 				yyerror("unknown codel option \"%s\"", $1);
23120a70aaf8SLuiz Otavio O Souza 				free($1);
23130a70aaf8SLuiz Otavio O Souza 				YYERROR;
23140a70aaf8SLuiz Otavio O Souza 			}
23150a70aaf8SLuiz Otavio O Souza 			free($1);
23160a70aaf8SLuiz Otavio O Souza 		}
23170a70aaf8SLuiz Otavio O Souza 		;
23180a70aaf8SLuiz Otavio O Souza 
23193b3a8eb9SGleb Smirnoff qassign		: /* empty */		{ $$ = NULL; }
23203b3a8eb9SGleb Smirnoff 		| qassign_item		{ $$ = $1; }
23213b3a8eb9SGleb Smirnoff 		| '{' optnl qassign_list '}'	{ $$ = $3; }
23223b3a8eb9SGleb Smirnoff 		;
23233b3a8eb9SGleb Smirnoff 
23243b3a8eb9SGleb Smirnoff qassign_list	: qassign_item optnl		{ $$ = $1; }
23253b3a8eb9SGleb Smirnoff 		| qassign_list comma qassign_item optnl	{
23263b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
23273b3a8eb9SGleb Smirnoff 			$1->tail = $3;
23283b3a8eb9SGleb Smirnoff 			$$ = $1;
23293b3a8eb9SGleb Smirnoff 		}
23303b3a8eb9SGleb Smirnoff 		;
23313b3a8eb9SGleb Smirnoff 
23323b3a8eb9SGleb Smirnoff qassign_item	: STRING			{
23333b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_queue));
23343b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
23353b3a8eb9SGleb Smirnoff 				err(1, "qassign_item: calloc");
23363b3a8eb9SGleb Smirnoff 			if (strlcpy($$->queue, $1, sizeof($$->queue)) >=
23373b3a8eb9SGleb Smirnoff 			    sizeof($$->queue)) {
23383b3a8eb9SGleb Smirnoff 				yyerror("queue name '%s' too long (max "
23393b3a8eb9SGleb Smirnoff 				    "%d chars)", $1, sizeof($$->queue)-1);
23403b3a8eb9SGleb Smirnoff 				free($1);
23413b3a8eb9SGleb Smirnoff 				free($$);
23423b3a8eb9SGleb Smirnoff 				YYERROR;
23433b3a8eb9SGleb Smirnoff 			}
23443b3a8eb9SGleb Smirnoff 			free($1);
23453b3a8eb9SGleb Smirnoff 			$$->next = NULL;
23463b3a8eb9SGleb Smirnoff 			$$->tail = $$;
23473b3a8eb9SGleb Smirnoff 		}
23483b3a8eb9SGleb Smirnoff 		;
23493b3a8eb9SGleb Smirnoff 
23503b3a8eb9SGleb Smirnoff pfrule		: action dir logquick interface route af proto fromto
23513b3a8eb9SGleb Smirnoff 		    filter_opts
23523b3a8eb9SGleb Smirnoff 		{
2353e9eb0941SKristof Provost 			struct pfctl_rule	 r;
23543b3a8eb9SGleb Smirnoff 			struct node_state_opt	*o;
23553b3a8eb9SGleb Smirnoff 			struct node_proto	*proto;
23563b3a8eb9SGleb Smirnoff 			int			 srctrack = 0;
23573b3a8eb9SGleb Smirnoff 			int			 statelock = 0;
23583b3a8eb9SGleb Smirnoff 			int			 adaptive = 0;
23593b3a8eb9SGleb Smirnoff 			int			 defaults = 0;
23603b3a8eb9SGleb Smirnoff 
23613b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_FILTER))
23623b3a8eb9SGleb Smirnoff 				YYERROR;
23633b3a8eb9SGleb Smirnoff 
23643b3a8eb9SGleb Smirnoff 			memset(&r, 0, sizeof(r));
23653b3a8eb9SGleb Smirnoff 
23663b3a8eb9SGleb Smirnoff 			r.action = $1.b1;
23673b3a8eb9SGleb Smirnoff 			switch ($1.b2) {
23683b3a8eb9SGleb Smirnoff 			case PFRULE_RETURNRST:
23693b3a8eb9SGleb Smirnoff 				r.rule_flag |= PFRULE_RETURNRST;
23703b3a8eb9SGleb Smirnoff 				r.return_ttl = $1.w;
23713b3a8eb9SGleb Smirnoff 				break;
23723b3a8eb9SGleb Smirnoff 			case PFRULE_RETURNICMP:
23733b3a8eb9SGleb Smirnoff 				r.rule_flag |= PFRULE_RETURNICMP;
23743b3a8eb9SGleb Smirnoff 				r.return_icmp = $1.w;
23753b3a8eb9SGleb Smirnoff 				r.return_icmp6 = $1.w2;
23763b3a8eb9SGleb Smirnoff 				break;
23773b3a8eb9SGleb Smirnoff 			case PFRULE_RETURN:
23783b3a8eb9SGleb Smirnoff 				r.rule_flag |= PFRULE_RETURN;
23793b3a8eb9SGleb Smirnoff 				r.return_icmp = $1.w;
23803b3a8eb9SGleb Smirnoff 				r.return_icmp6 = $1.w2;
23813b3a8eb9SGleb Smirnoff 				break;
23823b3a8eb9SGleb Smirnoff 			}
23833b3a8eb9SGleb Smirnoff 			r.direction = $2;
23843b3a8eb9SGleb Smirnoff 			r.log = $3.log;
23853b3a8eb9SGleb Smirnoff 			r.logif = $3.logif;
23863b3a8eb9SGleb Smirnoff 			r.quick = $3.quick;
23873b3a8eb9SGleb Smirnoff 			r.prob = $9.prob;
23883b3a8eb9SGleb Smirnoff 			r.rtableid = $9.rtableid;
23893b3a8eb9SGleb Smirnoff 
239039282ef3SKajetan Staszkiewicz 			if ($9.nodf)
239139282ef3SKajetan Staszkiewicz 				r.scrub_flags |= PFSTATE_NODF;
239239282ef3SKajetan Staszkiewicz 			if ($9.randomid)
239339282ef3SKajetan Staszkiewicz 				r.scrub_flags |= PFSTATE_RANDOMID;
239439282ef3SKajetan Staszkiewicz 			if ($9.minttl)
239539282ef3SKajetan Staszkiewicz 				r.min_ttl = $9.minttl;
239639282ef3SKajetan Staszkiewicz 			if ($9.max_mss)
239739282ef3SKajetan Staszkiewicz 				r.max_mss = $9.max_mss;
239839282ef3SKajetan Staszkiewicz 			if ($9.marker & FOM_SETTOS) {
239939282ef3SKajetan Staszkiewicz 				r.scrub_flags |= PFSTATE_SETTOS;
240039282ef3SKajetan Staszkiewicz 				r.set_tos = $9.settos;
240139282ef3SKajetan Staszkiewicz 			}
240239282ef3SKajetan Staszkiewicz 			if ($9.marker & FOM_SCRUB_TCP)
240339282ef3SKajetan Staszkiewicz 				r.scrub_flags |= PFSTATE_SCRUB_TCP;
240439282ef3SKajetan Staszkiewicz 
24053e248e0fSKristof Provost 			if ($9.marker & FOM_PRIO) {
24063e248e0fSKristof Provost 				if ($9.prio == 0)
24073e248e0fSKristof Provost 					r.prio = PF_PRIO_ZERO;
24083e248e0fSKristof Provost 				else
24093e248e0fSKristof Provost 					r.prio = $9.prio;
24103e248e0fSKristof Provost 			}
24113e248e0fSKristof Provost 			if ($9.marker & FOM_SETPRIO) {
24123e248e0fSKristof Provost 				r.set_prio[0] = $9.set_prio[0];
24133e248e0fSKristof Provost 				r.set_prio[1] = $9.set_prio[1];
24143e248e0fSKristof Provost 				r.scrub_flags |= PFSTATE_SETPRIO;
24153e248e0fSKristof Provost 			}
24163e248e0fSKristof Provost 
24173b3a8eb9SGleb Smirnoff 			r.af = $6;
24183b3a8eb9SGleb Smirnoff 			if ($9.tag)
24193b3a8eb9SGleb Smirnoff 				if (strlcpy(r.tagname, $9.tag,
24203b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
24213b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
24223b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
24233b3a8eb9SGleb Smirnoff 					YYERROR;
24243b3a8eb9SGleb Smirnoff 				}
24253b3a8eb9SGleb Smirnoff 			if ($9.match_tag)
24263b3a8eb9SGleb Smirnoff 				if (strlcpy(r.match_tagname, $9.match_tag,
24273b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
24283b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
24293b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
24303b3a8eb9SGleb Smirnoff 					YYERROR;
24313b3a8eb9SGleb Smirnoff 				}
24323b3a8eb9SGleb Smirnoff 			r.match_tag_not = $9.match_tag_not;
24333b3a8eb9SGleb Smirnoff 			if (rule_label(&r, $9.label))
24343b3a8eb9SGleb Smirnoff 				YYERROR;
24356fcc8e04SKristof Provost 			for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++)
24366fcc8e04SKristof Provost 				free($9.label[i]);
243776c5eeccSKristof Provost 			r.ridentifier = $9.ridentifier;
24383b3a8eb9SGleb Smirnoff 			r.flags = $9.flags.b1;
24393b3a8eb9SGleb Smirnoff 			r.flagset = $9.flags.b2;
24403b3a8eb9SGleb Smirnoff 			if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
24413b3a8eb9SGleb Smirnoff 				yyerror("flags always false");
24423b3a8eb9SGleb Smirnoff 				YYERROR;
24433b3a8eb9SGleb Smirnoff 			}
24443b3a8eb9SGleb Smirnoff 			if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
24453b3a8eb9SGleb Smirnoff 				for (proto = $7; proto != NULL &&
24463b3a8eb9SGleb Smirnoff 				    proto->proto != IPPROTO_TCP;
24473b3a8eb9SGleb Smirnoff 				    proto = proto->next)
24483b3a8eb9SGleb Smirnoff 					;	/* nothing */
24493b3a8eb9SGleb Smirnoff 				if (proto == NULL && $7 != NULL) {
24503b3a8eb9SGleb Smirnoff 					if ($9.flags.b1 || $9.flags.b2)
24513b3a8eb9SGleb Smirnoff 						yyerror(
24523b3a8eb9SGleb Smirnoff 						    "flags only apply to tcp");
24533b3a8eb9SGleb Smirnoff 					if ($8.src_os)
24543b3a8eb9SGleb Smirnoff 						yyerror(
24553b3a8eb9SGleb Smirnoff 						    "OS fingerprinting only "
24563b3a8eb9SGleb Smirnoff 						    "apply to tcp");
24573b3a8eb9SGleb Smirnoff 					YYERROR;
24583b3a8eb9SGleb Smirnoff 				}
24593b3a8eb9SGleb Smirnoff #if 0
24603b3a8eb9SGleb Smirnoff 				if (($9.flags.b1 & parse_flags("S")) == 0 &&
24613b3a8eb9SGleb Smirnoff 				    $8.src_os) {
24623b3a8eb9SGleb Smirnoff 					yyerror("OS fingerprinting requires "
24633b3a8eb9SGleb Smirnoff 					    "the SYN TCP flag (flags S/SA)");
24643b3a8eb9SGleb Smirnoff 					YYERROR;
24653b3a8eb9SGleb Smirnoff 				}
24663b3a8eb9SGleb Smirnoff #endif
24673b3a8eb9SGleb Smirnoff 			}
24683b3a8eb9SGleb Smirnoff 
24693b3a8eb9SGleb Smirnoff 			r.tos = $9.tos;
24703b3a8eb9SGleb Smirnoff 			r.keep_state = $9.keep.action;
24713b3a8eb9SGleb Smirnoff 			o = $9.keep.options;
24723b3a8eb9SGleb Smirnoff 
24733b3a8eb9SGleb Smirnoff 			/* 'keep state' by default on pass rules. */
24743b3a8eb9SGleb Smirnoff 			if (!r.keep_state && !r.action &&
24753b3a8eb9SGleb Smirnoff 			    !($9.marker & FOM_KEEP)) {
24763b3a8eb9SGleb Smirnoff 				r.keep_state = PF_STATE_NORMAL;
24773b3a8eb9SGleb Smirnoff 				o = keep_state_defaults;
24783b3a8eb9SGleb Smirnoff 				defaults = 1;
24793b3a8eb9SGleb Smirnoff 			}
24803b3a8eb9SGleb Smirnoff 
24813b3a8eb9SGleb Smirnoff 			while (o) {
24823b3a8eb9SGleb Smirnoff 				struct node_state_opt	*p = o;
24833b3a8eb9SGleb Smirnoff 
24843b3a8eb9SGleb Smirnoff 				switch (o->type) {
24853b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_MAX:
24863b3a8eb9SGleb Smirnoff 					if (r.max_states) {
24873b3a8eb9SGleb Smirnoff 						yyerror("state option 'max' "
24883b3a8eb9SGleb Smirnoff 						    "multiple definitions");
24893b3a8eb9SGleb Smirnoff 						YYERROR;
24903b3a8eb9SGleb Smirnoff 					}
24913b3a8eb9SGleb Smirnoff 					r.max_states = o->data.max_states;
24923b3a8eb9SGleb Smirnoff 					break;
24933b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_NOSYNC:
24943b3a8eb9SGleb Smirnoff 					if (r.rule_flag & PFRULE_NOSYNC) {
24953b3a8eb9SGleb Smirnoff 						yyerror("state option 'sync' "
24963b3a8eb9SGleb Smirnoff 						    "multiple definitions");
24973b3a8eb9SGleb Smirnoff 						YYERROR;
24983b3a8eb9SGleb Smirnoff 					}
24993b3a8eb9SGleb Smirnoff 					r.rule_flag |= PFRULE_NOSYNC;
25003b3a8eb9SGleb Smirnoff 					break;
25013b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_SRCTRACK:
25023b3a8eb9SGleb Smirnoff 					if (srctrack) {
25033b3a8eb9SGleb Smirnoff 						yyerror("state option "
25043b3a8eb9SGleb Smirnoff 						    "'source-track' "
25053b3a8eb9SGleb Smirnoff 						    "multiple definitions");
25063b3a8eb9SGleb Smirnoff 						YYERROR;
25073b3a8eb9SGleb Smirnoff 					}
25083b3a8eb9SGleb Smirnoff 					srctrack =  o->data.src_track;
25093b3a8eb9SGleb Smirnoff 					r.rule_flag |= PFRULE_SRCTRACK;
25103b3a8eb9SGleb Smirnoff 					break;
25113b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_MAX_SRC_STATES:
25123b3a8eb9SGleb Smirnoff 					if (r.max_src_states) {
25133b3a8eb9SGleb Smirnoff 						yyerror("state option "
25143b3a8eb9SGleb Smirnoff 						    "'max-src-states' "
25153b3a8eb9SGleb Smirnoff 						    "multiple definitions");
25163b3a8eb9SGleb Smirnoff 						YYERROR;
25173b3a8eb9SGleb Smirnoff 					}
25183b3a8eb9SGleb Smirnoff 					if (o->data.max_src_states == 0) {
25193b3a8eb9SGleb Smirnoff 						yyerror("'max-src-states' must "
25203b3a8eb9SGleb Smirnoff 						    "be > 0");
25213b3a8eb9SGleb Smirnoff 						YYERROR;
25223b3a8eb9SGleb Smirnoff 					}
25233b3a8eb9SGleb Smirnoff 					r.max_src_states =
25243b3a8eb9SGleb Smirnoff 					    o->data.max_src_states;
25253b3a8eb9SGleb Smirnoff 					r.rule_flag |= PFRULE_SRCTRACK;
25263b3a8eb9SGleb Smirnoff 					break;
25273b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_OVERLOAD:
25283b3a8eb9SGleb Smirnoff 					if (r.overload_tblname[0]) {
25293b3a8eb9SGleb Smirnoff 						yyerror("multiple 'overload' "
25303b3a8eb9SGleb Smirnoff 						    "table definitions");
25313b3a8eb9SGleb Smirnoff 						YYERROR;
25323b3a8eb9SGleb Smirnoff 					}
25333b3a8eb9SGleb Smirnoff 					if (strlcpy(r.overload_tblname,
25343b3a8eb9SGleb Smirnoff 					    o->data.overload.tblname,
25353b3a8eb9SGleb Smirnoff 					    PF_TABLE_NAME_SIZE) >=
25363b3a8eb9SGleb Smirnoff 					    PF_TABLE_NAME_SIZE) {
25373b3a8eb9SGleb Smirnoff 						yyerror("state option: "
25383b3a8eb9SGleb Smirnoff 						    "strlcpy");
25393b3a8eb9SGleb Smirnoff 						YYERROR;
25403b3a8eb9SGleb Smirnoff 					}
25413b3a8eb9SGleb Smirnoff 					r.flush = o->data.overload.flush;
25423b3a8eb9SGleb Smirnoff 					break;
25433b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_MAX_SRC_CONN:
25443b3a8eb9SGleb Smirnoff 					if (r.max_src_conn) {
25453b3a8eb9SGleb Smirnoff 						yyerror("state option "
25463b3a8eb9SGleb Smirnoff 						    "'max-src-conn' "
25473b3a8eb9SGleb Smirnoff 						    "multiple definitions");
25483b3a8eb9SGleb Smirnoff 						YYERROR;
25493b3a8eb9SGleb Smirnoff 					}
25503b3a8eb9SGleb Smirnoff 					if (o->data.max_src_conn == 0) {
25513b3a8eb9SGleb Smirnoff 						yyerror("'max-src-conn' "
25523b3a8eb9SGleb Smirnoff 						    "must be > 0");
25533b3a8eb9SGleb Smirnoff 						YYERROR;
25543b3a8eb9SGleb Smirnoff 					}
25553b3a8eb9SGleb Smirnoff 					r.max_src_conn =
25563b3a8eb9SGleb Smirnoff 					    o->data.max_src_conn;
25573b3a8eb9SGleb Smirnoff 					r.rule_flag |= PFRULE_SRCTRACK |
25583b3a8eb9SGleb Smirnoff 					    PFRULE_RULESRCTRACK;
25593b3a8eb9SGleb Smirnoff 					break;
25603b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_MAX_SRC_CONN_RATE:
25613b3a8eb9SGleb Smirnoff 					if (r.max_src_conn_rate.limit) {
25623b3a8eb9SGleb Smirnoff 						yyerror("state option "
25633b3a8eb9SGleb Smirnoff 						    "'max-src-conn-rate' "
25643b3a8eb9SGleb Smirnoff 						    "multiple definitions");
25653b3a8eb9SGleb Smirnoff 						YYERROR;
25663b3a8eb9SGleb Smirnoff 					}
25673b3a8eb9SGleb Smirnoff 					if (!o->data.max_src_conn_rate.limit ||
25683b3a8eb9SGleb Smirnoff 					    !o->data.max_src_conn_rate.seconds) {
25693b3a8eb9SGleb Smirnoff 						yyerror("'max-src-conn-rate' "
25703b3a8eb9SGleb Smirnoff 						    "values must be > 0");
25713b3a8eb9SGleb Smirnoff 						YYERROR;
25723b3a8eb9SGleb Smirnoff 					}
25733b3a8eb9SGleb Smirnoff 					if (o->data.max_src_conn_rate.limit >
25743b3a8eb9SGleb Smirnoff 					    PF_THRESHOLD_MAX) {
25753b3a8eb9SGleb Smirnoff 						yyerror("'max-src-conn-rate' "
25763b3a8eb9SGleb Smirnoff 						    "maximum rate must be < %u",
25773b3a8eb9SGleb Smirnoff 						    PF_THRESHOLD_MAX);
25783b3a8eb9SGleb Smirnoff 						YYERROR;
25793b3a8eb9SGleb Smirnoff 					}
25803b3a8eb9SGleb Smirnoff 					r.max_src_conn_rate.limit =
25813b3a8eb9SGleb Smirnoff 					    o->data.max_src_conn_rate.limit;
25823b3a8eb9SGleb Smirnoff 					r.max_src_conn_rate.seconds =
25833b3a8eb9SGleb Smirnoff 					    o->data.max_src_conn_rate.seconds;
25843b3a8eb9SGleb Smirnoff 					r.rule_flag |= PFRULE_SRCTRACK |
25853b3a8eb9SGleb Smirnoff 					    PFRULE_RULESRCTRACK;
25863b3a8eb9SGleb Smirnoff 					break;
25873b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_MAX_SRC_NODES:
25883b3a8eb9SGleb Smirnoff 					if (r.max_src_nodes) {
25893b3a8eb9SGleb Smirnoff 						yyerror("state option "
25903b3a8eb9SGleb Smirnoff 						    "'max-src-nodes' "
25913b3a8eb9SGleb Smirnoff 						    "multiple definitions");
25923b3a8eb9SGleb Smirnoff 						YYERROR;
25933b3a8eb9SGleb Smirnoff 					}
25943b3a8eb9SGleb Smirnoff 					if (o->data.max_src_nodes == 0) {
25953b3a8eb9SGleb Smirnoff 						yyerror("'max-src-nodes' must "
25963b3a8eb9SGleb Smirnoff 						    "be > 0");
25973b3a8eb9SGleb Smirnoff 						YYERROR;
25983b3a8eb9SGleb Smirnoff 					}
25993b3a8eb9SGleb Smirnoff 					r.max_src_nodes =
26003b3a8eb9SGleb Smirnoff 					    o->data.max_src_nodes;
26013b3a8eb9SGleb Smirnoff 					r.rule_flag |= PFRULE_SRCTRACK |
26023b3a8eb9SGleb Smirnoff 					    PFRULE_RULESRCTRACK;
26033b3a8eb9SGleb Smirnoff 					break;
26043b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_STATELOCK:
26053b3a8eb9SGleb Smirnoff 					if (statelock) {
26063b3a8eb9SGleb Smirnoff 						yyerror("state locking option: "
26073b3a8eb9SGleb Smirnoff 						    "multiple definitions");
26083b3a8eb9SGleb Smirnoff 						YYERROR;
26093b3a8eb9SGleb Smirnoff 					}
26103b3a8eb9SGleb Smirnoff 					statelock = 1;
26113b3a8eb9SGleb Smirnoff 					r.rule_flag |= o->data.statelock;
26123b3a8eb9SGleb Smirnoff 					break;
26133b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_SLOPPY:
26143b3a8eb9SGleb Smirnoff 					if (r.rule_flag & PFRULE_STATESLOPPY) {
26153b3a8eb9SGleb Smirnoff 						yyerror("state sloppy option: "
26163b3a8eb9SGleb Smirnoff 						    "multiple definitions");
26173b3a8eb9SGleb Smirnoff 						YYERROR;
26183b3a8eb9SGleb Smirnoff 					}
26193b3a8eb9SGleb Smirnoff 					r.rule_flag |= PFRULE_STATESLOPPY;
26203b3a8eb9SGleb Smirnoff 					break;
2621baf9b6d0SKristof Provost 				case PF_STATE_OPT_PFLOW:
2622baf9b6d0SKristof Provost 					if (r.rule_flag & PFRULE_PFLOW) {
2623baf9b6d0SKristof Provost 						yyerror("state pflow option: "
2624baf9b6d0SKristof Provost 						    "multiple definitions");
2625baf9b6d0SKristof Provost 						YYERROR;
2626baf9b6d0SKristof Provost 					}
2627baf9b6d0SKristof Provost 					r.rule_flag |= PFRULE_PFLOW;
2628baf9b6d0SKristof Provost 					break;
26293b3a8eb9SGleb Smirnoff 				case PF_STATE_OPT_TIMEOUT:
26303b3a8eb9SGleb Smirnoff 					if (o->data.timeout.number ==
26313b3a8eb9SGleb Smirnoff 					    PFTM_ADAPTIVE_START ||
26323b3a8eb9SGleb Smirnoff 					    o->data.timeout.number ==
26333b3a8eb9SGleb Smirnoff 					    PFTM_ADAPTIVE_END)
26343b3a8eb9SGleb Smirnoff 						adaptive = 1;
26353b3a8eb9SGleb Smirnoff 					if (r.timeout[o->data.timeout.number]) {
26363b3a8eb9SGleb Smirnoff 						yyerror("state timeout %s "
26373b3a8eb9SGleb Smirnoff 						    "multiple definitions",
26383b3a8eb9SGleb Smirnoff 						    pf_timeouts[o->data.
26393b3a8eb9SGleb Smirnoff 						    timeout.number].name);
26403b3a8eb9SGleb Smirnoff 						YYERROR;
26413b3a8eb9SGleb Smirnoff 					}
26423b3a8eb9SGleb Smirnoff 					r.timeout[o->data.timeout.number] =
26433b3a8eb9SGleb Smirnoff 					    o->data.timeout.seconds;
26443b3a8eb9SGleb Smirnoff 				}
26453b3a8eb9SGleb Smirnoff 				o = o->next;
26463b3a8eb9SGleb Smirnoff 				if (!defaults)
26473b3a8eb9SGleb Smirnoff 					free(p);
26483b3a8eb9SGleb Smirnoff 			}
26493b3a8eb9SGleb Smirnoff 
26503b3a8eb9SGleb Smirnoff 			/* 'flags S/SA' by default on stateful rules */
26513b3a8eb9SGleb Smirnoff 			if (!r.action && !r.flags && !r.flagset &&
26523b3a8eb9SGleb Smirnoff 			    !$9.fragment && !($9.marker & FOM_FLAGS) &&
26533b3a8eb9SGleb Smirnoff 			    r.keep_state) {
26543b3a8eb9SGleb Smirnoff 				r.flags = parse_flags("S");
26553b3a8eb9SGleb Smirnoff 				r.flagset =  parse_flags("SA");
26563b3a8eb9SGleb Smirnoff 			}
26573b3a8eb9SGleb Smirnoff 			if (!adaptive && r.max_states) {
26583b3a8eb9SGleb Smirnoff 				r.timeout[PFTM_ADAPTIVE_START] =
26593b3a8eb9SGleb Smirnoff 				    (r.max_states / 10) * 6;
26603b3a8eb9SGleb Smirnoff 				r.timeout[PFTM_ADAPTIVE_END] =
26613b3a8eb9SGleb Smirnoff 				    (r.max_states / 10) * 12;
26623b3a8eb9SGleb Smirnoff 			}
26633b3a8eb9SGleb Smirnoff 			if (r.rule_flag & PFRULE_SRCTRACK) {
26643b3a8eb9SGleb Smirnoff 				if (srctrack == PF_SRCTRACK_GLOBAL &&
26653b3a8eb9SGleb Smirnoff 				    r.max_src_nodes) {
26663b3a8eb9SGleb Smirnoff 					yyerror("'max-src-nodes' is "
26673b3a8eb9SGleb Smirnoff 					    "incompatible with "
26683b3a8eb9SGleb Smirnoff 					    "'source-track global'");
26693b3a8eb9SGleb Smirnoff 					YYERROR;
26703b3a8eb9SGleb Smirnoff 				}
26713b3a8eb9SGleb Smirnoff 				if (srctrack == PF_SRCTRACK_GLOBAL &&
26723b3a8eb9SGleb Smirnoff 				    r.max_src_conn) {
26733b3a8eb9SGleb Smirnoff 					yyerror("'max-src-conn' is "
26743b3a8eb9SGleb Smirnoff 					    "incompatible with "
26753b3a8eb9SGleb Smirnoff 					    "'source-track global'");
26763b3a8eb9SGleb Smirnoff 					YYERROR;
26773b3a8eb9SGleb Smirnoff 				}
26783b3a8eb9SGleb Smirnoff 				if (srctrack == PF_SRCTRACK_GLOBAL &&
26793b3a8eb9SGleb Smirnoff 				    r.max_src_conn_rate.seconds) {
26803b3a8eb9SGleb Smirnoff 					yyerror("'max-src-conn-rate' is "
26813b3a8eb9SGleb Smirnoff 					    "incompatible with "
26823b3a8eb9SGleb Smirnoff 					    "'source-track global'");
26833b3a8eb9SGleb Smirnoff 					YYERROR;
26843b3a8eb9SGleb Smirnoff 				}
26853b3a8eb9SGleb Smirnoff 				if (r.timeout[PFTM_SRC_NODE] <
26863b3a8eb9SGleb Smirnoff 				    r.max_src_conn_rate.seconds)
26873b3a8eb9SGleb Smirnoff 					r.timeout[PFTM_SRC_NODE] =
26883b3a8eb9SGleb Smirnoff 					    r.max_src_conn_rate.seconds;
26893b3a8eb9SGleb Smirnoff 				r.rule_flag |= PFRULE_SRCTRACK;
26903b3a8eb9SGleb Smirnoff 				if (srctrack == PF_SRCTRACK_RULE)
26913b3a8eb9SGleb Smirnoff 					r.rule_flag |= PFRULE_RULESRCTRACK;
26923b3a8eb9SGleb Smirnoff 			}
26933b3a8eb9SGleb Smirnoff 			if (r.keep_state && !statelock)
26943b3a8eb9SGleb Smirnoff 				r.rule_flag |= default_statelock;
26953b3a8eb9SGleb Smirnoff 
26963b3a8eb9SGleb Smirnoff 			if ($9.fragment)
26973b3a8eb9SGleb Smirnoff 				r.rule_flag |= PFRULE_FRAGMENT;
26983b3a8eb9SGleb Smirnoff 			r.allow_opts = $9.allowopts;
26993b3a8eb9SGleb Smirnoff 
27003b3a8eb9SGleb Smirnoff 			decide_address_family($8.src.host, &r.af);
27013b3a8eb9SGleb Smirnoff 			decide_address_family($8.dst.host, &r.af);
27023b3a8eb9SGleb Smirnoff 
27033b3a8eb9SGleb Smirnoff 			if ($5.rt) {
27043b3a8eb9SGleb Smirnoff 				if (!r.direction) {
27053b3a8eb9SGleb Smirnoff 					yyerror("direction must be explicit "
27063b3a8eb9SGleb Smirnoff 					    "with rules that specify routing");
27073b3a8eb9SGleb Smirnoff 					YYERROR;
27083b3a8eb9SGleb Smirnoff 				}
27093b3a8eb9SGleb Smirnoff 				r.rt = $5.rt;
27103b3a8eb9SGleb Smirnoff 				r.rpool.opts = $5.pool_opts;
27113b3a8eb9SGleb Smirnoff 				if ($5.key != NULL)
27123b3a8eb9SGleb Smirnoff 					memcpy(&r.rpool.key, $5.key,
27133b3a8eb9SGleb Smirnoff 					    sizeof(struct pf_poolhashkey));
27143b3a8eb9SGleb Smirnoff 			}
2715813196a1SKristof Provost 			if (r.rt) {
27163b3a8eb9SGleb Smirnoff 				decide_address_family($5.host, &r.af);
27173b3a8eb9SGleb Smirnoff 				remove_invalid_hosts(&$5.host, &r.af);
27183b3a8eb9SGleb Smirnoff 				if ($5.host == NULL) {
27193b3a8eb9SGleb Smirnoff 					yyerror("no routing address with "
27203b3a8eb9SGleb Smirnoff 					    "matching address family found.");
27213b3a8eb9SGleb Smirnoff 					YYERROR;
27223b3a8eb9SGleb Smirnoff 				}
27233b3a8eb9SGleb Smirnoff 				if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
27243b3a8eb9SGleb Smirnoff 				    PF_POOL_NONE && ($5.host->next != NULL ||
27253b3a8eb9SGleb Smirnoff 				    $5.host->addr.type == PF_ADDR_TABLE ||
27263b3a8eb9SGleb Smirnoff 				    DYNIF_MULTIADDR($5.host->addr)))
27273b3a8eb9SGleb Smirnoff 					r.rpool.opts |= PF_POOL_ROUNDROBIN;
27283b3a8eb9SGleb Smirnoff 				if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
27293b3a8eb9SGleb Smirnoff 				    PF_POOL_ROUNDROBIN &&
27303b3a8eb9SGleb Smirnoff 				    disallow_table($5.host, "tables are only "
27313b3a8eb9SGleb Smirnoff 				    "supported in round-robin routing pools"))
27323b3a8eb9SGleb Smirnoff 					YYERROR;
27333b3a8eb9SGleb Smirnoff 				if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
27343b3a8eb9SGleb Smirnoff 				    PF_POOL_ROUNDROBIN &&
27353b3a8eb9SGleb Smirnoff 				    disallow_alias($5.host, "interface (%s) "
27363b3a8eb9SGleb Smirnoff 				    "is only supported in round-robin "
27373b3a8eb9SGleb Smirnoff 				    "routing pools"))
27383b3a8eb9SGleb Smirnoff 					YYERROR;
27393b3a8eb9SGleb Smirnoff 				if ($5.host->next != NULL) {
27403b3a8eb9SGleb Smirnoff 					if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
27413b3a8eb9SGleb Smirnoff 					    PF_POOL_ROUNDROBIN) {
27423b3a8eb9SGleb Smirnoff 						yyerror("r.rpool.opts must "
27433b3a8eb9SGleb Smirnoff 						    "be PF_POOL_ROUNDROBIN");
27443b3a8eb9SGleb Smirnoff 						YYERROR;
27453b3a8eb9SGleb Smirnoff 					}
27463b3a8eb9SGleb Smirnoff 				}
27473b3a8eb9SGleb Smirnoff 			}
27483b3a8eb9SGleb Smirnoff 			if ($9.queues.qname != NULL) {
27493b3a8eb9SGleb Smirnoff 				if (strlcpy(r.qname, $9.queues.qname,
27503b3a8eb9SGleb Smirnoff 				    sizeof(r.qname)) >= sizeof(r.qname)) {
27513b3a8eb9SGleb Smirnoff 					yyerror("rule qname too long (max "
27523b3a8eb9SGleb Smirnoff 					    "%d chars)", sizeof(r.qname)-1);
27533b3a8eb9SGleb Smirnoff 					YYERROR;
27543b3a8eb9SGleb Smirnoff 				}
27553b3a8eb9SGleb Smirnoff 				free($9.queues.qname);
27563b3a8eb9SGleb Smirnoff 			}
27573b3a8eb9SGleb Smirnoff 			if ($9.queues.pqname != NULL) {
27583b3a8eb9SGleb Smirnoff 				if (strlcpy(r.pqname, $9.queues.pqname,
27593b3a8eb9SGleb Smirnoff 				    sizeof(r.pqname)) >= sizeof(r.pqname)) {
27603b3a8eb9SGleb Smirnoff 					yyerror("rule pqname too long (max "
27613b3a8eb9SGleb Smirnoff 					    "%d chars)", sizeof(r.pqname)-1);
27623b3a8eb9SGleb Smirnoff 					YYERROR;
27633b3a8eb9SGleb Smirnoff 				}
27643b3a8eb9SGleb Smirnoff 				free($9.queues.pqname);
27653b3a8eb9SGleb Smirnoff 			}
27663b3a8eb9SGleb Smirnoff #ifdef __FreeBSD__
27673b3a8eb9SGleb Smirnoff 			r.divert.port = $9.divert.port;
27683b3a8eb9SGleb Smirnoff #else
27693b3a8eb9SGleb Smirnoff 			if ((r.divert.port = $9.divert.port)) {
27703b3a8eb9SGleb Smirnoff 				if (r.direction == PF_OUT) {
27713b3a8eb9SGleb Smirnoff 					if ($9.divert.addr) {
27723b3a8eb9SGleb Smirnoff 						yyerror("address specified "
27733b3a8eb9SGleb Smirnoff 						    "for outgoing divert");
27743b3a8eb9SGleb Smirnoff 						YYERROR;
27753b3a8eb9SGleb Smirnoff 					}
27763b3a8eb9SGleb Smirnoff 					bzero(&r.divert.addr,
27773b3a8eb9SGleb Smirnoff 					    sizeof(r.divert.addr));
27783b3a8eb9SGleb Smirnoff 				} else {
27793b3a8eb9SGleb Smirnoff 					if (!$9.divert.addr) {
27803b3a8eb9SGleb Smirnoff 						yyerror("no address specified "
27813b3a8eb9SGleb Smirnoff 						    "for incoming divert");
27823b3a8eb9SGleb Smirnoff 						YYERROR;
27833b3a8eb9SGleb Smirnoff 					}
27843b3a8eb9SGleb Smirnoff 					if ($9.divert.addr->af != r.af) {
27853b3a8eb9SGleb Smirnoff 						yyerror("address family "
27863b3a8eb9SGleb Smirnoff 						    "mismatch for divert");
27873b3a8eb9SGleb Smirnoff 						YYERROR;
27883b3a8eb9SGleb Smirnoff 					}
27893b3a8eb9SGleb Smirnoff 					r.divert.addr =
27903b3a8eb9SGleb Smirnoff 					    $9.divert.addr->addr.v.a.addr;
27913b3a8eb9SGleb Smirnoff 				}
27923b3a8eb9SGleb Smirnoff 			}
27933b3a8eb9SGleb Smirnoff #endif
27943b3a8eb9SGleb Smirnoff 
279563b3c1c7SKristof Provost 			if ($9.dnpipe || $9.dnrpipe) {
279663b3c1c7SKristof Provost 				r.dnpipe = $9.dnpipe;
279763b3c1c7SKristof Provost 				r.dnrpipe = $9.dnrpipe;
279863b3c1c7SKristof Provost 				if ($9.free_flags & PFRULE_DN_IS_PIPE)
279963b3c1c7SKristof Provost 					r.free_flags |= PFRULE_DN_IS_PIPE;
280063b3c1c7SKristof Provost 				else
280163b3c1c7SKristof Provost 					r.free_flags |= PFRULE_DN_IS_QUEUE;
280263b3c1c7SKristof Provost 			}
280363b3c1c7SKristof Provost 
28043b3a8eb9SGleb Smirnoff 			expand_rule(&r, $4, $5.host, $7, $8.src_os,
28053b3a8eb9SGleb Smirnoff 			    $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
28062339ead6SKristof Provost 			    $9.uid, $9.gid, $9.rcv, $9.icmpspec, "");
28073b3a8eb9SGleb Smirnoff 		}
28083b3a8eb9SGleb Smirnoff 		;
28093b3a8eb9SGleb Smirnoff 
28103b3a8eb9SGleb Smirnoff filter_opts	:	{
28113b3a8eb9SGleb Smirnoff 				bzero(&filter_opts, sizeof filter_opts);
28123b3a8eb9SGleb Smirnoff 				filter_opts.rtableid = -1;
28133b3a8eb9SGleb Smirnoff 			}
28143b3a8eb9SGleb Smirnoff 		    filter_opts_l
28153b3a8eb9SGleb Smirnoff 			{ $$ = filter_opts; }
28163b3a8eb9SGleb Smirnoff 		| /* empty */	{
28173b3a8eb9SGleb Smirnoff 			bzero(&filter_opts, sizeof filter_opts);
28183b3a8eb9SGleb Smirnoff 			filter_opts.rtableid = -1;
28193b3a8eb9SGleb Smirnoff 			$$ = filter_opts;
28203b3a8eb9SGleb Smirnoff 		}
28213b3a8eb9SGleb Smirnoff 		;
28223b3a8eb9SGleb Smirnoff 
28233b3a8eb9SGleb Smirnoff filter_opts_l	: filter_opts_l filter_opt
28243b3a8eb9SGleb Smirnoff 		| filter_opt
28253b3a8eb9SGleb Smirnoff 		;
28263b3a8eb9SGleb Smirnoff 
28273b3a8eb9SGleb Smirnoff filter_opt	: USER uids {
28283b3a8eb9SGleb Smirnoff 			if (filter_opts.uid)
28293b3a8eb9SGleb Smirnoff 				$2->tail->next = filter_opts.uid;
28303b3a8eb9SGleb Smirnoff 			filter_opts.uid = $2;
28313b3a8eb9SGleb Smirnoff 		}
28323b3a8eb9SGleb Smirnoff 		| GROUP gids {
28333b3a8eb9SGleb Smirnoff 			if (filter_opts.gid)
28343b3a8eb9SGleb Smirnoff 				$2->tail->next = filter_opts.gid;
28353b3a8eb9SGleb Smirnoff 			filter_opts.gid = $2;
28363b3a8eb9SGleb Smirnoff 		}
28373b3a8eb9SGleb Smirnoff 		| flags {
28383b3a8eb9SGleb Smirnoff 			if (filter_opts.marker & FOM_FLAGS) {
28393b3a8eb9SGleb Smirnoff 				yyerror("flags cannot be redefined");
28403b3a8eb9SGleb Smirnoff 				YYERROR;
28413b3a8eb9SGleb Smirnoff 			}
28423b3a8eb9SGleb Smirnoff 			filter_opts.marker |= FOM_FLAGS;
28433b3a8eb9SGleb Smirnoff 			filter_opts.flags.b1 |= $1.b1;
28443b3a8eb9SGleb Smirnoff 			filter_opts.flags.b2 |= $1.b2;
28453b3a8eb9SGleb Smirnoff 			filter_opts.flags.w |= $1.w;
28463b3a8eb9SGleb Smirnoff 			filter_opts.flags.w2 |= $1.w2;
28473b3a8eb9SGleb Smirnoff 		}
28483b3a8eb9SGleb Smirnoff 		| icmpspec {
28493b3a8eb9SGleb Smirnoff 			if (filter_opts.marker & FOM_ICMP) {
28503b3a8eb9SGleb Smirnoff 				yyerror("icmp-type cannot be redefined");
28513b3a8eb9SGleb Smirnoff 				YYERROR;
28523b3a8eb9SGleb Smirnoff 			}
28533b3a8eb9SGleb Smirnoff 			filter_opts.marker |= FOM_ICMP;
28543b3a8eb9SGleb Smirnoff 			filter_opts.icmpspec = $1;
28553b3a8eb9SGleb Smirnoff 		}
28563e248e0fSKristof Provost 		| PRIO NUMBER {
28573e248e0fSKristof Provost 			if (filter_opts.marker & FOM_PRIO) {
28583e248e0fSKristof Provost 				yyerror("prio cannot be redefined");
28593e248e0fSKristof Provost 				YYERROR;
28603e248e0fSKristof Provost 			}
28613e248e0fSKristof Provost 			if ($2 < 0 || $2 > PF_PRIO_MAX) {
28623e248e0fSKristof Provost 				yyerror("prio must be 0 - %u", PF_PRIO_MAX);
28633e248e0fSKristof Provost 				YYERROR;
28643e248e0fSKristof Provost 			}
28653e248e0fSKristof Provost 			filter_opts.marker |= FOM_PRIO;
28663e248e0fSKristof Provost 			filter_opts.prio = $2;
28673e248e0fSKristof Provost 		}
28683b3a8eb9SGleb Smirnoff 		| TOS tos {
28693b3a8eb9SGleb Smirnoff 			if (filter_opts.marker & FOM_TOS) {
28703b3a8eb9SGleb Smirnoff 				yyerror("tos cannot be redefined");
28713b3a8eb9SGleb Smirnoff 				YYERROR;
28723b3a8eb9SGleb Smirnoff 			}
28733b3a8eb9SGleb Smirnoff 			filter_opts.marker |= FOM_TOS;
28743b3a8eb9SGleb Smirnoff 			filter_opts.tos = $2;
28753b3a8eb9SGleb Smirnoff 		}
28763b3a8eb9SGleb Smirnoff 		| keep {
28773b3a8eb9SGleb Smirnoff 			if (filter_opts.marker & FOM_KEEP) {
28783b3a8eb9SGleb Smirnoff 				yyerror("modulate or keep cannot be redefined");
28793b3a8eb9SGleb Smirnoff 				YYERROR;
28803b3a8eb9SGleb Smirnoff 			}
28813b3a8eb9SGleb Smirnoff 			filter_opts.marker |= FOM_KEEP;
28823b3a8eb9SGleb Smirnoff 			filter_opts.keep.action = $1.action;
28833b3a8eb9SGleb Smirnoff 			filter_opts.keep.options = $1.options;
28843b3a8eb9SGleb Smirnoff 		}
288576c5eeccSKristof Provost 		| RIDENTIFIER number {
288676c5eeccSKristof Provost 			filter_opts.ridentifier = $2;
288776c5eeccSKristof Provost 		}
28883b3a8eb9SGleb Smirnoff 		| FRAGMENT {
28893b3a8eb9SGleb Smirnoff 			filter_opts.fragment = 1;
28903b3a8eb9SGleb Smirnoff 		}
28913b3a8eb9SGleb Smirnoff 		| ALLOWOPTS {
28923b3a8eb9SGleb Smirnoff 			filter_opts.allowopts = 1;
28933b3a8eb9SGleb Smirnoff 		}
28943b3a8eb9SGleb Smirnoff 		| label	{
28956fcc8e04SKristof Provost 			if (filter_opts.labelcount >= PF_RULE_MAX_LABEL_COUNT) {
28966fcc8e04SKristof Provost 				yyerror("label can only be used %d times", PF_RULE_MAX_LABEL_COUNT);
28973b3a8eb9SGleb Smirnoff 				YYERROR;
28983b3a8eb9SGleb Smirnoff 			}
28996fcc8e04SKristof Provost 			filter_opts.label[filter_opts.labelcount++] = $1;
29003b3a8eb9SGleb Smirnoff 		}
29013b3a8eb9SGleb Smirnoff 		| qname	{
29023b3a8eb9SGleb Smirnoff 			if (filter_opts.queues.qname) {
29033b3a8eb9SGleb Smirnoff 				yyerror("queue cannot be redefined");
29043b3a8eb9SGleb Smirnoff 				YYERROR;
29053b3a8eb9SGleb Smirnoff 			}
29063b3a8eb9SGleb Smirnoff 			filter_opts.queues = $1;
29073b3a8eb9SGleb Smirnoff 		}
290863b3c1c7SKristof Provost 		| DNPIPE number {
290963b3c1c7SKristof Provost 			filter_opts.dnpipe = $2;
291063b3c1c7SKristof Provost 			filter_opts.free_flags |= PFRULE_DN_IS_PIPE;
291163b3c1c7SKristof Provost 		}
291263b3c1c7SKristof Provost 		| DNPIPE '(' number ')' {
291363b3c1c7SKristof Provost 			filter_opts.dnpipe = $3;
291463b3c1c7SKristof Provost 			filter_opts.free_flags |= PFRULE_DN_IS_PIPE;
291563b3c1c7SKristof Provost 		}
291663b3c1c7SKristof Provost 		| DNPIPE '(' number comma number ')' {
291763b3c1c7SKristof Provost 			filter_opts.dnrpipe = $5;
291863b3c1c7SKristof Provost 			filter_opts.dnpipe = $3;
291963b3c1c7SKristof Provost 			filter_opts.free_flags |= PFRULE_DN_IS_PIPE;
292063b3c1c7SKristof Provost 		}
292163b3c1c7SKristof Provost 		| DNQUEUE number {
292263b3c1c7SKristof Provost 			filter_opts.dnpipe = $2;
292363b3c1c7SKristof Provost 			filter_opts.free_flags |= PFRULE_DN_IS_QUEUE;
292463b3c1c7SKristof Provost 		}
292563b3c1c7SKristof Provost 		| DNQUEUE '(' number comma number ')' {
292663b3c1c7SKristof Provost 			filter_opts.dnrpipe = $5;
292763b3c1c7SKristof Provost 			filter_opts.dnpipe = $3;
292863b3c1c7SKristof Provost 			filter_opts.free_flags |= PFRULE_DN_IS_QUEUE;
292963b3c1c7SKristof Provost 		}
293063b3c1c7SKristof Provost 		| DNQUEUE '(' number ')' {
293163b3c1c7SKristof Provost 			filter_opts.dnpipe = $3;
293263b3c1c7SKristof Provost 			filter_opts.free_flags |= PFRULE_DN_IS_QUEUE;
293363b3c1c7SKristof Provost 		}
29343b3a8eb9SGleb Smirnoff 		| TAG string				{
29353b3a8eb9SGleb Smirnoff 			filter_opts.tag = $2;
29363b3a8eb9SGleb Smirnoff 		}
29373b3a8eb9SGleb Smirnoff 		| not TAGGED string			{
29383b3a8eb9SGleb Smirnoff 			filter_opts.match_tag = $3;
29393b3a8eb9SGleb Smirnoff 			filter_opts.match_tag_not = $1;
29403b3a8eb9SGleb Smirnoff 		}
29412339ead6SKristof Provost 		| RECEIVEDON if_item {
29422339ead6SKristof Provost 			if (filter_opts.rcv) {
29432339ead6SKristof Provost 				yyerror("cannot respecify received-on");
29442339ead6SKristof Provost 				YYERROR;
29452339ead6SKristof Provost 			}
29462339ead6SKristof Provost 			filter_opts.rcv = $2;
29472339ead6SKristof Provost 		}
29483b3a8eb9SGleb Smirnoff 		| PROBABILITY probability		{
29493b3a8eb9SGleb Smirnoff 			double	p;
29503b3a8eb9SGleb Smirnoff 
29513b3a8eb9SGleb Smirnoff 			p = floor($2 * UINT_MAX + 0.5);
29523b3a8eb9SGleb Smirnoff 			if (p < 0.0 || p > UINT_MAX) {
29533b3a8eb9SGleb Smirnoff 				yyerror("invalid probability: %lf", p);
29543b3a8eb9SGleb Smirnoff 				YYERROR;
29553b3a8eb9SGleb Smirnoff 			}
29563b3a8eb9SGleb Smirnoff 			filter_opts.prob = (u_int32_t)p;
29573b3a8eb9SGleb Smirnoff 			if (filter_opts.prob == 0)
29583b3a8eb9SGleb Smirnoff 				filter_opts.prob = 1;
29593b3a8eb9SGleb Smirnoff 		}
29603b3a8eb9SGleb Smirnoff 		| RTABLE NUMBER				{
29613b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > rt_tableid_max()) {
29623b3a8eb9SGleb Smirnoff 				yyerror("invalid rtable id");
29633b3a8eb9SGleb Smirnoff 				YYERROR;
29643b3a8eb9SGleb Smirnoff 			}
29653b3a8eb9SGleb Smirnoff 			filter_opts.rtableid = $2;
29663b3a8eb9SGleb Smirnoff 		}
29673b3a8eb9SGleb Smirnoff 		| DIVERTTO portplain {
29683b3a8eb9SGleb Smirnoff #ifdef __FreeBSD__
29693b3a8eb9SGleb Smirnoff 			filter_opts.divert.port = $2.a;
29703b3a8eb9SGleb Smirnoff 			if (!filter_opts.divert.port) {
29713b3a8eb9SGleb Smirnoff 				yyerror("invalid divert port: %u", ntohs($2.a));
29723b3a8eb9SGleb Smirnoff 				YYERROR;
29733b3a8eb9SGleb Smirnoff 			}
29743b3a8eb9SGleb Smirnoff #endif
29753b3a8eb9SGleb Smirnoff 		}
29763b3a8eb9SGleb Smirnoff 		| DIVERTTO STRING PORT portplain {
29773b3a8eb9SGleb Smirnoff #ifndef __FreeBSD__
29783b3a8eb9SGleb Smirnoff 			if ((filter_opts.divert.addr = host($2)) == NULL) {
29793b3a8eb9SGleb Smirnoff 				yyerror("could not parse divert address: %s",
29803b3a8eb9SGleb Smirnoff 				    $2);
29813b3a8eb9SGleb Smirnoff 				free($2);
29823b3a8eb9SGleb Smirnoff 				YYERROR;
29833b3a8eb9SGleb Smirnoff 			}
29843b3a8eb9SGleb Smirnoff #else
29853b3a8eb9SGleb Smirnoff 			if ($2)
29863b3a8eb9SGleb Smirnoff #endif
29873b3a8eb9SGleb Smirnoff 			free($2);
29883b3a8eb9SGleb Smirnoff 			filter_opts.divert.port = $4.a;
29893b3a8eb9SGleb Smirnoff 			if (!filter_opts.divert.port) {
29903b3a8eb9SGleb Smirnoff 				yyerror("invalid divert port: %u", ntohs($4.a));
29913b3a8eb9SGleb Smirnoff 				YYERROR;
29923b3a8eb9SGleb Smirnoff 			}
29933b3a8eb9SGleb Smirnoff 		}
29943b3a8eb9SGleb Smirnoff 		| DIVERTREPLY {
29953b3a8eb9SGleb Smirnoff #ifdef __FreeBSD__
29963b3a8eb9SGleb Smirnoff 			yyerror("divert-reply has no meaning in FreeBSD pf(4)");
29973b3a8eb9SGleb Smirnoff 			YYERROR;
29983b3a8eb9SGleb Smirnoff #else
29993b3a8eb9SGleb Smirnoff 			filter_opts.divert.port = 1;	/* some random value */
30003b3a8eb9SGleb Smirnoff #endif
30013b3a8eb9SGleb Smirnoff 		}
300239282ef3SKajetan Staszkiewicz 		| SCRUB '(' scrub_opts ')' {
300339282ef3SKajetan Staszkiewicz 			filter_opts.nodf = $3.nodf;
300439282ef3SKajetan Staszkiewicz 			filter_opts.minttl = $3.minttl;
300539282ef3SKajetan Staszkiewicz 			if ($3.marker & FOM_SETTOS) {
300639282ef3SKajetan Staszkiewicz 				/* Old style rules are "scrub set-tos 0x42"
300739282ef3SKajetan Staszkiewicz 				 * New style are "set tos 0x42 scrub (...)"
300839282ef3SKajetan Staszkiewicz 				 * What is in "scrub(...)"" is unfortunately the
300939282ef3SKajetan Staszkiewicz 				 * original scrub syntax so it would overwrite
301039282ef3SKajetan Staszkiewicz 				 * "set tos" of a pass/match rule.
301139282ef3SKajetan Staszkiewicz 				 */
301239282ef3SKajetan Staszkiewicz 				filter_opts.settos = $3.settos;
301339282ef3SKajetan Staszkiewicz 			}
301439282ef3SKajetan Staszkiewicz 			filter_opts.randomid = $3.randomid;
301539282ef3SKajetan Staszkiewicz 			filter_opts.max_mss = $3.maxmss;
301639282ef3SKajetan Staszkiewicz 			if ($3.reassemble_tcp)
301739282ef3SKajetan Staszkiewicz 				filter_opts.marker |= FOM_SCRUB_TCP;
301839282ef3SKajetan Staszkiewicz 			filter_opts.marker |= $3.marker;
301939282ef3SKajetan Staszkiewicz 		}
30203e248e0fSKristof Provost 		| filter_sets
30213e248e0fSKristof Provost 		;
30223e248e0fSKristof Provost 
30233e248e0fSKristof Provost filter_sets	: SET '(' filter_sets_l ')'	{ $$ = filter_opts; }
30243e248e0fSKristof Provost 		| SET filter_set		{ $$ = filter_opts; }
30253e248e0fSKristof Provost 		;
30263e248e0fSKristof Provost 
30273e248e0fSKristof Provost filter_sets_l	: filter_sets_l comma filter_set
30283e248e0fSKristof Provost 		| filter_set
30293e248e0fSKristof Provost 		;
30303e248e0fSKristof Provost 
30313e248e0fSKristof Provost filter_set	: prio {
30323e248e0fSKristof Provost 			if (filter_opts.marker & FOM_SETPRIO) {
30333e248e0fSKristof Provost 				yyerror("prio cannot be redefined");
30343e248e0fSKristof Provost 				YYERROR;
30353e248e0fSKristof Provost 			}
30363e248e0fSKristof Provost 			filter_opts.marker |= FOM_SETPRIO;
30373e248e0fSKristof Provost 			filter_opts.set_prio[0] = $1.b1;
30383e248e0fSKristof Provost 			filter_opts.set_prio[1] = $1.b2;
30393e248e0fSKristof Provost 		}
304039282ef3SKajetan Staszkiewicz 		| TOS tos {
304139282ef3SKajetan Staszkiewicz 			if (filter_opts.marker & FOM_SETTOS) {
304239282ef3SKajetan Staszkiewicz 				yyerror("tos cannot be respecified");
304339282ef3SKajetan Staszkiewicz 				YYERROR;
304439282ef3SKajetan Staszkiewicz 			}
304539282ef3SKajetan Staszkiewicz 			filter_opts.marker |= FOM_SETTOS;
304639282ef3SKajetan Staszkiewicz 			filter_opts.settos = $2;
304739282ef3SKajetan Staszkiewicz 		}
30483e248e0fSKristof Provost prio		: PRIO NUMBER {
30493e248e0fSKristof Provost 			if ($2 < 0 || $2 > PF_PRIO_MAX) {
30503e248e0fSKristof Provost 				yyerror("prio must be 0 - %u", PF_PRIO_MAX);
30513e248e0fSKristof Provost 				YYERROR;
30523e248e0fSKristof Provost 			}
30533e248e0fSKristof Provost 			$$.b1 = $$.b2 = $2;
30543e248e0fSKristof Provost 		}
30553e248e0fSKristof Provost 		| PRIO '(' NUMBER comma NUMBER ')' {
30563e248e0fSKristof Provost 			if ($3 < 0 || $3 > PF_PRIO_MAX ||
30573e248e0fSKristof Provost 			    $5 < 0 || $5 > PF_PRIO_MAX) {
30583e248e0fSKristof Provost 				yyerror("prio must be 0 - %u", PF_PRIO_MAX);
30593e248e0fSKristof Provost 				YYERROR;
30603e248e0fSKristof Provost 			}
30613e248e0fSKristof Provost 			$$.b1 = $3;
30623e248e0fSKristof Provost 			$$.b2 = $5;
30633e248e0fSKristof Provost 		}
30643b3a8eb9SGleb Smirnoff 		;
30653b3a8eb9SGleb Smirnoff 
30663b3a8eb9SGleb Smirnoff probability	: STRING				{
30673b3a8eb9SGleb Smirnoff 			char	*e;
30683b3a8eb9SGleb Smirnoff 			double	 p = strtod($1, &e);
30693b3a8eb9SGleb Smirnoff 
30703b3a8eb9SGleb Smirnoff 			if (*e == '%') {
30713b3a8eb9SGleb Smirnoff 				p *= 0.01;
30723b3a8eb9SGleb Smirnoff 				e++;
30733b3a8eb9SGleb Smirnoff 			}
30743b3a8eb9SGleb Smirnoff 			if (*e) {
30753b3a8eb9SGleb Smirnoff 				yyerror("invalid probability: %s", $1);
30763b3a8eb9SGleb Smirnoff 				free($1);
30773b3a8eb9SGleb Smirnoff 				YYERROR;
30783b3a8eb9SGleb Smirnoff 			}
30793b3a8eb9SGleb Smirnoff 			free($1);
30803b3a8eb9SGleb Smirnoff 			$$ = p;
30813b3a8eb9SGleb Smirnoff 		}
30823b3a8eb9SGleb Smirnoff 		| NUMBER				{
30833b3a8eb9SGleb Smirnoff 			$$ = (double)$1;
30843b3a8eb9SGleb Smirnoff 		}
30853b3a8eb9SGleb Smirnoff 		;
30863b3a8eb9SGleb Smirnoff 
30873b3a8eb9SGleb Smirnoff 
3088150182e3SKristof Provost action		: PASS 			{
3089150182e3SKristof Provost 			$$.b1 = PF_PASS;
3090150182e3SKristof Provost 			$$.b2 = failpolicy;
3091150182e3SKristof Provost 			$$.w = returnicmpdefault;
3092150182e3SKristof Provost 			$$.w2 = returnicmp6default;
3093150182e3SKristof Provost 		}
3094ef950daaSKristof Provost 		| MATCH			{ $$.b1 = PF_MATCH; $$.b2 = $$.w = 0; }
30953b3a8eb9SGleb Smirnoff 		| BLOCK blockspec	{ $$ = $2; $$.b1 = PF_DROP; }
30963b3a8eb9SGleb Smirnoff 		;
30973b3a8eb9SGleb Smirnoff 
30983b3a8eb9SGleb Smirnoff blockspec	: /* empty */		{
30993b3a8eb9SGleb Smirnoff 			$$.b2 = blockpolicy;
31003b3a8eb9SGleb Smirnoff 			$$.w = returnicmpdefault;
31013b3a8eb9SGleb Smirnoff 			$$.w2 = returnicmp6default;
31023b3a8eb9SGleb Smirnoff 		}
31033b3a8eb9SGleb Smirnoff 		| DROP			{
31043b3a8eb9SGleb Smirnoff 			$$.b2 = PFRULE_DROP;
31053b3a8eb9SGleb Smirnoff 			$$.w = 0;
31063b3a8eb9SGleb Smirnoff 			$$.w2 = 0;
31073b3a8eb9SGleb Smirnoff 		}
31083b3a8eb9SGleb Smirnoff 		| RETURNRST		{
31093b3a8eb9SGleb Smirnoff 			$$.b2 = PFRULE_RETURNRST;
31103b3a8eb9SGleb Smirnoff 			$$.w = 0;
31113b3a8eb9SGleb Smirnoff 			$$.w2 = 0;
31123b3a8eb9SGleb Smirnoff 		}
31133b3a8eb9SGleb Smirnoff 		| RETURNRST '(' TTL NUMBER ')'	{
31143b3a8eb9SGleb Smirnoff 			if ($4 < 0 || $4 > 255) {
31153b3a8eb9SGleb Smirnoff 				yyerror("illegal ttl value %d", $4);
31163b3a8eb9SGleb Smirnoff 				YYERROR;
31173b3a8eb9SGleb Smirnoff 			}
31183b3a8eb9SGleb Smirnoff 			$$.b2 = PFRULE_RETURNRST;
31193b3a8eb9SGleb Smirnoff 			$$.w = $4;
31203b3a8eb9SGleb Smirnoff 			$$.w2 = 0;
31213b3a8eb9SGleb Smirnoff 		}
31223b3a8eb9SGleb Smirnoff 		| RETURNICMP		{
31233b3a8eb9SGleb Smirnoff 			$$.b2 = PFRULE_RETURNICMP;
31243b3a8eb9SGleb Smirnoff 			$$.w = returnicmpdefault;
31253b3a8eb9SGleb Smirnoff 			$$.w2 = returnicmp6default;
31263b3a8eb9SGleb Smirnoff 		}
31273b3a8eb9SGleb Smirnoff 		| RETURNICMP6		{
31283b3a8eb9SGleb Smirnoff 			$$.b2 = PFRULE_RETURNICMP;
31293b3a8eb9SGleb Smirnoff 			$$.w = returnicmpdefault;
31303b3a8eb9SGleb Smirnoff 			$$.w2 = returnicmp6default;
31313b3a8eb9SGleb Smirnoff 		}
31323b3a8eb9SGleb Smirnoff 		| RETURNICMP '(' reticmpspec ')'	{
31333b3a8eb9SGleb Smirnoff 			$$.b2 = PFRULE_RETURNICMP;
31343b3a8eb9SGleb Smirnoff 			$$.w = $3;
31353b3a8eb9SGleb Smirnoff 			$$.w2 = returnicmpdefault;
31363b3a8eb9SGleb Smirnoff 		}
31373b3a8eb9SGleb Smirnoff 		| RETURNICMP6 '(' reticmp6spec ')'	{
31383b3a8eb9SGleb Smirnoff 			$$.b2 = PFRULE_RETURNICMP;
31393b3a8eb9SGleb Smirnoff 			$$.w = returnicmpdefault;
31403b3a8eb9SGleb Smirnoff 			$$.w2 = $3;
31413b3a8eb9SGleb Smirnoff 		}
31423b3a8eb9SGleb Smirnoff 		| RETURNICMP '(' reticmpspec comma reticmp6spec ')' {
31433b3a8eb9SGleb Smirnoff 			$$.b2 = PFRULE_RETURNICMP;
31443b3a8eb9SGleb Smirnoff 			$$.w = $3;
31453b3a8eb9SGleb Smirnoff 			$$.w2 = $5;
31463b3a8eb9SGleb Smirnoff 		}
31473b3a8eb9SGleb Smirnoff 		| RETURN {
31483b3a8eb9SGleb Smirnoff 			$$.b2 = PFRULE_RETURN;
31493b3a8eb9SGleb Smirnoff 			$$.w = returnicmpdefault;
31503b3a8eb9SGleb Smirnoff 			$$.w2 = returnicmp6default;
31513b3a8eb9SGleb Smirnoff 		}
31523b3a8eb9SGleb Smirnoff 		;
31533b3a8eb9SGleb Smirnoff 
31543b3a8eb9SGleb Smirnoff reticmpspec	: STRING			{
31553b3a8eb9SGleb Smirnoff 			if (!($$ = parseicmpspec($1, AF_INET))) {
31563b3a8eb9SGleb Smirnoff 				free($1);
31573b3a8eb9SGleb Smirnoff 				YYERROR;
31583b3a8eb9SGleb Smirnoff 			}
31593b3a8eb9SGleb Smirnoff 			free($1);
31603b3a8eb9SGleb Smirnoff 		}
31613b3a8eb9SGleb Smirnoff 		| NUMBER			{
31623b3a8eb9SGleb Smirnoff 			u_int8_t		icmptype;
31633b3a8eb9SGleb Smirnoff 
31643b3a8eb9SGleb Smirnoff 			if ($1 < 0 || $1 > 255) {
31653b3a8eb9SGleb Smirnoff 				yyerror("invalid icmp code %lu", $1);
31663b3a8eb9SGleb Smirnoff 				YYERROR;
31673b3a8eb9SGleb Smirnoff 			}
31683b3a8eb9SGleb Smirnoff 			icmptype = returnicmpdefault >> 8;
31693b3a8eb9SGleb Smirnoff 			$$ = (icmptype << 8 | $1);
31703b3a8eb9SGleb Smirnoff 		}
31713b3a8eb9SGleb Smirnoff 		;
31723b3a8eb9SGleb Smirnoff 
31733b3a8eb9SGleb Smirnoff reticmp6spec	: STRING			{
31743b3a8eb9SGleb Smirnoff 			if (!($$ = parseicmpspec($1, AF_INET6))) {
31753b3a8eb9SGleb Smirnoff 				free($1);
31763b3a8eb9SGleb Smirnoff 				YYERROR;
31773b3a8eb9SGleb Smirnoff 			}
31783b3a8eb9SGleb Smirnoff 			free($1);
31793b3a8eb9SGleb Smirnoff 		}
31803b3a8eb9SGleb Smirnoff 		| NUMBER			{
31813b3a8eb9SGleb Smirnoff 			u_int8_t		icmptype;
31823b3a8eb9SGleb Smirnoff 
31833b3a8eb9SGleb Smirnoff 			if ($1 < 0 || $1 > 255) {
31843b3a8eb9SGleb Smirnoff 				yyerror("invalid icmp code %lu", $1);
31853b3a8eb9SGleb Smirnoff 				YYERROR;
31863b3a8eb9SGleb Smirnoff 			}
31873b3a8eb9SGleb Smirnoff 			icmptype = returnicmp6default >> 8;
31883b3a8eb9SGleb Smirnoff 			$$ = (icmptype << 8 | $1);
31893b3a8eb9SGleb Smirnoff 		}
31903b3a8eb9SGleb Smirnoff 		;
31913b3a8eb9SGleb Smirnoff 
31923b3a8eb9SGleb Smirnoff dir		: /* empty */			{ $$ = PF_INOUT; }
31933b3a8eb9SGleb Smirnoff 		| IN				{ $$ = PF_IN; }
31943b3a8eb9SGleb Smirnoff 		| OUT				{ $$ = PF_OUT; }
31953b3a8eb9SGleb Smirnoff 		;
31963b3a8eb9SGleb Smirnoff 
31973b3a8eb9SGleb Smirnoff quick		: /* empty */			{ $$.quick = 0; }
31983b3a8eb9SGleb Smirnoff 		| QUICK				{ $$.quick = 1; }
31993b3a8eb9SGleb Smirnoff 		;
32003b3a8eb9SGleb Smirnoff 
32013b3a8eb9SGleb Smirnoff logquick	: /* empty */	{ $$.log = 0; $$.quick = 0; $$.logif = 0; }
32023b3a8eb9SGleb Smirnoff 		| log		{ $$ = $1; $$.quick = 0; }
32033b3a8eb9SGleb Smirnoff 		| QUICK		{ $$.quick = 1; $$.log = 0; $$.logif = 0; }
32043b3a8eb9SGleb Smirnoff 		| log QUICK	{ $$ = $1; $$.quick = 1; }
32053b3a8eb9SGleb Smirnoff 		| QUICK log	{ $$ = $2; $$.quick = 1; }
32063b3a8eb9SGleb Smirnoff 		;
32073b3a8eb9SGleb Smirnoff 
32083b3a8eb9SGleb Smirnoff log		: LOG			{ $$.log = PF_LOG; $$.logif = 0; }
32093b3a8eb9SGleb Smirnoff 		| LOG '(' logopts ')'	{
32103b3a8eb9SGleb Smirnoff 			$$.log = PF_LOG | $3.log;
32113b3a8eb9SGleb Smirnoff 			$$.logif = $3.logif;
32123b3a8eb9SGleb Smirnoff 		}
32133b3a8eb9SGleb Smirnoff 		;
32143b3a8eb9SGleb Smirnoff 
32153b3a8eb9SGleb Smirnoff logopts		: logopt			{ $$ = $1; }
32163b3a8eb9SGleb Smirnoff 		| logopts comma logopt		{
32173b3a8eb9SGleb Smirnoff 			$$.log = $1.log | $3.log;
32183b3a8eb9SGleb Smirnoff 			$$.logif = $3.logif;
32193b3a8eb9SGleb Smirnoff 			if ($$.logif == 0)
32203b3a8eb9SGleb Smirnoff 				$$.logif = $1.logif;
32213b3a8eb9SGleb Smirnoff 		}
32223b3a8eb9SGleb Smirnoff 		;
32233b3a8eb9SGleb Smirnoff 
32243b3a8eb9SGleb Smirnoff logopt		: ALL		{ $$.log = PF_LOG_ALL; $$.logif = 0; }
3225f3ab00c2SKristof Provost 		| MATCHES		{ $$.log = PF_LOG_MATCHES; $$.logif = 0; }
32263b3a8eb9SGleb Smirnoff 		| USER		{ $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
32273b3a8eb9SGleb Smirnoff 		| GROUP		{ $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
32283b3a8eb9SGleb Smirnoff 		| TO string	{
32293b3a8eb9SGleb Smirnoff 			const char	*errstr;
32303b3a8eb9SGleb Smirnoff 			u_int		 i;
32313b3a8eb9SGleb Smirnoff 
32323b3a8eb9SGleb Smirnoff 			$$.log = 0;
32333b3a8eb9SGleb Smirnoff 			if (strncmp($2, "pflog", 5)) {
32343b3a8eb9SGleb Smirnoff 				yyerror("%s: should be a pflog interface", $2);
32353b3a8eb9SGleb Smirnoff 				free($2);
32363b3a8eb9SGleb Smirnoff 				YYERROR;
32373b3a8eb9SGleb Smirnoff 			}
32383b3a8eb9SGleb Smirnoff 			i = strtonum($2 + 5, 0, 255, &errstr);
32393b3a8eb9SGleb Smirnoff 			if (errstr) {
32403b3a8eb9SGleb Smirnoff 				yyerror("%s: %s", $2, errstr);
32413b3a8eb9SGleb Smirnoff 				free($2);
32423b3a8eb9SGleb Smirnoff 				YYERROR;
32433b3a8eb9SGleb Smirnoff 			}
32443b3a8eb9SGleb Smirnoff 			free($2);
32453b3a8eb9SGleb Smirnoff 			$$.logif = i;
32463b3a8eb9SGleb Smirnoff 		}
32473b3a8eb9SGleb Smirnoff 		;
32483b3a8eb9SGleb Smirnoff 
32493b3a8eb9SGleb Smirnoff interface	: /* empty */			{ $$ = NULL; }
32503b3a8eb9SGleb Smirnoff 		| ON if_item_not		{ $$ = $2; }
32513b3a8eb9SGleb Smirnoff 		| ON '{' optnl if_list '}'	{ $$ = $4; }
32523b3a8eb9SGleb Smirnoff 		;
32533b3a8eb9SGleb Smirnoff 
32543b3a8eb9SGleb Smirnoff if_list		: if_item_not optnl		{ $$ = $1; }
32553b3a8eb9SGleb Smirnoff 		| if_list comma if_item_not optnl	{
32563b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
32573b3a8eb9SGleb Smirnoff 			$1->tail = $3;
32583b3a8eb9SGleb Smirnoff 			$$ = $1;
32593b3a8eb9SGleb Smirnoff 		}
32603b3a8eb9SGleb Smirnoff 		;
32613b3a8eb9SGleb Smirnoff 
32623b3a8eb9SGleb Smirnoff if_item_not	: not if_item			{ $$ = $2; $$->not = $1; }
32633b3a8eb9SGleb Smirnoff 		;
32643b3a8eb9SGleb Smirnoff 
32653b3a8eb9SGleb Smirnoff if_item		: STRING			{
32663b3a8eb9SGleb Smirnoff 			struct node_host	*n;
32673b3a8eb9SGleb Smirnoff 
32683b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_if));
32693b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
32703b3a8eb9SGleb Smirnoff 				err(1, "if_item: calloc");
32713b3a8eb9SGleb Smirnoff 			if (strlcpy($$->ifname, $1, sizeof($$->ifname)) >=
32723b3a8eb9SGleb Smirnoff 			    sizeof($$->ifname)) {
32733b3a8eb9SGleb Smirnoff 				free($1);
32743b3a8eb9SGleb Smirnoff 				free($$);
32753b3a8eb9SGleb Smirnoff 				yyerror("interface name too long");
32763b3a8eb9SGleb Smirnoff 				YYERROR;
32773b3a8eb9SGleb Smirnoff 			}
32783b3a8eb9SGleb Smirnoff 
32793b3a8eb9SGleb Smirnoff 			if ((n = ifa_exists($1)) != NULL)
32803b3a8eb9SGleb Smirnoff 				$$->ifa_flags = n->ifa_flags;
32813b3a8eb9SGleb Smirnoff 
32823b3a8eb9SGleb Smirnoff 			free($1);
32833b3a8eb9SGleb Smirnoff 			$$->not = 0;
32843b3a8eb9SGleb Smirnoff 			$$->next = NULL;
32853b3a8eb9SGleb Smirnoff 			$$->tail = $$;
32863b3a8eb9SGleb Smirnoff 		}
32873b3a8eb9SGleb Smirnoff 		;
32883b3a8eb9SGleb Smirnoff 
32893b3a8eb9SGleb Smirnoff af		: /* empty */			{ $$ = 0; }
32903b3a8eb9SGleb Smirnoff 		| INET				{ $$ = AF_INET; }
32913b3a8eb9SGleb Smirnoff 		| INET6				{ $$ = AF_INET6; }
32923b3a8eb9SGleb Smirnoff 		;
32933b3a8eb9SGleb Smirnoff 
32942b29ceb8SKristof Provost etherproto	: /* empty */				{ $$ = NULL; }
32952b29ceb8SKristof Provost 		| PROTO etherproto_item			{ $$ = $2; }
32962b29ceb8SKristof Provost 		| PROTO '{' optnl etherproto_list '}'	{ $$ = $4; }
32972b29ceb8SKristof Provost 		;
32982b29ceb8SKristof Provost 
32992b29ceb8SKristof Provost etherproto_list	: etherproto_item optnl			{ $$ = $1; }
33002b29ceb8SKristof Provost 		| etherproto_list comma etherproto_item optnl	{
33012b29ceb8SKristof Provost 			$1->tail->next = $3;
33022b29ceb8SKristof Provost 			$1->tail = $3;
33032b29ceb8SKristof Provost 			$$ = $1;
33042b29ceb8SKristof Provost 		}
33052b29ceb8SKristof Provost 		;
33062b29ceb8SKristof Provost 
33072b29ceb8SKristof Provost etherproto_item	: etherprotoval		{
33082b29ceb8SKristof Provost 			u_int16_t	pr;
33092b29ceb8SKristof Provost 
33102b29ceb8SKristof Provost 			pr = (u_int16_t)$1;
33112b29ceb8SKristof Provost 			if (pr == 0) {
33122b29ceb8SKristof Provost 				yyerror("proto 0 cannot be used");
33132b29ceb8SKristof Provost 				YYERROR;
33142b29ceb8SKristof Provost 			}
33152b29ceb8SKristof Provost 			$$ = calloc(1, sizeof(struct node_proto));
33162b29ceb8SKristof Provost 			if ($$ == NULL)
33172b29ceb8SKristof Provost 				err(1, "proto_item: calloc");
33182b29ceb8SKristof Provost 			$$->proto = pr;
33192b29ceb8SKristof Provost 			$$->next = NULL;
33202b29ceb8SKristof Provost 			$$->tail = $$;
33212b29ceb8SKristof Provost 		}
33222b29ceb8SKristof Provost 		;
33232b29ceb8SKristof Provost 
33242b29ceb8SKristof Provost etherprotoval	: NUMBER			{
33252b29ceb8SKristof Provost 			if ($1 < 0 || $1 > 65565) {
33262b29ceb8SKristof Provost 				yyerror("protocol outside range");
33272b29ceb8SKristof Provost 				YYERROR;
33282b29ceb8SKristof Provost 			}
33292b29ceb8SKristof Provost 		}
33302b29ceb8SKristof Provost 		| STRING
33312b29ceb8SKristof Provost 		{
33322b29ceb8SKristof Provost 			if (!strncmp($1, "0x", 2)) {
33332b29ceb8SKristof Provost 				if (sscanf($1, "0x%4x", &$$) != 1) {
33342b29ceb8SKristof Provost 					free($1);
33352b29ceb8SKristof Provost 					yyerror("invalid EtherType hex");
33362b29ceb8SKristof Provost 					YYERROR;
33372b29ceb8SKristof Provost 				}
33382b29ceb8SKristof Provost 			} else {
33392b29ceb8SKristof Provost 				yyerror("Symbolic EtherType not yet supported");
33402b29ceb8SKristof Provost 			}
33412b29ceb8SKristof Provost 		}
33422b29ceb8SKristof Provost 		;
33432b29ceb8SKristof Provost 
33443b3a8eb9SGleb Smirnoff proto		: /* empty */				{ $$ = NULL; }
33453b3a8eb9SGleb Smirnoff 		| PROTO proto_item			{ $$ = $2; }
33463b3a8eb9SGleb Smirnoff 		| PROTO '{' optnl proto_list '}'	{ $$ = $4; }
33473b3a8eb9SGleb Smirnoff 		;
33483b3a8eb9SGleb Smirnoff 
33493b3a8eb9SGleb Smirnoff proto_list	: proto_item optnl		{ $$ = $1; }
33503b3a8eb9SGleb Smirnoff 		| proto_list comma proto_item optnl	{
33513b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
33523b3a8eb9SGleb Smirnoff 			$1->tail = $3;
33533b3a8eb9SGleb Smirnoff 			$$ = $1;
33543b3a8eb9SGleb Smirnoff 		}
33553b3a8eb9SGleb Smirnoff 		;
33563b3a8eb9SGleb Smirnoff 
33573b3a8eb9SGleb Smirnoff proto_item	: protoval			{
33583b3a8eb9SGleb Smirnoff 			u_int8_t	pr;
33593b3a8eb9SGleb Smirnoff 
33603b3a8eb9SGleb Smirnoff 			pr = (u_int8_t)$1;
33613b3a8eb9SGleb Smirnoff 			if (pr == 0) {
33623b3a8eb9SGleb Smirnoff 				yyerror("proto 0 cannot be used");
33633b3a8eb9SGleb Smirnoff 				YYERROR;
33643b3a8eb9SGleb Smirnoff 			}
33653b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_proto));
33663b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
33673b3a8eb9SGleb Smirnoff 				err(1, "proto_item: calloc");
33683b3a8eb9SGleb Smirnoff 			$$->proto = pr;
33693b3a8eb9SGleb Smirnoff 			$$->next = NULL;
33703b3a8eb9SGleb Smirnoff 			$$->tail = $$;
33713b3a8eb9SGleb Smirnoff 		}
33723b3a8eb9SGleb Smirnoff 		;
33733b3a8eb9SGleb Smirnoff 
33743b3a8eb9SGleb Smirnoff protoval	: STRING			{
33753b3a8eb9SGleb Smirnoff 			struct protoent	*p;
33763b3a8eb9SGleb Smirnoff 
33773b3a8eb9SGleb Smirnoff 			p = getprotobyname($1);
33783b3a8eb9SGleb Smirnoff 			if (p == NULL) {
33793b3a8eb9SGleb Smirnoff 				yyerror("unknown protocol %s", $1);
33803b3a8eb9SGleb Smirnoff 				free($1);
33813b3a8eb9SGleb Smirnoff 				YYERROR;
33823b3a8eb9SGleb Smirnoff 			}
33833b3a8eb9SGleb Smirnoff 			$$ = p->p_proto;
33843b3a8eb9SGleb Smirnoff 			free($1);
33853b3a8eb9SGleb Smirnoff 		}
33863b3a8eb9SGleb Smirnoff 		| NUMBER			{
33873b3a8eb9SGleb Smirnoff 			if ($1 < 0 || $1 > 255) {
33883b3a8eb9SGleb Smirnoff 				yyerror("protocol outside range");
33893b3a8eb9SGleb Smirnoff 				YYERROR;
33903b3a8eb9SGleb Smirnoff 			}
33913b3a8eb9SGleb Smirnoff 		}
33923b3a8eb9SGleb Smirnoff 		;
33933b3a8eb9SGleb Smirnoff 
33948a42005dSKristof Provost l3fromto	: /* empty */			{
33958a42005dSKristof Provost 			bzero(&$$, sizeof($$));
33968a42005dSKristof Provost 		}
33978a42005dSKristof Provost 		| L3 fromto			{
33983468cd95SKristof Provost 			if ($2.src.host != NULL &&
3399812839e5SKristof Provost 			    $2.src.host->addr.type != PF_ADDR_ADDRMASK &&
3400812839e5SKristof Provost 			    $2.src.host->addr.type != PF_ADDR_TABLE) {
3401812839e5SKristof Provost 				yyerror("from must be an address or table");
34023468cd95SKristof Provost 				YYERROR;
34033468cd95SKristof Provost 			}
34043468cd95SKristof Provost 			if ($2.dst.host != NULL &&
3405812839e5SKristof Provost 			    $2.dst.host->addr.type != PF_ADDR_ADDRMASK &&
3406812839e5SKristof Provost 			    $2.dst.host->addr.type != PF_ADDR_TABLE) {
3407812839e5SKristof Provost 				yyerror("to must be an address or table");
34083468cd95SKristof Provost 				YYERROR;
34093468cd95SKristof Provost 			}
34108a42005dSKristof Provost 			$$ = $2;
34118a42005dSKristof Provost 		}
34128a42005dSKristof Provost 		;
34132b29ceb8SKristof Provost etherfromto	: ALL				{
341487a89d6eSKristof Provost 			$$.src = NULL;
341587a89d6eSKristof Provost 			$$.dst = NULL;
34162b29ceb8SKristof Provost 		}
34172b29ceb8SKristof Provost 		| etherfrom etherto		{
341887a89d6eSKristof Provost 			$$.src = $1.mac;
341987a89d6eSKristof Provost 			$$.dst = $2.mac;
34202b29ceb8SKristof Provost 		}
34212b29ceb8SKristof Provost 		;
34222b29ceb8SKristof Provost 
34232b29ceb8SKristof Provost etherfrom	: /* emtpy */			{
34242b29ceb8SKristof Provost 			bzero(&$$, sizeof($$));
34252b29ceb8SKristof Provost 		}
342687a89d6eSKristof Provost 		| FROM macspec			{
342787a89d6eSKristof Provost 			$$.mac = $2;
34282b29ceb8SKristof Provost 		}
34292b29ceb8SKristof Provost 		;
34302b29ceb8SKristof Provost 
34312b29ceb8SKristof Provost etherto		: /* empty */			{
34322b29ceb8SKristof Provost 			bzero(&$$, sizeof($$));
34332b29ceb8SKristof Provost 		}
343487a89d6eSKristof Provost 		| TO macspec			{
343587a89d6eSKristof Provost 			$$.mac = $2;
34362b29ceb8SKristof Provost 		}
34372b29ceb8SKristof Provost 		;
34382b29ceb8SKristof Provost 
3439b590f17aSKristof Provost mac		: string '/' NUMBER		{
3440b590f17aSKristof Provost 			$$ = node_mac_from_string_masklen($1, $3);
34412b29ceb8SKristof Provost 			free($1);
3442b590f17aSKristof Provost 			if ($$ == NULL)
34432b29ceb8SKristof Provost 				YYERROR;
34442b29ceb8SKristof Provost 		}
3445b590f17aSKristof Provost 		| string			{
3446b590f17aSKristof Provost 			if (strchr($1, '&')) {
3447b590f17aSKristof Provost 				/* mac&mask */
3448b590f17aSKristof Provost 				char *mac = strtok($1, "&");
3449b590f17aSKristof Provost 				char *mask = strtok(NULL, "&");
3450b590f17aSKristof Provost 				$$ = node_mac_from_string_mask(mac, mask);
3451b590f17aSKristof Provost 			} else {
3452b590f17aSKristof Provost 				$$ = node_mac_from_string($1);
3453b590f17aSKristof Provost 			}
345487a89d6eSKristof Provost 			free($1);
3455b590f17aSKristof Provost 			if ($$ == NULL)
3456b590f17aSKristof Provost 				YYERROR;
3457b590f17aSKristof Provost 
345887a89d6eSKristof Provost 		}
345987a89d6eSKristof Provost xmac		: not mac {
346087a89d6eSKristof Provost 			struct node_mac	*n;
346187a89d6eSKristof Provost 
346287a89d6eSKristof Provost 			for (n = $2; n != NULL; n = n->next)
346387a89d6eSKristof Provost 				n->neg = $1;
346487a89d6eSKristof Provost 			$$ = $2;
34652b29ceb8SKristof Provost 		}
34662b29ceb8SKristof Provost 		;
346787a89d6eSKristof Provost macspec		: xmac {
346887a89d6eSKristof Provost 			$$ = $1;
346987a89d6eSKristof Provost 		}
347087a89d6eSKristof Provost 		| '{' optnl mac_list '}'
347187a89d6eSKristof Provost 		{
347287a89d6eSKristof Provost 			$$ = $3;
347387a89d6eSKristof Provost 		}
347487a89d6eSKristof Provost 		;
347587a89d6eSKristof Provost mac_list	: xmac optnl {
347687a89d6eSKristof Provost 			$$ = $1;
347787a89d6eSKristof Provost 		}
347887a89d6eSKristof Provost 		| mac_list comma xmac {
347987a89d6eSKristof Provost 			if ($3 == NULL)
348087a89d6eSKristof Provost 				$$ = $1;
348187a89d6eSKristof Provost 			else if ($1 == NULL)
348287a89d6eSKristof Provost 				$$ = $3;
348387a89d6eSKristof Provost 			else {
348487a89d6eSKristof Provost 				$1->tail->next = $3;
348587a89d6eSKristof Provost 				$1->tail = $3->tail;
348687a89d6eSKristof Provost 				$$ = $1;
348787a89d6eSKristof Provost 			}
348887a89d6eSKristof Provost 		}
34892b29ceb8SKristof Provost 
34903b3a8eb9SGleb Smirnoff fromto		: ALL				{
34913b3a8eb9SGleb Smirnoff 			$$.src.host = NULL;
34923b3a8eb9SGleb Smirnoff 			$$.src.port = NULL;
34933b3a8eb9SGleb Smirnoff 			$$.dst.host = NULL;
34943b3a8eb9SGleb Smirnoff 			$$.dst.port = NULL;
34953b3a8eb9SGleb Smirnoff 			$$.src_os = NULL;
34963b3a8eb9SGleb Smirnoff 		}
34973b3a8eb9SGleb Smirnoff 		| from os to			{
34983b3a8eb9SGleb Smirnoff 			$$.src = $1;
34993b3a8eb9SGleb Smirnoff 			$$.src_os = $2;
35003b3a8eb9SGleb Smirnoff 			$$.dst = $3;
35013b3a8eb9SGleb Smirnoff 		}
35023b3a8eb9SGleb Smirnoff 		;
35033b3a8eb9SGleb Smirnoff 
35043b3a8eb9SGleb Smirnoff os		: /* empty */			{ $$ = NULL; }
35053b3a8eb9SGleb Smirnoff 		| OS xos			{ $$ = $2; }
35063b3a8eb9SGleb Smirnoff 		| OS '{' optnl os_list '}'	{ $$ = $4; }
35073b3a8eb9SGleb Smirnoff 		;
35083b3a8eb9SGleb Smirnoff 
35093b3a8eb9SGleb Smirnoff xos		: STRING {
35103b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_os));
35113b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
35123b3a8eb9SGleb Smirnoff 				err(1, "os: calloc");
35133b3a8eb9SGleb Smirnoff 			$$->os = $1;
35143b3a8eb9SGleb Smirnoff 			$$->tail = $$;
35153b3a8eb9SGleb Smirnoff 		}
35163b3a8eb9SGleb Smirnoff 		;
35173b3a8eb9SGleb Smirnoff 
35183b3a8eb9SGleb Smirnoff os_list		: xos optnl 			{ $$ = $1; }
35193b3a8eb9SGleb Smirnoff 		| os_list comma xos optnl	{
35203b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
35213b3a8eb9SGleb Smirnoff 			$1->tail = $3;
35223b3a8eb9SGleb Smirnoff 			$$ = $1;
35233b3a8eb9SGleb Smirnoff 		}
35243b3a8eb9SGleb Smirnoff 		;
35253b3a8eb9SGleb Smirnoff 
35263b3a8eb9SGleb Smirnoff from		: /* empty */			{
35273b3a8eb9SGleb Smirnoff 			$$.host = NULL;
35283b3a8eb9SGleb Smirnoff 			$$.port = NULL;
35293b3a8eb9SGleb Smirnoff 		}
35303b3a8eb9SGleb Smirnoff 		| FROM ipportspec		{
35313b3a8eb9SGleb Smirnoff 			$$ = $2;
35323b3a8eb9SGleb Smirnoff 		}
35333b3a8eb9SGleb Smirnoff 		;
35343b3a8eb9SGleb Smirnoff 
35353b3a8eb9SGleb Smirnoff to		: /* empty */			{
35363b3a8eb9SGleb Smirnoff 			$$.host = NULL;
35373b3a8eb9SGleb Smirnoff 			$$.port = NULL;
35383b3a8eb9SGleb Smirnoff 		}
35393b3a8eb9SGleb Smirnoff 		| TO ipportspec		{
35403b3a8eb9SGleb Smirnoff 			if (disallow_urpf_failed($2.host, "\"urpf-failed\" is "
35413b3a8eb9SGleb Smirnoff 			    "not permitted in a destination address"))
35423b3a8eb9SGleb Smirnoff 				YYERROR;
35433b3a8eb9SGleb Smirnoff 			$$ = $2;
35443b3a8eb9SGleb Smirnoff 		}
35453b3a8eb9SGleb Smirnoff 		;
35463b3a8eb9SGleb Smirnoff 
35473b3a8eb9SGleb Smirnoff ipportspec	: ipspec			{
35483b3a8eb9SGleb Smirnoff 			$$.host = $1;
35493b3a8eb9SGleb Smirnoff 			$$.port = NULL;
35503b3a8eb9SGleb Smirnoff 		}
35513b3a8eb9SGleb Smirnoff 		| ipspec PORT portspec		{
35523b3a8eb9SGleb Smirnoff 			$$.host = $1;
35533b3a8eb9SGleb Smirnoff 			$$.port = $3;
35543b3a8eb9SGleb Smirnoff 		}
35553b3a8eb9SGleb Smirnoff 		| PORT portspec			{
35563b3a8eb9SGleb Smirnoff 			$$.host = NULL;
35573b3a8eb9SGleb Smirnoff 			$$.port = $2;
35583b3a8eb9SGleb Smirnoff 		}
35593b3a8eb9SGleb Smirnoff 		;
35603b3a8eb9SGleb Smirnoff 
35613b3a8eb9SGleb Smirnoff optnl		: '\n' optnl
35623b3a8eb9SGleb Smirnoff 		|
35633b3a8eb9SGleb Smirnoff 		;
35643b3a8eb9SGleb Smirnoff 
35653b3a8eb9SGleb Smirnoff ipspec		: ANY				{ $$ = NULL; }
35663b3a8eb9SGleb Smirnoff 		| xhost				{ $$ = $1; }
35673b3a8eb9SGleb Smirnoff 		| '{' optnl host_list '}'	{ $$ = $3; }
35683b3a8eb9SGleb Smirnoff 		;
35693b3a8eb9SGleb Smirnoff 
35703b3a8eb9SGleb Smirnoff toipspec	: TO ipspec			{ $$ = $2; }
35713b3a8eb9SGleb Smirnoff 		| /* empty */			{ $$ = NULL; }
35723b3a8eb9SGleb Smirnoff 		;
35733b3a8eb9SGleb Smirnoff 
35743b3a8eb9SGleb Smirnoff host_list	: ipspec optnl			{ $$ = $1; }
35753b3a8eb9SGleb Smirnoff 		| host_list comma ipspec optnl	{
3576637d81c5SKristof Provost 			if ($1 == NULL) {
3577637d81c5SKristof Provost 				freehostlist($3);
35783b3a8eb9SGleb Smirnoff 				$$ = $1;
3579637d81c5SKristof Provost 			} else if ($3 == NULL) {
3580637d81c5SKristof Provost 				freehostlist($1);
35813b3a8eb9SGleb Smirnoff 				$$ = $3;
3582637d81c5SKristof Provost 			} else {
35833b3a8eb9SGleb Smirnoff 				$1->tail->next = $3;
35843b3a8eb9SGleb Smirnoff 				$1->tail = $3->tail;
35853b3a8eb9SGleb Smirnoff 				$$ = $1;
35863b3a8eb9SGleb Smirnoff 			}
35873b3a8eb9SGleb Smirnoff 		}
35883b3a8eb9SGleb Smirnoff 		;
35893b3a8eb9SGleb Smirnoff 
35903b3a8eb9SGleb Smirnoff xhost		: not host			{
35913b3a8eb9SGleb Smirnoff 			struct node_host	*n;
35923b3a8eb9SGleb Smirnoff 
35933b3a8eb9SGleb Smirnoff 			for (n = $2; n != NULL; n = n->next)
35943b3a8eb9SGleb Smirnoff 				n->not = $1;
35953b3a8eb9SGleb Smirnoff 			$$ = $2;
35963b3a8eb9SGleb Smirnoff 		}
35973b3a8eb9SGleb Smirnoff 		| not NOROUTE			{
35983b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_host));
35993b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
36003b3a8eb9SGleb Smirnoff 				err(1, "xhost: calloc");
36013b3a8eb9SGleb Smirnoff 			$$->addr.type = PF_ADDR_NOROUTE;
36023b3a8eb9SGleb Smirnoff 			$$->next = NULL;
36033b3a8eb9SGleb Smirnoff 			$$->not = $1;
36043b3a8eb9SGleb Smirnoff 			$$->tail = $$;
36053b3a8eb9SGleb Smirnoff 		}
36063b3a8eb9SGleb Smirnoff 		| not URPFFAILED		{
36073b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_host));
36083b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
36093b3a8eb9SGleb Smirnoff 				err(1, "xhost: calloc");
36103b3a8eb9SGleb Smirnoff 			$$->addr.type = PF_ADDR_URPFFAILED;
36113b3a8eb9SGleb Smirnoff 			$$->next = NULL;
36123b3a8eb9SGleb Smirnoff 			$$->not = $1;
36133b3a8eb9SGleb Smirnoff 			$$->tail = $$;
36143b3a8eb9SGleb Smirnoff 		}
36153b3a8eb9SGleb Smirnoff 		;
36163b3a8eb9SGleb Smirnoff 
36173b3a8eb9SGleb Smirnoff host		: STRING			{
36183b3a8eb9SGleb Smirnoff 			if (($$ = host($1)) == NULL)	{
36193b3a8eb9SGleb Smirnoff 				/* error. "any" is handled elsewhere */
36203b3a8eb9SGleb Smirnoff 				free($1);
36213b3a8eb9SGleb Smirnoff 				yyerror("could not parse host specification");
36223b3a8eb9SGleb Smirnoff 				YYERROR;
36233b3a8eb9SGleb Smirnoff 			}
36243b3a8eb9SGleb Smirnoff 			free($1);
36253b3a8eb9SGleb Smirnoff 
36263b3a8eb9SGleb Smirnoff 		}
36273b3a8eb9SGleb Smirnoff 		| STRING '-' STRING		{
36283b3a8eb9SGleb Smirnoff 			struct node_host *b, *e;
36293b3a8eb9SGleb Smirnoff 
36303b3a8eb9SGleb Smirnoff 			if ((b = host($1)) == NULL || (e = host($3)) == NULL) {
36313b3a8eb9SGleb Smirnoff 				free($1);
36323b3a8eb9SGleb Smirnoff 				free($3);
36333b3a8eb9SGleb Smirnoff 				yyerror("could not parse host specification");
36343b3a8eb9SGleb Smirnoff 				YYERROR;
36353b3a8eb9SGleb Smirnoff 			}
36363b3a8eb9SGleb Smirnoff 			if (b->af != e->af ||
36373b3a8eb9SGleb Smirnoff 			    b->addr.type != PF_ADDR_ADDRMASK ||
36383b3a8eb9SGleb Smirnoff 			    e->addr.type != PF_ADDR_ADDRMASK ||
36393b3a8eb9SGleb Smirnoff 			    unmask(&b->addr.v.a.mask, b->af) !=
36403b3a8eb9SGleb Smirnoff 			    (b->af == AF_INET ? 32 : 128) ||
36413b3a8eb9SGleb Smirnoff 			    unmask(&e->addr.v.a.mask, e->af) !=
36423b3a8eb9SGleb Smirnoff 			    (e->af == AF_INET ? 32 : 128) ||
36433b3a8eb9SGleb Smirnoff 			    b->next != NULL || b->not ||
36443b3a8eb9SGleb Smirnoff 			    e->next != NULL || e->not) {
36453b3a8eb9SGleb Smirnoff 				free(b);
36463b3a8eb9SGleb Smirnoff 				free(e);
36473b3a8eb9SGleb Smirnoff 				free($1);
36483b3a8eb9SGleb Smirnoff 				free($3);
36493b3a8eb9SGleb Smirnoff 				yyerror("invalid address range");
36503b3a8eb9SGleb Smirnoff 				YYERROR;
36513b3a8eb9SGleb Smirnoff 			}
36523b3a8eb9SGleb Smirnoff 			memcpy(&b->addr.v.a.mask, &e->addr.v.a.addr,
36533b3a8eb9SGleb Smirnoff 			    sizeof(b->addr.v.a.mask));
36543b3a8eb9SGleb Smirnoff 			b->addr.type = PF_ADDR_RANGE;
36553b3a8eb9SGleb Smirnoff 			$$ = b;
36563b3a8eb9SGleb Smirnoff 			free(e);
36573b3a8eb9SGleb Smirnoff 			free($1);
36583b3a8eb9SGleb Smirnoff 			free($3);
36593b3a8eb9SGleb Smirnoff 		}
36603b3a8eb9SGleb Smirnoff 		| STRING '/' NUMBER		{
36613b3a8eb9SGleb Smirnoff 			char	*buf;
36623b3a8eb9SGleb Smirnoff 
36633b3a8eb9SGleb Smirnoff 			if (asprintf(&buf, "%s/%lld", $1, (long long)$3) == -1)
36643b3a8eb9SGleb Smirnoff 				err(1, "host: asprintf");
36653b3a8eb9SGleb Smirnoff 			free($1);
36663b3a8eb9SGleb Smirnoff 			if (($$ = host(buf)) == NULL)	{
36673b3a8eb9SGleb Smirnoff 				/* error. "any" is handled elsewhere */
36683b3a8eb9SGleb Smirnoff 				free(buf);
36693b3a8eb9SGleb Smirnoff 				yyerror("could not parse host specification");
36703b3a8eb9SGleb Smirnoff 				YYERROR;
36713b3a8eb9SGleb Smirnoff 			}
36723b3a8eb9SGleb Smirnoff 			free(buf);
36733b3a8eb9SGleb Smirnoff 		}
36743b3a8eb9SGleb Smirnoff 		| NUMBER '/' NUMBER		{
36753b3a8eb9SGleb Smirnoff 			char	*buf;
36763b3a8eb9SGleb Smirnoff 
36773b3a8eb9SGleb Smirnoff 			/* ie. for 10/8 parsing */
36783b3a8eb9SGleb Smirnoff #ifdef __FreeBSD__
36793b3a8eb9SGleb Smirnoff 			if (asprintf(&buf, "%lld/%lld", (long long)$1, (long long)$3) == -1)
36803b3a8eb9SGleb Smirnoff #else
36813b3a8eb9SGleb Smirnoff 			if (asprintf(&buf, "%lld/%lld", $1, $3) == -1)
36823b3a8eb9SGleb Smirnoff #endif
36833b3a8eb9SGleb Smirnoff 				err(1, "host: asprintf");
36843b3a8eb9SGleb Smirnoff 			if (($$ = host(buf)) == NULL)	{
36853b3a8eb9SGleb Smirnoff 				/* error. "any" is handled elsewhere */
36863b3a8eb9SGleb Smirnoff 				free(buf);
36873b3a8eb9SGleb Smirnoff 				yyerror("could not parse host specification");
36883b3a8eb9SGleb Smirnoff 				YYERROR;
36893b3a8eb9SGleb Smirnoff 			}
36903b3a8eb9SGleb Smirnoff 			free(buf);
36913b3a8eb9SGleb Smirnoff 		}
36923b3a8eb9SGleb Smirnoff 		| dynaddr
36933b3a8eb9SGleb Smirnoff 		| dynaddr '/' NUMBER		{
36943b3a8eb9SGleb Smirnoff 			struct node_host	*n;
36953b3a8eb9SGleb Smirnoff 
36963b3a8eb9SGleb Smirnoff 			if ($3 < 0 || $3 > 128) {
36973b3a8eb9SGleb Smirnoff 				yyerror("bit number too big");
36983b3a8eb9SGleb Smirnoff 				YYERROR;
36993b3a8eb9SGleb Smirnoff 			}
37003b3a8eb9SGleb Smirnoff 			$$ = $1;
37013b3a8eb9SGleb Smirnoff 			for (n = $1; n != NULL; n = n->next)
37023b3a8eb9SGleb Smirnoff 				set_ipmask(n, $3);
37033b3a8eb9SGleb Smirnoff 		}
37043b3a8eb9SGleb Smirnoff 		| '<' STRING '>'	{
37053b3a8eb9SGleb Smirnoff 			if (strlen($2) >= PF_TABLE_NAME_SIZE) {
37063b3a8eb9SGleb Smirnoff 				yyerror("table name '%s' too long", $2);
37073b3a8eb9SGleb Smirnoff 				free($2);
37083b3a8eb9SGleb Smirnoff 				YYERROR;
37093b3a8eb9SGleb Smirnoff 			}
37103b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_host));
37113b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
37123b3a8eb9SGleb Smirnoff 				err(1, "host: calloc");
37133b3a8eb9SGleb Smirnoff 			$$->addr.type = PF_ADDR_TABLE;
37143b3a8eb9SGleb Smirnoff 			if (strlcpy($$->addr.v.tblname, $2,
37153b3a8eb9SGleb Smirnoff 			    sizeof($$->addr.v.tblname)) >=
37163b3a8eb9SGleb Smirnoff 			    sizeof($$->addr.v.tblname))
37173b3a8eb9SGleb Smirnoff 				errx(1, "host: strlcpy");
37183b3a8eb9SGleb Smirnoff 			free($2);
37193b3a8eb9SGleb Smirnoff 			$$->next = NULL;
37203b3a8eb9SGleb Smirnoff 			$$->tail = $$;
37213b3a8eb9SGleb Smirnoff 		}
37223b3a8eb9SGleb Smirnoff 		;
37233b3a8eb9SGleb Smirnoff 
37243b3a8eb9SGleb Smirnoff number		: NUMBER
37253b3a8eb9SGleb Smirnoff 		| STRING		{
37263b3a8eb9SGleb Smirnoff 			u_long	ulval;
37273b3a8eb9SGleb Smirnoff 
37283b3a8eb9SGleb Smirnoff 			if (atoul($1, &ulval) == -1) {
37293b3a8eb9SGleb Smirnoff 				yyerror("%s is not a number", $1);
37303b3a8eb9SGleb Smirnoff 				free($1);
37313b3a8eb9SGleb Smirnoff 				YYERROR;
37323b3a8eb9SGleb Smirnoff 			} else
37333b3a8eb9SGleb Smirnoff 				$$ = ulval;
37343b3a8eb9SGleb Smirnoff 			free($1);
37353b3a8eb9SGleb Smirnoff 		}
37363b3a8eb9SGleb Smirnoff 		;
37373b3a8eb9SGleb Smirnoff 
37383b3a8eb9SGleb Smirnoff dynaddr		: '(' STRING ')'		{
37393b3a8eb9SGleb Smirnoff 			int	 flags = 0;
37403b3a8eb9SGleb Smirnoff 			char	*p, *op;
37413b3a8eb9SGleb Smirnoff 
37423b3a8eb9SGleb Smirnoff 			op = $2;
37433b3a8eb9SGleb Smirnoff 			if (!isalpha(op[0])) {
37443b3a8eb9SGleb Smirnoff 				yyerror("invalid interface name '%s'", op);
37453b3a8eb9SGleb Smirnoff 				free(op);
37463b3a8eb9SGleb Smirnoff 				YYERROR;
37473b3a8eb9SGleb Smirnoff 			}
37483b3a8eb9SGleb Smirnoff 			while ((p = strrchr($2, ':')) != NULL) {
37493b3a8eb9SGleb Smirnoff 				if (!strcmp(p+1, "network"))
37503b3a8eb9SGleb Smirnoff 					flags |= PFI_AFLAG_NETWORK;
37513b3a8eb9SGleb Smirnoff 				else if (!strcmp(p+1, "broadcast"))
37523b3a8eb9SGleb Smirnoff 					flags |= PFI_AFLAG_BROADCAST;
37533b3a8eb9SGleb Smirnoff 				else if (!strcmp(p+1, "peer"))
37543b3a8eb9SGleb Smirnoff 					flags |= PFI_AFLAG_PEER;
37553b3a8eb9SGleb Smirnoff 				else if (!strcmp(p+1, "0"))
37563b3a8eb9SGleb Smirnoff 					flags |= PFI_AFLAG_NOALIAS;
37573b3a8eb9SGleb Smirnoff 				else {
37583b3a8eb9SGleb Smirnoff 					yyerror("interface %s has bad modifier",
37593b3a8eb9SGleb Smirnoff 					    $2);
37603b3a8eb9SGleb Smirnoff 					free(op);
37613b3a8eb9SGleb Smirnoff 					YYERROR;
37623b3a8eb9SGleb Smirnoff 				}
37633b3a8eb9SGleb Smirnoff 				*p = '\0';
37643b3a8eb9SGleb Smirnoff 			}
37653b3a8eb9SGleb Smirnoff 			if (flags & (flags - 1) & PFI_AFLAG_MODEMASK) {
37663b3a8eb9SGleb Smirnoff 				free(op);
37673b3a8eb9SGleb Smirnoff 				yyerror("illegal combination of "
37683b3a8eb9SGleb Smirnoff 				    "interface modifiers");
37693b3a8eb9SGleb Smirnoff 				YYERROR;
37703b3a8eb9SGleb Smirnoff 			}
37713b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_host));
37723b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
37733b3a8eb9SGleb Smirnoff 				err(1, "address: calloc");
37743b3a8eb9SGleb Smirnoff 			$$->af = 0;
37753b3a8eb9SGleb Smirnoff 			set_ipmask($$, 128);
37763b3a8eb9SGleb Smirnoff 			$$->addr.type = PF_ADDR_DYNIFTL;
37773b3a8eb9SGleb Smirnoff 			$$->addr.iflags = flags;
37783b3a8eb9SGleb Smirnoff 			if (strlcpy($$->addr.v.ifname, $2,
37793b3a8eb9SGleb Smirnoff 			    sizeof($$->addr.v.ifname)) >=
37803b3a8eb9SGleb Smirnoff 			    sizeof($$->addr.v.ifname)) {
37813b3a8eb9SGleb Smirnoff 				free(op);
37823b3a8eb9SGleb Smirnoff 				free($$);
37833b3a8eb9SGleb Smirnoff 				yyerror("interface name too long");
37843b3a8eb9SGleb Smirnoff 				YYERROR;
37853b3a8eb9SGleb Smirnoff 			}
37863b3a8eb9SGleb Smirnoff 			free(op);
37873b3a8eb9SGleb Smirnoff 			$$->next = NULL;
37883b3a8eb9SGleb Smirnoff 			$$->tail = $$;
37893b3a8eb9SGleb Smirnoff 		}
37903b3a8eb9SGleb Smirnoff 		;
37913b3a8eb9SGleb Smirnoff 
37923b3a8eb9SGleb Smirnoff portspec	: port_item			{ $$ = $1; }
37933b3a8eb9SGleb Smirnoff 		| '{' optnl port_list '}'	{ $$ = $3; }
37943b3a8eb9SGleb Smirnoff 		;
37953b3a8eb9SGleb Smirnoff 
37963b3a8eb9SGleb Smirnoff port_list	: port_item optnl		{ $$ = $1; }
37973b3a8eb9SGleb Smirnoff 		| port_list comma port_item optnl	{
37983b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
37993b3a8eb9SGleb Smirnoff 			$1->tail = $3;
38003b3a8eb9SGleb Smirnoff 			$$ = $1;
38013b3a8eb9SGleb Smirnoff 		}
38023b3a8eb9SGleb Smirnoff 		;
38033b3a8eb9SGleb Smirnoff 
38043b3a8eb9SGleb Smirnoff port_item	: portrange			{
38053b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_port));
38063b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
38073b3a8eb9SGleb Smirnoff 				err(1, "port_item: calloc");
38083b3a8eb9SGleb Smirnoff 			$$->port[0] = $1.a;
38093b3a8eb9SGleb Smirnoff 			$$->port[1] = $1.b;
38103b3a8eb9SGleb Smirnoff 			if ($1.t)
38113b3a8eb9SGleb Smirnoff 				$$->op = PF_OP_RRG;
38123b3a8eb9SGleb Smirnoff 			else
38133b3a8eb9SGleb Smirnoff 				$$->op = PF_OP_EQ;
38143b3a8eb9SGleb Smirnoff 			$$->next = NULL;
38153b3a8eb9SGleb Smirnoff 			$$->tail = $$;
38163b3a8eb9SGleb Smirnoff 		}
38173b3a8eb9SGleb Smirnoff 		| unaryop portrange	{
38183b3a8eb9SGleb Smirnoff 			if ($2.t) {
38193b3a8eb9SGleb Smirnoff 				yyerror("':' cannot be used with an other "
38203b3a8eb9SGleb Smirnoff 				    "port operator");
38213b3a8eb9SGleb Smirnoff 				YYERROR;
38223b3a8eb9SGleb Smirnoff 			}
38233b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_port));
38243b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
38253b3a8eb9SGleb Smirnoff 				err(1, "port_item: calloc");
38263b3a8eb9SGleb Smirnoff 			$$->port[0] = $2.a;
38273b3a8eb9SGleb Smirnoff 			$$->port[1] = $2.b;
38283b3a8eb9SGleb Smirnoff 			$$->op = $1;
38293b3a8eb9SGleb Smirnoff 			$$->next = NULL;
38303b3a8eb9SGleb Smirnoff 			$$->tail = $$;
38313b3a8eb9SGleb Smirnoff 		}
38323b3a8eb9SGleb Smirnoff 		| portrange PORTBINARY portrange	{
38333b3a8eb9SGleb Smirnoff 			if ($1.t || $3.t) {
38343b3a8eb9SGleb Smirnoff 				yyerror("':' cannot be used with an other "
38353b3a8eb9SGleb Smirnoff 				    "port operator");
38363b3a8eb9SGleb Smirnoff 				YYERROR;
38373b3a8eb9SGleb Smirnoff 			}
38383b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_port));
38393b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
38403b3a8eb9SGleb Smirnoff 				err(1, "port_item: calloc");
38413b3a8eb9SGleb Smirnoff 			$$->port[0] = $1.a;
38423b3a8eb9SGleb Smirnoff 			$$->port[1] = $3.a;
38433b3a8eb9SGleb Smirnoff 			$$->op = $2;
38443b3a8eb9SGleb Smirnoff 			$$->next = NULL;
38453b3a8eb9SGleb Smirnoff 			$$->tail = $$;
38463b3a8eb9SGleb Smirnoff 		}
38473b3a8eb9SGleb Smirnoff 		;
38483b3a8eb9SGleb Smirnoff 
38493b3a8eb9SGleb Smirnoff portplain	: numberstring			{
38503b3a8eb9SGleb Smirnoff 			if (parseport($1, &$$, 0) == -1) {
38513b3a8eb9SGleb Smirnoff 				free($1);
38523b3a8eb9SGleb Smirnoff 				YYERROR;
38533b3a8eb9SGleb Smirnoff 			}
38543b3a8eb9SGleb Smirnoff 			free($1);
38553b3a8eb9SGleb Smirnoff 		}
38563b3a8eb9SGleb Smirnoff 		;
38573b3a8eb9SGleb Smirnoff 
38583b3a8eb9SGleb Smirnoff portrange	: numberstring			{
38593b3a8eb9SGleb Smirnoff 			if (parseport($1, &$$, PPORT_RANGE) == -1) {
38603b3a8eb9SGleb Smirnoff 				free($1);
38613b3a8eb9SGleb Smirnoff 				YYERROR;
38623b3a8eb9SGleb Smirnoff 			}
38633b3a8eb9SGleb Smirnoff 			free($1);
38643b3a8eb9SGleb Smirnoff 		}
38653b3a8eb9SGleb Smirnoff 		;
38663b3a8eb9SGleb Smirnoff 
38673b3a8eb9SGleb Smirnoff uids		: uid_item			{ $$ = $1; }
38683b3a8eb9SGleb Smirnoff 		| '{' optnl uid_list '}'	{ $$ = $3; }
38693b3a8eb9SGleb Smirnoff 		;
38703b3a8eb9SGleb Smirnoff 
38713b3a8eb9SGleb Smirnoff uid_list	: uid_item optnl		{ $$ = $1; }
38723b3a8eb9SGleb Smirnoff 		| uid_list comma uid_item optnl	{
38733b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
38743b3a8eb9SGleb Smirnoff 			$1->tail = $3;
38753b3a8eb9SGleb Smirnoff 			$$ = $1;
38763b3a8eb9SGleb Smirnoff 		}
38773b3a8eb9SGleb Smirnoff 		;
38783b3a8eb9SGleb Smirnoff 
38793b3a8eb9SGleb Smirnoff uid_item	: uid				{
38803b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_uid));
38813b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
38823b3a8eb9SGleb Smirnoff 				err(1, "uid_item: calloc");
38833b3a8eb9SGleb Smirnoff 			$$->uid[0] = $1;
38843b3a8eb9SGleb Smirnoff 			$$->uid[1] = $1;
38853b3a8eb9SGleb Smirnoff 			$$->op = PF_OP_EQ;
38863b3a8eb9SGleb Smirnoff 			$$->next = NULL;
38873b3a8eb9SGleb Smirnoff 			$$->tail = $$;
38883b3a8eb9SGleb Smirnoff 		}
38893b3a8eb9SGleb Smirnoff 		| unaryop uid			{
38903b3a8eb9SGleb Smirnoff 			if ($2 == UID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
38913b3a8eb9SGleb Smirnoff 				yyerror("user unknown requires operator = or "
38923b3a8eb9SGleb Smirnoff 				    "!=");
38933b3a8eb9SGleb Smirnoff 				YYERROR;
38943b3a8eb9SGleb Smirnoff 			}
38953b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_uid));
38963b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
38973b3a8eb9SGleb Smirnoff 				err(1, "uid_item: calloc");
38983b3a8eb9SGleb Smirnoff 			$$->uid[0] = $2;
38993b3a8eb9SGleb Smirnoff 			$$->uid[1] = $2;
39003b3a8eb9SGleb Smirnoff 			$$->op = $1;
39013b3a8eb9SGleb Smirnoff 			$$->next = NULL;
39023b3a8eb9SGleb Smirnoff 			$$->tail = $$;
39033b3a8eb9SGleb Smirnoff 		}
39043b3a8eb9SGleb Smirnoff 		| uid PORTBINARY uid		{
39053b3a8eb9SGleb Smirnoff 			if ($1 == UID_MAX || $3 == UID_MAX) {
39063b3a8eb9SGleb Smirnoff 				yyerror("user unknown requires operator = or "
39073b3a8eb9SGleb Smirnoff 				    "!=");
39083b3a8eb9SGleb Smirnoff 				YYERROR;
39093b3a8eb9SGleb Smirnoff 			}
39103b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_uid));
39113b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
39123b3a8eb9SGleb Smirnoff 				err(1, "uid_item: calloc");
39133b3a8eb9SGleb Smirnoff 			$$->uid[0] = $1;
39143b3a8eb9SGleb Smirnoff 			$$->uid[1] = $3;
39153b3a8eb9SGleb Smirnoff 			$$->op = $2;
39163b3a8eb9SGleb Smirnoff 			$$->next = NULL;
39173b3a8eb9SGleb Smirnoff 			$$->tail = $$;
39183b3a8eb9SGleb Smirnoff 		}
39193b3a8eb9SGleb Smirnoff 		;
39203b3a8eb9SGleb Smirnoff 
39213b3a8eb9SGleb Smirnoff uid		: STRING			{
39223b3a8eb9SGleb Smirnoff 			if (!strcmp($1, "unknown"))
39233b3a8eb9SGleb Smirnoff 				$$ = UID_MAX;
39243b3a8eb9SGleb Smirnoff 			else {
39253b3a8eb9SGleb Smirnoff 				struct passwd	*pw;
39263b3a8eb9SGleb Smirnoff 
39273b3a8eb9SGleb Smirnoff 				if ((pw = getpwnam($1)) == NULL) {
39283b3a8eb9SGleb Smirnoff 					yyerror("unknown user %s", $1);
39293b3a8eb9SGleb Smirnoff 					free($1);
39303b3a8eb9SGleb Smirnoff 					YYERROR;
39313b3a8eb9SGleb Smirnoff 				}
39323b3a8eb9SGleb Smirnoff 				$$ = pw->pw_uid;
39333b3a8eb9SGleb Smirnoff 			}
39343b3a8eb9SGleb Smirnoff 			free($1);
39353b3a8eb9SGleb Smirnoff 		}
39363b3a8eb9SGleb Smirnoff 		| NUMBER			{
39373b3a8eb9SGleb Smirnoff 			if ($1 < 0 || $1 >= UID_MAX) {
39383b3a8eb9SGleb Smirnoff 				yyerror("illegal uid value %lu", $1);
39393b3a8eb9SGleb Smirnoff 				YYERROR;
39403b3a8eb9SGleb Smirnoff 			}
39413b3a8eb9SGleb Smirnoff 			$$ = $1;
39423b3a8eb9SGleb Smirnoff 		}
39433b3a8eb9SGleb Smirnoff 		;
39443b3a8eb9SGleb Smirnoff 
39453b3a8eb9SGleb Smirnoff gids		: gid_item			{ $$ = $1; }
39463b3a8eb9SGleb Smirnoff 		| '{' optnl gid_list '}'	{ $$ = $3; }
39473b3a8eb9SGleb Smirnoff 		;
39483b3a8eb9SGleb Smirnoff 
39493b3a8eb9SGleb Smirnoff gid_list	: gid_item optnl		{ $$ = $1; }
39503b3a8eb9SGleb Smirnoff 		| gid_list comma gid_item optnl	{
39513b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
39523b3a8eb9SGleb Smirnoff 			$1->tail = $3;
39533b3a8eb9SGleb Smirnoff 			$$ = $1;
39543b3a8eb9SGleb Smirnoff 		}
39553b3a8eb9SGleb Smirnoff 		;
39563b3a8eb9SGleb Smirnoff 
39573b3a8eb9SGleb Smirnoff gid_item	: gid				{
39583b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_gid));
39593b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
39603b3a8eb9SGleb Smirnoff 				err(1, "gid_item: calloc");
39613b3a8eb9SGleb Smirnoff 			$$->gid[0] = $1;
39623b3a8eb9SGleb Smirnoff 			$$->gid[1] = $1;
39633b3a8eb9SGleb Smirnoff 			$$->op = PF_OP_EQ;
39643b3a8eb9SGleb Smirnoff 			$$->next = NULL;
39653b3a8eb9SGleb Smirnoff 			$$->tail = $$;
39663b3a8eb9SGleb Smirnoff 		}
39673b3a8eb9SGleb Smirnoff 		| unaryop gid			{
39683b3a8eb9SGleb Smirnoff 			if ($2 == GID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
39693b3a8eb9SGleb Smirnoff 				yyerror("group unknown requires operator = or "
39703b3a8eb9SGleb Smirnoff 				    "!=");
39713b3a8eb9SGleb Smirnoff 				YYERROR;
39723b3a8eb9SGleb Smirnoff 			}
39733b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_gid));
39743b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
39753b3a8eb9SGleb Smirnoff 				err(1, "gid_item: calloc");
39763b3a8eb9SGleb Smirnoff 			$$->gid[0] = $2;
39773b3a8eb9SGleb Smirnoff 			$$->gid[1] = $2;
39783b3a8eb9SGleb Smirnoff 			$$->op = $1;
39793b3a8eb9SGleb Smirnoff 			$$->next = NULL;
39803b3a8eb9SGleb Smirnoff 			$$->tail = $$;
39813b3a8eb9SGleb Smirnoff 		}
39823b3a8eb9SGleb Smirnoff 		| gid PORTBINARY gid		{
39833b3a8eb9SGleb Smirnoff 			if ($1 == GID_MAX || $3 == GID_MAX) {
39843b3a8eb9SGleb Smirnoff 				yyerror("group unknown requires operator = or "
39853b3a8eb9SGleb Smirnoff 				    "!=");
39863b3a8eb9SGleb Smirnoff 				YYERROR;
39873b3a8eb9SGleb Smirnoff 			}
39883b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_gid));
39893b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
39903b3a8eb9SGleb Smirnoff 				err(1, "gid_item: calloc");
39913b3a8eb9SGleb Smirnoff 			$$->gid[0] = $1;
39923b3a8eb9SGleb Smirnoff 			$$->gid[1] = $3;
39933b3a8eb9SGleb Smirnoff 			$$->op = $2;
39943b3a8eb9SGleb Smirnoff 			$$->next = NULL;
39953b3a8eb9SGleb Smirnoff 			$$->tail = $$;
39963b3a8eb9SGleb Smirnoff 		}
39973b3a8eb9SGleb Smirnoff 		;
39983b3a8eb9SGleb Smirnoff 
39993b3a8eb9SGleb Smirnoff gid		: STRING			{
40003b3a8eb9SGleb Smirnoff 			if (!strcmp($1, "unknown"))
40013b3a8eb9SGleb Smirnoff 				$$ = GID_MAX;
40023b3a8eb9SGleb Smirnoff 			else {
40033b3a8eb9SGleb Smirnoff 				struct group	*grp;
40043b3a8eb9SGleb Smirnoff 
40053b3a8eb9SGleb Smirnoff 				if ((grp = getgrnam($1)) == NULL) {
40063b3a8eb9SGleb Smirnoff 					yyerror("unknown group %s", $1);
40073b3a8eb9SGleb Smirnoff 					free($1);
40083b3a8eb9SGleb Smirnoff 					YYERROR;
40093b3a8eb9SGleb Smirnoff 				}
40103b3a8eb9SGleb Smirnoff 				$$ = grp->gr_gid;
40113b3a8eb9SGleb Smirnoff 			}
40123b3a8eb9SGleb Smirnoff 			free($1);
40133b3a8eb9SGleb Smirnoff 		}
40143b3a8eb9SGleb Smirnoff 		| NUMBER			{
40153b3a8eb9SGleb Smirnoff 			if ($1 < 0 || $1 >= GID_MAX) {
40163b3a8eb9SGleb Smirnoff 				yyerror("illegal gid value %lu", $1);
40173b3a8eb9SGleb Smirnoff 				YYERROR;
40183b3a8eb9SGleb Smirnoff 			}
40193b3a8eb9SGleb Smirnoff 			$$ = $1;
40203b3a8eb9SGleb Smirnoff 		}
40213b3a8eb9SGleb Smirnoff 		;
40223b3a8eb9SGleb Smirnoff 
40233b3a8eb9SGleb Smirnoff flag		: STRING			{
40243b3a8eb9SGleb Smirnoff 			int	f;
40253b3a8eb9SGleb Smirnoff 
40263b3a8eb9SGleb Smirnoff 			if ((f = parse_flags($1)) < 0) {
40273b3a8eb9SGleb Smirnoff 				yyerror("bad flags %s", $1);
40283b3a8eb9SGleb Smirnoff 				free($1);
40293b3a8eb9SGleb Smirnoff 				YYERROR;
40303b3a8eb9SGleb Smirnoff 			}
40313b3a8eb9SGleb Smirnoff 			free($1);
40323b3a8eb9SGleb Smirnoff 			$$.b1 = f;
40333b3a8eb9SGleb Smirnoff 		}
40343b3a8eb9SGleb Smirnoff 		;
40353b3a8eb9SGleb Smirnoff 
40363b3a8eb9SGleb Smirnoff flags		: FLAGS flag '/' flag	{ $$.b1 = $2.b1; $$.b2 = $4.b1; }
40373b3a8eb9SGleb Smirnoff 		| FLAGS '/' flag	{ $$.b1 = 0; $$.b2 = $3.b1; }
40383b3a8eb9SGleb Smirnoff 		| FLAGS ANY		{ $$.b1 = 0; $$.b2 = 0; }
40393b3a8eb9SGleb Smirnoff 		;
40403b3a8eb9SGleb Smirnoff 
40413b3a8eb9SGleb Smirnoff icmpspec	: ICMPTYPE icmp_item			{ $$ = $2; }
40423b3a8eb9SGleb Smirnoff 		| ICMPTYPE '{' optnl icmp_list '}'	{ $$ = $4; }
40433b3a8eb9SGleb Smirnoff 		| ICMP6TYPE icmp6_item			{ $$ = $2; }
40443b3a8eb9SGleb Smirnoff 		| ICMP6TYPE '{' optnl icmp6_list '}'	{ $$ = $4; }
40453b3a8eb9SGleb Smirnoff 		;
40463b3a8eb9SGleb Smirnoff 
40473b3a8eb9SGleb Smirnoff icmp_list	: icmp_item optnl		{ $$ = $1; }
40483b3a8eb9SGleb Smirnoff 		| icmp_list comma icmp_item optnl {
40493b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
40503b3a8eb9SGleb Smirnoff 			$1->tail = $3;
40513b3a8eb9SGleb Smirnoff 			$$ = $1;
40523b3a8eb9SGleb Smirnoff 		}
40533b3a8eb9SGleb Smirnoff 		;
40543b3a8eb9SGleb Smirnoff 
40553b3a8eb9SGleb Smirnoff icmp6_list	: icmp6_item optnl		{ $$ = $1; }
40563b3a8eb9SGleb Smirnoff 		| icmp6_list comma icmp6_item optnl {
40573b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
40583b3a8eb9SGleb Smirnoff 			$1->tail = $3;
40593b3a8eb9SGleb Smirnoff 			$$ = $1;
40603b3a8eb9SGleb Smirnoff 		}
40613b3a8eb9SGleb Smirnoff 		;
40623b3a8eb9SGleb Smirnoff 
40633b3a8eb9SGleb Smirnoff icmp_item	: icmptype		{
40643b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_icmp));
40653b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
40663b3a8eb9SGleb Smirnoff 				err(1, "icmp_item: calloc");
40673b3a8eb9SGleb Smirnoff 			$$->type = $1;
40683b3a8eb9SGleb Smirnoff 			$$->code = 0;
40693b3a8eb9SGleb Smirnoff 			$$->proto = IPPROTO_ICMP;
40703b3a8eb9SGleb Smirnoff 			$$->next = NULL;
40713b3a8eb9SGleb Smirnoff 			$$->tail = $$;
40723b3a8eb9SGleb Smirnoff 		}
40733b3a8eb9SGleb Smirnoff 		| icmptype CODE STRING	{
40743b3a8eb9SGleb Smirnoff 			const struct icmpcodeent	*p;
40753b3a8eb9SGleb Smirnoff 
40763b3a8eb9SGleb Smirnoff 			if ((p = geticmpcodebyname($1-1, $3, AF_INET)) == NULL) {
40773b3a8eb9SGleb Smirnoff 				yyerror("unknown icmp-code %s", $3);
40783b3a8eb9SGleb Smirnoff 				free($3);
40793b3a8eb9SGleb Smirnoff 				YYERROR;
40803b3a8eb9SGleb Smirnoff 			}
40813b3a8eb9SGleb Smirnoff 
40823b3a8eb9SGleb Smirnoff 			free($3);
40833b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_icmp));
40843b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
40853b3a8eb9SGleb Smirnoff 				err(1, "icmp_item: calloc");
40863b3a8eb9SGleb Smirnoff 			$$->type = $1;
40873b3a8eb9SGleb Smirnoff 			$$->code = p->code + 1;
40883b3a8eb9SGleb Smirnoff 			$$->proto = IPPROTO_ICMP;
40893b3a8eb9SGleb Smirnoff 			$$->next = NULL;
40903b3a8eb9SGleb Smirnoff 			$$->tail = $$;
40913b3a8eb9SGleb Smirnoff 		}
40923b3a8eb9SGleb Smirnoff 		| icmptype CODE NUMBER	{
40933b3a8eb9SGleb Smirnoff 			if ($3 < 0 || $3 > 255) {
40943b3a8eb9SGleb Smirnoff 				yyerror("illegal icmp-code %lu", $3);
40953b3a8eb9SGleb Smirnoff 				YYERROR;
40963b3a8eb9SGleb Smirnoff 			}
40973b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_icmp));
40983b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
40993b3a8eb9SGleb Smirnoff 				err(1, "icmp_item: calloc");
41003b3a8eb9SGleb Smirnoff 			$$->type = $1;
41013b3a8eb9SGleb Smirnoff 			$$->code = $3 + 1;
41023b3a8eb9SGleb Smirnoff 			$$->proto = IPPROTO_ICMP;
41033b3a8eb9SGleb Smirnoff 			$$->next = NULL;
41043b3a8eb9SGleb Smirnoff 			$$->tail = $$;
41053b3a8eb9SGleb Smirnoff 		}
41063b3a8eb9SGleb Smirnoff 		;
41073b3a8eb9SGleb Smirnoff 
41083b3a8eb9SGleb Smirnoff icmp6_item	: icmp6type		{
41093b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_icmp));
41103b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
41113b3a8eb9SGleb Smirnoff 				err(1, "icmp_item: calloc");
41123b3a8eb9SGleb Smirnoff 			$$->type = $1;
41133b3a8eb9SGleb Smirnoff 			$$->code = 0;
41143b3a8eb9SGleb Smirnoff 			$$->proto = IPPROTO_ICMPV6;
41153b3a8eb9SGleb Smirnoff 			$$->next = NULL;
41163b3a8eb9SGleb Smirnoff 			$$->tail = $$;
41173b3a8eb9SGleb Smirnoff 		}
41183b3a8eb9SGleb Smirnoff 		| icmp6type CODE STRING	{
41193b3a8eb9SGleb Smirnoff 			const struct icmpcodeent	*p;
41203b3a8eb9SGleb Smirnoff 
41213b3a8eb9SGleb Smirnoff 			if ((p = geticmpcodebyname($1-1, $3, AF_INET6)) == NULL) {
41223b3a8eb9SGleb Smirnoff 				yyerror("unknown icmp6-code %s", $3);
41233b3a8eb9SGleb Smirnoff 				free($3);
41243b3a8eb9SGleb Smirnoff 				YYERROR;
41253b3a8eb9SGleb Smirnoff 			}
41263b3a8eb9SGleb Smirnoff 			free($3);
41273b3a8eb9SGleb Smirnoff 
41283b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_icmp));
41293b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
41303b3a8eb9SGleb Smirnoff 				err(1, "icmp_item: calloc");
41313b3a8eb9SGleb Smirnoff 			$$->type = $1;
41323b3a8eb9SGleb Smirnoff 			$$->code = p->code + 1;
41333b3a8eb9SGleb Smirnoff 			$$->proto = IPPROTO_ICMPV6;
41343b3a8eb9SGleb Smirnoff 			$$->next = NULL;
41353b3a8eb9SGleb Smirnoff 			$$->tail = $$;
41363b3a8eb9SGleb Smirnoff 		}
41373b3a8eb9SGleb Smirnoff 		| icmp6type CODE NUMBER	{
41383b3a8eb9SGleb Smirnoff 			if ($3 < 0 || $3 > 255) {
41393b3a8eb9SGleb Smirnoff 				yyerror("illegal icmp-code %lu", $3);
41403b3a8eb9SGleb Smirnoff 				YYERROR;
41413b3a8eb9SGleb Smirnoff 			}
41423b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_icmp));
41433b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
41443b3a8eb9SGleb Smirnoff 				err(1, "icmp_item: calloc");
41453b3a8eb9SGleb Smirnoff 			$$->type = $1;
41463b3a8eb9SGleb Smirnoff 			$$->code = $3 + 1;
41473b3a8eb9SGleb Smirnoff 			$$->proto = IPPROTO_ICMPV6;
41483b3a8eb9SGleb Smirnoff 			$$->next = NULL;
41493b3a8eb9SGleb Smirnoff 			$$->tail = $$;
41503b3a8eb9SGleb Smirnoff 		}
41513b3a8eb9SGleb Smirnoff 		;
41523b3a8eb9SGleb Smirnoff 
41533b3a8eb9SGleb Smirnoff icmptype	: STRING			{
41543b3a8eb9SGleb Smirnoff 			const struct icmptypeent	*p;
41553b3a8eb9SGleb Smirnoff 
41563b3a8eb9SGleb Smirnoff 			if ((p = geticmptypebyname($1, AF_INET)) == NULL) {
41573b3a8eb9SGleb Smirnoff 				yyerror("unknown icmp-type %s", $1);
41583b3a8eb9SGleb Smirnoff 				free($1);
41593b3a8eb9SGleb Smirnoff 				YYERROR;
41603b3a8eb9SGleb Smirnoff 			}
41613b3a8eb9SGleb Smirnoff 			$$ = p->type + 1;
41623b3a8eb9SGleb Smirnoff 			free($1);
41633b3a8eb9SGleb Smirnoff 		}
41643b3a8eb9SGleb Smirnoff 		| NUMBER			{
41653b3a8eb9SGleb Smirnoff 			if ($1 < 0 || $1 > 255) {
41663b3a8eb9SGleb Smirnoff 				yyerror("illegal icmp-type %lu", $1);
41673b3a8eb9SGleb Smirnoff 				YYERROR;
41683b3a8eb9SGleb Smirnoff 			}
41693b3a8eb9SGleb Smirnoff 			$$ = $1 + 1;
41703b3a8eb9SGleb Smirnoff 		}
41713b3a8eb9SGleb Smirnoff 		;
41723b3a8eb9SGleb Smirnoff 
41733b3a8eb9SGleb Smirnoff icmp6type	: STRING			{
41743b3a8eb9SGleb Smirnoff 			const struct icmptypeent	*p;
41753b3a8eb9SGleb Smirnoff 
41763b3a8eb9SGleb Smirnoff 			if ((p = geticmptypebyname($1, AF_INET6)) ==
41773b3a8eb9SGleb Smirnoff 			    NULL) {
41783b3a8eb9SGleb Smirnoff 				yyerror("unknown icmp6-type %s", $1);
41793b3a8eb9SGleb Smirnoff 				free($1);
41803b3a8eb9SGleb Smirnoff 				YYERROR;
41813b3a8eb9SGleb Smirnoff 			}
41823b3a8eb9SGleb Smirnoff 			$$ = p->type + 1;
41833b3a8eb9SGleb Smirnoff 			free($1);
41843b3a8eb9SGleb Smirnoff 		}
41853b3a8eb9SGleb Smirnoff 		| NUMBER			{
41863b3a8eb9SGleb Smirnoff 			if ($1 < 0 || $1 > 255) {
41873b3a8eb9SGleb Smirnoff 				yyerror("illegal icmp6-type %lu", $1);
41883b3a8eb9SGleb Smirnoff 				YYERROR;
41893b3a8eb9SGleb Smirnoff 			}
41903b3a8eb9SGleb Smirnoff 			$$ = $1 + 1;
41913b3a8eb9SGleb Smirnoff 		}
41923b3a8eb9SGleb Smirnoff 		;
41933b3a8eb9SGleb Smirnoff 
41943b3a8eb9SGleb Smirnoff tos	: STRING			{
41951f495578SKristof Provost 			int val;
41961f495578SKristof Provost 			char *end;
41971f495578SKristof Provost 
41981f495578SKristof Provost 			if (map_tos($1, &val))
41991f495578SKristof Provost 				$$ = val;
42001f495578SKristof Provost 			else if ($1[0] == '0' && $1[1] == 'x') {
42011f495578SKristof Provost 				errno = 0;
42021f495578SKristof Provost 				$$ = strtoul($1, &end, 16);
42031f495578SKristof Provost 				if (errno || *end != '\0')
42041f495578SKristof Provost 					$$ = 256;
42051f495578SKristof Provost 			} else
42060cd7a91aSKristof Provost 				$$ = 256;		/* flag bad argument */
42070cd7a91aSKristof Provost 			if ($$ < 0 || $$ > 255) {
42083b3a8eb9SGleb Smirnoff 				yyerror("illegal tos value %s", $1);
42093b3a8eb9SGleb Smirnoff 				free($1);
42103b3a8eb9SGleb Smirnoff 				YYERROR;
42113b3a8eb9SGleb Smirnoff 			}
42123b3a8eb9SGleb Smirnoff 			free($1);
42133b3a8eb9SGleb Smirnoff 		}
42143b3a8eb9SGleb Smirnoff 		| NUMBER			{
42153b3a8eb9SGleb Smirnoff 			$$ = $1;
42160cd7a91aSKristof Provost 			if ($$ < 0 || $$ > 255) {
4217*6562157dSKristof Provost 				yyerror("illegal tos value %lu", $1);
42183b3a8eb9SGleb Smirnoff 				YYERROR;
42193b3a8eb9SGleb Smirnoff 			}
42203b3a8eb9SGleb Smirnoff 		}
42213b3a8eb9SGleb Smirnoff 		;
42223b3a8eb9SGleb Smirnoff 
42233b3a8eb9SGleb Smirnoff sourcetrack	: SOURCETRACK		{ $$ = PF_SRCTRACK; }
42243b3a8eb9SGleb Smirnoff 		| SOURCETRACK GLOBAL	{ $$ = PF_SRCTRACK_GLOBAL; }
42253b3a8eb9SGleb Smirnoff 		| SOURCETRACK RULE	{ $$ = PF_SRCTRACK_RULE; }
42263b3a8eb9SGleb Smirnoff 		;
42273b3a8eb9SGleb Smirnoff 
42283b3a8eb9SGleb Smirnoff statelock	: IFBOUND {
42293b3a8eb9SGleb Smirnoff 			$$ = PFRULE_IFBOUND;
42303b3a8eb9SGleb Smirnoff 		}
42313b3a8eb9SGleb Smirnoff 		| FLOATING {
42323b3a8eb9SGleb Smirnoff 			$$ = 0;
42333b3a8eb9SGleb Smirnoff 		}
42343b3a8eb9SGleb Smirnoff 		;
42353b3a8eb9SGleb Smirnoff 
42363b3a8eb9SGleb Smirnoff keep		: NO STATE			{
42373b3a8eb9SGleb Smirnoff 			$$.action = 0;
42383b3a8eb9SGleb Smirnoff 			$$.options = NULL;
42393b3a8eb9SGleb Smirnoff 		}
42403b3a8eb9SGleb Smirnoff 		| KEEP STATE state_opt_spec	{
42413b3a8eb9SGleb Smirnoff 			$$.action = PF_STATE_NORMAL;
42423b3a8eb9SGleb Smirnoff 			$$.options = $3;
42433b3a8eb9SGleb Smirnoff 		}
42443b3a8eb9SGleb Smirnoff 		| MODULATE STATE state_opt_spec {
42453b3a8eb9SGleb Smirnoff 			$$.action = PF_STATE_MODULATE;
42463b3a8eb9SGleb Smirnoff 			$$.options = $3;
42473b3a8eb9SGleb Smirnoff 		}
42483b3a8eb9SGleb Smirnoff 		| SYNPROXY STATE state_opt_spec {
42493b3a8eb9SGleb Smirnoff 			$$.action = PF_STATE_SYNPROXY;
42503b3a8eb9SGleb Smirnoff 			$$.options = $3;
42513b3a8eb9SGleb Smirnoff 		}
42523b3a8eb9SGleb Smirnoff 		;
42533b3a8eb9SGleb Smirnoff 
42543b3a8eb9SGleb Smirnoff flush		: /* empty */			{ $$ = 0; }
42553b3a8eb9SGleb Smirnoff 		| FLUSH				{ $$ = PF_FLUSH; }
42563b3a8eb9SGleb Smirnoff 		| FLUSH GLOBAL			{
42573b3a8eb9SGleb Smirnoff 			$$ = PF_FLUSH | PF_FLUSH_GLOBAL;
42583b3a8eb9SGleb Smirnoff 		}
42593b3a8eb9SGleb Smirnoff 		;
42603b3a8eb9SGleb Smirnoff 
42613b3a8eb9SGleb Smirnoff state_opt_spec	: '(' state_opt_list ')'	{ $$ = $2; }
42623b3a8eb9SGleb Smirnoff 		| /* empty */			{ $$ = NULL; }
42633b3a8eb9SGleb Smirnoff 		;
42643b3a8eb9SGleb Smirnoff 
42653b3a8eb9SGleb Smirnoff state_opt_list	: state_opt_item		{ $$ = $1; }
42663b3a8eb9SGleb Smirnoff 		| state_opt_list comma state_opt_item {
42673b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
42683b3a8eb9SGleb Smirnoff 			$1->tail = $3;
42693b3a8eb9SGleb Smirnoff 			$$ = $1;
42703b3a8eb9SGleb Smirnoff 		}
42713b3a8eb9SGleb Smirnoff 		;
42723b3a8eb9SGleb Smirnoff 
42733b3a8eb9SGleb Smirnoff state_opt_item	: MAXIMUM NUMBER		{
42743b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > UINT_MAX) {
42753b3a8eb9SGleb Smirnoff 				yyerror("only positive values permitted");
42763b3a8eb9SGleb Smirnoff 				YYERROR;
42773b3a8eb9SGleb Smirnoff 			}
42783b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
42793b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
42803b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
42813b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_MAX;
42823b3a8eb9SGleb Smirnoff 			$$->data.max_states = $2;
42833b3a8eb9SGleb Smirnoff 			$$->next = NULL;
42843b3a8eb9SGleb Smirnoff 			$$->tail = $$;
42853b3a8eb9SGleb Smirnoff 		}
42863b3a8eb9SGleb Smirnoff 		| NOSYNC				{
42873b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
42883b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
42893b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
42903b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_NOSYNC;
42913b3a8eb9SGleb Smirnoff 			$$->next = NULL;
42923b3a8eb9SGleb Smirnoff 			$$->tail = $$;
42933b3a8eb9SGleb Smirnoff 		}
42943b3a8eb9SGleb Smirnoff 		| MAXSRCSTATES NUMBER			{
42953b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > UINT_MAX) {
42963b3a8eb9SGleb Smirnoff 				yyerror("only positive values permitted");
42973b3a8eb9SGleb Smirnoff 				YYERROR;
42983b3a8eb9SGleb Smirnoff 			}
42993b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
43003b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
43013b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
43023b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_MAX_SRC_STATES;
43033b3a8eb9SGleb Smirnoff 			$$->data.max_src_states = $2;
43043b3a8eb9SGleb Smirnoff 			$$->next = NULL;
43053b3a8eb9SGleb Smirnoff 			$$->tail = $$;
43063b3a8eb9SGleb Smirnoff 		}
43073b3a8eb9SGleb Smirnoff 		| MAXSRCCONN NUMBER			{
43083b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > UINT_MAX) {
43093b3a8eb9SGleb Smirnoff 				yyerror("only positive values permitted");
43103b3a8eb9SGleb Smirnoff 				YYERROR;
43113b3a8eb9SGleb Smirnoff 			}
43123b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
43133b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
43143b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
43153b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_MAX_SRC_CONN;
43163b3a8eb9SGleb Smirnoff 			$$->data.max_src_conn = $2;
43173b3a8eb9SGleb Smirnoff 			$$->next = NULL;
43183b3a8eb9SGleb Smirnoff 			$$->tail = $$;
43193b3a8eb9SGleb Smirnoff 		}
43203b3a8eb9SGleb Smirnoff 		| MAXSRCCONNRATE NUMBER '/' NUMBER	{
43213b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > UINT_MAX ||
43223b3a8eb9SGleb Smirnoff 			    $4 < 0 || $4 > UINT_MAX) {
43233b3a8eb9SGleb Smirnoff 				yyerror("only positive values permitted");
43243b3a8eb9SGleb Smirnoff 				YYERROR;
43253b3a8eb9SGleb Smirnoff 			}
43263b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
43273b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
43283b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
43293b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_MAX_SRC_CONN_RATE;
43303b3a8eb9SGleb Smirnoff 			$$->data.max_src_conn_rate.limit = $2;
43313b3a8eb9SGleb Smirnoff 			$$->data.max_src_conn_rate.seconds = $4;
43323b3a8eb9SGleb Smirnoff 			$$->next = NULL;
43333b3a8eb9SGleb Smirnoff 			$$->tail = $$;
43343b3a8eb9SGleb Smirnoff 		}
43353b3a8eb9SGleb Smirnoff 		| OVERLOAD '<' STRING '>' flush		{
43363b3a8eb9SGleb Smirnoff 			if (strlen($3) >= PF_TABLE_NAME_SIZE) {
43373b3a8eb9SGleb Smirnoff 				yyerror("table name '%s' too long", $3);
43383b3a8eb9SGleb Smirnoff 				free($3);
43393b3a8eb9SGleb Smirnoff 				YYERROR;
43403b3a8eb9SGleb Smirnoff 			}
43413b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
43423b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
43433b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
43443b3a8eb9SGleb Smirnoff 			if (strlcpy($$->data.overload.tblname, $3,
43453b3a8eb9SGleb Smirnoff 			    PF_TABLE_NAME_SIZE) >= PF_TABLE_NAME_SIZE)
43463b3a8eb9SGleb Smirnoff 				errx(1, "state_opt_item: strlcpy");
43473b3a8eb9SGleb Smirnoff 			free($3);
43483b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_OVERLOAD;
43493b3a8eb9SGleb Smirnoff 			$$->data.overload.flush = $5;
43503b3a8eb9SGleb Smirnoff 			$$->next = NULL;
43513b3a8eb9SGleb Smirnoff 			$$->tail = $$;
43523b3a8eb9SGleb Smirnoff 		}
43533b3a8eb9SGleb Smirnoff 		| MAXSRCNODES NUMBER			{
43543b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > UINT_MAX) {
43553b3a8eb9SGleb Smirnoff 				yyerror("only positive values permitted");
43563b3a8eb9SGleb Smirnoff 				YYERROR;
43573b3a8eb9SGleb Smirnoff 			}
43583b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
43593b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
43603b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
43613b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_MAX_SRC_NODES;
43623b3a8eb9SGleb Smirnoff 			$$->data.max_src_nodes = $2;
43633b3a8eb9SGleb Smirnoff 			$$->next = NULL;
43643b3a8eb9SGleb Smirnoff 			$$->tail = $$;
43653b3a8eb9SGleb Smirnoff 		}
43663b3a8eb9SGleb Smirnoff 		| sourcetrack {
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_SRCTRACK;
43713b3a8eb9SGleb Smirnoff 			$$->data.src_track = $1;
43723b3a8eb9SGleb Smirnoff 			$$->next = NULL;
43733b3a8eb9SGleb Smirnoff 			$$->tail = $$;
43743b3a8eb9SGleb Smirnoff 		}
43753b3a8eb9SGleb Smirnoff 		| statelock {
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_STATELOCK;
43803b3a8eb9SGleb Smirnoff 			$$->data.statelock = $1;
43813b3a8eb9SGleb Smirnoff 			$$->next = NULL;
43823b3a8eb9SGleb Smirnoff 			$$->tail = $$;
43833b3a8eb9SGleb Smirnoff 		}
43843b3a8eb9SGleb Smirnoff 		| SLOPPY {
43853b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
43863b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
43873b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
43883b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_SLOPPY;
43893b3a8eb9SGleb Smirnoff 			$$->next = NULL;
43903b3a8eb9SGleb Smirnoff 			$$->tail = $$;
43913b3a8eb9SGleb Smirnoff 		}
4392baf9b6d0SKristof Provost 		| PFLOW {
4393baf9b6d0SKristof Provost 			$$ = calloc(1, sizeof(struct node_state_opt));
4394baf9b6d0SKristof Provost 			if ($$ == NULL)
4395baf9b6d0SKristof Provost 				err(1, "state_opt_item: calloc");
4396baf9b6d0SKristof Provost 			$$->type = PF_STATE_OPT_PFLOW;
4397baf9b6d0SKristof Provost 			$$->next = NULL;
4398baf9b6d0SKristof Provost 			$$->tail = $$;
4399baf9b6d0SKristof Provost 		}
44003b3a8eb9SGleb Smirnoff 		| STRING NUMBER			{
44013b3a8eb9SGleb Smirnoff 			int	i;
44023b3a8eb9SGleb Smirnoff 
44033b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > UINT_MAX) {
44043b3a8eb9SGleb Smirnoff 				yyerror("only positive values permitted");
44053b3a8eb9SGleb Smirnoff 				YYERROR;
44063b3a8eb9SGleb Smirnoff 			}
44073b3a8eb9SGleb Smirnoff 			for (i = 0; pf_timeouts[i].name &&
44083b3a8eb9SGleb Smirnoff 			    strcmp(pf_timeouts[i].name, $1); ++i)
44093b3a8eb9SGleb Smirnoff 				;	/* nothing */
44103b3a8eb9SGleb Smirnoff 			if (!pf_timeouts[i].name) {
44113b3a8eb9SGleb Smirnoff 				yyerror("illegal timeout name %s", $1);
44123b3a8eb9SGleb Smirnoff 				free($1);
44133b3a8eb9SGleb Smirnoff 				YYERROR;
44143b3a8eb9SGleb Smirnoff 			}
44153b3a8eb9SGleb Smirnoff 			if (strchr(pf_timeouts[i].name, '.') == NULL) {
44163b3a8eb9SGleb Smirnoff 				yyerror("illegal state timeout %s", $1);
44173b3a8eb9SGleb Smirnoff 				free($1);
44183b3a8eb9SGleb Smirnoff 				YYERROR;
44193b3a8eb9SGleb Smirnoff 			}
44203b3a8eb9SGleb Smirnoff 			free($1);
44213b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_state_opt));
44223b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
44233b3a8eb9SGleb Smirnoff 				err(1, "state_opt_item: calloc");
44243b3a8eb9SGleb Smirnoff 			$$->type = PF_STATE_OPT_TIMEOUT;
44253b3a8eb9SGleb Smirnoff 			$$->data.timeout.number = pf_timeouts[i].timeout;
44263b3a8eb9SGleb Smirnoff 			$$->data.timeout.seconds = $2;
44273b3a8eb9SGleb Smirnoff 			$$->next = NULL;
44283b3a8eb9SGleb Smirnoff 			$$->tail = $$;
44293b3a8eb9SGleb Smirnoff 		}
44303b3a8eb9SGleb Smirnoff 		;
44313b3a8eb9SGleb Smirnoff 
44323b3a8eb9SGleb Smirnoff label		: LABEL STRING			{
44333b3a8eb9SGleb Smirnoff 			$$ = $2;
44343b3a8eb9SGleb Smirnoff 		}
44353b3a8eb9SGleb Smirnoff 		;
44363b3a8eb9SGleb Smirnoff 
44372b29ceb8SKristof Provost etherqname	: QUEUE STRING				{
44382b29ceb8SKristof Provost 			$$.qname = $2;
44392b29ceb8SKristof Provost 		}
44402b29ceb8SKristof Provost 		| QUEUE '(' STRING ')'			{
44412b29ceb8SKristof Provost 			$$.qname = $3;
44422b29ceb8SKristof Provost 		}
44432b29ceb8SKristof Provost 		;
44442b29ceb8SKristof Provost 
44453b3a8eb9SGleb Smirnoff qname		: QUEUE STRING				{
44463b3a8eb9SGleb Smirnoff 			$$.qname = $2;
44473b3a8eb9SGleb Smirnoff 			$$.pqname = NULL;
44483b3a8eb9SGleb Smirnoff 		}
44493b3a8eb9SGleb Smirnoff 		| QUEUE '(' STRING ')'			{
44503b3a8eb9SGleb Smirnoff 			$$.qname = $3;
44513b3a8eb9SGleb Smirnoff 			$$.pqname = NULL;
44523b3a8eb9SGleb Smirnoff 		}
44533b3a8eb9SGleb Smirnoff 		| QUEUE '(' STRING comma STRING ')'	{
44543b3a8eb9SGleb Smirnoff 			$$.qname = $3;
44553b3a8eb9SGleb Smirnoff 			$$.pqname = $5;
44563b3a8eb9SGleb Smirnoff 		}
44573b3a8eb9SGleb Smirnoff 		;
44583b3a8eb9SGleb Smirnoff 
44593b3a8eb9SGleb Smirnoff no		: /* empty */			{ $$ = 0; }
44603b3a8eb9SGleb Smirnoff 		| NO				{ $$ = 1; }
44613b3a8eb9SGleb Smirnoff 		;
44623b3a8eb9SGleb Smirnoff 
44633b3a8eb9SGleb Smirnoff portstar	: numberstring			{
44643b3a8eb9SGleb Smirnoff 			if (parseport($1, &$$, PPORT_RANGE|PPORT_STAR) == -1) {
44653b3a8eb9SGleb Smirnoff 				free($1);
44663b3a8eb9SGleb Smirnoff 				YYERROR;
44673b3a8eb9SGleb Smirnoff 			}
44683b3a8eb9SGleb Smirnoff 			free($1);
44693b3a8eb9SGleb Smirnoff 		}
44703b3a8eb9SGleb Smirnoff 		;
44713b3a8eb9SGleb Smirnoff 
44723b3a8eb9SGleb Smirnoff redirspec	: host				{ $$ = $1; }
44733b3a8eb9SGleb Smirnoff 		| '{' optnl redir_host_list '}'	{ $$ = $3; }
44743b3a8eb9SGleb Smirnoff 		;
44753b3a8eb9SGleb Smirnoff 
44763b3a8eb9SGleb Smirnoff redir_host_list	: host optnl			{ $$ = $1; }
44773b3a8eb9SGleb Smirnoff 		| redir_host_list comma host optnl {
44783b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
44793b3a8eb9SGleb Smirnoff 			$1->tail = $3->tail;
44803b3a8eb9SGleb Smirnoff 			$$ = $1;
44813b3a8eb9SGleb Smirnoff 		}
44823b3a8eb9SGleb Smirnoff 		;
44833b3a8eb9SGleb Smirnoff 
44843b3a8eb9SGleb Smirnoff redirpool	: /* empty */			{ $$ = NULL; }
44853b3a8eb9SGleb Smirnoff 		| ARROW redirspec		{
44863b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct redirection));
44873b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
44883b3a8eb9SGleb Smirnoff 				err(1, "redirection: calloc");
44893b3a8eb9SGleb Smirnoff 			$$->host = $2;
44903b3a8eb9SGleb Smirnoff 			$$->rport.a = $$->rport.b = $$->rport.t = 0;
44913b3a8eb9SGleb Smirnoff 		}
44923b3a8eb9SGleb Smirnoff 		| ARROW redirspec PORT portstar	{
44933b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct redirection));
44943b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
44953b3a8eb9SGleb Smirnoff 				err(1, "redirection: calloc");
44963b3a8eb9SGleb Smirnoff 			$$->host = $2;
44973b3a8eb9SGleb Smirnoff 			$$->rport = $4;
44983b3a8eb9SGleb Smirnoff 		}
44993b3a8eb9SGleb Smirnoff 		;
45003b3a8eb9SGleb Smirnoff 
45013b3a8eb9SGleb Smirnoff hashkey		: /* empty */
45023b3a8eb9SGleb Smirnoff 		{
45033b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct pf_poolhashkey));
45043b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
45053b3a8eb9SGleb Smirnoff 				err(1, "hashkey: calloc");
45063b3a8eb9SGleb Smirnoff 			$$->key32[0] = arc4random();
45073b3a8eb9SGleb Smirnoff 			$$->key32[1] = arc4random();
45083b3a8eb9SGleb Smirnoff 			$$->key32[2] = arc4random();
45093b3a8eb9SGleb Smirnoff 			$$->key32[3] = arc4random();
45103b3a8eb9SGleb Smirnoff 		}
45113b3a8eb9SGleb Smirnoff 		| string
45123b3a8eb9SGleb Smirnoff 		{
45133b3a8eb9SGleb Smirnoff 			if (!strncmp($1, "0x", 2)) {
45143b3a8eb9SGleb Smirnoff 				if (strlen($1) != 34) {
45153b3a8eb9SGleb Smirnoff 					free($1);
45163b3a8eb9SGleb Smirnoff 					yyerror("hex key must be 128 bits "
45173b3a8eb9SGleb Smirnoff 						"(32 hex digits) long");
45183b3a8eb9SGleb Smirnoff 					YYERROR;
45193b3a8eb9SGleb Smirnoff 				}
45203b3a8eb9SGleb Smirnoff 				$$ = calloc(1, sizeof(struct pf_poolhashkey));
45213b3a8eb9SGleb Smirnoff 				if ($$ == NULL)
45223b3a8eb9SGleb Smirnoff 					err(1, "hashkey: calloc");
45233b3a8eb9SGleb Smirnoff 
45243b3a8eb9SGleb Smirnoff 				if (sscanf($1, "0x%8x%8x%8x%8x",
45253b3a8eb9SGleb Smirnoff 				    &$$->key32[0], &$$->key32[1],
45263b3a8eb9SGleb Smirnoff 				    &$$->key32[2], &$$->key32[3]) != 4) {
45273b3a8eb9SGleb Smirnoff 					free($$);
45283b3a8eb9SGleb Smirnoff 					free($1);
45293b3a8eb9SGleb Smirnoff 					yyerror("invalid hex key");
45303b3a8eb9SGleb Smirnoff 					YYERROR;
45313b3a8eb9SGleb Smirnoff 				}
45323b3a8eb9SGleb Smirnoff 			} else {
45333b3a8eb9SGleb Smirnoff 				MD5_CTX	context;
45343b3a8eb9SGleb Smirnoff 
45353b3a8eb9SGleb Smirnoff 				$$ = calloc(1, sizeof(struct pf_poolhashkey));
45363b3a8eb9SGleb Smirnoff 				if ($$ == NULL)
45373b3a8eb9SGleb Smirnoff 					err(1, "hashkey: calloc");
45383b3a8eb9SGleb Smirnoff 				MD5Init(&context);
45393b3a8eb9SGleb Smirnoff 				MD5Update(&context, (unsigned char *)$1,
45403b3a8eb9SGleb Smirnoff 				    strlen($1));
45413b3a8eb9SGleb Smirnoff 				MD5Final((unsigned char *)$$, &context);
45423b3a8eb9SGleb Smirnoff 				HTONL($$->key32[0]);
45433b3a8eb9SGleb Smirnoff 				HTONL($$->key32[1]);
45443b3a8eb9SGleb Smirnoff 				HTONL($$->key32[2]);
45453b3a8eb9SGleb Smirnoff 				HTONL($$->key32[3]);
45463b3a8eb9SGleb Smirnoff 			}
45473b3a8eb9SGleb Smirnoff 			free($1);
45483b3a8eb9SGleb Smirnoff 		}
45493b3a8eb9SGleb Smirnoff 		;
45503b3a8eb9SGleb Smirnoff 
45513b3a8eb9SGleb Smirnoff pool_opts	:	{ bzero(&pool_opts, sizeof pool_opts); }
45523b3a8eb9SGleb Smirnoff 		    pool_opts_l
45533b3a8eb9SGleb Smirnoff 			{ $$ = pool_opts; }
45543b3a8eb9SGleb Smirnoff 		| /* empty */	{
45553b3a8eb9SGleb Smirnoff 			bzero(&pool_opts, sizeof pool_opts);
45563b3a8eb9SGleb Smirnoff 			$$ = pool_opts;
45573b3a8eb9SGleb Smirnoff 		}
45583b3a8eb9SGleb Smirnoff 		;
45593b3a8eb9SGleb Smirnoff 
45603b3a8eb9SGleb Smirnoff pool_opts_l	: pool_opts_l pool_opt
45613b3a8eb9SGleb Smirnoff 		| pool_opt
45623b3a8eb9SGleb Smirnoff 		;
45633b3a8eb9SGleb Smirnoff 
45643b3a8eb9SGleb Smirnoff pool_opt	: BITMASK	{
45653b3a8eb9SGleb Smirnoff 			if (pool_opts.type) {
45663b3a8eb9SGleb Smirnoff 				yyerror("pool type cannot be redefined");
45673b3a8eb9SGleb Smirnoff 				YYERROR;
45683b3a8eb9SGleb Smirnoff 			}
45693b3a8eb9SGleb Smirnoff 			pool_opts.type =  PF_POOL_BITMASK;
45703b3a8eb9SGleb Smirnoff 		}
45713b3a8eb9SGleb Smirnoff 		| RANDOM	{
45723b3a8eb9SGleb Smirnoff 			if (pool_opts.type) {
45733b3a8eb9SGleb Smirnoff 				yyerror("pool type cannot be redefined");
45743b3a8eb9SGleb Smirnoff 				YYERROR;
45753b3a8eb9SGleb Smirnoff 			}
45763b3a8eb9SGleb Smirnoff 			pool_opts.type = PF_POOL_RANDOM;
45773b3a8eb9SGleb Smirnoff 		}
45783b3a8eb9SGleb Smirnoff 		| SOURCEHASH hashkey {
45793b3a8eb9SGleb Smirnoff 			if (pool_opts.type) {
45803b3a8eb9SGleb Smirnoff 				yyerror("pool type cannot be redefined");
45813b3a8eb9SGleb Smirnoff 				YYERROR;
45823b3a8eb9SGleb Smirnoff 			}
45833b3a8eb9SGleb Smirnoff 			pool_opts.type = PF_POOL_SRCHASH;
45843b3a8eb9SGleb Smirnoff 			pool_opts.key = $2;
45853b3a8eb9SGleb Smirnoff 		}
45863b3a8eb9SGleb Smirnoff 		| ROUNDROBIN	{
45873b3a8eb9SGleb Smirnoff 			if (pool_opts.type) {
45883b3a8eb9SGleb Smirnoff 				yyerror("pool type cannot be redefined");
45893b3a8eb9SGleb Smirnoff 				YYERROR;
45903b3a8eb9SGleb Smirnoff 			}
45913b3a8eb9SGleb Smirnoff 			pool_opts.type = PF_POOL_ROUNDROBIN;
45923b3a8eb9SGleb Smirnoff 		}
45933b3a8eb9SGleb Smirnoff 		| STATICPORT	{
45943b3a8eb9SGleb Smirnoff 			if (pool_opts.staticport) {
45953b3a8eb9SGleb Smirnoff 				yyerror("static-port cannot be redefined");
45963b3a8eb9SGleb Smirnoff 				YYERROR;
45973b3a8eb9SGleb Smirnoff 			}
45983b3a8eb9SGleb Smirnoff 			pool_opts.staticport = 1;
45993b3a8eb9SGleb Smirnoff 		}
46003b3a8eb9SGleb Smirnoff 		| STICKYADDRESS	{
46011e73fbd8SFranco Fichtner 			if (pool_opts.marker & POM_STICKYADDRESS) {
46023b3a8eb9SGleb Smirnoff 				yyerror("sticky-address cannot be redefined");
46033b3a8eb9SGleb Smirnoff 				YYERROR;
46043b3a8eb9SGleb Smirnoff 			}
46053b3a8eb9SGleb Smirnoff 			pool_opts.marker |= POM_STICKYADDRESS;
46063b3a8eb9SGleb Smirnoff 			pool_opts.opts |= PF_POOL_STICKYADDR;
46073b3a8eb9SGleb Smirnoff 		}
4608390dc369STom Jones 		| ENDPI {
4609390dc369STom Jones 			if (pool_opts.marker & POM_ENDPI) {
4610390dc369STom Jones 				yyerror("endpoint-independent cannot be redefined");
4611390dc369STom Jones 				YYERROR;
4612390dc369STom Jones 			}
4613390dc369STom Jones 			pool_opts.marker |= POM_ENDPI;
4614390dc369STom Jones 			pool_opts.opts |= PF_POOL_ENDPI;
4615390dc369STom Jones 		}
46162aa21096SKurosawa Takahiro 		| MAPEPORTSET number '/' number '/' number {
46172aa21096SKurosawa Takahiro 			if (pool_opts.mape.offset) {
46182aa21096SKurosawa Takahiro 				yyerror("map-e-portset cannot be redefined");
46192aa21096SKurosawa Takahiro 				YYERROR;
46202aa21096SKurosawa Takahiro 			}
46212aa21096SKurosawa Takahiro 			if (pool_opts.type) {
46222aa21096SKurosawa Takahiro 				yyerror("map-e-portset cannot be used with "
46232aa21096SKurosawa Takahiro 					"address pools");
46242aa21096SKurosawa Takahiro 				YYERROR;
46252aa21096SKurosawa Takahiro 			}
46262aa21096SKurosawa Takahiro 			if ($2 <= 0 || $2 >= 16) {
46272aa21096SKurosawa Takahiro 				yyerror("MAP-E PSID offset must be 1-15");
46282aa21096SKurosawa Takahiro 				YYERROR;
46292aa21096SKurosawa Takahiro 			}
46302aa21096SKurosawa Takahiro 			if ($4 < 0 || $4 >= 16 || $2 + $4 > 16) {
46312aa21096SKurosawa Takahiro 				yyerror("Invalid MAP-E PSID length");
46322aa21096SKurosawa Takahiro 				YYERROR;
46332aa21096SKurosawa Takahiro 			} else if ($4 == 0) {
46342aa21096SKurosawa Takahiro 				yyerror("PSID Length = 0: this means"
46352aa21096SKurosawa Takahiro 				    " you do not need MAP-E");
46362aa21096SKurosawa Takahiro 				YYERROR;
46372aa21096SKurosawa Takahiro 			}
46382aa21096SKurosawa Takahiro 			if ($6 < 0 || $6 > 65535) {
46392aa21096SKurosawa Takahiro 				yyerror("Invalid MAP-E PSID");
46402aa21096SKurosawa Takahiro 				YYERROR;
46412aa21096SKurosawa Takahiro 			}
46422aa21096SKurosawa Takahiro 			pool_opts.mape.offset = $2;
46432aa21096SKurosawa Takahiro 			pool_opts.mape.psidlen = $4;
46442aa21096SKurosawa Takahiro 			pool_opts.mape.psid = $6;
46452aa21096SKurosawa Takahiro 		}
46463b3a8eb9SGleb Smirnoff 		;
46473b3a8eb9SGleb Smirnoff 
46483b3a8eb9SGleb Smirnoff redirection	: /* empty */			{ $$ = NULL; }
46493b3a8eb9SGleb Smirnoff 		| ARROW host			{
46503b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct redirection));
46513b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
46523b3a8eb9SGleb Smirnoff 				err(1, "redirection: calloc");
46533b3a8eb9SGleb Smirnoff 			$$->host = $2;
46543b3a8eb9SGleb Smirnoff 			$$->rport.a = $$->rport.b = $$->rport.t = 0;
46553b3a8eb9SGleb Smirnoff 		}
46563b3a8eb9SGleb Smirnoff 		| ARROW host PORT portstar	{
46573b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct redirection));
46583b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
46593b3a8eb9SGleb Smirnoff 				err(1, "redirection: calloc");
46603b3a8eb9SGleb Smirnoff 			$$->host = $2;
46613b3a8eb9SGleb Smirnoff 			$$->rport = $4;
46623b3a8eb9SGleb Smirnoff 		}
46633b3a8eb9SGleb Smirnoff 		;
46643b3a8eb9SGleb Smirnoff 
46653b3a8eb9SGleb Smirnoff natpasslog	: /* empty */	{ $$.b1 = $$.b2 = 0; $$.w2 = 0; }
46663b3a8eb9SGleb Smirnoff 		| PASS		{ $$.b1 = 1; $$.b2 = 0; $$.w2 = 0; }
46673b3a8eb9SGleb Smirnoff 		| PASS log	{ $$.b1 = 1; $$.b2 = $2.log; $$.w2 = $2.logif; }
46683b3a8eb9SGleb Smirnoff 		| log		{ $$.b1 = 0; $$.b2 = $1.log; $$.w2 = $1.logif; }
46693b3a8eb9SGleb Smirnoff 		;
46703b3a8eb9SGleb Smirnoff 
46713b3a8eb9SGleb Smirnoff nataction	: no NAT natpasslog {
46723b3a8eb9SGleb Smirnoff 			if ($1 && $3.b1) {
46733b3a8eb9SGleb Smirnoff 				yyerror("\"pass\" not valid with \"no\"");
46743b3a8eb9SGleb Smirnoff 				YYERROR;
46753b3a8eb9SGleb Smirnoff 			}
46763b3a8eb9SGleb Smirnoff 			if ($1)
46773b3a8eb9SGleb Smirnoff 				$$.b1 = PF_NONAT;
46783b3a8eb9SGleb Smirnoff 			else
46793b3a8eb9SGleb Smirnoff 				$$.b1 = PF_NAT;
46803b3a8eb9SGleb Smirnoff 			$$.b2 = $3.b1;
46813b3a8eb9SGleb Smirnoff 			$$.w = $3.b2;
46823b3a8eb9SGleb Smirnoff 			$$.w2 = $3.w2;
46833b3a8eb9SGleb Smirnoff 		}
46843b3a8eb9SGleb Smirnoff 		| no RDR natpasslog {
46853b3a8eb9SGleb Smirnoff 			if ($1 && $3.b1) {
46863b3a8eb9SGleb Smirnoff 				yyerror("\"pass\" not valid with \"no\"");
46873b3a8eb9SGleb Smirnoff 				YYERROR;
46883b3a8eb9SGleb Smirnoff 			}
46893b3a8eb9SGleb Smirnoff 			if ($1)
46903b3a8eb9SGleb Smirnoff 				$$.b1 = PF_NORDR;
46913b3a8eb9SGleb Smirnoff 			else
46923b3a8eb9SGleb Smirnoff 				$$.b1 = PF_RDR;
46933b3a8eb9SGleb Smirnoff 			$$.b2 = $3.b1;
46943b3a8eb9SGleb Smirnoff 			$$.w = $3.b2;
46953b3a8eb9SGleb Smirnoff 			$$.w2 = $3.w2;
46963b3a8eb9SGleb Smirnoff 		}
46973b3a8eb9SGleb Smirnoff 		;
46983b3a8eb9SGleb Smirnoff 
46993b3a8eb9SGleb Smirnoff natrule		: nataction interface af proto fromto tag tagged rtable
47003b3a8eb9SGleb Smirnoff 		    redirpool pool_opts
47013b3a8eb9SGleb Smirnoff 		{
4702e9eb0941SKristof Provost 			struct pfctl_rule	r;
4703fc6e5069SKristof Provost 			struct node_state_opt	*o;
47043b3a8eb9SGleb Smirnoff 
47053b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_NAT))
47063b3a8eb9SGleb Smirnoff 				YYERROR;
47073b3a8eb9SGleb Smirnoff 
47083b3a8eb9SGleb Smirnoff 			memset(&r, 0, sizeof(r));
47093b3a8eb9SGleb Smirnoff 
47103b3a8eb9SGleb Smirnoff 			r.action = $1.b1;
47113b3a8eb9SGleb Smirnoff 			r.natpass = $1.b2;
47123b3a8eb9SGleb Smirnoff 			r.log = $1.w;
47133b3a8eb9SGleb Smirnoff 			r.logif = $1.w2;
47143b3a8eb9SGleb Smirnoff 			r.af = $3;
47153b3a8eb9SGleb Smirnoff 
47163b3a8eb9SGleb Smirnoff 			if (!r.af) {
47173b3a8eb9SGleb Smirnoff 				if ($5.src.host && $5.src.host->af &&
47183b3a8eb9SGleb Smirnoff 				    !$5.src.host->ifindex)
47193b3a8eb9SGleb Smirnoff 					r.af = $5.src.host->af;
47203b3a8eb9SGleb Smirnoff 				else if ($5.dst.host && $5.dst.host->af &&
47213b3a8eb9SGleb Smirnoff 				    !$5.dst.host->ifindex)
47223b3a8eb9SGleb Smirnoff 					r.af = $5.dst.host->af;
47233b3a8eb9SGleb Smirnoff 			}
47243b3a8eb9SGleb Smirnoff 
47253b3a8eb9SGleb Smirnoff 			if ($6 != NULL)
47263b3a8eb9SGleb Smirnoff 				if (strlcpy(r.tagname, $6, PF_TAG_NAME_SIZE) >=
47273b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) {
47283b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
47293b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
47303b3a8eb9SGleb Smirnoff 					YYERROR;
47313b3a8eb9SGleb Smirnoff 				}
47323b3a8eb9SGleb Smirnoff 
47333b3a8eb9SGleb Smirnoff 			if ($7.name)
47343b3a8eb9SGleb Smirnoff 				if (strlcpy(r.match_tagname, $7.name,
47353b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
47363b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
47373b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
47383b3a8eb9SGleb Smirnoff 					YYERROR;
47393b3a8eb9SGleb Smirnoff 				}
47403b3a8eb9SGleb Smirnoff 			r.match_tag_not = $7.neg;
47413b3a8eb9SGleb Smirnoff 			r.rtableid = $8;
47423b3a8eb9SGleb Smirnoff 
47433b3a8eb9SGleb Smirnoff 			if (r.action == PF_NONAT || r.action == PF_NORDR) {
47443b3a8eb9SGleb Smirnoff 				if ($9 != NULL) {
47453b3a8eb9SGleb Smirnoff 					yyerror("translation rule with 'no' "
47463b3a8eb9SGleb Smirnoff 					    "does not need '->'");
47473b3a8eb9SGleb Smirnoff 					YYERROR;
47483b3a8eb9SGleb Smirnoff 				}
47493b3a8eb9SGleb Smirnoff 			} else {
47503b3a8eb9SGleb Smirnoff 				if ($9 == NULL || $9->host == NULL) {
47513b3a8eb9SGleb Smirnoff 					yyerror("translation rule requires '-> "
47523b3a8eb9SGleb Smirnoff 					    "address'");
47533b3a8eb9SGleb Smirnoff 					YYERROR;
47543b3a8eb9SGleb Smirnoff 				}
47553b3a8eb9SGleb Smirnoff 				if (!r.af && ! $9->host->ifindex)
47563b3a8eb9SGleb Smirnoff 					r.af = $9->host->af;
47573b3a8eb9SGleb Smirnoff 
47583b3a8eb9SGleb Smirnoff 				remove_invalid_hosts(&$9->host, &r.af);
47593b3a8eb9SGleb Smirnoff 				if (invalid_redirect($9->host, r.af))
47603b3a8eb9SGleb Smirnoff 					YYERROR;
47617ce98cf2SKristof Provost 				if ($9->host->addr.type == PF_ADDR_DYNIFTL) {
47627ce98cf2SKristof Provost 					if (($9->host = gen_dynnode($9->host, r.af)) == NULL)
47637ce98cf2SKristof Provost 						err(1, "calloc");
47647ce98cf2SKristof Provost 				}
47653b3a8eb9SGleb Smirnoff 				if (check_netmask($9->host, r.af))
47663b3a8eb9SGleb Smirnoff 					YYERROR;
47673b3a8eb9SGleb Smirnoff 
47683b3a8eb9SGleb Smirnoff 				r.rpool.proxy_port[0] = ntohs($9->rport.a);
47693b3a8eb9SGleb Smirnoff 
47703b3a8eb9SGleb Smirnoff 				switch (r.action) {
47713b3a8eb9SGleb Smirnoff 				case PF_RDR:
47723b3a8eb9SGleb Smirnoff 					if (!$9->rport.b && $9->rport.t &&
47733b3a8eb9SGleb Smirnoff 					    $5.dst.port != NULL) {
47743b3a8eb9SGleb Smirnoff 						r.rpool.proxy_port[1] =
47753b3a8eb9SGleb Smirnoff 						    ntohs($9->rport.a) +
47763b3a8eb9SGleb Smirnoff 						    (ntohs(
47773b3a8eb9SGleb Smirnoff 						    $5.dst.port->port[1]) -
47783b3a8eb9SGleb Smirnoff 						    ntohs(
47793b3a8eb9SGleb Smirnoff 						    $5.dst.port->port[0]));
47803b3a8eb9SGleb Smirnoff 					} else
47813b3a8eb9SGleb Smirnoff 						r.rpool.proxy_port[1] =
47823b3a8eb9SGleb Smirnoff 						    ntohs($9->rport.b);
47833b3a8eb9SGleb Smirnoff 					break;
47843b3a8eb9SGleb Smirnoff 				case PF_NAT:
47853b3a8eb9SGleb Smirnoff 					r.rpool.proxy_port[1] =
47863b3a8eb9SGleb Smirnoff 					    ntohs($9->rport.b);
47873b3a8eb9SGleb Smirnoff 					if (!r.rpool.proxy_port[0] &&
47883b3a8eb9SGleb Smirnoff 					    !r.rpool.proxy_port[1]) {
47893b3a8eb9SGleb Smirnoff 						r.rpool.proxy_port[0] =
47903b3a8eb9SGleb Smirnoff 						    PF_NAT_PROXY_PORT_LOW;
47913b3a8eb9SGleb Smirnoff 						r.rpool.proxy_port[1] =
47923b3a8eb9SGleb Smirnoff 						    PF_NAT_PROXY_PORT_HIGH;
47933b3a8eb9SGleb Smirnoff 					} else if (!r.rpool.proxy_port[1])
47943b3a8eb9SGleb Smirnoff 						r.rpool.proxy_port[1] =
47953b3a8eb9SGleb Smirnoff 						    r.rpool.proxy_port[0];
47963b3a8eb9SGleb Smirnoff 					break;
47973b3a8eb9SGleb Smirnoff 				default:
47983b3a8eb9SGleb Smirnoff 					break;
47993b3a8eb9SGleb Smirnoff 				}
48003b3a8eb9SGleb Smirnoff 
48013b3a8eb9SGleb Smirnoff 				r.rpool.opts = $10.type;
48023b3a8eb9SGleb Smirnoff 				if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
48033b3a8eb9SGleb Smirnoff 				    PF_POOL_NONE && ($9->host->next != NULL ||
48043b3a8eb9SGleb Smirnoff 				    $9->host->addr.type == PF_ADDR_TABLE ||
48053b3a8eb9SGleb Smirnoff 				    DYNIF_MULTIADDR($9->host->addr)))
48063b3a8eb9SGleb Smirnoff 					r.rpool.opts = PF_POOL_ROUNDROBIN;
48073b3a8eb9SGleb Smirnoff 				if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
48083b3a8eb9SGleb Smirnoff 				    PF_POOL_ROUNDROBIN &&
48093b3a8eb9SGleb Smirnoff 				    disallow_table($9->host, "tables are only "
48103b3a8eb9SGleb Smirnoff 				    "supported in round-robin redirection "
48113b3a8eb9SGleb Smirnoff 				    "pools"))
48123b3a8eb9SGleb Smirnoff 					YYERROR;
48133b3a8eb9SGleb Smirnoff 				if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
48143b3a8eb9SGleb Smirnoff 				    PF_POOL_ROUNDROBIN &&
48153b3a8eb9SGleb Smirnoff 				    disallow_alias($9->host, "interface (%s) "
48163b3a8eb9SGleb Smirnoff 				    "is only supported in round-robin "
48173b3a8eb9SGleb Smirnoff 				    "redirection pools"))
48183b3a8eb9SGleb Smirnoff 					YYERROR;
48193b3a8eb9SGleb Smirnoff 				if ($9->host->next != NULL) {
48203b3a8eb9SGleb Smirnoff 					if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
48213b3a8eb9SGleb Smirnoff 					    PF_POOL_ROUNDROBIN) {
48223b3a8eb9SGleb Smirnoff 						yyerror("only round-robin "
48233b3a8eb9SGleb Smirnoff 						    "valid for multiple "
48243b3a8eb9SGleb Smirnoff 						    "redirection addresses");
48253b3a8eb9SGleb Smirnoff 						YYERROR;
48263b3a8eb9SGleb Smirnoff 					}
48273b3a8eb9SGleb Smirnoff 				}
48283b3a8eb9SGleb Smirnoff 			}
48293b3a8eb9SGleb Smirnoff 
48303b3a8eb9SGleb Smirnoff 			if ($10.key != NULL)
48313b3a8eb9SGleb Smirnoff 				memcpy(&r.rpool.key, $10.key,
48323b3a8eb9SGleb Smirnoff 				    sizeof(struct pf_poolhashkey));
48333b3a8eb9SGleb Smirnoff 
48343b3a8eb9SGleb Smirnoff 			 if ($10.opts)
48353b3a8eb9SGleb Smirnoff 				r.rpool.opts |= $10.opts;
48363b3a8eb9SGleb Smirnoff 
48373b3a8eb9SGleb Smirnoff 			if ($10.staticport) {
48383b3a8eb9SGleb Smirnoff 				if (r.action != PF_NAT) {
48393b3a8eb9SGleb Smirnoff 					yyerror("the 'static-port' option is "
48403b3a8eb9SGleb Smirnoff 					    "only valid with nat rules");
48413b3a8eb9SGleb Smirnoff 					YYERROR;
48423b3a8eb9SGleb Smirnoff 				}
48433b3a8eb9SGleb Smirnoff 				if (r.rpool.proxy_port[0] !=
48443b3a8eb9SGleb Smirnoff 				    PF_NAT_PROXY_PORT_LOW &&
48453b3a8eb9SGleb Smirnoff 				    r.rpool.proxy_port[1] !=
48463b3a8eb9SGleb Smirnoff 				    PF_NAT_PROXY_PORT_HIGH) {
48473b3a8eb9SGleb Smirnoff 					yyerror("the 'static-port' option can't"
48483b3a8eb9SGleb Smirnoff 					    " be used when specifying a port"
48493b3a8eb9SGleb Smirnoff 					    " range");
48503b3a8eb9SGleb Smirnoff 					YYERROR;
48513b3a8eb9SGleb Smirnoff 				}
48523b3a8eb9SGleb Smirnoff 				r.rpool.proxy_port[0] = 0;
48533b3a8eb9SGleb Smirnoff 				r.rpool.proxy_port[1] = 0;
48543b3a8eb9SGleb Smirnoff 			}
48553b3a8eb9SGleb Smirnoff 
48562aa21096SKurosawa Takahiro 			if ($10.mape.offset) {
48572aa21096SKurosawa Takahiro 				if (r.action != PF_NAT) {
48582aa21096SKurosawa Takahiro 					yyerror("the 'map-e-portset' option is"
48592aa21096SKurosawa Takahiro 					    " only valid with nat rules");
48602aa21096SKurosawa Takahiro 					YYERROR;
48612aa21096SKurosawa Takahiro 				}
48622aa21096SKurosawa Takahiro 				if ($10.staticport) {
48632aa21096SKurosawa Takahiro 					yyerror("the 'map-e-portset' option"
48642aa21096SKurosawa Takahiro 					    " can't be used 'static-port'");
48652aa21096SKurosawa Takahiro 					YYERROR;
48662aa21096SKurosawa Takahiro 				}
48672aa21096SKurosawa Takahiro 				if (r.rpool.proxy_port[0] !=
48682aa21096SKurosawa Takahiro 				    PF_NAT_PROXY_PORT_LOW &&
48692aa21096SKurosawa Takahiro 				    r.rpool.proxy_port[1] !=
48702aa21096SKurosawa Takahiro 				    PF_NAT_PROXY_PORT_HIGH) {
48712aa21096SKurosawa Takahiro 					yyerror("the 'map-e-portset' option"
48722aa21096SKurosawa Takahiro 					    " can't be used when specifying"
48732aa21096SKurosawa Takahiro 					    " a port range");
48742aa21096SKurosawa Takahiro 					YYERROR;
48752aa21096SKurosawa Takahiro 				}
48762aa21096SKurosawa Takahiro 				r.rpool.mape = $10.mape;
48772aa21096SKurosawa Takahiro 			}
48782aa21096SKurosawa Takahiro 
4879fc6e5069SKristof Provost 			o = keep_state_defaults;
4880fc6e5069SKristof Provost 			while (o) {
4881fc6e5069SKristof Provost 				switch (o->type) {
4882fc6e5069SKristof Provost 				case PF_STATE_OPT_PFLOW:
4883fc6e5069SKristof Provost 					if (r.rule_flag & PFRULE_PFLOW) {
4884fc6e5069SKristof Provost 						yyerror("state pflow option: "
4885fc6e5069SKristof Provost 						    "multiple definitions");
4886fc6e5069SKristof Provost 						YYERROR;
4887fc6e5069SKristof Provost 					}
4888fc6e5069SKristof Provost 					r.rule_flag |= PFRULE_PFLOW;
4889fc6e5069SKristof Provost 					break;
4890fc6e5069SKristof Provost 				}
4891fc6e5069SKristof Provost 				o = o->next;
4892fc6e5069SKristof Provost 			}
4893fc6e5069SKristof Provost 
48943b3a8eb9SGleb Smirnoff 			expand_rule(&r, $2, $9 == NULL ? NULL : $9->host, $4,
48953b3a8eb9SGleb Smirnoff 			    $5.src_os, $5.src.host, $5.src.port, $5.dst.host,
48962339ead6SKristof Provost 			    $5.dst.port, 0, 0, 0, 0, "");
48973b3a8eb9SGleb Smirnoff 			free($9);
48983b3a8eb9SGleb Smirnoff 		}
48993b3a8eb9SGleb Smirnoff 		;
49003b3a8eb9SGleb Smirnoff 
49011e93588bSLuiz Otavio O Souza binatrule	: no BINAT natpasslog interface af proto FROM ipspec toipspec tag
49023b3a8eb9SGleb Smirnoff 		    tagged rtable redirection
49033b3a8eb9SGleb Smirnoff 		{
4904e9eb0941SKristof Provost 			struct pfctl_rule	binat;
49053b3a8eb9SGleb Smirnoff 			struct pf_pooladdr	*pa;
49063b3a8eb9SGleb Smirnoff 
49073b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_NAT))
49083b3a8eb9SGleb Smirnoff 				YYERROR;
49093b3a8eb9SGleb Smirnoff 			if (disallow_urpf_failed($9, "\"urpf-failed\" is not "
49103b3a8eb9SGleb Smirnoff 			    "permitted as a binat destination"))
49113b3a8eb9SGleb Smirnoff 				YYERROR;
49123b3a8eb9SGleb Smirnoff 
49133b3a8eb9SGleb Smirnoff 			memset(&binat, 0, sizeof(binat));
49143b3a8eb9SGleb Smirnoff 
49153b3a8eb9SGleb Smirnoff 			if ($1 && $3.b1) {
49163b3a8eb9SGleb Smirnoff 				yyerror("\"pass\" not valid with \"no\"");
49173b3a8eb9SGleb Smirnoff 				YYERROR;
49183b3a8eb9SGleb Smirnoff 			}
49193b3a8eb9SGleb Smirnoff 			if ($1)
49203b3a8eb9SGleb Smirnoff 				binat.action = PF_NOBINAT;
49213b3a8eb9SGleb Smirnoff 			else
49223b3a8eb9SGleb Smirnoff 				binat.action = PF_BINAT;
49233b3a8eb9SGleb Smirnoff 			binat.natpass = $3.b1;
49243b3a8eb9SGleb Smirnoff 			binat.log = $3.b2;
49253b3a8eb9SGleb Smirnoff 			binat.logif = $3.w2;
49263b3a8eb9SGleb Smirnoff 			binat.af = $5;
49273b3a8eb9SGleb Smirnoff 			if (!binat.af && $8 != NULL && $8->af)
49283b3a8eb9SGleb Smirnoff 				binat.af = $8->af;
49293b3a8eb9SGleb Smirnoff 			if (!binat.af && $9 != NULL && $9->af)
49303b3a8eb9SGleb Smirnoff 				binat.af = $9->af;
49313b3a8eb9SGleb Smirnoff 
49323b3a8eb9SGleb Smirnoff 			if (!binat.af && $13 != NULL && $13->host)
49333b3a8eb9SGleb Smirnoff 				binat.af = $13->host->af;
49343b3a8eb9SGleb Smirnoff 			if (!binat.af) {
49353b3a8eb9SGleb Smirnoff 				yyerror("address family (inet/inet6) "
49363b3a8eb9SGleb Smirnoff 				    "undefined");
49373b3a8eb9SGleb Smirnoff 				YYERROR;
49383b3a8eb9SGleb Smirnoff 			}
49393b3a8eb9SGleb Smirnoff 
49403b3a8eb9SGleb Smirnoff 			if ($4 != NULL) {
49413b3a8eb9SGleb Smirnoff 				memcpy(binat.ifname, $4->ifname,
49423b3a8eb9SGleb Smirnoff 				    sizeof(binat.ifname));
49433b3a8eb9SGleb Smirnoff 				binat.ifnot = $4->not;
49443b3a8eb9SGleb Smirnoff 				free($4);
49453b3a8eb9SGleb Smirnoff 			}
49463b3a8eb9SGleb Smirnoff 
49473b3a8eb9SGleb Smirnoff 			if ($10 != NULL)
49483b3a8eb9SGleb Smirnoff 				if (strlcpy(binat.tagname, $10,
49493b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
49503b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
49513b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
49523b3a8eb9SGleb Smirnoff 					YYERROR;
49533b3a8eb9SGleb Smirnoff 				}
49543b3a8eb9SGleb Smirnoff 			if ($11.name)
49553b3a8eb9SGleb Smirnoff 				if (strlcpy(binat.match_tagname, $11.name,
49563b3a8eb9SGleb Smirnoff 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
49573b3a8eb9SGleb Smirnoff 					yyerror("tag too long, max %u chars",
49583b3a8eb9SGleb Smirnoff 					    PF_TAG_NAME_SIZE - 1);
49593b3a8eb9SGleb Smirnoff 					YYERROR;
49603b3a8eb9SGleb Smirnoff 				}
49613b3a8eb9SGleb Smirnoff 			binat.match_tag_not = $11.neg;
49623b3a8eb9SGleb Smirnoff 			binat.rtableid = $12;
49633b3a8eb9SGleb Smirnoff 
49643b3a8eb9SGleb Smirnoff 			if ($6 != NULL) {
49653b3a8eb9SGleb Smirnoff 				binat.proto = $6->proto;
49663b3a8eb9SGleb Smirnoff 				free($6);
49673b3a8eb9SGleb Smirnoff 			}
49683b3a8eb9SGleb Smirnoff 
49693b3a8eb9SGleb Smirnoff 			if ($8 != NULL && disallow_table($8, "invalid use of "
49703b3a8eb9SGleb Smirnoff 			    "table <%s> as the source address of a binat rule"))
49713b3a8eb9SGleb Smirnoff 				YYERROR;
49723b3a8eb9SGleb Smirnoff 			if ($8 != NULL && disallow_alias($8, "invalid use of "
49733b3a8eb9SGleb Smirnoff 			    "interface (%s) as the source address of a binat "
49743b3a8eb9SGleb Smirnoff 			    "rule"))
49753b3a8eb9SGleb Smirnoff 				YYERROR;
49763b3a8eb9SGleb Smirnoff 			if ($13 != NULL && $13->host != NULL && disallow_table(
49773b3a8eb9SGleb Smirnoff 			    $13->host, "invalid use of table <%s> as the "
49783b3a8eb9SGleb Smirnoff 			    "redirect address of a binat rule"))
49793b3a8eb9SGleb Smirnoff 				YYERROR;
49803b3a8eb9SGleb Smirnoff 			if ($13 != NULL && $13->host != NULL && disallow_alias(
49813b3a8eb9SGleb Smirnoff 			    $13->host, "invalid use of interface (%s) as the "
49823b3a8eb9SGleb Smirnoff 			    "redirect address of a binat rule"))
49833b3a8eb9SGleb Smirnoff 				YYERROR;
49843b3a8eb9SGleb Smirnoff 
49853b3a8eb9SGleb Smirnoff 			if ($8 != NULL) {
49863b3a8eb9SGleb Smirnoff 				if ($8->next) {
49873b3a8eb9SGleb Smirnoff 					yyerror("multiple binat ip addresses");
49883b3a8eb9SGleb Smirnoff 					YYERROR;
49893b3a8eb9SGleb Smirnoff 				}
49903b3a8eb9SGleb Smirnoff 				if ($8->addr.type == PF_ADDR_DYNIFTL)
49913b3a8eb9SGleb Smirnoff 					$8->af = binat.af;
49923b3a8eb9SGleb Smirnoff 				if ($8->af != binat.af) {
49933b3a8eb9SGleb Smirnoff 					yyerror("binat ip versions must match");
49943b3a8eb9SGleb Smirnoff 					YYERROR;
49953b3a8eb9SGleb Smirnoff 				}
49967ce98cf2SKristof Provost 				if ($8->addr.type == PF_ADDR_DYNIFTL) {
49977ce98cf2SKristof Provost 					if (($8 = gen_dynnode($8, binat.af)) == NULL)
49987ce98cf2SKristof Provost 						err(1, "calloc");
49997ce98cf2SKristof Provost 				}
50003b3a8eb9SGleb Smirnoff 				if (check_netmask($8, binat.af))
50013b3a8eb9SGleb Smirnoff 					YYERROR;
50023b3a8eb9SGleb Smirnoff 				memcpy(&binat.src.addr, &$8->addr,
50033b3a8eb9SGleb Smirnoff 				    sizeof(binat.src.addr));
50043b3a8eb9SGleb Smirnoff 				free($8);
50053b3a8eb9SGleb Smirnoff 			}
50063b3a8eb9SGleb Smirnoff 			if ($9 != NULL) {
50073b3a8eb9SGleb Smirnoff 				if ($9->next) {
50083b3a8eb9SGleb Smirnoff 					yyerror("multiple binat ip addresses");
50093b3a8eb9SGleb Smirnoff 					YYERROR;
50103b3a8eb9SGleb Smirnoff 				}
50113b3a8eb9SGleb Smirnoff 				if ($9->af != binat.af && $9->af) {
50123b3a8eb9SGleb Smirnoff 					yyerror("binat ip versions must match");
50133b3a8eb9SGleb Smirnoff 					YYERROR;
50143b3a8eb9SGleb Smirnoff 				}
50157ce98cf2SKristof Provost 				if ($9->addr.type == PF_ADDR_DYNIFTL) {
50167ce98cf2SKristof Provost 					if (($9 = gen_dynnode($9, binat.af)) == NULL)
50177ce98cf2SKristof Provost 						err(1, "calloc");
50187ce98cf2SKristof Provost 				}
50193b3a8eb9SGleb Smirnoff 				if (check_netmask($9, binat.af))
50203b3a8eb9SGleb Smirnoff 					YYERROR;
50213b3a8eb9SGleb Smirnoff 				memcpy(&binat.dst.addr, &$9->addr,
50223b3a8eb9SGleb Smirnoff 				    sizeof(binat.dst.addr));
50233b3a8eb9SGleb Smirnoff 				binat.dst.neg = $9->not;
50243b3a8eb9SGleb Smirnoff 				free($9);
50253b3a8eb9SGleb Smirnoff 			}
50263b3a8eb9SGleb Smirnoff 
50273b3a8eb9SGleb Smirnoff 			if (binat.action == PF_NOBINAT) {
50283b3a8eb9SGleb Smirnoff 				if ($13 != NULL) {
50293b3a8eb9SGleb Smirnoff 					yyerror("'no binat' rule does not need"
50303b3a8eb9SGleb Smirnoff 					    " '->'");
50313b3a8eb9SGleb Smirnoff 					YYERROR;
50323b3a8eb9SGleb Smirnoff 				}
50333b3a8eb9SGleb Smirnoff 			} else {
50343b3a8eb9SGleb Smirnoff 				if ($13 == NULL || $13->host == NULL) {
50353b3a8eb9SGleb Smirnoff 					yyerror("'binat' rule requires"
50363b3a8eb9SGleb Smirnoff 					    " '-> address'");
50373b3a8eb9SGleb Smirnoff 					YYERROR;
50383b3a8eb9SGleb Smirnoff 				}
50393b3a8eb9SGleb Smirnoff 
50403b3a8eb9SGleb Smirnoff 				remove_invalid_hosts(&$13->host, &binat.af);
50413b3a8eb9SGleb Smirnoff 				if (invalid_redirect($13->host, binat.af))
50423b3a8eb9SGleb Smirnoff 					YYERROR;
50433b3a8eb9SGleb Smirnoff 				if ($13->host->next != NULL) {
50443b3a8eb9SGleb Smirnoff 					yyerror("binat rule must redirect to "
50453b3a8eb9SGleb Smirnoff 					    "a single address");
50463b3a8eb9SGleb Smirnoff 					YYERROR;
50473b3a8eb9SGleb Smirnoff 				}
50487ce98cf2SKristof Provost 				if ($13->host->addr.type == PF_ADDR_DYNIFTL) {
50497ce98cf2SKristof Provost 					if (($13->host = gen_dynnode($13->host, binat.af)) == NULL)
50507ce98cf2SKristof Provost 						err(1, "calloc");
50517ce98cf2SKristof Provost 				}
50523b3a8eb9SGleb Smirnoff 				if (check_netmask($13->host, binat.af))
50533b3a8eb9SGleb Smirnoff 					YYERROR;
50543b3a8eb9SGleb Smirnoff 
50553b3a8eb9SGleb Smirnoff 				if (!PF_AZERO(&binat.src.addr.v.a.mask,
50563b3a8eb9SGleb Smirnoff 				    binat.af) &&
50573b3a8eb9SGleb Smirnoff 				    !PF_AEQ(&binat.src.addr.v.a.mask,
50583b3a8eb9SGleb Smirnoff 				    &$13->host->addr.v.a.mask, binat.af)) {
50593b3a8eb9SGleb Smirnoff 					yyerror("'binat' source mask and "
50603b3a8eb9SGleb Smirnoff 					    "redirect mask must be the same");
50613b3a8eb9SGleb Smirnoff 					YYERROR;
50623b3a8eb9SGleb Smirnoff 				}
50633b3a8eb9SGleb Smirnoff 
50643b3a8eb9SGleb Smirnoff 				TAILQ_INIT(&binat.rpool.list);
50653b3a8eb9SGleb Smirnoff 				pa = calloc(1, sizeof(struct pf_pooladdr));
50663b3a8eb9SGleb Smirnoff 				if (pa == NULL)
50673b3a8eb9SGleb Smirnoff 					err(1, "binat: calloc");
50683b3a8eb9SGleb Smirnoff 				pa->addr = $13->host->addr;
50693b3a8eb9SGleb Smirnoff 				pa->ifname[0] = 0;
50703b3a8eb9SGleb Smirnoff 				TAILQ_INSERT_TAIL(&binat.rpool.list,
50713b3a8eb9SGleb Smirnoff 				    pa, entries);
50723b3a8eb9SGleb Smirnoff 
50733b3a8eb9SGleb Smirnoff 				free($13);
50743b3a8eb9SGleb Smirnoff 			}
50753b3a8eb9SGleb Smirnoff 
50760d71f9f3SKristof Provost 			pfctl_append_rule(pf, &binat, "");
50773b3a8eb9SGleb Smirnoff 		}
50783b3a8eb9SGleb Smirnoff 		;
50793b3a8eb9SGleb Smirnoff 
50803b3a8eb9SGleb Smirnoff tag		: /* empty */		{ $$ = NULL; }
50813b3a8eb9SGleb Smirnoff 		| TAG STRING		{ $$ = $2; }
50823b3a8eb9SGleb Smirnoff 		;
50833b3a8eb9SGleb Smirnoff 
50843b3a8eb9SGleb Smirnoff tagged		: /* empty */		{ $$.neg = 0; $$.name = NULL; }
50853b3a8eb9SGleb Smirnoff 		| not TAGGED string	{ $$.neg = $1; $$.name = $3; }
50863b3a8eb9SGleb Smirnoff 		;
50873b3a8eb9SGleb Smirnoff 
50883b3a8eb9SGleb Smirnoff rtable		: /* empty */		{ $$ = -1; }
50893b3a8eb9SGleb Smirnoff 		| RTABLE NUMBER		{
50903b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > rt_tableid_max()) {
50913b3a8eb9SGleb Smirnoff 				yyerror("invalid rtable id");
50923b3a8eb9SGleb Smirnoff 				YYERROR;
50933b3a8eb9SGleb Smirnoff 			}
50943b3a8eb9SGleb Smirnoff 			$$ = $2;
50953b3a8eb9SGleb Smirnoff 		}
50963b3a8eb9SGleb Smirnoff 		;
50973b3a8eb9SGleb Smirnoff 
50983b3a8eb9SGleb Smirnoff route_host	: STRING			{
50993b3a8eb9SGleb Smirnoff 			$$ = calloc(1, sizeof(struct node_host));
51003b3a8eb9SGleb Smirnoff 			if ($$ == NULL)
51013b3a8eb9SGleb Smirnoff 				err(1, "route_host: calloc");
5102e68de669SKristof Provost 			if (strlen($1) >= IFNAMSIZ) {
5103e68de669SKristof Provost 				yyerror("interface name too long");
5104e68de669SKristof Provost 				YYERROR;
5105e68de669SKristof Provost 			}
5106a2a90d6eSKristof Provost 			$$->ifname = strdup($1);
51073b3a8eb9SGleb Smirnoff 			set_ipmask($$, 128);
51083b3a8eb9SGleb Smirnoff 			$$->next = NULL;
51093b3a8eb9SGleb Smirnoff 			$$->tail = $$;
51103b3a8eb9SGleb Smirnoff 		}
51113b3a8eb9SGleb Smirnoff 		| '(' STRING host ')'		{
511258c8430aSKristof Provost 			struct node_host *n;
511358c8430aSKristof Provost 
51143b3a8eb9SGleb Smirnoff 			$$ = $3;
5115e68de669SKristof Provost 			for (n = $3; n != NULL; n = n->next) {
5116e68de669SKristof Provost 				if (strlen($2) >= IFNAMSIZ) {
5117e68de669SKristof Provost 					yyerror("interface name too long");
5118e68de669SKristof Provost 					YYERROR;
5119e68de669SKristof Provost 				}
5120a2a90d6eSKristof Provost 				n->ifname = strdup($2);
51213b3a8eb9SGleb Smirnoff 			}
5122e68de669SKristof Provost 		}
51233b3a8eb9SGleb Smirnoff 		;
51243b3a8eb9SGleb Smirnoff 
51253b3a8eb9SGleb Smirnoff route_host_list	: route_host optnl			{ $$ = $1; }
51263b3a8eb9SGleb Smirnoff 		| route_host_list comma route_host optnl {
51273b3a8eb9SGleb Smirnoff 			if ($1->af == 0)
51283b3a8eb9SGleb Smirnoff 				$1->af = $3->af;
51293b3a8eb9SGleb Smirnoff 			if ($1->af != $3->af) {
51303b3a8eb9SGleb Smirnoff 				yyerror("all pool addresses must be in the "
51313b3a8eb9SGleb Smirnoff 				    "same address family");
51323b3a8eb9SGleb Smirnoff 				YYERROR;
51333b3a8eb9SGleb Smirnoff 			}
51343b3a8eb9SGleb Smirnoff 			$1->tail->next = $3;
51353b3a8eb9SGleb Smirnoff 			$1->tail = $3->tail;
51363b3a8eb9SGleb Smirnoff 			$$ = $1;
51373b3a8eb9SGleb Smirnoff 		}
51383b3a8eb9SGleb Smirnoff 		;
51393b3a8eb9SGleb Smirnoff 
51403b3a8eb9SGleb Smirnoff routespec	: route_host			{ $$ = $1; }
51413b3a8eb9SGleb Smirnoff 		| '{' optnl route_host_list '}'	{ $$ = $3; }
51423b3a8eb9SGleb Smirnoff 		;
51433b3a8eb9SGleb Smirnoff 
51443b3a8eb9SGleb Smirnoff route		: /* empty */			{
51453b3a8eb9SGleb Smirnoff 			$$.host = NULL;
51463b3a8eb9SGleb Smirnoff 			$$.rt = 0;
51473b3a8eb9SGleb Smirnoff 			$$.pool_opts = 0;
51483b3a8eb9SGleb Smirnoff 		}
51493b3a8eb9SGleb Smirnoff 		| FASTROUTE {
5150813196a1SKristof Provost 			/* backwards-compat */
51513b3a8eb9SGleb Smirnoff 			$$.host = NULL;
5152813196a1SKristof Provost 			$$.rt = 0;
51533b3a8eb9SGleb Smirnoff 			$$.pool_opts = 0;
51543b3a8eb9SGleb Smirnoff 		}
51553b3a8eb9SGleb Smirnoff 		| ROUTETO routespec pool_opts {
51563b3a8eb9SGleb Smirnoff 			$$.host = $2;
51573b3a8eb9SGleb Smirnoff 			$$.rt = PF_ROUTETO;
51583b3a8eb9SGleb Smirnoff 			$$.pool_opts = $3.type | $3.opts;
51593b3a8eb9SGleb Smirnoff 			if ($3.key != NULL)
51603b3a8eb9SGleb Smirnoff 				$$.key = $3.key;
51613b3a8eb9SGleb Smirnoff 		}
51623b3a8eb9SGleb Smirnoff 		| REPLYTO routespec pool_opts {
51633b3a8eb9SGleb Smirnoff 			$$.host = $2;
51643b3a8eb9SGleb Smirnoff 			$$.rt = PF_REPLYTO;
51653b3a8eb9SGleb Smirnoff 			$$.pool_opts = $3.type | $3.opts;
51663b3a8eb9SGleb Smirnoff 			if ($3.key != NULL)
51673b3a8eb9SGleb Smirnoff 				$$.key = $3.key;
51683b3a8eb9SGleb Smirnoff 		}
51693b3a8eb9SGleb Smirnoff 		| DUPTO routespec pool_opts {
51703b3a8eb9SGleb Smirnoff 			$$.host = $2;
51713b3a8eb9SGleb Smirnoff 			$$.rt = PF_DUPTO;
51723b3a8eb9SGleb Smirnoff 			$$.pool_opts = $3.type | $3.opts;
51733b3a8eb9SGleb Smirnoff 			if ($3.key != NULL)
51743b3a8eb9SGleb Smirnoff 				$$.key = $3.key;
51753b3a8eb9SGleb Smirnoff 		}
51763b3a8eb9SGleb Smirnoff 		;
51773b3a8eb9SGleb Smirnoff 
51783b3a8eb9SGleb Smirnoff timeout_spec	: STRING NUMBER
51793b3a8eb9SGleb Smirnoff 		{
51803b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_OPTION)) {
51813b3a8eb9SGleb Smirnoff 				free($1);
51823b3a8eb9SGleb Smirnoff 				YYERROR;
51833b3a8eb9SGleb Smirnoff 			}
51843b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > UINT_MAX) {
51853b3a8eb9SGleb Smirnoff 				yyerror("only positive values permitted");
51863b3a8eb9SGleb Smirnoff 				YYERROR;
51873b3a8eb9SGleb Smirnoff 			}
518830bad751SKristof Provost 			if (pfctl_apply_timeout(pf, $1, $2, 0) != 0) {
51893b3a8eb9SGleb Smirnoff 				yyerror("unknown timeout %s", $1);
51903b3a8eb9SGleb Smirnoff 				free($1);
51913b3a8eb9SGleb Smirnoff 				YYERROR;
51923b3a8eb9SGleb Smirnoff 			}
51933b3a8eb9SGleb Smirnoff 			free($1);
51943b3a8eb9SGleb Smirnoff 		}
51957f8af000SLuiz Otavio O Souza 		| INTERVAL NUMBER		{
51967f8af000SLuiz Otavio O Souza 			if (check_rulestate(PFCTL_STATE_OPTION))
51977f8af000SLuiz Otavio O Souza 				YYERROR;
51987f8af000SLuiz Otavio O Souza 			if ($2 < 0 || $2 > UINT_MAX) {
51997f8af000SLuiz Otavio O Souza 				yyerror("only positive values permitted");
52007f8af000SLuiz Otavio O Souza 				YYERROR;
52017f8af000SLuiz Otavio O Souza 			}
520230bad751SKristof Provost 			if (pfctl_apply_timeout(pf, "interval", $2, 0) != 0)
52037f8af000SLuiz Otavio O Souza 				YYERROR;
52047f8af000SLuiz Otavio O Souza 		}
52053b3a8eb9SGleb Smirnoff 		;
52063b3a8eb9SGleb Smirnoff 
52073b3a8eb9SGleb Smirnoff timeout_list	: timeout_list comma timeout_spec optnl
52083b3a8eb9SGleb Smirnoff 		| timeout_spec optnl
52093b3a8eb9SGleb Smirnoff 		;
52103b3a8eb9SGleb Smirnoff 
52113b3a8eb9SGleb Smirnoff limit_spec	: STRING NUMBER
52123b3a8eb9SGleb Smirnoff 		{
52133b3a8eb9SGleb Smirnoff 			if (check_rulestate(PFCTL_STATE_OPTION)) {
52143b3a8eb9SGleb Smirnoff 				free($1);
52153b3a8eb9SGleb Smirnoff 				YYERROR;
52163b3a8eb9SGleb Smirnoff 			}
52173b3a8eb9SGleb Smirnoff 			if ($2 < 0 || $2 > UINT_MAX) {
52183b3a8eb9SGleb Smirnoff 				yyerror("only positive values permitted");
52193b3a8eb9SGleb Smirnoff 				YYERROR;
52203b3a8eb9SGleb Smirnoff 			}
5221d9ab8999SKristof Provost 			if (pfctl_apply_limit(pf, $1, $2) != 0) {
52223b3a8eb9SGleb Smirnoff 				yyerror("unable to set limit %s %u", $1, $2);
52233b3a8eb9SGleb Smirnoff 				free($1);
52243b3a8eb9SGleb Smirnoff 				YYERROR;
52253b3a8eb9SGleb Smirnoff 			}
52263b3a8eb9SGleb Smirnoff 			free($1);
52273b3a8eb9SGleb Smirnoff 		}
52283b3a8eb9SGleb Smirnoff 		;
52293b3a8eb9SGleb Smirnoff 
52303b3a8eb9SGleb Smirnoff limit_list	: limit_list comma limit_spec optnl
52313b3a8eb9SGleb Smirnoff 		| limit_spec optnl
52323b3a8eb9SGleb Smirnoff 		;
52333b3a8eb9SGleb Smirnoff 
52343b3a8eb9SGleb Smirnoff comma		: ','
52353b3a8eb9SGleb Smirnoff 		| /* empty */
52363b3a8eb9SGleb Smirnoff 		;
52373b3a8eb9SGleb Smirnoff 
52383b3a8eb9SGleb Smirnoff yesno		: NO			{ $$ = 0; }
52393b3a8eb9SGleb Smirnoff 		| STRING		{
52403b3a8eb9SGleb Smirnoff 			if (!strcmp($1, "yes"))
52413b3a8eb9SGleb Smirnoff 				$$ = 1;
52423b3a8eb9SGleb Smirnoff 			else {
52433b3a8eb9SGleb Smirnoff 				yyerror("invalid value '%s', expected 'yes' "
52443b3a8eb9SGleb Smirnoff 				    "or 'no'", $1);
52453b3a8eb9SGleb Smirnoff 				free($1);
52463b3a8eb9SGleb Smirnoff 				YYERROR;
52473b3a8eb9SGleb Smirnoff 			}
52483b3a8eb9SGleb Smirnoff 			free($1);
52493b3a8eb9SGleb Smirnoff 		}
52503b3a8eb9SGleb Smirnoff 		;
52513b3a8eb9SGleb Smirnoff 
52523b3a8eb9SGleb Smirnoff unaryop		: '='		{ $$ = PF_OP_EQ; }
525380eb861dSKristof Provost 		| NE			{ $$ = PF_OP_NE; }
525480eb861dSKristof Provost 		| LE			{ $$ = PF_OP_LE; }
52553b3a8eb9SGleb Smirnoff 		| '<'		{ $$ = PF_OP_LT; }
525680eb861dSKristof Provost 		| GE		{ $$ = PF_OP_GE; }
52573b3a8eb9SGleb Smirnoff 		| '>'		{ $$ = PF_OP_GT; }
52583b3a8eb9SGleb Smirnoff 		;
52593b3a8eb9SGleb Smirnoff 
52603b3a8eb9SGleb Smirnoff %%
52613b3a8eb9SGleb Smirnoff 
52623b3a8eb9SGleb Smirnoff int
52633b3a8eb9SGleb Smirnoff yyerror(const char *fmt, ...)
52643b3a8eb9SGleb Smirnoff {
52653b3a8eb9SGleb Smirnoff 	va_list		 ap;
52663b3a8eb9SGleb Smirnoff 
52673b3a8eb9SGleb Smirnoff 	file->errors++;
52683b3a8eb9SGleb Smirnoff 	va_start(ap, fmt);
52693b3a8eb9SGleb Smirnoff 	fprintf(stderr, "%s:%d: ", file->name, yylval.lineno);
52703b3a8eb9SGleb Smirnoff 	vfprintf(stderr, fmt, ap);
52713b3a8eb9SGleb Smirnoff 	fprintf(stderr, "\n");
52723b3a8eb9SGleb Smirnoff 	va_end(ap);
52733b3a8eb9SGleb Smirnoff 	return (0);
52743b3a8eb9SGleb Smirnoff }
52753b3a8eb9SGleb Smirnoff 
52763b3a8eb9SGleb Smirnoff int
52773b3a8eb9SGleb Smirnoff disallow_table(struct node_host *h, const char *fmt)
52783b3a8eb9SGleb Smirnoff {
52793b3a8eb9SGleb Smirnoff 	for (; h != NULL; h = h->next)
52803b3a8eb9SGleb Smirnoff 		if (h->addr.type == PF_ADDR_TABLE) {
52813b3a8eb9SGleb Smirnoff 			yyerror(fmt, h->addr.v.tblname);
52823b3a8eb9SGleb Smirnoff 			return (1);
52833b3a8eb9SGleb Smirnoff 		}
52843b3a8eb9SGleb Smirnoff 	return (0);
52853b3a8eb9SGleb Smirnoff }
52863b3a8eb9SGleb Smirnoff 
52873b3a8eb9SGleb Smirnoff int
52883b3a8eb9SGleb Smirnoff disallow_urpf_failed(struct node_host *h, const char *fmt)
52893b3a8eb9SGleb Smirnoff {
52903b3a8eb9SGleb Smirnoff 	for (; h != NULL; h = h->next)
52913b3a8eb9SGleb Smirnoff 		if (h->addr.type == PF_ADDR_URPFFAILED) {
52923b3a8eb9SGleb Smirnoff 			yyerror(fmt);
52933b3a8eb9SGleb Smirnoff 			return (1);
52943b3a8eb9SGleb Smirnoff 		}
52953b3a8eb9SGleb Smirnoff 	return (0);
52963b3a8eb9SGleb Smirnoff }
52973b3a8eb9SGleb Smirnoff 
52983b3a8eb9SGleb Smirnoff int
52993b3a8eb9SGleb Smirnoff disallow_alias(struct node_host *h, const char *fmt)
53003b3a8eb9SGleb Smirnoff {
53013b3a8eb9SGleb Smirnoff 	for (; h != NULL; h = h->next)
53023b3a8eb9SGleb Smirnoff 		if (DYNIF_MULTIADDR(h->addr)) {
53033b3a8eb9SGleb Smirnoff 			yyerror(fmt, h->addr.v.tblname);
53043b3a8eb9SGleb Smirnoff 			return (1);
53053b3a8eb9SGleb Smirnoff 		}
53063b3a8eb9SGleb Smirnoff 	return (0);
53073b3a8eb9SGleb Smirnoff }
53083b3a8eb9SGleb Smirnoff 
53093b3a8eb9SGleb Smirnoff int
5310e9eb0941SKristof Provost rule_consistent(struct pfctl_rule *r, int anchor_call)
53113b3a8eb9SGleb Smirnoff {
53123b3a8eb9SGleb Smirnoff 	int	problems = 0;
53133b3a8eb9SGleb Smirnoff 
53143b3a8eb9SGleb Smirnoff 	switch (r->action) {
53153b3a8eb9SGleb Smirnoff 	case PF_PASS:
531639282ef3SKajetan Staszkiewicz 	case PF_MATCH:
53173b3a8eb9SGleb Smirnoff 	case PF_DROP:
53183b3a8eb9SGleb Smirnoff 	case PF_SCRUB:
53193b3a8eb9SGleb Smirnoff 	case PF_NOSCRUB:
53203b3a8eb9SGleb Smirnoff 		problems = filter_consistent(r, anchor_call);
53213b3a8eb9SGleb Smirnoff 		break;
53223b3a8eb9SGleb Smirnoff 	case PF_NAT:
53233b3a8eb9SGleb Smirnoff 	case PF_NONAT:
53243b3a8eb9SGleb Smirnoff 		problems = nat_consistent(r);
53253b3a8eb9SGleb Smirnoff 		break;
53263b3a8eb9SGleb Smirnoff 	case PF_RDR:
53273b3a8eb9SGleb Smirnoff 	case PF_NORDR:
53283b3a8eb9SGleb Smirnoff 		problems = rdr_consistent(r);
53293b3a8eb9SGleb Smirnoff 		break;
53303b3a8eb9SGleb Smirnoff 	case PF_BINAT:
53313b3a8eb9SGleb Smirnoff 	case PF_NOBINAT:
53323b3a8eb9SGleb Smirnoff 	default:
53333b3a8eb9SGleb Smirnoff 		break;
53343b3a8eb9SGleb Smirnoff 	}
53353b3a8eb9SGleb Smirnoff 	return (problems);
53363b3a8eb9SGleb Smirnoff }
53373b3a8eb9SGleb Smirnoff 
53383b3a8eb9SGleb Smirnoff int
5339e9eb0941SKristof Provost filter_consistent(struct pfctl_rule *r, int anchor_call)
53403b3a8eb9SGleb Smirnoff {
53413b3a8eb9SGleb Smirnoff 	int	problems = 0;
53423b3a8eb9SGleb Smirnoff 
53433b3a8eb9SGleb Smirnoff 	if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP &&
53440bd4a683SKristof Provost 	    r->proto != IPPROTO_SCTP &&
53453b3a8eb9SGleb Smirnoff 	    (r->src.port_op || r->dst.port_op)) {
53460bd4a683SKristof Provost 		yyerror("port only applies to tcp/udp/sctp");
53473b3a8eb9SGleb Smirnoff 		problems++;
53483b3a8eb9SGleb Smirnoff 	}
53493b3a8eb9SGleb Smirnoff 	if (r->proto != IPPROTO_ICMP && r->proto != IPPROTO_ICMPV6 &&
53503b3a8eb9SGleb Smirnoff 	    (r->type || r->code)) {
53513b3a8eb9SGleb Smirnoff 		yyerror("icmp-type/code only applies to icmp");
53523b3a8eb9SGleb Smirnoff 		problems++;
53533b3a8eb9SGleb Smirnoff 	}
53543b3a8eb9SGleb Smirnoff 	if (!r->af && (r->type || r->code)) {
53553b3a8eb9SGleb Smirnoff 		yyerror("must indicate address family with icmp-type/code");
53563b3a8eb9SGleb Smirnoff 		problems++;
53573b3a8eb9SGleb Smirnoff 	}
53583b3a8eb9SGleb Smirnoff 	if (r->overload_tblname[0] &&
53593b3a8eb9SGleb Smirnoff 	    r->max_src_conn == 0 && r->max_src_conn_rate.seconds == 0) {
53603b3a8eb9SGleb Smirnoff 		yyerror("'overload' requires 'max-src-conn' "
53613b3a8eb9SGleb Smirnoff 		    "or 'max-src-conn-rate'");
53623b3a8eb9SGleb Smirnoff 		problems++;
53633b3a8eb9SGleb Smirnoff 	}
53643b3a8eb9SGleb Smirnoff 	if ((r->proto == IPPROTO_ICMP && r->af == AF_INET6) ||
53653b3a8eb9SGleb Smirnoff 	    (r->proto == IPPROTO_ICMPV6 && r->af == AF_INET)) {
53663b3a8eb9SGleb Smirnoff 		yyerror("proto %s doesn't match address family %s",
53673b3a8eb9SGleb Smirnoff 		    r->proto == IPPROTO_ICMP ? "icmp" : "icmp6",
53683b3a8eb9SGleb Smirnoff 		    r->af == AF_INET ? "inet" : "inet6");
53693b3a8eb9SGleb Smirnoff 		problems++;
53703b3a8eb9SGleb Smirnoff 	}
53713b3a8eb9SGleb Smirnoff 	if (r->allow_opts && r->action != PF_PASS) {
53723b3a8eb9SGleb Smirnoff 		yyerror("allow-opts can only be specified for pass rules");
53733b3a8eb9SGleb Smirnoff 		problems++;
53743b3a8eb9SGleb Smirnoff 	}
53753b3a8eb9SGleb Smirnoff 	if (r->rule_flag & PFRULE_FRAGMENT && (r->src.port_op ||
53763b3a8eb9SGleb Smirnoff 	    r->dst.port_op || r->flagset || r->type || r->code)) {
53773b3a8eb9SGleb Smirnoff 		yyerror("fragments can be filtered only on IP header fields");
53783b3a8eb9SGleb Smirnoff 		problems++;
53793b3a8eb9SGleb Smirnoff 	}
53803b3a8eb9SGleb Smirnoff 	if (r->rule_flag & PFRULE_RETURNRST && r->proto != IPPROTO_TCP) {
53813b3a8eb9SGleb Smirnoff 		yyerror("return-rst can only be applied to TCP rules");
53823b3a8eb9SGleb Smirnoff 		problems++;
53833b3a8eb9SGleb Smirnoff 	}
53843b3a8eb9SGleb Smirnoff 	if (r->max_src_nodes && !(r->rule_flag & PFRULE_RULESRCTRACK)) {
53853b3a8eb9SGleb Smirnoff 		yyerror("max-src-nodes requires 'source-track rule'");
53863b3a8eb9SGleb Smirnoff 		problems++;
53873b3a8eb9SGleb Smirnoff 	}
538839282ef3SKajetan Staszkiewicz 	if (r->action != PF_PASS && r->keep_state) {
538939282ef3SKajetan Staszkiewicz 		yyerror("keep state is great, but only for pass rules");
53903b3a8eb9SGleb Smirnoff 		problems++;
53913b3a8eb9SGleb Smirnoff 	}
53923b3a8eb9SGleb Smirnoff 	if (r->rule_flag & PFRULE_STATESLOPPY &&
53933b3a8eb9SGleb Smirnoff 	    (r->keep_state == PF_STATE_MODULATE ||
53943b3a8eb9SGleb Smirnoff 	    r->keep_state == PF_STATE_SYNPROXY)) {
53953b3a8eb9SGleb Smirnoff 		yyerror("sloppy state matching cannot be used with "
53963b3a8eb9SGleb Smirnoff 		    "synproxy state or modulate state");
53973b3a8eb9SGleb Smirnoff 		problems++;
53983b3a8eb9SGleb Smirnoff 	}
539939282ef3SKajetan Staszkiewicz 	/* match rules rules */
540039282ef3SKajetan Staszkiewicz 	if (r->action == PF_MATCH) {
540139282ef3SKajetan Staszkiewicz 		if (r->divert.port) {
540239282ef3SKajetan Staszkiewicz 			yyerror("divert is not supported on match rules");
540339282ef3SKajetan Staszkiewicz 			problems++;
540439282ef3SKajetan Staszkiewicz 		}
540539282ef3SKajetan Staszkiewicz 		if (r->rt) {
540639282ef3SKajetan Staszkiewicz 			yyerror("route-to, reply-to, dup-to and fastroute "
540739282ef3SKajetan Staszkiewicz 			   "must not be used on match rules");
540839282ef3SKajetan Staszkiewicz 			problems++;
540939282ef3SKajetan Staszkiewicz 		}
541039282ef3SKajetan Staszkiewicz 	}
5411788f194fSKajetan Staszkiewicz 	if (r->rpool.opts & PF_POOL_STICKYADDR && !r->keep_state) {
5412788f194fSKajetan Staszkiewicz 		yyerror("'sticky-address' requires 'keep state'");
5413788f194fSKajetan Staszkiewicz 		problems++;
5414788f194fSKajetan Staszkiewicz 	}
54153b3a8eb9SGleb Smirnoff 	return (-problems);
54163b3a8eb9SGleb Smirnoff }
54173b3a8eb9SGleb Smirnoff 
54183b3a8eb9SGleb Smirnoff int
5419e9eb0941SKristof Provost nat_consistent(struct pfctl_rule *r)
54203b3a8eb9SGleb Smirnoff {
54213b3a8eb9SGleb Smirnoff 	return (0);	/* yeah! */
54223b3a8eb9SGleb Smirnoff }
54233b3a8eb9SGleb Smirnoff 
54243b3a8eb9SGleb Smirnoff int
5425e9eb0941SKristof Provost rdr_consistent(struct pfctl_rule *r)
54263b3a8eb9SGleb Smirnoff {
54273b3a8eb9SGleb Smirnoff 	int			 problems = 0;
54283b3a8eb9SGleb Smirnoff 
54290bd4a683SKristof Provost 	if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP &&
54300bd4a683SKristof Provost 	    r->proto != IPPROTO_SCTP) {
54313b3a8eb9SGleb Smirnoff 		if (r->src.port_op) {
54320bd4a683SKristof Provost 			yyerror("src port only applies to tcp/udp/sctp");
54333b3a8eb9SGleb Smirnoff 			problems++;
54343b3a8eb9SGleb Smirnoff 		}
54353b3a8eb9SGleb Smirnoff 		if (r->dst.port_op) {
54360bd4a683SKristof Provost 			yyerror("dst port only applies to tcp/udp/sctp");
54373b3a8eb9SGleb Smirnoff 			problems++;
54383b3a8eb9SGleb Smirnoff 		}
54393b3a8eb9SGleb Smirnoff 		if (r->rpool.proxy_port[0]) {
54400bd4a683SKristof Provost 			yyerror("rpool port only applies to tcp/udp/sctp");
54413b3a8eb9SGleb Smirnoff 			problems++;
54423b3a8eb9SGleb Smirnoff 		}
54433b3a8eb9SGleb Smirnoff 	}
54443b3a8eb9SGleb Smirnoff 	if (r->dst.port_op &&
54453b3a8eb9SGleb Smirnoff 	    r->dst.port_op != PF_OP_EQ && r->dst.port_op != PF_OP_RRG) {
54463b3a8eb9SGleb Smirnoff 		yyerror("invalid port operator for rdr destination port");
54473b3a8eb9SGleb Smirnoff 		problems++;
54483b3a8eb9SGleb Smirnoff 	}
54493b3a8eb9SGleb Smirnoff 	return (-problems);
54503b3a8eb9SGleb Smirnoff }
54513b3a8eb9SGleb Smirnoff 
54523b3a8eb9SGleb Smirnoff int
54533b3a8eb9SGleb Smirnoff process_tabledef(char *name, struct table_opts *opts)
54543b3a8eb9SGleb Smirnoff {
54553b3a8eb9SGleb Smirnoff 	struct pfr_buffer	 ab;
54563b3a8eb9SGleb Smirnoff 	struct node_tinit	*ti;
5457542feeffSKristof Provost 	unsigned long		 maxcount;
5458542feeffSKristof Provost 	size_t			 s = sizeof(maxcount);
54593b3a8eb9SGleb Smirnoff 
54603b3a8eb9SGleb Smirnoff 	bzero(&ab, sizeof(ab));
54613b3a8eb9SGleb Smirnoff 	ab.pfrb_type = PFRB_ADDRS;
54623b3a8eb9SGleb Smirnoff 	SIMPLEQ_FOREACH(ti, &opts->init_nodes, entries) {
54633b3a8eb9SGleb Smirnoff 		if (ti->file)
54643b3a8eb9SGleb Smirnoff 			if (pfr_buf_load(&ab, ti->file, 0, append_addr)) {
54653b3a8eb9SGleb Smirnoff 				if (errno)
54663b3a8eb9SGleb Smirnoff 					yyerror("cannot load \"%s\": %s",
54673b3a8eb9SGleb Smirnoff 					    ti->file, strerror(errno));
54683b3a8eb9SGleb Smirnoff 				else
54693b3a8eb9SGleb Smirnoff 					yyerror("file \"%s\" contains bad data",
54703b3a8eb9SGleb Smirnoff 					    ti->file);
54713b3a8eb9SGleb Smirnoff 				goto _error;
54723b3a8eb9SGleb Smirnoff 			}
54733b3a8eb9SGleb Smirnoff 		if (ti->host)
54743b3a8eb9SGleb Smirnoff 			if (append_addr_host(&ab, ti->host, 0, 0)) {
54753b3a8eb9SGleb Smirnoff 				yyerror("cannot create address buffer: %s",
54763b3a8eb9SGleb Smirnoff 				    strerror(errno));
54773b3a8eb9SGleb Smirnoff 				goto _error;
54783b3a8eb9SGleb Smirnoff 			}
54793b3a8eb9SGleb Smirnoff 	}
54803b3a8eb9SGleb Smirnoff 	if (pf->opts & PF_OPT_VERBOSE)
54813b3a8eb9SGleb Smirnoff 		print_tabledef(name, opts->flags, opts->init_addr,
54823b3a8eb9SGleb Smirnoff 		    &opts->init_nodes);
54833b3a8eb9SGleb Smirnoff 	if (!(pf->opts & PF_OPT_NOACTION) &&
54843b3a8eb9SGleb Smirnoff 	    pfctl_define_table(name, opts->flags, opts->init_addr,
54853b3a8eb9SGleb Smirnoff 	    pf->anchor->name, &ab, pf->anchor->ruleset.tticket)) {
5486542feeffSKristof Provost 
5487542feeffSKristof Provost 		if (sysctlbyname("net.pf.request_maxcount", &maxcount, &s,
5488542feeffSKristof Provost 		    NULL, 0) == -1)
5489542feeffSKristof Provost 			maxcount = 65535;
5490542feeffSKristof Provost 
5491542feeffSKristof Provost 		if (ab.pfrb_size > maxcount)
5492542feeffSKristof Provost 			yyerror("cannot define table %s: too many elements.\n"
5493542feeffSKristof Provost 			    "Consider increasing net.pf.request_maxcount.",
5494542feeffSKristof Provost 			    name);
5495542feeffSKristof Provost 		else
54963b3a8eb9SGleb Smirnoff 			yyerror("cannot define table %s: %s", name,
54973b3a8eb9SGleb Smirnoff 			    pfr_strerror(errno));
5498542feeffSKristof Provost 
54993b3a8eb9SGleb Smirnoff 		goto _error;
55003b3a8eb9SGleb Smirnoff 	}
55013b3a8eb9SGleb Smirnoff 	pf->tdirty = 1;
55023b3a8eb9SGleb Smirnoff 	pfr_buf_clear(&ab);
55033b3a8eb9SGleb Smirnoff 	return (0);
55043b3a8eb9SGleb Smirnoff _error:
55053b3a8eb9SGleb Smirnoff 	pfr_buf_clear(&ab);
55063b3a8eb9SGleb Smirnoff 	return (-1);
55073b3a8eb9SGleb Smirnoff }
55083b3a8eb9SGleb Smirnoff 
55093b3a8eb9SGleb Smirnoff struct keywords {
55103b3a8eb9SGleb Smirnoff 	const char	*k_name;
55113b3a8eb9SGleb Smirnoff 	int		 k_val;
55123b3a8eb9SGleb Smirnoff };
55133b3a8eb9SGleb Smirnoff 
55143b3a8eb9SGleb Smirnoff /* macro gore, but you should've seen the prior indentation nightmare... */
55153b3a8eb9SGleb Smirnoff 
55163b3a8eb9SGleb Smirnoff #define FREE_LIST(T,r) \
55173b3a8eb9SGleb Smirnoff 	do { \
55183b3a8eb9SGleb Smirnoff 		T *p, *node = r; \
55193b3a8eb9SGleb Smirnoff 		while (node != NULL) { \
55203b3a8eb9SGleb Smirnoff 			p = node; \
55213b3a8eb9SGleb Smirnoff 			node = node->next; \
55223b3a8eb9SGleb Smirnoff 			free(p); \
55233b3a8eb9SGleb Smirnoff 		} \
55243b3a8eb9SGleb Smirnoff 	} while (0)
55253b3a8eb9SGleb Smirnoff 
55263b3a8eb9SGleb Smirnoff #define LOOP_THROUGH(T,n,r,C) \
55273b3a8eb9SGleb Smirnoff 	do { \
55283b3a8eb9SGleb Smirnoff 		T *n; \
55293b3a8eb9SGleb Smirnoff 		if (r == NULL) { \
55303b3a8eb9SGleb Smirnoff 			r = calloc(1, sizeof(T)); \
55313b3a8eb9SGleb Smirnoff 			if (r == NULL) \
55323b3a8eb9SGleb Smirnoff 				err(1, "LOOP: calloc"); \
55333b3a8eb9SGleb Smirnoff 			r->next = NULL; \
55343b3a8eb9SGleb Smirnoff 		} \
55353b3a8eb9SGleb Smirnoff 		n = r; \
55363b3a8eb9SGleb Smirnoff 		while (n != NULL) { \
55373b3a8eb9SGleb Smirnoff 			do { \
55383b3a8eb9SGleb Smirnoff 				C; \
55393b3a8eb9SGleb Smirnoff 			} while (0); \
55403b3a8eb9SGleb Smirnoff 			n = n->next; \
55413b3a8eb9SGleb Smirnoff 		} \
55423b3a8eb9SGleb Smirnoff 	} while (0)
55433b3a8eb9SGleb Smirnoff 
55443b3a8eb9SGleb Smirnoff void
55453b3a8eb9SGleb Smirnoff expand_label_str(char *label, size_t len, const char *srch, const char *repl)
55463b3a8eb9SGleb Smirnoff {
55473b3a8eb9SGleb Smirnoff 	char *tmp;
55483b3a8eb9SGleb Smirnoff 	char *p, *q;
55493b3a8eb9SGleb Smirnoff 
55503b3a8eb9SGleb Smirnoff 	if ((tmp = calloc(1, len)) == NULL)
55513b3a8eb9SGleb Smirnoff 		err(1, "expand_label_str: calloc");
55523b3a8eb9SGleb Smirnoff 	p = q = label;
55533b3a8eb9SGleb Smirnoff 	while ((q = strstr(p, srch)) != NULL) {
55543b3a8eb9SGleb Smirnoff 		*q = '\0';
55553b3a8eb9SGleb Smirnoff 		if ((strlcat(tmp, p, len) >= len) ||
55563b3a8eb9SGleb Smirnoff 		    (strlcat(tmp, repl, len) >= len))
55573b3a8eb9SGleb Smirnoff 			errx(1, "expand_label: label too long");
55583b3a8eb9SGleb Smirnoff 		q += strlen(srch);
55593b3a8eb9SGleb Smirnoff 		p = q;
55603b3a8eb9SGleb Smirnoff 	}
55613b3a8eb9SGleb Smirnoff 	if (strlcat(tmp, p, len) >= len)
55623b3a8eb9SGleb Smirnoff 		errx(1, "expand_label: label too long");
55633b3a8eb9SGleb Smirnoff 	strlcpy(label, tmp, len);	/* always fits */
55643b3a8eb9SGleb Smirnoff 	free(tmp);
55653b3a8eb9SGleb Smirnoff }
55663b3a8eb9SGleb Smirnoff 
55673b3a8eb9SGleb Smirnoff void
55683b3a8eb9SGleb Smirnoff expand_label_if(const char *name, char *label, size_t len, const char *ifname)
55693b3a8eb9SGleb Smirnoff {
55703b3a8eb9SGleb Smirnoff 	if (strstr(label, name) != NULL) {
55713b3a8eb9SGleb Smirnoff 		if (!*ifname)
55723b3a8eb9SGleb Smirnoff 			expand_label_str(label, len, name, "any");
55733b3a8eb9SGleb Smirnoff 		else
55743b3a8eb9SGleb Smirnoff 			expand_label_str(label, len, name, ifname);
55753b3a8eb9SGleb Smirnoff 	}
55763b3a8eb9SGleb Smirnoff }
55773b3a8eb9SGleb Smirnoff 
55783b3a8eb9SGleb Smirnoff void
55793b3a8eb9SGleb Smirnoff expand_label_addr(const char *name, char *label, size_t len, sa_family_t af,
558009c7f238SKristof Provost     struct pf_rule_addr *addr)
55813b3a8eb9SGleb Smirnoff {
55823b3a8eb9SGleb Smirnoff 	char tmp[64], tmp_not[66];
55833b3a8eb9SGleb Smirnoff 
55843b3a8eb9SGleb Smirnoff 	if (strstr(label, name) != NULL) {
558509c7f238SKristof Provost 		switch (addr->addr.type) {
55863b3a8eb9SGleb Smirnoff 		case PF_ADDR_DYNIFTL:
558709c7f238SKristof Provost 			snprintf(tmp, sizeof(tmp), "(%s)", addr->addr.v.ifname);
55883b3a8eb9SGleb Smirnoff 			break;
55893b3a8eb9SGleb Smirnoff 		case PF_ADDR_TABLE:
559009c7f238SKristof Provost 			snprintf(tmp, sizeof(tmp), "<%s>", addr->addr.v.tblname);
55913b3a8eb9SGleb Smirnoff 			break;
55923b3a8eb9SGleb Smirnoff 		case PF_ADDR_NOROUTE:
55933b3a8eb9SGleb Smirnoff 			snprintf(tmp, sizeof(tmp), "no-route");
55943b3a8eb9SGleb Smirnoff 			break;
55953b3a8eb9SGleb Smirnoff 		case PF_ADDR_URPFFAILED:
55963b3a8eb9SGleb Smirnoff 			snprintf(tmp, sizeof(tmp), "urpf-failed");
55973b3a8eb9SGleb Smirnoff 			break;
55983b3a8eb9SGleb Smirnoff 		case PF_ADDR_ADDRMASK:
559909c7f238SKristof Provost 			if (!af || (PF_AZERO(&addr->addr.v.a.addr, af) &&
560009c7f238SKristof Provost 			    PF_AZERO(&addr->addr.v.a.mask, af)))
56013b3a8eb9SGleb Smirnoff 				snprintf(tmp, sizeof(tmp), "any");
56023b3a8eb9SGleb Smirnoff 			else {
56033b3a8eb9SGleb Smirnoff 				char	a[48];
56043b3a8eb9SGleb Smirnoff 				int	bits;
56053b3a8eb9SGleb Smirnoff 
560609c7f238SKristof Provost 				if (inet_ntop(af, &addr->addr.v.a.addr, a,
56073b3a8eb9SGleb Smirnoff 				    sizeof(a)) == NULL)
56083b3a8eb9SGleb Smirnoff 					snprintf(tmp, sizeof(tmp), "?");
56093b3a8eb9SGleb Smirnoff 				else {
561009c7f238SKristof Provost 					bits = unmask(&addr->addr.v.a.mask, af);
56113b3a8eb9SGleb Smirnoff 					if ((af == AF_INET && bits < 32) ||
56123b3a8eb9SGleb Smirnoff 					    (af == AF_INET6 && bits < 128))
56133b3a8eb9SGleb Smirnoff 						snprintf(tmp, sizeof(tmp),
56143b3a8eb9SGleb Smirnoff 						    "%s/%d", a, bits);
56153b3a8eb9SGleb Smirnoff 					else
56163b3a8eb9SGleb Smirnoff 						snprintf(tmp, sizeof(tmp),
56173b3a8eb9SGleb Smirnoff 						    "%s", a);
56183b3a8eb9SGleb Smirnoff 				}
56193b3a8eb9SGleb Smirnoff 			}
56203b3a8eb9SGleb Smirnoff 			break;
56213b3a8eb9SGleb Smirnoff 		default:
56223b3a8eb9SGleb Smirnoff 			snprintf(tmp, sizeof(tmp), "?");
56233b3a8eb9SGleb Smirnoff 			break;
56243b3a8eb9SGleb Smirnoff 		}
56253b3a8eb9SGleb Smirnoff 
562609c7f238SKristof Provost 		if (addr->neg) {
56273b3a8eb9SGleb Smirnoff 			snprintf(tmp_not, sizeof(tmp_not), "! %s", tmp);
56283b3a8eb9SGleb Smirnoff 			expand_label_str(label, len, name, tmp_not);
56293b3a8eb9SGleb Smirnoff 		} else
56303b3a8eb9SGleb Smirnoff 			expand_label_str(label, len, name, tmp);
56313b3a8eb9SGleb Smirnoff 	}
56323b3a8eb9SGleb Smirnoff }
56333b3a8eb9SGleb Smirnoff 
56343b3a8eb9SGleb Smirnoff void
56353b3a8eb9SGleb Smirnoff expand_label_port(const char *name, char *label, size_t len,
563609c7f238SKristof Provost     struct pf_rule_addr *addr)
56373b3a8eb9SGleb Smirnoff {
56383b3a8eb9SGleb Smirnoff 	char	 a1[6], a2[6], op[13] = "";
56393b3a8eb9SGleb Smirnoff 
56403b3a8eb9SGleb Smirnoff 	if (strstr(label, name) != NULL) {
564109c7f238SKristof Provost 		snprintf(a1, sizeof(a1), "%u", ntohs(addr->port[0]));
564209c7f238SKristof Provost 		snprintf(a2, sizeof(a2), "%u", ntohs(addr->port[1]));
564309c7f238SKristof Provost 		if (!addr->port_op)
56443b3a8eb9SGleb Smirnoff 			;
564509c7f238SKristof Provost 		else if (addr->port_op == PF_OP_IRG)
56463b3a8eb9SGleb Smirnoff 			snprintf(op, sizeof(op), "%s><%s", a1, a2);
564709c7f238SKristof Provost 		else if (addr->port_op == PF_OP_XRG)
56483b3a8eb9SGleb Smirnoff 			snprintf(op, sizeof(op), "%s<>%s", a1, a2);
564909c7f238SKristof Provost 		else if (addr->port_op == PF_OP_EQ)
56503b3a8eb9SGleb Smirnoff 			snprintf(op, sizeof(op), "%s", a1);
565109c7f238SKristof Provost 		else if (addr->port_op == PF_OP_NE)
56523b3a8eb9SGleb Smirnoff 			snprintf(op, sizeof(op), "!=%s", a1);
565309c7f238SKristof Provost 		else if (addr->port_op == PF_OP_LT)
56543b3a8eb9SGleb Smirnoff 			snprintf(op, sizeof(op), "<%s", a1);
565509c7f238SKristof Provost 		else if (addr->port_op == PF_OP_LE)
56563b3a8eb9SGleb Smirnoff 			snprintf(op, sizeof(op), "<=%s", a1);
565709c7f238SKristof Provost 		else if (addr->port_op == PF_OP_GT)
56583b3a8eb9SGleb Smirnoff 			snprintf(op, sizeof(op), ">%s", a1);
565909c7f238SKristof Provost 		else if (addr->port_op == PF_OP_GE)
56603b3a8eb9SGleb Smirnoff 			snprintf(op, sizeof(op), ">=%s", a1);
56613b3a8eb9SGleb Smirnoff 		expand_label_str(label, len, name, op);
56623b3a8eb9SGleb Smirnoff 	}
56633b3a8eb9SGleb Smirnoff }
56643b3a8eb9SGleb Smirnoff 
56653b3a8eb9SGleb Smirnoff void
56663b3a8eb9SGleb Smirnoff expand_label_proto(const char *name, char *label, size_t len, u_int8_t proto)
56673b3a8eb9SGleb Smirnoff {
5668858937beSMateusz Guzik 	const char *protoname;
56693b3a8eb9SGleb Smirnoff 	char n[4];
56703b3a8eb9SGleb Smirnoff 
56713b3a8eb9SGleb Smirnoff 	if (strstr(label, name) != NULL) {
5672858937beSMateusz Guzik 		protoname = pfctl_proto2name(proto);
5673858937beSMateusz Guzik 		if (protoname != NULL)
5674858937beSMateusz Guzik 			expand_label_str(label, len, name, protoname);
56753b3a8eb9SGleb Smirnoff 		else {
56763b3a8eb9SGleb Smirnoff 			snprintf(n, sizeof(n), "%u", proto);
56773b3a8eb9SGleb Smirnoff 			expand_label_str(label, len, name, n);
56783b3a8eb9SGleb Smirnoff 		}
56793b3a8eb9SGleb Smirnoff 	}
56803b3a8eb9SGleb Smirnoff }
56813b3a8eb9SGleb Smirnoff 
56823b3a8eb9SGleb Smirnoff void
568309c7f238SKristof Provost expand_label_nr(const char *name, char *label, size_t len,
568409c7f238SKristof Provost     struct pfctl_rule *r)
56853b3a8eb9SGleb Smirnoff {
56863b3a8eb9SGleb Smirnoff 	char n[11];
56873b3a8eb9SGleb Smirnoff 
56883b3a8eb9SGleb Smirnoff 	if (strstr(label, name) != NULL) {
568909c7f238SKristof Provost 		snprintf(n, sizeof(n), "%u", r->nr);
56903b3a8eb9SGleb Smirnoff 		expand_label_str(label, len, name, n);
56913b3a8eb9SGleb Smirnoff 	}
56923b3a8eb9SGleb Smirnoff }
56933b3a8eb9SGleb Smirnoff 
56943b3a8eb9SGleb Smirnoff void
569509c7f238SKristof Provost expand_label(char *label, size_t len, struct pfctl_rule *r)
56963b3a8eb9SGleb Smirnoff {
569709c7f238SKristof Provost 	expand_label_if("$if", label, len, r->ifname);
569809c7f238SKristof Provost 	expand_label_addr("$srcaddr", label, len, r->af, &r->src);
569909c7f238SKristof Provost 	expand_label_addr("$dstaddr", label, len, r->af, &r->dst);
570009c7f238SKristof Provost 	expand_label_port("$srcport", label, len, &r->src);
570109c7f238SKristof Provost 	expand_label_port("$dstport", label, len, &r->dst);
570209c7f238SKristof Provost 	expand_label_proto("$proto", label, len, r->proto);
570309c7f238SKristof Provost 	expand_label_nr("$nr", label, len, r);
57043b3a8eb9SGleb Smirnoff }
57053b3a8eb9SGleb Smirnoff 
57063b3a8eb9SGleb Smirnoff int
57073b3a8eb9SGleb Smirnoff expand_altq(struct pf_altq *a, struct node_if *interfaces,
57083b3a8eb9SGleb Smirnoff     struct node_queue *nqueues, struct node_queue_bw bwspec,
57093b3a8eb9SGleb Smirnoff     struct node_queue_opt *opts)
57103b3a8eb9SGleb Smirnoff {
57113b3a8eb9SGleb Smirnoff 	struct pf_altq		 pa, pb;
57123b3a8eb9SGleb Smirnoff 	char			 qname[PF_QNAME_SIZE];
57133b3a8eb9SGleb Smirnoff 	struct node_queue	*n;
57143b3a8eb9SGleb Smirnoff 	struct node_queue_bw	 bw;
57153b3a8eb9SGleb Smirnoff 	int			 errs = 0;
57163b3a8eb9SGleb Smirnoff 
57173b3a8eb9SGleb Smirnoff 	if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
57183b3a8eb9SGleb Smirnoff 		FREE_LIST(struct node_if, interfaces);
57190a70aaf8SLuiz Otavio O Souza 		if (nqueues)
57203b3a8eb9SGleb Smirnoff 			FREE_LIST(struct node_queue, nqueues);
57213b3a8eb9SGleb Smirnoff 		return (0);
57223b3a8eb9SGleb Smirnoff 	}
57233b3a8eb9SGleb Smirnoff 
57243b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_if, interface, interfaces,
57253b3a8eb9SGleb Smirnoff 		memcpy(&pa, a, sizeof(struct pf_altq));
57263b3a8eb9SGleb Smirnoff 		if (strlcpy(pa.ifname, interface->ifname,
57273b3a8eb9SGleb Smirnoff 		    sizeof(pa.ifname)) >= sizeof(pa.ifname))
57283b3a8eb9SGleb Smirnoff 			errx(1, "expand_altq: strlcpy");
57293b3a8eb9SGleb Smirnoff 
57303b3a8eb9SGleb Smirnoff 		if (interface->not) {
57313b3a8eb9SGleb Smirnoff 			yyerror("altq on ! <interface> is not supported");
57323b3a8eb9SGleb Smirnoff 			errs++;
57333b3a8eb9SGleb Smirnoff 		} else {
57343b3a8eb9SGleb Smirnoff 			if (eval_pfaltq(pf, &pa, &bwspec, opts))
57353b3a8eb9SGleb Smirnoff 				errs++;
57363b3a8eb9SGleb Smirnoff 			else
57373b3a8eb9SGleb Smirnoff 				if (pfctl_add_altq(pf, &pa))
57383b3a8eb9SGleb Smirnoff 					errs++;
57393b3a8eb9SGleb Smirnoff 
57403b3a8eb9SGleb Smirnoff 			if (pf->opts & PF_OPT_VERBOSE) {
57413b3a8eb9SGleb Smirnoff 				print_altq(&pf->paltq->altq, 0,
57423b3a8eb9SGleb Smirnoff 				    &bwspec, opts);
57433b3a8eb9SGleb Smirnoff 				if (nqueues && nqueues->tail) {
57443b3a8eb9SGleb Smirnoff 					printf("queue { ");
57453b3a8eb9SGleb Smirnoff 					LOOP_THROUGH(struct node_queue, queue,
57463b3a8eb9SGleb Smirnoff 					    nqueues,
57473b3a8eb9SGleb Smirnoff 						printf("%s ",
57483b3a8eb9SGleb Smirnoff 						    queue->queue);
57493b3a8eb9SGleb Smirnoff 					);
57503b3a8eb9SGleb Smirnoff 					printf("}");
57513b3a8eb9SGleb Smirnoff 				}
57523b3a8eb9SGleb Smirnoff 				printf("\n");
57533b3a8eb9SGleb Smirnoff 			}
57543b3a8eb9SGleb Smirnoff 
57553b3a8eb9SGleb Smirnoff 			if (pa.scheduler == ALTQT_CBQ ||
5756dc784287SKristof Provost 			    pa.scheduler == ALTQT_HFSC ||
5757dc784287SKristof Provost 			    pa.scheduler == ALTQT_FAIRQ) {
57583b3a8eb9SGleb Smirnoff 				/* now create a root queue */
57593b3a8eb9SGleb Smirnoff 				memset(&pb, 0, sizeof(struct pf_altq));
57603b3a8eb9SGleb Smirnoff 				if (strlcpy(qname, "root_", sizeof(qname)) >=
57613b3a8eb9SGleb Smirnoff 				    sizeof(qname))
57623b3a8eb9SGleb Smirnoff 					errx(1, "expand_altq: strlcpy");
57633b3a8eb9SGleb Smirnoff 				if (strlcat(qname, interface->ifname,
57643b3a8eb9SGleb Smirnoff 				    sizeof(qname)) >= sizeof(qname))
57653b3a8eb9SGleb Smirnoff 					errx(1, "expand_altq: strlcat");
57663b3a8eb9SGleb Smirnoff 				if (strlcpy(pb.qname, qname,
57673b3a8eb9SGleb Smirnoff 				    sizeof(pb.qname)) >= sizeof(pb.qname))
57683b3a8eb9SGleb Smirnoff 					errx(1, "expand_altq: strlcpy");
57693b3a8eb9SGleb Smirnoff 				if (strlcpy(pb.ifname, interface->ifname,
57703b3a8eb9SGleb Smirnoff 				    sizeof(pb.ifname)) >= sizeof(pb.ifname))
57713b3a8eb9SGleb Smirnoff 					errx(1, "expand_altq: strlcpy");
57723b3a8eb9SGleb Smirnoff 				pb.qlimit = pa.qlimit;
57733b3a8eb9SGleb Smirnoff 				pb.scheduler = pa.scheduler;
57743b3a8eb9SGleb Smirnoff 				bw.bw_absolute = pa.ifbandwidth;
57753b3a8eb9SGleb Smirnoff 				bw.bw_percent = 0;
57763b3a8eb9SGleb Smirnoff 				if (eval_pfqueue(pf, &pb, &bw, opts))
57773b3a8eb9SGleb Smirnoff 					errs++;
57783b3a8eb9SGleb Smirnoff 				else
57793b3a8eb9SGleb Smirnoff 					if (pfctl_add_altq(pf, &pb))
57803b3a8eb9SGleb Smirnoff 						errs++;
57813b3a8eb9SGleb Smirnoff 			}
57823b3a8eb9SGleb Smirnoff 
57833b3a8eb9SGleb Smirnoff 			LOOP_THROUGH(struct node_queue, queue, nqueues,
57843b3a8eb9SGleb Smirnoff 				n = calloc(1, sizeof(struct node_queue));
57853b3a8eb9SGleb Smirnoff 				if (n == NULL)
57863b3a8eb9SGleb Smirnoff 					err(1, "expand_altq: calloc");
57873b3a8eb9SGleb Smirnoff 				if (pa.scheduler == ALTQT_CBQ ||
5788dc784287SKristof Provost 				    pa.scheduler == ALTQT_HFSC ||
5789dc784287SKristof Provost 				    pa.scheduler == ALTQT_FAIRQ)
57903b3a8eb9SGleb Smirnoff 					if (strlcpy(n->parent, qname,
57913b3a8eb9SGleb Smirnoff 					    sizeof(n->parent)) >=
57923b3a8eb9SGleb Smirnoff 					    sizeof(n->parent))
57933b3a8eb9SGleb Smirnoff 						errx(1, "expand_altq: strlcpy");
57943b3a8eb9SGleb Smirnoff 				if (strlcpy(n->queue, queue->queue,
57953b3a8eb9SGleb Smirnoff 				    sizeof(n->queue)) >= sizeof(n->queue))
57963b3a8eb9SGleb Smirnoff 					errx(1, "expand_altq: strlcpy");
57973b3a8eb9SGleb Smirnoff 				if (strlcpy(n->ifname, interface->ifname,
57983b3a8eb9SGleb Smirnoff 				    sizeof(n->ifname)) >= sizeof(n->ifname))
57993b3a8eb9SGleb Smirnoff 					errx(1, "expand_altq: strlcpy");
58003b3a8eb9SGleb Smirnoff 				n->scheduler = pa.scheduler;
58013b3a8eb9SGleb Smirnoff 				n->next = NULL;
58023b3a8eb9SGleb Smirnoff 				n->tail = n;
58033b3a8eb9SGleb Smirnoff 				if (queues == NULL)
58043b3a8eb9SGleb Smirnoff 					queues = n;
58053b3a8eb9SGleb Smirnoff 				else {
58063b3a8eb9SGleb Smirnoff 					queues->tail->next = n;
58073b3a8eb9SGleb Smirnoff 					queues->tail = n;
58083b3a8eb9SGleb Smirnoff 				}
58093b3a8eb9SGleb Smirnoff 			);
58103b3a8eb9SGleb Smirnoff 		}
58113b3a8eb9SGleb Smirnoff 	);
58123b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_if, interfaces);
58130a70aaf8SLuiz Otavio O Souza 	if (nqueues)
58143b3a8eb9SGleb Smirnoff 		FREE_LIST(struct node_queue, nqueues);
58153b3a8eb9SGleb Smirnoff 
58163b3a8eb9SGleb Smirnoff 	return (errs);
58173b3a8eb9SGleb Smirnoff }
58183b3a8eb9SGleb Smirnoff 
58193b3a8eb9SGleb Smirnoff int
58203b3a8eb9SGleb Smirnoff expand_queue(struct pf_altq *a, struct node_if *interfaces,
58213b3a8eb9SGleb Smirnoff     struct node_queue *nqueues, struct node_queue_bw bwspec,
58223b3a8eb9SGleb Smirnoff     struct node_queue_opt *opts)
58233b3a8eb9SGleb Smirnoff {
58243b3a8eb9SGleb Smirnoff 	struct node_queue	*n, *nq;
58253b3a8eb9SGleb Smirnoff 	struct pf_altq		 pa;
58263b3a8eb9SGleb Smirnoff 	u_int8_t		 found = 0;
58273b3a8eb9SGleb Smirnoff 	u_int8_t		 errs = 0;
58283b3a8eb9SGleb Smirnoff 
58293b3a8eb9SGleb Smirnoff 	if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
58303b3a8eb9SGleb Smirnoff 		FREE_LIST(struct node_queue, nqueues);
58313b3a8eb9SGleb Smirnoff 		return (0);
58323b3a8eb9SGleb Smirnoff 	}
58333b3a8eb9SGleb Smirnoff 
58343b3a8eb9SGleb Smirnoff 	if (queues == NULL) {
58353b3a8eb9SGleb Smirnoff 		yyerror("queue %s has no parent", a->qname);
58363b3a8eb9SGleb Smirnoff 		FREE_LIST(struct node_queue, nqueues);
58373b3a8eb9SGleb Smirnoff 		return (1);
58383b3a8eb9SGleb Smirnoff 	}
58393b3a8eb9SGleb Smirnoff 
58403b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_if, interface, interfaces,
58413b3a8eb9SGleb Smirnoff 		LOOP_THROUGH(struct node_queue, tqueue, queues,
58423b3a8eb9SGleb Smirnoff 			if (!strncmp(a->qname, tqueue->queue, PF_QNAME_SIZE) &&
58433b3a8eb9SGleb Smirnoff 			    (interface->ifname[0] == 0 ||
58443b3a8eb9SGleb Smirnoff 			    (!interface->not && !strncmp(interface->ifname,
58453b3a8eb9SGleb Smirnoff 			    tqueue->ifname, IFNAMSIZ)) ||
58463b3a8eb9SGleb Smirnoff 			    (interface->not && strncmp(interface->ifname,
58473b3a8eb9SGleb Smirnoff 			    tqueue->ifname, IFNAMSIZ)))) {
58483b3a8eb9SGleb Smirnoff 				/* found ourself in queues */
58493b3a8eb9SGleb Smirnoff 				found++;
58503b3a8eb9SGleb Smirnoff 
58513b3a8eb9SGleb Smirnoff 				memcpy(&pa, a, sizeof(struct pf_altq));
58523b3a8eb9SGleb Smirnoff 
58533b3a8eb9SGleb Smirnoff 				if (pa.scheduler != ALTQT_NONE &&
58543b3a8eb9SGleb Smirnoff 				    pa.scheduler != tqueue->scheduler) {
58553b3a8eb9SGleb Smirnoff 					yyerror("exactly one scheduler type "
58563b3a8eb9SGleb Smirnoff 					    "per interface allowed");
58573b3a8eb9SGleb Smirnoff 					return (1);
58583b3a8eb9SGleb Smirnoff 				}
58593b3a8eb9SGleb Smirnoff 				pa.scheduler = tqueue->scheduler;
58603b3a8eb9SGleb Smirnoff 
58613b3a8eb9SGleb Smirnoff 				/* scheduler dependent error checking */
58623b3a8eb9SGleb Smirnoff 				switch (pa.scheduler) {
58633b3a8eb9SGleb Smirnoff 				case ALTQT_PRIQ:
58643b3a8eb9SGleb Smirnoff 					if (nqueues != NULL) {
58653b3a8eb9SGleb Smirnoff 						yyerror("priq queues cannot "
58663b3a8eb9SGleb Smirnoff 						    "have child queues");
58673b3a8eb9SGleb Smirnoff 						return (1);
58683b3a8eb9SGleb Smirnoff 					}
58693b3a8eb9SGleb Smirnoff 					if (bwspec.bw_absolute > 0 ||
58703b3a8eb9SGleb Smirnoff 					    bwspec.bw_percent < 100) {
58713b3a8eb9SGleb Smirnoff 						yyerror("priq doesn't take "
58723b3a8eb9SGleb Smirnoff 						    "bandwidth");
58733b3a8eb9SGleb Smirnoff 						return (1);
58743b3a8eb9SGleb Smirnoff 					}
58753b3a8eb9SGleb Smirnoff 					break;
58763b3a8eb9SGleb Smirnoff 				default:
58773b3a8eb9SGleb Smirnoff 					break;
58783b3a8eb9SGleb Smirnoff 				}
58793b3a8eb9SGleb Smirnoff 
58803b3a8eb9SGleb Smirnoff 				if (strlcpy(pa.ifname, tqueue->ifname,
58813b3a8eb9SGleb Smirnoff 				    sizeof(pa.ifname)) >= sizeof(pa.ifname))
58823b3a8eb9SGleb Smirnoff 					errx(1, "expand_queue: strlcpy");
58833b3a8eb9SGleb Smirnoff 				if (strlcpy(pa.parent, tqueue->parent,
58843b3a8eb9SGleb Smirnoff 				    sizeof(pa.parent)) >= sizeof(pa.parent))
58853b3a8eb9SGleb Smirnoff 					errx(1, "expand_queue: strlcpy");
58863b3a8eb9SGleb Smirnoff 
58873b3a8eb9SGleb Smirnoff 				if (eval_pfqueue(pf, &pa, &bwspec, opts))
58883b3a8eb9SGleb Smirnoff 					errs++;
58893b3a8eb9SGleb Smirnoff 				else
58903b3a8eb9SGleb Smirnoff 					if (pfctl_add_altq(pf, &pa))
58913b3a8eb9SGleb Smirnoff 						errs++;
58923b3a8eb9SGleb Smirnoff 
58933b3a8eb9SGleb Smirnoff 				for (nq = nqueues; nq != NULL; nq = nq->next) {
58943b3a8eb9SGleb Smirnoff 					if (!strcmp(a->qname, nq->queue)) {
58953b3a8eb9SGleb Smirnoff 						yyerror("queue cannot have "
58963b3a8eb9SGleb Smirnoff 						    "itself as child");
58973b3a8eb9SGleb Smirnoff 						errs++;
58983b3a8eb9SGleb Smirnoff 						continue;
58993b3a8eb9SGleb Smirnoff 					}
59003b3a8eb9SGleb Smirnoff 					n = calloc(1,
59013b3a8eb9SGleb Smirnoff 					    sizeof(struct node_queue));
59023b3a8eb9SGleb Smirnoff 					if (n == NULL)
59033b3a8eb9SGleb Smirnoff 						err(1, "expand_queue: calloc");
59043b3a8eb9SGleb Smirnoff 					if (strlcpy(n->parent, a->qname,
59053b3a8eb9SGleb Smirnoff 					    sizeof(n->parent)) >=
59063b3a8eb9SGleb Smirnoff 					    sizeof(n->parent))
59073b3a8eb9SGleb Smirnoff 						errx(1, "expand_queue strlcpy");
59083b3a8eb9SGleb Smirnoff 					if (strlcpy(n->queue, nq->queue,
59093b3a8eb9SGleb Smirnoff 					    sizeof(n->queue)) >=
59103b3a8eb9SGleb Smirnoff 					    sizeof(n->queue))
59113b3a8eb9SGleb Smirnoff 						errx(1, "expand_queue strlcpy");
59123b3a8eb9SGleb Smirnoff 					if (strlcpy(n->ifname, tqueue->ifname,
59133b3a8eb9SGleb Smirnoff 					    sizeof(n->ifname)) >=
59143b3a8eb9SGleb Smirnoff 					    sizeof(n->ifname))
59153b3a8eb9SGleb Smirnoff 						errx(1, "expand_queue strlcpy");
59163b3a8eb9SGleb Smirnoff 					n->scheduler = tqueue->scheduler;
59173b3a8eb9SGleb Smirnoff 					n->next = NULL;
59183b3a8eb9SGleb Smirnoff 					n->tail = n;
59193b3a8eb9SGleb Smirnoff 					if (queues == NULL)
59203b3a8eb9SGleb Smirnoff 						queues = n;
59213b3a8eb9SGleb Smirnoff 					else {
59223b3a8eb9SGleb Smirnoff 						queues->tail->next = n;
59233b3a8eb9SGleb Smirnoff 						queues->tail = n;
59243b3a8eb9SGleb Smirnoff 					}
59253b3a8eb9SGleb Smirnoff 				}
59263b3a8eb9SGleb Smirnoff 				if ((pf->opts & PF_OPT_VERBOSE) && (
59273b3a8eb9SGleb Smirnoff 				    (found == 1 && interface->ifname[0] == 0) ||
59283b3a8eb9SGleb Smirnoff 				    (found > 0 && interface->ifname[0] != 0))) {
59293b3a8eb9SGleb Smirnoff 					print_queue(&pf->paltq->altq, 0,
59303b3a8eb9SGleb Smirnoff 					    &bwspec, interface->ifname[0] != 0,
59313b3a8eb9SGleb Smirnoff 					    opts);
59323b3a8eb9SGleb Smirnoff 					if (nqueues && nqueues->tail) {
59333b3a8eb9SGleb Smirnoff 						printf("{ ");
59343b3a8eb9SGleb Smirnoff 						LOOP_THROUGH(struct node_queue,
59353b3a8eb9SGleb Smirnoff 						    queue, nqueues,
59363b3a8eb9SGleb Smirnoff 							printf("%s ",
59373b3a8eb9SGleb Smirnoff 							    queue->queue);
59383b3a8eb9SGleb Smirnoff 						);
59393b3a8eb9SGleb Smirnoff 						printf("}");
59403b3a8eb9SGleb Smirnoff 					}
59413b3a8eb9SGleb Smirnoff 					printf("\n");
59423b3a8eb9SGleb Smirnoff 				}
59433b3a8eb9SGleb Smirnoff 			}
59443b3a8eb9SGleb Smirnoff 		);
59453b3a8eb9SGleb Smirnoff 	);
59463b3a8eb9SGleb Smirnoff 
59473b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_queue, nqueues);
59483b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_if, interfaces);
59493b3a8eb9SGleb Smirnoff 
59503b3a8eb9SGleb Smirnoff 	if (!found) {
59513b3a8eb9SGleb Smirnoff 		yyerror("queue %s has no parent", a->qname);
59523b3a8eb9SGleb Smirnoff 		errs++;
59533b3a8eb9SGleb Smirnoff 	}
59543b3a8eb9SGleb Smirnoff 
59553b3a8eb9SGleb Smirnoff 	if (errs)
59563b3a8eb9SGleb Smirnoff 		return (1);
59573b3a8eb9SGleb Smirnoff 	else
59583b3a8eb9SGleb Smirnoff 		return (0);
59593b3a8eb9SGleb Smirnoff }
59603b3a8eb9SGleb Smirnoff 
59618a42005dSKristof Provost static int
59628a42005dSKristof Provost pf_af_to_proto(sa_family_t af)
59638a42005dSKristof Provost {
59648a42005dSKristof Provost 	if (af == AF_INET)
59658a42005dSKristof Provost 		return (ETHERTYPE_IP);
59668a42005dSKristof Provost 	if (af == AF_INET6)
59678a42005dSKristof Provost 		return (ETHERTYPE_IPV6);
59688a42005dSKristof Provost 
59698a42005dSKristof Provost 	return (0);
59708a42005dSKristof Provost }
59718a42005dSKristof Provost 
59723b3a8eb9SGleb Smirnoff void
59732b29ceb8SKristof Provost expand_eth_rule(struct pfctl_eth_rule *r,
597487a89d6eSKristof Provost     struct node_if *interfaces, struct node_etherproto *protos,
59758a42005dSKristof Provost     struct node_mac *srcs, struct node_mac *dsts,
59768a8af942SKristof Provost     struct node_host *ipsrcs, struct node_host *ipdsts,
59778a8af942SKristof Provost     const char *bridge_to, const char *anchor_call)
59782b29ceb8SKristof Provost {
59791f61367fSKristof Provost 	char tagname[PF_TAG_NAME_SIZE];
59801f61367fSKristof Provost 	char match_tagname[PF_TAG_NAME_SIZE];
59811f61367fSKristof Provost 	char qname[PF_QNAME_SIZE];
59821f61367fSKristof Provost 
59831f61367fSKristof Provost 	if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname))
59841f61367fSKristof Provost 		errx(1, "expand_eth_rule: tagname");
59851f61367fSKristof Provost 	if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >=
59861f61367fSKristof Provost 	    sizeof(match_tagname))
59871f61367fSKristof Provost 		errx(1, "expand_eth_rule: match_tagname");
59881f61367fSKristof Provost 	if (strlcpy(qname, r->qname, sizeof(qname)) >= sizeof(qname))
59891f61367fSKristof Provost 		errx(1, "expand_eth_rule: qname");
59901f61367fSKristof Provost 
59912b29ceb8SKristof Provost 	LOOP_THROUGH(struct node_if, interface, interfaces,
59922b29ceb8SKristof Provost 	LOOP_THROUGH(struct node_etherproto, proto, protos,
599387a89d6eSKristof Provost 	LOOP_THROUGH(struct node_mac, src, srcs,
599487a89d6eSKristof Provost 	LOOP_THROUGH(struct node_mac, dst, dsts,
59958a42005dSKristof Provost 	LOOP_THROUGH(struct node_host, ipsrc, ipsrcs,
59968a42005dSKristof Provost 	LOOP_THROUGH(struct node_host, ipdst, ipdsts,
59972b29ceb8SKristof Provost 		strlcpy(r->ifname, interface->ifname,
59982b29ceb8SKristof Provost 		    sizeof(r->ifname));
59992b29ceb8SKristof Provost 		r->ifnot = interface->not;
60002b29ceb8SKristof Provost 		r->proto = proto->proto;
60018a42005dSKristof Provost 		if (!r->proto && ipsrc->af)
60028a42005dSKristof Provost 			r->proto = pf_af_to_proto(ipsrc->af);
60038a42005dSKristof Provost 		else if (!r->proto && ipdst->af)
60048a42005dSKristof Provost 			r->proto = pf_af_to_proto(ipdst->af);
600587a89d6eSKristof Provost 		bcopy(src->mac, r->src.addr, ETHER_ADDR_LEN);
6006b590f17aSKristof Provost 		bcopy(src->mask, r->src.mask, ETHER_ADDR_LEN);
600787a89d6eSKristof Provost 		r->src.neg = src->neg;
6008c32cd180SKristof Provost 		r->src.isset = src->isset;
60098a42005dSKristof Provost 		r->ipsrc.addr = ipsrc->addr;
60108a42005dSKristof Provost 		r->ipsrc.neg = ipsrc->not;
60118a42005dSKristof Provost 		r->ipdst.addr = ipdst->addr;
60128a42005dSKristof Provost 		r->ipdst.neg = ipdst->not;
601387a89d6eSKristof Provost 		bcopy(dst->mac, r->dst.addr, ETHER_ADDR_LEN);
6014b590f17aSKristof Provost 		bcopy(dst->mask, r->dst.mask, ETHER_ADDR_LEN);
601587a89d6eSKristof Provost 		r->dst.neg = dst->neg;
6016c32cd180SKristof Provost 		r->dst.isset = dst->isset;
6017c5131afeSKristof Provost 		r->nr = pf->eastack[pf->asd]->match++;
60182b29ceb8SKristof Provost 
60191f61367fSKristof Provost 		if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >=
60201f61367fSKristof Provost 		    sizeof(r->tagname))
60211f61367fSKristof Provost 			errx(1, "expand_eth_rule: r->tagname");
60221f61367fSKristof Provost 		if (strlcpy(r->match_tagname, match_tagname,
60231f61367fSKristof Provost 		    sizeof(r->match_tagname)) >= sizeof(r->match_tagname))
60241f61367fSKristof Provost 			errx(1, "expand_eth_rule: r->match_tagname");
60251f61367fSKristof Provost 		if (strlcpy(r->qname, qname, sizeof(r->qname)) >= sizeof(r->qname))
60261f61367fSKristof Provost 			errx(1, "expand_eth_rule: r->qname");
60271f61367fSKristof Provost 
60288a8af942SKristof Provost 		if (bridge_to)
60298a8af942SKristof Provost 			strlcpy(r->bridge_to, bridge_to, sizeof(r->bridge_to));
60308a8af942SKristof Provost 
6031c5131afeSKristof Provost 		pfctl_append_eth_rule(pf, r, anchor_call);
60328a42005dSKristof Provost 	))))));
60332b29ceb8SKristof Provost 
60342b29ceb8SKristof Provost 	FREE_LIST(struct node_if, interfaces);
60352b29ceb8SKristof Provost 	FREE_LIST(struct node_etherproto, protos);
603687a89d6eSKristof Provost 	FREE_LIST(struct node_mac, srcs);
603787a89d6eSKristof Provost 	FREE_LIST(struct node_mac, dsts);
60388a42005dSKristof Provost 	FREE_LIST(struct node_host, ipsrcs);
60398a42005dSKristof Provost 	FREE_LIST(struct node_host, ipdsts);
60402b29ceb8SKristof Provost }
60412b29ceb8SKristof Provost 
60422b29ceb8SKristof Provost void
6043e9eb0941SKristof Provost expand_rule(struct pfctl_rule *r,
60443b3a8eb9SGleb Smirnoff     struct node_if *interfaces, struct node_host *rpool_hosts,
60453b3a8eb9SGleb Smirnoff     struct node_proto *protos, struct node_os *src_oses,
60463b3a8eb9SGleb Smirnoff     struct node_host *src_hosts, struct node_port *src_ports,
60473b3a8eb9SGleb Smirnoff     struct node_host *dst_hosts, struct node_port *dst_ports,
60482339ead6SKristof Provost     struct node_uid *uids, struct node_gid *gids, struct node_if *rcv,
60492339ead6SKristof Provost     struct node_icmp *icmp_types, const char *anchor_call)
60503b3a8eb9SGleb Smirnoff {
60513b3a8eb9SGleb Smirnoff 	sa_family_t		 af = r->af;
60523b3a8eb9SGleb Smirnoff 	int			 added = 0, error = 0;
60533b3a8eb9SGleb Smirnoff 	char			 ifname[IF_NAMESIZE];
60546fcc8e04SKristof Provost 	char			 label[PF_RULE_MAX_LABEL_COUNT][PF_RULE_LABEL_SIZE];
60553b3a8eb9SGleb Smirnoff 	char			 tagname[PF_TAG_NAME_SIZE];
60563b3a8eb9SGleb Smirnoff 	char			 match_tagname[PF_TAG_NAME_SIZE];
60573b3a8eb9SGleb Smirnoff 	struct pf_pooladdr	*pa;
60587ce98cf2SKristof Provost 	struct node_host	*h, *osrch, *odsth;
60593b3a8eb9SGleb Smirnoff 	u_int8_t		 flags, flagset, keep_state;
60603b3a8eb9SGleb Smirnoff 
60616fcc8e04SKristof Provost 	memcpy(label, r->label, sizeof(r->label));
60626fcc8e04SKristof Provost 	assert(sizeof(r->label) == sizeof(label));
60633b3a8eb9SGleb Smirnoff 	if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname))
60643b3a8eb9SGleb Smirnoff 		errx(1, "expand_rule: strlcpy");
60653b3a8eb9SGleb Smirnoff 	if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >=
60663b3a8eb9SGleb Smirnoff 	    sizeof(match_tagname))
60673b3a8eb9SGleb Smirnoff 		errx(1, "expand_rule: strlcpy");
60683b3a8eb9SGleb Smirnoff 	flags = r->flags;
60693b3a8eb9SGleb Smirnoff 	flagset = r->flagset;
60703b3a8eb9SGleb Smirnoff 	keep_state = r->keep_state;
60713b3a8eb9SGleb Smirnoff 
60723b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_if, interface, interfaces,
60733b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_proto, proto, protos,
60743b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_icmp, icmp_type, icmp_types,
60753b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_host, src_host, src_hosts,
60763b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_host, dst_host, dst_hosts,
6077288bec2bSKristof Provost 	LOOP_THROUGH(struct node_port, src_port, src_ports,
60783b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_port, dst_port, dst_ports,
6079288bec2bSKristof Provost 	LOOP_THROUGH(struct node_os, src_os, src_oses,
60803b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_uid, uid, uids,
60813b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_gid, gid, gids,
60823b3a8eb9SGleb Smirnoff 
60833b3a8eb9SGleb Smirnoff 		r->af = af;
60843b3a8eb9SGleb Smirnoff 		/* for link-local IPv6 address, interface must match up */
60853b3a8eb9SGleb Smirnoff 		if ((r->af && src_host->af && r->af != src_host->af) ||
60863b3a8eb9SGleb Smirnoff 		    (r->af && dst_host->af && r->af != dst_host->af) ||
60873b3a8eb9SGleb Smirnoff 		    (src_host->af && dst_host->af &&
60883b3a8eb9SGleb Smirnoff 		    src_host->af != dst_host->af) ||
60893b3a8eb9SGleb Smirnoff 		    (src_host->ifindex && dst_host->ifindex &&
60903b3a8eb9SGleb Smirnoff 		    src_host->ifindex != dst_host->ifindex) ||
60913b3a8eb9SGleb Smirnoff 		    (src_host->ifindex && *interface->ifname &&
60923b3a8eb9SGleb Smirnoff 		    src_host->ifindex != if_nametoindex(interface->ifname)) ||
60933b3a8eb9SGleb Smirnoff 		    (dst_host->ifindex && *interface->ifname &&
60943b3a8eb9SGleb Smirnoff 		    dst_host->ifindex != if_nametoindex(interface->ifname)))
60953b3a8eb9SGleb Smirnoff 			continue;
60963b3a8eb9SGleb Smirnoff 		if (!r->af && src_host->af)
60973b3a8eb9SGleb Smirnoff 			r->af = src_host->af;
60983b3a8eb9SGleb Smirnoff 		else if (!r->af && dst_host->af)
60993b3a8eb9SGleb Smirnoff 			r->af = dst_host->af;
61003b3a8eb9SGleb Smirnoff 
61013b3a8eb9SGleb Smirnoff 		if (*interface->ifname)
61023b3a8eb9SGleb Smirnoff 			strlcpy(r->ifname, interface->ifname,
61033b3a8eb9SGleb Smirnoff 			    sizeof(r->ifname));
61043b3a8eb9SGleb Smirnoff 		else if (if_indextoname(src_host->ifindex, ifname))
61053b3a8eb9SGleb Smirnoff 			strlcpy(r->ifname, ifname, sizeof(r->ifname));
61063b3a8eb9SGleb Smirnoff 		else if (if_indextoname(dst_host->ifindex, ifname))
61073b3a8eb9SGleb Smirnoff 			strlcpy(r->ifname, ifname, sizeof(r->ifname));
61083b3a8eb9SGleb Smirnoff 		else
61093b3a8eb9SGleb Smirnoff 			memset(r->ifname, '\0', sizeof(r->ifname));
61103b3a8eb9SGleb Smirnoff 
61116fcc8e04SKristof Provost 		memcpy(r->label, label, sizeof(r->label));
61123b3a8eb9SGleb Smirnoff 		if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >=
61133b3a8eb9SGleb Smirnoff 		    sizeof(r->tagname))
61143b3a8eb9SGleb Smirnoff 			errx(1, "expand_rule: strlcpy");
61153b3a8eb9SGleb Smirnoff 		if (strlcpy(r->match_tagname, match_tagname,
61163b3a8eb9SGleb Smirnoff 		    sizeof(r->match_tagname)) >= sizeof(r->match_tagname))
61173b3a8eb9SGleb Smirnoff 			errx(1, "expand_rule: strlcpy");
61183b3a8eb9SGleb Smirnoff 
61197ce98cf2SKristof Provost 		osrch = odsth = NULL;
61207ce98cf2SKristof Provost 		if (src_host->addr.type == PF_ADDR_DYNIFTL) {
61217ce98cf2SKristof Provost 			osrch = src_host;
61227ce98cf2SKristof Provost 			if ((src_host = gen_dynnode(src_host, r->af)) == NULL)
61237ce98cf2SKristof Provost 				err(1, "expand_rule: calloc");
61247ce98cf2SKristof Provost 		}
61257ce98cf2SKristof Provost 		if (dst_host->addr.type == PF_ADDR_DYNIFTL) {
61267ce98cf2SKristof Provost 			odsth = dst_host;
61277ce98cf2SKristof Provost 			if ((dst_host = gen_dynnode(dst_host, r->af)) == NULL)
61287ce98cf2SKristof Provost 				err(1, "expand_rule: calloc");
61297ce98cf2SKristof Provost 		}
61307ce98cf2SKristof Provost 
61313b3a8eb9SGleb Smirnoff 		error += check_netmask(src_host, r->af);
61323b3a8eb9SGleb Smirnoff 		error += check_netmask(dst_host, r->af);
61333b3a8eb9SGleb Smirnoff 
61343b3a8eb9SGleb Smirnoff 		r->ifnot = interface->not;
61353b3a8eb9SGleb Smirnoff 		r->proto = proto->proto;
61363b3a8eb9SGleb Smirnoff 		r->src.addr = src_host->addr;
61373b3a8eb9SGleb Smirnoff 		r->src.neg = src_host->not;
61383b3a8eb9SGleb Smirnoff 		r->src.port[0] = src_port->port[0];
61393b3a8eb9SGleb Smirnoff 		r->src.port[1] = src_port->port[1];
61403b3a8eb9SGleb Smirnoff 		r->src.port_op = src_port->op;
61413b3a8eb9SGleb Smirnoff 		r->dst.addr = dst_host->addr;
61423b3a8eb9SGleb Smirnoff 		r->dst.neg = dst_host->not;
61433b3a8eb9SGleb Smirnoff 		r->dst.port[0] = dst_port->port[0];
61443b3a8eb9SGleb Smirnoff 		r->dst.port[1] = dst_port->port[1];
61453b3a8eb9SGleb Smirnoff 		r->dst.port_op = dst_port->op;
61463b3a8eb9SGleb Smirnoff 		r->uid.op = uid->op;
61473b3a8eb9SGleb Smirnoff 		r->uid.uid[0] = uid->uid[0];
61483b3a8eb9SGleb Smirnoff 		r->uid.uid[1] = uid->uid[1];
61493b3a8eb9SGleb Smirnoff 		r->gid.op = gid->op;
61503b3a8eb9SGleb Smirnoff 		r->gid.gid[0] = gid->gid[0];
61513b3a8eb9SGleb Smirnoff 		r->gid.gid[1] = gid->gid[1];
61522339ead6SKristof Provost 		if (rcv) {
61532339ead6SKristof Provost 			strlcpy(r->rcv_ifname, rcv->ifname,
61542339ead6SKristof Provost 			    sizeof(r->rcv_ifname));
61552339ead6SKristof Provost 		}
61563b3a8eb9SGleb Smirnoff 		r->type = icmp_type->type;
61573b3a8eb9SGleb Smirnoff 		r->code = icmp_type->code;
61583b3a8eb9SGleb Smirnoff 
61593b3a8eb9SGleb Smirnoff 		if ((keep_state == PF_STATE_MODULATE ||
61603b3a8eb9SGleb Smirnoff 		    keep_state == PF_STATE_SYNPROXY) &&
61613b3a8eb9SGleb Smirnoff 		    r->proto && r->proto != IPPROTO_TCP)
61623b3a8eb9SGleb Smirnoff 			r->keep_state = PF_STATE_NORMAL;
61633b3a8eb9SGleb Smirnoff 		else
61643b3a8eb9SGleb Smirnoff 			r->keep_state = keep_state;
61653b3a8eb9SGleb Smirnoff 
61663b3a8eb9SGleb Smirnoff 		if (r->proto && r->proto != IPPROTO_TCP) {
61673b3a8eb9SGleb Smirnoff 			r->flags = 0;
61683b3a8eb9SGleb Smirnoff 			r->flagset = 0;
61693b3a8eb9SGleb Smirnoff 		} else {
61703b3a8eb9SGleb Smirnoff 			r->flags = flags;
61713b3a8eb9SGleb Smirnoff 			r->flagset = flagset;
61723b3a8eb9SGleb Smirnoff 		}
61733b3a8eb9SGleb Smirnoff 		if (icmp_type->proto && r->proto != icmp_type->proto) {
61743b3a8eb9SGleb Smirnoff 			yyerror("icmp-type mismatch");
61753b3a8eb9SGleb Smirnoff 			error++;
61763b3a8eb9SGleb Smirnoff 		}
61773b3a8eb9SGleb Smirnoff 
61783b3a8eb9SGleb Smirnoff 		if (src_os && src_os->os) {
61793b3a8eb9SGleb Smirnoff 			r->os_fingerprint = pfctl_get_fingerprint(src_os->os);
61803b3a8eb9SGleb Smirnoff 			if ((pf->opts & PF_OPT_VERBOSE2) &&
61813b3a8eb9SGleb Smirnoff 			    r->os_fingerprint == PF_OSFP_NOMATCH)
61823b3a8eb9SGleb Smirnoff 				fprintf(stderr,
61833b3a8eb9SGleb Smirnoff 				    "warning: unknown '%s' OS fingerprint\n",
61843b3a8eb9SGleb Smirnoff 				    src_os->os);
61853b3a8eb9SGleb Smirnoff 		} else {
61863b3a8eb9SGleb Smirnoff 			r->os_fingerprint = PF_OSFP_ANY;
61873b3a8eb9SGleb Smirnoff 		}
61883b3a8eb9SGleb Smirnoff 
61893b3a8eb9SGleb Smirnoff 		TAILQ_INIT(&r->rpool.list);
61903b3a8eb9SGleb Smirnoff 		for (h = rpool_hosts; h != NULL; h = h->next) {
61913b3a8eb9SGleb Smirnoff 			pa = calloc(1, sizeof(struct pf_pooladdr));
61923b3a8eb9SGleb Smirnoff 			if (pa == NULL)
61933b3a8eb9SGleb Smirnoff 				err(1, "expand_rule: calloc");
61943b3a8eb9SGleb Smirnoff 			pa->addr = h->addr;
61953b3a8eb9SGleb Smirnoff 			if (h->ifname != NULL) {
61963b3a8eb9SGleb Smirnoff 				if (strlcpy(pa->ifname, h->ifname,
61973b3a8eb9SGleb Smirnoff 				    sizeof(pa->ifname)) >=
61983b3a8eb9SGleb Smirnoff 				    sizeof(pa->ifname))
61993b3a8eb9SGleb Smirnoff 					errx(1, "expand_rule: strlcpy");
62003b3a8eb9SGleb Smirnoff 			} else
62013b3a8eb9SGleb Smirnoff 				pa->ifname[0] = 0;
62023b3a8eb9SGleb Smirnoff 			TAILQ_INSERT_TAIL(&r->rpool.list, pa, entries);
62033b3a8eb9SGleb Smirnoff 		}
62043b3a8eb9SGleb Smirnoff 
62053b3a8eb9SGleb Smirnoff 		if (rule_consistent(r, anchor_call[0]) < 0 || error)
62063b3a8eb9SGleb Smirnoff 			yyerror("skipping rule due to errors");
62073b3a8eb9SGleb Smirnoff 		else {
62083b3a8eb9SGleb Smirnoff 			r->nr = pf->astack[pf->asd]->match++;
62090d71f9f3SKristof Provost 			pfctl_append_rule(pf, r, anchor_call);
62103b3a8eb9SGleb Smirnoff 			added++;
62113b3a8eb9SGleb Smirnoff 		}
62123b3a8eb9SGleb Smirnoff 
62137ce98cf2SKristof Provost 		if (osrch && src_host->addr.type == PF_ADDR_DYNIFTL) {
62147ce98cf2SKristof Provost 			free(src_host);
62157ce98cf2SKristof Provost 			src_host = osrch;
62167ce98cf2SKristof Provost 		}
62177ce98cf2SKristof Provost 		if (odsth && dst_host->addr.type == PF_ADDR_DYNIFTL) {
62187ce98cf2SKristof Provost 			free(dst_host);
62197ce98cf2SKristof Provost 			dst_host = odsth;
62207ce98cf2SKristof Provost 		}
62217ce98cf2SKristof Provost 
62223b3a8eb9SGleb Smirnoff 	))))))))));
62233b3a8eb9SGleb Smirnoff 
62243b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_if, interfaces);
62253b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_proto, protos);
62263b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_host, src_hosts);
62273b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_port, src_ports);
62283b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_os, src_oses);
62293b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_host, dst_hosts);
62303b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_port, dst_ports);
62313b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_uid, uids);
62323b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_gid, gids);
62333b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_icmp, icmp_types);
62343b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_host, rpool_hosts);
62353b3a8eb9SGleb Smirnoff 
62363b3a8eb9SGleb Smirnoff 	if (!added)
62373b3a8eb9SGleb Smirnoff 		yyerror("rule expands to no valid combination");
62383b3a8eb9SGleb Smirnoff }
62393b3a8eb9SGleb Smirnoff 
62403b3a8eb9SGleb Smirnoff int
62413b3a8eb9SGleb Smirnoff expand_skip_interface(struct node_if *interfaces)
62423b3a8eb9SGleb Smirnoff {
62433b3a8eb9SGleb Smirnoff 	int	errs = 0;
62443b3a8eb9SGleb Smirnoff 
62453b3a8eb9SGleb Smirnoff 	if (!interfaces || (!interfaces->next && !interfaces->not &&
62463b3a8eb9SGleb Smirnoff 	    !strcmp(interfaces->ifname, "none"))) {
62473b3a8eb9SGleb Smirnoff 		if (pf->opts & PF_OPT_VERBOSE)
62483b3a8eb9SGleb Smirnoff 			printf("set skip on none\n");
62493b3a8eb9SGleb Smirnoff 		errs = pfctl_set_interface_flags(pf, "", PFI_IFLAG_SKIP, 0);
62503b3a8eb9SGleb Smirnoff 		return (errs);
62513b3a8eb9SGleb Smirnoff 	}
62523b3a8eb9SGleb Smirnoff 
62533b3a8eb9SGleb Smirnoff 	if (pf->opts & PF_OPT_VERBOSE)
62543b3a8eb9SGleb Smirnoff 		printf("set skip on {");
62553b3a8eb9SGleb Smirnoff 	LOOP_THROUGH(struct node_if, interface, interfaces,
62563b3a8eb9SGleb Smirnoff 		if (pf->opts & PF_OPT_VERBOSE)
62573b3a8eb9SGleb Smirnoff 			printf(" %s", interface->ifname);
62583b3a8eb9SGleb Smirnoff 		if (interface->not) {
62593b3a8eb9SGleb Smirnoff 			yyerror("skip on ! <interface> is not supported");
62603b3a8eb9SGleb Smirnoff 			errs++;
62613b3a8eb9SGleb Smirnoff 		} else
62623b3a8eb9SGleb Smirnoff 			errs += pfctl_set_interface_flags(pf,
62633b3a8eb9SGleb Smirnoff 			    interface->ifname, PFI_IFLAG_SKIP, 1);
62643b3a8eb9SGleb Smirnoff 	);
62653b3a8eb9SGleb Smirnoff 	if (pf->opts & PF_OPT_VERBOSE)
62663b3a8eb9SGleb Smirnoff 		printf(" }\n");
62673b3a8eb9SGleb Smirnoff 
62683b3a8eb9SGleb Smirnoff 	FREE_LIST(struct node_if, interfaces);
62693b3a8eb9SGleb Smirnoff 
62703b3a8eb9SGleb Smirnoff 	if (errs)
62713b3a8eb9SGleb Smirnoff 		return (1);
62723b3a8eb9SGleb Smirnoff 	else
62733b3a8eb9SGleb Smirnoff 		return (0);
62743b3a8eb9SGleb Smirnoff }
62753b3a8eb9SGleb Smirnoff 
6276637d81c5SKristof Provost void
6277637d81c5SKristof Provost freehostlist(struct node_host *h)
6278637d81c5SKristof Provost {
6279637d81c5SKristof Provost 	FREE_LIST(struct node_host, h);
6280637d81c5SKristof Provost }
6281637d81c5SKristof Provost 
62823b3a8eb9SGleb Smirnoff #undef FREE_LIST
62833b3a8eb9SGleb Smirnoff #undef LOOP_THROUGH
62843b3a8eb9SGleb Smirnoff 
62853b3a8eb9SGleb Smirnoff int
62863b3a8eb9SGleb Smirnoff check_rulestate(int desired_state)
62873b3a8eb9SGleb Smirnoff {
62883b3a8eb9SGleb Smirnoff 	if (require_order && (rulestate > desired_state)) {
62892b29ceb8SKristof Provost 		yyerror("Rules must be in order: options, ethernet, "
62902b29ceb8SKristof Provost 		    "normalization, queueing, translation, filtering");
62913b3a8eb9SGleb Smirnoff 		return (1);
62923b3a8eb9SGleb Smirnoff 	}
62933b3a8eb9SGleb Smirnoff 	rulestate = desired_state;
62943b3a8eb9SGleb Smirnoff 	return (0);
62953b3a8eb9SGleb Smirnoff }
62963b3a8eb9SGleb Smirnoff 
62973b3a8eb9SGleb Smirnoff int
62983b3a8eb9SGleb Smirnoff kw_cmp(const void *k, const void *e)
62993b3a8eb9SGleb Smirnoff {
63003b3a8eb9SGleb Smirnoff 	return (strcmp(k, ((const struct keywords *)e)->k_name));
63013b3a8eb9SGleb Smirnoff }
63023b3a8eb9SGleb Smirnoff 
63033b3a8eb9SGleb Smirnoff int
63043b3a8eb9SGleb Smirnoff lookup(char *s)
63053b3a8eb9SGleb Smirnoff {
63063b3a8eb9SGleb Smirnoff 	/* this has to be sorted always */
63073b3a8eb9SGleb Smirnoff 	static const struct keywords keywords[] = {
63083b3a8eb9SGleb Smirnoff 		{ "all",		ALL},
63093b3a8eb9SGleb Smirnoff 		{ "allow-opts",		ALLOWOPTS},
63103b3a8eb9SGleb Smirnoff 		{ "altq",		ALTQ},
63113b3a8eb9SGleb Smirnoff 		{ "anchor",		ANCHOR},
63123b3a8eb9SGleb Smirnoff 		{ "antispoof",		ANTISPOOF},
63133b3a8eb9SGleb Smirnoff 		{ "any",		ANY},
63143b3a8eb9SGleb Smirnoff 		{ "bandwidth",		BANDWIDTH},
63153b3a8eb9SGleb Smirnoff 		{ "binat",		BINAT},
63163b3a8eb9SGleb Smirnoff 		{ "binat-anchor",	BINATANCHOR},
63173b3a8eb9SGleb Smirnoff 		{ "bitmask",		BITMASK},
63183b3a8eb9SGleb Smirnoff 		{ "block",		BLOCK},
63193b3a8eb9SGleb Smirnoff 		{ "block-policy",	BLOCKPOLICY},
63208a8af942SKristof Provost 		{ "bridge-to",		BRIDGE_TO},
6321a5b789f6SErmal Luçi 		{ "buckets",		BUCKETS},
63223b3a8eb9SGleb Smirnoff 		{ "cbq",		CBQ},
63233b3a8eb9SGleb Smirnoff 		{ "code",		CODE},
63240a70aaf8SLuiz Otavio O Souza 		{ "codelq",		CODEL},
63253b3a8eb9SGleb Smirnoff 		{ "debug",		DEBUG},
63263b3a8eb9SGleb Smirnoff 		{ "divert-reply",	DIVERTREPLY},
63273b3a8eb9SGleb Smirnoff 		{ "divert-to",		DIVERTTO},
632863b3c1c7SKristof Provost 		{ "dnpipe",		DNPIPE},
632963b3c1c7SKristof Provost 		{ "dnqueue",		DNQUEUE},
63303b3a8eb9SGleb Smirnoff 		{ "drop",		DROP},
63313b3a8eb9SGleb Smirnoff 		{ "dup-to",		DUPTO},
6332390dc369STom Jones 		{ "endpoint-independent", ENDPI},
63332b29ceb8SKristof Provost 		{ "ether",		ETHER},
6334150182e3SKristof Provost 		{ "fail-policy",	FAILPOLICY},
6335a5b789f6SErmal Luçi 		{ "fairq",		FAIRQ},
63363b3a8eb9SGleb Smirnoff 		{ "fastroute",		FASTROUTE},
63373b3a8eb9SGleb Smirnoff 		{ "file",		FILENAME},
63383b3a8eb9SGleb Smirnoff 		{ "fingerprints",	FINGERPRINTS},
63393b3a8eb9SGleb Smirnoff 		{ "flags",		FLAGS},
63403b3a8eb9SGleb Smirnoff 		{ "floating",		FLOATING},
63413b3a8eb9SGleb Smirnoff 		{ "flush",		FLUSH},
63423b3a8eb9SGleb Smirnoff 		{ "for",		FOR},
63433b3a8eb9SGleb Smirnoff 		{ "fragment",		FRAGMENT},
63443b3a8eb9SGleb Smirnoff 		{ "from",		FROM},
63453b3a8eb9SGleb Smirnoff 		{ "global",		GLOBAL},
63463b3a8eb9SGleb Smirnoff 		{ "group",		GROUP},
63473b3a8eb9SGleb Smirnoff 		{ "hfsc",		HFSC},
6348a5b789f6SErmal Luçi 		{ "hogs",		HOGS},
63493b3a8eb9SGleb Smirnoff 		{ "hostid",		HOSTID},
63503b3a8eb9SGleb Smirnoff 		{ "icmp-type",		ICMPTYPE},
63513b3a8eb9SGleb Smirnoff 		{ "icmp6-type",		ICMP6TYPE},
63523b3a8eb9SGleb Smirnoff 		{ "if-bound",		IFBOUND},
63533b3a8eb9SGleb Smirnoff 		{ "in",			IN},
63543b3a8eb9SGleb Smirnoff 		{ "include",		INCLUDE},
63553b3a8eb9SGleb Smirnoff 		{ "inet",		INET},
63563b3a8eb9SGleb Smirnoff 		{ "inet6",		INET6},
63570a70aaf8SLuiz Otavio O Souza 		{ "interval",		INTERVAL},
63583b3a8eb9SGleb Smirnoff 		{ "keep",		KEEP},
635942ec75f8SKristof Provost 		{ "keepcounters",	KEEPCOUNTERS},
63608a42005dSKristof Provost 		{ "l3",			L3},
63613b3a8eb9SGleb Smirnoff 		{ "label",		LABEL},
63623b3a8eb9SGleb Smirnoff 		{ "limit",		LIMIT},
63633b3a8eb9SGleb Smirnoff 		{ "linkshare",		LINKSHARE},
63643b3a8eb9SGleb Smirnoff 		{ "load",		LOAD},
63653b3a8eb9SGleb Smirnoff 		{ "log",		LOG},
63663b3a8eb9SGleb Smirnoff 		{ "loginterface",	LOGINTERFACE},
63672aa21096SKurosawa Takahiro 		{ "map-e-portset",	MAPEPORTSET},
6368ef950daaSKristof Provost 		{ "match",		MATCH},
6369f3ab00c2SKristof Provost 		{ "matches",	MATCHES},
63703b3a8eb9SGleb Smirnoff 		{ "max",		MAXIMUM},
63713b3a8eb9SGleb Smirnoff 		{ "max-mss",		MAXMSS},
63723b3a8eb9SGleb Smirnoff 		{ "max-src-conn",	MAXSRCCONN},
63733b3a8eb9SGleb Smirnoff 		{ "max-src-conn-rate",	MAXSRCCONNRATE},
63743b3a8eb9SGleb Smirnoff 		{ "max-src-nodes",	MAXSRCNODES},
63753b3a8eb9SGleb Smirnoff 		{ "max-src-states",	MAXSRCSTATES},
63763b3a8eb9SGleb Smirnoff 		{ "min-ttl",		MINTTL},
63773b3a8eb9SGleb Smirnoff 		{ "modulate",		MODULATE},
63783b3a8eb9SGleb Smirnoff 		{ "nat",		NAT},
63793b3a8eb9SGleb Smirnoff 		{ "nat-anchor",		NATANCHOR},
63803b3a8eb9SGleb Smirnoff 		{ "no",			NO},
63813b3a8eb9SGleb Smirnoff 		{ "no-df",		NODF},
63823b3a8eb9SGleb Smirnoff 		{ "no-route",		NOROUTE},
63833b3a8eb9SGleb Smirnoff 		{ "no-sync",		NOSYNC},
63843b3a8eb9SGleb Smirnoff 		{ "on",			ON},
63853b3a8eb9SGleb Smirnoff 		{ "optimization",	OPTIMIZATION},
63863b3a8eb9SGleb Smirnoff 		{ "os",			OS},
63873b3a8eb9SGleb Smirnoff 		{ "out",		OUT},
63883b3a8eb9SGleb Smirnoff 		{ "overload",		OVERLOAD},
63893b3a8eb9SGleb Smirnoff 		{ "pass",		PASS},
6390baf9b6d0SKristof Provost 		{ "pflow",		PFLOW},
63913b3a8eb9SGleb Smirnoff 		{ "port",		PORT},
63923e248e0fSKristof Provost 		{ "prio",		PRIO},
63933b3a8eb9SGleb Smirnoff 		{ "priority",		PRIORITY},
63943b3a8eb9SGleb Smirnoff 		{ "priq",		PRIQ},
63953b3a8eb9SGleb Smirnoff 		{ "probability",	PROBABILITY},
63963b3a8eb9SGleb Smirnoff 		{ "proto",		PROTO},
63973b3a8eb9SGleb Smirnoff 		{ "qlimit",		QLIMIT},
63983b3a8eb9SGleb Smirnoff 		{ "queue",		QUEUE},
63993b3a8eb9SGleb Smirnoff 		{ "quick",		QUICK},
64003b3a8eb9SGleb Smirnoff 		{ "random",		RANDOM},
64013b3a8eb9SGleb Smirnoff 		{ "random-id",		RANDOMID},
64023b3a8eb9SGleb Smirnoff 		{ "rdr",		RDR},
64033b3a8eb9SGleb Smirnoff 		{ "rdr-anchor",		RDRANCHOR},
64043b3a8eb9SGleb Smirnoff 		{ "realtime",		REALTIME},
64053b3a8eb9SGleb Smirnoff 		{ "reassemble",		REASSEMBLE},
64062339ead6SKristof Provost 		{ "received-on",	RECEIVEDON},
64073b3a8eb9SGleb Smirnoff 		{ "reply-to",		REPLYTO},
64083b3a8eb9SGleb Smirnoff 		{ "require-order",	REQUIREORDER},
64093b3a8eb9SGleb Smirnoff 		{ "return",		RETURN},
64103b3a8eb9SGleb Smirnoff 		{ "return-icmp",	RETURNICMP},
64113b3a8eb9SGleb Smirnoff 		{ "return-icmp6",	RETURNICMP6},
64123b3a8eb9SGleb Smirnoff 		{ "return-rst",		RETURNRST},
641376c5eeccSKristof Provost 		{ "ridentifier",	RIDENTIFIER},
64143b3a8eb9SGleb Smirnoff 		{ "round-robin",	ROUNDROBIN},
64153b3a8eb9SGleb Smirnoff 		{ "route",		ROUTE},
64163b3a8eb9SGleb Smirnoff 		{ "route-to",		ROUTETO},
64173b3a8eb9SGleb Smirnoff 		{ "rtable",		RTABLE},
64183b3a8eb9SGleb Smirnoff 		{ "rule",		RULE},
64193b3a8eb9SGleb Smirnoff 		{ "ruleset-optimization",	RULESET_OPTIMIZATION},
64203b3a8eb9SGleb Smirnoff 		{ "scrub",		SCRUB},
64213b3a8eb9SGleb Smirnoff 		{ "set",		SET},
64223b3a8eb9SGleb Smirnoff 		{ "set-tos",		SETTOS},
64233b3a8eb9SGleb Smirnoff 		{ "skip",		SKIP},
64243b3a8eb9SGleb Smirnoff 		{ "sloppy",		SLOPPY},
64253b3a8eb9SGleb Smirnoff 		{ "source-hash",	SOURCEHASH},
64263b3a8eb9SGleb Smirnoff 		{ "source-track",	SOURCETRACK},
64273b3a8eb9SGleb Smirnoff 		{ "state",		STATE},
64283b3a8eb9SGleb Smirnoff 		{ "state-defaults",	STATEDEFAULTS},
64293b3a8eb9SGleb Smirnoff 		{ "state-policy",	STATEPOLICY},
64303b3a8eb9SGleb Smirnoff 		{ "static-port",	STATICPORT},
64313b3a8eb9SGleb Smirnoff 		{ "sticky-address",	STICKYADDRESS},
6432c69121c4SKristof Provost 		{ "syncookies",         SYNCOOKIES},
64333b3a8eb9SGleb Smirnoff 		{ "synproxy",		SYNPROXY},
64343b3a8eb9SGleb Smirnoff 		{ "table",		TABLE},
64353b3a8eb9SGleb Smirnoff 		{ "tag",		TAG},
64363b3a8eb9SGleb Smirnoff 		{ "tagged",		TAGGED},
64370a70aaf8SLuiz Otavio O Souza 		{ "target",		TARGET},
64383b3a8eb9SGleb Smirnoff 		{ "tbrsize",		TBRSIZE},
64393b3a8eb9SGleb Smirnoff 		{ "timeout",		TIMEOUT},
64403b3a8eb9SGleb Smirnoff 		{ "to",			TO},
64413b3a8eb9SGleb Smirnoff 		{ "tos",		TOS},
64423b3a8eb9SGleb Smirnoff 		{ "ttl",		TTL},
64433b3a8eb9SGleb Smirnoff 		{ "upperlimit",		UPPERLIMIT},
64443b3a8eb9SGleb Smirnoff 		{ "urpf-failed",	URPFFAILED},
64453b3a8eb9SGleb Smirnoff 		{ "user",		USER},
64463b3a8eb9SGleb Smirnoff 	};
64473b3a8eb9SGleb Smirnoff 	const struct keywords	*p;
64483b3a8eb9SGleb Smirnoff 
64493b3a8eb9SGleb Smirnoff 	p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
64503b3a8eb9SGleb Smirnoff 	    sizeof(keywords[0]), kw_cmp);
64513b3a8eb9SGleb Smirnoff 
64523b3a8eb9SGleb Smirnoff 	if (p) {
64533b3a8eb9SGleb Smirnoff 		if (debug > 1)
64543b3a8eb9SGleb Smirnoff 			fprintf(stderr, "%s: %d\n", s, p->k_val);
64553b3a8eb9SGleb Smirnoff 		return (p->k_val);
64563b3a8eb9SGleb Smirnoff 	} else {
64573b3a8eb9SGleb Smirnoff 		if (debug > 1)
64583b3a8eb9SGleb Smirnoff 			fprintf(stderr, "string: %s\n", s);
64593b3a8eb9SGleb Smirnoff 		return (STRING);
64603b3a8eb9SGleb Smirnoff 	}
64613b3a8eb9SGleb Smirnoff }
64623b3a8eb9SGleb Smirnoff 
64633b3a8eb9SGleb Smirnoff #define MAXPUSHBACK	128
64643b3a8eb9SGleb Smirnoff 
646513cfafabSKristof Provost static char	*parsebuf;
646613cfafabSKristof Provost static int	 parseindex;
646713cfafabSKristof Provost static char	 pushback_buffer[MAXPUSHBACK];
646813cfafabSKristof Provost static int	 pushback_index = 0;
64693b3a8eb9SGleb Smirnoff 
64703b3a8eb9SGleb Smirnoff int
64713b3a8eb9SGleb Smirnoff lgetc(int quotec)
64723b3a8eb9SGleb Smirnoff {
64733b3a8eb9SGleb Smirnoff 	int		c, next;
64743b3a8eb9SGleb Smirnoff 
64753b3a8eb9SGleb Smirnoff 	if (parsebuf) {
64763b3a8eb9SGleb Smirnoff 		/* Read character from the parsebuffer instead of input. */
64773b3a8eb9SGleb Smirnoff 		if (parseindex >= 0) {
64783b3a8eb9SGleb Smirnoff 			c = parsebuf[parseindex++];
64793b3a8eb9SGleb Smirnoff 			if (c != '\0')
64803b3a8eb9SGleb Smirnoff 				return (c);
64813b3a8eb9SGleb Smirnoff 			parsebuf = NULL;
64823b3a8eb9SGleb Smirnoff 		} else
64833b3a8eb9SGleb Smirnoff 			parseindex++;
64843b3a8eb9SGleb Smirnoff 	}
64853b3a8eb9SGleb Smirnoff 
64863b3a8eb9SGleb Smirnoff 	if (pushback_index)
64873b3a8eb9SGleb Smirnoff 		return (pushback_buffer[--pushback_index]);
64883b3a8eb9SGleb Smirnoff 
64893b3a8eb9SGleb Smirnoff 	if (quotec) {
64903b3a8eb9SGleb Smirnoff 		if ((c = getc(file->stream)) == EOF) {
64913b3a8eb9SGleb Smirnoff 			yyerror("reached end of file while parsing quoted string");
64923b3a8eb9SGleb Smirnoff 			if (popfile() == EOF)
64933b3a8eb9SGleb Smirnoff 				return (EOF);
64943b3a8eb9SGleb Smirnoff 			return (quotec);
64953b3a8eb9SGleb Smirnoff 		}
64963b3a8eb9SGleb Smirnoff 		return (c);
64973b3a8eb9SGleb Smirnoff 	}
64983b3a8eb9SGleb Smirnoff 
64993b3a8eb9SGleb Smirnoff 	while ((c = getc(file->stream)) == '\\') {
65003b3a8eb9SGleb Smirnoff 		next = getc(file->stream);
65013b3a8eb9SGleb Smirnoff 		if (next != '\n') {
65023b3a8eb9SGleb Smirnoff 			c = next;
65033b3a8eb9SGleb Smirnoff 			break;
65043b3a8eb9SGleb Smirnoff 		}
65053b3a8eb9SGleb Smirnoff 		yylval.lineno = file->lineno;
65063b3a8eb9SGleb Smirnoff 		file->lineno++;
65073b3a8eb9SGleb Smirnoff 	}
65083b3a8eb9SGleb Smirnoff 
65093b3a8eb9SGleb Smirnoff 	while (c == EOF) {
65103b3a8eb9SGleb Smirnoff 		if (popfile() == EOF)
65113b3a8eb9SGleb Smirnoff 			return (EOF);
65123b3a8eb9SGleb Smirnoff 		c = getc(file->stream);
65133b3a8eb9SGleb Smirnoff 	}
65143b3a8eb9SGleb Smirnoff 	return (c);
65153b3a8eb9SGleb Smirnoff }
65163b3a8eb9SGleb Smirnoff 
65173b3a8eb9SGleb Smirnoff int
65183b3a8eb9SGleb Smirnoff lungetc(int c)
65193b3a8eb9SGleb Smirnoff {
65203b3a8eb9SGleb Smirnoff 	if (c == EOF)
65213b3a8eb9SGleb Smirnoff 		return (EOF);
65223b3a8eb9SGleb Smirnoff 	if (parsebuf) {
65233b3a8eb9SGleb Smirnoff 		parseindex--;
65243b3a8eb9SGleb Smirnoff 		if (parseindex >= 0)
65253b3a8eb9SGleb Smirnoff 			return (c);
65263b3a8eb9SGleb Smirnoff 	}
65273b3a8eb9SGleb Smirnoff 	if (pushback_index < MAXPUSHBACK-1)
65283b3a8eb9SGleb Smirnoff 		return (pushback_buffer[pushback_index++] = c);
65293b3a8eb9SGleb Smirnoff 	else
65303b3a8eb9SGleb Smirnoff 		return (EOF);
65313b3a8eb9SGleb Smirnoff }
65323b3a8eb9SGleb Smirnoff 
65333b3a8eb9SGleb Smirnoff int
65343b3a8eb9SGleb Smirnoff findeol(void)
65353b3a8eb9SGleb Smirnoff {
65363b3a8eb9SGleb Smirnoff 	int	c;
65373b3a8eb9SGleb Smirnoff 
65383b3a8eb9SGleb Smirnoff 	parsebuf = NULL;
65393b3a8eb9SGleb Smirnoff 
65403b3a8eb9SGleb Smirnoff 	/* skip to either EOF or the first real EOL */
65413b3a8eb9SGleb Smirnoff 	while (1) {
65423b3a8eb9SGleb Smirnoff 		if (pushback_index)
65433b3a8eb9SGleb Smirnoff 			c = pushback_buffer[--pushback_index];
65443b3a8eb9SGleb Smirnoff 		else
65453b3a8eb9SGleb Smirnoff 			c = lgetc(0);
65463b3a8eb9SGleb Smirnoff 		if (c == '\n') {
65473b3a8eb9SGleb Smirnoff 			file->lineno++;
65483b3a8eb9SGleb Smirnoff 			break;
65493b3a8eb9SGleb Smirnoff 		}
65503b3a8eb9SGleb Smirnoff 		if (c == EOF)
65513b3a8eb9SGleb Smirnoff 			break;
65523b3a8eb9SGleb Smirnoff 	}
65533b3a8eb9SGleb Smirnoff 	return (ERROR);
65543b3a8eb9SGleb Smirnoff }
65553b3a8eb9SGleb Smirnoff 
65563b3a8eb9SGleb Smirnoff int
65573b3a8eb9SGleb Smirnoff yylex(void)
65583b3a8eb9SGleb Smirnoff {
65593b3a8eb9SGleb Smirnoff 	char	 buf[8096];
65603b3a8eb9SGleb Smirnoff 	char	*p, *val;
65613b3a8eb9SGleb Smirnoff 	int	 quotec, next, c;
65623b3a8eb9SGleb Smirnoff 	int	 token;
65633b3a8eb9SGleb Smirnoff 
65643b3a8eb9SGleb Smirnoff top:
65653b3a8eb9SGleb Smirnoff 	p = buf;
65663b3a8eb9SGleb Smirnoff 	while ((c = lgetc(0)) == ' ' || c == '\t')
65673b3a8eb9SGleb Smirnoff 		; /* nothing */
65683b3a8eb9SGleb Smirnoff 
65693b3a8eb9SGleb Smirnoff 	yylval.lineno = file->lineno;
65703b3a8eb9SGleb Smirnoff 	if (c == '#')
65713b3a8eb9SGleb Smirnoff 		while ((c = lgetc(0)) != '\n' && c != EOF)
65723b3a8eb9SGleb Smirnoff 			; /* nothing */
65733b3a8eb9SGleb Smirnoff 	if (c == '$' && parsebuf == NULL) {
65743b3a8eb9SGleb Smirnoff 		while (1) {
65753b3a8eb9SGleb Smirnoff 			if ((c = lgetc(0)) == EOF)
65763b3a8eb9SGleb Smirnoff 				return (0);
65773b3a8eb9SGleb Smirnoff 
65783b3a8eb9SGleb Smirnoff 			if (p + 1 >= buf + sizeof(buf) - 1) {
65793b3a8eb9SGleb Smirnoff 				yyerror("string too long");
65803b3a8eb9SGleb Smirnoff 				return (findeol());
65813b3a8eb9SGleb Smirnoff 			}
65823b3a8eb9SGleb Smirnoff 			if (isalnum(c) || c == '_') {
65833b3a8eb9SGleb Smirnoff 				*p++ = (char)c;
65843b3a8eb9SGleb Smirnoff 				continue;
65853b3a8eb9SGleb Smirnoff 			}
65863b3a8eb9SGleb Smirnoff 			*p = '\0';
65873b3a8eb9SGleb Smirnoff 			lungetc(c);
65883b3a8eb9SGleb Smirnoff 			break;
65893b3a8eb9SGleb Smirnoff 		}
65903b3a8eb9SGleb Smirnoff 		val = symget(buf);
65913b3a8eb9SGleb Smirnoff 		if (val == NULL) {
65923b3a8eb9SGleb Smirnoff 			yyerror("macro '%s' not defined", buf);
65933b3a8eb9SGleb Smirnoff 			return (findeol());
65943b3a8eb9SGleb Smirnoff 		}
65953b3a8eb9SGleb Smirnoff 		parsebuf = val;
65963b3a8eb9SGleb Smirnoff 		parseindex = 0;
65973b3a8eb9SGleb Smirnoff 		goto top;
65983b3a8eb9SGleb Smirnoff 	}
65993b3a8eb9SGleb Smirnoff 
66003b3a8eb9SGleb Smirnoff 	switch (c) {
66013b3a8eb9SGleb Smirnoff 	case '\'':
66023b3a8eb9SGleb Smirnoff 	case '"':
66033b3a8eb9SGleb Smirnoff 		quotec = c;
66043b3a8eb9SGleb Smirnoff 		while (1) {
66053b3a8eb9SGleb Smirnoff 			if ((c = lgetc(quotec)) == EOF)
66063b3a8eb9SGleb Smirnoff 				return (0);
66073b3a8eb9SGleb Smirnoff 			if (c == '\n') {
66083b3a8eb9SGleb Smirnoff 				file->lineno++;
66093b3a8eb9SGleb Smirnoff 				continue;
66103b3a8eb9SGleb Smirnoff 			} else if (c == '\\') {
66113b3a8eb9SGleb Smirnoff 				if ((next = lgetc(quotec)) == EOF)
66123b3a8eb9SGleb Smirnoff 					return (0);
66133b3a8eb9SGleb Smirnoff 				if (next == quotec || c == ' ' || c == '\t')
66143b3a8eb9SGleb Smirnoff 					c = next;
66154a8e4793SKristof Provost 				else if (next == '\n') {
66164a8e4793SKristof Provost 					file->lineno++;
66173b3a8eb9SGleb Smirnoff 					continue;
66184a8e4793SKristof Provost 				}
66193b3a8eb9SGleb Smirnoff 				else
66203b3a8eb9SGleb Smirnoff 					lungetc(next);
66213b3a8eb9SGleb Smirnoff 			} else if (c == quotec) {
66223b3a8eb9SGleb Smirnoff 				*p = '\0';
66233b3a8eb9SGleb Smirnoff 				break;
66243b3a8eb9SGleb Smirnoff 			}
66253b3a8eb9SGleb Smirnoff 			if (p + 1 >= buf + sizeof(buf) - 1) {
66263b3a8eb9SGleb Smirnoff 				yyerror("string too long");
66273b3a8eb9SGleb Smirnoff 				return (findeol());
66283b3a8eb9SGleb Smirnoff 			}
66293b3a8eb9SGleb Smirnoff 			*p++ = (char)c;
66303b3a8eb9SGleb Smirnoff 		}
66313b3a8eb9SGleb Smirnoff 		yylval.v.string = strdup(buf);
66323b3a8eb9SGleb Smirnoff 		if (yylval.v.string == NULL)
66333b3a8eb9SGleb Smirnoff 			err(1, "yylex: strdup");
66343b3a8eb9SGleb Smirnoff 		return (STRING);
663580eb861dSKristof Provost 		case '!':
663680eb861dSKristof Provost 			next = lgetc(0);
663780eb861dSKristof Provost 			if (next == '=')
663880eb861dSKristof Provost 				return (NE);
663980eb861dSKristof Provost 			lungetc(next);
664080eb861dSKristof Provost 		break;
66413b3a8eb9SGleb Smirnoff 	case '<':
66423b3a8eb9SGleb Smirnoff 		next = lgetc(0);
66433b3a8eb9SGleb Smirnoff 		if (next == '>') {
66443b3a8eb9SGleb Smirnoff 			yylval.v.i = PF_OP_XRG;
66453b3a8eb9SGleb Smirnoff 			return (PORTBINARY);
664680eb861dSKristof Provost 		} else if (next == '=')
664780eb861dSKristof Provost 			return (LE);
66483b3a8eb9SGleb Smirnoff 		lungetc(next);
66493b3a8eb9SGleb Smirnoff 		break;
66503b3a8eb9SGleb Smirnoff 	case '>':
66513b3a8eb9SGleb Smirnoff 		next = lgetc(0);
66523b3a8eb9SGleb Smirnoff 		if (next == '<') {
66533b3a8eb9SGleb Smirnoff 			yylval.v.i = PF_OP_IRG;
66543b3a8eb9SGleb Smirnoff 			return (PORTBINARY);
665580eb861dSKristof Provost 		} else if (next == '=')
665680eb861dSKristof Provost 			return (GE);
66573b3a8eb9SGleb Smirnoff 		lungetc(next);
66583b3a8eb9SGleb Smirnoff 		break;
66593b3a8eb9SGleb Smirnoff 	case '-':
66603b3a8eb9SGleb Smirnoff 		next = lgetc(0);
66613b3a8eb9SGleb Smirnoff 		if (next == '>')
66623b3a8eb9SGleb Smirnoff 			return (ARROW);
66633b3a8eb9SGleb Smirnoff 		lungetc(next);
66643b3a8eb9SGleb Smirnoff 		break;
66653b3a8eb9SGleb Smirnoff 	}
66663b3a8eb9SGleb Smirnoff 
66673b3a8eb9SGleb Smirnoff #define allowed_to_end_number(x) \
66683b3a8eb9SGleb Smirnoff 	(isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
66693b3a8eb9SGleb Smirnoff 
66703b3a8eb9SGleb Smirnoff 	if (c == '-' || isdigit(c)) {
66713b3a8eb9SGleb Smirnoff 		do {
66723b3a8eb9SGleb Smirnoff 			*p++ = c;
66733b3a8eb9SGleb Smirnoff 			if ((unsigned)(p-buf) >= sizeof(buf)) {
66743b3a8eb9SGleb Smirnoff 				yyerror("string too long");
66753b3a8eb9SGleb Smirnoff 				return (findeol());
66763b3a8eb9SGleb Smirnoff 			}
66773b3a8eb9SGleb Smirnoff 		} while ((c = lgetc(0)) != EOF && isdigit(c));
66783b3a8eb9SGleb Smirnoff 		lungetc(c);
66793b3a8eb9SGleb Smirnoff 		if (p == buf + 1 && buf[0] == '-')
66803b3a8eb9SGleb Smirnoff 			goto nodigits;
66813b3a8eb9SGleb Smirnoff 		if (c == EOF || allowed_to_end_number(c)) {
66823b3a8eb9SGleb Smirnoff 			const char *errstr = NULL;
66833b3a8eb9SGleb Smirnoff 
66843b3a8eb9SGleb Smirnoff 			*p = '\0';
66853b3a8eb9SGleb Smirnoff 			yylval.v.number = strtonum(buf, LLONG_MIN,
66863b3a8eb9SGleb Smirnoff 			    LLONG_MAX, &errstr);
66873b3a8eb9SGleb Smirnoff 			if (errstr) {
66883b3a8eb9SGleb Smirnoff 				yyerror("\"%s\" invalid number: %s",
66893b3a8eb9SGleb Smirnoff 				    buf, errstr);
66903b3a8eb9SGleb Smirnoff 				return (findeol());
66913b3a8eb9SGleb Smirnoff 			}
66923b3a8eb9SGleb Smirnoff 			return (NUMBER);
66933b3a8eb9SGleb Smirnoff 		} else {
66943b3a8eb9SGleb Smirnoff nodigits:
66953b3a8eb9SGleb Smirnoff 			while (p > buf + 1)
66963b3a8eb9SGleb Smirnoff 				lungetc(*--p);
66973b3a8eb9SGleb Smirnoff 			c = *--p;
66983b3a8eb9SGleb Smirnoff 			if (c == '-')
66993b3a8eb9SGleb Smirnoff 				return (c);
67003b3a8eb9SGleb Smirnoff 		}
67013b3a8eb9SGleb Smirnoff 	}
67023b3a8eb9SGleb Smirnoff 
67033b3a8eb9SGleb Smirnoff #define allowed_in_string(x) \
67043b3a8eb9SGleb Smirnoff 	(isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
67053b3a8eb9SGleb Smirnoff 	x != '{' && x != '}' && x != '<' && x != '>' && \
67063b3a8eb9SGleb Smirnoff 	x != '!' && x != '=' && x != '/' && x != '#' && \
67073b3a8eb9SGleb Smirnoff 	x != ','))
67083b3a8eb9SGleb Smirnoff 
67093b3a8eb9SGleb Smirnoff 	if (isalnum(c) || c == ':' || c == '_') {
67103b3a8eb9SGleb Smirnoff 		do {
67113b3a8eb9SGleb Smirnoff 			*p++ = c;
67123b3a8eb9SGleb Smirnoff 			if ((unsigned)(p-buf) >= sizeof(buf)) {
67133b3a8eb9SGleb Smirnoff 				yyerror("string too long");
67143b3a8eb9SGleb Smirnoff 				return (findeol());
67153b3a8eb9SGleb Smirnoff 			}
67163b3a8eb9SGleb Smirnoff 		} while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
67173b3a8eb9SGleb Smirnoff 		lungetc(c);
67183b3a8eb9SGleb Smirnoff 		*p = '\0';
67193b3a8eb9SGleb Smirnoff 		if ((token = lookup(buf)) == STRING)
67203b3a8eb9SGleb Smirnoff 			if ((yylval.v.string = strdup(buf)) == NULL)
67213b3a8eb9SGleb Smirnoff 				err(1, "yylex: strdup");
67223b3a8eb9SGleb Smirnoff 		return (token);
67233b3a8eb9SGleb Smirnoff 	}
67243b3a8eb9SGleb Smirnoff 	if (c == '\n') {
67253b3a8eb9SGleb Smirnoff 		yylval.lineno = file->lineno;
67263b3a8eb9SGleb Smirnoff 		file->lineno++;
67273b3a8eb9SGleb Smirnoff 	}
67283b3a8eb9SGleb Smirnoff 	if (c == EOF)
67293b3a8eb9SGleb Smirnoff 		return (0);
67303b3a8eb9SGleb Smirnoff 	return (c);
67313b3a8eb9SGleb Smirnoff }
67323b3a8eb9SGleb Smirnoff 
67333b3a8eb9SGleb Smirnoff int
67343b3a8eb9SGleb Smirnoff check_file_secrecy(int fd, const char *fname)
67353b3a8eb9SGleb Smirnoff {
67363b3a8eb9SGleb Smirnoff 	struct stat	st;
67373b3a8eb9SGleb Smirnoff 
67383b3a8eb9SGleb Smirnoff 	if (fstat(fd, &st)) {
67393b3a8eb9SGleb Smirnoff 		warn("cannot stat %s", fname);
67403b3a8eb9SGleb Smirnoff 		return (-1);
67413b3a8eb9SGleb Smirnoff 	}
67423b3a8eb9SGleb Smirnoff 	if (st.st_uid != 0 && st.st_uid != getuid()) {
67433b3a8eb9SGleb Smirnoff 		warnx("%s: owner not root or current user", fname);
67443b3a8eb9SGleb Smirnoff 		return (-1);
67453b3a8eb9SGleb Smirnoff 	}
67463b3a8eb9SGleb Smirnoff 	if (st.st_mode & (S_IRWXG | S_IRWXO)) {
67473b3a8eb9SGleb Smirnoff 		warnx("%s: group/world readable/writeable", fname);
67483b3a8eb9SGleb Smirnoff 		return (-1);
67493b3a8eb9SGleb Smirnoff 	}
67503b3a8eb9SGleb Smirnoff 	return (0);
67513b3a8eb9SGleb Smirnoff }
67523b3a8eb9SGleb Smirnoff 
67533b3a8eb9SGleb Smirnoff struct file *
67543b3a8eb9SGleb Smirnoff pushfile(const char *name, int secret)
67553b3a8eb9SGleb Smirnoff {
67563b3a8eb9SGleb Smirnoff 	struct file	*nfile;
67573b3a8eb9SGleb Smirnoff 
67583b3a8eb9SGleb Smirnoff 	if ((nfile = calloc(1, sizeof(struct file))) == NULL ||
67593b3a8eb9SGleb Smirnoff 	    (nfile->name = strdup(name)) == NULL) {
67603b3a8eb9SGleb Smirnoff 		warn("malloc");
67613b3a8eb9SGleb Smirnoff 		return (NULL);
67623b3a8eb9SGleb Smirnoff 	}
67633b3a8eb9SGleb Smirnoff 	if (TAILQ_FIRST(&files) == NULL && strcmp(nfile->name, "-") == 0) {
67643b3a8eb9SGleb Smirnoff 		nfile->stream = stdin;
67653b3a8eb9SGleb Smirnoff 		free(nfile->name);
67663b3a8eb9SGleb Smirnoff 		if ((nfile->name = strdup("stdin")) == NULL) {
67673b3a8eb9SGleb Smirnoff 			warn("strdup");
67683b3a8eb9SGleb Smirnoff 			free(nfile);
67693b3a8eb9SGleb Smirnoff 			return (NULL);
67703b3a8eb9SGleb Smirnoff 		}
67713b3a8eb9SGleb Smirnoff 	} else if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
67723b3a8eb9SGleb Smirnoff 		warn("%s", nfile->name);
67733b3a8eb9SGleb Smirnoff 		free(nfile->name);
67743b3a8eb9SGleb Smirnoff 		free(nfile);
67753b3a8eb9SGleb Smirnoff 		return (NULL);
67763b3a8eb9SGleb Smirnoff 	} else if (secret &&
67773b3a8eb9SGleb Smirnoff 	    check_file_secrecy(fileno(nfile->stream), nfile->name)) {
67783b3a8eb9SGleb Smirnoff 		fclose(nfile->stream);
67793b3a8eb9SGleb Smirnoff 		free(nfile->name);
67803b3a8eb9SGleb Smirnoff 		free(nfile);
67813b3a8eb9SGleb Smirnoff 		return (NULL);
67823b3a8eb9SGleb Smirnoff 	}
67833b3a8eb9SGleb Smirnoff 	nfile->lineno = 1;
67843b3a8eb9SGleb Smirnoff 	TAILQ_INSERT_TAIL(&files, nfile, entry);
67853b3a8eb9SGleb Smirnoff 	return (nfile);
67863b3a8eb9SGleb Smirnoff }
67873b3a8eb9SGleb Smirnoff 
67883b3a8eb9SGleb Smirnoff int
67893b3a8eb9SGleb Smirnoff popfile(void)
67903b3a8eb9SGleb Smirnoff {
67913b3a8eb9SGleb Smirnoff 	struct file	*prev;
67923b3a8eb9SGleb Smirnoff 
67933b3a8eb9SGleb Smirnoff 	if ((prev = TAILQ_PREV(file, files, entry)) != NULL) {
67943b3a8eb9SGleb Smirnoff 		prev->errors += file->errors;
67953b3a8eb9SGleb Smirnoff 		TAILQ_REMOVE(&files, file, entry);
67963b3a8eb9SGleb Smirnoff 		fclose(file->stream);
67973b3a8eb9SGleb Smirnoff 		free(file->name);
67983b3a8eb9SGleb Smirnoff 		free(file);
67993b3a8eb9SGleb Smirnoff 		file = prev;
68003b3a8eb9SGleb Smirnoff 		return (0);
68013b3a8eb9SGleb Smirnoff 	}
68023b3a8eb9SGleb Smirnoff 	return (EOF);
68033b3a8eb9SGleb Smirnoff }
68043b3a8eb9SGleb Smirnoff 
68053b3a8eb9SGleb Smirnoff int
68063b3a8eb9SGleb Smirnoff parse_config(char *filename, struct pfctl *xpf)
68073b3a8eb9SGleb Smirnoff {
68083b3a8eb9SGleb Smirnoff 	int		 errors = 0;
68093b3a8eb9SGleb Smirnoff 	struct sym	*sym;
68103b3a8eb9SGleb Smirnoff 
68113b3a8eb9SGleb Smirnoff 	pf = xpf;
68123b3a8eb9SGleb Smirnoff 	errors = 0;
68133b3a8eb9SGleb Smirnoff 	rulestate = PFCTL_STATE_NONE;
68143b3a8eb9SGleb Smirnoff 	returnicmpdefault = (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
68153b3a8eb9SGleb Smirnoff 	returnicmp6default =
68163b3a8eb9SGleb Smirnoff 	    (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
68173b3a8eb9SGleb Smirnoff 	blockpolicy = PFRULE_DROP;
6818150182e3SKristof Provost 	failpolicy = PFRULE_DROP;
68193b3a8eb9SGleb Smirnoff 	require_order = 1;
68203b3a8eb9SGleb Smirnoff 
68213b3a8eb9SGleb Smirnoff 	if ((file = pushfile(filename, 0)) == NULL) {
68223b3a8eb9SGleb Smirnoff 		warn("cannot open the main config file!");
68233b3a8eb9SGleb Smirnoff 		return (-1);
68243b3a8eb9SGleb Smirnoff 	}
68253b3a8eb9SGleb Smirnoff 
68263b3a8eb9SGleb Smirnoff 	yyparse();
68273b3a8eb9SGleb Smirnoff 	errors = file->errors;
68283b3a8eb9SGleb Smirnoff 	popfile();
68293b3a8eb9SGleb Smirnoff 
68303b3a8eb9SGleb Smirnoff 	/* Free macros and check which have not been used. */
68313b3a8eb9SGleb Smirnoff 	while ((sym = TAILQ_FIRST(&symhead))) {
68323b3a8eb9SGleb Smirnoff 		if ((pf->opts & PF_OPT_VERBOSE2) && !sym->used)
68333b3a8eb9SGleb Smirnoff 			fprintf(stderr, "warning: macro '%s' not "
68343b3a8eb9SGleb Smirnoff 			    "used\n", sym->nam);
68353b3a8eb9SGleb Smirnoff 		free(sym->nam);
68363b3a8eb9SGleb Smirnoff 		free(sym->val);
68373b3a8eb9SGleb Smirnoff 		TAILQ_REMOVE(&symhead, sym, entry);
68383b3a8eb9SGleb Smirnoff 		free(sym);
68393b3a8eb9SGleb Smirnoff 	}
68403b3a8eb9SGleb Smirnoff 
68413b3a8eb9SGleb Smirnoff 	return (errors ? -1 : 0);
68423b3a8eb9SGleb Smirnoff }
68433b3a8eb9SGleb Smirnoff 
68443b3a8eb9SGleb Smirnoff int
68453b3a8eb9SGleb Smirnoff symset(const char *nam, const char *val, int persist)
68463b3a8eb9SGleb Smirnoff {
68473b3a8eb9SGleb Smirnoff 	struct sym	*sym;
68483b3a8eb9SGleb Smirnoff 
68493b3a8eb9SGleb Smirnoff 	for (sym = TAILQ_FIRST(&symhead); sym && strcmp(nam, sym->nam);
68503b3a8eb9SGleb Smirnoff 	    sym = TAILQ_NEXT(sym, entry))
68513b3a8eb9SGleb Smirnoff 		;	/* nothing */
68523b3a8eb9SGleb Smirnoff 
68533b3a8eb9SGleb Smirnoff 	if (sym != NULL) {
68543b3a8eb9SGleb Smirnoff 		if (sym->persist == 1)
68553b3a8eb9SGleb Smirnoff 			return (0);
68563b3a8eb9SGleb Smirnoff 		else {
68573b3a8eb9SGleb Smirnoff 			free(sym->nam);
68583b3a8eb9SGleb Smirnoff 			free(sym->val);
68593b3a8eb9SGleb Smirnoff 			TAILQ_REMOVE(&symhead, sym, entry);
68603b3a8eb9SGleb Smirnoff 			free(sym);
68613b3a8eb9SGleb Smirnoff 		}
68623b3a8eb9SGleb Smirnoff 	}
68633b3a8eb9SGleb Smirnoff 	if ((sym = calloc(1, sizeof(*sym))) == NULL)
68643b3a8eb9SGleb Smirnoff 		return (-1);
68653b3a8eb9SGleb Smirnoff 
68663b3a8eb9SGleb Smirnoff 	sym->nam = strdup(nam);
68673b3a8eb9SGleb Smirnoff 	if (sym->nam == NULL) {
68683b3a8eb9SGleb Smirnoff 		free(sym);
68693b3a8eb9SGleb Smirnoff 		return (-1);
68703b3a8eb9SGleb Smirnoff 	}
68713b3a8eb9SGleb Smirnoff 	sym->val = strdup(val);
68723b3a8eb9SGleb Smirnoff 	if (sym->val == NULL) {
68733b3a8eb9SGleb Smirnoff 		free(sym->nam);
68743b3a8eb9SGleb Smirnoff 		free(sym);
68753b3a8eb9SGleb Smirnoff 		return (-1);
68763b3a8eb9SGleb Smirnoff 	}
68773b3a8eb9SGleb Smirnoff 	sym->used = 0;
68783b3a8eb9SGleb Smirnoff 	sym->persist = persist;
68793b3a8eb9SGleb Smirnoff 	TAILQ_INSERT_TAIL(&symhead, sym, entry);
68803b3a8eb9SGleb Smirnoff 	return (0);
68813b3a8eb9SGleb Smirnoff }
68823b3a8eb9SGleb Smirnoff 
68833b3a8eb9SGleb Smirnoff int
68843b3a8eb9SGleb Smirnoff pfctl_cmdline_symset(char *s)
68853b3a8eb9SGleb Smirnoff {
68863b3a8eb9SGleb Smirnoff 	char	*sym, *val;
68873b3a8eb9SGleb Smirnoff 	int	 ret;
68883b3a8eb9SGleb Smirnoff 
68893b3a8eb9SGleb Smirnoff 	if ((val = strrchr(s, '=')) == NULL)
68903b3a8eb9SGleb Smirnoff 		return (-1);
68913b3a8eb9SGleb Smirnoff 
68923b3a8eb9SGleb Smirnoff 	if ((sym = malloc(strlen(s) - strlen(val) + 1)) == NULL)
68933b3a8eb9SGleb Smirnoff 		err(1, "pfctl_cmdline_symset: malloc");
68943b3a8eb9SGleb Smirnoff 
68953b3a8eb9SGleb Smirnoff 	strlcpy(sym, s, strlen(s) - strlen(val) + 1);
68963b3a8eb9SGleb Smirnoff 
68973b3a8eb9SGleb Smirnoff 	ret = symset(sym, val + 1, 1);
68983b3a8eb9SGleb Smirnoff 	free(sym);
68993b3a8eb9SGleb Smirnoff 
69003b3a8eb9SGleb Smirnoff 	return (ret);
69013b3a8eb9SGleb Smirnoff }
69023b3a8eb9SGleb Smirnoff 
69033b3a8eb9SGleb Smirnoff char *
69043b3a8eb9SGleb Smirnoff symget(const char *nam)
69053b3a8eb9SGleb Smirnoff {
69063b3a8eb9SGleb Smirnoff 	struct sym	*sym;
69073b3a8eb9SGleb Smirnoff 
69083b3a8eb9SGleb Smirnoff 	TAILQ_FOREACH(sym, &symhead, entry)
69093b3a8eb9SGleb Smirnoff 		if (strcmp(nam, sym->nam) == 0) {
69103b3a8eb9SGleb Smirnoff 			sym->used = 1;
69113b3a8eb9SGleb Smirnoff 			return (sym->val);
69123b3a8eb9SGleb Smirnoff 		}
69133b3a8eb9SGleb Smirnoff 	return (NULL);
69143b3a8eb9SGleb Smirnoff }
69153b3a8eb9SGleb Smirnoff 
69163b3a8eb9SGleb Smirnoff void
6917e9eb0941SKristof Provost mv_rules(struct pfctl_ruleset *src, struct pfctl_ruleset *dst)
69183b3a8eb9SGleb Smirnoff {
69193b3a8eb9SGleb Smirnoff 	int i;
6920e9eb0941SKristof Provost 	struct pfctl_rule *r;
69213b3a8eb9SGleb Smirnoff 
69223b3a8eb9SGleb Smirnoff 	for (i = 0; i < PF_RULESET_MAX; ++i) {
69233b3a8eb9SGleb Smirnoff 		while ((r = TAILQ_FIRST(src->rules[i].active.ptr))
69243b3a8eb9SGleb Smirnoff 		    != NULL) {
69253b3a8eb9SGleb Smirnoff 			TAILQ_REMOVE(src->rules[i].active.ptr, r, entries);
69263b3a8eb9SGleb Smirnoff 			TAILQ_INSERT_TAIL(dst->rules[i].active.ptr, r, entries);
69273b3a8eb9SGleb Smirnoff 			dst->anchor->match++;
69283b3a8eb9SGleb Smirnoff 		}
69293b3a8eb9SGleb Smirnoff 		src->anchor->match = 0;
69303b3a8eb9SGleb Smirnoff 		while ((r = TAILQ_FIRST(src->rules[i].inactive.ptr))
69313b3a8eb9SGleb Smirnoff 		    != NULL) {
69323b3a8eb9SGleb Smirnoff 			TAILQ_REMOVE(src->rules[i].inactive.ptr, r, entries);
69333b3a8eb9SGleb Smirnoff 			TAILQ_INSERT_TAIL(dst->rules[i].inactive.ptr,
69343b3a8eb9SGleb Smirnoff 				r, entries);
69353b3a8eb9SGleb Smirnoff 		}
69363b3a8eb9SGleb Smirnoff 	}
69373b3a8eb9SGleb Smirnoff }
69383b3a8eb9SGleb Smirnoff 
69393b3a8eb9SGleb Smirnoff void
6940c5131afeSKristof Provost mv_eth_rules(struct pfctl_eth_ruleset *src, struct pfctl_eth_ruleset *dst)
6941c5131afeSKristof Provost {
6942c5131afeSKristof Provost 	struct pfctl_eth_rule *r;
6943c5131afeSKristof Provost 
6944c5131afeSKristof Provost 	while ((r = TAILQ_FIRST(&src->rules)) != NULL) {
6945c5131afeSKristof Provost 		TAILQ_REMOVE(&src->rules, r, entries);
6946c5131afeSKristof Provost 		TAILQ_INSERT_TAIL(&dst->rules, r, entries);
6947c5131afeSKristof Provost 		dst->anchor->match++;
6948c5131afeSKristof Provost 	}
6949c5131afeSKristof Provost 	src->anchor->match = 0;
6950c5131afeSKristof Provost }
6951c5131afeSKristof Provost 
6952c5131afeSKristof Provost void
69533b3a8eb9SGleb Smirnoff decide_address_family(struct node_host *n, sa_family_t *af)
69543b3a8eb9SGleb Smirnoff {
69553b3a8eb9SGleb Smirnoff 	if (*af != 0 || n == NULL)
69563b3a8eb9SGleb Smirnoff 		return;
69573b3a8eb9SGleb Smirnoff 	*af = n->af;
69583b3a8eb9SGleb Smirnoff 	while ((n = n->next) != NULL) {
69593b3a8eb9SGleb Smirnoff 		if (n->af != *af) {
69603b3a8eb9SGleb Smirnoff 			*af = 0;
69613b3a8eb9SGleb Smirnoff 			return;
69623b3a8eb9SGleb Smirnoff 		}
69633b3a8eb9SGleb Smirnoff 	}
69643b3a8eb9SGleb Smirnoff }
69653b3a8eb9SGleb Smirnoff 
69663b3a8eb9SGleb Smirnoff void
69673b3a8eb9SGleb Smirnoff remove_invalid_hosts(struct node_host **nh, sa_family_t *af)
69683b3a8eb9SGleb Smirnoff {
69693b3a8eb9SGleb Smirnoff 	struct node_host	*n = *nh, *prev = NULL;
69703b3a8eb9SGleb Smirnoff 
69713b3a8eb9SGleb Smirnoff 	while (n != NULL) {
69723b3a8eb9SGleb Smirnoff 		if (*af && n->af && n->af != *af) {
69733b3a8eb9SGleb Smirnoff 			/* unlink and free n */
69743b3a8eb9SGleb Smirnoff 			struct node_host *next = n->next;
69753b3a8eb9SGleb Smirnoff 
69763b3a8eb9SGleb Smirnoff 			/* adjust tail pointer */
69773b3a8eb9SGleb Smirnoff 			if (n == (*nh)->tail)
69783b3a8eb9SGleb Smirnoff 				(*nh)->tail = prev;
69793b3a8eb9SGleb Smirnoff 			/* adjust previous node's next pointer */
69803b3a8eb9SGleb Smirnoff 			if (prev == NULL)
69813b3a8eb9SGleb Smirnoff 				*nh = next;
69823b3a8eb9SGleb Smirnoff 			else
69833b3a8eb9SGleb Smirnoff 				prev->next = next;
69843b3a8eb9SGleb Smirnoff 			/* free node */
69853b3a8eb9SGleb Smirnoff 			if (n->ifname != NULL)
69863b3a8eb9SGleb Smirnoff 				free(n->ifname);
69873b3a8eb9SGleb Smirnoff 			free(n);
69883b3a8eb9SGleb Smirnoff 			n = next;
69893b3a8eb9SGleb Smirnoff 		} else {
69903b3a8eb9SGleb Smirnoff 			if (n->af && !*af)
69913b3a8eb9SGleb Smirnoff 				*af = n->af;
69923b3a8eb9SGleb Smirnoff 			prev = n;
69933b3a8eb9SGleb Smirnoff 			n = n->next;
69943b3a8eb9SGleb Smirnoff 		}
69953b3a8eb9SGleb Smirnoff 	}
69963b3a8eb9SGleb Smirnoff }
69973b3a8eb9SGleb Smirnoff 
69983b3a8eb9SGleb Smirnoff int
69993b3a8eb9SGleb Smirnoff invalid_redirect(struct node_host *nh, sa_family_t af)
70003b3a8eb9SGleb Smirnoff {
70013b3a8eb9SGleb Smirnoff 	if (!af) {
70023b3a8eb9SGleb Smirnoff 		struct node_host *n;
70033b3a8eb9SGleb Smirnoff 
70043b3a8eb9SGleb Smirnoff 		/* tables and dyniftl are ok without an address family */
70053b3a8eb9SGleb Smirnoff 		for (n = nh; n != NULL; n = n->next) {
70063b3a8eb9SGleb Smirnoff 			if (n->addr.type != PF_ADDR_TABLE &&
70073b3a8eb9SGleb Smirnoff 			    n->addr.type != PF_ADDR_DYNIFTL) {
70083b3a8eb9SGleb Smirnoff 				yyerror("address family not given and "
70093b3a8eb9SGleb Smirnoff 				    "translation address expands to multiple "
70103b3a8eb9SGleb Smirnoff 				    "address families");
70113b3a8eb9SGleb Smirnoff 				return (1);
70123b3a8eb9SGleb Smirnoff 			}
70133b3a8eb9SGleb Smirnoff 		}
70143b3a8eb9SGleb Smirnoff 	}
70153b3a8eb9SGleb Smirnoff 	if (nh == NULL) {
70163b3a8eb9SGleb Smirnoff 		yyerror("no translation address with matching address family "
70173b3a8eb9SGleb Smirnoff 		    "found.");
70183b3a8eb9SGleb Smirnoff 		return (1);
70193b3a8eb9SGleb Smirnoff 	}
70203b3a8eb9SGleb Smirnoff 	return (0);
70213b3a8eb9SGleb Smirnoff }
70223b3a8eb9SGleb Smirnoff 
70233b3a8eb9SGleb Smirnoff int
70243b3a8eb9SGleb Smirnoff atoul(char *s, u_long *ulvalp)
70253b3a8eb9SGleb Smirnoff {
70263b3a8eb9SGleb Smirnoff 	u_long	 ulval;
70273b3a8eb9SGleb Smirnoff 	char	*ep;
70283b3a8eb9SGleb Smirnoff 
70293b3a8eb9SGleb Smirnoff 	errno = 0;
70303b3a8eb9SGleb Smirnoff 	ulval = strtoul(s, &ep, 0);
70313b3a8eb9SGleb Smirnoff 	if (s[0] == '\0' || *ep != '\0')
70323b3a8eb9SGleb Smirnoff 		return (-1);
70333b3a8eb9SGleb Smirnoff 	if (errno == ERANGE && ulval == ULONG_MAX)
70343b3a8eb9SGleb Smirnoff 		return (-1);
70353b3a8eb9SGleb Smirnoff 	*ulvalp = ulval;
70363b3a8eb9SGleb Smirnoff 	return (0);
70373b3a8eb9SGleb Smirnoff }
70383b3a8eb9SGleb Smirnoff 
70393b3a8eb9SGleb Smirnoff int
70403b3a8eb9SGleb Smirnoff getservice(char *n)
70413b3a8eb9SGleb Smirnoff {
70423b3a8eb9SGleb Smirnoff 	struct servent	*s;
70433b3a8eb9SGleb Smirnoff 	u_long		 ulval;
70443b3a8eb9SGleb Smirnoff 
70453b3a8eb9SGleb Smirnoff 	if (atoul(n, &ulval) == 0) {
70463b3a8eb9SGleb Smirnoff 		if (ulval > 65535) {
70473b3a8eb9SGleb Smirnoff 			yyerror("illegal port value %lu", ulval);
70483b3a8eb9SGleb Smirnoff 			return (-1);
70493b3a8eb9SGleb Smirnoff 		}
70503b3a8eb9SGleb Smirnoff 		return (htons(ulval));
70513b3a8eb9SGleb Smirnoff 	} else {
70523b3a8eb9SGleb Smirnoff 		s = getservbyname(n, "tcp");
70533b3a8eb9SGleb Smirnoff 		if (s == NULL)
70543b3a8eb9SGleb Smirnoff 			s = getservbyname(n, "udp");
70550bd4a683SKristof Provost 		if (s == NULL)
70560bd4a683SKristof Provost 			s = getservbyname(n, "sctp");
70573b3a8eb9SGleb Smirnoff 		if (s == NULL) {
70583b3a8eb9SGleb Smirnoff 			yyerror("unknown port %s", n);
70593b3a8eb9SGleb Smirnoff 			return (-1);
70603b3a8eb9SGleb Smirnoff 		}
70613b3a8eb9SGleb Smirnoff 		return (s->s_port);
70623b3a8eb9SGleb Smirnoff 	}
70633b3a8eb9SGleb Smirnoff }
70643b3a8eb9SGleb Smirnoff 
70653b3a8eb9SGleb Smirnoff int
70666fcc8e04SKristof Provost rule_label(struct pfctl_rule *r, char *s[PF_RULE_MAX_LABEL_COUNT])
70673b3a8eb9SGleb Smirnoff {
70686fcc8e04SKristof Provost 	for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++) {
70696fcc8e04SKristof Provost 		if (s[i] == NULL)
70706fcc8e04SKristof Provost 			return (0);
70716fcc8e04SKristof Provost 
70726fcc8e04SKristof Provost 		if (strlcpy(r->label[i], s[i], sizeof(r->label[0])) >=
70736fcc8e04SKristof Provost 		    sizeof(r->label[0])) {
70743b3a8eb9SGleb Smirnoff 			yyerror("rule label too long (max %d chars)",
70756fcc8e04SKristof Provost 			    sizeof(r->label[0])-1);
70763b3a8eb9SGleb Smirnoff 			return (-1);
70773b3a8eb9SGleb Smirnoff 		}
70783b3a8eb9SGleb Smirnoff 	}
70793b3a8eb9SGleb Smirnoff 	return (0);
70803b3a8eb9SGleb Smirnoff }
70813b3a8eb9SGleb Smirnoff 
7082ef661d4aSChristian McDonald int
7083ef661d4aSChristian McDonald eth_rule_label(struct pfctl_eth_rule *r, char *s[PF_RULE_MAX_LABEL_COUNT])
7084ef661d4aSChristian McDonald {
7085ef661d4aSChristian McDonald 	for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++) {
7086ef661d4aSChristian McDonald 		if (s[i] == NULL)
7087ef661d4aSChristian McDonald 			return (0);
7088ef661d4aSChristian McDonald 
7089ef661d4aSChristian McDonald 		if (strlcpy(r->label[i], s[i], sizeof(r->label[0])) >=
7090ef661d4aSChristian McDonald 		    sizeof(r->label[0])) {
7091ef661d4aSChristian McDonald 			yyerror("rule label too long (max %d chars)",
7092ef661d4aSChristian McDonald 			    sizeof(r->label[0])-1);
7093ef661d4aSChristian McDonald 			return (-1);
7094ef661d4aSChristian McDonald 		}
7095ef661d4aSChristian McDonald 	}
7096ef661d4aSChristian McDonald 	return (0);
7097ef661d4aSChristian McDonald }
7098ef661d4aSChristian McDonald 
70993b3a8eb9SGleb Smirnoff u_int16_t
71003b3a8eb9SGleb Smirnoff parseicmpspec(char *w, sa_family_t af)
71013b3a8eb9SGleb Smirnoff {
71023b3a8eb9SGleb Smirnoff 	const struct icmpcodeent	*p;
71033b3a8eb9SGleb Smirnoff 	u_long				 ulval;
71043b3a8eb9SGleb Smirnoff 	u_int8_t			 icmptype;
71053b3a8eb9SGleb Smirnoff 
71063b3a8eb9SGleb Smirnoff 	if (af == AF_INET)
71073b3a8eb9SGleb Smirnoff 		icmptype = returnicmpdefault >> 8;
71083b3a8eb9SGleb Smirnoff 	else
71093b3a8eb9SGleb Smirnoff 		icmptype = returnicmp6default >> 8;
71103b3a8eb9SGleb Smirnoff 
71113b3a8eb9SGleb Smirnoff 	if (atoul(w, &ulval) == -1) {
71123b3a8eb9SGleb Smirnoff 		if ((p = geticmpcodebyname(icmptype, w, af)) == NULL) {
71133b3a8eb9SGleb Smirnoff 			yyerror("unknown icmp code %s", w);
71143b3a8eb9SGleb Smirnoff 			return (0);
71153b3a8eb9SGleb Smirnoff 		}
71163b3a8eb9SGleb Smirnoff 		ulval = p->code;
71173b3a8eb9SGleb Smirnoff 	}
71183b3a8eb9SGleb Smirnoff 	if (ulval > 255) {
71193b3a8eb9SGleb Smirnoff 		yyerror("invalid icmp code %lu", ulval);
71203b3a8eb9SGleb Smirnoff 		return (0);
71213b3a8eb9SGleb Smirnoff 	}
71223b3a8eb9SGleb Smirnoff 	return (icmptype << 8 | ulval);
71233b3a8eb9SGleb Smirnoff }
71243b3a8eb9SGleb Smirnoff 
71253b3a8eb9SGleb Smirnoff int
71263b3a8eb9SGleb Smirnoff parseport(char *port, struct range *r, int extensions)
71273b3a8eb9SGleb Smirnoff {
71283b3a8eb9SGleb Smirnoff 	char	*p = strchr(port, ':');
71293b3a8eb9SGleb Smirnoff 
71303b3a8eb9SGleb Smirnoff 	if (p == NULL) {
71313b3a8eb9SGleb Smirnoff 		if ((r->a = getservice(port)) == -1)
71323b3a8eb9SGleb Smirnoff 			return (-1);
71333b3a8eb9SGleb Smirnoff 		r->b = 0;
71343b3a8eb9SGleb Smirnoff 		r->t = PF_OP_NONE;
71353b3a8eb9SGleb Smirnoff 		return (0);
71363b3a8eb9SGleb Smirnoff 	}
71373b3a8eb9SGleb Smirnoff 	if ((extensions & PPORT_STAR) && !strcmp(p+1, "*")) {
71383b3a8eb9SGleb Smirnoff 		*p = 0;
71393b3a8eb9SGleb Smirnoff 		if ((r->a = getservice(port)) == -1)
71403b3a8eb9SGleb Smirnoff 			return (-1);
71413b3a8eb9SGleb Smirnoff 		r->b = 0;
71423b3a8eb9SGleb Smirnoff 		r->t = PF_OP_IRG;
71433b3a8eb9SGleb Smirnoff 		return (0);
71443b3a8eb9SGleb Smirnoff 	}
71453b3a8eb9SGleb Smirnoff 	if ((extensions & PPORT_RANGE)) {
71463b3a8eb9SGleb Smirnoff 		*p++ = 0;
71473b3a8eb9SGleb Smirnoff 		if ((r->a = getservice(port)) == -1 ||
71483b3a8eb9SGleb Smirnoff 		    (r->b = getservice(p)) == -1)
71493b3a8eb9SGleb Smirnoff 			return (-1);
71503b3a8eb9SGleb Smirnoff 		if (r->a == r->b) {
71513b3a8eb9SGleb Smirnoff 			r->b = 0;
71523b3a8eb9SGleb Smirnoff 			r->t = PF_OP_NONE;
71533b3a8eb9SGleb Smirnoff 		} else
71543b3a8eb9SGleb Smirnoff 			r->t = PF_OP_RRG;
71553b3a8eb9SGleb Smirnoff 		return (0);
71563b3a8eb9SGleb Smirnoff 	}
71573b3a8eb9SGleb Smirnoff 	return (-1);
71583b3a8eb9SGleb Smirnoff }
71593b3a8eb9SGleb Smirnoff 
71603b3a8eb9SGleb Smirnoff int
71613b3a8eb9SGleb Smirnoff pfctl_load_anchors(int dev, struct pfctl *pf, struct pfr_buffer *trans)
71623b3a8eb9SGleb Smirnoff {
71633b3a8eb9SGleb Smirnoff 	struct loadanchors	*la;
71643b3a8eb9SGleb Smirnoff 
71653b3a8eb9SGleb Smirnoff 	TAILQ_FOREACH(la, &loadanchorshead, entries) {
71663b3a8eb9SGleb Smirnoff 		if (pf->opts & PF_OPT_VERBOSE)
71673b3a8eb9SGleb Smirnoff 			fprintf(stderr, "\nLoading anchor %s from %s\n",
71683b3a8eb9SGleb Smirnoff 			    la->anchorname, la->filename);
71693b3a8eb9SGleb Smirnoff 		if (pfctl_rules(dev, la->filename, pf->opts, pf->optimize,
71703b3a8eb9SGleb Smirnoff 		    la->anchorname, trans) == -1)
71713b3a8eb9SGleb Smirnoff 			return (-1);
71723b3a8eb9SGleb Smirnoff 	}
71733b3a8eb9SGleb Smirnoff 
71743b3a8eb9SGleb Smirnoff 	return (0);
71753b3a8eb9SGleb Smirnoff }
71763b3a8eb9SGleb Smirnoff 
71773b3a8eb9SGleb Smirnoff int
71781f495578SKristof Provost kw_casecmp(const void *k, const void *e)
71791f495578SKristof Provost {
71801f495578SKristof Provost 	return (strcasecmp(k, ((const struct keywords *)e)->k_name));
71811f495578SKristof Provost }
71821f495578SKristof Provost 
71831f495578SKristof Provost int
71841f495578SKristof Provost map_tos(char *s, int *val)
71851f495578SKristof Provost {
71861f495578SKristof Provost 	/* DiffServ Codepoints and other TOS mappings */
71871f495578SKristof Provost 	const struct keywords	 toswords[] = {
71881f495578SKristof Provost 		{ "af11",		IPTOS_DSCP_AF11 },
71891f495578SKristof Provost 		{ "af12",		IPTOS_DSCP_AF12 },
71901f495578SKristof Provost 		{ "af13",		IPTOS_DSCP_AF13 },
71911f495578SKristof Provost 		{ "af21",		IPTOS_DSCP_AF21 },
71921f495578SKristof Provost 		{ "af22",		IPTOS_DSCP_AF22 },
71931f495578SKristof Provost 		{ "af23",		IPTOS_DSCP_AF23 },
71941f495578SKristof Provost 		{ "af31",		IPTOS_DSCP_AF31 },
71951f495578SKristof Provost 		{ "af32",		IPTOS_DSCP_AF32 },
71961f495578SKristof Provost 		{ "af33",		IPTOS_DSCP_AF33 },
71971f495578SKristof Provost 		{ "af41",		IPTOS_DSCP_AF41 },
71981f495578SKristof Provost 		{ "af42",		IPTOS_DSCP_AF42 },
71991f495578SKristof Provost 		{ "af43",		IPTOS_DSCP_AF43 },
72001f495578SKristof Provost 		{ "critical",		IPTOS_PREC_CRITIC_ECP },
72011f495578SKristof Provost 		{ "cs0",		IPTOS_DSCP_CS0 },
72021f495578SKristof Provost 		{ "cs1",		IPTOS_DSCP_CS1 },
72031f495578SKristof Provost 		{ "cs2",		IPTOS_DSCP_CS2 },
72041f495578SKristof Provost 		{ "cs3",		IPTOS_DSCP_CS3 },
72051f495578SKristof Provost 		{ "cs4",		IPTOS_DSCP_CS4 },
72061f495578SKristof Provost 		{ "cs5",		IPTOS_DSCP_CS5 },
72071f495578SKristof Provost 		{ "cs6",		IPTOS_DSCP_CS6 },
72081f495578SKristof Provost 		{ "cs7",		IPTOS_DSCP_CS7 },
72091f495578SKristof Provost 		{ "ef",			IPTOS_DSCP_EF },
72101f495578SKristof Provost 		{ "inetcontrol",	IPTOS_PREC_INTERNETCONTROL },
72111f495578SKristof Provost 		{ "lowdelay",		IPTOS_LOWDELAY },
72121f495578SKristof Provost 		{ "netcontrol",		IPTOS_PREC_NETCONTROL },
72131f495578SKristof Provost 		{ "reliability",	IPTOS_RELIABILITY },
7214b4e3f3c2SKristof Provost 		{ "throughput",		IPTOS_THROUGHPUT },
7215b4e3f3c2SKristof Provost 		{ "va",			IPTOS_DSCP_VA }
72161f495578SKristof Provost 	};
72171f495578SKristof Provost 	const struct keywords	*p;
72181f495578SKristof Provost 
72191f495578SKristof Provost 	p = bsearch(s, toswords, sizeof(toswords)/sizeof(toswords[0]),
72201f495578SKristof Provost 	    sizeof(toswords[0]), kw_casecmp);
72211f495578SKristof Provost 
72221f495578SKristof Provost 	if (p) {
72231f495578SKristof Provost 		*val = p->k_val;
72241f495578SKristof Provost 		return (1);
72251f495578SKristof Provost 	}
72261f495578SKristof Provost 	return (0);
72271f495578SKristof Provost }
72281f495578SKristof Provost 
72291f495578SKristof Provost int
72303b3a8eb9SGleb Smirnoff rt_tableid_max(void)
72313b3a8eb9SGleb Smirnoff {
72323b3a8eb9SGleb Smirnoff #ifdef __FreeBSD__
72333b3a8eb9SGleb Smirnoff 	int fibs;
72343b3a8eb9SGleb Smirnoff 	size_t l = sizeof(fibs);
72353b3a8eb9SGleb Smirnoff 
72363b3a8eb9SGleb Smirnoff         if (sysctlbyname("net.fibs", &fibs, &l, NULL, 0) == -1)
72373b3a8eb9SGleb Smirnoff 		fibs = 16;	/* XXX RT_MAXFIBS, at least limit it some. */
72383b3a8eb9SGleb Smirnoff 	/*
72393b3a8eb9SGleb Smirnoff 	 * As the OpenBSD code only compares > and not >= we need to adjust
72403b3a8eb9SGleb Smirnoff 	 * here given we only accept values of 0..n and want to avoid #ifdefs
7241b68ac800SPedro F. Giffuni 	 * in the grammar.
72423b3a8eb9SGleb Smirnoff 	 */
72433b3a8eb9SGleb Smirnoff 	return (fibs - 1);
72443b3a8eb9SGleb Smirnoff #else
72453b3a8eb9SGleb Smirnoff 	return (RT_TABLEID_MAX);
72463b3a8eb9SGleb Smirnoff #endif
72473b3a8eb9SGleb Smirnoff }
7248b590f17aSKristof Provost 
7249b590f17aSKristof Provost struct node_mac*
7250b590f17aSKristof Provost node_mac_from_string(const char *str)
7251b590f17aSKristof Provost {
7252b590f17aSKristof Provost 	struct node_mac *m;
7253b590f17aSKristof Provost 
7254b590f17aSKristof Provost 	m = calloc(1, sizeof(struct node_mac));
7255b590f17aSKristof Provost 	if (m == NULL)
7256b590f17aSKristof Provost 		err(1, "mac: calloc");
7257b590f17aSKristof Provost 
7258b590f17aSKristof Provost 	if (sscanf(str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
7259b590f17aSKristof Provost 	    &m->mac[0], &m->mac[1], &m->mac[2], &m->mac[3], &m->mac[4],
7260b590f17aSKristof Provost 	    &m->mac[5]) != 6) {
7261b590f17aSKristof Provost 		free(m);
7262b590f17aSKristof Provost 		yyerror("invalid MAC address");
7263b590f17aSKristof Provost 		return (NULL);
7264b590f17aSKristof Provost 	}
7265b590f17aSKristof Provost 
7266b590f17aSKristof Provost 	memset(m->mask, 0xff, ETHER_ADDR_LEN);
7267c32cd180SKristof Provost 	m->isset = true;
7268b590f17aSKristof Provost 	m->next = NULL;
7269b590f17aSKristof Provost 	m->tail = m;
7270b590f17aSKristof Provost 
7271b590f17aSKristof Provost 	return (m);
7272b590f17aSKristof Provost }
7273b590f17aSKristof Provost 
7274b590f17aSKristof Provost struct node_mac*
7275b590f17aSKristof Provost node_mac_from_string_masklen(const char *str, int masklen)
7276b590f17aSKristof Provost {
7277b590f17aSKristof Provost 	struct node_mac *m;
7278b590f17aSKristof Provost 
7279b590f17aSKristof Provost 	if (masklen < 0 || masklen > (ETHER_ADDR_LEN * 8)) {
7280b590f17aSKristof Provost 		yyerror("invalid MAC mask length");
7281b590f17aSKristof Provost 		return (NULL);
7282b590f17aSKristof Provost 	}
7283b590f17aSKristof Provost 
7284b590f17aSKristof Provost 	m = node_mac_from_string(str);
7285b590f17aSKristof Provost 	if (m == NULL)
7286b590f17aSKristof Provost 		return (NULL);
7287b590f17aSKristof Provost 
7288b590f17aSKristof Provost 	memset(m->mask, 0, ETHER_ADDR_LEN);
7289b590f17aSKristof Provost 	for (int i = 0; i < masklen; i++)
7290b590f17aSKristof Provost 		m->mask[i / 8] |= 1 << (i % 8);
7291b590f17aSKristof Provost 
7292b590f17aSKristof Provost 	return (m);
7293b590f17aSKristof Provost }
7294b590f17aSKristof Provost 
7295b590f17aSKristof Provost struct node_mac*
7296b590f17aSKristof Provost node_mac_from_string_mask(const char *str, const char *mask)
7297b590f17aSKristof Provost {
7298b590f17aSKristof Provost 	struct node_mac *m;
7299b590f17aSKristof Provost 
7300b590f17aSKristof Provost 	m = node_mac_from_string(str);
7301b590f17aSKristof Provost 	if (m == NULL)
7302b590f17aSKristof Provost 		return (NULL);
7303b590f17aSKristof Provost 
7304b590f17aSKristof Provost 	if (sscanf(mask, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
7305b590f17aSKristof Provost 	    &m->mask[0], &m->mask[1], &m->mask[2], &m->mask[3], &m->mask[4],
7306b590f17aSKristof Provost 	    &m->mask[5]) != 6) {
7307b590f17aSKristof Provost 		free(m);
7308b590f17aSKristof Provost 		yyerror("invalid MAC mask");
7309b590f17aSKristof Provost 		return (NULL);
7310b590f17aSKristof Provost 	}
7311b590f17aSKristof Provost 
7312b590f17aSKristof Provost 	return (m);
7313b590f17aSKristof Provost }
7314