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