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