parse.y revision 1.420
1/* $OpenBSD: parse.y,v 1.420 2003/11/06 15:16:50 henning Exp $ */ 2 3/* 4 * Copyright (c) 2001 Markus Friedl. All rights reserved. 5 * Copyright (c) 2001 Daniel Hartmeier. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27%{ 28#include <sys/types.h> 29#include <sys/socket.h> 30#include <net/if.h> 31#include <netinet/in.h> 32#include <netinet/in_systm.h> 33#include <netinet/ip.h> 34#include <netinet/ip_icmp.h> 35#include <netinet/icmp6.h> 36#include <net/pfvar.h> 37#include <arpa/inet.h> 38#include <altq/altq.h> 39#include <altq/altq_cbq.h> 40#include <altq/altq_priq.h> 41#include <altq/altq_hfsc.h> 42 43#include <stdio.h> 44#include <stdlib.h> 45#include <netdb.h> 46#include <stdarg.h> 47#include <errno.h> 48#include <string.h> 49#include <ctype.h> 50#include <err.h> 51#include <pwd.h> 52#include <grp.h> 53#include <md5.h> 54 55#include "pfctl_parser.h" 56#include "pfctl.h" 57 58static struct pfctl *pf = NULL; 59static FILE *fin = NULL; 60static int debug = 0; 61static int lineno = 1; 62static int errors = 0; 63static int rulestate = 0; 64static u_int16_t returnicmpdefault = 65 (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT; 66static u_int16_t returnicmp6default = 67 (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT; 68static int blockpolicy = PFRULE_DROP; 69static int require_order = 1; 70 71enum { 72 PFCTL_STATE_NONE, 73 PFCTL_STATE_OPTION, 74 PFCTL_STATE_SCRUB, 75 PFCTL_STATE_QUEUE, 76 PFCTL_STATE_NAT, 77 PFCTL_STATE_FILTER 78}; 79 80struct node_proto { 81 u_int8_t proto; 82 struct node_proto *next; 83 struct node_proto *tail; 84}; 85 86struct node_port { 87 u_int16_t port[2]; 88 u_int8_t op; 89 struct node_port *next; 90 struct node_port *tail; 91}; 92 93struct node_uid { 94 uid_t uid[2]; 95 u_int8_t op; 96 struct node_uid *next; 97 struct node_uid *tail; 98}; 99 100struct node_gid { 101 gid_t gid[2]; 102 u_int8_t op; 103 struct node_gid *next; 104 struct node_gid *tail; 105}; 106 107struct node_icmp { 108 u_int8_t code; 109 u_int8_t type; 110 u_int8_t proto; 111 struct node_icmp *next; 112 struct node_icmp *tail; 113}; 114 115enum { PF_STATE_OPT_MAX=0, PF_STATE_OPT_TIMEOUT=1 }; 116struct node_state_opt { 117 int type; 118 union { 119 u_int32_t max_states; 120 struct { 121 int number; 122 u_int32_t seconds; 123 } timeout; 124 } data; 125 struct node_state_opt *next; 126 struct node_state_opt *tail; 127}; 128 129struct peer { 130 struct node_host *host; 131 struct node_port *port; 132}; 133 134struct node_queue { 135 char queue[PF_QNAME_SIZE]; 136 char parent[PF_QNAME_SIZE]; 137 char ifname[IFNAMSIZ]; 138 int scheduler; 139 struct node_queue *next; 140 struct node_queue *tail; 141} *queues = NULL; 142 143struct node_qassign { 144 char *qname; 145 char *pqname; 146}; 147 148struct filter_opts { 149 int marker; 150#define FOM_FLAGS 0x01 151#define FOM_ICMP 0x02 152#define FOM_TOS 0x04 153#define FOM_KEEP 0x08 154 struct node_uid *uid; 155 struct node_gid *gid; 156 struct { 157 u_int8_t b1; 158 u_int8_t b2; 159 u_int16_t w; 160 u_int16_t w2; 161 } flags; 162 struct node_icmp *icmpspec; 163 u_int32_t tos; 164 struct { 165 int action; 166 struct node_state_opt *options; 167 } keep; 168 int fragment; 169 int allowopts; 170 char *label; 171 struct node_qassign queues; 172 char *tag; 173 char *match_tag; 174 u_int8_t match_tag_not; 175} filter_opts; 176 177struct antispoof_opts { 178 char *label; 179} antispoof_opts; 180 181struct scrub_opts { 182 int marker; 183#define SOM_MINTTL 0x01 184#define SOM_MAXMSS 0x02 185#define SOM_FRAGCACHE 0x04 186 int nodf; 187 int minttl; 188 int maxmss; 189 int fragcache; 190 int randomid; 191 int reassemble_tcp; 192} scrub_opts; 193 194struct queue_opts { 195 int marker; 196#define QOM_BWSPEC 0x01 197#define QOM_SCHEDULER 0x02 198#define QOM_PRIORITY 0x04 199#define QOM_TBRSIZE 0x08 200#define QOM_QLIMIT 0x10 201 struct node_queue_bw queue_bwspec; 202 struct node_queue_opt scheduler; 203 int priority; 204 int tbrsize; 205 int qlimit; 206} queue_opts; 207 208struct table_opts { 209 int flags; 210 int init_addr; 211 struct node_tinithead init_nodes; 212} table_opts; 213 214struct node_hfsc_opts hfsc_opts; 215 216int yyerror(const char *, ...); 217int disallow_table(struct node_host *, const char *); 218int rule_consistent(struct pf_rule *); 219int filter_consistent(struct pf_rule *); 220int nat_consistent(struct pf_rule *); 221int rdr_consistent(struct pf_rule *); 222int process_tabledef(char *, struct table_opts *); 223int yyparse(void); 224void expand_label_str(char *, size_t, const char *, const char *); 225void expand_label_if(const char *, char *, size_t, const char *); 226void expand_label_addr(const char *, char *, size_t, u_int8_t, 227 struct node_host *); 228void expand_label_port(const char *, char *, size_t, struct node_port *); 229void expand_label_proto(const char *, char *, size_t, u_int8_t); 230void expand_label_nr(const char *, char *, size_t); 231void expand_label(char *, size_t, const char *, u_int8_t, struct node_host *, 232 struct node_port *, struct node_host *, struct node_port *, 233 u_int8_t); 234void expand_rule(struct pf_rule *, struct node_if *, struct node_host *, 235 struct node_proto *, struct node_os*, struct node_host *, 236 struct node_port *, struct node_host *, struct node_port *, 237 struct node_uid *, struct node_gid *, struct node_icmp *); 238int expand_altq(struct pf_altq *, struct node_if *, struct node_queue *, 239 struct node_queue_bw bwspec, struct node_queue_opt *); 240int expand_queue(struct pf_altq *, struct node_if *, struct node_queue *, 241 struct node_queue_bw, struct node_queue_opt *); 242 243int check_rulestate(int); 244int kw_cmp(const void *, const void *); 245int lookup(char *); 246int lgetc(FILE *); 247int lungetc(int); 248int findeol(void); 249int yylex(void); 250int atoul(char *, u_long *); 251int getservice(char *); 252int rule_label(struct pf_rule *, char *); 253 254TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead); 255struct sym { 256 TAILQ_ENTRY(sym) entries; 257 int used; 258 int persist; 259 char *nam; 260 char *val; 261}; 262 263 264int symset(const char *, const char *, int); 265char *symget(const char *); 266 267void decide_address_family(struct node_host *, sa_family_t *); 268void remove_invalid_hosts(struct node_host **, sa_family_t *); 269int invalid_redirect(struct node_host *, sa_family_t); 270u_int16_t parseicmpspec(char *, sa_family_t); 271 272TAILQ_HEAD(loadanchorshead, loadanchors) loadanchorshead = 273 TAILQ_HEAD_INITIALIZER(loadanchorshead); 274struct loadanchors { 275 TAILQ_ENTRY(loadanchors) entries; 276 char *anchorname; 277 char *rulesetname; 278 char *filename; 279}; 280 281typedef struct { 282 union { 283 u_int32_t number; 284 int i; 285 char *string; 286 struct { 287 u_int8_t b1; 288 u_int8_t b2; 289 u_int16_t w; 290 u_int16_t w2; 291 } b; 292 struct range { 293 int a; 294 int b; 295 int t; 296 } range; 297 struct node_if *interface; 298 struct node_proto *proto; 299 struct node_icmp *icmp; 300 struct node_host *host; 301 struct node_os *os; 302 struct node_port *port; 303 struct node_uid *uid; 304 struct node_gid *gid; 305 struct node_state_opt *state_opt; 306 struct peer peer; 307 struct { 308 struct peer src, dst; 309 struct node_os *src_os; 310 } fromto; 311 struct pf_poolhashkey *hashkey; 312 struct { 313 struct node_host *host; 314 u_int8_t rt; 315 u_int8_t pool_opts; 316 sa_family_t af; 317 struct pf_poolhashkey *key; 318 } route; 319 struct redirection { 320 struct node_host *host; 321 struct range rport; 322 } *redirection; 323 struct { 324 int type; 325 struct pf_poolhashkey *key; 326 } pooltype; 327 struct { 328 int action; 329 struct node_state_opt *options; 330 } keep_state; 331 struct { 332 u_int8_t log; 333 u_int8_t quick; 334 } logquick; 335 struct node_queue *queue; 336 struct node_queue_opt queue_options; 337 struct node_queue_bw queue_bwspec; 338 struct node_qassign qassign; 339 struct filter_opts filter_opts; 340 struct antispoof_opts antispoof_opts; 341 struct queue_opts queue_opts; 342 struct scrub_opts scrub_opts; 343 struct table_opts table_opts; 344 struct node_hfsc_opts hfsc_opts; 345 } v; 346 int lineno; 347} YYSTYPE; 348 349#define PREPARE_ANCHOR_RULE(r, a) \ 350 do { \ 351 memset(&(r), 0, sizeof(r)); \ 352 if (strlcpy(r.anchorname, (a), \ 353 sizeof(r.anchorname)) >= \ 354 sizeof(r.anchorname)) { \ 355 yyerror("anchor name '%s' too long", \ 356 (a)); \ 357 YYERROR; \ 358 } \ 359 } while (0) 360 361%} 362 363%token PASS BLOCK SCRUB RETURN IN OS OUT LOG LOGALL QUICK ON FROM TO FLAGS 364%token RETURNRST RETURNICMP RETURNICMP6 PROTO INET INET6 ALL ANY ICMPTYPE 365%token ICMP6TYPE CODE KEEP MODULATE STATE PORT RDR NAT BINAT ARROW NODF 366%token MINTTL ERROR ALLOWOPTS FASTROUTE FILENAME ROUTETO DUPTO REPLYTO NO LABEL 367%token NOROUTE FRAGMENT USER GROUP MAXMSS MAXIMUM TTL TOS DROP TABLE 368%token REASSEMBLE FRAGDROP FRAGCROP ANCHOR NATANCHOR RDRANCHOR BINATANCHOR 369%token SET OPTIMIZATION TIMEOUT LIMIT LOGINTERFACE BLOCKPOLICY RANDOMID 370%token REQUIREORDER SYNPROXY FINGERPRINTS 371%token ANTISPOOF FOR 372%token BITMASK RANDOM SOURCEHASH ROUNDROBIN STATICPORT 373%token ALTQ CBQ PRIQ HFSC BANDWIDTH TBRSIZE LINKSHARE REALTIME UPPERLIMIT 374%token QUEUE PRIORITY QLIMIT 375%token LOAD 376%token TAGGED TAG 377%token <v.string> STRING 378%token <v.i> PORTBINARY 379%type <v.interface> interface if_list if_item_not if_item 380%type <v.number> number icmptype icmp6type uid gid 381%type <v.number> tos not yesno natpass 382%type <v.i> no dir log af fragcache 383%type <v.i> staticport unaryop 384%type <v.b> action nataction flags flag blockspec 385%type <v.range> port rport 386%type <v.hashkey> hashkey 387%type <v.pooltype> pooltype 388%type <v.proto> proto proto_list proto_item 389%type <v.icmp> icmpspec 390%type <v.icmp> icmp_list icmp_item 391%type <v.icmp> icmp6_list icmp6_item 392%type <v.fromto> fromto 393%type <v.peer> ipportspec from to 394%type <v.host> ipspec xhost host dynaddr host_list 395%type <v.host> redir_host_list redirspec 396%type <v.host> route_host route_host_list routespec 397%type <v.os> os xos os_list 398%type <v.port> portspec port_list port_item 399%type <v.uid> uids uid_list uid_item 400%type <v.gid> gids gid_list gid_item 401%type <v.route> route 402%type <v.redirection> redirection redirpool 403%type <v.string> label string tag 404%type <v.keep_state> keep 405%type <v.state_opt> state_opt_spec state_opt_list state_opt_item 406%type <v.logquick> logquick 407%type <v.interface> antispoof_ifspc antispoof_iflst 408%type <v.qassign> qname 409%type <v.queue> qassign qassign_list qassign_item 410%type <v.queue_options> scheduler 411%type <v.number> cbqflags_list cbqflags_item 412%type <v.number> priqflags_list priqflags_item 413%type <v.hfsc_opts> hfscopts_list hfscopts_item hfsc_opts 414%type <v.queue_bwspec> bandwidth 415%type <v.filter_opts> filter_opts filter_opt filter_opts_l 416%type <v.antispoof_opts> antispoof_opts antispoof_opt antispoof_opts_l 417%type <v.queue_opts> queue_opts queue_opt queue_opts_l 418%type <v.scrub_opts> scrub_opts scrub_opt scrub_opts_l 419%type <v.table_opts> table_opts table_opt table_opts_l 420%% 421 422ruleset : /* empty */ 423 | ruleset '\n' 424 | ruleset option '\n' 425 | ruleset scrubrule '\n' 426 | ruleset natrule '\n' 427 | ruleset binatrule '\n' 428 | ruleset pfrule '\n' 429 | ruleset anchorrule '\n' 430 | ruleset loadrule '\n' 431 | ruleset altqif '\n' 432 | ruleset queuespec '\n' 433 | ruleset varset '\n' 434 | ruleset antispoof '\n' 435 | ruleset tabledef '\n' 436 | ruleset error '\n' { errors++; } 437 ; 438 439option : SET OPTIMIZATION STRING { 440 if (check_rulestate(PFCTL_STATE_OPTION)) 441 YYERROR; 442 if (pfctl_set_optimization(pf, $3) != 0) { 443 yyerror("unknown optimization %s", $3); 444 YYERROR; 445 } 446 } 447 | SET TIMEOUT timeout_spec 448 | SET TIMEOUT '{' timeout_list '}' 449 | SET LIMIT limit_spec 450 | SET LIMIT '{' limit_list '}' 451 | SET LOGINTERFACE STRING { 452 if (check_rulestate(PFCTL_STATE_OPTION)) 453 YYERROR; 454 if ((ifa_exists($3) == NULL) && strcmp($3, "none")) { 455 yyerror("interface %s doesn't exist", $3); 456 YYERROR; 457 } 458 if (pfctl_set_logif(pf, $3) != 0) { 459 yyerror("error setting loginterface %s", $3); 460 YYERROR; 461 } 462 } 463 | SET BLOCKPOLICY DROP { 464 if (pf->opts & PF_OPT_VERBOSE) 465 printf("set block-policy drop\n"); 466 if (check_rulestate(PFCTL_STATE_OPTION)) 467 YYERROR; 468 blockpolicy = PFRULE_DROP; 469 } 470 | SET BLOCKPOLICY RETURN { 471 if (pf->opts & PF_OPT_VERBOSE) 472 printf("set block-policy return\n"); 473 if (check_rulestate(PFCTL_STATE_OPTION)) 474 YYERROR; 475 blockpolicy = PFRULE_RETURN; 476 } 477 | SET REQUIREORDER yesno { 478 if (pf->opts & PF_OPT_VERBOSE) 479 printf("set require-order %s\n", 480 $3 == 1 ? "yes" : "no"); 481 require_order = $3; 482 } 483 | SET FINGERPRINTS STRING { 484 if (pf->opts & PF_OPT_VERBOSE) 485 printf("fingerprints %s\n", $3); 486 if (check_rulestate(PFCTL_STATE_OPTION)) 487 YYERROR; 488 if (pfctl_file_fingerprints(pf->dev, pf->opts, $3)) { 489 yyerror("error loading fingerprints %s", $3); 490 YYERROR; 491 } 492 } 493 ; 494 495string : string STRING { 496 if (asprintf(&$$, "%s %s", $1, $2) == -1) 497 err(1, "string: asprintf"); 498 free($1); 499 free($2); 500 } 501 | STRING 502 ; 503 504varset : STRING '=' string { 505 if (pf->opts & PF_OPT_VERBOSE) 506 printf("%s = \"%s\"\n", $1, $3); 507 if (symset($1, $3, 0) == -1) 508 err(1, "cannot store variable %s", $1); 509 } 510 ; 511 512anchorrule : ANCHOR string dir interface af proto fromto { 513 struct pf_rule r; 514 515 if (check_rulestate(PFCTL_STATE_FILTER)) 516 YYERROR; 517 518 PREPARE_ANCHOR_RULE(r, $2); 519 r.direction = $3; 520 r.af = $5; 521 522 decide_address_family($7.src.host, &r.af); 523 decide_address_family($7.dst.host, &r.af); 524 525 expand_rule(&r, $4, NULL, $6, $7.src_os, 526 $7.src.host, $7.src.port, $7.dst.host, $7.dst.port, 527 0, 0, 0); 528 } 529 | NATANCHOR string interface af proto fromto { 530 struct pf_rule r; 531 532 if (check_rulestate(PFCTL_STATE_NAT)) 533 YYERROR; 534 535 PREPARE_ANCHOR_RULE(r, $2); 536 r.action = PF_NAT; 537 r.af = $4; 538 539 decide_address_family($6.src.host, &r.af); 540 decide_address_family($6.dst.host, &r.af); 541 542 expand_rule(&r, $3, NULL, $5, $6.src_os, 543 $6.src.host, $6.src.port, $6.dst.host, $6.dst.port, 544 0, 0, 0); 545 } 546 | RDRANCHOR string interface af proto fromto { 547 struct pf_rule r; 548 549 if (check_rulestate(PFCTL_STATE_NAT)) 550 YYERROR; 551 552 PREPARE_ANCHOR_RULE(r, $2); 553 r.action = PF_RDR; 554 r.af = $4; 555 556 decide_address_family($6.src.host, &r.af); 557 decide_address_family($6.dst.host, &r.af); 558 559 if ($6.src.port != NULL) { 560 yyerror("source port parameter not supported" 561 " in rdr-anchor"); 562 YYERROR; 563 } 564 if ($6.dst.port != NULL) { 565 if ($6.dst.port->next != NULL) { 566 yyerror("destination port list " 567 "expansion not supported in " 568 "rdr-anchor"); 569 YYERROR; 570 } else if ($6.dst.port->op != PF_OP_EQ) { 571 yyerror("destination port operators" 572 " not supported in rdr-anchor"); 573 YYERROR; 574 } 575 r.dst.port[0] = $6.dst.port->port[0]; 576 r.dst.port[1] = $6.dst.port->port[1]; 577 r.dst.port_op = $6.dst.port->op; 578 } 579 580 expand_rule(&r, $3, NULL, $5, $6.src_os, 581 $6.src.host, $6.src.port, $6.dst.host, $6.dst.port, 582 0, 0, 0); 583 } 584 | BINATANCHOR string interface af proto fromto { 585 struct pf_rule r; 586 587 if (check_rulestate(PFCTL_STATE_NAT)) 588 YYERROR; 589 590 PREPARE_ANCHOR_RULE(r, $2); 591 r.action = PF_BINAT; 592 r.af = $4; 593 if ($5 != NULL) { 594 if ($5->next != NULL) { 595 yyerror("proto list expansion" 596 " not supported in binat-anchor"); 597 YYERROR; 598 } 599 r.proto = $5->proto; 600 free($5); 601 } 602 603 if ($6.src.host != NULL || $6.src.port != NULL || 604 $6.dst.host != NULL || $6.dst.port != NULL) { 605 yyerror("fromto parameter not supported" 606 " in binat-anchor"); 607 YYERROR; 608 } 609 610 decide_address_family($6.src.host, &r.af); 611 decide_address_family($6.dst.host, &r.af); 612 613 pfctl_add_rule(pf, &r); 614 } 615 ; 616 617loadrule : LOAD ANCHOR string FROM string { 618 char *t; 619 struct loadanchors *loadanchor; 620 621 t = strsep(&$3, ":"); 622 if (*t == '\0' || *$3 == '\0') { 623 yyerror("anchor '%s' invalid\n", $3); 624 YYERROR; 625 } 626 if (strlen(t) >= PF_ANCHOR_NAME_SIZE) { 627 yyerror("anchorname %s too long, max %u\n", 628 t, PF_ANCHOR_NAME_SIZE - 1); 629 YYERROR; 630 } 631 if (strlen($3) >= PF_RULESET_NAME_SIZE) { 632 yyerror("rulesetname %s too long, max %u\n", 633 $3, PF_RULESET_NAME_SIZE - 1); 634 YYERROR; 635 } 636 637 loadanchor = calloc(1, sizeof(struct loadanchors)); 638 if (loadanchor == NULL) 639 err(1, "loadrule: calloc"); 640 if ((loadanchor->anchorname = strdup(t)) == NULL) 641 err(1, "loadrule: strdup"); 642 if ((loadanchor->rulesetname = strdup($3)) == NULL) 643 err(1, "loadrule: strdup"); 644 if ((loadanchor->filename = strdup($5)) == NULL) 645 err(1, "loadrule: strdup"); 646 647 TAILQ_INSERT_TAIL(&loadanchorshead, loadanchor, 648 entries); 649 650 free(t); /* not $3 */ 651 free($5); 652 }; 653 654scrubrule : SCRUB dir logquick interface af proto fromto scrub_opts 655 { 656 struct pf_rule r; 657 658 if (check_rulestate(PFCTL_STATE_SCRUB)) 659 YYERROR; 660 661 memset(&r, 0, sizeof(r)); 662 663 r.action = PF_SCRUB; 664 r.direction = $2; 665 666 r.log = $3.log; 667 if ($3.quick) { 668 yyerror("scrub rules do not support 'quick'"); 669 YYERROR; 670 } 671 672 if ($4) { 673 if ($4->not) { 674 yyerror("scrub rules do not support " 675 "'! <if>'"); 676 YYERROR; 677 } 678 } 679 r.af = $5; 680 if ($8.nodf) 681 r.rule_flag |= PFRULE_NODF; 682 if ($8.randomid) 683 r.rule_flag |= PFRULE_RANDOMID; 684 if ($8.reassemble_tcp) { 685 if (r.direction != PF_INOUT) { 686 yyerror("reassemble tcp rules can not " 687 "specify direction"); 688 YYERROR; 689 } 690 r.rule_flag |= PFRULE_REASSEMBLE_TCP; 691 } 692 if ($8.minttl) 693 r.min_ttl = $8.minttl; 694 if ($8.maxmss) 695 r.max_mss = $8.maxmss; 696 if ($8.fragcache) 697 r.rule_flag |= $8.fragcache; 698 699 expand_rule(&r, $4, NULL, $6, $7.src_os, 700 $7.src.host, $7.src.port, $7.dst.host, $7.dst.port, 701 NULL, NULL, NULL); 702 } 703 ; 704 705scrub_opts : { 706 bzero(&scrub_opts, sizeof scrub_opts); 707 } 708 scrub_opts_l 709 { $$ = scrub_opts; } 710 | /* empty */ { 711 bzero(&scrub_opts, sizeof scrub_opts); 712 $$ = scrub_opts; 713 } 714 ; 715 716scrub_opts_l : scrub_opts_l scrub_opt 717 | scrub_opt 718 ; 719 720scrub_opt : NODF { 721 if (scrub_opts.nodf) { 722 yyerror("no-df cannot be respecified"); 723 YYERROR; 724 } 725 scrub_opts.nodf = 1; 726 } 727 | MINTTL number { 728 if (scrub_opts.marker & SOM_MINTTL) { 729 yyerror("min-ttl cannot be respecified"); 730 YYERROR; 731 } 732 if ($2 > 255) { 733 yyerror("illegal min-ttl value %d", $2); 734 YYERROR; 735 } 736 scrub_opts.marker |= SOM_MINTTL; 737 scrub_opts.minttl = $2; 738 } 739 | MAXMSS number { 740 if (scrub_opts.marker & SOM_MAXMSS) { 741 yyerror("max-mss cannot be respecified"); 742 YYERROR; 743 } 744 if ($2 > 65535) { 745 yyerror("illegal max-mss value %d", $2); 746 YYERROR; 747 } 748 scrub_opts.marker |= SOM_MAXMSS; 749 scrub_opts.maxmss = $2; 750 } 751 | fragcache { 752 if (scrub_opts.marker & SOM_FRAGCACHE) { 753 yyerror("fragcache cannot be respecified"); 754 YYERROR; 755 } 756 scrub_opts.marker |= SOM_FRAGCACHE; 757 scrub_opts.fragcache = $1; 758 } 759 | REASSEMBLE STRING { 760 if (strcasecmp($2, "tcp") != 0) 761 YYERROR; 762 if (scrub_opts.reassemble_tcp) { 763 yyerror("reassemble tcp cannot be respecified"); 764 YYERROR; 765 } 766 scrub_opts.reassemble_tcp = 1; 767 } 768 | RANDOMID { 769 if (scrub_opts.randomid) { 770 yyerror("random-id cannot be respecified"); 771 YYERROR; 772 } 773 scrub_opts.randomid = 1; 774 } 775 ; 776 777fragcache : FRAGMENT REASSEMBLE { $$ = 0; /* default */ } 778 | FRAGMENT FRAGCROP { $$ = PFRULE_FRAGCROP; } 779 | FRAGMENT FRAGDROP { $$ = PFRULE_FRAGDROP; } 780 ; 781 782antispoof : ANTISPOOF logquick antispoof_ifspc af antispoof_opts { 783 struct pf_rule r; 784 struct node_host *h = NULL; 785 struct node_if *i, *j; 786 787 if (check_rulestate(PFCTL_STATE_FILTER)) 788 YYERROR; 789 790 for (i = $3; i; i = i->next) { 791 bzero(&r, sizeof(r)); 792 793 r.action = PF_DROP; 794 r.direction = PF_IN; 795 r.log = $2.log; 796 r.quick = $2.quick; 797 r.af = $4; 798 if (rule_label(&r, $5.label)) 799 YYERROR; 800 j = calloc(1, sizeof(struct node_if)); 801 if (j == NULL) 802 err(1, "antispoof: calloc"); 803 if (strlcpy(j->ifname, i->ifname, 804 sizeof(j->ifname)) >= sizeof(j->ifname)) { 805 free(j); 806 yyerror("interface name too long"); 807 YYERROR; 808 } 809 j->not = 1; 810 h = ifa_lookup(j->ifname, PFCTL_IFLOOKUP_NET); 811 812 expand_rule(&r, j, NULL, NULL, NULL, h, NULL, 813 NULL, NULL, NULL, NULL, NULL); 814 815 if ((i->ifa_flags & IFF_LOOPBACK) == 0) { 816 bzero(&r, sizeof(r)); 817 818 r.action = PF_DROP; 819 r.direction = PF_IN; 820 r.log = $2.log; 821 r.quick = $2.quick; 822 r.af = $4; 823 if (rule_label(&r, $5.label)) 824 YYERROR; 825 h = ifa_lookup(i->ifname, 826 PFCTL_IFLOOKUP_HOST); 827 expand_rule(&r, NULL, NULL, NULL, NULL, 828 h, NULL, NULL, NULL, NULL, NULL, 829 NULL); 830 } 831 } 832 free($5.label); 833 } 834 ; 835 836antispoof_ifspc : FOR if_item { $$ = $2; } 837 | FOR '{' antispoof_iflst '}' { $$ = $3; } 838 ; 839 840antispoof_iflst : if_item { $$ = $1; } 841 | antispoof_iflst comma if_item { 842 $1->tail->next = $3; 843 $1->tail = $3; 844 $$ = $1; 845 } 846 ; 847 848antispoof_opts : { bzero(&antispoof_opts, sizeof antispoof_opts); } 849 antispoof_opts_l 850 { $$ = antispoof_opts; } 851 | /* empty */ { 852 bzero(&antispoof_opts, sizeof antispoof_opts); 853 $$ = antispoof_opts; 854 } 855 ; 856 857antispoof_opts_l : antispoof_opts_l antispoof_opt 858 | antispoof_opt 859 ; 860 861antispoof_opt : label { 862 if (antispoof_opts.label) { 863 yyerror("label cannot be redefined"); 864 YYERROR; 865 } 866 antispoof_opts.label = $1; 867 } 868 ; 869 870not : '!' { $$ = 1; } 871 | /* empty */ { $$ = 0; } 872 873tabledef : TABLE '<' STRING '>' table_opts { 874 struct node_host *h, *nh; 875 struct node_tinit *ti, *nti; 876 877 if (strlen($3) >= PF_TABLE_NAME_SIZE) { 878 yyerror("table name too long, max %d chars", 879 PF_TABLE_NAME_SIZE - 1); 880 YYERROR; 881 } 882 if (pf->loadopt & PFCTL_FLAG_TABLE) 883 if (process_tabledef($3, &$5)) 884 YYERROR; 885 for (ti = SIMPLEQ_FIRST(&$5.init_nodes); 886 ti != SIMPLEQ_END(&$5.init_nodes); ti = nti) { 887 if (ti->file) 888 free(ti->file); 889 for (h = ti->host; h != NULL; h = nh) { 890 nh = h->next; 891 free(h); 892 } 893 nti = SIMPLEQ_NEXT(ti, entries); 894 free (ti); 895 } 896 } 897 ; 898 899table_opts : { 900 bzero(&table_opts, sizeof table_opts); 901 SIMPLEQ_INIT(&table_opts.init_nodes); 902 } 903 table_opts_l 904 { $$ = table_opts; } 905 | /* empty */ 906 { 907 bzero(&table_opts, sizeof table_opts); 908 SIMPLEQ_INIT(&table_opts.init_nodes); 909 $$ = table_opts; 910 } 911 ; 912 913table_opts_l : table_opts_l table_opt 914 | table_opt 915 ; 916 917table_opt : STRING { 918 if (!strcmp($1, "const")) 919 table_opts.flags |= PFR_TFLAG_CONST; 920 else if (!strcmp($1, "persist")) 921 table_opts.flags |= PFR_TFLAG_PERSIST; 922 else 923 YYERROR; 924 } 925 | '{' '}' { table_opts.init_addr = 1; } 926 | '{' host_list '}' { 927 struct node_host *n; 928 struct node_tinit *ti; 929 930 for (n = $2; n != NULL; n = n->next) { 931 switch(n->addr.type) { 932 case PF_ADDR_ADDRMASK: 933 continue; /* ok */ 934 case PF_ADDR_DYNIFTL: 935 yyerror("dynamic addresses are not " 936 "permitted inside tables"); 937 break; 938 case PF_ADDR_TABLE: 939 yyerror("tables cannot contain tables"); 940 break; 941 case PF_ADDR_NOROUTE: 942 yyerror("\"no-route\" is not permitted " 943 "inside tables"); 944 break; 945 default: 946 yyerror("unknown address type %d", 947 n->addr.type); 948 } 949 YYERROR; 950 } 951 if (!(ti = calloc(1, sizeof(*ti)))) 952 err(1, "table_opt: calloc"); 953 ti->host = $2; 954 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti, 955 entries); 956 table_opts.init_addr = 1; 957 } 958 | FILENAME STRING { 959 struct node_tinit *ti; 960 961 if (!(ti = calloc(1, sizeof(*ti)))) 962 err(1, "table_opt: calloc"); 963 ti->file = $2; 964 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti, 965 entries); 966 table_opts.init_addr = 1; 967 } 968 ; 969 970altqif : ALTQ interface queue_opts QUEUE qassign { 971 struct pf_altq a; 972 973 if (check_rulestate(PFCTL_STATE_QUEUE)) 974 YYERROR; 975 976 memset(&a, 0, sizeof(a)); 977 if ($3.scheduler.qtype == ALTQT_NONE) { 978 yyerror("no scheduler specified!"); 979 YYERROR; 980 } 981 a.scheduler = $3.scheduler.qtype; 982 a.qlimit = $3.qlimit; 983 a.tbrsize = $3.tbrsize; 984 if ($5 == NULL) { 985 yyerror("no child queues specified"); 986 YYERROR; 987 } 988 if (expand_altq(&a, $2, $5, $3.queue_bwspec, 989 &$3.scheduler)) 990 YYERROR; 991 } 992 ; 993 994queuespec : QUEUE STRING interface queue_opts qassign { 995 struct pf_altq a; 996 997 if (check_rulestate(PFCTL_STATE_QUEUE)) 998 YYERROR; 999 1000 memset(&a, 0, sizeof(a)); 1001 1002 if (strlcpy(a.qname, $2, sizeof(a.qname)) >= 1003 sizeof(a.qname)) { 1004 yyerror("queue name too long (max " 1005 "%d chars)", PF_QNAME_SIZE-1); 1006 YYERROR; 1007 } 1008 if ($4.tbrsize) { 1009 yyerror("cannot specify tbrsize for queue"); 1010 YYERROR; 1011 } 1012 if ($4.priority > 255) { 1013 yyerror("priority out of range: max 255"); 1014 YYERROR; 1015 } 1016 a.priority = $4.priority; 1017 a.qlimit = $4.qlimit; 1018 a.scheduler = $4.scheduler.qtype; 1019 if (expand_queue(&a, $3, $5, $4.queue_bwspec, 1020 &$4.scheduler)) { 1021 yyerror("errors in queue definition"); 1022 YYERROR; 1023 } 1024 } 1025 ; 1026 1027queue_opts : { 1028 bzero(&queue_opts, sizeof queue_opts); 1029 queue_opts.priority = DEFAULT_PRIORITY; 1030 queue_opts.qlimit = DEFAULT_QLIMIT; 1031 queue_opts.scheduler.qtype = ALTQT_NONE; 1032 queue_opts.queue_bwspec.bw_percent = 100; 1033 } 1034 queue_opts_l 1035 { $$ = queue_opts; } 1036 | /* empty */ { 1037 bzero(&queue_opts, sizeof queue_opts); 1038 queue_opts.priority = DEFAULT_PRIORITY; 1039 queue_opts.qlimit = DEFAULT_QLIMIT; 1040 queue_opts.scheduler.qtype = ALTQT_NONE; 1041 queue_opts.queue_bwspec.bw_percent = 100; 1042 $$ = queue_opts; 1043 } 1044 ; 1045 1046queue_opts_l : queue_opts_l queue_opt 1047 | queue_opt 1048 ; 1049 1050queue_opt : BANDWIDTH bandwidth { 1051 if (queue_opts.marker & QOM_BWSPEC) { 1052 yyerror("bandwidth cannot be respecified"); 1053 YYERROR; 1054 } 1055 queue_opts.marker |= QOM_BWSPEC; 1056 queue_opts.queue_bwspec = $2; 1057 } 1058 | PRIORITY number { 1059 if (queue_opts.marker & QOM_PRIORITY) { 1060 yyerror("priority cannot be respecified"); 1061 YYERROR; 1062 } 1063 if ($2 > 255) { 1064 yyerror("priority out of range: max 255"); 1065 YYERROR; 1066 } 1067 queue_opts.marker |= QOM_PRIORITY; 1068 queue_opts.priority = $2; 1069 } 1070 | QLIMIT number { 1071 if (queue_opts.marker & QOM_QLIMIT) { 1072 yyerror("qlimit cannot be respecified"); 1073 YYERROR; 1074 } 1075 if ($2 > 65535) { 1076 yyerror("qlimit out of range: max 65535"); 1077 YYERROR; 1078 } 1079 queue_opts.marker |= QOM_QLIMIT; 1080 queue_opts.qlimit = $2; 1081 } 1082 | scheduler { 1083 if (queue_opts.marker & QOM_SCHEDULER) { 1084 yyerror("scheduler cannot be respecified"); 1085 YYERROR; 1086 } 1087 queue_opts.marker |= QOM_SCHEDULER; 1088 queue_opts.scheduler = $1; 1089 } 1090 | TBRSIZE number { 1091 if (queue_opts.marker & QOM_TBRSIZE) { 1092 yyerror("tbrsize cannot be respecified"); 1093 YYERROR; 1094 } 1095 if ($2 > 65535) { 1096 yyerror("tbrsize too big: max 65535"); 1097 YYERROR; 1098 } 1099 queue_opts.marker |= QOM_TBRSIZE; 1100 queue_opts.tbrsize = $2; 1101 } 1102 ; 1103 1104bandwidth : STRING { 1105 double bps; 1106 char *cp; 1107 1108 $$.bw_percent = 0; 1109 1110 bps = strtod($1, &cp); 1111 if (cp != NULL) { 1112 if (!strcmp(cp, "b")) 1113 ; /* nothing */ 1114 else if (!strcmp(cp, "Kb")) 1115 bps *= 1000; 1116 else if (!strcmp(cp, "Mb")) 1117 bps *= 1000 * 1000; 1118 else if (!strcmp(cp, "Gb")) 1119 bps *= 1000 * 1000 * 1000; 1120 else if (!strcmp(cp, "%")) { 1121 if (bps < 0 || bps > 100) { 1122 yyerror("bandwidth spec " 1123 "out of range"); 1124 YYERROR; 1125 } 1126 $$.bw_percent = bps; 1127 bps = 0; 1128 } else { 1129 yyerror("unknown unit %s", cp); 1130 YYERROR; 1131 } 1132 } 1133 $$.bw_absolute = (u_int32_t)bps; 1134 } 1135 1136scheduler : CBQ { 1137 $$.qtype = ALTQT_CBQ; 1138 $$.data.cbq_opts.flags = 0; 1139 } 1140 | CBQ '(' cbqflags_list ')' { 1141 $$.qtype = ALTQT_CBQ; 1142 $$.data.cbq_opts.flags = $3; 1143 } 1144 | PRIQ { 1145 $$.qtype = ALTQT_PRIQ; 1146 $$.data.priq_opts.flags = 0; 1147 } 1148 | PRIQ '(' priqflags_list ')' { 1149 $$.qtype = ALTQT_PRIQ; 1150 $$.data.priq_opts.flags = $3; 1151 } 1152 | HFSC { 1153 $$.qtype = ALTQT_HFSC; 1154 bzero(&$$.data.hfsc_opts, 1155 sizeof(struct node_hfsc_opts)); 1156 } 1157 | HFSC '(' hfsc_opts ')' { 1158 $$.qtype = ALTQT_HFSC; 1159 $$.data.hfsc_opts = $3; 1160 } 1161 ; 1162 1163cbqflags_list : cbqflags_item { $$ |= $1; } 1164 | cbqflags_list comma cbqflags_item { $$ |= $3; } 1165 ; 1166 1167cbqflags_item : STRING { 1168 if (!strcmp($1, "default")) 1169 $$ = CBQCLF_DEFCLASS; 1170 else if (!strcmp($1, "borrow")) 1171 $$ = CBQCLF_BORROW; 1172 else if (!strcmp($1, "red")) 1173 $$ = CBQCLF_RED; 1174 else if (!strcmp($1, "ecn")) 1175 $$ = CBQCLF_RED|CBQCLF_ECN; 1176 else if (!strcmp($1, "rio")) 1177 $$ = CBQCLF_RIO; 1178 else { 1179 yyerror("unknown cbq flag \"%s\"", $1); 1180 YYERROR; 1181 } 1182 } 1183 ; 1184 1185priqflags_list : priqflags_item { $$ |= $1; } 1186 | priqflags_list comma priqflags_item { $$ |= $3; } 1187 ; 1188 1189priqflags_item : STRING { 1190 if (!strcmp($1, "default")) 1191 $$ = PRCF_DEFAULTCLASS; 1192 else if (!strcmp($1, "red")) 1193 $$ = PRCF_RED; 1194 else if (!strcmp($1, "ecn")) 1195 $$ = PRCF_RED|PRCF_ECN; 1196 else if (!strcmp($1, "rio")) 1197 $$ = PRCF_RIO; 1198 else { 1199 yyerror("unknown priq flag \"%s\"", $1); 1200 YYERROR; 1201 } 1202 } 1203 ; 1204 1205hfsc_opts : { 1206 bzero(&hfsc_opts, 1207 sizeof(struct node_hfsc_opts)); 1208 } 1209 hfscopts_list { 1210 $$ = hfsc_opts; 1211 } 1212 ; 1213 1214hfscopts_list : hfscopts_item 1215 | hfscopts_list comma hfscopts_item 1216 ; 1217 1218hfscopts_item : LINKSHARE bandwidth { 1219 if (hfsc_opts.linkshare.used) { 1220 yyerror("linkshare already specified"); 1221 YYERROR; 1222 } 1223 hfsc_opts.linkshare.m2 = $2; 1224 hfsc_opts.linkshare.used = 1; 1225 } 1226 | LINKSHARE '(' bandwidth number bandwidth ')' { 1227 if (hfsc_opts.linkshare.used) { 1228 yyerror("linkshare already specified"); 1229 YYERROR; 1230 } 1231 hfsc_opts.linkshare.m1 = $3; 1232 hfsc_opts.linkshare.d = $4; 1233 hfsc_opts.linkshare.m2 = $5; 1234 hfsc_opts.linkshare.used = 1; 1235 } 1236 | REALTIME bandwidth { 1237 if (hfsc_opts.realtime.used) { 1238 yyerror("realtime already specified"); 1239 YYERROR; 1240 } 1241 hfsc_opts.realtime.m2 = $2; 1242 hfsc_opts.realtime.used = 1; 1243 } 1244 | REALTIME '(' bandwidth number bandwidth ')' { 1245 if (hfsc_opts.realtime.used) { 1246 yyerror("realtime already specified"); 1247 YYERROR; 1248 } 1249 hfsc_opts.realtime.m1 = $3; 1250 hfsc_opts.realtime.d = $4; 1251 hfsc_opts.realtime.m2 = $5; 1252 hfsc_opts.realtime.used = 1; 1253 } 1254 | UPPERLIMIT bandwidth { 1255 if (hfsc_opts.upperlimit.used) { 1256 yyerror("upperlimit already specified"); 1257 YYERROR; 1258 } 1259 hfsc_opts.upperlimit.m2 = $2; 1260 hfsc_opts.upperlimit.used = 1; 1261 } 1262 | UPPERLIMIT '(' bandwidth number bandwidth ')' { 1263 if (hfsc_opts.upperlimit.used) { 1264 yyerror("upperlimit already specified"); 1265 YYERROR; 1266 } 1267 hfsc_opts.upperlimit.m1 = $3; 1268 hfsc_opts.upperlimit.d = $4; 1269 hfsc_opts.upperlimit.m2 = $5; 1270 hfsc_opts.upperlimit.used = 1; 1271 } 1272 | STRING { 1273 if (!strcmp($1, "default")) 1274 hfsc_opts.flags |= HFCF_DEFAULTCLASS; 1275 else if (!strcmp($1, "red")) 1276 hfsc_opts.flags |= HFCF_RED; 1277 else if (!strcmp($1, "ecn")) 1278 hfsc_opts.flags |= HFCF_RED|HFCF_ECN; 1279 else if (!strcmp($1, "rio")) 1280 hfsc_opts.flags |= HFCF_RIO; 1281 else { 1282 yyerror("unknown hfsc flag \"%s\"", $1); 1283 YYERROR; 1284 } 1285 } 1286 ; 1287 1288qassign : /* empty */ { $$ = NULL; } 1289 | qassign_item { $$ = $1; } 1290 | '{' qassign_list '}' { $$ = $2; } 1291 ; 1292 1293qassign_list : qassign_item { $$ = $1; } 1294 | qassign_list comma qassign_item { 1295 $1->tail->next = $3; 1296 $1->tail = $3; 1297 $$ = $1; 1298 } 1299 ; 1300 1301qassign_item : STRING { 1302 $$ = calloc(1, sizeof(struct node_queue)); 1303 if ($$ == NULL) 1304 err(1, "qassign_item: calloc"); 1305 if (strlcpy($$->queue, $1, sizeof($$->queue)) >= 1306 sizeof($$->queue)) { 1307 free($$); 1308 yyerror("queue name '%s' too long (max " 1309 "%d chars)", $1, sizeof($$->queue)-1); 1310 YYERROR; 1311 } 1312 $$->next = NULL; 1313 $$->tail = $$; 1314 } 1315 ; 1316 1317pfrule : action dir logquick interface route af proto fromto 1318 filter_opts 1319 { 1320 struct pf_rule r; 1321 struct node_state_opt *o; 1322 struct node_proto *proto; 1323 1324 if (check_rulestate(PFCTL_STATE_FILTER)) 1325 YYERROR; 1326 1327 memset(&r, 0, sizeof(r)); 1328 1329 r.action = $1.b1; 1330 switch ($1.b2) { 1331 case PFRULE_RETURNRST: 1332 r.rule_flag |= PFRULE_RETURNRST; 1333 r.return_ttl = $1.w; 1334 break; 1335 case PFRULE_RETURNICMP: 1336 r.rule_flag |= PFRULE_RETURNICMP; 1337 r.return_icmp = $1.w; 1338 r.return_icmp6 = $1.w2; 1339 break; 1340 case PFRULE_RETURN: 1341 r.rule_flag |= PFRULE_RETURN; 1342 r.return_icmp = $1.w; 1343 r.return_icmp6 = $1.w2; 1344 break; 1345 } 1346 r.direction = $2; 1347 r.log = $3.log; 1348 r.quick = $3.quick; 1349 1350 r.af = $6; 1351 if ($9.tag) 1352 if (strlcpy(r.tagname, $9.tag, 1353 PF_TAG_NAME_SIZE) > PF_TAG_NAME_SIZE) { 1354 yyerror("tag too long, max %u chars", 1355 PF_TAG_NAME_SIZE - 1); 1356 YYERROR; 1357 } 1358 if ($9.match_tag) 1359 if (strlcpy(r.match_tagname, $9.match_tag, 1360 PF_TAG_NAME_SIZE) > PF_TAG_NAME_SIZE) { 1361 yyerror("tag too long, max %u chars", 1362 PF_TAG_NAME_SIZE - 1); 1363 YYERROR; 1364 } 1365 r.match_tag_not = $9.match_tag_not; 1366 r.flags = $9.flags.b1; 1367 r.flagset = $9.flags.b2; 1368 if (rule_label(&r, $9.label)) 1369 YYERROR; 1370 free($9.label); 1371 if ($9.flags.b1 || $9.flags.b2 || $8.src_os) { 1372 for (proto = $7; proto != NULL && 1373 proto->proto != IPPROTO_TCP; 1374 proto = proto->next) 1375 ; /* nothing */ 1376 if (proto == NULL && $7 != NULL) { 1377 if ($9.flags.b1 || $9.flags.b2) 1378 yyerror( 1379 "flags only apply to tcp"); 1380 if ($8.src_os) 1381 yyerror( 1382 "OS fingerprinting only " 1383 "apply to tcp"); 1384 YYERROR; 1385 } 1386#if 0 1387 if (($9.flags.b1 & parse_flags("S")) == 0 && 1388 $8.src_os) { 1389 yyerror("OS fingerprinting requires " 1390 "the SYN TCP flag (flags S/SA)"); 1391 YYERROR; 1392 } 1393#endif 1394 } 1395 1396 r.tos = $9.tos; 1397 r.keep_state = $9.keep.action; 1398 o = $9.keep.options; 1399 while (o) { 1400 struct node_state_opt *p = o; 1401 1402 switch (o->type) { 1403 case PF_STATE_OPT_MAX: 1404 if (r.max_states) { 1405 yyerror("state option 'max' " 1406 "multiple definitions"); 1407 YYERROR; 1408 } 1409 r.max_states = o->data.max_states; 1410 break; 1411 case PF_STATE_OPT_TIMEOUT: 1412 if (r.timeout[o->data.timeout.number]) { 1413 yyerror("state timeout %s " 1414 "multiple definitions", 1415 pf_timeouts[o->data. 1416 timeout.number].name); 1417 YYERROR; 1418 } 1419 r.timeout[o->data.timeout.number] = 1420 o->data.timeout.seconds; 1421 } 1422 o = o->next; 1423 free(p); 1424 } 1425 1426 if ($9.fragment) 1427 r.rule_flag |= PFRULE_FRAGMENT; 1428 r.allow_opts = $9.allowopts; 1429 1430 decide_address_family($8.src.host, &r.af); 1431 decide_address_family($8.dst.host, &r.af); 1432 1433 if ($5.rt) { 1434 if (!r.direction) { 1435 yyerror("direction must be explicit " 1436 "with rules that specify routing"); 1437 YYERROR; 1438 } 1439 r.rt = $5.rt; 1440 r.rpool.opts = $5.pool_opts; 1441 if ($5.key != NULL) 1442 memcpy(&r.rpool.key, $5.key, 1443 sizeof(struct pf_poolhashkey)); 1444 } 1445 if (r.rt && r.rt != PF_FASTROUTE) { 1446 decide_address_family($5.host, &r.af); 1447 remove_invalid_hosts(&$5.host, &r.af); 1448 if ($5.host == NULL) { 1449 yyerror("no routing address with " 1450 "matching address family found."); 1451 YYERROR; 1452 } 1453 if (r.rpool.opts == PF_POOL_NONE && ( 1454 $5.host->next != NULL || 1455 $5.host->addr.type == PF_ADDR_TABLE)) 1456 r.rpool.opts = PF_POOL_ROUNDROBIN; 1457 if (r.rpool.opts != PF_POOL_ROUNDROBIN) 1458 if (disallow_table($5.host, "tables " 1459 "are only supported in round-robin " 1460 "routing pools")) 1461 YYERROR; 1462 if ($5.host->next != NULL) { 1463 if (r.rpool.opts != 1464 PF_POOL_ROUNDROBIN) { 1465 yyerror("r.rpool.opts must " 1466 "be PF_POOL_ROUNDROBIN"); 1467 YYERROR; 1468 } 1469 } 1470 } 1471 if ($9.queues.qname != NULL) { 1472 if (strlcpy(r.qname, $9.queues.qname, 1473 sizeof(r.qname)) >= sizeof(r.qname)) { 1474 yyerror("rule qname too long (max " 1475 "%d chars)", sizeof(r.qname)-1); 1476 YYERROR; 1477 } 1478 free($9.queues.qname); 1479 } 1480 if ($9.queues.pqname != NULL) { 1481 if (strlcpy(r.pqname, $9.queues.pqname, 1482 sizeof(r.pqname)) >= sizeof(r.pqname)) { 1483 yyerror("rule pqname too long (max " 1484 "%d chars)", sizeof(r.pqname)-1); 1485 YYERROR; 1486 } 1487 free($9.queues.pqname); 1488 } 1489 1490 expand_rule(&r, $4, $5.host, $7, $8.src_os, 1491 $8.src.host, $8.src.port, $8.dst.host, $8.dst.port, 1492 $9.uid, $9.gid, $9.icmpspec); 1493 } 1494 ; 1495 1496filter_opts : { bzero(&filter_opts, sizeof filter_opts); } 1497 filter_opts_l 1498 { $$ = filter_opts; } 1499 | /* empty */ { 1500 bzero(&filter_opts, sizeof filter_opts); 1501 $$ = filter_opts; 1502 } 1503 ; 1504 1505filter_opts_l : filter_opts_l filter_opt 1506 | filter_opt 1507 ; 1508 1509filter_opt : USER uids { 1510 if (filter_opts.uid) 1511 $2->tail->next = filter_opts.uid; 1512 filter_opts.uid = $2; 1513 } 1514 | GROUP gids { 1515 if (filter_opts.gid) 1516 $2->tail->next = filter_opts.gid; 1517 filter_opts.gid = $2; 1518 } 1519 | flags { 1520 if (filter_opts.marker & FOM_FLAGS) { 1521 yyerror("flags cannot be redefined"); 1522 YYERROR; 1523 } 1524 filter_opts.marker |= FOM_FLAGS; 1525 filter_opts.flags.b1 |= $1.b1; 1526 filter_opts.flags.b2 |= $1.b2; 1527 filter_opts.flags.w |= $1.w; 1528 filter_opts.flags.w2 |= $1.w2; 1529 } 1530 | icmpspec { 1531 if (filter_opts.marker & FOM_ICMP) { 1532 yyerror("icmp-type cannot be redefined"); 1533 YYERROR; 1534 } 1535 filter_opts.marker |= FOM_ICMP; 1536 filter_opts.icmpspec = $1; 1537 } 1538 | tos { 1539 if (filter_opts.marker & FOM_TOS) { 1540 yyerror("tos cannot be redefined"); 1541 YYERROR; 1542 } 1543 filter_opts.marker |= FOM_TOS; 1544 filter_opts.tos = $1; 1545 } 1546 | keep { 1547 if (filter_opts.marker & FOM_KEEP) { 1548 yyerror("modulate or keep cannot be redefined"); 1549 YYERROR; 1550 } 1551 filter_opts.marker |= FOM_KEEP; 1552 filter_opts.keep.action = $1.action; 1553 filter_opts.keep.options = $1.options; 1554 } 1555 | FRAGMENT { 1556 filter_opts.fragment = 1; 1557 } 1558 | ALLOWOPTS { 1559 filter_opts.allowopts = 1; 1560 } 1561 | label { 1562 if (filter_opts.label) { 1563 yyerror("label cannot be redefined"); 1564 YYERROR; 1565 } 1566 filter_opts.label = $1; 1567 } 1568 | qname { 1569 if (filter_opts.queues.qname) { 1570 yyerror("queue cannot be redefined"); 1571 YYERROR; 1572 } 1573 filter_opts.queues = $1; 1574 } 1575 | TAG string { 1576 filter_opts.tag = $2; 1577 } 1578 | not TAGGED string { 1579 filter_opts.match_tag = $3; 1580 filter_opts.match_tag_not = $1; 1581 } 1582 ; 1583 1584action : PASS { $$.b1 = PF_PASS; $$.b2 = $$.w = 0; } 1585 | BLOCK blockspec { $$ = $2; $$.b1 = PF_DROP; } 1586 ; 1587 1588blockspec : /* empty */ { 1589 $$.b2 = blockpolicy; 1590 $$.w = returnicmpdefault; 1591 $$.w2 = returnicmp6default; 1592 } 1593 | DROP { 1594 $$.b2 = PFRULE_DROP; 1595 $$.w = 0; 1596 $$.w2 = 0; 1597 } 1598 | RETURNRST { 1599 $$.b2 = PFRULE_RETURNRST; 1600 $$.w = 0; 1601 $$.w2 = 0; 1602 } 1603 | RETURNRST '(' TTL number ')' { 1604 if ($4 > 255) { 1605 yyerror("illegal ttl value %d", $4); 1606 YYERROR; 1607 } 1608 $$.b2 = PFRULE_RETURNRST; 1609 $$.w = $4; 1610 $$.w2 = 0; 1611 } 1612 | RETURNICMP { 1613 $$.b2 = PFRULE_RETURNICMP; 1614 $$.w = returnicmpdefault; 1615 $$.w2 = returnicmp6default; 1616 } 1617 | RETURNICMP6 { 1618 $$.b2 = PFRULE_RETURNICMP; 1619 $$.w = returnicmpdefault; 1620 $$.w2 = returnicmp6default; 1621 } 1622 | RETURNICMP '(' STRING ')' { 1623 $$.b2 = PFRULE_RETURNICMP; 1624 if (!($$.w = parseicmpspec($3, AF_INET))) 1625 YYERROR; 1626 $$.w2 = returnicmp6default; 1627 } 1628 | RETURNICMP6 '(' STRING ')' { 1629 $$.b2 = PFRULE_RETURNICMP; 1630 $$.w = returnicmpdefault; 1631 if (!($$.w2 = parseicmpspec($3, AF_INET6))) 1632 YYERROR; 1633 } 1634 | RETURNICMP '(' STRING comma STRING ')' { 1635 $$.b2 = PFRULE_RETURNICMP; 1636 if (!($$.w = parseicmpspec($3, AF_INET))) 1637 YYERROR; 1638 if (!($$.w2 = parseicmpspec($5, AF_INET6))) 1639 YYERROR; 1640 } 1641 | RETURN { 1642 $$.b2 = PFRULE_RETURN; 1643 $$.w = returnicmpdefault; 1644 $$.w2 = returnicmp6default; 1645 } 1646 ; 1647 1648dir : /* empty */ { $$ = 0; } 1649 | IN { $$ = PF_IN; } 1650 | OUT { $$ = PF_OUT; } 1651 ; 1652 1653logquick : /* empty */ { $$.log = 0; $$.quick = 0; } 1654 | log { $$.log = $1; $$.quick = 0; } 1655 | QUICK { $$.log = 0; $$.quick = 1; } 1656 | log QUICK { $$.log = $1; $$.quick = 1; } 1657 | QUICK log { $$.log = $2; $$.quick = 1; } 1658 ; 1659 1660log : LOG { $$ = 1; } 1661 | LOGALL { $$ = 2; } 1662 ; 1663 1664interface : /* empty */ { $$ = NULL; } 1665 | ON if_item_not { $$ = $2; } 1666 | ON '{' if_list '}' { $$ = $3; } 1667 ; 1668 1669if_list : if_item_not { $$ = $1; } 1670 | if_list comma if_item_not { 1671 $1->tail->next = $3; 1672 $1->tail = $3; 1673 $$ = $1; 1674 } 1675 ; 1676 1677if_item_not : not if_item { $$ = $2; $$->not = $1; } 1678 ; 1679 1680if_item : STRING { 1681 struct node_host *n; 1682 1683 if ((n = ifa_exists($1)) == NULL) { 1684 yyerror("unknown interface %s", $1); 1685 YYERROR; 1686 } 1687 $$ = calloc(1, sizeof(struct node_if)); 1688 if ($$ == NULL) 1689 err(1, "if_item: calloc"); 1690 if (strlcpy($$->ifname, $1, sizeof($$->ifname)) >= 1691 sizeof($$->ifname)) { 1692 free($$); 1693 yyerror("interface name too long"); 1694 YYERROR; 1695 } 1696 $$->ifa_flags = n->ifa_flags; 1697 $$->not = 0; 1698 $$->next = NULL; 1699 $$->tail = $$; 1700 } 1701 ; 1702 1703af : /* empty */ { $$ = 0; } 1704 | INET { $$ = AF_INET; } 1705 | INET6 { $$ = AF_INET6; } 1706 1707proto : /* empty */ { $$ = NULL; } 1708 | PROTO proto_item { $$ = $2; } 1709 | PROTO '{' proto_list '}' { $$ = $3; } 1710 ; 1711 1712proto_list : proto_item { $$ = $1; } 1713 | proto_list comma proto_item { 1714 $1->tail->next = $3; 1715 $1->tail = $3; 1716 $$ = $1; 1717 } 1718 ; 1719 1720proto_item : STRING { 1721 u_int8_t pr; 1722 u_long ulval; 1723 1724 if (atoul($1, &ulval) == 0) { 1725 if (ulval > 255) { 1726 yyerror("protocol outside range"); 1727 YYERROR; 1728 } 1729 pr = (u_int8_t)ulval; 1730 } else { 1731 struct protoent *p; 1732 1733 p = getprotobyname($1); 1734 if (p == NULL) { 1735 yyerror("unknown protocol %s", $1); 1736 YYERROR; 1737 } 1738 pr = p->p_proto; 1739 } 1740 if (pr == 0) { 1741 yyerror("proto 0 cannot be used"); 1742 YYERROR; 1743 } 1744 $$ = calloc(1, sizeof(struct node_proto)); 1745 if ($$ == NULL) 1746 err(1, "proto_item: calloc"); 1747 $$->proto = pr; 1748 $$->next = NULL; 1749 $$->tail = $$; 1750 } 1751 ; 1752 1753fromto : ALL { 1754 $$.src.host = NULL; 1755 $$.src.port = NULL; 1756 $$.dst.host = NULL; 1757 $$.dst.port = NULL; 1758 $$.src_os = NULL; 1759 } 1760 | from os to { 1761 $$.src = $1; 1762 $$.src_os = $2; 1763 $$.dst = $3; 1764 } 1765 ; 1766 1767os : /* empty */ { $$ = NULL; } 1768 | OS xos { $$ = $2; } 1769 | OS '{' os_list '}' { $$ = $3; } 1770 ; 1771 1772xos : STRING { 1773 $$ = calloc(1, sizeof(struct node_os)); 1774 if ($$ == NULL) 1775 err(1, "os: calloc"); 1776 $$->os = $1; 1777 $$->tail = $$; 1778 } 1779 ; 1780 1781os_list : xos { $$ = $1; } 1782 | os_list comma xos { 1783 $1->tail->next = $3; 1784 $1->tail = $3; 1785 $$ = $1; 1786 } 1787 ; 1788 1789from : /* empty */ { 1790 $$.host = NULL; 1791 $$.port = NULL; 1792 } 1793 | FROM ipportspec { 1794 $$ = $2; 1795 } 1796 ; 1797 1798to : /* empty */ { 1799 $$.host = NULL; 1800 $$.port = NULL; 1801 } 1802 | TO ipportspec { 1803 $$ = $2; 1804 } 1805 ; 1806 1807ipportspec : ipspec { 1808 $$.host = $1; 1809 $$.port = NULL; 1810 } 1811 | ipspec PORT portspec { 1812 $$.host = $1; 1813 $$.port = $3; 1814 } 1815 | PORT portspec { 1816 $$.host = NULL; 1817 $$.port = $2; 1818 } 1819 ; 1820 1821ipspec : ANY { $$ = NULL; } 1822 | xhost { $$ = $1; } 1823 | '{' host_list '}' { $$ = $2; } 1824 ; 1825 1826host_list : xhost { $$ = $1; } 1827 | host_list comma xhost { 1828 if ($3 == NULL) 1829 $$ = $1; 1830 else if ($1 == NULL) 1831 $$ = $3; 1832 else { 1833 $1->tail->next = $3; 1834 $1->tail = $3->tail; 1835 $$ = $1; 1836 } 1837 } 1838 ; 1839 1840xhost : not host { 1841 struct node_host *n; 1842 1843 for (n = $2; n != NULL; n = n->next) 1844 n->not = $1; 1845 $$ = $2; 1846 } 1847 | NOROUTE { 1848 $$ = calloc(1, sizeof(struct node_host)); 1849 if ($$ == NULL) 1850 err(1, "xhost: calloc"); 1851 $$->addr.type = PF_ADDR_NOROUTE; 1852 $$->next = NULL; 1853 $$->tail = $$; 1854 } 1855 ; 1856 1857host : STRING { 1858 if (($$ = host($1)) == NULL) { 1859 /* error. "any" is handled elsewhere */ 1860 yyerror("could not parse host specification"); 1861 YYERROR; 1862 } 1863 1864 } 1865 | STRING '/' number { 1866 char *buf; 1867 1868 if (asprintf(&buf, "%s/%u", $1, $3) == -1) 1869 err(1, "host: asprintf"); 1870 if (($$ = host(buf)) == NULL) { 1871 /* error. "any" is handled elsewhere */ 1872 free(buf); 1873 yyerror("could not parse host specification"); 1874 YYERROR; 1875 } 1876 free(buf); 1877 } 1878 | dynaddr 1879 | dynaddr '/' number { 1880 struct node_host *n; 1881 1882 $$ = $1; 1883 for (n = $1; n != NULL; n = n->next) 1884 set_ipmask(n, $3); 1885 } 1886 | '<' STRING '>' { 1887 if (strlen($2) >= PF_TABLE_NAME_SIZE) { 1888 yyerror("table name '%s' too long"); 1889 YYERROR; 1890 } 1891 $$ = calloc(1, sizeof(struct node_host)); 1892 if ($$ == NULL) 1893 err(1, "host: calloc"); 1894 $$->addr.type = PF_ADDR_TABLE; 1895 if (strlcpy($$->addr.v.tblname, $2, 1896 sizeof($$->addr.v.tblname)) >= 1897 sizeof($$->addr.v.tblname)) 1898 errx(1, "host: strlcpy"); 1899 $$->next = NULL; 1900 $$->tail = $$; 1901 } 1902 ; 1903 1904number : STRING { 1905 u_long ulval; 1906 1907 if (atoul($1, &ulval) == -1) { 1908 yyerror("%s is not a number", $1); 1909 YYERROR; 1910 } else 1911 $$ = ulval; 1912 } 1913 ; 1914 1915dynaddr : '(' STRING ')' { 1916 if (ifa_exists($2) == NULL) { 1917 yyerror("interface %s does not exist", $2); 1918 YYERROR; 1919 } 1920 $$ = calloc(1, sizeof(struct node_host)); 1921 if ($$ == NULL) 1922 err(1, "address: calloc"); 1923 $$->af = 0; 1924 set_ipmask($$, 128); 1925 $$->addr.type = PF_ADDR_DYNIFTL; 1926 if (strlcpy($$->addr.v.ifname, $2, 1927 sizeof($$->addr.v.ifname)) >= 1928 sizeof($$->addr.v.ifname)) { 1929 free($$); 1930 yyerror("interface name too long"); 1931 YYERROR; 1932 } 1933 $$->next = NULL; 1934 $$->tail = $$; 1935 } 1936 ; 1937 1938portspec : port_item { $$ = $1; } 1939 | '{' port_list '}' { $$ = $2; } 1940 ; 1941 1942port_list : port_item { $$ = $1; } 1943 | port_list comma port_item { 1944 $1->tail->next = $3; 1945 $1->tail = $3; 1946 $$ = $1; 1947 } 1948 ; 1949 1950port_item : port { 1951 $$ = calloc(1, sizeof(struct node_port)); 1952 if ($$ == NULL) 1953 err(1, "port_item: calloc"); 1954 $$->port[0] = $1.a; 1955 $$->port[1] = $1.b; 1956 if ($1.t) 1957 $$->op = PF_OP_RRG; 1958 else 1959 $$->op = PF_OP_EQ; 1960 $$->next = NULL; 1961 $$->tail = $$; 1962 } 1963 | unaryop port { 1964 if ($2.t) { 1965 yyerror("':' cannot be used with an other " 1966 "port operator"); 1967 YYERROR; 1968 } 1969 $$ = calloc(1, sizeof(struct node_port)); 1970 if ($$ == NULL) 1971 err(1, "port_item: calloc"); 1972 $$->port[0] = $2.a; 1973 $$->port[1] = $2.b; 1974 $$->op = $1; 1975 $$->next = NULL; 1976 $$->tail = $$; 1977 } 1978 | port PORTBINARY port { 1979 if ($1.t || $3.t) { 1980 yyerror("':' cannot be used with an other " 1981 "port operator"); 1982 YYERROR; 1983 } 1984 $$ = calloc(1, sizeof(struct node_port)); 1985 if ($$ == NULL) 1986 err(1, "port_item: calloc"); 1987 $$->port[0] = $1.a; 1988 $$->port[1] = $3.a; 1989 $$->op = $2; 1990 $$->next = NULL; 1991 $$->tail = $$; 1992 } 1993 ; 1994 1995port : STRING { 1996 char *p = strchr($1, ':'); 1997 struct servent *s = NULL; 1998 u_long ulval; 1999 2000 if (p == NULL) { 2001 if (atoul($1, &ulval) == 0) { 2002 if (ulval > 65535) { 2003 yyerror("illegal port value %d", 2004 ulval); 2005 YYERROR; 2006 } 2007 $$.a = htons(ulval); 2008 } else { 2009 s = getservbyname($1, "tcp"); 2010 if (s == NULL) 2011 s = getservbyname($1, "udp"); 2012 if (s == NULL) { 2013 yyerror("unknown port %s", $1); 2014 YYERROR; 2015 } 2016 $$.a = s->s_port; 2017 } 2018 $$.b = 0; 2019 $$.t = 0; 2020 } else { 2021 int port[2]; 2022 2023 *p++ = 0; 2024 if ((port[0] = getservice($1)) == -1 || 2025 (port[1] = getservice(p)) == -1) 2026 YYERROR; 2027 $$.a = port[0]; 2028 $$.b = port[1]; 2029 $$.t = PF_OP_RRG; 2030 } 2031 } 2032 ; 2033 2034uids : uid_item { $$ = $1; } 2035 | '{' uid_list '}' { $$ = $2; } 2036 ; 2037 2038uid_list : uid_item { $$ = $1; } 2039 | uid_list comma uid_item { 2040 $1->tail->next = $3; 2041 $1->tail = $3; 2042 $$ = $1; 2043 } 2044 ; 2045 2046uid_item : uid { 2047 $$ = calloc(1, sizeof(struct node_uid)); 2048 if ($$ == NULL) 2049 err(1, "uid_item: calloc"); 2050 $$->uid[0] = $1; 2051 $$->uid[1] = $1; 2052 $$->op = PF_OP_EQ; 2053 $$->next = NULL; 2054 $$->tail = $$; 2055 } 2056 | unaryop uid { 2057 if ($2 == UID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) { 2058 yyerror("user unknown requires operator = or " 2059 "!="); 2060 YYERROR; 2061 } 2062 $$ = calloc(1, sizeof(struct node_uid)); 2063 if ($$ == NULL) 2064 err(1, "uid_item: calloc"); 2065 $$->uid[0] = $2; 2066 $$->uid[1] = $2; 2067 $$->op = $1; 2068 $$->next = NULL; 2069 $$->tail = $$; 2070 } 2071 | uid PORTBINARY uid { 2072 if ($1 == UID_MAX || $3 == UID_MAX) { 2073 yyerror("user unknown requires operator = or " 2074 "!="); 2075 YYERROR; 2076 } 2077 $$ = calloc(1, sizeof(struct node_uid)); 2078 if ($$ == NULL) 2079 err(1, "uid_item: calloc"); 2080 $$->uid[0] = $1; 2081 $$->uid[1] = $3; 2082 $$->op = $2; 2083 $$->next = NULL; 2084 $$->tail = $$; 2085 } 2086 ; 2087 2088uid : STRING { 2089 u_long ulval; 2090 2091 if (atoul($1, &ulval) == -1) { 2092 if (!strcmp($1, "unknown")) 2093 $$ = UID_MAX; 2094 else { 2095 struct passwd *pw; 2096 2097 if ((pw = getpwnam($1)) == NULL) { 2098 yyerror("unknown user %s", $1); 2099 YYERROR; 2100 } 2101 $$ = pw->pw_uid; 2102 } 2103 } else { 2104 if (ulval >= UID_MAX) { 2105 yyerror("illegal uid value %lu", ulval); 2106 YYERROR; 2107 } 2108 $$ = ulval; 2109 } 2110 } 2111 ; 2112 2113gids : gid_item { $$ = $1; } 2114 | '{' gid_list '}' { $$ = $2; } 2115 ; 2116 2117gid_list : gid_item { $$ = $1; } 2118 | gid_list comma gid_item { 2119 $1->tail->next = $3; 2120 $1->tail = $3; 2121 $$ = $1; 2122 } 2123 ; 2124 2125gid_item : gid { 2126 $$ = calloc(1, sizeof(struct node_gid)); 2127 if ($$ == NULL) 2128 err(1, "gid_item: calloc"); 2129 $$->gid[0] = $1; 2130 $$->gid[1] = $1; 2131 $$->op = PF_OP_EQ; 2132 $$->next = NULL; 2133 $$->tail = $$; 2134 } 2135 | unaryop gid { 2136 if ($2 == GID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) { 2137 yyerror("group unknown requires operator = or " 2138 "!="); 2139 YYERROR; 2140 } 2141 $$ = calloc(1, sizeof(struct node_gid)); 2142 if ($$ == NULL) 2143 err(1, "gid_item: calloc"); 2144 $$->gid[0] = $2; 2145 $$->gid[1] = $2; 2146 $$->op = $1; 2147 $$->next = NULL; 2148 $$->tail = $$; 2149 } 2150 | gid PORTBINARY gid { 2151 if ($1 == GID_MAX || $3 == GID_MAX) { 2152 yyerror("group unknown requires operator = or " 2153 "!="); 2154 YYERROR; 2155 } 2156 $$ = calloc(1, sizeof(struct node_gid)); 2157 if ($$ == NULL) 2158 err(1, "gid_item: calloc"); 2159 $$->gid[0] = $1; 2160 $$->gid[1] = $3; 2161 $$->op = $2; 2162 $$->next = NULL; 2163 $$->tail = $$; 2164 } 2165 ; 2166 2167gid : STRING { 2168 u_long ulval; 2169 2170 if (atoul($1, &ulval) == -1) { 2171 if (!strcmp($1, "unknown")) 2172 $$ = GID_MAX; 2173 else { 2174 struct group *grp; 2175 2176 if ((grp = getgrnam($1)) == NULL) { 2177 yyerror("unknown group %s", $1); 2178 YYERROR; 2179 } 2180 $$ = grp->gr_gid; 2181 } 2182 } else { 2183 if (ulval >= GID_MAX) { 2184 yyerror("illegal gid value %lu", ulval); 2185 YYERROR; 2186 } 2187 $$ = ulval; 2188 } 2189 } 2190 ; 2191 2192flag : STRING { 2193 int f; 2194 2195 if ((f = parse_flags($1)) < 0) { 2196 yyerror("bad flags %s", $1); 2197 YYERROR; 2198 } 2199 $$.b1 = f; 2200 } 2201 ; 2202 2203flags : FLAGS flag '/' flag { $$.b1 = $2.b1; $$.b2 = $4.b1; } 2204 | FLAGS '/' flag { $$.b1 = 0; $$.b2 = $3.b1; } 2205 ; 2206 2207icmpspec : ICMPTYPE icmp_item { $$ = $2; } 2208 | ICMPTYPE '{' icmp_list '}' { $$ = $3; } 2209 | ICMP6TYPE icmp6_item { $$ = $2; } 2210 | ICMP6TYPE '{' icmp6_list '}' { $$ = $3; } 2211 ; 2212 2213icmp_list : icmp_item { $$ = $1; } 2214 | icmp_list comma icmp_item { 2215 $1->tail->next = $3; 2216 $1->tail = $3; 2217 $$ = $1; 2218 } 2219 ; 2220 2221icmp6_list : icmp6_item { $$ = $1; } 2222 | icmp6_list comma icmp6_item { 2223 $1->tail->next = $3; 2224 $1->tail = $3; 2225 $$ = $1; 2226 } 2227 ; 2228 2229icmp_item : icmptype { 2230 $$ = calloc(1, sizeof(struct node_icmp)); 2231 if ($$ == NULL) 2232 err(1, "icmp_item: calloc"); 2233 $$->type = $1; 2234 $$->code = 0; 2235 $$->proto = IPPROTO_ICMP; 2236 $$->next = NULL; 2237 $$->tail = $$; 2238 } 2239 | icmptype CODE STRING { 2240 const struct icmpcodeent *p; 2241 u_long ulval; 2242 2243 if (atoul($3, &ulval) == 0) { 2244 if (ulval > 255) { 2245 yyerror("illegal icmp-code %d", ulval); 2246 YYERROR; 2247 } 2248 } else { 2249 if ((p = geticmpcodebyname($1-1, $3, 2250 AF_INET)) == NULL) { 2251 yyerror("unknown icmp-code %s", $3); 2252 YYERROR; 2253 } 2254 ulval = p->code; 2255 } 2256 $$ = calloc(1, sizeof(struct node_icmp)); 2257 if ($$ == NULL) 2258 err(1, "icmp_item: calloc"); 2259 $$->type = $1; 2260 $$->code = ulval + 1; 2261 $$->proto = IPPROTO_ICMP; 2262 $$->next = NULL; 2263 $$->tail = $$; 2264 } 2265 ; 2266 2267icmp6_item : icmp6type { 2268 $$ = calloc(1, sizeof(struct node_icmp)); 2269 if ($$ == NULL) 2270 err(1, "icmp_item: calloc"); 2271 $$->type = $1; 2272 $$->code = 0; 2273 $$->proto = IPPROTO_ICMPV6; 2274 $$->next = NULL; 2275 $$->tail = $$; 2276 } 2277 | icmp6type CODE STRING { 2278 const struct icmpcodeent *p; 2279 u_long ulval; 2280 2281 if (atoul($3, &ulval) == 0) { 2282 if (ulval > 255) { 2283 yyerror("illegal icmp6-code %ld", 2284 ulval); 2285 YYERROR; 2286 } 2287 } else { 2288 if ((p = geticmpcodebyname($1-1, $3, 2289 AF_INET6)) == NULL) { 2290 yyerror("unknown icmp6-code %s", $3); 2291 YYERROR; 2292 } 2293 ulval = p->code; 2294 } 2295 $$ = calloc(1, sizeof(struct node_icmp)); 2296 if ($$ == NULL) 2297 err(1, "icmp_item: calloc"); 2298 $$->type = $1; 2299 $$->code = ulval + 1; 2300 $$->proto = IPPROTO_ICMPV6; 2301 $$->next = NULL; 2302 $$->tail = $$; 2303 } 2304 ; 2305 2306icmptype : STRING { 2307 const struct icmptypeent *p; 2308 u_long ulval; 2309 2310 if (atoul($1, &ulval) == 0) { 2311 if (ulval > 255) { 2312 yyerror("illegal icmp-type %d", ulval); 2313 YYERROR; 2314 } 2315 $$ = ulval + 1; 2316 } else { 2317 if ((p = geticmptypebyname($1, AF_INET)) == 2318 NULL) { 2319 yyerror("unknown icmp-type %s", $1); 2320 YYERROR; 2321 } 2322 $$ = p->type + 1; 2323 } 2324 } 2325 ; 2326 2327icmp6type : STRING { 2328 const struct icmptypeent *p; 2329 u_long ulval; 2330 2331 if (atoul($1, &ulval) == 0) { 2332 if (ulval > 255) { 2333 yyerror("illegal icmp6-type %d", ulval); 2334 YYERROR; 2335 } 2336 $$ = ulval + 1; 2337 } else { 2338 if ((p = geticmptypebyname($1, AF_INET6)) == 2339 NULL) { 2340 yyerror("unknown icmp6-type %s", $1); 2341 YYERROR; 2342 } 2343 $$ = p->type + 1; 2344 } 2345 } 2346 ; 2347 2348tos : TOS STRING { 2349 if (!strcmp($2, "lowdelay")) 2350 $$ = IPTOS_LOWDELAY; 2351 else if (!strcmp($2, "throughput")) 2352 $$ = IPTOS_THROUGHPUT; 2353 else if (!strcmp($2, "reliability")) 2354 $$ = IPTOS_RELIABILITY; 2355 else if ($2[0] == '0' && $2[1] == 'x') 2356 $$ = strtoul($2, NULL, 16); 2357 else 2358 $$ = strtoul($2, NULL, 10); 2359 if (!$$ || $$ > 255) { 2360 yyerror("illegal tos value %s", $2); 2361 YYERROR; 2362 } 2363 } 2364 ; 2365 2366keep : KEEP STATE state_opt_spec { 2367 $$.action = PF_STATE_NORMAL; 2368 $$.options = $3; 2369 } 2370 | MODULATE STATE state_opt_spec { 2371 $$.action = PF_STATE_MODULATE; 2372 $$.options = $3; 2373 } 2374 | SYNPROXY STATE state_opt_spec { 2375 $$.action = PF_STATE_SYNPROXY; 2376 $$.options = $3; 2377 } 2378 ; 2379 2380state_opt_spec : '(' state_opt_list ')' { $$ = $2; } 2381 | /* empty */ { $$ = NULL; } 2382 ; 2383 2384state_opt_list : state_opt_item { $$ = $1; } 2385 | state_opt_list comma state_opt_item { 2386 $1->tail->next = $3; 2387 $1->tail = $3; 2388 $$ = $1; 2389 } 2390 ; 2391 2392state_opt_item : MAXIMUM number { 2393 $$ = calloc(1, sizeof(struct node_state_opt)); 2394 if ($$ == NULL) 2395 err(1, "state_opt_item: calloc"); 2396 $$->type = PF_STATE_OPT_MAX; 2397 $$->data.max_states = $2; 2398 $$->next = NULL; 2399 $$->tail = $$; 2400 } 2401 | STRING number { 2402 int i; 2403 2404 for (i = 0; pf_timeouts[i].name && 2405 strcmp(pf_timeouts[i].name, $1); ++i) 2406 ; /* nothing */ 2407 if (!pf_timeouts[i].name) { 2408 yyerror("illegal timeout name %s", $1); 2409 YYERROR; 2410 } 2411 if (strchr(pf_timeouts[i].name, '.') == NULL) { 2412 yyerror("illegal state timeout %s", $1); 2413 YYERROR; 2414 } 2415 $$ = calloc(1, sizeof(struct node_state_opt)); 2416 if ($$ == NULL) 2417 err(1, "state_opt_item: calloc"); 2418 $$->type = PF_STATE_OPT_TIMEOUT; 2419 $$->data.timeout.number = pf_timeouts[i].timeout; 2420 $$->data.timeout.seconds = $2; 2421 $$->next = NULL; 2422 $$->tail = $$; 2423 } 2424 ; 2425 2426label : LABEL STRING { 2427 if (($$ = strdup($2)) == NULL) 2428 err(1, "rule label strdup() failed"); 2429 } 2430 ; 2431 2432qname : QUEUE STRING { 2433 if (($$.qname = strdup($2)) == NULL) 2434 err(1, "qname strdup() failed"); 2435 } 2436 | QUEUE '(' STRING ')' { 2437 if (($$.qname = strdup($3)) == NULL) 2438 err(1, "qname strdup() failed"); 2439 } 2440 | QUEUE '(' STRING comma STRING ')' { 2441 if (($$.qname = strdup($3)) == NULL || 2442 ($$.pqname = strdup($5)) == NULL) 2443 err(1, "qname strdup() failed"); 2444 } 2445 ; 2446 2447no : /* empty */ { $$ = 0; } 2448 | NO { $$ = 1; } 2449 ; 2450 2451rport : STRING { 2452 char *p = strchr($1, ':'); 2453 2454 if (p == NULL) { 2455 if (($$.a = getservice($1)) == -1) 2456 YYERROR; 2457 $$.b = $$.t = 0; 2458 } else if (!strcmp(p+1, "*")) { 2459 *p = 0; 2460 if (($$.a = getservice($1)) == -1) 2461 YYERROR; 2462 $$.b = 0; 2463 $$.t = 1; 2464 } else { 2465 *p++ = 0; 2466 if (($$.a = getservice($1)) == -1 || 2467 ($$.b = getservice(p)) == -1) 2468 YYERROR; 2469 if ($$.a == $$.b) 2470 $$.b = 0; 2471 $$.t = 0; 2472 } 2473 } 2474 ; 2475 2476redirspec : host { $$ = $1; } 2477 | '{' redir_host_list '}' { $$ = $2; } 2478 ; 2479 2480redir_host_list : host { $$ = $1; } 2481 | redir_host_list comma host { 2482 $1->tail->next = $3; 2483 $1->tail = $3->tail; 2484 $$ = $1; 2485 } 2486 ; 2487 2488redirpool : /* empty */ { $$ = NULL; } 2489 | ARROW redirspec { 2490 $$ = calloc(1, sizeof(struct redirection)); 2491 if ($$ == NULL) 2492 err(1, "redirection: calloc"); 2493 $$->host = $2; 2494 $$->rport.a = $$->rport.b = $$->rport.t = 0; 2495 } 2496 | ARROW redirspec PORT rport { 2497 $$ = calloc(1, sizeof(struct redirection)); 2498 if ($$ == NULL) 2499 err(1, "redirection: calloc"); 2500 $$->host = $2; 2501 $$->rport = $4; 2502 } 2503 ; 2504 2505hashkey : /* empty */ 2506 { 2507 $$ = calloc(1, sizeof(struct pf_poolhashkey)); 2508 if ($$ == NULL) 2509 err(1, "hashkey: calloc"); 2510 $$->key32[0] = arc4random(); 2511 $$->key32[1] = arc4random(); 2512 $$->key32[2] = arc4random(); 2513 $$->key32[3] = arc4random(); 2514 } 2515 | string 2516 { 2517 if (!strncmp($1, "0x", 2)) { 2518 if (strlen($1) != 34) { 2519 yyerror("hex key must be 128 bits " 2520 "(32 hex digits) long"); 2521 YYERROR; 2522 } 2523 $$ = calloc(1, sizeof(struct pf_poolhashkey)); 2524 if ($$ == NULL) 2525 err(1, "hashkey: calloc"); 2526 2527 if (sscanf($1, "0x%8x%8x%8x%8x", 2528 &$$->key32[0], &$$->key32[1], 2529 &$$->key32[2], &$$->key32[3]) != 4) { 2530 free($$); 2531 yyerror("invalid hex key"); 2532 YYERROR; 2533 } 2534 } else { 2535 MD5_CTX context; 2536 2537 $$ = calloc(1, sizeof(struct pf_poolhashkey)); 2538 if ($$ == NULL) 2539 err(1, "hashkey: calloc"); 2540 MD5Init(&context); 2541 MD5Update(&context, (unsigned char *)$1, 2542 strlen($1)); 2543 MD5Final((unsigned char *)$$, &context); 2544 HTONL($$->key32[0]); 2545 HTONL($$->key32[1]); 2546 HTONL($$->key32[2]); 2547 HTONL($$->key32[3]); 2548 } 2549 } 2550 ; 2551 2552pooltype : /* empty */ 2553 { 2554 $$.type = PF_POOL_NONE; 2555 $$.key = NULL; 2556 } 2557 | BITMASK 2558 { 2559 $$.type = PF_POOL_BITMASK; 2560 $$.key = NULL; 2561 } 2562 | RANDOM 2563 { 2564 $$.type = PF_POOL_RANDOM; 2565 $$.key = NULL; 2566 } 2567 | SOURCEHASH hashkey 2568 { 2569 $$.type = PF_POOL_SRCHASH; 2570 $$.key = $2; 2571 } 2572 | ROUNDROBIN 2573 { 2574 $$.type = PF_POOL_ROUNDROBIN; 2575 $$.key = NULL; 2576 } 2577 ; 2578 2579staticport : /* empty */ { $$ = 0; } 2580 | STATICPORT { $$ = 1; } 2581 ; 2582 2583redirection : /* empty */ { $$ = NULL; } 2584 | ARROW host { 2585 $$ = calloc(1, sizeof(struct redirection)); 2586 if ($$ == NULL) 2587 err(1, "redirection: calloc"); 2588 $$->host = $2; 2589 $$->rport.a = $$->rport.b = $$->rport.t = 0; 2590 } 2591 | ARROW host PORT rport { 2592 $$ = calloc(1, sizeof(struct redirection)); 2593 if ($$ == NULL) 2594 err(1, "redirection: calloc"); 2595 $$->host = $2; 2596 $$->rport = $4; 2597 } 2598 ; 2599 2600natpass : /* empty */ { $$ = 0; } 2601 | PASS { $$ = 1; } 2602 ; 2603 2604nataction : no NAT natpass { 2605 $$.b2 = $$.w = 0; 2606 if ($1) 2607 $$.b1 = PF_NONAT; 2608 else 2609 $$.b1 = PF_NAT; 2610 $$.b2 = $3; 2611 } 2612 | no RDR natpass { 2613 $$.b2 = $$.w = 0; 2614 if ($1) 2615 $$.b1 = PF_NORDR; 2616 else 2617 $$.b1 = PF_RDR; 2618 $$.b2 = $3; 2619 } 2620 ; 2621 2622natrule : nataction interface af proto fromto tag redirpool pooltype 2623 staticport 2624 { 2625 struct pf_rule r; 2626 2627 if (check_rulestate(PFCTL_STATE_NAT)) 2628 YYERROR; 2629 2630 memset(&r, 0, sizeof(r)); 2631 2632 r.action = $1.b1; 2633 r.natpass = $1.b2; 2634 r.af = $3; 2635 2636 if (!r.af) { 2637 if ($5.src.host && $5.src.host->af && 2638 !$5.src.host->ifindex) 2639 r.af = $5.src.host->af; 2640 else if ($5.dst.host && $5.dst.host->af && 2641 !$5.dst.host->ifindex) 2642 r.af = $5.dst.host->af; 2643 } 2644 2645 if ($6 != NULL) 2646 if (strlcpy(r.tagname, $6, PF_TAG_NAME_SIZE) > 2647 PF_TAG_NAME_SIZE) { 2648 yyerror("tag too long, max %u chars", 2649 PF_TAG_NAME_SIZE - 1); 2650 YYERROR; 2651 } 2652 2653 if (r.action == PF_NONAT || r.action == PF_NORDR) { 2654 if ($7 != NULL) { 2655 yyerror("translation rule with 'no' " 2656 "does not need '->'"); 2657 YYERROR; 2658 } 2659 } else { 2660 if ($7 == NULL || $7->host == NULL) { 2661 yyerror("translation rule requires '-> " 2662 "address'"); 2663 YYERROR; 2664 } 2665 if (!r.af && ! $7->host->ifindex) 2666 r.af = $7->host->af; 2667 2668 remove_invalid_hosts(&$7->host, &r.af); 2669 if (invalid_redirect($7->host, r.af)) 2670 YYERROR; 2671 if (check_netmask($7->host, r.af)) 2672 YYERROR; 2673 2674 r.rpool.proxy_port[0] = ntohs($7->rport.a); 2675 2676 switch (r.action) { 2677 case PF_RDR: 2678 if (!$7->rport.b && $7->rport.t && 2679 $5.dst.port != NULL) { 2680 r.rpool.proxy_port[1] = 2681 ntohs($7->rport.a) + 2682 (ntohs($5.dst.port->port[1]) - 2683 ntohs($5.dst.port->port[0])); 2684 } else 2685 r.rpool.proxy_port[1] = 2686 ntohs($7->rport.b); 2687 break; 2688 case PF_NAT: 2689 r.rpool.proxy_port[1] = ntohs($7->rport.b); 2690 if (!r.rpool.proxy_port[0] && 2691 !r.rpool.proxy_port[1]) { 2692 r.rpool.proxy_port[0] = 2693 PF_NAT_PROXY_PORT_LOW; 2694 r.rpool.proxy_port[1] = 2695 PF_NAT_PROXY_PORT_HIGH; 2696 } else if (!r.rpool.proxy_port[1]) 2697 r.rpool.proxy_port[1] = 2698 r.rpool.proxy_port[0]; 2699 break; 2700 default: 2701 break; 2702 } 2703 2704 r.rpool.opts = $8.type; 2705 if (r.rpool.opts == PF_POOL_NONE) 2706 r.rpool.opts = PF_POOL_ROUNDROBIN; 2707 if (r.rpool.opts != PF_POOL_ROUNDROBIN) 2708 if (disallow_table($7->host, "tables " 2709 "are only supported in round-robin " 2710 "redirection pools")) 2711 YYERROR; 2712 if ($7->host->next) { 2713 if (r.rpool.opts != 2714 PF_POOL_ROUNDROBIN) { 2715 yyerror("only round-robin " 2716 "valid for multiple " 2717 "redirection addresses"); 2718 YYERROR; 2719 } 2720 } else { 2721 if ((r.af == AF_INET && 2722 unmask(&$7->host->addr.v.a.mask, 2723 r.af) == 32) || 2724 (r.af == AF_INET6 && 2725 unmask(&$7->host->addr.v.a.mask, 2726 r.af) == 128)) { 2727 r.rpool.opts = PF_POOL_NONE; 2728 } 2729 } 2730 } 2731 2732 if ($8.key != NULL) 2733 memcpy(&r.rpool.key, $8.key, 2734 sizeof(struct pf_poolhashkey)); 2735 2736 if ($9 != 0) { 2737 if (r.action != PF_NAT) { 2738 yyerror("the 'static-port' option is " 2739 "only valid with nat rules"); 2740 YYERROR; 2741 } 2742 if (r.rpool.proxy_port[0] != 2743 PF_NAT_PROXY_PORT_LOW && 2744 r.rpool.proxy_port[1] != 2745 PF_NAT_PROXY_PORT_HIGH) { 2746 yyerror("the 'static-port' option can't" 2747 " be used when specifying a port" 2748 " range"); 2749 YYERROR; 2750 } 2751 r.rpool.proxy_port[0] = 0; 2752 r.rpool.proxy_port[1] = 0; 2753 } 2754 2755 expand_rule(&r, $2, $7 == NULL ? NULL : $7->host, $4, 2756 $5.src_os, $5.src.host, $5.src.port, $5.dst.host, 2757 $5.dst.port, 0, 0, 0); 2758 free($7); 2759 } 2760 ; 2761 2762binatrule : no BINAT natpass interface af proto FROM host TO ipspec tag 2763 redirection 2764 { 2765 struct pf_rule binat; 2766 struct pf_pooladdr *pa; 2767 2768 if (check_rulestate(PFCTL_STATE_NAT)) 2769 YYERROR; 2770 2771 memset(&binat, 0, sizeof(binat)); 2772 2773 if ($1) 2774 binat.action = PF_NOBINAT; 2775 else 2776 binat.action = PF_BINAT; 2777 binat.natpass = $3; 2778 binat.af = $5; 2779 if (!binat.af && $8 != NULL && $8->af) 2780 binat.af = $8->af; 2781 if (!binat.af && $10 != NULL && $10->af) 2782 binat.af = $10->af; 2783 if (!binat.af && $12 != NULL && $12->host) 2784 binat.af = $12->host->af; 2785 if (!binat.af) { 2786 yyerror("address family (inet/inet6) " 2787 "undefined"); 2788 YYERROR; 2789 } 2790 2791 if ($4 != NULL) { 2792 memcpy(binat.ifname, $4->ifname, 2793 sizeof(binat.ifname)); 2794 free($4); 2795 } 2796 if ($11 != NULL) 2797 if (strlcpy(binat.tagname, $11, 2798 PF_TAG_NAME_SIZE) > PF_TAG_NAME_SIZE) { 2799 yyerror("tag too long, max %u chars", 2800 PF_TAG_NAME_SIZE - 1); 2801 YYERROR; 2802 } 2803 2804 if ($6 != NULL) { 2805 binat.proto = $6->proto; 2806 free($6); 2807 } 2808 2809 if ($8 != NULL && disallow_table($8, "invalid use of " 2810 "table <%s> as the source address of a binat rule")) 2811 YYERROR; 2812 if ($12 != NULL && $12->host != NULL && disallow_table( 2813 $12->host, "invalid use of table <%s> as the " 2814 "redirect address of a binat rule")) 2815 YYERROR; 2816 2817 if ($8 != NULL) { 2818 if ($8->next) { 2819 yyerror("multiple binat ip addresses"); 2820 YYERROR; 2821 } 2822 if ($8->addr.type == PF_ADDR_DYNIFTL) 2823 $8->af = binat.af; 2824 if ($8->af != binat.af) { 2825 yyerror("binat ip versions must match"); 2826 YYERROR; 2827 } 2828 if (check_netmask($8, binat.af)) 2829 YYERROR; 2830 memcpy(&binat.src.addr, &$8->addr, 2831 sizeof(binat.src.addr)); 2832 free($8); 2833 } 2834 if ($10 != NULL) { 2835 if ($10->next) { 2836 yyerror("multiple binat ip addresses"); 2837 YYERROR; 2838 } 2839 if ($10->af != binat.af && $10->af) { 2840 yyerror("binat ip versions must match"); 2841 YYERROR; 2842 } 2843 if (check_netmask($10, binat.af)) 2844 YYERROR; 2845 memcpy(&binat.dst.addr, &$10->addr, 2846 sizeof(binat.dst.addr)); 2847 binat.dst.not = $10->not; 2848 free($10); 2849 } 2850 2851 if (binat.action == PF_NOBINAT) { 2852 if ($12 != NULL) { 2853 yyerror("'no binat' rule does not need" 2854 " '->'"); 2855 YYERROR; 2856 } 2857 } else { 2858 if ($12 == NULL || $12->host == NULL) { 2859 yyerror("'binat' rule requires" 2860 " '-> address'"); 2861 YYERROR; 2862 } 2863 2864 remove_invalid_hosts(&$12->host, &binat.af); 2865 if (invalid_redirect($12->host, binat.af)) 2866 YYERROR; 2867 if ($12->host->next != NULL) { 2868 yyerror("binat rule must redirect to " 2869 "a single address"); 2870 YYERROR; 2871 } 2872 if (check_netmask($12->host, binat.af)) 2873 YYERROR; 2874 2875 if (!PF_AZERO(&binat.src.addr.v.a.mask, 2876 binat.af) && 2877 !PF_AEQ(&binat.src.addr.v.a.mask, 2878 &$12->host->addr.v.a.mask, binat.af)) { 2879 yyerror("'binat' source mask and " 2880 "redirect mask must be the same"); 2881 YYERROR; 2882 } 2883 2884 TAILQ_INIT(&binat.rpool.list); 2885 pa = calloc(1, sizeof(struct pf_pooladdr)); 2886 if (pa == NULL) 2887 err(1, "binat: calloc"); 2888 pa->addr = $12->host->addr; 2889 pa->ifname[0] = 0; 2890 TAILQ_INSERT_TAIL(&binat.rpool.list, 2891 pa, entries); 2892 2893 free($12); 2894 } 2895 2896 pfctl_add_rule(pf, &binat); 2897 } 2898 ; 2899 2900tag : /* empty */ { $$ = NULL; } 2901 | TAG STRING { $$ = $2; } 2902 2903route_host : STRING { 2904 struct node_host *n; 2905 2906 $$ = calloc(1, sizeof(struct node_host)); 2907 if ($$ == NULL) 2908 err(1, "route_host: calloc"); 2909 if (($$->ifname = strdup($1)) == NULL) 2910 err(1, "routeto: strdup"); 2911 if ((n = ifa_exists($$->ifname)) == NULL) { 2912 yyerror("routeto: unknown interface %s", 2913 $$->ifname); 2914 YYERROR; 2915 } 2916 set_ipmask($$, 128); 2917 $$->next = NULL; 2918 $$->tail = $$; 2919 } 2920 | '(' STRING host ')' { 2921 struct node_host *n; 2922 2923 $$ = $3; 2924 if (($$->ifname = strdup($2)) == NULL) 2925 err(1, "routeto: strdup"); 2926 if ((n = ifa_exists($$->ifname)) == NULL) { 2927 yyerror("routeto: unknown interface %s", 2928 $$->ifname); 2929 YYERROR; 2930 } 2931 } 2932 ; 2933 2934route_host_list : route_host { $$ = $1; } 2935 | route_host_list comma route_host { 2936 if ($1->af == 0) 2937 $1->af = $3->af; 2938 if ($1->af != $3->af) { 2939 yyerror("all pool addresses must be in the " 2940 "same address family"); 2941 YYERROR; 2942 } 2943 $1->tail->next = $3; 2944 $1->tail = $3->tail; 2945 $$ = $1; 2946 } 2947 ; 2948 2949routespec : route_host { $$ = $1; } 2950 | '{' route_host_list '}' { $$ = $2; } 2951 ; 2952 2953route : /* empty */ { 2954 $$.host = NULL; 2955 $$.rt = 0; 2956 $$.pool_opts = 0; 2957 } 2958 | FASTROUTE { 2959 $$.host = NULL; 2960 $$.rt = PF_FASTROUTE; 2961 $$.pool_opts = 0; 2962 } 2963 | ROUTETO routespec pooltype { 2964 $$.host = $2; 2965 $$.rt = PF_ROUTETO; 2966 $$.pool_opts = $3.type; 2967 if ($3.key != NULL) 2968 $$.key = $3.key; 2969 } 2970 | REPLYTO routespec pooltype { 2971 $$.host = $2; 2972 $$.rt = PF_REPLYTO; 2973 $$.pool_opts = $3.type; 2974 if ($3.key != NULL) 2975 $$.key = $3.key; 2976 } 2977 | DUPTO routespec pooltype { 2978 $$.host = $2; 2979 $$.rt = PF_DUPTO; 2980 $$.pool_opts = $3.type; 2981 if ($3.key != NULL) 2982 $$.key = $3.key; 2983 } 2984 ; 2985 2986timeout_spec : STRING number 2987 { 2988 if (check_rulestate(PFCTL_STATE_OPTION)) 2989 YYERROR; 2990 if (pfctl_set_timeout(pf, $1, $2, 0) != 0) { 2991 yyerror("unknown timeout %s", $1); 2992 YYERROR; 2993 } 2994 } 2995 ; 2996 2997timeout_list : timeout_list comma timeout_spec 2998 | timeout_spec 2999 ; 3000 3001limit_spec : STRING number 3002 { 3003 if (check_rulestate(PFCTL_STATE_OPTION)) 3004 YYERROR; 3005 if (pfctl_set_limit(pf, $1, $2) != 0) { 3006 yyerror("unable to set limit %s %u", $1, $2); 3007 YYERROR; 3008 } 3009 } 3010 3011limit_list : limit_list comma limit_spec 3012 | limit_spec 3013 ; 3014 3015comma : ',' 3016 | /* empty */ 3017 ; 3018 3019yesno : NO { $$ = 0; } 3020 | STRING { 3021 if (!strcmp($1, "yes")) 3022 $$ = 1; 3023 else 3024 YYERROR; 3025 } 3026 3027unaryop : '=' { $$ = PF_OP_EQ; } 3028 | '!' '=' { $$ = PF_OP_NE; } 3029 | '<' '=' { $$ = PF_OP_LE; } 3030 | '<' { $$ = PF_OP_LT; } 3031 | '>' '=' { $$ = PF_OP_GE; } 3032 | '>' { $$ = PF_OP_GT; } 3033 ; 3034 3035%% 3036 3037int 3038yyerror(const char *fmt, ...) 3039{ 3040 va_list ap; 3041 extern char *infile; 3042 3043 errors = 1; 3044 va_start(ap, fmt); 3045 fprintf(stderr, "%s:%d: ", infile, yylval.lineno); 3046 vfprintf(stderr, fmt, ap); 3047 fprintf(stderr, "\n"); 3048 va_end(ap); 3049 return (0); 3050} 3051 3052int 3053disallow_table(struct node_host *h, const char *fmt) 3054{ 3055 for (; h != NULL; h = h->next) 3056 if (h->addr.type == PF_ADDR_TABLE) { 3057 yyerror(fmt, h->addr.v.tblname); 3058 return (1); 3059 } 3060 return (0); 3061} 3062 3063int 3064rule_consistent(struct pf_rule *r) 3065{ 3066 int problems = 0; 3067 3068 switch (r->action) { 3069 case PF_PASS: 3070 case PF_DROP: 3071 case PF_SCRUB: 3072 problems = filter_consistent(r); 3073 break; 3074 case PF_NAT: 3075 case PF_NONAT: 3076 problems = nat_consistent(r); 3077 break; 3078 case PF_RDR: 3079 case PF_NORDR: 3080 problems = rdr_consistent(r); 3081 break; 3082 case PF_BINAT: 3083 case PF_NOBINAT: 3084 default: 3085 break; 3086 } 3087 return (problems); 3088} 3089 3090int 3091filter_consistent(struct pf_rule *r) 3092{ 3093 int problems = 0; 3094 3095 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP && 3096 (r->src.port_op || r->dst.port_op)) { 3097 yyerror("port only applies to tcp/udp"); 3098 problems++; 3099 } 3100 if (r->src.port_op == PF_OP_RRG || r->dst.port_op == PF_OP_RRG) { 3101 yyerror("the ':' port operator only applies to rdr"); 3102 problems++; 3103 } 3104 if (r->proto != IPPROTO_ICMP && r->proto != IPPROTO_ICMPV6 && 3105 (r->type || r->code)) { 3106 yyerror("icmp-type/code only applies to icmp"); 3107 problems++; 3108 } 3109 if (!r->af && (r->type || r->code)) { 3110 yyerror("must indicate address family with icmp-type/code"); 3111 problems++; 3112 } 3113 if ((r->proto == IPPROTO_ICMP && r->af == AF_INET6) || 3114 (r->proto == IPPROTO_ICMPV6 && r->af == AF_INET)) { 3115 yyerror("proto %s doesn't match address family %s", 3116 r->proto == IPPROTO_ICMP ? "icmp" : "icmp6", 3117 r->af == AF_INET ? "inet" : "inet6"); 3118 problems++; 3119 } 3120 if ((r->keep_state == PF_STATE_MODULATE || r->keep_state == 3121 PF_STATE_SYNPROXY) && r->proto && r->proto != IPPROTO_TCP) { 3122 yyerror("modulate/synproxy state can only be applied to " 3123 "TCP rules"); 3124 problems++; 3125 } 3126 if (r->allow_opts && r->action != PF_PASS) { 3127 yyerror("allow-opts can only be specified for pass rules"); 3128 problems++; 3129 } 3130 if (!r->af && (r->src.addr.type == PF_ADDR_DYNIFTL || 3131 r->dst.addr.type == PF_ADDR_DYNIFTL)) { 3132 yyerror("dynamic addresses require address family " 3133 "(inet/inet6)"); 3134 problems++; 3135 } 3136 if (r->rule_flag & PFRULE_FRAGMENT && (r->src.port_op || 3137 r->dst.port_op || r->flagset || r->type || r->code)) { 3138 yyerror("fragments can be filtered only on IP header fields"); 3139 problems++; 3140 } 3141 if (r->rule_flag & PFRULE_RETURNRST && r->proto != IPPROTO_TCP) { 3142 yyerror("return-rst can only be applied to TCP rules"); 3143 problems++; 3144 } 3145 if (r->action == PF_DROP && r->keep_state) { 3146 yyerror("keep state on block rules doesn't make sense"); 3147 problems++; 3148 } 3149 if ((r->tagname[0] || r->match_tagname[0]) && !r->keep_state && 3150 r->action == PF_PASS) { 3151 yyerror("tags cannot be used without keep state"); 3152 problems++; 3153 } 3154 return (-problems); 3155} 3156 3157int 3158nat_consistent(struct pf_rule *r) 3159{ 3160 int problems = 0; 3161 struct pf_pooladdr *pa; 3162 3163 if (r->src.port_op == PF_OP_RRG || r->dst.port_op == PF_OP_RRG) { 3164 yyerror("the ':' port operator only applies to rdr"); 3165 problems++; 3166 } 3167 if (!r->af) { 3168 TAILQ_FOREACH(pa, &r->rpool.list, entries) { 3169 if (pa->addr.type == PF_ADDR_DYNIFTL) { 3170 yyerror("dynamic addresses require " 3171 "address family (inet/inet6)"); 3172 problems++; 3173 break; 3174 } 3175 } 3176 } 3177 return (-problems); 3178} 3179 3180int 3181rdr_consistent(struct pf_rule *r) 3182{ 3183 int problems = 0; 3184 struct pf_pooladdr *pa; 3185 3186 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP) { 3187 if (r->src.port_op) { 3188 yyerror("src port only applies to tcp/udp"); 3189 problems++; 3190 } 3191 if (r->dst.port_op) { 3192 yyerror("dst port only applies to tcp/udp"); 3193 problems++; 3194 } 3195 if (r->rpool.proxy_port[0]) { 3196 yyerror("rpool port only applies to tcp/udp"); 3197 problems++; 3198 } 3199 } 3200 if (r->dst.port_op && 3201 r->dst.port_op != PF_OP_EQ && r->dst.port_op != PF_OP_RRG) { 3202 yyerror("invalid port operator for rdr destination port"); 3203 problems++; 3204 } 3205 if (r->src.port_op == PF_OP_RRG) { 3206 yyerror("the ':' port operator only applies to rdr " 3207 "destination port"); 3208 problems++; 3209 } 3210 if (!r->af) { 3211 if (r->src.addr.type == PF_ADDR_DYNIFTL || 3212 r->dst.addr.type == PF_ADDR_DYNIFTL) { 3213 yyerror("dynamic addresses require address family " 3214 "(inet/inet6)"); 3215 problems++; 3216 } else { 3217 TAILQ_FOREACH(pa, &r->rpool.list, entries) { 3218 if (pa->addr.type == PF_ADDR_DYNIFTL) { 3219 yyerror("dynamic addresses require " 3220 "address family (inet/inet6)"); 3221 problems++; 3222 break; 3223 } 3224 } 3225 } 3226 } 3227 return (-problems); 3228} 3229 3230int 3231process_tabledef(char *name, struct table_opts *opts) 3232{ 3233 struct pfr_buffer ab; 3234 struct node_tinit *ti; 3235 3236 bzero(&ab, sizeof(ab)); 3237 ab.pfrb_type = PFRB_ADDRS; 3238 SIMPLEQ_FOREACH(ti, &opts->init_nodes, entries) { 3239 if (ti->file) 3240 if (pfr_buf_load(&ab, ti->file, 0, append_addr)) { 3241 if (errno) 3242 yyerror("cannot load \"%s\": %s", 3243 ti->file, strerror(errno)); 3244 else 3245 yyerror("file \"%s\" contains bad data", 3246 ti->file); 3247 goto _error; 3248 } 3249 if (ti->host) 3250 if (append_addr_host(&ab, ti->host, 0, 0)) { 3251 yyerror("cannot create address buffer: %s", 3252 strerror(errno)); 3253 goto _error; 3254 } 3255 } 3256 if (pf->opts & PF_OPT_VERBOSE) 3257 print_tabledef(name, opts->flags, opts->init_addr, 3258 &opts->init_nodes); 3259 if (!(pf->opts & PF_OPT_NOACTION) && 3260 pfctl_define_table(name, opts->flags, opts->init_addr, 3261 pf->anchor, pf->ruleset, &ab, pf->tticket)) { 3262 yyerror("cannot define table %s: %s", name, 3263 pfr_strerror(errno)); 3264 goto _error; 3265 } 3266 pf->tdirty = 1; 3267 pfr_buf_clear(&ab); 3268 return (0); 3269_error: 3270 pfr_buf_clear(&ab); 3271 return (-1); 3272} 3273 3274struct keywords { 3275 const char *k_name; 3276 int k_val; 3277}; 3278 3279/* macro gore, but you should've seen the prior indentation nightmare... */ 3280 3281#define FREE_LIST(T,r) \ 3282 do { \ 3283 T *p, *node = r; \ 3284 while (node != NULL) { \ 3285 p = node; \ 3286 node = node->next; \ 3287 free(p); \ 3288 } \ 3289 } while (0) 3290 3291#define LOOP_THROUGH(T,n,r,C) \ 3292 do { \ 3293 T *n; \ 3294 if (r == NULL) { \ 3295 r = calloc(1, sizeof(T)); \ 3296 if (r == NULL) \ 3297 err(1, "LOOP: calloc"); \ 3298 r->next = NULL; \ 3299 } \ 3300 n = r; \ 3301 while (n != NULL) { \ 3302 do { \ 3303 C; \ 3304 } while (0); \ 3305 n = n->next; \ 3306 } \ 3307 } while (0) 3308 3309void 3310expand_label_str(char *label, size_t len, const char *srch, const char *repl) 3311{ 3312 char *tmp; 3313 char *p, *q; 3314 3315 if ((tmp = calloc(1, len)) == NULL) 3316 err(1, "expand_label_str: calloc"); 3317 p = q = label; 3318 while ((q = strstr(p, srch)) != NULL) { 3319 *q = '\0'; 3320 if ((strlcat(tmp, p, len) >= len) || 3321 (strlcat(tmp, repl, len) >= len)) 3322 errx(1, "expand_label: label too long"); 3323 q += strlen(srch); 3324 p = q; 3325 } 3326 if (strlcat(tmp, p, len) >= len) 3327 errx(1, "expand_label: label too long"); 3328 strlcpy(label, tmp, len); /* always fits */ 3329 free(tmp); 3330} 3331 3332void 3333expand_label_if(const char *name, char *label, size_t len, const char *ifname) 3334{ 3335 if (strstr(label, name) != NULL) { 3336 if (!*ifname) 3337 expand_label_str(label, len, name, "any"); 3338 else 3339 expand_label_str(label, len, name, ifname); 3340 } 3341} 3342 3343void 3344expand_label_addr(const char *name, char *label, size_t len, sa_family_t af, 3345 struct node_host *h) 3346{ 3347 char tmp[64], tmp_not[66]; 3348 3349 if (strstr(label, name) != NULL) { 3350 switch (h->addr.type) { 3351 case PF_ADDR_DYNIFTL: 3352 snprintf(tmp, sizeof(tmp), "(%s)", h->addr.v.ifname); 3353 break; 3354 case PF_ADDR_TABLE: 3355 snprintf(tmp, sizeof(tmp), "<%s>", h->addr.v.tblname); 3356 break; 3357 case PF_ADDR_NOROUTE: 3358 snprintf(tmp, sizeof(tmp), "no-route"); 3359 break; 3360 case PF_ADDR_ADDRMASK: 3361 if (!af || (PF_AZERO(&h->addr.v.a.addr, af) && 3362 PF_AZERO(&h->addr.v.a.mask, af))) 3363 snprintf(tmp, sizeof(tmp), "any"); 3364 else { 3365 char a[48]; 3366 int bits; 3367 3368 if (inet_ntop(af, &h->addr.v.a.addr, a, 3369 sizeof(a)) == NULL) 3370 snprintf(tmp, sizeof(tmp), "?"); 3371 else { 3372 bits = unmask(&h->addr.v.a.mask, af); 3373 if ((af == AF_INET && bits < 32) || 3374 (af == AF_INET6 && bits < 128)) 3375 snprintf(tmp, sizeof(tmp), 3376 "%s/%d", a, bits); 3377 else 3378 snprintf(tmp, sizeof(tmp), 3379 "%s", a); 3380 } 3381 } 3382 break; 3383 default: 3384 snprintf(tmp, sizeof(tmp), "?"); 3385 break; 3386 } 3387 3388 if (h->not) { 3389 snprintf(tmp_not, sizeof(tmp_not), "! %s", tmp); 3390 expand_label_str(label, len, name, tmp_not); 3391 } else 3392 expand_label_str(label, len, name, tmp); 3393 } 3394} 3395 3396void 3397expand_label_port(const char *name, char *label, size_t len, 3398 struct node_port *port) 3399{ 3400 char a1[6], a2[6], op[13] = ""; 3401 3402 if (strstr(label, name) != NULL) { 3403 snprintf(a1, sizeof(a1), "%u", ntohs(port->port[0])); 3404 snprintf(a2, sizeof(a2), "%u", ntohs(port->port[1])); 3405 if (!port->op) 3406 ; 3407 else if (port->op == PF_OP_IRG) 3408 snprintf(op, sizeof(op), "%s><%s", a1, a2); 3409 else if (port->op == PF_OP_XRG) 3410 snprintf(op, sizeof(op), "%s<>%s", a1, a2); 3411 else if (port->op == PF_OP_EQ) 3412 snprintf(op, sizeof(op), "%s", a1); 3413 else if (port->op == PF_OP_NE) 3414 snprintf(op, sizeof(op), "!=%s", a1); 3415 else if (port->op == PF_OP_LT) 3416 snprintf(op, sizeof(op), "<%s", a1); 3417 else if (port->op == PF_OP_LE) 3418 snprintf(op, sizeof(op), "<=%s", a1); 3419 else if (port->op == PF_OP_GT) 3420 snprintf(op, sizeof(op), ">%s", a1); 3421 else if (port->op == PF_OP_GE) 3422 snprintf(op, sizeof(op), ">=%s", a1); 3423 expand_label_str(label, len, name, op); 3424 } 3425} 3426 3427void 3428expand_label_proto(const char *name, char *label, size_t len, u_int8_t proto) 3429{ 3430 struct protoent *pe; 3431 char n[4]; 3432 3433 if (strstr(label, name) != NULL) { 3434 pe = getprotobynumber(proto); 3435 if (pe != NULL) 3436 expand_label_str(label, len, name, pe->p_name); 3437 else { 3438 snprintf(n, sizeof(n), "%u", proto); 3439 expand_label_str(label, len, name, n); 3440 } 3441 } 3442} 3443 3444void 3445expand_label_nr(const char *name, char *label, size_t len) 3446{ 3447 char n[11]; 3448 3449 if (strstr(label, name) != NULL) { 3450 snprintf(n, sizeof(n), "%u", pf->rule_nr); 3451 expand_label_str(label, len, name, n); 3452 } 3453} 3454 3455void 3456expand_label(char *label, size_t len, const char *ifname, sa_family_t af, 3457 struct node_host *src_host, struct node_port *src_port, 3458 struct node_host *dst_host, struct node_port *dst_port, 3459 u_int8_t proto) 3460{ 3461 expand_label_if("$if", label, len, ifname); 3462 expand_label_addr("$srcaddr", label, len, af, src_host); 3463 expand_label_addr("$dstaddr", label, len, af, dst_host); 3464 expand_label_port("$srcport", label, len, src_port); 3465 expand_label_port("$dstport", label, len, dst_port); 3466 expand_label_proto("$proto", label, len, proto); 3467 expand_label_nr("$nr", label, len); 3468} 3469 3470int 3471expand_altq(struct pf_altq *a, struct node_if *interfaces, 3472 struct node_queue *nqueues, struct node_queue_bw bwspec, 3473 struct node_queue_opt *opts) 3474{ 3475 struct pf_altq pa, pb; 3476 char qname[PF_QNAME_SIZE]; 3477 struct node_queue *n; 3478 struct node_queue_bw bw; 3479 int errs = 0; 3480 3481 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) { 3482 FREE_LIST(struct node_if, interfaces); 3483 FREE_LIST(struct node_queue, nqueues); 3484 return (0); 3485 } 3486 3487 LOOP_THROUGH(struct node_if, interface, interfaces, 3488 memcpy(&pa, a, sizeof(struct pf_altq)); 3489 if (strlcpy(pa.ifname, interface->ifname, 3490 sizeof(pa.ifname)) >= sizeof(pa.ifname)) 3491 errx(1, "expand_altq: strlcpy"); 3492 3493 if (interface->not) { 3494 yyerror("altq on ! <interface> is not supported"); 3495 errs++; 3496 } else { 3497 if (eval_pfaltq(pf, &pa, &bwspec, opts)) 3498 errs++; 3499 else 3500 if (pfctl_add_altq(pf, &pa)) 3501 errs++; 3502 3503 if (pf->opts & PF_OPT_VERBOSE) { 3504 print_altq(&pf->paltq->altq, 0, 3505 &bwspec, opts); 3506 if (nqueues && nqueues->tail) { 3507 printf("queue { "); 3508 LOOP_THROUGH(struct node_queue, queue, 3509 nqueues, 3510 printf("%s ", 3511 queue->queue); 3512 ); 3513 printf("}"); 3514 } 3515 printf("\n"); 3516 } 3517 3518 if (pa.scheduler == ALTQT_CBQ || 3519 pa.scheduler == ALTQT_HFSC) { 3520 /* now create a root queue */ 3521 memset(&pb, 0, sizeof(struct pf_altq)); 3522 if (strlcpy(qname, "root_", sizeof(qname)) >= 3523 sizeof(qname)) 3524 errx(1, "expand_altq: strlcpy"); 3525 if (strlcat(qname, interface->ifname, 3526 sizeof(qname)) >= sizeof(qname)) 3527 errx(1, "expand_altq: strlcat"); 3528 if (strlcpy(pb.qname, qname, 3529 sizeof(pb.qname)) >= sizeof(pb.qname)) 3530 errx(1, "expand_altq: strlcpy"); 3531 if (strlcpy(pb.ifname, interface->ifname, 3532 sizeof(pb.ifname)) >= sizeof(pb.ifname)) 3533 errx(1, "expand_altq: strlcpy"); 3534 pb.qlimit = pa.qlimit; 3535 pb.scheduler = pa.scheduler; 3536 bw.bw_absolute = pa.ifbandwidth; 3537 bw.bw_percent = 0; 3538 if (eval_pfqueue(pf, &pb, &bw, opts)) 3539 errs++; 3540 else 3541 if (pfctl_add_altq(pf, &pb)) 3542 errs++; 3543 } 3544 3545 LOOP_THROUGH(struct node_queue, queue, nqueues, 3546 n = calloc(1, sizeof(struct node_queue)); 3547 if (n == NULL) 3548 err(1, "expand_altq: calloc"); 3549 if (pa.scheduler == ALTQT_CBQ || 3550 pa.scheduler == ALTQT_HFSC) 3551 if (strlcpy(n->parent, qname, 3552 sizeof(n->parent)) >= 3553 sizeof(n->parent)) 3554 errx(1, "expand_altq: strlcpy"); 3555 if (strlcpy(n->queue, queue->queue, 3556 sizeof(n->queue)) >= sizeof(n->queue)) 3557 errx(1, "expand_altq: strlcpy"); 3558 if (strlcpy(n->ifname, interface->ifname, 3559 sizeof(n->ifname)) >= sizeof(n->ifname)) 3560 errx(1, "expand_altq: strlcpy"); 3561 n->scheduler = pa.scheduler; 3562 n->next = NULL; 3563 n->tail = n; 3564 if (queues == NULL) 3565 queues = n; 3566 else { 3567 queues->tail->next = n; 3568 queues->tail = n; 3569 } 3570 ); 3571 } 3572 ); 3573 FREE_LIST(struct node_if, interfaces); 3574 FREE_LIST(struct node_queue, nqueues); 3575 3576 return (errs); 3577} 3578 3579int 3580expand_queue(struct pf_altq *a, struct node_if *interfaces, 3581 struct node_queue *nqueues, struct node_queue_bw bwspec, 3582 struct node_queue_opt *opts) 3583{ 3584 struct node_queue *n, *nq; 3585 struct pf_altq pa; 3586 u_int8_t found = 0; 3587 u_int8_t errs = 0; 3588 3589 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) { 3590 FREE_LIST(struct node_queue, nqueues); 3591 return (0); 3592 } 3593 3594 if (queues == NULL) { 3595 yyerror("queue %s has no parent", a->qname); 3596 FREE_LIST(struct node_queue, nqueues); 3597 return (1); 3598 } 3599 3600 LOOP_THROUGH(struct node_if, interface, interfaces, 3601 LOOP_THROUGH(struct node_queue, tqueue, queues, 3602 if (!strncmp(a->qname, tqueue->queue, PF_QNAME_SIZE) && 3603 (interface->ifname[0] == 0 || 3604 (!interface->not && !strncmp(interface->ifname, 3605 tqueue->ifname, IFNAMSIZ)) || 3606 (interface->not && strncmp(interface->ifname, 3607 tqueue->ifname, IFNAMSIZ)))) { 3608 /* found ourself in queues */ 3609 found++; 3610 3611 memcpy(&pa, a, sizeof(struct pf_altq)); 3612 3613 if (pa.scheduler != ALTQT_NONE && 3614 pa.scheduler != tqueue->scheduler) { 3615 yyerror("exactly one scheduler type " 3616 "per interface allowed"); 3617 return (1); 3618 } 3619 pa.scheduler = tqueue->scheduler; 3620 3621 /* scheduler dependent error checking */ 3622 switch (pa.scheduler) { 3623 case ALTQT_PRIQ: 3624 if (nqueues != NULL) { 3625 yyerror("priq queues cannot " 3626 "have child queues"); 3627 return (1); 3628 } 3629 if (bwspec.bw_absolute > 0 || 3630 bwspec.bw_percent < 100) { 3631 yyerror("priq doesn't take " 3632 "bandwidth"); 3633 return (1); 3634 } 3635 break; 3636 default: 3637 break; 3638 } 3639 3640 if (strlcpy(pa.ifname, tqueue->ifname, 3641 sizeof(pa.ifname)) >= sizeof(pa.ifname)) 3642 errx(1, "expand_queue: strlcpy"); 3643 if (strlcpy(pa.parent, tqueue->parent, 3644 sizeof(pa.parent)) >= sizeof(pa.parent)) 3645 errx(1, "expand_queue: strlcpy"); 3646 3647 if (eval_pfqueue(pf, &pa, &bwspec, opts)) 3648 errs++; 3649 else 3650 if (pfctl_add_altq(pf, &pa)) 3651 errs++; 3652 3653 for (nq = nqueues; nq != NULL; nq = nq->next) { 3654 if (!strcmp(a->qname, nq->queue)) { 3655 yyerror("queue cannot have " 3656 "itself as child"); 3657 errs++; 3658 continue; 3659 } 3660 n = calloc(1, 3661 sizeof(struct node_queue)); 3662 if (n == NULL) 3663 err(1, "expand_queue: calloc"); 3664 if (strlcpy(n->parent, a->qname, 3665 sizeof(n->parent)) >= 3666 sizeof(n->parent)) 3667 errx(1, "expand_queue strlcpy"); 3668 if (strlcpy(n->queue, nq->queue, 3669 sizeof(n->queue)) >= 3670 sizeof(n->queue)) 3671 errx(1, "expand_queue strlcpy"); 3672 if (strlcpy(n->ifname, tqueue->ifname, 3673 sizeof(n->ifname)) >= 3674 sizeof(n->ifname)) 3675 errx(1, "expand_queue strlcpy"); 3676 n->scheduler = tqueue->scheduler; 3677 n->next = NULL; 3678 n->tail = n; 3679 if (queues == NULL) 3680 queues = n; 3681 else { 3682 queues->tail->next = n; 3683 queues->tail = n; 3684 } 3685 } 3686 if ((pf->opts & PF_OPT_VERBOSE) && ( 3687 (found == 1 && interface->ifname[0] == 0) || 3688 (found > 0 && interface->ifname[0] != 0))) { 3689 print_queue(&pf->paltq->altq, 0, 3690 &bwspec, interface->ifname[0] != 0, 3691 opts); 3692 if (nqueues && nqueues->tail) { 3693 printf("{ "); 3694 LOOP_THROUGH(struct node_queue, 3695 queue, nqueues, 3696 printf("%s ", 3697 queue->queue); 3698 ); 3699 printf("}"); 3700 } 3701 printf("\n"); 3702 } 3703 } 3704 ); 3705 ); 3706 3707 FREE_LIST(struct node_queue, nqueues); 3708 FREE_LIST(struct node_if, interfaces); 3709 3710 if (!found) { 3711 yyerror("queue %s has no parent", a->qname); 3712 errs++; 3713 } 3714 3715 if (errs) 3716 return (1); 3717 else 3718 return (0); 3719} 3720 3721void 3722expand_rule(struct pf_rule *r, 3723 struct node_if *interfaces, struct node_host *rpool_hosts, 3724 struct node_proto *protos, struct node_os *src_oses, 3725 struct node_host *src_hosts, struct node_port *src_ports, 3726 struct node_host *dst_hosts, struct node_port *dst_ports, 3727 struct node_uid *uids, struct node_gid *gids, struct node_icmp *icmp_types) 3728{ 3729 sa_family_t af = r->af; 3730 int added = 0, error = 0; 3731 char ifname[IF_NAMESIZE]; 3732 char label[PF_RULE_LABEL_SIZE]; 3733 struct pf_pooladdr *pa; 3734 struct node_host *h; 3735 u_int8_t flags, flagset; 3736 3737 if (strlcpy(label, r->label, sizeof(label)) >= sizeof(label)) 3738 errx(1, "expand_rule: strlcpy"); 3739 flags = r->flags; 3740 flagset = r->flagset; 3741 3742 LOOP_THROUGH(struct node_if, interface, interfaces, 3743 LOOP_THROUGH(struct node_proto, proto, protos, 3744 LOOP_THROUGH(struct node_icmp, icmp_type, icmp_types, 3745 LOOP_THROUGH(struct node_host, src_host, src_hosts, 3746 LOOP_THROUGH(struct node_port, src_port, src_ports, 3747 LOOP_THROUGH(struct node_os, src_os, src_oses, 3748 LOOP_THROUGH(struct node_host, dst_host, dst_hosts, 3749 LOOP_THROUGH(struct node_port, dst_port, dst_ports, 3750 LOOP_THROUGH(struct node_uid, uid, uids, 3751 LOOP_THROUGH(struct node_gid, gid, gids, 3752 3753 r->af = af; 3754 /* for link-local IPv6 address, interface must match up */ 3755 if ((r->af && src_host->af && r->af != src_host->af) || 3756 (r->af && dst_host->af && r->af != dst_host->af) || 3757 (src_host->af && dst_host->af && 3758 src_host->af != dst_host->af) || 3759 (src_host->ifindex && dst_host->ifindex && 3760 src_host->ifindex != dst_host->ifindex) || 3761 (src_host->ifindex && if_nametoindex(interface->ifname) && 3762 src_host->ifindex != if_nametoindex(interface->ifname)) || 3763 (dst_host->ifindex && if_nametoindex(interface->ifname) && 3764 dst_host->ifindex != if_nametoindex(interface->ifname))) 3765 continue; 3766 if (!r->af && src_host->af) 3767 r->af = src_host->af; 3768 else if (!r->af && dst_host->af) 3769 r->af = dst_host->af; 3770 3771 if (if_indextoname(src_host->ifindex, ifname)) 3772 memcpy(r->ifname, ifname, sizeof(r->ifname)); 3773 else if (if_indextoname(dst_host->ifindex, ifname)) 3774 memcpy(r->ifname, ifname, sizeof(r->ifname)); 3775 else 3776 memcpy(r->ifname, interface->ifname, sizeof(r->ifname)); 3777 3778 if (strlcpy(r->label, label, sizeof(r->label)) >= 3779 sizeof(r->label)) 3780 errx(1, "expand_rule: strlcpy"); 3781 expand_label(r->label, PF_RULE_LABEL_SIZE, r->ifname, r->af, 3782 src_host, src_port, dst_host, dst_port, proto->proto); 3783 expand_label(r->tagname, PF_TAG_NAME_SIZE, r->ifname, r->af, 3784 src_host, src_port, dst_host, dst_port, proto->proto); 3785 expand_label(r->match_tagname, PF_TAG_NAME_SIZE, r->ifname, 3786 r->af, src_host, src_port, dst_host, dst_port, 3787 proto->proto); 3788 3789 error += check_netmask(src_host, r->af); 3790 error += check_netmask(dst_host, r->af); 3791 3792 r->ifnot = interface->not; 3793 r->proto = proto->proto; 3794 r->src.addr = src_host->addr; 3795 r->src.not = src_host->not; 3796 r->src.port[0] = src_port->port[0]; 3797 r->src.port[1] = src_port->port[1]; 3798 r->src.port_op = src_port->op; 3799 r->dst.addr = dst_host->addr; 3800 r->dst.not = dst_host->not; 3801 r->dst.port[0] = dst_port->port[0]; 3802 r->dst.port[1] = dst_port->port[1]; 3803 r->dst.port_op = dst_port->op; 3804 r->uid.op = uid->op; 3805 r->uid.uid[0] = uid->uid[0]; 3806 r->uid.uid[1] = uid->uid[1]; 3807 r->gid.op = gid->op; 3808 r->gid.gid[0] = gid->gid[0]; 3809 r->gid.gid[1] = gid->gid[1]; 3810 r->type = icmp_type->type; 3811 r->code = icmp_type->code; 3812 3813 if (r->proto && r->proto != IPPROTO_TCP) { 3814 r->flags = 0; 3815 r->flagset = 0; 3816 } else { 3817 r->flags = flags; 3818 r->flagset = flagset; 3819 } 3820 if (icmp_type->proto && r->proto != icmp_type->proto) { 3821 yyerror("icmp-type mismatch"); 3822 error++; 3823 } 3824 3825 if (src_os && src_os->os) { 3826 r->os_fingerprint = pfctl_get_fingerprint(src_os->os); 3827 if ((pf->opts & PF_OPT_VERBOSE2) && 3828 r->os_fingerprint == PF_OSFP_NOMATCH) 3829 fprintf(stderr, 3830 "warning: unknown '%s' OS fingerprint\n", 3831 src_os->os); 3832 } else { 3833 r->os_fingerprint = PF_OSFP_ANY; 3834 } 3835 3836 TAILQ_INIT(&r->rpool.list); 3837 for (h = rpool_hosts; h != NULL; h = h->next) { 3838 pa = calloc(1, sizeof(struct pf_pooladdr)); 3839 if (pa == NULL) 3840 err(1, "expand_rule: calloc"); 3841 pa->addr = h->addr; 3842 if (h->ifname != NULL) { 3843 if (strlcpy(pa->ifname, h->ifname, 3844 sizeof(pa->ifname)) >= 3845 sizeof(pa->ifname)) 3846 errx(1, "expand_rule: strlcpy"); 3847 } else 3848 pa->ifname[0] = 0; 3849 TAILQ_INSERT_TAIL(&r->rpool.list, pa, entries); 3850 } 3851 3852 if (rule_consistent(r) < 0 || error) 3853 yyerror("skipping rule due to errors"); 3854 else { 3855 r->nr = pf->rule_nr++; 3856 pfctl_add_rule(pf, r); 3857 added++; 3858 } 3859 3860 )))))))))); 3861 3862 FREE_LIST(struct node_if, interfaces); 3863 FREE_LIST(struct node_proto, protos); 3864 FREE_LIST(struct node_host, src_hosts); 3865 FREE_LIST(struct node_port, src_ports); 3866 FREE_LIST(struct node_os, src_oses); 3867 FREE_LIST(struct node_host, dst_hosts); 3868 FREE_LIST(struct node_port, dst_ports); 3869 FREE_LIST(struct node_uid, uids); 3870 FREE_LIST(struct node_gid, gids); 3871 FREE_LIST(struct node_icmp, icmp_types); 3872 FREE_LIST(struct node_host, rpool_hosts); 3873 3874 if (!added) 3875 yyerror("rule expands to no valid combination"); 3876} 3877 3878#undef FREE_LIST 3879#undef LOOP_THROUGH 3880 3881int 3882check_rulestate(int desired_state) 3883{ 3884 if (require_order && (rulestate > desired_state)) { 3885 yyerror("Rules must be in order: options, normalization, " 3886 "queueing, translation, filtering"); 3887 return (1); 3888 } 3889 rulestate = desired_state; 3890 return (0); 3891} 3892 3893int 3894kw_cmp(const void *k, const void *e) 3895{ 3896 return (strcmp(k, ((const struct keywords *)e)->k_name)); 3897} 3898 3899int 3900lookup(char *s) 3901{ 3902 /* this has to be sorted always */ 3903 static const struct keywords keywords[] = { 3904 { "all", ALL}, 3905 { "allow-opts", ALLOWOPTS}, 3906 { "altq", ALTQ}, 3907 { "anchor", ANCHOR}, 3908 { "antispoof", ANTISPOOF}, 3909 { "any", ANY}, 3910 { "bandwidth", BANDWIDTH}, 3911 { "binat", BINAT}, 3912 { "binat-anchor", BINATANCHOR}, 3913 { "bitmask", BITMASK}, 3914 { "block", BLOCK}, 3915 { "block-policy", BLOCKPOLICY}, 3916 { "cbq", CBQ}, 3917 { "code", CODE}, 3918 { "crop", FRAGCROP}, 3919 { "drop", DROP}, 3920 { "drop-ovl", FRAGDROP}, 3921 { "dup-to", DUPTO}, 3922 { "fastroute", FASTROUTE}, 3923 { "file", FILENAME}, 3924 { "fingerprints", FINGERPRINTS}, 3925 { "flags", FLAGS}, 3926 { "for", FOR}, 3927 { "fragment", FRAGMENT}, 3928 { "from", FROM}, 3929 { "group", GROUP}, 3930 { "hfsc", HFSC}, 3931 { "icmp-type", ICMPTYPE}, 3932 { "icmp6-type", ICMP6TYPE}, 3933 { "in", IN}, 3934 { "inet", INET}, 3935 { "inet6", INET6}, 3936 { "keep", KEEP}, 3937 { "label", LABEL}, 3938 { "limit", LIMIT}, 3939 { "linkshare", LINKSHARE}, 3940 { "load", LOAD}, 3941 { "log", LOG}, 3942 { "log-all", LOGALL}, 3943 { "loginterface", LOGINTERFACE}, 3944 { "max", MAXIMUM}, 3945 { "max-mss", MAXMSS}, 3946 { "min-ttl", MINTTL}, 3947 { "modulate", MODULATE}, 3948 { "nat", NAT}, 3949 { "nat-anchor", NATANCHOR}, 3950 { "no", NO}, 3951 { "no-df", NODF}, 3952 { "no-route", NOROUTE}, 3953 { "on", ON}, 3954 { "optimization", OPTIMIZATION}, 3955 { "os", OS}, 3956 { "out", OUT}, 3957 { "pass", PASS}, 3958 { "port", PORT}, 3959 { "priority", PRIORITY}, 3960 { "priq", PRIQ}, 3961 { "proto", PROTO}, 3962 { "qlimit", QLIMIT}, 3963 { "queue", QUEUE}, 3964 { "quick", QUICK}, 3965 { "random", RANDOM}, 3966 { "random-id", RANDOMID}, 3967 { "rdr", RDR}, 3968 { "rdr-anchor", RDRANCHOR}, 3969 { "realtime", REALTIME}, 3970 { "reassemble", REASSEMBLE}, 3971 { "reply-to", REPLYTO}, 3972 { "require-order", REQUIREORDER}, 3973 { "return", RETURN}, 3974 { "return-icmp", RETURNICMP}, 3975 { "return-icmp6", RETURNICMP6}, 3976 { "return-rst", RETURNRST}, 3977 { "round-robin", ROUNDROBIN}, 3978 { "route-to", ROUTETO}, 3979 { "scrub", SCRUB}, 3980 { "set", SET}, 3981 { "source-hash", SOURCEHASH}, 3982 { "state", STATE}, 3983 { "static-port", STATICPORT}, 3984 { "synproxy", SYNPROXY}, 3985 { "table", TABLE}, 3986 { "tag", TAG}, 3987 { "tagged", TAGGED}, 3988 { "tbrsize", TBRSIZE}, 3989 { "timeout", TIMEOUT}, 3990 { "to", TO}, 3991 { "tos", TOS}, 3992 { "ttl", TTL}, 3993 { "upperlimit", UPPERLIMIT}, 3994 { "user", USER}, 3995 }; 3996 const struct keywords *p; 3997 3998 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]), 3999 sizeof(keywords[0]), kw_cmp); 4000 4001 if (p) { 4002 if (debug > 1) 4003 fprintf(stderr, "%s: %d\n", s, p->k_val); 4004 return (p->k_val); 4005 } else { 4006 if (debug > 1) 4007 fprintf(stderr, "string: %s\n", s); 4008 return (STRING); 4009 } 4010} 4011 4012#define MAXPUSHBACK 128 4013 4014char *parsebuf; 4015int parseindex; 4016char pushback_buffer[MAXPUSHBACK]; 4017int pushback_index = 0; 4018 4019int 4020lgetc(FILE *f) 4021{ 4022 int c, next; 4023 4024 if (parsebuf) { 4025 /* Read character from the parsebuffer instead of input. */ 4026 if (parseindex >= 0) { 4027 c = parsebuf[parseindex++]; 4028 if (c != '\0') 4029 return (c); 4030 parsebuf = NULL; 4031 } else 4032 parseindex++; 4033 } 4034 4035 if (pushback_index) 4036 return (pushback_buffer[--pushback_index]); 4037 4038 while ((c = getc(f)) == '\\') { 4039 next = getc(f); 4040 if (next != '\n') { 4041 if (isspace(next)) 4042 yyerror("whitespace after \\"); 4043 ungetc(next, f); 4044 break; 4045 } 4046 yylval.lineno = lineno; 4047 lineno++; 4048 } 4049 if (c == '\t' || c == ' ') { 4050 /* Compress blanks to a single space. */ 4051 do { 4052 c = getc(f); 4053 } while (c == '\t' || c == ' '); 4054 ungetc(c, f); 4055 c = ' '; 4056 } 4057 4058 return (c); 4059} 4060 4061int 4062lungetc(int c) 4063{ 4064 if (c == EOF) 4065 return (EOF); 4066 if (parsebuf) { 4067 parseindex--; 4068 if (parseindex >= 0) 4069 return (c); 4070 } 4071 if (pushback_index < MAXPUSHBACK-1) 4072 return (pushback_buffer[pushback_index++] = c); 4073 else 4074 return (EOF); 4075} 4076 4077int 4078findeol(void) 4079{ 4080 int c; 4081 4082 parsebuf = NULL; 4083 pushback_index = 0; 4084 4085 /* skip to either EOF or the first real EOL */ 4086 while (1) { 4087 c = lgetc(fin); 4088 if (c == '\n') { 4089 lineno++; 4090 break; 4091 } 4092 if (c == EOF) 4093 break; 4094 } 4095 return (ERROR); 4096} 4097 4098int 4099yylex(void) 4100{ 4101 char buf[8096]; 4102 char *p, *val; 4103 int endc, c, next; 4104 int token; 4105 4106top: 4107 p = buf; 4108 while ((c = lgetc(fin)) == ' ') 4109 ; /* nothing */ 4110 4111 yylval.lineno = lineno; 4112 if (c == '#') 4113 while ((c = lgetc(fin)) != '\n' && c != EOF) 4114 ; /* nothing */ 4115 if (c == '$' && parsebuf == NULL) { 4116 while (1) { 4117 if ((c = lgetc(fin)) == EOF) 4118 return (0); 4119 4120 if (p + 1 >= buf + sizeof(buf) - 1) { 4121 yyerror("string too long"); 4122 return (findeol()); 4123 } 4124 if (isalnum(c) || c == '_') { 4125 *p++ = (char)c; 4126 continue; 4127 } 4128 *p = '\0'; 4129 lungetc(c); 4130 break; 4131 } 4132 val = symget(buf); 4133 if (val == NULL) { 4134 yyerror("macro '%s' not defined", buf); 4135 return (findeol()); 4136 } 4137 parsebuf = val; 4138 parseindex = 0; 4139 goto top; 4140 } 4141 4142 switch (c) { 4143 case '\'': 4144 case '"': 4145 endc = c; 4146 while (1) { 4147 if ((c = lgetc(fin)) == EOF) 4148 return (0); 4149 if (c == endc) { 4150 *p = '\0'; 4151 break; 4152 } 4153 if (c == '\n') { 4154 lineno++; 4155 continue; 4156 } 4157 if (p + 1 >= buf + sizeof(buf) - 1) { 4158 yyerror("string too long"); 4159 return (findeol()); 4160 } 4161 *p++ = (char)c; 4162 } 4163 yylval.v.string = strdup(buf); 4164 if (yylval.v.string == NULL) 4165 err(1, "yylex: strdup"); 4166 return (STRING); 4167 case '<': 4168 next = lgetc(fin); 4169 if (next == '>') { 4170 yylval.v.i = PF_OP_XRG; 4171 return (PORTBINARY); 4172 } 4173 lungetc(next); 4174 break; 4175 case '>': 4176 next = lgetc(fin); 4177 if (next == '<') { 4178 yylval.v.i = PF_OP_IRG; 4179 return (PORTBINARY); 4180 } 4181 lungetc(next); 4182 break; 4183 case '-': 4184 next = lgetc(fin); 4185 if (next == '>') 4186 return (ARROW); 4187 lungetc(next); 4188 break; 4189 } 4190 4191#define allowed_in_string(x) \ 4192 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \ 4193 x != '{' && x != '}' && x != '<' && x != '>' && \ 4194 x != '!' && x != '=' && x != '/' && x != '#' && \ 4195 x != ',')) 4196 4197 if (isalnum(c) || c == ':' || c == '_') { 4198 do { 4199 *p++ = c; 4200 if ((unsigned)(p-buf) >= sizeof(buf)) { 4201 yyerror("string too long"); 4202 return (findeol()); 4203 } 4204 } while ((c = lgetc(fin)) != EOF && (allowed_in_string(c))); 4205 lungetc(c); 4206 *p = '\0'; 4207 token = lookup(buf); 4208 yylval.v.string = strdup(buf); 4209 if (yylval.v.string == NULL) 4210 err(1, "yylex: strdup"); 4211 return (token); 4212 } 4213 if (c == '\n') { 4214 yylval.lineno = lineno; 4215 lineno++; 4216 } 4217 if (c == EOF) 4218 return (0); 4219 return (c); 4220} 4221 4222int 4223parse_rules(FILE *input, struct pfctl *xpf) 4224{ 4225 struct sym *sym; 4226 4227 fin = input; 4228 pf = xpf; 4229 lineno = 1; 4230 errors = 0; 4231 rulestate = PFCTL_STATE_NONE; 4232 returnicmpdefault = (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT; 4233 returnicmp6default = 4234 (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT; 4235 blockpolicy = PFRULE_DROP; 4236 require_order = 1; 4237 4238 yyparse(); 4239 4240 /* Free macros and check which have not been used. */ 4241 TAILQ_FOREACH(sym, &symhead, entries) { 4242 if ((pf->opts & PF_OPT_VERBOSE2) && !sym->used) 4243 fprintf(stderr, "warning: macro '%s' not " 4244 "used\n", sym->nam); 4245 free(sym->nam); 4246 free(sym->val); 4247 TAILQ_REMOVE(&symhead, sym, entries); 4248 } 4249 4250 return (errors ? -1 : 0); 4251} 4252 4253/* 4254 * Over-designed efficiency is a French and German concept, so how about 4255 * we wait until they discover this ugliness and make it all fancy. 4256 */ 4257int 4258symset(const char *nam, const char *val, int persist) 4259{ 4260 struct sym *sym; 4261 4262 for (sym = TAILQ_FIRST(&symhead); sym && strcmp(nam, sym->nam); 4263 sym = TAILQ_NEXT(sym, entries)) 4264 ; /* nothing */ 4265 4266 if (sym != NULL) { 4267 if (sym->persist == 1) 4268 return (0); 4269 else { 4270 free(sym->nam); 4271 free(sym->val); 4272 TAILQ_REMOVE(&symhead, sym, entries); 4273 free(sym); 4274 } 4275 } 4276 if ((sym = calloc(1, sizeof(*sym))) == NULL) 4277 return (-1); 4278 4279 sym->nam = strdup(nam); 4280 if (sym->nam == NULL) { 4281 free(sym); 4282 return (-1); 4283 } 4284 sym->val = strdup(val); 4285 if (sym->val == NULL) { 4286 free(sym->nam); 4287 free(sym); 4288 return (-1); 4289 } 4290 sym->used = 0; 4291 sym->persist = persist; 4292 TAILQ_INSERT_TAIL(&symhead, sym, entries); 4293 return (0); 4294} 4295 4296int 4297pfctl_cmdline_symset(char *s) 4298{ 4299 char *sym, *val; 4300 int ret; 4301 4302 if ((val = strrchr(s, '=')) == NULL) 4303 return (-1); 4304 4305 if ((sym = malloc(strlen(s) - strlen(val) + 1)) == NULL) 4306 err(1, "pfctl_cmdline_symset: malloc"); 4307 4308 strlcpy(sym, s, strlen(s) - strlen(val) + 1); 4309 4310 ret = symset(sym, val + 1, 1); 4311 free(sym); 4312 4313 return (ret); 4314} 4315 4316char * 4317symget(const char *nam) 4318{ 4319 struct sym *sym; 4320 4321 TAILQ_FOREACH(sym, &symhead, entries) 4322 if (strcmp(nam, sym->nam) == 0) { 4323 sym->used = 1; 4324 return (sym->val); 4325 } 4326 return (NULL); 4327} 4328 4329void 4330decide_address_family(struct node_host *n, sa_family_t *af) 4331{ 4332 sa_family_t target_af = 0; 4333 4334 while (!*af && n != NULL) { 4335 if (n->af) { 4336 if (target_af == 0) 4337 target_af = n->af; 4338 if (target_af != n->af) 4339 return; 4340 } 4341 n = n->next; 4342 } 4343 if (!*af && target_af) 4344 *af = target_af; 4345} 4346 4347void 4348remove_invalid_hosts(struct node_host **nh, sa_family_t *af) 4349{ 4350 struct node_host *n = *nh, *prev = NULL; 4351 4352 while (n != NULL) { 4353 if (*af && n->af && n->af != *af) { 4354 /* unlink and free n */ 4355 struct node_host *next = n->next; 4356 4357 /* adjust tail pointer */ 4358 if (n == (*nh)->tail) 4359 (*nh)->tail = prev; 4360 /* adjust previous node's next pointer */ 4361 if (prev == NULL) 4362 *nh = next; 4363 else 4364 prev->next = next; 4365 /* free node */ 4366 if (n->ifname != NULL) 4367 free(n->ifname); 4368 free(n); 4369 n = next; 4370 } else { 4371 if (n->af && !*af) 4372 *af = n->af; 4373 prev = n; 4374 n = n->next; 4375 } 4376 } 4377} 4378 4379int 4380invalid_redirect(struct node_host *nh, sa_family_t af) 4381{ 4382 if (!af) { 4383 struct node_host *n; 4384 4385 /* only tables are ok without an address family */ 4386 for (n = nh; n != NULL; n = n->next) { 4387 if (n->addr.type != PF_ADDR_TABLE) { 4388 yyerror("address family not given and " 4389 "translation address expands to multiple " 4390 "address families"); 4391 return (1); 4392 } 4393 } 4394 } 4395 if (nh == NULL) { 4396 yyerror("no translation address with matching address family " 4397 "found."); 4398 return (1); 4399 } 4400 return (0); 4401} 4402 4403int 4404atoul(char *s, u_long *ulvalp) 4405{ 4406 u_long ulval; 4407 char *ep; 4408 4409 errno = 0; 4410 ulval = strtoul(s, &ep, 0); 4411 if (s[0] == '\0' || *ep != '\0') 4412 return (-1); 4413 if (errno == ERANGE && ulval == ULONG_MAX) 4414 return (-1); 4415 *ulvalp = ulval; 4416 return (0); 4417} 4418 4419int 4420getservice(char *n) 4421{ 4422 struct servent *s; 4423 u_long ulval; 4424 4425 if (atoul(n, &ulval) == 0) { 4426 if (ulval > 65535) { 4427 yyerror("illegal port value %d", ulval); 4428 return (-1); 4429 } 4430 return (htons(ulval)); 4431 } else { 4432 s = getservbyname(n, "tcp"); 4433 if (s == NULL) 4434 s = getservbyname(n, "udp"); 4435 if (s == NULL) { 4436 yyerror("unknown port %s", n); 4437 return (-1); 4438 } 4439 return (s->s_port); 4440 } 4441} 4442 4443int 4444rule_label(struct pf_rule *r, char *s) 4445{ 4446 if (s) { 4447 if (strlcpy(r->label, s, sizeof(r->label)) >= 4448 sizeof(r->label)) { 4449 yyerror("rule label too long (max %d chars)", 4450 sizeof(r->label)-1); 4451 return (-1); 4452 } 4453 } 4454 return (0); 4455} 4456 4457u_int16_t 4458parseicmpspec(char *w, sa_family_t af) 4459{ 4460 const struct icmpcodeent *p; 4461 u_long ulval; 4462 u_int8_t icmptype; 4463 4464 if (af == AF_INET) 4465 icmptype = returnicmpdefault >> 8; 4466 else 4467 icmptype = returnicmp6default >> 8; 4468 4469 if (atoul(w, &ulval) == -1) { 4470 if ((p = geticmpcodebyname(icmptype, w, af)) == NULL) { 4471 yyerror("unknown icmp code %s", w); 4472 return (0); 4473 } 4474 ulval = p->code; 4475 } 4476 if (ulval > 255) { 4477 yyerror("invalid icmp code %ld", ulval); 4478 return (0); 4479 } 4480 return (icmptype << 8 | ulval); 4481} 4482 4483int 4484pfctl_load_anchors(int dev, int opts, struct pfr_buffer *trans) 4485{ 4486 struct loadanchors *la; 4487 4488 TAILQ_FOREACH(la, &loadanchorshead, entries) { 4489 if (opts & PF_OPT_VERBOSE) 4490 fprintf(stderr, "\nLoading anchor %s:%s from %s\n", 4491 la->anchorname, la->rulesetname, la->filename); 4492 if (pfctl_rules(dev, la->filename, opts, la->anchorname, 4493 la->rulesetname, trans) == -1) 4494 return (-1); 4495 } 4496 4497 return (0); 4498} 4499 4500