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