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