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 $$->ifname = strdup($1); 4586 set_ipmask($$, 128); 4587 $$->next = NULL; 4588 $$->tail = $$; 4589 } 4590 | '(' STRING host ')' { 4591 struct node_host *n; 4592 4593 $$ = $3; 4594 for (n = $3; n != NULL; n = n->next) 4595 n->ifname = strdup($2); 4596 } 4597 ; 4598 4599 route_host_list : route_host optnl { $$ = $1; } 4600 | route_host_list comma route_host optnl { 4601 if ($1->af == 0) 4602 $1->af = $3->af; 4603 if ($1->af != $3->af) { 4604 yyerror("all pool addresses must be in the " 4605 "same address family"); 4606 YYERROR; 4607 } 4608 $1->tail->next = $3; 4609 $1->tail = $3->tail; 4610 $$ = $1; 4611 } 4612 ; 4613 4614 routespec : route_host { $$ = $1; } 4615 | '{' optnl route_host_list '}' { $$ = $3; } 4616 ; 4617 4618 route : /* empty */ { 4619 $$.host = NULL; 4620 $$.rt = 0; 4621 $$.pool_opts = 0; 4622 } 4623 | FASTROUTE { 4624 /* backwards-compat */ 4625 $$.host = NULL; 4626 $$.rt = 0; 4627 $$.pool_opts = 0; 4628 } 4629 | ROUTETO routespec pool_opts { 4630 $$.host = $2; 4631 $$.rt = PF_ROUTETO; 4632 $$.pool_opts = $3.type | $3.opts; 4633 if ($3.key != NULL) 4634 $$.key = $3.key; 4635 } 4636 | REPLYTO routespec pool_opts { 4637 $$.host = $2; 4638 $$.rt = PF_REPLYTO; 4639 $$.pool_opts = $3.type | $3.opts; 4640 if ($3.key != NULL) 4641 $$.key = $3.key; 4642 } 4643 | DUPTO routespec pool_opts { 4644 $$.host = $2; 4645 $$.rt = PF_DUPTO; 4646 $$.pool_opts = $3.type | $3.opts; 4647 if ($3.key != NULL) 4648 $$.key = $3.key; 4649 } 4650 ; 4651 4652 timeout_spec : STRING NUMBER 4653 { 4654 if (check_rulestate(PFCTL_STATE_OPTION)) { 4655 free($1); 4656 YYERROR; 4657 } 4658 if ($2 < 0 || $2 > UINT_MAX) { 4659 yyerror("only positive values permitted"); 4660 YYERROR; 4661 } 4662 if (pfctl_set_timeout(pf, $1, $2, 0) != 0) { 4663 yyerror("unknown timeout %s", $1); 4664 free($1); 4665 YYERROR; 4666 } 4667 free($1); 4668 } 4669 | INTERVAL NUMBER { 4670 if (check_rulestate(PFCTL_STATE_OPTION)) 4671 YYERROR; 4672 if ($2 < 0 || $2 > UINT_MAX) { 4673 yyerror("only positive values permitted"); 4674 YYERROR; 4675 } 4676 if (pfctl_set_timeout(pf, "interval", $2, 0) != 0) 4677 YYERROR; 4678 } 4679 ; 4680 4681 timeout_list : timeout_list comma timeout_spec optnl 4682 | timeout_spec optnl 4683 ; 4684 4685 limit_spec : STRING NUMBER 4686 { 4687 if (check_rulestate(PFCTL_STATE_OPTION)) { 4688 free($1); 4689 YYERROR; 4690 } 4691 if ($2 < 0 || $2 > UINT_MAX) { 4692 yyerror("only positive values permitted"); 4693 YYERROR; 4694 } 4695 if (pfctl_set_limit(pf, $1, $2) != 0) { 4696 yyerror("unable to set limit %s %u", $1, $2); 4697 free($1); 4698 YYERROR; 4699 } 4700 free($1); 4701 } 4702 ; 4703 4704 limit_list : limit_list comma limit_spec optnl 4705 | limit_spec optnl 4706 ; 4707 4708 comma : ',' 4709 | /* empty */ 4710 ; 4711 4712 yesno : NO { $$ = 0; } 4713 | STRING { 4714 if (!strcmp($1, "yes")) 4715 $$ = 1; 4716 else { 4717 yyerror("invalid value '%s', expected 'yes' " 4718 "or 'no'", $1); 4719 free($1); 4720 YYERROR; 4721 } 4722 free($1); 4723 } 4724 ; 4725 4726 unaryop : '=' { $$ = PF_OP_EQ; } 4727 | '!' '=' { $$ = PF_OP_NE; } 4728 | '<' '=' { $$ = PF_OP_LE; } 4729 | '<' { $$ = PF_OP_LT; } 4730 | '>' '=' { $$ = PF_OP_GE; } 4731 | '>' { $$ = PF_OP_GT; } 4732 ; 4733 4734 %% 4735 4736 int 4737 yyerror(const char *fmt, ...) 4738 { 4739 va_list ap; 4740 4741 file->errors++; 4742 va_start(ap, fmt); 4743 fprintf(stderr, "%s:%d: ", file->name, yylval.lineno); 4744 vfprintf(stderr, fmt, ap); 4745 fprintf(stderr, "\n"); 4746 va_end(ap); 4747 return (0); 4748 } 4749 4750 int 4751 disallow_table(struct node_host *h, const char *fmt) 4752 { 4753 for (; h != NULL; h = h->next) 4754 if (h->addr.type == PF_ADDR_TABLE) { 4755 yyerror(fmt, h->addr.v.tblname); 4756 return (1); 4757 } 4758 return (0); 4759 } 4760 4761 int 4762 disallow_urpf_failed(struct node_host *h, const char *fmt) 4763 { 4764 for (; h != NULL; h = h->next) 4765 if (h->addr.type == PF_ADDR_URPFFAILED) { 4766 yyerror(fmt); 4767 return (1); 4768 } 4769 return (0); 4770 } 4771 4772 int 4773 disallow_alias(struct node_host *h, const char *fmt) 4774 { 4775 for (; h != NULL; h = h->next) 4776 if (DYNIF_MULTIADDR(h->addr)) { 4777 yyerror(fmt, h->addr.v.tblname); 4778 return (1); 4779 } 4780 return (0); 4781 } 4782 4783 int 4784 rule_consistent(struct pfctl_rule *r, int anchor_call) 4785 { 4786 int problems = 0; 4787 4788 switch (r->action) { 4789 case PF_PASS: 4790 case PF_DROP: 4791 case PF_SCRUB: 4792 case PF_NOSCRUB: 4793 problems = filter_consistent(r, anchor_call); 4794 break; 4795 case PF_NAT: 4796 case PF_NONAT: 4797 problems = nat_consistent(r); 4798 break; 4799 case PF_RDR: 4800 case PF_NORDR: 4801 problems = rdr_consistent(r); 4802 break; 4803 case PF_BINAT: 4804 case PF_NOBINAT: 4805 default: 4806 break; 4807 } 4808 return (problems); 4809 } 4810 4811 int 4812 filter_consistent(struct pfctl_rule *r, int anchor_call) 4813 { 4814 int problems = 0; 4815 4816 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP && 4817 (r->src.port_op || r->dst.port_op)) { 4818 yyerror("port only applies to tcp/udp"); 4819 problems++; 4820 } 4821 if (r->proto != IPPROTO_ICMP && r->proto != IPPROTO_ICMPV6 && 4822 (r->type || r->code)) { 4823 yyerror("icmp-type/code only applies to icmp"); 4824 problems++; 4825 } 4826 if (!r->af && (r->type || r->code)) { 4827 yyerror("must indicate address family with icmp-type/code"); 4828 problems++; 4829 } 4830 if (r->overload_tblname[0] && 4831 r->max_src_conn == 0 && r->max_src_conn_rate.seconds == 0) { 4832 yyerror("'overload' requires 'max-src-conn' " 4833 "or 'max-src-conn-rate'"); 4834 problems++; 4835 } 4836 if ((r->proto == IPPROTO_ICMP && r->af == AF_INET6) || 4837 (r->proto == IPPROTO_ICMPV6 && r->af == AF_INET)) { 4838 yyerror("proto %s doesn't match address family %s", 4839 r->proto == IPPROTO_ICMP ? "icmp" : "icmp6", 4840 r->af == AF_INET ? "inet" : "inet6"); 4841 problems++; 4842 } 4843 if (r->allow_opts && r->action != PF_PASS) { 4844 yyerror("allow-opts can only be specified for pass rules"); 4845 problems++; 4846 } 4847 if (r->rule_flag & PFRULE_FRAGMENT && (r->src.port_op || 4848 r->dst.port_op || r->flagset || r->type || r->code)) { 4849 yyerror("fragments can be filtered only on IP header fields"); 4850 problems++; 4851 } 4852 if (r->rule_flag & PFRULE_RETURNRST && r->proto != IPPROTO_TCP) { 4853 yyerror("return-rst can only be applied to TCP rules"); 4854 problems++; 4855 } 4856 if (r->max_src_nodes && !(r->rule_flag & PFRULE_RULESRCTRACK)) { 4857 yyerror("max-src-nodes requires 'source-track rule'"); 4858 problems++; 4859 } 4860 if (r->action == PF_DROP && r->keep_state) { 4861 yyerror("keep state on block rules doesn't make sense"); 4862 problems++; 4863 } 4864 if (r->rule_flag & PFRULE_STATESLOPPY && 4865 (r->keep_state == PF_STATE_MODULATE || 4866 r->keep_state == PF_STATE_SYNPROXY)) { 4867 yyerror("sloppy state matching cannot be used with " 4868 "synproxy state or modulate state"); 4869 problems++; 4870 } 4871 return (-problems); 4872 } 4873 4874 int 4875 nat_consistent(struct pfctl_rule *r) 4876 { 4877 return (0); /* yeah! */ 4878 } 4879 4880 int 4881 rdr_consistent(struct pfctl_rule *r) 4882 { 4883 int problems = 0; 4884 4885 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP) { 4886 if (r->src.port_op) { 4887 yyerror("src port only applies to tcp/udp"); 4888 problems++; 4889 } 4890 if (r->dst.port_op) { 4891 yyerror("dst port only applies to tcp/udp"); 4892 problems++; 4893 } 4894 if (r->rpool.proxy_port[0]) { 4895 yyerror("rpool port only applies to tcp/udp"); 4896 problems++; 4897 } 4898 } 4899 if (r->dst.port_op && 4900 r->dst.port_op != PF_OP_EQ && r->dst.port_op != PF_OP_RRG) { 4901 yyerror("invalid port operator for rdr destination port"); 4902 problems++; 4903 } 4904 return (-problems); 4905 } 4906 4907 int 4908 process_tabledef(char *name, struct table_opts *opts) 4909 { 4910 struct pfr_buffer ab; 4911 struct node_tinit *ti; 4912 unsigned long maxcount; 4913 size_t s = sizeof(maxcount); 4914 4915 bzero(&ab, sizeof(ab)); 4916 ab.pfrb_type = PFRB_ADDRS; 4917 SIMPLEQ_FOREACH(ti, &opts->init_nodes, entries) { 4918 if (ti->file) 4919 if (pfr_buf_load(&ab, ti->file, 0, append_addr)) { 4920 if (errno) 4921 yyerror("cannot load \"%s\": %s", 4922 ti->file, strerror(errno)); 4923 else 4924 yyerror("file \"%s\" contains bad data", 4925 ti->file); 4926 goto _error; 4927 } 4928 if (ti->host) 4929 if (append_addr_host(&ab, ti->host, 0, 0)) { 4930 yyerror("cannot create address buffer: %s", 4931 strerror(errno)); 4932 goto _error; 4933 } 4934 } 4935 if (pf->opts & PF_OPT_VERBOSE) 4936 print_tabledef(name, opts->flags, opts->init_addr, 4937 &opts->init_nodes); 4938 if (!(pf->opts & PF_OPT_NOACTION) && 4939 pfctl_define_table(name, opts->flags, opts->init_addr, 4940 pf->anchor->name, &ab, pf->anchor->ruleset.tticket)) { 4941 4942 if (sysctlbyname("net.pf.request_maxcount", &maxcount, &s, 4943 NULL, 0) == -1) 4944 maxcount = 65535; 4945 4946 if (ab.pfrb_size > maxcount) 4947 yyerror("cannot define table %s: too many elements.\n" 4948 "Consider increasing net.pf.request_maxcount.", 4949 name); 4950 else 4951 yyerror("cannot define table %s: %s", name, 4952 pfr_strerror(errno)); 4953 4954 goto _error; 4955 } 4956 pf->tdirty = 1; 4957 pfr_buf_clear(&ab); 4958 return (0); 4959 _error: 4960 pfr_buf_clear(&ab); 4961 return (-1); 4962 } 4963 4964 struct keywords { 4965 const char *k_name; 4966 int k_val; 4967 }; 4968 4969 /* macro gore, but you should've seen the prior indentation nightmare... */ 4970 4971 #define FREE_LIST(T,r) \ 4972 do { \ 4973 T *p, *node = r; \ 4974 while (node != NULL) { \ 4975 p = node; \ 4976 node = node->next; \ 4977 free(p); \ 4978 } \ 4979 } while (0) 4980 4981 #define LOOP_THROUGH(T,n,r,C) \ 4982 do { \ 4983 T *n; \ 4984 if (r == NULL) { \ 4985 r = calloc(1, sizeof(T)); \ 4986 if (r == NULL) \ 4987 err(1, "LOOP: calloc"); \ 4988 r->next = NULL; \ 4989 } \ 4990 n = r; \ 4991 while (n != NULL) { \ 4992 do { \ 4993 C; \ 4994 } while (0); \ 4995 n = n->next; \ 4996 } \ 4997 } while (0) 4998 4999 void 5000 expand_label_str(char *label, size_t len, const char *srch, const char *repl) 5001 { 5002 char *tmp; 5003 char *p, *q; 5004 5005 if ((tmp = calloc(1, len)) == NULL) 5006 err(1, "expand_label_str: calloc"); 5007 p = q = label; 5008 while ((q = strstr(p, srch)) != NULL) { 5009 *q = '\0'; 5010 if ((strlcat(tmp, p, len) >= len) || 5011 (strlcat(tmp, repl, len) >= len)) 5012 errx(1, "expand_label: label too long"); 5013 q += strlen(srch); 5014 p = q; 5015 } 5016 if (strlcat(tmp, p, len) >= len) 5017 errx(1, "expand_label: label too long"); 5018 strlcpy(label, tmp, len); /* always fits */ 5019 free(tmp); 5020 } 5021 5022 void 5023 expand_label_if(const char *name, char *label, size_t len, const char *ifname) 5024 { 5025 if (strstr(label, name) != NULL) { 5026 if (!*ifname) 5027 expand_label_str(label, len, name, "any"); 5028 else 5029 expand_label_str(label, len, name, ifname); 5030 } 5031 } 5032 5033 void 5034 expand_label_addr(const char *name, char *label, size_t len, sa_family_t af, 5035 struct pf_rule_addr *addr) 5036 { 5037 char tmp[64], tmp_not[66]; 5038 5039 if (strstr(label, name) != NULL) { 5040 switch (addr->addr.type) { 5041 case PF_ADDR_DYNIFTL: 5042 snprintf(tmp, sizeof(tmp), "(%s)", addr->addr.v.ifname); 5043 break; 5044 case PF_ADDR_TABLE: 5045 snprintf(tmp, sizeof(tmp), "<%s>", addr->addr.v.tblname); 5046 break; 5047 case PF_ADDR_NOROUTE: 5048 snprintf(tmp, sizeof(tmp), "no-route"); 5049 break; 5050 case PF_ADDR_URPFFAILED: 5051 snprintf(tmp, sizeof(tmp), "urpf-failed"); 5052 break; 5053 case PF_ADDR_ADDRMASK: 5054 if (!af || (PF_AZERO(&addr->addr.v.a.addr, af) && 5055 PF_AZERO(&addr->addr.v.a.mask, af))) 5056 snprintf(tmp, sizeof(tmp), "any"); 5057 else { 5058 char a[48]; 5059 int bits; 5060 5061 if (inet_ntop(af, &addr->addr.v.a.addr, a, 5062 sizeof(a)) == NULL) 5063 snprintf(tmp, sizeof(tmp), "?"); 5064 else { 5065 bits = unmask(&addr->addr.v.a.mask, af); 5066 if ((af == AF_INET && bits < 32) || 5067 (af == AF_INET6 && bits < 128)) 5068 snprintf(tmp, sizeof(tmp), 5069 "%s/%d", a, bits); 5070 else 5071 snprintf(tmp, sizeof(tmp), 5072 "%s", a); 5073 } 5074 } 5075 break; 5076 default: 5077 snprintf(tmp, sizeof(tmp), "?"); 5078 break; 5079 } 5080 5081 if (addr->neg) { 5082 snprintf(tmp_not, sizeof(tmp_not), "! %s", tmp); 5083 expand_label_str(label, len, name, tmp_not); 5084 } else 5085 expand_label_str(label, len, name, tmp); 5086 } 5087 } 5088 5089 void 5090 expand_label_port(const char *name, char *label, size_t len, 5091 struct pf_rule_addr *addr) 5092 { 5093 char a1[6], a2[6], op[13] = ""; 5094 5095 if (strstr(label, name) != NULL) { 5096 snprintf(a1, sizeof(a1), "%u", ntohs(addr->port[0])); 5097 snprintf(a2, sizeof(a2), "%u", ntohs(addr->port[1])); 5098 if (!addr->port_op) 5099 ; 5100 else if (addr->port_op == PF_OP_IRG) 5101 snprintf(op, sizeof(op), "%s><%s", a1, a2); 5102 else if (addr->port_op == PF_OP_XRG) 5103 snprintf(op, sizeof(op), "%s<>%s", a1, a2); 5104 else if (addr->port_op == PF_OP_EQ) 5105 snprintf(op, sizeof(op), "%s", a1); 5106 else if (addr->port_op == PF_OP_NE) 5107 snprintf(op, sizeof(op), "!=%s", a1); 5108 else if (addr->port_op == PF_OP_LT) 5109 snprintf(op, sizeof(op), "<%s", a1); 5110 else if (addr->port_op == PF_OP_LE) 5111 snprintf(op, sizeof(op), "<=%s", a1); 5112 else if (addr->port_op == PF_OP_GT) 5113 snprintf(op, sizeof(op), ">%s", a1); 5114 else if (addr->port_op == PF_OP_GE) 5115 snprintf(op, sizeof(op), ">=%s", a1); 5116 expand_label_str(label, len, name, op); 5117 } 5118 } 5119 5120 void 5121 expand_label_proto(const char *name, char *label, size_t len, u_int8_t proto) 5122 { 5123 const char *protoname; 5124 char n[4]; 5125 5126 if (strstr(label, name) != NULL) { 5127 protoname = pfctl_proto2name(proto); 5128 if (protoname != NULL) 5129 expand_label_str(label, len, name, protoname); 5130 else { 5131 snprintf(n, sizeof(n), "%u", proto); 5132 expand_label_str(label, len, name, n); 5133 } 5134 } 5135 } 5136 5137 void 5138 expand_label_nr(const char *name, char *label, size_t len, 5139 struct pfctl_rule *r) 5140 { 5141 char n[11]; 5142 5143 if (strstr(label, name) != NULL) { 5144 snprintf(n, sizeof(n), "%u", r->nr); 5145 expand_label_str(label, len, name, n); 5146 } 5147 } 5148 5149 void 5150 expand_label(char *label, size_t len, struct pfctl_rule *r) 5151 { 5152 expand_label_if("$if", label, len, r->ifname); 5153 expand_label_addr("$srcaddr", label, len, r->af, &r->src); 5154 expand_label_addr("$dstaddr", label, len, r->af, &r->dst); 5155 expand_label_port("$srcport", label, len, &r->src); 5156 expand_label_port("$dstport", label, len, &r->dst); 5157 expand_label_proto("$proto", label, len, r->proto); 5158 expand_label_nr("$nr", label, len, r); 5159 } 5160 5161 int 5162 expand_altq(struct pf_altq *a, struct node_if *interfaces, 5163 struct node_queue *nqueues, struct node_queue_bw bwspec, 5164 struct node_queue_opt *opts) 5165 { 5166 struct pf_altq pa, pb; 5167 char qname[PF_QNAME_SIZE]; 5168 struct node_queue *n; 5169 struct node_queue_bw bw; 5170 int errs = 0; 5171 5172 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) { 5173 FREE_LIST(struct node_if, interfaces); 5174 if (nqueues) 5175 FREE_LIST(struct node_queue, nqueues); 5176 return (0); 5177 } 5178 5179 LOOP_THROUGH(struct node_if, interface, interfaces, 5180 memcpy(&pa, a, sizeof(struct pf_altq)); 5181 if (strlcpy(pa.ifname, interface->ifname, 5182 sizeof(pa.ifname)) >= sizeof(pa.ifname)) 5183 errx(1, "expand_altq: strlcpy"); 5184 5185 if (interface->not) { 5186 yyerror("altq on ! <interface> is not supported"); 5187 errs++; 5188 } else { 5189 if (eval_pfaltq(pf, &pa, &bwspec, opts)) 5190 errs++; 5191 else 5192 if (pfctl_add_altq(pf, &pa)) 5193 errs++; 5194 5195 if (pf->opts & PF_OPT_VERBOSE) { 5196 print_altq(&pf->paltq->altq, 0, 5197 &bwspec, opts); 5198 if (nqueues && nqueues->tail) { 5199 printf("queue { "); 5200 LOOP_THROUGH(struct node_queue, queue, 5201 nqueues, 5202 printf("%s ", 5203 queue->queue); 5204 ); 5205 printf("}"); 5206 } 5207 printf("\n"); 5208 } 5209 5210 if (pa.scheduler == ALTQT_CBQ || 5211 pa.scheduler == ALTQT_HFSC || 5212 pa.scheduler == ALTQT_FAIRQ) { 5213 /* now create a root queue */ 5214 memset(&pb, 0, sizeof(struct pf_altq)); 5215 if (strlcpy(qname, "root_", sizeof(qname)) >= 5216 sizeof(qname)) 5217 errx(1, "expand_altq: strlcpy"); 5218 if (strlcat(qname, interface->ifname, 5219 sizeof(qname)) >= sizeof(qname)) 5220 errx(1, "expand_altq: strlcat"); 5221 if (strlcpy(pb.qname, qname, 5222 sizeof(pb.qname)) >= sizeof(pb.qname)) 5223 errx(1, "expand_altq: strlcpy"); 5224 if (strlcpy(pb.ifname, interface->ifname, 5225 sizeof(pb.ifname)) >= sizeof(pb.ifname)) 5226 errx(1, "expand_altq: strlcpy"); 5227 pb.qlimit = pa.qlimit; 5228 pb.scheduler = pa.scheduler; 5229 bw.bw_absolute = pa.ifbandwidth; 5230 bw.bw_percent = 0; 5231 if (eval_pfqueue(pf, &pb, &bw, opts)) 5232 errs++; 5233 else 5234 if (pfctl_add_altq(pf, &pb)) 5235 errs++; 5236 } 5237 5238 LOOP_THROUGH(struct node_queue, queue, nqueues, 5239 n = calloc(1, sizeof(struct node_queue)); 5240 if (n == NULL) 5241 err(1, "expand_altq: calloc"); 5242 if (pa.scheduler == ALTQT_CBQ || 5243 pa.scheduler == ALTQT_HFSC || 5244 pa.scheduler == ALTQT_FAIRQ) 5245 if (strlcpy(n->parent, qname, 5246 sizeof(n->parent)) >= 5247 sizeof(n->parent)) 5248 errx(1, "expand_altq: strlcpy"); 5249 if (strlcpy(n->queue, queue->queue, 5250 sizeof(n->queue)) >= sizeof(n->queue)) 5251 errx(1, "expand_altq: strlcpy"); 5252 if (strlcpy(n->ifname, interface->ifname, 5253 sizeof(n->ifname)) >= sizeof(n->ifname)) 5254 errx(1, "expand_altq: strlcpy"); 5255 n->scheduler = pa.scheduler; 5256 n->next = NULL; 5257 n->tail = n; 5258 if (queues == NULL) 5259 queues = n; 5260 else { 5261 queues->tail->next = n; 5262 queues->tail = n; 5263 } 5264 ); 5265 } 5266 ); 5267 FREE_LIST(struct node_if, interfaces); 5268 if (nqueues) 5269 FREE_LIST(struct node_queue, nqueues); 5270 5271 return (errs); 5272 } 5273 5274 int 5275 expand_queue(struct pf_altq *a, struct node_if *interfaces, 5276 struct node_queue *nqueues, struct node_queue_bw bwspec, 5277 struct node_queue_opt *opts) 5278 { 5279 struct node_queue *n, *nq; 5280 struct pf_altq pa; 5281 u_int8_t found = 0; 5282 u_int8_t errs = 0; 5283 5284 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) { 5285 FREE_LIST(struct node_queue, nqueues); 5286 return (0); 5287 } 5288 5289 if (queues == NULL) { 5290 yyerror("queue %s has no parent", a->qname); 5291 FREE_LIST(struct node_queue, nqueues); 5292 return (1); 5293 } 5294 5295 LOOP_THROUGH(struct node_if, interface, interfaces, 5296 LOOP_THROUGH(struct node_queue, tqueue, queues, 5297 if (!strncmp(a->qname, tqueue->queue, PF_QNAME_SIZE) && 5298 (interface->ifname[0] == 0 || 5299 (!interface->not && !strncmp(interface->ifname, 5300 tqueue->ifname, IFNAMSIZ)) || 5301 (interface->not && strncmp(interface->ifname, 5302 tqueue->ifname, IFNAMSIZ)))) { 5303 /* found ourself in queues */ 5304 found++; 5305 5306 memcpy(&pa, a, sizeof(struct pf_altq)); 5307 5308 if (pa.scheduler != ALTQT_NONE && 5309 pa.scheduler != tqueue->scheduler) { 5310 yyerror("exactly one scheduler type " 5311 "per interface allowed"); 5312 return (1); 5313 } 5314 pa.scheduler = tqueue->scheduler; 5315 5316 /* scheduler dependent error checking */ 5317 switch (pa.scheduler) { 5318 case ALTQT_PRIQ: 5319 if (nqueues != NULL) { 5320 yyerror("priq queues cannot " 5321 "have child queues"); 5322 return (1); 5323 } 5324 if (bwspec.bw_absolute > 0 || 5325 bwspec.bw_percent < 100) { 5326 yyerror("priq doesn't take " 5327 "bandwidth"); 5328 return (1); 5329 } 5330 break; 5331 default: 5332 break; 5333 } 5334 5335 if (strlcpy(pa.ifname, tqueue->ifname, 5336 sizeof(pa.ifname)) >= sizeof(pa.ifname)) 5337 errx(1, "expand_queue: strlcpy"); 5338 if (strlcpy(pa.parent, tqueue->parent, 5339 sizeof(pa.parent)) >= sizeof(pa.parent)) 5340 errx(1, "expand_queue: strlcpy"); 5341 5342 if (eval_pfqueue(pf, &pa, &bwspec, opts)) 5343 errs++; 5344 else 5345 if (pfctl_add_altq(pf, &pa)) 5346 errs++; 5347 5348 for (nq = nqueues; nq != NULL; nq = nq->next) { 5349 if (!strcmp(a->qname, nq->queue)) { 5350 yyerror("queue cannot have " 5351 "itself as child"); 5352 errs++; 5353 continue; 5354 } 5355 n = calloc(1, 5356 sizeof(struct node_queue)); 5357 if (n == NULL) 5358 err(1, "expand_queue: calloc"); 5359 if (strlcpy(n->parent, a->qname, 5360 sizeof(n->parent)) >= 5361 sizeof(n->parent)) 5362 errx(1, "expand_queue strlcpy"); 5363 if (strlcpy(n->queue, nq->queue, 5364 sizeof(n->queue)) >= 5365 sizeof(n->queue)) 5366 errx(1, "expand_queue strlcpy"); 5367 if (strlcpy(n->ifname, tqueue->ifname, 5368 sizeof(n->ifname)) >= 5369 sizeof(n->ifname)) 5370 errx(1, "expand_queue strlcpy"); 5371 n->scheduler = tqueue->scheduler; 5372 n->next = NULL; 5373 n->tail = n; 5374 if (queues == NULL) 5375 queues = n; 5376 else { 5377 queues->tail->next = n; 5378 queues->tail = n; 5379 } 5380 } 5381 if ((pf->opts & PF_OPT_VERBOSE) && ( 5382 (found == 1 && interface->ifname[0] == 0) || 5383 (found > 0 && interface->ifname[0] != 0))) { 5384 print_queue(&pf->paltq->altq, 0, 5385 &bwspec, interface->ifname[0] != 0, 5386 opts); 5387 if (nqueues && nqueues->tail) { 5388 printf("{ "); 5389 LOOP_THROUGH(struct node_queue, 5390 queue, nqueues, 5391 printf("%s ", 5392 queue->queue); 5393 ); 5394 printf("}"); 5395 } 5396 printf("\n"); 5397 } 5398 } 5399 ); 5400 ); 5401 5402 FREE_LIST(struct node_queue, nqueues); 5403 FREE_LIST(struct node_if, interfaces); 5404 5405 if (!found) { 5406 yyerror("queue %s has no parent", a->qname); 5407 errs++; 5408 } 5409 5410 if (errs) 5411 return (1); 5412 else 5413 return (0); 5414 } 5415 5416 void 5417 expand_rule(struct pfctl_rule *r, 5418 struct node_if *interfaces, struct node_host *rpool_hosts, 5419 struct node_proto *protos, struct node_os *src_oses, 5420 struct node_host *src_hosts, struct node_port *src_ports, 5421 struct node_host *dst_hosts, struct node_port *dst_ports, 5422 struct node_uid *uids, struct node_gid *gids, struct node_icmp *icmp_types, 5423 const char *anchor_call) 5424 { 5425 sa_family_t af = r->af; 5426 int added = 0, error = 0; 5427 char ifname[IF_NAMESIZE]; 5428 char label[PF_RULE_MAX_LABEL_COUNT][PF_RULE_LABEL_SIZE]; 5429 char tagname[PF_TAG_NAME_SIZE]; 5430 char match_tagname[PF_TAG_NAME_SIZE]; 5431 struct pf_pooladdr *pa; 5432 struct node_host *h; 5433 u_int8_t flags, flagset, keep_state; 5434 5435 memcpy(label, r->label, sizeof(r->label)); 5436 assert(sizeof(r->label) == sizeof(label)); 5437 if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname)) 5438 errx(1, "expand_rule: strlcpy"); 5439 if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >= 5440 sizeof(match_tagname)) 5441 errx(1, "expand_rule: strlcpy"); 5442 flags = r->flags; 5443 flagset = r->flagset; 5444 keep_state = r->keep_state; 5445 5446 LOOP_THROUGH(struct node_if, interface, interfaces, 5447 LOOP_THROUGH(struct node_proto, proto, protos, 5448 LOOP_THROUGH(struct node_icmp, icmp_type, icmp_types, 5449 LOOP_THROUGH(struct node_host, src_host, src_hosts, 5450 LOOP_THROUGH(struct node_port, src_port, src_ports, 5451 LOOP_THROUGH(struct node_os, src_os, src_oses, 5452 LOOP_THROUGH(struct node_host, dst_host, dst_hosts, 5453 LOOP_THROUGH(struct node_port, dst_port, dst_ports, 5454 LOOP_THROUGH(struct node_uid, uid, uids, 5455 LOOP_THROUGH(struct node_gid, gid, gids, 5456 5457 r->af = af; 5458 /* for link-local IPv6 address, interface must match up */ 5459 if ((r->af && src_host->af && r->af != src_host->af) || 5460 (r->af && dst_host->af && r->af != dst_host->af) || 5461 (src_host->af && dst_host->af && 5462 src_host->af != dst_host->af) || 5463 (src_host->ifindex && dst_host->ifindex && 5464 src_host->ifindex != dst_host->ifindex) || 5465 (src_host->ifindex && *interface->ifname && 5466 src_host->ifindex != if_nametoindex(interface->ifname)) || 5467 (dst_host->ifindex && *interface->ifname && 5468 dst_host->ifindex != if_nametoindex(interface->ifname))) 5469 continue; 5470 if (!r->af && src_host->af) 5471 r->af = src_host->af; 5472 else if (!r->af && dst_host->af) 5473 r->af = dst_host->af; 5474 5475 if (*interface->ifname) 5476 strlcpy(r->ifname, interface->ifname, 5477 sizeof(r->ifname)); 5478 else if (if_indextoname(src_host->ifindex, ifname)) 5479 strlcpy(r->ifname, ifname, sizeof(r->ifname)); 5480 else if (if_indextoname(dst_host->ifindex, ifname)) 5481 strlcpy(r->ifname, ifname, sizeof(r->ifname)); 5482 else 5483 memset(r->ifname, '\0', sizeof(r->ifname)); 5484 5485 memcpy(r->label, label, sizeof(r->label)); 5486 if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >= 5487 sizeof(r->tagname)) 5488 errx(1, "expand_rule: strlcpy"); 5489 if (strlcpy(r->match_tagname, match_tagname, 5490 sizeof(r->match_tagname)) >= sizeof(r->match_tagname)) 5491 errx(1, "expand_rule: strlcpy"); 5492 5493 error += check_netmask(src_host, r->af); 5494 error += check_netmask(dst_host, r->af); 5495 5496 r->ifnot = interface->not; 5497 r->proto = proto->proto; 5498 r->src.addr = src_host->addr; 5499 r->src.neg = src_host->not; 5500 r->src.port[0] = src_port->port[0]; 5501 r->src.port[1] = src_port->port[1]; 5502 r->src.port_op = src_port->op; 5503 r->dst.addr = dst_host->addr; 5504 r->dst.neg = dst_host->not; 5505 r->dst.port[0] = dst_port->port[0]; 5506 r->dst.port[1] = dst_port->port[1]; 5507 r->dst.port_op = dst_port->op; 5508 r->uid.op = uid->op; 5509 r->uid.uid[0] = uid->uid[0]; 5510 r->uid.uid[1] = uid->uid[1]; 5511 r->gid.op = gid->op; 5512 r->gid.gid[0] = gid->gid[0]; 5513 r->gid.gid[1] = gid->gid[1]; 5514 r->type = icmp_type->type; 5515 r->code = icmp_type->code; 5516 5517 if ((keep_state == PF_STATE_MODULATE || 5518 keep_state == PF_STATE_SYNPROXY) && 5519 r->proto && r->proto != IPPROTO_TCP) 5520 r->keep_state = PF_STATE_NORMAL; 5521 else 5522 r->keep_state = keep_state; 5523 5524 if (r->proto && r->proto != IPPROTO_TCP) { 5525 r->flags = 0; 5526 r->flagset = 0; 5527 } else { 5528 r->flags = flags; 5529 r->flagset = flagset; 5530 } 5531 if (icmp_type->proto && r->proto != icmp_type->proto) { 5532 yyerror("icmp-type mismatch"); 5533 error++; 5534 } 5535 5536 if (src_os && src_os->os) { 5537 r->os_fingerprint = pfctl_get_fingerprint(src_os->os); 5538 if ((pf->opts & PF_OPT_VERBOSE2) && 5539 r->os_fingerprint == PF_OSFP_NOMATCH) 5540 fprintf(stderr, 5541 "warning: unknown '%s' OS fingerprint\n", 5542 src_os->os); 5543 } else { 5544 r->os_fingerprint = PF_OSFP_ANY; 5545 } 5546 5547 TAILQ_INIT(&r->rpool.list); 5548 for (h = rpool_hosts; h != NULL; h = h->next) { 5549 pa = calloc(1, sizeof(struct pf_pooladdr)); 5550 if (pa == NULL) 5551 err(1, "expand_rule: calloc"); 5552 pa->addr = h->addr; 5553 if (h->ifname != NULL) { 5554 if (strlcpy(pa->ifname, h->ifname, 5555 sizeof(pa->ifname)) >= 5556 sizeof(pa->ifname)) 5557 errx(1, "expand_rule: strlcpy"); 5558 } else 5559 pa->ifname[0] = 0; 5560 TAILQ_INSERT_TAIL(&r->rpool.list, pa, entries); 5561 } 5562 5563 if (rule_consistent(r, anchor_call[0]) < 0 || error) 5564 yyerror("skipping rule due to errors"); 5565 else { 5566 r->nr = pf->astack[pf->asd]->match++; 5567 pfctl_append_rule(pf, r, anchor_call); 5568 added++; 5569 } 5570 5571 )))))))))); 5572 5573 FREE_LIST(struct node_if, interfaces); 5574 FREE_LIST(struct node_proto, protos); 5575 FREE_LIST(struct node_host, src_hosts); 5576 FREE_LIST(struct node_port, src_ports); 5577 FREE_LIST(struct node_os, src_oses); 5578 FREE_LIST(struct node_host, dst_hosts); 5579 FREE_LIST(struct node_port, dst_ports); 5580 FREE_LIST(struct node_uid, uids); 5581 FREE_LIST(struct node_gid, gids); 5582 FREE_LIST(struct node_icmp, icmp_types); 5583 FREE_LIST(struct node_host, rpool_hosts); 5584 5585 if (!added) 5586 yyerror("rule expands to no valid combination"); 5587 } 5588 5589 int 5590 expand_skip_interface(struct node_if *interfaces) 5591 { 5592 int errs = 0; 5593 5594 if (!interfaces || (!interfaces->next && !interfaces->not && 5595 !strcmp(interfaces->ifname, "none"))) { 5596 if (pf->opts & PF_OPT_VERBOSE) 5597 printf("set skip on none\n"); 5598 errs = pfctl_set_interface_flags(pf, "", PFI_IFLAG_SKIP, 0); 5599 return (errs); 5600 } 5601 5602 if (pf->opts & PF_OPT_VERBOSE) 5603 printf("set skip on {"); 5604 LOOP_THROUGH(struct node_if, interface, interfaces, 5605 if (pf->opts & PF_OPT_VERBOSE) 5606 printf(" %s", interface->ifname); 5607 if (interface->not) { 5608 yyerror("skip on ! <interface> is not supported"); 5609 errs++; 5610 } else 5611 errs += pfctl_set_interface_flags(pf, 5612 interface->ifname, PFI_IFLAG_SKIP, 1); 5613 ); 5614 if (pf->opts & PF_OPT_VERBOSE) 5615 printf(" }\n"); 5616 5617 FREE_LIST(struct node_if, interfaces); 5618 5619 if (errs) 5620 return (1); 5621 else 5622 return (0); 5623 } 5624 5625 #undef FREE_LIST 5626 #undef LOOP_THROUGH 5627 5628 int 5629 check_rulestate(int desired_state) 5630 { 5631 if (require_order && (rulestate > desired_state)) { 5632 yyerror("Rules must be in order: options, normalization, " 5633 "queueing, translation, filtering"); 5634 return (1); 5635 } 5636 rulestate = desired_state; 5637 return (0); 5638 } 5639 5640 int 5641 kw_cmp(const void *k, const void *e) 5642 { 5643 return (strcmp(k, ((const struct keywords *)e)->k_name)); 5644 } 5645 5646 int 5647 lookup(char *s) 5648 { 5649 /* this has to be sorted always */ 5650 static const struct keywords keywords[] = { 5651 { "all", ALL}, 5652 { "allow-opts", ALLOWOPTS}, 5653 { "altq", ALTQ}, 5654 { "anchor", ANCHOR}, 5655 { "antispoof", ANTISPOOF}, 5656 { "any", ANY}, 5657 { "bandwidth", BANDWIDTH}, 5658 { "binat", BINAT}, 5659 { "binat-anchor", BINATANCHOR}, 5660 { "bitmask", BITMASK}, 5661 { "block", BLOCK}, 5662 { "block-policy", BLOCKPOLICY}, 5663 { "buckets", BUCKETS}, 5664 { "cbq", CBQ}, 5665 { "code", CODE}, 5666 { "codelq", CODEL}, 5667 { "crop", FRAGCROP}, 5668 { "debug", DEBUG}, 5669 { "divert-reply", DIVERTREPLY}, 5670 { "divert-to", DIVERTTO}, 5671 { "dnpipe", DNPIPE}, 5672 { "dnqueue", DNQUEUE}, 5673 { "drop", DROP}, 5674 { "drop-ovl", FRAGDROP}, 5675 { "dup-to", DUPTO}, 5676 { "fail-policy", FAILPOLICY}, 5677 { "fairq", FAIRQ}, 5678 { "fastroute", FASTROUTE}, 5679 { "file", FILENAME}, 5680 { "fingerprints", FINGERPRINTS}, 5681 { "flags", FLAGS}, 5682 { "floating", FLOATING}, 5683 { "flush", FLUSH}, 5684 { "for", FOR}, 5685 { "fragment", FRAGMENT}, 5686 { "from", FROM}, 5687 { "global", GLOBAL}, 5688 { "group", GROUP}, 5689 { "hfsc", HFSC}, 5690 { "hogs", HOGS}, 5691 { "hostid", HOSTID}, 5692 { "icmp-type", ICMPTYPE}, 5693 { "icmp6-type", ICMP6TYPE}, 5694 { "if-bound", IFBOUND}, 5695 { "in", IN}, 5696 { "include", INCLUDE}, 5697 { "inet", INET}, 5698 { "inet6", INET6}, 5699 { "interval", INTERVAL}, 5700 { "keep", KEEP}, 5701 { "keepcounters", KEEPCOUNTERS}, 5702 { "label", LABEL}, 5703 { "limit", LIMIT}, 5704 { "linkshare", LINKSHARE}, 5705 { "load", LOAD}, 5706 { "log", LOG}, 5707 { "loginterface", LOGINTERFACE}, 5708 { "map-e-portset", MAPEPORTSET}, 5709 { "match", MATCH}, 5710 { "max", MAXIMUM}, 5711 { "max-mss", MAXMSS}, 5712 { "max-src-conn", MAXSRCCONN}, 5713 { "max-src-conn-rate", MAXSRCCONNRATE}, 5714 { "max-src-nodes", MAXSRCNODES}, 5715 { "max-src-states", MAXSRCSTATES}, 5716 { "min-ttl", MINTTL}, 5717 { "modulate", MODULATE}, 5718 { "nat", NAT}, 5719 { "nat-anchor", NATANCHOR}, 5720 { "no", NO}, 5721 { "no-df", NODF}, 5722 { "no-route", NOROUTE}, 5723 { "no-sync", NOSYNC}, 5724 { "on", ON}, 5725 { "optimization", OPTIMIZATION}, 5726 { "os", OS}, 5727 { "out", OUT}, 5728 { "overload", OVERLOAD}, 5729 { "pass", PASS}, 5730 { "port", PORT}, 5731 { "prio", PRIO}, 5732 { "priority", PRIORITY}, 5733 { "priq", PRIQ}, 5734 { "probability", PROBABILITY}, 5735 { "proto", PROTO}, 5736 { "qlimit", QLIMIT}, 5737 { "queue", QUEUE}, 5738 { "quick", QUICK}, 5739 { "random", RANDOM}, 5740 { "random-id", RANDOMID}, 5741 { "rdr", RDR}, 5742 { "rdr-anchor", RDRANCHOR}, 5743 { "realtime", REALTIME}, 5744 { "reassemble", REASSEMBLE}, 5745 { "reply-to", REPLYTO}, 5746 { "require-order", REQUIREORDER}, 5747 { "return", RETURN}, 5748 { "return-icmp", RETURNICMP}, 5749 { "return-icmp6", RETURNICMP6}, 5750 { "return-rst", RETURNRST}, 5751 { "ridentifier", RIDENTIFIER}, 5752 { "round-robin", ROUNDROBIN}, 5753 { "route", ROUTE}, 5754 { "route-to", ROUTETO}, 5755 { "rtable", RTABLE}, 5756 { "rule", RULE}, 5757 { "ruleset-optimization", RULESET_OPTIMIZATION}, 5758 { "scrub", SCRUB}, 5759 { "set", SET}, 5760 { "set-tos", SETTOS}, 5761 { "skip", SKIP}, 5762 { "sloppy", SLOPPY}, 5763 { "source-hash", SOURCEHASH}, 5764 { "source-track", SOURCETRACK}, 5765 { "state", STATE}, 5766 { "state-defaults", STATEDEFAULTS}, 5767 { "state-policy", STATEPOLICY}, 5768 { "static-port", STATICPORT}, 5769 { "sticky-address", STICKYADDRESS}, 5770 { "syncookies", SYNCOOKIES}, 5771 { "synproxy", SYNPROXY}, 5772 { "table", TABLE}, 5773 { "tag", TAG}, 5774 { "tagged", TAGGED}, 5775 { "target", TARGET}, 5776 { "tbrsize", TBRSIZE}, 5777 { "timeout", TIMEOUT}, 5778 { "to", TO}, 5779 { "tos", TOS}, 5780 { "ttl", TTL}, 5781 { "upperlimit", UPPERLIMIT}, 5782 { "urpf-failed", URPFFAILED}, 5783 { "user", USER}, 5784 }; 5785 const struct keywords *p; 5786 5787 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]), 5788 sizeof(keywords[0]), kw_cmp); 5789 5790 if (p) { 5791 if (debug > 1) 5792 fprintf(stderr, "%s: %d\n", s, p->k_val); 5793 return (p->k_val); 5794 } else { 5795 if (debug > 1) 5796 fprintf(stderr, "string: %s\n", s); 5797 return (STRING); 5798 } 5799 } 5800 5801 #define MAXPUSHBACK 128 5802 5803 static char *parsebuf; 5804 static int parseindex; 5805 static char pushback_buffer[MAXPUSHBACK]; 5806 static int pushback_index = 0; 5807 5808 int 5809 lgetc(int quotec) 5810 { 5811 int c, next; 5812 5813 if (parsebuf) { 5814 /* Read character from the parsebuffer instead of input. */ 5815 if (parseindex >= 0) { 5816 c = parsebuf[parseindex++]; 5817 if (c != '\0') 5818 return (c); 5819 parsebuf = NULL; 5820 } else 5821 parseindex++; 5822 } 5823 5824 if (pushback_index) 5825 return (pushback_buffer[--pushback_index]); 5826 5827 if (quotec) { 5828 if ((c = getc(file->stream)) == EOF) { 5829 yyerror("reached end of file while parsing quoted string"); 5830 if (popfile() == EOF) 5831 return (EOF); 5832 return (quotec); 5833 } 5834 return (c); 5835 } 5836 5837 while ((c = getc(file->stream)) == '\\') { 5838 next = getc(file->stream); 5839 if (next != '\n') { 5840 c = next; 5841 break; 5842 } 5843 yylval.lineno = file->lineno; 5844 file->lineno++; 5845 } 5846 5847 while (c == EOF) { 5848 if (popfile() == EOF) 5849 return (EOF); 5850 c = getc(file->stream); 5851 } 5852 return (c); 5853 } 5854 5855 int 5856 lungetc(int c) 5857 { 5858 if (c == EOF) 5859 return (EOF); 5860 if (parsebuf) { 5861 parseindex--; 5862 if (parseindex >= 0) 5863 return (c); 5864 } 5865 if (pushback_index < MAXPUSHBACK-1) 5866 return (pushback_buffer[pushback_index++] = c); 5867 else 5868 return (EOF); 5869 } 5870 5871 int 5872 findeol(void) 5873 { 5874 int c; 5875 5876 parsebuf = NULL; 5877 5878 /* skip to either EOF or the first real EOL */ 5879 while (1) { 5880 if (pushback_index) 5881 c = pushback_buffer[--pushback_index]; 5882 else 5883 c = lgetc(0); 5884 if (c == '\n') { 5885 file->lineno++; 5886 break; 5887 } 5888 if (c == EOF) 5889 break; 5890 } 5891 return (ERROR); 5892 } 5893 5894 int 5895 yylex(void) 5896 { 5897 char buf[8096]; 5898 char *p, *val; 5899 int quotec, next, c; 5900 int token; 5901 5902 top: 5903 p = buf; 5904 while ((c = lgetc(0)) == ' ' || c == '\t') 5905 ; /* nothing */ 5906 5907 yylval.lineno = file->lineno; 5908 if (c == '#') 5909 while ((c = lgetc(0)) != '\n' && c != EOF) 5910 ; /* nothing */ 5911 if (c == '$' && parsebuf == NULL) { 5912 while (1) { 5913 if ((c = lgetc(0)) == EOF) 5914 return (0); 5915 5916 if (p + 1 >= buf + sizeof(buf) - 1) { 5917 yyerror("string too long"); 5918 return (findeol()); 5919 } 5920 if (isalnum(c) || c == '_') { 5921 *p++ = (char)c; 5922 continue; 5923 } 5924 *p = '\0'; 5925 lungetc(c); 5926 break; 5927 } 5928 val = symget(buf); 5929 if (val == NULL) { 5930 yyerror("macro '%s' not defined", buf); 5931 return (findeol()); 5932 } 5933 parsebuf = val; 5934 parseindex = 0; 5935 goto top; 5936 } 5937 5938 switch (c) { 5939 case '\'': 5940 case '"': 5941 quotec = c; 5942 while (1) { 5943 if ((c = lgetc(quotec)) == EOF) 5944 return (0); 5945 if (c == '\n') { 5946 file->lineno++; 5947 continue; 5948 } else if (c == '\\') { 5949 if ((next = lgetc(quotec)) == EOF) 5950 return (0); 5951 if (next == quotec || c == ' ' || c == '\t') 5952 c = next; 5953 else if (next == '\n') { 5954 file->lineno++; 5955 continue; 5956 } 5957 else 5958 lungetc(next); 5959 } else if (c == quotec) { 5960 *p = '\0'; 5961 break; 5962 } 5963 if (p + 1 >= buf + sizeof(buf) - 1) { 5964 yyerror("string too long"); 5965 return (findeol()); 5966 } 5967 *p++ = (char)c; 5968 } 5969 yylval.v.string = strdup(buf); 5970 if (yylval.v.string == NULL) 5971 err(1, "yylex: strdup"); 5972 return (STRING); 5973 case '<': 5974 next = lgetc(0); 5975 if (next == '>') { 5976 yylval.v.i = PF_OP_XRG; 5977 return (PORTBINARY); 5978 } 5979 lungetc(next); 5980 break; 5981 case '>': 5982 next = lgetc(0); 5983 if (next == '<') { 5984 yylval.v.i = PF_OP_IRG; 5985 return (PORTBINARY); 5986 } 5987 lungetc(next); 5988 break; 5989 case '-': 5990 next = lgetc(0); 5991 if (next == '>') 5992 return (ARROW); 5993 lungetc(next); 5994 break; 5995 } 5996 5997 #define allowed_to_end_number(x) \ 5998 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=') 5999 6000 if (c == '-' || isdigit(c)) { 6001 do { 6002 *p++ = c; 6003 if ((unsigned)(p-buf) >= sizeof(buf)) { 6004 yyerror("string too long"); 6005 return (findeol()); 6006 } 6007 } while ((c = lgetc(0)) != EOF && isdigit(c)); 6008 lungetc(c); 6009 if (p == buf + 1 && buf[0] == '-') 6010 goto nodigits; 6011 if (c == EOF || allowed_to_end_number(c)) { 6012 const char *errstr = NULL; 6013 6014 *p = '\0'; 6015 yylval.v.number = strtonum(buf, LLONG_MIN, 6016 LLONG_MAX, &errstr); 6017 if (errstr) { 6018 yyerror("\"%s\" invalid number: %s", 6019 buf, errstr); 6020 return (findeol()); 6021 } 6022 return (NUMBER); 6023 } else { 6024 nodigits: 6025 while (p > buf + 1) 6026 lungetc(*--p); 6027 c = *--p; 6028 if (c == '-') 6029 return (c); 6030 } 6031 } 6032 6033 #define allowed_in_string(x) \ 6034 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \ 6035 x != '{' && x != '}' && x != '<' && x != '>' && \ 6036 x != '!' && x != '=' && x != '/' && x != '#' && \ 6037 x != ',')) 6038 6039 if (isalnum(c) || c == ':' || c == '_') { 6040 do { 6041 *p++ = c; 6042 if ((unsigned)(p-buf) >= sizeof(buf)) { 6043 yyerror("string too long"); 6044 return (findeol()); 6045 } 6046 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c))); 6047 lungetc(c); 6048 *p = '\0'; 6049 if ((token = lookup(buf)) == STRING) 6050 if ((yylval.v.string = strdup(buf)) == NULL) 6051 err(1, "yylex: strdup"); 6052 return (token); 6053 } 6054 if (c == '\n') { 6055 yylval.lineno = file->lineno; 6056 file->lineno++; 6057 } 6058 if (c == EOF) 6059 return (0); 6060 return (c); 6061 } 6062 6063 int 6064 check_file_secrecy(int fd, const char *fname) 6065 { 6066 struct stat st; 6067 6068 if (fstat(fd, &st)) { 6069 warn("cannot stat %s", fname); 6070 return (-1); 6071 } 6072 if (st.st_uid != 0 && st.st_uid != getuid()) { 6073 warnx("%s: owner not root or current user", fname); 6074 return (-1); 6075 } 6076 if (st.st_mode & (S_IRWXG | S_IRWXO)) { 6077 warnx("%s: group/world readable/writeable", fname); 6078 return (-1); 6079 } 6080 return (0); 6081 } 6082 6083 struct file * 6084 pushfile(const char *name, int secret) 6085 { 6086 struct file *nfile; 6087 6088 if ((nfile = calloc(1, sizeof(struct file))) == NULL || 6089 (nfile->name = strdup(name)) == NULL) { 6090 warn("malloc"); 6091 return (NULL); 6092 } 6093 if (TAILQ_FIRST(&files) == NULL && strcmp(nfile->name, "-") == 0) { 6094 nfile->stream = stdin; 6095 free(nfile->name); 6096 if ((nfile->name = strdup("stdin")) == NULL) { 6097 warn("strdup"); 6098 free(nfile); 6099 return (NULL); 6100 } 6101 } else if ((nfile->stream = fopen(nfile->name, "r")) == NULL) { 6102 warn("%s", nfile->name); 6103 free(nfile->name); 6104 free(nfile); 6105 return (NULL); 6106 } else if (secret && 6107 check_file_secrecy(fileno(nfile->stream), nfile->name)) { 6108 fclose(nfile->stream); 6109 free(nfile->name); 6110 free(nfile); 6111 return (NULL); 6112 } 6113 nfile->lineno = 1; 6114 TAILQ_INSERT_TAIL(&files, nfile, entry); 6115 return (nfile); 6116 } 6117 6118 int 6119 popfile(void) 6120 { 6121 struct file *prev; 6122 6123 if ((prev = TAILQ_PREV(file, files, entry)) != NULL) { 6124 prev->errors += file->errors; 6125 TAILQ_REMOVE(&files, file, entry); 6126 fclose(file->stream); 6127 free(file->name); 6128 free(file); 6129 file = prev; 6130 return (0); 6131 } 6132 return (EOF); 6133 } 6134 6135 int 6136 parse_config(char *filename, struct pfctl *xpf) 6137 { 6138 int errors = 0; 6139 struct sym *sym; 6140 6141 pf = xpf; 6142 errors = 0; 6143 rulestate = PFCTL_STATE_NONE; 6144 returnicmpdefault = (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT; 6145 returnicmp6default = 6146 (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT; 6147 blockpolicy = PFRULE_DROP; 6148 failpolicy = PFRULE_DROP; 6149 require_order = 1; 6150 6151 if ((file = pushfile(filename, 0)) == NULL) { 6152 warn("cannot open the main config file!"); 6153 return (-1); 6154 } 6155 6156 yyparse(); 6157 errors = file->errors; 6158 popfile(); 6159 6160 /* Free macros and check which have not been used. */ 6161 while ((sym = TAILQ_FIRST(&symhead))) { 6162 if ((pf->opts & PF_OPT_VERBOSE2) && !sym->used) 6163 fprintf(stderr, "warning: macro '%s' not " 6164 "used\n", sym->nam); 6165 free(sym->nam); 6166 free(sym->val); 6167 TAILQ_REMOVE(&symhead, sym, entry); 6168 free(sym); 6169 } 6170 6171 return (errors ? -1 : 0); 6172 } 6173 6174 int 6175 symset(const char *nam, const char *val, int persist) 6176 { 6177 struct sym *sym; 6178 6179 for (sym = TAILQ_FIRST(&symhead); sym && strcmp(nam, sym->nam); 6180 sym = TAILQ_NEXT(sym, entry)) 6181 ; /* nothing */ 6182 6183 if (sym != NULL) { 6184 if (sym->persist == 1) 6185 return (0); 6186 else { 6187 free(sym->nam); 6188 free(sym->val); 6189 TAILQ_REMOVE(&symhead, sym, entry); 6190 free(sym); 6191 } 6192 } 6193 if ((sym = calloc(1, sizeof(*sym))) == NULL) 6194 return (-1); 6195 6196 sym->nam = strdup(nam); 6197 if (sym->nam == NULL) { 6198 free(sym); 6199 return (-1); 6200 } 6201 sym->val = strdup(val); 6202 if (sym->val == NULL) { 6203 free(sym->nam); 6204 free(sym); 6205 return (-1); 6206 } 6207 sym->used = 0; 6208 sym->persist = persist; 6209 TAILQ_INSERT_TAIL(&symhead, sym, entry); 6210 return (0); 6211 } 6212 6213 int 6214 pfctl_cmdline_symset(char *s) 6215 { 6216 char *sym, *val; 6217 int ret; 6218 6219 if ((val = strrchr(s, '=')) == NULL) 6220 return (-1); 6221 6222 if ((sym = malloc(strlen(s) - strlen(val) + 1)) == NULL) 6223 err(1, "pfctl_cmdline_symset: malloc"); 6224 6225 strlcpy(sym, s, strlen(s) - strlen(val) + 1); 6226 6227 ret = symset(sym, val + 1, 1); 6228 free(sym); 6229 6230 return (ret); 6231 } 6232 6233 char * 6234 symget(const char *nam) 6235 { 6236 struct sym *sym; 6237 6238 TAILQ_FOREACH(sym, &symhead, entry) 6239 if (strcmp(nam, sym->nam) == 0) { 6240 sym->used = 1; 6241 return (sym->val); 6242 } 6243 return (NULL); 6244 } 6245 6246 void 6247 mv_rules(struct pfctl_ruleset *src, struct pfctl_ruleset *dst) 6248 { 6249 int i; 6250 struct pfctl_rule *r; 6251 6252 for (i = 0; i < PF_RULESET_MAX; ++i) { 6253 while ((r = TAILQ_FIRST(src->rules[i].active.ptr)) 6254 != NULL) { 6255 TAILQ_REMOVE(src->rules[i].active.ptr, r, entries); 6256 TAILQ_INSERT_TAIL(dst->rules[i].active.ptr, r, entries); 6257 dst->anchor->match++; 6258 } 6259 src->anchor->match = 0; 6260 while ((r = TAILQ_FIRST(src->rules[i].inactive.ptr)) 6261 != NULL) { 6262 TAILQ_REMOVE(src->rules[i].inactive.ptr, r, entries); 6263 TAILQ_INSERT_TAIL(dst->rules[i].inactive.ptr, 6264 r, entries); 6265 } 6266 } 6267 } 6268 6269 void 6270 decide_address_family(struct node_host *n, sa_family_t *af) 6271 { 6272 if (*af != 0 || n == NULL) 6273 return; 6274 *af = n->af; 6275 while ((n = n->next) != NULL) { 6276 if (n->af != *af) { 6277 *af = 0; 6278 return; 6279 } 6280 } 6281 } 6282 6283 void 6284 remove_invalid_hosts(struct node_host **nh, sa_family_t *af) 6285 { 6286 struct node_host *n = *nh, *prev = NULL; 6287 6288 while (n != NULL) { 6289 if (*af && n->af && n->af != *af) { 6290 /* unlink and free n */ 6291 struct node_host *next = n->next; 6292 6293 /* adjust tail pointer */ 6294 if (n == (*nh)->tail) 6295 (*nh)->tail = prev; 6296 /* adjust previous node's next pointer */ 6297 if (prev == NULL) 6298 *nh = next; 6299 else 6300 prev->next = next; 6301 /* free node */ 6302 if (n->ifname != NULL) 6303 free(n->ifname); 6304 free(n); 6305 n = next; 6306 } else { 6307 if (n->af && !*af) 6308 *af = n->af; 6309 prev = n; 6310 n = n->next; 6311 } 6312 } 6313 } 6314 6315 int 6316 invalid_redirect(struct node_host *nh, sa_family_t af) 6317 { 6318 if (!af) { 6319 struct node_host *n; 6320 6321 /* tables and dyniftl are ok without an address family */ 6322 for (n = nh; n != NULL; n = n->next) { 6323 if (n->addr.type != PF_ADDR_TABLE && 6324 n->addr.type != PF_ADDR_DYNIFTL) { 6325 yyerror("address family not given and " 6326 "translation address expands to multiple " 6327 "address families"); 6328 return (1); 6329 } 6330 } 6331 } 6332 if (nh == NULL) { 6333 yyerror("no translation address with matching address family " 6334 "found."); 6335 return (1); 6336 } 6337 return (0); 6338 } 6339 6340 int 6341 atoul(char *s, u_long *ulvalp) 6342 { 6343 u_long ulval; 6344 char *ep; 6345 6346 errno = 0; 6347 ulval = strtoul(s, &ep, 0); 6348 if (s[0] == '\0' || *ep != '\0') 6349 return (-1); 6350 if (errno == ERANGE && ulval == ULONG_MAX) 6351 return (-1); 6352 *ulvalp = ulval; 6353 return (0); 6354 } 6355 6356 int 6357 getservice(char *n) 6358 { 6359 struct servent *s; 6360 u_long ulval; 6361 6362 if (atoul(n, &ulval) == 0) { 6363 if (ulval > 65535) { 6364 yyerror("illegal port value %lu", ulval); 6365 return (-1); 6366 } 6367 return (htons(ulval)); 6368 } else { 6369 s = getservbyname(n, "tcp"); 6370 if (s == NULL) 6371 s = getservbyname(n, "udp"); 6372 if (s == NULL) { 6373 yyerror("unknown port %s", n); 6374 return (-1); 6375 } 6376 return (s->s_port); 6377 } 6378 } 6379 6380 int 6381 rule_label(struct pfctl_rule *r, char *s[PF_RULE_MAX_LABEL_COUNT]) 6382 { 6383 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++) { 6384 if (s[i] == NULL) 6385 return (0); 6386 6387 if (strlcpy(r->label[i], s[i], sizeof(r->label[0])) >= 6388 sizeof(r->label[0])) { 6389 yyerror("rule label too long (max %d chars)", 6390 sizeof(r->label[0])-1); 6391 return (-1); 6392 } 6393 } 6394 return (0); 6395 } 6396 6397 u_int16_t 6398 parseicmpspec(char *w, sa_family_t af) 6399 { 6400 const struct icmpcodeent *p; 6401 u_long ulval; 6402 u_int8_t icmptype; 6403 6404 if (af == AF_INET) 6405 icmptype = returnicmpdefault >> 8; 6406 else 6407 icmptype = returnicmp6default >> 8; 6408 6409 if (atoul(w, &ulval) == -1) { 6410 if ((p = geticmpcodebyname(icmptype, w, af)) == NULL) { 6411 yyerror("unknown icmp code %s", w); 6412 return (0); 6413 } 6414 ulval = p->code; 6415 } 6416 if (ulval > 255) { 6417 yyerror("invalid icmp code %lu", ulval); 6418 return (0); 6419 } 6420 return (icmptype << 8 | ulval); 6421 } 6422 6423 int 6424 parseport(char *port, struct range *r, int extensions) 6425 { 6426 char *p = strchr(port, ':'); 6427 6428 if (p == NULL) { 6429 if ((r->a = getservice(port)) == -1) 6430 return (-1); 6431 r->b = 0; 6432 r->t = PF_OP_NONE; 6433 return (0); 6434 } 6435 if ((extensions & PPORT_STAR) && !strcmp(p+1, "*")) { 6436 *p = 0; 6437 if ((r->a = getservice(port)) == -1) 6438 return (-1); 6439 r->b = 0; 6440 r->t = PF_OP_IRG; 6441 return (0); 6442 } 6443 if ((extensions & PPORT_RANGE)) { 6444 *p++ = 0; 6445 if ((r->a = getservice(port)) == -1 || 6446 (r->b = getservice(p)) == -1) 6447 return (-1); 6448 if (r->a == r->b) { 6449 r->b = 0; 6450 r->t = PF_OP_NONE; 6451 } else 6452 r->t = PF_OP_RRG; 6453 return (0); 6454 } 6455 return (-1); 6456 } 6457 6458 int 6459 pfctl_load_anchors(int dev, struct pfctl *pf, struct pfr_buffer *trans) 6460 { 6461 struct loadanchors *la; 6462 6463 TAILQ_FOREACH(la, &loadanchorshead, entries) { 6464 if (pf->opts & PF_OPT_VERBOSE) 6465 fprintf(stderr, "\nLoading anchor %s from %s\n", 6466 la->anchorname, la->filename); 6467 if (pfctl_rules(dev, la->filename, pf->opts, pf->optimize, 6468 la->anchorname, trans) == -1) 6469 return (-1); 6470 } 6471 6472 return (0); 6473 } 6474 6475 int 6476 kw_casecmp(const void *k, const void *e) 6477 { 6478 return (strcasecmp(k, ((const struct keywords *)e)->k_name)); 6479 } 6480 6481 int 6482 map_tos(char *s, int *val) 6483 { 6484 /* DiffServ Codepoints and other TOS mappings */ 6485 const struct keywords toswords[] = { 6486 { "af11", IPTOS_DSCP_AF11 }, 6487 { "af12", IPTOS_DSCP_AF12 }, 6488 { "af13", IPTOS_DSCP_AF13 }, 6489 { "af21", IPTOS_DSCP_AF21 }, 6490 { "af22", IPTOS_DSCP_AF22 }, 6491 { "af23", IPTOS_DSCP_AF23 }, 6492 { "af31", IPTOS_DSCP_AF31 }, 6493 { "af32", IPTOS_DSCP_AF32 }, 6494 { "af33", IPTOS_DSCP_AF33 }, 6495 { "af41", IPTOS_DSCP_AF41 }, 6496 { "af42", IPTOS_DSCP_AF42 }, 6497 { "af43", IPTOS_DSCP_AF43 }, 6498 { "critical", IPTOS_PREC_CRITIC_ECP }, 6499 { "cs0", IPTOS_DSCP_CS0 }, 6500 { "cs1", IPTOS_DSCP_CS1 }, 6501 { "cs2", IPTOS_DSCP_CS2 }, 6502 { "cs3", IPTOS_DSCP_CS3 }, 6503 { "cs4", IPTOS_DSCP_CS4 }, 6504 { "cs5", IPTOS_DSCP_CS5 }, 6505 { "cs6", IPTOS_DSCP_CS6 }, 6506 { "cs7", IPTOS_DSCP_CS7 }, 6507 { "ef", IPTOS_DSCP_EF }, 6508 { "inetcontrol", IPTOS_PREC_INTERNETCONTROL }, 6509 { "lowdelay", IPTOS_LOWDELAY }, 6510 { "netcontrol", IPTOS_PREC_NETCONTROL }, 6511 { "reliability", IPTOS_RELIABILITY }, 6512 { "throughput", IPTOS_THROUGHPUT }, 6513 { "va", IPTOS_DSCP_VA } 6514 }; 6515 const struct keywords *p; 6516 6517 p = bsearch(s, toswords, sizeof(toswords)/sizeof(toswords[0]), 6518 sizeof(toswords[0]), kw_casecmp); 6519 6520 if (p) { 6521 *val = p->k_val; 6522 return (1); 6523 } 6524 return (0); 6525 } 6526 6527 int 6528 rt_tableid_max(void) 6529 { 6530 #ifdef __FreeBSD__ 6531 int fibs; 6532 size_t l = sizeof(fibs); 6533 6534 if (sysctlbyname("net.fibs", &fibs, &l, NULL, 0) == -1) 6535 fibs = 16; /* XXX RT_MAXFIBS, at least limit it some. */ 6536 /* 6537 * As the OpenBSD code only compares > and not >= we need to adjust 6538 * here given we only accept values of 0..n and want to avoid #ifdefs 6539 * in the grammar. 6540 */ 6541 return (fibs - 1); 6542 #else 6543 return (RT_TABLEID_MAX); 6544 #endif 6545 } 6546