parse.y revision 1.552
1/* $OpenBSD: parse.y,v 1.552 2008/09/10 00:32:03 deraadt Exp $ */ 2 3/* 4 * Copyright (c) 2001 Markus Friedl. All rights reserved. 5 * Copyright (c) 2001 Daniel Hartmeier. All rights reserved. 6 * Copyright (c) 2001 Theo de Raadt. All rights reserved. 7 * Copyright (c) 2002,2003 Henning Brauer. All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29%{ 30#include <sys/types.h> 31#include <sys/socket.h> 32#include <sys/stat.h> 33#include <net/if.h> 34#include <netinet/in.h> 35#include <netinet/in_systm.h> 36#include <netinet/ip.h> 37#include <netinet/ip_icmp.h> 38#include <netinet/icmp6.h> 39#include <net/pfvar.h> 40#include <arpa/inet.h> 41#include <altq/altq.h> 42#include <altq/altq_cbq.h> 43#include <altq/altq_priq.h> 44#include <altq/altq_hfsc.h> 45 46#include <stdio.h> 47#include <unistd.h> 48#include <stdlib.h> 49#include <netdb.h> 50#include <stdarg.h> 51#include <errno.h> 52#include <string.h> 53#include <ctype.h> 54#include <math.h> 55#include <err.h> 56#include <limits.h> 57#include <pwd.h> 58#include <grp.h> 59#include <md5.h> 60 61#include "pfctl_parser.h" 62#include "pfctl.h" 63 64static struct pfctl *pf = NULL; 65static int debug = 0; 66static int rulestate = 0; 67static u_int16_t returnicmpdefault = 68 (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT; 69static u_int16_t returnicmp6default = 70 (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT; 71static int blockpolicy = PFRULE_DROP; 72static int require_order = 1; 73static int default_statelock; 74 75TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files); 76static struct file { 77 TAILQ_ENTRY(file) entry; 78 FILE *stream; 79 char *name; 80 int lineno; 81 int errors; 82} *file; 83struct file *pushfile(const char *, int); 84int popfile(void); 85int check_file_secrecy(int, const char *); 86int yyparse(void); 87int yylex(void); 88int yyerror(const char *, ...); 89int kw_cmp(const void *, const void *); 90int lookup(char *); 91int lgetc(int); 92int lungetc(int); 93int findeol(void); 94 95TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead); 96struct sym { 97 TAILQ_ENTRY(sym) entry; 98 int used; 99 int persist; 100 char *nam; 101 char *val; 102}; 103int symset(const char *, const char *, int); 104char *symget(const char *); 105 106int atoul(char *, u_long *); 107 108enum { 109 PFCTL_STATE_NONE, 110 PFCTL_STATE_OPTION, 111 PFCTL_STATE_SCRUB, 112 PFCTL_STATE_QUEUE, 113 PFCTL_STATE_NAT, 114 PFCTL_STATE_FILTER 115}; 116 117struct node_proto { 118 u_int8_t proto; 119 struct node_proto *next; 120 struct node_proto *tail; 121}; 122 123struct node_port { 124 u_int16_t port[2]; 125 u_int8_t op; 126 struct node_port *next; 127 struct node_port *tail; 128}; 129 130struct node_uid { 131 uid_t uid[2]; 132 u_int8_t op; 133 struct node_uid *next; 134 struct node_uid *tail; 135}; 136 137struct node_gid { 138 gid_t gid[2]; 139 u_int8_t op; 140 struct node_gid *next; 141 struct node_gid *tail; 142}; 143 144struct node_icmp { 145 u_int8_t code; 146 u_int8_t type; 147 u_int8_t proto; 148 struct node_icmp *next; 149 struct node_icmp *tail; 150}; 151 152enum { PF_STATE_OPT_MAX, PF_STATE_OPT_NOSYNC, PF_STATE_OPT_SRCTRACK, 153 PF_STATE_OPT_MAX_SRC_STATES, PF_STATE_OPT_MAX_SRC_CONN, 154 PF_STATE_OPT_MAX_SRC_CONN_RATE, PF_STATE_OPT_MAX_SRC_NODES, 155 PF_STATE_OPT_OVERLOAD, PF_STATE_OPT_STATELOCK, 156 PF_STATE_OPT_TIMEOUT, PF_STATE_OPT_SLOPPY, 157 PF_STATE_OPT_PFLOW }; 158 159enum { PF_SRCTRACK_NONE, PF_SRCTRACK, PF_SRCTRACK_GLOBAL, PF_SRCTRACK_RULE }; 160 161struct node_state_opt { 162 int type; 163 union { 164 u_int32_t max_states; 165 u_int32_t max_src_states; 166 u_int32_t max_src_conn; 167 struct { 168 u_int32_t limit; 169 u_int32_t seconds; 170 } max_src_conn_rate; 171 struct { 172 u_int8_t flush; 173 char tblname[PF_TABLE_NAME_SIZE]; 174 } overload; 175 u_int32_t max_src_nodes; 176 u_int8_t src_track; 177 u_int32_t statelock; 178 struct { 179 int number; 180 u_int32_t seconds; 181 } timeout; 182 } data; 183 struct node_state_opt *next; 184 struct node_state_opt *tail; 185}; 186 187struct peer { 188 struct node_host *host; 189 struct node_port *port; 190}; 191 192struct node_queue { 193 char queue[PF_QNAME_SIZE]; 194 char parent[PF_QNAME_SIZE]; 195 char ifname[IFNAMSIZ]; 196 int scheduler; 197 struct node_queue *next; 198 struct node_queue *tail; 199} *queues = NULL; 200 201struct node_qassign { 202 char *qname; 203 char *pqname; 204}; 205 206struct filter_opts { 207 int marker; 208#define FOM_FLAGS 0x01 209#define FOM_ICMP 0x02 210#define FOM_TOS 0x04 211#define FOM_KEEP 0x08 212#define FOM_SRCTRACK 0x10 213 struct node_uid *uid; 214 struct node_gid *gid; 215 struct { 216 u_int8_t b1; 217 u_int8_t b2; 218 u_int16_t w; 219 u_int16_t w2; 220 } flags; 221 struct node_icmp *icmpspec; 222 u_int32_t tos; 223 u_int32_t prob; 224 struct { 225 int action; 226 struct node_state_opt *options; 227 } keep; 228 int fragment; 229 int allowopts; 230 char *label; 231 struct node_qassign queues; 232 char *tag; 233 char *match_tag; 234 u_int8_t match_tag_not; 235 u_int rtableid; 236 struct { 237 struct node_host *addr; 238 u_int16_t port; 239 } divert; 240} filter_opts; 241 242struct antispoof_opts { 243 char *label; 244 u_int rtableid; 245} antispoof_opts; 246 247struct scrub_opts { 248 int marker; 249#define SOM_MINTTL 0x01 250#define SOM_MAXMSS 0x02 251#define SOM_FRAGCACHE 0x04 252#define SOM_SETTOS 0x08 253 int nodf; 254 int minttl; 255 int maxmss; 256 int settos; 257 int fragcache; 258 int randomid; 259 int reassemble_tcp; 260 char *match_tag; 261 u_int8_t match_tag_not; 262 u_int rtableid; 263} scrub_opts; 264 265struct queue_opts { 266 int marker; 267#define QOM_BWSPEC 0x01 268#define QOM_SCHEDULER 0x02 269#define QOM_PRIORITY 0x04 270#define QOM_TBRSIZE 0x08 271#define QOM_QLIMIT 0x10 272 struct node_queue_bw queue_bwspec; 273 struct node_queue_opt scheduler; 274 int priority; 275 int tbrsize; 276 int qlimit; 277} queue_opts; 278 279struct table_opts { 280 int flags; 281 int init_addr; 282 struct node_tinithead init_nodes; 283} table_opts; 284 285struct pool_opts { 286 int marker; 287#define POM_TYPE 0x01 288#define POM_STICKYADDRESS 0x02 289 u_int8_t opts; 290 int type; 291 int staticport; 292 struct pf_poolhashkey *key; 293 294} pool_opts; 295 296 297struct node_hfsc_opts hfsc_opts; 298 299int disallow_table(struct node_host *, const char *); 300int disallow_urpf_failed(struct node_host *, const char *); 301int disallow_alias(struct node_host *, const char *); 302int rule_consistent(struct pf_rule *, int); 303int filter_consistent(struct pf_rule *, int); 304int nat_consistent(struct pf_rule *); 305int rdr_consistent(struct pf_rule *); 306int process_tabledef(char *, struct table_opts *); 307void expand_label_str(char *, size_t, const char *, const char *); 308void expand_label_if(const char *, char *, size_t, const char *); 309void expand_label_addr(const char *, char *, size_t, u_int8_t, 310 struct node_host *); 311void expand_label_port(const char *, char *, size_t, 312 struct node_port *); 313void expand_label_proto(const char *, char *, size_t, u_int8_t); 314void expand_label_nr(const char *, char *, size_t); 315void expand_label(char *, size_t, const char *, u_int8_t, 316 struct node_host *, struct node_port *, struct node_host *, 317 struct node_port *, u_int8_t); 318void expand_rule(struct pf_rule *, struct node_if *, 319 struct node_host *, struct node_proto *, struct node_os *, 320 struct node_host *, struct node_port *, struct node_host *, 321 struct node_port *, struct node_uid *, struct node_gid *, 322 struct node_icmp *, const char *); 323int expand_altq(struct pf_altq *, struct node_if *, 324 struct node_queue *, struct node_queue_bw bwspec, 325 struct node_queue_opt *); 326int expand_queue(struct pf_altq *, struct node_if *, 327 struct node_queue *, struct node_queue_bw, 328 struct node_queue_opt *); 329int expand_skip_interface(struct node_if *); 330 331int check_rulestate(int); 332int getservice(char *); 333int rule_label(struct pf_rule *, char *); 334 335void mv_rules(struct pf_ruleset *, struct pf_ruleset *); 336void decide_address_family(struct node_host *, sa_family_t *); 337void remove_invalid_hosts(struct node_host **, sa_family_t *); 338int invalid_redirect(struct node_host *, sa_family_t); 339u_int16_t parseicmpspec(char *, sa_family_t); 340 341TAILQ_HEAD(loadanchorshead, loadanchors) 342 loadanchorshead = TAILQ_HEAD_INITIALIZER(loadanchorshead); 343 344struct loadanchors { 345 TAILQ_ENTRY(loadanchors) entries; 346 char *anchorname; 347 char *filename; 348}; 349 350typedef struct { 351 union { 352 int64_t number; 353 double probability; 354 int i; 355 char *string; 356 u_int rtableid; 357 struct { 358 u_int8_t b1; 359 u_int8_t b2; 360 u_int16_t w; 361 u_int16_t w2; 362 } b; 363 struct range { 364 int a; 365 int b; 366 int t; 367 } range; 368 struct node_if *interface; 369 struct node_proto *proto; 370 struct node_icmp *icmp; 371 struct node_host *host; 372 struct node_os *os; 373 struct node_port *port; 374 struct node_uid *uid; 375 struct node_gid *gid; 376 struct node_state_opt *state_opt; 377 struct peer peer; 378 struct { 379 struct peer src, dst; 380 struct node_os *src_os; 381 } fromto; 382 struct { 383 struct node_host *host; 384 u_int8_t rt; 385 u_int8_t pool_opts; 386 sa_family_t af; 387 struct pf_poolhashkey *key; 388 } route; 389 struct redirection { 390 struct node_host *host; 391 struct range rport; 392 } *redirection; 393 struct { 394 int action; 395 struct node_state_opt *options; 396 } keep_state; 397 struct { 398 u_int8_t log; 399 u_int8_t logif; 400 u_int8_t quick; 401 } logquick; 402 struct { 403 int neg; 404 char *name; 405 } tagged; 406 struct pf_poolhashkey *hashkey; 407 struct node_queue *queue; 408 struct node_queue_opt queue_options; 409 struct node_queue_bw queue_bwspec; 410 struct node_qassign qassign; 411 struct filter_opts filter_opts; 412 struct antispoof_opts antispoof_opts; 413 struct queue_opts queue_opts; 414 struct scrub_opts scrub_opts; 415 struct table_opts table_opts; 416 struct pool_opts pool_opts; 417 struct node_hfsc_opts hfsc_opts; 418 } v; 419 int lineno; 420} YYSTYPE; 421 422#define PPORT_RANGE 1 423#define PPORT_STAR 2 424int parseport(char *, struct range *r, int); 425 426#define DYNIF_MULTIADDR(addr) ((addr).type == PF_ADDR_DYNIFTL && \ 427 (!((addr).iflags & PFI_AFLAG_NOALIAS) || \ 428 !isdigit((addr).v.ifname[strlen((addr).v.ifname)-1]))) 429 430%} 431 432%token PASS BLOCK SCRUB RETURN IN OS OUT LOG QUICK ON FROM TO FLAGS 433%token RETURNRST RETURNICMP RETURNICMP6 PROTO INET INET6 ALL ANY ICMPTYPE 434%token ICMP6TYPE CODE KEEP MODULATE STATE PORT RDR NAT BINAT ARROW NODF 435%token MINTTL ERROR ALLOWOPTS FASTROUTE FILENAME ROUTETO DUPTO REPLYTO NO LABEL 436%token NOROUTE URPFFAILED FRAGMENT USER GROUP MAXMSS MAXIMUM TTL TOS DROP TABLE 437%token REASSEMBLE FRAGDROP FRAGCROP ANCHOR NATANCHOR RDRANCHOR BINATANCHOR 438%token SET OPTIMIZATION TIMEOUT LIMIT LOGINTERFACE BLOCKPOLICY RANDOMID 439%token REQUIREORDER SYNPROXY FINGERPRINTS NOSYNC DEBUG SKIP HOSTID 440%token ANTISPOOF FOR INCLUDE 441%token BITMASK RANDOM SOURCEHASH ROUNDROBIN STATICPORT PROBABILITY 442%token ALTQ CBQ PRIQ HFSC BANDWIDTH TBRSIZE LINKSHARE REALTIME UPPERLIMIT 443%token QUEUE PRIORITY QLIMIT RTABLE 444%token LOAD RULESET_OPTIMIZATION 445%token STICKYADDRESS MAXSRCSTATES MAXSRCNODES SOURCETRACK GLOBAL RULE 446%token MAXSRCCONN MAXSRCCONNRATE OVERLOAD FLUSH SLOPPY PFLOW 447%token TAGGED TAG IFBOUND FLOATING STATEPOLICY ROUTE SETTOS 448%token DIVERTTO DIVERTREPLY 449%token <v.string> STRING 450%token <v.number> NUMBER 451%token <v.i> PORTBINARY 452%type <v.interface> interface if_list if_item_not if_item 453%type <v.number> number icmptype icmp6type uid gid 454%type <v.number> tos not yesno 455%type <v.probability> probability 456%type <v.i> no dir af fragcache optimizer 457%type <v.i> sourcetrack flush unaryop statelock 458%type <v.b> action nataction natpasslog scrubaction 459%type <v.b> flags flag blockspec 460%type <v.range> portplain portstar portrange 461%type <v.hashkey> hashkey 462%type <v.proto> proto proto_list proto_item 463%type <v.number> protoval 464%type <v.icmp> icmpspec 465%type <v.icmp> icmp_list icmp_item 466%type <v.icmp> icmp6_list icmp6_item 467%type <v.number> reticmpspec reticmp6spec 468%type <v.fromto> fromto 469%type <v.peer> ipportspec from to 470%type <v.host> ipspec toipspec xhost host dynaddr host_list 471%type <v.host> redir_host_list redirspec 472%type <v.host> route_host route_host_list routespec 473%type <v.os> os xos os_list 474%type <v.port> portspec port_list port_item 475%type <v.uid> uids uid_list uid_item 476%type <v.gid> gids gid_list gid_item 477%type <v.route> route 478%type <v.redirection> redirection redirpool 479%type <v.string> label stringall tag anchorname 480%type <v.string> string varstring numberstring 481%type <v.keep_state> keep 482%type <v.state_opt> state_opt_spec state_opt_list state_opt_item 483%type <v.logquick> logquick quick log logopts logopt 484%type <v.interface> antispoof_ifspc antispoof_iflst antispoof_if 485%type <v.qassign> qname 486%type <v.queue> qassign qassign_list qassign_item 487%type <v.queue_options> scheduler 488%type <v.number> cbqflags_list cbqflags_item 489%type <v.number> priqflags_list priqflags_item 490%type <v.hfsc_opts> hfscopts_list hfscopts_item hfsc_opts 491%type <v.queue_bwspec> bandwidth 492%type <v.filter_opts> filter_opts filter_opt filter_opts_l 493%type <v.antispoof_opts> antispoof_opts antispoof_opt antispoof_opts_l 494%type <v.queue_opts> queue_opts queue_opt queue_opts_l 495%type <v.scrub_opts> scrub_opts scrub_opt scrub_opts_l 496%type <v.table_opts> table_opts table_opt table_opts_l 497%type <v.pool_opts> pool_opts pool_opt pool_opts_l 498%type <v.tagged> tagged 499%type <v.rtableid> rtable 500%% 501 502ruleset : /* empty */ 503 | ruleset include '\n' 504 | ruleset '\n' 505 | ruleset option '\n' 506 | ruleset scrubrule '\n' 507 | ruleset natrule '\n' 508 | ruleset binatrule '\n' 509 | ruleset pfrule '\n' 510 | ruleset anchorrule '\n' 511 | ruleset loadrule '\n' 512 | ruleset altqif '\n' 513 | ruleset queuespec '\n' 514 | ruleset varset '\n' 515 | ruleset antispoof '\n' 516 | ruleset tabledef '\n' 517 | '{' fakeanchor '}' '\n'; 518 | ruleset error '\n' { file->errors++; } 519 ; 520 521include : INCLUDE STRING { 522 struct file *nfile; 523 524 if ((nfile = pushfile($2, 0)) == NULL) { 525 yyerror("failed to include file %s", $2); 526 free($2); 527 YYERROR; 528 } 529 free($2); 530 531 file = nfile; 532 lungetc('\n'); 533 } 534 ; 535 536/* 537 * apply to previouslys specified rule: must be careful to note 538 * what that is: pf or nat or binat or rdr 539 */ 540fakeanchor : fakeanchor '\n' 541 | fakeanchor anchorrule '\n' 542 | fakeanchor binatrule '\n' 543 | fakeanchor natrule '\n' 544 | fakeanchor pfrule '\n' 545 | fakeanchor error '\n' 546 ; 547 548optimizer : string { 549 if (!strcmp($1, "none")) 550 $$ = 0; 551 else if (!strcmp($1, "basic")) 552 $$ = PF_OPTIMIZE_BASIC; 553 else if (!strcmp($1, "profile")) 554 $$ = PF_OPTIMIZE_BASIC | PF_OPTIMIZE_PROFILE; 555 else { 556 yyerror("unknown ruleset-optimization %s", $1); 557 YYERROR; 558 } 559 } 560 ; 561 562option : SET OPTIMIZATION STRING { 563 if (check_rulestate(PFCTL_STATE_OPTION)) { 564 free($3); 565 YYERROR; 566 } 567 if (pfctl_set_optimization(pf, $3) != 0) { 568 yyerror("unknown optimization %s", $3); 569 free($3); 570 YYERROR; 571 } 572 free($3); 573 } 574 | SET RULESET_OPTIMIZATION optimizer { 575 if (!(pf->opts & PF_OPT_OPTIMIZE)) { 576 pf->opts |= PF_OPT_OPTIMIZE; 577 pf->optimize = $3; 578 } 579 } 580 | SET TIMEOUT timeout_spec 581 | SET TIMEOUT '{' optnl timeout_list '}' 582 | SET LIMIT limit_spec 583 | SET LIMIT '{' optnl limit_list '}' 584 | SET LOGINTERFACE stringall { 585 if (check_rulestate(PFCTL_STATE_OPTION)) { 586 free($3); 587 YYERROR; 588 } 589 if (pfctl_set_logif(pf, $3) != 0) { 590 yyerror("error setting loginterface %s", $3); 591 free($3); 592 YYERROR; 593 } 594 free($3); 595 } 596 | SET HOSTID number { 597 if ($3 == 0 || $3 > UINT_MAX) { 598 yyerror("hostid must be non-zero"); 599 YYERROR; 600 } 601 if (pfctl_set_hostid(pf, $3) != 0) { 602 yyerror("error setting hostid %08x", $3); 603 YYERROR; 604 } 605 } 606 | SET BLOCKPOLICY DROP { 607 if (pf->opts & PF_OPT_VERBOSE) 608 printf("set block-policy drop\n"); 609 if (check_rulestate(PFCTL_STATE_OPTION)) 610 YYERROR; 611 blockpolicy = PFRULE_DROP; 612 } 613 | SET BLOCKPOLICY RETURN { 614 if (pf->opts & PF_OPT_VERBOSE) 615 printf("set block-policy return\n"); 616 if (check_rulestate(PFCTL_STATE_OPTION)) 617 YYERROR; 618 blockpolicy = PFRULE_RETURN; 619 } 620 | SET REQUIREORDER yesno { 621 if (pf->opts & PF_OPT_VERBOSE) 622 printf("set require-order %s\n", 623 $3 == 1 ? "yes" : "no"); 624 require_order = $3; 625 } 626 | SET FINGERPRINTS STRING { 627 if (pf->opts & PF_OPT_VERBOSE) 628 printf("set fingerprints \"%s\"\n", $3); 629 if (check_rulestate(PFCTL_STATE_OPTION)) { 630 free($3); 631 YYERROR; 632 } 633 if (!pf->anchor->name[0]) { 634 if (pfctl_file_fingerprints(pf->dev, 635 pf->opts, $3)) { 636 yyerror("error loading " 637 "fingerprints %s", $3); 638 free($3); 639 YYERROR; 640 } 641 } 642 free($3); 643 } 644 | SET STATEPOLICY statelock { 645 if (pf->opts & PF_OPT_VERBOSE) 646 switch ($3) { 647 case 0: 648 printf("set state-policy floating\n"); 649 break; 650 case PFRULE_IFBOUND: 651 printf("set state-policy if-bound\n"); 652 break; 653 } 654 default_statelock = $3; 655 } 656 | SET DEBUG STRING { 657 if (check_rulestate(PFCTL_STATE_OPTION)) { 658 free($3); 659 YYERROR; 660 } 661 if (pfctl_set_debug(pf, $3) != 0) { 662 yyerror("error setting debuglevel %s", $3); 663 free($3); 664 YYERROR; 665 } 666 free($3); 667 } 668 | SET SKIP interface { 669 if (expand_skip_interface($3) != 0) { 670 yyerror("error setting skip interface(s)"); 671 YYERROR; 672 } 673 } 674 ; 675 676stringall : STRING { $$ = $1; } 677 | ALL { 678 if (($$ = strdup("all")) == NULL) { 679 err(1, "stringall: strdup"); 680 } 681 } 682 ; 683 684string : STRING string { 685 if (asprintf(&$$, "%s %s", $1, $2) == -1) 686 err(1, "string: asprintf"); 687 free($1); 688 free($2); 689 } 690 | STRING 691 ; 692 693varstring : numberstring varstring { 694 if (asprintf(&$$, "%s %s", $1, $2) == -1) 695 err(1, "string: asprintf"); 696 free($1); 697 free($2); 698 } 699 | numberstring 700 ; 701 702numberstring : NUMBER { 703 char *s; 704 if (asprintf(&s, "%lld", $1) == -1) { 705 yyerror("string: asprintf"); 706 YYERROR; 707 } 708 $$ = s; 709 } 710 | STRING 711 ; 712 713varset : STRING '=' varstring { 714 if (pf->opts & PF_OPT_VERBOSE) 715 printf("%s = \"%s\"\n", $1, $3); 716 if (symset($1, $3, 0) == -1) 717 err(1, "cannot store variable %s", $1); 718 free($1); 719 free($3); 720 } 721 ; 722 723anchorname : STRING { $$ = $1; } 724 | /* empty */ { $$ = NULL; } 725 ; 726 727pfa_anchorlist : /* empty */ 728 | pfa_anchorlist '\n' 729 | pfa_anchorlist pfrule '\n' 730 | pfa_anchorlist anchorrule '\n' 731 ; 732 733pfa_anchor : '{' 734 { 735 char ta[PF_ANCHOR_NAME_SIZE]; 736 struct pf_ruleset *rs; 737 738 /* steping into a brace anchor */ 739 pf->asd++; 740 pf->bn++; 741 pf->brace = 1; 742 743 /* create a holding ruleset in the root */ 744 snprintf(ta, PF_ANCHOR_NAME_SIZE, "_%d", pf->bn); 745 rs = pf_find_or_create_ruleset(ta); 746 if (rs == NULL) 747 err(1, "pfa_anchor: pf_find_or_create_ruleset"); 748 pf->astack[pf->asd] = rs->anchor; 749 pf->anchor = rs->anchor; 750 } '\n' pfa_anchorlist '}' 751 { 752 pf->alast = pf->anchor; 753 pf->asd--; 754 pf->anchor = pf->astack[pf->asd]; 755 } 756 | /* empty */ 757 ; 758 759anchorrule : ANCHOR anchorname dir quick interface af proto fromto 760 filter_opts pfa_anchor 761 { 762 struct pf_rule r; 763 struct node_proto *proto; 764 765 if (check_rulestate(PFCTL_STATE_FILTER)) { 766 if ($2) 767 free($2); 768 YYERROR; 769 } 770 771 if ($2 && ($2[0] == '_' || strstr($2, "/_") != NULL)) { 772 free($2); 773 yyerror("anchor names beginning with '_' " 774 "are reserved for internal use"); 775 YYERROR; 776 } 777 778 memset(&r, 0, sizeof(r)); 779 if (pf->astack[pf->asd + 1]) { 780 /* move inline rules into relative location */ 781 pf_anchor_setup(&r, 782 &pf->astack[pf->asd]->ruleset, 783 $2 ? $2 : pf->alast->name); 784 785 if (r.anchor == NULL) 786 err(1, "anchorrule: unable to " 787 "create ruleset"); 788 789 if (pf->alast != r.anchor) { 790 if (r.anchor->match) { 791 yyerror("inline anchor '%s' " 792 "already exists", 793 r.anchor->name); 794 YYERROR; 795 } 796 mv_rules(&pf->alast->ruleset, 797 &r.anchor->ruleset); 798 } 799 pf_remove_if_empty_ruleset(&pf->alast->ruleset); 800 pf->alast = r.anchor; 801 } else { 802 if (!$2) { 803 yyerror("anchors without explicit " 804 "rules must specify a name"); 805 YYERROR; 806 } 807 } 808 r.direction = $3; 809 r.quick = $4.quick; 810 r.af = $6; 811 r.prob = $9.prob; 812 r.rtableid = $9.rtableid; 813 814 if ($9.tag) 815 if (strlcpy(r.tagname, $9.tag, 816 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) { 817 yyerror("tag too long, max %u chars", 818 PF_TAG_NAME_SIZE - 1); 819 YYERROR; 820 } 821 if ($9.match_tag) 822 if (strlcpy(r.match_tagname, $9.match_tag, 823 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) { 824 yyerror("tag too long, max %u chars", 825 PF_TAG_NAME_SIZE - 1); 826 YYERROR; 827 } 828 r.match_tag_not = $9.match_tag_not; 829 if (rule_label(&r, $9.label)) 830 YYERROR; 831 free($9.label); 832 r.flags = $9.flags.b1; 833 r.flagset = $9.flags.b2; 834 if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) { 835 yyerror("flags always false"); 836 YYERROR; 837 } 838 if ($9.flags.b1 || $9.flags.b2 || $8.src_os) { 839 for (proto = $7; proto != NULL && 840 proto->proto != IPPROTO_TCP; 841 proto = proto->next) 842 ; /* nothing */ 843 if (proto == NULL && $7 != NULL) { 844 if ($9.flags.b1 || $9.flags.b2) 845 yyerror( 846 "flags only apply to tcp"); 847 if ($8.src_os) 848 yyerror( 849 "OS fingerprinting only " 850 "applies to tcp"); 851 YYERROR; 852 } 853 } 854 855 r.tos = $9.tos; 856 857 if ($9.keep.action) { 858 yyerror("cannot specify state handling " 859 "on anchors"); 860 YYERROR; 861 } 862 863 if ($9.match_tag) 864 if (strlcpy(r.match_tagname, $9.match_tag, 865 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) { 866 yyerror("tag too long, max %u chars", 867 PF_TAG_NAME_SIZE - 1); 868 YYERROR; 869 } 870 r.match_tag_not = $9.match_tag_not; 871 872 decide_address_family($8.src.host, &r.af); 873 decide_address_family($8.dst.host, &r.af); 874 875 expand_rule(&r, $5, NULL, $7, $8.src_os, 876 $8.src.host, $8.src.port, $8.dst.host, $8.dst.port, 877 $9.uid, $9.gid, $9.icmpspec, 878 pf->astack[pf->asd + 1] ? pf->alast->name : $2); 879 free($2); 880 pf->astack[pf->asd + 1] = NULL; 881 } 882 | NATANCHOR string interface af proto fromto rtable { 883 struct pf_rule r; 884 885 if (check_rulestate(PFCTL_STATE_NAT)) { 886 free($2); 887 YYERROR; 888 } 889 890 memset(&r, 0, sizeof(r)); 891 r.action = PF_NAT; 892 r.af = $4; 893 r.rtableid = $7; 894 895 decide_address_family($6.src.host, &r.af); 896 decide_address_family($6.dst.host, &r.af); 897 898 expand_rule(&r, $3, NULL, $5, $6.src_os, 899 $6.src.host, $6.src.port, $6.dst.host, $6.dst.port, 900 0, 0, 0, $2); 901 free($2); 902 } 903 | RDRANCHOR string interface af proto fromto rtable { 904 struct pf_rule r; 905 906 if (check_rulestate(PFCTL_STATE_NAT)) { 907 free($2); 908 YYERROR; 909 } 910 911 memset(&r, 0, sizeof(r)); 912 r.action = PF_RDR; 913 r.af = $4; 914 r.rtableid = $7; 915 916 decide_address_family($6.src.host, &r.af); 917 decide_address_family($6.dst.host, &r.af); 918 919 if ($6.src.port != NULL) { 920 yyerror("source port parameter not supported" 921 " in rdr-anchor"); 922 YYERROR; 923 } 924 if ($6.dst.port != NULL) { 925 if ($6.dst.port->next != NULL) { 926 yyerror("destination port list " 927 "expansion not supported in " 928 "rdr-anchor"); 929 YYERROR; 930 } else if ($6.dst.port->op != PF_OP_EQ) { 931 yyerror("destination port operators" 932 " not supported in rdr-anchor"); 933 YYERROR; 934 } 935 r.dst.port[0] = $6.dst.port->port[0]; 936 r.dst.port[1] = $6.dst.port->port[1]; 937 r.dst.port_op = $6.dst.port->op; 938 } 939 940 expand_rule(&r, $3, NULL, $5, $6.src_os, 941 $6.src.host, $6.src.port, $6.dst.host, $6.dst.port, 942 0, 0, 0, $2); 943 free($2); 944 } 945 | BINATANCHOR string interface af proto fromto rtable { 946 struct pf_rule r; 947 948 if (check_rulestate(PFCTL_STATE_NAT)) { 949 free($2); 950 YYERROR; 951 } 952 953 memset(&r, 0, sizeof(r)); 954 r.action = PF_BINAT; 955 r.af = $4; 956 r.rtableid = $7; 957 if ($5 != NULL) { 958 if ($5->next != NULL) { 959 yyerror("proto list expansion" 960 " not supported in binat-anchor"); 961 YYERROR; 962 } 963 r.proto = $5->proto; 964 free($5); 965 } 966 967 if ($6.src.host != NULL || $6.src.port != NULL || 968 $6.dst.host != NULL || $6.dst.port != NULL) { 969 yyerror("fromto parameter not supported" 970 " in binat-anchor"); 971 YYERROR; 972 } 973 974 decide_address_family($6.src.host, &r.af); 975 decide_address_family($6.dst.host, &r.af); 976 977 pfctl_add_rule(pf, &r, $2); 978 free($2); 979 } 980 ; 981 982loadrule : LOAD ANCHOR string FROM string { 983 struct loadanchors *loadanchor; 984 985 if (strlen(pf->anchor->name) + 1 + 986 strlen($3) >= MAXPATHLEN) { 987 yyerror("anchorname %s too long, max %u\n", 988 $3, MAXPATHLEN - 1); 989 free($3); 990 YYERROR; 991 } 992 loadanchor = calloc(1, sizeof(struct loadanchors)); 993 if (loadanchor == NULL) 994 err(1, "loadrule: calloc"); 995 if ((loadanchor->anchorname = malloc(MAXPATHLEN)) == 996 NULL) 997 err(1, "loadrule: malloc"); 998 if (pf->anchor->name[0]) 999 snprintf(loadanchor->anchorname, MAXPATHLEN, 1000 "%s/%s", pf->anchor->name, $3); 1001 else 1002 strlcpy(loadanchor->anchorname, $3, MAXPATHLEN); 1003 if ((loadanchor->filename = strdup($5)) == NULL) 1004 err(1, "loadrule: strdup"); 1005 1006 TAILQ_INSERT_TAIL(&loadanchorshead, loadanchor, 1007 entries); 1008 1009 free($3); 1010 free($5); 1011 }; 1012 1013scrubaction : no SCRUB { 1014 $$.b2 = $$.w = 0; 1015 if ($1) 1016 $$.b1 = PF_NOSCRUB; 1017 else 1018 $$.b1 = PF_SCRUB; 1019 } 1020 ; 1021 1022scrubrule : scrubaction dir logquick interface af proto fromto scrub_opts 1023 { 1024 struct pf_rule r; 1025 1026 if (check_rulestate(PFCTL_STATE_SCRUB)) 1027 YYERROR; 1028 1029 memset(&r, 0, sizeof(r)); 1030 1031 r.action = $1.b1; 1032 r.direction = $2; 1033 1034 r.log = $3.log; 1035 r.logif = $3.logif; 1036 if ($3.quick) { 1037 yyerror("scrub rules do not support 'quick'"); 1038 YYERROR; 1039 } 1040 1041 r.af = $5; 1042 if ($8.nodf) 1043 r.rule_flag |= PFRULE_NODF; 1044 if ($8.randomid) 1045 r.rule_flag |= PFRULE_RANDOMID; 1046 if ($8.reassemble_tcp) { 1047 if (r.direction != PF_INOUT) { 1048 yyerror("reassemble tcp rules can not " 1049 "specify direction"); 1050 YYERROR; 1051 } 1052 r.rule_flag |= PFRULE_REASSEMBLE_TCP; 1053 } 1054 if ($8.minttl) 1055 r.min_ttl = $8.minttl; 1056 if ($8.maxmss) 1057 r.max_mss = $8.maxmss; 1058 if ($8.marker & SOM_SETTOS) { 1059 r.rule_flag |= PFRULE_SET_TOS; 1060 r.set_tos = $8.settos; 1061 } 1062 if ($8.fragcache) 1063 r.rule_flag |= $8.fragcache; 1064 if ($8.match_tag) 1065 if (strlcpy(r.match_tagname, $8.match_tag, 1066 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) { 1067 yyerror("tag too long, max %u chars", 1068 PF_TAG_NAME_SIZE - 1); 1069 YYERROR; 1070 } 1071 r.match_tag_not = $8.match_tag_not; 1072 r.rtableid = $8.rtableid; 1073 1074 expand_rule(&r, $4, NULL, $6, $7.src_os, 1075 $7.src.host, $7.src.port, $7.dst.host, $7.dst.port, 1076 NULL, NULL, NULL, ""); 1077 } 1078 ; 1079 1080scrub_opts : { 1081 bzero(&scrub_opts, sizeof scrub_opts); 1082 scrub_opts.rtableid = -1; 1083 } 1084 scrub_opts_l 1085 { $$ = scrub_opts; } 1086 | /* empty */ { 1087 bzero(&scrub_opts, sizeof scrub_opts); 1088 scrub_opts.rtableid = -1; 1089 $$ = scrub_opts; 1090 } 1091 ; 1092 1093scrub_opts_l : scrub_opts_l scrub_opt 1094 | scrub_opt 1095 ; 1096 1097scrub_opt : NODF { 1098 if (scrub_opts.nodf) { 1099 yyerror("no-df cannot be respecified"); 1100 YYERROR; 1101 } 1102 scrub_opts.nodf = 1; 1103 } 1104 | MINTTL NUMBER { 1105 if (scrub_opts.marker & SOM_MINTTL) { 1106 yyerror("min-ttl cannot be respecified"); 1107 YYERROR; 1108 } 1109 if ($2 < 0 || $2 > 255) { 1110 yyerror("illegal min-ttl value %d", $2); 1111 YYERROR; 1112 } 1113 scrub_opts.marker |= SOM_MINTTL; 1114 scrub_opts.minttl = $2; 1115 } 1116 | MAXMSS NUMBER { 1117 if (scrub_opts.marker & SOM_MAXMSS) { 1118 yyerror("max-mss cannot be respecified"); 1119 YYERROR; 1120 } 1121 if ($2 < 0 || $2 > 65535) { 1122 yyerror("illegal max-mss value %d", $2); 1123 YYERROR; 1124 } 1125 scrub_opts.marker |= SOM_MAXMSS; 1126 scrub_opts.maxmss = $2; 1127 } 1128 | SETTOS tos { 1129 if (scrub_opts.marker & SOM_SETTOS) { 1130 yyerror("set-tos cannot be respecified"); 1131 YYERROR; 1132 } 1133 scrub_opts.marker |= SOM_SETTOS; 1134 scrub_opts.settos = $2; 1135 } 1136 | fragcache { 1137 if (scrub_opts.marker & SOM_FRAGCACHE) { 1138 yyerror("fragcache cannot be respecified"); 1139 YYERROR; 1140 } 1141 scrub_opts.marker |= SOM_FRAGCACHE; 1142 scrub_opts.fragcache = $1; 1143 } 1144 | REASSEMBLE STRING { 1145 if (strcasecmp($2, "tcp") != 0) { 1146 yyerror("scrub reassemble supports only tcp, " 1147 "not '%s'", $2); 1148 free($2); 1149 YYERROR; 1150 } 1151 free($2); 1152 if (scrub_opts.reassemble_tcp) { 1153 yyerror("reassemble tcp cannot be respecified"); 1154 YYERROR; 1155 } 1156 scrub_opts.reassemble_tcp = 1; 1157 } 1158 | RANDOMID { 1159 if (scrub_opts.randomid) { 1160 yyerror("random-id cannot be respecified"); 1161 YYERROR; 1162 } 1163 scrub_opts.randomid = 1; 1164 } 1165 | RTABLE NUMBER { 1166 if ($2 < 0 || $2 > RT_TABLEID_MAX) { 1167 yyerror("invalid rtable id"); 1168 YYERROR; 1169 } 1170 scrub_opts.rtableid = $2; 1171 } 1172 | not TAGGED string { 1173 scrub_opts.match_tag = $3; 1174 scrub_opts.match_tag_not = $1; 1175 } 1176 ; 1177 1178fragcache : FRAGMENT REASSEMBLE { $$ = 0; /* default */ } 1179 | FRAGMENT FRAGCROP { $$ = PFRULE_FRAGCROP; } 1180 | FRAGMENT FRAGDROP { $$ = PFRULE_FRAGDROP; } 1181 ; 1182 1183antispoof : ANTISPOOF logquick antispoof_ifspc af antispoof_opts { 1184 struct pf_rule r; 1185 struct node_host *h = NULL, *hh; 1186 struct node_if *i, *j; 1187 1188 if (check_rulestate(PFCTL_STATE_FILTER)) 1189 YYERROR; 1190 1191 for (i = $3; i; i = i->next) { 1192 bzero(&r, sizeof(r)); 1193 1194 r.action = PF_DROP; 1195 r.direction = PF_IN; 1196 r.log = $2.log; 1197 r.logif = $2.logif; 1198 r.quick = $2.quick; 1199 r.af = $4; 1200 if (rule_label(&r, $5.label)) 1201 YYERROR; 1202 r.rtableid = $5.rtableid; 1203 j = calloc(1, sizeof(struct node_if)); 1204 if (j == NULL) 1205 err(1, "antispoof: calloc"); 1206 if (strlcpy(j->ifname, i->ifname, 1207 sizeof(j->ifname)) >= sizeof(j->ifname)) { 1208 free(j); 1209 yyerror("interface name too long"); 1210 YYERROR; 1211 } 1212 j->not = 1; 1213 if (i->dynamic) { 1214 h = calloc(1, sizeof(*h)); 1215 if (h == NULL) 1216 err(1, "address: calloc"); 1217 h->addr.type = PF_ADDR_DYNIFTL; 1218 set_ipmask(h, 128); 1219 if (strlcpy(h->addr.v.ifname, i->ifname, 1220 sizeof(h->addr.v.ifname)) >= 1221 sizeof(h->addr.v.ifname)) { 1222 free(h); 1223 yyerror( 1224 "interface name too long"); 1225 YYERROR; 1226 } 1227 hh = malloc(sizeof(*hh)); 1228 if (hh == NULL) 1229 err(1, "address: malloc"); 1230 bcopy(h, hh, sizeof(*hh)); 1231 h->addr.iflags = PFI_AFLAG_NETWORK; 1232 } else { 1233 h = ifa_lookup(j->ifname, 1234 PFI_AFLAG_NETWORK); 1235 hh = NULL; 1236 } 1237 1238 if (h != NULL) 1239 expand_rule(&r, j, NULL, NULL, NULL, h, 1240 NULL, NULL, NULL, NULL, NULL, 1241 NULL, ""); 1242 1243 if ((i->ifa_flags & IFF_LOOPBACK) == 0) { 1244 bzero(&r, sizeof(r)); 1245 1246 r.action = PF_DROP; 1247 r.direction = PF_IN; 1248 r.log = $2.log; 1249 r.logif = $2.logif; 1250 r.quick = $2.quick; 1251 r.af = $4; 1252 if (rule_label(&r, $5.label)) 1253 YYERROR; 1254 r.rtableid = $5.rtableid; 1255 if (hh != NULL) 1256 h = hh; 1257 else 1258 h = ifa_lookup(i->ifname, 0); 1259 if (h != NULL) 1260 expand_rule(&r, NULL, NULL, 1261 NULL, NULL, h, NULL, NULL, 1262 NULL, NULL, NULL, NULL, ""); 1263 } else 1264 free(hh); 1265 } 1266 free($5.label); 1267 } 1268 ; 1269 1270antispoof_ifspc : FOR antispoof_if { $$ = $2; } 1271 | FOR '{' optnl antispoof_iflst '}' { $$ = $4; } 1272 ; 1273 1274antispoof_iflst : antispoof_if optnl { $$ = $1; } 1275 | antispoof_iflst comma antispoof_if optnl { 1276 $1->tail->next = $3; 1277 $1->tail = $3; 1278 $$ = $1; 1279 } 1280 ; 1281 1282antispoof_if : if_item { $$ = $1; } 1283 | '(' if_item ')' { 1284 $2->dynamic = 1; 1285 $$ = $2; 1286 } 1287 ; 1288 1289antispoof_opts : { 1290 bzero(&antispoof_opts, sizeof antispoof_opts); 1291 antispoof_opts.rtableid = -1; 1292 } 1293 antispoof_opts_l 1294 { $$ = antispoof_opts; } 1295 | /* empty */ { 1296 bzero(&antispoof_opts, sizeof antispoof_opts); 1297 antispoof_opts.rtableid = -1; 1298 $$ = antispoof_opts; 1299 } 1300 ; 1301 1302antispoof_opts_l : antispoof_opts_l antispoof_opt 1303 | antispoof_opt 1304 ; 1305 1306antispoof_opt : label { 1307 if (antispoof_opts.label) { 1308 yyerror("label cannot be redefined"); 1309 YYERROR; 1310 } 1311 antispoof_opts.label = $1; 1312 } 1313 | RTABLE NUMBER { 1314 if ($2 < 0 || $2 > RT_TABLEID_MAX) { 1315 yyerror("invalid rtable id"); 1316 YYERROR; 1317 } 1318 antispoof_opts.rtableid = $2; 1319 } 1320 ; 1321 1322not : '!' { $$ = 1; } 1323 | /* empty */ { $$ = 0; } 1324 ; 1325 1326tabledef : TABLE '<' STRING '>' table_opts { 1327 struct node_host *h, *nh; 1328 struct node_tinit *ti, *nti; 1329 1330 if (strlen($3) >= PF_TABLE_NAME_SIZE) { 1331 yyerror("table name too long, max %d chars", 1332 PF_TABLE_NAME_SIZE - 1); 1333 free($3); 1334 YYERROR; 1335 } 1336 if (pf->loadopt & PFCTL_FLAG_TABLE) 1337 if (process_tabledef($3, &$5)) { 1338 free($3); 1339 YYERROR; 1340 } 1341 free($3); 1342 for (ti = SIMPLEQ_FIRST(&$5.init_nodes); 1343 ti != SIMPLEQ_END(&$5.init_nodes); ti = nti) { 1344 if (ti->file) 1345 free(ti->file); 1346 for (h = ti->host; h != NULL; h = nh) { 1347 nh = h->next; 1348 free(h); 1349 } 1350 nti = SIMPLEQ_NEXT(ti, entries); 1351 free(ti); 1352 } 1353 } 1354 ; 1355 1356table_opts : { 1357 bzero(&table_opts, sizeof table_opts); 1358 SIMPLEQ_INIT(&table_opts.init_nodes); 1359 } 1360 table_opts_l 1361 { $$ = table_opts; } 1362 | /* empty */ 1363 { 1364 bzero(&table_opts, sizeof table_opts); 1365 SIMPLEQ_INIT(&table_opts.init_nodes); 1366 $$ = table_opts; 1367 } 1368 ; 1369 1370table_opts_l : table_opts_l table_opt 1371 | table_opt 1372 ; 1373 1374table_opt : STRING { 1375 if (!strcmp($1, "const")) 1376 table_opts.flags |= PFR_TFLAG_CONST; 1377 else if (!strcmp($1, "persist")) 1378 table_opts.flags |= PFR_TFLAG_PERSIST; 1379 else if (!strcmp($1, "counters")) 1380 table_opts.flags |= PFR_TFLAG_COUNTERS; 1381 else { 1382 yyerror("invalid table option '%s'", $1); 1383 free($1); 1384 YYERROR; 1385 } 1386 free($1); 1387 } 1388 | '{' optnl '}' { table_opts.init_addr = 1; } 1389 | '{' optnl host_list '}' { 1390 struct node_host *n; 1391 struct node_tinit *ti; 1392 1393 for (n = $3; n != NULL; n = n->next) { 1394 switch (n->addr.type) { 1395 case PF_ADDR_ADDRMASK: 1396 continue; /* ok */ 1397 case PF_ADDR_RANGE: 1398 yyerror("address ranges are not " 1399 "permitted inside tables"); 1400 break; 1401 case PF_ADDR_DYNIFTL: 1402 yyerror("dynamic addresses are not " 1403 "permitted inside tables"); 1404 break; 1405 case PF_ADDR_TABLE: 1406 yyerror("tables cannot contain tables"); 1407 break; 1408 case PF_ADDR_NOROUTE: 1409 yyerror("\"no-route\" is not permitted " 1410 "inside tables"); 1411 break; 1412 case PF_ADDR_URPFFAILED: 1413 yyerror("\"urpf-failed\" is not " 1414 "permitted inside tables"); 1415 break; 1416 default: 1417 yyerror("unknown address type %d", 1418 n->addr.type); 1419 } 1420 YYERROR; 1421 } 1422 if (!(ti = calloc(1, sizeof(*ti)))) 1423 err(1, "table_opt: calloc"); 1424 ti->host = $3; 1425 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti, 1426 entries); 1427 table_opts.init_addr = 1; 1428 } 1429 | FILENAME STRING { 1430 struct node_tinit *ti; 1431 1432 if (!(ti = calloc(1, sizeof(*ti)))) 1433 err(1, "table_opt: calloc"); 1434 ti->file = $2; 1435 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti, 1436 entries); 1437 table_opts.init_addr = 1; 1438 } 1439 ; 1440 1441altqif : ALTQ interface queue_opts QUEUE qassign { 1442 struct pf_altq a; 1443 1444 if (check_rulestate(PFCTL_STATE_QUEUE)) 1445 YYERROR; 1446 1447 memset(&a, 0, sizeof(a)); 1448 if ($3.scheduler.qtype == ALTQT_NONE) { 1449 yyerror("no scheduler specified!"); 1450 YYERROR; 1451 } 1452 a.scheduler = $3.scheduler.qtype; 1453 a.qlimit = $3.qlimit; 1454 a.tbrsize = $3.tbrsize; 1455 if ($5 == NULL) { 1456 yyerror("no child queues specified"); 1457 YYERROR; 1458 } 1459 if (expand_altq(&a, $2, $5, $3.queue_bwspec, 1460 &$3.scheduler)) 1461 YYERROR; 1462 } 1463 ; 1464 1465queuespec : QUEUE STRING interface queue_opts qassign { 1466 struct pf_altq a; 1467 1468 if (check_rulestate(PFCTL_STATE_QUEUE)) { 1469 free($2); 1470 YYERROR; 1471 } 1472 1473 memset(&a, 0, sizeof(a)); 1474 1475 if (strlcpy(a.qname, $2, sizeof(a.qname)) >= 1476 sizeof(a.qname)) { 1477 yyerror("queue name too long (max " 1478 "%d chars)", PF_QNAME_SIZE-1); 1479 free($2); 1480 YYERROR; 1481 } 1482 free($2); 1483 if ($4.tbrsize) { 1484 yyerror("cannot specify tbrsize for queue"); 1485 YYERROR; 1486 } 1487 if ($4.priority > 255) { 1488 yyerror("priority out of range: max 255"); 1489 YYERROR; 1490 } 1491 a.priority = $4.priority; 1492 a.qlimit = $4.qlimit; 1493 a.scheduler = $4.scheduler.qtype; 1494 if (expand_queue(&a, $3, $5, $4.queue_bwspec, 1495 &$4.scheduler)) { 1496 yyerror("errors in queue definition"); 1497 YYERROR; 1498 } 1499 } 1500 ; 1501 1502queue_opts : { 1503 bzero(&queue_opts, sizeof queue_opts); 1504 queue_opts.priority = DEFAULT_PRIORITY; 1505 queue_opts.qlimit = DEFAULT_QLIMIT; 1506 queue_opts.scheduler.qtype = ALTQT_NONE; 1507 queue_opts.queue_bwspec.bw_percent = 100; 1508 } 1509 queue_opts_l 1510 { $$ = queue_opts; } 1511 | /* empty */ { 1512 bzero(&queue_opts, sizeof queue_opts); 1513 queue_opts.priority = DEFAULT_PRIORITY; 1514 queue_opts.qlimit = DEFAULT_QLIMIT; 1515 queue_opts.scheduler.qtype = ALTQT_NONE; 1516 queue_opts.queue_bwspec.bw_percent = 100; 1517 $$ = queue_opts; 1518 } 1519 ; 1520 1521queue_opts_l : queue_opts_l queue_opt 1522 | queue_opt 1523 ; 1524 1525queue_opt : BANDWIDTH bandwidth { 1526 if (queue_opts.marker & QOM_BWSPEC) { 1527 yyerror("bandwidth cannot be respecified"); 1528 YYERROR; 1529 } 1530 queue_opts.marker |= QOM_BWSPEC; 1531 queue_opts.queue_bwspec = $2; 1532 } 1533 | PRIORITY NUMBER { 1534 if (queue_opts.marker & QOM_PRIORITY) { 1535 yyerror("priority cannot be respecified"); 1536 YYERROR; 1537 } 1538 if ($2 < 0 || $2 > 255) { 1539 yyerror("priority out of range: max 255"); 1540 YYERROR; 1541 } 1542 queue_opts.marker |= QOM_PRIORITY; 1543 queue_opts.priority = $2; 1544 } 1545 | QLIMIT NUMBER { 1546 if (queue_opts.marker & QOM_QLIMIT) { 1547 yyerror("qlimit cannot be respecified"); 1548 YYERROR; 1549 } 1550 if ($2 < 0 || $2 > 65535) { 1551 yyerror("qlimit out of range: max 65535"); 1552 YYERROR; 1553 } 1554 queue_opts.marker |= QOM_QLIMIT; 1555 queue_opts.qlimit = $2; 1556 } 1557 | scheduler { 1558 if (queue_opts.marker & QOM_SCHEDULER) { 1559 yyerror("scheduler cannot be respecified"); 1560 YYERROR; 1561 } 1562 queue_opts.marker |= QOM_SCHEDULER; 1563 queue_opts.scheduler = $1; 1564 } 1565 | TBRSIZE NUMBER { 1566 if (queue_opts.marker & QOM_TBRSIZE) { 1567 yyerror("tbrsize cannot be respecified"); 1568 YYERROR; 1569 } 1570 if ($2 < 0 || $2 > 65535) { 1571 yyerror("tbrsize too big: max 65535"); 1572 YYERROR; 1573 } 1574 queue_opts.marker |= QOM_TBRSIZE; 1575 queue_opts.tbrsize = $2; 1576 } 1577 ; 1578 1579bandwidth : STRING { 1580 double bps; 1581 char *cp; 1582 1583 $$.bw_percent = 0; 1584 1585 bps = strtod($1, &cp); 1586 if (cp != NULL) { 1587 if (!strcmp(cp, "b")) 1588 ; /* nothing */ 1589 else if (!strcmp(cp, "Kb")) 1590 bps *= 1000; 1591 else if (!strcmp(cp, "Mb")) 1592 bps *= 1000 * 1000; 1593 else if (!strcmp(cp, "Gb")) 1594 bps *= 1000 * 1000 * 1000; 1595 else if (!strcmp(cp, "%")) { 1596 if (bps < 0 || bps > 100) { 1597 yyerror("bandwidth spec " 1598 "out of range"); 1599 free($1); 1600 YYERROR; 1601 } 1602 $$.bw_percent = bps; 1603 bps = 0; 1604 } else { 1605 yyerror("unknown unit %s", cp); 1606 free($1); 1607 YYERROR; 1608 } 1609 } 1610 free($1); 1611 $$.bw_absolute = (u_int32_t)bps; 1612 } 1613 | NUMBER { 1614 if ($1 < 0 || $1 > UINT_MAX) { 1615 yyerror("bandwidth number too big"); 1616 YYERROR; 1617 } 1618 $$.bw_percent = 0; 1619 $$.bw_absolute = $1; 1620 } 1621 ; 1622 1623scheduler : CBQ { 1624 $$.qtype = ALTQT_CBQ; 1625 $$.data.cbq_opts.flags = 0; 1626 } 1627 | CBQ '(' cbqflags_list ')' { 1628 $$.qtype = ALTQT_CBQ; 1629 $$.data.cbq_opts.flags = $3; 1630 } 1631 | PRIQ { 1632 $$.qtype = ALTQT_PRIQ; 1633 $$.data.priq_opts.flags = 0; 1634 } 1635 | PRIQ '(' priqflags_list ')' { 1636 $$.qtype = ALTQT_PRIQ; 1637 $$.data.priq_opts.flags = $3; 1638 } 1639 | HFSC { 1640 $$.qtype = ALTQT_HFSC; 1641 bzero(&$$.data.hfsc_opts, 1642 sizeof(struct node_hfsc_opts)); 1643 } 1644 | HFSC '(' hfsc_opts ')' { 1645 $$.qtype = ALTQT_HFSC; 1646 $$.data.hfsc_opts = $3; 1647 } 1648 ; 1649 1650cbqflags_list : cbqflags_item { $$ |= $1; } 1651 | cbqflags_list comma cbqflags_item { $$ |= $3; } 1652 ; 1653 1654cbqflags_item : STRING { 1655 if (!strcmp($1, "default")) 1656 $$ = CBQCLF_DEFCLASS; 1657 else if (!strcmp($1, "borrow")) 1658 $$ = CBQCLF_BORROW; 1659 else if (!strcmp($1, "red")) 1660 $$ = CBQCLF_RED; 1661 else if (!strcmp($1, "ecn")) 1662 $$ = CBQCLF_RED|CBQCLF_ECN; 1663 else if (!strcmp($1, "rio")) 1664 $$ = CBQCLF_RIO; 1665 else { 1666 yyerror("unknown cbq flag \"%s\"", $1); 1667 free($1); 1668 YYERROR; 1669 } 1670 free($1); 1671 } 1672 ; 1673 1674priqflags_list : priqflags_item { $$ |= $1; } 1675 | priqflags_list comma priqflags_item { $$ |= $3; } 1676 ; 1677 1678priqflags_item : STRING { 1679 if (!strcmp($1, "default")) 1680 $$ = PRCF_DEFAULTCLASS; 1681 else if (!strcmp($1, "red")) 1682 $$ = PRCF_RED; 1683 else if (!strcmp($1, "ecn")) 1684 $$ = PRCF_RED|PRCF_ECN; 1685 else if (!strcmp($1, "rio")) 1686 $$ = PRCF_RIO; 1687 else { 1688 yyerror("unknown priq flag \"%s\"", $1); 1689 free($1); 1690 YYERROR; 1691 } 1692 free($1); 1693 } 1694 ; 1695 1696hfsc_opts : { 1697 bzero(&hfsc_opts, 1698 sizeof(struct node_hfsc_opts)); 1699 } 1700 hfscopts_list { 1701 $$ = hfsc_opts; 1702 } 1703 ; 1704 1705hfscopts_list : hfscopts_item 1706 | hfscopts_list comma hfscopts_item 1707 ; 1708 1709hfscopts_item : LINKSHARE bandwidth { 1710 if (hfsc_opts.linkshare.used) { 1711 yyerror("linkshare already specified"); 1712 YYERROR; 1713 } 1714 hfsc_opts.linkshare.m2 = $2; 1715 hfsc_opts.linkshare.used = 1; 1716 } 1717 | LINKSHARE '(' bandwidth comma NUMBER comma bandwidth ')' 1718 { 1719 if ($5 < 0 || $5 > INT_MAX) { 1720 yyerror("timing in curve out of range"); 1721 YYERROR; 1722 } 1723 if (hfsc_opts.linkshare.used) { 1724 yyerror("linkshare already specified"); 1725 YYERROR; 1726 } 1727 hfsc_opts.linkshare.m1 = $3; 1728 hfsc_opts.linkshare.d = $5; 1729 hfsc_opts.linkshare.m2 = $7; 1730 hfsc_opts.linkshare.used = 1; 1731 } 1732 | REALTIME bandwidth { 1733 if (hfsc_opts.realtime.used) { 1734 yyerror("realtime already specified"); 1735 YYERROR; 1736 } 1737 hfsc_opts.realtime.m2 = $2; 1738 hfsc_opts.realtime.used = 1; 1739 } 1740 | REALTIME '(' bandwidth comma NUMBER comma bandwidth ')' 1741 { 1742 if ($5 < 0 || $5 > INT_MAX) { 1743 yyerror("timing in curve out of range"); 1744 YYERROR; 1745 } 1746 if (hfsc_opts.realtime.used) { 1747 yyerror("realtime already specified"); 1748 YYERROR; 1749 } 1750 hfsc_opts.realtime.m1 = $3; 1751 hfsc_opts.realtime.d = $5; 1752 hfsc_opts.realtime.m2 = $7; 1753 hfsc_opts.realtime.used = 1; 1754 } 1755 | UPPERLIMIT bandwidth { 1756 if (hfsc_opts.upperlimit.used) { 1757 yyerror("upperlimit already specified"); 1758 YYERROR; 1759 } 1760 hfsc_opts.upperlimit.m2 = $2; 1761 hfsc_opts.upperlimit.used = 1; 1762 } 1763 | UPPERLIMIT '(' bandwidth comma NUMBER comma bandwidth ')' 1764 { 1765 if ($5 < 0 || $5 > INT_MAX) { 1766 yyerror("timing in curve out of range"); 1767 YYERROR; 1768 } 1769 if (hfsc_opts.upperlimit.used) { 1770 yyerror("upperlimit already specified"); 1771 YYERROR; 1772 } 1773 hfsc_opts.upperlimit.m1 = $3; 1774 hfsc_opts.upperlimit.d = $5; 1775 hfsc_opts.upperlimit.m2 = $7; 1776 hfsc_opts.upperlimit.used = 1; 1777 } 1778 | STRING { 1779 if (!strcmp($1, "default")) 1780 hfsc_opts.flags |= HFCF_DEFAULTCLASS; 1781 else if (!strcmp($1, "red")) 1782 hfsc_opts.flags |= HFCF_RED; 1783 else if (!strcmp($1, "ecn")) 1784 hfsc_opts.flags |= HFCF_RED|HFCF_ECN; 1785 else if (!strcmp($1, "rio")) 1786 hfsc_opts.flags |= HFCF_RIO; 1787 else { 1788 yyerror("unknown hfsc flag \"%s\"", $1); 1789 free($1); 1790 YYERROR; 1791 } 1792 free($1); 1793 } 1794 ; 1795 1796qassign : /* empty */ { $$ = NULL; } 1797 | qassign_item { $$ = $1; } 1798 | '{' optnl qassign_list '}' { $$ = $3; } 1799 ; 1800 1801qassign_list : qassign_item optnl { $$ = $1; } 1802 | qassign_list comma qassign_item optnl { 1803 $1->tail->next = $3; 1804 $1->tail = $3; 1805 $$ = $1; 1806 } 1807 ; 1808 1809qassign_item : STRING { 1810 $$ = calloc(1, sizeof(struct node_queue)); 1811 if ($$ == NULL) 1812 err(1, "qassign_item: calloc"); 1813 if (strlcpy($$->queue, $1, sizeof($$->queue)) >= 1814 sizeof($$->queue)) { 1815 yyerror("queue name '%s' too long (max " 1816 "%d chars)", $1, sizeof($$->queue)-1); 1817 free($1); 1818 free($$); 1819 YYERROR; 1820 } 1821 free($1); 1822 $$->next = NULL; 1823 $$->tail = $$; 1824 } 1825 ; 1826 1827pfrule : action dir logquick interface route af proto fromto 1828 filter_opts 1829 { 1830 struct pf_rule r; 1831 struct node_state_opt *o; 1832 struct node_proto *proto; 1833 int srctrack = 0; 1834 int statelock = 0; 1835 int adaptive = 0; 1836 1837 if (check_rulestate(PFCTL_STATE_FILTER)) 1838 YYERROR; 1839 1840 memset(&r, 0, sizeof(r)); 1841 1842 r.action = $1.b1; 1843 switch ($1.b2) { 1844 case PFRULE_RETURNRST: 1845 r.rule_flag |= PFRULE_RETURNRST; 1846 r.return_ttl = $1.w; 1847 break; 1848 case PFRULE_RETURNICMP: 1849 r.rule_flag |= PFRULE_RETURNICMP; 1850 r.return_icmp = $1.w; 1851 r.return_icmp6 = $1.w2; 1852 break; 1853 case PFRULE_RETURN: 1854 r.rule_flag |= PFRULE_RETURN; 1855 r.return_icmp = $1.w; 1856 r.return_icmp6 = $1.w2; 1857 break; 1858 } 1859 r.direction = $2; 1860 r.log = $3.log; 1861 r.logif = $3.logif; 1862 r.quick = $3.quick; 1863 r.prob = $9.prob; 1864 r.rtableid = $9.rtableid; 1865 1866 r.af = $6; 1867 if ($9.tag) 1868 if (strlcpy(r.tagname, $9.tag, 1869 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) { 1870 yyerror("tag too long, max %u chars", 1871 PF_TAG_NAME_SIZE - 1); 1872 YYERROR; 1873 } 1874 if ($9.match_tag) 1875 if (strlcpy(r.match_tagname, $9.match_tag, 1876 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) { 1877 yyerror("tag too long, max %u chars", 1878 PF_TAG_NAME_SIZE - 1); 1879 YYERROR; 1880 } 1881 r.match_tag_not = $9.match_tag_not; 1882 if (rule_label(&r, $9.label)) 1883 YYERROR; 1884 free($9.label); 1885 r.flags = $9.flags.b1; 1886 r.flagset = $9.flags.b2; 1887 if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) { 1888 yyerror("flags always false"); 1889 YYERROR; 1890 } 1891 if ($9.flags.b1 || $9.flags.b2 || $8.src_os) { 1892 for (proto = $7; proto != NULL && 1893 proto->proto != IPPROTO_TCP; 1894 proto = proto->next) 1895 ; /* nothing */ 1896 if (proto == NULL && $7 != NULL) { 1897 if ($9.flags.b1 || $9.flags.b2) 1898 yyerror( 1899 "flags only apply to tcp"); 1900 if ($8.src_os) 1901 yyerror( 1902 "OS fingerprinting only " 1903 "apply to tcp"); 1904 YYERROR; 1905 } 1906#if 0 1907 if (($9.flags.b1 & parse_flags("S")) == 0 && 1908 $8.src_os) { 1909 yyerror("OS fingerprinting requires " 1910 "the SYN TCP flag (flags S/SA)"); 1911 YYERROR; 1912 } 1913#endif 1914 } 1915 1916 r.tos = $9.tos; 1917 r.keep_state = $9.keep.action; 1918 1919 /* 'keep state' by default on pass rules. */ 1920 if (!r.keep_state && !r.action && 1921 !($9.marker & FOM_KEEP)) 1922 r.keep_state = PF_STATE_NORMAL; 1923 1924 o = $9.keep.options; 1925 while (o) { 1926 struct node_state_opt *p = o; 1927 1928 switch (o->type) { 1929 case PF_STATE_OPT_MAX: 1930 if (r.max_states) { 1931 yyerror("state option 'max' " 1932 "multiple definitions"); 1933 YYERROR; 1934 } 1935 r.max_states = o->data.max_states; 1936 break; 1937 case PF_STATE_OPT_NOSYNC: 1938 if (r.rule_flag & PFRULE_NOSYNC) { 1939 yyerror("state option 'sync' " 1940 "multiple definitions"); 1941 YYERROR; 1942 } 1943 r.rule_flag |= PFRULE_NOSYNC; 1944 break; 1945 case PF_STATE_OPT_SRCTRACK: 1946 if (srctrack) { 1947 yyerror("state option " 1948 "'source-track' " 1949 "multiple definitions"); 1950 YYERROR; 1951 } 1952 srctrack = o->data.src_track; 1953 r.rule_flag |= PFRULE_SRCTRACK; 1954 break; 1955 case PF_STATE_OPT_MAX_SRC_STATES: 1956 if (r.max_src_states) { 1957 yyerror("state option " 1958 "'max-src-states' " 1959 "multiple definitions"); 1960 YYERROR; 1961 } 1962 if (o->data.max_src_states == 0) { 1963 yyerror("'max-src-states' must " 1964 "be > 0"); 1965 YYERROR; 1966 } 1967 r.max_src_states = 1968 o->data.max_src_states; 1969 r.rule_flag |= PFRULE_SRCTRACK; 1970 break; 1971 case PF_STATE_OPT_OVERLOAD: 1972 if (r.overload_tblname[0]) { 1973 yyerror("multiple 'overload' " 1974 "table definitions"); 1975 YYERROR; 1976 } 1977 if (strlcpy(r.overload_tblname, 1978 o->data.overload.tblname, 1979 PF_TABLE_NAME_SIZE) >= 1980 PF_TABLE_NAME_SIZE) { 1981 yyerror("state option: " 1982 "strlcpy"); 1983 YYERROR; 1984 } 1985 r.flush = o->data.overload.flush; 1986 break; 1987 case PF_STATE_OPT_MAX_SRC_CONN: 1988 if (r.max_src_conn) { 1989 yyerror("state option " 1990 "'max-src-conn' " 1991 "multiple definitions"); 1992 YYERROR; 1993 } 1994 if (o->data.max_src_conn == 0) { 1995 yyerror("'max-src-conn' " 1996 "must be > 0"); 1997 YYERROR; 1998 } 1999 r.max_src_conn = 2000 o->data.max_src_conn; 2001 r.rule_flag |= PFRULE_SRCTRACK | 2002 PFRULE_RULESRCTRACK; 2003 break; 2004 case PF_STATE_OPT_MAX_SRC_CONN_RATE: 2005 if (r.max_src_conn_rate.limit) { 2006 yyerror("state option " 2007 "'max-src-conn-rate' " 2008 "multiple definitions"); 2009 YYERROR; 2010 } 2011 if (!o->data.max_src_conn_rate.limit || 2012 !o->data.max_src_conn_rate.seconds) { 2013 yyerror("'max-src-conn-rate' " 2014 "values must be > 0"); 2015 YYERROR; 2016 } 2017 if (o->data.max_src_conn_rate.limit > 2018 PF_THRESHOLD_MAX) { 2019 yyerror("'max-src-conn-rate' " 2020 "maximum rate must be < %u", 2021 PF_THRESHOLD_MAX); 2022 YYERROR; 2023 } 2024 r.max_src_conn_rate.limit = 2025 o->data.max_src_conn_rate.limit; 2026 r.max_src_conn_rate.seconds = 2027 o->data.max_src_conn_rate.seconds; 2028 r.rule_flag |= PFRULE_SRCTRACK | 2029 PFRULE_RULESRCTRACK; 2030 break; 2031 case PF_STATE_OPT_MAX_SRC_NODES: 2032 if (r.max_src_nodes) { 2033 yyerror("state option " 2034 "'max-src-nodes' " 2035 "multiple definitions"); 2036 YYERROR; 2037 } 2038 if (o->data.max_src_nodes == 0) { 2039 yyerror("'max-src-nodes' must " 2040 "be > 0"); 2041 YYERROR; 2042 } 2043 r.max_src_nodes = 2044 o->data.max_src_nodes; 2045 r.rule_flag |= PFRULE_SRCTRACK | 2046 PFRULE_RULESRCTRACK; 2047 break; 2048 case PF_STATE_OPT_STATELOCK: 2049 if (statelock) { 2050 yyerror("state locking option: " 2051 "multiple definitions"); 2052 YYERROR; 2053 } 2054 statelock = 1; 2055 r.rule_flag |= o->data.statelock; 2056 break; 2057 case PF_STATE_OPT_SLOPPY: 2058 if (r.rule_flag & PFRULE_STATESLOPPY) { 2059 yyerror("state sloppy option: " 2060 "multiple definitions"); 2061 YYERROR; 2062 } 2063 r.rule_flag |= PFRULE_STATESLOPPY; 2064 break; 2065 case PF_STATE_OPT_PFLOW: 2066 if (r.rule_flag & PFRULE_PFLOW) { 2067 yyerror("state pflow " 2068 "option: multiple " 2069 "definitions"); 2070 YYERROR; 2071 } 2072 r.rule_flag |= PFRULE_PFLOW; 2073 break; 2074 case PF_STATE_OPT_TIMEOUT: 2075 if (o->data.timeout.number == 2076 PFTM_ADAPTIVE_START || 2077 o->data.timeout.number == 2078 PFTM_ADAPTIVE_END) 2079 adaptive = 1; 2080 if (r.timeout[o->data.timeout.number]) { 2081 yyerror("state timeout %s " 2082 "multiple definitions", 2083 pf_timeouts[o->data. 2084 timeout.number].name); 2085 YYERROR; 2086 } 2087 r.timeout[o->data.timeout.number] = 2088 o->data.timeout.seconds; 2089 } 2090 o = o->next; 2091 free(p); 2092 } 2093 2094 /* 'flags S/SA' by default on stateful rules */ 2095 if (!r.action && !r.flags && !r.flagset && 2096 !$9.fragment && !($9.marker & FOM_FLAGS) && 2097 r.keep_state) { 2098 r.flags = parse_flags("S"); 2099 r.flagset = parse_flags("SA"); 2100 } 2101 if (!adaptive && r.max_states) { 2102 r.timeout[PFTM_ADAPTIVE_START] = 2103 (r.max_states / 10) * 6; 2104 r.timeout[PFTM_ADAPTIVE_END] = 2105 (r.max_states / 10) * 12; 2106 } 2107 if (r.rule_flag & PFRULE_SRCTRACK) { 2108 if (srctrack == PF_SRCTRACK_GLOBAL && 2109 r.max_src_nodes) { 2110 yyerror("'max-src-nodes' is " 2111 "incompatible with " 2112 "'source-track global'"); 2113 YYERROR; 2114 } 2115 if (srctrack == PF_SRCTRACK_GLOBAL && 2116 r.max_src_conn) { 2117 yyerror("'max-src-conn' is " 2118 "incompatible with " 2119 "'source-track global'"); 2120 YYERROR; 2121 } 2122 if (srctrack == PF_SRCTRACK_GLOBAL && 2123 r.max_src_conn_rate.seconds) { 2124 yyerror("'max-src-conn-rate' is " 2125 "incompatible with " 2126 "'source-track global'"); 2127 YYERROR; 2128 } 2129 if (r.timeout[PFTM_SRC_NODE] < 2130 r.max_src_conn_rate.seconds) 2131 r.timeout[PFTM_SRC_NODE] = 2132 r.max_src_conn_rate.seconds; 2133 r.rule_flag |= PFRULE_SRCTRACK; 2134 if (srctrack == PF_SRCTRACK_RULE) 2135 r.rule_flag |= PFRULE_RULESRCTRACK; 2136 } 2137 if (r.keep_state && !statelock) 2138 r.rule_flag |= default_statelock; 2139 2140 if ($9.fragment) 2141 r.rule_flag |= PFRULE_FRAGMENT; 2142 r.allow_opts = $9.allowopts; 2143 2144 decide_address_family($8.src.host, &r.af); 2145 decide_address_family($8.dst.host, &r.af); 2146 2147 if ($5.rt) { 2148 if (!r.direction) { 2149 yyerror("direction must be explicit " 2150 "with rules that specify routing"); 2151 YYERROR; 2152 } 2153 r.rt = $5.rt; 2154 r.rpool.opts = $5.pool_opts; 2155 if ($5.key != NULL) 2156 memcpy(&r.rpool.key, $5.key, 2157 sizeof(struct pf_poolhashkey)); 2158 } 2159 if (r.rt && r.rt != PF_FASTROUTE) { 2160 decide_address_family($5.host, &r.af); 2161 remove_invalid_hosts(&$5.host, &r.af); 2162 if ($5.host == NULL) { 2163 yyerror("no routing address with " 2164 "matching address family found."); 2165 YYERROR; 2166 } 2167 if ((r.rpool.opts & PF_POOL_TYPEMASK) == 2168 PF_POOL_NONE && ($5.host->next != NULL || 2169 $5.host->addr.type == PF_ADDR_TABLE || 2170 DYNIF_MULTIADDR($5.host->addr))) 2171 r.rpool.opts |= PF_POOL_ROUNDROBIN; 2172 if ((r.rpool.opts & PF_POOL_TYPEMASK) != 2173 PF_POOL_ROUNDROBIN && 2174 disallow_table($5.host, "tables are only " 2175 "supported in round-robin routing pools")) 2176 YYERROR; 2177 if ((r.rpool.opts & PF_POOL_TYPEMASK) != 2178 PF_POOL_ROUNDROBIN && 2179 disallow_alias($5.host, "interface (%s) " 2180 "is only supported in round-robin " 2181 "routing pools")) 2182 YYERROR; 2183 if ($5.host->next != NULL) { 2184 if ((r.rpool.opts & PF_POOL_TYPEMASK) != 2185 PF_POOL_ROUNDROBIN) { 2186 yyerror("r.rpool.opts must " 2187 "be PF_POOL_ROUNDROBIN"); 2188 YYERROR; 2189 } 2190 } 2191 } 2192 if ($9.queues.qname != NULL) { 2193 if (strlcpy(r.qname, $9.queues.qname, 2194 sizeof(r.qname)) >= sizeof(r.qname)) { 2195 yyerror("rule qname too long (max " 2196 "%d chars)", sizeof(r.qname)-1); 2197 YYERROR; 2198 } 2199 free($9.queues.qname); 2200 } 2201 if ($9.queues.pqname != NULL) { 2202 if (strlcpy(r.pqname, $9.queues.pqname, 2203 sizeof(r.pqname)) >= sizeof(r.pqname)) { 2204 yyerror("rule pqname too long (max " 2205 "%d chars)", sizeof(r.pqname)-1); 2206 YYERROR; 2207 } 2208 free($9.queues.pqname); 2209 } 2210 if ((r.divert.port = $9.divert.port)) { 2211 if (r.direction == PF_OUT) { 2212 if ($9.divert.addr) { 2213 yyerror("address specified " 2214 "for outgoing divert"); 2215 YYERROR; 2216 } 2217 bzero(&r.divert.addr, 2218 sizeof(r.divert.addr)); 2219 } else { 2220 if (!$9.divert.addr) { 2221 yyerror("no address specified " 2222 "for incoming divert"); 2223 YYERROR; 2224 } 2225 if ($9.divert.addr->af != r.af) { 2226 yyerror("address family " 2227 "mismatch for divert"); 2228 YYERROR; 2229 } 2230 r.divert.addr = 2231 $9.divert.addr->addr.v.a.addr; 2232 } 2233 } 2234 2235 expand_rule(&r, $4, $5.host, $7, $8.src_os, 2236 $8.src.host, $8.src.port, $8.dst.host, $8.dst.port, 2237 $9.uid, $9.gid, $9.icmpspec, ""); 2238 } 2239 ; 2240 2241filter_opts : { 2242 bzero(&filter_opts, sizeof filter_opts); 2243 filter_opts.rtableid = -1; 2244 } 2245 filter_opts_l 2246 { $$ = filter_opts; } 2247 | /* empty */ { 2248 bzero(&filter_opts, sizeof filter_opts); 2249 filter_opts.rtableid = -1; 2250 $$ = filter_opts; 2251 } 2252 ; 2253 2254filter_opts_l : filter_opts_l filter_opt 2255 | filter_opt 2256 ; 2257 2258filter_opt : USER uids { 2259 if (filter_opts.uid) 2260 $2->tail->next = filter_opts.uid; 2261 filter_opts.uid = $2; 2262 } 2263 | GROUP gids { 2264 if (filter_opts.gid) 2265 $2->tail->next = filter_opts.gid; 2266 filter_opts.gid = $2; 2267 } 2268 | flags { 2269 if (filter_opts.marker & FOM_FLAGS) { 2270 yyerror("flags cannot be redefined"); 2271 YYERROR; 2272 } 2273 filter_opts.marker |= FOM_FLAGS; 2274 filter_opts.flags.b1 |= $1.b1; 2275 filter_opts.flags.b2 |= $1.b2; 2276 filter_opts.flags.w |= $1.w; 2277 filter_opts.flags.w2 |= $1.w2; 2278 } 2279 | icmpspec { 2280 if (filter_opts.marker & FOM_ICMP) { 2281 yyerror("icmp-type cannot be redefined"); 2282 YYERROR; 2283 } 2284 filter_opts.marker |= FOM_ICMP; 2285 filter_opts.icmpspec = $1; 2286 } 2287 | TOS tos { 2288 if (filter_opts.marker & FOM_TOS) { 2289 yyerror("tos cannot be redefined"); 2290 YYERROR; 2291 } 2292 filter_opts.marker |= FOM_TOS; 2293 filter_opts.tos = $2; 2294 } 2295 | keep { 2296 if (filter_opts.marker & FOM_KEEP) { 2297 yyerror("modulate or keep cannot be redefined"); 2298 YYERROR; 2299 } 2300 filter_opts.marker |= FOM_KEEP; 2301 filter_opts.keep.action = $1.action; 2302 filter_opts.keep.options = $1.options; 2303 } 2304 | FRAGMENT { 2305 filter_opts.fragment = 1; 2306 } 2307 | ALLOWOPTS { 2308 filter_opts.allowopts = 1; 2309 } 2310 | label { 2311 if (filter_opts.label) { 2312 yyerror("label cannot be redefined"); 2313 YYERROR; 2314 } 2315 filter_opts.label = $1; 2316 } 2317 | qname { 2318 if (filter_opts.queues.qname) { 2319 yyerror("queue cannot be redefined"); 2320 YYERROR; 2321 } 2322 filter_opts.queues = $1; 2323 } 2324 | TAG string { 2325 filter_opts.tag = $2; 2326 } 2327 | not TAGGED string { 2328 filter_opts.match_tag = $3; 2329 filter_opts.match_tag_not = $1; 2330 } 2331 | PROBABILITY probability { 2332 double p; 2333 2334 p = floor($2 * UINT_MAX + 0.5); 2335 if (p < 0.0 || p > UINT_MAX) { 2336 yyerror("invalid probability: %lf", p); 2337 YYERROR; 2338 } 2339 filter_opts.prob = (u_int32_t)p; 2340 if (filter_opts.prob == 0) 2341 filter_opts.prob = 1; 2342 } 2343 | RTABLE NUMBER { 2344 if ($2 < 0 || $2 > RT_TABLEID_MAX) { 2345 yyerror("invalid rtable id"); 2346 YYERROR; 2347 } 2348 filter_opts.rtableid = $2; 2349 } 2350 | DIVERTTO STRING PORT portplain { 2351 if ((filter_opts.divert.addr = host($2)) == NULL) { 2352 yyerror("could not parse divert address: %s", 2353 $2); 2354 free($2); 2355 YYERROR; 2356 } 2357 free($2); 2358 filter_opts.divert.port = $4.a; 2359 if (!filter_opts.divert.port) { 2360 yyerror("invalid divert port: %u", ntohs($4.a)); 2361 YYERROR; 2362 } 2363 } 2364 | DIVERTREPLY { 2365 filter_opts.divert.port = 1; /* some random value */ 2366 } 2367 ; 2368 2369probability : STRING { 2370 char *e; 2371 double p = strtod($1, &e); 2372 2373 if (*e == '%') { 2374 p *= 0.01; 2375 e++; 2376 } 2377 if (*e) { 2378 yyerror("invalid probability: %s", $1); 2379 free($1); 2380 YYERROR; 2381 } 2382 free($1); 2383 $$ = p; 2384 } 2385 | NUMBER { 2386 $$ = (double)$1; 2387 } 2388 ; 2389 2390 2391action : PASS { $$.b1 = PF_PASS; $$.b2 = $$.w = 0; } 2392 | BLOCK blockspec { $$ = $2; $$.b1 = PF_DROP; } 2393 ; 2394 2395blockspec : /* empty */ { 2396 $$.b2 = blockpolicy; 2397 $$.w = returnicmpdefault; 2398 $$.w2 = returnicmp6default; 2399 } 2400 | DROP { 2401 $$.b2 = PFRULE_DROP; 2402 $$.w = 0; 2403 $$.w2 = 0; 2404 } 2405 | RETURNRST { 2406 $$.b2 = PFRULE_RETURNRST; 2407 $$.w = 0; 2408 $$.w2 = 0; 2409 } 2410 | RETURNRST '(' TTL NUMBER ')' { 2411 if ($4 < 0 || $4 > 255) { 2412 yyerror("illegal ttl value %d", $4); 2413 YYERROR; 2414 } 2415 $$.b2 = PFRULE_RETURNRST; 2416 $$.w = $4; 2417 $$.w2 = 0; 2418 } 2419 | RETURNICMP { 2420 $$.b2 = PFRULE_RETURNICMP; 2421 $$.w = returnicmpdefault; 2422 $$.w2 = returnicmp6default; 2423 } 2424 | RETURNICMP6 { 2425 $$.b2 = PFRULE_RETURNICMP; 2426 $$.w = returnicmpdefault; 2427 $$.w2 = returnicmp6default; 2428 } 2429 | RETURNICMP '(' reticmpspec ')' { 2430 $$.b2 = PFRULE_RETURNICMP; 2431 $$.w = $3; 2432 $$.w2 = returnicmpdefault; 2433 } 2434 | RETURNICMP6 '(' reticmp6spec ')' { 2435 $$.b2 = PFRULE_RETURNICMP; 2436 $$.w = returnicmpdefault; 2437 $$.w2 = $3; 2438 } 2439 | RETURNICMP '(' reticmpspec comma reticmp6spec ')' { 2440 $$.b2 = PFRULE_RETURNICMP; 2441 $$.w = $3; 2442 $$.w2 = $5; 2443 } 2444 | RETURN { 2445 $$.b2 = PFRULE_RETURN; 2446 $$.w = returnicmpdefault; 2447 $$.w2 = returnicmp6default; 2448 } 2449 ; 2450 2451reticmpspec : STRING { 2452 if (!($$ = parseicmpspec($1, AF_INET))) { 2453 free($1); 2454 YYERROR; 2455 } 2456 free($1); 2457 } 2458 | NUMBER { 2459 u_int8_t icmptype; 2460 2461 if ($1 < 0 || $1 > 255) { 2462 yyerror("invalid icmp code %lu", $1); 2463 YYERROR; 2464 } 2465 icmptype = returnicmpdefault >> 8; 2466 $$ = (icmptype << 8 | $1); 2467 } 2468 ; 2469 2470reticmp6spec : STRING { 2471 if (!($$ = parseicmpspec($1, AF_INET6))) { 2472 free($1); 2473 YYERROR; 2474 } 2475 free($1); 2476 } 2477 | NUMBER { 2478 u_int8_t icmptype; 2479 2480 if ($1 < 0 || $1 > 255) { 2481 yyerror("invalid icmp code %lu", $1); 2482 YYERROR; 2483 } 2484 icmptype = returnicmp6default >> 8; 2485 $$ = (icmptype << 8 | $1); 2486 } 2487 ; 2488 2489dir : /* empty */ { $$ = PF_INOUT; } 2490 | IN { $$ = PF_IN; } 2491 | OUT { $$ = PF_OUT; } 2492 ; 2493 2494quick : /* empty */ { $$.quick = 0; } 2495 | QUICK { $$.quick = 1; } 2496 ; 2497 2498logquick : /* empty */ { $$.log = 0; $$.quick = 0; $$.logif = 0; } 2499 | log { $$ = $1; $$.quick = 0; } 2500 | QUICK { $$.quick = 1; $$.log = 0; $$.logif = 0; } 2501 | log QUICK { $$ = $1; $$.quick = 1; } 2502 | QUICK log { $$ = $2; $$.quick = 1; } 2503 ; 2504 2505log : LOG { $$.log = PF_LOG; $$.logif = 0; } 2506 | LOG '(' logopts ')' { 2507 $$.log = PF_LOG | $3.log; 2508 $$.logif = $3.logif; 2509 } 2510 ; 2511 2512logopts : logopt { $$ = $1; } 2513 | logopts comma logopt { 2514 $$.log = $1.log | $3.log; 2515 $$.logif = $3.logif; 2516 if ($$.logif == 0) 2517 $$.logif = $1.logif; 2518 } 2519 ; 2520 2521logopt : ALL { $$.log = PF_LOG_ALL; $$.logif = 0; } 2522 | USER { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; } 2523 | GROUP { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; } 2524 | TO string { 2525 const char *errstr; 2526 u_int i; 2527 2528 $$.log = 0; 2529 if (strncmp($2, "pflog", 5)) { 2530 yyerror("%s: should be a pflog interface", $2); 2531 free($2); 2532 YYERROR; 2533 } 2534 i = strtonum($2 + 5, 0, 255, &errstr); 2535 if (errstr) { 2536 yyerror("%s: %s", $2, errstr); 2537 free($2); 2538 YYERROR; 2539 } 2540 free($2); 2541 $$.logif = i; 2542 } 2543 ; 2544 2545interface : /* empty */ { $$ = NULL; } 2546 | ON if_item_not { $$ = $2; } 2547 | ON '{' optnl if_list '}' { $$ = $4; } 2548 ; 2549 2550if_list : if_item_not optnl { $$ = $1; } 2551 | if_list comma if_item_not optnl { 2552 $1->tail->next = $3; 2553 $1->tail = $3; 2554 $$ = $1; 2555 } 2556 ; 2557 2558if_item_not : not if_item { $$ = $2; $$->not = $1; } 2559 ; 2560 2561if_item : STRING { 2562 struct node_host *n; 2563 2564 $$ = calloc(1, sizeof(struct node_if)); 2565 if ($$ == NULL) 2566 err(1, "if_item: calloc"); 2567 if (strlcpy($$->ifname, $1, sizeof($$->ifname)) >= 2568 sizeof($$->ifname)) { 2569 free($1); 2570 free($$); 2571 yyerror("interface name too long"); 2572 YYERROR; 2573 } 2574 2575 if ((n = ifa_exists($1)) != NULL) 2576 $$->ifa_flags = n->ifa_flags; 2577 2578 free($1); 2579 $$->not = 0; 2580 $$->next = NULL; 2581 $$->tail = $$; 2582 } 2583 ; 2584 2585af : /* empty */ { $$ = 0; } 2586 | INET { $$ = AF_INET; } 2587 | INET6 { $$ = AF_INET6; } 2588 ; 2589 2590proto : /* empty */ { $$ = NULL; } 2591 | PROTO proto_item { $$ = $2; } 2592 | PROTO '{' optnl proto_list '}' { $$ = $4; } 2593 ; 2594 2595proto_list : proto_item optnl { $$ = $1; } 2596 | proto_list comma proto_item optnl { 2597 $1->tail->next = $3; 2598 $1->tail = $3; 2599 $$ = $1; 2600 } 2601 ; 2602 2603proto_item : protoval { 2604 u_int8_t pr; 2605 2606 pr = (u_int8_t)$1; 2607 if (pr == 0) { 2608 yyerror("proto 0 cannot be used"); 2609 YYERROR; 2610 } 2611 $$ = calloc(1, sizeof(struct node_proto)); 2612 if ($$ == NULL) 2613 err(1, "proto_item: calloc"); 2614 $$->proto = pr; 2615 $$->next = NULL; 2616 $$->tail = $$; 2617 } 2618 ; 2619 2620protoval : STRING { 2621 struct protoent *p; 2622 2623 p = getprotobyname($1); 2624 if (p == NULL) { 2625 yyerror("unknown protocol %s", $1); 2626 free($1); 2627 YYERROR; 2628 } 2629 $$ = p->p_proto; 2630 free($1); 2631 } 2632 | NUMBER { 2633 if ($1 < 0 || $1 > 255) { 2634 yyerror("protocol outside range"); 2635 YYERROR; 2636 } 2637 } 2638 ; 2639 2640fromto : ALL { 2641 $$.src.host = NULL; 2642 $$.src.port = NULL; 2643 $$.dst.host = NULL; 2644 $$.dst.port = NULL; 2645 $$.src_os = NULL; 2646 } 2647 | from os to { 2648 $$.src = $1; 2649 $$.src_os = $2; 2650 $$.dst = $3; 2651 } 2652 ; 2653 2654os : /* empty */ { $$ = NULL; } 2655 | OS xos { $$ = $2; } 2656 | OS '{' optnl os_list '}' { $$ = $4; } 2657 ; 2658 2659xos : STRING { 2660 $$ = calloc(1, sizeof(struct node_os)); 2661 if ($$ == NULL) 2662 err(1, "os: calloc"); 2663 $$->os = $1; 2664 $$->tail = $$; 2665 } 2666 ; 2667 2668os_list : xos optnl { $$ = $1; } 2669 | os_list comma xos optnl { 2670 $1->tail->next = $3; 2671 $1->tail = $3; 2672 $$ = $1; 2673 } 2674 ; 2675 2676from : /* empty */ { 2677 $$.host = NULL; 2678 $$.port = NULL; 2679 } 2680 | FROM ipportspec { 2681 $$ = $2; 2682 } 2683 ; 2684 2685to : /* empty */ { 2686 $$.host = NULL; 2687 $$.port = NULL; 2688 } 2689 | TO ipportspec { 2690 if (disallow_urpf_failed($2.host, "\"urpf-failed\" is " 2691 "not permitted in a destination address")) 2692 YYERROR; 2693 $$ = $2; 2694 } 2695 ; 2696 2697ipportspec : ipspec { 2698 $$.host = $1; 2699 $$.port = NULL; 2700 } 2701 | ipspec PORT portspec { 2702 $$.host = $1; 2703 $$.port = $3; 2704 } 2705 | PORT portspec { 2706 $$.host = NULL; 2707 $$.port = $2; 2708 } 2709 ; 2710 2711optnl : '\n' optnl 2712 | 2713 ; 2714 2715ipspec : ANY { $$ = NULL; } 2716 | xhost { $$ = $1; } 2717 | '{' optnl host_list '}' { $$ = $3; } 2718 ; 2719 2720toipspec : TO ipspec { $$ = $2; } 2721 | /* empty */ { $$ = NULL; } 2722 ; 2723 2724host_list : ipspec optnl { $$ = $1; } 2725 | host_list comma ipspec optnl { 2726 if ($3 == NULL) 2727 $$ = $1; 2728 else if ($1 == NULL) 2729 $$ = $3; 2730 else { 2731 $1->tail->next = $3; 2732 $1->tail = $3->tail; 2733 $$ = $1; 2734 } 2735 } 2736 ; 2737 2738xhost : not host { 2739 struct node_host *n; 2740 2741 for (n = $2; n != NULL; n = n->next) 2742 n->not = $1; 2743 $$ = $2; 2744 } 2745 | not NOROUTE { 2746 $$ = calloc(1, sizeof(struct node_host)); 2747 if ($$ == NULL) 2748 err(1, "xhost: calloc"); 2749 $$->addr.type = PF_ADDR_NOROUTE; 2750 $$->next = NULL; 2751 $$->not = $1; 2752 $$->tail = $$; 2753 } 2754 | not URPFFAILED { 2755 $$ = calloc(1, sizeof(struct node_host)); 2756 if ($$ == NULL) 2757 err(1, "xhost: calloc"); 2758 $$->addr.type = PF_ADDR_URPFFAILED; 2759 $$->next = NULL; 2760 $$->not = $1; 2761 $$->tail = $$; 2762 } 2763 ; 2764 2765host : STRING { 2766 if (($$ = host($1)) == NULL) { 2767 /* error. "any" is handled elsewhere */ 2768 free($1); 2769 yyerror("could not parse host specification"); 2770 YYERROR; 2771 } 2772 free($1); 2773 2774 } 2775 | STRING '-' STRING { 2776 struct node_host *b, *e; 2777 2778 if ((b = host($1)) == NULL || (e = host($3)) == NULL) { 2779 free($1); 2780 free($3); 2781 yyerror("could not parse host specification"); 2782 YYERROR; 2783 } 2784 if (b->af != e->af || 2785 b->addr.type != PF_ADDR_ADDRMASK || 2786 e->addr.type != PF_ADDR_ADDRMASK || 2787 unmask(&b->addr.v.a.mask, b->af) != 2788 (b->af == AF_INET ? 32 : 128) || 2789 unmask(&e->addr.v.a.mask, e->af) != 2790 (e->af == AF_INET ? 32 : 128) || 2791 b->next != NULL || b->not || 2792 e->next != NULL || e->not) { 2793 free(b); 2794 free(e); 2795 free($1); 2796 free($3); 2797 yyerror("invalid address range"); 2798 YYERROR; 2799 } 2800 memcpy(&b->addr.v.a.mask, &e->addr.v.a.addr, 2801 sizeof(b->addr.v.a.mask)); 2802 b->addr.type = PF_ADDR_RANGE; 2803 $$ = b; 2804 free(e); 2805 free($1); 2806 free($3); 2807 } 2808 | STRING '/' NUMBER { 2809 char *buf; 2810 2811 if (asprintf(&buf, "%s/%lld", $1, $3) == -1) 2812 err(1, "host: asprintf"); 2813 free($1); 2814 if (($$ = host(buf)) == NULL) { 2815 /* error. "any" is handled elsewhere */ 2816 free(buf); 2817 yyerror("could not parse host specification"); 2818 YYERROR; 2819 } 2820 free(buf); 2821 } 2822 | NUMBER '/' NUMBER { 2823 char *buf; 2824 2825 /* ie. for 10/8 parsing */ 2826 if (asprintf(&buf, "%lld/%lld", $1, $3) == -1) 2827 err(1, "host: asprintf"); 2828 if (($$ = host(buf)) == NULL) { 2829 /* error. "any" is handled elsewhere */ 2830 free(buf); 2831 yyerror("could not parse host specification"); 2832 YYERROR; 2833 } 2834 free(buf); 2835 } 2836 | dynaddr 2837 | dynaddr '/' NUMBER { 2838 struct node_host *n; 2839 2840 if ($3 < 0 || $3 > 128) { 2841 yyerror("bit number too big"); 2842 YYERROR; 2843 } 2844 $$ = $1; 2845 for (n = $1; n != NULL; n = n->next) 2846 set_ipmask(n, $3); 2847 } 2848 | '<' STRING '>' { 2849 if (strlen($2) >= PF_TABLE_NAME_SIZE) { 2850 yyerror("table name '%s' too long", $2); 2851 free($2); 2852 YYERROR; 2853 } 2854 $$ = calloc(1, sizeof(struct node_host)); 2855 if ($$ == NULL) 2856 err(1, "host: calloc"); 2857 $$->addr.type = PF_ADDR_TABLE; 2858 if (strlcpy($$->addr.v.tblname, $2, 2859 sizeof($$->addr.v.tblname)) >= 2860 sizeof($$->addr.v.tblname)) 2861 errx(1, "host: strlcpy"); 2862 free($2); 2863 $$->next = NULL; 2864 $$->tail = $$; 2865 } 2866 | ROUTE STRING { 2867 $$ = calloc(1, sizeof(struct node_host)); 2868 if ($$ == NULL) { 2869 free($2); 2870 err(1, "host: calloc"); 2871 } 2872 $$->addr.type = PF_ADDR_RTLABEL; 2873 if (strlcpy($$->addr.v.rtlabelname, $2, 2874 sizeof($$->addr.v.rtlabelname)) >= 2875 sizeof($$->addr.v.rtlabelname)) { 2876 yyerror("route label too long, max %u chars", 2877 sizeof($$->addr.v.rtlabelname) - 1); 2878 free($2); 2879 free($$); 2880 YYERROR; 2881 } 2882 $$->next = NULL; 2883 $$->tail = $$; 2884 free($2); 2885 } 2886 ; 2887 2888number : NUMBER 2889 | STRING { 2890 u_long ulval; 2891 2892 if (atoul($1, &ulval) == -1) { 2893 yyerror("%s is not a number", $1); 2894 free($1); 2895 YYERROR; 2896 } else 2897 $$ = ulval; 2898 free($1); 2899 } 2900 ; 2901 2902dynaddr : '(' STRING ')' { 2903 int flags = 0; 2904 char *p, *op; 2905 2906 op = $2; 2907 if (!isalpha(op[0])) { 2908 yyerror("invalid interface name '%s'", op); 2909 free(op); 2910 YYERROR; 2911 } 2912 while ((p = strrchr($2, ':')) != NULL) { 2913 if (!strcmp(p+1, "network")) 2914 flags |= PFI_AFLAG_NETWORK; 2915 else if (!strcmp(p+1, "broadcast")) 2916 flags |= PFI_AFLAG_BROADCAST; 2917 else if (!strcmp(p+1, "peer")) 2918 flags |= PFI_AFLAG_PEER; 2919 else if (!strcmp(p+1, "0")) 2920 flags |= PFI_AFLAG_NOALIAS; 2921 else { 2922 yyerror("interface %s has bad modifier", 2923 $2); 2924 free(op); 2925 YYERROR; 2926 } 2927 *p = '\0'; 2928 } 2929 if (flags & (flags - 1) & PFI_AFLAG_MODEMASK) { 2930 free(op); 2931 yyerror("illegal combination of " 2932 "interface modifiers"); 2933 YYERROR; 2934 } 2935 $$ = calloc(1, sizeof(struct node_host)); 2936 if ($$ == NULL) 2937 err(1, "address: calloc"); 2938 $$->af = 0; 2939 set_ipmask($$, 128); 2940 $$->addr.type = PF_ADDR_DYNIFTL; 2941 $$->addr.iflags = flags; 2942 if (strlcpy($$->addr.v.ifname, $2, 2943 sizeof($$->addr.v.ifname)) >= 2944 sizeof($$->addr.v.ifname)) { 2945 free(op); 2946 free($$); 2947 yyerror("interface name too long"); 2948 YYERROR; 2949 } 2950 free(op); 2951 $$->next = NULL; 2952 $$->tail = $$; 2953 } 2954 ; 2955 2956portspec : port_item { $$ = $1; } 2957 | '{' optnl port_list '}' { $$ = $3; } 2958 ; 2959 2960port_list : port_item optnl { $$ = $1; } 2961 | port_list comma port_item optnl { 2962 $1->tail->next = $3; 2963 $1->tail = $3; 2964 $$ = $1; 2965 } 2966 ; 2967 2968port_item : portrange { 2969 $$ = calloc(1, sizeof(struct node_port)); 2970 if ($$ == NULL) 2971 err(1, "port_item: calloc"); 2972 $$->port[0] = $1.a; 2973 $$->port[1] = $1.b; 2974 if ($1.t) 2975 $$->op = PF_OP_RRG; 2976 else 2977 $$->op = PF_OP_EQ; 2978 $$->next = NULL; 2979 $$->tail = $$; 2980 } 2981 | unaryop portrange { 2982 if ($2.t) { 2983 yyerror("':' cannot be used with an other " 2984 "port operator"); 2985 YYERROR; 2986 } 2987 $$ = calloc(1, sizeof(struct node_port)); 2988 if ($$ == NULL) 2989 err(1, "port_item: calloc"); 2990 $$->port[0] = $2.a; 2991 $$->port[1] = $2.b; 2992 $$->op = $1; 2993 $$->next = NULL; 2994 $$->tail = $$; 2995 } 2996 | portrange PORTBINARY portrange { 2997 if ($1.t || $3.t) { 2998 yyerror("':' cannot be used with an other " 2999 "port operator"); 3000 YYERROR; 3001 } 3002 $$ = calloc(1, sizeof(struct node_port)); 3003 if ($$ == NULL) 3004 err(1, "port_item: calloc"); 3005 $$->port[0] = $1.a; 3006 $$->port[1] = $3.a; 3007 $$->op = $2; 3008 $$->next = NULL; 3009 $$->tail = $$; 3010 } 3011 ; 3012 3013portplain : numberstring { 3014 if (parseport($1, &$$, 0) == -1) { 3015 free($1); 3016 YYERROR; 3017 } 3018 free($1); 3019 } 3020 ; 3021 3022portrange : numberstring { 3023 if (parseport($1, &$$, PPORT_RANGE) == -1) { 3024 free($1); 3025 YYERROR; 3026 } 3027 free($1); 3028 } 3029 ; 3030 3031uids : uid_item { $$ = $1; } 3032 | '{' optnl uid_list '}' { $$ = $3; } 3033 ; 3034 3035uid_list : uid_item optnl { $$ = $1; } 3036 | uid_list comma uid_item optnl { 3037 $1->tail->next = $3; 3038 $1->tail = $3; 3039 $$ = $1; 3040 } 3041 ; 3042 3043uid_item : uid { 3044 $$ = calloc(1, sizeof(struct node_uid)); 3045 if ($$ == NULL) 3046 err(1, "uid_item: calloc"); 3047 $$->uid[0] = $1; 3048 $$->uid[1] = $1; 3049 $$->op = PF_OP_EQ; 3050 $$->next = NULL; 3051 $$->tail = $$; 3052 } 3053 | unaryop uid { 3054 if ($2 == UID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) { 3055 yyerror("user unknown requires operator = or " 3056 "!="); 3057 YYERROR; 3058 } 3059 $$ = calloc(1, sizeof(struct node_uid)); 3060 if ($$ == NULL) 3061 err(1, "uid_item: calloc"); 3062 $$->uid[0] = $2; 3063 $$->uid[1] = $2; 3064 $$->op = $1; 3065 $$->next = NULL; 3066 $$->tail = $$; 3067 } 3068 | uid PORTBINARY uid { 3069 if ($1 == UID_MAX || $3 == UID_MAX) { 3070 yyerror("user unknown requires operator = or " 3071 "!="); 3072 YYERROR; 3073 } 3074 $$ = calloc(1, sizeof(struct node_uid)); 3075 if ($$ == NULL) 3076 err(1, "uid_item: calloc"); 3077 $$->uid[0] = $1; 3078 $$->uid[1] = $3; 3079 $$->op = $2; 3080 $$->next = NULL; 3081 $$->tail = $$; 3082 } 3083 ; 3084 3085uid : STRING { 3086 if (!strcmp($1, "unknown")) 3087 $$ = UID_MAX; 3088 else { 3089 struct passwd *pw; 3090 3091 if ((pw = getpwnam($1)) == NULL) { 3092 yyerror("unknown user %s", $1); 3093 free($1); 3094 YYERROR; 3095 } 3096 $$ = pw->pw_uid; 3097 } 3098 free($1); 3099 } 3100 | NUMBER { 3101 if ($1 < 0 || $1 >= UID_MAX) { 3102 yyerror("illegal uid value %lu", $1); 3103 YYERROR; 3104 } 3105 $$ = $1; 3106 } 3107 ; 3108 3109gids : gid_item { $$ = $1; } 3110 | '{' optnl gid_list '}' { $$ = $3; } 3111 ; 3112 3113gid_list : gid_item optnl { $$ = $1; } 3114 | gid_list comma gid_item optnl { 3115 $1->tail->next = $3; 3116 $1->tail = $3; 3117 $$ = $1; 3118 } 3119 ; 3120 3121gid_item : gid { 3122 $$ = calloc(1, sizeof(struct node_gid)); 3123 if ($$ == NULL) 3124 err(1, "gid_item: calloc"); 3125 $$->gid[0] = $1; 3126 $$->gid[1] = $1; 3127 $$->op = PF_OP_EQ; 3128 $$->next = NULL; 3129 $$->tail = $$; 3130 } 3131 | unaryop gid { 3132 if ($2 == GID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) { 3133 yyerror("group unknown requires operator = or " 3134 "!="); 3135 YYERROR; 3136 } 3137 $$ = calloc(1, sizeof(struct node_gid)); 3138 if ($$ == NULL) 3139 err(1, "gid_item: calloc"); 3140 $$->gid[0] = $2; 3141 $$->gid[1] = $2; 3142 $$->op = $1; 3143 $$->next = NULL; 3144 $$->tail = $$; 3145 } 3146 | gid PORTBINARY gid { 3147 if ($1 == GID_MAX || $3 == GID_MAX) { 3148 yyerror("group unknown requires operator = or " 3149 "!="); 3150 YYERROR; 3151 } 3152 $$ = calloc(1, sizeof(struct node_gid)); 3153 if ($$ == NULL) 3154 err(1, "gid_item: calloc"); 3155 $$->gid[0] = $1; 3156 $$->gid[1] = $3; 3157 $$->op = $2; 3158 $$->next = NULL; 3159 $$->tail = $$; 3160 } 3161 ; 3162 3163gid : STRING { 3164 if (!strcmp($1, "unknown")) 3165 $$ = GID_MAX; 3166 else { 3167 struct group *grp; 3168 3169 if ((grp = getgrnam($1)) == NULL) { 3170 yyerror("unknown group %s", $1); 3171 free($1); 3172 YYERROR; 3173 } 3174 $$ = grp->gr_gid; 3175 } 3176 free($1); 3177 } 3178 | NUMBER { 3179 if ($1 < 0 || $1 >= GID_MAX) { 3180 yyerror("illegal gid value %lu", $1); 3181 YYERROR; 3182 } 3183 $$ = $1; 3184 } 3185 ; 3186 3187flag : STRING { 3188 int f; 3189 3190 if ((f = parse_flags($1)) < 0) { 3191 yyerror("bad flags %s", $1); 3192 free($1); 3193 YYERROR; 3194 } 3195 free($1); 3196 $$.b1 = f; 3197 } 3198 ; 3199 3200flags : FLAGS flag '/' flag { $$.b1 = $2.b1; $$.b2 = $4.b1; } 3201 | FLAGS '/' flag { $$.b1 = 0; $$.b2 = $3.b1; } 3202 | FLAGS ANY { $$.b1 = 0; $$.b2 = 0; } 3203 ; 3204 3205icmpspec : ICMPTYPE icmp_item { $$ = $2; } 3206 | ICMPTYPE '{' optnl icmp_list '}' { $$ = $4; } 3207 | ICMP6TYPE icmp6_item { $$ = $2; } 3208 | ICMP6TYPE '{' optnl icmp6_list '}' { $$ = $4; } 3209 ; 3210 3211icmp_list : icmp_item optnl { $$ = $1; } 3212 | icmp_list comma icmp_item optnl { 3213 $1->tail->next = $3; 3214 $1->tail = $3; 3215 $$ = $1; 3216 } 3217 ; 3218 3219icmp6_list : icmp6_item optnl { $$ = $1; } 3220 | icmp6_list comma icmp6_item optnl { 3221 $1->tail->next = $3; 3222 $1->tail = $3; 3223 $$ = $1; 3224 } 3225 ; 3226 3227icmp_item : icmptype { 3228 $$ = calloc(1, sizeof(struct node_icmp)); 3229 if ($$ == NULL) 3230 err(1, "icmp_item: calloc"); 3231 $$->type = $1; 3232 $$->code = 0; 3233 $$->proto = IPPROTO_ICMP; 3234 $$->next = NULL; 3235 $$->tail = $$; 3236 } 3237 | icmptype CODE STRING { 3238 const struct icmpcodeent *p; 3239 3240 if ((p = geticmpcodebyname($1-1, $3, AF_INET)) == NULL) { 3241 yyerror("unknown icmp-code %s", $3); 3242 free($3); 3243 YYERROR; 3244 } 3245 3246 free($3); 3247 $$ = calloc(1, sizeof(struct node_icmp)); 3248 if ($$ == NULL) 3249 err(1, "icmp_item: calloc"); 3250 $$->type = $1; 3251 $$->code = p->code + 1; 3252 $$->proto = IPPROTO_ICMP; 3253 $$->next = NULL; 3254 $$->tail = $$; 3255 } 3256 | icmptype CODE NUMBER { 3257 if ($3 < 0 || $3 > 255) { 3258 yyerror("illegal icmp-code %lu", $3); 3259 YYERROR; 3260 } 3261 $$ = calloc(1, sizeof(struct node_icmp)); 3262 if ($$ == NULL) 3263 err(1, "icmp_item: calloc"); 3264 $$->type = $1; 3265 $$->code = $3 + 1; 3266 $$->proto = IPPROTO_ICMP; 3267 $$->next = NULL; 3268 $$->tail = $$; 3269 } 3270 ; 3271 3272icmp6_item : icmp6type { 3273 $$ = calloc(1, sizeof(struct node_icmp)); 3274 if ($$ == NULL) 3275 err(1, "icmp_item: calloc"); 3276 $$->type = $1; 3277 $$->code = 0; 3278 $$->proto = IPPROTO_ICMPV6; 3279 $$->next = NULL; 3280 $$->tail = $$; 3281 } 3282 | icmp6type CODE STRING { 3283 const struct icmpcodeent *p; 3284 3285 if ((p = geticmpcodebyname($1-1, $3, AF_INET6)) == NULL) { 3286 yyerror("unknown icmp6-code %s", $3); 3287 free($3); 3288 YYERROR; 3289 } 3290 free($3); 3291 3292 $$ = calloc(1, sizeof(struct node_icmp)); 3293 if ($$ == NULL) 3294 err(1, "icmp_item: calloc"); 3295 $$->type = $1; 3296 $$->code = p->code + 1; 3297 $$->proto = IPPROTO_ICMPV6; 3298 $$->next = NULL; 3299 $$->tail = $$; 3300 } 3301 | icmp6type CODE NUMBER { 3302 if ($3 < 0 || $3 > 255) { 3303 yyerror("illegal icmp-code %lu", $3); 3304 YYERROR; 3305 } 3306 $$ = calloc(1, sizeof(struct node_icmp)); 3307 if ($$ == NULL) 3308 err(1, "icmp_item: calloc"); 3309 $$->type = $1; 3310 $$->code = $3 + 1; 3311 $$->proto = IPPROTO_ICMPV6; 3312 $$->next = NULL; 3313 $$->tail = $$; 3314 } 3315 ; 3316 3317icmptype : STRING { 3318 const struct icmptypeent *p; 3319 3320 if ((p = geticmptypebyname($1, AF_INET)) == NULL) { 3321 yyerror("unknown icmp-type %s", $1); 3322 free($1); 3323 YYERROR; 3324 } 3325 $$ = p->type + 1; 3326 free($1); 3327 } 3328 | NUMBER { 3329 if ($1 < 0 || $1 > 255) { 3330 yyerror("illegal icmp-type %lu", $1); 3331 YYERROR; 3332 } 3333 $$ = $1 + 1; 3334 } 3335 ; 3336 3337icmp6type : STRING { 3338 const struct icmptypeent *p; 3339 3340 if ((p = geticmptypebyname($1, AF_INET6)) == 3341 NULL) { 3342 yyerror("unknown icmp6-type %s", $1); 3343 free($1); 3344 YYERROR; 3345 } 3346 $$ = p->type + 1; 3347 free($1); 3348 } 3349 | NUMBER { 3350 if ($1 < 0 || $1 > 255) { 3351 yyerror("illegal icmp6-type %lu", $1); 3352 YYERROR; 3353 } 3354 $$ = $1 + 1; 3355 } 3356 ; 3357 3358tos : STRING { 3359 if (!strcmp($1, "lowdelay")) 3360 $$ = IPTOS_LOWDELAY; 3361 else if (!strcmp($1, "throughput")) 3362 $$ = IPTOS_THROUGHPUT; 3363 else if (!strcmp($1, "reliability")) 3364 $$ = IPTOS_RELIABILITY; 3365 else if ($1[0] == '0' && $1[1] == 'x') 3366 $$ = strtoul($1, NULL, 16); 3367 else 3368 $$ = 0; /* flag bad argument */ 3369 if (!$$ || $$ > 255) { 3370 yyerror("illegal tos value %s", $1); 3371 free($1); 3372 YYERROR; 3373 } 3374 free($1); 3375 } 3376 | NUMBER { 3377 $$ = $1; 3378 if (!$$ || $$ > 255) { 3379 yyerror("illegal tos value %s", $1); 3380 YYERROR; 3381 } 3382 } 3383 ; 3384 3385sourcetrack : SOURCETRACK { $$ = PF_SRCTRACK; } 3386 | SOURCETRACK GLOBAL { $$ = PF_SRCTRACK_GLOBAL; } 3387 | SOURCETRACK RULE { $$ = PF_SRCTRACK_RULE; } 3388 ; 3389 3390statelock : IFBOUND { 3391 $$ = PFRULE_IFBOUND; 3392 } 3393 | FLOATING { 3394 $$ = 0; 3395 } 3396 ; 3397 3398keep : NO STATE { 3399 $$.action = 0; 3400 $$.options = NULL; 3401 } 3402 | KEEP STATE state_opt_spec { 3403 $$.action = PF_STATE_NORMAL; 3404 $$.options = $3; 3405 } 3406 | MODULATE STATE state_opt_spec { 3407 $$.action = PF_STATE_MODULATE; 3408 $$.options = $3; 3409 } 3410 | SYNPROXY STATE state_opt_spec { 3411 $$.action = PF_STATE_SYNPROXY; 3412 $$.options = $3; 3413 } 3414 ; 3415 3416flush : /* empty */ { $$ = 0; } 3417 | FLUSH { $$ = PF_FLUSH; } 3418 | FLUSH GLOBAL { 3419 $$ = PF_FLUSH | PF_FLUSH_GLOBAL; 3420 } 3421 ; 3422 3423state_opt_spec : '(' state_opt_list ')' { $$ = $2; } 3424 | /* empty */ { $$ = NULL; } 3425 ; 3426 3427state_opt_list : state_opt_item { $$ = $1; } 3428 | state_opt_list comma state_opt_item { 3429 $1->tail->next = $3; 3430 $1->tail = $3; 3431 $$ = $1; 3432 } 3433 ; 3434 3435state_opt_item : MAXIMUM NUMBER { 3436 if ($2 < 0 || $2 > UINT_MAX) { 3437 yyerror("only positive values permitted"); 3438 YYERROR; 3439 } 3440 $$ = calloc(1, sizeof(struct node_state_opt)); 3441 if ($$ == NULL) 3442 err(1, "state_opt_item: calloc"); 3443 $$->type = PF_STATE_OPT_MAX; 3444 $$->data.max_states = $2; 3445 $$->next = NULL; 3446 $$->tail = $$; 3447 } 3448 | NOSYNC { 3449 $$ = calloc(1, sizeof(struct node_state_opt)); 3450 if ($$ == NULL) 3451 err(1, "state_opt_item: calloc"); 3452 $$->type = PF_STATE_OPT_NOSYNC; 3453 $$->next = NULL; 3454 $$->tail = $$; 3455 } 3456 | MAXSRCSTATES NUMBER { 3457 if ($2 < 0 || $2 > UINT_MAX) { 3458 yyerror("only positive values permitted"); 3459 YYERROR; 3460 } 3461 $$ = calloc(1, sizeof(struct node_state_opt)); 3462 if ($$ == NULL) 3463 err(1, "state_opt_item: calloc"); 3464 $$->type = PF_STATE_OPT_MAX_SRC_STATES; 3465 $$->data.max_src_states = $2; 3466 $$->next = NULL; 3467 $$->tail = $$; 3468 } 3469 | MAXSRCCONN NUMBER { 3470 if ($2 < 0 || $2 > UINT_MAX) { 3471 yyerror("only positive values permitted"); 3472 YYERROR; 3473 } 3474 $$ = calloc(1, sizeof(struct node_state_opt)); 3475 if ($$ == NULL) 3476 err(1, "state_opt_item: calloc"); 3477 $$->type = PF_STATE_OPT_MAX_SRC_CONN; 3478 $$->data.max_src_conn = $2; 3479 $$->next = NULL; 3480 $$->tail = $$; 3481 } 3482 | MAXSRCCONNRATE NUMBER '/' NUMBER { 3483 if ($2 < 0 || $2 > UINT_MAX || 3484 $4 < 0 || $4 > UINT_MAX) { 3485 yyerror("only positive values permitted"); 3486 YYERROR; 3487 } 3488 $$ = calloc(1, sizeof(struct node_state_opt)); 3489 if ($$ == NULL) 3490 err(1, "state_opt_item: calloc"); 3491 $$->type = PF_STATE_OPT_MAX_SRC_CONN_RATE; 3492 $$->data.max_src_conn_rate.limit = $2; 3493 $$->data.max_src_conn_rate.seconds = $4; 3494 $$->next = NULL; 3495 $$->tail = $$; 3496 } 3497 | OVERLOAD '<' STRING '>' flush { 3498 if (strlen($3) >= PF_TABLE_NAME_SIZE) { 3499 yyerror("table name '%s' too long", $3); 3500 free($3); 3501 YYERROR; 3502 } 3503 $$ = calloc(1, sizeof(struct node_state_opt)); 3504 if ($$ == NULL) 3505 err(1, "state_opt_item: calloc"); 3506 if (strlcpy($$->data.overload.tblname, $3, 3507 PF_TABLE_NAME_SIZE) >= PF_TABLE_NAME_SIZE) 3508 errx(1, "state_opt_item: strlcpy"); 3509 free($3); 3510 $$->type = PF_STATE_OPT_OVERLOAD; 3511 $$->data.overload.flush = $5; 3512 $$->next = NULL; 3513 $$->tail = $$; 3514 } 3515 | MAXSRCNODES NUMBER { 3516 if ($2 < 0 || $2 > UINT_MAX) { 3517 yyerror("only positive values permitted"); 3518 YYERROR; 3519 } 3520 $$ = calloc(1, sizeof(struct node_state_opt)); 3521 if ($$ == NULL) 3522 err(1, "state_opt_item: calloc"); 3523 $$->type = PF_STATE_OPT_MAX_SRC_NODES; 3524 $$->data.max_src_nodes = $2; 3525 $$->next = NULL; 3526 $$->tail = $$; 3527 } 3528 | sourcetrack { 3529 $$ = calloc(1, sizeof(struct node_state_opt)); 3530 if ($$ == NULL) 3531 err(1, "state_opt_item: calloc"); 3532 $$->type = PF_STATE_OPT_SRCTRACK; 3533 $$->data.src_track = $1; 3534 $$->next = NULL; 3535 $$->tail = $$; 3536 } 3537 | statelock { 3538 $$ = calloc(1, sizeof(struct node_state_opt)); 3539 if ($$ == NULL) 3540 err(1, "state_opt_item: calloc"); 3541 $$->type = PF_STATE_OPT_STATELOCK; 3542 $$->data.statelock = $1; 3543 $$->next = NULL; 3544 $$->tail = $$; 3545 } 3546 | SLOPPY { 3547 $$ = calloc(1, sizeof(struct node_state_opt)); 3548 if ($$ == NULL) 3549 err(1, "state_opt_item: calloc"); 3550 $$->type = PF_STATE_OPT_SLOPPY; 3551 $$->next = NULL; 3552 $$->tail = $$; 3553 } 3554 | PFLOW { 3555 $$ = calloc(1, sizeof(struct node_state_opt)); 3556 if ($$ == NULL) 3557 err(1, "state_opt_item: calloc"); 3558 $$->type = PF_STATE_OPT_PFLOW; 3559 $$->next = NULL; 3560 $$->tail = $$; 3561 } 3562 | STRING NUMBER { 3563 int i; 3564 3565 if ($2 < 0 || $2 > UINT_MAX) { 3566 yyerror("only positive values permitted"); 3567 YYERROR; 3568 } 3569 for (i = 0; pf_timeouts[i].name && 3570 strcmp(pf_timeouts[i].name, $1); ++i) 3571 ; /* nothing */ 3572 if (!pf_timeouts[i].name) { 3573 yyerror("illegal timeout name %s", $1); 3574 free($1); 3575 YYERROR; 3576 } 3577 if (strchr(pf_timeouts[i].name, '.') == NULL) { 3578 yyerror("illegal state timeout %s", $1); 3579 free($1); 3580 YYERROR; 3581 } 3582 free($1); 3583 $$ = calloc(1, sizeof(struct node_state_opt)); 3584 if ($$ == NULL) 3585 err(1, "state_opt_item: calloc"); 3586 $$->type = PF_STATE_OPT_TIMEOUT; 3587 $$->data.timeout.number = pf_timeouts[i].timeout; 3588 $$->data.timeout.seconds = $2; 3589 $$->next = NULL; 3590 $$->tail = $$; 3591 } 3592 ; 3593 3594label : LABEL STRING { 3595 $$ = $2; 3596 } 3597 ; 3598 3599qname : QUEUE STRING { 3600 $$.qname = $2; 3601 $$.pqname = NULL; 3602 } 3603 | QUEUE '(' STRING ')' { 3604 $$.qname = $3; 3605 $$.pqname = NULL; 3606 } 3607 | QUEUE '(' STRING comma STRING ')' { 3608 $$.qname = $3; 3609 $$.pqname = $5; 3610 } 3611 ; 3612 3613no : /* empty */ { $$ = 0; } 3614 | NO { $$ = 1; } 3615 ; 3616 3617portstar : numberstring { 3618 if (parseport($1, &$$, PPORT_RANGE|PPORT_STAR) == -1) { 3619 free($1); 3620 YYERROR; 3621 } 3622 free($1); 3623 } 3624 ; 3625 3626redirspec : host { $$ = $1; } 3627 | '{' optnl redir_host_list '}' { $$ = $3; } 3628 ; 3629 3630redir_host_list : host optnl { $$ = $1; } 3631 | redir_host_list comma host optnl { 3632 $1->tail->next = $3; 3633 $1->tail = $3->tail; 3634 $$ = $1; 3635 } 3636 ; 3637 3638redirpool : /* empty */ { $$ = NULL; } 3639 | ARROW redirspec { 3640 $$ = calloc(1, sizeof(struct redirection)); 3641 if ($$ == NULL) 3642 err(1, "redirection: calloc"); 3643 $$->host = $2; 3644 $$->rport.a = $$->rport.b = $$->rport.t = 0; 3645 } 3646 | ARROW redirspec PORT portstar { 3647 $$ = calloc(1, sizeof(struct redirection)); 3648 if ($$ == NULL) 3649 err(1, "redirection: calloc"); 3650 $$->host = $2; 3651 $$->rport = $4; 3652 } 3653 ; 3654 3655hashkey : /* empty */ 3656 { 3657 $$ = calloc(1, sizeof(struct pf_poolhashkey)); 3658 if ($$ == NULL) 3659 err(1, "hashkey: calloc"); 3660 $$->key32[0] = arc4random(); 3661 $$->key32[1] = arc4random(); 3662 $$->key32[2] = arc4random(); 3663 $$->key32[3] = arc4random(); 3664 } 3665 | string 3666 { 3667 if (!strncmp($1, "0x", 2)) { 3668 if (strlen($1) != 34) { 3669 free($1); 3670 yyerror("hex key must be 128 bits " 3671 "(32 hex digits) long"); 3672 YYERROR; 3673 } 3674 $$ = calloc(1, sizeof(struct pf_poolhashkey)); 3675 if ($$ == NULL) 3676 err(1, "hashkey: calloc"); 3677 3678 if (sscanf($1, "0x%8x%8x%8x%8x", 3679 &$$->key32[0], &$$->key32[1], 3680 &$$->key32[2], &$$->key32[3]) != 4) { 3681 free($$); 3682 free($1); 3683 yyerror("invalid hex key"); 3684 YYERROR; 3685 } 3686 } else { 3687 MD5_CTX context; 3688 3689 $$ = calloc(1, sizeof(struct pf_poolhashkey)); 3690 if ($$ == NULL) 3691 err(1, "hashkey: calloc"); 3692 MD5Init(&context); 3693 MD5Update(&context, (unsigned char *)$1, 3694 strlen($1)); 3695 MD5Final((unsigned char *)$$, &context); 3696 HTONL($$->key32[0]); 3697 HTONL($$->key32[1]); 3698 HTONL($$->key32[2]); 3699 HTONL($$->key32[3]); 3700 } 3701 free($1); 3702 } 3703 ; 3704 3705pool_opts : { bzero(&pool_opts, sizeof pool_opts); } 3706 pool_opts_l 3707 { $$ = pool_opts; } 3708 | /* empty */ { 3709 bzero(&pool_opts, sizeof pool_opts); 3710 $$ = pool_opts; 3711 } 3712 ; 3713 3714pool_opts_l : pool_opts_l pool_opt 3715 | pool_opt 3716 ; 3717 3718pool_opt : BITMASK { 3719 if (pool_opts.type) { 3720 yyerror("pool type cannot be redefined"); 3721 YYERROR; 3722 } 3723 pool_opts.type = PF_POOL_BITMASK; 3724 } 3725 | RANDOM { 3726 if (pool_opts.type) { 3727 yyerror("pool type cannot be redefined"); 3728 YYERROR; 3729 } 3730 pool_opts.type = PF_POOL_RANDOM; 3731 } 3732 | SOURCEHASH hashkey { 3733 if (pool_opts.type) { 3734 yyerror("pool type cannot be redefined"); 3735 YYERROR; 3736 } 3737 pool_opts.type = PF_POOL_SRCHASH; 3738 pool_opts.key = $2; 3739 } 3740 | ROUNDROBIN { 3741 if (pool_opts.type) { 3742 yyerror("pool type cannot be redefined"); 3743 YYERROR; 3744 } 3745 pool_opts.type = PF_POOL_ROUNDROBIN; 3746 } 3747 | STATICPORT { 3748 if (pool_opts.staticport) { 3749 yyerror("static-port cannot be redefined"); 3750 YYERROR; 3751 } 3752 pool_opts.staticport = 1; 3753 } 3754 | STICKYADDRESS { 3755 if (filter_opts.marker & POM_STICKYADDRESS) { 3756 yyerror("sticky-address cannot be redefined"); 3757 YYERROR; 3758 } 3759 pool_opts.marker |= POM_STICKYADDRESS; 3760 pool_opts.opts |= PF_POOL_STICKYADDR; 3761 } 3762 ; 3763 3764redirection : /* empty */ { $$ = NULL; } 3765 | ARROW host { 3766 $$ = calloc(1, sizeof(struct redirection)); 3767 if ($$ == NULL) 3768 err(1, "redirection: calloc"); 3769 $$->host = $2; 3770 $$->rport.a = $$->rport.b = $$->rport.t = 0; 3771 } 3772 | ARROW host PORT portstar { 3773 $$ = calloc(1, sizeof(struct redirection)); 3774 if ($$ == NULL) 3775 err(1, "redirection: calloc"); 3776 $$->host = $2; 3777 $$->rport = $4; 3778 } 3779 ; 3780 3781natpasslog : /* empty */ { $$.b1 = $$.b2 = 0; $$.w2 = 0; } 3782 | PASS { $$.b1 = 1; $$.b2 = 0; $$.w2 = 0; } 3783 | PASS log { $$.b1 = 1; $$.b2 = $2.log; $$.w2 = $2.logif; } 3784 | log { $$.b1 = 0; $$.b2 = $1.log; $$.w2 = $1.logif; } 3785 ; 3786 3787nataction : no NAT natpasslog { 3788 if ($1 && $3.b1) { 3789 yyerror("\"pass\" not valid with \"no\""); 3790 YYERROR; 3791 } 3792 if ($1) 3793 $$.b1 = PF_NONAT; 3794 else 3795 $$.b1 = PF_NAT; 3796 $$.b2 = $3.b1; 3797 $$.w = $3.b2; 3798 $$.w2 = $3.w2; 3799 } 3800 | no RDR natpasslog { 3801 if ($1 && $3.b1) { 3802 yyerror("\"pass\" not valid with \"no\""); 3803 YYERROR; 3804 } 3805 if ($1) 3806 $$.b1 = PF_NORDR; 3807 else 3808 $$.b1 = PF_RDR; 3809 $$.b2 = $3.b1; 3810 $$.w = $3.b2; 3811 $$.w2 = $3.w2; 3812 } 3813 ; 3814 3815natrule : nataction interface af proto fromto tag tagged rtable 3816 redirpool pool_opts 3817 { 3818 struct pf_rule r; 3819 3820 if (check_rulestate(PFCTL_STATE_NAT)) 3821 YYERROR; 3822 3823 memset(&r, 0, sizeof(r)); 3824 3825 r.action = $1.b1; 3826 r.natpass = $1.b2; 3827 r.log = $1.w; 3828 r.logif = $1.w2; 3829 r.af = $3; 3830 3831 if (!r.af) { 3832 if ($5.src.host && $5.src.host->af && 3833 !$5.src.host->ifindex) 3834 r.af = $5.src.host->af; 3835 else if ($5.dst.host && $5.dst.host->af && 3836 !$5.dst.host->ifindex) 3837 r.af = $5.dst.host->af; 3838 } 3839 3840 if ($6 != NULL) 3841 if (strlcpy(r.tagname, $6, PF_TAG_NAME_SIZE) >= 3842 PF_TAG_NAME_SIZE) { 3843 yyerror("tag too long, max %u chars", 3844 PF_TAG_NAME_SIZE - 1); 3845 YYERROR; 3846 } 3847 3848 if ($7.name) 3849 if (strlcpy(r.match_tagname, $7.name, 3850 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) { 3851 yyerror("tag too long, max %u chars", 3852 PF_TAG_NAME_SIZE - 1); 3853 YYERROR; 3854 } 3855 r.match_tag_not = $7.neg; 3856 r.rtableid = $8; 3857 3858 if (r.action == PF_NONAT || r.action == PF_NORDR) { 3859 if ($9 != NULL) { 3860 yyerror("translation rule with 'no' " 3861 "does not need '->'"); 3862 YYERROR; 3863 } 3864 } else { 3865 if ($9 == NULL || $9->host == NULL) { 3866 yyerror("translation rule requires '-> " 3867 "address'"); 3868 YYERROR; 3869 } 3870 if (!r.af && ! $9->host->ifindex) 3871 r.af = $9->host->af; 3872 3873 remove_invalid_hosts(&$9->host, &r.af); 3874 if (invalid_redirect($9->host, r.af)) 3875 YYERROR; 3876 if (check_netmask($9->host, r.af)) 3877 YYERROR; 3878 3879 r.rpool.proxy_port[0] = ntohs($9->rport.a); 3880 3881 switch (r.action) { 3882 case PF_RDR: 3883 if (!$9->rport.b && $9->rport.t && 3884 $5.dst.port != NULL) { 3885 r.rpool.proxy_port[1] = 3886 ntohs($9->rport.a) + 3887 (ntohs( 3888 $5.dst.port->port[1]) - 3889 ntohs( 3890 $5.dst.port->port[0])); 3891 } else 3892 r.rpool.proxy_port[1] = 3893 ntohs($9->rport.b); 3894 break; 3895 case PF_NAT: 3896 r.rpool.proxy_port[1] = 3897 ntohs($9->rport.b); 3898 if (!r.rpool.proxy_port[0] && 3899 !r.rpool.proxy_port[1]) { 3900 r.rpool.proxy_port[0] = 3901 PF_NAT_PROXY_PORT_LOW; 3902 r.rpool.proxy_port[1] = 3903 PF_NAT_PROXY_PORT_HIGH; 3904 } else if (!r.rpool.proxy_port[1]) 3905 r.rpool.proxy_port[1] = 3906 r.rpool.proxy_port[0]; 3907 break; 3908 default: 3909 break; 3910 } 3911 3912 r.rpool.opts = $10.type; 3913 if ((r.rpool.opts & PF_POOL_TYPEMASK) == 3914 PF_POOL_NONE && ($9->host->next != NULL || 3915 $9->host->addr.type == PF_ADDR_TABLE || 3916 DYNIF_MULTIADDR($9->host->addr))) 3917 r.rpool.opts = PF_POOL_ROUNDROBIN; 3918 if ((r.rpool.opts & PF_POOL_TYPEMASK) != 3919 PF_POOL_ROUNDROBIN && 3920 disallow_table($9->host, "tables are only " 3921 "supported in round-robin redirection " 3922 "pools")) 3923 YYERROR; 3924 if ((r.rpool.opts & PF_POOL_TYPEMASK) != 3925 PF_POOL_ROUNDROBIN && 3926 disallow_alias($9->host, "interface (%s) " 3927 "is only supported in round-robin " 3928 "redirection pools")) 3929 YYERROR; 3930 if ($9->host->next != NULL) { 3931 if ((r.rpool.opts & PF_POOL_TYPEMASK) != 3932 PF_POOL_ROUNDROBIN) { 3933 yyerror("only round-robin " 3934 "valid for multiple " 3935 "redirection addresses"); 3936 YYERROR; 3937 } 3938 } 3939 } 3940 3941 if ($10.key != NULL) 3942 memcpy(&r.rpool.key, $10.key, 3943 sizeof(struct pf_poolhashkey)); 3944 3945 if ($10.opts) 3946 r.rpool.opts |= $10.opts; 3947 3948 if ($10.staticport) { 3949 if (r.action != PF_NAT) { 3950 yyerror("the 'static-port' option is " 3951 "only valid with nat rules"); 3952 YYERROR; 3953 } 3954 if (r.rpool.proxy_port[0] != 3955 PF_NAT_PROXY_PORT_LOW && 3956 r.rpool.proxy_port[1] != 3957 PF_NAT_PROXY_PORT_HIGH) { 3958 yyerror("the 'static-port' option can't" 3959 " be used when specifying a port" 3960 " range"); 3961 YYERROR; 3962 } 3963 r.rpool.proxy_port[0] = 0; 3964 r.rpool.proxy_port[1] = 0; 3965 } 3966 3967 expand_rule(&r, $2, $9 == NULL ? NULL : $9->host, $4, 3968 $5.src_os, $5.src.host, $5.src.port, $5.dst.host, 3969 $5.dst.port, 0, 0, 0, ""); 3970 free($9); 3971 } 3972 ; 3973 3974binatrule : no BINAT natpasslog interface af proto FROM host toipspec tag 3975 tagged rtable redirection 3976 { 3977 struct pf_rule binat; 3978 struct pf_pooladdr *pa; 3979 3980 if (check_rulestate(PFCTL_STATE_NAT)) 3981 YYERROR; 3982 if (disallow_urpf_failed($9, "\"urpf-failed\" is not " 3983 "permitted as a binat destination")) 3984 YYERROR; 3985 3986 memset(&binat, 0, sizeof(binat)); 3987 3988 if ($1 && $3.b1) { 3989 yyerror("\"pass\" not valid with \"no\""); 3990 YYERROR; 3991 } 3992 if ($1) 3993 binat.action = PF_NOBINAT; 3994 else 3995 binat.action = PF_BINAT; 3996 binat.natpass = $3.b1; 3997 binat.log = $3.b2; 3998 binat.logif = $3.w2; 3999 binat.af = $5; 4000 if (!binat.af && $8 != NULL && $8->af) 4001 binat.af = $8->af; 4002 if (!binat.af && $9 != NULL && $9->af) 4003 binat.af = $9->af; 4004 4005 if (!binat.af && $13 != NULL && $13->host) 4006 binat.af = $13->host->af; 4007 if (!binat.af) { 4008 yyerror("address family (inet/inet6) " 4009 "undefined"); 4010 YYERROR; 4011 } 4012 4013 if ($4 != NULL) { 4014 memcpy(binat.ifname, $4->ifname, 4015 sizeof(binat.ifname)); 4016 binat.ifnot = $4->not; 4017 free($4); 4018 } 4019 4020 if ($10 != NULL) 4021 if (strlcpy(binat.tagname, $10, 4022 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) { 4023 yyerror("tag too long, max %u chars", 4024 PF_TAG_NAME_SIZE - 1); 4025 YYERROR; 4026 } 4027 if ($11.name) 4028 if (strlcpy(binat.match_tagname, $11.name, 4029 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) { 4030 yyerror("tag too long, max %u chars", 4031 PF_TAG_NAME_SIZE - 1); 4032 YYERROR; 4033 } 4034 binat.match_tag_not = $11.neg; 4035 binat.rtableid = $12; 4036 4037 if ($6 != NULL) { 4038 binat.proto = $6->proto; 4039 free($6); 4040 } 4041 4042 if ($8 != NULL && disallow_table($8, "invalid use of " 4043 "table <%s> as the source address of a binat rule")) 4044 YYERROR; 4045 if ($8 != NULL && disallow_alias($8, "invalid use of " 4046 "interface (%s) as the source address of a binat " 4047 "rule")) 4048 YYERROR; 4049 if ($13 != NULL && $13->host != NULL && disallow_table( 4050 $13->host, "invalid use of table <%s> as the " 4051 "redirect address of a binat rule")) 4052 YYERROR; 4053 if ($13 != NULL && $13->host != NULL && disallow_alias( 4054 $13->host, "invalid use of interface (%s) as the " 4055 "redirect address of a binat rule")) 4056 YYERROR; 4057 4058 if ($8 != NULL) { 4059 if ($8->next) { 4060 yyerror("multiple binat ip addresses"); 4061 YYERROR; 4062 } 4063 if ($8->addr.type == PF_ADDR_DYNIFTL) 4064 $8->af = binat.af; 4065 if ($8->af != binat.af) { 4066 yyerror("binat ip versions must match"); 4067 YYERROR; 4068 } 4069 if (check_netmask($8, binat.af)) 4070 YYERROR; 4071 memcpy(&binat.src.addr, &$8->addr, 4072 sizeof(binat.src.addr)); 4073 free($8); 4074 } 4075 if ($9 != NULL) { 4076 if ($9->next) { 4077 yyerror("multiple binat ip addresses"); 4078 YYERROR; 4079 } 4080 if ($9->af != binat.af && $9->af) { 4081 yyerror("binat ip versions must match"); 4082 YYERROR; 4083 } 4084 if (check_netmask($9, binat.af)) 4085 YYERROR; 4086 memcpy(&binat.dst.addr, &$9->addr, 4087 sizeof(binat.dst.addr)); 4088 binat.dst.neg = $9->not; 4089 free($9); 4090 } 4091 4092 if (binat.action == PF_NOBINAT) { 4093 if ($13 != NULL) { 4094 yyerror("'no binat' rule does not need" 4095 " '->'"); 4096 YYERROR; 4097 } 4098 } else { 4099 if ($13 == NULL || $13->host == NULL) { 4100 yyerror("'binat' rule requires" 4101 " '-> address'"); 4102 YYERROR; 4103 } 4104 4105 remove_invalid_hosts(&$13->host, &binat.af); 4106 if (invalid_redirect($13->host, binat.af)) 4107 YYERROR; 4108 if ($13->host->next != NULL) { 4109 yyerror("binat rule must redirect to " 4110 "a single address"); 4111 YYERROR; 4112 } 4113 if (check_netmask($13->host, binat.af)) 4114 YYERROR; 4115 4116 if (!PF_AZERO(&binat.src.addr.v.a.mask, 4117 binat.af) && 4118 !PF_AEQ(&binat.src.addr.v.a.mask, 4119 &$13->host->addr.v.a.mask, binat.af)) { 4120 yyerror("'binat' source mask and " 4121 "redirect mask must be the same"); 4122 YYERROR; 4123 } 4124 4125 TAILQ_INIT(&binat.rpool.list); 4126 pa = calloc(1, sizeof(struct pf_pooladdr)); 4127 if (pa == NULL) 4128 err(1, "binat: calloc"); 4129 pa->addr = $13->host->addr; 4130 pa->ifname[0] = 0; 4131 TAILQ_INSERT_TAIL(&binat.rpool.list, 4132 pa, entries); 4133 4134 free($13); 4135 } 4136 4137 pfctl_add_rule(pf, &binat, ""); 4138 } 4139 ; 4140 4141tag : /* empty */ { $$ = NULL; } 4142 | TAG STRING { $$ = $2; } 4143 ; 4144 4145tagged : /* empty */ { $$.neg = 0; $$.name = NULL; } 4146 | not TAGGED string { $$.neg = $1; $$.name = $3; } 4147 ; 4148 4149rtable : /* empty */ { $$ = -1; } 4150 | RTABLE NUMBER { 4151 if ($2 < 0 || $2 > RT_TABLEID_MAX) { 4152 yyerror("invalid rtable id"); 4153 YYERROR; 4154 } 4155 $$ = $2; 4156 } 4157 ; 4158 4159route_host : STRING { 4160 $$ = calloc(1, sizeof(struct node_host)); 4161 if ($$ == NULL) 4162 err(1, "route_host: calloc"); 4163 $$->ifname = $1; 4164 set_ipmask($$, 128); 4165 $$->next = NULL; 4166 $$->tail = $$; 4167 } 4168 | '(' STRING host ')' { 4169 $$ = $3; 4170 $$->ifname = $2; 4171 } 4172 ; 4173 4174route_host_list : route_host optnl { $$ = $1; } 4175 | route_host_list comma route_host optnl { 4176 if ($1->af == 0) 4177 $1->af = $3->af; 4178 if ($1->af != $3->af) { 4179 yyerror("all pool addresses must be in the " 4180 "same address family"); 4181 YYERROR; 4182 } 4183 $1->tail->next = $3; 4184 $1->tail = $3->tail; 4185 $$ = $1; 4186 } 4187 ; 4188 4189routespec : route_host { $$ = $1; } 4190 | '{' optnl route_host_list '}' { $$ = $3; } 4191 ; 4192 4193route : /* empty */ { 4194 $$.host = NULL; 4195 $$.rt = 0; 4196 $$.pool_opts = 0; 4197 } 4198 | FASTROUTE { 4199 $$.host = NULL; 4200 $$.rt = PF_FASTROUTE; 4201 $$.pool_opts = 0; 4202 } 4203 | ROUTETO routespec pool_opts { 4204 $$.host = $2; 4205 $$.rt = PF_ROUTETO; 4206 $$.pool_opts = $3.type | $3.opts; 4207 if ($3.key != NULL) 4208 $$.key = $3.key; 4209 } 4210 | REPLYTO routespec pool_opts { 4211 $$.host = $2; 4212 $$.rt = PF_REPLYTO; 4213 $$.pool_opts = $3.type | $3.opts; 4214 if ($3.key != NULL) 4215 $$.key = $3.key; 4216 } 4217 | DUPTO routespec pool_opts { 4218 $$.host = $2; 4219 $$.rt = PF_DUPTO; 4220 $$.pool_opts = $3.type | $3.opts; 4221 if ($3.key != NULL) 4222 $$.key = $3.key; 4223 } 4224 ; 4225 4226timeout_spec : STRING NUMBER 4227 { 4228 if (check_rulestate(PFCTL_STATE_OPTION)) { 4229 free($1); 4230 YYERROR; 4231 } 4232 if ($2 < 0 || $2 > UINT_MAX) { 4233 yyerror("only positive values permitted"); 4234 YYERROR; 4235 } 4236 if (pfctl_set_timeout(pf, $1, $2, 0) != 0) { 4237 yyerror("unknown timeout %s", $1); 4238 free($1); 4239 YYERROR; 4240 } 4241 free($1); 4242 } 4243 ; 4244 4245timeout_list : timeout_list comma timeout_spec optnl 4246 | timeout_spec optnl 4247 ; 4248 4249limit_spec : STRING NUMBER 4250 { 4251 if (check_rulestate(PFCTL_STATE_OPTION)) { 4252 free($1); 4253 YYERROR; 4254 } 4255 if ($2 < 0 || $2 > UINT_MAX) { 4256 yyerror("only positive values permitted"); 4257 YYERROR; 4258 } 4259 if (pfctl_set_limit(pf, $1, $2) != 0) { 4260 yyerror("unable to set limit %s %u", $1, $2); 4261 free($1); 4262 YYERROR; 4263 } 4264 free($1); 4265 } 4266 ; 4267 4268limit_list : limit_list comma limit_spec optnl 4269 | limit_spec optnl 4270 ; 4271 4272comma : ',' 4273 | /* empty */ 4274 ; 4275 4276yesno : NO { $$ = 0; } 4277 | STRING { 4278 if (!strcmp($1, "yes")) 4279 $$ = 1; 4280 else { 4281 yyerror("invalid value '%s', expected 'yes' " 4282 "or 'no'", $1); 4283 free($1); 4284 YYERROR; 4285 } 4286 free($1); 4287 } 4288 ; 4289 4290unaryop : '=' { $$ = PF_OP_EQ; } 4291 | '!' '=' { $$ = PF_OP_NE; } 4292 | '<' '=' { $$ = PF_OP_LE; } 4293 | '<' { $$ = PF_OP_LT; } 4294 | '>' '=' { $$ = PF_OP_GE; } 4295 | '>' { $$ = PF_OP_GT; } 4296 ; 4297 4298%% 4299 4300int 4301yyerror(const char *fmt, ...) 4302{ 4303 va_list ap; 4304 4305 file->errors++; 4306 va_start(ap, fmt); 4307 fprintf(stderr, "%s:%d: ", file->name, yylval.lineno); 4308 vfprintf(stderr, fmt, ap); 4309 fprintf(stderr, "\n"); 4310 va_end(ap); 4311 return (0); 4312} 4313 4314int 4315disallow_table(struct node_host *h, const char *fmt) 4316{ 4317 for (; h != NULL; h = h->next) 4318 if (h->addr.type == PF_ADDR_TABLE) { 4319 yyerror(fmt, h->addr.v.tblname); 4320 return (1); 4321 } 4322 return (0); 4323} 4324 4325int 4326disallow_urpf_failed(struct node_host *h, const char *fmt) 4327{ 4328 for (; h != NULL; h = h->next) 4329 if (h->addr.type == PF_ADDR_URPFFAILED) { 4330 yyerror(fmt); 4331 return (1); 4332 } 4333 return (0); 4334} 4335 4336int 4337disallow_alias(struct node_host *h, const char *fmt) 4338{ 4339 for (; h != NULL; h = h->next) 4340 if (DYNIF_MULTIADDR(h->addr)) { 4341 yyerror(fmt, h->addr.v.tblname); 4342 return (1); 4343 } 4344 return (0); 4345} 4346 4347int 4348rule_consistent(struct pf_rule *r, int anchor_call) 4349{ 4350 int problems = 0; 4351 4352 switch (r->action) { 4353 case PF_PASS: 4354 case PF_DROP: 4355 case PF_SCRUB: 4356 case PF_NOSCRUB: 4357 problems = filter_consistent(r, anchor_call); 4358 break; 4359 case PF_NAT: 4360 case PF_NONAT: 4361 problems = nat_consistent(r); 4362 break; 4363 case PF_RDR: 4364 case PF_NORDR: 4365 problems = rdr_consistent(r); 4366 break; 4367 case PF_BINAT: 4368 case PF_NOBINAT: 4369 default: 4370 break; 4371 } 4372 return (problems); 4373} 4374 4375int 4376filter_consistent(struct pf_rule *r, int anchor_call) 4377{ 4378 int problems = 0; 4379 4380 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP && 4381 (r->src.port_op || r->dst.port_op)) { 4382 yyerror("port only applies to tcp/udp"); 4383 problems++; 4384 } 4385 if (r->proto != IPPROTO_ICMP && r->proto != IPPROTO_ICMPV6 && 4386 (r->type || r->code)) { 4387 yyerror("icmp-type/code only applies to icmp"); 4388 problems++; 4389 } 4390 if (!r->af && (r->type || r->code)) { 4391 yyerror("must indicate address family with icmp-type/code"); 4392 problems++; 4393 } 4394 if (r->overload_tblname[0] && 4395 r->max_src_conn == 0 && r->max_src_conn_rate.seconds == 0) { 4396 yyerror("'overload' requires 'max-src-conn' " 4397 "or 'max-src-conn-rate'"); 4398 problems++; 4399 } 4400 if ((r->proto == IPPROTO_ICMP && r->af == AF_INET6) || 4401 (r->proto == IPPROTO_ICMPV6 && r->af == AF_INET)) { 4402 yyerror("proto %s doesn't match address family %s", 4403 r->proto == IPPROTO_ICMP ? "icmp" : "icmp6", 4404 r->af == AF_INET ? "inet" : "inet6"); 4405 problems++; 4406 } 4407 if (r->allow_opts && r->action != PF_PASS) { 4408 yyerror("allow-opts can only be specified for pass rules"); 4409 problems++; 4410 } 4411 if (r->rule_flag & PFRULE_FRAGMENT && (r->src.port_op || 4412 r->dst.port_op || r->flagset || r->type || r->code)) { 4413 yyerror("fragments can be filtered only on IP header fields"); 4414 problems++; 4415 } 4416 if (r->rule_flag & PFRULE_RETURNRST && r->proto != IPPROTO_TCP) { 4417 yyerror("return-rst can only be applied to TCP rules"); 4418 problems++; 4419 } 4420 if (r->max_src_nodes && !(r->rule_flag & PFRULE_RULESRCTRACK)) { 4421 yyerror("max-src-nodes requires 'source-track rule'"); 4422 problems++; 4423 } 4424 if (r->action == PF_DROP && r->keep_state) { 4425 yyerror("keep state on block rules doesn't make sense"); 4426 problems++; 4427 } 4428 if (r->rule_flag & PFRULE_STATESLOPPY && 4429 (r->keep_state == PF_STATE_MODULATE || 4430 r->keep_state == PF_STATE_SYNPROXY)) { 4431 yyerror("sloppy state matching cannot be used with " 4432 "synproxy state or modulate state"); 4433 problems++; 4434 } 4435 return (-problems); 4436} 4437 4438int 4439nat_consistent(struct pf_rule *r) 4440{ 4441 return (0); /* yeah! */ 4442} 4443 4444int 4445rdr_consistent(struct pf_rule *r) 4446{ 4447 int problems = 0; 4448 4449 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP) { 4450 if (r->src.port_op) { 4451 yyerror("src port only applies to tcp/udp"); 4452 problems++; 4453 } 4454 if (r->dst.port_op) { 4455 yyerror("dst port only applies to tcp/udp"); 4456 problems++; 4457 } 4458 if (r->rpool.proxy_port[0]) { 4459 yyerror("rpool port only applies to tcp/udp"); 4460 problems++; 4461 } 4462 } 4463 if (r->dst.port_op && 4464 r->dst.port_op != PF_OP_EQ && r->dst.port_op != PF_OP_RRG) { 4465 yyerror("invalid port operator for rdr destination port"); 4466 problems++; 4467 } 4468 return (-problems); 4469} 4470 4471int 4472process_tabledef(char *name, struct table_opts *opts) 4473{ 4474 struct pfr_buffer ab; 4475 struct node_tinit *ti; 4476 4477 bzero(&ab, sizeof(ab)); 4478 ab.pfrb_type = PFRB_ADDRS; 4479 SIMPLEQ_FOREACH(ti, &opts->init_nodes, entries) { 4480 if (ti->file) 4481 if (pfr_buf_load(&ab, ti->file, 0, append_addr)) { 4482 if (errno) 4483 yyerror("cannot load \"%s\": %s", 4484 ti->file, strerror(errno)); 4485 else 4486 yyerror("file \"%s\" contains bad data", 4487 ti->file); 4488 goto _error; 4489 } 4490 if (ti->host) 4491 if (append_addr_host(&ab, ti->host, 0, 0)) { 4492 yyerror("cannot create address buffer: %s", 4493 strerror(errno)); 4494 goto _error; 4495 } 4496 } 4497 if (pf->opts & PF_OPT_VERBOSE) 4498 print_tabledef(name, opts->flags, opts->init_addr, 4499 &opts->init_nodes); 4500 if (!(pf->opts & PF_OPT_NOACTION) && 4501 pfctl_define_table(name, opts->flags, opts->init_addr, 4502 pf->anchor->name, &ab, pf->anchor->ruleset.tticket)) { 4503 yyerror("cannot define table %s: %s", name, 4504 pfr_strerror(errno)); 4505 goto _error; 4506 } 4507 pf->tdirty = 1; 4508 pfr_buf_clear(&ab); 4509 return (0); 4510_error: 4511 pfr_buf_clear(&ab); 4512 return (-1); 4513} 4514 4515struct keywords { 4516 const char *k_name; 4517 int k_val; 4518}; 4519 4520/* macro gore, but you should've seen the prior indentation nightmare... */ 4521 4522#define FREE_LIST(T,r) \ 4523 do { \ 4524 T *p, *node = r; \ 4525 while (node != NULL) { \ 4526 p = node; \ 4527 node = node->next; \ 4528 free(p); \ 4529 } \ 4530 } while (0) 4531 4532#define LOOP_THROUGH(T,n,r,C) \ 4533 do { \ 4534 T *n; \ 4535 if (r == NULL) { \ 4536 r = calloc(1, sizeof(T)); \ 4537 if (r == NULL) \ 4538 err(1, "LOOP: calloc"); \ 4539 r->next = NULL; \ 4540 } \ 4541 n = r; \ 4542 while (n != NULL) { \ 4543 do { \ 4544 C; \ 4545 } while (0); \ 4546 n = n->next; \ 4547 } \ 4548 } while (0) 4549 4550void 4551expand_label_str(char *label, size_t len, const char *srch, const char *repl) 4552{ 4553 char *tmp; 4554 char *p, *q; 4555 4556 if ((tmp = calloc(1, len)) == NULL) 4557 err(1, "expand_label_str: calloc"); 4558 p = q = label; 4559 while ((q = strstr(p, srch)) != NULL) { 4560 *q = '\0'; 4561 if ((strlcat(tmp, p, len) >= len) || 4562 (strlcat(tmp, repl, len) >= len)) 4563 errx(1, "expand_label: label too long"); 4564 q += strlen(srch); 4565 p = q; 4566 } 4567 if (strlcat(tmp, p, len) >= len) 4568 errx(1, "expand_label: label too long"); 4569 strlcpy(label, tmp, len); /* always fits */ 4570 free(tmp); 4571} 4572 4573void 4574expand_label_if(const char *name, char *label, size_t len, const char *ifname) 4575{ 4576 if (strstr(label, name) != NULL) { 4577 if (!*ifname) 4578 expand_label_str(label, len, name, "any"); 4579 else 4580 expand_label_str(label, len, name, ifname); 4581 } 4582} 4583 4584void 4585expand_label_addr(const char *name, char *label, size_t len, sa_family_t af, 4586 struct node_host *h) 4587{ 4588 char tmp[64], tmp_not[66]; 4589 4590 if (strstr(label, name) != NULL) { 4591 switch (h->addr.type) { 4592 case PF_ADDR_DYNIFTL: 4593 snprintf(tmp, sizeof(tmp), "(%s)", h->addr.v.ifname); 4594 break; 4595 case PF_ADDR_TABLE: 4596 snprintf(tmp, sizeof(tmp), "<%s>", h->addr.v.tblname); 4597 break; 4598 case PF_ADDR_NOROUTE: 4599 snprintf(tmp, sizeof(tmp), "no-route"); 4600 break; 4601 case PF_ADDR_URPFFAILED: 4602 snprintf(tmp, sizeof(tmp), "urpf-failed"); 4603 break; 4604 case PF_ADDR_ADDRMASK: 4605 if (!af || (PF_AZERO(&h->addr.v.a.addr, af) && 4606 PF_AZERO(&h->addr.v.a.mask, af))) 4607 snprintf(tmp, sizeof(tmp), "any"); 4608 else { 4609 char a[48]; 4610 int bits; 4611 4612 if (inet_ntop(af, &h->addr.v.a.addr, a, 4613 sizeof(a)) == NULL) 4614 snprintf(tmp, sizeof(tmp), "?"); 4615 else { 4616 bits = unmask(&h->addr.v.a.mask, af); 4617 if ((af == AF_INET && bits < 32) || 4618 (af == AF_INET6 && bits < 128)) 4619 snprintf(tmp, sizeof(tmp), 4620 "%s/%d", a, bits); 4621 else 4622 snprintf(tmp, sizeof(tmp), 4623 "%s", a); 4624 } 4625 } 4626 break; 4627 default: 4628 snprintf(tmp, sizeof(tmp), "?"); 4629 break; 4630 } 4631 4632 if (h->not) { 4633 snprintf(tmp_not, sizeof(tmp_not), "! %s", tmp); 4634 expand_label_str(label, len, name, tmp_not); 4635 } else 4636 expand_label_str(label, len, name, tmp); 4637 } 4638} 4639 4640void 4641expand_label_port(const char *name, char *label, size_t len, 4642 struct node_port *port) 4643{ 4644 char a1[6], a2[6], op[13] = ""; 4645 4646 if (strstr(label, name) != NULL) { 4647 snprintf(a1, sizeof(a1), "%u", ntohs(port->port[0])); 4648 snprintf(a2, sizeof(a2), "%u", ntohs(port->port[1])); 4649 if (!port->op) 4650 ; 4651 else if (port->op == PF_OP_IRG) 4652 snprintf(op, sizeof(op), "%s><%s", a1, a2); 4653 else if (port->op == PF_OP_XRG) 4654 snprintf(op, sizeof(op), "%s<>%s", a1, a2); 4655 else if (port->op == PF_OP_EQ) 4656 snprintf(op, sizeof(op), "%s", a1); 4657 else if (port->op == PF_OP_NE) 4658 snprintf(op, sizeof(op), "!=%s", a1); 4659 else if (port->op == PF_OP_LT) 4660 snprintf(op, sizeof(op), "<%s", a1); 4661 else if (port->op == PF_OP_LE) 4662 snprintf(op, sizeof(op), "<=%s", a1); 4663 else if (port->op == PF_OP_GT) 4664 snprintf(op, sizeof(op), ">%s", a1); 4665 else if (port->op == PF_OP_GE) 4666 snprintf(op, sizeof(op), ">=%s", a1); 4667 expand_label_str(label, len, name, op); 4668 } 4669} 4670 4671void 4672expand_label_proto(const char *name, char *label, size_t len, u_int8_t proto) 4673{ 4674 struct protoent *pe; 4675 char n[4]; 4676 4677 if (strstr(label, name) != NULL) { 4678 pe = getprotobynumber(proto); 4679 if (pe != NULL) 4680 expand_label_str(label, len, name, pe->p_name); 4681 else { 4682 snprintf(n, sizeof(n), "%u", proto); 4683 expand_label_str(label, len, name, n); 4684 } 4685 } 4686} 4687 4688void 4689expand_label_nr(const char *name, char *label, size_t len) 4690{ 4691 char n[11]; 4692 4693 if (strstr(label, name) != NULL) { 4694 snprintf(n, sizeof(n), "%u", pf->anchor->match); 4695 expand_label_str(label, len, name, n); 4696 } 4697} 4698 4699void 4700expand_label(char *label, size_t len, const char *ifname, sa_family_t af, 4701 struct node_host *src_host, struct node_port *src_port, 4702 struct node_host *dst_host, struct node_port *dst_port, 4703 u_int8_t proto) 4704{ 4705 expand_label_if("$if", label, len, ifname); 4706 expand_label_addr("$srcaddr", label, len, af, src_host); 4707 expand_label_addr("$dstaddr", label, len, af, dst_host); 4708 expand_label_port("$srcport", label, len, src_port); 4709 expand_label_port("$dstport", label, len, dst_port); 4710 expand_label_proto("$proto", label, len, proto); 4711 expand_label_nr("$nr", label, len); 4712} 4713 4714int 4715expand_altq(struct pf_altq *a, struct node_if *interfaces, 4716 struct node_queue *nqueues, struct node_queue_bw bwspec, 4717 struct node_queue_opt *opts) 4718{ 4719 struct pf_altq pa, pb; 4720 char qname[PF_QNAME_SIZE]; 4721 struct node_queue *n; 4722 struct node_queue_bw bw; 4723 int errs = 0; 4724 4725 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) { 4726 FREE_LIST(struct node_if, interfaces); 4727 FREE_LIST(struct node_queue, nqueues); 4728 return (0); 4729 } 4730 4731 LOOP_THROUGH(struct node_if, interface, interfaces, 4732 memcpy(&pa, a, sizeof(struct pf_altq)); 4733 if (strlcpy(pa.ifname, interface->ifname, 4734 sizeof(pa.ifname)) >= sizeof(pa.ifname)) 4735 errx(1, "expand_altq: strlcpy"); 4736 4737 if (interface->not) { 4738 yyerror("altq on ! <interface> is not supported"); 4739 errs++; 4740 } else { 4741 if (eval_pfaltq(pf, &pa, &bwspec, opts)) 4742 errs++; 4743 else 4744 if (pfctl_add_altq(pf, &pa)) 4745 errs++; 4746 4747 if (pf->opts & PF_OPT_VERBOSE) { 4748 print_altq(&pf->paltq->altq, 0, 4749 &bwspec, opts); 4750 if (nqueues && nqueues->tail) { 4751 printf("queue { "); 4752 LOOP_THROUGH(struct node_queue, queue, 4753 nqueues, 4754 printf("%s ", 4755 queue->queue); 4756 ); 4757 printf("}"); 4758 } 4759 printf("\n"); 4760 } 4761 4762 if (pa.scheduler == ALTQT_CBQ || 4763 pa.scheduler == ALTQT_HFSC) { 4764 /* now create a root queue */ 4765 memset(&pb, 0, sizeof(struct pf_altq)); 4766 if (strlcpy(qname, "root_", sizeof(qname)) >= 4767 sizeof(qname)) 4768 errx(1, "expand_altq: strlcpy"); 4769 if (strlcat(qname, interface->ifname, 4770 sizeof(qname)) >= sizeof(qname)) 4771 errx(1, "expand_altq: strlcat"); 4772 if (strlcpy(pb.qname, qname, 4773 sizeof(pb.qname)) >= sizeof(pb.qname)) 4774 errx(1, "expand_altq: strlcpy"); 4775 if (strlcpy(pb.ifname, interface->ifname, 4776 sizeof(pb.ifname)) >= sizeof(pb.ifname)) 4777 errx(1, "expand_altq: strlcpy"); 4778 pb.qlimit = pa.qlimit; 4779 pb.scheduler = pa.scheduler; 4780 bw.bw_absolute = pa.ifbandwidth; 4781 bw.bw_percent = 0; 4782 if (eval_pfqueue(pf, &pb, &bw, opts)) 4783 errs++; 4784 else 4785 if (pfctl_add_altq(pf, &pb)) 4786 errs++; 4787 } 4788 4789 LOOP_THROUGH(struct node_queue, queue, nqueues, 4790 n = calloc(1, sizeof(struct node_queue)); 4791 if (n == NULL) 4792 err(1, "expand_altq: calloc"); 4793 if (pa.scheduler == ALTQT_CBQ || 4794 pa.scheduler == ALTQT_HFSC) 4795 if (strlcpy(n->parent, qname, 4796 sizeof(n->parent)) >= 4797 sizeof(n->parent)) 4798 errx(1, "expand_altq: strlcpy"); 4799 if (strlcpy(n->queue, queue->queue, 4800 sizeof(n->queue)) >= sizeof(n->queue)) 4801 errx(1, "expand_altq: strlcpy"); 4802 if (strlcpy(n->ifname, interface->ifname, 4803 sizeof(n->ifname)) >= sizeof(n->ifname)) 4804 errx(1, "expand_altq: strlcpy"); 4805 n->scheduler = pa.scheduler; 4806 n->next = NULL; 4807 n->tail = n; 4808 if (queues == NULL) 4809 queues = n; 4810 else { 4811 queues->tail->next = n; 4812 queues->tail = n; 4813 } 4814 ); 4815 } 4816 ); 4817 FREE_LIST(struct node_if, interfaces); 4818 FREE_LIST(struct node_queue, nqueues); 4819 4820 return (errs); 4821} 4822 4823int 4824expand_queue(struct pf_altq *a, struct node_if *interfaces, 4825 struct node_queue *nqueues, struct node_queue_bw bwspec, 4826 struct node_queue_opt *opts) 4827{ 4828 struct node_queue *n, *nq; 4829 struct pf_altq pa; 4830 u_int8_t found = 0; 4831 u_int8_t errs = 0; 4832 4833 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) { 4834 FREE_LIST(struct node_queue, nqueues); 4835 return (0); 4836 } 4837 4838 if (queues == NULL) { 4839 yyerror("queue %s has no parent", a->qname); 4840 FREE_LIST(struct node_queue, nqueues); 4841 return (1); 4842 } 4843 4844 LOOP_THROUGH(struct node_if, interface, interfaces, 4845 LOOP_THROUGH(struct node_queue, tqueue, queues, 4846 if (!strncmp(a->qname, tqueue->queue, PF_QNAME_SIZE) && 4847 (interface->ifname[0] == 0 || 4848 (!interface->not && !strncmp(interface->ifname, 4849 tqueue->ifname, IFNAMSIZ)) || 4850 (interface->not && strncmp(interface->ifname, 4851 tqueue->ifname, IFNAMSIZ)))) { 4852 /* found ourself in queues */ 4853 found++; 4854 4855 memcpy(&pa, a, sizeof(struct pf_altq)); 4856 4857 if (pa.scheduler != ALTQT_NONE && 4858 pa.scheduler != tqueue->scheduler) { 4859 yyerror("exactly one scheduler type " 4860 "per interface allowed"); 4861 return (1); 4862 } 4863 pa.scheduler = tqueue->scheduler; 4864 4865 /* scheduler dependent error checking */ 4866 switch (pa.scheduler) { 4867 case ALTQT_PRIQ: 4868 if (nqueues != NULL) { 4869 yyerror("priq queues cannot " 4870 "have child queues"); 4871 return (1); 4872 } 4873 if (bwspec.bw_absolute > 0 || 4874 bwspec.bw_percent < 100) { 4875 yyerror("priq doesn't take " 4876 "bandwidth"); 4877 return (1); 4878 } 4879 break; 4880 default: 4881 break; 4882 } 4883 4884 if (strlcpy(pa.ifname, tqueue->ifname, 4885 sizeof(pa.ifname)) >= sizeof(pa.ifname)) 4886 errx(1, "expand_queue: strlcpy"); 4887 if (strlcpy(pa.parent, tqueue->parent, 4888 sizeof(pa.parent)) >= sizeof(pa.parent)) 4889 errx(1, "expand_queue: strlcpy"); 4890 4891 if (eval_pfqueue(pf, &pa, &bwspec, opts)) 4892 errs++; 4893 else 4894 if (pfctl_add_altq(pf, &pa)) 4895 errs++; 4896 4897 for (nq = nqueues; nq != NULL; nq = nq->next) { 4898 if (!strcmp(a->qname, nq->queue)) { 4899 yyerror("queue cannot have " 4900 "itself as child"); 4901 errs++; 4902 continue; 4903 } 4904 n = calloc(1, 4905 sizeof(struct node_queue)); 4906 if (n == NULL) 4907 err(1, "expand_queue: calloc"); 4908 if (strlcpy(n->parent, a->qname, 4909 sizeof(n->parent)) >= 4910 sizeof(n->parent)) 4911 errx(1, "expand_queue strlcpy"); 4912 if (strlcpy(n->queue, nq->queue, 4913 sizeof(n->queue)) >= 4914 sizeof(n->queue)) 4915 errx(1, "expand_queue strlcpy"); 4916 if (strlcpy(n->ifname, tqueue->ifname, 4917 sizeof(n->ifname)) >= 4918 sizeof(n->ifname)) 4919 errx(1, "expand_queue strlcpy"); 4920 n->scheduler = tqueue->scheduler; 4921 n->next = NULL; 4922 n->tail = n; 4923 if (queues == NULL) 4924 queues = n; 4925 else { 4926 queues->tail->next = n; 4927 queues->tail = n; 4928 } 4929 } 4930 if ((pf->opts & PF_OPT_VERBOSE) && ( 4931 (found == 1 && interface->ifname[0] == 0) || 4932 (found > 0 && interface->ifname[0] != 0))) { 4933 print_queue(&pf->paltq->altq, 0, 4934 &bwspec, interface->ifname[0] != 0, 4935 opts); 4936 if (nqueues && nqueues->tail) { 4937 printf("{ "); 4938 LOOP_THROUGH(struct node_queue, 4939 queue, nqueues, 4940 printf("%s ", 4941 queue->queue); 4942 ); 4943 printf("}"); 4944 } 4945 printf("\n"); 4946 } 4947 } 4948 ); 4949 ); 4950 4951 FREE_LIST(struct node_queue, nqueues); 4952 FREE_LIST(struct node_if, interfaces); 4953 4954 if (!found) { 4955 yyerror("queue %s has no parent", a->qname); 4956 errs++; 4957 } 4958 4959 if (errs) 4960 return (1); 4961 else 4962 return (0); 4963} 4964 4965void 4966expand_rule(struct pf_rule *r, 4967 struct node_if *interfaces, struct node_host *rpool_hosts, 4968 struct node_proto *protos, struct node_os *src_oses, 4969 struct node_host *src_hosts, struct node_port *src_ports, 4970 struct node_host *dst_hosts, struct node_port *dst_ports, 4971 struct node_uid *uids, struct node_gid *gids, struct node_icmp *icmp_types, 4972 const char *anchor_call) 4973{ 4974 sa_family_t af = r->af; 4975 int added = 0, error = 0; 4976 char ifname[IF_NAMESIZE]; 4977 char label[PF_RULE_LABEL_SIZE]; 4978 char tagname[PF_TAG_NAME_SIZE]; 4979 char match_tagname[PF_TAG_NAME_SIZE]; 4980 struct pf_pooladdr *pa; 4981 struct node_host *h; 4982 u_int8_t flags, flagset, keep_state; 4983 4984 if (strlcpy(label, r->label, sizeof(label)) >= sizeof(label)) 4985 errx(1, "expand_rule: strlcpy"); 4986 if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname)) 4987 errx(1, "expand_rule: strlcpy"); 4988 if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >= 4989 sizeof(match_tagname)) 4990 errx(1, "expand_rule: strlcpy"); 4991 flags = r->flags; 4992 flagset = r->flagset; 4993 keep_state = r->keep_state; 4994 4995 LOOP_THROUGH(struct node_if, interface, interfaces, 4996 LOOP_THROUGH(struct node_proto, proto, protos, 4997 LOOP_THROUGH(struct node_icmp, icmp_type, icmp_types, 4998 LOOP_THROUGH(struct node_host, src_host, src_hosts, 4999 LOOP_THROUGH(struct node_port, src_port, src_ports, 5000 LOOP_THROUGH(struct node_os, src_os, src_oses, 5001 LOOP_THROUGH(struct node_host, dst_host, dst_hosts, 5002 LOOP_THROUGH(struct node_port, dst_port, dst_ports, 5003 LOOP_THROUGH(struct node_uid, uid, uids, 5004 LOOP_THROUGH(struct node_gid, gid, gids, 5005 5006 r->af = af; 5007 /* for link-local IPv6 address, interface must match up */ 5008 if ((r->af && src_host->af && r->af != src_host->af) || 5009 (r->af && dst_host->af && r->af != dst_host->af) || 5010 (src_host->af && dst_host->af && 5011 src_host->af != dst_host->af) || 5012 (src_host->ifindex && dst_host->ifindex && 5013 src_host->ifindex != dst_host->ifindex) || 5014 (src_host->ifindex && *interface->ifname && 5015 src_host->ifindex != if_nametoindex(interface->ifname)) || 5016 (dst_host->ifindex && *interface->ifname && 5017 dst_host->ifindex != if_nametoindex(interface->ifname))) 5018 continue; 5019 if (!r->af && src_host->af) 5020 r->af = src_host->af; 5021 else if (!r->af && dst_host->af) 5022 r->af = dst_host->af; 5023 5024 if (*interface->ifname) 5025 strlcpy(r->ifname, interface->ifname, 5026 sizeof(r->ifname)); 5027 else if (if_indextoname(src_host->ifindex, ifname)) 5028 strlcpy(r->ifname, ifname, sizeof(r->ifname)); 5029 else if (if_indextoname(dst_host->ifindex, ifname)) 5030 strlcpy(r->ifname, ifname, sizeof(r->ifname)); 5031 else 5032 memset(r->ifname, '\0', sizeof(r->ifname)); 5033 5034 if (strlcpy(r->label, label, sizeof(r->label)) >= 5035 sizeof(r->label)) 5036 errx(1, "expand_rule: strlcpy"); 5037 if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >= 5038 sizeof(r->tagname)) 5039 errx(1, "expand_rule: strlcpy"); 5040 if (strlcpy(r->match_tagname, match_tagname, 5041 sizeof(r->match_tagname)) >= sizeof(r->match_tagname)) 5042 errx(1, "expand_rule: strlcpy"); 5043 expand_label(r->label, PF_RULE_LABEL_SIZE, r->ifname, r->af, 5044 src_host, src_port, dst_host, dst_port, proto->proto); 5045 expand_label(r->tagname, PF_TAG_NAME_SIZE, r->ifname, r->af, 5046 src_host, src_port, dst_host, dst_port, proto->proto); 5047 expand_label(r->match_tagname, PF_TAG_NAME_SIZE, r->ifname, 5048 r->af, src_host, src_port, dst_host, dst_port, 5049 proto->proto); 5050 5051 error += check_netmask(src_host, r->af); 5052 error += check_netmask(dst_host, r->af); 5053 5054 r->ifnot = interface->not; 5055 r->proto = proto->proto; 5056 r->src.addr = src_host->addr; 5057 r->src.neg = src_host->not; 5058 r->src.port[0] = src_port->port[0]; 5059 r->src.port[1] = src_port->port[1]; 5060 r->src.port_op = src_port->op; 5061 r->dst.addr = dst_host->addr; 5062 r->dst.neg = dst_host->not; 5063 r->dst.port[0] = dst_port->port[0]; 5064 r->dst.port[1] = dst_port->port[1]; 5065 r->dst.port_op = dst_port->op; 5066 r->uid.op = uid->op; 5067 r->uid.uid[0] = uid->uid[0]; 5068 r->uid.uid[1] = uid->uid[1]; 5069 r->gid.op = gid->op; 5070 r->gid.gid[0] = gid->gid[0]; 5071 r->gid.gid[1] = gid->gid[1]; 5072 r->type = icmp_type->type; 5073 r->code = icmp_type->code; 5074 5075 if ((keep_state == PF_STATE_MODULATE || 5076 keep_state == PF_STATE_SYNPROXY) && 5077 r->proto && r->proto != IPPROTO_TCP) 5078 r->keep_state = PF_STATE_NORMAL; 5079 else 5080 r->keep_state = keep_state; 5081 5082 if (r->proto && r->proto != IPPROTO_TCP) { 5083 r->flags = 0; 5084 r->flagset = 0; 5085 } else { 5086 r->flags = flags; 5087 r->flagset = flagset; 5088 } 5089 if (icmp_type->proto && r->proto != icmp_type->proto) { 5090 yyerror("icmp-type mismatch"); 5091 error++; 5092 } 5093 5094 if (src_os && src_os->os) { 5095 r->os_fingerprint = pfctl_get_fingerprint(src_os->os); 5096 if ((pf->opts & PF_OPT_VERBOSE2) && 5097 r->os_fingerprint == PF_OSFP_NOMATCH) 5098 fprintf(stderr, 5099 "warning: unknown '%s' OS fingerprint\n", 5100 src_os->os); 5101 } else { 5102 r->os_fingerprint = PF_OSFP_ANY; 5103 } 5104 5105 TAILQ_INIT(&r->rpool.list); 5106 for (h = rpool_hosts; h != NULL; h = h->next) { 5107 pa = calloc(1, sizeof(struct pf_pooladdr)); 5108 if (pa == NULL) 5109 err(1, "expand_rule: calloc"); 5110 pa->addr = h->addr; 5111 if (h->ifname != NULL) { 5112 if (strlcpy(pa->ifname, h->ifname, 5113 sizeof(pa->ifname)) >= 5114 sizeof(pa->ifname)) 5115 errx(1, "expand_rule: strlcpy"); 5116 } else 5117 pa->ifname[0] = 0; 5118 TAILQ_INSERT_TAIL(&r->rpool.list, pa, entries); 5119 } 5120 5121 if (rule_consistent(r, anchor_call[0]) < 0 || error) 5122 yyerror("skipping rule due to errors"); 5123 else { 5124 r->nr = pf->astack[pf->asd]->match++; 5125 pfctl_add_rule(pf, r, anchor_call); 5126 added++; 5127 } 5128 5129 )))))))))); 5130 5131 FREE_LIST(struct node_if, interfaces); 5132 FREE_LIST(struct node_proto, protos); 5133 FREE_LIST(struct node_host, src_hosts); 5134 FREE_LIST(struct node_port, src_ports); 5135 FREE_LIST(struct node_os, src_oses); 5136 FREE_LIST(struct node_host, dst_hosts); 5137 FREE_LIST(struct node_port, dst_ports); 5138 FREE_LIST(struct node_uid, uids); 5139 FREE_LIST(struct node_gid, gids); 5140 FREE_LIST(struct node_icmp, icmp_types); 5141 FREE_LIST(struct node_host, rpool_hosts); 5142 5143 if (!added) 5144 yyerror("rule expands to no valid combination"); 5145} 5146 5147int 5148expand_skip_interface(struct node_if *interfaces) 5149{ 5150 int errs = 0; 5151 5152 if (!interfaces || (!interfaces->next && !interfaces->not && 5153 !strcmp(interfaces->ifname, "none"))) { 5154 if (pf->opts & PF_OPT_VERBOSE) 5155 printf("set skip on none\n"); 5156 errs = pfctl_set_interface_flags(pf, "", PFI_IFLAG_SKIP, 0); 5157 return (errs); 5158 } 5159 5160 if (pf->opts & PF_OPT_VERBOSE) 5161 printf("set skip on {"); 5162 LOOP_THROUGH(struct node_if, interface, interfaces, 5163 if (pf->opts & PF_OPT_VERBOSE) 5164 printf(" %s", interface->ifname); 5165 if (interface->not) { 5166 yyerror("skip on ! <interface> is not supported"); 5167 errs++; 5168 } else 5169 errs += pfctl_set_interface_flags(pf, 5170 interface->ifname, PFI_IFLAG_SKIP, 1); 5171 ); 5172 if (pf->opts & PF_OPT_VERBOSE) 5173 printf(" }\n"); 5174 5175 FREE_LIST(struct node_if, interfaces); 5176 5177 if (errs) 5178 return (1); 5179 else 5180 return (0); 5181} 5182 5183#undef FREE_LIST 5184#undef LOOP_THROUGH 5185 5186int 5187check_rulestate(int desired_state) 5188{ 5189 if (require_order && (rulestate > desired_state)) { 5190 yyerror("Rules must be in order: options, normalization, " 5191 "queueing, translation, filtering"); 5192 return (1); 5193 } 5194 rulestate = desired_state; 5195 return (0); 5196} 5197 5198int 5199kw_cmp(const void *k, const void *e) 5200{ 5201 return (strcmp(k, ((const struct keywords *)e)->k_name)); 5202} 5203 5204int 5205lookup(char *s) 5206{ 5207 /* this has to be sorted always */ 5208 static const struct keywords keywords[] = { 5209 { "all", ALL}, 5210 { "allow-opts", ALLOWOPTS}, 5211 { "altq", ALTQ}, 5212 { "anchor", ANCHOR}, 5213 { "antispoof", ANTISPOOF}, 5214 { "any", ANY}, 5215 { "bandwidth", BANDWIDTH}, 5216 { "binat", BINAT}, 5217 { "binat-anchor", BINATANCHOR}, 5218 { "bitmask", BITMASK}, 5219 { "block", BLOCK}, 5220 { "block-policy", BLOCKPOLICY}, 5221 { "cbq", CBQ}, 5222 { "code", CODE}, 5223 { "crop", FRAGCROP}, 5224 { "debug", DEBUG}, 5225 { "divert-reply", DIVERTREPLY}, 5226 { "divert-to", DIVERTTO}, 5227 { "drop", DROP}, 5228 { "drop-ovl", FRAGDROP}, 5229 { "dup-to", DUPTO}, 5230 { "fastroute", FASTROUTE}, 5231 { "file", FILENAME}, 5232 { "fingerprints", FINGERPRINTS}, 5233 { "flags", FLAGS}, 5234 { "floating", FLOATING}, 5235 { "flush", FLUSH}, 5236 { "for", FOR}, 5237 { "fragment", FRAGMENT}, 5238 { "from", FROM}, 5239 { "global", GLOBAL}, 5240 { "group", GROUP}, 5241 { "hfsc", HFSC}, 5242 { "hostid", HOSTID}, 5243 { "icmp-type", ICMPTYPE}, 5244 { "icmp6-type", ICMP6TYPE}, 5245 { "if-bound", IFBOUND}, 5246 { "in", IN}, 5247 { "include", INCLUDE}, 5248 { "inet", INET}, 5249 { "inet6", INET6}, 5250 { "keep", KEEP}, 5251 { "label", LABEL}, 5252 { "limit", LIMIT}, 5253 { "linkshare", LINKSHARE}, 5254 { "load", LOAD}, 5255 { "log", LOG}, 5256 { "loginterface", LOGINTERFACE}, 5257 { "max", MAXIMUM}, 5258 { "max-mss", MAXMSS}, 5259 { "max-src-conn", MAXSRCCONN}, 5260 { "max-src-conn-rate", MAXSRCCONNRATE}, 5261 { "max-src-nodes", MAXSRCNODES}, 5262 { "max-src-states", MAXSRCSTATES}, 5263 { "min-ttl", MINTTL}, 5264 { "modulate", MODULATE}, 5265 { "nat", NAT}, 5266 { "nat-anchor", NATANCHOR}, 5267 { "no", NO}, 5268 { "no-df", NODF}, 5269 { "no-route", NOROUTE}, 5270 { "no-sync", NOSYNC}, 5271 { "on", ON}, 5272 { "optimization", OPTIMIZATION}, 5273 { "os", OS}, 5274 { "out", OUT}, 5275 { "overload", OVERLOAD}, 5276 { "pass", PASS}, 5277 { "pflow", PFLOW}, 5278 { "port", PORT}, 5279 { "priority", PRIORITY}, 5280 { "priq", PRIQ}, 5281 { "probability", PROBABILITY}, 5282 { "proto", PROTO}, 5283 { "qlimit", QLIMIT}, 5284 { "queue", QUEUE}, 5285 { "quick", QUICK}, 5286 { "random", RANDOM}, 5287 { "random-id", RANDOMID}, 5288 { "rdr", RDR}, 5289 { "rdr-anchor", RDRANCHOR}, 5290 { "realtime", REALTIME}, 5291 { "reassemble", REASSEMBLE}, 5292 { "reply-to", REPLYTO}, 5293 { "require-order", REQUIREORDER}, 5294 { "return", RETURN}, 5295 { "return-icmp", RETURNICMP}, 5296 { "return-icmp6", RETURNICMP6}, 5297 { "return-rst", RETURNRST}, 5298 { "round-robin", ROUNDROBIN}, 5299 { "route", ROUTE}, 5300 { "route-to", ROUTETO}, 5301 { "rtable", RTABLE}, 5302 { "rule", RULE}, 5303 { "ruleset-optimization", RULESET_OPTIMIZATION}, 5304 { "scrub", SCRUB}, 5305 { "set", SET}, 5306 { "set-tos", SETTOS}, 5307 { "skip", SKIP}, 5308 { "sloppy", SLOPPY}, 5309 { "source-hash", SOURCEHASH}, 5310 { "source-track", SOURCETRACK}, 5311 { "state", STATE}, 5312 { "state-policy", STATEPOLICY}, 5313 { "static-port", STATICPORT}, 5314 { "sticky-address", STICKYADDRESS}, 5315 { "synproxy", SYNPROXY}, 5316 { "table", TABLE}, 5317 { "tag", TAG}, 5318 { "tagged", TAGGED}, 5319 { "tbrsize", TBRSIZE}, 5320 { "timeout", TIMEOUT}, 5321 { "to", TO}, 5322 { "tos", TOS}, 5323 { "ttl", TTL}, 5324 { "upperlimit", UPPERLIMIT}, 5325 { "urpf-failed", URPFFAILED}, 5326 { "user", USER}, 5327 }; 5328 const struct keywords *p; 5329 5330 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]), 5331 sizeof(keywords[0]), kw_cmp); 5332 5333 if (p) { 5334 if (debug > 1) 5335 fprintf(stderr, "%s: %d\n", s, p->k_val); 5336 return (p->k_val); 5337 } else { 5338 if (debug > 1) 5339 fprintf(stderr, "string: %s\n", s); 5340 return (STRING); 5341 } 5342} 5343 5344#define MAXPUSHBACK 128 5345 5346char *parsebuf; 5347int parseindex; 5348char pushback_buffer[MAXPUSHBACK]; 5349int pushback_index = 0; 5350 5351int 5352lgetc(int quotec) 5353{ 5354 int c, next; 5355 5356 if (parsebuf) { 5357 /* Read character from the parsebuffer instead of input. */ 5358 if (parseindex >= 0) { 5359 c = parsebuf[parseindex++]; 5360 if (c != '\0') 5361 return (c); 5362 parsebuf = NULL; 5363 } else 5364 parseindex++; 5365 } 5366 5367 if (pushback_index) 5368 return (pushback_buffer[--pushback_index]); 5369 5370 if (quotec) { 5371 if ((c = getc(file->stream)) == EOF) { 5372 yyerror("reached end of file while parsing quoted string"); 5373 if (popfile() == EOF) 5374 return (EOF); 5375 return (quotec); 5376 } 5377 return (c); 5378 } 5379 5380 while ((c = getc(file->stream)) == '\\') { 5381 next = getc(file->stream); 5382 if (next != '\n') { 5383 c = next; 5384 break; 5385 } 5386 yylval.lineno = file->lineno; 5387 file->lineno++; 5388 } 5389 5390 while (c == EOF) { 5391 if (popfile() == EOF) 5392 return (EOF); 5393 c = getc(file->stream); 5394 } 5395 return (c); 5396} 5397 5398int 5399lungetc(int c) 5400{ 5401 if (c == EOF) 5402 return (EOF); 5403 if (parsebuf) { 5404 parseindex--; 5405 if (parseindex >= 0) 5406 return (c); 5407 } 5408 if (pushback_index < MAXPUSHBACK-1) 5409 return (pushback_buffer[pushback_index++] = c); 5410 else 5411 return (EOF); 5412} 5413 5414int 5415findeol(void) 5416{ 5417 int c; 5418 5419 parsebuf = NULL; 5420 pushback_index = 0; 5421 5422 /* skip to either EOF or the first real EOL */ 5423 while (1) { 5424 c = lgetc(0); 5425 if (c == '\n') { 5426 file->lineno++; 5427 break; 5428 } 5429 if (c == EOF) 5430 break; 5431 } 5432 return (ERROR); 5433} 5434 5435int 5436yylex(void) 5437{ 5438 char buf[8096]; 5439 char *p, *val; 5440 int quotec, next, c; 5441 int token; 5442 5443top: 5444 p = buf; 5445 while ((c = lgetc(0)) == ' ' || c == '\t') 5446 ; /* nothing */ 5447 5448 yylval.lineno = file->lineno; 5449 if (c == '#') 5450 while ((c = lgetc(0)) != '\n' && c != EOF) 5451 ; /* nothing */ 5452 if (c == '$' && parsebuf == NULL) { 5453 while (1) { 5454 if ((c = lgetc(0)) == EOF) 5455 return (0); 5456 5457 if (p + 1 >= buf + sizeof(buf) - 1) { 5458 yyerror("string too long"); 5459 return (findeol()); 5460 } 5461 if (isalnum(c) || c == '_') { 5462 *p++ = (char)c; 5463 continue; 5464 } 5465 *p = '\0'; 5466 lungetc(c); 5467 break; 5468 } 5469 val = symget(buf); 5470 if (val == NULL) { 5471 yyerror("macro '%s' not defined", buf); 5472 return (findeol()); 5473 } 5474 parsebuf = val; 5475 parseindex = 0; 5476 goto top; 5477 } 5478 5479 switch (c) { 5480 case '\'': 5481 case '"': 5482 quotec = c; 5483 while (1) { 5484 if ((c = lgetc(quotec)) == EOF) 5485 return (0); 5486 if (c == '\n') { 5487 file->lineno++; 5488 continue; 5489 } else if (c == '\\') { 5490 if ((next = lgetc(quotec)) == EOF) 5491 return (0); 5492 if (next == quotec || c == ' ' || c == '\t') 5493 c = next; 5494 else if (next == '\n') 5495 continue; 5496 else 5497 lungetc(next); 5498 } else if (c == quotec) { 5499 *p = '\0'; 5500 break; 5501 } 5502 if (p + 1 >= buf + sizeof(buf) - 1) { 5503 yyerror("string too long"); 5504 return (findeol()); 5505 } 5506 *p++ = (char)c; 5507 } 5508 yylval.v.string = strdup(buf); 5509 if (yylval.v.string == NULL) 5510 err(1, "yylex: strdup"); 5511 return (STRING); 5512 case '<': 5513 next = lgetc(0); 5514 if (next == '>') { 5515 yylval.v.i = PF_OP_XRG; 5516 return (PORTBINARY); 5517 } 5518 lungetc(next); 5519 break; 5520 case '>': 5521 next = lgetc(0); 5522 if (next == '<') { 5523 yylval.v.i = PF_OP_IRG; 5524 return (PORTBINARY); 5525 } 5526 lungetc(next); 5527 break; 5528 case '-': 5529 next = lgetc(0); 5530 if (next == '>') 5531 return (ARROW); 5532 lungetc(next); 5533 break; 5534 } 5535 5536#define allowed_to_end_number(x) \ 5537 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=') 5538 5539 if (c == '-' || isdigit(c)) { 5540 do { 5541 *p++ = c; 5542 if ((unsigned)(p-buf) >= sizeof(buf)) { 5543 yyerror("string too long"); 5544 return (findeol()); 5545 } 5546 } while ((c = lgetc(0)) != EOF && isdigit(c)); 5547 lungetc(c); 5548 if (p == buf + 1 && buf[0] == '-') 5549 goto nodigits; 5550 if (c == EOF || allowed_to_end_number(c)) { 5551 const char *errstr = NULL; 5552 5553 *p = '\0'; 5554 yylval.v.number = strtonum(buf, LLONG_MIN, 5555 LLONG_MAX, &errstr); 5556 if (errstr) { 5557 yyerror("\"%s\" invalid number: %s", 5558 buf, errstr); 5559 return (findeol()); 5560 } 5561 return (NUMBER); 5562 } else { 5563nodigits: 5564 while (p > buf + 1) 5565 lungetc(*--p); 5566 c = *--p; 5567 if (c == '-') 5568 return (c); 5569 } 5570 } 5571 5572#define allowed_in_string(x) \ 5573 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \ 5574 x != '{' && x != '}' && x != '<' && x != '>' && \ 5575 x != '!' && x != '=' && x != '/' && x != '#' && \ 5576 x != ',')) 5577 5578 if (isalnum(c) || c == ':' || c == '_') { 5579 do { 5580 *p++ = c; 5581 if ((unsigned)(p-buf) >= sizeof(buf)) { 5582 yyerror("string too long"); 5583 return (findeol()); 5584 } 5585 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c))); 5586 lungetc(c); 5587 *p = '\0'; 5588 if ((token = lookup(buf)) == STRING) 5589 if ((yylval.v.string = strdup(buf)) == NULL) 5590 err(1, "yylex: strdup"); 5591 return (token); 5592 } 5593 if (c == '\n') { 5594 yylval.lineno = file->lineno; 5595 file->lineno++; 5596 } 5597 if (c == EOF) 5598 return (0); 5599 return (c); 5600} 5601 5602int 5603check_file_secrecy(int fd, const char *fname) 5604{ 5605 struct stat st; 5606 5607 if (fstat(fd, &st)) { 5608 warn("cannot stat %s", fname); 5609 return (-1); 5610 } 5611 if (st.st_uid != 0 && st.st_uid != getuid()) { 5612 warnx("%s: owner not root or current user", fname); 5613 return (-1); 5614 } 5615 if (st.st_mode & (S_IRWXG | S_IRWXO)) { 5616 warnx("%s: group/world readable/writeable", fname); 5617 return (-1); 5618 } 5619 return (0); 5620} 5621 5622struct file * 5623pushfile(const char *name, int secret) 5624{ 5625 struct file *nfile; 5626 5627 if ((nfile = calloc(1, sizeof(struct file))) == NULL || 5628 (nfile->name = strdup(name)) == NULL) { 5629 warn("malloc"); 5630 return (NULL); 5631 } 5632 if (TAILQ_FIRST(&files) == NULL && strcmp(nfile->name, "-") == 0) { 5633 nfile->stream = stdin; 5634 free(nfile->name); 5635 if ((nfile->name = strdup("stdin")) == NULL) { 5636 warn("strdup"); 5637 free(nfile); 5638 return (NULL); 5639 } 5640 } else if ((nfile->stream = fopen(nfile->name, "r")) == NULL) { 5641 warn("%s", nfile->name); 5642 free(nfile->name); 5643 free(nfile); 5644 return (NULL); 5645 } else if (secret && 5646 check_file_secrecy(fileno(nfile->stream), nfile->name)) { 5647 fclose(nfile->stream); 5648 free(nfile->name); 5649 free(nfile); 5650 return (NULL); 5651 } 5652 nfile->lineno = 1; 5653 TAILQ_INSERT_TAIL(&files, nfile, entry); 5654 return (nfile); 5655} 5656 5657int 5658popfile(void) 5659{ 5660 struct file *prev; 5661 5662 if ((prev = TAILQ_PREV(file, files, entry)) != NULL) { 5663 prev->errors += file->errors; 5664 TAILQ_REMOVE(&files, file, entry); 5665 fclose(file->stream); 5666 free(file->name); 5667 free(file); 5668 file = prev; 5669 return (0); 5670 } 5671 return (EOF); 5672} 5673 5674int 5675parse_config(char *filename, struct pfctl *xpf) 5676{ 5677 int errors = 0; 5678 struct sym *sym; 5679 5680 pf = xpf; 5681 errors = 0; 5682 rulestate = PFCTL_STATE_NONE; 5683 returnicmpdefault = (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT; 5684 returnicmp6default = 5685 (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT; 5686 blockpolicy = PFRULE_DROP; 5687 require_order = 1; 5688 5689 if ((file = pushfile(filename, 0)) == NULL) { 5690 warn("cannot open the main config file!"); 5691 return (-1); 5692 } 5693 5694 yyparse(); 5695 errors = file->errors; 5696 popfile(); 5697 5698 /* Free macros and check which have not been used. */ 5699 while ((sym = TAILQ_FIRST(&symhead))) { 5700 if ((pf->opts & PF_OPT_VERBOSE2) && !sym->used) 5701 fprintf(stderr, "warning: macro '%s' not " 5702 "used\n", sym->nam); 5703 free(sym->nam); 5704 free(sym->val); 5705 TAILQ_REMOVE(&symhead, sym, entry); 5706 free(sym); 5707 } 5708 5709 return (errors ? -1 : 0); 5710} 5711 5712int 5713symset(const char *nam, const char *val, int persist) 5714{ 5715 struct sym *sym; 5716 5717 for (sym = TAILQ_FIRST(&symhead); sym && strcmp(nam, sym->nam); 5718 sym = TAILQ_NEXT(sym, entry)) 5719 ; /* nothing */ 5720 5721 if (sym != NULL) { 5722 if (sym->persist == 1) 5723 return (0); 5724 else { 5725 free(sym->nam); 5726 free(sym->val); 5727 TAILQ_REMOVE(&symhead, sym, entry); 5728 free(sym); 5729 } 5730 } 5731 if ((sym = calloc(1, sizeof(*sym))) == NULL) 5732 return (-1); 5733 5734 sym->nam = strdup(nam); 5735 if (sym->nam == NULL) { 5736 free(sym); 5737 return (-1); 5738 } 5739 sym->val = strdup(val); 5740 if (sym->val == NULL) { 5741 free(sym->nam); 5742 free(sym); 5743 return (-1); 5744 } 5745 sym->used = 0; 5746 sym->persist = persist; 5747 TAILQ_INSERT_TAIL(&symhead, sym, entry); 5748 return (0); 5749} 5750 5751int 5752pfctl_cmdline_symset(char *s) 5753{ 5754 char *sym, *val; 5755 int ret; 5756 5757 if ((val = strrchr(s, '=')) == NULL) 5758 return (-1); 5759 5760 if ((sym = malloc(strlen(s) - strlen(val) + 1)) == NULL) 5761 err(1, "pfctl_cmdline_symset: malloc"); 5762 5763 strlcpy(sym, s, strlen(s) - strlen(val) + 1); 5764 5765 ret = symset(sym, val + 1, 1); 5766 free(sym); 5767 5768 return (ret); 5769} 5770 5771char * 5772symget(const char *nam) 5773{ 5774 struct sym *sym; 5775 5776 TAILQ_FOREACH(sym, &symhead, entry) 5777 if (strcmp(nam, sym->nam) == 0) { 5778 sym->used = 1; 5779 return (sym->val); 5780 } 5781 return (NULL); 5782} 5783 5784void 5785mv_rules(struct pf_ruleset *src, struct pf_ruleset *dst) 5786{ 5787 int i; 5788 struct pf_rule *r; 5789 5790 for (i = 0; i < PF_RULESET_MAX; ++i) { 5791 while ((r = TAILQ_FIRST(src->rules[i].active.ptr)) 5792 != NULL) { 5793 TAILQ_REMOVE(src->rules[i].active.ptr, r, entries); 5794 TAILQ_INSERT_TAIL(dst->rules[i].active.ptr, r, entries); 5795 dst->anchor->match++; 5796 } 5797 src->anchor->match = 0; 5798 while ((r = TAILQ_FIRST(src->rules[i].inactive.ptr)) 5799 != NULL) { 5800 TAILQ_REMOVE(src->rules[i].inactive.ptr, r, entries); 5801 TAILQ_INSERT_TAIL(dst->rules[i].inactive.ptr, 5802 r, entries); 5803 } 5804 } 5805} 5806 5807void 5808decide_address_family(struct node_host *n, sa_family_t *af) 5809{ 5810 if (*af != 0 || n == NULL) 5811 return; 5812 *af = n->af; 5813 while ((n = n->next) != NULL) { 5814 if (n->af != *af) { 5815 *af = 0; 5816 return; 5817 } 5818 } 5819} 5820 5821void 5822remove_invalid_hosts(struct node_host **nh, sa_family_t *af) 5823{ 5824 struct node_host *n = *nh, *prev = NULL; 5825 5826 while (n != NULL) { 5827 if (*af && n->af && n->af != *af) { 5828 /* unlink and free n */ 5829 struct node_host *next = n->next; 5830 5831 /* adjust tail pointer */ 5832 if (n == (*nh)->tail) 5833 (*nh)->tail = prev; 5834 /* adjust previous node's next pointer */ 5835 if (prev == NULL) 5836 *nh = next; 5837 else 5838 prev->next = next; 5839 /* free node */ 5840 if (n->ifname != NULL) 5841 free(n->ifname); 5842 free(n); 5843 n = next; 5844 } else { 5845 if (n->af && !*af) 5846 *af = n->af; 5847 prev = n; 5848 n = n->next; 5849 } 5850 } 5851} 5852 5853int 5854invalid_redirect(struct node_host *nh, sa_family_t af) 5855{ 5856 if (!af) { 5857 struct node_host *n; 5858 5859 /* tables and dyniftl are ok without an address family */ 5860 for (n = nh; n != NULL; n = n->next) { 5861 if (n->addr.type != PF_ADDR_TABLE && 5862 n->addr.type != PF_ADDR_DYNIFTL) { 5863 yyerror("address family not given and " 5864 "translation address expands to multiple " 5865 "address families"); 5866 return (1); 5867 } 5868 } 5869 } 5870 if (nh == NULL) { 5871 yyerror("no translation address with matching address family " 5872 "found."); 5873 return (1); 5874 } 5875 return (0); 5876} 5877 5878int 5879atoul(char *s, u_long *ulvalp) 5880{ 5881 u_long ulval; 5882 char *ep; 5883 5884 errno = 0; 5885 ulval = strtoul(s, &ep, 0); 5886 if (s[0] == '\0' || *ep != '\0') 5887 return (-1); 5888 if (errno == ERANGE && ulval == ULONG_MAX) 5889 return (-1); 5890 *ulvalp = ulval; 5891 return (0); 5892} 5893 5894int 5895getservice(char *n) 5896{ 5897 struct servent *s; 5898 u_long ulval; 5899 5900 if (atoul(n, &ulval) == 0) { 5901 if (ulval > 65535) { 5902 yyerror("illegal port value %lu", ulval); 5903 return (-1); 5904 } 5905 return (htons(ulval)); 5906 } else { 5907 s = getservbyname(n, "tcp"); 5908 if (s == NULL) 5909 s = getservbyname(n, "udp"); 5910 if (s == NULL) { 5911 yyerror("unknown port %s", n); 5912 return (-1); 5913 } 5914 return (s->s_port); 5915 } 5916} 5917 5918int 5919rule_label(struct pf_rule *r, char *s) 5920{ 5921 if (s) { 5922 if (strlcpy(r->label, s, sizeof(r->label)) >= 5923 sizeof(r->label)) { 5924 yyerror("rule label too long (max %d chars)", 5925 sizeof(r->label)-1); 5926 return (-1); 5927 } 5928 } 5929 return (0); 5930} 5931 5932u_int16_t 5933parseicmpspec(char *w, sa_family_t af) 5934{ 5935 const struct icmpcodeent *p; 5936 u_long ulval; 5937 u_int8_t icmptype; 5938 5939 if (af == AF_INET) 5940 icmptype = returnicmpdefault >> 8; 5941 else 5942 icmptype = returnicmp6default >> 8; 5943 5944 if (atoul(w, &ulval) == -1) { 5945 if ((p = geticmpcodebyname(icmptype, w, af)) == NULL) { 5946 yyerror("unknown icmp code %s", w); 5947 return (0); 5948 } 5949 ulval = p->code; 5950 } 5951 if (ulval > 255) { 5952 yyerror("invalid icmp code %lu", ulval); 5953 return (0); 5954 } 5955 return (icmptype << 8 | ulval); 5956} 5957 5958int 5959parseport(char *port, struct range *r, int extensions) 5960{ 5961 char *p = strchr(port, ':'); 5962 5963 if (p == NULL) { 5964 if ((r->a = getservice(port)) == -1) 5965 return (-1); 5966 r->b = 0; 5967 r->t = PF_OP_NONE; 5968 return (0); 5969 } 5970 if ((extensions & PPORT_STAR) && !strcmp(p+1, "*")) { 5971 *p = 0; 5972 if ((r->a = getservice(port)) == -1) 5973 return (-1); 5974 r->b = 0; 5975 r->t = PF_OP_IRG; 5976 return (0); 5977 } 5978 if ((extensions & PPORT_RANGE)) { 5979 *p++ = 0; 5980 if ((r->a = getservice(port)) == -1 || 5981 (r->b = getservice(p)) == -1) 5982 return (-1); 5983 if (r->a == r->b) { 5984 r->b = 0; 5985 r->t = PF_OP_NONE; 5986 } else 5987 r->t = PF_OP_RRG; 5988 return (0); 5989 } 5990 return (-1); 5991} 5992 5993int 5994pfctl_load_anchors(int dev, struct pfctl *pf, struct pfr_buffer *trans) 5995{ 5996 struct loadanchors *la; 5997 5998 TAILQ_FOREACH(la, &loadanchorshead, entries) { 5999 if (pf->opts & PF_OPT_VERBOSE) 6000 fprintf(stderr, "\nLoading anchor %s from %s\n", 6001 la->anchorname, la->filename); 6002 if (pfctl_rules(dev, la->filename, pf->opts, pf->optimize, 6003 la->anchorname, trans) == -1) 6004 return (-1); 6005 } 6006 6007 return (0); 6008} 6009