1 /* $OpenBSD: parse.y,v 1.519 2007/06/21 19:30:03 henning Exp $ */
4 * Copyright (c) 2001 Markus Friedl. All rights reserved.
5 * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
6 * Copyright (c) 2001 Theo de Raadt. All rights reserved.
7 * Copyright (c) 2002,2003 Henning Brauer. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/types.h>
31 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <netinet/in_systm.h>
35 #include <netinet/ip.h>
36 #include <netinet/ip_icmp.h>
37 #include <netinet/icmp6.h>
38 #include <net/pf/pfvar.h>
39 #include <arpa/inet.h>
40 #include <net/altq/altq.h>
41 #include <net/altq/altq_cbq.h>
42 #include <net/altq/altq_priq.h>
43 #include <net/altq/altq_hfsc.h>
44 #include <net/altq/altq_fairq.h>
60 #include "pfctl_parser.h"
63 static struct pfctl *pf = NULL;
64 static FILE *fin = NULL;
66 static int lineno = 1;
67 static int errors = 0;
68 static int rulestate = 0;
69 static u_int16_t returnicmpdefault =
70 (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
71 static u_int16_t returnicmp6default =
72 (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
73 static int blockpolicy = PFRULE_DROP;
74 static int require_order = 1;
75 static int default_statelock;
76 static int default_keeppolicy_action;
77 static struct node_state_opt *default_keeppolicy_options;
90 struct node_proto *next;
91 struct node_proto *tail;
97 struct node_port *next;
98 struct node_port *tail;
104 struct node_uid *next;
105 struct node_uid *tail;
111 struct node_gid *next;
112 struct node_gid *tail;
119 struct node_icmp *next;
120 struct node_icmp *tail;
123 enum { PF_STATE_OPT_MAX, PF_STATE_OPT_NOSYNC, PF_STATE_OPT_SRCTRACK,
124 PF_STATE_OPT_MAX_SRC_STATES, PF_STATE_OPT_MAX_SRC_CONN,
125 PF_STATE_OPT_MAX_SRC_CONN_RATE, PF_STATE_OPT_MAX_SRC_NODES,
126 PF_STATE_OPT_OVERLOAD, PF_STATE_OPT_STATELOCK,
127 PF_STATE_OPT_TIMEOUT, PF_STATE_OPT_PICKUPS };
129 enum { PF_SRCTRACK_NONE, PF_SRCTRACK, PF_SRCTRACK_GLOBAL, PF_SRCTRACK_RULE };
131 struct node_state_opt {
134 u_int32_t max_states;
135 u_int32_t max_src_states;
136 u_int32_t max_src_conn;
143 char tblname[PF_TABLE_NAME_SIZE];
145 u_int32_t max_src_nodes;
147 u_int8_t pickup_mode;
154 struct node_state_opt *next;
155 struct node_state_opt *tail;
159 struct node_host *host;
160 struct node_port *port;
164 char queue[PF_QNAME_SIZE];
165 char parent[PF_QNAME_SIZE];
166 char ifname[IFNAMSIZ];
168 struct node_queue *next;
169 struct node_queue *tail;
172 struct node_qassign {
179 #define FOM_FLAGS 0x01
180 #define FOM_ICMP 0x02
182 #define FOM_KEEP 0x08
183 #define FOM_SRCTRACK 0x10
184 struct node_uid *uid;
185 struct node_gid *gid;
192 struct node_icmp *icmpspec;
197 struct node_state_opt *options;
202 struct node_qassign queues;
205 u_int8_t match_tag_not;
209 struct antispoof_opts {
216 #define SOM_MINTTL 0x01
217 #define SOM_MAXMSS 0x02
218 #define SOM_FRAGCACHE 0x04
230 #define QOM_BWSPEC 0x01
231 #define QOM_SCHEDULER 0x02
232 #define QOM_PRIORITY 0x04
233 #define QOM_TBRSIZE 0x08
234 #define QOM_QLIMIT 0x10
235 struct node_queue_bw queue_bwspec;
236 struct node_queue_opt scheduler;
245 struct node_tinithead init_nodes;
250 #define POM_TYPE 0x01
251 #define POM_STICKYADDRESS 0x02
255 struct pf_poolhashkey *key;
260 struct node_hfsc_opts hfsc_opts;
261 struct node_fairq_opts fairq_opts;
263 int yyerror(const char *, ...);
264 int disallow_table(struct node_host *, const char *);
265 int disallow_urpf_failed(struct node_host *, const char *);
266 int disallow_alias(struct node_host *, const char *);
267 int rule_consistent(struct pf_rule *, int);
268 int filter_consistent(struct pf_rule *, int);
269 int nat_consistent(struct pf_rule *);
270 int rdr_consistent(struct pf_rule *);
271 int process_tabledef(char *, struct table_opts *);
273 void expand_label_str(char *, size_t, const char *, const char *);
274 void expand_label_if(const char *, char *, size_t, const char *);
275 void expand_label_addr(const char *, char *, size_t, u_int8_t,
277 void expand_label_port(const char *, char *, size_t, struct node_port *);
278 void expand_label_proto(const char *, char *, size_t, u_int8_t);
279 void expand_label_nr(const char *, char *, size_t);
280 void expand_label(char *, size_t, const char *, u_int8_t, struct node_host *,
281 struct node_port *, struct node_host *, struct node_port *,
283 void expand_rule(struct pf_rule *, struct node_if *, struct node_host *,
284 struct node_proto *, struct node_os*, struct node_host *,
285 struct node_port *, struct node_host *, struct node_port *,
286 struct node_uid *, struct node_gid *, struct node_icmp *,
288 int expand_altq(struct pf_altq *, struct node_if *, struct node_queue *,
289 struct node_queue_bw bwspec, struct node_queue_opt *);
290 int expand_queue(struct pf_altq *, struct node_if *, struct node_queue *,
291 struct node_queue_bw, struct node_queue_opt *);
292 int expand_skip_interface(struct node_if *);
294 int check_rulestate(int);
295 int kw_cmp(const void *, const void *);
301 int atoul(char *, u_long *);
302 int getservice(char *);
303 int rule_label(struct pf_rule *, char *);
305 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
307 TAILQ_ENTRY(sym) entries;
315 int symset(const char *, const char *, int);
316 char *symget(const char *);
318 void mv_rules(struct pf_ruleset *, struct pf_ruleset *);
319 void decide_address_family(struct node_host *, sa_family_t *);
320 void remove_invalid_hosts(struct node_host **, sa_family_t *);
321 int invalid_redirect(struct node_host *, sa_family_t);
322 u_int16_t parseicmpspec(char *, sa_family_t);
324 TAILQ_HEAD(loadanchorshead, loadanchors)
325 loadanchorshead = TAILQ_HEAD_INITIALIZER(loadanchorshead);
328 TAILQ_ENTRY(loadanchors) entries;
350 struct node_if *interface;
351 struct node_proto *proto;
352 struct node_icmp *icmp;
353 struct node_host *host;
355 struct node_port *port;
356 struct node_uid *uid;
357 struct node_gid *gid;
358 struct node_state_opt *state_opt;
361 struct peer src, dst;
362 struct node_os *src_os;
365 struct node_host *host;
369 struct pf_poolhashkey *key;
372 struct node_host *host;
377 struct node_state_opt *options;
388 struct pf_poolhashkey *hashkey;
389 struct node_queue *queue;
390 struct node_queue_opt queue_options;
391 struct node_queue_bw queue_bwspec;
392 struct node_qassign qassign;
393 struct filter_opts filter_opts;
394 struct antispoof_opts antispoof_opts;
395 struct queue_opts queue_opts;
396 struct scrub_opts scrub_opts;
397 struct table_opts table_opts;
398 struct pool_opts pool_opts;
399 struct node_hfsc_opts hfsc_opts;
400 struct node_fairq_opts fairq_opts;
405 #define DYNIF_MULTIADDR(addr) ((addr).type == PF_ADDR_DYNIFTL && \
406 (!((addr).iflags & PFI_AFLAG_NOALIAS) || \
407 !isdigit((addr).v.ifname[strlen((addr).v.ifname)-1])))
413 %token PASS BLOCK SCRUB RETURN IN OS OUT LOG QUICK ON FROM TO FLAGS
414 %token RETURNRST RETURNICMP RETURNICMP6 PROTO INET INET6 ALL ANY ICMPTYPE
415 %token ICMP6TYPE CODE KEEP MODULATE STATE PORT RDR NAT BINAT ARROW NODF
416 %token MINTTL ERROR ALLOWOPTS FASTROUTE FILENAME ROUTETO DUPTO REPLYTO NO LABEL
417 %token NOROUTE URPFFAILED FRAGMENT USER GROUP MAXMSS MAXIMUM TTL TOS DROP TABLE
418 %token REASSEMBLE FRAGDROP FRAGCROP ANCHOR NATANCHOR RDRANCHOR BINATANCHOR
419 %token SET OPTIMIZATION TIMEOUT LIMIT LOGINTERFACE BLOCKPOLICY RANDOMID
420 %token REQUIREORDER SYNPROXY FINGERPRINTS NOSYNC DEBUG SKIP HOSTID
422 %token BITMASK RANDOM SOURCEHASH ROUNDROBIN STATICPORT PROBABILITY
423 %token ALTQ CBQ PRIQ HFSC FAIRQ BANDWIDTH TBRSIZE LINKSHARE REALTIME UPPERLIMIT
424 %token QUEUE PRIORITY QLIMIT RTABLE HOGS BUCKETS
425 %token LOAD RULESET_OPTIMIZATION
426 %token PICKUPS NOPICKUPS HASHONLY
427 %token STICKYADDRESS MAXSRCSTATES MAXSRCNODES SOURCETRACK GLOBAL RULE
428 %token MAXSRCCONN MAXSRCCONNRATE OVERLOAD FLUSH
429 %token TAGGED TAG IFBOUND FLOATING STATEPOLICY ROUTE KEEPPOLICY
430 %token <v.string> STRING
431 %token <v.i> PORTBINARY
432 %type <v.interface> interface if_list if_item_not if_item
433 %type <v.number> number icmptype icmp6type uid gid
434 %type <v.number> tos not yesno
435 %type <v.i> no dir af fragcache optimizer
436 %type <v.i> sourcetrack flush unaryop statelock
437 %type <v.b> action nataction natpasslog scrubaction
438 %type <v.b> flags flag blockspec
439 %type <v.range> port rport
440 %type <v.hashkey> hashkey
441 %type <v.proto> proto proto_list proto_item
442 %type <v.icmp> icmpspec
443 %type <v.icmp> icmp_list icmp_item
444 %type <v.icmp> icmp6_list icmp6_item
445 %type <v.fromto> fromto
446 %type <v.peer> ipportspec from to
447 %type <v.host> ipspec xhost host dynaddr host_list
448 %type <v.host> redir_host_list redirspec
449 %type <v.host> route_host route_host_list routespec
450 %type <v.os> os xos os_list
451 %type <v.port> portspec port_list port_item
452 %type <v.uid> uids uid_list uid_item
453 %type <v.gid> gids gid_list gid_item
454 %type <v.route> route
455 %type <v.redirection> redirection redirpool
456 %type <v.string> label string tag anchorname
457 %type <v.keep_state> keep
458 %type <v.state_opt> state_opt_spec state_opt_list state_opt_item
459 %type <v.logquick> logquick quick log logopts logopt
460 %type <v.interface> antispoof_ifspc antispoof_iflst antispoof_if
461 %type <v.qassign> qname
462 %type <v.queue> qassign qassign_list qassign_item
463 %type <v.queue_options> scheduler
464 %type <v.number> cbqflags_list cbqflags_item
465 %type <v.number> priqflags_list priqflags_item
466 %type <v.hfsc_opts> hfscopts_list hfscopts_item hfsc_opts
467 %type <v.fairq_opts> fairqopts_list fairqopts_item fairq_opts
468 %type <v.queue_bwspec> bandwidth
469 %type <v.filter_opts> filter_opts filter_opt filter_opts_l
470 %type <v.antispoof_opts> antispoof_opts antispoof_opt antispoof_opts_l
471 %type <v.queue_opts> queue_opts queue_opt queue_opts_l
472 %type <v.scrub_opts> scrub_opts scrub_opt scrub_opts_l
473 %type <v.table_opts> table_opts table_opt table_opts_l
474 %type <v.pool_opts> pool_opts pool_opt pool_opts_l
475 %type <v.tagged> tagged
476 %type <v.rtableid> rtable
479 ruleset : /* empty */
481 | ruleset option '\n'
482 | ruleset scrubrule '\n'
483 | ruleset natrule '\n'
484 | ruleset binatrule '\n'
485 | ruleset pfrule '\n'
486 | ruleset anchorrule '\n'
487 | ruleset loadrule '\n'
488 | ruleset altqif '\n'
489 | ruleset queuespec '\n'
490 | ruleset varset '\n'
491 | ruleset antispoof '\n'
492 | ruleset tabledef '\n'
493 | '{' fakeanchor '}' '\n';
494 | ruleset error '\n' { errors++; }
498 * apply to previouslys specified rule: must be careful to note
499 * what that is: pf or nat or binat or rdr
501 fakeanchor : fakeanchor '\n'
502 | fakeanchor anchorrule '\n'
503 | fakeanchor binatrule '\n'
504 | fakeanchor natrule '\n'
505 | fakeanchor pfrule '\n'
506 | fakeanchor error '\n'
510 if (!strcmp($1, "none"))
512 else if (!strcmp($1, "basic"))
513 $$ = PF_OPTIMIZE_BASIC;
514 else if (!strcmp($1, "profile"))
515 $$ = PF_OPTIMIZE_BASIC | PF_OPTIMIZE_PROFILE;
517 yyerror("unknown ruleset-optimization %s", $$);
523 option : SET OPTIMIZATION STRING {
524 if (check_rulestate(PFCTL_STATE_OPTION)) {
528 if (pfctl_set_optimization(pf, $3) != 0) {
529 yyerror("unknown optimization %s", $3);
535 | SET RULESET_OPTIMIZATION optimizer {
536 if (!(pf->opts & PF_OPT_OPTIMIZE)) {
537 pf->opts |= PF_OPT_OPTIMIZE;
541 | SET TIMEOUT timeout_spec
542 | SET TIMEOUT '{' timeout_list '}'
543 | SET LIMIT limit_spec
544 | SET LIMIT '{' limit_list '}'
545 | SET LOGINTERFACE STRING {
546 if (check_rulestate(PFCTL_STATE_OPTION)) {
550 if (pfctl_set_logif(pf, $3) != 0) {
551 yyerror("error setting loginterface %s", $3);
557 | SET HOSTID number {
559 yyerror("hostid must be non-zero");
562 if (pfctl_set_hostid(pf, $3) != 0) {
563 yyerror("error setting hostid %08x", $3);
567 | SET BLOCKPOLICY DROP {
568 if (pf->opts & PF_OPT_VERBOSE)
569 printf("set block-policy drop\n");
570 if (check_rulestate(PFCTL_STATE_OPTION))
572 blockpolicy = PFRULE_DROP;
574 | SET BLOCKPOLICY RETURN {
575 if (pf->opts & PF_OPT_VERBOSE)
576 printf("set block-policy return\n");
577 if (check_rulestate(PFCTL_STATE_OPTION))
579 blockpolicy = PFRULE_RETURN;
581 | SET REQUIREORDER yesno {
582 if (pf->opts & PF_OPT_VERBOSE)
583 printf("set require-order %s\n",
584 $3 == 1 ? "yes" : "no");
587 | SET FINGERPRINTS STRING {
588 if (pf->opts & PF_OPT_VERBOSE)
589 printf("set fingerprints \"%s\"\n", $3);
590 if (check_rulestate(PFCTL_STATE_OPTION)) {
594 if (!pf->anchor->name[0]) {
595 if (pfctl_file_fingerprints(pf->dev,
597 yyerror("error loading "
598 "fingerprints %s", $3);
605 | SET STATEPOLICY statelock {
606 if (pf->opts & PF_OPT_VERBOSE)
609 printf("set state-policy floating\n");
612 printf("set state-policy if-bound\n");
615 default_statelock = $3;
617 | SET KEEPPOLICY keep {
618 if (pf->opts & PF_OPT_VERBOSE)
619 printf("A default keeppolicy was set\n");
620 default_keeppolicy_action = $3.action;
621 default_keeppolicy_options = $3.options;
624 if (check_rulestate(PFCTL_STATE_OPTION)) {
628 if (pfctl_set_debug(pf, $3) != 0) {
629 yyerror("error setting debuglevel %s", $3);
635 | SET SKIP interface {
636 if (expand_skip_interface($3) != 0) {
637 yyerror("error setting skip interface(s)");
643 string : string STRING {
644 if (asprintf(&$$, "%s %s", $1, $2) == -1)
645 err(1, "string: asprintf");
652 varset : STRING '=' string {
653 if (pf->opts & PF_OPT_VERBOSE)
654 printf("%s = \"%s\"\n", $1, $3);
655 if (symset($1, $3, 0) == -1)
656 err(1, "cannot store variable %s", $1);
662 anchorname : STRING { $$ = $1; }
663 | /* empty */ { $$ = NULL; }
670 pfa_anchorlist : pfrule optnl
672 | pfa_anchorlist pfrule optnl
673 | pfa_anchorlist anchorrule optnl
678 char ta[PF_ANCHOR_NAME_SIZE];
679 struct pf_ruleset *rs;
681 /* steping into a brace anchor */
686 /* create a holding ruleset in the root */
687 snprintf(ta, PF_ANCHOR_NAME_SIZE, "_%d", pf->bn);
688 rs = pf_find_or_create_ruleset(ta);
690 err(1, "pfa_anchor: pf_find_or_create_ruleset");
691 pf->astack[pf->asd] = rs->anchor;
692 pf->anchor = rs->anchor;
693 } '\n' pfa_anchorlist '}'
695 pf->alast = pf->anchor;
697 pf->anchor = pf->astack[pf->asd];
702 anchorrule : ANCHOR anchorname dir quick interface af proto fromto
703 filter_opts pfa_anchor
707 if (check_rulestate(PFCTL_STATE_FILTER)) {
713 if ($2 && ($2[0] == '_' || strstr($2, "/_") != NULL)) {
715 yyerror("anchor names beginning with '_' "
716 "are reserved for internal use");
720 memset(&r, 0, sizeof(r));
721 if (pf->astack[pf->asd + 1]) {
722 /* move inline rules into relative location */
724 &pf->astack[pf->asd]->ruleset,
725 $2 ? $2 : pf->alast->name);
727 if (r.anchor == NULL)
728 err(1, "anchorrule: unable to "
731 if (pf->alast != r.anchor) {
732 if (r.anchor->match) {
733 yyerror("inline anchor '%s' "
738 mv_rules(&pf->alast->ruleset,
741 pf_remove_if_empty_ruleset(&pf->alast->ruleset);
742 pf->alast = r.anchor;
745 yyerror("anchors without explicit "
746 "rules must specify a name");
754 r.rtableid = $9.rtableid;
757 if (strlcpy(r.match_tagname, $9.match_tag,
758 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
759 yyerror("tag too long, max %u chars",
760 PF_TAG_NAME_SIZE - 1);
763 r.match_tag_not = $9.match_tag_not;
765 decide_address_family($8.src.host, &r.af);
766 decide_address_family($8.dst.host, &r.af);
768 expand_rule(&r, $5, NULL, $7, $8.src_os,
769 $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
770 0, 0, 0, pf->astack[pf->asd + 1] ?
771 pf->alast->name : $2);
773 pf->astack[pf->asd + 1] = NULL;
775 | NATANCHOR string interface af proto fromto rtable {
778 if (check_rulestate(PFCTL_STATE_NAT)) {
783 memset(&r, 0, sizeof(r));
788 decide_address_family($6.src.host, &r.af);
789 decide_address_family($6.dst.host, &r.af);
791 expand_rule(&r, $3, NULL, $5, $6.src_os,
792 $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
796 | RDRANCHOR string interface af proto fromto rtable {
799 if (check_rulestate(PFCTL_STATE_NAT)) {
804 memset(&r, 0, sizeof(r));
809 decide_address_family($6.src.host, &r.af);
810 decide_address_family($6.dst.host, &r.af);
812 if ($6.src.port != NULL) {
813 yyerror("source port parameter not supported"
817 if ($6.dst.port != NULL) {
818 if ($6.dst.port->next != NULL) {
819 yyerror("destination port list "
820 "expansion not supported in "
823 } else if ($6.dst.port->op != PF_OP_EQ) {
824 yyerror("destination port operators"
825 " not supported in rdr-anchor");
828 r.dst.port[0] = $6.dst.port->port[0];
829 r.dst.port[1] = $6.dst.port->port[1];
830 r.dst.port_op = $6.dst.port->op;
833 expand_rule(&r, $3, NULL, $5, $6.src_os,
834 $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
838 | BINATANCHOR string interface af proto fromto rtable {
841 if (check_rulestate(PFCTL_STATE_NAT)) {
846 memset(&r, 0, sizeof(r));
851 if ($5->next != NULL) {
852 yyerror("proto list expansion"
853 " not supported in binat-anchor");
860 if ($6.src.host != NULL || $6.src.port != NULL ||
861 $6.dst.host != NULL || $6.dst.port != NULL) {
862 yyerror("fromto parameter not supported"
867 decide_address_family($6.src.host, &r.af);
868 decide_address_family($6.dst.host, &r.af);
870 pfctl_add_rule(pf, &r, $2);
875 loadrule : LOAD ANCHOR string FROM string {
876 struct loadanchors *loadanchor;
878 if (strlen(pf->anchor->name) + 1 +
879 strlen($3) >= MAXPATHLEN) {
880 yyerror("anchorname %s too long, max %u\n",
885 loadanchor = calloc(1, sizeof(struct loadanchors));
886 if (loadanchor == NULL)
887 err(1, "loadrule: calloc");
888 if ((loadanchor->anchorname = malloc(MAXPATHLEN)) ==
890 err(1, "loadrule: malloc");
891 if (pf->anchor->name[0])
892 snprintf(loadanchor->anchorname, MAXPATHLEN,
893 "%s/%s", pf->anchor->name, $3);
895 strlcpy(loadanchor->anchorname, $3, MAXPATHLEN);
896 if ((loadanchor->filename = strdup($5)) == NULL)
897 err(1, "loadrule: strdup");
899 TAILQ_INSERT_TAIL(&loadanchorshead, loadanchor,
906 scrubaction : no SCRUB {
915 scrubrule : scrubaction dir logquick interface af proto fromto scrub_opts
919 if (check_rulestate(PFCTL_STATE_SCRUB))
922 memset(&r, 0, sizeof(r));
930 yyerror("scrub rules do not support 'quick'");
936 r.rule_flag |= PFRULE_NODF;
938 r.rule_flag |= PFRULE_RANDOMID;
939 if ($8.reassemble_tcp) {
940 if (r.direction != PF_INOUT) {
941 yyerror("reassemble tcp rules can not "
942 "specify direction");
945 r.rule_flag |= PFRULE_REASSEMBLE_TCP;
948 r.min_ttl = $8.minttl;
950 r.max_mss = $8.maxmss;
952 r.rule_flag |= $8.fragcache;
953 r.rtableid = $8.rtableid;
955 expand_rule(&r, $4, NULL, $6, $7.src_os,
956 $7.src.host, $7.src.port, $7.dst.host, $7.dst.port,
957 NULL, NULL, NULL, "");
962 bzero(&scrub_opts, sizeof scrub_opts);
963 scrub_opts.rtableid = -1;
968 bzero(&scrub_opts, sizeof scrub_opts);
969 scrub_opts.rtableid = -1;
974 scrub_opts_l : scrub_opts_l scrub_opt
979 if (scrub_opts.nodf) {
980 yyerror("no-df cannot be respecified");
986 if (scrub_opts.marker & SOM_MINTTL) {
987 yyerror("min-ttl cannot be respecified");
991 yyerror("illegal min-ttl value %d", $2);
994 scrub_opts.marker |= SOM_MINTTL;
995 scrub_opts.minttl = $2;
998 if (scrub_opts.marker & SOM_MAXMSS) {
999 yyerror("max-mss cannot be respecified");
1003 yyerror("illegal max-mss value %d", $2);
1006 scrub_opts.marker |= SOM_MAXMSS;
1007 scrub_opts.maxmss = $2;
1010 if (scrub_opts.marker & SOM_FRAGCACHE) {
1011 yyerror("fragcache cannot be respecified");
1014 scrub_opts.marker |= SOM_FRAGCACHE;
1015 scrub_opts.fragcache = $1;
1017 | REASSEMBLE STRING {
1018 if (strcasecmp($2, "tcp") != 0) {
1019 yyerror("scrub reassemble supports only tcp, "
1025 if (scrub_opts.reassemble_tcp) {
1026 yyerror("reassemble tcp cannot be respecified");
1029 scrub_opts.reassemble_tcp = 1;
1032 if (scrub_opts.randomid) {
1033 yyerror("random-id cannot be respecified");
1036 scrub_opts.randomid = 1;
1039 scrub_opts.rtableid = $2;
1043 fragcache : FRAGMENT REASSEMBLE { $$ = 0; /* default */ }
1044 | FRAGMENT FRAGCROP { $$ = PFRULE_FRAGCROP; }
1045 | FRAGMENT FRAGDROP { $$ = PFRULE_FRAGDROP; }
1048 antispoof : ANTISPOOF logquick antispoof_ifspc af antispoof_opts {
1050 struct node_host *h = NULL, *hh;
1051 struct node_if *i, *j;
1053 if (check_rulestate(PFCTL_STATE_FILTER))
1056 for (i = $3; i; i = i->next) {
1057 bzero(&r, sizeof(r));
1060 r.direction = PF_IN;
1065 if (rule_label(&r, $5.label))
1067 r.rtableid = $5.rtableid;
1068 j = calloc(1, sizeof(struct node_if));
1070 err(1, "antispoof: calloc");
1071 if (strlcpy(j->ifname, i->ifname,
1072 sizeof(j->ifname)) >= sizeof(j->ifname)) {
1074 yyerror("interface name too long");
1079 h = calloc(1, sizeof(*h));
1081 err(1, "address: calloc");
1082 h->addr.type = PF_ADDR_DYNIFTL;
1084 if (strlcpy(h->addr.v.ifname, i->ifname,
1085 sizeof(h->addr.v.ifname)) >=
1086 sizeof(h->addr.v.ifname)) {
1089 "interface name too long");
1092 hh = malloc(sizeof(*hh));
1094 err(1, "address: malloc");
1095 bcopy(h, hh, sizeof(*hh));
1096 h->addr.iflags = PFI_AFLAG_NETWORK;
1098 h = ifa_lookup(j->ifname,
1104 expand_rule(&r, j, NULL, NULL, NULL, h,
1105 NULL, NULL, NULL, NULL, NULL,
1108 if ((i->ifa_flags & IFF_LOOPBACK) == 0) {
1109 bzero(&r, sizeof(r));
1112 r.direction = PF_IN;
1116 if (rule_label(&r, $5.label))
1118 r.rtableid = $5.rtableid;
1122 h = ifa_lookup(i->ifname, 0);
1124 expand_rule(&r, NULL, NULL,
1125 NULL, NULL, h, NULL, NULL,
1126 NULL, NULL, NULL, NULL, "");
1134 antispoof_ifspc : FOR antispoof_if { $$ = $2; }
1135 | FOR '{' antispoof_iflst '}' { $$ = $3; }
1138 antispoof_iflst : antispoof_if { $$ = $1; }
1139 | antispoof_iflst comma antispoof_if {
1140 $1->tail->next = $3;
1146 antispoof_if : if_item { $$ = $1; }
1154 bzero(&antispoof_opts, sizeof antispoof_opts);
1155 antispoof_opts.rtableid = -1;
1158 { $$ = antispoof_opts; }
1160 bzero(&antispoof_opts, sizeof antispoof_opts);
1161 antispoof_opts.rtableid = -1;
1162 $$ = antispoof_opts;
1166 antispoof_opts_l : antispoof_opts_l antispoof_opt
1170 antispoof_opt : label {
1171 if (antispoof_opts.label) {
1172 yyerror("label cannot be redefined");
1175 antispoof_opts.label = $1;
1178 antispoof_opts.rtableid = $2;
1182 not : '!' { $$ = 1; }
1183 | /* empty */ { $$ = 0; }
1186 tabledef : TABLE '<' STRING '>' table_opts {
1187 struct node_host *h, *nh;
1188 struct node_tinit *ti, *nti;
1190 if (strlen($3) >= PF_TABLE_NAME_SIZE) {
1191 yyerror("table name too long, max %d chars",
1192 PF_TABLE_NAME_SIZE - 1);
1196 if (pf->loadopt & PFCTL_FLAG_TABLE)
1197 if (process_tabledef($3, &$5)) {
1202 for (ti = SIMPLEQ_FIRST(&$5.init_nodes);
1203 ti != SIMPLEQ_END(&$5.init_nodes); ti = nti) {
1206 for (h = ti->host; h != NULL; h = nh) {
1210 nti = SIMPLEQ_NEXT(ti, entries);
1217 bzero(&table_opts, sizeof table_opts);
1218 SIMPLEQ_INIT(&table_opts.init_nodes);
1221 { $$ = table_opts; }
1224 bzero(&table_opts, sizeof table_opts);
1225 SIMPLEQ_INIT(&table_opts.init_nodes);
1230 table_opts_l : table_opts_l table_opt
1234 table_opt : STRING {
1235 if (!strcmp($1, "const"))
1236 table_opts.flags |= PFR_TFLAG_CONST;
1237 else if (!strcmp($1, "persist"))
1238 table_opts.flags |= PFR_TFLAG_PERSIST;
1240 yyerror("invalid table option '%s'", $1);
1246 | '{' '}' { table_opts.init_addr = 1; }
1247 | '{' host_list '}' {
1248 struct node_host *n;
1249 struct node_tinit *ti;
1251 for (n = $2; n != NULL; n = n->next) {
1252 switch (n->addr.type) {
1253 case PF_ADDR_ADDRMASK:
1255 case PF_ADDR_DYNIFTL:
1256 yyerror("dynamic addresses are not "
1257 "permitted inside tables");
1260 yyerror("tables cannot contain tables");
1262 case PF_ADDR_NOROUTE:
1263 yyerror("\"no-route\" is not permitted "
1266 case PF_ADDR_URPFFAILED:
1267 yyerror("\"urpf-failed\" is not "
1268 "permitted inside tables");
1271 yyerror("unknown address type %d",
1276 if (!(ti = calloc(1, sizeof(*ti))))
1277 err(1, "table_opt: calloc");
1279 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1281 table_opts.init_addr = 1;
1284 struct node_tinit *ti;
1286 if (!(ti = calloc(1, sizeof(*ti))))
1287 err(1, "table_opt: calloc");
1289 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1291 table_opts.init_addr = 1;
1295 altqif : ALTQ interface queue_opts QUEUE qassign {
1298 if (check_rulestate(PFCTL_STATE_QUEUE))
1301 memset(&a, 0, sizeof(a));
1302 if ($3.scheduler.qtype == ALTQT_NONE) {
1303 yyerror("no scheduler specified!");
1306 a.scheduler = $3.scheduler.qtype;
1307 a.qlimit = $3.qlimit;
1308 a.tbrsize = $3.tbrsize;
1310 yyerror("no child queues specified");
1313 if (expand_altq(&a, $2, $5, $3.queue_bwspec,
1319 queuespec : QUEUE STRING interface queue_opts qassign {
1322 if (check_rulestate(PFCTL_STATE_QUEUE)) {
1327 memset(&a, 0, sizeof(a));
1329 if (strlcpy(a.qname, $2, sizeof(a.qname)) >=
1331 yyerror("queue name too long (max "
1332 "%d chars)", PF_QNAME_SIZE-1);
1338 yyerror("cannot specify tbrsize for queue");
1341 if ($4.priority > 255) {
1342 yyerror("priority out of range: max 255");
1345 a.priority = $4.priority;
1346 a.qlimit = $4.qlimit;
1347 a.scheduler = $4.scheduler.qtype;
1348 if (expand_queue(&a, $3, $5, $4.queue_bwspec,
1350 yyerror("errors in queue definition");
1357 bzero(&queue_opts, sizeof queue_opts);
1358 queue_opts.priority = DEFAULT_PRIORITY;
1359 queue_opts.qlimit = DEFAULT_QLIMIT;
1360 queue_opts.scheduler.qtype = ALTQT_NONE;
1361 queue_opts.queue_bwspec.bw_percent = 100;
1364 { $$ = queue_opts; }
1366 bzero(&queue_opts, sizeof queue_opts);
1367 queue_opts.priority = DEFAULT_PRIORITY;
1368 queue_opts.qlimit = DEFAULT_QLIMIT;
1369 queue_opts.scheduler.qtype = ALTQT_NONE;
1370 queue_opts.queue_bwspec.bw_percent = 100;
1375 queue_opts_l : queue_opts_l queue_opt
1379 queue_opt : BANDWIDTH bandwidth {
1380 if (queue_opts.marker & QOM_BWSPEC) {
1381 yyerror("bandwidth cannot be respecified");
1384 queue_opts.marker |= QOM_BWSPEC;
1385 queue_opts.queue_bwspec = $2;
1388 if (queue_opts.marker & QOM_PRIORITY) {
1389 yyerror("priority cannot be respecified");
1393 yyerror("priority out of range: max 255");
1396 queue_opts.marker |= QOM_PRIORITY;
1397 queue_opts.priority = $2;
1400 if (queue_opts.marker & QOM_QLIMIT) {
1401 yyerror("qlimit cannot be respecified");
1405 yyerror("qlimit out of range: max 65535");
1408 queue_opts.marker |= QOM_QLIMIT;
1409 queue_opts.qlimit = $2;
1412 if (queue_opts.marker & QOM_SCHEDULER) {
1413 yyerror("scheduler cannot be respecified");
1416 queue_opts.marker |= QOM_SCHEDULER;
1417 queue_opts.scheduler = $1;
1420 if (queue_opts.marker & QOM_TBRSIZE) {
1421 yyerror("tbrsize cannot be respecified");
1425 yyerror("tbrsize too big: max 65535");
1428 queue_opts.marker |= QOM_TBRSIZE;
1429 queue_opts.tbrsize = $2;
1433 bandwidth : STRING {
1439 bps = strtod($1, &cp);
1441 if (!strcmp(cp, "b"))
1443 else if (!strcmp(cp, "Kb"))
1445 else if (!strcmp(cp, "Mb"))
1447 else if (!strcmp(cp, "Gb"))
1448 bps *= 1000 * 1000 * 1000;
1449 else if (!strcmp(cp, "%")) {
1450 if (bps < 0 || bps > 100) {
1451 yyerror("bandwidth spec "
1456 $$.bw_percent = bps;
1459 yyerror("unknown unit %s", cp);
1465 $$.bw_absolute = (u_int32_t)bps;
1470 $$.qtype = ALTQT_CBQ;
1471 $$.data.cbq_opts.flags = 0;
1473 | CBQ '(' cbqflags_list ')' {
1474 $$.qtype = ALTQT_CBQ;
1475 $$.data.cbq_opts.flags = $3;
1478 $$.qtype = ALTQT_PRIQ;
1479 $$.data.priq_opts.flags = 0;
1481 | PRIQ '(' priqflags_list ')' {
1482 $$.qtype = ALTQT_PRIQ;
1483 $$.data.priq_opts.flags = $3;
1486 $$.qtype = ALTQT_HFSC;
1487 bzero(&$$.data.hfsc_opts,
1488 sizeof(struct node_hfsc_opts));
1490 | HFSC '(' hfsc_opts ')' {
1491 $$.qtype = ALTQT_HFSC;
1492 $$.data.hfsc_opts = $3;
1495 $$.qtype = ALTQT_FAIRQ;
1496 bzero(&$$.data.fairq_opts,
1497 sizeof(struct node_fairq_opts));
1499 | FAIRQ '(' fairq_opts ')' {
1500 $$.qtype = ALTQT_FAIRQ;
1501 $$.data.fairq_opts = $3;
1505 cbqflags_list : cbqflags_item { $$ |= $1; }
1506 | cbqflags_list comma cbqflags_item { $$ |= $3; }
1509 cbqflags_item : STRING {
1510 if (!strcmp($1, "default"))
1511 $$ = CBQCLF_DEFCLASS;
1512 else if (!strcmp($1, "borrow"))
1514 else if (!strcmp($1, "red"))
1516 else if (!strcmp($1, "ecn"))
1517 $$ = CBQCLF_RED|CBQCLF_ECN;
1518 else if (!strcmp($1, "rio"))
1521 yyerror("unknown cbq flag \"%s\"", $1);
1529 priqflags_list : priqflags_item { $$ |= $1; }
1530 | priqflags_list comma priqflags_item { $$ |= $3; }
1533 priqflags_item : STRING {
1534 if (!strcmp($1, "default"))
1535 $$ = PRCF_DEFAULTCLASS;
1536 else if (!strcmp($1, "red"))
1538 else if (!strcmp($1, "ecn"))
1539 $$ = PRCF_RED|PRCF_ECN;
1540 else if (!strcmp($1, "rio"))
1543 yyerror("unknown priq flag \"%s\"", $1);
1553 sizeof(struct node_hfsc_opts));
1560 hfscopts_list : hfscopts_item
1561 | hfscopts_list comma hfscopts_item
1564 hfscopts_item : LINKSHARE bandwidth {
1565 if (hfsc_opts.linkshare.used) {
1566 yyerror("linkshare already specified");
1569 hfsc_opts.linkshare.m2 = $2;
1570 hfsc_opts.linkshare.used = 1;
1572 | LINKSHARE '(' bandwidth comma number comma bandwidth ')'
1574 if (hfsc_opts.linkshare.used) {
1575 yyerror("linkshare already specified");
1578 hfsc_opts.linkshare.m1 = $3;
1579 hfsc_opts.linkshare.d = $5;
1580 hfsc_opts.linkshare.m2 = $7;
1581 hfsc_opts.linkshare.used = 1;
1583 | REALTIME bandwidth {
1584 if (hfsc_opts.realtime.used) {
1585 yyerror("realtime already specified");
1588 hfsc_opts.realtime.m2 = $2;
1589 hfsc_opts.realtime.used = 1;
1591 | REALTIME '(' bandwidth comma number comma bandwidth ')'
1593 if (hfsc_opts.realtime.used) {
1594 yyerror("realtime already specified");
1597 hfsc_opts.realtime.m1 = $3;
1598 hfsc_opts.realtime.d = $5;
1599 hfsc_opts.realtime.m2 = $7;
1600 hfsc_opts.realtime.used = 1;
1602 | UPPERLIMIT bandwidth {
1603 if (hfsc_opts.upperlimit.used) {
1604 yyerror("upperlimit already specified");
1607 hfsc_opts.upperlimit.m2 = $2;
1608 hfsc_opts.upperlimit.used = 1;
1610 | UPPERLIMIT '(' bandwidth comma number comma bandwidth ')'
1612 if (hfsc_opts.upperlimit.used) {
1613 yyerror("upperlimit already specified");
1616 hfsc_opts.upperlimit.m1 = $3;
1617 hfsc_opts.upperlimit.d = $5;
1618 hfsc_opts.upperlimit.m2 = $7;
1619 hfsc_opts.upperlimit.used = 1;
1622 if (!strcmp($1, "default"))
1623 hfsc_opts.flags |= HFCF_DEFAULTCLASS;
1624 else if (!strcmp($1, "red"))
1625 hfsc_opts.flags |= HFCF_RED;
1626 else if (!strcmp($1, "ecn"))
1627 hfsc_opts.flags |= HFCF_RED|HFCF_ECN;
1628 else if (!strcmp($1, "rio"))
1629 hfsc_opts.flags |= HFCF_RIO;
1631 yyerror("unknown hfsc flag \"%s\"", $1);
1641 sizeof(struct node_fairq_opts));
1648 fairqopts_list : fairqopts_item
1649 | fairqopts_list comma fairqopts_item
1652 fairqopts_item : LINKSHARE bandwidth {
1653 if (fairq_opts.linkshare.used) {
1654 yyerror("linkshare already specified");
1657 fairq_opts.linkshare.m2 = $2;
1658 fairq_opts.linkshare.used = 1;
1660 | LINKSHARE '(' bandwidth number bandwidth ')' {
1661 if (fairq_opts.linkshare.used) {
1662 yyerror("linkshare already specified");
1665 fairq_opts.linkshare.m1 = $3;
1666 fairq_opts.linkshare.d = $4;
1667 fairq_opts.linkshare.m2 = $5;
1668 fairq_opts.linkshare.used = 1;
1671 fairq_opts.hogs_bw = $2;
1674 fairq_opts.nbuckets = $2;
1677 if (!strcmp($1, "default"))
1678 fairq_opts.flags |= FARF_DEFAULTCLASS;
1679 else if (!strcmp($1, "red"))
1680 fairq_opts.flags |= FARF_RED;
1681 else if (!strcmp($1, "ecn"))
1682 fairq_opts.flags |= FARF_RED|FARF_ECN;
1683 else if (!strcmp($1, "rio"))
1684 fairq_opts.flags |= FARF_RIO;
1686 yyerror("unknown fairq flag \"%s\"", $1);
1694 qassign : /* empty */ { $$ = NULL; }
1695 | qassign_item { $$ = $1; }
1696 | '{' qassign_list '}' { $$ = $2; }
1699 qassign_list : qassign_item { $$ = $1; }
1700 | qassign_list comma qassign_item {
1701 $1->tail->next = $3;
1707 qassign_item : STRING {
1708 $$ = calloc(1, sizeof(struct node_queue));
1710 err(1, "qassign_item: calloc");
1711 if (strlcpy($$->queue, $1, sizeof($$->queue)) >=
1712 sizeof($$->queue)) {
1713 yyerror("queue name '%s' too long (max "
1714 "%d chars)", $1, sizeof($$->queue)-1);
1725 pfrule : action dir logquick interface route af proto fromto
1729 struct node_state_opt *o;
1730 struct node_proto *proto;
1736 if (check_rulestate(PFCTL_STATE_FILTER))
1739 memset(&r, 0, sizeof(r));
1743 case PFRULE_RETURNRST:
1744 r.rule_flag |= PFRULE_RETURNRST;
1745 r.return_ttl = $1.w;
1747 case PFRULE_RETURNICMP:
1748 r.rule_flag |= PFRULE_RETURNICMP;
1749 r.return_icmp = $1.w;
1750 r.return_icmp6 = $1.w2;
1753 r.rule_flag |= PFRULE_RETURN;
1754 r.return_icmp = $1.w;
1755 r.return_icmp6 = $1.w2;
1763 r.rtableid = $9.rtableid;
1767 if (strlcpy(r.tagname, $9.tag,
1768 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1769 yyerror("tag too long, max %u chars",
1770 PF_TAG_NAME_SIZE - 1);
1774 if (strlcpy(r.match_tagname, $9.match_tag,
1775 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1776 yyerror("tag too long, max %u chars",
1777 PF_TAG_NAME_SIZE - 1);
1780 r.match_tag_not = $9.match_tag_not;
1781 if (rule_label(&r, $9.label))
1784 r.flags = $9.flags.b1;
1785 r.flagset = $9.flags.b2;
1786 if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
1787 yyerror("flags always false");
1790 if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
1791 for (proto = $7; proto != NULL &&
1792 proto->proto != IPPROTO_TCP;
1793 proto = proto->next)
1795 if (proto == NULL && $7 != NULL) {
1796 if ($9.flags.b1 || $9.flags.b2)
1798 "flags only apply to tcp");
1801 "OS fingerprinting only "
1806 if (($9.flags.b1 & parse_flags("S")) == 0 &&
1808 yyerror("OS fingerprinting requires "
1809 "the SYN TCP flag (flags S/SA)");
1817 if (filter_opts.marker & FOM_KEEP) {
1818 r.keep_state = $9.keep.action;
1819 o = $9.keep.options;
1821 } else if (r.action == PF_PASS) {
1822 r.keep_state = default_keeppolicy_action;
1823 o = default_keeppolicy_options;
1830 struct node_state_opt *p = o;
1833 case PF_STATE_OPT_MAX:
1835 yyerror("state option 'max' "
1836 "multiple definitions");
1839 r.max_states = o->data.max_states;
1841 case PF_STATE_OPT_NOSYNC:
1842 if (r.rule_flag & PFRULE_NOSYNC) {
1843 yyerror("state option 'sync' "
1844 "multiple definitions");
1847 r.rule_flag |= PFRULE_NOSYNC;
1849 case PF_STATE_OPT_SRCTRACK:
1851 yyerror("state option "
1853 "multiple definitions");
1856 srctrack = o->data.src_track;
1857 r.rule_flag |= PFRULE_SRCTRACK;
1859 case PF_STATE_OPT_MAX_SRC_STATES:
1860 if (r.max_src_states) {
1861 yyerror("state option "
1863 "multiple definitions");
1866 if (o->data.max_src_states == 0) {
1867 yyerror("'max-src-states' must "
1872 o->data.max_src_states;
1873 r.rule_flag |= PFRULE_SRCTRACK;
1875 case PF_STATE_OPT_OVERLOAD:
1876 if (r.overload_tblname[0]) {
1877 yyerror("multiple 'overload' "
1878 "table definitions");
1881 if (strlcpy(r.overload_tblname,
1882 o->data.overload.tblname,
1883 PF_TABLE_NAME_SIZE) >=
1884 PF_TABLE_NAME_SIZE) {
1885 yyerror("state option: "
1889 r.flush = o->data.overload.flush;
1891 case PF_STATE_OPT_MAX_SRC_CONN:
1892 if (r.max_src_conn) {
1893 yyerror("state option "
1895 "multiple definitions");
1898 if (o->data.max_src_conn == 0) {
1899 yyerror("'max-src-conn' "
1904 o->data.max_src_conn;
1905 r.rule_flag |= PFRULE_SRCTRACK |
1906 PFRULE_RULESRCTRACK;
1908 case PF_STATE_OPT_MAX_SRC_CONN_RATE:
1909 if (r.max_src_conn_rate.limit) {
1910 yyerror("state option "
1911 "'max-src-conn-rate' "
1912 "multiple definitions");
1915 if (!o->data.max_src_conn_rate.limit ||
1916 !o->data.max_src_conn_rate.seconds) {
1917 yyerror("'max-src-conn-rate' "
1918 "values must be > 0");
1921 if (o->data.max_src_conn_rate.limit >
1923 yyerror("'max-src-conn-rate' "
1924 "maximum rate must be < %u",
1928 r.max_src_conn_rate.limit =
1929 o->data.max_src_conn_rate.limit;
1930 r.max_src_conn_rate.seconds =
1931 o->data.max_src_conn_rate.seconds;
1932 r.rule_flag |= PFRULE_SRCTRACK |
1933 PFRULE_RULESRCTRACK;
1935 case PF_STATE_OPT_MAX_SRC_NODES:
1936 if (r.max_src_nodes) {
1937 yyerror("state option "
1939 "multiple definitions");
1942 if (o->data.max_src_nodes == 0) {
1943 yyerror("'max-src-nodes' must "
1948 o->data.max_src_nodes;
1949 r.rule_flag |= PFRULE_SRCTRACK |
1950 PFRULE_RULESRCTRACK;
1952 case PF_STATE_OPT_STATELOCK:
1954 yyerror("state locking option: "
1955 "multiple definitions");
1959 r.rule_flag |= o->data.statelock;
1961 case PF_STATE_OPT_TIMEOUT:
1962 if (o->data.timeout.number ==
1963 PFTM_ADAPTIVE_START ||
1964 o->data.timeout.number ==
1967 if (r.timeout[o->data.timeout.number]) {
1968 yyerror("state timeout %s "
1969 "multiple definitions",
1970 pf_timeouts[o->data.
1971 timeout.number].name);
1974 r.timeout[o->data.timeout.number] =
1975 o->data.timeout.seconds;
1977 case PF_STATE_OPT_PICKUPS:
1978 r.pickup_mode = o->data.pickup_mode;
1987 /* 'flags S/SA' by default on stateful rules */
1988 if (!r.action && !r.flags && !r.flagset &&
1989 !$9.fragment && !($9.marker & FOM_FLAGS) &&
1991 r.flags = parse_flags("S");
1992 r.flagset = parse_flags("SA");
1995 if (!adaptive && r.max_states) {
1996 r.timeout[PFTM_ADAPTIVE_START] =
1997 (r.max_states / 10) * 6;
1998 r.timeout[PFTM_ADAPTIVE_END] =
1999 (r.max_states / 10) * 12;
2001 if (r.rule_flag & PFRULE_SRCTRACK) {
2002 if (srctrack == PF_SRCTRACK_GLOBAL &&
2004 yyerror("'max-src-nodes' is "
2005 "incompatible with "
2006 "'source-track global'");
2009 if (srctrack == PF_SRCTRACK_GLOBAL &&
2011 yyerror("'max-src-conn' is "
2012 "incompatible with "
2013 "'source-track global'");
2016 if (srctrack == PF_SRCTRACK_GLOBAL &&
2017 r.max_src_conn_rate.seconds) {
2018 yyerror("'max-src-conn-rate' is "
2019 "incompatible with "
2020 "'source-track global'");
2023 if (r.timeout[PFTM_SRC_NODE] <
2024 r.max_src_conn_rate.seconds)
2025 r.timeout[PFTM_SRC_NODE] =
2026 r.max_src_conn_rate.seconds;
2027 r.rule_flag |= PFRULE_SRCTRACK;
2028 if (srctrack == PF_SRCTRACK_RULE)
2029 r.rule_flag |= PFRULE_RULESRCTRACK;
2031 if (r.keep_state && !statelock)
2032 r.rule_flag |= default_statelock;
2035 r.rule_flag |= PFRULE_FRAGMENT;
2036 r.allow_opts = $9.allowopts;
2038 decide_address_family($8.src.host, &r.af);
2039 decide_address_family($8.dst.host, &r.af);
2043 yyerror("direction must be explicit "
2044 "with rules that specify routing");
2048 r.rpool.opts = $5.pool_opts;
2050 memcpy(&r.rpool.key, $5.key,
2051 sizeof(struct pf_poolhashkey));
2053 if (r.rt && r.rt != PF_FASTROUTE) {
2054 decide_address_family($5.host, &r.af);
2055 remove_invalid_hosts(&$5.host, &r.af);
2056 if ($5.host == NULL) {
2057 yyerror("no routing address with "
2058 "matching address family found.");
2061 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
2062 PF_POOL_NONE && ($5.host->next != NULL ||
2063 $5.host->addr.type == PF_ADDR_TABLE ||
2064 DYNIF_MULTIADDR($5.host->addr)))
2065 r.rpool.opts |= PF_POOL_ROUNDROBIN;
2066 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2067 PF_POOL_ROUNDROBIN &&
2068 disallow_table($5.host, "tables are only "
2069 "supported in round-robin routing pools"))
2071 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2072 PF_POOL_ROUNDROBIN &&
2073 disallow_alias($5.host, "interface (%s) "
2074 "is only supported in round-robin "
2077 if ($5.host->next != NULL) {
2078 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2079 PF_POOL_ROUNDROBIN) {
2080 yyerror("r.rpool.opts must "
2081 "be PF_POOL_ROUNDROBIN");
2086 if ($9.queues.qname != NULL) {
2087 if (strlcpy(r.qname, $9.queues.qname,
2088 sizeof(r.qname)) >= sizeof(r.qname)) {
2089 yyerror("rule qname too long (max "
2090 "%d chars)", sizeof(r.qname)-1);
2093 free($9.queues.qname);
2095 if ($9.queues.pqname != NULL) {
2096 if (strlcpy(r.pqname, $9.queues.pqname,
2097 sizeof(r.pqname)) >= sizeof(r.pqname)) {
2098 yyerror("rule pqname too long (max "
2099 "%d chars)", sizeof(r.pqname)-1);
2102 free($9.queues.pqname);
2105 expand_rule(&r, $4, $5.host, $7, $8.src_os,
2106 $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
2107 $9.uid, $9.gid, $9.icmpspec, "");
2112 bzero(&filter_opts, sizeof filter_opts);
2113 filter_opts.rtableid = -1;
2116 { $$ = filter_opts; }
2118 bzero(&filter_opts, sizeof filter_opts);
2119 filter_opts.rtableid = -1;
2124 filter_opts_l : filter_opts_l filter_opt
2128 filter_opt : USER uids {
2129 if (filter_opts.uid)
2130 $2->tail->next = filter_opts.uid;
2131 filter_opts.uid = $2;
2134 if (filter_opts.gid)
2135 $2->tail->next = filter_opts.gid;
2136 filter_opts.gid = $2;
2139 if (filter_opts.marker & FOM_FLAGS) {
2140 yyerror("flags cannot be redefined");
2143 filter_opts.marker |= FOM_FLAGS;
2144 filter_opts.flags.b1 |= $1.b1;
2145 filter_opts.flags.b2 |= $1.b2;
2146 filter_opts.flags.w |= $1.w;
2147 filter_opts.flags.w2 |= $1.w2;
2150 if (filter_opts.marker & FOM_ICMP) {
2151 yyerror("icmp-type cannot be redefined");
2154 filter_opts.marker |= FOM_ICMP;
2155 filter_opts.icmpspec = $1;
2158 if (filter_opts.marker & FOM_TOS) {
2159 yyerror("tos cannot be redefined");
2162 filter_opts.marker |= FOM_TOS;
2163 filter_opts.tos = $1;
2166 if (filter_opts.marker & FOM_KEEP) {
2167 yyerror("modulate or keep cannot be redefined");
2170 filter_opts.marker |= FOM_KEEP;
2171 filter_opts.keep.action = $1.action;
2172 filter_opts.keep.options = $1.options;
2175 filter_opts.fragment = 1;
2178 filter_opts.allowopts = 1;
2181 if (filter_opts.label) {
2182 yyerror("label cannot be redefined");
2185 filter_opts.label = $1;
2188 if (filter_opts.queues.qname) {
2189 yyerror("queue cannot be redefined");
2192 filter_opts.queues = $1;
2195 filter_opts.tag = $2;
2197 | not TAGGED string {
2198 filter_opts.match_tag = $3;
2199 filter_opts.match_tag_not = $1;
2201 | PROBABILITY STRING {
2203 double p = strtod($2, &e);
2210 yyerror("invalid probability: %s", $2);
2214 p = floor(p * (UINT_MAX+1.0) + 0.5);
2215 if (p < 1.0 || p >= (UINT_MAX+1.0)) {
2216 yyerror("invalid probability: %s", $2);
2220 filter_opts.prob = (u_int32_t)p;
2224 filter_opts.rtableid = $2;
2228 action : PASS { $$.b1 = PF_PASS; $$.b2 = $$.w = 0; }
2229 | BLOCK blockspec { $$ = $2; $$.b1 = PF_DROP; }
2232 blockspec : /* empty */ {
2233 $$.b2 = blockpolicy;
2234 $$.w = returnicmpdefault;
2235 $$.w2 = returnicmp6default;
2238 $$.b2 = PFRULE_DROP;
2243 $$.b2 = PFRULE_RETURNRST;
2247 | RETURNRST '(' TTL number ')' {
2249 yyerror("illegal ttl value %d", $4);
2252 $$.b2 = PFRULE_RETURNRST;
2257 $$.b2 = PFRULE_RETURNICMP;
2258 $$.w = returnicmpdefault;
2259 $$.w2 = returnicmp6default;
2262 $$.b2 = PFRULE_RETURNICMP;
2263 $$.w = returnicmpdefault;
2264 $$.w2 = returnicmp6default;
2266 | RETURNICMP '(' STRING ')' {
2267 $$.b2 = PFRULE_RETURNICMP;
2268 if (!($$.w = parseicmpspec($3, AF_INET))) {
2273 $$.w2 = returnicmp6default;
2275 | RETURNICMP6 '(' STRING ')' {
2276 $$.b2 = PFRULE_RETURNICMP;
2277 $$.w = returnicmpdefault;
2278 if (!($$.w2 = parseicmpspec($3, AF_INET6))) {
2284 | RETURNICMP '(' STRING comma STRING ')' {
2285 $$.b2 = PFRULE_RETURNICMP;
2286 if (!($$.w = parseicmpspec($3, AF_INET)) ||
2287 !($$.w2 = parseicmpspec($5, AF_INET6))) {
2296 $$.b2 = PFRULE_RETURN;
2297 $$.w = returnicmpdefault;
2298 $$.w2 = returnicmp6default;
2302 dir : /* empty */ { $$ = 0; }
2303 | IN { $$ = PF_IN; }
2304 | OUT { $$ = PF_OUT; }
2307 quick : /* empty */ { $$.quick = 0; }
2308 | QUICK { $$.quick = 1; }
2311 logquick : /* empty */ { $$.log = 0; $$.quick = 0; $$.logif = 0; }
2312 | log { $$ = $1; $$.quick = 0; }
2313 | QUICK { $$.quick = 1; $$.log = 0; $$.logif = 0; }
2314 | log QUICK { $$ = $1; $$.quick = 1; }
2315 | QUICK log { $$ = $2; $$.quick = 1; }
2318 log : LOG { $$.log = PF_LOG; $$.logif = 0; }
2319 | LOG '(' logopts ')' {
2320 $$.log = PF_LOG | $3.log;
2321 $$.logif = $3.logif;
2325 logopts : logopt { $$ = $1; }
2326 | logopts comma logopt {
2327 $$.log = $1.log | $3.log;
2328 $$.logif = $3.logif;
2330 $$.logif = $1.logif;
2334 logopt : ALL { $$.log = PF_LOG_ALL; $$.logif = 0; }
2335 | USER { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
2336 | GROUP { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
2342 if (strncmp($2, "pflog", 5)) {
2343 yyerror("%s: should be a pflog interface", $2);
2347 i = strtonum($2 + 5, 0, 255, &errstr);
2349 yyerror("%s: %s", $2, errstr);
2358 interface : /* empty */ { $$ = NULL; }
2359 | ON if_item_not { $$ = $2; }
2360 | ON '{' if_list '}' { $$ = $3; }
2363 if_list : if_item_not { $$ = $1; }
2364 | if_list comma if_item_not {
2365 $1->tail->next = $3;
2371 if_item_not : not if_item { $$ = $2; $$->not = $1; }
2375 struct node_host *n;
2377 $$ = calloc(1, sizeof(struct node_if));
2379 err(1, "if_item: calloc");
2380 if (strlcpy($$->ifname, $1, sizeof($$->ifname)) >=
2381 sizeof($$->ifname)) {
2384 yyerror("interface name too long");
2388 if ((n = ifa_exists($1)) != NULL)
2389 $$->ifa_flags = n->ifa_flags;
2398 af : /* empty */ { $$ = 0; }
2399 | INET { $$ = AF_INET; }
2400 | INET6 { $$ = AF_INET6; }
2403 proto : /* empty */ { $$ = NULL; }
2404 | PROTO proto_item { $$ = $2; }
2405 | PROTO '{' proto_list '}' { $$ = $3; }
2408 proto_list : proto_item { $$ = $1; }
2409 | proto_list comma proto_item {
2410 $1->tail->next = $3;
2416 proto_item : STRING {
2420 if (atoul($1, &ulval) == 0) {
2422 yyerror("protocol outside range");
2426 pr = (u_int8_t)ulval;
2430 p = getprotobyname($1);
2432 yyerror("unknown protocol %s", $1);
2440 yyerror("proto 0 cannot be used");
2443 $$ = calloc(1, sizeof(struct node_proto));
2445 err(1, "proto_item: calloc");
2466 os : /* empty */ { $$ = NULL; }
2467 | OS xos { $$ = $2; }
2468 | OS '{' os_list '}' { $$ = $3; }
2472 $$ = calloc(1, sizeof(struct node_os));
2474 err(1, "os: calloc");
2480 os_list : xos { $$ = $1; }
2481 | os_list comma xos {
2482 $1->tail->next = $3;
2488 from : /* empty */ {
2502 if (disallow_urpf_failed($2.host, "\"urpf-failed\" is "
2503 "not permitted in a destination address"))
2509 ipportspec : ipspec {
2513 | ipspec PORT portspec {
2523 ipspec : ANY { $$ = NULL; }
2524 | xhost { $$ = $1; }
2525 | '{' host_list '}' { $$ = $2; }
2528 host_list : ipspec { $$ = $1; }
2529 | host_list comma ipspec {
2532 else if ($1 == NULL)
2535 $1->tail->next = $3;
2536 $1->tail = $3->tail;
2543 struct node_host *n;
2545 for (n = $2; n != NULL; n = n->next)
2550 $$ = calloc(1, sizeof(struct node_host));
2552 err(1, "xhost: calloc");
2553 $$->addr.type = PF_ADDR_NOROUTE;
2559 $$ = calloc(1, sizeof(struct node_host));
2561 err(1, "xhost: calloc");
2562 $$->addr.type = PF_ADDR_URPFFAILED;
2570 if (($$ = host($1)) == NULL) {
2571 /* error. "any" is handled elsewhere */
2573 yyerror("could not parse host specification");
2579 | STRING '/' number {
2582 if (asprintf(&buf, "%s/%u", $1, $3) == -1)
2583 err(1, "host: asprintf");
2585 if (($$ = host(buf)) == NULL) {
2586 /* error. "any" is handled elsewhere */
2588 yyerror("could not parse host specification");
2594 | dynaddr '/' number {
2595 struct node_host *n;
2598 for (n = $1; n != NULL; n = n->next)
2602 if (strlen($2) >= PF_TABLE_NAME_SIZE) {
2603 yyerror("table name '%s' too long", $2);
2607 $$ = calloc(1, sizeof(struct node_host));
2609 err(1, "host: calloc");
2610 $$->addr.type = PF_ADDR_TABLE;
2611 if (strlcpy($$->addr.v.tblname, $2,
2612 sizeof($$->addr.v.tblname)) >=
2613 sizeof($$->addr.v.tblname))
2614 errx(1, "host: strlcpy");
2620 $$ = calloc(1, sizeof(struct node_host));
2623 err(1, "host: calloc");
2625 $$->addr.type = PF_ADDR_RTLABEL;
2626 if (strlcpy($$->addr.v.rtlabelname, $2,
2627 sizeof($$->addr.v.rtlabelname)) >=
2628 sizeof($$->addr.v.rtlabelname)) {
2629 yyerror("route label too long, max %u chars",
2630 sizeof($$->addr.v.rtlabelname) - 1);
2644 if (atoul($1, &ulval) == -1) {
2645 yyerror("%s is not a number", $1);
2654 dynaddr : '(' STRING ')' {
2659 if (!isalpha(op[0])) {
2660 yyerror("invalid interface name '%s'", op);
2664 while ((p = strrchr($2, ':')) != NULL) {
2665 if (!strcmp(p+1, "network"))
2666 flags |= PFI_AFLAG_NETWORK;
2667 else if (!strcmp(p+1, "broadcast"))
2668 flags |= PFI_AFLAG_BROADCAST;
2669 else if (!strcmp(p+1, "peer"))
2670 flags |= PFI_AFLAG_PEER;
2671 else if (!strcmp(p+1, "0"))
2672 flags |= PFI_AFLAG_NOALIAS;
2674 yyerror("interface %s has bad modifier",
2681 if (flags & (flags - 1) & PFI_AFLAG_MODEMASK) {
2683 yyerror("illegal combination of "
2684 "interface modifiers");
2687 $$ = calloc(1, sizeof(struct node_host));
2689 err(1, "address: calloc");
2691 set_ipmask($$, 128);
2692 $$->addr.type = PF_ADDR_DYNIFTL;
2693 $$->addr.iflags = flags;
2694 if (strlcpy($$->addr.v.ifname, $2,
2695 sizeof($$->addr.v.ifname)) >=
2696 sizeof($$->addr.v.ifname)) {
2699 yyerror("interface name too long");
2708 portspec : port_item { $$ = $1; }
2709 | '{' port_list '}' { $$ = $2; }
2712 port_list : port_item { $$ = $1; }
2713 | port_list comma port_item {
2714 $1->tail->next = $3;
2721 $$ = calloc(1, sizeof(struct node_port));
2723 err(1, "port_item: calloc");
2735 yyerror("':' cannot be used with an other "
2739 $$ = calloc(1, sizeof(struct node_port));
2741 err(1, "port_item: calloc");
2748 | port PORTBINARY port {
2750 yyerror("':' cannot be used with an other "
2754 $$ = calloc(1, sizeof(struct node_port));
2756 err(1, "port_item: calloc");
2766 char *p = strchr($1, ':');
2769 if (($$.a = getservice($1)) == -1) {
2778 if ((port[0] = getservice($1)) == -1 ||
2779 (port[1] = getservice(p)) == -1) {
2791 uids : uid_item { $$ = $1; }
2792 | '{' uid_list '}' { $$ = $2; }
2795 uid_list : uid_item { $$ = $1; }
2796 | uid_list comma uid_item {
2797 $1->tail->next = $3;
2804 $$ = calloc(1, sizeof(struct node_uid));
2806 err(1, "uid_item: calloc");
2814 if ($2 == UID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
2815 yyerror("user unknown requires operator = or "
2819 $$ = calloc(1, sizeof(struct node_uid));
2821 err(1, "uid_item: calloc");
2828 | uid PORTBINARY uid {
2829 if ($1 == UID_MAX || $3 == UID_MAX) {
2830 yyerror("user unknown requires operator = or "
2834 $$ = calloc(1, sizeof(struct node_uid));
2836 err(1, "uid_item: calloc");
2848 if (atoul($1, &ulval) == -1) {
2849 if (!strcmp($1, "unknown"))
2854 if ((pw = getpwnam($1)) == NULL) {
2855 yyerror("unknown user %s", $1);
2862 if (ulval >= UID_MAX) {
2864 yyerror("illegal uid value %lu", ulval);
2873 gids : gid_item { $$ = $1; }
2874 | '{' gid_list '}' { $$ = $2; }
2877 gid_list : gid_item { $$ = $1; }
2878 | gid_list comma gid_item {
2879 $1->tail->next = $3;
2886 $$ = calloc(1, sizeof(struct node_gid));
2888 err(1, "gid_item: calloc");
2896 if ($2 == GID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
2897 yyerror("group unknown requires operator = or "
2901 $$ = calloc(1, sizeof(struct node_gid));
2903 err(1, "gid_item: calloc");
2910 | gid PORTBINARY gid {
2911 if ($1 == GID_MAX || $3 == GID_MAX) {
2912 yyerror("group unknown requires operator = or "
2916 $$ = calloc(1, sizeof(struct node_gid));
2918 err(1, "gid_item: calloc");
2930 if (atoul($1, &ulval) == -1) {
2931 if (!strcmp($1, "unknown"))
2936 if ((grp = getgrnam($1)) == NULL) {
2937 yyerror("unknown group %s", $1);
2944 if (ulval >= GID_MAX) {
2945 yyerror("illegal gid value %lu", ulval);
2958 if ((f = parse_flags($1)) < 0) {
2959 yyerror("bad flags %s", $1);
2968 flags : FLAGS flag '/' flag { $$.b1 = $2.b1; $$.b2 = $4.b1; }
2969 | FLAGS '/' flag { $$.b1 = 0; $$.b2 = $3.b1; }
2970 | FLAGS ANY { $$.b1 = 0; $$.b2 = 0; }
2973 icmpspec : ICMPTYPE icmp_item { $$ = $2; }
2974 | ICMPTYPE '{' icmp_list '}' { $$ = $3; }
2975 | ICMP6TYPE icmp6_item { $$ = $2; }
2976 | ICMP6TYPE '{' icmp6_list '}' { $$ = $3; }
2979 icmp_list : icmp_item { $$ = $1; }
2980 | icmp_list comma icmp_item {
2981 $1->tail->next = $3;
2987 icmp6_list : icmp6_item { $$ = $1; }
2988 | icmp6_list comma icmp6_item {
2989 $1->tail->next = $3;
2995 icmp_item : icmptype {
2996 $$ = calloc(1, sizeof(struct node_icmp));
2998 err(1, "icmp_item: calloc");
3001 $$->proto = IPPROTO_ICMP;
3005 | icmptype CODE STRING {
3006 const struct icmpcodeent *p;
3009 if (atoul($3, &ulval) == 0) {
3012 yyerror("illegal icmp-code %lu", ulval);
3016 if ((p = geticmpcodebyname($1-1, $3,
3017 AF_INET)) == NULL) {
3018 yyerror("unknown icmp-code %s", $3);
3025 $$ = calloc(1, sizeof(struct node_icmp));
3027 err(1, "icmp_item: calloc");
3029 $$->code = ulval + 1;
3030 $$->proto = IPPROTO_ICMP;
3036 icmp6_item : icmp6type {
3037 $$ = calloc(1, sizeof(struct node_icmp));
3039 err(1, "icmp_item: calloc");
3042 $$->proto = IPPROTO_ICMPV6;
3046 | icmp6type CODE STRING {
3047 const struct icmpcodeent *p;
3050 if (atoul($3, &ulval) == 0) {
3052 yyerror("illegal icmp6-code %lu",
3058 if ((p = geticmpcodebyname($1-1, $3,
3059 AF_INET6)) == NULL) {
3060 yyerror("unknown icmp6-code %s", $3);
3067 $$ = calloc(1, sizeof(struct node_icmp));
3069 err(1, "icmp_item: calloc");
3071 $$->code = ulval + 1;
3072 $$->proto = IPPROTO_ICMPV6;
3079 const struct icmptypeent *p;
3082 if (atoul($1, &ulval) == 0) {
3084 yyerror("illegal icmp-type %lu", ulval);
3090 if ((p = geticmptypebyname($1, AF_INET)) ==
3092 yyerror("unknown icmp-type %s", $1);
3102 icmp6type : STRING {
3103 const struct icmptypeent *p;
3106 if (atoul($1, &ulval) == 0) {
3108 yyerror("illegal icmp6-type %lu",
3115 if ((p = geticmptypebyname($1, AF_INET6)) ==
3117 yyerror("unknown icmp6-type %s", $1);
3128 if (!strcmp($2, "lowdelay"))
3129 $$ = IPTOS_LOWDELAY;
3130 else if (!strcmp($2, "throughput"))
3131 $$ = IPTOS_THROUGHPUT;
3132 else if (!strcmp($2, "reliability"))
3133 $$ = IPTOS_RELIABILITY;
3134 else if ($2[0] == '0' && $2[1] == 'x')
3135 $$ = strtoul($2, NULL, 16);
3137 $$ = strtoul($2, NULL, 10);
3138 if (!$$ || $$ > 255) {
3139 yyerror("illegal tos value %s", $2);
3147 sourcetrack : SOURCETRACK { $$ = PF_SRCTRACK; }
3148 | SOURCETRACK GLOBAL { $$ = PF_SRCTRACK_GLOBAL; }
3149 | SOURCETRACK RULE { $$ = PF_SRCTRACK_RULE; }
3152 statelock : IFBOUND {
3153 $$ = PFRULE_IFBOUND;
3164 | KEEP STATE state_opt_spec {
3165 $$.action = PF_STATE_NORMAL;
3168 | MODULATE STATE state_opt_spec {
3169 $$.action = PF_STATE_MODULATE;
3172 | SYNPROXY STATE state_opt_spec {
3173 $$.action = PF_STATE_SYNPROXY;
3178 flush : /* empty */ { $$ = 0; }
3179 | FLUSH { $$ = PF_FLUSH; }
3181 $$ = PF_FLUSH | PF_FLUSH_GLOBAL;
3185 state_opt_spec : '(' state_opt_list ')' { $$ = $2; }
3186 | /* empty */ { $$ = NULL; }
3189 state_opt_list : state_opt_item { $$ = $1; }
3190 | state_opt_list comma state_opt_item {
3191 $1->tail->next = $3;
3197 state_opt_item : MAXIMUM number {
3198 $$ = calloc(1, sizeof(struct node_state_opt));
3200 err(1, "state_opt_item: calloc");
3201 $$->type = PF_STATE_OPT_MAX;
3202 $$->data.max_states = $2;
3207 $$ = calloc(1, sizeof(struct node_state_opt));
3209 err(1, "state_opt_item: calloc");
3210 $$->type = PF_STATE_OPT_NOSYNC;
3214 | MAXSRCSTATES number {
3215 $$ = calloc(1, sizeof(struct node_state_opt));
3217 err(1, "state_opt_item: calloc");
3218 $$->type = PF_STATE_OPT_MAX_SRC_STATES;
3219 $$->data.max_src_states = $2;
3223 | MAXSRCCONN number {
3224 $$ = calloc(1, sizeof(struct node_state_opt));
3226 err(1, "state_opt_item: calloc");
3227 $$->type = PF_STATE_OPT_MAX_SRC_CONN;
3228 $$->data.max_src_conn = $2;
3232 | MAXSRCCONNRATE number '/' number {
3233 $$ = calloc(1, sizeof(struct node_state_opt));
3235 err(1, "state_opt_item: calloc");
3236 $$->type = PF_STATE_OPT_MAX_SRC_CONN_RATE;
3237 $$->data.max_src_conn_rate.limit = $2;
3238 $$->data.max_src_conn_rate.seconds = $4;
3242 | OVERLOAD '<' STRING '>' flush {
3243 if (strlen($3) >= PF_TABLE_NAME_SIZE) {
3244 yyerror("table name '%s' too long", $3);
3248 $$ = calloc(1, sizeof(struct node_state_opt));
3250 err(1, "state_opt_item: calloc");
3251 if (strlcpy($$->data.overload.tblname, $3,
3252 PF_TABLE_NAME_SIZE) >= PF_TABLE_NAME_SIZE)
3253 errx(1, "state_opt_item: strlcpy");
3255 $$->type = PF_STATE_OPT_OVERLOAD;
3256 $$->data.overload.flush = $5;
3260 | MAXSRCNODES number {
3261 $$ = calloc(1, sizeof(struct node_state_opt));
3263 err(1, "state_opt_item: calloc");
3264 $$->type = PF_STATE_OPT_MAX_SRC_NODES;
3265 $$->data.max_src_nodes = $2;
3270 $$ = calloc(1, sizeof(struct node_state_opt));
3272 err(1, "state_opt_item: calloc");
3273 $$->type = PF_STATE_OPT_PICKUPS;
3274 $$->data.pickup_mode = PF_PICKUPS_ENABLED;
3279 $$ = calloc(1, sizeof(struct node_state_opt));
3281 err(1, "state_opt_item: calloc");
3282 $$->type = PF_STATE_OPT_PICKUPS;
3283 $$->data.pickup_mode = PF_PICKUPS_DISABLED;
3288 $$ = calloc(1, sizeof(struct node_state_opt));
3290 err(1, "state_opt_item: calloc");
3291 $$->type = PF_STATE_OPT_PICKUPS;
3292 $$->data.pickup_mode = PF_PICKUPS_HASHONLY;
3297 $$ = calloc(1, sizeof(struct node_state_opt));
3299 err(1, "state_opt_item: calloc");
3300 $$->type = PF_STATE_OPT_SRCTRACK;
3301 $$->data.src_track = $1;
3306 $$ = calloc(1, sizeof(struct node_state_opt));
3308 err(1, "state_opt_item: calloc");
3309 $$->type = PF_STATE_OPT_STATELOCK;
3310 $$->data.statelock = $1;
3317 for (i = 0; pf_timeouts[i].name &&
3318 strcmp(pf_timeouts[i].name, $1); ++i)
3320 if (!pf_timeouts[i].name) {
3321 yyerror("illegal timeout name %s", $1);
3325 if (strchr(pf_timeouts[i].name, '.') == NULL) {
3326 yyerror("illegal state timeout %s", $1);
3331 $$ = calloc(1, sizeof(struct node_state_opt));
3333 err(1, "state_opt_item: calloc");
3334 $$->type = PF_STATE_OPT_TIMEOUT;
3335 $$->data.timeout.number = pf_timeouts[i].timeout;
3336 $$->data.timeout.seconds = $2;
3342 label : LABEL STRING {
3347 qname : QUEUE STRING {
3350 | QUEUE '(' STRING ')' {
3353 | QUEUE '(' STRING comma STRING ')' {
3359 no : /* empty */ { $$ = 0; }
3364 char *p = strchr($1, ':');
3367 if (($$.a = getservice($1)) == -1) {
3372 } else if (!strcmp(p+1, "*")) {
3374 if (($$.a = getservice($1)) == -1) {
3382 if (($$.a = getservice($1)) == -1 ||
3383 ($$.b = getservice(p)) == -1) {
3395 redirspec : host { $$ = $1; }
3396 | '{' redir_host_list '}' { $$ = $2; }
3399 redir_host_list : host { $$ = $1; }
3400 | redir_host_list comma host {
3401 $1->tail->next = $3;
3402 $1->tail = $3->tail;
3407 redirpool : /* empty */ { $$ = NULL; }
3409 $$ = calloc(1, sizeof(struct redirection));
3411 err(1, "redirection: calloc");
3413 $$->rport.a = $$->rport.b = $$->rport.t = 0;
3415 | ARROW redirspec PORT rport {
3416 $$ = calloc(1, sizeof(struct redirection));
3418 err(1, "redirection: calloc");
3424 hashkey : /* empty */
3426 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3428 err(1, "hashkey: calloc");
3429 $$->key32[0] = arc4random();
3430 $$->key32[1] = arc4random();
3431 $$->key32[2] = arc4random();
3432 $$->key32[3] = arc4random();
3436 if (!strncmp($1, "0x", 2)) {
3437 if (strlen($1) != 34) {
3439 yyerror("hex key must be 128 bits "
3440 "(32 hex digits) long");
3443 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3445 err(1, "hashkey: calloc");
3447 if (sscanf($1, "0x%8x%8x%8x%8x",
3448 &$$->key32[0], &$$->key32[1],
3449 &$$->key32[2], &$$->key32[3]) != 4) {
3452 yyerror("invalid hex key");
3458 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3460 err(1, "hashkey: calloc");
3462 MD5Update(&context, (unsigned char *)$1,
3464 MD5Final((unsigned char *)$$, &context);
3465 $$->key32[0] = htonl($$->key32[0]);
3466 $$->key32[1] = htonl($$->key32[1]);
3467 $$->key32[2] = htonl($$->key32[2]);
3468 $$->key32[3] = htonl($$->key32[3]);
3474 pool_opts : { bzero(&pool_opts, sizeof pool_opts); }
3478 bzero(&pool_opts, sizeof pool_opts);
3483 pool_opts_l : pool_opts_l pool_opt
3487 pool_opt : BITMASK {
3488 if (pool_opts.type) {
3489 yyerror("pool type cannot be redefined");
3492 pool_opts.type = PF_POOL_BITMASK;
3495 if (pool_opts.type) {
3496 yyerror("pool type cannot be redefined");
3499 pool_opts.type = PF_POOL_RANDOM;
3501 | SOURCEHASH hashkey {
3502 if (pool_opts.type) {
3503 yyerror("pool type cannot be redefined");
3506 pool_opts.type = PF_POOL_SRCHASH;
3510 if (pool_opts.type) {
3511 yyerror("pool type cannot be redefined");
3514 pool_opts.type = PF_POOL_ROUNDROBIN;
3517 if (pool_opts.staticport) {
3518 yyerror("static-port cannot be redefined");
3521 pool_opts.staticport = 1;
3524 if (filter_opts.marker & POM_STICKYADDRESS) {
3525 yyerror("sticky-address cannot be redefined");
3528 pool_opts.marker |= POM_STICKYADDRESS;
3529 pool_opts.opts |= PF_POOL_STICKYADDR;
3533 redirection : /* empty */ { $$ = NULL; }
3535 $$ = calloc(1, sizeof(struct redirection));
3537 err(1, "redirection: calloc");
3539 $$->rport.a = $$->rport.b = $$->rport.t = 0;
3541 | ARROW host PORT rport {
3542 $$ = calloc(1, sizeof(struct redirection));
3544 err(1, "redirection: calloc");
3550 natpasslog : /* empty */ { $$.b1 = $$.b2 = 0; $$.w2 = 0; }
3551 | PASS { $$.b1 = 1; $$.b2 = 0; $$.w2 = 0; }
3552 | PASS log { $$.b1 = 1; $$.b2 = $2.log; $$.w2 = $2.logif; }
3553 | log { $$.b1 = 0; $$.b2 = $1.log; $$.w2 = $1.logif; }
3556 nataction : no NAT natpasslog {
3558 yyerror("\"pass\" not valid with \"no\"");
3569 | no RDR natpasslog {
3571 yyerror("\"pass\" not valid with \"no\"");
3584 natrule : nataction interface af proto fromto tag tagged rtable
3589 if (check_rulestate(PFCTL_STATE_NAT))
3592 memset(&r, 0, sizeof(r));
3601 if ($5.src.host && $5.src.host->af &&
3602 !$5.src.host->ifindex)
3603 r.af = $5.src.host->af;
3604 else if ($5.dst.host && $5.dst.host->af &&
3605 !$5.dst.host->ifindex)
3606 r.af = $5.dst.host->af;
3610 if (strlcpy(r.tagname, $6, PF_TAG_NAME_SIZE) >=
3612 yyerror("tag too long, max %u chars",
3613 PF_TAG_NAME_SIZE - 1);
3618 if (strlcpy(r.match_tagname, $7.name,
3619 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
3620 yyerror("tag too long, max %u chars",
3621 PF_TAG_NAME_SIZE - 1);
3624 r.match_tag_not = $7.neg;
3627 if (r.action == PF_NONAT || r.action == PF_NORDR) {
3629 yyerror("translation rule with 'no' "
3630 "does not need '->'");
3634 if ($9 == NULL || $9->host == NULL) {
3635 yyerror("translation rule requires '-> "
3639 if (!r.af && ! $9->host->ifindex)
3640 r.af = $9->host->af;
3642 remove_invalid_hosts(&$9->host, &r.af);
3643 if (invalid_redirect($9->host, r.af))
3645 if (check_netmask($9->host, r.af))
3648 r.rpool.proxy_port[0] = ntohs($9->rport.a);
3652 if (!$9->rport.b && $9->rport.t &&
3653 $5.dst.port != NULL) {
3654 r.rpool.proxy_port[1] =
3655 ntohs($9->rport.a) +
3657 $5.dst.port->port[1]) -
3659 $5.dst.port->port[0]));
3661 r.rpool.proxy_port[1] =
3665 r.rpool.proxy_port[1] =
3667 if (!r.rpool.proxy_port[0] &&
3668 !r.rpool.proxy_port[1]) {
3669 r.rpool.proxy_port[0] =
3670 PF_NAT_PROXY_PORT_LOW;
3671 r.rpool.proxy_port[1] =
3672 PF_NAT_PROXY_PORT_HIGH;
3673 } else if (!r.rpool.proxy_port[1])
3674 r.rpool.proxy_port[1] =
3675 r.rpool.proxy_port[0];
3681 r.rpool.opts = $10.type;
3682 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
3683 PF_POOL_NONE && ($9->host->next != NULL ||
3684 $9->host->addr.type == PF_ADDR_TABLE ||
3685 DYNIF_MULTIADDR($9->host->addr)))
3686 r.rpool.opts = PF_POOL_ROUNDROBIN;
3687 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
3688 PF_POOL_ROUNDROBIN &&
3689 disallow_table($9->host, "tables are only "
3690 "supported in round-robin redirection "
3693 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
3694 PF_POOL_ROUNDROBIN &&
3695 disallow_alias($9->host, "interface (%s) "
3696 "is only supported in round-robin "
3697 "redirection pools"))
3699 if ($9->host->next != NULL) {
3700 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
3701 PF_POOL_ROUNDROBIN) {
3702 yyerror("only round-robin "
3703 "valid for multiple "
3704 "redirection addresses");
3710 if ($10.key != NULL)
3711 memcpy(&r.rpool.key, $10.key,
3712 sizeof(struct pf_poolhashkey));
3715 r.rpool.opts |= $10.opts;
3717 if ($10.staticport) {
3718 if (r.action != PF_NAT) {
3719 yyerror("the 'static-port' option is "
3720 "only valid with nat rules");
3723 if (r.rpool.proxy_port[0] !=
3724 PF_NAT_PROXY_PORT_LOW &&
3725 r.rpool.proxy_port[1] !=
3726 PF_NAT_PROXY_PORT_HIGH) {
3727 yyerror("the 'static-port' option can't"
3728 " be used when specifying a port"
3732 r.rpool.proxy_port[0] = 0;
3733 r.rpool.proxy_port[1] = 0;
3736 expand_rule(&r, $2, $9 == NULL ? NULL : $9->host, $4,
3737 $5.src_os, $5.src.host, $5.src.port, $5.dst.host,
3738 $5.dst.port, 0, 0, 0, "");
3743 binatrule : no BINAT natpasslog interface af proto FROM host TO ipspec tag
3744 tagged rtable redirection
3746 struct pf_rule binat;
3747 struct pf_pooladdr *pa;
3749 if (check_rulestate(PFCTL_STATE_NAT))
3751 if (disallow_urpf_failed($10, "\"urpf-failed\" is not "
3752 "permitted as a binat destination"))
3755 memset(&binat, 0, sizeof(binat));
3758 yyerror("\"pass\" not valid with \"no\"");
3762 binat.action = PF_NOBINAT;
3764 binat.action = PF_BINAT;
3765 binat.natpass = $3.b1;
3767 binat.logif = $3.w2;
3769 if (!binat.af && $8 != NULL && $8->af)
3771 if (!binat.af && $10 != NULL && $10->af)
3774 if (!binat.af && $14 != NULL && $14->host)
3775 binat.af = $14->host->af;
3777 yyerror("address family (inet/inet6) "
3783 memcpy(binat.ifname, $4->ifname,
3784 sizeof(binat.ifname));
3785 binat.ifnot = $4->not;
3790 if (strlcpy(binat.tagname, $11,
3791 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
3792 yyerror("tag too long, max %u chars",
3793 PF_TAG_NAME_SIZE - 1);
3797 if (strlcpy(binat.match_tagname, $12.name,
3798 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
3799 yyerror("tag too long, max %u chars",
3800 PF_TAG_NAME_SIZE - 1);
3803 binat.match_tag_not = $12.neg;
3804 binat.rtableid = $13;
3807 binat.proto = $6->proto;
3811 if ($8 != NULL && disallow_table($8, "invalid use of "
3812 "table <%s> as the source address of a binat rule"))
3814 if ($8 != NULL && disallow_alias($8, "invalid use of "
3815 "interface (%s) as the source address of a binat "
3818 if ($14 != NULL && $14->host != NULL && disallow_table(
3819 $14->host, "invalid use of table <%s> as the "
3820 "redirect address of a binat rule"))
3822 if ($14 != NULL && $14->host != NULL && disallow_alias(
3823 $14->host, "invalid use of interface (%s) as the "
3824 "redirect address of a binat rule"))
3829 yyerror("multiple binat ip addresses");
3832 if ($8->addr.type == PF_ADDR_DYNIFTL)
3834 if ($8->af != binat.af) {
3835 yyerror("binat ip versions must match");
3838 if (check_netmask($8, binat.af))
3840 memcpy(&binat.src.addr, &$8->addr,
3841 sizeof(binat.src.addr));
3846 yyerror("multiple binat ip addresses");
3849 if ($10->af != binat.af && $10->af) {
3850 yyerror("binat ip versions must match");
3853 if (check_netmask($10, binat.af))
3855 memcpy(&binat.dst.addr, &$10->addr,
3856 sizeof(binat.dst.addr));
3857 binat.dst.neg = $10->not;
3861 if (binat.action == PF_NOBINAT) {
3863 yyerror("'no binat' rule does not need"
3868 if ($14 == NULL || $14->host == NULL) {
3869 yyerror("'binat' rule requires"
3874 remove_invalid_hosts(&$14->host, &binat.af);
3875 if (invalid_redirect($14->host, binat.af))
3877 if ($14->host->next != NULL) {
3878 yyerror("binat rule must redirect to "
3879 "a single address");
3882 if (check_netmask($14->host, binat.af))
3885 if (!PF_AZERO(&binat.src.addr.v.a.mask,
3887 !PF_AEQ(&binat.src.addr.v.a.mask,
3888 &$14->host->addr.v.a.mask, binat.af)) {
3889 yyerror("'binat' source mask and "
3890 "redirect mask must be the same");
3894 TAILQ_INIT(&binat.rpool.list);
3895 pa = calloc(1, sizeof(struct pf_pooladdr));
3897 err(1, "binat: calloc");
3898 pa->addr = $14->host->addr;
3900 TAILQ_INSERT_TAIL(&binat.rpool.list,
3906 pfctl_add_rule(pf, &binat, "");
3910 tag : /* empty */ { $$ = NULL; }
3911 | TAG STRING { $$ = $2; }
3914 tagged : /* empty */ { $$.neg = 0; $$.name = NULL; }
3915 | not TAGGED string { $$.neg = $1; $$.name = $3; }
3918 rtable : /* empty */ { $$ = -1; }
3924 route_host : STRING {
3925 $$ = calloc(1, sizeof(struct node_host));
3927 err(1, "route_host: calloc");
3929 set_ipmask($$, 128);
3933 | '(' STRING host ')' {
3939 route_host_list : route_host { $$ = $1; }
3940 | route_host_list comma route_host {
3943 if ($1->af != $3->af) {
3944 yyerror("all pool addresses must be in the "
3945 "same address family");
3948 $1->tail->next = $3;
3949 $1->tail = $3->tail;
3954 routespec : route_host { $$ = $1; }
3955 | '{' route_host_list '}' { $$ = $2; }
3958 route : /* empty */ {
3965 $$.rt = PF_FASTROUTE;
3968 | ROUTETO routespec pool_opts {
3971 $$.pool_opts = $3.type | $3.opts;
3975 | REPLYTO routespec pool_opts {
3978 $$.pool_opts = $3.type | $3.opts;
3982 | DUPTO routespec pool_opts {
3985 $$.pool_opts = $3.type | $3.opts;
3991 timeout_spec : STRING number
3993 if (check_rulestate(PFCTL_STATE_OPTION)) {
3997 if (pfctl_set_timeout(pf, $1, $2, 0) != 0) {
3998 yyerror("unknown timeout %s", $1);
4006 timeout_list : timeout_list comma timeout_spec
4010 limit_spec : STRING number
4012 if (check_rulestate(PFCTL_STATE_OPTION)) {
4016 if (pfctl_set_limit(pf, $1, $2) != 0) {
4017 yyerror("unable to set limit %s %u", $1, $2);
4025 limit_list : limit_list comma limit_spec
4033 yesno : NO { $$ = 0; }
4035 if (!strcmp($1, "yes"))
4038 yyerror("invalid value '%s', expected 'yes' "
4047 unaryop : '=' { $$ = PF_OP_EQ; }
4048 | '!' '=' { $$ = PF_OP_NE; }
4049 | '<' '=' { $$ = PF_OP_LE; }
4050 | '<' { $$ = PF_OP_LT; }
4051 | '>' '=' { $$ = PF_OP_GE; }
4052 | '>' { $$ = PF_OP_GT; }
4058 yyerror(const char *fmt, ...)
4064 fprintf(stderr, "%s:%d: ", infile, yylval.lineno);
4065 vfprintf(stderr, fmt, ap);
4066 fprintf(stderr, "\n");
4072 disallow_table(struct node_host *h, const char *fmt)
4074 for (; h != NULL; h = h->next)
4075 if (h->addr.type == PF_ADDR_TABLE) {
4076 yyerror(fmt, h->addr.v.tblname);
4083 disallow_urpf_failed(struct node_host *h, const char *fmt)
4085 for (; h != NULL; h = h->next)
4086 if (h->addr.type == PF_ADDR_URPFFAILED) {
4094 disallow_alias(struct node_host *h, const char *fmt)
4096 for (; h != NULL; h = h->next)
4097 if (DYNIF_MULTIADDR(h->addr)) {
4098 yyerror(fmt, h->addr.v.tblname);
4105 rule_consistent(struct pf_rule *r, int anchor_call __unused)
4109 switch (r->action) {
4114 problems = filter_consistent(r, anchor_call);
4118 problems = nat_consistent(r);
4122 problems = rdr_consistent(r);
4133 filter_consistent(struct pf_rule *r, int anchor_call __unused)
4137 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP &&
4138 (r->src.port_op || r->dst.port_op)) {
4139 yyerror("port only applies to tcp/udp");
4142 if (r->proto != IPPROTO_ICMP && r->proto != IPPROTO_ICMPV6 &&
4143 (r->type || r->code)) {
4144 yyerror("icmp-type/code only applies to icmp");
4147 if (!r->af && (r->type || r->code)) {
4148 yyerror("must indicate address family with icmp-type/code");
4151 if (r->overload_tblname[0] &&
4152 r->max_src_conn == 0 && r->max_src_conn_rate.seconds == 0) {
4153 yyerror("'overload' requires 'max-src-conn' "
4154 "or 'max-src-conn-rate'");
4157 if ((r->proto == IPPROTO_ICMP && r->af == AF_INET6) ||
4158 (r->proto == IPPROTO_ICMPV6 && r->af == AF_INET)) {
4159 yyerror("proto %s doesn't match address family %s",
4160 r->proto == IPPROTO_ICMP ? "icmp" : "icmp6",
4161 r->af == AF_INET ? "inet" : "inet6");
4164 if (r->allow_opts && r->action != PF_PASS) {
4165 yyerror("allow-opts can only be specified for pass rules");
4168 if (r->rule_flag & PFRULE_FRAGMENT && (r->src.port_op ||
4169 r->dst.port_op || r->flagset || r->type || r->code)) {
4170 yyerror("fragments can be filtered only on IP header fields");
4173 if (r->rule_flag & PFRULE_RETURNRST && r->proto != IPPROTO_TCP) {
4174 yyerror("return-rst can only be applied to TCP rules");
4177 if (r->max_src_nodes && !(r->rule_flag & PFRULE_RULESRCTRACK)) {
4178 yyerror("max-src-nodes requires 'source-track rule'");
4181 if (r->action == PF_DROP && r->keep_state) {
4182 yyerror("keep state on block rules doesn't make sense");
4189 nat_consistent(struct pf_rule *r __unused)
4191 return (0); /* yeah! */
4195 rdr_consistent(struct pf_rule *r)
4199 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP) {
4200 if (r->src.port_op) {
4201 yyerror("src port only applies to tcp/udp");
4204 if (r->dst.port_op) {
4205 yyerror("dst port only applies to tcp/udp");
4208 if (r->rpool.proxy_port[0]) {
4209 yyerror("rpool port only applies to tcp/udp");
4213 if (r->dst.port_op &&
4214 r->dst.port_op != PF_OP_EQ && r->dst.port_op != PF_OP_RRG) {
4215 yyerror("invalid port operator for rdr destination port");
4222 process_tabledef(char *name, struct table_opts *opts)
4224 struct pfr_buffer ab;
4225 struct node_tinit *ti;
4227 bzero(&ab, sizeof(ab));
4228 ab.pfrb_type = PFRB_ADDRS;
4229 SIMPLEQ_FOREACH(ti, &opts->init_nodes, entries) {
4231 if (pfr_buf_load(&ab, ti->file, 0, append_addr)) {
4233 yyerror("cannot load \"%s\": %s",
4234 ti->file, strerror(errno));
4236 yyerror("file \"%s\" contains bad data",
4241 if (append_addr_host(&ab, ti->host, 0, 0)) {
4242 yyerror("cannot create address buffer: %s",
4247 if (pf->opts & PF_OPT_VERBOSE)
4248 print_tabledef(name, opts->flags, opts->init_addr,
4250 if (!(pf->opts & PF_OPT_NOACTION) &&
4251 pfctl_define_table(name, opts->flags, opts->init_addr,
4252 pf->anchor->name, &ab, pf->anchor->ruleset.tticket)) {
4253 yyerror("cannot define table %s: %s", name,
4254 pfr_strerror(errno));
4270 /* macro gore, but you should've seen the prior indentation nightmare... */
4272 #define FREE_LIST(T,r) \
4275 while (node != NULL) { \
4277 node = node->next; \
4282 #define LOOP_THROUGH(T,n,r,C) \
4286 r = calloc(1, sizeof(T)); \
4288 err(1, "LOOP: calloc"); \
4292 while (n != NULL) { \
4301 expand_label_str(char *label, size_t len, const char *srch, const char *repl)
4306 if ((tmp = calloc(1, len)) == NULL)
4307 err(1, "expand_label_str: calloc");
4309 while ((q = strstr(p, srch)) != NULL) {
4311 if ((strlcat(tmp, p, len) >= len) ||
4312 (strlcat(tmp, repl, len) >= len))
4313 errx(1, "expand_label: label too long");
4317 if (strlcat(tmp, p, len) >= len)
4318 errx(1, "expand_label: label too long");
4319 strlcpy(label, tmp, len); /* always fits */
4324 expand_label_if(const char *name, char *label, size_t len, const char *ifname)
4326 if (strstr(label, name) != NULL) {
4328 expand_label_str(label, len, name, "any");
4330 expand_label_str(label, len, name, ifname);
4335 expand_label_addr(const char *name, char *label, size_t len, sa_family_t af,
4336 struct node_host *h)
4338 char tmp[64], tmp_not[66];
4340 if (strstr(label, name) != NULL) {
4341 switch (h->addr.type) {
4342 case PF_ADDR_DYNIFTL:
4343 snprintf(tmp, sizeof(tmp), "(%s)", h->addr.v.ifname);
4346 snprintf(tmp, sizeof(tmp), "<%s>", h->addr.v.tblname);
4348 case PF_ADDR_NOROUTE:
4349 snprintf(tmp, sizeof(tmp), "no-route");
4351 case PF_ADDR_URPFFAILED:
4352 snprintf(tmp, sizeof(tmp), "urpf-failed");
4354 case PF_ADDR_ADDRMASK:
4355 if (!af || (PF_AZERO(&h->addr.v.a.addr, af) &&
4356 PF_AZERO(&h->addr.v.a.mask, af)))
4357 snprintf(tmp, sizeof(tmp), "any");
4362 if (inet_ntop(af, &h->addr.v.a.addr, a,
4364 snprintf(tmp, sizeof(tmp), "?");
4366 bits = unmask(&h->addr.v.a.mask, af);
4367 if ((af == AF_INET && bits < 32) ||
4368 (af == AF_INET6 && bits < 128))
4369 snprintf(tmp, sizeof(tmp),
4372 snprintf(tmp, sizeof(tmp),
4378 snprintf(tmp, sizeof(tmp), "?");
4383 snprintf(tmp_not, sizeof(tmp_not), "! %s", tmp);
4384 expand_label_str(label, len, name, tmp_not);
4386 expand_label_str(label, len, name, tmp);
4391 expand_label_port(const char *name, char *label, size_t len,
4392 struct node_port *port)
4394 char a1[6], a2[6], op[13] = "";
4396 if (strstr(label, name) != NULL) {
4397 snprintf(a1, sizeof(a1), "%u", ntohs(port->port[0]));
4398 snprintf(a2, sizeof(a2), "%u", ntohs(port->port[1]));
4401 else if (port->op == PF_OP_IRG)
4402 snprintf(op, sizeof(op), "%s><%s", a1, a2);
4403 else if (port->op == PF_OP_XRG)
4404 snprintf(op, sizeof(op), "%s<>%s", a1, a2);
4405 else if (port->op == PF_OP_EQ)
4406 snprintf(op, sizeof(op), "%s", a1);
4407 else if (port->op == PF_OP_NE)
4408 snprintf(op, sizeof(op), "!=%s", a1);
4409 else if (port->op == PF_OP_LT)
4410 snprintf(op, sizeof(op), "<%s", a1);
4411 else if (port->op == PF_OP_LE)
4412 snprintf(op, sizeof(op), "<=%s", a1);
4413 else if (port->op == PF_OP_GT)
4414 snprintf(op, sizeof(op), ">%s", a1);
4415 else if (port->op == PF_OP_GE)
4416 snprintf(op, sizeof(op), ">=%s", a1);
4417 expand_label_str(label, len, name, op);
4422 expand_label_proto(const char *name, char *label, size_t len, u_int8_t proto)
4424 struct protoent *pe;
4427 if (strstr(label, name) != NULL) {
4428 pe = getprotobynumber(proto);
4430 expand_label_str(label, len, name, pe->p_name);
4432 snprintf(n, sizeof(n), "%u", proto);
4433 expand_label_str(label, len, name, n);
4439 expand_label_nr(const char *name, char *label, size_t len)
4443 if (strstr(label, name) != NULL) {
4444 snprintf(n, sizeof(n), "%u", pf->anchor->match);
4445 expand_label_str(label, len, name, n);
4450 expand_label(char *label, size_t len, const char *ifname, sa_family_t af,
4451 struct node_host *src_host, struct node_port *src_port,
4452 struct node_host *dst_host, struct node_port *dst_port,
4455 expand_label_if("$if", label, len, ifname);
4456 expand_label_addr("$srcaddr", label, len, af, src_host);
4457 expand_label_addr("$dstaddr", label, len, af, dst_host);
4458 expand_label_port("$srcport", label, len, src_port);
4459 expand_label_port("$dstport", label, len, dst_port);
4460 expand_label_proto("$proto", label, len, proto);
4461 expand_label_nr("$nr", label, len);
4465 expand_altq(struct pf_altq *a, struct node_if *interfaces,
4466 struct node_queue *nqueues, struct node_queue_bw bwspec,
4467 struct node_queue_opt *opts)
4469 struct pf_altq pa, pb;
4470 char qname[PF_QNAME_SIZE];
4471 struct node_queue *n;
4472 struct node_queue_bw bw;
4475 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
4476 FREE_LIST(struct node_if, interfaces);
4477 FREE_LIST(struct node_queue, nqueues);
4481 LOOP_THROUGH(struct node_if, interface, interfaces,
4482 memcpy(&pa, a, sizeof(struct pf_altq));
4483 if (strlcpy(pa.ifname, interface->ifname,
4484 sizeof(pa.ifname)) >= sizeof(pa.ifname))
4485 errx(1, "expand_altq: strlcpy");
4487 if (interface->not) {
4488 yyerror("altq on ! <interface> is not supported");
4491 if (eval_pfaltq(pf, &pa, &bwspec, opts))
4494 if (pfctl_add_altq(pf, &pa))
4497 if (pf->opts & PF_OPT_VERBOSE) {
4498 print_altq(&pf->paltq->altq, 0,
4500 if (nqueues && nqueues->tail) {
4502 LOOP_THROUGH(struct node_queue, queue,
4512 if (pa.scheduler == ALTQT_CBQ ||
4513 pa.scheduler == ALTQT_HFSC) {
4514 /* now create a root queue */
4515 memset(&pb, 0, sizeof(struct pf_altq));
4516 if (strlcpy(qname, "root_", sizeof(qname)) >=
4518 errx(1, "expand_altq: strlcpy");
4519 if (strlcat(qname, interface->ifname,
4520 sizeof(qname)) >= sizeof(qname))
4521 errx(1, "expand_altq: strlcat");
4522 if (strlcpy(pb.qname, qname,
4523 sizeof(pb.qname)) >= sizeof(pb.qname))
4524 errx(1, "expand_altq: strlcpy");
4525 if (strlcpy(pb.ifname, interface->ifname,
4526 sizeof(pb.ifname)) >= sizeof(pb.ifname))
4527 errx(1, "expand_altq: strlcpy");
4528 pb.qlimit = pa.qlimit;
4529 pb.scheduler = pa.scheduler;
4530 bw.bw_absolute = pa.ifbandwidth;
4532 if (eval_pfqueue(pf, &pb, &bw, opts))
4535 if (pfctl_add_altq(pf, &pb))
4539 LOOP_THROUGH(struct node_queue, queue, nqueues,
4540 n = calloc(1, sizeof(struct node_queue));
4542 err(1, "expand_altq: calloc");
4543 if (pa.scheduler == ALTQT_CBQ ||
4544 pa.scheduler == ALTQT_HFSC /*||
4545 pa.scheduler == ALTQT_FAIRQ*/)
4546 if (strlcpy(n->parent, qname,
4547 sizeof(n->parent)) >=
4549 errx(1, "expand_altq: strlcpy");
4550 if (strlcpy(n->queue, queue->queue,
4551 sizeof(n->queue)) >= sizeof(n->queue))
4552 errx(1, "expand_altq: strlcpy");
4553 if (strlcpy(n->ifname, interface->ifname,
4554 sizeof(n->ifname)) >= sizeof(n->ifname))
4555 errx(1, "expand_altq: strlcpy");
4556 n->scheduler = pa.scheduler;
4562 queues->tail->next = n;
4568 FREE_LIST(struct node_if, interfaces);
4569 FREE_LIST(struct node_queue, nqueues);
4575 expand_queue(struct pf_altq *a, struct node_if *interfaces,
4576 struct node_queue *nqueues, struct node_queue_bw bwspec,
4577 struct node_queue_opt *opts)
4579 struct node_queue *n, *nq;
4584 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
4585 FREE_LIST(struct node_queue, nqueues);
4589 if (queues == NULL) {
4590 yyerror("queue %s has no parent", a->qname);
4591 FREE_LIST(struct node_queue, nqueues);
4595 LOOP_THROUGH(struct node_if, interface, interfaces,
4596 LOOP_THROUGH(struct node_queue, tqueue, queues,
4597 if (!strncmp(a->qname, tqueue->queue, PF_QNAME_SIZE) &&
4598 (interface->ifname[0] == 0 ||
4599 (!interface->not && !strncmp(interface->ifname,
4600 tqueue->ifname, IFNAMSIZ)) ||
4601 (interface->not && strncmp(interface->ifname,
4602 tqueue->ifname, IFNAMSIZ)))) {
4603 /* found ourself in queues */
4606 memcpy(&pa, a, sizeof(struct pf_altq));
4608 if (pa.scheduler != ALTQT_NONE &&
4609 pa.scheduler != tqueue->scheduler) {
4610 yyerror("exactly one scheduler type "
4611 "per interface allowed");
4614 pa.scheduler = tqueue->scheduler;
4616 /* scheduler dependent error checking */
4617 switch (pa.scheduler) {
4619 if (nqueues != NULL) {
4620 yyerror("priq queues cannot "
4621 "have child queues");
4624 if (bwspec.bw_absolute > 0 ||
4625 bwspec.bw_percent < 100) {
4626 yyerror("priq doesn't take "
4635 if (strlcpy(pa.ifname, tqueue->ifname,
4636 sizeof(pa.ifname)) >= sizeof(pa.ifname))
4637 errx(1, "expand_queue: strlcpy");
4638 if (strlcpy(pa.parent, tqueue->parent,
4639 sizeof(pa.parent)) >= sizeof(pa.parent))
4640 errx(1, "expand_queue: strlcpy");
4642 if (eval_pfqueue(pf, &pa, &bwspec, opts))
4645 if (pfctl_add_altq(pf, &pa))
4648 for (nq = nqueues; nq != NULL; nq = nq->next) {
4649 if (!strcmp(a->qname, nq->queue)) {
4650 yyerror("queue cannot have "
4656 sizeof(struct node_queue));
4658 err(1, "expand_queue: calloc");
4659 if (strlcpy(n->parent, a->qname,
4660 sizeof(n->parent)) >=
4662 errx(1, "expand_queue strlcpy");
4663 if (strlcpy(n->queue, nq->queue,
4664 sizeof(n->queue)) >=
4666 errx(1, "expand_queue strlcpy");
4667 if (strlcpy(n->ifname, tqueue->ifname,
4668 sizeof(n->ifname)) >=
4670 errx(1, "expand_queue strlcpy");
4671 n->scheduler = tqueue->scheduler;
4677 queues->tail->next = n;
4681 if ((pf->opts & PF_OPT_VERBOSE) && (
4682 (found == 1 && interface->ifname[0] == 0) ||
4683 (found > 0 && interface->ifname[0] != 0))) {
4684 print_queue(&pf->paltq->altq, 0,
4685 &bwspec, interface->ifname[0] != 0,
4687 if (nqueues && nqueues->tail) {
4689 LOOP_THROUGH(struct node_queue,
4702 FREE_LIST(struct node_queue, nqueues);
4703 FREE_LIST(struct node_if, interfaces);
4706 yyerror("queue %s has no parent", a->qname);
4717 expand_rule(struct pf_rule *r,
4718 struct node_if *interfaces, struct node_host *rpool_hosts,
4719 struct node_proto *protos, struct node_os *src_oses,
4720 struct node_host *src_hosts, struct node_port *src_ports,
4721 struct node_host *dst_hosts, struct node_port *dst_ports,
4722 struct node_uid *uids, struct node_gid *gids, struct node_icmp *icmp_types,
4723 const char *anchor_call)
4725 sa_family_t af = r->af;
4726 int added = 0, error = 0;
4727 char ifname[IF_NAMESIZE];
4728 char label[PF_RULE_LABEL_SIZE];
4729 char tagname[PF_TAG_NAME_SIZE];
4730 char match_tagname[PF_TAG_NAME_SIZE];
4731 struct pf_pooladdr *pa;
4732 struct node_host *h;
4733 u_int8_t flags, flagset, keep_state;
4735 if (strlcpy(label, r->label, sizeof(label)) >= sizeof(label))
4736 errx(1, "expand_rule: strlcpy");
4737 if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname))
4738 errx(1, "expand_rule: strlcpy");
4739 if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >=
4740 sizeof(match_tagname))
4741 errx(1, "expand_rule: strlcpy");
4743 flagset = r->flagset;
4744 keep_state = r->keep_state;
4746 LOOP_THROUGH(struct node_if, interface, interfaces,
4747 LOOP_THROUGH(struct node_proto, proto, protos,
4748 LOOP_THROUGH(struct node_icmp, icmp_type, icmp_types,
4749 LOOP_THROUGH(struct node_host, src_host, src_hosts,
4750 LOOP_THROUGH(struct node_port, src_port, src_ports,
4751 LOOP_THROUGH(struct node_os, src_os, src_oses,
4752 LOOP_THROUGH(struct node_host, dst_host, dst_hosts,
4753 LOOP_THROUGH(struct node_port, dst_port, dst_ports,
4754 LOOP_THROUGH(struct node_uid, uid, uids,
4755 LOOP_THROUGH(struct node_gid, gid, gids,
4758 /* for link-local IPv6 address, interface must match up */
4759 if ((r->af && src_host->af && r->af != src_host->af) ||
4760 (r->af && dst_host->af && r->af != dst_host->af) ||
4761 (src_host->af && dst_host->af &&
4762 src_host->af != dst_host->af) ||
4763 (src_host->ifindex && dst_host->ifindex &&
4764 src_host->ifindex != dst_host->ifindex) ||
4765 (src_host->ifindex && *interface->ifname &&
4766 src_host->ifindex != if_nametoindex(interface->ifname)) ||
4767 (dst_host->ifindex && *interface->ifname &&
4768 dst_host->ifindex != if_nametoindex(interface->ifname)))
4770 if (!r->af && src_host->af)
4771 r->af = src_host->af;
4772 else if (!r->af && dst_host->af)
4773 r->af = dst_host->af;
4775 if (*interface->ifname)
4776 strlcpy(r->ifname, interface->ifname,
4778 else if (if_indextoname(src_host->ifindex, ifname))
4779 strlcpy(r->ifname, ifname, sizeof(r->ifname));
4780 else if (if_indextoname(dst_host->ifindex, ifname))
4781 strlcpy(r->ifname, ifname, sizeof(r->ifname));
4783 memset(r->ifname, '\0', sizeof(r->ifname));
4785 if (strlcpy(r->label, label, sizeof(r->label)) >=
4787 errx(1, "expand_rule: strlcpy");
4788 if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >=
4790 errx(1, "expand_rule: strlcpy");
4791 if (strlcpy(r->match_tagname, match_tagname,
4792 sizeof(r->match_tagname)) >= sizeof(r->match_tagname))
4793 errx(1, "expand_rule: strlcpy");
4794 expand_label(r->label, PF_RULE_LABEL_SIZE, r->ifname, r->af,
4795 src_host, src_port, dst_host, dst_port, proto->proto);
4796 expand_label(r->tagname, PF_TAG_NAME_SIZE, r->ifname, r->af,
4797 src_host, src_port, dst_host, dst_port, proto->proto);
4798 expand_label(r->match_tagname, PF_TAG_NAME_SIZE, r->ifname,
4799 r->af, src_host, src_port, dst_host, dst_port,
4802 error += check_netmask(src_host, r->af);
4803 error += check_netmask(dst_host, r->af);
4805 r->ifnot = interface->not;
4806 r->proto = proto->proto;
4807 r->src.addr = src_host->addr;
4808 r->src.neg = src_host->not;
4809 r->src.port[0] = src_port->port[0];
4810 r->src.port[1] = src_port->port[1];
4811 r->src.port_op = src_port->op;
4812 r->dst.addr = dst_host->addr;
4813 r->dst.neg = dst_host->not;
4814 r->dst.port[0] = dst_port->port[0];
4815 r->dst.port[1] = dst_port->port[1];
4816 r->dst.port_op = dst_port->op;
4817 r->uid.op = uid->op;
4818 r->uid.uid[0] = uid->uid[0];
4819 r->uid.uid[1] = uid->uid[1];
4820 r->gid.op = gid->op;
4821 r->gid.gid[0] = gid->gid[0];
4822 r->gid.gid[1] = gid->gid[1];
4823 r->type = icmp_type->type;
4824 r->code = icmp_type->code;
4826 if ((keep_state == PF_STATE_MODULATE ||
4827 keep_state == PF_STATE_SYNPROXY) &&
4828 r->proto && r->proto != IPPROTO_TCP)
4829 r->keep_state = PF_STATE_NORMAL;
4831 r->keep_state = keep_state;
4833 if (r->proto && r->proto != IPPROTO_TCP) {
4838 r->flagset = flagset;
4840 if (icmp_type->proto && r->proto != icmp_type->proto) {
4841 yyerror("icmp-type mismatch");
4845 if (src_os && src_os->os) {
4846 r->os_fingerprint = pfctl_get_fingerprint(src_os->os);
4847 if ((pf->opts & PF_OPT_VERBOSE2) &&
4848 r->os_fingerprint == PF_OSFP_NOMATCH)
4850 "warning: unknown '%s' OS fingerprint\n",
4853 r->os_fingerprint = PF_OSFP_ANY;
4856 TAILQ_INIT(&r->rpool.list);
4857 for (h = rpool_hosts; h != NULL; h = h->next) {
4858 pa = calloc(1, sizeof(struct pf_pooladdr));
4860 err(1, "expand_rule: calloc");
4862 if (h->ifname != NULL) {
4863 if (strlcpy(pa->ifname, h->ifname,
4864 sizeof(pa->ifname)) >=
4866 errx(1, "expand_rule: strlcpy");
4869 TAILQ_INSERT_TAIL(&r->rpool.list, pa, entries);
4872 if (rule_consistent(r, anchor_call[0]) < 0 || error)
4873 yyerror("skipping rule due to errors");
4875 r->nr = pf->astack[pf->asd]->match++;
4876 pfctl_add_rule(pf, r, anchor_call);
4882 FREE_LIST(struct node_if, interfaces);
4883 FREE_LIST(struct node_proto, protos);
4884 FREE_LIST(struct node_host, src_hosts);
4885 FREE_LIST(struct node_port, src_ports);
4886 FREE_LIST(struct node_os, src_oses);
4887 FREE_LIST(struct node_host, dst_hosts);
4888 FREE_LIST(struct node_port, dst_ports);
4889 FREE_LIST(struct node_uid, uids);
4890 FREE_LIST(struct node_gid, gids);
4891 FREE_LIST(struct node_icmp, icmp_types);
4892 FREE_LIST(struct node_host, rpool_hosts);
4895 yyerror("rule expands to no valid combination");
4899 expand_skip_interface(struct node_if *interfaces)
4903 if (!interfaces || (!interfaces->next && !interfaces->not &&
4904 !strcmp(interfaces->ifname, "none"))) {
4905 if (pf->opts & PF_OPT_VERBOSE)
4906 printf("set skip on none\n");
4907 errs = pfctl_set_interface_flags(pf, __DECONST(char *, ""), PFI_IFLAG_SKIP, 0);
4911 if (pf->opts & PF_OPT_VERBOSE)
4912 printf("set skip on {");
4913 LOOP_THROUGH(struct node_if, interface, interfaces,
4914 if (pf->opts & PF_OPT_VERBOSE)
4915 printf(" %s", interface->ifname);
4916 if (interface->not) {
4917 yyerror("skip on ! <interface> is not supported");
4920 errs += pfctl_set_interface_flags(pf,
4921 interface->ifname, PFI_IFLAG_SKIP, 1);
4923 if (pf->opts & PF_OPT_VERBOSE)
4926 FREE_LIST(struct node_if, interfaces);
4938 check_rulestate(int desired_state)
4940 if (require_order && (rulestate > desired_state)) {
4941 yyerror("Rules must be in order: options, normalization, "
4942 "queueing, translation, filtering");
4945 rulestate = desired_state;
4950 kw_cmp(const void *k, const void *e)
4952 return (strcmp(k, ((const struct keywords *)e)->k_name));
4958 /* this has to be sorted always */
4959 static const struct keywords keywords[] = {
4961 { "allow-opts", ALLOWOPTS},
4963 { "anchor", ANCHOR},
4964 { "antispoof", ANTISPOOF},
4966 { "bandwidth", BANDWIDTH},
4968 { "binat-anchor", BINATANCHOR},
4969 { "bitmask", BITMASK},
4971 { "block-policy", BLOCKPOLICY},
4972 { "buckets", BUCKETS},
4975 { "crop", FRAGCROP},
4978 { "drop-ovl", FRAGDROP},
4981 { "fastroute", FASTROUTE},
4982 { "file", FILENAME},
4983 { "fingerprints", FINGERPRINTS},
4985 { "floating", FLOATING},
4988 { "fragment", FRAGMENT},
4990 { "global", GLOBAL},
4992 { "hash-only", HASHONLY},
4995 { "hostid", HOSTID},
4996 { "icmp-type", ICMPTYPE},
4997 { "icmp6-type", ICMP6TYPE},
4998 { "if-bound", IFBOUND},
5003 { "keep-policy", KEEPPOLICY},
5006 { "linkshare", LINKSHARE},
5009 { "loginterface", LOGINTERFACE},
5011 { "max-mss", MAXMSS},
5012 { "max-src-conn", MAXSRCCONN},
5013 { "max-src-conn-rate", MAXSRCCONNRATE},
5014 { "max-src-nodes", MAXSRCNODES},
5015 { "max-src-states", MAXSRCSTATES},
5016 { "min-ttl", MINTTL},
5017 { "modulate", MODULATE},
5019 { "nat-anchor", NATANCHOR},
5022 { "no-pickups", NOPICKUPS},
5023 { "no-route", NOROUTE},
5024 { "no-sync", NOSYNC},
5026 { "optimization", OPTIMIZATION},
5029 { "overload", OVERLOAD},
5031 { "pickups", PICKUPS},
5033 { "priority", PRIORITY},
5035 { "probability", PROBABILITY},
5037 { "qlimit", QLIMIT},
5040 { "random", RANDOM},
5041 { "random-id", RANDOMID},
5043 { "rdr-anchor", RDRANCHOR},
5044 { "realtime", REALTIME},
5045 { "reassemble", REASSEMBLE},
5046 { "reply-to", REPLYTO},
5047 { "require-order", REQUIREORDER},
5048 { "return", RETURN},
5049 { "return-icmp", RETURNICMP},
5050 { "return-icmp6", RETURNICMP6},
5051 { "return-rst", RETURNRST},
5052 { "round-robin", ROUNDROBIN},
5054 { "route-to", ROUTETO},
5055 { "rtable", RTABLE},
5057 { "ruleset-optimization", RULESET_OPTIMIZATION},
5061 { "source-hash", SOURCEHASH},
5062 { "source-track", SOURCETRACK},
5064 { "state-policy", STATEPOLICY},
5065 { "static-port", STATICPORT},
5066 { "sticky-address", STICKYADDRESS},
5067 { "synproxy", SYNPROXY},
5070 { "tagged", TAGGED},
5071 { "tbrsize", TBRSIZE},
5072 { "timeout", TIMEOUT},
5076 { "upperlimit", UPPERLIMIT},
5077 { "urpf-failed", URPFFAILED},
5080 const struct keywords *p;
5082 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
5083 sizeof(keywords[0]), kw_cmp);
5087 fprintf(stderr, "%s: %d\n", s, p->k_val);
5091 fprintf(stderr, "string: %s\n", s);
5096 #define MAXPUSHBACK 128
5100 char pushback_buffer[MAXPUSHBACK];
5101 int pushback_index = 0;
5109 /* Read character from the parsebuffer instead of input. */
5110 if (parseindex >= 0) {
5111 c = parsebuf[parseindex++];
5120 return (pushback_buffer[--pushback_index]);
5122 while ((c = getc(f)) == '\\') {
5128 yylval.lineno = lineno;
5131 if (c == '\t' || c == ' ') {
5132 /* Compress blanks to a single space. */
5135 } while (c == '\t' || c == ' ');
5150 if (parseindex >= 0)
5153 if (pushback_index < MAXPUSHBACK-1)
5154 return (pushback_buffer[pushback_index++] = c);
5167 /* skip to either EOF or the first real EOL */
5190 while ((c = lgetc(fin)) == ' ')
5193 yylval.lineno = lineno;
5195 while ((c = lgetc(fin)) != '\n' && c != EOF)
5197 if (c == '$' && parsebuf == NULL) {
5199 if ((c = lgetc(fin)) == EOF)
5202 if (p + 1 >= buf + sizeof(buf) - 1) {
5203 yyerror("string too long");
5206 if (isalnum(c) || c == '_') {
5216 yyerror("macro '%s' not defined", buf);
5229 if ((c = lgetc(fin)) == EOF)
5239 if (p + 1 >= buf + sizeof(buf) - 1) {
5240 yyerror("string too long");
5245 yylval.v.string = strdup(buf);
5246 if (yylval.v.string == NULL)
5247 err(1, "yylex: strdup");
5252 yylval.v.i = PF_OP_XRG;
5253 return (PORTBINARY);
5260 yylval.v.i = PF_OP_IRG;
5261 return (PORTBINARY);
5273 #define allowed_in_string(x) \
5274 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
5275 x != '{' && x != '}' && x != '<' && x != '>' && \
5276 x != '!' && x != '=' && x != '/' && x != '#' && \
5279 if (isalnum(c) || c == ':' || c == '_') {
5282 if ((unsigned)(p-buf) >= sizeof(buf)) {
5283 yyerror("string too long");
5286 } while ((c = lgetc(fin)) != EOF && (allowed_in_string(c)));
5289 if ((token = lookup(buf)) == STRING)
5290 if ((yylval.v.string = strdup(buf)) == NULL)
5291 err(1, "yylex: strdup");
5295 yylval.lineno = lineno;
5304 parse_rules(FILE *input, struct pfctl *xpf)
5306 struct sym *sym, *next;
5312 rulestate = PFCTL_STATE_NONE;
5313 returnicmpdefault = (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
5314 returnicmp6default =
5315 (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
5316 blockpolicy = PFRULE_DROP;
5321 /* Free macros and check which have not been used. */
5322 for (sym = TAILQ_FIRST(&symhead); sym != NULL; sym = next) {
5323 next = TAILQ_NEXT(sym, entries);
5324 if ((pf->opts & PF_OPT_VERBOSE2) && !sym->used)
5325 fprintf(stderr, "warning: macro '%s' not "
5326 "used\n", sym->nam);
5329 TAILQ_REMOVE(&symhead, sym, entries);
5333 return (errors ? -1 : 0);
5337 * Over-designed efficiency is a French and German concept, so how about
5338 * we wait until they discover this ugliness and make it all fancy.
5341 symset(const char *nam, const char *val, int persist)
5345 for (sym = TAILQ_FIRST(&symhead); sym && strcmp(nam, sym->nam);
5346 sym = TAILQ_NEXT(sym, entries))
5350 if (sym->persist == 1)
5355 TAILQ_REMOVE(&symhead, sym, entries);
5359 if ((sym = calloc(1, sizeof(*sym))) == NULL)
5362 sym->nam = strdup(nam);
5363 if (sym->nam == NULL) {
5367 sym->val = strdup(val);
5368 if (sym->val == NULL) {
5374 sym->persist = persist;
5375 TAILQ_INSERT_TAIL(&symhead, sym, entries);
5380 pfctl_cmdline_symset(char *s)
5385 if ((val = strrchr(s, '=')) == NULL)
5388 if ((sym = malloc(strlen(s) - strlen(val) + 1)) == NULL)
5389 err(1, "pfctl_cmdline_symset: malloc");
5391 strlcpy(sym, s, strlen(s) - strlen(val) + 1);
5393 ret = symset(sym, val + 1, 1);
5400 symget(const char *nam)
5404 TAILQ_FOREACH(sym, &symhead, entries)
5405 if (strcmp(nam, sym->nam) == 0) {
5413 mv_rules(struct pf_ruleset *src, struct pf_ruleset *dst)
5418 for (i = 0; i < PF_RULESET_MAX; ++i) {
5419 while ((r = TAILQ_FIRST(src->rules[i].active.ptr))
5421 TAILQ_REMOVE(src->rules[i].active.ptr, r, entries);
5422 TAILQ_INSERT_TAIL(dst->rules[i].active.ptr, r, entries);
5423 dst->anchor->match++;
5425 src->anchor->match = 0;
5426 while ((r = TAILQ_FIRST(src->rules[i].inactive.ptr))
5428 TAILQ_REMOVE(src->rules[i].inactive.ptr, r, entries);
5429 TAILQ_INSERT_TAIL(dst->rules[i].inactive.ptr,
5436 decide_address_family(struct node_host *n, sa_family_t *af)
5438 if (*af != 0 || n == NULL)
5441 while ((n = n->next) != NULL) {
5450 remove_invalid_hosts(struct node_host **nh, sa_family_t *af)
5452 struct node_host *n = *nh, *prev = NULL;
5455 if (*af && n->af && n->af != *af) {
5456 /* unlink and free n */
5457 struct node_host *next = n->next;
5459 /* adjust tail pointer */
5460 if (n == (*nh)->tail)
5462 /* adjust previous node's next pointer */
5468 if (n->ifname != NULL)
5482 invalid_redirect(struct node_host *nh, sa_family_t af)
5485 struct node_host *n;
5487 /* tables and dyniftl are ok without an address family */
5488 for (n = nh; n != NULL; n = n->next) {
5489 if (n->addr.type != PF_ADDR_TABLE &&
5490 n->addr.type != PF_ADDR_DYNIFTL) {
5491 yyerror("address family not given and "
5492 "translation address expands to multiple "
5493 "address families");
5499 yyerror("no translation address with matching address family "
5507 atoul(char *s, u_long *ulvalp)
5513 ulval = strtoul(s, &ep, 0);
5514 if (s[0] == '\0' || *ep != '\0')
5516 if (errno == ERANGE && ulval == ULONG_MAX)
5528 if (atoul(n, &ulval) == 0) {
5529 if (ulval > 65535) {
5530 yyerror("illegal port value %lu", ulval);
5533 return (htons(ulval));
5535 s = getservbyname(n, "tcp");
5537 s = getservbyname(n, "udp");
5539 yyerror("unknown port %s", n);
5547 rule_label(struct pf_rule *r, char *s)
5550 if (strlcpy(r->label, s, sizeof(r->label)) >=
5552 yyerror("rule label too long (max %d chars)",
5553 sizeof(r->label)-1);
5561 parseicmpspec(char *w, sa_family_t af)
5563 const struct icmpcodeent *p;
5568 icmptype = returnicmpdefault >> 8;
5570 icmptype = returnicmp6default >> 8;
5572 if (atoul(w, &ulval) == -1) {
5573 if ((p = geticmpcodebyname(icmptype, w, af)) == NULL) {
5574 yyerror("unknown icmp code %s", w);
5580 yyerror("invalid icmp code %lu", ulval);
5583 return (icmptype << 8 | ulval);
5587 pfctl_load_anchors(int dev, struct pfctl *his_pf, struct pfr_buffer *trans)
5589 struct loadanchors *la;
5592 TAILQ_FOREACH(la, &loadanchorshead, entries) {
5593 if (his_pf->opts & PF_OPT_VERBOSE)
5594 fprintf(stderr, "\nLoading anchor %s from %s\n",
5595 la->anchorname, la->filename);
5596 if ((his_fin = pfctl_fopen(la->filename, "r")) == NULL) {
5597 warn("%s", la->filename);
5600 if (pfctl_rules(dev, la->filename, his_fin, his_pf->opts, his_pf->optimize,
5601 la->anchorname, trans) == -1)