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