1 /* $OpenBSD: parse.y,v 1.554 2008/10/17 12:59:53 henning Exp $ */
4 * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c) 2001 Markus Friedl. All rights reserved.
7 * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
8 * Copyright (c) 2001 Theo de Raadt. All rights reserved.
9 * Copyright (c) 2002,2003 Henning Brauer. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #define PFIOC_USE_LATEST
37 #include <sys/types.h>
38 #include <sys/socket.h>
41 #include <sys/sysctl.h>
44 #include <netinet/in.h>
45 #include <netinet/in_systm.h>
46 #include <netinet/ip.h>
47 #include <netinet/ip_icmp.h>
48 #include <netinet/icmp6.h>
49 #include <net/pfvar.h>
50 #include <arpa/inet.h>
51 #include <net/altq/altq.h>
52 #include <net/altq/altq_cbq.h>
53 #include <net/altq/altq_codel.h>
54 #include <net/altq/altq_priq.h>
55 #include <net/altq/altq_hfsc.h>
56 #include <net/altq/altq_fairq.h>
74 #include "pfctl_parser.h"
77 static struct pfctl *pf = NULL;
79 static int rulestate = 0;
80 static u_int16_t returnicmpdefault =
81 (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
82 static u_int16_t returnicmp6default =
83 (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
84 static int blockpolicy = PFRULE_DROP;
85 static int failpolicy = PFRULE_DROP;
86 static int require_order = 1;
87 static int default_statelock;
89 static TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
91 TAILQ_ENTRY(file) entry;
97 struct file *pushfile(const char *, int);
99 int check_file_secrecy(int, const char *);
102 int yyerror(const char *, ...);
103 int kw_cmp(const void *, const void *);
109 static TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
111 TAILQ_ENTRY(sym) entry;
117 int symset(const char *, const char *, int);
118 char *symget(const char *);
120 int atoul(char *, u_long *);
133 struct node_proto *next;
134 struct node_proto *tail;
140 struct node_port *next;
141 struct node_port *tail;
147 struct node_uid *next;
148 struct node_uid *tail;
154 struct node_gid *next;
155 struct node_gid *tail;
162 struct node_icmp *next;
163 struct node_icmp *tail;
166 enum { PF_STATE_OPT_MAX, PF_STATE_OPT_NOSYNC, PF_STATE_OPT_SRCTRACK,
167 PF_STATE_OPT_MAX_SRC_STATES, PF_STATE_OPT_MAX_SRC_CONN,
168 PF_STATE_OPT_MAX_SRC_CONN_RATE, PF_STATE_OPT_MAX_SRC_NODES,
169 PF_STATE_OPT_OVERLOAD, PF_STATE_OPT_STATELOCK,
170 PF_STATE_OPT_TIMEOUT, PF_STATE_OPT_SLOPPY, };
172 enum { PF_SRCTRACK_NONE, PF_SRCTRACK, PF_SRCTRACK_GLOBAL, PF_SRCTRACK_RULE };
174 struct node_state_opt {
177 u_int32_t max_states;
178 u_int32_t max_src_states;
179 u_int32_t max_src_conn;
186 char tblname[PF_TABLE_NAME_SIZE];
188 u_int32_t max_src_nodes;
196 struct node_state_opt *next;
197 struct node_state_opt *tail;
201 struct node_host *host;
202 struct node_port *port;
205 static struct node_queue {
206 char queue[PF_QNAME_SIZE];
207 char parent[PF_QNAME_SIZE];
208 char ifname[IFNAMSIZ];
210 struct node_queue *next;
211 struct node_queue *tail;
214 struct node_qassign {
219 static struct filter_opts {
221 #define FOM_FLAGS 0x01
222 #define FOM_ICMP 0x02
224 #define FOM_KEEP 0x08
225 #define FOM_SRCTRACK 0x10
226 #define FOM_SETPRIO 0x0400
227 #define FOM_PRIO 0x2000
228 struct node_uid *uid;
229 struct node_gid *gid;
236 struct node_icmp *icmpspec;
239 u_int32_t ridentifier;
242 struct node_state_opt *options;
246 char *label[PF_RULE_MAX_LABEL_COUNT];
248 struct node_qassign queues;
251 u_int8_t match_tag_not;
254 u_int32_t free_flags;
257 u_int8_t set_prio[2];
259 struct node_host *addr;
264 static struct antispoof_opts {
265 char *label[PF_RULE_MAX_LABEL_COUNT];
267 u_int32_t ridentifier;
271 static struct scrub_opts {
273 #define SOM_MINTTL 0x01
274 #define SOM_MAXMSS 0x02
275 #define SOM_FRAGCACHE 0x04
276 #define SOM_SETTOS 0x08
285 u_int8_t match_tag_not;
289 static struct queue_opts {
291 #define QOM_BWSPEC 0x01
292 #define QOM_SCHEDULER 0x02
293 #define QOM_PRIORITY 0x04
294 #define QOM_TBRSIZE 0x08
295 #define QOM_QLIMIT 0x10
296 struct node_queue_bw queue_bwspec;
297 struct node_queue_opt scheduler;
299 unsigned int tbrsize;
303 static struct table_opts {
306 struct node_tinithead init_nodes;
309 static struct pool_opts {
311 #define POM_TYPE 0x01
312 #define POM_STICKYADDRESS 0x02
316 struct pf_poolhashkey *key;
317 struct pf_mape_portset mape;
321 static struct codel_opts codel_opts;
322 static struct node_hfsc_opts hfsc_opts;
323 static struct node_fairq_opts fairq_opts;
324 static struct node_state_opt *keep_state_defaults = NULL;
325 static struct pfctl_watermarks syncookie_opts;
327 int disallow_table(struct node_host *, const char *);
328 int disallow_urpf_failed(struct node_host *, const char *);
329 int disallow_alias(struct node_host *, const char *);
330 int rule_consistent(struct pfctl_rule *, int);
331 int filter_consistent(struct pfctl_rule *, int);
332 int nat_consistent(struct pfctl_rule *);
333 int rdr_consistent(struct pfctl_rule *);
334 int process_tabledef(char *, struct table_opts *);
335 void expand_label_str(char *, size_t, const char *, const char *);
336 void expand_label_if(const char *, char *, size_t, const char *);
337 void expand_label_addr(const char *, char *, size_t, u_int8_t,
338 struct pf_rule_addr *);
339 void expand_label_port(const char *, char *, size_t,
340 struct pf_rule_addr *);
341 void expand_label_proto(const char *, char *, size_t, u_int8_t);
342 void expand_label_nr(const char *, char *, size_t,
343 struct pfctl_rule *);
344 void expand_rule(struct pfctl_rule *, struct node_if *,
345 struct node_host *, struct node_proto *, struct node_os *,
346 struct node_host *, struct node_port *, struct node_host *,
347 struct node_port *, struct node_uid *, struct node_gid *,
348 struct node_icmp *, const char *);
349 int expand_altq(struct pf_altq *, struct node_if *,
350 struct node_queue *, struct node_queue_bw bwspec,
351 struct node_queue_opt *);
352 int expand_queue(struct pf_altq *, struct node_if *,
353 struct node_queue *, struct node_queue_bw,
354 struct node_queue_opt *);
355 int expand_skip_interface(struct node_if *);
357 int check_rulestate(int);
358 int getservice(char *);
359 int rule_label(struct pfctl_rule *, char *s[PF_RULE_MAX_LABEL_COUNT]);
360 int rt_tableid_max(void);
362 void mv_rules(struct pfctl_ruleset *, struct pfctl_ruleset *);
363 void decide_address_family(struct node_host *, sa_family_t *);
364 void remove_invalid_hosts(struct node_host **, sa_family_t *);
365 int invalid_redirect(struct node_host *, sa_family_t);
366 u_int16_t parseicmpspec(char *, sa_family_t);
367 int kw_casecmp(const void *, const void *);
368 int map_tos(char *string, int *);
370 static TAILQ_HEAD(loadanchorshead, loadanchors)
371 loadanchorshead = TAILQ_HEAD_INITIALIZER(loadanchorshead);
374 TAILQ_ENTRY(loadanchors) entries;
397 struct node_if *interface;
398 struct node_proto *proto;
399 struct node_icmp *icmp;
400 struct node_host *host;
402 struct node_port *port;
403 struct node_uid *uid;
404 struct node_gid *gid;
405 struct node_state_opt *state_opt;
408 struct peer src, dst;
409 struct node_os *src_os;
412 struct node_host *host;
416 struct pf_poolhashkey *key;
419 struct node_host *host;
424 struct node_state_opt *options;
435 struct pf_poolhashkey *hashkey;
436 struct node_queue *queue;
437 struct node_queue_opt queue_options;
438 struct node_queue_bw queue_bwspec;
439 struct node_qassign qassign;
440 struct filter_opts filter_opts;
441 struct antispoof_opts antispoof_opts;
442 struct queue_opts queue_opts;
443 struct scrub_opts scrub_opts;
444 struct table_opts table_opts;
445 struct pool_opts pool_opts;
446 struct node_hfsc_opts hfsc_opts;
447 struct node_fairq_opts fairq_opts;
448 struct codel_opts codel_opts;
449 struct pfctl_watermarks *watermarks;
454 #define PPORT_RANGE 1
456 int parseport(char *, struct range *r, int);
458 #define DYNIF_MULTIADDR(addr) ((addr).type == PF_ADDR_DYNIFTL && \
459 (!((addr).iflags & PFI_AFLAG_NOALIAS) || \
460 !isdigit((addr).v.ifname[strlen((addr).v.ifname)-1])))
464 %token PASS BLOCK MATCH SCRUB RETURN IN OS OUT LOG QUICK ON FROM TO FLAGS
465 %token RETURNRST RETURNICMP RETURNICMP6 PROTO INET INET6 ALL ANY ICMPTYPE
466 %token ICMP6TYPE CODE KEEP MODULATE STATE PORT RDR NAT BINAT ARROW NODF
467 %token MINTTL ERROR ALLOWOPTS FASTROUTE FILENAME ROUTETO DUPTO REPLYTO NO LABEL
468 %token NOROUTE URPFFAILED FRAGMENT USER GROUP MAXMSS MAXIMUM TTL TOS DROP TABLE
469 %token REASSEMBLE FRAGDROP FRAGCROP ANCHOR NATANCHOR RDRANCHOR BINATANCHOR
470 %token SET OPTIMIZATION TIMEOUT LIMIT LOGINTERFACE BLOCKPOLICY FAILPOLICY
471 %token RANDOMID REQUIREORDER SYNPROXY FINGERPRINTS NOSYNC DEBUG SKIP HOSTID
472 %token ANTISPOOF FOR INCLUDE KEEPCOUNTERS SYNCOOKIES
473 %token BITMASK RANDOM SOURCEHASH ROUNDROBIN STATICPORT PROBABILITY MAPEPORTSET
474 %token ALTQ CBQ CODEL PRIQ HFSC FAIRQ BANDWIDTH TBRSIZE LINKSHARE REALTIME
475 %token UPPERLIMIT QUEUE PRIORITY QLIMIT HOGS BUCKETS RTABLE TARGET INTERVAL
476 %token DNPIPE DNQUEUE RIDENTIFIER
477 %token LOAD RULESET_OPTIMIZATION PRIO
478 %token STICKYADDRESS MAXSRCSTATES MAXSRCNODES SOURCETRACK GLOBAL RULE
479 %token MAXSRCCONN MAXSRCCONNRATE OVERLOAD FLUSH SLOPPY
480 %token TAGGED TAG IFBOUND FLOATING STATEPOLICY STATEDEFAULTS ROUTE SETTOS
481 %token DIVERTTO DIVERTREPLY
482 %token <v.string> STRING
483 %token <v.number> NUMBER
484 %token <v.i> PORTBINARY
485 %type <v.interface> interface if_list if_item_not if_item
486 %type <v.number> number icmptype icmp6type uid gid
487 %type <v.number> tos not yesno
488 %type <v.probability> probability
489 %type <v.i> no dir af fragcache optimizer syncookie_val
490 %type <v.i> sourcetrack flush unaryop statelock
491 %type <v.b> action nataction natpasslog scrubaction
492 %type <v.b> flags flag blockspec prio
493 %type <v.range> portplain portstar portrange
494 %type <v.hashkey> hashkey
495 %type <v.proto> proto proto_list proto_item
496 %type <v.number> protoval
497 %type <v.icmp> icmpspec
498 %type <v.icmp> icmp_list icmp_item
499 %type <v.icmp> icmp6_list icmp6_item
500 %type <v.number> reticmpspec reticmp6spec
501 %type <v.fromto> fromto
502 %type <v.peer> ipportspec from to
503 %type <v.host> ipspec toipspec xhost host dynaddr host_list
504 %type <v.host> redir_host_list redirspec
505 %type <v.host> route_host route_host_list routespec
506 %type <v.os> os xos os_list
507 %type <v.port> portspec port_list port_item
508 %type <v.uid> uids uid_list uid_item
509 %type <v.gid> gids gid_list gid_item
510 %type <v.route> route
511 %type <v.redirection> redirection redirpool
512 %type <v.string> label stringall tag anchorname
513 %type <v.string> string varstring numberstring
514 %type <v.keep_state> keep
515 %type <v.state_opt> state_opt_spec state_opt_list state_opt_item
516 %type <v.logquick> logquick quick log logopts logopt
517 %type <v.interface> antispoof_ifspc antispoof_iflst antispoof_if
518 %type <v.qassign> qname
519 %type <v.queue> qassign qassign_list qassign_item
520 %type <v.queue_options> scheduler
521 %type <v.number> cbqflags_list cbqflags_item
522 %type <v.number> priqflags_list priqflags_item
523 %type <v.hfsc_opts> hfscopts_list hfscopts_item hfsc_opts
524 %type <v.fairq_opts> fairqopts_list fairqopts_item fairq_opts
525 %type <v.codel_opts> codelopts_list codelopts_item codel_opts
526 %type <v.queue_bwspec> bandwidth
527 %type <v.filter_opts> filter_opts filter_opt filter_opts_l
528 %type <v.filter_opts> filter_sets filter_set filter_sets_l
529 %type <v.antispoof_opts> antispoof_opts antispoof_opt antispoof_opts_l
530 %type <v.queue_opts> queue_opts queue_opt queue_opts_l
531 %type <v.scrub_opts> scrub_opts scrub_opt scrub_opts_l
532 %type <v.table_opts> table_opts table_opt table_opts_l
533 %type <v.pool_opts> pool_opts pool_opt pool_opts_l
534 %type <v.tagged> tagged
535 %type <v.rtableid> rtable
536 %type <v.watermarks> syncookie_opts
539 ruleset : /* empty */
540 | ruleset include '\n'
542 | ruleset option '\n'
543 | ruleset scrubrule '\n'
544 | ruleset natrule '\n'
545 | ruleset binatrule '\n'
546 | ruleset pfrule '\n'
547 | ruleset anchorrule '\n'
548 | ruleset loadrule '\n'
549 | ruleset altqif '\n'
550 | ruleset queuespec '\n'
551 | ruleset varset '\n'
552 | ruleset antispoof '\n'
553 | ruleset tabledef '\n'
554 | '{' fakeanchor '}' '\n';
555 | ruleset error '\n' { file->errors++; }
558 include : INCLUDE STRING {
561 if ((nfile = pushfile($2, 0)) == NULL) {
562 yyerror("failed to include file %s", $2);
574 * apply to previouslys specified rule: must be careful to note
575 * what that is: pf or nat or binat or rdr
577 fakeanchor : fakeanchor '\n'
578 | fakeanchor anchorrule '\n'
579 | fakeanchor binatrule '\n'
580 | fakeanchor natrule '\n'
581 | fakeanchor pfrule '\n'
582 | fakeanchor error '\n'
586 if (!strcmp($1, "none"))
588 else if (!strcmp($1, "basic"))
589 $$ = PF_OPTIMIZE_BASIC;
590 else if (!strcmp($1, "profile"))
591 $$ = PF_OPTIMIZE_BASIC | PF_OPTIMIZE_PROFILE;
593 yyerror("unknown ruleset-optimization %s", $1);
599 option : SET OPTIMIZATION STRING {
600 if (check_rulestate(PFCTL_STATE_OPTION)) {
604 if (pfctl_set_optimization(pf, $3) != 0) {
605 yyerror("unknown optimization %s", $3);
611 | SET RULESET_OPTIMIZATION optimizer {
612 if (!(pf->opts & PF_OPT_OPTIMIZE)) {
613 pf->opts |= PF_OPT_OPTIMIZE;
617 | SET TIMEOUT timeout_spec
618 | SET TIMEOUT '{' optnl timeout_list '}'
619 | SET LIMIT limit_spec
620 | SET LIMIT '{' optnl limit_list '}'
621 | SET LOGINTERFACE stringall {
622 if (check_rulestate(PFCTL_STATE_OPTION)) {
626 if (pfctl_set_logif(pf, $3) != 0) {
627 yyerror("error setting loginterface %s", $3);
633 | SET HOSTID number {
634 if ($3 == 0 || $3 > UINT_MAX) {
635 yyerror("hostid must be non-zero");
638 if (pfctl_set_hostid(pf, $3) != 0) {
639 yyerror("error setting hostid %08x", $3);
643 | SET BLOCKPOLICY DROP {
644 if (pf->opts & PF_OPT_VERBOSE)
645 printf("set block-policy drop\n");
646 if (check_rulestate(PFCTL_STATE_OPTION))
648 blockpolicy = PFRULE_DROP;
650 | SET BLOCKPOLICY RETURN {
651 if (pf->opts & PF_OPT_VERBOSE)
652 printf("set block-policy return\n");
653 if (check_rulestate(PFCTL_STATE_OPTION))
655 blockpolicy = PFRULE_RETURN;
657 | SET FAILPOLICY DROP {
658 if (pf->opts & PF_OPT_VERBOSE)
659 printf("set fail-policy drop\n");
660 if (check_rulestate(PFCTL_STATE_OPTION))
662 failpolicy = PFRULE_DROP;
664 | SET FAILPOLICY RETURN {
665 if (pf->opts & PF_OPT_VERBOSE)
666 printf("set fail-policy return\n");
667 if (check_rulestate(PFCTL_STATE_OPTION))
669 failpolicy = PFRULE_RETURN;
671 | SET REQUIREORDER yesno {
672 if (pf->opts & PF_OPT_VERBOSE)
673 printf("set require-order %s\n",
674 $3 == 1 ? "yes" : "no");
677 | SET FINGERPRINTS STRING {
678 if (pf->opts & PF_OPT_VERBOSE)
679 printf("set fingerprints \"%s\"\n", $3);
680 if (check_rulestate(PFCTL_STATE_OPTION)) {
684 if (!pf->anchor->name[0]) {
685 if (pfctl_file_fingerprints(pf->dev,
687 yyerror("error loading "
688 "fingerprints %s", $3);
695 | SET STATEPOLICY statelock {
696 if (pf->opts & PF_OPT_VERBOSE)
699 printf("set state-policy floating\n");
702 printf("set state-policy if-bound\n");
705 default_statelock = $3;
708 if (check_rulestate(PFCTL_STATE_OPTION)) {
712 if (pfctl_set_debug(pf, $3) != 0) {
713 yyerror("error setting debuglevel %s", $3);
719 | SET SKIP interface {
720 if (expand_skip_interface($3) != 0) {
721 yyerror("error setting skip interface(s)");
725 | SET STATEDEFAULTS state_opt_list {
726 if (keep_state_defaults != NULL) {
727 yyerror("cannot redefine state-defaults");
730 keep_state_defaults = $3;
733 pf->keep_counters = true;
735 | SET SYNCOOKIES syncookie_val syncookie_opts {
736 if (pfctl_cfg_syncookies(pf, $3, $4)) {
737 yyerror("error setting syncookies");
743 syncookie_val : STRING {
744 if (!strcmp($1, "never"))
745 $$ = PFCTL_SYNCOOKIES_NEVER;
746 else if (!strcmp($1, "adaptive"))
747 $$ = PFCTL_SYNCOOKIES_ADAPTIVE;
748 else if (!strcmp($1, "always"))
749 $$ = PFCTL_SYNCOOKIES_ALWAYS;
751 yyerror("illegal value for syncookies");
756 syncookie_opts : /* empty */ { $$ = NULL; }
758 memset(&syncookie_opts, 0, sizeof(syncookie_opts));
759 } '(' syncookie_opt_l ')' { $$ = &syncookie_opts; }
762 syncookie_opt_l : syncookie_opt_l comma syncookie_opt
766 syncookie_opt : STRING STRING {
770 val = strtod($2, &cp);
771 if (cp == NULL || strcmp(cp, "%"))
773 if (val <= 0 || val > 100) {
774 yyerror("illegal percentage value");
777 if (!strcmp($1, "start")) {
778 syncookie_opts.hi = val;
779 } else if (!strcmp($1, "end")) {
780 syncookie_opts.lo = val;
782 yyerror("illegal syncookie option");
788 stringall : STRING { $$ = $1; }
790 if (($$ = strdup("all")) == NULL) {
791 err(1, "stringall: strdup");
796 string : STRING string {
797 if (asprintf(&$$, "%s %s", $1, $2) == -1)
798 err(1, "string: asprintf");
805 varstring : numberstring varstring {
806 if (asprintf(&$$, "%s %s", $1, $2) == -1)
807 err(1, "string: asprintf");
814 numberstring : NUMBER {
816 if (asprintf(&s, "%lld", (long long)$1) == -1) {
817 yyerror("string: asprintf");
825 varset : STRING '=' varstring {
827 if (pf->opts & PF_OPT_VERBOSE)
828 printf("%s = \"%s\"\n", $1, $3);
830 if (isspace((unsigned char)*s)) {
831 yyerror("macro name cannot contain "
836 if (symset($1, $3, 0) == -1)
837 err(1, "cannot store variable %s", $1);
843 anchorname : STRING { $$ = $1; }
844 | /* empty */ { $$ = NULL; }
847 pfa_anchorlist : /* empty */
848 | pfa_anchorlist '\n'
849 | pfa_anchorlist pfrule '\n'
850 | pfa_anchorlist anchorrule '\n'
855 char ta[PF_ANCHOR_NAME_SIZE];
856 struct pfctl_ruleset *rs;
858 /* steping into a brace anchor */
862 /* create a holding ruleset in the root */
863 snprintf(ta, PF_ANCHOR_NAME_SIZE, "_%d", pf->bn);
864 rs = pf_find_or_create_ruleset(ta);
866 err(1, "pfa_anchor: pf_find_or_create_ruleset");
867 pf->astack[pf->asd] = rs->anchor;
868 pf->anchor = rs->anchor;
869 } '\n' pfa_anchorlist '}'
871 pf->alast = pf->anchor;
873 pf->anchor = pf->astack[pf->asd];
878 anchorrule : ANCHOR anchorname dir quick interface af proto fromto
879 filter_opts pfa_anchor
882 struct node_proto *proto;
884 if (check_rulestate(PFCTL_STATE_FILTER)) {
890 if ($2 && ($2[0] == '_' || strstr($2, "/_") != NULL)) {
892 yyerror("anchor names beginning with '_' "
893 "are reserved for internal use");
897 memset(&r, 0, sizeof(r));
898 if (pf->astack[pf->asd + 1]) {
899 /* move inline rules into relative location */
900 pfctl_anchor_setup(&r,
901 &pf->astack[pf->asd]->ruleset,
902 $2 ? $2 : pf->alast->name);
904 if (r.anchor == NULL)
905 err(1, "anchorrule: unable to "
908 if (pf->alast != r.anchor) {
909 if (r.anchor->match) {
910 yyerror("inline anchor '%s' "
915 mv_rules(&pf->alast->ruleset,
918 pf_remove_if_empty_ruleset(&pf->alast->ruleset);
919 pf->alast = r.anchor;
922 yyerror("anchors without explicit "
923 "rules must specify a name");
931 r.rtableid = $9.rtableid;
932 r.ridentifier = $9.ridentifier;
935 if (strlcpy(r.tagname, $9.tag,
936 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
937 yyerror("tag too long, max %u chars",
938 PF_TAG_NAME_SIZE - 1);
942 if (strlcpy(r.match_tagname, $9.match_tag,
943 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
944 yyerror("tag too long, max %u chars",
945 PF_TAG_NAME_SIZE - 1);
948 r.match_tag_not = $9.match_tag_not;
949 if (rule_label(&r, $9.label))
951 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++)
953 r.flags = $9.flags.b1;
954 r.flagset = $9.flags.b2;
955 if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
956 yyerror("flags always false");
959 if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
960 for (proto = $7; proto != NULL &&
961 proto->proto != IPPROTO_TCP;
964 if (proto == NULL && $7 != NULL) {
965 if ($9.flags.b1 || $9.flags.b2)
967 "flags only apply to tcp");
970 "OS fingerprinting only "
978 if ($9.keep.action) {
979 yyerror("cannot specify state handling "
985 if (strlcpy(r.match_tagname, $9.match_tag,
986 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
987 yyerror("tag too long, max %u chars",
988 PF_TAG_NAME_SIZE - 1);
991 r.match_tag_not = $9.match_tag_not;
992 if ($9.marker & FOM_PRIO) {
994 r.prio = PF_PRIO_ZERO;
998 if ($9.marker & FOM_SETPRIO) {
999 r.set_prio[0] = $9.set_prio[0];
1000 r.set_prio[1] = $9.set_prio[1];
1001 r.scrub_flags |= PFSTATE_SETPRIO;
1004 decide_address_family($8.src.host, &r.af);
1005 decide_address_family($8.dst.host, &r.af);
1007 expand_rule(&r, $5, NULL, $7, $8.src_os,
1008 $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
1009 $9.uid, $9.gid, $9.icmpspec,
1010 pf->astack[pf->asd + 1] ? pf->alast->name : $2);
1012 pf->astack[pf->asd + 1] = NULL;
1014 | NATANCHOR string interface af proto fromto rtable {
1015 struct pfctl_rule r;
1017 if (check_rulestate(PFCTL_STATE_NAT)) {
1022 memset(&r, 0, sizeof(r));
1027 decide_address_family($6.src.host, &r.af);
1028 decide_address_family($6.dst.host, &r.af);
1030 expand_rule(&r, $3, NULL, $5, $6.src_os,
1031 $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
1035 | RDRANCHOR string interface af proto fromto rtable {
1036 struct pfctl_rule r;
1038 if (check_rulestate(PFCTL_STATE_NAT)) {
1043 memset(&r, 0, sizeof(r));
1048 decide_address_family($6.src.host, &r.af);
1049 decide_address_family($6.dst.host, &r.af);
1051 if ($6.src.port != NULL) {
1052 yyerror("source port parameter not supported"
1056 if ($6.dst.port != NULL) {
1057 if ($6.dst.port->next != NULL) {
1058 yyerror("destination port list "
1059 "expansion not supported in "
1062 } else if ($6.dst.port->op != PF_OP_EQ) {
1063 yyerror("destination port operators"
1064 " not supported in rdr-anchor");
1067 r.dst.port[0] = $6.dst.port->port[0];
1068 r.dst.port[1] = $6.dst.port->port[1];
1069 r.dst.port_op = $6.dst.port->op;
1072 expand_rule(&r, $3, NULL, $5, $6.src_os,
1073 $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
1077 | BINATANCHOR string interface af proto fromto rtable {
1078 struct pfctl_rule r;
1080 if (check_rulestate(PFCTL_STATE_NAT)) {
1085 memset(&r, 0, sizeof(r));
1086 r.action = PF_BINAT;
1090 if ($5->next != NULL) {
1091 yyerror("proto list expansion"
1092 " not supported in binat-anchor");
1095 r.proto = $5->proto;
1099 if ($6.src.host != NULL || $6.src.port != NULL ||
1100 $6.dst.host != NULL || $6.dst.port != NULL) {
1101 yyerror("fromto parameter not supported"
1102 " in binat-anchor");
1106 decide_address_family($6.src.host, &r.af);
1107 decide_address_family($6.dst.host, &r.af);
1109 pfctl_append_rule(pf, &r, $2);
1114 loadrule : LOAD ANCHOR string FROM string {
1115 struct loadanchors *loadanchor;
1117 if (strlen(pf->anchor->name) + 1 +
1118 strlen($3) >= MAXPATHLEN) {
1119 yyerror("anchorname %s too long, max %u\n",
1120 $3, MAXPATHLEN - 1);
1124 loadanchor = calloc(1, sizeof(struct loadanchors));
1125 if (loadanchor == NULL)
1126 err(1, "loadrule: calloc");
1127 if ((loadanchor->anchorname = malloc(MAXPATHLEN)) ==
1129 err(1, "loadrule: malloc");
1130 if (pf->anchor->name[0])
1131 snprintf(loadanchor->anchorname, MAXPATHLEN,
1132 "%s/%s", pf->anchor->name, $3);
1134 strlcpy(loadanchor->anchorname, $3, MAXPATHLEN);
1135 if ((loadanchor->filename = strdup($5)) == NULL)
1136 err(1, "loadrule: strdup");
1138 TAILQ_INSERT_TAIL(&loadanchorshead, loadanchor,
1145 scrubaction : no SCRUB {
1154 scrubrule : scrubaction dir logquick interface af proto fromto scrub_opts
1156 struct pfctl_rule r;
1158 if (check_rulestate(PFCTL_STATE_SCRUB))
1161 memset(&r, 0, sizeof(r));
1169 yyerror("scrub rules do not support 'quick'");
1175 r.rule_flag |= PFRULE_NODF;
1177 r.rule_flag |= PFRULE_RANDOMID;
1178 if ($8.reassemble_tcp) {
1179 if (r.direction != PF_INOUT) {
1180 yyerror("reassemble tcp rules can not "
1181 "specify direction");
1184 r.rule_flag |= PFRULE_REASSEMBLE_TCP;
1187 r.min_ttl = $8.minttl;
1189 r.max_mss = $8.maxmss;
1190 if ($8.marker & SOM_SETTOS) {
1191 r.rule_flag |= PFRULE_SET_TOS;
1192 r.set_tos = $8.settos;
1195 r.rule_flag |= $8.fragcache;
1197 if (strlcpy(r.match_tagname, $8.match_tag,
1198 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1199 yyerror("tag too long, max %u chars",
1200 PF_TAG_NAME_SIZE - 1);
1203 r.match_tag_not = $8.match_tag_not;
1204 r.rtableid = $8.rtableid;
1206 expand_rule(&r, $4, NULL, $6, $7.src_os,
1207 $7.src.host, $7.src.port, $7.dst.host, $7.dst.port,
1208 NULL, NULL, NULL, "");
1213 bzero(&scrub_opts, sizeof scrub_opts);
1214 scrub_opts.rtableid = -1;
1217 { $$ = scrub_opts; }
1219 bzero(&scrub_opts, sizeof scrub_opts);
1220 scrub_opts.rtableid = -1;
1225 scrub_opts_l : scrub_opts_l scrub_opt
1230 if (scrub_opts.nodf) {
1231 yyerror("no-df cannot be respecified");
1234 scrub_opts.nodf = 1;
1237 if (scrub_opts.marker & SOM_MINTTL) {
1238 yyerror("min-ttl cannot be respecified");
1241 if ($2 < 0 || $2 > 255) {
1242 yyerror("illegal min-ttl value %d", $2);
1245 scrub_opts.marker |= SOM_MINTTL;
1246 scrub_opts.minttl = $2;
1249 if (scrub_opts.marker & SOM_MAXMSS) {
1250 yyerror("max-mss cannot be respecified");
1253 if ($2 < 0 || $2 > 65535) {
1254 yyerror("illegal max-mss value %d", $2);
1257 scrub_opts.marker |= SOM_MAXMSS;
1258 scrub_opts.maxmss = $2;
1261 if (scrub_opts.marker & SOM_SETTOS) {
1262 yyerror("set-tos cannot be respecified");
1265 scrub_opts.marker |= SOM_SETTOS;
1266 scrub_opts.settos = $2;
1269 if (scrub_opts.marker & SOM_FRAGCACHE) {
1270 yyerror("fragcache cannot be respecified");
1273 scrub_opts.marker |= SOM_FRAGCACHE;
1274 scrub_opts.fragcache = $1;
1276 | REASSEMBLE STRING {
1277 if (strcasecmp($2, "tcp") != 0) {
1278 yyerror("scrub reassemble supports only tcp, "
1284 if (scrub_opts.reassemble_tcp) {
1285 yyerror("reassemble tcp cannot be respecified");
1288 scrub_opts.reassemble_tcp = 1;
1291 if (scrub_opts.randomid) {
1292 yyerror("random-id cannot be respecified");
1295 scrub_opts.randomid = 1;
1298 if ($2 < 0 || $2 > rt_tableid_max()) {
1299 yyerror("invalid rtable id");
1302 scrub_opts.rtableid = $2;
1304 | not TAGGED string {
1305 scrub_opts.match_tag = $3;
1306 scrub_opts.match_tag_not = $1;
1310 fragcache : FRAGMENT REASSEMBLE { $$ = 0; /* default */ }
1311 | FRAGMENT FRAGCROP { $$ = 0; }
1312 | FRAGMENT FRAGDROP { $$ = 0; }
1315 antispoof : ANTISPOOF logquick antispoof_ifspc af antispoof_opts {
1316 struct pfctl_rule r;
1317 struct node_host *h = NULL, *hh;
1318 struct node_if *i, *j;
1320 if (check_rulestate(PFCTL_STATE_FILTER))
1323 for (i = $3; i; i = i->next) {
1324 bzero(&r, sizeof(r));
1327 r.direction = PF_IN;
1332 r.ridentifier = $5.ridentifier;
1333 if (rule_label(&r, $5.label))
1335 r.rtableid = $5.rtableid;
1336 j = calloc(1, sizeof(struct node_if));
1338 err(1, "antispoof: calloc");
1339 if (strlcpy(j->ifname, i->ifname,
1340 sizeof(j->ifname)) >= sizeof(j->ifname)) {
1342 yyerror("interface name too long");
1347 h = calloc(1, sizeof(*h));
1349 err(1, "address: calloc");
1350 h->addr.type = PF_ADDR_DYNIFTL;
1352 if (strlcpy(h->addr.v.ifname, i->ifname,
1353 sizeof(h->addr.v.ifname)) >=
1354 sizeof(h->addr.v.ifname)) {
1357 "interface name too long");
1360 hh = malloc(sizeof(*hh));
1362 err(1, "address: malloc");
1363 bcopy(h, hh, sizeof(*hh));
1364 h->addr.iflags = PFI_AFLAG_NETWORK;
1366 h = ifa_lookup(j->ifname,
1372 expand_rule(&r, j, NULL, NULL, NULL, h,
1373 NULL, NULL, NULL, NULL, NULL,
1376 if ((i->ifa_flags & IFF_LOOPBACK) == 0) {
1377 bzero(&r, sizeof(r));
1380 r.direction = PF_IN;
1385 r.ridentifier = $5.ridentifier;
1386 if (rule_label(&r, $5.label))
1388 r.rtableid = $5.rtableid;
1392 h = ifa_lookup(i->ifname, 0);
1394 expand_rule(&r, NULL, NULL,
1395 NULL, NULL, h, NULL, NULL,
1396 NULL, NULL, NULL, NULL, "");
1400 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++)
1405 antispoof_ifspc : FOR antispoof_if { $$ = $2; }
1406 | FOR '{' optnl antispoof_iflst '}' { $$ = $4; }
1409 antispoof_iflst : antispoof_if optnl { $$ = $1; }
1410 | antispoof_iflst comma antispoof_if optnl {
1411 $1->tail->next = $3;
1417 antispoof_if : if_item { $$ = $1; }
1425 bzero(&antispoof_opts, sizeof antispoof_opts);
1426 antispoof_opts.rtableid = -1;
1429 { $$ = antispoof_opts; }
1431 bzero(&antispoof_opts, sizeof antispoof_opts);
1432 antispoof_opts.rtableid = -1;
1433 $$ = antispoof_opts;
1437 antispoof_opts_l : antispoof_opts_l antispoof_opt
1441 antispoof_opt : label {
1442 if (antispoof_opts.labelcount >= PF_RULE_MAX_LABEL_COUNT) {
1443 yyerror("label can only be used %d times", PF_RULE_MAX_LABEL_COUNT);
1446 antispoof_opts.label[antispoof_opts.labelcount++] = $1;
1448 | RIDENTIFIER number {
1449 antispoof_opts.ridentifier = $2;
1452 if ($2 < 0 || $2 > rt_tableid_max()) {
1453 yyerror("invalid rtable id");
1456 antispoof_opts.rtableid = $2;
1460 not : '!' { $$ = 1; }
1461 | /* empty */ { $$ = 0; }
1464 tabledef : TABLE '<' STRING '>' table_opts {
1465 struct node_host *h, *nh;
1466 struct node_tinit *ti, *nti;
1468 if (strlen($3) >= PF_TABLE_NAME_SIZE) {
1469 yyerror("table name too long, max %d chars",
1470 PF_TABLE_NAME_SIZE - 1);
1474 if (pf->loadopt & PFCTL_FLAG_TABLE)
1475 if (process_tabledef($3, &$5)) {
1480 for (ti = SIMPLEQ_FIRST(&$5.init_nodes);
1481 ti != SIMPLEQ_END(&$5.init_nodes); ti = nti) {
1484 for (h = ti->host; h != NULL; h = nh) {
1488 nti = SIMPLEQ_NEXT(ti, entries);
1495 bzero(&table_opts, sizeof table_opts);
1496 SIMPLEQ_INIT(&table_opts.init_nodes);
1499 { $$ = table_opts; }
1502 bzero(&table_opts, sizeof table_opts);
1503 SIMPLEQ_INIT(&table_opts.init_nodes);
1508 table_opts_l : table_opts_l table_opt
1512 table_opt : STRING {
1513 if (!strcmp($1, "const"))
1514 table_opts.flags |= PFR_TFLAG_CONST;
1515 else if (!strcmp($1, "persist"))
1516 table_opts.flags |= PFR_TFLAG_PERSIST;
1517 else if (!strcmp($1, "counters"))
1518 table_opts.flags |= PFR_TFLAG_COUNTERS;
1520 yyerror("invalid table option '%s'", $1);
1526 | '{' optnl '}' { table_opts.init_addr = 1; }
1527 | '{' optnl host_list '}' {
1528 struct node_host *n;
1529 struct node_tinit *ti;
1531 for (n = $3; n != NULL; n = n->next) {
1532 switch (n->addr.type) {
1533 case PF_ADDR_ADDRMASK:
1536 yyerror("address ranges are not "
1537 "permitted inside tables");
1539 case PF_ADDR_DYNIFTL:
1540 yyerror("dynamic addresses are not "
1541 "permitted inside tables");
1544 yyerror("tables cannot contain tables");
1546 case PF_ADDR_NOROUTE:
1547 yyerror("\"no-route\" is not permitted "
1550 case PF_ADDR_URPFFAILED:
1551 yyerror("\"urpf-failed\" is not "
1552 "permitted inside tables");
1555 yyerror("unknown address type %d",
1560 if (!(ti = calloc(1, sizeof(*ti))))
1561 err(1, "table_opt: calloc");
1563 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1565 table_opts.init_addr = 1;
1568 struct node_tinit *ti;
1570 if (!(ti = calloc(1, sizeof(*ti))))
1571 err(1, "table_opt: calloc");
1573 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1575 table_opts.init_addr = 1;
1579 altqif : ALTQ interface queue_opts QUEUE qassign {
1582 if (check_rulestate(PFCTL_STATE_QUEUE))
1585 memset(&a, 0, sizeof(a));
1586 if ($3.scheduler.qtype == ALTQT_NONE) {
1587 yyerror("no scheduler specified!");
1590 a.scheduler = $3.scheduler.qtype;
1591 a.qlimit = $3.qlimit;
1592 a.tbrsize = $3.tbrsize;
1593 if ($5 == NULL && $3.scheduler.qtype != ALTQT_CODEL) {
1594 yyerror("no child queues specified");
1597 if (expand_altq(&a, $2, $5, $3.queue_bwspec,
1603 queuespec : QUEUE STRING interface queue_opts qassign {
1606 if (check_rulestate(PFCTL_STATE_QUEUE)) {
1611 memset(&a, 0, sizeof(a));
1613 if (strlcpy(a.qname, $2, sizeof(a.qname)) >=
1615 yyerror("queue name too long (max "
1616 "%d chars)", PF_QNAME_SIZE-1);
1622 yyerror("cannot specify tbrsize for queue");
1625 if ($4.priority > 255) {
1626 yyerror("priority out of range: max 255");
1629 a.priority = $4.priority;
1630 a.qlimit = $4.qlimit;
1631 a.scheduler = $4.scheduler.qtype;
1632 if (expand_queue(&a, $3, $5, $4.queue_bwspec,
1634 yyerror("errors in queue definition");
1641 bzero(&queue_opts, sizeof queue_opts);
1642 queue_opts.priority = DEFAULT_PRIORITY;
1643 queue_opts.qlimit = DEFAULT_QLIMIT;
1644 queue_opts.scheduler.qtype = ALTQT_NONE;
1645 queue_opts.queue_bwspec.bw_percent = 100;
1648 { $$ = queue_opts; }
1650 bzero(&queue_opts, sizeof queue_opts);
1651 queue_opts.priority = DEFAULT_PRIORITY;
1652 queue_opts.qlimit = DEFAULT_QLIMIT;
1653 queue_opts.scheduler.qtype = ALTQT_NONE;
1654 queue_opts.queue_bwspec.bw_percent = 100;
1659 queue_opts_l : queue_opts_l queue_opt
1663 queue_opt : BANDWIDTH bandwidth {
1664 if (queue_opts.marker & QOM_BWSPEC) {
1665 yyerror("bandwidth cannot be respecified");
1668 queue_opts.marker |= QOM_BWSPEC;
1669 queue_opts.queue_bwspec = $2;
1672 if (queue_opts.marker & QOM_PRIORITY) {
1673 yyerror("priority cannot be respecified");
1676 if ($2 < 0 || $2 > 255) {
1677 yyerror("priority out of range: max 255");
1680 queue_opts.marker |= QOM_PRIORITY;
1681 queue_opts.priority = $2;
1684 if (queue_opts.marker & QOM_QLIMIT) {
1685 yyerror("qlimit cannot be respecified");
1688 if ($2 < 0 || $2 > 65535) {
1689 yyerror("qlimit out of range: max 65535");
1692 queue_opts.marker |= QOM_QLIMIT;
1693 queue_opts.qlimit = $2;
1696 if (queue_opts.marker & QOM_SCHEDULER) {
1697 yyerror("scheduler cannot be respecified");
1700 queue_opts.marker |= QOM_SCHEDULER;
1701 queue_opts.scheduler = $1;
1704 if (queue_opts.marker & QOM_TBRSIZE) {
1705 yyerror("tbrsize cannot be respecified");
1708 if ($2 < 0 || $2 > UINT_MAX) {
1709 yyerror("tbrsize too big: max %u", UINT_MAX);
1712 queue_opts.marker |= QOM_TBRSIZE;
1713 queue_opts.tbrsize = $2;
1717 bandwidth : STRING {
1723 bps = strtod($1, &cp);
1725 if (strlen(cp) > 1) {
1727 if (!strcmp(cu, "Bit") ||
1729 !strcmp(cu, "bit") ||
1734 if (!strcmp(cp, "b"))
1736 else if (!strcmp(cp, "K"))
1738 else if (!strcmp(cp, "M"))
1740 else if (!strcmp(cp, "G"))
1741 bps *= 1000 * 1000 * 1000;
1742 else if (!strcmp(cp, "%")) {
1743 if (bps < 0 || bps > 100) {
1744 yyerror("bandwidth spec "
1749 $$.bw_percent = bps;
1752 yyerror("unknown unit %s", cp);
1758 $$.bw_absolute = (u_int64_t)bps;
1761 if ($1 < 0 || $1 >= LLONG_MAX) {
1762 yyerror("bandwidth number too big");
1766 $$.bw_absolute = $1;
1771 $$.qtype = ALTQT_CBQ;
1772 $$.data.cbq_opts.flags = 0;
1774 | CBQ '(' cbqflags_list ')' {
1775 $$.qtype = ALTQT_CBQ;
1776 $$.data.cbq_opts.flags = $3;
1779 $$.qtype = ALTQT_PRIQ;
1780 $$.data.priq_opts.flags = 0;
1782 | PRIQ '(' priqflags_list ')' {
1783 $$.qtype = ALTQT_PRIQ;
1784 $$.data.priq_opts.flags = $3;
1787 $$.qtype = ALTQT_HFSC;
1788 bzero(&$$.data.hfsc_opts,
1789 sizeof(struct node_hfsc_opts));
1791 | HFSC '(' hfsc_opts ')' {
1792 $$.qtype = ALTQT_HFSC;
1793 $$.data.hfsc_opts = $3;
1796 $$.qtype = ALTQT_FAIRQ;
1797 bzero(&$$.data.fairq_opts,
1798 sizeof(struct node_fairq_opts));
1800 | FAIRQ '(' fairq_opts ')' {
1801 $$.qtype = ALTQT_FAIRQ;
1802 $$.data.fairq_opts = $3;
1805 $$.qtype = ALTQT_CODEL;
1806 bzero(&$$.data.codel_opts,
1807 sizeof(struct codel_opts));
1809 | CODEL '(' codel_opts ')' {
1810 $$.qtype = ALTQT_CODEL;
1811 $$.data.codel_opts = $3;
1815 cbqflags_list : cbqflags_item { $$ |= $1; }
1816 | cbqflags_list comma cbqflags_item { $$ |= $3; }
1819 cbqflags_item : STRING {
1820 if (!strcmp($1, "default"))
1821 $$ = CBQCLF_DEFCLASS;
1822 else if (!strcmp($1, "borrow"))
1824 else if (!strcmp($1, "red"))
1826 else if (!strcmp($1, "ecn"))
1827 $$ = CBQCLF_RED|CBQCLF_ECN;
1828 else if (!strcmp($1, "rio"))
1830 else if (!strcmp($1, "codel"))
1833 yyerror("unknown cbq flag \"%s\"", $1);
1841 priqflags_list : priqflags_item { $$ |= $1; }
1842 | priqflags_list comma priqflags_item { $$ |= $3; }
1845 priqflags_item : STRING {
1846 if (!strcmp($1, "default"))
1847 $$ = PRCF_DEFAULTCLASS;
1848 else if (!strcmp($1, "red"))
1850 else if (!strcmp($1, "ecn"))
1851 $$ = PRCF_RED|PRCF_ECN;
1852 else if (!strcmp($1, "rio"))
1854 else if (!strcmp($1, "codel"))
1857 yyerror("unknown priq flag \"%s\"", $1);
1867 sizeof(struct node_hfsc_opts));
1874 hfscopts_list : hfscopts_item
1875 | hfscopts_list comma hfscopts_item
1878 hfscopts_item : LINKSHARE bandwidth {
1879 if (hfsc_opts.linkshare.used) {
1880 yyerror("linkshare already specified");
1883 hfsc_opts.linkshare.m2 = $2;
1884 hfsc_opts.linkshare.used = 1;
1886 | LINKSHARE '(' bandwidth comma NUMBER comma bandwidth ')'
1888 if ($5 < 0 || $5 > INT_MAX) {
1889 yyerror("timing in curve out of range");
1892 if (hfsc_opts.linkshare.used) {
1893 yyerror("linkshare already specified");
1896 hfsc_opts.linkshare.m1 = $3;
1897 hfsc_opts.linkshare.d = $5;
1898 hfsc_opts.linkshare.m2 = $7;
1899 hfsc_opts.linkshare.used = 1;
1901 | REALTIME bandwidth {
1902 if (hfsc_opts.realtime.used) {
1903 yyerror("realtime already specified");
1906 hfsc_opts.realtime.m2 = $2;
1907 hfsc_opts.realtime.used = 1;
1909 | REALTIME '(' bandwidth comma NUMBER comma bandwidth ')'
1911 if ($5 < 0 || $5 > INT_MAX) {
1912 yyerror("timing in curve out of range");
1915 if (hfsc_opts.realtime.used) {
1916 yyerror("realtime already specified");
1919 hfsc_opts.realtime.m1 = $3;
1920 hfsc_opts.realtime.d = $5;
1921 hfsc_opts.realtime.m2 = $7;
1922 hfsc_opts.realtime.used = 1;
1924 | UPPERLIMIT bandwidth {
1925 if (hfsc_opts.upperlimit.used) {
1926 yyerror("upperlimit already specified");
1929 hfsc_opts.upperlimit.m2 = $2;
1930 hfsc_opts.upperlimit.used = 1;
1932 | UPPERLIMIT '(' bandwidth comma NUMBER comma bandwidth ')'
1934 if ($5 < 0 || $5 > INT_MAX) {
1935 yyerror("timing in curve out of range");
1938 if (hfsc_opts.upperlimit.used) {
1939 yyerror("upperlimit already specified");
1942 hfsc_opts.upperlimit.m1 = $3;
1943 hfsc_opts.upperlimit.d = $5;
1944 hfsc_opts.upperlimit.m2 = $7;
1945 hfsc_opts.upperlimit.used = 1;
1948 if (!strcmp($1, "default"))
1949 hfsc_opts.flags |= HFCF_DEFAULTCLASS;
1950 else if (!strcmp($1, "red"))
1951 hfsc_opts.flags |= HFCF_RED;
1952 else if (!strcmp($1, "ecn"))
1953 hfsc_opts.flags |= HFCF_RED|HFCF_ECN;
1954 else if (!strcmp($1, "rio"))
1955 hfsc_opts.flags |= HFCF_RIO;
1956 else if (!strcmp($1, "codel"))
1957 hfsc_opts.flags |= HFCF_CODEL;
1959 yyerror("unknown hfsc flag \"%s\"", $1);
1969 sizeof(struct node_fairq_opts));
1976 fairqopts_list : fairqopts_item
1977 | fairqopts_list comma fairqopts_item
1980 fairqopts_item : LINKSHARE bandwidth {
1981 if (fairq_opts.linkshare.used) {
1982 yyerror("linkshare already specified");
1985 fairq_opts.linkshare.m2 = $2;
1986 fairq_opts.linkshare.used = 1;
1988 | LINKSHARE '(' bandwidth number bandwidth ')' {
1989 if (fairq_opts.linkshare.used) {
1990 yyerror("linkshare already specified");
1993 fairq_opts.linkshare.m1 = $3;
1994 fairq_opts.linkshare.d = $4;
1995 fairq_opts.linkshare.m2 = $5;
1996 fairq_opts.linkshare.used = 1;
1999 fairq_opts.hogs_bw = $2;
2002 fairq_opts.nbuckets = $2;
2005 if (!strcmp($1, "default"))
2006 fairq_opts.flags |= FARF_DEFAULTCLASS;
2007 else if (!strcmp($1, "red"))
2008 fairq_opts.flags |= FARF_RED;
2009 else if (!strcmp($1, "ecn"))
2010 fairq_opts.flags |= FARF_RED|FARF_ECN;
2011 else if (!strcmp($1, "rio"))
2012 fairq_opts.flags |= FARF_RIO;
2013 else if (!strcmp($1, "codel"))
2014 fairq_opts.flags |= FARF_CODEL;
2016 yyerror("unknown fairq flag \"%s\"", $1);
2026 sizeof(struct codel_opts));
2033 codelopts_list : codelopts_item
2034 | codelopts_list comma codelopts_item
2037 codelopts_item : INTERVAL number {
2038 if (codel_opts.interval) {
2039 yyerror("interval already specified");
2042 codel_opts.interval = $2;
2045 if (codel_opts.target) {
2046 yyerror("target already specified");
2049 codel_opts.target = $2;
2052 if (!strcmp($1, "ecn"))
2055 yyerror("unknown codel option \"%s\"", $1);
2063 qassign : /* empty */ { $$ = NULL; }
2064 | qassign_item { $$ = $1; }
2065 | '{' optnl qassign_list '}' { $$ = $3; }
2068 qassign_list : qassign_item optnl { $$ = $1; }
2069 | qassign_list comma qassign_item optnl {
2070 $1->tail->next = $3;
2076 qassign_item : STRING {
2077 $$ = calloc(1, sizeof(struct node_queue));
2079 err(1, "qassign_item: calloc");
2080 if (strlcpy($$->queue, $1, sizeof($$->queue)) >=
2081 sizeof($$->queue)) {
2082 yyerror("queue name '%s' too long (max "
2083 "%d chars)", $1, sizeof($$->queue)-1);
2094 pfrule : action dir logquick interface route af proto fromto
2097 struct pfctl_rule r;
2098 struct node_state_opt *o;
2099 struct node_proto *proto;
2105 if (check_rulestate(PFCTL_STATE_FILTER))
2108 memset(&r, 0, sizeof(r));
2112 case PFRULE_RETURNRST:
2113 r.rule_flag |= PFRULE_RETURNRST;
2114 r.return_ttl = $1.w;
2116 case PFRULE_RETURNICMP:
2117 r.rule_flag |= PFRULE_RETURNICMP;
2118 r.return_icmp = $1.w;
2119 r.return_icmp6 = $1.w2;
2122 r.rule_flag |= PFRULE_RETURN;
2123 r.return_icmp = $1.w;
2124 r.return_icmp6 = $1.w2;
2132 r.rtableid = $9.rtableid;
2134 if ($9.marker & FOM_PRIO) {
2136 r.prio = PF_PRIO_ZERO;
2140 if ($9.marker & FOM_SETPRIO) {
2141 r.set_prio[0] = $9.set_prio[0];
2142 r.set_prio[1] = $9.set_prio[1];
2143 r.scrub_flags |= PFSTATE_SETPRIO;
2148 if (strlcpy(r.tagname, $9.tag,
2149 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
2150 yyerror("tag too long, max %u chars",
2151 PF_TAG_NAME_SIZE - 1);
2155 if (strlcpy(r.match_tagname, $9.match_tag,
2156 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
2157 yyerror("tag too long, max %u chars",
2158 PF_TAG_NAME_SIZE - 1);
2161 r.match_tag_not = $9.match_tag_not;
2162 if (rule_label(&r, $9.label))
2164 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++)
2166 r.ridentifier = $9.ridentifier;
2167 r.flags = $9.flags.b1;
2168 r.flagset = $9.flags.b2;
2169 if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
2170 yyerror("flags always false");
2173 if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
2174 for (proto = $7; proto != NULL &&
2175 proto->proto != IPPROTO_TCP;
2176 proto = proto->next)
2178 if (proto == NULL && $7 != NULL) {
2179 if ($9.flags.b1 || $9.flags.b2)
2181 "flags only apply to tcp");
2184 "OS fingerprinting only "
2189 if (($9.flags.b1 & parse_flags("S")) == 0 &&
2191 yyerror("OS fingerprinting requires "
2192 "the SYN TCP flag (flags S/SA)");
2199 r.keep_state = $9.keep.action;
2200 o = $9.keep.options;
2202 /* 'keep state' by default on pass rules. */
2203 if (!r.keep_state && !r.action &&
2204 !($9.marker & FOM_KEEP)) {
2205 r.keep_state = PF_STATE_NORMAL;
2206 o = keep_state_defaults;
2211 struct node_state_opt *p = o;
2214 case PF_STATE_OPT_MAX:
2216 yyerror("state option 'max' "
2217 "multiple definitions");
2220 r.max_states = o->data.max_states;
2222 case PF_STATE_OPT_NOSYNC:
2223 if (r.rule_flag & PFRULE_NOSYNC) {
2224 yyerror("state option 'sync' "
2225 "multiple definitions");
2228 r.rule_flag |= PFRULE_NOSYNC;
2230 case PF_STATE_OPT_SRCTRACK:
2232 yyerror("state option "
2234 "multiple definitions");
2237 srctrack = o->data.src_track;
2238 r.rule_flag |= PFRULE_SRCTRACK;
2240 case PF_STATE_OPT_MAX_SRC_STATES:
2241 if (r.max_src_states) {
2242 yyerror("state option "
2244 "multiple definitions");
2247 if (o->data.max_src_states == 0) {
2248 yyerror("'max-src-states' must "
2253 o->data.max_src_states;
2254 r.rule_flag |= PFRULE_SRCTRACK;
2256 case PF_STATE_OPT_OVERLOAD:
2257 if (r.overload_tblname[0]) {
2258 yyerror("multiple 'overload' "
2259 "table definitions");
2262 if (strlcpy(r.overload_tblname,
2263 o->data.overload.tblname,
2264 PF_TABLE_NAME_SIZE) >=
2265 PF_TABLE_NAME_SIZE) {
2266 yyerror("state option: "
2270 r.flush = o->data.overload.flush;
2272 case PF_STATE_OPT_MAX_SRC_CONN:
2273 if (r.max_src_conn) {
2274 yyerror("state option "
2276 "multiple definitions");
2279 if (o->data.max_src_conn == 0) {
2280 yyerror("'max-src-conn' "
2285 o->data.max_src_conn;
2286 r.rule_flag |= PFRULE_SRCTRACK |
2287 PFRULE_RULESRCTRACK;
2289 case PF_STATE_OPT_MAX_SRC_CONN_RATE:
2290 if (r.max_src_conn_rate.limit) {
2291 yyerror("state option "
2292 "'max-src-conn-rate' "
2293 "multiple definitions");
2296 if (!o->data.max_src_conn_rate.limit ||
2297 !o->data.max_src_conn_rate.seconds) {
2298 yyerror("'max-src-conn-rate' "
2299 "values must be > 0");
2302 if (o->data.max_src_conn_rate.limit >
2304 yyerror("'max-src-conn-rate' "
2305 "maximum rate must be < %u",
2309 r.max_src_conn_rate.limit =
2310 o->data.max_src_conn_rate.limit;
2311 r.max_src_conn_rate.seconds =
2312 o->data.max_src_conn_rate.seconds;
2313 r.rule_flag |= PFRULE_SRCTRACK |
2314 PFRULE_RULESRCTRACK;
2316 case PF_STATE_OPT_MAX_SRC_NODES:
2317 if (r.max_src_nodes) {
2318 yyerror("state option "
2320 "multiple definitions");
2323 if (o->data.max_src_nodes == 0) {
2324 yyerror("'max-src-nodes' must "
2329 o->data.max_src_nodes;
2330 r.rule_flag |= PFRULE_SRCTRACK |
2331 PFRULE_RULESRCTRACK;
2333 case PF_STATE_OPT_STATELOCK:
2335 yyerror("state locking option: "
2336 "multiple definitions");
2340 r.rule_flag |= o->data.statelock;
2342 case PF_STATE_OPT_SLOPPY:
2343 if (r.rule_flag & PFRULE_STATESLOPPY) {
2344 yyerror("state sloppy option: "
2345 "multiple definitions");
2348 r.rule_flag |= PFRULE_STATESLOPPY;
2350 case PF_STATE_OPT_TIMEOUT:
2351 if (o->data.timeout.number ==
2352 PFTM_ADAPTIVE_START ||
2353 o->data.timeout.number ==
2356 if (r.timeout[o->data.timeout.number]) {
2357 yyerror("state timeout %s "
2358 "multiple definitions",
2359 pf_timeouts[o->data.
2360 timeout.number].name);
2363 r.timeout[o->data.timeout.number] =
2364 o->data.timeout.seconds;
2371 /* 'flags S/SA' by default on stateful rules */
2372 if (!r.action && !r.flags && !r.flagset &&
2373 !$9.fragment && !($9.marker & FOM_FLAGS) &&
2375 r.flags = parse_flags("S");
2376 r.flagset = parse_flags("SA");
2378 if (!adaptive && r.max_states) {
2379 r.timeout[PFTM_ADAPTIVE_START] =
2380 (r.max_states / 10) * 6;
2381 r.timeout[PFTM_ADAPTIVE_END] =
2382 (r.max_states / 10) * 12;
2384 if (r.rule_flag & PFRULE_SRCTRACK) {
2385 if (srctrack == PF_SRCTRACK_GLOBAL &&
2387 yyerror("'max-src-nodes' is "
2388 "incompatible with "
2389 "'source-track global'");
2392 if (srctrack == PF_SRCTRACK_GLOBAL &&
2394 yyerror("'max-src-conn' is "
2395 "incompatible with "
2396 "'source-track global'");
2399 if (srctrack == PF_SRCTRACK_GLOBAL &&
2400 r.max_src_conn_rate.seconds) {
2401 yyerror("'max-src-conn-rate' is "
2402 "incompatible with "
2403 "'source-track global'");
2406 if (r.timeout[PFTM_SRC_NODE] <
2407 r.max_src_conn_rate.seconds)
2408 r.timeout[PFTM_SRC_NODE] =
2409 r.max_src_conn_rate.seconds;
2410 r.rule_flag |= PFRULE_SRCTRACK;
2411 if (srctrack == PF_SRCTRACK_RULE)
2412 r.rule_flag |= PFRULE_RULESRCTRACK;
2414 if (r.keep_state && !statelock)
2415 r.rule_flag |= default_statelock;
2418 r.rule_flag |= PFRULE_FRAGMENT;
2419 r.allow_opts = $9.allowopts;
2421 decide_address_family($8.src.host, &r.af);
2422 decide_address_family($8.dst.host, &r.af);
2426 yyerror("direction must be explicit "
2427 "with rules that specify routing");
2431 r.rpool.opts = $5.pool_opts;
2433 memcpy(&r.rpool.key, $5.key,
2434 sizeof(struct pf_poolhashkey));
2437 decide_address_family($5.host, &r.af);
2438 remove_invalid_hosts(&$5.host, &r.af);
2439 if ($5.host == NULL) {
2440 yyerror("no routing address with "
2441 "matching address family found.");
2444 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
2445 PF_POOL_NONE && ($5.host->next != NULL ||
2446 $5.host->addr.type == PF_ADDR_TABLE ||
2447 DYNIF_MULTIADDR($5.host->addr)))
2448 r.rpool.opts |= PF_POOL_ROUNDROBIN;
2449 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2450 PF_POOL_ROUNDROBIN &&
2451 disallow_table($5.host, "tables are only "
2452 "supported in round-robin routing pools"))
2454 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2455 PF_POOL_ROUNDROBIN &&
2456 disallow_alias($5.host, "interface (%s) "
2457 "is only supported in round-robin "
2460 if ($5.host->next != NULL) {
2461 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2462 PF_POOL_ROUNDROBIN) {
2463 yyerror("r.rpool.opts must "
2464 "be PF_POOL_ROUNDROBIN");
2469 if ($9.queues.qname != NULL) {
2470 if (strlcpy(r.qname, $9.queues.qname,
2471 sizeof(r.qname)) >= sizeof(r.qname)) {
2472 yyerror("rule qname too long (max "
2473 "%d chars)", sizeof(r.qname)-1);
2476 free($9.queues.qname);
2478 if ($9.queues.pqname != NULL) {
2479 if (strlcpy(r.pqname, $9.queues.pqname,
2480 sizeof(r.pqname)) >= sizeof(r.pqname)) {
2481 yyerror("rule pqname too long (max "
2482 "%d chars)", sizeof(r.pqname)-1);
2485 free($9.queues.pqname);
2488 r.divert.port = $9.divert.port;
2490 if ((r.divert.port = $9.divert.port)) {
2491 if (r.direction == PF_OUT) {
2492 if ($9.divert.addr) {
2493 yyerror("address specified "
2494 "for outgoing divert");
2497 bzero(&r.divert.addr,
2498 sizeof(r.divert.addr));
2500 if (!$9.divert.addr) {
2501 yyerror("no address specified "
2502 "for incoming divert");
2505 if ($9.divert.addr->af != r.af) {
2506 yyerror("address family "
2507 "mismatch for divert");
2511 $9.divert.addr->addr.v.a.addr;
2516 if ($9.dnpipe || $9.dnrpipe) {
2517 r.dnpipe = $9.dnpipe;
2518 r.dnrpipe = $9.dnrpipe;
2519 if ($9.free_flags & PFRULE_DN_IS_PIPE)
2520 r.free_flags |= PFRULE_DN_IS_PIPE;
2522 r.free_flags |= PFRULE_DN_IS_QUEUE;
2525 expand_rule(&r, $4, $5.host, $7, $8.src_os,
2526 $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
2527 $9.uid, $9.gid, $9.icmpspec, "");
2532 bzero(&filter_opts, sizeof filter_opts);
2533 filter_opts.rtableid = -1;
2536 { $$ = filter_opts; }
2538 bzero(&filter_opts, sizeof filter_opts);
2539 filter_opts.rtableid = -1;
2544 filter_opts_l : filter_opts_l filter_opt
2548 filter_opt : USER uids {
2549 if (filter_opts.uid)
2550 $2->tail->next = filter_opts.uid;
2551 filter_opts.uid = $2;
2554 if (filter_opts.gid)
2555 $2->tail->next = filter_opts.gid;
2556 filter_opts.gid = $2;
2559 if (filter_opts.marker & FOM_FLAGS) {
2560 yyerror("flags cannot be redefined");
2563 filter_opts.marker |= FOM_FLAGS;
2564 filter_opts.flags.b1 |= $1.b1;
2565 filter_opts.flags.b2 |= $1.b2;
2566 filter_opts.flags.w |= $1.w;
2567 filter_opts.flags.w2 |= $1.w2;
2570 if (filter_opts.marker & FOM_ICMP) {
2571 yyerror("icmp-type cannot be redefined");
2574 filter_opts.marker |= FOM_ICMP;
2575 filter_opts.icmpspec = $1;
2578 if (filter_opts.marker & FOM_PRIO) {
2579 yyerror("prio cannot be redefined");
2582 if ($2 < 0 || $2 > PF_PRIO_MAX) {
2583 yyerror("prio must be 0 - %u", PF_PRIO_MAX);
2586 filter_opts.marker |= FOM_PRIO;
2587 filter_opts.prio = $2;
2590 if (filter_opts.marker & FOM_TOS) {
2591 yyerror("tos cannot be redefined");
2594 filter_opts.marker |= FOM_TOS;
2595 filter_opts.tos = $2;
2598 if (filter_opts.marker & FOM_KEEP) {
2599 yyerror("modulate or keep cannot be redefined");
2602 filter_opts.marker |= FOM_KEEP;
2603 filter_opts.keep.action = $1.action;
2604 filter_opts.keep.options = $1.options;
2606 | RIDENTIFIER number {
2607 filter_opts.ridentifier = $2;
2610 filter_opts.fragment = 1;
2613 filter_opts.allowopts = 1;
2616 if (filter_opts.labelcount >= PF_RULE_MAX_LABEL_COUNT) {
2617 yyerror("label can only be used %d times", PF_RULE_MAX_LABEL_COUNT);
2620 filter_opts.label[filter_opts.labelcount++] = $1;
2623 if (filter_opts.queues.qname) {
2624 yyerror("queue cannot be redefined");
2627 filter_opts.queues = $1;
2630 filter_opts.dnpipe = $2;
2631 filter_opts.free_flags |= PFRULE_DN_IS_PIPE;
2633 | DNPIPE '(' number ')' {
2634 filter_opts.dnpipe = $3;
2635 filter_opts.free_flags |= PFRULE_DN_IS_PIPE;
2637 | DNPIPE '(' number comma number ')' {
2638 filter_opts.dnrpipe = $5;
2639 filter_opts.dnpipe = $3;
2640 filter_opts.free_flags |= PFRULE_DN_IS_PIPE;
2643 filter_opts.dnpipe = $2;
2644 filter_opts.free_flags |= PFRULE_DN_IS_QUEUE;
2646 | DNQUEUE '(' number comma number ')' {
2647 filter_opts.dnrpipe = $5;
2648 filter_opts.dnpipe = $3;
2649 filter_opts.free_flags |= PFRULE_DN_IS_QUEUE;
2651 | DNQUEUE '(' number ')' {
2652 filter_opts.dnpipe = $3;
2653 filter_opts.free_flags |= PFRULE_DN_IS_QUEUE;
2656 filter_opts.tag = $2;
2658 | not TAGGED string {
2659 filter_opts.match_tag = $3;
2660 filter_opts.match_tag_not = $1;
2662 | PROBABILITY probability {
2665 p = floor($2 * UINT_MAX + 0.5);
2666 if (p < 0.0 || p > UINT_MAX) {
2667 yyerror("invalid probability: %lf", p);
2670 filter_opts.prob = (u_int32_t)p;
2671 if (filter_opts.prob == 0)
2672 filter_opts.prob = 1;
2675 if ($2 < 0 || $2 > rt_tableid_max()) {
2676 yyerror("invalid rtable id");
2679 filter_opts.rtableid = $2;
2681 | DIVERTTO portplain {
2683 filter_opts.divert.port = $2.a;
2684 if (!filter_opts.divert.port) {
2685 yyerror("invalid divert port: %u", ntohs($2.a));
2690 | DIVERTTO STRING PORT portplain {
2692 if ((filter_opts.divert.addr = host($2)) == NULL) {
2693 yyerror("could not parse divert address: %s",
2702 filter_opts.divert.port = $4.a;
2703 if (!filter_opts.divert.port) {
2704 yyerror("invalid divert port: %u", ntohs($4.a));
2710 yyerror("divert-reply has no meaning in FreeBSD pf(4)");
2713 filter_opts.divert.port = 1; /* some random value */
2719 filter_sets : SET '(' filter_sets_l ')' { $$ = filter_opts; }
2720 | SET filter_set { $$ = filter_opts; }
2723 filter_sets_l : filter_sets_l comma filter_set
2728 if (filter_opts.marker & FOM_SETPRIO) {
2729 yyerror("prio cannot be redefined");
2732 filter_opts.marker |= FOM_SETPRIO;
2733 filter_opts.set_prio[0] = $1.b1;
2734 filter_opts.set_prio[1] = $1.b2;
2736 prio : PRIO NUMBER {
2737 if ($2 < 0 || $2 > PF_PRIO_MAX) {
2738 yyerror("prio must be 0 - %u", PF_PRIO_MAX);
2743 | PRIO '(' NUMBER comma NUMBER ')' {
2744 if ($3 < 0 || $3 > PF_PRIO_MAX ||
2745 $5 < 0 || $5 > PF_PRIO_MAX) {
2746 yyerror("prio must be 0 - %u", PF_PRIO_MAX);
2754 probability : STRING {
2756 double p = strtod($1, &e);
2763 yyerror("invalid probability: %s", $1);
2779 $$.w = returnicmpdefault;
2780 $$.w2 = returnicmp6default;
2782 | MATCH { $$.b1 = PF_MATCH; $$.b2 = $$.w = 0; }
2783 | BLOCK blockspec { $$ = $2; $$.b1 = PF_DROP; }
2786 blockspec : /* empty */ {
2787 $$.b2 = blockpolicy;
2788 $$.w = returnicmpdefault;
2789 $$.w2 = returnicmp6default;
2792 $$.b2 = PFRULE_DROP;
2797 $$.b2 = PFRULE_RETURNRST;
2801 | RETURNRST '(' TTL NUMBER ')' {
2802 if ($4 < 0 || $4 > 255) {
2803 yyerror("illegal ttl value %d", $4);
2806 $$.b2 = PFRULE_RETURNRST;
2811 $$.b2 = PFRULE_RETURNICMP;
2812 $$.w = returnicmpdefault;
2813 $$.w2 = returnicmp6default;
2816 $$.b2 = PFRULE_RETURNICMP;
2817 $$.w = returnicmpdefault;
2818 $$.w2 = returnicmp6default;
2820 | RETURNICMP '(' reticmpspec ')' {
2821 $$.b2 = PFRULE_RETURNICMP;
2823 $$.w2 = returnicmpdefault;
2825 | RETURNICMP6 '(' reticmp6spec ')' {
2826 $$.b2 = PFRULE_RETURNICMP;
2827 $$.w = returnicmpdefault;
2830 | RETURNICMP '(' reticmpspec comma reticmp6spec ')' {
2831 $$.b2 = PFRULE_RETURNICMP;
2836 $$.b2 = PFRULE_RETURN;
2837 $$.w = returnicmpdefault;
2838 $$.w2 = returnicmp6default;
2842 reticmpspec : STRING {
2843 if (!($$ = parseicmpspec($1, AF_INET))) {
2852 if ($1 < 0 || $1 > 255) {
2853 yyerror("invalid icmp code %lu", $1);
2856 icmptype = returnicmpdefault >> 8;
2857 $$ = (icmptype << 8 | $1);
2861 reticmp6spec : STRING {
2862 if (!($$ = parseicmpspec($1, AF_INET6))) {
2871 if ($1 < 0 || $1 > 255) {
2872 yyerror("invalid icmp code %lu", $1);
2875 icmptype = returnicmp6default >> 8;
2876 $$ = (icmptype << 8 | $1);
2880 dir : /* empty */ { $$ = PF_INOUT; }
2881 | IN { $$ = PF_IN; }
2882 | OUT { $$ = PF_OUT; }
2885 quick : /* empty */ { $$.quick = 0; }
2886 | QUICK { $$.quick = 1; }
2889 logquick : /* empty */ { $$.log = 0; $$.quick = 0; $$.logif = 0; }
2890 | log { $$ = $1; $$.quick = 0; }
2891 | QUICK { $$.quick = 1; $$.log = 0; $$.logif = 0; }
2892 | log QUICK { $$ = $1; $$.quick = 1; }
2893 | QUICK log { $$ = $2; $$.quick = 1; }
2896 log : LOG { $$.log = PF_LOG; $$.logif = 0; }
2897 | LOG '(' logopts ')' {
2898 $$.log = PF_LOG | $3.log;
2899 $$.logif = $3.logif;
2903 logopts : logopt { $$ = $1; }
2904 | logopts comma logopt {
2905 $$.log = $1.log | $3.log;
2906 $$.logif = $3.logif;
2908 $$.logif = $1.logif;
2912 logopt : ALL { $$.log = PF_LOG_ALL; $$.logif = 0; }
2913 | USER { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
2914 | GROUP { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
2920 if (strncmp($2, "pflog", 5)) {
2921 yyerror("%s: should be a pflog interface", $2);
2925 i = strtonum($2 + 5, 0, 255, &errstr);
2927 yyerror("%s: %s", $2, errstr);
2936 interface : /* empty */ { $$ = NULL; }
2937 | ON if_item_not { $$ = $2; }
2938 | ON '{' optnl if_list '}' { $$ = $4; }
2941 if_list : if_item_not optnl { $$ = $1; }
2942 | if_list comma if_item_not optnl {
2943 $1->tail->next = $3;
2949 if_item_not : not if_item { $$ = $2; $$->not = $1; }
2953 struct node_host *n;
2955 $$ = calloc(1, sizeof(struct node_if));
2957 err(1, "if_item: calloc");
2958 if (strlcpy($$->ifname, $1, sizeof($$->ifname)) >=
2959 sizeof($$->ifname)) {
2962 yyerror("interface name too long");
2966 if ((n = ifa_exists($1)) != NULL)
2967 $$->ifa_flags = n->ifa_flags;
2976 af : /* empty */ { $$ = 0; }
2977 | INET { $$ = AF_INET; }
2978 | INET6 { $$ = AF_INET6; }
2981 proto : /* empty */ { $$ = NULL; }
2982 | PROTO proto_item { $$ = $2; }
2983 | PROTO '{' optnl proto_list '}' { $$ = $4; }
2986 proto_list : proto_item optnl { $$ = $1; }
2987 | proto_list comma proto_item optnl {
2988 $1->tail->next = $3;
2994 proto_item : protoval {
2999 yyerror("proto 0 cannot be used");
3002 $$ = calloc(1, sizeof(struct node_proto));
3004 err(1, "proto_item: calloc");
3014 p = getprotobyname($1);
3016 yyerror("unknown protocol %s", $1);
3024 if ($1 < 0 || $1 > 255) {
3025 yyerror("protocol outside range");
3045 os : /* empty */ { $$ = NULL; }
3046 | OS xos { $$ = $2; }
3047 | OS '{' optnl os_list '}' { $$ = $4; }
3051 $$ = calloc(1, sizeof(struct node_os));
3053 err(1, "os: calloc");
3059 os_list : xos optnl { $$ = $1; }
3060 | os_list comma xos optnl {
3061 $1->tail->next = $3;
3067 from : /* empty */ {
3081 if (disallow_urpf_failed($2.host, "\"urpf-failed\" is "
3082 "not permitted in a destination address"))
3088 ipportspec : ipspec {
3092 | ipspec PORT portspec {
3106 ipspec : ANY { $$ = NULL; }
3107 | xhost { $$ = $1; }
3108 | '{' optnl host_list '}' { $$ = $3; }
3111 toipspec : TO ipspec { $$ = $2; }
3112 | /* empty */ { $$ = NULL; }
3115 host_list : ipspec optnl { $$ = $1; }
3116 | host_list comma ipspec optnl {
3119 else if ($1 == NULL)
3122 $1->tail->next = $3;
3123 $1->tail = $3->tail;
3130 struct node_host *n;
3132 for (n = $2; n != NULL; n = n->next)
3137 $$ = calloc(1, sizeof(struct node_host));
3139 err(1, "xhost: calloc");
3140 $$->addr.type = PF_ADDR_NOROUTE;
3146 $$ = calloc(1, sizeof(struct node_host));
3148 err(1, "xhost: calloc");
3149 $$->addr.type = PF_ADDR_URPFFAILED;
3157 if (($$ = host($1)) == NULL) {
3158 /* error. "any" is handled elsewhere */
3160 yyerror("could not parse host specification");
3166 | STRING '-' STRING {
3167 struct node_host *b, *e;
3169 if ((b = host($1)) == NULL || (e = host($3)) == NULL) {
3172 yyerror("could not parse host specification");
3175 if (b->af != e->af ||
3176 b->addr.type != PF_ADDR_ADDRMASK ||
3177 e->addr.type != PF_ADDR_ADDRMASK ||
3178 unmask(&b->addr.v.a.mask, b->af) !=
3179 (b->af == AF_INET ? 32 : 128) ||
3180 unmask(&e->addr.v.a.mask, e->af) !=
3181 (e->af == AF_INET ? 32 : 128) ||
3182 b->next != NULL || b->not ||
3183 e->next != NULL || e->not) {
3188 yyerror("invalid address range");
3191 memcpy(&b->addr.v.a.mask, &e->addr.v.a.addr,
3192 sizeof(b->addr.v.a.mask));
3193 b->addr.type = PF_ADDR_RANGE;
3199 | STRING '/' NUMBER {
3202 if (asprintf(&buf, "%s/%lld", $1, (long long)$3) == -1)
3203 err(1, "host: asprintf");
3205 if (($$ = host(buf)) == NULL) {
3206 /* error. "any" is handled elsewhere */
3208 yyerror("could not parse host specification");
3213 | NUMBER '/' NUMBER {
3216 /* ie. for 10/8 parsing */
3218 if (asprintf(&buf, "%lld/%lld", (long long)$1, (long long)$3) == -1)
3220 if (asprintf(&buf, "%lld/%lld", $1, $3) == -1)
3222 err(1, "host: asprintf");
3223 if (($$ = host(buf)) == NULL) {
3224 /* error. "any" is handled elsewhere */
3226 yyerror("could not parse host specification");
3232 | dynaddr '/' NUMBER {
3233 struct node_host *n;
3235 if ($3 < 0 || $3 > 128) {
3236 yyerror("bit number too big");
3240 for (n = $1; n != NULL; n = n->next)
3244 if (strlen($2) >= PF_TABLE_NAME_SIZE) {
3245 yyerror("table name '%s' too long", $2);
3249 $$ = calloc(1, sizeof(struct node_host));
3251 err(1, "host: calloc");
3252 $$->addr.type = PF_ADDR_TABLE;
3253 if (strlcpy($$->addr.v.tblname, $2,
3254 sizeof($$->addr.v.tblname)) >=
3255 sizeof($$->addr.v.tblname))
3256 errx(1, "host: strlcpy");
3267 if (atoul($1, &ulval) == -1) {
3268 yyerror("%s is not a number", $1);
3277 dynaddr : '(' STRING ')' {
3282 if (!isalpha(op[0])) {
3283 yyerror("invalid interface name '%s'", op);
3287 while ((p = strrchr($2, ':')) != NULL) {
3288 if (!strcmp(p+1, "network"))
3289 flags |= PFI_AFLAG_NETWORK;
3290 else if (!strcmp(p+1, "broadcast"))
3291 flags |= PFI_AFLAG_BROADCAST;
3292 else if (!strcmp(p+1, "peer"))
3293 flags |= PFI_AFLAG_PEER;
3294 else if (!strcmp(p+1, "0"))
3295 flags |= PFI_AFLAG_NOALIAS;
3297 yyerror("interface %s has bad modifier",
3304 if (flags & (flags - 1) & PFI_AFLAG_MODEMASK) {
3306 yyerror("illegal combination of "
3307 "interface modifiers");
3310 $$ = calloc(1, sizeof(struct node_host));
3312 err(1, "address: calloc");
3314 set_ipmask($$, 128);
3315 $$->addr.type = PF_ADDR_DYNIFTL;
3316 $$->addr.iflags = flags;
3317 if (strlcpy($$->addr.v.ifname, $2,
3318 sizeof($$->addr.v.ifname)) >=
3319 sizeof($$->addr.v.ifname)) {
3322 yyerror("interface name too long");
3331 portspec : port_item { $$ = $1; }
3332 | '{' optnl port_list '}' { $$ = $3; }
3335 port_list : port_item optnl { $$ = $1; }
3336 | port_list comma port_item optnl {
3337 $1->tail->next = $3;
3343 port_item : portrange {
3344 $$ = calloc(1, sizeof(struct node_port));
3346 err(1, "port_item: calloc");
3356 | unaryop portrange {
3358 yyerror("':' cannot be used with an other "
3362 $$ = calloc(1, sizeof(struct node_port));
3364 err(1, "port_item: calloc");
3371 | portrange PORTBINARY portrange {
3373 yyerror("':' cannot be used with an other "
3377 $$ = calloc(1, sizeof(struct node_port));
3379 err(1, "port_item: calloc");
3388 portplain : numberstring {
3389 if (parseport($1, &$$, 0) == -1) {
3397 portrange : numberstring {
3398 if (parseport($1, &$$, PPORT_RANGE) == -1) {
3406 uids : uid_item { $$ = $1; }
3407 | '{' optnl uid_list '}' { $$ = $3; }
3410 uid_list : uid_item optnl { $$ = $1; }
3411 | uid_list comma uid_item optnl {
3412 $1->tail->next = $3;
3419 $$ = calloc(1, sizeof(struct node_uid));
3421 err(1, "uid_item: calloc");
3429 if ($2 == UID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
3430 yyerror("user unknown requires operator = or "
3434 $$ = calloc(1, sizeof(struct node_uid));
3436 err(1, "uid_item: calloc");
3443 | uid PORTBINARY uid {
3444 if ($1 == UID_MAX || $3 == UID_MAX) {
3445 yyerror("user unknown requires operator = or "
3449 $$ = calloc(1, sizeof(struct node_uid));
3451 err(1, "uid_item: calloc");
3461 if (!strcmp($1, "unknown"))
3466 if ((pw = getpwnam($1)) == NULL) {
3467 yyerror("unknown user %s", $1);
3476 if ($1 < 0 || $1 >= UID_MAX) {
3477 yyerror("illegal uid value %lu", $1);
3484 gids : gid_item { $$ = $1; }
3485 | '{' optnl gid_list '}' { $$ = $3; }
3488 gid_list : gid_item optnl { $$ = $1; }
3489 | gid_list comma gid_item optnl {
3490 $1->tail->next = $3;
3497 $$ = calloc(1, sizeof(struct node_gid));
3499 err(1, "gid_item: calloc");
3507 if ($2 == GID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
3508 yyerror("group unknown requires operator = or "
3512 $$ = calloc(1, sizeof(struct node_gid));
3514 err(1, "gid_item: calloc");
3521 | gid PORTBINARY gid {
3522 if ($1 == GID_MAX || $3 == GID_MAX) {
3523 yyerror("group unknown requires operator = or "
3527 $$ = calloc(1, sizeof(struct node_gid));
3529 err(1, "gid_item: calloc");
3539 if (!strcmp($1, "unknown"))
3544 if ((grp = getgrnam($1)) == NULL) {
3545 yyerror("unknown group %s", $1);
3554 if ($1 < 0 || $1 >= GID_MAX) {
3555 yyerror("illegal gid value %lu", $1);
3565 if ((f = parse_flags($1)) < 0) {
3566 yyerror("bad flags %s", $1);
3575 flags : FLAGS flag '/' flag { $$.b1 = $2.b1; $$.b2 = $4.b1; }
3576 | FLAGS '/' flag { $$.b1 = 0; $$.b2 = $3.b1; }
3577 | FLAGS ANY { $$.b1 = 0; $$.b2 = 0; }
3580 icmpspec : ICMPTYPE icmp_item { $$ = $2; }
3581 | ICMPTYPE '{' optnl icmp_list '}' { $$ = $4; }
3582 | ICMP6TYPE icmp6_item { $$ = $2; }
3583 | ICMP6TYPE '{' optnl icmp6_list '}' { $$ = $4; }
3586 icmp_list : icmp_item optnl { $$ = $1; }
3587 | icmp_list comma icmp_item optnl {
3588 $1->tail->next = $3;
3594 icmp6_list : icmp6_item optnl { $$ = $1; }
3595 | icmp6_list comma icmp6_item optnl {
3596 $1->tail->next = $3;
3602 icmp_item : icmptype {
3603 $$ = calloc(1, sizeof(struct node_icmp));
3605 err(1, "icmp_item: calloc");
3608 $$->proto = IPPROTO_ICMP;
3612 | icmptype CODE STRING {
3613 const struct icmpcodeent *p;
3615 if ((p = geticmpcodebyname($1-1, $3, AF_INET)) == NULL) {
3616 yyerror("unknown icmp-code %s", $3);
3622 $$ = calloc(1, sizeof(struct node_icmp));
3624 err(1, "icmp_item: calloc");
3626 $$->code = p->code + 1;
3627 $$->proto = IPPROTO_ICMP;
3631 | icmptype CODE NUMBER {
3632 if ($3 < 0 || $3 > 255) {
3633 yyerror("illegal icmp-code %lu", $3);
3636 $$ = calloc(1, sizeof(struct node_icmp));
3638 err(1, "icmp_item: calloc");
3641 $$->proto = IPPROTO_ICMP;
3647 icmp6_item : icmp6type {
3648 $$ = calloc(1, sizeof(struct node_icmp));
3650 err(1, "icmp_item: calloc");
3653 $$->proto = IPPROTO_ICMPV6;
3657 | icmp6type CODE STRING {
3658 const struct icmpcodeent *p;
3660 if ((p = geticmpcodebyname($1-1, $3, AF_INET6)) == NULL) {
3661 yyerror("unknown icmp6-code %s", $3);
3667 $$ = calloc(1, sizeof(struct node_icmp));
3669 err(1, "icmp_item: calloc");
3671 $$->code = p->code + 1;
3672 $$->proto = IPPROTO_ICMPV6;
3676 | icmp6type CODE NUMBER {
3677 if ($3 < 0 || $3 > 255) {
3678 yyerror("illegal icmp-code %lu", $3);
3681 $$ = calloc(1, sizeof(struct node_icmp));
3683 err(1, "icmp_item: calloc");
3686 $$->proto = IPPROTO_ICMPV6;
3693 const struct icmptypeent *p;
3695 if ((p = geticmptypebyname($1, AF_INET)) == NULL) {
3696 yyerror("unknown icmp-type %s", $1);
3704 if ($1 < 0 || $1 > 255) {
3705 yyerror("illegal icmp-type %lu", $1);
3712 icmp6type : STRING {
3713 const struct icmptypeent *p;
3715 if ((p = geticmptypebyname($1, AF_INET6)) ==
3717 yyerror("unknown icmp6-type %s", $1);
3725 if ($1 < 0 || $1 > 255) {
3726 yyerror("illegal icmp6-type %lu", $1);
3737 if (map_tos($1, &val))
3739 else if ($1[0] == '0' && $1[1] == 'x') {
3741 $$ = strtoul($1, &end, 16);
3742 if (errno || *end != '\0')
3745 $$ = 256; /* flag bad argument */
3746 if ($$ < 0 || $$ > 255) {
3747 yyerror("illegal tos value %s", $1);
3755 if ($$ < 0 || $$ > 255) {
3756 yyerror("illegal tos value %s", $1);
3762 sourcetrack : SOURCETRACK { $$ = PF_SRCTRACK; }
3763 | SOURCETRACK GLOBAL { $$ = PF_SRCTRACK_GLOBAL; }
3764 | SOURCETRACK RULE { $$ = PF_SRCTRACK_RULE; }
3767 statelock : IFBOUND {
3768 $$ = PFRULE_IFBOUND;
3779 | KEEP STATE state_opt_spec {
3780 $$.action = PF_STATE_NORMAL;
3783 | MODULATE STATE state_opt_spec {
3784 $$.action = PF_STATE_MODULATE;
3787 | SYNPROXY STATE state_opt_spec {
3788 $$.action = PF_STATE_SYNPROXY;
3793 flush : /* empty */ { $$ = 0; }
3794 | FLUSH { $$ = PF_FLUSH; }
3796 $$ = PF_FLUSH | PF_FLUSH_GLOBAL;
3800 state_opt_spec : '(' state_opt_list ')' { $$ = $2; }
3801 | /* empty */ { $$ = NULL; }
3804 state_opt_list : state_opt_item { $$ = $1; }
3805 | state_opt_list comma state_opt_item {
3806 $1->tail->next = $3;
3812 state_opt_item : MAXIMUM NUMBER {
3813 if ($2 < 0 || $2 > UINT_MAX) {
3814 yyerror("only positive values permitted");
3817 $$ = calloc(1, sizeof(struct node_state_opt));
3819 err(1, "state_opt_item: calloc");
3820 $$->type = PF_STATE_OPT_MAX;
3821 $$->data.max_states = $2;
3826 $$ = calloc(1, sizeof(struct node_state_opt));
3828 err(1, "state_opt_item: calloc");
3829 $$->type = PF_STATE_OPT_NOSYNC;
3833 | MAXSRCSTATES NUMBER {
3834 if ($2 < 0 || $2 > UINT_MAX) {
3835 yyerror("only positive values permitted");
3838 $$ = calloc(1, sizeof(struct node_state_opt));
3840 err(1, "state_opt_item: calloc");
3841 $$->type = PF_STATE_OPT_MAX_SRC_STATES;
3842 $$->data.max_src_states = $2;
3846 | MAXSRCCONN NUMBER {
3847 if ($2 < 0 || $2 > UINT_MAX) {
3848 yyerror("only positive values permitted");
3851 $$ = calloc(1, sizeof(struct node_state_opt));
3853 err(1, "state_opt_item: calloc");
3854 $$->type = PF_STATE_OPT_MAX_SRC_CONN;
3855 $$->data.max_src_conn = $2;
3859 | MAXSRCCONNRATE NUMBER '/' NUMBER {
3860 if ($2 < 0 || $2 > UINT_MAX ||
3861 $4 < 0 || $4 > UINT_MAX) {
3862 yyerror("only positive values permitted");
3865 $$ = calloc(1, sizeof(struct node_state_opt));
3867 err(1, "state_opt_item: calloc");
3868 $$->type = PF_STATE_OPT_MAX_SRC_CONN_RATE;
3869 $$->data.max_src_conn_rate.limit = $2;
3870 $$->data.max_src_conn_rate.seconds = $4;
3874 | OVERLOAD '<' STRING '>' flush {
3875 if (strlen($3) >= PF_TABLE_NAME_SIZE) {
3876 yyerror("table name '%s' too long", $3);
3880 $$ = calloc(1, sizeof(struct node_state_opt));
3882 err(1, "state_opt_item: calloc");
3883 if (strlcpy($$->data.overload.tblname, $3,
3884 PF_TABLE_NAME_SIZE) >= PF_TABLE_NAME_SIZE)
3885 errx(1, "state_opt_item: strlcpy");
3887 $$->type = PF_STATE_OPT_OVERLOAD;
3888 $$->data.overload.flush = $5;
3892 | MAXSRCNODES NUMBER {
3893 if ($2 < 0 || $2 > UINT_MAX) {
3894 yyerror("only positive values permitted");
3897 $$ = calloc(1, sizeof(struct node_state_opt));
3899 err(1, "state_opt_item: calloc");
3900 $$->type = PF_STATE_OPT_MAX_SRC_NODES;
3901 $$->data.max_src_nodes = $2;
3906 $$ = calloc(1, sizeof(struct node_state_opt));
3908 err(1, "state_opt_item: calloc");
3909 $$->type = PF_STATE_OPT_SRCTRACK;
3910 $$->data.src_track = $1;
3915 $$ = calloc(1, sizeof(struct node_state_opt));
3917 err(1, "state_opt_item: calloc");
3918 $$->type = PF_STATE_OPT_STATELOCK;
3919 $$->data.statelock = $1;
3924 $$ = calloc(1, sizeof(struct node_state_opt));
3926 err(1, "state_opt_item: calloc");
3927 $$->type = PF_STATE_OPT_SLOPPY;
3934 if ($2 < 0 || $2 > UINT_MAX) {
3935 yyerror("only positive values permitted");
3938 for (i = 0; pf_timeouts[i].name &&
3939 strcmp(pf_timeouts[i].name, $1); ++i)
3941 if (!pf_timeouts[i].name) {
3942 yyerror("illegal timeout name %s", $1);
3946 if (strchr(pf_timeouts[i].name, '.') == NULL) {
3947 yyerror("illegal state timeout %s", $1);
3952 $$ = calloc(1, sizeof(struct node_state_opt));
3954 err(1, "state_opt_item: calloc");
3955 $$->type = PF_STATE_OPT_TIMEOUT;
3956 $$->data.timeout.number = pf_timeouts[i].timeout;
3957 $$->data.timeout.seconds = $2;
3963 label : LABEL STRING {
3968 qname : QUEUE STRING {
3972 | QUEUE '(' STRING ')' {
3976 | QUEUE '(' STRING comma STRING ')' {
3982 no : /* empty */ { $$ = 0; }
3986 portstar : numberstring {
3987 if (parseport($1, &$$, PPORT_RANGE|PPORT_STAR) == -1) {
3995 redirspec : host { $$ = $1; }
3996 | '{' optnl redir_host_list '}' { $$ = $3; }
3999 redir_host_list : host optnl { $$ = $1; }
4000 | redir_host_list comma host optnl {
4001 $1->tail->next = $3;
4002 $1->tail = $3->tail;
4007 redirpool : /* empty */ { $$ = NULL; }
4009 $$ = calloc(1, sizeof(struct redirection));
4011 err(1, "redirection: calloc");
4013 $$->rport.a = $$->rport.b = $$->rport.t = 0;
4015 | ARROW redirspec PORT portstar {
4016 $$ = calloc(1, sizeof(struct redirection));
4018 err(1, "redirection: calloc");
4024 hashkey : /* empty */
4026 $$ = calloc(1, sizeof(struct pf_poolhashkey));
4028 err(1, "hashkey: calloc");
4029 $$->key32[0] = arc4random();
4030 $$->key32[1] = arc4random();
4031 $$->key32[2] = arc4random();
4032 $$->key32[3] = arc4random();
4036 if (!strncmp($1, "0x", 2)) {
4037 if (strlen($1) != 34) {
4039 yyerror("hex key must be 128 bits "
4040 "(32 hex digits) long");
4043 $$ = calloc(1, sizeof(struct pf_poolhashkey));
4045 err(1, "hashkey: calloc");
4047 if (sscanf($1, "0x%8x%8x%8x%8x",
4048 &$$->key32[0], &$$->key32[1],
4049 &$$->key32[2], &$$->key32[3]) != 4) {
4052 yyerror("invalid hex key");
4058 $$ = calloc(1, sizeof(struct pf_poolhashkey));
4060 err(1, "hashkey: calloc");
4062 MD5Update(&context, (unsigned char *)$1,
4064 MD5Final((unsigned char *)$$, &context);
4065 HTONL($$->key32[0]);
4066 HTONL($$->key32[1]);
4067 HTONL($$->key32[2]);
4068 HTONL($$->key32[3]);
4074 pool_opts : { bzero(&pool_opts, sizeof pool_opts); }
4078 bzero(&pool_opts, sizeof pool_opts);
4083 pool_opts_l : pool_opts_l pool_opt
4087 pool_opt : BITMASK {
4088 if (pool_opts.type) {
4089 yyerror("pool type cannot be redefined");
4092 pool_opts.type = PF_POOL_BITMASK;
4095 if (pool_opts.type) {
4096 yyerror("pool type cannot be redefined");
4099 pool_opts.type = PF_POOL_RANDOM;
4101 | SOURCEHASH hashkey {
4102 if (pool_opts.type) {
4103 yyerror("pool type cannot be redefined");
4106 pool_opts.type = PF_POOL_SRCHASH;
4110 if (pool_opts.type) {
4111 yyerror("pool type cannot be redefined");
4114 pool_opts.type = PF_POOL_ROUNDROBIN;
4117 if (pool_opts.staticport) {
4118 yyerror("static-port cannot be redefined");
4121 pool_opts.staticport = 1;
4124 if (filter_opts.marker & POM_STICKYADDRESS) {
4125 yyerror("sticky-address cannot be redefined");
4128 pool_opts.marker |= POM_STICKYADDRESS;
4129 pool_opts.opts |= PF_POOL_STICKYADDR;
4131 | MAPEPORTSET number '/' number '/' number {
4132 if (pool_opts.mape.offset) {
4133 yyerror("map-e-portset cannot be redefined");
4136 if (pool_opts.type) {
4137 yyerror("map-e-portset cannot be used with "
4141 if ($2 <= 0 || $2 >= 16) {
4142 yyerror("MAP-E PSID offset must be 1-15");
4145 if ($4 < 0 || $4 >= 16 || $2 + $4 > 16) {
4146 yyerror("Invalid MAP-E PSID length");
4148 } else if ($4 == 0) {
4149 yyerror("PSID Length = 0: this means"
4150 " you do not need MAP-E");
4153 if ($6 < 0 || $6 > 65535) {
4154 yyerror("Invalid MAP-E PSID");
4157 pool_opts.mape.offset = $2;
4158 pool_opts.mape.psidlen = $4;
4159 pool_opts.mape.psid = $6;
4163 redirection : /* empty */ { $$ = NULL; }
4165 $$ = calloc(1, sizeof(struct redirection));
4167 err(1, "redirection: calloc");
4169 $$->rport.a = $$->rport.b = $$->rport.t = 0;
4171 | ARROW host PORT portstar {
4172 $$ = calloc(1, sizeof(struct redirection));
4174 err(1, "redirection: calloc");
4180 natpasslog : /* empty */ { $$.b1 = $$.b2 = 0; $$.w2 = 0; }
4181 | PASS { $$.b1 = 1; $$.b2 = 0; $$.w2 = 0; }
4182 | PASS log { $$.b1 = 1; $$.b2 = $2.log; $$.w2 = $2.logif; }
4183 | log { $$.b1 = 0; $$.b2 = $1.log; $$.w2 = $1.logif; }
4186 nataction : no NAT natpasslog {
4188 yyerror("\"pass\" not valid with \"no\"");
4199 | no RDR natpasslog {
4201 yyerror("\"pass\" not valid with \"no\"");
4214 natrule : nataction interface af proto fromto tag tagged rtable
4217 struct pfctl_rule r;
4219 if (check_rulestate(PFCTL_STATE_NAT))
4222 memset(&r, 0, sizeof(r));
4231 if ($5.src.host && $5.src.host->af &&
4232 !$5.src.host->ifindex)
4233 r.af = $5.src.host->af;
4234 else if ($5.dst.host && $5.dst.host->af &&
4235 !$5.dst.host->ifindex)
4236 r.af = $5.dst.host->af;
4240 if (strlcpy(r.tagname, $6, PF_TAG_NAME_SIZE) >=
4242 yyerror("tag too long, max %u chars",
4243 PF_TAG_NAME_SIZE - 1);
4248 if (strlcpy(r.match_tagname, $7.name,
4249 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4250 yyerror("tag too long, max %u chars",
4251 PF_TAG_NAME_SIZE - 1);
4254 r.match_tag_not = $7.neg;
4257 if (r.action == PF_NONAT || r.action == PF_NORDR) {
4259 yyerror("translation rule with 'no' "
4260 "does not need '->'");
4264 if ($9 == NULL || $9->host == NULL) {
4265 yyerror("translation rule requires '-> "
4269 if (!r.af && ! $9->host->ifindex)
4270 r.af = $9->host->af;
4272 remove_invalid_hosts(&$9->host, &r.af);
4273 if (invalid_redirect($9->host, r.af))
4275 if (check_netmask($9->host, r.af))
4278 r.rpool.proxy_port[0] = ntohs($9->rport.a);
4282 if (!$9->rport.b && $9->rport.t &&
4283 $5.dst.port != NULL) {
4284 r.rpool.proxy_port[1] =
4285 ntohs($9->rport.a) +
4287 $5.dst.port->port[1]) -
4289 $5.dst.port->port[0]));
4291 r.rpool.proxy_port[1] =
4295 r.rpool.proxy_port[1] =
4297 if (!r.rpool.proxy_port[0] &&
4298 !r.rpool.proxy_port[1]) {
4299 r.rpool.proxy_port[0] =
4300 PF_NAT_PROXY_PORT_LOW;
4301 r.rpool.proxy_port[1] =
4302 PF_NAT_PROXY_PORT_HIGH;
4303 } else if (!r.rpool.proxy_port[1])
4304 r.rpool.proxy_port[1] =
4305 r.rpool.proxy_port[0];
4311 r.rpool.opts = $10.type;
4312 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
4313 PF_POOL_NONE && ($9->host->next != NULL ||
4314 $9->host->addr.type == PF_ADDR_TABLE ||
4315 DYNIF_MULTIADDR($9->host->addr)))
4316 r.rpool.opts = PF_POOL_ROUNDROBIN;
4317 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4318 PF_POOL_ROUNDROBIN &&
4319 disallow_table($9->host, "tables are only "
4320 "supported in round-robin redirection "
4323 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4324 PF_POOL_ROUNDROBIN &&
4325 disallow_alias($9->host, "interface (%s) "
4326 "is only supported in round-robin "
4327 "redirection pools"))
4329 if ($9->host->next != NULL) {
4330 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4331 PF_POOL_ROUNDROBIN) {
4332 yyerror("only round-robin "
4333 "valid for multiple "
4334 "redirection addresses");
4340 if ($10.key != NULL)
4341 memcpy(&r.rpool.key, $10.key,
4342 sizeof(struct pf_poolhashkey));
4345 r.rpool.opts |= $10.opts;
4347 if ($10.staticport) {
4348 if (r.action != PF_NAT) {
4349 yyerror("the 'static-port' option is "
4350 "only valid with nat rules");
4353 if (r.rpool.proxy_port[0] !=
4354 PF_NAT_PROXY_PORT_LOW &&
4355 r.rpool.proxy_port[1] !=
4356 PF_NAT_PROXY_PORT_HIGH) {
4357 yyerror("the 'static-port' option can't"
4358 " be used when specifying a port"
4362 r.rpool.proxy_port[0] = 0;
4363 r.rpool.proxy_port[1] = 0;
4366 if ($10.mape.offset) {
4367 if (r.action != PF_NAT) {
4368 yyerror("the 'map-e-portset' option is"
4369 " only valid with nat rules");
4372 if ($10.staticport) {
4373 yyerror("the 'map-e-portset' option"
4374 " can't be used 'static-port'");
4377 if (r.rpool.proxy_port[0] !=
4378 PF_NAT_PROXY_PORT_LOW &&
4379 r.rpool.proxy_port[1] !=
4380 PF_NAT_PROXY_PORT_HIGH) {
4381 yyerror("the 'map-e-portset' option"
4382 " can't be used when specifying"
4386 r.rpool.mape = $10.mape;
4389 expand_rule(&r, $2, $9 == NULL ? NULL : $9->host, $4,
4390 $5.src_os, $5.src.host, $5.src.port, $5.dst.host,
4391 $5.dst.port, 0, 0, 0, "");
4396 binatrule : no BINAT natpasslog interface af proto FROM ipspec toipspec tag
4397 tagged rtable redirection
4399 struct pfctl_rule binat;
4400 struct pf_pooladdr *pa;
4402 if (check_rulestate(PFCTL_STATE_NAT))
4404 if (disallow_urpf_failed($9, "\"urpf-failed\" is not "
4405 "permitted as a binat destination"))
4408 memset(&binat, 0, sizeof(binat));
4411 yyerror("\"pass\" not valid with \"no\"");
4415 binat.action = PF_NOBINAT;
4417 binat.action = PF_BINAT;
4418 binat.natpass = $3.b1;
4420 binat.logif = $3.w2;
4422 if (!binat.af && $8 != NULL && $8->af)
4424 if (!binat.af && $9 != NULL && $9->af)
4427 if (!binat.af && $13 != NULL && $13->host)
4428 binat.af = $13->host->af;
4430 yyerror("address family (inet/inet6) "
4436 memcpy(binat.ifname, $4->ifname,
4437 sizeof(binat.ifname));
4438 binat.ifnot = $4->not;
4443 if (strlcpy(binat.tagname, $10,
4444 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4445 yyerror("tag too long, max %u chars",
4446 PF_TAG_NAME_SIZE - 1);
4450 if (strlcpy(binat.match_tagname, $11.name,
4451 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4452 yyerror("tag too long, max %u chars",
4453 PF_TAG_NAME_SIZE - 1);
4456 binat.match_tag_not = $11.neg;
4457 binat.rtableid = $12;
4460 binat.proto = $6->proto;
4464 if ($8 != NULL && disallow_table($8, "invalid use of "
4465 "table <%s> as the source address of a binat rule"))
4467 if ($8 != NULL && disallow_alias($8, "invalid use of "
4468 "interface (%s) as the source address of a binat "
4471 if ($13 != NULL && $13->host != NULL && disallow_table(
4472 $13->host, "invalid use of table <%s> as the "
4473 "redirect address of a binat rule"))
4475 if ($13 != NULL && $13->host != NULL && disallow_alias(
4476 $13->host, "invalid use of interface (%s) as the "
4477 "redirect address of a binat rule"))
4482 yyerror("multiple binat ip addresses");
4485 if ($8->addr.type == PF_ADDR_DYNIFTL)
4487 if ($8->af != binat.af) {
4488 yyerror("binat ip versions must match");
4491 if (check_netmask($8, binat.af))
4493 memcpy(&binat.src.addr, &$8->addr,
4494 sizeof(binat.src.addr));
4499 yyerror("multiple binat ip addresses");
4502 if ($9->af != binat.af && $9->af) {
4503 yyerror("binat ip versions must match");
4506 if (check_netmask($9, binat.af))
4508 memcpy(&binat.dst.addr, &$9->addr,
4509 sizeof(binat.dst.addr));
4510 binat.dst.neg = $9->not;
4514 if (binat.action == PF_NOBINAT) {
4516 yyerror("'no binat' rule does not need"
4521 if ($13 == NULL || $13->host == NULL) {
4522 yyerror("'binat' rule requires"
4527 remove_invalid_hosts(&$13->host, &binat.af);
4528 if (invalid_redirect($13->host, binat.af))
4530 if ($13->host->next != NULL) {
4531 yyerror("binat rule must redirect to "
4532 "a single address");
4535 if (check_netmask($13->host, binat.af))
4538 if (!PF_AZERO(&binat.src.addr.v.a.mask,
4540 !PF_AEQ(&binat.src.addr.v.a.mask,
4541 &$13->host->addr.v.a.mask, binat.af)) {
4542 yyerror("'binat' source mask and "
4543 "redirect mask must be the same");
4547 TAILQ_INIT(&binat.rpool.list);
4548 pa = calloc(1, sizeof(struct pf_pooladdr));
4550 err(1, "binat: calloc");
4551 pa->addr = $13->host->addr;
4553 TAILQ_INSERT_TAIL(&binat.rpool.list,
4559 pfctl_append_rule(pf, &binat, "");
4563 tag : /* empty */ { $$ = NULL; }
4564 | TAG STRING { $$ = $2; }
4567 tagged : /* empty */ { $$.neg = 0; $$.name = NULL; }
4568 | not TAGGED string { $$.neg = $1; $$.name = $3; }
4571 rtable : /* empty */ { $$ = -1; }
4573 if ($2 < 0 || $2 > rt_tableid_max()) {
4574 yyerror("invalid rtable id");
4581 route_host : STRING {
4582 $$ = calloc(1, sizeof(struct node_host));
4584 err(1, "route_host: calloc");
4585 $$->ifname = strdup($1);
4586 set_ipmask($$, 128);
4590 | '(' STRING host ')' {
4591 struct node_host *n;
4594 for (n = $3; n != NULL; n = n->next)
4595 n->ifname = strdup($2);
4599 route_host_list : route_host optnl { $$ = $1; }
4600 | route_host_list comma route_host optnl {
4603 if ($1->af != $3->af) {
4604 yyerror("all pool addresses must be in the "
4605 "same address family");
4608 $1->tail->next = $3;
4609 $1->tail = $3->tail;
4614 routespec : route_host { $$ = $1; }
4615 | '{' optnl route_host_list '}' { $$ = $3; }
4618 route : /* empty */ {
4624 /* backwards-compat */
4629 | ROUTETO routespec pool_opts {
4632 $$.pool_opts = $3.type | $3.opts;
4636 | REPLYTO routespec pool_opts {
4639 $$.pool_opts = $3.type | $3.opts;
4643 | DUPTO routespec pool_opts {
4646 $$.pool_opts = $3.type | $3.opts;
4652 timeout_spec : STRING NUMBER
4654 if (check_rulestate(PFCTL_STATE_OPTION)) {
4658 if ($2 < 0 || $2 > UINT_MAX) {
4659 yyerror("only positive values permitted");
4662 if (pfctl_set_timeout(pf, $1, $2, 0) != 0) {
4663 yyerror("unknown timeout %s", $1);
4670 if (check_rulestate(PFCTL_STATE_OPTION))
4672 if ($2 < 0 || $2 > UINT_MAX) {
4673 yyerror("only positive values permitted");
4676 if (pfctl_set_timeout(pf, "interval", $2, 0) != 0)
4681 timeout_list : timeout_list comma timeout_spec optnl
4682 | timeout_spec optnl
4685 limit_spec : STRING NUMBER
4687 if (check_rulestate(PFCTL_STATE_OPTION)) {
4691 if ($2 < 0 || $2 > UINT_MAX) {
4692 yyerror("only positive values permitted");
4695 if (pfctl_set_limit(pf, $1, $2) != 0) {
4696 yyerror("unable to set limit %s %u", $1, $2);
4704 limit_list : limit_list comma limit_spec optnl
4712 yesno : NO { $$ = 0; }
4714 if (!strcmp($1, "yes"))
4717 yyerror("invalid value '%s', expected 'yes' "
4726 unaryop : '=' { $$ = PF_OP_EQ; }
4727 | '!' '=' { $$ = PF_OP_NE; }
4728 | '<' '=' { $$ = PF_OP_LE; }
4729 | '<' { $$ = PF_OP_LT; }
4730 | '>' '=' { $$ = PF_OP_GE; }
4731 | '>' { $$ = PF_OP_GT; }
4737 yyerror(const char *fmt, ...)
4743 fprintf(stderr, "%s:%d: ", file->name, yylval.lineno);
4744 vfprintf(stderr, fmt, ap);
4745 fprintf(stderr, "\n");
4751 disallow_table(struct node_host *h, const char *fmt)
4753 for (; h != NULL; h = h->next)
4754 if (h->addr.type == PF_ADDR_TABLE) {
4755 yyerror(fmt, h->addr.v.tblname);
4762 disallow_urpf_failed(struct node_host *h, const char *fmt)
4764 for (; h != NULL; h = h->next)
4765 if (h->addr.type == PF_ADDR_URPFFAILED) {
4773 disallow_alias(struct node_host *h, const char *fmt)
4775 for (; h != NULL; h = h->next)
4776 if (DYNIF_MULTIADDR(h->addr)) {
4777 yyerror(fmt, h->addr.v.tblname);
4784 rule_consistent(struct pfctl_rule *r, int anchor_call)
4788 switch (r->action) {
4793 problems = filter_consistent(r, anchor_call);
4797 problems = nat_consistent(r);
4801 problems = rdr_consistent(r);
4812 filter_consistent(struct pfctl_rule *r, int anchor_call)
4816 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP &&
4817 (r->src.port_op || r->dst.port_op)) {
4818 yyerror("port only applies to tcp/udp");
4821 if (r->proto != IPPROTO_ICMP && r->proto != IPPROTO_ICMPV6 &&
4822 (r->type || r->code)) {
4823 yyerror("icmp-type/code only applies to icmp");
4826 if (!r->af && (r->type || r->code)) {
4827 yyerror("must indicate address family with icmp-type/code");
4830 if (r->overload_tblname[0] &&
4831 r->max_src_conn == 0 && r->max_src_conn_rate.seconds == 0) {
4832 yyerror("'overload' requires 'max-src-conn' "
4833 "or 'max-src-conn-rate'");
4836 if ((r->proto == IPPROTO_ICMP && r->af == AF_INET6) ||
4837 (r->proto == IPPROTO_ICMPV6 && r->af == AF_INET)) {
4838 yyerror("proto %s doesn't match address family %s",
4839 r->proto == IPPROTO_ICMP ? "icmp" : "icmp6",
4840 r->af == AF_INET ? "inet" : "inet6");
4843 if (r->allow_opts && r->action != PF_PASS) {
4844 yyerror("allow-opts can only be specified for pass rules");
4847 if (r->rule_flag & PFRULE_FRAGMENT && (r->src.port_op ||
4848 r->dst.port_op || r->flagset || r->type || r->code)) {
4849 yyerror("fragments can be filtered only on IP header fields");
4852 if (r->rule_flag & PFRULE_RETURNRST && r->proto != IPPROTO_TCP) {
4853 yyerror("return-rst can only be applied to TCP rules");
4856 if (r->max_src_nodes && !(r->rule_flag & PFRULE_RULESRCTRACK)) {
4857 yyerror("max-src-nodes requires 'source-track rule'");
4860 if (r->action == PF_DROP && r->keep_state) {
4861 yyerror("keep state on block rules doesn't make sense");
4864 if (r->rule_flag & PFRULE_STATESLOPPY &&
4865 (r->keep_state == PF_STATE_MODULATE ||
4866 r->keep_state == PF_STATE_SYNPROXY)) {
4867 yyerror("sloppy state matching cannot be used with "
4868 "synproxy state or modulate state");
4875 nat_consistent(struct pfctl_rule *r)
4877 return (0); /* yeah! */
4881 rdr_consistent(struct pfctl_rule *r)
4885 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP) {
4886 if (r->src.port_op) {
4887 yyerror("src port only applies to tcp/udp");
4890 if (r->dst.port_op) {
4891 yyerror("dst port only applies to tcp/udp");
4894 if (r->rpool.proxy_port[0]) {
4895 yyerror("rpool port only applies to tcp/udp");
4899 if (r->dst.port_op &&
4900 r->dst.port_op != PF_OP_EQ && r->dst.port_op != PF_OP_RRG) {
4901 yyerror("invalid port operator for rdr destination port");
4908 process_tabledef(char *name, struct table_opts *opts)
4910 struct pfr_buffer ab;
4911 struct node_tinit *ti;
4912 unsigned long maxcount;
4913 size_t s = sizeof(maxcount);
4915 bzero(&ab, sizeof(ab));
4916 ab.pfrb_type = PFRB_ADDRS;
4917 SIMPLEQ_FOREACH(ti, &opts->init_nodes, entries) {
4919 if (pfr_buf_load(&ab, ti->file, 0, append_addr)) {
4921 yyerror("cannot load \"%s\": %s",
4922 ti->file, strerror(errno));
4924 yyerror("file \"%s\" contains bad data",
4929 if (append_addr_host(&ab, ti->host, 0, 0)) {
4930 yyerror("cannot create address buffer: %s",
4935 if (pf->opts & PF_OPT_VERBOSE)
4936 print_tabledef(name, opts->flags, opts->init_addr,
4938 if (!(pf->opts & PF_OPT_NOACTION) &&
4939 pfctl_define_table(name, opts->flags, opts->init_addr,
4940 pf->anchor->name, &ab, pf->anchor->ruleset.tticket)) {
4942 if (sysctlbyname("net.pf.request_maxcount", &maxcount, &s,
4946 if (ab.pfrb_size > maxcount)
4947 yyerror("cannot define table %s: too many elements.\n"
4948 "Consider increasing net.pf.request_maxcount.",
4951 yyerror("cannot define table %s: %s", name,
4952 pfr_strerror(errno));
4969 /* macro gore, but you should've seen the prior indentation nightmare... */
4971 #define FREE_LIST(T,r) \
4974 while (node != NULL) { \
4976 node = node->next; \
4981 #define LOOP_THROUGH(T,n,r,C) \
4985 r = calloc(1, sizeof(T)); \
4987 err(1, "LOOP: calloc"); \
4991 while (n != NULL) { \
5000 expand_label_str(char *label, size_t len, const char *srch, const char *repl)
5005 if ((tmp = calloc(1, len)) == NULL)
5006 err(1, "expand_label_str: calloc");
5008 while ((q = strstr(p, srch)) != NULL) {
5010 if ((strlcat(tmp, p, len) >= len) ||
5011 (strlcat(tmp, repl, len) >= len))
5012 errx(1, "expand_label: label too long");
5016 if (strlcat(tmp, p, len) >= len)
5017 errx(1, "expand_label: label too long");
5018 strlcpy(label, tmp, len); /* always fits */
5023 expand_label_if(const char *name, char *label, size_t len, const char *ifname)
5025 if (strstr(label, name) != NULL) {
5027 expand_label_str(label, len, name, "any");
5029 expand_label_str(label, len, name, ifname);
5034 expand_label_addr(const char *name, char *label, size_t len, sa_family_t af,
5035 struct pf_rule_addr *addr)
5037 char tmp[64], tmp_not[66];
5039 if (strstr(label, name) != NULL) {
5040 switch (addr->addr.type) {
5041 case PF_ADDR_DYNIFTL:
5042 snprintf(tmp, sizeof(tmp), "(%s)", addr->addr.v.ifname);
5045 snprintf(tmp, sizeof(tmp), "<%s>", addr->addr.v.tblname);
5047 case PF_ADDR_NOROUTE:
5048 snprintf(tmp, sizeof(tmp), "no-route");
5050 case PF_ADDR_URPFFAILED:
5051 snprintf(tmp, sizeof(tmp), "urpf-failed");
5053 case PF_ADDR_ADDRMASK:
5054 if (!af || (PF_AZERO(&addr->addr.v.a.addr, af) &&
5055 PF_AZERO(&addr->addr.v.a.mask, af)))
5056 snprintf(tmp, sizeof(tmp), "any");
5061 if (inet_ntop(af, &addr->addr.v.a.addr, a,
5063 snprintf(tmp, sizeof(tmp), "?");
5065 bits = unmask(&addr->addr.v.a.mask, af);
5066 if ((af == AF_INET && bits < 32) ||
5067 (af == AF_INET6 && bits < 128))
5068 snprintf(tmp, sizeof(tmp),
5071 snprintf(tmp, sizeof(tmp),
5077 snprintf(tmp, sizeof(tmp), "?");
5082 snprintf(tmp_not, sizeof(tmp_not), "! %s", tmp);
5083 expand_label_str(label, len, name, tmp_not);
5085 expand_label_str(label, len, name, tmp);
5090 expand_label_port(const char *name, char *label, size_t len,
5091 struct pf_rule_addr *addr)
5093 char a1[6], a2[6], op[13] = "";
5095 if (strstr(label, name) != NULL) {
5096 snprintf(a1, sizeof(a1), "%u", ntohs(addr->port[0]));
5097 snprintf(a2, sizeof(a2), "%u", ntohs(addr->port[1]));
5100 else if (addr->port_op == PF_OP_IRG)
5101 snprintf(op, sizeof(op), "%s><%s", a1, a2);
5102 else if (addr->port_op == PF_OP_XRG)
5103 snprintf(op, sizeof(op), "%s<>%s", a1, a2);
5104 else if (addr->port_op == PF_OP_EQ)
5105 snprintf(op, sizeof(op), "%s", a1);
5106 else if (addr->port_op == PF_OP_NE)
5107 snprintf(op, sizeof(op), "!=%s", a1);
5108 else if (addr->port_op == PF_OP_LT)
5109 snprintf(op, sizeof(op), "<%s", a1);
5110 else if (addr->port_op == PF_OP_LE)
5111 snprintf(op, sizeof(op), "<=%s", a1);
5112 else if (addr->port_op == PF_OP_GT)
5113 snprintf(op, sizeof(op), ">%s", a1);
5114 else if (addr->port_op == PF_OP_GE)
5115 snprintf(op, sizeof(op), ">=%s", a1);
5116 expand_label_str(label, len, name, op);
5121 expand_label_proto(const char *name, char *label, size_t len, u_int8_t proto)
5123 const char *protoname;
5126 if (strstr(label, name) != NULL) {
5127 protoname = pfctl_proto2name(proto);
5128 if (protoname != NULL)
5129 expand_label_str(label, len, name, protoname);
5131 snprintf(n, sizeof(n), "%u", proto);
5132 expand_label_str(label, len, name, n);
5138 expand_label_nr(const char *name, char *label, size_t len,
5139 struct pfctl_rule *r)
5143 if (strstr(label, name) != NULL) {
5144 snprintf(n, sizeof(n), "%u", r->nr);
5145 expand_label_str(label, len, name, n);
5150 expand_label(char *label, size_t len, struct pfctl_rule *r)
5152 expand_label_if("$if", label, len, r->ifname);
5153 expand_label_addr("$srcaddr", label, len, r->af, &r->src);
5154 expand_label_addr("$dstaddr", label, len, r->af, &r->dst);
5155 expand_label_port("$srcport", label, len, &r->src);
5156 expand_label_port("$dstport", label, len, &r->dst);
5157 expand_label_proto("$proto", label, len, r->proto);
5158 expand_label_nr("$nr", label, len, r);
5162 expand_altq(struct pf_altq *a, struct node_if *interfaces,
5163 struct node_queue *nqueues, struct node_queue_bw bwspec,
5164 struct node_queue_opt *opts)
5166 struct pf_altq pa, pb;
5167 char qname[PF_QNAME_SIZE];
5168 struct node_queue *n;
5169 struct node_queue_bw bw;
5172 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
5173 FREE_LIST(struct node_if, interfaces);
5175 FREE_LIST(struct node_queue, nqueues);
5179 LOOP_THROUGH(struct node_if, interface, interfaces,
5180 memcpy(&pa, a, sizeof(struct pf_altq));
5181 if (strlcpy(pa.ifname, interface->ifname,
5182 sizeof(pa.ifname)) >= sizeof(pa.ifname))
5183 errx(1, "expand_altq: strlcpy");
5185 if (interface->not) {
5186 yyerror("altq on ! <interface> is not supported");
5189 if (eval_pfaltq(pf, &pa, &bwspec, opts))
5192 if (pfctl_add_altq(pf, &pa))
5195 if (pf->opts & PF_OPT_VERBOSE) {
5196 print_altq(&pf->paltq->altq, 0,
5198 if (nqueues && nqueues->tail) {
5200 LOOP_THROUGH(struct node_queue, queue,
5210 if (pa.scheduler == ALTQT_CBQ ||
5211 pa.scheduler == ALTQT_HFSC ||
5212 pa.scheduler == ALTQT_FAIRQ) {
5213 /* now create a root queue */
5214 memset(&pb, 0, sizeof(struct pf_altq));
5215 if (strlcpy(qname, "root_", sizeof(qname)) >=
5217 errx(1, "expand_altq: strlcpy");
5218 if (strlcat(qname, interface->ifname,
5219 sizeof(qname)) >= sizeof(qname))
5220 errx(1, "expand_altq: strlcat");
5221 if (strlcpy(pb.qname, qname,
5222 sizeof(pb.qname)) >= sizeof(pb.qname))
5223 errx(1, "expand_altq: strlcpy");
5224 if (strlcpy(pb.ifname, interface->ifname,
5225 sizeof(pb.ifname)) >= sizeof(pb.ifname))
5226 errx(1, "expand_altq: strlcpy");
5227 pb.qlimit = pa.qlimit;
5228 pb.scheduler = pa.scheduler;
5229 bw.bw_absolute = pa.ifbandwidth;
5231 if (eval_pfqueue(pf, &pb, &bw, opts))
5234 if (pfctl_add_altq(pf, &pb))
5238 LOOP_THROUGH(struct node_queue, queue, nqueues,
5239 n = calloc(1, sizeof(struct node_queue));
5241 err(1, "expand_altq: calloc");
5242 if (pa.scheduler == ALTQT_CBQ ||
5243 pa.scheduler == ALTQT_HFSC ||
5244 pa.scheduler == ALTQT_FAIRQ)
5245 if (strlcpy(n->parent, qname,
5246 sizeof(n->parent)) >=
5248 errx(1, "expand_altq: strlcpy");
5249 if (strlcpy(n->queue, queue->queue,
5250 sizeof(n->queue)) >= sizeof(n->queue))
5251 errx(1, "expand_altq: strlcpy");
5252 if (strlcpy(n->ifname, interface->ifname,
5253 sizeof(n->ifname)) >= sizeof(n->ifname))
5254 errx(1, "expand_altq: strlcpy");
5255 n->scheduler = pa.scheduler;
5261 queues->tail->next = n;
5267 FREE_LIST(struct node_if, interfaces);
5269 FREE_LIST(struct node_queue, nqueues);
5275 expand_queue(struct pf_altq *a, struct node_if *interfaces,
5276 struct node_queue *nqueues, struct node_queue_bw bwspec,
5277 struct node_queue_opt *opts)
5279 struct node_queue *n, *nq;
5284 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
5285 FREE_LIST(struct node_queue, nqueues);
5289 if (queues == NULL) {
5290 yyerror("queue %s has no parent", a->qname);
5291 FREE_LIST(struct node_queue, nqueues);
5295 LOOP_THROUGH(struct node_if, interface, interfaces,
5296 LOOP_THROUGH(struct node_queue, tqueue, queues,
5297 if (!strncmp(a->qname, tqueue->queue, PF_QNAME_SIZE) &&
5298 (interface->ifname[0] == 0 ||
5299 (!interface->not && !strncmp(interface->ifname,
5300 tqueue->ifname, IFNAMSIZ)) ||
5301 (interface->not && strncmp(interface->ifname,
5302 tqueue->ifname, IFNAMSIZ)))) {
5303 /* found ourself in queues */
5306 memcpy(&pa, a, sizeof(struct pf_altq));
5308 if (pa.scheduler != ALTQT_NONE &&
5309 pa.scheduler != tqueue->scheduler) {
5310 yyerror("exactly one scheduler type "
5311 "per interface allowed");
5314 pa.scheduler = tqueue->scheduler;
5316 /* scheduler dependent error checking */
5317 switch (pa.scheduler) {
5319 if (nqueues != NULL) {
5320 yyerror("priq queues cannot "
5321 "have child queues");
5324 if (bwspec.bw_absolute > 0 ||
5325 bwspec.bw_percent < 100) {
5326 yyerror("priq doesn't take "
5335 if (strlcpy(pa.ifname, tqueue->ifname,
5336 sizeof(pa.ifname)) >= sizeof(pa.ifname))
5337 errx(1, "expand_queue: strlcpy");
5338 if (strlcpy(pa.parent, tqueue->parent,
5339 sizeof(pa.parent)) >= sizeof(pa.parent))
5340 errx(1, "expand_queue: strlcpy");
5342 if (eval_pfqueue(pf, &pa, &bwspec, opts))
5345 if (pfctl_add_altq(pf, &pa))
5348 for (nq = nqueues; nq != NULL; nq = nq->next) {
5349 if (!strcmp(a->qname, nq->queue)) {
5350 yyerror("queue cannot have "
5356 sizeof(struct node_queue));
5358 err(1, "expand_queue: calloc");
5359 if (strlcpy(n->parent, a->qname,
5360 sizeof(n->parent)) >=
5362 errx(1, "expand_queue strlcpy");
5363 if (strlcpy(n->queue, nq->queue,
5364 sizeof(n->queue)) >=
5366 errx(1, "expand_queue strlcpy");
5367 if (strlcpy(n->ifname, tqueue->ifname,
5368 sizeof(n->ifname)) >=
5370 errx(1, "expand_queue strlcpy");
5371 n->scheduler = tqueue->scheduler;
5377 queues->tail->next = n;
5381 if ((pf->opts & PF_OPT_VERBOSE) && (
5382 (found == 1 && interface->ifname[0] == 0) ||
5383 (found > 0 && interface->ifname[0] != 0))) {
5384 print_queue(&pf->paltq->altq, 0,
5385 &bwspec, interface->ifname[0] != 0,
5387 if (nqueues && nqueues->tail) {
5389 LOOP_THROUGH(struct node_queue,
5402 FREE_LIST(struct node_queue, nqueues);
5403 FREE_LIST(struct node_if, interfaces);
5406 yyerror("queue %s has no parent", a->qname);
5417 expand_rule(struct pfctl_rule *r,
5418 struct node_if *interfaces, struct node_host *rpool_hosts,
5419 struct node_proto *protos, struct node_os *src_oses,
5420 struct node_host *src_hosts, struct node_port *src_ports,
5421 struct node_host *dst_hosts, struct node_port *dst_ports,
5422 struct node_uid *uids, struct node_gid *gids, struct node_icmp *icmp_types,
5423 const char *anchor_call)
5425 sa_family_t af = r->af;
5426 int added = 0, error = 0;
5427 char ifname[IF_NAMESIZE];
5428 char label[PF_RULE_MAX_LABEL_COUNT][PF_RULE_LABEL_SIZE];
5429 char tagname[PF_TAG_NAME_SIZE];
5430 char match_tagname[PF_TAG_NAME_SIZE];
5431 struct pf_pooladdr *pa;
5432 struct node_host *h;
5433 u_int8_t flags, flagset, keep_state;
5435 memcpy(label, r->label, sizeof(r->label));
5436 assert(sizeof(r->label) == sizeof(label));
5437 if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname))
5438 errx(1, "expand_rule: strlcpy");
5439 if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >=
5440 sizeof(match_tagname))
5441 errx(1, "expand_rule: strlcpy");
5443 flagset = r->flagset;
5444 keep_state = r->keep_state;
5446 LOOP_THROUGH(struct node_if, interface, interfaces,
5447 LOOP_THROUGH(struct node_proto, proto, protos,
5448 LOOP_THROUGH(struct node_icmp, icmp_type, icmp_types,
5449 LOOP_THROUGH(struct node_host, src_host, src_hosts,
5450 LOOP_THROUGH(struct node_port, src_port, src_ports,
5451 LOOP_THROUGH(struct node_os, src_os, src_oses,
5452 LOOP_THROUGH(struct node_host, dst_host, dst_hosts,
5453 LOOP_THROUGH(struct node_port, dst_port, dst_ports,
5454 LOOP_THROUGH(struct node_uid, uid, uids,
5455 LOOP_THROUGH(struct node_gid, gid, gids,
5458 /* for link-local IPv6 address, interface must match up */
5459 if ((r->af && src_host->af && r->af != src_host->af) ||
5460 (r->af && dst_host->af && r->af != dst_host->af) ||
5461 (src_host->af && dst_host->af &&
5462 src_host->af != dst_host->af) ||
5463 (src_host->ifindex && dst_host->ifindex &&
5464 src_host->ifindex != dst_host->ifindex) ||
5465 (src_host->ifindex && *interface->ifname &&
5466 src_host->ifindex != if_nametoindex(interface->ifname)) ||
5467 (dst_host->ifindex && *interface->ifname &&
5468 dst_host->ifindex != if_nametoindex(interface->ifname)))
5470 if (!r->af && src_host->af)
5471 r->af = src_host->af;
5472 else if (!r->af && dst_host->af)
5473 r->af = dst_host->af;
5475 if (*interface->ifname)
5476 strlcpy(r->ifname, interface->ifname,
5478 else if (if_indextoname(src_host->ifindex, ifname))
5479 strlcpy(r->ifname, ifname, sizeof(r->ifname));
5480 else if (if_indextoname(dst_host->ifindex, ifname))
5481 strlcpy(r->ifname, ifname, sizeof(r->ifname));
5483 memset(r->ifname, '\0', sizeof(r->ifname));
5485 memcpy(r->label, label, sizeof(r->label));
5486 if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >=
5488 errx(1, "expand_rule: strlcpy");
5489 if (strlcpy(r->match_tagname, match_tagname,
5490 sizeof(r->match_tagname)) >= sizeof(r->match_tagname))
5491 errx(1, "expand_rule: strlcpy");
5493 error += check_netmask(src_host, r->af);
5494 error += check_netmask(dst_host, r->af);
5496 r->ifnot = interface->not;
5497 r->proto = proto->proto;
5498 r->src.addr = src_host->addr;
5499 r->src.neg = src_host->not;
5500 r->src.port[0] = src_port->port[0];
5501 r->src.port[1] = src_port->port[1];
5502 r->src.port_op = src_port->op;
5503 r->dst.addr = dst_host->addr;
5504 r->dst.neg = dst_host->not;
5505 r->dst.port[0] = dst_port->port[0];
5506 r->dst.port[1] = dst_port->port[1];
5507 r->dst.port_op = dst_port->op;
5508 r->uid.op = uid->op;
5509 r->uid.uid[0] = uid->uid[0];
5510 r->uid.uid[1] = uid->uid[1];
5511 r->gid.op = gid->op;
5512 r->gid.gid[0] = gid->gid[0];
5513 r->gid.gid[1] = gid->gid[1];
5514 r->type = icmp_type->type;
5515 r->code = icmp_type->code;
5517 if ((keep_state == PF_STATE_MODULATE ||
5518 keep_state == PF_STATE_SYNPROXY) &&
5519 r->proto && r->proto != IPPROTO_TCP)
5520 r->keep_state = PF_STATE_NORMAL;
5522 r->keep_state = keep_state;
5524 if (r->proto && r->proto != IPPROTO_TCP) {
5529 r->flagset = flagset;
5531 if (icmp_type->proto && r->proto != icmp_type->proto) {
5532 yyerror("icmp-type mismatch");
5536 if (src_os && src_os->os) {
5537 r->os_fingerprint = pfctl_get_fingerprint(src_os->os);
5538 if ((pf->opts & PF_OPT_VERBOSE2) &&
5539 r->os_fingerprint == PF_OSFP_NOMATCH)
5541 "warning: unknown '%s' OS fingerprint\n",
5544 r->os_fingerprint = PF_OSFP_ANY;
5547 TAILQ_INIT(&r->rpool.list);
5548 for (h = rpool_hosts; h != NULL; h = h->next) {
5549 pa = calloc(1, sizeof(struct pf_pooladdr));
5551 err(1, "expand_rule: calloc");
5553 if (h->ifname != NULL) {
5554 if (strlcpy(pa->ifname, h->ifname,
5555 sizeof(pa->ifname)) >=
5557 errx(1, "expand_rule: strlcpy");
5560 TAILQ_INSERT_TAIL(&r->rpool.list, pa, entries);
5563 if (rule_consistent(r, anchor_call[0]) < 0 || error)
5564 yyerror("skipping rule due to errors");
5566 r->nr = pf->astack[pf->asd]->match++;
5567 pfctl_append_rule(pf, r, anchor_call);
5573 FREE_LIST(struct node_if, interfaces);
5574 FREE_LIST(struct node_proto, protos);
5575 FREE_LIST(struct node_host, src_hosts);
5576 FREE_LIST(struct node_port, src_ports);
5577 FREE_LIST(struct node_os, src_oses);
5578 FREE_LIST(struct node_host, dst_hosts);
5579 FREE_LIST(struct node_port, dst_ports);
5580 FREE_LIST(struct node_uid, uids);
5581 FREE_LIST(struct node_gid, gids);
5582 FREE_LIST(struct node_icmp, icmp_types);
5583 FREE_LIST(struct node_host, rpool_hosts);
5586 yyerror("rule expands to no valid combination");
5590 expand_skip_interface(struct node_if *interfaces)
5594 if (!interfaces || (!interfaces->next && !interfaces->not &&
5595 !strcmp(interfaces->ifname, "none"))) {
5596 if (pf->opts & PF_OPT_VERBOSE)
5597 printf("set skip on none\n");
5598 errs = pfctl_set_interface_flags(pf, "", PFI_IFLAG_SKIP, 0);
5602 if (pf->opts & PF_OPT_VERBOSE)
5603 printf("set skip on {");
5604 LOOP_THROUGH(struct node_if, interface, interfaces,
5605 if (pf->opts & PF_OPT_VERBOSE)
5606 printf(" %s", interface->ifname);
5607 if (interface->not) {
5608 yyerror("skip on ! <interface> is not supported");
5611 errs += pfctl_set_interface_flags(pf,
5612 interface->ifname, PFI_IFLAG_SKIP, 1);
5614 if (pf->opts & PF_OPT_VERBOSE)
5617 FREE_LIST(struct node_if, interfaces);
5629 check_rulestate(int desired_state)
5631 if (require_order && (rulestate > desired_state)) {
5632 yyerror("Rules must be in order: options, normalization, "
5633 "queueing, translation, filtering");
5636 rulestate = desired_state;
5641 kw_cmp(const void *k, const void *e)
5643 return (strcmp(k, ((const struct keywords *)e)->k_name));
5649 /* this has to be sorted always */
5650 static const struct keywords keywords[] = {
5652 { "allow-opts", ALLOWOPTS},
5654 { "anchor", ANCHOR},
5655 { "antispoof", ANTISPOOF},
5657 { "bandwidth", BANDWIDTH},
5659 { "binat-anchor", BINATANCHOR},
5660 { "bitmask", BITMASK},
5662 { "block-policy", BLOCKPOLICY},
5663 { "buckets", BUCKETS},
5667 { "crop", FRAGCROP},
5669 { "divert-reply", DIVERTREPLY},
5670 { "divert-to", DIVERTTO},
5671 { "dnpipe", DNPIPE},
5672 { "dnqueue", DNQUEUE},
5674 { "drop-ovl", FRAGDROP},
5676 { "fail-policy", FAILPOLICY},
5678 { "fastroute", FASTROUTE},
5679 { "file", FILENAME},
5680 { "fingerprints", FINGERPRINTS},
5682 { "floating", FLOATING},
5685 { "fragment", FRAGMENT},
5687 { "global", GLOBAL},
5691 { "hostid", HOSTID},
5692 { "icmp-type", ICMPTYPE},
5693 { "icmp6-type", ICMP6TYPE},
5694 { "if-bound", IFBOUND},
5696 { "include", INCLUDE},
5699 { "interval", INTERVAL},
5701 { "keepcounters", KEEPCOUNTERS},
5704 { "linkshare", LINKSHARE},
5707 { "loginterface", LOGINTERFACE},
5708 { "map-e-portset", MAPEPORTSET},
5711 { "max-mss", MAXMSS},
5712 { "max-src-conn", MAXSRCCONN},
5713 { "max-src-conn-rate", MAXSRCCONNRATE},
5714 { "max-src-nodes", MAXSRCNODES},
5715 { "max-src-states", MAXSRCSTATES},
5716 { "min-ttl", MINTTL},
5717 { "modulate", MODULATE},
5719 { "nat-anchor", NATANCHOR},
5722 { "no-route", NOROUTE},
5723 { "no-sync", NOSYNC},
5725 { "optimization", OPTIMIZATION},
5728 { "overload", OVERLOAD},
5732 { "priority", PRIORITY},
5734 { "probability", PROBABILITY},
5736 { "qlimit", QLIMIT},
5739 { "random", RANDOM},
5740 { "random-id", RANDOMID},
5742 { "rdr-anchor", RDRANCHOR},
5743 { "realtime", REALTIME},
5744 { "reassemble", REASSEMBLE},
5745 { "reply-to", REPLYTO},
5746 { "require-order", REQUIREORDER},
5747 { "return", RETURN},
5748 { "return-icmp", RETURNICMP},
5749 { "return-icmp6", RETURNICMP6},
5750 { "return-rst", RETURNRST},
5751 { "ridentifier", RIDENTIFIER},
5752 { "round-robin", ROUNDROBIN},
5754 { "route-to", ROUTETO},
5755 { "rtable", RTABLE},
5757 { "ruleset-optimization", RULESET_OPTIMIZATION},
5760 { "set-tos", SETTOS},
5762 { "sloppy", SLOPPY},
5763 { "source-hash", SOURCEHASH},
5764 { "source-track", SOURCETRACK},
5766 { "state-defaults", STATEDEFAULTS},
5767 { "state-policy", STATEPOLICY},
5768 { "static-port", STATICPORT},
5769 { "sticky-address", STICKYADDRESS},
5770 { "syncookies", SYNCOOKIES},
5771 { "synproxy", SYNPROXY},
5774 { "tagged", TAGGED},
5775 { "target", TARGET},
5776 { "tbrsize", TBRSIZE},
5777 { "timeout", TIMEOUT},
5781 { "upperlimit", UPPERLIMIT},
5782 { "urpf-failed", URPFFAILED},
5785 const struct keywords *p;
5787 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
5788 sizeof(keywords[0]), kw_cmp);
5792 fprintf(stderr, "%s: %d\n", s, p->k_val);
5796 fprintf(stderr, "string: %s\n", s);
5801 #define MAXPUSHBACK 128
5803 static char *parsebuf;
5804 static int parseindex;
5805 static char pushback_buffer[MAXPUSHBACK];
5806 static int pushback_index = 0;
5814 /* Read character from the parsebuffer instead of input. */
5815 if (parseindex >= 0) {
5816 c = parsebuf[parseindex++];
5825 return (pushback_buffer[--pushback_index]);
5828 if ((c = getc(file->stream)) == EOF) {
5829 yyerror("reached end of file while parsing quoted string");
5830 if (popfile() == EOF)
5837 while ((c = getc(file->stream)) == '\\') {
5838 next = getc(file->stream);
5843 yylval.lineno = file->lineno;
5848 if (popfile() == EOF)
5850 c = getc(file->stream);
5862 if (parseindex >= 0)
5865 if (pushback_index < MAXPUSHBACK-1)
5866 return (pushback_buffer[pushback_index++] = c);
5878 /* skip to either EOF or the first real EOL */
5881 c = pushback_buffer[--pushback_index];
5899 int quotec, next, c;
5904 while ((c = lgetc(0)) == ' ' || c == '\t')
5907 yylval.lineno = file->lineno;
5909 while ((c = lgetc(0)) != '\n' && c != EOF)
5911 if (c == '$' && parsebuf == NULL) {
5913 if ((c = lgetc(0)) == EOF)
5916 if (p + 1 >= buf + sizeof(buf) - 1) {
5917 yyerror("string too long");
5920 if (isalnum(c) || c == '_') {
5930 yyerror("macro '%s' not defined", buf);
5943 if ((c = lgetc(quotec)) == EOF)
5948 } else if (c == '\\') {
5949 if ((next = lgetc(quotec)) == EOF)
5951 if (next == quotec || c == ' ' || c == '\t')
5953 else if (next == '\n') {
5959 } else if (c == quotec) {
5963 if (p + 1 >= buf + sizeof(buf) - 1) {
5964 yyerror("string too long");
5969 yylval.v.string = strdup(buf);
5970 if (yylval.v.string == NULL)
5971 err(1, "yylex: strdup");
5976 yylval.v.i = PF_OP_XRG;
5977 return (PORTBINARY);
5984 yylval.v.i = PF_OP_IRG;
5985 return (PORTBINARY);
5997 #define allowed_to_end_number(x) \
5998 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
6000 if (c == '-' || isdigit(c)) {
6003 if ((unsigned)(p-buf) >= sizeof(buf)) {
6004 yyerror("string too long");
6007 } while ((c = lgetc(0)) != EOF && isdigit(c));
6009 if (p == buf + 1 && buf[0] == '-')
6011 if (c == EOF || allowed_to_end_number(c)) {
6012 const char *errstr = NULL;
6015 yylval.v.number = strtonum(buf, LLONG_MIN,
6016 LLONG_MAX, &errstr);
6018 yyerror("\"%s\" invalid number: %s",
6033 #define allowed_in_string(x) \
6034 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
6035 x != '{' && x != '}' && x != '<' && x != '>' && \
6036 x != '!' && x != '=' && x != '/' && x != '#' && \
6039 if (isalnum(c) || c == ':' || c == '_') {
6042 if ((unsigned)(p-buf) >= sizeof(buf)) {
6043 yyerror("string too long");
6046 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
6049 if ((token = lookup(buf)) == STRING)
6050 if ((yylval.v.string = strdup(buf)) == NULL)
6051 err(1, "yylex: strdup");
6055 yylval.lineno = file->lineno;
6064 check_file_secrecy(int fd, const char *fname)
6068 if (fstat(fd, &st)) {
6069 warn("cannot stat %s", fname);
6072 if (st.st_uid != 0 && st.st_uid != getuid()) {
6073 warnx("%s: owner not root or current user", fname);
6076 if (st.st_mode & (S_IRWXG | S_IRWXO)) {
6077 warnx("%s: group/world readable/writeable", fname);
6084 pushfile(const char *name, int secret)
6088 if ((nfile = calloc(1, sizeof(struct file))) == NULL ||
6089 (nfile->name = strdup(name)) == NULL) {
6093 if (TAILQ_FIRST(&files) == NULL && strcmp(nfile->name, "-") == 0) {
6094 nfile->stream = stdin;
6096 if ((nfile->name = strdup("stdin")) == NULL) {
6101 } else if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
6102 warn("%s", nfile->name);
6106 } else if (secret &&
6107 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
6108 fclose(nfile->stream);
6114 TAILQ_INSERT_TAIL(&files, nfile, entry);
6123 if ((prev = TAILQ_PREV(file, files, entry)) != NULL) {
6124 prev->errors += file->errors;
6125 TAILQ_REMOVE(&files, file, entry);
6126 fclose(file->stream);
6136 parse_config(char *filename, struct pfctl *xpf)
6143 rulestate = PFCTL_STATE_NONE;
6144 returnicmpdefault = (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
6145 returnicmp6default =
6146 (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
6147 blockpolicy = PFRULE_DROP;
6148 failpolicy = PFRULE_DROP;
6151 if ((file = pushfile(filename, 0)) == NULL) {
6152 warn("cannot open the main config file!");
6157 errors = file->errors;
6160 /* Free macros and check which have not been used. */
6161 while ((sym = TAILQ_FIRST(&symhead))) {
6162 if ((pf->opts & PF_OPT_VERBOSE2) && !sym->used)
6163 fprintf(stderr, "warning: macro '%s' not "
6164 "used\n", sym->nam);
6167 TAILQ_REMOVE(&symhead, sym, entry);
6171 return (errors ? -1 : 0);
6175 symset(const char *nam, const char *val, int persist)
6179 for (sym = TAILQ_FIRST(&symhead); sym && strcmp(nam, sym->nam);
6180 sym = TAILQ_NEXT(sym, entry))
6184 if (sym->persist == 1)
6189 TAILQ_REMOVE(&symhead, sym, entry);
6193 if ((sym = calloc(1, sizeof(*sym))) == NULL)
6196 sym->nam = strdup(nam);
6197 if (sym->nam == NULL) {
6201 sym->val = strdup(val);
6202 if (sym->val == NULL) {
6208 sym->persist = persist;
6209 TAILQ_INSERT_TAIL(&symhead, sym, entry);
6214 pfctl_cmdline_symset(char *s)
6219 if ((val = strrchr(s, '=')) == NULL)
6222 if ((sym = malloc(strlen(s) - strlen(val) + 1)) == NULL)
6223 err(1, "pfctl_cmdline_symset: malloc");
6225 strlcpy(sym, s, strlen(s) - strlen(val) + 1);
6227 ret = symset(sym, val + 1, 1);
6234 symget(const char *nam)
6238 TAILQ_FOREACH(sym, &symhead, entry)
6239 if (strcmp(nam, sym->nam) == 0) {
6247 mv_rules(struct pfctl_ruleset *src, struct pfctl_ruleset *dst)
6250 struct pfctl_rule *r;
6252 for (i = 0; i < PF_RULESET_MAX; ++i) {
6253 while ((r = TAILQ_FIRST(src->rules[i].active.ptr))
6255 TAILQ_REMOVE(src->rules[i].active.ptr, r, entries);
6256 TAILQ_INSERT_TAIL(dst->rules[i].active.ptr, r, entries);
6257 dst->anchor->match++;
6259 src->anchor->match = 0;
6260 while ((r = TAILQ_FIRST(src->rules[i].inactive.ptr))
6262 TAILQ_REMOVE(src->rules[i].inactive.ptr, r, entries);
6263 TAILQ_INSERT_TAIL(dst->rules[i].inactive.ptr,
6270 decide_address_family(struct node_host *n, sa_family_t *af)
6272 if (*af != 0 || n == NULL)
6275 while ((n = n->next) != NULL) {
6284 remove_invalid_hosts(struct node_host **nh, sa_family_t *af)
6286 struct node_host *n = *nh, *prev = NULL;
6289 if (*af && n->af && n->af != *af) {
6290 /* unlink and free n */
6291 struct node_host *next = n->next;
6293 /* adjust tail pointer */
6294 if (n == (*nh)->tail)
6296 /* adjust previous node's next pointer */
6302 if (n->ifname != NULL)
6316 invalid_redirect(struct node_host *nh, sa_family_t af)
6319 struct node_host *n;
6321 /* tables and dyniftl are ok without an address family */
6322 for (n = nh; n != NULL; n = n->next) {
6323 if (n->addr.type != PF_ADDR_TABLE &&
6324 n->addr.type != PF_ADDR_DYNIFTL) {
6325 yyerror("address family not given and "
6326 "translation address expands to multiple "
6327 "address families");
6333 yyerror("no translation address with matching address family "
6341 atoul(char *s, u_long *ulvalp)
6347 ulval = strtoul(s, &ep, 0);
6348 if (s[0] == '\0' || *ep != '\0')
6350 if (errno == ERANGE && ulval == ULONG_MAX)
6362 if (atoul(n, &ulval) == 0) {
6363 if (ulval > 65535) {
6364 yyerror("illegal port value %lu", ulval);
6367 return (htons(ulval));
6369 s = getservbyname(n, "tcp");
6371 s = getservbyname(n, "udp");
6373 yyerror("unknown port %s", n);
6381 rule_label(struct pfctl_rule *r, char *s[PF_RULE_MAX_LABEL_COUNT])
6383 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++) {
6387 if (strlcpy(r->label[i], s[i], sizeof(r->label[0])) >=
6388 sizeof(r->label[0])) {
6389 yyerror("rule label too long (max %d chars)",
6390 sizeof(r->label[0])-1);
6398 parseicmpspec(char *w, sa_family_t af)
6400 const struct icmpcodeent *p;
6405 icmptype = returnicmpdefault >> 8;
6407 icmptype = returnicmp6default >> 8;
6409 if (atoul(w, &ulval) == -1) {
6410 if ((p = geticmpcodebyname(icmptype, w, af)) == NULL) {
6411 yyerror("unknown icmp code %s", w);
6417 yyerror("invalid icmp code %lu", ulval);
6420 return (icmptype << 8 | ulval);
6424 parseport(char *port, struct range *r, int extensions)
6426 char *p = strchr(port, ':');
6429 if ((r->a = getservice(port)) == -1)
6435 if ((extensions & PPORT_STAR) && !strcmp(p+1, "*")) {
6437 if ((r->a = getservice(port)) == -1)
6443 if ((extensions & PPORT_RANGE)) {
6445 if ((r->a = getservice(port)) == -1 ||
6446 (r->b = getservice(p)) == -1)
6459 pfctl_load_anchors(int dev, struct pfctl *pf, struct pfr_buffer *trans)
6461 struct loadanchors *la;
6463 TAILQ_FOREACH(la, &loadanchorshead, entries) {
6464 if (pf->opts & PF_OPT_VERBOSE)
6465 fprintf(stderr, "\nLoading anchor %s from %s\n",
6466 la->anchorname, la->filename);
6467 if (pfctl_rules(dev, la->filename, pf->opts, pf->optimize,
6468 la->anchorname, trans) == -1)
6476 kw_casecmp(const void *k, const void *e)
6478 return (strcasecmp(k, ((const struct keywords *)e)->k_name));
6482 map_tos(char *s, int *val)
6484 /* DiffServ Codepoints and other TOS mappings */
6485 const struct keywords toswords[] = {
6486 { "af11", IPTOS_DSCP_AF11 },
6487 { "af12", IPTOS_DSCP_AF12 },
6488 { "af13", IPTOS_DSCP_AF13 },
6489 { "af21", IPTOS_DSCP_AF21 },
6490 { "af22", IPTOS_DSCP_AF22 },
6491 { "af23", IPTOS_DSCP_AF23 },
6492 { "af31", IPTOS_DSCP_AF31 },
6493 { "af32", IPTOS_DSCP_AF32 },
6494 { "af33", IPTOS_DSCP_AF33 },
6495 { "af41", IPTOS_DSCP_AF41 },
6496 { "af42", IPTOS_DSCP_AF42 },
6497 { "af43", IPTOS_DSCP_AF43 },
6498 { "critical", IPTOS_PREC_CRITIC_ECP },
6499 { "cs0", IPTOS_DSCP_CS0 },
6500 { "cs1", IPTOS_DSCP_CS1 },
6501 { "cs2", IPTOS_DSCP_CS2 },
6502 { "cs3", IPTOS_DSCP_CS3 },
6503 { "cs4", IPTOS_DSCP_CS4 },
6504 { "cs5", IPTOS_DSCP_CS5 },
6505 { "cs6", IPTOS_DSCP_CS6 },
6506 { "cs7", IPTOS_DSCP_CS7 },
6507 { "ef", IPTOS_DSCP_EF },
6508 { "inetcontrol", IPTOS_PREC_INTERNETCONTROL },
6509 { "lowdelay", IPTOS_LOWDELAY },
6510 { "netcontrol", IPTOS_PREC_NETCONTROL },
6511 { "reliability", IPTOS_RELIABILITY },
6512 { "throughput", IPTOS_THROUGHPUT },
6513 { "va", IPTOS_DSCP_VA }
6515 const struct keywords *p;
6517 p = bsearch(s, toswords, sizeof(toswords)/sizeof(toswords[0]),
6518 sizeof(toswords[0]), kw_casecmp);
6528 rt_tableid_max(void)
6532 size_t l = sizeof(fibs);
6534 if (sysctlbyname("net.fibs", &fibs, &l, NULL, 0) == -1)
6535 fibs = 16; /* XXX RT_MAXFIBS, at least limit it some. */
6537 * As the OpenBSD code only compares > and not >= we need to adjust
6538 * here given we only accept values of 0..n and want to avoid #ifdefs
6543 return (RT_TABLEID_MAX);