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