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 };
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
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_TIMEOUT:
1895 if (o->data.timeout.number ==
1896 PFTM_ADAPTIVE_START ||
1897 o->data.timeout.number ==
1900 if (r.timeout[o->data.timeout.number]) {
1901 yyerror("state timeout %s "
1902 "multiple definitions",
1903 pf_timeouts[o->data.
1904 timeout.number].name);
1907 r.timeout[o->data.timeout.number] =
1908 o->data.timeout.seconds;
1914 /* 'flags S/SA' by default on stateful rules */
1915 if (!r.action && !r.flags && !r.flagset &&
1916 !$9.fragment && !($9.marker & FOM_FLAGS) &&
1918 r.flags = parse_flags("S");
1919 r.flagset = parse_flags("SA");
1921 if (!adaptive && r.max_states) {
1922 r.timeout[PFTM_ADAPTIVE_START] =
1923 (r.max_states / 10) * 6;
1924 r.timeout[PFTM_ADAPTIVE_END] =
1925 (r.max_states / 10) * 12;
1927 if (r.rule_flag & PFRULE_SRCTRACK) {
1928 if (srctrack == PF_SRCTRACK_GLOBAL &&
1930 yyerror("'max-src-nodes' is "
1931 "incompatible with "
1932 "'source-track global'");
1935 if (srctrack == PF_SRCTRACK_GLOBAL &&
1937 yyerror("'max-src-conn' is "
1938 "incompatible with "
1939 "'source-track global'");
1942 if (srctrack == PF_SRCTRACK_GLOBAL &&
1943 r.max_src_conn_rate.seconds) {
1944 yyerror("'max-src-conn-rate' is "
1945 "incompatible with "
1946 "'source-track global'");
1949 if (r.timeout[PFTM_SRC_NODE] <
1950 r.max_src_conn_rate.seconds)
1951 r.timeout[PFTM_SRC_NODE] =
1952 r.max_src_conn_rate.seconds;
1953 r.rule_flag |= PFRULE_SRCTRACK;
1954 if (srctrack == PF_SRCTRACK_RULE)
1955 r.rule_flag |= PFRULE_RULESRCTRACK;
1957 if (r.keep_state && !statelock)
1958 r.rule_flag |= default_statelock;
1961 r.rule_flag |= PFRULE_FRAGMENT;
1962 r.allow_opts = $9.allowopts;
1964 decide_address_family($8.src.host, &r.af);
1965 decide_address_family($8.dst.host, &r.af);
1969 yyerror("direction must be explicit "
1970 "with rules that specify routing");
1974 r.rpool.opts = $5.pool_opts;
1976 memcpy(&r.rpool.key, $5.key,
1977 sizeof(struct pf_poolhashkey));
1979 if (r.rt && r.rt != PF_FASTROUTE) {
1980 decide_address_family($5.host, &r.af);
1981 remove_invalid_hosts(&$5.host, &r.af);
1982 if ($5.host == NULL) {
1983 yyerror("no routing address with "
1984 "matching address family found.");
1987 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
1988 PF_POOL_NONE && ($5.host->next != NULL ||
1989 $5.host->addr.type == PF_ADDR_TABLE ||
1990 DYNIF_MULTIADDR($5.host->addr)))
1991 r.rpool.opts |= PF_POOL_ROUNDROBIN;
1992 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
1993 PF_POOL_ROUNDROBIN &&
1994 disallow_table($5.host, "tables are only "
1995 "supported in round-robin routing pools"))
1997 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
1998 PF_POOL_ROUNDROBIN &&
1999 disallow_alias($5.host, "interface (%s) "
2000 "is only supported in round-robin "
2003 if ($5.host->next != NULL) {
2004 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2005 PF_POOL_ROUNDROBIN) {
2006 yyerror("r.rpool.opts must "
2007 "be PF_POOL_ROUNDROBIN");
2012 if ($9.queues.qname != NULL) {
2013 if (strlcpy(r.qname, $9.queues.qname,
2014 sizeof(r.qname)) >= sizeof(r.qname)) {
2015 yyerror("rule qname too long (max "
2016 "%d chars)", sizeof(r.qname)-1);
2019 free($9.queues.qname);
2021 if ($9.queues.pqname != NULL) {
2022 if (strlcpy(r.pqname, $9.queues.pqname,
2023 sizeof(r.pqname)) >= sizeof(r.pqname)) {
2024 yyerror("rule pqname too long (max "
2025 "%d chars)", sizeof(r.pqname)-1);
2028 free($9.queues.pqname);
2031 expand_rule(&r, $4, $5.host, $7, $8.src_os,
2032 $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
2033 $9.uid, $9.gid, $9.icmpspec, "");
2038 bzero(&filter_opts, sizeof filter_opts);
2039 filter_opts.rtableid = -1;
2042 { $$ = filter_opts; }
2044 bzero(&filter_opts, sizeof filter_opts);
2045 filter_opts.rtableid = -1;
2050 filter_opts_l : filter_opts_l filter_opt
2054 filter_opt : USER uids {
2055 if (filter_opts.uid)
2056 $2->tail->next = filter_opts.uid;
2057 filter_opts.uid = $2;
2060 if (filter_opts.gid)
2061 $2->tail->next = filter_opts.gid;
2062 filter_opts.gid = $2;
2065 if (filter_opts.marker & FOM_FLAGS) {
2066 yyerror("flags cannot be redefined");
2069 filter_opts.marker |= FOM_FLAGS;
2070 filter_opts.flags.b1 |= $1.b1;
2071 filter_opts.flags.b2 |= $1.b2;
2072 filter_opts.flags.w |= $1.w;
2073 filter_opts.flags.w2 |= $1.w2;
2076 if (filter_opts.marker & FOM_ICMP) {
2077 yyerror("icmp-type cannot be redefined");
2080 filter_opts.marker |= FOM_ICMP;
2081 filter_opts.icmpspec = $1;
2084 if (filter_opts.marker & FOM_TOS) {
2085 yyerror("tos cannot be redefined");
2088 filter_opts.marker |= FOM_TOS;
2089 filter_opts.tos = $1;
2092 if (filter_opts.marker & FOM_KEEP) {
2093 yyerror("modulate or keep cannot be redefined");
2096 filter_opts.marker |= FOM_KEEP;
2097 filter_opts.keep.action = $1.action;
2098 filter_opts.keep.options = $1.options;
2101 filter_opts.fragment = 1;
2104 filter_opts.allowopts = 1;
2107 if (filter_opts.label) {
2108 yyerror("label cannot be redefined");
2111 filter_opts.label = $1;
2114 if (filter_opts.queues.qname) {
2115 yyerror("queue cannot be redefined");
2118 filter_opts.queues = $1;
2121 filter_opts.tag = $2;
2123 | not TAGGED string {
2124 filter_opts.match_tag = $3;
2125 filter_opts.match_tag_not = $1;
2127 | PROBABILITY STRING {
2129 double p = strtod($2, &e);
2136 yyerror("invalid probability: %s", $2);
2140 p = floor(p * (UINT_MAX+1.0) + 0.5);
2141 if (p < 1.0 || p >= (UINT_MAX+1.0)) {
2142 yyerror("invalid probability: %s", $2);
2146 filter_opts.prob = (u_int32_t)p;
2151 if ($2 > RT_TABLEID_MAX || $2 < 0) {
2152 yyerror("invalid rtable id");
2156 filter_opts.rtableid = $2;
2160 action : PASS { $$.b1 = PF_PASS; $$.b2 = $$.w = 0; }
2161 | BLOCK blockspec { $$ = $2; $$.b1 = PF_DROP; }
2164 blockspec : /* empty */ {
2165 $$.b2 = blockpolicy;
2166 $$.w = returnicmpdefault;
2167 $$.w2 = returnicmp6default;
2170 $$.b2 = PFRULE_DROP;
2175 $$.b2 = PFRULE_RETURNRST;
2179 | RETURNRST '(' TTL number ')' {
2181 yyerror("illegal ttl value %d", $4);
2184 $$.b2 = PFRULE_RETURNRST;
2189 $$.b2 = PFRULE_RETURNICMP;
2190 $$.w = returnicmpdefault;
2191 $$.w2 = returnicmp6default;
2194 $$.b2 = PFRULE_RETURNICMP;
2195 $$.w = returnicmpdefault;
2196 $$.w2 = returnicmp6default;
2198 | RETURNICMP '(' STRING ')' {
2199 $$.b2 = PFRULE_RETURNICMP;
2200 if (!($$.w = parseicmpspec($3, AF_INET))) {
2205 $$.w2 = returnicmp6default;
2207 | RETURNICMP6 '(' STRING ')' {
2208 $$.b2 = PFRULE_RETURNICMP;
2209 $$.w = returnicmpdefault;
2210 if (!($$.w2 = parseicmpspec($3, AF_INET6))) {
2216 | RETURNICMP '(' STRING comma STRING ')' {
2217 $$.b2 = PFRULE_RETURNICMP;
2218 if (!($$.w = parseicmpspec($3, AF_INET)) ||
2219 !($$.w2 = parseicmpspec($5, AF_INET6))) {
2228 $$.b2 = PFRULE_RETURN;
2229 $$.w = returnicmpdefault;
2230 $$.w2 = returnicmp6default;
2234 dir : /* empty */ { $$ = 0; }
2235 | IN { $$ = PF_IN; }
2236 | OUT { $$ = PF_OUT; }
2239 quick : /* empty */ { $$.quick = 0; }
2240 | QUICK { $$.quick = 1; }
2243 logquick : /* empty */ { $$.log = 0; $$.quick = 0; $$.logif = 0; }
2244 | log { $$ = $1; $$.quick = 0; }
2245 | QUICK { $$.quick = 1; $$.log = 0; $$.logif = 0; }
2246 | log QUICK { $$ = $1; $$.quick = 1; }
2247 | QUICK log { $$ = $2; $$.quick = 1; }
2250 log : LOG { $$.log = PF_LOG; $$.logif = 0; }
2251 | LOG '(' logopts ')' {
2252 $$.log = PF_LOG | $3.log;
2253 $$.logif = $3.logif;
2257 logopts : logopt { $$ = $1; }
2258 | logopts comma logopt {
2259 $$.log = $1.log | $3.log;
2260 $$.logif = $3.logif;
2262 $$.logif = $1.logif;
2266 logopt : ALL { $$.log = PF_LOG_ALL; $$.logif = 0; }
2267 | USER { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
2268 | GROUP { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
2274 if (strncmp($2, "pflog", 5)) {
2275 yyerror("%s: should be a pflog interface", $2);
2279 i = strtonum($2 + 5, 0, 255, &errstr);
2281 yyerror("%s: %s", $2, errstr);
2290 interface : /* empty */ { $$ = NULL; }
2291 | ON if_item_not { $$ = $2; }
2292 | ON '{' if_list '}' { $$ = $3; }
2295 if_list : if_item_not { $$ = $1; }
2296 | if_list comma if_item_not {
2297 $1->tail->next = $3;
2303 if_item_not : not if_item { $$ = $2; $$->not = $1; }
2307 struct node_host *n;
2309 $$ = calloc(1, sizeof(struct node_if));
2311 err(1, "if_item: calloc");
2312 if (strlcpy($$->ifname, $1, sizeof($$->ifname)) >=
2313 sizeof($$->ifname)) {
2316 yyerror("interface name too long");
2320 if ((n = ifa_exists($1)) != NULL)
2321 $$->ifa_flags = n->ifa_flags;
2330 af : /* empty */ { $$ = 0; }
2331 | INET { $$ = AF_INET; }
2332 | INET6 { $$ = AF_INET6; }
2335 proto : /* empty */ { $$ = NULL; }
2336 | PROTO proto_item { $$ = $2; }
2337 | PROTO '{' proto_list '}' { $$ = $3; }
2340 proto_list : proto_item { $$ = $1; }
2341 | proto_list comma proto_item {
2342 $1->tail->next = $3;
2348 proto_item : STRING {
2352 if (atoul($1, &ulval) == 0) {
2354 yyerror("protocol outside range");
2358 pr = (u_int8_t)ulval;
2362 p = getprotobyname($1);
2364 yyerror("unknown protocol %s", $1);
2372 yyerror("proto 0 cannot be used");
2375 $$ = calloc(1, sizeof(struct node_proto));
2377 err(1, "proto_item: calloc");
2398 os : /* empty */ { $$ = NULL; }
2399 | OS xos { $$ = $2; }
2400 | OS '{' os_list '}' { $$ = $3; }
2404 $$ = calloc(1, sizeof(struct node_os));
2406 err(1, "os: calloc");
2412 os_list : xos { $$ = $1; }
2413 | os_list comma xos {
2414 $1->tail->next = $3;
2420 from : /* empty */ {
2434 if (disallow_urpf_failed($2.host, "\"urpf-failed\" is "
2435 "not permitted in a destination address"))
2441 ipportspec : ipspec {
2445 | ipspec PORT portspec {
2455 ipspec : ANY { $$ = NULL; }
2456 | xhost { $$ = $1; }
2457 | '{' host_list '}' { $$ = $2; }
2460 host_list : ipspec { $$ = $1; }
2461 | host_list comma ipspec {
2464 else if ($1 == NULL)
2467 $1->tail->next = $3;
2468 $1->tail = $3->tail;
2475 struct node_host *n;
2477 for (n = $2; n != NULL; n = n->next)
2482 $$ = calloc(1, sizeof(struct node_host));
2484 err(1, "xhost: calloc");
2485 $$->addr.type = PF_ADDR_NOROUTE;
2491 $$ = calloc(1, sizeof(struct node_host));
2493 err(1, "xhost: calloc");
2494 $$->addr.type = PF_ADDR_URPFFAILED;
2502 if (($$ = host($1)) == NULL) {
2503 /* error. "any" is handled elsewhere */
2505 yyerror("could not parse host specification");
2511 | STRING '/' number {
2514 if (asprintf(&buf, "%s/%u", $1, $3) == -1)
2515 err(1, "host: asprintf");
2517 if (($$ = host(buf)) == NULL) {
2518 /* error. "any" is handled elsewhere */
2520 yyerror("could not parse host specification");
2526 | dynaddr '/' number {
2527 struct node_host *n;
2530 for (n = $1; n != NULL; n = n->next)
2534 if (strlen($2) >= PF_TABLE_NAME_SIZE) {
2535 yyerror("table name '%s' too long", $2);
2539 $$ = calloc(1, sizeof(struct node_host));
2541 err(1, "host: calloc");
2542 $$->addr.type = PF_ADDR_TABLE;
2543 if (strlcpy($$->addr.v.tblname, $2,
2544 sizeof($$->addr.v.tblname)) >=
2545 sizeof($$->addr.v.tblname))
2546 errx(1, "host: strlcpy");
2552 $$ = calloc(1, sizeof(struct node_host));
2555 err(1, "host: calloc");
2557 $$->addr.type = PF_ADDR_RTLABEL;
2558 if (strlcpy($$->addr.v.rtlabelname, $2,
2559 sizeof($$->addr.v.rtlabelname)) >=
2560 sizeof($$->addr.v.rtlabelname)) {
2561 yyerror("route label too long, max %u chars",
2562 sizeof($$->addr.v.rtlabelname) - 1);
2576 if (atoul($1, &ulval) == -1) {
2577 yyerror("%s is not a number", $1);
2586 dynaddr : '(' STRING ')' {
2591 if (!isalpha(op[0])) {
2592 yyerror("invalid interface name '%s'", op);
2596 while ((p = strrchr($2, ':')) != NULL) {
2597 if (!strcmp(p+1, "network"))
2598 flags |= PFI_AFLAG_NETWORK;
2599 else if (!strcmp(p+1, "broadcast"))
2600 flags |= PFI_AFLAG_BROADCAST;
2601 else if (!strcmp(p+1, "peer"))
2602 flags |= PFI_AFLAG_PEER;
2603 else if (!strcmp(p+1, "0"))
2604 flags |= PFI_AFLAG_NOALIAS;
2606 yyerror("interface %s has bad modifier",
2613 if (flags & (flags - 1) & PFI_AFLAG_MODEMASK) {
2615 yyerror("illegal combination of "
2616 "interface modifiers");
2619 $$ = calloc(1, sizeof(struct node_host));
2621 err(1, "address: calloc");
2623 set_ipmask($$, 128);
2624 $$->addr.type = PF_ADDR_DYNIFTL;
2625 $$->addr.iflags = flags;
2626 if (strlcpy($$->addr.v.ifname, $2,
2627 sizeof($$->addr.v.ifname)) >=
2628 sizeof($$->addr.v.ifname)) {
2631 yyerror("interface name too long");
2640 portspec : port_item { $$ = $1; }
2641 | '{' port_list '}' { $$ = $2; }
2644 port_list : port_item { $$ = $1; }
2645 | port_list comma port_item {
2646 $1->tail->next = $3;
2653 $$ = calloc(1, sizeof(struct node_port));
2655 err(1, "port_item: calloc");
2667 yyerror("':' cannot be used with an other "
2671 $$ = calloc(1, sizeof(struct node_port));
2673 err(1, "port_item: calloc");
2680 | port PORTBINARY port {
2682 yyerror("':' cannot be used with an other "
2686 $$ = calloc(1, sizeof(struct node_port));
2688 err(1, "port_item: calloc");
2698 char *p = strchr($1, ':');
2701 if (($$.a = getservice($1)) == -1) {
2710 if ((port[0] = getservice($1)) == -1 ||
2711 (port[1] = getservice(p)) == -1) {
2723 uids : uid_item { $$ = $1; }
2724 | '{' uid_list '}' { $$ = $2; }
2727 uid_list : uid_item { $$ = $1; }
2728 | uid_list comma uid_item {
2729 $1->tail->next = $3;
2736 $$ = calloc(1, sizeof(struct node_uid));
2738 err(1, "uid_item: calloc");
2746 if ($2 == UID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
2747 yyerror("user unknown requires operator = or "
2751 $$ = calloc(1, sizeof(struct node_uid));
2753 err(1, "uid_item: calloc");
2760 | uid PORTBINARY uid {
2761 if ($1 == UID_MAX || $3 == UID_MAX) {
2762 yyerror("user unknown requires operator = or "
2766 $$ = calloc(1, sizeof(struct node_uid));
2768 err(1, "uid_item: calloc");
2780 if (atoul($1, &ulval) == -1) {
2781 if (!strcmp($1, "unknown"))
2786 if ((pw = getpwnam($1)) == NULL) {
2787 yyerror("unknown user %s", $1);
2794 if (ulval >= UID_MAX) {
2796 yyerror("illegal uid value %lu", ulval);
2805 gids : gid_item { $$ = $1; }
2806 | '{' gid_list '}' { $$ = $2; }
2809 gid_list : gid_item { $$ = $1; }
2810 | gid_list comma gid_item {
2811 $1->tail->next = $3;
2818 $$ = calloc(1, sizeof(struct node_gid));
2820 err(1, "gid_item: calloc");
2828 if ($2 == GID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
2829 yyerror("group unknown requires operator = or "
2833 $$ = calloc(1, sizeof(struct node_gid));
2835 err(1, "gid_item: calloc");
2842 | gid PORTBINARY gid {
2843 if ($1 == GID_MAX || $3 == GID_MAX) {
2844 yyerror("group unknown requires operator = or "
2848 $$ = calloc(1, sizeof(struct node_gid));
2850 err(1, "gid_item: calloc");
2862 if (atoul($1, &ulval) == -1) {
2863 if (!strcmp($1, "unknown"))
2868 if ((grp = getgrnam($1)) == NULL) {
2869 yyerror("unknown group %s", $1);
2876 if (ulval >= GID_MAX) {
2877 yyerror("illegal gid value %lu", ulval);
2890 if ((f = parse_flags($1)) < 0) {
2891 yyerror("bad flags %s", $1);
2900 flags : FLAGS flag '/' flag { $$.b1 = $2.b1; $$.b2 = $4.b1; }
2901 | FLAGS '/' flag { $$.b1 = 0; $$.b2 = $3.b1; }
2902 | FLAGS ANY { $$.b1 = 0; $$.b2 = 0; }
2905 icmpspec : ICMPTYPE icmp_item { $$ = $2; }
2906 | ICMPTYPE '{' icmp_list '}' { $$ = $3; }
2907 | ICMP6TYPE icmp6_item { $$ = $2; }
2908 | ICMP6TYPE '{' icmp6_list '}' { $$ = $3; }
2911 icmp_list : icmp_item { $$ = $1; }
2912 | icmp_list comma icmp_item {
2913 $1->tail->next = $3;
2919 icmp6_list : icmp6_item { $$ = $1; }
2920 | icmp6_list comma icmp6_item {
2921 $1->tail->next = $3;
2927 icmp_item : icmptype {
2928 $$ = calloc(1, sizeof(struct node_icmp));
2930 err(1, "icmp_item: calloc");
2933 $$->proto = IPPROTO_ICMP;
2937 | icmptype CODE STRING {
2938 const struct icmpcodeent *p;
2941 if (atoul($3, &ulval) == 0) {
2944 yyerror("illegal icmp-code %lu", ulval);
2948 if ((p = geticmpcodebyname($1-1, $3,
2949 AF_INET)) == NULL) {
2950 yyerror("unknown icmp-code %s", $3);
2957 $$ = calloc(1, sizeof(struct node_icmp));
2959 err(1, "icmp_item: calloc");
2961 $$->code = ulval + 1;
2962 $$->proto = IPPROTO_ICMP;
2968 icmp6_item : icmp6type {
2969 $$ = calloc(1, sizeof(struct node_icmp));
2971 err(1, "icmp_item: calloc");
2974 $$->proto = IPPROTO_ICMPV6;
2978 | icmp6type CODE STRING {
2979 const struct icmpcodeent *p;
2982 if (atoul($3, &ulval) == 0) {
2984 yyerror("illegal icmp6-code %lu",
2990 if ((p = geticmpcodebyname($1-1, $3,
2991 AF_INET6)) == NULL) {
2992 yyerror("unknown icmp6-code %s", $3);
2999 $$ = calloc(1, sizeof(struct node_icmp));
3001 err(1, "icmp_item: calloc");
3003 $$->code = ulval + 1;
3004 $$->proto = IPPROTO_ICMPV6;
3011 const struct icmptypeent *p;
3014 if (atoul($1, &ulval) == 0) {
3016 yyerror("illegal icmp-type %lu", ulval);
3022 if ((p = geticmptypebyname($1, AF_INET)) ==
3024 yyerror("unknown icmp-type %s", $1);
3034 icmp6type : STRING {
3035 const struct icmptypeent *p;
3038 if (atoul($1, &ulval) == 0) {
3040 yyerror("illegal icmp6-type %lu",
3047 if ((p = geticmptypebyname($1, AF_INET6)) ==
3049 yyerror("unknown icmp6-type %s", $1);
3060 if (!strcmp($2, "lowdelay"))
3061 $$ = IPTOS_LOWDELAY;
3062 else if (!strcmp($2, "throughput"))
3063 $$ = IPTOS_THROUGHPUT;
3064 else if (!strcmp($2, "reliability"))
3065 $$ = IPTOS_RELIABILITY;
3066 else if ($2[0] == '0' && $2[1] == 'x')
3067 $$ = strtoul($2, NULL, 16);
3069 $$ = strtoul($2, NULL, 10);
3070 if (!$$ || $$ > 255) {
3071 yyerror("illegal tos value %s", $2);
3079 sourcetrack : SOURCETRACK { $$ = PF_SRCTRACK; }
3080 | SOURCETRACK GLOBAL { $$ = PF_SRCTRACK_GLOBAL; }
3081 | SOURCETRACK RULE { $$ = PF_SRCTRACK_RULE; }
3084 statelock : IFBOUND {
3085 $$ = PFRULE_IFBOUND;
3096 | KEEP STATE state_opt_spec {
3097 $$.action = PF_STATE_NORMAL;
3100 | MODULATE STATE state_opt_spec {
3101 $$.action = PF_STATE_MODULATE;
3104 | SYNPROXY STATE state_opt_spec {
3105 $$.action = PF_STATE_SYNPROXY;
3110 flush : /* empty */ { $$ = 0; }
3111 | FLUSH { $$ = PF_FLUSH; }
3113 $$ = PF_FLUSH | PF_FLUSH_GLOBAL;
3117 state_opt_spec : '(' state_opt_list ')' { $$ = $2; }
3118 | /* empty */ { $$ = NULL; }
3121 state_opt_list : state_opt_item { $$ = $1; }
3122 | state_opt_list comma state_opt_item {
3123 $1->tail->next = $3;
3129 state_opt_item : MAXIMUM number {
3130 $$ = calloc(1, sizeof(struct node_state_opt));
3132 err(1, "state_opt_item: calloc");
3133 $$->type = PF_STATE_OPT_MAX;
3134 $$->data.max_states = $2;
3139 $$ = calloc(1, sizeof(struct node_state_opt));
3141 err(1, "state_opt_item: calloc");
3142 $$->type = PF_STATE_OPT_NOSYNC;
3146 | MAXSRCSTATES number {
3147 $$ = calloc(1, sizeof(struct node_state_opt));
3149 err(1, "state_opt_item: calloc");
3150 $$->type = PF_STATE_OPT_MAX_SRC_STATES;
3151 $$->data.max_src_states = $2;
3155 | MAXSRCCONN number {
3156 $$ = calloc(1, sizeof(struct node_state_opt));
3158 err(1, "state_opt_item: calloc");
3159 $$->type = PF_STATE_OPT_MAX_SRC_CONN;
3160 $$->data.max_src_conn = $2;
3164 | MAXSRCCONNRATE number '/' number {
3165 $$ = calloc(1, sizeof(struct node_state_opt));
3167 err(1, "state_opt_item: calloc");
3168 $$->type = PF_STATE_OPT_MAX_SRC_CONN_RATE;
3169 $$->data.max_src_conn_rate.limit = $2;
3170 $$->data.max_src_conn_rate.seconds = $4;
3174 | OVERLOAD '<' STRING '>' flush {
3175 if (strlen($3) >= PF_TABLE_NAME_SIZE) {
3176 yyerror("table name '%s' too long", $3);
3180 $$ = calloc(1, sizeof(struct node_state_opt));
3182 err(1, "state_opt_item: calloc");
3183 if (strlcpy($$->data.overload.tblname, $3,
3184 PF_TABLE_NAME_SIZE) >= PF_TABLE_NAME_SIZE)
3185 errx(1, "state_opt_item: strlcpy");
3187 $$->type = PF_STATE_OPT_OVERLOAD;
3188 $$->data.overload.flush = $5;
3192 | MAXSRCNODES number {
3193 $$ = calloc(1, sizeof(struct node_state_opt));
3195 err(1, "state_opt_item: calloc");
3196 $$->type = PF_STATE_OPT_MAX_SRC_NODES;
3197 $$->data.max_src_nodes = $2;
3202 $$ = calloc(1, sizeof(struct node_state_opt));
3204 err(1, "state_opt_item: calloc");
3205 $$->type = PF_STATE_OPT_SRCTRACK;
3206 $$->data.src_track = $1;
3211 $$ = calloc(1, sizeof(struct node_state_opt));
3213 err(1, "state_opt_item: calloc");
3214 $$->type = PF_STATE_OPT_STATELOCK;
3215 $$->data.statelock = $1;
3222 for (i = 0; pf_timeouts[i].name &&
3223 strcmp(pf_timeouts[i].name, $1); ++i)
3225 if (!pf_timeouts[i].name) {
3226 yyerror("illegal timeout name %s", $1);
3230 if (strchr(pf_timeouts[i].name, '.') == NULL) {
3231 yyerror("illegal state timeout %s", $1);
3236 $$ = calloc(1, sizeof(struct node_state_opt));
3238 err(1, "state_opt_item: calloc");
3239 $$->type = PF_STATE_OPT_TIMEOUT;
3240 $$->data.timeout.number = pf_timeouts[i].timeout;
3241 $$->data.timeout.seconds = $2;
3247 label : LABEL STRING {
3252 qname : QUEUE STRING {
3255 | QUEUE '(' STRING ')' {
3258 | QUEUE '(' STRING comma STRING ')' {
3264 no : /* empty */ { $$ = 0; }
3269 char *p = strchr($1, ':');
3272 if (($$.a = getservice($1)) == -1) {
3277 } else if (!strcmp(p+1, "*")) {
3279 if (($$.a = getservice($1)) == -1) {
3287 if (($$.a = getservice($1)) == -1 ||
3288 ($$.b = getservice(p)) == -1) {
3300 redirspec : host { $$ = $1; }
3301 | '{' redir_host_list '}' { $$ = $2; }
3304 redir_host_list : host { $$ = $1; }
3305 | redir_host_list comma host {
3306 $1->tail->next = $3;
3307 $1->tail = $3->tail;
3312 redirpool : /* empty */ { $$ = NULL; }
3314 $$ = calloc(1, sizeof(struct redirection));
3316 err(1, "redirection: calloc");
3318 $$->rport.a = $$->rport.b = $$->rport.t = 0;
3320 | ARROW redirspec PORT rport {
3321 $$ = calloc(1, sizeof(struct redirection));
3323 err(1, "redirection: calloc");
3329 hashkey : /* empty */
3331 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3333 err(1, "hashkey: calloc");
3334 $$->key32[0] = arc4random();
3335 $$->key32[1] = arc4random();
3336 $$->key32[2] = arc4random();
3337 $$->key32[3] = arc4random();
3341 if (!strncmp($1, "0x", 2)) {
3342 if (strlen($1) != 34) {
3344 yyerror("hex key must be 128 bits "
3345 "(32 hex digits) long");
3348 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3350 err(1, "hashkey: calloc");
3352 if (sscanf($1, "0x%8x%8x%8x%8x",
3353 &$$->key32[0], &$$->key32[1],
3354 &$$->key32[2], &$$->key32[3]) != 4) {
3357 yyerror("invalid hex key");
3363 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3365 err(1, "hashkey: calloc");
3367 MD5Update(&context, (unsigned char *)$1,
3369 MD5Final((unsigned char *)$$, &context);
3370 HTONL($$->key32[0]);
3371 HTONL($$->key32[1]);
3372 HTONL($$->key32[2]);
3373 HTONL($$->key32[3]);
3379 pool_opts : { bzero(&pool_opts, sizeof pool_opts); }
3383 bzero(&pool_opts, sizeof pool_opts);
3388 pool_opts_l : pool_opts_l pool_opt
3392 pool_opt : BITMASK {
3393 if (pool_opts.type) {
3394 yyerror("pool type cannot be redefined");
3397 pool_opts.type = PF_POOL_BITMASK;
3400 if (pool_opts.type) {
3401 yyerror("pool type cannot be redefined");
3404 pool_opts.type = PF_POOL_RANDOM;
3406 | SOURCEHASH hashkey {
3407 if (pool_opts.type) {
3408 yyerror("pool type cannot be redefined");
3411 pool_opts.type = PF_POOL_SRCHASH;
3415 if (pool_opts.type) {
3416 yyerror("pool type cannot be redefined");
3419 pool_opts.type = PF_POOL_ROUNDROBIN;
3422 if (pool_opts.staticport) {
3423 yyerror("static-port cannot be redefined");
3426 pool_opts.staticport = 1;
3429 if (filter_opts.marker & POM_STICKYADDRESS) {
3430 yyerror("sticky-address cannot be redefined");
3433 pool_opts.marker |= POM_STICKYADDRESS;
3434 pool_opts.opts |= PF_POOL_STICKYADDR;
3438 redirection : /* empty */ { $$ = NULL; }
3440 $$ = calloc(1, sizeof(struct redirection));
3442 err(1, "redirection: calloc");
3444 $$->rport.a = $$->rport.b = $$->rport.t = 0;
3446 | ARROW host PORT rport {
3447 $$ = calloc(1, sizeof(struct redirection));
3449 err(1, "redirection: calloc");
3455 /* ifdef __FreeBSD__ */
3456 natpass : /* empty */ { $$.b1 = $$.b2 = 0; $$.w2 = 0; }
3457 | PASS { $$.b1 = 1; $$.b2 = 0; $$.w2 = 0; }
3459 natpass : empty { $$.b1 = $$.b2 = 0; }
3460 | PASS { $$.b1 = 1; $$.b2 = 0; }
3462 | PASS log { $$.b1 = 1; $$.b2 = $2.log; $$.w2 = $2.logif; }
3465 nataction : no NAT natpass {
3467 yyerror("\"pass\" not valid with \"no\"");
3480 yyerror("\"pass\" not valid with \"no\"");
3493 natrule : nataction interface af proto fromto tag tagged rtable
3498 if (check_rulestate(PFCTL_STATE_NAT))
3501 memset(&r, 0, sizeof(r));
3510 if ($5.src.host && $5.src.host->af &&
3511 !$5.src.host->ifindex)
3512 r.af = $5.src.host->af;
3513 else if ($5.dst.host && $5.dst.host->af &&
3514 !$5.dst.host->ifindex)
3515 r.af = $5.dst.host->af;
3519 if (strlcpy(r.tagname, $6, PF_TAG_NAME_SIZE) >=
3521 yyerror("tag too long, max %u chars",
3522 PF_TAG_NAME_SIZE - 1);
3527 if (strlcpy(r.match_tagname, $7.name,
3528 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
3529 yyerror("tag too long, max %u chars",
3530 PF_TAG_NAME_SIZE - 1);
3533 r.match_tag_not = $7.neg;
3536 if (r.action == PF_NONAT || r.action == PF_NORDR) {
3538 yyerror("translation rule with 'no' "
3539 "does not need '->'");
3543 if ($9 == NULL || $9->host == NULL) {
3544 yyerror("translation rule requires '-> "
3548 if (!r.af && ! $9->host->ifindex)
3549 r.af = $9->host->af;
3551 remove_invalid_hosts(&$9->host, &r.af);
3552 if (invalid_redirect($9->host, r.af))
3554 if (check_netmask($9->host, r.af))
3557 r.rpool.proxy_port[0] = ntohs($9->rport.a);
3561 if (!$9->rport.b && $9->rport.t &&
3562 $5.dst.port != NULL) {
3563 r.rpool.proxy_port[1] =
3564 ntohs($9->rport.a) +
3566 $5.dst.port->port[1]) -
3568 $5.dst.port->port[0]));
3570 r.rpool.proxy_port[1] =
3574 r.rpool.proxy_port[1] =
3576 if (!r.rpool.proxy_port[0] &&
3577 !r.rpool.proxy_port[1]) {
3578 r.rpool.proxy_port[0] =
3579 PF_NAT_PROXY_PORT_LOW;
3580 r.rpool.proxy_port[1] =
3581 PF_NAT_PROXY_PORT_HIGH;
3582 } else if (!r.rpool.proxy_port[1])
3583 r.rpool.proxy_port[1] =
3584 r.rpool.proxy_port[0];
3590 r.rpool.opts = $10.type;
3591 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
3592 PF_POOL_NONE && ($9->host->next != NULL ||
3593 $9->host->addr.type == PF_ADDR_TABLE ||
3594 DYNIF_MULTIADDR($9->host->addr)))
3595 r.rpool.opts = PF_POOL_ROUNDROBIN;
3596 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
3597 PF_POOL_ROUNDROBIN &&
3598 disallow_table($9->host, "tables are only "
3599 "supported in round-robin redirection "
3602 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
3603 PF_POOL_ROUNDROBIN &&
3604 disallow_alias($9->host, "interface (%s) "
3605 "is only supported in round-robin "
3606 "redirection pools"))
3608 if ($9->host->next != NULL) {
3609 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
3610 PF_POOL_ROUNDROBIN) {
3611 yyerror("only round-robin "
3612 "valid for multiple "
3613 "redirection addresses");
3619 if ($10.key != NULL)
3620 memcpy(&r.rpool.key, $10.key,
3621 sizeof(struct pf_poolhashkey));
3624 r.rpool.opts |= $10.opts;
3626 if ($10.staticport) {
3627 if (r.action != PF_NAT) {
3628 yyerror("the 'static-port' option is "
3629 "only valid with nat rules");
3632 if (r.rpool.proxy_port[0] !=
3633 PF_NAT_PROXY_PORT_LOW &&
3634 r.rpool.proxy_port[1] !=
3635 PF_NAT_PROXY_PORT_HIGH) {
3636 yyerror("the 'static-port' option can't"
3637 " be used when specifying a port"
3641 r.rpool.proxy_port[0] = 0;
3642 r.rpool.proxy_port[1] = 0;
3645 expand_rule(&r, $2, $9 == NULL ? NULL : $9->host, $4,
3646 $5.src_os, $5.src.host, $5.src.port, $5.dst.host,
3647 $5.dst.port, 0, 0, 0, "");
3652 binatrule : no BINAT natpass interface af proto FROM host TO ipspec tag
3653 tagged rtable redirection
3655 struct pf_rule binat;
3656 struct pf_pooladdr *pa;
3658 if (check_rulestate(PFCTL_STATE_NAT))
3660 if (disallow_urpf_failed($10, "\"urpf-failed\" is not "
3661 "permitted as a binat destination"))
3664 memset(&binat, 0, sizeof(binat));
3667 yyerror("\"pass\" not valid with \"no\"");
3671 binat.action = PF_NOBINAT;
3673 binat.action = PF_BINAT;
3674 binat.natpass = $3.b1;
3676 binat.logif = $3.w2;
3678 if (!binat.af && $8 != NULL && $8->af)
3680 if (!binat.af && $10 != NULL && $10->af)
3683 if (!binat.af && $14 != NULL && $14->host)
3684 binat.af = $14->host->af;
3686 yyerror("address family (inet/inet6) "
3692 memcpy(binat.ifname, $4->ifname,
3693 sizeof(binat.ifname));
3694 binat.ifnot = $4->not;
3699 if (strlcpy(binat.tagname, $11,
3700 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
3701 yyerror("tag too long, max %u chars",
3702 PF_TAG_NAME_SIZE - 1);
3706 if (strlcpy(binat.match_tagname, $12.name,
3707 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
3708 yyerror("tag too long, max %u chars",
3709 PF_TAG_NAME_SIZE - 1);
3712 binat.match_tag_not = $12.neg;
3713 binat.rtableid = $13;
3716 binat.proto = $6->proto;
3720 if ($8 != NULL && disallow_table($8, "invalid use of "
3721 "table <%s> as the source address of a binat rule"))
3723 if ($8 != NULL && disallow_alias($8, "invalid use of "
3724 "interface (%s) as the source address of a binat "
3727 if ($14 != NULL && $14->host != NULL && disallow_table(
3728 $14->host, "invalid use of table <%s> as the "
3729 "redirect address of a binat rule"))
3731 if ($14 != NULL && $14->host != NULL && disallow_alias(
3732 $14->host, "invalid use of interface (%s) as the "
3733 "redirect address of a binat rule"))
3738 yyerror("multiple binat ip addresses");
3741 if ($8->addr.type == PF_ADDR_DYNIFTL)
3743 if ($8->af != binat.af) {
3744 yyerror("binat ip versions must match");
3747 if (check_netmask($8, binat.af))
3749 memcpy(&binat.src.addr, &$8->addr,
3750 sizeof(binat.src.addr));
3755 yyerror("multiple binat ip addresses");
3758 if ($10->af != binat.af && $10->af) {
3759 yyerror("binat ip versions must match");
3762 if (check_netmask($10, binat.af))
3764 memcpy(&binat.dst.addr, &$10->addr,
3765 sizeof(binat.dst.addr));
3766 binat.dst.neg = $10->not;
3770 if (binat.action == PF_NOBINAT) {
3772 yyerror("'no binat' rule does not need"
3777 if ($14 == NULL || $14->host == NULL) {
3778 yyerror("'binat' rule requires"
3783 remove_invalid_hosts(&$14->host, &binat.af);
3784 if (invalid_redirect($14->host, binat.af))
3786 if ($14->host->next != NULL) {
3787 yyerror("binat rule must redirect to "
3788 "a single address");
3791 if (check_netmask($14->host, binat.af))
3794 if (!PF_AZERO(&binat.src.addr.v.a.mask,
3796 !PF_AEQ(&binat.src.addr.v.a.mask,
3797 &$14->host->addr.v.a.mask, binat.af)) {
3798 yyerror("'binat' source mask and "
3799 "redirect mask must be the same");
3803 TAILQ_INIT(&binat.rpool.list);
3804 pa = calloc(1, sizeof(struct pf_pooladdr));
3806 err(1, "binat: calloc");
3807 pa->addr = $14->host->addr;
3809 TAILQ_INSERT_TAIL(&binat.rpool.list,
3815 pfctl_add_rule(pf, &binat, "");
3819 tag : /* empty */ { $$ = NULL; }
3820 | TAG STRING { $$ = $2; }
3823 tagged : /* empty */ { $$.neg = 0; $$.name = NULL; }
3824 | not TAGGED string { $$.neg = $1; $$.name = $3; }
3827 rtable : /* empty */ { $$ = -1; }
3830 yyerror("rtable id not supported in FreeBSD, yet");
3833 if ($2 > RT_TABLEID_MAX || $2 < 0) {
3834 yyerror("invalid rtable id");
3842 route_host : STRING {
3843 $$ = calloc(1, sizeof(struct node_host));
3845 err(1, "route_host: calloc");
3847 set_ipmask($$, 128);
3851 | '(' STRING host ')' {
3857 route_host_list : route_host { $$ = $1; }
3858 | route_host_list comma route_host {
3861 if ($1->af != $3->af) {
3862 yyerror("all pool addresses must be in the "
3863 "same address family");
3866 $1->tail->next = $3;
3867 $1->tail = $3->tail;
3872 routespec : route_host { $$ = $1; }
3873 | '{' route_host_list '}' { $$ = $2; }
3876 route : /* empty */ {
3883 $$.rt = PF_FASTROUTE;
3886 | ROUTETO routespec pool_opts {
3889 $$.pool_opts = $3.type | $3.opts;
3893 | REPLYTO routespec pool_opts {
3896 $$.pool_opts = $3.type | $3.opts;
3900 | DUPTO routespec pool_opts {
3903 $$.pool_opts = $3.type | $3.opts;
3909 timeout_spec : STRING number
3911 if (check_rulestate(PFCTL_STATE_OPTION)) {
3915 if (pfctl_set_timeout(pf, $1, $2, 0) != 0) {
3916 yyerror("unknown timeout %s", $1);
3924 timeout_list : timeout_list comma timeout_spec
3928 limit_spec : STRING number
3930 if (check_rulestate(PFCTL_STATE_OPTION)) {
3934 if (pfctl_set_limit(pf, $1, $2) != 0) {
3935 yyerror("unable to set limit %s %u", $1, $2);
3943 limit_list : limit_list comma limit_spec
3951 yesno : NO { $$ = 0; }
3953 if (!strcmp($1, "yes"))
3956 yyerror("invalid value '%s', expected 'yes' "
3965 unaryop : '=' { $$ = PF_OP_EQ; }
3966 | '!' '=' { $$ = PF_OP_NE; }
3967 | '<' '=' { $$ = PF_OP_LE; }
3968 | '<' { $$ = PF_OP_LT; }
3969 | '>' '=' { $$ = PF_OP_GE; }
3970 | '>' { $$ = PF_OP_GT; }
3976 yyerror(const char *fmt, ...)
3979 extern char *infile;
3983 fprintf(stderr, "%s:%d: ", infile, yylval.lineno);
3984 vfprintf(stderr, fmt, ap);
3985 fprintf(stderr, "\n");
3991 disallow_table(struct node_host *h, const char *fmt)
3993 for (; h != NULL; h = h->next)
3994 if (h->addr.type == PF_ADDR_TABLE) {
3995 yyerror(fmt, h->addr.v.tblname);
4002 disallow_urpf_failed(struct node_host *h, const char *fmt)
4004 for (; h != NULL; h = h->next)
4005 if (h->addr.type == PF_ADDR_URPFFAILED) {
4013 disallow_alias(struct node_host *h, const char *fmt)
4015 for (; h != NULL; h = h->next)
4016 if (DYNIF_MULTIADDR(h->addr)) {
4017 yyerror(fmt, h->addr.v.tblname);
4024 rule_consistent(struct pf_rule *r, int anchor_call)
4028 switch (r->action) {
4033 problems = filter_consistent(r, anchor_call);
4037 problems = nat_consistent(r);
4041 problems = rdr_consistent(r);
4052 filter_consistent(struct pf_rule *r, int anchor_call)
4056 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP &&
4057 (r->src.port_op || r->dst.port_op)) {
4058 yyerror("port only applies to tcp/udp");
4061 if (r->proto != IPPROTO_ICMP && r->proto != IPPROTO_ICMPV6 &&
4062 (r->type || r->code)) {
4063 yyerror("icmp-type/code only applies to icmp");
4066 if (!r->af && (r->type || r->code)) {
4067 yyerror("must indicate address family with icmp-type/code");
4070 if (r->overload_tblname[0] &&
4071 r->max_src_conn == 0 && r->max_src_conn_rate.seconds == 0) {
4072 yyerror("'overload' requires 'max-src-conn' "
4073 "or 'max-src-conn-rate'");
4076 if ((r->proto == IPPROTO_ICMP && r->af == AF_INET6) ||
4077 (r->proto == IPPROTO_ICMPV6 && r->af == AF_INET)) {
4078 yyerror("proto %s doesn't match address family %s",
4079 r->proto == IPPROTO_ICMP ? "icmp" : "icmp6",
4080 r->af == AF_INET ? "inet" : "inet6");
4083 if (r->allow_opts && r->action != PF_PASS) {
4084 yyerror("allow-opts can only be specified for pass rules");
4087 if (r->rule_flag & PFRULE_FRAGMENT && (r->src.port_op ||
4088 r->dst.port_op || r->flagset || r->type || r->code)) {
4089 yyerror("fragments can be filtered only on IP header fields");
4092 if (r->rule_flag & PFRULE_RETURNRST && r->proto != IPPROTO_TCP) {
4093 yyerror("return-rst can only be applied to TCP rules");
4096 if (r->max_src_nodes && !(r->rule_flag & PFRULE_RULESRCTRACK)) {
4097 yyerror("max-src-nodes requires 'source-track rule'");
4100 if (r->action == PF_DROP && r->keep_state) {
4101 yyerror("keep state on block rules doesn't make sense");
4108 nat_consistent(struct pf_rule *r)
4110 return (0); /* yeah! */
4114 rdr_consistent(struct pf_rule *r)
4118 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP) {
4119 if (r->src.port_op) {
4120 yyerror("src port only applies to tcp/udp");
4123 if (r->dst.port_op) {
4124 yyerror("dst port only applies to tcp/udp");
4127 if (r->rpool.proxy_port[0]) {
4128 yyerror("rpool port only applies to tcp/udp");
4132 if (r->dst.port_op &&
4133 r->dst.port_op != PF_OP_EQ && r->dst.port_op != PF_OP_RRG) {
4134 yyerror("invalid port operator for rdr destination port");
4141 process_tabledef(char *name, struct table_opts *opts)
4143 struct pfr_buffer ab;
4144 struct node_tinit *ti;
4146 bzero(&ab, sizeof(ab));
4147 ab.pfrb_type = PFRB_ADDRS;
4148 SIMPLEQ_FOREACH(ti, &opts->init_nodes, entries) {
4150 if (pfr_buf_load(&ab, ti->file, 0, append_addr)) {
4152 yyerror("cannot load \"%s\": %s",
4153 ti->file, strerror(errno));
4155 yyerror("file \"%s\" contains bad data",
4160 if (append_addr_host(&ab, ti->host, 0, 0)) {
4161 yyerror("cannot create address buffer: %s",
4166 if (pf->opts & PF_OPT_VERBOSE)
4167 print_tabledef(name, opts->flags, opts->init_addr,
4169 if (!(pf->opts & PF_OPT_NOACTION) &&
4170 pfctl_define_table(name, opts->flags, opts->init_addr,
4171 pf->anchor->name, &ab, pf->anchor->ruleset.tticket)) {
4172 yyerror("cannot define table %s: %s", name,
4173 pfr_strerror(errno));
4189 /* macro gore, but you should've seen the prior indentation nightmare... */
4191 #define FREE_LIST(T,r) \
4194 while (node != NULL) { \
4196 node = node->next; \
4201 #define LOOP_THROUGH(T,n,r,C) \
4205 r = calloc(1, sizeof(T)); \
4207 err(1, "LOOP: calloc"); \
4211 while (n != NULL) { \
4220 expand_label_str(char *label, size_t len, const char *srch, const char *repl)
4225 if ((tmp = calloc(1, len)) == NULL)
4226 err(1, "expand_label_str: calloc");
4228 while ((q = strstr(p, srch)) != NULL) {
4230 if ((strlcat(tmp, p, len) >= len) ||
4231 (strlcat(tmp, repl, len) >= len))
4232 errx(1, "expand_label: label too long");
4236 if (strlcat(tmp, p, len) >= len)
4237 errx(1, "expand_label: label too long");
4238 strlcpy(label, tmp, len); /* always fits */
4243 expand_label_if(const char *name, char *label, size_t len, const char *ifname)
4245 if (strstr(label, name) != NULL) {
4247 expand_label_str(label, len, name, "any");
4249 expand_label_str(label, len, name, ifname);
4254 expand_label_addr(const char *name, char *label, size_t len, sa_family_t af,
4255 struct node_host *h)
4257 char tmp[64], tmp_not[66];
4259 if (strstr(label, name) != NULL) {
4260 switch (h->addr.type) {
4261 case PF_ADDR_DYNIFTL:
4262 snprintf(tmp, sizeof(tmp), "(%s)", h->addr.v.ifname);
4265 snprintf(tmp, sizeof(tmp), "<%s>", h->addr.v.tblname);
4267 case PF_ADDR_NOROUTE:
4268 snprintf(tmp, sizeof(tmp), "no-route");
4270 case PF_ADDR_URPFFAILED:
4271 snprintf(tmp, sizeof(tmp), "urpf-failed");
4273 case PF_ADDR_ADDRMASK:
4274 if (!af || (PF_AZERO(&h->addr.v.a.addr, af) &&
4275 PF_AZERO(&h->addr.v.a.mask, af)))
4276 snprintf(tmp, sizeof(tmp), "any");
4281 if (inet_ntop(af, &h->addr.v.a.addr, a,
4283 snprintf(tmp, sizeof(tmp), "?");
4285 bits = unmask(&h->addr.v.a.mask, af);
4286 if ((af == AF_INET && bits < 32) ||
4287 (af == AF_INET6 && bits < 128))
4288 snprintf(tmp, sizeof(tmp),
4291 snprintf(tmp, sizeof(tmp),
4297 snprintf(tmp, sizeof(tmp), "?");
4302 snprintf(tmp_not, sizeof(tmp_not), "! %s", tmp);
4303 expand_label_str(label, len, name, tmp_not);
4305 expand_label_str(label, len, name, tmp);
4310 expand_label_port(const char *name, char *label, size_t len,
4311 struct node_port *port)
4313 char a1[6], a2[6], op[13] = "";
4315 if (strstr(label, name) != NULL) {
4316 snprintf(a1, sizeof(a1), "%u", ntohs(port->port[0]));
4317 snprintf(a2, sizeof(a2), "%u", ntohs(port->port[1]));
4320 else if (port->op == PF_OP_IRG)
4321 snprintf(op, sizeof(op), "%s><%s", a1, a2);
4322 else if (port->op == PF_OP_XRG)
4323 snprintf(op, sizeof(op), "%s<>%s", a1, a2);
4324 else if (port->op == PF_OP_EQ)
4325 snprintf(op, sizeof(op), "%s", a1);
4326 else if (port->op == PF_OP_NE)
4327 snprintf(op, sizeof(op), "!=%s", a1);
4328 else if (port->op == PF_OP_LT)
4329 snprintf(op, sizeof(op), "<%s", a1);
4330 else if (port->op == PF_OP_LE)
4331 snprintf(op, sizeof(op), "<=%s", a1);
4332 else if (port->op == PF_OP_GT)
4333 snprintf(op, sizeof(op), ">%s", a1);
4334 else if (port->op == PF_OP_GE)
4335 snprintf(op, sizeof(op), ">=%s", a1);
4336 expand_label_str(label, len, name, op);
4341 expand_label_proto(const char *name, char *label, size_t len, u_int8_t proto)
4343 struct protoent *pe;
4346 if (strstr(label, name) != NULL) {
4347 pe = getprotobynumber(proto);
4349 expand_label_str(label, len, name, pe->p_name);
4351 snprintf(n, sizeof(n), "%u", proto);
4352 expand_label_str(label, len, name, n);
4358 expand_label_nr(const char *name, char *label, size_t len)
4362 if (strstr(label, name) != NULL) {
4363 snprintf(n, sizeof(n), "%u", pf->anchor->match);
4364 expand_label_str(label, len, name, n);
4369 expand_label(char *label, size_t len, const char *ifname, sa_family_t af,
4370 struct node_host *src_host, struct node_port *src_port,
4371 struct node_host *dst_host, struct node_port *dst_port,
4374 expand_label_if("$if", label, len, ifname);
4375 expand_label_addr("$srcaddr", label, len, af, src_host);
4376 expand_label_addr("$dstaddr", label, len, af, dst_host);
4377 expand_label_port("$srcport", label, len, src_port);
4378 expand_label_port("$dstport", label, len, dst_port);
4379 expand_label_proto("$proto", label, len, proto);
4380 expand_label_nr("$nr", label, len);
4384 expand_altq(struct pf_altq *a, struct node_if *interfaces,
4385 struct node_queue *nqueues, struct node_queue_bw bwspec,
4386 struct node_queue_opt *opts)
4388 struct pf_altq pa, pb;
4389 char qname[PF_QNAME_SIZE];
4390 struct node_queue *n;
4391 struct node_queue_bw bw;
4394 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
4395 FREE_LIST(struct node_if, interfaces);
4396 FREE_LIST(struct node_queue, nqueues);
4400 LOOP_THROUGH(struct node_if, interface, interfaces,
4401 memcpy(&pa, a, sizeof(struct pf_altq));
4402 if (strlcpy(pa.ifname, interface->ifname,
4403 sizeof(pa.ifname)) >= sizeof(pa.ifname))
4404 errx(1, "expand_altq: strlcpy");
4406 if (interface->not) {
4407 yyerror("altq on ! <interface> is not supported");
4410 if (eval_pfaltq(pf, &pa, &bwspec, opts))
4413 if (pfctl_add_altq(pf, &pa))
4416 if (pf->opts & PF_OPT_VERBOSE) {
4417 print_altq(&pf->paltq->altq, 0,
4419 if (nqueues && nqueues->tail) {
4421 LOOP_THROUGH(struct node_queue, queue,
4431 if (pa.scheduler == ALTQT_CBQ ||
4432 pa.scheduler == ALTQT_HFSC) {
4433 /* now create a root queue */
4434 memset(&pb, 0, sizeof(struct pf_altq));
4435 if (strlcpy(qname, "root_", sizeof(qname)) >=
4437 errx(1, "expand_altq: strlcpy");
4438 if (strlcat(qname, interface->ifname,
4439 sizeof(qname)) >= sizeof(qname))
4440 errx(1, "expand_altq: strlcat");
4441 if (strlcpy(pb.qname, qname,
4442 sizeof(pb.qname)) >= sizeof(pb.qname))
4443 errx(1, "expand_altq: strlcpy");
4444 if (strlcpy(pb.ifname, interface->ifname,
4445 sizeof(pb.ifname)) >= sizeof(pb.ifname))
4446 errx(1, "expand_altq: strlcpy");
4447 pb.qlimit = pa.qlimit;
4448 pb.scheduler = pa.scheduler;
4449 bw.bw_absolute = pa.ifbandwidth;
4451 if (eval_pfqueue(pf, &pb, &bw, opts))
4454 if (pfctl_add_altq(pf, &pb))
4458 LOOP_THROUGH(struct node_queue, queue, nqueues,
4459 n = calloc(1, sizeof(struct node_queue));
4461 err(1, "expand_altq: calloc");
4462 if (pa.scheduler == ALTQT_CBQ ||
4463 pa.scheduler == ALTQT_HFSC)
4464 if (strlcpy(n->parent, qname,
4465 sizeof(n->parent)) >=
4467 errx(1, "expand_altq: strlcpy");
4468 if (strlcpy(n->queue, queue->queue,
4469 sizeof(n->queue)) >= sizeof(n->queue))
4470 errx(1, "expand_altq: strlcpy");
4471 if (strlcpy(n->ifname, interface->ifname,
4472 sizeof(n->ifname)) >= sizeof(n->ifname))
4473 errx(1, "expand_altq: strlcpy");
4474 n->scheduler = pa.scheduler;
4480 queues->tail->next = n;
4486 FREE_LIST(struct node_if, interfaces);
4487 FREE_LIST(struct node_queue, nqueues);
4493 expand_queue(struct pf_altq *a, struct node_if *interfaces,
4494 struct node_queue *nqueues, struct node_queue_bw bwspec,
4495 struct node_queue_opt *opts)
4497 struct node_queue *n, *nq;
4502 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
4503 FREE_LIST(struct node_queue, nqueues);
4507 if (queues == NULL) {
4508 yyerror("queue %s has no parent", a->qname);
4509 FREE_LIST(struct node_queue, nqueues);
4513 LOOP_THROUGH(struct node_if, interface, interfaces,
4514 LOOP_THROUGH(struct node_queue, tqueue, queues,
4515 if (!strncmp(a->qname, tqueue->queue, PF_QNAME_SIZE) &&
4516 (interface->ifname[0] == 0 ||
4517 (!interface->not && !strncmp(interface->ifname,
4518 tqueue->ifname, IFNAMSIZ)) ||
4519 (interface->not && strncmp(interface->ifname,
4520 tqueue->ifname, IFNAMSIZ)))) {
4521 /* found ourself in queues */
4524 memcpy(&pa, a, sizeof(struct pf_altq));
4526 if (pa.scheduler != ALTQT_NONE &&
4527 pa.scheduler != tqueue->scheduler) {
4528 yyerror("exactly one scheduler type "
4529 "per interface allowed");
4532 pa.scheduler = tqueue->scheduler;
4534 /* scheduler dependent error checking */
4535 switch (pa.scheduler) {
4537 if (nqueues != NULL) {
4538 yyerror("priq queues cannot "
4539 "have child queues");
4542 if (bwspec.bw_absolute > 0 ||
4543 bwspec.bw_percent < 100) {
4544 yyerror("priq doesn't take "
4553 if (strlcpy(pa.ifname, tqueue->ifname,
4554 sizeof(pa.ifname)) >= sizeof(pa.ifname))
4555 errx(1, "expand_queue: strlcpy");
4556 if (strlcpy(pa.parent, tqueue->parent,
4557 sizeof(pa.parent)) >= sizeof(pa.parent))
4558 errx(1, "expand_queue: strlcpy");
4560 if (eval_pfqueue(pf, &pa, &bwspec, opts))
4563 if (pfctl_add_altq(pf, &pa))
4566 for (nq = nqueues; nq != NULL; nq = nq->next) {
4567 if (!strcmp(a->qname, nq->queue)) {
4568 yyerror("queue cannot have "
4574 sizeof(struct node_queue));
4576 err(1, "expand_queue: calloc");
4577 if (strlcpy(n->parent, a->qname,
4578 sizeof(n->parent)) >=
4580 errx(1, "expand_queue strlcpy");
4581 if (strlcpy(n->queue, nq->queue,
4582 sizeof(n->queue)) >=
4584 errx(1, "expand_queue strlcpy");
4585 if (strlcpy(n->ifname, tqueue->ifname,
4586 sizeof(n->ifname)) >=
4588 errx(1, "expand_queue strlcpy");
4589 n->scheduler = tqueue->scheduler;
4595 queues->tail->next = n;
4599 if ((pf->opts & PF_OPT_VERBOSE) && (
4600 (found == 1 && interface->ifname[0] == 0) ||
4601 (found > 0 && interface->ifname[0] != 0))) {
4602 print_queue(&pf->paltq->altq, 0,
4603 &bwspec, interface->ifname[0] != 0,
4605 if (nqueues && nqueues->tail) {
4607 LOOP_THROUGH(struct node_queue,
4620 FREE_LIST(struct node_queue, nqueues);
4621 FREE_LIST(struct node_if, interfaces);
4624 yyerror("queue %s has no parent", a->qname);
4635 expand_rule(struct pf_rule *r,
4636 struct node_if *interfaces, struct node_host *rpool_hosts,
4637 struct node_proto *protos, struct node_os *src_oses,
4638 struct node_host *src_hosts, struct node_port *src_ports,
4639 struct node_host *dst_hosts, struct node_port *dst_ports,
4640 struct node_uid *uids, struct node_gid *gids, struct node_icmp *icmp_types,
4641 const char *anchor_call)
4643 sa_family_t af = r->af;
4644 int added = 0, error = 0;
4645 char ifname[IF_NAMESIZE];
4646 char label[PF_RULE_LABEL_SIZE];
4647 char tagname[PF_TAG_NAME_SIZE];
4648 char match_tagname[PF_TAG_NAME_SIZE];
4649 struct pf_pooladdr *pa;
4650 struct node_host *h;
4651 u_int8_t flags, flagset, keep_state;
4653 if (strlcpy(label, r->label, sizeof(label)) >= sizeof(label))
4654 errx(1, "expand_rule: strlcpy");
4655 if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname))
4656 errx(1, "expand_rule: strlcpy");
4657 if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >=
4658 sizeof(match_tagname))
4659 errx(1, "expand_rule: strlcpy");
4661 flagset = r->flagset;
4662 keep_state = r->keep_state;
4664 LOOP_THROUGH(struct node_if, interface, interfaces,
4665 LOOP_THROUGH(struct node_proto, proto, protos,
4666 LOOP_THROUGH(struct node_icmp, icmp_type, icmp_types,
4667 LOOP_THROUGH(struct node_host, src_host, src_hosts,
4668 LOOP_THROUGH(struct node_port, src_port, src_ports,
4669 LOOP_THROUGH(struct node_os, src_os, src_oses,
4670 LOOP_THROUGH(struct node_host, dst_host, dst_hosts,
4671 LOOP_THROUGH(struct node_port, dst_port, dst_ports,
4672 LOOP_THROUGH(struct node_uid, uid, uids,
4673 LOOP_THROUGH(struct node_gid, gid, gids,
4676 /* for link-local IPv6 address, interface must match up */
4677 if ((r->af && src_host->af && r->af != src_host->af) ||
4678 (r->af && dst_host->af && r->af != dst_host->af) ||
4679 (src_host->af && dst_host->af &&
4680 src_host->af != dst_host->af) ||
4681 (src_host->ifindex && dst_host->ifindex &&
4682 src_host->ifindex != dst_host->ifindex) ||
4683 (src_host->ifindex && *interface->ifname &&
4684 src_host->ifindex != if_nametoindex(interface->ifname)) ||
4685 (dst_host->ifindex && *interface->ifname &&
4686 dst_host->ifindex != if_nametoindex(interface->ifname)))
4688 if (!r->af && src_host->af)
4689 r->af = src_host->af;
4690 else if (!r->af && dst_host->af)
4691 r->af = dst_host->af;
4693 if (*interface->ifname)
4694 strlcpy(r->ifname, interface->ifname,
4696 else if (if_indextoname(src_host->ifindex, ifname))
4697 strlcpy(r->ifname, ifname, sizeof(r->ifname));
4698 else if (if_indextoname(dst_host->ifindex, ifname))
4699 strlcpy(r->ifname, ifname, sizeof(r->ifname));
4701 memset(r->ifname, '\0', sizeof(r->ifname));
4703 if (strlcpy(r->label, label, sizeof(r->label)) >=
4705 errx(1, "expand_rule: strlcpy");
4706 if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >=
4708 errx(1, "expand_rule: strlcpy");
4709 if (strlcpy(r->match_tagname, match_tagname,
4710 sizeof(r->match_tagname)) >= sizeof(r->match_tagname))
4711 errx(1, "expand_rule: strlcpy");
4712 expand_label(r->label, PF_RULE_LABEL_SIZE, r->ifname, r->af,
4713 src_host, src_port, dst_host, dst_port, proto->proto);
4714 expand_label(r->tagname, PF_TAG_NAME_SIZE, r->ifname, r->af,
4715 src_host, src_port, dst_host, dst_port, proto->proto);
4716 expand_label(r->match_tagname, PF_TAG_NAME_SIZE, r->ifname,
4717 r->af, src_host, src_port, dst_host, dst_port,
4720 error += check_netmask(src_host, r->af);
4721 error += check_netmask(dst_host, r->af);
4723 r->ifnot = interface->not;
4724 r->proto = proto->proto;
4725 r->src.addr = src_host->addr;
4726 r->src.neg = src_host->not;
4727 r->src.port[0] = src_port->port[0];
4728 r->src.port[1] = src_port->port[1];
4729 r->src.port_op = src_port->op;
4730 r->dst.addr = dst_host->addr;
4731 r->dst.neg = dst_host->not;
4732 r->dst.port[0] = dst_port->port[0];
4733 r->dst.port[1] = dst_port->port[1];
4734 r->dst.port_op = dst_port->op;
4735 r->uid.op = uid->op;
4736 r->uid.uid[0] = uid->uid[0];
4737 r->uid.uid[1] = uid->uid[1];
4738 r->gid.op = gid->op;
4739 r->gid.gid[0] = gid->gid[0];
4740 r->gid.gid[1] = gid->gid[1];
4741 r->type = icmp_type->type;
4742 r->code = icmp_type->code;
4744 if ((keep_state == PF_STATE_MODULATE ||
4745 keep_state == PF_STATE_SYNPROXY) &&
4746 r->proto && r->proto != IPPROTO_TCP)
4747 r->keep_state = PF_STATE_NORMAL;
4749 r->keep_state = keep_state;
4751 if (r->proto && r->proto != IPPROTO_TCP) {
4756 r->flagset = flagset;
4758 if (icmp_type->proto && r->proto != icmp_type->proto) {
4759 yyerror("icmp-type mismatch");
4763 if (src_os && src_os->os) {
4764 r->os_fingerprint = pfctl_get_fingerprint(src_os->os);
4765 if ((pf->opts & PF_OPT_VERBOSE2) &&
4766 r->os_fingerprint == PF_OSFP_NOMATCH)
4768 "warning: unknown '%s' OS fingerprint\n",
4771 r->os_fingerprint = PF_OSFP_ANY;
4774 TAILQ_INIT(&r->rpool.list);
4775 for (h = rpool_hosts; h != NULL; h = h->next) {
4776 pa = calloc(1, sizeof(struct pf_pooladdr));
4778 err(1, "expand_rule: calloc");
4780 if (h->ifname != NULL) {
4781 if (strlcpy(pa->ifname, h->ifname,
4782 sizeof(pa->ifname)) >=
4784 errx(1, "expand_rule: strlcpy");
4787 TAILQ_INSERT_TAIL(&r->rpool.list, pa, entries);
4790 if (rule_consistent(r, anchor_call[0]) < 0 || error)
4791 yyerror("skipping rule due to errors");
4793 r->nr = pf->astack[pf->asd]->match++;
4794 pfctl_add_rule(pf, r, anchor_call);
4800 FREE_LIST(struct node_if, interfaces);
4801 FREE_LIST(struct node_proto, protos);
4802 FREE_LIST(struct node_host, src_hosts);
4803 FREE_LIST(struct node_port, src_ports);
4804 FREE_LIST(struct node_os, src_oses);
4805 FREE_LIST(struct node_host, dst_hosts);
4806 FREE_LIST(struct node_port, dst_ports);
4807 FREE_LIST(struct node_uid, uids);
4808 FREE_LIST(struct node_gid, gids);
4809 FREE_LIST(struct node_icmp, icmp_types);
4810 FREE_LIST(struct node_host, rpool_hosts);
4813 yyerror("rule expands to no valid combination");
4817 expand_skip_interface(struct node_if *interfaces)
4821 if (!interfaces || (!interfaces->next && !interfaces->not &&
4822 !strcmp(interfaces->ifname, "none"))) {
4823 if (pf->opts & PF_OPT_VERBOSE)
4824 printf("set skip on none\n");
4825 errs = pfctl_set_interface_flags(pf, "", PFI_IFLAG_SKIP, 0);
4829 if (pf->opts & PF_OPT_VERBOSE)
4830 printf("set skip on {");
4831 LOOP_THROUGH(struct node_if, interface, interfaces,
4832 if (pf->opts & PF_OPT_VERBOSE)
4833 printf(" %s", interface->ifname);
4834 if (interface->not) {
4835 yyerror("skip on ! <interface> is not supported");
4838 errs += pfctl_set_interface_flags(pf,
4839 interface->ifname, PFI_IFLAG_SKIP, 1);
4841 if (pf->opts & PF_OPT_VERBOSE)
4844 FREE_LIST(struct node_if, interfaces);
4856 check_rulestate(int desired_state)
4858 if (require_order && (rulestate > desired_state)) {
4859 yyerror("Rules must be in order: options, normalization, "
4860 "queueing, translation, filtering");
4863 rulestate = desired_state;
4868 kw_cmp(const void *k, const void *e)
4870 return (strcmp(k, ((const struct keywords *)e)->k_name));
4876 /* this has to be sorted always */
4877 static const struct keywords keywords[] = {
4879 { "allow-opts", ALLOWOPTS},
4881 { "anchor", ANCHOR},
4882 { "antispoof", ANTISPOOF},
4884 { "bandwidth", BANDWIDTH},
4886 { "binat-anchor", BINATANCHOR},
4887 { "bitmask", BITMASK},
4889 { "block-policy", BLOCKPOLICY},
4892 { "crop", FRAGCROP},
4895 { "drop-ovl", FRAGDROP},
4897 { "fastroute", FASTROUTE},
4898 { "file", FILENAME},
4899 { "fingerprints", FINGERPRINTS},
4901 { "floating", FLOATING},
4904 { "fragment", FRAGMENT},
4906 { "global", GLOBAL},
4909 { "hostid", HOSTID},
4910 { "icmp-type", ICMPTYPE},
4911 { "icmp6-type", ICMP6TYPE},
4912 { "if-bound", IFBOUND},
4919 { "linkshare", LINKSHARE},
4922 { "loginterface", LOGINTERFACE},
4924 { "max-mss", MAXMSS},
4925 { "max-src-conn", MAXSRCCONN},
4926 { "max-src-conn-rate", MAXSRCCONNRATE},
4927 { "max-src-nodes", MAXSRCNODES},
4928 { "max-src-states", MAXSRCSTATES},
4929 { "min-ttl", MINTTL},
4930 { "modulate", MODULATE},
4932 { "nat-anchor", NATANCHOR},
4935 { "no-route", NOROUTE},
4936 { "no-sync", NOSYNC},
4938 { "optimization", OPTIMIZATION},
4941 { "overload", OVERLOAD},
4944 { "priority", PRIORITY},
4946 { "probability", PROBABILITY},
4948 { "qlimit", QLIMIT},
4951 { "random", RANDOM},
4952 { "random-id", RANDOMID},
4954 { "rdr-anchor", RDRANCHOR},
4955 { "realtime", REALTIME},
4956 { "reassemble", REASSEMBLE},
4957 { "reply-to", REPLYTO},
4958 { "require-order", REQUIREORDER},
4959 { "return", RETURN},
4960 { "return-icmp", RETURNICMP},
4961 { "return-icmp6", RETURNICMP6},
4962 { "return-rst", RETURNRST},
4963 { "round-robin", ROUNDROBIN},
4965 { "route-to", ROUTETO},
4966 { "rtable", RTABLE},
4968 { "ruleset-optimization", RULESET_OPTIMIZATION},
4972 { "source-hash", SOURCEHASH},
4973 { "source-track", SOURCETRACK},
4975 { "state-policy", STATEPOLICY},
4976 { "static-port", STATICPORT},
4977 { "sticky-address", STICKYADDRESS},
4978 { "synproxy", SYNPROXY},
4981 { "tagged", TAGGED},
4982 { "tbrsize", TBRSIZE},
4983 { "timeout", TIMEOUT},
4987 { "upperlimit", UPPERLIMIT},
4988 { "urpf-failed", URPFFAILED},
4991 const struct keywords *p;
4993 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
4994 sizeof(keywords[0]), kw_cmp);
4998 fprintf(stderr, "%s: %d\n", s, p->k_val);
5002 fprintf(stderr, "string: %s\n", s);
5007 #define MAXPUSHBACK 128
5011 char pushback_buffer[MAXPUSHBACK];
5012 int pushback_index = 0;
5020 /* Read character from the parsebuffer instead of input. */
5021 if (parseindex >= 0) {
5022 c = parsebuf[parseindex++];
5031 return (pushback_buffer[--pushback_index]);
5033 while ((c = getc(f)) == '\\') {
5039 yylval.lineno = lineno;
5042 if (c == '\t' || c == ' ') {
5043 /* Compress blanks to a single space. */
5046 } while (c == '\t' || c == ' ');
5061 if (parseindex >= 0)
5064 if (pushback_index < MAXPUSHBACK-1)
5065 return (pushback_buffer[pushback_index++] = c);
5078 /* skip to either EOF or the first real EOL */
5101 while ((c = lgetc(fin)) == ' ')
5104 yylval.lineno = lineno;
5106 while ((c = lgetc(fin)) != '\n' && c != EOF)
5108 if (c == '$' && parsebuf == NULL) {
5110 if ((c = lgetc(fin)) == EOF)
5113 if (p + 1 >= buf + sizeof(buf) - 1) {
5114 yyerror("string too long");
5117 if (isalnum(c) || c == '_') {
5127 yyerror("macro '%s' not defined", buf);
5140 if ((c = lgetc(fin)) == EOF)
5150 if (p + 1 >= buf + sizeof(buf) - 1) {
5151 yyerror("string too long");
5156 yylval.v.string = strdup(buf);
5157 if (yylval.v.string == NULL)
5158 err(1, "yylex: strdup");
5163 yylval.v.i = PF_OP_XRG;
5164 return (PORTBINARY);
5171 yylval.v.i = PF_OP_IRG;
5172 return (PORTBINARY);
5184 #define allowed_in_string(x) \
5185 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
5186 x != '{' && x != '}' && x != '<' && x != '>' && \
5187 x != '!' && x != '=' && x != '/' && x != '#' && \
5190 if (isalnum(c) || c == ':' || c == '_') {
5193 if ((unsigned)(p-buf) >= sizeof(buf)) {
5194 yyerror("string too long");
5197 } while ((c = lgetc(fin)) != EOF && (allowed_in_string(c)));
5200 if ((token = lookup(buf)) == STRING)
5201 if ((yylval.v.string = strdup(buf)) == NULL)
5202 err(1, "yylex: strdup");
5206 yylval.lineno = lineno;
5215 parse_rules(FILE *input, struct pfctl *xpf)
5217 struct sym *sym, *next;
5223 rulestate = PFCTL_STATE_NONE;
5224 returnicmpdefault = (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
5225 returnicmp6default =
5226 (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
5227 blockpolicy = PFRULE_DROP;
5232 /* Free macros and check which have not been used. */
5233 for (sym = TAILQ_FIRST(&symhead); sym != NULL; sym = next) {
5234 next = TAILQ_NEXT(sym, entries);
5235 if ((pf->opts & PF_OPT_VERBOSE2) && !sym->used)
5236 fprintf(stderr, "warning: macro '%s' not "
5237 "used\n", sym->nam);
5240 TAILQ_REMOVE(&symhead, sym, entries);
5244 return (errors ? -1 : 0);
5248 * Over-designed efficiency is a French and German concept, so how about
5249 * we wait until they discover this ugliness and make it all fancy.
5252 symset(const char *nam, const char *val, int persist)
5256 for (sym = TAILQ_FIRST(&symhead); sym && strcmp(nam, sym->nam);
5257 sym = TAILQ_NEXT(sym, entries))
5261 if (sym->persist == 1)
5266 TAILQ_REMOVE(&symhead, sym, entries);
5270 if ((sym = calloc(1, sizeof(*sym))) == NULL)
5273 sym->nam = strdup(nam);
5274 if (sym->nam == NULL) {
5278 sym->val = strdup(val);
5279 if (sym->val == NULL) {
5285 sym->persist = persist;
5286 TAILQ_INSERT_TAIL(&symhead, sym, entries);
5291 pfctl_cmdline_symset(char *s)
5296 if ((val = strrchr(s, '=')) == NULL)
5299 if ((sym = malloc(strlen(s) - strlen(val) + 1)) == NULL)
5300 err(1, "pfctl_cmdline_symset: malloc");
5302 strlcpy(sym, s, strlen(s) - strlen(val) + 1);
5304 ret = symset(sym, val + 1, 1);
5311 symget(const char *nam)
5315 TAILQ_FOREACH(sym, &symhead, entries)
5316 if (strcmp(nam, sym->nam) == 0) {
5324 mv_rules(struct pf_ruleset *src, struct pf_ruleset *dst)
5329 for (i = 0; i < PF_RULESET_MAX; ++i) {
5330 while ((r = TAILQ_FIRST(src->rules[i].active.ptr))
5332 TAILQ_REMOVE(src->rules[i].active.ptr, r, entries);
5333 TAILQ_INSERT_TAIL(dst->rules[i].active.ptr, r, entries);
5334 dst->anchor->match++;
5336 src->anchor->match = 0;
5337 while ((r = TAILQ_FIRST(src->rules[i].inactive.ptr))
5339 TAILQ_REMOVE(src->rules[i].inactive.ptr, r, entries);
5340 TAILQ_INSERT_TAIL(dst->rules[i].inactive.ptr,
5347 decide_address_family(struct node_host *n, sa_family_t *af)
5349 if (*af != 0 || n == NULL)
5352 while ((n = n->next) != NULL) {
5361 remove_invalid_hosts(struct node_host **nh, sa_family_t *af)
5363 struct node_host *n = *nh, *prev = NULL;
5366 if (*af && n->af && n->af != *af) {
5367 /* unlink and free n */
5368 struct node_host *next = n->next;
5370 /* adjust tail pointer */
5371 if (n == (*nh)->tail)
5373 /* adjust previous node's next pointer */
5379 if (n->ifname != NULL)
5393 invalid_redirect(struct node_host *nh, sa_family_t af)
5396 struct node_host *n;
5398 /* tables and dyniftl are ok without an address family */
5399 for (n = nh; n != NULL; n = n->next) {
5400 if (n->addr.type != PF_ADDR_TABLE &&
5401 n->addr.type != PF_ADDR_DYNIFTL) {
5402 yyerror("address family not given and "
5403 "translation address expands to multiple "
5404 "address families");
5410 yyerror("no translation address with matching address family "
5418 atoul(char *s, u_long *ulvalp)
5424 ulval = strtoul(s, &ep, 0);
5425 if (s[0] == '\0' || *ep != '\0')
5427 if (errno == ERANGE && ulval == ULONG_MAX)
5439 if (atoul(n, &ulval) == 0) {
5440 if (ulval > 65535) {
5441 yyerror("illegal port value %lu", ulval);
5444 return (htons(ulval));
5446 s = getservbyname(n, "tcp");
5448 s = getservbyname(n, "udp");
5450 yyerror("unknown port %s", n);
5458 rule_label(struct pf_rule *r, char *s)
5461 if (strlcpy(r->label, s, sizeof(r->label)) >=
5463 yyerror("rule label too long (max %d chars)",
5464 sizeof(r->label)-1);
5472 parseicmpspec(char *w, sa_family_t af)
5474 const struct icmpcodeent *p;
5479 icmptype = returnicmpdefault >> 8;
5481 icmptype = returnicmp6default >> 8;
5483 if (atoul(w, &ulval) == -1) {
5484 if ((p = geticmpcodebyname(icmptype, w, af)) == NULL) {
5485 yyerror("unknown icmp code %s", w);
5491 yyerror("invalid icmp code %lu", ulval);
5494 return (icmptype << 8 | ulval);
5498 pfctl_load_anchors(int dev, struct pfctl *pf, struct pfr_buffer *trans)
5500 struct loadanchors *la;
5503 TAILQ_FOREACH(la, &loadanchorshead, entries) {
5504 if (pf->opts & PF_OPT_VERBOSE)
5505 fprintf(stderr, "\nLoading anchor %s from %s\n",
5506 la->anchorname, la->filename);
5507 if ((fin = pfctl_fopen(la->filename, "r")) == NULL) {
5508 warn("%s", la->filename);
5511 if (pfctl_rules(dev, la->filename, fin, pf->opts, pf->optimize,
5512 la->anchorname, trans) == -1)