1 /* $OpenBSD: parse.y,v 1.554 2008/10/17 12:59:53 henning Exp $ */
4 * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c) 2001 Markus Friedl. All rights reserved.
7 * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
8 * Copyright (c) 2001 Theo de Raadt. All rights reserved.
9 * Copyright (c) 2002,2003 Henning Brauer. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #define PFIOC_USE_LATEST
37 #include <sys/types.h>
38 #include <sys/socket.h>
41 #include <sys/sysctl.h>
44 #include <netinet/in.h>
45 #include <netinet/in_systm.h>
46 #include <netinet/ip.h>
47 #include <netinet/ip_icmp.h>
48 #include <netinet/icmp6.h>
49 #include <net/pfvar.h>
50 #include <arpa/inet.h>
51 #include <net/altq/altq.h>
52 #include <net/altq/altq_cbq.h>
53 #include <net/altq/altq_codel.h>
54 #include <net/altq/altq_priq.h>
55 #include <net/altq/altq_hfsc.h>
56 #include <net/altq/altq_fairq.h>
74 #include "pfctl_parser.h"
77 static struct pfctl *pf = NULL;
79 static int rulestate = 0;
80 static u_int16_t returnicmpdefault =
81 (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
82 static u_int16_t returnicmp6default =
83 (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
84 static int blockpolicy = PFRULE_DROP;
85 static int failpolicy = PFRULE_DROP;
86 static int require_order = 1;
87 static int default_statelock;
89 static TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
91 TAILQ_ENTRY(file) entry;
97 struct file *pushfile(const char *, int);
99 int check_file_secrecy(int, const char *);
102 int yyerror(const char *, ...);
103 int kw_cmp(const void *, const void *);
109 static TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
111 TAILQ_ENTRY(sym) entry;
117 int symset(const char *, const char *, int);
118 char *symget(const char *);
120 int atoul(char *, u_long *);
133 struct node_proto *next;
134 struct node_proto *tail;
140 struct node_port *next;
141 struct node_port *tail;
147 struct node_uid *next;
148 struct node_uid *tail;
154 struct node_gid *next;
155 struct node_gid *tail;
162 struct node_icmp *next;
163 struct node_icmp *tail;
166 enum { PF_STATE_OPT_MAX, PF_STATE_OPT_NOSYNC, PF_STATE_OPT_SRCTRACK,
167 PF_STATE_OPT_MAX_SRC_STATES, PF_STATE_OPT_MAX_SRC_CONN,
168 PF_STATE_OPT_MAX_SRC_CONN_RATE, PF_STATE_OPT_MAX_SRC_NODES,
169 PF_STATE_OPT_OVERLOAD, PF_STATE_OPT_STATELOCK,
170 PF_STATE_OPT_TIMEOUT, PF_STATE_OPT_SLOPPY, };
172 enum { PF_SRCTRACK_NONE, PF_SRCTRACK, PF_SRCTRACK_GLOBAL, PF_SRCTRACK_RULE };
174 struct node_state_opt {
177 u_int32_t max_states;
178 u_int32_t max_src_states;
179 u_int32_t max_src_conn;
186 char tblname[PF_TABLE_NAME_SIZE];
188 u_int32_t max_src_nodes;
196 struct node_state_opt *next;
197 struct node_state_opt *tail;
201 struct node_host *host;
202 struct node_port *port;
205 static struct node_queue {
206 char queue[PF_QNAME_SIZE];
207 char parent[PF_QNAME_SIZE];
208 char ifname[IFNAMSIZ];
210 struct node_queue *next;
211 struct node_queue *tail;
214 struct node_qassign {
219 static struct filter_opts {
221 #define FOM_FLAGS 0x01
222 #define FOM_ICMP 0x02
224 #define FOM_KEEP 0x08
225 #define FOM_SRCTRACK 0x10
226 #define FOM_SETPRIO 0x0400
227 #define FOM_PRIO 0x2000
228 struct node_uid *uid;
229 struct node_gid *gid;
236 struct node_icmp *icmpspec;
241 struct node_state_opt *options;
245 char *label[PF_RULE_MAX_LABEL_COUNT];
247 struct node_qassign queues;
250 u_int8_t match_tag_not;
253 u_int8_t set_prio[2];
255 struct node_host *addr;
260 static struct antispoof_opts {
261 char *label[PF_RULE_MAX_LABEL_COUNT];
266 static struct scrub_opts {
268 #define SOM_MINTTL 0x01
269 #define SOM_MAXMSS 0x02
270 #define SOM_FRAGCACHE 0x04
271 #define SOM_SETTOS 0x08
280 u_int8_t match_tag_not;
284 static struct queue_opts {
286 #define QOM_BWSPEC 0x01
287 #define QOM_SCHEDULER 0x02
288 #define QOM_PRIORITY 0x04
289 #define QOM_TBRSIZE 0x08
290 #define QOM_QLIMIT 0x10
291 struct node_queue_bw queue_bwspec;
292 struct node_queue_opt scheduler;
294 unsigned int tbrsize;
298 static struct table_opts {
301 struct node_tinithead init_nodes;
304 static struct pool_opts {
306 #define POM_TYPE 0x01
307 #define POM_STICKYADDRESS 0x02
311 struct pf_poolhashkey *key;
312 struct pf_mape_portset mape;
316 static struct codel_opts codel_opts;
317 static struct node_hfsc_opts hfsc_opts;
318 static struct node_fairq_opts fairq_opts;
319 static struct node_state_opt *keep_state_defaults = NULL;
321 int disallow_table(struct node_host *, const char *);
322 int disallow_urpf_failed(struct node_host *, const char *);
323 int disallow_alias(struct node_host *, const char *);
324 int rule_consistent(struct pfctl_rule *, int);
325 int filter_consistent(struct pfctl_rule *, int);
326 int nat_consistent(struct pfctl_rule *);
327 int rdr_consistent(struct pfctl_rule *);
328 int process_tabledef(char *, struct table_opts *);
329 void expand_label_str(char *, size_t, const char *, const char *);
330 void expand_label_if(const char *, char *, size_t, const char *);
331 void expand_label_addr(const char *, char *, size_t, u_int8_t,
333 void expand_label_port(const char *, char *, size_t,
335 void expand_label_proto(const char *, char *, size_t, u_int8_t);
336 void expand_label_nr(const char *, char *, size_t);
337 void expand_label(char *, size_t, const char *, u_int8_t,
338 struct node_host *, struct node_port *, struct node_host *,
339 struct node_port *, u_int8_t);
340 void expand_rule(struct pfctl_rule *, struct node_if *,
341 struct node_host *, struct node_proto *, struct node_os *,
342 struct node_host *, struct node_port *, struct node_host *,
343 struct node_port *, struct node_uid *, struct node_gid *,
344 struct node_icmp *, const char *);
345 int expand_altq(struct pf_altq *, struct node_if *,
346 struct node_queue *, struct node_queue_bw bwspec,
347 struct node_queue_opt *);
348 int expand_queue(struct pf_altq *, struct node_if *,
349 struct node_queue *, struct node_queue_bw,
350 struct node_queue_opt *);
351 int expand_skip_interface(struct node_if *);
353 int check_rulestate(int);
354 int getservice(char *);
355 int rule_label(struct pfctl_rule *, char *s[PF_RULE_MAX_LABEL_COUNT]);
356 int rt_tableid_max(void);
358 void mv_rules(struct pfctl_ruleset *, struct pfctl_ruleset *);
359 void decide_address_family(struct node_host *, sa_family_t *);
360 void remove_invalid_hosts(struct node_host **, sa_family_t *);
361 int invalid_redirect(struct node_host *, sa_family_t);
362 u_int16_t parseicmpspec(char *, sa_family_t);
363 int kw_casecmp(const void *, const void *);
364 int map_tos(char *string, int *);
366 static TAILQ_HEAD(loadanchorshead, loadanchors)
367 loadanchorshead = TAILQ_HEAD_INITIALIZER(loadanchorshead);
370 TAILQ_ENTRY(loadanchors) entries;
393 struct node_if *interface;
394 struct node_proto *proto;
395 struct node_icmp *icmp;
396 struct node_host *host;
398 struct node_port *port;
399 struct node_uid *uid;
400 struct node_gid *gid;
401 struct node_state_opt *state_opt;
404 struct peer src, dst;
405 struct node_os *src_os;
408 struct node_host *host;
412 struct pf_poolhashkey *key;
415 struct node_host *host;
420 struct node_state_opt *options;
431 struct pf_poolhashkey *hashkey;
432 struct node_queue *queue;
433 struct node_queue_opt queue_options;
434 struct node_queue_bw queue_bwspec;
435 struct node_qassign qassign;
436 struct filter_opts filter_opts;
437 struct antispoof_opts antispoof_opts;
438 struct queue_opts queue_opts;
439 struct scrub_opts scrub_opts;
440 struct table_opts table_opts;
441 struct pool_opts pool_opts;
442 struct node_hfsc_opts hfsc_opts;
443 struct node_fairq_opts fairq_opts;
444 struct codel_opts codel_opts;
449 #define PPORT_RANGE 1
451 int parseport(char *, struct range *r, int);
453 #define DYNIF_MULTIADDR(addr) ((addr).type == PF_ADDR_DYNIFTL && \
454 (!((addr).iflags & PFI_AFLAG_NOALIAS) || \
455 !isdigit((addr).v.ifname[strlen((addr).v.ifname)-1])))
459 %token PASS BLOCK SCRUB RETURN IN OS OUT LOG QUICK ON FROM TO FLAGS
460 %token RETURNRST RETURNICMP RETURNICMP6 PROTO INET INET6 ALL ANY ICMPTYPE
461 %token ICMP6TYPE CODE KEEP MODULATE STATE PORT RDR NAT BINAT ARROW NODF
462 %token MINTTL ERROR ALLOWOPTS FASTROUTE FILENAME ROUTETO DUPTO REPLYTO NO LABEL
463 %token NOROUTE URPFFAILED FRAGMENT USER GROUP MAXMSS MAXIMUM TTL TOS DROP TABLE
464 %token REASSEMBLE FRAGDROP FRAGCROP ANCHOR NATANCHOR RDRANCHOR BINATANCHOR
465 %token SET OPTIMIZATION TIMEOUT LIMIT LOGINTERFACE BLOCKPOLICY FAILPOLICY
466 %token RANDOMID REQUIREORDER SYNPROXY FINGERPRINTS NOSYNC DEBUG SKIP HOSTID
467 %token ANTISPOOF FOR INCLUDE KEEPCOUNTERS
468 %token BITMASK RANDOM SOURCEHASH ROUNDROBIN STATICPORT PROBABILITY MAPEPORTSET
469 %token ALTQ CBQ CODEL PRIQ HFSC FAIRQ BANDWIDTH TBRSIZE LINKSHARE REALTIME
470 %token UPPERLIMIT QUEUE PRIORITY QLIMIT HOGS BUCKETS RTABLE TARGET INTERVAL
471 %token LOAD RULESET_OPTIMIZATION PRIO
472 %token STICKYADDRESS MAXSRCSTATES MAXSRCNODES SOURCETRACK GLOBAL RULE
473 %token MAXSRCCONN MAXSRCCONNRATE OVERLOAD FLUSH SLOPPY
474 %token TAGGED TAG IFBOUND FLOATING STATEPOLICY STATEDEFAULTS ROUTE SETTOS
475 %token DIVERTTO DIVERTREPLY
476 %token <v.string> STRING
477 %token <v.number> NUMBER
478 %token <v.i> PORTBINARY
479 %type <v.interface> interface if_list if_item_not if_item
480 %type <v.number> number icmptype icmp6type uid gid
481 %type <v.number> tos not yesno
482 %type <v.probability> probability
483 %type <v.i> no dir af fragcache optimizer
484 %type <v.i> sourcetrack flush unaryop statelock
485 %type <v.b> action nataction natpasslog scrubaction
486 %type <v.b> flags flag blockspec prio
487 %type <v.range> portplain portstar portrange
488 %type <v.hashkey> hashkey
489 %type <v.proto> proto proto_list proto_item
490 %type <v.number> protoval
491 %type <v.icmp> icmpspec
492 %type <v.icmp> icmp_list icmp_item
493 %type <v.icmp> icmp6_list icmp6_item
494 %type <v.number> reticmpspec reticmp6spec
495 %type <v.fromto> fromto
496 %type <v.peer> ipportspec from to
497 %type <v.host> ipspec toipspec xhost host dynaddr host_list
498 %type <v.host> redir_host_list redirspec
499 %type <v.host> route_host route_host_list routespec
500 %type <v.os> os xos os_list
501 %type <v.port> portspec port_list port_item
502 %type <v.uid> uids uid_list uid_item
503 %type <v.gid> gids gid_list gid_item
504 %type <v.route> route
505 %type <v.redirection> redirection redirpool
506 %type <v.string> label stringall tag anchorname
507 %type <v.string> string varstring numberstring
508 %type <v.keep_state> keep
509 %type <v.state_opt> state_opt_spec state_opt_list state_opt_item
510 %type <v.logquick> logquick quick log logopts logopt
511 %type <v.interface> antispoof_ifspc antispoof_iflst antispoof_if
512 %type <v.qassign> qname
513 %type <v.queue> qassign qassign_list qassign_item
514 %type <v.queue_options> scheduler
515 %type <v.number> cbqflags_list cbqflags_item
516 %type <v.number> priqflags_list priqflags_item
517 %type <v.hfsc_opts> hfscopts_list hfscopts_item hfsc_opts
518 %type <v.fairq_opts> fairqopts_list fairqopts_item fairq_opts
519 %type <v.codel_opts> codelopts_list codelopts_item codel_opts
520 %type <v.queue_bwspec> bandwidth
521 %type <v.filter_opts> filter_opts filter_opt filter_opts_l
522 %type <v.filter_opts> filter_sets filter_set filter_sets_l
523 %type <v.antispoof_opts> antispoof_opts antispoof_opt antispoof_opts_l
524 %type <v.queue_opts> queue_opts queue_opt queue_opts_l
525 %type <v.scrub_opts> scrub_opts scrub_opt scrub_opts_l
526 %type <v.table_opts> table_opts table_opt table_opts_l
527 %type <v.pool_opts> pool_opts pool_opt pool_opts_l
528 %type <v.tagged> tagged
529 %type <v.rtableid> rtable
532 ruleset : /* empty */
533 | ruleset include '\n'
535 | ruleset option '\n'
536 | ruleset scrubrule '\n'
537 | ruleset natrule '\n'
538 | ruleset binatrule '\n'
539 | ruleset pfrule '\n'
540 | ruleset anchorrule '\n'
541 | ruleset loadrule '\n'
542 | ruleset altqif '\n'
543 | ruleset queuespec '\n'
544 | ruleset varset '\n'
545 | ruleset antispoof '\n'
546 | ruleset tabledef '\n'
547 | '{' fakeanchor '}' '\n';
548 | ruleset error '\n' { file->errors++; }
551 include : INCLUDE STRING {
554 if ((nfile = pushfile($2, 0)) == NULL) {
555 yyerror("failed to include file %s", $2);
567 * apply to previouslys specified rule: must be careful to note
568 * what that is: pf or nat or binat or rdr
570 fakeanchor : fakeanchor '\n'
571 | fakeanchor anchorrule '\n'
572 | fakeanchor binatrule '\n'
573 | fakeanchor natrule '\n'
574 | fakeanchor pfrule '\n'
575 | fakeanchor error '\n'
579 if (!strcmp($1, "none"))
581 else if (!strcmp($1, "basic"))
582 $$ = PF_OPTIMIZE_BASIC;
583 else if (!strcmp($1, "profile"))
584 $$ = PF_OPTIMIZE_BASIC | PF_OPTIMIZE_PROFILE;
586 yyerror("unknown ruleset-optimization %s", $1);
592 option : SET OPTIMIZATION STRING {
593 if (check_rulestate(PFCTL_STATE_OPTION)) {
597 if (pfctl_set_optimization(pf, $3) != 0) {
598 yyerror("unknown optimization %s", $3);
604 | SET RULESET_OPTIMIZATION optimizer {
605 if (!(pf->opts & PF_OPT_OPTIMIZE)) {
606 pf->opts |= PF_OPT_OPTIMIZE;
610 | SET TIMEOUT timeout_spec
611 | SET TIMEOUT '{' optnl timeout_list '}'
612 | SET LIMIT limit_spec
613 | SET LIMIT '{' optnl limit_list '}'
614 | SET LOGINTERFACE stringall {
615 if (check_rulestate(PFCTL_STATE_OPTION)) {
619 if (pfctl_set_logif(pf, $3) != 0) {
620 yyerror("error setting loginterface %s", $3);
626 | SET HOSTID number {
627 if ($3 == 0 || $3 > UINT_MAX) {
628 yyerror("hostid must be non-zero");
631 if (pfctl_set_hostid(pf, $3) != 0) {
632 yyerror("error setting hostid %08x", $3);
636 | SET BLOCKPOLICY DROP {
637 if (pf->opts & PF_OPT_VERBOSE)
638 printf("set block-policy drop\n");
639 if (check_rulestate(PFCTL_STATE_OPTION))
641 blockpolicy = PFRULE_DROP;
643 | SET BLOCKPOLICY RETURN {
644 if (pf->opts & PF_OPT_VERBOSE)
645 printf("set block-policy return\n");
646 if (check_rulestate(PFCTL_STATE_OPTION))
648 blockpolicy = PFRULE_RETURN;
650 | SET FAILPOLICY DROP {
651 if (pf->opts & PF_OPT_VERBOSE)
652 printf("set fail-policy drop\n");
653 if (check_rulestate(PFCTL_STATE_OPTION))
655 failpolicy = PFRULE_DROP;
657 | SET FAILPOLICY RETURN {
658 if (pf->opts & PF_OPT_VERBOSE)
659 printf("set fail-policy return\n");
660 if (check_rulestate(PFCTL_STATE_OPTION))
662 failpolicy = PFRULE_RETURN;
664 | SET REQUIREORDER yesno {
665 if (pf->opts & PF_OPT_VERBOSE)
666 printf("set require-order %s\n",
667 $3 == 1 ? "yes" : "no");
670 | SET FINGERPRINTS STRING {
671 if (pf->opts & PF_OPT_VERBOSE)
672 printf("set fingerprints \"%s\"\n", $3);
673 if (check_rulestate(PFCTL_STATE_OPTION)) {
677 if (!pf->anchor->name[0]) {
678 if (pfctl_file_fingerprints(pf->dev,
680 yyerror("error loading "
681 "fingerprints %s", $3);
688 | SET STATEPOLICY statelock {
689 if (pf->opts & PF_OPT_VERBOSE)
692 printf("set state-policy floating\n");
695 printf("set state-policy if-bound\n");
698 default_statelock = $3;
701 if (check_rulestate(PFCTL_STATE_OPTION)) {
705 if (pfctl_set_debug(pf, $3) != 0) {
706 yyerror("error setting debuglevel %s", $3);
712 | SET SKIP interface {
713 if (expand_skip_interface($3) != 0) {
714 yyerror("error setting skip interface(s)");
718 | SET STATEDEFAULTS state_opt_list {
719 if (keep_state_defaults != NULL) {
720 yyerror("cannot redefine state-defaults");
723 keep_state_defaults = $3;
726 pf->keep_counters = true;
730 stringall : STRING { $$ = $1; }
732 if (($$ = strdup("all")) == NULL) {
733 err(1, "stringall: strdup");
738 string : STRING string {
739 if (asprintf(&$$, "%s %s", $1, $2) == -1)
740 err(1, "string: asprintf");
747 varstring : numberstring varstring {
748 if (asprintf(&$$, "%s %s", $1, $2) == -1)
749 err(1, "string: asprintf");
756 numberstring : NUMBER {
758 if (asprintf(&s, "%lld", (long long)$1) == -1) {
759 yyerror("string: asprintf");
767 varset : STRING '=' varstring {
769 if (pf->opts & PF_OPT_VERBOSE)
770 printf("%s = \"%s\"\n", $1, $3);
772 if (isspace((unsigned char)*s)) {
773 yyerror("macro name cannot contain "
778 if (symset($1, $3, 0) == -1)
779 err(1, "cannot store variable %s", $1);
785 anchorname : STRING { $$ = $1; }
786 | /* empty */ { $$ = NULL; }
789 pfa_anchorlist : /* empty */
790 | pfa_anchorlist '\n'
791 | pfa_anchorlist pfrule '\n'
792 | pfa_anchorlist anchorrule '\n'
797 char ta[PF_ANCHOR_NAME_SIZE];
798 struct pfctl_ruleset *rs;
800 /* steping into a brace anchor */
805 /* create a holding ruleset in the root */
806 snprintf(ta, PF_ANCHOR_NAME_SIZE, "_%d", pf->bn);
807 rs = pf_find_or_create_ruleset(ta);
809 err(1, "pfa_anchor: pf_find_or_create_ruleset");
810 pf->astack[pf->asd] = rs->anchor;
811 pf->anchor = rs->anchor;
812 } '\n' pfa_anchorlist '}'
814 pf->alast = pf->anchor;
816 pf->anchor = pf->astack[pf->asd];
821 anchorrule : ANCHOR anchorname dir quick interface af proto fromto
822 filter_opts pfa_anchor
825 struct node_proto *proto;
827 if (check_rulestate(PFCTL_STATE_FILTER)) {
833 if ($2 && ($2[0] == '_' || strstr($2, "/_") != NULL)) {
835 yyerror("anchor names beginning with '_' "
836 "are reserved for internal use");
840 memset(&r, 0, sizeof(r));
841 if (pf->astack[pf->asd + 1]) {
842 /* move inline rules into relative location */
843 pfctl_anchor_setup(&r,
844 &pf->astack[pf->asd]->ruleset,
845 $2 ? $2 : pf->alast->name);
847 if (r.anchor == NULL)
848 err(1, "anchorrule: unable to "
851 if (pf->alast != r.anchor) {
852 if (r.anchor->match) {
853 yyerror("inline anchor '%s' "
858 mv_rules(&pf->alast->ruleset,
861 pf_remove_if_empty_ruleset(&pf->alast->ruleset);
862 pf->alast = r.anchor;
865 yyerror("anchors without explicit "
866 "rules must specify a name");
874 r.rtableid = $9.rtableid;
877 if (strlcpy(r.tagname, $9.tag,
878 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
879 yyerror("tag too long, max %u chars",
880 PF_TAG_NAME_SIZE - 1);
884 if (strlcpy(r.match_tagname, $9.match_tag,
885 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
886 yyerror("tag too long, max %u chars",
887 PF_TAG_NAME_SIZE - 1);
890 r.match_tag_not = $9.match_tag_not;
891 if (rule_label(&r, $9.label))
893 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++)
895 r.flags = $9.flags.b1;
896 r.flagset = $9.flags.b2;
897 if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
898 yyerror("flags always false");
901 if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
902 for (proto = $7; proto != NULL &&
903 proto->proto != IPPROTO_TCP;
906 if (proto == NULL && $7 != NULL) {
907 if ($9.flags.b1 || $9.flags.b2)
909 "flags only apply to tcp");
912 "OS fingerprinting only "
920 if ($9.keep.action) {
921 yyerror("cannot specify state handling "
927 if (strlcpy(r.match_tagname, $9.match_tag,
928 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
929 yyerror("tag too long, max %u chars",
930 PF_TAG_NAME_SIZE - 1);
933 r.match_tag_not = $9.match_tag_not;
934 if ($9.marker & FOM_PRIO) {
936 r.prio = PF_PRIO_ZERO;
940 if ($9.marker & FOM_SETPRIO) {
941 r.set_prio[0] = $9.set_prio[0];
942 r.set_prio[1] = $9.set_prio[1];
943 r.scrub_flags |= PFSTATE_SETPRIO;
946 decide_address_family($8.src.host, &r.af);
947 decide_address_family($8.dst.host, &r.af);
949 expand_rule(&r, $5, NULL, $7, $8.src_os,
950 $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
951 $9.uid, $9.gid, $9.icmpspec,
952 pf->astack[pf->asd + 1] ? pf->alast->name : $2);
954 pf->astack[pf->asd + 1] = NULL;
956 | NATANCHOR string interface af proto fromto rtable {
959 if (check_rulestate(PFCTL_STATE_NAT)) {
964 memset(&r, 0, sizeof(r));
969 decide_address_family($6.src.host, &r.af);
970 decide_address_family($6.dst.host, &r.af);
972 expand_rule(&r, $3, NULL, $5, $6.src_os,
973 $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
977 | RDRANCHOR string interface af proto fromto rtable {
980 if (check_rulestate(PFCTL_STATE_NAT)) {
985 memset(&r, 0, sizeof(r));
990 decide_address_family($6.src.host, &r.af);
991 decide_address_family($6.dst.host, &r.af);
993 if ($6.src.port != NULL) {
994 yyerror("source port parameter not supported"
998 if ($6.dst.port != NULL) {
999 if ($6.dst.port->next != NULL) {
1000 yyerror("destination port list "
1001 "expansion not supported in "
1004 } else if ($6.dst.port->op != PF_OP_EQ) {
1005 yyerror("destination port operators"
1006 " not supported in rdr-anchor");
1009 r.dst.port[0] = $6.dst.port->port[0];
1010 r.dst.port[1] = $6.dst.port->port[1];
1011 r.dst.port_op = $6.dst.port->op;
1014 expand_rule(&r, $3, NULL, $5, $6.src_os,
1015 $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
1019 | BINATANCHOR string interface af proto fromto rtable {
1020 struct pfctl_rule r;
1022 if (check_rulestate(PFCTL_STATE_NAT)) {
1027 memset(&r, 0, sizeof(r));
1028 r.action = PF_BINAT;
1032 if ($5->next != NULL) {
1033 yyerror("proto list expansion"
1034 " not supported in binat-anchor");
1037 r.proto = $5->proto;
1041 if ($6.src.host != NULL || $6.src.port != NULL ||
1042 $6.dst.host != NULL || $6.dst.port != NULL) {
1043 yyerror("fromto parameter not supported"
1044 " in binat-anchor");
1048 decide_address_family($6.src.host, &r.af);
1049 decide_address_family($6.dst.host, &r.af);
1051 pfctl_append_rule(pf, &r, $2);
1056 loadrule : LOAD ANCHOR string FROM string {
1057 struct loadanchors *loadanchor;
1059 if (strlen(pf->anchor->name) + 1 +
1060 strlen($3) >= MAXPATHLEN) {
1061 yyerror("anchorname %s too long, max %u\n",
1062 $3, MAXPATHLEN - 1);
1066 loadanchor = calloc(1, sizeof(struct loadanchors));
1067 if (loadanchor == NULL)
1068 err(1, "loadrule: calloc");
1069 if ((loadanchor->anchorname = malloc(MAXPATHLEN)) ==
1071 err(1, "loadrule: malloc");
1072 if (pf->anchor->name[0])
1073 snprintf(loadanchor->anchorname, MAXPATHLEN,
1074 "%s/%s", pf->anchor->name, $3);
1076 strlcpy(loadanchor->anchorname, $3, MAXPATHLEN);
1077 if ((loadanchor->filename = strdup($5)) == NULL)
1078 err(1, "loadrule: strdup");
1080 TAILQ_INSERT_TAIL(&loadanchorshead, loadanchor,
1087 scrubaction : no SCRUB {
1096 scrubrule : scrubaction dir logquick interface af proto fromto scrub_opts
1098 struct pfctl_rule r;
1100 if (check_rulestate(PFCTL_STATE_SCRUB))
1103 memset(&r, 0, sizeof(r));
1111 yyerror("scrub rules do not support 'quick'");
1117 r.rule_flag |= PFRULE_NODF;
1119 r.rule_flag |= PFRULE_RANDOMID;
1120 if ($8.reassemble_tcp) {
1121 if (r.direction != PF_INOUT) {
1122 yyerror("reassemble tcp rules can not "
1123 "specify direction");
1126 r.rule_flag |= PFRULE_REASSEMBLE_TCP;
1129 r.min_ttl = $8.minttl;
1131 r.max_mss = $8.maxmss;
1132 if ($8.marker & SOM_SETTOS) {
1133 r.rule_flag |= PFRULE_SET_TOS;
1134 r.set_tos = $8.settos;
1137 r.rule_flag |= $8.fragcache;
1139 if (strlcpy(r.match_tagname, $8.match_tag,
1140 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1141 yyerror("tag too long, max %u chars",
1142 PF_TAG_NAME_SIZE - 1);
1145 r.match_tag_not = $8.match_tag_not;
1146 r.rtableid = $8.rtableid;
1148 expand_rule(&r, $4, NULL, $6, $7.src_os,
1149 $7.src.host, $7.src.port, $7.dst.host, $7.dst.port,
1150 NULL, NULL, NULL, "");
1155 bzero(&scrub_opts, sizeof scrub_opts);
1156 scrub_opts.rtableid = -1;
1159 { $$ = scrub_opts; }
1161 bzero(&scrub_opts, sizeof scrub_opts);
1162 scrub_opts.rtableid = -1;
1167 scrub_opts_l : scrub_opts_l scrub_opt
1172 if (scrub_opts.nodf) {
1173 yyerror("no-df cannot be respecified");
1176 scrub_opts.nodf = 1;
1179 if (scrub_opts.marker & SOM_MINTTL) {
1180 yyerror("min-ttl cannot be respecified");
1183 if ($2 < 0 || $2 > 255) {
1184 yyerror("illegal min-ttl value %d", $2);
1187 scrub_opts.marker |= SOM_MINTTL;
1188 scrub_opts.minttl = $2;
1191 if (scrub_opts.marker & SOM_MAXMSS) {
1192 yyerror("max-mss cannot be respecified");
1195 if ($2 < 0 || $2 > 65535) {
1196 yyerror("illegal max-mss value %d", $2);
1199 scrub_opts.marker |= SOM_MAXMSS;
1200 scrub_opts.maxmss = $2;
1203 if (scrub_opts.marker & SOM_SETTOS) {
1204 yyerror("set-tos cannot be respecified");
1207 scrub_opts.marker |= SOM_SETTOS;
1208 scrub_opts.settos = $2;
1211 if (scrub_opts.marker & SOM_FRAGCACHE) {
1212 yyerror("fragcache cannot be respecified");
1215 scrub_opts.marker |= SOM_FRAGCACHE;
1216 scrub_opts.fragcache = $1;
1218 | REASSEMBLE STRING {
1219 if (strcasecmp($2, "tcp") != 0) {
1220 yyerror("scrub reassemble supports only tcp, "
1226 if (scrub_opts.reassemble_tcp) {
1227 yyerror("reassemble tcp cannot be respecified");
1230 scrub_opts.reassemble_tcp = 1;
1233 if (scrub_opts.randomid) {
1234 yyerror("random-id cannot be respecified");
1237 scrub_opts.randomid = 1;
1240 if ($2 < 0 || $2 > rt_tableid_max()) {
1241 yyerror("invalid rtable id");
1244 scrub_opts.rtableid = $2;
1246 | not TAGGED string {
1247 scrub_opts.match_tag = $3;
1248 scrub_opts.match_tag_not = $1;
1252 fragcache : FRAGMENT REASSEMBLE { $$ = 0; /* default */ }
1253 | FRAGMENT FRAGCROP { $$ = 0; }
1254 | FRAGMENT FRAGDROP { $$ = 0; }
1257 antispoof : ANTISPOOF logquick antispoof_ifspc af antispoof_opts {
1258 struct pfctl_rule r;
1259 struct node_host *h = NULL, *hh;
1260 struct node_if *i, *j;
1262 if (check_rulestate(PFCTL_STATE_FILTER))
1265 for (i = $3; i; i = i->next) {
1266 bzero(&r, sizeof(r));
1269 r.direction = PF_IN;
1274 if (rule_label(&r, $5.label))
1276 r.rtableid = $5.rtableid;
1277 j = calloc(1, sizeof(struct node_if));
1279 err(1, "antispoof: calloc");
1280 if (strlcpy(j->ifname, i->ifname,
1281 sizeof(j->ifname)) >= sizeof(j->ifname)) {
1283 yyerror("interface name too long");
1288 h = calloc(1, sizeof(*h));
1290 err(1, "address: calloc");
1291 h->addr.type = PF_ADDR_DYNIFTL;
1293 if (strlcpy(h->addr.v.ifname, i->ifname,
1294 sizeof(h->addr.v.ifname)) >=
1295 sizeof(h->addr.v.ifname)) {
1298 "interface name too long");
1301 hh = malloc(sizeof(*hh));
1303 err(1, "address: malloc");
1304 bcopy(h, hh, sizeof(*hh));
1305 h->addr.iflags = PFI_AFLAG_NETWORK;
1307 h = ifa_lookup(j->ifname,
1313 expand_rule(&r, j, NULL, NULL, NULL, h,
1314 NULL, NULL, NULL, NULL, NULL,
1317 if ((i->ifa_flags & IFF_LOOPBACK) == 0) {
1318 bzero(&r, sizeof(r));
1321 r.direction = PF_IN;
1326 if (rule_label(&r, $5.label))
1328 r.rtableid = $5.rtableid;
1332 h = ifa_lookup(i->ifname, 0);
1334 expand_rule(&r, NULL, NULL,
1335 NULL, NULL, h, NULL, NULL,
1336 NULL, NULL, NULL, NULL, "");
1340 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++)
1345 antispoof_ifspc : FOR antispoof_if { $$ = $2; }
1346 | FOR '{' optnl antispoof_iflst '}' { $$ = $4; }
1349 antispoof_iflst : antispoof_if optnl { $$ = $1; }
1350 | antispoof_iflst comma antispoof_if optnl {
1351 $1->tail->next = $3;
1357 antispoof_if : if_item { $$ = $1; }
1365 bzero(&antispoof_opts, sizeof antispoof_opts);
1366 antispoof_opts.rtableid = -1;
1369 { $$ = antispoof_opts; }
1371 bzero(&antispoof_opts, sizeof antispoof_opts);
1372 antispoof_opts.rtableid = -1;
1373 $$ = antispoof_opts;
1377 antispoof_opts_l : antispoof_opts_l antispoof_opt
1381 antispoof_opt : label {
1382 if (antispoof_opts.labelcount >= PF_RULE_MAX_LABEL_COUNT) {
1383 yyerror("label can only be used %d times", PF_RULE_MAX_LABEL_COUNT);
1386 antispoof_opts.label[antispoof_opts.labelcount++] = $1;
1389 if ($2 < 0 || $2 > rt_tableid_max()) {
1390 yyerror("invalid rtable id");
1393 antispoof_opts.rtableid = $2;
1397 not : '!' { $$ = 1; }
1398 | /* empty */ { $$ = 0; }
1401 tabledef : TABLE '<' STRING '>' table_opts {
1402 struct node_host *h, *nh;
1403 struct node_tinit *ti, *nti;
1405 if (strlen($3) >= PF_TABLE_NAME_SIZE) {
1406 yyerror("table name too long, max %d chars",
1407 PF_TABLE_NAME_SIZE - 1);
1411 if (pf->loadopt & PFCTL_FLAG_TABLE)
1412 if (process_tabledef($3, &$5)) {
1417 for (ti = SIMPLEQ_FIRST(&$5.init_nodes);
1418 ti != SIMPLEQ_END(&$5.init_nodes); ti = nti) {
1421 for (h = ti->host; h != NULL; h = nh) {
1425 nti = SIMPLEQ_NEXT(ti, entries);
1432 bzero(&table_opts, sizeof table_opts);
1433 SIMPLEQ_INIT(&table_opts.init_nodes);
1436 { $$ = table_opts; }
1439 bzero(&table_opts, sizeof table_opts);
1440 SIMPLEQ_INIT(&table_opts.init_nodes);
1445 table_opts_l : table_opts_l table_opt
1449 table_opt : STRING {
1450 if (!strcmp($1, "const"))
1451 table_opts.flags |= PFR_TFLAG_CONST;
1452 else if (!strcmp($1, "persist"))
1453 table_opts.flags |= PFR_TFLAG_PERSIST;
1454 else if (!strcmp($1, "counters"))
1455 table_opts.flags |= PFR_TFLAG_COUNTERS;
1457 yyerror("invalid table option '%s'", $1);
1463 | '{' optnl '}' { table_opts.init_addr = 1; }
1464 | '{' optnl host_list '}' {
1465 struct node_host *n;
1466 struct node_tinit *ti;
1468 for (n = $3; n != NULL; n = n->next) {
1469 switch (n->addr.type) {
1470 case PF_ADDR_ADDRMASK:
1473 yyerror("address ranges are not "
1474 "permitted inside tables");
1476 case PF_ADDR_DYNIFTL:
1477 yyerror("dynamic addresses are not "
1478 "permitted inside tables");
1481 yyerror("tables cannot contain tables");
1483 case PF_ADDR_NOROUTE:
1484 yyerror("\"no-route\" is not permitted "
1487 case PF_ADDR_URPFFAILED:
1488 yyerror("\"urpf-failed\" is not "
1489 "permitted inside tables");
1492 yyerror("unknown address type %d",
1497 if (!(ti = calloc(1, sizeof(*ti))))
1498 err(1, "table_opt: calloc");
1500 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1502 table_opts.init_addr = 1;
1505 struct node_tinit *ti;
1507 if (!(ti = calloc(1, sizeof(*ti))))
1508 err(1, "table_opt: calloc");
1510 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1512 table_opts.init_addr = 1;
1516 altqif : ALTQ interface queue_opts QUEUE qassign {
1519 if (check_rulestate(PFCTL_STATE_QUEUE))
1522 memset(&a, 0, sizeof(a));
1523 if ($3.scheduler.qtype == ALTQT_NONE) {
1524 yyerror("no scheduler specified!");
1527 a.scheduler = $3.scheduler.qtype;
1528 a.qlimit = $3.qlimit;
1529 a.tbrsize = $3.tbrsize;
1530 if ($5 == NULL && $3.scheduler.qtype != ALTQT_CODEL) {
1531 yyerror("no child queues specified");
1534 if (expand_altq(&a, $2, $5, $3.queue_bwspec,
1540 queuespec : QUEUE STRING interface queue_opts qassign {
1543 if (check_rulestate(PFCTL_STATE_QUEUE)) {
1548 memset(&a, 0, sizeof(a));
1550 if (strlcpy(a.qname, $2, sizeof(a.qname)) >=
1552 yyerror("queue name too long (max "
1553 "%d chars)", PF_QNAME_SIZE-1);
1559 yyerror("cannot specify tbrsize for queue");
1562 if ($4.priority > 255) {
1563 yyerror("priority out of range: max 255");
1566 a.priority = $4.priority;
1567 a.qlimit = $4.qlimit;
1568 a.scheduler = $4.scheduler.qtype;
1569 if (expand_queue(&a, $3, $5, $4.queue_bwspec,
1571 yyerror("errors in queue definition");
1578 bzero(&queue_opts, sizeof queue_opts);
1579 queue_opts.priority = DEFAULT_PRIORITY;
1580 queue_opts.qlimit = DEFAULT_QLIMIT;
1581 queue_opts.scheduler.qtype = ALTQT_NONE;
1582 queue_opts.queue_bwspec.bw_percent = 100;
1585 { $$ = queue_opts; }
1587 bzero(&queue_opts, sizeof queue_opts);
1588 queue_opts.priority = DEFAULT_PRIORITY;
1589 queue_opts.qlimit = DEFAULT_QLIMIT;
1590 queue_opts.scheduler.qtype = ALTQT_NONE;
1591 queue_opts.queue_bwspec.bw_percent = 100;
1596 queue_opts_l : queue_opts_l queue_opt
1600 queue_opt : BANDWIDTH bandwidth {
1601 if (queue_opts.marker & QOM_BWSPEC) {
1602 yyerror("bandwidth cannot be respecified");
1605 queue_opts.marker |= QOM_BWSPEC;
1606 queue_opts.queue_bwspec = $2;
1609 if (queue_opts.marker & QOM_PRIORITY) {
1610 yyerror("priority cannot be respecified");
1613 if ($2 < 0 || $2 > 255) {
1614 yyerror("priority out of range: max 255");
1617 queue_opts.marker |= QOM_PRIORITY;
1618 queue_opts.priority = $2;
1621 if (queue_opts.marker & QOM_QLIMIT) {
1622 yyerror("qlimit cannot be respecified");
1625 if ($2 < 0 || $2 > 65535) {
1626 yyerror("qlimit out of range: max 65535");
1629 queue_opts.marker |= QOM_QLIMIT;
1630 queue_opts.qlimit = $2;
1633 if (queue_opts.marker & QOM_SCHEDULER) {
1634 yyerror("scheduler cannot be respecified");
1637 queue_opts.marker |= QOM_SCHEDULER;
1638 queue_opts.scheduler = $1;
1641 if (queue_opts.marker & QOM_TBRSIZE) {
1642 yyerror("tbrsize cannot be respecified");
1645 if ($2 < 0 || $2 > UINT_MAX) {
1646 yyerror("tbrsize too big: max %u", UINT_MAX);
1649 queue_opts.marker |= QOM_TBRSIZE;
1650 queue_opts.tbrsize = $2;
1654 bandwidth : STRING {
1660 bps = strtod($1, &cp);
1662 if (strlen(cp) > 1) {
1664 if (!strcmp(cu, "Bit") ||
1666 !strcmp(cu, "bit") ||
1671 if (!strcmp(cp, "b"))
1673 else if (!strcmp(cp, "K"))
1675 else if (!strcmp(cp, "M"))
1677 else if (!strcmp(cp, "G"))
1678 bps *= 1000 * 1000 * 1000;
1679 else if (!strcmp(cp, "%")) {
1680 if (bps < 0 || bps > 100) {
1681 yyerror("bandwidth spec "
1686 $$.bw_percent = bps;
1689 yyerror("unknown unit %s", cp);
1695 $$.bw_absolute = (u_int64_t)bps;
1698 if ($1 < 0 || $1 >= LLONG_MAX) {
1699 yyerror("bandwidth number too big");
1703 $$.bw_absolute = $1;
1708 $$.qtype = ALTQT_CBQ;
1709 $$.data.cbq_opts.flags = 0;
1711 | CBQ '(' cbqflags_list ')' {
1712 $$.qtype = ALTQT_CBQ;
1713 $$.data.cbq_opts.flags = $3;
1716 $$.qtype = ALTQT_PRIQ;
1717 $$.data.priq_opts.flags = 0;
1719 | PRIQ '(' priqflags_list ')' {
1720 $$.qtype = ALTQT_PRIQ;
1721 $$.data.priq_opts.flags = $3;
1724 $$.qtype = ALTQT_HFSC;
1725 bzero(&$$.data.hfsc_opts,
1726 sizeof(struct node_hfsc_opts));
1728 | HFSC '(' hfsc_opts ')' {
1729 $$.qtype = ALTQT_HFSC;
1730 $$.data.hfsc_opts = $3;
1733 $$.qtype = ALTQT_FAIRQ;
1734 bzero(&$$.data.fairq_opts,
1735 sizeof(struct node_fairq_opts));
1737 | FAIRQ '(' fairq_opts ')' {
1738 $$.qtype = ALTQT_FAIRQ;
1739 $$.data.fairq_opts = $3;
1742 $$.qtype = ALTQT_CODEL;
1743 bzero(&$$.data.codel_opts,
1744 sizeof(struct codel_opts));
1746 | CODEL '(' codel_opts ')' {
1747 $$.qtype = ALTQT_CODEL;
1748 $$.data.codel_opts = $3;
1752 cbqflags_list : cbqflags_item { $$ |= $1; }
1753 | cbqflags_list comma cbqflags_item { $$ |= $3; }
1756 cbqflags_item : STRING {
1757 if (!strcmp($1, "default"))
1758 $$ = CBQCLF_DEFCLASS;
1759 else if (!strcmp($1, "borrow"))
1761 else if (!strcmp($1, "red"))
1763 else if (!strcmp($1, "ecn"))
1764 $$ = CBQCLF_RED|CBQCLF_ECN;
1765 else if (!strcmp($1, "rio"))
1767 else if (!strcmp($1, "codel"))
1770 yyerror("unknown cbq flag \"%s\"", $1);
1778 priqflags_list : priqflags_item { $$ |= $1; }
1779 | priqflags_list comma priqflags_item { $$ |= $3; }
1782 priqflags_item : STRING {
1783 if (!strcmp($1, "default"))
1784 $$ = PRCF_DEFAULTCLASS;
1785 else if (!strcmp($1, "red"))
1787 else if (!strcmp($1, "ecn"))
1788 $$ = PRCF_RED|PRCF_ECN;
1789 else if (!strcmp($1, "rio"))
1791 else if (!strcmp($1, "codel"))
1794 yyerror("unknown priq flag \"%s\"", $1);
1804 sizeof(struct node_hfsc_opts));
1811 hfscopts_list : hfscopts_item
1812 | hfscopts_list comma hfscopts_item
1815 hfscopts_item : LINKSHARE bandwidth {
1816 if (hfsc_opts.linkshare.used) {
1817 yyerror("linkshare already specified");
1820 hfsc_opts.linkshare.m2 = $2;
1821 hfsc_opts.linkshare.used = 1;
1823 | LINKSHARE '(' bandwidth comma NUMBER comma bandwidth ')'
1825 if ($5 < 0 || $5 > INT_MAX) {
1826 yyerror("timing in curve out of range");
1829 if (hfsc_opts.linkshare.used) {
1830 yyerror("linkshare already specified");
1833 hfsc_opts.linkshare.m1 = $3;
1834 hfsc_opts.linkshare.d = $5;
1835 hfsc_opts.linkshare.m2 = $7;
1836 hfsc_opts.linkshare.used = 1;
1838 | REALTIME bandwidth {
1839 if (hfsc_opts.realtime.used) {
1840 yyerror("realtime already specified");
1843 hfsc_opts.realtime.m2 = $2;
1844 hfsc_opts.realtime.used = 1;
1846 | REALTIME '(' bandwidth comma NUMBER comma bandwidth ')'
1848 if ($5 < 0 || $5 > INT_MAX) {
1849 yyerror("timing in curve out of range");
1852 if (hfsc_opts.realtime.used) {
1853 yyerror("realtime already specified");
1856 hfsc_opts.realtime.m1 = $3;
1857 hfsc_opts.realtime.d = $5;
1858 hfsc_opts.realtime.m2 = $7;
1859 hfsc_opts.realtime.used = 1;
1861 | UPPERLIMIT bandwidth {
1862 if (hfsc_opts.upperlimit.used) {
1863 yyerror("upperlimit already specified");
1866 hfsc_opts.upperlimit.m2 = $2;
1867 hfsc_opts.upperlimit.used = 1;
1869 | UPPERLIMIT '(' bandwidth comma NUMBER comma bandwidth ')'
1871 if ($5 < 0 || $5 > INT_MAX) {
1872 yyerror("timing in curve out of range");
1875 if (hfsc_opts.upperlimit.used) {
1876 yyerror("upperlimit already specified");
1879 hfsc_opts.upperlimit.m1 = $3;
1880 hfsc_opts.upperlimit.d = $5;
1881 hfsc_opts.upperlimit.m2 = $7;
1882 hfsc_opts.upperlimit.used = 1;
1885 if (!strcmp($1, "default"))
1886 hfsc_opts.flags |= HFCF_DEFAULTCLASS;
1887 else if (!strcmp($1, "red"))
1888 hfsc_opts.flags |= HFCF_RED;
1889 else if (!strcmp($1, "ecn"))
1890 hfsc_opts.flags |= HFCF_RED|HFCF_ECN;
1891 else if (!strcmp($1, "rio"))
1892 hfsc_opts.flags |= HFCF_RIO;
1893 else if (!strcmp($1, "codel"))
1894 hfsc_opts.flags |= HFCF_CODEL;
1896 yyerror("unknown hfsc flag \"%s\"", $1);
1906 sizeof(struct node_fairq_opts));
1913 fairqopts_list : fairqopts_item
1914 | fairqopts_list comma fairqopts_item
1917 fairqopts_item : LINKSHARE bandwidth {
1918 if (fairq_opts.linkshare.used) {
1919 yyerror("linkshare already specified");
1922 fairq_opts.linkshare.m2 = $2;
1923 fairq_opts.linkshare.used = 1;
1925 | LINKSHARE '(' bandwidth number bandwidth ')' {
1926 if (fairq_opts.linkshare.used) {
1927 yyerror("linkshare already specified");
1930 fairq_opts.linkshare.m1 = $3;
1931 fairq_opts.linkshare.d = $4;
1932 fairq_opts.linkshare.m2 = $5;
1933 fairq_opts.linkshare.used = 1;
1936 fairq_opts.hogs_bw = $2;
1939 fairq_opts.nbuckets = $2;
1942 if (!strcmp($1, "default"))
1943 fairq_opts.flags |= FARF_DEFAULTCLASS;
1944 else if (!strcmp($1, "red"))
1945 fairq_opts.flags |= FARF_RED;
1946 else if (!strcmp($1, "ecn"))
1947 fairq_opts.flags |= FARF_RED|FARF_ECN;
1948 else if (!strcmp($1, "rio"))
1949 fairq_opts.flags |= FARF_RIO;
1950 else if (!strcmp($1, "codel"))
1951 fairq_opts.flags |= FARF_CODEL;
1953 yyerror("unknown fairq flag \"%s\"", $1);
1963 sizeof(struct codel_opts));
1970 codelopts_list : codelopts_item
1971 | codelopts_list comma codelopts_item
1974 codelopts_item : INTERVAL number {
1975 if (codel_opts.interval) {
1976 yyerror("interval already specified");
1979 codel_opts.interval = $2;
1982 if (codel_opts.target) {
1983 yyerror("target already specified");
1986 codel_opts.target = $2;
1989 if (!strcmp($1, "ecn"))
1992 yyerror("unknown codel option \"%s\"", $1);
2000 qassign : /* empty */ { $$ = NULL; }
2001 | qassign_item { $$ = $1; }
2002 | '{' optnl qassign_list '}' { $$ = $3; }
2005 qassign_list : qassign_item optnl { $$ = $1; }
2006 | qassign_list comma qassign_item optnl {
2007 $1->tail->next = $3;
2013 qassign_item : STRING {
2014 $$ = calloc(1, sizeof(struct node_queue));
2016 err(1, "qassign_item: calloc");
2017 if (strlcpy($$->queue, $1, sizeof($$->queue)) >=
2018 sizeof($$->queue)) {
2019 yyerror("queue name '%s' too long (max "
2020 "%d chars)", $1, sizeof($$->queue)-1);
2031 pfrule : action dir logquick interface route af proto fromto
2034 struct pfctl_rule r;
2035 struct node_state_opt *o;
2036 struct node_proto *proto;
2042 if (check_rulestate(PFCTL_STATE_FILTER))
2045 memset(&r, 0, sizeof(r));
2049 case PFRULE_RETURNRST:
2050 r.rule_flag |= PFRULE_RETURNRST;
2051 r.return_ttl = $1.w;
2053 case PFRULE_RETURNICMP:
2054 r.rule_flag |= PFRULE_RETURNICMP;
2055 r.return_icmp = $1.w;
2056 r.return_icmp6 = $1.w2;
2059 r.rule_flag |= PFRULE_RETURN;
2060 r.return_icmp = $1.w;
2061 r.return_icmp6 = $1.w2;
2069 r.rtableid = $9.rtableid;
2071 if ($9.marker & FOM_PRIO) {
2073 r.prio = PF_PRIO_ZERO;
2077 if ($9.marker & FOM_SETPRIO) {
2078 r.set_prio[0] = $9.set_prio[0];
2079 r.set_prio[1] = $9.set_prio[1];
2080 r.scrub_flags |= PFSTATE_SETPRIO;
2085 if (strlcpy(r.tagname, $9.tag,
2086 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
2087 yyerror("tag too long, max %u chars",
2088 PF_TAG_NAME_SIZE - 1);
2092 if (strlcpy(r.match_tagname, $9.match_tag,
2093 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
2094 yyerror("tag too long, max %u chars",
2095 PF_TAG_NAME_SIZE - 1);
2098 r.match_tag_not = $9.match_tag_not;
2099 if (rule_label(&r, $9.label))
2101 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++)
2103 r.flags = $9.flags.b1;
2104 r.flagset = $9.flags.b2;
2105 if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
2106 yyerror("flags always false");
2109 if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
2110 for (proto = $7; proto != NULL &&
2111 proto->proto != IPPROTO_TCP;
2112 proto = proto->next)
2114 if (proto == NULL && $7 != NULL) {
2115 if ($9.flags.b1 || $9.flags.b2)
2117 "flags only apply to tcp");
2120 "OS fingerprinting only "
2125 if (($9.flags.b1 & parse_flags("S")) == 0 &&
2127 yyerror("OS fingerprinting requires "
2128 "the SYN TCP flag (flags S/SA)");
2135 r.keep_state = $9.keep.action;
2136 o = $9.keep.options;
2138 /* 'keep state' by default on pass rules. */
2139 if (!r.keep_state && !r.action &&
2140 !($9.marker & FOM_KEEP)) {
2141 r.keep_state = PF_STATE_NORMAL;
2142 o = keep_state_defaults;
2147 struct node_state_opt *p = o;
2150 case PF_STATE_OPT_MAX:
2152 yyerror("state option 'max' "
2153 "multiple definitions");
2156 r.max_states = o->data.max_states;
2158 case PF_STATE_OPT_NOSYNC:
2159 if (r.rule_flag & PFRULE_NOSYNC) {
2160 yyerror("state option 'sync' "
2161 "multiple definitions");
2164 r.rule_flag |= PFRULE_NOSYNC;
2166 case PF_STATE_OPT_SRCTRACK:
2168 yyerror("state option "
2170 "multiple definitions");
2173 srctrack = o->data.src_track;
2174 r.rule_flag |= PFRULE_SRCTRACK;
2176 case PF_STATE_OPT_MAX_SRC_STATES:
2177 if (r.max_src_states) {
2178 yyerror("state option "
2180 "multiple definitions");
2183 if (o->data.max_src_states == 0) {
2184 yyerror("'max-src-states' must "
2189 o->data.max_src_states;
2190 r.rule_flag |= PFRULE_SRCTRACK;
2192 case PF_STATE_OPT_OVERLOAD:
2193 if (r.overload_tblname[0]) {
2194 yyerror("multiple 'overload' "
2195 "table definitions");
2198 if (strlcpy(r.overload_tblname,
2199 o->data.overload.tblname,
2200 PF_TABLE_NAME_SIZE) >=
2201 PF_TABLE_NAME_SIZE) {
2202 yyerror("state option: "
2206 r.flush = o->data.overload.flush;
2208 case PF_STATE_OPT_MAX_SRC_CONN:
2209 if (r.max_src_conn) {
2210 yyerror("state option "
2212 "multiple definitions");
2215 if (o->data.max_src_conn == 0) {
2216 yyerror("'max-src-conn' "
2221 o->data.max_src_conn;
2222 r.rule_flag |= PFRULE_SRCTRACK |
2223 PFRULE_RULESRCTRACK;
2225 case PF_STATE_OPT_MAX_SRC_CONN_RATE:
2226 if (r.max_src_conn_rate.limit) {
2227 yyerror("state option "
2228 "'max-src-conn-rate' "
2229 "multiple definitions");
2232 if (!o->data.max_src_conn_rate.limit ||
2233 !o->data.max_src_conn_rate.seconds) {
2234 yyerror("'max-src-conn-rate' "
2235 "values must be > 0");
2238 if (o->data.max_src_conn_rate.limit >
2240 yyerror("'max-src-conn-rate' "
2241 "maximum rate must be < %u",
2245 r.max_src_conn_rate.limit =
2246 o->data.max_src_conn_rate.limit;
2247 r.max_src_conn_rate.seconds =
2248 o->data.max_src_conn_rate.seconds;
2249 r.rule_flag |= PFRULE_SRCTRACK |
2250 PFRULE_RULESRCTRACK;
2252 case PF_STATE_OPT_MAX_SRC_NODES:
2253 if (r.max_src_nodes) {
2254 yyerror("state option "
2256 "multiple definitions");
2259 if (o->data.max_src_nodes == 0) {
2260 yyerror("'max-src-nodes' must "
2265 o->data.max_src_nodes;
2266 r.rule_flag |= PFRULE_SRCTRACK |
2267 PFRULE_RULESRCTRACK;
2269 case PF_STATE_OPT_STATELOCK:
2271 yyerror("state locking option: "
2272 "multiple definitions");
2276 r.rule_flag |= o->data.statelock;
2278 case PF_STATE_OPT_SLOPPY:
2279 if (r.rule_flag & PFRULE_STATESLOPPY) {
2280 yyerror("state sloppy option: "
2281 "multiple definitions");
2284 r.rule_flag |= PFRULE_STATESLOPPY;
2286 case PF_STATE_OPT_TIMEOUT:
2287 if (o->data.timeout.number ==
2288 PFTM_ADAPTIVE_START ||
2289 o->data.timeout.number ==
2292 if (r.timeout[o->data.timeout.number]) {
2293 yyerror("state timeout %s "
2294 "multiple definitions",
2295 pf_timeouts[o->data.
2296 timeout.number].name);
2299 r.timeout[o->data.timeout.number] =
2300 o->data.timeout.seconds;
2307 /* 'flags S/SA' by default on stateful rules */
2308 if (!r.action && !r.flags && !r.flagset &&
2309 !$9.fragment && !($9.marker & FOM_FLAGS) &&
2311 r.flags = parse_flags("S");
2312 r.flagset = parse_flags("SA");
2314 if (!adaptive && r.max_states) {
2315 r.timeout[PFTM_ADAPTIVE_START] =
2316 (r.max_states / 10) * 6;
2317 r.timeout[PFTM_ADAPTIVE_END] =
2318 (r.max_states / 10) * 12;
2320 if (r.rule_flag & PFRULE_SRCTRACK) {
2321 if (srctrack == PF_SRCTRACK_GLOBAL &&
2323 yyerror("'max-src-nodes' is "
2324 "incompatible with "
2325 "'source-track global'");
2328 if (srctrack == PF_SRCTRACK_GLOBAL &&
2330 yyerror("'max-src-conn' is "
2331 "incompatible with "
2332 "'source-track global'");
2335 if (srctrack == PF_SRCTRACK_GLOBAL &&
2336 r.max_src_conn_rate.seconds) {
2337 yyerror("'max-src-conn-rate' is "
2338 "incompatible with "
2339 "'source-track global'");
2342 if (r.timeout[PFTM_SRC_NODE] <
2343 r.max_src_conn_rate.seconds)
2344 r.timeout[PFTM_SRC_NODE] =
2345 r.max_src_conn_rate.seconds;
2346 r.rule_flag |= PFRULE_SRCTRACK;
2347 if (srctrack == PF_SRCTRACK_RULE)
2348 r.rule_flag |= PFRULE_RULESRCTRACK;
2350 if (r.keep_state && !statelock)
2351 r.rule_flag |= default_statelock;
2354 r.rule_flag |= PFRULE_FRAGMENT;
2355 r.allow_opts = $9.allowopts;
2357 decide_address_family($8.src.host, &r.af);
2358 decide_address_family($8.dst.host, &r.af);
2362 yyerror("direction must be explicit "
2363 "with rules that specify routing");
2367 r.rpool.opts = $5.pool_opts;
2369 memcpy(&r.rpool.key, $5.key,
2370 sizeof(struct pf_poolhashkey));
2373 decide_address_family($5.host, &r.af);
2374 remove_invalid_hosts(&$5.host, &r.af);
2375 if ($5.host == NULL) {
2376 yyerror("no routing address with "
2377 "matching address family found.");
2380 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
2381 PF_POOL_NONE && ($5.host->next != NULL ||
2382 $5.host->addr.type == PF_ADDR_TABLE ||
2383 DYNIF_MULTIADDR($5.host->addr)))
2384 r.rpool.opts |= PF_POOL_ROUNDROBIN;
2385 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2386 PF_POOL_ROUNDROBIN &&
2387 disallow_table($5.host, "tables are only "
2388 "supported in round-robin routing pools"))
2390 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2391 PF_POOL_ROUNDROBIN &&
2392 disallow_alias($5.host, "interface (%s) "
2393 "is only supported in round-robin "
2396 if ($5.host->next != NULL) {
2397 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2398 PF_POOL_ROUNDROBIN) {
2399 yyerror("r.rpool.opts must "
2400 "be PF_POOL_ROUNDROBIN");
2405 if ($9.queues.qname != NULL) {
2406 if (strlcpy(r.qname, $9.queues.qname,
2407 sizeof(r.qname)) >= sizeof(r.qname)) {
2408 yyerror("rule qname too long (max "
2409 "%d chars)", sizeof(r.qname)-1);
2412 free($9.queues.qname);
2414 if ($9.queues.pqname != NULL) {
2415 if (strlcpy(r.pqname, $9.queues.pqname,
2416 sizeof(r.pqname)) >= sizeof(r.pqname)) {
2417 yyerror("rule pqname too long (max "
2418 "%d chars)", sizeof(r.pqname)-1);
2421 free($9.queues.pqname);
2424 r.divert.port = $9.divert.port;
2426 if ((r.divert.port = $9.divert.port)) {
2427 if (r.direction == PF_OUT) {
2428 if ($9.divert.addr) {
2429 yyerror("address specified "
2430 "for outgoing divert");
2433 bzero(&r.divert.addr,
2434 sizeof(r.divert.addr));
2436 if (!$9.divert.addr) {
2437 yyerror("no address specified "
2438 "for incoming divert");
2441 if ($9.divert.addr->af != r.af) {
2442 yyerror("address family "
2443 "mismatch for divert");
2447 $9.divert.addr->addr.v.a.addr;
2452 expand_rule(&r, $4, $5.host, $7, $8.src_os,
2453 $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
2454 $9.uid, $9.gid, $9.icmpspec, "");
2459 bzero(&filter_opts, sizeof filter_opts);
2460 filter_opts.rtableid = -1;
2463 { $$ = filter_opts; }
2465 bzero(&filter_opts, sizeof filter_opts);
2466 filter_opts.rtableid = -1;
2471 filter_opts_l : filter_opts_l filter_opt
2475 filter_opt : USER uids {
2476 if (filter_opts.uid)
2477 $2->tail->next = filter_opts.uid;
2478 filter_opts.uid = $2;
2481 if (filter_opts.gid)
2482 $2->tail->next = filter_opts.gid;
2483 filter_opts.gid = $2;
2486 if (filter_opts.marker & FOM_FLAGS) {
2487 yyerror("flags cannot be redefined");
2490 filter_opts.marker |= FOM_FLAGS;
2491 filter_opts.flags.b1 |= $1.b1;
2492 filter_opts.flags.b2 |= $1.b2;
2493 filter_opts.flags.w |= $1.w;
2494 filter_opts.flags.w2 |= $1.w2;
2497 if (filter_opts.marker & FOM_ICMP) {
2498 yyerror("icmp-type cannot be redefined");
2501 filter_opts.marker |= FOM_ICMP;
2502 filter_opts.icmpspec = $1;
2505 if (filter_opts.marker & FOM_PRIO) {
2506 yyerror("prio cannot be redefined");
2509 if ($2 < 0 || $2 > PF_PRIO_MAX) {
2510 yyerror("prio must be 0 - %u", PF_PRIO_MAX);
2513 filter_opts.marker |= FOM_PRIO;
2514 filter_opts.prio = $2;
2517 if (filter_opts.marker & FOM_TOS) {
2518 yyerror("tos cannot be redefined");
2521 filter_opts.marker |= FOM_TOS;
2522 filter_opts.tos = $2;
2525 if (filter_opts.marker & FOM_KEEP) {
2526 yyerror("modulate or keep cannot be redefined");
2529 filter_opts.marker |= FOM_KEEP;
2530 filter_opts.keep.action = $1.action;
2531 filter_opts.keep.options = $1.options;
2534 filter_opts.fragment = 1;
2537 filter_opts.allowopts = 1;
2540 if (filter_opts.labelcount >= PF_RULE_MAX_LABEL_COUNT) {
2541 yyerror("label can only be used %d times", PF_RULE_MAX_LABEL_COUNT);
2544 filter_opts.label[filter_opts.labelcount++] = $1;
2547 if (filter_opts.queues.qname) {
2548 yyerror("queue cannot be redefined");
2551 filter_opts.queues = $1;
2554 filter_opts.tag = $2;
2556 | not TAGGED string {
2557 filter_opts.match_tag = $3;
2558 filter_opts.match_tag_not = $1;
2560 | PROBABILITY probability {
2563 p = floor($2 * UINT_MAX + 0.5);
2564 if (p < 0.0 || p > UINT_MAX) {
2565 yyerror("invalid probability: %lf", p);
2568 filter_opts.prob = (u_int32_t)p;
2569 if (filter_opts.prob == 0)
2570 filter_opts.prob = 1;
2573 if ($2 < 0 || $2 > rt_tableid_max()) {
2574 yyerror("invalid rtable id");
2577 filter_opts.rtableid = $2;
2579 | DIVERTTO portplain {
2581 filter_opts.divert.port = $2.a;
2582 if (!filter_opts.divert.port) {
2583 yyerror("invalid divert port: %u", ntohs($2.a));
2588 | DIVERTTO STRING PORT portplain {
2590 if ((filter_opts.divert.addr = host($2)) == NULL) {
2591 yyerror("could not parse divert address: %s",
2600 filter_opts.divert.port = $4.a;
2601 if (!filter_opts.divert.port) {
2602 yyerror("invalid divert port: %u", ntohs($4.a));
2608 yyerror("divert-reply has no meaning in FreeBSD pf(4)");
2611 filter_opts.divert.port = 1; /* some random value */
2617 filter_sets : SET '(' filter_sets_l ')' { $$ = filter_opts; }
2618 | SET filter_set { $$ = filter_opts; }
2621 filter_sets_l : filter_sets_l comma filter_set
2626 if (filter_opts.marker & FOM_SETPRIO) {
2627 yyerror("prio cannot be redefined");
2630 filter_opts.marker |= FOM_SETPRIO;
2631 filter_opts.set_prio[0] = $1.b1;
2632 filter_opts.set_prio[1] = $1.b2;
2634 prio : PRIO NUMBER {
2635 if ($2 < 0 || $2 > PF_PRIO_MAX) {
2636 yyerror("prio must be 0 - %u", PF_PRIO_MAX);
2641 | PRIO '(' NUMBER comma NUMBER ')' {
2642 if ($3 < 0 || $3 > PF_PRIO_MAX ||
2643 $5 < 0 || $5 > PF_PRIO_MAX) {
2644 yyerror("prio must be 0 - %u", PF_PRIO_MAX);
2652 probability : STRING {
2654 double p = strtod($1, &e);
2661 yyerror("invalid probability: %s", $1);
2677 $$.w = returnicmpdefault;
2678 $$.w2 = returnicmp6default;
2680 | BLOCK blockspec { $$ = $2; $$.b1 = PF_DROP; }
2683 blockspec : /* empty */ {
2684 $$.b2 = blockpolicy;
2685 $$.w = returnicmpdefault;
2686 $$.w2 = returnicmp6default;
2689 $$.b2 = PFRULE_DROP;
2694 $$.b2 = PFRULE_RETURNRST;
2698 | RETURNRST '(' TTL NUMBER ')' {
2699 if ($4 < 0 || $4 > 255) {
2700 yyerror("illegal ttl value %d", $4);
2703 $$.b2 = PFRULE_RETURNRST;
2708 $$.b2 = PFRULE_RETURNICMP;
2709 $$.w = returnicmpdefault;
2710 $$.w2 = returnicmp6default;
2713 $$.b2 = PFRULE_RETURNICMP;
2714 $$.w = returnicmpdefault;
2715 $$.w2 = returnicmp6default;
2717 | RETURNICMP '(' reticmpspec ')' {
2718 $$.b2 = PFRULE_RETURNICMP;
2720 $$.w2 = returnicmpdefault;
2722 | RETURNICMP6 '(' reticmp6spec ')' {
2723 $$.b2 = PFRULE_RETURNICMP;
2724 $$.w = returnicmpdefault;
2727 | RETURNICMP '(' reticmpspec comma reticmp6spec ')' {
2728 $$.b2 = PFRULE_RETURNICMP;
2733 $$.b2 = PFRULE_RETURN;
2734 $$.w = returnicmpdefault;
2735 $$.w2 = returnicmp6default;
2739 reticmpspec : STRING {
2740 if (!($$ = parseicmpspec($1, AF_INET))) {
2749 if ($1 < 0 || $1 > 255) {
2750 yyerror("invalid icmp code %lu", $1);
2753 icmptype = returnicmpdefault >> 8;
2754 $$ = (icmptype << 8 | $1);
2758 reticmp6spec : STRING {
2759 if (!($$ = parseicmpspec($1, AF_INET6))) {
2768 if ($1 < 0 || $1 > 255) {
2769 yyerror("invalid icmp code %lu", $1);
2772 icmptype = returnicmp6default >> 8;
2773 $$ = (icmptype << 8 | $1);
2777 dir : /* empty */ { $$ = PF_INOUT; }
2778 | IN { $$ = PF_IN; }
2779 | OUT { $$ = PF_OUT; }
2782 quick : /* empty */ { $$.quick = 0; }
2783 | QUICK { $$.quick = 1; }
2786 logquick : /* empty */ { $$.log = 0; $$.quick = 0; $$.logif = 0; }
2787 | log { $$ = $1; $$.quick = 0; }
2788 | QUICK { $$.quick = 1; $$.log = 0; $$.logif = 0; }
2789 | log QUICK { $$ = $1; $$.quick = 1; }
2790 | QUICK log { $$ = $2; $$.quick = 1; }
2793 log : LOG { $$.log = PF_LOG; $$.logif = 0; }
2794 | LOG '(' logopts ')' {
2795 $$.log = PF_LOG | $3.log;
2796 $$.logif = $3.logif;
2800 logopts : logopt { $$ = $1; }
2801 | logopts comma logopt {
2802 $$.log = $1.log | $3.log;
2803 $$.logif = $3.logif;
2805 $$.logif = $1.logif;
2809 logopt : ALL { $$.log = PF_LOG_ALL; $$.logif = 0; }
2810 | USER { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
2811 | GROUP { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
2817 if (strncmp($2, "pflog", 5)) {
2818 yyerror("%s: should be a pflog interface", $2);
2822 i = strtonum($2 + 5, 0, 255, &errstr);
2824 yyerror("%s: %s", $2, errstr);
2833 interface : /* empty */ { $$ = NULL; }
2834 | ON if_item_not { $$ = $2; }
2835 | ON '{' optnl if_list '}' { $$ = $4; }
2838 if_list : if_item_not optnl { $$ = $1; }
2839 | if_list comma if_item_not optnl {
2840 $1->tail->next = $3;
2846 if_item_not : not if_item { $$ = $2; $$->not = $1; }
2850 struct node_host *n;
2852 $$ = calloc(1, sizeof(struct node_if));
2854 err(1, "if_item: calloc");
2855 if (strlcpy($$->ifname, $1, sizeof($$->ifname)) >=
2856 sizeof($$->ifname)) {
2859 yyerror("interface name too long");
2863 if ((n = ifa_exists($1)) != NULL)
2864 $$->ifa_flags = n->ifa_flags;
2873 af : /* empty */ { $$ = 0; }
2874 | INET { $$ = AF_INET; }
2875 | INET6 { $$ = AF_INET6; }
2878 proto : /* empty */ { $$ = NULL; }
2879 | PROTO proto_item { $$ = $2; }
2880 | PROTO '{' optnl proto_list '}' { $$ = $4; }
2883 proto_list : proto_item optnl { $$ = $1; }
2884 | proto_list comma proto_item optnl {
2885 $1->tail->next = $3;
2891 proto_item : protoval {
2896 yyerror("proto 0 cannot be used");
2899 $$ = calloc(1, sizeof(struct node_proto));
2901 err(1, "proto_item: calloc");
2911 p = getprotobyname($1);
2913 yyerror("unknown protocol %s", $1);
2921 if ($1 < 0 || $1 > 255) {
2922 yyerror("protocol outside range");
2942 os : /* empty */ { $$ = NULL; }
2943 | OS xos { $$ = $2; }
2944 | OS '{' optnl os_list '}' { $$ = $4; }
2948 $$ = calloc(1, sizeof(struct node_os));
2950 err(1, "os: calloc");
2956 os_list : xos optnl { $$ = $1; }
2957 | os_list comma xos optnl {
2958 $1->tail->next = $3;
2964 from : /* empty */ {
2978 if (disallow_urpf_failed($2.host, "\"urpf-failed\" is "
2979 "not permitted in a destination address"))
2985 ipportspec : ipspec {
2989 | ipspec PORT portspec {
3003 ipspec : ANY { $$ = NULL; }
3004 | xhost { $$ = $1; }
3005 | '{' optnl host_list '}' { $$ = $3; }
3008 toipspec : TO ipspec { $$ = $2; }
3009 | /* empty */ { $$ = NULL; }
3012 host_list : ipspec optnl { $$ = $1; }
3013 | host_list comma ipspec optnl {
3016 else if ($1 == NULL)
3019 $1->tail->next = $3;
3020 $1->tail = $3->tail;
3027 struct node_host *n;
3029 for (n = $2; n != NULL; n = n->next)
3034 $$ = calloc(1, sizeof(struct node_host));
3036 err(1, "xhost: calloc");
3037 $$->addr.type = PF_ADDR_NOROUTE;
3043 $$ = calloc(1, sizeof(struct node_host));
3045 err(1, "xhost: calloc");
3046 $$->addr.type = PF_ADDR_URPFFAILED;
3054 if (($$ = host($1)) == NULL) {
3055 /* error. "any" is handled elsewhere */
3057 yyerror("could not parse host specification");
3063 | STRING '-' STRING {
3064 struct node_host *b, *e;
3066 if ((b = host($1)) == NULL || (e = host($3)) == NULL) {
3069 yyerror("could not parse host specification");
3072 if (b->af != e->af ||
3073 b->addr.type != PF_ADDR_ADDRMASK ||
3074 e->addr.type != PF_ADDR_ADDRMASK ||
3075 unmask(&b->addr.v.a.mask, b->af) !=
3076 (b->af == AF_INET ? 32 : 128) ||
3077 unmask(&e->addr.v.a.mask, e->af) !=
3078 (e->af == AF_INET ? 32 : 128) ||
3079 b->next != NULL || b->not ||
3080 e->next != NULL || e->not) {
3085 yyerror("invalid address range");
3088 memcpy(&b->addr.v.a.mask, &e->addr.v.a.addr,
3089 sizeof(b->addr.v.a.mask));
3090 b->addr.type = PF_ADDR_RANGE;
3096 | STRING '/' NUMBER {
3099 if (asprintf(&buf, "%s/%lld", $1, (long long)$3) == -1)
3100 err(1, "host: asprintf");
3102 if (($$ = host(buf)) == NULL) {
3103 /* error. "any" is handled elsewhere */
3105 yyerror("could not parse host specification");
3110 | NUMBER '/' NUMBER {
3113 /* ie. for 10/8 parsing */
3115 if (asprintf(&buf, "%lld/%lld", (long long)$1, (long long)$3) == -1)
3117 if (asprintf(&buf, "%lld/%lld", $1, $3) == -1)
3119 err(1, "host: asprintf");
3120 if (($$ = host(buf)) == NULL) {
3121 /* error. "any" is handled elsewhere */
3123 yyerror("could not parse host specification");
3129 | dynaddr '/' NUMBER {
3130 struct node_host *n;
3132 if ($3 < 0 || $3 > 128) {
3133 yyerror("bit number too big");
3137 for (n = $1; n != NULL; n = n->next)
3141 if (strlen($2) >= PF_TABLE_NAME_SIZE) {
3142 yyerror("table name '%s' too long", $2);
3146 $$ = calloc(1, sizeof(struct node_host));
3148 err(1, "host: calloc");
3149 $$->addr.type = PF_ADDR_TABLE;
3150 if (strlcpy($$->addr.v.tblname, $2,
3151 sizeof($$->addr.v.tblname)) >=
3152 sizeof($$->addr.v.tblname))
3153 errx(1, "host: strlcpy");
3164 if (atoul($1, &ulval) == -1) {
3165 yyerror("%s is not a number", $1);
3174 dynaddr : '(' STRING ')' {
3179 if (!isalpha(op[0])) {
3180 yyerror("invalid interface name '%s'", op);
3184 while ((p = strrchr($2, ':')) != NULL) {
3185 if (!strcmp(p+1, "network"))
3186 flags |= PFI_AFLAG_NETWORK;
3187 else if (!strcmp(p+1, "broadcast"))
3188 flags |= PFI_AFLAG_BROADCAST;
3189 else if (!strcmp(p+1, "peer"))
3190 flags |= PFI_AFLAG_PEER;
3191 else if (!strcmp(p+1, "0"))
3192 flags |= PFI_AFLAG_NOALIAS;
3194 yyerror("interface %s has bad modifier",
3201 if (flags & (flags - 1) & PFI_AFLAG_MODEMASK) {
3203 yyerror("illegal combination of "
3204 "interface modifiers");
3207 $$ = calloc(1, sizeof(struct node_host));
3209 err(1, "address: calloc");
3211 set_ipmask($$, 128);
3212 $$->addr.type = PF_ADDR_DYNIFTL;
3213 $$->addr.iflags = flags;
3214 if (strlcpy($$->addr.v.ifname, $2,
3215 sizeof($$->addr.v.ifname)) >=
3216 sizeof($$->addr.v.ifname)) {
3219 yyerror("interface name too long");
3228 portspec : port_item { $$ = $1; }
3229 | '{' optnl port_list '}' { $$ = $3; }
3232 port_list : port_item optnl { $$ = $1; }
3233 | port_list comma port_item optnl {
3234 $1->tail->next = $3;
3240 port_item : portrange {
3241 $$ = calloc(1, sizeof(struct node_port));
3243 err(1, "port_item: calloc");
3253 | unaryop portrange {
3255 yyerror("':' cannot be used with an other "
3259 $$ = calloc(1, sizeof(struct node_port));
3261 err(1, "port_item: calloc");
3268 | portrange PORTBINARY portrange {
3270 yyerror("':' cannot be used with an other "
3274 $$ = calloc(1, sizeof(struct node_port));
3276 err(1, "port_item: calloc");
3285 portplain : numberstring {
3286 if (parseport($1, &$$, 0) == -1) {
3294 portrange : numberstring {
3295 if (parseport($1, &$$, PPORT_RANGE) == -1) {
3303 uids : uid_item { $$ = $1; }
3304 | '{' optnl uid_list '}' { $$ = $3; }
3307 uid_list : uid_item optnl { $$ = $1; }
3308 | uid_list comma uid_item optnl {
3309 $1->tail->next = $3;
3316 $$ = calloc(1, sizeof(struct node_uid));
3318 err(1, "uid_item: calloc");
3326 if ($2 == UID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
3327 yyerror("user unknown requires operator = or "
3331 $$ = calloc(1, sizeof(struct node_uid));
3333 err(1, "uid_item: calloc");
3340 | uid PORTBINARY uid {
3341 if ($1 == UID_MAX || $3 == UID_MAX) {
3342 yyerror("user unknown requires operator = or "
3346 $$ = calloc(1, sizeof(struct node_uid));
3348 err(1, "uid_item: calloc");
3358 if (!strcmp($1, "unknown"))
3363 if ((pw = getpwnam($1)) == NULL) {
3364 yyerror("unknown user %s", $1);
3373 if ($1 < 0 || $1 >= UID_MAX) {
3374 yyerror("illegal uid value %lu", $1);
3381 gids : gid_item { $$ = $1; }
3382 | '{' optnl gid_list '}' { $$ = $3; }
3385 gid_list : gid_item optnl { $$ = $1; }
3386 | gid_list comma gid_item optnl {
3387 $1->tail->next = $3;
3394 $$ = calloc(1, sizeof(struct node_gid));
3396 err(1, "gid_item: calloc");
3404 if ($2 == GID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
3405 yyerror("group unknown requires operator = or "
3409 $$ = calloc(1, sizeof(struct node_gid));
3411 err(1, "gid_item: calloc");
3418 | gid PORTBINARY gid {
3419 if ($1 == GID_MAX || $3 == GID_MAX) {
3420 yyerror("group unknown requires operator = or "
3424 $$ = calloc(1, sizeof(struct node_gid));
3426 err(1, "gid_item: calloc");
3436 if (!strcmp($1, "unknown"))
3441 if ((grp = getgrnam($1)) == NULL) {
3442 yyerror("unknown group %s", $1);
3451 if ($1 < 0 || $1 >= GID_MAX) {
3452 yyerror("illegal gid value %lu", $1);
3462 if ((f = parse_flags($1)) < 0) {
3463 yyerror("bad flags %s", $1);
3472 flags : FLAGS flag '/' flag { $$.b1 = $2.b1; $$.b2 = $4.b1; }
3473 | FLAGS '/' flag { $$.b1 = 0; $$.b2 = $3.b1; }
3474 | FLAGS ANY { $$.b1 = 0; $$.b2 = 0; }
3477 icmpspec : ICMPTYPE icmp_item { $$ = $2; }
3478 | ICMPTYPE '{' optnl icmp_list '}' { $$ = $4; }
3479 | ICMP6TYPE icmp6_item { $$ = $2; }
3480 | ICMP6TYPE '{' optnl icmp6_list '}' { $$ = $4; }
3483 icmp_list : icmp_item optnl { $$ = $1; }
3484 | icmp_list comma icmp_item optnl {
3485 $1->tail->next = $3;
3491 icmp6_list : icmp6_item optnl { $$ = $1; }
3492 | icmp6_list comma icmp6_item optnl {
3493 $1->tail->next = $3;
3499 icmp_item : icmptype {
3500 $$ = calloc(1, sizeof(struct node_icmp));
3502 err(1, "icmp_item: calloc");
3505 $$->proto = IPPROTO_ICMP;
3509 | icmptype CODE STRING {
3510 const struct icmpcodeent *p;
3512 if ((p = geticmpcodebyname($1-1, $3, AF_INET)) == NULL) {
3513 yyerror("unknown icmp-code %s", $3);
3519 $$ = calloc(1, sizeof(struct node_icmp));
3521 err(1, "icmp_item: calloc");
3523 $$->code = p->code + 1;
3524 $$->proto = IPPROTO_ICMP;
3528 | icmptype CODE NUMBER {
3529 if ($3 < 0 || $3 > 255) {
3530 yyerror("illegal icmp-code %lu", $3);
3533 $$ = calloc(1, sizeof(struct node_icmp));
3535 err(1, "icmp_item: calloc");
3538 $$->proto = IPPROTO_ICMP;
3544 icmp6_item : icmp6type {
3545 $$ = calloc(1, sizeof(struct node_icmp));
3547 err(1, "icmp_item: calloc");
3550 $$->proto = IPPROTO_ICMPV6;
3554 | icmp6type CODE STRING {
3555 const struct icmpcodeent *p;
3557 if ((p = geticmpcodebyname($1-1, $3, AF_INET6)) == NULL) {
3558 yyerror("unknown icmp6-code %s", $3);
3564 $$ = calloc(1, sizeof(struct node_icmp));
3566 err(1, "icmp_item: calloc");
3568 $$->code = p->code + 1;
3569 $$->proto = IPPROTO_ICMPV6;
3573 | icmp6type CODE NUMBER {
3574 if ($3 < 0 || $3 > 255) {
3575 yyerror("illegal icmp-code %lu", $3);
3578 $$ = calloc(1, sizeof(struct node_icmp));
3580 err(1, "icmp_item: calloc");
3583 $$->proto = IPPROTO_ICMPV6;
3590 const struct icmptypeent *p;
3592 if ((p = geticmptypebyname($1, AF_INET)) == NULL) {
3593 yyerror("unknown icmp-type %s", $1);
3601 if ($1 < 0 || $1 > 255) {
3602 yyerror("illegal icmp-type %lu", $1);
3609 icmp6type : STRING {
3610 const struct icmptypeent *p;
3612 if ((p = geticmptypebyname($1, AF_INET6)) ==
3614 yyerror("unknown icmp6-type %s", $1);
3622 if ($1 < 0 || $1 > 255) {
3623 yyerror("illegal icmp6-type %lu", $1);
3634 if (map_tos($1, &val))
3636 else if ($1[0] == '0' && $1[1] == 'x') {
3638 $$ = strtoul($1, &end, 16);
3639 if (errno || *end != '\0')
3642 $$ = 256; /* flag bad argument */
3643 if ($$ < 0 || $$ > 255) {
3644 yyerror("illegal tos value %s", $1);
3652 if ($$ < 0 || $$ > 255) {
3653 yyerror("illegal tos value %s", $1);
3659 sourcetrack : SOURCETRACK { $$ = PF_SRCTRACK; }
3660 | SOURCETRACK GLOBAL { $$ = PF_SRCTRACK_GLOBAL; }
3661 | SOURCETRACK RULE { $$ = PF_SRCTRACK_RULE; }
3664 statelock : IFBOUND {
3665 $$ = PFRULE_IFBOUND;
3676 | KEEP STATE state_opt_spec {
3677 $$.action = PF_STATE_NORMAL;
3680 | MODULATE STATE state_opt_spec {
3681 $$.action = PF_STATE_MODULATE;
3684 | SYNPROXY STATE state_opt_spec {
3685 $$.action = PF_STATE_SYNPROXY;
3690 flush : /* empty */ { $$ = 0; }
3691 | FLUSH { $$ = PF_FLUSH; }
3693 $$ = PF_FLUSH | PF_FLUSH_GLOBAL;
3697 state_opt_spec : '(' state_opt_list ')' { $$ = $2; }
3698 | /* empty */ { $$ = NULL; }
3701 state_opt_list : state_opt_item { $$ = $1; }
3702 | state_opt_list comma state_opt_item {
3703 $1->tail->next = $3;
3709 state_opt_item : MAXIMUM NUMBER {
3710 if ($2 < 0 || $2 > UINT_MAX) {
3711 yyerror("only positive values permitted");
3714 $$ = calloc(1, sizeof(struct node_state_opt));
3716 err(1, "state_opt_item: calloc");
3717 $$->type = PF_STATE_OPT_MAX;
3718 $$->data.max_states = $2;
3723 $$ = calloc(1, sizeof(struct node_state_opt));
3725 err(1, "state_opt_item: calloc");
3726 $$->type = PF_STATE_OPT_NOSYNC;
3730 | MAXSRCSTATES NUMBER {
3731 if ($2 < 0 || $2 > UINT_MAX) {
3732 yyerror("only positive values permitted");
3735 $$ = calloc(1, sizeof(struct node_state_opt));
3737 err(1, "state_opt_item: calloc");
3738 $$->type = PF_STATE_OPT_MAX_SRC_STATES;
3739 $$->data.max_src_states = $2;
3743 | MAXSRCCONN NUMBER {
3744 if ($2 < 0 || $2 > UINT_MAX) {
3745 yyerror("only positive values permitted");
3748 $$ = calloc(1, sizeof(struct node_state_opt));
3750 err(1, "state_opt_item: calloc");
3751 $$->type = PF_STATE_OPT_MAX_SRC_CONN;
3752 $$->data.max_src_conn = $2;
3756 | MAXSRCCONNRATE NUMBER '/' NUMBER {
3757 if ($2 < 0 || $2 > UINT_MAX ||
3758 $4 < 0 || $4 > UINT_MAX) {
3759 yyerror("only positive values permitted");
3762 $$ = calloc(1, sizeof(struct node_state_opt));
3764 err(1, "state_opt_item: calloc");
3765 $$->type = PF_STATE_OPT_MAX_SRC_CONN_RATE;
3766 $$->data.max_src_conn_rate.limit = $2;
3767 $$->data.max_src_conn_rate.seconds = $4;
3771 | OVERLOAD '<' STRING '>' flush {
3772 if (strlen($3) >= PF_TABLE_NAME_SIZE) {
3773 yyerror("table name '%s' too long", $3);
3777 $$ = calloc(1, sizeof(struct node_state_opt));
3779 err(1, "state_opt_item: calloc");
3780 if (strlcpy($$->data.overload.tblname, $3,
3781 PF_TABLE_NAME_SIZE) >= PF_TABLE_NAME_SIZE)
3782 errx(1, "state_opt_item: strlcpy");
3784 $$->type = PF_STATE_OPT_OVERLOAD;
3785 $$->data.overload.flush = $5;
3789 | MAXSRCNODES NUMBER {
3790 if ($2 < 0 || $2 > UINT_MAX) {
3791 yyerror("only positive values permitted");
3794 $$ = calloc(1, sizeof(struct node_state_opt));
3796 err(1, "state_opt_item: calloc");
3797 $$->type = PF_STATE_OPT_MAX_SRC_NODES;
3798 $$->data.max_src_nodes = $2;
3803 $$ = calloc(1, sizeof(struct node_state_opt));
3805 err(1, "state_opt_item: calloc");
3806 $$->type = PF_STATE_OPT_SRCTRACK;
3807 $$->data.src_track = $1;
3812 $$ = calloc(1, sizeof(struct node_state_opt));
3814 err(1, "state_opt_item: calloc");
3815 $$->type = PF_STATE_OPT_STATELOCK;
3816 $$->data.statelock = $1;
3821 $$ = calloc(1, sizeof(struct node_state_opt));
3823 err(1, "state_opt_item: calloc");
3824 $$->type = PF_STATE_OPT_SLOPPY;
3831 if ($2 < 0 || $2 > UINT_MAX) {
3832 yyerror("only positive values permitted");
3835 for (i = 0; pf_timeouts[i].name &&
3836 strcmp(pf_timeouts[i].name, $1); ++i)
3838 if (!pf_timeouts[i].name) {
3839 yyerror("illegal timeout name %s", $1);
3843 if (strchr(pf_timeouts[i].name, '.') == NULL) {
3844 yyerror("illegal state timeout %s", $1);
3849 $$ = calloc(1, sizeof(struct node_state_opt));
3851 err(1, "state_opt_item: calloc");
3852 $$->type = PF_STATE_OPT_TIMEOUT;
3853 $$->data.timeout.number = pf_timeouts[i].timeout;
3854 $$->data.timeout.seconds = $2;
3860 label : LABEL STRING {
3865 qname : QUEUE STRING {
3869 | QUEUE '(' STRING ')' {
3873 | QUEUE '(' STRING comma STRING ')' {
3879 no : /* empty */ { $$ = 0; }
3883 portstar : numberstring {
3884 if (parseport($1, &$$, PPORT_RANGE|PPORT_STAR) == -1) {
3892 redirspec : host { $$ = $1; }
3893 | '{' optnl redir_host_list '}' { $$ = $3; }
3896 redir_host_list : host optnl { $$ = $1; }
3897 | redir_host_list comma host optnl {
3898 $1->tail->next = $3;
3899 $1->tail = $3->tail;
3904 redirpool : /* empty */ { $$ = NULL; }
3906 $$ = calloc(1, sizeof(struct redirection));
3908 err(1, "redirection: calloc");
3910 $$->rport.a = $$->rport.b = $$->rport.t = 0;
3912 | ARROW redirspec PORT portstar {
3913 $$ = calloc(1, sizeof(struct redirection));
3915 err(1, "redirection: calloc");
3921 hashkey : /* empty */
3923 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3925 err(1, "hashkey: calloc");
3926 $$->key32[0] = arc4random();
3927 $$->key32[1] = arc4random();
3928 $$->key32[2] = arc4random();
3929 $$->key32[3] = arc4random();
3933 if (!strncmp($1, "0x", 2)) {
3934 if (strlen($1) != 34) {
3936 yyerror("hex key must be 128 bits "
3937 "(32 hex digits) long");
3940 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3942 err(1, "hashkey: calloc");
3944 if (sscanf($1, "0x%8x%8x%8x%8x",
3945 &$$->key32[0], &$$->key32[1],
3946 &$$->key32[2], &$$->key32[3]) != 4) {
3949 yyerror("invalid hex key");
3955 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3957 err(1, "hashkey: calloc");
3959 MD5Update(&context, (unsigned char *)$1,
3961 MD5Final((unsigned char *)$$, &context);
3962 HTONL($$->key32[0]);
3963 HTONL($$->key32[1]);
3964 HTONL($$->key32[2]);
3965 HTONL($$->key32[3]);
3971 pool_opts : { bzero(&pool_opts, sizeof pool_opts); }
3975 bzero(&pool_opts, sizeof pool_opts);
3980 pool_opts_l : pool_opts_l pool_opt
3984 pool_opt : BITMASK {
3985 if (pool_opts.type) {
3986 yyerror("pool type cannot be redefined");
3989 pool_opts.type = PF_POOL_BITMASK;
3992 if (pool_opts.type) {
3993 yyerror("pool type cannot be redefined");
3996 pool_opts.type = PF_POOL_RANDOM;
3998 | SOURCEHASH hashkey {
3999 if (pool_opts.type) {
4000 yyerror("pool type cannot be redefined");
4003 pool_opts.type = PF_POOL_SRCHASH;
4007 if (pool_opts.type) {
4008 yyerror("pool type cannot be redefined");
4011 pool_opts.type = PF_POOL_ROUNDROBIN;
4014 if (pool_opts.staticport) {
4015 yyerror("static-port cannot be redefined");
4018 pool_opts.staticport = 1;
4021 if (filter_opts.marker & POM_STICKYADDRESS) {
4022 yyerror("sticky-address cannot be redefined");
4025 pool_opts.marker |= POM_STICKYADDRESS;
4026 pool_opts.opts |= PF_POOL_STICKYADDR;
4028 | MAPEPORTSET number '/' number '/' number {
4029 if (pool_opts.mape.offset) {
4030 yyerror("map-e-portset cannot be redefined");
4033 if (pool_opts.type) {
4034 yyerror("map-e-portset cannot be used with "
4038 if ($2 <= 0 || $2 >= 16) {
4039 yyerror("MAP-E PSID offset must be 1-15");
4042 if ($4 < 0 || $4 >= 16 || $2 + $4 > 16) {
4043 yyerror("Invalid MAP-E PSID length");
4045 } else if ($4 == 0) {
4046 yyerror("PSID Length = 0: this means"
4047 " you do not need MAP-E");
4050 if ($6 < 0 || $6 > 65535) {
4051 yyerror("Invalid MAP-E PSID");
4054 pool_opts.mape.offset = $2;
4055 pool_opts.mape.psidlen = $4;
4056 pool_opts.mape.psid = $6;
4060 redirection : /* empty */ { $$ = NULL; }
4062 $$ = calloc(1, sizeof(struct redirection));
4064 err(1, "redirection: calloc");
4066 $$->rport.a = $$->rport.b = $$->rport.t = 0;
4068 | ARROW host PORT portstar {
4069 $$ = calloc(1, sizeof(struct redirection));
4071 err(1, "redirection: calloc");
4077 natpasslog : /* empty */ { $$.b1 = $$.b2 = 0; $$.w2 = 0; }
4078 | PASS { $$.b1 = 1; $$.b2 = 0; $$.w2 = 0; }
4079 | PASS log { $$.b1 = 1; $$.b2 = $2.log; $$.w2 = $2.logif; }
4080 | log { $$.b1 = 0; $$.b2 = $1.log; $$.w2 = $1.logif; }
4083 nataction : no NAT natpasslog {
4085 yyerror("\"pass\" not valid with \"no\"");
4096 | no RDR natpasslog {
4098 yyerror("\"pass\" not valid with \"no\"");
4111 natrule : nataction interface af proto fromto tag tagged rtable
4114 struct pfctl_rule r;
4116 if (check_rulestate(PFCTL_STATE_NAT))
4119 memset(&r, 0, sizeof(r));
4128 if ($5.src.host && $5.src.host->af &&
4129 !$5.src.host->ifindex)
4130 r.af = $5.src.host->af;
4131 else if ($5.dst.host && $5.dst.host->af &&
4132 !$5.dst.host->ifindex)
4133 r.af = $5.dst.host->af;
4137 if (strlcpy(r.tagname, $6, PF_TAG_NAME_SIZE) >=
4139 yyerror("tag too long, max %u chars",
4140 PF_TAG_NAME_SIZE - 1);
4145 if (strlcpy(r.match_tagname, $7.name,
4146 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4147 yyerror("tag too long, max %u chars",
4148 PF_TAG_NAME_SIZE - 1);
4151 r.match_tag_not = $7.neg;
4154 if (r.action == PF_NONAT || r.action == PF_NORDR) {
4156 yyerror("translation rule with 'no' "
4157 "does not need '->'");
4161 if ($9 == NULL || $9->host == NULL) {
4162 yyerror("translation rule requires '-> "
4166 if (!r.af && ! $9->host->ifindex)
4167 r.af = $9->host->af;
4169 remove_invalid_hosts(&$9->host, &r.af);
4170 if (invalid_redirect($9->host, r.af))
4172 if (check_netmask($9->host, r.af))
4175 r.rpool.proxy_port[0] = ntohs($9->rport.a);
4179 if (!$9->rport.b && $9->rport.t &&
4180 $5.dst.port != NULL) {
4181 r.rpool.proxy_port[1] =
4182 ntohs($9->rport.a) +
4184 $5.dst.port->port[1]) -
4186 $5.dst.port->port[0]));
4188 r.rpool.proxy_port[1] =
4192 r.rpool.proxy_port[1] =
4194 if (!r.rpool.proxy_port[0] &&
4195 !r.rpool.proxy_port[1]) {
4196 r.rpool.proxy_port[0] =
4197 PF_NAT_PROXY_PORT_LOW;
4198 r.rpool.proxy_port[1] =
4199 PF_NAT_PROXY_PORT_HIGH;
4200 } else if (!r.rpool.proxy_port[1])
4201 r.rpool.proxy_port[1] =
4202 r.rpool.proxy_port[0];
4208 r.rpool.opts = $10.type;
4209 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
4210 PF_POOL_NONE && ($9->host->next != NULL ||
4211 $9->host->addr.type == PF_ADDR_TABLE ||
4212 DYNIF_MULTIADDR($9->host->addr)))
4213 r.rpool.opts = PF_POOL_ROUNDROBIN;
4214 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4215 PF_POOL_ROUNDROBIN &&
4216 disallow_table($9->host, "tables are only "
4217 "supported in round-robin redirection "
4220 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4221 PF_POOL_ROUNDROBIN &&
4222 disallow_alias($9->host, "interface (%s) "
4223 "is only supported in round-robin "
4224 "redirection pools"))
4226 if ($9->host->next != NULL) {
4227 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4228 PF_POOL_ROUNDROBIN) {
4229 yyerror("only round-robin "
4230 "valid for multiple "
4231 "redirection addresses");
4237 if ($10.key != NULL)
4238 memcpy(&r.rpool.key, $10.key,
4239 sizeof(struct pf_poolhashkey));
4242 r.rpool.opts |= $10.opts;
4244 if ($10.staticport) {
4245 if (r.action != PF_NAT) {
4246 yyerror("the 'static-port' option is "
4247 "only valid with nat rules");
4250 if (r.rpool.proxy_port[0] !=
4251 PF_NAT_PROXY_PORT_LOW &&
4252 r.rpool.proxy_port[1] !=
4253 PF_NAT_PROXY_PORT_HIGH) {
4254 yyerror("the 'static-port' option can't"
4255 " be used when specifying a port"
4259 r.rpool.proxy_port[0] = 0;
4260 r.rpool.proxy_port[1] = 0;
4263 if ($10.mape.offset) {
4264 if (r.action != PF_NAT) {
4265 yyerror("the 'map-e-portset' option is"
4266 " only valid with nat rules");
4269 if ($10.staticport) {
4270 yyerror("the 'map-e-portset' option"
4271 " can't be used 'static-port'");
4274 if (r.rpool.proxy_port[0] !=
4275 PF_NAT_PROXY_PORT_LOW &&
4276 r.rpool.proxy_port[1] !=
4277 PF_NAT_PROXY_PORT_HIGH) {
4278 yyerror("the 'map-e-portset' option"
4279 " can't be used when specifying"
4283 r.rpool.mape = $10.mape;
4286 expand_rule(&r, $2, $9 == NULL ? NULL : $9->host, $4,
4287 $5.src_os, $5.src.host, $5.src.port, $5.dst.host,
4288 $5.dst.port, 0, 0, 0, "");
4293 binatrule : no BINAT natpasslog interface af proto FROM ipspec toipspec tag
4294 tagged rtable redirection
4296 struct pfctl_rule binat;
4297 struct pf_pooladdr *pa;
4299 if (check_rulestate(PFCTL_STATE_NAT))
4301 if (disallow_urpf_failed($9, "\"urpf-failed\" is not "
4302 "permitted as a binat destination"))
4305 memset(&binat, 0, sizeof(binat));
4308 yyerror("\"pass\" not valid with \"no\"");
4312 binat.action = PF_NOBINAT;
4314 binat.action = PF_BINAT;
4315 binat.natpass = $3.b1;
4317 binat.logif = $3.w2;
4319 if (!binat.af && $8 != NULL && $8->af)
4321 if (!binat.af && $9 != NULL && $9->af)
4324 if (!binat.af && $13 != NULL && $13->host)
4325 binat.af = $13->host->af;
4327 yyerror("address family (inet/inet6) "
4333 memcpy(binat.ifname, $4->ifname,
4334 sizeof(binat.ifname));
4335 binat.ifnot = $4->not;
4340 if (strlcpy(binat.tagname, $10,
4341 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4342 yyerror("tag too long, max %u chars",
4343 PF_TAG_NAME_SIZE - 1);
4347 if (strlcpy(binat.match_tagname, $11.name,
4348 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4349 yyerror("tag too long, max %u chars",
4350 PF_TAG_NAME_SIZE - 1);
4353 binat.match_tag_not = $11.neg;
4354 binat.rtableid = $12;
4357 binat.proto = $6->proto;
4361 if ($8 != NULL && disallow_table($8, "invalid use of "
4362 "table <%s> as the source address of a binat rule"))
4364 if ($8 != NULL && disallow_alias($8, "invalid use of "
4365 "interface (%s) as the source address of a binat "
4368 if ($13 != NULL && $13->host != NULL && disallow_table(
4369 $13->host, "invalid use of table <%s> as the "
4370 "redirect address of a binat rule"))
4372 if ($13 != NULL && $13->host != NULL && disallow_alias(
4373 $13->host, "invalid use of interface (%s) as the "
4374 "redirect address of a binat rule"))
4379 yyerror("multiple binat ip addresses");
4382 if ($8->addr.type == PF_ADDR_DYNIFTL)
4384 if ($8->af != binat.af) {
4385 yyerror("binat ip versions must match");
4388 if (check_netmask($8, binat.af))
4390 memcpy(&binat.src.addr, &$8->addr,
4391 sizeof(binat.src.addr));
4396 yyerror("multiple binat ip addresses");
4399 if ($9->af != binat.af && $9->af) {
4400 yyerror("binat ip versions must match");
4403 if (check_netmask($9, binat.af))
4405 memcpy(&binat.dst.addr, &$9->addr,
4406 sizeof(binat.dst.addr));
4407 binat.dst.neg = $9->not;
4411 if (binat.action == PF_NOBINAT) {
4413 yyerror("'no binat' rule does not need"
4418 if ($13 == NULL || $13->host == NULL) {
4419 yyerror("'binat' rule requires"
4424 remove_invalid_hosts(&$13->host, &binat.af);
4425 if (invalid_redirect($13->host, binat.af))
4427 if ($13->host->next != NULL) {
4428 yyerror("binat rule must redirect to "
4429 "a single address");
4432 if (check_netmask($13->host, binat.af))
4435 if (!PF_AZERO(&binat.src.addr.v.a.mask,
4437 !PF_AEQ(&binat.src.addr.v.a.mask,
4438 &$13->host->addr.v.a.mask, binat.af)) {
4439 yyerror("'binat' source mask and "
4440 "redirect mask must be the same");
4444 TAILQ_INIT(&binat.rpool.list);
4445 pa = calloc(1, sizeof(struct pf_pooladdr));
4447 err(1, "binat: calloc");
4448 pa->addr = $13->host->addr;
4450 TAILQ_INSERT_TAIL(&binat.rpool.list,
4456 pfctl_append_rule(pf, &binat, "");
4460 tag : /* empty */ { $$ = NULL; }
4461 | TAG STRING { $$ = $2; }
4464 tagged : /* empty */ { $$.neg = 0; $$.name = NULL; }
4465 | not TAGGED string { $$.neg = $1; $$.name = $3; }
4468 rtable : /* empty */ { $$ = -1; }
4470 if ($2 < 0 || $2 > rt_tableid_max()) {
4471 yyerror("invalid rtable id");
4478 route_host : STRING {
4479 $$ = calloc(1, sizeof(struct node_host));
4481 err(1, "route_host: calloc");
4482 $$->ifname = strdup($1);
4483 set_ipmask($$, 128);
4487 | '(' STRING host ')' {
4488 struct node_host *n;
4491 for (n = $3; n != NULL; n = n->next)
4492 n->ifname = strdup($2);
4496 route_host_list : route_host optnl { $$ = $1; }
4497 | route_host_list comma route_host optnl {
4500 if ($1->af != $3->af) {
4501 yyerror("all pool addresses must be in the "
4502 "same address family");
4505 $1->tail->next = $3;
4506 $1->tail = $3->tail;
4511 routespec : route_host { $$ = $1; }
4512 | '{' optnl route_host_list '}' { $$ = $3; }
4515 route : /* empty */ {
4521 /* backwards-compat */
4526 | ROUTETO routespec pool_opts {
4529 $$.pool_opts = $3.type | $3.opts;
4533 | REPLYTO routespec pool_opts {
4536 $$.pool_opts = $3.type | $3.opts;
4540 | DUPTO routespec pool_opts {
4543 $$.pool_opts = $3.type | $3.opts;
4549 timeout_spec : STRING NUMBER
4551 if (check_rulestate(PFCTL_STATE_OPTION)) {
4555 if ($2 < 0 || $2 > UINT_MAX) {
4556 yyerror("only positive values permitted");
4559 if (pfctl_set_timeout(pf, $1, $2, 0) != 0) {
4560 yyerror("unknown timeout %s", $1);
4567 if (check_rulestate(PFCTL_STATE_OPTION))
4569 if ($2 < 0 || $2 > UINT_MAX) {
4570 yyerror("only positive values permitted");
4573 if (pfctl_set_timeout(pf, "interval", $2, 0) != 0)
4578 timeout_list : timeout_list comma timeout_spec optnl
4579 | timeout_spec optnl
4582 limit_spec : STRING NUMBER
4584 if (check_rulestate(PFCTL_STATE_OPTION)) {
4588 if ($2 < 0 || $2 > UINT_MAX) {
4589 yyerror("only positive values permitted");
4592 if (pfctl_set_limit(pf, $1, $2) != 0) {
4593 yyerror("unable to set limit %s %u", $1, $2);
4601 limit_list : limit_list comma limit_spec optnl
4609 yesno : NO { $$ = 0; }
4611 if (!strcmp($1, "yes"))
4614 yyerror("invalid value '%s', expected 'yes' "
4623 unaryop : '=' { $$ = PF_OP_EQ; }
4624 | '!' '=' { $$ = PF_OP_NE; }
4625 | '<' '=' { $$ = PF_OP_LE; }
4626 | '<' { $$ = PF_OP_LT; }
4627 | '>' '=' { $$ = PF_OP_GE; }
4628 | '>' { $$ = PF_OP_GT; }
4634 yyerror(const char *fmt, ...)
4640 fprintf(stderr, "%s:%d: ", file->name, yylval.lineno);
4641 vfprintf(stderr, fmt, ap);
4642 fprintf(stderr, "\n");
4648 disallow_table(struct node_host *h, const char *fmt)
4650 for (; h != NULL; h = h->next)
4651 if (h->addr.type == PF_ADDR_TABLE) {
4652 yyerror(fmt, h->addr.v.tblname);
4659 disallow_urpf_failed(struct node_host *h, const char *fmt)
4661 for (; h != NULL; h = h->next)
4662 if (h->addr.type == PF_ADDR_URPFFAILED) {
4670 disallow_alias(struct node_host *h, const char *fmt)
4672 for (; h != NULL; h = h->next)
4673 if (DYNIF_MULTIADDR(h->addr)) {
4674 yyerror(fmt, h->addr.v.tblname);
4681 rule_consistent(struct pfctl_rule *r, int anchor_call)
4685 switch (r->action) {
4690 problems = filter_consistent(r, anchor_call);
4694 problems = nat_consistent(r);
4698 problems = rdr_consistent(r);
4709 filter_consistent(struct pfctl_rule *r, int anchor_call)
4713 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP &&
4714 (r->src.port_op || r->dst.port_op)) {
4715 yyerror("port only applies to tcp/udp");
4718 if (r->proto != IPPROTO_ICMP && r->proto != IPPROTO_ICMPV6 &&
4719 (r->type || r->code)) {
4720 yyerror("icmp-type/code only applies to icmp");
4723 if (!r->af && (r->type || r->code)) {
4724 yyerror("must indicate address family with icmp-type/code");
4727 if (r->overload_tblname[0] &&
4728 r->max_src_conn == 0 && r->max_src_conn_rate.seconds == 0) {
4729 yyerror("'overload' requires 'max-src-conn' "
4730 "or 'max-src-conn-rate'");
4733 if ((r->proto == IPPROTO_ICMP && r->af == AF_INET6) ||
4734 (r->proto == IPPROTO_ICMPV6 && r->af == AF_INET)) {
4735 yyerror("proto %s doesn't match address family %s",
4736 r->proto == IPPROTO_ICMP ? "icmp" : "icmp6",
4737 r->af == AF_INET ? "inet" : "inet6");
4740 if (r->allow_opts && r->action != PF_PASS) {
4741 yyerror("allow-opts can only be specified for pass rules");
4744 if (r->rule_flag & PFRULE_FRAGMENT && (r->src.port_op ||
4745 r->dst.port_op || r->flagset || r->type || r->code)) {
4746 yyerror("fragments can be filtered only on IP header fields");
4749 if (r->rule_flag & PFRULE_RETURNRST && r->proto != IPPROTO_TCP) {
4750 yyerror("return-rst can only be applied to TCP rules");
4753 if (r->max_src_nodes && !(r->rule_flag & PFRULE_RULESRCTRACK)) {
4754 yyerror("max-src-nodes requires 'source-track rule'");
4757 if (r->action == PF_DROP && r->keep_state) {
4758 yyerror("keep state on block rules doesn't make sense");
4761 if (r->rule_flag & PFRULE_STATESLOPPY &&
4762 (r->keep_state == PF_STATE_MODULATE ||
4763 r->keep_state == PF_STATE_SYNPROXY)) {
4764 yyerror("sloppy state matching cannot be used with "
4765 "synproxy state or modulate state");
4772 nat_consistent(struct pfctl_rule *r)
4774 return (0); /* yeah! */
4778 rdr_consistent(struct pfctl_rule *r)
4782 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP) {
4783 if (r->src.port_op) {
4784 yyerror("src port only applies to tcp/udp");
4787 if (r->dst.port_op) {
4788 yyerror("dst port only applies to tcp/udp");
4791 if (r->rpool.proxy_port[0]) {
4792 yyerror("rpool port only applies to tcp/udp");
4796 if (r->dst.port_op &&
4797 r->dst.port_op != PF_OP_EQ && r->dst.port_op != PF_OP_RRG) {
4798 yyerror("invalid port operator for rdr destination port");
4805 process_tabledef(char *name, struct table_opts *opts)
4807 struct pfr_buffer ab;
4808 struct node_tinit *ti;
4809 unsigned long maxcount;
4810 size_t s = sizeof(maxcount);
4812 bzero(&ab, sizeof(ab));
4813 ab.pfrb_type = PFRB_ADDRS;
4814 SIMPLEQ_FOREACH(ti, &opts->init_nodes, entries) {
4816 if (pfr_buf_load(&ab, ti->file, 0, append_addr)) {
4818 yyerror("cannot load \"%s\": %s",
4819 ti->file, strerror(errno));
4821 yyerror("file \"%s\" contains bad data",
4826 if (append_addr_host(&ab, ti->host, 0, 0)) {
4827 yyerror("cannot create address buffer: %s",
4832 if (pf->opts & PF_OPT_VERBOSE)
4833 print_tabledef(name, opts->flags, opts->init_addr,
4835 if (!(pf->opts & PF_OPT_NOACTION) &&
4836 pfctl_define_table(name, opts->flags, opts->init_addr,
4837 pf->anchor->name, &ab, pf->anchor->ruleset.tticket)) {
4839 if (sysctlbyname("net.pf.request_maxcount", &maxcount, &s,
4843 if (ab.pfrb_size > maxcount)
4844 yyerror("cannot define table %s: too many elements.\n"
4845 "Consider increasing net.pf.request_maxcount.",
4848 yyerror("cannot define table %s: %s", name,
4849 pfr_strerror(errno));
4866 /* macro gore, but you should've seen the prior indentation nightmare... */
4868 #define FREE_LIST(T,r) \
4871 while (node != NULL) { \
4873 node = node->next; \
4878 #define LOOP_THROUGH(T,n,r,C) \
4882 r = calloc(1, sizeof(T)); \
4884 err(1, "LOOP: calloc"); \
4888 while (n != NULL) { \
4897 expand_label_str(char *label, size_t len, const char *srch, const char *repl)
4902 if ((tmp = calloc(1, len)) == NULL)
4903 err(1, "expand_label_str: calloc");
4905 while ((q = strstr(p, srch)) != NULL) {
4907 if ((strlcat(tmp, p, len) >= len) ||
4908 (strlcat(tmp, repl, len) >= len))
4909 errx(1, "expand_label: label too long");
4913 if (strlcat(tmp, p, len) >= len)
4914 errx(1, "expand_label: label too long");
4915 strlcpy(label, tmp, len); /* always fits */
4920 expand_label_if(const char *name, char *label, size_t len, const char *ifname)
4922 if (strstr(label, name) != NULL) {
4924 expand_label_str(label, len, name, "any");
4926 expand_label_str(label, len, name, ifname);
4931 expand_label_addr(const char *name, char *label, size_t len, sa_family_t af,
4932 struct node_host *h)
4934 char tmp[64], tmp_not[66];
4936 if (strstr(label, name) != NULL) {
4937 switch (h->addr.type) {
4938 case PF_ADDR_DYNIFTL:
4939 snprintf(tmp, sizeof(tmp), "(%s)", h->addr.v.ifname);
4942 snprintf(tmp, sizeof(tmp), "<%s>", h->addr.v.tblname);
4944 case PF_ADDR_NOROUTE:
4945 snprintf(tmp, sizeof(tmp), "no-route");
4947 case PF_ADDR_URPFFAILED:
4948 snprintf(tmp, sizeof(tmp), "urpf-failed");
4950 case PF_ADDR_ADDRMASK:
4951 if (!af || (PF_AZERO(&h->addr.v.a.addr, af) &&
4952 PF_AZERO(&h->addr.v.a.mask, af)))
4953 snprintf(tmp, sizeof(tmp), "any");
4958 if (inet_ntop(af, &h->addr.v.a.addr, a,
4960 snprintf(tmp, sizeof(tmp), "?");
4962 bits = unmask(&h->addr.v.a.mask, af);
4963 if ((af == AF_INET && bits < 32) ||
4964 (af == AF_INET6 && bits < 128))
4965 snprintf(tmp, sizeof(tmp),
4968 snprintf(tmp, sizeof(tmp),
4974 snprintf(tmp, sizeof(tmp), "?");
4979 snprintf(tmp_not, sizeof(tmp_not), "! %s", tmp);
4980 expand_label_str(label, len, name, tmp_not);
4982 expand_label_str(label, len, name, tmp);
4987 expand_label_port(const char *name, char *label, size_t len,
4988 struct node_port *port)
4990 char a1[6], a2[6], op[13] = "";
4992 if (strstr(label, name) != NULL) {
4993 snprintf(a1, sizeof(a1), "%u", ntohs(port->port[0]));
4994 snprintf(a2, sizeof(a2), "%u", ntohs(port->port[1]));
4997 else if (port->op == PF_OP_IRG)
4998 snprintf(op, sizeof(op), "%s><%s", a1, a2);
4999 else if (port->op == PF_OP_XRG)
5000 snprintf(op, sizeof(op), "%s<>%s", a1, a2);
5001 else if (port->op == PF_OP_EQ)
5002 snprintf(op, sizeof(op), "%s", a1);
5003 else if (port->op == PF_OP_NE)
5004 snprintf(op, sizeof(op), "!=%s", a1);
5005 else if (port->op == PF_OP_LT)
5006 snprintf(op, sizeof(op), "<%s", a1);
5007 else if (port->op == PF_OP_LE)
5008 snprintf(op, sizeof(op), "<=%s", a1);
5009 else if (port->op == PF_OP_GT)
5010 snprintf(op, sizeof(op), ">%s", a1);
5011 else if (port->op == PF_OP_GE)
5012 snprintf(op, sizeof(op), ">=%s", a1);
5013 expand_label_str(label, len, name, op);
5018 expand_label_proto(const char *name, char *label, size_t len, u_int8_t proto)
5020 struct protoent *pe;
5023 if (strstr(label, name) != NULL) {
5024 pe = getprotobynumber(proto);
5026 expand_label_str(label, len, name, pe->p_name);
5028 snprintf(n, sizeof(n), "%u", proto);
5029 expand_label_str(label, len, name, n);
5035 expand_label_nr(const char *name, char *label, size_t len)
5039 if (strstr(label, name) != NULL) {
5040 snprintf(n, sizeof(n), "%u", pf->anchor->match);
5041 expand_label_str(label, len, name, n);
5046 expand_label(char *label, size_t len, const char *ifname, sa_family_t af,
5047 struct node_host *src_host, struct node_port *src_port,
5048 struct node_host *dst_host, struct node_port *dst_port,
5051 expand_label_if("$if", label, len, ifname);
5052 expand_label_addr("$srcaddr", label, len, af, src_host);
5053 expand_label_addr("$dstaddr", label, len, af, dst_host);
5054 expand_label_port("$srcport", label, len, src_port);
5055 expand_label_port("$dstport", label, len, dst_port);
5056 expand_label_proto("$proto", label, len, proto);
5057 expand_label_nr("$nr", label, len);
5061 expand_altq(struct pf_altq *a, struct node_if *interfaces,
5062 struct node_queue *nqueues, struct node_queue_bw bwspec,
5063 struct node_queue_opt *opts)
5065 struct pf_altq pa, pb;
5066 char qname[PF_QNAME_SIZE];
5067 struct node_queue *n;
5068 struct node_queue_bw bw;
5071 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
5072 FREE_LIST(struct node_if, interfaces);
5074 FREE_LIST(struct node_queue, nqueues);
5078 LOOP_THROUGH(struct node_if, interface, interfaces,
5079 memcpy(&pa, a, sizeof(struct pf_altq));
5080 if (strlcpy(pa.ifname, interface->ifname,
5081 sizeof(pa.ifname)) >= sizeof(pa.ifname))
5082 errx(1, "expand_altq: strlcpy");
5084 if (interface->not) {
5085 yyerror("altq on ! <interface> is not supported");
5088 if (eval_pfaltq(pf, &pa, &bwspec, opts))
5091 if (pfctl_add_altq(pf, &pa))
5094 if (pf->opts & PF_OPT_VERBOSE) {
5095 print_altq(&pf->paltq->altq, 0,
5097 if (nqueues && nqueues->tail) {
5099 LOOP_THROUGH(struct node_queue, queue,
5109 if (pa.scheduler == ALTQT_CBQ ||
5110 pa.scheduler == ALTQT_HFSC ||
5111 pa.scheduler == ALTQT_FAIRQ) {
5112 /* now create a root queue */
5113 memset(&pb, 0, sizeof(struct pf_altq));
5114 if (strlcpy(qname, "root_", sizeof(qname)) >=
5116 errx(1, "expand_altq: strlcpy");
5117 if (strlcat(qname, interface->ifname,
5118 sizeof(qname)) >= sizeof(qname))
5119 errx(1, "expand_altq: strlcat");
5120 if (strlcpy(pb.qname, qname,
5121 sizeof(pb.qname)) >= sizeof(pb.qname))
5122 errx(1, "expand_altq: strlcpy");
5123 if (strlcpy(pb.ifname, interface->ifname,
5124 sizeof(pb.ifname)) >= sizeof(pb.ifname))
5125 errx(1, "expand_altq: strlcpy");
5126 pb.qlimit = pa.qlimit;
5127 pb.scheduler = pa.scheduler;
5128 bw.bw_absolute = pa.ifbandwidth;
5130 if (eval_pfqueue(pf, &pb, &bw, opts))
5133 if (pfctl_add_altq(pf, &pb))
5137 LOOP_THROUGH(struct node_queue, queue, nqueues,
5138 n = calloc(1, sizeof(struct node_queue));
5140 err(1, "expand_altq: calloc");
5141 if (pa.scheduler == ALTQT_CBQ ||
5142 pa.scheduler == ALTQT_HFSC ||
5143 pa.scheduler == ALTQT_FAIRQ)
5144 if (strlcpy(n->parent, qname,
5145 sizeof(n->parent)) >=
5147 errx(1, "expand_altq: strlcpy");
5148 if (strlcpy(n->queue, queue->queue,
5149 sizeof(n->queue)) >= sizeof(n->queue))
5150 errx(1, "expand_altq: strlcpy");
5151 if (strlcpy(n->ifname, interface->ifname,
5152 sizeof(n->ifname)) >= sizeof(n->ifname))
5153 errx(1, "expand_altq: strlcpy");
5154 n->scheduler = pa.scheduler;
5160 queues->tail->next = n;
5166 FREE_LIST(struct node_if, interfaces);
5168 FREE_LIST(struct node_queue, nqueues);
5174 expand_queue(struct pf_altq *a, struct node_if *interfaces,
5175 struct node_queue *nqueues, struct node_queue_bw bwspec,
5176 struct node_queue_opt *opts)
5178 struct node_queue *n, *nq;
5183 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
5184 FREE_LIST(struct node_queue, nqueues);
5188 if (queues == NULL) {
5189 yyerror("queue %s has no parent", a->qname);
5190 FREE_LIST(struct node_queue, nqueues);
5194 LOOP_THROUGH(struct node_if, interface, interfaces,
5195 LOOP_THROUGH(struct node_queue, tqueue, queues,
5196 if (!strncmp(a->qname, tqueue->queue, PF_QNAME_SIZE) &&
5197 (interface->ifname[0] == 0 ||
5198 (!interface->not && !strncmp(interface->ifname,
5199 tqueue->ifname, IFNAMSIZ)) ||
5200 (interface->not && strncmp(interface->ifname,
5201 tqueue->ifname, IFNAMSIZ)))) {
5202 /* found ourself in queues */
5205 memcpy(&pa, a, sizeof(struct pf_altq));
5207 if (pa.scheduler != ALTQT_NONE &&
5208 pa.scheduler != tqueue->scheduler) {
5209 yyerror("exactly one scheduler type "
5210 "per interface allowed");
5213 pa.scheduler = tqueue->scheduler;
5215 /* scheduler dependent error checking */
5216 switch (pa.scheduler) {
5218 if (nqueues != NULL) {
5219 yyerror("priq queues cannot "
5220 "have child queues");
5223 if (bwspec.bw_absolute > 0 ||
5224 bwspec.bw_percent < 100) {
5225 yyerror("priq doesn't take "
5234 if (strlcpy(pa.ifname, tqueue->ifname,
5235 sizeof(pa.ifname)) >= sizeof(pa.ifname))
5236 errx(1, "expand_queue: strlcpy");
5237 if (strlcpy(pa.parent, tqueue->parent,
5238 sizeof(pa.parent)) >= sizeof(pa.parent))
5239 errx(1, "expand_queue: strlcpy");
5241 if (eval_pfqueue(pf, &pa, &bwspec, opts))
5244 if (pfctl_add_altq(pf, &pa))
5247 for (nq = nqueues; nq != NULL; nq = nq->next) {
5248 if (!strcmp(a->qname, nq->queue)) {
5249 yyerror("queue cannot have "
5255 sizeof(struct node_queue));
5257 err(1, "expand_queue: calloc");
5258 if (strlcpy(n->parent, a->qname,
5259 sizeof(n->parent)) >=
5261 errx(1, "expand_queue strlcpy");
5262 if (strlcpy(n->queue, nq->queue,
5263 sizeof(n->queue)) >=
5265 errx(1, "expand_queue strlcpy");
5266 if (strlcpy(n->ifname, tqueue->ifname,
5267 sizeof(n->ifname)) >=
5269 errx(1, "expand_queue strlcpy");
5270 n->scheduler = tqueue->scheduler;
5276 queues->tail->next = n;
5280 if ((pf->opts & PF_OPT_VERBOSE) && (
5281 (found == 1 && interface->ifname[0] == 0) ||
5282 (found > 0 && interface->ifname[0] != 0))) {
5283 print_queue(&pf->paltq->altq, 0,
5284 &bwspec, interface->ifname[0] != 0,
5286 if (nqueues && nqueues->tail) {
5288 LOOP_THROUGH(struct node_queue,
5301 FREE_LIST(struct node_queue, nqueues);
5302 FREE_LIST(struct node_if, interfaces);
5305 yyerror("queue %s has no parent", a->qname);
5316 expand_rule(struct pfctl_rule *r,
5317 struct node_if *interfaces, struct node_host *rpool_hosts,
5318 struct node_proto *protos, struct node_os *src_oses,
5319 struct node_host *src_hosts, struct node_port *src_ports,
5320 struct node_host *dst_hosts, struct node_port *dst_ports,
5321 struct node_uid *uids, struct node_gid *gids, struct node_icmp *icmp_types,
5322 const char *anchor_call)
5324 sa_family_t af = r->af;
5325 int added = 0, error = 0;
5326 char ifname[IF_NAMESIZE];
5327 char label[PF_RULE_MAX_LABEL_COUNT][PF_RULE_LABEL_SIZE];
5328 char tagname[PF_TAG_NAME_SIZE];
5329 char match_tagname[PF_TAG_NAME_SIZE];
5330 struct pf_pooladdr *pa;
5331 struct node_host *h;
5332 u_int8_t flags, flagset, keep_state;
5334 memcpy(label, r->label, sizeof(r->label));
5335 assert(sizeof(r->label) == sizeof(label));
5336 if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname))
5337 errx(1, "expand_rule: strlcpy");
5338 if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >=
5339 sizeof(match_tagname))
5340 errx(1, "expand_rule: strlcpy");
5342 flagset = r->flagset;
5343 keep_state = r->keep_state;
5345 LOOP_THROUGH(struct node_if, interface, interfaces,
5346 LOOP_THROUGH(struct node_proto, proto, protos,
5347 LOOP_THROUGH(struct node_icmp, icmp_type, icmp_types,
5348 LOOP_THROUGH(struct node_host, src_host, src_hosts,
5349 LOOP_THROUGH(struct node_port, src_port, src_ports,
5350 LOOP_THROUGH(struct node_os, src_os, src_oses,
5351 LOOP_THROUGH(struct node_host, dst_host, dst_hosts,
5352 LOOP_THROUGH(struct node_port, dst_port, dst_ports,
5353 LOOP_THROUGH(struct node_uid, uid, uids,
5354 LOOP_THROUGH(struct node_gid, gid, gids,
5357 /* for link-local IPv6 address, interface must match up */
5358 if ((r->af && src_host->af && r->af != src_host->af) ||
5359 (r->af && dst_host->af && r->af != dst_host->af) ||
5360 (src_host->af && dst_host->af &&
5361 src_host->af != dst_host->af) ||
5362 (src_host->ifindex && dst_host->ifindex &&
5363 src_host->ifindex != dst_host->ifindex) ||
5364 (src_host->ifindex && *interface->ifname &&
5365 src_host->ifindex != if_nametoindex(interface->ifname)) ||
5366 (dst_host->ifindex && *interface->ifname &&
5367 dst_host->ifindex != if_nametoindex(interface->ifname)))
5369 if (!r->af && src_host->af)
5370 r->af = src_host->af;
5371 else if (!r->af && dst_host->af)
5372 r->af = dst_host->af;
5374 if (*interface->ifname)
5375 strlcpy(r->ifname, interface->ifname,
5377 else if (if_indextoname(src_host->ifindex, ifname))
5378 strlcpy(r->ifname, ifname, sizeof(r->ifname));
5379 else if (if_indextoname(dst_host->ifindex, ifname))
5380 strlcpy(r->ifname, ifname, sizeof(r->ifname));
5382 memset(r->ifname, '\0', sizeof(r->ifname));
5384 memcpy(r->label, label, sizeof(r->label));
5385 if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >=
5387 errx(1, "expand_rule: strlcpy");
5388 if (strlcpy(r->match_tagname, match_tagname,
5389 sizeof(r->match_tagname)) >= sizeof(r->match_tagname))
5390 errx(1, "expand_rule: strlcpy");
5391 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++)
5392 expand_label(r->label[i], PF_RULE_LABEL_SIZE,
5393 r->ifname, r->af, src_host, src_port, dst_host,
5394 dst_port, proto->proto);
5395 expand_label(r->tagname, PF_TAG_NAME_SIZE, r->ifname, r->af,
5396 src_host, src_port, dst_host, dst_port, proto->proto);
5397 expand_label(r->match_tagname, PF_TAG_NAME_SIZE, r->ifname,
5398 r->af, src_host, src_port, dst_host, dst_port,
5401 error += check_netmask(src_host, r->af);
5402 error += check_netmask(dst_host, r->af);
5404 r->ifnot = interface->not;
5405 r->proto = proto->proto;
5406 r->src.addr = src_host->addr;
5407 r->src.neg = src_host->not;
5408 r->src.port[0] = src_port->port[0];
5409 r->src.port[1] = src_port->port[1];
5410 r->src.port_op = src_port->op;
5411 r->dst.addr = dst_host->addr;
5412 r->dst.neg = dst_host->not;
5413 r->dst.port[0] = dst_port->port[0];
5414 r->dst.port[1] = dst_port->port[1];
5415 r->dst.port_op = dst_port->op;
5416 r->uid.op = uid->op;
5417 r->uid.uid[0] = uid->uid[0];
5418 r->uid.uid[1] = uid->uid[1];
5419 r->gid.op = gid->op;
5420 r->gid.gid[0] = gid->gid[0];
5421 r->gid.gid[1] = gid->gid[1];
5422 r->type = icmp_type->type;
5423 r->code = icmp_type->code;
5425 if ((keep_state == PF_STATE_MODULATE ||
5426 keep_state == PF_STATE_SYNPROXY) &&
5427 r->proto && r->proto != IPPROTO_TCP)
5428 r->keep_state = PF_STATE_NORMAL;
5430 r->keep_state = keep_state;
5432 if (r->proto && r->proto != IPPROTO_TCP) {
5437 r->flagset = flagset;
5439 if (icmp_type->proto && r->proto != icmp_type->proto) {
5440 yyerror("icmp-type mismatch");
5444 if (src_os && src_os->os) {
5445 r->os_fingerprint = pfctl_get_fingerprint(src_os->os);
5446 if ((pf->opts & PF_OPT_VERBOSE2) &&
5447 r->os_fingerprint == PF_OSFP_NOMATCH)
5449 "warning: unknown '%s' OS fingerprint\n",
5452 r->os_fingerprint = PF_OSFP_ANY;
5455 TAILQ_INIT(&r->rpool.list);
5456 for (h = rpool_hosts; h != NULL; h = h->next) {
5457 pa = calloc(1, sizeof(struct pf_pooladdr));
5459 err(1, "expand_rule: calloc");
5461 if (h->ifname != NULL) {
5462 if (strlcpy(pa->ifname, h->ifname,
5463 sizeof(pa->ifname)) >=
5465 errx(1, "expand_rule: strlcpy");
5468 TAILQ_INSERT_TAIL(&r->rpool.list, pa, entries);
5471 if (rule_consistent(r, anchor_call[0]) < 0 || error)
5472 yyerror("skipping rule due to errors");
5474 r->nr = pf->astack[pf->asd]->match++;
5475 pfctl_append_rule(pf, r, anchor_call);
5481 FREE_LIST(struct node_if, interfaces);
5482 FREE_LIST(struct node_proto, protos);
5483 FREE_LIST(struct node_host, src_hosts);
5484 FREE_LIST(struct node_port, src_ports);
5485 FREE_LIST(struct node_os, src_oses);
5486 FREE_LIST(struct node_host, dst_hosts);
5487 FREE_LIST(struct node_port, dst_ports);
5488 FREE_LIST(struct node_uid, uids);
5489 FREE_LIST(struct node_gid, gids);
5490 FREE_LIST(struct node_icmp, icmp_types);
5491 FREE_LIST(struct node_host, rpool_hosts);
5494 yyerror("rule expands to no valid combination");
5498 expand_skip_interface(struct node_if *interfaces)
5502 if (!interfaces || (!interfaces->next && !interfaces->not &&
5503 !strcmp(interfaces->ifname, "none"))) {
5504 if (pf->opts & PF_OPT_VERBOSE)
5505 printf("set skip on none\n");
5506 errs = pfctl_set_interface_flags(pf, "", PFI_IFLAG_SKIP, 0);
5510 if (pf->opts & PF_OPT_VERBOSE)
5511 printf("set skip on {");
5512 LOOP_THROUGH(struct node_if, interface, interfaces,
5513 if (pf->opts & PF_OPT_VERBOSE)
5514 printf(" %s", interface->ifname);
5515 if (interface->not) {
5516 yyerror("skip on ! <interface> is not supported");
5519 errs += pfctl_set_interface_flags(pf,
5520 interface->ifname, PFI_IFLAG_SKIP, 1);
5522 if (pf->opts & PF_OPT_VERBOSE)
5525 FREE_LIST(struct node_if, interfaces);
5537 check_rulestate(int desired_state)
5539 if (require_order && (rulestate > desired_state)) {
5540 yyerror("Rules must be in order: options, normalization, "
5541 "queueing, translation, filtering");
5544 rulestate = desired_state;
5549 kw_cmp(const void *k, const void *e)
5551 return (strcmp(k, ((const struct keywords *)e)->k_name));
5557 /* this has to be sorted always */
5558 static const struct keywords keywords[] = {
5560 { "allow-opts", ALLOWOPTS},
5562 { "anchor", ANCHOR},
5563 { "antispoof", ANTISPOOF},
5565 { "bandwidth", BANDWIDTH},
5567 { "binat-anchor", BINATANCHOR},
5568 { "bitmask", BITMASK},
5570 { "block-policy", BLOCKPOLICY},
5571 { "buckets", BUCKETS},
5575 { "crop", FRAGCROP},
5577 { "divert-reply", DIVERTREPLY},
5578 { "divert-to", DIVERTTO},
5580 { "drop-ovl", FRAGDROP},
5582 { "fail-policy", FAILPOLICY},
5584 { "fastroute", FASTROUTE},
5585 { "file", FILENAME},
5586 { "fingerprints", FINGERPRINTS},
5588 { "floating", FLOATING},
5591 { "fragment", FRAGMENT},
5593 { "global", GLOBAL},
5597 { "hostid", HOSTID},
5598 { "icmp-type", ICMPTYPE},
5599 { "icmp6-type", ICMP6TYPE},
5600 { "if-bound", IFBOUND},
5602 { "include", INCLUDE},
5605 { "interval", INTERVAL},
5607 { "keepcounters", KEEPCOUNTERS},
5610 { "linkshare", LINKSHARE},
5613 { "loginterface", LOGINTERFACE},
5614 { "map-e-portset", MAPEPORTSET},
5616 { "max-mss", MAXMSS},
5617 { "max-src-conn", MAXSRCCONN},
5618 { "max-src-conn-rate", MAXSRCCONNRATE},
5619 { "max-src-nodes", MAXSRCNODES},
5620 { "max-src-states", MAXSRCSTATES},
5621 { "min-ttl", MINTTL},
5622 { "modulate", MODULATE},
5624 { "nat-anchor", NATANCHOR},
5627 { "no-route", NOROUTE},
5628 { "no-sync", NOSYNC},
5630 { "optimization", OPTIMIZATION},
5633 { "overload", OVERLOAD},
5637 { "priority", PRIORITY},
5639 { "probability", PROBABILITY},
5641 { "qlimit", QLIMIT},
5644 { "random", RANDOM},
5645 { "random-id", RANDOMID},
5647 { "rdr-anchor", RDRANCHOR},
5648 { "realtime", REALTIME},
5649 { "reassemble", REASSEMBLE},
5650 { "reply-to", REPLYTO},
5651 { "require-order", REQUIREORDER},
5652 { "return", RETURN},
5653 { "return-icmp", RETURNICMP},
5654 { "return-icmp6", RETURNICMP6},
5655 { "return-rst", RETURNRST},
5656 { "round-robin", ROUNDROBIN},
5658 { "route-to", ROUTETO},
5659 { "rtable", RTABLE},
5661 { "ruleset-optimization", RULESET_OPTIMIZATION},
5664 { "set-tos", SETTOS},
5666 { "sloppy", SLOPPY},
5667 { "source-hash", SOURCEHASH},
5668 { "source-track", SOURCETRACK},
5670 { "state-defaults", STATEDEFAULTS},
5671 { "state-policy", STATEPOLICY},
5672 { "static-port", STATICPORT},
5673 { "sticky-address", STICKYADDRESS},
5674 { "synproxy", SYNPROXY},
5677 { "tagged", TAGGED},
5678 { "target", TARGET},
5679 { "tbrsize", TBRSIZE},
5680 { "timeout", TIMEOUT},
5684 { "upperlimit", UPPERLIMIT},
5685 { "urpf-failed", URPFFAILED},
5688 const struct keywords *p;
5690 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
5691 sizeof(keywords[0]), kw_cmp);
5695 fprintf(stderr, "%s: %d\n", s, p->k_val);
5699 fprintf(stderr, "string: %s\n", s);
5704 #define MAXPUSHBACK 128
5706 static char *parsebuf;
5707 static int parseindex;
5708 static char pushback_buffer[MAXPUSHBACK];
5709 static int pushback_index = 0;
5717 /* Read character from the parsebuffer instead of input. */
5718 if (parseindex >= 0) {
5719 c = parsebuf[parseindex++];
5728 return (pushback_buffer[--pushback_index]);
5731 if ((c = getc(file->stream)) == EOF) {
5732 yyerror("reached end of file while parsing quoted string");
5733 if (popfile() == EOF)
5740 while ((c = getc(file->stream)) == '\\') {
5741 next = getc(file->stream);
5746 yylval.lineno = file->lineno;
5751 if (popfile() == EOF)
5753 c = getc(file->stream);
5765 if (parseindex >= 0)
5768 if (pushback_index < MAXPUSHBACK-1)
5769 return (pushback_buffer[pushback_index++] = c);
5781 /* skip to either EOF or the first real EOL */
5784 c = pushback_buffer[--pushback_index];
5802 int quotec, next, c;
5807 while ((c = lgetc(0)) == ' ' || c == '\t')
5810 yylval.lineno = file->lineno;
5812 while ((c = lgetc(0)) != '\n' && c != EOF)
5814 if (c == '$' && parsebuf == NULL) {
5816 if ((c = lgetc(0)) == EOF)
5819 if (p + 1 >= buf + sizeof(buf) - 1) {
5820 yyerror("string too long");
5823 if (isalnum(c) || c == '_') {
5833 yyerror("macro '%s' not defined", buf);
5846 if ((c = lgetc(quotec)) == EOF)
5851 } else if (c == '\\') {
5852 if ((next = lgetc(quotec)) == EOF)
5854 if (next == quotec || c == ' ' || c == '\t')
5856 else if (next == '\n') {
5862 } else if (c == quotec) {
5866 if (p + 1 >= buf + sizeof(buf) - 1) {
5867 yyerror("string too long");
5872 yylval.v.string = strdup(buf);
5873 if (yylval.v.string == NULL)
5874 err(1, "yylex: strdup");
5879 yylval.v.i = PF_OP_XRG;
5880 return (PORTBINARY);
5887 yylval.v.i = PF_OP_IRG;
5888 return (PORTBINARY);
5900 #define allowed_to_end_number(x) \
5901 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
5903 if (c == '-' || isdigit(c)) {
5906 if ((unsigned)(p-buf) >= sizeof(buf)) {
5907 yyerror("string too long");
5910 } while ((c = lgetc(0)) != EOF && isdigit(c));
5912 if (p == buf + 1 && buf[0] == '-')
5914 if (c == EOF || allowed_to_end_number(c)) {
5915 const char *errstr = NULL;
5918 yylval.v.number = strtonum(buf, LLONG_MIN,
5919 LLONG_MAX, &errstr);
5921 yyerror("\"%s\" invalid number: %s",
5936 #define allowed_in_string(x) \
5937 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
5938 x != '{' && x != '}' && x != '<' && x != '>' && \
5939 x != '!' && x != '=' && x != '/' && x != '#' && \
5942 if (isalnum(c) || c == ':' || c == '_') {
5945 if ((unsigned)(p-buf) >= sizeof(buf)) {
5946 yyerror("string too long");
5949 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
5952 if ((token = lookup(buf)) == STRING)
5953 if ((yylval.v.string = strdup(buf)) == NULL)
5954 err(1, "yylex: strdup");
5958 yylval.lineno = file->lineno;
5967 check_file_secrecy(int fd, const char *fname)
5971 if (fstat(fd, &st)) {
5972 warn("cannot stat %s", fname);
5975 if (st.st_uid != 0 && st.st_uid != getuid()) {
5976 warnx("%s: owner not root or current user", fname);
5979 if (st.st_mode & (S_IRWXG | S_IRWXO)) {
5980 warnx("%s: group/world readable/writeable", fname);
5987 pushfile(const char *name, int secret)
5991 if ((nfile = calloc(1, sizeof(struct file))) == NULL ||
5992 (nfile->name = strdup(name)) == NULL) {
5996 if (TAILQ_FIRST(&files) == NULL && strcmp(nfile->name, "-") == 0) {
5997 nfile->stream = stdin;
5999 if ((nfile->name = strdup("stdin")) == NULL) {
6004 } else if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
6005 warn("%s", nfile->name);
6009 } else if (secret &&
6010 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
6011 fclose(nfile->stream);
6017 TAILQ_INSERT_TAIL(&files, nfile, entry);
6026 if ((prev = TAILQ_PREV(file, files, entry)) != NULL) {
6027 prev->errors += file->errors;
6028 TAILQ_REMOVE(&files, file, entry);
6029 fclose(file->stream);
6039 parse_config(char *filename, struct pfctl *xpf)
6046 rulestate = PFCTL_STATE_NONE;
6047 returnicmpdefault = (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
6048 returnicmp6default =
6049 (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
6050 blockpolicy = PFRULE_DROP;
6051 failpolicy = PFRULE_DROP;
6054 if ((file = pushfile(filename, 0)) == NULL) {
6055 warn("cannot open the main config file!");
6060 errors = file->errors;
6063 /* Free macros and check which have not been used. */
6064 while ((sym = TAILQ_FIRST(&symhead))) {
6065 if ((pf->opts & PF_OPT_VERBOSE2) && !sym->used)
6066 fprintf(stderr, "warning: macro '%s' not "
6067 "used\n", sym->nam);
6070 TAILQ_REMOVE(&symhead, sym, entry);
6074 return (errors ? -1 : 0);
6078 symset(const char *nam, const char *val, int persist)
6082 for (sym = TAILQ_FIRST(&symhead); sym && strcmp(nam, sym->nam);
6083 sym = TAILQ_NEXT(sym, entry))
6087 if (sym->persist == 1)
6092 TAILQ_REMOVE(&symhead, sym, entry);
6096 if ((sym = calloc(1, sizeof(*sym))) == NULL)
6099 sym->nam = strdup(nam);
6100 if (sym->nam == NULL) {
6104 sym->val = strdup(val);
6105 if (sym->val == NULL) {
6111 sym->persist = persist;
6112 TAILQ_INSERT_TAIL(&symhead, sym, entry);
6117 pfctl_cmdline_symset(char *s)
6122 if ((val = strrchr(s, '=')) == NULL)
6125 if ((sym = malloc(strlen(s) - strlen(val) + 1)) == NULL)
6126 err(1, "pfctl_cmdline_symset: malloc");
6128 strlcpy(sym, s, strlen(s) - strlen(val) + 1);
6130 ret = symset(sym, val + 1, 1);
6137 symget(const char *nam)
6141 TAILQ_FOREACH(sym, &symhead, entry)
6142 if (strcmp(nam, sym->nam) == 0) {
6150 mv_rules(struct pfctl_ruleset *src, struct pfctl_ruleset *dst)
6153 struct pfctl_rule *r;
6155 for (i = 0; i < PF_RULESET_MAX; ++i) {
6156 while ((r = TAILQ_FIRST(src->rules[i].active.ptr))
6158 TAILQ_REMOVE(src->rules[i].active.ptr, r, entries);
6159 TAILQ_INSERT_TAIL(dst->rules[i].active.ptr, r, entries);
6160 dst->anchor->match++;
6162 src->anchor->match = 0;
6163 while ((r = TAILQ_FIRST(src->rules[i].inactive.ptr))
6165 TAILQ_REMOVE(src->rules[i].inactive.ptr, r, entries);
6166 TAILQ_INSERT_TAIL(dst->rules[i].inactive.ptr,
6173 decide_address_family(struct node_host *n, sa_family_t *af)
6175 if (*af != 0 || n == NULL)
6178 while ((n = n->next) != NULL) {
6187 remove_invalid_hosts(struct node_host **nh, sa_family_t *af)
6189 struct node_host *n = *nh, *prev = NULL;
6192 if (*af && n->af && n->af != *af) {
6193 /* unlink and free n */
6194 struct node_host *next = n->next;
6196 /* adjust tail pointer */
6197 if (n == (*nh)->tail)
6199 /* adjust previous node's next pointer */
6205 if (n->ifname != NULL)
6219 invalid_redirect(struct node_host *nh, sa_family_t af)
6222 struct node_host *n;
6224 /* tables and dyniftl are ok without an address family */
6225 for (n = nh; n != NULL; n = n->next) {
6226 if (n->addr.type != PF_ADDR_TABLE &&
6227 n->addr.type != PF_ADDR_DYNIFTL) {
6228 yyerror("address family not given and "
6229 "translation address expands to multiple "
6230 "address families");
6236 yyerror("no translation address with matching address family "
6244 atoul(char *s, u_long *ulvalp)
6250 ulval = strtoul(s, &ep, 0);
6251 if (s[0] == '\0' || *ep != '\0')
6253 if (errno == ERANGE && ulval == ULONG_MAX)
6265 if (atoul(n, &ulval) == 0) {
6266 if (ulval > 65535) {
6267 yyerror("illegal port value %lu", ulval);
6270 return (htons(ulval));
6272 s = getservbyname(n, "tcp");
6274 s = getservbyname(n, "udp");
6276 yyerror("unknown port %s", n);
6284 rule_label(struct pfctl_rule *r, char *s[PF_RULE_MAX_LABEL_COUNT])
6286 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++) {
6290 if (strlcpy(r->label[i], s[i], sizeof(r->label[0])) >=
6291 sizeof(r->label[0])) {
6292 yyerror("rule label too long (max %d chars)",
6293 sizeof(r->label[0])-1);
6301 parseicmpspec(char *w, sa_family_t af)
6303 const struct icmpcodeent *p;
6308 icmptype = returnicmpdefault >> 8;
6310 icmptype = returnicmp6default >> 8;
6312 if (atoul(w, &ulval) == -1) {
6313 if ((p = geticmpcodebyname(icmptype, w, af)) == NULL) {
6314 yyerror("unknown icmp code %s", w);
6320 yyerror("invalid icmp code %lu", ulval);
6323 return (icmptype << 8 | ulval);
6327 parseport(char *port, struct range *r, int extensions)
6329 char *p = strchr(port, ':');
6332 if ((r->a = getservice(port)) == -1)
6338 if ((extensions & PPORT_STAR) && !strcmp(p+1, "*")) {
6340 if ((r->a = getservice(port)) == -1)
6346 if ((extensions & PPORT_RANGE)) {
6348 if ((r->a = getservice(port)) == -1 ||
6349 (r->b = getservice(p)) == -1)
6362 pfctl_load_anchors(int dev, struct pfctl *pf, struct pfr_buffer *trans)
6364 struct loadanchors *la;
6366 TAILQ_FOREACH(la, &loadanchorshead, entries) {
6367 if (pf->opts & PF_OPT_VERBOSE)
6368 fprintf(stderr, "\nLoading anchor %s from %s\n",
6369 la->anchorname, la->filename);
6370 if (pfctl_rules(dev, la->filename, pf->opts, pf->optimize,
6371 la->anchorname, trans) == -1)
6379 kw_casecmp(const void *k, const void *e)
6381 return (strcasecmp(k, ((const struct keywords *)e)->k_name));
6385 map_tos(char *s, int *val)
6387 /* DiffServ Codepoints and other TOS mappings */
6388 const struct keywords toswords[] = {
6389 { "af11", IPTOS_DSCP_AF11 },
6390 { "af12", IPTOS_DSCP_AF12 },
6391 { "af13", IPTOS_DSCP_AF13 },
6392 { "af21", IPTOS_DSCP_AF21 },
6393 { "af22", IPTOS_DSCP_AF22 },
6394 { "af23", IPTOS_DSCP_AF23 },
6395 { "af31", IPTOS_DSCP_AF31 },
6396 { "af32", IPTOS_DSCP_AF32 },
6397 { "af33", IPTOS_DSCP_AF33 },
6398 { "af41", IPTOS_DSCP_AF41 },
6399 { "af42", IPTOS_DSCP_AF42 },
6400 { "af43", IPTOS_DSCP_AF43 },
6401 { "critical", IPTOS_PREC_CRITIC_ECP },
6402 { "cs0", IPTOS_DSCP_CS0 },
6403 { "cs1", IPTOS_DSCP_CS1 },
6404 { "cs2", IPTOS_DSCP_CS2 },
6405 { "cs3", IPTOS_DSCP_CS3 },
6406 { "cs4", IPTOS_DSCP_CS4 },
6407 { "cs5", IPTOS_DSCP_CS5 },
6408 { "cs6", IPTOS_DSCP_CS6 },
6409 { "cs7", IPTOS_DSCP_CS7 },
6410 { "ef", IPTOS_DSCP_EF },
6411 { "inetcontrol", IPTOS_PREC_INTERNETCONTROL },
6412 { "lowdelay", IPTOS_LOWDELAY },
6413 { "netcontrol", IPTOS_PREC_NETCONTROL },
6414 { "reliability", IPTOS_RELIABILITY },
6415 { "throughput", IPTOS_THROUGHPUT },
6416 { "va", IPTOS_DSCP_VA }
6418 const struct keywords *p;
6420 p = bsearch(s, toswords, sizeof(toswords)/sizeof(toswords[0]),
6421 sizeof(toswords[0]), kw_casecmp);
6431 rt_tableid_max(void)
6435 size_t l = sizeof(fibs);
6437 if (sysctlbyname("net.fibs", &fibs, &l, NULL, 0) == -1)
6438 fibs = 16; /* XXX RT_MAXFIBS, at least limit it some. */
6440 * As the OpenBSD code only compares > and not >= we need to adjust
6441 * here given we only accept values of 0..n and want to avoid #ifdefs
6446 return (RT_TABLEID_MAX);