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