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