1 /* $OpenBSD: parse.y,v 1.554 2008/10/17 12:59:53 henning Exp $ */
4 * Copyright (c) 2001 Markus Friedl. All rights reserved.
5 * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
6 * Copyright (c) 2001 Theo de Raadt. All rights reserved.
7 * Copyright (c) 2002,2003 Henning Brauer. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/types.h>
34 #include <sys/socket.h>
37 #include <sys/sysctl.h>
40 #include <netinet/in.h>
41 #include <netinet/in_systm.h>
42 #include <netinet/ip.h>
43 #include <netinet/ip_icmp.h>
44 #include <netinet/icmp6.h>
45 #include <net/pfvar.h>
46 #include <arpa/inet.h>
47 #include <altq/altq.h>
48 #include <altq/altq_cbq.h>
49 #include <altq/altq_priq.h>
50 #include <altq/altq_hfsc.h>
67 #include "pfctl_parser.h"
70 static struct pfctl *pf = NULL;
72 static int rulestate = 0;
73 static u_int16_t returnicmpdefault =
74 (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
75 static u_int16_t returnicmp6default =
76 (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
77 static int blockpolicy = PFRULE_DROP;
78 static int require_order = 1;
79 static int default_statelock;
81 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
83 TAILQ_ENTRY(file) entry;
89 struct file *pushfile(const char *, int);
91 int check_file_secrecy(int, const char *);
94 int yyerror(const char *, ...);
95 int kw_cmp(const void *, const void *);
101 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
103 TAILQ_ENTRY(sym) entry;
109 int symset(const char *, const char *, int);
110 char *symget(const char *);
112 int atoul(char *, u_long *);
125 struct node_proto *next;
126 struct node_proto *tail;
132 struct node_port *next;
133 struct node_port *tail;
139 struct node_uid *next;
140 struct node_uid *tail;
146 struct node_gid *next;
147 struct node_gid *tail;
154 struct node_icmp *next;
155 struct node_icmp *tail;
158 enum { PF_STATE_OPT_MAX, PF_STATE_OPT_NOSYNC, PF_STATE_OPT_SRCTRACK,
159 PF_STATE_OPT_MAX_SRC_STATES, PF_STATE_OPT_MAX_SRC_CONN,
160 PF_STATE_OPT_MAX_SRC_CONN_RATE, PF_STATE_OPT_MAX_SRC_NODES,
161 PF_STATE_OPT_OVERLOAD, PF_STATE_OPT_STATELOCK,
162 PF_STATE_OPT_TIMEOUT, PF_STATE_OPT_SLOPPY,
163 PF_STATE_OPT_PFLOW };
165 enum { PF_SRCTRACK_NONE, PF_SRCTRACK, PF_SRCTRACK_GLOBAL, PF_SRCTRACK_RULE };
167 struct node_state_opt {
170 u_int32_t max_states;
171 u_int32_t max_src_states;
172 u_int32_t max_src_conn;
179 char tblname[PF_TABLE_NAME_SIZE];
181 u_int32_t max_src_nodes;
189 struct node_state_opt *next;
190 struct node_state_opt *tail;
194 struct node_host *host;
195 struct node_port *port;
199 char queue[PF_QNAME_SIZE];
200 char parent[PF_QNAME_SIZE];
201 char ifname[IFNAMSIZ];
203 struct node_queue *next;
204 struct node_queue *tail;
207 struct node_qassign {
214 #define FOM_FLAGS 0x01
215 #define FOM_ICMP 0x02
217 #define FOM_KEEP 0x08
218 #define FOM_SRCTRACK 0x10
219 struct node_uid *uid;
220 struct node_gid *gid;
227 struct node_icmp *icmpspec;
232 struct node_state_opt *options;
237 struct node_qassign queues;
240 u_int8_t match_tag_not;
243 struct node_host *addr;
248 struct antispoof_opts {
255 #define SOM_MINTTL 0x01
256 #define SOM_MAXMSS 0x02
257 #define SOM_FRAGCACHE 0x04
258 #define SOM_SETTOS 0x08
267 u_int8_t match_tag_not;
273 #define QOM_BWSPEC 0x01
274 #define QOM_SCHEDULER 0x02
275 #define QOM_PRIORITY 0x04
276 #define QOM_TBRSIZE 0x08
277 #define QOM_QLIMIT 0x10
278 struct node_queue_bw queue_bwspec;
279 struct node_queue_opt scheduler;
288 struct node_tinithead init_nodes;
293 #define POM_TYPE 0x01
294 #define POM_STICKYADDRESS 0x02
298 struct pf_poolhashkey *key;
303 struct node_hfsc_opts hfsc_opts;
304 struct node_state_opt *keep_state_defaults = NULL;
306 int disallow_table(struct node_host *, const char *);
307 int disallow_urpf_failed(struct node_host *, const char *);
308 int disallow_alias(struct node_host *, const char *);
309 int rule_consistent(struct pf_rule *, int);
310 int filter_consistent(struct pf_rule *, int);
311 int nat_consistent(struct pf_rule *);
312 int rdr_consistent(struct pf_rule *);
313 int process_tabledef(char *, struct table_opts *);
314 void expand_label_str(char *, size_t, const char *, const char *);
315 void expand_label_if(const char *, char *, size_t, const char *);
316 void expand_label_addr(const char *, char *, size_t, u_int8_t,
318 void expand_label_port(const char *, char *, size_t,
320 void expand_label_proto(const char *, char *, size_t, u_int8_t);
321 void expand_label_nr(const char *, char *, size_t);
322 void expand_label(char *, size_t, const char *, u_int8_t,
323 struct node_host *, struct node_port *, struct node_host *,
324 struct node_port *, u_int8_t);
325 void expand_rule(struct pf_rule *, struct node_if *,
326 struct node_host *, struct node_proto *, struct node_os *,
327 struct node_host *, struct node_port *, struct node_host *,
328 struct node_port *, struct node_uid *, struct node_gid *,
329 struct node_icmp *, const char *);
330 int expand_altq(struct pf_altq *, struct node_if *,
331 struct node_queue *, struct node_queue_bw bwspec,
332 struct node_queue_opt *);
333 int expand_queue(struct pf_altq *, struct node_if *,
334 struct node_queue *, struct node_queue_bw,
335 struct node_queue_opt *);
336 int expand_skip_interface(struct node_if *);
338 int check_rulestate(int);
339 int getservice(char *);
340 int rule_label(struct pf_rule *, char *);
341 int rt_tableid_max(void);
343 void mv_rules(struct pf_ruleset *, struct pf_ruleset *);
344 void decide_address_family(struct node_host *, sa_family_t *);
345 void remove_invalid_hosts(struct node_host **, sa_family_t *);
346 int invalid_redirect(struct node_host *, sa_family_t);
347 u_int16_t parseicmpspec(char *, sa_family_t);
349 TAILQ_HEAD(loadanchorshead, loadanchors)
350 loadanchorshead = TAILQ_HEAD_INITIALIZER(loadanchorshead);
353 TAILQ_ENTRY(loadanchors) entries;
376 struct node_if *interface;
377 struct node_proto *proto;
378 struct node_icmp *icmp;
379 struct node_host *host;
381 struct node_port *port;
382 struct node_uid *uid;
383 struct node_gid *gid;
384 struct node_state_opt *state_opt;
387 struct peer src, dst;
388 struct node_os *src_os;
391 struct node_host *host;
395 struct pf_poolhashkey *key;
398 struct node_host *host;
403 struct node_state_opt *options;
414 struct pf_poolhashkey *hashkey;
415 struct node_queue *queue;
416 struct node_queue_opt queue_options;
417 struct node_queue_bw queue_bwspec;
418 struct node_qassign qassign;
419 struct filter_opts filter_opts;
420 struct antispoof_opts antispoof_opts;
421 struct queue_opts queue_opts;
422 struct scrub_opts scrub_opts;
423 struct table_opts table_opts;
424 struct pool_opts pool_opts;
425 struct node_hfsc_opts hfsc_opts;
430 #define PPORT_RANGE 1
432 int parseport(char *, struct range *r, int);
434 #define DYNIF_MULTIADDR(addr) ((addr).type == PF_ADDR_DYNIFTL && \
435 (!((addr).iflags & PFI_AFLAG_NOALIAS) || \
436 !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 BANDWIDTH TBRSIZE LINKSHARE REALTIME UPPERLIMIT
451 %token QUEUE PRIORITY QLIMIT RTABLE
452 %token LOAD RULESET_OPTIMIZATION
453 %token STICKYADDRESS MAXSRCSTATES MAXSRCNODES SOURCETRACK GLOBAL RULE
454 %token MAXSRCCONN MAXSRCCONNRATE OVERLOAD FLUSH SLOPPY PFLOW
455 %token TAGGED TAG IFBOUND FLOATING STATEPOLICY STATEDEFAULTS ROUTE SETTOS
456 %token DIVERTTO DIVERTREPLY
457 %token <v.string> STRING
458 %token <v.number> NUMBER
459 %token <v.i> PORTBINARY
460 %type <v.interface> interface if_list if_item_not if_item
461 %type <v.number> number icmptype icmp6type uid gid
462 %type <v.number> tos not yesno
463 %type <v.probability> probability
464 %type <v.i> no dir af fragcache optimizer
465 %type <v.i> sourcetrack flush unaryop statelock
466 %type <v.b> action nataction natpasslog scrubaction
467 %type <v.b> flags flag blockspec
468 %type <v.range> portplain portstar portrange
469 %type <v.hashkey> hashkey
470 %type <v.proto> proto proto_list proto_item
471 %type <v.number> protoval
472 %type <v.icmp> icmpspec
473 %type <v.icmp> icmp_list icmp_item
474 %type <v.icmp> icmp6_list icmp6_item
475 %type <v.number> reticmpspec reticmp6spec
476 %type <v.fromto> fromto
477 %type <v.peer> ipportspec from to
478 %type <v.host> ipspec toipspec xhost host dynaddr host_list
479 %type <v.host> redir_host_list redirspec
480 %type <v.host> route_host route_host_list routespec
481 %type <v.os> os xos os_list
482 %type <v.port> portspec port_list port_item
483 %type <v.uid> uids uid_list uid_item
484 %type <v.gid> gids gid_list gid_item
485 %type <v.route> route
486 %type <v.redirection> redirection redirpool
487 %type <v.string> label stringall tag anchorname
488 %type <v.string> string varstring numberstring
489 %type <v.keep_state> keep
490 %type <v.state_opt> state_opt_spec state_opt_list state_opt_item
491 %type <v.logquick> logquick quick log logopts logopt
492 %type <v.interface> antispoof_ifspc antispoof_iflst antispoof_if
493 %type <v.qassign> qname
494 %type <v.queue> qassign qassign_list qassign_item
495 %type <v.queue_options> scheduler
496 %type <v.number> cbqflags_list cbqflags_item
497 %type <v.number> priqflags_list priqflags_item
498 %type <v.hfsc_opts> hfscopts_list hfscopts_item hfsc_opts
499 %type <v.queue_bwspec> bandwidth
500 %type <v.filter_opts> filter_opts filter_opt filter_opts_l
501 %type <v.antispoof_opts> antispoof_opts antispoof_opt antispoof_opts_l
502 %type <v.queue_opts> queue_opts queue_opt queue_opts_l
503 %type <v.scrub_opts> scrub_opts scrub_opt scrub_opts_l
504 %type <v.table_opts> table_opts table_opt table_opts_l
505 %type <v.pool_opts> pool_opts pool_opt pool_opts_l
506 %type <v.tagged> tagged
507 %type <v.rtableid> rtable
510 ruleset : /* empty */
511 | ruleset include '\n'
513 | ruleset option '\n'
514 | ruleset scrubrule '\n'
515 | ruleset natrule '\n'
516 | ruleset binatrule '\n'
517 | ruleset pfrule '\n'
518 | ruleset anchorrule '\n'
519 | ruleset loadrule '\n'
520 | ruleset altqif '\n'
521 | ruleset queuespec '\n'
522 | ruleset varset '\n'
523 | ruleset antispoof '\n'
524 | ruleset tabledef '\n'
525 | '{' fakeanchor '}' '\n';
526 | ruleset error '\n' { file->errors++; }
529 include : INCLUDE STRING {
532 if ((nfile = pushfile($2, 0)) == NULL) {
533 yyerror("failed to include file %s", $2);
545 * apply to previouslys specified rule: must be careful to note
546 * what that is: pf or nat or binat or rdr
548 fakeanchor : fakeanchor '\n'
549 | fakeanchor anchorrule '\n'
550 | fakeanchor binatrule '\n'
551 | fakeanchor natrule '\n'
552 | fakeanchor pfrule '\n'
553 | fakeanchor error '\n'
557 if (!strcmp($1, "none"))
559 else if (!strcmp($1, "basic"))
560 $$ = PF_OPTIMIZE_BASIC;
561 else if (!strcmp($1, "profile"))
562 $$ = PF_OPTIMIZE_BASIC | PF_OPTIMIZE_PROFILE;
564 yyerror("unknown ruleset-optimization %s", $1);
570 option : SET OPTIMIZATION STRING {
571 if (check_rulestate(PFCTL_STATE_OPTION)) {
575 if (pfctl_set_optimization(pf, $3) != 0) {
576 yyerror("unknown optimization %s", $3);
582 | SET RULESET_OPTIMIZATION optimizer {
583 if (!(pf->opts & PF_OPT_OPTIMIZE)) {
584 pf->opts |= PF_OPT_OPTIMIZE;
588 | SET TIMEOUT timeout_spec
589 | SET TIMEOUT '{' optnl timeout_list '}'
590 | SET LIMIT limit_spec
591 | SET LIMIT '{' optnl limit_list '}'
592 | SET LOGINTERFACE stringall {
593 if (check_rulestate(PFCTL_STATE_OPTION)) {
597 if (pfctl_set_logif(pf, $3) != 0) {
598 yyerror("error setting loginterface %s", $3);
604 | SET HOSTID number {
605 if ($3 == 0 || $3 > UINT_MAX) {
606 yyerror("hostid must be non-zero");
609 if (pfctl_set_hostid(pf, $3) != 0) {
610 yyerror("error setting hostid %08x", $3);
614 | SET BLOCKPOLICY DROP {
615 if (pf->opts & PF_OPT_VERBOSE)
616 printf("set block-policy drop\n");
617 if (check_rulestate(PFCTL_STATE_OPTION))
619 blockpolicy = PFRULE_DROP;
621 | SET BLOCKPOLICY RETURN {
622 if (pf->opts & PF_OPT_VERBOSE)
623 printf("set block-policy return\n");
624 if (check_rulestate(PFCTL_STATE_OPTION))
626 blockpolicy = PFRULE_RETURN;
628 | SET REQUIREORDER yesno {
629 if (pf->opts & PF_OPT_VERBOSE)
630 printf("set require-order %s\n",
631 $3 == 1 ? "yes" : "no");
634 | SET FINGERPRINTS STRING {
635 if (pf->opts & PF_OPT_VERBOSE)
636 printf("set fingerprints \"%s\"\n", $3);
637 if (check_rulestate(PFCTL_STATE_OPTION)) {
641 if (!pf->anchor->name[0]) {
642 if (pfctl_file_fingerprints(pf->dev,
644 yyerror("error loading "
645 "fingerprints %s", $3);
652 | SET STATEPOLICY statelock {
653 if (pf->opts & PF_OPT_VERBOSE)
656 printf("set state-policy floating\n");
659 printf("set state-policy if-bound\n");
662 default_statelock = $3;
665 if (check_rulestate(PFCTL_STATE_OPTION)) {
669 if (pfctl_set_debug(pf, $3) != 0) {
670 yyerror("error setting debuglevel %s", $3);
676 | SET SKIP interface {
677 if (expand_skip_interface($3) != 0) {
678 yyerror("error setting skip interface(s)");
682 | SET STATEDEFAULTS state_opt_list {
683 if (keep_state_defaults != NULL) {
684 yyerror("cannot redefine state-defaults");
687 keep_state_defaults = $3;
691 stringall : STRING { $$ = $1; }
693 if (($$ = strdup("all")) == NULL) {
694 err(1, "stringall: strdup");
699 string : STRING string {
700 if (asprintf(&$$, "%s %s", $1, $2) == -1)
701 err(1, "string: asprintf");
708 varstring : numberstring varstring {
709 if (asprintf(&$$, "%s %s", $1, $2) == -1)
710 err(1, "string: asprintf");
717 numberstring : NUMBER {
719 if (asprintf(&s, "%lld", (long long)$1) == -1) {
720 yyerror("string: asprintf");
728 varset : STRING '=' varstring {
729 if (pf->opts & PF_OPT_VERBOSE)
730 printf("%s = \"%s\"\n", $1, $3);
731 if (symset($1, $3, 0) == -1)
732 err(1, "cannot store variable %s", $1);
738 anchorname : STRING { $$ = $1; }
739 | /* empty */ { $$ = NULL; }
742 pfa_anchorlist : /* empty */
743 | pfa_anchorlist '\n'
744 | pfa_anchorlist pfrule '\n'
745 | pfa_anchorlist anchorrule '\n'
750 char ta[PF_ANCHOR_NAME_SIZE];
751 struct pf_ruleset *rs;
753 /* steping into a brace anchor */
758 /* create a holding ruleset in the root */
759 snprintf(ta, PF_ANCHOR_NAME_SIZE, "_%d", pf->bn);
760 rs = pf_find_or_create_ruleset(ta);
762 err(1, "pfa_anchor: pf_find_or_create_ruleset");
763 pf->astack[pf->asd] = rs->anchor;
764 pf->anchor = rs->anchor;
765 } '\n' pfa_anchorlist '}'
767 pf->alast = pf->anchor;
769 pf->anchor = pf->astack[pf->asd];
774 anchorrule : ANCHOR anchorname dir quick interface af proto fromto
775 filter_opts pfa_anchor
778 struct node_proto *proto;
780 if (check_rulestate(PFCTL_STATE_FILTER)) {
786 if ($2 && ($2[0] == '_' || strstr($2, "/_") != NULL)) {
788 yyerror("anchor names beginning with '_' "
789 "are reserved for internal use");
793 memset(&r, 0, sizeof(r));
794 if (pf->astack[pf->asd + 1]) {
795 /* move inline rules into relative location */
797 &pf->astack[pf->asd]->ruleset,
798 $2 ? $2 : pf->alast->name);
800 if (r.anchor == NULL)
801 err(1, "anchorrule: unable to "
804 if (pf->alast != r.anchor) {
805 if (r.anchor->match) {
806 yyerror("inline anchor '%s' "
811 mv_rules(&pf->alast->ruleset,
814 pf_remove_if_empty_ruleset(&pf->alast->ruleset);
815 pf->alast = r.anchor;
818 yyerror("anchors without explicit "
819 "rules must specify a name");
827 r.rtableid = $9.rtableid;
830 if (strlcpy(r.tagname, $9.tag,
831 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
832 yyerror("tag too long, max %u chars",
833 PF_TAG_NAME_SIZE - 1);
837 if (strlcpy(r.match_tagname, $9.match_tag,
838 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
839 yyerror("tag too long, max %u chars",
840 PF_TAG_NAME_SIZE - 1);
843 r.match_tag_not = $9.match_tag_not;
844 if (rule_label(&r, $9.label))
847 r.flags = $9.flags.b1;
848 r.flagset = $9.flags.b2;
849 if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
850 yyerror("flags always false");
853 if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
854 for (proto = $7; proto != NULL &&
855 proto->proto != IPPROTO_TCP;
858 if (proto == NULL && $7 != NULL) {
859 if ($9.flags.b1 || $9.flags.b2)
861 "flags only apply to tcp");
864 "OS fingerprinting only "
872 if ($9.keep.action) {
873 yyerror("cannot specify state handling "
879 if (strlcpy(r.match_tagname, $9.match_tag,
880 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
881 yyerror("tag too long, max %u chars",
882 PF_TAG_NAME_SIZE - 1);
885 r.match_tag_not = $9.match_tag_not;
887 decide_address_family($8.src.host, &r.af);
888 decide_address_family($8.dst.host, &r.af);
890 expand_rule(&r, $5, NULL, $7, $8.src_os,
891 $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
892 $9.uid, $9.gid, $9.icmpspec,
893 pf->astack[pf->asd + 1] ? pf->alast->name : $2);
895 pf->astack[pf->asd + 1] = NULL;
897 | NATANCHOR string interface af proto fromto rtable {
900 if (check_rulestate(PFCTL_STATE_NAT)) {
905 memset(&r, 0, sizeof(r));
910 decide_address_family($6.src.host, &r.af);
911 decide_address_family($6.dst.host, &r.af);
913 expand_rule(&r, $3, NULL, $5, $6.src_os,
914 $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
918 | RDRANCHOR string interface af proto fromto rtable {
921 if (check_rulestate(PFCTL_STATE_NAT)) {
926 memset(&r, 0, sizeof(r));
931 decide_address_family($6.src.host, &r.af);
932 decide_address_family($6.dst.host, &r.af);
934 if ($6.src.port != NULL) {
935 yyerror("source port parameter not supported"
939 if ($6.dst.port != NULL) {
940 if ($6.dst.port->next != NULL) {
941 yyerror("destination port list "
942 "expansion not supported in "
945 } else if ($6.dst.port->op != PF_OP_EQ) {
946 yyerror("destination port operators"
947 " not supported in rdr-anchor");
950 r.dst.port[0] = $6.dst.port->port[0];
951 r.dst.port[1] = $6.dst.port->port[1];
952 r.dst.port_op = $6.dst.port->op;
955 expand_rule(&r, $3, NULL, $5, $6.src_os,
956 $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
960 | BINATANCHOR string interface af proto fromto rtable {
963 if (check_rulestate(PFCTL_STATE_NAT)) {
968 memset(&r, 0, sizeof(r));
973 if ($5->next != NULL) {
974 yyerror("proto list expansion"
975 " not supported in binat-anchor");
982 if ($6.src.host != NULL || $6.src.port != NULL ||
983 $6.dst.host != NULL || $6.dst.port != NULL) {
984 yyerror("fromto parameter not supported"
989 decide_address_family($6.src.host, &r.af);
990 decide_address_family($6.dst.host, &r.af);
992 pfctl_add_rule(pf, &r, $2);
997 loadrule : LOAD ANCHOR string FROM string {
998 struct loadanchors *loadanchor;
1000 if (strlen(pf->anchor->name) + 1 +
1001 strlen($3) >= MAXPATHLEN) {
1002 yyerror("anchorname %s too long, max %u\n",
1003 $3, MAXPATHLEN - 1);
1007 loadanchor = calloc(1, sizeof(struct loadanchors));
1008 if (loadanchor == NULL)
1009 err(1, "loadrule: calloc");
1010 if ((loadanchor->anchorname = malloc(MAXPATHLEN)) ==
1012 err(1, "loadrule: malloc");
1013 if (pf->anchor->name[0])
1014 snprintf(loadanchor->anchorname, MAXPATHLEN,
1015 "%s/%s", pf->anchor->name, $3);
1017 strlcpy(loadanchor->anchorname, $3, MAXPATHLEN);
1018 if ((loadanchor->filename = strdup($5)) == NULL)
1019 err(1, "loadrule: strdup");
1021 TAILQ_INSERT_TAIL(&loadanchorshead, loadanchor,
1028 scrubaction : no SCRUB {
1037 scrubrule : scrubaction dir logquick interface af proto fromto scrub_opts
1041 if (check_rulestate(PFCTL_STATE_SCRUB))
1044 memset(&r, 0, sizeof(r));
1052 yyerror("scrub rules do not support 'quick'");
1058 r.rule_flag |= PFRULE_NODF;
1060 r.rule_flag |= PFRULE_RANDOMID;
1061 if ($8.reassemble_tcp) {
1062 if (r.direction != PF_INOUT) {
1063 yyerror("reassemble tcp rules can not "
1064 "specify direction");
1067 r.rule_flag |= PFRULE_REASSEMBLE_TCP;
1070 r.min_ttl = $8.minttl;
1072 r.max_mss = $8.maxmss;
1073 if ($8.marker & SOM_SETTOS) {
1074 r.rule_flag |= PFRULE_SET_TOS;
1075 r.set_tos = $8.settos;
1078 r.rule_flag |= $8.fragcache;
1080 if (strlcpy(r.match_tagname, $8.match_tag,
1081 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1082 yyerror("tag too long, max %u chars",
1083 PF_TAG_NAME_SIZE - 1);
1086 r.match_tag_not = $8.match_tag_not;
1087 r.rtableid = $8.rtableid;
1089 expand_rule(&r, $4, NULL, $6, $7.src_os,
1090 $7.src.host, $7.src.port, $7.dst.host, $7.dst.port,
1091 NULL, NULL, NULL, "");
1096 bzero(&scrub_opts, sizeof scrub_opts);
1097 scrub_opts.rtableid = -1;
1100 { $$ = scrub_opts; }
1102 bzero(&scrub_opts, sizeof scrub_opts);
1103 scrub_opts.rtableid = -1;
1108 scrub_opts_l : scrub_opts_l scrub_opt
1113 if (scrub_opts.nodf) {
1114 yyerror("no-df cannot be respecified");
1117 scrub_opts.nodf = 1;
1120 if (scrub_opts.marker & SOM_MINTTL) {
1121 yyerror("min-ttl cannot be respecified");
1124 if ($2 < 0 || $2 > 255) {
1125 yyerror("illegal min-ttl value %d", $2);
1128 scrub_opts.marker |= SOM_MINTTL;
1129 scrub_opts.minttl = $2;
1132 if (scrub_opts.marker & SOM_MAXMSS) {
1133 yyerror("max-mss cannot be respecified");
1136 if ($2 < 0 || $2 > 65535) {
1137 yyerror("illegal max-mss value %d", $2);
1140 scrub_opts.marker |= SOM_MAXMSS;
1141 scrub_opts.maxmss = $2;
1144 if (scrub_opts.marker & SOM_SETTOS) {
1145 yyerror("set-tos cannot be respecified");
1148 scrub_opts.marker |= SOM_SETTOS;
1149 scrub_opts.settos = $2;
1152 if (scrub_opts.marker & SOM_FRAGCACHE) {
1153 yyerror("fragcache cannot be respecified");
1156 scrub_opts.marker |= SOM_FRAGCACHE;
1157 scrub_opts.fragcache = $1;
1159 | REASSEMBLE STRING {
1160 if (strcasecmp($2, "tcp") != 0) {
1161 yyerror("scrub reassemble supports only tcp, "
1167 if (scrub_opts.reassemble_tcp) {
1168 yyerror("reassemble tcp cannot be respecified");
1171 scrub_opts.reassemble_tcp = 1;
1174 if (scrub_opts.randomid) {
1175 yyerror("random-id cannot be respecified");
1178 scrub_opts.randomid = 1;
1181 if ($2 < 0 || $2 > rt_tableid_max()) {
1182 yyerror("invalid rtable id");
1185 scrub_opts.rtableid = $2;
1187 | not TAGGED string {
1188 scrub_opts.match_tag = $3;
1189 scrub_opts.match_tag_not = $1;
1193 fragcache : FRAGMENT REASSEMBLE { $$ = 0; /* default */ }
1194 | FRAGMENT FRAGCROP { $$ = PFRULE_FRAGCROP; }
1195 | FRAGMENT FRAGDROP { $$ = PFRULE_FRAGDROP; }
1198 antispoof : ANTISPOOF logquick antispoof_ifspc af antispoof_opts {
1200 struct node_host *h = NULL, *hh;
1201 struct node_if *i, *j;
1203 if (check_rulestate(PFCTL_STATE_FILTER))
1206 for (i = $3; i; i = i->next) {
1207 bzero(&r, sizeof(r));
1210 r.direction = PF_IN;
1215 if (rule_label(&r, $5.label))
1217 r.rtableid = $5.rtableid;
1218 j = calloc(1, sizeof(struct node_if));
1220 err(1, "antispoof: calloc");
1221 if (strlcpy(j->ifname, i->ifname,
1222 sizeof(j->ifname)) >= sizeof(j->ifname)) {
1224 yyerror("interface name too long");
1229 h = calloc(1, sizeof(*h));
1231 err(1, "address: calloc");
1232 h->addr.type = PF_ADDR_DYNIFTL;
1234 if (strlcpy(h->addr.v.ifname, i->ifname,
1235 sizeof(h->addr.v.ifname)) >=
1236 sizeof(h->addr.v.ifname)) {
1239 "interface name too long");
1242 hh = malloc(sizeof(*hh));
1244 err(1, "address: malloc");
1245 bcopy(h, hh, sizeof(*hh));
1246 h->addr.iflags = PFI_AFLAG_NETWORK;
1248 h = ifa_lookup(j->ifname,
1254 expand_rule(&r, j, NULL, NULL, NULL, h,
1255 NULL, NULL, NULL, NULL, NULL,
1258 if ((i->ifa_flags & IFF_LOOPBACK) == 0) {
1259 bzero(&r, sizeof(r));
1262 r.direction = PF_IN;
1267 if (rule_label(&r, $5.label))
1269 r.rtableid = $5.rtableid;
1273 h = ifa_lookup(i->ifname, 0);
1275 expand_rule(&r, NULL, NULL,
1276 NULL, NULL, h, NULL, NULL,
1277 NULL, NULL, NULL, NULL, "");
1285 antispoof_ifspc : FOR antispoof_if { $$ = $2; }
1286 | FOR '{' optnl antispoof_iflst '}' { $$ = $4; }
1289 antispoof_iflst : antispoof_if optnl { $$ = $1; }
1290 | antispoof_iflst comma antispoof_if optnl {
1291 $1->tail->next = $3;
1297 antispoof_if : if_item { $$ = $1; }
1305 bzero(&antispoof_opts, sizeof antispoof_opts);
1306 antispoof_opts.rtableid = -1;
1309 { $$ = antispoof_opts; }
1311 bzero(&antispoof_opts, sizeof antispoof_opts);
1312 antispoof_opts.rtableid = -1;
1313 $$ = antispoof_opts;
1317 antispoof_opts_l : antispoof_opts_l antispoof_opt
1321 antispoof_opt : label {
1322 if (antispoof_opts.label) {
1323 yyerror("label cannot be redefined");
1326 antispoof_opts.label = $1;
1329 if ($2 < 0 || $2 > rt_tableid_max()) {
1330 yyerror("invalid rtable id");
1333 antispoof_opts.rtableid = $2;
1337 not : '!' { $$ = 1; }
1338 | /* empty */ { $$ = 0; }
1341 tabledef : TABLE '<' STRING '>' table_opts {
1342 struct node_host *h, *nh;
1343 struct node_tinit *ti, *nti;
1345 if (strlen($3) >= PF_TABLE_NAME_SIZE) {
1346 yyerror("table name too long, max %d chars",
1347 PF_TABLE_NAME_SIZE - 1);
1351 if (pf->loadopt & PFCTL_FLAG_TABLE)
1352 if (process_tabledef($3, &$5)) {
1357 for (ti = SIMPLEQ_FIRST(&$5.init_nodes);
1358 ti != SIMPLEQ_END(&$5.init_nodes); ti = nti) {
1361 for (h = ti->host; h != NULL; h = nh) {
1365 nti = SIMPLEQ_NEXT(ti, entries);
1372 bzero(&table_opts, sizeof table_opts);
1373 SIMPLEQ_INIT(&table_opts.init_nodes);
1376 { $$ = table_opts; }
1379 bzero(&table_opts, sizeof table_opts);
1380 SIMPLEQ_INIT(&table_opts.init_nodes);
1385 table_opts_l : table_opts_l table_opt
1389 table_opt : STRING {
1390 if (!strcmp($1, "const"))
1391 table_opts.flags |= PFR_TFLAG_CONST;
1392 else if (!strcmp($1, "persist"))
1393 table_opts.flags |= PFR_TFLAG_PERSIST;
1394 else if (!strcmp($1, "counters"))
1395 table_opts.flags |= PFR_TFLAG_COUNTERS;
1397 yyerror("invalid table option '%s'", $1);
1403 | '{' optnl '}' { table_opts.init_addr = 1; }
1404 | '{' optnl host_list '}' {
1405 struct node_host *n;
1406 struct node_tinit *ti;
1408 for (n = $3; n != NULL; n = n->next) {
1409 switch (n->addr.type) {
1410 case PF_ADDR_ADDRMASK:
1413 yyerror("address ranges are not "
1414 "permitted inside tables");
1416 case PF_ADDR_DYNIFTL:
1417 yyerror("dynamic addresses are not "
1418 "permitted inside tables");
1421 yyerror("tables cannot contain tables");
1423 case PF_ADDR_NOROUTE:
1424 yyerror("\"no-route\" is not permitted "
1427 case PF_ADDR_URPFFAILED:
1428 yyerror("\"urpf-failed\" is not "
1429 "permitted inside tables");
1432 yyerror("unknown address type %d",
1437 if (!(ti = calloc(1, sizeof(*ti))))
1438 err(1, "table_opt: calloc");
1440 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1442 table_opts.init_addr = 1;
1445 struct node_tinit *ti;
1447 if (!(ti = calloc(1, sizeof(*ti))))
1448 err(1, "table_opt: calloc");
1450 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1452 table_opts.init_addr = 1;
1456 altqif : ALTQ interface queue_opts QUEUE qassign {
1459 if (check_rulestate(PFCTL_STATE_QUEUE))
1462 memset(&a, 0, sizeof(a));
1463 if ($3.scheduler.qtype == ALTQT_NONE) {
1464 yyerror("no scheduler specified!");
1467 a.scheduler = $3.scheduler.qtype;
1468 a.qlimit = $3.qlimit;
1469 a.tbrsize = $3.tbrsize;
1471 yyerror("no child queues specified");
1474 if (expand_altq(&a, $2, $5, $3.queue_bwspec,
1480 queuespec : QUEUE STRING interface queue_opts qassign {
1483 if (check_rulestate(PFCTL_STATE_QUEUE)) {
1488 memset(&a, 0, sizeof(a));
1490 if (strlcpy(a.qname, $2, sizeof(a.qname)) >=
1492 yyerror("queue name too long (max "
1493 "%d chars)", PF_QNAME_SIZE-1);
1499 yyerror("cannot specify tbrsize for queue");
1502 if ($4.priority > 255) {
1503 yyerror("priority out of range: max 255");
1506 a.priority = $4.priority;
1507 a.qlimit = $4.qlimit;
1508 a.scheduler = $4.scheduler.qtype;
1509 if (expand_queue(&a, $3, $5, $4.queue_bwspec,
1511 yyerror("errors in queue definition");
1518 bzero(&queue_opts, sizeof queue_opts);
1519 queue_opts.priority = DEFAULT_PRIORITY;
1520 queue_opts.qlimit = DEFAULT_QLIMIT;
1521 queue_opts.scheduler.qtype = ALTQT_NONE;
1522 queue_opts.queue_bwspec.bw_percent = 100;
1525 { $$ = queue_opts; }
1527 bzero(&queue_opts, sizeof queue_opts);
1528 queue_opts.priority = DEFAULT_PRIORITY;
1529 queue_opts.qlimit = DEFAULT_QLIMIT;
1530 queue_opts.scheduler.qtype = ALTQT_NONE;
1531 queue_opts.queue_bwspec.bw_percent = 100;
1536 queue_opts_l : queue_opts_l queue_opt
1540 queue_opt : BANDWIDTH bandwidth {
1541 if (queue_opts.marker & QOM_BWSPEC) {
1542 yyerror("bandwidth cannot be respecified");
1545 queue_opts.marker |= QOM_BWSPEC;
1546 queue_opts.queue_bwspec = $2;
1549 if (queue_opts.marker & QOM_PRIORITY) {
1550 yyerror("priority cannot be respecified");
1553 if ($2 < 0 || $2 > 255) {
1554 yyerror("priority out of range: max 255");
1557 queue_opts.marker |= QOM_PRIORITY;
1558 queue_opts.priority = $2;
1561 if (queue_opts.marker & QOM_QLIMIT) {
1562 yyerror("qlimit cannot be respecified");
1565 if ($2 < 0 || $2 > 65535) {
1566 yyerror("qlimit out of range: max 65535");
1569 queue_opts.marker |= QOM_QLIMIT;
1570 queue_opts.qlimit = $2;
1573 if (queue_opts.marker & QOM_SCHEDULER) {
1574 yyerror("scheduler cannot be respecified");
1577 queue_opts.marker |= QOM_SCHEDULER;
1578 queue_opts.scheduler = $1;
1581 if (queue_opts.marker & QOM_TBRSIZE) {
1582 yyerror("tbrsize cannot be respecified");
1585 if ($2 < 0 || $2 > 65535) {
1586 yyerror("tbrsize too big: max 65535");
1589 queue_opts.marker |= QOM_TBRSIZE;
1590 queue_opts.tbrsize = $2;
1594 bandwidth : STRING {
1600 bps = strtod($1, &cp);
1602 if (!strcmp(cp, "b"))
1604 else if (!strcmp(cp, "Kb"))
1606 else if (!strcmp(cp, "Mb"))
1608 else if (!strcmp(cp, "Gb"))
1609 bps *= 1000 * 1000 * 1000;
1610 else if (!strcmp(cp, "%")) {
1611 if (bps < 0 || bps > 100) {
1612 yyerror("bandwidth spec "
1617 $$.bw_percent = bps;
1620 yyerror("unknown unit %s", cp);
1626 $$.bw_absolute = (u_int32_t)bps;
1629 if ($1 < 0 || $1 > UINT_MAX) {
1630 yyerror("bandwidth number too big");
1634 $$.bw_absolute = $1;
1639 $$.qtype = ALTQT_CBQ;
1640 $$.data.cbq_opts.flags = 0;
1642 | CBQ '(' cbqflags_list ')' {
1643 $$.qtype = ALTQT_CBQ;
1644 $$.data.cbq_opts.flags = $3;
1647 $$.qtype = ALTQT_PRIQ;
1648 $$.data.priq_opts.flags = 0;
1650 | PRIQ '(' priqflags_list ')' {
1651 $$.qtype = ALTQT_PRIQ;
1652 $$.data.priq_opts.flags = $3;
1655 $$.qtype = ALTQT_HFSC;
1656 bzero(&$$.data.hfsc_opts,
1657 sizeof(struct node_hfsc_opts));
1659 | HFSC '(' hfsc_opts ')' {
1660 $$.qtype = ALTQT_HFSC;
1661 $$.data.hfsc_opts = $3;
1665 cbqflags_list : cbqflags_item { $$ |= $1; }
1666 | cbqflags_list comma cbqflags_item { $$ |= $3; }
1669 cbqflags_item : STRING {
1670 if (!strcmp($1, "default"))
1671 $$ = CBQCLF_DEFCLASS;
1672 else if (!strcmp($1, "borrow"))
1674 else if (!strcmp($1, "red"))
1676 else if (!strcmp($1, "ecn"))
1677 $$ = CBQCLF_RED|CBQCLF_ECN;
1678 else if (!strcmp($1, "rio"))
1681 yyerror("unknown cbq flag \"%s\"", $1);
1689 priqflags_list : priqflags_item { $$ |= $1; }
1690 | priqflags_list comma priqflags_item { $$ |= $3; }
1693 priqflags_item : STRING {
1694 if (!strcmp($1, "default"))
1695 $$ = PRCF_DEFAULTCLASS;
1696 else if (!strcmp($1, "red"))
1698 else if (!strcmp($1, "ecn"))
1699 $$ = PRCF_RED|PRCF_ECN;
1700 else if (!strcmp($1, "rio"))
1703 yyerror("unknown priq flag \"%s\"", $1);
1713 sizeof(struct node_hfsc_opts));
1720 hfscopts_list : hfscopts_item
1721 | hfscopts_list comma hfscopts_item
1724 hfscopts_item : LINKSHARE bandwidth {
1725 if (hfsc_opts.linkshare.used) {
1726 yyerror("linkshare already specified");
1729 hfsc_opts.linkshare.m2 = $2;
1730 hfsc_opts.linkshare.used = 1;
1732 | LINKSHARE '(' bandwidth comma NUMBER comma bandwidth ')'
1734 if ($5 < 0 || $5 > INT_MAX) {
1735 yyerror("timing in curve out of range");
1738 if (hfsc_opts.linkshare.used) {
1739 yyerror("linkshare already specified");
1742 hfsc_opts.linkshare.m1 = $3;
1743 hfsc_opts.linkshare.d = $5;
1744 hfsc_opts.linkshare.m2 = $7;
1745 hfsc_opts.linkshare.used = 1;
1747 | REALTIME bandwidth {
1748 if (hfsc_opts.realtime.used) {
1749 yyerror("realtime already specified");
1752 hfsc_opts.realtime.m2 = $2;
1753 hfsc_opts.realtime.used = 1;
1755 | REALTIME '(' bandwidth comma NUMBER comma bandwidth ')'
1757 if ($5 < 0 || $5 > INT_MAX) {
1758 yyerror("timing in curve out of range");
1761 if (hfsc_opts.realtime.used) {
1762 yyerror("realtime already specified");
1765 hfsc_opts.realtime.m1 = $3;
1766 hfsc_opts.realtime.d = $5;
1767 hfsc_opts.realtime.m2 = $7;
1768 hfsc_opts.realtime.used = 1;
1770 | UPPERLIMIT bandwidth {
1771 if (hfsc_opts.upperlimit.used) {
1772 yyerror("upperlimit already specified");
1775 hfsc_opts.upperlimit.m2 = $2;
1776 hfsc_opts.upperlimit.used = 1;
1778 | UPPERLIMIT '(' bandwidth comma NUMBER comma bandwidth ')'
1780 if ($5 < 0 || $5 > INT_MAX) {
1781 yyerror("timing in curve out of range");
1784 if (hfsc_opts.upperlimit.used) {
1785 yyerror("upperlimit already specified");
1788 hfsc_opts.upperlimit.m1 = $3;
1789 hfsc_opts.upperlimit.d = $5;
1790 hfsc_opts.upperlimit.m2 = $7;
1791 hfsc_opts.upperlimit.used = 1;
1794 if (!strcmp($1, "default"))
1795 hfsc_opts.flags |= HFCF_DEFAULTCLASS;
1796 else if (!strcmp($1, "red"))
1797 hfsc_opts.flags |= HFCF_RED;
1798 else if (!strcmp($1, "ecn"))
1799 hfsc_opts.flags |= HFCF_RED|HFCF_ECN;
1800 else if (!strcmp($1, "rio"))
1801 hfsc_opts.flags |= HFCF_RIO;
1803 yyerror("unknown hfsc flag \"%s\"", $1);
1811 qassign : /* empty */ { $$ = NULL; }
1812 | qassign_item { $$ = $1; }
1813 | '{' optnl qassign_list '}' { $$ = $3; }
1816 qassign_list : qassign_item optnl { $$ = $1; }
1817 | qassign_list comma qassign_item optnl {
1818 $1->tail->next = $3;
1824 qassign_item : STRING {
1825 $$ = calloc(1, sizeof(struct node_queue));
1827 err(1, "qassign_item: calloc");
1828 if (strlcpy($$->queue, $1, sizeof($$->queue)) >=
1829 sizeof($$->queue)) {
1830 yyerror("queue name '%s' too long (max "
1831 "%d chars)", $1, sizeof($$->queue)-1);
1842 pfrule : action dir logquick interface route af proto fromto
1846 struct node_state_opt *o;
1847 struct node_proto *proto;
1853 if (check_rulestate(PFCTL_STATE_FILTER))
1856 memset(&r, 0, sizeof(r));
1860 case PFRULE_RETURNRST:
1861 r.rule_flag |= PFRULE_RETURNRST;
1862 r.return_ttl = $1.w;
1864 case PFRULE_RETURNICMP:
1865 r.rule_flag |= PFRULE_RETURNICMP;
1866 r.return_icmp = $1.w;
1867 r.return_icmp6 = $1.w2;
1870 r.rule_flag |= PFRULE_RETURN;
1871 r.return_icmp = $1.w;
1872 r.return_icmp6 = $1.w2;
1880 r.rtableid = $9.rtableid;
1884 if (strlcpy(r.tagname, $9.tag,
1885 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1886 yyerror("tag too long, max %u chars",
1887 PF_TAG_NAME_SIZE - 1);
1891 if (strlcpy(r.match_tagname, $9.match_tag,
1892 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1893 yyerror("tag too long, max %u chars",
1894 PF_TAG_NAME_SIZE - 1);
1897 r.match_tag_not = $9.match_tag_not;
1898 if (rule_label(&r, $9.label))
1901 r.flags = $9.flags.b1;
1902 r.flagset = $9.flags.b2;
1903 if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
1904 yyerror("flags always false");
1907 if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
1908 for (proto = $7; proto != NULL &&
1909 proto->proto != IPPROTO_TCP;
1910 proto = proto->next)
1912 if (proto == NULL && $7 != NULL) {
1913 if ($9.flags.b1 || $9.flags.b2)
1915 "flags only apply to tcp");
1918 "OS fingerprinting only "
1923 if (($9.flags.b1 & parse_flags("S")) == 0 &&
1925 yyerror("OS fingerprinting requires "
1926 "the SYN TCP flag (flags S/SA)");
1933 r.keep_state = $9.keep.action;
1934 o = $9.keep.options;
1936 /* 'keep state' by default on pass rules. */
1937 if (!r.keep_state && !r.action &&
1938 !($9.marker & FOM_KEEP)) {
1939 r.keep_state = PF_STATE_NORMAL;
1940 o = keep_state_defaults;
1945 struct node_state_opt *p = o;
1948 case PF_STATE_OPT_MAX:
1950 yyerror("state option 'max' "
1951 "multiple definitions");
1954 r.max_states = o->data.max_states;
1956 case PF_STATE_OPT_NOSYNC:
1957 if (r.rule_flag & PFRULE_NOSYNC) {
1958 yyerror("state option 'sync' "
1959 "multiple definitions");
1962 r.rule_flag |= PFRULE_NOSYNC;
1964 case PF_STATE_OPT_SRCTRACK:
1966 yyerror("state option "
1968 "multiple definitions");
1971 srctrack = o->data.src_track;
1972 r.rule_flag |= PFRULE_SRCTRACK;
1974 case PF_STATE_OPT_MAX_SRC_STATES:
1975 if (r.max_src_states) {
1976 yyerror("state option "
1978 "multiple definitions");
1981 if (o->data.max_src_states == 0) {
1982 yyerror("'max-src-states' must "
1987 o->data.max_src_states;
1988 r.rule_flag |= PFRULE_SRCTRACK;
1990 case PF_STATE_OPT_OVERLOAD:
1991 if (r.overload_tblname[0]) {
1992 yyerror("multiple 'overload' "
1993 "table definitions");
1996 if (strlcpy(r.overload_tblname,
1997 o->data.overload.tblname,
1998 PF_TABLE_NAME_SIZE) >=
1999 PF_TABLE_NAME_SIZE) {
2000 yyerror("state option: "
2004 r.flush = o->data.overload.flush;
2006 case PF_STATE_OPT_MAX_SRC_CONN:
2007 if (r.max_src_conn) {
2008 yyerror("state option "
2010 "multiple definitions");
2013 if (o->data.max_src_conn == 0) {
2014 yyerror("'max-src-conn' "
2019 o->data.max_src_conn;
2020 r.rule_flag |= PFRULE_SRCTRACK |
2021 PFRULE_RULESRCTRACK;
2023 case PF_STATE_OPT_MAX_SRC_CONN_RATE:
2024 if (r.max_src_conn_rate.limit) {
2025 yyerror("state option "
2026 "'max-src-conn-rate' "
2027 "multiple definitions");
2030 if (!o->data.max_src_conn_rate.limit ||
2031 !o->data.max_src_conn_rate.seconds) {
2032 yyerror("'max-src-conn-rate' "
2033 "values must be > 0");
2036 if (o->data.max_src_conn_rate.limit >
2038 yyerror("'max-src-conn-rate' "
2039 "maximum rate must be < %u",
2043 r.max_src_conn_rate.limit =
2044 o->data.max_src_conn_rate.limit;
2045 r.max_src_conn_rate.seconds =
2046 o->data.max_src_conn_rate.seconds;
2047 r.rule_flag |= PFRULE_SRCTRACK |
2048 PFRULE_RULESRCTRACK;
2050 case PF_STATE_OPT_MAX_SRC_NODES:
2051 if (r.max_src_nodes) {
2052 yyerror("state option "
2054 "multiple definitions");
2057 if (o->data.max_src_nodes == 0) {
2058 yyerror("'max-src-nodes' must "
2063 o->data.max_src_nodes;
2064 r.rule_flag |= PFRULE_SRCTRACK |
2065 PFRULE_RULESRCTRACK;
2067 case PF_STATE_OPT_STATELOCK:
2069 yyerror("state locking option: "
2070 "multiple definitions");
2074 r.rule_flag |= o->data.statelock;
2076 case PF_STATE_OPT_SLOPPY:
2077 if (r.rule_flag & PFRULE_STATESLOPPY) {
2078 yyerror("state sloppy option: "
2079 "multiple definitions");
2082 r.rule_flag |= PFRULE_STATESLOPPY;
2084 case PF_STATE_OPT_PFLOW:
2085 if (r.rule_flag & PFRULE_PFLOW) {
2086 yyerror("state pflow "
2091 r.rule_flag |= PFRULE_PFLOW;
2093 case PF_STATE_OPT_TIMEOUT:
2094 if (o->data.timeout.number ==
2095 PFTM_ADAPTIVE_START ||
2096 o->data.timeout.number ==
2099 if (r.timeout[o->data.timeout.number]) {
2100 yyerror("state timeout %s "
2101 "multiple definitions",
2102 pf_timeouts[o->data.
2103 timeout.number].name);
2106 r.timeout[o->data.timeout.number] =
2107 o->data.timeout.seconds;
2114 /* 'flags S/SA' by default on stateful rules */
2115 if (!r.action && !r.flags && !r.flagset &&
2116 !$9.fragment && !($9.marker & FOM_FLAGS) &&
2118 r.flags = parse_flags("S");
2119 r.flagset = parse_flags("SA");
2121 if (!adaptive && r.max_states) {
2122 r.timeout[PFTM_ADAPTIVE_START] =
2123 (r.max_states / 10) * 6;
2124 r.timeout[PFTM_ADAPTIVE_END] =
2125 (r.max_states / 10) * 12;
2127 if (r.rule_flag & PFRULE_SRCTRACK) {
2128 if (srctrack == PF_SRCTRACK_GLOBAL &&
2130 yyerror("'max-src-nodes' is "
2131 "incompatible with "
2132 "'source-track global'");
2135 if (srctrack == PF_SRCTRACK_GLOBAL &&
2137 yyerror("'max-src-conn' is "
2138 "incompatible with "
2139 "'source-track global'");
2142 if (srctrack == PF_SRCTRACK_GLOBAL &&
2143 r.max_src_conn_rate.seconds) {
2144 yyerror("'max-src-conn-rate' is "
2145 "incompatible with "
2146 "'source-track global'");
2149 if (r.timeout[PFTM_SRC_NODE] <
2150 r.max_src_conn_rate.seconds)
2151 r.timeout[PFTM_SRC_NODE] =
2152 r.max_src_conn_rate.seconds;
2153 r.rule_flag |= PFRULE_SRCTRACK;
2154 if (srctrack == PF_SRCTRACK_RULE)
2155 r.rule_flag |= PFRULE_RULESRCTRACK;
2157 if (r.keep_state && !statelock)
2158 r.rule_flag |= default_statelock;
2161 r.rule_flag |= PFRULE_FRAGMENT;
2162 r.allow_opts = $9.allowopts;
2164 decide_address_family($8.src.host, &r.af);
2165 decide_address_family($8.dst.host, &r.af);
2169 yyerror("direction must be explicit "
2170 "with rules that specify routing");
2174 r.rpool.opts = $5.pool_opts;
2176 memcpy(&r.rpool.key, $5.key,
2177 sizeof(struct pf_poolhashkey));
2179 if (r.rt && r.rt != PF_FASTROUTE) {
2180 decide_address_family($5.host, &r.af);
2181 remove_invalid_hosts(&$5.host, &r.af);
2182 if ($5.host == NULL) {
2183 yyerror("no routing address with "
2184 "matching address family found.");
2187 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
2188 PF_POOL_NONE && ($5.host->next != NULL ||
2189 $5.host->addr.type == PF_ADDR_TABLE ||
2190 DYNIF_MULTIADDR($5.host->addr)))
2191 r.rpool.opts |= PF_POOL_ROUNDROBIN;
2192 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2193 PF_POOL_ROUNDROBIN &&
2194 disallow_table($5.host, "tables are only "
2195 "supported in round-robin routing pools"))
2197 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2198 PF_POOL_ROUNDROBIN &&
2199 disallow_alias($5.host, "interface (%s) "
2200 "is only supported in round-robin "
2203 if ($5.host->next != NULL) {
2204 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2205 PF_POOL_ROUNDROBIN) {
2206 yyerror("r.rpool.opts must "
2207 "be PF_POOL_ROUNDROBIN");
2212 if ($9.queues.qname != NULL) {
2213 if (strlcpy(r.qname, $9.queues.qname,
2214 sizeof(r.qname)) >= sizeof(r.qname)) {
2215 yyerror("rule qname too long (max "
2216 "%d chars)", sizeof(r.qname)-1);
2219 free($9.queues.qname);
2221 if ($9.queues.pqname != NULL) {
2222 if (strlcpy(r.pqname, $9.queues.pqname,
2223 sizeof(r.pqname)) >= sizeof(r.pqname)) {
2224 yyerror("rule pqname too long (max "
2225 "%d chars)", sizeof(r.pqname)-1);
2228 free($9.queues.pqname);
2231 r.divert.port = $9.divert.port;
2233 if ((r.divert.port = $9.divert.port)) {
2234 if (r.direction == PF_OUT) {
2235 if ($9.divert.addr) {
2236 yyerror("address specified "
2237 "for outgoing divert");
2240 bzero(&r.divert.addr,
2241 sizeof(r.divert.addr));
2243 if (!$9.divert.addr) {
2244 yyerror("no address specified "
2245 "for incoming divert");
2248 if ($9.divert.addr->af != r.af) {
2249 yyerror("address family "
2250 "mismatch for divert");
2254 $9.divert.addr->addr.v.a.addr;
2259 expand_rule(&r, $4, $5.host, $7, $8.src_os,
2260 $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
2261 $9.uid, $9.gid, $9.icmpspec, "");
2266 bzero(&filter_opts, sizeof filter_opts);
2267 filter_opts.rtableid = -1;
2270 { $$ = filter_opts; }
2272 bzero(&filter_opts, sizeof filter_opts);
2273 filter_opts.rtableid = -1;
2278 filter_opts_l : filter_opts_l filter_opt
2282 filter_opt : USER uids {
2283 if (filter_opts.uid)
2284 $2->tail->next = filter_opts.uid;
2285 filter_opts.uid = $2;
2288 if (filter_opts.gid)
2289 $2->tail->next = filter_opts.gid;
2290 filter_opts.gid = $2;
2293 if (filter_opts.marker & FOM_FLAGS) {
2294 yyerror("flags cannot be redefined");
2297 filter_opts.marker |= FOM_FLAGS;
2298 filter_opts.flags.b1 |= $1.b1;
2299 filter_opts.flags.b2 |= $1.b2;
2300 filter_opts.flags.w |= $1.w;
2301 filter_opts.flags.w2 |= $1.w2;
2304 if (filter_opts.marker & FOM_ICMP) {
2305 yyerror("icmp-type cannot be redefined");
2308 filter_opts.marker |= FOM_ICMP;
2309 filter_opts.icmpspec = $1;
2312 if (filter_opts.marker & FOM_TOS) {
2313 yyerror("tos cannot be redefined");
2316 filter_opts.marker |= FOM_TOS;
2317 filter_opts.tos = $2;
2320 if (filter_opts.marker & FOM_KEEP) {
2321 yyerror("modulate or keep cannot be redefined");
2324 filter_opts.marker |= FOM_KEEP;
2325 filter_opts.keep.action = $1.action;
2326 filter_opts.keep.options = $1.options;
2329 filter_opts.fragment = 1;
2332 filter_opts.allowopts = 1;
2335 if (filter_opts.label) {
2336 yyerror("label cannot be redefined");
2339 filter_opts.label = $1;
2342 if (filter_opts.queues.qname) {
2343 yyerror("queue cannot be redefined");
2346 filter_opts.queues = $1;
2349 filter_opts.tag = $2;
2351 | not TAGGED string {
2352 filter_opts.match_tag = $3;
2353 filter_opts.match_tag_not = $1;
2355 | PROBABILITY probability {
2358 p = floor($2 * UINT_MAX + 0.5);
2359 if (p < 0.0 || p > UINT_MAX) {
2360 yyerror("invalid probability: %lf", p);
2363 filter_opts.prob = (u_int32_t)p;
2364 if (filter_opts.prob == 0)
2365 filter_opts.prob = 1;
2368 if ($2 < 0 || $2 > rt_tableid_max()) {
2369 yyerror("invalid rtable id");
2372 filter_opts.rtableid = $2;
2374 | DIVERTTO portplain {
2376 filter_opts.divert.port = $2.a;
2377 if (!filter_opts.divert.port) {
2378 yyerror("invalid divert port: %u", ntohs($2.a));
2383 | DIVERTTO STRING PORT portplain {
2385 if ((filter_opts.divert.addr = host($2)) == NULL) {
2386 yyerror("could not parse divert address: %s",
2395 filter_opts.divert.port = $4.a;
2396 if (!filter_opts.divert.port) {
2397 yyerror("invalid divert port: %u", ntohs($4.a));
2403 yyerror("divert-reply has no meaning in FreeBSD pf(4)");
2406 filter_opts.divert.port = 1; /* some random value */
2411 probability : STRING {
2413 double p = strtod($1, &e);
2420 yyerror("invalid probability: %s", $1);
2433 action : PASS { $$.b1 = PF_PASS; $$.b2 = $$.w = 0; }
2434 | BLOCK blockspec { $$ = $2; $$.b1 = PF_DROP; }
2437 blockspec : /* empty */ {
2438 $$.b2 = blockpolicy;
2439 $$.w = returnicmpdefault;
2440 $$.w2 = returnicmp6default;
2443 $$.b2 = PFRULE_DROP;
2448 $$.b2 = PFRULE_RETURNRST;
2452 | RETURNRST '(' TTL NUMBER ')' {
2453 if ($4 < 0 || $4 > 255) {
2454 yyerror("illegal ttl value %d", $4);
2457 $$.b2 = PFRULE_RETURNRST;
2462 $$.b2 = PFRULE_RETURNICMP;
2463 $$.w = returnicmpdefault;
2464 $$.w2 = returnicmp6default;
2467 $$.b2 = PFRULE_RETURNICMP;
2468 $$.w = returnicmpdefault;
2469 $$.w2 = returnicmp6default;
2471 | RETURNICMP '(' reticmpspec ')' {
2472 $$.b2 = PFRULE_RETURNICMP;
2474 $$.w2 = returnicmpdefault;
2476 | RETURNICMP6 '(' reticmp6spec ')' {
2477 $$.b2 = PFRULE_RETURNICMP;
2478 $$.w = returnicmpdefault;
2481 | RETURNICMP '(' reticmpspec comma reticmp6spec ')' {
2482 $$.b2 = PFRULE_RETURNICMP;
2487 $$.b2 = PFRULE_RETURN;
2488 $$.w = returnicmpdefault;
2489 $$.w2 = returnicmp6default;
2493 reticmpspec : STRING {
2494 if (!($$ = parseicmpspec($1, AF_INET))) {
2503 if ($1 < 0 || $1 > 255) {
2504 yyerror("invalid icmp code %lu", $1);
2507 icmptype = returnicmpdefault >> 8;
2508 $$ = (icmptype << 8 | $1);
2512 reticmp6spec : STRING {
2513 if (!($$ = parseicmpspec($1, AF_INET6))) {
2522 if ($1 < 0 || $1 > 255) {
2523 yyerror("invalid icmp code %lu", $1);
2526 icmptype = returnicmp6default >> 8;
2527 $$ = (icmptype << 8 | $1);
2531 dir : /* empty */ { $$ = PF_INOUT; }
2532 | IN { $$ = PF_IN; }
2533 | OUT { $$ = PF_OUT; }
2536 quick : /* empty */ { $$.quick = 0; }
2537 | QUICK { $$.quick = 1; }
2540 logquick : /* empty */ { $$.log = 0; $$.quick = 0; $$.logif = 0; }
2541 | log { $$ = $1; $$.quick = 0; }
2542 | QUICK { $$.quick = 1; $$.log = 0; $$.logif = 0; }
2543 | log QUICK { $$ = $1; $$.quick = 1; }
2544 | QUICK log { $$ = $2; $$.quick = 1; }
2547 log : LOG { $$.log = PF_LOG; $$.logif = 0; }
2548 | LOG '(' logopts ')' {
2549 $$.log = PF_LOG | $3.log;
2550 $$.logif = $3.logif;
2554 logopts : logopt { $$ = $1; }
2555 | logopts comma logopt {
2556 $$.log = $1.log | $3.log;
2557 $$.logif = $3.logif;
2559 $$.logif = $1.logif;
2563 logopt : ALL { $$.log = PF_LOG_ALL; $$.logif = 0; }
2564 | USER { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
2565 | GROUP { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
2571 if (strncmp($2, "pflog", 5)) {
2572 yyerror("%s: should be a pflog interface", $2);
2576 i = strtonum($2 + 5, 0, 255, &errstr);
2578 yyerror("%s: %s", $2, errstr);
2587 interface : /* empty */ { $$ = NULL; }
2588 | ON if_item_not { $$ = $2; }
2589 | ON '{' optnl if_list '}' { $$ = $4; }
2592 if_list : if_item_not optnl { $$ = $1; }
2593 | if_list comma if_item_not optnl {
2594 $1->tail->next = $3;
2600 if_item_not : not if_item { $$ = $2; $$->not = $1; }
2604 struct node_host *n;
2606 $$ = calloc(1, sizeof(struct node_if));
2608 err(1, "if_item: calloc");
2609 if (strlcpy($$->ifname, $1, sizeof($$->ifname)) >=
2610 sizeof($$->ifname)) {
2613 yyerror("interface name too long");
2617 if ((n = ifa_exists($1)) != NULL)
2618 $$->ifa_flags = n->ifa_flags;
2627 af : /* empty */ { $$ = 0; }
2628 | INET { $$ = AF_INET; }
2629 | INET6 { $$ = AF_INET6; }
2632 proto : /* empty */ { $$ = NULL; }
2633 | PROTO proto_item { $$ = $2; }
2634 | PROTO '{' optnl proto_list '}' { $$ = $4; }
2637 proto_list : proto_item optnl { $$ = $1; }
2638 | proto_list comma proto_item optnl {
2639 $1->tail->next = $3;
2645 proto_item : protoval {
2650 yyerror("proto 0 cannot be used");
2653 $$ = calloc(1, sizeof(struct node_proto));
2655 err(1, "proto_item: calloc");
2665 p = getprotobyname($1);
2667 yyerror("unknown protocol %s", $1);
2675 if ($1 < 0 || $1 > 255) {
2676 yyerror("protocol outside range");
2696 os : /* empty */ { $$ = NULL; }
2697 | OS xos { $$ = $2; }
2698 | OS '{' optnl os_list '}' { $$ = $4; }
2702 $$ = calloc(1, sizeof(struct node_os));
2704 err(1, "os: calloc");
2710 os_list : xos optnl { $$ = $1; }
2711 | os_list comma xos optnl {
2712 $1->tail->next = $3;
2718 from : /* empty */ {
2732 if (disallow_urpf_failed($2.host, "\"urpf-failed\" is "
2733 "not permitted in a destination address"))
2739 ipportspec : ipspec {
2743 | ipspec PORT portspec {
2757 ipspec : ANY { $$ = NULL; }
2758 | xhost { $$ = $1; }
2759 | '{' optnl host_list '}' { $$ = $3; }
2762 toipspec : TO ipspec { $$ = $2; }
2763 | /* empty */ { $$ = NULL; }
2766 host_list : ipspec optnl { $$ = $1; }
2767 | host_list comma ipspec optnl {
2770 else if ($1 == NULL)
2773 $1->tail->next = $3;
2774 $1->tail = $3->tail;
2781 struct node_host *n;
2783 for (n = $2; n != NULL; n = n->next)
2788 $$ = calloc(1, sizeof(struct node_host));
2790 err(1, "xhost: calloc");
2791 $$->addr.type = PF_ADDR_NOROUTE;
2797 $$ = calloc(1, sizeof(struct node_host));
2799 err(1, "xhost: calloc");
2800 $$->addr.type = PF_ADDR_URPFFAILED;
2808 if (($$ = host($1)) == NULL) {
2809 /* error. "any" is handled elsewhere */
2811 yyerror("could not parse host specification");
2817 | STRING '-' STRING {
2818 struct node_host *b, *e;
2820 if ((b = host($1)) == NULL || (e = host($3)) == NULL) {
2823 yyerror("could not parse host specification");
2826 if (b->af != e->af ||
2827 b->addr.type != PF_ADDR_ADDRMASK ||
2828 e->addr.type != PF_ADDR_ADDRMASK ||
2829 unmask(&b->addr.v.a.mask, b->af) !=
2830 (b->af == AF_INET ? 32 : 128) ||
2831 unmask(&e->addr.v.a.mask, e->af) !=
2832 (e->af == AF_INET ? 32 : 128) ||
2833 b->next != NULL || b->not ||
2834 e->next != NULL || e->not) {
2839 yyerror("invalid address range");
2842 memcpy(&b->addr.v.a.mask, &e->addr.v.a.addr,
2843 sizeof(b->addr.v.a.mask));
2844 b->addr.type = PF_ADDR_RANGE;
2850 | STRING '/' NUMBER {
2853 if (asprintf(&buf, "%s/%lld", $1, (long long)$3) == -1)
2854 err(1, "host: asprintf");
2856 if (($$ = host(buf)) == NULL) {
2857 /* error. "any" is handled elsewhere */
2859 yyerror("could not parse host specification");
2864 | NUMBER '/' NUMBER {
2867 /* ie. for 10/8 parsing */
2869 if (asprintf(&buf, "%lld/%lld", (long long)$1, (long long)$3) == -1)
2871 if (asprintf(&buf, "%lld/%lld", $1, $3) == -1)
2873 err(1, "host: asprintf");
2874 if (($$ = host(buf)) == NULL) {
2875 /* error. "any" is handled elsewhere */
2877 yyerror("could not parse host specification");
2883 | dynaddr '/' NUMBER {
2884 struct node_host *n;
2886 if ($3 < 0 || $3 > 128) {
2887 yyerror("bit number too big");
2891 for (n = $1; n != NULL; n = n->next)
2895 if (strlen($2) >= PF_TABLE_NAME_SIZE) {
2896 yyerror("table name '%s' too long", $2);
2900 $$ = calloc(1, sizeof(struct node_host));
2902 err(1, "host: calloc");
2903 $$->addr.type = PF_ADDR_TABLE;
2904 if (strlcpy($$->addr.v.tblname, $2,
2905 sizeof($$->addr.v.tblname)) >=
2906 sizeof($$->addr.v.tblname))
2907 errx(1, "host: strlcpy");
2913 $$ = calloc(1, sizeof(struct node_host));
2916 err(1, "host: calloc");
2918 $$->addr.type = PF_ADDR_RTLABEL;
2919 if (strlcpy($$->addr.v.rtlabelname, $2,
2920 sizeof($$->addr.v.rtlabelname)) >=
2921 sizeof($$->addr.v.rtlabelname)) {
2922 yyerror("route label too long, max %u chars",
2923 sizeof($$->addr.v.rtlabelname) - 1);
2938 if (atoul($1, &ulval) == -1) {
2939 yyerror("%s is not a number", $1);
2948 dynaddr : '(' STRING ')' {
2953 if (!isalpha(op[0])) {
2954 yyerror("invalid interface name '%s'", op);
2958 while ((p = strrchr($2, ':')) != NULL) {
2959 if (!strcmp(p+1, "network"))
2960 flags |= PFI_AFLAG_NETWORK;
2961 else if (!strcmp(p+1, "broadcast"))
2962 flags |= PFI_AFLAG_BROADCAST;
2963 else if (!strcmp(p+1, "peer"))
2964 flags |= PFI_AFLAG_PEER;
2965 else if (!strcmp(p+1, "0"))
2966 flags |= PFI_AFLAG_NOALIAS;
2968 yyerror("interface %s has bad modifier",
2975 if (flags & (flags - 1) & PFI_AFLAG_MODEMASK) {
2977 yyerror("illegal combination of "
2978 "interface modifiers");
2981 $$ = calloc(1, sizeof(struct node_host));
2983 err(1, "address: calloc");
2985 set_ipmask($$, 128);
2986 $$->addr.type = PF_ADDR_DYNIFTL;
2987 $$->addr.iflags = flags;
2988 if (strlcpy($$->addr.v.ifname, $2,
2989 sizeof($$->addr.v.ifname)) >=
2990 sizeof($$->addr.v.ifname)) {
2993 yyerror("interface name too long");
3002 portspec : port_item { $$ = $1; }
3003 | '{' optnl port_list '}' { $$ = $3; }
3006 port_list : port_item optnl { $$ = $1; }
3007 | port_list comma port_item optnl {
3008 $1->tail->next = $3;
3014 port_item : portrange {
3015 $$ = calloc(1, sizeof(struct node_port));
3017 err(1, "port_item: calloc");
3027 | unaryop portrange {
3029 yyerror("':' cannot be used with an other "
3033 $$ = calloc(1, sizeof(struct node_port));
3035 err(1, "port_item: calloc");
3042 | portrange PORTBINARY portrange {
3044 yyerror("':' cannot be used with an other "
3048 $$ = calloc(1, sizeof(struct node_port));
3050 err(1, "port_item: calloc");
3059 portplain : numberstring {
3060 if (parseport($1, &$$, 0) == -1) {
3068 portrange : numberstring {
3069 if (parseport($1, &$$, PPORT_RANGE) == -1) {
3077 uids : uid_item { $$ = $1; }
3078 | '{' optnl uid_list '}' { $$ = $3; }
3081 uid_list : uid_item optnl { $$ = $1; }
3082 | uid_list comma uid_item optnl {
3083 $1->tail->next = $3;
3090 $$ = calloc(1, sizeof(struct node_uid));
3092 err(1, "uid_item: calloc");
3100 if ($2 == UID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
3101 yyerror("user unknown requires operator = or "
3105 $$ = calloc(1, sizeof(struct node_uid));
3107 err(1, "uid_item: calloc");
3114 | uid PORTBINARY uid {
3115 if ($1 == UID_MAX || $3 == UID_MAX) {
3116 yyerror("user unknown requires operator = or "
3120 $$ = calloc(1, sizeof(struct node_uid));
3122 err(1, "uid_item: calloc");
3132 if (!strcmp($1, "unknown"))
3137 if ((pw = getpwnam($1)) == NULL) {
3138 yyerror("unknown user %s", $1);
3147 if ($1 < 0 || $1 >= UID_MAX) {
3148 yyerror("illegal uid value %lu", $1);
3155 gids : gid_item { $$ = $1; }
3156 | '{' optnl gid_list '}' { $$ = $3; }
3159 gid_list : gid_item optnl { $$ = $1; }
3160 | gid_list comma gid_item optnl {
3161 $1->tail->next = $3;
3168 $$ = calloc(1, sizeof(struct node_gid));
3170 err(1, "gid_item: calloc");
3178 if ($2 == GID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
3179 yyerror("group unknown requires operator = or "
3183 $$ = calloc(1, sizeof(struct node_gid));
3185 err(1, "gid_item: calloc");
3192 | gid PORTBINARY gid {
3193 if ($1 == GID_MAX || $3 == GID_MAX) {
3194 yyerror("group unknown requires operator = or "
3198 $$ = calloc(1, sizeof(struct node_gid));
3200 err(1, "gid_item: calloc");
3210 if (!strcmp($1, "unknown"))
3215 if ((grp = getgrnam($1)) == NULL) {
3216 yyerror("unknown group %s", $1);
3225 if ($1 < 0 || $1 >= GID_MAX) {
3226 yyerror("illegal gid value %lu", $1);
3236 if ((f = parse_flags($1)) < 0) {
3237 yyerror("bad flags %s", $1);
3246 flags : FLAGS flag '/' flag { $$.b1 = $2.b1; $$.b2 = $4.b1; }
3247 | FLAGS '/' flag { $$.b1 = 0; $$.b2 = $3.b1; }
3248 | FLAGS ANY { $$.b1 = 0; $$.b2 = 0; }
3251 icmpspec : ICMPTYPE icmp_item { $$ = $2; }
3252 | ICMPTYPE '{' optnl icmp_list '}' { $$ = $4; }
3253 | ICMP6TYPE icmp6_item { $$ = $2; }
3254 | ICMP6TYPE '{' optnl icmp6_list '}' { $$ = $4; }
3257 icmp_list : icmp_item optnl { $$ = $1; }
3258 | icmp_list comma icmp_item optnl {
3259 $1->tail->next = $3;
3265 icmp6_list : icmp6_item optnl { $$ = $1; }
3266 | icmp6_list comma icmp6_item optnl {
3267 $1->tail->next = $3;
3273 icmp_item : icmptype {
3274 $$ = calloc(1, sizeof(struct node_icmp));
3276 err(1, "icmp_item: calloc");
3279 $$->proto = IPPROTO_ICMP;
3283 | icmptype CODE STRING {
3284 const struct icmpcodeent *p;
3286 if ((p = geticmpcodebyname($1-1, $3, AF_INET)) == NULL) {
3287 yyerror("unknown icmp-code %s", $3);
3293 $$ = calloc(1, sizeof(struct node_icmp));
3295 err(1, "icmp_item: calloc");
3297 $$->code = p->code + 1;
3298 $$->proto = IPPROTO_ICMP;
3302 | icmptype CODE NUMBER {
3303 if ($3 < 0 || $3 > 255) {
3304 yyerror("illegal icmp-code %lu", $3);
3307 $$ = calloc(1, sizeof(struct node_icmp));
3309 err(1, "icmp_item: calloc");
3312 $$->proto = IPPROTO_ICMP;
3318 icmp6_item : icmp6type {
3319 $$ = calloc(1, sizeof(struct node_icmp));
3321 err(1, "icmp_item: calloc");
3324 $$->proto = IPPROTO_ICMPV6;
3328 | icmp6type CODE STRING {
3329 const struct icmpcodeent *p;
3331 if ((p = geticmpcodebyname($1-1, $3, AF_INET6)) == NULL) {
3332 yyerror("unknown icmp6-code %s", $3);
3338 $$ = calloc(1, sizeof(struct node_icmp));
3340 err(1, "icmp_item: calloc");
3342 $$->code = p->code + 1;
3343 $$->proto = IPPROTO_ICMPV6;
3347 | icmp6type CODE NUMBER {
3348 if ($3 < 0 || $3 > 255) {
3349 yyerror("illegal icmp-code %lu", $3);
3352 $$ = calloc(1, sizeof(struct node_icmp));
3354 err(1, "icmp_item: calloc");
3357 $$->proto = IPPROTO_ICMPV6;
3364 const struct icmptypeent *p;
3366 if ((p = geticmptypebyname($1, AF_INET)) == NULL) {
3367 yyerror("unknown icmp-type %s", $1);
3375 if ($1 < 0 || $1 > 255) {
3376 yyerror("illegal icmp-type %lu", $1);
3383 icmp6type : STRING {
3384 const struct icmptypeent *p;
3386 if ((p = geticmptypebyname($1, AF_INET6)) ==
3388 yyerror("unknown icmp6-type %s", $1);
3396 if ($1 < 0 || $1 > 255) {
3397 yyerror("illegal icmp6-type %lu", $1);
3405 if (!strcmp($1, "lowdelay"))
3406 $$ = IPTOS_LOWDELAY;
3407 else if (!strcmp($1, "throughput"))
3408 $$ = IPTOS_THROUGHPUT;
3409 else if (!strcmp($1, "reliability"))
3410 $$ = IPTOS_RELIABILITY;
3411 else if ($1[0] == '0' && $1[1] == 'x')
3412 $$ = strtoul($1, NULL, 16);
3414 $$ = 0; /* flag bad argument */
3415 if (!$$ || $$ > 255) {
3416 yyerror("illegal tos value %s", $1);
3424 if (!$$ || $$ > 255) {
3425 yyerror("illegal tos value %s", $1);
3431 sourcetrack : SOURCETRACK { $$ = PF_SRCTRACK; }
3432 | SOURCETRACK GLOBAL { $$ = PF_SRCTRACK_GLOBAL; }
3433 | SOURCETRACK RULE { $$ = PF_SRCTRACK_RULE; }
3436 statelock : IFBOUND {
3437 $$ = PFRULE_IFBOUND;
3448 | KEEP STATE state_opt_spec {
3449 $$.action = PF_STATE_NORMAL;
3452 | MODULATE STATE state_opt_spec {
3453 $$.action = PF_STATE_MODULATE;
3456 | SYNPROXY STATE state_opt_spec {
3457 $$.action = PF_STATE_SYNPROXY;
3462 flush : /* empty */ { $$ = 0; }
3463 | FLUSH { $$ = PF_FLUSH; }
3465 $$ = PF_FLUSH | PF_FLUSH_GLOBAL;
3469 state_opt_spec : '(' state_opt_list ')' { $$ = $2; }
3470 | /* empty */ { $$ = NULL; }
3473 state_opt_list : state_opt_item { $$ = $1; }
3474 | state_opt_list comma state_opt_item {
3475 $1->tail->next = $3;
3481 state_opt_item : MAXIMUM NUMBER {
3482 if ($2 < 0 || $2 > UINT_MAX) {
3483 yyerror("only positive values permitted");
3486 $$ = calloc(1, sizeof(struct node_state_opt));
3488 err(1, "state_opt_item: calloc");
3489 $$->type = PF_STATE_OPT_MAX;
3490 $$->data.max_states = $2;
3495 $$ = calloc(1, sizeof(struct node_state_opt));
3497 err(1, "state_opt_item: calloc");
3498 $$->type = PF_STATE_OPT_NOSYNC;
3502 | MAXSRCSTATES NUMBER {
3503 if ($2 < 0 || $2 > UINT_MAX) {
3504 yyerror("only positive values permitted");
3507 $$ = calloc(1, sizeof(struct node_state_opt));
3509 err(1, "state_opt_item: calloc");
3510 $$->type = PF_STATE_OPT_MAX_SRC_STATES;
3511 $$->data.max_src_states = $2;
3515 | MAXSRCCONN NUMBER {
3516 if ($2 < 0 || $2 > UINT_MAX) {
3517 yyerror("only positive values permitted");
3520 $$ = calloc(1, sizeof(struct node_state_opt));
3522 err(1, "state_opt_item: calloc");
3523 $$->type = PF_STATE_OPT_MAX_SRC_CONN;
3524 $$->data.max_src_conn = $2;
3528 | MAXSRCCONNRATE NUMBER '/' NUMBER {
3529 if ($2 < 0 || $2 > UINT_MAX ||
3530 $4 < 0 || $4 > UINT_MAX) {
3531 yyerror("only positive values permitted");
3534 $$ = calloc(1, sizeof(struct node_state_opt));
3536 err(1, "state_opt_item: calloc");
3537 $$->type = PF_STATE_OPT_MAX_SRC_CONN_RATE;
3538 $$->data.max_src_conn_rate.limit = $2;
3539 $$->data.max_src_conn_rate.seconds = $4;
3543 | OVERLOAD '<' STRING '>' flush {
3544 if (strlen($3) >= PF_TABLE_NAME_SIZE) {
3545 yyerror("table name '%s' too long", $3);
3549 $$ = calloc(1, sizeof(struct node_state_opt));
3551 err(1, "state_opt_item: calloc");
3552 if (strlcpy($$->data.overload.tblname, $3,
3553 PF_TABLE_NAME_SIZE) >= PF_TABLE_NAME_SIZE)
3554 errx(1, "state_opt_item: strlcpy");
3556 $$->type = PF_STATE_OPT_OVERLOAD;
3557 $$->data.overload.flush = $5;
3561 | MAXSRCNODES NUMBER {
3562 if ($2 < 0 || $2 > UINT_MAX) {
3563 yyerror("only positive values permitted");
3566 $$ = calloc(1, sizeof(struct node_state_opt));
3568 err(1, "state_opt_item: calloc");
3569 $$->type = PF_STATE_OPT_MAX_SRC_NODES;
3570 $$->data.max_src_nodes = $2;
3575 $$ = calloc(1, sizeof(struct node_state_opt));
3577 err(1, "state_opt_item: calloc");
3578 $$->type = PF_STATE_OPT_SRCTRACK;
3579 $$->data.src_track = $1;
3584 $$ = calloc(1, sizeof(struct node_state_opt));
3586 err(1, "state_opt_item: calloc");
3587 $$->type = PF_STATE_OPT_STATELOCK;
3588 $$->data.statelock = $1;
3593 $$ = calloc(1, sizeof(struct node_state_opt));
3595 err(1, "state_opt_item: calloc");
3596 $$->type = PF_STATE_OPT_SLOPPY;
3601 $$ = calloc(1, sizeof(struct node_state_opt));
3603 err(1, "state_opt_item: calloc");
3604 $$->type = PF_STATE_OPT_PFLOW;
3611 if ($2 < 0 || $2 > UINT_MAX) {
3612 yyerror("only positive values permitted");
3615 for (i = 0; pf_timeouts[i].name &&
3616 strcmp(pf_timeouts[i].name, $1); ++i)
3618 if (!pf_timeouts[i].name) {
3619 yyerror("illegal timeout name %s", $1);
3623 if (strchr(pf_timeouts[i].name, '.') == NULL) {
3624 yyerror("illegal state timeout %s", $1);
3629 $$ = calloc(1, sizeof(struct node_state_opt));
3631 err(1, "state_opt_item: calloc");
3632 $$->type = PF_STATE_OPT_TIMEOUT;
3633 $$->data.timeout.number = pf_timeouts[i].timeout;
3634 $$->data.timeout.seconds = $2;
3640 label : LABEL STRING {
3645 qname : QUEUE STRING {
3649 | QUEUE '(' STRING ')' {
3653 | QUEUE '(' STRING comma STRING ')' {
3659 no : /* empty */ { $$ = 0; }
3663 portstar : numberstring {
3664 if (parseport($1, &$$, PPORT_RANGE|PPORT_STAR) == -1) {
3672 redirspec : host { $$ = $1; }
3673 | '{' optnl redir_host_list '}' { $$ = $3; }
3676 redir_host_list : host optnl { $$ = $1; }
3677 | redir_host_list comma host optnl {
3678 $1->tail->next = $3;
3679 $1->tail = $3->tail;
3684 redirpool : /* empty */ { $$ = NULL; }
3686 $$ = calloc(1, sizeof(struct redirection));
3688 err(1, "redirection: calloc");
3690 $$->rport.a = $$->rport.b = $$->rport.t = 0;
3692 | ARROW redirspec PORT portstar {
3693 $$ = calloc(1, sizeof(struct redirection));
3695 err(1, "redirection: calloc");
3701 hashkey : /* empty */
3703 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3705 err(1, "hashkey: calloc");
3706 $$->key32[0] = arc4random();
3707 $$->key32[1] = arc4random();
3708 $$->key32[2] = arc4random();
3709 $$->key32[3] = arc4random();
3713 if (!strncmp($1, "0x", 2)) {
3714 if (strlen($1) != 34) {
3716 yyerror("hex key must be 128 bits "
3717 "(32 hex digits) long");
3720 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3722 err(1, "hashkey: calloc");
3724 if (sscanf($1, "0x%8x%8x%8x%8x",
3725 &$$->key32[0], &$$->key32[1],
3726 &$$->key32[2], &$$->key32[3]) != 4) {
3729 yyerror("invalid hex key");
3735 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3737 err(1, "hashkey: calloc");
3739 MD5Update(&context, (unsigned char *)$1,
3741 MD5Final((unsigned char *)$$, &context);
3742 HTONL($$->key32[0]);
3743 HTONL($$->key32[1]);
3744 HTONL($$->key32[2]);
3745 HTONL($$->key32[3]);
3751 pool_opts : { bzero(&pool_opts, sizeof pool_opts); }
3755 bzero(&pool_opts, sizeof pool_opts);
3760 pool_opts_l : pool_opts_l pool_opt
3764 pool_opt : BITMASK {
3765 if (pool_opts.type) {
3766 yyerror("pool type cannot be redefined");
3769 pool_opts.type = PF_POOL_BITMASK;
3772 if (pool_opts.type) {
3773 yyerror("pool type cannot be redefined");
3776 pool_opts.type = PF_POOL_RANDOM;
3778 | SOURCEHASH hashkey {
3779 if (pool_opts.type) {
3780 yyerror("pool type cannot be redefined");
3783 pool_opts.type = PF_POOL_SRCHASH;
3787 if (pool_opts.type) {
3788 yyerror("pool type cannot be redefined");
3791 pool_opts.type = PF_POOL_ROUNDROBIN;
3794 if (pool_opts.staticport) {
3795 yyerror("static-port cannot be redefined");
3798 pool_opts.staticport = 1;
3801 if (filter_opts.marker & POM_STICKYADDRESS) {
3802 yyerror("sticky-address cannot be redefined");
3805 pool_opts.marker |= POM_STICKYADDRESS;
3806 pool_opts.opts |= PF_POOL_STICKYADDR;
3810 redirection : /* empty */ { $$ = NULL; }
3812 $$ = calloc(1, sizeof(struct redirection));
3814 err(1, "redirection: calloc");
3816 $$->rport.a = $$->rport.b = $$->rport.t = 0;
3818 | ARROW host PORT portstar {
3819 $$ = calloc(1, sizeof(struct redirection));
3821 err(1, "redirection: calloc");
3827 natpasslog : /* empty */ { $$.b1 = $$.b2 = 0; $$.w2 = 0; }
3828 | PASS { $$.b1 = 1; $$.b2 = 0; $$.w2 = 0; }
3829 | PASS log { $$.b1 = 1; $$.b2 = $2.log; $$.w2 = $2.logif; }
3830 | log { $$.b1 = 0; $$.b2 = $1.log; $$.w2 = $1.logif; }
3833 nataction : no NAT natpasslog {
3835 yyerror("\"pass\" not valid with \"no\"");
3846 | no RDR natpasslog {
3848 yyerror("\"pass\" not valid with \"no\"");
3861 natrule : nataction interface af proto fromto tag tagged rtable
3866 if (check_rulestate(PFCTL_STATE_NAT))
3869 memset(&r, 0, sizeof(r));
3878 if ($5.src.host && $5.src.host->af &&
3879 !$5.src.host->ifindex)
3880 r.af = $5.src.host->af;
3881 else if ($5.dst.host && $5.dst.host->af &&
3882 !$5.dst.host->ifindex)
3883 r.af = $5.dst.host->af;
3887 if (strlcpy(r.tagname, $6, PF_TAG_NAME_SIZE) >=
3889 yyerror("tag too long, max %u chars",
3890 PF_TAG_NAME_SIZE - 1);
3895 if (strlcpy(r.match_tagname, $7.name,
3896 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
3897 yyerror("tag too long, max %u chars",
3898 PF_TAG_NAME_SIZE - 1);
3901 r.match_tag_not = $7.neg;
3904 if (r.action == PF_NONAT || r.action == PF_NORDR) {
3906 yyerror("translation rule with 'no' "
3907 "does not need '->'");
3911 if ($9 == NULL || $9->host == NULL) {
3912 yyerror("translation rule requires '-> "
3916 if (!r.af && ! $9->host->ifindex)
3917 r.af = $9->host->af;
3919 remove_invalid_hosts(&$9->host, &r.af);
3920 if (invalid_redirect($9->host, r.af))
3922 if (check_netmask($9->host, r.af))
3925 r.rpool.proxy_port[0] = ntohs($9->rport.a);
3929 if (!$9->rport.b && $9->rport.t &&
3930 $5.dst.port != NULL) {
3931 r.rpool.proxy_port[1] =
3932 ntohs($9->rport.a) +
3934 $5.dst.port->port[1]) -
3936 $5.dst.port->port[0]));
3938 r.rpool.proxy_port[1] =
3942 r.rpool.proxy_port[1] =
3944 if (!r.rpool.proxy_port[0] &&
3945 !r.rpool.proxy_port[1]) {
3946 r.rpool.proxy_port[0] =
3947 PF_NAT_PROXY_PORT_LOW;
3948 r.rpool.proxy_port[1] =
3949 PF_NAT_PROXY_PORT_HIGH;
3950 } else if (!r.rpool.proxy_port[1])
3951 r.rpool.proxy_port[1] =
3952 r.rpool.proxy_port[0];
3958 r.rpool.opts = $10.type;
3959 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
3960 PF_POOL_NONE && ($9->host->next != NULL ||
3961 $9->host->addr.type == PF_ADDR_TABLE ||
3962 DYNIF_MULTIADDR($9->host->addr)))
3963 r.rpool.opts = PF_POOL_ROUNDROBIN;
3964 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
3965 PF_POOL_ROUNDROBIN &&
3966 disallow_table($9->host, "tables are only "
3967 "supported in round-robin redirection "
3970 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
3971 PF_POOL_ROUNDROBIN &&
3972 disallow_alias($9->host, "interface (%s) "
3973 "is only supported in round-robin "
3974 "redirection pools"))
3976 if ($9->host->next != NULL) {
3977 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
3978 PF_POOL_ROUNDROBIN) {
3979 yyerror("only round-robin "
3980 "valid for multiple "
3981 "redirection addresses");
3987 if ($10.key != NULL)
3988 memcpy(&r.rpool.key, $10.key,
3989 sizeof(struct pf_poolhashkey));
3992 r.rpool.opts |= $10.opts;
3994 if ($10.staticport) {
3995 if (r.action != PF_NAT) {
3996 yyerror("the 'static-port' option is "
3997 "only valid with nat rules");
4000 if (r.rpool.proxy_port[0] !=
4001 PF_NAT_PROXY_PORT_LOW &&
4002 r.rpool.proxy_port[1] !=
4003 PF_NAT_PROXY_PORT_HIGH) {
4004 yyerror("the 'static-port' option can't"
4005 " be used when specifying a port"
4009 r.rpool.proxy_port[0] = 0;
4010 r.rpool.proxy_port[1] = 0;
4013 expand_rule(&r, $2, $9 == NULL ? NULL : $9->host, $4,
4014 $5.src_os, $5.src.host, $5.src.port, $5.dst.host,
4015 $5.dst.port, 0, 0, 0, "");
4020 binatrule : no BINAT natpasslog interface af proto FROM host toipspec tag
4021 tagged rtable redirection
4023 struct pf_rule binat;
4024 struct pf_pooladdr *pa;
4026 if (check_rulestate(PFCTL_STATE_NAT))
4028 if (disallow_urpf_failed($9, "\"urpf-failed\" is not "
4029 "permitted as a binat destination"))
4032 memset(&binat, 0, sizeof(binat));
4035 yyerror("\"pass\" not valid with \"no\"");
4039 binat.action = PF_NOBINAT;
4041 binat.action = PF_BINAT;
4042 binat.natpass = $3.b1;
4044 binat.logif = $3.w2;
4046 if (!binat.af && $8 != NULL && $8->af)
4048 if (!binat.af && $9 != NULL && $9->af)
4051 if (!binat.af && $13 != NULL && $13->host)
4052 binat.af = $13->host->af;
4054 yyerror("address family (inet/inet6) "
4060 memcpy(binat.ifname, $4->ifname,
4061 sizeof(binat.ifname));
4062 binat.ifnot = $4->not;
4067 if (strlcpy(binat.tagname, $10,
4068 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4069 yyerror("tag too long, max %u chars",
4070 PF_TAG_NAME_SIZE - 1);
4074 if (strlcpy(binat.match_tagname, $11.name,
4075 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4076 yyerror("tag too long, max %u chars",
4077 PF_TAG_NAME_SIZE - 1);
4080 binat.match_tag_not = $11.neg;
4081 binat.rtableid = $12;
4084 binat.proto = $6->proto;
4088 if ($8 != NULL && disallow_table($8, "invalid use of "
4089 "table <%s> as the source address of a binat rule"))
4091 if ($8 != NULL && disallow_alias($8, "invalid use of "
4092 "interface (%s) as the source address of a binat "
4095 if ($13 != NULL && $13->host != NULL && disallow_table(
4096 $13->host, "invalid use of table <%s> as the "
4097 "redirect address of a binat rule"))
4099 if ($13 != NULL && $13->host != NULL && disallow_alias(
4100 $13->host, "invalid use of interface (%s) as the "
4101 "redirect address of a binat rule"))
4106 yyerror("multiple binat ip addresses");
4109 if ($8->addr.type == PF_ADDR_DYNIFTL)
4111 if ($8->af != binat.af) {
4112 yyerror("binat ip versions must match");
4115 if (check_netmask($8, binat.af))
4117 memcpy(&binat.src.addr, &$8->addr,
4118 sizeof(binat.src.addr));
4123 yyerror("multiple binat ip addresses");
4126 if ($9->af != binat.af && $9->af) {
4127 yyerror("binat ip versions must match");
4130 if (check_netmask($9, binat.af))
4132 memcpy(&binat.dst.addr, &$9->addr,
4133 sizeof(binat.dst.addr));
4134 binat.dst.neg = $9->not;
4138 if (binat.action == PF_NOBINAT) {
4140 yyerror("'no binat' rule does not need"
4145 if ($13 == NULL || $13->host == NULL) {
4146 yyerror("'binat' rule requires"
4151 remove_invalid_hosts(&$13->host, &binat.af);
4152 if (invalid_redirect($13->host, binat.af))
4154 if ($13->host->next != NULL) {
4155 yyerror("binat rule must redirect to "
4156 "a single address");
4159 if (check_netmask($13->host, binat.af))
4162 if (!PF_AZERO(&binat.src.addr.v.a.mask,
4164 !PF_AEQ(&binat.src.addr.v.a.mask,
4165 &$13->host->addr.v.a.mask, binat.af)) {
4166 yyerror("'binat' source mask and "
4167 "redirect mask must be the same");
4171 TAILQ_INIT(&binat.rpool.list);
4172 pa = calloc(1, sizeof(struct pf_pooladdr));
4174 err(1, "binat: calloc");
4175 pa->addr = $13->host->addr;
4177 TAILQ_INSERT_TAIL(&binat.rpool.list,
4183 pfctl_add_rule(pf, &binat, "");
4187 tag : /* empty */ { $$ = NULL; }
4188 | TAG STRING { $$ = $2; }
4191 tagged : /* empty */ { $$.neg = 0; $$.name = NULL; }
4192 | not TAGGED string { $$.neg = $1; $$.name = $3; }
4195 rtable : /* empty */ { $$ = -1; }
4197 if ($2 < 0 || $2 > rt_tableid_max()) {
4198 yyerror("invalid rtable id");
4205 route_host : STRING {
4206 $$ = calloc(1, sizeof(struct node_host));
4208 err(1, "route_host: calloc");
4210 set_ipmask($$, 128);
4214 | '(' STRING host ')' {
4220 route_host_list : route_host optnl { $$ = $1; }
4221 | route_host_list comma route_host optnl {
4224 if ($1->af != $3->af) {
4225 yyerror("all pool addresses must be in the "
4226 "same address family");
4229 $1->tail->next = $3;
4230 $1->tail = $3->tail;
4235 routespec : route_host { $$ = $1; }
4236 | '{' optnl route_host_list '}' { $$ = $3; }
4239 route : /* empty */ {
4246 $$.rt = PF_FASTROUTE;
4249 | ROUTETO routespec pool_opts {
4252 $$.pool_opts = $3.type | $3.opts;
4256 | REPLYTO routespec pool_opts {
4259 $$.pool_opts = $3.type | $3.opts;
4263 | DUPTO routespec pool_opts {
4266 $$.pool_opts = $3.type | $3.opts;
4272 timeout_spec : STRING NUMBER
4274 if (check_rulestate(PFCTL_STATE_OPTION)) {
4278 if ($2 < 0 || $2 > UINT_MAX) {
4279 yyerror("only positive values permitted");
4282 if (pfctl_set_timeout(pf, $1, $2, 0) != 0) {
4283 yyerror("unknown timeout %s", $1);
4291 timeout_list : timeout_list comma timeout_spec optnl
4292 | timeout_spec optnl
4295 limit_spec : STRING NUMBER
4297 if (check_rulestate(PFCTL_STATE_OPTION)) {
4301 if ($2 < 0 || $2 > UINT_MAX) {
4302 yyerror("only positive values permitted");
4305 if (pfctl_set_limit(pf, $1, $2) != 0) {
4306 yyerror("unable to set limit %s %u", $1, $2);
4314 limit_list : limit_list comma limit_spec optnl
4322 yesno : NO { $$ = 0; }
4324 if (!strcmp($1, "yes"))
4327 yyerror("invalid value '%s', expected 'yes' "
4336 unaryop : '=' { $$ = PF_OP_EQ; }
4337 | '!' '=' { $$ = PF_OP_NE; }
4338 | '<' '=' { $$ = PF_OP_LE; }
4339 | '<' { $$ = PF_OP_LT; }
4340 | '>' '=' { $$ = PF_OP_GE; }
4341 | '>' { $$ = PF_OP_GT; }
4347 yyerror(const char *fmt, ...)
4353 fprintf(stderr, "%s:%d: ", file->name, yylval.lineno);
4354 vfprintf(stderr, fmt, ap);
4355 fprintf(stderr, "\n");
4361 disallow_table(struct node_host *h, const char *fmt)
4363 for (; h != NULL; h = h->next)
4364 if (h->addr.type == PF_ADDR_TABLE) {
4365 yyerror(fmt, h->addr.v.tblname);
4372 disallow_urpf_failed(struct node_host *h, const char *fmt)
4374 for (; h != NULL; h = h->next)
4375 if (h->addr.type == PF_ADDR_URPFFAILED) {
4383 disallow_alias(struct node_host *h, const char *fmt)
4385 for (; h != NULL; h = h->next)
4386 if (DYNIF_MULTIADDR(h->addr)) {
4387 yyerror(fmt, h->addr.v.tblname);
4394 rule_consistent(struct pf_rule *r, int anchor_call)
4398 switch (r->action) {
4403 problems = filter_consistent(r, anchor_call);
4407 problems = nat_consistent(r);
4411 problems = rdr_consistent(r);
4422 filter_consistent(struct pf_rule *r, int anchor_call)
4426 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP &&
4427 (r->src.port_op || r->dst.port_op)) {
4428 yyerror("port only applies to tcp/udp");
4431 if (r->proto != IPPROTO_ICMP && r->proto != IPPROTO_ICMPV6 &&
4432 (r->type || r->code)) {
4433 yyerror("icmp-type/code only applies to icmp");
4436 if (!r->af && (r->type || r->code)) {
4437 yyerror("must indicate address family with icmp-type/code");
4440 if (r->overload_tblname[0] &&
4441 r->max_src_conn == 0 && r->max_src_conn_rate.seconds == 0) {
4442 yyerror("'overload' requires 'max-src-conn' "
4443 "or 'max-src-conn-rate'");
4446 if ((r->proto == IPPROTO_ICMP && r->af == AF_INET6) ||
4447 (r->proto == IPPROTO_ICMPV6 && r->af == AF_INET)) {
4448 yyerror("proto %s doesn't match address family %s",
4449 r->proto == IPPROTO_ICMP ? "icmp" : "icmp6",
4450 r->af == AF_INET ? "inet" : "inet6");
4453 if (r->allow_opts && r->action != PF_PASS) {
4454 yyerror("allow-opts can only be specified for pass rules");
4457 if (r->rule_flag & PFRULE_FRAGMENT && (r->src.port_op ||
4458 r->dst.port_op || r->flagset || r->type || r->code)) {
4459 yyerror("fragments can be filtered only on IP header fields");
4462 if (r->rule_flag & PFRULE_RETURNRST && r->proto != IPPROTO_TCP) {
4463 yyerror("return-rst can only be applied to TCP rules");
4466 if (r->max_src_nodes && !(r->rule_flag & PFRULE_RULESRCTRACK)) {
4467 yyerror("max-src-nodes requires 'source-track rule'");
4470 if (r->action == PF_DROP && r->keep_state) {
4471 yyerror("keep state on block rules doesn't make sense");
4474 if (r->rule_flag & PFRULE_STATESLOPPY &&
4475 (r->keep_state == PF_STATE_MODULATE ||
4476 r->keep_state == PF_STATE_SYNPROXY)) {
4477 yyerror("sloppy state matching cannot be used with "
4478 "synproxy state or modulate state");
4485 nat_consistent(struct pf_rule *r)
4487 return (0); /* yeah! */
4491 rdr_consistent(struct pf_rule *r)
4495 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP) {
4496 if (r->src.port_op) {
4497 yyerror("src port only applies to tcp/udp");
4500 if (r->dst.port_op) {
4501 yyerror("dst port only applies to tcp/udp");
4504 if (r->rpool.proxy_port[0]) {
4505 yyerror("rpool port only applies to tcp/udp");
4509 if (r->dst.port_op &&
4510 r->dst.port_op != PF_OP_EQ && r->dst.port_op != PF_OP_RRG) {
4511 yyerror("invalid port operator for rdr destination port");
4518 process_tabledef(char *name, struct table_opts *opts)
4520 struct pfr_buffer ab;
4521 struct node_tinit *ti;
4523 bzero(&ab, sizeof(ab));
4524 ab.pfrb_type = PFRB_ADDRS;
4525 SIMPLEQ_FOREACH(ti, &opts->init_nodes, entries) {
4527 if (pfr_buf_load(&ab, ti->file, 0, append_addr)) {
4529 yyerror("cannot load \"%s\": %s",
4530 ti->file, strerror(errno));
4532 yyerror("file \"%s\" contains bad data",
4537 if (append_addr_host(&ab, ti->host, 0, 0)) {
4538 yyerror("cannot create address buffer: %s",
4543 if (pf->opts & PF_OPT_VERBOSE)
4544 print_tabledef(name, opts->flags, opts->init_addr,
4546 if (!(pf->opts & PF_OPT_NOACTION) &&
4547 pfctl_define_table(name, opts->flags, opts->init_addr,
4548 pf->anchor->name, &ab, pf->anchor->ruleset.tticket)) {
4549 yyerror("cannot define table %s: %s", name,
4550 pfr_strerror(errno));
4566 /* macro gore, but you should've seen the prior indentation nightmare... */
4568 #define FREE_LIST(T,r) \
4571 while (node != NULL) { \
4573 node = node->next; \
4578 #define LOOP_THROUGH(T,n,r,C) \
4582 r = calloc(1, sizeof(T)); \
4584 err(1, "LOOP: calloc"); \
4588 while (n != NULL) { \
4597 expand_label_str(char *label, size_t len, const char *srch, const char *repl)
4602 if ((tmp = calloc(1, len)) == NULL)
4603 err(1, "expand_label_str: calloc");
4605 while ((q = strstr(p, srch)) != NULL) {
4607 if ((strlcat(tmp, p, len) >= len) ||
4608 (strlcat(tmp, repl, len) >= len))
4609 errx(1, "expand_label: label too long");
4613 if (strlcat(tmp, p, len) >= len)
4614 errx(1, "expand_label: label too long");
4615 strlcpy(label, tmp, len); /* always fits */
4620 expand_label_if(const char *name, char *label, size_t len, const char *ifname)
4622 if (strstr(label, name) != NULL) {
4624 expand_label_str(label, len, name, "any");
4626 expand_label_str(label, len, name, ifname);
4631 expand_label_addr(const char *name, char *label, size_t len, sa_family_t af,
4632 struct node_host *h)
4634 char tmp[64], tmp_not[66];
4636 if (strstr(label, name) != NULL) {
4637 switch (h->addr.type) {
4638 case PF_ADDR_DYNIFTL:
4639 snprintf(tmp, sizeof(tmp), "(%s)", h->addr.v.ifname);
4642 snprintf(tmp, sizeof(tmp), "<%s>", h->addr.v.tblname);
4644 case PF_ADDR_NOROUTE:
4645 snprintf(tmp, sizeof(tmp), "no-route");
4647 case PF_ADDR_URPFFAILED:
4648 snprintf(tmp, sizeof(tmp), "urpf-failed");
4650 case PF_ADDR_ADDRMASK:
4651 if (!af || (PF_AZERO(&h->addr.v.a.addr, af) &&
4652 PF_AZERO(&h->addr.v.a.mask, af)))
4653 snprintf(tmp, sizeof(tmp), "any");
4658 if (inet_ntop(af, &h->addr.v.a.addr, a,
4660 snprintf(tmp, sizeof(tmp), "?");
4662 bits = unmask(&h->addr.v.a.mask, af);
4663 if ((af == AF_INET && bits < 32) ||
4664 (af == AF_INET6 && bits < 128))
4665 snprintf(tmp, sizeof(tmp),
4668 snprintf(tmp, sizeof(tmp),
4674 snprintf(tmp, sizeof(tmp), "?");
4679 snprintf(tmp_not, sizeof(tmp_not), "! %s", tmp);
4680 expand_label_str(label, len, name, tmp_not);
4682 expand_label_str(label, len, name, tmp);
4687 expand_label_port(const char *name, char *label, size_t len,
4688 struct node_port *port)
4690 char a1[6], a2[6], op[13] = "";
4692 if (strstr(label, name) != NULL) {
4693 snprintf(a1, sizeof(a1), "%u", ntohs(port->port[0]));
4694 snprintf(a2, sizeof(a2), "%u", ntohs(port->port[1]));
4697 else if (port->op == PF_OP_IRG)
4698 snprintf(op, sizeof(op), "%s><%s", a1, a2);
4699 else if (port->op == PF_OP_XRG)
4700 snprintf(op, sizeof(op), "%s<>%s", a1, a2);
4701 else if (port->op == PF_OP_EQ)
4702 snprintf(op, sizeof(op), "%s", a1);
4703 else if (port->op == PF_OP_NE)
4704 snprintf(op, sizeof(op), "!=%s", a1);
4705 else if (port->op == PF_OP_LT)
4706 snprintf(op, sizeof(op), "<%s", a1);
4707 else if (port->op == PF_OP_LE)
4708 snprintf(op, sizeof(op), "<=%s", a1);
4709 else if (port->op == PF_OP_GT)
4710 snprintf(op, sizeof(op), ">%s", a1);
4711 else if (port->op == PF_OP_GE)
4712 snprintf(op, sizeof(op), ">=%s", a1);
4713 expand_label_str(label, len, name, op);
4718 expand_label_proto(const char *name, char *label, size_t len, u_int8_t proto)
4720 struct protoent *pe;
4723 if (strstr(label, name) != NULL) {
4724 pe = getprotobynumber(proto);
4726 expand_label_str(label, len, name, pe->p_name);
4728 snprintf(n, sizeof(n), "%u", proto);
4729 expand_label_str(label, len, name, n);
4735 expand_label_nr(const char *name, char *label, size_t len)
4739 if (strstr(label, name) != NULL) {
4740 snprintf(n, sizeof(n), "%u", pf->anchor->match);
4741 expand_label_str(label, len, name, n);
4746 expand_label(char *label, size_t len, const char *ifname, sa_family_t af,
4747 struct node_host *src_host, struct node_port *src_port,
4748 struct node_host *dst_host, struct node_port *dst_port,
4751 expand_label_if("$if", label, len, ifname);
4752 expand_label_addr("$srcaddr", label, len, af, src_host);
4753 expand_label_addr("$dstaddr", label, len, af, dst_host);
4754 expand_label_port("$srcport", label, len, src_port);
4755 expand_label_port("$dstport", label, len, dst_port);
4756 expand_label_proto("$proto", label, len, proto);
4757 expand_label_nr("$nr", label, len);
4761 expand_altq(struct pf_altq *a, struct node_if *interfaces,
4762 struct node_queue *nqueues, struct node_queue_bw bwspec,
4763 struct node_queue_opt *opts)
4765 struct pf_altq pa, pb;
4766 char qname[PF_QNAME_SIZE];
4767 struct node_queue *n;
4768 struct node_queue_bw bw;
4771 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
4772 FREE_LIST(struct node_if, interfaces);
4773 FREE_LIST(struct node_queue, nqueues);
4777 LOOP_THROUGH(struct node_if, interface, interfaces,
4778 memcpy(&pa, a, sizeof(struct pf_altq));
4779 if (strlcpy(pa.ifname, interface->ifname,
4780 sizeof(pa.ifname)) >= sizeof(pa.ifname))
4781 errx(1, "expand_altq: strlcpy");
4783 if (interface->not) {
4784 yyerror("altq on ! <interface> is not supported");
4787 if (eval_pfaltq(pf, &pa, &bwspec, opts))
4790 if (pfctl_add_altq(pf, &pa))
4793 if (pf->opts & PF_OPT_VERBOSE) {
4794 print_altq(&pf->paltq->altq, 0,
4796 if (nqueues && nqueues->tail) {
4798 LOOP_THROUGH(struct node_queue, queue,
4808 if (pa.scheduler == ALTQT_CBQ ||
4809 pa.scheduler == ALTQT_HFSC) {
4810 /* now create a root queue */
4811 memset(&pb, 0, sizeof(struct pf_altq));
4812 if (strlcpy(qname, "root_", sizeof(qname)) >=
4814 errx(1, "expand_altq: strlcpy");
4815 if (strlcat(qname, interface->ifname,
4816 sizeof(qname)) >= sizeof(qname))
4817 errx(1, "expand_altq: strlcat");
4818 if (strlcpy(pb.qname, qname,
4819 sizeof(pb.qname)) >= sizeof(pb.qname))
4820 errx(1, "expand_altq: strlcpy");
4821 if (strlcpy(pb.ifname, interface->ifname,
4822 sizeof(pb.ifname)) >= sizeof(pb.ifname))
4823 errx(1, "expand_altq: strlcpy");
4824 pb.qlimit = pa.qlimit;
4825 pb.scheduler = pa.scheduler;
4826 bw.bw_absolute = pa.ifbandwidth;
4828 if (eval_pfqueue(pf, &pb, &bw, opts))
4831 if (pfctl_add_altq(pf, &pb))
4835 LOOP_THROUGH(struct node_queue, queue, nqueues,
4836 n = calloc(1, sizeof(struct node_queue));
4838 err(1, "expand_altq: calloc");
4839 if (pa.scheduler == ALTQT_CBQ ||
4840 pa.scheduler == ALTQT_HFSC)
4841 if (strlcpy(n->parent, qname,
4842 sizeof(n->parent)) >=
4844 errx(1, "expand_altq: strlcpy");
4845 if (strlcpy(n->queue, queue->queue,
4846 sizeof(n->queue)) >= sizeof(n->queue))
4847 errx(1, "expand_altq: strlcpy");
4848 if (strlcpy(n->ifname, interface->ifname,
4849 sizeof(n->ifname)) >= sizeof(n->ifname))
4850 errx(1, "expand_altq: strlcpy");
4851 n->scheduler = pa.scheduler;
4857 queues->tail->next = n;
4863 FREE_LIST(struct node_if, interfaces);
4864 FREE_LIST(struct node_queue, nqueues);
4870 expand_queue(struct pf_altq *a, struct node_if *interfaces,
4871 struct node_queue *nqueues, struct node_queue_bw bwspec,
4872 struct node_queue_opt *opts)
4874 struct node_queue *n, *nq;
4879 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
4880 FREE_LIST(struct node_queue, nqueues);
4884 if (queues == NULL) {
4885 yyerror("queue %s has no parent", a->qname);
4886 FREE_LIST(struct node_queue, nqueues);
4890 LOOP_THROUGH(struct node_if, interface, interfaces,
4891 LOOP_THROUGH(struct node_queue, tqueue, queues,
4892 if (!strncmp(a->qname, tqueue->queue, PF_QNAME_SIZE) &&
4893 (interface->ifname[0] == 0 ||
4894 (!interface->not && !strncmp(interface->ifname,
4895 tqueue->ifname, IFNAMSIZ)) ||
4896 (interface->not && strncmp(interface->ifname,
4897 tqueue->ifname, IFNAMSIZ)))) {
4898 /* found ourself in queues */
4901 memcpy(&pa, a, sizeof(struct pf_altq));
4903 if (pa.scheduler != ALTQT_NONE &&
4904 pa.scheduler != tqueue->scheduler) {
4905 yyerror("exactly one scheduler type "
4906 "per interface allowed");
4909 pa.scheduler = tqueue->scheduler;
4911 /* scheduler dependent error checking */
4912 switch (pa.scheduler) {
4914 if (nqueues != NULL) {
4915 yyerror("priq queues cannot "
4916 "have child queues");
4919 if (bwspec.bw_absolute > 0 ||
4920 bwspec.bw_percent < 100) {
4921 yyerror("priq doesn't take "
4930 if (strlcpy(pa.ifname, tqueue->ifname,
4931 sizeof(pa.ifname)) >= sizeof(pa.ifname))
4932 errx(1, "expand_queue: strlcpy");
4933 if (strlcpy(pa.parent, tqueue->parent,
4934 sizeof(pa.parent)) >= sizeof(pa.parent))
4935 errx(1, "expand_queue: strlcpy");
4937 if (eval_pfqueue(pf, &pa, &bwspec, opts))
4940 if (pfctl_add_altq(pf, &pa))
4943 for (nq = nqueues; nq != NULL; nq = nq->next) {
4944 if (!strcmp(a->qname, nq->queue)) {
4945 yyerror("queue cannot have "
4951 sizeof(struct node_queue));
4953 err(1, "expand_queue: calloc");
4954 if (strlcpy(n->parent, a->qname,
4955 sizeof(n->parent)) >=
4957 errx(1, "expand_queue strlcpy");
4958 if (strlcpy(n->queue, nq->queue,
4959 sizeof(n->queue)) >=
4961 errx(1, "expand_queue strlcpy");
4962 if (strlcpy(n->ifname, tqueue->ifname,
4963 sizeof(n->ifname)) >=
4965 errx(1, "expand_queue strlcpy");
4966 n->scheduler = tqueue->scheduler;
4972 queues->tail->next = n;
4976 if ((pf->opts & PF_OPT_VERBOSE) && (
4977 (found == 1 && interface->ifname[0] == 0) ||
4978 (found > 0 && interface->ifname[0] != 0))) {
4979 print_queue(&pf->paltq->altq, 0,
4980 &bwspec, interface->ifname[0] != 0,
4982 if (nqueues && nqueues->tail) {
4984 LOOP_THROUGH(struct node_queue,
4997 FREE_LIST(struct node_queue, nqueues);
4998 FREE_LIST(struct node_if, interfaces);
5001 yyerror("queue %s has no parent", a->qname);
5012 expand_rule(struct pf_rule *r,
5013 struct node_if *interfaces, struct node_host *rpool_hosts,
5014 struct node_proto *protos, struct node_os *src_oses,
5015 struct node_host *src_hosts, struct node_port *src_ports,
5016 struct node_host *dst_hosts, struct node_port *dst_ports,
5017 struct node_uid *uids, struct node_gid *gids, struct node_icmp *icmp_types,
5018 const char *anchor_call)
5020 sa_family_t af = r->af;
5021 int added = 0, error = 0;
5022 char ifname[IF_NAMESIZE];
5023 char label[PF_RULE_LABEL_SIZE];
5024 char tagname[PF_TAG_NAME_SIZE];
5025 char match_tagname[PF_TAG_NAME_SIZE];
5026 struct pf_pooladdr *pa;
5027 struct node_host *h;
5028 u_int8_t flags, flagset, keep_state;
5030 if (strlcpy(label, r->label, sizeof(label)) >= sizeof(label))
5031 errx(1, "expand_rule: strlcpy");
5032 if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname))
5033 errx(1, "expand_rule: strlcpy");
5034 if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >=
5035 sizeof(match_tagname))
5036 errx(1, "expand_rule: strlcpy");
5038 flagset = r->flagset;
5039 keep_state = r->keep_state;
5041 LOOP_THROUGH(struct node_if, interface, interfaces,
5042 LOOP_THROUGH(struct node_proto, proto, protos,
5043 LOOP_THROUGH(struct node_icmp, icmp_type, icmp_types,
5044 LOOP_THROUGH(struct node_host, src_host, src_hosts,
5045 LOOP_THROUGH(struct node_port, src_port, src_ports,
5046 LOOP_THROUGH(struct node_os, src_os, src_oses,
5047 LOOP_THROUGH(struct node_host, dst_host, dst_hosts,
5048 LOOP_THROUGH(struct node_port, dst_port, dst_ports,
5049 LOOP_THROUGH(struct node_uid, uid, uids,
5050 LOOP_THROUGH(struct node_gid, gid, gids,
5053 /* for link-local IPv6 address, interface must match up */
5054 if ((r->af && src_host->af && r->af != src_host->af) ||
5055 (r->af && dst_host->af && r->af != dst_host->af) ||
5056 (src_host->af && dst_host->af &&
5057 src_host->af != dst_host->af) ||
5058 (src_host->ifindex && dst_host->ifindex &&
5059 src_host->ifindex != dst_host->ifindex) ||
5060 (src_host->ifindex && *interface->ifname &&
5061 src_host->ifindex != if_nametoindex(interface->ifname)) ||
5062 (dst_host->ifindex && *interface->ifname &&
5063 dst_host->ifindex != if_nametoindex(interface->ifname)))
5065 if (!r->af && src_host->af)
5066 r->af = src_host->af;
5067 else if (!r->af && dst_host->af)
5068 r->af = dst_host->af;
5070 if (*interface->ifname)
5071 strlcpy(r->ifname, interface->ifname,
5073 else if (if_indextoname(src_host->ifindex, ifname))
5074 strlcpy(r->ifname, ifname, sizeof(r->ifname));
5075 else if (if_indextoname(dst_host->ifindex, ifname))
5076 strlcpy(r->ifname, ifname, sizeof(r->ifname));
5078 memset(r->ifname, '\0', sizeof(r->ifname));
5080 if (strlcpy(r->label, label, sizeof(r->label)) >=
5082 errx(1, "expand_rule: strlcpy");
5083 if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >=
5085 errx(1, "expand_rule: strlcpy");
5086 if (strlcpy(r->match_tagname, match_tagname,
5087 sizeof(r->match_tagname)) >= sizeof(r->match_tagname))
5088 errx(1, "expand_rule: strlcpy");
5089 expand_label(r->label, PF_RULE_LABEL_SIZE, r->ifname, r->af,
5090 src_host, src_port, dst_host, dst_port, proto->proto);
5091 expand_label(r->tagname, PF_TAG_NAME_SIZE, r->ifname, r->af,
5092 src_host, src_port, dst_host, dst_port, proto->proto);
5093 expand_label(r->match_tagname, PF_TAG_NAME_SIZE, r->ifname,
5094 r->af, src_host, src_port, dst_host, dst_port,
5097 error += check_netmask(src_host, r->af);
5098 error += check_netmask(dst_host, r->af);
5100 r->ifnot = interface->not;
5101 r->proto = proto->proto;
5102 r->src.addr = src_host->addr;
5103 r->src.neg = src_host->not;
5104 r->src.port[0] = src_port->port[0];
5105 r->src.port[1] = src_port->port[1];
5106 r->src.port_op = src_port->op;
5107 r->dst.addr = dst_host->addr;
5108 r->dst.neg = dst_host->not;
5109 r->dst.port[0] = dst_port->port[0];
5110 r->dst.port[1] = dst_port->port[1];
5111 r->dst.port_op = dst_port->op;
5112 r->uid.op = uid->op;
5113 r->uid.uid[0] = uid->uid[0];
5114 r->uid.uid[1] = uid->uid[1];
5115 r->gid.op = gid->op;
5116 r->gid.gid[0] = gid->gid[0];
5117 r->gid.gid[1] = gid->gid[1];
5118 r->type = icmp_type->type;
5119 r->code = icmp_type->code;
5121 if ((keep_state == PF_STATE_MODULATE ||
5122 keep_state == PF_STATE_SYNPROXY) &&
5123 r->proto && r->proto != IPPROTO_TCP)
5124 r->keep_state = PF_STATE_NORMAL;
5126 r->keep_state = keep_state;
5128 if (r->proto && r->proto != IPPROTO_TCP) {
5133 r->flagset = flagset;
5135 if (icmp_type->proto && r->proto != icmp_type->proto) {
5136 yyerror("icmp-type mismatch");
5140 if (src_os && src_os->os) {
5141 r->os_fingerprint = pfctl_get_fingerprint(src_os->os);
5142 if ((pf->opts & PF_OPT_VERBOSE2) &&
5143 r->os_fingerprint == PF_OSFP_NOMATCH)
5145 "warning: unknown '%s' OS fingerprint\n",
5148 r->os_fingerprint = PF_OSFP_ANY;
5151 TAILQ_INIT(&r->rpool.list);
5152 for (h = rpool_hosts; h != NULL; h = h->next) {
5153 pa = calloc(1, sizeof(struct pf_pooladdr));
5155 err(1, "expand_rule: calloc");
5157 if (h->ifname != NULL) {
5158 if (strlcpy(pa->ifname, h->ifname,
5159 sizeof(pa->ifname)) >=
5161 errx(1, "expand_rule: strlcpy");
5164 TAILQ_INSERT_TAIL(&r->rpool.list, pa, entries);
5167 if (rule_consistent(r, anchor_call[0]) < 0 || error)
5168 yyerror("skipping rule due to errors");
5170 r->nr = pf->astack[pf->asd]->match++;
5171 pfctl_add_rule(pf, r, anchor_call);
5177 FREE_LIST(struct node_if, interfaces);
5178 FREE_LIST(struct node_proto, protos);
5179 FREE_LIST(struct node_host, src_hosts);
5180 FREE_LIST(struct node_port, src_ports);
5181 FREE_LIST(struct node_os, src_oses);
5182 FREE_LIST(struct node_host, dst_hosts);
5183 FREE_LIST(struct node_port, dst_ports);
5184 FREE_LIST(struct node_uid, uids);
5185 FREE_LIST(struct node_gid, gids);
5186 FREE_LIST(struct node_icmp, icmp_types);
5187 FREE_LIST(struct node_host, rpool_hosts);
5190 yyerror("rule expands to no valid combination");
5194 expand_skip_interface(struct node_if *interfaces)
5198 if (!interfaces || (!interfaces->next && !interfaces->not &&
5199 !strcmp(interfaces->ifname, "none"))) {
5200 if (pf->opts & PF_OPT_VERBOSE)
5201 printf("set skip on none\n");
5202 errs = pfctl_set_interface_flags(pf, "", PFI_IFLAG_SKIP, 0);
5206 if (pf->opts & PF_OPT_VERBOSE)
5207 printf("set skip on {");
5208 LOOP_THROUGH(struct node_if, interface, interfaces,
5209 if (pf->opts & PF_OPT_VERBOSE)
5210 printf(" %s", interface->ifname);
5211 if (interface->not) {
5212 yyerror("skip on ! <interface> is not supported");
5215 errs += pfctl_set_interface_flags(pf,
5216 interface->ifname, PFI_IFLAG_SKIP, 1);
5218 if (pf->opts & PF_OPT_VERBOSE)
5221 FREE_LIST(struct node_if, interfaces);
5233 check_rulestate(int desired_state)
5235 if (require_order && (rulestate > desired_state)) {
5236 yyerror("Rules must be in order: options, normalization, "
5237 "queueing, translation, filtering");
5240 rulestate = desired_state;
5245 kw_cmp(const void *k, const void *e)
5247 return (strcmp(k, ((const struct keywords *)e)->k_name));
5253 /* this has to be sorted always */
5254 static const struct keywords keywords[] = {
5256 { "allow-opts", ALLOWOPTS},
5258 { "anchor", ANCHOR},
5259 { "antispoof", ANTISPOOF},
5261 { "bandwidth", BANDWIDTH},
5263 { "binat-anchor", BINATANCHOR},
5264 { "bitmask", BITMASK},
5266 { "block-policy", BLOCKPOLICY},
5269 { "crop", FRAGCROP},
5271 { "divert-reply", DIVERTREPLY},
5272 { "divert-to", DIVERTTO},
5274 { "drop-ovl", FRAGDROP},
5276 { "fastroute", FASTROUTE},
5277 { "file", FILENAME},
5278 { "fingerprints", FINGERPRINTS},
5280 { "floating", FLOATING},
5283 { "fragment", FRAGMENT},
5285 { "global", GLOBAL},
5288 { "hostid", HOSTID},
5289 { "icmp-type", ICMPTYPE},
5290 { "icmp6-type", ICMP6TYPE},
5291 { "if-bound", IFBOUND},
5293 { "include", INCLUDE},
5299 { "linkshare", LINKSHARE},
5302 { "loginterface", LOGINTERFACE},
5304 { "max-mss", MAXMSS},
5305 { "max-src-conn", MAXSRCCONN},
5306 { "max-src-conn-rate", MAXSRCCONNRATE},
5307 { "max-src-nodes", MAXSRCNODES},
5308 { "max-src-states", MAXSRCSTATES},
5309 { "min-ttl", MINTTL},
5310 { "modulate", MODULATE},
5312 { "nat-anchor", NATANCHOR},
5315 { "no-route", NOROUTE},
5316 { "no-sync", NOSYNC},
5318 { "optimization", OPTIMIZATION},
5321 { "overload", OVERLOAD},
5325 { "priority", PRIORITY},
5327 { "probability", PROBABILITY},
5329 { "qlimit", QLIMIT},
5332 { "random", RANDOM},
5333 { "random-id", RANDOMID},
5335 { "rdr-anchor", RDRANCHOR},
5336 { "realtime", REALTIME},
5337 { "reassemble", REASSEMBLE},
5338 { "reply-to", REPLYTO},
5339 { "require-order", REQUIREORDER},
5340 { "return", RETURN},
5341 { "return-icmp", RETURNICMP},
5342 { "return-icmp6", RETURNICMP6},
5343 { "return-rst", RETURNRST},
5344 { "round-robin", ROUNDROBIN},
5346 { "route-to", ROUTETO},
5347 { "rtable", RTABLE},
5349 { "ruleset-optimization", RULESET_OPTIMIZATION},
5352 { "set-tos", SETTOS},
5354 { "sloppy", SLOPPY},
5355 { "source-hash", SOURCEHASH},
5356 { "source-track", SOURCETRACK},
5358 { "state-defaults", STATEDEFAULTS},
5359 { "state-policy", STATEPOLICY},
5360 { "static-port", STATICPORT},
5361 { "sticky-address", STICKYADDRESS},
5362 { "synproxy", SYNPROXY},
5365 { "tagged", TAGGED},
5366 { "tbrsize", TBRSIZE},
5367 { "timeout", TIMEOUT},
5371 { "upperlimit", UPPERLIMIT},
5372 { "urpf-failed", URPFFAILED},
5375 const struct keywords *p;
5377 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
5378 sizeof(keywords[0]), kw_cmp);
5382 fprintf(stderr, "%s: %d\n", s, p->k_val);
5386 fprintf(stderr, "string: %s\n", s);
5391 #define MAXPUSHBACK 128
5395 char pushback_buffer[MAXPUSHBACK];
5396 int pushback_index = 0;
5404 /* Read character from the parsebuffer instead of input. */
5405 if (parseindex >= 0) {
5406 c = parsebuf[parseindex++];
5415 return (pushback_buffer[--pushback_index]);
5418 if ((c = getc(file->stream)) == EOF) {
5419 yyerror("reached end of file while parsing quoted string");
5420 if (popfile() == EOF)
5427 while ((c = getc(file->stream)) == '\\') {
5428 next = getc(file->stream);
5433 yylval.lineno = file->lineno;
5438 if (popfile() == EOF)
5440 c = getc(file->stream);
5452 if (parseindex >= 0)
5455 if (pushback_index < MAXPUSHBACK-1)
5456 return (pushback_buffer[pushback_index++] = c);
5468 /* skip to either EOF or the first real EOL */
5471 c = pushback_buffer[--pushback_index];
5489 int quotec, next, c;
5494 while ((c = lgetc(0)) == ' ' || c == '\t')
5497 yylval.lineno = file->lineno;
5499 while ((c = lgetc(0)) != '\n' && c != EOF)
5501 if (c == '$' && parsebuf == NULL) {
5503 if ((c = lgetc(0)) == EOF)
5506 if (p + 1 >= buf + sizeof(buf) - 1) {
5507 yyerror("string too long");
5510 if (isalnum(c) || c == '_') {
5520 yyerror("macro '%s' not defined", buf);
5533 if ((c = lgetc(quotec)) == EOF)
5538 } else if (c == '\\') {
5539 if ((next = lgetc(quotec)) == EOF)
5541 if (next == quotec || c == ' ' || c == '\t')
5543 else if (next == '\n')
5547 } else if (c == quotec) {
5551 if (p + 1 >= buf + sizeof(buf) - 1) {
5552 yyerror("string too long");
5557 yylval.v.string = strdup(buf);
5558 if (yylval.v.string == NULL)
5559 err(1, "yylex: strdup");
5564 yylval.v.i = PF_OP_XRG;
5565 return (PORTBINARY);
5572 yylval.v.i = PF_OP_IRG;
5573 return (PORTBINARY);
5585 #define allowed_to_end_number(x) \
5586 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
5588 if (c == '-' || isdigit(c)) {
5591 if ((unsigned)(p-buf) >= sizeof(buf)) {
5592 yyerror("string too long");
5595 } while ((c = lgetc(0)) != EOF && isdigit(c));
5597 if (p == buf + 1 && buf[0] == '-')
5599 if (c == EOF || allowed_to_end_number(c)) {
5600 const char *errstr = NULL;
5603 yylval.v.number = strtonum(buf, LLONG_MIN,
5604 LLONG_MAX, &errstr);
5606 yyerror("\"%s\" invalid number: %s",
5621 #define allowed_in_string(x) \
5622 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
5623 x != '{' && x != '}' && x != '<' && x != '>' && \
5624 x != '!' && x != '=' && x != '/' && x != '#' && \
5627 if (isalnum(c) || c == ':' || c == '_') {
5630 if ((unsigned)(p-buf) >= sizeof(buf)) {
5631 yyerror("string too long");
5634 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
5637 if ((token = lookup(buf)) == STRING)
5638 if ((yylval.v.string = strdup(buf)) == NULL)
5639 err(1, "yylex: strdup");
5643 yylval.lineno = file->lineno;
5652 check_file_secrecy(int fd, const char *fname)
5656 if (fstat(fd, &st)) {
5657 warn("cannot stat %s", fname);
5660 if (st.st_uid != 0 && st.st_uid != getuid()) {
5661 warnx("%s: owner not root or current user", fname);
5664 if (st.st_mode & (S_IRWXG | S_IRWXO)) {
5665 warnx("%s: group/world readable/writeable", fname);
5672 pushfile(const char *name, int secret)
5676 if ((nfile = calloc(1, sizeof(struct file))) == NULL ||
5677 (nfile->name = strdup(name)) == NULL) {
5681 if (TAILQ_FIRST(&files) == NULL && strcmp(nfile->name, "-") == 0) {
5682 nfile->stream = stdin;
5684 if ((nfile->name = strdup("stdin")) == NULL) {
5689 } else if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
5690 warn("%s", nfile->name);
5694 } else if (secret &&
5695 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
5696 fclose(nfile->stream);
5702 TAILQ_INSERT_TAIL(&files, nfile, entry);
5711 if ((prev = TAILQ_PREV(file, files, entry)) != NULL) {
5712 prev->errors += file->errors;
5713 TAILQ_REMOVE(&files, file, entry);
5714 fclose(file->stream);
5724 parse_config(char *filename, struct pfctl *xpf)
5731 rulestate = PFCTL_STATE_NONE;
5732 returnicmpdefault = (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
5733 returnicmp6default =
5734 (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
5735 blockpolicy = PFRULE_DROP;
5738 if ((file = pushfile(filename, 0)) == NULL) {
5739 warn("cannot open the main config file!");
5744 errors = file->errors;
5747 /* Free macros and check which have not been used. */
5748 while ((sym = TAILQ_FIRST(&symhead))) {
5749 if ((pf->opts & PF_OPT_VERBOSE2) && !sym->used)
5750 fprintf(stderr, "warning: macro '%s' not "
5751 "used\n", sym->nam);
5754 TAILQ_REMOVE(&symhead, sym, entry);
5758 return (errors ? -1 : 0);
5762 symset(const char *nam, const char *val, int persist)
5766 for (sym = TAILQ_FIRST(&symhead); sym && strcmp(nam, sym->nam);
5767 sym = TAILQ_NEXT(sym, entry))
5771 if (sym->persist == 1)
5776 TAILQ_REMOVE(&symhead, sym, entry);
5780 if ((sym = calloc(1, sizeof(*sym))) == NULL)
5783 sym->nam = strdup(nam);
5784 if (sym->nam == NULL) {
5788 sym->val = strdup(val);
5789 if (sym->val == NULL) {
5795 sym->persist = persist;
5796 TAILQ_INSERT_TAIL(&symhead, sym, entry);
5801 pfctl_cmdline_symset(char *s)
5806 if ((val = strrchr(s, '=')) == NULL)
5809 if ((sym = malloc(strlen(s) - strlen(val) + 1)) == NULL)
5810 err(1, "pfctl_cmdline_symset: malloc");
5812 strlcpy(sym, s, strlen(s) - strlen(val) + 1);
5814 ret = symset(sym, val + 1, 1);
5821 symget(const char *nam)
5825 TAILQ_FOREACH(sym, &symhead, entry)
5826 if (strcmp(nam, sym->nam) == 0) {
5834 mv_rules(struct pf_ruleset *src, struct pf_ruleset *dst)
5839 for (i = 0; i < PF_RULESET_MAX; ++i) {
5840 while ((r = TAILQ_FIRST(src->rules[i].active.ptr))
5842 TAILQ_REMOVE(src->rules[i].active.ptr, r, entries);
5843 TAILQ_INSERT_TAIL(dst->rules[i].active.ptr, r, entries);
5844 dst->anchor->match++;
5846 src->anchor->match = 0;
5847 while ((r = TAILQ_FIRST(src->rules[i].inactive.ptr))
5849 TAILQ_REMOVE(src->rules[i].inactive.ptr, r, entries);
5850 TAILQ_INSERT_TAIL(dst->rules[i].inactive.ptr,
5857 decide_address_family(struct node_host *n, sa_family_t *af)
5859 if (*af != 0 || n == NULL)
5862 while ((n = n->next) != NULL) {
5871 remove_invalid_hosts(struct node_host **nh, sa_family_t *af)
5873 struct node_host *n = *nh, *prev = NULL;
5876 if (*af && n->af && n->af != *af) {
5877 /* unlink and free n */
5878 struct node_host *next = n->next;
5880 /* adjust tail pointer */
5881 if (n == (*nh)->tail)
5883 /* adjust previous node's next pointer */
5889 if (n->ifname != NULL)
5903 invalid_redirect(struct node_host *nh, sa_family_t af)
5906 struct node_host *n;
5908 /* tables and dyniftl are ok without an address family */
5909 for (n = nh; n != NULL; n = n->next) {
5910 if (n->addr.type != PF_ADDR_TABLE &&
5911 n->addr.type != PF_ADDR_DYNIFTL) {
5912 yyerror("address family not given and "
5913 "translation address expands to multiple "
5914 "address families");
5920 yyerror("no translation address with matching address family "
5928 atoul(char *s, u_long *ulvalp)
5934 ulval = strtoul(s, &ep, 0);
5935 if (s[0] == '\0' || *ep != '\0')
5937 if (errno == ERANGE && ulval == ULONG_MAX)
5949 if (atoul(n, &ulval) == 0) {
5950 if (ulval > 65535) {
5951 yyerror("illegal port value %lu", ulval);
5954 return (htons(ulval));
5956 s = getservbyname(n, "tcp");
5958 s = getservbyname(n, "udp");
5960 yyerror("unknown port %s", n);
5968 rule_label(struct pf_rule *r, char *s)
5971 if (strlcpy(r->label, s, sizeof(r->label)) >=
5973 yyerror("rule label too long (max %d chars)",
5974 sizeof(r->label)-1);
5982 parseicmpspec(char *w, sa_family_t af)
5984 const struct icmpcodeent *p;
5989 icmptype = returnicmpdefault >> 8;
5991 icmptype = returnicmp6default >> 8;
5993 if (atoul(w, &ulval) == -1) {
5994 if ((p = geticmpcodebyname(icmptype, w, af)) == NULL) {
5995 yyerror("unknown icmp code %s", w);
6001 yyerror("invalid icmp code %lu", ulval);
6004 return (icmptype << 8 | ulval);
6008 parseport(char *port, struct range *r, int extensions)
6010 char *p = strchr(port, ':');
6013 if ((r->a = getservice(port)) == -1)
6019 if ((extensions & PPORT_STAR) && !strcmp(p+1, "*")) {
6021 if ((r->a = getservice(port)) == -1)
6027 if ((extensions & PPORT_RANGE)) {
6029 if ((r->a = getservice(port)) == -1 ||
6030 (r->b = getservice(p)) == -1)
6043 pfctl_load_anchors(int dev, struct pfctl *pf, struct pfr_buffer *trans)
6045 struct loadanchors *la;
6047 TAILQ_FOREACH(la, &loadanchorshead, entries) {
6048 if (pf->opts & PF_OPT_VERBOSE)
6049 fprintf(stderr, "\nLoading anchor %s from %s\n",
6050 la->anchorname, la->filename);
6051 if (pfctl_rules(dev, la->filename, pf->opts, pf->optimize,
6052 la->anchorname, trans) == -1)
6060 rt_tableid_max(void)
6064 size_t l = sizeof(fibs);
6066 if (sysctlbyname("net.fibs", &fibs, &l, NULL, 0) == -1)
6067 fibs = 16; /* XXX RT_MAXFIBS, at least limit it some. */
6069 * As the OpenBSD code only compares > and not >= we need to adjust
6070 * here given we only accept values of 0..n and want to avoid #ifdefs
6075 return (RT_TABLEID_MAX);