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