1 /* $OpenBSD: parse.y,v 1.549 2008/07/03 16:09:34 deraadt Exp $ */
4 * Copyright (c) 2001 Markus Friedl. All rights reserved.
5 * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
6 * Copyright (c) 2001 Theo de Raadt. All rights reserved.
7 * Copyright (c) 2002,2003 Henning Brauer. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/types.h>
31 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <netinet/in_systm.h>
36 #include <netinet/ip.h>
37 #include <netinet/ip_icmp.h>
38 #include <netinet/icmp6.h>
39 #include <net/pf/pfvar.h>
40 #include <arpa/inet.h>
41 #include <net/altq/altq.h>
42 #include <net/altq/altq_cbq.h>
43 #include <net/altq/altq_priq.h>
44 #include <net/altq/altq_hfsc.h>
45 #include <net/altq/altq_fairq.h>
63 #include "pfctl_parser.h"
66 static struct pfctl *pf = NULL;
68 static int rulestate = 0;
69 static u_int16_t returnicmpdefault =
70 (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
71 static u_int16_t returnicmp6default =
72 (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
73 static int blockpolicy = PFRULE_DROP;
74 static int require_order = 1;
75 static int default_statelock;
76 static int default_keeppolicy_action;
77 static struct node_state_opt *default_keeppolicy_options;
79 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
81 TAILQ_ENTRY(file) entry;
87 struct file *pushfile(const char *, int);
89 int check_file_secrecy(int, const char *);
92 int yyerror(const char *, ...) __printflike(1, 2);
93 int kw_cmp(const void *, const void *);
99 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
101 TAILQ_ENTRY(sym) entry;
107 int symset(const char *, const char *, int);
108 char *symget(const char *);
110 int atoul(char *, u_long *);
123 struct node_proto *next;
124 struct node_proto *tail;
130 struct node_port *next;
131 struct node_port *tail;
137 struct node_uid *next;
138 struct node_uid *tail;
144 struct node_gid *next;
145 struct node_gid *tail;
152 struct node_icmp *next;
153 struct node_icmp *tail;
156 enum { PF_STATE_OPT_MAX, PF_STATE_OPT_NOSYNC, PF_STATE_OPT_SRCTRACK,
157 PF_STATE_OPT_MAX_SRC_STATES, PF_STATE_OPT_MAX_SRC_CONN,
158 PF_STATE_OPT_MAX_SRC_CONN_RATE, PF_STATE_OPT_MAX_SRC_NODES,
159 PF_STATE_OPT_OVERLOAD, PF_STATE_OPT_STATELOCK,
160 PF_STATE_OPT_TIMEOUT, PF_STATE_OPT_SLOPPY, PF_STATE_OPT_PICKUPS };
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;
180 u_int8_t pickup_mode;
187 struct node_state_opt *next;
188 struct node_state_opt *tail;
192 struct node_host *host;
193 struct node_port *port;
197 char queue[PF_QNAME_SIZE];
198 char parent[PF_QNAME_SIZE];
199 char ifname[IFNAMSIZ];
201 struct node_queue *next;
202 struct node_queue *tail;
205 struct node_qassign {
212 #define FOM_FLAGS 0x01
213 #define FOM_ICMP 0x02
215 #define FOM_KEEP 0x08
216 #define FOM_SRCTRACK 0x10
217 struct node_uid *uid;
218 struct node_gid *gid;
225 struct node_icmp *icmpspec;
230 struct node_state_opt *options;
235 struct node_qassign queues;
238 u_int8_t match_tag_not;
241 struct node_host *addr;
246 struct antispoof_opts {
253 #define SOM_MINTTL 0x01
254 #define SOM_MAXMSS 0x02
255 #define SOM_FRAGCACHE 0x04
256 #define SOM_SETTOS 0x08
265 u_int8_t match_tag_not;
271 #define QOM_BWSPEC 0x01
272 #define QOM_SCHEDULER 0x02
273 #define QOM_PRIORITY 0x04
274 #define QOM_TBRSIZE 0x08
275 #define QOM_QLIMIT 0x10
276 struct node_queue_bw queue_bwspec;
277 struct node_queue_opt scheduler;
286 struct node_tinithead init_nodes;
291 #define POM_TYPE 0x01
292 #define POM_STICKYADDRESS 0x02
296 struct pf_poolhashkey *key;
301 struct node_hfsc_opts hfsc_opts;
302 struct node_fairq_opts fairq_opts;
304 int disallow_table(struct node_host *, const char *);
305 int disallow_urpf_failed(struct node_host *, const char *);
306 int disallow_alias(struct node_host *, const char *);
307 int rule_consistent(struct pf_rule *, int);
308 int filter_consistent(struct pf_rule *, int);
309 int nat_consistent(struct pf_rule *);
310 int rdr_consistent(struct pf_rule *);
311 int process_tabledef(char *, struct table_opts *);
312 void expand_label_str(char *, size_t, const char *, const char *);
313 void expand_label_if(const char *, char *, size_t, const char *);
314 void expand_label_addr(const char *, char *, size_t, u_int8_t,
316 void expand_label_port(const char *, char *, size_t,
318 void expand_label_proto(const char *, char *, size_t, u_int8_t);
319 void expand_label_nr(const char *, char *, size_t);
320 void expand_label(char *, size_t, const char *, u_int8_t,
321 struct node_host *, struct node_port *, struct node_host *,
322 struct node_port *, u_int8_t);
323 void expand_rule(struct pf_rule *, struct node_if *,
324 struct node_host *, struct node_proto *, struct node_os *,
325 struct node_host *, struct node_port *, struct node_host *,
326 struct node_port *, struct node_uid *, struct node_gid *,
327 struct node_icmp *, const char *);
328 int expand_altq(struct pf_altq *, struct node_if *,
329 struct node_queue *, struct node_queue_bw bwspec,
330 struct node_queue_opt *);
331 int expand_queue(struct pf_altq *, struct node_if *,
332 struct node_queue *, struct node_queue_bw,
333 struct node_queue_opt *);
334 int expand_skip_interface(struct node_if *);
336 int check_rulestate(int);
337 int getservice(char *);
338 int rule_label(struct pf_rule *, char *);
340 void mv_rules(struct pf_ruleset *, struct pf_ruleset *);
341 void decide_address_family(struct node_host *, sa_family_t *);
342 void remove_invalid_hosts(struct node_host **, sa_family_t *);
343 int invalid_redirect(struct node_host *, sa_family_t);
344 u_int16_t parseicmpspec(char *, sa_family_t);
346 TAILQ_HEAD(loadanchorshead, loadanchors)
347 loadanchorshead = TAILQ_HEAD_INITIALIZER(loadanchorshead);
350 TAILQ_ENTRY(loadanchors) entries;
373 struct node_if *interface;
374 struct node_proto *proto;
375 struct node_icmp *icmp;
376 struct node_host *host;
378 struct node_port *port;
379 struct node_uid *uid;
380 struct node_gid *gid;
381 struct node_state_opt *state_opt;
384 struct peer src, dst;
385 struct node_os *src_os;
388 struct node_host *host;
392 struct pf_poolhashkey *key;
395 struct node_host *host;
400 struct node_state_opt *options;
411 struct pf_poolhashkey *hashkey;
412 struct node_queue *queue;
413 struct node_queue_opt queue_options;
414 struct node_queue_bw queue_bwspec;
415 struct node_qassign qassign;
416 struct filter_opts filter_opts;
417 struct antispoof_opts antispoof_opts;
418 struct queue_opts queue_opts;
419 struct scrub_opts scrub_opts;
420 struct table_opts table_opts;
421 struct pool_opts pool_opts;
422 struct node_hfsc_opts hfsc_opts;
423 struct node_fairq_opts fairq_opts;
428 #define PPORT_RANGE 1
430 int parseport(char *, struct range *r, int);
432 #define DYNIF_MULTIADDR(addr) ((addr).type == PF_ADDR_DYNIFTL && \
433 (!((addr).iflags & PFI_AFLAG_NOALIAS) || \
434 !isdigit((addr).v.ifname[strlen((addr).v.ifname)-1])))
440 %token PASS BLOCK SCRUB RETURN IN OS OUT LOG QUICK ON FROM TO FLAGS
441 %token RETURNRST RETURNICMP RETURNICMP6 PROTO INET INET6 ALL ANY ICMPTYPE
442 %token ICMP6TYPE CODE KEEP MODULATE STATE PORT RDR NAT BINAT ARROW NODF
443 %token MINTTL ERROR ALLOWOPTS FASTROUTE FILENAME ROUTETO DUPTO REPLYTO NO LABEL
444 %token NOROUTE URPFFAILED FRAGMENT USER GROUP MAXMSS MAXIMUM TTL TOS DROP TABLE
445 %token REASSEMBLE FRAGDROP FRAGCROP ANCHOR NATANCHOR RDRANCHOR BINATANCHOR
446 %token SET OPTIMIZATION TIMEOUT LIMIT LOGINTERFACE BLOCKPOLICY RANDOMID
447 %token REQUIREORDER SYNPROXY FINGERPRINTS NOSYNC DEBUG SKIP HOSTID
448 %token ANTISPOOF FOR INCLUDE
449 %token BITMASK RANDOM SOURCEHASH ROUNDROBIN STATICPORT PROBABILITY
450 %token ALTQ CBQ PRIQ HFSC FAIRQ BANDWIDTH TBRSIZE LINKSHARE REALTIME UPPERLIMIT
451 %token QUEUE PRIORITY QLIMIT RTABLE HOGS BUCKETS
452 %token LOAD RULESET_OPTIMIZATION
453 %token PICKUPS NOPICKUPS HASHONLY
454 %token STICKYADDRESS MAXSRCSTATES MAXSRCNODES SOURCETRACK GLOBAL RULE
455 %token MAXSRCCONN MAXSRCCONNRATE OVERLOAD FLUSH SLOPPY
456 %token TAGGED TAG IFBOUND FLOATING STATEPOLICY ROUTE SETTOS KEEPPOLICY
457 %token DIVERTTO DIVERTREPLY
458 %token <v.string> STRING
459 %token <v.number> NUMBER
460 %token <v.i> PORTBINARY
461 %type <v.interface> interface if_list if_item_not if_item
462 %type <v.number> number icmptype icmp6type uid gid
463 %type <v.number> tos not yesno
464 %type <v.probability> probability
465 %type <v.i> no dir af fragcache optimizer
466 %type <v.i> sourcetrack flush unaryop statelock
467 %type <v.b> action nataction natpasslog scrubaction
468 %type <v.b> flags flag blockspec
469 %type <v.range> portplain portstar portrange
470 %type <v.hashkey> hashkey
471 %type <v.proto> proto proto_list proto_item
472 %type <v.number> protoval
473 %type <v.icmp> icmpspec
474 %type <v.icmp> icmp_list icmp_item
475 %type <v.icmp> icmp6_list icmp6_item
476 %type <v.number> reticmpspec reticmp6spec
477 %type <v.fromto> fromto
478 %type <v.peer> ipportspec from to
479 %type <v.host> ipspec toipspec xhost host dynaddr host_list
480 %type <v.host> redir_host_list redirspec
481 %type <v.host> route_host route_host_list routespec
482 %type <v.os> os xos os_list
483 %type <v.port> portspec port_list port_item
484 %type <v.uid> uids uid_list uid_item
485 %type <v.gid> gids gid_list gid_item
486 %type <v.route> route
487 %type <v.redirection> redirection redirpool
488 %type <v.string> label stringall tag anchorname
489 %type <v.string> string varstring numberstring
490 %type <v.keep_state> keep
491 %type <v.state_opt> state_opt_spec state_opt_list state_opt_item
492 %type <v.logquick> logquick quick log logopts logopt
493 %type <v.interface> antispoof_ifspc antispoof_iflst antispoof_if
494 %type <v.qassign> qname
495 %type <v.queue> qassign qassign_list qassign_item
496 %type <v.queue_options> scheduler
497 %type <v.number> cbqflags_list cbqflags_item
498 %type <v.number> priqflags_list priqflags_item
499 %type <v.hfsc_opts> hfscopts_list hfscopts_item hfsc_opts
500 %type <v.fairq_opts> fairqopts_list fairqopts_item fairq_opts
501 %type <v.queue_bwspec> bandwidth
502 %type <v.filter_opts> filter_opts filter_opt filter_opts_l
503 %type <v.antispoof_opts> antispoof_opts antispoof_opt antispoof_opts_l
504 %type <v.queue_opts> queue_opts queue_opt queue_opts_l
505 %type <v.scrub_opts> scrub_opts scrub_opt scrub_opts_l
506 %type <v.table_opts> table_opts table_opt table_opts_l
507 %type <v.pool_opts> pool_opts pool_opt pool_opts_l
508 %type <v.tagged> tagged
509 %type <v.rtableid> rtable
512 ruleset : /* empty */
513 | ruleset include '\n'
515 | ruleset option '\n'
516 | ruleset scrubrule '\n'
517 | ruleset natrule '\n'
518 | ruleset binatrule '\n'
519 | ruleset pfrule '\n'
520 | ruleset anchorrule '\n'
521 | ruleset loadrule '\n'
522 | ruleset altqif '\n'
523 | ruleset queuespec '\n'
524 | ruleset varset '\n'
525 | ruleset antispoof '\n'
526 | ruleset tabledef '\n'
527 | '{' fakeanchor '}' '\n';
528 | ruleset error '\n' { file->errors++; }
531 include : INCLUDE STRING {
534 if ((nfile = pushfile($2, 0)) == NULL) {
535 yyerror("failed to include file %s", $2);
547 * apply to previouslys specified rule: must be careful to note
548 * what that is: pf or nat or binat or rdr
550 fakeanchor : fakeanchor '\n'
551 | fakeanchor anchorrule '\n'
552 | fakeanchor binatrule '\n'
553 | fakeanchor natrule '\n'
554 | fakeanchor pfrule '\n'
555 | fakeanchor error '\n'
559 if (!strcmp($1, "none"))
561 else if (!strcmp($1, "basic"))
562 $$ = PF_OPTIMIZE_BASIC;
563 else if (!strcmp($1, "profile"))
564 $$ = PF_OPTIMIZE_BASIC | PF_OPTIMIZE_PROFILE;
566 yyerror("unknown ruleset-optimization %s", $1);
572 option : SET OPTIMIZATION STRING {
573 if (check_rulestate(PFCTL_STATE_OPTION)) {
577 if (pfctl_set_optimization(pf, $3) != 0) {
578 yyerror("unknown optimization %s", $3);
584 | SET RULESET_OPTIMIZATION optimizer {
585 if (!(pf->opts & PF_OPT_OPTIMIZE)) {
586 pf->opts |= PF_OPT_OPTIMIZE;
590 | SET TIMEOUT timeout_spec
591 | SET TIMEOUT '{' optnl timeout_list '}'
592 | SET LIMIT limit_spec
593 | SET LIMIT '{' optnl limit_list '}'
594 | SET LOGINTERFACE stringall {
595 if (check_rulestate(PFCTL_STATE_OPTION)) {
599 if (pfctl_set_logif(pf, $3) != 0) {
600 yyerror("error setting loginterface %s", $3);
606 | SET HOSTID number {
607 if ($3 == 0 || $3 > UINT_MAX) {
608 yyerror("hostid must be non-zero");
611 if (pfctl_set_hostid(pf, $3) != 0) {
612 yyerror("error setting hostid %08" PRId64, $3);
616 | SET BLOCKPOLICY DROP {
617 if (pf->opts & PF_OPT_VERBOSE)
618 printf("set block-policy drop\n");
619 if (check_rulestate(PFCTL_STATE_OPTION))
621 blockpolicy = PFRULE_DROP;
623 | SET BLOCKPOLICY RETURN {
624 if (pf->opts & PF_OPT_VERBOSE)
625 printf("set block-policy return\n");
626 if (check_rulestate(PFCTL_STATE_OPTION))
628 blockpolicy = PFRULE_RETURN;
630 | SET REQUIREORDER yesno {
631 if (pf->opts & PF_OPT_VERBOSE)
632 printf("set require-order %s\n",
633 $3 == 1 ? "yes" : "no");
636 | SET FINGERPRINTS STRING {
637 if (pf->opts & PF_OPT_VERBOSE)
638 printf("set fingerprints \"%s\"\n", $3);
639 if (check_rulestate(PFCTL_STATE_OPTION)) {
643 if (!pf->anchor->name[0]) {
644 if (pfctl_file_fingerprints(pf->dev,
646 yyerror("error loading "
647 "fingerprints %s", $3);
654 | SET STATEPOLICY statelock {
655 if (pf->opts & PF_OPT_VERBOSE)
658 printf("set state-policy floating\n");
661 printf("set state-policy if-bound\n");
664 default_statelock = $3;
666 | SET KEEPPOLICY keep {
667 if (pf->opts & PF_OPT_VERBOSE)
668 printf("A default keeppolicy was set\n");
669 default_keeppolicy_action = $3.action;
670 default_keeppolicy_options = $3.options;
673 if (check_rulestate(PFCTL_STATE_OPTION)) {
677 if (pfctl_set_debug(pf, $3) != 0) {
678 yyerror("error setting debuglevel %s", $3);
684 | SET SKIP interface {
685 if (expand_skip_interface($3) != 0) {
686 yyerror("error setting skip interface(s)");
692 stringall : STRING { $$ = $1; }
694 if (($$ = strdup("all")) == NULL) {
695 err(1, "stringall: strdup");
700 string : STRING string {
701 if (asprintf(&$$, "%s %s", $1, $2) == -1)
702 err(1, "string: asprintf");
709 varstring : numberstring varstring {
710 if (asprintf(&$$, "%s %s", $1, $2) == -1)
711 err(1, "string: asprintf");
718 numberstring : NUMBER {
720 if (asprintf(&s, "%" PRId64, $1) == -1) {
721 yyerror("string: asprintf");
729 varset : STRING '=' varstring {
730 if (pf->opts & PF_OPT_VERBOSE)
731 printf("%s = \"%s\"\n", $1, $3);
732 if (symset($1, $3, 0) == -1)
733 err(1, "cannot store variable %s", $1);
739 anchorname : STRING { $$ = $1; }
740 | /* empty */ { $$ = NULL; }
743 pfa_anchorlist : /* empty */
744 | pfa_anchorlist '\n'
745 | pfa_anchorlist pfrule '\n'
746 | pfa_anchorlist anchorrule '\n'
751 char ta[PF_ANCHOR_NAME_SIZE];
752 struct pf_ruleset *rs;
754 /* steping into a brace anchor */
759 /* create a holding ruleset in the root */
760 snprintf(ta, PF_ANCHOR_NAME_SIZE, "_%d", pf->bn);
761 rs = pf_find_or_create_ruleset(ta);
763 err(1, "pfa_anchor: pf_find_or_create_ruleset");
764 pf->astack[pf->asd] = rs->anchor;
765 pf->anchor = rs->anchor;
766 } '\n' pfa_anchorlist '}'
768 pf->alast = pf->anchor;
770 pf->anchor = pf->astack[pf->asd];
775 anchorrule : ANCHOR anchorname dir quick interface af proto fromto
776 filter_opts pfa_anchor
779 struct node_proto *proto;
781 if (check_rulestate(PFCTL_STATE_FILTER)) {
787 if ($2 && ($2[0] == '_' || strstr($2, "/_") != NULL)) {
789 yyerror("anchor names beginning with '_' "
790 "are reserved for internal use");
794 memset(&r, 0, sizeof(r));
795 if (pf->astack[pf->asd + 1]) {
796 /* move inline rules into relative location */
798 &pf->astack[pf->asd]->ruleset,
799 $2 ? $2 : pf->alast->name);
801 if (r.anchor == NULL)
802 err(1, "anchorrule: unable to "
805 if (pf->alast != r.anchor) {
806 if (r.anchor->match) {
807 yyerror("inline anchor '%s' "
812 mv_rules(&pf->alast->ruleset,
815 pf_remove_if_empty_ruleset(&pf->alast->ruleset);
816 pf->alast = r.anchor;
819 yyerror("anchors without explicit "
820 "rules must specify a name");
828 r.rtableid = $9.rtableid;
831 if (strlcpy(r.tagname, $9.tag,
832 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
833 yyerror("tag too long, max %u chars",
834 PF_TAG_NAME_SIZE - 1);
838 if (strlcpy(r.match_tagname, $9.match_tag,
839 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
840 yyerror("tag too long, max %u chars",
841 PF_TAG_NAME_SIZE - 1);
844 r.match_tag_not = $9.match_tag_not;
845 if (rule_label(&r, $9.label))
848 r.flags = $9.flags.b1;
849 r.flagset = $9.flags.b2;
850 if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
851 yyerror("flags always false");
854 if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
855 for (proto = $7; proto != NULL &&
856 proto->proto != IPPROTO_TCP;
859 if (proto == NULL && $7 != NULL) {
860 if ($9.flags.b1 || $9.flags.b2)
862 "flags only apply to tcp");
865 "OS fingerprinting only "
873 if ($9.keep.action) {
874 yyerror("cannot specify state handling "
880 if (strlcpy(r.match_tagname, $9.match_tag,
881 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
882 yyerror("tag too long, max %u chars",
883 PF_TAG_NAME_SIZE - 1);
886 r.match_tag_not = $9.match_tag_not;
888 decide_address_family($8.src.host, &r.af);
889 decide_address_family($8.dst.host, &r.af);
891 expand_rule(&r, $5, NULL, $7, $8.src_os,
892 $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
893 $9.uid, $9.gid, $9.icmpspec,
894 pf->astack[pf->asd + 1] ? pf->alast->name : $2);
896 pf->astack[pf->asd + 1] = NULL;
898 | NATANCHOR string interface af proto fromto rtable {
901 if (check_rulestate(PFCTL_STATE_NAT)) {
906 memset(&r, 0, sizeof(r));
911 decide_address_family($6.src.host, &r.af);
912 decide_address_family($6.dst.host, &r.af);
914 expand_rule(&r, $3, NULL, $5, $6.src_os,
915 $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
919 | RDRANCHOR string interface af proto fromto rtable {
922 if (check_rulestate(PFCTL_STATE_NAT)) {
927 memset(&r, 0, sizeof(r));
932 decide_address_family($6.src.host, &r.af);
933 decide_address_family($6.dst.host, &r.af);
935 if ($6.src.port != NULL) {
936 yyerror("source port parameter not supported"
940 if ($6.dst.port != NULL) {
941 if ($6.dst.port->next != NULL) {
942 yyerror("destination port list "
943 "expansion not supported in "
946 } else if ($6.dst.port->op != PF_OP_EQ) {
947 yyerror("destination port operators"
948 " not supported in rdr-anchor");
951 r.dst.port[0] = $6.dst.port->port[0];
952 r.dst.port[1] = $6.dst.port->port[1];
953 r.dst.port_op = $6.dst.port->op;
956 expand_rule(&r, $3, NULL, $5, $6.src_os,
957 $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
961 | BINATANCHOR string interface af proto fromto rtable {
964 if (check_rulestate(PFCTL_STATE_NAT)) {
969 memset(&r, 0, sizeof(r));
974 if ($5->next != NULL) {
975 yyerror("proto list expansion"
976 " not supported in binat-anchor");
983 if ($6.src.host != NULL || $6.src.port != NULL ||
984 $6.dst.host != NULL || $6.dst.port != NULL) {
985 yyerror("fromto parameter not supported"
990 decide_address_family($6.src.host, &r.af);
991 decide_address_family($6.dst.host, &r.af);
993 pfctl_add_rule(pf, &r, $2);
998 loadrule : LOAD ANCHOR string FROM string {
999 struct loadanchors *loadanchor;
1001 if (strlen(pf->anchor->name) + 1 +
1002 strlen($3) >= MAXPATHLEN) {
1003 yyerror("anchorname %s too long, max %u\n",
1004 $3, MAXPATHLEN - 1);
1008 loadanchor = calloc(1, sizeof(struct loadanchors));
1009 if (loadanchor == NULL)
1010 err(1, "loadrule: calloc");
1011 if ((loadanchor->anchorname = malloc(MAXPATHLEN)) ==
1013 err(1, "loadrule: malloc");
1014 if (pf->anchor->name[0])
1015 snprintf(loadanchor->anchorname, MAXPATHLEN,
1016 "%s/%s", pf->anchor->name, $3);
1018 strlcpy(loadanchor->anchorname, $3, MAXPATHLEN);
1019 if ((loadanchor->filename = strdup($5)) == NULL)
1020 err(1, "loadrule: strdup");
1022 TAILQ_INSERT_TAIL(&loadanchorshead, loadanchor,
1029 scrubaction : no SCRUB {
1038 scrubrule : scrubaction dir logquick interface af proto fromto scrub_opts
1042 if (check_rulestate(PFCTL_STATE_SCRUB))
1045 memset(&r, 0, sizeof(r));
1053 yyerror("scrub rules do not support 'quick'");
1059 r.rule_flag |= PFRULE_NODF;
1061 r.rule_flag |= PFRULE_RANDOMID;
1062 if ($8.reassemble_tcp) {
1063 if (r.direction != PF_INOUT) {
1064 yyerror("reassemble tcp rules can not "
1065 "specify direction");
1068 r.rule_flag |= PFRULE_REASSEMBLE_TCP;
1071 r.min_ttl = $8.minttl;
1073 r.max_mss = $8.maxmss;
1074 if ($8.marker & SOM_SETTOS) {
1075 r.rule_flag |= PFRULE_SET_TOS;
1076 r.set_tos = $8.settos;
1079 r.rule_flag |= $8.fragcache;
1081 if (strlcpy(r.match_tagname, $8.match_tag,
1082 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1083 yyerror("tag too long, max %u chars",
1084 PF_TAG_NAME_SIZE - 1);
1087 r.match_tag_not = $8.match_tag_not;
1088 r.rtableid = $8.rtableid;
1090 expand_rule(&r, $4, NULL, $6, $7.src_os,
1091 $7.src.host, $7.src.port, $7.dst.host, $7.dst.port,
1092 NULL, NULL, NULL, "");
1097 bzero(&scrub_opts, sizeof scrub_opts);
1098 scrub_opts.rtableid = -1;
1101 { $$ = scrub_opts; }
1103 bzero(&scrub_opts, sizeof scrub_opts);
1104 scrub_opts.rtableid = -1;
1109 scrub_opts_l : scrub_opts_l scrub_opt
1114 if (scrub_opts.nodf) {
1115 yyerror("no-df cannot be respecified");
1118 scrub_opts.nodf = 1;
1121 if (scrub_opts.marker & SOM_MINTTL) {
1122 yyerror("min-ttl cannot be respecified");
1125 if ($2 < 0 || $2 > 255) {
1126 yyerror("illegal min-ttl value %" PRId64, $2);
1129 scrub_opts.marker |= SOM_MINTTL;
1130 scrub_opts.minttl = $2;
1133 if (scrub_opts.marker & SOM_MAXMSS) {
1134 yyerror("max-mss cannot be respecified");
1137 if ($2 < 0 || $2 > 65535) {
1138 yyerror("illegal max-mss value %" PRId64, $2);
1141 scrub_opts.marker |= SOM_MAXMSS;
1142 scrub_opts.maxmss = $2;
1145 if (scrub_opts.marker & SOM_SETTOS) {
1146 yyerror("set-tos cannot be respecified");
1149 scrub_opts.marker |= SOM_SETTOS;
1150 scrub_opts.settos = $2;
1153 if (scrub_opts.marker & SOM_FRAGCACHE) {
1154 yyerror("fragcache cannot be respecified");
1157 scrub_opts.marker |= SOM_FRAGCACHE;
1158 scrub_opts.fragcache = $1;
1160 | REASSEMBLE STRING {
1161 if (strcasecmp($2, "tcp") != 0) {
1162 yyerror("scrub reassemble supports only tcp, "
1168 if (scrub_opts.reassemble_tcp) {
1169 yyerror("reassemble tcp cannot be respecified");
1172 scrub_opts.reassemble_tcp = 1;
1175 if (scrub_opts.randomid) {
1176 yyerror("random-id cannot be respecified");
1179 scrub_opts.randomid = 1;
1182 scrub_opts.rtableid = $2;
1184 | not TAGGED string {
1185 scrub_opts.match_tag = $3;
1186 scrub_opts.match_tag_not = $1;
1190 fragcache : FRAGMENT REASSEMBLE { $$ = 0; /* default */ }
1191 | FRAGMENT FRAGCROP { $$ = PFRULE_FRAGCROP; }
1192 | FRAGMENT FRAGDROP { $$ = PFRULE_FRAGDROP; }
1195 antispoof : ANTISPOOF logquick antispoof_ifspc af antispoof_opts {
1197 struct node_host *h = NULL, *hh;
1198 struct node_if *i, *j;
1200 if (check_rulestate(PFCTL_STATE_FILTER))
1203 for (i = $3; i; i = i->next) {
1204 bzero(&r, sizeof(r));
1207 r.direction = PF_IN;
1212 if (rule_label(&r, $5.label))
1214 r.rtableid = $5.rtableid;
1215 j = calloc(1, sizeof(struct node_if));
1217 err(1, "antispoof: calloc");
1218 if (strlcpy(j->ifname, i->ifname,
1219 sizeof(j->ifname)) >= sizeof(j->ifname)) {
1221 yyerror("interface name too long");
1226 h = calloc(1, sizeof(*h));
1228 err(1, "address: calloc");
1229 h->addr.type = PF_ADDR_DYNIFTL;
1231 if (strlcpy(h->addr.v.ifname, i->ifname,
1232 sizeof(h->addr.v.ifname)) >=
1233 sizeof(h->addr.v.ifname)) {
1236 "interface name too long");
1239 hh = malloc(sizeof(*hh));
1241 err(1, "address: malloc");
1242 bcopy(h, hh, sizeof(*hh));
1243 h->addr.iflags = PFI_AFLAG_NETWORK;
1245 h = ifa_lookup(j->ifname,
1251 expand_rule(&r, j, NULL, NULL, NULL, h,
1252 NULL, NULL, NULL, NULL, NULL,
1255 if ((i->ifa_flags & IFF_LOOPBACK) == 0) {
1256 bzero(&r, sizeof(r));
1259 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 antispoof_opts.rtableid = $2;
1329 not : '!' { $$ = 1; }
1330 | /* empty */ { $$ = 0; }
1333 tabledef : TABLE '<' STRING '>' table_opts {
1334 struct node_host *h, *nh;
1335 struct node_tinit *ti, *nti;
1337 if (strlen($3) >= PF_TABLE_NAME_SIZE) {
1338 yyerror("table name too long, max %d chars",
1339 PF_TABLE_NAME_SIZE - 1);
1343 if (pf->loadopt & PFCTL_FLAG_TABLE)
1344 if (process_tabledef($3, &$5)) {
1349 for (ti = SIMPLEQ_FIRST(&$5.init_nodes);
1350 ti != SIMPLEQ_END(&$5.init_nodes); ti = nti) {
1353 for (h = ti->host; h != NULL; h = nh) {
1357 nti = SIMPLEQ_NEXT(ti, entries);
1364 bzero(&table_opts, sizeof table_opts);
1365 SIMPLEQ_INIT(&table_opts.init_nodes);
1368 { $$ = table_opts; }
1371 bzero(&table_opts, sizeof table_opts);
1372 SIMPLEQ_INIT(&table_opts.init_nodes);
1377 table_opts_l : table_opts_l table_opt
1381 table_opt : STRING {
1382 if (!strcmp($1, "const"))
1383 table_opts.flags |= PFR_TFLAG_CONST;
1384 else if (!strcmp($1, "persist"))
1385 table_opts.flags |= PFR_TFLAG_PERSIST;
1386 else if (!strcmp($1, "counters"))
1387 table_opts.flags |= PFR_TFLAG_COUNTERS;
1389 yyerror("invalid table option '%s'", $1);
1395 | '{' optnl '}' { table_opts.init_addr = 1; }
1396 | '{' optnl host_list '}' {
1397 struct node_host *n;
1398 struct node_tinit *ti;
1400 for (n = $3; n != NULL; n = n->next) {
1401 switch (n->addr.type) {
1402 case PF_ADDR_ADDRMASK:
1405 yyerror("address ranges are not "
1406 "permitted inside tables");
1408 case PF_ADDR_DYNIFTL:
1409 yyerror("dynamic addresses are not "
1410 "permitted inside tables");
1413 yyerror("tables cannot contain tables");
1415 case PF_ADDR_NOROUTE:
1416 yyerror("\"no-route\" is not permitted "
1419 case PF_ADDR_URPFFAILED:
1420 yyerror("\"urpf-failed\" is not "
1421 "permitted inside tables");
1424 yyerror("unknown address type %d",
1429 if (!(ti = calloc(1, sizeof(*ti))))
1430 err(1, "table_opt: calloc");
1432 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1434 table_opts.init_addr = 1;
1437 struct node_tinit *ti;
1439 if (!(ti = calloc(1, sizeof(*ti))))
1440 err(1, "table_opt: calloc");
1442 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1444 table_opts.init_addr = 1;
1448 altqif : ALTQ interface queue_opts QUEUE qassign {
1451 if (check_rulestate(PFCTL_STATE_QUEUE))
1454 memset(&a, 0, sizeof(a));
1455 if ($3.scheduler.qtype == ALTQT_NONE) {
1456 yyerror("no scheduler specified!");
1459 a.scheduler = $3.scheduler.qtype;
1460 a.qlimit = $3.qlimit;
1461 a.tbrsize = $3.tbrsize;
1463 yyerror("no child queues specified");
1466 if (expand_altq(&a, $2, $5, $3.queue_bwspec,
1472 queuespec : QUEUE STRING interface queue_opts qassign {
1475 if (check_rulestate(PFCTL_STATE_QUEUE)) {
1480 memset(&a, 0, sizeof(a));
1482 if (strlcpy(a.qname, $2, sizeof(a.qname)) >=
1484 yyerror("queue name too long (max "
1485 "%d chars)", PF_QNAME_SIZE-1);
1491 yyerror("cannot specify tbrsize for queue");
1494 if ($4.priority > 255) {
1495 yyerror("priority out of range: max 255");
1498 a.priority = $4.priority;
1499 a.qlimit = $4.qlimit;
1500 a.scheduler = $4.scheduler.qtype;
1501 if (expand_queue(&a, $3, $5, $4.queue_bwspec,
1503 yyerror("errors in queue definition");
1510 bzero(&queue_opts, sizeof queue_opts);
1511 queue_opts.priority = DEFAULT_PRIORITY;
1512 queue_opts.qlimit = DEFAULT_QLIMIT;
1513 queue_opts.scheduler.qtype = ALTQT_NONE;
1514 queue_opts.queue_bwspec.bw_percent = 100;
1517 { $$ = queue_opts; }
1519 bzero(&queue_opts, sizeof queue_opts);
1520 queue_opts.priority = DEFAULT_PRIORITY;
1521 queue_opts.qlimit = DEFAULT_QLIMIT;
1522 queue_opts.scheduler.qtype = ALTQT_NONE;
1523 queue_opts.queue_bwspec.bw_percent = 100;
1528 queue_opts_l : queue_opts_l queue_opt
1532 queue_opt : BANDWIDTH bandwidth {
1533 if (queue_opts.marker & QOM_BWSPEC) {
1534 yyerror("bandwidth cannot be respecified");
1537 queue_opts.marker |= QOM_BWSPEC;
1538 queue_opts.queue_bwspec = $2;
1541 if (queue_opts.marker & QOM_PRIORITY) {
1542 yyerror("priority cannot be respecified");
1545 if ($2 < 0 || $2 > 255) {
1546 yyerror("priority out of range: max 255");
1549 queue_opts.marker |= QOM_PRIORITY;
1550 queue_opts.priority = $2;
1553 if (queue_opts.marker & QOM_QLIMIT) {
1554 yyerror("qlimit cannot be respecified");
1557 if ($2 < 0 || $2 > 65535) {
1558 yyerror("qlimit out of range: max 65535");
1561 queue_opts.marker |= QOM_QLIMIT;
1562 queue_opts.qlimit = $2;
1565 if (queue_opts.marker & QOM_SCHEDULER) {
1566 yyerror("scheduler cannot be respecified");
1569 queue_opts.marker |= QOM_SCHEDULER;
1570 queue_opts.scheduler = $1;
1573 if (queue_opts.marker & QOM_TBRSIZE) {
1574 yyerror("tbrsize cannot be respecified");
1577 if ($2 < 0 || $2 > 65535) {
1578 yyerror("tbrsize too big: max 65535");
1581 queue_opts.marker |= QOM_TBRSIZE;
1582 queue_opts.tbrsize = $2;
1586 bandwidth : STRING {
1592 bps = strtod($1, &cp);
1594 if (!strcmp(cp, "b"))
1596 else if (!strcmp(cp, "Kb"))
1598 else if (!strcmp(cp, "Mb"))
1600 else if (!strcmp(cp, "Gb"))
1601 bps *= 1000 * 1000 * 1000;
1602 else if (!strcmp(cp, "%")) {
1603 if (bps < 0 || bps > 100) {
1604 yyerror("bandwidth spec "
1609 $$.bw_percent = bps;
1612 yyerror("unknown unit %s", cp);
1618 $$.bw_absolute = (u_int32_t)bps;
1621 if ($1 < 0 || $1 > UINT_MAX) {
1622 yyerror("bandwidth number too big");
1626 $$.bw_absolute = $1;
1631 $$.qtype = ALTQT_CBQ;
1632 $$.data.cbq_opts.flags = 0;
1634 | CBQ '(' cbqflags_list ')' {
1635 $$.qtype = ALTQT_CBQ;
1636 $$.data.cbq_opts.flags = $3;
1639 $$.qtype = ALTQT_PRIQ;
1640 $$.data.priq_opts.flags = 0;
1642 | PRIQ '(' priqflags_list ')' {
1643 $$.qtype = ALTQT_PRIQ;
1644 $$.data.priq_opts.flags = $3;
1647 $$.qtype = ALTQT_HFSC;
1648 bzero(&$$.data.hfsc_opts,
1649 sizeof(struct node_hfsc_opts));
1651 | HFSC '(' hfsc_opts ')' {
1652 $$.qtype = ALTQT_HFSC;
1653 $$.data.hfsc_opts = $3;
1656 $$.qtype = ALTQT_FAIRQ;
1657 bzero(&$$.data.fairq_opts,
1658 sizeof(struct node_fairq_opts));
1660 | FAIRQ '(' fairq_opts ')' {
1661 $$.qtype = ALTQT_FAIRQ;
1662 $$.data.fairq_opts = $3;
1666 cbqflags_list : cbqflags_item { $$ |= $1; }
1667 | cbqflags_list comma cbqflags_item { $$ |= $3; }
1670 cbqflags_item : STRING {
1671 if (!strcmp($1, "default"))
1672 $$ = CBQCLF_DEFCLASS;
1673 else if (!strcmp($1, "borrow"))
1675 else if (!strcmp($1, "red"))
1677 else if (!strcmp($1, "ecn"))
1678 $$ = CBQCLF_RED|CBQCLF_ECN;
1679 else if (!strcmp($1, "rio"))
1682 yyerror("unknown cbq flag \"%s\"", $1);
1690 priqflags_list : priqflags_item { $$ |= $1; }
1691 | priqflags_list comma priqflags_item { $$ |= $3; }
1694 priqflags_item : STRING {
1695 if (!strcmp($1, "default"))
1696 $$ = PRCF_DEFAULTCLASS;
1697 else if (!strcmp($1, "red"))
1699 else if (!strcmp($1, "ecn"))
1700 $$ = PRCF_RED|PRCF_ECN;
1701 else if (!strcmp($1, "rio"))
1704 yyerror("unknown priq flag \"%s\"", $1);
1714 sizeof(struct node_hfsc_opts));
1721 hfscopts_list : hfscopts_item
1722 | hfscopts_list comma hfscopts_item
1725 hfscopts_item : LINKSHARE bandwidth {
1726 if (hfsc_opts.linkshare.used) {
1727 yyerror("linkshare already specified");
1730 hfsc_opts.linkshare.m2 = $2;
1731 hfsc_opts.linkshare.used = 1;
1733 | LINKSHARE '(' bandwidth comma NUMBER comma bandwidth ')'
1735 if ($5 < 0 || $5 > INT_MAX) {
1736 yyerror("timing in curve out of range");
1739 if (hfsc_opts.linkshare.used) {
1740 yyerror("linkshare already specified");
1743 hfsc_opts.linkshare.m1 = $3;
1744 hfsc_opts.linkshare.d = $5;
1745 hfsc_opts.linkshare.m2 = $7;
1746 hfsc_opts.linkshare.used = 1;
1748 | REALTIME bandwidth {
1749 if (hfsc_opts.realtime.used) {
1750 yyerror("realtime already specified");
1753 hfsc_opts.realtime.m2 = $2;
1754 hfsc_opts.realtime.used = 1;
1756 | REALTIME '(' bandwidth comma NUMBER comma bandwidth ')'
1758 if ($5 < 0 || $5 > INT_MAX) {
1759 yyerror("timing in curve out of range");
1762 if (hfsc_opts.realtime.used) {
1763 yyerror("realtime already specified");
1766 hfsc_opts.realtime.m1 = $3;
1767 hfsc_opts.realtime.d = $5;
1768 hfsc_opts.realtime.m2 = $7;
1769 hfsc_opts.realtime.used = 1;
1771 | UPPERLIMIT bandwidth {
1772 if (hfsc_opts.upperlimit.used) {
1773 yyerror("upperlimit already specified");
1776 hfsc_opts.upperlimit.m2 = $2;
1777 hfsc_opts.upperlimit.used = 1;
1779 | UPPERLIMIT '(' bandwidth comma NUMBER comma bandwidth ')'
1781 if ($5 < 0 || $5 > INT_MAX) {
1782 yyerror("timing in curve out of range");
1785 if (hfsc_opts.upperlimit.used) {
1786 yyerror("upperlimit already specified");
1789 hfsc_opts.upperlimit.m1 = $3;
1790 hfsc_opts.upperlimit.d = $5;
1791 hfsc_opts.upperlimit.m2 = $7;
1792 hfsc_opts.upperlimit.used = 1;
1795 if (!strcmp($1, "default"))
1796 hfsc_opts.flags |= HFCF_DEFAULTCLASS;
1797 else if (!strcmp($1, "red"))
1798 hfsc_opts.flags |= HFCF_RED;
1799 else if (!strcmp($1, "ecn"))
1800 hfsc_opts.flags |= HFCF_RED|HFCF_ECN;
1801 else if (!strcmp($1, "rio"))
1802 hfsc_opts.flags |= HFCF_RIO;
1804 yyerror("unknown hfsc flag \"%s\"", $1);
1814 sizeof(struct node_fairq_opts));
1821 fairqopts_list : fairqopts_item
1822 | fairqopts_list comma fairqopts_item
1825 fairqopts_item : LINKSHARE bandwidth {
1826 if (fairq_opts.linkshare.used) {
1827 yyerror("linkshare already specified");
1830 fairq_opts.linkshare.m2 = $2;
1831 fairq_opts.linkshare.used = 1;
1833 | LINKSHARE '(' bandwidth number bandwidth ')' {
1834 if (fairq_opts.linkshare.used) {
1835 yyerror("linkshare already specified");
1838 fairq_opts.linkshare.m1 = $3;
1839 fairq_opts.linkshare.d = $4;
1840 fairq_opts.linkshare.m2 = $5;
1841 fairq_opts.linkshare.used = 1;
1844 fairq_opts.hogs_bw = $2;
1847 fairq_opts.nbuckets = $2;
1850 if (!strcmp($1, "default"))
1851 fairq_opts.flags |= FARF_DEFAULTCLASS;
1852 else if (!strcmp($1, "red"))
1853 fairq_opts.flags |= FARF_RED;
1854 else if (!strcmp($1, "ecn"))
1855 fairq_opts.flags |= FARF_RED|FARF_ECN;
1856 else if (!strcmp($1, "rio"))
1857 fairq_opts.flags |= FARF_RIO;
1859 yyerror("unknown fairq flag \"%s\"", $1);
1867 qassign : /* empty */ { $$ = NULL; }
1868 | qassign_item { $$ = $1; }
1869 | '{' optnl qassign_list '}' { $$ = $3; }
1872 qassign_list : qassign_item optnl { $$ = $1; }
1873 | qassign_list comma qassign_item optnl {
1874 $1->tail->next = $3;
1880 qassign_item : STRING {
1881 $$ = calloc(1, sizeof(struct node_queue));
1883 err(1, "qassign_item: calloc");
1884 if (strlcpy($$->queue, $1, sizeof($$->queue)) >=
1885 sizeof($$->queue)) {
1886 yyerror("queue name '%s' too long (max "
1887 "%zd chars)", $1, sizeof($$->queue)-1);
1898 pfrule : action dir logquick interface route af proto fromto
1902 struct node_state_opt *o;
1903 struct node_proto *proto;
1909 if (check_rulestate(PFCTL_STATE_FILTER))
1912 memset(&r, 0, sizeof(r));
1916 case PFRULE_RETURNRST:
1917 r.rule_flag |= PFRULE_RETURNRST;
1918 r.return_ttl = $1.w;
1920 case PFRULE_RETURNICMP:
1921 r.rule_flag |= PFRULE_RETURNICMP;
1922 r.return_icmp = $1.w;
1923 r.return_icmp6 = $1.w2;
1926 r.rule_flag |= PFRULE_RETURN;
1927 r.return_icmp = $1.w;
1928 r.return_icmp6 = $1.w2;
1936 r.rtableid = $9.rtableid;
1940 if (strlcpy(r.tagname, $9.tag,
1941 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1942 yyerror("tag too long, max %u chars",
1943 PF_TAG_NAME_SIZE - 1);
1947 if (strlcpy(r.match_tagname, $9.match_tag,
1948 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1949 yyerror("tag too long, max %u chars",
1950 PF_TAG_NAME_SIZE - 1);
1953 r.match_tag_not = $9.match_tag_not;
1954 if (rule_label(&r, $9.label))
1957 r.flags = $9.flags.b1;
1958 r.flagset = $9.flags.b2;
1959 if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
1960 yyerror("flags always false");
1963 if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
1964 for (proto = $7; proto != NULL &&
1965 proto->proto != IPPROTO_TCP;
1966 proto = proto->next)
1968 if (proto == NULL && $7 != NULL) {
1969 if ($9.flags.b1 || $9.flags.b2)
1971 "flags only apply to tcp");
1974 "OS fingerprinting only "
1979 if (($9.flags.b1 & parse_flags("S")) == 0 &&
1981 yyerror("OS fingerprinting requires "
1982 "the SYN TCP flag (flags S/SA)");
1990 if (filter_opts.marker & FOM_KEEP) {
1991 r.keep_state = $9.keep.action;
1992 o = $9.keep.options;
1994 } else if (r.action == PF_PASS) {
1995 r.keep_state = default_keeppolicy_action;
1996 o = default_keeppolicy_options;
2003 struct node_state_opt *p = o;
2006 case PF_STATE_OPT_MAX:
2008 yyerror("state option 'max' "
2009 "multiple definitions");
2012 r.max_states = o->data.max_states;
2014 case PF_STATE_OPT_NOSYNC:
2015 if (r.rule_flag & PFRULE_NOSYNC) {
2016 yyerror("state option 'sync' "
2017 "multiple definitions");
2020 r.rule_flag |= PFRULE_NOSYNC;
2022 case PF_STATE_OPT_SRCTRACK:
2024 yyerror("state option "
2026 "multiple definitions");
2029 srctrack = o->data.src_track;
2030 r.rule_flag |= PFRULE_SRCTRACK;
2032 case PF_STATE_OPT_MAX_SRC_STATES:
2033 if (r.max_src_states) {
2034 yyerror("state option "
2036 "multiple definitions");
2039 if (o->data.max_src_states == 0) {
2040 yyerror("'max-src-states' must "
2045 o->data.max_src_states;
2046 r.rule_flag |= PFRULE_SRCTRACK;
2048 case PF_STATE_OPT_OVERLOAD:
2049 if (r.overload_tblname[0]) {
2050 yyerror("multiple 'overload' "
2051 "table definitions");
2054 if (strlcpy(r.overload_tblname,
2055 o->data.overload.tblname,
2056 PF_TABLE_NAME_SIZE) >=
2057 PF_TABLE_NAME_SIZE) {
2058 yyerror("state option: "
2062 r.flush = o->data.overload.flush;
2064 case PF_STATE_OPT_MAX_SRC_CONN:
2065 if (r.max_src_conn) {
2066 yyerror("state option "
2068 "multiple definitions");
2071 if (o->data.max_src_conn == 0) {
2072 yyerror("'max-src-conn' "
2077 o->data.max_src_conn;
2078 r.rule_flag |= PFRULE_SRCTRACK |
2079 PFRULE_RULESRCTRACK;
2081 case PF_STATE_OPT_MAX_SRC_CONN_RATE:
2082 if (r.max_src_conn_rate.limit) {
2083 yyerror("state option "
2084 "'max-src-conn-rate' "
2085 "multiple definitions");
2088 if (!o->data.max_src_conn_rate.limit ||
2089 !o->data.max_src_conn_rate.seconds) {
2090 yyerror("'max-src-conn-rate' "
2091 "values must be > 0");
2094 if (o->data.max_src_conn_rate.limit >
2096 yyerror("'max-src-conn-rate' "
2097 "maximum rate must be < %u",
2101 r.max_src_conn_rate.limit =
2102 o->data.max_src_conn_rate.limit;
2103 r.max_src_conn_rate.seconds =
2104 o->data.max_src_conn_rate.seconds;
2105 r.rule_flag |= PFRULE_SRCTRACK |
2106 PFRULE_RULESRCTRACK;
2108 case PF_STATE_OPT_MAX_SRC_NODES:
2109 if (r.max_src_nodes) {
2110 yyerror("state option "
2112 "multiple definitions");
2115 if (o->data.max_src_nodes == 0) {
2116 yyerror("'max-src-nodes' must "
2121 o->data.max_src_nodes;
2122 r.rule_flag |= PFRULE_SRCTRACK |
2123 PFRULE_RULESRCTRACK;
2125 case PF_STATE_OPT_STATELOCK:
2127 yyerror("state locking option: "
2128 "multiple definitions");
2132 r.rule_flag |= o->data.statelock;
2134 case PF_STATE_OPT_SLOPPY:
2135 if (r.rule_flag & PFRULE_STATESLOPPY) {
2136 yyerror("state sloppy option: "
2137 "multiple definitions");
2140 r.rule_flag |= PFRULE_STATESLOPPY;
2142 case PF_STATE_OPT_TIMEOUT:
2143 if (o->data.timeout.number ==
2144 PFTM_ADAPTIVE_START ||
2145 o->data.timeout.number ==
2148 if (r.timeout[o->data.timeout.number]) {
2149 yyerror("state timeout %s "
2150 "multiple definitions",
2151 pf_timeouts[o->data.
2152 timeout.number].name);
2155 r.timeout[o->data.timeout.number] =
2156 o->data.timeout.seconds;
2158 case PF_STATE_OPT_PICKUPS:
2159 r.pickup_mode = o->data.pickup_mode;
2168 /* 'flags S/SA' by default on stateful rules */
2169 if (!r.action && !r.flags && !r.flagset &&
2170 !$9.fragment && !($9.marker & FOM_FLAGS) &&
2172 r.flags = parse_flags("S");
2173 r.flagset = parse_flags("SA");
2176 if (!adaptive && r.max_states) {
2177 r.timeout[PFTM_ADAPTIVE_START] =
2178 (r.max_states / 10) * 6;
2179 r.timeout[PFTM_ADAPTIVE_END] =
2180 (r.max_states / 10) * 12;
2182 if (r.rule_flag & PFRULE_SRCTRACK) {
2183 if (srctrack == PF_SRCTRACK_GLOBAL &&
2185 yyerror("'max-src-nodes' is "
2186 "incompatible with "
2187 "'source-track global'");
2190 if (srctrack == PF_SRCTRACK_GLOBAL &&
2192 yyerror("'max-src-conn' is "
2193 "incompatible with "
2194 "'source-track global'");
2197 if (srctrack == PF_SRCTRACK_GLOBAL &&
2198 r.max_src_conn_rate.seconds) {
2199 yyerror("'max-src-conn-rate' is "
2200 "incompatible with "
2201 "'source-track global'");
2204 if (r.timeout[PFTM_SRC_NODE] <
2205 r.max_src_conn_rate.seconds)
2206 r.timeout[PFTM_SRC_NODE] =
2207 r.max_src_conn_rate.seconds;
2208 r.rule_flag |= PFRULE_SRCTRACK;
2209 if (srctrack == PF_SRCTRACK_RULE)
2210 r.rule_flag |= PFRULE_RULESRCTRACK;
2212 if (r.keep_state && !statelock)
2213 r.rule_flag |= default_statelock;
2216 r.rule_flag |= PFRULE_FRAGMENT;
2217 r.allow_opts = $9.allowopts;
2219 decide_address_family($8.src.host, &r.af);
2220 decide_address_family($8.dst.host, &r.af);
2224 yyerror("direction must be explicit "
2225 "with rules that specify routing");
2229 r.rpool.opts = $5.pool_opts;
2231 memcpy(&r.rpool.key, $5.key,
2232 sizeof(struct pf_poolhashkey));
2234 if (r.rt && r.rt != PF_FASTROUTE) {
2235 decide_address_family($5.host, &r.af);
2236 remove_invalid_hosts(&$5.host, &r.af);
2237 if ($5.host == NULL) {
2238 yyerror("no routing address with "
2239 "matching address family found.");
2242 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
2243 PF_POOL_NONE && ($5.host->next != NULL ||
2244 $5.host->addr.type == PF_ADDR_TABLE ||
2245 DYNIF_MULTIADDR($5.host->addr)))
2246 r.rpool.opts |= PF_POOL_ROUNDROBIN;
2247 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2248 PF_POOL_ROUNDROBIN &&
2249 disallow_table($5.host, "tables are only "
2250 "supported in round-robin routing pools"))
2252 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2253 PF_POOL_ROUNDROBIN &&
2254 disallow_alias($5.host, "interface (%s) "
2255 "is only supported in round-robin "
2258 if ($5.host->next != NULL) {
2259 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2260 PF_POOL_ROUNDROBIN) {
2261 yyerror("r.rpool.opts must "
2262 "be PF_POOL_ROUNDROBIN");
2267 if ($9.queues.qname != NULL) {
2268 if (strlcpy(r.qname, $9.queues.qname,
2269 sizeof(r.qname)) >= sizeof(r.qname)) {
2270 yyerror("rule qname too long (max "
2271 "%zd chars)", sizeof(r.qname)-1);
2274 free($9.queues.qname);
2276 if ($9.queues.pqname != NULL) {
2277 if (strlcpy(r.pqname, $9.queues.pqname,
2278 sizeof(r.pqname)) >= sizeof(r.pqname)) {
2279 yyerror("rule pqname too long (max "
2280 "%zd chars)", sizeof(r.pqname)-1);
2283 free($9.queues.pqname);
2285 if ((r.divert.port = $9.divert.port)) {
2286 if (r.direction == PF_OUT) {
2287 if ($9.divert.addr) {
2288 yyerror("address specified "
2289 "for outgoing divert");
2292 bzero(&r.divert.addr,
2293 sizeof(r.divert.addr));
2295 if (!$9.divert.addr) {
2296 yyerror("no address specified "
2297 "for incoming divert");
2300 if ($9.divert.addr->af != r.af) {
2301 yyerror("address family "
2302 "mismatch for divert");
2306 $9.divert.addr->addr.v.a.addr;
2310 expand_rule(&r, $4, $5.host, $7, $8.src_os,
2311 $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
2312 $9.uid, $9.gid, $9.icmpspec, "");
2317 bzero(&filter_opts, sizeof filter_opts);
2318 filter_opts.rtableid = -1;
2321 { $$ = filter_opts; }
2323 bzero(&filter_opts, sizeof filter_opts);
2324 filter_opts.rtableid = -1;
2329 filter_opts_l : filter_opts_l filter_opt
2333 filter_opt : USER uids {
2334 if (filter_opts.uid)
2335 $2->tail->next = filter_opts.uid;
2336 filter_opts.uid = $2;
2339 if (filter_opts.gid)
2340 $2->tail->next = filter_opts.gid;
2341 filter_opts.gid = $2;
2344 if (filter_opts.marker & FOM_FLAGS) {
2345 yyerror("flags cannot be redefined");
2348 filter_opts.marker |= FOM_FLAGS;
2349 filter_opts.flags.b1 |= $1.b1;
2350 filter_opts.flags.b2 |= $1.b2;
2351 filter_opts.flags.w |= $1.w;
2352 filter_opts.flags.w2 |= $1.w2;
2355 if (filter_opts.marker & FOM_ICMP) {
2356 yyerror("icmp-type cannot be redefined");
2359 filter_opts.marker |= FOM_ICMP;
2360 filter_opts.icmpspec = $1;
2363 if (filter_opts.marker & FOM_TOS) {
2364 yyerror("tos cannot be redefined");
2367 filter_opts.marker |= FOM_TOS;
2368 filter_opts.tos = $2;
2371 if (filter_opts.marker & FOM_KEEP) {
2372 yyerror("modulate or keep cannot be redefined");
2375 filter_opts.marker |= FOM_KEEP;
2376 filter_opts.keep.action = $1.action;
2377 filter_opts.keep.options = $1.options;
2380 filter_opts.fragment = 1;
2383 filter_opts.allowopts = 1;
2386 if (filter_opts.label) {
2387 yyerror("label cannot be redefined");
2390 filter_opts.label = $1;
2393 if (filter_opts.queues.qname) {
2394 yyerror("queue cannot be redefined");
2397 filter_opts.queues = $1;
2400 filter_opts.tag = $2;
2402 | not TAGGED string {
2403 filter_opts.match_tag = $3;
2404 filter_opts.match_tag_not = $1;
2406 | PROBABILITY probability {
2409 p = floor($2 * UINT_MAX + 0.5);
2410 if (p < 0.0 || p > UINT_MAX) {
2411 yyerror("invalid probability: %lf", p);
2414 filter_opts.prob = (u_int32_t)p;
2415 if (filter_opts.prob == 0)
2416 filter_opts.prob = 1;
2419 filter_opts.rtableid = $2;
2421 | DIVERTTO STRING PORT portplain {
2422 if ((filter_opts.divert.addr = host($2)) == NULL) {
2423 yyerror("could not parse divert address: %s",
2429 filter_opts.divert.port = $4.a;
2430 if (!filter_opts.divert.port) {
2431 yyerror("invalid divert port: %u", ntohs($4.a));
2436 filter_opts.divert.port = 1; /* some random value */
2440 probability : STRING {
2442 double p = strtod($1, &e);
2449 yyerror("invalid probability: %s", $1);
2462 action : PASS { $$.b1 = PF_PASS; $$.b2 = $$.w = 0; }
2463 | BLOCK blockspec { $$ = $2; $$.b1 = PF_DROP; }
2466 blockspec : /* empty */ {
2467 $$.b2 = blockpolicy;
2468 $$.w = returnicmpdefault;
2469 $$.w2 = returnicmp6default;
2472 $$.b2 = PFRULE_DROP;
2477 $$.b2 = PFRULE_RETURNRST;
2481 | RETURNRST '(' TTL NUMBER ')' {
2482 if ($4 < 0 || $4 > 255) {
2483 yyerror("illegal ttl value %" PRId64, $4);
2486 $$.b2 = PFRULE_RETURNRST;
2491 $$.b2 = PFRULE_RETURNICMP;
2492 $$.w = returnicmpdefault;
2493 $$.w2 = returnicmp6default;
2496 $$.b2 = PFRULE_RETURNICMP;
2497 $$.w = returnicmpdefault;
2498 $$.w2 = returnicmp6default;
2500 | RETURNICMP '(' reticmpspec ')' {
2501 $$.b2 = PFRULE_RETURNICMP;
2503 $$.w2 = returnicmpdefault;
2505 | RETURNICMP6 '(' reticmp6spec ')' {
2506 $$.b2 = PFRULE_RETURNICMP;
2507 $$.w = returnicmpdefault;
2510 | RETURNICMP '(' reticmpspec comma reticmp6spec ')' {
2511 $$.b2 = PFRULE_RETURNICMP;
2516 $$.b2 = PFRULE_RETURN;
2517 $$.w = returnicmpdefault;
2518 $$.w2 = returnicmp6default;
2522 reticmpspec : STRING {
2523 if (!($$ = parseicmpspec($1, AF_INET))) {
2532 if ($1 < 0 || $1 > 255) {
2533 yyerror("invalid icmp code %" PRId64, $1);
2536 icmptype = returnicmpdefault >> 8;
2537 $$ = (icmptype << 8 | $1);
2541 reticmp6spec : STRING {
2542 if (!($$ = parseicmpspec($1, AF_INET6))) {
2551 if ($1 < 0 || $1 > 255) {
2552 yyerror("invalid icmp code %" PRId64, $1);
2555 icmptype = returnicmp6default >> 8;
2556 $$ = (icmptype << 8 | $1);
2560 dir : /* empty */ { $$ = PF_INOUT; }
2561 | IN { $$ = PF_IN; }
2562 | OUT { $$ = PF_OUT; }
2565 quick : /* empty */ { $$.quick = 0; }
2566 | QUICK { $$.quick = 1; }
2569 logquick : /* empty */ { $$.log = 0; $$.quick = 0; $$.logif = 0; }
2570 | log { $$ = $1; $$.quick = 0; }
2571 | QUICK { $$.quick = 1; $$.log = 0; $$.logif = 0; }
2572 | log QUICK { $$ = $1; $$.quick = 1; }
2573 | QUICK log { $$ = $2; $$.quick = 1; }
2576 log : LOG { $$.log = PF_LOG; $$.logif = 0; }
2577 | LOG '(' logopts ')' {
2578 $$.log = PF_LOG | $3.log;
2579 $$.logif = $3.logif;
2583 logopts : logopt { $$ = $1; }
2584 | logopts comma logopt {
2585 $$.log = $1.log | $3.log;
2586 $$.logif = $3.logif;
2588 $$.logif = $1.logif;
2592 logopt : ALL { $$.log = PF_LOG_ALL; $$.logif = 0; }
2593 | USER { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
2594 | GROUP { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
2600 if (strncmp($2, "pflog", 5)) {
2601 yyerror("%s: should be a pflog interface", $2);
2605 i = strtonum($2 + 5, 0, 255, &errstr);
2607 yyerror("%s: %s", $2, errstr);
2616 interface : /* empty */ { $$ = NULL; }
2617 | ON if_item_not { $$ = $2; }
2618 | ON '{' optnl if_list '}' { $$ = $4; }
2621 if_list : if_item_not optnl { $$ = $1; }
2622 | if_list comma if_item_not optnl {
2623 $1->tail->next = $3;
2629 if_item_not : not if_item { $$ = $2; $$->not = $1; }
2633 struct node_host *n;
2635 $$ = calloc(1, sizeof(struct node_if));
2637 err(1, "if_item: calloc");
2638 if (strlcpy($$->ifname, $1, sizeof($$->ifname)) >=
2639 sizeof($$->ifname)) {
2642 yyerror("interface name too long");
2646 if ((n = ifa_exists($1)) != NULL)
2647 $$->ifa_flags = n->ifa_flags;
2656 af : /* empty */ { $$ = 0; }
2657 | INET { $$ = AF_INET; }
2658 | INET6 { $$ = AF_INET6; }
2661 proto : /* empty */ { $$ = NULL; }
2662 | PROTO proto_item { $$ = $2; }
2663 | PROTO '{' optnl proto_list '}' { $$ = $4; }
2666 proto_list : proto_item optnl { $$ = $1; }
2667 | proto_list comma proto_item optnl {
2668 $1->tail->next = $3;
2674 proto_item : protoval {
2679 yyerror("proto 0 cannot be used");
2682 $$ = calloc(1, sizeof(struct node_proto));
2684 err(1, "proto_item: calloc");
2694 p = getprotobyname($1);
2696 yyerror("unknown protocol %s", $1);
2704 if ($1 < 0 || $1 > 255) {
2705 yyerror("protocol outside range");
2725 os : /* empty */ { $$ = NULL; }
2726 | OS xos { $$ = $2; }
2727 | OS '{' optnl os_list '}' { $$ = $4; }
2731 $$ = calloc(1, sizeof(struct node_os));
2733 err(1, "os: calloc");
2739 os_list : xos optnl { $$ = $1; }
2740 | os_list comma xos optnl {
2741 $1->tail->next = $3;
2747 from : /* empty */ {
2761 if (disallow_urpf_failed($2.host, "\"urpf-failed\" is "
2762 "not permitted in a destination address"))
2768 ipportspec : ipspec {
2772 | ipspec PORT portspec {
2786 ipspec : ANY { $$ = NULL; }
2787 | xhost { $$ = $1; }
2788 | '{' optnl host_list '}' { $$ = $3; }
2791 toipspec : TO ipspec { $$ = $2; }
2792 | /* empty */ { $$ = NULL; }
2795 host_list : ipspec optnl { $$ = $1; }
2796 | host_list comma ipspec optnl {
2799 else if ($1 == NULL)
2802 $1->tail->next = $3;
2803 $1->tail = $3->tail;
2810 struct node_host *n;
2812 for (n = $2; n != NULL; n = n->next)
2817 $$ = calloc(1, sizeof(struct node_host));
2819 err(1, "xhost: calloc");
2820 $$->addr.type = PF_ADDR_NOROUTE;
2826 $$ = calloc(1, sizeof(struct node_host));
2828 err(1, "xhost: calloc");
2829 $$->addr.type = PF_ADDR_URPFFAILED;
2837 if (($$ = host($1)) == NULL) {
2838 /* error. "any" is handled elsewhere */
2840 yyerror("could not parse host specification");
2846 | STRING '-' STRING {
2847 struct node_host *b, *e;
2849 if ((b = host($1)) == NULL || (e = host($3)) == NULL) {
2852 yyerror("could not parse host specification");
2855 if (b->af != e->af ||
2856 b->addr.type != PF_ADDR_ADDRMASK ||
2857 e->addr.type != PF_ADDR_ADDRMASK ||
2858 unmask(&b->addr.v.a.mask, b->af) !=
2859 (b->af == AF_INET ? 32 : 128) ||
2860 unmask(&e->addr.v.a.mask, e->af) !=
2861 (e->af == AF_INET ? 32 : 128) ||
2862 b->next != NULL || b->not ||
2863 e->next != NULL || e->not) {
2868 yyerror("invalid address range");
2871 memcpy(&b->addr.v.a.mask, &e->addr.v.a.addr,
2872 sizeof(b->addr.v.a.mask));
2873 b->addr.type = PF_ADDR_RANGE;
2879 | STRING '/' NUMBER {
2882 if (asprintf(&buf, "%s/%" PRId64, $1, $3) == -1)
2883 err(1, "host: asprintf");
2885 if (($$ = host(buf)) == NULL) {
2886 /* error. "any" is handled elsewhere */
2888 yyerror("could not parse host specification");
2893 | NUMBER '/' NUMBER {
2896 /* ie. for 10/8 parsing */
2897 if (asprintf(&buf, "%" PRId64 "/%" PRId64, $1, $3) == -1)
2898 err(1, "host: asprintf");
2899 if (($$ = host(buf)) == NULL) {
2900 /* error. "any" is handled elsewhere */
2902 yyerror("could not parse host specification");
2908 | dynaddr '/' NUMBER {
2909 struct node_host *n;
2911 if ($3 < 0 || $3 > 128) {
2912 yyerror("bit number too big");
2916 for (n = $1; n != NULL; n = n->next)
2920 if (strlen($2) >= PF_TABLE_NAME_SIZE) {
2921 yyerror("table name '%s' too long", $2);
2925 $$ = calloc(1, sizeof(struct node_host));
2927 err(1, "host: calloc");
2928 $$->addr.type = PF_ADDR_TABLE;
2929 if (strlcpy($$->addr.v.tblname, $2,
2930 sizeof($$->addr.v.tblname)) >=
2931 sizeof($$->addr.v.tblname))
2932 errx(1, "host: strlcpy");
2938 $$ = calloc(1, sizeof(struct node_host));
2941 err(1, "host: calloc");
2943 $$->addr.type = PF_ADDR_RTLABEL;
2944 if (strlcpy($$->addr.v.rtlabelname, $2,
2945 sizeof($$->addr.v.rtlabelname)) >=
2946 sizeof($$->addr.v.rtlabelname)) {
2947 yyerror("route label too long, max %zd chars",
2948 sizeof($$->addr.v.rtlabelname) - 1);
2963 if (atoul($1, &ulval) == -1) {
2964 yyerror("%s is not a number", $1);
2973 dynaddr : '(' STRING ')' {
2978 if (!isalpha(op[0])) {
2979 yyerror("invalid interface name '%s'", op);
2983 while ((p = strrchr($2, ':')) != NULL) {
2984 if (!strcmp(p+1, "network"))
2985 flags |= PFI_AFLAG_NETWORK;
2986 else if (!strcmp(p+1, "broadcast"))
2987 flags |= PFI_AFLAG_BROADCAST;
2988 else if (!strcmp(p+1, "peer"))
2989 flags |= PFI_AFLAG_PEER;
2990 else if (!strcmp(p+1, "0"))
2991 flags |= PFI_AFLAG_NOALIAS;
2993 yyerror("interface %s has bad modifier",
3000 if (flags & (flags - 1) & PFI_AFLAG_MODEMASK) {
3002 yyerror("illegal combination of "
3003 "interface modifiers");
3006 $$ = calloc(1, sizeof(struct node_host));
3008 err(1, "address: calloc");
3010 set_ipmask($$, 128);
3011 $$->addr.type = PF_ADDR_DYNIFTL;
3012 $$->addr.iflags = flags;
3013 if (strlcpy($$->addr.v.ifname, $2,
3014 sizeof($$->addr.v.ifname)) >=
3015 sizeof($$->addr.v.ifname)) {
3018 yyerror("interface name too long");
3027 portspec : port_item { $$ = $1; }
3028 | '{' optnl port_list '}' { $$ = $3; }
3031 port_list : port_item optnl { $$ = $1; }
3032 | port_list comma port_item optnl {
3033 $1->tail->next = $3;
3039 port_item : portrange {
3040 $$ = calloc(1, sizeof(struct node_port));
3042 err(1, "port_item: calloc");
3052 | unaryop portrange {
3054 yyerror("':' cannot be used with an other "
3058 $$ = calloc(1, sizeof(struct node_port));
3060 err(1, "port_item: calloc");
3067 | portrange PORTBINARY portrange {
3069 yyerror("':' cannot be used with an other "
3073 $$ = calloc(1, sizeof(struct node_port));
3075 err(1, "port_item: calloc");
3084 portplain : numberstring {
3085 if (parseport($1, &$$, 0) == -1) {
3093 portrange : numberstring {
3094 if (parseport($1, &$$, PPORT_RANGE) == -1) {
3102 uids : uid_item { $$ = $1; }
3103 | '{' optnl uid_list '}' { $$ = $3; }
3106 uid_list : uid_item optnl { $$ = $1; }
3107 | uid_list comma uid_item optnl {
3108 $1->tail->next = $3;
3115 $$ = calloc(1, sizeof(struct node_uid));
3117 err(1, "uid_item: calloc");
3125 if ($2 == UID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
3126 yyerror("user unknown requires operator = or "
3130 $$ = calloc(1, sizeof(struct node_uid));
3132 err(1, "uid_item: calloc");
3139 | uid PORTBINARY uid {
3140 if ($1 == UID_MAX || $3 == UID_MAX) {
3141 yyerror("user unknown requires operator = or "
3145 $$ = calloc(1, sizeof(struct node_uid));
3147 err(1, "uid_item: calloc");
3157 if (!strcmp($1, "unknown"))
3162 if ((pw = getpwnam($1)) == NULL) {
3163 yyerror("unknown user %s", $1);
3172 if ($1 < 0 || $1 >= UID_MAX) {
3173 yyerror("illegal uid value %" PRId64, $1);
3180 gids : gid_item { $$ = $1; }
3181 | '{' optnl gid_list '}' { $$ = $3; }
3184 gid_list : gid_item optnl { $$ = $1; }
3185 | gid_list comma gid_item optnl {
3186 $1->tail->next = $3;
3193 $$ = calloc(1, sizeof(struct node_gid));
3195 err(1, "gid_item: calloc");
3203 if ($2 == GID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
3204 yyerror("group unknown requires operator = or "
3208 $$ = calloc(1, sizeof(struct node_gid));
3210 err(1, "gid_item: calloc");
3217 | gid PORTBINARY gid {
3218 if ($1 == GID_MAX || $3 == GID_MAX) {
3219 yyerror("group unknown requires operator = or "
3223 $$ = calloc(1, sizeof(struct node_gid));
3225 err(1, "gid_item: calloc");
3235 if (!strcmp($1, "unknown"))
3240 if ((grp = getgrnam($1)) == NULL) {
3241 yyerror("unknown group %s", $1);
3250 if ($1 < 0 || $1 >= GID_MAX) {
3251 yyerror("illegal gid value %" PRId64, $1);
3261 if ((f = parse_flags($1)) < 0) {
3262 yyerror("bad flags %s", $1);
3271 flags : FLAGS flag '/' flag { $$.b1 = $2.b1; $$.b2 = $4.b1; }
3272 | FLAGS '/' flag { $$.b1 = 0; $$.b2 = $3.b1; }
3273 | FLAGS ANY { $$.b1 = 0; $$.b2 = 0; }
3276 icmpspec : ICMPTYPE icmp_item { $$ = $2; }
3277 | ICMPTYPE '{' optnl icmp_list '}' { $$ = $4; }
3278 | ICMP6TYPE icmp6_item { $$ = $2; }
3279 | ICMP6TYPE '{' optnl icmp6_list '}' { $$ = $4; }
3282 icmp_list : icmp_item optnl { $$ = $1; }
3283 | icmp_list comma icmp_item optnl {
3284 $1->tail->next = $3;
3290 icmp6_list : icmp6_item optnl { $$ = $1; }
3291 | icmp6_list comma icmp6_item optnl {
3292 $1->tail->next = $3;
3298 icmp_item : icmptype {
3299 $$ = calloc(1, sizeof(struct node_icmp));
3301 err(1, "icmp_item: calloc");
3304 $$->proto = IPPROTO_ICMP;
3308 | icmptype CODE STRING {
3309 const struct icmpcodeent *p;
3311 if ((p = geticmpcodebyname($1-1, $3, AF_INET)) == NULL) {
3312 yyerror("unknown icmp-code %s", $3);
3318 $$ = calloc(1, sizeof(struct node_icmp));
3320 err(1, "icmp_item: calloc");
3322 $$->code = p->code + 1;
3323 $$->proto = IPPROTO_ICMP;
3327 | icmptype CODE NUMBER {
3328 if ($3 < 0 || $3 > 255) {
3329 yyerror("illegal icmp-code %" PRId64, $3);
3332 $$ = calloc(1, sizeof(struct node_icmp));
3334 err(1, "icmp_item: calloc");
3337 $$->proto = IPPROTO_ICMP;
3343 icmp6_item : icmp6type {
3344 $$ = calloc(1, sizeof(struct node_icmp));
3346 err(1, "icmp_item: calloc");
3349 $$->proto = IPPROTO_ICMPV6;
3353 | icmp6type CODE STRING {
3354 const struct icmpcodeent *p;
3356 if ((p = geticmpcodebyname($1-1, $3, AF_INET6)) == NULL) {
3357 yyerror("unknown icmp6-code %s", $3);
3363 $$ = calloc(1, sizeof(struct node_icmp));
3365 err(1, "icmp_item: calloc");
3367 $$->code = p->code + 1;
3368 $$->proto = IPPROTO_ICMPV6;
3372 | icmp6type CODE NUMBER {
3373 if ($3 < 0 || $3 > 255) {
3374 yyerror("illegal icmp-code %" PRId64, $3);
3377 $$ = calloc(1, sizeof(struct node_icmp));
3379 err(1, "icmp_item: calloc");
3382 $$->proto = IPPROTO_ICMPV6;
3389 const struct icmptypeent *p;
3391 if ((p = geticmptypebyname($1, AF_INET)) == NULL) {
3392 yyerror("unknown icmp-type %s", $1);
3400 if ($1 < 0 || $1 > 255) {
3401 yyerror("illegal icmp-type %" PRId64, $1);
3408 icmp6type : STRING {
3409 const struct icmptypeent *p;
3411 if ((p = geticmptypebyname($1, AF_INET6)) ==
3413 yyerror("unknown icmp6-type %s", $1);
3421 if ($1 < 0 || $1 > 255) {
3422 yyerror("illegal icmp6-type %" PRId64, $1);
3430 if (!strcmp($1, "lowdelay"))
3431 $$ = IPTOS_LOWDELAY;
3432 else if (!strcmp($1, "throughput"))
3433 $$ = IPTOS_THROUGHPUT;
3434 else if (!strcmp($1, "reliability"))
3435 $$ = IPTOS_RELIABILITY;
3436 else if ($1[0] == '0' && $1[1] == 'x')
3437 $$ = strtoul($1, NULL, 16);
3439 $$ = 0; /* flag bad argument */
3440 if (!$$ || $$ > 255) {
3441 yyerror("illegal tos value %s", $1);
3449 if (!$$ || $$ > 255) {
3450 yyerror("illegal tos value %" PRId64, $1);
3456 sourcetrack : SOURCETRACK { $$ = PF_SRCTRACK; }
3457 | SOURCETRACK GLOBAL { $$ = PF_SRCTRACK_GLOBAL; }
3458 | SOURCETRACK RULE { $$ = PF_SRCTRACK_RULE; }
3461 statelock : IFBOUND {
3462 $$ = PFRULE_IFBOUND;
3473 | KEEP STATE state_opt_spec {
3474 $$.action = PF_STATE_NORMAL;
3477 | MODULATE STATE state_opt_spec {
3478 $$.action = PF_STATE_MODULATE;
3481 | SYNPROXY STATE state_opt_spec {
3482 $$.action = PF_STATE_SYNPROXY;
3487 flush : /* empty */ { $$ = 0; }
3488 | FLUSH { $$ = PF_FLUSH; }
3490 $$ = PF_FLUSH | PF_FLUSH_GLOBAL;
3494 state_opt_spec : '(' state_opt_list ')' { $$ = $2; }
3495 | /* empty */ { $$ = NULL; }
3498 state_opt_list : state_opt_item { $$ = $1; }
3499 | state_opt_list comma state_opt_item {
3500 $1->tail->next = $3;
3506 state_opt_item : MAXIMUM NUMBER {
3507 if ($2 < 0 || $2 > UINT_MAX) {
3508 yyerror("only positive values permitted");
3511 $$ = calloc(1, sizeof(struct node_state_opt));
3513 err(1, "state_opt_item: calloc");
3514 $$->type = PF_STATE_OPT_MAX;
3515 $$->data.max_states = $2;
3520 $$ = calloc(1, sizeof(struct node_state_opt));
3522 err(1, "state_opt_item: calloc");
3523 $$->type = PF_STATE_OPT_NOSYNC;
3527 | MAXSRCSTATES NUMBER {
3528 if ($2 < 0 || $2 > UINT_MAX) {
3529 yyerror("only positive values permitted");
3532 $$ = calloc(1, sizeof(struct node_state_opt));
3534 err(1, "state_opt_item: calloc");
3535 $$->type = PF_STATE_OPT_MAX_SRC_STATES;
3536 $$->data.max_src_states = $2;
3540 | MAXSRCCONN NUMBER {
3541 if ($2 < 0 || $2 > UINT_MAX) {
3542 yyerror("only positive values permitted");
3545 $$ = calloc(1, sizeof(struct node_state_opt));
3547 err(1, "state_opt_item: calloc");
3548 $$->type = PF_STATE_OPT_MAX_SRC_CONN;
3549 $$->data.max_src_conn = $2;
3553 | MAXSRCCONNRATE NUMBER '/' NUMBER {
3554 if ($2 < 0 || $2 > UINT_MAX ||
3555 $4 < 0 || $4 > UINT_MAX) {
3556 yyerror("only positive values permitted");
3559 $$ = calloc(1, sizeof(struct node_state_opt));
3561 err(1, "state_opt_item: calloc");
3562 $$->type = PF_STATE_OPT_MAX_SRC_CONN_RATE;
3563 $$->data.max_src_conn_rate.limit = $2;
3564 $$->data.max_src_conn_rate.seconds = $4;
3568 | OVERLOAD '<' STRING '>' flush {
3569 if (strlen($3) >= PF_TABLE_NAME_SIZE) {
3570 yyerror("table name '%s' too long", $3);
3574 $$ = calloc(1, sizeof(struct node_state_opt));
3576 err(1, "state_opt_item: calloc");
3577 if (strlcpy($$->data.overload.tblname, $3,
3578 PF_TABLE_NAME_SIZE) >= PF_TABLE_NAME_SIZE)
3579 errx(1, "state_opt_item: strlcpy");
3581 $$->type = PF_STATE_OPT_OVERLOAD;
3582 $$->data.overload.flush = $5;
3586 | MAXSRCNODES NUMBER {
3587 if ($2 < 0 || $2 > UINT_MAX) {
3588 yyerror("only positive values permitted");
3591 $$ = calloc(1, sizeof(struct node_state_opt));
3593 err(1, "state_opt_item: calloc");
3594 $$->type = PF_STATE_OPT_MAX_SRC_NODES;
3595 $$->data.max_src_nodes = $2;
3600 $$ = calloc(1, sizeof(struct node_state_opt));
3602 err(1, "state_opt_item: calloc");
3603 $$->type = PF_STATE_OPT_PICKUPS;
3604 $$->data.pickup_mode = PF_PICKUPS_ENABLED;
3609 $$ = calloc(1, sizeof(struct node_state_opt));
3611 err(1, "state_opt_item: calloc");
3612 $$->type = PF_STATE_OPT_PICKUPS;
3613 $$->data.pickup_mode = PF_PICKUPS_DISABLED;
3618 $$ = calloc(1, sizeof(struct node_state_opt));
3620 err(1, "state_opt_item: calloc");
3621 $$->type = PF_STATE_OPT_PICKUPS;
3622 $$->data.pickup_mode = PF_PICKUPS_HASHONLY;
3627 $$ = calloc(1, sizeof(struct node_state_opt));
3629 err(1, "state_opt_item: calloc");
3630 $$->type = PF_STATE_OPT_SRCTRACK;
3631 $$->data.src_track = $1;
3636 $$ = calloc(1, sizeof(struct node_state_opt));
3638 err(1, "state_opt_item: calloc");
3639 $$->type = PF_STATE_OPT_STATELOCK;
3640 $$->data.statelock = $1;
3645 $$ = calloc(1, sizeof(struct node_state_opt));
3647 err(1, "state_opt_item: calloc");
3648 $$->type = PF_STATE_OPT_SLOPPY;
3655 if ($2 < 0 || $2 > UINT_MAX) {
3656 yyerror("only positive values permitted");
3659 for (i = 0; pf_timeouts[i].name &&
3660 strcmp(pf_timeouts[i].name, $1); ++i)
3662 if (!pf_timeouts[i].name) {
3663 yyerror("illegal timeout name %s", $1);
3667 if (strchr(pf_timeouts[i].name, '.') == NULL) {
3668 yyerror("illegal state timeout %s", $1);
3673 $$ = calloc(1, sizeof(struct node_state_opt));
3675 err(1, "state_opt_item: calloc");
3676 $$->type = PF_STATE_OPT_TIMEOUT;
3677 $$->data.timeout.number = pf_timeouts[i].timeout;
3678 $$->data.timeout.seconds = $2;
3684 label : LABEL STRING {
3689 qname : QUEUE STRING {
3693 | QUEUE '(' STRING ')' {
3697 | QUEUE '(' STRING comma STRING ')' {
3703 no : /* empty */ { $$ = 0; }
3707 portstar : numberstring {
3708 if (parseport($1, &$$, PPORT_RANGE|PPORT_STAR) == -1) {
3716 redirspec : host { $$ = $1; }
3717 | '{' optnl redir_host_list '}' { $$ = $3; }
3720 redir_host_list : host optnl { $$ = $1; }
3721 | redir_host_list comma host optnl {
3722 $1->tail->next = $3;
3723 $1->tail = $3->tail;
3728 redirpool : /* empty */ { $$ = NULL; }
3730 $$ = calloc(1, sizeof(struct redirection));
3732 err(1, "redirection: calloc");
3734 $$->rport.a = $$->rport.b = $$->rport.t = 0;
3736 | ARROW redirspec PORT portstar {
3737 $$ = calloc(1, sizeof(struct redirection));
3739 err(1, "redirection: calloc");
3745 hashkey : /* empty */
3747 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3749 err(1, "hashkey: calloc");
3750 $$->key32[0] = arc4random();
3751 $$->key32[1] = arc4random();
3752 $$->key32[2] = arc4random();
3753 $$->key32[3] = arc4random();
3757 if (!strncmp($1, "0x", 2)) {
3758 if (strlen($1) != 34) {
3760 yyerror("hex key must be 128 bits "
3761 "(32 hex digits) long");
3764 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3766 err(1, "hashkey: calloc");
3768 if (sscanf($1, "0x%8x%8x%8x%8x",
3769 &$$->key32[0], &$$->key32[1],
3770 &$$->key32[2], &$$->key32[3]) != 4) {
3773 yyerror("invalid hex key");
3779 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3781 err(1, "hashkey: calloc");
3783 MD5Update(&context, (unsigned char *)$1,
3785 MD5Final((unsigned char *)$$, &context);
3786 $$->key32[0] = htonl($$->key32[0]);
3787 $$->key32[1] = htonl($$->key32[1]);
3788 $$->key32[2] = htonl($$->key32[2]);
3789 $$->key32[3] = htonl($$->key32[3]);
3795 pool_opts : { bzero(&pool_opts, sizeof pool_opts); }
3799 bzero(&pool_opts, sizeof pool_opts);
3804 pool_opts_l : pool_opts_l pool_opt
3808 pool_opt : BITMASK {
3809 if (pool_opts.type) {
3810 yyerror("pool type cannot be redefined");
3813 pool_opts.type = PF_POOL_BITMASK;
3816 if (pool_opts.type) {
3817 yyerror("pool type cannot be redefined");
3820 pool_opts.type = PF_POOL_RANDOM;
3822 | SOURCEHASH hashkey {
3823 if (pool_opts.type) {
3824 yyerror("pool type cannot be redefined");
3827 pool_opts.type = PF_POOL_SRCHASH;
3831 if (pool_opts.type) {
3832 yyerror("pool type cannot be redefined");
3835 pool_opts.type = PF_POOL_ROUNDROBIN;
3838 if (pool_opts.staticport) {
3839 yyerror("static-port cannot be redefined");
3842 pool_opts.staticport = 1;
3845 if (filter_opts.marker & POM_STICKYADDRESS) {
3846 yyerror("sticky-address cannot be redefined");
3849 pool_opts.marker |= POM_STICKYADDRESS;
3850 pool_opts.opts |= PF_POOL_STICKYADDR;
3854 redirection : /* empty */ { $$ = NULL; }
3856 $$ = calloc(1, sizeof(struct redirection));
3858 err(1, "redirection: calloc");
3860 $$->rport.a = $$->rport.b = $$->rport.t = 0;
3862 | ARROW host PORT portstar {
3863 $$ = calloc(1, sizeof(struct redirection));
3865 err(1, "redirection: calloc");
3871 natpasslog : /* empty */ { $$.b1 = $$.b2 = 0; $$.w2 = 0; }
3872 | PASS { $$.b1 = 1; $$.b2 = 0; $$.w2 = 0; }
3873 | PASS log { $$.b1 = 1; $$.b2 = $2.log; $$.w2 = $2.logif; }
3874 | log { $$.b1 = 0; $$.b2 = $1.log; $$.w2 = $1.logif; }
3877 nataction : no NAT natpasslog {
3879 yyerror("\"pass\" not valid with \"no\"");
3890 | no RDR natpasslog {
3892 yyerror("\"pass\" not valid with \"no\"");
3905 natrule : nataction interface af proto fromto tag tagged rtable
3910 if (check_rulestate(PFCTL_STATE_NAT))
3913 memset(&r, 0, sizeof(r));
3922 if ($5.src.host && $5.src.host->af &&
3923 !$5.src.host->ifindex)
3924 r.af = $5.src.host->af;
3925 else if ($5.dst.host && $5.dst.host->af &&
3926 !$5.dst.host->ifindex)
3927 r.af = $5.dst.host->af;
3931 if (strlcpy(r.tagname, $6, PF_TAG_NAME_SIZE) >=
3933 yyerror("tag too long, max %u chars",
3934 PF_TAG_NAME_SIZE - 1);
3939 if (strlcpy(r.match_tagname, $7.name,
3940 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
3941 yyerror("tag too long, max %u chars",
3942 PF_TAG_NAME_SIZE - 1);
3945 r.match_tag_not = $7.neg;
3948 if (r.action == PF_NONAT || r.action == PF_NORDR) {
3950 yyerror("translation rule with 'no' "
3951 "does not need '->'");
3955 if ($9 == NULL || $9->host == NULL) {
3956 yyerror("translation rule requires '-> "
3960 if (!r.af && ! $9->host->ifindex)
3961 r.af = $9->host->af;
3963 remove_invalid_hosts(&$9->host, &r.af);
3964 if (invalid_redirect($9->host, r.af))
3966 if (check_netmask($9->host, r.af))
3969 r.rpool.proxy_port[0] = ntohs($9->rport.a);
3973 if (!$9->rport.b && $9->rport.t &&
3974 $5.dst.port != NULL) {
3975 r.rpool.proxy_port[1] =
3976 ntohs($9->rport.a) +
3978 $5.dst.port->port[1]) -
3980 $5.dst.port->port[0]));
3982 r.rpool.proxy_port[1] =
3986 r.rpool.proxy_port[1] =
3988 if (!r.rpool.proxy_port[0] &&
3989 !r.rpool.proxy_port[1]) {
3990 r.rpool.proxy_port[0] =
3991 PF_NAT_PROXY_PORT_LOW;
3992 r.rpool.proxy_port[1] =
3993 PF_NAT_PROXY_PORT_HIGH;
3994 } else if (!r.rpool.proxy_port[1])
3995 r.rpool.proxy_port[1] =
3996 r.rpool.proxy_port[0];
4002 r.rpool.opts = $10.type;
4003 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
4004 PF_POOL_NONE && ($9->host->next != NULL ||
4005 $9->host->addr.type == PF_ADDR_TABLE ||
4006 DYNIF_MULTIADDR($9->host->addr)))
4007 r.rpool.opts = PF_POOL_ROUNDROBIN;
4008 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4009 PF_POOL_ROUNDROBIN &&
4010 disallow_table($9->host, "tables are only "
4011 "supported in round-robin redirection "
4014 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4015 PF_POOL_ROUNDROBIN &&
4016 disallow_alias($9->host, "interface (%s) "
4017 "is only supported in round-robin "
4018 "redirection pools"))
4020 if ($9->host->next != NULL) {
4021 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4022 PF_POOL_ROUNDROBIN) {
4023 yyerror("only round-robin "
4024 "valid for multiple "
4025 "redirection addresses");
4031 if ($10.key != NULL)
4032 memcpy(&r.rpool.key, $10.key,
4033 sizeof(struct pf_poolhashkey));
4036 r.rpool.opts |= $10.opts;
4038 if ($10.staticport) {
4039 if (r.action != PF_NAT) {
4040 yyerror("the 'static-port' option is "
4041 "only valid with nat rules");
4044 if (r.rpool.proxy_port[0] !=
4045 PF_NAT_PROXY_PORT_LOW &&
4046 r.rpool.proxy_port[1] !=
4047 PF_NAT_PROXY_PORT_HIGH) {
4048 yyerror("the 'static-port' option can't"
4049 " be used when specifying a port"
4053 r.rpool.proxy_port[0] = 0;
4054 r.rpool.proxy_port[1] = 0;
4057 expand_rule(&r, $2, $9 == NULL ? NULL : $9->host, $4,
4058 $5.src_os, $5.src.host, $5.src.port, $5.dst.host,
4059 $5.dst.port, 0, 0, 0, "");
4064 binatrule : no BINAT natpasslog interface af proto FROM host toipspec tag
4065 tagged rtable redirection
4067 struct pf_rule binat;
4068 struct pf_pooladdr *pa;
4070 if (check_rulestate(PFCTL_STATE_NAT))
4072 if (disallow_urpf_failed($9, "\"urpf-failed\" is not "
4073 "permitted as a binat destination"))
4076 memset(&binat, 0, sizeof(binat));
4079 yyerror("\"pass\" not valid with \"no\"");
4083 binat.action = PF_NOBINAT;
4085 binat.action = PF_BINAT;
4086 binat.natpass = $3.b1;
4088 binat.logif = $3.w2;
4090 if (!binat.af && $8 != NULL && $8->af)
4092 if (!binat.af && $9 != NULL && $9->af)
4095 if (!binat.af && $13 != NULL && $13->host)
4096 binat.af = $13->host->af;
4098 yyerror("address family (inet/inet6) "
4104 memcpy(binat.ifname, $4->ifname,
4105 sizeof(binat.ifname));
4106 binat.ifnot = $4->not;
4111 if (strlcpy(binat.tagname, $10,
4112 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4113 yyerror("tag too long, max %u chars",
4114 PF_TAG_NAME_SIZE - 1);
4118 if (strlcpy(binat.match_tagname, $11.name,
4119 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4120 yyerror("tag too long, max %u chars",
4121 PF_TAG_NAME_SIZE - 1);
4124 binat.match_tag_not = $11.neg;
4125 binat.rtableid = $12;
4128 binat.proto = $6->proto;
4132 if ($8 != NULL && disallow_table($8, "invalid use of "
4133 "table <%s> as the source address of a binat rule"))
4135 if ($8 != NULL && disallow_alias($8, "invalid use of "
4136 "interface (%s) as the source address of a binat "
4139 if ($13 != NULL && $13->host != NULL && disallow_table(
4140 $13->host, "invalid use of table <%s> as the "
4141 "redirect address of a binat rule"))
4143 if ($13 != NULL && $13->host != NULL && disallow_alias(
4144 $13->host, "invalid use of interface (%s) as the "
4145 "redirect address of a binat rule"))
4150 yyerror("multiple binat ip addresses");
4153 if ($8->addr.type == PF_ADDR_DYNIFTL)
4155 if ($8->af != binat.af) {
4156 yyerror("binat ip versions must match");
4159 if (check_netmask($8, binat.af))
4161 memcpy(&binat.src.addr, &$8->addr,
4162 sizeof(binat.src.addr));
4167 yyerror("multiple binat ip addresses");
4170 if ($9->af != binat.af && $9->af) {
4171 yyerror("binat ip versions must match");
4174 if (check_netmask($9, binat.af))
4176 memcpy(&binat.dst.addr, &$9->addr,
4177 sizeof(binat.dst.addr));
4178 binat.dst.neg = $9->not;
4182 if (binat.action == PF_NOBINAT) {
4184 yyerror("'no binat' rule does not need"
4189 if ($13 == NULL || $13->host == NULL) {
4190 yyerror("'binat' rule requires"
4195 remove_invalid_hosts(&$13->host, &binat.af);
4196 if (invalid_redirect($13->host, binat.af))
4198 if ($13->host->next != NULL) {
4199 yyerror("binat rule must redirect to "
4200 "a single address");
4203 if (check_netmask($13->host, binat.af))
4206 if (!PF_AZERO(&binat.src.addr.v.a.mask,
4208 !PF_AEQ(&binat.src.addr.v.a.mask,
4209 &$13->host->addr.v.a.mask, binat.af)) {
4210 yyerror("'binat' source mask and "
4211 "redirect mask must be the same");
4215 TAILQ_INIT(&binat.rpool.list);
4216 pa = calloc(1, sizeof(struct pf_pooladdr));
4218 err(1, "binat: calloc");
4219 pa->addr = $13->host->addr;
4221 TAILQ_INSERT_TAIL(&binat.rpool.list,
4227 pfctl_add_rule(pf, &binat, "");
4231 tag : /* empty */ { $$ = NULL; }
4232 | TAG STRING { $$ = $2; }
4235 tagged : /* empty */ { $$.neg = 0; $$.name = NULL; }
4236 | not TAGGED string { $$.neg = $1; $$.name = $3; }
4239 rtable : /* empty */ { $$ = -1; }
4245 route_host : STRING {
4246 $$ = calloc(1, sizeof(struct node_host));
4248 err(1, "route_host: calloc");
4250 set_ipmask($$, 128);
4254 | '(' STRING host ')' {
4260 route_host_list : route_host optnl { $$ = $1; }
4261 | route_host_list comma route_host optnl {
4264 if ($1->af != $3->af) {
4265 yyerror("all pool addresses must be in the "
4266 "same address family");
4269 $1->tail->next = $3;
4270 $1->tail = $3->tail;
4275 routespec : route_host { $$ = $1; }
4276 | '{' optnl route_host_list '}' { $$ = $3; }
4279 route : /* empty */ {
4286 $$.rt = PF_FASTROUTE;
4289 | ROUTETO routespec pool_opts {
4292 $$.pool_opts = $3.type | $3.opts;
4296 | REPLYTO routespec pool_opts {
4299 $$.pool_opts = $3.type | $3.opts;
4303 | DUPTO routespec pool_opts {
4306 $$.pool_opts = $3.type | $3.opts;
4312 timeout_spec : STRING NUMBER
4314 if (check_rulestate(PFCTL_STATE_OPTION)) {
4318 if ($2 < 0 || $2 > UINT_MAX) {
4319 yyerror("only positive values permitted");
4322 if (pfctl_set_timeout(pf, $1, $2, 0) != 0) {
4323 yyerror("unknown timeout %s", $1);
4331 timeout_list : timeout_list comma timeout_spec optnl
4332 | timeout_spec optnl
4335 limit_spec : STRING NUMBER
4337 if (check_rulestate(PFCTL_STATE_OPTION)) {
4341 if ($2 < 0 || $2 > UINT_MAX) {
4342 yyerror("only positive values permitted");
4345 if (pfctl_set_limit(pf, $1, $2) != 0) {
4346 yyerror("unable to set limit %s %" PRId64, $1, $2);
4354 limit_list : limit_list comma limit_spec optnl
4362 yesno : NO { $$ = 0; }
4364 if (!strcmp($1, "yes"))
4367 yyerror("invalid value '%s', expected 'yes' "
4376 unaryop : '=' { $$ = PF_OP_EQ; }
4377 | '!' '=' { $$ = PF_OP_NE; }
4378 | '<' '=' { $$ = PF_OP_LE; }
4379 | '<' { $$ = PF_OP_LT; }
4380 | '>' '=' { $$ = PF_OP_GE; }
4381 | '>' { $$ = PF_OP_GT; }
4387 yyerror(const char *fmt, ...)
4393 fprintf(stderr, "%s:%d: ", file->name, yylval.lineno);
4394 vfprintf(stderr, fmt, ap);
4395 fprintf(stderr, "\n");
4401 disallow_table(struct node_host *h, const char *fmt)
4403 for (; h != NULL; h = h->next)
4404 if (h->addr.type == PF_ADDR_TABLE) {
4405 yyerror(fmt, h->addr.v.tblname);
4412 disallow_urpf_failed(struct node_host *h, const char *fmt)
4414 for (; h != NULL; h = h->next)
4415 if (h->addr.type == PF_ADDR_URPFFAILED) {
4423 disallow_alias(struct node_host *h, const char *fmt)
4425 for (; h != NULL; h = h->next)
4426 if (DYNIF_MULTIADDR(h->addr)) {
4427 yyerror(fmt, h->addr.v.tblname);
4434 rule_consistent(struct pf_rule *r, int anchor_call __unused)
4438 switch (r->action) {
4443 problems = filter_consistent(r, anchor_call);
4447 problems = nat_consistent(r);
4451 problems = rdr_consistent(r);
4462 filter_consistent(struct pf_rule *r, int anchor_call __unused)
4466 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP &&
4467 (r->src.port_op || r->dst.port_op)) {
4468 yyerror("port only applies to tcp/udp");
4471 if (r->proto != IPPROTO_ICMP && r->proto != IPPROTO_ICMPV6 &&
4472 (r->type || r->code)) {
4473 yyerror("icmp-type/code only applies to icmp");
4476 if (!r->af && (r->type || r->code)) {
4477 yyerror("must indicate address family with icmp-type/code");
4480 if (r->overload_tblname[0] &&
4481 r->max_src_conn == 0 && r->max_src_conn_rate.seconds == 0) {
4482 yyerror("'overload' requires 'max-src-conn' "
4483 "or 'max-src-conn-rate'");
4486 if ((r->proto == IPPROTO_ICMP && r->af == AF_INET6) ||
4487 (r->proto == IPPROTO_ICMPV6 && r->af == AF_INET)) {
4488 yyerror("proto %s doesn't match address family %s",
4489 r->proto == IPPROTO_ICMP ? "icmp" : "icmp6",
4490 r->af == AF_INET ? "inet" : "inet6");
4493 if (r->allow_opts && r->action != PF_PASS) {
4494 yyerror("allow-opts can only be specified for pass rules");
4497 if (r->rule_flag & PFRULE_FRAGMENT && (r->src.port_op ||
4498 r->dst.port_op || r->flagset || r->type || r->code)) {
4499 yyerror("fragments can be filtered only on IP header fields");
4502 if (r->rule_flag & PFRULE_RETURNRST && r->proto != IPPROTO_TCP) {
4503 yyerror("return-rst can only be applied to TCP rules");
4506 if (r->max_src_nodes && !(r->rule_flag & PFRULE_RULESRCTRACK)) {
4507 yyerror("max-src-nodes requires 'source-track rule'");
4510 if (r->action == PF_DROP && r->keep_state) {
4511 yyerror("keep state on block rules doesn't make sense");
4514 if (r->rule_flag & PFRULE_STATESLOPPY &&
4515 (r->keep_state == PF_STATE_MODULATE ||
4516 r->keep_state == PF_STATE_SYNPROXY)) {
4517 yyerror("sloppy state matching cannot be used with "
4518 "synproxy state or modulate state");
4525 nat_consistent(struct pf_rule *r __unused)
4527 return (0); /* yeah! */
4531 rdr_consistent(struct pf_rule *r)
4535 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP) {
4536 if (r->src.port_op) {
4537 yyerror("src port only applies to tcp/udp");
4540 if (r->dst.port_op) {
4541 yyerror("dst port only applies to tcp/udp");
4544 if (r->rpool.proxy_port[0]) {
4545 yyerror("rpool port only applies to tcp/udp");
4549 if (r->dst.port_op &&
4550 r->dst.port_op != PF_OP_EQ && r->dst.port_op != PF_OP_RRG) {
4551 yyerror("invalid port operator for rdr destination port");
4558 process_tabledef(char *name, struct table_opts *opts)
4560 struct pfr_buffer ab;
4561 struct node_tinit *ti;
4563 bzero(&ab, sizeof(ab));
4564 ab.pfrb_type = PFRB_ADDRS;
4565 SIMPLEQ_FOREACH(ti, &opts->init_nodes, entries) {
4567 if (pfr_buf_load(&ab, ti->file, 0, append_addr)) {
4569 yyerror("cannot load \"%s\": %s",
4570 ti->file, strerror(errno));
4572 yyerror("file \"%s\" contains bad data",
4577 if (append_addr_host(&ab, ti->host, 0, 0)) {
4578 yyerror("cannot create address buffer: %s",
4583 if (pf->opts & PF_OPT_VERBOSE)
4584 print_tabledef(name, opts->flags, opts->init_addr,
4586 if (!(pf->opts & PF_OPT_NOACTION) &&
4587 pfctl_define_table(name, opts->flags, opts->init_addr,
4588 pf->anchor->name, &ab, pf->anchor->ruleset.tticket)) {
4589 yyerror("cannot define table %s: %s", name,
4590 pfr_strerror(errno));
4606 /* macro gore, but you should've seen the prior indentation nightmare... */
4608 #define FREE_LIST(T,r) \
4611 while (node != NULL) { \
4613 node = node->next; \
4618 #define LOOP_THROUGH(T,n,r,C) \
4622 r = calloc(1, sizeof(T)); \
4624 err(1, "LOOP: calloc"); \
4628 while (n != NULL) { \
4637 expand_label_str(char *label, size_t len, const char *srch, const char *repl)
4642 if ((tmp = calloc(1, len)) == NULL)
4643 err(1, "expand_label_str: calloc");
4645 while ((q = strstr(p, srch)) != NULL) {
4647 if ((strlcat(tmp, p, len) >= len) ||
4648 (strlcat(tmp, repl, len) >= len))
4649 errx(1, "expand_label: label too long");
4653 if (strlcat(tmp, p, len) >= len)
4654 errx(1, "expand_label: label too long");
4655 strlcpy(label, tmp, len); /* always fits */
4660 expand_label_if(const char *name, char *label, size_t len, const char *ifname)
4662 if (strstr(label, name) != NULL) {
4664 expand_label_str(label, len, name, "any");
4666 expand_label_str(label, len, name, ifname);
4671 expand_label_addr(const char *name, char *label, size_t len, sa_family_t af,
4672 struct node_host *h)
4674 char tmp[64], tmp_not[66];
4676 if (strstr(label, name) != NULL) {
4677 switch (h->addr.type) {
4678 case PF_ADDR_DYNIFTL:
4679 snprintf(tmp, sizeof(tmp), "(%s)", h->addr.v.ifname);
4682 snprintf(tmp, sizeof(tmp), "<%s>", h->addr.v.tblname);
4684 case PF_ADDR_NOROUTE:
4685 snprintf(tmp, sizeof(tmp), "no-route");
4687 case PF_ADDR_URPFFAILED:
4688 snprintf(tmp, sizeof(tmp), "urpf-failed");
4690 case PF_ADDR_ADDRMASK:
4691 if (!af || (PF_AZERO(&h->addr.v.a.addr, af) &&
4692 PF_AZERO(&h->addr.v.a.mask, af)))
4693 snprintf(tmp, sizeof(tmp), "any");
4698 if (inet_ntop(af, &h->addr.v.a.addr, a,
4700 snprintf(tmp, sizeof(tmp), "?");
4702 bits = unmask(&h->addr.v.a.mask, af);
4703 if ((af == AF_INET && bits < 32) ||
4704 (af == AF_INET6 && bits < 128))
4705 snprintf(tmp, sizeof(tmp),
4708 snprintf(tmp, sizeof(tmp),
4714 snprintf(tmp, sizeof(tmp), "?");
4719 snprintf(tmp_not, sizeof(tmp_not), "! %s", tmp);
4720 expand_label_str(label, len, name, tmp_not);
4722 expand_label_str(label, len, name, tmp);
4727 expand_label_port(const char *name, char *label, size_t len,
4728 struct node_port *port)
4730 char a1[6], a2[6], op[13] = "";
4732 if (strstr(label, name) != NULL) {
4733 snprintf(a1, sizeof(a1), "%u", ntohs(port->port[0]));
4734 snprintf(a2, sizeof(a2), "%u", ntohs(port->port[1]));
4737 else if (port->op == PF_OP_IRG)
4738 snprintf(op, sizeof(op), "%s><%s", a1, a2);
4739 else if (port->op == PF_OP_XRG)
4740 snprintf(op, sizeof(op), "%s<>%s", a1, a2);
4741 else if (port->op == PF_OP_EQ)
4742 snprintf(op, sizeof(op), "%s", a1);
4743 else if (port->op == PF_OP_NE)
4744 snprintf(op, sizeof(op), "!=%s", a1);
4745 else if (port->op == PF_OP_LT)
4746 snprintf(op, sizeof(op), "<%s", a1);
4747 else if (port->op == PF_OP_LE)
4748 snprintf(op, sizeof(op), "<=%s", a1);
4749 else if (port->op == PF_OP_GT)
4750 snprintf(op, sizeof(op), ">%s", a1);
4751 else if (port->op == PF_OP_GE)
4752 snprintf(op, sizeof(op), ">=%s", a1);
4753 expand_label_str(label, len, name, op);
4758 expand_label_proto(const char *name, char *label, size_t len, u_int8_t proto)
4760 struct protoent *pe;
4763 if (strstr(label, name) != NULL) {
4764 pe = getprotobynumber(proto);
4766 expand_label_str(label, len, name, pe->p_name);
4768 snprintf(n, sizeof(n), "%u", proto);
4769 expand_label_str(label, len, name, n);
4775 expand_label_nr(const char *name, char *label, size_t len)
4779 if (strstr(label, name) != NULL) {
4780 snprintf(n, sizeof(n), "%u", pf->anchor->match);
4781 expand_label_str(label, len, name, n);
4786 expand_label(char *label, size_t len, const char *ifname, sa_family_t af,
4787 struct node_host *src_host, struct node_port *src_port,
4788 struct node_host *dst_host, struct node_port *dst_port,
4791 expand_label_if("$if", label, len, ifname);
4792 expand_label_addr("$srcaddr", label, len, af, src_host);
4793 expand_label_addr("$dstaddr", label, len, af, dst_host);
4794 expand_label_port("$srcport", label, len, src_port);
4795 expand_label_port("$dstport", label, len, dst_port);
4796 expand_label_proto("$proto", label, len, proto);
4797 expand_label_nr("$nr", label, len);
4801 expand_altq(struct pf_altq *a, struct node_if *interfaces,
4802 struct node_queue *nqueues, struct node_queue_bw bwspec,
4803 struct node_queue_opt *opts)
4805 struct pf_altq pa, pb;
4806 char qname[PF_QNAME_SIZE];
4807 struct node_queue *n;
4808 struct node_queue_bw bw;
4811 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
4812 FREE_LIST(struct node_if, interfaces);
4813 FREE_LIST(struct node_queue, nqueues);
4817 LOOP_THROUGH(struct node_if, interface, interfaces,
4818 memcpy(&pa, a, sizeof(struct pf_altq));
4819 if (strlcpy(pa.ifname, interface->ifname,
4820 sizeof(pa.ifname)) >= sizeof(pa.ifname))
4821 errx(1, "expand_altq: strlcpy");
4823 if (interface->not) {
4824 yyerror("altq on ! <interface> is not supported");
4827 if (eval_pfaltq(pf, &pa, &bwspec, opts))
4830 if (pfctl_add_altq(pf, &pa))
4833 if (pf->opts & PF_OPT_VERBOSE) {
4834 print_altq(&pf->paltq->altq, 0,
4836 if (nqueues && nqueues->tail) {
4838 LOOP_THROUGH(struct node_queue, queue,
4848 if (pa.scheduler == ALTQT_CBQ ||
4849 pa.scheduler == ALTQT_HFSC) {
4850 /* now create a root queue */
4851 memset(&pb, 0, sizeof(struct pf_altq));
4852 if (strlcpy(qname, "root_", sizeof(qname)) >=
4854 errx(1, "expand_altq: strlcpy");
4855 if (strlcat(qname, interface->ifname,
4856 sizeof(qname)) >= sizeof(qname))
4857 errx(1, "expand_altq: strlcat");
4858 if (strlcpy(pb.qname, qname,
4859 sizeof(pb.qname)) >= sizeof(pb.qname))
4860 errx(1, "expand_altq: strlcpy");
4861 if (strlcpy(pb.ifname, interface->ifname,
4862 sizeof(pb.ifname)) >= sizeof(pb.ifname))
4863 errx(1, "expand_altq: strlcpy");
4864 pb.qlimit = pa.qlimit;
4865 pb.scheduler = pa.scheduler;
4866 bw.bw_absolute = pa.ifbandwidth;
4868 if (eval_pfqueue(pf, &pb, &bw, opts))
4871 if (pfctl_add_altq(pf, &pb))
4875 LOOP_THROUGH(struct node_queue, queue, nqueues,
4876 n = calloc(1, sizeof(struct node_queue));
4878 err(1, "expand_altq: calloc");
4879 if (pa.scheduler == ALTQT_CBQ ||
4880 pa.scheduler == ALTQT_HFSC /*||
4881 pa.scheduler == ALTQT_FAIRQ*/)
4882 if (strlcpy(n->parent, qname,
4883 sizeof(n->parent)) >=
4885 errx(1, "expand_altq: strlcpy");
4886 if (strlcpy(n->queue, queue->queue,
4887 sizeof(n->queue)) >= sizeof(n->queue))
4888 errx(1, "expand_altq: strlcpy");
4889 if (strlcpy(n->ifname, interface->ifname,
4890 sizeof(n->ifname)) >= sizeof(n->ifname))
4891 errx(1, "expand_altq: strlcpy");
4892 n->scheduler = pa.scheduler;
4898 queues->tail->next = n;
4904 FREE_LIST(struct node_if, interfaces);
4905 FREE_LIST(struct node_queue, nqueues);
4911 expand_queue(struct pf_altq *a, struct node_if *interfaces,
4912 struct node_queue *nqueues, struct node_queue_bw bwspec,
4913 struct node_queue_opt *opts)
4915 struct node_queue *n, *nq;
4920 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
4921 FREE_LIST(struct node_queue, nqueues);
4925 if (queues == NULL) {
4926 yyerror("queue %s has no parent", a->qname);
4927 FREE_LIST(struct node_queue, nqueues);
4931 LOOP_THROUGH(struct node_if, interface, interfaces,
4932 LOOP_THROUGH(struct node_queue, tqueue, queues,
4933 if (!strncmp(a->qname, tqueue->queue, PF_QNAME_SIZE) &&
4934 (interface->ifname[0] == 0 ||
4935 (!interface->not && !strncmp(interface->ifname,
4936 tqueue->ifname, IFNAMSIZ)) ||
4937 (interface->not && strncmp(interface->ifname,
4938 tqueue->ifname, IFNAMSIZ)))) {
4939 /* found ourself in queues */
4942 memcpy(&pa, a, sizeof(struct pf_altq));
4944 if (pa.scheduler != ALTQT_NONE &&
4945 pa.scheduler != tqueue->scheduler) {
4946 yyerror("exactly one scheduler type "
4947 "per interface allowed");
4950 pa.scheduler = tqueue->scheduler;
4952 /* scheduler dependent error checking */
4953 switch (pa.scheduler) {
4955 if (nqueues != NULL) {
4956 yyerror("priq queues cannot "
4957 "have child queues");
4960 if (bwspec.bw_absolute > 0 ||
4961 bwspec.bw_percent < 100) {
4962 yyerror("priq doesn't take "
4971 if (strlcpy(pa.ifname, tqueue->ifname,
4972 sizeof(pa.ifname)) >= sizeof(pa.ifname))
4973 errx(1, "expand_queue: strlcpy");
4974 if (strlcpy(pa.parent, tqueue->parent,
4975 sizeof(pa.parent)) >= sizeof(pa.parent))
4976 errx(1, "expand_queue: strlcpy");
4978 if (eval_pfqueue(pf, &pa, &bwspec, opts))
4981 if (pfctl_add_altq(pf, &pa))
4984 for (nq = nqueues; nq != NULL; nq = nq->next) {
4985 if (!strcmp(a->qname, nq->queue)) {
4986 yyerror("queue cannot have "
4992 sizeof(struct node_queue));
4994 err(1, "expand_queue: calloc");
4995 if (strlcpy(n->parent, a->qname,
4996 sizeof(n->parent)) >=
4998 errx(1, "expand_queue strlcpy");
4999 if (strlcpy(n->queue, nq->queue,
5000 sizeof(n->queue)) >=
5002 errx(1, "expand_queue strlcpy");
5003 if (strlcpy(n->ifname, tqueue->ifname,
5004 sizeof(n->ifname)) >=
5006 errx(1, "expand_queue strlcpy");
5007 n->scheduler = tqueue->scheduler;
5013 queues->tail->next = n;
5017 if ((pf->opts & PF_OPT_VERBOSE) && (
5018 (found == 1 && interface->ifname[0] == 0) ||
5019 (found > 0 && interface->ifname[0] != 0))) {
5020 print_queue(&pf->paltq->altq, 0,
5021 &bwspec, interface->ifname[0] != 0,
5023 if (nqueues && nqueues->tail) {
5025 LOOP_THROUGH(struct node_queue,
5038 FREE_LIST(struct node_queue, nqueues);
5039 FREE_LIST(struct node_if, interfaces);
5042 yyerror("queue %s has no parent", a->qname);
5053 expand_rule(struct pf_rule *r,
5054 struct node_if *interfaces, struct node_host *rpool_hosts,
5055 struct node_proto *protos, struct node_os *src_oses,
5056 struct node_host *src_hosts, struct node_port *src_ports,
5057 struct node_host *dst_hosts, struct node_port *dst_ports,
5058 struct node_uid *uids, struct node_gid *gids, struct node_icmp *icmp_types,
5059 const char *anchor_call)
5061 sa_family_t af = r->af;
5062 int added = 0, error = 0;
5063 char ifname[IF_NAMESIZE];
5064 char label[PF_RULE_LABEL_SIZE];
5065 char tagname[PF_TAG_NAME_SIZE];
5066 char match_tagname[PF_TAG_NAME_SIZE];
5067 struct pf_pooladdr *pa;
5068 struct node_host *h;
5069 u_int8_t flags, flagset, keep_state;
5071 if (strlcpy(label, r->label, sizeof(label)) >= sizeof(label))
5072 errx(1, "expand_rule: strlcpy");
5073 if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname))
5074 errx(1, "expand_rule: strlcpy");
5075 if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >=
5076 sizeof(match_tagname))
5077 errx(1, "expand_rule: strlcpy");
5079 flagset = r->flagset;
5080 keep_state = r->keep_state;
5082 LOOP_THROUGH(struct node_if, interface, interfaces,
5083 LOOP_THROUGH(struct node_proto, proto, protos,
5084 LOOP_THROUGH(struct node_icmp, icmp_type, icmp_types,
5085 LOOP_THROUGH(struct node_host, src_host, src_hosts,
5086 LOOP_THROUGH(struct node_port, src_port, src_ports,
5087 LOOP_THROUGH(struct node_os, src_os, src_oses,
5088 LOOP_THROUGH(struct node_host, dst_host, dst_hosts,
5089 LOOP_THROUGH(struct node_port, dst_port, dst_ports,
5090 LOOP_THROUGH(struct node_uid, uid, uids,
5091 LOOP_THROUGH(struct node_gid, gid, gids,
5094 /* for link-local IPv6 address, interface must match up */
5095 if ((r->af && src_host->af && r->af != src_host->af) ||
5096 (r->af && dst_host->af && r->af != dst_host->af) ||
5097 (src_host->af && dst_host->af &&
5098 src_host->af != dst_host->af) ||
5099 (src_host->ifindex && dst_host->ifindex &&
5100 src_host->ifindex != dst_host->ifindex) ||
5101 (src_host->ifindex && *interface->ifname &&
5102 src_host->ifindex != if_nametoindex(interface->ifname)) ||
5103 (dst_host->ifindex && *interface->ifname &&
5104 dst_host->ifindex != if_nametoindex(interface->ifname)))
5106 if (!r->af && src_host->af)
5107 r->af = src_host->af;
5108 else if (!r->af && dst_host->af)
5109 r->af = dst_host->af;
5111 if (*interface->ifname)
5112 strlcpy(r->ifname, interface->ifname,
5114 else if (if_indextoname(src_host->ifindex, ifname))
5115 strlcpy(r->ifname, ifname, sizeof(r->ifname));
5116 else if (if_indextoname(dst_host->ifindex, ifname))
5117 strlcpy(r->ifname, ifname, sizeof(r->ifname));
5119 memset(r->ifname, '\0', sizeof(r->ifname));
5121 if (strlcpy(r->label, label, sizeof(r->label)) >=
5123 errx(1, "expand_rule: strlcpy");
5124 if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >=
5126 errx(1, "expand_rule: strlcpy");
5127 if (strlcpy(r->match_tagname, match_tagname,
5128 sizeof(r->match_tagname)) >= sizeof(r->match_tagname))
5129 errx(1, "expand_rule: strlcpy");
5130 expand_label(r->label, PF_RULE_LABEL_SIZE, r->ifname, r->af,
5131 src_host, src_port, dst_host, dst_port, proto->proto);
5132 expand_label(r->tagname, PF_TAG_NAME_SIZE, r->ifname, r->af,
5133 src_host, src_port, dst_host, dst_port, proto->proto);
5134 expand_label(r->match_tagname, PF_TAG_NAME_SIZE, r->ifname,
5135 r->af, src_host, src_port, dst_host, dst_port,
5138 error += check_netmask(src_host, r->af);
5139 error += check_netmask(dst_host, r->af);
5141 r->ifnot = interface->not;
5142 r->proto = proto->proto;
5143 r->src.addr = src_host->addr;
5144 r->src.neg = src_host->not;
5145 r->src.port[0] = src_port->port[0];
5146 r->src.port[1] = src_port->port[1];
5147 r->src.port_op = src_port->op;
5148 r->dst.addr = dst_host->addr;
5149 r->dst.neg = dst_host->not;
5150 r->dst.port[0] = dst_port->port[0];
5151 r->dst.port[1] = dst_port->port[1];
5152 r->dst.port_op = dst_port->op;
5153 r->uid.op = uid->op;
5154 r->uid.uid[0] = uid->uid[0];
5155 r->uid.uid[1] = uid->uid[1];
5156 r->gid.op = gid->op;
5157 r->gid.gid[0] = gid->gid[0];
5158 r->gid.gid[1] = gid->gid[1];
5159 r->type = icmp_type->type;
5160 r->code = icmp_type->code;
5162 if ((keep_state == PF_STATE_MODULATE ||
5163 keep_state == PF_STATE_SYNPROXY) &&
5164 r->proto && r->proto != IPPROTO_TCP)
5165 r->keep_state = PF_STATE_NORMAL;
5167 r->keep_state = keep_state;
5169 if (r->proto && r->proto != IPPROTO_TCP) {
5174 r->flagset = flagset;
5176 if (icmp_type->proto && r->proto != icmp_type->proto) {
5177 yyerror("icmp-type mismatch");
5181 if (src_os && src_os->os) {
5182 r->os_fingerprint = pfctl_get_fingerprint(src_os->os);
5183 if ((pf->opts & PF_OPT_VERBOSE2) &&
5184 r->os_fingerprint == PF_OSFP_NOMATCH)
5186 "warning: unknown '%s' OS fingerprint\n",
5189 r->os_fingerprint = PF_OSFP_ANY;
5192 TAILQ_INIT(&r->rpool.list);
5193 for (h = rpool_hosts; h != NULL; h = h->next) {
5194 pa = calloc(1, sizeof(struct pf_pooladdr));
5196 err(1, "expand_rule: calloc");
5198 if (h->ifname != NULL) {
5199 if (strlcpy(pa->ifname, h->ifname,
5200 sizeof(pa->ifname)) >=
5202 errx(1, "expand_rule: strlcpy");
5205 TAILQ_INSERT_TAIL(&r->rpool.list, pa, entries);
5208 if (rule_consistent(r, anchor_call[0]) < 0 || error)
5209 yyerror("skipping rule due to errors");
5211 r->nr = pf->astack[pf->asd]->match++;
5212 pfctl_add_rule(pf, r, anchor_call);
5218 FREE_LIST(struct node_if, interfaces);
5219 FREE_LIST(struct node_proto, protos);
5220 FREE_LIST(struct node_host, src_hosts);
5221 FREE_LIST(struct node_port, src_ports);
5222 FREE_LIST(struct node_os, src_oses);
5223 FREE_LIST(struct node_host, dst_hosts);
5224 FREE_LIST(struct node_port, dst_ports);
5225 FREE_LIST(struct node_uid, uids);
5226 FREE_LIST(struct node_gid, gids);
5227 FREE_LIST(struct node_icmp, icmp_types);
5228 FREE_LIST(struct node_host, rpool_hosts);
5231 yyerror("rule expands to no valid combination");
5235 expand_skip_interface(struct node_if *interfaces)
5239 if (!interfaces || (!interfaces->next && !interfaces->not &&
5240 !strcmp(interfaces->ifname, "none"))) {
5241 if (pf->opts & PF_OPT_VERBOSE)
5242 printf("set skip on none\n");
5243 errs = pfctl_set_interface_flags(pf, __DECONST(char *, ""), PFI_IFLAG_SKIP, 0);
5247 if (pf->opts & PF_OPT_VERBOSE)
5248 printf("set skip on {");
5249 LOOP_THROUGH(struct node_if, interface, interfaces,
5250 if (pf->opts & PF_OPT_VERBOSE)
5251 printf(" %s", interface->ifname);
5252 if (interface->not) {
5253 yyerror("skip on ! <interface> is not supported");
5256 errs += pfctl_set_interface_flags(pf,
5257 interface->ifname, PFI_IFLAG_SKIP, 1);
5259 if (pf->opts & PF_OPT_VERBOSE)
5262 FREE_LIST(struct node_if, interfaces);
5274 check_rulestate(int desired_state)
5276 if (require_order && (rulestate > desired_state)) {
5277 yyerror("Rules must be in order: options, normalization, "
5278 "queueing, translation, filtering");
5281 rulestate = desired_state;
5286 kw_cmp(const void *k, const void *e)
5288 return (strcmp(k, ((const struct keywords *)e)->k_name));
5294 /* this has to be sorted always */
5295 static const struct keywords keywords[] = {
5297 { "allow-opts", ALLOWOPTS},
5299 { "anchor", ANCHOR},
5300 { "antispoof", ANTISPOOF},
5302 { "bandwidth", BANDWIDTH},
5304 { "binat-anchor", BINATANCHOR},
5305 { "bitmask", BITMASK},
5307 { "block-policy", BLOCKPOLICY},
5308 { "buckets", BUCKETS},
5311 { "crop", FRAGCROP},
5313 { "divert-reply", DIVERTREPLY},
5314 { "divert-to", DIVERTTO},
5316 { "drop-ovl", FRAGDROP},
5319 { "fastroute", FASTROUTE},
5320 { "file", FILENAME},
5321 { "fingerprints", FINGERPRINTS},
5323 { "floating", FLOATING},
5326 { "fragment", FRAGMENT},
5328 { "global", GLOBAL},
5330 { "hash-only", HASHONLY},
5333 { "hostid", HOSTID},
5334 { "icmp-type", ICMPTYPE},
5335 { "icmp6-type", ICMP6TYPE},
5336 { "if-bound", IFBOUND},
5338 { "include", INCLUDE},
5342 { "keep-policy", KEEPPOLICY},
5345 { "linkshare", LINKSHARE},
5348 { "loginterface", LOGINTERFACE},
5350 { "max-mss", MAXMSS},
5351 { "max-src-conn", MAXSRCCONN},
5352 { "max-src-conn-rate", MAXSRCCONNRATE},
5353 { "max-src-nodes", MAXSRCNODES},
5354 { "max-src-states", MAXSRCSTATES},
5355 { "min-ttl", MINTTL},
5356 { "modulate", MODULATE},
5358 { "nat-anchor", NATANCHOR},
5361 { "no-pickups", NOPICKUPS},
5362 { "no-route", NOROUTE},
5363 { "no-sync", NOSYNC},
5365 { "optimization", OPTIMIZATION},
5368 { "overload", OVERLOAD},
5370 { "pickups", PICKUPS},
5372 { "priority", PRIORITY},
5374 { "probability", PROBABILITY},
5376 { "qlimit", QLIMIT},
5379 { "random", RANDOM},
5380 { "random-id", RANDOMID},
5382 { "rdr-anchor", RDRANCHOR},
5383 { "realtime", REALTIME},
5384 { "reassemble", REASSEMBLE},
5385 { "reply-to", REPLYTO},
5386 { "require-order", REQUIREORDER},
5387 { "return", RETURN},
5388 { "return-icmp", RETURNICMP},
5389 { "return-icmp6", RETURNICMP6},
5390 { "return-rst", RETURNRST},
5391 { "round-robin", ROUNDROBIN},
5393 { "route-to", ROUTETO},
5394 { "rtable", RTABLE},
5396 { "ruleset-optimization", RULESET_OPTIMIZATION},
5399 { "set-tos", SETTOS},
5401 { "sloppy", SLOPPY},
5402 { "source-hash", SOURCEHASH},
5403 { "source-track", SOURCETRACK},
5405 { "state-policy", STATEPOLICY},
5406 { "static-port", STATICPORT},
5407 { "sticky-address", STICKYADDRESS},
5408 { "synproxy", SYNPROXY},
5411 { "tagged", TAGGED},
5412 { "tbrsize", TBRSIZE},
5413 { "timeout", TIMEOUT},
5417 { "upperlimit", UPPERLIMIT},
5418 { "urpf-failed", URPFFAILED},
5421 const struct keywords *p;
5423 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
5424 sizeof(keywords[0]), kw_cmp);
5428 fprintf(stderr, "%s: %d\n", s, p->k_val);
5432 fprintf(stderr, "string: %s\n", s);
5437 #define MAXPUSHBACK 128
5441 char pushback_buffer[MAXPUSHBACK];
5442 int pushback_index = 0;
5450 /* Read character from the parsebuffer instead of input. */
5451 if (parseindex >= 0) {
5452 c = parsebuf[parseindex++];
5461 return (pushback_buffer[--pushback_index]);
5464 if ((c = getc(file->stream)) == EOF) {
5465 yyerror("reached end of file while parsing quoted string");
5466 if (popfile() == EOF)
5473 while ((c = getc(file->stream)) == '\\') {
5474 next = getc(file->stream);
5479 yylval.lineno = file->lineno;
5484 if (popfile() == EOF)
5486 c = getc(file->stream);
5498 if (parseindex >= 0)
5501 if (pushback_index < MAXPUSHBACK-1)
5502 return (pushback_buffer[pushback_index++] = c);
5515 /* skip to either EOF or the first real EOL */
5533 int quotec, next, c;
5538 while ((c = lgetc(0)) == ' ' || c == '\t')
5541 yylval.lineno = file->lineno;
5543 while ((c = lgetc(0)) != '\n' && c != EOF)
5545 if (c == '$' && parsebuf == NULL) {
5547 if ((c = lgetc(0)) == EOF)
5550 if (p + 1 >= buf + sizeof(buf) - 1) {
5551 yyerror("string too long");
5554 if (isalnum(c) || c == '_') {
5564 yyerror("macro '%s' not defined", buf);
5577 if ((c = lgetc(quotec)) == EOF)
5582 } else if (c == '\\') {
5583 if ((next = lgetc(quotec)) == EOF)
5585 if (next == quotec || c == ' ' || c == '\t')
5587 else if (next == '\n')
5591 } else if (c == quotec) {
5595 if (p + 1 >= buf + sizeof(buf) - 1) {
5596 yyerror("string too long");
5601 yylval.v.string = strdup(buf);
5602 if (yylval.v.string == NULL)
5603 err(1, "yylex: strdup");
5608 yylval.v.i = PF_OP_XRG;
5609 return (PORTBINARY);
5616 yylval.v.i = PF_OP_IRG;
5617 return (PORTBINARY);
5629 #define allowed_to_end_number(x) \
5630 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
5632 if (c == '-' || isdigit(c)) {
5635 if ((unsigned)(p-buf) >= sizeof(buf)) {
5636 yyerror("string too long");
5639 } while ((c = lgetc(0)) != EOF && isdigit(c));
5641 if (p == buf + 1 && buf[0] == '-')
5643 if (c == EOF || allowed_to_end_number(c)) {
5644 const char *errstr = NULL;
5647 yylval.v.number = strtonum(buf, LLONG_MIN,
5648 LLONG_MAX, &errstr);
5650 yyerror("\"%s\" invalid number: %s",
5665 #define allowed_in_string(x) \
5666 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
5667 x != '{' && x != '}' && x != '<' && x != '>' && \
5668 x != '!' && x != '=' && x != '/' && x != '#' && \
5671 if (isalnum(c) || c == ':' || c == '_') {
5674 if ((unsigned)(p-buf) >= sizeof(buf)) {
5675 yyerror("string too long");
5678 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
5681 if ((token = lookup(buf)) == STRING)
5682 if ((yylval.v.string = strdup(buf)) == NULL)
5683 err(1, "yylex: strdup");
5687 yylval.lineno = file->lineno;
5696 check_file_secrecy(int fd, const char *fname)
5700 if (fstat(fd, &st)) {
5701 warn("cannot stat %s", fname);
5704 if (st.st_uid != 0 && st.st_uid != getuid()) {
5705 warnx("%s: owner not root or current user", fname);
5708 if (st.st_mode & (S_IRWXG | S_IRWXO)) {
5709 warnx("%s: group/world readable/writeable", fname);
5716 pushfile(const char *name, int secret)
5720 if ((nfile = calloc(1, sizeof(struct file))) == NULL ||
5721 (nfile->name = strdup(name)) == NULL) {
5725 if (TAILQ_FIRST(&files) == NULL && strcmp(nfile->name, "-") == 0) {
5726 nfile->stream = stdin;
5728 if ((nfile->name = strdup("stdin")) == NULL) {
5733 } else if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
5734 warn("%s", nfile->name);
5738 } else if (secret &&
5739 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
5740 fclose(nfile->stream);
5746 TAILQ_INSERT_TAIL(&files, nfile, entry);
5755 if ((prev = TAILQ_PREV(file, files, entry)) != NULL) {
5756 prev->errors += file->errors;
5757 TAILQ_REMOVE(&files, file, entry);
5758 fclose(file->stream);
5768 parse_config(char *filename, struct pfctl *xpf)
5775 rulestate = PFCTL_STATE_NONE;
5776 returnicmpdefault = (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
5777 returnicmp6default =
5778 (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
5779 blockpolicy = PFRULE_DROP;
5782 if ((file = pushfile(filename, 0)) == NULL) {
5783 warn("cannot open the main config file!");
5788 errors = file->errors;
5791 /* Free macros and check which have not been used. */
5792 while ((sym = TAILQ_FIRST(&symhead))) {
5793 if ((pf->opts & PF_OPT_VERBOSE2) && !sym->used)
5794 fprintf(stderr, "warning: macro '%s' not "
5795 "used\n", sym->nam);
5798 TAILQ_REMOVE(&symhead, sym, entry);
5802 return (errors ? -1 : 0);
5806 symset(const char *nam, const char *val, int persist)
5810 for (sym = TAILQ_FIRST(&symhead); sym && strcmp(nam, sym->nam);
5811 sym = TAILQ_NEXT(sym, entry))
5815 if (sym->persist == 1)
5820 TAILQ_REMOVE(&symhead, sym, entry);
5824 if ((sym = calloc(1, sizeof(*sym))) == NULL)
5827 sym->nam = strdup(nam);
5828 if (sym->nam == NULL) {
5832 sym->val = strdup(val);
5833 if (sym->val == NULL) {
5839 sym->persist = persist;
5840 TAILQ_INSERT_TAIL(&symhead, sym, entry);
5845 pfctl_cmdline_symset(char *s)
5850 if ((val = strrchr(s, '=')) == NULL)
5853 if ((sym = malloc(strlen(s) - strlen(val) + 1)) == NULL)
5854 err(1, "pfctl_cmdline_symset: malloc");
5856 strlcpy(sym, s, strlen(s) - strlen(val) + 1);
5858 ret = symset(sym, val + 1, 1);
5865 symget(const char *nam)
5869 TAILQ_FOREACH(sym, &symhead, entry)
5870 if (strcmp(nam, sym->nam) == 0) {
5878 mv_rules(struct pf_ruleset *src, struct pf_ruleset *dst)
5883 for (i = 0; i < PF_RULESET_MAX; ++i) {
5884 while ((r = TAILQ_FIRST(src->rules[i].active.ptr))
5886 TAILQ_REMOVE(src->rules[i].active.ptr, r, entries);
5887 TAILQ_INSERT_TAIL(dst->rules[i].active.ptr, r, entries);
5888 dst->anchor->match++;
5890 src->anchor->match = 0;
5891 while ((r = TAILQ_FIRST(src->rules[i].inactive.ptr))
5893 TAILQ_REMOVE(src->rules[i].inactive.ptr, r, entries);
5894 TAILQ_INSERT_TAIL(dst->rules[i].inactive.ptr,
5901 decide_address_family(struct node_host *n, sa_family_t *af)
5903 if (*af != 0 || n == NULL)
5906 while ((n = n->next) != NULL) {
5915 remove_invalid_hosts(struct node_host **nh, sa_family_t *af)
5917 struct node_host *n = *nh, *prev = NULL;
5920 if (*af && n->af && n->af != *af) {
5921 /* unlink and free n */
5922 struct node_host *next = n->next;
5924 /* adjust tail pointer */
5925 if (n == (*nh)->tail)
5927 /* adjust previous node's next pointer */
5933 if (n->ifname != NULL)
5947 invalid_redirect(struct node_host *nh, sa_family_t af)
5950 struct node_host *n;
5952 /* tables and dyniftl are ok without an address family */
5953 for (n = nh; n != NULL; n = n->next) {
5954 if (n->addr.type != PF_ADDR_TABLE &&
5955 n->addr.type != PF_ADDR_DYNIFTL) {
5956 yyerror("address family not given and "
5957 "translation address expands to multiple "
5958 "address families");
5964 yyerror("no translation address with matching address family "
5972 atoul(char *s, u_long *ulvalp)
5978 ulval = strtoul(s, &ep, 0);
5979 if (s[0] == '\0' || *ep != '\0')
5981 if (errno == ERANGE && ulval == ULONG_MAX)
5993 if (atoul(n, &ulval) == 0) {
5994 if (ulval > 65535) {
5995 yyerror("illegal port value %lu", ulval);
5998 return (htons(ulval));
6000 s = getservbyname(n, "tcp");
6002 s = getservbyname(n, "udp");
6004 yyerror("unknown port %s", n);
6012 rule_label(struct pf_rule *r, char *s)
6015 if (strlcpy(r->label, s, sizeof(r->label)) >=
6017 yyerror("rule label too long (max %zd chars)",
6018 sizeof(r->label)-1);
6026 parseicmpspec(char *w, sa_family_t af)
6028 const struct icmpcodeent *p;
6033 icmptype = returnicmpdefault >> 8;
6035 icmptype = returnicmp6default >> 8;
6037 if (atoul(w, &ulval) == -1) {
6038 if ((p = geticmpcodebyname(icmptype, w, af)) == NULL) {
6039 yyerror("unknown icmp code %s", w);
6045 yyerror("invalid icmp code %lu", ulval);
6048 return (icmptype << 8 | ulval);
6052 parseport(char *port, struct range *r, int extensions)
6054 char *p = strchr(port, ':');
6057 if ((r->a = getservice(port)) == -1)
6063 if ((extensions & PPORT_STAR) && !strcmp(p+1, "*")) {
6065 if ((r->a = getservice(port)) == -1)
6071 if ((extensions & PPORT_RANGE)) {
6073 if ((r->a = getservice(port)) == -1 ||
6074 (r->b = getservice(p)) == -1)
6087 pfctl_load_anchors(int dev, struct pfctl *his_pf, struct pfr_buffer *trans)
6089 struct loadanchors *la;
6091 TAILQ_FOREACH(la, &loadanchorshead, entries) {
6092 if (his_pf->opts & PF_OPT_VERBOSE)
6093 fprintf(stderr, "\nLoading anchor %s from %s\n",
6094 la->anchorname, la->filename);
6095 if (pfctl_rules(dev, la->filename, his_pf->opts, his_pf->optimize,
6096 la->anchorname, trans) == -1)