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