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