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