1 /* $OpenBSD: parse.y,v 1.554 2008/10/17 12:59:53 henning Exp $ */
4 * Copyright (c) 2001 Markus Friedl. All rights reserved.
5 * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
6 * Copyright (c) 2001 Theo de Raadt. All rights reserved.
7 * Copyright (c) 2002,2003 Henning Brauer. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/types.h>
34 #include <sys/socket.h>
37 #include <sys/sysctl.h>
40 #include <netinet/in.h>
41 #include <netinet/in_systm.h>
42 #include <netinet/ip.h>
43 #include <netinet/ip_icmp.h>
44 #include <netinet/icmp6.h>
45 #include <net/pfvar.h>
46 #include <arpa/inet.h>
47 #include <altq/altq.h>
48 #include <altq/altq_cbq.h>
49 #include <altq/altq_priq.h>
50 #include <altq/altq_hfsc.h>
67 #include "pfctl_parser.h"
70 static struct pfctl *pf = NULL;
72 static int rulestate = 0;
73 static u_int16_t returnicmpdefault =
74 (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
75 static u_int16_t returnicmp6default =
76 (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
77 static int blockpolicy = PFRULE_DROP;
78 static int require_order = 1;
79 static int default_statelock;
81 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
83 TAILQ_ENTRY(file) entry;
89 struct file *pushfile(const char *, int);
91 int check_file_secrecy(int, const char *);
94 int yyerror(const char *, ...);
95 int kw_cmp(const void *, const void *);
101 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
103 TAILQ_ENTRY(sym) entry;
109 int symset(const char *, const char *, int);
110 char *symget(const char *);
112 int atoul(char *, u_long *);
125 struct node_proto *next;
126 struct node_proto *tail;
132 struct node_port *next;
133 struct node_port *tail;
139 struct node_uid *next;
140 struct node_uid *tail;
146 struct node_gid *next;
147 struct node_gid *tail;
154 struct node_icmp *next;
155 struct node_icmp *tail;
158 enum { PF_STATE_OPT_MAX, PF_STATE_OPT_NOSYNC, PF_STATE_OPT_SRCTRACK,
159 PF_STATE_OPT_MAX_SRC_STATES, PF_STATE_OPT_MAX_SRC_CONN,
160 PF_STATE_OPT_MAX_SRC_CONN_RATE, PF_STATE_OPT_MAX_SRC_NODES,
161 PF_STATE_OPT_OVERLOAD, PF_STATE_OPT_STATELOCK,
162 PF_STATE_OPT_TIMEOUT, PF_STATE_OPT_SLOPPY, };
164 enum { PF_SRCTRACK_NONE, PF_SRCTRACK, PF_SRCTRACK_GLOBAL, PF_SRCTRACK_RULE };
166 struct node_state_opt {
169 u_int32_t max_states;
170 u_int32_t max_src_states;
171 u_int32_t max_src_conn;
178 char tblname[PF_TABLE_NAME_SIZE];
180 u_int32_t max_src_nodes;
188 struct node_state_opt *next;
189 struct node_state_opt *tail;
193 struct node_host *host;
194 struct node_port *port;
198 char queue[PF_QNAME_SIZE];
199 char parent[PF_QNAME_SIZE];
200 char ifname[IFNAMSIZ];
202 struct node_queue *next;
203 struct node_queue *tail;
206 struct node_qassign {
213 #define FOM_FLAGS 0x01
214 #define FOM_ICMP 0x02
216 #define FOM_KEEP 0x08
217 #define FOM_SRCTRACK 0x10
218 struct node_uid *uid;
219 struct node_gid *gid;
226 struct node_icmp *icmpspec;
231 struct node_state_opt *options;
236 struct node_qassign queues;
239 u_int8_t match_tag_not;
242 struct node_host *addr;
247 struct antispoof_opts {
254 #define SOM_MINTTL 0x01
255 #define SOM_MAXMSS 0x02
256 #define SOM_FRAGCACHE 0x04
257 #define SOM_SETTOS 0x08
266 u_int8_t match_tag_not;
272 #define QOM_BWSPEC 0x01
273 #define QOM_SCHEDULER 0x02
274 #define QOM_PRIORITY 0x04
275 #define QOM_TBRSIZE 0x08
276 #define QOM_QLIMIT 0x10
277 struct node_queue_bw queue_bwspec;
278 struct node_queue_opt scheduler;
287 struct node_tinithead init_nodes;
292 #define POM_TYPE 0x01
293 #define POM_STICKYADDRESS 0x02
297 struct pf_poolhashkey *key;
302 struct node_hfsc_opts hfsc_opts;
303 struct node_state_opt *keep_state_defaults = NULL;
305 int disallow_table(struct node_host *, const char *);
306 int disallow_urpf_failed(struct node_host *, const char *);
307 int disallow_alias(struct node_host *, const char *);
308 int rule_consistent(struct pf_rule *, int);
309 int filter_consistent(struct pf_rule *, int);
310 int nat_consistent(struct pf_rule *);
311 int rdr_consistent(struct pf_rule *);
312 int process_tabledef(char *, struct table_opts *);
313 void expand_label_str(char *, size_t, const char *, const char *);
314 void expand_label_if(const char *, char *, size_t, const char *);
315 void expand_label_addr(const char *, char *, size_t, u_int8_t,
317 void expand_label_port(const char *, char *, size_t,
319 void expand_label_proto(const char *, char *, size_t, u_int8_t);
320 void expand_label_nr(const char *, char *, size_t);
321 void expand_label(char *, size_t, const char *, u_int8_t,
322 struct node_host *, struct node_port *, struct node_host *,
323 struct node_port *, u_int8_t);
324 void expand_rule(struct pf_rule *, struct node_if *,
325 struct node_host *, struct node_proto *, struct node_os *,
326 struct node_host *, struct node_port *, struct node_host *,
327 struct node_port *, struct node_uid *, struct node_gid *,
328 struct node_icmp *, const char *);
329 int expand_altq(struct pf_altq *, struct node_if *,
330 struct node_queue *, struct node_queue_bw bwspec,
331 struct node_queue_opt *);
332 int expand_queue(struct pf_altq *, struct node_if *,
333 struct node_queue *, struct node_queue_bw,
334 struct node_queue_opt *);
335 int expand_skip_interface(struct node_if *);
337 int check_rulestate(int);
338 int getservice(char *);
339 int rule_label(struct pf_rule *, char *);
340 int rt_tableid_max(void);
342 void mv_rules(struct pf_ruleset *, struct pf_ruleset *);
343 void decide_address_family(struct node_host *, sa_family_t *);
344 void remove_invalid_hosts(struct node_host **, sa_family_t *);
345 int invalid_redirect(struct node_host *, sa_family_t);
346 u_int16_t parseicmpspec(char *, sa_family_t);
348 TAILQ_HEAD(loadanchorshead, loadanchors)
349 loadanchorshead = TAILQ_HEAD_INITIALIZER(loadanchorshead);
352 TAILQ_ENTRY(loadanchors) entries;
375 struct node_if *interface;
376 struct node_proto *proto;
377 struct node_icmp *icmp;
378 struct node_host *host;
380 struct node_port *port;
381 struct node_uid *uid;
382 struct node_gid *gid;
383 struct node_state_opt *state_opt;
386 struct peer src, dst;
387 struct node_os *src_os;
390 struct node_host *host;
394 struct pf_poolhashkey *key;
397 struct node_host *host;
402 struct node_state_opt *options;
413 struct pf_poolhashkey *hashkey;
414 struct node_queue *queue;
415 struct node_queue_opt queue_options;
416 struct node_queue_bw queue_bwspec;
417 struct node_qassign qassign;
418 struct filter_opts filter_opts;
419 struct antispoof_opts antispoof_opts;
420 struct queue_opts queue_opts;
421 struct scrub_opts scrub_opts;
422 struct table_opts table_opts;
423 struct pool_opts pool_opts;
424 struct node_hfsc_opts hfsc_opts;
429 #define PPORT_RANGE 1
431 int parseport(char *, struct range *r, int);
433 #define DYNIF_MULTIADDR(addr) ((addr).type == PF_ADDR_DYNIFTL && \
434 (!((addr).iflags & PFI_AFLAG_NOALIAS) || \
435 !isdigit((addr).v.ifname[strlen((addr).v.ifname)-1])))
439 %token PASS BLOCK SCRUB RETURN IN OS OUT LOG QUICK ON FROM TO FLAGS
440 %token RETURNRST RETURNICMP RETURNICMP6 PROTO INET INET6 ALL ANY ICMPTYPE
441 %token ICMP6TYPE CODE KEEP MODULATE STATE PORT RDR NAT BINAT ARROW NODF
442 %token MINTTL ERROR ALLOWOPTS FASTROUTE FILENAME ROUTETO DUPTO REPLYTO NO LABEL
443 %token NOROUTE URPFFAILED FRAGMENT USER GROUP MAXMSS MAXIMUM TTL TOS DROP TABLE
444 %token REASSEMBLE FRAGDROP FRAGCROP ANCHOR NATANCHOR RDRANCHOR BINATANCHOR
445 %token SET OPTIMIZATION TIMEOUT LIMIT LOGINTERFACE BLOCKPOLICY RANDOMID
446 %token REQUIREORDER SYNPROXY FINGERPRINTS NOSYNC DEBUG SKIP HOSTID
447 %token ANTISPOOF FOR INCLUDE
448 %token BITMASK RANDOM SOURCEHASH ROUNDROBIN STATICPORT PROBABILITY
449 %token ALTQ CBQ PRIQ HFSC BANDWIDTH TBRSIZE LINKSHARE REALTIME UPPERLIMIT
450 %token QUEUE PRIORITY QLIMIT RTABLE
451 %token LOAD RULESET_OPTIMIZATION
452 %token STICKYADDRESS MAXSRCSTATES MAXSRCNODES SOURCETRACK GLOBAL RULE
453 %token MAXSRCCONN MAXSRCCONNRATE OVERLOAD FLUSH SLOPPY
454 %token TAGGED TAG IFBOUND FLOATING STATEPOLICY STATEDEFAULTS ROUTE SETTOS
455 %token DIVERTTO DIVERTREPLY
456 %token <v.string> STRING
457 %token <v.number> NUMBER
458 %token <v.i> PORTBINARY
459 %type <v.interface> interface if_list if_item_not if_item
460 %type <v.number> number icmptype icmp6type uid gid
461 %type <v.number> tos not yesno
462 %type <v.probability> probability
463 %type <v.i> no dir af fragcache optimizer
464 %type <v.i> sourcetrack flush unaryop statelock
465 %type <v.b> action nataction natpasslog scrubaction
466 %type <v.b> flags flag blockspec
467 %type <v.range> portplain portstar portrange
468 %type <v.hashkey> hashkey
469 %type <v.proto> proto proto_list proto_item
470 %type <v.number> protoval
471 %type <v.icmp> icmpspec
472 %type <v.icmp> icmp_list icmp_item
473 %type <v.icmp> icmp6_list icmp6_item
474 %type <v.number> reticmpspec reticmp6spec
475 %type <v.fromto> fromto
476 %type <v.peer> ipportspec from to
477 %type <v.host> ipspec toipspec xhost host dynaddr host_list
478 %type <v.host> redir_host_list redirspec
479 %type <v.host> route_host route_host_list routespec
480 %type <v.os> os xos os_list
481 %type <v.port> portspec port_list port_item
482 %type <v.uid> uids uid_list uid_item
483 %type <v.gid> gids gid_list gid_item
484 %type <v.route> route
485 %type <v.redirection> redirection redirpool
486 %type <v.string> label stringall tag anchorname
487 %type <v.string> string varstring numberstring
488 %type <v.keep_state> keep
489 %type <v.state_opt> state_opt_spec state_opt_list state_opt_item
490 %type <v.logquick> logquick quick log logopts logopt
491 %type <v.interface> antispoof_ifspc antispoof_iflst antispoof_if
492 %type <v.qassign> qname
493 %type <v.queue> qassign qassign_list qassign_item
494 %type <v.queue_options> scheduler
495 %type <v.number> cbqflags_list cbqflags_item
496 %type <v.number> priqflags_list priqflags_item
497 %type <v.hfsc_opts> hfscopts_list hfscopts_item hfsc_opts
498 %type <v.queue_bwspec> bandwidth
499 %type <v.filter_opts> filter_opts filter_opt filter_opts_l
500 %type <v.antispoof_opts> antispoof_opts antispoof_opt antispoof_opts_l
501 %type <v.queue_opts> queue_opts queue_opt queue_opts_l
502 %type <v.scrub_opts> scrub_opts scrub_opt scrub_opts_l
503 %type <v.table_opts> table_opts table_opt table_opts_l
504 %type <v.pool_opts> pool_opts pool_opt pool_opts_l
505 %type <v.tagged> tagged
506 %type <v.rtableid> rtable
509 ruleset : /* empty */
510 | ruleset include '\n'
512 | ruleset option '\n'
513 | ruleset scrubrule '\n'
514 | ruleset natrule '\n'
515 | ruleset binatrule '\n'
516 | ruleset pfrule '\n'
517 | ruleset anchorrule '\n'
518 | ruleset loadrule '\n'
519 | ruleset altqif '\n'
520 | ruleset queuespec '\n'
521 | ruleset varset '\n'
522 | ruleset antispoof '\n'
523 | ruleset tabledef '\n'
524 | '{' fakeanchor '}' '\n';
525 | ruleset error '\n' { file->errors++; }
528 include : INCLUDE STRING {
531 if ((nfile = pushfile($2, 0)) == NULL) {
532 yyerror("failed to include file %s", $2);
544 * apply to previouslys specified rule: must be careful to note
545 * what that is: pf or nat or binat or rdr
547 fakeanchor : fakeanchor '\n'
548 | fakeanchor anchorrule '\n'
549 | fakeanchor binatrule '\n'
550 | fakeanchor natrule '\n'
551 | fakeanchor pfrule '\n'
552 | fakeanchor error '\n'
556 if (!strcmp($1, "none"))
558 else if (!strcmp($1, "basic"))
559 $$ = PF_OPTIMIZE_BASIC;
560 else if (!strcmp($1, "profile"))
561 $$ = PF_OPTIMIZE_BASIC | PF_OPTIMIZE_PROFILE;
563 yyerror("unknown ruleset-optimization %s", $1);
569 option : SET OPTIMIZATION STRING {
570 if (check_rulestate(PFCTL_STATE_OPTION)) {
574 if (pfctl_set_optimization(pf, $3) != 0) {
575 yyerror("unknown optimization %s", $3);
581 | SET RULESET_OPTIMIZATION optimizer {
582 if (!(pf->opts & PF_OPT_OPTIMIZE)) {
583 pf->opts |= PF_OPT_OPTIMIZE;
587 | SET TIMEOUT timeout_spec
588 | SET TIMEOUT '{' optnl timeout_list '}'
589 | SET LIMIT limit_spec
590 | SET LIMIT '{' optnl limit_list '}'
591 | SET LOGINTERFACE stringall {
592 if (check_rulestate(PFCTL_STATE_OPTION)) {
596 if (pfctl_set_logif(pf, $3) != 0) {
597 yyerror("error setting loginterface %s", $3);
603 | SET HOSTID number {
604 if ($3 == 0 || $3 > UINT_MAX) {
605 yyerror("hostid must be non-zero");
608 if (pfctl_set_hostid(pf, $3) != 0) {
609 yyerror("error setting hostid %08x", $3);
613 | SET BLOCKPOLICY DROP {
614 if (pf->opts & PF_OPT_VERBOSE)
615 printf("set block-policy drop\n");
616 if (check_rulestate(PFCTL_STATE_OPTION))
618 blockpolicy = PFRULE_DROP;
620 | SET BLOCKPOLICY RETURN {
621 if (pf->opts & PF_OPT_VERBOSE)
622 printf("set block-policy return\n");
623 if (check_rulestate(PFCTL_STATE_OPTION))
625 blockpolicy = PFRULE_RETURN;
627 | SET REQUIREORDER yesno {
628 if (pf->opts & PF_OPT_VERBOSE)
629 printf("set require-order %s\n",
630 $3 == 1 ? "yes" : "no");
633 | SET FINGERPRINTS STRING {
634 if (pf->opts & PF_OPT_VERBOSE)
635 printf("set fingerprints \"%s\"\n", $3);
636 if (check_rulestate(PFCTL_STATE_OPTION)) {
640 if (!pf->anchor->name[0]) {
641 if (pfctl_file_fingerprints(pf->dev,
643 yyerror("error loading "
644 "fingerprints %s", $3);
651 | SET STATEPOLICY statelock {
652 if (pf->opts & PF_OPT_VERBOSE)
655 printf("set state-policy floating\n");
658 printf("set state-policy if-bound\n");
661 default_statelock = $3;
664 if (check_rulestate(PFCTL_STATE_OPTION)) {
668 if (pfctl_set_debug(pf, $3) != 0) {
669 yyerror("error setting debuglevel %s", $3);
675 | SET SKIP interface {
676 if (expand_skip_interface($3) != 0) {
677 yyerror("error setting skip interface(s)");
681 | SET STATEDEFAULTS state_opt_list {
682 if (keep_state_defaults != NULL) {
683 yyerror("cannot redefine state-defaults");
686 keep_state_defaults = $3;
690 stringall : STRING { $$ = $1; }
692 if (($$ = strdup("all")) == NULL) {
693 err(1, "stringall: strdup");
698 string : STRING string {
699 if (asprintf(&$$, "%s %s", $1, $2) == -1)
700 err(1, "string: asprintf");
707 varstring : numberstring varstring {
708 if (asprintf(&$$, "%s %s", $1, $2) == -1)
709 err(1, "string: asprintf");
716 numberstring : NUMBER {
718 if (asprintf(&s, "%lld", (long long)$1) == -1) {
719 yyerror("string: asprintf");
727 varset : STRING '=' varstring {
728 if (pf->opts & PF_OPT_VERBOSE)
729 printf("%s = \"%s\"\n", $1, $3);
730 if (symset($1, $3, 0) == -1)
731 err(1, "cannot store variable %s", $1);
737 anchorname : STRING { $$ = $1; }
738 | /* empty */ { $$ = NULL; }
741 pfa_anchorlist : /* empty */
742 | pfa_anchorlist '\n'
743 | pfa_anchorlist pfrule '\n'
744 | pfa_anchorlist anchorrule '\n'
749 char ta[PF_ANCHOR_NAME_SIZE];
750 struct pf_ruleset *rs;
752 /* steping into a brace anchor */
757 /* create a holding ruleset in the root */
758 snprintf(ta, PF_ANCHOR_NAME_SIZE, "_%d", pf->bn);
759 rs = pf_find_or_create_ruleset(ta);
761 err(1, "pfa_anchor: pf_find_or_create_ruleset");
762 pf->astack[pf->asd] = rs->anchor;
763 pf->anchor = rs->anchor;
764 } '\n' pfa_anchorlist '}'
766 pf->alast = pf->anchor;
768 pf->anchor = pf->astack[pf->asd];
773 anchorrule : ANCHOR anchorname dir quick interface af proto fromto
774 filter_opts pfa_anchor
777 struct node_proto *proto;
779 if (check_rulestate(PFCTL_STATE_FILTER)) {
785 if ($2 && ($2[0] == '_' || strstr($2, "/_") != NULL)) {
787 yyerror("anchor names beginning with '_' "
788 "are reserved for internal use");
792 memset(&r, 0, sizeof(r));
793 if (pf->astack[pf->asd + 1]) {
794 /* move inline rules into relative location */
796 &pf->astack[pf->asd]->ruleset,
797 $2 ? $2 : pf->alast->name);
799 if (r.anchor == NULL)
800 err(1, "anchorrule: unable to "
803 if (pf->alast != r.anchor) {
804 if (r.anchor->match) {
805 yyerror("inline anchor '%s' "
810 mv_rules(&pf->alast->ruleset,
813 pf_remove_if_empty_ruleset(&pf->alast->ruleset);
814 pf->alast = r.anchor;
817 yyerror("anchors without explicit "
818 "rules must specify a name");
826 r.rtableid = $9.rtableid;
829 if (strlcpy(r.tagname, $9.tag,
830 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
831 yyerror("tag too long, max %u chars",
832 PF_TAG_NAME_SIZE - 1);
836 if (strlcpy(r.match_tagname, $9.match_tag,
837 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
838 yyerror("tag too long, max %u chars",
839 PF_TAG_NAME_SIZE - 1);
842 r.match_tag_not = $9.match_tag_not;
843 if (rule_label(&r, $9.label))
846 r.flags = $9.flags.b1;
847 r.flagset = $9.flags.b2;
848 if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
849 yyerror("flags always false");
852 if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
853 for (proto = $7; proto != NULL &&
854 proto->proto != IPPROTO_TCP;
857 if (proto == NULL && $7 != NULL) {
858 if ($9.flags.b1 || $9.flags.b2)
860 "flags only apply to tcp");
863 "OS fingerprinting only "
871 if ($9.keep.action) {
872 yyerror("cannot specify state handling "
878 if (strlcpy(r.match_tagname, $9.match_tag,
879 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
880 yyerror("tag too long, max %u chars",
881 PF_TAG_NAME_SIZE - 1);
884 r.match_tag_not = $9.match_tag_not;
886 decide_address_family($8.src.host, &r.af);
887 decide_address_family($8.dst.host, &r.af);
889 expand_rule(&r, $5, NULL, $7, $8.src_os,
890 $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
891 $9.uid, $9.gid, $9.icmpspec,
892 pf->astack[pf->asd + 1] ? pf->alast->name : $2);
894 pf->astack[pf->asd + 1] = NULL;
896 | NATANCHOR string interface af proto fromto rtable {
899 if (check_rulestate(PFCTL_STATE_NAT)) {
904 memset(&r, 0, sizeof(r));
909 decide_address_family($6.src.host, &r.af);
910 decide_address_family($6.dst.host, &r.af);
912 expand_rule(&r, $3, NULL, $5, $6.src_os,
913 $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
917 | RDRANCHOR string interface af proto fromto rtable {
920 if (check_rulestate(PFCTL_STATE_NAT)) {
925 memset(&r, 0, sizeof(r));
930 decide_address_family($6.src.host, &r.af);
931 decide_address_family($6.dst.host, &r.af);
933 if ($6.src.port != NULL) {
934 yyerror("source port parameter not supported"
938 if ($6.dst.port != NULL) {
939 if ($6.dst.port->next != NULL) {
940 yyerror("destination port list "
941 "expansion not supported in "
944 } else if ($6.dst.port->op != PF_OP_EQ) {
945 yyerror("destination port operators"
946 " not supported in rdr-anchor");
949 r.dst.port[0] = $6.dst.port->port[0];
950 r.dst.port[1] = $6.dst.port->port[1];
951 r.dst.port_op = $6.dst.port->op;
954 expand_rule(&r, $3, NULL, $5, $6.src_os,
955 $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
959 | BINATANCHOR string interface af proto fromto rtable {
962 if (check_rulestate(PFCTL_STATE_NAT)) {
967 memset(&r, 0, sizeof(r));
972 if ($5->next != NULL) {
973 yyerror("proto list expansion"
974 " not supported in binat-anchor");
981 if ($6.src.host != NULL || $6.src.port != NULL ||
982 $6.dst.host != NULL || $6.dst.port != NULL) {
983 yyerror("fromto parameter not supported"
988 decide_address_family($6.src.host, &r.af);
989 decide_address_family($6.dst.host, &r.af);
991 pfctl_add_rule(pf, &r, $2);
996 loadrule : LOAD ANCHOR string FROM string {
997 struct loadanchors *loadanchor;
999 if (strlen(pf->anchor->name) + 1 +
1000 strlen($3) >= MAXPATHLEN) {
1001 yyerror("anchorname %s too long, max %u\n",
1002 $3, MAXPATHLEN - 1);
1006 loadanchor = calloc(1, sizeof(struct loadanchors));
1007 if (loadanchor == NULL)
1008 err(1, "loadrule: calloc");
1009 if ((loadanchor->anchorname = malloc(MAXPATHLEN)) ==
1011 err(1, "loadrule: malloc");
1012 if (pf->anchor->name[0])
1013 snprintf(loadanchor->anchorname, MAXPATHLEN,
1014 "%s/%s", pf->anchor->name, $3);
1016 strlcpy(loadanchor->anchorname, $3, MAXPATHLEN);
1017 if ((loadanchor->filename = strdup($5)) == NULL)
1018 err(1, "loadrule: strdup");
1020 TAILQ_INSERT_TAIL(&loadanchorshead, loadanchor,
1027 scrubaction : no SCRUB {
1036 scrubrule : scrubaction dir logquick interface af proto fromto scrub_opts
1040 if (check_rulestate(PFCTL_STATE_SCRUB))
1043 memset(&r, 0, sizeof(r));
1051 yyerror("scrub rules do not support 'quick'");
1057 r.rule_flag |= PFRULE_NODF;
1059 r.rule_flag |= PFRULE_RANDOMID;
1060 if ($8.reassemble_tcp) {
1061 if (r.direction != PF_INOUT) {
1062 yyerror("reassemble tcp rules can not "
1063 "specify direction");
1066 r.rule_flag |= PFRULE_REASSEMBLE_TCP;
1069 r.min_ttl = $8.minttl;
1071 r.max_mss = $8.maxmss;
1072 if ($8.marker & SOM_SETTOS) {
1073 r.rule_flag |= PFRULE_SET_TOS;
1074 r.set_tos = $8.settos;
1077 r.rule_flag |= $8.fragcache;
1079 if (strlcpy(r.match_tagname, $8.match_tag,
1080 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1081 yyerror("tag too long, max %u chars",
1082 PF_TAG_NAME_SIZE - 1);
1085 r.match_tag_not = $8.match_tag_not;
1086 r.rtableid = $8.rtableid;
1088 expand_rule(&r, $4, NULL, $6, $7.src_os,
1089 $7.src.host, $7.src.port, $7.dst.host, $7.dst.port,
1090 NULL, NULL, NULL, "");
1095 bzero(&scrub_opts, sizeof scrub_opts);
1096 scrub_opts.rtableid = -1;
1099 { $$ = scrub_opts; }
1101 bzero(&scrub_opts, sizeof scrub_opts);
1102 scrub_opts.rtableid = -1;
1107 scrub_opts_l : scrub_opts_l scrub_opt
1112 if (scrub_opts.nodf) {
1113 yyerror("no-df cannot be respecified");
1116 scrub_opts.nodf = 1;
1119 if (scrub_opts.marker & SOM_MINTTL) {
1120 yyerror("min-ttl cannot be respecified");
1123 if ($2 < 0 || $2 > 255) {
1124 yyerror("illegal min-ttl value %d", $2);
1127 scrub_opts.marker |= SOM_MINTTL;
1128 scrub_opts.minttl = $2;
1131 if (scrub_opts.marker & SOM_MAXMSS) {
1132 yyerror("max-mss cannot be respecified");
1135 if ($2 < 0 || $2 > 65535) {
1136 yyerror("illegal max-mss value %d", $2);
1139 scrub_opts.marker |= SOM_MAXMSS;
1140 scrub_opts.maxmss = $2;
1143 if (scrub_opts.marker & SOM_SETTOS) {
1144 yyerror("set-tos cannot be respecified");
1147 scrub_opts.marker |= SOM_SETTOS;
1148 scrub_opts.settos = $2;
1151 if (scrub_opts.marker & SOM_FRAGCACHE) {
1152 yyerror("fragcache cannot be respecified");
1155 scrub_opts.marker |= SOM_FRAGCACHE;
1156 scrub_opts.fragcache = $1;
1158 | REASSEMBLE STRING {
1159 if (strcasecmp($2, "tcp") != 0) {
1160 yyerror("scrub reassemble supports only tcp, "
1166 if (scrub_opts.reassemble_tcp) {
1167 yyerror("reassemble tcp cannot be respecified");
1170 scrub_opts.reassemble_tcp = 1;
1173 if (scrub_opts.randomid) {
1174 yyerror("random-id cannot be respecified");
1177 scrub_opts.randomid = 1;
1180 if ($2 < 0 || $2 > rt_tableid_max()) {
1181 yyerror("invalid rtable id");
1184 scrub_opts.rtableid = $2;
1186 | not TAGGED string {
1187 scrub_opts.match_tag = $3;
1188 scrub_opts.match_tag_not = $1;
1192 fragcache : FRAGMENT REASSEMBLE { $$ = 0; /* default */ }
1193 | FRAGMENT FRAGCROP { $$ = PFRULE_FRAGCROP; }
1194 | FRAGMENT FRAGDROP { $$ = PFRULE_FRAGDROP; }
1197 antispoof : ANTISPOOF logquick antispoof_ifspc af antispoof_opts {
1199 struct node_host *h = NULL, *hh;
1200 struct node_if *i, *j;
1202 if (check_rulestate(PFCTL_STATE_FILTER))
1205 for (i = $3; i; i = i->next) {
1206 bzero(&r, sizeof(r));
1209 r.direction = PF_IN;
1214 if (rule_label(&r, $5.label))
1216 r.rtableid = $5.rtableid;
1217 j = calloc(1, sizeof(struct node_if));
1219 err(1, "antispoof: calloc");
1220 if (strlcpy(j->ifname, i->ifname,
1221 sizeof(j->ifname)) >= sizeof(j->ifname)) {
1223 yyerror("interface name too long");
1228 h = calloc(1, sizeof(*h));
1230 err(1, "address: calloc");
1231 h->addr.type = PF_ADDR_DYNIFTL;
1233 if (strlcpy(h->addr.v.ifname, i->ifname,
1234 sizeof(h->addr.v.ifname)) >=
1235 sizeof(h->addr.v.ifname)) {
1238 "interface name too long");
1241 hh = malloc(sizeof(*hh));
1243 err(1, "address: malloc");
1244 bcopy(h, hh, sizeof(*hh));
1245 h->addr.iflags = PFI_AFLAG_NETWORK;
1247 h = ifa_lookup(j->ifname,
1253 expand_rule(&r, j, NULL, NULL, NULL, h,
1254 NULL, NULL, NULL, NULL, NULL,
1257 if ((i->ifa_flags & IFF_LOOPBACK) == 0) {
1258 bzero(&r, sizeof(r));
1261 r.direction = PF_IN;
1266 if (rule_label(&r, $5.label))
1268 r.rtableid = $5.rtableid;
1272 h = ifa_lookup(i->ifname, 0);
1274 expand_rule(&r, NULL, NULL,
1275 NULL, NULL, h, NULL, NULL,
1276 NULL, NULL, NULL, NULL, "");
1284 antispoof_ifspc : FOR antispoof_if { $$ = $2; }
1285 | FOR '{' optnl antispoof_iflst '}' { $$ = $4; }
1288 antispoof_iflst : antispoof_if optnl { $$ = $1; }
1289 | antispoof_iflst comma antispoof_if optnl {
1290 $1->tail->next = $3;
1296 antispoof_if : if_item { $$ = $1; }
1304 bzero(&antispoof_opts, sizeof antispoof_opts);
1305 antispoof_opts.rtableid = -1;
1308 { $$ = antispoof_opts; }
1310 bzero(&antispoof_opts, sizeof antispoof_opts);
1311 antispoof_opts.rtableid = -1;
1312 $$ = antispoof_opts;
1316 antispoof_opts_l : antispoof_opts_l antispoof_opt
1320 antispoof_opt : label {
1321 if (antispoof_opts.label) {
1322 yyerror("label cannot be redefined");
1325 antispoof_opts.label = $1;
1328 if ($2 < 0 || $2 > rt_tableid_max()) {
1329 yyerror("invalid rtable id");
1332 antispoof_opts.rtableid = $2;
1336 not : '!' { $$ = 1; }
1337 | /* empty */ { $$ = 0; }
1340 tabledef : TABLE '<' STRING '>' table_opts {
1341 struct node_host *h, *nh;
1342 struct node_tinit *ti, *nti;
1344 if (strlen($3) >= PF_TABLE_NAME_SIZE) {
1345 yyerror("table name too long, max %d chars",
1346 PF_TABLE_NAME_SIZE - 1);
1350 if (pf->loadopt & PFCTL_FLAG_TABLE)
1351 if (process_tabledef($3, &$5)) {
1356 for (ti = SIMPLEQ_FIRST(&$5.init_nodes);
1357 ti != SIMPLEQ_END(&$5.init_nodes); ti = nti) {
1360 for (h = ti->host; h != NULL; h = nh) {
1364 nti = SIMPLEQ_NEXT(ti, entries);
1371 bzero(&table_opts, sizeof table_opts);
1372 SIMPLEQ_INIT(&table_opts.init_nodes);
1375 { $$ = table_opts; }
1378 bzero(&table_opts, sizeof table_opts);
1379 SIMPLEQ_INIT(&table_opts.init_nodes);
1384 table_opts_l : table_opts_l table_opt
1388 table_opt : STRING {
1389 if (!strcmp($1, "const"))
1390 table_opts.flags |= PFR_TFLAG_CONST;
1391 else if (!strcmp($1, "persist"))
1392 table_opts.flags |= PFR_TFLAG_PERSIST;
1393 else if (!strcmp($1, "counters"))
1394 table_opts.flags |= PFR_TFLAG_COUNTERS;
1396 yyerror("invalid table option '%s'", $1);
1402 | '{' optnl '}' { table_opts.init_addr = 1; }
1403 | '{' optnl host_list '}' {
1404 struct node_host *n;
1405 struct node_tinit *ti;
1407 for (n = $3; n != NULL; n = n->next) {
1408 switch (n->addr.type) {
1409 case PF_ADDR_ADDRMASK:
1412 yyerror("address ranges are not "
1413 "permitted inside tables");
1415 case PF_ADDR_DYNIFTL:
1416 yyerror("dynamic addresses are not "
1417 "permitted inside tables");
1420 yyerror("tables cannot contain tables");
1422 case PF_ADDR_NOROUTE:
1423 yyerror("\"no-route\" is not permitted "
1426 case PF_ADDR_URPFFAILED:
1427 yyerror("\"urpf-failed\" is not "
1428 "permitted inside tables");
1431 yyerror("unknown address type %d",
1436 if (!(ti = calloc(1, sizeof(*ti))))
1437 err(1, "table_opt: calloc");
1439 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1441 table_opts.init_addr = 1;
1444 struct node_tinit *ti;
1446 if (!(ti = calloc(1, sizeof(*ti))))
1447 err(1, "table_opt: calloc");
1449 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1451 table_opts.init_addr = 1;
1455 altqif : ALTQ interface queue_opts QUEUE qassign {
1458 if (check_rulestate(PFCTL_STATE_QUEUE))
1461 memset(&a, 0, sizeof(a));
1462 if ($3.scheduler.qtype == ALTQT_NONE) {
1463 yyerror("no scheduler specified!");
1466 a.scheduler = $3.scheduler.qtype;
1467 a.qlimit = $3.qlimit;
1468 a.tbrsize = $3.tbrsize;
1470 yyerror("no child queues specified");
1473 if (expand_altq(&a, $2, $5, $3.queue_bwspec,
1479 queuespec : QUEUE STRING interface queue_opts qassign {
1482 if (check_rulestate(PFCTL_STATE_QUEUE)) {
1487 memset(&a, 0, sizeof(a));
1489 if (strlcpy(a.qname, $2, sizeof(a.qname)) >=
1491 yyerror("queue name too long (max "
1492 "%d chars)", PF_QNAME_SIZE-1);
1498 yyerror("cannot specify tbrsize for queue");
1501 if ($4.priority > 255) {
1502 yyerror("priority out of range: max 255");
1505 a.priority = $4.priority;
1506 a.qlimit = $4.qlimit;
1507 a.scheduler = $4.scheduler.qtype;
1508 if (expand_queue(&a, $3, $5, $4.queue_bwspec,
1510 yyerror("errors in queue definition");
1517 bzero(&queue_opts, sizeof queue_opts);
1518 queue_opts.priority = DEFAULT_PRIORITY;
1519 queue_opts.qlimit = DEFAULT_QLIMIT;
1520 queue_opts.scheduler.qtype = ALTQT_NONE;
1521 queue_opts.queue_bwspec.bw_percent = 100;
1524 { $$ = queue_opts; }
1526 bzero(&queue_opts, sizeof queue_opts);
1527 queue_opts.priority = DEFAULT_PRIORITY;
1528 queue_opts.qlimit = DEFAULT_QLIMIT;
1529 queue_opts.scheduler.qtype = ALTQT_NONE;
1530 queue_opts.queue_bwspec.bw_percent = 100;
1535 queue_opts_l : queue_opts_l queue_opt
1539 queue_opt : BANDWIDTH bandwidth {
1540 if (queue_opts.marker & QOM_BWSPEC) {
1541 yyerror("bandwidth cannot be respecified");
1544 queue_opts.marker |= QOM_BWSPEC;
1545 queue_opts.queue_bwspec = $2;
1548 if (queue_opts.marker & QOM_PRIORITY) {
1549 yyerror("priority cannot be respecified");
1552 if ($2 < 0 || $2 > 255) {
1553 yyerror("priority out of range: max 255");
1556 queue_opts.marker |= QOM_PRIORITY;
1557 queue_opts.priority = $2;
1560 if (queue_opts.marker & QOM_QLIMIT) {
1561 yyerror("qlimit cannot be respecified");
1564 if ($2 < 0 || $2 > 65535) {
1565 yyerror("qlimit out of range: max 65535");
1568 queue_opts.marker |= QOM_QLIMIT;
1569 queue_opts.qlimit = $2;
1572 if (queue_opts.marker & QOM_SCHEDULER) {
1573 yyerror("scheduler cannot be respecified");
1576 queue_opts.marker |= QOM_SCHEDULER;
1577 queue_opts.scheduler = $1;
1580 if (queue_opts.marker & QOM_TBRSIZE) {
1581 yyerror("tbrsize cannot be respecified");
1584 if ($2 < 0 || $2 > 65535) {
1585 yyerror("tbrsize too big: max 65535");
1588 queue_opts.marker |= QOM_TBRSIZE;
1589 queue_opts.tbrsize = $2;
1593 bandwidth : STRING {
1599 bps = strtod($1, &cp);
1601 if (!strcmp(cp, "b"))
1603 else if (!strcmp(cp, "Kb"))
1605 else if (!strcmp(cp, "Mb"))
1607 else if (!strcmp(cp, "Gb"))
1608 bps *= 1000 * 1000 * 1000;
1609 else if (!strcmp(cp, "%")) {
1610 if (bps < 0 || bps > 100) {
1611 yyerror("bandwidth spec "
1616 $$.bw_percent = bps;
1619 yyerror("unknown unit %s", cp);
1625 $$.bw_absolute = (u_int32_t)bps;
1628 if ($1 < 0 || $1 > UINT_MAX) {
1629 yyerror("bandwidth number too big");
1633 $$.bw_absolute = $1;
1638 $$.qtype = ALTQT_CBQ;
1639 $$.data.cbq_opts.flags = 0;
1641 | CBQ '(' cbqflags_list ')' {
1642 $$.qtype = ALTQT_CBQ;
1643 $$.data.cbq_opts.flags = $3;
1646 $$.qtype = ALTQT_PRIQ;
1647 $$.data.priq_opts.flags = 0;
1649 | PRIQ '(' priqflags_list ')' {
1650 $$.qtype = ALTQT_PRIQ;
1651 $$.data.priq_opts.flags = $3;
1654 $$.qtype = ALTQT_HFSC;
1655 bzero(&$$.data.hfsc_opts,
1656 sizeof(struct node_hfsc_opts));
1658 | HFSC '(' hfsc_opts ')' {
1659 $$.qtype = ALTQT_HFSC;
1660 $$.data.hfsc_opts = $3;
1664 cbqflags_list : cbqflags_item { $$ |= $1; }
1665 | cbqflags_list comma cbqflags_item { $$ |= $3; }
1668 cbqflags_item : STRING {
1669 if (!strcmp($1, "default"))
1670 $$ = CBQCLF_DEFCLASS;
1671 else if (!strcmp($1, "borrow"))
1673 else if (!strcmp($1, "red"))
1675 else if (!strcmp($1, "ecn"))
1676 $$ = CBQCLF_RED|CBQCLF_ECN;
1677 else if (!strcmp($1, "rio"))
1680 yyerror("unknown cbq flag \"%s\"", $1);
1688 priqflags_list : priqflags_item { $$ |= $1; }
1689 | priqflags_list comma priqflags_item { $$ |= $3; }
1692 priqflags_item : STRING {
1693 if (!strcmp($1, "default"))
1694 $$ = PRCF_DEFAULTCLASS;
1695 else if (!strcmp($1, "red"))
1697 else if (!strcmp($1, "ecn"))
1698 $$ = PRCF_RED|PRCF_ECN;
1699 else if (!strcmp($1, "rio"))
1702 yyerror("unknown priq flag \"%s\"", $1);
1712 sizeof(struct node_hfsc_opts));
1719 hfscopts_list : hfscopts_item
1720 | hfscopts_list comma hfscopts_item
1723 hfscopts_item : LINKSHARE bandwidth {
1724 if (hfsc_opts.linkshare.used) {
1725 yyerror("linkshare already specified");
1728 hfsc_opts.linkshare.m2 = $2;
1729 hfsc_opts.linkshare.used = 1;
1731 | LINKSHARE '(' bandwidth comma NUMBER comma bandwidth ')'
1733 if ($5 < 0 || $5 > INT_MAX) {
1734 yyerror("timing in curve out of range");
1737 if (hfsc_opts.linkshare.used) {
1738 yyerror("linkshare already specified");
1741 hfsc_opts.linkshare.m1 = $3;
1742 hfsc_opts.linkshare.d = $5;
1743 hfsc_opts.linkshare.m2 = $7;
1744 hfsc_opts.linkshare.used = 1;
1746 | REALTIME bandwidth {
1747 if (hfsc_opts.realtime.used) {
1748 yyerror("realtime already specified");
1751 hfsc_opts.realtime.m2 = $2;
1752 hfsc_opts.realtime.used = 1;
1754 | REALTIME '(' bandwidth comma NUMBER comma bandwidth ')'
1756 if ($5 < 0 || $5 > INT_MAX) {
1757 yyerror("timing in curve out of range");
1760 if (hfsc_opts.realtime.used) {
1761 yyerror("realtime already specified");
1764 hfsc_opts.realtime.m1 = $3;
1765 hfsc_opts.realtime.d = $5;
1766 hfsc_opts.realtime.m2 = $7;
1767 hfsc_opts.realtime.used = 1;
1769 | UPPERLIMIT bandwidth {
1770 if (hfsc_opts.upperlimit.used) {
1771 yyerror("upperlimit already specified");
1774 hfsc_opts.upperlimit.m2 = $2;
1775 hfsc_opts.upperlimit.used = 1;
1777 | UPPERLIMIT '(' bandwidth comma NUMBER comma bandwidth ')'
1779 if ($5 < 0 || $5 > INT_MAX) {
1780 yyerror("timing in curve out of range");
1783 if (hfsc_opts.upperlimit.used) {
1784 yyerror("upperlimit already specified");
1787 hfsc_opts.upperlimit.m1 = $3;
1788 hfsc_opts.upperlimit.d = $5;
1789 hfsc_opts.upperlimit.m2 = $7;
1790 hfsc_opts.upperlimit.used = 1;
1793 if (!strcmp($1, "default"))
1794 hfsc_opts.flags |= HFCF_DEFAULTCLASS;
1795 else if (!strcmp($1, "red"))
1796 hfsc_opts.flags |= HFCF_RED;
1797 else if (!strcmp($1, "ecn"))
1798 hfsc_opts.flags |= HFCF_RED|HFCF_ECN;
1799 else if (!strcmp($1, "rio"))
1800 hfsc_opts.flags |= HFCF_RIO;
1802 yyerror("unknown hfsc flag \"%s\"", $1);
1810 qassign : /* empty */ { $$ = NULL; }
1811 | qassign_item { $$ = $1; }
1812 | '{' optnl qassign_list '}' { $$ = $3; }
1815 qassign_list : qassign_item optnl { $$ = $1; }
1816 | qassign_list comma qassign_item optnl {
1817 $1->tail->next = $3;
1823 qassign_item : STRING {
1824 $$ = calloc(1, sizeof(struct node_queue));
1826 err(1, "qassign_item: calloc");
1827 if (strlcpy($$->queue, $1, sizeof($$->queue)) >=
1828 sizeof($$->queue)) {
1829 yyerror("queue name '%s' too long (max "
1830 "%d chars)", $1, sizeof($$->queue)-1);
1841 pfrule : action dir logquick interface route af proto fromto
1845 struct node_state_opt *o;
1846 struct node_proto *proto;
1852 if (check_rulestate(PFCTL_STATE_FILTER))
1855 memset(&r, 0, sizeof(r));
1859 case PFRULE_RETURNRST:
1860 r.rule_flag |= PFRULE_RETURNRST;
1861 r.return_ttl = $1.w;
1863 case PFRULE_RETURNICMP:
1864 r.rule_flag |= PFRULE_RETURNICMP;
1865 r.return_icmp = $1.w;
1866 r.return_icmp6 = $1.w2;
1869 r.rule_flag |= PFRULE_RETURN;
1870 r.return_icmp = $1.w;
1871 r.return_icmp6 = $1.w2;
1879 r.rtableid = $9.rtableid;
1883 if (strlcpy(r.tagname, $9.tag,
1884 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1885 yyerror("tag too long, max %u chars",
1886 PF_TAG_NAME_SIZE - 1);
1890 if (strlcpy(r.match_tagname, $9.match_tag,
1891 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1892 yyerror("tag too long, max %u chars",
1893 PF_TAG_NAME_SIZE - 1);
1896 r.match_tag_not = $9.match_tag_not;
1897 if (rule_label(&r, $9.label))
1900 r.flags = $9.flags.b1;
1901 r.flagset = $9.flags.b2;
1902 if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
1903 yyerror("flags always false");
1906 if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
1907 for (proto = $7; proto != NULL &&
1908 proto->proto != IPPROTO_TCP;
1909 proto = proto->next)
1911 if (proto == NULL && $7 != NULL) {
1912 if ($9.flags.b1 || $9.flags.b2)
1914 "flags only apply to tcp");
1917 "OS fingerprinting only "
1922 if (($9.flags.b1 & parse_flags("S")) == 0 &&
1924 yyerror("OS fingerprinting requires "
1925 "the SYN TCP flag (flags S/SA)");
1932 r.keep_state = $9.keep.action;
1933 o = $9.keep.options;
1935 /* 'keep state' by default on pass rules. */
1936 if (!r.keep_state && !r.action &&
1937 !($9.marker & FOM_KEEP)) {
1938 r.keep_state = PF_STATE_NORMAL;
1939 o = keep_state_defaults;
1944 struct node_state_opt *p = o;
1947 case PF_STATE_OPT_MAX:
1949 yyerror("state option 'max' "
1950 "multiple definitions");
1953 r.max_states = o->data.max_states;
1955 case PF_STATE_OPT_NOSYNC:
1956 if (r.rule_flag & PFRULE_NOSYNC) {
1957 yyerror("state option 'sync' "
1958 "multiple definitions");
1961 r.rule_flag |= PFRULE_NOSYNC;
1963 case PF_STATE_OPT_SRCTRACK:
1965 yyerror("state option "
1967 "multiple definitions");
1970 srctrack = o->data.src_track;
1971 r.rule_flag |= PFRULE_SRCTRACK;
1973 case PF_STATE_OPT_MAX_SRC_STATES:
1974 if (r.max_src_states) {
1975 yyerror("state option "
1977 "multiple definitions");
1980 if (o->data.max_src_states == 0) {
1981 yyerror("'max-src-states' must "
1986 o->data.max_src_states;
1987 r.rule_flag |= PFRULE_SRCTRACK;
1989 case PF_STATE_OPT_OVERLOAD:
1990 if (r.overload_tblname[0]) {
1991 yyerror("multiple 'overload' "
1992 "table definitions");
1995 if (strlcpy(r.overload_tblname,
1996 o->data.overload.tblname,
1997 PF_TABLE_NAME_SIZE) >=
1998 PF_TABLE_NAME_SIZE) {
1999 yyerror("state option: "
2003 r.flush = o->data.overload.flush;
2005 case PF_STATE_OPT_MAX_SRC_CONN:
2006 if (r.max_src_conn) {
2007 yyerror("state option "
2009 "multiple definitions");
2012 if (o->data.max_src_conn == 0) {
2013 yyerror("'max-src-conn' "
2018 o->data.max_src_conn;
2019 r.rule_flag |= PFRULE_SRCTRACK |
2020 PFRULE_RULESRCTRACK;
2022 case PF_STATE_OPT_MAX_SRC_CONN_RATE:
2023 if (r.max_src_conn_rate.limit) {
2024 yyerror("state option "
2025 "'max-src-conn-rate' "
2026 "multiple definitions");
2029 if (!o->data.max_src_conn_rate.limit ||
2030 !o->data.max_src_conn_rate.seconds) {
2031 yyerror("'max-src-conn-rate' "
2032 "values must be > 0");
2035 if (o->data.max_src_conn_rate.limit >
2037 yyerror("'max-src-conn-rate' "
2038 "maximum rate must be < %u",
2042 r.max_src_conn_rate.limit =
2043 o->data.max_src_conn_rate.limit;
2044 r.max_src_conn_rate.seconds =
2045 o->data.max_src_conn_rate.seconds;
2046 r.rule_flag |= PFRULE_SRCTRACK |
2047 PFRULE_RULESRCTRACK;
2049 case PF_STATE_OPT_MAX_SRC_NODES:
2050 if (r.max_src_nodes) {
2051 yyerror("state option "
2053 "multiple definitions");
2056 if (o->data.max_src_nodes == 0) {
2057 yyerror("'max-src-nodes' must "
2062 o->data.max_src_nodes;
2063 r.rule_flag |= PFRULE_SRCTRACK |
2064 PFRULE_RULESRCTRACK;
2066 case PF_STATE_OPT_STATELOCK:
2068 yyerror("state locking option: "
2069 "multiple definitions");
2073 r.rule_flag |= o->data.statelock;
2075 case PF_STATE_OPT_SLOPPY:
2076 if (r.rule_flag & PFRULE_STATESLOPPY) {
2077 yyerror("state sloppy option: "
2078 "multiple definitions");
2081 r.rule_flag |= PFRULE_STATESLOPPY;
2083 case PF_STATE_OPT_TIMEOUT:
2084 if (o->data.timeout.number ==
2085 PFTM_ADAPTIVE_START ||
2086 o->data.timeout.number ==
2089 if (r.timeout[o->data.timeout.number]) {
2090 yyerror("state timeout %s "
2091 "multiple definitions",
2092 pf_timeouts[o->data.
2093 timeout.number].name);
2096 r.timeout[o->data.timeout.number] =
2097 o->data.timeout.seconds;
2104 /* 'flags S/SA' by default on stateful rules */
2105 if (!r.action && !r.flags && !r.flagset &&
2106 !$9.fragment && !($9.marker & FOM_FLAGS) &&
2108 r.flags = parse_flags("S");
2109 r.flagset = parse_flags("SA");
2111 if (!adaptive && r.max_states) {
2112 r.timeout[PFTM_ADAPTIVE_START] =
2113 (r.max_states / 10) * 6;
2114 r.timeout[PFTM_ADAPTIVE_END] =
2115 (r.max_states / 10) * 12;
2117 if (r.rule_flag & PFRULE_SRCTRACK) {
2118 if (srctrack == PF_SRCTRACK_GLOBAL &&
2120 yyerror("'max-src-nodes' is "
2121 "incompatible with "
2122 "'source-track global'");
2125 if (srctrack == PF_SRCTRACK_GLOBAL &&
2127 yyerror("'max-src-conn' is "
2128 "incompatible with "
2129 "'source-track global'");
2132 if (srctrack == PF_SRCTRACK_GLOBAL &&
2133 r.max_src_conn_rate.seconds) {
2134 yyerror("'max-src-conn-rate' is "
2135 "incompatible with "
2136 "'source-track global'");
2139 if (r.timeout[PFTM_SRC_NODE] <
2140 r.max_src_conn_rate.seconds)
2141 r.timeout[PFTM_SRC_NODE] =
2142 r.max_src_conn_rate.seconds;
2143 r.rule_flag |= PFRULE_SRCTRACK;
2144 if (srctrack == PF_SRCTRACK_RULE)
2145 r.rule_flag |= PFRULE_RULESRCTRACK;
2147 if (r.keep_state && !statelock)
2148 r.rule_flag |= default_statelock;
2151 r.rule_flag |= PFRULE_FRAGMENT;
2152 r.allow_opts = $9.allowopts;
2154 decide_address_family($8.src.host, &r.af);
2155 decide_address_family($8.dst.host, &r.af);
2159 yyerror("direction must be explicit "
2160 "with rules that specify routing");
2164 r.rpool.opts = $5.pool_opts;
2166 memcpy(&r.rpool.key, $5.key,
2167 sizeof(struct pf_poolhashkey));
2169 if (r.rt && r.rt != PF_FASTROUTE) {
2170 decide_address_family($5.host, &r.af);
2171 remove_invalid_hosts(&$5.host, &r.af);
2172 if ($5.host == NULL) {
2173 yyerror("no routing address with "
2174 "matching address family found.");
2177 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
2178 PF_POOL_NONE && ($5.host->next != NULL ||
2179 $5.host->addr.type == PF_ADDR_TABLE ||
2180 DYNIF_MULTIADDR($5.host->addr)))
2181 r.rpool.opts |= PF_POOL_ROUNDROBIN;
2182 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2183 PF_POOL_ROUNDROBIN &&
2184 disallow_table($5.host, "tables are only "
2185 "supported in round-robin routing pools"))
2187 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2188 PF_POOL_ROUNDROBIN &&
2189 disallow_alias($5.host, "interface (%s) "
2190 "is only supported in round-robin "
2193 if ($5.host->next != NULL) {
2194 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2195 PF_POOL_ROUNDROBIN) {
2196 yyerror("r.rpool.opts must "
2197 "be PF_POOL_ROUNDROBIN");
2202 if ($9.queues.qname != NULL) {
2203 if (strlcpy(r.qname, $9.queues.qname,
2204 sizeof(r.qname)) >= sizeof(r.qname)) {
2205 yyerror("rule qname too long (max "
2206 "%d chars)", sizeof(r.qname)-1);
2209 free($9.queues.qname);
2211 if ($9.queues.pqname != NULL) {
2212 if (strlcpy(r.pqname, $9.queues.pqname,
2213 sizeof(r.pqname)) >= sizeof(r.pqname)) {
2214 yyerror("rule pqname too long (max "
2215 "%d chars)", sizeof(r.pqname)-1);
2218 free($9.queues.pqname);
2221 r.divert.port = $9.divert.port;
2223 if ((r.divert.port = $9.divert.port)) {
2224 if (r.direction == PF_OUT) {
2225 if ($9.divert.addr) {
2226 yyerror("address specified "
2227 "for outgoing divert");
2230 bzero(&r.divert.addr,
2231 sizeof(r.divert.addr));
2233 if (!$9.divert.addr) {
2234 yyerror("no address specified "
2235 "for incoming divert");
2238 if ($9.divert.addr->af != r.af) {
2239 yyerror("address family "
2240 "mismatch for divert");
2244 $9.divert.addr->addr.v.a.addr;
2249 expand_rule(&r, $4, $5.host, $7, $8.src_os,
2250 $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
2251 $9.uid, $9.gid, $9.icmpspec, "");
2256 bzero(&filter_opts, sizeof filter_opts);
2257 filter_opts.rtableid = -1;
2260 { $$ = filter_opts; }
2262 bzero(&filter_opts, sizeof filter_opts);
2263 filter_opts.rtableid = -1;
2268 filter_opts_l : filter_opts_l filter_opt
2272 filter_opt : USER uids {
2273 if (filter_opts.uid)
2274 $2->tail->next = filter_opts.uid;
2275 filter_opts.uid = $2;
2278 if (filter_opts.gid)
2279 $2->tail->next = filter_opts.gid;
2280 filter_opts.gid = $2;
2283 if (filter_opts.marker & FOM_FLAGS) {
2284 yyerror("flags cannot be redefined");
2287 filter_opts.marker |= FOM_FLAGS;
2288 filter_opts.flags.b1 |= $1.b1;
2289 filter_opts.flags.b2 |= $1.b2;
2290 filter_opts.flags.w |= $1.w;
2291 filter_opts.flags.w2 |= $1.w2;
2294 if (filter_opts.marker & FOM_ICMP) {
2295 yyerror("icmp-type cannot be redefined");
2298 filter_opts.marker |= FOM_ICMP;
2299 filter_opts.icmpspec = $1;
2302 if (filter_opts.marker & FOM_TOS) {
2303 yyerror("tos cannot be redefined");
2306 filter_opts.marker |= FOM_TOS;
2307 filter_opts.tos = $2;
2310 if (filter_opts.marker & FOM_KEEP) {
2311 yyerror("modulate or keep cannot be redefined");
2314 filter_opts.marker |= FOM_KEEP;
2315 filter_opts.keep.action = $1.action;
2316 filter_opts.keep.options = $1.options;
2319 filter_opts.fragment = 1;
2322 filter_opts.allowopts = 1;
2325 if (filter_opts.label) {
2326 yyerror("label cannot be redefined");
2329 filter_opts.label = $1;
2332 if (filter_opts.queues.qname) {
2333 yyerror("queue cannot be redefined");
2336 filter_opts.queues = $1;
2339 filter_opts.tag = $2;
2341 | not TAGGED string {
2342 filter_opts.match_tag = $3;
2343 filter_opts.match_tag_not = $1;
2345 | PROBABILITY probability {
2348 p = floor($2 * UINT_MAX + 0.5);
2349 if (p < 0.0 || p > UINT_MAX) {
2350 yyerror("invalid probability: %lf", p);
2353 filter_opts.prob = (u_int32_t)p;
2354 if (filter_opts.prob == 0)
2355 filter_opts.prob = 1;
2358 if ($2 < 0 || $2 > rt_tableid_max()) {
2359 yyerror("invalid rtable id");
2362 filter_opts.rtableid = $2;
2364 | DIVERTTO portplain {
2366 filter_opts.divert.port = $2.a;
2367 if (!filter_opts.divert.port) {
2368 yyerror("invalid divert port: %u", ntohs($2.a));
2373 | DIVERTTO STRING PORT portplain {
2375 if ((filter_opts.divert.addr = host($2)) == NULL) {
2376 yyerror("could not parse divert address: %s",
2385 filter_opts.divert.port = $4.a;
2386 if (!filter_opts.divert.port) {
2387 yyerror("invalid divert port: %u", ntohs($4.a));
2393 yyerror("divert-reply has no meaning in FreeBSD pf(4)");
2396 filter_opts.divert.port = 1; /* some random value */
2401 probability : STRING {
2403 double p = strtod($1, &e);
2410 yyerror("invalid probability: %s", $1);
2423 action : PASS { $$.b1 = PF_PASS; $$.b2 = $$.w = 0; }
2424 | BLOCK blockspec { $$ = $2; $$.b1 = PF_DROP; }
2427 blockspec : /* empty */ {
2428 $$.b2 = blockpolicy;
2429 $$.w = returnicmpdefault;
2430 $$.w2 = returnicmp6default;
2433 $$.b2 = PFRULE_DROP;
2438 $$.b2 = PFRULE_RETURNRST;
2442 | RETURNRST '(' TTL NUMBER ')' {
2443 if ($4 < 0 || $4 > 255) {
2444 yyerror("illegal ttl value %d", $4);
2447 $$.b2 = PFRULE_RETURNRST;
2452 $$.b2 = PFRULE_RETURNICMP;
2453 $$.w = returnicmpdefault;
2454 $$.w2 = returnicmp6default;
2457 $$.b2 = PFRULE_RETURNICMP;
2458 $$.w = returnicmpdefault;
2459 $$.w2 = returnicmp6default;
2461 | RETURNICMP '(' reticmpspec ')' {
2462 $$.b2 = PFRULE_RETURNICMP;
2464 $$.w2 = returnicmpdefault;
2466 | RETURNICMP6 '(' reticmp6spec ')' {
2467 $$.b2 = PFRULE_RETURNICMP;
2468 $$.w = returnicmpdefault;
2471 | RETURNICMP '(' reticmpspec comma reticmp6spec ')' {
2472 $$.b2 = PFRULE_RETURNICMP;
2477 $$.b2 = PFRULE_RETURN;
2478 $$.w = returnicmpdefault;
2479 $$.w2 = returnicmp6default;
2483 reticmpspec : STRING {
2484 if (!($$ = parseicmpspec($1, AF_INET))) {
2493 if ($1 < 0 || $1 > 255) {
2494 yyerror("invalid icmp code %lu", $1);
2497 icmptype = returnicmpdefault >> 8;
2498 $$ = (icmptype << 8 | $1);
2502 reticmp6spec : STRING {
2503 if (!($$ = parseicmpspec($1, AF_INET6))) {
2512 if ($1 < 0 || $1 > 255) {
2513 yyerror("invalid icmp code %lu", $1);
2516 icmptype = returnicmp6default >> 8;
2517 $$ = (icmptype << 8 | $1);
2521 dir : /* empty */ { $$ = PF_INOUT; }
2522 | IN { $$ = PF_IN; }
2523 | OUT { $$ = PF_OUT; }
2526 quick : /* empty */ { $$.quick = 0; }
2527 | QUICK { $$.quick = 1; }
2530 logquick : /* empty */ { $$.log = 0; $$.quick = 0; $$.logif = 0; }
2531 | log { $$ = $1; $$.quick = 0; }
2532 | QUICK { $$.quick = 1; $$.log = 0; $$.logif = 0; }
2533 | log QUICK { $$ = $1; $$.quick = 1; }
2534 | QUICK log { $$ = $2; $$.quick = 1; }
2537 log : LOG { $$.log = PF_LOG; $$.logif = 0; }
2538 | LOG '(' logopts ')' {
2539 $$.log = PF_LOG | $3.log;
2540 $$.logif = $3.logif;
2544 logopts : logopt { $$ = $1; }
2545 | logopts comma logopt {
2546 $$.log = $1.log | $3.log;
2547 $$.logif = $3.logif;
2549 $$.logif = $1.logif;
2553 logopt : ALL { $$.log = PF_LOG_ALL; $$.logif = 0; }
2554 | USER { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
2555 | GROUP { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
2561 if (strncmp($2, "pflog", 5)) {
2562 yyerror("%s: should be a pflog interface", $2);
2566 i = strtonum($2 + 5, 0, 255, &errstr);
2568 yyerror("%s: %s", $2, errstr);
2577 interface : /* empty */ { $$ = NULL; }
2578 | ON if_item_not { $$ = $2; }
2579 | ON '{' optnl if_list '}' { $$ = $4; }
2582 if_list : if_item_not optnl { $$ = $1; }
2583 | if_list comma if_item_not optnl {
2584 $1->tail->next = $3;
2590 if_item_not : not if_item { $$ = $2; $$->not = $1; }
2594 struct node_host *n;
2596 $$ = calloc(1, sizeof(struct node_if));
2598 err(1, "if_item: calloc");
2599 if (strlcpy($$->ifname, $1, sizeof($$->ifname)) >=
2600 sizeof($$->ifname)) {
2603 yyerror("interface name too long");
2607 if ((n = ifa_exists($1)) != NULL)
2608 $$->ifa_flags = n->ifa_flags;
2617 af : /* empty */ { $$ = 0; }
2618 | INET { $$ = AF_INET; }
2619 | INET6 { $$ = AF_INET6; }
2622 proto : /* empty */ { $$ = NULL; }
2623 | PROTO proto_item { $$ = $2; }
2624 | PROTO '{' optnl proto_list '}' { $$ = $4; }
2627 proto_list : proto_item optnl { $$ = $1; }
2628 | proto_list comma proto_item optnl {
2629 $1->tail->next = $3;
2635 proto_item : protoval {
2640 yyerror("proto 0 cannot be used");
2643 $$ = calloc(1, sizeof(struct node_proto));
2645 err(1, "proto_item: calloc");
2655 p = getprotobyname($1);
2657 yyerror("unknown protocol %s", $1);
2665 if ($1 < 0 || $1 > 255) {
2666 yyerror("protocol outside range");
2686 os : /* empty */ { $$ = NULL; }
2687 | OS xos { $$ = $2; }
2688 | OS '{' optnl os_list '}' { $$ = $4; }
2692 $$ = calloc(1, sizeof(struct node_os));
2694 err(1, "os: calloc");
2700 os_list : xos optnl { $$ = $1; }
2701 | os_list comma xos optnl {
2702 $1->tail->next = $3;
2708 from : /* empty */ {
2722 if (disallow_urpf_failed($2.host, "\"urpf-failed\" is "
2723 "not permitted in a destination address"))
2729 ipportspec : ipspec {
2733 | ipspec PORT portspec {
2747 ipspec : ANY { $$ = NULL; }
2748 | xhost { $$ = $1; }
2749 | '{' optnl host_list '}' { $$ = $3; }
2752 toipspec : TO ipspec { $$ = $2; }
2753 | /* empty */ { $$ = NULL; }
2756 host_list : ipspec optnl { $$ = $1; }
2757 | host_list comma ipspec optnl {
2760 else if ($1 == NULL)
2763 $1->tail->next = $3;
2764 $1->tail = $3->tail;
2771 struct node_host *n;
2773 for (n = $2; n != NULL; n = n->next)
2778 $$ = calloc(1, sizeof(struct node_host));
2780 err(1, "xhost: calloc");
2781 $$->addr.type = PF_ADDR_NOROUTE;
2787 $$ = calloc(1, sizeof(struct node_host));
2789 err(1, "xhost: calloc");
2790 $$->addr.type = PF_ADDR_URPFFAILED;
2798 if (($$ = host($1)) == NULL) {
2799 /* error. "any" is handled elsewhere */
2801 yyerror("could not parse host specification");
2807 | STRING '-' STRING {
2808 struct node_host *b, *e;
2810 if ((b = host($1)) == NULL || (e = host($3)) == NULL) {
2813 yyerror("could not parse host specification");
2816 if (b->af != e->af ||
2817 b->addr.type != PF_ADDR_ADDRMASK ||
2818 e->addr.type != PF_ADDR_ADDRMASK ||
2819 unmask(&b->addr.v.a.mask, b->af) !=
2820 (b->af == AF_INET ? 32 : 128) ||
2821 unmask(&e->addr.v.a.mask, e->af) !=
2822 (e->af == AF_INET ? 32 : 128) ||
2823 b->next != NULL || b->not ||
2824 e->next != NULL || e->not) {
2829 yyerror("invalid address range");
2832 memcpy(&b->addr.v.a.mask, &e->addr.v.a.addr,
2833 sizeof(b->addr.v.a.mask));
2834 b->addr.type = PF_ADDR_RANGE;
2840 | STRING '/' NUMBER {
2843 if (asprintf(&buf, "%s/%lld", $1, (long long)$3) == -1)
2844 err(1, "host: asprintf");
2846 if (($$ = host(buf)) == NULL) {
2847 /* error. "any" is handled elsewhere */
2849 yyerror("could not parse host specification");
2854 | NUMBER '/' NUMBER {
2857 /* ie. for 10/8 parsing */
2859 if (asprintf(&buf, "%lld/%lld", (long long)$1, (long long)$3) == -1)
2861 if (asprintf(&buf, "%lld/%lld", $1, $3) == -1)
2863 err(1, "host: asprintf");
2864 if (($$ = host(buf)) == NULL) {
2865 /* error. "any" is handled elsewhere */
2867 yyerror("could not parse host specification");
2873 | dynaddr '/' NUMBER {
2874 struct node_host *n;
2876 if ($3 < 0 || $3 > 128) {
2877 yyerror("bit number too big");
2881 for (n = $1; n != NULL; n = n->next)
2885 if (strlen($2) >= PF_TABLE_NAME_SIZE) {
2886 yyerror("table name '%s' too long", $2);
2890 $$ = calloc(1, sizeof(struct node_host));
2892 err(1, "host: calloc");
2893 $$->addr.type = PF_ADDR_TABLE;
2894 if (strlcpy($$->addr.v.tblname, $2,
2895 sizeof($$->addr.v.tblname)) >=
2896 sizeof($$->addr.v.tblname))
2897 errx(1, "host: strlcpy");
2908 if (atoul($1, &ulval) == -1) {
2909 yyerror("%s is not a number", $1);
2918 dynaddr : '(' STRING ')' {
2923 if (!isalpha(op[0])) {
2924 yyerror("invalid interface name '%s'", op);
2928 while ((p = strrchr($2, ':')) != NULL) {
2929 if (!strcmp(p+1, "network"))
2930 flags |= PFI_AFLAG_NETWORK;
2931 else if (!strcmp(p+1, "broadcast"))
2932 flags |= PFI_AFLAG_BROADCAST;
2933 else if (!strcmp(p+1, "peer"))
2934 flags |= PFI_AFLAG_PEER;
2935 else if (!strcmp(p+1, "0"))
2936 flags |= PFI_AFLAG_NOALIAS;
2938 yyerror("interface %s has bad modifier",
2945 if (flags & (flags - 1) & PFI_AFLAG_MODEMASK) {
2947 yyerror("illegal combination of "
2948 "interface modifiers");
2951 $$ = calloc(1, sizeof(struct node_host));
2953 err(1, "address: calloc");
2955 set_ipmask($$, 128);
2956 $$->addr.type = PF_ADDR_DYNIFTL;
2957 $$->addr.iflags = flags;
2958 if (strlcpy($$->addr.v.ifname, $2,
2959 sizeof($$->addr.v.ifname)) >=
2960 sizeof($$->addr.v.ifname)) {
2963 yyerror("interface name too long");
2972 portspec : port_item { $$ = $1; }
2973 | '{' optnl port_list '}' { $$ = $3; }
2976 port_list : port_item optnl { $$ = $1; }
2977 | port_list comma port_item optnl {
2978 $1->tail->next = $3;
2984 port_item : portrange {
2985 $$ = calloc(1, sizeof(struct node_port));
2987 err(1, "port_item: calloc");
2997 | unaryop portrange {
2999 yyerror("':' cannot be used with an other "
3003 $$ = calloc(1, sizeof(struct node_port));
3005 err(1, "port_item: calloc");
3012 | portrange PORTBINARY portrange {
3014 yyerror("':' cannot be used with an other "
3018 $$ = calloc(1, sizeof(struct node_port));
3020 err(1, "port_item: calloc");
3029 portplain : numberstring {
3030 if (parseport($1, &$$, 0) == -1) {
3038 portrange : numberstring {
3039 if (parseport($1, &$$, PPORT_RANGE) == -1) {
3047 uids : uid_item { $$ = $1; }
3048 | '{' optnl uid_list '}' { $$ = $3; }
3051 uid_list : uid_item optnl { $$ = $1; }
3052 | uid_list comma uid_item optnl {
3053 $1->tail->next = $3;
3060 $$ = calloc(1, sizeof(struct node_uid));
3062 err(1, "uid_item: calloc");
3070 if ($2 == UID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
3071 yyerror("user unknown requires operator = or "
3075 $$ = calloc(1, sizeof(struct node_uid));
3077 err(1, "uid_item: calloc");
3084 | uid PORTBINARY uid {
3085 if ($1 == UID_MAX || $3 == UID_MAX) {
3086 yyerror("user unknown requires operator = or "
3090 $$ = calloc(1, sizeof(struct node_uid));
3092 err(1, "uid_item: calloc");
3102 if (!strcmp($1, "unknown"))
3107 if ((pw = getpwnam($1)) == NULL) {
3108 yyerror("unknown user %s", $1);
3117 if ($1 < 0 || $1 >= UID_MAX) {
3118 yyerror("illegal uid value %lu", $1);
3125 gids : gid_item { $$ = $1; }
3126 | '{' optnl gid_list '}' { $$ = $3; }
3129 gid_list : gid_item optnl { $$ = $1; }
3130 | gid_list comma gid_item optnl {
3131 $1->tail->next = $3;
3138 $$ = calloc(1, sizeof(struct node_gid));
3140 err(1, "gid_item: calloc");
3148 if ($2 == GID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
3149 yyerror("group unknown requires operator = or "
3153 $$ = calloc(1, sizeof(struct node_gid));
3155 err(1, "gid_item: calloc");
3162 | gid PORTBINARY gid {
3163 if ($1 == GID_MAX || $3 == GID_MAX) {
3164 yyerror("group unknown requires operator = or "
3168 $$ = calloc(1, sizeof(struct node_gid));
3170 err(1, "gid_item: calloc");
3180 if (!strcmp($1, "unknown"))
3185 if ((grp = getgrnam($1)) == NULL) {
3186 yyerror("unknown group %s", $1);
3195 if ($1 < 0 || $1 >= GID_MAX) {
3196 yyerror("illegal gid value %lu", $1);
3206 if ((f = parse_flags($1)) < 0) {
3207 yyerror("bad flags %s", $1);
3216 flags : FLAGS flag '/' flag { $$.b1 = $2.b1; $$.b2 = $4.b1; }
3217 | FLAGS '/' flag { $$.b1 = 0; $$.b2 = $3.b1; }
3218 | FLAGS ANY { $$.b1 = 0; $$.b2 = 0; }
3221 icmpspec : ICMPTYPE icmp_item { $$ = $2; }
3222 | ICMPTYPE '{' optnl icmp_list '}' { $$ = $4; }
3223 | ICMP6TYPE icmp6_item { $$ = $2; }
3224 | ICMP6TYPE '{' optnl icmp6_list '}' { $$ = $4; }
3227 icmp_list : icmp_item optnl { $$ = $1; }
3228 | icmp_list comma icmp_item optnl {
3229 $1->tail->next = $3;
3235 icmp6_list : icmp6_item optnl { $$ = $1; }
3236 | icmp6_list comma icmp6_item optnl {
3237 $1->tail->next = $3;
3243 icmp_item : icmptype {
3244 $$ = calloc(1, sizeof(struct node_icmp));
3246 err(1, "icmp_item: calloc");
3249 $$->proto = IPPROTO_ICMP;
3253 | icmptype CODE STRING {
3254 const struct icmpcodeent *p;
3256 if ((p = geticmpcodebyname($1-1, $3, AF_INET)) == NULL) {
3257 yyerror("unknown icmp-code %s", $3);
3263 $$ = calloc(1, sizeof(struct node_icmp));
3265 err(1, "icmp_item: calloc");
3267 $$->code = p->code + 1;
3268 $$->proto = IPPROTO_ICMP;
3272 | icmptype CODE NUMBER {
3273 if ($3 < 0 || $3 > 255) {
3274 yyerror("illegal icmp-code %lu", $3);
3277 $$ = calloc(1, sizeof(struct node_icmp));
3279 err(1, "icmp_item: calloc");
3282 $$->proto = IPPROTO_ICMP;
3288 icmp6_item : icmp6type {
3289 $$ = calloc(1, sizeof(struct node_icmp));
3291 err(1, "icmp_item: calloc");
3294 $$->proto = IPPROTO_ICMPV6;
3298 | icmp6type CODE STRING {
3299 const struct icmpcodeent *p;
3301 if ((p = geticmpcodebyname($1-1, $3, AF_INET6)) == NULL) {
3302 yyerror("unknown icmp6-code %s", $3);
3308 $$ = calloc(1, sizeof(struct node_icmp));
3310 err(1, "icmp_item: calloc");
3312 $$->code = p->code + 1;
3313 $$->proto = IPPROTO_ICMPV6;
3317 | icmp6type CODE NUMBER {
3318 if ($3 < 0 || $3 > 255) {
3319 yyerror("illegal icmp-code %lu", $3);
3322 $$ = calloc(1, sizeof(struct node_icmp));
3324 err(1, "icmp_item: calloc");
3327 $$->proto = IPPROTO_ICMPV6;
3334 const struct icmptypeent *p;
3336 if ((p = geticmptypebyname($1, AF_INET)) == NULL) {
3337 yyerror("unknown icmp-type %s", $1);
3345 if ($1 < 0 || $1 > 255) {
3346 yyerror("illegal icmp-type %lu", $1);
3353 icmp6type : STRING {
3354 const struct icmptypeent *p;
3356 if ((p = geticmptypebyname($1, AF_INET6)) ==
3358 yyerror("unknown icmp6-type %s", $1);
3366 if ($1 < 0 || $1 > 255) {
3367 yyerror("illegal icmp6-type %lu", $1);
3375 if (!strcmp($1, "lowdelay"))
3376 $$ = IPTOS_LOWDELAY;
3377 else if (!strcmp($1, "throughput"))
3378 $$ = IPTOS_THROUGHPUT;
3379 else if (!strcmp($1, "reliability"))
3380 $$ = IPTOS_RELIABILITY;
3381 else if ($1[0] == '0' && $1[1] == 'x')
3382 $$ = strtoul($1, NULL, 16);
3384 $$ = 0; /* flag bad argument */
3385 if (!$$ || $$ > 255) {
3386 yyerror("illegal tos value %s", $1);
3394 if (!$$ || $$ > 255) {
3395 yyerror("illegal tos value %s", $1);
3401 sourcetrack : SOURCETRACK { $$ = PF_SRCTRACK; }
3402 | SOURCETRACK GLOBAL { $$ = PF_SRCTRACK_GLOBAL; }
3403 | SOURCETRACK RULE { $$ = PF_SRCTRACK_RULE; }
3406 statelock : IFBOUND {
3407 $$ = PFRULE_IFBOUND;
3418 | KEEP STATE state_opt_spec {
3419 $$.action = PF_STATE_NORMAL;
3422 | MODULATE STATE state_opt_spec {
3423 $$.action = PF_STATE_MODULATE;
3426 | SYNPROXY STATE state_opt_spec {
3427 $$.action = PF_STATE_SYNPROXY;
3432 flush : /* empty */ { $$ = 0; }
3433 | FLUSH { $$ = PF_FLUSH; }
3435 $$ = PF_FLUSH | PF_FLUSH_GLOBAL;
3439 state_opt_spec : '(' state_opt_list ')' { $$ = $2; }
3440 | /* empty */ { $$ = NULL; }
3443 state_opt_list : state_opt_item { $$ = $1; }
3444 | state_opt_list comma state_opt_item {
3445 $1->tail->next = $3;
3451 state_opt_item : MAXIMUM NUMBER {
3452 if ($2 < 0 || $2 > UINT_MAX) {
3453 yyerror("only positive values permitted");
3456 $$ = calloc(1, sizeof(struct node_state_opt));
3458 err(1, "state_opt_item: calloc");
3459 $$->type = PF_STATE_OPT_MAX;
3460 $$->data.max_states = $2;
3465 $$ = calloc(1, sizeof(struct node_state_opt));
3467 err(1, "state_opt_item: calloc");
3468 $$->type = PF_STATE_OPT_NOSYNC;
3472 | MAXSRCSTATES NUMBER {
3473 if ($2 < 0 || $2 > UINT_MAX) {
3474 yyerror("only positive values permitted");
3477 $$ = calloc(1, sizeof(struct node_state_opt));
3479 err(1, "state_opt_item: calloc");
3480 $$->type = PF_STATE_OPT_MAX_SRC_STATES;
3481 $$->data.max_src_states = $2;
3485 | MAXSRCCONN NUMBER {
3486 if ($2 < 0 || $2 > UINT_MAX) {
3487 yyerror("only positive values permitted");
3490 $$ = calloc(1, sizeof(struct node_state_opt));
3492 err(1, "state_opt_item: calloc");
3493 $$->type = PF_STATE_OPT_MAX_SRC_CONN;
3494 $$->data.max_src_conn = $2;
3498 | MAXSRCCONNRATE NUMBER '/' NUMBER {
3499 if ($2 < 0 || $2 > UINT_MAX ||
3500 $4 < 0 || $4 > UINT_MAX) {
3501 yyerror("only positive values permitted");
3504 $$ = calloc(1, sizeof(struct node_state_opt));
3506 err(1, "state_opt_item: calloc");
3507 $$->type = PF_STATE_OPT_MAX_SRC_CONN_RATE;
3508 $$->data.max_src_conn_rate.limit = $2;
3509 $$->data.max_src_conn_rate.seconds = $4;
3513 | OVERLOAD '<' STRING '>' flush {
3514 if (strlen($3) >= PF_TABLE_NAME_SIZE) {
3515 yyerror("table name '%s' too long", $3);
3519 $$ = calloc(1, sizeof(struct node_state_opt));
3521 err(1, "state_opt_item: calloc");
3522 if (strlcpy($$->data.overload.tblname, $3,
3523 PF_TABLE_NAME_SIZE) >= PF_TABLE_NAME_SIZE)
3524 errx(1, "state_opt_item: strlcpy");
3526 $$->type = PF_STATE_OPT_OVERLOAD;
3527 $$->data.overload.flush = $5;
3531 | MAXSRCNODES NUMBER {
3532 if ($2 < 0 || $2 > UINT_MAX) {
3533 yyerror("only positive values permitted");
3536 $$ = calloc(1, sizeof(struct node_state_opt));
3538 err(1, "state_opt_item: calloc");
3539 $$->type = PF_STATE_OPT_MAX_SRC_NODES;
3540 $$->data.max_src_nodes = $2;
3545 $$ = calloc(1, sizeof(struct node_state_opt));
3547 err(1, "state_opt_item: calloc");
3548 $$->type = PF_STATE_OPT_SRCTRACK;
3549 $$->data.src_track = $1;
3554 $$ = calloc(1, sizeof(struct node_state_opt));
3556 err(1, "state_opt_item: calloc");
3557 $$->type = PF_STATE_OPT_STATELOCK;
3558 $$->data.statelock = $1;
3563 $$ = calloc(1, sizeof(struct node_state_opt));
3565 err(1, "state_opt_item: calloc");
3566 $$->type = PF_STATE_OPT_SLOPPY;
3573 if ($2 < 0 || $2 > UINT_MAX) {
3574 yyerror("only positive values permitted");
3577 for (i = 0; pf_timeouts[i].name &&
3578 strcmp(pf_timeouts[i].name, $1); ++i)
3580 if (!pf_timeouts[i].name) {
3581 yyerror("illegal timeout name %s", $1);
3585 if (strchr(pf_timeouts[i].name, '.') == NULL) {
3586 yyerror("illegal state timeout %s", $1);
3591 $$ = calloc(1, sizeof(struct node_state_opt));
3593 err(1, "state_opt_item: calloc");
3594 $$->type = PF_STATE_OPT_TIMEOUT;
3595 $$->data.timeout.number = pf_timeouts[i].timeout;
3596 $$->data.timeout.seconds = $2;
3602 label : LABEL STRING {
3607 qname : QUEUE STRING {
3611 | QUEUE '(' STRING ')' {
3615 | QUEUE '(' STRING comma STRING ')' {
3621 no : /* empty */ { $$ = 0; }
3625 portstar : numberstring {
3626 if (parseport($1, &$$, PPORT_RANGE|PPORT_STAR) == -1) {
3634 redirspec : host { $$ = $1; }
3635 | '{' optnl redir_host_list '}' { $$ = $3; }
3638 redir_host_list : host optnl { $$ = $1; }
3639 | redir_host_list comma host optnl {
3640 $1->tail->next = $3;
3641 $1->tail = $3->tail;
3646 redirpool : /* empty */ { $$ = NULL; }
3648 $$ = calloc(1, sizeof(struct redirection));
3650 err(1, "redirection: calloc");
3652 $$->rport.a = $$->rport.b = $$->rport.t = 0;
3654 | ARROW redirspec PORT portstar {
3655 $$ = calloc(1, sizeof(struct redirection));
3657 err(1, "redirection: calloc");
3663 hashkey : /* empty */
3665 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3667 err(1, "hashkey: calloc");
3668 $$->key32[0] = arc4random();
3669 $$->key32[1] = arc4random();
3670 $$->key32[2] = arc4random();
3671 $$->key32[3] = arc4random();
3675 if (!strncmp($1, "0x", 2)) {
3676 if (strlen($1) != 34) {
3678 yyerror("hex key must be 128 bits "
3679 "(32 hex digits) long");
3682 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3684 err(1, "hashkey: calloc");
3686 if (sscanf($1, "0x%8x%8x%8x%8x",
3687 &$$->key32[0], &$$->key32[1],
3688 &$$->key32[2], &$$->key32[3]) != 4) {
3691 yyerror("invalid hex key");
3697 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3699 err(1, "hashkey: calloc");
3701 MD5Update(&context, (unsigned char *)$1,
3703 MD5Final((unsigned char *)$$, &context);
3704 HTONL($$->key32[0]);
3705 HTONL($$->key32[1]);
3706 HTONL($$->key32[2]);
3707 HTONL($$->key32[3]);
3713 pool_opts : { bzero(&pool_opts, sizeof pool_opts); }
3717 bzero(&pool_opts, sizeof pool_opts);
3722 pool_opts_l : pool_opts_l pool_opt
3726 pool_opt : BITMASK {
3727 if (pool_opts.type) {
3728 yyerror("pool type cannot be redefined");
3731 pool_opts.type = PF_POOL_BITMASK;
3734 if (pool_opts.type) {
3735 yyerror("pool type cannot be redefined");
3738 pool_opts.type = PF_POOL_RANDOM;
3740 | SOURCEHASH hashkey {
3741 if (pool_opts.type) {
3742 yyerror("pool type cannot be redefined");
3745 pool_opts.type = PF_POOL_SRCHASH;
3749 if (pool_opts.type) {
3750 yyerror("pool type cannot be redefined");
3753 pool_opts.type = PF_POOL_ROUNDROBIN;
3756 if (pool_opts.staticport) {
3757 yyerror("static-port cannot be redefined");
3760 pool_opts.staticport = 1;
3763 if (filter_opts.marker & POM_STICKYADDRESS) {
3764 yyerror("sticky-address cannot be redefined");
3767 pool_opts.marker |= POM_STICKYADDRESS;
3768 pool_opts.opts |= PF_POOL_STICKYADDR;
3772 redirection : /* empty */ { $$ = NULL; }
3774 $$ = calloc(1, sizeof(struct redirection));
3776 err(1, "redirection: calloc");
3778 $$->rport.a = $$->rport.b = $$->rport.t = 0;
3780 | ARROW host PORT portstar {
3781 $$ = calloc(1, sizeof(struct redirection));
3783 err(1, "redirection: calloc");
3789 natpasslog : /* empty */ { $$.b1 = $$.b2 = 0; $$.w2 = 0; }
3790 | PASS { $$.b1 = 1; $$.b2 = 0; $$.w2 = 0; }
3791 | PASS log { $$.b1 = 1; $$.b2 = $2.log; $$.w2 = $2.logif; }
3792 | log { $$.b1 = 0; $$.b2 = $1.log; $$.w2 = $1.logif; }
3795 nataction : no NAT natpasslog {
3797 yyerror("\"pass\" not valid with \"no\"");
3808 | no RDR natpasslog {
3810 yyerror("\"pass\" not valid with \"no\"");
3823 natrule : nataction interface af proto fromto tag tagged rtable
3828 if (check_rulestate(PFCTL_STATE_NAT))
3831 memset(&r, 0, sizeof(r));
3840 if ($5.src.host && $5.src.host->af &&
3841 !$5.src.host->ifindex)
3842 r.af = $5.src.host->af;
3843 else if ($5.dst.host && $5.dst.host->af &&
3844 !$5.dst.host->ifindex)
3845 r.af = $5.dst.host->af;
3849 if (strlcpy(r.tagname, $6, PF_TAG_NAME_SIZE) >=
3851 yyerror("tag too long, max %u chars",
3852 PF_TAG_NAME_SIZE - 1);
3857 if (strlcpy(r.match_tagname, $7.name,
3858 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
3859 yyerror("tag too long, max %u chars",
3860 PF_TAG_NAME_SIZE - 1);
3863 r.match_tag_not = $7.neg;
3866 if (r.action == PF_NONAT || r.action == PF_NORDR) {
3868 yyerror("translation rule with 'no' "
3869 "does not need '->'");
3873 if ($9 == NULL || $9->host == NULL) {
3874 yyerror("translation rule requires '-> "
3878 if (!r.af && ! $9->host->ifindex)
3879 r.af = $9->host->af;
3881 remove_invalid_hosts(&$9->host, &r.af);
3882 if (invalid_redirect($9->host, r.af))
3884 if (check_netmask($9->host, r.af))
3887 r.rpool.proxy_port[0] = ntohs($9->rport.a);
3891 if (!$9->rport.b && $9->rport.t &&
3892 $5.dst.port != NULL) {
3893 r.rpool.proxy_port[1] =
3894 ntohs($9->rport.a) +
3896 $5.dst.port->port[1]) -
3898 $5.dst.port->port[0]));
3900 r.rpool.proxy_port[1] =
3904 r.rpool.proxy_port[1] =
3906 if (!r.rpool.proxy_port[0] &&
3907 !r.rpool.proxy_port[1]) {
3908 r.rpool.proxy_port[0] =
3909 PF_NAT_PROXY_PORT_LOW;
3910 r.rpool.proxy_port[1] =
3911 PF_NAT_PROXY_PORT_HIGH;
3912 } else if (!r.rpool.proxy_port[1])
3913 r.rpool.proxy_port[1] =
3914 r.rpool.proxy_port[0];
3920 r.rpool.opts = $10.type;
3921 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
3922 PF_POOL_NONE && ($9->host->next != NULL ||
3923 $9->host->addr.type == PF_ADDR_TABLE ||
3924 DYNIF_MULTIADDR($9->host->addr)))
3925 r.rpool.opts = PF_POOL_ROUNDROBIN;
3926 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
3927 PF_POOL_ROUNDROBIN &&
3928 disallow_table($9->host, "tables are only "
3929 "supported in round-robin redirection "
3932 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
3933 PF_POOL_ROUNDROBIN &&
3934 disallow_alias($9->host, "interface (%s) "
3935 "is only supported in round-robin "
3936 "redirection pools"))
3938 if ($9->host->next != NULL) {
3939 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
3940 PF_POOL_ROUNDROBIN) {
3941 yyerror("only round-robin "
3942 "valid for multiple "
3943 "redirection addresses");
3949 if ($10.key != NULL)
3950 memcpy(&r.rpool.key, $10.key,
3951 sizeof(struct pf_poolhashkey));
3954 r.rpool.opts |= $10.opts;
3956 if ($10.staticport) {
3957 if (r.action != PF_NAT) {
3958 yyerror("the 'static-port' option is "
3959 "only valid with nat rules");
3962 if (r.rpool.proxy_port[0] !=
3963 PF_NAT_PROXY_PORT_LOW &&
3964 r.rpool.proxy_port[1] !=
3965 PF_NAT_PROXY_PORT_HIGH) {
3966 yyerror("the 'static-port' option can't"
3967 " be used when specifying a port"
3971 r.rpool.proxy_port[0] = 0;
3972 r.rpool.proxy_port[1] = 0;
3975 expand_rule(&r, $2, $9 == NULL ? NULL : $9->host, $4,
3976 $5.src_os, $5.src.host, $5.src.port, $5.dst.host,
3977 $5.dst.port, 0, 0, 0, "");
3982 binatrule : no BINAT natpasslog interface af proto FROM host toipspec tag
3983 tagged rtable redirection
3985 struct pf_rule binat;
3986 struct pf_pooladdr *pa;
3988 if (check_rulestate(PFCTL_STATE_NAT))
3990 if (disallow_urpf_failed($9, "\"urpf-failed\" is not "
3991 "permitted as a binat destination"))
3994 memset(&binat, 0, sizeof(binat));
3997 yyerror("\"pass\" not valid with \"no\"");
4001 binat.action = PF_NOBINAT;
4003 binat.action = PF_BINAT;
4004 binat.natpass = $3.b1;
4006 binat.logif = $3.w2;
4008 if (!binat.af && $8 != NULL && $8->af)
4010 if (!binat.af && $9 != NULL && $9->af)
4013 if (!binat.af && $13 != NULL && $13->host)
4014 binat.af = $13->host->af;
4016 yyerror("address family (inet/inet6) "
4022 memcpy(binat.ifname, $4->ifname,
4023 sizeof(binat.ifname));
4024 binat.ifnot = $4->not;
4029 if (strlcpy(binat.tagname, $10,
4030 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4031 yyerror("tag too long, max %u chars",
4032 PF_TAG_NAME_SIZE - 1);
4036 if (strlcpy(binat.match_tagname, $11.name,
4037 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4038 yyerror("tag too long, max %u chars",
4039 PF_TAG_NAME_SIZE - 1);
4042 binat.match_tag_not = $11.neg;
4043 binat.rtableid = $12;
4046 binat.proto = $6->proto;
4050 if ($8 != NULL && disallow_table($8, "invalid use of "
4051 "table <%s> as the source address of a binat rule"))
4053 if ($8 != NULL && disallow_alias($8, "invalid use of "
4054 "interface (%s) as the source address of a binat "
4057 if ($13 != NULL && $13->host != NULL && disallow_table(
4058 $13->host, "invalid use of table <%s> as the "
4059 "redirect address of a binat rule"))
4061 if ($13 != NULL && $13->host != NULL && disallow_alias(
4062 $13->host, "invalid use of interface (%s) as the "
4063 "redirect address of a binat rule"))
4068 yyerror("multiple binat ip addresses");
4071 if ($8->addr.type == PF_ADDR_DYNIFTL)
4073 if ($8->af != binat.af) {
4074 yyerror("binat ip versions must match");
4077 if (check_netmask($8, binat.af))
4079 memcpy(&binat.src.addr, &$8->addr,
4080 sizeof(binat.src.addr));
4085 yyerror("multiple binat ip addresses");
4088 if ($9->af != binat.af && $9->af) {
4089 yyerror("binat ip versions must match");
4092 if (check_netmask($9, binat.af))
4094 memcpy(&binat.dst.addr, &$9->addr,
4095 sizeof(binat.dst.addr));
4096 binat.dst.neg = $9->not;
4100 if (binat.action == PF_NOBINAT) {
4102 yyerror("'no binat' rule does not need"
4107 if ($13 == NULL || $13->host == NULL) {
4108 yyerror("'binat' rule requires"
4113 remove_invalid_hosts(&$13->host, &binat.af);
4114 if (invalid_redirect($13->host, binat.af))
4116 if ($13->host->next != NULL) {
4117 yyerror("binat rule must redirect to "
4118 "a single address");
4121 if (check_netmask($13->host, binat.af))
4124 if (!PF_AZERO(&binat.src.addr.v.a.mask,
4126 !PF_AEQ(&binat.src.addr.v.a.mask,
4127 &$13->host->addr.v.a.mask, binat.af)) {
4128 yyerror("'binat' source mask and "
4129 "redirect mask must be the same");
4133 TAILQ_INIT(&binat.rpool.list);
4134 pa = calloc(1, sizeof(struct pf_pooladdr));
4136 err(1, "binat: calloc");
4137 pa->addr = $13->host->addr;
4139 TAILQ_INSERT_TAIL(&binat.rpool.list,
4145 pfctl_add_rule(pf, &binat, "");
4149 tag : /* empty */ { $$ = NULL; }
4150 | TAG STRING { $$ = $2; }
4153 tagged : /* empty */ { $$.neg = 0; $$.name = NULL; }
4154 | not TAGGED string { $$.neg = $1; $$.name = $3; }
4157 rtable : /* empty */ { $$ = -1; }
4159 if ($2 < 0 || $2 > rt_tableid_max()) {
4160 yyerror("invalid rtable id");
4167 route_host : STRING {
4168 $$ = calloc(1, sizeof(struct node_host));
4170 err(1, "route_host: calloc");
4172 set_ipmask($$, 128);
4176 | '(' STRING host ')' {
4182 route_host_list : route_host optnl { $$ = $1; }
4183 | route_host_list comma route_host optnl {
4186 if ($1->af != $3->af) {
4187 yyerror("all pool addresses must be in the "
4188 "same address family");
4191 $1->tail->next = $3;
4192 $1->tail = $3->tail;
4197 routespec : route_host { $$ = $1; }
4198 | '{' optnl route_host_list '}' { $$ = $3; }
4201 route : /* empty */ {
4208 $$.rt = PF_FASTROUTE;
4211 | ROUTETO routespec pool_opts {
4214 $$.pool_opts = $3.type | $3.opts;
4218 | REPLYTO routespec pool_opts {
4221 $$.pool_opts = $3.type | $3.opts;
4225 | DUPTO routespec pool_opts {
4228 $$.pool_opts = $3.type | $3.opts;
4234 timeout_spec : STRING NUMBER
4236 if (check_rulestate(PFCTL_STATE_OPTION)) {
4240 if ($2 < 0 || $2 > UINT_MAX) {
4241 yyerror("only positive values permitted");
4244 if (pfctl_set_timeout(pf, $1, $2, 0) != 0) {
4245 yyerror("unknown timeout %s", $1);
4253 timeout_list : timeout_list comma timeout_spec optnl
4254 | timeout_spec optnl
4257 limit_spec : STRING NUMBER
4259 if (check_rulestate(PFCTL_STATE_OPTION)) {
4263 if ($2 < 0 || $2 > UINT_MAX) {
4264 yyerror("only positive values permitted");
4267 if (pfctl_set_limit(pf, $1, $2) != 0) {
4268 yyerror("unable to set limit %s %u", $1, $2);
4276 limit_list : limit_list comma limit_spec optnl
4284 yesno : NO { $$ = 0; }
4286 if (!strcmp($1, "yes"))
4289 yyerror("invalid value '%s', expected 'yes' "
4298 unaryop : '=' { $$ = PF_OP_EQ; }
4299 | '!' '=' { $$ = PF_OP_NE; }
4300 | '<' '=' { $$ = PF_OP_LE; }
4301 | '<' { $$ = PF_OP_LT; }
4302 | '>' '=' { $$ = PF_OP_GE; }
4303 | '>' { $$ = PF_OP_GT; }
4309 yyerror(const char *fmt, ...)
4315 fprintf(stderr, "%s:%d: ", file->name, yylval.lineno);
4316 vfprintf(stderr, fmt, ap);
4317 fprintf(stderr, "\n");
4323 disallow_table(struct node_host *h, const char *fmt)
4325 for (; h != NULL; h = h->next)
4326 if (h->addr.type == PF_ADDR_TABLE) {
4327 yyerror(fmt, h->addr.v.tblname);
4334 disallow_urpf_failed(struct node_host *h, const char *fmt)
4336 for (; h != NULL; h = h->next)
4337 if (h->addr.type == PF_ADDR_URPFFAILED) {
4345 disallow_alias(struct node_host *h, const char *fmt)
4347 for (; h != NULL; h = h->next)
4348 if (DYNIF_MULTIADDR(h->addr)) {
4349 yyerror(fmt, h->addr.v.tblname);
4356 rule_consistent(struct pf_rule *r, int anchor_call)
4360 switch (r->action) {
4365 problems = filter_consistent(r, anchor_call);
4369 problems = nat_consistent(r);
4373 problems = rdr_consistent(r);
4384 filter_consistent(struct pf_rule *r, int anchor_call)
4388 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP &&
4389 (r->src.port_op || r->dst.port_op)) {
4390 yyerror("port only applies to tcp/udp");
4393 if (r->proto != IPPROTO_ICMP && r->proto != IPPROTO_ICMPV6 &&
4394 (r->type || r->code)) {
4395 yyerror("icmp-type/code only applies to icmp");
4398 if (!r->af && (r->type || r->code)) {
4399 yyerror("must indicate address family with icmp-type/code");
4402 if (r->overload_tblname[0] &&
4403 r->max_src_conn == 0 && r->max_src_conn_rate.seconds == 0) {
4404 yyerror("'overload' requires 'max-src-conn' "
4405 "or 'max-src-conn-rate'");
4408 if ((r->proto == IPPROTO_ICMP && r->af == AF_INET6) ||
4409 (r->proto == IPPROTO_ICMPV6 && r->af == AF_INET)) {
4410 yyerror("proto %s doesn't match address family %s",
4411 r->proto == IPPROTO_ICMP ? "icmp" : "icmp6",
4412 r->af == AF_INET ? "inet" : "inet6");
4415 if (r->allow_opts && r->action != PF_PASS) {
4416 yyerror("allow-opts can only be specified for pass rules");
4419 if (r->rule_flag & PFRULE_FRAGMENT && (r->src.port_op ||
4420 r->dst.port_op || r->flagset || r->type || r->code)) {
4421 yyerror("fragments can be filtered only on IP header fields");
4424 if (r->rule_flag & PFRULE_RETURNRST && r->proto != IPPROTO_TCP) {
4425 yyerror("return-rst can only be applied to TCP rules");
4428 if (r->max_src_nodes && !(r->rule_flag & PFRULE_RULESRCTRACK)) {
4429 yyerror("max-src-nodes requires 'source-track rule'");
4432 if (r->action == PF_DROP && r->keep_state) {
4433 yyerror("keep state on block rules doesn't make sense");
4436 if (r->rule_flag & PFRULE_STATESLOPPY &&
4437 (r->keep_state == PF_STATE_MODULATE ||
4438 r->keep_state == PF_STATE_SYNPROXY)) {
4439 yyerror("sloppy state matching cannot be used with "
4440 "synproxy state or modulate state");
4447 nat_consistent(struct pf_rule *r)
4449 return (0); /* yeah! */
4453 rdr_consistent(struct pf_rule *r)
4457 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP) {
4458 if (r->src.port_op) {
4459 yyerror("src port only applies to tcp/udp");
4462 if (r->dst.port_op) {
4463 yyerror("dst port only applies to tcp/udp");
4466 if (r->rpool.proxy_port[0]) {
4467 yyerror("rpool port only applies to tcp/udp");
4471 if (r->dst.port_op &&
4472 r->dst.port_op != PF_OP_EQ && r->dst.port_op != PF_OP_RRG) {
4473 yyerror("invalid port operator for rdr destination port");
4480 process_tabledef(char *name, struct table_opts *opts)
4482 struct pfr_buffer ab;
4483 struct node_tinit *ti;
4485 bzero(&ab, sizeof(ab));
4486 ab.pfrb_type = PFRB_ADDRS;
4487 SIMPLEQ_FOREACH(ti, &opts->init_nodes, entries) {
4489 if (pfr_buf_load(&ab, ti->file, 0, append_addr)) {
4491 yyerror("cannot load \"%s\": %s",
4492 ti->file, strerror(errno));
4494 yyerror("file \"%s\" contains bad data",
4499 if (append_addr_host(&ab, ti->host, 0, 0)) {
4500 yyerror("cannot create address buffer: %s",
4505 if (pf->opts & PF_OPT_VERBOSE)
4506 print_tabledef(name, opts->flags, opts->init_addr,
4508 if (!(pf->opts & PF_OPT_NOACTION) &&
4509 pfctl_define_table(name, opts->flags, opts->init_addr,
4510 pf->anchor->name, &ab, pf->anchor->ruleset.tticket)) {
4511 yyerror("cannot define table %s: %s", name,
4512 pfr_strerror(errno));
4528 /* macro gore, but you should've seen the prior indentation nightmare... */
4530 #define FREE_LIST(T,r) \
4533 while (node != NULL) { \
4535 node = node->next; \
4540 #define LOOP_THROUGH(T,n,r,C) \
4544 r = calloc(1, sizeof(T)); \
4546 err(1, "LOOP: calloc"); \
4550 while (n != NULL) { \
4559 expand_label_str(char *label, size_t len, const char *srch, const char *repl)
4564 if ((tmp = calloc(1, len)) == NULL)
4565 err(1, "expand_label_str: calloc");
4567 while ((q = strstr(p, srch)) != NULL) {
4569 if ((strlcat(tmp, p, len) >= len) ||
4570 (strlcat(tmp, repl, len) >= len))
4571 errx(1, "expand_label: label too long");
4575 if (strlcat(tmp, p, len) >= len)
4576 errx(1, "expand_label: label too long");
4577 strlcpy(label, tmp, len); /* always fits */
4582 expand_label_if(const char *name, char *label, size_t len, const char *ifname)
4584 if (strstr(label, name) != NULL) {
4586 expand_label_str(label, len, name, "any");
4588 expand_label_str(label, len, name, ifname);
4593 expand_label_addr(const char *name, char *label, size_t len, sa_family_t af,
4594 struct node_host *h)
4596 char tmp[64], tmp_not[66];
4598 if (strstr(label, name) != NULL) {
4599 switch (h->addr.type) {
4600 case PF_ADDR_DYNIFTL:
4601 snprintf(tmp, sizeof(tmp), "(%s)", h->addr.v.ifname);
4604 snprintf(tmp, sizeof(tmp), "<%s>", h->addr.v.tblname);
4606 case PF_ADDR_NOROUTE:
4607 snprintf(tmp, sizeof(tmp), "no-route");
4609 case PF_ADDR_URPFFAILED:
4610 snprintf(tmp, sizeof(tmp), "urpf-failed");
4612 case PF_ADDR_ADDRMASK:
4613 if (!af || (PF_AZERO(&h->addr.v.a.addr, af) &&
4614 PF_AZERO(&h->addr.v.a.mask, af)))
4615 snprintf(tmp, sizeof(tmp), "any");
4620 if (inet_ntop(af, &h->addr.v.a.addr, a,
4622 snprintf(tmp, sizeof(tmp), "?");
4624 bits = unmask(&h->addr.v.a.mask, af);
4625 if ((af == AF_INET && bits < 32) ||
4626 (af == AF_INET6 && bits < 128))
4627 snprintf(tmp, sizeof(tmp),
4630 snprintf(tmp, sizeof(tmp),
4636 snprintf(tmp, sizeof(tmp), "?");
4641 snprintf(tmp_not, sizeof(tmp_not), "! %s", tmp);
4642 expand_label_str(label, len, name, tmp_not);
4644 expand_label_str(label, len, name, tmp);
4649 expand_label_port(const char *name, char *label, size_t len,
4650 struct node_port *port)
4652 char a1[6], a2[6], op[13] = "";
4654 if (strstr(label, name) != NULL) {
4655 snprintf(a1, sizeof(a1), "%u", ntohs(port->port[0]));
4656 snprintf(a2, sizeof(a2), "%u", ntohs(port->port[1]));
4659 else if (port->op == PF_OP_IRG)
4660 snprintf(op, sizeof(op), "%s><%s", a1, a2);
4661 else if (port->op == PF_OP_XRG)
4662 snprintf(op, sizeof(op), "%s<>%s", a1, a2);
4663 else if (port->op == PF_OP_EQ)
4664 snprintf(op, sizeof(op), "%s", a1);
4665 else if (port->op == PF_OP_NE)
4666 snprintf(op, sizeof(op), "!=%s", a1);
4667 else if (port->op == PF_OP_LT)
4668 snprintf(op, sizeof(op), "<%s", a1);
4669 else if (port->op == PF_OP_LE)
4670 snprintf(op, sizeof(op), "<=%s", a1);
4671 else if (port->op == PF_OP_GT)
4672 snprintf(op, sizeof(op), ">%s", a1);
4673 else if (port->op == PF_OP_GE)
4674 snprintf(op, sizeof(op), ">=%s", a1);
4675 expand_label_str(label, len, name, op);
4680 expand_label_proto(const char *name, char *label, size_t len, u_int8_t proto)
4682 struct protoent *pe;
4685 if (strstr(label, name) != NULL) {
4686 pe = getprotobynumber(proto);
4688 expand_label_str(label, len, name, pe->p_name);
4690 snprintf(n, sizeof(n), "%u", proto);
4691 expand_label_str(label, len, name, n);
4697 expand_label_nr(const char *name, char *label, size_t len)
4701 if (strstr(label, name) != NULL) {
4702 snprintf(n, sizeof(n), "%u", pf->anchor->match);
4703 expand_label_str(label, len, name, n);
4708 expand_label(char *label, size_t len, const char *ifname, sa_family_t af,
4709 struct node_host *src_host, struct node_port *src_port,
4710 struct node_host *dst_host, struct node_port *dst_port,
4713 expand_label_if("$if", label, len, ifname);
4714 expand_label_addr("$srcaddr", label, len, af, src_host);
4715 expand_label_addr("$dstaddr", label, len, af, dst_host);
4716 expand_label_port("$srcport", label, len, src_port);
4717 expand_label_port("$dstport", label, len, dst_port);
4718 expand_label_proto("$proto", label, len, proto);
4719 expand_label_nr("$nr", label, len);
4723 expand_altq(struct pf_altq *a, struct node_if *interfaces,
4724 struct node_queue *nqueues, struct node_queue_bw bwspec,
4725 struct node_queue_opt *opts)
4727 struct pf_altq pa, pb;
4728 char qname[PF_QNAME_SIZE];
4729 struct node_queue *n;
4730 struct node_queue_bw bw;
4733 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
4734 FREE_LIST(struct node_if, interfaces);
4735 FREE_LIST(struct node_queue, nqueues);
4739 LOOP_THROUGH(struct node_if, interface, interfaces,
4740 memcpy(&pa, a, sizeof(struct pf_altq));
4741 if (strlcpy(pa.ifname, interface->ifname,
4742 sizeof(pa.ifname)) >= sizeof(pa.ifname))
4743 errx(1, "expand_altq: strlcpy");
4745 if (interface->not) {
4746 yyerror("altq on ! <interface> is not supported");
4749 if (eval_pfaltq(pf, &pa, &bwspec, opts))
4752 if (pfctl_add_altq(pf, &pa))
4755 if (pf->opts & PF_OPT_VERBOSE) {
4756 print_altq(&pf->paltq->altq, 0,
4758 if (nqueues && nqueues->tail) {
4760 LOOP_THROUGH(struct node_queue, queue,
4770 if (pa.scheduler == ALTQT_CBQ ||
4771 pa.scheduler == ALTQT_HFSC) {
4772 /* now create a root queue */
4773 memset(&pb, 0, sizeof(struct pf_altq));
4774 if (strlcpy(qname, "root_", sizeof(qname)) >=
4776 errx(1, "expand_altq: strlcpy");
4777 if (strlcat(qname, interface->ifname,
4778 sizeof(qname)) >= sizeof(qname))
4779 errx(1, "expand_altq: strlcat");
4780 if (strlcpy(pb.qname, qname,
4781 sizeof(pb.qname)) >= sizeof(pb.qname))
4782 errx(1, "expand_altq: strlcpy");
4783 if (strlcpy(pb.ifname, interface->ifname,
4784 sizeof(pb.ifname)) >= sizeof(pb.ifname))
4785 errx(1, "expand_altq: strlcpy");
4786 pb.qlimit = pa.qlimit;
4787 pb.scheduler = pa.scheduler;
4788 bw.bw_absolute = pa.ifbandwidth;
4790 if (eval_pfqueue(pf, &pb, &bw, opts))
4793 if (pfctl_add_altq(pf, &pb))
4797 LOOP_THROUGH(struct node_queue, queue, nqueues,
4798 n = calloc(1, sizeof(struct node_queue));
4800 err(1, "expand_altq: calloc");
4801 if (pa.scheduler == ALTQT_CBQ ||
4802 pa.scheduler == ALTQT_HFSC)
4803 if (strlcpy(n->parent, qname,
4804 sizeof(n->parent)) >=
4806 errx(1, "expand_altq: strlcpy");
4807 if (strlcpy(n->queue, queue->queue,
4808 sizeof(n->queue)) >= sizeof(n->queue))
4809 errx(1, "expand_altq: strlcpy");
4810 if (strlcpy(n->ifname, interface->ifname,
4811 sizeof(n->ifname)) >= sizeof(n->ifname))
4812 errx(1, "expand_altq: strlcpy");
4813 n->scheduler = pa.scheduler;
4819 queues->tail->next = n;
4825 FREE_LIST(struct node_if, interfaces);
4826 FREE_LIST(struct node_queue, nqueues);
4832 expand_queue(struct pf_altq *a, struct node_if *interfaces,
4833 struct node_queue *nqueues, struct node_queue_bw bwspec,
4834 struct node_queue_opt *opts)
4836 struct node_queue *n, *nq;
4841 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
4842 FREE_LIST(struct node_queue, nqueues);
4846 if (queues == NULL) {
4847 yyerror("queue %s has no parent", a->qname);
4848 FREE_LIST(struct node_queue, nqueues);
4852 LOOP_THROUGH(struct node_if, interface, interfaces,
4853 LOOP_THROUGH(struct node_queue, tqueue, queues,
4854 if (!strncmp(a->qname, tqueue->queue, PF_QNAME_SIZE) &&
4855 (interface->ifname[0] == 0 ||
4856 (!interface->not && !strncmp(interface->ifname,
4857 tqueue->ifname, IFNAMSIZ)) ||
4858 (interface->not && strncmp(interface->ifname,
4859 tqueue->ifname, IFNAMSIZ)))) {
4860 /* found ourself in queues */
4863 memcpy(&pa, a, sizeof(struct pf_altq));
4865 if (pa.scheduler != ALTQT_NONE &&
4866 pa.scheduler != tqueue->scheduler) {
4867 yyerror("exactly one scheduler type "
4868 "per interface allowed");
4871 pa.scheduler = tqueue->scheduler;
4873 /* scheduler dependent error checking */
4874 switch (pa.scheduler) {
4876 if (nqueues != NULL) {
4877 yyerror("priq queues cannot "
4878 "have child queues");
4881 if (bwspec.bw_absolute > 0 ||
4882 bwspec.bw_percent < 100) {
4883 yyerror("priq doesn't take "
4892 if (strlcpy(pa.ifname, tqueue->ifname,
4893 sizeof(pa.ifname)) >= sizeof(pa.ifname))
4894 errx(1, "expand_queue: strlcpy");
4895 if (strlcpy(pa.parent, tqueue->parent,
4896 sizeof(pa.parent)) >= sizeof(pa.parent))
4897 errx(1, "expand_queue: strlcpy");
4899 if (eval_pfqueue(pf, &pa, &bwspec, opts))
4902 if (pfctl_add_altq(pf, &pa))
4905 for (nq = nqueues; nq != NULL; nq = nq->next) {
4906 if (!strcmp(a->qname, nq->queue)) {
4907 yyerror("queue cannot have "
4913 sizeof(struct node_queue));
4915 err(1, "expand_queue: calloc");
4916 if (strlcpy(n->parent, a->qname,
4917 sizeof(n->parent)) >=
4919 errx(1, "expand_queue strlcpy");
4920 if (strlcpy(n->queue, nq->queue,
4921 sizeof(n->queue)) >=
4923 errx(1, "expand_queue strlcpy");
4924 if (strlcpy(n->ifname, tqueue->ifname,
4925 sizeof(n->ifname)) >=
4927 errx(1, "expand_queue strlcpy");
4928 n->scheduler = tqueue->scheduler;
4934 queues->tail->next = n;
4938 if ((pf->opts & PF_OPT_VERBOSE) && (
4939 (found == 1 && interface->ifname[0] == 0) ||
4940 (found > 0 && interface->ifname[0] != 0))) {
4941 print_queue(&pf->paltq->altq, 0,
4942 &bwspec, interface->ifname[0] != 0,
4944 if (nqueues && nqueues->tail) {
4946 LOOP_THROUGH(struct node_queue,
4959 FREE_LIST(struct node_queue, nqueues);
4960 FREE_LIST(struct node_if, interfaces);
4963 yyerror("queue %s has no parent", a->qname);
4974 expand_rule(struct pf_rule *r,
4975 struct node_if *interfaces, struct node_host *rpool_hosts,
4976 struct node_proto *protos, struct node_os *src_oses,
4977 struct node_host *src_hosts, struct node_port *src_ports,
4978 struct node_host *dst_hosts, struct node_port *dst_ports,
4979 struct node_uid *uids, struct node_gid *gids, struct node_icmp *icmp_types,
4980 const char *anchor_call)
4982 sa_family_t af = r->af;
4983 int added = 0, error = 0;
4984 char ifname[IF_NAMESIZE];
4985 char label[PF_RULE_LABEL_SIZE];
4986 char tagname[PF_TAG_NAME_SIZE];
4987 char match_tagname[PF_TAG_NAME_SIZE];
4988 struct pf_pooladdr *pa;
4989 struct node_host *h;
4990 u_int8_t flags, flagset, keep_state;
4992 if (strlcpy(label, r->label, sizeof(label)) >= sizeof(label))
4993 errx(1, "expand_rule: strlcpy");
4994 if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname))
4995 errx(1, "expand_rule: strlcpy");
4996 if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >=
4997 sizeof(match_tagname))
4998 errx(1, "expand_rule: strlcpy");
5000 flagset = r->flagset;
5001 keep_state = r->keep_state;
5003 LOOP_THROUGH(struct node_if, interface, interfaces,
5004 LOOP_THROUGH(struct node_proto, proto, protos,
5005 LOOP_THROUGH(struct node_icmp, icmp_type, icmp_types,
5006 LOOP_THROUGH(struct node_host, src_host, src_hosts,
5007 LOOP_THROUGH(struct node_port, src_port, src_ports,
5008 LOOP_THROUGH(struct node_os, src_os, src_oses,
5009 LOOP_THROUGH(struct node_host, dst_host, dst_hosts,
5010 LOOP_THROUGH(struct node_port, dst_port, dst_ports,
5011 LOOP_THROUGH(struct node_uid, uid, uids,
5012 LOOP_THROUGH(struct node_gid, gid, gids,
5015 /* for link-local IPv6 address, interface must match up */
5016 if ((r->af && src_host->af && r->af != src_host->af) ||
5017 (r->af && dst_host->af && r->af != dst_host->af) ||
5018 (src_host->af && dst_host->af &&
5019 src_host->af != dst_host->af) ||
5020 (src_host->ifindex && dst_host->ifindex &&
5021 src_host->ifindex != dst_host->ifindex) ||
5022 (src_host->ifindex && *interface->ifname &&
5023 src_host->ifindex != if_nametoindex(interface->ifname)) ||
5024 (dst_host->ifindex && *interface->ifname &&
5025 dst_host->ifindex != if_nametoindex(interface->ifname)))
5027 if (!r->af && src_host->af)
5028 r->af = src_host->af;
5029 else if (!r->af && dst_host->af)
5030 r->af = dst_host->af;
5032 if (*interface->ifname)
5033 strlcpy(r->ifname, interface->ifname,
5035 else if (if_indextoname(src_host->ifindex, ifname))
5036 strlcpy(r->ifname, ifname, sizeof(r->ifname));
5037 else if (if_indextoname(dst_host->ifindex, ifname))
5038 strlcpy(r->ifname, ifname, sizeof(r->ifname));
5040 memset(r->ifname, '\0', sizeof(r->ifname));
5042 if (strlcpy(r->label, label, sizeof(r->label)) >=
5044 errx(1, "expand_rule: strlcpy");
5045 if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >=
5047 errx(1, "expand_rule: strlcpy");
5048 if (strlcpy(r->match_tagname, match_tagname,
5049 sizeof(r->match_tagname)) >= sizeof(r->match_tagname))
5050 errx(1, "expand_rule: strlcpy");
5051 expand_label(r->label, PF_RULE_LABEL_SIZE, r->ifname, r->af,
5052 src_host, src_port, dst_host, dst_port, proto->proto);
5053 expand_label(r->tagname, PF_TAG_NAME_SIZE, r->ifname, r->af,
5054 src_host, src_port, dst_host, dst_port, proto->proto);
5055 expand_label(r->match_tagname, PF_TAG_NAME_SIZE, r->ifname,
5056 r->af, src_host, src_port, dst_host, dst_port,
5059 error += check_netmask(src_host, r->af);
5060 error += check_netmask(dst_host, r->af);
5062 r->ifnot = interface->not;
5063 r->proto = proto->proto;
5064 r->src.addr = src_host->addr;
5065 r->src.neg = src_host->not;
5066 r->src.port[0] = src_port->port[0];
5067 r->src.port[1] = src_port->port[1];
5068 r->src.port_op = src_port->op;
5069 r->dst.addr = dst_host->addr;
5070 r->dst.neg = dst_host->not;
5071 r->dst.port[0] = dst_port->port[0];
5072 r->dst.port[1] = dst_port->port[1];
5073 r->dst.port_op = dst_port->op;
5074 r->uid.op = uid->op;
5075 r->uid.uid[0] = uid->uid[0];
5076 r->uid.uid[1] = uid->uid[1];
5077 r->gid.op = gid->op;
5078 r->gid.gid[0] = gid->gid[0];
5079 r->gid.gid[1] = gid->gid[1];
5080 r->type = icmp_type->type;
5081 r->code = icmp_type->code;
5083 if ((keep_state == PF_STATE_MODULATE ||
5084 keep_state == PF_STATE_SYNPROXY) &&
5085 r->proto && r->proto != IPPROTO_TCP)
5086 r->keep_state = PF_STATE_NORMAL;
5088 r->keep_state = keep_state;
5090 if (r->proto && r->proto != IPPROTO_TCP) {
5095 r->flagset = flagset;
5097 if (icmp_type->proto && r->proto != icmp_type->proto) {
5098 yyerror("icmp-type mismatch");
5102 if (src_os && src_os->os) {
5103 r->os_fingerprint = pfctl_get_fingerprint(src_os->os);
5104 if ((pf->opts & PF_OPT_VERBOSE2) &&
5105 r->os_fingerprint == PF_OSFP_NOMATCH)
5107 "warning: unknown '%s' OS fingerprint\n",
5110 r->os_fingerprint = PF_OSFP_ANY;
5113 TAILQ_INIT(&r->rpool.list);
5114 for (h = rpool_hosts; h != NULL; h = h->next) {
5115 pa = calloc(1, sizeof(struct pf_pooladdr));
5117 err(1, "expand_rule: calloc");
5119 if (h->ifname != NULL) {
5120 if (strlcpy(pa->ifname, h->ifname,
5121 sizeof(pa->ifname)) >=
5123 errx(1, "expand_rule: strlcpy");
5126 TAILQ_INSERT_TAIL(&r->rpool.list, pa, entries);
5129 if (rule_consistent(r, anchor_call[0]) < 0 || error)
5130 yyerror("skipping rule due to errors");
5132 r->nr = pf->astack[pf->asd]->match++;
5133 pfctl_add_rule(pf, r, anchor_call);
5139 FREE_LIST(struct node_if, interfaces);
5140 FREE_LIST(struct node_proto, protos);
5141 FREE_LIST(struct node_host, src_hosts);
5142 FREE_LIST(struct node_port, src_ports);
5143 FREE_LIST(struct node_os, src_oses);
5144 FREE_LIST(struct node_host, dst_hosts);
5145 FREE_LIST(struct node_port, dst_ports);
5146 FREE_LIST(struct node_uid, uids);
5147 FREE_LIST(struct node_gid, gids);
5148 FREE_LIST(struct node_icmp, icmp_types);
5149 FREE_LIST(struct node_host, rpool_hosts);
5152 yyerror("rule expands to no valid combination");
5156 expand_skip_interface(struct node_if *interfaces)
5160 if (!interfaces || (!interfaces->next && !interfaces->not &&
5161 !strcmp(interfaces->ifname, "none"))) {
5162 if (pf->opts & PF_OPT_VERBOSE)
5163 printf("set skip on none\n");
5164 errs = pfctl_set_interface_flags(pf, "", PFI_IFLAG_SKIP, 0);
5168 if (pf->opts & PF_OPT_VERBOSE)
5169 printf("set skip on {");
5170 LOOP_THROUGH(struct node_if, interface, interfaces,
5171 if (pf->opts & PF_OPT_VERBOSE)
5172 printf(" %s", interface->ifname);
5173 if (interface->not) {
5174 yyerror("skip on ! <interface> is not supported");
5177 errs += pfctl_set_interface_flags(pf,
5178 interface->ifname, PFI_IFLAG_SKIP, 1);
5180 if (pf->opts & PF_OPT_VERBOSE)
5183 FREE_LIST(struct node_if, interfaces);
5195 check_rulestate(int desired_state)
5197 if (require_order && (rulestate > desired_state)) {
5198 yyerror("Rules must be in order: options, normalization, "
5199 "queueing, translation, filtering");
5202 rulestate = desired_state;
5207 kw_cmp(const void *k, const void *e)
5209 return (strcmp(k, ((const struct keywords *)e)->k_name));
5215 /* this has to be sorted always */
5216 static const struct keywords keywords[] = {
5218 { "allow-opts", ALLOWOPTS},
5220 { "anchor", ANCHOR},
5221 { "antispoof", ANTISPOOF},
5223 { "bandwidth", BANDWIDTH},
5225 { "binat-anchor", BINATANCHOR},
5226 { "bitmask", BITMASK},
5228 { "block-policy", BLOCKPOLICY},
5231 { "crop", FRAGCROP},
5233 { "divert-reply", DIVERTREPLY},
5234 { "divert-to", DIVERTTO},
5236 { "drop-ovl", FRAGDROP},
5238 { "fastroute", FASTROUTE},
5239 { "file", FILENAME},
5240 { "fingerprints", FINGERPRINTS},
5242 { "floating", FLOATING},
5245 { "fragment", FRAGMENT},
5247 { "global", GLOBAL},
5250 { "hostid", HOSTID},
5251 { "icmp-type", ICMPTYPE},
5252 { "icmp6-type", ICMP6TYPE},
5253 { "if-bound", IFBOUND},
5255 { "include", INCLUDE},
5261 { "linkshare", LINKSHARE},
5264 { "loginterface", LOGINTERFACE},
5266 { "max-mss", MAXMSS},
5267 { "max-src-conn", MAXSRCCONN},
5268 { "max-src-conn-rate", MAXSRCCONNRATE},
5269 { "max-src-nodes", MAXSRCNODES},
5270 { "max-src-states", MAXSRCSTATES},
5271 { "min-ttl", MINTTL},
5272 { "modulate", MODULATE},
5274 { "nat-anchor", NATANCHOR},
5277 { "no-route", NOROUTE},
5278 { "no-sync", NOSYNC},
5280 { "optimization", OPTIMIZATION},
5283 { "overload", OVERLOAD},
5286 { "priority", PRIORITY},
5288 { "probability", PROBABILITY},
5290 { "qlimit", QLIMIT},
5293 { "random", RANDOM},
5294 { "random-id", RANDOMID},
5296 { "rdr-anchor", RDRANCHOR},
5297 { "realtime", REALTIME},
5298 { "reassemble", REASSEMBLE},
5299 { "reply-to", REPLYTO},
5300 { "require-order", REQUIREORDER},
5301 { "return", RETURN},
5302 { "return-icmp", RETURNICMP},
5303 { "return-icmp6", RETURNICMP6},
5304 { "return-rst", RETURNRST},
5305 { "round-robin", ROUNDROBIN},
5307 { "route-to", ROUTETO},
5308 { "rtable", RTABLE},
5310 { "ruleset-optimization", RULESET_OPTIMIZATION},
5313 { "set-tos", SETTOS},
5315 { "sloppy", SLOPPY},
5316 { "source-hash", SOURCEHASH},
5317 { "source-track", SOURCETRACK},
5319 { "state-defaults", STATEDEFAULTS},
5320 { "state-policy", STATEPOLICY},
5321 { "static-port", STATICPORT},
5322 { "sticky-address", STICKYADDRESS},
5323 { "synproxy", SYNPROXY},
5326 { "tagged", TAGGED},
5327 { "tbrsize", TBRSIZE},
5328 { "timeout", TIMEOUT},
5332 { "upperlimit", UPPERLIMIT},
5333 { "urpf-failed", URPFFAILED},
5336 const struct keywords *p;
5338 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
5339 sizeof(keywords[0]), kw_cmp);
5343 fprintf(stderr, "%s: %d\n", s, p->k_val);
5347 fprintf(stderr, "string: %s\n", s);
5352 #define MAXPUSHBACK 128
5356 char pushback_buffer[MAXPUSHBACK];
5357 int pushback_index = 0;
5365 /* Read character from the parsebuffer instead of input. */
5366 if (parseindex >= 0) {
5367 c = parsebuf[parseindex++];
5376 return (pushback_buffer[--pushback_index]);
5379 if ((c = getc(file->stream)) == EOF) {
5380 yyerror("reached end of file while parsing quoted string");
5381 if (popfile() == EOF)
5388 while ((c = getc(file->stream)) == '\\') {
5389 next = getc(file->stream);
5394 yylval.lineno = file->lineno;
5399 if (popfile() == EOF)
5401 c = getc(file->stream);
5413 if (parseindex >= 0)
5416 if (pushback_index < MAXPUSHBACK-1)
5417 return (pushback_buffer[pushback_index++] = c);
5429 /* skip to either EOF or the first real EOL */
5432 c = pushback_buffer[--pushback_index];
5450 int quotec, next, c;
5455 while ((c = lgetc(0)) == ' ' || c == '\t')
5458 yylval.lineno = file->lineno;
5460 while ((c = lgetc(0)) != '\n' && c != EOF)
5462 if (c == '$' && parsebuf == NULL) {
5464 if ((c = lgetc(0)) == EOF)
5467 if (p + 1 >= buf + sizeof(buf) - 1) {
5468 yyerror("string too long");
5471 if (isalnum(c) || c == '_') {
5481 yyerror("macro '%s' not defined", buf);
5494 if ((c = lgetc(quotec)) == EOF)
5499 } else if (c == '\\') {
5500 if ((next = lgetc(quotec)) == EOF)
5502 if (next == quotec || c == ' ' || c == '\t')
5504 else if (next == '\n')
5508 } else if (c == quotec) {
5512 if (p + 1 >= buf + sizeof(buf) - 1) {
5513 yyerror("string too long");
5518 yylval.v.string = strdup(buf);
5519 if (yylval.v.string == NULL)
5520 err(1, "yylex: strdup");
5525 yylval.v.i = PF_OP_XRG;
5526 return (PORTBINARY);
5533 yylval.v.i = PF_OP_IRG;
5534 return (PORTBINARY);
5546 #define allowed_to_end_number(x) \
5547 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
5549 if (c == '-' || isdigit(c)) {
5552 if ((unsigned)(p-buf) >= sizeof(buf)) {
5553 yyerror("string too long");
5556 } while ((c = lgetc(0)) != EOF && isdigit(c));
5558 if (p == buf + 1 && buf[0] == '-')
5560 if (c == EOF || allowed_to_end_number(c)) {
5561 const char *errstr = NULL;
5564 yylval.v.number = strtonum(buf, LLONG_MIN,
5565 LLONG_MAX, &errstr);
5567 yyerror("\"%s\" invalid number: %s",
5582 #define allowed_in_string(x) \
5583 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
5584 x != '{' && x != '}' && x != '<' && x != '>' && \
5585 x != '!' && x != '=' && x != '/' && x != '#' && \
5588 if (isalnum(c) || c == ':' || c == '_') {
5591 if ((unsigned)(p-buf) >= sizeof(buf)) {
5592 yyerror("string too long");
5595 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
5598 if ((token = lookup(buf)) == STRING)
5599 if ((yylval.v.string = strdup(buf)) == NULL)
5600 err(1, "yylex: strdup");
5604 yylval.lineno = file->lineno;
5613 check_file_secrecy(int fd, const char *fname)
5617 if (fstat(fd, &st)) {
5618 warn("cannot stat %s", fname);
5621 if (st.st_uid != 0 && st.st_uid != getuid()) {
5622 warnx("%s: owner not root or current user", fname);
5625 if (st.st_mode & (S_IRWXG | S_IRWXO)) {
5626 warnx("%s: group/world readable/writeable", fname);
5633 pushfile(const char *name, int secret)
5637 if ((nfile = calloc(1, sizeof(struct file))) == NULL ||
5638 (nfile->name = strdup(name)) == NULL) {
5642 if (TAILQ_FIRST(&files) == NULL && strcmp(nfile->name, "-") == 0) {
5643 nfile->stream = stdin;
5645 if ((nfile->name = strdup("stdin")) == NULL) {
5650 } else if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
5651 warn("%s", nfile->name);
5655 } else if (secret &&
5656 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
5657 fclose(nfile->stream);
5663 TAILQ_INSERT_TAIL(&files, nfile, entry);
5672 if ((prev = TAILQ_PREV(file, files, entry)) != NULL) {
5673 prev->errors += file->errors;
5674 TAILQ_REMOVE(&files, file, entry);
5675 fclose(file->stream);
5685 parse_config(char *filename, struct pfctl *xpf)
5692 rulestate = PFCTL_STATE_NONE;
5693 returnicmpdefault = (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
5694 returnicmp6default =
5695 (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
5696 blockpolicy = PFRULE_DROP;
5699 if ((file = pushfile(filename, 0)) == NULL) {
5700 warn("cannot open the main config file!");
5705 errors = file->errors;
5708 /* Free macros and check which have not been used. */
5709 while ((sym = TAILQ_FIRST(&symhead))) {
5710 if ((pf->opts & PF_OPT_VERBOSE2) && !sym->used)
5711 fprintf(stderr, "warning: macro '%s' not "
5712 "used\n", sym->nam);
5715 TAILQ_REMOVE(&symhead, sym, entry);
5719 return (errors ? -1 : 0);
5723 symset(const char *nam, const char *val, int persist)
5727 for (sym = TAILQ_FIRST(&symhead); sym && strcmp(nam, sym->nam);
5728 sym = TAILQ_NEXT(sym, entry))
5732 if (sym->persist == 1)
5737 TAILQ_REMOVE(&symhead, sym, entry);
5741 if ((sym = calloc(1, sizeof(*sym))) == NULL)
5744 sym->nam = strdup(nam);
5745 if (sym->nam == NULL) {
5749 sym->val = strdup(val);
5750 if (sym->val == NULL) {
5756 sym->persist = persist;
5757 TAILQ_INSERT_TAIL(&symhead, sym, entry);
5762 pfctl_cmdline_symset(char *s)
5767 if ((val = strrchr(s, '=')) == NULL)
5770 if ((sym = malloc(strlen(s) - strlen(val) + 1)) == NULL)
5771 err(1, "pfctl_cmdline_symset: malloc");
5773 strlcpy(sym, s, strlen(s) - strlen(val) + 1);
5775 ret = symset(sym, val + 1, 1);
5782 symget(const char *nam)
5786 TAILQ_FOREACH(sym, &symhead, entry)
5787 if (strcmp(nam, sym->nam) == 0) {
5795 mv_rules(struct pf_ruleset *src, struct pf_ruleset *dst)
5800 for (i = 0; i < PF_RULESET_MAX; ++i) {
5801 while ((r = TAILQ_FIRST(src->rules[i].active.ptr))
5803 TAILQ_REMOVE(src->rules[i].active.ptr, r, entries);
5804 TAILQ_INSERT_TAIL(dst->rules[i].active.ptr, r, entries);
5805 dst->anchor->match++;
5807 src->anchor->match = 0;
5808 while ((r = TAILQ_FIRST(src->rules[i].inactive.ptr))
5810 TAILQ_REMOVE(src->rules[i].inactive.ptr, r, entries);
5811 TAILQ_INSERT_TAIL(dst->rules[i].inactive.ptr,
5818 decide_address_family(struct node_host *n, sa_family_t *af)
5820 if (*af != 0 || n == NULL)
5823 while ((n = n->next) != NULL) {
5832 remove_invalid_hosts(struct node_host **nh, sa_family_t *af)
5834 struct node_host *n = *nh, *prev = NULL;
5837 if (*af && n->af && n->af != *af) {
5838 /* unlink and free n */
5839 struct node_host *next = n->next;
5841 /* adjust tail pointer */
5842 if (n == (*nh)->tail)
5844 /* adjust previous node's next pointer */
5850 if (n->ifname != NULL)
5864 invalid_redirect(struct node_host *nh, sa_family_t af)
5867 struct node_host *n;
5869 /* tables and dyniftl are ok without an address family */
5870 for (n = nh; n != NULL; n = n->next) {
5871 if (n->addr.type != PF_ADDR_TABLE &&
5872 n->addr.type != PF_ADDR_DYNIFTL) {
5873 yyerror("address family not given and "
5874 "translation address expands to multiple "
5875 "address families");
5881 yyerror("no translation address with matching address family "
5889 atoul(char *s, u_long *ulvalp)
5895 ulval = strtoul(s, &ep, 0);
5896 if (s[0] == '\0' || *ep != '\0')
5898 if (errno == ERANGE && ulval == ULONG_MAX)
5910 if (atoul(n, &ulval) == 0) {
5911 if (ulval > 65535) {
5912 yyerror("illegal port value %lu", ulval);
5915 return (htons(ulval));
5917 s = getservbyname(n, "tcp");
5919 s = getservbyname(n, "udp");
5921 yyerror("unknown port %s", n);
5929 rule_label(struct pf_rule *r, char *s)
5932 if (strlcpy(r->label, s, sizeof(r->label)) >=
5934 yyerror("rule label too long (max %d chars)",
5935 sizeof(r->label)-1);
5943 parseicmpspec(char *w, sa_family_t af)
5945 const struct icmpcodeent *p;
5950 icmptype = returnicmpdefault >> 8;
5952 icmptype = returnicmp6default >> 8;
5954 if (atoul(w, &ulval) == -1) {
5955 if ((p = geticmpcodebyname(icmptype, w, af)) == NULL) {
5956 yyerror("unknown icmp code %s", w);
5962 yyerror("invalid icmp code %lu", ulval);
5965 return (icmptype << 8 | ulval);
5969 parseport(char *port, struct range *r, int extensions)
5971 char *p = strchr(port, ':');
5974 if ((r->a = getservice(port)) == -1)
5980 if ((extensions & PPORT_STAR) && !strcmp(p+1, "*")) {
5982 if ((r->a = getservice(port)) == -1)
5988 if ((extensions & PPORT_RANGE)) {
5990 if ((r->a = getservice(port)) == -1 ||
5991 (r->b = getservice(p)) == -1)
6004 pfctl_load_anchors(int dev, struct pfctl *pf, struct pfr_buffer *trans)
6006 struct loadanchors *la;
6008 TAILQ_FOREACH(la, &loadanchorshead, entries) {
6009 if (pf->opts & PF_OPT_VERBOSE)
6010 fprintf(stderr, "\nLoading anchor %s from %s\n",
6011 la->anchorname, la->filename);
6012 if (pfctl_rules(dev, la->filename, pf->opts, pf->optimize,
6013 la->anchorname, trans) == -1)
6021 rt_tableid_max(void)
6025 size_t l = sizeof(fibs);
6027 if (sysctlbyname("net.fibs", &fibs, &l, NULL, 0) == -1)
6028 fibs = 16; /* XXX RT_MAXFIBS, at least limit it some. */
6030 * As the OpenBSD code only compares > and not >= we need to adjust
6031 * here given we only accept values of 0..n and want to avoid #ifdefs
6036 return (RT_TABLEID_MAX);