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 <net/altq/altq.h>
48 #include <net/altq/altq_cbq.h>
49 #include <net/altq/altq_codel.h>
50 #include <net/altq/altq_priq.h>
51 #include <net/altq/altq_hfsc.h>
52 #include <net/altq/altq_fairq.h>
69 #include "pfctl_parser.h"
72 static struct pfctl *pf = NULL;
74 static int rulestate = 0;
75 static u_int16_t returnicmpdefault =
76 (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
77 static u_int16_t returnicmp6default =
78 (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
79 static int blockpolicy = PFRULE_DROP;
80 static int require_order = 1;
81 static int default_statelock;
83 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
85 TAILQ_ENTRY(file) entry;
91 struct file *pushfile(const char *, int);
93 int check_file_secrecy(int, const char *);
96 int yyerror(const char *, ...);
97 int kw_cmp(const void *, const void *);
103 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
105 TAILQ_ENTRY(sym) entry;
111 int symset(const char *, const char *, int);
112 char *symget(const char *);
114 int atoul(char *, u_long *);
127 struct node_proto *next;
128 struct node_proto *tail;
134 struct node_port *next;
135 struct node_port *tail;
141 struct node_uid *next;
142 struct node_uid *tail;
148 struct node_gid *next;
149 struct node_gid *tail;
156 struct node_icmp *next;
157 struct node_icmp *tail;
160 enum { PF_STATE_OPT_MAX, PF_STATE_OPT_NOSYNC, PF_STATE_OPT_SRCTRACK,
161 PF_STATE_OPT_MAX_SRC_STATES, PF_STATE_OPT_MAX_SRC_CONN,
162 PF_STATE_OPT_MAX_SRC_CONN_RATE, PF_STATE_OPT_MAX_SRC_NODES,
163 PF_STATE_OPT_OVERLOAD, PF_STATE_OPT_STATELOCK,
164 PF_STATE_OPT_TIMEOUT, PF_STATE_OPT_SLOPPY, };
166 enum { PF_SRCTRACK_NONE, PF_SRCTRACK, PF_SRCTRACK_GLOBAL, PF_SRCTRACK_RULE };
168 struct node_state_opt {
171 u_int32_t max_states;
172 u_int32_t max_src_states;
173 u_int32_t max_src_conn;
180 char tblname[PF_TABLE_NAME_SIZE];
182 u_int32_t max_src_nodes;
190 struct node_state_opt *next;
191 struct node_state_opt *tail;
195 struct node_host *host;
196 struct node_port *port;
200 char queue[PF_QNAME_SIZE];
201 char parent[PF_QNAME_SIZE];
202 char ifname[IFNAMSIZ];
204 struct node_queue *next;
205 struct node_queue *tail;
208 struct node_qassign {
215 #define FOM_FLAGS 0x01
216 #define FOM_ICMP 0x02
218 #define FOM_KEEP 0x08
219 #define FOM_SRCTRACK 0x10
220 struct node_uid *uid;
221 struct node_gid *gid;
228 struct node_icmp *icmpspec;
233 struct node_state_opt *options;
238 struct node_qassign queues;
241 u_int8_t match_tag_not;
244 struct node_host *addr;
249 struct antispoof_opts {
256 #define SOM_MINTTL 0x01
257 #define SOM_MAXMSS 0x02
258 #define SOM_FRAGCACHE 0x04
259 #define SOM_SETTOS 0x08
268 u_int8_t match_tag_not;
274 #define QOM_BWSPEC 0x01
275 #define QOM_SCHEDULER 0x02
276 #define QOM_PRIORITY 0x04
277 #define QOM_TBRSIZE 0x08
278 #define QOM_QLIMIT 0x10
279 struct node_queue_bw queue_bwspec;
280 struct node_queue_opt scheduler;
289 struct node_tinithead init_nodes;
294 #define POM_TYPE 0x01
295 #define POM_STICKYADDRESS 0x02
299 struct pf_poolhashkey *key;
303 struct codel_opts codel_opts;
304 struct node_hfsc_opts hfsc_opts;
305 struct node_fairq_opts fairq_opts;
306 struct node_state_opt *keep_state_defaults = NULL;
308 int disallow_table(struct node_host *, const char *);
309 int disallow_urpf_failed(struct node_host *, const char *);
310 int disallow_alias(struct node_host *, const char *);
311 int rule_consistent(struct pf_rule *, int);
312 int filter_consistent(struct pf_rule *, int);
313 int nat_consistent(struct pf_rule *);
314 int rdr_consistent(struct pf_rule *);
315 int process_tabledef(char *, struct table_opts *);
316 void expand_label_str(char *, size_t, const char *, const char *);
317 void expand_label_if(const char *, char *, size_t, const char *);
318 void expand_label_addr(const char *, char *, size_t, u_int8_t,
320 void expand_label_port(const char *, char *, size_t,
322 void expand_label_proto(const char *, char *, size_t, u_int8_t);
323 void expand_label_nr(const char *, char *, size_t);
324 void expand_label(char *, size_t, const char *, u_int8_t,
325 struct node_host *, struct node_port *, struct node_host *,
326 struct node_port *, u_int8_t);
327 void expand_rule(struct pf_rule *, struct node_if *,
328 struct node_host *, struct node_proto *, struct node_os *,
329 struct node_host *, struct node_port *, struct node_host *,
330 struct node_port *, struct node_uid *, struct node_gid *,
331 struct node_icmp *, const char *);
332 int expand_altq(struct pf_altq *, struct node_if *,
333 struct node_queue *, struct node_queue_bw bwspec,
334 struct node_queue_opt *);
335 int expand_queue(struct pf_altq *, struct node_if *,
336 struct node_queue *, struct node_queue_bw,
337 struct node_queue_opt *);
338 int expand_skip_interface(struct node_if *);
340 int check_rulestate(int);
341 int getservice(char *);
342 int rule_label(struct pf_rule *, char *);
343 int rt_tableid_max(void);
345 void mv_rules(struct pf_ruleset *, struct pf_ruleset *);
346 void decide_address_family(struct node_host *, sa_family_t *);
347 void remove_invalid_hosts(struct node_host **, sa_family_t *);
348 int invalid_redirect(struct node_host *, sa_family_t);
349 u_int16_t parseicmpspec(char *, sa_family_t);
351 TAILQ_HEAD(loadanchorshead, loadanchors)
352 loadanchorshead = TAILQ_HEAD_INITIALIZER(loadanchorshead);
355 TAILQ_ENTRY(loadanchors) entries;
378 struct node_if *interface;
379 struct node_proto *proto;
380 struct node_icmp *icmp;
381 struct node_host *host;
383 struct node_port *port;
384 struct node_uid *uid;
385 struct node_gid *gid;
386 struct node_state_opt *state_opt;
389 struct peer src, dst;
390 struct node_os *src_os;
393 struct node_host *host;
397 struct pf_poolhashkey *key;
400 struct node_host *host;
405 struct node_state_opt *options;
416 struct pf_poolhashkey *hashkey;
417 struct node_queue *queue;
418 struct node_queue_opt queue_options;
419 struct node_queue_bw queue_bwspec;
420 struct node_qassign qassign;
421 struct filter_opts filter_opts;
422 struct antispoof_opts antispoof_opts;
423 struct queue_opts queue_opts;
424 struct scrub_opts scrub_opts;
425 struct table_opts table_opts;
426 struct pool_opts pool_opts;
427 struct node_hfsc_opts hfsc_opts;
428 struct node_fairq_opts fairq_opts;
429 struct codel_opts codel_opts;
434 #define PPORT_RANGE 1
436 int parseport(char *, struct range *r, int);
438 #define DYNIF_MULTIADDR(addr) ((addr).type == PF_ADDR_DYNIFTL && \
439 (!((addr).iflags & PFI_AFLAG_NOALIAS) || \
440 !isdigit((addr).v.ifname[strlen((addr).v.ifname)-1])))
444 %token PASS BLOCK SCRUB RETURN IN OS OUT LOG QUICK ON FROM TO FLAGS
445 %token RETURNRST RETURNICMP RETURNICMP6 PROTO INET INET6 ALL ANY ICMPTYPE
446 %token ICMP6TYPE CODE KEEP MODULATE STATE PORT RDR NAT BINAT ARROW NODF
447 %token MINTTL ERROR ALLOWOPTS FASTROUTE FILENAME ROUTETO DUPTO REPLYTO NO LABEL
448 %token NOROUTE URPFFAILED FRAGMENT USER GROUP MAXMSS MAXIMUM TTL TOS DROP TABLE
449 %token REASSEMBLE FRAGDROP FRAGCROP ANCHOR NATANCHOR RDRANCHOR BINATANCHOR
450 %token SET OPTIMIZATION TIMEOUT LIMIT LOGINTERFACE BLOCKPOLICY RANDOMID
451 %token REQUIREORDER SYNPROXY FINGERPRINTS NOSYNC DEBUG SKIP HOSTID
452 %token ANTISPOOF FOR INCLUDE
453 %token BITMASK RANDOM SOURCEHASH ROUNDROBIN STATICPORT PROBABILITY
454 %token ALTQ CBQ CODEL PRIQ HFSC FAIRQ BANDWIDTH TBRSIZE LINKSHARE REALTIME
455 %token UPPERLIMIT QUEUE PRIORITY QLIMIT HOGS BUCKETS RTABLE TARGET INTERVAL
456 %token LOAD RULESET_OPTIMIZATION
457 %token STICKYADDRESS MAXSRCSTATES MAXSRCNODES SOURCETRACK GLOBAL RULE
458 %token MAXSRCCONN MAXSRCCONNRATE OVERLOAD FLUSH SLOPPY
459 %token TAGGED TAG IFBOUND FLOATING STATEPOLICY STATEDEFAULTS ROUTE SETTOS
460 %token DIVERTTO DIVERTREPLY
461 %token <v.string> STRING
462 %token <v.number> NUMBER
463 %token <v.i> PORTBINARY
464 %type <v.interface> interface if_list if_item_not if_item
465 %type <v.number> number icmptype icmp6type uid gid
466 %type <v.number> tos not yesno
467 %type <v.probability> probability
468 %type <v.i> no dir af fragcache optimizer
469 %type <v.i> sourcetrack flush unaryop statelock
470 %type <v.b> action nataction natpasslog scrubaction
471 %type <v.b> flags flag blockspec
472 %type <v.range> portplain portstar portrange
473 %type <v.hashkey> hashkey
474 %type <v.proto> proto proto_list proto_item
475 %type <v.number> protoval
476 %type <v.icmp> icmpspec
477 %type <v.icmp> icmp_list icmp_item
478 %type <v.icmp> icmp6_list icmp6_item
479 %type <v.number> reticmpspec reticmp6spec
480 %type <v.fromto> fromto
481 %type <v.peer> ipportspec from to
482 %type <v.host> ipspec toipspec xhost host dynaddr host_list
483 %type <v.host> redir_host_list redirspec
484 %type <v.host> route_host route_host_list routespec
485 %type <v.os> os xos os_list
486 %type <v.port> portspec port_list port_item
487 %type <v.uid> uids uid_list uid_item
488 %type <v.gid> gids gid_list gid_item
489 %type <v.route> route
490 %type <v.redirection> redirection redirpool
491 %type <v.string> label stringall tag anchorname
492 %type <v.string> string varstring numberstring
493 %type <v.keep_state> keep
494 %type <v.state_opt> state_opt_spec state_opt_list state_opt_item
495 %type <v.logquick> logquick quick log logopts logopt
496 %type <v.interface> antispoof_ifspc antispoof_iflst antispoof_if
497 %type <v.qassign> qname
498 %type <v.queue> qassign qassign_list qassign_item
499 %type <v.queue_options> scheduler
500 %type <v.number> cbqflags_list cbqflags_item
501 %type <v.number> priqflags_list priqflags_item
502 %type <v.hfsc_opts> hfscopts_list hfscopts_item hfsc_opts
503 %type <v.fairq_opts> fairqopts_list fairqopts_item fairq_opts
504 %type <v.codel_opts> codelopts_list codelopts_item codel_opts
505 %type <v.queue_bwspec> bandwidth
506 %type <v.filter_opts> filter_opts filter_opt filter_opts_l
507 %type <v.antispoof_opts> antispoof_opts antispoof_opt antispoof_opts_l
508 %type <v.queue_opts> queue_opts queue_opt queue_opts_l
509 %type <v.scrub_opts> scrub_opts scrub_opt scrub_opts_l
510 %type <v.table_opts> table_opts table_opt table_opts_l
511 %type <v.pool_opts> pool_opts pool_opt pool_opts_l
512 %type <v.tagged> tagged
513 %type <v.rtableid> rtable
516 ruleset : /* empty */
517 | ruleset include '\n'
519 | ruleset option '\n'
520 | ruleset scrubrule '\n'
521 | ruleset natrule '\n'
522 | ruleset binatrule '\n'
523 | ruleset pfrule '\n'
524 | ruleset anchorrule '\n'
525 | ruleset loadrule '\n'
526 | ruleset altqif '\n'
527 | ruleset queuespec '\n'
528 | ruleset varset '\n'
529 | ruleset antispoof '\n'
530 | ruleset tabledef '\n'
531 | '{' fakeanchor '}' '\n';
532 | ruleset error '\n' { file->errors++; }
535 include : INCLUDE STRING {
538 if ((nfile = pushfile($2, 0)) == NULL) {
539 yyerror("failed to include file %s", $2);
551 * apply to previouslys specified rule: must be careful to note
552 * what that is: pf or nat or binat or rdr
554 fakeanchor : fakeanchor '\n'
555 | fakeanchor anchorrule '\n'
556 | fakeanchor binatrule '\n'
557 | fakeanchor natrule '\n'
558 | fakeanchor pfrule '\n'
559 | fakeanchor error '\n'
563 if (!strcmp($1, "none"))
565 else if (!strcmp($1, "basic"))
566 $$ = PF_OPTIMIZE_BASIC;
567 else if (!strcmp($1, "profile"))
568 $$ = PF_OPTIMIZE_BASIC | PF_OPTIMIZE_PROFILE;
570 yyerror("unknown ruleset-optimization %s", $1);
576 option : SET OPTIMIZATION STRING {
577 if (check_rulestate(PFCTL_STATE_OPTION)) {
581 if (pfctl_set_optimization(pf, $3) != 0) {
582 yyerror("unknown optimization %s", $3);
588 | SET RULESET_OPTIMIZATION optimizer {
589 if (!(pf->opts & PF_OPT_OPTIMIZE)) {
590 pf->opts |= PF_OPT_OPTIMIZE;
594 | SET TIMEOUT timeout_spec
595 | SET TIMEOUT '{' optnl timeout_list '}'
596 | SET LIMIT limit_spec
597 | SET LIMIT '{' optnl limit_list '}'
598 | SET LOGINTERFACE stringall {
599 if (check_rulestate(PFCTL_STATE_OPTION)) {
603 if (pfctl_set_logif(pf, $3) != 0) {
604 yyerror("error setting loginterface %s", $3);
610 | SET HOSTID number {
611 if ($3 == 0 || $3 > UINT_MAX) {
612 yyerror("hostid must be non-zero");
615 if (pfctl_set_hostid(pf, $3) != 0) {
616 yyerror("error setting hostid %08x", $3);
620 | SET BLOCKPOLICY DROP {
621 if (pf->opts & PF_OPT_VERBOSE)
622 printf("set block-policy drop\n");
623 if (check_rulestate(PFCTL_STATE_OPTION))
625 blockpolicy = PFRULE_DROP;
627 | SET BLOCKPOLICY RETURN {
628 if (pf->opts & PF_OPT_VERBOSE)
629 printf("set block-policy return\n");
630 if (check_rulestate(PFCTL_STATE_OPTION))
632 blockpolicy = PFRULE_RETURN;
634 | SET REQUIREORDER yesno {
635 if (pf->opts & PF_OPT_VERBOSE)
636 printf("set require-order %s\n",
637 $3 == 1 ? "yes" : "no");
640 | SET FINGERPRINTS STRING {
641 if (pf->opts & PF_OPT_VERBOSE)
642 printf("set fingerprints \"%s\"\n", $3);
643 if (check_rulestate(PFCTL_STATE_OPTION)) {
647 if (!pf->anchor->name[0]) {
648 if (pfctl_file_fingerprints(pf->dev,
650 yyerror("error loading "
651 "fingerprints %s", $3);
658 | SET STATEPOLICY statelock {
659 if (pf->opts & PF_OPT_VERBOSE)
662 printf("set state-policy floating\n");
665 printf("set state-policy if-bound\n");
668 default_statelock = $3;
671 if (check_rulestate(PFCTL_STATE_OPTION)) {
675 if (pfctl_set_debug(pf, $3) != 0) {
676 yyerror("error setting debuglevel %s", $3);
682 | SET SKIP interface {
683 if (expand_skip_interface($3) != 0) {
684 yyerror("error setting skip interface(s)");
688 | SET STATEDEFAULTS state_opt_list {
689 if (keep_state_defaults != NULL) {
690 yyerror("cannot redefine state-defaults");
693 keep_state_defaults = $3;
697 stringall : STRING { $$ = $1; }
699 if (($$ = strdup("all")) == NULL) {
700 err(1, "stringall: strdup");
705 string : STRING string {
706 if (asprintf(&$$, "%s %s", $1, $2) == -1)
707 err(1, "string: asprintf");
714 varstring : numberstring varstring {
715 if (asprintf(&$$, "%s %s", $1, $2) == -1)
716 err(1, "string: asprintf");
723 numberstring : NUMBER {
725 if (asprintf(&s, "%lld", (long long)$1) == -1) {
726 yyerror("string: asprintf");
734 varset : STRING '=' varstring {
735 if (pf->opts & PF_OPT_VERBOSE)
736 printf("%s = \"%s\"\n", $1, $3);
737 if (symset($1, $3, 0) == -1)
738 err(1, "cannot store variable %s", $1);
744 anchorname : STRING { $$ = $1; }
745 | /* empty */ { $$ = NULL; }
748 pfa_anchorlist : /* empty */
749 | pfa_anchorlist '\n'
750 | pfa_anchorlist pfrule '\n'
751 | pfa_anchorlist anchorrule '\n'
756 char ta[PF_ANCHOR_NAME_SIZE];
757 struct pf_ruleset *rs;
759 /* steping into a brace anchor */
764 /* create a holding ruleset in the root */
765 snprintf(ta, PF_ANCHOR_NAME_SIZE, "_%d", pf->bn);
766 rs = pf_find_or_create_ruleset(ta);
768 err(1, "pfa_anchor: pf_find_or_create_ruleset");
769 pf->astack[pf->asd] = rs->anchor;
770 pf->anchor = rs->anchor;
771 } '\n' pfa_anchorlist '}'
773 pf->alast = pf->anchor;
775 pf->anchor = pf->astack[pf->asd];
780 anchorrule : ANCHOR anchorname dir quick interface af proto fromto
781 filter_opts pfa_anchor
784 struct node_proto *proto;
786 if (check_rulestate(PFCTL_STATE_FILTER)) {
792 if ($2 && ($2[0] == '_' || strstr($2, "/_") != NULL)) {
794 yyerror("anchor names beginning with '_' "
795 "are reserved for internal use");
799 memset(&r, 0, sizeof(r));
800 if (pf->astack[pf->asd + 1]) {
801 /* move inline rules into relative location */
803 &pf->astack[pf->asd]->ruleset,
804 $2 ? $2 : pf->alast->name);
806 if (r.anchor == NULL)
807 err(1, "anchorrule: unable to "
810 if (pf->alast != r.anchor) {
811 if (r.anchor->match) {
812 yyerror("inline anchor '%s' "
817 mv_rules(&pf->alast->ruleset,
820 pf_remove_if_empty_ruleset(&pf->alast->ruleset);
821 pf->alast = r.anchor;
824 yyerror("anchors without explicit "
825 "rules must specify a name");
833 r.rtableid = $9.rtableid;
836 if (strlcpy(r.tagname, $9.tag,
837 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
838 yyerror("tag too long, max %u chars",
839 PF_TAG_NAME_SIZE - 1);
843 if (strlcpy(r.match_tagname, $9.match_tag,
844 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
845 yyerror("tag too long, max %u chars",
846 PF_TAG_NAME_SIZE - 1);
849 r.match_tag_not = $9.match_tag_not;
850 if (rule_label(&r, $9.label))
853 r.flags = $9.flags.b1;
854 r.flagset = $9.flags.b2;
855 if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
856 yyerror("flags always false");
859 if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
860 for (proto = $7; proto != NULL &&
861 proto->proto != IPPROTO_TCP;
864 if (proto == NULL && $7 != NULL) {
865 if ($9.flags.b1 || $9.flags.b2)
867 "flags only apply to tcp");
870 "OS fingerprinting only "
878 if ($9.keep.action) {
879 yyerror("cannot specify state handling "
885 if (strlcpy(r.match_tagname, $9.match_tag,
886 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
887 yyerror("tag too long, max %u chars",
888 PF_TAG_NAME_SIZE - 1);
891 r.match_tag_not = $9.match_tag_not;
893 decide_address_family($8.src.host, &r.af);
894 decide_address_family($8.dst.host, &r.af);
896 expand_rule(&r, $5, NULL, $7, $8.src_os,
897 $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
898 $9.uid, $9.gid, $9.icmpspec,
899 pf->astack[pf->asd + 1] ? pf->alast->name : $2);
901 pf->astack[pf->asd + 1] = NULL;
903 | NATANCHOR string interface af proto fromto rtable {
906 if (check_rulestate(PFCTL_STATE_NAT)) {
911 memset(&r, 0, sizeof(r));
916 decide_address_family($6.src.host, &r.af);
917 decide_address_family($6.dst.host, &r.af);
919 expand_rule(&r, $3, NULL, $5, $6.src_os,
920 $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
924 | RDRANCHOR string interface af proto fromto rtable {
927 if (check_rulestate(PFCTL_STATE_NAT)) {
932 memset(&r, 0, sizeof(r));
937 decide_address_family($6.src.host, &r.af);
938 decide_address_family($6.dst.host, &r.af);
940 if ($6.src.port != NULL) {
941 yyerror("source port parameter not supported"
945 if ($6.dst.port != NULL) {
946 if ($6.dst.port->next != NULL) {
947 yyerror("destination port list "
948 "expansion not supported in "
951 } else if ($6.dst.port->op != PF_OP_EQ) {
952 yyerror("destination port operators"
953 " not supported in rdr-anchor");
956 r.dst.port[0] = $6.dst.port->port[0];
957 r.dst.port[1] = $6.dst.port->port[1];
958 r.dst.port_op = $6.dst.port->op;
961 expand_rule(&r, $3, NULL, $5, $6.src_os,
962 $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
966 | BINATANCHOR string interface af proto fromto rtable {
969 if (check_rulestate(PFCTL_STATE_NAT)) {
974 memset(&r, 0, sizeof(r));
979 if ($5->next != NULL) {
980 yyerror("proto list expansion"
981 " not supported in binat-anchor");
988 if ($6.src.host != NULL || $6.src.port != NULL ||
989 $6.dst.host != NULL || $6.dst.port != NULL) {
990 yyerror("fromto parameter not supported"
995 decide_address_family($6.src.host, &r.af);
996 decide_address_family($6.dst.host, &r.af);
998 pfctl_add_rule(pf, &r, $2);
1003 loadrule : LOAD ANCHOR string FROM string {
1004 struct loadanchors *loadanchor;
1006 if (strlen(pf->anchor->name) + 1 +
1007 strlen($3) >= MAXPATHLEN) {
1008 yyerror("anchorname %s too long, max %u\n",
1009 $3, MAXPATHLEN - 1);
1013 loadanchor = calloc(1, sizeof(struct loadanchors));
1014 if (loadanchor == NULL)
1015 err(1, "loadrule: calloc");
1016 if ((loadanchor->anchorname = malloc(MAXPATHLEN)) ==
1018 err(1, "loadrule: malloc");
1019 if (pf->anchor->name[0])
1020 snprintf(loadanchor->anchorname, MAXPATHLEN,
1021 "%s/%s", pf->anchor->name, $3);
1023 strlcpy(loadanchor->anchorname, $3, MAXPATHLEN);
1024 if ((loadanchor->filename = strdup($5)) == NULL)
1025 err(1, "loadrule: strdup");
1027 TAILQ_INSERT_TAIL(&loadanchorshead, loadanchor,
1034 scrubaction : no SCRUB {
1043 scrubrule : scrubaction dir logquick interface af proto fromto scrub_opts
1047 if (check_rulestate(PFCTL_STATE_SCRUB))
1050 memset(&r, 0, sizeof(r));
1058 yyerror("scrub rules do not support 'quick'");
1064 r.rule_flag |= PFRULE_NODF;
1066 r.rule_flag |= PFRULE_RANDOMID;
1067 if ($8.reassemble_tcp) {
1068 if (r.direction != PF_INOUT) {
1069 yyerror("reassemble tcp rules can not "
1070 "specify direction");
1073 r.rule_flag |= PFRULE_REASSEMBLE_TCP;
1076 r.min_ttl = $8.minttl;
1078 r.max_mss = $8.maxmss;
1079 if ($8.marker & SOM_SETTOS) {
1080 r.rule_flag |= PFRULE_SET_TOS;
1081 r.set_tos = $8.settos;
1084 r.rule_flag |= $8.fragcache;
1086 if (strlcpy(r.match_tagname, $8.match_tag,
1087 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1088 yyerror("tag too long, max %u chars",
1089 PF_TAG_NAME_SIZE - 1);
1092 r.match_tag_not = $8.match_tag_not;
1093 r.rtableid = $8.rtableid;
1095 expand_rule(&r, $4, NULL, $6, $7.src_os,
1096 $7.src.host, $7.src.port, $7.dst.host, $7.dst.port,
1097 NULL, NULL, NULL, "");
1102 bzero(&scrub_opts, sizeof scrub_opts);
1103 scrub_opts.rtableid = -1;
1106 { $$ = scrub_opts; }
1108 bzero(&scrub_opts, sizeof scrub_opts);
1109 scrub_opts.rtableid = -1;
1114 scrub_opts_l : scrub_opts_l scrub_opt
1119 if (scrub_opts.nodf) {
1120 yyerror("no-df cannot be respecified");
1123 scrub_opts.nodf = 1;
1126 if (scrub_opts.marker & SOM_MINTTL) {
1127 yyerror("min-ttl cannot be respecified");
1130 if ($2 < 0 || $2 > 255) {
1131 yyerror("illegal min-ttl value %d", $2);
1134 scrub_opts.marker |= SOM_MINTTL;
1135 scrub_opts.minttl = $2;
1138 if (scrub_opts.marker & SOM_MAXMSS) {
1139 yyerror("max-mss cannot be respecified");
1142 if ($2 < 0 || $2 > 65535) {
1143 yyerror("illegal max-mss value %d", $2);
1146 scrub_opts.marker |= SOM_MAXMSS;
1147 scrub_opts.maxmss = $2;
1150 if (scrub_opts.marker & SOM_SETTOS) {
1151 yyerror("set-tos cannot be respecified");
1154 scrub_opts.marker |= SOM_SETTOS;
1155 scrub_opts.settos = $2;
1158 if (scrub_opts.marker & SOM_FRAGCACHE) {
1159 yyerror("fragcache cannot be respecified");
1162 scrub_opts.marker |= SOM_FRAGCACHE;
1163 scrub_opts.fragcache = $1;
1165 | REASSEMBLE STRING {
1166 if (strcasecmp($2, "tcp") != 0) {
1167 yyerror("scrub reassemble supports only tcp, "
1173 if (scrub_opts.reassemble_tcp) {
1174 yyerror("reassemble tcp cannot be respecified");
1177 scrub_opts.reassemble_tcp = 1;
1180 if (scrub_opts.randomid) {
1181 yyerror("random-id cannot be respecified");
1184 scrub_opts.randomid = 1;
1187 if ($2 < 0 || $2 > rt_tableid_max()) {
1188 yyerror("invalid rtable id");
1191 scrub_opts.rtableid = $2;
1193 | not TAGGED string {
1194 scrub_opts.match_tag = $3;
1195 scrub_opts.match_tag_not = $1;
1199 fragcache : FRAGMENT REASSEMBLE { $$ = 0; /* default */ }
1200 | FRAGMENT FRAGCROP { $$ = 0; }
1201 | FRAGMENT FRAGDROP { $$ = 0; }
1204 antispoof : ANTISPOOF logquick antispoof_ifspc af antispoof_opts {
1206 struct node_host *h = NULL, *hh;
1207 struct node_if *i, *j;
1209 if (check_rulestate(PFCTL_STATE_FILTER))
1212 for (i = $3; i; i = i->next) {
1213 bzero(&r, sizeof(r));
1216 r.direction = PF_IN;
1221 if (rule_label(&r, $5.label))
1223 r.rtableid = $5.rtableid;
1224 j = calloc(1, sizeof(struct node_if));
1226 err(1, "antispoof: calloc");
1227 if (strlcpy(j->ifname, i->ifname,
1228 sizeof(j->ifname)) >= sizeof(j->ifname)) {
1230 yyerror("interface name too long");
1235 h = calloc(1, sizeof(*h));
1237 err(1, "address: calloc");
1238 h->addr.type = PF_ADDR_DYNIFTL;
1240 if (strlcpy(h->addr.v.ifname, i->ifname,
1241 sizeof(h->addr.v.ifname)) >=
1242 sizeof(h->addr.v.ifname)) {
1245 "interface name too long");
1248 hh = malloc(sizeof(*hh));
1250 err(1, "address: malloc");
1251 bcopy(h, hh, sizeof(*hh));
1252 h->addr.iflags = PFI_AFLAG_NETWORK;
1254 h = ifa_lookup(j->ifname,
1260 expand_rule(&r, j, NULL, NULL, NULL, h,
1261 NULL, NULL, NULL, NULL, NULL,
1264 if ((i->ifa_flags & IFF_LOOPBACK) == 0) {
1265 bzero(&r, sizeof(r));
1268 r.direction = PF_IN;
1273 if (rule_label(&r, $5.label))
1275 r.rtableid = $5.rtableid;
1279 h = ifa_lookup(i->ifname, 0);
1281 expand_rule(&r, NULL, NULL,
1282 NULL, NULL, h, NULL, NULL,
1283 NULL, NULL, NULL, NULL, "");
1291 antispoof_ifspc : FOR antispoof_if { $$ = $2; }
1292 | FOR '{' optnl antispoof_iflst '}' { $$ = $4; }
1295 antispoof_iflst : antispoof_if optnl { $$ = $1; }
1296 | antispoof_iflst comma antispoof_if optnl {
1297 $1->tail->next = $3;
1303 antispoof_if : if_item { $$ = $1; }
1311 bzero(&antispoof_opts, sizeof antispoof_opts);
1312 antispoof_opts.rtableid = -1;
1315 { $$ = antispoof_opts; }
1317 bzero(&antispoof_opts, sizeof antispoof_opts);
1318 antispoof_opts.rtableid = -1;
1319 $$ = antispoof_opts;
1323 antispoof_opts_l : antispoof_opts_l antispoof_opt
1327 antispoof_opt : label {
1328 if (antispoof_opts.label) {
1329 yyerror("label cannot be redefined");
1332 antispoof_opts.label = $1;
1335 if ($2 < 0 || $2 > rt_tableid_max()) {
1336 yyerror("invalid rtable id");
1339 antispoof_opts.rtableid = $2;
1343 not : '!' { $$ = 1; }
1344 | /* empty */ { $$ = 0; }
1347 tabledef : TABLE '<' STRING '>' table_opts {
1348 struct node_host *h, *nh;
1349 struct node_tinit *ti, *nti;
1351 if (strlen($3) >= PF_TABLE_NAME_SIZE) {
1352 yyerror("table name too long, max %d chars",
1353 PF_TABLE_NAME_SIZE - 1);
1357 if (pf->loadopt & PFCTL_FLAG_TABLE)
1358 if (process_tabledef($3, &$5)) {
1363 for (ti = SIMPLEQ_FIRST(&$5.init_nodes);
1364 ti != SIMPLEQ_END(&$5.init_nodes); ti = nti) {
1367 for (h = ti->host; h != NULL; h = nh) {
1371 nti = SIMPLEQ_NEXT(ti, entries);
1378 bzero(&table_opts, sizeof table_opts);
1379 SIMPLEQ_INIT(&table_opts.init_nodes);
1382 { $$ = table_opts; }
1385 bzero(&table_opts, sizeof table_opts);
1386 SIMPLEQ_INIT(&table_opts.init_nodes);
1391 table_opts_l : table_opts_l table_opt
1395 table_opt : STRING {
1396 if (!strcmp($1, "const"))
1397 table_opts.flags |= PFR_TFLAG_CONST;
1398 else if (!strcmp($1, "persist"))
1399 table_opts.flags |= PFR_TFLAG_PERSIST;
1400 else if (!strcmp($1, "counters"))
1401 table_opts.flags |= PFR_TFLAG_COUNTERS;
1403 yyerror("invalid table option '%s'", $1);
1409 | '{' optnl '}' { table_opts.init_addr = 1; }
1410 | '{' optnl host_list '}' {
1411 struct node_host *n;
1412 struct node_tinit *ti;
1414 for (n = $3; n != NULL; n = n->next) {
1415 switch (n->addr.type) {
1416 case PF_ADDR_ADDRMASK:
1419 yyerror("address ranges are not "
1420 "permitted inside tables");
1422 case PF_ADDR_DYNIFTL:
1423 yyerror("dynamic addresses are not "
1424 "permitted inside tables");
1427 yyerror("tables cannot contain tables");
1429 case PF_ADDR_NOROUTE:
1430 yyerror("\"no-route\" is not permitted "
1433 case PF_ADDR_URPFFAILED:
1434 yyerror("\"urpf-failed\" is not "
1435 "permitted inside tables");
1438 yyerror("unknown address type %d",
1443 if (!(ti = calloc(1, sizeof(*ti))))
1444 err(1, "table_opt: calloc");
1446 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1448 table_opts.init_addr = 1;
1451 struct node_tinit *ti;
1453 if (!(ti = calloc(1, sizeof(*ti))))
1454 err(1, "table_opt: calloc");
1456 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1458 table_opts.init_addr = 1;
1462 altqif : ALTQ interface queue_opts QUEUE qassign {
1465 if (check_rulestate(PFCTL_STATE_QUEUE))
1468 memset(&a, 0, sizeof(a));
1469 if ($3.scheduler.qtype == ALTQT_NONE) {
1470 yyerror("no scheduler specified!");
1473 a.scheduler = $3.scheduler.qtype;
1474 a.qlimit = $3.qlimit;
1475 a.tbrsize = $3.tbrsize;
1476 if ($5 == NULL && $3.scheduler.qtype != ALTQT_CODEL) {
1477 yyerror("no child queues specified");
1480 if (expand_altq(&a, $2, $5, $3.queue_bwspec,
1486 queuespec : QUEUE STRING interface queue_opts qassign {
1489 if (check_rulestate(PFCTL_STATE_QUEUE)) {
1494 memset(&a, 0, sizeof(a));
1496 if (strlcpy(a.qname, $2, sizeof(a.qname)) >=
1498 yyerror("queue name too long (max "
1499 "%d chars)", PF_QNAME_SIZE-1);
1505 yyerror("cannot specify tbrsize for queue");
1508 if ($4.priority > 255) {
1509 yyerror("priority out of range: max 255");
1512 a.priority = $4.priority;
1513 a.qlimit = $4.qlimit;
1514 a.scheduler = $4.scheduler.qtype;
1515 if (expand_queue(&a, $3, $5, $4.queue_bwspec,
1517 yyerror("errors in queue definition");
1524 bzero(&queue_opts, sizeof queue_opts);
1525 queue_opts.priority = DEFAULT_PRIORITY;
1526 queue_opts.qlimit = DEFAULT_QLIMIT;
1527 queue_opts.scheduler.qtype = ALTQT_NONE;
1528 queue_opts.queue_bwspec.bw_percent = 100;
1531 { $$ = queue_opts; }
1533 bzero(&queue_opts, sizeof queue_opts);
1534 queue_opts.priority = DEFAULT_PRIORITY;
1535 queue_opts.qlimit = DEFAULT_QLIMIT;
1536 queue_opts.scheduler.qtype = ALTQT_NONE;
1537 queue_opts.queue_bwspec.bw_percent = 100;
1542 queue_opts_l : queue_opts_l queue_opt
1546 queue_opt : BANDWIDTH bandwidth {
1547 if (queue_opts.marker & QOM_BWSPEC) {
1548 yyerror("bandwidth cannot be respecified");
1551 queue_opts.marker |= QOM_BWSPEC;
1552 queue_opts.queue_bwspec = $2;
1555 if (queue_opts.marker & QOM_PRIORITY) {
1556 yyerror("priority cannot be respecified");
1559 if ($2 < 0 || $2 > 255) {
1560 yyerror("priority out of range: max 255");
1563 queue_opts.marker |= QOM_PRIORITY;
1564 queue_opts.priority = $2;
1567 if (queue_opts.marker & QOM_QLIMIT) {
1568 yyerror("qlimit cannot be respecified");
1571 if ($2 < 0 || $2 > 65535) {
1572 yyerror("qlimit out of range: max 65535");
1575 queue_opts.marker |= QOM_QLIMIT;
1576 queue_opts.qlimit = $2;
1579 if (queue_opts.marker & QOM_SCHEDULER) {
1580 yyerror("scheduler cannot be respecified");
1583 queue_opts.marker |= QOM_SCHEDULER;
1584 queue_opts.scheduler = $1;
1587 if (queue_opts.marker & QOM_TBRSIZE) {
1588 yyerror("tbrsize cannot be respecified");
1591 if ($2 < 0 || $2 > 65535) {
1592 yyerror("tbrsize too big: max 65535");
1595 queue_opts.marker |= QOM_TBRSIZE;
1596 queue_opts.tbrsize = $2;
1600 bandwidth : STRING {
1606 bps = strtod($1, &cp);
1608 if (!strcmp(cp, "b"))
1610 else if (!strcmp(cp, "Kb"))
1612 else if (!strcmp(cp, "Mb"))
1614 else if (!strcmp(cp, "Gb"))
1615 bps *= 1000 * 1000 * 1000;
1616 else if (!strcmp(cp, "%")) {
1617 if (bps < 0 || bps > 100) {
1618 yyerror("bandwidth spec "
1623 $$.bw_percent = bps;
1626 yyerror("unknown unit %s", cp);
1632 $$.bw_absolute = (u_int32_t)bps;
1635 if ($1 < 0 || $1 > UINT_MAX) {
1636 yyerror("bandwidth number too big");
1640 $$.bw_absolute = $1;
1645 $$.qtype = ALTQT_CBQ;
1646 $$.data.cbq_opts.flags = 0;
1648 | CBQ '(' cbqflags_list ')' {
1649 $$.qtype = ALTQT_CBQ;
1650 $$.data.cbq_opts.flags = $3;
1653 $$.qtype = ALTQT_PRIQ;
1654 $$.data.priq_opts.flags = 0;
1656 | PRIQ '(' priqflags_list ')' {
1657 $$.qtype = ALTQT_PRIQ;
1658 $$.data.priq_opts.flags = $3;
1661 $$.qtype = ALTQT_HFSC;
1662 bzero(&$$.data.hfsc_opts,
1663 sizeof(struct node_hfsc_opts));
1665 | HFSC '(' hfsc_opts ')' {
1666 $$.qtype = ALTQT_HFSC;
1667 $$.data.hfsc_opts = $3;
1670 $$.qtype = ALTQT_FAIRQ;
1671 bzero(&$$.data.fairq_opts,
1672 sizeof(struct node_fairq_opts));
1674 | FAIRQ '(' fairq_opts ')' {
1675 $$.qtype = ALTQT_FAIRQ;
1676 $$.data.fairq_opts = $3;
1679 $$.qtype = ALTQT_CODEL;
1680 bzero(&$$.data.codel_opts,
1681 sizeof(struct codel_opts));
1683 | CODEL '(' codel_opts ')' {
1684 $$.qtype = ALTQT_CODEL;
1685 $$.data.codel_opts = $3;
1689 cbqflags_list : cbqflags_item { $$ |= $1; }
1690 | cbqflags_list comma cbqflags_item { $$ |= $3; }
1693 cbqflags_item : STRING {
1694 if (!strcmp($1, "default"))
1695 $$ = CBQCLF_DEFCLASS;
1696 else if (!strcmp($1, "borrow"))
1698 else if (!strcmp($1, "red"))
1700 else if (!strcmp($1, "ecn"))
1701 $$ = CBQCLF_RED|CBQCLF_ECN;
1702 else if (!strcmp($1, "rio"))
1704 else if (!strcmp($1, "codel"))
1707 yyerror("unknown cbq flag \"%s\"", $1);
1715 priqflags_list : priqflags_item { $$ |= $1; }
1716 | priqflags_list comma priqflags_item { $$ |= $3; }
1719 priqflags_item : STRING {
1720 if (!strcmp($1, "default"))
1721 $$ = PRCF_DEFAULTCLASS;
1722 else if (!strcmp($1, "red"))
1724 else if (!strcmp($1, "ecn"))
1725 $$ = PRCF_RED|PRCF_ECN;
1726 else if (!strcmp($1, "rio"))
1728 else if (!strcmp($1, "codel"))
1731 yyerror("unknown priq flag \"%s\"", $1);
1741 sizeof(struct node_hfsc_opts));
1748 hfscopts_list : hfscopts_item
1749 | hfscopts_list comma hfscopts_item
1752 hfscopts_item : LINKSHARE bandwidth {
1753 if (hfsc_opts.linkshare.used) {
1754 yyerror("linkshare already specified");
1757 hfsc_opts.linkshare.m2 = $2;
1758 hfsc_opts.linkshare.used = 1;
1760 | LINKSHARE '(' bandwidth comma NUMBER comma bandwidth ')'
1762 if ($5 < 0 || $5 > INT_MAX) {
1763 yyerror("timing in curve out of range");
1766 if (hfsc_opts.linkshare.used) {
1767 yyerror("linkshare already specified");
1770 hfsc_opts.linkshare.m1 = $3;
1771 hfsc_opts.linkshare.d = $5;
1772 hfsc_opts.linkshare.m2 = $7;
1773 hfsc_opts.linkshare.used = 1;
1775 | REALTIME bandwidth {
1776 if (hfsc_opts.realtime.used) {
1777 yyerror("realtime already specified");
1780 hfsc_opts.realtime.m2 = $2;
1781 hfsc_opts.realtime.used = 1;
1783 | REALTIME '(' bandwidth comma NUMBER comma bandwidth ')'
1785 if ($5 < 0 || $5 > INT_MAX) {
1786 yyerror("timing in curve out of range");
1789 if (hfsc_opts.realtime.used) {
1790 yyerror("realtime already specified");
1793 hfsc_opts.realtime.m1 = $3;
1794 hfsc_opts.realtime.d = $5;
1795 hfsc_opts.realtime.m2 = $7;
1796 hfsc_opts.realtime.used = 1;
1798 | UPPERLIMIT bandwidth {
1799 if (hfsc_opts.upperlimit.used) {
1800 yyerror("upperlimit already specified");
1803 hfsc_opts.upperlimit.m2 = $2;
1804 hfsc_opts.upperlimit.used = 1;
1806 | UPPERLIMIT '(' bandwidth comma NUMBER comma bandwidth ')'
1808 if ($5 < 0 || $5 > INT_MAX) {
1809 yyerror("timing in curve out of range");
1812 if (hfsc_opts.upperlimit.used) {
1813 yyerror("upperlimit already specified");
1816 hfsc_opts.upperlimit.m1 = $3;
1817 hfsc_opts.upperlimit.d = $5;
1818 hfsc_opts.upperlimit.m2 = $7;
1819 hfsc_opts.upperlimit.used = 1;
1822 if (!strcmp($1, "default"))
1823 hfsc_opts.flags |= HFCF_DEFAULTCLASS;
1824 else if (!strcmp($1, "red"))
1825 hfsc_opts.flags |= HFCF_RED;
1826 else if (!strcmp($1, "ecn"))
1827 hfsc_opts.flags |= HFCF_RED|HFCF_ECN;
1828 else if (!strcmp($1, "rio"))
1829 hfsc_opts.flags |= HFCF_RIO;
1830 else if (!strcmp($1, "codel"))
1831 hfsc_opts.flags |= HFCF_CODEL;
1833 yyerror("unknown hfsc flag \"%s\"", $1);
1843 sizeof(struct node_fairq_opts));
1850 fairqopts_list : fairqopts_item
1851 | fairqopts_list comma fairqopts_item
1854 fairqopts_item : LINKSHARE bandwidth {
1855 if (fairq_opts.linkshare.used) {
1856 yyerror("linkshare already specified");
1859 fairq_opts.linkshare.m2 = $2;
1860 fairq_opts.linkshare.used = 1;
1862 | LINKSHARE '(' bandwidth number bandwidth ')' {
1863 if (fairq_opts.linkshare.used) {
1864 yyerror("linkshare already specified");
1867 fairq_opts.linkshare.m1 = $3;
1868 fairq_opts.linkshare.d = $4;
1869 fairq_opts.linkshare.m2 = $5;
1870 fairq_opts.linkshare.used = 1;
1873 fairq_opts.hogs_bw = $2;
1876 fairq_opts.nbuckets = $2;
1879 if (!strcmp($1, "default"))
1880 fairq_opts.flags |= FARF_DEFAULTCLASS;
1881 else if (!strcmp($1, "red"))
1882 fairq_opts.flags |= FARF_RED;
1883 else if (!strcmp($1, "ecn"))
1884 fairq_opts.flags |= FARF_RED|FARF_ECN;
1885 else if (!strcmp($1, "rio"))
1886 fairq_opts.flags |= FARF_RIO;
1887 else if (!strcmp($1, "codel"))
1888 fairq_opts.flags |= FARF_CODEL;
1890 yyerror("unknown fairq flag \"%s\"", $1);
1900 sizeof(struct codel_opts));
1907 codelopts_list : codelopts_item
1908 | codelopts_list comma codelopts_item
1911 codelopts_item : INTERVAL number {
1912 if (codel_opts.interval) {
1913 yyerror("interval already specified");
1916 codel_opts.interval = $2;
1919 if (codel_opts.target) {
1920 yyerror("target already specified");
1923 codel_opts.target = $2;
1926 if (!strcmp($1, "ecn"))
1929 yyerror("unknown codel option \"%s\"", $1);
1937 qassign : /* empty */ { $$ = NULL; }
1938 | qassign_item { $$ = $1; }
1939 | '{' optnl qassign_list '}' { $$ = $3; }
1942 qassign_list : qassign_item optnl { $$ = $1; }
1943 | qassign_list comma qassign_item optnl {
1944 $1->tail->next = $3;
1950 qassign_item : STRING {
1951 $$ = calloc(1, sizeof(struct node_queue));
1953 err(1, "qassign_item: calloc");
1954 if (strlcpy($$->queue, $1, sizeof($$->queue)) >=
1955 sizeof($$->queue)) {
1956 yyerror("queue name '%s' too long (max "
1957 "%d chars)", $1, sizeof($$->queue)-1);
1968 pfrule : action dir logquick interface route af proto fromto
1972 struct node_state_opt *o;
1973 struct node_proto *proto;
1979 if (check_rulestate(PFCTL_STATE_FILTER))
1982 memset(&r, 0, sizeof(r));
1986 case PFRULE_RETURNRST:
1987 r.rule_flag |= PFRULE_RETURNRST;
1988 r.return_ttl = $1.w;
1990 case PFRULE_RETURNICMP:
1991 r.rule_flag |= PFRULE_RETURNICMP;
1992 r.return_icmp = $1.w;
1993 r.return_icmp6 = $1.w2;
1996 r.rule_flag |= PFRULE_RETURN;
1997 r.return_icmp = $1.w;
1998 r.return_icmp6 = $1.w2;
2006 r.rtableid = $9.rtableid;
2010 if (strlcpy(r.tagname, $9.tag,
2011 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
2012 yyerror("tag too long, max %u chars",
2013 PF_TAG_NAME_SIZE - 1);
2017 if (strlcpy(r.match_tagname, $9.match_tag,
2018 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
2019 yyerror("tag too long, max %u chars",
2020 PF_TAG_NAME_SIZE - 1);
2023 r.match_tag_not = $9.match_tag_not;
2024 if (rule_label(&r, $9.label))
2027 r.flags = $9.flags.b1;
2028 r.flagset = $9.flags.b2;
2029 if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
2030 yyerror("flags always false");
2033 if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
2034 for (proto = $7; proto != NULL &&
2035 proto->proto != IPPROTO_TCP;
2036 proto = proto->next)
2038 if (proto == NULL && $7 != NULL) {
2039 if ($9.flags.b1 || $9.flags.b2)
2041 "flags only apply to tcp");
2044 "OS fingerprinting only "
2049 if (($9.flags.b1 & parse_flags("S")) == 0 &&
2051 yyerror("OS fingerprinting requires "
2052 "the SYN TCP flag (flags S/SA)");
2059 r.keep_state = $9.keep.action;
2060 o = $9.keep.options;
2062 /* 'keep state' by default on pass rules. */
2063 if (!r.keep_state && !r.action &&
2064 !($9.marker & FOM_KEEP)) {
2065 r.keep_state = PF_STATE_NORMAL;
2066 o = keep_state_defaults;
2071 struct node_state_opt *p = o;
2074 case PF_STATE_OPT_MAX:
2076 yyerror("state option 'max' "
2077 "multiple definitions");
2080 r.max_states = o->data.max_states;
2082 case PF_STATE_OPT_NOSYNC:
2083 if (r.rule_flag & PFRULE_NOSYNC) {
2084 yyerror("state option 'sync' "
2085 "multiple definitions");
2088 r.rule_flag |= PFRULE_NOSYNC;
2090 case PF_STATE_OPT_SRCTRACK:
2092 yyerror("state option "
2094 "multiple definitions");
2097 srctrack = o->data.src_track;
2098 r.rule_flag |= PFRULE_SRCTRACK;
2100 case PF_STATE_OPT_MAX_SRC_STATES:
2101 if (r.max_src_states) {
2102 yyerror("state option "
2104 "multiple definitions");
2107 if (o->data.max_src_states == 0) {
2108 yyerror("'max-src-states' must "
2113 o->data.max_src_states;
2114 r.rule_flag |= PFRULE_SRCTRACK;
2116 case PF_STATE_OPT_OVERLOAD:
2117 if (r.overload_tblname[0]) {
2118 yyerror("multiple 'overload' "
2119 "table definitions");
2122 if (strlcpy(r.overload_tblname,
2123 o->data.overload.tblname,
2124 PF_TABLE_NAME_SIZE) >=
2125 PF_TABLE_NAME_SIZE) {
2126 yyerror("state option: "
2130 r.flush = o->data.overload.flush;
2132 case PF_STATE_OPT_MAX_SRC_CONN:
2133 if (r.max_src_conn) {
2134 yyerror("state option "
2136 "multiple definitions");
2139 if (o->data.max_src_conn == 0) {
2140 yyerror("'max-src-conn' "
2145 o->data.max_src_conn;
2146 r.rule_flag |= PFRULE_SRCTRACK |
2147 PFRULE_RULESRCTRACK;
2149 case PF_STATE_OPT_MAX_SRC_CONN_RATE:
2150 if (r.max_src_conn_rate.limit) {
2151 yyerror("state option "
2152 "'max-src-conn-rate' "
2153 "multiple definitions");
2156 if (!o->data.max_src_conn_rate.limit ||
2157 !o->data.max_src_conn_rate.seconds) {
2158 yyerror("'max-src-conn-rate' "
2159 "values must be > 0");
2162 if (o->data.max_src_conn_rate.limit >
2164 yyerror("'max-src-conn-rate' "
2165 "maximum rate must be < %u",
2169 r.max_src_conn_rate.limit =
2170 o->data.max_src_conn_rate.limit;
2171 r.max_src_conn_rate.seconds =
2172 o->data.max_src_conn_rate.seconds;
2173 r.rule_flag |= PFRULE_SRCTRACK |
2174 PFRULE_RULESRCTRACK;
2176 case PF_STATE_OPT_MAX_SRC_NODES:
2177 if (r.max_src_nodes) {
2178 yyerror("state option "
2180 "multiple definitions");
2183 if (o->data.max_src_nodes == 0) {
2184 yyerror("'max-src-nodes' must "
2189 o->data.max_src_nodes;
2190 r.rule_flag |= PFRULE_SRCTRACK |
2191 PFRULE_RULESRCTRACK;
2193 case PF_STATE_OPT_STATELOCK:
2195 yyerror("state locking option: "
2196 "multiple definitions");
2200 r.rule_flag |= o->data.statelock;
2202 case PF_STATE_OPT_SLOPPY:
2203 if (r.rule_flag & PFRULE_STATESLOPPY) {
2204 yyerror("state sloppy option: "
2205 "multiple definitions");
2208 r.rule_flag |= PFRULE_STATESLOPPY;
2210 case PF_STATE_OPT_TIMEOUT:
2211 if (o->data.timeout.number ==
2212 PFTM_ADAPTIVE_START ||
2213 o->data.timeout.number ==
2216 if (r.timeout[o->data.timeout.number]) {
2217 yyerror("state timeout %s "
2218 "multiple definitions",
2219 pf_timeouts[o->data.
2220 timeout.number].name);
2223 r.timeout[o->data.timeout.number] =
2224 o->data.timeout.seconds;
2231 /* 'flags S/SA' by default on stateful rules */
2232 if (!r.action && !r.flags && !r.flagset &&
2233 !$9.fragment && !($9.marker & FOM_FLAGS) &&
2235 r.flags = parse_flags("S");
2236 r.flagset = parse_flags("SA");
2238 if (!adaptive && r.max_states) {
2239 r.timeout[PFTM_ADAPTIVE_START] =
2240 (r.max_states / 10) * 6;
2241 r.timeout[PFTM_ADAPTIVE_END] =
2242 (r.max_states / 10) * 12;
2244 if (r.rule_flag & PFRULE_SRCTRACK) {
2245 if (srctrack == PF_SRCTRACK_GLOBAL &&
2247 yyerror("'max-src-nodes' is "
2248 "incompatible with "
2249 "'source-track global'");
2252 if (srctrack == PF_SRCTRACK_GLOBAL &&
2254 yyerror("'max-src-conn' is "
2255 "incompatible with "
2256 "'source-track global'");
2259 if (srctrack == PF_SRCTRACK_GLOBAL &&
2260 r.max_src_conn_rate.seconds) {
2261 yyerror("'max-src-conn-rate' is "
2262 "incompatible with "
2263 "'source-track global'");
2266 if (r.timeout[PFTM_SRC_NODE] <
2267 r.max_src_conn_rate.seconds)
2268 r.timeout[PFTM_SRC_NODE] =
2269 r.max_src_conn_rate.seconds;
2270 r.rule_flag |= PFRULE_SRCTRACK;
2271 if (srctrack == PF_SRCTRACK_RULE)
2272 r.rule_flag |= PFRULE_RULESRCTRACK;
2274 if (r.keep_state && !statelock)
2275 r.rule_flag |= default_statelock;
2278 r.rule_flag |= PFRULE_FRAGMENT;
2279 r.allow_opts = $9.allowopts;
2281 decide_address_family($8.src.host, &r.af);
2282 decide_address_family($8.dst.host, &r.af);
2286 yyerror("direction must be explicit "
2287 "with rules that specify routing");
2291 r.rpool.opts = $5.pool_opts;
2293 memcpy(&r.rpool.key, $5.key,
2294 sizeof(struct pf_poolhashkey));
2296 if (r.rt && r.rt != PF_FASTROUTE) {
2297 decide_address_family($5.host, &r.af);
2298 remove_invalid_hosts(&$5.host, &r.af);
2299 if ($5.host == NULL) {
2300 yyerror("no routing address with "
2301 "matching address family found.");
2304 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
2305 PF_POOL_NONE && ($5.host->next != NULL ||
2306 $5.host->addr.type == PF_ADDR_TABLE ||
2307 DYNIF_MULTIADDR($5.host->addr)))
2308 r.rpool.opts |= PF_POOL_ROUNDROBIN;
2309 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2310 PF_POOL_ROUNDROBIN &&
2311 disallow_table($5.host, "tables are only "
2312 "supported in round-robin routing pools"))
2314 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2315 PF_POOL_ROUNDROBIN &&
2316 disallow_alias($5.host, "interface (%s) "
2317 "is only supported in round-robin "
2320 if ($5.host->next != NULL) {
2321 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2322 PF_POOL_ROUNDROBIN) {
2323 yyerror("r.rpool.opts must "
2324 "be PF_POOL_ROUNDROBIN");
2329 if ($9.queues.qname != NULL) {
2330 if (strlcpy(r.qname, $9.queues.qname,
2331 sizeof(r.qname)) >= sizeof(r.qname)) {
2332 yyerror("rule qname too long (max "
2333 "%d chars)", sizeof(r.qname)-1);
2336 free($9.queues.qname);
2338 if ($9.queues.pqname != NULL) {
2339 if (strlcpy(r.pqname, $9.queues.pqname,
2340 sizeof(r.pqname)) >= sizeof(r.pqname)) {
2341 yyerror("rule pqname too long (max "
2342 "%d chars)", sizeof(r.pqname)-1);
2345 free($9.queues.pqname);
2348 r.divert.port = $9.divert.port;
2350 if ((r.divert.port = $9.divert.port)) {
2351 if (r.direction == PF_OUT) {
2352 if ($9.divert.addr) {
2353 yyerror("address specified "
2354 "for outgoing divert");
2357 bzero(&r.divert.addr,
2358 sizeof(r.divert.addr));
2360 if (!$9.divert.addr) {
2361 yyerror("no address specified "
2362 "for incoming divert");
2365 if ($9.divert.addr->af != r.af) {
2366 yyerror("address family "
2367 "mismatch for divert");
2371 $9.divert.addr->addr.v.a.addr;
2376 expand_rule(&r, $4, $5.host, $7, $8.src_os,
2377 $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
2378 $9.uid, $9.gid, $9.icmpspec, "");
2383 bzero(&filter_opts, sizeof filter_opts);
2384 filter_opts.rtableid = -1;
2387 { $$ = filter_opts; }
2389 bzero(&filter_opts, sizeof filter_opts);
2390 filter_opts.rtableid = -1;
2395 filter_opts_l : filter_opts_l filter_opt
2399 filter_opt : USER uids {
2400 if (filter_opts.uid)
2401 $2->tail->next = filter_opts.uid;
2402 filter_opts.uid = $2;
2405 if (filter_opts.gid)
2406 $2->tail->next = filter_opts.gid;
2407 filter_opts.gid = $2;
2410 if (filter_opts.marker & FOM_FLAGS) {
2411 yyerror("flags cannot be redefined");
2414 filter_opts.marker |= FOM_FLAGS;
2415 filter_opts.flags.b1 |= $1.b1;
2416 filter_opts.flags.b2 |= $1.b2;
2417 filter_opts.flags.w |= $1.w;
2418 filter_opts.flags.w2 |= $1.w2;
2421 if (filter_opts.marker & FOM_ICMP) {
2422 yyerror("icmp-type cannot be redefined");
2425 filter_opts.marker |= FOM_ICMP;
2426 filter_opts.icmpspec = $1;
2429 if (filter_opts.marker & FOM_TOS) {
2430 yyerror("tos cannot be redefined");
2433 filter_opts.marker |= FOM_TOS;
2434 filter_opts.tos = $2;
2437 if (filter_opts.marker & FOM_KEEP) {
2438 yyerror("modulate or keep cannot be redefined");
2441 filter_opts.marker |= FOM_KEEP;
2442 filter_opts.keep.action = $1.action;
2443 filter_opts.keep.options = $1.options;
2446 filter_opts.fragment = 1;
2449 filter_opts.allowopts = 1;
2452 if (filter_opts.label) {
2453 yyerror("label cannot be redefined");
2456 filter_opts.label = $1;
2459 if (filter_opts.queues.qname) {
2460 yyerror("queue cannot be redefined");
2463 filter_opts.queues = $1;
2466 filter_opts.tag = $2;
2468 | not TAGGED string {
2469 filter_opts.match_tag = $3;
2470 filter_opts.match_tag_not = $1;
2472 | PROBABILITY probability {
2475 p = floor($2 * UINT_MAX + 0.5);
2476 if (p < 0.0 || p > UINT_MAX) {
2477 yyerror("invalid probability: %lf", p);
2480 filter_opts.prob = (u_int32_t)p;
2481 if (filter_opts.prob == 0)
2482 filter_opts.prob = 1;
2485 if ($2 < 0 || $2 > rt_tableid_max()) {
2486 yyerror("invalid rtable id");
2489 filter_opts.rtableid = $2;
2491 | DIVERTTO portplain {
2493 filter_opts.divert.port = $2.a;
2494 if (!filter_opts.divert.port) {
2495 yyerror("invalid divert port: %u", ntohs($2.a));
2500 | DIVERTTO STRING PORT portplain {
2502 if ((filter_opts.divert.addr = host($2)) == NULL) {
2503 yyerror("could not parse divert address: %s",
2512 filter_opts.divert.port = $4.a;
2513 if (!filter_opts.divert.port) {
2514 yyerror("invalid divert port: %u", ntohs($4.a));
2520 yyerror("divert-reply has no meaning in FreeBSD pf(4)");
2523 filter_opts.divert.port = 1; /* some random value */
2528 probability : STRING {
2530 double p = strtod($1, &e);
2537 yyerror("invalid probability: %s", $1);
2550 action : PASS { $$.b1 = PF_PASS; $$.b2 = $$.w = 0; }
2551 | BLOCK blockspec { $$ = $2; $$.b1 = PF_DROP; }
2554 blockspec : /* empty */ {
2555 $$.b2 = blockpolicy;
2556 $$.w = returnicmpdefault;
2557 $$.w2 = returnicmp6default;
2560 $$.b2 = PFRULE_DROP;
2565 $$.b2 = PFRULE_RETURNRST;
2569 | RETURNRST '(' TTL NUMBER ')' {
2570 if ($4 < 0 || $4 > 255) {
2571 yyerror("illegal ttl value %d", $4);
2574 $$.b2 = PFRULE_RETURNRST;
2579 $$.b2 = PFRULE_RETURNICMP;
2580 $$.w = returnicmpdefault;
2581 $$.w2 = returnicmp6default;
2584 $$.b2 = PFRULE_RETURNICMP;
2585 $$.w = returnicmpdefault;
2586 $$.w2 = returnicmp6default;
2588 | RETURNICMP '(' reticmpspec ')' {
2589 $$.b2 = PFRULE_RETURNICMP;
2591 $$.w2 = returnicmpdefault;
2593 | RETURNICMP6 '(' reticmp6spec ')' {
2594 $$.b2 = PFRULE_RETURNICMP;
2595 $$.w = returnicmpdefault;
2598 | RETURNICMP '(' reticmpspec comma reticmp6spec ')' {
2599 $$.b2 = PFRULE_RETURNICMP;
2604 $$.b2 = PFRULE_RETURN;
2605 $$.w = returnicmpdefault;
2606 $$.w2 = returnicmp6default;
2610 reticmpspec : STRING {
2611 if (!($$ = parseicmpspec($1, AF_INET))) {
2620 if ($1 < 0 || $1 > 255) {
2621 yyerror("invalid icmp code %lu", $1);
2624 icmptype = returnicmpdefault >> 8;
2625 $$ = (icmptype << 8 | $1);
2629 reticmp6spec : STRING {
2630 if (!($$ = parseicmpspec($1, AF_INET6))) {
2639 if ($1 < 0 || $1 > 255) {
2640 yyerror("invalid icmp code %lu", $1);
2643 icmptype = returnicmp6default >> 8;
2644 $$ = (icmptype << 8 | $1);
2648 dir : /* empty */ { $$ = PF_INOUT; }
2649 | IN { $$ = PF_IN; }
2650 | OUT { $$ = PF_OUT; }
2653 quick : /* empty */ { $$.quick = 0; }
2654 | QUICK { $$.quick = 1; }
2657 logquick : /* empty */ { $$.log = 0; $$.quick = 0; $$.logif = 0; }
2658 | log { $$ = $1; $$.quick = 0; }
2659 | QUICK { $$.quick = 1; $$.log = 0; $$.logif = 0; }
2660 | log QUICK { $$ = $1; $$.quick = 1; }
2661 | QUICK log { $$ = $2; $$.quick = 1; }
2664 log : LOG { $$.log = PF_LOG; $$.logif = 0; }
2665 | LOG '(' logopts ')' {
2666 $$.log = PF_LOG | $3.log;
2667 $$.logif = $3.logif;
2671 logopts : logopt { $$ = $1; }
2672 | logopts comma logopt {
2673 $$.log = $1.log | $3.log;
2674 $$.logif = $3.logif;
2676 $$.logif = $1.logif;
2680 logopt : ALL { $$.log = PF_LOG_ALL; $$.logif = 0; }
2681 | USER { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
2682 | GROUP { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
2688 if (strncmp($2, "pflog", 5)) {
2689 yyerror("%s: should be a pflog interface", $2);
2693 i = strtonum($2 + 5, 0, 255, &errstr);
2695 yyerror("%s: %s", $2, errstr);
2704 interface : /* empty */ { $$ = NULL; }
2705 | ON if_item_not { $$ = $2; }
2706 | ON '{' optnl if_list '}' { $$ = $4; }
2709 if_list : if_item_not optnl { $$ = $1; }
2710 | if_list comma if_item_not optnl {
2711 $1->tail->next = $3;
2717 if_item_not : not if_item { $$ = $2; $$->not = $1; }
2721 struct node_host *n;
2723 $$ = calloc(1, sizeof(struct node_if));
2725 err(1, "if_item: calloc");
2726 if (strlcpy($$->ifname, $1, sizeof($$->ifname)) >=
2727 sizeof($$->ifname)) {
2730 yyerror("interface name too long");
2734 if ((n = ifa_exists($1)) != NULL)
2735 $$->ifa_flags = n->ifa_flags;
2744 af : /* empty */ { $$ = 0; }
2745 | INET { $$ = AF_INET; }
2746 | INET6 { $$ = AF_INET6; }
2749 proto : /* empty */ { $$ = NULL; }
2750 | PROTO proto_item { $$ = $2; }
2751 | PROTO '{' optnl proto_list '}' { $$ = $4; }
2754 proto_list : proto_item optnl { $$ = $1; }
2755 | proto_list comma proto_item optnl {
2756 $1->tail->next = $3;
2762 proto_item : protoval {
2767 yyerror("proto 0 cannot be used");
2770 $$ = calloc(1, sizeof(struct node_proto));
2772 err(1, "proto_item: calloc");
2782 p = getprotobyname($1);
2784 yyerror("unknown protocol %s", $1);
2792 if ($1 < 0 || $1 > 255) {
2793 yyerror("protocol outside range");
2813 os : /* empty */ { $$ = NULL; }
2814 | OS xos { $$ = $2; }
2815 | OS '{' optnl os_list '}' { $$ = $4; }
2819 $$ = calloc(1, sizeof(struct node_os));
2821 err(1, "os: calloc");
2827 os_list : xos optnl { $$ = $1; }
2828 | os_list comma xos optnl {
2829 $1->tail->next = $3;
2835 from : /* empty */ {
2849 if (disallow_urpf_failed($2.host, "\"urpf-failed\" is "
2850 "not permitted in a destination address"))
2856 ipportspec : ipspec {
2860 | ipspec PORT portspec {
2874 ipspec : ANY { $$ = NULL; }
2875 | xhost { $$ = $1; }
2876 | '{' optnl host_list '}' { $$ = $3; }
2879 toipspec : TO ipspec { $$ = $2; }
2880 | /* empty */ { $$ = NULL; }
2883 host_list : ipspec optnl { $$ = $1; }
2884 | host_list comma ipspec optnl {
2887 else if ($1 == NULL)
2890 $1->tail->next = $3;
2891 $1->tail = $3->tail;
2898 struct node_host *n;
2900 for (n = $2; n != NULL; n = n->next)
2905 $$ = calloc(1, sizeof(struct node_host));
2907 err(1, "xhost: calloc");
2908 $$->addr.type = PF_ADDR_NOROUTE;
2914 $$ = calloc(1, sizeof(struct node_host));
2916 err(1, "xhost: calloc");
2917 $$->addr.type = PF_ADDR_URPFFAILED;
2925 if (($$ = host($1)) == NULL) {
2926 /* error. "any" is handled elsewhere */
2928 yyerror("could not parse host specification");
2934 | STRING '-' STRING {
2935 struct node_host *b, *e;
2937 if ((b = host($1)) == NULL || (e = host($3)) == NULL) {
2940 yyerror("could not parse host specification");
2943 if (b->af != e->af ||
2944 b->addr.type != PF_ADDR_ADDRMASK ||
2945 e->addr.type != PF_ADDR_ADDRMASK ||
2946 unmask(&b->addr.v.a.mask, b->af) !=
2947 (b->af == AF_INET ? 32 : 128) ||
2948 unmask(&e->addr.v.a.mask, e->af) !=
2949 (e->af == AF_INET ? 32 : 128) ||
2950 b->next != NULL || b->not ||
2951 e->next != NULL || e->not) {
2956 yyerror("invalid address range");
2959 memcpy(&b->addr.v.a.mask, &e->addr.v.a.addr,
2960 sizeof(b->addr.v.a.mask));
2961 b->addr.type = PF_ADDR_RANGE;
2967 | STRING '/' NUMBER {
2970 if (asprintf(&buf, "%s/%lld", $1, (long long)$3) == -1)
2971 err(1, "host: asprintf");
2973 if (($$ = host(buf)) == NULL) {
2974 /* error. "any" is handled elsewhere */
2976 yyerror("could not parse host specification");
2981 | NUMBER '/' NUMBER {
2984 /* ie. for 10/8 parsing */
2986 if (asprintf(&buf, "%lld/%lld", (long long)$1, (long long)$3) == -1)
2988 if (asprintf(&buf, "%lld/%lld", $1, $3) == -1)
2990 err(1, "host: asprintf");
2991 if (($$ = host(buf)) == NULL) {
2992 /* error. "any" is handled elsewhere */
2994 yyerror("could not parse host specification");
3000 | dynaddr '/' NUMBER {
3001 struct node_host *n;
3003 if ($3 < 0 || $3 > 128) {
3004 yyerror("bit number too big");
3008 for (n = $1; n != NULL; n = n->next)
3012 if (strlen($2) >= PF_TABLE_NAME_SIZE) {
3013 yyerror("table name '%s' too long", $2);
3017 $$ = calloc(1, sizeof(struct node_host));
3019 err(1, "host: calloc");
3020 $$->addr.type = PF_ADDR_TABLE;
3021 if (strlcpy($$->addr.v.tblname, $2,
3022 sizeof($$->addr.v.tblname)) >=
3023 sizeof($$->addr.v.tblname))
3024 errx(1, "host: strlcpy");
3035 if (atoul($1, &ulval) == -1) {
3036 yyerror("%s is not a number", $1);
3045 dynaddr : '(' STRING ')' {
3050 if (!isalpha(op[0])) {
3051 yyerror("invalid interface name '%s'", op);
3055 while ((p = strrchr($2, ':')) != NULL) {
3056 if (!strcmp(p+1, "network"))
3057 flags |= PFI_AFLAG_NETWORK;
3058 else if (!strcmp(p+1, "broadcast"))
3059 flags |= PFI_AFLAG_BROADCAST;
3060 else if (!strcmp(p+1, "peer"))
3061 flags |= PFI_AFLAG_PEER;
3062 else if (!strcmp(p+1, "0"))
3063 flags |= PFI_AFLAG_NOALIAS;
3065 yyerror("interface %s has bad modifier",
3072 if (flags & (flags - 1) & PFI_AFLAG_MODEMASK) {
3074 yyerror("illegal combination of "
3075 "interface modifiers");
3078 $$ = calloc(1, sizeof(struct node_host));
3080 err(1, "address: calloc");
3082 set_ipmask($$, 128);
3083 $$->addr.type = PF_ADDR_DYNIFTL;
3084 $$->addr.iflags = flags;
3085 if (strlcpy($$->addr.v.ifname, $2,
3086 sizeof($$->addr.v.ifname)) >=
3087 sizeof($$->addr.v.ifname)) {
3090 yyerror("interface name too long");
3099 portspec : port_item { $$ = $1; }
3100 | '{' optnl port_list '}' { $$ = $3; }
3103 port_list : port_item optnl { $$ = $1; }
3104 | port_list comma port_item optnl {
3105 $1->tail->next = $3;
3111 port_item : portrange {
3112 $$ = calloc(1, sizeof(struct node_port));
3114 err(1, "port_item: calloc");
3124 | unaryop portrange {
3126 yyerror("':' cannot be used with an other "
3130 $$ = calloc(1, sizeof(struct node_port));
3132 err(1, "port_item: calloc");
3139 | portrange PORTBINARY portrange {
3141 yyerror("':' cannot be used with an other "
3145 $$ = calloc(1, sizeof(struct node_port));
3147 err(1, "port_item: calloc");
3156 portplain : numberstring {
3157 if (parseport($1, &$$, 0) == -1) {
3165 portrange : numberstring {
3166 if (parseport($1, &$$, PPORT_RANGE) == -1) {
3174 uids : uid_item { $$ = $1; }
3175 | '{' optnl uid_list '}' { $$ = $3; }
3178 uid_list : uid_item optnl { $$ = $1; }
3179 | uid_list comma uid_item optnl {
3180 $1->tail->next = $3;
3187 $$ = calloc(1, sizeof(struct node_uid));
3189 err(1, "uid_item: calloc");
3197 if ($2 == UID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
3198 yyerror("user unknown requires operator = or "
3202 $$ = calloc(1, sizeof(struct node_uid));
3204 err(1, "uid_item: calloc");
3211 | uid PORTBINARY uid {
3212 if ($1 == UID_MAX || $3 == UID_MAX) {
3213 yyerror("user unknown requires operator = or "
3217 $$ = calloc(1, sizeof(struct node_uid));
3219 err(1, "uid_item: calloc");
3229 if (!strcmp($1, "unknown"))
3234 if ((pw = getpwnam($1)) == NULL) {
3235 yyerror("unknown user %s", $1);
3244 if ($1 < 0 || $1 >= UID_MAX) {
3245 yyerror("illegal uid value %lu", $1);
3252 gids : gid_item { $$ = $1; }
3253 | '{' optnl gid_list '}' { $$ = $3; }
3256 gid_list : gid_item optnl { $$ = $1; }
3257 | gid_list comma gid_item optnl {
3258 $1->tail->next = $3;
3265 $$ = calloc(1, sizeof(struct node_gid));
3267 err(1, "gid_item: calloc");
3275 if ($2 == GID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
3276 yyerror("group unknown requires operator = or "
3280 $$ = calloc(1, sizeof(struct node_gid));
3282 err(1, "gid_item: calloc");
3289 | gid PORTBINARY gid {
3290 if ($1 == GID_MAX || $3 == GID_MAX) {
3291 yyerror("group unknown requires operator = or "
3295 $$ = calloc(1, sizeof(struct node_gid));
3297 err(1, "gid_item: calloc");
3307 if (!strcmp($1, "unknown"))
3312 if ((grp = getgrnam($1)) == NULL) {
3313 yyerror("unknown group %s", $1);
3322 if ($1 < 0 || $1 >= GID_MAX) {
3323 yyerror("illegal gid value %lu", $1);
3333 if ((f = parse_flags($1)) < 0) {
3334 yyerror("bad flags %s", $1);
3343 flags : FLAGS flag '/' flag { $$.b1 = $2.b1; $$.b2 = $4.b1; }
3344 | FLAGS '/' flag { $$.b1 = 0; $$.b2 = $3.b1; }
3345 | FLAGS ANY { $$.b1 = 0; $$.b2 = 0; }
3348 icmpspec : ICMPTYPE icmp_item { $$ = $2; }
3349 | ICMPTYPE '{' optnl icmp_list '}' { $$ = $4; }
3350 | ICMP6TYPE icmp6_item { $$ = $2; }
3351 | ICMP6TYPE '{' optnl icmp6_list '}' { $$ = $4; }
3354 icmp_list : icmp_item optnl { $$ = $1; }
3355 | icmp_list comma icmp_item optnl {
3356 $1->tail->next = $3;
3362 icmp6_list : icmp6_item optnl { $$ = $1; }
3363 | icmp6_list comma icmp6_item optnl {
3364 $1->tail->next = $3;
3370 icmp_item : icmptype {
3371 $$ = calloc(1, sizeof(struct node_icmp));
3373 err(1, "icmp_item: calloc");
3376 $$->proto = IPPROTO_ICMP;
3380 | icmptype CODE STRING {
3381 const struct icmpcodeent *p;
3383 if ((p = geticmpcodebyname($1-1, $3, AF_INET)) == NULL) {
3384 yyerror("unknown icmp-code %s", $3);
3390 $$ = calloc(1, sizeof(struct node_icmp));
3392 err(1, "icmp_item: calloc");
3394 $$->code = p->code + 1;
3395 $$->proto = IPPROTO_ICMP;
3399 | icmptype CODE NUMBER {
3400 if ($3 < 0 || $3 > 255) {
3401 yyerror("illegal icmp-code %lu", $3);
3404 $$ = calloc(1, sizeof(struct node_icmp));
3406 err(1, "icmp_item: calloc");
3409 $$->proto = IPPROTO_ICMP;
3415 icmp6_item : icmp6type {
3416 $$ = calloc(1, sizeof(struct node_icmp));
3418 err(1, "icmp_item: calloc");
3421 $$->proto = IPPROTO_ICMPV6;
3425 | icmp6type CODE STRING {
3426 const struct icmpcodeent *p;
3428 if ((p = geticmpcodebyname($1-1, $3, AF_INET6)) == NULL) {
3429 yyerror("unknown icmp6-code %s", $3);
3435 $$ = calloc(1, sizeof(struct node_icmp));
3437 err(1, "icmp_item: calloc");
3439 $$->code = p->code + 1;
3440 $$->proto = IPPROTO_ICMPV6;
3444 | icmp6type CODE NUMBER {
3445 if ($3 < 0 || $3 > 255) {
3446 yyerror("illegal icmp-code %lu", $3);
3449 $$ = calloc(1, sizeof(struct node_icmp));
3451 err(1, "icmp_item: calloc");
3454 $$->proto = IPPROTO_ICMPV6;
3461 const struct icmptypeent *p;
3463 if ((p = geticmptypebyname($1, AF_INET)) == NULL) {
3464 yyerror("unknown icmp-type %s", $1);
3472 if ($1 < 0 || $1 > 255) {
3473 yyerror("illegal icmp-type %lu", $1);
3480 icmp6type : STRING {
3481 const struct icmptypeent *p;
3483 if ((p = geticmptypebyname($1, AF_INET6)) ==
3485 yyerror("unknown icmp6-type %s", $1);
3493 if ($1 < 0 || $1 > 255) {
3494 yyerror("illegal icmp6-type %lu", $1);
3502 if (!strcmp($1, "lowdelay"))
3503 $$ = IPTOS_LOWDELAY;
3504 else if (!strcmp($1, "throughput"))
3505 $$ = IPTOS_THROUGHPUT;
3506 else if (!strcmp($1, "reliability"))
3507 $$ = IPTOS_RELIABILITY;
3508 else if ($1[0] == '0' && $1[1] == 'x')
3509 $$ = strtoul($1, NULL, 16);
3511 $$ = 0; /* flag bad argument */
3512 if (!$$ || $$ > 255) {
3513 yyerror("illegal tos value %s", $1);
3521 if (!$$ || $$ > 255) {
3522 yyerror("illegal tos value %s", $1);
3528 sourcetrack : SOURCETRACK { $$ = PF_SRCTRACK; }
3529 | SOURCETRACK GLOBAL { $$ = PF_SRCTRACK_GLOBAL; }
3530 | SOURCETRACK RULE { $$ = PF_SRCTRACK_RULE; }
3533 statelock : IFBOUND {
3534 $$ = PFRULE_IFBOUND;
3545 | KEEP STATE state_opt_spec {
3546 $$.action = PF_STATE_NORMAL;
3549 | MODULATE STATE state_opt_spec {
3550 $$.action = PF_STATE_MODULATE;
3553 | SYNPROXY STATE state_opt_spec {
3554 $$.action = PF_STATE_SYNPROXY;
3559 flush : /* empty */ { $$ = 0; }
3560 | FLUSH { $$ = PF_FLUSH; }
3562 $$ = PF_FLUSH | PF_FLUSH_GLOBAL;
3566 state_opt_spec : '(' state_opt_list ')' { $$ = $2; }
3567 | /* empty */ { $$ = NULL; }
3570 state_opt_list : state_opt_item { $$ = $1; }
3571 | state_opt_list comma state_opt_item {
3572 $1->tail->next = $3;
3578 state_opt_item : MAXIMUM NUMBER {
3579 if ($2 < 0 || $2 > UINT_MAX) {
3580 yyerror("only positive values permitted");
3583 $$ = calloc(1, sizeof(struct node_state_opt));
3585 err(1, "state_opt_item: calloc");
3586 $$->type = PF_STATE_OPT_MAX;
3587 $$->data.max_states = $2;
3592 $$ = calloc(1, sizeof(struct node_state_opt));
3594 err(1, "state_opt_item: calloc");
3595 $$->type = PF_STATE_OPT_NOSYNC;
3599 | MAXSRCSTATES NUMBER {
3600 if ($2 < 0 || $2 > UINT_MAX) {
3601 yyerror("only positive values permitted");
3604 $$ = calloc(1, sizeof(struct node_state_opt));
3606 err(1, "state_opt_item: calloc");
3607 $$->type = PF_STATE_OPT_MAX_SRC_STATES;
3608 $$->data.max_src_states = $2;
3612 | MAXSRCCONN NUMBER {
3613 if ($2 < 0 || $2 > UINT_MAX) {
3614 yyerror("only positive values permitted");
3617 $$ = calloc(1, sizeof(struct node_state_opt));
3619 err(1, "state_opt_item: calloc");
3620 $$->type = PF_STATE_OPT_MAX_SRC_CONN;
3621 $$->data.max_src_conn = $2;
3625 | MAXSRCCONNRATE NUMBER '/' NUMBER {
3626 if ($2 < 0 || $2 > UINT_MAX ||
3627 $4 < 0 || $4 > UINT_MAX) {
3628 yyerror("only positive values permitted");
3631 $$ = calloc(1, sizeof(struct node_state_opt));
3633 err(1, "state_opt_item: calloc");
3634 $$->type = PF_STATE_OPT_MAX_SRC_CONN_RATE;
3635 $$->data.max_src_conn_rate.limit = $2;
3636 $$->data.max_src_conn_rate.seconds = $4;
3640 | OVERLOAD '<' STRING '>' flush {
3641 if (strlen($3) >= PF_TABLE_NAME_SIZE) {
3642 yyerror("table name '%s' too long", $3);
3646 $$ = calloc(1, sizeof(struct node_state_opt));
3648 err(1, "state_opt_item: calloc");
3649 if (strlcpy($$->data.overload.tblname, $3,
3650 PF_TABLE_NAME_SIZE) >= PF_TABLE_NAME_SIZE)
3651 errx(1, "state_opt_item: strlcpy");
3653 $$->type = PF_STATE_OPT_OVERLOAD;
3654 $$->data.overload.flush = $5;
3658 | MAXSRCNODES NUMBER {
3659 if ($2 < 0 || $2 > UINT_MAX) {
3660 yyerror("only positive values permitted");
3663 $$ = calloc(1, sizeof(struct node_state_opt));
3665 err(1, "state_opt_item: calloc");
3666 $$->type = PF_STATE_OPT_MAX_SRC_NODES;
3667 $$->data.max_src_nodes = $2;
3672 $$ = calloc(1, sizeof(struct node_state_opt));
3674 err(1, "state_opt_item: calloc");
3675 $$->type = PF_STATE_OPT_SRCTRACK;
3676 $$->data.src_track = $1;
3681 $$ = calloc(1, sizeof(struct node_state_opt));
3683 err(1, "state_opt_item: calloc");
3684 $$->type = PF_STATE_OPT_STATELOCK;
3685 $$->data.statelock = $1;
3690 $$ = calloc(1, sizeof(struct node_state_opt));
3692 err(1, "state_opt_item: calloc");
3693 $$->type = PF_STATE_OPT_SLOPPY;
3700 if ($2 < 0 || $2 > UINT_MAX) {
3701 yyerror("only positive values permitted");
3704 for (i = 0; pf_timeouts[i].name &&
3705 strcmp(pf_timeouts[i].name, $1); ++i)
3707 if (!pf_timeouts[i].name) {
3708 yyerror("illegal timeout name %s", $1);
3712 if (strchr(pf_timeouts[i].name, '.') == NULL) {
3713 yyerror("illegal state timeout %s", $1);
3718 $$ = calloc(1, sizeof(struct node_state_opt));
3720 err(1, "state_opt_item: calloc");
3721 $$->type = PF_STATE_OPT_TIMEOUT;
3722 $$->data.timeout.number = pf_timeouts[i].timeout;
3723 $$->data.timeout.seconds = $2;
3729 label : LABEL STRING {
3734 qname : QUEUE STRING {
3738 | QUEUE '(' STRING ')' {
3742 | QUEUE '(' STRING comma STRING ')' {
3748 no : /* empty */ { $$ = 0; }
3752 portstar : numberstring {
3753 if (parseport($1, &$$, PPORT_RANGE|PPORT_STAR) == -1) {
3761 redirspec : host { $$ = $1; }
3762 | '{' optnl redir_host_list '}' { $$ = $3; }
3765 redir_host_list : host optnl { $$ = $1; }
3766 | redir_host_list comma host optnl {
3767 $1->tail->next = $3;
3768 $1->tail = $3->tail;
3773 redirpool : /* empty */ { $$ = NULL; }
3775 $$ = calloc(1, sizeof(struct redirection));
3777 err(1, "redirection: calloc");
3779 $$->rport.a = $$->rport.b = $$->rport.t = 0;
3781 | ARROW redirspec PORT portstar {
3782 $$ = calloc(1, sizeof(struct redirection));
3784 err(1, "redirection: calloc");
3790 hashkey : /* empty */
3792 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3794 err(1, "hashkey: calloc");
3795 $$->key32[0] = arc4random();
3796 $$->key32[1] = arc4random();
3797 $$->key32[2] = arc4random();
3798 $$->key32[3] = arc4random();
3802 if (!strncmp($1, "0x", 2)) {
3803 if (strlen($1) != 34) {
3805 yyerror("hex key must be 128 bits "
3806 "(32 hex digits) long");
3809 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3811 err(1, "hashkey: calloc");
3813 if (sscanf($1, "0x%8x%8x%8x%8x",
3814 &$$->key32[0], &$$->key32[1],
3815 &$$->key32[2], &$$->key32[3]) != 4) {
3818 yyerror("invalid hex key");
3824 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3826 err(1, "hashkey: calloc");
3828 MD5Update(&context, (unsigned char *)$1,
3830 MD5Final((unsigned char *)$$, &context);
3831 HTONL($$->key32[0]);
3832 HTONL($$->key32[1]);
3833 HTONL($$->key32[2]);
3834 HTONL($$->key32[3]);
3840 pool_opts : { bzero(&pool_opts, sizeof pool_opts); }
3844 bzero(&pool_opts, sizeof pool_opts);
3849 pool_opts_l : pool_opts_l pool_opt
3853 pool_opt : BITMASK {
3854 if (pool_opts.type) {
3855 yyerror("pool type cannot be redefined");
3858 pool_opts.type = PF_POOL_BITMASK;
3861 if (pool_opts.type) {
3862 yyerror("pool type cannot be redefined");
3865 pool_opts.type = PF_POOL_RANDOM;
3867 | SOURCEHASH hashkey {
3868 if (pool_opts.type) {
3869 yyerror("pool type cannot be redefined");
3872 pool_opts.type = PF_POOL_SRCHASH;
3876 if (pool_opts.type) {
3877 yyerror("pool type cannot be redefined");
3880 pool_opts.type = PF_POOL_ROUNDROBIN;
3883 if (pool_opts.staticport) {
3884 yyerror("static-port cannot be redefined");
3887 pool_opts.staticport = 1;
3890 if (filter_opts.marker & POM_STICKYADDRESS) {
3891 yyerror("sticky-address cannot be redefined");
3894 pool_opts.marker |= POM_STICKYADDRESS;
3895 pool_opts.opts |= PF_POOL_STICKYADDR;
3899 redirection : /* empty */ { $$ = NULL; }
3901 $$ = calloc(1, sizeof(struct redirection));
3903 err(1, "redirection: calloc");
3905 $$->rport.a = $$->rport.b = $$->rport.t = 0;
3907 | ARROW host PORT portstar {
3908 $$ = calloc(1, sizeof(struct redirection));
3910 err(1, "redirection: calloc");
3916 natpasslog : /* empty */ { $$.b1 = $$.b2 = 0; $$.w2 = 0; }
3917 | PASS { $$.b1 = 1; $$.b2 = 0; $$.w2 = 0; }
3918 | PASS log { $$.b1 = 1; $$.b2 = $2.log; $$.w2 = $2.logif; }
3919 | log { $$.b1 = 0; $$.b2 = $1.log; $$.w2 = $1.logif; }
3922 nataction : no NAT natpasslog {
3924 yyerror("\"pass\" not valid with \"no\"");
3935 | no RDR natpasslog {
3937 yyerror("\"pass\" not valid with \"no\"");
3950 natrule : nataction interface af proto fromto tag tagged rtable
3955 if (check_rulestate(PFCTL_STATE_NAT))
3958 memset(&r, 0, sizeof(r));
3967 if ($5.src.host && $5.src.host->af &&
3968 !$5.src.host->ifindex)
3969 r.af = $5.src.host->af;
3970 else if ($5.dst.host && $5.dst.host->af &&
3971 !$5.dst.host->ifindex)
3972 r.af = $5.dst.host->af;
3976 if (strlcpy(r.tagname, $6, PF_TAG_NAME_SIZE) >=
3978 yyerror("tag too long, max %u chars",
3979 PF_TAG_NAME_SIZE - 1);
3984 if (strlcpy(r.match_tagname, $7.name,
3985 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
3986 yyerror("tag too long, max %u chars",
3987 PF_TAG_NAME_SIZE - 1);
3990 r.match_tag_not = $7.neg;
3993 if (r.action == PF_NONAT || r.action == PF_NORDR) {
3995 yyerror("translation rule with 'no' "
3996 "does not need '->'");
4000 if ($9 == NULL || $9->host == NULL) {
4001 yyerror("translation rule requires '-> "
4005 if (!r.af && ! $9->host->ifindex)
4006 r.af = $9->host->af;
4008 remove_invalid_hosts(&$9->host, &r.af);
4009 if (invalid_redirect($9->host, r.af))
4011 if (check_netmask($9->host, r.af))
4014 r.rpool.proxy_port[0] = ntohs($9->rport.a);
4018 if (!$9->rport.b && $9->rport.t &&
4019 $5.dst.port != NULL) {
4020 r.rpool.proxy_port[1] =
4021 ntohs($9->rport.a) +
4023 $5.dst.port->port[1]) -
4025 $5.dst.port->port[0]));
4027 r.rpool.proxy_port[1] =
4031 r.rpool.proxy_port[1] =
4033 if (!r.rpool.proxy_port[0] &&
4034 !r.rpool.proxy_port[1]) {
4035 r.rpool.proxy_port[0] =
4036 PF_NAT_PROXY_PORT_LOW;
4037 r.rpool.proxy_port[1] =
4038 PF_NAT_PROXY_PORT_HIGH;
4039 } else if (!r.rpool.proxy_port[1])
4040 r.rpool.proxy_port[1] =
4041 r.rpool.proxy_port[0];
4047 r.rpool.opts = $10.type;
4048 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
4049 PF_POOL_NONE && ($9->host->next != NULL ||
4050 $9->host->addr.type == PF_ADDR_TABLE ||
4051 DYNIF_MULTIADDR($9->host->addr)))
4052 r.rpool.opts = PF_POOL_ROUNDROBIN;
4053 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4054 PF_POOL_ROUNDROBIN &&
4055 disallow_table($9->host, "tables are only "
4056 "supported in round-robin redirection "
4059 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4060 PF_POOL_ROUNDROBIN &&
4061 disallow_alias($9->host, "interface (%s) "
4062 "is only supported in round-robin "
4063 "redirection pools"))
4065 if ($9->host->next != NULL) {
4066 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4067 PF_POOL_ROUNDROBIN) {
4068 yyerror("only round-robin "
4069 "valid for multiple "
4070 "redirection addresses");
4076 if ($10.key != NULL)
4077 memcpy(&r.rpool.key, $10.key,
4078 sizeof(struct pf_poolhashkey));
4081 r.rpool.opts |= $10.opts;
4083 if ($10.staticport) {
4084 if (r.action != PF_NAT) {
4085 yyerror("the 'static-port' option is "
4086 "only valid with nat rules");
4089 if (r.rpool.proxy_port[0] !=
4090 PF_NAT_PROXY_PORT_LOW &&
4091 r.rpool.proxy_port[1] !=
4092 PF_NAT_PROXY_PORT_HIGH) {
4093 yyerror("the 'static-port' option can't"
4094 " be used when specifying a port"
4098 r.rpool.proxy_port[0] = 0;
4099 r.rpool.proxy_port[1] = 0;
4102 expand_rule(&r, $2, $9 == NULL ? NULL : $9->host, $4,
4103 $5.src_os, $5.src.host, $5.src.port, $5.dst.host,
4104 $5.dst.port, 0, 0, 0, "");
4109 binatrule : no BINAT natpasslog interface af proto FROM host toipspec tag
4110 tagged rtable redirection
4112 struct pf_rule binat;
4113 struct pf_pooladdr *pa;
4115 if (check_rulestate(PFCTL_STATE_NAT))
4117 if (disallow_urpf_failed($9, "\"urpf-failed\" is not "
4118 "permitted as a binat destination"))
4121 memset(&binat, 0, sizeof(binat));
4124 yyerror("\"pass\" not valid with \"no\"");
4128 binat.action = PF_NOBINAT;
4130 binat.action = PF_BINAT;
4131 binat.natpass = $3.b1;
4133 binat.logif = $3.w2;
4135 if (!binat.af && $8 != NULL && $8->af)
4137 if (!binat.af && $9 != NULL && $9->af)
4140 if (!binat.af && $13 != NULL && $13->host)
4141 binat.af = $13->host->af;
4143 yyerror("address family (inet/inet6) "
4149 memcpy(binat.ifname, $4->ifname,
4150 sizeof(binat.ifname));
4151 binat.ifnot = $4->not;
4156 if (strlcpy(binat.tagname, $10,
4157 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4158 yyerror("tag too long, max %u chars",
4159 PF_TAG_NAME_SIZE - 1);
4163 if (strlcpy(binat.match_tagname, $11.name,
4164 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4165 yyerror("tag too long, max %u chars",
4166 PF_TAG_NAME_SIZE - 1);
4169 binat.match_tag_not = $11.neg;
4170 binat.rtableid = $12;
4173 binat.proto = $6->proto;
4177 if ($8 != NULL && disallow_table($8, "invalid use of "
4178 "table <%s> as the source address of a binat rule"))
4180 if ($8 != NULL && disallow_alias($8, "invalid use of "
4181 "interface (%s) as the source address of a binat "
4184 if ($13 != NULL && $13->host != NULL && disallow_table(
4185 $13->host, "invalid use of table <%s> as the "
4186 "redirect address of a binat rule"))
4188 if ($13 != NULL && $13->host != NULL && disallow_alias(
4189 $13->host, "invalid use of interface (%s) as the "
4190 "redirect address of a binat rule"))
4195 yyerror("multiple binat ip addresses");
4198 if ($8->addr.type == PF_ADDR_DYNIFTL)
4200 if ($8->af != binat.af) {
4201 yyerror("binat ip versions must match");
4204 if (check_netmask($8, binat.af))
4206 memcpy(&binat.src.addr, &$8->addr,
4207 sizeof(binat.src.addr));
4212 yyerror("multiple binat ip addresses");
4215 if ($9->af != binat.af && $9->af) {
4216 yyerror("binat ip versions must match");
4219 if (check_netmask($9, binat.af))
4221 memcpy(&binat.dst.addr, &$9->addr,
4222 sizeof(binat.dst.addr));
4223 binat.dst.neg = $9->not;
4227 if (binat.action == PF_NOBINAT) {
4229 yyerror("'no binat' rule does not need"
4234 if ($13 == NULL || $13->host == NULL) {
4235 yyerror("'binat' rule requires"
4240 remove_invalid_hosts(&$13->host, &binat.af);
4241 if (invalid_redirect($13->host, binat.af))
4243 if ($13->host->next != NULL) {
4244 yyerror("binat rule must redirect to "
4245 "a single address");
4248 if (check_netmask($13->host, binat.af))
4251 if (!PF_AZERO(&binat.src.addr.v.a.mask,
4253 !PF_AEQ(&binat.src.addr.v.a.mask,
4254 &$13->host->addr.v.a.mask, binat.af)) {
4255 yyerror("'binat' source mask and "
4256 "redirect mask must be the same");
4260 TAILQ_INIT(&binat.rpool.list);
4261 pa = calloc(1, sizeof(struct pf_pooladdr));
4263 err(1, "binat: calloc");
4264 pa->addr = $13->host->addr;
4266 TAILQ_INSERT_TAIL(&binat.rpool.list,
4272 pfctl_add_rule(pf, &binat, "");
4276 tag : /* empty */ { $$ = NULL; }
4277 | TAG STRING { $$ = $2; }
4280 tagged : /* empty */ { $$.neg = 0; $$.name = NULL; }
4281 | not TAGGED string { $$.neg = $1; $$.name = $3; }
4284 rtable : /* empty */ { $$ = -1; }
4286 if ($2 < 0 || $2 > rt_tableid_max()) {
4287 yyerror("invalid rtable id");
4294 route_host : STRING {
4295 $$ = calloc(1, sizeof(struct node_host));
4297 err(1, "route_host: calloc");
4299 set_ipmask($$, 128);
4303 | '(' STRING host ')' {
4309 route_host_list : route_host optnl { $$ = $1; }
4310 | route_host_list comma route_host optnl {
4313 if ($1->af != $3->af) {
4314 yyerror("all pool addresses must be in the "
4315 "same address family");
4318 $1->tail->next = $3;
4319 $1->tail = $3->tail;
4324 routespec : route_host { $$ = $1; }
4325 | '{' optnl route_host_list '}' { $$ = $3; }
4328 route : /* empty */ {
4335 $$.rt = PF_FASTROUTE;
4338 | ROUTETO routespec pool_opts {
4341 $$.pool_opts = $3.type | $3.opts;
4345 | REPLYTO routespec pool_opts {
4348 $$.pool_opts = $3.type | $3.opts;
4352 | DUPTO routespec pool_opts {
4355 $$.pool_opts = $3.type | $3.opts;
4361 timeout_spec : STRING NUMBER
4363 if (check_rulestate(PFCTL_STATE_OPTION)) {
4367 if ($2 < 0 || $2 > UINT_MAX) {
4368 yyerror("only positive values permitted");
4371 if (pfctl_set_timeout(pf, $1, $2, 0) != 0) {
4372 yyerror("unknown timeout %s", $1);
4380 timeout_list : timeout_list comma timeout_spec optnl
4381 | timeout_spec optnl
4384 limit_spec : STRING NUMBER
4386 if (check_rulestate(PFCTL_STATE_OPTION)) {
4390 if ($2 < 0 || $2 > UINT_MAX) {
4391 yyerror("only positive values permitted");
4394 if (pfctl_set_limit(pf, $1, $2) != 0) {
4395 yyerror("unable to set limit %s %u", $1, $2);
4403 limit_list : limit_list comma limit_spec optnl
4411 yesno : NO { $$ = 0; }
4413 if (!strcmp($1, "yes"))
4416 yyerror("invalid value '%s', expected 'yes' "
4425 unaryop : '=' { $$ = PF_OP_EQ; }
4426 | '!' '=' { $$ = PF_OP_NE; }
4427 | '<' '=' { $$ = PF_OP_LE; }
4428 | '<' { $$ = PF_OP_LT; }
4429 | '>' '=' { $$ = PF_OP_GE; }
4430 | '>' { $$ = PF_OP_GT; }
4436 yyerror(const char *fmt, ...)
4442 fprintf(stderr, "%s:%d: ", file->name, yylval.lineno);
4443 vfprintf(stderr, fmt, ap);
4444 fprintf(stderr, "\n");
4450 disallow_table(struct node_host *h, const char *fmt)
4452 for (; h != NULL; h = h->next)
4453 if (h->addr.type == PF_ADDR_TABLE) {
4454 yyerror(fmt, h->addr.v.tblname);
4461 disallow_urpf_failed(struct node_host *h, const char *fmt)
4463 for (; h != NULL; h = h->next)
4464 if (h->addr.type == PF_ADDR_URPFFAILED) {
4472 disallow_alias(struct node_host *h, const char *fmt)
4474 for (; h != NULL; h = h->next)
4475 if (DYNIF_MULTIADDR(h->addr)) {
4476 yyerror(fmt, h->addr.v.tblname);
4483 rule_consistent(struct pf_rule *r, int anchor_call)
4487 switch (r->action) {
4492 problems = filter_consistent(r, anchor_call);
4496 problems = nat_consistent(r);
4500 problems = rdr_consistent(r);
4511 filter_consistent(struct pf_rule *r, int anchor_call)
4515 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP &&
4516 (r->src.port_op || r->dst.port_op)) {
4517 yyerror("port only applies to tcp/udp");
4520 if (r->proto != IPPROTO_ICMP && r->proto != IPPROTO_ICMPV6 &&
4521 (r->type || r->code)) {
4522 yyerror("icmp-type/code only applies to icmp");
4525 if (!r->af && (r->type || r->code)) {
4526 yyerror("must indicate address family with icmp-type/code");
4529 if (r->overload_tblname[0] &&
4530 r->max_src_conn == 0 && r->max_src_conn_rate.seconds == 0) {
4531 yyerror("'overload' requires 'max-src-conn' "
4532 "or 'max-src-conn-rate'");
4535 if ((r->proto == IPPROTO_ICMP && r->af == AF_INET6) ||
4536 (r->proto == IPPROTO_ICMPV6 && r->af == AF_INET)) {
4537 yyerror("proto %s doesn't match address family %s",
4538 r->proto == IPPROTO_ICMP ? "icmp" : "icmp6",
4539 r->af == AF_INET ? "inet" : "inet6");
4542 if (r->allow_opts && r->action != PF_PASS) {
4543 yyerror("allow-opts can only be specified for pass rules");
4546 if (r->rule_flag & PFRULE_FRAGMENT && (r->src.port_op ||
4547 r->dst.port_op || r->flagset || r->type || r->code)) {
4548 yyerror("fragments can be filtered only on IP header fields");
4551 if (r->rule_flag & PFRULE_RETURNRST && r->proto != IPPROTO_TCP) {
4552 yyerror("return-rst can only be applied to TCP rules");
4555 if (r->max_src_nodes && !(r->rule_flag & PFRULE_RULESRCTRACK)) {
4556 yyerror("max-src-nodes requires 'source-track rule'");
4559 if (r->action == PF_DROP && r->keep_state) {
4560 yyerror("keep state on block rules doesn't make sense");
4563 if (r->rule_flag & PFRULE_STATESLOPPY &&
4564 (r->keep_state == PF_STATE_MODULATE ||
4565 r->keep_state == PF_STATE_SYNPROXY)) {
4566 yyerror("sloppy state matching cannot be used with "
4567 "synproxy state or modulate state");
4574 nat_consistent(struct pf_rule *r)
4576 return (0); /* yeah! */
4580 rdr_consistent(struct pf_rule *r)
4584 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP) {
4585 if (r->src.port_op) {
4586 yyerror("src port only applies to tcp/udp");
4589 if (r->dst.port_op) {
4590 yyerror("dst port only applies to tcp/udp");
4593 if (r->rpool.proxy_port[0]) {
4594 yyerror("rpool port only applies to tcp/udp");
4598 if (r->dst.port_op &&
4599 r->dst.port_op != PF_OP_EQ && r->dst.port_op != PF_OP_RRG) {
4600 yyerror("invalid port operator for rdr destination port");
4607 process_tabledef(char *name, struct table_opts *opts)
4609 struct pfr_buffer ab;
4610 struct node_tinit *ti;
4612 bzero(&ab, sizeof(ab));
4613 ab.pfrb_type = PFRB_ADDRS;
4614 SIMPLEQ_FOREACH(ti, &opts->init_nodes, entries) {
4616 if (pfr_buf_load(&ab, ti->file, 0, append_addr)) {
4618 yyerror("cannot load \"%s\": %s",
4619 ti->file, strerror(errno));
4621 yyerror("file \"%s\" contains bad data",
4626 if (append_addr_host(&ab, ti->host, 0, 0)) {
4627 yyerror("cannot create address buffer: %s",
4632 if (pf->opts & PF_OPT_VERBOSE)
4633 print_tabledef(name, opts->flags, opts->init_addr,
4635 if (!(pf->opts & PF_OPT_NOACTION) &&
4636 pfctl_define_table(name, opts->flags, opts->init_addr,
4637 pf->anchor->name, &ab, pf->anchor->ruleset.tticket)) {
4638 yyerror("cannot define table %s: %s", name,
4639 pfr_strerror(errno));
4655 /* macro gore, but you should've seen the prior indentation nightmare... */
4657 #define FREE_LIST(T,r) \
4660 while (node != NULL) { \
4662 node = node->next; \
4667 #define LOOP_THROUGH(T,n,r,C) \
4671 r = calloc(1, sizeof(T)); \
4673 err(1, "LOOP: calloc"); \
4677 while (n != NULL) { \
4686 expand_label_str(char *label, size_t len, const char *srch, const char *repl)
4691 if ((tmp = calloc(1, len)) == NULL)
4692 err(1, "expand_label_str: calloc");
4694 while ((q = strstr(p, srch)) != NULL) {
4696 if ((strlcat(tmp, p, len) >= len) ||
4697 (strlcat(tmp, repl, len) >= len))
4698 errx(1, "expand_label: label too long");
4702 if (strlcat(tmp, p, len) >= len)
4703 errx(1, "expand_label: label too long");
4704 strlcpy(label, tmp, len); /* always fits */
4709 expand_label_if(const char *name, char *label, size_t len, const char *ifname)
4711 if (strstr(label, name) != NULL) {
4713 expand_label_str(label, len, name, "any");
4715 expand_label_str(label, len, name, ifname);
4720 expand_label_addr(const char *name, char *label, size_t len, sa_family_t af,
4721 struct node_host *h)
4723 char tmp[64], tmp_not[66];
4725 if (strstr(label, name) != NULL) {
4726 switch (h->addr.type) {
4727 case PF_ADDR_DYNIFTL:
4728 snprintf(tmp, sizeof(tmp), "(%s)", h->addr.v.ifname);
4731 snprintf(tmp, sizeof(tmp), "<%s>", h->addr.v.tblname);
4733 case PF_ADDR_NOROUTE:
4734 snprintf(tmp, sizeof(tmp), "no-route");
4736 case PF_ADDR_URPFFAILED:
4737 snprintf(tmp, sizeof(tmp), "urpf-failed");
4739 case PF_ADDR_ADDRMASK:
4740 if (!af || (PF_AZERO(&h->addr.v.a.addr, af) &&
4741 PF_AZERO(&h->addr.v.a.mask, af)))
4742 snprintf(tmp, sizeof(tmp), "any");
4747 if (inet_ntop(af, &h->addr.v.a.addr, a,
4749 snprintf(tmp, sizeof(tmp), "?");
4751 bits = unmask(&h->addr.v.a.mask, af);
4752 if ((af == AF_INET && bits < 32) ||
4753 (af == AF_INET6 && bits < 128))
4754 snprintf(tmp, sizeof(tmp),
4757 snprintf(tmp, sizeof(tmp),
4763 snprintf(tmp, sizeof(tmp), "?");
4768 snprintf(tmp_not, sizeof(tmp_not), "! %s", tmp);
4769 expand_label_str(label, len, name, tmp_not);
4771 expand_label_str(label, len, name, tmp);
4776 expand_label_port(const char *name, char *label, size_t len,
4777 struct node_port *port)
4779 char a1[6], a2[6], op[13] = "";
4781 if (strstr(label, name) != NULL) {
4782 snprintf(a1, sizeof(a1), "%u", ntohs(port->port[0]));
4783 snprintf(a2, sizeof(a2), "%u", ntohs(port->port[1]));
4786 else if (port->op == PF_OP_IRG)
4787 snprintf(op, sizeof(op), "%s><%s", a1, a2);
4788 else if (port->op == PF_OP_XRG)
4789 snprintf(op, sizeof(op), "%s<>%s", a1, a2);
4790 else if (port->op == PF_OP_EQ)
4791 snprintf(op, sizeof(op), "%s", a1);
4792 else if (port->op == PF_OP_NE)
4793 snprintf(op, sizeof(op), "!=%s", a1);
4794 else if (port->op == PF_OP_LT)
4795 snprintf(op, sizeof(op), "<%s", a1);
4796 else if (port->op == PF_OP_LE)
4797 snprintf(op, sizeof(op), "<=%s", a1);
4798 else if (port->op == PF_OP_GT)
4799 snprintf(op, sizeof(op), ">%s", a1);
4800 else if (port->op == PF_OP_GE)
4801 snprintf(op, sizeof(op), ">=%s", a1);
4802 expand_label_str(label, len, name, op);
4807 expand_label_proto(const char *name, char *label, size_t len, u_int8_t proto)
4809 struct protoent *pe;
4812 if (strstr(label, name) != NULL) {
4813 pe = getprotobynumber(proto);
4815 expand_label_str(label, len, name, pe->p_name);
4817 snprintf(n, sizeof(n), "%u", proto);
4818 expand_label_str(label, len, name, n);
4824 expand_label_nr(const char *name, char *label, size_t len)
4828 if (strstr(label, name) != NULL) {
4829 snprintf(n, sizeof(n), "%u", pf->anchor->match);
4830 expand_label_str(label, len, name, n);
4835 expand_label(char *label, size_t len, const char *ifname, sa_family_t af,
4836 struct node_host *src_host, struct node_port *src_port,
4837 struct node_host *dst_host, struct node_port *dst_port,
4840 expand_label_if("$if", label, len, ifname);
4841 expand_label_addr("$srcaddr", label, len, af, src_host);
4842 expand_label_addr("$dstaddr", label, len, af, dst_host);
4843 expand_label_port("$srcport", label, len, src_port);
4844 expand_label_port("$dstport", label, len, dst_port);
4845 expand_label_proto("$proto", label, len, proto);
4846 expand_label_nr("$nr", label, len);
4850 expand_altq(struct pf_altq *a, struct node_if *interfaces,
4851 struct node_queue *nqueues, struct node_queue_bw bwspec,
4852 struct node_queue_opt *opts)
4854 struct pf_altq pa, pb;
4855 char qname[PF_QNAME_SIZE];
4856 struct node_queue *n;
4857 struct node_queue_bw bw;
4860 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
4861 FREE_LIST(struct node_if, interfaces);
4863 FREE_LIST(struct node_queue, nqueues);
4867 LOOP_THROUGH(struct node_if, interface, interfaces,
4868 memcpy(&pa, a, sizeof(struct pf_altq));
4869 if (strlcpy(pa.ifname, interface->ifname,
4870 sizeof(pa.ifname)) >= sizeof(pa.ifname))
4871 errx(1, "expand_altq: strlcpy");
4873 if (interface->not) {
4874 yyerror("altq on ! <interface> is not supported");
4877 if (eval_pfaltq(pf, &pa, &bwspec, opts))
4880 if (pfctl_add_altq(pf, &pa))
4883 if (pf->opts & PF_OPT_VERBOSE) {
4884 print_altq(&pf->paltq->altq, 0,
4886 if (nqueues && nqueues->tail) {
4888 LOOP_THROUGH(struct node_queue, queue,
4898 if (pa.scheduler == ALTQT_CBQ ||
4899 pa.scheduler == ALTQT_HFSC) {
4900 /* now create a root queue */
4901 memset(&pb, 0, sizeof(struct pf_altq));
4902 if (strlcpy(qname, "root_", sizeof(qname)) >=
4904 errx(1, "expand_altq: strlcpy");
4905 if (strlcat(qname, interface->ifname,
4906 sizeof(qname)) >= sizeof(qname))
4907 errx(1, "expand_altq: strlcat");
4908 if (strlcpy(pb.qname, qname,
4909 sizeof(pb.qname)) >= sizeof(pb.qname))
4910 errx(1, "expand_altq: strlcpy");
4911 if (strlcpy(pb.ifname, interface->ifname,
4912 sizeof(pb.ifname)) >= sizeof(pb.ifname))
4913 errx(1, "expand_altq: strlcpy");
4914 pb.qlimit = pa.qlimit;
4915 pb.scheduler = pa.scheduler;
4916 bw.bw_absolute = pa.ifbandwidth;
4918 if (eval_pfqueue(pf, &pb, &bw, opts))
4921 if (pfctl_add_altq(pf, &pb))
4925 LOOP_THROUGH(struct node_queue, queue, nqueues,
4926 n = calloc(1, sizeof(struct node_queue));
4928 err(1, "expand_altq: calloc");
4929 if (pa.scheduler == ALTQT_CBQ ||
4930 pa.scheduler == ALTQT_HFSC)
4931 if (strlcpy(n->parent, qname,
4932 sizeof(n->parent)) >=
4934 errx(1, "expand_altq: strlcpy");
4935 if (strlcpy(n->queue, queue->queue,
4936 sizeof(n->queue)) >= sizeof(n->queue))
4937 errx(1, "expand_altq: strlcpy");
4938 if (strlcpy(n->ifname, interface->ifname,
4939 sizeof(n->ifname)) >= sizeof(n->ifname))
4940 errx(1, "expand_altq: strlcpy");
4941 n->scheduler = pa.scheduler;
4947 queues->tail->next = n;
4953 FREE_LIST(struct node_if, interfaces);
4955 FREE_LIST(struct node_queue, nqueues);
4961 expand_queue(struct pf_altq *a, struct node_if *interfaces,
4962 struct node_queue *nqueues, struct node_queue_bw bwspec,
4963 struct node_queue_opt *opts)
4965 struct node_queue *n, *nq;
4970 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
4971 FREE_LIST(struct node_queue, nqueues);
4975 if (queues == NULL) {
4976 yyerror("queue %s has no parent", a->qname);
4977 FREE_LIST(struct node_queue, nqueues);
4981 LOOP_THROUGH(struct node_if, interface, interfaces,
4982 LOOP_THROUGH(struct node_queue, tqueue, queues,
4983 if (!strncmp(a->qname, tqueue->queue, PF_QNAME_SIZE) &&
4984 (interface->ifname[0] == 0 ||
4985 (!interface->not && !strncmp(interface->ifname,
4986 tqueue->ifname, IFNAMSIZ)) ||
4987 (interface->not && strncmp(interface->ifname,
4988 tqueue->ifname, IFNAMSIZ)))) {
4989 /* found ourself in queues */
4992 memcpy(&pa, a, sizeof(struct pf_altq));
4994 if (pa.scheduler != ALTQT_NONE &&
4995 pa.scheduler != tqueue->scheduler) {
4996 yyerror("exactly one scheduler type "
4997 "per interface allowed");
5000 pa.scheduler = tqueue->scheduler;
5002 /* scheduler dependent error checking */
5003 switch (pa.scheduler) {
5005 if (nqueues != NULL) {
5006 yyerror("priq queues cannot "
5007 "have child queues");
5010 if (bwspec.bw_absolute > 0 ||
5011 bwspec.bw_percent < 100) {
5012 yyerror("priq doesn't take "
5021 if (strlcpy(pa.ifname, tqueue->ifname,
5022 sizeof(pa.ifname)) >= sizeof(pa.ifname))
5023 errx(1, "expand_queue: strlcpy");
5024 if (strlcpy(pa.parent, tqueue->parent,
5025 sizeof(pa.parent)) >= sizeof(pa.parent))
5026 errx(1, "expand_queue: strlcpy");
5028 if (eval_pfqueue(pf, &pa, &bwspec, opts))
5031 if (pfctl_add_altq(pf, &pa))
5034 for (nq = nqueues; nq != NULL; nq = nq->next) {
5035 if (!strcmp(a->qname, nq->queue)) {
5036 yyerror("queue cannot have "
5042 sizeof(struct node_queue));
5044 err(1, "expand_queue: calloc");
5045 if (strlcpy(n->parent, a->qname,
5046 sizeof(n->parent)) >=
5048 errx(1, "expand_queue strlcpy");
5049 if (strlcpy(n->queue, nq->queue,
5050 sizeof(n->queue)) >=
5052 errx(1, "expand_queue strlcpy");
5053 if (strlcpy(n->ifname, tqueue->ifname,
5054 sizeof(n->ifname)) >=
5056 errx(1, "expand_queue strlcpy");
5057 n->scheduler = tqueue->scheduler;
5063 queues->tail->next = n;
5067 if ((pf->opts & PF_OPT_VERBOSE) && (
5068 (found == 1 && interface->ifname[0] == 0) ||
5069 (found > 0 && interface->ifname[0] != 0))) {
5070 print_queue(&pf->paltq->altq, 0,
5071 &bwspec, interface->ifname[0] != 0,
5073 if (nqueues && nqueues->tail) {
5075 LOOP_THROUGH(struct node_queue,
5088 FREE_LIST(struct node_queue, nqueues);
5089 FREE_LIST(struct node_if, interfaces);
5092 yyerror("queue %s has no parent", a->qname);
5103 expand_rule(struct pf_rule *r,
5104 struct node_if *interfaces, struct node_host *rpool_hosts,
5105 struct node_proto *protos, struct node_os *src_oses,
5106 struct node_host *src_hosts, struct node_port *src_ports,
5107 struct node_host *dst_hosts, struct node_port *dst_ports,
5108 struct node_uid *uids, struct node_gid *gids, struct node_icmp *icmp_types,
5109 const char *anchor_call)
5111 sa_family_t af = r->af;
5112 int added = 0, error = 0;
5113 char ifname[IF_NAMESIZE];
5114 char label[PF_RULE_LABEL_SIZE];
5115 char tagname[PF_TAG_NAME_SIZE];
5116 char match_tagname[PF_TAG_NAME_SIZE];
5117 struct pf_pooladdr *pa;
5118 struct node_host *h;
5119 u_int8_t flags, flagset, keep_state;
5121 if (strlcpy(label, r->label, sizeof(label)) >= sizeof(label))
5122 errx(1, "expand_rule: strlcpy");
5123 if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname))
5124 errx(1, "expand_rule: strlcpy");
5125 if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >=
5126 sizeof(match_tagname))
5127 errx(1, "expand_rule: strlcpy");
5129 flagset = r->flagset;
5130 keep_state = r->keep_state;
5132 LOOP_THROUGH(struct node_if, interface, interfaces,
5133 LOOP_THROUGH(struct node_proto, proto, protos,
5134 LOOP_THROUGH(struct node_icmp, icmp_type, icmp_types,
5135 LOOP_THROUGH(struct node_host, src_host, src_hosts,
5136 LOOP_THROUGH(struct node_port, src_port, src_ports,
5137 LOOP_THROUGH(struct node_os, src_os, src_oses,
5138 LOOP_THROUGH(struct node_host, dst_host, dst_hosts,
5139 LOOP_THROUGH(struct node_port, dst_port, dst_ports,
5140 LOOP_THROUGH(struct node_uid, uid, uids,
5141 LOOP_THROUGH(struct node_gid, gid, gids,
5144 /* for link-local IPv6 address, interface must match up */
5145 if ((r->af && src_host->af && r->af != src_host->af) ||
5146 (r->af && dst_host->af && r->af != dst_host->af) ||
5147 (src_host->af && dst_host->af &&
5148 src_host->af != dst_host->af) ||
5149 (src_host->ifindex && dst_host->ifindex &&
5150 src_host->ifindex != dst_host->ifindex) ||
5151 (src_host->ifindex && *interface->ifname &&
5152 src_host->ifindex != if_nametoindex(interface->ifname)) ||
5153 (dst_host->ifindex && *interface->ifname &&
5154 dst_host->ifindex != if_nametoindex(interface->ifname)))
5156 if (!r->af && src_host->af)
5157 r->af = src_host->af;
5158 else if (!r->af && dst_host->af)
5159 r->af = dst_host->af;
5161 if (*interface->ifname)
5162 strlcpy(r->ifname, interface->ifname,
5164 else if (if_indextoname(src_host->ifindex, ifname))
5165 strlcpy(r->ifname, ifname, sizeof(r->ifname));
5166 else if (if_indextoname(dst_host->ifindex, ifname))
5167 strlcpy(r->ifname, ifname, sizeof(r->ifname));
5169 memset(r->ifname, '\0', sizeof(r->ifname));
5171 if (strlcpy(r->label, label, sizeof(r->label)) >=
5173 errx(1, "expand_rule: strlcpy");
5174 if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >=
5176 errx(1, "expand_rule: strlcpy");
5177 if (strlcpy(r->match_tagname, match_tagname,
5178 sizeof(r->match_tagname)) >= sizeof(r->match_tagname))
5179 errx(1, "expand_rule: strlcpy");
5180 expand_label(r->label, PF_RULE_LABEL_SIZE, r->ifname, r->af,
5181 src_host, src_port, dst_host, dst_port, proto->proto);
5182 expand_label(r->tagname, PF_TAG_NAME_SIZE, r->ifname, r->af,
5183 src_host, src_port, dst_host, dst_port, proto->proto);
5184 expand_label(r->match_tagname, PF_TAG_NAME_SIZE, r->ifname,
5185 r->af, src_host, src_port, dst_host, dst_port,
5188 error += check_netmask(src_host, r->af);
5189 error += check_netmask(dst_host, r->af);
5191 r->ifnot = interface->not;
5192 r->proto = proto->proto;
5193 r->src.addr = src_host->addr;
5194 r->src.neg = src_host->not;
5195 r->src.port[0] = src_port->port[0];
5196 r->src.port[1] = src_port->port[1];
5197 r->src.port_op = src_port->op;
5198 r->dst.addr = dst_host->addr;
5199 r->dst.neg = dst_host->not;
5200 r->dst.port[0] = dst_port->port[0];
5201 r->dst.port[1] = dst_port->port[1];
5202 r->dst.port_op = dst_port->op;
5203 r->uid.op = uid->op;
5204 r->uid.uid[0] = uid->uid[0];
5205 r->uid.uid[1] = uid->uid[1];
5206 r->gid.op = gid->op;
5207 r->gid.gid[0] = gid->gid[0];
5208 r->gid.gid[1] = gid->gid[1];
5209 r->type = icmp_type->type;
5210 r->code = icmp_type->code;
5212 if ((keep_state == PF_STATE_MODULATE ||
5213 keep_state == PF_STATE_SYNPROXY) &&
5214 r->proto && r->proto != IPPROTO_TCP)
5215 r->keep_state = PF_STATE_NORMAL;
5217 r->keep_state = keep_state;
5219 if (r->proto && r->proto != IPPROTO_TCP) {
5224 r->flagset = flagset;
5226 if (icmp_type->proto && r->proto != icmp_type->proto) {
5227 yyerror("icmp-type mismatch");
5231 if (src_os && src_os->os) {
5232 r->os_fingerprint = pfctl_get_fingerprint(src_os->os);
5233 if ((pf->opts & PF_OPT_VERBOSE2) &&
5234 r->os_fingerprint == PF_OSFP_NOMATCH)
5236 "warning: unknown '%s' OS fingerprint\n",
5239 r->os_fingerprint = PF_OSFP_ANY;
5242 TAILQ_INIT(&r->rpool.list);
5243 for (h = rpool_hosts; h != NULL; h = h->next) {
5244 pa = calloc(1, sizeof(struct pf_pooladdr));
5246 err(1, "expand_rule: calloc");
5248 if (h->ifname != NULL) {
5249 if (strlcpy(pa->ifname, h->ifname,
5250 sizeof(pa->ifname)) >=
5252 errx(1, "expand_rule: strlcpy");
5255 TAILQ_INSERT_TAIL(&r->rpool.list, pa, entries);
5258 if (rule_consistent(r, anchor_call[0]) < 0 || error)
5259 yyerror("skipping rule due to errors");
5261 r->nr = pf->astack[pf->asd]->match++;
5262 pfctl_add_rule(pf, r, anchor_call);
5268 FREE_LIST(struct node_if, interfaces);
5269 FREE_LIST(struct node_proto, protos);
5270 FREE_LIST(struct node_host, src_hosts);
5271 FREE_LIST(struct node_port, src_ports);
5272 FREE_LIST(struct node_os, src_oses);
5273 FREE_LIST(struct node_host, dst_hosts);
5274 FREE_LIST(struct node_port, dst_ports);
5275 FREE_LIST(struct node_uid, uids);
5276 FREE_LIST(struct node_gid, gids);
5277 FREE_LIST(struct node_icmp, icmp_types);
5278 FREE_LIST(struct node_host, rpool_hosts);
5281 yyerror("rule expands to no valid combination");
5285 expand_skip_interface(struct node_if *interfaces)
5289 if (!interfaces || (!interfaces->next && !interfaces->not &&
5290 !strcmp(interfaces->ifname, "none"))) {
5291 if (pf->opts & PF_OPT_VERBOSE)
5292 printf("set skip on none\n");
5293 errs = pfctl_set_interface_flags(pf, "", PFI_IFLAG_SKIP, 0);
5297 if (pf->opts & PF_OPT_VERBOSE)
5298 printf("set skip on {");
5299 LOOP_THROUGH(struct node_if, interface, interfaces,
5300 if (pf->opts & PF_OPT_VERBOSE)
5301 printf(" %s", interface->ifname);
5302 if (interface->not) {
5303 yyerror("skip on ! <interface> is not supported");
5306 errs += pfctl_set_interface_flags(pf,
5307 interface->ifname, PFI_IFLAG_SKIP, 1);
5309 if (pf->opts & PF_OPT_VERBOSE)
5312 FREE_LIST(struct node_if, interfaces);
5324 check_rulestate(int desired_state)
5326 if (require_order && (rulestate > desired_state)) {
5327 yyerror("Rules must be in order: options, normalization, "
5328 "queueing, translation, filtering");
5331 rulestate = desired_state;
5336 kw_cmp(const void *k, const void *e)
5338 return (strcmp(k, ((const struct keywords *)e)->k_name));
5344 /* this has to be sorted always */
5345 static const struct keywords keywords[] = {
5347 { "allow-opts", ALLOWOPTS},
5349 { "anchor", ANCHOR},
5350 { "antispoof", ANTISPOOF},
5352 { "bandwidth", BANDWIDTH},
5354 { "binat-anchor", BINATANCHOR},
5355 { "bitmask", BITMASK},
5357 { "block-policy", BLOCKPOLICY},
5358 { "buckets", BUCKETS},
5362 { "crop", FRAGCROP},
5364 { "divert-reply", DIVERTREPLY},
5365 { "divert-to", DIVERTTO},
5367 { "drop-ovl", FRAGDROP},
5370 { "fastroute", FASTROUTE},
5371 { "file", FILENAME},
5372 { "fingerprints", FINGERPRINTS},
5374 { "floating", FLOATING},
5377 { "fragment", FRAGMENT},
5379 { "global", GLOBAL},
5383 { "hostid", HOSTID},
5384 { "icmp-type", ICMPTYPE},
5385 { "icmp6-type", ICMP6TYPE},
5386 { "if-bound", IFBOUND},
5388 { "include", INCLUDE},
5391 { "interval", INTERVAL},
5395 { "linkshare", LINKSHARE},
5398 { "loginterface", LOGINTERFACE},
5400 { "max-mss", MAXMSS},
5401 { "max-src-conn", MAXSRCCONN},
5402 { "max-src-conn-rate", MAXSRCCONNRATE},
5403 { "max-src-nodes", MAXSRCNODES},
5404 { "max-src-states", MAXSRCSTATES},
5405 { "min-ttl", MINTTL},
5406 { "modulate", MODULATE},
5408 { "nat-anchor", NATANCHOR},
5411 { "no-route", NOROUTE},
5412 { "no-sync", NOSYNC},
5414 { "optimization", OPTIMIZATION},
5417 { "overload", OVERLOAD},
5420 { "priority", PRIORITY},
5422 { "probability", PROBABILITY},
5424 { "qlimit", QLIMIT},
5427 { "random", RANDOM},
5428 { "random-id", RANDOMID},
5430 { "rdr-anchor", RDRANCHOR},
5431 { "realtime", REALTIME},
5432 { "reassemble", REASSEMBLE},
5433 { "reply-to", REPLYTO},
5434 { "require-order", REQUIREORDER},
5435 { "return", RETURN},
5436 { "return-icmp", RETURNICMP},
5437 { "return-icmp6", RETURNICMP6},
5438 { "return-rst", RETURNRST},
5439 { "round-robin", ROUNDROBIN},
5441 { "route-to", ROUTETO},
5442 { "rtable", RTABLE},
5444 { "ruleset-optimization", RULESET_OPTIMIZATION},
5447 { "set-tos", SETTOS},
5449 { "sloppy", SLOPPY},
5450 { "source-hash", SOURCEHASH},
5451 { "source-track", SOURCETRACK},
5453 { "state-defaults", STATEDEFAULTS},
5454 { "state-policy", STATEPOLICY},
5455 { "static-port", STATICPORT},
5456 { "sticky-address", STICKYADDRESS},
5457 { "synproxy", SYNPROXY},
5460 { "tagged", TAGGED},
5461 { "target", TARGET},
5462 { "tbrsize", TBRSIZE},
5463 { "timeout", TIMEOUT},
5467 { "upperlimit", UPPERLIMIT},
5468 { "urpf-failed", URPFFAILED},
5471 const struct keywords *p;
5473 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
5474 sizeof(keywords[0]), kw_cmp);
5478 fprintf(stderr, "%s: %d\n", s, p->k_val);
5482 fprintf(stderr, "string: %s\n", s);
5487 #define MAXPUSHBACK 128
5491 char pushback_buffer[MAXPUSHBACK];
5492 int pushback_index = 0;
5500 /* Read character from the parsebuffer instead of input. */
5501 if (parseindex >= 0) {
5502 c = parsebuf[parseindex++];
5511 return (pushback_buffer[--pushback_index]);
5514 if ((c = getc(file->stream)) == EOF) {
5515 yyerror("reached end of file while parsing quoted string");
5516 if (popfile() == EOF)
5523 while ((c = getc(file->stream)) == '\\') {
5524 next = getc(file->stream);
5529 yylval.lineno = file->lineno;
5534 if (popfile() == EOF)
5536 c = getc(file->stream);
5548 if (parseindex >= 0)
5551 if (pushback_index < MAXPUSHBACK-1)
5552 return (pushback_buffer[pushback_index++] = c);
5564 /* skip to either EOF or the first real EOL */
5567 c = pushback_buffer[--pushback_index];
5585 int quotec, next, c;
5590 while ((c = lgetc(0)) == ' ' || c == '\t')
5593 yylval.lineno = file->lineno;
5595 while ((c = lgetc(0)) != '\n' && c != EOF)
5597 if (c == '$' && parsebuf == NULL) {
5599 if ((c = lgetc(0)) == EOF)
5602 if (p + 1 >= buf + sizeof(buf) - 1) {
5603 yyerror("string too long");
5606 if (isalnum(c) || c == '_') {
5616 yyerror("macro '%s' not defined", buf);
5629 if ((c = lgetc(quotec)) == EOF)
5634 } else if (c == '\\') {
5635 if ((next = lgetc(quotec)) == EOF)
5637 if (next == quotec || c == ' ' || c == '\t')
5639 else if (next == '\n')
5643 } else if (c == quotec) {
5647 if (p + 1 >= buf + sizeof(buf) - 1) {
5648 yyerror("string too long");
5653 yylval.v.string = strdup(buf);
5654 if (yylval.v.string == NULL)
5655 err(1, "yylex: strdup");
5660 yylval.v.i = PF_OP_XRG;
5661 return (PORTBINARY);
5668 yylval.v.i = PF_OP_IRG;
5669 return (PORTBINARY);
5681 #define allowed_to_end_number(x) \
5682 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
5684 if (c == '-' || isdigit(c)) {
5687 if ((unsigned)(p-buf) >= sizeof(buf)) {
5688 yyerror("string too long");
5691 } while ((c = lgetc(0)) != EOF && isdigit(c));
5693 if (p == buf + 1 && buf[0] == '-')
5695 if (c == EOF || allowed_to_end_number(c)) {
5696 const char *errstr = NULL;
5699 yylval.v.number = strtonum(buf, LLONG_MIN,
5700 LLONG_MAX, &errstr);
5702 yyerror("\"%s\" invalid number: %s",
5717 #define allowed_in_string(x) \
5718 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
5719 x != '{' && x != '}' && x != '<' && x != '>' && \
5720 x != '!' && x != '=' && x != '/' && x != '#' && \
5723 if (isalnum(c) || c == ':' || c == '_') {
5726 if ((unsigned)(p-buf) >= sizeof(buf)) {
5727 yyerror("string too long");
5730 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
5733 if ((token = lookup(buf)) == STRING)
5734 if ((yylval.v.string = strdup(buf)) == NULL)
5735 err(1, "yylex: strdup");
5739 yylval.lineno = file->lineno;
5748 check_file_secrecy(int fd, const char *fname)
5752 if (fstat(fd, &st)) {
5753 warn("cannot stat %s", fname);
5756 if (st.st_uid != 0 && st.st_uid != getuid()) {
5757 warnx("%s: owner not root or current user", fname);
5760 if (st.st_mode & (S_IRWXG | S_IRWXO)) {
5761 warnx("%s: group/world readable/writeable", fname);
5768 pushfile(const char *name, int secret)
5772 if ((nfile = calloc(1, sizeof(struct file))) == NULL ||
5773 (nfile->name = strdup(name)) == NULL) {
5777 if (TAILQ_FIRST(&files) == NULL && strcmp(nfile->name, "-") == 0) {
5778 nfile->stream = stdin;
5780 if ((nfile->name = strdup("stdin")) == NULL) {
5785 } else if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
5786 warn("%s", nfile->name);
5790 } else if (secret &&
5791 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
5792 fclose(nfile->stream);
5798 TAILQ_INSERT_TAIL(&files, nfile, entry);
5807 if ((prev = TAILQ_PREV(file, files, entry)) != NULL) {
5808 prev->errors += file->errors;
5809 TAILQ_REMOVE(&files, file, entry);
5810 fclose(file->stream);
5820 parse_config(char *filename, struct pfctl *xpf)
5827 rulestate = PFCTL_STATE_NONE;
5828 returnicmpdefault = (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
5829 returnicmp6default =
5830 (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
5831 blockpolicy = PFRULE_DROP;
5834 if ((file = pushfile(filename, 0)) == NULL) {
5835 warn("cannot open the main config file!");
5840 errors = file->errors;
5843 /* Free macros and check which have not been used. */
5844 while ((sym = TAILQ_FIRST(&symhead))) {
5845 if ((pf->opts & PF_OPT_VERBOSE2) && !sym->used)
5846 fprintf(stderr, "warning: macro '%s' not "
5847 "used\n", sym->nam);
5850 TAILQ_REMOVE(&symhead, sym, entry);
5854 return (errors ? -1 : 0);
5858 symset(const char *nam, const char *val, int persist)
5862 for (sym = TAILQ_FIRST(&symhead); sym && strcmp(nam, sym->nam);
5863 sym = TAILQ_NEXT(sym, entry))
5867 if (sym->persist == 1)
5872 TAILQ_REMOVE(&symhead, sym, entry);
5876 if ((sym = calloc(1, sizeof(*sym))) == NULL)
5879 sym->nam = strdup(nam);
5880 if (sym->nam == NULL) {
5884 sym->val = strdup(val);
5885 if (sym->val == NULL) {
5891 sym->persist = persist;
5892 TAILQ_INSERT_TAIL(&symhead, sym, entry);
5897 pfctl_cmdline_symset(char *s)
5902 if ((val = strrchr(s, '=')) == NULL)
5905 if ((sym = malloc(strlen(s) - strlen(val) + 1)) == NULL)
5906 err(1, "pfctl_cmdline_symset: malloc");
5908 strlcpy(sym, s, strlen(s) - strlen(val) + 1);
5910 ret = symset(sym, val + 1, 1);
5917 symget(const char *nam)
5921 TAILQ_FOREACH(sym, &symhead, entry)
5922 if (strcmp(nam, sym->nam) == 0) {
5930 mv_rules(struct pf_ruleset *src, struct pf_ruleset *dst)
5935 for (i = 0; i < PF_RULESET_MAX; ++i) {
5936 while ((r = TAILQ_FIRST(src->rules[i].active.ptr))
5938 TAILQ_REMOVE(src->rules[i].active.ptr, r, entries);
5939 TAILQ_INSERT_TAIL(dst->rules[i].active.ptr, r, entries);
5940 dst->anchor->match++;
5942 src->anchor->match = 0;
5943 while ((r = TAILQ_FIRST(src->rules[i].inactive.ptr))
5945 TAILQ_REMOVE(src->rules[i].inactive.ptr, r, entries);
5946 TAILQ_INSERT_TAIL(dst->rules[i].inactive.ptr,
5953 decide_address_family(struct node_host *n, sa_family_t *af)
5955 if (*af != 0 || n == NULL)
5958 while ((n = n->next) != NULL) {
5967 remove_invalid_hosts(struct node_host **nh, sa_family_t *af)
5969 struct node_host *n = *nh, *prev = NULL;
5972 if (*af && n->af && n->af != *af) {
5973 /* unlink and free n */
5974 struct node_host *next = n->next;
5976 /* adjust tail pointer */
5977 if (n == (*nh)->tail)
5979 /* adjust previous node's next pointer */
5985 if (n->ifname != NULL)
5999 invalid_redirect(struct node_host *nh, sa_family_t af)
6002 struct node_host *n;
6004 /* tables and dyniftl are ok without an address family */
6005 for (n = nh; n != NULL; n = n->next) {
6006 if (n->addr.type != PF_ADDR_TABLE &&
6007 n->addr.type != PF_ADDR_DYNIFTL) {
6008 yyerror("address family not given and "
6009 "translation address expands to multiple "
6010 "address families");
6016 yyerror("no translation address with matching address family "
6024 atoul(char *s, u_long *ulvalp)
6030 ulval = strtoul(s, &ep, 0);
6031 if (s[0] == '\0' || *ep != '\0')
6033 if (errno == ERANGE && ulval == ULONG_MAX)
6045 if (atoul(n, &ulval) == 0) {
6046 if (ulval > 65535) {
6047 yyerror("illegal port value %lu", ulval);
6050 return (htons(ulval));
6052 s = getservbyname(n, "tcp");
6054 s = getservbyname(n, "udp");
6056 yyerror("unknown port %s", n);
6064 rule_label(struct pf_rule *r, char *s)
6067 if (strlcpy(r->label, s, sizeof(r->label)) >=
6069 yyerror("rule label too long (max %d chars)",
6070 sizeof(r->label)-1);
6078 parseicmpspec(char *w, sa_family_t af)
6080 const struct icmpcodeent *p;
6085 icmptype = returnicmpdefault >> 8;
6087 icmptype = returnicmp6default >> 8;
6089 if (atoul(w, &ulval) == -1) {
6090 if ((p = geticmpcodebyname(icmptype, w, af)) == NULL) {
6091 yyerror("unknown icmp code %s", w);
6097 yyerror("invalid icmp code %lu", ulval);
6100 return (icmptype << 8 | ulval);
6104 parseport(char *port, struct range *r, int extensions)
6106 char *p = strchr(port, ':');
6109 if ((r->a = getservice(port)) == -1)
6115 if ((extensions & PPORT_STAR) && !strcmp(p+1, "*")) {
6117 if ((r->a = getservice(port)) == -1)
6123 if ((extensions & PPORT_RANGE)) {
6125 if ((r->a = getservice(port)) == -1 ||
6126 (r->b = getservice(p)) == -1)
6139 pfctl_load_anchors(int dev, struct pfctl *pf, struct pfr_buffer *trans)
6141 struct loadanchors *la;
6143 TAILQ_FOREACH(la, &loadanchorshead, entries) {
6144 if (pf->opts & PF_OPT_VERBOSE)
6145 fprintf(stderr, "\nLoading anchor %s from %s\n",
6146 la->anchorname, la->filename);
6147 if (pfctl_rules(dev, la->filename, pf->opts, pf->optimize,
6148 la->anchorname, trans) == -1)
6156 rt_tableid_max(void)
6160 size_t l = sizeof(fibs);
6162 if (sysctlbyname("net.fibs", &fibs, &l, NULL, 0) == -1)
6163 fibs = 16; /* XXX RT_MAXFIBS, at least limit it some. */
6165 * As the OpenBSD code only compares > and not >= we need to adjust
6166 * here given we only accept values of 0..n and want to avoid #ifdefs
6171 return (RT_TABLEID_MAX);