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