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
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;
727 stringall : STRING { $$ = $1; }
729 if (($$ = strdup("all")) == NULL) {
730 err(1, "stringall: strdup");
735 string : STRING string {
736 if (asprintf(&$$, "%s %s", $1, $2) == -1)
737 err(1, "string: asprintf");
744 varstring : numberstring varstring {
745 if (asprintf(&$$, "%s %s", $1, $2) == -1)
746 err(1, "string: asprintf");
753 numberstring : NUMBER {
755 if (asprintf(&s, "%lld", (long long)$1) == -1) {
756 yyerror("string: asprintf");
764 varset : STRING '=' varstring {
765 if (pf->opts & PF_OPT_VERBOSE)
766 printf("%s = \"%s\"\n", $1, $3);
767 if (symset($1, $3, 0) == -1)
768 err(1, "cannot store variable %s", $1);
774 anchorname : STRING { $$ = $1; }
775 | /* empty */ { $$ = NULL; }
778 pfa_anchorlist : /* empty */
779 | pfa_anchorlist '\n'
780 | pfa_anchorlist pfrule '\n'
781 | pfa_anchorlist anchorrule '\n'
786 char ta[PF_ANCHOR_NAME_SIZE];
787 struct pfctl_ruleset *rs;
789 /* steping into a brace anchor */
794 /* create a holding ruleset in the root */
795 snprintf(ta, PF_ANCHOR_NAME_SIZE, "_%d", pf->bn);
796 rs = pf_find_or_create_ruleset(ta);
798 err(1, "pfa_anchor: pf_find_or_create_ruleset");
799 pf->astack[pf->asd] = rs->anchor;
800 pf->anchor = rs->anchor;
801 } '\n' pfa_anchorlist '}'
803 pf->alast = pf->anchor;
805 pf->anchor = pf->astack[pf->asd];
810 anchorrule : ANCHOR anchorname dir quick interface af proto fromto
811 filter_opts pfa_anchor
814 struct node_proto *proto;
816 if (check_rulestate(PFCTL_STATE_FILTER)) {
822 if ($2 && ($2[0] == '_' || strstr($2, "/_") != NULL)) {
824 yyerror("anchor names beginning with '_' "
825 "are reserved for internal use");
829 memset(&r, 0, sizeof(r));
830 if (pf->astack[pf->asd + 1]) {
831 /* move inline rules into relative location */
832 pfctl_anchor_setup(&r,
833 &pf->astack[pf->asd]->ruleset,
834 $2 ? $2 : pf->alast->name);
836 if (r.anchor == NULL)
837 err(1, "anchorrule: unable to "
840 if (pf->alast != r.anchor) {
841 if (r.anchor->match) {
842 yyerror("inline anchor '%s' "
847 mv_rules(&pf->alast->ruleset,
850 pf_remove_if_empty_ruleset(&pf->alast->ruleset);
851 pf->alast = r.anchor;
854 yyerror("anchors without explicit "
855 "rules must specify a name");
863 r.rtableid = $9.rtableid;
866 if (strlcpy(r.tagname, $9.tag,
867 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
868 yyerror("tag too long, max %u chars",
869 PF_TAG_NAME_SIZE - 1);
873 if (strlcpy(r.match_tagname, $9.match_tag,
874 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
875 yyerror("tag too long, max %u chars",
876 PF_TAG_NAME_SIZE - 1);
879 r.match_tag_not = $9.match_tag_not;
880 if (rule_label(&r, $9.label))
882 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++)
884 r.flags = $9.flags.b1;
885 r.flagset = $9.flags.b2;
886 if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
887 yyerror("flags always false");
890 if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
891 for (proto = $7; proto != NULL &&
892 proto->proto != IPPROTO_TCP;
895 if (proto == NULL && $7 != NULL) {
896 if ($9.flags.b1 || $9.flags.b2)
898 "flags only apply to tcp");
901 "OS fingerprinting only "
909 if ($9.keep.action) {
910 yyerror("cannot specify state handling "
916 if (strlcpy(r.match_tagname, $9.match_tag,
917 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
918 yyerror("tag too long, max %u chars",
919 PF_TAG_NAME_SIZE - 1);
922 r.match_tag_not = $9.match_tag_not;
923 if ($9.marker & FOM_PRIO) {
925 r.prio = PF_PRIO_ZERO;
929 if ($9.marker & FOM_SETPRIO) {
930 r.set_prio[0] = $9.set_prio[0];
931 r.set_prio[1] = $9.set_prio[1];
932 r.scrub_flags |= PFSTATE_SETPRIO;
935 decide_address_family($8.src.host, &r.af);
936 decide_address_family($8.dst.host, &r.af);
938 expand_rule(&r, $5, NULL, $7, $8.src_os,
939 $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
940 $9.uid, $9.gid, $9.icmpspec,
941 pf->astack[pf->asd + 1] ? pf->alast->name : $2);
943 pf->astack[pf->asd + 1] = NULL;
945 | NATANCHOR string interface af proto fromto rtable {
948 if (check_rulestate(PFCTL_STATE_NAT)) {
953 memset(&r, 0, sizeof(r));
958 decide_address_family($6.src.host, &r.af);
959 decide_address_family($6.dst.host, &r.af);
961 expand_rule(&r, $3, NULL, $5, $6.src_os,
962 $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
966 | RDRANCHOR string interface af proto fromto rtable {
969 if (check_rulestate(PFCTL_STATE_NAT)) {
974 memset(&r, 0, sizeof(r));
979 decide_address_family($6.src.host, &r.af);
980 decide_address_family($6.dst.host, &r.af);
982 if ($6.src.port != NULL) {
983 yyerror("source port parameter not supported"
987 if ($6.dst.port != NULL) {
988 if ($6.dst.port->next != NULL) {
989 yyerror("destination port list "
990 "expansion not supported in "
993 } else if ($6.dst.port->op != PF_OP_EQ) {
994 yyerror("destination port operators"
995 " not supported in rdr-anchor");
998 r.dst.port[0] = $6.dst.port->port[0];
999 r.dst.port[1] = $6.dst.port->port[1];
1000 r.dst.port_op = $6.dst.port->op;
1003 expand_rule(&r, $3, NULL, $5, $6.src_os,
1004 $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
1008 | BINATANCHOR string interface af proto fromto rtable {
1009 struct pfctl_rule r;
1011 if (check_rulestate(PFCTL_STATE_NAT)) {
1016 memset(&r, 0, sizeof(r));
1017 r.action = PF_BINAT;
1021 if ($5->next != NULL) {
1022 yyerror("proto list expansion"
1023 " not supported in binat-anchor");
1026 r.proto = $5->proto;
1030 if ($6.src.host != NULL || $6.src.port != NULL ||
1031 $6.dst.host != NULL || $6.dst.port != NULL) {
1032 yyerror("fromto parameter not supported"
1033 " in binat-anchor");
1037 decide_address_family($6.src.host, &r.af);
1038 decide_address_family($6.dst.host, &r.af);
1040 pfctl_append_rule(pf, &r, $2);
1045 loadrule : LOAD ANCHOR string FROM string {
1046 struct loadanchors *loadanchor;
1048 if (strlen(pf->anchor->name) + 1 +
1049 strlen($3) >= MAXPATHLEN) {
1050 yyerror("anchorname %s too long, max %u\n",
1051 $3, MAXPATHLEN - 1);
1055 loadanchor = calloc(1, sizeof(struct loadanchors));
1056 if (loadanchor == NULL)
1057 err(1, "loadrule: calloc");
1058 if ((loadanchor->anchorname = malloc(MAXPATHLEN)) ==
1060 err(1, "loadrule: malloc");
1061 if (pf->anchor->name[0])
1062 snprintf(loadanchor->anchorname, MAXPATHLEN,
1063 "%s/%s", pf->anchor->name, $3);
1065 strlcpy(loadanchor->anchorname, $3, MAXPATHLEN);
1066 if ((loadanchor->filename = strdup($5)) == NULL)
1067 err(1, "loadrule: strdup");
1069 TAILQ_INSERT_TAIL(&loadanchorshead, loadanchor,
1076 scrubaction : no SCRUB {
1085 scrubrule : scrubaction dir logquick interface af proto fromto scrub_opts
1087 struct pfctl_rule r;
1089 if (check_rulestate(PFCTL_STATE_SCRUB))
1092 memset(&r, 0, sizeof(r));
1100 yyerror("scrub rules do not support 'quick'");
1106 r.rule_flag |= PFRULE_NODF;
1108 r.rule_flag |= PFRULE_RANDOMID;
1109 if ($8.reassemble_tcp) {
1110 if (r.direction != PF_INOUT) {
1111 yyerror("reassemble tcp rules can not "
1112 "specify direction");
1115 r.rule_flag |= PFRULE_REASSEMBLE_TCP;
1118 r.min_ttl = $8.minttl;
1120 r.max_mss = $8.maxmss;
1121 if ($8.marker & SOM_SETTOS) {
1122 r.rule_flag |= PFRULE_SET_TOS;
1123 r.set_tos = $8.settos;
1126 r.rule_flag |= $8.fragcache;
1128 if (strlcpy(r.match_tagname, $8.match_tag,
1129 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1130 yyerror("tag too long, max %u chars",
1131 PF_TAG_NAME_SIZE - 1);
1134 r.match_tag_not = $8.match_tag_not;
1135 r.rtableid = $8.rtableid;
1137 expand_rule(&r, $4, NULL, $6, $7.src_os,
1138 $7.src.host, $7.src.port, $7.dst.host, $7.dst.port,
1139 NULL, NULL, NULL, "");
1144 bzero(&scrub_opts, sizeof scrub_opts);
1145 scrub_opts.rtableid = -1;
1148 { $$ = scrub_opts; }
1150 bzero(&scrub_opts, sizeof scrub_opts);
1151 scrub_opts.rtableid = -1;
1156 scrub_opts_l : scrub_opts_l scrub_opt
1161 if (scrub_opts.nodf) {
1162 yyerror("no-df cannot be respecified");
1165 scrub_opts.nodf = 1;
1168 if (scrub_opts.marker & SOM_MINTTL) {
1169 yyerror("min-ttl cannot be respecified");
1172 if ($2 < 0 || $2 > 255) {
1173 yyerror("illegal min-ttl value %d", $2);
1176 scrub_opts.marker |= SOM_MINTTL;
1177 scrub_opts.minttl = $2;
1180 if (scrub_opts.marker & SOM_MAXMSS) {
1181 yyerror("max-mss cannot be respecified");
1184 if ($2 < 0 || $2 > 65535) {
1185 yyerror("illegal max-mss value %d", $2);
1188 scrub_opts.marker |= SOM_MAXMSS;
1189 scrub_opts.maxmss = $2;
1192 if (scrub_opts.marker & SOM_SETTOS) {
1193 yyerror("set-tos cannot be respecified");
1196 scrub_opts.marker |= SOM_SETTOS;
1197 scrub_opts.settos = $2;
1200 if (scrub_opts.marker & SOM_FRAGCACHE) {
1201 yyerror("fragcache cannot be respecified");
1204 scrub_opts.marker |= SOM_FRAGCACHE;
1205 scrub_opts.fragcache = $1;
1207 | REASSEMBLE STRING {
1208 if (strcasecmp($2, "tcp") != 0) {
1209 yyerror("scrub reassemble supports only tcp, "
1215 if (scrub_opts.reassemble_tcp) {
1216 yyerror("reassemble tcp cannot be respecified");
1219 scrub_opts.reassemble_tcp = 1;
1222 if (scrub_opts.randomid) {
1223 yyerror("random-id cannot be respecified");
1226 scrub_opts.randomid = 1;
1229 if ($2 < 0 || $2 > rt_tableid_max()) {
1230 yyerror("invalid rtable id");
1233 scrub_opts.rtableid = $2;
1235 | not TAGGED string {
1236 scrub_opts.match_tag = $3;
1237 scrub_opts.match_tag_not = $1;
1241 fragcache : FRAGMENT REASSEMBLE { $$ = 0; /* default */ }
1242 | FRAGMENT FRAGCROP { $$ = 0; }
1243 | FRAGMENT FRAGDROP { $$ = 0; }
1246 antispoof : ANTISPOOF logquick antispoof_ifspc af antispoof_opts {
1247 struct pfctl_rule r;
1248 struct node_host *h = NULL, *hh;
1249 struct node_if *i, *j;
1251 if (check_rulestate(PFCTL_STATE_FILTER))
1254 for (i = $3; i; i = i->next) {
1255 bzero(&r, sizeof(r));
1258 r.direction = PF_IN;
1263 if (rule_label(&r, $5.label))
1265 r.rtableid = $5.rtableid;
1266 j = calloc(1, sizeof(struct node_if));
1268 err(1, "antispoof: calloc");
1269 if (strlcpy(j->ifname, i->ifname,
1270 sizeof(j->ifname)) >= sizeof(j->ifname)) {
1272 yyerror("interface name too long");
1277 h = calloc(1, sizeof(*h));
1279 err(1, "address: calloc");
1280 h->addr.type = PF_ADDR_DYNIFTL;
1282 if (strlcpy(h->addr.v.ifname, i->ifname,
1283 sizeof(h->addr.v.ifname)) >=
1284 sizeof(h->addr.v.ifname)) {
1287 "interface name too long");
1290 hh = malloc(sizeof(*hh));
1292 err(1, "address: malloc");
1293 bcopy(h, hh, sizeof(*hh));
1294 h->addr.iflags = PFI_AFLAG_NETWORK;
1296 h = ifa_lookup(j->ifname,
1302 expand_rule(&r, j, NULL, NULL, NULL, h,
1303 NULL, NULL, NULL, NULL, NULL,
1306 if ((i->ifa_flags & IFF_LOOPBACK) == 0) {
1307 bzero(&r, sizeof(r));
1310 r.direction = PF_IN;
1315 if (rule_label(&r, $5.label))
1317 r.rtableid = $5.rtableid;
1321 h = ifa_lookup(i->ifname, 0);
1323 expand_rule(&r, NULL, NULL,
1324 NULL, NULL, h, NULL, NULL,
1325 NULL, NULL, NULL, NULL, "");
1329 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++)
1334 antispoof_ifspc : FOR antispoof_if { $$ = $2; }
1335 | FOR '{' optnl antispoof_iflst '}' { $$ = $4; }
1338 antispoof_iflst : antispoof_if optnl { $$ = $1; }
1339 | antispoof_iflst comma antispoof_if optnl {
1340 $1->tail->next = $3;
1346 antispoof_if : if_item { $$ = $1; }
1354 bzero(&antispoof_opts, sizeof antispoof_opts);
1355 antispoof_opts.rtableid = -1;
1358 { $$ = antispoof_opts; }
1360 bzero(&antispoof_opts, sizeof antispoof_opts);
1361 antispoof_opts.rtableid = -1;
1362 $$ = antispoof_opts;
1366 antispoof_opts_l : antispoof_opts_l antispoof_opt
1370 antispoof_opt : label {
1371 if (antispoof_opts.labelcount >= PF_RULE_MAX_LABEL_COUNT) {
1372 yyerror("label can only be used %d times", PF_RULE_MAX_LABEL_COUNT);
1375 antispoof_opts.label[antispoof_opts.labelcount++] = $1;
1378 if ($2 < 0 || $2 > rt_tableid_max()) {
1379 yyerror("invalid rtable id");
1382 antispoof_opts.rtableid = $2;
1386 not : '!' { $$ = 1; }
1387 | /* empty */ { $$ = 0; }
1390 tabledef : TABLE '<' STRING '>' table_opts {
1391 struct node_host *h, *nh;
1392 struct node_tinit *ti, *nti;
1394 if (strlen($3) >= PF_TABLE_NAME_SIZE) {
1395 yyerror("table name too long, max %d chars",
1396 PF_TABLE_NAME_SIZE - 1);
1400 if (pf->loadopt & PFCTL_FLAG_TABLE)
1401 if (process_tabledef($3, &$5)) {
1406 for (ti = SIMPLEQ_FIRST(&$5.init_nodes);
1407 ti != SIMPLEQ_END(&$5.init_nodes); ti = nti) {
1410 for (h = ti->host; h != NULL; h = nh) {
1414 nti = SIMPLEQ_NEXT(ti, entries);
1421 bzero(&table_opts, sizeof table_opts);
1422 SIMPLEQ_INIT(&table_opts.init_nodes);
1425 { $$ = table_opts; }
1428 bzero(&table_opts, sizeof table_opts);
1429 SIMPLEQ_INIT(&table_opts.init_nodes);
1434 table_opts_l : table_opts_l table_opt
1438 table_opt : STRING {
1439 if (!strcmp($1, "const"))
1440 table_opts.flags |= PFR_TFLAG_CONST;
1441 else if (!strcmp($1, "persist"))
1442 table_opts.flags |= PFR_TFLAG_PERSIST;
1443 else if (!strcmp($1, "counters"))
1444 table_opts.flags |= PFR_TFLAG_COUNTERS;
1446 yyerror("invalid table option '%s'", $1);
1452 | '{' optnl '}' { table_opts.init_addr = 1; }
1453 | '{' optnl host_list '}' {
1454 struct node_host *n;
1455 struct node_tinit *ti;
1457 for (n = $3; n != NULL; n = n->next) {
1458 switch (n->addr.type) {
1459 case PF_ADDR_ADDRMASK:
1462 yyerror("address ranges are not "
1463 "permitted inside tables");
1465 case PF_ADDR_DYNIFTL:
1466 yyerror("dynamic addresses are not "
1467 "permitted inside tables");
1470 yyerror("tables cannot contain tables");
1472 case PF_ADDR_NOROUTE:
1473 yyerror("\"no-route\" is not permitted "
1476 case PF_ADDR_URPFFAILED:
1477 yyerror("\"urpf-failed\" is not "
1478 "permitted inside tables");
1481 yyerror("unknown address type %d",
1486 if (!(ti = calloc(1, sizeof(*ti))))
1487 err(1, "table_opt: calloc");
1489 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1491 table_opts.init_addr = 1;
1494 struct node_tinit *ti;
1496 if (!(ti = calloc(1, sizeof(*ti))))
1497 err(1, "table_opt: calloc");
1499 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1501 table_opts.init_addr = 1;
1505 altqif : ALTQ interface queue_opts QUEUE qassign {
1508 if (check_rulestate(PFCTL_STATE_QUEUE))
1511 memset(&a, 0, sizeof(a));
1512 if ($3.scheduler.qtype == ALTQT_NONE) {
1513 yyerror("no scheduler specified!");
1516 a.scheduler = $3.scheduler.qtype;
1517 a.qlimit = $3.qlimit;
1518 a.tbrsize = $3.tbrsize;
1519 if ($5 == NULL && $3.scheduler.qtype != ALTQT_CODEL) {
1520 yyerror("no child queues specified");
1523 if (expand_altq(&a, $2, $5, $3.queue_bwspec,
1529 queuespec : QUEUE STRING interface queue_opts qassign {
1532 if (check_rulestate(PFCTL_STATE_QUEUE)) {
1537 memset(&a, 0, sizeof(a));
1539 if (strlcpy(a.qname, $2, sizeof(a.qname)) >=
1541 yyerror("queue name too long (max "
1542 "%d chars)", PF_QNAME_SIZE-1);
1548 yyerror("cannot specify tbrsize for queue");
1551 if ($4.priority > 255) {
1552 yyerror("priority out of range: max 255");
1555 a.priority = $4.priority;
1556 a.qlimit = $4.qlimit;
1557 a.scheduler = $4.scheduler.qtype;
1558 if (expand_queue(&a, $3, $5, $4.queue_bwspec,
1560 yyerror("errors in queue definition");
1567 bzero(&queue_opts, sizeof queue_opts);
1568 queue_opts.priority = DEFAULT_PRIORITY;
1569 queue_opts.qlimit = DEFAULT_QLIMIT;
1570 queue_opts.scheduler.qtype = ALTQT_NONE;
1571 queue_opts.queue_bwspec.bw_percent = 100;
1574 { $$ = queue_opts; }
1576 bzero(&queue_opts, sizeof queue_opts);
1577 queue_opts.priority = DEFAULT_PRIORITY;
1578 queue_opts.qlimit = DEFAULT_QLIMIT;
1579 queue_opts.scheduler.qtype = ALTQT_NONE;
1580 queue_opts.queue_bwspec.bw_percent = 100;
1585 queue_opts_l : queue_opts_l queue_opt
1589 queue_opt : BANDWIDTH bandwidth {
1590 if (queue_opts.marker & QOM_BWSPEC) {
1591 yyerror("bandwidth cannot be respecified");
1594 queue_opts.marker |= QOM_BWSPEC;
1595 queue_opts.queue_bwspec = $2;
1598 if (queue_opts.marker & QOM_PRIORITY) {
1599 yyerror("priority cannot be respecified");
1602 if ($2 < 0 || $2 > 255) {
1603 yyerror("priority out of range: max 255");
1606 queue_opts.marker |= QOM_PRIORITY;
1607 queue_opts.priority = $2;
1610 if (queue_opts.marker & QOM_QLIMIT) {
1611 yyerror("qlimit cannot be respecified");
1614 if ($2 < 0 || $2 > 65535) {
1615 yyerror("qlimit out of range: max 65535");
1618 queue_opts.marker |= QOM_QLIMIT;
1619 queue_opts.qlimit = $2;
1622 if (queue_opts.marker & QOM_SCHEDULER) {
1623 yyerror("scheduler cannot be respecified");
1626 queue_opts.marker |= QOM_SCHEDULER;
1627 queue_opts.scheduler = $1;
1630 if (queue_opts.marker & QOM_TBRSIZE) {
1631 yyerror("tbrsize cannot be respecified");
1634 if ($2 < 0 || $2 > UINT_MAX) {
1635 yyerror("tbrsize too big: max %u", UINT_MAX);
1638 queue_opts.marker |= QOM_TBRSIZE;
1639 queue_opts.tbrsize = $2;
1643 bandwidth : STRING {
1649 bps = strtod($1, &cp);
1651 if (strlen(cp) > 1) {
1653 if (!strcmp(cu, "Bit") ||
1655 !strcmp(cu, "bit") ||
1660 if (!strcmp(cp, "b"))
1662 else if (!strcmp(cp, "K"))
1664 else if (!strcmp(cp, "M"))
1666 else if (!strcmp(cp, "G"))
1667 bps *= 1000 * 1000 * 1000;
1668 else if (!strcmp(cp, "%")) {
1669 if (bps < 0 || bps > 100) {
1670 yyerror("bandwidth spec "
1675 $$.bw_percent = bps;
1678 yyerror("unknown unit %s", cp);
1684 $$.bw_absolute = (u_int64_t)bps;
1687 if ($1 < 0 || $1 >= LLONG_MAX) {
1688 yyerror("bandwidth number too big");
1692 $$.bw_absolute = $1;
1697 $$.qtype = ALTQT_CBQ;
1698 $$.data.cbq_opts.flags = 0;
1700 | CBQ '(' cbqflags_list ')' {
1701 $$.qtype = ALTQT_CBQ;
1702 $$.data.cbq_opts.flags = $3;
1705 $$.qtype = ALTQT_PRIQ;
1706 $$.data.priq_opts.flags = 0;
1708 | PRIQ '(' priqflags_list ')' {
1709 $$.qtype = ALTQT_PRIQ;
1710 $$.data.priq_opts.flags = $3;
1713 $$.qtype = ALTQT_HFSC;
1714 bzero(&$$.data.hfsc_opts,
1715 sizeof(struct node_hfsc_opts));
1717 | HFSC '(' hfsc_opts ')' {
1718 $$.qtype = ALTQT_HFSC;
1719 $$.data.hfsc_opts = $3;
1722 $$.qtype = ALTQT_FAIRQ;
1723 bzero(&$$.data.fairq_opts,
1724 sizeof(struct node_fairq_opts));
1726 | FAIRQ '(' fairq_opts ')' {
1727 $$.qtype = ALTQT_FAIRQ;
1728 $$.data.fairq_opts = $3;
1731 $$.qtype = ALTQT_CODEL;
1732 bzero(&$$.data.codel_opts,
1733 sizeof(struct codel_opts));
1735 | CODEL '(' codel_opts ')' {
1736 $$.qtype = ALTQT_CODEL;
1737 $$.data.codel_opts = $3;
1741 cbqflags_list : cbqflags_item { $$ |= $1; }
1742 | cbqflags_list comma cbqflags_item { $$ |= $3; }
1745 cbqflags_item : STRING {
1746 if (!strcmp($1, "default"))
1747 $$ = CBQCLF_DEFCLASS;
1748 else if (!strcmp($1, "borrow"))
1750 else if (!strcmp($1, "red"))
1752 else if (!strcmp($1, "ecn"))
1753 $$ = CBQCLF_RED|CBQCLF_ECN;
1754 else if (!strcmp($1, "rio"))
1756 else if (!strcmp($1, "codel"))
1759 yyerror("unknown cbq flag \"%s\"", $1);
1767 priqflags_list : priqflags_item { $$ |= $1; }
1768 | priqflags_list comma priqflags_item { $$ |= $3; }
1771 priqflags_item : STRING {
1772 if (!strcmp($1, "default"))
1773 $$ = PRCF_DEFAULTCLASS;
1774 else if (!strcmp($1, "red"))
1776 else if (!strcmp($1, "ecn"))
1777 $$ = PRCF_RED|PRCF_ECN;
1778 else if (!strcmp($1, "rio"))
1780 else if (!strcmp($1, "codel"))
1783 yyerror("unknown priq flag \"%s\"", $1);
1793 sizeof(struct node_hfsc_opts));
1800 hfscopts_list : hfscopts_item
1801 | hfscopts_list comma hfscopts_item
1804 hfscopts_item : LINKSHARE bandwidth {
1805 if (hfsc_opts.linkshare.used) {
1806 yyerror("linkshare already specified");
1809 hfsc_opts.linkshare.m2 = $2;
1810 hfsc_opts.linkshare.used = 1;
1812 | LINKSHARE '(' bandwidth comma NUMBER comma bandwidth ')'
1814 if ($5 < 0 || $5 > INT_MAX) {
1815 yyerror("timing in curve out of range");
1818 if (hfsc_opts.linkshare.used) {
1819 yyerror("linkshare already specified");
1822 hfsc_opts.linkshare.m1 = $3;
1823 hfsc_opts.linkshare.d = $5;
1824 hfsc_opts.linkshare.m2 = $7;
1825 hfsc_opts.linkshare.used = 1;
1827 | REALTIME bandwidth {
1828 if (hfsc_opts.realtime.used) {
1829 yyerror("realtime already specified");
1832 hfsc_opts.realtime.m2 = $2;
1833 hfsc_opts.realtime.used = 1;
1835 | REALTIME '(' bandwidth comma NUMBER comma bandwidth ')'
1837 if ($5 < 0 || $5 > INT_MAX) {
1838 yyerror("timing in curve out of range");
1841 if (hfsc_opts.realtime.used) {
1842 yyerror("realtime already specified");
1845 hfsc_opts.realtime.m1 = $3;
1846 hfsc_opts.realtime.d = $5;
1847 hfsc_opts.realtime.m2 = $7;
1848 hfsc_opts.realtime.used = 1;
1850 | UPPERLIMIT bandwidth {
1851 if (hfsc_opts.upperlimit.used) {
1852 yyerror("upperlimit already specified");
1855 hfsc_opts.upperlimit.m2 = $2;
1856 hfsc_opts.upperlimit.used = 1;
1858 | UPPERLIMIT '(' bandwidth comma NUMBER comma bandwidth ')'
1860 if ($5 < 0 || $5 > INT_MAX) {
1861 yyerror("timing in curve out of range");
1864 if (hfsc_opts.upperlimit.used) {
1865 yyerror("upperlimit already specified");
1868 hfsc_opts.upperlimit.m1 = $3;
1869 hfsc_opts.upperlimit.d = $5;
1870 hfsc_opts.upperlimit.m2 = $7;
1871 hfsc_opts.upperlimit.used = 1;
1874 if (!strcmp($1, "default"))
1875 hfsc_opts.flags |= HFCF_DEFAULTCLASS;
1876 else if (!strcmp($1, "red"))
1877 hfsc_opts.flags |= HFCF_RED;
1878 else if (!strcmp($1, "ecn"))
1879 hfsc_opts.flags |= HFCF_RED|HFCF_ECN;
1880 else if (!strcmp($1, "rio"))
1881 hfsc_opts.flags |= HFCF_RIO;
1882 else if (!strcmp($1, "codel"))
1883 hfsc_opts.flags |= HFCF_CODEL;
1885 yyerror("unknown hfsc flag \"%s\"", $1);
1895 sizeof(struct node_fairq_opts));
1902 fairqopts_list : fairqopts_item
1903 | fairqopts_list comma fairqopts_item
1906 fairqopts_item : LINKSHARE bandwidth {
1907 if (fairq_opts.linkshare.used) {
1908 yyerror("linkshare already specified");
1911 fairq_opts.linkshare.m2 = $2;
1912 fairq_opts.linkshare.used = 1;
1914 | LINKSHARE '(' bandwidth number bandwidth ')' {
1915 if (fairq_opts.linkshare.used) {
1916 yyerror("linkshare already specified");
1919 fairq_opts.linkshare.m1 = $3;
1920 fairq_opts.linkshare.d = $4;
1921 fairq_opts.linkshare.m2 = $5;
1922 fairq_opts.linkshare.used = 1;
1925 fairq_opts.hogs_bw = $2;
1928 fairq_opts.nbuckets = $2;
1931 if (!strcmp($1, "default"))
1932 fairq_opts.flags |= FARF_DEFAULTCLASS;
1933 else if (!strcmp($1, "red"))
1934 fairq_opts.flags |= FARF_RED;
1935 else if (!strcmp($1, "ecn"))
1936 fairq_opts.flags |= FARF_RED|FARF_ECN;
1937 else if (!strcmp($1, "rio"))
1938 fairq_opts.flags |= FARF_RIO;
1939 else if (!strcmp($1, "codel"))
1940 fairq_opts.flags |= FARF_CODEL;
1942 yyerror("unknown fairq flag \"%s\"", $1);
1952 sizeof(struct codel_opts));
1959 codelopts_list : codelopts_item
1960 | codelopts_list comma codelopts_item
1963 codelopts_item : INTERVAL number {
1964 if (codel_opts.interval) {
1965 yyerror("interval already specified");
1968 codel_opts.interval = $2;
1971 if (codel_opts.target) {
1972 yyerror("target already specified");
1975 codel_opts.target = $2;
1978 if (!strcmp($1, "ecn"))
1981 yyerror("unknown codel option \"%s\"", $1);
1989 qassign : /* empty */ { $$ = NULL; }
1990 | qassign_item { $$ = $1; }
1991 | '{' optnl qassign_list '}' { $$ = $3; }
1994 qassign_list : qassign_item optnl { $$ = $1; }
1995 | qassign_list comma qassign_item optnl {
1996 $1->tail->next = $3;
2002 qassign_item : STRING {
2003 $$ = calloc(1, sizeof(struct node_queue));
2005 err(1, "qassign_item: calloc");
2006 if (strlcpy($$->queue, $1, sizeof($$->queue)) >=
2007 sizeof($$->queue)) {
2008 yyerror("queue name '%s' too long (max "
2009 "%d chars)", $1, sizeof($$->queue)-1);
2020 pfrule : action dir logquick interface route af proto fromto
2023 struct pfctl_rule r;
2024 struct node_state_opt *o;
2025 struct node_proto *proto;
2031 if (check_rulestate(PFCTL_STATE_FILTER))
2034 memset(&r, 0, sizeof(r));
2038 case PFRULE_RETURNRST:
2039 r.rule_flag |= PFRULE_RETURNRST;
2040 r.return_ttl = $1.w;
2042 case PFRULE_RETURNICMP:
2043 r.rule_flag |= PFRULE_RETURNICMP;
2044 r.return_icmp = $1.w;
2045 r.return_icmp6 = $1.w2;
2048 r.rule_flag |= PFRULE_RETURN;
2049 r.return_icmp = $1.w;
2050 r.return_icmp6 = $1.w2;
2058 r.rtableid = $9.rtableid;
2060 if ($9.marker & FOM_PRIO) {
2062 r.prio = PF_PRIO_ZERO;
2066 if ($9.marker & FOM_SETPRIO) {
2067 r.set_prio[0] = $9.set_prio[0];
2068 r.set_prio[1] = $9.set_prio[1];
2069 r.scrub_flags |= PFSTATE_SETPRIO;
2074 if (strlcpy(r.tagname, $9.tag,
2075 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
2076 yyerror("tag too long, max %u chars",
2077 PF_TAG_NAME_SIZE - 1);
2081 if (strlcpy(r.match_tagname, $9.match_tag,
2082 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
2083 yyerror("tag too long, max %u chars",
2084 PF_TAG_NAME_SIZE - 1);
2087 r.match_tag_not = $9.match_tag_not;
2088 if (rule_label(&r, $9.label))
2090 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++)
2092 r.flags = $9.flags.b1;
2093 r.flagset = $9.flags.b2;
2094 if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
2095 yyerror("flags always false");
2098 if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
2099 for (proto = $7; proto != NULL &&
2100 proto->proto != IPPROTO_TCP;
2101 proto = proto->next)
2103 if (proto == NULL && $7 != NULL) {
2104 if ($9.flags.b1 || $9.flags.b2)
2106 "flags only apply to tcp");
2109 "OS fingerprinting only "
2114 if (($9.flags.b1 & parse_flags("S")) == 0 &&
2116 yyerror("OS fingerprinting requires "
2117 "the SYN TCP flag (flags S/SA)");
2124 r.keep_state = $9.keep.action;
2125 o = $9.keep.options;
2127 /* 'keep state' by default on pass rules. */
2128 if (!r.keep_state && !r.action &&
2129 !($9.marker & FOM_KEEP)) {
2130 r.keep_state = PF_STATE_NORMAL;
2131 o = keep_state_defaults;
2136 struct node_state_opt *p = o;
2139 case PF_STATE_OPT_MAX:
2141 yyerror("state option 'max' "
2142 "multiple definitions");
2145 r.max_states = o->data.max_states;
2147 case PF_STATE_OPT_NOSYNC:
2148 if (r.rule_flag & PFRULE_NOSYNC) {
2149 yyerror("state option 'sync' "
2150 "multiple definitions");
2153 r.rule_flag |= PFRULE_NOSYNC;
2155 case PF_STATE_OPT_SRCTRACK:
2157 yyerror("state option "
2159 "multiple definitions");
2162 srctrack = o->data.src_track;
2163 r.rule_flag |= PFRULE_SRCTRACK;
2165 case PF_STATE_OPT_MAX_SRC_STATES:
2166 if (r.max_src_states) {
2167 yyerror("state option "
2169 "multiple definitions");
2172 if (o->data.max_src_states == 0) {
2173 yyerror("'max-src-states' must "
2178 o->data.max_src_states;
2179 r.rule_flag |= PFRULE_SRCTRACK;
2181 case PF_STATE_OPT_OVERLOAD:
2182 if (r.overload_tblname[0]) {
2183 yyerror("multiple 'overload' "
2184 "table definitions");
2187 if (strlcpy(r.overload_tblname,
2188 o->data.overload.tblname,
2189 PF_TABLE_NAME_SIZE) >=
2190 PF_TABLE_NAME_SIZE) {
2191 yyerror("state option: "
2195 r.flush = o->data.overload.flush;
2197 case PF_STATE_OPT_MAX_SRC_CONN:
2198 if (r.max_src_conn) {
2199 yyerror("state option "
2201 "multiple definitions");
2204 if (o->data.max_src_conn == 0) {
2205 yyerror("'max-src-conn' "
2210 o->data.max_src_conn;
2211 r.rule_flag |= PFRULE_SRCTRACK |
2212 PFRULE_RULESRCTRACK;
2214 case PF_STATE_OPT_MAX_SRC_CONN_RATE:
2215 if (r.max_src_conn_rate.limit) {
2216 yyerror("state option "
2217 "'max-src-conn-rate' "
2218 "multiple definitions");
2221 if (!o->data.max_src_conn_rate.limit ||
2222 !o->data.max_src_conn_rate.seconds) {
2223 yyerror("'max-src-conn-rate' "
2224 "values must be > 0");
2227 if (o->data.max_src_conn_rate.limit >
2229 yyerror("'max-src-conn-rate' "
2230 "maximum rate must be < %u",
2234 r.max_src_conn_rate.limit =
2235 o->data.max_src_conn_rate.limit;
2236 r.max_src_conn_rate.seconds =
2237 o->data.max_src_conn_rate.seconds;
2238 r.rule_flag |= PFRULE_SRCTRACK |
2239 PFRULE_RULESRCTRACK;
2241 case PF_STATE_OPT_MAX_SRC_NODES:
2242 if (r.max_src_nodes) {
2243 yyerror("state option "
2245 "multiple definitions");
2248 if (o->data.max_src_nodes == 0) {
2249 yyerror("'max-src-nodes' must "
2254 o->data.max_src_nodes;
2255 r.rule_flag |= PFRULE_SRCTRACK |
2256 PFRULE_RULESRCTRACK;
2258 case PF_STATE_OPT_STATELOCK:
2260 yyerror("state locking option: "
2261 "multiple definitions");
2265 r.rule_flag |= o->data.statelock;
2267 case PF_STATE_OPT_SLOPPY:
2268 if (r.rule_flag & PFRULE_STATESLOPPY) {
2269 yyerror("state sloppy option: "
2270 "multiple definitions");
2273 r.rule_flag |= PFRULE_STATESLOPPY;
2275 case PF_STATE_OPT_TIMEOUT:
2276 if (o->data.timeout.number ==
2277 PFTM_ADAPTIVE_START ||
2278 o->data.timeout.number ==
2281 if (r.timeout[o->data.timeout.number]) {
2282 yyerror("state timeout %s "
2283 "multiple definitions",
2284 pf_timeouts[o->data.
2285 timeout.number].name);
2288 r.timeout[o->data.timeout.number] =
2289 o->data.timeout.seconds;
2296 /* 'flags S/SA' by default on stateful rules */
2297 if (!r.action && !r.flags && !r.flagset &&
2298 !$9.fragment && !($9.marker & FOM_FLAGS) &&
2300 r.flags = parse_flags("S");
2301 r.flagset = parse_flags("SA");
2303 if (!adaptive && r.max_states) {
2304 r.timeout[PFTM_ADAPTIVE_START] =
2305 (r.max_states / 10) * 6;
2306 r.timeout[PFTM_ADAPTIVE_END] =
2307 (r.max_states / 10) * 12;
2309 if (r.rule_flag & PFRULE_SRCTRACK) {
2310 if (srctrack == PF_SRCTRACK_GLOBAL &&
2312 yyerror("'max-src-nodes' is "
2313 "incompatible with "
2314 "'source-track global'");
2317 if (srctrack == PF_SRCTRACK_GLOBAL &&
2319 yyerror("'max-src-conn' is "
2320 "incompatible with "
2321 "'source-track global'");
2324 if (srctrack == PF_SRCTRACK_GLOBAL &&
2325 r.max_src_conn_rate.seconds) {
2326 yyerror("'max-src-conn-rate' is "
2327 "incompatible with "
2328 "'source-track global'");
2331 if (r.timeout[PFTM_SRC_NODE] <
2332 r.max_src_conn_rate.seconds)
2333 r.timeout[PFTM_SRC_NODE] =
2334 r.max_src_conn_rate.seconds;
2335 r.rule_flag |= PFRULE_SRCTRACK;
2336 if (srctrack == PF_SRCTRACK_RULE)
2337 r.rule_flag |= PFRULE_RULESRCTRACK;
2339 if (r.keep_state && !statelock)
2340 r.rule_flag |= default_statelock;
2343 r.rule_flag |= PFRULE_FRAGMENT;
2344 r.allow_opts = $9.allowopts;
2346 decide_address_family($8.src.host, &r.af);
2347 decide_address_family($8.dst.host, &r.af);
2351 yyerror("direction must be explicit "
2352 "with rules that specify routing");
2356 r.rpool.opts = $5.pool_opts;
2358 memcpy(&r.rpool.key, $5.key,
2359 sizeof(struct pf_poolhashkey));
2362 decide_address_family($5.host, &r.af);
2363 remove_invalid_hosts(&$5.host, &r.af);
2364 if ($5.host == NULL) {
2365 yyerror("no routing address with "
2366 "matching address family found.");
2369 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
2370 PF_POOL_NONE && ($5.host->next != NULL ||
2371 $5.host->addr.type == PF_ADDR_TABLE ||
2372 DYNIF_MULTIADDR($5.host->addr)))
2373 r.rpool.opts |= PF_POOL_ROUNDROBIN;
2374 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2375 PF_POOL_ROUNDROBIN &&
2376 disallow_table($5.host, "tables are only "
2377 "supported in round-robin routing pools"))
2379 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2380 PF_POOL_ROUNDROBIN &&
2381 disallow_alias($5.host, "interface (%s) "
2382 "is only supported in round-robin "
2385 if ($5.host->next != NULL) {
2386 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2387 PF_POOL_ROUNDROBIN) {
2388 yyerror("r.rpool.opts must "
2389 "be PF_POOL_ROUNDROBIN");
2394 if ($9.queues.qname != NULL) {
2395 if (strlcpy(r.qname, $9.queues.qname,
2396 sizeof(r.qname)) >= sizeof(r.qname)) {
2397 yyerror("rule qname too long (max "
2398 "%d chars)", sizeof(r.qname)-1);
2401 free($9.queues.qname);
2403 if ($9.queues.pqname != NULL) {
2404 if (strlcpy(r.pqname, $9.queues.pqname,
2405 sizeof(r.pqname)) >= sizeof(r.pqname)) {
2406 yyerror("rule pqname too long (max "
2407 "%d chars)", sizeof(r.pqname)-1);
2410 free($9.queues.pqname);
2413 r.divert.port = $9.divert.port;
2415 if ((r.divert.port = $9.divert.port)) {
2416 if (r.direction == PF_OUT) {
2417 if ($9.divert.addr) {
2418 yyerror("address specified "
2419 "for outgoing divert");
2422 bzero(&r.divert.addr,
2423 sizeof(r.divert.addr));
2425 if (!$9.divert.addr) {
2426 yyerror("no address specified "
2427 "for incoming divert");
2430 if ($9.divert.addr->af != r.af) {
2431 yyerror("address family "
2432 "mismatch for divert");
2436 $9.divert.addr->addr.v.a.addr;
2441 expand_rule(&r, $4, $5.host, $7, $8.src_os,
2442 $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
2443 $9.uid, $9.gid, $9.icmpspec, "");
2448 bzero(&filter_opts, sizeof filter_opts);
2449 filter_opts.rtableid = -1;
2452 { $$ = filter_opts; }
2454 bzero(&filter_opts, sizeof filter_opts);
2455 filter_opts.rtableid = -1;
2460 filter_opts_l : filter_opts_l filter_opt
2464 filter_opt : USER uids {
2465 if (filter_opts.uid)
2466 $2->tail->next = filter_opts.uid;
2467 filter_opts.uid = $2;
2470 if (filter_opts.gid)
2471 $2->tail->next = filter_opts.gid;
2472 filter_opts.gid = $2;
2475 if (filter_opts.marker & FOM_FLAGS) {
2476 yyerror("flags cannot be redefined");
2479 filter_opts.marker |= FOM_FLAGS;
2480 filter_opts.flags.b1 |= $1.b1;
2481 filter_opts.flags.b2 |= $1.b2;
2482 filter_opts.flags.w |= $1.w;
2483 filter_opts.flags.w2 |= $1.w2;
2486 if (filter_opts.marker & FOM_ICMP) {
2487 yyerror("icmp-type cannot be redefined");
2490 filter_opts.marker |= FOM_ICMP;
2491 filter_opts.icmpspec = $1;
2494 if (filter_opts.marker & FOM_PRIO) {
2495 yyerror("prio cannot be redefined");
2498 if ($2 < 0 || $2 > PF_PRIO_MAX) {
2499 yyerror("prio must be 0 - %u", PF_PRIO_MAX);
2502 filter_opts.marker |= FOM_PRIO;
2503 filter_opts.prio = $2;
2506 if (filter_opts.marker & FOM_TOS) {
2507 yyerror("tos cannot be redefined");
2510 filter_opts.marker |= FOM_TOS;
2511 filter_opts.tos = $2;
2514 if (filter_opts.marker & FOM_KEEP) {
2515 yyerror("modulate or keep cannot be redefined");
2518 filter_opts.marker |= FOM_KEEP;
2519 filter_opts.keep.action = $1.action;
2520 filter_opts.keep.options = $1.options;
2523 filter_opts.fragment = 1;
2526 filter_opts.allowopts = 1;
2529 if (filter_opts.labelcount >= PF_RULE_MAX_LABEL_COUNT) {
2530 yyerror("label can only be used %d times", PF_RULE_MAX_LABEL_COUNT);
2533 filter_opts.label[filter_opts.labelcount++] = $1;
2536 if (filter_opts.queues.qname) {
2537 yyerror("queue cannot be redefined");
2540 filter_opts.queues = $1;
2543 filter_opts.tag = $2;
2545 | not TAGGED string {
2546 filter_opts.match_tag = $3;
2547 filter_opts.match_tag_not = $1;
2549 | PROBABILITY probability {
2552 p = floor($2 * UINT_MAX + 0.5);
2553 if (p < 0.0 || p > UINT_MAX) {
2554 yyerror("invalid probability: %lf", p);
2557 filter_opts.prob = (u_int32_t)p;
2558 if (filter_opts.prob == 0)
2559 filter_opts.prob = 1;
2562 if ($2 < 0 || $2 > rt_tableid_max()) {
2563 yyerror("invalid rtable id");
2566 filter_opts.rtableid = $2;
2568 | DIVERTTO portplain {
2570 filter_opts.divert.port = $2.a;
2571 if (!filter_opts.divert.port) {
2572 yyerror("invalid divert port: %u", ntohs($2.a));
2577 | DIVERTTO STRING PORT portplain {
2579 if ((filter_opts.divert.addr = host($2)) == NULL) {
2580 yyerror("could not parse divert address: %s",
2589 filter_opts.divert.port = $4.a;
2590 if (!filter_opts.divert.port) {
2591 yyerror("invalid divert port: %u", ntohs($4.a));
2597 yyerror("divert-reply has no meaning in FreeBSD pf(4)");
2600 filter_opts.divert.port = 1; /* some random value */
2606 filter_sets : SET '(' filter_sets_l ')' { $$ = filter_opts; }
2607 | SET filter_set { $$ = filter_opts; }
2610 filter_sets_l : filter_sets_l comma filter_set
2615 if (filter_opts.marker & FOM_SETPRIO) {
2616 yyerror("prio cannot be redefined");
2619 filter_opts.marker |= FOM_SETPRIO;
2620 filter_opts.set_prio[0] = $1.b1;
2621 filter_opts.set_prio[1] = $1.b2;
2623 prio : PRIO NUMBER {
2624 if ($2 < 0 || $2 > PF_PRIO_MAX) {
2625 yyerror("prio must be 0 - %u", PF_PRIO_MAX);
2630 | PRIO '(' NUMBER comma NUMBER ')' {
2631 if ($3 < 0 || $3 > PF_PRIO_MAX ||
2632 $5 < 0 || $5 > PF_PRIO_MAX) {
2633 yyerror("prio must be 0 - %u", PF_PRIO_MAX);
2641 probability : STRING {
2643 double p = strtod($1, &e);
2650 yyerror("invalid probability: %s", $1);
2666 $$.w = returnicmpdefault;
2667 $$.w2 = returnicmp6default;
2669 | BLOCK blockspec { $$ = $2; $$.b1 = PF_DROP; }
2672 blockspec : /* empty */ {
2673 $$.b2 = blockpolicy;
2674 $$.w = returnicmpdefault;
2675 $$.w2 = returnicmp6default;
2678 $$.b2 = PFRULE_DROP;
2683 $$.b2 = PFRULE_RETURNRST;
2687 | RETURNRST '(' TTL NUMBER ')' {
2688 if ($4 < 0 || $4 > 255) {
2689 yyerror("illegal ttl value %d", $4);
2692 $$.b2 = PFRULE_RETURNRST;
2697 $$.b2 = PFRULE_RETURNICMP;
2698 $$.w = returnicmpdefault;
2699 $$.w2 = returnicmp6default;
2702 $$.b2 = PFRULE_RETURNICMP;
2703 $$.w = returnicmpdefault;
2704 $$.w2 = returnicmp6default;
2706 | RETURNICMP '(' reticmpspec ')' {
2707 $$.b2 = PFRULE_RETURNICMP;
2709 $$.w2 = returnicmpdefault;
2711 | RETURNICMP6 '(' reticmp6spec ')' {
2712 $$.b2 = PFRULE_RETURNICMP;
2713 $$.w = returnicmpdefault;
2716 | RETURNICMP '(' reticmpspec comma reticmp6spec ')' {
2717 $$.b2 = PFRULE_RETURNICMP;
2722 $$.b2 = PFRULE_RETURN;
2723 $$.w = returnicmpdefault;
2724 $$.w2 = returnicmp6default;
2728 reticmpspec : STRING {
2729 if (!($$ = parseicmpspec($1, AF_INET))) {
2738 if ($1 < 0 || $1 > 255) {
2739 yyerror("invalid icmp code %lu", $1);
2742 icmptype = returnicmpdefault >> 8;
2743 $$ = (icmptype << 8 | $1);
2747 reticmp6spec : STRING {
2748 if (!($$ = parseicmpspec($1, AF_INET6))) {
2757 if ($1 < 0 || $1 > 255) {
2758 yyerror("invalid icmp code %lu", $1);
2761 icmptype = returnicmp6default >> 8;
2762 $$ = (icmptype << 8 | $1);
2766 dir : /* empty */ { $$ = PF_INOUT; }
2767 | IN { $$ = PF_IN; }
2768 | OUT { $$ = PF_OUT; }
2771 quick : /* empty */ { $$.quick = 0; }
2772 | QUICK { $$.quick = 1; }
2775 logquick : /* empty */ { $$.log = 0; $$.quick = 0; $$.logif = 0; }
2776 | log { $$ = $1; $$.quick = 0; }
2777 | QUICK { $$.quick = 1; $$.log = 0; $$.logif = 0; }
2778 | log QUICK { $$ = $1; $$.quick = 1; }
2779 | QUICK log { $$ = $2; $$.quick = 1; }
2782 log : LOG { $$.log = PF_LOG; $$.logif = 0; }
2783 | LOG '(' logopts ')' {
2784 $$.log = PF_LOG | $3.log;
2785 $$.logif = $3.logif;
2789 logopts : logopt { $$ = $1; }
2790 | logopts comma logopt {
2791 $$.log = $1.log | $3.log;
2792 $$.logif = $3.logif;
2794 $$.logif = $1.logif;
2798 logopt : ALL { $$.log = PF_LOG_ALL; $$.logif = 0; }
2799 | USER { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
2800 | GROUP { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
2806 if (strncmp($2, "pflog", 5)) {
2807 yyerror("%s: should be a pflog interface", $2);
2811 i = strtonum($2 + 5, 0, 255, &errstr);
2813 yyerror("%s: %s", $2, errstr);
2822 interface : /* empty */ { $$ = NULL; }
2823 | ON if_item_not { $$ = $2; }
2824 | ON '{' optnl if_list '}' { $$ = $4; }
2827 if_list : if_item_not optnl { $$ = $1; }
2828 | if_list comma if_item_not optnl {
2829 $1->tail->next = $3;
2835 if_item_not : not if_item { $$ = $2; $$->not = $1; }
2839 struct node_host *n;
2841 $$ = calloc(1, sizeof(struct node_if));
2843 err(1, "if_item: calloc");
2844 if (strlcpy($$->ifname, $1, sizeof($$->ifname)) >=
2845 sizeof($$->ifname)) {
2848 yyerror("interface name too long");
2852 if ((n = ifa_exists($1)) != NULL)
2853 $$->ifa_flags = n->ifa_flags;
2862 af : /* empty */ { $$ = 0; }
2863 | INET { $$ = AF_INET; }
2864 | INET6 { $$ = AF_INET6; }
2867 proto : /* empty */ { $$ = NULL; }
2868 | PROTO proto_item { $$ = $2; }
2869 | PROTO '{' optnl proto_list '}' { $$ = $4; }
2872 proto_list : proto_item optnl { $$ = $1; }
2873 | proto_list comma proto_item optnl {
2874 $1->tail->next = $3;
2880 proto_item : protoval {
2885 yyerror("proto 0 cannot be used");
2888 $$ = calloc(1, sizeof(struct node_proto));
2890 err(1, "proto_item: calloc");
2900 p = getprotobyname($1);
2902 yyerror("unknown protocol %s", $1);
2910 if ($1 < 0 || $1 > 255) {
2911 yyerror("protocol outside range");
2931 os : /* empty */ { $$ = NULL; }
2932 | OS xos { $$ = $2; }
2933 | OS '{' optnl os_list '}' { $$ = $4; }
2937 $$ = calloc(1, sizeof(struct node_os));
2939 err(1, "os: calloc");
2945 os_list : xos optnl { $$ = $1; }
2946 | os_list comma xos optnl {
2947 $1->tail->next = $3;
2953 from : /* empty */ {
2967 if (disallow_urpf_failed($2.host, "\"urpf-failed\" is "
2968 "not permitted in a destination address"))
2974 ipportspec : ipspec {
2978 | ipspec PORT portspec {
2992 ipspec : ANY { $$ = NULL; }
2993 | xhost { $$ = $1; }
2994 | '{' optnl host_list '}' { $$ = $3; }
2997 toipspec : TO ipspec { $$ = $2; }
2998 | /* empty */ { $$ = NULL; }
3001 host_list : ipspec optnl { $$ = $1; }
3002 | host_list comma ipspec optnl {
3005 else if ($1 == NULL)
3008 $1->tail->next = $3;
3009 $1->tail = $3->tail;
3016 struct node_host *n;
3018 for (n = $2; n != NULL; n = n->next)
3023 $$ = calloc(1, sizeof(struct node_host));
3025 err(1, "xhost: calloc");
3026 $$->addr.type = PF_ADDR_NOROUTE;
3032 $$ = calloc(1, sizeof(struct node_host));
3034 err(1, "xhost: calloc");
3035 $$->addr.type = PF_ADDR_URPFFAILED;
3043 if (($$ = host($1)) == NULL) {
3044 /* error. "any" is handled elsewhere */
3046 yyerror("could not parse host specification");
3052 | STRING '-' STRING {
3053 struct node_host *b, *e;
3055 if ((b = host($1)) == NULL || (e = host($3)) == NULL) {
3058 yyerror("could not parse host specification");
3061 if (b->af != e->af ||
3062 b->addr.type != PF_ADDR_ADDRMASK ||
3063 e->addr.type != PF_ADDR_ADDRMASK ||
3064 unmask(&b->addr.v.a.mask, b->af) !=
3065 (b->af == AF_INET ? 32 : 128) ||
3066 unmask(&e->addr.v.a.mask, e->af) !=
3067 (e->af == AF_INET ? 32 : 128) ||
3068 b->next != NULL || b->not ||
3069 e->next != NULL || e->not) {
3074 yyerror("invalid address range");
3077 memcpy(&b->addr.v.a.mask, &e->addr.v.a.addr,
3078 sizeof(b->addr.v.a.mask));
3079 b->addr.type = PF_ADDR_RANGE;
3085 | STRING '/' NUMBER {
3088 if (asprintf(&buf, "%s/%lld", $1, (long long)$3) == -1)
3089 err(1, "host: asprintf");
3091 if (($$ = host(buf)) == NULL) {
3092 /* error. "any" is handled elsewhere */
3094 yyerror("could not parse host specification");
3099 | NUMBER '/' NUMBER {
3102 /* ie. for 10/8 parsing */
3104 if (asprintf(&buf, "%lld/%lld", (long long)$1, (long long)$3) == -1)
3106 if (asprintf(&buf, "%lld/%lld", $1, $3) == -1)
3108 err(1, "host: asprintf");
3109 if (($$ = host(buf)) == NULL) {
3110 /* error. "any" is handled elsewhere */
3112 yyerror("could not parse host specification");
3118 | dynaddr '/' NUMBER {
3119 struct node_host *n;
3121 if ($3 < 0 || $3 > 128) {
3122 yyerror("bit number too big");
3126 for (n = $1; n != NULL; n = n->next)
3130 if (strlen($2) >= PF_TABLE_NAME_SIZE) {
3131 yyerror("table name '%s' too long", $2);
3135 $$ = calloc(1, sizeof(struct node_host));
3137 err(1, "host: calloc");
3138 $$->addr.type = PF_ADDR_TABLE;
3139 if (strlcpy($$->addr.v.tblname, $2,
3140 sizeof($$->addr.v.tblname)) >=
3141 sizeof($$->addr.v.tblname))
3142 errx(1, "host: strlcpy");
3153 if (atoul($1, &ulval) == -1) {
3154 yyerror("%s is not a number", $1);
3163 dynaddr : '(' STRING ')' {
3168 if (!isalpha(op[0])) {
3169 yyerror("invalid interface name '%s'", op);
3173 while ((p = strrchr($2, ':')) != NULL) {
3174 if (!strcmp(p+1, "network"))
3175 flags |= PFI_AFLAG_NETWORK;
3176 else if (!strcmp(p+1, "broadcast"))
3177 flags |= PFI_AFLAG_BROADCAST;
3178 else if (!strcmp(p+1, "peer"))
3179 flags |= PFI_AFLAG_PEER;
3180 else if (!strcmp(p+1, "0"))
3181 flags |= PFI_AFLAG_NOALIAS;
3183 yyerror("interface %s has bad modifier",
3190 if (flags & (flags - 1) & PFI_AFLAG_MODEMASK) {
3192 yyerror("illegal combination of "
3193 "interface modifiers");
3196 $$ = calloc(1, sizeof(struct node_host));
3198 err(1, "address: calloc");
3200 set_ipmask($$, 128);
3201 $$->addr.type = PF_ADDR_DYNIFTL;
3202 $$->addr.iflags = flags;
3203 if (strlcpy($$->addr.v.ifname, $2,
3204 sizeof($$->addr.v.ifname)) >=
3205 sizeof($$->addr.v.ifname)) {
3208 yyerror("interface name too long");
3217 portspec : port_item { $$ = $1; }
3218 | '{' optnl port_list '}' { $$ = $3; }
3221 port_list : port_item optnl { $$ = $1; }
3222 | port_list comma port_item optnl {
3223 $1->tail->next = $3;
3229 port_item : portrange {
3230 $$ = calloc(1, sizeof(struct node_port));
3232 err(1, "port_item: calloc");
3242 | unaryop portrange {
3244 yyerror("':' cannot be used with an other "
3248 $$ = calloc(1, sizeof(struct node_port));
3250 err(1, "port_item: calloc");
3257 | portrange PORTBINARY portrange {
3259 yyerror("':' cannot be used with an other "
3263 $$ = calloc(1, sizeof(struct node_port));
3265 err(1, "port_item: calloc");
3274 portplain : numberstring {
3275 if (parseport($1, &$$, 0) == -1) {
3283 portrange : numberstring {
3284 if (parseport($1, &$$, PPORT_RANGE) == -1) {
3292 uids : uid_item { $$ = $1; }
3293 | '{' optnl uid_list '}' { $$ = $3; }
3296 uid_list : uid_item optnl { $$ = $1; }
3297 | uid_list comma uid_item optnl {
3298 $1->tail->next = $3;
3305 $$ = calloc(1, sizeof(struct node_uid));
3307 err(1, "uid_item: calloc");
3315 if ($2 == UID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
3316 yyerror("user unknown requires operator = or "
3320 $$ = calloc(1, sizeof(struct node_uid));
3322 err(1, "uid_item: calloc");
3329 | uid PORTBINARY uid {
3330 if ($1 == UID_MAX || $3 == UID_MAX) {
3331 yyerror("user unknown requires operator = or "
3335 $$ = calloc(1, sizeof(struct node_uid));
3337 err(1, "uid_item: calloc");
3347 if (!strcmp($1, "unknown"))
3352 if ((pw = getpwnam($1)) == NULL) {
3353 yyerror("unknown user %s", $1);
3362 if ($1 < 0 || $1 >= UID_MAX) {
3363 yyerror("illegal uid value %lu", $1);
3370 gids : gid_item { $$ = $1; }
3371 | '{' optnl gid_list '}' { $$ = $3; }
3374 gid_list : gid_item optnl { $$ = $1; }
3375 | gid_list comma gid_item optnl {
3376 $1->tail->next = $3;
3383 $$ = calloc(1, sizeof(struct node_gid));
3385 err(1, "gid_item: calloc");
3393 if ($2 == GID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
3394 yyerror("group unknown requires operator = or "
3398 $$ = calloc(1, sizeof(struct node_gid));
3400 err(1, "gid_item: calloc");
3407 | gid PORTBINARY gid {
3408 if ($1 == GID_MAX || $3 == GID_MAX) {
3409 yyerror("group unknown requires operator = or "
3413 $$ = calloc(1, sizeof(struct node_gid));
3415 err(1, "gid_item: calloc");
3425 if (!strcmp($1, "unknown"))
3430 if ((grp = getgrnam($1)) == NULL) {
3431 yyerror("unknown group %s", $1);
3440 if ($1 < 0 || $1 >= GID_MAX) {
3441 yyerror("illegal gid value %lu", $1);
3451 if ((f = parse_flags($1)) < 0) {
3452 yyerror("bad flags %s", $1);
3461 flags : FLAGS flag '/' flag { $$.b1 = $2.b1; $$.b2 = $4.b1; }
3462 | FLAGS '/' flag { $$.b1 = 0; $$.b2 = $3.b1; }
3463 | FLAGS ANY { $$.b1 = 0; $$.b2 = 0; }
3466 icmpspec : ICMPTYPE icmp_item { $$ = $2; }
3467 | ICMPTYPE '{' optnl icmp_list '}' { $$ = $4; }
3468 | ICMP6TYPE icmp6_item { $$ = $2; }
3469 | ICMP6TYPE '{' optnl icmp6_list '}' { $$ = $4; }
3472 icmp_list : icmp_item optnl { $$ = $1; }
3473 | icmp_list comma icmp_item optnl {
3474 $1->tail->next = $3;
3480 icmp6_list : icmp6_item optnl { $$ = $1; }
3481 | icmp6_list comma icmp6_item optnl {
3482 $1->tail->next = $3;
3488 icmp_item : icmptype {
3489 $$ = calloc(1, sizeof(struct node_icmp));
3491 err(1, "icmp_item: calloc");
3494 $$->proto = IPPROTO_ICMP;
3498 | icmptype CODE STRING {
3499 const struct icmpcodeent *p;
3501 if ((p = geticmpcodebyname($1-1, $3, AF_INET)) == NULL) {
3502 yyerror("unknown icmp-code %s", $3);
3508 $$ = calloc(1, sizeof(struct node_icmp));
3510 err(1, "icmp_item: calloc");
3512 $$->code = p->code + 1;
3513 $$->proto = IPPROTO_ICMP;
3517 | icmptype CODE NUMBER {
3518 if ($3 < 0 || $3 > 255) {
3519 yyerror("illegal icmp-code %lu", $3);
3522 $$ = calloc(1, sizeof(struct node_icmp));
3524 err(1, "icmp_item: calloc");
3527 $$->proto = IPPROTO_ICMP;
3533 icmp6_item : icmp6type {
3534 $$ = calloc(1, sizeof(struct node_icmp));
3536 err(1, "icmp_item: calloc");
3539 $$->proto = IPPROTO_ICMPV6;
3543 | icmp6type CODE STRING {
3544 const struct icmpcodeent *p;
3546 if ((p = geticmpcodebyname($1-1, $3, AF_INET6)) == NULL) {
3547 yyerror("unknown icmp6-code %s", $3);
3553 $$ = calloc(1, sizeof(struct node_icmp));
3555 err(1, "icmp_item: calloc");
3557 $$->code = p->code + 1;
3558 $$->proto = IPPROTO_ICMPV6;
3562 | icmp6type CODE NUMBER {
3563 if ($3 < 0 || $3 > 255) {
3564 yyerror("illegal icmp-code %lu", $3);
3567 $$ = calloc(1, sizeof(struct node_icmp));
3569 err(1, "icmp_item: calloc");
3572 $$->proto = IPPROTO_ICMPV6;
3579 const struct icmptypeent *p;
3581 if ((p = geticmptypebyname($1, AF_INET)) == NULL) {
3582 yyerror("unknown icmp-type %s", $1);
3590 if ($1 < 0 || $1 > 255) {
3591 yyerror("illegal icmp-type %lu", $1);
3598 icmp6type : STRING {
3599 const struct icmptypeent *p;
3601 if ((p = geticmptypebyname($1, AF_INET6)) ==
3603 yyerror("unknown icmp6-type %s", $1);
3611 if ($1 < 0 || $1 > 255) {
3612 yyerror("illegal icmp6-type %lu", $1);
3623 if (map_tos($1, &val))
3625 else if ($1[0] == '0' && $1[1] == 'x') {
3627 $$ = strtoul($1, &end, 16);
3628 if (errno || *end != '\0')
3631 $$ = 256; /* flag bad argument */
3632 if ($$ < 0 || $$ > 255) {
3633 yyerror("illegal tos value %s", $1);
3641 if ($$ < 0 || $$ > 255) {
3642 yyerror("illegal tos value %s", $1);
3648 sourcetrack : SOURCETRACK { $$ = PF_SRCTRACK; }
3649 | SOURCETRACK GLOBAL { $$ = PF_SRCTRACK_GLOBAL; }
3650 | SOURCETRACK RULE { $$ = PF_SRCTRACK_RULE; }
3653 statelock : IFBOUND {
3654 $$ = PFRULE_IFBOUND;
3665 | KEEP STATE state_opt_spec {
3666 $$.action = PF_STATE_NORMAL;
3669 | MODULATE STATE state_opt_spec {
3670 $$.action = PF_STATE_MODULATE;
3673 | SYNPROXY STATE state_opt_spec {
3674 $$.action = PF_STATE_SYNPROXY;
3679 flush : /* empty */ { $$ = 0; }
3680 | FLUSH { $$ = PF_FLUSH; }
3682 $$ = PF_FLUSH | PF_FLUSH_GLOBAL;
3686 state_opt_spec : '(' state_opt_list ')' { $$ = $2; }
3687 | /* empty */ { $$ = NULL; }
3690 state_opt_list : state_opt_item { $$ = $1; }
3691 | state_opt_list comma state_opt_item {
3692 $1->tail->next = $3;
3698 state_opt_item : MAXIMUM NUMBER {
3699 if ($2 < 0 || $2 > UINT_MAX) {
3700 yyerror("only positive values permitted");
3703 $$ = calloc(1, sizeof(struct node_state_opt));
3705 err(1, "state_opt_item: calloc");
3706 $$->type = PF_STATE_OPT_MAX;
3707 $$->data.max_states = $2;
3712 $$ = calloc(1, sizeof(struct node_state_opt));
3714 err(1, "state_opt_item: calloc");
3715 $$->type = PF_STATE_OPT_NOSYNC;
3719 | MAXSRCSTATES NUMBER {
3720 if ($2 < 0 || $2 > UINT_MAX) {
3721 yyerror("only positive values permitted");
3724 $$ = calloc(1, sizeof(struct node_state_opt));
3726 err(1, "state_opt_item: calloc");
3727 $$->type = PF_STATE_OPT_MAX_SRC_STATES;
3728 $$->data.max_src_states = $2;
3732 | MAXSRCCONN NUMBER {
3733 if ($2 < 0 || $2 > UINT_MAX) {
3734 yyerror("only positive values permitted");
3737 $$ = calloc(1, sizeof(struct node_state_opt));
3739 err(1, "state_opt_item: calloc");
3740 $$->type = PF_STATE_OPT_MAX_SRC_CONN;
3741 $$->data.max_src_conn = $2;
3745 | MAXSRCCONNRATE NUMBER '/' NUMBER {
3746 if ($2 < 0 || $2 > UINT_MAX ||
3747 $4 < 0 || $4 > UINT_MAX) {
3748 yyerror("only positive values permitted");
3751 $$ = calloc(1, sizeof(struct node_state_opt));
3753 err(1, "state_opt_item: calloc");
3754 $$->type = PF_STATE_OPT_MAX_SRC_CONN_RATE;
3755 $$->data.max_src_conn_rate.limit = $2;
3756 $$->data.max_src_conn_rate.seconds = $4;
3760 | OVERLOAD '<' STRING '>' flush {
3761 if (strlen($3) >= PF_TABLE_NAME_SIZE) {
3762 yyerror("table name '%s' too long", $3);
3766 $$ = calloc(1, sizeof(struct node_state_opt));
3768 err(1, "state_opt_item: calloc");
3769 if (strlcpy($$->data.overload.tblname, $3,
3770 PF_TABLE_NAME_SIZE) >= PF_TABLE_NAME_SIZE)
3771 errx(1, "state_opt_item: strlcpy");
3773 $$->type = PF_STATE_OPT_OVERLOAD;
3774 $$->data.overload.flush = $5;
3778 | MAXSRCNODES NUMBER {
3779 if ($2 < 0 || $2 > UINT_MAX) {
3780 yyerror("only positive values permitted");
3783 $$ = calloc(1, sizeof(struct node_state_opt));
3785 err(1, "state_opt_item: calloc");
3786 $$->type = PF_STATE_OPT_MAX_SRC_NODES;
3787 $$->data.max_src_nodes = $2;
3792 $$ = calloc(1, sizeof(struct node_state_opt));
3794 err(1, "state_opt_item: calloc");
3795 $$->type = PF_STATE_OPT_SRCTRACK;
3796 $$->data.src_track = $1;
3801 $$ = calloc(1, sizeof(struct node_state_opt));
3803 err(1, "state_opt_item: calloc");
3804 $$->type = PF_STATE_OPT_STATELOCK;
3805 $$->data.statelock = $1;
3810 $$ = calloc(1, sizeof(struct node_state_opt));
3812 err(1, "state_opt_item: calloc");
3813 $$->type = PF_STATE_OPT_SLOPPY;
3820 if ($2 < 0 || $2 > UINT_MAX) {
3821 yyerror("only positive values permitted");
3824 for (i = 0; pf_timeouts[i].name &&
3825 strcmp(pf_timeouts[i].name, $1); ++i)
3827 if (!pf_timeouts[i].name) {
3828 yyerror("illegal timeout name %s", $1);
3832 if (strchr(pf_timeouts[i].name, '.') == NULL) {
3833 yyerror("illegal state timeout %s", $1);
3838 $$ = calloc(1, sizeof(struct node_state_opt));
3840 err(1, "state_opt_item: calloc");
3841 $$->type = PF_STATE_OPT_TIMEOUT;
3842 $$->data.timeout.number = pf_timeouts[i].timeout;
3843 $$->data.timeout.seconds = $2;
3849 label : LABEL STRING {
3854 qname : QUEUE STRING {
3858 | QUEUE '(' STRING ')' {
3862 | QUEUE '(' STRING comma STRING ')' {
3868 no : /* empty */ { $$ = 0; }
3872 portstar : numberstring {
3873 if (parseport($1, &$$, PPORT_RANGE|PPORT_STAR) == -1) {
3881 redirspec : host { $$ = $1; }
3882 | '{' optnl redir_host_list '}' { $$ = $3; }
3885 redir_host_list : host optnl { $$ = $1; }
3886 | redir_host_list comma host optnl {
3887 $1->tail->next = $3;
3888 $1->tail = $3->tail;
3893 redirpool : /* empty */ { $$ = NULL; }
3895 $$ = calloc(1, sizeof(struct redirection));
3897 err(1, "redirection: calloc");
3899 $$->rport.a = $$->rport.b = $$->rport.t = 0;
3901 | ARROW redirspec PORT portstar {
3902 $$ = calloc(1, sizeof(struct redirection));
3904 err(1, "redirection: calloc");
3910 hashkey : /* empty */
3912 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3914 err(1, "hashkey: calloc");
3915 $$->key32[0] = arc4random();
3916 $$->key32[1] = arc4random();
3917 $$->key32[2] = arc4random();
3918 $$->key32[3] = arc4random();
3922 if (!strncmp($1, "0x", 2)) {
3923 if (strlen($1) != 34) {
3925 yyerror("hex key must be 128 bits "
3926 "(32 hex digits) long");
3929 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3931 err(1, "hashkey: calloc");
3933 if (sscanf($1, "0x%8x%8x%8x%8x",
3934 &$$->key32[0], &$$->key32[1],
3935 &$$->key32[2], &$$->key32[3]) != 4) {
3938 yyerror("invalid hex key");
3944 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3946 err(1, "hashkey: calloc");
3948 MD5Update(&context, (unsigned char *)$1,
3950 MD5Final((unsigned char *)$$, &context);
3951 HTONL($$->key32[0]);
3952 HTONL($$->key32[1]);
3953 HTONL($$->key32[2]);
3954 HTONL($$->key32[3]);
3960 pool_opts : { bzero(&pool_opts, sizeof pool_opts); }
3964 bzero(&pool_opts, sizeof pool_opts);
3969 pool_opts_l : pool_opts_l pool_opt
3973 pool_opt : BITMASK {
3974 if (pool_opts.type) {
3975 yyerror("pool type cannot be redefined");
3978 pool_opts.type = PF_POOL_BITMASK;
3981 if (pool_opts.type) {
3982 yyerror("pool type cannot be redefined");
3985 pool_opts.type = PF_POOL_RANDOM;
3987 | SOURCEHASH hashkey {
3988 if (pool_opts.type) {
3989 yyerror("pool type cannot be redefined");
3992 pool_opts.type = PF_POOL_SRCHASH;
3996 if (pool_opts.type) {
3997 yyerror("pool type cannot be redefined");
4000 pool_opts.type = PF_POOL_ROUNDROBIN;
4003 if (pool_opts.staticport) {
4004 yyerror("static-port cannot be redefined");
4007 pool_opts.staticport = 1;
4010 if (filter_opts.marker & POM_STICKYADDRESS) {
4011 yyerror("sticky-address cannot be redefined");
4014 pool_opts.marker |= POM_STICKYADDRESS;
4015 pool_opts.opts |= PF_POOL_STICKYADDR;
4017 | MAPEPORTSET number '/' number '/' number {
4018 if (pool_opts.mape.offset) {
4019 yyerror("map-e-portset cannot be redefined");
4022 if (pool_opts.type) {
4023 yyerror("map-e-portset cannot be used with "
4027 if ($2 <= 0 || $2 >= 16) {
4028 yyerror("MAP-E PSID offset must be 1-15");
4031 if ($4 < 0 || $4 >= 16 || $2 + $4 > 16) {
4032 yyerror("Invalid MAP-E PSID length");
4034 } else if ($4 == 0) {
4035 yyerror("PSID Length = 0: this means"
4036 " you do not need MAP-E");
4039 if ($6 < 0 || $6 > 65535) {
4040 yyerror("Invalid MAP-E PSID");
4043 pool_opts.mape.offset = $2;
4044 pool_opts.mape.psidlen = $4;
4045 pool_opts.mape.psid = $6;
4049 redirection : /* empty */ { $$ = NULL; }
4051 $$ = calloc(1, sizeof(struct redirection));
4053 err(1, "redirection: calloc");
4055 $$->rport.a = $$->rport.b = $$->rport.t = 0;
4057 | ARROW host PORT portstar {
4058 $$ = calloc(1, sizeof(struct redirection));
4060 err(1, "redirection: calloc");
4066 natpasslog : /* empty */ { $$.b1 = $$.b2 = 0; $$.w2 = 0; }
4067 | PASS { $$.b1 = 1; $$.b2 = 0; $$.w2 = 0; }
4068 | PASS log { $$.b1 = 1; $$.b2 = $2.log; $$.w2 = $2.logif; }
4069 | log { $$.b1 = 0; $$.b2 = $1.log; $$.w2 = $1.logif; }
4072 nataction : no NAT natpasslog {
4074 yyerror("\"pass\" not valid with \"no\"");
4085 | no RDR natpasslog {
4087 yyerror("\"pass\" not valid with \"no\"");
4100 natrule : nataction interface af proto fromto tag tagged rtable
4103 struct pfctl_rule r;
4105 if (check_rulestate(PFCTL_STATE_NAT))
4108 memset(&r, 0, sizeof(r));
4117 if ($5.src.host && $5.src.host->af &&
4118 !$5.src.host->ifindex)
4119 r.af = $5.src.host->af;
4120 else if ($5.dst.host && $5.dst.host->af &&
4121 !$5.dst.host->ifindex)
4122 r.af = $5.dst.host->af;
4126 if (strlcpy(r.tagname, $6, PF_TAG_NAME_SIZE) >=
4128 yyerror("tag too long, max %u chars",
4129 PF_TAG_NAME_SIZE - 1);
4134 if (strlcpy(r.match_tagname, $7.name,
4135 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4136 yyerror("tag too long, max %u chars",
4137 PF_TAG_NAME_SIZE - 1);
4140 r.match_tag_not = $7.neg;
4143 if (r.action == PF_NONAT || r.action == PF_NORDR) {
4145 yyerror("translation rule with 'no' "
4146 "does not need '->'");
4150 if ($9 == NULL || $9->host == NULL) {
4151 yyerror("translation rule requires '-> "
4155 if (!r.af && ! $9->host->ifindex)
4156 r.af = $9->host->af;
4158 remove_invalid_hosts(&$9->host, &r.af);
4159 if (invalid_redirect($9->host, r.af))
4161 if (check_netmask($9->host, r.af))
4164 r.rpool.proxy_port[0] = ntohs($9->rport.a);
4168 if (!$9->rport.b && $9->rport.t &&
4169 $5.dst.port != NULL) {
4170 r.rpool.proxy_port[1] =
4171 ntohs($9->rport.a) +
4173 $5.dst.port->port[1]) -
4175 $5.dst.port->port[0]));
4177 r.rpool.proxy_port[1] =
4181 r.rpool.proxy_port[1] =
4183 if (!r.rpool.proxy_port[0] &&
4184 !r.rpool.proxy_port[1]) {
4185 r.rpool.proxy_port[0] =
4186 PF_NAT_PROXY_PORT_LOW;
4187 r.rpool.proxy_port[1] =
4188 PF_NAT_PROXY_PORT_HIGH;
4189 } else if (!r.rpool.proxy_port[1])
4190 r.rpool.proxy_port[1] =
4191 r.rpool.proxy_port[0];
4197 r.rpool.opts = $10.type;
4198 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
4199 PF_POOL_NONE && ($9->host->next != NULL ||
4200 $9->host->addr.type == PF_ADDR_TABLE ||
4201 DYNIF_MULTIADDR($9->host->addr)))
4202 r.rpool.opts = PF_POOL_ROUNDROBIN;
4203 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4204 PF_POOL_ROUNDROBIN &&
4205 disallow_table($9->host, "tables are only "
4206 "supported in round-robin redirection "
4209 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4210 PF_POOL_ROUNDROBIN &&
4211 disallow_alias($9->host, "interface (%s) "
4212 "is only supported in round-robin "
4213 "redirection pools"))
4215 if ($9->host->next != NULL) {
4216 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4217 PF_POOL_ROUNDROBIN) {
4218 yyerror("only round-robin "
4219 "valid for multiple "
4220 "redirection addresses");
4226 if ($10.key != NULL)
4227 memcpy(&r.rpool.key, $10.key,
4228 sizeof(struct pf_poolhashkey));
4231 r.rpool.opts |= $10.opts;
4233 if ($10.staticport) {
4234 if (r.action != PF_NAT) {
4235 yyerror("the 'static-port' option is "
4236 "only valid with nat rules");
4239 if (r.rpool.proxy_port[0] !=
4240 PF_NAT_PROXY_PORT_LOW &&
4241 r.rpool.proxy_port[1] !=
4242 PF_NAT_PROXY_PORT_HIGH) {
4243 yyerror("the 'static-port' option can't"
4244 " be used when specifying a port"
4248 r.rpool.proxy_port[0] = 0;
4249 r.rpool.proxy_port[1] = 0;
4252 if ($10.mape.offset) {
4253 if (r.action != PF_NAT) {
4254 yyerror("the 'map-e-portset' option is"
4255 " only valid with nat rules");
4258 if ($10.staticport) {
4259 yyerror("the 'map-e-portset' option"
4260 " can't be used 'static-port'");
4263 if (r.rpool.proxy_port[0] !=
4264 PF_NAT_PROXY_PORT_LOW &&
4265 r.rpool.proxy_port[1] !=
4266 PF_NAT_PROXY_PORT_HIGH) {
4267 yyerror("the 'map-e-portset' option"
4268 " can't be used when specifying"
4272 r.rpool.mape = $10.mape;
4275 expand_rule(&r, $2, $9 == NULL ? NULL : $9->host, $4,
4276 $5.src_os, $5.src.host, $5.src.port, $5.dst.host,
4277 $5.dst.port, 0, 0, 0, "");
4282 binatrule : no BINAT natpasslog interface af proto FROM ipspec toipspec tag
4283 tagged rtable redirection
4285 struct pfctl_rule binat;
4286 struct pf_pooladdr *pa;
4288 if (check_rulestate(PFCTL_STATE_NAT))
4290 if (disallow_urpf_failed($9, "\"urpf-failed\" is not "
4291 "permitted as a binat destination"))
4294 memset(&binat, 0, sizeof(binat));
4297 yyerror("\"pass\" not valid with \"no\"");
4301 binat.action = PF_NOBINAT;
4303 binat.action = PF_BINAT;
4304 binat.natpass = $3.b1;
4306 binat.logif = $3.w2;
4308 if (!binat.af && $8 != NULL && $8->af)
4310 if (!binat.af && $9 != NULL && $9->af)
4313 if (!binat.af && $13 != NULL && $13->host)
4314 binat.af = $13->host->af;
4316 yyerror("address family (inet/inet6) "
4322 memcpy(binat.ifname, $4->ifname,
4323 sizeof(binat.ifname));
4324 binat.ifnot = $4->not;
4329 if (strlcpy(binat.tagname, $10,
4330 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4331 yyerror("tag too long, max %u chars",
4332 PF_TAG_NAME_SIZE - 1);
4336 if (strlcpy(binat.match_tagname, $11.name,
4337 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4338 yyerror("tag too long, max %u chars",
4339 PF_TAG_NAME_SIZE - 1);
4342 binat.match_tag_not = $11.neg;
4343 binat.rtableid = $12;
4346 binat.proto = $6->proto;
4350 if ($8 != NULL && disallow_table($8, "invalid use of "
4351 "table <%s> as the source address of a binat rule"))
4353 if ($8 != NULL && disallow_alias($8, "invalid use of "
4354 "interface (%s) as the source address of a binat "
4357 if ($13 != NULL && $13->host != NULL && disallow_table(
4358 $13->host, "invalid use of table <%s> as the "
4359 "redirect address of a binat rule"))
4361 if ($13 != NULL && $13->host != NULL && disallow_alias(
4362 $13->host, "invalid use of interface (%s) as the "
4363 "redirect address of a binat rule"))
4368 yyerror("multiple binat ip addresses");
4371 if ($8->addr.type == PF_ADDR_DYNIFTL)
4373 if ($8->af != binat.af) {
4374 yyerror("binat ip versions must match");
4377 if (check_netmask($8, binat.af))
4379 memcpy(&binat.src.addr, &$8->addr,
4380 sizeof(binat.src.addr));
4385 yyerror("multiple binat ip addresses");
4388 if ($9->af != binat.af && $9->af) {
4389 yyerror("binat ip versions must match");
4392 if (check_netmask($9, binat.af))
4394 memcpy(&binat.dst.addr, &$9->addr,
4395 sizeof(binat.dst.addr));
4396 binat.dst.neg = $9->not;
4400 if (binat.action == PF_NOBINAT) {
4402 yyerror("'no binat' rule does not need"
4407 if ($13 == NULL || $13->host == NULL) {
4408 yyerror("'binat' rule requires"
4413 remove_invalid_hosts(&$13->host, &binat.af);
4414 if (invalid_redirect($13->host, binat.af))
4416 if ($13->host->next != NULL) {
4417 yyerror("binat rule must redirect to "
4418 "a single address");
4421 if (check_netmask($13->host, binat.af))
4424 if (!PF_AZERO(&binat.src.addr.v.a.mask,
4426 !PF_AEQ(&binat.src.addr.v.a.mask,
4427 &$13->host->addr.v.a.mask, binat.af)) {
4428 yyerror("'binat' source mask and "
4429 "redirect mask must be the same");
4433 TAILQ_INIT(&binat.rpool.list);
4434 pa = calloc(1, sizeof(struct pf_pooladdr));
4436 err(1, "binat: calloc");
4437 pa->addr = $13->host->addr;
4439 TAILQ_INSERT_TAIL(&binat.rpool.list,
4445 pfctl_append_rule(pf, &binat, "");
4449 tag : /* empty */ { $$ = NULL; }
4450 | TAG STRING { $$ = $2; }
4453 tagged : /* empty */ { $$.neg = 0; $$.name = NULL; }
4454 | not TAGGED string { $$.neg = $1; $$.name = $3; }
4457 rtable : /* empty */ { $$ = -1; }
4459 if ($2 < 0 || $2 > rt_tableid_max()) {
4460 yyerror("invalid rtable id");
4467 route_host : STRING {
4468 $$ = calloc(1, sizeof(struct node_host));
4470 err(1, "route_host: calloc");
4471 $$->ifname = strdup($1);
4472 set_ipmask($$, 128);
4476 | '(' STRING host ')' {
4477 struct node_host *n;
4480 for (n = $3; n != NULL; n = n->next)
4481 n->ifname = strdup($2);
4485 route_host_list : route_host optnl { $$ = $1; }
4486 | route_host_list comma route_host optnl {
4489 if ($1->af != $3->af) {
4490 yyerror("all pool addresses must be in the "
4491 "same address family");
4494 $1->tail->next = $3;
4495 $1->tail = $3->tail;
4500 routespec : route_host { $$ = $1; }
4501 | '{' optnl route_host_list '}' { $$ = $3; }
4504 route : /* empty */ {
4510 /* backwards-compat */
4515 | ROUTETO routespec pool_opts {
4518 $$.pool_opts = $3.type | $3.opts;
4522 | REPLYTO routespec pool_opts {
4525 $$.pool_opts = $3.type | $3.opts;
4529 | DUPTO routespec pool_opts {
4532 $$.pool_opts = $3.type | $3.opts;
4538 timeout_spec : STRING NUMBER
4540 if (check_rulestate(PFCTL_STATE_OPTION)) {
4544 if ($2 < 0 || $2 > UINT_MAX) {
4545 yyerror("only positive values permitted");
4548 if (pfctl_set_timeout(pf, $1, $2, 0) != 0) {
4549 yyerror("unknown timeout %s", $1);
4556 if (check_rulestate(PFCTL_STATE_OPTION))
4558 if ($2 < 0 || $2 > UINT_MAX) {
4559 yyerror("only positive values permitted");
4562 if (pfctl_set_timeout(pf, "interval", $2, 0) != 0)
4567 timeout_list : timeout_list comma timeout_spec optnl
4568 | timeout_spec optnl
4571 limit_spec : STRING NUMBER
4573 if (check_rulestate(PFCTL_STATE_OPTION)) {
4577 if ($2 < 0 || $2 > UINT_MAX) {
4578 yyerror("only positive values permitted");
4581 if (pfctl_set_limit(pf, $1, $2) != 0) {
4582 yyerror("unable to set limit %s %u", $1, $2);
4590 limit_list : limit_list comma limit_spec optnl
4598 yesno : NO { $$ = 0; }
4600 if (!strcmp($1, "yes"))
4603 yyerror("invalid value '%s', expected 'yes' "
4612 unaryop : '=' { $$ = PF_OP_EQ; }
4613 | '!' '=' { $$ = PF_OP_NE; }
4614 | '<' '=' { $$ = PF_OP_LE; }
4615 | '<' { $$ = PF_OP_LT; }
4616 | '>' '=' { $$ = PF_OP_GE; }
4617 | '>' { $$ = PF_OP_GT; }
4623 yyerror(const char *fmt, ...)
4629 fprintf(stderr, "%s:%d: ", file->name, yylval.lineno);
4630 vfprintf(stderr, fmt, ap);
4631 fprintf(stderr, "\n");
4637 disallow_table(struct node_host *h, const char *fmt)
4639 for (; h != NULL; h = h->next)
4640 if (h->addr.type == PF_ADDR_TABLE) {
4641 yyerror(fmt, h->addr.v.tblname);
4648 disallow_urpf_failed(struct node_host *h, const char *fmt)
4650 for (; h != NULL; h = h->next)
4651 if (h->addr.type == PF_ADDR_URPFFAILED) {
4659 disallow_alias(struct node_host *h, const char *fmt)
4661 for (; h != NULL; h = h->next)
4662 if (DYNIF_MULTIADDR(h->addr)) {
4663 yyerror(fmt, h->addr.v.tblname);
4670 rule_consistent(struct pfctl_rule *r, int anchor_call)
4674 switch (r->action) {
4679 problems = filter_consistent(r, anchor_call);
4683 problems = nat_consistent(r);
4687 problems = rdr_consistent(r);
4698 filter_consistent(struct pfctl_rule *r, int anchor_call)
4702 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP &&
4703 (r->src.port_op || r->dst.port_op)) {
4704 yyerror("port only applies to tcp/udp");
4707 if (r->proto != IPPROTO_ICMP && r->proto != IPPROTO_ICMPV6 &&
4708 (r->type || r->code)) {
4709 yyerror("icmp-type/code only applies to icmp");
4712 if (!r->af && (r->type || r->code)) {
4713 yyerror("must indicate address family with icmp-type/code");
4716 if (r->overload_tblname[0] &&
4717 r->max_src_conn == 0 && r->max_src_conn_rate.seconds == 0) {
4718 yyerror("'overload' requires 'max-src-conn' "
4719 "or 'max-src-conn-rate'");
4722 if ((r->proto == IPPROTO_ICMP && r->af == AF_INET6) ||
4723 (r->proto == IPPROTO_ICMPV6 && r->af == AF_INET)) {
4724 yyerror("proto %s doesn't match address family %s",
4725 r->proto == IPPROTO_ICMP ? "icmp" : "icmp6",
4726 r->af == AF_INET ? "inet" : "inet6");
4729 if (r->allow_opts && r->action != PF_PASS) {
4730 yyerror("allow-opts can only be specified for pass rules");
4733 if (r->rule_flag & PFRULE_FRAGMENT && (r->src.port_op ||
4734 r->dst.port_op || r->flagset || r->type || r->code)) {
4735 yyerror("fragments can be filtered only on IP header fields");
4738 if (r->rule_flag & PFRULE_RETURNRST && r->proto != IPPROTO_TCP) {
4739 yyerror("return-rst can only be applied to TCP rules");
4742 if (r->max_src_nodes && !(r->rule_flag & PFRULE_RULESRCTRACK)) {
4743 yyerror("max-src-nodes requires 'source-track rule'");
4746 if (r->action == PF_DROP && r->keep_state) {
4747 yyerror("keep state on block rules doesn't make sense");
4750 if (r->rule_flag & PFRULE_STATESLOPPY &&
4751 (r->keep_state == PF_STATE_MODULATE ||
4752 r->keep_state == PF_STATE_SYNPROXY)) {
4753 yyerror("sloppy state matching cannot be used with "
4754 "synproxy state or modulate state");
4761 nat_consistent(struct pfctl_rule *r)
4763 return (0); /* yeah! */
4767 rdr_consistent(struct pfctl_rule *r)
4771 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP) {
4772 if (r->src.port_op) {
4773 yyerror("src port only applies to tcp/udp");
4776 if (r->dst.port_op) {
4777 yyerror("dst port only applies to tcp/udp");
4780 if (r->rpool.proxy_port[0]) {
4781 yyerror("rpool port only applies to tcp/udp");
4785 if (r->dst.port_op &&
4786 r->dst.port_op != PF_OP_EQ && r->dst.port_op != PF_OP_RRG) {
4787 yyerror("invalid port operator for rdr destination port");
4794 process_tabledef(char *name, struct table_opts *opts)
4796 struct pfr_buffer ab;
4797 struct node_tinit *ti;
4798 unsigned long maxcount;
4799 size_t s = sizeof(maxcount);
4801 bzero(&ab, sizeof(ab));
4802 ab.pfrb_type = PFRB_ADDRS;
4803 SIMPLEQ_FOREACH(ti, &opts->init_nodes, entries) {
4805 if (pfr_buf_load(&ab, ti->file, 0, append_addr)) {
4807 yyerror("cannot load \"%s\": %s",
4808 ti->file, strerror(errno));
4810 yyerror("file \"%s\" contains bad data",
4815 if (append_addr_host(&ab, ti->host, 0, 0)) {
4816 yyerror("cannot create address buffer: %s",
4821 if (pf->opts & PF_OPT_VERBOSE)
4822 print_tabledef(name, opts->flags, opts->init_addr,
4824 if (!(pf->opts & PF_OPT_NOACTION) &&
4825 pfctl_define_table(name, opts->flags, opts->init_addr,
4826 pf->anchor->name, &ab, pf->anchor->ruleset.tticket)) {
4828 if (sysctlbyname("net.pf.request_maxcount", &maxcount, &s,
4832 if (ab.pfrb_size > maxcount)
4833 yyerror("cannot define table %s: too many elements.\n"
4834 "Consider increasing net.pf.request_maxcount.",
4837 yyerror("cannot define table %s: %s", name,
4838 pfr_strerror(errno));
4855 /* macro gore, but you should've seen the prior indentation nightmare... */
4857 #define FREE_LIST(T,r) \
4860 while (node != NULL) { \
4862 node = node->next; \
4867 #define LOOP_THROUGH(T,n,r,C) \
4871 r = calloc(1, sizeof(T)); \
4873 err(1, "LOOP: calloc"); \
4877 while (n != NULL) { \
4886 expand_label_str(char *label, size_t len, const char *srch, const char *repl)
4891 if ((tmp = calloc(1, len)) == NULL)
4892 err(1, "expand_label_str: calloc");
4894 while ((q = strstr(p, srch)) != NULL) {
4896 if ((strlcat(tmp, p, len) >= len) ||
4897 (strlcat(tmp, repl, len) >= len))
4898 errx(1, "expand_label: label too long");
4902 if (strlcat(tmp, p, len) >= len)
4903 errx(1, "expand_label: label too long");
4904 strlcpy(label, tmp, len); /* always fits */
4909 expand_label_if(const char *name, char *label, size_t len, const char *ifname)
4911 if (strstr(label, name) != NULL) {
4913 expand_label_str(label, len, name, "any");
4915 expand_label_str(label, len, name, ifname);
4920 expand_label_addr(const char *name, char *label, size_t len, sa_family_t af,
4921 struct node_host *h)
4923 char tmp[64], tmp_not[66];
4925 if (strstr(label, name) != NULL) {
4926 switch (h->addr.type) {
4927 case PF_ADDR_DYNIFTL:
4928 snprintf(tmp, sizeof(tmp), "(%s)", h->addr.v.ifname);
4931 snprintf(tmp, sizeof(tmp), "<%s>", h->addr.v.tblname);
4933 case PF_ADDR_NOROUTE:
4934 snprintf(tmp, sizeof(tmp), "no-route");
4936 case PF_ADDR_URPFFAILED:
4937 snprintf(tmp, sizeof(tmp), "urpf-failed");
4939 case PF_ADDR_ADDRMASK:
4940 if (!af || (PF_AZERO(&h->addr.v.a.addr, af) &&
4941 PF_AZERO(&h->addr.v.a.mask, af)))
4942 snprintf(tmp, sizeof(tmp), "any");
4947 if (inet_ntop(af, &h->addr.v.a.addr, a,
4949 snprintf(tmp, sizeof(tmp), "?");
4951 bits = unmask(&h->addr.v.a.mask, af);
4952 if ((af == AF_INET && bits < 32) ||
4953 (af == AF_INET6 && bits < 128))
4954 snprintf(tmp, sizeof(tmp),
4957 snprintf(tmp, sizeof(tmp),
4963 snprintf(tmp, sizeof(tmp), "?");
4968 snprintf(tmp_not, sizeof(tmp_not), "! %s", tmp);
4969 expand_label_str(label, len, name, tmp_not);
4971 expand_label_str(label, len, name, tmp);
4976 expand_label_port(const char *name, char *label, size_t len,
4977 struct node_port *port)
4979 char a1[6], a2[6], op[13] = "";
4981 if (strstr(label, name) != NULL) {
4982 snprintf(a1, sizeof(a1), "%u", ntohs(port->port[0]));
4983 snprintf(a2, sizeof(a2), "%u", ntohs(port->port[1]));
4986 else if (port->op == PF_OP_IRG)
4987 snprintf(op, sizeof(op), "%s><%s", a1, a2);
4988 else if (port->op == PF_OP_XRG)
4989 snprintf(op, sizeof(op), "%s<>%s", a1, a2);
4990 else if (port->op == PF_OP_EQ)
4991 snprintf(op, sizeof(op), "%s", a1);
4992 else if (port->op == PF_OP_NE)
4993 snprintf(op, sizeof(op), "!=%s", a1);
4994 else if (port->op == PF_OP_LT)
4995 snprintf(op, sizeof(op), "<%s", a1);
4996 else if (port->op == PF_OP_LE)
4997 snprintf(op, sizeof(op), "<=%s", a1);
4998 else if (port->op == PF_OP_GT)
4999 snprintf(op, sizeof(op), ">%s", a1);
5000 else if (port->op == PF_OP_GE)
5001 snprintf(op, sizeof(op), ">=%s", a1);
5002 expand_label_str(label, len, name, op);
5007 expand_label_proto(const char *name, char *label, size_t len, u_int8_t proto)
5009 struct protoent *pe;
5012 if (strstr(label, name) != NULL) {
5013 pe = getprotobynumber(proto);
5015 expand_label_str(label, len, name, pe->p_name);
5017 snprintf(n, sizeof(n), "%u", proto);
5018 expand_label_str(label, len, name, n);
5024 expand_label_nr(const char *name, char *label, size_t len)
5028 if (strstr(label, name) != NULL) {
5029 snprintf(n, sizeof(n), "%u", pf->anchor->match);
5030 expand_label_str(label, len, name, n);
5035 expand_label(char *label, size_t len, const char *ifname, sa_family_t af,
5036 struct node_host *src_host, struct node_port *src_port,
5037 struct node_host *dst_host, struct node_port *dst_port,
5040 expand_label_if("$if", label, len, ifname);
5041 expand_label_addr("$srcaddr", label, len, af, src_host);
5042 expand_label_addr("$dstaddr", label, len, af, dst_host);
5043 expand_label_port("$srcport", label, len, src_port);
5044 expand_label_port("$dstport", label, len, dst_port);
5045 expand_label_proto("$proto", label, len, proto);
5046 expand_label_nr("$nr", label, len);
5050 expand_altq(struct pf_altq *a, struct node_if *interfaces,
5051 struct node_queue *nqueues, struct node_queue_bw bwspec,
5052 struct node_queue_opt *opts)
5054 struct pf_altq pa, pb;
5055 char qname[PF_QNAME_SIZE];
5056 struct node_queue *n;
5057 struct node_queue_bw bw;
5060 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
5061 FREE_LIST(struct node_if, interfaces);
5063 FREE_LIST(struct node_queue, nqueues);
5067 LOOP_THROUGH(struct node_if, interface, interfaces,
5068 memcpy(&pa, a, sizeof(struct pf_altq));
5069 if (strlcpy(pa.ifname, interface->ifname,
5070 sizeof(pa.ifname)) >= sizeof(pa.ifname))
5071 errx(1, "expand_altq: strlcpy");
5073 if (interface->not) {
5074 yyerror("altq on ! <interface> is not supported");
5077 if (eval_pfaltq(pf, &pa, &bwspec, opts))
5080 if (pfctl_add_altq(pf, &pa))
5083 if (pf->opts & PF_OPT_VERBOSE) {
5084 print_altq(&pf->paltq->altq, 0,
5086 if (nqueues && nqueues->tail) {
5088 LOOP_THROUGH(struct node_queue, queue,
5098 if (pa.scheduler == ALTQT_CBQ ||
5099 pa.scheduler == ALTQT_HFSC) {
5100 /* now create a root queue */
5101 memset(&pb, 0, sizeof(struct pf_altq));
5102 if (strlcpy(qname, "root_", sizeof(qname)) >=
5104 errx(1, "expand_altq: strlcpy");
5105 if (strlcat(qname, interface->ifname,
5106 sizeof(qname)) >= sizeof(qname))
5107 errx(1, "expand_altq: strlcat");
5108 if (strlcpy(pb.qname, qname,
5109 sizeof(pb.qname)) >= sizeof(pb.qname))
5110 errx(1, "expand_altq: strlcpy");
5111 if (strlcpy(pb.ifname, interface->ifname,
5112 sizeof(pb.ifname)) >= sizeof(pb.ifname))
5113 errx(1, "expand_altq: strlcpy");
5114 pb.qlimit = pa.qlimit;
5115 pb.scheduler = pa.scheduler;
5116 bw.bw_absolute = pa.ifbandwidth;
5118 if (eval_pfqueue(pf, &pb, &bw, opts))
5121 if (pfctl_add_altq(pf, &pb))
5125 LOOP_THROUGH(struct node_queue, queue, nqueues,
5126 n = calloc(1, sizeof(struct node_queue));
5128 err(1, "expand_altq: calloc");
5129 if (pa.scheduler == ALTQT_CBQ ||
5130 pa.scheduler == ALTQT_HFSC)
5131 if (strlcpy(n->parent, qname,
5132 sizeof(n->parent)) >=
5134 errx(1, "expand_altq: strlcpy");
5135 if (strlcpy(n->queue, queue->queue,
5136 sizeof(n->queue)) >= sizeof(n->queue))
5137 errx(1, "expand_altq: strlcpy");
5138 if (strlcpy(n->ifname, interface->ifname,
5139 sizeof(n->ifname)) >= sizeof(n->ifname))
5140 errx(1, "expand_altq: strlcpy");
5141 n->scheduler = pa.scheduler;
5147 queues->tail->next = n;
5153 FREE_LIST(struct node_if, interfaces);
5155 FREE_LIST(struct node_queue, nqueues);
5161 expand_queue(struct pf_altq *a, struct node_if *interfaces,
5162 struct node_queue *nqueues, struct node_queue_bw bwspec,
5163 struct node_queue_opt *opts)
5165 struct node_queue *n, *nq;
5170 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
5171 FREE_LIST(struct node_queue, nqueues);
5175 if (queues == NULL) {
5176 yyerror("queue %s has no parent", a->qname);
5177 FREE_LIST(struct node_queue, nqueues);
5181 LOOP_THROUGH(struct node_if, interface, interfaces,
5182 LOOP_THROUGH(struct node_queue, tqueue, queues,
5183 if (!strncmp(a->qname, tqueue->queue, PF_QNAME_SIZE) &&
5184 (interface->ifname[0] == 0 ||
5185 (!interface->not && !strncmp(interface->ifname,
5186 tqueue->ifname, IFNAMSIZ)) ||
5187 (interface->not && strncmp(interface->ifname,
5188 tqueue->ifname, IFNAMSIZ)))) {
5189 /* found ourself in queues */
5192 memcpy(&pa, a, sizeof(struct pf_altq));
5194 if (pa.scheduler != ALTQT_NONE &&
5195 pa.scheduler != tqueue->scheduler) {
5196 yyerror("exactly one scheduler type "
5197 "per interface allowed");
5200 pa.scheduler = tqueue->scheduler;
5202 /* scheduler dependent error checking */
5203 switch (pa.scheduler) {
5205 if (nqueues != NULL) {
5206 yyerror("priq queues cannot "
5207 "have child queues");
5210 if (bwspec.bw_absolute > 0 ||
5211 bwspec.bw_percent < 100) {
5212 yyerror("priq doesn't take "
5221 if (strlcpy(pa.ifname, tqueue->ifname,
5222 sizeof(pa.ifname)) >= sizeof(pa.ifname))
5223 errx(1, "expand_queue: strlcpy");
5224 if (strlcpy(pa.parent, tqueue->parent,
5225 sizeof(pa.parent)) >= sizeof(pa.parent))
5226 errx(1, "expand_queue: strlcpy");
5228 if (eval_pfqueue(pf, &pa, &bwspec, opts))
5231 if (pfctl_add_altq(pf, &pa))
5234 for (nq = nqueues; nq != NULL; nq = nq->next) {
5235 if (!strcmp(a->qname, nq->queue)) {
5236 yyerror("queue cannot have "
5242 sizeof(struct node_queue));
5244 err(1, "expand_queue: calloc");
5245 if (strlcpy(n->parent, a->qname,
5246 sizeof(n->parent)) >=
5248 errx(1, "expand_queue strlcpy");
5249 if (strlcpy(n->queue, nq->queue,
5250 sizeof(n->queue)) >=
5252 errx(1, "expand_queue strlcpy");
5253 if (strlcpy(n->ifname, tqueue->ifname,
5254 sizeof(n->ifname)) >=
5256 errx(1, "expand_queue strlcpy");
5257 n->scheduler = tqueue->scheduler;
5263 queues->tail->next = n;
5267 if ((pf->opts & PF_OPT_VERBOSE) && (
5268 (found == 1 && interface->ifname[0] == 0) ||
5269 (found > 0 && interface->ifname[0] != 0))) {
5270 print_queue(&pf->paltq->altq, 0,
5271 &bwspec, interface->ifname[0] != 0,
5273 if (nqueues && nqueues->tail) {
5275 LOOP_THROUGH(struct node_queue,
5288 FREE_LIST(struct node_queue, nqueues);
5289 FREE_LIST(struct node_if, interfaces);
5292 yyerror("queue %s has no parent", a->qname);
5303 expand_rule(struct pfctl_rule *r,
5304 struct node_if *interfaces, struct node_host *rpool_hosts,
5305 struct node_proto *protos, struct node_os *src_oses,
5306 struct node_host *src_hosts, struct node_port *src_ports,
5307 struct node_host *dst_hosts, struct node_port *dst_ports,
5308 struct node_uid *uids, struct node_gid *gids, struct node_icmp *icmp_types,
5309 const char *anchor_call)
5311 sa_family_t af = r->af;
5312 int added = 0, error = 0;
5313 char ifname[IF_NAMESIZE];
5314 char label[PF_RULE_MAX_LABEL_COUNT][PF_RULE_LABEL_SIZE];
5315 char tagname[PF_TAG_NAME_SIZE];
5316 char match_tagname[PF_TAG_NAME_SIZE];
5317 struct pf_pooladdr *pa;
5318 struct node_host *h;
5319 u_int8_t flags, flagset, keep_state;
5321 memcpy(label, r->label, sizeof(r->label));
5322 assert(sizeof(r->label) == sizeof(label));
5323 if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname))
5324 errx(1, "expand_rule: strlcpy");
5325 if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >=
5326 sizeof(match_tagname))
5327 errx(1, "expand_rule: strlcpy");
5329 flagset = r->flagset;
5330 keep_state = r->keep_state;
5332 LOOP_THROUGH(struct node_if, interface, interfaces,
5333 LOOP_THROUGH(struct node_proto, proto, protos,
5334 LOOP_THROUGH(struct node_icmp, icmp_type, icmp_types,
5335 LOOP_THROUGH(struct node_host, src_host, src_hosts,
5336 LOOP_THROUGH(struct node_port, src_port, src_ports,
5337 LOOP_THROUGH(struct node_os, src_os, src_oses,
5338 LOOP_THROUGH(struct node_host, dst_host, dst_hosts,
5339 LOOP_THROUGH(struct node_port, dst_port, dst_ports,
5340 LOOP_THROUGH(struct node_uid, uid, uids,
5341 LOOP_THROUGH(struct node_gid, gid, gids,
5344 /* for link-local IPv6 address, interface must match up */
5345 if ((r->af && src_host->af && r->af != src_host->af) ||
5346 (r->af && dst_host->af && r->af != dst_host->af) ||
5347 (src_host->af && dst_host->af &&
5348 src_host->af != dst_host->af) ||
5349 (src_host->ifindex && dst_host->ifindex &&
5350 src_host->ifindex != dst_host->ifindex) ||
5351 (src_host->ifindex && *interface->ifname &&
5352 src_host->ifindex != if_nametoindex(interface->ifname)) ||
5353 (dst_host->ifindex && *interface->ifname &&
5354 dst_host->ifindex != if_nametoindex(interface->ifname)))
5356 if (!r->af && src_host->af)
5357 r->af = src_host->af;
5358 else if (!r->af && dst_host->af)
5359 r->af = dst_host->af;
5361 if (*interface->ifname)
5362 strlcpy(r->ifname, interface->ifname,
5364 else if (if_indextoname(src_host->ifindex, ifname))
5365 strlcpy(r->ifname, ifname, sizeof(r->ifname));
5366 else if (if_indextoname(dst_host->ifindex, ifname))
5367 strlcpy(r->ifname, ifname, sizeof(r->ifname));
5369 memset(r->ifname, '\0', sizeof(r->ifname));
5371 memcpy(r->label, label, sizeof(r->label));
5372 if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >=
5374 errx(1, "expand_rule: strlcpy");
5375 if (strlcpy(r->match_tagname, match_tagname,
5376 sizeof(r->match_tagname)) >= sizeof(r->match_tagname))
5377 errx(1, "expand_rule: strlcpy");
5378 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++)
5379 expand_label(r->label[i], PF_RULE_LABEL_SIZE,
5380 r->ifname, r->af, src_host, src_port, dst_host,
5381 dst_port, proto->proto);
5382 expand_label(r->tagname, PF_TAG_NAME_SIZE, r->ifname, r->af,
5383 src_host, src_port, dst_host, dst_port, proto->proto);
5384 expand_label(r->match_tagname, PF_TAG_NAME_SIZE, r->ifname,
5385 r->af, src_host, src_port, dst_host, dst_port,
5388 error += check_netmask(src_host, r->af);
5389 error += check_netmask(dst_host, r->af);
5391 r->ifnot = interface->not;
5392 r->proto = proto->proto;
5393 r->src.addr = src_host->addr;
5394 r->src.neg = src_host->not;
5395 r->src.port[0] = src_port->port[0];
5396 r->src.port[1] = src_port->port[1];
5397 r->src.port_op = src_port->op;
5398 r->dst.addr = dst_host->addr;
5399 r->dst.neg = dst_host->not;
5400 r->dst.port[0] = dst_port->port[0];
5401 r->dst.port[1] = dst_port->port[1];
5402 r->dst.port_op = dst_port->op;
5403 r->uid.op = uid->op;
5404 r->uid.uid[0] = uid->uid[0];
5405 r->uid.uid[1] = uid->uid[1];
5406 r->gid.op = gid->op;
5407 r->gid.gid[0] = gid->gid[0];
5408 r->gid.gid[1] = gid->gid[1];
5409 r->type = icmp_type->type;
5410 r->code = icmp_type->code;
5412 if ((keep_state == PF_STATE_MODULATE ||
5413 keep_state == PF_STATE_SYNPROXY) &&
5414 r->proto && r->proto != IPPROTO_TCP)
5415 r->keep_state = PF_STATE_NORMAL;
5417 r->keep_state = keep_state;
5419 if (r->proto && r->proto != IPPROTO_TCP) {
5424 r->flagset = flagset;
5426 if (icmp_type->proto && r->proto != icmp_type->proto) {
5427 yyerror("icmp-type mismatch");
5431 if (src_os && src_os->os) {
5432 r->os_fingerprint = pfctl_get_fingerprint(src_os->os);
5433 if ((pf->opts & PF_OPT_VERBOSE2) &&
5434 r->os_fingerprint == PF_OSFP_NOMATCH)
5436 "warning: unknown '%s' OS fingerprint\n",
5439 r->os_fingerprint = PF_OSFP_ANY;
5442 TAILQ_INIT(&r->rpool.list);
5443 for (h = rpool_hosts; h != NULL; h = h->next) {
5444 pa = calloc(1, sizeof(struct pf_pooladdr));
5446 err(1, "expand_rule: calloc");
5448 if (h->ifname != NULL) {
5449 if (strlcpy(pa->ifname, h->ifname,
5450 sizeof(pa->ifname)) >=
5452 errx(1, "expand_rule: strlcpy");
5455 TAILQ_INSERT_TAIL(&r->rpool.list, pa, entries);
5458 if (rule_consistent(r, anchor_call[0]) < 0 || error)
5459 yyerror("skipping rule due to errors");
5461 r->nr = pf->astack[pf->asd]->match++;
5462 pfctl_append_rule(pf, r, anchor_call);
5468 FREE_LIST(struct node_if, interfaces);
5469 FREE_LIST(struct node_proto, protos);
5470 FREE_LIST(struct node_host, src_hosts);
5471 FREE_LIST(struct node_port, src_ports);
5472 FREE_LIST(struct node_os, src_oses);
5473 FREE_LIST(struct node_host, dst_hosts);
5474 FREE_LIST(struct node_port, dst_ports);
5475 FREE_LIST(struct node_uid, uids);
5476 FREE_LIST(struct node_gid, gids);
5477 FREE_LIST(struct node_icmp, icmp_types);
5478 FREE_LIST(struct node_host, rpool_hosts);
5481 yyerror("rule expands to no valid combination");
5485 expand_skip_interface(struct node_if *interfaces)
5489 if (!interfaces || (!interfaces->next && !interfaces->not &&
5490 !strcmp(interfaces->ifname, "none"))) {
5491 if (pf->opts & PF_OPT_VERBOSE)
5492 printf("set skip on none\n");
5493 errs = pfctl_set_interface_flags(pf, "", PFI_IFLAG_SKIP, 0);
5497 if (pf->opts & PF_OPT_VERBOSE)
5498 printf("set skip on {");
5499 LOOP_THROUGH(struct node_if, interface, interfaces,
5500 if (pf->opts & PF_OPT_VERBOSE)
5501 printf(" %s", interface->ifname);
5502 if (interface->not) {
5503 yyerror("skip on ! <interface> is not supported");
5506 errs += pfctl_set_interface_flags(pf,
5507 interface->ifname, PFI_IFLAG_SKIP, 1);
5509 if (pf->opts & PF_OPT_VERBOSE)
5512 FREE_LIST(struct node_if, interfaces);
5524 check_rulestate(int desired_state)
5526 if (require_order && (rulestate > desired_state)) {
5527 yyerror("Rules must be in order: options, normalization, "
5528 "queueing, translation, filtering");
5531 rulestate = desired_state;
5536 kw_cmp(const void *k, const void *e)
5538 return (strcmp(k, ((const struct keywords *)e)->k_name));
5544 /* this has to be sorted always */
5545 static const struct keywords keywords[] = {
5547 { "allow-opts", ALLOWOPTS},
5549 { "anchor", ANCHOR},
5550 { "antispoof", ANTISPOOF},
5552 { "bandwidth", BANDWIDTH},
5554 { "binat-anchor", BINATANCHOR},
5555 { "bitmask", BITMASK},
5557 { "block-policy", BLOCKPOLICY},
5558 { "buckets", BUCKETS},
5562 { "crop", FRAGCROP},
5564 { "divert-reply", DIVERTREPLY},
5565 { "divert-to", DIVERTTO},
5567 { "drop-ovl", FRAGDROP},
5569 { "fail-policy", FAILPOLICY},
5571 { "fastroute", FASTROUTE},
5572 { "file", FILENAME},
5573 { "fingerprints", FINGERPRINTS},
5575 { "floating", FLOATING},
5578 { "fragment", FRAGMENT},
5580 { "global", GLOBAL},
5584 { "hostid", HOSTID},
5585 { "icmp-type", ICMPTYPE},
5586 { "icmp6-type", ICMP6TYPE},
5587 { "if-bound", IFBOUND},
5589 { "include", INCLUDE},
5592 { "interval", INTERVAL},
5596 { "linkshare", LINKSHARE},
5599 { "loginterface", LOGINTERFACE},
5600 { "map-e-portset", MAPEPORTSET},
5602 { "max-mss", MAXMSS},
5603 { "max-src-conn", MAXSRCCONN},
5604 { "max-src-conn-rate", MAXSRCCONNRATE},
5605 { "max-src-nodes", MAXSRCNODES},
5606 { "max-src-states", MAXSRCSTATES},
5607 { "min-ttl", MINTTL},
5608 { "modulate", MODULATE},
5610 { "nat-anchor", NATANCHOR},
5613 { "no-route", NOROUTE},
5614 { "no-sync", NOSYNC},
5616 { "optimization", OPTIMIZATION},
5619 { "overload", OVERLOAD},
5623 { "priority", PRIORITY},
5625 { "probability", PROBABILITY},
5627 { "qlimit", QLIMIT},
5630 { "random", RANDOM},
5631 { "random-id", RANDOMID},
5633 { "rdr-anchor", RDRANCHOR},
5634 { "realtime", REALTIME},
5635 { "reassemble", REASSEMBLE},
5636 { "reply-to", REPLYTO},
5637 { "require-order", REQUIREORDER},
5638 { "return", RETURN},
5639 { "return-icmp", RETURNICMP},
5640 { "return-icmp6", RETURNICMP6},
5641 { "return-rst", RETURNRST},
5642 { "round-robin", ROUNDROBIN},
5644 { "route-to", ROUTETO},
5645 { "rtable", RTABLE},
5647 { "ruleset-optimization", RULESET_OPTIMIZATION},
5650 { "set-tos", SETTOS},
5652 { "sloppy", SLOPPY},
5653 { "source-hash", SOURCEHASH},
5654 { "source-track", SOURCETRACK},
5656 { "state-defaults", STATEDEFAULTS},
5657 { "state-policy", STATEPOLICY},
5658 { "static-port", STATICPORT},
5659 { "sticky-address", STICKYADDRESS},
5660 { "synproxy", SYNPROXY},
5663 { "tagged", TAGGED},
5664 { "target", TARGET},
5665 { "tbrsize", TBRSIZE},
5666 { "timeout", TIMEOUT},
5670 { "upperlimit", UPPERLIMIT},
5671 { "urpf-failed", URPFFAILED},
5674 const struct keywords *p;
5676 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
5677 sizeof(keywords[0]), kw_cmp);
5681 fprintf(stderr, "%s: %d\n", s, p->k_val);
5685 fprintf(stderr, "string: %s\n", s);
5690 #define MAXPUSHBACK 128
5692 static char *parsebuf;
5693 static int parseindex;
5694 static char pushback_buffer[MAXPUSHBACK];
5695 static int pushback_index = 0;
5703 /* Read character from the parsebuffer instead of input. */
5704 if (parseindex >= 0) {
5705 c = parsebuf[parseindex++];
5714 return (pushback_buffer[--pushback_index]);
5717 if ((c = getc(file->stream)) == EOF) {
5718 yyerror("reached end of file while parsing quoted string");
5719 if (popfile() == EOF)
5726 while ((c = getc(file->stream)) == '\\') {
5727 next = getc(file->stream);
5732 yylval.lineno = file->lineno;
5737 if (popfile() == EOF)
5739 c = getc(file->stream);
5751 if (parseindex >= 0)
5754 if (pushback_index < MAXPUSHBACK-1)
5755 return (pushback_buffer[pushback_index++] = c);
5767 /* skip to either EOF or the first real EOL */
5770 c = pushback_buffer[--pushback_index];
5788 int quotec, next, c;
5793 while ((c = lgetc(0)) == ' ' || c == '\t')
5796 yylval.lineno = file->lineno;
5798 while ((c = lgetc(0)) != '\n' && c != EOF)
5800 if (c == '$' && parsebuf == NULL) {
5802 if ((c = lgetc(0)) == EOF)
5805 if (p + 1 >= buf + sizeof(buf) - 1) {
5806 yyerror("string too long");
5809 if (isalnum(c) || c == '_') {
5819 yyerror("macro '%s' not defined", buf);
5832 if ((c = lgetc(quotec)) == EOF)
5837 } else if (c == '\\') {
5838 if ((next = lgetc(quotec)) == EOF)
5840 if (next == quotec || c == ' ' || c == '\t')
5842 else if (next == '\n') {
5848 } else if (c == quotec) {
5852 if (p + 1 >= buf + sizeof(buf) - 1) {
5853 yyerror("string too long");
5858 yylval.v.string = strdup(buf);
5859 if (yylval.v.string == NULL)
5860 err(1, "yylex: strdup");
5865 yylval.v.i = PF_OP_XRG;
5866 return (PORTBINARY);
5873 yylval.v.i = PF_OP_IRG;
5874 return (PORTBINARY);
5886 #define allowed_to_end_number(x) \
5887 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
5889 if (c == '-' || isdigit(c)) {
5892 if ((unsigned)(p-buf) >= sizeof(buf)) {
5893 yyerror("string too long");
5896 } while ((c = lgetc(0)) != EOF && isdigit(c));
5898 if (p == buf + 1 && buf[0] == '-')
5900 if (c == EOF || allowed_to_end_number(c)) {
5901 const char *errstr = NULL;
5904 yylval.v.number = strtonum(buf, LLONG_MIN,
5905 LLONG_MAX, &errstr);
5907 yyerror("\"%s\" invalid number: %s",
5922 #define allowed_in_string(x) \
5923 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
5924 x != '{' && x != '}' && x != '<' && x != '>' && \
5925 x != '!' && x != '=' && x != '/' && x != '#' && \
5928 if (isalnum(c) || c == ':' || c == '_') {
5931 if ((unsigned)(p-buf) >= sizeof(buf)) {
5932 yyerror("string too long");
5935 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
5938 if ((token = lookup(buf)) == STRING)
5939 if ((yylval.v.string = strdup(buf)) == NULL)
5940 err(1, "yylex: strdup");
5944 yylval.lineno = file->lineno;
5953 check_file_secrecy(int fd, const char *fname)
5957 if (fstat(fd, &st)) {
5958 warn("cannot stat %s", fname);
5961 if (st.st_uid != 0 && st.st_uid != getuid()) {
5962 warnx("%s: owner not root or current user", fname);
5965 if (st.st_mode & (S_IRWXG | S_IRWXO)) {
5966 warnx("%s: group/world readable/writeable", fname);
5973 pushfile(const char *name, int secret)
5977 if ((nfile = calloc(1, sizeof(struct file))) == NULL ||
5978 (nfile->name = strdup(name)) == NULL) {
5982 if (TAILQ_FIRST(&files) == NULL && strcmp(nfile->name, "-") == 0) {
5983 nfile->stream = stdin;
5985 if ((nfile->name = strdup("stdin")) == NULL) {
5990 } else if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
5991 warn("%s", nfile->name);
5995 } else if (secret &&
5996 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
5997 fclose(nfile->stream);
6003 TAILQ_INSERT_TAIL(&files, nfile, entry);
6012 if ((prev = TAILQ_PREV(file, files, entry)) != NULL) {
6013 prev->errors += file->errors;
6014 TAILQ_REMOVE(&files, file, entry);
6015 fclose(file->stream);
6025 parse_config(char *filename, struct pfctl *xpf)
6032 rulestate = PFCTL_STATE_NONE;
6033 returnicmpdefault = (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
6034 returnicmp6default =
6035 (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
6036 blockpolicy = PFRULE_DROP;
6037 failpolicy = PFRULE_DROP;
6040 if ((file = pushfile(filename, 0)) == NULL) {
6041 warn("cannot open the main config file!");
6046 errors = file->errors;
6049 /* Free macros and check which have not been used. */
6050 while ((sym = TAILQ_FIRST(&symhead))) {
6051 if ((pf->opts & PF_OPT_VERBOSE2) && !sym->used)
6052 fprintf(stderr, "warning: macro '%s' not "
6053 "used\n", sym->nam);
6056 TAILQ_REMOVE(&symhead, sym, entry);
6060 return (errors ? -1 : 0);
6064 symset(const char *nam, const char *val, int persist)
6068 for (sym = TAILQ_FIRST(&symhead); sym && strcmp(nam, sym->nam);
6069 sym = TAILQ_NEXT(sym, entry))
6073 if (sym->persist == 1)
6078 TAILQ_REMOVE(&symhead, sym, entry);
6082 if ((sym = calloc(1, sizeof(*sym))) == NULL)
6085 sym->nam = strdup(nam);
6086 if (sym->nam == NULL) {
6090 sym->val = strdup(val);
6091 if (sym->val == NULL) {
6097 sym->persist = persist;
6098 TAILQ_INSERT_TAIL(&symhead, sym, entry);
6103 pfctl_cmdline_symset(char *s)
6108 if ((val = strrchr(s, '=')) == NULL)
6111 if ((sym = malloc(strlen(s) - strlen(val) + 1)) == NULL)
6112 err(1, "pfctl_cmdline_symset: malloc");
6114 strlcpy(sym, s, strlen(s) - strlen(val) + 1);
6116 ret = symset(sym, val + 1, 1);
6123 symget(const char *nam)
6127 TAILQ_FOREACH(sym, &symhead, entry)
6128 if (strcmp(nam, sym->nam) == 0) {
6136 mv_rules(struct pfctl_ruleset *src, struct pfctl_ruleset *dst)
6139 struct pfctl_rule *r;
6141 for (i = 0; i < PF_RULESET_MAX; ++i) {
6142 while ((r = TAILQ_FIRST(src->rules[i].active.ptr))
6144 TAILQ_REMOVE(src->rules[i].active.ptr, r, entries);
6145 TAILQ_INSERT_TAIL(dst->rules[i].active.ptr, r, entries);
6146 dst->anchor->match++;
6148 src->anchor->match = 0;
6149 while ((r = TAILQ_FIRST(src->rules[i].inactive.ptr))
6151 TAILQ_REMOVE(src->rules[i].inactive.ptr, r, entries);
6152 TAILQ_INSERT_TAIL(dst->rules[i].inactive.ptr,
6159 decide_address_family(struct node_host *n, sa_family_t *af)
6161 if (*af != 0 || n == NULL)
6164 while ((n = n->next) != NULL) {
6173 remove_invalid_hosts(struct node_host **nh, sa_family_t *af)
6175 struct node_host *n = *nh, *prev = NULL;
6178 if (*af && n->af && n->af != *af) {
6179 /* unlink and free n */
6180 struct node_host *next = n->next;
6182 /* adjust tail pointer */
6183 if (n == (*nh)->tail)
6185 /* adjust previous node's next pointer */
6191 if (n->ifname != NULL)
6205 invalid_redirect(struct node_host *nh, sa_family_t af)
6208 struct node_host *n;
6210 /* tables and dyniftl are ok without an address family */
6211 for (n = nh; n != NULL; n = n->next) {
6212 if (n->addr.type != PF_ADDR_TABLE &&
6213 n->addr.type != PF_ADDR_DYNIFTL) {
6214 yyerror("address family not given and "
6215 "translation address expands to multiple "
6216 "address families");
6222 yyerror("no translation address with matching address family "
6230 atoul(char *s, u_long *ulvalp)
6236 ulval = strtoul(s, &ep, 0);
6237 if (s[0] == '\0' || *ep != '\0')
6239 if (errno == ERANGE && ulval == ULONG_MAX)
6251 if (atoul(n, &ulval) == 0) {
6252 if (ulval > 65535) {
6253 yyerror("illegal port value %lu", ulval);
6256 return (htons(ulval));
6258 s = getservbyname(n, "tcp");
6260 s = getservbyname(n, "udp");
6262 yyerror("unknown port %s", n);
6270 rule_label(struct pfctl_rule *r, char *s[PF_RULE_MAX_LABEL_COUNT])
6272 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++) {
6276 if (strlcpy(r->label[i], s[i], sizeof(r->label[0])) >=
6277 sizeof(r->label[0])) {
6278 yyerror("rule label too long (max %d chars)",
6279 sizeof(r->label[0])-1);
6287 parseicmpspec(char *w, sa_family_t af)
6289 const struct icmpcodeent *p;
6294 icmptype = returnicmpdefault >> 8;
6296 icmptype = returnicmp6default >> 8;
6298 if (atoul(w, &ulval) == -1) {
6299 if ((p = geticmpcodebyname(icmptype, w, af)) == NULL) {
6300 yyerror("unknown icmp code %s", w);
6306 yyerror("invalid icmp code %lu", ulval);
6309 return (icmptype << 8 | ulval);
6313 parseport(char *port, struct range *r, int extensions)
6315 char *p = strchr(port, ':');
6318 if ((r->a = getservice(port)) == -1)
6324 if ((extensions & PPORT_STAR) && !strcmp(p+1, "*")) {
6326 if ((r->a = getservice(port)) == -1)
6332 if ((extensions & PPORT_RANGE)) {
6334 if ((r->a = getservice(port)) == -1 ||
6335 (r->b = getservice(p)) == -1)
6348 pfctl_load_anchors(int dev, struct pfctl *pf, struct pfr_buffer *trans)
6350 struct loadanchors *la;
6352 TAILQ_FOREACH(la, &loadanchorshead, entries) {
6353 if (pf->opts & PF_OPT_VERBOSE)
6354 fprintf(stderr, "\nLoading anchor %s from %s\n",
6355 la->anchorname, la->filename);
6356 if (pfctl_rules(dev, la->filename, pf->opts, pf->optimize,
6357 la->anchorname, trans) == -1)
6365 kw_casecmp(const void *k, const void *e)
6367 return (strcasecmp(k, ((const struct keywords *)e)->k_name));
6371 map_tos(char *s, int *val)
6373 /* DiffServ Codepoints and other TOS mappings */
6374 const struct keywords toswords[] = {
6375 { "af11", IPTOS_DSCP_AF11 },
6376 { "af12", IPTOS_DSCP_AF12 },
6377 { "af13", IPTOS_DSCP_AF13 },
6378 { "af21", IPTOS_DSCP_AF21 },
6379 { "af22", IPTOS_DSCP_AF22 },
6380 { "af23", IPTOS_DSCP_AF23 },
6381 { "af31", IPTOS_DSCP_AF31 },
6382 { "af32", IPTOS_DSCP_AF32 },
6383 { "af33", IPTOS_DSCP_AF33 },
6384 { "af41", IPTOS_DSCP_AF41 },
6385 { "af42", IPTOS_DSCP_AF42 },
6386 { "af43", IPTOS_DSCP_AF43 },
6387 { "critical", IPTOS_PREC_CRITIC_ECP },
6388 { "cs0", IPTOS_DSCP_CS0 },
6389 { "cs1", IPTOS_DSCP_CS1 },
6390 { "cs2", IPTOS_DSCP_CS2 },
6391 { "cs3", IPTOS_DSCP_CS3 },
6392 { "cs4", IPTOS_DSCP_CS4 },
6393 { "cs5", IPTOS_DSCP_CS5 },
6394 { "cs6", IPTOS_DSCP_CS6 },
6395 { "cs7", IPTOS_DSCP_CS7 },
6396 { "ef", IPTOS_DSCP_EF },
6397 { "inetcontrol", IPTOS_PREC_INTERNETCONTROL },
6398 { "lowdelay", IPTOS_LOWDELAY },
6399 { "netcontrol", IPTOS_PREC_NETCONTROL },
6400 { "reliability", IPTOS_RELIABILITY },
6401 { "throughput", IPTOS_THROUGHPUT },
6402 { "va", IPTOS_DSCP_VA }
6404 const struct keywords *p;
6406 p = bsearch(s, toswords, sizeof(toswords)/sizeof(toswords[0]),
6407 sizeof(toswords[0]), kw_casecmp);
6417 rt_tableid_max(void)
6421 size_t l = sizeof(fibs);
6423 if (sysctlbyname("net.fibs", &fibs, &l, NULL, 0) == -1)
6424 fibs = 16; /* XXX RT_MAXFIBS, at least limit it some. */
6426 * As the OpenBSD code only compares > and not >= we need to adjust
6427 * here given we only accept values of 0..n and want to avoid #ifdefs
6432 return (RT_TABLEID_MAX);