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