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 }; 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 /* new-style scrub opts */ 308 int nodf; 309 int minttl; 310 int settos; 311 int randomid; 312 int max_mss; 313 } filter_opts; 314 315 static struct antispoof_opts { 316 char *label[PF_RULE_MAX_LABEL_COUNT]; 317 int labelcount; 318 u_int32_t ridentifier; 319 u_int rtableid; 320 } antispoof_opts; 321 322 static struct scrub_opts { 323 int marker; 324 int nodf; 325 int minttl; 326 int maxmss; 327 int settos; 328 int fragcache; 329 int randomid; 330 int reassemble_tcp; 331 char *match_tag; 332 u_int8_t match_tag_not; 333 u_int rtableid; 334 } scrub_opts; 335 336 static struct queue_opts { 337 int marker; 338 #define QOM_BWSPEC 0x01 339 #define QOM_SCHEDULER 0x02 340 #define QOM_PRIORITY 0x04 341 #define QOM_TBRSIZE 0x08 342 #define QOM_QLIMIT 0x10 343 struct node_queue_bw queue_bwspec; 344 struct node_queue_opt scheduler; 345 int priority; 346 unsigned int tbrsize; 347 int qlimit; 348 } queue_opts; 349 350 static struct table_opts { 351 int flags; 352 int init_addr; 353 struct node_tinithead init_nodes; 354 } table_opts; 355 356 static struct codel_opts codel_opts; 357 static struct node_hfsc_opts hfsc_opts; 358 static struct node_fairq_opts fairq_opts; 359 static struct node_state_opt *keep_state_defaults = NULL; 360 static struct pfctl_watermarks syncookie_opts; 361 362 int disallow_table(struct node_host *, const char *); 363 int disallow_urpf_failed(struct node_host *, const char *); 364 int disallow_alias(struct node_host *, const char *); 365 int rule_consistent(struct pfctl_rule *, int); 366 int filter_consistent(struct pfctl_rule *, int); 367 int nat_consistent(struct pfctl_rule *); 368 int rdr_consistent(struct pfctl_rule *); 369 int process_tabledef(char *, struct table_opts *); 370 void expand_label_str(char *, size_t, const char *, const char *); 371 void expand_label_if(const char *, char *, size_t, const char *); 372 void expand_label_addr(const char *, char *, size_t, sa_family_t, 373 struct pf_rule_addr *); 374 void expand_label_port(const char *, char *, size_t, 375 struct pf_rule_addr *); 376 void expand_label_proto(const char *, char *, size_t, u_int8_t); 377 void expand_label_nr(const char *, char *, size_t, 378 struct pfctl_rule *); 379 void expand_eth_rule(struct pfctl_eth_rule *, 380 struct node_if *, struct node_etherproto *, 381 struct node_mac *, struct node_mac *, 382 struct node_host *, struct node_host *, const char *, 383 const char *); 384 void expand_rule(struct pfctl_rule *, struct node_if *, 385 struct redirspec *, struct redirspec *, struct node_host *, 386 struct node_host *, struct node_proto *, struct node_os *, 387 struct node_host *, struct node_port *, struct node_host *, 388 struct node_port *, struct node_uid *, struct node_gid *, 389 struct node_if *, struct node_icmp *, const char *); 390 int expand_altq(struct pf_altq *, struct node_if *, 391 struct node_queue *, struct node_queue_bw bwspec, 392 struct node_queue_opt *); 393 int expand_queue(struct pf_altq *, struct node_if *, 394 struct node_queue *, struct node_queue_bw, 395 struct node_queue_opt *); 396 int expand_skip_interface(struct node_if *); 397 398 int check_rulestate(int); 399 int getservice(char *); 400 int rule_label(struct pfctl_rule *, char *s[PF_RULE_MAX_LABEL_COUNT]); 401 int eth_rule_label(struct pfctl_eth_rule *, char *s[PF_RULE_MAX_LABEL_COUNT]); 402 int rt_tableid_max(void); 403 404 void mv_rules(struct pfctl_ruleset *, struct pfctl_ruleset *); 405 void mv_eth_rules(struct pfctl_eth_ruleset *, struct pfctl_eth_ruleset *); 406 void decide_address_family(struct node_host *, sa_family_t *); 407 void remove_invalid_hosts(struct node_host **, sa_family_t *); 408 int invalid_redirect(struct node_host *, sa_family_t); 409 u_int16_t parseicmpspec(char *, sa_family_t); 410 int kw_casecmp(const void *, const void *); 411 int map_tos(char *string, int *); 412 struct node_mac* node_mac_from_string(const char *); 413 struct node_mac* node_mac_from_string_masklen(const char *, int); 414 struct node_mac* node_mac_from_string_mask(const char *, const char *); 415 416 static TAILQ_HEAD(loadanchorshead, loadanchors) 417 loadanchorshead = TAILQ_HEAD_INITIALIZER(loadanchorshead); 418 419 struct loadanchors { 420 TAILQ_ENTRY(loadanchors) entries; 421 char *anchorname; 422 char *filename; 423 }; 424 425 typedef struct { 426 union { 427 int64_t number; 428 double probability; 429 int i; 430 char *string; 431 u_int rtableid; 432 struct { 433 u_int8_t b1; 434 u_int8_t b2; 435 u_int16_t w; 436 u_int16_t w2; 437 } b; 438 struct range range; 439 struct node_if *interface; 440 struct node_proto *proto; 441 struct node_etherproto *etherproto; 442 struct node_icmp *icmp; 443 struct node_host *host; 444 struct node_os *os; 445 struct node_port *port; 446 struct node_uid *uid; 447 struct node_gid *gid; 448 struct node_state_opt *state_opt; 449 struct peer peer; 450 struct { 451 struct peer src, dst; 452 struct node_os *src_os; 453 } fromto; 454 struct { 455 struct node_mac *src; 456 struct node_mac *dst; 457 } etherfromto; 458 struct node_mac *mac; 459 struct { 460 struct node_mac *mac; 461 } etheraddr; 462 char *bridge_to; 463 struct { 464 struct node_host *host; 465 u_int8_t rt; 466 u_int8_t pool_opts; 467 sa_family_t af; 468 struct pf_poolhashkey *key; 469 } route; 470 struct redirection *redirection; 471 struct { 472 int action; 473 struct node_state_opt *options; 474 } keep_state; 475 struct { 476 u_int8_t log; 477 u_int8_t logif; 478 u_int8_t quick; 479 } logquick; 480 struct { 481 int neg; 482 char *name; 483 } tagged; 484 struct pf_poolhashkey *hashkey; 485 struct node_queue *queue; 486 struct node_queue_opt queue_options; 487 struct node_queue_bw queue_bwspec; 488 struct node_qassign qassign; 489 struct filter_opts filter_opts; 490 struct antispoof_opts antispoof_opts; 491 struct queue_opts queue_opts; 492 struct scrub_opts scrub_opts; 493 struct table_opts table_opts; 494 struct pool_opts pool_opts; 495 struct node_hfsc_opts hfsc_opts; 496 struct node_fairq_opts fairq_opts; 497 struct codel_opts codel_opts; 498 struct pfctl_watermarks *watermarks; 499 } v; 500 int lineno; 501 } YYSTYPE; 502 503 #define PPORT_RANGE 1 504 #define PPORT_STAR 2 505 int parseport(char *, struct range *r, int); 506 507 #define DYNIF_MULTIADDR(addr) ((addr).type == PF_ADDR_DYNIFTL && \ 508 (!((addr).iflags & PFI_AFLAG_NOALIAS) || \ 509 !isdigit((addr).v.ifname[strlen((addr).v.ifname)-1]))) 510 511 %} 512 513 %token PASS BLOCK MATCH SCRUB RETURN IN OS OUT LOG QUICK ON FROM TO FLAGS 514 %token RETURNRST RETURNICMP RETURNICMP6 PROTO INET INET6 ALL ANY ICMPTYPE 515 %token ICMP6TYPE CODE KEEP MODULATE STATE PORT RDR NAT BINAT ARROW NODF 516 %token MINTTL ERROR ALLOWOPTS FASTROUTE FILENAME ROUTETO DUPTO REPLYTO NO LABEL 517 %token NOROUTE URPFFAILED FRAGMENT USER GROUP MAXMSS MAXIMUM TTL TOS DROP TABLE 518 %token REASSEMBLE ANCHOR NATANCHOR RDRANCHOR BINATANCHOR 519 %token SET OPTIMIZATION TIMEOUT LIMIT LOGINTERFACE BLOCKPOLICY FAILPOLICY 520 %token RANDOMID REQUIREORDER SYNPROXY FINGERPRINTS NOSYNC DEBUG SKIP HOSTID 521 %token ANTISPOOF FOR INCLUDE KEEPCOUNTERS SYNCOOKIES L3 MATCHES 522 %token ETHER 523 %token BITMASK RANDOM SOURCEHASH ROUNDROBIN STATICPORT PROBABILITY MAPEPORTSET 524 %token ALTQ CBQ CODEL PRIQ HFSC FAIRQ BANDWIDTH TBRSIZE LINKSHARE REALTIME 525 %token UPPERLIMIT QUEUE PRIORITY QLIMIT HOGS BUCKETS RTABLE TARGET INTERVAL 526 %token DNPIPE DNQUEUE RIDENTIFIER 527 %token LOAD RULESET_OPTIMIZATION PRIO 528 %token STICKYADDRESS ENDPI MAXSRCSTATES MAXSRCNODES SOURCETRACK GLOBAL RULE 529 %token MAXSRCCONN MAXSRCCONNRATE OVERLOAD FLUSH SLOPPY PFLOW 530 %token TAGGED TAG IFBOUND FLOATING STATEPOLICY STATEDEFAULTS ROUTE SETTOS 531 %token DIVERTTO DIVERTREPLY BRIDGE_TO RECEIVEDON NE LE GE AFTO 532 %token <v.string> STRING 533 %token <v.number> NUMBER 534 %token <v.i> PORTBINARY 535 %type <v.interface> interface if_list if_item_not if_item 536 %type <v.number> number icmptype icmp6type uid gid 537 %type <v.number> tos not yesno optnodf 538 %type <v.probability> probability 539 %type <v.i> no dir af fragcache optimizer syncookie_val 540 %type <v.i> sourcetrack flush unaryop statelock 541 %type <v.i> etherprotoval 542 %type <v.b> action nataction natpasslog scrubaction 543 %type <v.b> flags flag blockspec prio 544 %type <v.range> portplain portstar portrange 545 %type <v.hashkey> hashkey 546 %type <v.proto> proto proto_list proto_item 547 %type <v.number> protoval 548 %type <v.icmp> icmpspec 549 %type <v.icmp> icmp_list icmp_item 550 %type <v.icmp> icmp6_list icmp6_item 551 %type <v.number> reticmpspec reticmp6spec 552 %type <v.fromto> fromto l3fromto 553 %type <v.peer> ipportspec from to 554 %type <v.host> ipspec toipspec xhost host dynaddr host_list 555 %type <v.host> redir_host_list redirspec 556 %type <v.host> route_host route_host_list routespec 557 %type <v.os> os xos os_list 558 %type <v.port> portspec port_list port_item 559 %type <v.uid> uids uid_list uid_item 560 %type <v.gid> gids gid_list gid_item 561 %type <v.route> route 562 %type <v.redirection> redirection redirpool 563 %type <v.string> label stringall tag anchorname 564 %type <v.string> string varstring numberstring 565 %type <v.keep_state> keep 566 %type <v.state_opt> state_opt_spec state_opt_list state_opt_item 567 %type <v.logquick> logquick quick log logopts logopt 568 %type <v.interface> antispoof_ifspc antispoof_iflst antispoof_if 569 %type <v.qassign> qname etherqname 570 %type <v.queue> qassign qassign_list qassign_item 571 %type <v.queue_options> scheduler 572 %type <v.number> cbqflags_list cbqflags_item 573 %type <v.number> priqflags_list priqflags_item 574 %type <v.hfsc_opts> hfscopts_list hfscopts_item hfsc_opts 575 %type <v.fairq_opts> fairqopts_list fairqopts_item fairq_opts 576 %type <v.codel_opts> codelopts_list codelopts_item codel_opts 577 %type <v.queue_bwspec> bandwidth 578 %type <v.filter_opts> filter_opts filter_opt filter_opts_l etherfilter_opts etherfilter_opt etherfilter_opts_l 579 %type <v.filter_opts> filter_sets filter_set filter_sets_l 580 %type <v.antispoof_opts> antispoof_opts antispoof_opt antispoof_opts_l 581 %type <v.queue_opts> queue_opts queue_opt queue_opts_l 582 %type <v.scrub_opts> scrub_opts scrub_opt scrub_opts_l 583 %type <v.table_opts> table_opts table_opt table_opts_l 584 %type <v.pool_opts> pool_opts pool_opt pool_opts_l 585 %type <v.tagged> tagged 586 %type <v.rtableid> rtable 587 %type <v.watermarks> syncookie_opts 588 %type <v.etherproto> etherproto etherproto_list etherproto_item 589 %type <v.etherfromto> etherfromto 590 %type <v.etheraddr> etherfrom etherto 591 %type <v.bridge_to> bridge 592 %type <v.mac> xmac mac mac_list macspec 593 %% 594 595 ruleset : /* empty */ 596 | ruleset include '\n' 597 | ruleset '\n' 598 | ruleset option '\n' 599 | ruleset etherrule '\n' 600 | ruleset etheranchorrule '\n' 601 | ruleset scrubrule '\n' 602 | ruleset natrule '\n' 603 | ruleset binatrule '\n' 604 | ruleset pfrule '\n' 605 | ruleset anchorrule '\n' 606 | ruleset loadrule '\n' 607 | ruleset altqif '\n' 608 | ruleset queuespec '\n' 609 | ruleset varset '\n' 610 | ruleset antispoof '\n' 611 | ruleset tabledef '\n' 612 | '{' fakeanchor '}' '\n'; 613 | ruleset error '\n' { file->errors++; } 614 ; 615 616 include : INCLUDE STRING { 617 struct file *nfile; 618 619 if ((nfile = pushfile($2, 0)) == NULL) { 620 yyerror("failed to include file %s", $2); 621 free($2); 622 YYERROR; 623 } 624 free($2); 625 626 file = nfile; 627 lungetc('\n'); 628 } 629 ; 630 631 /* 632 * apply to previouslys specified rule: must be careful to note 633 * what that is: pf or nat or binat or rdr 634 */ 635 fakeanchor : fakeanchor '\n' 636 | fakeanchor anchorrule '\n' 637 | fakeanchor binatrule '\n' 638 | fakeanchor natrule '\n' 639 | fakeanchor pfrule '\n' 640 | fakeanchor error '\n' 641 ; 642 643 optimizer : string { 644 if (!strcmp($1, "none")) 645 $$ = 0; 646 else if (!strcmp($1, "basic")) 647 $$ = PF_OPTIMIZE_BASIC; 648 else if (!strcmp($1, "profile")) 649 $$ = PF_OPTIMIZE_BASIC | PF_OPTIMIZE_PROFILE; 650 else { 651 yyerror("unknown ruleset-optimization %s", $1); 652 YYERROR; 653 } 654 } 655 ; 656 657 optnodf : /* empty */ { $$ = 0; } 658 | NODF { $$ = 1; } 659 ; 660 661 option : SET REASSEMBLE yesno optnodf { 662 if (check_rulestate(PFCTL_STATE_OPTION)) 663 YYERROR; 664 pfctl_set_reassembly(pf, $3, $4); 665 } 666 | SET OPTIMIZATION STRING { 667 if (check_rulestate(PFCTL_STATE_OPTION)) { 668 free($3); 669 YYERROR; 670 } 671 if (pfctl_set_optimization(pf, $3) != 0) { 672 yyerror("unknown optimization %s", $3); 673 free($3); 674 YYERROR; 675 } 676 free($3); 677 } 678 | SET RULESET_OPTIMIZATION optimizer { 679 if (!(pf->opts & PF_OPT_OPTIMIZE)) { 680 pf->opts |= PF_OPT_OPTIMIZE; 681 pf->optimize = $3; 682 } 683 } 684 | SET TIMEOUT timeout_spec 685 | SET TIMEOUT '{' optnl timeout_list '}' 686 | SET LIMIT limit_spec 687 | SET LIMIT '{' optnl limit_list '}' 688 | SET LOGINTERFACE stringall { 689 if (check_rulestate(PFCTL_STATE_OPTION)) { 690 free($3); 691 YYERROR; 692 } 693 if (pfctl_set_logif(pf, $3) != 0) { 694 yyerror("error setting loginterface %s", $3); 695 free($3); 696 YYERROR; 697 } 698 free($3); 699 } 700 | SET HOSTID number { 701 if ($3 == 0 || $3 > UINT_MAX) { 702 yyerror("hostid must be non-zero"); 703 YYERROR; 704 } 705 pfctl_set_hostid(pf, $3); 706 } 707 | SET BLOCKPOLICY DROP { 708 if (pf->opts & PF_OPT_VERBOSE) 709 printf("set block-policy drop\n"); 710 if (check_rulestate(PFCTL_STATE_OPTION)) 711 YYERROR; 712 blockpolicy = PFRULE_DROP; 713 } 714 | SET BLOCKPOLICY RETURN { 715 if (pf->opts & PF_OPT_VERBOSE) 716 printf("set block-policy return\n"); 717 if (check_rulestate(PFCTL_STATE_OPTION)) 718 YYERROR; 719 blockpolicy = PFRULE_RETURN; 720 } 721 | SET FAILPOLICY DROP { 722 if (pf->opts & PF_OPT_VERBOSE) 723 printf("set fail-policy drop\n"); 724 if (check_rulestate(PFCTL_STATE_OPTION)) 725 YYERROR; 726 failpolicy = PFRULE_DROP; 727 } 728 | SET FAILPOLICY RETURN { 729 if (pf->opts & PF_OPT_VERBOSE) 730 printf("set fail-policy return\n"); 731 if (check_rulestate(PFCTL_STATE_OPTION)) 732 YYERROR; 733 failpolicy = PFRULE_RETURN; 734 } 735 | SET REQUIREORDER yesno { 736 if (pf->opts & PF_OPT_VERBOSE) 737 printf("set require-order %s\n", 738 $3 == 1 ? "yes" : "no"); 739 require_order = $3; 740 } 741 | SET FINGERPRINTS STRING { 742 if (pf->opts & PF_OPT_VERBOSE) 743 printf("set fingerprints \"%s\"\n", $3); 744 if (check_rulestate(PFCTL_STATE_OPTION)) { 745 free($3); 746 YYERROR; 747 } 748 if (!pf->anchor->name[0]) { 749 if (pfctl_file_fingerprints(pf->dev, 750 pf->opts, $3)) { 751 yyerror("error loading " 752 "fingerprints %s", $3); 753 free($3); 754 YYERROR; 755 } 756 } 757 free($3); 758 } 759 | SET STATEPOLICY statelock { 760 if (pf->opts & PF_OPT_VERBOSE) 761 switch ($3) { 762 case 0: 763 printf("set state-policy floating\n"); 764 break; 765 case PFRULE_IFBOUND: 766 printf("set state-policy if-bound\n"); 767 break; 768 } 769 default_statelock = $3; 770 } 771 | SET DEBUG STRING { 772 if (check_rulestate(PFCTL_STATE_OPTION)) { 773 free($3); 774 YYERROR; 775 } 776 if (pfctl_do_set_debug(pf, $3) != 0) { 777 yyerror("error setting debuglevel %s", $3); 778 free($3); 779 YYERROR; 780 } 781 free($3); 782 } 783 | SET SKIP interface { 784 if (expand_skip_interface($3) != 0) { 785 yyerror("error setting skip interface(s)"); 786 YYERROR; 787 } 788 } 789 | SET STATEDEFAULTS state_opt_list { 790 if (keep_state_defaults != NULL) { 791 yyerror("cannot redefine state-defaults"); 792 YYERROR; 793 } 794 keep_state_defaults = $3; 795 } 796 | SET KEEPCOUNTERS { 797 pf->keep_counters = true; 798 } 799 | SET SYNCOOKIES syncookie_val syncookie_opts { 800 if (pfctl_cfg_syncookies(pf, $3, $4)) { 801 yyerror("error setting syncookies"); 802 YYERROR; 803 } 804 } 805 ; 806 807 syncookie_val : STRING { 808 if (!strcmp($1, "never")) 809 $$ = PFCTL_SYNCOOKIES_NEVER; 810 else if (!strcmp($1, "adaptive")) 811 $$ = PFCTL_SYNCOOKIES_ADAPTIVE; 812 else if (!strcmp($1, "always")) 813 $$ = PFCTL_SYNCOOKIES_ALWAYS; 814 else { 815 yyerror("illegal value for syncookies"); 816 YYERROR; 817 } 818 } 819 ; 820 syncookie_opts : /* empty */ { $$ = NULL; } 821 | { 822 memset(&syncookie_opts, 0, sizeof(syncookie_opts)); 823 } '(' syncookie_opt_l ')' { $$ = &syncookie_opts; } 824 ; 825 826 syncookie_opt_l : syncookie_opt_l comma syncookie_opt 827 | syncookie_opt 828 ; 829 830 syncookie_opt : STRING STRING { 831 double val; 832 char *cp; 833 834 val = strtod($2, &cp); 835 if (cp == NULL || strcmp(cp, "%")) 836 YYERROR; 837 if (val <= 0 || val > 100) { 838 yyerror("illegal percentage value"); 839 YYERROR; 840 } 841 if (!strcmp($1, "start")) { 842 syncookie_opts.hi = val; 843 } else if (!strcmp($1, "end")) { 844 syncookie_opts.lo = val; 845 } else { 846 yyerror("illegal syncookie option"); 847 YYERROR; 848 } 849 } 850 ; 851 852 stringall : STRING { $$ = $1; } 853 | ALL { 854 if (($$ = strdup("all")) == NULL) { 855 err(1, "stringall: strdup"); 856 } 857 } 858 ; 859 860 string : STRING string { 861 if (asprintf(&$$, "%s %s", $1, $2) == -1) 862 err(1, "string: asprintf"); 863 free($1); 864 free($2); 865 } 866 | STRING 867 ; 868 869 varstring : numberstring varstring { 870 if (asprintf(&$$, "%s %s", $1, $2) == -1) 871 err(1, "string: asprintf"); 872 free($1); 873 free($2); 874 } 875 | numberstring 876 ; 877 878 numberstring : NUMBER { 879 char *s; 880 if (asprintf(&s, "%lld", (long long)$1) == -1) { 881 yyerror("string: asprintf"); 882 YYERROR; 883 } 884 $$ = s; 885 } 886 | STRING 887 ; 888 889 varset : STRING '=' varstring { 890 char *s = $1; 891 if (pf->opts & PF_OPT_VERBOSE) 892 printf("%s = \"%s\"\n", $1, $3); 893 while (*s++) { 894 if (isspace((unsigned char)*s)) { 895 yyerror("macro name cannot contain " 896 "whitespace"); 897 YYERROR; 898 } 899 } 900 if (symset($1, $3, 0) == -1) 901 err(1, "cannot store variable %s", $1); 902 free($1); 903 free($3); 904 } 905 ; 906 907 anchorname : STRING { $$ = $1; } 908 | /* empty */ { $$ = NULL; } 909 ; 910 911 pfa_anchorlist : /* empty */ 912 | pfa_anchorlist '\n' 913 | pfa_anchorlist pfrule '\n' 914 | pfa_anchorlist anchorrule '\n' 915 ; 916 917 pfa_anchor : '{' 918 { 919 char ta[PF_ANCHOR_NAME_SIZE]; 920 struct pfctl_ruleset *rs; 921 922 /* stepping into a brace anchor */ 923 pf->asd++; 924 pf->bn++; 925 926 /* 927 * Anchor contents are parsed before the anchor rule 928 * production completes, so we don't know the real 929 * location yet. Create a holding ruleset in the root; 930 * contents will be moved afterwards. 931 */ 932 snprintf(ta, PF_ANCHOR_NAME_SIZE, "_%d", pf->bn); 933 rs = pf_find_or_create_ruleset(ta); 934 if (rs == NULL) 935 err(1, "pfa_anchor: pf_find_or_create_ruleset"); 936 pf->astack[pf->asd] = rs->anchor; 937 pf->anchor = rs->anchor; 938 } '\n' pfa_anchorlist '}' 939 { 940 pf->alast = pf->anchor; 941 pf->asd--; 942 pf->anchor = pf->astack[pf->asd]; 943 } 944 | /* empty */ 945 ; 946 947 anchorrule : ANCHOR anchorname dir quick interface af proto fromto 948 filter_opts pfa_anchor 949 { 950 struct pfctl_rule r; 951 struct node_proto *proto; 952 953 if (check_rulestate(PFCTL_STATE_FILTER)) { 954 if ($2) 955 free($2); 956 YYERROR; 957 } 958 959 if ($2 && ($2[0] == '_' || strstr($2, "/_") != NULL)) { 960 free($2); 961 yyerror("anchor names beginning with '_' " 962 "are reserved for internal use"); 963 YYERROR; 964 } 965 966 memset(&r, 0, sizeof(r)); 967 if (pf->astack[pf->asd + 1]) { 968 if ($2 && strchr($2, '/') != NULL) { 969 free($2); 970 yyerror("anchor paths containing '/' " 971 "cannot be used for inline anchors."); 972 YYERROR; 973 } 974 975 /* Move inline rules into relative location. */ 976 pfctl_anchor_setup(&r, 977 &pf->astack[pf->asd]->ruleset, 978 $2 ? $2 : pf->alast->name); 979 980 if (r.anchor == NULL) 981 err(1, "anchorrule: unable to " 982 "create ruleset"); 983 984 if (pf->alast != r.anchor) { 985 if (r.anchor->match) { 986 yyerror("inline anchor '%s' " 987 "already exists", 988 r.anchor->name); 989 YYERROR; 990 } 991 mv_rules(&pf->alast->ruleset, 992 &r.anchor->ruleset); 993 } 994 pf_remove_if_empty_ruleset(&pf->alast->ruleset); 995 pf->alast = r.anchor; 996 } else { 997 if (!$2) { 998 yyerror("anchors without explicit " 999 "rules must specify a name"); 1000 YYERROR; 1001 } 1002 } 1003 r.direction = $3; 1004 r.quick = $4.quick; 1005 r.af = $6; 1006 r.prob = $9.prob; 1007 r.rtableid = $9.rtableid; 1008 r.ridentifier = $9.ridentifier; 1009 1010 if ($9.tag) 1011 if (strlcpy(r.tagname, $9.tag, 1012 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) { 1013 yyerror("tag too long, max %u chars", 1014 PF_TAG_NAME_SIZE - 1); 1015 YYERROR; 1016 } 1017 if ($9.match_tag) 1018 if (strlcpy(r.match_tagname, $9.match_tag, 1019 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) { 1020 yyerror("tag too long, max %u chars", 1021 PF_TAG_NAME_SIZE - 1); 1022 YYERROR; 1023 } 1024 r.match_tag_not = $9.match_tag_not; 1025 if (rule_label(&r, $9.label)) 1026 YYERROR; 1027 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++) 1028 free($9.label[i]); 1029 r.flags = $9.flags.b1; 1030 r.flagset = $9.flags.b2; 1031 if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) { 1032 yyerror("flags always false"); 1033 YYERROR; 1034 } 1035 if ($9.flags.b1 || $9.flags.b2 || $8.src_os) { 1036 for (proto = $7; proto != NULL && 1037 proto->proto != IPPROTO_TCP; 1038 proto = proto->next) 1039 ; /* nothing */ 1040 if (proto == NULL && $7 != NULL) { 1041 if ($9.flags.b1 || $9.flags.b2) 1042 yyerror( 1043 "flags only apply to tcp"); 1044 if ($8.src_os) 1045 yyerror( 1046 "OS fingerprinting only " 1047 "applies to tcp"); 1048 YYERROR; 1049 } 1050 } 1051 1052 r.tos = $9.tos; 1053 1054 if ($9.keep.action) { 1055 yyerror("cannot specify state handling " 1056 "on anchors"); 1057 YYERROR; 1058 } 1059 1060 if ($9.match_tag) 1061 if (strlcpy(r.match_tagname, $9.match_tag, 1062 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) { 1063 yyerror("tag too long, max %u chars", 1064 PF_TAG_NAME_SIZE - 1); 1065 YYERROR; 1066 } 1067 r.match_tag_not = $9.match_tag_not; 1068 if ($9.marker & FOM_PRIO) { 1069 if ($9.prio == 0) 1070 r.prio = PF_PRIO_ZERO; 1071 else 1072 r.prio = $9.prio; 1073 } 1074 if ($9.marker & FOM_SETPRIO) { 1075 r.set_prio[0] = $9.set_prio[0]; 1076 r.set_prio[1] = $9.set_prio[1]; 1077 r.scrub_flags |= PFSTATE_SETPRIO; 1078 } 1079 1080 decide_address_family($8.src.host, &r.af); 1081 decide_address_family($8.dst.host, &r.af); 1082 1083 expand_rule(&r, $5, NULL, NULL, NULL, NULL, $7, $8.src_os, 1084 $8.src.host, $8.src.port, $8.dst.host, $8.dst.port, 1085 $9.uid, $9.gid, $9.rcv, $9.icmpspec, 1086 pf->astack[pf->asd + 1] ? pf->alast->name : $2); 1087 free($2); 1088 pf->astack[pf->asd + 1] = NULL; 1089 } 1090 | NATANCHOR string interface af proto fromto rtable { 1091 struct pfctl_rule r; 1092 1093 if (check_rulestate(PFCTL_STATE_NAT)) { 1094 free($2); 1095 YYERROR; 1096 } 1097 1098 memset(&r, 0, sizeof(r)); 1099 r.action = PF_NAT; 1100 r.af = $4; 1101 r.rtableid = $7; 1102 1103 decide_address_family($6.src.host, &r.af); 1104 decide_address_family($6.dst.host, &r.af); 1105 1106 expand_rule(&r, $3, NULL, NULL, NULL, NULL, $5, $6.src_os, 1107 $6.src.host, $6.src.port, $6.dst.host, $6.dst.port, 1108 0, 0, 0, 0, $2); 1109 free($2); 1110 } 1111 | RDRANCHOR string interface af proto fromto rtable { 1112 struct pfctl_rule r; 1113 1114 if (check_rulestate(PFCTL_STATE_NAT)) { 1115 free($2); 1116 YYERROR; 1117 } 1118 1119 memset(&r, 0, sizeof(r)); 1120 r.action = PF_RDR; 1121 r.af = $4; 1122 r.rtableid = $7; 1123 1124 decide_address_family($6.src.host, &r.af); 1125 decide_address_family($6.dst.host, &r.af); 1126 1127 if ($6.src.port != NULL) { 1128 yyerror("source port parameter not supported" 1129 " in rdr-anchor"); 1130 YYERROR; 1131 } 1132 if ($6.dst.port != NULL) { 1133 if ($6.dst.port->next != NULL) { 1134 yyerror("destination port list " 1135 "expansion not supported in " 1136 "rdr-anchor"); 1137 YYERROR; 1138 } else if ($6.dst.port->op != PF_OP_EQ) { 1139 yyerror("destination port operators" 1140 " not supported in rdr-anchor"); 1141 YYERROR; 1142 } 1143 r.dst.port[0] = $6.dst.port->port[0]; 1144 r.dst.port[1] = $6.dst.port->port[1]; 1145 r.dst.port_op = $6.dst.port->op; 1146 } 1147 1148 expand_rule(&r, $3, NULL, NULL, NULL, NULL, $5, $6.src_os, 1149 $6.src.host, $6.src.port, $6.dst.host, $6.dst.port, 1150 0, 0, 0, 0, $2); 1151 free($2); 1152 } 1153 | BINATANCHOR string interface af proto fromto rtable { 1154 struct pfctl_rule r; 1155 1156 if (check_rulestate(PFCTL_STATE_NAT)) { 1157 free($2); 1158 YYERROR; 1159 } 1160 1161 memset(&r, 0, sizeof(r)); 1162 r.action = PF_BINAT; 1163 r.af = $4; 1164 r.rtableid = $7; 1165 if ($5 != NULL) { 1166 if ($5->next != NULL) { 1167 yyerror("proto list expansion" 1168 " not supported in binat-anchor"); 1169 YYERROR; 1170 } 1171 r.proto = $5->proto; 1172 free($5); 1173 } 1174 1175 if ($6.src.host != NULL || $6.src.port != NULL || 1176 $6.dst.host != NULL || $6.dst.port != NULL) { 1177 yyerror("fromto parameter not supported" 1178 " in binat-anchor"); 1179 YYERROR; 1180 } 1181 1182 decide_address_family($6.src.host, &r.af); 1183 decide_address_family($6.dst.host, &r.af); 1184 1185 pfctl_append_rule(pf, &r, $2); 1186 free($2); 1187 } 1188 ; 1189 1190 loadrule : LOAD ANCHOR string FROM string { 1191 struct loadanchors *loadanchor; 1192 1193 if (strlen(pf->anchor->name) + 1 + 1194 strlen($3) >= MAXPATHLEN) { 1195 yyerror("anchorname %s too long, max %u\n", 1196 $3, MAXPATHLEN - 1); 1197 free($3); 1198 YYERROR; 1199 } 1200 loadanchor = calloc(1, sizeof(struct loadanchors)); 1201 if (loadanchor == NULL) 1202 err(1, "loadrule: calloc"); 1203 if ((loadanchor->anchorname = malloc(MAXPATHLEN)) == 1204 NULL) 1205 err(1, "loadrule: malloc"); 1206 if (pf->anchor->name[0]) 1207 snprintf(loadanchor->anchorname, MAXPATHLEN, 1208 "%s/%s", pf->anchor->name, $3); 1209 else 1210 strlcpy(loadanchor->anchorname, $3, MAXPATHLEN); 1211 if ((loadanchor->filename = strdup($5)) == NULL) 1212 err(1, "loadrule: strdup"); 1213 1214 TAILQ_INSERT_TAIL(&loadanchorshead, loadanchor, 1215 entries); 1216 1217 free($3); 1218 free($5); 1219 }; 1220 1221 scrubaction : no SCRUB { 1222 $$.b2 = $$.w = 0; 1223 if ($1) 1224 $$.b1 = PF_NOSCRUB; 1225 else 1226 $$.b1 = PF_SCRUB; 1227 } 1228 ; 1229 1230 etherrule : ETHER action dir quick interface bridge etherproto etherfromto l3fromto etherfilter_opts 1231 { 1232 struct pfctl_eth_rule r; 1233 1234 bzero(&r, sizeof(r)); 1235 1236 if (check_rulestate(PFCTL_STATE_ETHER)) 1237 YYERROR; 1238 1239 r.action = $2.b1; 1240 r.direction = $3; 1241 r.quick = $4.quick; 1242 if ($10.tag != NULL) 1243 strlcpy(r.tagname, $10.tag, sizeof(r.tagname)); 1244 if ($10.match_tag) 1245 if (strlcpy(r.match_tagname, $10.match_tag, 1246 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) { 1247 yyerror("tag too long, max %u chars", 1248 PF_TAG_NAME_SIZE - 1); 1249 YYERROR; 1250 } 1251 r.match_tag_not = $10.match_tag_not; 1252 if ($10.queues.qname != NULL) 1253 strlcpy(r.qname, $10.queues.qname, sizeof(r.qname)); 1254 r.dnpipe = $10.dnpipe; 1255 r.dnflags = $10.free_flags; 1256 if (eth_rule_label(&r, $10.label)) 1257 YYERROR; 1258 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++) 1259 free($10.label[i]); 1260 r.ridentifier = $10.ridentifier; 1261 1262 expand_eth_rule(&r, $5, $7, $8.src, $8.dst, 1263 $9.src.host, $9.dst.host, $6, ""); 1264 } 1265 ; 1266 1267 etherpfa_anchorlist : /* empty */ 1268 | etherpfa_anchorlist '\n' 1269 | etherpfa_anchorlist etherrule '\n' 1270 | etherpfa_anchorlist etheranchorrule '\n' 1271 ; 1272 1273 etherpfa_anchor : '{' 1274 { 1275 char ta[PF_ANCHOR_NAME_SIZE]; 1276 struct pfctl_eth_ruleset *rs; 1277 1278 /* steping into a brace anchor */ 1279 pf->asd++; 1280 pf->bn++; 1281 1282 /* create a holding ruleset in the root */ 1283 snprintf(ta, PF_ANCHOR_NAME_SIZE, "_%d", pf->bn); 1284 rs = pf_find_or_create_eth_ruleset(ta); 1285 if (rs == NULL) 1286 err(1, "etherpfa_anchor: pf_find_or_create_eth_ruleset"); 1287 pf->eastack[pf->asd] = rs->anchor; 1288 pf->eanchor = rs->anchor; 1289 } '\n' etherpfa_anchorlist '}' 1290 { 1291 pf->ealast = pf->eanchor; 1292 pf->asd--; 1293 pf->eanchor = pf->eastack[pf->asd]; 1294 } 1295 | /* empty */ 1296 ; 1297 1298 etheranchorrule : ETHER ANCHOR anchorname dir quick interface etherproto etherfromto l3fromto etherpfa_anchor 1299 { 1300 struct pfctl_eth_rule r; 1301 1302 if (check_rulestate(PFCTL_STATE_ETHER)) { 1303 free($3); 1304 YYERROR; 1305 } 1306 1307 if ($3 && ($3[0] == '_' || strstr($3, "/_") != NULL)) { 1308 free($3); 1309 yyerror("anchor names beginning with '_' " 1310 "are reserved for internal use"); 1311 YYERROR; 1312 } 1313 1314 memset(&r, 0, sizeof(r)); 1315 if (pf->eastack[pf->asd + 1]) { 1316 if ($3 && strchr($3, '/') != NULL) { 1317 free($3); 1318 yyerror("anchor paths containing '/' " 1319 "cannot be used for inline anchors."); 1320 YYERROR; 1321 } 1322 1323 /* Move inline rules into relative location. */ 1324 pfctl_eth_anchor_setup(pf, &r, 1325 &pf->eastack[pf->asd]->ruleset, 1326 $3 ? $3 : pf->ealast->name); 1327 if (r.anchor == NULL) 1328 err(1, "etheranchorrule: unable to " 1329 "create ruleset"); 1330 1331 if (pf->ealast != r.anchor) { 1332 if (r.anchor->match) { 1333 yyerror("inline anchor '%s' " 1334 "already exists", 1335 r.anchor->name); 1336 YYERROR; 1337 } 1338 mv_eth_rules(&pf->ealast->ruleset, 1339 &r.anchor->ruleset); 1340 } 1341 pf_remove_if_empty_eth_ruleset(&pf->ealast->ruleset); 1342 pf->ealast = r.anchor; 1343 } else { 1344 if (!$3) { 1345 yyerror("anchors without explicit " 1346 "rules must specify a name"); 1347 YYERROR; 1348 } 1349 } 1350 1351 r.direction = $4; 1352 r.quick = $5.quick; 1353 1354 expand_eth_rule(&r, $6, $7, $8.src, $8.dst, 1355 $9.src.host, $9.dst.host, NULL, 1356 pf->eastack[pf->asd + 1] ? pf->ealast->name : $3); 1357 1358 free($3); 1359 pf->eastack[pf->asd + 1] = NULL; 1360 } 1361 ; 1362 1363 etherfilter_opts : { 1364 bzero(&filter_opts, sizeof filter_opts); 1365 } 1366 etherfilter_opts_l 1367 { $$ = filter_opts; } 1368 | /* empty */ { 1369 bzero(&filter_opts, sizeof filter_opts); 1370 $$ = filter_opts; 1371 } 1372 ; 1373 1374 etherfilter_opts_l : etherfilter_opts_l etherfilter_opt 1375 | etherfilter_opt 1376 1377 etherfilter_opt : etherqname { 1378 if (filter_opts.queues.qname) { 1379 yyerror("queue cannot be redefined"); 1380 YYERROR; 1381 } 1382 filter_opts.queues = $1; 1383 } 1384 | RIDENTIFIER number { 1385 filter_opts.ridentifier = $2; 1386 } 1387 | label { 1388 if (filter_opts.labelcount >= PF_RULE_MAX_LABEL_COUNT) { 1389 yyerror("label can only be used %d times", PF_RULE_MAX_LABEL_COUNT); 1390 YYERROR; 1391 } 1392 filter_opts.label[filter_opts.labelcount++] = $1; 1393 } 1394 | TAG string { 1395 filter_opts.tag = $2; 1396 } 1397 | not TAGGED string { 1398 filter_opts.match_tag = $3; 1399 filter_opts.match_tag_not = $1; 1400 } 1401 | DNPIPE number { 1402 filter_opts.dnpipe = $2; 1403 filter_opts.free_flags |= PFRULE_DN_IS_PIPE; 1404 } 1405 | DNQUEUE number { 1406 filter_opts.dnpipe = $2; 1407 filter_opts.free_flags |= PFRULE_DN_IS_QUEUE; 1408 } 1409 ; 1410 1411 bridge : /* empty */ { 1412 $$ = NULL; 1413 } 1414 | BRIDGE_TO STRING { 1415 $$ = strdup($2); 1416 } 1417 ; 1418 1419 scrubrule : scrubaction dir logquick interface af proto fromto scrub_opts 1420 { 1421 struct pfctl_rule r; 1422 1423 if (check_rulestate(PFCTL_STATE_SCRUB)) 1424 YYERROR; 1425 1426 memset(&r, 0, sizeof(r)); 1427 1428 r.action = $1.b1; 1429 r.direction = $2; 1430 1431 r.log = $3.log; 1432 r.logif = $3.logif; 1433 if ($3.quick) { 1434 yyerror("scrub rules do not support 'quick'"); 1435 YYERROR; 1436 } 1437 1438 r.af = $5; 1439 if ($8.nodf) 1440 r.rule_flag |= PFRULE_NODF; 1441 if ($8.randomid) 1442 r.rule_flag |= PFRULE_RANDOMID; 1443 if ($8.reassemble_tcp) { 1444 if (r.direction != PF_INOUT) { 1445 yyerror("reassemble tcp rules can not " 1446 "specify direction"); 1447 YYERROR; 1448 } 1449 r.rule_flag |= PFRULE_REASSEMBLE_TCP; 1450 } 1451 if ($8.minttl) 1452 r.min_ttl = $8.minttl; 1453 if ($8.maxmss) 1454 r.max_mss = $8.maxmss; 1455 if ($8.marker & FOM_SETTOS) { 1456 r.rule_flag |= PFRULE_SET_TOS; 1457 r.set_tos = $8.settos; 1458 } 1459 if ($8.fragcache) 1460 r.rule_flag |= $8.fragcache; 1461 if ($8.match_tag) 1462 if (strlcpy(r.match_tagname, $8.match_tag, 1463 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) { 1464 yyerror("tag too long, max %u chars", 1465 PF_TAG_NAME_SIZE - 1); 1466 YYERROR; 1467 } 1468 r.match_tag_not = $8.match_tag_not; 1469 r.rtableid = $8.rtableid; 1470 1471 expand_rule(&r, $4, NULL, NULL, NULL, NULL, $6, $7.src_os, 1472 $7.src.host, $7.src.port, $7.dst.host, $7.dst.port, 1473 NULL, NULL, NULL, NULL, ""); 1474 } 1475 ; 1476 1477 scrub_opts : { 1478 bzero(&scrub_opts, sizeof scrub_opts); 1479 scrub_opts.rtableid = -1; 1480 } 1481 scrub_opts_l 1482 { $$ = scrub_opts; } 1483 | /* empty */ { 1484 bzero(&scrub_opts, sizeof scrub_opts); 1485 scrub_opts.rtableid = -1; 1486 $$ = scrub_opts; 1487 } 1488 ; 1489 1490 scrub_opts_l : scrub_opts_l comma scrub_opt 1491 | scrub_opt 1492 ; 1493 1494 scrub_opt : NODF { 1495 if (scrub_opts.nodf) { 1496 yyerror("no-df cannot be respecified"); 1497 YYERROR; 1498 } 1499 scrub_opts.nodf = 1; 1500 } 1501 | MINTTL NUMBER { 1502 if (scrub_opts.marker & FOM_MINTTL) { 1503 yyerror("min-ttl cannot be respecified"); 1504 YYERROR; 1505 } 1506 if ($2 < 0 || $2 > 255) { 1507 yyerror("illegal min-ttl value %d", $2); 1508 YYERROR; 1509 } 1510 scrub_opts.marker |= FOM_MINTTL; 1511 scrub_opts.minttl = $2; 1512 } 1513 | MAXMSS NUMBER { 1514 if (scrub_opts.marker & FOM_MAXMSS) { 1515 yyerror("max-mss cannot be respecified"); 1516 YYERROR; 1517 } 1518 if ($2 < 0 || $2 > 65535) { 1519 yyerror("illegal max-mss value %d", $2); 1520 YYERROR; 1521 } 1522 scrub_opts.marker |= FOM_MAXMSS; 1523 scrub_opts.maxmss = $2; 1524 } 1525 | SETTOS tos { 1526 if (scrub_opts.marker & FOM_SETTOS) { 1527 yyerror("set-tos cannot be respecified"); 1528 YYERROR; 1529 } 1530 scrub_opts.marker |= FOM_SETTOS; 1531 scrub_opts.settos = $2; 1532 } 1533 | fragcache { 1534 if (scrub_opts.marker & FOM_FRAGCACHE) { 1535 yyerror("fragcache cannot be respecified"); 1536 YYERROR; 1537 } 1538 scrub_opts.marker |= FOM_FRAGCACHE; 1539 scrub_opts.fragcache = $1; 1540 } 1541 | REASSEMBLE STRING { 1542 if (strcasecmp($2, "tcp") != 0) { 1543 yyerror("scrub reassemble supports only tcp, " 1544 "not '%s'", $2); 1545 free($2); 1546 YYERROR; 1547 } 1548 free($2); 1549 if (scrub_opts.reassemble_tcp) { 1550 yyerror("reassemble tcp cannot be respecified"); 1551 YYERROR; 1552 } 1553 scrub_opts.reassemble_tcp = 1; 1554 } 1555 | RANDOMID { 1556 if (scrub_opts.randomid) { 1557 yyerror("random-id cannot be respecified"); 1558 YYERROR; 1559 } 1560 scrub_opts.randomid = 1; 1561 } 1562 | RTABLE NUMBER { 1563 if ($2 < 0 || $2 > rt_tableid_max()) { 1564 yyerror("invalid rtable id"); 1565 YYERROR; 1566 } 1567 scrub_opts.rtableid = $2; 1568 } 1569 | not TAGGED string { 1570 scrub_opts.match_tag = $3; 1571 scrub_opts.match_tag_not = $1; 1572 } 1573 ; 1574 1575 fragcache : FRAGMENT REASSEMBLE { $$ = 0; /* default */ } 1576 | FRAGMENT NO REASSEMBLE { $$ = PFRULE_FRAGMENT_NOREASS; } 1577 ; 1578 1579 antispoof : ANTISPOOF logquick antispoof_ifspc af antispoof_opts { 1580 struct pfctl_rule r; 1581 struct node_host *h = NULL, *hh; 1582 struct node_if *i, *j; 1583 1584 if (check_rulestate(PFCTL_STATE_FILTER)) 1585 YYERROR; 1586 1587 for (i = $3; i; i = i->next) { 1588 bzero(&r, sizeof(r)); 1589 1590 r.action = PF_DROP; 1591 r.direction = PF_IN; 1592 r.log = $2.log; 1593 r.logif = $2.logif; 1594 r.quick = $2.quick; 1595 r.af = $4; 1596 r.ridentifier = $5.ridentifier; 1597 if (rule_label(&r, $5.label)) 1598 YYERROR; 1599 r.rtableid = $5.rtableid; 1600 j = calloc(1, sizeof(struct node_if)); 1601 if (j == NULL) 1602 err(1, "antispoof: calloc"); 1603 if (strlcpy(j->ifname, i->ifname, 1604 sizeof(j->ifname)) >= sizeof(j->ifname)) { 1605 free(j); 1606 yyerror("interface name too long"); 1607 YYERROR; 1608 } 1609 j->not = 1; 1610 if (i->dynamic) { 1611 h = calloc(1, sizeof(*h)); 1612 if (h == NULL) 1613 err(1, "address: calloc"); 1614 h->addr.type = PF_ADDR_DYNIFTL; 1615 set_ipmask(h, 128); 1616 if (strlcpy(h->addr.v.ifname, i->ifname, 1617 sizeof(h->addr.v.ifname)) >= 1618 sizeof(h->addr.v.ifname)) { 1619 free(h); 1620 yyerror( 1621 "interface name too long"); 1622 YYERROR; 1623 } 1624 hh = malloc(sizeof(*hh)); 1625 if (hh == NULL) 1626 err(1, "address: malloc"); 1627 bcopy(h, hh, sizeof(*hh)); 1628 h->addr.iflags = PFI_AFLAG_NETWORK; 1629 } else { 1630 h = ifa_lookup(j->ifname, 1631 PFI_AFLAG_NETWORK); 1632 hh = NULL; 1633 } 1634 1635 if (h != NULL) 1636 expand_rule(&r, j, NULL, NULL, NULL, NULL, NULL, NULL, h, 1637 NULL, NULL, NULL, NULL, NULL, 1638 NULL, NULL, ""); 1639 1640 if ((i->ifa_flags & IFF_LOOPBACK) == 0) { 1641 bzero(&r, sizeof(r)); 1642 1643 r.action = PF_DROP; 1644 r.direction = PF_IN; 1645 r.log = $2.log; 1646 r.logif = $2.logif; 1647 r.quick = $2.quick; 1648 r.af = $4; 1649 r.ridentifier = $5.ridentifier; 1650 if (rule_label(&r, $5.label)) 1651 YYERROR; 1652 r.rtableid = $5.rtableid; 1653 if (hh != NULL) 1654 h = hh; 1655 else 1656 h = ifa_lookup(i->ifname, 0); 1657 if (h != NULL) 1658 expand_rule(&r, NULL, NULL, NULL, NULL, NULL, 1659 NULL, NULL, h, NULL, NULL, 1660 NULL, NULL, NULL, NULL, NULL, ""); 1661 } else 1662 free(hh); 1663 } 1664 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++) 1665 free($5.label[i]); 1666 } 1667 ; 1668 1669 antispoof_ifspc : FOR antispoof_if { $$ = $2; } 1670 | FOR '{' optnl antispoof_iflst '}' { $$ = $4; } 1671 ; 1672 1673 antispoof_iflst : antispoof_if optnl { $$ = $1; } 1674 | antispoof_iflst comma antispoof_if optnl { 1675 $1->tail->next = $3; 1676 $1->tail = $3; 1677 $$ = $1; 1678 } 1679 ; 1680 1681 antispoof_if : if_item { $$ = $1; } 1682 | '(' if_item ')' { 1683 $2->dynamic = 1; 1684 $$ = $2; 1685 } 1686 ; 1687 1688 antispoof_opts : { 1689 bzero(&antispoof_opts, sizeof antispoof_opts); 1690 antispoof_opts.rtableid = -1; 1691 } 1692 antispoof_opts_l 1693 { $$ = antispoof_opts; } 1694 | /* empty */ { 1695 bzero(&antispoof_opts, sizeof antispoof_opts); 1696 antispoof_opts.rtableid = -1; 1697 $$ = antispoof_opts; 1698 } 1699 ; 1700 1701 antispoof_opts_l : antispoof_opts_l antispoof_opt 1702 | antispoof_opt 1703 ; 1704 1705 antispoof_opt : label { 1706 if (antispoof_opts.labelcount >= PF_RULE_MAX_LABEL_COUNT) { 1707 yyerror("label can only be used %d times", PF_RULE_MAX_LABEL_COUNT); 1708 YYERROR; 1709 } 1710 antispoof_opts.label[antispoof_opts.labelcount++] = $1; 1711 } 1712 | RIDENTIFIER number { 1713 antispoof_opts.ridentifier = $2; 1714 } 1715 | RTABLE NUMBER { 1716 if ($2 < 0 || $2 > rt_tableid_max()) { 1717 yyerror("invalid rtable id"); 1718 YYERROR; 1719 } 1720 antispoof_opts.rtableid = $2; 1721 } 1722 ; 1723 1724 not : '!' { $$ = 1; } 1725 | /* empty */ { $$ = 0; } 1726 ; 1727 1728 tabledef : TABLE '<' STRING '>' table_opts { 1729 struct node_host *h, *nh; 1730 struct node_tinit *ti, *nti; 1731 1732 if (strlen($3) >= PF_TABLE_NAME_SIZE) { 1733 yyerror("table name too long, max %d chars", 1734 PF_TABLE_NAME_SIZE - 1); 1735 free($3); 1736 YYERROR; 1737 } 1738 if (pf->loadopt & PFCTL_FLAG_TABLE) 1739 if (process_tabledef($3, &$5)) { 1740 free($3); 1741 YYERROR; 1742 } 1743 free($3); 1744 for (ti = SIMPLEQ_FIRST(&$5.init_nodes); 1745 ti != SIMPLEQ_END(&$5.init_nodes); ti = nti) { 1746 if (ti->file) 1747 free(ti->file); 1748 for (h = ti->host; h != NULL; h = nh) { 1749 nh = h->next; 1750 free(h); 1751 } 1752 nti = SIMPLEQ_NEXT(ti, entries); 1753 free(ti); 1754 } 1755 } 1756 ; 1757 1758 table_opts : { 1759 bzero(&table_opts, sizeof table_opts); 1760 SIMPLEQ_INIT(&table_opts.init_nodes); 1761 } 1762 table_opts_l 1763 { $$ = table_opts; } 1764 | /* empty */ 1765 { 1766 bzero(&table_opts, sizeof table_opts); 1767 SIMPLEQ_INIT(&table_opts.init_nodes); 1768 $$ = table_opts; 1769 } 1770 ; 1771 1772 table_opts_l : table_opts_l table_opt 1773 | table_opt 1774 ; 1775 1776 table_opt : STRING { 1777 if (!strcmp($1, "const")) 1778 table_opts.flags |= PFR_TFLAG_CONST; 1779 else if (!strcmp($1, "persist")) 1780 table_opts.flags |= PFR_TFLAG_PERSIST; 1781 else if (!strcmp($1, "counters")) 1782 table_opts.flags |= PFR_TFLAG_COUNTERS; 1783 else { 1784 yyerror("invalid table option '%s'", $1); 1785 free($1); 1786 YYERROR; 1787 } 1788 free($1); 1789 } 1790 | '{' optnl '}' { table_opts.init_addr = 1; } 1791 | '{' optnl host_list '}' { 1792 struct node_host *n; 1793 struct node_tinit *ti; 1794 1795 for (n = $3; n != NULL; n = n->next) { 1796 switch (n->addr.type) { 1797 case PF_ADDR_ADDRMASK: 1798 continue; /* ok */ 1799 case PF_ADDR_RANGE: 1800 yyerror("address ranges are not " 1801 "permitted inside tables"); 1802 break; 1803 case PF_ADDR_DYNIFTL: 1804 yyerror("dynamic addresses are not " 1805 "permitted inside tables"); 1806 break; 1807 case PF_ADDR_TABLE: 1808 yyerror("tables cannot contain tables"); 1809 break; 1810 case PF_ADDR_NOROUTE: 1811 yyerror("\"no-route\" is not permitted " 1812 "inside tables"); 1813 break; 1814 case PF_ADDR_URPFFAILED: 1815 yyerror("\"urpf-failed\" is not " 1816 "permitted inside tables"); 1817 break; 1818 default: 1819 yyerror("unknown address type %d", 1820 n->addr.type); 1821 } 1822 YYERROR; 1823 } 1824 if (!(ti = calloc(1, sizeof(*ti)))) 1825 err(1, "table_opt: calloc"); 1826 ti->host = $3; 1827 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti, 1828 entries); 1829 table_opts.init_addr = 1; 1830 } 1831 | FILENAME STRING { 1832 struct node_tinit *ti; 1833 1834 if (!(ti = calloc(1, sizeof(*ti)))) 1835 err(1, "table_opt: calloc"); 1836 ti->file = $2; 1837 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti, 1838 entries); 1839 table_opts.init_addr = 1; 1840 } 1841 ; 1842 1843 altqif : ALTQ interface queue_opts QUEUE qassign { 1844 struct pf_altq a; 1845 1846 if (check_rulestate(PFCTL_STATE_QUEUE)) 1847 YYERROR; 1848 1849 memset(&a, 0, sizeof(a)); 1850 if ($3.scheduler.qtype == ALTQT_NONE) { 1851 yyerror("no scheduler specified!"); 1852 YYERROR; 1853 } 1854 a.scheduler = $3.scheduler.qtype; 1855 a.qlimit = $3.qlimit; 1856 a.tbrsize = $3.tbrsize; 1857 if ($5 == NULL && $3.scheduler.qtype != ALTQT_CODEL) { 1858 yyerror("no child queues specified"); 1859 YYERROR; 1860 } 1861 if (expand_altq(&a, $2, $5, $3.queue_bwspec, 1862 &$3.scheduler)) 1863 YYERROR; 1864 } 1865 ; 1866 1867 queuespec : QUEUE STRING interface queue_opts qassign { 1868 struct pf_altq a; 1869 1870 if (check_rulestate(PFCTL_STATE_QUEUE)) { 1871 free($2); 1872 YYERROR; 1873 } 1874 1875 memset(&a, 0, sizeof(a)); 1876 1877 if (strlcpy(a.qname, $2, sizeof(a.qname)) >= 1878 sizeof(a.qname)) { 1879 yyerror("queue name too long (max " 1880 "%d chars)", PF_QNAME_SIZE-1); 1881 free($2); 1882 YYERROR; 1883 } 1884 free($2); 1885 if ($4.tbrsize) { 1886 yyerror("cannot specify tbrsize for queue"); 1887 YYERROR; 1888 } 1889 if ($4.priority > 255) { 1890 yyerror("priority out of range: max 255"); 1891 YYERROR; 1892 } 1893 a.priority = $4.priority; 1894 a.qlimit = $4.qlimit; 1895 a.scheduler = $4.scheduler.qtype; 1896 if (expand_queue(&a, $3, $5, $4.queue_bwspec, 1897 &$4.scheduler)) { 1898 yyerror("errors in queue definition"); 1899 YYERROR; 1900 } 1901 } 1902 ; 1903 1904 queue_opts : { 1905 bzero(&queue_opts, sizeof queue_opts); 1906 queue_opts.priority = DEFAULT_PRIORITY; 1907 queue_opts.qlimit = DEFAULT_QLIMIT; 1908 queue_opts.scheduler.qtype = ALTQT_NONE; 1909 queue_opts.queue_bwspec.bw_percent = 100; 1910 } 1911 queue_opts_l 1912 { $$ = queue_opts; } 1913 | /* empty */ { 1914 bzero(&queue_opts, sizeof queue_opts); 1915 queue_opts.priority = DEFAULT_PRIORITY; 1916 queue_opts.qlimit = DEFAULT_QLIMIT; 1917 queue_opts.scheduler.qtype = ALTQT_NONE; 1918 queue_opts.queue_bwspec.bw_percent = 100; 1919 $$ = queue_opts; 1920 } 1921 ; 1922 1923 queue_opts_l : queue_opts_l queue_opt 1924 | queue_opt 1925 ; 1926 1927 queue_opt : BANDWIDTH bandwidth { 1928 if (queue_opts.marker & QOM_BWSPEC) { 1929 yyerror("bandwidth cannot be respecified"); 1930 YYERROR; 1931 } 1932 queue_opts.marker |= QOM_BWSPEC; 1933 queue_opts.queue_bwspec = $2; 1934 } 1935 | PRIORITY NUMBER { 1936 if (queue_opts.marker & QOM_PRIORITY) { 1937 yyerror("priority cannot be respecified"); 1938 YYERROR; 1939 } 1940 if ($2 < 0 || $2 > 255) { 1941 yyerror("priority out of range: max 255"); 1942 YYERROR; 1943 } 1944 queue_opts.marker |= QOM_PRIORITY; 1945 queue_opts.priority = $2; 1946 } 1947 | QLIMIT NUMBER { 1948 if (queue_opts.marker & QOM_QLIMIT) { 1949 yyerror("qlimit cannot be respecified"); 1950 YYERROR; 1951 } 1952 if ($2 < 0 || $2 > 65535) { 1953 yyerror("qlimit out of range: max 65535"); 1954 YYERROR; 1955 } 1956 queue_opts.marker |= QOM_QLIMIT; 1957 queue_opts.qlimit = $2; 1958 } 1959 | scheduler { 1960 if (queue_opts.marker & QOM_SCHEDULER) { 1961 yyerror("scheduler cannot be respecified"); 1962 YYERROR; 1963 } 1964 queue_opts.marker |= QOM_SCHEDULER; 1965 queue_opts.scheduler = $1; 1966 } 1967 | TBRSIZE NUMBER { 1968 if (queue_opts.marker & QOM_TBRSIZE) { 1969 yyerror("tbrsize cannot be respecified"); 1970 YYERROR; 1971 } 1972 if ($2 < 0 || $2 > UINT_MAX) { 1973 yyerror("tbrsize too big: max %u", UINT_MAX); 1974 YYERROR; 1975 } 1976 queue_opts.marker |= QOM_TBRSIZE; 1977 queue_opts.tbrsize = $2; 1978 } 1979 ; 1980 1981 bandwidth : STRING { 1982 double bps; 1983 char *cp; 1984 1985 $$.bw_percent = 0; 1986 1987 bps = strtod($1, &cp); 1988 if (cp != NULL) { 1989 if (strlen(cp) > 1) { 1990 char *cu = cp + 1; 1991 if (!strcmp(cu, "Bit") || 1992 !strcmp(cu, "B") || 1993 !strcmp(cu, "bit") || 1994 !strcmp(cu, "b")) { 1995 *cu = 0; 1996 } 1997 } 1998 if (!strcmp(cp, "b")) 1999 ; /* nothing */ 2000 else if (!strcmp(cp, "K")) 2001 bps *= 1000; 2002 else if (!strcmp(cp, "M")) 2003 bps *= 1000 * 1000; 2004 else if (!strcmp(cp, "G")) 2005 bps *= 1000 * 1000 * 1000; 2006 else if (!strcmp(cp, "%")) { 2007 if (bps < 0 || bps > 100) { 2008 yyerror("bandwidth spec " 2009 "out of range"); 2010 free($1); 2011 YYERROR; 2012 } 2013 $$.bw_percent = bps; 2014 bps = 0; 2015 } else { 2016 yyerror("unknown unit %s", cp); 2017 free($1); 2018 YYERROR; 2019 } 2020 } 2021 free($1); 2022 $$.bw_absolute = (u_int64_t)bps; 2023 } 2024 | NUMBER { 2025 if ($1 < 0 || $1 >= LLONG_MAX) { 2026 yyerror("bandwidth number too big"); 2027 YYERROR; 2028 } 2029 $$.bw_percent = 0; 2030 $$.bw_absolute = $1; 2031 } 2032 ; 2033 2034 scheduler : CBQ { 2035 $$.qtype = ALTQT_CBQ; 2036 $$.data.cbq_opts.flags = 0; 2037 } 2038 | CBQ '(' cbqflags_list ')' { 2039 $$.qtype = ALTQT_CBQ; 2040 $$.data.cbq_opts.flags = $3; 2041 } 2042 | PRIQ { 2043 $$.qtype = ALTQT_PRIQ; 2044 $$.data.priq_opts.flags = 0; 2045 } 2046 | PRIQ '(' priqflags_list ')' { 2047 $$.qtype = ALTQT_PRIQ; 2048 $$.data.priq_opts.flags = $3; 2049 } 2050 | HFSC { 2051 $$.qtype = ALTQT_HFSC; 2052 bzero(&$$.data.hfsc_opts, 2053 sizeof(struct node_hfsc_opts)); 2054 } 2055 | HFSC '(' hfsc_opts ')' { 2056 $$.qtype = ALTQT_HFSC; 2057 $$.data.hfsc_opts = $3; 2058 } 2059 | FAIRQ { 2060 $$.qtype = ALTQT_FAIRQ; 2061 bzero(&$$.data.fairq_opts, 2062 sizeof(struct node_fairq_opts)); 2063 } 2064 | FAIRQ '(' fairq_opts ')' { 2065 $$.qtype = ALTQT_FAIRQ; 2066 $$.data.fairq_opts = $3; 2067 } 2068 | CODEL { 2069 $$.qtype = ALTQT_CODEL; 2070 bzero(&$$.data.codel_opts, 2071 sizeof(struct codel_opts)); 2072 } 2073 | CODEL '(' codel_opts ')' { 2074 $$.qtype = ALTQT_CODEL; 2075 $$.data.codel_opts = $3; 2076 } 2077 ; 2078 2079 cbqflags_list : cbqflags_item { $$ |= $1; } 2080 | cbqflags_list comma cbqflags_item { $$ |= $3; } 2081 ; 2082 2083 cbqflags_item : STRING { 2084 if (!strcmp($1, "default")) 2085 $$ = CBQCLF_DEFCLASS; 2086 else if (!strcmp($1, "borrow")) 2087 $$ = CBQCLF_BORROW; 2088 else if (!strcmp($1, "red")) 2089 $$ = CBQCLF_RED; 2090 else if (!strcmp($1, "ecn")) 2091 $$ = CBQCLF_RED|CBQCLF_ECN; 2092 else if (!strcmp($1, "rio")) 2093 $$ = CBQCLF_RIO; 2094 else if (!strcmp($1, "codel")) 2095 $$ = CBQCLF_CODEL; 2096 else { 2097 yyerror("unknown cbq flag \"%s\"", $1); 2098 free($1); 2099 YYERROR; 2100 } 2101 free($1); 2102 } 2103 ; 2104 2105 priqflags_list : priqflags_item { $$ |= $1; } 2106 | priqflags_list comma priqflags_item { $$ |= $3; } 2107 ; 2108 2109 priqflags_item : STRING { 2110 if (!strcmp($1, "default")) 2111 $$ = PRCF_DEFAULTCLASS; 2112 else if (!strcmp($1, "red")) 2113 $$ = PRCF_RED; 2114 else if (!strcmp($1, "ecn")) 2115 $$ = PRCF_RED|PRCF_ECN; 2116 else if (!strcmp($1, "rio")) 2117 $$ = PRCF_RIO; 2118 else if (!strcmp($1, "codel")) 2119 $$ = PRCF_CODEL; 2120 else { 2121 yyerror("unknown priq flag \"%s\"", $1); 2122 free($1); 2123 YYERROR; 2124 } 2125 free($1); 2126 } 2127 ; 2128 2129 hfsc_opts : { 2130 bzero(&hfsc_opts, 2131 sizeof(struct node_hfsc_opts)); 2132 } 2133 hfscopts_list { 2134 $$ = hfsc_opts; 2135 } 2136 ; 2137 2138 hfscopts_list : hfscopts_item 2139 | hfscopts_list comma hfscopts_item 2140 ; 2141 2142 hfscopts_item : LINKSHARE bandwidth { 2143 if (hfsc_opts.linkshare.used) { 2144 yyerror("linkshare already specified"); 2145 YYERROR; 2146 } 2147 hfsc_opts.linkshare.m2 = $2; 2148 hfsc_opts.linkshare.used = 1; 2149 } 2150 | LINKSHARE '(' bandwidth comma NUMBER comma bandwidth ')' 2151 { 2152 if ($5 < 0 || $5 > INT_MAX) { 2153 yyerror("timing in curve out of range"); 2154 YYERROR; 2155 } 2156 if (hfsc_opts.linkshare.used) { 2157 yyerror("linkshare already specified"); 2158 YYERROR; 2159 } 2160 hfsc_opts.linkshare.m1 = $3; 2161 hfsc_opts.linkshare.d = $5; 2162 hfsc_opts.linkshare.m2 = $7; 2163 hfsc_opts.linkshare.used = 1; 2164 } 2165 | REALTIME bandwidth { 2166 if (hfsc_opts.realtime.used) { 2167 yyerror("realtime already specified"); 2168 YYERROR; 2169 } 2170 hfsc_opts.realtime.m2 = $2; 2171 hfsc_opts.realtime.used = 1; 2172 } 2173 | REALTIME '(' bandwidth comma NUMBER comma bandwidth ')' 2174 { 2175 if ($5 < 0 || $5 > INT_MAX) { 2176 yyerror("timing in curve out of range"); 2177 YYERROR; 2178 } 2179 if (hfsc_opts.realtime.used) { 2180 yyerror("realtime already specified"); 2181 YYERROR; 2182 } 2183 hfsc_opts.realtime.m1 = $3; 2184 hfsc_opts.realtime.d = $5; 2185 hfsc_opts.realtime.m2 = $7; 2186 hfsc_opts.realtime.used = 1; 2187 } 2188 | UPPERLIMIT bandwidth { 2189 if (hfsc_opts.upperlimit.used) { 2190 yyerror("upperlimit already specified"); 2191 YYERROR; 2192 } 2193 hfsc_opts.upperlimit.m2 = $2; 2194 hfsc_opts.upperlimit.used = 1; 2195 } 2196 | UPPERLIMIT '(' bandwidth comma NUMBER comma bandwidth ')' 2197 { 2198 if ($5 < 0 || $5 > INT_MAX) { 2199 yyerror("timing in curve out of range"); 2200 YYERROR; 2201 } 2202 if (hfsc_opts.upperlimit.used) { 2203 yyerror("upperlimit already specified"); 2204 YYERROR; 2205 } 2206 hfsc_opts.upperlimit.m1 = $3; 2207 hfsc_opts.upperlimit.d = $5; 2208 hfsc_opts.upperlimit.m2 = $7; 2209 hfsc_opts.upperlimit.used = 1; 2210 } 2211 | STRING { 2212 if (!strcmp($1, "default")) 2213 hfsc_opts.flags |= HFCF_DEFAULTCLASS; 2214 else if (!strcmp($1, "red")) 2215 hfsc_opts.flags |= HFCF_RED; 2216 else if (!strcmp($1, "ecn")) 2217 hfsc_opts.flags |= HFCF_RED|HFCF_ECN; 2218 else if (!strcmp($1, "rio")) 2219 hfsc_opts.flags |= HFCF_RIO; 2220 else if (!strcmp($1, "codel")) 2221 hfsc_opts.flags |= HFCF_CODEL; 2222 else { 2223 yyerror("unknown hfsc flag \"%s\"", $1); 2224 free($1); 2225 YYERROR; 2226 } 2227 free($1); 2228 } 2229 ; 2230 2231 fairq_opts : { 2232 bzero(&fairq_opts, 2233 sizeof(struct node_fairq_opts)); 2234 } 2235 fairqopts_list { 2236 $$ = fairq_opts; 2237 } 2238 ; 2239 2240 fairqopts_list : fairqopts_item 2241 | fairqopts_list comma fairqopts_item 2242 ; 2243 2244 fairqopts_item : LINKSHARE bandwidth { 2245 if (fairq_opts.linkshare.used) { 2246 yyerror("linkshare already specified"); 2247 YYERROR; 2248 } 2249 fairq_opts.linkshare.m2 = $2; 2250 fairq_opts.linkshare.used = 1; 2251 } 2252 | LINKSHARE '(' bandwidth number bandwidth ')' { 2253 if (fairq_opts.linkshare.used) { 2254 yyerror("linkshare already specified"); 2255 YYERROR; 2256 } 2257 fairq_opts.linkshare.m1 = $3; 2258 fairq_opts.linkshare.d = $4; 2259 fairq_opts.linkshare.m2 = $5; 2260 fairq_opts.linkshare.used = 1; 2261 } 2262 | HOGS bandwidth { 2263 fairq_opts.hogs_bw = $2; 2264 } 2265 | BUCKETS number { 2266 fairq_opts.nbuckets = $2; 2267 } 2268 | STRING { 2269 if (!strcmp($1, "default")) 2270 fairq_opts.flags |= FARF_DEFAULTCLASS; 2271 else if (!strcmp($1, "red")) 2272 fairq_opts.flags |= FARF_RED; 2273 else if (!strcmp($1, "ecn")) 2274 fairq_opts.flags |= FARF_RED|FARF_ECN; 2275 else if (!strcmp($1, "rio")) 2276 fairq_opts.flags |= FARF_RIO; 2277 else if (!strcmp($1, "codel")) 2278 fairq_opts.flags |= FARF_CODEL; 2279 else { 2280 yyerror("unknown fairq flag \"%s\"", $1); 2281 free($1); 2282 YYERROR; 2283 } 2284 free($1); 2285 } 2286 ; 2287 2288 codel_opts : { 2289 bzero(&codel_opts, 2290 sizeof(struct codel_opts)); 2291 } 2292 codelopts_list { 2293 $$ = codel_opts; 2294 } 2295 ; 2296 2297 codelopts_list : codelopts_item 2298 | codelopts_list comma codelopts_item 2299 ; 2300 2301 codelopts_item : INTERVAL number { 2302 if (codel_opts.interval) { 2303 yyerror("interval already specified"); 2304 YYERROR; 2305 } 2306 codel_opts.interval = $2; 2307 } 2308 | TARGET number { 2309 if (codel_opts.target) { 2310 yyerror("target already specified"); 2311 YYERROR; 2312 } 2313 codel_opts.target = $2; 2314 } 2315 | STRING { 2316 if (!strcmp($1, "ecn")) 2317 codel_opts.ecn = 1; 2318 else { 2319 yyerror("unknown codel option \"%s\"", $1); 2320 free($1); 2321 YYERROR; 2322 } 2323 free($1); 2324 } 2325 ; 2326 2327 qassign : /* empty */ { $$ = NULL; } 2328 | qassign_item { $$ = $1; } 2329 | '{' optnl qassign_list '}' { $$ = $3; } 2330 ; 2331 2332 qassign_list : qassign_item optnl { $$ = $1; } 2333 | qassign_list comma qassign_item optnl { 2334 $1->tail->next = $3; 2335 $1->tail = $3; 2336 $$ = $1; 2337 } 2338 ; 2339 2340 qassign_item : STRING { 2341 $$ = calloc(1, sizeof(struct node_queue)); 2342 if ($$ == NULL) 2343 err(1, "qassign_item: calloc"); 2344 if (strlcpy($$->queue, $1, sizeof($$->queue)) >= 2345 sizeof($$->queue)) { 2346 yyerror("queue name '%s' too long (max " 2347 "%d chars)", $1, sizeof($$->queue)-1); 2348 free($1); 2349 free($$); 2350 YYERROR; 2351 } 2352 free($1); 2353 $$->next = NULL; 2354 $$->tail = $$; 2355 } 2356 ; 2357 2358 pfrule : action dir logquick interface route af proto fromto 2359 filter_opts 2360 { 2361 struct pfctl_rule r; 2362 struct node_state_opt *o; 2363 struct node_proto *proto; 2364 int srctrack = 0; 2365 int statelock = 0; 2366 int adaptive = 0; 2367 int defaults = 0; 2368 2369 if (check_rulestate(PFCTL_STATE_FILTER)) 2370 YYERROR; 2371 2372 memset(&r, 0, sizeof(r)); 2373 2374 r.action = $1.b1; 2375 switch ($1.b2) { 2376 case PFRULE_RETURNRST: 2377 r.rule_flag |= PFRULE_RETURNRST; 2378 r.return_ttl = $1.w; 2379 break; 2380 case PFRULE_RETURNICMP: 2381 r.rule_flag |= PFRULE_RETURNICMP; 2382 r.return_icmp = $1.w; 2383 r.return_icmp6 = $1.w2; 2384 break; 2385 case PFRULE_RETURN: 2386 r.rule_flag |= PFRULE_RETURN; 2387 r.return_icmp = $1.w; 2388 r.return_icmp6 = $1.w2; 2389 break; 2390 } 2391 r.direction = $2; 2392 r.log = $3.log; 2393 r.logif = $3.logif; 2394 r.quick = $3.quick; 2395 r.prob = $9.prob; 2396 r.rtableid = $9.rtableid; 2397 2398 if ($9.nodf) 2399 r.scrub_flags |= PFSTATE_NODF; 2400 if ($9.randomid) 2401 r.scrub_flags |= PFSTATE_RANDOMID; 2402 if ($9.minttl) 2403 r.min_ttl = $9.minttl; 2404 if ($9.max_mss) 2405 r.max_mss = $9.max_mss; 2406 if ($9.marker & FOM_SETTOS) { 2407 r.scrub_flags |= PFSTATE_SETTOS; 2408 r.set_tos = $9.settos; 2409 } 2410 if ($9.marker & FOM_SCRUB_TCP) 2411 r.scrub_flags |= PFSTATE_SCRUB_TCP; 2412 2413 if ($9.marker & FOM_PRIO) { 2414 if ($9.prio == 0) 2415 r.prio = PF_PRIO_ZERO; 2416 else 2417 r.prio = $9.prio; 2418 } 2419 if ($9.marker & FOM_SETPRIO) { 2420 r.set_prio[0] = $9.set_prio[0]; 2421 r.set_prio[1] = $9.set_prio[1]; 2422 r.scrub_flags |= PFSTATE_SETPRIO; 2423 } 2424 2425 if ($9.marker & FOM_AFTO) { 2426 if (!$6) { 2427 yyerror("must indicate source address " 2428 "family with af-to"); 2429 YYERROR; 2430 } 2431 if ($6 == $9.nat.af) { 2432 yyerror("incorrect address family " 2433 "translation"); 2434 YYERROR; 2435 } 2436 r.rule_flag |= PFRULE_AFTO; 2437 } 2438 2439 r.af = $6; 2440 if ($9.tag) 2441 if (strlcpy(r.tagname, $9.tag, 2442 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) { 2443 yyerror("tag too long, max %u chars", 2444 PF_TAG_NAME_SIZE - 1); 2445 YYERROR; 2446 } 2447 if ($9.match_tag) 2448 if (strlcpy(r.match_tagname, $9.match_tag, 2449 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) { 2450 yyerror("tag too long, max %u chars", 2451 PF_TAG_NAME_SIZE - 1); 2452 YYERROR; 2453 } 2454 r.match_tag_not = $9.match_tag_not; 2455 if (rule_label(&r, $9.label)) 2456 YYERROR; 2457 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++) 2458 free($9.label[i]); 2459 r.ridentifier = $9.ridentifier; 2460 r.flags = $9.flags.b1; 2461 r.flagset = $9.flags.b2; 2462 if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) { 2463 yyerror("flags always false"); 2464 YYERROR; 2465 } 2466 if ($9.flags.b1 || $9.flags.b2 || $8.src_os) { 2467 for (proto = $7; proto != NULL && 2468 proto->proto != IPPROTO_TCP; 2469 proto = proto->next) 2470 ; /* nothing */ 2471 if (proto == NULL && $7 != NULL) { 2472 if ($9.flags.b1 || $9.flags.b2) 2473 yyerror( 2474 "flags only apply to tcp"); 2475 if ($8.src_os) 2476 yyerror( 2477 "OS fingerprinting only " 2478 "apply to tcp"); 2479 YYERROR; 2480 } 2481 #if 0 2482 if (($9.flags.b1 & parse_flags("S")) == 0 && 2483 $8.src_os) { 2484 yyerror("OS fingerprinting requires " 2485 "the SYN TCP flag (flags S/SA)"); 2486 YYERROR; 2487 } 2488 #endif 2489 } 2490 2491 r.tos = $9.tos; 2492 r.keep_state = $9.keep.action; 2493 o = $9.keep.options; 2494 2495 /* 'keep state' by default on pass rules. */ 2496 if (!r.keep_state && !r.action && 2497 !($9.marker & FOM_KEEP)) { 2498 r.keep_state = PF_STATE_NORMAL; 2499 o = keep_state_defaults; 2500 defaults = 1; 2501 } 2502 2503 while (o) { 2504 struct node_state_opt *p = o; 2505 2506 switch (o->type) { 2507 case PF_STATE_OPT_MAX: 2508 if (r.max_states) { 2509 yyerror("state option 'max' " 2510 "multiple definitions"); 2511 YYERROR; 2512 } 2513 r.max_states = o->data.max_states; 2514 break; 2515 case PF_STATE_OPT_NOSYNC: 2516 if (r.rule_flag & PFRULE_NOSYNC) { 2517 yyerror("state option 'sync' " 2518 "multiple definitions"); 2519 YYERROR; 2520 } 2521 r.rule_flag |= PFRULE_NOSYNC; 2522 break; 2523 case PF_STATE_OPT_SRCTRACK: 2524 if (srctrack) { 2525 yyerror("state option " 2526 "'source-track' " 2527 "multiple definitions"); 2528 YYERROR; 2529 } 2530 srctrack = o->data.src_track; 2531 r.rule_flag |= PFRULE_SRCTRACK; 2532 break; 2533 case PF_STATE_OPT_MAX_SRC_STATES: 2534 if (r.max_src_states) { 2535 yyerror("state option " 2536 "'max-src-states' " 2537 "multiple definitions"); 2538 YYERROR; 2539 } 2540 if (o->data.max_src_states == 0) { 2541 yyerror("'max-src-states' must " 2542 "be > 0"); 2543 YYERROR; 2544 } 2545 r.max_src_states = 2546 o->data.max_src_states; 2547 r.rule_flag |= PFRULE_SRCTRACK; 2548 break; 2549 case PF_STATE_OPT_OVERLOAD: 2550 if (r.overload_tblname[0]) { 2551 yyerror("multiple 'overload' " 2552 "table definitions"); 2553 YYERROR; 2554 } 2555 if (strlcpy(r.overload_tblname, 2556 o->data.overload.tblname, 2557 PF_TABLE_NAME_SIZE) >= 2558 PF_TABLE_NAME_SIZE) { 2559 yyerror("state option: " 2560 "strlcpy"); 2561 YYERROR; 2562 } 2563 r.flush = o->data.overload.flush; 2564 break; 2565 case PF_STATE_OPT_MAX_SRC_CONN: 2566 if (r.max_src_conn) { 2567 yyerror("state option " 2568 "'max-src-conn' " 2569 "multiple definitions"); 2570 YYERROR; 2571 } 2572 if (o->data.max_src_conn == 0) { 2573 yyerror("'max-src-conn' " 2574 "must be > 0"); 2575 YYERROR; 2576 } 2577 r.max_src_conn = 2578 o->data.max_src_conn; 2579 r.rule_flag |= PFRULE_SRCTRACK | 2580 PFRULE_RULESRCTRACK; 2581 break; 2582 case PF_STATE_OPT_MAX_SRC_CONN_RATE: 2583 if (r.max_src_conn_rate.limit) { 2584 yyerror("state option " 2585 "'max-src-conn-rate' " 2586 "multiple definitions"); 2587 YYERROR; 2588 } 2589 if (!o->data.max_src_conn_rate.limit || 2590 !o->data.max_src_conn_rate.seconds) { 2591 yyerror("'max-src-conn-rate' " 2592 "values must be > 0"); 2593 YYERROR; 2594 } 2595 if (o->data.max_src_conn_rate.limit > 2596 PF_THRESHOLD_MAX) { 2597 yyerror("'max-src-conn-rate' " 2598 "maximum rate must be < %u", 2599 PF_THRESHOLD_MAX); 2600 YYERROR; 2601 } 2602 r.max_src_conn_rate.limit = 2603 o->data.max_src_conn_rate.limit; 2604 r.max_src_conn_rate.seconds = 2605 o->data.max_src_conn_rate.seconds; 2606 r.rule_flag |= PFRULE_SRCTRACK | 2607 PFRULE_RULESRCTRACK; 2608 break; 2609 case PF_STATE_OPT_MAX_SRC_NODES: 2610 if (r.max_src_nodes) { 2611 yyerror("state option " 2612 "'max-src-nodes' " 2613 "multiple definitions"); 2614 YYERROR; 2615 } 2616 if (o->data.max_src_nodes == 0) { 2617 yyerror("'max-src-nodes' must " 2618 "be > 0"); 2619 YYERROR; 2620 } 2621 r.max_src_nodes = 2622 o->data.max_src_nodes; 2623 r.rule_flag |= PFRULE_SRCTRACK | 2624 PFRULE_RULESRCTRACK; 2625 break; 2626 case PF_STATE_OPT_STATELOCK: 2627 if (statelock) { 2628 yyerror("state locking option: " 2629 "multiple definitions"); 2630 YYERROR; 2631 } 2632 statelock = 1; 2633 r.rule_flag |= o->data.statelock; 2634 break; 2635 case PF_STATE_OPT_SLOPPY: 2636 if (r.rule_flag & PFRULE_STATESLOPPY) { 2637 yyerror("state sloppy option: " 2638 "multiple definitions"); 2639 YYERROR; 2640 } 2641 r.rule_flag |= PFRULE_STATESLOPPY; 2642 break; 2643 case PF_STATE_OPT_PFLOW: 2644 if (r.rule_flag & PFRULE_PFLOW) { 2645 yyerror("state pflow option: " 2646 "multiple definitions"); 2647 YYERROR; 2648 } 2649 r.rule_flag |= PFRULE_PFLOW; 2650 break; 2651 case PF_STATE_OPT_TIMEOUT: 2652 if (o->data.timeout.number == 2653 PFTM_ADAPTIVE_START || 2654 o->data.timeout.number == 2655 PFTM_ADAPTIVE_END) 2656 adaptive = 1; 2657 if (r.timeout[o->data.timeout.number]) { 2658 yyerror("state timeout %s " 2659 "multiple definitions", 2660 pf_timeouts[o->data. 2661 timeout.number].name); 2662 YYERROR; 2663 } 2664 r.timeout[o->data.timeout.number] = 2665 o->data.timeout.seconds; 2666 } 2667 o = o->next; 2668 if (!defaults) 2669 free(p); 2670 } 2671 2672 /* 'flags S/SA' by default on stateful rules */ 2673 if (!r.action && !r.flags && !r.flagset && 2674 !$9.fragment && !($9.marker & FOM_FLAGS) && 2675 r.keep_state) { 2676 r.flags = parse_flags("S"); 2677 r.flagset = parse_flags("SA"); 2678 } 2679 if (!adaptive && r.max_states) { 2680 r.timeout[PFTM_ADAPTIVE_START] = 2681 (r.max_states / 10) * 6; 2682 r.timeout[PFTM_ADAPTIVE_END] = 2683 (r.max_states / 10) * 12; 2684 } 2685 if (r.rule_flag & PFRULE_SRCTRACK) { 2686 if (srctrack == PF_SRCTRACK_GLOBAL && 2687 r.max_src_nodes) { 2688 yyerror("'max-src-nodes' is " 2689 "incompatible with " 2690 "'source-track global'"); 2691 YYERROR; 2692 } 2693 if (srctrack == PF_SRCTRACK_GLOBAL && 2694 r.max_src_conn) { 2695 yyerror("'max-src-conn' is " 2696 "incompatible with " 2697 "'source-track global'"); 2698 YYERROR; 2699 } 2700 if (srctrack == PF_SRCTRACK_GLOBAL && 2701 r.max_src_conn_rate.seconds) { 2702 yyerror("'max-src-conn-rate' is " 2703 "incompatible with " 2704 "'source-track global'"); 2705 YYERROR; 2706 } 2707 if (r.timeout[PFTM_SRC_NODE] < 2708 r.max_src_conn_rate.seconds) 2709 r.timeout[PFTM_SRC_NODE] = 2710 r.max_src_conn_rate.seconds; 2711 r.rule_flag |= PFRULE_SRCTRACK; 2712 if (srctrack == PF_SRCTRACK_RULE) 2713 r.rule_flag |= PFRULE_RULESRCTRACK; 2714 } 2715 if (r.keep_state && !statelock) 2716 r.rule_flag |= default_statelock; 2717 2718 if ($9.fragment) 2719 r.rule_flag |= PFRULE_FRAGMENT; 2720 r.allow_opts = $9.allowopts; 2721 2722 decide_address_family($8.src.host, &r.af); 2723 decide_address_family($8.dst.host, &r.af); 2724 2725 if ($5.rt) { 2726 if (!r.direction) { 2727 yyerror("direction must be explicit " 2728 "with rules that specify routing"); 2729 YYERROR; 2730 } 2731 r.rt = $5.rt; 2732 r.rpool.opts = $5.pool_opts; 2733 if ($5.key != NULL) 2734 memcpy(&r.rpool.key, $5.key, 2735 sizeof(struct pf_poolhashkey)); 2736 } 2737 if (r.rt) { 2738 decide_address_family($5.host, &r.af); 2739 remove_invalid_hosts(&$5.host, &r.af); 2740 if ($5.host == NULL) { 2741 yyerror("no routing address with " 2742 "matching address family found."); 2743 YYERROR; 2744 } 2745 if ((r.rpool.opts & PF_POOL_TYPEMASK) == 2746 PF_POOL_NONE && ($5.host->next != NULL || 2747 $5.host->addr.type == PF_ADDR_TABLE || 2748 DYNIF_MULTIADDR($5.host->addr))) 2749 r.rpool.opts |= PF_POOL_ROUNDROBIN; 2750 if ((r.rpool.opts & PF_POOL_TYPEMASK) != 2751 PF_POOL_ROUNDROBIN && 2752 disallow_table($5.host, "tables are only " 2753 "supported in round-robin routing pools")) 2754 YYERROR; 2755 if ((r.rpool.opts & PF_POOL_TYPEMASK) != 2756 PF_POOL_ROUNDROBIN && 2757 disallow_alias($5.host, "interface (%s) " 2758 "is only supported in round-robin " 2759 "routing pools")) 2760 YYERROR; 2761 if ($5.host->next != NULL) { 2762 if ((r.rpool.opts & PF_POOL_TYPEMASK) != 2763 PF_POOL_ROUNDROBIN) { 2764 yyerror("r.rpool.opts must " 2765 "be PF_POOL_ROUNDROBIN"); 2766 YYERROR; 2767 } 2768 } 2769 } 2770 if ($9.queues.qname != NULL) { 2771 if (strlcpy(r.qname, $9.queues.qname, 2772 sizeof(r.qname)) >= sizeof(r.qname)) { 2773 yyerror("rule qname too long (max " 2774 "%d chars)", sizeof(r.qname)-1); 2775 YYERROR; 2776 } 2777 free($9.queues.qname); 2778 } 2779 if ($9.queues.pqname != NULL) { 2780 if (strlcpy(r.pqname, $9.queues.pqname, 2781 sizeof(r.pqname)) >= sizeof(r.pqname)) { 2782 yyerror("rule pqname too long (max " 2783 "%d chars)", sizeof(r.pqname)-1); 2784 YYERROR; 2785 } 2786 free($9.queues.pqname); 2787 } 2788 #ifdef __FreeBSD__ 2789 r.divert.port = $9.divert.port; 2790 #else 2791 if ((r.divert.port = $9.divert.port)) { 2792 if (r.direction == PF_OUT) { 2793 if ($9.divert.addr) { 2794 yyerror("address specified " 2795 "for outgoing divert"); 2796 YYERROR; 2797 } 2798 bzero(&r.divert.addr, 2799 sizeof(r.divert.addr)); 2800 } else { 2801 if (!$9.divert.addr) { 2802 yyerror("no address specified " 2803 "for incoming divert"); 2804 YYERROR; 2805 } 2806 if ($9.divert.addr->af != r.af) { 2807 yyerror("address family " 2808 "mismatch for divert"); 2809 YYERROR; 2810 } 2811 r.divert.addr = 2812 $9.divert.addr->addr.v.a.addr; 2813 } 2814 } 2815 #endif 2816 2817 if ($9.dnpipe || $9.dnrpipe) { 2818 r.dnpipe = $9.dnpipe; 2819 r.dnrpipe = $9.dnrpipe; 2820 if ($9.free_flags & PFRULE_DN_IS_PIPE) 2821 r.free_flags |= PFRULE_DN_IS_PIPE; 2822 else 2823 r.free_flags |= PFRULE_DN_IS_QUEUE; 2824 } 2825 2826 if ($9.marker & FOM_AFTO) { 2827 r.naf = $9.nat.af; 2828 2829 r.nat.opts = $9.nat.pool_opts.type; 2830 r.nat.opts |= $9.nat.pool_opts.opts; 2831 2832 if ((r.nat.opts & PF_POOL_TYPEMASK) != 2833 PF_POOL_ROUNDROBIN && 2834 disallow_table($9.nat.rdr->host, "tables are only " 2835 "supported in round-robin pools")) 2836 YYERROR; 2837 } 2838 2839 expand_rule(&r, $4, &$9.nat, &$9.rdr, $5.host, $9.nat.rdr ? $9.nat.rdr->host : NULL, 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 | STRING NUMBER { 4494 int i; 4495 4496 if ($2 < 0 || $2 > UINT_MAX) { 4497 yyerror("only positive values permitted"); 4498 YYERROR; 4499 } 4500 for (i = 0; pf_timeouts[i].name && 4501 strcmp(pf_timeouts[i].name, $1); ++i) 4502 ; /* nothing */ 4503 if (!pf_timeouts[i].name) { 4504 yyerror("illegal timeout name %s", $1); 4505 free($1); 4506 YYERROR; 4507 } 4508 if (strchr(pf_timeouts[i].name, '.') == NULL) { 4509 yyerror("illegal state timeout %s", $1); 4510 free($1); 4511 YYERROR; 4512 } 4513 free($1); 4514 $$ = calloc(1, sizeof(struct node_state_opt)); 4515 if ($$ == NULL) 4516 err(1, "state_opt_item: calloc"); 4517 $$->type = PF_STATE_OPT_TIMEOUT; 4518 $$->data.timeout.number = pf_timeouts[i].timeout; 4519 $$->data.timeout.seconds = $2; 4520 $$->next = NULL; 4521 $$->tail = $$; 4522 } 4523 ; 4524 4525 label : LABEL STRING { 4526 $$ = $2; 4527 } 4528 ; 4529 4530 etherqname : QUEUE STRING { 4531 $$.qname = $2; 4532 } 4533 | QUEUE '(' STRING ')' { 4534 $$.qname = $3; 4535 } 4536 ; 4537 4538 qname : QUEUE STRING { 4539 $$.qname = $2; 4540 $$.pqname = NULL; 4541 } 4542 | QUEUE '(' STRING ')' { 4543 $$.qname = $3; 4544 $$.pqname = NULL; 4545 } 4546 | QUEUE '(' STRING comma STRING ')' { 4547 $$.qname = $3; 4548 $$.pqname = $5; 4549 } 4550 ; 4551 4552 no : /* empty */ { $$ = 0; } 4553 | NO { $$ = 1; } 4554 ; 4555 4556 portstar : numberstring { 4557 if (parseport($1, &$$, PPORT_RANGE|PPORT_STAR) == -1) { 4558 free($1); 4559 YYERROR; 4560 } 4561 free($1); 4562 } 4563 ; 4564 4565 redirspec : host { $$ = $1; } 4566 | '{' optnl redir_host_list '}' { $$ = $3; } 4567 ; 4568 4569 redir_host_list : host optnl { $$ = $1; } 4570 | redir_host_list comma host optnl { 4571 $1->tail->next = $3; 4572 $1->tail = $3->tail; 4573 $$ = $1; 4574 } 4575 ; 4576 4577 redirpool : /* empty */ { $$ = NULL; } 4578 | ARROW redirspec { 4579 $$ = calloc(1, sizeof(struct redirection)); 4580 if ($$ == NULL) 4581 err(1, "redirection: calloc"); 4582 $$->host = $2; 4583 $$->rport.a = $$->rport.b = $$->rport.t = 0; 4584 } 4585 | ARROW redirspec PORT portstar { 4586 $$ = calloc(1, sizeof(struct redirection)); 4587 if ($$ == NULL) 4588 err(1, "redirection: calloc"); 4589 $$->host = $2; 4590 $$->rport = $4; 4591 } 4592 ; 4593 4594 hashkey : /* empty */ 4595 { 4596 $$ = calloc(1, sizeof(struct pf_poolhashkey)); 4597 if ($$ == NULL) 4598 err(1, "hashkey: calloc"); 4599 $$->key32[0] = arc4random(); 4600 $$->key32[1] = arc4random(); 4601 $$->key32[2] = arc4random(); 4602 $$->key32[3] = arc4random(); 4603 } 4604 | string 4605 { 4606 if (!strncmp($1, "0x", 2)) { 4607 if (strlen($1) != 34) { 4608 free($1); 4609 yyerror("hex key must be 128 bits " 4610 "(32 hex digits) long"); 4611 YYERROR; 4612 } 4613 $$ = calloc(1, sizeof(struct pf_poolhashkey)); 4614 if ($$ == NULL) 4615 err(1, "hashkey: calloc"); 4616 4617 if (sscanf($1, "0x%8x%8x%8x%8x", 4618 &$$->key32[0], &$$->key32[1], 4619 &$$->key32[2], &$$->key32[3]) != 4) { 4620 free($$); 4621 free($1); 4622 yyerror("invalid hex key"); 4623 YYERROR; 4624 } 4625 } else { 4626 MD5_CTX context; 4627 4628 $$ = calloc(1, sizeof(struct pf_poolhashkey)); 4629 if ($$ == NULL) 4630 err(1, "hashkey: calloc"); 4631 MD5Init(&context); 4632 MD5Update(&context, (unsigned char *)$1, 4633 strlen($1)); 4634 MD5Final((unsigned char *)$$, &context); 4635 HTONL($$->key32[0]); 4636 HTONL($$->key32[1]); 4637 HTONL($$->key32[2]); 4638 HTONL($$->key32[3]); 4639 } 4640 free($1); 4641 } 4642 ; 4643 4644 pool_opts : { bzero(&pool_opts, sizeof pool_opts); } 4645 pool_opts_l 4646 { $$ = pool_opts; } 4647 | /* empty */ { 4648 bzero(&pool_opts, sizeof pool_opts); 4649 $$ = pool_opts; 4650 } 4651 ; 4652 4653 pool_opts_l : pool_opts_l pool_opt 4654 | pool_opt 4655 ; 4656 4657 pool_opt : BITMASK { 4658 if (pool_opts.type) { 4659 yyerror("pool type cannot be redefined"); 4660 YYERROR; 4661 } 4662 pool_opts.type = PF_POOL_BITMASK; 4663 } 4664 | RANDOM { 4665 if (pool_opts.type) { 4666 yyerror("pool type cannot be redefined"); 4667 YYERROR; 4668 } 4669 pool_opts.type = PF_POOL_RANDOM; 4670 } 4671 | SOURCEHASH hashkey { 4672 if (pool_opts.type) { 4673 yyerror("pool type cannot be redefined"); 4674 YYERROR; 4675 } 4676 pool_opts.type = PF_POOL_SRCHASH; 4677 pool_opts.key = $2; 4678 } 4679 | ROUNDROBIN { 4680 if (pool_opts.type) { 4681 yyerror("pool type cannot be redefined"); 4682 YYERROR; 4683 } 4684 pool_opts.type = PF_POOL_ROUNDROBIN; 4685 } 4686 | STATICPORT { 4687 if (pool_opts.staticport) { 4688 yyerror("static-port cannot be redefined"); 4689 YYERROR; 4690 } 4691 pool_opts.staticport = 1; 4692 } 4693 | STICKYADDRESS { 4694 if (pool_opts.marker & POM_STICKYADDRESS) { 4695 yyerror("sticky-address cannot be redefined"); 4696 YYERROR; 4697 } 4698 pool_opts.marker |= POM_STICKYADDRESS; 4699 pool_opts.opts |= PF_POOL_STICKYADDR; 4700 } 4701 | ENDPI { 4702 if (pool_opts.marker & POM_ENDPI) { 4703 yyerror("endpoint-independent cannot be redefined"); 4704 YYERROR; 4705 } 4706 pool_opts.marker |= POM_ENDPI; 4707 pool_opts.opts |= PF_POOL_ENDPI; 4708 } 4709 | MAPEPORTSET number '/' number '/' number { 4710 if (pool_opts.mape.offset) { 4711 yyerror("map-e-portset cannot be redefined"); 4712 YYERROR; 4713 } 4714 if (pool_opts.type) { 4715 yyerror("map-e-portset cannot be used with " 4716 "address pools"); 4717 YYERROR; 4718 } 4719 if ($2 <= 0 || $2 >= 16) { 4720 yyerror("MAP-E PSID offset must be 1-15"); 4721 YYERROR; 4722 } 4723 if ($4 < 0 || $4 >= 16 || $2 + $4 > 16) { 4724 yyerror("Invalid MAP-E PSID length"); 4725 YYERROR; 4726 } else if ($4 == 0) { 4727 yyerror("PSID Length = 0: this means" 4728 " you do not need MAP-E"); 4729 YYERROR; 4730 } 4731 if ($6 < 0 || $6 > 65535) { 4732 yyerror("Invalid MAP-E PSID"); 4733 YYERROR; 4734 } 4735 pool_opts.mape.offset = $2; 4736 pool_opts.mape.psidlen = $4; 4737 pool_opts.mape.psid = $6; 4738 } 4739 ; 4740 4741 redirection : /* empty */ { $$ = NULL; } 4742 | ARROW host { 4743 $$ = calloc(1, sizeof(struct redirection)); 4744 if ($$ == NULL) 4745 err(1, "redirection: calloc"); 4746 $$->host = $2; 4747 $$->rport.a = $$->rport.b = $$->rport.t = 0; 4748 } 4749 | ARROW host PORT portstar { 4750 $$ = calloc(1, sizeof(struct redirection)); 4751 if ($$ == NULL) 4752 err(1, "redirection: calloc"); 4753 $$->host = $2; 4754 $$->rport = $4; 4755 } 4756 ; 4757 4758 natpasslog : /* empty */ { $$.b1 = $$.b2 = 0; $$.w2 = 0; } 4759 | PASS { $$.b1 = 1; $$.b2 = 0; $$.w2 = 0; } 4760 | PASS log { $$.b1 = 1; $$.b2 = $2.log; $$.w2 = $2.logif; } 4761 | log { $$.b1 = 0; $$.b2 = $1.log; $$.w2 = $1.logif; } 4762 ; 4763 4764 nataction : no NAT natpasslog { 4765 if ($1 && $3.b1) { 4766 yyerror("\"pass\" not valid with \"no\""); 4767 YYERROR; 4768 } 4769 if ($1) 4770 $$.b1 = PF_NONAT; 4771 else 4772 $$.b1 = PF_NAT; 4773 $$.b2 = $3.b1; 4774 $$.w = $3.b2; 4775 $$.w2 = $3.w2; 4776 } 4777 | no RDR natpasslog { 4778 if ($1 && $3.b1) { 4779 yyerror("\"pass\" not valid with \"no\""); 4780 YYERROR; 4781 } 4782 if ($1) 4783 $$.b1 = PF_NORDR; 4784 else 4785 $$.b1 = PF_RDR; 4786 $$.b2 = $3.b1; 4787 $$.w = $3.b2; 4788 $$.w2 = $3.w2; 4789 } 4790 ; 4791 4792 natrule : nataction interface af proto fromto tag tagged rtable 4793 redirpool pool_opts 4794 { 4795 struct pfctl_rule r; 4796 struct node_state_opt *o; 4797 4798 if (check_rulestate(PFCTL_STATE_NAT)) 4799 YYERROR; 4800 4801 memset(&r, 0, sizeof(r)); 4802 4803 r.action = $1.b1; 4804 r.natpass = $1.b2; 4805 r.log = $1.w; 4806 r.logif = $1.w2; 4807 r.af = $3; 4808 4809 if (!r.af) { 4810 if ($5.src.host && $5.src.host->af && 4811 !$5.src.host->ifindex) 4812 r.af = $5.src.host->af; 4813 else if ($5.dst.host && $5.dst.host->af && 4814 !$5.dst.host->ifindex) 4815 r.af = $5.dst.host->af; 4816 } 4817 4818 if ($6 != NULL) 4819 if (strlcpy(r.tagname, $6, PF_TAG_NAME_SIZE) >= 4820 PF_TAG_NAME_SIZE) { 4821 yyerror("tag too long, max %u chars", 4822 PF_TAG_NAME_SIZE - 1); 4823 YYERROR; 4824 } 4825 4826 if ($7.name) 4827 if (strlcpy(r.match_tagname, $7.name, 4828 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) { 4829 yyerror("tag too long, max %u chars", 4830 PF_TAG_NAME_SIZE - 1); 4831 YYERROR; 4832 } 4833 r.match_tag_not = $7.neg; 4834 r.rtableid = $8; 4835 4836 if (r.action == PF_NONAT || r.action == PF_NORDR) { 4837 if ($9 != NULL) { 4838 yyerror("translation rule with 'no' " 4839 "does not need '->'"); 4840 YYERROR; 4841 } 4842 } else { 4843 if ($9 == NULL || $9->host == NULL) { 4844 yyerror("translation rule requires '-> " 4845 "address'"); 4846 YYERROR; 4847 } 4848 if (!r.af && ! $9->host->ifindex) 4849 r.af = $9->host->af; 4850 4851 remove_invalid_hosts(&$9->host, &r.af); 4852 if (invalid_redirect($9->host, r.af)) 4853 YYERROR; 4854 if ($9->host->addr.type == PF_ADDR_DYNIFTL) { 4855 if (($9->host = gen_dynnode($9->host, r.af)) == NULL) 4856 err(1, "calloc"); 4857 } 4858 if (check_netmask($9->host, r.af)) 4859 YYERROR; 4860 4861 r.rpool.proxy_port[0] = ntohs($9->rport.a); 4862 4863 switch (r.action) { 4864 case PF_RDR: 4865 if (!$9->rport.b && $9->rport.t && 4866 $5.dst.port != NULL) { 4867 r.rpool.proxy_port[1] = 4868 ntohs($9->rport.a) + 4869 (ntohs( 4870 $5.dst.port->port[1]) - 4871 ntohs( 4872 $5.dst.port->port[0])); 4873 } else 4874 r.rpool.proxy_port[1] = 4875 ntohs($9->rport.b); 4876 break; 4877 case PF_NAT: 4878 r.rpool.proxy_port[1] = 4879 ntohs($9->rport.b); 4880 if (!r.rpool.proxy_port[0] && 4881 !r.rpool.proxy_port[1]) { 4882 r.rpool.proxy_port[0] = 4883 PF_NAT_PROXY_PORT_LOW; 4884 r.rpool.proxy_port[1] = 4885 PF_NAT_PROXY_PORT_HIGH; 4886 } else if (!r.rpool.proxy_port[1]) 4887 r.rpool.proxy_port[1] = 4888 r.rpool.proxy_port[0]; 4889 break; 4890 default: 4891 break; 4892 } 4893 4894 r.rpool.opts = $10.type; 4895 if ((r.rpool.opts & PF_POOL_TYPEMASK) == 4896 PF_POOL_NONE && ($9->host->next != NULL || 4897 $9->host->addr.type == PF_ADDR_TABLE || 4898 DYNIF_MULTIADDR($9->host->addr))) 4899 r.rpool.opts = PF_POOL_ROUNDROBIN; 4900 if ((r.rpool.opts & PF_POOL_TYPEMASK) != 4901 PF_POOL_ROUNDROBIN && 4902 disallow_table($9->host, "tables are only " 4903 "supported in round-robin redirection " 4904 "pools")) 4905 YYERROR; 4906 if ((r.rpool.opts & PF_POOL_TYPEMASK) != 4907 PF_POOL_ROUNDROBIN && 4908 disallow_alias($9->host, "interface (%s) " 4909 "is only supported in round-robin " 4910 "redirection pools")) 4911 YYERROR; 4912 if ($9->host->next != NULL) { 4913 if ((r.rpool.opts & PF_POOL_TYPEMASK) != 4914 PF_POOL_ROUNDROBIN) { 4915 yyerror("only round-robin " 4916 "valid for multiple " 4917 "redirection addresses"); 4918 YYERROR; 4919 } 4920 } 4921 } 4922 4923 if ($10.key != NULL) 4924 memcpy(&r.rpool.key, $10.key, 4925 sizeof(struct pf_poolhashkey)); 4926 4927 if ($10.opts) 4928 r.rpool.opts |= $10.opts; 4929 4930 if ($10.staticport) { 4931 if (r.action != PF_NAT) { 4932 yyerror("the 'static-port' option is " 4933 "only valid with nat rules"); 4934 YYERROR; 4935 } 4936 if (r.rpool.proxy_port[0] != 4937 PF_NAT_PROXY_PORT_LOW && 4938 r.rpool.proxy_port[1] != 4939 PF_NAT_PROXY_PORT_HIGH) { 4940 yyerror("the 'static-port' option can't" 4941 " be used when specifying a port" 4942 " range"); 4943 YYERROR; 4944 } 4945 r.rpool.proxy_port[0] = 0; 4946 r.rpool.proxy_port[1] = 0; 4947 } 4948 4949 if ($10.mape.offset) { 4950 if (r.action != PF_NAT) { 4951 yyerror("the 'map-e-portset' option is" 4952 " only valid with nat rules"); 4953 YYERROR; 4954 } 4955 if ($10.staticport) { 4956 yyerror("the 'map-e-portset' option" 4957 " can't be used 'static-port'"); 4958 YYERROR; 4959 } 4960 if (r.rpool.proxy_port[0] != 4961 PF_NAT_PROXY_PORT_LOW && 4962 r.rpool.proxy_port[1] != 4963 PF_NAT_PROXY_PORT_HIGH) { 4964 yyerror("the 'map-e-portset' option" 4965 " can't be used when specifying" 4966 " a port range"); 4967 YYERROR; 4968 } 4969 r.rpool.mape = $10.mape; 4970 } 4971 4972 o = keep_state_defaults; 4973 while (o) { 4974 switch (o->type) { 4975 case PF_STATE_OPT_PFLOW: 4976 if (r.rule_flag & PFRULE_PFLOW) { 4977 yyerror("state pflow option: " 4978 "multiple definitions"); 4979 YYERROR; 4980 } 4981 r.rule_flag |= PFRULE_PFLOW; 4982 break; 4983 } 4984 o = o->next; 4985 } 4986 4987 expand_rule(&r, $2, NULL, NULL, $9 == NULL ? NULL : $9->host, 4988 NULL, $4, $5.src_os, $5.src.host, $5.src.port, $5.dst.host, 4989 $5.dst.port, 0, 0, 0, 0, ""); 4990 free($9); 4991 } 4992 ; 4993 4994 binatrule : no BINAT natpasslog interface af proto FROM ipspec toipspec tag 4995 tagged rtable redirection 4996 { 4997 struct pfctl_rule binat; 4998 struct pf_pooladdr *pa; 4999 5000 if (check_rulestate(PFCTL_STATE_NAT)) 5001 YYERROR; 5002 if (disallow_urpf_failed($9, "\"urpf-failed\" is not " 5003 "permitted as a binat destination")) 5004 YYERROR; 5005 5006 memset(&binat, 0, sizeof(binat)); 5007 5008 if ($1 && $3.b1) { 5009 yyerror("\"pass\" not valid with \"no\""); 5010 YYERROR; 5011 } 5012 if ($1) 5013 binat.action = PF_NOBINAT; 5014 else 5015 binat.action = PF_BINAT; 5016 binat.natpass = $3.b1; 5017 binat.log = $3.b2; 5018 binat.logif = $3.w2; 5019 binat.af = $5; 5020 if (!binat.af && $8 != NULL && $8->af) 5021 binat.af = $8->af; 5022 if (!binat.af && $9 != NULL && $9->af) 5023 binat.af = $9->af; 5024 5025 if (!binat.af && $13 != NULL && $13->host) 5026 binat.af = $13->host->af; 5027 if (!binat.af) { 5028 yyerror("address family (inet/inet6) " 5029 "undefined"); 5030 YYERROR; 5031 } 5032 5033 if ($4 != NULL) { 5034 memcpy(binat.ifname, $4->ifname, 5035 sizeof(binat.ifname)); 5036 binat.ifnot = $4->not; 5037 free($4); 5038 } 5039 5040 if ($10 != NULL) 5041 if (strlcpy(binat.tagname, $10, 5042 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) { 5043 yyerror("tag too long, max %u chars", 5044 PF_TAG_NAME_SIZE - 1); 5045 YYERROR; 5046 } 5047 if ($11.name) 5048 if (strlcpy(binat.match_tagname, $11.name, 5049 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) { 5050 yyerror("tag too long, max %u chars", 5051 PF_TAG_NAME_SIZE - 1); 5052 YYERROR; 5053 } 5054 binat.match_tag_not = $11.neg; 5055 binat.rtableid = $12; 5056 5057 if ($6 != NULL) { 5058 binat.proto = $6->proto; 5059 free($6); 5060 } 5061 5062 if ($8 != NULL && disallow_table($8, "invalid use of " 5063 "table <%s> as the source address of a binat rule")) 5064 YYERROR; 5065 if ($8 != NULL && disallow_alias($8, "invalid use of " 5066 "interface (%s) as the source address of a binat " 5067 "rule")) 5068 YYERROR; 5069 if ($13 != NULL && $13->host != NULL && disallow_table( 5070 $13->host, "invalid use of table <%s> as the " 5071 "redirect address of a binat rule")) 5072 YYERROR; 5073 if ($13 != NULL && $13->host != NULL && disallow_alias( 5074 $13->host, "invalid use of interface (%s) as the " 5075 "redirect address of a binat rule")) 5076 YYERROR; 5077 5078 if ($8 != NULL) { 5079 if ($8->next) { 5080 yyerror("multiple binat ip addresses"); 5081 YYERROR; 5082 } 5083 if ($8->addr.type == PF_ADDR_DYNIFTL) 5084 $8->af = binat.af; 5085 if ($8->af != binat.af) { 5086 yyerror("binat ip versions must match"); 5087 YYERROR; 5088 } 5089 if ($8->addr.type == PF_ADDR_DYNIFTL) { 5090 if (($8 = gen_dynnode($8, binat.af)) == NULL) 5091 err(1, "calloc"); 5092 } 5093 if (check_netmask($8, binat.af)) 5094 YYERROR; 5095 memcpy(&binat.src.addr, &$8->addr, 5096 sizeof(binat.src.addr)); 5097 free($8); 5098 } 5099 if ($9 != NULL) { 5100 if ($9->next) { 5101 yyerror("multiple binat ip addresses"); 5102 YYERROR; 5103 } 5104 if ($9->af != binat.af && $9->af) { 5105 yyerror("binat ip versions must match"); 5106 YYERROR; 5107 } 5108 if ($9->addr.type == PF_ADDR_DYNIFTL) { 5109 if (($9 = gen_dynnode($9, binat.af)) == NULL) 5110 err(1, "calloc"); 5111 } 5112 if (check_netmask($9, binat.af)) 5113 YYERROR; 5114 memcpy(&binat.dst.addr, &$9->addr, 5115 sizeof(binat.dst.addr)); 5116 binat.dst.neg = $9->not; 5117 free($9); 5118 } 5119 5120 if (binat.action == PF_NOBINAT) { 5121 if ($13 != NULL) { 5122 yyerror("'no binat' rule does not need" 5123 " '->'"); 5124 YYERROR; 5125 } 5126 } else { 5127 if ($13 == NULL || $13->host == NULL) { 5128 yyerror("'binat' rule requires" 5129 " '-> address'"); 5130 YYERROR; 5131 } 5132 5133 remove_invalid_hosts(&$13->host, &binat.af); 5134 if (invalid_redirect($13->host, binat.af)) 5135 YYERROR; 5136 if ($13->host->next != NULL) { 5137 yyerror("binat rule must redirect to " 5138 "a single address"); 5139 YYERROR; 5140 } 5141 if ($13->host->addr.type == PF_ADDR_DYNIFTL) { 5142 if (($13->host = gen_dynnode($13->host, binat.af)) == NULL) 5143 err(1, "calloc"); 5144 } 5145 if (check_netmask($13->host, binat.af)) 5146 YYERROR; 5147 5148 if (!PF_AZERO(&binat.src.addr.v.a.mask, 5149 binat.af) && 5150 !PF_AEQ(&binat.src.addr.v.a.mask, 5151 &$13->host->addr.v.a.mask, binat.af)) { 5152 yyerror("'binat' source mask and " 5153 "redirect mask must be the same"); 5154 YYERROR; 5155 } 5156 5157 TAILQ_INIT(&binat.rpool.list); 5158 pa = calloc(1, sizeof(struct pf_pooladdr)); 5159 if (pa == NULL) 5160 err(1, "binat: calloc"); 5161 pa->addr = $13->host->addr; 5162 pa->ifname[0] = 0; 5163 TAILQ_INSERT_TAIL(&binat.rpool.list, 5164 pa, entries); 5165 5166 free($13); 5167 } 5168 5169 pfctl_append_rule(pf, &binat, ""); 5170 } 5171 ; 5172 5173 tag : /* empty */ { $$ = NULL; } 5174 | TAG STRING { $$ = $2; } 5175 ; 5176 5177 tagged : /* empty */ { $$.neg = 0; $$.name = NULL; } 5178 | not TAGGED string { $$.neg = $1; $$.name = $3; } 5179 ; 5180 5181 rtable : /* empty */ { $$ = -1; } 5182 | RTABLE NUMBER { 5183 if ($2 < 0 || $2 > rt_tableid_max()) { 5184 yyerror("invalid rtable id"); 5185 YYERROR; 5186 } 5187 $$ = $2; 5188 } 5189 ; 5190 5191 route_host : STRING { 5192 $$ = calloc(1, sizeof(struct node_host)); 5193 if ($$ == NULL) 5194 err(1, "route_host: calloc"); 5195 if (strlen($1) >= IFNAMSIZ) { 5196 yyerror("interface name too long"); 5197 YYERROR; 5198 } 5199 $$->ifname = strdup($1); 5200 set_ipmask($$, 128); 5201 $$->next = NULL; 5202 $$->tail = $$; 5203 } 5204 | '(' STRING host ')' { 5205 struct node_host *n; 5206 5207 $$ = $3; 5208 for (n = $3; n != NULL; n = n->next) { 5209 if (strlen($2) >= IFNAMSIZ) { 5210 yyerror("interface name too long"); 5211 YYERROR; 5212 } 5213 n->ifname = strdup($2); 5214 } 5215 } 5216 ; 5217 5218 route_host_list : route_host optnl { $$ = $1; } 5219 | route_host_list comma route_host optnl { 5220 if ($1->af == 0) 5221 $1->af = $3->af; 5222 if ($1->af != $3->af) { 5223 yyerror("all pool addresses must be in the " 5224 "same address family"); 5225 YYERROR; 5226 } 5227 $1->tail->next = $3; 5228 $1->tail = $3->tail; 5229 $$ = $1; 5230 } 5231 ; 5232 5233 routespec : route_host { $$ = $1; } 5234 | '{' optnl route_host_list '}' { $$ = $3; } 5235 ; 5236 5237 route : /* empty */ { 5238 $$.host = NULL; 5239 $$.rt = 0; 5240 $$.pool_opts = 0; 5241 } 5242 | FASTROUTE { 5243 /* backwards-compat */ 5244 $$.host = NULL; 5245 $$.rt = 0; 5246 $$.pool_opts = 0; 5247 } 5248 | ROUTETO routespec pool_opts { 5249 $$.host = $2; 5250 $$.rt = PF_ROUTETO; 5251 $$.pool_opts = $3.type | $3.opts; 5252 if ($3.key != NULL) 5253 $$.key = $3.key; 5254 } 5255 | REPLYTO routespec pool_opts { 5256 $$.host = $2; 5257 $$.rt = PF_REPLYTO; 5258 $$.pool_opts = $3.type | $3.opts; 5259 if ($3.key != NULL) 5260 $$.key = $3.key; 5261 } 5262 | DUPTO routespec pool_opts { 5263 $$.host = $2; 5264 $$.rt = PF_DUPTO; 5265 $$.pool_opts = $3.type | $3.opts; 5266 if ($3.key != NULL) 5267 $$.key = $3.key; 5268 } 5269 ; 5270 5271 timeout_spec : STRING NUMBER 5272 { 5273 if (check_rulestate(PFCTL_STATE_OPTION)) { 5274 free($1); 5275 YYERROR; 5276 } 5277 if ($2 < 0 || $2 > UINT_MAX) { 5278 yyerror("only positive values permitted"); 5279 YYERROR; 5280 } 5281 if (pfctl_apply_timeout(pf, $1, $2, 0) != 0) { 5282 yyerror("unknown timeout %s", $1); 5283 free($1); 5284 YYERROR; 5285 } 5286 free($1); 5287 } 5288 | INTERVAL NUMBER { 5289 if (check_rulestate(PFCTL_STATE_OPTION)) 5290 YYERROR; 5291 if ($2 < 0 || $2 > UINT_MAX) { 5292 yyerror("only positive values permitted"); 5293 YYERROR; 5294 } 5295 if (pfctl_apply_timeout(pf, "interval", $2, 0) != 0) 5296 YYERROR; 5297 } 5298 ; 5299 5300 timeout_list : timeout_list comma timeout_spec optnl 5301 | timeout_spec optnl 5302 ; 5303 5304 limit_spec : STRING NUMBER 5305 { 5306 if (check_rulestate(PFCTL_STATE_OPTION)) { 5307 free($1); 5308 YYERROR; 5309 } 5310 if ($2 < 0 || $2 > UINT_MAX) { 5311 yyerror("only positive values permitted"); 5312 YYERROR; 5313 } 5314 if (pfctl_apply_limit(pf, $1, $2) != 0) { 5315 yyerror("unable to set limit %s %u", $1, $2); 5316 free($1); 5317 YYERROR; 5318 } 5319 free($1); 5320 } 5321 ; 5322 5323 limit_list : limit_list comma limit_spec optnl 5324 | limit_spec optnl 5325 ; 5326 5327 comma : ',' 5328 | /* empty */ 5329 ; 5330 5331 yesno : NO { $$ = 0; } 5332 | STRING { 5333 if (!strcmp($1, "yes")) 5334 $$ = 1; 5335 else { 5336 yyerror("invalid value '%s', expected 'yes' " 5337 "or 'no'", $1); 5338 free($1); 5339 YYERROR; 5340 } 5341 free($1); 5342 } 5343 ; 5344 5345 unaryop : '=' { $$ = PF_OP_EQ; } 5346 | NE { $$ = PF_OP_NE; } 5347 | LE { $$ = PF_OP_LE; } 5348 | '<' { $$ = PF_OP_LT; } 5349 | GE { $$ = PF_OP_GE; } 5350 | '>' { $$ = PF_OP_GT; } 5351 ; 5352 5353 %% 5354 5355 int 5356 yyerror(const char *fmt, ...) 5357 { 5358 va_list ap; 5359 5360 file->errors++; 5361 va_start(ap, fmt); 5362 fprintf(stderr, "%s:%d: ", file->name, yylval.lineno); 5363 vfprintf(stderr, fmt, ap); 5364 fprintf(stderr, "\n"); 5365 va_end(ap); 5366 return (0); 5367 } 5368 5369 int 5370 disallow_table(struct node_host *h, const char *fmt) 5371 { 5372 for (; h != NULL; h = h->next) 5373 if (h->addr.type == PF_ADDR_TABLE) { 5374 yyerror(fmt, h->addr.v.tblname); 5375 return (1); 5376 } 5377 return (0); 5378 } 5379 5380 int 5381 disallow_urpf_failed(struct node_host *h, const char *fmt) 5382 { 5383 for (; h != NULL; h = h->next) 5384 if (h->addr.type == PF_ADDR_URPFFAILED) { 5385 yyerror(fmt); 5386 return (1); 5387 } 5388 return (0); 5389 } 5390 5391 int 5392 disallow_alias(struct node_host *h, const char *fmt) 5393 { 5394 for (; h != NULL; h = h->next) 5395 if (DYNIF_MULTIADDR(h->addr)) { 5396 yyerror(fmt, h->addr.v.tblname); 5397 return (1); 5398 } 5399 return (0); 5400 } 5401 5402 int 5403 rule_consistent(struct pfctl_rule *r, int anchor_call) 5404 { 5405 int problems = 0; 5406 5407 switch (r->action) { 5408 case PF_PASS: 5409 case PF_MATCH: 5410 case PF_DROP: 5411 case PF_SCRUB: 5412 case PF_NOSCRUB: 5413 problems = filter_consistent(r, anchor_call); 5414 break; 5415 case PF_NAT: 5416 case PF_NONAT: 5417 problems = nat_consistent(r); 5418 break; 5419 case PF_RDR: 5420 case PF_NORDR: 5421 problems = rdr_consistent(r); 5422 break; 5423 case PF_BINAT: 5424 case PF_NOBINAT: 5425 default: 5426 break; 5427 } 5428 return (problems); 5429 } 5430 5431 int 5432 filter_consistent(struct pfctl_rule *r, int anchor_call) 5433 { 5434 int problems = 0; 5435 5436 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP && 5437 r->proto != IPPROTO_SCTP && 5438 (r->src.port_op || r->dst.port_op)) { 5439 yyerror("port only applies to tcp/udp/sctp"); 5440 problems++; 5441 } 5442 if (r->proto != IPPROTO_ICMP && r->proto != IPPROTO_ICMPV6 && 5443 (r->type || r->code)) { 5444 yyerror("icmp-type/code only applies to icmp"); 5445 problems++; 5446 } 5447 if (!r->af && (r->type || r->code)) { 5448 yyerror("must indicate address family with icmp-type/code"); 5449 problems++; 5450 } 5451 if (r->overload_tblname[0] && 5452 r->max_src_conn == 0 && r->max_src_conn_rate.seconds == 0) { 5453 yyerror("'overload' requires 'max-src-conn' " 5454 "or 'max-src-conn-rate'"); 5455 problems++; 5456 } 5457 if ((r->proto == IPPROTO_ICMP && r->af == AF_INET6) || 5458 (r->proto == IPPROTO_ICMPV6 && r->af == AF_INET)) { 5459 yyerror("proto %s doesn't match address family %s", 5460 r->proto == IPPROTO_ICMP ? "icmp" : "icmp6", 5461 r->af == AF_INET ? "inet" : "inet6"); 5462 problems++; 5463 } 5464 if (r->allow_opts && r->action != PF_PASS) { 5465 yyerror("allow-opts can only be specified for pass rules"); 5466 problems++; 5467 } 5468 if (r->rule_flag & PFRULE_FRAGMENT && (r->src.port_op || 5469 r->dst.port_op || r->flagset || r->type || r->code)) { 5470 yyerror("fragments can be filtered only on IP header fields"); 5471 problems++; 5472 } 5473 if (r->rule_flag & PFRULE_RETURNRST && r->proto != IPPROTO_TCP) { 5474 yyerror("return-rst can only be applied to TCP rules"); 5475 problems++; 5476 } 5477 if (r->max_src_nodes && !(r->rule_flag & PFRULE_RULESRCTRACK)) { 5478 yyerror("max-src-nodes requires 'source-track rule'"); 5479 problems++; 5480 } 5481 if (r->action != PF_PASS && r->keep_state) { 5482 yyerror("keep state is great, but only for pass rules"); 5483 problems++; 5484 } 5485 if (r->rule_flag & PFRULE_STATESLOPPY && 5486 (r->keep_state == PF_STATE_MODULATE || 5487 r->keep_state == PF_STATE_SYNPROXY)) { 5488 yyerror("sloppy state matching cannot be used with " 5489 "synproxy state or modulate state"); 5490 problems++; 5491 } 5492 /* match rules rules */ 5493 if (r->action == PF_MATCH) { 5494 if (r->divert.port) { 5495 yyerror("divert is not supported on match rules"); 5496 problems++; 5497 } 5498 if (r->rt) { 5499 yyerror("route-to, reply-to, dup-to and fastroute " 5500 "must not be used on match rules"); 5501 problems++; 5502 } 5503 if (r->rule_flag & PFRULE_AFTO) { 5504 yyerror("af-to is not supported on match rules"); 5505 problems++; 5506 } 5507 } 5508 if (r->rpool.opts & PF_POOL_STICKYADDR && !r->keep_state) { 5509 yyerror("'sticky-address' requires 'keep state'"); 5510 problems++; 5511 } 5512 return (-problems); 5513 } 5514 5515 int 5516 nat_consistent(struct pfctl_rule *r) 5517 { 5518 return (0); /* yeah! */ 5519 } 5520 5521 int 5522 rdr_consistent(struct pfctl_rule *r) 5523 { 5524 int problems = 0; 5525 5526 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP && 5527 r->proto != IPPROTO_SCTP) { 5528 if (r->src.port_op) { 5529 yyerror("src port only applies to tcp/udp/sctp"); 5530 problems++; 5531 } 5532 if (r->dst.port_op) { 5533 yyerror("dst port only applies to tcp/udp/sctp"); 5534 problems++; 5535 } 5536 if (r->rpool.proxy_port[0]) { 5537 yyerror("rpool port only applies to tcp/udp/sctp"); 5538 problems++; 5539 } 5540 } 5541 if (r->dst.port_op && 5542 r->dst.port_op != PF_OP_EQ && r->dst.port_op != PF_OP_RRG) { 5543 yyerror("invalid port operator for rdr destination port"); 5544 problems++; 5545 } 5546 return (-problems); 5547 } 5548 5549 int 5550 process_tabledef(char *name, struct table_opts *opts) 5551 { 5552 struct pfr_buffer ab; 5553 struct node_tinit *ti; 5554 unsigned long maxcount; 5555 size_t s = sizeof(maxcount); 5556 5557 bzero(&ab, sizeof(ab)); 5558 ab.pfrb_type = PFRB_ADDRS; 5559 SIMPLEQ_FOREACH(ti, &opts->init_nodes, entries) { 5560 if (ti->file) 5561 if (pfr_buf_load(&ab, ti->file, 0, append_addr)) { 5562 if (errno) 5563 yyerror("cannot load \"%s\": %s", 5564 ti->file, strerror(errno)); 5565 else 5566 yyerror("file \"%s\" contains bad data", 5567 ti->file); 5568 goto _error; 5569 } 5570 if (ti->host) 5571 if (append_addr_host(&ab, ti->host, 0, 0)) { 5572 yyerror("cannot create address buffer: %s", 5573 strerror(errno)); 5574 goto _error; 5575 } 5576 } 5577 if (pf->opts & PF_OPT_VERBOSE) 5578 print_tabledef(name, opts->flags, opts->init_addr, 5579 &opts->init_nodes); 5580 if (!(pf->opts & PF_OPT_NOACTION) && 5581 pfctl_define_table(name, opts->flags, opts->init_addr, 5582 pf->anchor->name, &ab, pf->anchor->ruleset.tticket)) { 5583 5584 if (sysctlbyname("net.pf.request_maxcount", &maxcount, &s, 5585 NULL, 0) == -1) 5586 maxcount = 65535; 5587 5588 if (ab.pfrb_size > maxcount) 5589 yyerror("cannot define table %s: too many elements.\n" 5590 "Consider increasing net.pf.request_maxcount.", 5591 name); 5592 else 5593 yyerror("cannot define table %s: %s", name, 5594 pfr_strerror(errno)); 5595 5596 goto _error; 5597 } 5598 pf->tdirty = 1; 5599 pfr_buf_clear(&ab); 5600 return (0); 5601 _error: 5602 pfr_buf_clear(&ab); 5603 return (-1); 5604 } 5605 5606 struct keywords { 5607 const char *k_name; 5608 int k_val; 5609 }; 5610 5611 /* macro gore, but you should've seen the prior indentation nightmare... */ 5612 5613 #define FREE_LIST(T,r) \ 5614 do { \ 5615 T *p, *node = r; \ 5616 while (node != NULL) { \ 5617 p = node; \ 5618 node = node->next; \ 5619 free(p); \ 5620 } \ 5621 } while (0) 5622 5623 #define LOOP_THROUGH(T,n,r,C) \ 5624 do { \ 5625 T *n; \ 5626 if (r == NULL) { \ 5627 r = calloc(1, sizeof(T)); \ 5628 if (r == NULL) \ 5629 err(1, "LOOP: calloc"); \ 5630 r->next = NULL; \ 5631 } \ 5632 n = r; \ 5633 while (n != NULL) { \ 5634 do { \ 5635 C; \ 5636 } while (0); \ 5637 n = n->next; \ 5638 } \ 5639 } while (0) 5640 5641 void 5642 expand_label_str(char *label, size_t len, const char *srch, const char *repl) 5643 { 5644 char *tmp; 5645 char *p, *q; 5646 5647 if ((tmp = calloc(1, len)) == NULL) 5648 err(1, "expand_label_str: calloc"); 5649 p = q = label; 5650 while ((q = strstr(p, srch)) != NULL) { 5651 *q = '\0'; 5652 if ((strlcat(tmp, p, len) >= len) || 5653 (strlcat(tmp, repl, len) >= len)) 5654 errx(1, "expand_label: label too long"); 5655 q += strlen(srch); 5656 p = q; 5657 } 5658 if (strlcat(tmp, p, len) >= len) 5659 errx(1, "expand_label: label too long"); 5660 strlcpy(label, tmp, len); /* always fits */ 5661 free(tmp); 5662 } 5663 5664 void 5665 expand_label_if(const char *name, char *label, size_t len, const char *ifname) 5666 { 5667 if (strstr(label, name) != NULL) { 5668 if (!*ifname) 5669 expand_label_str(label, len, name, "any"); 5670 else 5671 expand_label_str(label, len, name, ifname); 5672 } 5673 } 5674 5675 void 5676 expand_label_addr(const char *name, char *label, size_t len, sa_family_t af, 5677 struct pf_rule_addr *addr) 5678 { 5679 char tmp[64], tmp_not[66]; 5680 5681 if (strstr(label, name) != NULL) { 5682 switch (addr->addr.type) { 5683 case PF_ADDR_DYNIFTL: 5684 snprintf(tmp, sizeof(tmp), "(%s)", addr->addr.v.ifname); 5685 break; 5686 case PF_ADDR_TABLE: 5687 snprintf(tmp, sizeof(tmp), "<%s>", addr->addr.v.tblname); 5688 break; 5689 case PF_ADDR_NOROUTE: 5690 snprintf(tmp, sizeof(tmp), "no-route"); 5691 break; 5692 case PF_ADDR_URPFFAILED: 5693 snprintf(tmp, sizeof(tmp), "urpf-failed"); 5694 break; 5695 case PF_ADDR_ADDRMASK: 5696 if (!af || (PF_AZERO(&addr->addr.v.a.addr, af) && 5697 PF_AZERO(&addr->addr.v.a.mask, af))) 5698 snprintf(tmp, sizeof(tmp), "any"); 5699 else { 5700 char a[48]; 5701 int bits; 5702 5703 if (inet_ntop(af, &addr->addr.v.a.addr, a, 5704 sizeof(a)) == NULL) 5705 snprintf(tmp, sizeof(tmp), "?"); 5706 else { 5707 bits = unmask(&addr->addr.v.a.mask, af); 5708 if ((af == AF_INET && bits < 32) || 5709 (af == AF_INET6 && bits < 128)) 5710 snprintf(tmp, sizeof(tmp), 5711 "%s/%d", a, bits); 5712 else 5713 snprintf(tmp, sizeof(tmp), 5714 "%s", a); 5715 } 5716 } 5717 break; 5718 default: 5719 snprintf(tmp, sizeof(tmp), "?"); 5720 break; 5721 } 5722 5723 if (addr->neg) { 5724 snprintf(tmp_not, sizeof(tmp_not), "! %s", tmp); 5725 expand_label_str(label, len, name, tmp_not); 5726 } else 5727 expand_label_str(label, len, name, tmp); 5728 } 5729 } 5730 5731 void 5732 expand_label_port(const char *name, char *label, size_t len, 5733 struct pf_rule_addr *addr) 5734 { 5735 char a1[6], a2[6], op[13] = ""; 5736 5737 if (strstr(label, name) != NULL) { 5738 snprintf(a1, sizeof(a1), "%u", ntohs(addr->port[0])); 5739 snprintf(a2, sizeof(a2), "%u", ntohs(addr->port[1])); 5740 if (!addr->port_op) 5741 ; 5742 else if (addr->port_op == PF_OP_IRG) 5743 snprintf(op, sizeof(op), "%s><%s", a1, a2); 5744 else if (addr->port_op == PF_OP_XRG) 5745 snprintf(op, sizeof(op), "%s<>%s", a1, a2); 5746 else if (addr->port_op == PF_OP_EQ) 5747 snprintf(op, sizeof(op), "%s", a1); 5748 else if (addr->port_op == PF_OP_NE) 5749 snprintf(op, sizeof(op), "!=%s", a1); 5750 else if (addr->port_op == PF_OP_LT) 5751 snprintf(op, sizeof(op), "<%s", a1); 5752 else if (addr->port_op == PF_OP_LE) 5753 snprintf(op, sizeof(op), "<=%s", a1); 5754 else if (addr->port_op == PF_OP_GT) 5755 snprintf(op, sizeof(op), ">%s", a1); 5756 else if (addr->port_op == PF_OP_GE) 5757 snprintf(op, sizeof(op), ">=%s", a1); 5758 expand_label_str(label, len, name, op); 5759 } 5760 } 5761 5762 void 5763 expand_label_proto(const char *name, char *label, size_t len, u_int8_t proto) 5764 { 5765 const char *protoname; 5766 char n[4]; 5767 5768 if (strstr(label, name) != NULL) { 5769 protoname = pfctl_proto2name(proto); 5770 if (protoname != NULL) 5771 expand_label_str(label, len, name, protoname); 5772 else { 5773 snprintf(n, sizeof(n), "%u", proto); 5774 expand_label_str(label, len, name, n); 5775 } 5776 } 5777 } 5778 5779 void 5780 expand_label_nr(const char *name, char *label, size_t len, 5781 struct pfctl_rule *r) 5782 { 5783 char n[11]; 5784 5785 if (strstr(label, name) != NULL) { 5786 snprintf(n, sizeof(n), "%u", r->nr); 5787 expand_label_str(label, len, name, n); 5788 } 5789 } 5790 5791 void 5792 expand_label(char *label, size_t len, struct pfctl_rule *r) 5793 { 5794 expand_label_if("$if", label, len, r->ifname); 5795 expand_label_addr("$srcaddr", label, len, r->af, &r->src); 5796 expand_label_addr("$dstaddr", label, len, r->af, &r->dst); 5797 expand_label_port("$srcport", label, len, &r->src); 5798 expand_label_port("$dstport", label, len, &r->dst); 5799 expand_label_proto("$proto", label, len, r->proto); 5800 expand_label_nr("$nr", label, len, r); 5801 } 5802 5803 int 5804 expand_altq(struct pf_altq *a, struct node_if *interfaces, 5805 struct node_queue *nqueues, struct node_queue_bw bwspec, 5806 struct node_queue_opt *opts) 5807 { 5808 struct pf_altq pa, pb; 5809 char qname[PF_QNAME_SIZE]; 5810 struct node_queue *n; 5811 struct node_queue_bw bw; 5812 int errs = 0; 5813 5814 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) { 5815 FREE_LIST(struct node_if, interfaces); 5816 if (nqueues) 5817 FREE_LIST(struct node_queue, nqueues); 5818 return (0); 5819 } 5820 5821 LOOP_THROUGH(struct node_if, interface, interfaces, 5822 memcpy(&pa, a, sizeof(struct pf_altq)); 5823 if (strlcpy(pa.ifname, interface->ifname, 5824 sizeof(pa.ifname)) >= sizeof(pa.ifname)) 5825 errx(1, "expand_altq: strlcpy"); 5826 5827 if (interface->not) { 5828 yyerror("altq on ! <interface> is not supported"); 5829 errs++; 5830 } else { 5831 if (eval_pfaltq(pf, &pa, &bwspec, opts)) 5832 errs++; 5833 else 5834 if (pfctl_add_altq(pf, &pa)) 5835 errs++; 5836 5837 if (pf->opts & PF_OPT_VERBOSE) { 5838 print_altq(&pf->paltq->altq, 0, 5839 &bwspec, opts); 5840 if (nqueues && nqueues->tail) { 5841 printf("queue { "); 5842 LOOP_THROUGH(struct node_queue, queue, 5843 nqueues, 5844 printf("%s ", 5845 queue->queue); 5846 ); 5847 printf("}"); 5848 } 5849 printf("\n"); 5850 } 5851 5852 if (pa.scheduler == ALTQT_CBQ || 5853 pa.scheduler == ALTQT_HFSC || 5854 pa.scheduler == ALTQT_FAIRQ) { 5855 /* now create a root queue */ 5856 memset(&pb, 0, sizeof(struct pf_altq)); 5857 if (strlcpy(qname, "root_", sizeof(qname)) >= 5858 sizeof(qname)) 5859 errx(1, "expand_altq: strlcpy"); 5860 if (strlcat(qname, interface->ifname, 5861 sizeof(qname)) >= sizeof(qname)) 5862 errx(1, "expand_altq: strlcat"); 5863 if (strlcpy(pb.qname, qname, 5864 sizeof(pb.qname)) >= sizeof(pb.qname)) 5865 errx(1, "expand_altq: strlcpy"); 5866 if (strlcpy(pb.ifname, interface->ifname, 5867 sizeof(pb.ifname)) >= sizeof(pb.ifname)) 5868 errx(1, "expand_altq: strlcpy"); 5869 pb.qlimit = pa.qlimit; 5870 pb.scheduler = pa.scheduler; 5871 bw.bw_absolute = pa.ifbandwidth; 5872 bw.bw_percent = 0; 5873 if (eval_pfqueue(pf, &pb, &bw, opts)) 5874 errs++; 5875 else 5876 if (pfctl_add_altq(pf, &pb)) 5877 errs++; 5878 } 5879 5880 LOOP_THROUGH(struct node_queue, queue, nqueues, 5881 n = calloc(1, sizeof(struct node_queue)); 5882 if (n == NULL) 5883 err(1, "expand_altq: calloc"); 5884 if (pa.scheduler == ALTQT_CBQ || 5885 pa.scheduler == ALTQT_HFSC || 5886 pa.scheduler == ALTQT_FAIRQ) 5887 if (strlcpy(n->parent, qname, 5888 sizeof(n->parent)) >= 5889 sizeof(n->parent)) 5890 errx(1, "expand_altq: strlcpy"); 5891 if (strlcpy(n->queue, queue->queue, 5892 sizeof(n->queue)) >= sizeof(n->queue)) 5893 errx(1, "expand_altq: strlcpy"); 5894 if (strlcpy(n->ifname, interface->ifname, 5895 sizeof(n->ifname)) >= sizeof(n->ifname)) 5896 errx(1, "expand_altq: strlcpy"); 5897 n->scheduler = pa.scheduler; 5898 n->next = NULL; 5899 n->tail = n; 5900 if (queues == NULL) 5901 queues = n; 5902 else { 5903 queues->tail->next = n; 5904 queues->tail = n; 5905 } 5906 ); 5907 } 5908 ); 5909 FREE_LIST(struct node_if, interfaces); 5910 if (nqueues) 5911 FREE_LIST(struct node_queue, nqueues); 5912 5913 return (errs); 5914 } 5915 5916 int 5917 expand_queue(struct pf_altq *a, struct node_if *interfaces, 5918 struct node_queue *nqueues, struct node_queue_bw bwspec, 5919 struct node_queue_opt *opts) 5920 { 5921 struct node_queue *n, *nq; 5922 struct pf_altq pa; 5923 u_int8_t found = 0; 5924 u_int8_t errs = 0; 5925 5926 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) { 5927 FREE_LIST(struct node_queue, nqueues); 5928 return (0); 5929 } 5930 5931 if (queues == NULL) { 5932 yyerror("queue %s has no parent", a->qname); 5933 FREE_LIST(struct node_queue, nqueues); 5934 return (1); 5935 } 5936 5937 LOOP_THROUGH(struct node_if, interface, interfaces, 5938 LOOP_THROUGH(struct node_queue, tqueue, queues, 5939 if (!strncmp(a->qname, tqueue->queue, PF_QNAME_SIZE) && 5940 (interface->ifname[0] == 0 || 5941 (!interface->not && !strncmp(interface->ifname, 5942 tqueue->ifname, IFNAMSIZ)) || 5943 (interface->not && strncmp(interface->ifname, 5944 tqueue->ifname, IFNAMSIZ)))) { 5945 /* found ourself in queues */ 5946 found++; 5947 5948 memcpy(&pa, a, sizeof(struct pf_altq)); 5949 5950 if (pa.scheduler != ALTQT_NONE && 5951 pa.scheduler != tqueue->scheduler) { 5952 yyerror("exactly one scheduler type " 5953 "per interface allowed"); 5954 return (1); 5955 } 5956 pa.scheduler = tqueue->scheduler; 5957 5958 /* scheduler dependent error checking */ 5959 switch (pa.scheduler) { 5960 case ALTQT_PRIQ: 5961 if (nqueues != NULL) { 5962 yyerror("priq queues cannot " 5963 "have child queues"); 5964 return (1); 5965 } 5966 if (bwspec.bw_absolute > 0 || 5967 bwspec.bw_percent < 100) { 5968 yyerror("priq doesn't take " 5969 "bandwidth"); 5970 return (1); 5971 } 5972 break; 5973 default: 5974 break; 5975 } 5976 5977 if (strlcpy(pa.ifname, tqueue->ifname, 5978 sizeof(pa.ifname)) >= sizeof(pa.ifname)) 5979 errx(1, "expand_queue: strlcpy"); 5980 if (strlcpy(pa.parent, tqueue->parent, 5981 sizeof(pa.parent)) >= sizeof(pa.parent)) 5982 errx(1, "expand_queue: strlcpy"); 5983 5984 if (eval_pfqueue(pf, &pa, &bwspec, opts)) 5985 errs++; 5986 else 5987 if (pfctl_add_altq(pf, &pa)) 5988 errs++; 5989 5990 for (nq = nqueues; nq != NULL; nq = nq->next) { 5991 if (!strcmp(a->qname, nq->queue)) { 5992 yyerror("queue cannot have " 5993 "itself as child"); 5994 errs++; 5995 continue; 5996 } 5997 n = calloc(1, 5998 sizeof(struct node_queue)); 5999 if (n == NULL) 6000 err(1, "expand_queue: calloc"); 6001 if (strlcpy(n->parent, a->qname, 6002 sizeof(n->parent)) >= 6003 sizeof(n->parent)) 6004 errx(1, "expand_queue strlcpy"); 6005 if (strlcpy(n->queue, nq->queue, 6006 sizeof(n->queue)) >= 6007 sizeof(n->queue)) 6008 errx(1, "expand_queue strlcpy"); 6009 if (strlcpy(n->ifname, tqueue->ifname, 6010 sizeof(n->ifname)) >= 6011 sizeof(n->ifname)) 6012 errx(1, "expand_queue strlcpy"); 6013 n->scheduler = tqueue->scheduler; 6014 n->next = NULL; 6015 n->tail = n; 6016 if (queues == NULL) 6017 queues = n; 6018 else { 6019 queues->tail->next = n; 6020 queues->tail = n; 6021 } 6022 } 6023 if ((pf->opts & PF_OPT_VERBOSE) && ( 6024 (found == 1 && interface->ifname[0] == 0) || 6025 (found > 0 && interface->ifname[0] != 0))) { 6026 print_queue(&pf->paltq->altq, 0, 6027 &bwspec, interface->ifname[0] != 0, 6028 opts); 6029 if (nqueues && nqueues->tail) { 6030 printf("{ "); 6031 LOOP_THROUGH(struct node_queue, 6032 queue, nqueues, 6033 printf("%s ", 6034 queue->queue); 6035 ); 6036 printf("}"); 6037 } 6038 printf("\n"); 6039 } 6040 } 6041 ); 6042 ); 6043 6044 FREE_LIST(struct node_queue, nqueues); 6045 FREE_LIST(struct node_if, interfaces); 6046 6047 if (!found) { 6048 yyerror("queue %s has no parent", a->qname); 6049 errs++; 6050 } 6051 6052 if (errs) 6053 return (1); 6054 else 6055 return (0); 6056 } 6057 6058 static int 6059 pf_af_to_proto(sa_family_t af) 6060 { 6061 if (af == AF_INET) 6062 return (ETHERTYPE_IP); 6063 if (af == AF_INET6) 6064 return (ETHERTYPE_IPV6); 6065 6066 return (0); 6067 } 6068 6069 void 6070 expand_eth_rule(struct pfctl_eth_rule *r, 6071 struct node_if *interfaces, struct node_etherproto *protos, 6072 struct node_mac *srcs, struct node_mac *dsts, 6073 struct node_host *ipsrcs, struct node_host *ipdsts, 6074 const char *bridge_to, const char *anchor_call) 6075 { 6076 char tagname[PF_TAG_NAME_SIZE]; 6077 char match_tagname[PF_TAG_NAME_SIZE]; 6078 char qname[PF_QNAME_SIZE]; 6079 6080 if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname)) 6081 errx(1, "expand_eth_rule: tagname"); 6082 if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >= 6083 sizeof(match_tagname)) 6084 errx(1, "expand_eth_rule: match_tagname"); 6085 if (strlcpy(qname, r->qname, sizeof(qname)) >= sizeof(qname)) 6086 errx(1, "expand_eth_rule: qname"); 6087 6088 LOOP_THROUGH(struct node_if, interface, interfaces, 6089 LOOP_THROUGH(struct node_etherproto, proto, protos, 6090 LOOP_THROUGH(struct node_mac, src, srcs, 6091 LOOP_THROUGH(struct node_mac, dst, dsts, 6092 LOOP_THROUGH(struct node_host, ipsrc, ipsrcs, 6093 LOOP_THROUGH(struct node_host, ipdst, ipdsts, 6094 strlcpy(r->ifname, interface->ifname, 6095 sizeof(r->ifname)); 6096 r->ifnot = interface->not; 6097 r->proto = proto->proto; 6098 if (!r->proto && ipsrc->af) 6099 r->proto = pf_af_to_proto(ipsrc->af); 6100 else if (!r->proto && ipdst->af) 6101 r->proto = pf_af_to_proto(ipdst->af); 6102 bcopy(src->mac, r->src.addr, ETHER_ADDR_LEN); 6103 bcopy(src->mask, r->src.mask, ETHER_ADDR_LEN); 6104 r->src.neg = src->neg; 6105 r->src.isset = src->isset; 6106 r->ipsrc.addr = ipsrc->addr; 6107 r->ipsrc.neg = ipsrc->not; 6108 r->ipdst.addr = ipdst->addr; 6109 r->ipdst.neg = ipdst->not; 6110 bcopy(dst->mac, r->dst.addr, ETHER_ADDR_LEN); 6111 bcopy(dst->mask, r->dst.mask, ETHER_ADDR_LEN); 6112 r->dst.neg = dst->neg; 6113 r->dst.isset = dst->isset; 6114 r->nr = pf->eastack[pf->asd]->match++; 6115 6116 if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >= 6117 sizeof(r->tagname)) 6118 errx(1, "expand_eth_rule: r->tagname"); 6119 if (strlcpy(r->match_tagname, match_tagname, 6120 sizeof(r->match_tagname)) >= sizeof(r->match_tagname)) 6121 errx(1, "expand_eth_rule: r->match_tagname"); 6122 if (strlcpy(r->qname, qname, sizeof(r->qname)) >= sizeof(r->qname)) 6123 errx(1, "expand_eth_rule: r->qname"); 6124 6125 if (bridge_to) 6126 strlcpy(r->bridge_to, bridge_to, sizeof(r->bridge_to)); 6127 6128 pfctl_append_eth_rule(pf, r, anchor_call); 6129 )))))); 6130 6131 FREE_LIST(struct node_if, interfaces); 6132 FREE_LIST(struct node_etherproto, protos); 6133 FREE_LIST(struct node_mac, srcs); 6134 FREE_LIST(struct node_mac, dsts); 6135 FREE_LIST(struct node_host, ipsrcs); 6136 FREE_LIST(struct node_host, ipdsts); 6137 } 6138 6139 void 6140 expand_rule(struct pfctl_rule *r, 6141 struct node_if *interfaces, struct redirspec *nat, 6142 struct redirspec *rdr, struct node_host *rdr_hosts, 6143 struct node_host *nat_hosts, 6144 struct node_proto *protos, struct node_os *src_oses, 6145 struct node_host *src_hosts, struct node_port *src_ports, 6146 struct node_host *dst_hosts, struct node_port *dst_ports, 6147 struct node_uid *uids, struct node_gid *gids, struct node_if *rcv, 6148 struct node_icmp *icmp_types, const char *anchor_call) 6149 { 6150 sa_family_t af = r->af; 6151 int added = 0, error = 0; 6152 char ifname[IF_NAMESIZE]; 6153 char label[PF_RULE_MAX_LABEL_COUNT][PF_RULE_LABEL_SIZE]; 6154 char tagname[PF_TAG_NAME_SIZE]; 6155 char match_tagname[PF_TAG_NAME_SIZE]; 6156 struct pf_pooladdr *pa; 6157 struct node_host *h, *osrch, *odsth; 6158 u_int8_t flags, flagset, keep_state; 6159 6160 memcpy(label, r->label, sizeof(r->label)); 6161 assert(sizeof(r->label) == sizeof(label)); 6162 if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname)) 6163 errx(1, "expand_rule: strlcpy"); 6164 if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >= 6165 sizeof(match_tagname)) 6166 errx(1, "expand_rule: strlcpy"); 6167 flags = r->flags; 6168 flagset = r->flagset; 6169 keep_state = r->keep_state; 6170 6171 LOOP_THROUGH(struct node_if, interface, interfaces, 6172 LOOP_THROUGH(struct node_proto, proto, protos, 6173 LOOP_THROUGH(struct node_icmp, icmp_type, icmp_types, 6174 LOOP_THROUGH(struct node_host, src_host, src_hosts, 6175 LOOP_THROUGH(struct node_host, dst_host, dst_hosts, 6176 LOOP_THROUGH(struct node_port, src_port, src_ports, 6177 LOOP_THROUGH(struct node_port, dst_port, dst_ports, 6178 LOOP_THROUGH(struct node_os, src_os, src_oses, 6179 LOOP_THROUGH(struct node_uid, uid, uids, 6180 LOOP_THROUGH(struct node_gid, gid, gids, 6181 6182 r->af = af; 6183 6184 if (r->rule_flag & PFRULE_AFTO) { 6185 assert(nat != NULL); 6186 r->naf = nat->af; 6187 } 6188 6189 /* for link-local IPv6 address, interface must match up */ 6190 if ((r->af && src_host->af && r->af != src_host->af) || 6191 (r->af && dst_host->af && r->af != dst_host->af) || 6192 (src_host->af && dst_host->af && 6193 src_host->af != dst_host->af) || 6194 (src_host->ifindex && dst_host->ifindex && 6195 src_host->ifindex != dst_host->ifindex) || 6196 (src_host->ifindex && *interface->ifname && 6197 src_host->ifindex != if_nametoindex(interface->ifname)) || 6198 (dst_host->ifindex && *interface->ifname && 6199 dst_host->ifindex != if_nametoindex(interface->ifname))) 6200 continue; 6201 if (!r->af && src_host->af) 6202 r->af = src_host->af; 6203 else if (!r->af && dst_host->af) 6204 r->af = dst_host->af; 6205 6206 if (*interface->ifname) 6207 strlcpy(r->ifname, interface->ifname, 6208 sizeof(r->ifname)); 6209 else if (if_indextoname(src_host->ifindex, ifname)) 6210 strlcpy(r->ifname, ifname, sizeof(r->ifname)); 6211 else if (if_indextoname(dst_host->ifindex, ifname)) 6212 strlcpy(r->ifname, ifname, sizeof(r->ifname)); 6213 else 6214 memset(r->ifname, '\0', sizeof(r->ifname)); 6215 6216 memcpy(r->label, label, sizeof(r->label)); 6217 if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >= 6218 sizeof(r->tagname)) 6219 errx(1, "expand_rule: strlcpy"); 6220 if (strlcpy(r->match_tagname, match_tagname, 6221 sizeof(r->match_tagname)) >= sizeof(r->match_tagname)) 6222 errx(1, "expand_rule: strlcpy"); 6223 6224 osrch = odsth = NULL; 6225 if (src_host->addr.type == PF_ADDR_DYNIFTL) { 6226 osrch = src_host; 6227 if ((src_host = gen_dynnode(src_host, r->af)) == NULL) 6228 err(1, "expand_rule: calloc"); 6229 } 6230 if (dst_host->addr.type == PF_ADDR_DYNIFTL) { 6231 odsth = dst_host; 6232 if ((dst_host = gen_dynnode(dst_host, r->af)) == NULL) 6233 err(1, "expand_rule: calloc"); 6234 } 6235 6236 error += check_netmask(src_host, r->af); 6237 error += check_netmask(dst_host, r->af); 6238 6239 r->ifnot = interface->not; 6240 r->proto = proto->proto; 6241 r->src.addr = src_host->addr; 6242 r->src.neg = src_host->not; 6243 r->src.port[0] = src_port->port[0]; 6244 r->src.port[1] = src_port->port[1]; 6245 r->src.port_op = src_port->op; 6246 r->dst.addr = dst_host->addr; 6247 r->dst.neg = dst_host->not; 6248 r->dst.port[0] = dst_port->port[0]; 6249 r->dst.port[1] = dst_port->port[1]; 6250 r->dst.port_op = dst_port->op; 6251 r->uid.op = uid->op; 6252 r->uid.uid[0] = uid->uid[0]; 6253 r->uid.uid[1] = uid->uid[1]; 6254 r->gid.op = gid->op; 6255 r->gid.gid[0] = gid->gid[0]; 6256 r->gid.gid[1] = gid->gid[1]; 6257 if (rcv) { 6258 strlcpy(r->rcv_ifname, rcv->ifname, 6259 sizeof(r->rcv_ifname)); 6260 } 6261 r->type = icmp_type->type; 6262 r->code = icmp_type->code; 6263 6264 if ((keep_state == PF_STATE_MODULATE || 6265 keep_state == PF_STATE_SYNPROXY) && 6266 r->proto && r->proto != IPPROTO_TCP) 6267 r->keep_state = PF_STATE_NORMAL; 6268 else 6269 r->keep_state = keep_state; 6270 6271 if (r->proto && r->proto != IPPROTO_TCP) { 6272 r->flags = 0; 6273 r->flagset = 0; 6274 } else { 6275 r->flags = flags; 6276 r->flagset = flagset; 6277 } 6278 if (icmp_type->proto && r->proto != icmp_type->proto) { 6279 yyerror("icmp-type mismatch"); 6280 error++; 6281 } 6282 6283 if (src_os && src_os->os) { 6284 r->os_fingerprint = pfctl_get_fingerprint(src_os->os); 6285 if ((pf->opts & PF_OPT_VERBOSE2) && 6286 r->os_fingerprint == PF_OSFP_NOMATCH) 6287 fprintf(stderr, 6288 "warning: unknown '%s' OS fingerprint\n", 6289 src_os->os); 6290 } else { 6291 r->os_fingerprint = PF_OSFP_ANY; 6292 } 6293 6294 TAILQ_INIT(&r->rdr.list); 6295 for (h = rdr_hosts; h != NULL; h = h->next) { 6296 pa = calloc(1, sizeof(struct pf_pooladdr)); 6297 if (pa == NULL) 6298 err(1, "expand_rule: calloc"); 6299 pa->addr = h->addr; 6300 if (h->ifname != NULL) { 6301 if (strlcpy(pa->ifname, h->ifname, 6302 sizeof(pa->ifname)) >= 6303 sizeof(pa->ifname)) 6304 errx(1, "expand_rule: strlcpy"); 6305 } else 6306 pa->ifname[0] = 0; 6307 TAILQ_INSERT_TAIL(&r->rpool.list, pa, entries); 6308 } 6309 TAILQ_INIT(&r->nat.list); 6310 for (h = nat_hosts; h != NULL; h = h->next) { 6311 pa = calloc(1, sizeof(struct pf_pooladdr)); 6312 if (pa == NULL) 6313 err(1, "expand_rule: calloc"); 6314 pa->addr = h->addr; 6315 if (h->ifname != NULL) { 6316 if (strlcpy(pa->ifname, h->ifname, 6317 sizeof(pa->ifname)) >= 6318 sizeof(pa->ifname)) 6319 errx(1, "expand_rule: strlcpy"); 6320 } else 6321 pa->ifname[0] = 0; 6322 TAILQ_INSERT_TAIL(&r->nat.list, pa, entries); 6323 } 6324 6325 r->nat.proxy_port[0] = PF_NAT_PROXY_PORT_LOW; 6326 r->nat.proxy_port[1] = PF_NAT_PROXY_PORT_HIGH; 6327 6328 if (rule_consistent(r, anchor_call[0]) < 0 || error) 6329 yyerror("skipping rule due to errors"); 6330 else { 6331 r->nr = pf->astack[pf->asd]->match++; 6332 pfctl_append_rule(pf, r, anchor_call); 6333 added++; 6334 } 6335 6336 if (osrch && src_host->addr.type == PF_ADDR_DYNIFTL) { 6337 free(src_host); 6338 src_host = osrch; 6339 } 6340 if (odsth && dst_host->addr.type == PF_ADDR_DYNIFTL) { 6341 free(dst_host); 6342 dst_host = odsth; 6343 } 6344 6345 )))))))))); 6346 6347 FREE_LIST(struct node_if, interfaces); 6348 FREE_LIST(struct node_proto, protos); 6349 FREE_LIST(struct node_host, src_hosts); 6350 FREE_LIST(struct node_port, src_ports); 6351 FREE_LIST(struct node_os, src_oses); 6352 FREE_LIST(struct node_host, dst_hosts); 6353 FREE_LIST(struct node_port, dst_ports); 6354 FREE_LIST(struct node_uid, uids); 6355 FREE_LIST(struct node_gid, gids); 6356 FREE_LIST(struct node_icmp, icmp_types); 6357 FREE_LIST(struct node_host, rdr_hosts); 6358 FREE_LIST(struct node_host, nat_hosts); 6359 6360 if (!added) 6361 yyerror("rule expands to no valid combination"); 6362 } 6363 6364 int 6365 expand_skip_interface(struct node_if *interfaces) 6366 { 6367 int errs = 0; 6368 6369 if (!interfaces || (!interfaces->next && !interfaces->not && 6370 !strcmp(interfaces->ifname, "none"))) { 6371 if (pf->opts & PF_OPT_VERBOSE) 6372 printf("set skip on none\n"); 6373 errs = pfctl_set_interface_flags(pf, "", PFI_IFLAG_SKIP, 0); 6374 return (errs); 6375 } 6376 6377 if (pf->opts & PF_OPT_VERBOSE) 6378 printf("set skip on {"); 6379 LOOP_THROUGH(struct node_if, interface, interfaces, 6380 if (pf->opts & PF_OPT_VERBOSE) 6381 printf(" %s", interface->ifname); 6382 if (interface->not) { 6383 yyerror("skip on ! <interface> is not supported"); 6384 errs++; 6385 } else 6386 errs += pfctl_set_interface_flags(pf, 6387 interface->ifname, PFI_IFLAG_SKIP, 1); 6388 ); 6389 if (pf->opts & PF_OPT_VERBOSE) 6390 printf(" }\n"); 6391 6392 FREE_LIST(struct node_if, interfaces); 6393 6394 if (errs) 6395 return (1); 6396 else 6397 return (0); 6398 } 6399 6400 void 6401 freehostlist(struct node_host *h) 6402 { 6403 FREE_LIST(struct node_host, h); 6404 } 6405 6406 #undef FREE_LIST 6407 #undef LOOP_THROUGH 6408 6409 int 6410 check_rulestate(int desired_state) 6411 { 6412 if (require_order && (rulestate > desired_state)) { 6413 yyerror("Rules must be in order: options, ethernet, " 6414 "normalization, queueing, translation, filtering"); 6415 return (1); 6416 } 6417 rulestate = desired_state; 6418 return (0); 6419 } 6420 6421 int 6422 kw_cmp(const void *k, const void *e) 6423 { 6424 return (strcmp(k, ((const struct keywords *)e)->k_name)); 6425 } 6426 6427 int 6428 lookup(char *s) 6429 { 6430 /* this has to be sorted always */ 6431 static const struct keywords keywords[] = { 6432 { "af-to", AFTO}, 6433 { "all", ALL}, 6434 { "allow-opts", ALLOWOPTS}, 6435 { "altq", ALTQ}, 6436 { "anchor", ANCHOR}, 6437 { "antispoof", ANTISPOOF}, 6438 { "any", ANY}, 6439 { "bandwidth", BANDWIDTH}, 6440 { "binat", BINAT}, 6441 { "binat-anchor", BINATANCHOR}, 6442 { "bitmask", BITMASK}, 6443 { "block", BLOCK}, 6444 { "block-policy", BLOCKPOLICY}, 6445 { "bridge-to", BRIDGE_TO}, 6446 { "buckets", BUCKETS}, 6447 { "cbq", CBQ}, 6448 { "code", CODE}, 6449 { "codelq", CODEL}, 6450 { "debug", DEBUG}, 6451 { "divert-reply", DIVERTREPLY}, 6452 { "divert-to", DIVERTTO}, 6453 { "dnpipe", DNPIPE}, 6454 { "dnqueue", DNQUEUE}, 6455 { "drop", DROP}, 6456 { "dup-to", DUPTO}, 6457 { "endpoint-independent", ENDPI}, 6458 { "ether", ETHER}, 6459 { "fail-policy", FAILPOLICY}, 6460 { "fairq", FAIRQ}, 6461 { "fastroute", FASTROUTE}, 6462 { "file", FILENAME}, 6463 { "fingerprints", FINGERPRINTS}, 6464 { "flags", FLAGS}, 6465 { "floating", FLOATING}, 6466 { "flush", FLUSH}, 6467 { "for", FOR}, 6468 { "fragment", FRAGMENT}, 6469 { "from", FROM}, 6470 { "global", GLOBAL}, 6471 { "group", GROUP}, 6472 { "hfsc", HFSC}, 6473 { "hogs", HOGS}, 6474 { "hostid", HOSTID}, 6475 { "icmp-type", ICMPTYPE}, 6476 { "icmp6-type", ICMP6TYPE}, 6477 { "if-bound", IFBOUND}, 6478 { "in", IN}, 6479 { "include", INCLUDE}, 6480 { "inet", INET}, 6481 { "inet6", INET6}, 6482 { "interval", INTERVAL}, 6483 { "keep", KEEP}, 6484 { "keepcounters", KEEPCOUNTERS}, 6485 { "l3", L3}, 6486 { "label", LABEL}, 6487 { "limit", LIMIT}, 6488 { "linkshare", LINKSHARE}, 6489 { "load", LOAD}, 6490 { "log", LOG}, 6491 { "loginterface", LOGINTERFACE}, 6492 { "map-e-portset", MAPEPORTSET}, 6493 { "match", MATCH}, 6494 { "matches", MATCHES}, 6495 { "max", MAXIMUM}, 6496 { "max-mss", MAXMSS}, 6497 { "max-src-conn", MAXSRCCONN}, 6498 { "max-src-conn-rate", MAXSRCCONNRATE}, 6499 { "max-src-nodes", MAXSRCNODES}, 6500 { "max-src-states", MAXSRCSTATES}, 6501 { "min-ttl", MINTTL}, 6502 { "modulate", MODULATE}, 6503 { "nat", NAT}, 6504 { "nat-anchor", NATANCHOR}, 6505 { "no", NO}, 6506 { "no-df", NODF}, 6507 { "no-route", NOROUTE}, 6508 { "no-sync", NOSYNC}, 6509 { "on", ON}, 6510 { "optimization", OPTIMIZATION}, 6511 { "os", OS}, 6512 { "out", OUT}, 6513 { "overload", OVERLOAD}, 6514 { "pass", PASS}, 6515 { "pflow", PFLOW}, 6516 { "port", PORT}, 6517 { "prio", PRIO}, 6518 { "priority", PRIORITY}, 6519 { "priq", PRIQ}, 6520 { "probability", PROBABILITY}, 6521 { "proto", PROTO}, 6522 { "qlimit", QLIMIT}, 6523 { "queue", QUEUE}, 6524 { "quick", QUICK}, 6525 { "random", RANDOM}, 6526 { "random-id", RANDOMID}, 6527 { "rdr", RDR}, 6528 { "rdr-anchor", RDRANCHOR}, 6529 { "realtime", REALTIME}, 6530 { "reassemble", REASSEMBLE}, 6531 { "received-on", RECEIVEDON}, 6532 { "reply-to", REPLYTO}, 6533 { "require-order", REQUIREORDER}, 6534 { "return", RETURN}, 6535 { "return-icmp", RETURNICMP}, 6536 { "return-icmp6", RETURNICMP6}, 6537 { "return-rst", RETURNRST}, 6538 { "ridentifier", RIDENTIFIER}, 6539 { "round-robin", ROUNDROBIN}, 6540 { "route", ROUTE}, 6541 { "route-to", ROUTETO}, 6542 { "rtable", RTABLE}, 6543 { "rule", RULE}, 6544 { "ruleset-optimization", RULESET_OPTIMIZATION}, 6545 { "scrub", SCRUB}, 6546 { "set", SET}, 6547 { "set-tos", SETTOS}, 6548 { "skip", SKIP}, 6549 { "sloppy", SLOPPY}, 6550 { "source-hash", SOURCEHASH}, 6551 { "source-track", SOURCETRACK}, 6552 { "state", STATE}, 6553 { "state-defaults", STATEDEFAULTS}, 6554 { "state-policy", STATEPOLICY}, 6555 { "static-port", STATICPORT}, 6556 { "sticky-address", STICKYADDRESS}, 6557 { "syncookies", SYNCOOKIES}, 6558 { "synproxy", SYNPROXY}, 6559 { "table", TABLE}, 6560 { "tag", TAG}, 6561 { "tagged", TAGGED}, 6562 { "target", TARGET}, 6563 { "tbrsize", TBRSIZE}, 6564 { "timeout", TIMEOUT}, 6565 { "to", TO}, 6566 { "tos", TOS}, 6567 { "ttl", TTL}, 6568 { "upperlimit", UPPERLIMIT}, 6569 { "urpf-failed", URPFFAILED}, 6570 { "user", USER}, 6571 }; 6572 const struct keywords *p; 6573 6574 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]), 6575 sizeof(keywords[0]), kw_cmp); 6576 6577 if (p) { 6578 if (debug > 1) 6579 fprintf(stderr, "%s: %d\n", s, p->k_val); 6580 return (p->k_val); 6581 } else { 6582 if (debug > 1) 6583 fprintf(stderr, "string: %s\n", s); 6584 return (STRING); 6585 } 6586 } 6587 6588 #define MAXPUSHBACK 128 6589 6590 static char *parsebuf; 6591 static int parseindex; 6592 static char pushback_buffer[MAXPUSHBACK]; 6593 static int pushback_index = 0; 6594 6595 int 6596 lgetc(int quotec) 6597 { 6598 int c, next; 6599 6600 if (parsebuf) { 6601 /* Read character from the parsebuffer instead of input. */ 6602 if (parseindex >= 0) { 6603 c = parsebuf[parseindex++]; 6604 if (c != '\0') 6605 return (c); 6606 parsebuf = NULL; 6607 } else 6608 parseindex++; 6609 } 6610 6611 if (pushback_index) 6612 return (pushback_buffer[--pushback_index]); 6613 6614 if (quotec) { 6615 if ((c = getc(file->stream)) == EOF) { 6616 yyerror("reached end of file while parsing quoted string"); 6617 if (popfile() == EOF) 6618 return (EOF); 6619 return (quotec); 6620 } 6621 return (c); 6622 } 6623 6624 while ((c = getc(file->stream)) == '\\') { 6625 next = getc(file->stream); 6626 if (next != '\n') { 6627 c = next; 6628 break; 6629 } 6630 yylval.lineno = file->lineno; 6631 file->lineno++; 6632 } 6633 6634 while (c == EOF) { 6635 if (popfile() == EOF) 6636 return (EOF); 6637 c = getc(file->stream); 6638 } 6639 return (c); 6640 } 6641 6642 int 6643 lungetc(int c) 6644 { 6645 if (c == EOF) 6646 return (EOF); 6647 if (parsebuf) { 6648 parseindex--; 6649 if (parseindex >= 0) 6650 return (c); 6651 } 6652 if (pushback_index < MAXPUSHBACK-1) 6653 return (pushback_buffer[pushback_index++] = c); 6654 else 6655 return (EOF); 6656 } 6657 6658 int 6659 findeol(void) 6660 { 6661 int c; 6662 6663 parsebuf = NULL; 6664 6665 /* skip to either EOF or the first real EOL */ 6666 while (1) { 6667 if (pushback_index) 6668 c = pushback_buffer[--pushback_index]; 6669 else 6670 c = lgetc(0); 6671 if (c == '\n') { 6672 file->lineno++; 6673 break; 6674 } 6675 if (c == EOF) 6676 break; 6677 } 6678 return (ERROR); 6679 } 6680 6681 int 6682 yylex(void) 6683 { 6684 char buf[8096]; 6685 char *p, *val; 6686 int quotec, next, c; 6687 int token; 6688 6689 top: 6690 p = buf; 6691 while ((c = lgetc(0)) == ' ' || c == '\t') 6692 ; /* nothing */ 6693 6694 yylval.lineno = file->lineno; 6695 if (c == '#') 6696 while ((c = lgetc(0)) != '\n' && c != EOF) 6697 ; /* nothing */ 6698 if (c == '$' && parsebuf == NULL) { 6699 while (1) { 6700 if ((c = lgetc(0)) == EOF) 6701 return (0); 6702 6703 if (p + 1 >= buf + sizeof(buf) - 1) { 6704 yyerror("string too long"); 6705 return (findeol()); 6706 } 6707 if (isalnum(c) || c == '_') { 6708 *p++ = (char)c; 6709 continue; 6710 } 6711 *p = '\0'; 6712 lungetc(c); 6713 break; 6714 } 6715 val = symget(buf); 6716 if (val == NULL) { 6717 yyerror("macro '%s' not defined", buf); 6718 return (findeol()); 6719 } 6720 parsebuf = val; 6721 parseindex = 0; 6722 goto top; 6723 } 6724 6725 switch (c) { 6726 case '\'': 6727 case '"': 6728 quotec = c; 6729 while (1) { 6730 if ((c = lgetc(quotec)) == EOF) 6731 return (0); 6732 if (c == '\n') { 6733 file->lineno++; 6734 continue; 6735 } else if (c == '\\') { 6736 if ((next = lgetc(quotec)) == EOF) 6737 return (0); 6738 if (next == quotec || c == ' ' || c == '\t') 6739 c = next; 6740 else if (next == '\n') { 6741 file->lineno++; 6742 continue; 6743 } 6744 else 6745 lungetc(next); 6746 } else if (c == quotec) { 6747 *p = '\0'; 6748 break; 6749 } 6750 if (p + 1 >= buf + sizeof(buf) - 1) { 6751 yyerror("string too long"); 6752 return (findeol()); 6753 } 6754 *p++ = (char)c; 6755 } 6756 yylval.v.string = strdup(buf); 6757 if (yylval.v.string == NULL) 6758 err(1, "yylex: strdup"); 6759 return (STRING); 6760 case '!': 6761 next = lgetc(0); 6762 if (next == '=') 6763 return (NE); 6764 lungetc(next); 6765 break; 6766 case '<': 6767 next = lgetc(0); 6768 if (next == '>') { 6769 yylval.v.i = PF_OP_XRG; 6770 return (PORTBINARY); 6771 } else if (next == '=') 6772 return (LE); 6773 lungetc(next); 6774 break; 6775 case '>': 6776 next = lgetc(0); 6777 if (next == '<') { 6778 yylval.v.i = PF_OP_IRG; 6779 return (PORTBINARY); 6780 } else if (next == '=') 6781 return (GE); 6782 lungetc(next); 6783 break; 6784 case '-': 6785 next = lgetc(0); 6786 if (next == '>') 6787 return (ARROW); 6788 lungetc(next); 6789 break; 6790 } 6791 6792 #define allowed_to_end_number(x) \ 6793 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=') 6794 6795 if (c == '-' || isdigit(c)) { 6796 do { 6797 *p++ = c; 6798 if ((unsigned)(p-buf) >= sizeof(buf)) { 6799 yyerror("string too long"); 6800 return (findeol()); 6801 } 6802 } while ((c = lgetc(0)) != EOF && isdigit(c)); 6803 lungetc(c); 6804 if (p == buf + 1 && buf[0] == '-') 6805 goto nodigits; 6806 if (c == EOF || allowed_to_end_number(c)) { 6807 const char *errstr = NULL; 6808 6809 *p = '\0'; 6810 yylval.v.number = strtonum(buf, LLONG_MIN, 6811 LLONG_MAX, &errstr); 6812 if (errstr) { 6813 yyerror("\"%s\" invalid number: %s", 6814 buf, errstr); 6815 return (findeol()); 6816 } 6817 return (NUMBER); 6818 } else { 6819 nodigits: 6820 while (p > buf + 1) 6821 lungetc(*--p); 6822 c = *--p; 6823 if (c == '-') 6824 return (c); 6825 } 6826 } 6827 6828 #define allowed_in_string(x) \ 6829 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \ 6830 x != '{' && x != '}' && x != '<' && x != '>' && \ 6831 x != '!' && x != '=' && x != '/' && x != '#' && \ 6832 x != ',')) 6833 6834 if (isalnum(c) || c == ':' || c == '_') { 6835 do { 6836 *p++ = c; 6837 if ((unsigned)(p-buf) >= sizeof(buf)) { 6838 yyerror("string too long"); 6839 return (findeol()); 6840 } 6841 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c))); 6842 lungetc(c); 6843 *p = '\0'; 6844 if ((token = lookup(buf)) == STRING) 6845 if ((yylval.v.string = strdup(buf)) == NULL) 6846 err(1, "yylex: strdup"); 6847 return (token); 6848 } 6849 if (c == '\n') { 6850 yylval.lineno = file->lineno; 6851 file->lineno++; 6852 } 6853 if (c == EOF) 6854 return (0); 6855 return (c); 6856 } 6857 6858 int 6859 check_file_secrecy(int fd, const char *fname) 6860 { 6861 struct stat st; 6862 6863 if (fstat(fd, &st)) { 6864 warn("cannot stat %s", fname); 6865 return (-1); 6866 } 6867 if (st.st_uid != 0 && st.st_uid != getuid()) { 6868 warnx("%s: owner not root or current user", fname); 6869 return (-1); 6870 } 6871 if (st.st_mode & (S_IRWXG | S_IRWXO)) { 6872 warnx("%s: group/world readable/writeable", fname); 6873 return (-1); 6874 } 6875 return (0); 6876 } 6877 6878 struct file * 6879 pushfile(const char *name, int secret) 6880 { 6881 struct file *nfile; 6882 6883 if ((nfile = calloc(1, sizeof(struct file))) == NULL || 6884 (nfile->name = strdup(name)) == NULL) { 6885 warn("malloc"); 6886 return (NULL); 6887 } 6888 if (TAILQ_FIRST(&files) == NULL && strcmp(nfile->name, "-") == 0) { 6889 nfile->stream = stdin; 6890 free(nfile->name); 6891 if ((nfile->name = strdup("stdin")) == NULL) { 6892 warn("strdup"); 6893 free(nfile); 6894 return (NULL); 6895 } 6896 } else if ((nfile->stream = fopen(nfile->name, "r")) == NULL) { 6897 warn("%s", nfile->name); 6898 free(nfile->name); 6899 free(nfile); 6900 return (NULL); 6901 } else if (secret && 6902 check_file_secrecy(fileno(nfile->stream), nfile->name)) { 6903 fclose(nfile->stream); 6904 free(nfile->name); 6905 free(nfile); 6906 return (NULL); 6907 } 6908 nfile->lineno = 1; 6909 TAILQ_INSERT_TAIL(&files, nfile, entry); 6910 return (nfile); 6911 } 6912 6913 int 6914 popfile(void) 6915 { 6916 struct file *prev; 6917 6918 if ((prev = TAILQ_PREV(file, files, entry)) != NULL) { 6919 prev->errors += file->errors; 6920 TAILQ_REMOVE(&files, file, entry); 6921 fclose(file->stream); 6922 free(file->name); 6923 free(file); 6924 file = prev; 6925 return (0); 6926 } 6927 return (EOF); 6928 } 6929 6930 int 6931 parse_config(char *filename, struct pfctl *xpf) 6932 { 6933 int errors = 0; 6934 struct sym *sym; 6935 6936 pf = xpf; 6937 errors = 0; 6938 rulestate = PFCTL_STATE_NONE; 6939 returnicmpdefault = (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT; 6940 returnicmp6default = 6941 (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT; 6942 blockpolicy = PFRULE_DROP; 6943 failpolicy = PFRULE_DROP; 6944 require_order = 1; 6945 6946 if ((file = pushfile(filename, 0)) == NULL) { 6947 warn("cannot open the main config file!"); 6948 return (-1); 6949 } 6950 6951 yyparse(); 6952 errors = file->errors; 6953 popfile(); 6954 6955 /* Free macros and check which have not been used. */ 6956 while ((sym = TAILQ_FIRST(&symhead))) { 6957 if ((pf->opts & PF_OPT_VERBOSE2) && !sym->used) 6958 fprintf(stderr, "warning: macro '%s' not " 6959 "used\n", sym->nam); 6960 free(sym->nam); 6961 free(sym->val); 6962 TAILQ_REMOVE(&symhead, sym, entry); 6963 free(sym); 6964 } 6965 6966 return (errors ? -1 : 0); 6967 } 6968 6969 int 6970 symset(const char *nam, const char *val, int persist) 6971 { 6972 struct sym *sym; 6973 6974 for (sym = TAILQ_FIRST(&symhead); sym && strcmp(nam, sym->nam); 6975 sym = TAILQ_NEXT(sym, entry)) 6976 ; /* nothing */ 6977 6978 if (sym != NULL) { 6979 if (sym->persist == 1) 6980 return (0); 6981 else { 6982 free(sym->nam); 6983 free(sym->val); 6984 TAILQ_REMOVE(&symhead, sym, entry); 6985 free(sym); 6986 } 6987 } 6988 if ((sym = calloc(1, sizeof(*sym))) == NULL) 6989 return (-1); 6990 6991 sym->nam = strdup(nam); 6992 if (sym->nam == NULL) { 6993 free(sym); 6994 return (-1); 6995 } 6996 sym->val = strdup(val); 6997 if (sym->val == NULL) { 6998 free(sym->nam); 6999 free(sym); 7000 return (-1); 7001 } 7002 sym->used = 0; 7003 sym->persist = persist; 7004 TAILQ_INSERT_TAIL(&symhead, sym, entry); 7005 return (0); 7006 } 7007 7008 int 7009 pfctl_cmdline_symset(char *s) 7010 { 7011 char *sym, *val; 7012 int ret; 7013 7014 if ((val = strrchr(s, '=')) == NULL) 7015 return (-1); 7016 7017 if ((sym = malloc(strlen(s) - strlen(val) + 1)) == NULL) 7018 err(1, "pfctl_cmdline_symset: malloc"); 7019 7020 strlcpy(sym, s, strlen(s) - strlen(val) + 1); 7021 7022 ret = symset(sym, val + 1, 1); 7023 free(sym); 7024 7025 return (ret); 7026 } 7027 7028 char * 7029 symget(const char *nam) 7030 { 7031 struct sym *sym; 7032 7033 TAILQ_FOREACH(sym, &symhead, entry) 7034 if (strcmp(nam, sym->nam) == 0) { 7035 sym->used = 1; 7036 return (sym->val); 7037 } 7038 return (NULL); 7039 } 7040 7041 void 7042 mv_rules(struct pfctl_ruleset *src, struct pfctl_ruleset *dst) 7043 { 7044 int i; 7045 struct pfctl_rule *r; 7046 7047 for (i = 0; i < PF_RULESET_MAX; ++i) { 7048 while ((r = TAILQ_FIRST(src->rules[i].active.ptr)) 7049 != NULL) { 7050 TAILQ_REMOVE(src->rules[i].active.ptr, r, entries); 7051 TAILQ_INSERT_TAIL(dst->rules[i].active.ptr, r, entries); 7052 dst->anchor->match++; 7053 } 7054 src->anchor->match = 0; 7055 while ((r = TAILQ_FIRST(src->rules[i].inactive.ptr)) 7056 != NULL) { 7057 TAILQ_REMOVE(src->rules[i].inactive.ptr, r, entries); 7058 TAILQ_INSERT_TAIL(dst->rules[i].inactive.ptr, 7059 r, entries); 7060 } 7061 } 7062 } 7063 7064 void 7065 mv_eth_rules(struct pfctl_eth_ruleset *src, struct pfctl_eth_ruleset *dst) 7066 { 7067 struct pfctl_eth_rule *r; 7068 7069 while ((r = TAILQ_FIRST(&src->rules)) != NULL) { 7070 TAILQ_REMOVE(&src->rules, r, entries); 7071 TAILQ_INSERT_TAIL(&dst->rules, r, entries); 7072 dst->anchor->match++; 7073 } 7074 src->anchor->match = 0; 7075 } 7076 7077 void 7078 decide_address_family(struct node_host *n, sa_family_t *af) 7079 { 7080 if (*af != 0 || n == NULL) 7081 return; 7082 *af = n->af; 7083 while ((n = n->next) != NULL) { 7084 if (n->af != *af) { 7085 *af = 0; 7086 return; 7087 } 7088 } 7089 } 7090 7091 void 7092 remove_invalid_hosts(struct node_host **nh, sa_family_t *af) 7093 { 7094 struct node_host *n = *nh, *prev = NULL; 7095 7096 while (n != NULL) { 7097 if (*af && n->af && n->af != *af) { 7098 /* unlink and free n */ 7099 struct node_host *next = n->next; 7100 7101 /* adjust tail pointer */ 7102 if (n == (*nh)->tail) 7103 (*nh)->tail = prev; 7104 /* adjust previous node's next pointer */ 7105 if (prev == NULL) 7106 *nh = next; 7107 else 7108 prev->next = next; 7109 /* free node */ 7110 if (n->ifname != NULL) 7111 free(n->ifname); 7112 free(n); 7113 n = next; 7114 } else { 7115 if (n->af && !*af) 7116 *af = n->af; 7117 prev = n; 7118 n = n->next; 7119 } 7120 } 7121 } 7122 7123 int 7124 invalid_redirect(struct node_host *nh, sa_family_t af) 7125 { 7126 if (!af) { 7127 struct node_host *n; 7128 7129 /* tables and dyniftl are ok without an address family */ 7130 for (n = nh; n != NULL; n = n->next) { 7131 if (n->addr.type != PF_ADDR_TABLE && 7132 n->addr.type != PF_ADDR_DYNIFTL) { 7133 yyerror("address family not given and " 7134 "translation address expands to multiple " 7135 "address families"); 7136 return (1); 7137 } 7138 } 7139 } 7140 if (nh == NULL) { 7141 yyerror("no translation address with matching address family " 7142 "found."); 7143 return (1); 7144 } 7145 return (0); 7146 } 7147 7148 int 7149 atoul(char *s, u_long *ulvalp) 7150 { 7151 u_long ulval; 7152 char *ep; 7153 7154 errno = 0; 7155 ulval = strtoul(s, &ep, 0); 7156 if (s[0] == '\0' || *ep != '\0') 7157 return (-1); 7158 if (errno == ERANGE && ulval == ULONG_MAX) 7159 return (-1); 7160 *ulvalp = ulval; 7161 return (0); 7162 } 7163 7164 int 7165 getservice(char *n) 7166 { 7167 struct servent *s; 7168 u_long ulval; 7169 7170 if (atoul(n, &ulval) == 0) { 7171 if (ulval > 65535) { 7172 yyerror("illegal port value %lu", ulval); 7173 return (-1); 7174 } 7175 return (htons(ulval)); 7176 } else { 7177 s = getservbyname(n, "tcp"); 7178 if (s == NULL) 7179 s = getservbyname(n, "udp"); 7180 if (s == NULL) 7181 s = getservbyname(n, "sctp"); 7182 if (s == NULL) { 7183 yyerror("unknown port %s", n); 7184 return (-1); 7185 } 7186 return (s->s_port); 7187 } 7188 } 7189 7190 int 7191 rule_label(struct pfctl_rule *r, char *s[PF_RULE_MAX_LABEL_COUNT]) 7192 { 7193 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++) { 7194 if (s[i] == NULL) 7195 return (0); 7196 7197 if (strlcpy(r->label[i], s[i], sizeof(r->label[0])) >= 7198 sizeof(r->label[0])) { 7199 yyerror("rule label too long (max %d chars)", 7200 sizeof(r->label[0])-1); 7201 return (-1); 7202 } 7203 } 7204 return (0); 7205 } 7206 7207 int 7208 eth_rule_label(struct pfctl_eth_rule *r, char *s[PF_RULE_MAX_LABEL_COUNT]) 7209 { 7210 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++) { 7211 if (s[i] == NULL) 7212 return (0); 7213 7214 if (strlcpy(r->label[i], s[i], sizeof(r->label[0])) >= 7215 sizeof(r->label[0])) { 7216 yyerror("rule label too long (max %d chars)", 7217 sizeof(r->label[0])-1); 7218 return (-1); 7219 } 7220 } 7221 return (0); 7222 } 7223 7224 u_int16_t 7225 parseicmpspec(char *w, sa_family_t af) 7226 { 7227 const struct icmpcodeent *p; 7228 u_long ulval; 7229 u_int8_t icmptype; 7230 7231 if (af == AF_INET) 7232 icmptype = returnicmpdefault >> 8; 7233 else 7234 icmptype = returnicmp6default >> 8; 7235 7236 if (atoul(w, &ulval) == -1) { 7237 if ((p = geticmpcodebyname(icmptype, w, af)) == NULL) { 7238 yyerror("unknown icmp code %s", w); 7239 return (0); 7240 } 7241 ulval = p->code; 7242 } 7243 if (ulval > 255) { 7244 yyerror("invalid icmp code %lu", ulval); 7245 return (0); 7246 } 7247 return (icmptype << 8 | ulval); 7248 } 7249 7250 int 7251 parseport(char *port, struct range *r, int extensions) 7252 { 7253 char *p = strchr(port, ':'); 7254 7255 if (p == NULL) { 7256 if ((r->a = getservice(port)) == -1) 7257 return (-1); 7258 r->b = 0; 7259 r->t = PF_OP_NONE; 7260 return (0); 7261 } 7262 if ((extensions & PPORT_STAR) && !strcmp(p+1, "*")) { 7263 *p = 0; 7264 if ((r->a = getservice(port)) == -1) 7265 return (-1); 7266 r->b = 0; 7267 r->t = PF_OP_IRG; 7268 return (0); 7269 } 7270 if ((extensions & PPORT_RANGE)) { 7271 *p++ = 0; 7272 if ((r->a = getservice(port)) == -1 || 7273 (r->b = getservice(p)) == -1) 7274 return (-1); 7275 if (r->a == r->b) { 7276 r->b = 0; 7277 r->t = PF_OP_NONE; 7278 } else 7279 r->t = PF_OP_RRG; 7280 return (0); 7281 } 7282 return (-1); 7283 } 7284 7285 int 7286 pfctl_load_anchors(int dev, struct pfctl *pf, struct pfr_buffer *trans) 7287 { 7288 struct loadanchors *la; 7289 7290 TAILQ_FOREACH(la, &loadanchorshead, entries) { 7291 if (pf->opts & PF_OPT_VERBOSE) 7292 fprintf(stderr, "\nLoading anchor %s from %s\n", 7293 la->anchorname, la->filename); 7294 if (pfctl_rules(dev, la->filename, pf->opts, pf->optimize, 7295 la->anchorname, trans) == -1) 7296 return (-1); 7297 } 7298 7299 return (0); 7300 } 7301 7302 int 7303 kw_casecmp(const void *k, const void *e) 7304 { 7305 return (strcasecmp(k, ((const struct keywords *)e)->k_name)); 7306 } 7307 7308 int 7309 map_tos(char *s, int *val) 7310 { 7311 /* DiffServ Codepoints and other TOS mappings */ 7312 const struct keywords toswords[] = { 7313 { "af11", IPTOS_DSCP_AF11 }, 7314 { "af12", IPTOS_DSCP_AF12 }, 7315 { "af13", IPTOS_DSCP_AF13 }, 7316 { "af21", IPTOS_DSCP_AF21 }, 7317 { "af22", IPTOS_DSCP_AF22 }, 7318 { "af23", IPTOS_DSCP_AF23 }, 7319 { "af31", IPTOS_DSCP_AF31 }, 7320 { "af32", IPTOS_DSCP_AF32 }, 7321 { "af33", IPTOS_DSCP_AF33 }, 7322 { "af41", IPTOS_DSCP_AF41 }, 7323 { "af42", IPTOS_DSCP_AF42 }, 7324 { "af43", IPTOS_DSCP_AF43 }, 7325 { "critical", IPTOS_PREC_CRITIC_ECP }, 7326 { "cs0", IPTOS_DSCP_CS0 }, 7327 { "cs1", IPTOS_DSCP_CS1 }, 7328 { "cs2", IPTOS_DSCP_CS2 }, 7329 { "cs3", IPTOS_DSCP_CS3 }, 7330 { "cs4", IPTOS_DSCP_CS4 }, 7331 { "cs5", IPTOS_DSCP_CS5 }, 7332 { "cs6", IPTOS_DSCP_CS6 }, 7333 { "cs7", IPTOS_DSCP_CS7 }, 7334 { "ef", IPTOS_DSCP_EF }, 7335 { "inetcontrol", IPTOS_PREC_INTERNETCONTROL }, 7336 { "lowdelay", IPTOS_LOWDELAY }, 7337 { "netcontrol", IPTOS_PREC_NETCONTROL }, 7338 { "reliability", IPTOS_RELIABILITY }, 7339 { "throughput", IPTOS_THROUGHPUT }, 7340 { "va", IPTOS_DSCP_VA } 7341 }; 7342 const struct keywords *p; 7343 7344 p = bsearch(s, toswords, sizeof(toswords)/sizeof(toswords[0]), 7345 sizeof(toswords[0]), kw_casecmp); 7346 7347 if (p) { 7348 *val = p->k_val; 7349 return (1); 7350 } 7351 return (0); 7352 } 7353 7354 int 7355 rt_tableid_max(void) 7356 { 7357 #ifdef __FreeBSD__ 7358 int fibs; 7359 size_t l = sizeof(fibs); 7360 7361 if (sysctlbyname("net.fibs", &fibs, &l, NULL, 0) == -1) 7362 fibs = 16; /* XXX RT_MAXFIBS, at least limit it some. */ 7363 /* 7364 * As the OpenBSD code only compares > and not >= we need to adjust 7365 * here given we only accept values of 0..n and want to avoid #ifdefs 7366 * in the grammar. 7367 */ 7368 return (fibs - 1); 7369 #else 7370 return (RT_TABLEID_MAX); 7371 #endif 7372 } 7373 7374 struct node_mac* 7375 node_mac_from_string(const char *str) 7376 { 7377 struct node_mac *m; 7378 7379 m = calloc(1, sizeof(struct node_mac)); 7380 if (m == NULL) 7381 err(1, "mac: calloc"); 7382 7383 if (sscanf(str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", 7384 &m->mac[0], &m->mac[1], &m->mac[2], &m->mac[3], &m->mac[4], 7385 &m->mac[5]) != 6) { 7386 free(m); 7387 yyerror("invalid MAC address"); 7388 return (NULL); 7389 } 7390 7391 memset(m->mask, 0xff, ETHER_ADDR_LEN); 7392 m->isset = true; 7393 m->next = NULL; 7394 m->tail = m; 7395 7396 return (m); 7397 } 7398 7399 struct node_mac* 7400 node_mac_from_string_masklen(const char *str, int masklen) 7401 { 7402 struct node_mac *m; 7403 7404 if (masklen < 0 || masklen > (ETHER_ADDR_LEN * 8)) { 7405 yyerror("invalid MAC mask length"); 7406 return (NULL); 7407 } 7408 7409 m = node_mac_from_string(str); 7410 if (m == NULL) 7411 return (NULL); 7412 7413 memset(m->mask, 0, ETHER_ADDR_LEN); 7414 for (int i = 0; i < masklen; i++) 7415 m->mask[i / 8] |= 1 << (i % 8); 7416 7417 return (m); 7418 } 7419 7420 struct node_mac* 7421 node_mac_from_string_mask(const char *str, const char *mask) 7422 { 7423 struct node_mac *m; 7424 7425 m = node_mac_from_string(str); 7426 if (m == NULL) 7427 return (NULL); 7428 7429 if (sscanf(mask, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", 7430 &m->mask[0], &m->mask[1], &m->mask[2], &m->mask[3], &m->mask[4], 7431 &m->mask[5]) != 6) { 7432 free(m); 7433 yyerror("invalid MAC mask"); 7434 return (NULL); 7435 } 7436 7437 return (m); 7438 } 7439