1 /* $OpenBSD: parse.y,v 1.517 2007/02/03 23:26:40 dhartmei 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>
36 #include <netinet/in.h>
37 #include <netinet/in_systm.h>
38 #include <netinet/ip.h>
39 #include <netinet/ip_icmp.h>
40 #include <netinet/icmp6.h>
41 #include <net/pfvar.h>
42 #include <arpa/inet.h>
43 #include <altq/altq.h>
44 #include <altq/altq_cbq.h>
45 #include <altq/altq_priq.h>
46 #include <altq/altq_hfsc.h>
62 #include "pfctl_parser.h"
66 #define HTONL(x) (x) = htonl((__uint32_t)(x))
69 static struct pfctl *pf = NULL;
70 static FILE *fin = NULL;
72 static int lineno = 1;
73 static int errors = 0;
74 static int rulestate = 0;
75 static u_int16_t returnicmpdefault =
76 (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
77 static u_int16_t returnicmp6default =
78 (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
79 static int blockpolicy = PFRULE_DROP;
80 static int require_order = 1;
81 static int default_statelock;
94 struct node_proto *next;
95 struct node_proto *tail;
101 struct node_port *next;
102 struct node_port *tail;
108 struct node_uid *next;
109 struct node_uid *tail;
115 struct node_gid *next;
116 struct node_gid *tail;
123 struct node_icmp *next;
124 struct node_icmp *tail;
127 enum { PF_STATE_OPT_MAX, PF_STATE_OPT_NOSYNC, PF_STATE_OPT_SRCTRACK,
128 PF_STATE_OPT_MAX_SRC_STATES, PF_STATE_OPT_MAX_SRC_CONN,
129 PF_STATE_OPT_MAX_SRC_CONN_RATE, PF_STATE_OPT_MAX_SRC_NODES,
130 PF_STATE_OPT_OVERLOAD, PF_STATE_OPT_STATELOCK,
131 PF_STATE_OPT_TIMEOUT, PF_STATE_OPT_SLOPPY };
133 enum { PF_SRCTRACK_NONE, PF_SRCTRACK, PF_SRCTRACK_GLOBAL, PF_SRCTRACK_RULE };
135 struct node_state_opt {
138 u_int32_t max_states;
139 u_int32_t max_src_states;
140 u_int32_t max_src_conn;
147 char tblname[PF_TABLE_NAME_SIZE];
149 u_int32_t max_src_nodes;
157 struct node_state_opt *next;
158 struct node_state_opt *tail;
162 struct node_host *host;
163 struct node_port *port;
167 char queue[PF_QNAME_SIZE];
168 char parent[PF_QNAME_SIZE];
169 char ifname[IFNAMSIZ];
171 struct node_queue *next;
172 struct node_queue *tail;
175 struct node_qassign {
182 #define FOM_FLAGS 0x01
183 #define FOM_ICMP 0x02
185 #define FOM_KEEP 0x08
186 #define FOM_SRCTRACK 0x10
187 struct node_uid *uid;
188 struct node_gid *gid;
195 struct node_icmp *icmpspec;
200 struct node_state_opt *options;
205 struct node_qassign queues;
208 u_int8_t match_tag_not;
212 struct antispoof_opts {
219 #define SOM_MINTTL 0x01
220 #define SOM_MAXMSS 0x02
221 #define SOM_FRAGCACHE 0x04
233 #define QOM_BWSPEC 0x01
234 #define QOM_SCHEDULER 0x02
235 #define QOM_PRIORITY 0x04
236 #define QOM_TBRSIZE 0x08
237 #define QOM_QLIMIT 0x10
238 struct node_queue_bw queue_bwspec;
239 struct node_queue_opt scheduler;
248 struct node_tinithead init_nodes;
253 #define POM_TYPE 0x01
254 #define POM_STICKYADDRESS 0x02
258 struct pf_poolhashkey *key;
263 struct node_hfsc_opts hfsc_opts;
265 int yyerror(const char *, ...);
266 int disallow_table(struct node_host *, const char *);
267 int disallow_urpf_failed(struct node_host *, const char *);
268 int disallow_alias(struct node_host *, const char *);
269 int rule_consistent(struct pf_rule *, int);
270 int filter_consistent(struct pf_rule *, int);
271 int nat_consistent(struct pf_rule *);
272 int rdr_consistent(struct pf_rule *);
273 int process_tabledef(char *, struct table_opts *);
275 void expand_label_str(char *, size_t, const char *, const char *);
276 void expand_label_if(const char *, char *, size_t, const char *);
277 void expand_label_addr(const char *, char *, size_t, u_int8_t,
279 void expand_label_port(const char *, char *, size_t, struct node_port *);
280 void expand_label_proto(const char *, char *, size_t, u_int8_t);
281 void expand_label_nr(const char *, char *, size_t);
282 void expand_label(char *, size_t, const char *, u_int8_t, struct node_host *,
283 struct node_port *, struct node_host *, struct node_port *,
285 void expand_rule(struct pf_rule *, struct node_if *, struct node_host *,
286 struct node_proto *, struct node_os*, struct node_host *,
287 struct node_port *, struct node_host *, struct node_port *,
288 struct node_uid *, struct node_gid *, struct node_icmp *,
290 int expand_altq(struct pf_altq *, struct node_if *, struct node_queue *,
291 struct node_queue_bw bwspec, struct node_queue_opt *);
292 int expand_queue(struct pf_altq *, struct node_if *, struct node_queue *,
293 struct node_queue_bw, struct node_queue_opt *);
294 int expand_skip_interface(struct node_if *);
296 int check_rulestate(int);
297 int kw_cmp(const void *, const void *);
303 int atoul(char *, u_long *);
304 int getservice(char *);
305 int rule_label(struct pf_rule *, char *);
307 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
309 TAILQ_ENTRY(sym) entries;
317 int symset(const char *, const char *, int);
318 char *symget(const char *);
320 void mv_rules(struct pf_ruleset *, struct pf_ruleset *);
321 void decide_address_family(struct node_host *, sa_family_t *);
322 void remove_invalid_hosts(struct node_host **, sa_family_t *);
323 int invalid_redirect(struct node_host *, sa_family_t);
324 u_int16_t parseicmpspec(char *, sa_family_t);
326 TAILQ_HEAD(loadanchorshead, loadanchors)
327 loadanchorshead = TAILQ_HEAD_INITIALIZER(loadanchorshead);
330 TAILQ_ENTRY(loadanchors) entries;
352 struct node_if *interface;
353 struct node_proto *proto;
354 struct node_icmp *icmp;
355 struct node_host *host;
357 struct node_port *port;
358 struct node_uid *uid;
359 struct node_gid *gid;
360 struct node_state_opt *state_opt;
363 struct peer src, dst;
364 struct node_os *src_os;
367 struct node_host *host;
371 struct pf_poolhashkey *key;
374 struct node_host *host;
379 struct node_state_opt *options;
390 struct pf_poolhashkey *hashkey;
391 struct node_queue *queue;
392 struct node_queue_opt queue_options;
393 struct node_queue_bw queue_bwspec;
394 struct node_qassign qassign;
395 struct filter_opts filter_opts;
396 struct antispoof_opts antispoof_opts;
397 struct queue_opts queue_opts;
398 struct scrub_opts scrub_opts;
399 struct table_opts table_opts;
400 struct pool_opts pool_opts;
401 struct node_hfsc_opts hfsc_opts;
406 #define DYNIF_MULTIADDR(addr) ((addr).type == PF_ADDR_DYNIFTL && \
407 (!((addr).iflags & PFI_AFLAG_NOALIAS) || \
408 !isdigit((addr).v.ifname[strlen((addr).v.ifname)-1])))
412 %token PASS BLOCK SCRUB RETURN IN OS OUT LOG QUICK ON FROM TO FLAGS
413 %token RETURNRST RETURNICMP RETURNICMP6 PROTO INET INET6 ALL ANY ICMPTYPE
414 %token ICMP6TYPE CODE KEEP MODULATE STATE PORT RDR NAT BINAT ARROW NODF
415 %token MINTTL ERROR ALLOWOPTS FASTROUTE FILENAME ROUTETO DUPTO REPLYTO NO LABEL
416 %token NOROUTE URPFFAILED FRAGMENT USER GROUP MAXMSS MAXIMUM TTL TOS DROP TABLE
417 %token REASSEMBLE FRAGDROP FRAGCROP ANCHOR NATANCHOR RDRANCHOR BINATANCHOR
418 %token SET OPTIMIZATION TIMEOUT LIMIT LOGINTERFACE BLOCKPOLICY RANDOMID
419 %token REQUIREORDER SYNPROXY FINGERPRINTS NOSYNC DEBUG SKIP HOSTID
421 %token BITMASK RANDOM SOURCEHASH ROUNDROBIN STATICPORT PROBABILITY
422 %token ALTQ CBQ PRIQ HFSC BANDWIDTH TBRSIZE LINKSHARE REALTIME UPPERLIMIT
423 %token QUEUE PRIORITY QLIMIT RTABLE
424 %token LOAD RULESET_OPTIMIZATION
425 %token STICKYADDRESS MAXSRCSTATES MAXSRCNODES SOURCETRACK GLOBAL RULE
426 %token MAXSRCCONN MAXSRCCONNRATE OVERLOAD FLUSH SLOPPY
427 %token TAGGED TAG IFBOUND FLOATING STATEPOLICY ROUTE
428 %token <v.string> STRING
429 %token <v.i> PORTBINARY
430 %type <v.interface> interface if_list if_item_not if_item
431 %type <v.number> number icmptype icmp6type uid gid
432 %type <v.number> tos not yesno
433 %type <v.i> no dir af fragcache optimizer
434 %type <v.i> sourcetrack flush unaryop statelock
435 %type <v.b> action nataction natpass scrubaction
436 %type <v.b> flags flag blockspec
437 %type <v.range> port rport
438 %type <v.hashkey> hashkey
439 %type <v.proto> proto proto_list proto_item
440 %type <v.icmp> icmpspec
441 %type <v.icmp> icmp_list icmp_item
442 %type <v.icmp> icmp6_list icmp6_item
443 %type <v.fromto> fromto
444 %type <v.peer> ipportspec from to
445 %type <v.host> ipspec xhost host dynaddr host_list
446 %type <v.host> redir_host_list redirspec
447 %type <v.host> route_host route_host_list routespec
448 %type <v.os> os xos os_list
449 %type <v.port> portspec port_list port_item
450 %type <v.uid> uids uid_list uid_item
451 %type <v.gid> gids gid_list gid_item
452 %type <v.route> route
453 %type <v.redirection> redirection redirpool
454 %type <v.string> label string tag anchorname
455 %type <v.keep_state> keep
456 %type <v.state_opt> state_opt_spec state_opt_list state_opt_item
457 %type <v.logquick> logquick quick log logopts logopt
458 %type <v.interface> antispoof_ifspc antispoof_iflst antispoof_if
459 %type <v.qassign> qname
460 %type <v.queue> qassign qassign_list qassign_item
461 %type <v.queue_options> scheduler
462 %type <v.number> cbqflags_list cbqflags_item
463 %type <v.number> priqflags_list priqflags_item
464 %type <v.hfsc_opts> hfscopts_list hfscopts_item hfsc_opts
465 %type <v.queue_bwspec> bandwidth
466 %type <v.filter_opts> filter_opts filter_opt filter_opts_l
467 %type <v.antispoof_opts> antispoof_opts antispoof_opt antispoof_opts_l
468 %type <v.queue_opts> queue_opts queue_opt queue_opts_l
469 %type <v.scrub_opts> scrub_opts scrub_opt scrub_opts_l
470 %type <v.table_opts> table_opts table_opt table_opts_l
471 %type <v.pool_opts> pool_opts pool_opt pool_opts_l
472 %type <v.tagged> tagged
473 %type <v.rtableid> rtable
476 ruleset : /* empty */
478 | ruleset option '\n'
479 | ruleset scrubrule '\n'
480 | ruleset natrule '\n'
481 | ruleset binatrule '\n'
482 | ruleset pfrule '\n'
483 | ruleset anchorrule '\n'
484 | ruleset loadrule '\n'
485 | ruleset altqif '\n'
486 | ruleset queuespec '\n'
487 | ruleset varset '\n'
488 | ruleset antispoof '\n'
489 | ruleset tabledef '\n'
490 | '{' fakeanchor '}' '\n';
491 | ruleset error '\n' { errors++; }
495 * apply to previouslys specified rule: must be careful to note
496 * what that is: pf or nat or binat or rdr
498 fakeanchor : fakeanchor '\n'
499 | fakeanchor anchorrule '\n'
500 | fakeanchor binatrule '\n'
501 | fakeanchor natrule '\n'
502 | fakeanchor pfrule '\n'
503 | fakeanchor error '\n'
507 if (!strcmp($1, "none"))
509 else if (!strcmp($1, "basic"))
510 $$ = PF_OPTIMIZE_BASIC;
511 else if (!strcmp($1, "profile"))
512 $$ = PF_OPTIMIZE_BASIC | PF_OPTIMIZE_PROFILE;
514 yyerror("unknown ruleset-optimization %s", $$);
520 option : SET OPTIMIZATION STRING {
521 if (check_rulestate(PFCTL_STATE_OPTION)) {
525 if (pfctl_set_optimization(pf, $3) != 0) {
526 yyerror("unknown optimization %s", $3);
532 | SET RULESET_OPTIMIZATION optimizer {
533 if (!(pf->opts & PF_OPT_OPTIMIZE)) {
534 pf->opts |= PF_OPT_OPTIMIZE;
538 | SET TIMEOUT timeout_spec
539 | SET TIMEOUT '{' timeout_list '}'
540 | SET LIMIT limit_spec
541 | SET LIMIT '{' limit_list '}'
542 | SET LOGINTERFACE STRING {
543 if (check_rulestate(PFCTL_STATE_OPTION)) {
547 if (pfctl_set_logif(pf, $3) != 0) {
548 yyerror("error setting loginterface %s", $3);
554 | SET HOSTID number {
556 yyerror("hostid must be non-zero");
559 if (pfctl_set_hostid(pf, $3) != 0) {
560 yyerror("error setting hostid %08x", $3);
564 | SET BLOCKPOLICY DROP {
565 if (pf->opts & PF_OPT_VERBOSE)
566 printf("set block-policy drop\n");
567 if (check_rulestate(PFCTL_STATE_OPTION))
569 blockpolicy = PFRULE_DROP;
571 | SET BLOCKPOLICY RETURN {
572 if (pf->opts & PF_OPT_VERBOSE)
573 printf("set block-policy return\n");
574 if (check_rulestate(PFCTL_STATE_OPTION))
576 blockpolicy = PFRULE_RETURN;
578 | SET REQUIREORDER yesno {
579 if (pf->opts & PF_OPT_VERBOSE)
580 printf("set require-order %s\n",
581 $3 == 1 ? "yes" : "no");
584 | SET FINGERPRINTS STRING {
585 if (pf->opts & PF_OPT_VERBOSE)
586 printf("set fingerprints \"%s\"\n", $3);
587 if (check_rulestate(PFCTL_STATE_OPTION)) {
591 if (!pf->anchor->name[0]) {
592 if (pfctl_file_fingerprints(pf->dev,
594 yyerror("error loading "
595 "fingerprints %s", $3);
602 | SET STATEPOLICY statelock {
603 if (pf->opts & PF_OPT_VERBOSE)
606 printf("set state-policy floating\n");
609 printf("set state-policy if-bound\n");
612 default_statelock = $3;
615 if (check_rulestate(PFCTL_STATE_OPTION)) {
619 if (pfctl_set_debug(pf, $3) != 0) {
620 yyerror("error setting debuglevel %s", $3);
626 | SET SKIP interface {
627 if (expand_skip_interface($3) != 0) {
628 yyerror("error setting skip interface(s)");
634 string : string STRING {
635 if (asprintf(&$$, "%s %s", $1, $2) == -1)
636 err(1, "string: asprintf");
643 varset : STRING '=' string {
644 if (pf->opts & PF_OPT_VERBOSE)
645 printf("%s = \"%s\"\n", $1, $3);
646 if (symset($1, $3, 0) == -1)
647 err(1, "cannot store variable %s", $1);
653 anchorname : STRING { $$ = $1; }
654 | /* empty */ { $$ = NULL; }
661 pfa_anchorlist : pfrule optnl
663 | pfa_anchorlist pfrule optnl
664 | pfa_anchorlist anchorrule optnl
669 char ta[PF_ANCHOR_NAME_SIZE];
670 struct pf_ruleset *rs;
672 /* steping into a brace anchor */
677 /* create a holding ruleset in the root */
678 snprintf(ta, PF_ANCHOR_NAME_SIZE, "_%d", pf->bn);
679 rs = pf_find_or_create_ruleset(ta);
681 err(1, "pfa_anchor: pf_find_or_create_ruleset");
682 pf->astack[pf->asd] = rs->anchor;
683 pf->anchor = rs->anchor;
684 } '\n' pfa_anchorlist '}'
686 pf->alast = pf->anchor;
688 pf->anchor = pf->astack[pf->asd];
693 anchorrule : ANCHOR anchorname dir quick interface af proto fromto
694 filter_opts pfa_anchor
698 if (check_rulestate(PFCTL_STATE_FILTER)) {
704 if ($2 && ($2[0] == '_' || strstr($2, "/_") != NULL)) {
706 yyerror("anchor names beginning with '_' "
707 "are reserved for internal use");
711 memset(&r, 0, sizeof(r));
712 if (pf->astack[pf->asd + 1]) {
713 /* move inline rules into relative location */
715 &pf->astack[pf->asd]->ruleset,
716 $2 ? $2 : pf->alast->name);
718 if (r.anchor == NULL)
719 err(1, "anchorrule: unable to "
722 if (pf->alast != r.anchor) {
723 if (r.anchor->match) {
724 yyerror("inline anchor '%s' "
729 mv_rules(&pf->alast->ruleset,
732 pf_remove_if_empty_ruleset(&pf->alast->ruleset);
733 pf->alast = r.anchor;
736 yyerror("anchors without explicit "
737 "rules must specify a name");
745 r.rtableid = $9.rtableid;
748 if (strlcpy(r.match_tagname, $9.match_tag,
749 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
750 yyerror("tag too long, max %u chars",
751 PF_TAG_NAME_SIZE - 1);
754 r.match_tag_not = $9.match_tag_not;
756 decide_address_family($8.src.host, &r.af);
757 decide_address_family($8.dst.host, &r.af);
759 expand_rule(&r, $5, NULL, $7, $8.src_os,
760 $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
761 0, 0, 0, pf->astack[pf->asd + 1] ?
762 pf->alast->name : $2);
764 pf->astack[pf->asd + 1] = NULL;
766 | NATANCHOR string interface af proto fromto rtable {
769 if (check_rulestate(PFCTL_STATE_NAT)) {
774 memset(&r, 0, sizeof(r));
779 decide_address_family($6.src.host, &r.af);
780 decide_address_family($6.dst.host, &r.af);
782 expand_rule(&r, $3, NULL, $5, $6.src_os,
783 $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
787 | RDRANCHOR string interface af proto fromto rtable {
790 if (check_rulestate(PFCTL_STATE_NAT)) {
795 memset(&r, 0, sizeof(r));
800 decide_address_family($6.src.host, &r.af);
801 decide_address_family($6.dst.host, &r.af);
803 if ($6.src.port != NULL) {
804 yyerror("source port parameter not supported"
808 if ($6.dst.port != NULL) {
809 if ($6.dst.port->next != NULL) {
810 yyerror("destination port list "
811 "expansion not supported in "
814 } else if ($6.dst.port->op != PF_OP_EQ) {
815 yyerror("destination port operators"
816 " not supported in rdr-anchor");
819 r.dst.port[0] = $6.dst.port->port[0];
820 r.dst.port[1] = $6.dst.port->port[1];
821 r.dst.port_op = $6.dst.port->op;
824 expand_rule(&r, $3, NULL, $5, $6.src_os,
825 $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
829 | BINATANCHOR string interface af proto fromto rtable {
832 if (check_rulestate(PFCTL_STATE_NAT)) {
837 memset(&r, 0, sizeof(r));
842 if ($5->next != NULL) {
843 yyerror("proto list expansion"
844 " not supported in binat-anchor");
851 if ($6.src.host != NULL || $6.src.port != NULL ||
852 $6.dst.host != NULL || $6.dst.port != NULL) {
853 yyerror("fromto parameter not supported"
858 decide_address_family($6.src.host, &r.af);
859 decide_address_family($6.dst.host, &r.af);
861 pfctl_add_rule(pf, &r, $2);
866 loadrule : LOAD ANCHOR string FROM string {
867 struct loadanchors *loadanchor;
869 if (strlen(pf->anchor->name) + 1 +
870 strlen($3) >= MAXPATHLEN) {
871 yyerror("anchorname %s too long, max %u\n",
876 loadanchor = calloc(1, sizeof(struct loadanchors));
877 if (loadanchor == NULL)
878 err(1, "loadrule: calloc");
879 if ((loadanchor->anchorname = malloc(MAXPATHLEN)) ==
881 err(1, "loadrule: malloc");
882 if (pf->anchor->name[0])
883 snprintf(loadanchor->anchorname, MAXPATHLEN,
884 "%s/%s", pf->anchor->name, $3);
886 strlcpy(loadanchor->anchorname, $3, MAXPATHLEN);
887 if ((loadanchor->filename = strdup($5)) == NULL)
888 err(1, "loadrule: strdup");
890 TAILQ_INSERT_TAIL(&loadanchorshead, loadanchor,
897 scrubaction : no SCRUB {
906 scrubrule : scrubaction dir logquick interface af proto fromto scrub_opts
910 if (check_rulestate(PFCTL_STATE_SCRUB))
913 memset(&r, 0, sizeof(r));
921 yyerror("scrub rules do not support 'quick'");
927 r.rule_flag |= PFRULE_NODF;
929 r.rule_flag |= PFRULE_RANDOMID;
930 if ($8.reassemble_tcp) {
931 if (r.direction != PF_INOUT) {
932 yyerror("reassemble tcp rules can not "
933 "specify direction");
936 r.rule_flag |= PFRULE_REASSEMBLE_TCP;
939 r.min_ttl = $8.minttl;
941 r.max_mss = $8.maxmss;
943 r.rule_flag |= $8.fragcache;
944 r.rtableid = $8.rtableid;
946 expand_rule(&r, $4, NULL, $6, $7.src_os,
947 $7.src.host, $7.src.port, $7.dst.host, $7.dst.port,
948 NULL, NULL, NULL, "");
953 bzero(&scrub_opts, sizeof scrub_opts);
954 scrub_opts.rtableid = -1;
959 bzero(&scrub_opts, sizeof scrub_opts);
960 scrub_opts.rtableid = -1;
965 scrub_opts_l : scrub_opts_l scrub_opt
970 if (scrub_opts.nodf) {
971 yyerror("no-df cannot be respecified");
977 if (scrub_opts.marker & SOM_MINTTL) {
978 yyerror("min-ttl cannot be respecified");
982 yyerror("illegal min-ttl value %d", $2);
985 scrub_opts.marker |= SOM_MINTTL;
986 scrub_opts.minttl = $2;
989 if (scrub_opts.marker & SOM_MAXMSS) {
990 yyerror("max-mss cannot be respecified");
994 yyerror("illegal max-mss value %d", $2);
997 scrub_opts.marker |= SOM_MAXMSS;
998 scrub_opts.maxmss = $2;
1001 if (scrub_opts.marker & SOM_FRAGCACHE) {
1002 yyerror("fragcache cannot be respecified");
1005 scrub_opts.marker |= SOM_FRAGCACHE;
1006 scrub_opts.fragcache = $1;
1008 | REASSEMBLE STRING {
1009 if (strcasecmp($2, "tcp") != 0) {
1010 yyerror("scrub reassemble supports only tcp, "
1016 if (scrub_opts.reassemble_tcp) {
1017 yyerror("reassemble tcp cannot be respecified");
1020 scrub_opts.reassemble_tcp = 1;
1023 if (scrub_opts.randomid) {
1024 yyerror("random-id cannot be respecified");
1027 scrub_opts.randomid = 1;
1031 if ($2 > RT_TABLEID_MAX || $2 < 0) {
1032 yyerror("invalid rtable id");
1036 scrub_opts.rtableid = $2;
1040 fragcache : FRAGMENT REASSEMBLE { $$ = 0; /* default */ }
1041 | FRAGMENT FRAGCROP { $$ = PFRULE_FRAGCROP; }
1042 | FRAGMENT FRAGDROP { $$ = PFRULE_FRAGDROP; }
1045 antispoof : ANTISPOOF logquick antispoof_ifspc af antispoof_opts {
1047 struct node_host *h = NULL, *hh;
1048 struct node_if *i, *j;
1050 if (check_rulestate(PFCTL_STATE_FILTER))
1053 for (i = $3; i; i = i->next) {
1054 bzero(&r, sizeof(r));
1057 r.direction = PF_IN;
1062 if (rule_label(&r, $5.label))
1064 r.rtableid = $5.rtableid;
1065 j = calloc(1, sizeof(struct node_if));
1067 err(1, "antispoof: calloc");
1068 if (strlcpy(j->ifname, i->ifname,
1069 sizeof(j->ifname)) >= sizeof(j->ifname)) {
1071 yyerror("interface name too long");
1076 h = calloc(1, sizeof(*h));
1078 err(1, "address: calloc");
1079 h->addr.type = PF_ADDR_DYNIFTL;
1081 if (strlcpy(h->addr.v.ifname, i->ifname,
1082 sizeof(h->addr.v.ifname)) >=
1083 sizeof(h->addr.v.ifname)) {
1086 "interface name too long");
1089 hh = malloc(sizeof(*hh));
1091 err(1, "address: malloc");
1092 bcopy(h, hh, sizeof(*hh));
1093 h->addr.iflags = PFI_AFLAG_NETWORK;
1095 h = ifa_lookup(j->ifname,
1101 expand_rule(&r, j, NULL, NULL, NULL, h,
1102 NULL, NULL, NULL, NULL, NULL,
1105 if ((i->ifa_flags & IFF_LOOPBACK) == 0) {
1106 bzero(&r, sizeof(r));
1109 r.direction = PF_IN;
1113 if (rule_label(&r, $5.label))
1115 r.rtableid = $5.rtableid;
1119 h = ifa_lookup(i->ifname, 0);
1121 expand_rule(&r, NULL, NULL,
1122 NULL, NULL, h, NULL, NULL,
1123 NULL, NULL, NULL, NULL, "");
1131 antispoof_ifspc : FOR antispoof_if { $$ = $2; }
1132 | FOR '{' antispoof_iflst '}' { $$ = $3; }
1135 antispoof_iflst : antispoof_if { $$ = $1; }
1136 | antispoof_iflst comma antispoof_if {
1137 $1->tail->next = $3;
1143 antispoof_if : if_item { $$ = $1; }
1151 bzero(&antispoof_opts, sizeof antispoof_opts);
1152 antispoof_opts.rtableid = -1;
1155 { $$ = antispoof_opts; }
1157 bzero(&antispoof_opts, sizeof antispoof_opts);
1158 antispoof_opts.rtableid = -1;
1159 $$ = antispoof_opts;
1163 antispoof_opts_l : antispoof_opts_l antispoof_opt
1167 antispoof_opt : label {
1168 if (antispoof_opts.label) {
1169 yyerror("label cannot be redefined");
1172 antispoof_opts.label = $1;
1176 if ($2 > RT_TABLEID_MAX || $2 < 0) {
1177 yyerror("invalid rtable id");
1181 antispoof_opts.rtableid = $2;
1185 not : '!' { $$ = 1; }
1186 | /* empty */ { $$ = 0; }
1189 tabledef : TABLE '<' STRING '>' table_opts {
1190 struct node_host *h, *nh;
1191 struct node_tinit *ti, *nti;
1193 if (strlen($3) >= PF_TABLE_NAME_SIZE) {
1194 yyerror("table name too long, max %d chars",
1195 PF_TABLE_NAME_SIZE - 1);
1199 if (pf->loadopt & PFCTL_FLAG_TABLE)
1200 if (process_tabledef($3, &$5)) {
1205 for (ti = SIMPLEQ_FIRST(&$5.init_nodes);
1206 ti != SIMPLEQ_END(&$5.init_nodes); ti = nti) {
1209 for (h = ti->host; h != NULL; h = nh) {
1213 nti = SIMPLEQ_NEXT(ti, entries);
1220 bzero(&table_opts, sizeof table_opts);
1221 SIMPLEQ_INIT(&table_opts.init_nodes);
1224 { $$ = table_opts; }
1227 bzero(&table_opts, sizeof table_opts);
1228 SIMPLEQ_INIT(&table_opts.init_nodes);
1233 table_opts_l : table_opts_l table_opt
1237 table_opt : STRING {
1238 if (!strcmp($1, "const"))
1239 table_opts.flags |= PFR_TFLAG_CONST;
1240 else if (!strcmp($1, "persist"))
1241 table_opts.flags |= PFR_TFLAG_PERSIST;
1243 yyerror("invalid table option '%s'", $1);
1249 | '{' '}' { table_opts.init_addr = 1; }
1250 | '{' host_list '}' {
1251 struct node_host *n;
1252 struct node_tinit *ti;
1254 for (n = $2; n != NULL; n = n->next) {
1255 switch (n->addr.type) {
1256 case PF_ADDR_ADDRMASK:
1258 case PF_ADDR_DYNIFTL:
1259 yyerror("dynamic addresses are not "
1260 "permitted inside tables");
1263 yyerror("tables cannot contain tables");
1265 case PF_ADDR_NOROUTE:
1266 yyerror("\"no-route\" is not permitted "
1269 case PF_ADDR_URPFFAILED:
1270 yyerror("\"urpf-failed\" is not "
1271 "permitted inside tables");
1274 yyerror("unknown address type %d",
1279 if (!(ti = calloc(1, sizeof(*ti))))
1280 err(1, "table_opt: calloc");
1282 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1284 table_opts.init_addr = 1;
1287 struct node_tinit *ti;
1289 if (!(ti = calloc(1, sizeof(*ti))))
1290 err(1, "table_opt: calloc");
1292 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1294 table_opts.init_addr = 1;
1298 altqif : ALTQ interface queue_opts QUEUE qassign {
1301 if (check_rulestate(PFCTL_STATE_QUEUE))
1304 memset(&a, 0, sizeof(a));
1305 if ($3.scheduler.qtype == ALTQT_NONE) {
1306 yyerror("no scheduler specified!");
1309 a.scheduler = $3.scheduler.qtype;
1310 a.qlimit = $3.qlimit;
1311 a.tbrsize = $3.tbrsize;
1313 yyerror("no child queues specified");
1316 if (expand_altq(&a, $2, $5, $3.queue_bwspec,
1322 queuespec : QUEUE STRING interface queue_opts qassign {
1325 if (check_rulestate(PFCTL_STATE_QUEUE)) {
1330 memset(&a, 0, sizeof(a));
1332 if (strlcpy(a.qname, $2, sizeof(a.qname)) >=
1334 yyerror("queue name too long (max "
1335 "%d chars)", PF_QNAME_SIZE-1);
1341 yyerror("cannot specify tbrsize for queue");
1344 if ($4.priority > 255) {
1345 yyerror("priority out of range: max 255");
1348 a.priority = $4.priority;
1349 a.qlimit = $4.qlimit;
1350 a.scheduler = $4.scheduler.qtype;
1351 if (expand_queue(&a, $3, $5, $4.queue_bwspec,
1353 yyerror("errors in queue definition");
1360 bzero(&queue_opts, sizeof queue_opts);
1361 queue_opts.priority = DEFAULT_PRIORITY;
1362 queue_opts.qlimit = DEFAULT_QLIMIT;
1363 queue_opts.scheduler.qtype = ALTQT_NONE;
1364 queue_opts.queue_bwspec.bw_percent = 100;
1367 { $$ = queue_opts; }
1369 bzero(&queue_opts, sizeof queue_opts);
1370 queue_opts.priority = DEFAULT_PRIORITY;
1371 queue_opts.qlimit = DEFAULT_QLIMIT;
1372 queue_opts.scheduler.qtype = ALTQT_NONE;
1373 queue_opts.queue_bwspec.bw_percent = 100;
1378 queue_opts_l : queue_opts_l queue_opt
1382 queue_opt : BANDWIDTH bandwidth {
1383 if (queue_opts.marker & QOM_BWSPEC) {
1384 yyerror("bandwidth cannot be respecified");
1387 queue_opts.marker |= QOM_BWSPEC;
1388 queue_opts.queue_bwspec = $2;
1391 if (queue_opts.marker & QOM_PRIORITY) {
1392 yyerror("priority cannot be respecified");
1396 yyerror("priority out of range: max 255");
1399 queue_opts.marker |= QOM_PRIORITY;
1400 queue_opts.priority = $2;
1403 if (queue_opts.marker & QOM_QLIMIT) {
1404 yyerror("qlimit cannot be respecified");
1408 yyerror("qlimit out of range: max 65535");
1411 queue_opts.marker |= QOM_QLIMIT;
1412 queue_opts.qlimit = $2;
1415 if (queue_opts.marker & QOM_SCHEDULER) {
1416 yyerror("scheduler cannot be respecified");
1419 queue_opts.marker |= QOM_SCHEDULER;
1420 queue_opts.scheduler = $1;
1423 if (queue_opts.marker & QOM_TBRSIZE) {
1424 yyerror("tbrsize cannot be respecified");
1428 yyerror("tbrsize too big: max 65535");
1431 queue_opts.marker |= QOM_TBRSIZE;
1432 queue_opts.tbrsize = $2;
1436 bandwidth : STRING {
1442 bps = strtod($1, &cp);
1444 if (!strcmp(cp, "b"))
1446 else if (!strcmp(cp, "Kb"))
1448 else if (!strcmp(cp, "Mb"))
1450 else if (!strcmp(cp, "Gb"))
1451 bps *= 1000 * 1000 * 1000;
1452 else if (!strcmp(cp, "%")) {
1453 if (bps < 0 || bps > 100) {
1454 yyerror("bandwidth spec "
1459 $$.bw_percent = bps;
1462 yyerror("unknown unit %s", cp);
1468 $$.bw_absolute = (u_int32_t)bps;
1473 $$.qtype = ALTQT_CBQ;
1474 $$.data.cbq_opts.flags = 0;
1476 | CBQ '(' cbqflags_list ')' {
1477 $$.qtype = ALTQT_CBQ;
1478 $$.data.cbq_opts.flags = $3;
1481 $$.qtype = ALTQT_PRIQ;
1482 $$.data.priq_opts.flags = 0;
1484 | PRIQ '(' priqflags_list ')' {
1485 $$.qtype = ALTQT_PRIQ;
1486 $$.data.priq_opts.flags = $3;
1489 $$.qtype = ALTQT_HFSC;
1490 bzero(&$$.data.hfsc_opts,
1491 sizeof(struct node_hfsc_opts));
1493 | HFSC '(' hfsc_opts ')' {
1494 $$.qtype = ALTQT_HFSC;
1495 $$.data.hfsc_opts = $3;
1499 cbqflags_list : cbqflags_item { $$ |= $1; }
1500 | cbqflags_list comma cbqflags_item { $$ |= $3; }
1503 cbqflags_item : STRING {
1504 if (!strcmp($1, "default"))
1505 $$ = CBQCLF_DEFCLASS;
1506 else if (!strcmp($1, "borrow"))
1508 else if (!strcmp($1, "red"))
1510 else if (!strcmp($1, "ecn"))
1511 $$ = CBQCLF_RED|CBQCLF_ECN;
1512 else if (!strcmp($1, "rio"))
1515 yyerror("unknown cbq flag \"%s\"", $1);
1523 priqflags_list : priqflags_item { $$ |= $1; }
1524 | priqflags_list comma priqflags_item { $$ |= $3; }
1527 priqflags_item : STRING {
1528 if (!strcmp($1, "default"))
1529 $$ = PRCF_DEFAULTCLASS;
1530 else if (!strcmp($1, "red"))
1532 else if (!strcmp($1, "ecn"))
1533 $$ = PRCF_RED|PRCF_ECN;
1534 else if (!strcmp($1, "rio"))
1537 yyerror("unknown priq flag \"%s\"", $1);
1547 sizeof(struct node_hfsc_opts));
1554 hfscopts_list : hfscopts_item
1555 | hfscopts_list comma hfscopts_item
1558 hfscopts_item : LINKSHARE bandwidth {
1559 if (hfsc_opts.linkshare.used) {
1560 yyerror("linkshare already specified");
1563 hfsc_opts.linkshare.m2 = $2;
1564 hfsc_opts.linkshare.used = 1;
1566 | LINKSHARE '(' bandwidth comma number comma bandwidth ')'
1568 if (hfsc_opts.linkshare.used) {
1569 yyerror("linkshare already specified");
1572 hfsc_opts.linkshare.m1 = $3;
1573 hfsc_opts.linkshare.d = $5;
1574 hfsc_opts.linkshare.m2 = $7;
1575 hfsc_opts.linkshare.used = 1;
1577 | REALTIME bandwidth {
1578 if (hfsc_opts.realtime.used) {
1579 yyerror("realtime already specified");
1582 hfsc_opts.realtime.m2 = $2;
1583 hfsc_opts.realtime.used = 1;
1585 | REALTIME '(' bandwidth comma number comma bandwidth ')'
1587 if (hfsc_opts.realtime.used) {
1588 yyerror("realtime already specified");
1591 hfsc_opts.realtime.m1 = $3;
1592 hfsc_opts.realtime.d = $5;
1593 hfsc_opts.realtime.m2 = $7;
1594 hfsc_opts.realtime.used = 1;
1596 | UPPERLIMIT bandwidth {
1597 if (hfsc_opts.upperlimit.used) {
1598 yyerror("upperlimit already specified");
1601 hfsc_opts.upperlimit.m2 = $2;
1602 hfsc_opts.upperlimit.used = 1;
1604 | UPPERLIMIT '(' bandwidth comma number comma bandwidth ')'
1606 if (hfsc_opts.upperlimit.used) {
1607 yyerror("upperlimit already specified");
1610 hfsc_opts.upperlimit.m1 = $3;
1611 hfsc_opts.upperlimit.d = $5;
1612 hfsc_opts.upperlimit.m2 = $7;
1613 hfsc_opts.upperlimit.used = 1;
1616 if (!strcmp($1, "default"))
1617 hfsc_opts.flags |= HFCF_DEFAULTCLASS;
1618 else if (!strcmp($1, "red"))
1619 hfsc_opts.flags |= HFCF_RED;
1620 else if (!strcmp($1, "ecn"))
1621 hfsc_opts.flags |= HFCF_RED|HFCF_ECN;
1622 else if (!strcmp($1, "rio"))
1623 hfsc_opts.flags |= HFCF_RIO;
1625 yyerror("unknown hfsc flag \"%s\"", $1);
1633 qassign : /* empty */ { $$ = NULL; }
1634 | qassign_item { $$ = $1; }
1635 | '{' qassign_list '}' { $$ = $2; }
1638 qassign_list : qassign_item { $$ = $1; }
1639 | qassign_list comma qassign_item {
1640 $1->tail->next = $3;
1646 qassign_item : STRING {
1647 $$ = calloc(1, sizeof(struct node_queue));
1649 err(1, "qassign_item: calloc");
1650 if (strlcpy($$->queue, $1, sizeof($$->queue)) >=
1651 sizeof($$->queue)) {
1652 yyerror("queue name '%s' too long (max "
1653 "%d chars)", $1, sizeof($$->queue)-1);
1664 pfrule : action dir logquick interface route af proto fromto
1668 struct node_state_opt *o;
1669 struct node_proto *proto;
1674 if (check_rulestate(PFCTL_STATE_FILTER))
1677 memset(&r, 0, sizeof(r));
1681 case PFRULE_RETURNRST:
1682 r.rule_flag |= PFRULE_RETURNRST;
1683 r.return_ttl = $1.w;
1685 case PFRULE_RETURNICMP:
1686 r.rule_flag |= PFRULE_RETURNICMP;
1687 r.return_icmp = $1.w;
1688 r.return_icmp6 = $1.w2;
1691 r.rule_flag |= PFRULE_RETURN;
1692 r.return_icmp = $1.w;
1693 r.return_icmp6 = $1.w2;
1701 r.rtableid = $9.rtableid;
1705 if (strlcpy(r.tagname, $9.tag,
1706 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1707 yyerror("tag too long, max %u chars",
1708 PF_TAG_NAME_SIZE - 1);
1712 if (strlcpy(r.match_tagname, $9.match_tag,
1713 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1714 yyerror("tag too long, max %u chars",
1715 PF_TAG_NAME_SIZE - 1);
1718 r.match_tag_not = $9.match_tag_not;
1719 if (rule_label(&r, $9.label))
1722 r.flags = $9.flags.b1;
1723 r.flagset = $9.flags.b2;
1724 if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
1725 yyerror("flags always false");
1728 if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
1729 for (proto = $7; proto != NULL &&
1730 proto->proto != IPPROTO_TCP;
1731 proto = proto->next)
1733 if (proto == NULL && $7 != NULL) {
1734 if ($9.flags.b1 || $9.flags.b2)
1736 "flags only apply to tcp");
1739 "OS fingerprinting only "
1744 if (($9.flags.b1 & parse_flags("S")) == 0 &&
1746 yyerror("OS fingerprinting requires "
1747 "the SYN TCP flag (flags S/SA)");
1754 r.keep_state = $9.keep.action;
1756 /* 'keep state' by default on pass rules. */
1757 if (!r.keep_state && !r.action &&
1758 !($9.marker & FOM_KEEP))
1759 r.keep_state = PF_STATE_NORMAL;
1761 o = $9.keep.options;
1763 struct node_state_opt *p = o;
1766 case PF_STATE_OPT_MAX:
1768 yyerror("state option 'max' "
1769 "multiple definitions");
1772 r.max_states = o->data.max_states;
1774 case PF_STATE_OPT_NOSYNC:
1775 if (r.rule_flag & PFRULE_NOSYNC) {
1776 yyerror("state option 'sync' "
1777 "multiple definitions");
1780 r.rule_flag |= PFRULE_NOSYNC;
1782 case PF_STATE_OPT_SRCTRACK:
1784 yyerror("state option "
1786 "multiple definitions");
1789 srctrack = o->data.src_track;
1790 r.rule_flag |= PFRULE_SRCTRACK;
1792 case PF_STATE_OPT_MAX_SRC_STATES:
1793 if (r.max_src_states) {
1794 yyerror("state option "
1796 "multiple definitions");
1799 if (o->data.max_src_states == 0) {
1800 yyerror("'max-src-states' must "
1805 o->data.max_src_states;
1806 r.rule_flag |= PFRULE_SRCTRACK;
1808 case PF_STATE_OPT_OVERLOAD:
1809 if (r.overload_tblname[0]) {
1810 yyerror("multiple 'overload' "
1811 "table definitions");
1814 if (strlcpy(r.overload_tblname,
1815 o->data.overload.tblname,
1816 PF_TABLE_NAME_SIZE) >=
1817 PF_TABLE_NAME_SIZE) {
1818 yyerror("state option: "
1822 r.flush = o->data.overload.flush;
1824 case PF_STATE_OPT_MAX_SRC_CONN:
1825 if (r.max_src_conn) {
1826 yyerror("state option "
1828 "multiple definitions");
1831 if (o->data.max_src_conn == 0) {
1832 yyerror("'max-src-conn' "
1837 o->data.max_src_conn;
1838 r.rule_flag |= PFRULE_SRCTRACK |
1839 PFRULE_RULESRCTRACK;
1841 case PF_STATE_OPT_MAX_SRC_CONN_RATE:
1842 if (r.max_src_conn_rate.limit) {
1843 yyerror("state option "
1844 "'max-src-conn-rate' "
1845 "multiple definitions");
1848 if (!o->data.max_src_conn_rate.limit ||
1849 !o->data.max_src_conn_rate.seconds) {
1850 yyerror("'max-src-conn-rate' "
1851 "values must be > 0");
1854 if (o->data.max_src_conn_rate.limit >
1856 yyerror("'max-src-conn-rate' "
1857 "maximum rate must be < %u",
1861 r.max_src_conn_rate.limit =
1862 o->data.max_src_conn_rate.limit;
1863 r.max_src_conn_rate.seconds =
1864 o->data.max_src_conn_rate.seconds;
1865 r.rule_flag |= PFRULE_SRCTRACK |
1866 PFRULE_RULESRCTRACK;
1868 case PF_STATE_OPT_MAX_SRC_NODES:
1869 if (r.max_src_nodes) {
1870 yyerror("state option "
1872 "multiple definitions");
1875 if (o->data.max_src_nodes == 0) {
1876 yyerror("'max-src-nodes' must "
1881 o->data.max_src_nodes;
1882 r.rule_flag |= PFRULE_SRCTRACK |
1883 PFRULE_RULESRCTRACK;
1885 case PF_STATE_OPT_STATELOCK:
1887 yyerror("state locking option: "
1888 "multiple definitions");
1892 r.rule_flag |= o->data.statelock;
1894 case PF_STATE_OPT_SLOPPY:
1895 if (r.rule_flag & PFRULE_STATESLOPPY) {
1896 yyerror("state sloppy option: "
1897 "multiple definitions");
1900 r.rule_flag |= PFRULE_STATESLOPPY;
1902 case PF_STATE_OPT_TIMEOUT:
1903 if (o->data.timeout.number ==
1904 PFTM_ADAPTIVE_START ||
1905 o->data.timeout.number ==
1908 if (r.timeout[o->data.timeout.number]) {
1909 yyerror("state timeout %s "
1910 "multiple definitions",
1911 pf_timeouts[o->data.
1912 timeout.number].name);
1915 r.timeout[o->data.timeout.number] =
1916 o->data.timeout.seconds;
1922 /* 'flags S/SA' by default on stateful rules */
1923 if (!r.action && !r.flags && !r.flagset &&
1924 !$9.fragment && !($9.marker & FOM_FLAGS) &&
1926 r.flags = parse_flags("S");
1927 r.flagset = parse_flags("SA");
1929 if (!adaptive && r.max_states) {
1930 r.timeout[PFTM_ADAPTIVE_START] =
1931 (r.max_states / 10) * 6;
1932 r.timeout[PFTM_ADAPTIVE_END] =
1933 (r.max_states / 10) * 12;
1935 if (r.rule_flag & PFRULE_SRCTRACK) {
1936 if (srctrack == PF_SRCTRACK_GLOBAL &&
1938 yyerror("'max-src-nodes' is "
1939 "incompatible with "
1940 "'source-track global'");
1943 if (srctrack == PF_SRCTRACK_GLOBAL &&
1945 yyerror("'max-src-conn' is "
1946 "incompatible with "
1947 "'source-track global'");
1950 if (srctrack == PF_SRCTRACK_GLOBAL &&
1951 r.max_src_conn_rate.seconds) {
1952 yyerror("'max-src-conn-rate' is "
1953 "incompatible with "
1954 "'source-track global'");
1957 if (r.timeout[PFTM_SRC_NODE] <
1958 r.max_src_conn_rate.seconds)
1959 r.timeout[PFTM_SRC_NODE] =
1960 r.max_src_conn_rate.seconds;
1961 r.rule_flag |= PFRULE_SRCTRACK;
1962 if (srctrack == PF_SRCTRACK_RULE)
1963 r.rule_flag |= PFRULE_RULESRCTRACK;
1965 if (r.keep_state && !statelock)
1966 r.rule_flag |= default_statelock;
1969 r.rule_flag |= PFRULE_FRAGMENT;
1970 r.allow_opts = $9.allowopts;
1972 decide_address_family($8.src.host, &r.af);
1973 decide_address_family($8.dst.host, &r.af);
1977 yyerror("direction must be explicit "
1978 "with rules that specify routing");
1982 r.rpool.opts = $5.pool_opts;
1984 memcpy(&r.rpool.key, $5.key,
1985 sizeof(struct pf_poolhashkey));
1987 if (r.rt && r.rt != PF_FASTROUTE) {
1988 decide_address_family($5.host, &r.af);
1989 remove_invalid_hosts(&$5.host, &r.af);
1990 if ($5.host == NULL) {
1991 yyerror("no routing address with "
1992 "matching address family found.");
1995 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
1996 PF_POOL_NONE && ($5.host->next != NULL ||
1997 $5.host->addr.type == PF_ADDR_TABLE ||
1998 DYNIF_MULTIADDR($5.host->addr)))
1999 r.rpool.opts |= PF_POOL_ROUNDROBIN;
2000 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2001 PF_POOL_ROUNDROBIN &&
2002 disallow_table($5.host, "tables are only "
2003 "supported in round-robin routing pools"))
2005 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2006 PF_POOL_ROUNDROBIN &&
2007 disallow_alias($5.host, "interface (%s) "
2008 "is only supported in round-robin "
2011 if ($5.host->next != NULL) {
2012 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2013 PF_POOL_ROUNDROBIN) {
2014 yyerror("r.rpool.opts must "
2015 "be PF_POOL_ROUNDROBIN");
2020 if ($9.queues.qname != NULL) {
2021 if (strlcpy(r.qname, $9.queues.qname,
2022 sizeof(r.qname)) >= sizeof(r.qname)) {
2023 yyerror("rule qname too long (max "
2024 "%d chars)", sizeof(r.qname)-1);
2027 free($9.queues.qname);
2029 if ($9.queues.pqname != NULL) {
2030 if (strlcpy(r.pqname, $9.queues.pqname,
2031 sizeof(r.pqname)) >= sizeof(r.pqname)) {
2032 yyerror("rule pqname too long (max "
2033 "%d chars)", sizeof(r.pqname)-1);
2036 free($9.queues.pqname);
2039 expand_rule(&r, $4, $5.host, $7, $8.src_os,
2040 $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
2041 $9.uid, $9.gid, $9.icmpspec, "");
2046 bzero(&filter_opts, sizeof filter_opts);
2047 filter_opts.rtableid = -1;
2050 { $$ = filter_opts; }
2052 bzero(&filter_opts, sizeof filter_opts);
2053 filter_opts.rtableid = -1;
2058 filter_opts_l : filter_opts_l filter_opt
2062 filter_opt : USER uids {
2063 if (filter_opts.uid)
2064 $2->tail->next = filter_opts.uid;
2065 filter_opts.uid = $2;
2068 if (filter_opts.gid)
2069 $2->tail->next = filter_opts.gid;
2070 filter_opts.gid = $2;
2073 if (filter_opts.marker & FOM_FLAGS) {
2074 yyerror("flags cannot be redefined");
2077 filter_opts.marker |= FOM_FLAGS;
2078 filter_opts.flags.b1 |= $1.b1;
2079 filter_opts.flags.b2 |= $1.b2;
2080 filter_opts.flags.w |= $1.w;
2081 filter_opts.flags.w2 |= $1.w2;
2084 if (filter_opts.marker & FOM_ICMP) {
2085 yyerror("icmp-type cannot be redefined");
2088 filter_opts.marker |= FOM_ICMP;
2089 filter_opts.icmpspec = $1;
2092 if (filter_opts.marker & FOM_TOS) {
2093 yyerror("tos cannot be redefined");
2096 filter_opts.marker |= FOM_TOS;
2097 filter_opts.tos = $1;
2100 if (filter_opts.marker & FOM_KEEP) {
2101 yyerror("modulate or keep cannot be redefined");
2104 filter_opts.marker |= FOM_KEEP;
2105 filter_opts.keep.action = $1.action;
2106 filter_opts.keep.options = $1.options;
2109 filter_opts.fragment = 1;
2112 filter_opts.allowopts = 1;
2115 if (filter_opts.label) {
2116 yyerror("label cannot be redefined");
2119 filter_opts.label = $1;
2122 if (filter_opts.queues.qname) {
2123 yyerror("queue cannot be redefined");
2126 filter_opts.queues = $1;
2129 filter_opts.tag = $2;
2131 | not TAGGED string {
2132 filter_opts.match_tag = $3;
2133 filter_opts.match_tag_not = $1;
2135 | PROBABILITY STRING {
2137 double p = strtod($2, &e);
2144 yyerror("invalid probability: %s", $2);
2148 p = floor(p * (UINT_MAX+1.0) + 0.5);
2149 if (p < 1.0 || p >= (UINT_MAX+1.0)) {
2150 yyerror("invalid probability: %s", $2);
2154 filter_opts.prob = (u_int32_t)p;
2159 if ($2 > RT_TABLEID_MAX || $2 < 0) {
2160 yyerror("invalid rtable id");
2164 filter_opts.rtableid = $2;
2168 action : PASS { $$.b1 = PF_PASS; $$.b2 = $$.w = 0; }
2169 | BLOCK blockspec { $$ = $2; $$.b1 = PF_DROP; }
2172 blockspec : /* empty */ {
2173 $$.b2 = blockpolicy;
2174 $$.w = returnicmpdefault;
2175 $$.w2 = returnicmp6default;
2178 $$.b2 = PFRULE_DROP;
2183 $$.b2 = PFRULE_RETURNRST;
2187 | RETURNRST '(' TTL number ')' {
2189 yyerror("illegal ttl value %d", $4);
2192 $$.b2 = PFRULE_RETURNRST;
2197 $$.b2 = PFRULE_RETURNICMP;
2198 $$.w = returnicmpdefault;
2199 $$.w2 = returnicmp6default;
2202 $$.b2 = PFRULE_RETURNICMP;
2203 $$.w = returnicmpdefault;
2204 $$.w2 = returnicmp6default;
2206 | RETURNICMP '(' STRING ')' {
2207 $$.b2 = PFRULE_RETURNICMP;
2208 if (!($$.w = parseicmpspec($3, AF_INET))) {
2213 $$.w2 = returnicmp6default;
2215 | RETURNICMP6 '(' STRING ')' {
2216 $$.b2 = PFRULE_RETURNICMP;
2217 $$.w = returnicmpdefault;
2218 if (!($$.w2 = parseicmpspec($3, AF_INET6))) {
2224 | RETURNICMP '(' STRING comma STRING ')' {
2225 $$.b2 = PFRULE_RETURNICMP;
2226 if (!($$.w = parseicmpspec($3, AF_INET)) ||
2227 !($$.w2 = parseicmpspec($5, AF_INET6))) {
2236 $$.b2 = PFRULE_RETURN;
2237 $$.w = returnicmpdefault;
2238 $$.w2 = returnicmp6default;
2242 dir : /* empty */ { $$ = 0; }
2243 | IN { $$ = PF_IN; }
2244 | OUT { $$ = PF_OUT; }
2247 quick : /* empty */ { $$.quick = 0; }
2248 | QUICK { $$.quick = 1; }
2251 logquick : /* empty */ { $$.log = 0; $$.quick = 0; $$.logif = 0; }
2252 | log { $$ = $1; $$.quick = 0; }
2253 | QUICK { $$.quick = 1; $$.log = 0; $$.logif = 0; }
2254 | log QUICK { $$ = $1; $$.quick = 1; }
2255 | QUICK log { $$ = $2; $$.quick = 1; }
2258 log : LOG { $$.log = PF_LOG; $$.logif = 0; }
2259 | LOG '(' logopts ')' {
2260 $$.log = PF_LOG | $3.log;
2261 $$.logif = $3.logif;
2265 logopts : logopt { $$ = $1; }
2266 | logopts comma logopt {
2267 $$.log = $1.log | $3.log;
2268 $$.logif = $3.logif;
2270 $$.logif = $1.logif;
2274 logopt : ALL { $$.log = PF_LOG_ALL; $$.logif = 0; }
2275 | USER { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
2276 | GROUP { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
2282 if (strncmp($2, "pflog", 5)) {
2283 yyerror("%s: should be a pflog interface", $2);
2287 i = strtonum($2 + 5, 0, 255, &errstr);
2289 yyerror("%s: %s", $2, errstr);
2298 interface : /* empty */ { $$ = NULL; }
2299 | ON if_item_not { $$ = $2; }
2300 | ON '{' if_list '}' { $$ = $3; }
2303 if_list : if_item_not { $$ = $1; }
2304 | if_list comma if_item_not {
2305 $1->tail->next = $3;
2311 if_item_not : not if_item { $$ = $2; $$->not = $1; }
2315 struct node_host *n;
2317 $$ = calloc(1, sizeof(struct node_if));
2319 err(1, "if_item: calloc");
2320 if (strlcpy($$->ifname, $1, sizeof($$->ifname)) >=
2321 sizeof($$->ifname)) {
2324 yyerror("interface name too long");
2328 if ((n = ifa_exists($1)) != NULL)
2329 $$->ifa_flags = n->ifa_flags;
2338 af : /* empty */ { $$ = 0; }
2339 | INET { $$ = AF_INET; }
2340 | INET6 { $$ = AF_INET6; }
2343 proto : /* empty */ { $$ = NULL; }
2344 | PROTO proto_item { $$ = $2; }
2345 | PROTO '{' proto_list '}' { $$ = $3; }
2348 proto_list : proto_item { $$ = $1; }
2349 | proto_list comma proto_item {
2350 $1->tail->next = $3;
2356 proto_item : STRING {
2360 if (atoul($1, &ulval) == 0) {
2362 yyerror("protocol outside range");
2366 pr = (u_int8_t)ulval;
2370 p = getprotobyname($1);
2372 yyerror("unknown protocol %s", $1);
2380 yyerror("proto 0 cannot be used");
2383 $$ = calloc(1, sizeof(struct node_proto));
2385 err(1, "proto_item: calloc");
2406 os : /* empty */ { $$ = NULL; }
2407 | OS xos { $$ = $2; }
2408 | OS '{' os_list '}' { $$ = $3; }
2412 $$ = calloc(1, sizeof(struct node_os));
2414 err(1, "os: calloc");
2420 os_list : xos { $$ = $1; }
2421 | os_list comma xos {
2422 $1->tail->next = $3;
2428 from : /* empty */ {
2442 if (disallow_urpf_failed($2.host, "\"urpf-failed\" is "
2443 "not permitted in a destination address"))
2449 ipportspec : ipspec {
2453 | ipspec PORT portspec {
2463 ipspec : ANY { $$ = NULL; }
2464 | xhost { $$ = $1; }
2465 | '{' host_list '}' { $$ = $2; }
2468 host_list : ipspec { $$ = $1; }
2469 | host_list comma ipspec {
2472 else if ($1 == NULL)
2475 $1->tail->next = $3;
2476 $1->tail = $3->tail;
2483 struct node_host *n;
2485 for (n = $2; n != NULL; n = n->next)
2490 $$ = calloc(1, sizeof(struct node_host));
2492 err(1, "xhost: calloc");
2493 $$->addr.type = PF_ADDR_NOROUTE;
2499 $$ = calloc(1, sizeof(struct node_host));
2501 err(1, "xhost: calloc");
2502 $$->addr.type = PF_ADDR_URPFFAILED;
2510 if (($$ = host($1)) == NULL) {
2511 /* error. "any" is handled elsewhere */
2513 yyerror("could not parse host specification");
2519 | STRING '/' number {
2522 if (asprintf(&buf, "%s/%u", $1, $3) == -1)
2523 err(1, "host: asprintf");
2525 if (($$ = host(buf)) == NULL) {
2526 /* error. "any" is handled elsewhere */
2528 yyerror("could not parse host specification");
2534 | dynaddr '/' number {
2535 struct node_host *n;
2538 for (n = $1; n != NULL; n = n->next)
2542 if (strlen($2) >= PF_TABLE_NAME_SIZE) {
2543 yyerror("table name '%s' too long", $2);
2547 $$ = calloc(1, sizeof(struct node_host));
2549 err(1, "host: calloc");
2550 $$->addr.type = PF_ADDR_TABLE;
2551 if (strlcpy($$->addr.v.tblname, $2,
2552 sizeof($$->addr.v.tblname)) >=
2553 sizeof($$->addr.v.tblname))
2554 errx(1, "host: strlcpy");
2560 $$ = calloc(1, sizeof(struct node_host));
2563 err(1, "host: calloc");
2565 $$->addr.type = PF_ADDR_RTLABEL;
2566 if (strlcpy($$->addr.v.rtlabelname, $2,
2567 sizeof($$->addr.v.rtlabelname)) >=
2568 sizeof($$->addr.v.rtlabelname)) {
2569 yyerror("route label too long, max %u chars",
2570 sizeof($$->addr.v.rtlabelname) - 1);
2584 if (atoul($1, &ulval) == -1) {
2585 yyerror("%s is not a number", $1);
2594 dynaddr : '(' STRING ')' {
2599 if (!isalpha(op[0])) {
2600 yyerror("invalid interface name '%s'", op);
2604 while ((p = strrchr($2, ':')) != NULL) {
2605 if (!strcmp(p+1, "network"))
2606 flags |= PFI_AFLAG_NETWORK;
2607 else if (!strcmp(p+1, "broadcast"))
2608 flags |= PFI_AFLAG_BROADCAST;
2609 else if (!strcmp(p+1, "peer"))
2610 flags |= PFI_AFLAG_PEER;
2611 else if (!strcmp(p+1, "0"))
2612 flags |= PFI_AFLAG_NOALIAS;
2614 yyerror("interface %s has bad modifier",
2621 if (flags & (flags - 1) & PFI_AFLAG_MODEMASK) {
2623 yyerror("illegal combination of "
2624 "interface modifiers");
2627 $$ = calloc(1, sizeof(struct node_host));
2629 err(1, "address: calloc");
2631 set_ipmask($$, 128);
2632 $$->addr.type = PF_ADDR_DYNIFTL;
2633 $$->addr.iflags = flags;
2634 if (strlcpy($$->addr.v.ifname, $2,
2635 sizeof($$->addr.v.ifname)) >=
2636 sizeof($$->addr.v.ifname)) {
2639 yyerror("interface name too long");
2648 portspec : port_item { $$ = $1; }
2649 | '{' port_list '}' { $$ = $2; }
2652 port_list : port_item { $$ = $1; }
2653 | port_list comma port_item {
2654 $1->tail->next = $3;
2661 $$ = calloc(1, sizeof(struct node_port));
2663 err(1, "port_item: calloc");
2675 yyerror("':' cannot be used with an other "
2679 $$ = calloc(1, sizeof(struct node_port));
2681 err(1, "port_item: calloc");
2688 | port PORTBINARY port {
2690 yyerror("':' cannot be used with an other "
2694 $$ = calloc(1, sizeof(struct node_port));
2696 err(1, "port_item: calloc");
2706 char *p = strchr($1, ':');
2709 if (($$.a = getservice($1)) == -1) {
2718 if ((port[0] = getservice($1)) == -1 ||
2719 (port[1] = getservice(p)) == -1) {
2731 uids : uid_item { $$ = $1; }
2732 | '{' uid_list '}' { $$ = $2; }
2735 uid_list : uid_item { $$ = $1; }
2736 | uid_list comma uid_item {
2737 $1->tail->next = $3;
2744 $$ = calloc(1, sizeof(struct node_uid));
2746 err(1, "uid_item: calloc");
2754 if ($2 == UID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
2755 yyerror("user unknown requires operator = or "
2759 $$ = calloc(1, sizeof(struct node_uid));
2761 err(1, "uid_item: calloc");
2768 | uid PORTBINARY uid {
2769 if ($1 == UID_MAX || $3 == UID_MAX) {
2770 yyerror("user unknown requires operator = or "
2774 $$ = calloc(1, sizeof(struct node_uid));
2776 err(1, "uid_item: calloc");
2788 if (atoul($1, &ulval) == -1) {
2789 if (!strcmp($1, "unknown"))
2794 if ((pw = getpwnam($1)) == NULL) {
2795 yyerror("unknown user %s", $1);
2802 if (ulval >= UID_MAX) {
2804 yyerror("illegal uid value %lu", ulval);
2813 gids : gid_item { $$ = $1; }
2814 | '{' gid_list '}' { $$ = $2; }
2817 gid_list : gid_item { $$ = $1; }
2818 | gid_list comma gid_item {
2819 $1->tail->next = $3;
2826 $$ = calloc(1, sizeof(struct node_gid));
2828 err(1, "gid_item: calloc");
2836 if ($2 == GID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
2837 yyerror("group unknown requires operator = or "
2841 $$ = calloc(1, sizeof(struct node_gid));
2843 err(1, "gid_item: calloc");
2850 | gid PORTBINARY gid {
2851 if ($1 == GID_MAX || $3 == GID_MAX) {
2852 yyerror("group unknown requires operator = or "
2856 $$ = calloc(1, sizeof(struct node_gid));
2858 err(1, "gid_item: calloc");
2870 if (atoul($1, &ulval) == -1) {
2871 if (!strcmp($1, "unknown"))
2876 if ((grp = getgrnam($1)) == NULL) {
2877 yyerror("unknown group %s", $1);
2884 if (ulval >= GID_MAX) {
2885 yyerror("illegal gid value %lu", ulval);
2898 if ((f = parse_flags($1)) < 0) {
2899 yyerror("bad flags %s", $1);
2908 flags : FLAGS flag '/' flag { $$.b1 = $2.b1; $$.b2 = $4.b1; }
2909 | FLAGS '/' flag { $$.b1 = 0; $$.b2 = $3.b1; }
2910 | FLAGS ANY { $$.b1 = 0; $$.b2 = 0; }
2913 icmpspec : ICMPTYPE icmp_item { $$ = $2; }
2914 | ICMPTYPE '{' icmp_list '}' { $$ = $3; }
2915 | ICMP6TYPE icmp6_item { $$ = $2; }
2916 | ICMP6TYPE '{' icmp6_list '}' { $$ = $3; }
2919 icmp_list : icmp_item { $$ = $1; }
2920 | icmp_list comma icmp_item {
2921 $1->tail->next = $3;
2927 icmp6_list : icmp6_item { $$ = $1; }
2928 | icmp6_list comma icmp6_item {
2929 $1->tail->next = $3;
2935 icmp_item : icmptype {
2936 $$ = calloc(1, sizeof(struct node_icmp));
2938 err(1, "icmp_item: calloc");
2941 $$->proto = IPPROTO_ICMP;
2945 | icmptype CODE STRING {
2946 const struct icmpcodeent *p;
2949 if (atoul($3, &ulval) == 0) {
2952 yyerror("illegal icmp-code %lu", ulval);
2956 if ((p = geticmpcodebyname($1-1, $3,
2957 AF_INET)) == NULL) {
2958 yyerror("unknown icmp-code %s", $3);
2965 $$ = calloc(1, sizeof(struct node_icmp));
2967 err(1, "icmp_item: calloc");
2969 $$->code = ulval + 1;
2970 $$->proto = IPPROTO_ICMP;
2976 icmp6_item : icmp6type {
2977 $$ = calloc(1, sizeof(struct node_icmp));
2979 err(1, "icmp_item: calloc");
2982 $$->proto = IPPROTO_ICMPV6;
2986 | icmp6type CODE STRING {
2987 const struct icmpcodeent *p;
2990 if (atoul($3, &ulval) == 0) {
2992 yyerror("illegal icmp6-code %lu",
2998 if ((p = geticmpcodebyname($1-1, $3,
2999 AF_INET6)) == NULL) {
3000 yyerror("unknown icmp6-code %s", $3);
3007 $$ = calloc(1, sizeof(struct node_icmp));
3009 err(1, "icmp_item: calloc");
3011 $$->code = ulval + 1;
3012 $$->proto = IPPROTO_ICMPV6;
3019 const struct icmptypeent *p;
3022 if (atoul($1, &ulval) == 0) {
3024 yyerror("illegal icmp-type %lu", ulval);
3030 if ((p = geticmptypebyname($1, AF_INET)) ==
3032 yyerror("unknown icmp-type %s", $1);
3042 icmp6type : STRING {
3043 const struct icmptypeent *p;
3046 if (atoul($1, &ulval) == 0) {
3048 yyerror("illegal icmp6-type %lu",
3055 if ((p = geticmptypebyname($1, AF_INET6)) ==
3057 yyerror("unknown icmp6-type %s", $1);
3068 if (!strcmp($2, "lowdelay"))
3069 $$ = IPTOS_LOWDELAY;
3070 else if (!strcmp($2, "throughput"))
3071 $$ = IPTOS_THROUGHPUT;
3072 else if (!strcmp($2, "reliability"))
3073 $$ = IPTOS_RELIABILITY;
3074 else if ($2[0] == '0' && $2[1] == 'x')
3075 $$ = strtoul($2, NULL, 16);
3077 $$ = strtoul($2, NULL, 10);
3078 if (!$$ || $$ > 255) {
3079 yyerror("illegal tos value %s", $2);
3087 sourcetrack : SOURCETRACK { $$ = PF_SRCTRACK; }
3088 | SOURCETRACK GLOBAL { $$ = PF_SRCTRACK_GLOBAL; }
3089 | SOURCETRACK RULE { $$ = PF_SRCTRACK_RULE; }
3092 statelock : IFBOUND {
3093 $$ = PFRULE_IFBOUND;
3104 | KEEP STATE state_opt_spec {
3105 $$.action = PF_STATE_NORMAL;
3108 | MODULATE STATE state_opt_spec {
3109 $$.action = PF_STATE_MODULATE;
3112 | SYNPROXY STATE state_opt_spec {
3113 $$.action = PF_STATE_SYNPROXY;
3118 flush : /* empty */ { $$ = 0; }
3119 | FLUSH { $$ = PF_FLUSH; }
3121 $$ = PF_FLUSH | PF_FLUSH_GLOBAL;
3125 state_opt_spec : '(' state_opt_list ')' { $$ = $2; }
3126 | /* empty */ { $$ = NULL; }
3129 state_opt_list : state_opt_item { $$ = $1; }
3130 | state_opt_list comma state_opt_item {
3131 $1->tail->next = $3;
3137 state_opt_item : MAXIMUM number {
3138 $$ = calloc(1, sizeof(struct node_state_opt));
3140 err(1, "state_opt_item: calloc");
3141 $$->type = PF_STATE_OPT_MAX;
3142 $$->data.max_states = $2;
3147 $$ = calloc(1, sizeof(struct node_state_opt));
3149 err(1, "state_opt_item: calloc");
3150 $$->type = PF_STATE_OPT_NOSYNC;
3154 | MAXSRCSTATES number {
3155 $$ = calloc(1, sizeof(struct node_state_opt));
3157 err(1, "state_opt_item: calloc");
3158 $$->type = PF_STATE_OPT_MAX_SRC_STATES;
3159 $$->data.max_src_states = $2;
3163 | MAXSRCCONN number {
3164 $$ = calloc(1, sizeof(struct node_state_opt));
3166 err(1, "state_opt_item: calloc");
3167 $$->type = PF_STATE_OPT_MAX_SRC_CONN;
3168 $$->data.max_src_conn = $2;
3172 | MAXSRCCONNRATE number '/' number {
3173 $$ = calloc(1, sizeof(struct node_state_opt));
3175 err(1, "state_opt_item: calloc");
3176 $$->type = PF_STATE_OPT_MAX_SRC_CONN_RATE;
3177 $$->data.max_src_conn_rate.limit = $2;
3178 $$->data.max_src_conn_rate.seconds = $4;
3182 | OVERLOAD '<' STRING '>' flush {
3183 if (strlen($3) >= PF_TABLE_NAME_SIZE) {
3184 yyerror("table name '%s' too long", $3);
3188 $$ = calloc(1, sizeof(struct node_state_opt));
3190 err(1, "state_opt_item: calloc");
3191 if (strlcpy($$->data.overload.tblname, $3,
3192 PF_TABLE_NAME_SIZE) >= PF_TABLE_NAME_SIZE)
3193 errx(1, "state_opt_item: strlcpy");
3195 $$->type = PF_STATE_OPT_OVERLOAD;
3196 $$->data.overload.flush = $5;
3200 | MAXSRCNODES number {
3201 $$ = calloc(1, sizeof(struct node_state_opt));
3203 err(1, "state_opt_item: calloc");
3204 $$->type = PF_STATE_OPT_MAX_SRC_NODES;
3205 $$->data.max_src_nodes = $2;
3210 $$ = calloc(1, sizeof(struct node_state_opt));
3212 err(1, "state_opt_item: calloc");
3213 $$->type = PF_STATE_OPT_SRCTRACK;
3214 $$->data.src_track = $1;
3219 $$ = calloc(1, sizeof(struct node_state_opt));
3221 err(1, "state_opt_item: calloc");
3222 $$->type = PF_STATE_OPT_STATELOCK;
3223 $$->data.statelock = $1;
3228 $$ = calloc(1, sizeof(struct node_state_opt));
3230 err(1, "state_opt_item: calloc");
3231 $$->type = PF_STATE_OPT_SLOPPY;
3238 for (i = 0; pf_timeouts[i].name &&
3239 strcmp(pf_timeouts[i].name, $1); ++i)
3241 if (!pf_timeouts[i].name) {
3242 yyerror("illegal timeout name %s", $1);
3246 if (strchr(pf_timeouts[i].name, '.') == NULL) {
3247 yyerror("illegal state timeout %s", $1);
3252 $$ = calloc(1, sizeof(struct node_state_opt));
3254 err(1, "state_opt_item: calloc");
3255 $$->type = PF_STATE_OPT_TIMEOUT;
3256 $$->data.timeout.number = pf_timeouts[i].timeout;
3257 $$->data.timeout.seconds = $2;
3263 label : LABEL STRING {
3268 qname : QUEUE STRING {
3271 | QUEUE '(' STRING ')' {
3274 | QUEUE '(' STRING comma STRING ')' {
3280 no : /* empty */ { $$ = 0; }
3285 char *p = strchr($1, ':');
3288 if (($$.a = getservice($1)) == -1) {
3293 } else if (!strcmp(p+1, "*")) {
3295 if (($$.a = getservice($1)) == -1) {
3303 if (($$.a = getservice($1)) == -1 ||
3304 ($$.b = getservice(p)) == -1) {
3316 redirspec : host { $$ = $1; }
3317 | '{' redir_host_list '}' { $$ = $2; }
3320 redir_host_list : host { $$ = $1; }
3321 | redir_host_list comma host {
3322 $1->tail->next = $3;
3323 $1->tail = $3->tail;
3328 redirpool : /* empty */ { $$ = NULL; }
3330 $$ = calloc(1, sizeof(struct redirection));
3332 err(1, "redirection: calloc");
3334 $$->rport.a = $$->rport.b = $$->rport.t = 0;
3336 | ARROW redirspec PORT rport {
3337 $$ = calloc(1, sizeof(struct redirection));
3339 err(1, "redirection: calloc");
3345 hashkey : /* empty */
3347 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3349 err(1, "hashkey: calloc");
3350 $$->key32[0] = arc4random();
3351 $$->key32[1] = arc4random();
3352 $$->key32[2] = arc4random();
3353 $$->key32[3] = arc4random();
3357 if (!strncmp($1, "0x", 2)) {
3358 if (strlen($1) != 34) {
3360 yyerror("hex key must be 128 bits "
3361 "(32 hex digits) long");
3364 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3366 err(1, "hashkey: calloc");
3368 if (sscanf($1, "0x%8x%8x%8x%8x",
3369 &$$->key32[0], &$$->key32[1],
3370 &$$->key32[2], &$$->key32[3]) != 4) {
3373 yyerror("invalid hex key");
3379 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3381 err(1, "hashkey: calloc");
3383 MD5Update(&context, (unsigned char *)$1,
3385 MD5Final((unsigned char *)$$, &context);
3386 HTONL($$->key32[0]);
3387 HTONL($$->key32[1]);
3388 HTONL($$->key32[2]);
3389 HTONL($$->key32[3]);
3395 pool_opts : { bzero(&pool_opts, sizeof pool_opts); }
3399 bzero(&pool_opts, sizeof pool_opts);
3404 pool_opts_l : pool_opts_l pool_opt
3408 pool_opt : BITMASK {
3409 if (pool_opts.type) {
3410 yyerror("pool type cannot be redefined");
3413 pool_opts.type = PF_POOL_BITMASK;
3416 if (pool_opts.type) {
3417 yyerror("pool type cannot be redefined");
3420 pool_opts.type = PF_POOL_RANDOM;
3422 | SOURCEHASH hashkey {
3423 if (pool_opts.type) {
3424 yyerror("pool type cannot be redefined");
3427 pool_opts.type = PF_POOL_SRCHASH;
3431 if (pool_opts.type) {
3432 yyerror("pool type cannot be redefined");
3435 pool_opts.type = PF_POOL_ROUNDROBIN;
3438 if (pool_opts.staticport) {
3439 yyerror("static-port cannot be redefined");
3442 pool_opts.staticport = 1;
3445 if (filter_opts.marker & POM_STICKYADDRESS) {
3446 yyerror("sticky-address cannot be redefined");
3449 pool_opts.marker |= POM_STICKYADDRESS;
3450 pool_opts.opts |= PF_POOL_STICKYADDR;
3454 redirection : /* empty */ { $$ = NULL; }
3456 $$ = calloc(1, sizeof(struct redirection));
3458 err(1, "redirection: calloc");
3460 $$->rport.a = $$->rport.b = $$->rport.t = 0;
3462 | ARROW host PORT rport {
3463 $$ = calloc(1, sizeof(struct redirection));
3465 err(1, "redirection: calloc");
3471 /* ifdef __FreeBSD__ */
3472 natpass : /* empty */ { $$.b1 = $$.b2 = 0; $$.w2 = 0; }
3473 | PASS { $$.b1 = 1; $$.b2 = 0; $$.w2 = 0; }
3475 natpass : empty { $$.b1 = $$.b2 = 0; }
3476 | PASS { $$.b1 = 1; $$.b2 = 0; }
3478 | PASS log { $$.b1 = 1; $$.b2 = $2.log; $$.w2 = $2.logif; }
3481 nataction : no NAT natpass {
3483 yyerror("\"pass\" not valid with \"no\"");
3496 yyerror("\"pass\" not valid with \"no\"");
3509 natrule : nataction interface af proto fromto tag tagged rtable
3514 if (check_rulestate(PFCTL_STATE_NAT))
3517 memset(&r, 0, sizeof(r));
3526 if ($5.src.host && $5.src.host->af &&
3527 !$5.src.host->ifindex)
3528 r.af = $5.src.host->af;
3529 else if ($5.dst.host && $5.dst.host->af &&
3530 !$5.dst.host->ifindex)
3531 r.af = $5.dst.host->af;
3535 if (strlcpy(r.tagname, $6, PF_TAG_NAME_SIZE) >=
3537 yyerror("tag too long, max %u chars",
3538 PF_TAG_NAME_SIZE - 1);
3543 if (strlcpy(r.match_tagname, $7.name,
3544 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
3545 yyerror("tag too long, max %u chars",
3546 PF_TAG_NAME_SIZE - 1);
3549 r.match_tag_not = $7.neg;
3552 if (r.action == PF_NONAT || r.action == PF_NORDR) {
3554 yyerror("translation rule with 'no' "
3555 "does not need '->'");
3559 if ($9 == NULL || $9->host == NULL) {
3560 yyerror("translation rule requires '-> "
3564 if (!r.af && ! $9->host->ifindex)
3565 r.af = $9->host->af;
3567 remove_invalid_hosts(&$9->host, &r.af);
3568 if (invalid_redirect($9->host, r.af))
3570 if (check_netmask($9->host, r.af))
3573 r.rpool.proxy_port[0] = ntohs($9->rport.a);
3577 if (!$9->rport.b && $9->rport.t &&
3578 $5.dst.port != NULL) {
3579 r.rpool.proxy_port[1] =
3580 ntohs($9->rport.a) +
3582 $5.dst.port->port[1]) -
3584 $5.dst.port->port[0]));
3586 r.rpool.proxy_port[1] =
3590 r.rpool.proxy_port[1] =
3592 if (!r.rpool.proxy_port[0] &&
3593 !r.rpool.proxy_port[1]) {
3594 r.rpool.proxy_port[0] =
3595 PF_NAT_PROXY_PORT_LOW;
3596 r.rpool.proxy_port[1] =
3597 PF_NAT_PROXY_PORT_HIGH;
3598 } else if (!r.rpool.proxy_port[1])
3599 r.rpool.proxy_port[1] =
3600 r.rpool.proxy_port[0];
3606 r.rpool.opts = $10.type;
3607 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
3608 PF_POOL_NONE && ($9->host->next != NULL ||
3609 $9->host->addr.type == PF_ADDR_TABLE ||
3610 DYNIF_MULTIADDR($9->host->addr)))
3611 r.rpool.opts = PF_POOL_ROUNDROBIN;
3612 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
3613 PF_POOL_ROUNDROBIN &&
3614 disallow_table($9->host, "tables are only "
3615 "supported in round-robin redirection "
3618 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
3619 PF_POOL_ROUNDROBIN &&
3620 disallow_alias($9->host, "interface (%s) "
3621 "is only supported in round-robin "
3622 "redirection pools"))
3624 if ($9->host->next != NULL) {
3625 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
3626 PF_POOL_ROUNDROBIN) {
3627 yyerror("only round-robin "
3628 "valid for multiple "
3629 "redirection addresses");
3635 if ($10.key != NULL)
3636 memcpy(&r.rpool.key, $10.key,
3637 sizeof(struct pf_poolhashkey));
3640 r.rpool.opts |= $10.opts;
3642 if ($10.staticport) {
3643 if (r.action != PF_NAT) {
3644 yyerror("the 'static-port' option is "
3645 "only valid with nat rules");
3648 if (r.rpool.proxy_port[0] !=
3649 PF_NAT_PROXY_PORT_LOW &&
3650 r.rpool.proxy_port[1] !=
3651 PF_NAT_PROXY_PORT_HIGH) {
3652 yyerror("the 'static-port' option can't"
3653 " be used when specifying a port"
3657 r.rpool.proxy_port[0] = 0;
3658 r.rpool.proxy_port[1] = 0;
3661 expand_rule(&r, $2, $9 == NULL ? NULL : $9->host, $4,
3662 $5.src_os, $5.src.host, $5.src.port, $5.dst.host,
3663 $5.dst.port, 0, 0, 0, "");
3668 binatrule : no BINAT natpass interface af proto FROM host TO ipspec tag
3669 tagged rtable redirection
3671 struct pf_rule binat;
3672 struct pf_pooladdr *pa;
3674 if (check_rulestate(PFCTL_STATE_NAT))
3676 if (disallow_urpf_failed($10, "\"urpf-failed\" is not "
3677 "permitted as a binat destination"))
3680 memset(&binat, 0, sizeof(binat));
3683 yyerror("\"pass\" not valid with \"no\"");
3687 binat.action = PF_NOBINAT;
3689 binat.action = PF_BINAT;
3690 binat.natpass = $3.b1;
3692 binat.logif = $3.w2;
3694 if (!binat.af && $8 != NULL && $8->af)
3696 if (!binat.af && $10 != NULL && $10->af)
3699 if (!binat.af && $14 != NULL && $14->host)
3700 binat.af = $14->host->af;
3702 yyerror("address family (inet/inet6) "
3708 memcpy(binat.ifname, $4->ifname,
3709 sizeof(binat.ifname));
3710 binat.ifnot = $4->not;
3715 if (strlcpy(binat.tagname, $11,
3716 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
3717 yyerror("tag too long, max %u chars",
3718 PF_TAG_NAME_SIZE - 1);
3722 if (strlcpy(binat.match_tagname, $12.name,
3723 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
3724 yyerror("tag too long, max %u chars",
3725 PF_TAG_NAME_SIZE - 1);
3728 binat.match_tag_not = $12.neg;
3729 binat.rtableid = $13;
3732 binat.proto = $6->proto;
3736 if ($8 != NULL && disallow_table($8, "invalid use of "
3737 "table <%s> as the source address of a binat rule"))
3739 if ($8 != NULL && disallow_alias($8, "invalid use of "
3740 "interface (%s) as the source address of a binat "
3743 if ($14 != NULL && $14->host != NULL && disallow_table(
3744 $14->host, "invalid use of table <%s> as the "
3745 "redirect address of a binat rule"))
3747 if ($14 != NULL && $14->host != NULL && disallow_alias(
3748 $14->host, "invalid use of interface (%s) as the "
3749 "redirect address of a binat rule"))
3754 yyerror("multiple binat ip addresses");
3757 if ($8->addr.type == PF_ADDR_DYNIFTL)
3759 if ($8->af != binat.af) {
3760 yyerror("binat ip versions must match");
3763 if (check_netmask($8, binat.af))
3765 memcpy(&binat.src.addr, &$8->addr,
3766 sizeof(binat.src.addr));
3771 yyerror("multiple binat ip addresses");
3774 if ($10->af != binat.af && $10->af) {
3775 yyerror("binat ip versions must match");
3778 if (check_netmask($10, binat.af))
3780 memcpy(&binat.dst.addr, &$10->addr,
3781 sizeof(binat.dst.addr));
3782 binat.dst.neg = $10->not;
3786 if (binat.action == PF_NOBINAT) {
3788 yyerror("'no binat' rule does not need"
3793 if ($14 == NULL || $14->host == NULL) {
3794 yyerror("'binat' rule requires"
3799 remove_invalid_hosts(&$14->host, &binat.af);
3800 if (invalid_redirect($14->host, binat.af))
3802 if ($14->host->next != NULL) {
3803 yyerror("binat rule must redirect to "
3804 "a single address");
3807 if (check_netmask($14->host, binat.af))
3810 if (!PF_AZERO(&binat.src.addr.v.a.mask,
3812 !PF_AEQ(&binat.src.addr.v.a.mask,
3813 &$14->host->addr.v.a.mask, binat.af)) {
3814 yyerror("'binat' source mask and "
3815 "redirect mask must be the same");
3819 TAILQ_INIT(&binat.rpool.list);
3820 pa = calloc(1, sizeof(struct pf_pooladdr));
3822 err(1, "binat: calloc");
3823 pa->addr = $14->host->addr;
3825 TAILQ_INSERT_TAIL(&binat.rpool.list,
3831 pfctl_add_rule(pf, &binat, "");
3835 tag : /* empty */ { $$ = NULL; }
3836 | TAG STRING { $$ = $2; }
3839 tagged : /* empty */ { $$.neg = 0; $$.name = NULL; }
3840 | not TAGGED string { $$.neg = $1; $$.name = $3; }
3843 rtable : /* empty */ { $$ = -1; }
3846 yyerror("rtable id not supported in FreeBSD, yet");
3849 if ($2 > RT_TABLEID_MAX || $2 < 0) {
3850 yyerror("invalid rtable id");
3858 route_host : STRING {
3859 $$ = calloc(1, sizeof(struct node_host));
3861 err(1, "route_host: calloc");
3863 set_ipmask($$, 128);
3867 | '(' STRING host ')' {
3873 route_host_list : route_host { $$ = $1; }
3874 | route_host_list comma route_host {
3877 if ($1->af != $3->af) {
3878 yyerror("all pool addresses must be in the "
3879 "same address family");
3882 $1->tail->next = $3;
3883 $1->tail = $3->tail;
3888 routespec : route_host { $$ = $1; }
3889 | '{' route_host_list '}' { $$ = $2; }
3892 route : /* empty */ {
3899 $$.rt = PF_FASTROUTE;
3902 | ROUTETO routespec pool_opts {
3905 $$.pool_opts = $3.type | $3.opts;
3909 | REPLYTO routespec pool_opts {
3912 $$.pool_opts = $3.type | $3.opts;
3916 | DUPTO routespec pool_opts {
3919 $$.pool_opts = $3.type | $3.opts;
3925 timeout_spec : STRING number
3927 if (check_rulestate(PFCTL_STATE_OPTION)) {
3931 if (pfctl_set_timeout(pf, $1, $2, 0) != 0) {
3932 yyerror("unknown timeout %s", $1);
3940 timeout_list : timeout_list comma timeout_spec
3944 limit_spec : STRING number
3946 if (check_rulestate(PFCTL_STATE_OPTION)) {
3950 if (pfctl_set_limit(pf, $1, $2) != 0) {
3951 yyerror("unable to set limit %s %u", $1, $2);
3959 limit_list : limit_list comma limit_spec
3967 yesno : NO { $$ = 0; }
3969 if (!strcmp($1, "yes"))
3972 yyerror("invalid value '%s', expected 'yes' "
3981 unaryop : '=' { $$ = PF_OP_EQ; }
3982 | '!' '=' { $$ = PF_OP_NE; }
3983 | '<' '=' { $$ = PF_OP_LE; }
3984 | '<' { $$ = PF_OP_LT; }
3985 | '>' '=' { $$ = PF_OP_GE; }
3986 | '>' { $$ = PF_OP_GT; }
3992 yyerror(const char *fmt, ...)
3995 extern char *infile;
3999 fprintf(stderr, "%s:%d: ", infile, yylval.lineno);
4000 vfprintf(stderr, fmt, ap);
4001 fprintf(stderr, "\n");
4007 disallow_table(struct node_host *h, const char *fmt)
4009 for (; h != NULL; h = h->next)
4010 if (h->addr.type == PF_ADDR_TABLE) {
4011 yyerror(fmt, h->addr.v.tblname);
4018 disallow_urpf_failed(struct node_host *h, const char *fmt)
4020 for (; h != NULL; h = h->next)
4021 if (h->addr.type == PF_ADDR_URPFFAILED) {
4029 disallow_alias(struct node_host *h, const char *fmt)
4031 for (; h != NULL; h = h->next)
4032 if (DYNIF_MULTIADDR(h->addr)) {
4033 yyerror(fmt, h->addr.v.tblname);
4040 rule_consistent(struct pf_rule *r, int anchor_call)
4044 switch (r->action) {
4049 problems = filter_consistent(r, anchor_call);
4053 problems = nat_consistent(r);
4057 problems = rdr_consistent(r);
4068 filter_consistent(struct pf_rule *r, int anchor_call)
4072 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP &&
4073 (r->src.port_op || r->dst.port_op)) {
4074 yyerror("port only applies to tcp/udp");
4077 if (r->proto != IPPROTO_ICMP && r->proto != IPPROTO_ICMPV6 &&
4078 (r->type || r->code)) {
4079 yyerror("icmp-type/code only applies to icmp");
4082 if (!r->af && (r->type || r->code)) {
4083 yyerror("must indicate address family with icmp-type/code");
4086 if (r->overload_tblname[0] &&
4087 r->max_src_conn == 0 && r->max_src_conn_rate.seconds == 0) {
4088 yyerror("'overload' requires 'max-src-conn' "
4089 "or 'max-src-conn-rate'");
4092 if ((r->proto == IPPROTO_ICMP && r->af == AF_INET6) ||
4093 (r->proto == IPPROTO_ICMPV6 && r->af == AF_INET)) {
4094 yyerror("proto %s doesn't match address family %s",
4095 r->proto == IPPROTO_ICMP ? "icmp" : "icmp6",
4096 r->af == AF_INET ? "inet" : "inet6");
4099 if (r->allow_opts && r->action != PF_PASS) {
4100 yyerror("allow-opts can only be specified for pass rules");
4103 if (r->rule_flag & PFRULE_FRAGMENT && (r->src.port_op ||
4104 r->dst.port_op || r->flagset || r->type || r->code)) {
4105 yyerror("fragments can be filtered only on IP header fields");
4108 if (r->rule_flag & PFRULE_RETURNRST && r->proto != IPPROTO_TCP) {
4109 yyerror("return-rst can only be applied to TCP rules");
4112 if (r->max_src_nodes && !(r->rule_flag & PFRULE_RULESRCTRACK)) {
4113 yyerror("max-src-nodes requires 'source-track rule'");
4116 if (r->action == PF_DROP && r->keep_state) {
4117 yyerror("keep state on block rules doesn't make sense");
4120 if (r->rule_flag & PFRULE_STATESLOPPY &&
4121 (r->keep_state == PF_STATE_MODULATE ||
4122 r->keep_state == PF_STATE_SYNPROXY)) {
4123 yyerror("sloppy state matching cannot be used with "
4124 "synproxy state or modulate state");
4131 nat_consistent(struct pf_rule *r)
4133 return (0); /* yeah! */
4137 rdr_consistent(struct pf_rule *r)
4141 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP) {
4142 if (r->src.port_op) {
4143 yyerror("src port only applies to tcp/udp");
4146 if (r->dst.port_op) {
4147 yyerror("dst port only applies to tcp/udp");
4150 if (r->rpool.proxy_port[0]) {
4151 yyerror("rpool port only applies to tcp/udp");
4155 if (r->dst.port_op &&
4156 r->dst.port_op != PF_OP_EQ && r->dst.port_op != PF_OP_RRG) {
4157 yyerror("invalid port operator for rdr destination port");
4164 process_tabledef(char *name, struct table_opts *opts)
4166 struct pfr_buffer ab;
4167 struct node_tinit *ti;
4169 bzero(&ab, sizeof(ab));
4170 ab.pfrb_type = PFRB_ADDRS;
4171 SIMPLEQ_FOREACH(ti, &opts->init_nodes, entries) {
4173 if (pfr_buf_load(&ab, ti->file, 0, append_addr)) {
4175 yyerror("cannot load \"%s\": %s",
4176 ti->file, strerror(errno));
4178 yyerror("file \"%s\" contains bad data",
4183 if (append_addr_host(&ab, ti->host, 0, 0)) {
4184 yyerror("cannot create address buffer: %s",
4189 if (pf->opts & PF_OPT_VERBOSE)
4190 print_tabledef(name, opts->flags, opts->init_addr,
4192 if (!(pf->opts & PF_OPT_NOACTION) &&
4193 pfctl_define_table(name, opts->flags, opts->init_addr,
4194 pf->anchor->name, &ab, pf->anchor->ruleset.tticket)) {
4195 yyerror("cannot define table %s: %s", name,
4196 pfr_strerror(errno));
4212 /* macro gore, but you should've seen the prior indentation nightmare... */
4214 #define FREE_LIST(T,r) \
4217 while (node != NULL) { \
4219 node = node->next; \
4224 #define LOOP_THROUGH(T,n,r,C) \
4228 r = calloc(1, sizeof(T)); \
4230 err(1, "LOOP: calloc"); \
4234 while (n != NULL) { \
4243 expand_label_str(char *label, size_t len, const char *srch, const char *repl)
4248 if ((tmp = calloc(1, len)) == NULL)
4249 err(1, "expand_label_str: calloc");
4251 while ((q = strstr(p, srch)) != NULL) {
4253 if ((strlcat(tmp, p, len) >= len) ||
4254 (strlcat(tmp, repl, len) >= len))
4255 errx(1, "expand_label: label too long");
4259 if (strlcat(tmp, p, len) >= len)
4260 errx(1, "expand_label: label too long");
4261 strlcpy(label, tmp, len); /* always fits */
4266 expand_label_if(const char *name, char *label, size_t len, const char *ifname)
4268 if (strstr(label, name) != NULL) {
4270 expand_label_str(label, len, name, "any");
4272 expand_label_str(label, len, name, ifname);
4277 expand_label_addr(const char *name, char *label, size_t len, sa_family_t af,
4278 struct node_host *h)
4280 char tmp[64], tmp_not[66];
4282 if (strstr(label, name) != NULL) {
4283 switch (h->addr.type) {
4284 case PF_ADDR_DYNIFTL:
4285 snprintf(tmp, sizeof(tmp), "(%s)", h->addr.v.ifname);
4288 snprintf(tmp, sizeof(tmp), "<%s>", h->addr.v.tblname);
4290 case PF_ADDR_NOROUTE:
4291 snprintf(tmp, sizeof(tmp), "no-route");
4293 case PF_ADDR_URPFFAILED:
4294 snprintf(tmp, sizeof(tmp), "urpf-failed");
4296 case PF_ADDR_ADDRMASK:
4297 if (!af || (PF_AZERO(&h->addr.v.a.addr, af) &&
4298 PF_AZERO(&h->addr.v.a.mask, af)))
4299 snprintf(tmp, sizeof(tmp), "any");
4304 if (inet_ntop(af, &h->addr.v.a.addr, a,
4306 snprintf(tmp, sizeof(tmp), "?");
4308 bits = unmask(&h->addr.v.a.mask, af);
4309 if ((af == AF_INET && bits < 32) ||
4310 (af == AF_INET6 && bits < 128))
4311 snprintf(tmp, sizeof(tmp),
4314 snprintf(tmp, sizeof(tmp),
4320 snprintf(tmp, sizeof(tmp), "?");
4325 snprintf(tmp_not, sizeof(tmp_not), "! %s", tmp);
4326 expand_label_str(label, len, name, tmp_not);
4328 expand_label_str(label, len, name, tmp);
4333 expand_label_port(const char *name, char *label, size_t len,
4334 struct node_port *port)
4336 char a1[6], a2[6], op[13] = "";
4338 if (strstr(label, name) != NULL) {
4339 snprintf(a1, sizeof(a1), "%u", ntohs(port->port[0]));
4340 snprintf(a2, sizeof(a2), "%u", ntohs(port->port[1]));
4343 else if (port->op == PF_OP_IRG)
4344 snprintf(op, sizeof(op), "%s><%s", a1, a2);
4345 else if (port->op == PF_OP_XRG)
4346 snprintf(op, sizeof(op), "%s<>%s", a1, a2);
4347 else if (port->op == PF_OP_EQ)
4348 snprintf(op, sizeof(op), "%s", a1);
4349 else if (port->op == PF_OP_NE)
4350 snprintf(op, sizeof(op), "!=%s", a1);
4351 else if (port->op == PF_OP_LT)
4352 snprintf(op, sizeof(op), "<%s", a1);
4353 else if (port->op == PF_OP_LE)
4354 snprintf(op, sizeof(op), "<=%s", a1);
4355 else if (port->op == PF_OP_GT)
4356 snprintf(op, sizeof(op), ">%s", a1);
4357 else if (port->op == PF_OP_GE)
4358 snprintf(op, sizeof(op), ">=%s", a1);
4359 expand_label_str(label, len, name, op);
4364 expand_label_proto(const char *name, char *label, size_t len, u_int8_t proto)
4366 struct protoent *pe;
4369 if (strstr(label, name) != NULL) {
4370 pe = getprotobynumber(proto);
4372 expand_label_str(label, len, name, pe->p_name);
4374 snprintf(n, sizeof(n), "%u", proto);
4375 expand_label_str(label, len, name, n);
4381 expand_label_nr(const char *name, char *label, size_t len)
4385 if (strstr(label, name) != NULL) {
4386 snprintf(n, sizeof(n), "%u", pf->anchor->match);
4387 expand_label_str(label, len, name, n);
4392 expand_label(char *label, size_t len, const char *ifname, sa_family_t af,
4393 struct node_host *src_host, struct node_port *src_port,
4394 struct node_host *dst_host, struct node_port *dst_port,
4397 expand_label_if("$if", label, len, ifname);
4398 expand_label_addr("$srcaddr", label, len, af, src_host);
4399 expand_label_addr("$dstaddr", label, len, af, dst_host);
4400 expand_label_port("$srcport", label, len, src_port);
4401 expand_label_port("$dstport", label, len, dst_port);
4402 expand_label_proto("$proto", label, len, proto);
4403 expand_label_nr("$nr", label, len);
4407 expand_altq(struct pf_altq *a, struct node_if *interfaces,
4408 struct node_queue *nqueues, struct node_queue_bw bwspec,
4409 struct node_queue_opt *opts)
4411 struct pf_altq pa, pb;
4412 char qname[PF_QNAME_SIZE];
4413 struct node_queue *n;
4414 struct node_queue_bw bw;
4417 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
4418 FREE_LIST(struct node_if, interfaces);
4419 FREE_LIST(struct node_queue, nqueues);
4423 LOOP_THROUGH(struct node_if, interface, interfaces,
4424 memcpy(&pa, a, sizeof(struct pf_altq));
4425 if (strlcpy(pa.ifname, interface->ifname,
4426 sizeof(pa.ifname)) >= sizeof(pa.ifname))
4427 errx(1, "expand_altq: strlcpy");
4429 if (interface->not) {
4430 yyerror("altq on ! <interface> is not supported");
4433 if (eval_pfaltq(pf, &pa, &bwspec, opts))
4436 if (pfctl_add_altq(pf, &pa))
4439 if (pf->opts & PF_OPT_VERBOSE) {
4440 print_altq(&pf->paltq->altq, 0,
4442 if (nqueues && nqueues->tail) {
4444 LOOP_THROUGH(struct node_queue, queue,
4454 if (pa.scheduler == ALTQT_CBQ ||
4455 pa.scheduler == ALTQT_HFSC) {
4456 /* now create a root queue */
4457 memset(&pb, 0, sizeof(struct pf_altq));
4458 if (strlcpy(qname, "root_", sizeof(qname)) >=
4460 errx(1, "expand_altq: strlcpy");
4461 if (strlcat(qname, interface->ifname,
4462 sizeof(qname)) >= sizeof(qname))
4463 errx(1, "expand_altq: strlcat");
4464 if (strlcpy(pb.qname, qname,
4465 sizeof(pb.qname)) >= sizeof(pb.qname))
4466 errx(1, "expand_altq: strlcpy");
4467 if (strlcpy(pb.ifname, interface->ifname,
4468 sizeof(pb.ifname)) >= sizeof(pb.ifname))
4469 errx(1, "expand_altq: strlcpy");
4470 pb.qlimit = pa.qlimit;
4471 pb.scheduler = pa.scheduler;
4472 bw.bw_absolute = pa.ifbandwidth;
4474 if (eval_pfqueue(pf, &pb, &bw, opts))
4477 if (pfctl_add_altq(pf, &pb))
4481 LOOP_THROUGH(struct node_queue, queue, nqueues,
4482 n = calloc(1, sizeof(struct node_queue));
4484 err(1, "expand_altq: calloc");
4485 if (pa.scheduler == ALTQT_CBQ ||
4486 pa.scheduler == ALTQT_HFSC)
4487 if (strlcpy(n->parent, qname,
4488 sizeof(n->parent)) >=
4490 errx(1, "expand_altq: strlcpy");
4491 if (strlcpy(n->queue, queue->queue,
4492 sizeof(n->queue)) >= sizeof(n->queue))
4493 errx(1, "expand_altq: strlcpy");
4494 if (strlcpy(n->ifname, interface->ifname,
4495 sizeof(n->ifname)) >= sizeof(n->ifname))
4496 errx(1, "expand_altq: strlcpy");
4497 n->scheduler = pa.scheduler;
4503 queues->tail->next = n;
4509 FREE_LIST(struct node_if, interfaces);
4510 FREE_LIST(struct node_queue, nqueues);
4516 expand_queue(struct pf_altq *a, struct node_if *interfaces,
4517 struct node_queue *nqueues, struct node_queue_bw bwspec,
4518 struct node_queue_opt *opts)
4520 struct node_queue *n, *nq;
4525 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
4526 FREE_LIST(struct node_queue, nqueues);
4530 if (queues == NULL) {
4531 yyerror("queue %s has no parent", a->qname);
4532 FREE_LIST(struct node_queue, nqueues);
4536 LOOP_THROUGH(struct node_if, interface, interfaces,
4537 LOOP_THROUGH(struct node_queue, tqueue, queues,
4538 if (!strncmp(a->qname, tqueue->queue, PF_QNAME_SIZE) &&
4539 (interface->ifname[0] == 0 ||
4540 (!interface->not && !strncmp(interface->ifname,
4541 tqueue->ifname, IFNAMSIZ)) ||
4542 (interface->not && strncmp(interface->ifname,
4543 tqueue->ifname, IFNAMSIZ)))) {
4544 /* found ourself in queues */
4547 memcpy(&pa, a, sizeof(struct pf_altq));
4549 if (pa.scheduler != ALTQT_NONE &&
4550 pa.scheduler != tqueue->scheduler) {
4551 yyerror("exactly one scheduler type "
4552 "per interface allowed");
4555 pa.scheduler = tqueue->scheduler;
4557 /* scheduler dependent error checking */
4558 switch (pa.scheduler) {
4560 if (nqueues != NULL) {
4561 yyerror("priq queues cannot "
4562 "have child queues");
4565 if (bwspec.bw_absolute > 0 ||
4566 bwspec.bw_percent < 100) {
4567 yyerror("priq doesn't take "
4576 if (strlcpy(pa.ifname, tqueue->ifname,
4577 sizeof(pa.ifname)) >= sizeof(pa.ifname))
4578 errx(1, "expand_queue: strlcpy");
4579 if (strlcpy(pa.parent, tqueue->parent,
4580 sizeof(pa.parent)) >= sizeof(pa.parent))
4581 errx(1, "expand_queue: strlcpy");
4583 if (eval_pfqueue(pf, &pa, &bwspec, opts))
4586 if (pfctl_add_altq(pf, &pa))
4589 for (nq = nqueues; nq != NULL; nq = nq->next) {
4590 if (!strcmp(a->qname, nq->queue)) {
4591 yyerror("queue cannot have "
4597 sizeof(struct node_queue));
4599 err(1, "expand_queue: calloc");
4600 if (strlcpy(n->parent, a->qname,
4601 sizeof(n->parent)) >=
4603 errx(1, "expand_queue strlcpy");
4604 if (strlcpy(n->queue, nq->queue,
4605 sizeof(n->queue)) >=
4607 errx(1, "expand_queue strlcpy");
4608 if (strlcpy(n->ifname, tqueue->ifname,
4609 sizeof(n->ifname)) >=
4611 errx(1, "expand_queue strlcpy");
4612 n->scheduler = tqueue->scheduler;
4618 queues->tail->next = n;
4622 if ((pf->opts & PF_OPT_VERBOSE) && (
4623 (found == 1 && interface->ifname[0] == 0) ||
4624 (found > 0 && interface->ifname[0] != 0))) {
4625 print_queue(&pf->paltq->altq, 0,
4626 &bwspec, interface->ifname[0] != 0,
4628 if (nqueues && nqueues->tail) {
4630 LOOP_THROUGH(struct node_queue,
4643 FREE_LIST(struct node_queue, nqueues);
4644 FREE_LIST(struct node_if, interfaces);
4647 yyerror("queue %s has no parent", a->qname);
4658 expand_rule(struct pf_rule *r,
4659 struct node_if *interfaces, struct node_host *rpool_hosts,
4660 struct node_proto *protos, struct node_os *src_oses,
4661 struct node_host *src_hosts, struct node_port *src_ports,
4662 struct node_host *dst_hosts, struct node_port *dst_ports,
4663 struct node_uid *uids, struct node_gid *gids, struct node_icmp *icmp_types,
4664 const char *anchor_call)
4666 sa_family_t af = r->af;
4667 int added = 0, error = 0;
4668 char ifname[IF_NAMESIZE];
4669 char label[PF_RULE_LABEL_SIZE];
4670 char tagname[PF_TAG_NAME_SIZE];
4671 char match_tagname[PF_TAG_NAME_SIZE];
4672 struct pf_pooladdr *pa;
4673 struct node_host *h;
4674 u_int8_t flags, flagset, keep_state;
4676 if (strlcpy(label, r->label, sizeof(label)) >= sizeof(label))
4677 errx(1, "expand_rule: strlcpy");
4678 if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname))
4679 errx(1, "expand_rule: strlcpy");
4680 if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >=
4681 sizeof(match_tagname))
4682 errx(1, "expand_rule: strlcpy");
4684 flagset = r->flagset;
4685 keep_state = r->keep_state;
4687 LOOP_THROUGH(struct node_if, interface, interfaces,
4688 LOOP_THROUGH(struct node_proto, proto, protos,
4689 LOOP_THROUGH(struct node_icmp, icmp_type, icmp_types,
4690 LOOP_THROUGH(struct node_host, src_host, src_hosts,
4691 LOOP_THROUGH(struct node_port, src_port, src_ports,
4692 LOOP_THROUGH(struct node_os, src_os, src_oses,
4693 LOOP_THROUGH(struct node_host, dst_host, dst_hosts,
4694 LOOP_THROUGH(struct node_port, dst_port, dst_ports,
4695 LOOP_THROUGH(struct node_uid, uid, uids,
4696 LOOP_THROUGH(struct node_gid, gid, gids,
4699 /* for link-local IPv6 address, interface must match up */
4700 if ((r->af && src_host->af && r->af != src_host->af) ||
4701 (r->af && dst_host->af && r->af != dst_host->af) ||
4702 (src_host->af && dst_host->af &&
4703 src_host->af != dst_host->af) ||
4704 (src_host->ifindex && dst_host->ifindex &&
4705 src_host->ifindex != dst_host->ifindex) ||
4706 (src_host->ifindex && *interface->ifname &&
4707 src_host->ifindex != if_nametoindex(interface->ifname)) ||
4708 (dst_host->ifindex && *interface->ifname &&
4709 dst_host->ifindex != if_nametoindex(interface->ifname)))
4711 if (!r->af && src_host->af)
4712 r->af = src_host->af;
4713 else if (!r->af && dst_host->af)
4714 r->af = dst_host->af;
4716 if (*interface->ifname)
4717 strlcpy(r->ifname, interface->ifname,
4719 else if (if_indextoname(src_host->ifindex, ifname))
4720 strlcpy(r->ifname, ifname, sizeof(r->ifname));
4721 else if (if_indextoname(dst_host->ifindex, ifname))
4722 strlcpy(r->ifname, ifname, sizeof(r->ifname));
4724 memset(r->ifname, '\0', sizeof(r->ifname));
4726 if (strlcpy(r->label, label, sizeof(r->label)) >=
4728 errx(1, "expand_rule: strlcpy");
4729 if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >=
4731 errx(1, "expand_rule: strlcpy");
4732 if (strlcpy(r->match_tagname, match_tagname,
4733 sizeof(r->match_tagname)) >= sizeof(r->match_tagname))
4734 errx(1, "expand_rule: strlcpy");
4735 expand_label(r->label, PF_RULE_LABEL_SIZE, r->ifname, r->af,
4736 src_host, src_port, dst_host, dst_port, proto->proto);
4737 expand_label(r->tagname, PF_TAG_NAME_SIZE, r->ifname, r->af,
4738 src_host, src_port, dst_host, dst_port, proto->proto);
4739 expand_label(r->match_tagname, PF_TAG_NAME_SIZE, r->ifname,
4740 r->af, src_host, src_port, dst_host, dst_port,
4743 error += check_netmask(src_host, r->af);
4744 error += check_netmask(dst_host, r->af);
4746 r->ifnot = interface->not;
4747 r->proto = proto->proto;
4748 r->src.addr = src_host->addr;
4749 r->src.neg = src_host->not;
4750 r->src.port[0] = src_port->port[0];
4751 r->src.port[1] = src_port->port[1];
4752 r->src.port_op = src_port->op;
4753 r->dst.addr = dst_host->addr;
4754 r->dst.neg = dst_host->not;
4755 r->dst.port[0] = dst_port->port[0];
4756 r->dst.port[1] = dst_port->port[1];
4757 r->dst.port_op = dst_port->op;
4758 r->uid.op = uid->op;
4759 r->uid.uid[0] = uid->uid[0];
4760 r->uid.uid[1] = uid->uid[1];
4761 r->gid.op = gid->op;
4762 r->gid.gid[0] = gid->gid[0];
4763 r->gid.gid[1] = gid->gid[1];
4764 r->type = icmp_type->type;
4765 r->code = icmp_type->code;
4767 if ((keep_state == PF_STATE_MODULATE ||
4768 keep_state == PF_STATE_SYNPROXY) &&
4769 r->proto && r->proto != IPPROTO_TCP)
4770 r->keep_state = PF_STATE_NORMAL;
4772 r->keep_state = keep_state;
4774 if (r->proto && r->proto != IPPROTO_TCP) {
4779 r->flagset = flagset;
4781 if (icmp_type->proto && r->proto != icmp_type->proto) {
4782 yyerror("icmp-type mismatch");
4786 if (src_os && src_os->os) {
4787 r->os_fingerprint = pfctl_get_fingerprint(src_os->os);
4788 if ((pf->opts & PF_OPT_VERBOSE2) &&
4789 r->os_fingerprint == PF_OSFP_NOMATCH)
4791 "warning: unknown '%s' OS fingerprint\n",
4794 r->os_fingerprint = PF_OSFP_ANY;
4797 TAILQ_INIT(&r->rpool.list);
4798 for (h = rpool_hosts; h != NULL; h = h->next) {
4799 pa = calloc(1, sizeof(struct pf_pooladdr));
4801 err(1, "expand_rule: calloc");
4803 if (h->ifname != NULL) {
4804 if (strlcpy(pa->ifname, h->ifname,
4805 sizeof(pa->ifname)) >=
4807 errx(1, "expand_rule: strlcpy");
4810 TAILQ_INSERT_TAIL(&r->rpool.list, pa, entries);
4813 if (rule_consistent(r, anchor_call[0]) < 0 || error)
4814 yyerror("skipping rule due to errors");
4816 r->nr = pf->astack[pf->asd]->match++;
4817 pfctl_add_rule(pf, r, anchor_call);
4823 FREE_LIST(struct node_if, interfaces);
4824 FREE_LIST(struct node_proto, protos);
4825 FREE_LIST(struct node_host, src_hosts);
4826 FREE_LIST(struct node_port, src_ports);
4827 FREE_LIST(struct node_os, src_oses);
4828 FREE_LIST(struct node_host, dst_hosts);
4829 FREE_LIST(struct node_port, dst_ports);
4830 FREE_LIST(struct node_uid, uids);
4831 FREE_LIST(struct node_gid, gids);
4832 FREE_LIST(struct node_icmp, icmp_types);
4833 FREE_LIST(struct node_host, rpool_hosts);
4836 yyerror("rule expands to no valid combination");
4840 expand_skip_interface(struct node_if *interfaces)
4844 if (!interfaces || (!interfaces->next && !interfaces->not &&
4845 !strcmp(interfaces->ifname, "none"))) {
4846 if (pf->opts & PF_OPT_VERBOSE)
4847 printf("set skip on none\n");
4848 errs = pfctl_set_interface_flags(pf, "", PFI_IFLAG_SKIP, 0);
4852 if (pf->opts & PF_OPT_VERBOSE)
4853 printf("set skip on {");
4854 LOOP_THROUGH(struct node_if, interface, interfaces,
4855 if (pf->opts & PF_OPT_VERBOSE)
4856 printf(" %s", interface->ifname);
4857 if (interface->not) {
4858 yyerror("skip on ! <interface> is not supported");
4861 errs += pfctl_set_interface_flags(pf,
4862 interface->ifname, PFI_IFLAG_SKIP, 1);
4864 if (pf->opts & PF_OPT_VERBOSE)
4867 FREE_LIST(struct node_if, interfaces);
4879 check_rulestate(int desired_state)
4881 if (require_order && (rulestate > desired_state)) {
4882 yyerror("Rules must be in order: options, normalization, "
4883 "queueing, translation, filtering");
4886 rulestate = desired_state;
4891 kw_cmp(const void *k, const void *e)
4893 return (strcmp(k, ((const struct keywords *)e)->k_name));
4899 /* this has to be sorted always */
4900 static const struct keywords keywords[] = {
4902 { "allow-opts", ALLOWOPTS},
4904 { "anchor", ANCHOR},
4905 { "antispoof", ANTISPOOF},
4907 { "bandwidth", BANDWIDTH},
4909 { "binat-anchor", BINATANCHOR},
4910 { "bitmask", BITMASK},
4912 { "block-policy", BLOCKPOLICY},
4915 { "crop", FRAGCROP},
4918 { "drop-ovl", FRAGDROP},
4920 { "fastroute", FASTROUTE},
4921 { "file", FILENAME},
4922 { "fingerprints", FINGERPRINTS},
4924 { "floating", FLOATING},
4927 { "fragment", FRAGMENT},
4929 { "global", GLOBAL},
4932 { "hostid", HOSTID},
4933 { "icmp-type", ICMPTYPE},
4934 { "icmp6-type", ICMP6TYPE},
4935 { "if-bound", IFBOUND},
4942 { "linkshare", LINKSHARE},
4945 { "loginterface", LOGINTERFACE},
4947 { "max-mss", MAXMSS},
4948 { "max-src-conn", MAXSRCCONN},
4949 { "max-src-conn-rate", MAXSRCCONNRATE},
4950 { "max-src-nodes", MAXSRCNODES},
4951 { "max-src-states", MAXSRCSTATES},
4952 { "min-ttl", MINTTL},
4953 { "modulate", MODULATE},
4955 { "nat-anchor", NATANCHOR},
4958 { "no-route", NOROUTE},
4959 { "no-sync", NOSYNC},
4961 { "optimization", OPTIMIZATION},
4964 { "overload", OVERLOAD},
4967 { "priority", PRIORITY},
4969 { "probability", PROBABILITY},
4971 { "qlimit", QLIMIT},
4974 { "random", RANDOM},
4975 { "random-id", RANDOMID},
4977 { "rdr-anchor", RDRANCHOR},
4978 { "realtime", REALTIME},
4979 { "reassemble", REASSEMBLE},
4980 { "reply-to", REPLYTO},
4981 { "require-order", REQUIREORDER},
4982 { "return", RETURN},
4983 { "return-icmp", RETURNICMP},
4984 { "return-icmp6", RETURNICMP6},
4985 { "return-rst", RETURNRST},
4986 { "round-robin", ROUNDROBIN},
4988 { "route-to", ROUTETO},
4989 { "rtable", RTABLE},
4991 { "ruleset-optimization", RULESET_OPTIMIZATION},
4995 { "sloppy", SLOPPY},
4996 { "source-hash", SOURCEHASH},
4997 { "source-track", SOURCETRACK},
4999 { "state-policy", STATEPOLICY},
5000 { "static-port", STATICPORT},
5001 { "sticky-address", STICKYADDRESS},
5002 { "synproxy", SYNPROXY},
5005 { "tagged", TAGGED},
5006 { "tbrsize", TBRSIZE},
5007 { "timeout", TIMEOUT},
5011 { "upperlimit", UPPERLIMIT},
5012 { "urpf-failed", URPFFAILED},
5015 const struct keywords *p;
5017 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
5018 sizeof(keywords[0]), kw_cmp);
5022 fprintf(stderr, "%s: %d\n", s, p->k_val);
5026 fprintf(stderr, "string: %s\n", s);
5031 #define MAXPUSHBACK 128
5035 char pushback_buffer[MAXPUSHBACK];
5036 int pushback_index = 0;
5044 /* Read character from the parsebuffer instead of input. */
5045 if (parseindex >= 0) {
5046 c = parsebuf[parseindex++];
5055 return (pushback_buffer[--pushback_index]);
5057 while ((c = getc(f)) == '\\') {
5063 yylval.lineno = lineno;
5066 if (c == '\t' || c == ' ') {
5067 /* Compress blanks to a single space. */
5070 } while (c == '\t' || c == ' ');
5085 if (parseindex >= 0)
5088 if (pushback_index < MAXPUSHBACK-1)
5089 return (pushback_buffer[pushback_index++] = c);
5102 /* skip to either EOF or the first real EOL */
5125 while ((c = lgetc(fin)) == ' ')
5128 yylval.lineno = lineno;
5130 while ((c = lgetc(fin)) != '\n' && c != EOF)
5132 if (c == '$' && parsebuf == NULL) {
5134 if ((c = lgetc(fin)) == EOF)
5137 if (p + 1 >= buf + sizeof(buf) - 1) {
5138 yyerror("string too long");
5141 if (isalnum(c) || c == '_') {
5151 yyerror("macro '%s' not defined", buf);
5164 if ((c = lgetc(fin)) == EOF)
5174 if (p + 1 >= buf + sizeof(buf) - 1) {
5175 yyerror("string too long");
5180 yylval.v.string = strdup(buf);
5181 if (yylval.v.string == NULL)
5182 err(1, "yylex: strdup");
5187 yylval.v.i = PF_OP_XRG;
5188 return (PORTBINARY);
5195 yylval.v.i = PF_OP_IRG;
5196 return (PORTBINARY);
5208 #define allowed_in_string(x) \
5209 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
5210 x != '{' && x != '}' && x != '<' && x != '>' && \
5211 x != '!' && x != '=' && x != '/' && x != '#' && \
5214 if (isalnum(c) || c == ':' || c == '_') {
5217 if ((unsigned)(p-buf) >= sizeof(buf)) {
5218 yyerror("string too long");
5221 } while ((c = lgetc(fin)) != EOF && (allowed_in_string(c)));
5224 if ((token = lookup(buf)) == STRING)
5225 if ((yylval.v.string = strdup(buf)) == NULL)
5226 err(1, "yylex: strdup");
5230 yylval.lineno = lineno;
5239 parse_rules(FILE *input, struct pfctl *xpf)
5241 struct sym *sym, *next;
5247 rulestate = PFCTL_STATE_NONE;
5248 returnicmpdefault = (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
5249 returnicmp6default =
5250 (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
5251 blockpolicy = PFRULE_DROP;
5256 /* Free macros and check which have not been used. */
5257 for (sym = TAILQ_FIRST(&symhead); sym != NULL; sym = next) {
5258 next = TAILQ_NEXT(sym, entries);
5259 if ((pf->opts & PF_OPT_VERBOSE2) && !sym->used)
5260 fprintf(stderr, "warning: macro '%s' not "
5261 "used\n", sym->nam);
5264 TAILQ_REMOVE(&symhead, sym, entries);
5268 return (errors ? -1 : 0);
5272 * Over-designed efficiency is a French and German concept, so how about
5273 * we wait until they discover this ugliness and make it all fancy.
5276 symset(const char *nam, const char *val, int persist)
5280 for (sym = TAILQ_FIRST(&symhead); sym && strcmp(nam, sym->nam);
5281 sym = TAILQ_NEXT(sym, entries))
5285 if (sym->persist == 1)
5290 TAILQ_REMOVE(&symhead, sym, entries);
5294 if ((sym = calloc(1, sizeof(*sym))) == NULL)
5297 sym->nam = strdup(nam);
5298 if (sym->nam == NULL) {
5302 sym->val = strdup(val);
5303 if (sym->val == NULL) {
5309 sym->persist = persist;
5310 TAILQ_INSERT_TAIL(&symhead, sym, entries);
5315 pfctl_cmdline_symset(char *s)
5320 if ((val = strrchr(s, '=')) == NULL)
5323 if ((sym = malloc(strlen(s) - strlen(val) + 1)) == NULL)
5324 err(1, "pfctl_cmdline_symset: malloc");
5326 strlcpy(sym, s, strlen(s) - strlen(val) + 1);
5328 ret = symset(sym, val + 1, 1);
5335 symget(const char *nam)
5339 TAILQ_FOREACH(sym, &symhead, entries)
5340 if (strcmp(nam, sym->nam) == 0) {
5348 mv_rules(struct pf_ruleset *src, struct pf_ruleset *dst)
5353 for (i = 0; i < PF_RULESET_MAX; ++i) {
5354 while ((r = TAILQ_FIRST(src->rules[i].active.ptr))
5356 TAILQ_REMOVE(src->rules[i].active.ptr, r, entries);
5357 TAILQ_INSERT_TAIL(dst->rules[i].active.ptr, r, entries);
5358 dst->anchor->match++;
5360 src->anchor->match = 0;
5361 while ((r = TAILQ_FIRST(src->rules[i].inactive.ptr))
5363 TAILQ_REMOVE(src->rules[i].inactive.ptr, r, entries);
5364 TAILQ_INSERT_TAIL(dst->rules[i].inactive.ptr,
5371 decide_address_family(struct node_host *n, sa_family_t *af)
5373 if (*af != 0 || n == NULL)
5376 while ((n = n->next) != NULL) {
5385 remove_invalid_hosts(struct node_host **nh, sa_family_t *af)
5387 struct node_host *n = *nh, *prev = NULL;
5390 if (*af && n->af && n->af != *af) {
5391 /* unlink and free n */
5392 struct node_host *next = n->next;
5394 /* adjust tail pointer */
5395 if (n == (*nh)->tail)
5397 /* adjust previous node's next pointer */
5403 if (n->ifname != NULL)
5417 invalid_redirect(struct node_host *nh, sa_family_t af)
5420 struct node_host *n;
5422 /* tables and dyniftl are ok without an address family */
5423 for (n = nh; n != NULL; n = n->next) {
5424 if (n->addr.type != PF_ADDR_TABLE &&
5425 n->addr.type != PF_ADDR_DYNIFTL) {
5426 yyerror("address family not given and "
5427 "translation address expands to multiple "
5428 "address families");
5434 yyerror("no translation address with matching address family "
5442 atoul(char *s, u_long *ulvalp)
5448 ulval = strtoul(s, &ep, 0);
5449 if (s[0] == '\0' || *ep != '\0')
5451 if (errno == ERANGE && ulval == ULONG_MAX)
5463 if (atoul(n, &ulval) == 0) {
5464 if (ulval > 65535) {
5465 yyerror("illegal port value %lu", ulval);
5468 return (htons(ulval));
5470 s = getservbyname(n, "tcp");
5472 s = getservbyname(n, "udp");
5474 yyerror("unknown port %s", n);
5482 rule_label(struct pf_rule *r, char *s)
5485 if (strlcpy(r->label, s, sizeof(r->label)) >=
5487 yyerror("rule label too long (max %d chars)",
5488 sizeof(r->label)-1);
5496 parseicmpspec(char *w, sa_family_t af)
5498 const struct icmpcodeent *p;
5503 icmptype = returnicmpdefault >> 8;
5505 icmptype = returnicmp6default >> 8;
5507 if (atoul(w, &ulval) == -1) {
5508 if ((p = geticmpcodebyname(icmptype, w, af)) == NULL) {
5509 yyerror("unknown icmp code %s", w);
5515 yyerror("invalid icmp code %lu", ulval);
5518 return (icmptype << 8 | ulval);
5522 pfctl_load_anchors(int dev, struct pfctl *pf, struct pfr_buffer *trans)
5524 struct loadanchors *la;
5527 TAILQ_FOREACH(la, &loadanchorshead, entries) {
5528 if (pf->opts & PF_OPT_VERBOSE)
5529 fprintf(stderr, "\nLoading anchor %s from %s\n",
5530 la->anchorname, la->filename);
5531 if ((fin = pfctl_fopen(la->filename, "r")) == NULL) {
5532 warn("%s", la->filename);
5535 if (pfctl_rules(dev, la->filename, fin, pf->opts, pf->optimize,
5536 la->anchorname, trans) == -1)