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