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 MATCH SCRUB RETURN IN OS OUT LOG QUICK ON FROM TO FLAGS
460 %token RETURNRST RETURNICMP RETURNICMP6 PROTO INET INET6 ALL ANY ICMPTYPE
461 %token ICMP6TYPE CODE KEEP MODULATE STATE PORT RDR NAT BINAT ARROW NODF
462 %token MINTTL ERROR ALLOWOPTS FASTROUTE FILENAME ROUTETO DUPTO REPLYTO NO LABEL
463 %token NOROUTE URPFFAILED FRAGMENT USER GROUP MAXMSS MAXIMUM TTL TOS DROP TABLE
464 %token REASSEMBLE FRAGDROP FRAGCROP ANCHOR NATANCHOR RDRANCHOR BINATANCHOR
465 %token SET OPTIMIZATION TIMEOUT LIMIT LOGINTERFACE BLOCKPOLICY FAILPOLICY
466 %token RANDOMID REQUIREORDER SYNPROXY FINGERPRINTS NOSYNC DEBUG SKIP HOSTID
467 %token ANTISPOOF FOR INCLUDE KEEPCOUNTERS SYNCOOKIES
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 syncookie_val
484 %type <v.i> sourcetrack flush unaryop statelock
485 %type <v.b> action nataction natpasslog scrubaction
486 %type <v.b> flags flag blockspec prio
487 %type <v.range> portplain portstar portrange
488 %type <v.hashkey> hashkey
489 %type <v.proto> proto proto_list proto_item
490 %type <v.number> protoval
491 %type <v.icmp> icmpspec
492 %type <v.icmp> icmp_list icmp_item
493 %type <v.icmp> icmp6_list icmp6_item
494 %type <v.number> reticmpspec reticmp6spec
495 %type <v.fromto> fromto
496 %type <v.peer> ipportspec from to
497 %type <v.host> ipspec toipspec xhost host dynaddr host_list
498 %type <v.host> redir_host_list redirspec
499 %type <v.host> route_host route_host_list routespec
500 %type <v.os> os xos os_list
501 %type <v.port> portspec port_list port_item
502 %type <v.uid> uids uid_list uid_item
503 %type <v.gid> gids gid_list gid_item
504 %type <v.route> route
505 %type <v.redirection> redirection redirpool
506 %type <v.string> label stringall tag anchorname
507 %type <v.string> string varstring numberstring
508 %type <v.keep_state> keep
509 %type <v.state_opt> state_opt_spec state_opt_list state_opt_item
510 %type <v.logquick> logquick quick log logopts logopt
511 %type <v.interface> antispoof_ifspc antispoof_iflst antispoof_if
512 %type <v.qassign> qname
513 %type <v.queue> qassign qassign_list qassign_item
514 %type <v.queue_options> scheduler
515 %type <v.number> cbqflags_list cbqflags_item
516 %type <v.number> priqflags_list priqflags_item
517 %type <v.hfsc_opts> hfscopts_list hfscopts_item hfsc_opts
518 %type <v.fairq_opts> fairqopts_list fairqopts_item fairq_opts
519 %type <v.codel_opts> codelopts_list codelopts_item codel_opts
520 %type <v.queue_bwspec> bandwidth
521 %type <v.filter_opts> filter_opts filter_opt filter_opts_l
522 %type <v.filter_opts> filter_sets filter_set filter_sets_l
523 %type <v.antispoof_opts> antispoof_opts antispoof_opt antispoof_opts_l
524 %type <v.queue_opts> queue_opts queue_opt queue_opts_l
525 %type <v.scrub_opts> scrub_opts scrub_opt scrub_opts_l
526 %type <v.table_opts> table_opts table_opt table_opts_l
527 %type <v.pool_opts> pool_opts pool_opt pool_opts_l
528 %type <v.tagged> tagged
529 %type <v.rtableid> rtable
532 ruleset : /* empty */
533 | ruleset include '\n'
535 | ruleset option '\n'
536 | ruleset scrubrule '\n'
537 | ruleset natrule '\n'
538 | ruleset binatrule '\n'
539 | ruleset pfrule '\n'
540 | ruleset anchorrule '\n'
541 | ruleset loadrule '\n'
542 | ruleset altqif '\n'
543 | ruleset queuespec '\n'
544 | ruleset varset '\n'
545 | ruleset antispoof '\n'
546 | ruleset tabledef '\n'
547 | '{' fakeanchor '}' '\n';
548 | ruleset error '\n' { file->errors++; }
551 include : INCLUDE STRING {
554 if ((nfile = pushfile($2, 0)) == NULL) {
555 yyerror("failed to include file %s", $2);
567 * apply to previouslys specified rule: must be careful to note
568 * what that is: pf or nat or binat or rdr
570 fakeanchor : fakeanchor '\n'
571 | fakeanchor anchorrule '\n'
572 | fakeanchor binatrule '\n'
573 | fakeanchor natrule '\n'
574 | fakeanchor pfrule '\n'
575 | fakeanchor error '\n'
579 if (!strcmp($1, "none"))
581 else if (!strcmp($1, "basic"))
582 $$ = PF_OPTIMIZE_BASIC;
583 else if (!strcmp($1, "profile"))
584 $$ = PF_OPTIMIZE_BASIC | PF_OPTIMIZE_PROFILE;
586 yyerror("unknown ruleset-optimization %s", $1);
592 option : SET OPTIMIZATION STRING {
593 if (check_rulestate(PFCTL_STATE_OPTION)) {
597 if (pfctl_set_optimization(pf, $3) != 0) {
598 yyerror("unknown optimization %s", $3);
604 | SET RULESET_OPTIMIZATION optimizer {
605 if (!(pf->opts & PF_OPT_OPTIMIZE)) {
606 pf->opts |= PF_OPT_OPTIMIZE;
610 | SET TIMEOUT timeout_spec
611 | SET TIMEOUT '{' optnl timeout_list '}'
612 | SET LIMIT limit_spec
613 | SET LIMIT '{' optnl limit_list '}'
614 | SET LOGINTERFACE stringall {
615 if (check_rulestate(PFCTL_STATE_OPTION)) {
619 if (pfctl_set_logif(pf, $3) != 0) {
620 yyerror("error setting loginterface %s", $3);
626 | SET HOSTID number {
627 if ($3 == 0 || $3 > UINT_MAX) {
628 yyerror("hostid must be non-zero");
631 if (pfctl_set_hostid(pf, $3) != 0) {
632 yyerror("error setting hostid %08x", $3);
636 | SET BLOCKPOLICY DROP {
637 if (pf->opts & PF_OPT_VERBOSE)
638 printf("set block-policy drop\n");
639 if (check_rulestate(PFCTL_STATE_OPTION))
641 blockpolicy = PFRULE_DROP;
643 | SET BLOCKPOLICY RETURN {
644 if (pf->opts & PF_OPT_VERBOSE)
645 printf("set block-policy return\n");
646 if (check_rulestate(PFCTL_STATE_OPTION))
648 blockpolicy = PFRULE_RETURN;
650 | SET FAILPOLICY DROP {
651 if (pf->opts & PF_OPT_VERBOSE)
652 printf("set fail-policy drop\n");
653 if (check_rulestate(PFCTL_STATE_OPTION))
655 failpolicy = PFRULE_DROP;
657 | SET FAILPOLICY RETURN {
658 if (pf->opts & PF_OPT_VERBOSE)
659 printf("set fail-policy return\n");
660 if (check_rulestate(PFCTL_STATE_OPTION))
662 failpolicy = PFRULE_RETURN;
664 | SET REQUIREORDER yesno {
665 if (pf->opts & PF_OPT_VERBOSE)
666 printf("set require-order %s\n",
667 $3 == 1 ? "yes" : "no");
670 | SET FINGERPRINTS STRING {
671 if (pf->opts & PF_OPT_VERBOSE)
672 printf("set fingerprints \"%s\"\n", $3);
673 if (check_rulestate(PFCTL_STATE_OPTION)) {
677 if (!pf->anchor->name[0]) {
678 if (pfctl_file_fingerprints(pf->dev,
680 yyerror("error loading "
681 "fingerprints %s", $3);
688 | SET STATEPOLICY statelock {
689 if (pf->opts & PF_OPT_VERBOSE)
692 printf("set state-policy floating\n");
695 printf("set state-policy if-bound\n");
698 default_statelock = $3;
701 if (check_rulestate(PFCTL_STATE_OPTION)) {
705 if (pfctl_set_debug(pf, $3) != 0) {
706 yyerror("error setting debuglevel %s", $3);
712 | SET SKIP interface {
713 if (expand_skip_interface($3) != 0) {
714 yyerror("error setting skip interface(s)");
718 | SET STATEDEFAULTS state_opt_list {
719 if (keep_state_defaults != NULL) {
720 yyerror("cannot redefine state-defaults");
723 keep_state_defaults = $3;
726 pf->keep_counters = true;
728 | SET SYNCOOKIES syncookie_val {
733 syncookie_val : STRING {
734 if (!strcmp($1, "never"))
735 $$ = PFCTL_SYNCOOKIES_NEVER;
736 else if (!strcmp($1, "always"))
737 $$ = PFCTL_SYNCOOKIES_ALWAYS;
739 yyerror("illegal value for syncookies");
745 stringall : STRING { $$ = $1; }
747 if (($$ = strdup("all")) == NULL) {
748 err(1, "stringall: strdup");
753 string : STRING string {
754 if (asprintf(&$$, "%s %s", $1, $2) == -1)
755 err(1, "string: asprintf");
762 varstring : numberstring varstring {
763 if (asprintf(&$$, "%s %s", $1, $2) == -1)
764 err(1, "string: asprintf");
771 numberstring : NUMBER {
773 if (asprintf(&s, "%lld", (long long)$1) == -1) {
774 yyerror("string: asprintf");
782 varset : STRING '=' varstring {
784 if (pf->opts & PF_OPT_VERBOSE)
785 printf("%s = \"%s\"\n", $1, $3);
787 if (isspace((unsigned char)*s)) {
788 yyerror("macro name cannot contain "
793 if (symset($1, $3, 0) == -1)
794 err(1, "cannot store variable %s", $1);
800 anchorname : STRING { $$ = $1; }
801 | /* empty */ { $$ = NULL; }
804 pfa_anchorlist : /* empty */
805 | pfa_anchorlist '\n'
806 | pfa_anchorlist pfrule '\n'
807 | pfa_anchorlist anchorrule '\n'
812 char ta[PF_ANCHOR_NAME_SIZE];
813 struct pfctl_ruleset *rs;
815 /* steping into a brace anchor */
820 /* create a holding ruleset in the root */
821 snprintf(ta, PF_ANCHOR_NAME_SIZE, "_%d", pf->bn);
822 rs = pf_find_or_create_ruleset(ta);
824 err(1, "pfa_anchor: pf_find_or_create_ruleset");
825 pf->astack[pf->asd] = rs->anchor;
826 pf->anchor = rs->anchor;
827 } '\n' pfa_anchorlist '}'
829 pf->alast = pf->anchor;
831 pf->anchor = pf->astack[pf->asd];
836 anchorrule : ANCHOR anchorname dir quick interface af proto fromto
837 filter_opts pfa_anchor
840 struct node_proto *proto;
842 if (check_rulestate(PFCTL_STATE_FILTER)) {
848 if ($2 && ($2[0] == '_' || strstr($2, "/_") != NULL)) {
850 yyerror("anchor names beginning with '_' "
851 "are reserved for internal use");
855 memset(&r, 0, sizeof(r));
856 if (pf->astack[pf->asd + 1]) {
857 /* move inline rules into relative location */
858 pfctl_anchor_setup(&r,
859 &pf->astack[pf->asd]->ruleset,
860 $2 ? $2 : pf->alast->name);
862 if (r.anchor == NULL)
863 err(1, "anchorrule: unable to "
866 if (pf->alast != r.anchor) {
867 if (r.anchor->match) {
868 yyerror("inline anchor '%s' "
873 mv_rules(&pf->alast->ruleset,
876 pf_remove_if_empty_ruleset(&pf->alast->ruleset);
877 pf->alast = r.anchor;
880 yyerror("anchors without explicit "
881 "rules must specify a name");
889 r.rtableid = $9.rtableid;
892 if (strlcpy(r.tagname, $9.tag,
893 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
894 yyerror("tag too long, max %u chars",
895 PF_TAG_NAME_SIZE - 1);
899 if (strlcpy(r.match_tagname, $9.match_tag,
900 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
901 yyerror("tag too long, max %u chars",
902 PF_TAG_NAME_SIZE - 1);
905 r.match_tag_not = $9.match_tag_not;
906 if (rule_label(&r, $9.label))
908 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++)
910 r.flags = $9.flags.b1;
911 r.flagset = $9.flags.b2;
912 if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
913 yyerror("flags always false");
916 if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
917 for (proto = $7; proto != NULL &&
918 proto->proto != IPPROTO_TCP;
921 if (proto == NULL && $7 != NULL) {
922 if ($9.flags.b1 || $9.flags.b2)
924 "flags only apply to tcp");
927 "OS fingerprinting only "
935 if ($9.keep.action) {
936 yyerror("cannot specify state handling "
942 if (strlcpy(r.match_tagname, $9.match_tag,
943 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
944 yyerror("tag too long, max %u chars",
945 PF_TAG_NAME_SIZE - 1);
948 r.match_tag_not = $9.match_tag_not;
949 if ($9.marker & FOM_PRIO) {
951 r.prio = PF_PRIO_ZERO;
955 if ($9.marker & FOM_SETPRIO) {
956 r.set_prio[0] = $9.set_prio[0];
957 r.set_prio[1] = $9.set_prio[1];
958 r.scrub_flags |= PFSTATE_SETPRIO;
961 decide_address_family($8.src.host, &r.af);
962 decide_address_family($8.dst.host, &r.af);
964 expand_rule(&r, $5, NULL, $7, $8.src_os,
965 $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
966 $9.uid, $9.gid, $9.icmpspec,
967 pf->astack[pf->asd + 1] ? pf->alast->name : $2);
969 pf->astack[pf->asd + 1] = NULL;
971 | NATANCHOR string interface af proto fromto rtable {
974 if (check_rulestate(PFCTL_STATE_NAT)) {
979 memset(&r, 0, sizeof(r));
984 decide_address_family($6.src.host, &r.af);
985 decide_address_family($6.dst.host, &r.af);
987 expand_rule(&r, $3, NULL, $5, $6.src_os,
988 $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
992 | RDRANCHOR string interface af proto fromto rtable {
995 if (check_rulestate(PFCTL_STATE_NAT)) {
1000 memset(&r, 0, sizeof(r));
1005 decide_address_family($6.src.host, &r.af);
1006 decide_address_family($6.dst.host, &r.af);
1008 if ($6.src.port != NULL) {
1009 yyerror("source port parameter not supported"
1013 if ($6.dst.port != NULL) {
1014 if ($6.dst.port->next != NULL) {
1015 yyerror("destination port list "
1016 "expansion not supported in "
1019 } else if ($6.dst.port->op != PF_OP_EQ) {
1020 yyerror("destination port operators"
1021 " not supported in rdr-anchor");
1024 r.dst.port[0] = $6.dst.port->port[0];
1025 r.dst.port[1] = $6.dst.port->port[1];
1026 r.dst.port_op = $6.dst.port->op;
1029 expand_rule(&r, $3, NULL, $5, $6.src_os,
1030 $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
1034 | BINATANCHOR string interface af proto fromto rtable {
1035 struct pfctl_rule r;
1037 if (check_rulestate(PFCTL_STATE_NAT)) {
1042 memset(&r, 0, sizeof(r));
1043 r.action = PF_BINAT;
1047 if ($5->next != NULL) {
1048 yyerror("proto list expansion"
1049 " not supported in binat-anchor");
1052 r.proto = $5->proto;
1056 if ($6.src.host != NULL || $6.src.port != NULL ||
1057 $6.dst.host != NULL || $6.dst.port != NULL) {
1058 yyerror("fromto parameter not supported"
1059 " in binat-anchor");
1063 decide_address_family($6.src.host, &r.af);
1064 decide_address_family($6.dst.host, &r.af);
1066 pfctl_append_rule(pf, &r, $2);
1071 loadrule : LOAD ANCHOR string FROM string {
1072 struct loadanchors *loadanchor;
1074 if (strlen(pf->anchor->name) + 1 +
1075 strlen($3) >= MAXPATHLEN) {
1076 yyerror("anchorname %s too long, max %u\n",
1077 $3, MAXPATHLEN - 1);
1081 loadanchor = calloc(1, sizeof(struct loadanchors));
1082 if (loadanchor == NULL)
1083 err(1, "loadrule: calloc");
1084 if ((loadanchor->anchorname = malloc(MAXPATHLEN)) ==
1086 err(1, "loadrule: malloc");
1087 if (pf->anchor->name[0])
1088 snprintf(loadanchor->anchorname, MAXPATHLEN,
1089 "%s/%s", pf->anchor->name, $3);
1091 strlcpy(loadanchor->anchorname, $3, MAXPATHLEN);
1092 if ((loadanchor->filename = strdup($5)) == NULL)
1093 err(1, "loadrule: strdup");
1095 TAILQ_INSERT_TAIL(&loadanchorshead, loadanchor,
1102 scrubaction : no SCRUB {
1111 scrubrule : scrubaction dir logquick interface af proto fromto scrub_opts
1113 struct pfctl_rule r;
1115 if (check_rulestate(PFCTL_STATE_SCRUB))
1118 memset(&r, 0, sizeof(r));
1126 yyerror("scrub rules do not support 'quick'");
1132 r.rule_flag |= PFRULE_NODF;
1134 r.rule_flag |= PFRULE_RANDOMID;
1135 if ($8.reassemble_tcp) {
1136 if (r.direction != PF_INOUT) {
1137 yyerror("reassemble tcp rules can not "
1138 "specify direction");
1141 r.rule_flag |= PFRULE_REASSEMBLE_TCP;
1144 r.min_ttl = $8.minttl;
1146 r.max_mss = $8.maxmss;
1147 if ($8.marker & SOM_SETTOS) {
1148 r.rule_flag |= PFRULE_SET_TOS;
1149 r.set_tos = $8.settos;
1152 r.rule_flag |= $8.fragcache;
1154 if (strlcpy(r.match_tagname, $8.match_tag,
1155 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1156 yyerror("tag too long, max %u chars",
1157 PF_TAG_NAME_SIZE - 1);
1160 r.match_tag_not = $8.match_tag_not;
1161 r.rtableid = $8.rtableid;
1163 expand_rule(&r, $4, NULL, $6, $7.src_os,
1164 $7.src.host, $7.src.port, $7.dst.host, $7.dst.port,
1165 NULL, NULL, NULL, "");
1170 bzero(&scrub_opts, sizeof scrub_opts);
1171 scrub_opts.rtableid = -1;
1174 { $$ = scrub_opts; }
1176 bzero(&scrub_opts, sizeof scrub_opts);
1177 scrub_opts.rtableid = -1;
1182 scrub_opts_l : scrub_opts_l scrub_opt
1187 if (scrub_opts.nodf) {
1188 yyerror("no-df cannot be respecified");
1191 scrub_opts.nodf = 1;
1194 if (scrub_opts.marker & SOM_MINTTL) {
1195 yyerror("min-ttl cannot be respecified");
1198 if ($2 < 0 || $2 > 255) {
1199 yyerror("illegal min-ttl value %d", $2);
1202 scrub_opts.marker |= SOM_MINTTL;
1203 scrub_opts.minttl = $2;
1206 if (scrub_opts.marker & SOM_MAXMSS) {
1207 yyerror("max-mss cannot be respecified");
1210 if ($2 < 0 || $2 > 65535) {
1211 yyerror("illegal max-mss value %d", $2);
1214 scrub_opts.marker |= SOM_MAXMSS;
1215 scrub_opts.maxmss = $2;
1218 if (scrub_opts.marker & SOM_SETTOS) {
1219 yyerror("set-tos cannot be respecified");
1222 scrub_opts.marker |= SOM_SETTOS;
1223 scrub_opts.settos = $2;
1226 if (scrub_opts.marker & SOM_FRAGCACHE) {
1227 yyerror("fragcache cannot be respecified");
1230 scrub_opts.marker |= SOM_FRAGCACHE;
1231 scrub_opts.fragcache = $1;
1233 | REASSEMBLE STRING {
1234 if (strcasecmp($2, "tcp") != 0) {
1235 yyerror("scrub reassemble supports only tcp, "
1241 if (scrub_opts.reassemble_tcp) {
1242 yyerror("reassemble tcp cannot be respecified");
1245 scrub_opts.reassemble_tcp = 1;
1248 if (scrub_opts.randomid) {
1249 yyerror("random-id cannot be respecified");
1252 scrub_opts.randomid = 1;
1255 if ($2 < 0 || $2 > rt_tableid_max()) {
1256 yyerror("invalid rtable id");
1259 scrub_opts.rtableid = $2;
1261 | not TAGGED string {
1262 scrub_opts.match_tag = $3;
1263 scrub_opts.match_tag_not = $1;
1267 fragcache : FRAGMENT REASSEMBLE { $$ = 0; /* default */ }
1268 | FRAGMENT FRAGCROP { $$ = 0; }
1269 | FRAGMENT FRAGDROP { $$ = 0; }
1272 antispoof : ANTISPOOF logquick antispoof_ifspc af antispoof_opts {
1273 struct pfctl_rule r;
1274 struct node_host *h = NULL, *hh;
1275 struct node_if *i, *j;
1277 if (check_rulestate(PFCTL_STATE_FILTER))
1280 for (i = $3; i; i = i->next) {
1281 bzero(&r, sizeof(r));
1284 r.direction = PF_IN;
1289 if (rule_label(&r, $5.label))
1291 r.rtableid = $5.rtableid;
1292 j = calloc(1, sizeof(struct node_if));
1294 err(1, "antispoof: calloc");
1295 if (strlcpy(j->ifname, i->ifname,
1296 sizeof(j->ifname)) >= sizeof(j->ifname)) {
1298 yyerror("interface name too long");
1303 h = calloc(1, sizeof(*h));
1305 err(1, "address: calloc");
1306 h->addr.type = PF_ADDR_DYNIFTL;
1308 if (strlcpy(h->addr.v.ifname, i->ifname,
1309 sizeof(h->addr.v.ifname)) >=
1310 sizeof(h->addr.v.ifname)) {
1313 "interface name too long");
1316 hh = malloc(sizeof(*hh));
1318 err(1, "address: malloc");
1319 bcopy(h, hh, sizeof(*hh));
1320 h->addr.iflags = PFI_AFLAG_NETWORK;
1322 h = ifa_lookup(j->ifname,
1328 expand_rule(&r, j, NULL, NULL, NULL, h,
1329 NULL, NULL, NULL, NULL, NULL,
1332 if ((i->ifa_flags & IFF_LOOPBACK) == 0) {
1333 bzero(&r, sizeof(r));
1336 r.direction = PF_IN;
1341 if (rule_label(&r, $5.label))
1343 r.rtableid = $5.rtableid;
1347 h = ifa_lookup(i->ifname, 0);
1349 expand_rule(&r, NULL, NULL,
1350 NULL, NULL, h, NULL, NULL,
1351 NULL, NULL, NULL, NULL, "");
1355 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++)
1360 antispoof_ifspc : FOR antispoof_if { $$ = $2; }
1361 | FOR '{' optnl antispoof_iflst '}' { $$ = $4; }
1364 antispoof_iflst : antispoof_if optnl { $$ = $1; }
1365 | antispoof_iflst comma antispoof_if optnl {
1366 $1->tail->next = $3;
1372 antispoof_if : if_item { $$ = $1; }
1380 bzero(&antispoof_opts, sizeof antispoof_opts);
1381 antispoof_opts.rtableid = -1;
1384 { $$ = antispoof_opts; }
1386 bzero(&antispoof_opts, sizeof antispoof_opts);
1387 antispoof_opts.rtableid = -1;
1388 $$ = antispoof_opts;
1392 antispoof_opts_l : antispoof_opts_l antispoof_opt
1396 antispoof_opt : label {
1397 if (antispoof_opts.labelcount >= PF_RULE_MAX_LABEL_COUNT) {
1398 yyerror("label can only be used %d times", PF_RULE_MAX_LABEL_COUNT);
1401 antispoof_opts.label[antispoof_opts.labelcount++] = $1;
1404 if ($2 < 0 || $2 > rt_tableid_max()) {
1405 yyerror("invalid rtable id");
1408 antispoof_opts.rtableid = $2;
1412 not : '!' { $$ = 1; }
1413 | /* empty */ { $$ = 0; }
1416 tabledef : TABLE '<' STRING '>' table_opts {
1417 struct node_host *h, *nh;
1418 struct node_tinit *ti, *nti;
1420 if (strlen($3) >= PF_TABLE_NAME_SIZE) {
1421 yyerror("table name too long, max %d chars",
1422 PF_TABLE_NAME_SIZE - 1);
1426 if (pf->loadopt & PFCTL_FLAG_TABLE)
1427 if (process_tabledef($3, &$5)) {
1432 for (ti = SIMPLEQ_FIRST(&$5.init_nodes);
1433 ti != SIMPLEQ_END(&$5.init_nodes); ti = nti) {
1436 for (h = ti->host; h != NULL; h = nh) {
1440 nti = SIMPLEQ_NEXT(ti, entries);
1447 bzero(&table_opts, sizeof table_opts);
1448 SIMPLEQ_INIT(&table_opts.init_nodes);
1451 { $$ = table_opts; }
1454 bzero(&table_opts, sizeof table_opts);
1455 SIMPLEQ_INIT(&table_opts.init_nodes);
1460 table_opts_l : table_opts_l table_opt
1464 table_opt : STRING {
1465 if (!strcmp($1, "const"))
1466 table_opts.flags |= PFR_TFLAG_CONST;
1467 else if (!strcmp($1, "persist"))
1468 table_opts.flags |= PFR_TFLAG_PERSIST;
1469 else if (!strcmp($1, "counters"))
1470 table_opts.flags |= PFR_TFLAG_COUNTERS;
1472 yyerror("invalid table option '%s'", $1);
1478 | '{' optnl '}' { table_opts.init_addr = 1; }
1479 | '{' optnl host_list '}' {
1480 struct node_host *n;
1481 struct node_tinit *ti;
1483 for (n = $3; n != NULL; n = n->next) {
1484 switch (n->addr.type) {
1485 case PF_ADDR_ADDRMASK:
1488 yyerror("address ranges are not "
1489 "permitted inside tables");
1491 case PF_ADDR_DYNIFTL:
1492 yyerror("dynamic addresses are not "
1493 "permitted inside tables");
1496 yyerror("tables cannot contain tables");
1498 case PF_ADDR_NOROUTE:
1499 yyerror("\"no-route\" is not permitted "
1502 case PF_ADDR_URPFFAILED:
1503 yyerror("\"urpf-failed\" is not "
1504 "permitted inside tables");
1507 yyerror("unknown address type %d",
1512 if (!(ti = calloc(1, sizeof(*ti))))
1513 err(1, "table_opt: calloc");
1515 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1517 table_opts.init_addr = 1;
1520 struct node_tinit *ti;
1522 if (!(ti = calloc(1, sizeof(*ti))))
1523 err(1, "table_opt: calloc");
1525 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1527 table_opts.init_addr = 1;
1531 altqif : ALTQ interface queue_opts QUEUE qassign {
1534 if (check_rulestate(PFCTL_STATE_QUEUE))
1537 memset(&a, 0, sizeof(a));
1538 if ($3.scheduler.qtype == ALTQT_NONE) {
1539 yyerror("no scheduler specified!");
1542 a.scheduler = $3.scheduler.qtype;
1543 a.qlimit = $3.qlimit;
1544 a.tbrsize = $3.tbrsize;
1545 if ($5 == NULL && $3.scheduler.qtype != ALTQT_CODEL) {
1546 yyerror("no child queues specified");
1549 if (expand_altq(&a, $2, $5, $3.queue_bwspec,
1555 queuespec : QUEUE STRING interface queue_opts qassign {
1558 if (check_rulestate(PFCTL_STATE_QUEUE)) {
1563 memset(&a, 0, sizeof(a));
1565 if (strlcpy(a.qname, $2, sizeof(a.qname)) >=
1567 yyerror("queue name too long (max "
1568 "%d chars)", PF_QNAME_SIZE-1);
1574 yyerror("cannot specify tbrsize for queue");
1577 if ($4.priority > 255) {
1578 yyerror("priority out of range: max 255");
1581 a.priority = $4.priority;
1582 a.qlimit = $4.qlimit;
1583 a.scheduler = $4.scheduler.qtype;
1584 if (expand_queue(&a, $3, $5, $4.queue_bwspec,
1586 yyerror("errors in queue definition");
1593 bzero(&queue_opts, sizeof queue_opts);
1594 queue_opts.priority = DEFAULT_PRIORITY;
1595 queue_opts.qlimit = DEFAULT_QLIMIT;
1596 queue_opts.scheduler.qtype = ALTQT_NONE;
1597 queue_opts.queue_bwspec.bw_percent = 100;
1600 { $$ = queue_opts; }
1602 bzero(&queue_opts, sizeof queue_opts);
1603 queue_opts.priority = DEFAULT_PRIORITY;
1604 queue_opts.qlimit = DEFAULT_QLIMIT;
1605 queue_opts.scheduler.qtype = ALTQT_NONE;
1606 queue_opts.queue_bwspec.bw_percent = 100;
1611 queue_opts_l : queue_opts_l queue_opt
1615 queue_opt : BANDWIDTH bandwidth {
1616 if (queue_opts.marker & QOM_BWSPEC) {
1617 yyerror("bandwidth cannot be respecified");
1620 queue_opts.marker |= QOM_BWSPEC;
1621 queue_opts.queue_bwspec = $2;
1624 if (queue_opts.marker & QOM_PRIORITY) {
1625 yyerror("priority cannot be respecified");
1628 if ($2 < 0 || $2 > 255) {
1629 yyerror("priority out of range: max 255");
1632 queue_opts.marker |= QOM_PRIORITY;
1633 queue_opts.priority = $2;
1636 if (queue_opts.marker & QOM_QLIMIT) {
1637 yyerror("qlimit cannot be respecified");
1640 if ($2 < 0 || $2 > 65535) {
1641 yyerror("qlimit out of range: max 65535");
1644 queue_opts.marker |= QOM_QLIMIT;
1645 queue_opts.qlimit = $2;
1648 if (queue_opts.marker & QOM_SCHEDULER) {
1649 yyerror("scheduler cannot be respecified");
1652 queue_opts.marker |= QOM_SCHEDULER;
1653 queue_opts.scheduler = $1;
1656 if (queue_opts.marker & QOM_TBRSIZE) {
1657 yyerror("tbrsize cannot be respecified");
1660 if ($2 < 0 || $2 > UINT_MAX) {
1661 yyerror("tbrsize too big: max %u", UINT_MAX);
1664 queue_opts.marker |= QOM_TBRSIZE;
1665 queue_opts.tbrsize = $2;
1669 bandwidth : STRING {
1675 bps = strtod($1, &cp);
1677 if (strlen(cp) > 1) {
1679 if (!strcmp(cu, "Bit") ||
1681 !strcmp(cu, "bit") ||
1686 if (!strcmp(cp, "b"))
1688 else if (!strcmp(cp, "K"))
1690 else if (!strcmp(cp, "M"))
1692 else if (!strcmp(cp, "G"))
1693 bps *= 1000 * 1000 * 1000;
1694 else if (!strcmp(cp, "%")) {
1695 if (bps < 0 || bps > 100) {
1696 yyerror("bandwidth spec "
1701 $$.bw_percent = bps;
1704 yyerror("unknown unit %s", cp);
1710 $$.bw_absolute = (u_int64_t)bps;
1713 if ($1 < 0 || $1 >= LLONG_MAX) {
1714 yyerror("bandwidth number too big");
1718 $$.bw_absolute = $1;
1723 $$.qtype = ALTQT_CBQ;
1724 $$.data.cbq_opts.flags = 0;
1726 | CBQ '(' cbqflags_list ')' {
1727 $$.qtype = ALTQT_CBQ;
1728 $$.data.cbq_opts.flags = $3;
1731 $$.qtype = ALTQT_PRIQ;
1732 $$.data.priq_opts.flags = 0;
1734 | PRIQ '(' priqflags_list ')' {
1735 $$.qtype = ALTQT_PRIQ;
1736 $$.data.priq_opts.flags = $3;
1739 $$.qtype = ALTQT_HFSC;
1740 bzero(&$$.data.hfsc_opts,
1741 sizeof(struct node_hfsc_opts));
1743 | HFSC '(' hfsc_opts ')' {
1744 $$.qtype = ALTQT_HFSC;
1745 $$.data.hfsc_opts = $3;
1748 $$.qtype = ALTQT_FAIRQ;
1749 bzero(&$$.data.fairq_opts,
1750 sizeof(struct node_fairq_opts));
1752 | FAIRQ '(' fairq_opts ')' {
1753 $$.qtype = ALTQT_FAIRQ;
1754 $$.data.fairq_opts = $3;
1757 $$.qtype = ALTQT_CODEL;
1758 bzero(&$$.data.codel_opts,
1759 sizeof(struct codel_opts));
1761 | CODEL '(' codel_opts ')' {
1762 $$.qtype = ALTQT_CODEL;
1763 $$.data.codel_opts = $3;
1767 cbqflags_list : cbqflags_item { $$ |= $1; }
1768 | cbqflags_list comma cbqflags_item { $$ |= $3; }
1771 cbqflags_item : STRING {
1772 if (!strcmp($1, "default"))
1773 $$ = CBQCLF_DEFCLASS;
1774 else if (!strcmp($1, "borrow"))
1776 else if (!strcmp($1, "red"))
1778 else if (!strcmp($1, "ecn"))
1779 $$ = CBQCLF_RED|CBQCLF_ECN;
1780 else if (!strcmp($1, "rio"))
1782 else if (!strcmp($1, "codel"))
1785 yyerror("unknown cbq flag \"%s\"", $1);
1793 priqflags_list : priqflags_item { $$ |= $1; }
1794 | priqflags_list comma priqflags_item { $$ |= $3; }
1797 priqflags_item : STRING {
1798 if (!strcmp($1, "default"))
1799 $$ = PRCF_DEFAULTCLASS;
1800 else if (!strcmp($1, "red"))
1802 else if (!strcmp($1, "ecn"))
1803 $$ = PRCF_RED|PRCF_ECN;
1804 else if (!strcmp($1, "rio"))
1806 else if (!strcmp($1, "codel"))
1809 yyerror("unknown priq flag \"%s\"", $1);
1819 sizeof(struct node_hfsc_opts));
1826 hfscopts_list : hfscopts_item
1827 | hfscopts_list comma hfscopts_item
1830 hfscopts_item : LINKSHARE bandwidth {
1831 if (hfsc_opts.linkshare.used) {
1832 yyerror("linkshare already specified");
1835 hfsc_opts.linkshare.m2 = $2;
1836 hfsc_opts.linkshare.used = 1;
1838 | LINKSHARE '(' bandwidth comma NUMBER comma bandwidth ')'
1840 if ($5 < 0 || $5 > INT_MAX) {
1841 yyerror("timing in curve out of range");
1844 if (hfsc_opts.linkshare.used) {
1845 yyerror("linkshare already specified");
1848 hfsc_opts.linkshare.m1 = $3;
1849 hfsc_opts.linkshare.d = $5;
1850 hfsc_opts.linkshare.m2 = $7;
1851 hfsc_opts.linkshare.used = 1;
1853 | REALTIME bandwidth {
1854 if (hfsc_opts.realtime.used) {
1855 yyerror("realtime already specified");
1858 hfsc_opts.realtime.m2 = $2;
1859 hfsc_opts.realtime.used = 1;
1861 | REALTIME '(' bandwidth comma NUMBER comma bandwidth ')'
1863 if ($5 < 0 || $5 > INT_MAX) {
1864 yyerror("timing in curve out of range");
1867 if (hfsc_opts.realtime.used) {
1868 yyerror("realtime already specified");
1871 hfsc_opts.realtime.m1 = $3;
1872 hfsc_opts.realtime.d = $5;
1873 hfsc_opts.realtime.m2 = $7;
1874 hfsc_opts.realtime.used = 1;
1876 | UPPERLIMIT bandwidth {
1877 if (hfsc_opts.upperlimit.used) {
1878 yyerror("upperlimit already specified");
1881 hfsc_opts.upperlimit.m2 = $2;
1882 hfsc_opts.upperlimit.used = 1;
1884 | UPPERLIMIT '(' bandwidth comma NUMBER comma bandwidth ')'
1886 if ($5 < 0 || $5 > INT_MAX) {
1887 yyerror("timing in curve out of range");
1890 if (hfsc_opts.upperlimit.used) {
1891 yyerror("upperlimit already specified");
1894 hfsc_opts.upperlimit.m1 = $3;
1895 hfsc_opts.upperlimit.d = $5;
1896 hfsc_opts.upperlimit.m2 = $7;
1897 hfsc_opts.upperlimit.used = 1;
1900 if (!strcmp($1, "default"))
1901 hfsc_opts.flags |= HFCF_DEFAULTCLASS;
1902 else if (!strcmp($1, "red"))
1903 hfsc_opts.flags |= HFCF_RED;
1904 else if (!strcmp($1, "ecn"))
1905 hfsc_opts.flags |= HFCF_RED|HFCF_ECN;
1906 else if (!strcmp($1, "rio"))
1907 hfsc_opts.flags |= HFCF_RIO;
1908 else if (!strcmp($1, "codel"))
1909 hfsc_opts.flags |= HFCF_CODEL;
1911 yyerror("unknown hfsc flag \"%s\"", $1);
1921 sizeof(struct node_fairq_opts));
1928 fairqopts_list : fairqopts_item
1929 | fairqopts_list comma fairqopts_item
1932 fairqopts_item : LINKSHARE bandwidth {
1933 if (fairq_opts.linkshare.used) {
1934 yyerror("linkshare already specified");
1937 fairq_opts.linkshare.m2 = $2;
1938 fairq_opts.linkshare.used = 1;
1940 | LINKSHARE '(' bandwidth number bandwidth ')' {
1941 if (fairq_opts.linkshare.used) {
1942 yyerror("linkshare already specified");
1945 fairq_opts.linkshare.m1 = $3;
1946 fairq_opts.linkshare.d = $4;
1947 fairq_opts.linkshare.m2 = $5;
1948 fairq_opts.linkshare.used = 1;
1951 fairq_opts.hogs_bw = $2;
1954 fairq_opts.nbuckets = $2;
1957 if (!strcmp($1, "default"))
1958 fairq_opts.flags |= FARF_DEFAULTCLASS;
1959 else if (!strcmp($1, "red"))
1960 fairq_opts.flags |= FARF_RED;
1961 else if (!strcmp($1, "ecn"))
1962 fairq_opts.flags |= FARF_RED|FARF_ECN;
1963 else if (!strcmp($1, "rio"))
1964 fairq_opts.flags |= FARF_RIO;
1965 else if (!strcmp($1, "codel"))
1966 fairq_opts.flags |= FARF_CODEL;
1968 yyerror("unknown fairq flag \"%s\"", $1);
1978 sizeof(struct codel_opts));
1985 codelopts_list : codelopts_item
1986 | codelopts_list comma codelopts_item
1989 codelopts_item : INTERVAL number {
1990 if (codel_opts.interval) {
1991 yyerror("interval already specified");
1994 codel_opts.interval = $2;
1997 if (codel_opts.target) {
1998 yyerror("target already specified");
2001 codel_opts.target = $2;
2004 if (!strcmp($1, "ecn"))
2007 yyerror("unknown codel option \"%s\"", $1);
2015 qassign : /* empty */ { $$ = NULL; }
2016 | qassign_item { $$ = $1; }
2017 | '{' optnl qassign_list '}' { $$ = $3; }
2020 qassign_list : qassign_item optnl { $$ = $1; }
2021 | qassign_list comma qassign_item optnl {
2022 $1->tail->next = $3;
2028 qassign_item : STRING {
2029 $$ = calloc(1, sizeof(struct node_queue));
2031 err(1, "qassign_item: calloc");
2032 if (strlcpy($$->queue, $1, sizeof($$->queue)) >=
2033 sizeof($$->queue)) {
2034 yyerror("queue name '%s' too long (max "
2035 "%d chars)", $1, sizeof($$->queue)-1);
2046 pfrule : action dir logquick interface route af proto fromto
2049 struct pfctl_rule r;
2050 struct node_state_opt *o;
2051 struct node_proto *proto;
2057 if (check_rulestate(PFCTL_STATE_FILTER))
2060 memset(&r, 0, sizeof(r));
2064 case PFRULE_RETURNRST:
2065 r.rule_flag |= PFRULE_RETURNRST;
2066 r.return_ttl = $1.w;
2068 case PFRULE_RETURNICMP:
2069 r.rule_flag |= PFRULE_RETURNICMP;
2070 r.return_icmp = $1.w;
2071 r.return_icmp6 = $1.w2;
2074 r.rule_flag |= PFRULE_RETURN;
2075 r.return_icmp = $1.w;
2076 r.return_icmp6 = $1.w2;
2084 r.rtableid = $9.rtableid;
2086 if ($9.marker & FOM_PRIO) {
2088 r.prio = PF_PRIO_ZERO;
2092 if ($9.marker & FOM_SETPRIO) {
2093 r.set_prio[0] = $9.set_prio[0];
2094 r.set_prio[1] = $9.set_prio[1];
2095 r.scrub_flags |= PFSTATE_SETPRIO;
2100 if (strlcpy(r.tagname, $9.tag,
2101 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
2102 yyerror("tag too long, max %u chars",
2103 PF_TAG_NAME_SIZE - 1);
2107 if (strlcpy(r.match_tagname, $9.match_tag,
2108 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
2109 yyerror("tag too long, max %u chars",
2110 PF_TAG_NAME_SIZE - 1);
2113 r.match_tag_not = $9.match_tag_not;
2114 if (rule_label(&r, $9.label))
2116 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++)
2118 r.flags = $9.flags.b1;
2119 r.flagset = $9.flags.b2;
2120 if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
2121 yyerror("flags always false");
2124 if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
2125 for (proto = $7; proto != NULL &&
2126 proto->proto != IPPROTO_TCP;
2127 proto = proto->next)
2129 if (proto == NULL && $7 != NULL) {
2130 if ($9.flags.b1 || $9.flags.b2)
2132 "flags only apply to tcp");
2135 "OS fingerprinting only "
2140 if (($9.flags.b1 & parse_flags("S")) == 0 &&
2142 yyerror("OS fingerprinting requires "
2143 "the SYN TCP flag (flags S/SA)");
2150 r.keep_state = $9.keep.action;
2151 o = $9.keep.options;
2153 /* 'keep state' by default on pass rules. */
2154 if (!r.keep_state && !r.action &&
2155 !($9.marker & FOM_KEEP)) {
2156 r.keep_state = PF_STATE_NORMAL;
2157 o = keep_state_defaults;
2162 struct node_state_opt *p = o;
2165 case PF_STATE_OPT_MAX:
2167 yyerror("state option 'max' "
2168 "multiple definitions");
2171 r.max_states = o->data.max_states;
2173 case PF_STATE_OPT_NOSYNC:
2174 if (r.rule_flag & PFRULE_NOSYNC) {
2175 yyerror("state option 'sync' "
2176 "multiple definitions");
2179 r.rule_flag |= PFRULE_NOSYNC;
2181 case PF_STATE_OPT_SRCTRACK:
2183 yyerror("state option "
2185 "multiple definitions");
2188 srctrack = o->data.src_track;
2189 r.rule_flag |= PFRULE_SRCTRACK;
2191 case PF_STATE_OPT_MAX_SRC_STATES:
2192 if (r.max_src_states) {
2193 yyerror("state option "
2195 "multiple definitions");
2198 if (o->data.max_src_states == 0) {
2199 yyerror("'max-src-states' must "
2204 o->data.max_src_states;
2205 r.rule_flag |= PFRULE_SRCTRACK;
2207 case PF_STATE_OPT_OVERLOAD:
2208 if (r.overload_tblname[0]) {
2209 yyerror("multiple 'overload' "
2210 "table definitions");
2213 if (strlcpy(r.overload_tblname,
2214 o->data.overload.tblname,
2215 PF_TABLE_NAME_SIZE) >=
2216 PF_TABLE_NAME_SIZE) {
2217 yyerror("state option: "
2221 r.flush = o->data.overload.flush;
2223 case PF_STATE_OPT_MAX_SRC_CONN:
2224 if (r.max_src_conn) {
2225 yyerror("state option "
2227 "multiple definitions");
2230 if (o->data.max_src_conn == 0) {
2231 yyerror("'max-src-conn' "
2236 o->data.max_src_conn;
2237 r.rule_flag |= PFRULE_SRCTRACK |
2238 PFRULE_RULESRCTRACK;
2240 case PF_STATE_OPT_MAX_SRC_CONN_RATE:
2241 if (r.max_src_conn_rate.limit) {
2242 yyerror("state option "
2243 "'max-src-conn-rate' "
2244 "multiple definitions");
2247 if (!o->data.max_src_conn_rate.limit ||
2248 !o->data.max_src_conn_rate.seconds) {
2249 yyerror("'max-src-conn-rate' "
2250 "values must be > 0");
2253 if (o->data.max_src_conn_rate.limit >
2255 yyerror("'max-src-conn-rate' "
2256 "maximum rate must be < %u",
2260 r.max_src_conn_rate.limit =
2261 o->data.max_src_conn_rate.limit;
2262 r.max_src_conn_rate.seconds =
2263 o->data.max_src_conn_rate.seconds;
2264 r.rule_flag |= PFRULE_SRCTRACK |
2265 PFRULE_RULESRCTRACK;
2267 case PF_STATE_OPT_MAX_SRC_NODES:
2268 if (r.max_src_nodes) {
2269 yyerror("state option "
2271 "multiple definitions");
2274 if (o->data.max_src_nodes == 0) {
2275 yyerror("'max-src-nodes' must "
2280 o->data.max_src_nodes;
2281 r.rule_flag |= PFRULE_SRCTRACK |
2282 PFRULE_RULESRCTRACK;
2284 case PF_STATE_OPT_STATELOCK:
2286 yyerror("state locking option: "
2287 "multiple definitions");
2291 r.rule_flag |= o->data.statelock;
2293 case PF_STATE_OPT_SLOPPY:
2294 if (r.rule_flag & PFRULE_STATESLOPPY) {
2295 yyerror("state sloppy option: "
2296 "multiple definitions");
2299 r.rule_flag |= PFRULE_STATESLOPPY;
2301 case PF_STATE_OPT_TIMEOUT:
2302 if (o->data.timeout.number ==
2303 PFTM_ADAPTIVE_START ||
2304 o->data.timeout.number ==
2307 if (r.timeout[o->data.timeout.number]) {
2308 yyerror("state timeout %s "
2309 "multiple definitions",
2310 pf_timeouts[o->data.
2311 timeout.number].name);
2314 r.timeout[o->data.timeout.number] =
2315 o->data.timeout.seconds;
2322 /* 'flags S/SA' by default on stateful rules */
2323 if (!r.action && !r.flags && !r.flagset &&
2324 !$9.fragment && !($9.marker & FOM_FLAGS) &&
2326 r.flags = parse_flags("S");
2327 r.flagset = parse_flags("SA");
2329 if (!adaptive && r.max_states) {
2330 r.timeout[PFTM_ADAPTIVE_START] =
2331 (r.max_states / 10) * 6;
2332 r.timeout[PFTM_ADAPTIVE_END] =
2333 (r.max_states / 10) * 12;
2335 if (r.rule_flag & PFRULE_SRCTRACK) {
2336 if (srctrack == PF_SRCTRACK_GLOBAL &&
2338 yyerror("'max-src-nodes' is "
2339 "incompatible with "
2340 "'source-track global'");
2343 if (srctrack == PF_SRCTRACK_GLOBAL &&
2345 yyerror("'max-src-conn' is "
2346 "incompatible with "
2347 "'source-track global'");
2350 if (srctrack == PF_SRCTRACK_GLOBAL &&
2351 r.max_src_conn_rate.seconds) {
2352 yyerror("'max-src-conn-rate' is "
2353 "incompatible with "
2354 "'source-track global'");
2357 if (r.timeout[PFTM_SRC_NODE] <
2358 r.max_src_conn_rate.seconds)
2359 r.timeout[PFTM_SRC_NODE] =
2360 r.max_src_conn_rate.seconds;
2361 r.rule_flag |= PFRULE_SRCTRACK;
2362 if (srctrack == PF_SRCTRACK_RULE)
2363 r.rule_flag |= PFRULE_RULESRCTRACK;
2365 if (r.keep_state && !statelock)
2366 r.rule_flag |= default_statelock;
2369 r.rule_flag |= PFRULE_FRAGMENT;
2370 r.allow_opts = $9.allowopts;
2372 decide_address_family($8.src.host, &r.af);
2373 decide_address_family($8.dst.host, &r.af);
2377 yyerror("direction must be explicit "
2378 "with rules that specify routing");
2382 r.rpool.opts = $5.pool_opts;
2384 memcpy(&r.rpool.key, $5.key,
2385 sizeof(struct pf_poolhashkey));
2388 decide_address_family($5.host, &r.af);
2389 remove_invalid_hosts(&$5.host, &r.af);
2390 if ($5.host == NULL) {
2391 yyerror("no routing address with "
2392 "matching address family found.");
2395 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
2396 PF_POOL_NONE && ($5.host->next != NULL ||
2397 $5.host->addr.type == PF_ADDR_TABLE ||
2398 DYNIF_MULTIADDR($5.host->addr)))
2399 r.rpool.opts |= PF_POOL_ROUNDROBIN;
2400 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2401 PF_POOL_ROUNDROBIN &&
2402 disallow_table($5.host, "tables are only "
2403 "supported in round-robin routing pools"))
2405 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2406 PF_POOL_ROUNDROBIN &&
2407 disallow_alias($5.host, "interface (%s) "
2408 "is only supported in round-robin "
2411 if ($5.host->next != NULL) {
2412 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2413 PF_POOL_ROUNDROBIN) {
2414 yyerror("r.rpool.opts must "
2415 "be PF_POOL_ROUNDROBIN");
2420 if ($9.queues.qname != NULL) {
2421 if (strlcpy(r.qname, $9.queues.qname,
2422 sizeof(r.qname)) >= sizeof(r.qname)) {
2423 yyerror("rule qname too long (max "
2424 "%d chars)", sizeof(r.qname)-1);
2427 free($9.queues.qname);
2429 if ($9.queues.pqname != NULL) {
2430 if (strlcpy(r.pqname, $9.queues.pqname,
2431 sizeof(r.pqname)) >= sizeof(r.pqname)) {
2432 yyerror("rule pqname too long (max "
2433 "%d chars)", sizeof(r.pqname)-1);
2436 free($9.queues.pqname);
2439 r.divert.port = $9.divert.port;
2441 if ((r.divert.port = $9.divert.port)) {
2442 if (r.direction == PF_OUT) {
2443 if ($9.divert.addr) {
2444 yyerror("address specified "
2445 "for outgoing divert");
2448 bzero(&r.divert.addr,
2449 sizeof(r.divert.addr));
2451 if (!$9.divert.addr) {
2452 yyerror("no address specified "
2453 "for incoming divert");
2456 if ($9.divert.addr->af != r.af) {
2457 yyerror("address family "
2458 "mismatch for divert");
2462 $9.divert.addr->addr.v.a.addr;
2467 expand_rule(&r, $4, $5.host, $7, $8.src_os,
2468 $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
2469 $9.uid, $9.gid, $9.icmpspec, "");
2474 bzero(&filter_opts, sizeof filter_opts);
2475 filter_opts.rtableid = -1;
2478 { $$ = filter_opts; }
2480 bzero(&filter_opts, sizeof filter_opts);
2481 filter_opts.rtableid = -1;
2486 filter_opts_l : filter_opts_l filter_opt
2490 filter_opt : USER uids {
2491 if (filter_opts.uid)
2492 $2->tail->next = filter_opts.uid;
2493 filter_opts.uid = $2;
2496 if (filter_opts.gid)
2497 $2->tail->next = filter_opts.gid;
2498 filter_opts.gid = $2;
2501 if (filter_opts.marker & FOM_FLAGS) {
2502 yyerror("flags cannot be redefined");
2505 filter_opts.marker |= FOM_FLAGS;
2506 filter_opts.flags.b1 |= $1.b1;
2507 filter_opts.flags.b2 |= $1.b2;
2508 filter_opts.flags.w |= $1.w;
2509 filter_opts.flags.w2 |= $1.w2;
2512 if (filter_opts.marker & FOM_ICMP) {
2513 yyerror("icmp-type cannot be redefined");
2516 filter_opts.marker |= FOM_ICMP;
2517 filter_opts.icmpspec = $1;
2520 if (filter_opts.marker & FOM_PRIO) {
2521 yyerror("prio cannot be redefined");
2524 if ($2 < 0 || $2 > PF_PRIO_MAX) {
2525 yyerror("prio must be 0 - %u", PF_PRIO_MAX);
2528 filter_opts.marker |= FOM_PRIO;
2529 filter_opts.prio = $2;
2532 if (filter_opts.marker & FOM_TOS) {
2533 yyerror("tos cannot be redefined");
2536 filter_opts.marker |= FOM_TOS;
2537 filter_opts.tos = $2;
2540 if (filter_opts.marker & FOM_KEEP) {
2541 yyerror("modulate or keep cannot be redefined");
2544 filter_opts.marker |= FOM_KEEP;
2545 filter_opts.keep.action = $1.action;
2546 filter_opts.keep.options = $1.options;
2549 filter_opts.fragment = 1;
2552 filter_opts.allowopts = 1;
2555 if (filter_opts.labelcount >= PF_RULE_MAX_LABEL_COUNT) {
2556 yyerror("label can only be used %d times", PF_RULE_MAX_LABEL_COUNT);
2559 filter_opts.label[filter_opts.labelcount++] = $1;
2562 if (filter_opts.queues.qname) {
2563 yyerror("queue cannot be redefined");
2566 filter_opts.queues = $1;
2569 filter_opts.tag = $2;
2571 | not TAGGED string {
2572 filter_opts.match_tag = $3;
2573 filter_opts.match_tag_not = $1;
2575 | PROBABILITY probability {
2578 p = floor($2 * UINT_MAX + 0.5);
2579 if (p < 0.0 || p > UINT_MAX) {
2580 yyerror("invalid probability: %lf", p);
2583 filter_opts.prob = (u_int32_t)p;
2584 if (filter_opts.prob == 0)
2585 filter_opts.prob = 1;
2588 if ($2 < 0 || $2 > rt_tableid_max()) {
2589 yyerror("invalid rtable id");
2592 filter_opts.rtableid = $2;
2594 | DIVERTTO portplain {
2596 filter_opts.divert.port = $2.a;
2597 if (!filter_opts.divert.port) {
2598 yyerror("invalid divert port: %u", ntohs($2.a));
2603 | DIVERTTO STRING PORT portplain {
2605 if ((filter_opts.divert.addr = host($2)) == NULL) {
2606 yyerror("could not parse divert address: %s",
2615 filter_opts.divert.port = $4.a;
2616 if (!filter_opts.divert.port) {
2617 yyerror("invalid divert port: %u", ntohs($4.a));
2623 yyerror("divert-reply has no meaning in FreeBSD pf(4)");
2626 filter_opts.divert.port = 1; /* some random value */
2632 filter_sets : SET '(' filter_sets_l ')' { $$ = filter_opts; }
2633 | SET filter_set { $$ = filter_opts; }
2636 filter_sets_l : filter_sets_l comma filter_set
2641 if (filter_opts.marker & FOM_SETPRIO) {
2642 yyerror("prio cannot be redefined");
2645 filter_opts.marker |= FOM_SETPRIO;
2646 filter_opts.set_prio[0] = $1.b1;
2647 filter_opts.set_prio[1] = $1.b2;
2649 prio : PRIO NUMBER {
2650 if ($2 < 0 || $2 > PF_PRIO_MAX) {
2651 yyerror("prio must be 0 - %u", PF_PRIO_MAX);
2656 | PRIO '(' NUMBER comma NUMBER ')' {
2657 if ($3 < 0 || $3 > PF_PRIO_MAX ||
2658 $5 < 0 || $5 > PF_PRIO_MAX) {
2659 yyerror("prio must be 0 - %u", PF_PRIO_MAX);
2667 probability : STRING {
2669 double p = strtod($1, &e);
2676 yyerror("invalid probability: %s", $1);
2692 $$.w = returnicmpdefault;
2693 $$.w2 = returnicmp6default;
2695 | MATCH { $$.b1 = PF_MATCH; $$.b2 = $$.w = 0; }
2696 | BLOCK blockspec { $$ = $2; $$.b1 = PF_DROP; }
2699 blockspec : /* empty */ {
2700 $$.b2 = blockpolicy;
2701 $$.w = returnicmpdefault;
2702 $$.w2 = returnicmp6default;
2705 $$.b2 = PFRULE_DROP;
2710 $$.b2 = PFRULE_RETURNRST;
2714 | RETURNRST '(' TTL NUMBER ')' {
2715 if ($4 < 0 || $4 > 255) {
2716 yyerror("illegal ttl value %d", $4);
2719 $$.b2 = PFRULE_RETURNRST;
2724 $$.b2 = PFRULE_RETURNICMP;
2725 $$.w = returnicmpdefault;
2726 $$.w2 = returnicmp6default;
2729 $$.b2 = PFRULE_RETURNICMP;
2730 $$.w = returnicmpdefault;
2731 $$.w2 = returnicmp6default;
2733 | RETURNICMP '(' reticmpspec ')' {
2734 $$.b2 = PFRULE_RETURNICMP;
2736 $$.w2 = returnicmpdefault;
2738 | RETURNICMP6 '(' reticmp6spec ')' {
2739 $$.b2 = PFRULE_RETURNICMP;
2740 $$.w = returnicmpdefault;
2743 | RETURNICMP '(' reticmpspec comma reticmp6spec ')' {
2744 $$.b2 = PFRULE_RETURNICMP;
2749 $$.b2 = PFRULE_RETURN;
2750 $$.w = returnicmpdefault;
2751 $$.w2 = returnicmp6default;
2755 reticmpspec : STRING {
2756 if (!($$ = parseicmpspec($1, AF_INET))) {
2765 if ($1 < 0 || $1 > 255) {
2766 yyerror("invalid icmp code %lu", $1);
2769 icmptype = returnicmpdefault >> 8;
2770 $$ = (icmptype << 8 | $1);
2774 reticmp6spec : STRING {
2775 if (!($$ = parseicmpspec($1, AF_INET6))) {
2784 if ($1 < 0 || $1 > 255) {
2785 yyerror("invalid icmp code %lu", $1);
2788 icmptype = returnicmp6default >> 8;
2789 $$ = (icmptype << 8 | $1);
2793 dir : /* empty */ { $$ = PF_INOUT; }
2794 | IN { $$ = PF_IN; }
2795 | OUT { $$ = PF_OUT; }
2798 quick : /* empty */ { $$.quick = 0; }
2799 | QUICK { $$.quick = 1; }
2802 logquick : /* empty */ { $$.log = 0; $$.quick = 0; $$.logif = 0; }
2803 | log { $$ = $1; $$.quick = 0; }
2804 | QUICK { $$.quick = 1; $$.log = 0; $$.logif = 0; }
2805 | log QUICK { $$ = $1; $$.quick = 1; }
2806 | QUICK log { $$ = $2; $$.quick = 1; }
2809 log : LOG { $$.log = PF_LOG; $$.logif = 0; }
2810 | LOG '(' logopts ')' {
2811 $$.log = PF_LOG | $3.log;
2812 $$.logif = $3.logif;
2816 logopts : logopt { $$ = $1; }
2817 | logopts comma logopt {
2818 $$.log = $1.log | $3.log;
2819 $$.logif = $3.logif;
2821 $$.logif = $1.logif;
2825 logopt : ALL { $$.log = PF_LOG_ALL; $$.logif = 0; }
2826 | USER { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
2827 | GROUP { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
2833 if (strncmp($2, "pflog", 5)) {
2834 yyerror("%s: should be a pflog interface", $2);
2838 i = strtonum($2 + 5, 0, 255, &errstr);
2840 yyerror("%s: %s", $2, errstr);
2849 interface : /* empty */ { $$ = NULL; }
2850 | ON if_item_not { $$ = $2; }
2851 | ON '{' optnl if_list '}' { $$ = $4; }
2854 if_list : if_item_not optnl { $$ = $1; }
2855 | if_list comma if_item_not optnl {
2856 $1->tail->next = $3;
2862 if_item_not : not if_item { $$ = $2; $$->not = $1; }
2866 struct node_host *n;
2868 $$ = calloc(1, sizeof(struct node_if));
2870 err(1, "if_item: calloc");
2871 if (strlcpy($$->ifname, $1, sizeof($$->ifname)) >=
2872 sizeof($$->ifname)) {
2875 yyerror("interface name too long");
2879 if ((n = ifa_exists($1)) != NULL)
2880 $$->ifa_flags = n->ifa_flags;
2889 af : /* empty */ { $$ = 0; }
2890 | INET { $$ = AF_INET; }
2891 | INET6 { $$ = AF_INET6; }
2894 proto : /* empty */ { $$ = NULL; }
2895 | PROTO proto_item { $$ = $2; }
2896 | PROTO '{' optnl proto_list '}' { $$ = $4; }
2899 proto_list : proto_item optnl { $$ = $1; }
2900 | proto_list comma proto_item optnl {
2901 $1->tail->next = $3;
2907 proto_item : protoval {
2912 yyerror("proto 0 cannot be used");
2915 $$ = calloc(1, sizeof(struct node_proto));
2917 err(1, "proto_item: calloc");
2927 p = getprotobyname($1);
2929 yyerror("unknown protocol %s", $1);
2937 if ($1 < 0 || $1 > 255) {
2938 yyerror("protocol outside range");
2958 os : /* empty */ { $$ = NULL; }
2959 | OS xos { $$ = $2; }
2960 | OS '{' optnl os_list '}' { $$ = $4; }
2964 $$ = calloc(1, sizeof(struct node_os));
2966 err(1, "os: calloc");
2972 os_list : xos optnl { $$ = $1; }
2973 | os_list comma xos optnl {
2974 $1->tail->next = $3;
2980 from : /* empty */ {
2994 if (disallow_urpf_failed($2.host, "\"urpf-failed\" is "
2995 "not permitted in a destination address"))
3001 ipportspec : ipspec {
3005 | ipspec PORT portspec {
3019 ipspec : ANY { $$ = NULL; }
3020 | xhost { $$ = $1; }
3021 | '{' optnl host_list '}' { $$ = $3; }
3024 toipspec : TO ipspec { $$ = $2; }
3025 | /* empty */ { $$ = NULL; }
3028 host_list : ipspec optnl { $$ = $1; }
3029 | host_list comma ipspec optnl {
3032 else if ($1 == NULL)
3035 $1->tail->next = $3;
3036 $1->tail = $3->tail;
3043 struct node_host *n;
3045 for (n = $2; n != NULL; n = n->next)
3050 $$ = calloc(1, sizeof(struct node_host));
3052 err(1, "xhost: calloc");
3053 $$->addr.type = PF_ADDR_NOROUTE;
3059 $$ = calloc(1, sizeof(struct node_host));
3061 err(1, "xhost: calloc");
3062 $$->addr.type = PF_ADDR_URPFFAILED;
3070 if (($$ = host($1)) == NULL) {
3071 /* error. "any" is handled elsewhere */
3073 yyerror("could not parse host specification");
3079 | STRING '-' STRING {
3080 struct node_host *b, *e;
3082 if ((b = host($1)) == NULL || (e = host($3)) == NULL) {
3085 yyerror("could not parse host specification");
3088 if (b->af != e->af ||
3089 b->addr.type != PF_ADDR_ADDRMASK ||
3090 e->addr.type != PF_ADDR_ADDRMASK ||
3091 unmask(&b->addr.v.a.mask, b->af) !=
3092 (b->af == AF_INET ? 32 : 128) ||
3093 unmask(&e->addr.v.a.mask, e->af) !=
3094 (e->af == AF_INET ? 32 : 128) ||
3095 b->next != NULL || b->not ||
3096 e->next != NULL || e->not) {
3101 yyerror("invalid address range");
3104 memcpy(&b->addr.v.a.mask, &e->addr.v.a.addr,
3105 sizeof(b->addr.v.a.mask));
3106 b->addr.type = PF_ADDR_RANGE;
3112 | STRING '/' NUMBER {
3115 if (asprintf(&buf, "%s/%lld", $1, (long long)$3) == -1)
3116 err(1, "host: asprintf");
3118 if (($$ = host(buf)) == NULL) {
3119 /* error. "any" is handled elsewhere */
3121 yyerror("could not parse host specification");
3126 | NUMBER '/' NUMBER {
3129 /* ie. for 10/8 parsing */
3131 if (asprintf(&buf, "%lld/%lld", (long long)$1, (long long)$3) == -1)
3133 if (asprintf(&buf, "%lld/%lld", $1, $3) == -1)
3135 err(1, "host: asprintf");
3136 if (($$ = host(buf)) == NULL) {
3137 /* error. "any" is handled elsewhere */
3139 yyerror("could not parse host specification");
3145 | dynaddr '/' NUMBER {
3146 struct node_host *n;
3148 if ($3 < 0 || $3 > 128) {
3149 yyerror("bit number too big");
3153 for (n = $1; n != NULL; n = n->next)
3157 if (strlen($2) >= PF_TABLE_NAME_SIZE) {
3158 yyerror("table name '%s' too long", $2);
3162 $$ = calloc(1, sizeof(struct node_host));
3164 err(1, "host: calloc");
3165 $$->addr.type = PF_ADDR_TABLE;
3166 if (strlcpy($$->addr.v.tblname, $2,
3167 sizeof($$->addr.v.tblname)) >=
3168 sizeof($$->addr.v.tblname))
3169 errx(1, "host: strlcpy");
3180 if (atoul($1, &ulval) == -1) {
3181 yyerror("%s is not a number", $1);
3190 dynaddr : '(' STRING ')' {
3195 if (!isalpha(op[0])) {
3196 yyerror("invalid interface name '%s'", op);
3200 while ((p = strrchr($2, ':')) != NULL) {
3201 if (!strcmp(p+1, "network"))
3202 flags |= PFI_AFLAG_NETWORK;
3203 else if (!strcmp(p+1, "broadcast"))
3204 flags |= PFI_AFLAG_BROADCAST;
3205 else if (!strcmp(p+1, "peer"))
3206 flags |= PFI_AFLAG_PEER;
3207 else if (!strcmp(p+1, "0"))
3208 flags |= PFI_AFLAG_NOALIAS;
3210 yyerror("interface %s has bad modifier",
3217 if (flags & (flags - 1) & PFI_AFLAG_MODEMASK) {
3219 yyerror("illegal combination of "
3220 "interface modifiers");
3223 $$ = calloc(1, sizeof(struct node_host));
3225 err(1, "address: calloc");
3227 set_ipmask($$, 128);
3228 $$->addr.type = PF_ADDR_DYNIFTL;
3229 $$->addr.iflags = flags;
3230 if (strlcpy($$->addr.v.ifname, $2,
3231 sizeof($$->addr.v.ifname)) >=
3232 sizeof($$->addr.v.ifname)) {
3235 yyerror("interface name too long");
3244 portspec : port_item { $$ = $1; }
3245 | '{' optnl port_list '}' { $$ = $3; }
3248 port_list : port_item optnl { $$ = $1; }
3249 | port_list comma port_item optnl {
3250 $1->tail->next = $3;
3256 port_item : portrange {
3257 $$ = calloc(1, sizeof(struct node_port));
3259 err(1, "port_item: calloc");
3269 | unaryop portrange {
3271 yyerror("':' cannot be used with an other "
3275 $$ = calloc(1, sizeof(struct node_port));
3277 err(1, "port_item: calloc");
3284 | portrange PORTBINARY portrange {
3286 yyerror("':' cannot be used with an other "
3290 $$ = calloc(1, sizeof(struct node_port));
3292 err(1, "port_item: calloc");
3301 portplain : numberstring {
3302 if (parseport($1, &$$, 0) == -1) {
3310 portrange : numberstring {
3311 if (parseport($1, &$$, PPORT_RANGE) == -1) {
3319 uids : uid_item { $$ = $1; }
3320 | '{' optnl uid_list '}' { $$ = $3; }
3323 uid_list : uid_item optnl { $$ = $1; }
3324 | uid_list comma uid_item optnl {
3325 $1->tail->next = $3;
3332 $$ = calloc(1, sizeof(struct node_uid));
3334 err(1, "uid_item: calloc");
3342 if ($2 == UID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
3343 yyerror("user unknown requires operator = or "
3347 $$ = calloc(1, sizeof(struct node_uid));
3349 err(1, "uid_item: calloc");
3356 | uid PORTBINARY uid {
3357 if ($1 == UID_MAX || $3 == UID_MAX) {
3358 yyerror("user unknown requires operator = or "
3362 $$ = calloc(1, sizeof(struct node_uid));
3364 err(1, "uid_item: calloc");
3374 if (!strcmp($1, "unknown"))
3379 if ((pw = getpwnam($1)) == NULL) {
3380 yyerror("unknown user %s", $1);
3389 if ($1 < 0 || $1 >= UID_MAX) {
3390 yyerror("illegal uid value %lu", $1);
3397 gids : gid_item { $$ = $1; }
3398 | '{' optnl gid_list '}' { $$ = $3; }
3401 gid_list : gid_item optnl { $$ = $1; }
3402 | gid_list comma gid_item optnl {
3403 $1->tail->next = $3;
3410 $$ = calloc(1, sizeof(struct node_gid));
3412 err(1, "gid_item: calloc");
3420 if ($2 == GID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
3421 yyerror("group unknown requires operator = or "
3425 $$ = calloc(1, sizeof(struct node_gid));
3427 err(1, "gid_item: calloc");
3434 | gid PORTBINARY gid {
3435 if ($1 == GID_MAX || $3 == GID_MAX) {
3436 yyerror("group unknown requires operator = or "
3440 $$ = calloc(1, sizeof(struct node_gid));
3442 err(1, "gid_item: calloc");
3452 if (!strcmp($1, "unknown"))
3457 if ((grp = getgrnam($1)) == NULL) {
3458 yyerror("unknown group %s", $1);
3467 if ($1 < 0 || $1 >= GID_MAX) {
3468 yyerror("illegal gid value %lu", $1);
3478 if ((f = parse_flags($1)) < 0) {
3479 yyerror("bad flags %s", $1);
3488 flags : FLAGS flag '/' flag { $$.b1 = $2.b1; $$.b2 = $4.b1; }
3489 | FLAGS '/' flag { $$.b1 = 0; $$.b2 = $3.b1; }
3490 | FLAGS ANY { $$.b1 = 0; $$.b2 = 0; }
3493 icmpspec : ICMPTYPE icmp_item { $$ = $2; }
3494 | ICMPTYPE '{' optnl icmp_list '}' { $$ = $4; }
3495 | ICMP6TYPE icmp6_item { $$ = $2; }
3496 | ICMP6TYPE '{' optnl icmp6_list '}' { $$ = $4; }
3499 icmp_list : icmp_item optnl { $$ = $1; }
3500 | icmp_list comma icmp_item optnl {
3501 $1->tail->next = $3;
3507 icmp6_list : icmp6_item optnl { $$ = $1; }
3508 | icmp6_list comma icmp6_item optnl {
3509 $1->tail->next = $3;
3515 icmp_item : icmptype {
3516 $$ = calloc(1, sizeof(struct node_icmp));
3518 err(1, "icmp_item: calloc");
3521 $$->proto = IPPROTO_ICMP;
3525 | icmptype CODE STRING {
3526 const struct icmpcodeent *p;
3528 if ((p = geticmpcodebyname($1-1, $3, AF_INET)) == NULL) {
3529 yyerror("unknown icmp-code %s", $3);
3535 $$ = calloc(1, sizeof(struct node_icmp));
3537 err(1, "icmp_item: calloc");
3539 $$->code = p->code + 1;
3540 $$->proto = IPPROTO_ICMP;
3544 | icmptype CODE NUMBER {
3545 if ($3 < 0 || $3 > 255) {
3546 yyerror("illegal icmp-code %lu", $3);
3549 $$ = calloc(1, sizeof(struct node_icmp));
3551 err(1, "icmp_item: calloc");
3554 $$->proto = IPPROTO_ICMP;
3560 icmp6_item : icmp6type {
3561 $$ = calloc(1, sizeof(struct node_icmp));
3563 err(1, "icmp_item: calloc");
3566 $$->proto = IPPROTO_ICMPV6;
3570 | icmp6type CODE STRING {
3571 const struct icmpcodeent *p;
3573 if ((p = geticmpcodebyname($1-1, $3, AF_INET6)) == NULL) {
3574 yyerror("unknown icmp6-code %s", $3);
3580 $$ = calloc(1, sizeof(struct node_icmp));
3582 err(1, "icmp_item: calloc");
3584 $$->code = p->code + 1;
3585 $$->proto = IPPROTO_ICMPV6;
3589 | icmp6type CODE NUMBER {
3590 if ($3 < 0 || $3 > 255) {
3591 yyerror("illegal icmp-code %lu", $3);
3594 $$ = calloc(1, sizeof(struct node_icmp));
3596 err(1, "icmp_item: calloc");
3599 $$->proto = IPPROTO_ICMPV6;
3606 const struct icmptypeent *p;
3608 if ((p = geticmptypebyname($1, AF_INET)) == NULL) {
3609 yyerror("unknown icmp-type %s", $1);
3617 if ($1 < 0 || $1 > 255) {
3618 yyerror("illegal icmp-type %lu", $1);
3625 icmp6type : STRING {
3626 const struct icmptypeent *p;
3628 if ((p = geticmptypebyname($1, AF_INET6)) ==
3630 yyerror("unknown icmp6-type %s", $1);
3638 if ($1 < 0 || $1 > 255) {
3639 yyerror("illegal icmp6-type %lu", $1);
3650 if (map_tos($1, &val))
3652 else if ($1[0] == '0' && $1[1] == 'x') {
3654 $$ = strtoul($1, &end, 16);
3655 if (errno || *end != '\0')
3658 $$ = 256; /* flag bad argument */
3659 if ($$ < 0 || $$ > 255) {
3660 yyerror("illegal tos value %s", $1);
3668 if ($$ < 0 || $$ > 255) {
3669 yyerror("illegal tos value %s", $1);
3675 sourcetrack : SOURCETRACK { $$ = PF_SRCTRACK; }
3676 | SOURCETRACK GLOBAL { $$ = PF_SRCTRACK_GLOBAL; }
3677 | SOURCETRACK RULE { $$ = PF_SRCTRACK_RULE; }
3680 statelock : IFBOUND {
3681 $$ = PFRULE_IFBOUND;
3692 | KEEP STATE state_opt_spec {
3693 $$.action = PF_STATE_NORMAL;
3696 | MODULATE STATE state_opt_spec {
3697 $$.action = PF_STATE_MODULATE;
3700 | SYNPROXY STATE state_opt_spec {
3701 $$.action = PF_STATE_SYNPROXY;
3706 flush : /* empty */ { $$ = 0; }
3707 | FLUSH { $$ = PF_FLUSH; }
3709 $$ = PF_FLUSH | PF_FLUSH_GLOBAL;
3713 state_opt_spec : '(' state_opt_list ')' { $$ = $2; }
3714 | /* empty */ { $$ = NULL; }
3717 state_opt_list : state_opt_item { $$ = $1; }
3718 | state_opt_list comma state_opt_item {
3719 $1->tail->next = $3;
3725 state_opt_item : MAXIMUM NUMBER {
3726 if ($2 < 0 || $2 > UINT_MAX) {
3727 yyerror("only positive values permitted");
3730 $$ = calloc(1, sizeof(struct node_state_opt));
3732 err(1, "state_opt_item: calloc");
3733 $$->type = PF_STATE_OPT_MAX;
3734 $$->data.max_states = $2;
3739 $$ = calloc(1, sizeof(struct node_state_opt));
3741 err(1, "state_opt_item: calloc");
3742 $$->type = PF_STATE_OPT_NOSYNC;
3746 | MAXSRCSTATES NUMBER {
3747 if ($2 < 0 || $2 > 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_STATES;
3755 $$->data.max_src_states = $2;
3759 | MAXSRCCONN NUMBER {
3760 if ($2 < 0 || $2 > UINT_MAX) {
3761 yyerror("only positive values permitted");
3764 $$ = calloc(1, sizeof(struct node_state_opt));
3766 err(1, "state_opt_item: calloc");
3767 $$->type = PF_STATE_OPT_MAX_SRC_CONN;
3768 $$->data.max_src_conn = $2;
3772 | MAXSRCCONNRATE NUMBER '/' NUMBER {
3773 if ($2 < 0 || $2 > UINT_MAX ||
3774 $4 < 0 || $4 > UINT_MAX) {
3775 yyerror("only positive values permitted");
3778 $$ = calloc(1, sizeof(struct node_state_opt));
3780 err(1, "state_opt_item: calloc");
3781 $$->type = PF_STATE_OPT_MAX_SRC_CONN_RATE;
3782 $$->data.max_src_conn_rate.limit = $2;
3783 $$->data.max_src_conn_rate.seconds = $4;
3787 | OVERLOAD '<' STRING '>' flush {
3788 if (strlen($3) >= PF_TABLE_NAME_SIZE) {
3789 yyerror("table name '%s' too long", $3);
3793 $$ = calloc(1, sizeof(struct node_state_opt));
3795 err(1, "state_opt_item: calloc");
3796 if (strlcpy($$->data.overload.tblname, $3,
3797 PF_TABLE_NAME_SIZE) >= PF_TABLE_NAME_SIZE)
3798 errx(1, "state_opt_item: strlcpy");
3800 $$->type = PF_STATE_OPT_OVERLOAD;
3801 $$->data.overload.flush = $5;
3805 | MAXSRCNODES NUMBER {
3806 if ($2 < 0 || $2 > UINT_MAX) {
3807 yyerror("only positive values permitted");
3810 $$ = calloc(1, sizeof(struct node_state_opt));
3812 err(1, "state_opt_item: calloc");
3813 $$->type = PF_STATE_OPT_MAX_SRC_NODES;
3814 $$->data.max_src_nodes = $2;
3819 $$ = calloc(1, sizeof(struct node_state_opt));
3821 err(1, "state_opt_item: calloc");
3822 $$->type = PF_STATE_OPT_SRCTRACK;
3823 $$->data.src_track = $1;
3828 $$ = calloc(1, sizeof(struct node_state_opt));
3830 err(1, "state_opt_item: calloc");
3831 $$->type = PF_STATE_OPT_STATELOCK;
3832 $$->data.statelock = $1;
3837 $$ = calloc(1, sizeof(struct node_state_opt));
3839 err(1, "state_opt_item: calloc");
3840 $$->type = PF_STATE_OPT_SLOPPY;
3847 if ($2 < 0 || $2 > UINT_MAX) {
3848 yyerror("only positive values permitted");
3851 for (i = 0; pf_timeouts[i].name &&
3852 strcmp(pf_timeouts[i].name, $1); ++i)
3854 if (!pf_timeouts[i].name) {
3855 yyerror("illegal timeout name %s", $1);
3859 if (strchr(pf_timeouts[i].name, '.') == NULL) {
3860 yyerror("illegal state timeout %s", $1);
3865 $$ = calloc(1, sizeof(struct node_state_opt));
3867 err(1, "state_opt_item: calloc");
3868 $$->type = PF_STATE_OPT_TIMEOUT;
3869 $$->data.timeout.number = pf_timeouts[i].timeout;
3870 $$->data.timeout.seconds = $2;
3876 label : LABEL STRING {
3881 qname : QUEUE STRING {
3885 | QUEUE '(' STRING ')' {
3889 | QUEUE '(' STRING comma STRING ')' {
3895 no : /* empty */ { $$ = 0; }
3899 portstar : numberstring {
3900 if (parseport($1, &$$, PPORT_RANGE|PPORT_STAR) == -1) {
3908 redirspec : host { $$ = $1; }
3909 | '{' optnl redir_host_list '}' { $$ = $3; }
3912 redir_host_list : host optnl { $$ = $1; }
3913 | redir_host_list comma host optnl {
3914 $1->tail->next = $3;
3915 $1->tail = $3->tail;
3920 redirpool : /* empty */ { $$ = NULL; }
3922 $$ = calloc(1, sizeof(struct redirection));
3924 err(1, "redirection: calloc");
3926 $$->rport.a = $$->rport.b = $$->rport.t = 0;
3928 | ARROW redirspec PORT portstar {
3929 $$ = calloc(1, sizeof(struct redirection));
3931 err(1, "redirection: calloc");
3937 hashkey : /* empty */
3939 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3941 err(1, "hashkey: calloc");
3942 $$->key32[0] = arc4random();
3943 $$->key32[1] = arc4random();
3944 $$->key32[2] = arc4random();
3945 $$->key32[3] = arc4random();
3949 if (!strncmp($1, "0x", 2)) {
3950 if (strlen($1) != 34) {
3952 yyerror("hex key must be 128 bits "
3953 "(32 hex digits) long");
3956 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3958 err(1, "hashkey: calloc");
3960 if (sscanf($1, "0x%8x%8x%8x%8x",
3961 &$$->key32[0], &$$->key32[1],
3962 &$$->key32[2], &$$->key32[3]) != 4) {
3965 yyerror("invalid hex key");
3971 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3973 err(1, "hashkey: calloc");
3975 MD5Update(&context, (unsigned char *)$1,
3977 MD5Final((unsigned char *)$$, &context);
3978 HTONL($$->key32[0]);
3979 HTONL($$->key32[1]);
3980 HTONL($$->key32[2]);
3981 HTONL($$->key32[3]);
3987 pool_opts : { bzero(&pool_opts, sizeof pool_opts); }
3991 bzero(&pool_opts, sizeof pool_opts);
3996 pool_opts_l : pool_opts_l pool_opt
4000 pool_opt : BITMASK {
4001 if (pool_opts.type) {
4002 yyerror("pool type cannot be redefined");
4005 pool_opts.type = PF_POOL_BITMASK;
4008 if (pool_opts.type) {
4009 yyerror("pool type cannot be redefined");
4012 pool_opts.type = PF_POOL_RANDOM;
4014 | SOURCEHASH hashkey {
4015 if (pool_opts.type) {
4016 yyerror("pool type cannot be redefined");
4019 pool_opts.type = PF_POOL_SRCHASH;
4023 if (pool_opts.type) {
4024 yyerror("pool type cannot be redefined");
4027 pool_opts.type = PF_POOL_ROUNDROBIN;
4030 if (pool_opts.staticport) {
4031 yyerror("static-port cannot be redefined");
4034 pool_opts.staticport = 1;
4037 if (filter_opts.marker & POM_STICKYADDRESS) {
4038 yyerror("sticky-address cannot be redefined");
4041 pool_opts.marker |= POM_STICKYADDRESS;
4042 pool_opts.opts |= PF_POOL_STICKYADDR;
4044 | MAPEPORTSET number '/' number '/' number {
4045 if (pool_opts.mape.offset) {
4046 yyerror("map-e-portset cannot be redefined");
4049 if (pool_opts.type) {
4050 yyerror("map-e-portset cannot be used with "
4054 if ($2 <= 0 || $2 >= 16) {
4055 yyerror("MAP-E PSID offset must be 1-15");
4058 if ($4 < 0 || $4 >= 16 || $2 + $4 > 16) {
4059 yyerror("Invalid MAP-E PSID length");
4061 } else if ($4 == 0) {
4062 yyerror("PSID Length = 0: this means"
4063 " you do not need MAP-E");
4066 if ($6 < 0 || $6 > 65535) {
4067 yyerror("Invalid MAP-E PSID");
4070 pool_opts.mape.offset = $2;
4071 pool_opts.mape.psidlen = $4;
4072 pool_opts.mape.psid = $6;
4076 redirection : /* empty */ { $$ = NULL; }
4078 $$ = calloc(1, sizeof(struct redirection));
4080 err(1, "redirection: calloc");
4082 $$->rport.a = $$->rport.b = $$->rport.t = 0;
4084 | ARROW host PORT portstar {
4085 $$ = calloc(1, sizeof(struct redirection));
4087 err(1, "redirection: calloc");
4093 natpasslog : /* empty */ { $$.b1 = $$.b2 = 0; $$.w2 = 0; }
4094 | PASS { $$.b1 = 1; $$.b2 = 0; $$.w2 = 0; }
4095 | PASS log { $$.b1 = 1; $$.b2 = $2.log; $$.w2 = $2.logif; }
4096 | log { $$.b1 = 0; $$.b2 = $1.log; $$.w2 = $1.logif; }
4099 nataction : no NAT natpasslog {
4101 yyerror("\"pass\" not valid with \"no\"");
4112 | no RDR natpasslog {
4114 yyerror("\"pass\" not valid with \"no\"");
4127 natrule : nataction interface af proto fromto tag tagged rtable
4130 struct pfctl_rule r;
4132 if (check_rulestate(PFCTL_STATE_NAT))
4135 memset(&r, 0, sizeof(r));
4144 if ($5.src.host && $5.src.host->af &&
4145 !$5.src.host->ifindex)
4146 r.af = $5.src.host->af;
4147 else if ($5.dst.host && $5.dst.host->af &&
4148 !$5.dst.host->ifindex)
4149 r.af = $5.dst.host->af;
4153 if (strlcpy(r.tagname, $6, PF_TAG_NAME_SIZE) >=
4155 yyerror("tag too long, max %u chars",
4156 PF_TAG_NAME_SIZE - 1);
4161 if (strlcpy(r.match_tagname, $7.name,
4162 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4163 yyerror("tag too long, max %u chars",
4164 PF_TAG_NAME_SIZE - 1);
4167 r.match_tag_not = $7.neg;
4170 if (r.action == PF_NONAT || r.action == PF_NORDR) {
4172 yyerror("translation rule with 'no' "
4173 "does not need '->'");
4177 if ($9 == NULL || $9->host == NULL) {
4178 yyerror("translation rule requires '-> "
4182 if (!r.af && ! $9->host->ifindex)
4183 r.af = $9->host->af;
4185 remove_invalid_hosts(&$9->host, &r.af);
4186 if (invalid_redirect($9->host, r.af))
4188 if (check_netmask($9->host, r.af))
4191 r.rpool.proxy_port[0] = ntohs($9->rport.a);
4195 if (!$9->rport.b && $9->rport.t &&
4196 $5.dst.port != NULL) {
4197 r.rpool.proxy_port[1] =
4198 ntohs($9->rport.a) +
4200 $5.dst.port->port[1]) -
4202 $5.dst.port->port[0]));
4204 r.rpool.proxy_port[1] =
4208 r.rpool.proxy_port[1] =
4210 if (!r.rpool.proxy_port[0] &&
4211 !r.rpool.proxy_port[1]) {
4212 r.rpool.proxy_port[0] =
4213 PF_NAT_PROXY_PORT_LOW;
4214 r.rpool.proxy_port[1] =
4215 PF_NAT_PROXY_PORT_HIGH;
4216 } else if (!r.rpool.proxy_port[1])
4217 r.rpool.proxy_port[1] =
4218 r.rpool.proxy_port[0];
4224 r.rpool.opts = $10.type;
4225 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
4226 PF_POOL_NONE && ($9->host->next != NULL ||
4227 $9->host->addr.type == PF_ADDR_TABLE ||
4228 DYNIF_MULTIADDR($9->host->addr)))
4229 r.rpool.opts = PF_POOL_ROUNDROBIN;
4230 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4231 PF_POOL_ROUNDROBIN &&
4232 disallow_table($9->host, "tables are only "
4233 "supported in round-robin redirection "
4236 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4237 PF_POOL_ROUNDROBIN &&
4238 disallow_alias($9->host, "interface (%s) "
4239 "is only supported in round-robin "
4240 "redirection pools"))
4242 if ($9->host->next != NULL) {
4243 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4244 PF_POOL_ROUNDROBIN) {
4245 yyerror("only round-robin "
4246 "valid for multiple "
4247 "redirection addresses");
4253 if ($10.key != NULL)
4254 memcpy(&r.rpool.key, $10.key,
4255 sizeof(struct pf_poolhashkey));
4258 r.rpool.opts |= $10.opts;
4260 if ($10.staticport) {
4261 if (r.action != PF_NAT) {
4262 yyerror("the 'static-port' option is "
4263 "only valid with nat rules");
4266 if (r.rpool.proxy_port[0] !=
4267 PF_NAT_PROXY_PORT_LOW &&
4268 r.rpool.proxy_port[1] !=
4269 PF_NAT_PROXY_PORT_HIGH) {
4270 yyerror("the 'static-port' option can't"
4271 " be used when specifying a port"
4275 r.rpool.proxy_port[0] = 0;
4276 r.rpool.proxy_port[1] = 0;
4279 if ($10.mape.offset) {
4280 if (r.action != PF_NAT) {
4281 yyerror("the 'map-e-portset' option is"
4282 " only valid with nat rules");
4285 if ($10.staticport) {
4286 yyerror("the 'map-e-portset' option"
4287 " can't be used 'static-port'");
4290 if (r.rpool.proxy_port[0] !=
4291 PF_NAT_PROXY_PORT_LOW &&
4292 r.rpool.proxy_port[1] !=
4293 PF_NAT_PROXY_PORT_HIGH) {
4294 yyerror("the 'map-e-portset' option"
4295 " can't be used when specifying"
4299 r.rpool.mape = $10.mape;
4302 expand_rule(&r, $2, $9 == NULL ? NULL : $9->host, $4,
4303 $5.src_os, $5.src.host, $5.src.port, $5.dst.host,
4304 $5.dst.port, 0, 0, 0, "");
4309 binatrule : no BINAT natpasslog interface af proto FROM ipspec toipspec tag
4310 tagged rtable redirection
4312 struct pfctl_rule binat;
4313 struct pf_pooladdr *pa;
4315 if (check_rulestate(PFCTL_STATE_NAT))
4317 if (disallow_urpf_failed($9, "\"urpf-failed\" is not "
4318 "permitted as a binat destination"))
4321 memset(&binat, 0, sizeof(binat));
4324 yyerror("\"pass\" not valid with \"no\"");
4328 binat.action = PF_NOBINAT;
4330 binat.action = PF_BINAT;
4331 binat.natpass = $3.b1;
4333 binat.logif = $3.w2;
4335 if (!binat.af && $8 != NULL && $8->af)
4337 if (!binat.af && $9 != NULL && $9->af)
4340 if (!binat.af && $13 != NULL && $13->host)
4341 binat.af = $13->host->af;
4343 yyerror("address family (inet/inet6) "
4349 memcpy(binat.ifname, $4->ifname,
4350 sizeof(binat.ifname));
4351 binat.ifnot = $4->not;
4356 if (strlcpy(binat.tagname, $10,
4357 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4358 yyerror("tag too long, max %u chars",
4359 PF_TAG_NAME_SIZE - 1);
4363 if (strlcpy(binat.match_tagname, $11.name,
4364 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4365 yyerror("tag too long, max %u chars",
4366 PF_TAG_NAME_SIZE - 1);
4369 binat.match_tag_not = $11.neg;
4370 binat.rtableid = $12;
4373 binat.proto = $6->proto;
4377 if ($8 != NULL && disallow_table($8, "invalid use of "
4378 "table <%s> as the source address of a binat rule"))
4380 if ($8 != NULL && disallow_alias($8, "invalid use of "
4381 "interface (%s) as the source address of a binat "
4384 if ($13 != NULL && $13->host != NULL && disallow_table(
4385 $13->host, "invalid use of table <%s> as the "
4386 "redirect address of a binat rule"))
4388 if ($13 != NULL && $13->host != NULL && disallow_alias(
4389 $13->host, "invalid use of interface (%s) as the "
4390 "redirect address of a binat rule"))
4395 yyerror("multiple binat ip addresses");
4398 if ($8->addr.type == PF_ADDR_DYNIFTL)
4400 if ($8->af != binat.af) {
4401 yyerror("binat ip versions must match");
4404 if (check_netmask($8, binat.af))
4406 memcpy(&binat.src.addr, &$8->addr,
4407 sizeof(binat.src.addr));
4412 yyerror("multiple binat ip addresses");
4415 if ($9->af != binat.af && $9->af) {
4416 yyerror("binat ip versions must match");
4419 if (check_netmask($9, binat.af))
4421 memcpy(&binat.dst.addr, &$9->addr,
4422 sizeof(binat.dst.addr));
4423 binat.dst.neg = $9->not;
4427 if (binat.action == PF_NOBINAT) {
4429 yyerror("'no binat' rule does not need"
4434 if ($13 == NULL || $13->host == NULL) {
4435 yyerror("'binat' rule requires"
4440 remove_invalid_hosts(&$13->host, &binat.af);
4441 if (invalid_redirect($13->host, binat.af))
4443 if ($13->host->next != NULL) {
4444 yyerror("binat rule must redirect to "
4445 "a single address");
4448 if (check_netmask($13->host, binat.af))
4451 if (!PF_AZERO(&binat.src.addr.v.a.mask,
4453 !PF_AEQ(&binat.src.addr.v.a.mask,
4454 &$13->host->addr.v.a.mask, binat.af)) {
4455 yyerror("'binat' source mask and "
4456 "redirect mask must be the same");
4460 TAILQ_INIT(&binat.rpool.list);
4461 pa = calloc(1, sizeof(struct pf_pooladdr));
4463 err(1, "binat: calloc");
4464 pa->addr = $13->host->addr;
4466 TAILQ_INSERT_TAIL(&binat.rpool.list,
4472 pfctl_append_rule(pf, &binat, "");
4476 tag : /* empty */ { $$ = NULL; }
4477 | TAG STRING { $$ = $2; }
4480 tagged : /* empty */ { $$.neg = 0; $$.name = NULL; }
4481 | not TAGGED string { $$.neg = $1; $$.name = $3; }
4484 rtable : /* empty */ { $$ = -1; }
4486 if ($2 < 0 || $2 > rt_tableid_max()) {
4487 yyerror("invalid rtable id");
4494 route_host : STRING {
4495 $$ = calloc(1, sizeof(struct node_host));
4497 err(1, "route_host: calloc");
4498 $$->ifname = strdup($1);
4499 set_ipmask($$, 128);
4503 | '(' STRING host ')' {
4504 struct node_host *n;
4507 for (n = $3; n != NULL; n = n->next)
4508 n->ifname = strdup($2);
4512 route_host_list : route_host optnl { $$ = $1; }
4513 | route_host_list comma route_host optnl {
4516 if ($1->af != $3->af) {
4517 yyerror("all pool addresses must be in the "
4518 "same address family");
4521 $1->tail->next = $3;
4522 $1->tail = $3->tail;
4527 routespec : route_host { $$ = $1; }
4528 | '{' optnl route_host_list '}' { $$ = $3; }
4531 route : /* empty */ {
4537 /* backwards-compat */
4542 | ROUTETO routespec pool_opts {
4545 $$.pool_opts = $3.type | $3.opts;
4549 | REPLYTO routespec pool_opts {
4552 $$.pool_opts = $3.type | $3.opts;
4556 | DUPTO routespec pool_opts {
4559 $$.pool_opts = $3.type | $3.opts;
4565 timeout_spec : STRING NUMBER
4567 if (check_rulestate(PFCTL_STATE_OPTION)) {
4571 if ($2 < 0 || $2 > UINT_MAX) {
4572 yyerror("only positive values permitted");
4575 if (pfctl_set_timeout(pf, $1, $2, 0) != 0) {
4576 yyerror("unknown timeout %s", $1);
4583 if (check_rulestate(PFCTL_STATE_OPTION))
4585 if ($2 < 0 || $2 > UINT_MAX) {
4586 yyerror("only positive values permitted");
4589 if (pfctl_set_timeout(pf, "interval", $2, 0) != 0)
4594 timeout_list : timeout_list comma timeout_spec optnl
4595 | timeout_spec optnl
4598 limit_spec : STRING NUMBER
4600 if (check_rulestate(PFCTL_STATE_OPTION)) {
4604 if ($2 < 0 || $2 > UINT_MAX) {
4605 yyerror("only positive values permitted");
4608 if (pfctl_set_limit(pf, $1, $2) != 0) {
4609 yyerror("unable to set limit %s %u", $1, $2);
4617 limit_list : limit_list comma limit_spec optnl
4625 yesno : NO { $$ = 0; }
4627 if (!strcmp($1, "yes"))
4630 yyerror("invalid value '%s', expected 'yes' "
4639 unaryop : '=' { $$ = PF_OP_EQ; }
4640 | '!' '=' { $$ = PF_OP_NE; }
4641 | '<' '=' { $$ = PF_OP_LE; }
4642 | '<' { $$ = PF_OP_LT; }
4643 | '>' '=' { $$ = PF_OP_GE; }
4644 | '>' { $$ = PF_OP_GT; }
4650 yyerror(const char *fmt, ...)
4656 fprintf(stderr, "%s:%d: ", file->name, yylval.lineno);
4657 vfprintf(stderr, fmt, ap);
4658 fprintf(stderr, "\n");
4664 disallow_table(struct node_host *h, const char *fmt)
4666 for (; h != NULL; h = h->next)
4667 if (h->addr.type == PF_ADDR_TABLE) {
4668 yyerror(fmt, h->addr.v.tblname);
4675 disallow_urpf_failed(struct node_host *h, const char *fmt)
4677 for (; h != NULL; h = h->next)
4678 if (h->addr.type == PF_ADDR_URPFFAILED) {
4686 disallow_alias(struct node_host *h, const char *fmt)
4688 for (; h != NULL; h = h->next)
4689 if (DYNIF_MULTIADDR(h->addr)) {
4690 yyerror(fmt, h->addr.v.tblname);
4697 rule_consistent(struct pfctl_rule *r, int anchor_call)
4701 switch (r->action) {
4706 problems = filter_consistent(r, anchor_call);
4710 problems = nat_consistent(r);
4714 problems = rdr_consistent(r);
4725 filter_consistent(struct pfctl_rule *r, int anchor_call)
4729 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP &&
4730 (r->src.port_op || r->dst.port_op)) {
4731 yyerror("port only applies to tcp/udp");
4734 if (r->proto != IPPROTO_ICMP && r->proto != IPPROTO_ICMPV6 &&
4735 (r->type || r->code)) {
4736 yyerror("icmp-type/code only applies to icmp");
4739 if (!r->af && (r->type || r->code)) {
4740 yyerror("must indicate address family with icmp-type/code");
4743 if (r->overload_tblname[0] &&
4744 r->max_src_conn == 0 && r->max_src_conn_rate.seconds == 0) {
4745 yyerror("'overload' requires 'max-src-conn' "
4746 "or 'max-src-conn-rate'");
4749 if ((r->proto == IPPROTO_ICMP && r->af == AF_INET6) ||
4750 (r->proto == IPPROTO_ICMPV6 && r->af == AF_INET)) {
4751 yyerror("proto %s doesn't match address family %s",
4752 r->proto == IPPROTO_ICMP ? "icmp" : "icmp6",
4753 r->af == AF_INET ? "inet" : "inet6");
4756 if (r->allow_opts && r->action != PF_PASS) {
4757 yyerror("allow-opts can only be specified for pass rules");
4760 if (r->rule_flag & PFRULE_FRAGMENT && (r->src.port_op ||
4761 r->dst.port_op || r->flagset || r->type || r->code)) {
4762 yyerror("fragments can be filtered only on IP header fields");
4765 if (r->rule_flag & PFRULE_RETURNRST && r->proto != IPPROTO_TCP) {
4766 yyerror("return-rst can only be applied to TCP rules");
4769 if (r->max_src_nodes && !(r->rule_flag & PFRULE_RULESRCTRACK)) {
4770 yyerror("max-src-nodes requires 'source-track rule'");
4773 if (r->action == PF_DROP && r->keep_state) {
4774 yyerror("keep state on block rules doesn't make sense");
4777 if (r->rule_flag & PFRULE_STATESLOPPY &&
4778 (r->keep_state == PF_STATE_MODULATE ||
4779 r->keep_state == PF_STATE_SYNPROXY)) {
4780 yyerror("sloppy state matching cannot be used with "
4781 "synproxy state or modulate state");
4788 nat_consistent(struct pfctl_rule *r)
4790 return (0); /* yeah! */
4794 rdr_consistent(struct pfctl_rule *r)
4798 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP) {
4799 if (r->src.port_op) {
4800 yyerror("src port only applies to tcp/udp");
4803 if (r->dst.port_op) {
4804 yyerror("dst port only applies to tcp/udp");
4807 if (r->rpool.proxy_port[0]) {
4808 yyerror("rpool port only applies to tcp/udp");
4812 if (r->dst.port_op &&
4813 r->dst.port_op != PF_OP_EQ && r->dst.port_op != PF_OP_RRG) {
4814 yyerror("invalid port operator for rdr destination port");
4821 process_tabledef(char *name, struct table_opts *opts)
4823 struct pfr_buffer ab;
4824 struct node_tinit *ti;
4825 unsigned long maxcount;
4826 size_t s = sizeof(maxcount);
4828 bzero(&ab, sizeof(ab));
4829 ab.pfrb_type = PFRB_ADDRS;
4830 SIMPLEQ_FOREACH(ti, &opts->init_nodes, entries) {
4832 if (pfr_buf_load(&ab, ti->file, 0, append_addr)) {
4834 yyerror("cannot load \"%s\": %s",
4835 ti->file, strerror(errno));
4837 yyerror("file \"%s\" contains bad data",
4842 if (append_addr_host(&ab, ti->host, 0, 0)) {
4843 yyerror("cannot create address buffer: %s",
4848 if (pf->opts & PF_OPT_VERBOSE)
4849 print_tabledef(name, opts->flags, opts->init_addr,
4851 if (!(pf->opts & PF_OPT_NOACTION) &&
4852 pfctl_define_table(name, opts->flags, opts->init_addr,
4853 pf->anchor->name, &ab, pf->anchor->ruleset.tticket)) {
4855 if (sysctlbyname("net.pf.request_maxcount", &maxcount, &s,
4859 if (ab.pfrb_size > maxcount)
4860 yyerror("cannot define table %s: too many elements.\n"
4861 "Consider increasing net.pf.request_maxcount.",
4864 yyerror("cannot define table %s: %s", name,
4865 pfr_strerror(errno));
4882 /* macro gore, but you should've seen the prior indentation nightmare... */
4884 #define FREE_LIST(T,r) \
4887 while (node != NULL) { \
4889 node = node->next; \
4894 #define LOOP_THROUGH(T,n,r,C) \
4898 r = calloc(1, sizeof(T)); \
4900 err(1, "LOOP: calloc"); \
4904 while (n != NULL) { \
4913 expand_label_str(char *label, size_t len, const char *srch, const char *repl)
4918 if ((tmp = calloc(1, len)) == NULL)
4919 err(1, "expand_label_str: calloc");
4921 while ((q = strstr(p, srch)) != NULL) {
4923 if ((strlcat(tmp, p, len) >= len) ||
4924 (strlcat(tmp, repl, len) >= len))
4925 errx(1, "expand_label: label too long");
4929 if (strlcat(tmp, p, len) >= len)
4930 errx(1, "expand_label: label too long");
4931 strlcpy(label, tmp, len); /* always fits */
4936 expand_label_if(const char *name, char *label, size_t len, const char *ifname)
4938 if (strstr(label, name) != NULL) {
4940 expand_label_str(label, len, name, "any");
4942 expand_label_str(label, len, name, ifname);
4947 expand_label_addr(const char *name, char *label, size_t len, sa_family_t af,
4948 struct node_host *h)
4950 char tmp[64], tmp_not[66];
4952 if (strstr(label, name) != NULL) {
4953 switch (h->addr.type) {
4954 case PF_ADDR_DYNIFTL:
4955 snprintf(tmp, sizeof(tmp), "(%s)", h->addr.v.ifname);
4958 snprintf(tmp, sizeof(tmp), "<%s>", h->addr.v.tblname);
4960 case PF_ADDR_NOROUTE:
4961 snprintf(tmp, sizeof(tmp), "no-route");
4963 case PF_ADDR_URPFFAILED:
4964 snprintf(tmp, sizeof(tmp), "urpf-failed");
4966 case PF_ADDR_ADDRMASK:
4967 if (!af || (PF_AZERO(&h->addr.v.a.addr, af) &&
4968 PF_AZERO(&h->addr.v.a.mask, af)))
4969 snprintf(tmp, sizeof(tmp), "any");
4974 if (inet_ntop(af, &h->addr.v.a.addr, a,
4976 snprintf(tmp, sizeof(tmp), "?");
4978 bits = unmask(&h->addr.v.a.mask, af);
4979 if ((af == AF_INET && bits < 32) ||
4980 (af == AF_INET6 && bits < 128))
4981 snprintf(tmp, sizeof(tmp),
4984 snprintf(tmp, sizeof(tmp),
4990 snprintf(tmp, sizeof(tmp), "?");
4995 snprintf(tmp_not, sizeof(tmp_not), "! %s", tmp);
4996 expand_label_str(label, len, name, tmp_not);
4998 expand_label_str(label, len, name, tmp);
5003 expand_label_port(const char *name, char *label, size_t len,
5004 struct node_port *port)
5006 char a1[6], a2[6], op[13] = "";
5008 if (strstr(label, name) != NULL) {
5009 snprintf(a1, sizeof(a1), "%u", ntohs(port->port[0]));
5010 snprintf(a2, sizeof(a2), "%u", ntohs(port->port[1]));
5013 else if (port->op == PF_OP_IRG)
5014 snprintf(op, sizeof(op), "%s><%s", a1, a2);
5015 else if (port->op == PF_OP_XRG)
5016 snprintf(op, sizeof(op), "%s<>%s", a1, a2);
5017 else if (port->op == PF_OP_EQ)
5018 snprintf(op, sizeof(op), "%s", a1);
5019 else if (port->op == PF_OP_NE)
5020 snprintf(op, sizeof(op), "!=%s", a1);
5021 else if (port->op == PF_OP_LT)
5022 snprintf(op, sizeof(op), "<%s", a1);
5023 else if (port->op == PF_OP_LE)
5024 snprintf(op, sizeof(op), "<=%s", a1);
5025 else if (port->op == PF_OP_GT)
5026 snprintf(op, sizeof(op), ">%s", a1);
5027 else if (port->op == PF_OP_GE)
5028 snprintf(op, sizeof(op), ">=%s", a1);
5029 expand_label_str(label, len, name, op);
5034 expand_label_proto(const char *name, char *label, size_t len, u_int8_t proto)
5036 const char *protoname;
5039 if (strstr(label, name) != NULL) {
5040 protoname = pfctl_proto2name(proto);
5041 if (protoname != NULL)
5042 expand_label_str(label, len, name, protoname);
5044 snprintf(n, sizeof(n), "%u", proto);
5045 expand_label_str(label, len, name, n);
5051 expand_label_nr(const char *name, char *label, size_t len)
5055 if (strstr(label, name) != NULL) {
5056 snprintf(n, sizeof(n), "%u", pf->anchor->match);
5057 expand_label_str(label, len, name, n);
5062 expand_label(char *label, size_t len, const char *ifname, sa_family_t af,
5063 struct node_host *src_host, struct node_port *src_port,
5064 struct node_host *dst_host, struct node_port *dst_port,
5067 expand_label_if("$if", label, len, ifname);
5068 expand_label_addr("$srcaddr", label, len, af, src_host);
5069 expand_label_addr("$dstaddr", label, len, af, dst_host);
5070 expand_label_port("$srcport", label, len, src_port);
5071 expand_label_port("$dstport", label, len, dst_port);
5072 expand_label_proto("$proto", label, len, proto);
5073 expand_label_nr("$nr", label, len);
5077 expand_altq(struct pf_altq *a, struct node_if *interfaces,
5078 struct node_queue *nqueues, struct node_queue_bw bwspec,
5079 struct node_queue_opt *opts)
5081 struct pf_altq pa, pb;
5082 char qname[PF_QNAME_SIZE];
5083 struct node_queue *n;
5084 struct node_queue_bw bw;
5087 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
5088 FREE_LIST(struct node_if, interfaces);
5090 FREE_LIST(struct node_queue, nqueues);
5094 LOOP_THROUGH(struct node_if, interface, interfaces,
5095 memcpy(&pa, a, sizeof(struct pf_altq));
5096 if (strlcpy(pa.ifname, interface->ifname,
5097 sizeof(pa.ifname)) >= sizeof(pa.ifname))
5098 errx(1, "expand_altq: strlcpy");
5100 if (interface->not) {
5101 yyerror("altq on ! <interface> is not supported");
5104 if (eval_pfaltq(pf, &pa, &bwspec, opts))
5107 if (pfctl_add_altq(pf, &pa))
5110 if (pf->opts & PF_OPT_VERBOSE) {
5111 print_altq(&pf->paltq->altq, 0,
5113 if (nqueues && nqueues->tail) {
5115 LOOP_THROUGH(struct node_queue, queue,
5125 if (pa.scheduler == ALTQT_CBQ ||
5126 pa.scheduler == ALTQT_HFSC ||
5127 pa.scheduler == ALTQT_FAIRQ) {
5128 /* now create a root queue */
5129 memset(&pb, 0, sizeof(struct pf_altq));
5130 if (strlcpy(qname, "root_", sizeof(qname)) >=
5132 errx(1, "expand_altq: strlcpy");
5133 if (strlcat(qname, interface->ifname,
5134 sizeof(qname)) >= sizeof(qname))
5135 errx(1, "expand_altq: strlcat");
5136 if (strlcpy(pb.qname, qname,
5137 sizeof(pb.qname)) >= sizeof(pb.qname))
5138 errx(1, "expand_altq: strlcpy");
5139 if (strlcpy(pb.ifname, interface->ifname,
5140 sizeof(pb.ifname)) >= sizeof(pb.ifname))
5141 errx(1, "expand_altq: strlcpy");
5142 pb.qlimit = pa.qlimit;
5143 pb.scheduler = pa.scheduler;
5144 bw.bw_absolute = pa.ifbandwidth;
5146 if (eval_pfqueue(pf, &pb, &bw, opts))
5149 if (pfctl_add_altq(pf, &pb))
5153 LOOP_THROUGH(struct node_queue, queue, nqueues,
5154 n = calloc(1, sizeof(struct node_queue));
5156 err(1, "expand_altq: calloc");
5157 if (pa.scheduler == ALTQT_CBQ ||
5158 pa.scheduler == ALTQT_HFSC ||
5159 pa.scheduler == ALTQT_FAIRQ)
5160 if (strlcpy(n->parent, qname,
5161 sizeof(n->parent)) >=
5163 errx(1, "expand_altq: strlcpy");
5164 if (strlcpy(n->queue, queue->queue,
5165 sizeof(n->queue)) >= sizeof(n->queue))
5166 errx(1, "expand_altq: strlcpy");
5167 if (strlcpy(n->ifname, interface->ifname,
5168 sizeof(n->ifname)) >= sizeof(n->ifname))
5169 errx(1, "expand_altq: strlcpy");
5170 n->scheduler = pa.scheduler;
5176 queues->tail->next = n;
5182 FREE_LIST(struct node_if, interfaces);
5184 FREE_LIST(struct node_queue, nqueues);
5190 expand_queue(struct pf_altq *a, struct node_if *interfaces,
5191 struct node_queue *nqueues, struct node_queue_bw bwspec,
5192 struct node_queue_opt *opts)
5194 struct node_queue *n, *nq;
5199 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
5200 FREE_LIST(struct node_queue, nqueues);
5204 if (queues == NULL) {
5205 yyerror("queue %s has no parent", a->qname);
5206 FREE_LIST(struct node_queue, nqueues);
5210 LOOP_THROUGH(struct node_if, interface, interfaces,
5211 LOOP_THROUGH(struct node_queue, tqueue, queues,
5212 if (!strncmp(a->qname, tqueue->queue, PF_QNAME_SIZE) &&
5213 (interface->ifname[0] == 0 ||
5214 (!interface->not && !strncmp(interface->ifname,
5215 tqueue->ifname, IFNAMSIZ)) ||
5216 (interface->not && strncmp(interface->ifname,
5217 tqueue->ifname, IFNAMSIZ)))) {
5218 /* found ourself in queues */
5221 memcpy(&pa, a, sizeof(struct pf_altq));
5223 if (pa.scheduler != ALTQT_NONE &&
5224 pa.scheduler != tqueue->scheduler) {
5225 yyerror("exactly one scheduler type "
5226 "per interface allowed");
5229 pa.scheduler = tqueue->scheduler;
5231 /* scheduler dependent error checking */
5232 switch (pa.scheduler) {
5234 if (nqueues != NULL) {
5235 yyerror("priq queues cannot "
5236 "have child queues");
5239 if (bwspec.bw_absolute > 0 ||
5240 bwspec.bw_percent < 100) {
5241 yyerror("priq doesn't take "
5250 if (strlcpy(pa.ifname, tqueue->ifname,
5251 sizeof(pa.ifname)) >= sizeof(pa.ifname))
5252 errx(1, "expand_queue: strlcpy");
5253 if (strlcpy(pa.parent, tqueue->parent,
5254 sizeof(pa.parent)) >= sizeof(pa.parent))
5255 errx(1, "expand_queue: strlcpy");
5257 if (eval_pfqueue(pf, &pa, &bwspec, opts))
5260 if (pfctl_add_altq(pf, &pa))
5263 for (nq = nqueues; nq != NULL; nq = nq->next) {
5264 if (!strcmp(a->qname, nq->queue)) {
5265 yyerror("queue cannot have "
5271 sizeof(struct node_queue));
5273 err(1, "expand_queue: calloc");
5274 if (strlcpy(n->parent, a->qname,
5275 sizeof(n->parent)) >=
5277 errx(1, "expand_queue strlcpy");
5278 if (strlcpy(n->queue, nq->queue,
5279 sizeof(n->queue)) >=
5281 errx(1, "expand_queue strlcpy");
5282 if (strlcpy(n->ifname, tqueue->ifname,
5283 sizeof(n->ifname)) >=
5285 errx(1, "expand_queue strlcpy");
5286 n->scheduler = tqueue->scheduler;
5292 queues->tail->next = n;
5296 if ((pf->opts & PF_OPT_VERBOSE) && (
5297 (found == 1 && interface->ifname[0] == 0) ||
5298 (found > 0 && interface->ifname[0] != 0))) {
5299 print_queue(&pf->paltq->altq, 0,
5300 &bwspec, interface->ifname[0] != 0,
5302 if (nqueues && nqueues->tail) {
5304 LOOP_THROUGH(struct node_queue,
5317 FREE_LIST(struct node_queue, nqueues);
5318 FREE_LIST(struct node_if, interfaces);
5321 yyerror("queue %s has no parent", a->qname);
5332 expand_rule(struct pfctl_rule *r,
5333 struct node_if *interfaces, struct node_host *rpool_hosts,
5334 struct node_proto *protos, struct node_os *src_oses,
5335 struct node_host *src_hosts, struct node_port *src_ports,
5336 struct node_host *dst_hosts, struct node_port *dst_ports,
5337 struct node_uid *uids, struct node_gid *gids, struct node_icmp *icmp_types,
5338 const char *anchor_call)
5340 sa_family_t af = r->af;
5341 int added = 0, error = 0;
5342 char ifname[IF_NAMESIZE];
5343 char label[PF_RULE_MAX_LABEL_COUNT][PF_RULE_LABEL_SIZE];
5344 char tagname[PF_TAG_NAME_SIZE];
5345 char match_tagname[PF_TAG_NAME_SIZE];
5346 struct pf_pooladdr *pa;
5347 struct node_host *h;
5348 u_int8_t flags, flagset, keep_state;
5350 memcpy(label, r->label, sizeof(r->label));
5351 assert(sizeof(r->label) == sizeof(label));
5352 if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname))
5353 errx(1, "expand_rule: strlcpy");
5354 if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >=
5355 sizeof(match_tagname))
5356 errx(1, "expand_rule: strlcpy");
5358 flagset = r->flagset;
5359 keep_state = r->keep_state;
5361 LOOP_THROUGH(struct node_if, interface, interfaces,
5362 LOOP_THROUGH(struct node_proto, proto, protos,
5363 LOOP_THROUGH(struct node_icmp, icmp_type, icmp_types,
5364 LOOP_THROUGH(struct node_host, src_host, src_hosts,
5365 LOOP_THROUGH(struct node_port, src_port, src_ports,
5366 LOOP_THROUGH(struct node_os, src_os, src_oses,
5367 LOOP_THROUGH(struct node_host, dst_host, dst_hosts,
5368 LOOP_THROUGH(struct node_port, dst_port, dst_ports,
5369 LOOP_THROUGH(struct node_uid, uid, uids,
5370 LOOP_THROUGH(struct node_gid, gid, gids,
5373 /* for link-local IPv6 address, interface must match up */
5374 if ((r->af && src_host->af && r->af != src_host->af) ||
5375 (r->af && dst_host->af && r->af != dst_host->af) ||
5376 (src_host->af && dst_host->af &&
5377 src_host->af != dst_host->af) ||
5378 (src_host->ifindex && dst_host->ifindex &&
5379 src_host->ifindex != dst_host->ifindex) ||
5380 (src_host->ifindex && *interface->ifname &&
5381 src_host->ifindex != if_nametoindex(interface->ifname)) ||
5382 (dst_host->ifindex && *interface->ifname &&
5383 dst_host->ifindex != if_nametoindex(interface->ifname)))
5385 if (!r->af && src_host->af)
5386 r->af = src_host->af;
5387 else if (!r->af && dst_host->af)
5388 r->af = dst_host->af;
5390 if (*interface->ifname)
5391 strlcpy(r->ifname, interface->ifname,
5393 else if (if_indextoname(src_host->ifindex, ifname))
5394 strlcpy(r->ifname, ifname, sizeof(r->ifname));
5395 else if (if_indextoname(dst_host->ifindex, ifname))
5396 strlcpy(r->ifname, ifname, sizeof(r->ifname));
5398 memset(r->ifname, '\0', sizeof(r->ifname));
5400 memcpy(r->label, label, sizeof(r->label));
5401 if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >=
5403 errx(1, "expand_rule: strlcpy");
5404 if (strlcpy(r->match_tagname, match_tagname,
5405 sizeof(r->match_tagname)) >= sizeof(r->match_tagname))
5406 errx(1, "expand_rule: strlcpy");
5407 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++)
5408 expand_label(r->label[i], PF_RULE_LABEL_SIZE,
5409 r->ifname, r->af, src_host, src_port, dst_host,
5410 dst_port, proto->proto);
5411 expand_label(r->tagname, PF_TAG_NAME_SIZE, r->ifname, r->af,
5412 src_host, src_port, dst_host, dst_port, proto->proto);
5413 expand_label(r->match_tagname, PF_TAG_NAME_SIZE, r->ifname,
5414 r->af, src_host, src_port, dst_host, dst_port,
5417 error += check_netmask(src_host, r->af);
5418 error += check_netmask(dst_host, r->af);
5420 r->ifnot = interface->not;
5421 r->proto = proto->proto;
5422 r->src.addr = src_host->addr;
5423 r->src.neg = src_host->not;
5424 r->src.port[0] = src_port->port[0];
5425 r->src.port[1] = src_port->port[1];
5426 r->src.port_op = src_port->op;
5427 r->dst.addr = dst_host->addr;
5428 r->dst.neg = dst_host->not;
5429 r->dst.port[0] = dst_port->port[0];
5430 r->dst.port[1] = dst_port->port[1];
5431 r->dst.port_op = dst_port->op;
5432 r->uid.op = uid->op;
5433 r->uid.uid[0] = uid->uid[0];
5434 r->uid.uid[1] = uid->uid[1];
5435 r->gid.op = gid->op;
5436 r->gid.gid[0] = gid->gid[0];
5437 r->gid.gid[1] = gid->gid[1];
5438 r->type = icmp_type->type;
5439 r->code = icmp_type->code;
5441 if ((keep_state == PF_STATE_MODULATE ||
5442 keep_state == PF_STATE_SYNPROXY) &&
5443 r->proto && r->proto != IPPROTO_TCP)
5444 r->keep_state = PF_STATE_NORMAL;
5446 r->keep_state = keep_state;
5448 if (r->proto && r->proto != IPPROTO_TCP) {
5453 r->flagset = flagset;
5455 if (icmp_type->proto && r->proto != icmp_type->proto) {
5456 yyerror("icmp-type mismatch");
5460 if (src_os && src_os->os) {
5461 r->os_fingerprint = pfctl_get_fingerprint(src_os->os);
5462 if ((pf->opts & PF_OPT_VERBOSE2) &&
5463 r->os_fingerprint == PF_OSFP_NOMATCH)
5465 "warning: unknown '%s' OS fingerprint\n",
5468 r->os_fingerprint = PF_OSFP_ANY;
5471 TAILQ_INIT(&r->rpool.list);
5472 for (h = rpool_hosts; h != NULL; h = h->next) {
5473 pa = calloc(1, sizeof(struct pf_pooladdr));
5475 err(1, "expand_rule: calloc");
5477 if (h->ifname != NULL) {
5478 if (strlcpy(pa->ifname, h->ifname,
5479 sizeof(pa->ifname)) >=
5481 errx(1, "expand_rule: strlcpy");
5484 TAILQ_INSERT_TAIL(&r->rpool.list, pa, entries);
5487 if (rule_consistent(r, anchor_call[0]) < 0 || error)
5488 yyerror("skipping rule due to errors");
5490 r->nr = pf->astack[pf->asd]->match++;
5491 pfctl_append_rule(pf, r, anchor_call);
5497 FREE_LIST(struct node_if, interfaces);
5498 FREE_LIST(struct node_proto, protos);
5499 FREE_LIST(struct node_host, src_hosts);
5500 FREE_LIST(struct node_port, src_ports);
5501 FREE_LIST(struct node_os, src_oses);
5502 FREE_LIST(struct node_host, dst_hosts);
5503 FREE_LIST(struct node_port, dst_ports);
5504 FREE_LIST(struct node_uid, uids);
5505 FREE_LIST(struct node_gid, gids);
5506 FREE_LIST(struct node_icmp, icmp_types);
5507 FREE_LIST(struct node_host, rpool_hosts);
5510 yyerror("rule expands to no valid combination");
5514 expand_skip_interface(struct node_if *interfaces)
5518 if (!interfaces || (!interfaces->next && !interfaces->not &&
5519 !strcmp(interfaces->ifname, "none"))) {
5520 if (pf->opts & PF_OPT_VERBOSE)
5521 printf("set skip on none\n");
5522 errs = pfctl_set_interface_flags(pf, "", PFI_IFLAG_SKIP, 0);
5526 if (pf->opts & PF_OPT_VERBOSE)
5527 printf("set skip on {");
5528 LOOP_THROUGH(struct node_if, interface, interfaces,
5529 if (pf->opts & PF_OPT_VERBOSE)
5530 printf(" %s", interface->ifname);
5531 if (interface->not) {
5532 yyerror("skip on ! <interface> is not supported");
5535 errs += pfctl_set_interface_flags(pf,
5536 interface->ifname, PFI_IFLAG_SKIP, 1);
5538 if (pf->opts & PF_OPT_VERBOSE)
5541 FREE_LIST(struct node_if, interfaces);
5553 check_rulestate(int desired_state)
5555 if (require_order && (rulestate > desired_state)) {
5556 yyerror("Rules must be in order: options, normalization, "
5557 "queueing, translation, filtering");
5560 rulestate = desired_state;
5565 kw_cmp(const void *k, const void *e)
5567 return (strcmp(k, ((const struct keywords *)e)->k_name));
5573 /* this has to be sorted always */
5574 static const struct keywords keywords[] = {
5576 { "allow-opts", ALLOWOPTS},
5578 { "anchor", ANCHOR},
5579 { "antispoof", ANTISPOOF},
5581 { "bandwidth", BANDWIDTH},
5583 { "binat-anchor", BINATANCHOR},
5584 { "bitmask", BITMASK},
5586 { "block-policy", BLOCKPOLICY},
5587 { "buckets", BUCKETS},
5591 { "crop", FRAGCROP},
5593 { "divert-reply", DIVERTREPLY},
5594 { "divert-to", DIVERTTO},
5596 { "drop-ovl", FRAGDROP},
5598 { "fail-policy", FAILPOLICY},
5600 { "fastroute", FASTROUTE},
5601 { "file", FILENAME},
5602 { "fingerprints", FINGERPRINTS},
5604 { "floating", FLOATING},
5607 { "fragment", FRAGMENT},
5609 { "global", GLOBAL},
5613 { "hostid", HOSTID},
5614 { "icmp-type", ICMPTYPE},
5615 { "icmp6-type", ICMP6TYPE},
5616 { "if-bound", IFBOUND},
5618 { "include", INCLUDE},
5621 { "interval", INTERVAL},
5623 { "keepcounters", KEEPCOUNTERS},
5626 { "linkshare", LINKSHARE},
5629 { "loginterface", LOGINTERFACE},
5630 { "map-e-portset", MAPEPORTSET},
5633 { "max-mss", MAXMSS},
5634 { "max-src-conn", MAXSRCCONN},
5635 { "max-src-conn-rate", MAXSRCCONNRATE},
5636 { "max-src-nodes", MAXSRCNODES},
5637 { "max-src-states", MAXSRCSTATES},
5638 { "min-ttl", MINTTL},
5639 { "modulate", MODULATE},
5641 { "nat-anchor", NATANCHOR},
5644 { "no-route", NOROUTE},
5645 { "no-sync", NOSYNC},
5647 { "optimization", OPTIMIZATION},
5650 { "overload", OVERLOAD},
5654 { "priority", PRIORITY},
5656 { "probability", PROBABILITY},
5658 { "qlimit", QLIMIT},
5661 { "random", RANDOM},
5662 { "random-id", RANDOMID},
5664 { "rdr-anchor", RDRANCHOR},
5665 { "realtime", REALTIME},
5666 { "reassemble", REASSEMBLE},
5667 { "reply-to", REPLYTO},
5668 { "require-order", REQUIREORDER},
5669 { "return", RETURN},
5670 { "return-icmp", RETURNICMP},
5671 { "return-icmp6", RETURNICMP6},
5672 { "return-rst", RETURNRST},
5673 { "round-robin", ROUNDROBIN},
5675 { "route-to", ROUTETO},
5676 { "rtable", RTABLE},
5678 { "ruleset-optimization", RULESET_OPTIMIZATION},
5681 { "set-tos", SETTOS},
5683 { "sloppy", SLOPPY},
5684 { "source-hash", SOURCEHASH},
5685 { "source-track", SOURCETRACK},
5687 { "state-defaults", STATEDEFAULTS},
5688 { "state-policy", STATEPOLICY},
5689 { "static-port", STATICPORT},
5690 { "sticky-address", STICKYADDRESS},
5691 { "syncookies", SYNCOOKIES},
5692 { "synproxy", SYNPROXY},
5695 { "tagged", TAGGED},
5696 { "target", TARGET},
5697 { "tbrsize", TBRSIZE},
5698 { "timeout", TIMEOUT},
5702 { "upperlimit", UPPERLIMIT},
5703 { "urpf-failed", URPFFAILED},
5706 const struct keywords *p;
5708 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
5709 sizeof(keywords[0]), kw_cmp);
5713 fprintf(stderr, "%s: %d\n", s, p->k_val);
5717 fprintf(stderr, "string: %s\n", s);
5722 #define MAXPUSHBACK 128
5724 static char *parsebuf;
5725 static int parseindex;
5726 static char pushback_buffer[MAXPUSHBACK];
5727 static int pushback_index = 0;
5735 /* Read character from the parsebuffer instead of input. */
5736 if (parseindex >= 0) {
5737 c = parsebuf[parseindex++];
5746 return (pushback_buffer[--pushback_index]);
5749 if ((c = getc(file->stream)) == EOF) {
5750 yyerror("reached end of file while parsing quoted string");
5751 if (popfile() == EOF)
5758 while ((c = getc(file->stream)) == '\\') {
5759 next = getc(file->stream);
5764 yylval.lineno = file->lineno;
5769 if (popfile() == EOF)
5771 c = getc(file->stream);
5783 if (parseindex >= 0)
5786 if (pushback_index < MAXPUSHBACK-1)
5787 return (pushback_buffer[pushback_index++] = c);
5799 /* skip to either EOF or the first real EOL */
5802 c = pushback_buffer[--pushback_index];
5820 int quotec, next, c;
5825 while ((c = lgetc(0)) == ' ' || c == '\t')
5828 yylval.lineno = file->lineno;
5830 while ((c = lgetc(0)) != '\n' && c != EOF)
5832 if (c == '$' && parsebuf == NULL) {
5834 if ((c = lgetc(0)) == EOF)
5837 if (p + 1 >= buf + sizeof(buf) - 1) {
5838 yyerror("string too long");
5841 if (isalnum(c) || c == '_') {
5851 yyerror("macro '%s' not defined", buf);
5864 if ((c = lgetc(quotec)) == EOF)
5869 } else if (c == '\\') {
5870 if ((next = lgetc(quotec)) == EOF)
5872 if (next == quotec || c == ' ' || c == '\t')
5874 else if (next == '\n') {
5880 } else if (c == quotec) {
5884 if (p + 1 >= buf + sizeof(buf) - 1) {
5885 yyerror("string too long");
5890 yylval.v.string = strdup(buf);
5891 if (yylval.v.string == NULL)
5892 err(1, "yylex: strdup");
5897 yylval.v.i = PF_OP_XRG;
5898 return (PORTBINARY);
5905 yylval.v.i = PF_OP_IRG;
5906 return (PORTBINARY);
5918 #define allowed_to_end_number(x) \
5919 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
5921 if (c == '-' || isdigit(c)) {
5924 if ((unsigned)(p-buf) >= sizeof(buf)) {
5925 yyerror("string too long");
5928 } while ((c = lgetc(0)) != EOF && isdigit(c));
5930 if (p == buf + 1 && buf[0] == '-')
5932 if (c == EOF || allowed_to_end_number(c)) {
5933 const char *errstr = NULL;
5936 yylval.v.number = strtonum(buf, LLONG_MIN,
5937 LLONG_MAX, &errstr);
5939 yyerror("\"%s\" invalid number: %s",
5954 #define allowed_in_string(x) \
5955 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
5956 x != '{' && x != '}' && x != '<' && x != '>' && \
5957 x != '!' && x != '=' && x != '/' && x != '#' && \
5960 if (isalnum(c) || c == ':' || c == '_') {
5963 if ((unsigned)(p-buf) >= sizeof(buf)) {
5964 yyerror("string too long");
5967 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
5970 if ((token = lookup(buf)) == STRING)
5971 if ((yylval.v.string = strdup(buf)) == NULL)
5972 err(1, "yylex: strdup");
5976 yylval.lineno = file->lineno;
5985 check_file_secrecy(int fd, const char *fname)
5989 if (fstat(fd, &st)) {
5990 warn("cannot stat %s", fname);
5993 if (st.st_uid != 0 && st.st_uid != getuid()) {
5994 warnx("%s: owner not root or current user", fname);
5997 if (st.st_mode & (S_IRWXG | S_IRWXO)) {
5998 warnx("%s: group/world readable/writeable", fname);
6005 pushfile(const char *name, int secret)
6009 if ((nfile = calloc(1, sizeof(struct file))) == NULL ||
6010 (nfile->name = strdup(name)) == NULL) {
6014 if (TAILQ_FIRST(&files) == NULL && strcmp(nfile->name, "-") == 0) {
6015 nfile->stream = stdin;
6017 if ((nfile->name = strdup("stdin")) == NULL) {
6022 } else if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
6023 warn("%s", nfile->name);
6027 } else if (secret &&
6028 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
6029 fclose(nfile->stream);
6035 TAILQ_INSERT_TAIL(&files, nfile, entry);
6044 if ((prev = TAILQ_PREV(file, files, entry)) != NULL) {
6045 prev->errors += file->errors;
6046 TAILQ_REMOVE(&files, file, entry);
6047 fclose(file->stream);
6057 parse_config(char *filename, struct pfctl *xpf)
6064 rulestate = PFCTL_STATE_NONE;
6065 returnicmpdefault = (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
6066 returnicmp6default =
6067 (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
6068 blockpolicy = PFRULE_DROP;
6069 failpolicy = PFRULE_DROP;
6072 if ((file = pushfile(filename, 0)) == NULL) {
6073 warn("cannot open the main config file!");
6078 errors = file->errors;
6081 /* Free macros and check which have not been used. */
6082 while ((sym = TAILQ_FIRST(&symhead))) {
6083 if ((pf->opts & PF_OPT_VERBOSE2) && !sym->used)
6084 fprintf(stderr, "warning: macro '%s' not "
6085 "used\n", sym->nam);
6088 TAILQ_REMOVE(&symhead, sym, entry);
6092 return (errors ? -1 : 0);
6096 symset(const char *nam, const char *val, int persist)
6100 for (sym = TAILQ_FIRST(&symhead); sym && strcmp(nam, sym->nam);
6101 sym = TAILQ_NEXT(sym, entry))
6105 if (sym->persist == 1)
6110 TAILQ_REMOVE(&symhead, sym, entry);
6114 if ((sym = calloc(1, sizeof(*sym))) == NULL)
6117 sym->nam = strdup(nam);
6118 if (sym->nam == NULL) {
6122 sym->val = strdup(val);
6123 if (sym->val == NULL) {
6129 sym->persist = persist;
6130 TAILQ_INSERT_TAIL(&symhead, sym, entry);
6135 pfctl_cmdline_symset(char *s)
6140 if ((val = strrchr(s, '=')) == NULL)
6143 if ((sym = malloc(strlen(s) - strlen(val) + 1)) == NULL)
6144 err(1, "pfctl_cmdline_symset: malloc");
6146 strlcpy(sym, s, strlen(s) - strlen(val) + 1);
6148 ret = symset(sym, val + 1, 1);
6155 symget(const char *nam)
6159 TAILQ_FOREACH(sym, &symhead, entry)
6160 if (strcmp(nam, sym->nam) == 0) {
6168 mv_rules(struct pfctl_ruleset *src, struct pfctl_ruleset *dst)
6171 struct pfctl_rule *r;
6173 for (i = 0; i < PF_RULESET_MAX; ++i) {
6174 while ((r = TAILQ_FIRST(src->rules[i].active.ptr))
6176 TAILQ_REMOVE(src->rules[i].active.ptr, r, entries);
6177 TAILQ_INSERT_TAIL(dst->rules[i].active.ptr, r, entries);
6178 dst->anchor->match++;
6180 src->anchor->match = 0;
6181 while ((r = TAILQ_FIRST(src->rules[i].inactive.ptr))
6183 TAILQ_REMOVE(src->rules[i].inactive.ptr, r, entries);
6184 TAILQ_INSERT_TAIL(dst->rules[i].inactive.ptr,
6191 decide_address_family(struct node_host *n, sa_family_t *af)
6193 if (*af != 0 || n == NULL)
6196 while ((n = n->next) != NULL) {
6205 remove_invalid_hosts(struct node_host **nh, sa_family_t *af)
6207 struct node_host *n = *nh, *prev = NULL;
6210 if (*af && n->af && n->af != *af) {
6211 /* unlink and free n */
6212 struct node_host *next = n->next;
6214 /* adjust tail pointer */
6215 if (n == (*nh)->tail)
6217 /* adjust previous node's next pointer */
6223 if (n->ifname != NULL)
6237 invalid_redirect(struct node_host *nh, sa_family_t af)
6240 struct node_host *n;
6242 /* tables and dyniftl are ok without an address family */
6243 for (n = nh; n != NULL; n = n->next) {
6244 if (n->addr.type != PF_ADDR_TABLE &&
6245 n->addr.type != PF_ADDR_DYNIFTL) {
6246 yyerror("address family not given and "
6247 "translation address expands to multiple "
6248 "address families");
6254 yyerror("no translation address with matching address family "
6262 atoul(char *s, u_long *ulvalp)
6268 ulval = strtoul(s, &ep, 0);
6269 if (s[0] == '\0' || *ep != '\0')
6271 if (errno == ERANGE && ulval == ULONG_MAX)
6283 if (atoul(n, &ulval) == 0) {
6284 if (ulval > 65535) {
6285 yyerror("illegal port value %lu", ulval);
6288 return (htons(ulval));
6290 s = getservbyname(n, "tcp");
6292 s = getservbyname(n, "udp");
6294 yyerror("unknown port %s", n);
6302 rule_label(struct pfctl_rule *r, char *s[PF_RULE_MAX_LABEL_COUNT])
6304 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++) {
6308 if (strlcpy(r->label[i], s[i], sizeof(r->label[0])) >=
6309 sizeof(r->label[0])) {
6310 yyerror("rule label too long (max %d chars)",
6311 sizeof(r->label[0])-1);
6319 parseicmpspec(char *w, sa_family_t af)
6321 const struct icmpcodeent *p;
6326 icmptype = returnicmpdefault >> 8;
6328 icmptype = returnicmp6default >> 8;
6330 if (atoul(w, &ulval) == -1) {
6331 if ((p = geticmpcodebyname(icmptype, w, af)) == NULL) {
6332 yyerror("unknown icmp code %s", w);
6338 yyerror("invalid icmp code %lu", ulval);
6341 return (icmptype << 8 | ulval);
6345 parseport(char *port, struct range *r, int extensions)
6347 char *p = strchr(port, ':');
6350 if ((r->a = getservice(port)) == -1)
6356 if ((extensions & PPORT_STAR) && !strcmp(p+1, "*")) {
6358 if ((r->a = getservice(port)) == -1)
6364 if ((extensions & PPORT_RANGE)) {
6366 if ((r->a = getservice(port)) == -1 ||
6367 (r->b = getservice(p)) == -1)
6380 pfctl_load_anchors(int dev, struct pfctl *pf, struct pfr_buffer *trans)
6382 struct loadanchors *la;
6384 TAILQ_FOREACH(la, &loadanchorshead, entries) {
6385 if (pf->opts & PF_OPT_VERBOSE)
6386 fprintf(stderr, "\nLoading anchor %s from %s\n",
6387 la->anchorname, la->filename);
6388 if (pfctl_rules(dev, la->filename, pf->opts, pf->optimize,
6389 la->anchorname, trans) == -1)
6397 kw_casecmp(const void *k, const void *e)
6399 return (strcasecmp(k, ((const struct keywords *)e)->k_name));
6403 map_tos(char *s, int *val)
6405 /* DiffServ Codepoints and other TOS mappings */
6406 const struct keywords toswords[] = {
6407 { "af11", IPTOS_DSCP_AF11 },
6408 { "af12", IPTOS_DSCP_AF12 },
6409 { "af13", IPTOS_DSCP_AF13 },
6410 { "af21", IPTOS_DSCP_AF21 },
6411 { "af22", IPTOS_DSCP_AF22 },
6412 { "af23", IPTOS_DSCP_AF23 },
6413 { "af31", IPTOS_DSCP_AF31 },
6414 { "af32", IPTOS_DSCP_AF32 },
6415 { "af33", IPTOS_DSCP_AF33 },
6416 { "af41", IPTOS_DSCP_AF41 },
6417 { "af42", IPTOS_DSCP_AF42 },
6418 { "af43", IPTOS_DSCP_AF43 },
6419 { "critical", IPTOS_PREC_CRITIC_ECP },
6420 { "cs0", IPTOS_DSCP_CS0 },
6421 { "cs1", IPTOS_DSCP_CS1 },
6422 { "cs2", IPTOS_DSCP_CS2 },
6423 { "cs3", IPTOS_DSCP_CS3 },
6424 { "cs4", IPTOS_DSCP_CS4 },
6425 { "cs5", IPTOS_DSCP_CS5 },
6426 { "cs6", IPTOS_DSCP_CS6 },
6427 { "cs7", IPTOS_DSCP_CS7 },
6428 { "ef", IPTOS_DSCP_EF },
6429 { "inetcontrol", IPTOS_PREC_INTERNETCONTROL },
6430 { "lowdelay", IPTOS_LOWDELAY },
6431 { "netcontrol", IPTOS_PREC_NETCONTROL },
6432 { "reliability", IPTOS_RELIABILITY },
6433 { "throughput", IPTOS_THROUGHPUT },
6434 { "va", IPTOS_DSCP_VA }
6436 const struct keywords *p;
6438 p = bsearch(s, toswords, sizeof(toswords)/sizeof(toswords[0]),
6439 sizeof(toswords[0]), kw_casecmp);
6449 rt_tableid_max(void)
6453 size_t l = sizeof(fibs);
6455 if (sysctlbyname("net.fibs", &fibs, &l, NULL, 0) == -1)
6456 fibs = 16; /* XXX RT_MAXFIBS, at least limit it some. */
6458 * As the OpenBSD code only compares > and not >= we need to adjust
6459 * here given we only accept values of 0..n and want to avoid #ifdefs
6464 return (RT_TABLEID_MAX);