1 /* $OpenBSD: parse.y,v 1.449 2004/03/20 23:20:20 david Exp $ */
2 /* $DragonFly: src/usr.sbin/pfctl/parse.y,v 1.3 2008/04/06 18:58:14 dillon Exp $ */
5 * Copyright (c) 2001 Markus Friedl. All rights reserved.
6 * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
7 * Copyright (c) 2001 Theo de Raadt. All rights reserved.
8 * Copyright (c) 2002,2003 Henning Brauer. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/types.h>
32 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <netinet/in_systm.h>
36 #include <netinet/ip.h>
37 #include <netinet/ip_icmp.h>
38 #include <netinet/icmp6.h>
39 #include <net/pf/pfvar.h>
40 #include <arpa/inet.h>
41 #include <net/altq/altq.h>
42 #include <net/altq/altq_cbq.h>
43 #include <net/altq/altq_priq.h>
44 #include <net/altq/altq_hfsc.h>
45 #include <net/altq/altq_fairq.h>
60 #include "pfctl_parser.h"
63 static struct pfctl *pf = NULL;
64 static FILE *fin = NULL;
66 static int lineno = 1;
67 static int errors = 0;
68 static int rulestate = 0;
69 static u_int16_t returnicmpdefault =
70 (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
71 static u_int16_t returnicmp6default =
72 (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
73 static int blockpolicy = PFRULE_DROP;
74 static int require_order = 1;
75 static int default_statelock;
88 struct node_proto *next;
89 struct node_proto *tail;
95 struct node_port *next;
96 struct node_port *tail;
102 struct node_uid *next;
103 struct node_uid *tail;
109 struct node_gid *next;
110 struct node_gid *tail;
117 struct node_icmp *next;
118 struct node_icmp *tail;
121 enum { PF_STATE_OPT_MAX, PF_STATE_OPT_NOSYNC, PF_STATE_OPT_SRCTRACK,
122 PF_STATE_OPT_MAX_SRC_STATES, PF_STATE_OPT_MAX_SRC_NODES,
123 PF_STATE_OPT_STATELOCK, PF_STATE_OPT_TIMEOUT };
125 enum { PF_SRCTRACK_NONE, PF_SRCTRACK, PF_SRCTRACK_GLOBAL, PF_SRCTRACK_RULE };
127 struct node_state_opt {
130 u_int32_t max_states;
131 u_int32_t max_src_states;
132 u_int32_t max_src_nodes;
140 struct node_state_opt *next;
141 struct node_state_opt *tail;
145 struct node_host *host;
146 struct node_port *port;
150 char queue[PF_QNAME_SIZE];
151 char parent[PF_QNAME_SIZE];
152 char ifname[IFNAMSIZ];
154 struct node_queue *next;
155 struct node_queue *tail;
158 struct node_qassign {
165 #define FOM_FLAGS 0x01
166 #define FOM_ICMP 0x02
168 #define FOM_KEEP 0x08
169 #define FOM_SRCTRACK 0x10
170 struct node_uid *uid;
171 struct node_gid *gid;
178 struct node_icmp *icmpspec;
182 struct node_state_opt *options;
187 struct node_qassign queues;
190 u_int8_t match_tag_not;
193 struct antispoof_opts {
199 #define SOM_MINTTL 0x01
200 #define SOM_MAXMSS 0x02
201 #define SOM_FRAGCACHE 0x04
212 #define QOM_BWSPEC 0x01
213 #define QOM_SCHEDULER 0x02
214 #define QOM_PRIORITY 0x04
215 #define QOM_TBRSIZE 0x08
216 #define QOM_QLIMIT 0x10
217 struct node_queue_bw queue_bwspec;
218 struct node_queue_opt scheduler;
227 struct node_tinithead init_nodes;
232 #define POM_TYPE 0x01
233 #define POM_STICKYADDRESS 0x02
237 struct pf_poolhashkey *key;
242 struct node_hfsc_opts hfsc_opts;
243 struct node_fairq_opts fairq_opts;
245 int yyerror(const char *, ...);
246 int disallow_table(struct node_host *, const char *);
247 int disallow_alias(struct node_host *, const char *);
248 int rule_consistent(struct pf_rule *);
249 int filter_consistent(struct pf_rule *);
250 int nat_consistent(struct pf_rule *);
251 int rdr_consistent(struct pf_rule *);
252 int process_tabledef(char *, struct table_opts *);
254 void expand_label_str(char *, size_t, const char *, const char *);
255 void expand_label_if(const char *, char *, size_t, const char *);
256 void expand_label_addr(const char *, char *, size_t, u_int8_t,
258 void expand_label_port(const char *, char *, size_t, struct node_port *);
259 void expand_label_proto(const char *, char *, size_t, u_int8_t);
260 void expand_label_nr(const char *, char *, size_t);
261 void expand_label(char *, size_t, const char *, u_int8_t, struct node_host *,
262 struct node_port *, struct node_host *, struct node_port *,
264 void expand_rule(struct pf_rule *, struct node_if *, struct node_host *,
265 struct node_proto *, struct node_os*, struct node_host *,
266 struct node_port *, struct node_host *, struct node_port *,
267 struct node_uid *, struct node_gid *, struct node_icmp *);
268 int expand_altq(struct pf_altq *, struct node_if *, struct node_queue *,
269 struct node_queue_bw bwspec, struct node_queue_opt *);
270 int expand_queue(struct pf_altq *, struct node_if *, struct node_queue *,
271 struct node_queue_bw, struct node_queue_opt *);
273 int check_rulestate(int);
274 int kw_cmp(const void *, const void *);
280 int atoul(char *, u_long *);
281 int getservice(char *);
282 int rule_label(struct pf_rule *, char *);
284 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
286 TAILQ_ENTRY(sym) entries;
294 int symset(const char *, const char *, int);
295 char *symget(const char *);
297 void decide_address_family(struct node_host *, sa_family_t *);
298 void remove_invalid_hosts(struct node_host **, sa_family_t *);
299 int invalid_redirect(struct node_host *, sa_family_t);
300 u_int16_t parseicmpspec(char *, sa_family_t);
302 TAILQ_HEAD(loadanchorshead, loadanchors)
303 loadanchorshead = TAILQ_HEAD_INITIALIZER(loadanchorshead);
306 TAILQ_ENTRY(loadanchors) entries;
328 struct node_if *interface;
329 struct node_proto *proto;
330 struct node_icmp *icmp;
331 struct node_host *host;
333 struct node_port *port;
334 struct node_uid *uid;
335 struct node_gid *gid;
336 struct node_state_opt *state_opt;
339 struct peer src, dst;
340 struct node_os *src_os;
343 struct node_host *host;
347 struct pf_poolhashkey *key;
350 struct node_host *host;
355 struct node_state_opt *options;
361 struct pf_poolhashkey *hashkey;
362 struct node_queue *queue;
363 struct node_queue_opt queue_options;
364 struct node_queue_bw queue_bwspec;
365 struct node_qassign qassign;
366 struct filter_opts filter_opts;
367 struct antispoof_opts antispoof_opts;
368 struct queue_opts queue_opts;
369 struct scrub_opts scrub_opts;
370 struct table_opts table_opts;
371 struct pool_opts pool_opts;
372 struct node_hfsc_opts hfsc_opts;
373 struct node_fairq_opts fairq_opts;
378 #define PREPARE_ANCHOR_RULE(r, a) \
380 memset(&(r), 0, sizeof(r)); \
381 if (strlcpy(r.anchorname, (a), \
382 sizeof(r.anchorname)) >= \
383 sizeof(r.anchorname)) { \
384 yyerror("anchor name '%s' too long", \
390 #define DYNIF_MULTIADDR(addr) ((addr).type == PF_ADDR_DYNIFTL && \
391 (!((addr).iflags & PFI_AFLAG_NOALIAS) || \
392 !isdigit((addr).v.ifname[strlen((addr).v.ifname)-1])))
398 %token PASS BLOCK SCRUB RETURN IN OS OUT LOG LOGALL QUICK ON FROM TO FLAGS
399 %token RETURNRST RETURNICMP RETURNICMP6 PROTO INET INET6 ALL ANY ICMPTYPE
400 %token ICMP6TYPE CODE KEEP MODULATE STATE PORT RDR NAT BINAT ARROW NODF
401 %token MINTTL ERROR ALLOWOPTS FASTROUTE FILENAME ROUTETO DUPTO REPLYTO NO LABEL
402 %token NOROUTE FRAGMENT USER GROUP MAXMSS MAXIMUM TTL TOS DROP TABLE
403 %token REASSEMBLE FRAGDROP FRAGCROP ANCHOR NATANCHOR RDRANCHOR BINATANCHOR
404 %token SET OPTIMIZATION TIMEOUT LIMIT LOGINTERFACE BLOCKPOLICY RANDOMID
405 %token REQUIREORDER SYNPROXY FINGERPRINTS NOSYNC DEBUG HOSTID
407 %token BITMASK RANDOM SOURCEHASH ROUNDROBIN STATICPORT
408 %token ALTQ CBQ PRIQ HFSC FAIRQ BANDWIDTH TBRSIZE LINKSHARE REALTIME UPPERLIMIT
409 %token QUEUE PRIORITY QLIMIT HOGS BUCKETS
411 %token STICKYADDRESS MAXSRCSTATES MAXSRCNODES SOURCETRACK GLOBAL RULE
412 %token TAGGED TAG IFBOUND GRBOUND FLOATING STATEPOLICY
413 %token <v.string> STRING
414 %token <v.i> PORTBINARY
415 %type <v.interface> interface if_list if_item_not if_item
416 %type <v.number> number icmptype icmp6type uid gid
417 %type <v.number> tos not yesno natpass
418 %type <v.i> no dir log af fragcache sourcetrack
419 %type <v.i> unaryop statelock
420 %type <v.b> action nataction flags flag blockspec
421 %type <v.range> port rport
422 %type <v.hashkey> hashkey
423 %type <v.proto> proto proto_list proto_item
424 %type <v.icmp> icmpspec
425 %type <v.icmp> icmp_list icmp_item
426 %type <v.icmp> icmp6_list icmp6_item
427 %type <v.fromto> fromto
428 %type <v.peer> ipportspec from to
429 %type <v.host> ipspec xhost host dynaddr host_list
430 %type <v.host> redir_host_list redirspec
431 %type <v.host> route_host route_host_list routespec
432 %type <v.os> os xos os_list
433 %type <v.port> portspec port_list port_item
434 %type <v.uid> uids uid_list uid_item
435 %type <v.gid> gids gid_list gid_item
436 %type <v.route> route
437 %type <v.redirection> redirection redirpool
438 %type <v.string> label string tag
439 %type <v.keep_state> keep
440 %type <v.state_opt> state_opt_spec state_opt_list state_opt_item
441 %type <v.logquick> logquick
442 %type <v.interface> antispoof_ifspc antispoof_iflst
443 %type <v.qassign> qname
444 %type <v.queue> qassign qassign_list qassign_item
445 %type <v.queue_options> scheduler
446 %type <v.number> cbqflags_list cbqflags_item
447 %type <v.number> priqflags_list priqflags_item
448 %type <v.hfsc_opts> hfscopts_list hfscopts_item hfsc_opts
449 %type <v.fairq_opts> fairqopts_list fairqopts_item fairq_opts
450 %type <v.queue_bwspec> bandwidth
451 %type <v.filter_opts> filter_opts filter_opt filter_opts_l
452 %type <v.antispoof_opts> antispoof_opts antispoof_opt antispoof_opts_l
453 %type <v.queue_opts> queue_opts queue_opt queue_opts_l
454 %type <v.scrub_opts> scrub_opts scrub_opt scrub_opts_l
455 %type <v.table_opts> table_opts table_opt table_opts_l
456 %type <v.pool_opts> pool_opts pool_opt pool_opts_l
459 ruleset : /* empty */
461 | ruleset option '\n'
462 | ruleset scrubrule '\n'
463 | ruleset natrule '\n'
464 | ruleset binatrule '\n'
465 | ruleset pfrule '\n'
466 | ruleset anchorrule '\n'
467 | ruleset loadrule '\n'
468 | ruleset altqif '\n'
469 | ruleset queuespec '\n'
470 | ruleset varset '\n'
471 | ruleset antispoof '\n'
472 | ruleset tabledef '\n'
473 | ruleset error '\n' { errors++; }
476 option : SET OPTIMIZATION STRING {
477 if (check_rulestate(PFCTL_STATE_OPTION)) {
481 if (pfctl_set_optimization(pf, $3) != 0) {
482 yyerror("unknown optimization %s", $3);
488 | SET TIMEOUT timeout_spec
489 | SET TIMEOUT '{' timeout_list '}'
490 | SET LIMIT limit_spec
491 | SET LIMIT '{' limit_list '}'
492 | SET LOGINTERFACE STRING {
493 if (check_rulestate(PFCTL_STATE_OPTION)) {
497 if ((ifa_exists($3, 0) == NULL) && strcmp($3, "none")) {
498 yyerror("interface %s doesn't exist", $3);
502 if (pfctl_set_logif(pf, $3) != 0) {
503 yyerror("error setting loginterface %s", $3);
509 | SET HOSTID number {
511 yyerror("hostid must be non-zero");
514 if (pfctl_set_hostid(pf, $3) != 0) {
515 yyerror("error setting loginterface %08x", $3);
519 | SET BLOCKPOLICY DROP {
520 if (pf->opts & PF_OPT_VERBOSE)
521 printf("set block-policy drop\n");
522 if (check_rulestate(PFCTL_STATE_OPTION))
524 blockpolicy = PFRULE_DROP;
526 | SET BLOCKPOLICY RETURN {
527 if (pf->opts & PF_OPT_VERBOSE)
528 printf("set block-policy return\n");
529 if (check_rulestate(PFCTL_STATE_OPTION))
531 blockpolicy = PFRULE_RETURN;
533 | SET REQUIREORDER yesno {
534 if (pf->opts & PF_OPT_VERBOSE)
535 printf("set require-order %s\n",
536 $3 == 1 ? "yes" : "no");
539 | SET FINGERPRINTS STRING {
540 if (pf->opts & PF_OPT_VERBOSE)
541 printf("fingerprints %s\n", $3);
542 if (check_rulestate(PFCTL_STATE_OPTION)) {
546 if (pfctl_file_fingerprints(pf->dev, pf->opts, $3)) {
547 yyerror("error loading fingerprints %s", $3);
553 | SET STATEPOLICY statelock {
554 if (pf->opts & PF_OPT_VERBOSE)
557 printf("set state-policy floating\n");
560 printf("set state-policy if-bound\n");
563 printf("set state-policy "
567 default_statelock = $3;
570 if (check_rulestate(PFCTL_STATE_OPTION)) {
574 if (pfctl_set_debug(pf, $3) != 0) {
575 yyerror("error setting debuglevel %s", $3);
583 string : string STRING {
584 if (asprintf(&$$, "%s %s", $1, $2) == -1)
585 err(1, "string: asprintf");
592 varset : STRING '=' string {
593 if (pf->opts & PF_OPT_VERBOSE)
594 printf("%s = \"%s\"\n", $1, $3);
595 if (symset($1, $3, 0) == -1)
596 err(1, "cannot store variable %s", $1);
602 anchorrule : ANCHOR string dir interface af proto fromto filter_opts {
605 if (check_rulestate(PFCTL_STATE_FILTER)) {
610 PREPARE_ANCHOR_RULE(r, $2);
615 if (strlcpy(r.match_tagname, $8.match_tag,
616 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
617 yyerror("tag too long, max %u chars",
618 PF_TAG_NAME_SIZE - 1);
621 r.match_tag_not = $8.match_tag_not;
623 decide_address_family($7.src.host, &r.af);
624 decide_address_family($7.dst.host, &r.af);
626 expand_rule(&r, $4, NULL, $6, $7.src_os,
627 $7.src.host, $7.src.port, $7.dst.host, $7.dst.port,
630 | NATANCHOR string interface af proto fromto {
633 if (check_rulestate(PFCTL_STATE_NAT)) {
638 PREPARE_ANCHOR_RULE(r, $2);
643 decide_address_family($6.src.host, &r.af);
644 decide_address_family($6.dst.host, &r.af);
646 expand_rule(&r, $3, NULL, $5, $6.src_os,
647 $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
650 | RDRANCHOR string interface af proto fromto {
653 if (check_rulestate(PFCTL_STATE_NAT)) {
658 PREPARE_ANCHOR_RULE(r, $2);
663 decide_address_family($6.src.host, &r.af);
664 decide_address_family($6.dst.host, &r.af);
666 if ($6.src.port != NULL) {
667 yyerror("source port parameter not supported"
671 if ($6.dst.port != NULL) {
672 if ($6.dst.port->next != NULL) {
673 yyerror("destination port list "
674 "expansion not supported in "
677 } else if ($6.dst.port->op != PF_OP_EQ) {
678 yyerror("destination port operators"
679 " not supported in rdr-anchor");
682 r.dst.port[0] = $6.dst.port->port[0];
683 r.dst.port[1] = $6.dst.port->port[1];
684 r.dst.port_op = $6.dst.port->op;
687 expand_rule(&r, $3, NULL, $5, $6.src_os,
688 $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
691 | BINATANCHOR string interface af proto fromto {
694 if (check_rulestate(PFCTL_STATE_NAT)) {
699 PREPARE_ANCHOR_RULE(r, $2);
704 if ($5->next != NULL) {
705 yyerror("proto list expansion"
706 " not supported in binat-anchor");
713 if ($6.src.host != NULL || $6.src.port != NULL ||
714 $6.dst.host != NULL || $6.dst.port != NULL) {
715 yyerror("fromto parameter not supported"
720 decide_address_family($6.src.host, &r.af);
721 decide_address_family($6.dst.host, &r.af);
723 pfctl_add_rule(pf, &r);
727 loadrule : LOAD ANCHOR string FROM string {
729 struct loadanchors *loadanchor;
731 t = strsep(&$3, ":");
732 if (*t == '\0' || $3 == NULL || *$3 == '\0') {
733 yyerror("anchor '%s' invalid\n", $3);
737 if (strlen(t) >= PF_ANCHOR_NAME_SIZE) {
738 yyerror("anchorname %s too long, max %u\n",
739 t, PF_ANCHOR_NAME_SIZE - 1);
743 if (strlen($3) >= PF_RULESET_NAME_SIZE) {
744 yyerror("rulesetname %s too long, max %u\n",
745 $3, PF_RULESET_NAME_SIZE - 1);
750 loadanchor = calloc(1, sizeof(struct loadanchors));
751 if (loadanchor == NULL)
752 err(1, "loadrule: calloc");
753 if ((loadanchor->anchorname = strdup(t)) == NULL)
754 err(1, "loadrule: strdup");
755 if ((loadanchor->rulesetname = strdup($3)) == NULL)
756 err(1, "loadrule: strdup");
757 if ((loadanchor->filename = strdup($5)) == NULL)
758 err(1, "loadrule: strdup");
760 TAILQ_INSERT_TAIL(&loadanchorshead, loadanchor,
763 free(t); /* not $3 */
767 scrubrule : SCRUB dir logquick interface af proto fromto scrub_opts
771 if (check_rulestate(PFCTL_STATE_SCRUB))
774 memset(&r, 0, sizeof(r));
781 yyerror("scrub rules do not support 'quick'");
787 r.rule_flag |= PFRULE_NODF;
789 r.rule_flag |= PFRULE_RANDOMID;
790 if ($8.reassemble_tcp) {
791 if (r.direction != PF_INOUT) {
792 yyerror("reassemble tcp rules can not "
793 "specify direction");
796 r.rule_flag |= PFRULE_REASSEMBLE_TCP;
799 r.min_ttl = $8.minttl;
801 r.max_mss = $8.maxmss;
803 r.rule_flag |= $8.fragcache;
805 expand_rule(&r, $4, NULL, $6, $7.src_os,
806 $7.src.host, $7.src.port, $7.dst.host, $7.dst.port,
812 bzero(&scrub_opts, sizeof scrub_opts);
817 bzero(&scrub_opts, sizeof scrub_opts);
822 scrub_opts_l : scrub_opts_l scrub_opt
827 if (scrub_opts.nodf) {
828 yyerror("no-df cannot be respecified");
834 if (scrub_opts.marker & SOM_MINTTL) {
835 yyerror("min-ttl cannot be respecified");
839 yyerror("illegal min-ttl value %d", $2);
842 scrub_opts.marker |= SOM_MINTTL;
843 scrub_opts.minttl = $2;
846 if (scrub_opts.marker & SOM_MAXMSS) {
847 yyerror("max-mss cannot be respecified");
851 yyerror("illegal max-mss value %d", $2);
854 scrub_opts.marker |= SOM_MAXMSS;
855 scrub_opts.maxmss = $2;
858 if (scrub_opts.marker & SOM_FRAGCACHE) {
859 yyerror("fragcache cannot be respecified");
862 scrub_opts.marker |= SOM_FRAGCACHE;
863 scrub_opts.fragcache = $1;
865 | REASSEMBLE STRING {
866 if (strcasecmp($2, "tcp") != 0) {
871 if (scrub_opts.reassemble_tcp) {
872 yyerror("reassemble tcp cannot be respecified");
875 scrub_opts.reassemble_tcp = 1;
878 if (scrub_opts.randomid) {
879 yyerror("random-id cannot be respecified");
882 scrub_opts.randomid = 1;
886 fragcache : FRAGMENT REASSEMBLE { $$ = 0; /* default */ }
887 | FRAGMENT FRAGCROP { $$ = PFRULE_FRAGCROP; }
888 | FRAGMENT FRAGDROP { $$ = PFRULE_FRAGDROP; }
891 antispoof : ANTISPOOF logquick antispoof_ifspc af antispoof_opts {
893 struct node_host *h = NULL;
894 struct node_if *i, *j;
896 if (check_rulestate(PFCTL_STATE_FILTER))
899 for (i = $3; i; i = i->next) {
900 bzero(&r, sizeof(r));
907 if (rule_label(&r, $5.label))
909 j = calloc(1, sizeof(struct node_if));
911 err(1, "antispoof: calloc");
912 if (strlcpy(j->ifname, i->ifname,
913 sizeof(j->ifname)) >= sizeof(j->ifname)) {
915 yyerror("interface name too long");
919 h = ifa_lookup(j->ifname, PFI_AFLAG_NETWORK);
922 expand_rule(&r, j, NULL, NULL, NULL, h,
923 NULL, NULL, NULL, NULL, NULL, NULL);
925 if ((i->ifa_flags & IFF_LOOPBACK) == 0) {
926 bzero(&r, sizeof(r));
933 if (rule_label(&r, $5.label))
935 h = ifa_lookup(i->ifname, 0);
937 expand_rule(&r, NULL, NULL,
938 NULL, NULL, h, NULL, NULL,
939 NULL, NULL, NULL, NULL);
946 antispoof_ifspc : FOR if_item { $$ = $2; }
947 | FOR '{' antispoof_iflst '}' { $$ = $3; }
950 antispoof_iflst : if_item { $$ = $1; }
951 | antispoof_iflst comma if_item {
958 antispoof_opts : { bzero(&antispoof_opts, sizeof antispoof_opts); }
960 { $$ = antispoof_opts; }
962 bzero(&antispoof_opts, sizeof antispoof_opts);
967 antispoof_opts_l : antispoof_opts_l antispoof_opt
971 antispoof_opt : label {
972 if (antispoof_opts.label) {
973 yyerror("label cannot be redefined");
976 antispoof_opts.label = $1;
980 not : '!' { $$ = 1; }
981 | /* empty */ { $$ = 0; }
984 tabledef : TABLE '<' STRING '>' table_opts {
985 struct node_host *h, *nh;
986 struct node_tinit *ti, *nti;
988 if (strlen($3) >= PF_TABLE_NAME_SIZE) {
989 yyerror("table name too long, max %d chars",
990 PF_TABLE_NAME_SIZE - 1);
994 if (pf->loadopt & PFCTL_FLAG_TABLE)
995 if (process_tabledef($3, &$5)) {
1000 for (ti = SIMPLEQ_FIRST(&$5.init_nodes);
1001 ti != SIMPLEQ_END(&$5.init_nodes); ti = nti) {
1004 for (h = ti->host; h != NULL; h = nh) {
1008 nti = SIMPLEQ_NEXT(ti, entries);
1015 bzero(&table_opts, sizeof table_opts);
1016 SIMPLEQ_INIT(&table_opts.init_nodes);
1019 { $$ = table_opts; }
1022 bzero(&table_opts, sizeof table_opts);
1023 SIMPLEQ_INIT(&table_opts.init_nodes);
1028 table_opts_l : table_opts_l table_opt
1032 table_opt : STRING {
1033 if (!strcmp($1, "const"))
1034 table_opts.flags |= PFR_TFLAG_CONST;
1035 else if (!strcmp($1, "persist"))
1036 table_opts.flags |= PFR_TFLAG_PERSIST;
1043 | '{' '}' { table_opts.init_addr = 1; }
1044 | '{' host_list '}' {
1045 struct node_host *n;
1046 struct node_tinit *ti;
1048 for (n = $2; n != NULL; n = n->next) {
1049 switch (n->addr.type) {
1050 case PF_ADDR_ADDRMASK:
1052 case PF_ADDR_DYNIFTL:
1053 yyerror("dynamic addresses are not "
1054 "permitted inside tables");
1057 yyerror("tables cannot contain tables");
1059 case PF_ADDR_NOROUTE:
1060 yyerror("\"no-route\" is not permitted "
1064 yyerror("unknown address type %d",
1069 if (!(ti = calloc(1, sizeof(*ti))))
1070 err(1, "table_opt: calloc");
1072 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1074 table_opts.init_addr = 1;
1077 struct node_tinit *ti;
1079 if (!(ti = calloc(1, sizeof(*ti))))
1080 err(1, "table_opt: calloc");
1082 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1084 table_opts.init_addr = 1;
1088 altqif : ALTQ interface queue_opts QUEUE qassign {
1091 if (check_rulestate(PFCTL_STATE_QUEUE))
1094 memset(&a, 0, sizeof(a));
1095 if ($3.scheduler.qtype == ALTQT_NONE) {
1096 yyerror("no scheduler specified!");
1099 a.scheduler = $3.scheduler.qtype;
1100 a.qlimit = $3.qlimit;
1101 a.tbrsize = $3.tbrsize;
1103 yyerror("no child queues specified");
1106 if (expand_altq(&a, $2, $5, $3.queue_bwspec,
1112 queuespec : QUEUE STRING interface queue_opts qassign {
1115 if (check_rulestate(PFCTL_STATE_QUEUE)) {
1120 memset(&a, 0, sizeof(a));
1122 if (strlcpy(a.qname, $2, sizeof(a.qname)) >=
1124 yyerror("queue name too long (max "
1125 "%d chars)", PF_QNAME_SIZE-1);
1131 yyerror("cannot specify tbrsize for queue");
1134 if ($4.priority > 255) {
1135 yyerror("priority out of range: max 255");
1138 a.priority = $4.priority;
1139 a.qlimit = $4.qlimit;
1140 a.scheduler = $4.scheduler.qtype;
1141 if (expand_queue(&a, $3, $5, $4.queue_bwspec,
1143 yyerror("errors in queue definition");
1150 bzero(&queue_opts, sizeof queue_opts);
1151 queue_opts.priority = DEFAULT_PRIORITY;
1152 queue_opts.qlimit = DEFAULT_QLIMIT;
1153 queue_opts.scheduler.qtype = ALTQT_NONE;
1154 queue_opts.queue_bwspec.bw_percent = 100;
1157 { $$ = queue_opts; }
1159 bzero(&queue_opts, sizeof queue_opts);
1160 queue_opts.priority = DEFAULT_PRIORITY;
1161 queue_opts.qlimit = DEFAULT_QLIMIT;
1162 queue_opts.scheduler.qtype = ALTQT_NONE;
1163 queue_opts.queue_bwspec.bw_percent = 100;
1168 queue_opts_l : queue_opts_l queue_opt
1172 queue_opt : BANDWIDTH bandwidth {
1173 if (queue_opts.marker & QOM_BWSPEC) {
1174 yyerror("bandwidth cannot be respecified");
1177 queue_opts.marker |= QOM_BWSPEC;
1178 queue_opts.queue_bwspec = $2;
1181 if (queue_opts.marker & QOM_PRIORITY) {
1182 yyerror("priority cannot be respecified");
1186 yyerror("priority out of range: max 255");
1189 queue_opts.marker |= QOM_PRIORITY;
1190 queue_opts.priority = $2;
1193 if (queue_opts.marker & QOM_QLIMIT) {
1194 yyerror("qlimit cannot be respecified");
1198 yyerror("qlimit out of range: max 65535");
1201 queue_opts.marker |= QOM_QLIMIT;
1202 queue_opts.qlimit = $2;
1205 if (queue_opts.marker & QOM_SCHEDULER) {
1206 yyerror("scheduler cannot be respecified");
1209 queue_opts.marker |= QOM_SCHEDULER;
1210 queue_opts.scheduler = $1;
1213 if (queue_opts.marker & QOM_TBRSIZE) {
1214 yyerror("tbrsize cannot be respecified");
1218 yyerror("tbrsize too big: max 65535");
1221 queue_opts.marker |= QOM_TBRSIZE;
1222 queue_opts.tbrsize = $2;
1226 bandwidth : STRING {
1232 bps = strtod($1, &cp);
1234 if (!strcmp(cp, "b"))
1236 else if (!strcmp(cp, "Kb"))
1238 else if (!strcmp(cp, "Mb"))
1240 else if (!strcmp(cp, "Gb"))
1241 bps *= 1000 * 1000 * 1000;
1242 else if (!strcmp(cp, "%")) {
1243 if (bps < 0 || bps > 100) {
1244 yyerror("bandwidth spec "
1249 $$.bw_percent = bps;
1252 yyerror("unknown unit %s", cp);
1258 $$.bw_absolute = (u_int32_t)bps;
1263 $$.qtype = ALTQT_CBQ;
1264 $$.data.cbq_opts.flags = 0;
1266 | CBQ '(' cbqflags_list ')' {
1267 $$.qtype = ALTQT_CBQ;
1268 $$.data.cbq_opts.flags = $3;
1271 $$.qtype = ALTQT_PRIQ;
1272 $$.data.priq_opts.flags = 0;
1274 | PRIQ '(' priqflags_list ')' {
1275 $$.qtype = ALTQT_PRIQ;
1276 $$.data.priq_opts.flags = $3;
1279 $$.qtype = ALTQT_HFSC;
1280 bzero(&$$.data.hfsc_opts,
1281 sizeof(struct node_hfsc_opts));
1283 | HFSC '(' hfsc_opts ')' {
1284 $$.qtype = ALTQT_HFSC;
1285 $$.data.hfsc_opts = $3;
1288 $$.qtype = ALTQT_FAIRQ;
1289 bzero(&$$.data.fairq_opts,
1290 sizeof(struct node_fairq_opts));
1292 | FAIRQ '(' fairq_opts ')' {
1293 $$.qtype = ALTQT_FAIRQ;
1294 $$.data.fairq_opts = $3;
1298 cbqflags_list : cbqflags_item { $$ |= $1; }
1299 | cbqflags_list comma cbqflags_item { $$ |= $3; }
1302 cbqflags_item : STRING {
1303 if (!strcmp($1, "default"))
1304 $$ = CBQCLF_DEFCLASS;
1305 else if (!strcmp($1, "borrow"))
1307 else if (!strcmp($1, "red"))
1309 else if (!strcmp($1, "ecn"))
1310 $$ = CBQCLF_RED|CBQCLF_ECN;
1311 else if (!strcmp($1, "rio"))
1314 yyerror("unknown cbq flag \"%s\"", $1);
1322 priqflags_list : priqflags_item { $$ |= $1; }
1323 | priqflags_list comma priqflags_item { $$ |= $3; }
1326 priqflags_item : STRING {
1327 if (!strcmp($1, "default"))
1328 $$ = PRCF_DEFAULTCLASS;
1329 else if (!strcmp($1, "red"))
1331 else if (!strcmp($1, "ecn"))
1332 $$ = PRCF_RED|PRCF_ECN;
1333 else if (!strcmp($1, "rio"))
1336 yyerror("unknown priq flag \"%s\"", $1);
1346 sizeof(struct node_hfsc_opts));
1353 hfscopts_list : hfscopts_item
1354 | hfscopts_list comma hfscopts_item
1357 hfscopts_item : LINKSHARE bandwidth {
1358 if (hfsc_opts.linkshare.used) {
1359 yyerror("linkshare already specified");
1362 hfsc_opts.linkshare.m2 = $2;
1363 hfsc_opts.linkshare.used = 1;
1365 | LINKSHARE '(' bandwidth number bandwidth ')' {
1366 if (hfsc_opts.linkshare.used) {
1367 yyerror("linkshare already specified");
1370 hfsc_opts.linkshare.m1 = $3;
1371 hfsc_opts.linkshare.d = $4;
1372 hfsc_opts.linkshare.m2 = $5;
1373 hfsc_opts.linkshare.used = 1;
1375 | REALTIME bandwidth {
1376 if (hfsc_opts.realtime.used) {
1377 yyerror("realtime already specified");
1380 hfsc_opts.realtime.m2 = $2;
1381 hfsc_opts.realtime.used = 1;
1383 | REALTIME '(' bandwidth number bandwidth ')' {
1384 if (hfsc_opts.realtime.used) {
1385 yyerror("realtime already specified");
1388 hfsc_opts.realtime.m1 = $3;
1389 hfsc_opts.realtime.d = $4;
1390 hfsc_opts.realtime.m2 = $5;
1391 hfsc_opts.realtime.used = 1;
1393 | UPPERLIMIT bandwidth {
1394 if (hfsc_opts.upperlimit.used) {
1395 yyerror("upperlimit already specified");
1398 hfsc_opts.upperlimit.m2 = $2;
1399 hfsc_opts.upperlimit.used = 1;
1401 | UPPERLIMIT '(' bandwidth number bandwidth ')' {
1402 if (hfsc_opts.upperlimit.used) {
1403 yyerror("upperlimit already specified");
1406 hfsc_opts.upperlimit.m1 = $3;
1407 hfsc_opts.upperlimit.d = $4;
1408 hfsc_opts.upperlimit.m2 = $5;
1409 hfsc_opts.upperlimit.used = 1;
1412 if (!strcmp($1, "default"))
1413 hfsc_opts.flags |= HFCF_DEFAULTCLASS;
1414 else if (!strcmp($1, "red"))
1415 hfsc_opts.flags |= HFCF_RED;
1416 else if (!strcmp($1, "ecn"))
1417 hfsc_opts.flags |= HFCF_RED|HFCF_ECN;
1418 else if (!strcmp($1, "rio"))
1419 hfsc_opts.flags |= HFCF_RIO;
1421 yyerror("unknown hfsc flag \"%s\"", $1);
1431 sizeof(struct node_fairq_opts));
1438 fairqopts_list : fairqopts_item
1439 | fairqopts_list comma fairqopts_item
1442 fairqopts_item : LINKSHARE bandwidth {
1443 if (fairq_opts.linkshare.used) {
1444 yyerror("linkshare already specified");
1447 fairq_opts.linkshare.m2 = $2;
1448 fairq_opts.linkshare.used = 1;
1450 | LINKSHARE '(' bandwidth number bandwidth ')' {
1451 if (fairq_opts.linkshare.used) {
1452 yyerror("linkshare already specified");
1455 fairq_opts.linkshare.m1 = $3;
1456 fairq_opts.linkshare.d = $4;
1457 fairq_opts.linkshare.m2 = $5;
1458 fairq_opts.linkshare.used = 1;
1461 fairq_opts.hogs_bw = $2;
1464 fairq_opts.nbuckets = $2;
1467 if (!strcmp($1, "default"))
1468 fairq_opts.flags |= FARF_DEFAULTCLASS;
1469 else if (!strcmp($1, "red"))
1470 fairq_opts.flags |= FARF_RED;
1471 else if (!strcmp($1, "ecn"))
1472 fairq_opts.flags |= FARF_RED|FARF_ECN;
1473 else if (!strcmp($1, "rio"))
1474 fairq_opts.flags |= FARF_RIO;
1476 yyerror("unknown fairq flag \"%s\"", $1);
1484 qassign : /* empty */ { $$ = NULL; }
1485 | qassign_item { $$ = $1; }
1486 | '{' qassign_list '}' { $$ = $2; }
1489 qassign_list : qassign_item { $$ = $1; }
1490 | qassign_list comma qassign_item {
1491 $1->tail->next = $3;
1497 qassign_item : STRING {
1498 $$ = calloc(1, sizeof(struct node_queue));
1500 err(1, "qassign_item: calloc");
1501 if (strlcpy($$->queue, $1, sizeof($$->queue)) >=
1502 sizeof($$->queue)) {
1503 yyerror("queue name '%s' too long (max "
1504 "%d chars)", $1, sizeof($$->queue)-1);
1515 pfrule : action dir logquick interface route af proto fromto
1519 struct node_state_opt *o;
1520 struct node_proto *proto;
1524 if (check_rulestate(PFCTL_STATE_FILTER))
1527 memset(&r, 0, sizeof(r));
1531 case PFRULE_RETURNRST:
1532 r.rule_flag |= PFRULE_RETURNRST;
1533 r.return_ttl = $1.w;
1535 case PFRULE_RETURNICMP:
1536 r.rule_flag |= PFRULE_RETURNICMP;
1537 r.return_icmp = $1.w;
1538 r.return_icmp6 = $1.w2;
1541 r.rule_flag |= PFRULE_RETURN;
1542 r.return_icmp = $1.w;
1543 r.return_icmp6 = $1.w2;
1552 if (strlcpy(r.tagname, $9.tag,
1553 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1554 yyerror("tag too long, max %u chars",
1555 PF_TAG_NAME_SIZE - 1);
1559 if (strlcpy(r.match_tagname, $9.match_tag,
1560 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1561 yyerror("tag too long, max %u chars",
1562 PF_TAG_NAME_SIZE - 1);
1565 r.match_tag_not = $9.match_tag_not;
1566 r.flags = $9.flags.b1;
1567 r.flagset = $9.flags.b2;
1568 if (rule_label(&r, $9.label))
1571 if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
1572 for (proto = $7; proto != NULL &&
1573 proto->proto != IPPROTO_TCP;
1574 proto = proto->next)
1576 if (proto == NULL && $7 != NULL) {
1577 if ($9.flags.b1 || $9.flags.b2)
1579 "flags only apply to tcp");
1582 "OS fingerprinting only "
1587 if (($9.flags.b1 & parse_flags("S")) == 0 &&
1589 yyerror("OS fingerprinting requires "
1590 "the SYN TCP flag (flags S/SA)");
1597 r.keep_state = $9.keep.action;
1598 o = $9.keep.options;
1600 struct node_state_opt *p = o;
1603 case PF_STATE_OPT_MAX:
1605 yyerror("state option 'max' "
1606 "multiple definitions");
1609 r.max_states = o->data.max_states;
1611 case PF_STATE_OPT_NOSYNC:
1612 if (r.rule_flag & PFRULE_NOSYNC) {
1613 yyerror("state option 'sync' "
1614 "multiple definitions");
1617 r.rule_flag |= PFRULE_NOSYNC;
1619 case PF_STATE_OPT_SRCTRACK:
1621 yyerror("state option "
1623 "multiple definitions");
1626 srctrack = o->data.src_track;
1628 case PF_STATE_OPT_MAX_SRC_STATES:
1629 if (r.max_src_states) {
1630 yyerror("state option "
1632 "multiple definitions");
1635 if (o->data.max_src_nodes == 0) {
1636 yyerror("'max-src-states' must "
1641 o->data.max_src_states;
1642 r.rule_flag |= PFRULE_SRCTRACK;
1644 case PF_STATE_OPT_MAX_SRC_NODES:
1645 if (r.max_src_nodes) {
1646 yyerror("state option "
1648 "multiple definitions");
1651 if (o->data.max_src_nodes == 0) {
1652 yyerror("'max-src-nodes' must "
1657 o->data.max_src_nodes;
1658 r.rule_flag |= PFRULE_SRCTRACK |
1659 PFRULE_RULESRCTRACK;
1661 case PF_STATE_OPT_STATELOCK:
1663 yyerror("state locking option: "
1664 "multiple definitions");
1668 r.rule_flag |= o->data.statelock;
1670 case PF_STATE_OPT_TIMEOUT:
1671 if (r.timeout[o->data.timeout.number]) {
1672 yyerror("state timeout %s "
1673 "multiple definitions",
1674 pf_timeouts[o->data.
1675 timeout.number].name);
1678 r.timeout[o->data.timeout.number] =
1679 o->data.timeout.seconds;
1685 if (srctrack == PF_SRCTRACK_GLOBAL &&
1687 yyerror("'max-src-nodes' is "
1688 "incompatible with "
1689 "'source-track global'");
1692 r.rule_flag |= PFRULE_SRCTRACK;
1693 if (srctrack == PF_SRCTRACK_RULE)
1694 r.rule_flag |= PFRULE_RULESRCTRACK;
1696 if (r.keep_state && !statelock)
1697 r.rule_flag |= default_statelock;
1700 r.rule_flag |= PFRULE_FRAGMENT;
1701 r.allow_opts = $9.allowopts;
1703 decide_address_family($8.src.host, &r.af);
1704 decide_address_family($8.dst.host, &r.af);
1708 yyerror("direction must be explicit "
1709 "with rules that specify routing");
1713 r.rpool.opts = $5.pool_opts;
1715 memcpy(&r.rpool.key, $5.key,
1716 sizeof(struct pf_poolhashkey));
1718 if (r.rt && r.rt != PF_FASTROUTE) {
1719 decide_address_family($5.host, &r.af);
1720 remove_invalid_hosts(&$5.host, &r.af);
1721 if ($5.host == NULL) {
1722 yyerror("no routing address with "
1723 "matching address family found.");
1726 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
1727 PF_POOL_NONE && ($5.host->next != NULL ||
1728 $5.host->addr.type == PF_ADDR_TABLE ||
1729 DYNIF_MULTIADDR($5.host->addr)))
1730 r.rpool.opts |= PF_POOL_ROUNDROBIN;
1731 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
1732 PF_POOL_ROUNDROBIN &&
1733 disallow_table($5.host, "tables are only "
1734 "supported in round-robin routing pools"))
1736 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
1737 PF_POOL_ROUNDROBIN &&
1738 disallow_alias($5.host, "interface (%s) "
1739 "is only supported in round-robin "
1742 if ($5.host->next != NULL) {
1743 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
1744 PF_POOL_ROUNDROBIN) {
1745 yyerror("r.rpool.opts must "
1746 "be PF_POOL_ROUNDROBIN");
1751 if ($9.queues.qname != NULL) {
1752 if (strlcpy(r.qname, $9.queues.qname,
1753 sizeof(r.qname)) >= sizeof(r.qname)) {
1754 yyerror("rule qname too long (max "
1755 "%d chars)", sizeof(r.qname)-1);
1758 free($9.queues.qname);
1760 if ($9.queues.pqname != NULL) {
1761 if (strlcpy(r.pqname, $9.queues.pqname,
1762 sizeof(r.pqname)) >= sizeof(r.pqname)) {
1763 yyerror("rule pqname too long (max "
1764 "%d chars)", sizeof(r.pqname)-1);
1767 free($9.queues.pqname);
1770 expand_rule(&r, $4, $5.host, $7, $8.src_os,
1771 $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
1772 $9.uid, $9.gid, $9.icmpspec);
1776 filter_opts : { bzero(&filter_opts, sizeof filter_opts); }
1778 { $$ = filter_opts; }
1780 bzero(&filter_opts, sizeof filter_opts);
1785 filter_opts_l : filter_opts_l filter_opt
1789 filter_opt : USER uids {
1790 if (filter_opts.uid)
1791 $2->tail->next = filter_opts.uid;
1792 filter_opts.uid = $2;
1795 if (filter_opts.gid)
1796 $2->tail->next = filter_opts.gid;
1797 filter_opts.gid = $2;
1800 if (filter_opts.marker & FOM_FLAGS) {
1801 yyerror("flags cannot be redefined");
1804 filter_opts.marker |= FOM_FLAGS;
1805 filter_opts.flags.b1 |= $1.b1;
1806 filter_opts.flags.b2 |= $1.b2;
1807 filter_opts.flags.w |= $1.w;
1808 filter_opts.flags.w2 |= $1.w2;
1811 if (filter_opts.marker & FOM_ICMP) {
1812 yyerror("icmp-type cannot be redefined");
1815 filter_opts.marker |= FOM_ICMP;
1816 filter_opts.icmpspec = $1;
1819 if (filter_opts.marker & FOM_TOS) {
1820 yyerror("tos cannot be redefined");
1823 filter_opts.marker |= FOM_TOS;
1824 filter_opts.tos = $1;
1827 if (filter_opts.marker & FOM_KEEP) {
1828 yyerror("modulate or keep cannot be redefined");
1831 filter_opts.marker |= FOM_KEEP;
1832 filter_opts.keep.action = $1.action;
1833 filter_opts.keep.options = $1.options;
1836 filter_opts.fragment = 1;
1839 filter_opts.allowopts = 1;
1842 if (filter_opts.label) {
1843 yyerror("label cannot be redefined");
1846 filter_opts.label = $1;
1849 if (filter_opts.queues.qname) {
1850 yyerror("queue cannot be redefined");
1853 filter_opts.queues = $1;
1856 filter_opts.tag = $2;
1858 | not TAGGED string {
1859 filter_opts.match_tag = $3;
1860 filter_opts.match_tag_not = $1;
1864 action : PASS { $$.b1 = PF_PASS; $$.b2 = $$.w = 0; }
1865 | BLOCK blockspec { $$ = $2; $$.b1 = PF_DROP; }
1868 blockspec : /* empty */ {
1869 $$.b2 = blockpolicy;
1870 $$.w = returnicmpdefault;
1871 $$.w2 = returnicmp6default;
1874 $$.b2 = PFRULE_DROP;
1879 $$.b2 = PFRULE_RETURNRST;
1883 | RETURNRST '(' TTL number ')' {
1885 yyerror("illegal ttl value %d", $4);
1888 $$.b2 = PFRULE_RETURNRST;
1893 $$.b2 = PFRULE_RETURNICMP;
1894 $$.w = returnicmpdefault;
1895 $$.w2 = returnicmp6default;
1898 $$.b2 = PFRULE_RETURNICMP;
1899 $$.w = returnicmpdefault;
1900 $$.w2 = returnicmp6default;
1902 | RETURNICMP '(' STRING ')' {
1903 $$.b2 = PFRULE_RETURNICMP;
1904 if (!($$.w = parseicmpspec($3, AF_INET))) {
1909 $$.w2 = returnicmp6default;
1911 | RETURNICMP6 '(' STRING ')' {
1912 $$.b2 = PFRULE_RETURNICMP;
1913 $$.w = returnicmpdefault;
1914 if (!($$.w2 = parseicmpspec($3, AF_INET6))) {
1920 | RETURNICMP '(' STRING comma STRING ')' {
1921 $$.b2 = PFRULE_RETURNICMP;
1922 if (!($$.w = parseicmpspec($3, AF_INET)) ||
1923 !($$.w2 = parseicmpspec($5, AF_INET6))) {
1932 $$.b2 = PFRULE_RETURN;
1933 $$.w = returnicmpdefault;
1934 $$.w2 = returnicmp6default;
1938 dir : /* empty */ { $$ = 0; }
1939 | IN { $$ = PF_IN; }
1940 | OUT { $$ = PF_OUT; }
1943 logquick : /* empty */ { $$.log = 0; $$.quick = 0; }
1944 | log { $$.log = $1; $$.quick = 0; }
1945 | QUICK { $$.log = 0; $$.quick = 1; }
1946 | log QUICK { $$.log = $1; $$.quick = 1; }
1947 | QUICK log { $$.log = $2; $$.quick = 1; }
1950 log : LOG { $$ = 1; }
1951 | LOGALL { $$ = 2; }
1954 interface : /* empty */ { $$ = NULL; }
1955 | ON if_item_not { $$ = $2; }
1956 | ON '{' if_list '}' { $$ = $3; }
1959 if_list : if_item_not { $$ = $1; }
1960 | if_list comma if_item_not {
1961 $1->tail->next = $3;
1967 if_item_not : not if_item { $$ = $2; $$->not = $1; }
1971 struct node_host *n;
1973 if ((n = ifa_exists($1, 1)) == NULL) {
1974 yyerror("unknown interface %s", $1);
1978 $$ = calloc(1, sizeof(struct node_if));
1980 err(1, "if_item: calloc");
1981 if (strlcpy($$->ifname, $1, sizeof($$->ifname)) >=
1982 sizeof($$->ifname)) {
1985 yyerror("interface name too long");
1989 $$->ifa_flags = n->ifa_flags;
1996 af : /* empty */ { $$ = 0; }
1997 | INET { $$ = AF_INET; }
1998 | INET6 { $$ = AF_INET6; }
2001 proto : /* empty */ { $$ = NULL; }
2002 | PROTO proto_item { $$ = $2; }
2003 | PROTO '{' proto_list '}' { $$ = $3; }
2006 proto_list : proto_item { $$ = $1; }
2007 | proto_list comma proto_item {
2008 $1->tail->next = $3;
2014 proto_item : STRING {
2018 if (atoul($1, &ulval) == 0) {
2020 yyerror("protocol outside range");
2024 pr = (u_int8_t)ulval;
2028 p = getprotobyname($1);
2030 yyerror("unknown protocol %s", $1);
2038 yyerror("proto 0 cannot be used");
2041 $$ = calloc(1, sizeof(struct node_proto));
2043 err(1, "proto_item: calloc");
2064 os : /* empty */ { $$ = NULL; }
2065 | OS xos { $$ = $2; }
2066 | OS '{' os_list '}' { $$ = $3; }
2070 $$ = calloc(1, sizeof(struct node_os));
2072 err(1, "os: calloc");
2078 os_list : xos { $$ = $1; }
2079 | os_list comma xos {
2080 $1->tail->next = $3;
2086 from : /* empty */ {
2104 ipportspec : ipspec {
2108 | ipspec PORT portspec {
2118 ipspec : ANY { $$ = NULL; }
2119 | xhost { $$ = $1; }
2120 | '{' host_list '}' { $$ = $2; }
2123 host_list : xhost { $$ = $1; }
2124 | host_list comma xhost {
2127 else if ($1 == NULL)
2130 $1->tail->next = $3;
2131 $1->tail = $3->tail;
2138 struct node_host *n;
2140 for (n = $2; n != NULL; n = n->next)
2145 $$ = calloc(1, sizeof(struct node_host));
2147 err(1, "xhost: calloc");
2148 $$->addr.type = PF_ADDR_NOROUTE;
2155 if (($$ = host($1)) == NULL) {
2156 /* error. "any" is handled elsewhere */
2158 yyerror("could not parse host specification");
2164 | STRING '/' number {
2167 if (asprintf(&buf, "%s/%u", $1, $3) == -1)
2168 err(1, "host: asprintf");
2170 if (($$ = host(buf)) == NULL) {
2171 /* error. "any" is handled elsewhere */
2173 yyerror("could not parse host specification");
2179 | dynaddr '/' number {
2180 struct node_host *n;
2183 for (n = $1; n != NULL; n = n->next)
2187 if (strlen($2) >= PF_TABLE_NAME_SIZE) {
2188 yyerror("table name '%s' too long", $2);
2192 $$ = calloc(1, sizeof(struct node_host));
2194 err(1, "host: calloc");
2195 $$->addr.type = PF_ADDR_TABLE;
2196 if (strlcpy($$->addr.v.tblname, $2,
2197 sizeof($$->addr.v.tblname)) >=
2198 sizeof($$->addr.v.tblname))
2199 errx(1, "host: strlcpy");
2209 if (atoul($1, &ulval) == -1) {
2210 yyerror("%s is not a number", $1);
2219 dynaddr : '(' STRING ')' {
2224 while ((p = strrchr($2, ':')) != NULL) {
2225 if (!strcmp(p+1, "network"))
2226 flags |= PFI_AFLAG_NETWORK;
2227 else if (!strcmp(p+1, "broadcast"))
2228 flags |= PFI_AFLAG_BROADCAST;
2229 else if (!strcmp(p+1, "peer"))
2230 flags |= PFI_AFLAG_PEER;
2231 else if (!strcmp(p+1, "0"))
2232 flags |= PFI_AFLAG_NOALIAS;
2234 yyerror("interface %s has bad modifier",
2241 if (flags & (flags - 1) & PFI_AFLAG_MODEMASK) {
2243 yyerror("illegal combination of "
2244 "interface modifiers");
2247 if (ifa_exists($2, 1) == NULL && strcmp($2, "self")) {
2248 yyerror("interface %s does not exist", $2);
2252 $$ = calloc(1, sizeof(struct node_host));
2254 err(1, "address: calloc");
2256 set_ipmask($$, 128);
2257 $$->addr.type = PF_ADDR_DYNIFTL;
2258 $$->addr.iflags = flags;
2259 if (strlcpy($$->addr.v.ifname, $2,
2260 sizeof($$->addr.v.ifname)) >=
2261 sizeof($$->addr.v.ifname)) {
2264 yyerror("interface name too long");
2273 portspec : port_item { $$ = $1; }
2274 | '{' port_list '}' { $$ = $2; }
2277 port_list : port_item { $$ = $1; }
2278 | port_list comma port_item {
2279 $1->tail->next = $3;
2286 $$ = calloc(1, sizeof(struct node_port));
2288 err(1, "port_item: calloc");
2300 yyerror("':' cannot be used with an other "
2304 $$ = calloc(1, sizeof(struct node_port));
2306 err(1, "port_item: calloc");
2313 | port PORTBINARY port {
2315 yyerror("':' cannot be used with an other "
2319 $$ = calloc(1, sizeof(struct node_port));
2321 err(1, "port_item: calloc");
2331 char *p = strchr($1, ':');
2332 struct servent *s = NULL;
2336 if (atoul($1, &ulval) == 0) {
2337 if (ulval > 65535) {
2339 yyerror("illegal port value %d",
2343 $$.a = htons(ulval);
2345 s = getservbyname($1, "tcp");
2347 s = getservbyname($1, "udp");
2349 yyerror("unknown port %s", $1);
2361 if ((port[0] = getservice($1)) == -1 ||
2362 (port[1] = getservice(p)) == -1) {
2374 uids : uid_item { $$ = $1; }
2375 | '{' uid_list '}' { $$ = $2; }
2378 uid_list : uid_item { $$ = $1; }
2379 | uid_list comma uid_item {
2380 $1->tail->next = $3;
2387 $$ = calloc(1, sizeof(struct node_uid));
2389 err(1, "uid_item: calloc");
2397 if ($2 == UID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
2398 yyerror("user unknown requires operator = or "
2402 $$ = calloc(1, sizeof(struct node_uid));
2404 err(1, "uid_item: calloc");
2411 | uid PORTBINARY uid {
2412 if ($1 == UID_MAX || $3 == UID_MAX) {
2413 yyerror("user unknown requires operator = or "
2417 $$ = calloc(1, sizeof(struct node_uid));
2419 err(1, "uid_item: calloc");
2431 if (atoul($1, &ulval) == -1) {
2432 if (!strcmp($1, "unknown"))
2437 if ((pw = getpwnam($1)) == NULL) {
2438 yyerror("unknown user %s", $1);
2445 if (ulval >= UID_MAX) {
2447 yyerror("illegal uid value %lu", ulval);
2456 gids : gid_item { $$ = $1; }
2457 | '{' gid_list '}' { $$ = $2; }
2460 gid_list : gid_item { $$ = $1; }
2461 | gid_list comma gid_item {
2462 $1->tail->next = $3;
2469 $$ = calloc(1, sizeof(struct node_gid));
2471 err(1, "gid_item: calloc");
2479 if ($2 == GID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
2480 yyerror("group unknown requires operator = or "
2484 $$ = calloc(1, sizeof(struct node_gid));
2486 err(1, "gid_item: calloc");
2493 | gid PORTBINARY gid {
2494 if ($1 == GID_MAX || $3 == GID_MAX) {
2495 yyerror("group unknown requires operator = or "
2499 $$ = calloc(1, sizeof(struct node_gid));
2501 err(1, "gid_item: calloc");
2513 if (atoul($1, &ulval) == -1) {
2514 if (!strcmp($1, "unknown"))
2519 if ((grp = getgrnam($1)) == NULL) {
2520 yyerror("unknown group %s", $1);
2527 if (ulval >= GID_MAX) {
2528 yyerror("illegal gid value %lu", ulval);
2541 if ((f = parse_flags($1)) < 0) {
2542 yyerror("bad flags %s", $1);
2551 flags : FLAGS flag '/' flag { $$.b1 = $2.b1; $$.b2 = $4.b1; }
2552 | FLAGS '/' flag { $$.b1 = 0; $$.b2 = $3.b1; }
2555 icmpspec : ICMPTYPE icmp_item { $$ = $2; }
2556 | ICMPTYPE '{' icmp_list '}' { $$ = $3; }
2557 | ICMP6TYPE icmp6_item { $$ = $2; }
2558 | ICMP6TYPE '{' icmp6_list '}' { $$ = $3; }
2561 icmp_list : icmp_item { $$ = $1; }
2562 | icmp_list comma icmp_item {
2563 $1->tail->next = $3;
2569 icmp6_list : icmp6_item { $$ = $1; }
2570 | icmp6_list comma icmp6_item {
2571 $1->tail->next = $3;
2577 icmp_item : icmptype {
2578 $$ = calloc(1, sizeof(struct node_icmp));
2580 err(1, "icmp_item: calloc");
2583 $$->proto = IPPROTO_ICMP;
2587 | icmptype CODE STRING {
2588 const struct icmpcodeent *p;
2591 if (atoul($3, &ulval) == 0) {
2594 yyerror("illegal icmp-code %d", ulval);
2598 if ((p = geticmpcodebyname($1-1, $3,
2599 AF_INET)) == NULL) {
2600 yyerror("unknown icmp-code %s", $3);
2607 $$ = calloc(1, sizeof(struct node_icmp));
2609 err(1, "icmp_item: calloc");
2611 $$->code = ulval + 1;
2612 $$->proto = IPPROTO_ICMP;
2618 icmp6_item : icmp6type {
2619 $$ = calloc(1, sizeof(struct node_icmp));
2621 err(1, "icmp_item: calloc");
2624 $$->proto = IPPROTO_ICMPV6;
2628 | icmp6type CODE STRING {
2629 const struct icmpcodeent *p;
2632 if (atoul($3, &ulval) == 0) {
2634 yyerror("illegal icmp6-code %ld",
2640 if ((p = geticmpcodebyname($1-1, $3,
2641 AF_INET6)) == NULL) {
2642 yyerror("unknown icmp6-code %s", $3);
2649 $$ = calloc(1, sizeof(struct node_icmp));
2651 err(1, "icmp_item: calloc");
2653 $$->code = ulval + 1;
2654 $$->proto = IPPROTO_ICMPV6;
2661 const struct icmptypeent *p;
2664 if (atoul($1, &ulval) == 0) {
2666 yyerror("illegal icmp-type %d", ulval);
2672 if ((p = geticmptypebyname($1, AF_INET)) ==
2674 yyerror("unknown icmp-type %s", $1);
2684 icmp6type : STRING {
2685 const struct icmptypeent *p;
2688 if (atoul($1, &ulval) == 0) {
2690 yyerror("illegal icmp6-type %d", ulval);
2696 if ((p = geticmptypebyname($1, AF_INET6)) ==
2698 yyerror("unknown icmp6-type %s", $1);
2709 if (!strcmp($2, "lowdelay"))
2710 $$ = IPTOS_LOWDELAY;
2711 else if (!strcmp($2, "throughput"))
2712 $$ = IPTOS_THROUGHPUT;
2713 else if (!strcmp($2, "reliability"))
2714 $$ = IPTOS_RELIABILITY;
2715 else if ($2[0] == '0' && $2[1] == 'x')
2716 $$ = strtoul($2, NULL, 16);
2718 $$ = strtoul($2, NULL, 10);
2719 if (!$$ || $$ > 255) {
2720 yyerror("illegal tos value %s", $2);
2728 sourcetrack : SOURCETRACK { $$ = PF_SRCTRACK; }
2729 | SOURCETRACK GLOBAL { $$ = PF_SRCTRACK_GLOBAL; }
2730 | SOURCETRACK RULE { $$ = PF_SRCTRACK_RULE; }
2733 statelock : IFBOUND {
2734 $$ = PFRULE_IFBOUND;
2737 $$ = PFRULE_GRBOUND;
2744 keep : KEEP STATE state_opt_spec {
2745 $$.action = PF_STATE_NORMAL;
2748 | MODULATE STATE state_opt_spec {
2749 $$.action = PF_STATE_MODULATE;
2752 | SYNPROXY STATE state_opt_spec {
2753 $$.action = PF_STATE_SYNPROXY;
2758 state_opt_spec : '(' state_opt_list ')' { $$ = $2; }
2759 | /* empty */ { $$ = NULL; }
2762 state_opt_list : state_opt_item { $$ = $1; }
2763 | state_opt_list comma state_opt_item {
2764 $1->tail->next = $3;
2770 state_opt_item : MAXIMUM number {
2771 $$ = calloc(1, sizeof(struct node_state_opt));
2773 err(1, "state_opt_item: calloc");
2774 $$->type = PF_STATE_OPT_MAX;
2775 $$->data.max_states = $2;
2780 $$ = calloc(1, sizeof(struct node_state_opt));
2782 err(1, "state_opt_item: calloc");
2783 $$->type = PF_STATE_OPT_NOSYNC;
2787 | MAXSRCSTATES number {
2788 $$ = calloc(1, sizeof(struct node_state_opt));
2790 err(1, "state_opt_item: calloc");
2791 $$->type = PF_STATE_OPT_MAX_SRC_STATES;
2792 $$->data.max_src_states = $2;
2796 | MAXSRCNODES number {
2797 $$ = calloc(1, sizeof(struct node_state_opt));
2799 err(1, "state_opt_item: calloc");
2800 $$->type = PF_STATE_OPT_MAX_SRC_NODES;
2801 $$->data.max_src_nodes = $2;
2806 $$ = calloc(1, sizeof(struct node_state_opt));
2808 err(1, "state_opt_item: calloc");
2809 $$->type = PF_STATE_OPT_SRCTRACK;
2810 $$->data.src_track = $1;
2815 $$ = calloc(1, sizeof(struct node_state_opt));
2817 err(1, "state_opt_item: calloc");
2818 $$->type = PF_STATE_OPT_STATELOCK;
2819 $$->data.statelock = $1;
2826 for (i = 0; pf_timeouts[i].name &&
2827 strcmp(pf_timeouts[i].name, $1); ++i)
2829 if (!pf_timeouts[i].name) {
2830 yyerror("illegal timeout name %s", $1);
2834 if (strchr(pf_timeouts[i].name, '.') == NULL) {
2835 yyerror("illegal state timeout %s", $1);
2840 $$ = calloc(1, sizeof(struct node_state_opt));
2842 err(1, "state_opt_item: calloc");
2843 $$->type = PF_STATE_OPT_TIMEOUT;
2844 $$->data.timeout.number = pf_timeouts[i].timeout;
2845 $$->data.timeout.seconds = $2;
2851 label : LABEL STRING {
2856 qname : QUEUE STRING {
2859 | QUEUE '(' STRING ')' {
2862 | QUEUE '(' STRING comma STRING ')' {
2868 no : /* empty */ { $$ = 0; }
2873 char *p = strchr($1, ':');
2876 if (($$.a = getservice($1)) == -1) {
2881 } else if (!strcmp(p+1, "*")) {
2883 if (($$.a = getservice($1)) == -1) {
2891 if (($$.a = getservice($1)) == -1 ||
2892 ($$.b = getservice(p)) == -1) {
2904 redirspec : host { $$ = $1; }
2905 | '{' redir_host_list '}' { $$ = $2; }
2908 redir_host_list : host { $$ = $1; }
2909 | redir_host_list comma host {
2910 $1->tail->next = $3;
2911 $1->tail = $3->tail;
2916 redirpool : /* empty */ { $$ = NULL; }
2918 $$ = calloc(1, sizeof(struct redirection));
2920 err(1, "redirection: calloc");
2922 $$->rport.a = $$->rport.b = $$->rport.t = 0;
2924 | ARROW redirspec PORT rport {
2925 $$ = calloc(1, sizeof(struct redirection));
2927 err(1, "redirection: calloc");
2933 hashkey : /* empty */
2935 $$ = calloc(1, sizeof(struct pf_poolhashkey));
2937 err(1, "hashkey: calloc");
2938 $$->key32[0] = arc4random();
2939 $$->key32[1] = arc4random();
2940 $$->key32[2] = arc4random();
2941 $$->key32[3] = arc4random();
2945 if (!strncmp($1, "0x", 2)) {
2946 if (strlen($1) != 34) {
2948 yyerror("hex key must be 128 bits "
2949 "(32 hex digits) long");
2952 $$ = calloc(1, sizeof(struct pf_poolhashkey));
2954 err(1, "hashkey: calloc");
2956 if (sscanf($1, "0x%8x%8x%8x%8x",
2957 &$$->key32[0], &$$->key32[1],
2958 &$$->key32[2], &$$->key32[3]) != 4) {
2961 yyerror("invalid hex key");
2967 $$ = calloc(1, sizeof(struct pf_poolhashkey));
2969 err(1, "hashkey: calloc");
2971 MD5Update(&context, (unsigned char *)$1,
2973 MD5Final((unsigned char *)$$, &context);
2974 $$->key32[0] = htonl($$->key32[0]);
2975 $$->key32[1] = htonl($$->key32[1]);
2976 $$->key32[2] = htonl($$->key32[2]);
2977 $$->key32[3] = htonl($$->key32[3]);
2983 pool_opts : { bzero(&pool_opts, sizeof pool_opts); }
2987 bzero(&pool_opts, sizeof pool_opts);
2992 pool_opts_l : pool_opts_l pool_opt
2996 pool_opt : BITMASK {
2997 if (pool_opts.type) {
2998 yyerror("pool type cannot be redefined");
3001 pool_opts.type = PF_POOL_BITMASK;
3004 if (pool_opts.type) {
3005 yyerror("pool type cannot be redefined");
3008 pool_opts.type = PF_POOL_RANDOM;
3010 | SOURCEHASH hashkey {
3011 if (pool_opts.type) {
3012 yyerror("pool type cannot be redefined");
3015 pool_opts.type = PF_POOL_SRCHASH;
3019 if (pool_opts.type) {
3020 yyerror("pool type cannot be redefined");
3023 pool_opts.type = PF_POOL_ROUNDROBIN;
3026 if (pool_opts.staticport) {
3027 yyerror("static-port cannot be redefined");
3030 pool_opts.staticport = 1;
3033 if (filter_opts.marker & POM_STICKYADDRESS) {
3034 yyerror("sticky-address cannot be redefined");
3037 pool_opts.marker |= POM_STICKYADDRESS;
3038 pool_opts.opts |= PF_POOL_STICKYADDR;
3042 redirection : /* empty */ { $$ = NULL; }
3044 $$ = calloc(1, sizeof(struct redirection));
3046 err(1, "redirection: calloc");
3048 $$->rport.a = $$->rport.b = $$->rport.t = 0;
3050 | ARROW host PORT rport {
3051 $$ = calloc(1, sizeof(struct redirection));
3053 err(1, "redirection: calloc");
3059 natpass : /* empty */ { $$ = 0; }
3063 nataction : no NAT natpass {
3081 natrule : nataction interface af proto fromto tag redirpool pool_opts
3085 if (check_rulestate(PFCTL_STATE_NAT))
3088 memset(&r, 0, sizeof(r));
3095 if ($5.src.host && $5.src.host->af &&
3096 !$5.src.host->ifindex)
3097 r.af = $5.src.host->af;
3098 else if ($5.dst.host && $5.dst.host->af &&
3099 !$5.dst.host->ifindex)
3100 r.af = $5.dst.host->af;
3104 if (strlcpy(r.tagname, $6, PF_TAG_NAME_SIZE) >=
3106 yyerror("tag too long, max %u chars",
3107 PF_TAG_NAME_SIZE - 1);
3111 if (r.action == PF_NONAT || r.action == PF_NORDR) {
3113 yyerror("translation rule with 'no' "
3114 "does not need '->'");
3118 if ($7 == NULL || $7->host == NULL) {
3119 yyerror("translation rule requires '-> "
3123 if (!r.af && ! $7->host->ifindex)
3124 r.af = $7->host->af;
3126 remove_invalid_hosts(&$7->host, &r.af);
3127 if (invalid_redirect($7->host, r.af))
3129 if (check_netmask($7->host, r.af))
3132 r.rpool.proxy_port[0] = ntohs($7->rport.a);
3136 if (!$7->rport.b && $7->rport.t &&
3137 $5.dst.port != NULL) {
3138 r.rpool.proxy_port[1] =
3139 ntohs($7->rport.a) +
3141 $5.dst.port->port[1]) -
3143 $5.dst.port->port[0]));
3145 r.rpool.proxy_port[1] =
3149 r.rpool.proxy_port[1] =
3151 if (!r.rpool.proxy_port[0] &&
3152 !r.rpool.proxy_port[1]) {
3153 r.rpool.proxy_port[0] =
3154 PF_NAT_PROXY_PORT_LOW;
3155 r.rpool.proxy_port[1] =
3156 PF_NAT_PROXY_PORT_HIGH;
3157 } else if (!r.rpool.proxy_port[1])
3158 r.rpool.proxy_port[1] =
3159 r.rpool.proxy_port[0];
3165 r.rpool.opts = $8.type;
3166 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
3167 PF_POOL_NONE && ($7->host->next != NULL ||
3168 $7->host->addr.type == PF_ADDR_TABLE ||
3169 DYNIF_MULTIADDR($7->host->addr)))
3170 r.rpool.opts = PF_POOL_ROUNDROBIN;
3171 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
3172 PF_POOL_ROUNDROBIN &&
3173 disallow_table($7->host, "tables are only "
3174 "supported in round-robin redirection "
3177 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
3178 PF_POOL_ROUNDROBIN &&
3179 disallow_alias($7->host, "interface (%s) "
3180 "is only supported in round-robin "
3181 "redirection pools"))
3183 if ($7->host->next != NULL) {
3184 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
3185 PF_POOL_ROUNDROBIN) {
3186 yyerror("only round-robin "
3187 "valid for multiple "
3188 "redirection addresses");
3195 memcpy(&r.rpool.key, $8.key,
3196 sizeof(struct pf_poolhashkey));
3199 r.rpool.opts |= $8.opts;
3201 if ($8.staticport) {
3202 if (r.action != PF_NAT) {
3203 yyerror("the 'static-port' option is "
3204 "only valid with nat rules");
3207 if (r.rpool.proxy_port[0] !=
3208 PF_NAT_PROXY_PORT_LOW &&
3209 r.rpool.proxy_port[1] !=
3210 PF_NAT_PROXY_PORT_HIGH) {
3211 yyerror("the 'static-port' option can't"
3212 " be used when specifying a port"
3216 r.rpool.proxy_port[0] = 0;
3217 r.rpool.proxy_port[1] = 0;
3220 expand_rule(&r, $2, $7 == NULL ? NULL : $7->host, $4,
3221 $5.src_os, $5.src.host, $5.src.port, $5.dst.host,
3222 $5.dst.port, 0, 0, 0);
3227 binatrule : no BINAT natpass interface af proto FROM host TO ipspec tag
3230 struct pf_rule binat;
3231 struct pf_pooladdr *pa;
3233 if (check_rulestate(PFCTL_STATE_NAT))
3236 memset(&binat, 0, sizeof(binat));
3239 binat.action = PF_NOBINAT;
3241 binat.action = PF_BINAT;
3244 if (!binat.af && $8 != NULL && $8->af)
3246 if (!binat.af && $10 != NULL && $10->af)
3248 if (!binat.af && $12 != NULL && $12->host)
3249 binat.af = $12->host->af;
3251 yyerror("address family (inet/inet6) "
3257 memcpy(binat.ifname, $4->ifname,
3258 sizeof(binat.ifname));
3259 binat.ifnot = $4->not;
3263 if (strlcpy(binat.tagname, $11,
3264 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
3265 yyerror("tag too long, max %u chars",
3266 PF_TAG_NAME_SIZE - 1);
3271 binat.proto = $6->proto;
3275 if ($8 != NULL && disallow_table($8, "invalid use of "
3276 "table <%s> as the source address of a binat rule"))
3278 if ($8 != NULL && disallow_alias($8, "invalid use of "
3279 "interface (%s) as the source address of a binat "
3282 if ($12 != NULL && $12->host != NULL && disallow_table(
3283 $12->host, "invalid use of table <%s> as the "
3284 "redirect address of a binat rule"))
3286 if ($12 != NULL && $12->host != NULL && disallow_alias(
3287 $12->host, "invalid use of interface (%s) as the "
3288 "redirect address of a binat rule"))
3293 yyerror("multiple binat ip addresses");
3296 if ($8->addr.type == PF_ADDR_DYNIFTL)
3298 if ($8->af != binat.af) {
3299 yyerror("binat ip versions must match");
3302 if (check_netmask($8, binat.af))
3304 memcpy(&binat.src.addr, &$8->addr,
3305 sizeof(binat.src.addr));
3310 yyerror("multiple binat ip addresses");
3313 if ($10->af != binat.af && $10->af) {
3314 yyerror("binat ip versions must match");
3317 if (check_netmask($10, binat.af))
3319 memcpy(&binat.dst.addr, &$10->addr,
3320 sizeof(binat.dst.addr));
3321 binat.dst.not = $10->not;
3325 if (binat.action == PF_NOBINAT) {
3327 yyerror("'no binat' rule does not need"
3332 if ($12 == NULL || $12->host == NULL) {
3333 yyerror("'binat' rule requires"
3338 remove_invalid_hosts(&$12->host, &binat.af);
3339 if (invalid_redirect($12->host, binat.af))
3341 if ($12->host->next != NULL) {
3342 yyerror("binat rule must redirect to "
3343 "a single address");
3346 if (check_netmask($12->host, binat.af))
3349 if (!PF_AZERO(&binat.src.addr.v.a.mask,
3351 !PF_AEQ(&binat.src.addr.v.a.mask,
3352 &$12->host->addr.v.a.mask, binat.af)) {
3353 yyerror("'binat' source mask and "
3354 "redirect mask must be the same");
3358 TAILQ_INIT(&binat.rpool.list);
3359 pa = calloc(1, sizeof(struct pf_pooladdr));
3361 err(1, "binat: calloc");
3362 pa->addr = $12->host->addr;
3364 TAILQ_INSERT_TAIL(&binat.rpool.list,
3370 pfctl_add_rule(pf, &binat);
3374 tag : /* empty */ { $$ = NULL; }
3375 | TAG STRING { $$ = $2; }
3378 route_host : STRING {
3379 $$ = calloc(1, sizeof(struct node_host));
3381 err(1, "route_host: calloc");
3383 if (ifa_exists($$->ifname, 0) == NULL) {
3384 yyerror("routeto: unknown interface %s",
3390 set_ipmask($$, 128);
3394 | '(' STRING host ')' {
3397 if (ifa_exists($$->ifname, 0) == NULL) {
3398 yyerror("routeto: unknown interface %s",
3405 route_host_list : route_host { $$ = $1; }
3406 | route_host_list comma route_host {
3409 if ($1->af != $3->af) {
3410 yyerror("all pool addresses must be in the "
3411 "same address family");
3414 $1->tail->next = $3;
3415 $1->tail = $3->tail;
3420 routespec : route_host { $$ = $1; }
3421 | '{' route_host_list '}' { $$ = $2; }
3424 route : /* empty */ {
3431 $$.rt = PF_FASTROUTE;
3434 | ROUTETO routespec pool_opts {
3437 $$.pool_opts = $3.type | $3.opts;
3441 | REPLYTO routespec pool_opts {
3444 $$.pool_opts = $3.type | $3.opts;
3448 | DUPTO routespec pool_opts {
3451 $$.pool_opts = $3.type | $3.opts;
3457 timeout_spec : STRING number
3459 if (check_rulestate(PFCTL_STATE_OPTION)) {
3463 if (pfctl_set_timeout(pf, $1, $2, 0) != 0) {
3464 yyerror("unknown timeout %s", $1);
3472 timeout_list : timeout_list comma timeout_spec
3476 limit_spec : STRING number
3478 if (check_rulestate(PFCTL_STATE_OPTION)) {
3482 if (pfctl_set_limit(pf, $1, $2) != 0) {
3483 yyerror("unable to set limit %s %u", $1, $2);
3491 limit_list : limit_list comma limit_spec
3499 yesno : NO { $$ = 0; }
3501 if (!strcmp($1, "yes"))
3511 unaryop : '=' { $$ = PF_OP_EQ; }
3512 | '!' '=' { $$ = PF_OP_NE; }
3513 | '<' '=' { $$ = PF_OP_LE; }
3514 | '<' { $$ = PF_OP_LT; }
3515 | '>' '=' { $$ = PF_OP_GE; }
3516 | '>' { $$ = PF_OP_GT; }
3522 yyerror(const char *fmt, ...)
3528 fprintf(stderr, "%s:%d: ", infile, yylval.lineno);
3529 vfprintf(stderr, fmt, ap);
3530 fprintf(stderr, "\n");
3536 disallow_table(struct node_host *h, const char *fmt)
3538 for (; h != NULL; h = h->next)
3539 if (h->addr.type == PF_ADDR_TABLE) {
3540 yyerror(fmt, h->addr.v.tblname);
3547 disallow_alias(struct node_host *h, const char *fmt)
3549 for (; h != NULL; h = h->next)
3550 if (DYNIF_MULTIADDR(h->addr)) {
3551 yyerror(fmt, h->addr.v.tblname);
3558 rule_consistent(struct pf_rule *r)
3562 switch (r->action) {
3566 problems = filter_consistent(r);
3570 problems = nat_consistent(r);
3574 problems = rdr_consistent(r);
3585 filter_consistent(struct pf_rule *r)
3589 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP &&
3590 (r->src.port_op || r->dst.port_op)) {
3591 yyerror("port only applies to tcp/udp");
3594 if (r->proto != IPPROTO_ICMP && r->proto != IPPROTO_ICMPV6 &&
3595 (r->type || r->code)) {
3596 yyerror("icmp-type/code only applies to icmp");
3599 if (!r->af && (r->type || r->code)) {
3600 yyerror("must indicate address family with icmp-type/code");
3603 if ((r->proto == IPPROTO_ICMP && r->af == AF_INET6) ||
3604 (r->proto == IPPROTO_ICMPV6 && r->af == AF_INET)) {
3605 yyerror("proto %s doesn't match address family %s",
3606 r->proto == IPPROTO_ICMP ? "icmp" : "icmp6",
3607 r->af == AF_INET ? "inet" : "inet6");
3610 if (r->allow_opts && r->action != PF_PASS) {
3611 yyerror("allow-opts can only be specified for pass rules");
3614 if (r->rule_flag & PFRULE_FRAGMENT && (r->src.port_op ||
3615 r->dst.port_op || r->flagset || r->type || r->code)) {
3616 yyerror("fragments can be filtered only on IP header fields");
3619 if (r->rule_flag & PFRULE_RETURNRST && r->proto != IPPROTO_TCP) {
3620 yyerror("return-rst can only be applied to TCP rules");
3623 if (r->max_src_nodes && !(r->rule_flag & PFRULE_RULESRCTRACK)) {
3624 yyerror("max-src-nodes requires 'source-track rule'");
3627 if (r->action == PF_DROP && r->keep_state) {
3628 yyerror("keep state on block rules doesn't make sense");
3631 if ((r->tagname[0] || r->match_tagname[0]) && !r->keep_state &&
3632 r->action == PF_PASS && !r->anchorname[0]) {
3633 yyerror("tags cannot be used without keep state");
3640 nat_consistent(struct pf_rule *r __unused)
3642 return (0); /* yeah! */
3646 rdr_consistent(struct pf_rule *r)
3650 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP) {
3651 if (r->src.port_op) {
3652 yyerror("src port only applies to tcp/udp");
3655 if (r->dst.port_op) {
3656 yyerror("dst port only applies to tcp/udp");
3659 if (r->rpool.proxy_port[0]) {
3660 yyerror("rpool port only applies to tcp/udp");
3664 if (r->dst.port_op &&
3665 r->dst.port_op != PF_OP_EQ && r->dst.port_op != PF_OP_RRG) {
3666 yyerror("invalid port operator for rdr destination port");
3673 process_tabledef(char *name, struct table_opts *opts)
3675 struct pfr_buffer ab;
3676 struct node_tinit *ti;
3678 bzero(&ab, sizeof(ab));
3679 ab.pfrb_type = PFRB_ADDRS;
3680 SIMPLEQ_FOREACH(ti, &opts->init_nodes, entries) {
3682 if (pfr_buf_load(&ab, ti->file, 0, append_addr)) {
3684 yyerror("cannot load \"%s\": %s",
3685 ti->file, strerror(errno));
3687 yyerror("file \"%s\" contains bad data",
3692 if (append_addr_host(&ab, ti->host, 0, 0)) {
3693 yyerror("cannot create address buffer: %s",
3698 if (pf->opts & PF_OPT_VERBOSE)
3699 print_tabledef(name, opts->flags, opts->init_addr,
3701 if (!(pf->opts & PF_OPT_NOACTION) &&
3702 pfctl_define_table(name, opts->flags, opts->init_addr,
3703 pf->anchor, pf->ruleset, &ab, pf->tticket)) {
3704 yyerror("cannot define table %s: %s", name,
3705 pfr_strerror(errno));
3721 /* macro gore, but you should've seen the prior indentation nightmare... */
3723 #define FREE_LIST(T,r) \
3726 while (node != NULL) { \
3728 node = node->next; \
3733 #define LOOP_THROUGH(T,n,r,C) \
3737 r = calloc(1, sizeof(T)); \
3739 err(1, "LOOP: calloc"); \
3743 while (n != NULL) { \
3752 expand_label_str(char *label, size_t len, const char *srch, const char *repl)
3757 if ((tmp = calloc(1, len)) == NULL)
3758 err(1, "expand_label_str: calloc");
3760 while ((q = strstr(p, srch)) != NULL) {
3762 if ((strlcat(tmp, p, len) >= len) ||
3763 (strlcat(tmp, repl, len) >= len))
3764 errx(1, "expand_label: label too long");
3768 if (strlcat(tmp, p, len) >= len)
3769 errx(1, "expand_label: label too long");
3770 strlcpy(label, tmp, len); /* always fits */
3775 expand_label_if(const char *name, char *label, size_t len, const char *ifname)
3777 if (strstr(label, name) != NULL) {
3779 expand_label_str(label, len, name, "any");
3781 expand_label_str(label, len, name, ifname);
3786 expand_label_addr(const char *name, char *label, size_t len, sa_family_t af,
3787 struct node_host *h)
3789 char tmp[64], tmp_not[66];
3791 if (strstr(label, name) != NULL) {
3792 switch (h->addr.type) {
3793 case PF_ADDR_DYNIFTL:
3794 snprintf(tmp, sizeof(tmp), "(%s)", h->addr.v.ifname);
3797 snprintf(tmp, sizeof(tmp), "<%s>", h->addr.v.tblname);
3799 case PF_ADDR_NOROUTE:
3800 snprintf(tmp, sizeof(tmp), "no-route");
3802 case PF_ADDR_ADDRMASK:
3803 if (!af || (PF_AZERO(&h->addr.v.a.addr, af) &&
3804 PF_AZERO(&h->addr.v.a.mask, af)))
3805 snprintf(tmp, sizeof(tmp), "any");
3810 if (inet_ntop(af, &h->addr.v.a.addr, a,
3812 snprintf(tmp, sizeof(tmp), "?");
3814 bits = unmask(&h->addr.v.a.mask, af);
3815 if ((af == AF_INET && bits < 32) ||
3816 (af == AF_INET6 && bits < 128))
3817 snprintf(tmp, sizeof(tmp),
3820 snprintf(tmp, sizeof(tmp),
3826 snprintf(tmp, sizeof(tmp), "?");
3831 snprintf(tmp_not, sizeof(tmp_not), "! %s", tmp);
3832 expand_label_str(label, len, name, tmp_not);
3834 expand_label_str(label, len, name, tmp);
3839 expand_label_port(const char *name, char *label, size_t len,
3840 struct node_port *port)
3842 char a1[6], a2[6], op[13] = "";
3844 if (strstr(label, name) != NULL) {
3845 snprintf(a1, sizeof(a1), "%u", ntohs(port->port[0]));
3846 snprintf(a2, sizeof(a2), "%u", ntohs(port->port[1]));
3849 else if (port->op == PF_OP_IRG)
3850 snprintf(op, sizeof(op), "%s><%s", a1, a2);
3851 else if (port->op == PF_OP_XRG)
3852 snprintf(op, sizeof(op), "%s<>%s", a1, a2);
3853 else if (port->op == PF_OP_EQ)
3854 snprintf(op, sizeof(op), "%s", a1);
3855 else if (port->op == PF_OP_NE)
3856 snprintf(op, sizeof(op), "!=%s", a1);
3857 else if (port->op == PF_OP_LT)
3858 snprintf(op, sizeof(op), "<%s", a1);
3859 else if (port->op == PF_OP_LE)
3860 snprintf(op, sizeof(op), "<=%s", a1);
3861 else if (port->op == PF_OP_GT)
3862 snprintf(op, sizeof(op), ">%s", a1);
3863 else if (port->op == PF_OP_GE)
3864 snprintf(op, sizeof(op), ">=%s", a1);
3865 expand_label_str(label, len, name, op);
3870 expand_label_proto(const char *name, char *label, size_t len, u_int8_t proto)
3872 struct protoent *pe;
3875 if (strstr(label, name) != NULL) {
3876 pe = getprotobynumber(proto);
3878 expand_label_str(label, len, name, pe->p_name);
3880 snprintf(n, sizeof(n), "%u", proto);
3881 expand_label_str(label, len, name, n);
3887 expand_label_nr(const char *name, char *label, size_t len)
3891 if (strstr(label, name) != NULL) {
3892 snprintf(n, sizeof(n), "%u", pf->rule_nr);
3893 expand_label_str(label, len, name, n);
3898 expand_label(char *label, size_t len, const char *ifname, sa_family_t af,
3899 struct node_host *src_host, struct node_port *src_port,
3900 struct node_host *dst_host, struct node_port *dst_port,
3903 expand_label_if("$if", label, len, ifname);
3904 expand_label_addr("$srcaddr", label, len, af, src_host);
3905 expand_label_addr("$dstaddr", label, len, af, dst_host);
3906 expand_label_port("$srcport", label, len, src_port);
3907 expand_label_port("$dstport", label, len, dst_port);
3908 expand_label_proto("$proto", label, len, proto);
3909 expand_label_nr("$nr", label, len);
3913 expand_altq(struct pf_altq *a, struct node_if *interfaces,
3914 struct node_queue *nqueues, struct node_queue_bw bwspec,
3915 struct node_queue_opt *opts)
3917 struct pf_altq pa, pb;
3918 char qname[PF_QNAME_SIZE];
3919 struct node_queue *n;
3920 struct node_queue_bw bw;
3923 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
3924 FREE_LIST(struct node_if, interfaces);
3925 FREE_LIST(struct node_queue, nqueues);
3929 LOOP_THROUGH(struct node_if, interface, interfaces,
3930 memcpy(&pa, a, sizeof(struct pf_altq));
3931 if (strlcpy(pa.ifname, interface->ifname,
3932 sizeof(pa.ifname)) >= sizeof(pa.ifname))
3933 errx(1, "expand_altq: strlcpy");
3935 if (interface->not) {
3936 yyerror("altq on ! <interface> is not supported");
3939 if (eval_pfaltq(pf, &pa, &bwspec, opts))
3942 if (pfctl_add_altq(pf, &pa))
3945 if (pf->opts & PF_OPT_VERBOSE) {
3946 print_altq(&pf->paltq->altq, 0,
3948 if (nqueues && nqueues->tail) {
3950 LOOP_THROUGH(struct node_queue, queue,
3960 if (pa.scheduler == ALTQT_CBQ ||
3961 pa.scheduler == ALTQT_HFSC) {
3962 /* now create a root queue */
3963 memset(&pb, 0, sizeof(struct pf_altq));
3964 if (strlcpy(qname, "root_", sizeof(qname)) >=
3966 errx(1, "expand_altq: strlcpy");
3967 if (strlcat(qname, interface->ifname,
3968 sizeof(qname)) >= sizeof(qname))
3969 errx(1, "expand_altq: strlcat");
3970 if (strlcpy(pb.qname, qname,
3971 sizeof(pb.qname)) >= sizeof(pb.qname))
3972 errx(1, "expand_altq: strlcpy");
3973 if (strlcpy(pb.ifname, interface->ifname,
3974 sizeof(pb.ifname)) >= sizeof(pb.ifname))
3975 errx(1, "expand_altq: strlcpy");
3976 pb.qlimit = pa.qlimit;
3977 pb.scheduler = pa.scheduler;
3978 bw.bw_absolute = pa.ifbandwidth;
3980 if (eval_pfqueue(pf, &pb, &bw, opts))
3983 if (pfctl_add_altq(pf, &pb))
3987 LOOP_THROUGH(struct node_queue, queue, nqueues,
3988 n = calloc(1, sizeof(struct node_queue));
3990 err(1, "expand_altq: calloc");
3991 if (pa.scheduler == ALTQT_CBQ ||
3992 pa.scheduler == ALTQT_HFSC /*||
3993 pa.scheduler == ALTQT_FAIRQ*/)
3994 if (strlcpy(n->parent, qname,
3995 sizeof(n->parent)) >=
3997 errx(1, "expand_altq: strlcpy");
3998 if (strlcpy(n->queue, queue->queue,
3999 sizeof(n->queue)) >= sizeof(n->queue))
4000 errx(1, "expand_altq: strlcpy");
4001 if (strlcpy(n->ifname, interface->ifname,
4002 sizeof(n->ifname)) >= sizeof(n->ifname))
4003 errx(1, "expand_altq: strlcpy");
4004 n->scheduler = pa.scheduler;
4010 queues->tail->next = n;
4016 FREE_LIST(struct node_if, interfaces);
4017 FREE_LIST(struct node_queue, nqueues);
4023 expand_queue(struct pf_altq *a, struct node_if *interfaces,
4024 struct node_queue *nqueues, struct node_queue_bw bwspec,
4025 struct node_queue_opt *opts)
4027 struct node_queue *n, *nq;
4032 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
4033 FREE_LIST(struct node_queue, nqueues);
4037 if (queues == NULL) {
4038 yyerror("queue %s has no parent", a->qname);
4039 FREE_LIST(struct node_queue, nqueues);
4043 LOOP_THROUGH(struct node_if, interface, interfaces,
4044 LOOP_THROUGH(struct node_queue, tqueue, queues,
4045 if (!strncmp(a->qname, tqueue->queue, PF_QNAME_SIZE) &&
4046 (interface->ifname[0] == 0 ||
4047 (!interface->not && !strncmp(interface->ifname,
4048 tqueue->ifname, IFNAMSIZ)) ||
4049 (interface->not && strncmp(interface->ifname,
4050 tqueue->ifname, IFNAMSIZ)))) {
4051 /* found ourself in queues */
4054 memcpy(&pa, a, sizeof(struct pf_altq));
4056 if (pa.scheduler != ALTQT_NONE &&
4057 pa.scheduler != tqueue->scheduler) {
4058 yyerror("exactly one scheduler type "
4059 "per interface allowed");
4062 pa.scheduler = tqueue->scheduler;
4064 /* scheduler dependent error checking */
4065 switch (pa.scheduler) {
4067 if (nqueues != NULL) {
4068 yyerror("priq queues cannot "
4069 "have child queues");
4072 if (bwspec.bw_absolute > 0 ||
4073 bwspec.bw_percent < 100) {
4074 yyerror("priq doesn't take "
4083 if (strlcpy(pa.ifname, tqueue->ifname,
4084 sizeof(pa.ifname)) >= sizeof(pa.ifname))
4085 errx(1, "expand_queue: strlcpy");
4086 if (strlcpy(pa.parent, tqueue->parent,
4087 sizeof(pa.parent)) >= sizeof(pa.parent))
4088 errx(1, "expand_queue: strlcpy");
4090 if (eval_pfqueue(pf, &pa, &bwspec, opts))
4093 if (pfctl_add_altq(pf, &pa))
4096 for (nq = nqueues; nq != NULL; nq = nq->next) {
4097 if (!strcmp(a->qname, nq->queue)) {
4098 yyerror("queue cannot have "
4104 sizeof(struct node_queue));
4106 err(1, "expand_queue: calloc");
4107 if (strlcpy(n->parent, a->qname,
4108 sizeof(n->parent)) >=
4110 errx(1, "expand_queue strlcpy");
4111 if (strlcpy(n->queue, nq->queue,
4112 sizeof(n->queue)) >=
4114 errx(1, "expand_queue strlcpy");
4115 if (strlcpy(n->ifname, tqueue->ifname,
4116 sizeof(n->ifname)) >=
4118 errx(1, "expand_queue strlcpy");
4119 n->scheduler = tqueue->scheduler;
4125 queues->tail->next = n;
4129 if ((pf->opts & PF_OPT_VERBOSE) && (
4130 (found == 1 && interface->ifname[0] == 0) ||
4131 (found > 0 && interface->ifname[0] != 0))) {
4132 print_queue(&pf->paltq->altq, 0,
4133 &bwspec, interface->ifname[0] != 0,
4135 if (nqueues && nqueues->tail) {
4137 LOOP_THROUGH(struct node_queue,
4150 FREE_LIST(struct node_queue, nqueues);
4151 FREE_LIST(struct node_if, interfaces);
4154 yyerror("queue %s has no parent", a->qname);
4165 expand_rule(struct pf_rule *r,
4166 struct node_if *interfaces, struct node_host *rpool_hosts,
4167 struct node_proto *protos, struct node_os *src_oses,
4168 struct node_host *src_hosts, struct node_port *src_ports,
4169 struct node_host *dst_hosts, struct node_port *dst_ports,
4170 struct node_uid *uids, struct node_gid *gids, struct node_icmp *icmp_types)
4172 sa_family_t af = r->af;
4173 int added = 0, error = 0;
4174 char ifname[IF_NAMESIZE];
4175 char label[PF_RULE_LABEL_SIZE];
4176 char tagname[PF_TAG_NAME_SIZE];
4177 char match_tagname[PF_TAG_NAME_SIZE];
4178 struct pf_pooladdr *pa;
4179 struct node_host *h;
4180 u_int8_t flags, flagset, keep_state;
4182 if (strlcpy(label, r->label, sizeof(label)) >= sizeof(label))
4183 errx(1, "expand_rule: strlcpy");
4184 if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname))
4185 errx(1, "expand_rule: strlcpy");
4186 if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >=
4187 sizeof(match_tagname))
4188 errx(1, "expand_rule: strlcpy");
4190 flagset = r->flagset;
4191 keep_state = r->keep_state;
4193 LOOP_THROUGH(struct node_if, interface, interfaces,
4194 LOOP_THROUGH(struct node_proto, proto, protos,
4195 LOOP_THROUGH(struct node_icmp, icmp_type, icmp_types,
4196 LOOP_THROUGH(struct node_host, src_host, src_hosts,
4197 LOOP_THROUGH(struct node_port, src_port, src_ports,
4198 LOOP_THROUGH(struct node_os, src_os, src_oses,
4199 LOOP_THROUGH(struct node_host, dst_host, dst_hosts,
4200 LOOP_THROUGH(struct node_port, dst_port, dst_ports,
4201 LOOP_THROUGH(struct node_uid, uid, uids,
4202 LOOP_THROUGH(struct node_gid, gid, gids,
4205 /* for link-local IPv6 address, interface must match up */
4206 if ((r->af && src_host->af && r->af != src_host->af) ||
4207 (r->af && dst_host->af && r->af != dst_host->af) ||
4208 (src_host->af && dst_host->af &&
4209 src_host->af != dst_host->af) ||
4210 (src_host->ifindex && dst_host->ifindex &&
4211 src_host->ifindex != dst_host->ifindex) ||
4212 (src_host->ifindex && *interface->ifname &&
4213 src_host->ifindex != if_nametoindex(interface->ifname)) ||
4214 (dst_host->ifindex && *interface->ifname &&
4215 dst_host->ifindex != if_nametoindex(interface->ifname)))
4217 if (!r->af && src_host->af)
4218 r->af = src_host->af;
4219 else if (!r->af && dst_host->af)
4220 r->af = dst_host->af;
4222 if (*interface->ifname)
4223 memcpy(r->ifname, interface->ifname, sizeof(r->ifname));
4224 else if (if_indextoname(src_host->ifindex, ifname))
4225 memcpy(r->ifname, ifname, sizeof(r->ifname));
4226 else if (if_indextoname(dst_host->ifindex, ifname))
4227 memcpy(r->ifname, ifname, sizeof(r->ifname));
4229 memset(r->ifname, '\0', sizeof(r->ifname));
4231 if (strlcpy(r->label, label, sizeof(r->label)) >=
4233 errx(1, "expand_rule: strlcpy");
4234 if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >=
4236 errx(1, "expand_rule: strlcpy");
4237 if (strlcpy(r->match_tagname, match_tagname,
4238 sizeof(r->match_tagname)) >= sizeof(r->match_tagname))
4239 errx(1, "expand_rule: strlcpy");
4240 expand_label(r->label, PF_RULE_LABEL_SIZE, r->ifname, r->af,
4241 src_host, src_port, dst_host, dst_port, proto->proto);
4242 expand_label(r->tagname, PF_TAG_NAME_SIZE, r->ifname, r->af,
4243 src_host, src_port, dst_host, dst_port, proto->proto);
4244 expand_label(r->match_tagname, PF_TAG_NAME_SIZE, r->ifname,
4245 r->af, src_host, src_port, dst_host, dst_port,
4248 error += check_netmask(src_host, r->af);
4249 error += check_netmask(dst_host, r->af);
4251 r->ifnot = interface->not;
4252 r->proto = proto->proto;
4253 r->src.addr = src_host->addr;
4254 r->src.not = src_host->not;
4255 r->src.port[0] = src_port->port[0];
4256 r->src.port[1] = src_port->port[1];
4257 r->src.port_op = src_port->op;
4258 r->dst.addr = dst_host->addr;
4259 r->dst.not = dst_host->not;
4260 r->dst.port[0] = dst_port->port[0];
4261 r->dst.port[1] = dst_port->port[1];
4262 r->dst.port_op = dst_port->op;
4263 r->uid.op = uid->op;
4264 r->uid.uid[0] = uid->uid[0];
4265 r->uid.uid[1] = uid->uid[1];
4266 r->gid.op = gid->op;
4267 r->gid.gid[0] = gid->gid[0];
4268 r->gid.gid[1] = gid->gid[1];
4269 r->type = icmp_type->type;
4270 r->code = icmp_type->code;
4272 if ((keep_state == PF_STATE_MODULATE ||
4273 keep_state == PF_STATE_SYNPROXY) &&
4274 r->proto && r->proto != IPPROTO_TCP)
4275 r->keep_state = PF_STATE_NORMAL;
4277 r->keep_state = keep_state;
4279 if (r->proto && r->proto != IPPROTO_TCP) {
4284 r->flagset = flagset;
4286 if (icmp_type->proto && r->proto != icmp_type->proto) {
4287 yyerror("icmp-type mismatch");
4291 if (src_os && src_os->os) {
4292 r->os_fingerprint = pfctl_get_fingerprint(src_os->os);
4293 if ((pf->opts & PF_OPT_VERBOSE2) &&
4294 r->os_fingerprint == PF_OSFP_NOMATCH)
4296 "warning: unknown '%s' OS fingerprint\n",
4299 r->os_fingerprint = PF_OSFP_ANY;
4302 TAILQ_INIT(&r->rpool.list);
4303 for (h = rpool_hosts; h != NULL; h = h->next) {
4304 pa = calloc(1, sizeof(struct pf_pooladdr));
4306 err(1, "expand_rule: calloc");
4308 if (h->ifname != NULL) {
4309 if (strlcpy(pa->ifname, h->ifname,
4310 sizeof(pa->ifname)) >=
4312 errx(1, "expand_rule: strlcpy");
4315 TAILQ_INSERT_TAIL(&r->rpool.list, pa, entries);
4318 if (rule_consistent(r) < 0 || error)
4319 yyerror("skipping rule due to errors");
4321 r->nr = pf->rule_nr++;
4322 pfctl_add_rule(pf, r);
4328 FREE_LIST(struct node_if, interfaces);
4329 FREE_LIST(struct node_proto, protos);
4330 FREE_LIST(struct node_host, src_hosts);
4331 FREE_LIST(struct node_port, src_ports);
4332 FREE_LIST(struct node_os, src_oses);
4333 FREE_LIST(struct node_host, dst_hosts);
4334 FREE_LIST(struct node_port, dst_ports);
4335 FREE_LIST(struct node_uid, uids);
4336 FREE_LIST(struct node_gid, gids);
4337 FREE_LIST(struct node_icmp, icmp_types);
4338 FREE_LIST(struct node_host, rpool_hosts);
4341 yyerror("rule expands to no valid combination");
4348 check_rulestate(int desired_state)
4350 if (require_order && (rulestate > desired_state)) {
4351 yyerror("Rules must be in order: options, normalization, "
4352 "queueing, translation, filtering");
4355 rulestate = desired_state;
4360 kw_cmp(const void *k, const void *e)
4362 return (strcmp(k, ((const struct keywords *)e)->k_name));
4368 /* this has to be sorted always */
4369 static const struct keywords keywords[] = {
4371 { "allow-opts", ALLOWOPTS},
4373 { "anchor", ANCHOR},
4374 { "antispoof", ANTISPOOF},
4376 { "bandwidth", BANDWIDTH},
4378 { "binat-anchor", BINATANCHOR},
4379 { "bitmask", BITMASK},
4381 { "block-policy", BLOCKPOLICY},
4382 { "buckets", BUCKETS},
4385 { "crop", FRAGCROP},
4388 { "drop-ovl", FRAGDROP},
4391 { "fastroute", FASTROUTE},
4392 { "file", FILENAME},
4393 { "fingerprints", FINGERPRINTS},
4395 { "floating", FLOATING},
4397 { "fragment", FRAGMENT},
4399 { "global", GLOBAL},
4401 { "group-bound", GRBOUND},
4404 { "hostid", HOSTID},
4405 { "icmp-type", ICMPTYPE},
4406 { "icmp6-type", ICMP6TYPE},
4407 { "if-bound", IFBOUND},
4414 { "linkshare", LINKSHARE},
4417 { "log-all", LOGALL},
4418 { "loginterface", LOGINTERFACE},
4420 { "max-mss", MAXMSS},
4421 { "max-src-nodes", MAXSRCNODES},
4422 { "max-src-states", MAXSRCSTATES},
4423 { "min-ttl", MINTTL},
4424 { "modulate", MODULATE},
4426 { "nat-anchor", NATANCHOR},
4429 { "no-route", NOROUTE},
4430 { "no-sync", NOSYNC},
4432 { "optimization", OPTIMIZATION},
4437 { "priority", PRIORITY},
4440 { "qlimit", QLIMIT},
4443 { "random", RANDOM},
4444 { "random-id", RANDOMID},
4446 { "rdr-anchor", RDRANCHOR},
4447 { "realtime", REALTIME},
4448 { "reassemble", REASSEMBLE},
4449 { "reply-to", REPLYTO},
4450 { "require-order", REQUIREORDER},
4451 { "return", RETURN},
4452 { "return-icmp", RETURNICMP},
4453 { "return-icmp6", RETURNICMP6},
4454 { "return-rst", RETURNRST},
4455 { "round-robin", ROUNDROBIN},
4456 { "route-to", ROUTETO},
4460 { "source-hash", SOURCEHASH},
4461 { "source-track", SOURCETRACK},
4463 { "state-policy", STATEPOLICY},
4464 { "static-port", STATICPORT},
4465 { "sticky-address", STICKYADDRESS},
4466 { "synproxy", SYNPROXY},
4469 { "tagged", TAGGED},
4470 { "tbrsize", TBRSIZE},
4471 { "timeout", TIMEOUT},
4475 { "upperlimit", UPPERLIMIT},
4478 const struct keywords *p;
4480 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
4481 sizeof(keywords[0]), kw_cmp);
4485 fprintf(stderr, "%s: %d\n", s, p->k_val);
4489 fprintf(stderr, "string: %s\n", s);
4494 #define MAXPUSHBACK 128
4498 char pushback_buffer[MAXPUSHBACK];
4499 int pushback_index = 0;
4507 /* Read character from the parsebuffer instead of input. */
4508 if (parseindex >= 0) {
4509 c = parsebuf[parseindex++];
4518 return (pushback_buffer[--pushback_index]);
4520 while ((c = getc(f)) == '\\') {
4524 yyerror("whitespace after \\");
4528 yylval.lineno = lineno;
4531 if (c == '\t' || c == ' ') {
4532 /* Compress blanks to a single space. */
4535 } while (c == '\t' || c == ' ');
4550 if (parseindex >= 0)
4553 if (pushback_index < MAXPUSHBACK-1)
4554 return (pushback_buffer[pushback_index++] = c);
4567 /* skip to either EOF or the first real EOL */
4590 while ((c = lgetc(fin)) == ' ')
4593 yylval.lineno = lineno;
4595 while ((c = lgetc(fin)) != '\n' && c != EOF)
4597 if (c == '$' && parsebuf == NULL) {
4599 if ((c = lgetc(fin)) == EOF)
4602 if (p + 1 >= buf + sizeof(buf) - 1) {
4603 yyerror("string too long");
4606 if (isalnum(c) || c == '_') {
4616 yyerror("macro '%s' not defined", buf);
4629 if ((c = lgetc(fin)) == EOF)
4639 if (p + 1 >= buf + sizeof(buf) - 1) {
4640 yyerror("string too long");
4645 yylval.v.string = strdup(buf);
4646 if (yylval.v.string == NULL)
4647 err(1, "yylex: strdup");
4652 yylval.v.i = PF_OP_XRG;
4653 return (PORTBINARY);
4660 yylval.v.i = PF_OP_IRG;
4661 return (PORTBINARY);
4673 #define allowed_in_string(x) \
4674 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
4675 x != '{' && x != '}' && x != '<' && x != '>' && \
4676 x != '!' && x != '=' && x != '/' && x != '#' && \
4679 if (isalnum(c) || c == ':' || c == '_') {
4682 if ((unsigned)(p-buf) >= sizeof(buf)) {
4683 yyerror("string too long");
4686 } while ((c = lgetc(fin)) != EOF && (allowed_in_string(c)));
4689 if ((token = lookup(buf)) == STRING)
4690 if ((yylval.v.string = strdup(buf)) == NULL)
4691 err(1, "yylex: strdup");
4695 yylval.lineno = lineno;
4704 parse_rules(FILE *input, struct pfctl *xpf)
4706 struct sym *sym, *next;
4712 rulestate = PFCTL_STATE_NONE;
4713 returnicmpdefault = (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
4714 returnicmp6default =
4715 (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
4716 blockpolicy = PFRULE_DROP;
4721 /* Free macros and check which have not been used. */
4722 for (sym = TAILQ_FIRST(&symhead); sym != NULL; sym = next) {
4723 next = TAILQ_NEXT(sym, entries);
4724 if ((pf->opts & PF_OPT_VERBOSE2) && !sym->used)
4725 fprintf(stderr, "warning: macro '%s' not "
4726 "used\n", sym->nam);
4729 TAILQ_REMOVE(&symhead, sym, entries);
4733 return (errors ? -1 : 0);
4737 * Over-designed efficiency is a French and German concept, so how about
4738 * we wait until they discover this ugliness and make it all fancy.
4741 symset(const char *nam, const char *val, int persist)
4745 for (sym = TAILQ_FIRST(&symhead); sym && strcmp(nam, sym->nam);
4746 sym = TAILQ_NEXT(sym, entries))
4750 if (sym->persist == 1)
4755 TAILQ_REMOVE(&symhead, sym, entries);
4759 if ((sym = calloc(1, sizeof(*sym))) == NULL)
4762 sym->nam = strdup(nam);
4763 if (sym->nam == NULL) {
4767 sym->val = strdup(val);
4768 if (sym->val == NULL) {
4774 sym->persist = persist;
4775 TAILQ_INSERT_TAIL(&symhead, sym, entries);
4780 pfctl_cmdline_symset(char *s)
4785 if ((val = strrchr(s, '=')) == NULL)
4788 if ((sym = malloc(strlen(s) - strlen(val) + 1)) == NULL)
4789 err(1, "pfctl_cmdline_symset: malloc");
4791 strlcpy(sym, s, strlen(s) - strlen(val) + 1);
4793 ret = symset(sym, val + 1, 1);
4800 symget(const char *nam)
4804 TAILQ_FOREACH(sym, &symhead, entries)
4805 if (strcmp(nam, sym->nam) == 0) {
4813 decide_address_family(struct node_host *n, sa_family_t *af)
4815 sa_family_t target_af = 0;
4817 while (!*af && n != NULL) {
4821 if (target_af != n->af)
4826 if (!*af && target_af)
4831 remove_invalid_hosts(struct node_host **nh, sa_family_t *af)
4833 struct node_host *n = *nh, *prev = NULL;
4836 if (*af && n->af && n->af != *af) {
4837 /* unlink and free n */
4838 struct node_host *next = n->next;
4840 /* adjust tail pointer */
4841 if (n == (*nh)->tail)
4843 /* adjust previous node's next pointer */
4849 if (n->ifname != NULL)
4863 invalid_redirect(struct node_host *nh, sa_family_t af)
4866 struct node_host *n;
4868 /* tables and dyniftl are ok without an address family */
4869 for (n = nh; n != NULL; n = n->next) {
4870 if (n->addr.type != PF_ADDR_TABLE &&
4871 n->addr.type != PF_ADDR_DYNIFTL) {
4872 yyerror("address family not given and "
4873 "translation address expands to multiple "
4874 "address families");
4880 yyerror("no translation address with matching address family "
4888 atoul(char *s, u_long *ulvalp)
4894 ulval = strtoul(s, &ep, 0);
4895 if (s[0] == '\0' || *ep != '\0')
4897 if (errno == ERANGE && ulval == ULONG_MAX)
4909 if (atoul(n, &ulval) == 0) {
4910 if (ulval > 65535) {
4911 yyerror("illegal port value %d", ulval);
4914 return (htons(ulval));
4916 s = getservbyname(n, "tcp");
4918 s = getservbyname(n, "udp");
4920 yyerror("unknown port %s", n);
4928 rule_label(struct pf_rule *r, char *s)
4931 if (strlcpy(r->label, s, sizeof(r->label)) >=
4933 yyerror("rule label too long (max %d chars)",
4934 sizeof(r->label)-1);
4942 parseicmpspec(char *w, sa_family_t af)
4944 const struct icmpcodeent *p;
4949 icmptype = returnicmpdefault >> 8;
4951 icmptype = returnicmp6default >> 8;
4953 if (atoul(w, &ulval) == -1) {
4954 if ((p = geticmpcodebyname(icmptype, w, af)) == NULL) {
4955 yyerror("unknown icmp code %s", w);
4961 yyerror("invalid icmp code %ld", ulval);
4964 return (icmptype << 8 | ulval);
4968 pfctl_load_anchors(int dev, int opts, struct pfr_buffer *trans)
4970 struct loadanchors *la;
4972 TAILQ_FOREACH(la, &loadanchorshead, entries) {
4973 if (opts & PF_OPT_VERBOSE)
4974 fprintf(stderr, "\nLoading anchor %s:%s from %s\n",
4975 la->anchorname, la->rulesetname, la->filename);
4976 if (pfctl_rules(dev, la->filename, opts, la->anchorname,
4977 la->rulesetname, trans) == -1)