1 /* $OpenBSD: parse.y,v 1.554 2008/10/17 12:59:53 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/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/types.h>
34 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <netinet/in_systm.h>
39 #include <netinet/ip.h>
40 #include <netinet/ip_icmp.h>
41 #include <netinet/icmp6.h>
42 #include <net/pfvar.h>
43 #include <arpa/inet.h>
44 #include <altq/altq.h>
45 #include <altq/altq_cbq.h>
46 #include <altq/altq_priq.h>
47 #include <altq/altq_hfsc.h>
64 #include "pfctl_parser.h"
67 static struct pfctl *pf = NULL;
69 static int rulestate = 0;
70 static u_int16_t returnicmpdefault =
71 (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
72 static u_int16_t returnicmp6default =
73 (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
74 static int blockpolicy = PFRULE_DROP;
75 static int require_order = 1;
76 static int default_statelock;
78 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
80 TAILQ_ENTRY(file) entry;
86 struct file *pushfile(const char *, int);
88 int check_file_secrecy(int, const char *);
91 int yyerror(const char *, ...);
92 int kw_cmp(const void *, const void *);
98 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
100 TAILQ_ENTRY(sym) entry;
106 int symset(const char *, const char *, int);
107 char *symget(const char *);
109 int atoul(char *, u_long *);
122 struct node_proto *next;
123 struct node_proto *tail;
129 struct node_port *next;
130 struct node_port *tail;
136 struct node_uid *next;
137 struct node_uid *tail;
143 struct node_gid *next;
144 struct node_gid *tail;
151 struct node_icmp *next;
152 struct node_icmp *tail;
155 enum { PF_STATE_OPT_MAX, PF_STATE_OPT_NOSYNC, PF_STATE_OPT_SRCTRACK,
156 PF_STATE_OPT_MAX_SRC_STATES, PF_STATE_OPT_MAX_SRC_CONN,
157 PF_STATE_OPT_MAX_SRC_CONN_RATE, PF_STATE_OPT_MAX_SRC_NODES,
158 PF_STATE_OPT_OVERLOAD, PF_STATE_OPT_STATELOCK,
159 PF_STATE_OPT_TIMEOUT, PF_STATE_OPT_SLOPPY,
160 PF_STATE_OPT_PFLOW };
162 enum { PF_SRCTRACK_NONE, PF_SRCTRACK, PF_SRCTRACK_GLOBAL, PF_SRCTRACK_RULE };
164 struct node_state_opt {
167 u_int32_t max_states;
168 u_int32_t max_src_states;
169 u_int32_t max_src_conn;
176 char tblname[PF_TABLE_NAME_SIZE];
178 u_int32_t max_src_nodes;
186 struct node_state_opt *next;
187 struct node_state_opt *tail;
191 struct node_host *host;
192 struct node_port *port;
196 char queue[PF_QNAME_SIZE];
197 char parent[PF_QNAME_SIZE];
198 char ifname[IFNAMSIZ];
200 struct node_queue *next;
201 struct node_queue *tail;
204 struct node_qassign {
211 #define FOM_FLAGS 0x01
212 #define FOM_ICMP 0x02
214 #define FOM_KEEP 0x08
215 #define FOM_SRCTRACK 0x10
216 struct node_uid *uid;
217 struct node_gid *gid;
224 struct node_icmp *icmpspec;
229 struct node_state_opt *options;
234 struct node_qassign queues;
237 u_int8_t match_tag_not;
240 struct node_host *addr;
245 struct antispoof_opts {
252 #define SOM_MINTTL 0x01
253 #define SOM_MAXMSS 0x02
254 #define SOM_FRAGCACHE 0x04
255 #define SOM_SETTOS 0x08
264 u_int8_t match_tag_not;
270 #define QOM_BWSPEC 0x01
271 #define QOM_SCHEDULER 0x02
272 #define QOM_PRIORITY 0x04
273 #define QOM_TBRSIZE 0x08
274 #define QOM_QLIMIT 0x10
275 struct node_queue_bw queue_bwspec;
276 struct node_queue_opt scheduler;
285 struct node_tinithead init_nodes;
290 #define POM_TYPE 0x01
291 #define POM_STICKYADDRESS 0x02
295 struct pf_poolhashkey *key;
300 struct node_hfsc_opts hfsc_opts;
301 struct node_state_opt *keep_state_defaults = NULL;
303 int disallow_table(struct node_host *, const char *);
304 int disallow_urpf_failed(struct node_host *, const char *);
305 int disallow_alias(struct node_host *, const char *);
306 int rule_consistent(struct pf_rule *, int);
307 int filter_consistent(struct pf_rule *, int);
308 int nat_consistent(struct pf_rule *);
309 int rdr_consistent(struct pf_rule *);
310 int process_tabledef(char *, struct table_opts *);
311 void expand_label_str(char *, size_t, const char *, const char *);
312 void expand_label_if(const char *, char *, size_t, const char *);
313 void expand_label_addr(const char *, char *, size_t, u_int8_t,
315 void expand_label_port(const char *, char *, size_t,
317 void expand_label_proto(const char *, char *, size_t, u_int8_t);
318 void expand_label_nr(const char *, char *, size_t);
319 void expand_label(char *, size_t, const char *, u_int8_t,
320 struct node_host *, struct node_port *, struct node_host *,
321 struct node_port *, u_int8_t);
322 void expand_rule(struct pf_rule *, struct node_if *,
323 struct node_host *, struct node_proto *, struct node_os *,
324 struct node_host *, struct node_port *, struct node_host *,
325 struct node_port *, struct node_uid *, struct node_gid *,
326 struct node_icmp *, const char *);
327 int expand_altq(struct pf_altq *, struct node_if *,
328 struct node_queue *, struct node_queue_bw bwspec,
329 struct node_queue_opt *);
330 int expand_queue(struct pf_altq *, struct node_if *,
331 struct node_queue *, struct node_queue_bw,
332 struct node_queue_opt *);
333 int expand_skip_interface(struct node_if *);
335 int check_rulestate(int);
336 int getservice(char *);
337 int rule_label(struct pf_rule *, char *);
339 void mv_rules(struct pf_ruleset *, struct pf_ruleset *);
340 void decide_address_family(struct node_host *, sa_family_t *);
341 void remove_invalid_hosts(struct node_host **, sa_family_t *);
342 int invalid_redirect(struct node_host *, sa_family_t);
343 u_int16_t parseicmpspec(char *, sa_family_t);
345 TAILQ_HEAD(loadanchorshead, loadanchors)
346 loadanchorshead = TAILQ_HEAD_INITIALIZER(loadanchorshead);
349 TAILQ_ENTRY(loadanchors) entries;
372 struct node_if *interface;
373 struct node_proto *proto;
374 struct node_icmp *icmp;
375 struct node_host *host;
377 struct node_port *port;
378 struct node_uid *uid;
379 struct node_gid *gid;
380 struct node_state_opt *state_opt;
383 struct peer src, dst;
384 struct node_os *src_os;
387 struct node_host *host;
391 struct pf_poolhashkey *key;
394 struct node_host *host;
399 struct node_state_opt *options;
410 struct pf_poolhashkey *hashkey;
411 struct node_queue *queue;
412 struct node_queue_opt queue_options;
413 struct node_queue_bw queue_bwspec;
414 struct node_qassign qassign;
415 struct filter_opts filter_opts;
416 struct antispoof_opts antispoof_opts;
417 struct queue_opts queue_opts;
418 struct scrub_opts scrub_opts;
419 struct table_opts table_opts;
420 struct pool_opts pool_opts;
421 struct node_hfsc_opts hfsc_opts;
426 #define PPORT_RANGE 1
428 int parseport(char *, struct range *r, int);
430 #define DYNIF_MULTIADDR(addr) ((addr).type == PF_ADDR_DYNIFTL && \
431 (!((addr).iflags & PFI_AFLAG_NOALIAS) || \
432 !isdigit((addr).v.ifname[strlen((addr).v.ifname)-1])))
436 %token PASS BLOCK SCRUB RETURN IN OS OUT LOG QUICK ON FROM TO FLAGS
437 %token RETURNRST RETURNICMP RETURNICMP6 PROTO INET INET6 ALL ANY ICMPTYPE
438 %token ICMP6TYPE CODE KEEP MODULATE STATE PORT RDR NAT BINAT ARROW NODF
439 %token MINTTL ERROR ALLOWOPTS FASTROUTE FILENAME ROUTETO DUPTO REPLYTO NO LABEL
440 %token NOROUTE URPFFAILED FRAGMENT USER GROUP MAXMSS MAXIMUM TTL TOS DROP TABLE
441 %token REASSEMBLE FRAGDROP FRAGCROP ANCHOR NATANCHOR RDRANCHOR BINATANCHOR
442 %token SET OPTIMIZATION TIMEOUT LIMIT LOGINTERFACE BLOCKPOLICY RANDOMID
443 %token REQUIREORDER SYNPROXY FINGERPRINTS NOSYNC DEBUG SKIP HOSTID
444 %token ANTISPOOF FOR INCLUDE
445 %token BITMASK RANDOM SOURCEHASH ROUNDROBIN STATICPORT PROBABILITY
446 %token ALTQ CBQ PRIQ HFSC BANDWIDTH TBRSIZE LINKSHARE REALTIME UPPERLIMIT
447 %token QUEUE PRIORITY QLIMIT RTABLE
448 %token LOAD RULESET_OPTIMIZATION
449 %token STICKYADDRESS MAXSRCSTATES MAXSRCNODES SOURCETRACK GLOBAL RULE
450 %token MAXSRCCONN MAXSRCCONNRATE OVERLOAD FLUSH SLOPPY PFLOW
451 %token TAGGED TAG IFBOUND FLOATING STATEPOLICY STATEDEFAULTS ROUTE SETTOS
452 %token DIVERTTO DIVERTREPLY
453 %token <v.string> STRING
454 %token <v.number> NUMBER
455 %token <v.i> PORTBINARY
456 %type <v.interface> interface if_list if_item_not if_item
457 %type <v.number> number icmptype icmp6type uid gid
458 %type <v.number> tos not yesno
459 %type <v.probability> probability
460 %type <v.i> no dir af fragcache optimizer
461 %type <v.i> sourcetrack flush unaryop statelock
462 %type <v.b> action nataction natpasslog scrubaction
463 %type <v.b> flags flag blockspec
464 %type <v.range> portplain portstar portrange
465 %type <v.hashkey> hashkey
466 %type <v.proto> proto proto_list proto_item
467 %type <v.number> protoval
468 %type <v.icmp> icmpspec
469 %type <v.icmp> icmp_list icmp_item
470 %type <v.icmp> icmp6_list icmp6_item
471 %type <v.number> reticmpspec reticmp6spec
472 %type <v.fromto> fromto
473 %type <v.peer> ipportspec from to
474 %type <v.host> ipspec toipspec xhost host dynaddr host_list
475 %type <v.host> redir_host_list redirspec
476 %type <v.host> route_host route_host_list routespec
477 %type <v.os> os xos os_list
478 %type <v.port> portspec port_list port_item
479 %type <v.uid> uids uid_list uid_item
480 %type <v.gid> gids gid_list gid_item
481 %type <v.route> route
482 %type <v.redirection> redirection redirpool
483 %type <v.string> label stringall tag anchorname
484 %type <v.string> string varstring numberstring
485 %type <v.keep_state> keep
486 %type <v.state_opt> state_opt_spec state_opt_list state_opt_item
487 %type <v.logquick> logquick quick log logopts logopt
488 %type <v.interface> antispoof_ifspc antispoof_iflst antispoof_if
489 %type <v.qassign> qname
490 %type <v.queue> qassign qassign_list qassign_item
491 %type <v.queue_options> scheduler
492 %type <v.number> cbqflags_list cbqflags_item
493 %type <v.number> priqflags_list priqflags_item
494 %type <v.hfsc_opts> hfscopts_list hfscopts_item hfsc_opts
495 %type <v.queue_bwspec> bandwidth
496 %type <v.filter_opts> filter_opts filter_opt filter_opts_l
497 %type <v.antispoof_opts> antispoof_opts antispoof_opt antispoof_opts_l
498 %type <v.queue_opts> queue_opts queue_opt queue_opts_l
499 %type <v.scrub_opts> scrub_opts scrub_opt scrub_opts_l
500 %type <v.table_opts> table_opts table_opt table_opts_l
501 %type <v.pool_opts> pool_opts pool_opt pool_opts_l
502 %type <v.tagged> tagged
503 %type <v.rtableid> rtable
506 ruleset : /* empty */
507 | ruleset include '\n'
509 | ruleset option '\n'
510 | ruleset scrubrule '\n'
511 | ruleset natrule '\n'
512 | ruleset binatrule '\n'
513 | ruleset pfrule '\n'
514 | ruleset anchorrule '\n'
515 | ruleset loadrule '\n'
516 | ruleset altqif '\n'
517 | ruleset queuespec '\n'
518 | ruleset varset '\n'
519 | ruleset antispoof '\n'
520 | ruleset tabledef '\n'
521 | '{' fakeanchor '}' '\n';
522 | ruleset error '\n' { file->errors++; }
525 include : INCLUDE STRING {
528 if ((nfile = pushfile($2, 0)) == NULL) {
529 yyerror("failed to include file %s", $2);
541 * apply to previouslys specified rule: must be careful to note
542 * what that is: pf or nat or binat or rdr
544 fakeanchor : fakeanchor '\n'
545 | fakeanchor anchorrule '\n'
546 | fakeanchor binatrule '\n'
547 | fakeanchor natrule '\n'
548 | fakeanchor pfrule '\n'
549 | fakeanchor error '\n'
553 if (!strcmp($1, "none"))
555 else if (!strcmp($1, "basic"))
556 $$ = PF_OPTIMIZE_BASIC;
557 else if (!strcmp($1, "profile"))
558 $$ = PF_OPTIMIZE_BASIC | PF_OPTIMIZE_PROFILE;
560 yyerror("unknown ruleset-optimization %s", $1);
566 option : SET OPTIMIZATION STRING {
567 if (check_rulestate(PFCTL_STATE_OPTION)) {
571 if (pfctl_set_optimization(pf, $3) != 0) {
572 yyerror("unknown optimization %s", $3);
578 | SET RULESET_OPTIMIZATION optimizer {
579 if (!(pf->opts & PF_OPT_OPTIMIZE)) {
580 pf->opts |= PF_OPT_OPTIMIZE;
584 | SET TIMEOUT timeout_spec
585 | SET TIMEOUT '{' optnl timeout_list '}'
586 | SET LIMIT limit_spec
587 | SET LIMIT '{' optnl limit_list '}'
588 | SET LOGINTERFACE stringall {
589 if (check_rulestate(PFCTL_STATE_OPTION)) {
593 if (pfctl_set_logif(pf, $3) != 0) {
594 yyerror("error setting loginterface %s", $3);
600 | SET HOSTID number {
601 if ($3 == 0 || $3 > UINT_MAX) {
602 yyerror("hostid must be non-zero");
605 if (pfctl_set_hostid(pf, $3) != 0) {
606 yyerror("error setting hostid %08x", $3);
610 | SET BLOCKPOLICY DROP {
611 if (pf->opts & PF_OPT_VERBOSE)
612 printf("set block-policy drop\n");
613 if (check_rulestate(PFCTL_STATE_OPTION))
615 blockpolicy = PFRULE_DROP;
617 | SET BLOCKPOLICY RETURN {
618 if (pf->opts & PF_OPT_VERBOSE)
619 printf("set block-policy return\n");
620 if (check_rulestate(PFCTL_STATE_OPTION))
622 blockpolicy = PFRULE_RETURN;
624 | SET REQUIREORDER yesno {
625 if (pf->opts & PF_OPT_VERBOSE)
626 printf("set require-order %s\n",
627 $3 == 1 ? "yes" : "no");
630 | SET FINGERPRINTS STRING {
631 if (pf->opts & PF_OPT_VERBOSE)
632 printf("set fingerprints \"%s\"\n", $3);
633 if (check_rulestate(PFCTL_STATE_OPTION)) {
637 if (!pf->anchor->name[0]) {
638 if (pfctl_file_fingerprints(pf->dev,
640 yyerror("error loading "
641 "fingerprints %s", $3);
648 | SET STATEPOLICY statelock {
649 if (pf->opts & PF_OPT_VERBOSE)
652 printf("set state-policy floating\n");
655 printf("set state-policy if-bound\n");
658 default_statelock = $3;
661 if (check_rulestate(PFCTL_STATE_OPTION)) {
665 if (pfctl_set_debug(pf, $3) != 0) {
666 yyerror("error setting debuglevel %s", $3);
672 | SET SKIP interface {
673 if (expand_skip_interface($3) != 0) {
674 yyerror("error setting skip interface(s)");
678 | SET STATEDEFAULTS state_opt_list {
679 if (keep_state_defaults != NULL) {
680 yyerror("cannot redefine state-defaults");
683 keep_state_defaults = $3;
687 stringall : STRING { $$ = $1; }
689 if (($$ = strdup("all")) == NULL) {
690 err(1, "stringall: strdup");
695 string : STRING string {
696 if (asprintf(&$$, "%s %s", $1, $2) == -1)
697 err(1, "string: asprintf");
704 varstring : numberstring varstring {
705 if (asprintf(&$$, "%s %s", $1, $2) == -1)
706 err(1, "string: asprintf");
713 numberstring : NUMBER {
715 if (asprintf(&s, "%lld", (long long)$1) == -1) {
716 yyerror("string: asprintf");
724 varset : STRING '=' varstring {
725 if (pf->opts & PF_OPT_VERBOSE)
726 printf("%s = \"%s\"\n", $1, $3);
727 if (symset($1, $3, 0) == -1)
728 err(1, "cannot store variable %s", $1);
734 anchorname : STRING { $$ = $1; }
735 | /* empty */ { $$ = NULL; }
738 pfa_anchorlist : /* empty */
739 | pfa_anchorlist '\n'
740 | pfa_anchorlist pfrule '\n'
741 | pfa_anchorlist anchorrule '\n'
746 char ta[PF_ANCHOR_NAME_SIZE];
747 struct pf_ruleset *rs;
749 /* steping into a brace anchor */
754 /* create a holding ruleset in the root */
755 snprintf(ta, PF_ANCHOR_NAME_SIZE, "_%d", pf->bn);
756 rs = pf_find_or_create_ruleset(ta);
758 err(1, "pfa_anchor: pf_find_or_create_ruleset");
759 pf->astack[pf->asd] = rs->anchor;
760 pf->anchor = rs->anchor;
761 } '\n' pfa_anchorlist '}'
763 pf->alast = pf->anchor;
765 pf->anchor = pf->astack[pf->asd];
770 anchorrule : ANCHOR anchorname dir quick interface af proto fromto
771 filter_opts pfa_anchor
774 struct node_proto *proto;
776 if (check_rulestate(PFCTL_STATE_FILTER)) {
782 if ($2 && ($2[0] == '_' || strstr($2, "/_") != NULL)) {
784 yyerror("anchor names beginning with '_' "
785 "are reserved for internal use");
789 memset(&r, 0, sizeof(r));
790 if (pf->astack[pf->asd + 1]) {
791 /* move inline rules into relative location */
793 &pf->astack[pf->asd]->ruleset,
794 $2 ? $2 : pf->alast->name);
796 if (r.anchor == NULL)
797 err(1, "anchorrule: unable to "
800 if (pf->alast != r.anchor) {
801 if (r.anchor->match) {
802 yyerror("inline anchor '%s' "
807 mv_rules(&pf->alast->ruleset,
810 pf_remove_if_empty_ruleset(&pf->alast->ruleset);
811 pf->alast = r.anchor;
814 yyerror("anchors without explicit "
815 "rules must specify a name");
823 r.rtableid = $9.rtableid;
826 if (strlcpy(r.tagname, $9.tag,
827 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
828 yyerror("tag too long, max %u chars",
829 PF_TAG_NAME_SIZE - 1);
833 if (strlcpy(r.match_tagname, $9.match_tag,
834 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
835 yyerror("tag too long, max %u chars",
836 PF_TAG_NAME_SIZE - 1);
839 r.match_tag_not = $9.match_tag_not;
840 if (rule_label(&r, $9.label))
843 r.flags = $9.flags.b1;
844 r.flagset = $9.flags.b2;
845 if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
846 yyerror("flags always false");
849 if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
850 for (proto = $7; proto != NULL &&
851 proto->proto != IPPROTO_TCP;
854 if (proto == NULL && $7 != NULL) {
855 if ($9.flags.b1 || $9.flags.b2)
857 "flags only apply to tcp");
860 "OS fingerprinting only "
868 if ($9.keep.action) {
869 yyerror("cannot specify state handling "
875 if (strlcpy(r.match_tagname, $9.match_tag,
876 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
877 yyerror("tag too long, max %u chars",
878 PF_TAG_NAME_SIZE - 1);
881 r.match_tag_not = $9.match_tag_not;
883 decide_address_family($8.src.host, &r.af);
884 decide_address_family($8.dst.host, &r.af);
886 expand_rule(&r, $5, NULL, $7, $8.src_os,
887 $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
888 $9.uid, $9.gid, $9.icmpspec,
889 pf->astack[pf->asd + 1] ? pf->alast->name : $2);
891 pf->astack[pf->asd + 1] = NULL;
893 | NATANCHOR string interface af proto fromto rtable {
896 if (check_rulestate(PFCTL_STATE_NAT)) {
901 memset(&r, 0, sizeof(r));
906 decide_address_family($6.src.host, &r.af);
907 decide_address_family($6.dst.host, &r.af);
909 expand_rule(&r, $3, NULL, $5, $6.src_os,
910 $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
914 | RDRANCHOR string interface af proto fromto rtable {
917 if (check_rulestate(PFCTL_STATE_NAT)) {
922 memset(&r, 0, sizeof(r));
927 decide_address_family($6.src.host, &r.af);
928 decide_address_family($6.dst.host, &r.af);
930 if ($6.src.port != NULL) {
931 yyerror("source port parameter not supported"
935 if ($6.dst.port != NULL) {
936 if ($6.dst.port->next != NULL) {
937 yyerror("destination port list "
938 "expansion not supported in "
941 } else if ($6.dst.port->op != PF_OP_EQ) {
942 yyerror("destination port operators"
943 " not supported in rdr-anchor");
946 r.dst.port[0] = $6.dst.port->port[0];
947 r.dst.port[1] = $6.dst.port->port[1];
948 r.dst.port_op = $6.dst.port->op;
951 expand_rule(&r, $3, NULL, $5, $6.src_os,
952 $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
956 | BINATANCHOR string interface af proto fromto rtable {
959 if (check_rulestate(PFCTL_STATE_NAT)) {
964 memset(&r, 0, sizeof(r));
969 if ($5->next != NULL) {
970 yyerror("proto list expansion"
971 " not supported in binat-anchor");
978 if ($6.src.host != NULL || $6.src.port != NULL ||
979 $6.dst.host != NULL || $6.dst.port != NULL) {
980 yyerror("fromto parameter not supported"
985 decide_address_family($6.src.host, &r.af);
986 decide_address_family($6.dst.host, &r.af);
988 pfctl_add_rule(pf, &r, $2);
993 loadrule : LOAD ANCHOR string FROM string {
994 struct loadanchors *loadanchor;
996 if (strlen(pf->anchor->name) + 1 +
997 strlen($3) >= MAXPATHLEN) {
998 yyerror("anchorname %s too long, max %u\n",
1003 loadanchor = calloc(1, sizeof(struct loadanchors));
1004 if (loadanchor == NULL)
1005 err(1, "loadrule: calloc");
1006 if ((loadanchor->anchorname = malloc(MAXPATHLEN)) ==
1008 err(1, "loadrule: malloc");
1009 if (pf->anchor->name[0])
1010 snprintf(loadanchor->anchorname, MAXPATHLEN,
1011 "%s/%s", pf->anchor->name, $3);
1013 strlcpy(loadanchor->anchorname, $3, MAXPATHLEN);
1014 if ((loadanchor->filename = strdup($5)) == NULL)
1015 err(1, "loadrule: strdup");
1017 TAILQ_INSERT_TAIL(&loadanchorshead, loadanchor,
1024 scrubaction : no SCRUB {
1033 scrubrule : scrubaction dir logquick interface af proto fromto scrub_opts
1037 if (check_rulestate(PFCTL_STATE_SCRUB))
1040 memset(&r, 0, sizeof(r));
1048 yyerror("scrub rules do not support 'quick'");
1054 r.rule_flag |= PFRULE_NODF;
1056 r.rule_flag |= PFRULE_RANDOMID;
1057 if ($8.reassemble_tcp) {
1058 if (r.direction != PF_INOUT) {
1059 yyerror("reassemble tcp rules can not "
1060 "specify direction");
1063 r.rule_flag |= PFRULE_REASSEMBLE_TCP;
1066 r.min_ttl = $8.minttl;
1068 r.max_mss = $8.maxmss;
1069 if ($8.marker & SOM_SETTOS) {
1070 r.rule_flag |= PFRULE_SET_TOS;
1071 r.set_tos = $8.settos;
1074 r.rule_flag |= $8.fragcache;
1076 if (strlcpy(r.match_tagname, $8.match_tag,
1077 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1078 yyerror("tag too long, max %u chars",
1079 PF_TAG_NAME_SIZE - 1);
1082 r.match_tag_not = $8.match_tag_not;
1083 r.rtableid = $8.rtableid;
1085 expand_rule(&r, $4, NULL, $6, $7.src_os,
1086 $7.src.host, $7.src.port, $7.dst.host, $7.dst.port,
1087 NULL, NULL, NULL, "");
1092 bzero(&scrub_opts, sizeof scrub_opts);
1093 scrub_opts.rtableid = -1;
1096 { $$ = scrub_opts; }
1098 bzero(&scrub_opts, sizeof scrub_opts);
1099 scrub_opts.rtableid = -1;
1104 scrub_opts_l : scrub_opts_l scrub_opt
1109 if (scrub_opts.nodf) {
1110 yyerror("no-df cannot be respecified");
1113 scrub_opts.nodf = 1;
1116 if (scrub_opts.marker & SOM_MINTTL) {
1117 yyerror("min-ttl cannot be respecified");
1120 if ($2 < 0 || $2 > 255) {
1121 yyerror("illegal min-ttl value %d", $2);
1124 scrub_opts.marker |= SOM_MINTTL;
1125 scrub_opts.minttl = $2;
1128 if (scrub_opts.marker & SOM_MAXMSS) {
1129 yyerror("max-mss cannot be respecified");
1132 if ($2 < 0 || $2 > 65535) {
1133 yyerror("illegal max-mss value %d", $2);
1136 scrub_opts.marker |= SOM_MAXMSS;
1137 scrub_opts.maxmss = $2;
1140 if (scrub_opts.marker & SOM_SETTOS) {
1141 yyerror("set-tos cannot be respecified");
1144 scrub_opts.marker |= SOM_SETTOS;
1145 scrub_opts.settos = $2;
1148 if (scrub_opts.marker & SOM_FRAGCACHE) {
1149 yyerror("fragcache cannot be respecified");
1152 scrub_opts.marker |= SOM_FRAGCACHE;
1153 scrub_opts.fragcache = $1;
1155 | REASSEMBLE STRING {
1156 if (strcasecmp($2, "tcp") != 0) {
1157 yyerror("scrub reassemble supports only tcp, "
1163 if (scrub_opts.reassemble_tcp) {
1164 yyerror("reassemble tcp cannot be respecified");
1167 scrub_opts.reassemble_tcp = 1;
1170 if (scrub_opts.randomid) {
1171 yyerror("random-id cannot be respecified");
1174 scrub_opts.randomid = 1;
1177 if ($2 < 0 /* || $2 > RT_TABLEID_MAX */) {
1178 yyerror("invalid rtable id");
1181 scrub_opts.rtableid = $2;
1183 | not TAGGED string {
1184 scrub_opts.match_tag = $3;
1185 scrub_opts.match_tag_not = $1;
1189 fragcache : FRAGMENT REASSEMBLE { $$ = 0; /* default */ }
1190 | FRAGMENT FRAGCROP { $$ = PFRULE_FRAGCROP; }
1191 | FRAGMENT FRAGDROP { $$ = PFRULE_FRAGDROP; }
1194 antispoof : ANTISPOOF logquick antispoof_ifspc af antispoof_opts {
1196 struct node_host *h = NULL, *hh;
1197 struct node_if *i, *j;
1199 if (check_rulestate(PFCTL_STATE_FILTER))
1202 for (i = $3; i; i = i->next) {
1203 bzero(&r, sizeof(r));
1206 r.direction = PF_IN;
1211 if (rule_label(&r, $5.label))
1213 r.rtableid = $5.rtableid;
1214 j = calloc(1, sizeof(struct node_if));
1216 err(1, "antispoof: calloc");
1217 if (strlcpy(j->ifname, i->ifname,
1218 sizeof(j->ifname)) >= sizeof(j->ifname)) {
1220 yyerror("interface name too long");
1225 h = calloc(1, sizeof(*h));
1227 err(1, "address: calloc");
1228 h->addr.type = PF_ADDR_DYNIFTL;
1230 if (strlcpy(h->addr.v.ifname, i->ifname,
1231 sizeof(h->addr.v.ifname)) >=
1232 sizeof(h->addr.v.ifname)) {
1235 "interface name too long");
1238 hh = malloc(sizeof(*hh));
1240 err(1, "address: malloc");
1241 bcopy(h, hh, sizeof(*hh));
1242 h->addr.iflags = PFI_AFLAG_NETWORK;
1244 h = ifa_lookup(j->ifname,
1250 expand_rule(&r, j, NULL, NULL, NULL, h,
1251 NULL, NULL, NULL, NULL, NULL,
1254 if ((i->ifa_flags & IFF_LOOPBACK) == 0) {
1255 bzero(&r, sizeof(r));
1258 r.direction = PF_IN;
1263 if (rule_label(&r, $5.label))
1265 r.rtableid = $5.rtableid;
1269 h = ifa_lookup(i->ifname, 0);
1271 expand_rule(&r, NULL, NULL,
1272 NULL, NULL, h, NULL, NULL,
1273 NULL, NULL, NULL, NULL, "");
1281 antispoof_ifspc : FOR antispoof_if { $$ = $2; }
1282 | FOR '{' optnl antispoof_iflst '}' { $$ = $4; }
1285 antispoof_iflst : antispoof_if optnl { $$ = $1; }
1286 | antispoof_iflst comma antispoof_if optnl {
1287 $1->tail->next = $3;
1293 antispoof_if : if_item { $$ = $1; }
1301 bzero(&antispoof_opts, sizeof antispoof_opts);
1302 antispoof_opts.rtableid = -1;
1305 { $$ = antispoof_opts; }
1307 bzero(&antispoof_opts, sizeof antispoof_opts);
1308 antispoof_opts.rtableid = -1;
1309 $$ = antispoof_opts;
1313 antispoof_opts_l : antispoof_opts_l antispoof_opt
1317 antispoof_opt : label {
1318 if (antispoof_opts.label) {
1319 yyerror("label cannot be redefined");
1322 antispoof_opts.label = $1;
1325 if ($2 < 0 /* || $2 > RT_TABLEID_MAX */ ) {
1326 yyerror("invalid rtable id");
1329 antispoof_opts.rtableid = $2;
1333 not : '!' { $$ = 1; }
1334 | /* empty */ { $$ = 0; }
1337 tabledef : TABLE '<' STRING '>' table_opts {
1338 struct node_host *h, *nh;
1339 struct node_tinit *ti, *nti;
1341 if (strlen($3) >= PF_TABLE_NAME_SIZE) {
1342 yyerror("table name too long, max %d chars",
1343 PF_TABLE_NAME_SIZE - 1);
1347 if (pf->loadopt & PFCTL_FLAG_TABLE)
1348 if (process_tabledef($3, &$5)) {
1353 for (ti = SIMPLEQ_FIRST(&$5.init_nodes);
1354 ti != SIMPLEQ_END(&$5.init_nodes); ti = nti) {
1357 for (h = ti->host; h != NULL; h = nh) {
1361 nti = SIMPLEQ_NEXT(ti, entries);
1368 bzero(&table_opts, sizeof table_opts);
1369 SIMPLEQ_INIT(&table_opts.init_nodes);
1372 { $$ = table_opts; }
1375 bzero(&table_opts, sizeof table_opts);
1376 SIMPLEQ_INIT(&table_opts.init_nodes);
1381 table_opts_l : table_opts_l table_opt
1385 table_opt : STRING {
1386 if (!strcmp($1, "const"))
1387 table_opts.flags |= PFR_TFLAG_CONST;
1388 else if (!strcmp($1, "persist"))
1389 table_opts.flags |= PFR_TFLAG_PERSIST;
1390 else if (!strcmp($1, "counters"))
1391 table_opts.flags |= PFR_TFLAG_COUNTERS;
1393 yyerror("invalid table option '%s'", $1);
1399 | '{' optnl '}' { table_opts.init_addr = 1; }
1400 | '{' optnl host_list '}' {
1401 struct node_host *n;
1402 struct node_tinit *ti;
1404 for (n = $3; n != NULL; n = n->next) {
1405 switch (n->addr.type) {
1406 case PF_ADDR_ADDRMASK:
1409 yyerror("address ranges are not "
1410 "permitted inside tables");
1412 case PF_ADDR_DYNIFTL:
1413 yyerror("dynamic addresses are not "
1414 "permitted inside tables");
1417 yyerror("tables cannot contain tables");
1419 case PF_ADDR_NOROUTE:
1420 yyerror("\"no-route\" is not permitted "
1423 case PF_ADDR_URPFFAILED:
1424 yyerror("\"urpf-failed\" is not "
1425 "permitted inside tables");
1428 yyerror("unknown address type %d",
1433 if (!(ti = calloc(1, sizeof(*ti))))
1434 err(1, "table_opt: calloc");
1436 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1438 table_opts.init_addr = 1;
1441 struct node_tinit *ti;
1443 if (!(ti = calloc(1, sizeof(*ti))))
1444 err(1, "table_opt: calloc");
1446 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1448 table_opts.init_addr = 1;
1452 altqif : ALTQ interface queue_opts QUEUE qassign {
1455 if (check_rulestate(PFCTL_STATE_QUEUE))
1458 memset(&a, 0, sizeof(a));
1459 if ($3.scheduler.qtype == ALTQT_NONE) {
1460 yyerror("no scheduler specified!");
1463 a.scheduler = $3.scheduler.qtype;
1464 a.qlimit = $3.qlimit;
1465 a.tbrsize = $3.tbrsize;
1467 yyerror("no child queues specified");
1470 if (expand_altq(&a, $2, $5, $3.queue_bwspec,
1476 queuespec : QUEUE STRING interface queue_opts qassign {
1479 if (check_rulestate(PFCTL_STATE_QUEUE)) {
1484 memset(&a, 0, sizeof(a));
1486 if (strlcpy(a.qname, $2, sizeof(a.qname)) >=
1488 yyerror("queue name too long (max "
1489 "%d chars)", PF_QNAME_SIZE-1);
1495 yyerror("cannot specify tbrsize for queue");
1498 if ($4.priority > 255) {
1499 yyerror("priority out of range: max 255");
1502 a.priority = $4.priority;
1503 a.qlimit = $4.qlimit;
1504 a.scheduler = $4.scheduler.qtype;
1505 if (expand_queue(&a, $3, $5, $4.queue_bwspec,
1507 yyerror("errors in queue definition");
1514 bzero(&queue_opts, sizeof queue_opts);
1515 queue_opts.priority = DEFAULT_PRIORITY;
1516 queue_opts.qlimit = DEFAULT_QLIMIT;
1517 queue_opts.scheduler.qtype = ALTQT_NONE;
1518 queue_opts.queue_bwspec.bw_percent = 100;
1521 { $$ = queue_opts; }
1523 bzero(&queue_opts, sizeof queue_opts);
1524 queue_opts.priority = DEFAULT_PRIORITY;
1525 queue_opts.qlimit = DEFAULT_QLIMIT;
1526 queue_opts.scheduler.qtype = ALTQT_NONE;
1527 queue_opts.queue_bwspec.bw_percent = 100;
1532 queue_opts_l : queue_opts_l queue_opt
1536 queue_opt : BANDWIDTH bandwidth {
1537 if (queue_opts.marker & QOM_BWSPEC) {
1538 yyerror("bandwidth cannot be respecified");
1541 queue_opts.marker |= QOM_BWSPEC;
1542 queue_opts.queue_bwspec = $2;
1545 if (queue_opts.marker & QOM_PRIORITY) {
1546 yyerror("priority cannot be respecified");
1549 if ($2 < 0 || $2 > 255) {
1550 yyerror("priority out of range: max 255");
1553 queue_opts.marker |= QOM_PRIORITY;
1554 queue_opts.priority = $2;
1557 if (queue_opts.marker & QOM_QLIMIT) {
1558 yyerror("qlimit cannot be respecified");
1561 if ($2 < 0 || $2 > 65535) {
1562 yyerror("qlimit out of range: max 65535");
1565 queue_opts.marker |= QOM_QLIMIT;
1566 queue_opts.qlimit = $2;
1569 if (queue_opts.marker & QOM_SCHEDULER) {
1570 yyerror("scheduler cannot be respecified");
1573 queue_opts.marker |= QOM_SCHEDULER;
1574 queue_opts.scheduler = $1;
1577 if (queue_opts.marker & QOM_TBRSIZE) {
1578 yyerror("tbrsize cannot be respecified");
1581 if ($2 < 0 || $2 > 65535) {
1582 yyerror("tbrsize too big: max 65535");
1585 queue_opts.marker |= QOM_TBRSIZE;
1586 queue_opts.tbrsize = $2;
1590 bandwidth : STRING {
1596 bps = strtod($1, &cp);
1598 if (!strcmp(cp, "b"))
1600 else if (!strcmp(cp, "Kb"))
1602 else if (!strcmp(cp, "Mb"))
1604 else if (!strcmp(cp, "Gb"))
1605 bps *= 1000 * 1000 * 1000;
1606 else if (!strcmp(cp, "%")) {
1607 if (bps < 0 || bps > 100) {
1608 yyerror("bandwidth spec "
1613 $$.bw_percent = bps;
1616 yyerror("unknown unit %s", cp);
1622 $$.bw_absolute = (u_int32_t)bps;
1625 if ($1 < 0 || $1 > UINT_MAX) {
1626 yyerror("bandwidth number too big");
1630 $$.bw_absolute = $1;
1635 $$.qtype = ALTQT_CBQ;
1636 $$.data.cbq_opts.flags = 0;
1638 | CBQ '(' cbqflags_list ')' {
1639 $$.qtype = ALTQT_CBQ;
1640 $$.data.cbq_opts.flags = $3;
1643 $$.qtype = ALTQT_PRIQ;
1644 $$.data.priq_opts.flags = 0;
1646 | PRIQ '(' priqflags_list ')' {
1647 $$.qtype = ALTQT_PRIQ;
1648 $$.data.priq_opts.flags = $3;
1651 $$.qtype = ALTQT_HFSC;
1652 bzero(&$$.data.hfsc_opts,
1653 sizeof(struct node_hfsc_opts));
1655 | HFSC '(' hfsc_opts ')' {
1656 $$.qtype = ALTQT_HFSC;
1657 $$.data.hfsc_opts = $3;
1661 cbqflags_list : cbqflags_item { $$ |= $1; }
1662 | cbqflags_list comma cbqflags_item { $$ |= $3; }
1665 cbqflags_item : STRING {
1666 if (!strcmp($1, "default"))
1667 $$ = CBQCLF_DEFCLASS;
1668 else if (!strcmp($1, "borrow"))
1670 else if (!strcmp($1, "red"))
1672 else if (!strcmp($1, "ecn"))
1673 $$ = CBQCLF_RED|CBQCLF_ECN;
1674 else if (!strcmp($1, "rio"))
1677 yyerror("unknown cbq flag \"%s\"", $1);
1685 priqflags_list : priqflags_item { $$ |= $1; }
1686 | priqflags_list comma priqflags_item { $$ |= $3; }
1689 priqflags_item : STRING {
1690 if (!strcmp($1, "default"))
1691 $$ = PRCF_DEFAULTCLASS;
1692 else if (!strcmp($1, "red"))
1694 else if (!strcmp($1, "ecn"))
1695 $$ = PRCF_RED|PRCF_ECN;
1696 else if (!strcmp($1, "rio"))
1699 yyerror("unknown priq flag \"%s\"", $1);
1709 sizeof(struct node_hfsc_opts));
1716 hfscopts_list : hfscopts_item
1717 | hfscopts_list comma hfscopts_item
1720 hfscopts_item : LINKSHARE bandwidth {
1721 if (hfsc_opts.linkshare.used) {
1722 yyerror("linkshare already specified");
1725 hfsc_opts.linkshare.m2 = $2;
1726 hfsc_opts.linkshare.used = 1;
1728 | LINKSHARE '(' bandwidth comma NUMBER comma bandwidth ')'
1730 if ($5 < 0 || $5 > INT_MAX) {
1731 yyerror("timing in curve out of range");
1734 if (hfsc_opts.linkshare.used) {
1735 yyerror("linkshare already specified");
1738 hfsc_opts.linkshare.m1 = $3;
1739 hfsc_opts.linkshare.d = $5;
1740 hfsc_opts.linkshare.m2 = $7;
1741 hfsc_opts.linkshare.used = 1;
1743 | REALTIME bandwidth {
1744 if (hfsc_opts.realtime.used) {
1745 yyerror("realtime already specified");
1748 hfsc_opts.realtime.m2 = $2;
1749 hfsc_opts.realtime.used = 1;
1751 | REALTIME '(' bandwidth comma NUMBER comma bandwidth ')'
1753 if ($5 < 0 || $5 > INT_MAX) {
1754 yyerror("timing in curve out of range");
1757 if (hfsc_opts.realtime.used) {
1758 yyerror("realtime already specified");
1761 hfsc_opts.realtime.m1 = $3;
1762 hfsc_opts.realtime.d = $5;
1763 hfsc_opts.realtime.m2 = $7;
1764 hfsc_opts.realtime.used = 1;
1766 | UPPERLIMIT bandwidth {
1767 if (hfsc_opts.upperlimit.used) {
1768 yyerror("upperlimit already specified");
1771 hfsc_opts.upperlimit.m2 = $2;
1772 hfsc_opts.upperlimit.used = 1;
1774 | UPPERLIMIT '(' bandwidth comma NUMBER comma bandwidth ')'
1776 if ($5 < 0 || $5 > INT_MAX) {
1777 yyerror("timing in curve out of range");
1780 if (hfsc_opts.upperlimit.used) {
1781 yyerror("upperlimit already specified");
1784 hfsc_opts.upperlimit.m1 = $3;
1785 hfsc_opts.upperlimit.d = $5;
1786 hfsc_opts.upperlimit.m2 = $7;
1787 hfsc_opts.upperlimit.used = 1;
1790 if (!strcmp($1, "default"))
1791 hfsc_opts.flags |= HFCF_DEFAULTCLASS;
1792 else if (!strcmp($1, "red"))
1793 hfsc_opts.flags |= HFCF_RED;
1794 else if (!strcmp($1, "ecn"))
1795 hfsc_opts.flags |= HFCF_RED|HFCF_ECN;
1796 else if (!strcmp($1, "rio"))
1797 hfsc_opts.flags |= HFCF_RIO;
1799 yyerror("unknown hfsc flag \"%s\"", $1);
1807 qassign : /* empty */ { $$ = NULL; }
1808 | qassign_item { $$ = $1; }
1809 | '{' optnl qassign_list '}' { $$ = $3; }
1812 qassign_list : qassign_item optnl { $$ = $1; }
1813 | qassign_list comma qassign_item optnl {
1814 $1->tail->next = $3;
1820 qassign_item : STRING {
1821 $$ = calloc(1, sizeof(struct node_queue));
1823 err(1, "qassign_item: calloc");
1824 if (strlcpy($$->queue, $1, sizeof($$->queue)) >=
1825 sizeof($$->queue)) {
1826 yyerror("queue name '%s' too long (max "
1827 "%d chars)", $1, sizeof($$->queue)-1);
1838 pfrule : action dir logquick interface route af proto fromto
1842 struct node_state_opt *o;
1843 struct node_proto *proto;
1849 if (check_rulestate(PFCTL_STATE_FILTER))
1852 memset(&r, 0, sizeof(r));
1856 case PFRULE_RETURNRST:
1857 r.rule_flag |= PFRULE_RETURNRST;
1858 r.return_ttl = $1.w;
1860 case PFRULE_RETURNICMP:
1861 r.rule_flag |= PFRULE_RETURNICMP;
1862 r.return_icmp = $1.w;
1863 r.return_icmp6 = $1.w2;
1866 r.rule_flag |= PFRULE_RETURN;
1867 r.return_icmp = $1.w;
1868 r.return_icmp6 = $1.w2;
1876 r.rtableid = $9.rtableid;
1880 if (strlcpy(r.tagname, $9.tag,
1881 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1882 yyerror("tag too long, max %u chars",
1883 PF_TAG_NAME_SIZE - 1);
1887 if (strlcpy(r.match_tagname, $9.match_tag,
1888 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1889 yyerror("tag too long, max %u chars",
1890 PF_TAG_NAME_SIZE - 1);
1893 r.match_tag_not = $9.match_tag_not;
1894 if (rule_label(&r, $9.label))
1897 r.flags = $9.flags.b1;
1898 r.flagset = $9.flags.b2;
1899 if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
1900 yyerror("flags always false");
1903 if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
1904 for (proto = $7; proto != NULL &&
1905 proto->proto != IPPROTO_TCP;
1906 proto = proto->next)
1908 if (proto == NULL && $7 != NULL) {
1909 if ($9.flags.b1 || $9.flags.b2)
1911 "flags only apply to tcp");
1914 "OS fingerprinting only "
1919 if (($9.flags.b1 & parse_flags("S")) == 0 &&
1921 yyerror("OS fingerprinting requires "
1922 "the SYN TCP flag (flags S/SA)");
1929 r.keep_state = $9.keep.action;
1930 o = $9.keep.options;
1932 /* 'keep state' by default on pass rules. */
1933 if (!r.keep_state && !r.action &&
1934 !($9.marker & FOM_KEEP)) {
1935 r.keep_state = PF_STATE_NORMAL;
1936 o = keep_state_defaults;
1941 struct node_state_opt *p = o;
1944 case PF_STATE_OPT_MAX:
1946 yyerror("state option 'max' "
1947 "multiple definitions");
1950 r.max_states = o->data.max_states;
1952 case PF_STATE_OPT_NOSYNC:
1953 if (r.rule_flag & PFRULE_NOSYNC) {
1954 yyerror("state option 'sync' "
1955 "multiple definitions");
1958 r.rule_flag |= PFRULE_NOSYNC;
1960 case PF_STATE_OPT_SRCTRACK:
1962 yyerror("state option "
1964 "multiple definitions");
1967 srctrack = o->data.src_track;
1968 r.rule_flag |= PFRULE_SRCTRACK;
1970 case PF_STATE_OPT_MAX_SRC_STATES:
1971 if (r.max_src_states) {
1972 yyerror("state option "
1974 "multiple definitions");
1977 if (o->data.max_src_states == 0) {
1978 yyerror("'max-src-states' must "
1983 o->data.max_src_states;
1984 r.rule_flag |= PFRULE_SRCTRACK;
1986 case PF_STATE_OPT_OVERLOAD:
1987 if (r.overload_tblname[0]) {
1988 yyerror("multiple 'overload' "
1989 "table definitions");
1992 if (strlcpy(r.overload_tblname,
1993 o->data.overload.tblname,
1994 PF_TABLE_NAME_SIZE) >=
1995 PF_TABLE_NAME_SIZE) {
1996 yyerror("state option: "
2000 r.flush = o->data.overload.flush;
2002 case PF_STATE_OPT_MAX_SRC_CONN:
2003 if (r.max_src_conn) {
2004 yyerror("state option "
2006 "multiple definitions");
2009 if (o->data.max_src_conn == 0) {
2010 yyerror("'max-src-conn' "
2015 o->data.max_src_conn;
2016 r.rule_flag |= PFRULE_SRCTRACK |
2017 PFRULE_RULESRCTRACK;
2019 case PF_STATE_OPT_MAX_SRC_CONN_RATE:
2020 if (r.max_src_conn_rate.limit) {
2021 yyerror("state option "
2022 "'max-src-conn-rate' "
2023 "multiple definitions");
2026 if (!o->data.max_src_conn_rate.limit ||
2027 !o->data.max_src_conn_rate.seconds) {
2028 yyerror("'max-src-conn-rate' "
2029 "values must be > 0");
2032 if (o->data.max_src_conn_rate.limit >
2034 yyerror("'max-src-conn-rate' "
2035 "maximum rate must be < %u",
2039 r.max_src_conn_rate.limit =
2040 o->data.max_src_conn_rate.limit;
2041 r.max_src_conn_rate.seconds =
2042 o->data.max_src_conn_rate.seconds;
2043 r.rule_flag |= PFRULE_SRCTRACK |
2044 PFRULE_RULESRCTRACK;
2046 case PF_STATE_OPT_MAX_SRC_NODES:
2047 if (r.max_src_nodes) {
2048 yyerror("state option "
2050 "multiple definitions");
2053 if (o->data.max_src_nodes == 0) {
2054 yyerror("'max-src-nodes' must "
2059 o->data.max_src_nodes;
2060 r.rule_flag |= PFRULE_SRCTRACK |
2061 PFRULE_RULESRCTRACK;
2063 case PF_STATE_OPT_STATELOCK:
2065 yyerror("state locking option: "
2066 "multiple definitions");
2070 r.rule_flag |= o->data.statelock;
2072 case PF_STATE_OPT_SLOPPY:
2073 if (r.rule_flag & PFRULE_STATESLOPPY) {
2074 yyerror("state sloppy option: "
2075 "multiple definitions");
2078 r.rule_flag |= PFRULE_STATESLOPPY;
2080 case PF_STATE_OPT_PFLOW:
2081 if (r.rule_flag & PFRULE_PFLOW) {
2082 yyerror("state pflow "
2087 r.rule_flag |= PFRULE_PFLOW;
2089 case PF_STATE_OPT_TIMEOUT:
2090 if (o->data.timeout.number ==
2091 PFTM_ADAPTIVE_START ||
2092 o->data.timeout.number ==
2095 if (r.timeout[o->data.timeout.number]) {
2096 yyerror("state timeout %s "
2097 "multiple definitions",
2098 pf_timeouts[o->data.
2099 timeout.number].name);
2102 r.timeout[o->data.timeout.number] =
2103 o->data.timeout.seconds;
2110 /* 'flags S/SA' by default on stateful rules */
2111 if (!r.action && !r.flags && !r.flagset &&
2112 !$9.fragment && !($9.marker & FOM_FLAGS) &&
2114 r.flags = parse_flags("S");
2115 r.flagset = parse_flags("SA");
2117 if (!adaptive && r.max_states) {
2118 r.timeout[PFTM_ADAPTIVE_START] =
2119 (r.max_states / 10) * 6;
2120 r.timeout[PFTM_ADAPTIVE_END] =
2121 (r.max_states / 10) * 12;
2123 if (r.rule_flag & PFRULE_SRCTRACK) {
2124 if (srctrack == PF_SRCTRACK_GLOBAL &&
2126 yyerror("'max-src-nodes' is "
2127 "incompatible with "
2128 "'source-track global'");
2131 if (srctrack == PF_SRCTRACK_GLOBAL &&
2133 yyerror("'max-src-conn' is "
2134 "incompatible with "
2135 "'source-track global'");
2138 if (srctrack == PF_SRCTRACK_GLOBAL &&
2139 r.max_src_conn_rate.seconds) {
2140 yyerror("'max-src-conn-rate' is "
2141 "incompatible with "
2142 "'source-track global'");
2145 if (r.timeout[PFTM_SRC_NODE] <
2146 r.max_src_conn_rate.seconds)
2147 r.timeout[PFTM_SRC_NODE] =
2148 r.max_src_conn_rate.seconds;
2149 r.rule_flag |= PFRULE_SRCTRACK;
2150 if (srctrack == PF_SRCTRACK_RULE)
2151 r.rule_flag |= PFRULE_RULESRCTRACK;
2153 if (r.keep_state && !statelock)
2154 r.rule_flag |= default_statelock;
2157 r.rule_flag |= PFRULE_FRAGMENT;
2158 r.allow_opts = $9.allowopts;
2160 decide_address_family($8.src.host, &r.af);
2161 decide_address_family($8.dst.host, &r.af);
2165 yyerror("direction must be explicit "
2166 "with rules that specify routing");
2170 r.rpool.opts = $5.pool_opts;
2172 memcpy(&r.rpool.key, $5.key,
2173 sizeof(struct pf_poolhashkey));
2175 if (r.rt && r.rt != PF_FASTROUTE) {
2176 decide_address_family($5.host, &r.af);
2177 remove_invalid_hosts(&$5.host, &r.af);
2178 if ($5.host == NULL) {
2179 yyerror("no routing address with "
2180 "matching address family found.");
2183 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
2184 PF_POOL_NONE && ($5.host->next != NULL ||
2185 $5.host->addr.type == PF_ADDR_TABLE ||
2186 DYNIF_MULTIADDR($5.host->addr)))
2187 r.rpool.opts |= PF_POOL_ROUNDROBIN;
2188 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2189 PF_POOL_ROUNDROBIN &&
2190 disallow_table($5.host, "tables are only "
2191 "supported in round-robin routing pools"))
2193 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2194 PF_POOL_ROUNDROBIN &&
2195 disallow_alias($5.host, "interface (%s) "
2196 "is only supported in round-robin "
2199 if ($5.host->next != NULL) {
2200 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2201 PF_POOL_ROUNDROBIN) {
2202 yyerror("r.rpool.opts must "
2203 "be PF_POOL_ROUNDROBIN");
2208 if ($9.queues.qname != NULL) {
2209 if (strlcpy(r.qname, $9.queues.qname,
2210 sizeof(r.qname)) >= sizeof(r.qname)) {
2211 yyerror("rule qname too long (max "
2212 "%d chars)", sizeof(r.qname)-1);
2215 free($9.queues.qname);
2217 if ($9.queues.pqname != NULL) {
2218 if (strlcpy(r.pqname, $9.queues.pqname,
2219 sizeof(r.pqname)) >= sizeof(r.pqname)) {
2220 yyerror("rule pqname too long (max "
2221 "%d chars)", sizeof(r.pqname)-1);
2224 free($9.queues.pqname);
2227 r.divert.port = $9.divert.port;
2229 if ((r.divert.port = $9.divert.port)) {
2230 if (r.direction == PF_OUT) {
2231 if ($9.divert.addr) {
2232 yyerror("address specified "
2233 "for outgoing divert");
2236 bzero(&r.divert.addr,
2237 sizeof(r.divert.addr));
2239 if (!$9.divert.addr) {
2240 yyerror("no address specified "
2241 "for incoming divert");
2244 if ($9.divert.addr->af != r.af) {
2245 yyerror("address family "
2246 "mismatch for divert");
2250 $9.divert.addr->addr.v.a.addr;
2255 expand_rule(&r, $4, $5.host, $7, $8.src_os,
2256 $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
2257 $9.uid, $9.gid, $9.icmpspec, "");
2262 bzero(&filter_opts, sizeof filter_opts);
2263 filter_opts.rtableid = -1;
2266 { $$ = filter_opts; }
2268 bzero(&filter_opts, sizeof filter_opts);
2269 filter_opts.rtableid = -1;
2274 filter_opts_l : filter_opts_l filter_opt
2278 filter_opt : USER uids {
2279 if (filter_opts.uid)
2280 $2->tail->next = filter_opts.uid;
2281 filter_opts.uid = $2;
2284 if (filter_opts.gid)
2285 $2->tail->next = filter_opts.gid;
2286 filter_opts.gid = $2;
2289 if (filter_opts.marker & FOM_FLAGS) {
2290 yyerror("flags cannot be redefined");
2293 filter_opts.marker |= FOM_FLAGS;
2294 filter_opts.flags.b1 |= $1.b1;
2295 filter_opts.flags.b2 |= $1.b2;
2296 filter_opts.flags.w |= $1.w;
2297 filter_opts.flags.w2 |= $1.w2;
2300 if (filter_opts.marker & FOM_ICMP) {
2301 yyerror("icmp-type cannot be redefined");
2304 filter_opts.marker |= FOM_ICMP;
2305 filter_opts.icmpspec = $1;
2308 if (filter_opts.marker & FOM_TOS) {
2309 yyerror("tos cannot be redefined");
2312 filter_opts.marker |= FOM_TOS;
2313 filter_opts.tos = $2;
2316 if (filter_opts.marker & FOM_KEEP) {
2317 yyerror("modulate or keep cannot be redefined");
2320 filter_opts.marker |= FOM_KEEP;
2321 filter_opts.keep.action = $1.action;
2322 filter_opts.keep.options = $1.options;
2325 filter_opts.fragment = 1;
2328 filter_opts.allowopts = 1;
2331 if (filter_opts.label) {
2332 yyerror("label cannot be redefined");
2335 filter_opts.label = $1;
2338 if (filter_opts.queues.qname) {
2339 yyerror("queue cannot be redefined");
2342 filter_opts.queues = $1;
2345 filter_opts.tag = $2;
2347 | not TAGGED string {
2348 filter_opts.match_tag = $3;
2349 filter_opts.match_tag_not = $1;
2351 | PROBABILITY probability {
2354 p = floor($2 * UINT_MAX + 0.5);
2355 if (p < 0.0 || p > UINT_MAX) {
2356 yyerror("invalid probability: %lf", p);
2359 filter_opts.prob = (u_int32_t)p;
2360 if (filter_opts.prob == 0)
2361 filter_opts.prob = 1;
2364 if ($2 < 0 /* || $2 > RT_TABLEID_MAX */ ) {
2365 yyerror("invalid rtable id");
2368 filter_opts.rtableid = $2;
2370 | DIVERTTO portplain {
2372 filter_opts.divert.port = $2.a;
2373 if (!filter_opts.divert.port) {
2374 yyerror("invalid divert port: %u", ntohs($2.a));
2379 | DIVERTTO STRING PORT portplain {
2381 if ((filter_opts.divert.addr = host($2)) == NULL) {
2382 yyerror("could not parse divert address: %s",
2391 filter_opts.divert.port = $4.a;
2392 if (!filter_opts.divert.port) {
2393 yyerror("invalid divert port: %u", ntohs($4.a));
2399 yyerror("divert-reply has no meaning in FreeBSD pf(4)");
2402 filter_opts.divert.port = 1; /* some random value */
2407 probability : STRING {
2409 double p = strtod($1, &e);
2416 yyerror("invalid probability: %s", $1);
2429 action : PASS { $$.b1 = PF_PASS; $$.b2 = $$.w = 0; }
2430 | BLOCK blockspec { $$ = $2; $$.b1 = PF_DROP; }
2433 blockspec : /* empty */ {
2434 $$.b2 = blockpolicy;
2435 $$.w = returnicmpdefault;
2436 $$.w2 = returnicmp6default;
2439 $$.b2 = PFRULE_DROP;
2444 $$.b2 = PFRULE_RETURNRST;
2448 | RETURNRST '(' TTL NUMBER ')' {
2449 if ($4 < 0 || $4 > 255) {
2450 yyerror("illegal ttl value %d", $4);
2453 $$.b2 = PFRULE_RETURNRST;
2458 $$.b2 = PFRULE_RETURNICMP;
2459 $$.w = returnicmpdefault;
2460 $$.w2 = returnicmp6default;
2463 $$.b2 = PFRULE_RETURNICMP;
2464 $$.w = returnicmpdefault;
2465 $$.w2 = returnicmp6default;
2467 | RETURNICMP '(' reticmpspec ')' {
2468 $$.b2 = PFRULE_RETURNICMP;
2470 $$.w2 = returnicmpdefault;
2472 | RETURNICMP6 '(' reticmp6spec ')' {
2473 $$.b2 = PFRULE_RETURNICMP;
2474 $$.w = returnicmpdefault;
2477 | RETURNICMP '(' reticmpspec comma reticmp6spec ')' {
2478 $$.b2 = PFRULE_RETURNICMP;
2483 $$.b2 = PFRULE_RETURN;
2484 $$.w = returnicmpdefault;
2485 $$.w2 = returnicmp6default;
2489 reticmpspec : STRING {
2490 if (!($$ = parseicmpspec($1, AF_INET))) {
2499 if ($1 < 0 || $1 > 255) {
2500 yyerror("invalid icmp code %lu", $1);
2503 icmptype = returnicmpdefault >> 8;
2504 $$ = (icmptype << 8 | $1);
2508 reticmp6spec : STRING {
2509 if (!($$ = parseicmpspec($1, AF_INET6))) {
2518 if ($1 < 0 || $1 > 255) {
2519 yyerror("invalid icmp code %lu", $1);
2522 icmptype = returnicmp6default >> 8;
2523 $$ = (icmptype << 8 | $1);
2527 dir : /* empty */ { $$ = PF_INOUT; }
2528 | IN { $$ = PF_IN; }
2529 | OUT { $$ = PF_OUT; }
2532 quick : /* empty */ { $$.quick = 0; }
2533 | QUICK { $$.quick = 1; }
2536 logquick : /* empty */ { $$.log = 0; $$.quick = 0; $$.logif = 0; }
2537 | log { $$ = $1; $$.quick = 0; }
2538 | QUICK { $$.quick = 1; $$.log = 0; $$.logif = 0; }
2539 | log QUICK { $$ = $1; $$.quick = 1; }
2540 | QUICK log { $$ = $2; $$.quick = 1; }
2543 log : LOG { $$.log = PF_LOG; $$.logif = 0; }
2544 | LOG '(' logopts ')' {
2545 $$.log = PF_LOG | $3.log;
2546 $$.logif = $3.logif;
2550 logopts : logopt { $$ = $1; }
2551 | logopts comma logopt {
2552 $$.log = $1.log | $3.log;
2553 $$.logif = $3.logif;
2555 $$.logif = $1.logif;
2559 logopt : ALL { $$.log = PF_LOG_ALL; $$.logif = 0; }
2560 | USER { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
2561 | GROUP { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
2567 if (strncmp($2, "pflog", 5)) {
2568 yyerror("%s: should be a pflog interface", $2);
2572 i = strtonum($2 + 5, 0, 255, &errstr);
2574 yyerror("%s: %s", $2, errstr);
2583 interface : /* empty */ { $$ = NULL; }
2584 | ON if_item_not { $$ = $2; }
2585 | ON '{' optnl if_list '}' { $$ = $4; }
2588 if_list : if_item_not optnl { $$ = $1; }
2589 | if_list comma if_item_not optnl {
2590 $1->tail->next = $3;
2596 if_item_not : not if_item { $$ = $2; $$->not = $1; }
2600 struct node_host *n;
2602 $$ = calloc(1, sizeof(struct node_if));
2604 err(1, "if_item: calloc");
2605 if (strlcpy($$->ifname, $1, sizeof($$->ifname)) >=
2606 sizeof($$->ifname)) {
2609 yyerror("interface name too long");
2613 if ((n = ifa_exists($1)) != NULL)
2614 $$->ifa_flags = n->ifa_flags;
2623 af : /* empty */ { $$ = 0; }
2624 | INET { $$ = AF_INET; }
2625 | INET6 { $$ = AF_INET6; }
2628 proto : /* empty */ { $$ = NULL; }
2629 | PROTO proto_item { $$ = $2; }
2630 | PROTO '{' optnl proto_list '}' { $$ = $4; }
2633 proto_list : proto_item optnl { $$ = $1; }
2634 | proto_list comma proto_item optnl {
2635 $1->tail->next = $3;
2641 proto_item : protoval {
2646 yyerror("proto 0 cannot be used");
2649 $$ = calloc(1, sizeof(struct node_proto));
2651 err(1, "proto_item: calloc");
2661 p = getprotobyname($1);
2663 yyerror("unknown protocol %s", $1);
2671 if ($1 < 0 || $1 > 255) {
2672 yyerror("protocol outside range");
2692 os : /* empty */ { $$ = NULL; }
2693 | OS xos { $$ = $2; }
2694 | OS '{' optnl os_list '}' { $$ = $4; }
2698 $$ = calloc(1, sizeof(struct node_os));
2700 err(1, "os: calloc");
2706 os_list : xos optnl { $$ = $1; }
2707 | os_list comma xos optnl {
2708 $1->tail->next = $3;
2714 from : /* empty */ {
2728 if (disallow_urpf_failed($2.host, "\"urpf-failed\" is "
2729 "not permitted in a destination address"))
2735 ipportspec : ipspec {
2739 | ipspec PORT portspec {
2753 ipspec : ANY { $$ = NULL; }
2754 | xhost { $$ = $1; }
2755 | '{' optnl host_list '}' { $$ = $3; }
2758 toipspec : TO ipspec { $$ = $2; }
2759 | /* empty */ { $$ = NULL; }
2762 host_list : ipspec optnl { $$ = $1; }
2763 | host_list comma ipspec optnl {
2766 else if ($1 == NULL)
2769 $1->tail->next = $3;
2770 $1->tail = $3->tail;
2777 struct node_host *n;
2779 for (n = $2; n != NULL; n = n->next)
2784 $$ = calloc(1, sizeof(struct node_host));
2786 err(1, "xhost: calloc");
2787 $$->addr.type = PF_ADDR_NOROUTE;
2793 $$ = calloc(1, sizeof(struct node_host));
2795 err(1, "xhost: calloc");
2796 $$->addr.type = PF_ADDR_URPFFAILED;
2804 if (($$ = host($1)) == NULL) {
2805 /* error. "any" is handled elsewhere */
2807 yyerror("could not parse host specification");
2813 | STRING '-' STRING {
2814 struct node_host *b, *e;
2816 if ((b = host($1)) == NULL || (e = host($3)) == NULL) {
2819 yyerror("could not parse host specification");
2822 if (b->af != e->af ||
2823 b->addr.type != PF_ADDR_ADDRMASK ||
2824 e->addr.type != PF_ADDR_ADDRMASK ||
2825 unmask(&b->addr.v.a.mask, b->af) !=
2826 (b->af == AF_INET ? 32 : 128) ||
2827 unmask(&e->addr.v.a.mask, e->af) !=
2828 (e->af == AF_INET ? 32 : 128) ||
2829 b->next != NULL || b->not ||
2830 e->next != NULL || e->not) {
2835 yyerror("invalid address range");
2838 memcpy(&b->addr.v.a.mask, &e->addr.v.a.addr,
2839 sizeof(b->addr.v.a.mask));
2840 b->addr.type = PF_ADDR_RANGE;
2846 | STRING '/' NUMBER {
2849 if (asprintf(&buf, "%s/%lld", $1, (long long)$3) == -1)
2850 err(1, "host: asprintf");
2852 if (($$ = host(buf)) == NULL) {
2853 /* error. "any" is handled elsewhere */
2855 yyerror("could not parse host specification");
2860 | NUMBER '/' NUMBER {
2863 /* ie. for 10/8 parsing */
2865 if (asprintf(&buf, "%lld/%lld", (long long)$1, (long long)$3) == -1)
2867 if (asprintf(&buf, "%lld/%lld", $1, $3) == -1)
2869 err(1, "host: asprintf");
2870 if (($$ = host(buf)) == NULL) {
2871 /* error. "any" is handled elsewhere */
2873 yyerror("could not parse host specification");
2879 | dynaddr '/' NUMBER {
2880 struct node_host *n;
2882 if ($3 < 0 || $3 > 128) {
2883 yyerror("bit number too big");
2887 for (n = $1; n != NULL; n = n->next)
2891 if (strlen($2) >= PF_TABLE_NAME_SIZE) {
2892 yyerror("table name '%s' too long", $2);
2896 $$ = calloc(1, sizeof(struct node_host));
2898 err(1, "host: calloc");
2899 $$->addr.type = PF_ADDR_TABLE;
2900 if (strlcpy($$->addr.v.tblname, $2,
2901 sizeof($$->addr.v.tblname)) >=
2902 sizeof($$->addr.v.tblname))
2903 errx(1, "host: strlcpy");
2909 $$ = calloc(1, sizeof(struct node_host));
2912 err(1, "host: calloc");
2914 $$->addr.type = PF_ADDR_RTLABEL;
2915 if (strlcpy($$->addr.v.rtlabelname, $2,
2916 sizeof($$->addr.v.rtlabelname)) >=
2917 sizeof($$->addr.v.rtlabelname)) {
2918 yyerror("route label too long, max %u chars",
2919 sizeof($$->addr.v.rtlabelname) - 1);
2934 if (atoul($1, &ulval) == -1) {
2935 yyerror("%s is not a number", $1);
2944 dynaddr : '(' STRING ')' {
2949 if (!isalpha(op[0])) {
2950 yyerror("invalid interface name '%s'", op);
2954 while ((p = strrchr($2, ':')) != NULL) {
2955 if (!strcmp(p+1, "network"))
2956 flags |= PFI_AFLAG_NETWORK;
2957 else if (!strcmp(p+1, "broadcast"))
2958 flags |= PFI_AFLAG_BROADCAST;
2959 else if (!strcmp(p+1, "peer"))
2960 flags |= PFI_AFLAG_PEER;
2961 else if (!strcmp(p+1, "0"))
2962 flags |= PFI_AFLAG_NOALIAS;
2964 yyerror("interface %s has bad modifier",
2971 if (flags & (flags - 1) & PFI_AFLAG_MODEMASK) {
2973 yyerror("illegal combination of "
2974 "interface modifiers");
2977 $$ = calloc(1, sizeof(struct node_host));
2979 err(1, "address: calloc");
2981 set_ipmask($$, 128);
2982 $$->addr.type = PF_ADDR_DYNIFTL;
2983 $$->addr.iflags = flags;
2984 if (strlcpy($$->addr.v.ifname, $2,
2985 sizeof($$->addr.v.ifname)) >=
2986 sizeof($$->addr.v.ifname)) {
2989 yyerror("interface name too long");
2998 portspec : port_item { $$ = $1; }
2999 | '{' optnl port_list '}' { $$ = $3; }
3002 port_list : port_item optnl { $$ = $1; }
3003 | port_list comma port_item optnl {
3004 $1->tail->next = $3;
3010 port_item : portrange {
3011 $$ = calloc(1, sizeof(struct node_port));
3013 err(1, "port_item: calloc");
3023 | unaryop portrange {
3025 yyerror("':' cannot be used with an other "
3029 $$ = calloc(1, sizeof(struct node_port));
3031 err(1, "port_item: calloc");
3038 | portrange PORTBINARY portrange {
3040 yyerror("':' cannot be used with an other "
3044 $$ = calloc(1, sizeof(struct node_port));
3046 err(1, "port_item: calloc");
3055 portplain : numberstring {
3056 if (parseport($1, &$$, 0) == -1) {
3064 portrange : numberstring {
3065 if (parseport($1, &$$, PPORT_RANGE) == -1) {
3073 uids : uid_item { $$ = $1; }
3074 | '{' optnl uid_list '}' { $$ = $3; }
3077 uid_list : uid_item optnl { $$ = $1; }
3078 | uid_list comma uid_item optnl {
3079 $1->tail->next = $3;
3086 $$ = calloc(1, sizeof(struct node_uid));
3088 err(1, "uid_item: calloc");
3096 if ($2 == UID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
3097 yyerror("user unknown requires operator = or "
3101 $$ = calloc(1, sizeof(struct node_uid));
3103 err(1, "uid_item: calloc");
3110 | uid PORTBINARY uid {
3111 if ($1 == UID_MAX || $3 == UID_MAX) {
3112 yyerror("user unknown requires operator = or "
3116 $$ = calloc(1, sizeof(struct node_uid));
3118 err(1, "uid_item: calloc");
3128 if (!strcmp($1, "unknown"))
3133 if ((pw = getpwnam($1)) == NULL) {
3134 yyerror("unknown user %s", $1);
3143 if ($1 < 0 || $1 >= UID_MAX) {
3144 yyerror("illegal uid value %lu", $1);
3151 gids : gid_item { $$ = $1; }
3152 | '{' optnl gid_list '}' { $$ = $3; }
3155 gid_list : gid_item optnl { $$ = $1; }
3156 | gid_list comma gid_item optnl {
3157 $1->tail->next = $3;
3164 $$ = calloc(1, sizeof(struct node_gid));
3166 err(1, "gid_item: calloc");
3174 if ($2 == GID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
3175 yyerror("group unknown requires operator = or "
3179 $$ = calloc(1, sizeof(struct node_gid));
3181 err(1, "gid_item: calloc");
3188 | gid PORTBINARY gid {
3189 if ($1 == GID_MAX || $3 == GID_MAX) {
3190 yyerror("group unknown requires operator = or "
3194 $$ = calloc(1, sizeof(struct node_gid));
3196 err(1, "gid_item: calloc");
3206 if (!strcmp($1, "unknown"))
3211 if ((grp = getgrnam($1)) == NULL) {
3212 yyerror("unknown group %s", $1);
3221 if ($1 < 0 || $1 >= GID_MAX) {
3222 yyerror("illegal gid value %lu", $1);
3232 if ((f = parse_flags($1)) < 0) {
3233 yyerror("bad flags %s", $1);
3242 flags : FLAGS flag '/' flag { $$.b1 = $2.b1; $$.b2 = $4.b1; }
3243 | FLAGS '/' flag { $$.b1 = 0; $$.b2 = $3.b1; }
3244 | FLAGS ANY { $$.b1 = 0; $$.b2 = 0; }
3247 icmpspec : ICMPTYPE icmp_item { $$ = $2; }
3248 | ICMPTYPE '{' optnl icmp_list '}' { $$ = $4; }
3249 | ICMP6TYPE icmp6_item { $$ = $2; }
3250 | ICMP6TYPE '{' optnl icmp6_list '}' { $$ = $4; }
3253 icmp_list : icmp_item optnl { $$ = $1; }
3254 | icmp_list comma icmp_item optnl {
3255 $1->tail->next = $3;
3261 icmp6_list : icmp6_item optnl { $$ = $1; }
3262 | icmp6_list comma icmp6_item optnl {
3263 $1->tail->next = $3;
3269 icmp_item : icmptype {
3270 $$ = calloc(1, sizeof(struct node_icmp));
3272 err(1, "icmp_item: calloc");
3275 $$->proto = IPPROTO_ICMP;
3279 | icmptype CODE STRING {
3280 const struct icmpcodeent *p;
3282 if ((p = geticmpcodebyname($1-1, $3, AF_INET)) == NULL) {
3283 yyerror("unknown icmp-code %s", $3);
3289 $$ = calloc(1, sizeof(struct node_icmp));
3291 err(1, "icmp_item: calloc");
3293 $$->code = p->code + 1;
3294 $$->proto = IPPROTO_ICMP;
3298 | icmptype CODE NUMBER {
3299 if ($3 < 0 || $3 > 255) {
3300 yyerror("illegal icmp-code %lu", $3);
3303 $$ = calloc(1, sizeof(struct node_icmp));
3305 err(1, "icmp_item: calloc");
3308 $$->proto = IPPROTO_ICMP;
3314 icmp6_item : icmp6type {
3315 $$ = calloc(1, sizeof(struct node_icmp));
3317 err(1, "icmp_item: calloc");
3320 $$->proto = IPPROTO_ICMPV6;
3324 | icmp6type CODE STRING {
3325 const struct icmpcodeent *p;
3327 if ((p = geticmpcodebyname($1-1, $3, AF_INET6)) == NULL) {
3328 yyerror("unknown icmp6-code %s", $3);
3334 $$ = calloc(1, sizeof(struct node_icmp));
3336 err(1, "icmp_item: calloc");
3338 $$->code = p->code + 1;
3339 $$->proto = IPPROTO_ICMPV6;
3343 | icmp6type CODE NUMBER {
3344 if ($3 < 0 || $3 > 255) {
3345 yyerror("illegal icmp-code %lu", $3);
3348 $$ = calloc(1, sizeof(struct node_icmp));
3350 err(1, "icmp_item: calloc");
3353 $$->proto = IPPROTO_ICMPV6;
3360 const struct icmptypeent *p;
3362 if ((p = geticmptypebyname($1, AF_INET)) == NULL) {
3363 yyerror("unknown icmp-type %s", $1);
3371 if ($1 < 0 || $1 > 255) {
3372 yyerror("illegal icmp-type %lu", $1);
3379 icmp6type : STRING {
3380 const struct icmptypeent *p;
3382 if ((p = geticmptypebyname($1, AF_INET6)) ==
3384 yyerror("unknown icmp6-type %s", $1);
3392 if ($1 < 0 || $1 > 255) {
3393 yyerror("illegal icmp6-type %lu", $1);
3401 if (!strcmp($1, "lowdelay"))
3402 $$ = IPTOS_LOWDELAY;
3403 else if (!strcmp($1, "throughput"))
3404 $$ = IPTOS_THROUGHPUT;
3405 else if (!strcmp($1, "reliability"))
3406 $$ = IPTOS_RELIABILITY;
3407 else if ($1[0] == '0' && $1[1] == 'x')
3408 $$ = strtoul($1, NULL, 16);
3410 $$ = 0; /* flag bad argument */
3411 if (!$$ || $$ > 255) {
3412 yyerror("illegal tos value %s", $1);
3420 if (!$$ || $$ > 255) {
3421 yyerror("illegal tos value %s", $1);
3427 sourcetrack : SOURCETRACK { $$ = PF_SRCTRACK; }
3428 | SOURCETRACK GLOBAL { $$ = PF_SRCTRACK_GLOBAL; }
3429 | SOURCETRACK RULE { $$ = PF_SRCTRACK_RULE; }
3432 statelock : IFBOUND {
3433 $$ = PFRULE_IFBOUND;
3444 | KEEP STATE state_opt_spec {
3445 $$.action = PF_STATE_NORMAL;
3448 | MODULATE STATE state_opt_spec {
3449 $$.action = PF_STATE_MODULATE;
3452 | SYNPROXY STATE state_opt_spec {
3453 $$.action = PF_STATE_SYNPROXY;
3458 flush : /* empty */ { $$ = 0; }
3459 | FLUSH { $$ = PF_FLUSH; }
3461 $$ = PF_FLUSH | PF_FLUSH_GLOBAL;
3465 state_opt_spec : '(' state_opt_list ')' { $$ = $2; }
3466 | /* empty */ { $$ = NULL; }
3469 state_opt_list : state_opt_item { $$ = $1; }
3470 | state_opt_list comma state_opt_item {
3471 $1->tail->next = $3;
3477 state_opt_item : MAXIMUM NUMBER {
3478 if ($2 < 0 || $2 > UINT_MAX) {
3479 yyerror("only positive values permitted");
3482 $$ = calloc(1, sizeof(struct node_state_opt));
3484 err(1, "state_opt_item: calloc");
3485 $$->type = PF_STATE_OPT_MAX;
3486 $$->data.max_states = $2;
3491 $$ = calloc(1, sizeof(struct node_state_opt));
3493 err(1, "state_opt_item: calloc");
3494 $$->type = PF_STATE_OPT_NOSYNC;
3498 | MAXSRCSTATES NUMBER {
3499 if ($2 < 0 || $2 > UINT_MAX) {
3500 yyerror("only positive values permitted");
3503 $$ = calloc(1, sizeof(struct node_state_opt));
3505 err(1, "state_opt_item: calloc");
3506 $$->type = PF_STATE_OPT_MAX_SRC_STATES;
3507 $$->data.max_src_states = $2;
3511 | MAXSRCCONN NUMBER {
3512 if ($2 < 0 || $2 > UINT_MAX) {
3513 yyerror("only positive values permitted");
3516 $$ = calloc(1, sizeof(struct node_state_opt));
3518 err(1, "state_opt_item: calloc");
3519 $$->type = PF_STATE_OPT_MAX_SRC_CONN;
3520 $$->data.max_src_conn = $2;
3524 | MAXSRCCONNRATE NUMBER '/' NUMBER {
3525 if ($2 < 0 || $2 > UINT_MAX ||
3526 $4 < 0 || $4 > UINT_MAX) {
3527 yyerror("only positive values permitted");
3530 $$ = calloc(1, sizeof(struct node_state_opt));
3532 err(1, "state_opt_item: calloc");
3533 $$->type = PF_STATE_OPT_MAX_SRC_CONN_RATE;
3534 $$->data.max_src_conn_rate.limit = $2;
3535 $$->data.max_src_conn_rate.seconds = $4;
3539 | OVERLOAD '<' STRING '>' flush {
3540 if (strlen($3) >= PF_TABLE_NAME_SIZE) {
3541 yyerror("table name '%s' too long", $3);
3545 $$ = calloc(1, sizeof(struct node_state_opt));
3547 err(1, "state_opt_item: calloc");
3548 if (strlcpy($$->data.overload.tblname, $3,
3549 PF_TABLE_NAME_SIZE) >= PF_TABLE_NAME_SIZE)
3550 errx(1, "state_opt_item: strlcpy");
3552 $$->type = PF_STATE_OPT_OVERLOAD;
3553 $$->data.overload.flush = $5;
3557 | MAXSRCNODES NUMBER {
3558 if ($2 < 0 || $2 > UINT_MAX) {
3559 yyerror("only positive values permitted");
3562 $$ = calloc(1, sizeof(struct node_state_opt));
3564 err(1, "state_opt_item: calloc");
3565 $$->type = PF_STATE_OPT_MAX_SRC_NODES;
3566 $$->data.max_src_nodes = $2;
3571 $$ = calloc(1, sizeof(struct node_state_opt));
3573 err(1, "state_opt_item: calloc");
3574 $$->type = PF_STATE_OPT_SRCTRACK;
3575 $$->data.src_track = $1;
3580 $$ = calloc(1, sizeof(struct node_state_opt));
3582 err(1, "state_opt_item: calloc");
3583 $$->type = PF_STATE_OPT_STATELOCK;
3584 $$->data.statelock = $1;
3589 $$ = calloc(1, sizeof(struct node_state_opt));
3591 err(1, "state_opt_item: calloc");
3592 $$->type = PF_STATE_OPT_SLOPPY;
3597 $$ = calloc(1, sizeof(struct node_state_opt));
3599 err(1, "state_opt_item: calloc");
3600 $$->type = PF_STATE_OPT_PFLOW;
3607 if ($2 < 0 || $2 > UINT_MAX) {
3608 yyerror("only positive values permitted");
3611 for (i = 0; pf_timeouts[i].name &&
3612 strcmp(pf_timeouts[i].name, $1); ++i)
3614 if (!pf_timeouts[i].name) {
3615 yyerror("illegal timeout name %s", $1);
3619 if (strchr(pf_timeouts[i].name, '.') == NULL) {
3620 yyerror("illegal state timeout %s", $1);
3625 $$ = calloc(1, sizeof(struct node_state_opt));
3627 err(1, "state_opt_item: calloc");
3628 $$->type = PF_STATE_OPT_TIMEOUT;
3629 $$->data.timeout.number = pf_timeouts[i].timeout;
3630 $$->data.timeout.seconds = $2;
3636 label : LABEL STRING {
3641 qname : QUEUE STRING {
3645 | QUEUE '(' STRING ')' {
3649 | QUEUE '(' STRING comma STRING ')' {
3655 no : /* empty */ { $$ = 0; }
3659 portstar : numberstring {
3660 if (parseport($1, &$$, PPORT_RANGE|PPORT_STAR) == -1) {
3668 redirspec : host { $$ = $1; }
3669 | '{' optnl redir_host_list '}' { $$ = $3; }
3672 redir_host_list : host optnl { $$ = $1; }
3673 | redir_host_list comma host optnl {
3674 $1->tail->next = $3;
3675 $1->tail = $3->tail;
3680 redirpool : /* empty */ { $$ = NULL; }
3682 $$ = calloc(1, sizeof(struct redirection));
3684 err(1, "redirection: calloc");
3686 $$->rport.a = $$->rport.b = $$->rport.t = 0;
3688 | ARROW redirspec PORT portstar {
3689 $$ = calloc(1, sizeof(struct redirection));
3691 err(1, "redirection: calloc");
3697 hashkey : /* empty */
3699 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3701 err(1, "hashkey: calloc");
3702 $$->key32[0] = arc4random();
3703 $$->key32[1] = arc4random();
3704 $$->key32[2] = arc4random();
3705 $$->key32[3] = arc4random();
3709 if (!strncmp($1, "0x", 2)) {
3710 if (strlen($1) != 34) {
3712 yyerror("hex key must be 128 bits "
3713 "(32 hex digits) long");
3716 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3718 err(1, "hashkey: calloc");
3720 if (sscanf($1, "0x%8x%8x%8x%8x",
3721 &$$->key32[0], &$$->key32[1],
3722 &$$->key32[2], &$$->key32[3]) != 4) {
3725 yyerror("invalid hex key");
3731 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3733 err(1, "hashkey: calloc");
3735 MD5Update(&context, (unsigned char *)$1,
3737 MD5Final((unsigned char *)$$, &context);
3738 HTONL($$->key32[0]);
3739 HTONL($$->key32[1]);
3740 HTONL($$->key32[2]);
3741 HTONL($$->key32[3]);
3747 pool_opts : { bzero(&pool_opts, sizeof pool_opts); }
3751 bzero(&pool_opts, sizeof pool_opts);
3756 pool_opts_l : pool_opts_l pool_opt
3760 pool_opt : BITMASK {
3761 if (pool_opts.type) {
3762 yyerror("pool type cannot be redefined");
3765 pool_opts.type = PF_POOL_BITMASK;
3768 if (pool_opts.type) {
3769 yyerror("pool type cannot be redefined");
3772 pool_opts.type = PF_POOL_RANDOM;
3774 | SOURCEHASH hashkey {
3775 if (pool_opts.type) {
3776 yyerror("pool type cannot be redefined");
3779 pool_opts.type = PF_POOL_SRCHASH;
3783 if (pool_opts.type) {
3784 yyerror("pool type cannot be redefined");
3787 pool_opts.type = PF_POOL_ROUNDROBIN;
3790 if (pool_opts.staticport) {
3791 yyerror("static-port cannot be redefined");
3794 pool_opts.staticport = 1;
3797 if (filter_opts.marker & POM_STICKYADDRESS) {
3798 yyerror("sticky-address cannot be redefined");
3801 pool_opts.marker |= POM_STICKYADDRESS;
3802 pool_opts.opts |= PF_POOL_STICKYADDR;
3806 redirection : /* empty */ { $$ = NULL; }
3808 $$ = calloc(1, sizeof(struct redirection));
3810 err(1, "redirection: calloc");
3812 $$->rport.a = $$->rport.b = $$->rport.t = 0;
3814 | ARROW host PORT portstar {
3815 $$ = calloc(1, sizeof(struct redirection));
3817 err(1, "redirection: calloc");
3823 natpasslog : /* empty */ { $$.b1 = $$.b2 = 0; $$.w2 = 0; }
3824 | PASS { $$.b1 = 1; $$.b2 = 0; $$.w2 = 0; }
3825 | PASS log { $$.b1 = 1; $$.b2 = $2.log; $$.w2 = $2.logif; }
3826 | log { $$.b1 = 0; $$.b2 = $1.log; $$.w2 = $1.logif; }
3829 nataction : no NAT natpasslog {
3831 yyerror("\"pass\" not valid with \"no\"");
3842 | no RDR natpasslog {
3844 yyerror("\"pass\" not valid with \"no\"");
3857 natrule : nataction interface af proto fromto tag tagged rtable
3862 if (check_rulestate(PFCTL_STATE_NAT))
3865 memset(&r, 0, sizeof(r));
3874 if ($5.src.host && $5.src.host->af &&
3875 !$5.src.host->ifindex)
3876 r.af = $5.src.host->af;
3877 else if ($5.dst.host && $5.dst.host->af &&
3878 !$5.dst.host->ifindex)
3879 r.af = $5.dst.host->af;
3883 if (strlcpy(r.tagname, $6, PF_TAG_NAME_SIZE) >=
3885 yyerror("tag too long, max %u chars",
3886 PF_TAG_NAME_SIZE - 1);
3891 if (strlcpy(r.match_tagname, $7.name,
3892 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
3893 yyerror("tag too long, max %u chars",
3894 PF_TAG_NAME_SIZE - 1);
3897 r.match_tag_not = $7.neg;
3900 if (r.action == PF_NONAT || r.action == PF_NORDR) {
3902 yyerror("translation rule with 'no' "
3903 "does not need '->'");
3907 if ($9 == NULL || $9->host == NULL) {
3908 yyerror("translation rule requires '-> "
3912 if (!r.af && ! $9->host->ifindex)
3913 r.af = $9->host->af;
3915 remove_invalid_hosts(&$9->host, &r.af);
3916 if (invalid_redirect($9->host, r.af))
3918 if (check_netmask($9->host, r.af))
3921 r.rpool.proxy_port[0] = ntohs($9->rport.a);
3925 if (!$9->rport.b && $9->rport.t &&
3926 $5.dst.port != NULL) {
3927 r.rpool.proxy_port[1] =
3928 ntohs($9->rport.a) +
3930 $5.dst.port->port[1]) -
3932 $5.dst.port->port[0]));
3934 r.rpool.proxy_port[1] =
3938 r.rpool.proxy_port[1] =
3940 if (!r.rpool.proxy_port[0] &&
3941 !r.rpool.proxy_port[1]) {
3942 r.rpool.proxy_port[0] =
3943 PF_NAT_PROXY_PORT_LOW;
3944 r.rpool.proxy_port[1] =
3945 PF_NAT_PROXY_PORT_HIGH;
3946 } else if (!r.rpool.proxy_port[1])
3947 r.rpool.proxy_port[1] =
3948 r.rpool.proxy_port[0];
3954 r.rpool.opts = $10.type;
3955 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
3956 PF_POOL_NONE && ($9->host->next != NULL ||
3957 $9->host->addr.type == PF_ADDR_TABLE ||
3958 DYNIF_MULTIADDR($9->host->addr)))
3959 r.rpool.opts = PF_POOL_ROUNDROBIN;
3960 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
3961 PF_POOL_ROUNDROBIN &&
3962 disallow_table($9->host, "tables are only "
3963 "supported in round-robin redirection "
3966 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
3967 PF_POOL_ROUNDROBIN &&
3968 disallow_alias($9->host, "interface (%s) "
3969 "is only supported in round-robin "
3970 "redirection pools"))
3972 if ($9->host->next != NULL) {
3973 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
3974 PF_POOL_ROUNDROBIN) {
3975 yyerror("only round-robin "
3976 "valid for multiple "
3977 "redirection addresses");
3983 if ($10.key != NULL)
3984 memcpy(&r.rpool.key, $10.key,
3985 sizeof(struct pf_poolhashkey));
3988 r.rpool.opts |= $10.opts;
3990 if ($10.staticport) {
3991 if (r.action != PF_NAT) {
3992 yyerror("the 'static-port' option is "
3993 "only valid with nat rules");
3996 if (r.rpool.proxy_port[0] !=
3997 PF_NAT_PROXY_PORT_LOW &&
3998 r.rpool.proxy_port[1] !=
3999 PF_NAT_PROXY_PORT_HIGH) {
4000 yyerror("the 'static-port' option can't"
4001 " be used when specifying a port"
4005 r.rpool.proxy_port[0] = 0;
4006 r.rpool.proxy_port[1] = 0;
4009 expand_rule(&r, $2, $9 == NULL ? NULL : $9->host, $4,
4010 $5.src_os, $5.src.host, $5.src.port, $5.dst.host,
4011 $5.dst.port, 0, 0, 0, "");
4016 binatrule : no BINAT natpasslog interface af proto FROM host toipspec tag
4017 tagged rtable redirection
4019 struct pf_rule binat;
4020 struct pf_pooladdr *pa;
4022 if (check_rulestate(PFCTL_STATE_NAT))
4024 if (disallow_urpf_failed($9, "\"urpf-failed\" is not "
4025 "permitted as a binat destination"))
4028 memset(&binat, 0, sizeof(binat));
4031 yyerror("\"pass\" not valid with \"no\"");
4035 binat.action = PF_NOBINAT;
4037 binat.action = PF_BINAT;
4038 binat.natpass = $3.b1;
4040 binat.logif = $3.w2;
4042 if (!binat.af && $8 != NULL && $8->af)
4044 if (!binat.af && $9 != NULL && $9->af)
4047 if (!binat.af && $13 != NULL && $13->host)
4048 binat.af = $13->host->af;
4050 yyerror("address family (inet/inet6) "
4056 memcpy(binat.ifname, $4->ifname,
4057 sizeof(binat.ifname));
4058 binat.ifnot = $4->not;
4063 if (strlcpy(binat.tagname, $10,
4064 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4065 yyerror("tag too long, max %u chars",
4066 PF_TAG_NAME_SIZE - 1);
4070 if (strlcpy(binat.match_tagname, $11.name,
4071 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4072 yyerror("tag too long, max %u chars",
4073 PF_TAG_NAME_SIZE - 1);
4076 binat.match_tag_not = $11.neg;
4077 binat.rtableid = $12;
4080 binat.proto = $6->proto;
4084 if ($8 != NULL && disallow_table($8, "invalid use of "
4085 "table <%s> as the source address of a binat rule"))
4087 if ($8 != NULL && disallow_alias($8, "invalid use of "
4088 "interface (%s) as the source address of a binat "
4091 if ($13 != NULL && $13->host != NULL && disallow_table(
4092 $13->host, "invalid use of table <%s> as the "
4093 "redirect address of a binat rule"))
4095 if ($13 != NULL && $13->host != NULL && disallow_alias(
4096 $13->host, "invalid use of interface (%s) as the "
4097 "redirect address of a binat rule"))
4102 yyerror("multiple binat ip addresses");
4105 if ($8->addr.type == PF_ADDR_DYNIFTL)
4107 if ($8->af != binat.af) {
4108 yyerror("binat ip versions must match");
4111 if (check_netmask($8, binat.af))
4113 memcpy(&binat.src.addr, &$8->addr,
4114 sizeof(binat.src.addr));
4119 yyerror("multiple binat ip addresses");
4122 if ($9->af != binat.af && $9->af) {
4123 yyerror("binat ip versions must match");
4126 if (check_netmask($9, binat.af))
4128 memcpy(&binat.dst.addr, &$9->addr,
4129 sizeof(binat.dst.addr));
4130 binat.dst.neg = $9->not;
4134 if (binat.action == PF_NOBINAT) {
4136 yyerror("'no binat' rule does not need"
4141 if ($13 == NULL || $13->host == NULL) {
4142 yyerror("'binat' rule requires"
4147 remove_invalid_hosts(&$13->host, &binat.af);
4148 if (invalid_redirect($13->host, binat.af))
4150 if ($13->host->next != NULL) {
4151 yyerror("binat rule must redirect to "
4152 "a single address");
4155 if (check_netmask($13->host, binat.af))
4158 if (!PF_AZERO(&binat.src.addr.v.a.mask,
4160 !PF_AEQ(&binat.src.addr.v.a.mask,
4161 &$13->host->addr.v.a.mask, binat.af)) {
4162 yyerror("'binat' source mask and "
4163 "redirect mask must be the same");
4167 TAILQ_INIT(&binat.rpool.list);
4168 pa = calloc(1, sizeof(struct pf_pooladdr));
4170 err(1, "binat: calloc");
4171 pa->addr = $13->host->addr;
4173 TAILQ_INSERT_TAIL(&binat.rpool.list,
4179 pfctl_add_rule(pf, &binat, "");
4183 tag : /* empty */ { $$ = NULL; }
4184 | TAG STRING { $$ = $2; }
4187 tagged : /* empty */ { $$.neg = 0; $$.name = NULL; }
4188 | not TAGGED string { $$.neg = $1; $$.name = $3; }
4191 rtable : /* empty */ { $$ = -1; }
4193 if ($2 < 0 /* || $2 > RT_TABLEID_MAX */ ) {
4194 yyerror("invalid rtable id");
4201 route_host : STRING {
4202 $$ = calloc(1, sizeof(struct node_host));
4204 err(1, "route_host: calloc");
4206 set_ipmask($$, 128);
4210 | '(' STRING host ')' {
4216 route_host_list : route_host optnl { $$ = $1; }
4217 | route_host_list comma route_host optnl {
4220 if ($1->af != $3->af) {
4221 yyerror("all pool addresses must be in the "
4222 "same address family");
4225 $1->tail->next = $3;
4226 $1->tail = $3->tail;
4231 routespec : route_host { $$ = $1; }
4232 | '{' optnl route_host_list '}' { $$ = $3; }
4235 route : /* empty */ {
4242 $$.rt = PF_FASTROUTE;
4245 | ROUTETO routespec pool_opts {
4248 $$.pool_opts = $3.type | $3.opts;
4252 | REPLYTO routespec pool_opts {
4255 $$.pool_opts = $3.type | $3.opts;
4259 | DUPTO routespec pool_opts {
4262 $$.pool_opts = $3.type | $3.opts;
4268 timeout_spec : STRING NUMBER
4270 if (check_rulestate(PFCTL_STATE_OPTION)) {
4274 if ($2 < 0 || $2 > UINT_MAX) {
4275 yyerror("only positive values permitted");
4278 if (pfctl_set_timeout(pf, $1, $2, 0) != 0) {
4279 yyerror("unknown timeout %s", $1);
4287 timeout_list : timeout_list comma timeout_spec optnl
4288 | timeout_spec optnl
4291 limit_spec : STRING NUMBER
4293 if (check_rulestate(PFCTL_STATE_OPTION)) {
4297 if ($2 < 0 || $2 > UINT_MAX) {
4298 yyerror("only positive values permitted");
4301 if (pfctl_set_limit(pf, $1, $2) != 0) {
4302 yyerror("unable to set limit %s %u", $1, $2);
4310 limit_list : limit_list comma limit_spec optnl
4318 yesno : NO { $$ = 0; }
4320 if (!strcmp($1, "yes"))
4323 yyerror("invalid value '%s', expected 'yes' "
4332 unaryop : '=' { $$ = PF_OP_EQ; }
4333 | '!' '=' { $$ = PF_OP_NE; }
4334 | '<' '=' { $$ = PF_OP_LE; }
4335 | '<' { $$ = PF_OP_LT; }
4336 | '>' '=' { $$ = PF_OP_GE; }
4337 | '>' { $$ = PF_OP_GT; }
4343 yyerror(const char *fmt, ...)
4349 fprintf(stderr, "%s:%d: ", file->name, yylval.lineno);
4350 vfprintf(stderr, fmt, ap);
4351 fprintf(stderr, "\n");
4357 disallow_table(struct node_host *h, const char *fmt)
4359 for (; h != NULL; h = h->next)
4360 if (h->addr.type == PF_ADDR_TABLE) {
4361 yyerror(fmt, h->addr.v.tblname);
4368 disallow_urpf_failed(struct node_host *h, const char *fmt)
4370 for (; h != NULL; h = h->next)
4371 if (h->addr.type == PF_ADDR_URPFFAILED) {
4379 disallow_alias(struct node_host *h, const char *fmt)
4381 for (; h != NULL; h = h->next)
4382 if (DYNIF_MULTIADDR(h->addr)) {
4383 yyerror(fmt, h->addr.v.tblname);
4390 rule_consistent(struct pf_rule *r, int anchor_call)
4394 switch (r->action) {
4399 problems = filter_consistent(r, anchor_call);
4403 problems = nat_consistent(r);
4407 problems = rdr_consistent(r);
4418 filter_consistent(struct pf_rule *r, int anchor_call)
4422 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP &&
4423 (r->src.port_op || r->dst.port_op)) {
4424 yyerror("port only applies to tcp/udp");
4427 if (r->proto != IPPROTO_ICMP && r->proto != IPPROTO_ICMPV6 &&
4428 (r->type || r->code)) {
4429 yyerror("icmp-type/code only applies to icmp");
4432 if (!r->af && (r->type || r->code)) {
4433 yyerror("must indicate address family with icmp-type/code");
4436 if (r->overload_tblname[0] &&
4437 r->max_src_conn == 0 && r->max_src_conn_rate.seconds == 0) {
4438 yyerror("'overload' requires 'max-src-conn' "
4439 "or 'max-src-conn-rate'");
4442 if ((r->proto == IPPROTO_ICMP && r->af == AF_INET6) ||
4443 (r->proto == IPPROTO_ICMPV6 && r->af == AF_INET)) {
4444 yyerror("proto %s doesn't match address family %s",
4445 r->proto == IPPROTO_ICMP ? "icmp" : "icmp6",
4446 r->af == AF_INET ? "inet" : "inet6");
4449 if (r->allow_opts && r->action != PF_PASS) {
4450 yyerror("allow-opts can only be specified for pass rules");
4453 if (r->rule_flag & PFRULE_FRAGMENT && (r->src.port_op ||
4454 r->dst.port_op || r->flagset || r->type || r->code)) {
4455 yyerror("fragments can be filtered only on IP header fields");
4458 if (r->rule_flag & PFRULE_RETURNRST && r->proto != IPPROTO_TCP) {
4459 yyerror("return-rst can only be applied to TCP rules");
4462 if (r->max_src_nodes && !(r->rule_flag & PFRULE_RULESRCTRACK)) {
4463 yyerror("max-src-nodes requires 'source-track rule'");
4466 if (r->action == PF_DROP && r->keep_state) {
4467 yyerror("keep state on block rules doesn't make sense");
4470 if (r->rule_flag & PFRULE_STATESLOPPY &&
4471 (r->keep_state == PF_STATE_MODULATE ||
4472 r->keep_state == PF_STATE_SYNPROXY)) {
4473 yyerror("sloppy state matching cannot be used with "
4474 "synproxy state or modulate state");
4481 nat_consistent(struct pf_rule *r)
4483 return (0); /* yeah! */
4487 rdr_consistent(struct pf_rule *r)
4491 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP) {
4492 if (r->src.port_op) {
4493 yyerror("src port only applies to tcp/udp");
4496 if (r->dst.port_op) {
4497 yyerror("dst port only applies to tcp/udp");
4500 if (r->rpool.proxy_port[0]) {
4501 yyerror("rpool port only applies to tcp/udp");
4505 if (r->dst.port_op &&
4506 r->dst.port_op != PF_OP_EQ && r->dst.port_op != PF_OP_RRG) {
4507 yyerror("invalid port operator for rdr destination port");
4514 process_tabledef(char *name, struct table_opts *opts)
4516 struct pfr_buffer ab;
4517 struct node_tinit *ti;
4519 bzero(&ab, sizeof(ab));
4520 ab.pfrb_type = PFRB_ADDRS;
4521 SIMPLEQ_FOREACH(ti, &opts->init_nodes, entries) {
4523 if (pfr_buf_load(&ab, ti->file, 0, append_addr)) {
4525 yyerror("cannot load \"%s\": %s",
4526 ti->file, strerror(errno));
4528 yyerror("file \"%s\" contains bad data",
4533 if (append_addr_host(&ab, ti->host, 0, 0)) {
4534 yyerror("cannot create address buffer: %s",
4539 if (pf->opts & PF_OPT_VERBOSE)
4540 print_tabledef(name, opts->flags, opts->init_addr,
4542 if (!(pf->opts & PF_OPT_NOACTION) &&
4543 pfctl_define_table(name, opts->flags, opts->init_addr,
4544 pf->anchor->name, &ab, pf->anchor->ruleset.tticket)) {
4545 yyerror("cannot define table %s: %s", name,
4546 pfr_strerror(errno));
4562 /* macro gore, but you should've seen the prior indentation nightmare... */
4564 #define FREE_LIST(T,r) \
4567 while (node != NULL) { \
4569 node = node->next; \
4574 #define LOOP_THROUGH(T,n,r,C) \
4578 r = calloc(1, sizeof(T)); \
4580 err(1, "LOOP: calloc"); \
4584 while (n != NULL) { \
4593 expand_label_str(char *label, size_t len, const char *srch, const char *repl)
4598 if ((tmp = calloc(1, len)) == NULL)
4599 err(1, "expand_label_str: calloc");
4601 while ((q = strstr(p, srch)) != NULL) {
4603 if ((strlcat(tmp, p, len) >= len) ||
4604 (strlcat(tmp, repl, len) >= len))
4605 errx(1, "expand_label: label too long");
4609 if (strlcat(tmp, p, len) >= len)
4610 errx(1, "expand_label: label too long");
4611 strlcpy(label, tmp, len); /* always fits */
4616 expand_label_if(const char *name, char *label, size_t len, const char *ifname)
4618 if (strstr(label, name) != NULL) {
4620 expand_label_str(label, len, name, "any");
4622 expand_label_str(label, len, name, ifname);
4627 expand_label_addr(const char *name, char *label, size_t len, sa_family_t af,
4628 struct node_host *h)
4630 char tmp[64], tmp_not[66];
4632 if (strstr(label, name) != NULL) {
4633 switch (h->addr.type) {
4634 case PF_ADDR_DYNIFTL:
4635 snprintf(tmp, sizeof(tmp), "(%s)", h->addr.v.ifname);
4638 snprintf(tmp, sizeof(tmp), "<%s>", h->addr.v.tblname);
4640 case PF_ADDR_NOROUTE:
4641 snprintf(tmp, sizeof(tmp), "no-route");
4643 case PF_ADDR_URPFFAILED:
4644 snprintf(tmp, sizeof(tmp), "urpf-failed");
4646 case PF_ADDR_ADDRMASK:
4647 if (!af || (PF_AZERO(&h->addr.v.a.addr, af) &&
4648 PF_AZERO(&h->addr.v.a.mask, af)))
4649 snprintf(tmp, sizeof(tmp), "any");
4654 if (inet_ntop(af, &h->addr.v.a.addr, a,
4656 snprintf(tmp, sizeof(tmp), "?");
4658 bits = unmask(&h->addr.v.a.mask, af);
4659 if ((af == AF_INET && bits < 32) ||
4660 (af == AF_INET6 && bits < 128))
4661 snprintf(tmp, sizeof(tmp),
4664 snprintf(tmp, sizeof(tmp),
4670 snprintf(tmp, sizeof(tmp), "?");
4675 snprintf(tmp_not, sizeof(tmp_not), "! %s", tmp);
4676 expand_label_str(label, len, name, tmp_not);
4678 expand_label_str(label, len, name, tmp);
4683 expand_label_port(const char *name, char *label, size_t len,
4684 struct node_port *port)
4686 char a1[6], a2[6], op[13] = "";
4688 if (strstr(label, name) != NULL) {
4689 snprintf(a1, sizeof(a1), "%u", ntohs(port->port[0]));
4690 snprintf(a2, sizeof(a2), "%u", ntohs(port->port[1]));
4693 else if (port->op == PF_OP_IRG)
4694 snprintf(op, sizeof(op), "%s><%s", a1, a2);
4695 else if (port->op == PF_OP_XRG)
4696 snprintf(op, sizeof(op), "%s<>%s", a1, a2);
4697 else if (port->op == PF_OP_EQ)
4698 snprintf(op, sizeof(op), "%s", a1);
4699 else if (port->op == PF_OP_NE)
4700 snprintf(op, sizeof(op), "!=%s", a1);
4701 else if (port->op == PF_OP_LT)
4702 snprintf(op, sizeof(op), "<%s", a1);
4703 else if (port->op == PF_OP_LE)
4704 snprintf(op, sizeof(op), "<=%s", a1);
4705 else if (port->op == PF_OP_GT)
4706 snprintf(op, sizeof(op), ">%s", a1);
4707 else if (port->op == PF_OP_GE)
4708 snprintf(op, sizeof(op), ">=%s", a1);
4709 expand_label_str(label, len, name, op);
4714 expand_label_proto(const char *name, char *label, size_t len, u_int8_t proto)
4716 struct protoent *pe;
4719 if (strstr(label, name) != NULL) {
4720 pe = getprotobynumber(proto);
4722 expand_label_str(label, len, name, pe->p_name);
4724 snprintf(n, sizeof(n), "%u", proto);
4725 expand_label_str(label, len, name, n);
4731 expand_label_nr(const char *name, char *label, size_t len)
4735 if (strstr(label, name) != NULL) {
4736 snprintf(n, sizeof(n), "%u", pf->anchor->match);
4737 expand_label_str(label, len, name, n);
4742 expand_label(char *label, size_t len, const char *ifname, sa_family_t af,
4743 struct node_host *src_host, struct node_port *src_port,
4744 struct node_host *dst_host, struct node_port *dst_port,
4747 expand_label_if("$if", label, len, ifname);
4748 expand_label_addr("$srcaddr", label, len, af, src_host);
4749 expand_label_addr("$dstaddr", label, len, af, dst_host);
4750 expand_label_port("$srcport", label, len, src_port);
4751 expand_label_port("$dstport", label, len, dst_port);
4752 expand_label_proto("$proto", label, len, proto);
4753 expand_label_nr("$nr", label, len);
4757 expand_altq(struct pf_altq *a, struct node_if *interfaces,
4758 struct node_queue *nqueues, struct node_queue_bw bwspec,
4759 struct node_queue_opt *opts)
4761 struct pf_altq pa, pb;
4762 char qname[PF_QNAME_SIZE];
4763 struct node_queue *n;
4764 struct node_queue_bw bw;
4767 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
4768 FREE_LIST(struct node_if, interfaces);
4769 FREE_LIST(struct node_queue, nqueues);
4773 LOOP_THROUGH(struct node_if, interface, interfaces,
4774 memcpy(&pa, a, sizeof(struct pf_altq));
4775 if (strlcpy(pa.ifname, interface->ifname,
4776 sizeof(pa.ifname)) >= sizeof(pa.ifname))
4777 errx(1, "expand_altq: strlcpy");
4779 if (interface->not) {
4780 yyerror("altq on ! <interface> is not supported");
4783 if (eval_pfaltq(pf, &pa, &bwspec, opts))
4786 if (pfctl_add_altq(pf, &pa))
4789 if (pf->opts & PF_OPT_VERBOSE) {
4790 print_altq(&pf->paltq->altq, 0,
4792 if (nqueues && nqueues->tail) {
4794 LOOP_THROUGH(struct node_queue, queue,
4804 if (pa.scheduler == ALTQT_CBQ ||
4805 pa.scheduler == ALTQT_HFSC) {
4806 /* now create a root queue */
4807 memset(&pb, 0, sizeof(struct pf_altq));
4808 if (strlcpy(qname, "root_", sizeof(qname)) >=
4810 errx(1, "expand_altq: strlcpy");
4811 if (strlcat(qname, interface->ifname,
4812 sizeof(qname)) >= sizeof(qname))
4813 errx(1, "expand_altq: strlcat");
4814 if (strlcpy(pb.qname, qname,
4815 sizeof(pb.qname)) >= sizeof(pb.qname))
4816 errx(1, "expand_altq: strlcpy");
4817 if (strlcpy(pb.ifname, interface->ifname,
4818 sizeof(pb.ifname)) >= sizeof(pb.ifname))
4819 errx(1, "expand_altq: strlcpy");
4820 pb.qlimit = pa.qlimit;
4821 pb.scheduler = pa.scheduler;
4822 bw.bw_absolute = pa.ifbandwidth;
4824 if (eval_pfqueue(pf, &pb, &bw, opts))
4827 if (pfctl_add_altq(pf, &pb))
4831 LOOP_THROUGH(struct node_queue, queue, nqueues,
4832 n = calloc(1, sizeof(struct node_queue));
4834 err(1, "expand_altq: calloc");
4835 if (pa.scheduler == ALTQT_CBQ ||
4836 pa.scheduler == ALTQT_HFSC)
4837 if (strlcpy(n->parent, qname,
4838 sizeof(n->parent)) >=
4840 errx(1, "expand_altq: strlcpy");
4841 if (strlcpy(n->queue, queue->queue,
4842 sizeof(n->queue)) >= sizeof(n->queue))
4843 errx(1, "expand_altq: strlcpy");
4844 if (strlcpy(n->ifname, interface->ifname,
4845 sizeof(n->ifname)) >= sizeof(n->ifname))
4846 errx(1, "expand_altq: strlcpy");
4847 n->scheduler = pa.scheduler;
4853 queues->tail->next = n;
4859 FREE_LIST(struct node_if, interfaces);
4860 FREE_LIST(struct node_queue, nqueues);
4866 expand_queue(struct pf_altq *a, struct node_if *interfaces,
4867 struct node_queue *nqueues, struct node_queue_bw bwspec,
4868 struct node_queue_opt *opts)
4870 struct node_queue *n, *nq;
4875 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
4876 FREE_LIST(struct node_queue, nqueues);
4880 if (queues == NULL) {
4881 yyerror("queue %s has no parent", a->qname);
4882 FREE_LIST(struct node_queue, nqueues);
4886 LOOP_THROUGH(struct node_if, interface, interfaces,
4887 LOOP_THROUGH(struct node_queue, tqueue, queues,
4888 if (!strncmp(a->qname, tqueue->queue, PF_QNAME_SIZE) &&
4889 (interface->ifname[0] == 0 ||
4890 (!interface->not && !strncmp(interface->ifname,
4891 tqueue->ifname, IFNAMSIZ)) ||
4892 (interface->not && strncmp(interface->ifname,
4893 tqueue->ifname, IFNAMSIZ)))) {
4894 /* found ourself in queues */
4897 memcpy(&pa, a, sizeof(struct pf_altq));
4899 if (pa.scheduler != ALTQT_NONE &&
4900 pa.scheduler != tqueue->scheduler) {
4901 yyerror("exactly one scheduler type "
4902 "per interface allowed");
4905 pa.scheduler = tqueue->scheduler;
4907 /* scheduler dependent error checking */
4908 switch (pa.scheduler) {
4910 if (nqueues != NULL) {
4911 yyerror("priq queues cannot "
4912 "have child queues");
4915 if (bwspec.bw_absolute > 0 ||
4916 bwspec.bw_percent < 100) {
4917 yyerror("priq doesn't take "
4926 if (strlcpy(pa.ifname, tqueue->ifname,
4927 sizeof(pa.ifname)) >= sizeof(pa.ifname))
4928 errx(1, "expand_queue: strlcpy");
4929 if (strlcpy(pa.parent, tqueue->parent,
4930 sizeof(pa.parent)) >= sizeof(pa.parent))
4931 errx(1, "expand_queue: strlcpy");
4933 if (eval_pfqueue(pf, &pa, &bwspec, opts))
4936 if (pfctl_add_altq(pf, &pa))
4939 for (nq = nqueues; nq != NULL; nq = nq->next) {
4940 if (!strcmp(a->qname, nq->queue)) {
4941 yyerror("queue cannot have "
4947 sizeof(struct node_queue));
4949 err(1, "expand_queue: calloc");
4950 if (strlcpy(n->parent, a->qname,
4951 sizeof(n->parent)) >=
4953 errx(1, "expand_queue strlcpy");
4954 if (strlcpy(n->queue, nq->queue,
4955 sizeof(n->queue)) >=
4957 errx(1, "expand_queue strlcpy");
4958 if (strlcpy(n->ifname, tqueue->ifname,
4959 sizeof(n->ifname)) >=
4961 errx(1, "expand_queue strlcpy");
4962 n->scheduler = tqueue->scheduler;
4968 queues->tail->next = n;
4972 if ((pf->opts & PF_OPT_VERBOSE) && (
4973 (found == 1 && interface->ifname[0] == 0) ||
4974 (found > 0 && interface->ifname[0] != 0))) {
4975 print_queue(&pf->paltq->altq, 0,
4976 &bwspec, interface->ifname[0] != 0,
4978 if (nqueues && nqueues->tail) {
4980 LOOP_THROUGH(struct node_queue,
4993 FREE_LIST(struct node_queue, nqueues);
4994 FREE_LIST(struct node_if, interfaces);
4997 yyerror("queue %s has no parent", a->qname);
5008 expand_rule(struct pf_rule *r,
5009 struct node_if *interfaces, struct node_host *rpool_hosts,
5010 struct node_proto *protos, struct node_os *src_oses,
5011 struct node_host *src_hosts, struct node_port *src_ports,
5012 struct node_host *dst_hosts, struct node_port *dst_ports,
5013 struct node_uid *uids, struct node_gid *gids, struct node_icmp *icmp_types,
5014 const char *anchor_call)
5016 sa_family_t af = r->af;
5017 int added = 0, error = 0;
5018 char ifname[IF_NAMESIZE];
5019 char label[PF_RULE_LABEL_SIZE];
5020 char tagname[PF_TAG_NAME_SIZE];
5021 char match_tagname[PF_TAG_NAME_SIZE];
5022 struct pf_pooladdr *pa;
5023 struct node_host *h;
5024 u_int8_t flags, flagset, keep_state;
5026 if (strlcpy(label, r->label, sizeof(label)) >= sizeof(label))
5027 errx(1, "expand_rule: strlcpy");
5028 if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname))
5029 errx(1, "expand_rule: strlcpy");
5030 if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >=
5031 sizeof(match_tagname))
5032 errx(1, "expand_rule: strlcpy");
5034 flagset = r->flagset;
5035 keep_state = r->keep_state;
5037 LOOP_THROUGH(struct node_if, interface, interfaces,
5038 LOOP_THROUGH(struct node_proto, proto, protos,
5039 LOOP_THROUGH(struct node_icmp, icmp_type, icmp_types,
5040 LOOP_THROUGH(struct node_host, src_host, src_hosts,
5041 LOOP_THROUGH(struct node_port, src_port, src_ports,
5042 LOOP_THROUGH(struct node_os, src_os, src_oses,
5043 LOOP_THROUGH(struct node_host, dst_host, dst_hosts,
5044 LOOP_THROUGH(struct node_port, dst_port, dst_ports,
5045 LOOP_THROUGH(struct node_uid, uid, uids,
5046 LOOP_THROUGH(struct node_gid, gid, gids,
5049 /* for link-local IPv6 address, interface must match up */
5050 if ((r->af && src_host->af && r->af != src_host->af) ||
5051 (r->af && dst_host->af && r->af != dst_host->af) ||
5052 (src_host->af && dst_host->af &&
5053 src_host->af != dst_host->af) ||
5054 (src_host->ifindex && dst_host->ifindex &&
5055 src_host->ifindex != dst_host->ifindex) ||
5056 (src_host->ifindex && *interface->ifname &&
5057 src_host->ifindex != if_nametoindex(interface->ifname)) ||
5058 (dst_host->ifindex && *interface->ifname &&
5059 dst_host->ifindex != if_nametoindex(interface->ifname)))
5061 if (!r->af && src_host->af)
5062 r->af = src_host->af;
5063 else if (!r->af && dst_host->af)
5064 r->af = dst_host->af;
5066 if (*interface->ifname)
5067 strlcpy(r->ifname, interface->ifname,
5069 else if (if_indextoname(src_host->ifindex, ifname))
5070 strlcpy(r->ifname, ifname, sizeof(r->ifname));
5071 else if (if_indextoname(dst_host->ifindex, ifname))
5072 strlcpy(r->ifname, ifname, sizeof(r->ifname));
5074 memset(r->ifname, '\0', sizeof(r->ifname));
5076 if (strlcpy(r->label, label, sizeof(r->label)) >=
5078 errx(1, "expand_rule: strlcpy");
5079 if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >=
5081 errx(1, "expand_rule: strlcpy");
5082 if (strlcpy(r->match_tagname, match_tagname,
5083 sizeof(r->match_tagname)) >= sizeof(r->match_tagname))
5084 errx(1, "expand_rule: strlcpy");
5085 expand_label(r->label, PF_RULE_LABEL_SIZE, r->ifname, r->af,
5086 src_host, src_port, dst_host, dst_port, proto->proto);
5087 expand_label(r->tagname, PF_TAG_NAME_SIZE, r->ifname, r->af,
5088 src_host, src_port, dst_host, dst_port, proto->proto);
5089 expand_label(r->match_tagname, PF_TAG_NAME_SIZE, r->ifname,
5090 r->af, src_host, src_port, dst_host, dst_port,
5093 error += check_netmask(src_host, r->af);
5094 error += check_netmask(dst_host, r->af);
5096 r->ifnot = interface->not;
5097 r->proto = proto->proto;
5098 r->src.addr = src_host->addr;
5099 r->src.neg = src_host->not;
5100 r->src.port[0] = src_port->port[0];
5101 r->src.port[1] = src_port->port[1];
5102 r->src.port_op = src_port->op;
5103 r->dst.addr = dst_host->addr;
5104 r->dst.neg = dst_host->not;
5105 r->dst.port[0] = dst_port->port[0];
5106 r->dst.port[1] = dst_port->port[1];
5107 r->dst.port_op = dst_port->op;
5108 r->uid.op = uid->op;
5109 r->uid.uid[0] = uid->uid[0];
5110 r->uid.uid[1] = uid->uid[1];
5111 r->gid.op = gid->op;
5112 r->gid.gid[0] = gid->gid[0];
5113 r->gid.gid[1] = gid->gid[1];
5114 r->type = icmp_type->type;
5115 r->code = icmp_type->code;
5117 if ((keep_state == PF_STATE_MODULATE ||
5118 keep_state == PF_STATE_SYNPROXY) &&
5119 r->proto && r->proto != IPPROTO_TCP)
5120 r->keep_state = PF_STATE_NORMAL;
5122 r->keep_state = keep_state;
5124 if (r->proto && r->proto != IPPROTO_TCP) {
5129 r->flagset = flagset;
5131 if (icmp_type->proto && r->proto != icmp_type->proto) {
5132 yyerror("icmp-type mismatch");
5136 if (src_os && src_os->os) {
5137 r->os_fingerprint = pfctl_get_fingerprint(src_os->os);
5138 if ((pf->opts & PF_OPT_VERBOSE2) &&
5139 r->os_fingerprint == PF_OSFP_NOMATCH)
5141 "warning: unknown '%s' OS fingerprint\n",
5144 r->os_fingerprint = PF_OSFP_ANY;
5147 TAILQ_INIT(&r->rpool.list);
5148 for (h = rpool_hosts; h != NULL; h = h->next) {
5149 pa = calloc(1, sizeof(struct pf_pooladdr));
5151 err(1, "expand_rule: calloc");
5153 if (h->ifname != NULL) {
5154 if (strlcpy(pa->ifname, h->ifname,
5155 sizeof(pa->ifname)) >=
5157 errx(1, "expand_rule: strlcpy");
5160 TAILQ_INSERT_TAIL(&r->rpool.list, pa, entries);
5163 if (rule_consistent(r, anchor_call[0]) < 0 || error)
5164 yyerror("skipping rule due to errors");
5166 r->nr = pf->astack[pf->asd]->match++;
5167 pfctl_add_rule(pf, r, anchor_call);
5173 FREE_LIST(struct node_if, interfaces);
5174 FREE_LIST(struct node_proto, protos);
5175 FREE_LIST(struct node_host, src_hosts);
5176 FREE_LIST(struct node_port, src_ports);
5177 FREE_LIST(struct node_os, src_oses);
5178 FREE_LIST(struct node_host, dst_hosts);
5179 FREE_LIST(struct node_port, dst_ports);
5180 FREE_LIST(struct node_uid, uids);
5181 FREE_LIST(struct node_gid, gids);
5182 FREE_LIST(struct node_icmp, icmp_types);
5183 FREE_LIST(struct node_host, rpool_hosts);
5186 yyerror("rule expands to no valid combination");
5190 expand_skip_interface(struct node_if *interfaces)
5194 if (!interfaces || (!interfaces->next && !interfaces->not &&
5195 !strcmp(interfaces->ifname, "none"))) {
5196 if (pf->opts & PF_OPT_VERBOSE)
5197 printf("set skip on none\n");
5198 errs = pfctl_set_interface_flags(pf, "", PFI_IFLAG_SKIP, 0);
5202 if (pf->opts & PF_OPT_VERBOSE)
5203 printf("set skip on {");
5204 LOOP_THROUGH(struct node_if, interface, interfaces,
5205 if (pf->opts & PF_OPT_VERBOSE)
5206 printf(" %s", interface->ifname);
5207 if (interface->not) {
5208 yyerror("skip on ! <interface> is not supported");
5211 errs += pfctl_set_interface_flags(pf,
5212 interface->ifname, PFI_IFLAG_SKIP, 1);
5214 if (pf->opts & PF_OPT_VERBOSE)
5217 FREE_LIST(struct node_if, interfaces);
5229 check_rulestate(int desired_state)
5231 if (require_order && (rulestate > desired_state)) {
5232 yyerror("Rules must be in order: options, normalization, "
5233 "queueing, translation, filtering");
5236 rulestate = desired_state;
5241 kw_cmp(const void *k, const void *e)
5243 return (strcmp(k, ((const struct keywords *)e)->k_name));
5249 /* this has to be sorted always */
5250 static const struct keywords keywords[] = {
5252 { "allow-opts", ALLOWOPTS},
5254 { "anchor", ANCHOR},
5255 { "antispoof", ANTISPOOF},
5257 { "bandwidth", BANDWIDTH},
5259 { "binat-anchor", BINATANCHOR},
5260 { "bitmask", BITMASK},
5262 { "block-policy", BLOCKPOLICY},
5265 { "crop", FRAGCROP},
5267 { "divert-reply", DIVERTREPLY},
5268 { "divert-to", DIVERTTO},
5270 { "drop-ovl", FRAGDROP},
5272 { "fastroute", FASTROUTE},
5273 { "file", FILENAME},
5274 { "fingerprints", FINGERPRINTS},
5276 { "floating", FLOATING},
5279 { "fragment", FRAGMENT},
5281 { "global", GLOBAL},
5284 { "hostid", HOSTID},
5285 { "icmp-type", ICMPTYPE},
5286 { "icmp6-type", ICMP6TYPE},
5287 { "if-bound", IFBOUND},
5289 { "include", INCLUDE},
5295 { "linkshare", LINKSHARE},
5298 { "loginterface", LOGINTERFACE},
5300 { "max-mss", MAXMSS},
5301 { "max-src-conn", MAXSRCCONN},
5302 { "max-src-conn-rate", MAXSRCCONNRATE},
5303 { "max-src-nodes", MAXSRCNODES},
5304 { "max-src-states", MAXSRCSTATES},
5305 { "min-ttl", MINTTL},
5306 { "modulate", MODULATE},
5308 { "nat-anchor", NATANCHOR},
5311 { "no-route", NOROUTE},
5312 { "no-sync", NOSYNC},
5314 { "optimization", OPTIMIZATION},
5317 { "overload", OVERLOAD},
5321 { "priority", PRIORITY},
5323 { "probability", PROBABILITY},
5325 { "qlimit", QLIMIT},
5328 { "random", RANDOM},
5329 { "random-id", RANDOMID},
5331 { "rdr-anchor", RDRANCHOR},
5332 { "realtime", REALTIME},
5333 { "reassemble", REASSEMBLE},
5334 { "reply-to", REPLYTO},
5335 { "require-order", REQUIREORDER},
5336 { "return", RETURN},
5337 { "return-icmp", RETURNICMP},
5338 { "return-icmp6", RETURNICMP6},
5339 { "return-rst", RETURNRST},
5340 { "round-robin", ROUNDROBIN},
5342 { "route-to", ROUTETO},
5343 { "rtable", RTABLE},
5345 { "ruleset-optimization", RULESET_OPTIMIZATION},
5348 { "set-tos", SETTOS},
5350 { "sloppy", SLOPPY},
5351 { "source-hash", SOURCEHASH},
5352 { "source-track", SOURCETRACK},
5354 { "state-defaults", STATEDEFAULTS},
5355 { "state-policy", STATEPOLICY},
5356 { "static-port", STATICPORT},
5357 { "sticky-address", STICKYADDRESS},
5358 { "synproxy", SYNPROXY},
5361 { "tagged", TAGGED},
5362 { "tbrsize", TBRSIZE},
5363 { "timeout", TIMEOUT},
5367 { "upperlimit", UPPERLIMIT},
5368 { "urpf-failed", URPFFAILED},
5371 const struct keywords *p;
5373 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
5374 sizeof(keywords[0]), kw_cmp);
5378 fprintf(stderr, "%s: %d\n", s, p->k_val);
5382 fprintf(stderr, "string: %s\n", s);
5387 #define MAXPUSHBACK 128
5391 char pushback_buffer[MAXPUSHBACK];
5392 int pushback_index = 0;
5400 /* Read character from the parsebuffer instead of input. */
5401 if (parseindex >= 0) {
5402 c = parsebuf[parseindex++];
5411 return (pushback_buffer[--pushback_index]);
5414 if ((c = getc(file->stream)) == EOF) {
5415 yyerror("reached end of file while parsing quoted string");
5416 if (popfile() == EOF)
5423 while ((c = getc(file->stream)) == '\\') {
5424 next = getc(file->stream);
5429 yylval.lineno = file->lineno;
5434 if (popfile() == EOF)
5436 c = getc(file->stream);
5448 if (parseindex >= 0)
5451 if (pushback_index < MAXPUSHBACK-1)
5452 return (pushback_buffer[pushback_index++] = c);
5464 /* skip to either EOF or the first real EOL */
5467 c = pushback_buffer[--pushback_index];
5485 int quotec, next, c;
5490 while ((c = lgetc(0)) == ' ' || c == '\t')
5493 yylval.lineno = file->lineno;
5495 while ((c = lgetc(0)) != '\n' && c != EOF)
5497 if (c == '$' && parsebuf == NULL) {
5499 if ((c = lgetc(0)) == EOF)
5502 if (p + 1 >= buf + sizeof(buf) - 1) {
5503 yyerror("string too long");
5506 if (isalnum(c) || c == '_') {
5516 yyerror("macro '%s' not defined", buf);
5529 if ((c = lgetc(quotec)) == EOF)
5534 } else if (c == '\\') {
5535 if ((next = lgetc(quotec)) == EOF)
5537 if (next == quotec || c == ' ' || c == '\t')
5539 else if (next == '\n')
5543 } else if (c == quotec) {
5547 if (p + 1 >= buf + sizeof(buf) - 1) {
5548 yyerror("string too long");
5553 yylval.v.string = strdup(buf);
5554 if (yylval.v.string == NULL)
5555 err(1, "yylex: strdup");
5560 yylval.v.i = PF_OP_XRG;
5561 return (PORTBINARY);
5568 yylval.v.i = PF_OP_IRG;
5569 return (PORTBINARY);
5581 #define allowed_to_end_number(x) \
5582 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
5584 if (c == '-' || isdigit(c)) {
5587 if ((unsigned)(p-buf) >= sizeof(buf)) {
5588 yyerror("string too long");
5591 } while ((c = lgetc(0)) != EOF && isdigit(c));
5593 if (p == buf + 1 && buf[0] == '-')
5595 if (c == EOF || allowed_to_end_number(c)) {
5596 const char *errstr = NULL;
5599 yylval.v.number = strtonum(buf, LLONG_MIN,
5600 LLONG_MAX, &errstr);
5602 yyerror("\"%s\" invalid number: %s",
5617 #define allowed_in_string(x) \
5618 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
5619 x != '{' && x != '}' && x != '<' && x != '>' && \
5620 x != '!' && x != '=' && x != '/' && x != '#' && \
5623 if (isalnum(c) || c == ':' || c == '_') {
5626 if ((unsigned)(p-buf) >= sizeof(buf)) {
5627 yyerror("string too long");
5630 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
5633 if ((token = lookup(buf)) == STRING)
5634 if ((yylval.v.string = strdup(buf)) == NULL)
5635 err(1, "yylex: strdup");
5639 yylval.lineno = file->lineno;
5648 check_file_secrecy(int fd, const char *fname)
5652 if (fstat(fd, &st)) {
5653 warn("cannot stat %s", fname);
5656 if (st.st_uid != 0 && st.st_uid != getuid()) {
5657 warnx("%s: owner not root or current user", fname);
5660 if (st.st_mode & (S_IRWXG | S_IRWXO)) {
5661 warnx("%s: group/world readable/writeable", fname);
5668 pushfile(const char *name, int secret)
5672 if ((nfile = calloc(1, sizeof(struct file))) == NULL ||
5673 (nfile->name = strdup(name)) == NULL) {
5677 if (TAILQ_FIRST(&files) == NULL && strcmp(nfile->name, "-") == 0) {
5678 nfile->stream = stdin;
5680 if ((nfile->name = strdup("stdin")) == NULL) {
5685 } else if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
5686 warn("%s", nfile->name);
5690 } else if (secret &&
5691 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
5692 fclose(nfile->stream);
5698 TAILQ_INSERT_TAIL(&files, nfile, entry);
5707 if ((prev = TAILQ_PREV(file, files, entry)) != NULL) {
5708 prev->errors += file->errors;
5709 TAILQ_REMOVE(&files, file, entry);
5710 fclose(file->stream);
5720 parse_config(char *filename, struct pfctl *xpf)
5727 rulestate = PFCTL_STATE_NONE;
5728 returnicmpdefault = (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
5729 returnicmp6default =
5730 (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
5731 blockpolicy = PFRULE_DROP;
5734 if ((file = pushfile(filename, 0)) == NULL) {
5735 warn("cannot open the main config file!");
5740 errors = file->errors;
5743 /* Free macros and check which have not been used. */
5744 while ((sym = TAILQ_FIRST(&symhead))) {
5745 if ((pf->opts & PF_OPT_VERBOSE2) && !sym->used)
5746 fprintf(stderr, "warning: macro '%s' not "
5747 "used\n", sym->nam);
5750 TAILQ_REMOVE(&symhead, sym, entry);
5754 return (errors ? -1 : 0);
5758 symset(const char *nam, const char *val, int persist)
5762 for (sym = TAILQ_FIRST(&symhead); sym && strcmp(nam, sym->nam);
5763 sym = TAILQ_NEXT(sym, entry))
5767 if (sym->persist == 1)
5772 TAILQ_REMOVE(&symhead, sym, entry);
5776 if ((sym = calloc(1, sizeof(*sym))) == NULL)
5779 sym->nam = strdup(nam);
5780 if (sym->nam == NULL) {
5784 sym->val = strdup(val);
5785 if (sym->val == NULL) {
5791 sym->persist = persist;
5792 TAILQ_INSERT_TAIL(&symhead, sym, entry);
5797 pfctl_cmdline_symset(char *s)
5802 if ((val = strrchr(s, '=')) == NULL)
5805 if ((sym = malloc(strlen(s) - strlen(val) + 1)) == NULL)
5806 err(1, "pfctl_cmdline_symset: malloc");
5808 strlcpy(sym, s, strlen(s) - strlen(val) + 1);
5810 ret = symset(sym, val + 1, 1);
5817 symget(const char *nam)
5821 TAILQ_FOREACH(sym, &symhead, entry)
5822 if (strcmp(nam, sym->nam) == 0) {
5830 mv_rules(struct pf_ruleset *src, struct pf_ruleset *dst)
5835 for (i = 0; i < PF_RULESET_MAX; ++i) {
5836 while ((r = TAILQ_FIRST(src->rules[i].active.ptr))
5838 TAILQ_REMOVE(src->rules[i].active.ptr, r, entries);
5839 TAILQ_INSERT_TAIL(dst->rules[i].active.ptr, r, entries);
5840 dst->anchor->match++;
5842 src->anchor->match = 0;
5843 while ((r = TAILQ_FIRST(src->rules[i].inactive.ptr))
5845 TAILQ_REMOVE(src->rules[i].inactive.ptr, r, entries);
5846 TAILQ_INSERT_TAIL(dst->rules[i].inactive.ptr,
5853 decide_address_family(struct node_host *n, sa_family_t *af)
5855 if (*af != 0 || n == NULL)
5858 while ((n = n->next) != NULL) {
5867 remove_invalid_hosts(struct node_host **nh, sa_family_t *af)
5869 struct node_host *n = *nh, *prev = NULL;
5872 if (*af && n->af && n->af != *af) {
5873 /* unlink and free n */
5874 struct node_host *next = n->next;
5876 /* adjust tail pointer */
5877 if (n == (*nh)->tail)
5879 /* adjust previous node's next pointer */
5885 if (n->ifname != NULL)
5899 invalid_redirect(struct node_host *nh, sa_family_t af)
5902 struct node_host *n;
5904 /* tables and dyniftl are ok without an address family */
5905 for (n = nh; n != NULL; n = n->next) {
5906 if (n->addr.type != PF_ADDR_TABLE &&
5907 n->addr.type != PF_ADDR_DYNIFTL) {
5908 yyerror("address family not given and "
5909 "translation address expands to multiple "
5910 "address families");
5916 yyerror("no translation address with matching address family "
5924 atoul(char *s, u_long *ulvalp)
5930 ulval = strtoul(s, &ep, 0);
5931 if (s[0] == '\0' || *ep != '\0')
5933 if (errno == ERANGE && ulval == ULONG_MAX)
5945 if (atoul(n, &ulval) == 0) {
5946 if (ulval > 65535) {
5947 yyerror("illegal port value %lu", ulval);
5950 return (htons(ulval));
5952 s = getservbyname(n, "tcp");
5954 s = getservbyname(n, "udp");
5956 yyerror("unknown port %s", n);
5964 rule_label(struct pf_rule *r, char *s)
5967 if (strlcpy(r->label, s, sizeof(r->label)) >=
5969 yyerror("rule label too long (max %d chars)",
5970 sizeof(r->label)-1);
5978 parseicmpspec(char *w, sa_family_t af)
5980 const struct icmpcodeent *p;
5985 icmptype = returnicmpdefault >> 8;
5987 icmptype = returnicmp6default >> 8;
5989 if (atoul(w, &ulval) == -1) {
5990 if ((p = geticmpcodebyname(icmptype, w, af)) == NULL) {
5991 yyerror("unknown icmp code %s", w);
5997 yyerror("invalid icmp code %lu", ulval);
6000 return (icmptype << 8 | ulval);
6004 parseport(char *port, struct range *r, int extensions)
6006 char *p = strchr(port, ':');
6009 if ((r->a = getservice(port)) == -1)
6015 if ((extensions & PPORT_STAR) && !strcmp(p+1, "*")) {
6017 if ((r->a = getservice(port)) == -1)
6023 if ((extensions & PPORT_RANGE)) {
6025 if ((r->a = getservice(port)) == -1 ||
6026 (r->b = getservice(p)) == -1)
6039 pfctl_load_anchors(int dev, struct pfctl *pf, struct pfr_buffer *trans)
6041 struct loadanchors *la;
6043 TAILQ_FOREACH(la, &loadanchorshead, entries) {
6044 if (pf->opts & PF_OPT_VERBOSE)
6045 fprintf(stderr, "\nLoading anchor %s from %s\n",
6046 la->anchorname, la->filename);
6047 if (pfctl_rules(dev, la->filename, pf->opts, pf->optimize,
6048 la->anchorname, trans) == -1)