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