1 /* $OpenBSD: parse.y,v 1.554 2008/10/17 12:59:53 henning Exp $ */
4 * SPDX-License-Identifier: BSD-2-Clause
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.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #define PFIOC_USE_LATEST
37 #include <sys/types.h>
38 #include <sys/socket.h>
41 #include <sys/sysctl.h>
44 #include <netinet/in.h>
45 #include <netinet/in_systm.h>
46 #include <netinet/ip.h>
47 #include <netinet/ip_icmp.h>
48 #include <netinet/icmp6.h>
49 #include <net/pfvar.h>
50 #include <arpa/inet.h>
51 #include <net/altq/altq.h>
52 #include <net/altq/altq_cbq.h>
53 #include <net/altq/altq_codel.h>
54 #include <net/altq/altq_priq.h>
55 #include <net/altq/altq_hfsc.h>
56 #include <net/altq/altq_fairq.h>
74 #include "pfctl_parser.h"
77 static struct pfctl *pf = NULL;
79 static int rulestate = 0;
80 static u_int16_t returnicmpdefault =
81 (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
82 static u_int16_t returnicmp6default =
83 (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
84 static int blockpolicy = PFRULE_DROP;
85 static int failpolicy = PFRULE_DROP;
86 static int require_order = 1;
87 static int default_statelock;
89 static TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
91 TAILQ_ENTRY(file) entry;
97 struct file *pushfile(const char *, int);
99 int check_file_secrecy(int, const char *);
102 int yyerror(const char *, ...);
103 int kw_cmp(const void *, const void *);
109 static TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
111 TAILQ_ENTRY(sym) entry;
117 int symset(const char *, const char *, int);
118 char *symget(const char *);
120 int atoul(char *, u_long *);
133 struct node_proto *next;
134 struct node_proto *tail;
140 struct node_port *next;
141 struct node_port *tail;
147 struct node_uid *next;
148 struct node_uid *tail;
154 struct node_gid *next;
155 struct node_gid *tail;
162 struct node_icmp *next;
163 struct node_icmp *tail;
166 enum { PF_STATE_OPT_MAX, PF_STATE_OPT_NOSYNC, PF_STATE_OPT_SRCTRACK,
167 PF_STATE_OPT_MAX_SRC_STATES, PF_STATE_OPT_MAX_SRC_CONN,
168 PF_STATE_OPT_MAX_SRC_CONN_RATE, PF_STATE_OPT_MAX_SRC_NODES,
169 PF_STATE_OPT_OVERLOAD, PF_STATE_OPT_STATELOCK,
170 PF_STATE_OPT_TIMEOUT, PF_STATE_OPT_SLOPPY, };
172 enum { PF_SRCTRACK_NONE, PF_SRCTRACK, PF_SRCTRACK_GLOBAL, PF_SRCTRACK_RULE };
174 struct node_state_opt {
177 u_int32_t max_states;
178 u_int32_t max_src_states;
179 u_int32_t max_src_conn;
186 char tblname[PF_TABLE_NAME_SIZE];
188 u_int32_t max_src_nodes;
196 struct node_state_opt *next;
197 struct node_state_opt *tail;
201 struct node_host *host;
202 struct node_port *port;
205 static struct node_queue {
206 char queue[PF_QNAME_SIZE];
207 char parent[PF_QNAME_SIZE];
208 char ifname[IFNAMSIZ];
210 struct node_queue *next;
211 struct node_queue *tail;
214 struct node_qassign {
219 static struct filter_opts {
221 #define FOM_FLAGS 0x01
222 #define FOM_ICMP 0x02
224 #define FOM_KEEP 0x08
225 #define FOM_SRCTRACK 0x10
226 #define FOM_SETPRIO 0x0400
227 #define FOM_PRIO 0x2000
228 struct node_uid *uid;
229 struct node_gid *gid;
236 struct node_icmp *icmpspec;
241 struct node_state_opt *options;
245 char *label[PF_RULE_MAX_LABEL_COUNT];
247 struct node_qassign queues;
250 u_int8_t match_tag_not;
253 u_int8_t set_prio[2];
255 struct node_host *addr;
260 static struct antispoof_opts {
261 char *label[PF_RULE_MAX_LABEL_COUNT];
266 static struct scrub_opts {
268 #define SOM_MINTTL 0x01
269 #define SOM_MAXMSS 0x02
270 #define SOM_FRAGCACHE 0x04
271 #define SOM_SETTOS 0x08
280 u_int8_t match_tag_not;
284 static struct queue_opts {
286 #define QOM_BWSPEC 0x01
287 #define QOM_SCHEDULER 0x02
288 #define QOM_PRIORITY 0x04
289 #define QOM_TBRSIZE 0x08
290 #define QOM_QLIMIT 0x10
291 struct node_queue_bw queue_bwspec;
292 struct node_queue_opt scheduler;
294 unsigned int tbrsize;
298 static struct table_opts {
301 struct node_tinithead init_nodes;
304 static struct pool_opts {
306 #define POM_TYPE 0x01
307 #define POM_STICKYADDRESS 0x02
311 struct pf_poolhashkey *key;
312 struct pf_mape_portset mape;
316 static struct codel_opts codel_opts;
317 static struct node_hfsc_opts hfsc_opts;
318 static struct node_fairq_opts fairq_opts;
319 static struct node_state_opt *keep_state_defaults = NULL;
321 int disallow_table(struct node_host *, const char *);
322 int disallow_urpf_failed(struct node_host *, const char *);
323 int disallow_alias(struct node_host *, const char *);
324 int rule_consistent(struct pfctl_rule *, int);
325 int filter_consistent(struct pfctl_rule *, int);
326 int nat_consistent(struct pfctl_rule *);
327 int rdr_consistent(struct pfctl_rule *);
328 int process_tabledef(char *, struct table_opts *);
329 void expand_label_str(char *, size_t, const char *, const char *);
330 void expand_label_if(const char *, char *, size_t, const char *);
331 void expand_label_addr(const char *, char *, size_t, u_int8_t,
333 void expand_label_port(const char *, char *, size_t,
335 void expand_label_proto(const char *, char *, size_t, u_int8_t);
336 void expand_label_nr(const char *, char *, size_t);
337 void expand_label(char *, size_t, const char *, u_int8_t,
338 struct node_host *, struct node_port *, struct node_host *,
339 struct node_port *, u_int8_t);
340 void expand_rule(struct pfctl_rule *, struct node_if *,
341 struct node_host *, struct node_proto *, struct node_os *,
342 struct node_host *, struct node_port *, struct node_host *,
343 struct node_port *, struct node_uid *, struct node_gid *,
344 struct node_icmp *, const char *);
345 int expand_altq(struct pf_altq *, struct node_if *,
346 struct node_queue *, struct node_queue_bw bwspec,
347 struct node_queue_opt *);
348 int expand_queue(struct pf_altq *, struct node_if *,
349 struct node_queue *, struct node_queue_bw,
350 struct node_queue_opt *);
351 int expand_skip_interface(struct node_if *);
353 int check_rulestate(int);
354 int getservice(char *);
355 int rule_label(struct pfctl_rule *, char *s[PF_RULE_MAX_LABEL_COUNT]);
356 int rt_tableid_max(void);
358 void mv_rules(struct pfctl_ruleset *, struct pfctl_ruleset *);
359 void decide_address_family(struct node_host *, sa_family_t *);
360 void remove_invalid_hosts(struct node_host **, sa_family_t *);
361 int invalid_redirect(struct node_host *, sa_family_t);
362 u_int16_t parseicmpspec(char *, sa_family_t);
363 int kw_casecmp(const void *, const void *);
364 int map_tos(char *string, int *);
366 static TAILQ_HEAD(loadanchorshead, loadanchors)
367 loadanchorshead = TAILQ_HEAD_INITIALIZER(loadanchorshead);
370 TAILQ_ENTRY(loadanchors) entries;
393 struct node_if *interface;
394 struct node_proto *proto;
395 struct node_icmp *icmp;
396 struct node_host *host;
398 struct node_port *port;
399 struct node_uid *uid;
400 struct node_gid *gid;
401 struct node_state_opt *state_opt;
404 struct peer src, dst;
405 struct node_os *src_os;
408 struct node_host *host;
412 struct pf_poolhashkey *key;
415 struct node_host *host;
420 struct node_state_opt *options;
431 struct pf_poolhashkey *hashkey;
432 struct node_queue *queue;
433 struct node_queue_opt queue_options;
434 struct node_queue_bw queue_bwspec;
435 struct node_qassign qassign;
436 struct filter_opts filter_opts;
437 struct antispoof_opts antispoof_opts;
438 struct queue_opts queue_opts;
439 struct scrub_opts scrub_opts;
440 struct table_opts table_opts;
441 struct pool_opts pool_opts;
442 struct node_hfsc_opts hfsc_opts;
443 struct node_fairq_opts fairq_opts;
444 struct codel_opts codel_opts;
449 #define PPORT_RANGE 1
451 int parseport(char *, struct range *r, int);
453 #define DYNIF_MULTIADDR(addr) ((addr).type == PF_ADDR_DYNIFTL && \
454 (!((addr).iflags & PFI_AFLAG_NOALIAS) || \
455 !isdigit((addr).v.ifname[strlen((addr).v.ifname)-1])))
459 %token PASS BLOCK SCRUB RETURN IN OS OUT LOG QUICK ON FROM TO FLAGS
460 %token RETURNRST RETURNICMP RETURNICMP6 PROTO INET INET6 ALL ANY ICMPTYPE
461 %token ICMP6TYPE CODE KEEP MODULATE STATE PORT RDR NAT BINAT ARROW NODF
462 %token MINTTL ERROR ALLOWOPTS FASTROUTE FILENAME ROUTETO DUPTO REPLYTO NO LABEL
463 %token NOROUTE URPFFAILED FRAGMENT USER GROUP MAXMSS MAXIMUM TTL TOS DROP TABLE
464 %token REASSEMBLE FRAGDROP FRAGCROP ANCHOR NATANCHOR RDRANCHOR BINATANCHOR
465 %token SET OPTIMIZATION TIMEOUT LIMIT LOGINTERFACE BLOCKPOLICY FAILPOLICY
466 %token RANDOMID REQUIREORDER SYNPROXY FINGERPRINTS NOSYNC DEBUG SKIP HOSTID
467 %token ANTISPOOF FOR INCLUDE KEEPCOUNTERS
468 %token BITMASK RANDOM SOURCEHASH ROUNDROBIN STATICPORT PROBABILITY MAPEPORTSET
469 %token ALTQ CBQ CODEL PRIQ HFSC FAIRQ BANDWIDTH TBRSIZE LINKSHARE REALTIME
470 %token UPPERLIMIT QUEUE PRIORITY QLIMIT HOGS BUCKETS RTABLE TARGET INTERVAL
471 %token LOAD RULESET_OPTIMIZATION PRIO
472 %token STICKYADDRESS MAXSRCSTATES MAXSRCNODES SOURCETRACK GLOBAL RULE
473 %token MAXSRCCONN MAXSRCCONNRATE OVERLOAD FLUSH SLOPPY
474 %token TAGGED TAG IFBOUND FLOATING STATEPOLICY STATEDEFAULTS ROUTE SETTOS
475 %token DIVERTTO DIVERTREPLY
476 %token <v.string> STRING
477 %token <v.number> NUMBER
478 %token <v.i> PORTBINARY
479 %type <v.interface> interface if_list if_item_not if_item
480 %type <v.number> number icmptype icmp6type uid gid
481 %type <v.number> tos not yesno
482 %type <v.probability> probability
483 %type <v.i> no dir af fragcache optimizer
484 %type <v.i> sourcetrack flush unaryop statelock
485 %type <v.b> action nataction natpasslog scrubaction
486 %type <v.b> flags flag blockspec prio
487 %type <v.range> portplain portstar portrange
488 %type <v.hashkey> hashkey
489 %type <v.proto> proto proto_list proto_item
490 %type <v.number> protoval
491 %type <v.icmp> icmpspec
492 %type <v.icmp> icmp_list icmp_item
493 %type <v.icmp> icmp6_list icmp6_item
494 %type <v.number> reticmpspec reticmp6spec
495 %type <v.fromto> fromto
496 %type <v.peer> ipportspec from to
497 %type <v.host> ipspec toipspec xhost host dynaddr host_list
498 %type <v.host> redir_host_list redirspec
499 %type <v.host> route_host route_host_list routespec
500 %type <v.os> os xos os_list
501 %type <v.port> portspec port_list port_item
502 %type <v.uid> uids uid_list uid_item
503 %type <v.gid> gids gid_list gid_item
504 %type <v.route> route
505 %type <v.redirection> redirection redirpool
506 %type <v.string> label stringall tag anchorname
507 %type <v.string> string varstring numberstring
508 %type <v.keep_state> keep
509 %type <v.state_opt> state_opt_spec state_opt_list state_opt_item
510 %type <v.logquick> logquick quick log logopts logopt
511 %type <v.interface> antispoof_ifspc antispoof_iflst antispoof_if
512 %type <v.qassign> qname
513 %type <v.queue> qassign qassign_list qassign_item
514 %type <v.queue_options> scheduler
515 %type <v.number> cbqflags_list cbqflags_item
516 %type <v.number> priqflags_list priqflags_item
517 %type <v.hfsc_opts> hfscopts_list hfscopts_item hfsc_opts
518 %type <v.fairq_opts> fairqopts_list fairqopts_item fairq_opts
519 %type <v.codel_opts> codelopts_list codelopts_item codel_opts
520 %type <v.queue_bwspec> bandwidth
521 %type <v.filter_opts> filter_opts filter_opt filter_opts_l
522 %type <v.filter_opts> filter_sets filter_set filter_sets_l
523 %type <v.antispoof_opts> antispoof_opts antispoof_opt antispoof_opts_l
524 %type <v.queue_opts> queue_opts queue_opt queue_opts_l
525 %type <v.scrub_opts> scrub_opts scrub_opt scrub_opts_l
526 %type <v.table_opts> table_opts table_opt table_opts_l
527 %type <v.pool_opts> pool_opts pool_opt pool_opts_l
528 %type <v.tagged> tagged
529 %type <v.rtableid> rtable
532 ruleset : /* empty */
533 | ruleset include '\n'
535 | ruleset option '\n'
536 | ruleset scrubrule '\n'
537 | ruleset natrule '\n'
538 | ruleset binatrule '\n'
539 | ruleset pfrule '\n'
540 | ruleset anchorrule '\n'
541 | ruleset loadrule '\n'
542 | ruleset altqif '\n'
543 | ruleset queuespec '\n'
544 | ruleset varset '\n'
545 | ruleset antispoof '\n'
546 | ruleset tabledef '\n'
547 | '{' fakeanchor '}' '\n';
548 | ruleset error '\n' { file->errors++; }
551 include : INCLUDE STRING {
554 if ((nfile = pushfile($2, 0)) == NULL) {
555 yyerror("failed to include file %s", $2);
567 * apply to previouslys specified rule: must be careful to note
568 * what that is: pf or nat or binat or rdr
570 fakeanchor : fakeanchor '\n'
571 | fakeanchor anchorrule '\n'
572 | fakeanchor binatrule '\n'
573 | fakeanchor natrule '\n'
574 | fakeanchor pfrule '\n'
575 | fakeanchor error '\n'
579 if (!strcmp($1, "none"))
581 else if (!strcmp($1, "basic"))
582 $$ = PF_OPTIMIZE_BASIC;
583 else if (!strcmp($1, "profile"))
584 $$ = PF_OPTIMIZE_BASIC | PF_OPTIMIZE_PROFILE;
586 yyerror("unknown ruleset-optimization %s", $1);
592 option : SET OPTIMIZATION STRING {
593 if (check_rulestate(PFCTL_STATE_OPTION)) {
597 if (pfctl_set_optimization(pf, $3) != 0) {
598 yyerror("unknown optimization %s", $3);
604 | SET RULESET_OPTIMIZATION optimizer {
605 if (!(pf->opts & PF_OPT_OPTIMIZE)) {
606 pf->opts |= PF_OPT_OPTIMIZE;
610 | SET TIMEOUT timeout_spec
611 | SET TIMEOUT '{' optnl timeout_list '}'
612 | SET LIMIT limit_spec
613 | SET LIMIT '{' optnl limit_list '}'
614 | SET LOGINTERFACE stringall {
615 if (check_rulestate(PFCTL_STATE_OPTION)) {
619 if (pfctl_set_logif(pf, $3) != 0) {
620 yyerror("error setting loginterface %s", $3);
626 | SET HOSTID number {
627 if ($3 == 0 || $3 > UINT_MAX) {
628 yyerror("hostid must be non-zero");
631 if (pfctl_set_hostid(pf, $3) != 0) {
632 yyerror("error setting hostid %08x", $3);
636 | SET BLOCKPOLICY DROP {
637 if (pf->opts & PF_OPT_VERBOSE)
638 printf("set block-policy drop\n");
639 if (check_rulestate(PFCTL_STATE_OPTION))
641 blockpolicy = PFRULE_DROP;
643 | SET BLOCKPOLICY RETURN {
644 if (pf->opts & PF_OPT_VERBOSE)
645 printf("set block-policy return\n");
646 if (check_rulestate(PFCTL_STATE_OPTION))
648 blockpolicy = PFRULE_RETURN;
650 | SET FAILPOLICY DROP {
651 if (pf->opts & PF_OPT_VERBOSE)
652 printf("set fail-policy drop\n");
653 if (check_rulestate(PFCTL_STATE_OPTION))
655 failpolicy = PFRULE_DROP;
657 | SET FAILPOLICY RETURN {
658 if (pf->opts & PF_OPT_VERBOSE)
659 printf("set fail-policy return\n");
660 if (check_rulestate(PFCTL_STATE_OPTION))
662 failpolicy = PFRULE_RETURN;
664 | SET REQUIREORDER yesno {
665 if (pf->opts & PF_OPT_VERBOSE)
666 printf("set require-order %s\n",
667 $3 == 1 ? "yes" : "no");
670 | SET FINGERPRINTS STRING {
671 if (pf->opts & PF_OPT_VERBOSE)
672 printf("set fingerprints \"%s\"\n", $3);
673 if (check_rulestate(PFCTL_STATE_OPTION)) {
677 if (!pf->anchor->name[0]) {
678 if (pfctl_file_fingerprints(pf->dev,
680 yyerror("error loading "
681 "fingerprints %s", $3);
688 | SET STATEPOLICY statelock {
689 if (pf->opts & PF_OPT_VERBOSE)
692 printf("set state-policy floating\n");
695 printf("set state-policy if-bound\n");
698 default_statelock = $3;
701 if (check_rulestate(PFCTL_STATE_OPTION)) {
705 if (pfctl_set_debug(pf, $3) != 0) {
706 yyerror("error setting debuglevel %s", $3);
712 | SET SKIP interface {
713 if (expand_skip_interface($3) != 0) {
714 yyerror("error setting skip interface(s)");
718 | SET STATEDEFAULTS state_opt_list {
719 if (keep_state_defaults != NULL) {
720 yyerror("cannot redefine state-defaults");
723 keep_state_defaults = $3;
726 pf->keep_counters = true;
730 stringall : STRING { $$ = $1; }
732 if (($$ = strdup("all")) == NULL) {
733 err(1, "stringall: strdup");
738 string : STRING string {
739 if (asprintf(&$$, "%s %s", $1, $2) == -1)
740 err(1, "string: asprintf");
747 varstring : numberstring varstring {
748 if (asprintf(&$$, "%s %s", $1, $2) == -1)
749 err(1, "string: asprintf");
756 numberstring : NUMBER {
758 if (asprintf(&s, "%lld", (long long)$1) == -1) {
759 yyerror("string: asprintf");
767 varset : STRING '=' varstring {
768 if (pf->opts & PF_OPT_VERBOSE)
769 printf("%s = \"%s\"\n", $1, $3);
770 if (symset($1, $3, 0) == -1)
771 err(1, "cannot store variable %s", $1);
777 anchorname : STRING { $$ = $1; }
778 | /* empty */ { $$ = NULL; }
781 pfa_anchorlist : /* empty */
782 | pfa_anchorlist '\n'
783 | pfa_anchorlist pfrule '\n'
784 | pfa_anchorlist anchorrule '\n'
789 char ta[PF_ANCHOR_NAME_SIZE];
790 struct pfctl_ruleset *rs;
792 /* steping into a brace anchor */
797 /* create a holding ruleset in the root */
798 snprintf(ta, PF_ANCHOR_NAME_SIZE, "_%d", pf->bn);
799 rs = pf_find_or_create_ruleset(ta);
801 err(1, "pfa_anchor: pf_find_or_create_ruleset");
802 pf->astack[pf->asd] = rs->anchor;
803 pf->anchor = rs->anchor;
804 } '\n' pfa_anchorlist '}'
806 pf->alast = pf->anchor;
808 pf->anchor = pf->astack[pf->asd];
813 anchorrule : ANCHOR anchorname dir quick interface af proto fromto
814 filter_opts pfa_anchor
817 struct node_proto *proto;
819 if (check_rulestate(PFCTL_STATE_FILTER)) {
825 if ($2 && ($2[0] == '_' || strstr($2, "/_") != NULL)) {
827 yyerror("anchor names beginning with '_' "
828 "are reserved for internal use");
832 memset(&r, 0, sizeof(r));
833 if (pf->astack[pf->asd + 1]) {
834 /* move inline rules into relative location */
835 pfctl_anchor_setup(&r,
836 &pf->astack[pf->asd]->ruleset,
837 $2 ? $2 : pf->alast->name);
839 if (r.anchor == NULL)
840 err(1, "anchorrule: unable to "
843 if (pf->alast != r.anchor) {
844 if (r.anchor->match) {
845 yyerror("inline anchor '%s' "
850 mv_rules(&pf->alast->ruleset,
853 pf_remove_if_empty_ruleset(&pf->alast->ruleset);
854 pf->alast = r.anchor;
857 yyerror("anchors without explicit "
858 "rules must specify a name");
866 r.rtableid = $9.rtableid;
869 if (strlcpy(r.tagname, $9.tag,
870 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
871 yyerror("tag too long, max %u chars",
872 PF_TAG_NAME_SIZE - 1);
876 if (strlcpy(r.match_tagname, $9.match_tag,
877 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
878 yyerror("tag too long, max %u chars",
879 PF_TAG_NAME_SIZE - 1);
882 r.match_tag_not = $9.match_tag_not;
883 if (rule_label(&r, $9.label))
885 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++)
887 r.flags = $9.flags.b1;
888 r.flagset = $9.flags.b2;
889 if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
890 yyerror("flags always false");
893 if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
894 for (proto = $7; proto != NULL &&
895 proto->proto != IPPROTO_TCP;
898 if (proto == NULL && $7 != NULL) {
899 if ($9.flags.b1 || $9.flags.b2)
901 "flags only apply to tcp");
904 "OS fingerprinting only "
912 if ($9.keep.action) {
913 yyerror("cannot specify state handling "
919 if (strlcpy(r.match_tagname, $9.match_tag,
920 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
921 yyerror("tag too long, max %u chars",
922 PF_TAG_NAME_SIZE - 1);
925 r.match_tag_not = $9.match_tag_not;
926 if ($9.marker & FOM_PRIO) {
928 r.prio = PF_PRIO_ZERO;
932 if ($9.marker & FOM_SETPRIO) {
933 r.set_prio[0] = $9.set_prio[0];
934 r.set_prio[1] = $9.set_prio[1];
935 r.scrub_flags |= PFSTATE_SETPRIO;
938 decide_address_family($8.src.host, &r.af);
939 decide_address_family($8.dst.host, &r.af);
941 expand_rule(&r, $5, NULL, $7, $8.src_os,
942 $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
943 $9.uid, $9.gid, $9.icmpspec,
944 pf->astack[pf->asd + 1] ? pf->alast->name : $2);
946 pf->astack[pf->asd + 1] = NULL;
948 | NATANCHOR string interface af proto fromto rtable {
951 if (check_rulestate(PFCTL_STATE_NAT)) {
956 memset(&r, 0, sizeof(r));
961 decide_address_family($6.src.host, &r.af);
962 decide_address_family($6.dst.host, &r.af);
964 expand_rule(&r, $3, NULL, $5, $6.src_os,
965 $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
969 | RDRANCHOR string interface af proto fromto rtable {
972 if (check_rulestate(PFCTL_STATE_NAT)) {
977 memset(&r, 0, sizeof(r));
982 decide_address_family($6.src.host, &r.af);
983 decide_address_family($6.dst.host, &r.af);
985 if ($6.src.port != NULL) {
986 yyerror("source port parameter not supported"
990 if ($6.dst.port != NULL) {
991 if ($6.dst.port->next != NULL) {
992 yyerror("destination port list "
993 "expansion not supported in "
996 } else if ($6.dst.port->op != PF_OP_EQ) {
997 yyerror("destination port operators"
998 " not supported in rdr-anchor");
1001 r.dst.port[0] = $6.dst.port->port[0];
1002 r.dst.port[1] = $6.dst.port->port[1];
1003 r.dst.port_op = $6.dst.port->op;
1006 expand_rule(&r, $3, NULL, $5, $6.src_os,
1007 $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
1011 | BINATANCHOR string interface af proto fromto rtable {
1012 struct pfctl_rule r;
1014 if (check_rulestate(PFCTL_STATE_NAT)) {
1019 memset(&r, 0, sizeof(r));
1020 r.action = PF_BINAT;
1024 if ($5->next != NULL) {
1025 yyerror("proto list expansion"
1026 " not supported in binat-anchor");
1029 r.proto = $5->proto;
1033 if ($6.src.host != NULL || $6.src.port != NULL ||
1034 $6.dst.host != NULL || $6.dst.port != NULL) {
1035 yyerror("fromto parameter not supported"
1036 " in binat-anchor");
1040 decide_address_family($6.src.host, &r.af);
1041 decide_address_family($6.dst.host, &r.af);
1043 pfctl_append_rule(pf, &r, $2);
1048 loadrule : LOAD ANCHOR string FROM string {
1049 struct loadanchors *loadanchor;
1051 if (strlen(pf->anchor->name) + 1 +
1052 strlen($3) >= MAXPATHLEN) {
1053 yyerror("anchorname %s too long, max %u\n",
1054 $3, MAXPATHLEN - 1);
1058 loadanchor = calloc(1, sizeof(struct loadanchors));
1059 if (loadanchor == NULL)
1060 err(1, "loadrule: calloc");
1061 if ((loadanchor->anchorname = malloc(MAXPATHLEN)) ==
1063 err(1, "loadrule: malloc");
1064 if (pf->anchor->name[0])
1065 snprintf(loadanchor->anchorname, MAXPATHLEN,
1066 "%s/%s", pf->anchor->name, $3);
1068 strlcpy(loadanchor->anchorname, $3, MAXPATHLEN);
1069 if ((loadanchor->filename = strdup($5)) == NULL)
1070 err(1, "loadrule: strdup");
1072 TAILQ_INSERT_TAIL(&loadanchorshead, loadanchor,
1079 scrubaction : no SCRUB {
1088 scrubrule : scrubaction dir logquick interface af proto fromto scrub_opts
1090 struct pfctl_rule r;
1092 if (check_rulestate(PFCTL_STATE_SCRUB))
1095 memset(&r, 0, sizeof(r));
1103 yyerror("scrub rules do not support 'quick'");
1109 r.rule_flag |= PFRULE_NODF;
1111 r.rule_flag |= PFRULE_RANDOMID;
1112 if ($8.reassemble_tcp) {
1113 if (r.direction != PF_INOUT) {
1114 yyerror("reassemble tcp rules can not "
1115 "specify direction");
1118 r.rule_flag |= PFRULE_REASSEMBLE_TCP;
1121 r.min_ttl = $8.minttl;
1123 r.max_mss = $8.maxmss;
1124 if ($8.marker & SOM_SETTOS) {
1125 r.rule_flag |= PFRULE_SET_TOS;
1126 r.set_tos = $8.settos;
1129 r.rule_flag |= $8.fragcache;
1131 if (strlcpy(r.match_tagname, $8.match_tag,
1132 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1133 yyerror("tag too long, max %u chars",
1134 PF_TAG_NAME_SIZE - 1);
1137 r.match_tag_not = $8.match_tag_not;
1138 r.rtableid = $8.rtableid;
1140 expand_rule(&r, $4, NULL, $6, $7.src_os,
1141 $7.src.host, $7.src.port, $7.dst.host, $7.dst.port,
1142 NULL, NULL, NULL, "");
1147 bzero(&scrub_opts, sizeof scrub_opts);
1148 scrub_opts.rtableid = -1;
1151 { $$ = scrub_opts; }
1153 bzero(&scrub_opts, sizeof scrub_opts);
1154 scrub_opts.rtableid = -1;
1159 scrub_opts_l : scrub_opts_l scrub_opt
1164 if (scrub_opts.nodf) {
1165 yyerror("no-df cannot be respecified");
1168 scrub_opts.nodf = 1;
1171 if (scrub_opts.marker & SOM_MINTTL) {
1172 yyerror("min-ttl cannot be respecified");
1175 if ($2 < 0 || $2 > 255) {
1176 yyerror("illegal min-ttl value %d", $2);
1179 scrub_opts.marker |= SOM_MINTTL;
1180 scrub_opts.minttl = $2;
1183 if (scrub_opts.marker & SOM_MAXMSS) {
1184 yyerror("max-mss cannot be respecified");
1187 if ($2 < 0 || $2 > 65535) {
1188 yyerror("illegal max-mss value %d", $2);
1191 scrub_opts.marker |= SOM_MAXMSS;
1192 scrub_opts.maxmss = $2;
1195 if (scrub_opts.marker & SOM_SETTOS) {
1196 yyerror("set-tos cannot be respecified");
1199 scrub_opts.marker |= SOM_SETTOS;
1200 scrub_opts.settos = $2;
1203 if (scrub_opts.marker & SOM_FRAGCACHE) {
1204 yyerror("fragcache cannot be respecified");
1207 scrub_opts.marker |= SOM_FRAGCACHE;
1208 scrub_opts.fragcache = $1;
1210 | REASSEMBLE STRING {
1211 if (strcasecmp($2, "tcp") != 0) {
1212 yyerror("scrub reassemble supports only tcp, "
1218 if (scrub_opts.reassemble_tcp) {
1219 yyerror("reassemble tcp cannot be respecified");
1222 scrub_opts.reassemble_tcp = 1;
1225 if (scrub_opts.randomid) {
1226 yyerror("random-id cannot be respecified");
1229 scrub_opts.randomid = 1;
1232 if ($2 < 0 || $2 > rt_tableid_max()) {
1233 yyerror("invalid rtable id");
1236 scrub_opts.rtableid = $2;
1238 | not TAGGED string {
1239 scrub_opts.match_tag = $3;
1240 scrub_opts.match_tag_not = $1;
1244 fragcache : FRAGMENT REASSEMBLE { $$ = 0; /* default */ }
1245 | FRAGMENT FRAGCROP { $$ = 0; }
1246 | FRAGMENT FRAGDROP { $$ = 0; }
1249 antispoof : ANTISPOOF logquick antispoof_ifspc af antispoof_opts {
1250 struct pfctl_rule r;
1251 struct node_host *h = NULL, *hh;
1252 struct node_if *i, *j;
1254 if (check_rulestate(PFCTL_STATE_FILTER))
1257 for (i = $3; i; i = i->next) {
1258 bzero(&r, sizeof(r));
1261 r.direction = PF_IN;
1266 if (rule_label(&r, $5.label))
1268 r.rtableid = $5.rtableid;
1269 j = calloc(1, sizeof(struct node_if));
1271 err(1, "antispoof: calloc");
1272 if (strlcpy(j->ifname, i->ifname,
1273 sizeof(j->ifname)) >= sizeof(j->ifname)) {
1275 yyerror("interface name too long");
1280 h = calloc(1, sizeof(*h));
1282 err(1, "address: calloc");
1283 h->addr.type = PF_ADDR_DYNIFTL;
1285 if (strlcpy(h->addr.v.ifname, i->ifname,
1286 sizeof(h->addr.v.ifname)) >=
1287 sizeof(h->addr.v.ifname)) {
1290 "interface name too long");
1293 hh = malloc(sizeof(*hh));
1295 err(1, "address: malloc");
1296 bcopy(h, hh, sizeof(*hh));
1297 h->addr.iflags = PFI_AFLAG_NETWORK;
1299 h = ifa_lookup(j->ifname,
1305 expand_rule(&r, j, NULL, NULL, NULL, h,
1306 NULL, NULL, NULL, NULL, NULL,
1309 if ((i->ifa_flags & IFF_LOOPBACK) == 0) {
1310 bzero(&r, sizeof(r));
1313 r.direction = PF_IN;
1318 if (rule_label(&r, $5.label))
1320 r.rtableid = $5.rtableid;
1324 h = ifa_lookup(i->ifname, 0);
1326 expand_rule(&r, NULL, NULL,
1327 NULL, NULL, h, NULL, NULL,
1328 NULL, NULL, NULL, NULL, "");
1332 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++)
1337 antispoof_ifspc : FOR antispoof_if { $$ = $2; }
1338 | FOR '{' optnl antispoof_iflst '}' { $$ = $4; }
1341 antispoof_iflst : antispoof_if optnl { $$ = $1; }
1342 | antispoof_iflst comma antispoof_if optnl {
1343 $1->tail->next = $3;
1349 antispoof_if : if_item { $$ = $1; }
1357 bzero(&antispoof_opts, sizeof antispoof_opts);
1358 antispoof_opts.rtableid = -1;
1361 { $$ = antispoof_opts; }
1363 bzero(&antispoof_opts, sizeof antispoof_opts);
1364 antispoof_opts.rtableid = -1;
1365 $$ = antispoof_opts;
1369 antispoof_opts_l : antispoof_opts_l antispoof_opt
1373 antispoof_opt : label {
1374 if (antispoof_opts.labelcount >= PF_RULE_MAX_LABEL_COUNT) {
1375 yyerror("label can only be used %d times", PF_RULE_MAX_LABEL_COUNT);
1378 antispoof_opts.label[antispoof_opts.labelcount++] = $1;
1381 if ($2 < 0 || $2 > rt_tableid_max()) {
1382 yyerror("invalid rtable id");
1385 antispoof_opts.rtableid = $2;
1389 not : '!' { $$ = 1; }
1390 | /* empty */ { $$ = 0; }
1393 tabledef : TABLE '<' STRING '>' table_opts {
1394 struct node_host *h, *nh;
1395 struct node_tinit *ti, *nti;
1397 if (strlen($3) >= PF_TABLE_NAME_SIZE) {
1398 yyerror("table name too long, max %d chars",
1399 PF_TABLE_NAME_SIZE - 1);
1403 if (pf->loadopt & PFCTL_FLAG_TABLE)
1404 if (process_tabledef($3, &$5)) {
1409 for (ti = SIMPLEQ_FIRST(&$5.init_nodes);
1410 ti != SIMPLEQ_END(&$5.init_nodes); ti = nti) {
1413 for (h = ti->host; h != NULL; h = nh) {
1417 nti = SIMPLEQ_NEXT(ti, entries);
1424 bzero(&table_opts, sizeof table_opts);
1425 SIMPLEQ_INIT(&table_opts.init_nodes);
1428 { $$ = table_opts; }
1431 bzero(&table_opts, sizeof table_opts);
1432 SIMPLEQ_INIT(&table_opts.init_nodes);
1437 table_opts_l : table_opts_l table_opt
1441 table_opt : STRING {
1442 if (!strcmp($1, "const"))
1443 table_opts.flags |= PFR_TFLAG_CONST;
1444 else if (!strcmp($1, "persist"))
1445 table_opts.flags |= PFR_TFLAG_PERSIST;
1446 else if (!strcmp($1, "counters"))
1447 table_opts.flags |= PFR_TFLAG_COUNTERS;
1449 yyerror("invalid table option '%s'", $1);
1455 | '{' optnl '}' { table_opts.init_addr = 1; }
1456 | '{' optnl host_list '}' {
1457 struct node_host *n;
1458 struct node_tinit *ti;
1460 for (n = $3; n != NULL; n = n->next) {
1461 switch (n->addr.type) {
1462 case PF_ADDR_ADDRMASK:
1465 yyerror("address ranges are not "
1466 "permitted inside tables");
1468 case PF_ADDR_DYNIFTL:
1469 yyerror("dynamic addresses are not "
1470 "permitted inside tables");
1473 yyerror("tables cannot contain tables");
1475 case PF_ADDR_NOROUTE:
1476 yyerror("\"no-route\" is not permitted "
1479 case PF_ADDR_URPFFAILED:
1480 yyerror("\"urpf-failed\" is not "
1481 "permitted inside tables");
1484 yyerror("unknown address type %d",
1489 if (!(ti = calloc(1, sizeof(*ti))))
1490 err(1, "table_opt: calloc");
1492 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1494 table_opts.init_addr = 1;
1497 struct node_tinit *ti;
1499 if (!(ti = calloc(1, sizeof(*ti))))
1500 err(1, "table_opt: calloc");
1502 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1504 table_opts.init_addr = 1;
1508 altqif : ALTQ interface queue_opts QUEUE qassign {
1511 if (check_rulestate(PFCTL_STATE_QUEUE))
1514 memset(&a, 0, sizeof(a));
1515 if ($3.scheduler.qtype == ALTQT_NONE) {
1516 yyerror("no scheduler specified!");
1519 a.scheduler = $3.scheduler.qtype;
1520 a.qlimit = $3.qlimit;
1521 a.tbrsize = $3.tbrsize;
1522 if ($5 == NULL && $3.scheduler.qtype != ALTQT_CODEL) {
1523 yyerror("no child queues specified");
1526 if (expand_altq(&a, $2, $5, $3.queue_bwspec,
1532 queuespec : QUEUE STRING interface queue_opts qassign {
1535 if (check_rulestate(PFCTL_STATE_QUEUE)) {
1540 memset(&a, 0, sizeof(a));
1542 if (strlcpy(a.qname, $2, sizeof(a.qname)) >=
1544 yyerror("queue name too long (max "
1545 "%d chars)", PF_QNAME_SIZE-1);
1551 yyerror("cannot specify tbrsize for queue");
1554 if ($4.priority > 255) {
1555 yyerror("priority out of range: max 255");
1558 a.priority = $4.priority;
1559 a.qlimit = $4.qlimit;
1560 a.scheduler = $4.scheduler.qtype;
1561 if (expand_queue(&a, $3, $5, $4.queue_bwspec,
1563 yyerror("errors in queue definition");
1570 bzero(&queue_opts, sizeof queue_opts);
1571 queue_opts.priority = DEFAULT_PRIORITY;
1572 queue_opts.qlimit = DEFAULT_QLIMIT;
1573 queue_opts.scheduler.qtype = ALTQT_NONE;
1574 queue_opts.queue_bwspec.bw_percent = 100;
1577 { $$ = queue_opts; }
1579 bzero(&queue_opts, sizeof queue_opts);
1580 queue_opts.priority = DEFAULT_PRIORITY;
1581 queue_opts.qlimit = DEFAULT_QLIMIT;
1582 queue_opts.scheduler.qtype = ALTQT_NONE;
1583 queue_opts.queue_bwspec.bw_percent = 100;
1588 queue_opts_l : queue_opts_l queue_opt
1592 queue_opt : BANDWIDTH bandwidth {
1593 if (queue_opts.marker & QOM_BWSPEC) {
1594 yyerror("bandwidth cannot be respecified");
1597 queue_opts.marker |= QOM_BWSPEC;
1598 queue_opts.queue_bwspec = $2;
1601 if (queue_opts.marker & QOM_PRIORITY) {
1602 yyerror("priority cannot be respecified");
1605 if ($2 < 0 || $2 > 255) {
1606 yyerror("priority out of range: max 255");
1609 queue_opts.marker |= QOM_PRIORITY;
1610 queue_opts.priority = $2;
1613 if (queue_opts.marker & QOM_QLIMIT) {
1614 yyerror("qlimit cannot be respecified");
1617 if ($2 < 0 || $2 > 65535) {
1618 yyerror("qlimit out of range: max 65535");
1621 queue_opts.marker |= QOM_QLIMIT;
1622 queue_opts.qlimit = $2;
1625 if (queue_opts.marker & QOM_SCHEDULER) {
1626 yyerror("scheduler cannot be respecified");
1629 queue_opts.marker |= QOM_SCHEDULER;
1630 queue_opts.scheduler = $1;
1633 if (queue_opts.marker & QOM_TBRSIZE) {
1634 yyerror("tbrsize cannot be respecified");
1637 if ($2 < 0 || $2 > UINT_MAX) {
1638 yyerror("tbrsize too big: max %u", UINT_MAX);
1641 queue_opts.marker |= QOM_TBRSIZE;
1642 queue_opts.tbrsize = $2;
1646 bandwidth : STRING {
1652 bps = strtod($1, &cp);
1654 if (strlen(cp) > 1) {
1656 if (!strcmp(cu, "Bit") ||
1658 !strcmp(cu, "bit") ||
1663 if (!strcmp(cp, "b"))
1665 else if (!strcmp(cp, "K"))
1667 else if (!strcmp(cp, "M"))
1669 else if (!strcmp(cp, "G"))
1670 bps *= 1000 * 1000 * 1000;
1671 else if (!strcmp(cp, "%")) {
1672 if (bps < 0 || bps > 100) {
1673 yyerror("bandwidth spec "
1678 $$.bw_percent = bps;
1681 yyerror("unknown unit %s", cp);
1687 $$.bw_absolute = (u_int64_t)bps;
1690 if ($1 < 0 || $1 >= LLONG_MAX) {
1691 yyerror("bandwidth number too big");
1695 $$.bw_absolute = $1;
1700 $$.qtype = ALTQT_CBQ;
1701 $$.data.cbq_opts.flags = 0;
1703 | CBQ '(' cbqflags_list ')' {
1704 $$.qtype = ALTQT_CBQ;
1705 $$.data.cbq_opts.flags = $3;
1708 $$.qtype = ALTQT_PRIQ;
1709 $$.data.priq_opts.flags = 0;
1711 | PRIQ '(' priqflags_list ')' {
1712 $$.qtype = ALTQT_PRIQ;
1713 $$.data.priq_opts.flags = $3;
1716 $$.qtype = ALTQT_HFSC;
1717 bzero(&$$.data.hfsc_opts,
1718 sizeof(struct node_hfsc_opts));
1720 | HFSC '(' hfsc_opts ')' {
1721 $$.qtype = ALTQT_HFSC;
1722 $$.data.hfsc_opts = $3;
1725 $$.qtype = ALTQT_FAIRQ;
1726 bzero(&$$.data.fairq_opts,
1727 sizeof(struct node_fairq_opts));
1729 | FAIRQ '(' fairq_opts ')' {
1730 $$.qtype = ALTQT_FAIRQ;
1731 $$.data.fairq_opts = $3;
1734 $$.qtype = ALTQT_CODEL;
1735 bzero(&$$.data.codel_opts,
1736 sizeof(struct codel_opts));
1738 | CODEL '(' codel_opts ')' {
1739 $$.qtype = ALTQT_CODEL;
1740 $$.data.codel_opts = $3;
1744 cbqflags_list : cbqflags_item { $$ |= $1; }
1745 | cbqflags_list comma cbqflags_item { $$ |= $3; }
1748 cbqflags_item : STRING {
1749 if (!strcmp($1, "default"))
1750 $$ = CBQCLF_DEFCLASS;
1751 else if (!strcmp($1, "borrow"))
1753 else if (!strcmp($1, "red"))
1755 else if (!strcmp($1, "ecn"))
1756 $$ = CBQCLF_RED|CBQCLF_ECN;
1757 else if (!strcmp($1, "rio"))
1759 else if (!strcmp($1, "codel"))
1762 yyerror("unknown cbq flag \"%s\"", $1);
1770 priqflags_list : priqflags_item { $$ |= $1; }
1771 | priqflags_list comma priqflags_item { $$ |= $3; }
1774 priqflags_item : STRING {
1775 if (!strcmp($1, "default"))
1776 $$ = PRCF_DEFAULTCLASS;
1777 else if (!strcmp($1, "red"))
1779 else if (!strcmp($1, "ecn"))
1780 $$ = PRCF_RED|PRCF_ECN;
1781 else if (!strcmp($1, "rio"))
1783 else if (!strcmp($1, "codel"))
1786 yyerror("unknown priq flag \"%s\"", $1);
1796 sizeof(struct node_hfsc_opts));
1803 hfscopts_list : hfscopts_item
1804 | hfscopts_list comma hfscopts_item
1807 hfscopts_item : LINKSHARE bandwidth {
1808 if (hfsc_opts.linkshare.used) {
1809 yyerror("linkshare already specified");
1812 hfsc_opts.linkshare.m2 = $2;
1813 hfsc_opts.linkshare.used = 1;
1815 | LINKSHARE '(' bandwidth comma NUMBER comma bandwidth ')'
1817 if ($5 < 0 || $5 > INT_MAX) {
1818 yyerror("timing in curve out of range");
1821 if (hfsc_opts.linkshare.used) {
1822 yyerror("linkshare already specified");
1825 hfsc_opts.linkshare.m1 = $3;
1826 hfsc_opts.linkshare.d = $5;
1827 hfsc_opts.linkshare.m2 = $7;
1828 hfsc_opts.linkshare.used = 1;
1830 | REALTIME bandwidth {
1831 if (hfsc_opts.realtime.used) {
1832 yyerror("realtime already specified");
1835 hfsc_opts.realtime.m2 = $2;
1836 hfsc_opts.realtime.used = 1;
1838 | REALTIME '(' bandwidth comma NUMBER comma bandwidth ')'
1840 if ($5 < 0 || $5 > INT_MAX) {
1841 yyerror("timing in curve out of range");
1844 if (hfsc_opts.realtime.used) {
1845 yyerror("realtime already specified");
1848 hfsc_opts.realtime.m1 = $3;
1849 hfsc_opts.realtime.d = $5;
1850 hfsc_opts.realtime.m2 = $7;
1851 hfsc_opts.realtime.used = 1;
1853 | UPPERLIMIT bandwidth {
1854 if (hfsc_opts.upperlimit.used) {
1855 yyerror("upperlimit already specified");
1858 hfsc_opts.upperlimit.m2 = $2;
1859 hfsc_opts.upperlimit.used = 1;
1861 | UPPERLIMIT '(' bandwidth comma NUMBER comma bandwidth ')'
1863 if ($5 < 0 || $5 > INT_MAX) {
1864 yyerror("timing in curve out of range");
1867 if (hfsc_opts.upperlimit.used) {
1868 yyerror("upperlimit already specified");
1871 hfsc_opts.upperlimit.m1 = $3;
1872 hfsc_opts.upperlimit.d = $5;
1873 hfsc_opts.upperlimit.m2 = $7;
1874 hfsc_opts.upperlimit.used = 1;
1877 if (!strcmp($1, "default"))
1878 hfsc_opts.flags |= HFCF_DEFAULTCLASS;
1879 else if (!strcmp($1, "red"))
1880 hfsc_opts.flags |= HFCF_RED;
1881 else if (!strcmp($1, "ecn"))
1882 hfsc_opts.flags |= HFCF_RED|HFCF_ECN;
1883 else if (!strcmp($1, "rio"))
1884 hfsc_opts.flags |= HFCF_RIO;
1885 else if (!strcmp($1, "codel"))
1886 hfsc_opts.flags |= HFCF_CODEL;
1888 yyerror("unknown hfsc flag \"%s\"", $1);
1898 sizeof(struct node_fairq_opts));
1905 fairqopts_list : fairqopts_item
1906 | fairqopts_list comma fairqopts_item
1909 fairqopts_item : LINKSHARE bandwidth {
1910 if (fairq_opts.linkshare.used) {
1911 yyerror("linkshare already specified");
1914 fairq_opts.linkshare.m2 = $2;
1915 fairq_opts.linkshare.used = 1;
1917 | LINKSHARE '(' bandwidth number bandwidth ')' {
1918 if (fairq_opts.linkshare.used) {
1919 yyerror("linkshare already specified");
1922 fairq_opts.linkshare.m1 = $3;
1923 fairq_opts.linkshare.d = $4;
1924 fairq_opts.linkshare.m2 = $5;
1925 fairq_opts.linkshare.used = 1;
1928 fairq_opts.hogs_bw = $2;
1931 fairq_opts.nbuckets = $2;
1934 if (!strcmp($1, "default"))
1935 fairq_opts.flags |= FARF_DEFAULTCLASS;
1936 else if (!strcmp($1, "red"))
1937 fairq_opts.flags |= FARF_RED;
1938 else if (!strcmp($1, "ecn"))
1939 fairq_opts.flags |= FARF_RED|FARF_ECN;
1940 else if (!strcmp($1, "rio"))
1941 fairq_opts.flags |= FARF_RIO;
1942 else if (!strcmp($1, "codel"))
1943 fairq_opts.flags |= FARF_CODEL;
1945 yyerror("unknown fairq flag \"%s\"", $1);
1955 sizeof(struct codel_opts));
1962 codelopts_list : codelopts_item
1963 | codelopts_list comma codelopts_item
1966 codelopts_item : INTERVAL number {
1967 if (codel_opts.interval) {
1968 yyerror("interval already specified");
1971 codel_opts.interval = $2;
1974 if (codel_opts.target) {
1975 yyerror("target already specified");
1978 codel_opts.target = $2;
1981 if (!strcmp($1, "ecn"))
1984 yyerror("unknown codel option \"%s\"", $1);
1992 qassign : /* empty */ { $$ = NULL; }
1993 | qassign_item { $$ = $1; }
1994 | '{' optnl qassign_list '}' { $$ = $3; }
1997 qassign_list : qassign_item optnl { $$ = $1; }
1998 | qassign_list comma qassign_item optnl {
1999 $1->tail->next = $3;
2005 qassign_item : STRING {
2006 $$ = calloc(1, sizeof(struct node_queue));
2008 err(1, "qassign_item: calloc");
2009 if (strlcpy($$->queue, $1, sizeof($$->queue)) >=
2010 sizeof($$->queue)) {
2011 yyerror("queue name '%s' too long (max "
2012 "%d chars)", $1, sizeof($$->queue)-1);
2023 pfrule : action dir logquick interface route af proto fromto
2026 struct pfctl_rule r;
2027 struct node_state_opt *o;
2028 struct node_proto *proto;
2034 if (check_rulestate(PFCTL_STATE_FILTER))
2037 memset(&r, 0, sizeof(r));
2041 case PFRULE_RETURNRST:
2042 r.rule_flag |= PFRULE_RETURNRST;
2043 r.return_ttl = $1.w;
2045 case PFRULE_RETURNICMP:
2046 r.rule_flag |= PFRULE_RETURNICMP;
2047 r.return_icmp = $1.w;
2048 r.return_icmp6 = $1.w2;
2051 r.rule_flag |= PFRULE_RETURN;
2052 r.return_icmp = $1.w;
2053 r.return_icmp6 = $1.w2;
2061 r.rtableid = $9.rtableid;
2063 if ($9.marker & FOM_PRIO) {
2065 r.prio = PF_PRIO_ZERO;
2069 if ($9.marker & FOM_SETPRIO) {
2070 r.set_prio[0] = $9.set_prio[0];
2071 r.set_prio[1] = $9.set_prio[1];
2072 r.scrub_flags |= PFSTATE_SETPRIO;
2077 if (strlcpy(r.tagname, $9.tag,
2078 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
2079 yyerror("tag too long, max %u chars",
2080 PF_TAG_NAME_SIZE - 1);
2084 if (strlcpy(r.match_tagname, $9.match_tag,
2085 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
2086 yyerror("tag too long, max %u chars",
2087 PF_TAG_NAME_SIZE - 1);
2090 r.match_tag_not = $9.match_tag_not;
2091 if (rule_label(&r, $9.label))
2093 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++)
2095 r.flags = $9.flags.b1;
2096 r.flagset = $9.flags.b2;
2097 if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
2098 yyerror("flags always false");
2101 if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
2102 for (proto = $7; proto != NULL &&
2103 proto->proto != IPPROTO_TCP;
2104 proto = proto->next)
2106 if (proto == NULL && $7 != NULL) {
2107 if ($9.flags.b1 || $9.flags.b2)
2109 "flags only apply to tcp");
2112 "OS fingerprinting only "
2117 if (($9.flags.b1 & parse_flags("S")) == 0 &&
2119 yyerror("OS fingerprinting requires "
2120 "the SYN TCP flag (flags S/SA)");
2127 r.keep_state = $9.keep.action;
2128 o = $9.keep.options;
2130 /* 'keep state' by default on pass rules. */
2131 if (!r.keep_state && !r.action &&
2132 !($9.marker & FOM_KEEP)) {
2133 r.keep_state = PF_STATE_NORMAL;
2134 o = keep_state_defaults;
2139 struct node_state_opt *p = o;
2142 case PF_STATE_OPT_MAX:
2144 yyerror("state option 'max' "
2145 "multiple definitions");
2148 r.max_states = o->data.max_states;
2150 case PF_STATE_OPT_NOSYNC:
2151 if (r.rule_flag & PFRULE_NOSYNC) {
2152 yyerror("state option 'sync' "
2153 "multiple definitions");
2156 r.rule_flag |= PFRULE_NOSYNC;
2158 case PF_STATE_OPT_SRCTRACK:
2160 yyerror("state option "
2162 "multiple definitions");
2165 srctrack = o->data.src_track;
2166 r.rule_flag |= PFRULE_SRCTRACK;
2168 case PF_STATE_OPT_MAX_SRC_STATES:
2169 if (r.max_src_states) {
2170 yyerror("state option "
2172 "multiple definitions");
2175 if (o->data.max_src_states == 0) {
2176 yyerror("'max-src-states' must "
2181 o->data.max_src_states;
2182 r.rule_flag |= PFRULE_SRCTRACK;
2184 case PF_STATE_OPT_OVERLOAD:
2185 if (r.overload_tblname[0]) {
2186 yyerror("multiple 'overload' "
2187 "table definitions");
2190 if (strlcpy(r.overload_tblname,
2191 o->data.overload.tblname,
2192 PF_TABLE_NAME_SIZE) >=
2193 PF_TABLE_NAME_SIZE) {
2194 yyerror("state option: "
2198 r.flush = o->data.overload.flush;
2200 case PF_STATE_OPT_MAX_SRC_CONN:
2201 if (r.max_src_conn) {
2202 yyerror("state option "
2204 "multiple definitions");
2207 if (o->data.max_src_conn == 0) {
2208 yyerror("'max-src-conn' "
2213 o->data.max_src_conn;
2214 r.rule_flag |= PFRULE_SRCTRACK |
2215 PFRULE_RULESRCTRACK;
2217 case PF_STATE_OPT_MAX_SRC_CONN_RATE:
2218 if (r.max_src_conn_rate.limit) {
2219 yyerror("state option "
2220 "'max-src-conn-rate' "
2221 "multiple definitions");
2224 if (!o->data.max_src_conn_rate.limit ||
2225 !o->data.max_src_conn_rate.seconds) {
2226 yyerror("'max-src-conn-rate' "
2227 "values must be > 0");
2230 if (o->data.max_src_conn_rate.limit >
2232 yyerror("'max-src-conn-rate' "
2233 "maximum rate must be < %u",
2237 r.max_src_conn_rate.limit =
2238 o->data.max_src_conn_rate.limit;
2239 r.max_src_conn_rate.seconds =
2240 o->data.max_src_conn_rate.seconds;
2241 r.rule_flag |= PFRULE_SRCTRACK |
2242 PFRULE_RULESRCTRACK;
2244 case PF_STATE_OPT_MAX_SRC_NODES:
2245 if (r.max_src_nodes) {
2246 yyerror("state option "
2248 "multiple definitions");
2251 if (o->data.max_src_nodes == 0) {
2252 yyerror("'max-src-nodes' must "
2257 o->data.max_src_nodes;
2258 r.rule_flag |= PFRULE_SRCTRACK |
2259 PFRULE_RULESRCTRACK;
2261 case PF_STATE_OPT_STATELOCK:
2263 yyerror("state locking option: "
2264 "multiple definitions");
2268 r.rule_flag |= o->data.statelock;
2270 case PF_STATE_OPT_SLOPPY:
2271 if (r.rule_flag & PFRULE_STATESLOPPY) {
2272 yyerror("state sloppy option: "
2273 "multiple definitions");
2276 r.rule_flag |= PFRULE_STATESLOPPY;
2278 case PF_STATE_OPT_TIMEOUT:
2279 if (o->data.timeout.number ==
2280 PFTM_ADAPTIVE_START ||
2281 o->data.timeout.number ==
2284 if (r.timeout[o->data.timeout.number]) {
2285 yyerror("state timeout %s "
2286 "multiple definitions",
2287 pf_timeouts[o->data.
2288 timeout.number].name);
2291 r.timeout[o->data.timeout.number] =
2292 o->data.timeout.seconds;
2299 /* 'flags S/SA' by default on stateful rules */
2300 if (!r.action && !r.flags && !r.flagset &&
2301 !$9.fragment && !($9.marker & FOM_FLAGS) &&
2303 r.flags = parse_flags("S");
2304 r.flagset = parse_flags("SA");
2306 if (!adaptive && r.max_states) {
2307 r.timeout[PFTM_ADAPTIVE_START] =
2308 (r.max_states / 10) * 6;
2309 r.timeout[PFTM_ADAPTIVE_END] =
2310 (r.max_states / 10) * 12;
2312 if (r.rule_flag & PFRULE_SRCTRACK) {
2313 if (srctrack == PF_SRCTRACK_GLOBAL &&
2315 yyerror("'max-src-nodes' is "
2316 "incompatible with "
2317 "'source-track global'");
2320 if (srctrack == PF_SRCTRACK_GLOBAL &&
2322 yyerror("'max-src-conn' is "
2323 "incompatible with "
2324 "'source-track global'");
2327 if (srctrack == PF_SRCTRACK_GLOBAL &&
2328 r.max_src_conn_rate.seconds) {
2329 yyerror("'max-src-conn-rate' is "
2330 "incompatible with "
2331 "'source-track global'");
2334 if (r.timeout[PFTM_SRC_NODE] <
2335 r.max_src_conn_rate.seconds)
2336 r.timeout[PFTM_SRC_NODE] =
2337 r.max_src_conn_rate.seconds;
2338 r.rule_flag |= PFRULE_SRCTRACK;
2339 if (srctrack == PF_SRCTRACK_RULE)
2340 r.rule_flag |= PFRULE_RULESRCTRACK;
2342 if (r.keep_state && !statelock)
2343 r.rule_flag |= default_statelock;
2346 r.rule_flag |= PFRULE_FRAGMENT;
2347 r.allow_opts = $9.allowopts;
2349 decide_address_family($8.src.host, &r.af);
2350 decide_address_family($8.dst.host, &r.af);
2354 yyerror("direction must be explicit "
2355 "with rules that specify routing");
2359 r.rpool.opts = $5.pool_opts;
2361 memcpy(&r.rpool.key, $5.key,
2362 sizeof(struct pf_poolhashkey));
2365 decide_address_family($5.host, &r.af);
2366 remove_invalid_hosts(&$5.host, &r.af);
2367 if ($5.host == NULL) {
2368 yyerror("no routing address with "
2369 "matching address family found.");
2372 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
2373 PF_POOL_NONE && ($5.host->next != NULL ||
2374 $5.host->addr.type == PF_ADDR_TABLE ||
2375 DYNIF_MULTIADDR($5.host->addr)))
2376 r.rpool.opts |= PF_POOL_ROUNDROBIN;
2377 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2378 PF_POOL_ROUNDROBIN &&
2379 disallow_table($5.host, "tables are only "
2380 "supported in round-robin routing pools"))
2382 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2383 PF_POOL_ROUNDROBIN &&
2384 disallow_alias($5.host, "interface (%s) "
2385 "is only supported in round-robin "
2388 if ($5.host->next != NULL) {
2389 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2390 PF_POOL_ROUNDROBIN) {
2391 yyerror("r.rpool.opts must "
2392 "be PF_POOL_ROUNDROBIN");
2397 if ($9.queues.qname != NULL) {
2398 if (strlcpy(r.qname, $9.queues.qname,
2399 sizeof(r.qname)) >= sizeof(r.qname)) {
2400 yyerror("rule qname too long (max "
2401 "%d chars)", sizeof(r.qname)-1);
2404 free($9.queues.qname);
2406 if ($9.queues.pqname != NULL) {
2407 if (strlcpy(r.pqname, $9.queues.pqname,
2408 sizeof(r.pqname)) >= sizeof(r.pqname)) {
2409 yyerror("rule pqname too long (max "
2410 "%d chars)", sizeof(r.pqname)-1);
2413 free($9.queues.pqname);
2416 r.divert.port = $9.divert.port;
2418 if ((r.divert.port = $9.divert.port)) {
2419 if (r.direction == PF_OUT) {
2420 if ($9.divert.addr) {
2421 yyerror("address specified "
2422 "for outgoing divert");
2425 bzero(&r.divert.addr,
2426 sizeof(r.divert.addr));
2428 if (!$9.divert.addr) {
2429 yyerror("no address specified "
2430 "for incoming divert");
2433 if ($9.divert.addr->af != r.af) {
2434 yyerror("address family "
2435 "mismatch for divert");
2439 $9.divert.addr->addr.v.a.addr;
2444 expand_rule(&r, $4, $5.host, $7, $8.src_os,
2445 $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
2446 $9.uid, $9.gid, $9.icmpspec, "");
2451 bzero(&filter_opts, sizeof filter_opts);
2452 filter_opts.rtableid = -1;
2455 { $$ = filter_opts; }
2457 bzero(&filter_opts, sizeof filter_opts);
2458 filter_opts.rtableid = -1;
2463 filter_opts_l : filter_opts_l filter_opt
2467 filter_opt : USER uids {
2468 if (filter_opts.uid)
2469 $2->tail->next = filter_opts.uid;
2470 filter_opts.uid = $2;
2473 if (filter_opts.gid)
2474 $2->tail->next = filter_opts.gid;
2475 filter_opts.gid = $2;
2478 if (filter_opts.marker & FOM_FLAGS) {
2479 yyerror("flags cannot be redefined");
2482 filter_opts.marker |= FOM_FLAGS;
2483 filter_opts.flags.b1 |= $1.b1;
2484 filter_opts.flags.b2 |= $1.b2;
2485 filter_opts.flags.w |= $1.w;
2486 filter_opts.flags.w2 |= $1.w2;
2489 if (filter_opts.marker & FOM_ICMP) {
2490 yyerror("icmp-type cannot be redefined");
2493 filter_opts.marker |= FOM_ICMP;
2494 filter_opts.icmpspec = $1;
2497 if (filter_opts.marker & FOM_PRIO) {
2498 yyerror("prio cannot be redefined");
2501 if ($2 < 0 || $2 > PF_PRIO_MAX) {
2502 yyerror("prio must be 0 - %u", PF_PRIO_MAX);
2505 filter_opts.marker |= FOM_PRIO;
2506 filter_opts.prio = $2;
2509 if (filter_opts.marker & FOM_TOS) {
2510 yyerror("tos cannot be redefined");
2513 filter_opts.marker |= FOM_TOS;
2514 filter_opts.tos = $2;
2517 if (filter_opts.marker & FOM_KEEP) {
2518 yyerror("modulate or keep cannot be redefined");
2521 filter_opts.marker |= FOM_KEEP;
2522 filter_opts.keep.action = $1.action;
2523 filter_opts.keep.options = $1.options;
2526 filter_opts.fragment = 1;
2529 filter_opts.allowopts = 1;
2532 if (filter_opts.labelcount >= PF_RULE_MAX_LABEL_COUNT) {
2533 yyerror("label can only be used %d times", PF_RULE_MAX_LABEL_COUNT);
2536 filter_opts.label[filter_opts.labelcount++] = $1;
2539 if (filter_opts.queues.qname) {
2540 yyerror("queue cannot be redefined");
2543 filter_opts.queues = $1;
2546 filter_opts.tag = $2;
2548 | not TAGGED string {
2549 filter_opts.match_tag = $3;
2550 filter_opts.match_tag_not = $1;
2552 | PROBABILITY probability {
2555 p = floor($2 * UINT_MAX + 0.5);
2556 if (p < 0.0 || p > UINT_MAX) {
2557 yyerror("invalid probability: %lf", p);
2560 filter_opts.prob = (u_int32_t)p;
2561 if (filter_opts.prob == 0)
2562 filter_opts.prob = 1;
2565 if ($2 < 0 || $2 > rt_tableid_max()) {
2566 yyerror("invalid rtable id");
2569 filter_opts.rtableid = $2;
2571 | DIVERTTO portplain {
2573 filter_opts.divert.port = $2.a;
2574 if (!filter_opts.divert.port) {
2575 yyerror("invalid divert port: %u", ntohs($2.a));
2580 | DIVERTTO STRING PORT portplain {
2582 if ((filter_opts.divert.addr = host($2)) == NULL) {
2583 yyerror("could not parse divert address: %s",
2592 filter_opts.divert.port = $4.a;
2593 if (!filter_opts.divert.port) {
2594 yyerror("invalid divert port: %u", ntohs($4.a));
2600 yyerror("divert-reply has no meaning in FreeBSD pf(4)");
2603 filter_opts.divert.port = 1; /* some random value */
2609 filter_sets : SET '(' filter_sets_l ')' { $$ = filter_opts; }
2610 | SET filter_set { $$ = filter_opts; }
2613 filter_sets_l : filter_sets_l comma filter_set
2618 if (filter_opts.marker & FOM_SETPRIO) {
2619 yyerror("prio cannot be redefined");
2622 filter_opts.marker |= FOM_SETPRIO;
2623 filter_opts.set_prio[0] = $1.b1;
2624 filter_opts.set_prio[1] = $1.b2;
2626 prio : PRIO NUMBER {
2627 if ($2 < 0 || $2 > PF_PRIO_MAX) {
2628 yyerror("prio must be 0 - %u", PF_PRIO_MAX);
2633 | PRIO '(' NUMBER comma NUMBER ')' {
2634 if ($3 < 0 || $3 > PF_PRIO_MAX ||
2635 $5 < 0 || $5 > PF_PRIO_MAX) {
2636 yyerror("prio must be 0 - %u", PF_PRIO_MAX);
2644 probability : STRING {
2646 double p = strtod($1, &e);
2653 yyerror("invalid probability: %s", $1);
2669 $$.w = returnicmpdefault;
2670 $$.w2 = returnicmp6default;
2672 | BLOCK blockspec { $$ = $2; $$.b1 = PF_DROP; }
2675 blockspec : /* empty */ {
2676 $$.b2 = blockpolicy;
2677 $$.w = returnicmpdefault;
2678 $$.w2 = returnicmp6default;
2681 $$.b2 = PFRULE_DROP;
2686 $$.b2 = PFRULE_RETURNRST;
2690 | RETURNRST '(' TTL NUMBER ')' {
2691 if ($4 < 0 || $4 > 255) {
2692 yyerror("illegal ttl value %d", $4);
2695 $$.b2 = PFRULE_RETURNRST;
2700 $$.b2 = PFRULE_RETURNICMP;
2701 $$.w = returnicmpdefault;
2702 $$.w2 = returnicmp6default;
2705 $$.b2 = PFRULE_RETURNICMP;
2706 $$.w = returnicmpdefault;
2707 $$.w2 = returnicmp6default;
2709 | RETURNICMP '(' reticmpspec ')' {
2710 $$.b2 = PFRULE_RETURNICMP;
2712 $$.w2 = returnicmpdefault;
2714 | RETURNICMP6 '(' reticmp6spec ')' {
2715 $$.b2 = PFRULE_RETURNICMP;
2716 $$.w = returnicmpdefault;
2719 | RETURNICMP '(' reticmpspec comma reticmp6spec ')' {
2720 $$.b2 = PFRULE_RETURNICMP;
2725 $$.b2 = PFRULE_RETURN;
2726 $$.w = returnicmpdefault;
2727 $$.w2 = returnicmp6default;
2731 reticmpspec : STRING {
2732 if (!($$ = parseicmpspec($1, AF_INET))) {
2741 if ($1 < 0 || $1 > 255) {
2742 yyerror("invalid icmp code %lu", $1);
2745 icmptype = returnicmpdefault >> 8;
2746 $$ = (icmptype << 8 | $1);
2750 reticmp6spec : STRING {
2751 if (!($$ = parseicmpspec($1, AF_INET6))) {
2760 if ($1 < 0 || $1 > 255) {
2761 yyerror("invalid icmp code %lu", $1);
2764 icmptype = returnicmp6default >> 8;
2765 $$ = (icmptype << 8 | $1);
2769 dir : /* empty */ { $$ = PF_INOUT; }
2770 | IN { $$ = PF_IN; }
2771 | OUT { $$ = PF_OUT; }
2774 quick : /* empty */ { $$.quick = 0; }
2775 | QUICK { $$.quick = 1; }
2778 logquick : /* empty */ { $$.log = 0; $$.quick = 0; $$.logif = 0; }
2779 | log { $$ = $1; $$.quick = 0; }
2780 | QUICK { $$.quick = 1; $$.log = 0; $$.logif = 0; }
2781 | log QUICK { $$ = $1; $$.quick = 1; }
2782 | QUICK log { $$ = $2; $$.quick = 1; }
2785 log : LOG { $$.log = PF_LOG; $$.logif = 0; }
2786 | LOG '(' logopts ')' {
2787 $$.log = PF_LOG | $3.log;
2788 $$.logif = $3.logif;
2792 logopts : logopt { $$ = $1; }
2793 | logopts comma logopt {
2794 $$.log = $1.log | $3.log;
2795 $$.logif = $3.logif;
2797 $$.logif = $1.logif;
2801 logopt : ALL { $$.log = PF_LOG_ALL; $$.logif = 0; }
2802 | USER { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
2803 | GROUP { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
2809 if (strncmp($2, "pflog", 5)) {
2810 yyerror("%s: should be a pflog interface", $2);
2814 i = strtonum($2 + 5, 0, 255, &errstr);
2816 yyerror("%s: %s", $2, errstr);
2825 interface : /* empty */ { $$ = NULL; }
2826 | ON if_item_not { $$ = $2; }
2827 | ON '{' optnl if_list '}' { $$ = $4; }
2830 if_list : if_item_not optnl { $$ = $1; }
2831 | if_list comma if_item_not optnl {
2832 $1->tail->next = $3;
2838 if_item_not : not if_item { $$ = $2; $$->not = $1; }
2842 struct node_host *n;
2844 $$ = calloc(1, sizeof(struct node_if));
2846 err(1, "if_item: calloc");
2847 if (strlcpy($$->ifname, $1, sizeof($$->ifname)) >=
2848 sizeof($$->ifname)) {
2851 yyerror("interface name too long");
2855 if ((n = ifa_exists($1)) != NULL)
2856 $$->ifa_flags = n->ifa_flags;
2865 af : /* empty */ { $$ = 0; }
2866 | INET { $$ = AF_INET; }
2867 | INET6 { $$ = AF_INET6; }
2870 proto : /* empty */ { $$ = NULL; }
2871 | PROTO proto_item { $$ = $2; }
2872 | PROTO '{' optnl proto_list '}' { $$ = $4; }
2875 proto_list : proto_item optnl { $$ = $1; }
2876 | proto_list comma proto_item optnl {
2877 $1->tail->next = $3;
2883 proto_item : protoval {
2888 yyerror("proto 0 cannot be used");
2891 $$ = calloc(1, sizeof(struct node_proto));
2893 err(1, "proto_item: calloc");
2903 p = getprotobyname($1);
2905 yyerror("unknown protocol %s", $1);
2913 if ($1 < 0 || $1 > 255) {
2914 yyerror("protocol outside range");
2934 os : /* empty */ { $$ = NULL; }
2935 | OS xos { $$ = $2; }
2936 | OS '{' optnl os_list '}' { $$ = $4; }
2940 $$ = calloc(1, sizeof(struct node_os));
2942 err(1, "os: calloc");
2948 os_list : xos optnl { $$ = $1; }
2949 | os_list comma xos optnl {
2950 $1->tail->next = $3;
2956 from : /* empty */ {
2970 if (disallow_urpf_failed($2.host, "\"urpf-failed\" is "
2971 "not permitted in a destination address"))
2977 ipportspec : ipspec {
2981 | ipspec PORT portspec {
2995 ipspec : ANY { $$ = NULL; }
2996 | xhost { $$ = $1; }
2997 | '{' optnl host_list '}' { $$ = $3; }
3000 toipspec : TO ipspec { $$ = $2; }
3001 | /* empty */ { $$ = NULL; }
3004 host_list : ipspec optnl { $$ = $1; }
3005 | host_list comma ipspec optnl {
3008 else if ($1 == NULL)
3011 $1->tail->next = $3;
3012 $1->tail = $3->tail;
3019 struct node_host *n;
3021 for (n = $2; n != NULL; n = n->next)
3026 $$ = calloc(1, sizeof(struct node_host));
3028 err(1, "xhost: calloc");
3029 $$->addr.type = PF_ADDR_NOROUTE;
3035 $$ = calloc(1, sizeof(struct node_host));
3037 err(1, "xhost: calloc");
3038 $$->addr.type = PF_ADDR_URPFFAILED;
3046 if (($$ = host($1)) == NULL) {
3047 /* error. "any" is handled elsewhere */
3049 yyerror("could not parse host specification");
3055 | STRING '-' STRING {
3056 struct node_host *b, *e;
3058 if ((b = host($1)) == NULL || (e = host($3)) == NULL) {
3061 yyerror("could not parse host specification");
3064 if (b->af != e->af ||
3065 b->addr.type != PF_ADDR_ADDRMASK ||
3066 e->addr.type != PF_ADDR_ADDRMASK ||
3067 unmask(&b->addr.v.a.mask, b->af) !=
3068 (b->af == AF_INET ? 32 : 128) ||
3069 unmask(&e->addr.v.a.mask, e->af) !=
3070 (e->af == AF_INET ? 32 : 128) ||
3071 b->next != NULL || b->not ||
3072 e->next != NULL || e->not) {
3077 yyerror("invalid address range");
3080 memcpy(&b->addr.v.a.mask, &e->addr.v.a.addr,
3081 sizeof(b->addr.v.a.mask));
3082 b->addr.type = PF_ADDR_RANGE;
3088 | STRING '/' NUMBER {
3091 if (asprintf(&buf, "%s/%lld", $1, (long long)$3) == -1)
3092 err(1, "host: asprintf");
3094 if (($$ = host(buf)) == NULL) {
3095 /* error. "any" is handled elsewhere */
3097 yyerror("could not parse host specification");
3102 | NUMBER '/' NUMBER {
3105 /* ie. for 10/8 parsing */
3107 if (asprintf(&buf, "%lld/%lld", (long long)$1, (long long)$3) == -1)
3109 if (asprintf(&buf, "%lld/%lld", $1, $3) == -1)
3111 err(1, "host: asprintf");
3112 if (($$ = host(buf)) == NULL) {
3113 /* error. "any" is handled elsewhere */
3115 yyerror("could not parse host specification");
3121 | dynaddr '/' NUMBER {
3122 struct node_host *n;
3124 if ($3 < 0 || $3 > 128) {
3125 yyerror("bit number too big");
3129 for (n = $1; n != NULL; n = n->next)
3133 if (strlen($2) >= PF_TABLE_NAME_SIZE) {
3134 yyerror("table name '%s' too long", $2);
3138 $$ = calloc(1, sizeof(struct node_host));
3140 err(1, "host: calloc");
3141 $$->addr.type = PF_ADDR_TABLE;
3142 if (strlcpy($$->addr.v.tblname, $2,
3143 sizeof($$->addr.v.tblname)) >=
3144 sizeof($$->addr.v.tblname))
3145 errx(1, "host: strlcpy");
3156 if (atoul($1, &ulval) == -1) {
3157 yyerror("%s is not a number", $1);
3166 dynaddr : '(' STRING ')' {
3171 if (!isalpha(op[0])) {
3172 yyerror("invalid interface name '%s'", op);
3176 while ((p = strrchr($2, ':')) != NULL) {
3177 if (!strcmp(p+1, "network"))
3178 flags |= PFI_AFLAG_NETWORK;
3179 else if (!strcmp(p+1, "broadcast"))
3180 flags |= PFI_AFLAG_BROADCAST;
3181 else if (!strcmp(p+1, "peer"))
3182 flags |= PFI_AFLAG_PEER;
3183 else if (!strcmp(p+1, "0"))
3184 flags |= PFI_AFLAG_NOALIAS;
3186 yyerror("interface %s has bad modifier",
3193 if (flags & (flags - 1) & PFI_AFLAG_MODEMASK) {
3195 yyerror("illegal combination of "
3196 "interface modifiers");
3199 $$ = calloc(1, sizeof(struct node_host));
3201 err(1, "address: calloc");
3203 set_ipmask($$, 128);
3204 $$->addr.type = PF_ADDR_DYNIFTL;
3205 $$->addr.iflags = flags;
3206 if (strlcpy($$->addr.v.ifname, $2,
3207 sizeof($$->addr.v.ifname)) >=
3208 sizeof($$->addr.v.ifname)) {
3211 yyerror("interface name too long");
3220 portspec : port_item { $$ = $1; }
3221 | '{' optnl port_list '}' { $$ = $3; }
3224 port_list : port_item optnl { $$ = $1; }
3225 | port_list comma port_item optnl {
3226 $1->tail->next = $3;
3232 port_item : portrange {
3233 $$ = calloc(1, sizeof(struct node_port));
3235 err(1, "port_item: calloc");
3245 | unaryop portrange {
3247 yyerror("':' cannot be used with an other "
3251 $$ = calloc(1, sizeof(struct node_port));
3253 err(1, "port_item: calloc");
3260 | portrange PORTBINARY portrange {
3262 yyerror("':' cannot be used with an other "
3266 $$ = calloc(1, sizeof(struct node_port));
3268 err(1, "port_item: calloc");
3277 portplain : numberstring {
3278 if (parseport($1, &$$, 0) == -1) {
3286 portrange : numberstring {
3287 if (parseport($1, &$$, PPORT_RANGE) == -1) {
3295 uids : uid_item { $$ = $1; }
3296 | '{' optnl uid_list '}' { $$ = $3; }
3299 uid_list : uid_item optnl { $$ = $1; }
3300 | uid_list comma uid_item optnl {
3301 $1->tail->next = $3;
3308 $$ = calloc(1, sizeof(struct node_uid));
3310 err(1, "uid_item: calloc");
3318 if ($2 == UID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
3319 yyerror("user unknown requires operator = or "
3323 $$ = calloc(1, sizeof(struct node_uid));
3325 err(1, "uid_item: calloc");
3332 | uid PORTBINARY uid {
3333 if ($1 == UID_MAX || $3 == UID_MAX) {
3334 yyerror("user unknown requires operator = or "
3338 $$ = calloc(1, sizeof(struct node_uid));
3340 err(1, "uid_item: calloc");
3350 if (!strcmp($1, "unknown"))
3355 if ((pw = getpwnam($1)) == NULL) {
3356 yyerror("unknown user %s", $1);
3365 if ($1 < 0 || $1 >= UID_MAX) {
3366 yyerror("illegal uid value %lu", $1);
3373 gids : gid_item { $$ = $1; }
3374 | '{' optnl gid_list '}' { $$ = $3; }
3377 gid_list : gid_item optnl { $$ = $1; }
3378 | gid_list comma gid_item optnl {
3379 $1->tail->next = $3;
3386 $$ = calloc(1, sizeof(struct node_gid));
3388 err(1, "gid_item: calloc");
3396 if ($2 == GID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
3397 yyerror("group unknown requires operator = or "
3401 $$ = calloc(1, sizeof(struct node_gid));
3403 err(1, "gid_item: calloc");
3410 | gid PORTBINARY gid {
3411 if ($1 == GID_MAX || $3 == GID_MAX) {
3412 yyerror("group unknown requires operator = or "
3416 $$ = calloc(1, sizeof(struct node_gid));
3418 err(1, "gid_item: calloc");
3428 if (!strcmp($1, "unknown"))
3433 if ((grp = getgrnam($1)) == NULL) {
3434 yyerror("unknown group %s", $1);
3443 if ($1 < 0 || $1 >= GID_MAX) {
3444 yyerror("illegal gid value %lu", $1);
3454 if ((f = parse_flags($1)) < 0) {
3455 yyerror("bad flags %s", $1);
3464 flags : FLAGS flag '/' flag { $$.b1 = $2.b1; $$.b2 = $4.b1; }
3465 | FLAGS '/' flag { $$.b1 = 0; $$.b2 = $3.b1; }
3466 | FLAGS ANY { $$.b1 = 0; $$.b2 = 0; }
3469 icmpspec : ICMPTYPE icmp_item { $$ = $2; }
3470 | ICMPTYPE '{' optnl icmp_list '}' { $$ = $4; }
3471 | ICMP6TYPE icmp6_item { $$ = $2; }
3472 | ICMP6TYPE '{' optnl icmp6_list '}' { $$ = $4; }
3475 icmp_list : icmp_item optnl { $$ = $1; }
3476 | icmp_list comma icmp_item optnl {
3477 $1->tail->next = $3;
3483 icmp6_list : icmp6_item optnl { $$ = $1; }
3484 | icmp6_list comma icmp6_item optnl {
3485 $1->tail->next = $3;
3491 icmp_item : icmptype {
3492 $$ = calloc(1, sizeof(struct node_icmp));
3494 err(1, "icmp_item: calloc");
3497 $$->proto = IPPROTO_ICMP;
3501 | icmptype CODE STRING {
3502 const struct icmpcodeent *p;
3504 if ((p = geticmpcodebyname($1-1, $3, AF_INET)) == NULL) {
3505 yyerror("unknown icmp-code %s", $3);
3511 $$ = calloc(1, sizeof(struct node_icmp));
3513 err(1, "icmp_item: calloc");
3515 $$->code = p->code + 1;
3516 $$->proto = IPPROTO_ICMP;
3520 | icmptype CODE NUMBER {
3521 if ($3 < 0 || $3 > 255) {
3522 yyerror("illegal icmp-code %lu", $3);
3525 $$ = calloc(1, sizeof(struct node_icmp));
3527 err(1, "icmp_item: calloc");
3530 $$->proto = IPPROTO_ICMP;
3536 icmp6_item : icmp6type {
3537 $$ = calloc(1, sizeof(struct node_icmp));
3539 err(1, "icmp_item: calloc");
3542 $$->proto = IPPROTO_ICMPV6;
3546 | icmp6type CODE STRING {
3547 const struct icmpcodeent *p;
3549 if ((p = geticmpcodebyname($1-1, $3, AF_INET6)) == NULL) {
3550 yyerror("unknown icmp6-code %s", $3);
3556 $$ = calloc(1, sizeof(struct node_icmp));
3558 err(1, "icmp_item: calloc");
3560 $$->code = p->code + 1;
3561 $$->proto = IPPROTO_ICMPV6;
3565 | icmp6type CODE NUMBER {
3566 if ($3 < 0 || $3 > 255) {
3567 yyerror("illegal icmp-code %lu", $3);
3570 $$ = calloc(1, sizeof(struct node_icmp));
3572 err(1, "icmp_item: calloc");
3575 $$->proto = IPPROTO_ICMPV6;
3582 const struct icmptypeent *p;
3584 if ((p = geticmptypebyname($1, AF_INET)) == NULL) {
3585 yyerror("unknown icmp-type %s", $1);
3593 if ($1 < 0 || $1 > 255) {
3594 yyerror("illegal icmp-type %lu", $1);
3601 icmp6type : STRING {
3602 const struct icmptypeent *p;
3604 if ((p = geticmptypebyname($1, AF_INET6)) ==
3606 yyerror("unknown icmp6-type %s", $1);
3614 if ($1 < 0 || $1 > 255) {
3615 yyerror("illegal icmp6-type %lu", $1);
3626 if (map_tos($1, &val))
3628 else if ($1[0] == '0' && $1[1] == 'x') {
3630 $$ = strtoul($1, &end, 16);
3631 if (errno || *end != '\0')
3634 $$ = 256; /* flag bad argument */
3635 if ($$ < 0 || $$ > 255) {
3636 yyerror("illegal tos value %s", $1);
3644 if ($$ < 0 || $$ > 255) {
3645 yyerror("illegal tos value %s", $1);
3651 sourcetrack : SOURCETRACK { $$ = PF_SRCTRACK; }
3652 | SOURCETRACK GLOBAL { $$ = PF_SRCTRACK_GLOBAL; }
3653 | SOURCETRACK RULE { $$ = PF_SRCTRACK_RULE; }
3656 statelock : IFBOUND {
3657 $$ = PFRULE_IFBOUND;
3668 | KEEP STATE state_opt_spec {
3669 $$.action = PF_STATE_NORMAL;
3672 | MODULATE STATE state_opt_spec {
3673 $$.action = PF_STATE_MODULATE;
3676 | SYNPROXY STATE state_opt_spec {
3677 $$.action = PF_STATE_SYNPROXY;
3682 flush : /* empty */ { $$ = 0; }
3683 | FLUSH { $$ = PF_FLUSH; }
3685 $$ = PF_FLUSH | PF_FLUSH_GLOBAL;
3689 state_opt_spec : '(' state_opt_list ')' { $$ = $2; }
3690 | /* empty */ { $$ = NULL; }
3693 state_opt_list : state_opt_item { $$ = $1; }
3694 | state_opt_list comma state_opt_item {
3695 $1->tail->next = $3;
3701 state_opt_item : MAXIMUM NUMBER {
3702 if ($2 < 0 || $2 > UINT_MAX) {
3703 yyerror("only positive values permitted");
3706 $$ = calloc(1, sizeof(struct node_state_opt));
3708 err(1, "state_opt_item: calloc");
3709 $$->type = PF_STATE_OPT_MAX;
3710 $$->data.max_states = $2;
3715 $$ = calloc(1, sizeof(struct node_state_opt));
3717 err(1, "state_opt_item: calloc");
3718 $$->type = PF_STATE_OPT_NOSYNC;
3722 | MAXSRCSTATES NUMBER {
3723 if ($2 < 0 || $2 > UINT_MAX) {
3724 yyerror("only positive values permitted");
3727 $$ = calloc(1, sizeof(struct node_state_opt));
3729 err(1, "state_opt_item: calloc");
3730 $$->type = PF_STATE_OPT_MAX_SRC_STATES;
3731 $$->data.max_src_states = $2;
3735 | MAXSRCCONN NUMBER {
3736 if ($2 < 0 || $2 > UINT_MAX) {
3737 yyerror("only positive values permitted");
3740 $$ = calloc(1, sizeof(struct node_state_opt));
3742 err(1, "state_opt_item: calloc");
3743 $$->type = PF_STATE_OPT_MAX_SRC_CONN;
3744 $$->data.max_src_conn = $2;
3748 | MAXSRCCONNRATE NUMBER '/' NUMBER {
3749 if ($2 < 0 || $2 > UINT_MAX ||
3750 $4 < 0 || $4 > UINT_MAX) {
3751 yyerror("only positive values permitted");
3754 $$ = calloc(1, sizeof(struct node_state_opt));
3756 err(1, "state_opt_item: calloc");
3757 $$->type = PF_STATE_OPT_MAX_SRC_CONN_RATE;
3758 $$->data.max_src_conn_rate.limit = $2;
3759 $$->data.max_src_conn_rate.seconds = $4;
3763 | OVERLOAD '<' STRING '>' flush {
3764 if (strlen($3) >= PF_TABLE_NAME_SIZE) {
3765 yyerror("table name '%s' too long", $3);
3769 $$ = calloc(1, sizeof(struct node_state_opt));
3771 err(1, "state_opt_item: calloc");
3772 if (strlcpy($$->data.overload.tblname, $3,
3773 PF_TABLE_NAME_SIZE) >= PF_TABLE_NAME_SIZE)
3774 errx(1, "state_opt_item: strlcpy");
3776 $$->type = PF_STATE_OPT_OVERLOAD;
3777 $$->data.overload.flush = $5;
3781 | MAXSRCNODES NUMBER {
3782 if ($2 < 0 || $2 > UINT_MAX) {
3783 yyerror("only positive values permitted");
3786 $$ = calloc(1, sizeof(struct node_state_opt));
3788 err(1, "state_opt_item: calloc");
3789 $$->type = PF_STATE_OPT_MAX_SRC_NODES;
3790 $$->data.max_src_nodes = $2;
3795 $$ = calloc(1, sizeof(struct node_state_opt));
3797 err(1, "state_opt_item: calloc");
3798 $$->type = PF_STATE_OPT_SRCTRACK;
3799 $$->data.src_track = $1;
3804 $$ = calloc(1, sizeof(struct node_state_opt));
3806 err(1, "state_opt_item: calloc");
3807 $$->type = PF_STATE_OPT_STATELOCK;
3808 $$->data.statelock = $1;
3813 $$ = calloc(1, sizeof(struct node_state_opt));
3815 err(1, "state_opt_item: calloc");
3816 $$->type = PF_STATE_OPT_SLOPPY;
3823 if ($2 < 0 || $2 > UINT_MAX) {
3824 yyerror("only positive values permitted");
3827 for (i = 0; pf_timeouts[i].name &&
3828 strcmp(pf_timeouts[i].name, $1); ++i)
3830 if (!pf_timeouts[i].name) {
3831 yyerror("illegal timeout name %s", $1);
3835 if (strchr(pf_timeouts[i].name, '.') == NULL) {
3836 yyerror("illegal state timeout %s", $1);
3841 $$ = calloc(1, sizeof(struct node_state_opt));
3843 err(1, "state_opt_item: calloc");
3844 $$->type = PF_STATE_OPT_TIMEOUT;
3845 $$->data.timeout.number = pf_timeouts[i].timeout;
3846 $$->data.timeout.seconds = $2;
3852 label : LABEL STRING {
3857 qname : QUEUE STRING {
3861 | QUEUE '(' STRING ')' {
3865 | QUEUE '(' STRING comma STRING ')' {
3871 no : /* empty */ { $$ = 0; }
3875 portstar : numberstring {
3876 if (parseport($1, &$$, PPORT_RANGE|PPORT_STAR) == -1) {
3884 redirspec : host { $$ = $1; }
3885 | '{' optnl redir_host_list '}' { $$ = $3; }
3888 redir_host_list : host optnl { $$ = $1; }
3889 | redir_host_list comma host optnl {
3890 $1->tail->next = $3;
3891 $1->tail = $3->tail;
3896 redirpool : /* empty */ { $$ = NULL; }
3898 $$ = calloc(1, sizeof(struct redirection));
3900 err(1, "redirection: calloc");
3902 $$->rport.a = $$->rport.b = $$->rport.t = 0;
3904 | ARROW redirspec PORT portstar {
3905 $$ = calloc(1, sizeof(struct redirection));
3907 err(1, "redirection: calloc");
3913 hashkey : /* empty */
3915 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3917 err(1, "hashkey: calloc");
3918 $$->key32[0] = arc4random();
3919 $$->key32[1] = arc4random();
3920 $$->key32[2] = arc4random();
3921 $$->key32[3] = arc4random();
3925 if (!strncmp($1, "0x", 2)) {
3926 if (strlen($1) != 34) {
3928 yyerror("hex key must be 128 bits "
3929 "(32 hex digits) long");
3932 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3934 err(1, "hashkey: calloc");
3936 if (sscanf($1, "0x%8x%8x%8x%8x",
3937 &$$->key32[0], &$$->key32[1],
3938 &$$->key32[2], &$$->key32[3]) != 4) {
3941 yyerror("invalid hex key");
3947 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3949 err(1, "hashkey: calloc");
3951 MD5Update(&context, (unsigned char *)$1,
3953 MD5Final((unsigned char *)$$, &context);
3954 HTONL($$->key32[0]);
3955 HTONL($$->key32[1]);
3956 HTONL($$->key32[2]);
3957 HTONL($$->key32[3]);
3963 pool_opts : { bzero(&pool_opts, sizeof pool_opts); }
3967 bzero(&pool_opts, sizeof pool_opts);
3972 pool_opts_l : pool_opts_l pool_opt
3976 pool_opt : BITMASK {
3977 if (pool_opts.type) {
3978 yyerror("pool type cannot be redefined");
3981 pool_opts.type = PF_POOL_BITMASK;
3984 if (pool_opts.type) {
3985 yyerror("pool type cannot be redefined");
3988 pool_opts.type = PF_POOL_RANDOM;
3990 | SOURCEHASH hashkey {
3991 if (pool_opts.type) {
3992 yyerror("pool type cannot be redefined");
3995 pool_opts.type = PF_POOL_SRCHASH;
3999 if (pool_opts.type) {
4000 yyerror("pool type cannot be redefined");
4003 pool_opts.type = PF_POOL_ROUNDROBIN;
4006 if (pool_opts.staticport) {
4007 yyerror("static-port cannot be redefined");
4010 pool_opts.staticport = 1;
4013 if (filter_opts.marker & POM_STICKYADDRESS) {
4014 yyerror("sticky-address cannot be redefined");
4017 pool_opts.marker |= POM_STICKYADDRESS;
4018 pool_opts.opts |= PF_POOL_STICKYADDR;
4020 | MAPEPORTSET number '/' number '/' number {
4021 if (pool_opts.mape.offset) {
4022 yyerror("map-e-portset cannot be redefined");
4025 if (pool_opts.type) {
4026 yyerror("map-e-portset cannot be used with "
4030 if ($2 <= 0 || $2 >= 16) {
4031 yyerror("MAP-E PSID offset must be 1-15");
4034 if ($4 < 0 || $4 >= 16 || $2 + $4 > 16) {
4035 yyerror("Invalid MAP-E PSID length");
4037 } else if ($4 == 0) {
4038 yyerror("PSID Length = 0: this means"
4039 " you do not need MAP-E");
4042 if ($6 < 0 || $6 > 65535) {
4043 yyerror("Invalid MAP-E PSID");
4046 pool_opts.mape.offset = $2;
4047 pool_opts.mape.psidlen = $4;
4048 pool_opts.mape.psid = $6;
4052 redirection : /* empty */ { $$ = NULL; }
4054 $$ = calloc(1, sizeof(struct redirection));
4056 err(1, "redirection: calloc");
4058 $$->rport.a = $$->rport.b = $$->rport.t = 0;
4060 | ARROW host PORT portstar {
4061 $$ = calloc(1, sizeof(struct redirection));
4063 err(1, "redirection: calloc");
4069 natpasslog : /* empty */ { $$.b1 = $$.b2 = 0; $$.w2 = 0; }
4070 | PASS { $$.b1 = 1; $$.b2 = 0; $$.w2 = 0; }
4071 | PASS log { $$.b1 = 1; $$.b2 = $2.log; $$.w2 = $2.logif; }
4072 | log { $$.b1 = 0; $$.b2 = $1.log; $$.w2 = $1.logif; }
4075 nataction : no NAT natpasslog {
4077 yyerror("\"pass\" not valid with \"no\"");
4088 | no RDR natpasslog {
4090 yyerror("\"pass\" not valid with \"no\"");
4103 natrule : nataction interface af proto fromto tag tagged rtable
4106 struct pfctl_rule r;
4108 if (check_rulestate(PFCTL_STATE_NAT))
4111 memset(&r, 0, sizeof(r));
4120 if ($5.src.host && $5.src.host->af &&
4121 !$5.src.host->ifindex)
4122 r.af = $5.src.host->af;
4123 else if ($5.dst.host && $5.dst.host->af &&
4124 !$5.dst.host->ifindex)
4125 r.af = $5.dst.host->af;
4129 if (strlcpy(r.tagname, $6, PF_TAG_NAME_SIZE) >=
4131 yyerror("tag too long, max %u chars",
4132 PF_TAG_NAME_SIZE - 1);
4137 if (strlcpy(r.match_tagname, $7.name,
4138 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4139 yyerror("tag too long, max %u chars",
4140 PF_TAG_NAME_SIZE - 1);
4143 r.match_tag_not = $7.neg;
4146 if (r.action == PF_NONAT || r.action == PF_NORDR) {
4148 yyerror("translation rule with 'no' "
4149 "does not need '->'");
4153 if ($9 == NULL || $9->host == NULL) {
4154 yyerror("translation rule requires '-> "
4158 if (!r.af && ! $9->host->ifindex)
4159 r.af = $9->host->af;
4161 remove_invalid_hosts(&$9->host, &r.af);
4162 if (invalid_redirect($9->host, r.af))
4164 if (check_netmask($9->host, r.af))
4167 r.rpool.proxy_port[0] = ntohs($9->rport.a);
4171 if (!$9->rport.b && $9->rport.t &&
4172 $5.dst.port != NULL) {
4173 r.rpool.proxy_port[1] =
4174 ntohs($9->rport.a) +
4176 $5.dst.port->port[1]) -
4178 $5.dst.port->port[0]));
4180 r.rpool.proxy_port[1] =
4184 r.rpool.proxy_port[1] =
4186 if (!r.rpool.proxy_port[0] &&
4187 !r.rpool.proxy_port[1]) {
4188 r.rpool.proxy_port[0] =
4189 PF_NAT_PROXY_PORT_LOW;
4190 r.rpool.proxy_port[1] =
4191 PF_NAT_PROXY_PORT_HIGH;
4192 } else if (!r.rpool.proxy_port[1])
4193 r.rpool.proxy_port[1] =
4194 r.rpool.proxy_port[0];
4200 r.rpool.opts = $10.type;
4201 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
4202 PF_POOL_NONE && ($9->host->next != NULL ||
4203 $9->host->addr.type == PF_ADDR_TABLE ||
4204 DYNIF_MULTIADDR($9->host->addr)))
4205 r.rpool.opts = PF_POOL_ROUNDROBIN;
4206 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4207 PF_POOL_ROUNDROBIN &&
4208 disallow_table($9->host, "tables are only "
4209 "supported in round-robin redirection "
4212 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4213 PF_POOL_ROUNDROBIN &&
4214 disallow_alias($9->host, "interface (%s) "
4215 "is only supported in round-robin "
4216 "redirection pools"))
4218 if ($9->host->next != NULL) {
4219 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4220 PF_POOL_ROUNDROBIN) {
4221 yyerror("only round-robin "
4222 "valid for multiple "
4223 "redirection addresses");
4229 if ($10.key != NULL)
4230 memcpy(&r.rpool.key, $10.key,
4231 sizeof(struct pf_poolhashkey));
4234 r.rpool.opts |= $10.opts;
4236 if ($10.staticport) {
4237 if (r.action != PF_NAT) {
4238 yyerror("the 'static-port' option is "
4239 "only valid with nat rules");
4242 if (r.rpool.proxy_port[0] !=
4243 PF_NAT_PROXY_PORT_LOW &&
4244 r.rpool.proxy_port[1] !=
4245 PF_NAT_PROXY_PORT_HIGH) {
4246 yyerror("the 'static-port' option can't"
4247 " be used when specifying a port"
4251 r.rpool.proxy_port[0] = 0;
4252 r.rpool.proxy_port[1] = 0;
4255 if ($10.mape.offset) {
4256 if (r.action != PF_NAT) {
4257 yyerror("the 'map-e-portset' option is"
4258 " only valid with nat rules");
4261 if ($10.staticport) {
4262 yyerror("the 'map-e-portset' option"
4263 " can't be used 'static-port'");
4266 if (r.rpool.proxy_port[0] !=
4267 PF_NAT_PROXY_PORT_LOW &&
4268 r.rpool.proxy_port[1] !=
4269 PF_NAT_PROXY_PORT_HIGH) {
4270 yyerror("the 'map-e-portset' option"
4271 " can't be used when specifying"
4275 r.rpool.mape = $10.mape;
4278 expand_rule(&r, $2, $9 == NULL ? NULL : $9->host, $4,
4279 $5.src_os, $5.src.host, $5.src.port, $5.dst.host,
4280 $5.dst.port, 0, 0, 0, "");
4285 binatrule : no BINAT natpasslog interface af proto FROM ipspec toipspec tag
4286 tagged rtable redirection
4288 struct pfctl_rule binat;
4289 struct pf_pooladdr *pa;
4291 if (check_rulestate(PFCTL_STATE_NAT))
4293 if (disallow_urpf_failed($9, "\"urpf-failed\" is not "
4294 "permitted as a binat destination"))
4297 memset(&binat, 0, sizeof(binat));
4300 yyerror("\"pass\" not valid with \"no\"");
4304 binat.action = PF_NOBINAT;
4306 binat.action = PF_BINAT;
4307 binat.natpass = $3.b1;
4309 binat.logif = $3.w2;
4311 if (!binat.af && $8 != NULL && $8->af)
4313 if (!binat.af && $9 != NULL && $9->af)
4316 if (!binat.af && $13 != NULL && $13->host)
4317 binat.af = $13->host->af;
4319 yyerror("address family (inet/inet6) "
4325 memcpy(binat.ifname, $4->ifname,
4326 sizeof(binat.ifname));
4327 binat.ifnot = $4->not;
4332 if (strlcpy(binat.tagname, $10,
4333 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4334 yyerror("tag too long, max %u chars",
4335 PF_TAG_NAME_SIZE - 1);
4339 if (strlcpy(binat.match_tagname, $11.name,
4340 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4341 yyerror("tag too long, max %u chars",
4342 PF_TAG_NAME_SIZE - 1);
4345 binat.match_tag_not = $11.neg;
4346 binat.rtableid = $12;
4349 binat.proto = $6->proto;
4353 if ($8 != NULL && disallow_table($8, "invalid use of "
4354 "table <%s> as the source address of a binat rule"))
4356 if ($8 != NULL && disallow_alias($8, "invalid use of "
4357 "interface (%s) as the source address of a binat "
4360 if ($13 != NULL && $13->host != NULL && disallow_table(
4361 $13->host, "invalid use of table <%s> as the "
4362 "redirect address of a binat rule"))
4364 if ($13 != NULL && $13->host != NULL && disallow_alias(
4365 $13->host, "invalid use of interface (%s) as the "
4366 "redirect address of a binat rule"))
4371 yyerror("multiple binat ip addresses");
4374 if ($8->addr.type == PF_ADDR_DYNIFTL)
4376 if ($8->af != binat.af) {
4377 yyerror("binat ip versions must match");
4380 if (check_netmask($8, binat.af))
4382 memcpy(&binat.src.addr, &$8->addr,
4383 sizeof(binat.src.addr));
4388 yyerror("multiple binat ip addresses");
4391 if ($9->af != binat.af && $9->af) {
4392 yyerror("binat ip versions must match");
4395 if (check_netmask($9, binat.af))
4397 memcpy(&binat.dst.addr, &$9->addr,
4398 sizeof(binat.dst.addr));
4399 binat.dst.neg = $9->not;
4403 if (binat.action == PF_NOBINAT) {
4405 yyerror("'no binat' rule does not need"
4410 if ($13 == NULL || $13->host == NULL) {
4411 yyerror("'binat' rule requires"
4416 remove_invalid_hosts(&$13->host, &binat.af);
4417 if (invalid_redirect($13->host, binat.af))
4419 if ($13->host->next != NULL) {
4420 yyerror("binat rule must redirect to "
4421 "a single address");
4424 if (check_netmask($13->host, binat.af))
4427 if (!PF_AZERO(&binat.src.addr.v.a.mask,
4429 !PF_AEQ(&binat.src.addr.v.a.mask,
4430 &$13->host->addr.v.a.mask, binat.af)) {
4431 yyerror("'binat' source mask and "
4432 "redirect mask must be the same");
4436 TAILQ_INIT(&binat.rpool.list);
4437 pa = calloc(1, sizeof(struct pf_pooladdr));
4439 err(1, "binat: calloc");
4440 pa->addr = $13->host->addr;
4442 TAILQ_INSERT_TAIL(&binat.rpool.list,
4448 pfctl_append_rule(pf, &binat, "");
4452 tag : /* empty */ { $$ = NULL; }
4453 | TAG STRING { $$ = $2; }
4456 tagged : /* empty */ { $$.neg = 0; $$.name = NULL; }
4457 | not TAGGED string { $$.neg = $1; $$.name = $3; }
4460 rtable : /* empty */ { $$ = -1; }
4462 if ($2 < 0 || $2 > rt_tableid_max()) {
4463 yyerror("invalid rtable id");
4470 route_host : STRING {
4471 $$ = calloc(1, sizeof(struct node_host));
4473 err(1, "route_host: calloc");
4474 $$->ifname = strdup($1);
4475 set_ipmask($$, 128);
4479 | '(' STRING host ')' {
4480 struct node_host *n;
4483 for (n = $3; n != NULL; n = n->next)
4484 n->ifname = strdup($2);
4488 route_host_list : route_host optnl { $$ = $1; }
4489 | route_host_list comma route_host optnl {
4492 if ($1->af != $3->af) {
4493 yyerror("all pool addresses must be in the "
4494 "same address family");
4497 $1->tail->next = $3;
4498 $1->tail = $3->tail;
4503 routespec : route_host { $$ = $1; }
4504 | '{' optnl route_host_list '}' { $$ = $3; }
4507 route : /* empty */ {
4513 /* backwards-compat */
4518 | ROUTETO routespec pool_opts {
4521 $$.pool_opts = $3.type | $3.opts;
4525 | REPLYTO routespec pool_opts {
4528 $$.pool_opts = $3.type | $3.opts;
4532 | DUPTO routespec pool_opts {
4535 $$.pool_opts = $3.type | $3.opts;
4541 timeout_spec : STRING NUMBER
4543 if (check_rulestate(PFCTL_STATE_OPTION)) {
4547 if ($2 < 0 || $2 > UINT_MAX) {
4548 yyerror("only positive values permitted");
4551 if (pfctl_set_timeout(pf, $1, $2, 0) != 0) {
4552 yyerror("unknown timeout %s", $1);
4559 if (check_rulestate(PFCTL_STATE_OPTION))
4561 if ($2 < 0 || $2 > UINT_MAX) {
4562 yyerror("only positive values permitted");
4565 if (pfctl_set_timeout(pf, "interval", $2, 0) != 0)
4570 timeout_list : timeout_list comma timeout_spec optnl
4571 | timeout_spec optnl
4574 limit_spec : STRING NUMBER
4576 if (check_rulestate(PFCTL_STATE_OPTION)) {
4580 if ($2 < 0 || $2 > UINT_MAX) {
4581 yyerror("only positive values permitted");
4584 if (pfctl_set_limit(pf, $1, $2) != 0) {
4585 yyerror("unable to set limit %s %u", $1, $2);
4593 limit_list : limit_list comma limit_spec optnl
4601 yesno : NO { $$ = 0; }
4603 if (!strcmp($1, "yes"))
4606 yyerror("invalid value '%s', expected 'yes' "
4615 unaryop : '=' { $$ = PF_OP_EQ; }
4616 | '!' '=' { $$ = PF_OP_NE; }
4617 | '<' '=' { $$ = PF_OP_LE; }
4618 | '<' { $$ = PF_OP_LT; }
4619 | '>' '=' { $$ = PF_OP_GE; }
4620 | '>' { $$ = PF_OP_GT; }
4626 yyerror(const char *fmt, ...)
4632 fprintf(stderr, "%s:%d: ", file->name, yylval.lineno);
4633 vfprintf(stderr, fmt, ap);
4634 fprintf(stderr, "\n");
4640 disallow_table(struct node_host *h, const char *fmt)
4642 for (; h != NULL; h = h->next)
4643 if (h->addr.type == PF_ADDR_TABLE) {
4644 yyerror(fmt, h->addr.v.tblname);
4651 disallow_urpf_failed(struct node_host *h, const char *fmt)
4653 for (; h != NULL; h = h->next)
4654 if (h->addr.type == PF_ADDR_URPFFAILED) {
4662 disallow_alias(struct node_host *h, const char *fmt)
4664 for (; h != NULL; h = h->next)
4665 if (DYNIF_MULTIADDR(h->addr)) {
4666 yyerror(fmt, h->addr.v.tblname);
4673 rule_consistent(struct pfctl_rule *r, int anchor_call)
4677 switch (r->action) {
4682 problems = filter_consistent(r, anchor_call);
4686 problems = nat_consistent(r);
4690 problems = rdr_consistent(r);
4701 filter_consistent(struct pfctl_rule *r, int anchor_call)
4705 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP &&
4706 (r->src.port_op || r->dst.port_op)) {
4707 yyerror("port only applies to tcp/udp");
4710 if (r->proto != IPPROTO_ICMP && r->proto != IPPROTO_ICMPV6 &&
4711 (r->type || r->code)) {
4712 yyerror("icmp-type/code only applies to icmp");
4715 if (!r->af && (r->type || r->code)) {
4716 yyerror("must indicate address family with icmp-type/code");
4719 if (r->overload_tblname[0] &&
4720 r->max_src_conn == 0 && r->max_src_conn_rate.seconds == 0) {
4721 yyerror("'overload' requires 'max-src-conn' "
4722 "or 'max-src-conn-rate'");
4725 if ((r->proto == IPPROTO_ICMP && r->af == AF_INET6) ||
4726 (r->proto == IPPROTO_ICMPV6 && r->af == AF_INET)) {
4727 yyerror("proto %s doesn't match address family %s",
4728 r->proto == IPPROTO_ICMP ? "icmp" : "icmp6",
4729 r->af == AF_INET ? "inet" : "inet6");
4732 if (r->allow_opts && r->action != PF_PASS) {
4733 yyerror("allow-opts can only be specified for pass rules");
4736 if (r->rule_flag & PFRULE_FRAGMENT && (r->src.port_op ||
4737 r->dst.port_op || r->flagset || r->type || r->code)) {
4738 yyerror("fragments can be filtered only on IP header fields");
4741 if (r->rule_flag & PFRULE_RETURNRST && r->proto != IPPROTO_TCP) {
4742 yyerror("return-rst can only be applied to TCP rules");
4745 if (r->max_src_nodes && !(r->rule_flag & PFRULE_RULESRCTRACK)) {
4746 yyerror("max-src-nodes requires 'source-track rule'");
4749 if (r->action == PF_DROP && r->keep_state) {
4750 yyerror("keep state on block rules doesn't make sense");
4753 if (r->rule_flag & PFRULE_STATESLOPPY &&
4754 (r->keep_state == PF_STATE_MODULATE ||
4755 r->keep_state == PF_STATE_SYNPROXY)) {
4756 yyerror("sloppy state matching cannot be used with "
4757 "synproxy state or modulate state");
4764 nat_consistent(struct pfctl_rule *r)
4766 return (0); /* yeah! */
4770 rdr_consistent(struct pfctl_rule *r)
4774 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP) {
4775 if (r->src.port_op) {
4776 yyerror("src port only applies to tcp/udp");
4779 if (r->dst.port_op) {
4780 yyerror("dst port only applies to tcp/udp");
4783 if (r->rpool.proxy_port[0]) {
4784 yyerror("rpool port only applies to tcp/udp");
4788 if (r->dst.port_op &&
4789 r->dst.port_op != PF_OP_EQ && r->dst.port_op != PF_OP_RRG) {
4790 yyerror("invalid port operator for rdr destination port");
4797 process_tabledef(char *name, struct table_opts *opts)
4799 struct pfr_buffer ab;
4800 struct node_tinit *ti;
4801 unsigned long maxcount;
4802 size_t s = sizeof(maxcount);
4804 bzero(&ab, sizeof(ab));
4805 ab.pfrb_type = PFRB_ADDRS;
4806 SIMPLEQ_FOREACH(ti, &opts->init_nodes, entries) {
4808 if (pfr_buf_load(&ab, ti->file, 0, append_addr)) {
4810 yyerror("cannot load \"%s\": %s",
4811 ti->file, strerror(errno));
4813 yyerror("file \"%s\" contains bad data",
4818 if (append_addr_host(&ab, ti->host, 0, 0)) {
4819 yyerror("cannot create address buffer: %s",
4824 if (pf->opts & PF_OPT_VERBOSE)
4825 print_tabledef(name, opts->flags, opts->init_addr,
4827 if (!(pf->opts & PF_OPT_NOACTION) &&
4828 pfctl_define_table(name, opts->flags, opts->init_addr,
4829 pf->anchor->name, &ab, pf->anchor->ruleset.tticket)) {
4831 if (sysctlbyname("net.pf.request_maxcount", &maxcount, &s,
4835 if (ab.pfrb_size > maxcount)
4836 yyerror("cannot define table %s: too many elements.\n"
4837 "Consider increasing net.pf.request_maxcount.",
4840 yyerror("cannot define table %s: %s", name,
4841 pfr_strerror(errno));
4858 /* macro gore, but you should've seen the prior indentation nightmare... */
4860 #define FREE_LIST(T,r) \
4863 while (node != NULL) { \
4865 node = node->next; \
4870 #define LOOP_THROUGH(T,n,r,C) \
4874 r = calloc(1, sizeof(T)); \
4876 err(1, "LOOP: calloc"); \
4880 while (n != NULL) { \
4889 expand_label_str(char *label, size_t len, const char *srch, const char *repl)
4894 if ((tmp = calloc(1, len)) == NULL)
4895 err(1, "expand_label_str: calloc");
4897 while ((q = strstr(p, srch)) != NULL) {
4899 if ((strlcat(tmp, p, len) >= len) ||
4900 (strlcat(tmp, repl, len) >= len))
4901 errx(1, "expand_label: label too long");
4905 if (strlcat(tmp, p, len) >= len)
4906 errx(1, "expand_label: label too long");
4907 strlcpy(label, tmp, len); /* always fits */
4912 expand_label_if(const char *name, char *label, size_t len, const char *ifname)
4914 if (strstr(label, name) != NULL) {
4916 expand_label_str(label, len, name, "any");
4918 expand_label_str(label, len, name, ifname);
4923 expand_label_addr(const char *name, char *label, size_t len, sa_family_t af,
4924 struct node_host *h)
4926 char tmp[64], tmp_not[66];
4928 if (strstr(label, name) != NULL) {
4929 switch (h->addr.type) {
4930 case PF_ADDR_DYNIFTL:
4931 snprintf(tmp, sizeof(tmp), "(%s)", h->addr.v.ifname);
4934 snprintf(tmp, sizeof(tmp), "<%s>", h->addr.v.tblname);
4936 case PF_ADDR_NOROUTE:
4937 snprintf(tmp, sizeof(tmp), "no-route");
4939 case PF_ADDR_URPFFAILED:
4940 snprintf(tmp, sizeof(tmp), "urpf-failed");
4942 case PF_ADDR_ADDRMASK:
4943 if (!af || (PF_AZERO(&h->addr.v.a.addr, af) &&
4944 PF_AZERO(&h->addr.v.a.mask, af)))
4945 snprintf(tmp, sizeof(tmp), "any");
4950 if (inet_ntop(af, &h->addr.v.a.addr, a,
4952 snprintf(tmp, sizeof(tmp), "?");
4954 bits = unmask(&h->addr.v.a.mask, af);
4955 if ((af == AF_INET && bits < 32) ||
4956 (af == AF_INET6 && bits < 128))
4957 snprintf(tmp, sizeof(tmp),
4960 snprintf(tmp, sizeof(tmp),
4966 snprintf(tmp, sizeof(tmp), "?");
4971 snprintf(tmp_not, sizeof(tmp_not), "! %s", tmp);
4972 expand_label_str(label, len, name, tmp_not);
4974 expand_label_str(label, len, name, tmp);
4979 expand_label_port(const char *name, char *label, size_t len,
4980 struct node_port *port)
4982 char a1[6], a2[6], op[13] = "";
4984 if (strstr(label, name) != NULL) {
4985 snprintf(a1, sizeof(a1), "%u", ntohs(port->port[0]));
4986 snprintf(a2, sizeof(a2), "%u", ntohs(port->port[1]));
4989 else if (port->op == PF_OP_IRG)
4990 snprintf(op, sizeof(op), "%s><%s", a1, a2);
4991 else if (port->op == PF_OP_XRG)
4992 snprintf(op, sizeof(op), "%s<>%s", a1, a2);
4993 else if (port->op == PF_OP_EQ)
4994 snprintf(op, sizeof(op), "%s", a1);
4995 else if (port->op == PF_OP_NE)
4996 snprintf(op, sizeof(op), "!=%s", a1);
4997 else if (port->op == PF_OP_LT)
4998 snprintf(op, sizeof(op), "<%s", a1);
4999 else if (port->op == PF_OP_LE)
5000 snprintf(op, sizeof(op), "<=%s", a1);
5001 else if (port->op == PF_OP_GT)
5002 snprintf(op, sizeof(op), ">%s", a1);
5003 else if (port->op == PF_OP_GE)
5004 snprintf(op, sizeof(op), ">=%s", a1);
5005 expand_label_str(label, len, name, op);
5010 expand_label_proto(const char *name, char *label, size_t len, u_int8_t proto)
5012 struct protoent *pe;
5015 if (strstr(label, name) != NULL) {
5016 pe = getprotobynumber(proto);
5018 expand_label_str(label, len, name, pe->p_name);
5020 snprintf(n, sizeof(n), "%u", proto);
5021 expand_label_str(label, len, name, n);
5027 expand_label_nr(const char *name, char *label, size_t len)
5031 if (strstr(label, name) != NULL) {
5032 snprintf(n, sizeof(n), "%u", pf->anchor->match);
5033 expand_label_str(label, len, name, n);
5038 expand_label(char *label, size_t len, const char *ifname, sa_family_t af,
5039 struct node_host *src_host, struct node_port *src_port,
5040 struct node_host *dst_host, struct node_port *dst_port,
5043 expand_label_if("$if", label, len, ifname);
5044 expand_label_addr("$srcaddr", label, len, af, src_host);
5045 expand_label_addr("$dstaddr", label, len, af, dst_host);
5046 expand_label_port("$srcport", label, len, src_port);
5047 expand_label_port("$dstport", label, len, dst_port);
5048 expand_label_proto("$proto", label, len, proto);
5049 expand_label_nr("$nr", label, len);
5053 expand_altq(struct pf_altq *a, struct node_if *interfaces,
5054 struct node_queue *nqueues, struct node_queue_bw bwspec,
5055 struct node_queue_opt *opts)
5057 struct pf_altq pa, pb;
5058 char qname[PF_QNAME_SIZE];
5059 struct node_queue *n;
5060 struct node_queue_bw bw;
5063 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
5064 FREE_LIST(struct node_if, interfaces);
5066 FREE_LIST(struct node_queue, nqueues);
5070 LOOP_THROUGH(struct node_if, interface, interfaces,
5071 memcpy(&pa, a, sizeof(struct pf_altq));
5072 if (strlcpy(pa.ifname, interface->ifname,
5073 sizeof(pa.ifname)) >= sizeof(pa.ifname))
5074 errx(1, "expand_altq: strlcpy");
5076 if (interface->not) {
5077 yyerror("altq on ! <interface> is not supported");
5080 if (eval_pfaltq(pf, &pa, &bwspec, opts))
5083 if (pfctl_add_altq(pf, &pa))
5086 if (pf->opts & PF_OPT_VERBOSE) {
5087 print_altq(&pf->paltq->altq, 0,
5089 if (nqueues && nqueues->tail) {
5091 LOOP_THROUGH(struct node_queue, queue,
5101 if (pa.scheduler == ALTQT_CBQ ||
5102 pa.scheduler == ALTQT_HFSC ||
5103 pa.scheduler == ALTQT_FAIRQ) {
5104 /* now create a root queue */
5105 memset(&pb, 0, sizeof(struct pf_altq));
5106 if (strlcpy(qname, "root_", sizeof(qname)) >=
5108 errx(1, "expand_altq: strlcpy");
5109 if (strlcat(qname, interface->ifname,
5110 sizeof(qname)) >= sizeof(qname))
5111 errx(1, "expand_altq: strlcat");
5112 if (strlcpy(pb.qname, qname,
5113 sizeof(pb.qname)) >= sizeof(pb.qname))
5114 errx(1, "expand_altq: strlcpy");
5115 if (strlcpy(pb.ifname, interface->ifname,
5116 sizeof(pb.ifname)) >= sizeof(pb.ifname))
5117 errx(1, "expand_altq: strlcpy");
5118 pb.qlimit = pa.qlimit;
5119 pb.scheduler = pa.scheduler;
5120 bw.bw_absolute = pa.ifbandwidth;
5122 if (eval_pfqueue(pf, &pb, &bw, opts))
5125 if (pfctl_add_altq(pf, &pb))
5129 LOOP_THROUGH(struct node_queue, queue, nqueues,
5130 n = calloc(1, sizeof(struct node_queue));
5132 err(1, "expand_altq: calloc");
5133 if (pa.scheduler == ALTQT_CBQ ||
5134 pa.scheduler == ALTQT_HFSC ||
5135 pa.scheduler == ALTQT_FAIRQ)
5136 if (strlcpy(n->parent, qname,
5137 sizeof(n->parent)) >=
5139 errx(1, "expand_altq: strlcpy");
5140 if (strlcpy(n->queue, queue->queue,
5141 sizeof(n->queue)) >= sizeof(n->queue))
5142 errx(1, "expand_altq: strlcpy");
5143 if (strlcpy(n->ifname, interface->ifname,
5144 sizeof(n->ifname)) >= sizeof(n->ifname))
5145 errx(1, "expand_altq: strlcpy");
5146 n->scheduler = pa.scheduler;
5152 queues->tail->next = n;
5158 FREE_LIST(struct node_if, interfaces);
5160 FREE_LIST(struct node_queue, nqueues);
5166 expand_queue(struct pf_altq *a, struct node_if *interfaces,
5167 struct node_queue *nqueues, struct node_queue_bw bwspec,
5168 struct node_queue_opt *opts)
5170 struct node_queue *n, *nq;
5175 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
5176 FREE_LIST(struct node_queue, nqueues);
5180 if (queues == NULL) {
5181 yyerror("queue %s has no parent", a->qname);
5182 FREE_LIST(struct node_queue, nqueues);
5186 LOOP_THROUGH(struct node_if, interface, interfaces,
5187 LOOP_THROUGH(struct node_queue, tqueue, queues,
5188 if (!strncmp(a->qname, tqueue->queue, PF_QNAME_SIZE) &&
5189 (interface->ifname[0] == 0 ||
5190 (!interface->not && !strncmp(interface->ifname,
5191 tqueue->ifname, IFNAMSIZ)) ||
5192 (interface->not && strncmp(interface->ifname,
5193 tqueue->ifname, IFNAMSIZ)))) {
5194 /* found ourself in queues */
5197 memcpy(&pa, a, sizeof(struct pf_altq));
5199 if (pa.scheduler != ALTQT_NONE &&
5200 pa.scheduler != tqueue->scheduler) {
5201 yyerror("exactly one scheduler type "
5202 "per interface allowed");
5205 pa.scheduler = tqueue->scheduler;
5207 /* scheduler dependent error checking */
5208 switch (pa.scheduler) {
5210 if (nqueues != NULL) {
5211 yyerror("priq queues cannot "
5212 "have child queues");
5215 if (bwspec.bw_absolute > 0 ||
5216 bwspec.bw_percent < 100) {
5217 yyerror("priq doesn't take "
5226 if (strlcpy(pa.ifname, tqueue->ifname,
5227 sizeof(pa.ifname)) >= sizeof(pa.ifname))
5228 errx(1, "expand_queue: strlcpy");
5229 if (strlcpy(pa.parent, tqueue->parent,
5230 sizeof(pa.parent)) >= sizeof(pa.parent))
5231 errx(1, "expand_queue: strlcpy");
5233 if (eval_pfqueue(pf, &pa, &bwspec, opts))
5236 if (pfctl_add_altq(pf, &pa))
5239 for (nq = nqueues; nq != NULL; nq = nq->next) {
5240 if (!strcmp(a->qname, nq->queue)) {
5241 yyerror("queue cannot have "
5247 sizeof(struct node_queue));
5249 err(1, "expand_queue: calloc");
5250 if (strlcpy(n->parent, a->qname,
5251 sizeof(n->parent)) >=
5253 errx(1, "expand_queue strlcpy");
5254 if (strlcpy(n->queue, nq->queue,
5255 sizeof(n->queue)) >=
5257 errx(1, "expand_queue strlcpy");
5258 if (strlcpy(n->ifname, tqueue->ifname,
5259 sizeof(n->ifname)) >=
5261 errx(1, "expand_queue strlcpy");
5262 n->scheduler = tqueue->scheduler;
5268 queues->tail->next = n;
5272 if ((pf->opts & PF_OPT_VERBOSE) && (
5273 (found == 1 && interface->ifname[0] == 0) ||
5274 (found > 0 && interface->ifname[0] != 0))) {
5275 print_queue(&pf->paltq->altq, 0,
5276 &bwspec, interface->ifname[0] != 0,
5278 if (nqueues && nqueues->tail) {
5280 LOOP_THROUGH(struct node_queue,
5293 FREE_LIST(struct node_queue, nqueues);
5294 FREE_LIST(struct node_if, interfaces);
5297 yyerror("queue %s has no parent", a->qname);
5308 expand_rule(struct pfctl_rule *r,
5309 struct node_if *interfaces, struct node_host *rpool_hosts,
5310 struct node_proto *protos, struct node_os *src_oses,
5311 struct node_host *src_hosts, struct node_port *src_ports,
5312 struct node_host *dst_hosts, struct node_port *dst_ports,
5313 struct node_uid *uids, struct node_gid *gids, struct node_icmp *icmp_types,
5314 const char *anchor_call)
5316 sa_family_t af = r->af;
5317 int added = 0, error = 0;
5318 char ifname[IF_NAMESIZE];
5319 char label[PF_RULE_MAX_LABEL_COUNT][PF_RULE_LABEL_SIZE];
5320 char tagname[PF_TAG_NAME_SIZE];
5321 char match_tagname[PF_TAG_NAME_SIZE];
5322 struct pf_pooladdr *pa;
5323 struct node_host *h;
5324 u_int8_t flags, flagset, keep_state;
5326 memcpy(label, r->label, sizeof(r->label));
5327 assert(sizeof(r->label) == sizeof(label));
5328 if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname))
5329 errx(1, "expand_rule: strlcpy");
5330 if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >=
5331 sizeof(match_tagname))
5332 errx(1, "expand_rule: strlcpy");
5334 flagset = r->flagset;
5335 keep_state = r->keep_state;
5337 LOOP_THROUGH(struct node_if, interface, interfaces,
5338 LOOP_THROUGH(struct node_proto, proto, protos,
5339 LOOP_THROUGH(struct node_icmp, icmp_type, icmp_types,
5340 LOOP_THROUGH(struct node_host, src_host, src_hosts,
5341 LOOP_THROUGH(struct node_port, src_port, src_ports,
5342 LOOP_THROUGH(struct node_os, src_os, src_oses,
5343 LOOP_THROUGH(struct node_host, dst_host, dst_hosts,
5344 LOOP_THROUGH(struct node_port, dst_port, dst_ports,
5345 LOOP_THROUGH(struct node_uid, uid, uids,
5346 LOOP_THROUGH(struct node_gid, gid, gids,
5349 /* for link-local IPv6 address, interface must match up */
5350 if ((r->af && src_host->af && r->af != src_host->af) ||
5351 (r->af && dst_host->af && r->af != dst_host->af) ||
5352 (src_host->af && dst_host->af &&
5353 src_host->af != dst_host->af) ||
5354 (src_host->ifindex && dst_host->ifindex &&
5355 src_host->ifindex != dst_host->ifindex) ||
5356 (src_host->ifindex && *interface->ifname &&
5357 src_host->ifindex != if_nametoindex(interface->ifname)) ||
5358 (dst_host->ifindex && *interface->ifname &&
5359 dst_host->ifindex != if_nametoindex(interface->ifname)))
5361 if (!r->af && src_host->af)
5362 r->af = src_host->af;
5363 else if (!r->af && dst_host->af)
5364 r->af = dst_host->af;
5366 if (*interface->ifname)
5367 strlcpy(r->ifname, interface->ifname,
5369 else if (if_indextoname(src_host->ifindex, ifname))
5370 strlcpy(r->ifname, ifname, sizeof(r->ifname));
5371 else if (if_indextoname(dst_host->ifindex, ifname))
5372 strlcpy(r->ifname, ifname, sizeof(r->ifname));
5374 memset(r->ifname, '\0', sizeof(r->ifname));
5376 memcpy(r->label, label, sizeof(r->label));
5377 if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >=
5379 errx(1, "expand_rule: strlcpy");
5380 if (strlcpy(r->match_tagname, match_tagname,
5381 sizeof(r->match_tagname)) >= sizeof(r->match_tagname))
5382 errx(1, "expand_rule: strlcpy");
5383 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++)
5384 expand_label(r->label[i], PF_RULE_LABEL_SIZE,
5385 r->ifname, r->af, src_host, src_port, dst_host,
5386 dst_port, proto->proto);
5387 expand_label(r->tagname, PF_TAG_NAME_SIZE, r->ifname, r->af,
5388 src_host, src_port, dst_host, dst_port, proto->proto);
5389 expand_label(r->match_tagname, PF_TAG_NAME_SIZE, r->ifname,
5390 r->af, src_host, src_port, dst_host, dst_port,
5393 error += check_netmask(src_host, r->af);
5394 error += check_netmask(dst_host, r->af);
5396 r->ifnot = interface->not;
5397 r->proto = proto->proto;
5398 r->src.addr = src_host->addr;
5399 r->src.neg = src_host->not;
5400 r->src.port[0] = src_port->port[0];
5401 r->src.port[1] = src_port->port[1];
5402 r->src.port_op = src_port->op;
5403 r->dst.addr = dst_host->addr;
5404 r->dst.neg = dst_host->not;
5405 r->dst.port[0] = dst_port->port[0];
5406 r->dst.port[1] = dst_port->port[1];
5407 r->dst.port_op = dst_port->op;
5408 r->uid.op = uid->op;
5409 r->uid.uid[0] = uid->uid[0];
5410 r->uid.uid[1] = uid->uid[1];
5411 r->gid.op = gid->op;
5412 r->gid.gid[0] = gid->gid[0];
5413 r->gid.gid[1] = gid->gid[1];
5414 r->type = icmp_type->type;
5415 r->code = icmp_type->code;
5417 if ((keep_state == PF_STATE_MODULATE ||
5418 keep_state == PF_STATE_SYNPROXY) &&
5419 r->proto && r->proto != IPPROTO_TCP)
5420 r->keep_state = PF_STATE_NORMAL;
5422 r->keep_state = keep_state;
5424 if (r->proto && r->proto != IPPROTO_TCP) {
5429 r->flagset = flagset;
5431 if (icmp_type->proto && r->proto != icmp_type->proto) {
5432 yyerror("icmp-type mismatch");
5436 if (src_os && src_os->os) {
5437 r->os_fingerprint = pfctl_get_fingerprint(src_os->os);
5438 if ((pf->opts & PF_OPT_VERBOSE2) &&
5439 r->os_fingerprint == PF_OSFP_NOMATCH)
5441 "warning: unknown '%s' OS fingerprint\n",
5444 r->os_fingerprint = PF_OSFP_ANY;
5447 TAILQ_INIT(&r->rpool.list);
5448 for (h = rpool_hosts; h != NULL; h = h->next) {
5449 pa = calloc(1, sizeof(struct pf_pooladdr));
5451 err(1, "expand_rule: calloc");
5453 if (h->ifname != NULL) {
5454 if (strlcpy(pa->ifname, h->ifname,
5455 sizeof(pa->ifname)) >=
5457 errx(1, "expand_rule: strlcpy");
5460 TAILQ_INSERT_TAIL(&r->rpool.list, pa, entries);
5463 if (rule_consistent(r, anchor_call[0]) < 0 || error)
5464 yyerror("skipping rule due to errors");
5466 r->nr = pf->astack[pf->asd]->match++;
5467 pfctl_append_rule(pf, r, anchor_call);
5473 FREE_LIST(struct node_if, interfaces);
5474 FREE_LIST(struct node_proto, protos);
5475 FREE_LIST(struct node_host, src_hosts);
5476 FREE_LIST(struct node_port, src_ports);
5477 FREE_LIST(struct node_os, src_oses);
5478 FREE_LIST(struct node_host, dst_hosts);
5479 FREE_LIST(struct node_port, dst_ports);
5480 FREE_LIST(struct node_uid, uids);
5481 FREE_LIST(struct node_gid, gids);
5482 FREE_LIST(struct node_icmp, icmp_types);
5483 FREE_LIST(struct node_host, rpool_hosts);
5486 yyerror("rule expands to no valid combination");
5490 expand_skip_interface(struct node_if *interfaces)
5494 if (!interfaces || (!interfaces->next && !interfaces->not &&
5495 !strcmp(interfaces->ifname, "none"))) {
5496 if (pf->opts & PF_OPT_VERBOSE)
5497 printf("set skip on none\n");
5498 errs = pfctl_set_interface_flags(pf, "", PFI_IFLAG_SKIP, 0);
5502 if (pf->opts & PF_OPT_VERBOSE)
5503 printf("set skip on {");
5504 LOOP_THROUGH(struct node_if, interface, interfaces,
5505 if (pf->opts & PF_OPT_VERBOSE)
5506 printf(" %s", interface->ifname);
5507 if (interface->not) {
5508 yyerror("skip on ! <interface> is not supported");
5511 errs += pfctl_set_interface_flags(pf,
5512 interface->ifname, PFI_IFLAG_SKIP, 1);
5514 if (pf->opts & PF_OPT_VERBOSE)
5517 FREE_LIST(struct node_if, interfaces);
5529 check_rulestate(int desired_state)
5531 if (require_order && (rulestate > desired_state)) {
5532 yyerror("Rules must be in order: options, normalization, "
5533 "queueing, translation, filtering");
5536 rulestate = desired_state;
5541 kw_cmp(const void *k, const void *e)
5543 return (strcmp(k, ((const struct keywords *)e)->k_name));
5549 /* this has to be sorted always */
5550 static const struct keywords keywords[] = {
5552 { "allow-opts", ALLOWOPTS},
5554 { "anchor", ANCHOR},
5555 { "antispoof", ANTISPOOF},
5557 { "bandwidth", BANDWIDTH},
5559 { "binat-anchor", BINATANCHOR},
5560 { "bitmask", BITMASK},
5562 { "block-policy", BLOCKPOLICY},
5563 { "buckets", BUCKETS},
5567 { "crop", FRAGCROP},
5569 { "divert-reply", DIVERTREPLY},
5570 { "divert-to", DIVERTTO},
5572 { "drop-ovl", FRAGDROP},
5574 { "fail-policy", FAILPOLICY},
5576 { "fastroute", FASTROUTE},
5577 { "file", FILENAME},
5578 { "fingerprints", FINGERPRINTS},
5580 { "floating", FLOATING},
5583 { "fragment", FRAGMENT},
5585 { "global", GLOBAL},
5589 { "hostid", HOSTID},
5590 { "icmp-type", ICMPTYPE},
5591 { "icmp6-type", ICMP6TYPE},
5592 { "if-bound", IFBOUND},
5594 { "include", INCLUDE},
5597 { "interval", INTERVAL},
5599 { "keepcounters", KEEPCOUNTERS},
5602 { "linkshare", LINKSHARE},
5605 { "loginterface", LOGINTERFACE},
5606 { "map-e-portset", MAPEPORTSET},
5608 { "max-mss", MAXMSS},
5609 { "max-src-conn", MAXSRCCONN},
5610 { "max-src-conn-rate", MAXSRCCONNRATE},
5611 { "max-src-nodes", MAXSRCNODES},
5612 { "max-src-states", MAXSRCSTATES},
5613 { "min-ttl", MINTTL},
5614 { "modulate", MODULATE},
5616 { "nat-anchor", NATANCHOR},
5619 { "no-route", NOROUTE},
5620 { "no-sync", NOSYNC},
5622 { "optimization", OPTIMIZATION},
5625 { "overload", OVERLOAD},
5629 { "priority", PRIORITY},
5631 { "probability", PROBABILITY},
5633 { "qlimit", QLIMIT},
5636 { "random", RANDOM},
5637 { "random-id", RANDOMID},
5639 { "rdr-anchor", RDRANCHOR},
5640 { "realtime", REALTIME},
5641 { "reassemble", REASSEMBLE},
5642 { "reply-to", REPLYTO},
5643 { "require-order", REQUIREORDER},
5644 { "return", RETURN},
5645 { "return-icmp", RETURNICMP},
5646 { "return-icmp6", RETURNICMP6},
5647 { "return-rst", RETURNRST},
5648 { "round-robin", ROUNDROBIN},
5650 { "route-to", ROUTETO},
5651 { "rtable", RTABLE},
5653 { "ruleset-optimization", RULESET_OPTIMIZATION},
5656 { "set-tos", SETTOS},
5658 { "sloppy", SLOPPY},
5659 { "source-hash", SOURCEHASH},
5660 { "source-track", SOURCETRACK},
5662 { "state-defaults", STATEDEFAULTS},
5663 { "state-policy", STATEPOLICY},
5664 { "static-port", STATICPORT},
5665 { "sticky-address", STICKYADDRESS},
5666 { "synproxy", SYNPROXY},
5669 { "tagged", TAGGED},
5670 { "target", TARGET},
5671 { "tbrsize", TBRSIZE},
5672 { "timeout", TIMEOUT},
5676 { "upperlimit", UPPERLIMIT},
5677 { "urpf-failed", URPFFAILED},
5680 const struct keywords *p;
5682 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
5683 sizeof(keywords[0]), kw_cmp);
5687 fprintf(stderr, "%s: %d\n", s, p->k_val);
5691 fprintf(stderr, "string: %s\n", s);
5696 #define MAXPUSHBACK 128
5698 static char *parsebuf;
5699 static int parseindex;
5700 static char pushback_buffer[MAXPUSHBACK];
5701 static int pushback_index = 0;
5709 /* Read character from the parsebuffer instead of input. */
5710 if (parseindex >= 0) {
5711 c = parsebuf[parseindex++];
5720 return (pushback_buffer[--pushback_index]);
5723 if ((c = getc(file->stream)) == EOF) {
5724 yyerror("reached end of file while parsing quoted string");
5725 if (popfile() == EOF)
5732 while ((c = getc(file->stream)) == '\\') {
5733 next = getc(file->stream);
5738 yylval.lineno = file->lineno;
5743 if (popfile() == EOF)
5745 c = getc(file->stream);
5757 if (parseindex >= 0)
5760 if (pushback_index < MAXPUSHBACK-1)
5761 return (pushback_buffer[pushback_index++] = c);
5773 /* skip to either EOF or the first real EOL */
5776 c = pushback_buffer[--pushback_index];
5794 int quotec, next, c;
5799 while ((c = lgetc(0)) == ' ' || c == '\t')
5802 yylval.lineno = file->lineno;
5804 while ((c = lgetc(0)) != '\n' && c != EOF)
5806 if (c == '$' && parsebuf == NULL) {
5808 if ((c = lgetc(0)) == EOF)
5811 if (p + 1 >= buf + sizeof(buf) - 1) {
5812 yyerror("string too long");
5815 if (isalnum(c) || c == '_') {
5825 yyerror("macro '%s' not defined", buf);
5838 if ((c = lgetc(quotec)) == EOF)
5843 } else if (c == '\\') {
5844 if ((next = lgetc(quotec)) == EOF)
5846 if (next == quotec || c == ' ' || c == '\t')
5848 else if (next == '\n') {
5854 } else if (c == quotec) {
5858 if (p + 1 >= buf + sizeof(buf) - 1) {
5859 yyerror("string too long");
5864 yylval.v.string = strdup(buf);
5865 if (yylval.v.string == NULL)
5866 err(1, "yylex: strdup");
5871 yylval.v.i = PF_OP_XRG;
5872 return (PORTBINARY);
5879 yylval.v.i = PF_OP_IRG;
5880 return (PORTBINARY);
5892 #define allowed_to_end_number(x) \
5893 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
5895 if (c == '-' || isdigit(c)) {
5898 if ((unsigned)(p-buf) >= sizeof(buf)) {
5899 yyerror("string too long");
5902 } while ((c = lgetc(0)) != EOF && isdigit(c));
5904 if (p == buf + 1 && buf[0] == '-')
5906 if (c == EOF || allowed_to_end_number(c)) {
5907 const char *errstr = NULL;
5910 yylval.v.number = strtonum(buf, LLONG_MIN,
5911 LLONG_MAX, &errstr);
5913 yyerror("\"%s\" invalid number: %s",
5928 #define allowed_in_string(x) \
5929 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
5930 x != '{' && x != '}' && x != '<' && x != '>' && \
5931 x != '!' && x != '=' && x != '/' && x != '#' && \
5934 if (isalnum(c) || c == ':' || c == '_') {
5937 if ((unsigned)(p-buf) >= sizeof(buf)) {
5938 yyerror("string too long");
5941 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
5944 if ((token = lookup(buf)) == STRING)
5945 if ((yylval.v.string = strdup(buf)) == NULL)
5946 err(1, "yylex: strdup");
5950 yylval.lineno = file->lineno;
5959 check_file_secrecy(int fd, const char *fname)
5963 if (fstat(fd, &st)) {
5964 warn("cannot stat %s", fname);
5967 if (st.st_uid != 0 && st.st_uid != getuid()) {
5968 warnx("%s: owner not root or current user", fname);
5971 if (st.st_mode & (S_IRWXG | S_IRWXO)) {
5972 warnx("%s: group/world readable/writeable", fname);
5979 pushfile(const char *name, int secret)
5983 if ((nfile = calloc(1, sizeof(struct file))) == NULL ||
5984 (nfile->name = strdup(name)) == NULL) {
5988 if (TAILQ_FIRST(&files) == NULL && strcmp(nfile->name, "-") == 0) {
5989 nfile->stream = stdin;
5991 if ((nfile->name = strdup("stdin")) == NULL) {
5996 } else if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
5997 warn("%s", nfile->name);
6001 } else if (secret &&
6002 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
6003 fclose(nfile->stream);
6009 TAILQ_INSERT_TAIL(&files, nfile, entry);
6018 if ((prev = TAILQ_PREV(file, files, entry)) != NULL) {
6019 prev->errors += file->errors;
6020 TAILQ_REMOVE(&files, file, entry);
6021 fclose(file->stream);
6031 parse_config(char *filename, struct pfctl *xpf)
6038 rulestate = PFCTL_STATE_NONE;
6039 returnicmpdefault = (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
6040 returnicmp6default =
6041 (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
6042 blockpolicy = PFRULE_DROP;
6043 failpolicy = PFRULE_DROP;
6046 if ((file = pushfile(filename, 0)) == NULL) {
6047 warn("cannot open the main config file!");
6052 errors = file->errors;
6055 /* Free macros and check which have not been used. */
6056 while ((sym = TAILQ_FIRST(&symhead))) {
6057 if ((pf->opts & PF_OPT_VERBOSE2) && !sym->used)
6058 fprintf(stderr, "warning: macro '%s' not "
6059 "used\n", sym->nam);
6062 TAILQ_REMOVE(&symhead, sym, entry);
6066 return (errors ? -1 : 0);
6070 symset(const char *nam, const char *val, int persist)
6074 for (sym = TAILQ_FIRST(&symhead); sym && strcmp(nam, sym->nam);
6075 sym = TAILQ_NEXT(sym, entry))
6079 if (sym->persist == 1)
6084 TAILQ_REMOVE(&symhead, sym, entry);
6088 if ((sym = calloc(1, sizeof(*sym))) == NULL)
6091 sym->nam = strdup(nam);
6092 if (sym->nam == NULL) {
6096 sym->val = strdup(val);
6097 if (sym->val == NULL) {
6103 sym->persist = persist;
6104 TAILQ_INSERT_TAIL(&symhead, sym, entry);
6109 pfctl_cmdline_symset(char *s)
6114 if ((val = strrchr(s, '=')) == NULL)
6117 if ((sym = malloc(strlen(s) - strlen(val) + 1)) == NULL)
6118 err(1, "pfctl_cmdline_symset: malloc");
6120 strlcpy(sym, s, strlen(s) - strlen(val) + 1);
6122 ret = symset(sym, val + 1, 1);
6129 symget(const char *nam)
6133 TAILQ_FOREACH(sym, &symhead, entry)
6134 if (strcmp(nam, sym->nam) == 0) {
6142 mv_rules(struct pfctl_ruleset *src, struct pfctl_ruleset *dst)
6145 struct pfctl_rule *r;
6147 for (i = 0; i < PF_RULESET_MAX; ++i) {
6148 while ((r = TAILQ_FIRST(src->rules[i].active.ptr))
6150 TAILQ_REMOVE(src->rules[i].active.ptr, r, entries);
6151 TAILQ_INSERT_TAIL(dst->rules[i].active.ptr, r, entries);
6152 dst->anchor->match++;
6154 src->anchor->match = 0;
6155 while ((r = TAILQ_FIRST(src->rules[i].inactive.ptr))
6157 TAILQ_REMOVE(src->rules[i].inactive.ptr, r, entries);
6158 TAILQ_INSERT_TAIL(dst->rules[i].inactive.ptr,
6165 decide_address_family(struct node_host *n, sa_family_t *af)
6167 if (*af != 0 || n == NULL)
6170 while ((n = n->next) != NULL) {
6179 remove_invalid_hosts(struct node_host **nh, sa_family_t *af)
6181 struct node_host *n = *nh, *prev = NULL;
6184 if (*af && n->af && n->af != *af) {
6185 /* unlink and free n */
6186 struct node_host *next = n->next;
6188 /* adjust tail pointer */
6189 if (n == (*nh)->tail)
6191 /* adjust previous node's next pointer */
6197 if (n->ifname != NULL)
6211 invalid_redirect(struct node_host *nh, sa_family_t af)
6214 struct node_host *n;
6216 /* tables and dyniftl are ok without an address family */
6217 for (n = nh; n != NULL; n = n->next) {
6218 if (n->addr.type != PF_ADDR_TABLE &&
6219 n->addr.type != PF_ADDR_DYNIFTL) {
6220 yyerror("address family not given and "
6221 "translation address expands to multiple "
6222 "address families");
6228 yyerror("no translation address with matching address family "
6236 atoul(char *s, u_long *ulvalp)
6242 ulval = strtoul(s, &ep, 0);
6243 if (s[0] == '\0' || *ep != '\0')
6245 if (errno == ERANGE && ulval == ULONG_MAX)
6257 if (atoul(n, &ulval) == 0) {
6258 if (ulval > 65535) {
6259 yyerror("illegal port value %lu", ulval);
6262 return (htons(ulval));
6264 s = getservbyname(n, "tcp");
6266 s = getservbyname(n, "udp");
6268 yyerror("unknown port %s", n);
6276 rule_label(struct pfctl_rule *r, char *s[PF_RULE_MAX_LABEL_COUNT])
6278 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++) {
6282 if (strlcpy(r->label[i], s[i], sizeof(r->label[0])) >=
6283 sizeof(r->label[0])) {
6284 yyerror("rule label too long (max %d chars)",
6285 sizeof(r->label[0])-1);
6293 parseicmpspec(char *w, sa_family_t af)
6295 const struct icmpcodeent *p;
6300 icmptype = returnicmpdefault >> 8;
6302 icmptype = returnicmp6default >> 8;
6304 if (atoul(w, &ulval) == -1) {
6305 if ((p = geticmpcodebyname(icmptype, w, af)) == NULL) {
6306 yyerror("unknown icmp code %s", w);
6312 yyerror("invalid icmp code %lu", ulval);
6315 return (icmptype << 8 | ulval);
6319 parseport(char *port, struct range *r, int extensions)
6321 char *p = strchr(port, ':');
6324 if ((r->a = getservice(port)) == -1)
6330 if ((extensions & PPORT_STAR) && !strcmp(p+1, "*")) {
6332 if ((r->a = getservice(port)) == -1)
6338 if ((extensions & PPORT_RANGE)) {
6340 if ((r->a = getservice(port)) == -1 ||
6341 (r->b = getservice(p)) == -1)
6354 pfctl_load_anchors(int dev, struct pfctl *pf, struct pfr_buffer *trans)
6356 struct loadanchors *la;
6358 TAILQ_FOREACH(la, &loadanchorshead, entries) {
6359 if (pf->opts & PF_OPT_VERBOSE)
6360 fprintf(stderr, "\nLoading anchor %s from %s\n",
6361 la->anchorname, la->filename);
6362 if (pfctl_rules(dev, la->filename, pf->opts, pf->optimize,
6363 la->anchorname, trans) == -1)
6371 kw_casecmp(const void *k, const void *e)
6373 return (strcasecmp(k, ((const struct keywords *)e)->k_name));
6377 map_tos(char *s, int *val)
6379 /* DiffServ Codepoints and other TOS mappings */
6380 const struct keywords toswords[] = {
6381 { "af11", IPTOS_DSCP_AF11 },
6382 { "af12", IPTOS_DSCP_AF12 },
6383 { "af13", IPTOS_DSCP_AF13 },
6384 { "af21", IPTOS_DSCP_AF21 },
6385 { "af22", IPTOS_DSCP_AF22 },
6386 { "af23", IPTOS_DSCP_AF23 },
6387 { "af31", IPTOS_DSCP_AF31 },
6388 { "af32", IPTOS_DSCP_AF32 },
6389 { "af33", IPTOS_DSCP_AF33 },
6390 { "af41", IPTOS_DSCP_AF41 },
6391 { "af42", IPTOS_DSCP_AF42 },
6392 { "af43", IPTOS_DSCP_AF43 },
6393 { "critical", IPTOS_PREC_CRITIC_ECP },
6394 { "cs0", IPTOS_DSCP_CS0 },
6395 { "cs1", IPTOS_DSCP_CS1 },
6396 { "cs2", IPTOS_DSCP_CS2 },
6397 { "cs3", IPTOS_DSCP_CS3 },
6398 { "cs4", IPTOS_DSCP_CS4 },
6399 { "cs5", IPTOS_DSCP_CS5 },
6400 { "cs6", IPTOS_DSCP_CS6 },
6401 { "cs7", IPTOS_DSCP_CS7 },
6402 { "ef", IPTOS_DSCP_EF },
6403 { "inetcontrol", IPTOS_PREC_INTERNETCONTROL },
6404 { "lowdelay", IPTOS_LOWDELAY },
6405 { "netcontrol", IPTOS_PREC_NETCONTROL },
6406 { "reliability", IPTOS_RELIABILITY },
6407 { "throughput", IPTOS_THROUGHPUT },
6408 { "va", IPTOS_DSCP_VA }
6410 const struct keywords *p;
6412 p = bsearch(s, toswords, sizeof(toswords)/sizeof(toswords[0]),
6413 sizeof(toswords[0]), kw_casecmp);
6423 rt_tableid_max(void)
6427 size_t l = sizeof(fibs);
6429 if (sysctlbyname("net.fibs", &fibs, &l, NULL, 0) == -1)
6430 fibs = 16; /* XXX RT_MAXFIBS, at least limit it some. */
6432 * As the OpenBSD code only compares > and not >= we need to adjust
6433 * here given we only accept values of 0..n and want to avoid #ifdefs
6438 return (RT_TABLEID_MAX);