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 #include <sys/types.h>
36 #include <sys/socket.h>
39 #include <sys/sysctl.h>
42 #include <netinet/in.h>
43 #include <netinet/in_systm.h>
44 #include <netinet/ip.h>
45 #include <netinet/ip_icmp.h>
46 #include <netinet/icmp6.h>
47 #include <net/pfvar.h>
48 #include <arpa/inet.h>
49 #include <net/altq/altq.h>
50 #include <net/altq/altq_cbq.h>
51 #include <net/altq/altq_codel.h>
52 #include <net/altq/altq_priq.h>
53 #include <net/altq/altq_hfsc.h>
54 #include <net/altq/altq_fairq.h>
71 #include "pfctl_parser.h"
74 static struct pfctl *pf = NULL;
76 static int rulestate = 0;
77 static u_int16_t returnicmpdefault =
78 (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
79 static u_int16_t returnicmp6default =
80 (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
81 static int blockpolicy = PFRULE_DROP;
82 static int failpolicy = PFRULE_DROP;
83 static int require_order = 1;
84 static int default_statelock;
86 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
88 TAILQ_ENTRY(file) entry;
94 struct file *pushfile(const char *, int);
96 int check_file_secrecy(int, const char *);
99 int yyerror(const char *, ...);
100 int kw_cmp(const void *, const void *);
106 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
108 TAILQ_ENTRY(sym) entry;
114 int symset(const char *, const char *, int);
115 char *symget(const char *);
117 int atoul(char *, u_long *);
130 struct node_proto *next;
131 struct node_proto *tail;
137 struct node_port *next;
138 struct node_port *tail;
144 struct node_uid *next;
145 struct node_uid *tail;
151 struct node_gid *next;
152 struct node_gid *tail;
159 struct node_icmp *next;
160 struct node_icmp *tail;
163 enum { PF_STATE_OPT_MAX, PF_STATE_OPT_NOSYNC, PF_STATE_OPT_SRCTRACK,
164 PF_STATE_OPT_MAX_SRC_STATES, PF_STATE_OPT_MAX_SRC_CONN,
165 PF_STATE_OPT_MAX_SRC_CONN_RATE, PF_STATE_OPT_MAX_SRC_NODES,
166 PF_STATE_OPT_OVERLOAD, PF_STATE_OPT_STATELOCK,
167 PF_STATE_OPT_TIMEOUT, PF_STATE_OPT_SLOPPY, };
169 enum { PF_SRCTRACK_NONE, PF_SRCTRACK, PF_SRCTRACK_GLOBAL, PF_SRCTRACK_RULE };
171 struct node_state_opt {
174 u_int32_t max_states;
175 u_int32_t max_src_states;
176 u_int32_t max_src_conn;
183 char tblname[PF_TABLE_NAME_SIZE];
185 u_int32_t max_src_nodes;
193 struct node_state_opt *next;
194 struct node_state_opt *tail;
198 struct node_host *host;
199 struct node_port *port;
203 char queue[PF_QNAME_SIZE];
204 char parent[PF_QNAME_SIZE];
205 char ifname[IFNAMSIZ];
207 struct node_queue *next;
208 struct node_queue *tail;
211 struct node_qassign {
218 #define FOM_FLAGS 0x01
219 #define FOM_ICMP 0x02
221 #define FOM_KEEP 0x08
222 #define FOM_SRCTRACK 0x10
223 #define FOM_SETPRIO 0x0400
224 #define FOM_PRIO 0x2000
225 struct node_uid *uid;
226 struct node_gid *gid;
233 struct node_icmp *icmpspec;
238 struct node_state_opt *options;
243 struct node_qassign queues;
246 u_int8_t match_tag_not;
249 u_int8_t set_prio[2];
251 struct node_host *addr;
256 struct antispoof_opts {
263 #define SOM_MINTTL 0x01
264 #define SOM_MAXMSS 0x02
265 #define SOM_FRAGCACHE 0x04
266 #define SOM_SETTOS 0x08
275 u_int8_t match_tag_not;
281 #define QOM_BWSPEC 0x01
282 #define QOM_SCHEDULER 0x02
283 #define QOM_PRIORITY 0x04
284 #define QOM_TBRSIZE 0x08
285 #define QOM_QLIMIT 0x10
286 struct node_queue_bw queue_bwspec;
287 struct node_queue_opt scheduler;
296 struct node_tinithead init_nodes;
301 #define POM_TYPE 0x01
302 #define POM_STICKYADDRESS 0x02
306 struct pf_poolhashkey *key;
310 struct codel_opts codel_opts;
311 struct node_hfsc_opts hfsc_opts;
312 struct node_fairq_opts fairq_opts;
313 struct node_state_opt *keep_state_defaults = NULL;
315 int disallow_table(struct node_host *, const char *);
316 int disallow_urpf_failed(struct node_host *, const char *);
317 int disallow_alias(struct node_host *, const char *);
318 int rule_consistent(struct pf_rule *, int);
319 int filter_consistent(struct pf_rule *, int);
320 int nat_consistent(struct pf_rule *);
321 int rdr_consistent(struct pf_rule *);
322 int process_tabledef(char *, struct table_opts *);
323 void expand_label_str(char *, size_t, const char *, const char *);
324 void expand_label_if(const char *, char *, size_t, const char *);
325 void expand_label_addr(const char *, char *, size_t, u_int8_t,
327 void expand_label_port(const char *, char *, size_t,
329 void expand_label_proto(const char *, char *, size_t, u_int8_t);
330 void expand_label_nr(const char *, char *, size_t);
331 void expand_label(char *, size_t, const char *, u_int8_t,
332 struct node_host *, struct node_port *, struct node_host *,
333 struct node_port *, u_int8_t);
334 void expand_rule(struct pf_rule *, struct node_if *,
335 struct node_host *, struct node_proto *, struct node_os *,
336 struct node_host *, struct node_port *, struct node_host *,
337 struct node_port *, struct node_uid *, struct node_gid *,
338 struct node_icmp *, const char *);
339 int expand_altq(struct pf_altq *, struct node_if *,
340 struct node_queue *, struct node_queue_bw bwspec,
341 struct node_queue_opt *);
342 int expand_queue(struct pf_altq *, struct node_if *,
343 struct node_queue *, struct node_queue_bw,
344 struct node_queue_opt *);
345 int expand_skip_interface(struct node_if *);
347 int check_rulestate(int);
348 int getservice(char *);
349 int rule_label(struct pf_rule *, char *);
350 int rt_tableid_max(void);
352 void mv_rules(struct pf_ruleset *, struct pf_ruleset *);
353 void decide_address_family(struct node_host *, sa_family_t *);
354 void remove_invalid_hosts(struct node_host **, sa_family_t *);
355 int invalid_redirect(struct node_host *, sa_family_t);
356 u_int16_t parseicmpspec(char *, sa_family_t);
358 TAILQ_HEAD(loadanchorshead, loadanchors)
359 loadanchorshead = TAILQ_HEAD_INITIALIZER(loadanchorshead);
362 TAILQ_ENTRY(loadanchors) entries;
385 struct node_if *interface;
386 struct node_proto *proto;
387 struct node_icmp *icmp;
388 struct node_host *host;
390 struct node_port *port;
391 struct node_uid *uid;
392 struct node_gid *gid;
393 struct node_state_opt *state_opt;
396 struct peer src, dst;
397 struct node_os *src_os;
400 struct node_host *host;
404 struct pf_poolhashkey *key;
407 struct node_host *host;
412 struct node_state_opt *options;
423 struct pf_poolhashkey *hashkey;
424 struct node_queue *queue;
425 struct node_queue_opt queue_options;
426 struct node_queue_bw queue_bwspec;
427 struct node_qassign qassign;
428 struct filter_opts filter_opts;
429 struct antispoof_opts antispoof_opts;
430 struct queue_opts queue_opts;
431 struct scrub_opts scrub_opts;
432 struct table_opts table_opts;
433 struct pool_opts pool_opts;
434 struct node_hfsc_opts hfsc_opts;
435 struct node_fairq_opts fairq_opts;
436 struct codel_opts codel_opts;
441 #define PPORT_RANGE 1
443 int parseport(char *, struct range *r, int);
445 #define DYNIF_MULTIADDR(addr) ((addr).type == PF_ADDR_DYNIFTL && \
446 (!((addr).iflags & PFI_AFLAG_NOALIAS) || \
447 !isdigit((addr).v.ifname[strlen((addr).v.ifname)-1])))
451 %token PASS BLOCK SCRUB RETURN IN OS OUT LOG QUICK ON FROM TO FLAGS
452 %token RETURNRST RETURNICMP RETURNICMP6 PROTO INET INET6 ALL ANY ICMPTYPE
453 %token ICMP6TYPE CODE KEEP MODULATE STATE PORT RDR NAT BINAT ARROW NODF
454 %token MINTTL ERROR ALLOWOPTS FASTROUTE FILENAME ROUTETO DUPTO REPLYTO NO LABEL
455 %token NOROUTE URPFFAILED FRAGMENT USER GROUP MAXMSS MAXIMUM TTL TOS DROP TABLE
456 %token REASSEMBLE FRAGDROP FRAGCROP ANCHOR NATANCHOR RDRANCHOR BINATANCHOR
457 %token SET OPTIMIZATION TIMEOUT LIMIT LOGINTERFACE BLOCKPOLICY FAILPOLICY
458 %token RANDOMID REQUIREORDER SYNPROXY FINGERPRINTS NOSYNC DEBUG SKIP HOSTID
459 %token ANTISPOOF FOR INCLUDE
460 %token BITMASK RANDOM SOURCEHASH ROUNDROBIN STATICPORT PROBABILITY
461 %token ALTQ CBQ CODEL PRIQ HFSC FAIRQ BANDWIDTH TBRSIZE LINKSHARE REALTIME
462 %token UPPERLIMIT QUEUE PRIORITY QLIMIT HOGS BUCKETS RTABLE TARGET INTERVAL
463 %token LOAD RULESET_OPTIMIZATION PRIO
464 %token STICKYADDRESS MAXSRCSTATES MAXSRCNODES SOURCETRACK GLOBAL RULE
465 %token MAXSRCCONN MAXSRCCONNRATE OVERLOAD FLUSH SLOPPY
466 %token TAGGED TAG IFBOUND FLOATING STATEPOLICY STATEDEFAULTS ROUTE SETTOS
467 %token DIVERTTO DIVERTREPLY
468 %token <v.string> STRING
469 %token <v.number> NUMBER
470 %token <v.i> PORTBINARY
471 %type <v.interface> interface if_list if_item_not if_item
472 %type <v.number> number icmptype icmp6type uid gid
473 %type <v.number> tos not yesno
474 %type <v.probability> probability
475 %type <v.i> no dir af fragcache optimizer
476 %type <v.i> sourcetrack flush unaryop statelock
477 %type <v.b> action nataction natpasslog scrubaction
478 %type <v.b> flags flag blockspec prio
479 %type <v.range> portplain portstar portrange
480 %type <v.hashkey> hashkey
481 %type <v.proto> proto proto_list proto_item
482 %type <v.number> protoval
483 %type <v.icmp> icmpspec
484 %type <v.icmp> icmp_list icmp_item
485 %type <v.icmp> icmp6_list icmp6_item
486 %type <v.number> reticmpspec reticmp6spec
487 %type <v.fromto> fromto
488 %type <v.peer> ipportspec from to
489 %type <v.host> ipspec toipspec xhost host dynaddr host_list
490 %type <v.host> redir_host_list redirspec
491 %type <v.host> route_host route_host_list routespec
492 %type <v.os> os xos os_list
493 %type <v.port> portspec port_list port_item
494 %type <v.uid> uids uid_list uid_item
495 %type <v.gid> gids gid_list gid_item
496 %type <v.route> route
497 %type <v.redirection> redirection redirpool
498 %type <v.string> label stringall tag anchorname
499 %type <v.string> string varstring numberstring
500 %type <v.keep_state> keep
501 %type <v.state_opt> state_opt_spec state_opt_list state_opt_item
502 %type <v.logquick> logquick quick log logopts logopt
503 %type <v.interface> antispoof_ifspc antispoof_iflst antispoof_if
504 %type <v.qassign> qname
505 %type <v.queue> qassign qassign_list qassign_item
506 %type <v.queue_options> scheduler
507 %type <v.number> cbqflags_list cbqflags_item
508 %type <v.number> priqflags_list priqflags_item
509 %type <v.hfsc_opts> hfscopts_list hfscopts_item hfsc_opts
510 %type <v.fairq_opts> fairqopts_list fairqopts_item fairq_opts
511 %type <v.codel_opts> codelopts_list codelopts_item codel_opts
512 %type <v.queue_bwspec> bandwidth
513 %type <v.filter_opts> filter_opts filter_opt filter_opts_l
514 %type <v.filter_opts> filter_sets filter_set filter_sets_l
515 %type <v.antispoof_opts> antispoof_opts antispoof_opt antispoof_opts_l
516 %type <v.queue_opts> queue_opts queue_opt queue_opts_l
517 %type <v.scrub_opts> scrub_opts scrub_opt scrub_opts_l
518 %type <v.table_opts> table_opts table_opt table_opts_l
519 %type <v.pool_opts> pool_opts pool_opt pool_opts_l
520 %type <v.tagged> tagged
521 %type <v.rtableid> rtable
524 ruleset : /* empty */
525 | ruleset include '\n'
527 | ruleset option '\n'
528 | ruleset scrubrule '\n'
529 | ruleset natrule '\n'
530 | ruleset binatrule '\n'
531 | ruleset pfrule '\n'
532 | ruleset anchorrule '\n'
533 | ruleset loadrule '\n'
534 | ruleset altqif '\n'
535 | ruleset queuespec '\n'
536 | ruleset varset '\n'
537 | ruleset antispoof '\n'
538 | ruleset tabledef '\n'
539 | '{' fakeanchor '}' '\n';
540 | ruleset error '\n' { file->errors++; }
543 include : INCLUDE STRING {
546 if ((nfile = pushfile($2, 0)) == NULL) {
547 yyerror("failed to include file %s", $2);
559 * apply to previouslys specified rule: must be careful to note
560 * what that is: pf or nat or binat or rdr
562 fakeanchor : fakeanchor '\n'
563 | fakeanchor anchorrule '\n'
564 | fakeanchor binatrule '\n'
565 | fakeanchor natrule '\n'
566 | fakeanchor pfrule '\n'
567 | fakeanchor error '\n'
571 if (!strcmp($1, "none"))
573 else if (!strcmp($1, "basic"))
574 $$ = PF_OPTIMIZE_BASIC;
575 else if (!strcmp($1, "profile"))
576 $$ = PF_OPTIMIZE_BASIC | PF_OPTIMIZE_PROFILE;
578 yyerror("unknown ruleset-optimization %s", $1);
584 option : SET OPTIMIZATION STRING {
585 if (check_rulestate(PFCTL_STATE_OPTION)) {
589 if (pfctl_set_optimization(pf, $3) != 0) {
590 yyerror("unknown optimization %s", $3);
596 | SET RULESET_OPTIMIZATION optimizer {
597 if (!(pf->opts & PF_OPT_OPTIMIZE)) {
598 pf->opts |= PF_OPT_OPTIMIZE;
602 | SET TIMEOUT timeout_spec
603 | SET TIMEOUT '{' optnl timeout_list '}'
604 | SET LIMIT limit_spec
605 | SET LIMIT '{' optnl limit_list '}'
606 | SET LOGINTERFACE stringall {
607 if (check_rulestate(PFCTL_STATE_OPTION)) {
611 if (pfctl_set_logif(pf, $3) != 0) {
612 yyerror("error setting loginterface %s", $3);
618 | SET HOSTID number {
619 if ($3 == 0 || $3 > UINT_MAX) {
620 yyerror("hostid must be non-zero");
623 if (pfctl_set_hostid(pf, $3) != 0) {
624 yyerror("error setting hostid %08x", $3);
628 | SET BLOCKPOLICY DROP {
629 if (pf->opts & PF_OPT_VERBOSE)
630 printf("set block-policy drop\n");
631 if (check_rulestate(PFCTL_STATE_OPTION))
633 blockpolicy = PFRULE_DROP;
635 | SET BLOCKPOLICY RETURN {
636 if (pf->opts & PF_OPT_VERBOSE)
637 printf("set block-policy return\n");
638 if (check_rulestate(PFCTL_STATE_OPTION))
640 blockpolicy = PFRULE_RETURN;
642 | SET FAILPOLICY DROP {
643 if (pf->opts & PF_OPT_VERBOSE)
644 printf("set fail-policy drop\n");
645 if (check_rulestate(PFCTL_STATE_OPTION))
647 failpolicy = PFRULE_DROP;
649 | SET FAILPOLICY RETURN {
650 if (pf->opts & PF_OPT_VERBOSE)
651 printf("set fail-policy return\n");
652 if (check_rulestate(PFCTL_STATE_OPTION))
654 failpolicy = PFRULE_RETURN;
656 | SET REQUIREORDER yesno {
657 if (pf->opts & PF_OPT_VERBOSE)
658 printf("set require-order %s\n",
659 $3 == 1 ? "yes" : "no");
662 | SET FINGERPRINTS STRING {
663 if (pf->opts & PF_OPT_VERBOSE)
664 printf("set fingerprints \"%s\"\n", $3);
665 if (check_rulestate(PFCTL_STATE_OPTION)) {
669 if (!pf->anchor->name[0]) {
670 if (pfctl_file_fingerprints(pf->dev,
672 yyerror("error loading "
673 "fingerprints %s", $3);
680 | SET STATEPOLICY statelock {
681 if (pf->opts & PF_OPT_VERBOSE)
684 printf("set state-policy floating\n");
687 printf("set state-policy if-bound\n");
690 default_statelock = $3;
693 if (check_rulestate(PFCTL_STATE_OPTION)) {
697 if (pfctl_set_debug(pf, $3) != 0) {
698 yyerror("error setting debuglevel %s", $3);
704 | SET SKIP interface {
705 if (expand_skip_interface($3) != 0) {
706 yyerror("error setting skip interface(s)");
710 | SET STATEDEFAULTS state_opt_list {
711 if (keep_state_defaults != NULL) {
712 yyerror("cannot redefine state-defaults");
715 keep_state_defaults = $3;
719 stringall : STRING { $$ = $1; }
721 if (($$ = strdup("all")) == NULL) {
722 err(1, "stringall: strdup");
727 string : STRING string {
728 if (asprintf(&$$, "%s %s", $1, $2) == -1)
729 err(1, "string: asprintf");
736 varstring : numberstring varstring {
737 if (asprintf(&$$, "%s %s", $1, $2) == -1)
738 err(1, "string: asprintf");
745 numberstring : NUMBER {
747 if (asprintf(&s, "%lld", (long long)$1) == -1) {
748 yyerror("string: asprintf");
756 varset : STRING '=' varstring {
757 if (pf->opts & PF_OPT_VERBOSE)
758 printf("%s = \"%s\"\n", $1, $3);
759 if (symset($1, $3, 0) == -1)
760 err(1, "cannot store variable %s", $1);
766 anchorname : STRING { $$ = $1; }
767 | /* empty */ { $$ = NULL; }
770 pfa_anchorlist : /* empty */
771 | pfa_anchorlist '\n'
772 | pfa_anchorlist pfrule '\n'
773 | pfa_anchorlist anchorrule '\n'
778 char ta[PF_ANCHOR_NAME_SIZE];
779 struct pf_ruleset *rs;
781 /* steping into a brace anchor */
786 /* create a holding ruleset in the root */
787 snprintf(ta, PF_ANCHOR_NAME_SIZE, "_%d", pf->bn);
788 rs = pf_find_or_create_ruleset(ta);
790 err(1, "pfa_anchor: pf_find_or_create_ruleset");
791 pf->astack[pf->asd] = rs->anchor;
792 pf->anchor = rs->anchor;
793 } '\n' pfa_anchorlist '}'
795 pf->alast = pf->anchor;
797 pf->anchor = pf->astack[pf->asd];
802 anchorrule : ANCHOR anchorname dir quick interface af proto fromto
803 filter_opts pfa_anchor
806 struct node_proto *proto;
808 if (check_rulestate(PFCTL_STATE_FILTER)) {
814 if ($2 && ($2[0] == '_' || strstr($2, "/_") != NULL)) {
816 yyerror("anchor names beginning with '_' "
817 "are reserved for internal use");
821 memset(&r, 0, sizeof(r));
822 if (pf->astack[pf->asd + 1]) {
823 /* move inline rules into relative location */
825 &pf->astack[pf->asd]->ruleset,
826 $2 ? $2 : pf->alast->name);
828 if (r.anchor == NULL)
829 err(1, "anchorrule: unable to "
832 if (pf->alast != r.anchor) {
833 if (r.anchor->match) {
834 yyerror("inline anchor '%s' "
839 mv_rules(&pf->alast->ruleset,
842 pf_remove_if_empty_ruleset(&pf->alast->ruleset);
843 pf->alast = r.anchor;
846 yyerror("anchors without explicit "
847 "rules must specify a name");
855 r.rtableid = $9.rtableid;
858 if (strlcpy(r.tagname, $9.tag,
859 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
860 yyerror("tag too long, max %u chars",
861 PF_TAG_NAME_SIZE - 1);
865 if (strlcpy(r.match_tagname, $9.match_tag,
866 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
867 yyerror("tag too long, max %u chars",
868 PF_TAG_NAME_SIZE - 1);
871 r.match_tag_not = $9.match_tag_not;
872 if (rule_label(&r, $9.label))
875 r.flags = $9.flags.b1;
876 r.flagset = $9.flags.b2;
877 if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
878 yyerror("flags always false");
881 if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
882 for (proto = $7; proto != NULL &&
883 proto->proto != IPPROTO_TCP;
886 if (proto == NULL && $7 != NULL) {
887 if ($9.flags.b1 || $9.flags.b2)
889 "flags only apply to tcp");
892 "OS fingerprinting only "
900 if ($9.keep.action) {
901 yyerror("cannot specify state handling "
907 if (strlcpy(r.match_tagname, $9.match_tag,
908 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
909 yyerror("tag too long, max %u chars",
910 PF_TAG_NAME_SIZE - 1);
913 r.match_tag_not = $9.match_tag_not;
914 if ($9.marker & FOM_PRIO) {
916 r.prio = PF_PRIO_ZERO;
920 if ($9.marker & FOM_SETPRIO) {
921 r.set_prio[0] = $9.set_prio[0];
922 r.set_prio[1] = $9.set_prio[1];
923 r.scrub_flags |= PFSTATE_SETPRIO;
926 decide_address_family($8.src.host, &r.af);
927 decide_address_family($8.dst.host, &r.af);
929 expand_rule(&r, $5, NULL, $7, $8.src_os,
930 $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
931 $9.uid, $9.gid, $9.icmpspec,
932 pf->astack[pf->asd + 1] ? pf->alast->name : $2);
934 pf->astack[pf->asd + 1] = NULL;
936 | NATANCHOR string interface af proto fromto rtable {
939 if (check_rulestate(PFCTL_STATE_NAT)) {
944 memset(&r, 0, sizeof(r));
949 decide_address_family($6.src.host, &r.af);
950 decide_address_family($6.dst.host, &r.af);
952 expand_rule(&r, $3, NULL, $5, $6.src_os,
953 $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
957 | RDRANCHOR string interface af proto fromto rtable {
960 if (check_rulestate(PFCTL_STATE_NAT)) {
965 memset(&r, 0, sizeof(r));
970 decide_address_family($6.src.host, &r.af);
971 decide_address_family($6.dst.host, &r.af);
973 if ($6.src.port != NULL) {
974 yyerror("source port parameter not supported"
978 if ($6.dst.port != NULL) {
979 if ($6.dst.port->next != NULL) {
980 yyerror("destination port list "
981 "expansion not supported in "
984 } else if ($6.dst.port->op != PF_OP_EQ) {
985 yyerror("destination port operators"
986 " not supported in rdr-anchor");
989 r.dst.port[0] = $6.dst.port->port[0];
990 r.dst.port[1] = $6.dst.port->port[1];
991 r.dst.port_op = $6.dst.port->op;
994 expand_rule(&r, $3, NULL, $5, $6.src_os,
995 $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
999 | BINATANCHOR string interface af proto fromto rtable {
1002 if (check_rulestate(PFCTL_STATE_NAT)) {
1007 memset(&r, 0, sizeof(r));
1008 r.action = PF_BINAT;
1012 if ($5->next != NULL) {
1013 yyerror("proto list expansion"
1014 " not supported in binat-anchor");
1017 r.proto = $5->proto;
1021 if ($6.src.host != NULL || $6.src.port != NULL ||
1022 $6.dst.host != NULL || $6.dst.port != NULL) {
1023 yyerror("fromto parameter not supported"
1024 " in binat-anchor");
1028 decide_address_family($6.src.host, &r.af);
1029 decide_address_family($6.dst.host, &r.af);
1031 pfctl_add_rule(pf, &r, $2);
1036 loadrule : LOAD ANCHOR string FROM string {
1037 struct loadanchors *loadanchor;
1039 if (strlen(pf->anchor->name) + 1 +
1040 strlen($3) >= MAXPATHLEN) {
1041 yyerror("anchorname %s too long, max %u\n",
1042 $3, MAXPATHLEN - 1);
1046 loadanchor = calloc(1, sizeof(struct loadanchors));
1047 if (loadanchor == NULL)
1048 err(1, "loadrule: calloc");
1049 if ((loadanchor->anchorname = malloc(MAXPATHLEN)) ==
1051 err(1, "loadrule: malloc");
1052 if (pf->anchor->name[0])
1053 snprintf(loadanchor->anchorname, MAXPATHLEN,
1054 "%s/%s", pf->anchor->name, $3);
1056 strlcpy(loadanchor->anchorname, $3, MAXPATHLEN);
1057 if ((loadanchor->filename = strdup($5)) == NULL)
1058 err(1, "loadrule: strdup");
1060 TAILQ_INSERT_TAIL(&loadanchorshead, loadanchor,
1067 scrubaction : no SCRUB {
1076 scrubrule : scrubaction dir logquick interface af proto fromto scrub_opts
1080 if (check_rulestate(PFCTL_STATE_SCRUB))
1083 memset(&r, 0, sizeof(r));
1091 yyerror("scrub rules do not support 'quick'");
1097 r.rule_flag |= PFRULE_NODF;
1099 r.rule_flag |= PFRULE_RANDOMID;
1100 if ($8.reassemble_tcp) {
1101 if (r.direction != PF_INOUT) {
1102 yyerror("reassemble tcp rules can not "
1103 "specify direction");
1106 r.rule_flag |= PFRULE_REASSEMBLE_TCP;
1109 r.min_ttl = $8.minttl;
1111 r.max_mss = $8.maxmss;
1112 if ($8.marker & SOM_SETTOS) {
1113 r.rule_flag |= PFRULE_SET_TOS;
1114 r.set_tos = $8.settos;
1117 r.rule_flag |= $8.fragcache;
1119 if (strlcpy(r.match_tagname, $8.match_tag,
1120 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1121 yyerror("tag too long, max %u chars",
1122 PF_TAG_NAME_SIZE - 1);
1125 r.match_tag_not = $8.match_tag_not;
1126 r.rtableid = $8.rtableid;
1128 expand_rule(&r, $4, NULL, $6, $7.src_os,
1129 $7.src.host, $7.src.port, $7.dst.host, $7.dst.port,
1130 NULL, NULL, NULL, "");
1135 bzero(&scrub_opts, sizeof scrub_opts);
1136 scrub_opts.rtableid = -1;
1139 { $$ = scrub_opts; }
1141 bzero(&scrub_opts, sizeof scrub_opts);
1142 scrub_opts.rtableid = -1;
1147 scrub_opts_l : scrub_opts_l scrub_opt
1152 if (scrub_opts.nodf) {
1153 yyerror("no-df cannot be respecified");
1156 scrub_opts.nodf = 1;
1159 if (scrub_opts.marker & SOM_MINTTL) {
1160 yyerror("min-ttl cannot be respecified");
1163 if ($2 < 0 || $2 > 255) {
1164 yyerror("illegal min-ttl value %d", $2);
1167 scrub_opts.marker |= SOM_MINTTL;
1168 scrub_opts.minttl = $2;
1171 if (scrub_opts.marker & SOM_MAXMSS) {
1172 yyerror("max-mss cannot be respecified");
1175 if ($2 < 0 || $2 > 65535) {
1176 yyerror("illegal max-mss value %d", $2);
1179 scrub_opts.marker |= SOM_MAXMSS;
1180 scrub_opts.maxmss = $2;
1183 if (scrub_opts.marker & SOM_SETTOS) {
1184 yyerror("set-tos cannot be respecified");
1187 scrub_opts.marker |= SOM_SETTOS;
1188 scrub_opts.settos = $2;
1191 if (scrub_opts.marker & SOM_FRAGCACHE) {
1192 yyerror("fragcache cannot be respecified");
1195 scrub_opts.marker |= SOM_FRAGCACHE;
1196 scrub_opts.fragcache = $1;
1198 | REASSEMBLE STRING {
1199 if (strcasecmp($2, "tcp") != 0) {
1200 yyerror("scrub reassemble supports only tcp, "
1206 if (scrub_opts.reassemble_tcp) {
1207 yyerror("reassemble tcp cannot be respecified");
1210 scrub_opts.reassemble_tcp = 1;
1213 if (scrub_opts.randomid) {
1214 yyerror("random-id cannot be respecified");
1217 scrub_opts.randomid = 1;
1220 if ($2 < 0 || $2 > rt_tableid_max()) {
1221 yyerror("invalid rtable id");
1224 scrub_opts.rtableid = $2;
1226 | not TAGGED string {
1227 scrub_opts.match_tag = $3;
1228 scrub_opts.match_tag_not = $1;
1232 fragcache : FRAGMENT REASSEMBLE { $$ = 0; /* default */ }
1233 | FRAGMENT FRAGCROP { $$ = 0; }
1234 | FRAGMENT FRAGDROP { $$ = 0; }
1237 antispoof : ANTISPOOF logquick antispoof_ifspc af antispoof_opts {
1239 struct node_host *h = NULL, *hh;
1240 struct node_if *i, *j;
1242 if (check_rulestate(PFCTL_STATE_FILTER))
1245 for (i = $3; i; i = i->next) {
1246 bzero(&r, sizeof(r));
1249 r.direction = PF_IN;
1254 if (rule_label(&r, $5.label))
1256 r.rtableid = $5.rtableid;
1257 j = calloc(1, sizeof(struct node_if));
1259 err(1, "antispoof: calloc");
1260 if (strlcpy(j->ifname, i->ifname,
1261 sizeof(j->ifname)) >= sizeof(j->ifname)) {
1263 yyerror("interface name too long");
1268 h = calloc(1, sizeof(*h));
1270 err(1, "address: calloc");
1271 h->addr.type = PF_ADDR_DYNIFTL;
1273 if (strlcpy(h->addr.v.ifname, i->ifname,
1274 sizeof(h->addr.v.ifname)) >=
1275 sizeof(h->addr.v.ifname)) {
1278 "interface name too long");
1281 hh = malloc(sizeof(*hh));
1283 err(1, "address: malloc");
1284 bcopy(h, hh, sizeof(*hh));
1285 h->addr.iflags = PFI_AFLAG_NETWORK;
1287 h = ifa_lookup(j->ifname,
1293 expand_rule(&r, j, NULL, NULL, NULL, h,
1294 NULL, NULL, NULL, NULL, NULL,
1297 if ((i->ifa_flags & IFF_LOOPBACK) == 0) {
1298 bzero(&r, sizeof(r));
1301 r.direction = PF_IN;
1306 if (rule_label(&r, $5.label))
1308 r.rtableid = $5.rtableid;
1312 h = ifa_lookup(i->ifname, 0);
1314 expand_rule(&r, NULL, NULL,
1315 NULL, NULL, h, NULL, NULL,
1316 NULL, NULL, NULL, NULL, "");
1324 antispoof_ifspc : FOR antispoof_if { $$ = $2; }
1325 | FOR '{' optnl antispoof_iflst '}' { $$ = $4; }
1328 antispoof_iflst : antispoof_if optnl { $$ = $1; }
1329 | antispoof_iflst comma antispoof_if optnl {
1330 $1->tail->next = $3;
1336 antispoof_if : if_item { $$ = $1; }
1344 bzero(&antispoof_opts, sizeof antispoof_opts);
1345 antispoof_opts.rtableid = -1;
1348 { $$ = antispoof_opts; }
1350 bzero(&antispoof_opts, sizeof antispoof_opts);
1351 antispoof_opts.rtableid = -1;
1352 $$ = antispoof_opts;
1356 antispoof_opts_l : antispoof_opts_l antispoof_opt
1360 antispoof_opt : label {
1361 if (antispoof_opts.label) {
1362 yyerror("label cannot be redefined");
1365 antispoof_opts.label = $1;
1368 if ($2 < 0 || $2 > rt_tableid_max()) {
1369 yyerror("invalid rtable id");
1372 antispoof_opts.rtableid = $2;
1376 not : '!' { $$ = 1; }
1377 | /* empty */ { $$ = 0; }
1380 tabledef : TABLE '<' STRING '>' table_opts {
1381 struct node_host *h, *nh;
1382 struct node_tinit *ti, *nti;
1384 if (strlen($3) >= PF_TABLE_NAME_SIZE) {
1385 yyerror("table name too long, max %d chars",
1386 PF_TABLE_NAME_SIZE - 1);
1390 if (pf->loadopt & PFCTL_FLAG_TABLE)
1391 if (process_tabledef($3, &$5)) {
1396 for (ti = SIMPLEQ_FIRST(&$5.init_nodes);
1397 ti != SIMPLEQ_END(&$5.init_nodes); ti = nti) {
1400 for (h = ti->host; h != NULL; h = nh) {
1404 nti = SIMPLEQ_NEXT(ti, entries);
1411 bzero(&table_opts, sizeof table_opts);
1412 SIMPLEQ_INIT(&table_opts.init_nodes);
1415 { $$ = table_opts; }
1418 bzero(&table_opts, sizeof table_opts);
1419 SIMPLEQ_INIT(&table_opts.init_nodes);
1424 table_opts_l : table_opts_l table_opt
1428 table_opt : STRING {
1429 if (!strcmp($1, "const"))
1430 table_opts.flags |= PFR_TFLAG_CONST;
1431 else if (!strcmp($1, "persist"))
1432 table_opts.flags |= PFR_TFLAG_PERSIST;
1433 else if (!strcmp($1, "counters"))
1434 table_opts.flags |= PFR_TFLAG_COUNTERS;
1436 yyerror("invalid table option '%s'", $1);
1442 | '{' optnl '}' { table_opts.init_addr = 1; }
1443 | '{' optnl host_list '}' {
1444 struct node_host *n;
1445 struct node_tinit *ti;
1447 for (n = $3; n != NULL; n = n->next) {
1448 switch (n->addr.type) {
1449 case PF_ADDR_ADDRMASK:
1452 yyerror("address ranges are not "
1453 "permitted inside tables");
1455 case PF_ADDR_DYNIFTL:
1456 yyerror("dynamic addresses are not "
1457 "permitted inside tables");
1460 yyerror("tables cannot contain tables");
1462 case PF_ADDR_NOROUTE:
1463 yyerror("\"no-route\" is not permitted "
1466 case PF_ADDR_URPFFAILED:
1467 yyerror("\"urpf-failed\" is not "
1468 "permitted inside tables");
1471 yyerror("unknown address type %d",
1476 if (!(ti = calloc(1, sizeof(*ti))))
1477 err(1, "table_opt: calloc");
1479 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1481 table_opts.init_addr = 1;
1484 struct node_tinit *ti;
1486 if (!(ti = calloc(1, sizeof(*ti))))
1487 err(1, "table_opt: calloc");
1489 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1491 table_opts.init_addr = 1;
1495 altqif : ALTQ interface queue_opts QUEUE qassign {
1498 if (check_rulestate(PFCTL_STATE_QUEUE))
1501 memset(&a, 0, sizeof(a));
1502 if ($3.scheduler.qtype == ALTQT_NONE) {
1503 yyerror("no scheduler specified!");
1506 a.scheduler = $3.scheduler.qtype;
1507 a.qlimit = $3.qlimit;
1508 a.tbrsize = $3.tbrsize;
1509 if ($5 == NULL && $3.scheduler.qtype != ALTQT_CODEL) {
1510 yyerror("no child queues specified");
1513 if (expand_altq(&a, $2, $5, $3.queue_bwspec,
1519 queuespec : QUEUE STRING interface queue_opts qassign {
1522 if (check_rulestate(PFCTL_STATE_QUEUE)) {
1527 memset(&a, 0, sizeof(a));
1529 if (strlcpy(a.qname, $2, sizeof(a.qname)) >=
1531 yyerror("queue name too long (max "
1532 "%d chars)", PF_QNAME_SIZE-1);
1538 yyerror("cannot specify tbrsize for queue");
1541 if ($4.priority > 255) {
1542 yyerror("priority out of range: max 255");
1545 a.priority = $4.priority;
1546 a.qlimit = $4.qlimit;
1547 a.scheduler = $4.scheduler.qtype;
1548 if (expand_queue(&a, $3, $5, $4.queue_bwspec,
1550 yyerror("errors in queue definition");
1557 bzero(&queue_opts, sizeof queue_opts);
1558 queue_opts.priority = DEFAULT_PRIORITY;
1559 queue_opts.qlimit = DEFAULT_QLIMIT;
1560 queue_opts.scheduler.qtype = ALTQT_NONE;
1561 queue_opts.queue_bwspec.bw_percent = 100;
1564 { $$ = queue_opts; }
1566 bzero(&queue_opts, sizeof queue_opts);
1567 queue_opts.priority = DEFAULT_PRIORITY;
1568 queue_opts.qlimit = DEFAULT_QLIMIT;
1569 queue_opts.scheduler.qtype = ALTQT_NONE;
1570 queue_opts.queue_bwspec.bw_percent = 100;
1575 queue_opts_l : queue_opts_l queue_opt
1579 queue_opt : BANDWIDTH bandwidth {
1580 if (queue_opts.marker & QOM_BWSPEC) {
1581 yyerror("bandwidth cannot be respecified");
1584 queue_opts.marker |= QOM_BWSPEC;
1585 queue_opts.queue_bwspec = $2;
1588 if (queue_opts.marker & QOM_PRIORITY) {
1589 yyerror("priority cannot be respecified");
1592 if ($2 < 0 || $2 > 255) {
1593 yyerror("priority out of range: max 255");
1596 queue_opts.marker |= QOM_PRIORITY;
1597 queue_opts.priority = $2;
1600 if (queue_opts.marker & QOM_QLIMIT) {
1601 yyerror("qlimit cannot be respecified");
1604 if ($2 < 0 || $2 > 65535) {
1605 yyerror("qlimit out of range: max 65535");
1608 queue_opts.marker |= QOM_QLIMIT;
1609 queue_opts.qlimit = $2;
1612 if (queue_opts.marker & QOM_SCHEDULER) {
1613 yyerror("scheduler cannot be respecified");
1616 queue_opts.marker |= QOM_SCHEDULER;
1617 queue_opts.scheduler = $1;
1620 if (queue_opts.marker & QOM_TBRSIZE) {
1621 yyerror("tbrsize cannot be respecified");
1624 if ($2 < 0 || $2 > 65535) {
1625 yyerror("tbrsize too big: max 65535");
1628 queue_opts.marker |= QOM_TBRSIZE;
1629 queue_opts.tbrsize = $2;
1633 bandwidth : STRING {
1639 bps = strtod($1, &cp);
1641 if (strlen(cp) > 1) {
1643 if (!strcmp(cu, "Bit") ||
1645 !strcmp(cu, "bit") ||
1650 if (!strcmp(cp, "b"))
1652 else if (!strcmp(cp, "K"))
1654 else if (!strcmp(cp, "M"))
1656 else if (!strcmp(cp, "G"))
1657 bps *= 1000 * 1000 * 1000;
1658 else if (!strcmp(cp, "%")) {
1659 if (bps < 0 || bps > 100) {
1660 yyerror("bandwidth spec "
1665 $$.bw_percent = bps;
1668 yyerror("unknown unit %s", cp);
1674 $$.bw_absolute = (u_int32_t)bps;
1677 if ($1 < 0 || $1 > UINT_MAX) {
1678 yyerror("bandwidth number too big");
1682 $$.bw_absolute = $1;
1687 $$.qtype = ALTQT_CBQ;
1688 $$.data.cbq_opts.flags = 0;
1690 | CBQ '(' cbqflags_list ')' {
1691 $$.qtype = ALTQT_CBQ;
1692 $$.data.cbq_opts.flags = $3;
1695 $$.qtype = ALTQT_PRIQ;
1696 $$.data.priq_opts.flags = 0;
1698 | PRIQ '(' priqflags_list ')' {
1699 $$.qtype = ALTQT_PRIQ;
1700 $$.data.priq_opts.flags = $3;
1703 $$.qtype = ALTQT_HFSC;
1704 bzero(&$$.data.hfsc_opts,
1705 sizeof(struct node_hfsc_opts));
1707 | HFSC '(' hfsc_opts ')' {
1708 $$.qtype = ALTQT_HFSC;
1709 $$.data.hfsc_opts = $3;
1712 $$.qtype = ALTQT_FAIRQ;
1713 bzero(&$$.data.fairq_opts,
1714 sizeof(struct node_fairq_opts));
1716 | FAIRQ '(' fairq_opts ')' {
1717 $$.qtype = ALTQT_FAIRQ;
1718 $$.data.fairq_opts = $3;
1721 $$.qtype = ALTQT_CODEL;
1722 bzero(&$$.data.codel_opts,
1723 sizeof(struct codel_opts));
1725 | CODEL '(' codel_opts ')' {
1726 $$.qtype = ALTQT_CODEL;
1727 $$.data.codel_opts = $3;
1731 cbqflags_list : cbqflags_item { $$ |= $1; }
1732 | cbqflags_list comma cbqflags_item { $$ |= $3; }
1735 cbqflags_item : STRING {
1736 if (!strcmp($1, "default"))
1737 $$ = CBQCLF_DEFCLASS;
1738 else if (!strcmp($1, "borrow"))
1740 else if (!strcmp($1, "red"))
1742 else if (!strcmp($1, "ecn"))
1743 $$ = CBQCLF_RED|CBQCLF_ECN;
1744 else if (!strcmp($1, "rio"))
1746 else if (!strcmp($1, "codel"))
1749 yyerror("unknown cbq flag \"%s\"", $1);
1757 priqflags_list : priqflags_item { $$ |= $1; }
1758 | priqflags_list comma priqflags_item { $$ |= $3; }
1761 priqflags_item : STRING {
1762 if (!strcmp($1, "default"))
1763 $$ = PRCF_DEFAULTCLASS;
1764 else if (!strcmp($1, "red"))
1766 else if (!strcmp($1, "ecn"))
1767 $$ = PRCF_RED|PRCF_ECN;
1768 else if (!strcmp($1, "rio"))
1770 else if (!strcmp($1, "codel"))
1773 yyerror("unknown priq flag \"%s\"", $1);
1783 sizeof(struct node_hfsc_opts));
1790 hfscopts_list : hfscopts_item
1791 | hfscopts_list comma hfscopts_item
1794 hfscopts_item : LINKSHARE bandwidth {
1795 if (hfsc_opts.linkshare.used) {
1796 yyerror("linkshare already specified");
1799 hfsc_opts.linkshare.m2 = $2;
1800 hfsc_opts.linkshare.used = 1;
1802 | LINKSHARE '(' bandwidth comma NUMBER comma bandwidth ')'
1804 if ($5 < 0 || $5 > INT_MAX) {
1805 yyerror("timing in curve out of range");
1808 if (hfsc_opts.linkshare.used) {
1809 yyerror("linkshare already specified");
1812 hfsc_opts.linkshare.m1 = $3;
1813 hfsc_opts.linkshare.d = $5;
1814 hfsc_opts.linkshare.m2 = $7;
1815 hfsc_opts.linkshare.used = 1;
1817 | REALTIME bandwidth {
1818 if (hfsc_opts.realtime.used) {
1819 yyerror("realtime already specified");
1822 hfsc_opts.realtime.m2 = $2;
1823 hfsc_opts.realtime.used = 1;
1825 | REALTIME '(' bandwidth comma NUMBER comma bandwidth ')'
1827 if ($5 < 0 || $5 > INT_MAX) {
1828 yyerror("timing in curve out of range");
1831 if (hfsc_opts.realtime.used) {
1832 yyerror("realtime already specified");
1835 hfsc_opts.realtime.m1 = $3;
1836 hfsc_opts.realtime.d = $5;
1837 hfsc_opts.realtime.m2 = $7;
1838 hfsc_opts.realtime.used = 1;
1840 | UPPERLIMIT bandwidth {
1841 if (hfsc_opts.upperlimit.used) {
1842 yyerror("upperlimit already specified");
1845 hfsc_opts.upperlimit.m2 = $2;
1846 hfsc_opts.upperlimit.used = 1;
1848 | UPPERLIMIT '(' bandwidth comma NUMBER comma bandwidth ')'
1850 if ($5 < 0 || $5 > INT_MAX) {
1851 yyerror("timing in curve out of range");
1854 if (hfsc_opts.upperlimit.used) {
1855 yyerror("upperlimit already specified");
1858 hfsc_opts.upperlimit.m1 = $3;
1859 hfsc_opts.upperlimit.d = $5;
1860 hfsc_opts.upperlimit.m2 = $7;
1861 hfsc_opts.upperlimit.used = 1;
1864 if (!strcmp($1, "default"))
1865 hfsc_opts.flags |= HFCF_DEFAULTCLASS;
1866 else if (!strcmp($1, "red"))
1867 hfsc_opts.flags |= HFCF_RED;
1868 else if (!strcmp($1, "ecn"))
1869 hfsc_opts.flags |= HFCF_RED|HFCF_ECN;
1870 else if (!strcmp($1, "rio"))
1871 hfsc_opts.flags |= HFCF_RIO;
1872 else if (!strcmp($1, "codel"))
1873 hfsc_opts.flags |= HFCF_CODEL;
1875 yyerror("unknown hfsc flag \"%s\"", $1);
1885 sizeof(struct node_fairq_opts));
1892 fairqopts_list : fairqopts_item
1893 | fairqopts_list comma fairqopts_item
1896 fairqopts_item : LINKSHARE bandwidth {
1897 if (fairq_opts.linkshare.used) {
1898 yyerror("linkshare already specified");
1901 fairq_opts.linkshare.m2 = $2;
1902 fairq_opts.linkshare.used = 1;
1904 | LINKSHARE '(' bandwidth number bandwidth ')' {
1905 if (fairq_opts.linkshare.used) {
1906 yyerror("linkshare already specified");
1909 fairq_opts.linkshare.m1 = $3;
1910 fairq_opts.linkshare.d = $4;
1911 fairq_opts.linkshare.m2 = $5;
1912 fairq_opts.linkshare.used = 1;
1915 fairq_opts.hogs_bw = $2;
1918 fairq_opts.nbuckets = $2;
1921 if (!strcmp($1, "default"))
1922 fairq_opts.flags |= FARF_DEFAULTCLASS;
1923 else if (!strcmp($1, "red"))
1924 fairq_opts.flags |= FARF_RED;
1925 else if (!strcmp($1, "ecn"))
1926 fairq_opts.flags |= FARF_RED|FARF_ECN;
1927 else if (!strcmp($1, "rio"))
1928 fairq_opts.flags |= FARF_RIO;
1929 else if (!strcmp($1, "codel"))
1930 fairq_opts.flags |= FARF_CODEL;
1932 yyerror("unknown fairq flag \"%s\"", $1);
1942 sizeof(struct codel_opts));
1949 codelopts_list : codelopts_item
1950 | codelopts_list comma codelopts_item
1953 codelopts_item : INTERVAL number {
1954 if (codel_opts.interval) {
1955 yyerror("interval already specified");
1958 codel_opts.interval = $2;
1961 if (codel_opts.target) {
1962 yyerror("target already specified");
1965 codel_opts.target = $2;
1968 if (!strcmp($1, "ecn"))
1971 yyerror("unknown codel option \"%s\"", $1);
1979 qassign : /* empty */ { $$ = NULL; }
1980 | qassign_item { $$ = $1; }
1981 | '{' optnl qassign_list '}' { $$ = $3; }
1984 qassign_list : qassign_item optnl { $$ = $1; }
1985 | qassign_list comma qassign_item optnl {
1986 $1->tail->next = $3;
1992 qassign_item : STRING {
1993 $$ = calloc(1, sizeof(struct node_queue));
1995 err(1, "qassign_item: calloc");
1996 if (strlcpy($$->queue, $1, sizeof($$->queue)) >=
1997 sizeof($$->queue)) {
1998 yyerror("queue name '%s' too long (max "
1999 "%d chars)", $1, sizeof($$->queue)-1);
2010 pfrule : action dir logquick interface route af proto fromto
2014 struct node_state_opt *o;
2015 struct node_proto *proto;
2021 if (check_rulestate(PFCTL_STATE_FILTER))
2024 memset(&r, 0, sizeof(r));
2028 case PFRULE_RETURNRST:
2029 r.rule_flag |= PFRULE_RETURNRST;
2030 r.return_ttl = $1.w;
2032 case PFRULE_RETURNICMP:
2033 r.rule_flag |= PFRULE_RETURNICMP;
2034 r.return_icmp = $1.w;
2035 r.return_icmp6 = $1.w2;
2038 r.rule_flag |= PFRULE_RETURN;
2039 r.return_icmp = $1.w;
2040 r.return_icmp6 = $1.w2;
2048 r.rtableid = $9.rtableid;
2050 if ($9.marker & FOM_PRIO) {
2052 r.prio = PF_PRIO_ZERO;
2056 if ($9.marker & FOM_SETPRIO) {
2057 r.set_prio[0] = $9.set_prio[0];
2058 r.set_prio[1] = $9.set_prio[1];
2059 r.scrub_flags |= PFSTATE_SETPRIO;
2064 if (strlcpy(r.tagname, $9.tag,
2065 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
2066 yyerror("tag too long, max %u chars",
2067 PF_TAG_NAME_SIZE - 1);
2071 if (strlcpy(r.match_tagname, $9.match_tag,
2072 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
2073 yyerror("tag too long, max %u chars",
2074 PF_TAG_NAME_SIZE - 1);
2077 r.match_tag_not = $9.match_tag_not;
2078 if (rule_label(&r, $9.label))
2081 r.flags = $9.flags.b1;
2082 r.flagset = $9.flags.b2;
2083 if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
2084 yyerror("flags always false");
2087 if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
2088 for (proto = $7; proto != NULL &&
2089 proto->proto != IPPROTO_TCP;
2090 proto = proto->next)
2092 if (proto == NULL && $7 != NULL) {
2093 if ($9.flags.b1 || $9.flags.b2)
2095 "flags only apply to tcp");
2098 "OS fingerprinting only "
2103 if (($9.flags.b1 & parse_flags("S")) == 0 &&
2105 yyerror("OS fingerprinting requires "
2106 "the SYN TCP flag (flags S/SA)");
2113 r.keep_state = $9.keep.action;
2114 o = $9.keep.options;
2116 /* 'keep state' by default on pass rules. */
2117 if (!r.keep_state && !r.action &&
2118 !($9.marker & FOM_KEEP)) {
2119 r.keep_state = PF_STATE_NORMAL;
2120 o = keep_state_defaults;
2125 struct node_state_opt *p = o;
2128 case PF_STATE_OPT_MAX:
2130 yyerror("state option 'max' "
2131 "multiple definitions");
2134 r.max_states = o->data.max_states;
2136 case PF_STATE_OPT_NOSYNC:
2137 if (r.rule_flag & PFRULE_NOSYNC) {
2138 yyerror("state option 'sync' "
2139 "multiple definitions");
2142 r.rule_flag |= PFRULE_NOSYNC;
2144 case PF_STATE_OPT_SRCTRACK:
2146 yyerror("state option "
2148 "multiple definitions");
2151 srctrack = o->data.src_track;
2152 r.rule_flag |= PFRULE_SRCTRACK;
2154 case PF_STATE_OPT_MAX_SRC_STATES:
2155 if (r.max_src_states) {
2156 yyerror("state option "
2158 "multiple definitions");
2161 if (o->data.max_src_states == 0) {
2162 yyerror("'max-src-states' must "
2167 o->data.max_src_states;
2168 r.rule_flag |= PFRULE_SRCTRACK;
2170 case PF_STATE_OPT_OVERLOAD:
2171 if (r.overload_tblname[0]) {
2172 yyerror("multiple 'overload' "
2173 "table definitions");
2176 if (strlcpy(r.overload_tblname,
2177 o->data.overload.tblname,
2178 PF_TABLE_NAME_SIZE) >=
2179 PF_TABLE_NAME_SIZE) {
2180 yyerror("state option: "
2184 r.flush = o->data.overload.flush;
2186 case PF_STATE_OPT_MAX_SRC_CONN:
2187 if (r.max_src_conn) {
2188 yyerror("state option "
2190 "multiple definitions");
2193 if (o->data.max_src_conn == 0) {
2194 yyerror("'max-src-conn' "
2199 o->data.max_src_conn;
2200 r.rule_flag |= PFRULE_SRCTRACK |
2201 PFRULE_RULESRCTRACK;
2203 case PF_STATE_OPT_MAX_SRC_CONN_RATE:
2204 if (r.max_src_conn_rate.limit) {
2205 yyerror("state option "
2206 "'max-src-conn-rate' "
2207 "multiple definitions");
2210 if (!o->data.max_src_conn_rate.limit ||
2211 !o->data.max_src_conn_rate.seconds) {
2212 yyerror("'max-src-conn-rate' "
2213 "values must be > 0");
2216 if (o->data.max_src_conn_rate.limit >
2218 yyerror("'max-src-conn-rate' "
2219 "maximum rate must be < %u",
2223 r.max_src_conn_rate.limit =
2224 o->data.max_src_conn_rate.limit;
2225 r.max_src_conn_rate.seconds =
2226 o->data.max_src_conn_rate.seconds;
2227 r.rule_flag |= PFRULE_SRCTRACK |
2228 PFRULE_RULESRCTRACK;
2230 case PF_STATE_OPT_MAX_SRC_NODES:
2231 if (r.max_src_nodes) {
2232 yyerror("state option "
2234 "multiple definitions");
2237 if (o->data.max_src_nodes == 0) {
2238 yyerror("'max-src-nodes' must "
2243 o->data.max_src_nodes;
2244 r.rule_flag |= PFRULE_SRCTRACK |
2245 PFRULE_RULESRCTRACK;
2247 case PF_STATE_OPT_STATELOCK:
2249 yyerror("state locking option: "
2250 "multiple definitions");
2254 r.rule_flag |= o->data.statelock;
2256 case PF_STATE_OPT_SLOPPY:
2257 if (r.rule_flag & PFRULE_STATESLOPPY) {
2258 yyerror("state sloppy option: "
2259 "multiple definitions");
2262 r.rule_flag |= PFRULE_STATESLOPPY;
2264 case PF_STATE_OPT_TIMEOUT:
2265 if (o->data.timeout.number ==
2266 PFTM_ADAPTIVE_START ||
2267 o->data.timeout.number ==
2270 if (r.timeout[o->data.timeout.number]) {
2271 yyerror("state timeout %s "
2272 "multiple definitions",
2273 pf_timeouts[o->data.
2274 timeout.number].name);
2277 r.timeout[o->data.timeout.number] =
2278 o->data.timeout.seconds;
2285 /* 'flags S/SA' by default on stateful rules */
2286 if (!r.action && !r.flags && !r.flagset &&
2287 !$9.fragment && !($9.marker & FOM_FLAGS) &&
2289 r.flags = parse_flags("S");
2290 r.flagset = parse_flags("SA");
2292 if (!adaptive && r.max_states) {
2293 r.timeout[PFTM_ADAPTIVE_START] =
2294 (r.max_states / 10) * 6;
2295 r.timeout[PFTM_ADAPTIVE_END] =
2296 (r.max_states / 10) * 12;
2298 if (r.rule_flag & PFRULE_SRCTRACK) {
2299 if (srctrack == PF_SRCTRACK_GLOBAL &&
2301 yyerror("'max-src-nodes' is "
2302 "incompatible with "
2303 "'source-track global'");
2306 if (srctrack == PF_SRCTRACK_GLOBAL &&
2308 yyerror("'max-src-conn' is "
2309 "incompatible with "
2310 "'source-track global'");
2313 if (srctrack == PF_SRCTRACK_GLOBAL &&
2314 r.max_src_conn_rate.seconds) {
2315 yyerror("'max-src-conn-rate' is "
2316 "incompatible with "
2317 "'source-track global'");
2320 if (r.timeout[PFTM_SRC_NODE] <
2321 r.max_src_conn_rate.seconds)
2322 r.timeout[PFTM_SRC_NODE] =
2323 r.max_src_conn_rate.seconds;
2324 r.rule_flag |= PFRULE_SRCTRACK;
2325 if (srctrack == PF_SRCTRACK_RULE)
2326 r.rule_flag |= PFRULE_RULESRCTRACK;
2328 if (r.keep_state && !statelock)
2329 r.rule_flag |= default_statelock;
2332 r.rule_flag |= PFRULE_FRAGMENT;
2333 r.allow_opts = $9.allowopts;
2335 decide_address_family($8.src.host, &r.af);
2336 decide_address_family($8.dst.host, &r.af);
2340 yyerror("direction must be explicit "
2341 "with rules that specify routing");
2345 r.rpool.opts = $5.pool_opts;
2347 memcpy(&r.rpool.key, $5.key,
2348 sizeof(struct pf_poolhashkey));
2350 if (r.rt && r.rt != PF_FASTROUTE) {
2351 decide_address_family($5.host, &r.af);
2352 remove_invalid_hosts(&$5.host, &r.af);
2353 if ($5.host == NULL) {
2354 yyerror("no routing address with "
2355 "matching address family found.");
2358 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
2359 PF_POOL_NONE && ($5.host->next != NULL ||
2360 $5.host->addr.type == PF_ADDR_TABLE ||
2361 DYNIF_MULTIADDR($5.host->addr)))
2362 r.rpool.opts |= PF_POOL_ROUNDROBIN;
2363 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2364 PF_POOL_ROUNDROBIN &&
2365 disallow_table($5.host, "tables are only "
2366 "supported in round-robin routing pools"))
2368 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2369 PF_POOL_ROUNDROBIN &&
2370 disallow_alias($5.host, "interface (%s) "
2371 "is only supported in round-robin "
2374 if ($5.host->next != NULL) {
2375 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2376 PF_POOL_ROUNDROBIN) {
2377 yyerror("r.rpool.opts must "
2378 "be PF_POOL_ROUNDROBIN");
2383 if ($9.queues.qname != NULL) {
2384 if (strlcpy(r.qname, $9.queues.qname,
2385 sizeof(r.qname)) >= sizeof(r.qname)) {
2386 yyerror("rule qname too long (max "
2387 "%d chars)", sizeof(r.qname)-1);
2390 free($9.queues.qname);
2392 if ($9.queues.pqname != NULL) {
2393 if (strlcpy(r.pqname, $9.queues.pqname,
2394 sizeof(r.pqname)) >= sizeof(r.pqname)) {
2395 yyerror("rule pqname too long (max "
2396 "%d chars)", sizeof(r.pqname)-1);
2399 free($9.queues.pqname);
2402 r.divert.port = $9.divert.port;
2404 if ((r.divert.port = $9.divert.port)) {
2405 if (r.direction == PF_OUT) {
2406 if ($9.divert.addr) {
2407 yyerror("address specified "
2408 "for outgoing divert");
2411 bzero(&r.divert.addr,
2412 sizeof(r.divert.addr));
2414 if (!$9.divert.addr) {
2415 yyerror("no address specified "
2416 "for incoming divert");
2419 if ($9.divert.addr->af != r.af) {
2420 yyerror("address family "
2421 "mismatch for divert");
2425 $9.divert.addr->addr.v.a.addr;
2430 expand_rule(&r, $4, $5.host, $7, $8.src_os,
2431 $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
2432 $9.uid, $9.gid, $9.icmpspec, "");
2437 bzero(&filter_opts, sizeof filter_opts);
2438 filter_opts.rtableid = -1;
2441 { $$ = filter_opts; }
2443 bzero(&filter_opts, sizeof filter_opts);
2444 filter_opts.rtableid = -1;
2449 filter_opts_l : filter_opts_l filter_opt
2453 filter_opt : USER uids {
2454 if (filter_opts.uid)
2455 $2->tail->next = filter_opts.uid;
2456 filter_opts.uid = $2;
2459 if (filter_opts.gid)
2460 $2->tail->next = filter_opts.gid;
2461 filter_opts.gid = $2;
2464 if (filter_opts.marker & FOM_FLAGS) {
2465 yyerror("flags cannot be redefined");
2468 filter_opts.marker |= FOM_FLAGS;
2469 filter_opts.flags.b1 |= $1.b1;
2470 filter_opts.flags.b2 |= $1.b2;
2471 filter_opts.flags.w |= $1.w;
2472 filter_opts.flags.w2 |= $1.w2;
2475 if (filter_opts.marker & FOM_ICMP) {
2476 yyerror("icmp-type cannot be redefined");
2479 filter_opts.marker |= FOM_ICMP;
2480 filter_opts.icmpspec = $1;
2483 if (filter_opts.marker & FOM_PRIO) {
2484 yyerror("prio cannot be redefined");
2487 if ($2 < 0 || $2 > PF_PRIO_MAX) {
2488 yyerror("prio must be 0 - %u", PF_PRIO_MAX);
2491 filter_opts.marker |= FOM_PRIO;
2492 filter_opts.prio = $2;
2495 if (filter_opts.marker & FOM_TOS) {
2496 yyerror("tos cannot be redefined");
2499 filter_opts.marker |= FOM_TOS;
2500 filter_opts.tos = $2;
2503 if (filter_opts.marker & FOM_KEEP) {
2504 yyerror("modulate or keep cannot be redefined");
2507 filter_opts.marker |= FOM_KEEP;
2508 filter_opts.keep.action = $1.action;
2509 filter_opts.keep.options = $1.options;
2512 filter_opts.fragment = 1;
2515 filter_opts.allowopts = 1;
2518 if (filter_opts.label) {
2519 yyerror("label cannot be redefined");
2522 filter_opts.label = $1;
2525 if (filter_opts.queues.qname) {
2526 yyerror("queue cannot be redefined");
2529 filter_opts.queues = $1;
2532 filter_opts.tag = $2;
2534 | not TAGGED string {
2535 filter_opts.match_tag = $3;
2536 filter_opts.match_tag_not = $1;
2538 | PROBABILITY probability {
2541 p = floor($2 * UINT_MAX + 0.5);
2542 if (p < 0.0 || p > UINT_MAX) {
2543 yyerror("invalid probability: %lf", p);
2546 filter_opts.prob = (u_int32_t)p;
2547 if (filter_opts.prob == 0)
2548 filter_opts.prob = 1;
2551 if ($2 < 0 || $2 > rt_tableid_max()) {
2552 yyerror("invalid rtable id");
2555 filter_opts.rtableid = $2;
2557 | DIVERTTO portplain {
2559 filter_opts.divert.port = $2.a;
2560 if (!filter_opts.divert.port) {
2561 yyerror("invalid divert port: %u", ntohs($2.a));
2566 | DIVERTTO STRING PORT portplain {
2568 if ((filter_opts.divert.addr = host($2)) == NULL) {
2569 yyerror("could not parse divert address: %s",
2578 filter_opts.divert.port = $4.a;
2579 if (!filter_opts.divert.port) {
2580 yyerror("invalid divert port: %u", ntohs($4.a));
2586 yyerror("divert-reply has no meaning in FreeBSD pf(4)");
2589 filter_opts.divert.port = 1; /* some random value */
2595 filter_sets : SET '(' filter_sets_l ')' { $$ = filter_opts; }
2596 | SET filter_set { $$ = filter_opts; }
2599 filter_sets_l : filter_sets_l comma filter_set
2604 if (filter_opts.marker & FOM_SETPRIO) {
2605 yyerror("prio cannot be redefined");
2608 filter_opts.marker |= FOM_SETPRIO;
2609 filter_opts.set_prio[0] = $1.b1;
2610 filter_opts.set_prio[1] = $1.b2;
2612 prio : PRIO NUMBER {
2613 if ($2 < 0 || $2 > PF_PRIO_MAX) {
2614 yyerror("prio must be 0 - %u", PF_PRIO_MAX);
2619 | PRIO '(' NUMBER comma NUMBER ')' {
2620 if ($3 < 0 || $3 > PF_PRIO_MAX ||
2621 $5 < 0 || $5 > PF_PRIO_MAX) {
2622 yyerror("prio must be 0 - %u", PF_PRIO_MAX);
2630 probability : STRING {
2632 double p = strtod($1, &e);
2639 yyerror("invalid probability: %s", $1);
2655 $$.w = returnicmpdefault;
2656 $$.w2 = returnicmp6default;
2658 | BLOCK blockspec { $$ = $2; $$.b1 = PF_DROP; }
2661 blockspec : /* empty */ {
2662 $$.b2 = blockpolicy;
2663 $$.w = returnicmpdefault;
2664 $$.w2 = returnicmp6default;
2667 $$.b2 = PFRULE_DROP;
2672 $$.b2 = PFRULE_RETURNRST;
2676 | RETURNRST '(' TTL NUMBER ')' {
2677 if ($4 < 0 || $4 > 255) {
2678 yyerror("illegal ttl value %d", $4);
2681 $$.b2 = PFRULE_RETURNRST;
2686 $$.b2 = PFRULE_RETURNICMP;
2687 $$.w = returnicmpdefault;
2688 $$.w2 = returnicmp6default;
2691 $$.b2 = PFRULE_RETURNICMP;
2692 $$.w = returnicmpdefault;
2693 $$.w2 = returnicmp6default;
2695 | RETURNICMP '(' reticmpspec ')' {
2696 $$.b2 = PFRULE_RETURNICMP;
2698 $$.w2 = returnicmpdefault;
2700 | RETURNICMP6 '(' reticmp6spec ')' {
2701 $$.b2 = PFRULE_RETURNICMP;
2702 $$.w = returnicmpdefault;
2705 | RETURNICMP '(' reticmpspec comma reticmp6spec ')' {
2706 $$.b2 = PFRULE_RETURNICMP;
2711 $$.b2 = PFRULE_RETURN;
2712 $$.w = returnicmpdefault;
2713 $$.w2 = returnicmp6default;
2717 reticmpspec : STRING {
2718 if (!($$ = parseicmpspec($1, AF_INET))) {
2727 if ($1 < 0 || $1 > 255) {
2728 yyerror("invalid icmp code %lu", $1);
2731 icmptype = returnicmpdefault >> 8;
2732 $$ = (icmptype << 8 | $1);
2736 reticmp6spec : STRING {
2737 if (!($$ = parseicmpspec($1, AF_INET6))) {
2746 if ($1 < 0 || $1 > 255) {
2747 yyerror("invalid icmp code %lu", $1);
2750 icmptype = returnicmp6default >> 8;
2751 $$ = (icmptype << 8 | $1);
2755 dir : /* empty */ { $$ = PF_INOUT; }
2756 | IN { $$ = PF_IN; }
2757 | OUT { $$ = PF_OUT; }
2760 quick : /* empty */ { $$.quick = 0; }
2761 | QUICK { $$.quick = 1; }
2764 logquick : /* empty */ { $$.log = 0; $$.quick = 0; $$.logif = 0; }
2765 | log { $$ = $1; $$.quick = 0; }
2766 | QUICK { $$.quick = 1; $$.log = 0; $$.logif = 0; }
2767 | log QUICK { $$ = $1; $$.quick = 1; }
2768 | QUICK log { $$ = $2; $$.quick = 1; }
2771 log : LOG { $$.log = PF_LOG; $$.logif = 0; }
2772 | LOG '(' logopts ')' {
2773 $$.log = PF_LOG | $3.log;
2774 $$.logif = $3.logif;
2778 logopts : logopt { $$ = $1; }
2779 | logopts comma logopt {
2780 $$.log = $1.log | $3.log;
2781 $$.logif = $3.logif;
2783 $$.logif = $1.logif;
2787 logopt : ALL { $$.log = PF_LOG_ALL; $$.logif = 0; }
2788 | USER { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
2789 | GROUP { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
2795 if (strncmp($2, "pflog", 5)) {
2796 yyerror("%s: should be a pflog interface", $2);
2800 i = strtonum($2 + 5, 0, 255, &errstr);
2802 yyerror("%s: %s", $2, errstr);
2811 interface : /* empty */ { $$ = NULL; }
2812 | ON if_item_not { $$ = $2; }
2813 | ON '{' optnl if_list '}' { $$ = $4; }
2816 if_list : if_item_not optnl { $$ = $1; }
2817 | if_list comma if_item_not optnl {
2818 $1->tail->next = $3;
2824 if_item_not : not if_item { $$ = $2; $$->not = $1; }
2828 struct node_host *n;
2830 $$ = calloc(1, sizeof(struct node_if));
2832 err(1, "if_item: calloc");
2833 if (strlcpy($$->ifname, $1, sizeof($$->ifname)) >=
2834 sizeof($$->ifname)) {
2837 yyerror("interface name too long");
2841 if ((n = ifa_exists($1)) != NULL)
2842 $$->ifa_flags = n->ifa_flags;
2851 af : /* empty */ { $$ = 0; }
2852 | INET { $$ = AF_INET; }
2853 | INET6 { $$ = AF_INET6; }
2856 proto : /* empty */ { $$ = NULL; }
2857 | PROTO proto_item { $$ = $2; }
2858 | PROTO '{' optnl proto_list '}' { $$ = $4; }
2861 proto_list : proto_item optnl { $$ = $1; }
2862 | proto_list comma proto_item optnl {
2863 $1->tail->next = $3;
2869 proto_item : protoval {
2874 yyerror("proto 0 cannot be used");
2877 $$ = calloc(1, sizeof(struct node_proto));
2879 err(1, "proto_item: calloc");
2889 p = getprotobyname($1);
2891 yyerror("unknown protocol %s", $1);
2899 if ($1 < 0 || $1 > 255) {
2900 yyerror("protocol outside range");
2920 os : /* empty */ { $$ = NULL; }
2921 | OS xos { $$ = $2; }
2922 | OS '{' optnl os_list '}' { $$ = $4; }
2926 $$ = calloc(1, sizeof(struct node_os));
2928 err(1, "os: calloc");
2934 os_list : xos optnl { $$ = $1; }
2935 | os_list comma xos optnl {
2936 $1->tail->next = $3;
2942 from : /* empty */ {
2956 if (disallow_urpf_failed($2.host, "\"urpf-failed\" is "
2957 "not permitted in a destination address"))
2963 ipportspec : ipspec {
2967 | ipspec PORT portspec {
2981 ipspec : ANY { $$ = NULL; }
2982 | xhost { $$ = $1; }
2983 | '{' optnl host_list '}' { $$ = $3; }
2986 toipspec : TO ipspec { $$ = $2; }
2987 | /* empty */ { $$ = NULL; }
2990 host_list : ipspec optnl { $$ = $1; }
2991 | host_list comma ipspec optnl {
2994 else if ($1 == NULL)
2997 $1->tail->next = $3;
2998 $1->tail = $3->tail;
3005 struct node_host *n;
3007 for (n = $2; n != NULL; n = n->next)
3012 $$ = calloc(1, sizeof(struct node_host));
3014 err(1, "xhost: calloc");
3015 $$->addr.type = PF_ADDR_NOROUTE;
3021 $$ = calloc(1, sizeof(struct node_host));
3023 err(1, "xhost: calloc");
3024 $$->addr.type = PF_ADDR_URPFFAILED;
3032 if (($$ = host($1)) == NULL) {
3033 /* error. "any" is handled elsewhere */
3035 yyerror("could not parse host specification");
3041 | STRING '-' STRING {
3042 struct node_host *b, *e;
3044 if ((b = host($1)) == NULL || (e = host($3)) == NULL) {
3047 yyerror("could not parse host specification");
3050 if (b->af != e->af ||
3051 b->addr.type != PF_ADDR_ADDRMASK ||
3052 e->addr.type != PF_ADDR_ADDRMASK ||
3053 unmask(&b->addr.v.a.mask, b->af) !=
3054 (b->af == AF_INET ? 32 : 128) ||
3055 unmask(&e->addr.v.a.mask, e->af) !=
3056 (e->af == AF_INET ? 32 : 128) ||
3057 b->next != NULL || b->not ||
3058 e->next != NULL || e->not) {
3063 yyerror("invalid address range");
3066 memcpy(&b->addr.v.a.mask, &e->addr.v.a.addr,
3067 sizeof(b->addr.v.a.mask));
3068 b->addr.type = PF_ADDR_RANGE;
3074 | STRING '/' NUMBER {
3077 if (asprintf(&buf, "%s/%lld", $1, (long long)$3) == -1)
3078 err(1, "host: asprintf");
3080 if (($$ = host(buf)) == NULL) {
3081 /* error. "any" is handled elsewhere */
3083 yyerror("could not parse host specification");
3088 | NUMBER '/' NUMBER {
3091 /* ie. for 10/8 parsing */
3093 if (asprintf(&buf, "%lld/%lld", (long long)$1, (long long)$3) == -1)
3095 if (asprintf(&buf, "%lld/%lld", $1, $3) == -1)
3097 err(1, "host: asprintf");
3098 if (($$ = host(buf)) == NULL) {
3099 /* error. "any" is handled elsewhere */
3101 yyerror("could not parse host specification");
3107 | dynaddr '/' NUMBER {
3108 struct node_host *n;
3110 if ($3 < 0 || $3 > 128) {
3111 yyerror("bit number too big");
3115 for (n = $1; n != NULL; n = n->next)
3119 if (strlen($2) >= PF_TABLE_NAME_SIZE) {
3120 yyerror("table name '%s' too long", $2);
3124 $$ = calloc(1, sizeof(struct node_host));
3126 err(1, "host: calloc");
3127 $$->addr.type = PF_ADDR_TABLE;
3128 if (strlcpy($$->addr.v.tblname, $2,
3129 sizeof($$->addr.v.tblname)) >=
3130 sizeof($$->addr.v.tblname))
3131 errx(1, "host: strlcpy");
3142 if (atoul($1, &ulval) == -1) {
3143 yyerror("%s is not a number", $1);
3152 dynaddr : '(' STRING ')' {
3157 if (!isalpha(op[0])) {
3158 yyerror("invalid interface name '%s'", op);
3162 while ((p = strrchr($2, ':')) != NULL) {
3163 if (!strcmp(p+1, "network"))
3164 flags |= PFI_AFLAG_NETWORK;
3165 else if (!strcmp(p+1, "broadcast"))
3166 flags |= PFI_AFLAG_BROADCAST;
3167 else if (!strcmp(p+1, "peer"))
3168 flags |= PFI_AFLAG_PEER;
3169 else if (!strcmp(p+1, "0"))
3170 flags |= PFI_AFLAG_NOALIAS;
3172 yyerror("interface %s has bad modifier",
3179 if (flags & (flags - 1) & PFI_AFLAG_MODEMASK) {
3181 yyerror("illegal combination of "
3182 "interface modifiers");
3185 $$ = calloc(1, sizeof(struct node_host));
3187 err(1, "address: calloc");
3189 set_ipmask($$, 128);
3190 $$->addr.type = PF_ADDR_DYNIFTL;
3191 $$->addr.iflags = flags;
3192 if (strlcpy($$->addr.v.ifname, $2,
3193 sizeof($$->addr.v.ifname)) >=
3194 sizeof($$->addr.v.ifname)) {
3197 yyerror("interface name too long");
3206 portspec : port_item { $$ = $1; }
3207 | '{' optnl port_list '}' { $$ = $3; }
3210 port_list : port_item optnl { $$ = $1; }
3211 | port_list comma port_item optnl {
3212 $1->tail->next = $3;
3218 port_item : portrange {
3219 $$ = calloc(1, sizeof(struct node_port));
3221 err(1, "port_item: calloc");
3231 | unaryop portrange {
3233 yyerror("':' cannot be used with an other "
3237 $$ = calloc(1, sizeof(struct node_port));
3239 err(1, "port_item: calloc");
3246 | portrange PORTBINARY portrange {
3248 yyerror("':' cannot be used with an other "
3252 $$ = calloc(1, sizeof(struct node_port));
3254 err(1, "port_item: calloc");
3263 portplain : numberstring {
3264 if (parseport($1, &$$, 0) == -1) {
3272 portrange : numberstring {
3273 if (parseport($1, &$$, PPORT_RANGE) == -1) {
3281 uids : uid_item { $$ = $1; }
3282 | '{' optnl uid_list '}' { $$ = $3; }
3285 uid_list : uid_item optnl { $$ = $1; }
3286 | uid_list comma uid_item optnl {
3287 $1->tail->next = $3;
3294 $$ = calloc(1, sizeof(struct node_uid));
3296 err(1, "uid_item: calloc");
3304 if ($2 == UID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
3305 yyerror("user unknown requires operator = or "
3309 $$ = calloc(1, sizeof(struct node_uid));
3311 err(1, "uid_item: calloc");
3318 | uid PORTBINARY uid {
3319 if ($1 == UID_MAX || $3 == UID_MAX) {
3320 yyerror("user unknown requires operator = or "
3324 $$ = calloc(1, sizeof(struct node_uid));
3326 err(1, "uid_item: calloc");
3336 if (!strcmp($1, "unknown"))
3341 if ((pw = getpwnam($1)) == NULL) {
3342 yyerror("unknown user %s", $1);
3351 if ($1 < 0 || $1 >= UID_MAX) {
3352 yyerror("illegal uid value %lu", $1);
3359 gids : gid_item { $$ = $1; }
3360 | '{' optnl gid_list '}' { $$ = $3; }
3363 gid_list : gid_item optnl { $$ = $1; }
3364 | gid_list comma gid_item optnl {
3365 $1->tail->next = $3;
3372 $$ = calloc(1, sizeof(struct node_gid));
3374 err(1, "gid_item: calloc");
3382 if ($2 == GID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
3383 yyerror("group unknown requires operator = or "
3387 $$ = calloc(1, sizeof(struct node_gid));
3389 err(1, "gid_item: calloc");
3396 | gid PORTBINARY gid {
3397 if ($1 == GID_MAX || $3 == GID_MAX) {
3398 yyerror("group unknown requires operator = or "
3402 $$ = calloc(1, sizeof(struct node_gid));
3404 err(1, "gid_item: calloc");
3414 if (!strcmp($1, "unknown"))
3419 if ((grp = getgrnam($1)) == NULL) {
3420 yyerror("unknown group %s", $1);
3429 if ($1 < 0 || $1 >= GID_MAX) {
3430 yyerror("illegal gid value %lu", $1);
3440 if ((f = parse_flags($1)) < 0) {
3441 yyerror("bad flags %s", $1);
3450 flags : FLAGS flag '/' flag { $$.b1 = $2.b1; $$.b2 = $4.b1; }
3451 | FLAGS '/' flag { $$.b1 = 0; $$.b2 = $3.b1; }
3452 | FLAGS ANY { $$.b1 = 0; $$.b2 = 0; }
3455 icmpspec : ICMPTYPE icmp_item { $$ = $2; }
3456 | ICMPTYPE '{' optnl icmp_list '}' { $$ = $4; }
3457 | ICMP6TYPE icmp6_item { $$ = $2; }
3458 | ICMP6TYPE '{' optnl icmp6_list '}' { $$ = $4; }
3461 icmp_list : icmp_item optnl { $$ = $1; }
3462 | icmp_list comma icmp_item optnl {
3463 $1->tail->next = $3;
3469 icmp6_list : icmp6_item optnl { $$ = $1; }
3470 | icmp6_list comma icmp6_item optnl {
3471 $1->tail->next = $3;
3477 icmp_item : icmptype {
3478 $$ = calloc(1, sizeof(struct node_icmp));
3480 err(1, "icmp_item: calloc");
3483 $$->proto = IPPROTO_ICMP;
3487 | icmptype CODE STRING {
3488 const struct icmpcodeent *p;
3490 if ((p = geticmpcodebyname($1-1, $3, AF_INET)) == NULL) {
3491 yyerror("unknown icmp-code %s", $3);
3497 $$ = calloc(1, sizeof(struct node_icmp));
3499 err(1, "icmp_item: calloc");
3501 $$->code = p->code + 1;
3502 $$->proto = IPPROTO_ICMP;
3506 | icmptype CODE NUMBER {
3507 if ($3 < 0 || $3 > 255) {
3508 yyerror("illegal icmp-code %lu", $3);
3511 $$ = calloc(1, sizeof(struct node_icmp));
3513 err(1, "icmp_item: calloc");
3516 $$->proto = IPPROTO_ICMP;
3522 icmp6_item : icmp6type {
3523 $$ = calloc(1, sizeof(struct node_icmp));
3525 err(1, "icmp_item: calloc");
3528 $$->proto = IPPROTO_ICMPV6;
3532 | icmp6type CODE STRING {
3533 const struct icmpcodeent *p;
3535 if ((p = geticmpcodebyname($1-1, $3, AF_INET6)) == NULL) {
3536 yyerror("unknown icmp6-code %s", $3);
3542 $$ = calloc(1, sizeof(struct node_icmp));
3544 err(1, "icmp_item: calloc");
3546 $$->code = p->code + 1;
3547 $$->proto = IPPROTO_ICMPV6;
3551 | icmp6type CODE NUMBER {
3552 if ($3 < 0 || $3 > 255) {
3553 yyerror("illegal icmp-code %lu", $3);
3556 $$ = calloc(1, sizeof(struct node_icmp));
3558 err(1, "icmp_item: calloc");
3561 $$->proto = IPPROTO_ICMPV6;
3568 const struct icmptypeent *p;
3570 if ((p = geticmptypebyname($1, AF_INET)) == NULL) {
3571 yyerror("unknown icmp-type %s", $1);
3579 if ($1 < 0 || $1 > 255) {
3580 yyerror("illegal icmp-type %lu", $1);
3587 icmp6type : STRING {
3588 const struct icmptypeent *p;
3590 if ((p = geticmptypebyname($1, AF_INET6)) ==
3592 yyerror("unknown icmp6-type %s", $1);
3600 if ($1 < 0 || $1 > 255) {
3601 yyerror("illegal icmp6-type %lu", $1);
3609 if (!strcmp($1, "lowdelay"))
3610 $$ = IPTOS_LOWDELAY;
3611 else if (!strcmp($1, "throughput"))
3612 $$ = IPTOS_THROUGHPUT;
3613 else if (!strcmp($1, "reliability"))
3614 $$ = IPTOS_RELIABILITY;
3615 else if ($1[0] == '0' && $1[1] == 'x')
3616 $$ = strtoul($1, NULL, 16);
3618 $$ = 256; /* flag bad argument */
3619 if ($$ < 0 || $$ > 255) {
3620 yyerror("illegal tos value %s", $1);
3628 if ($$ < 0 || $$ > 255) {
3629 yyerror("illegal tos value %s", $1);
3635 sourcetrack : SOURCETRACK { $$ = PF_SRCTRACK; }
3636 | SOURCETRACK GLOBAL { $$ = PF_SRCTRACK_GLOBAL; }
3637 | SOURCETRACK RULE { $$ = PF_SRCTRACK_RULE; }
3640 statelock : IFBOUND {
3641 $$ = PFRULE_IFBOUND;
3652 | KEEP STATE state_opt_spec {
3653 $$.action = PF_STATE_NORMAL;
3656 | MODULATE STATE state_opt_spec {
3657 $$.action = PF_STATE_MODULATE;
3660 | SYNPROXY STATE state_opt_spec {
3661 $$.action = PF_STATE_SYNPROXY;
3666 flush : /* empty */ { $$ = 0; }
3667 | FLUSH { $$ = PF_FLUSH; }
3669 $$ = PF_FLUSH | PF_FLUSH_GLOBAL;
3673 state_opt_spec : '(' state_opt_list ')' { $$ = $2; }
3674 | /* empty */ { $$ = NULL; }
3677 state_opt_list : state_opt_item { $$ = $1; }
3678 | state_opt_list comma state_opt_item {
3679 $1->tail->next = $3;
3685 state_opt_item : MAXIMUM NUMBER {
3686 if ($2 < 0 || $2 > UINT_MAX) {
3687 yyerror("only positive values permitted");
3690 $$ = calloc(1, sizeof(struct node_state_opt));
3692 err(1, "state_opt_item: calloc");
3693 $$->type = PF_STATE_OPT_MAX;
3694 $$->data.max_states = $2;
3699 $$ = calloc(1, sizeof(struct node_state_opt));
3701 err(1, "state_opt_item: calloc");
3702 $$->type = PF_STATE_OPT_NOSYNC;
3706 | MAXSRCSTATES NUMBER {
3707 if ($2 < 0 || $2 > UINT_MAX) {
3708 yyerror("only positive values permitted");
3711 $$ = calloc(1, sizeof(struct node_state_opt));
3713 err(1, "state_opt_item: calloc");
3714 $$->type = PF_STATE_OPT_MAX_SRC_STATES;
3715 $$->data.max_src_states = $2;
3719 | MAXSRCCONN NUMBER {
3720 if ($2 < 0 || $2 > UINT_MAX) {
3721 yyerror("only positive values permitted");
3724 $$ = calloc(1, sizeof(struct node_state_opt));
3726 err(1, "state_opt_item: calloc");
3727 $$->type = PF_STATE_OPT_MAX_SRC_CONN;
3728 $$->data.max_src_conn = $2;
3732 | MAXSRCCONNRATE NUMBER '/' NUMBER {
3733 if ($2 < 0 || $2 > UINT_MAX ||
3734 $4 < 0 || $4 > UINT_MAX) {
3735 yyerror("only positive values permitted");
3738 $$ = calloc(1, sizeof(struct node_state_opt));
3740 err(1, "state_opt_item: calloc");
3741 $$->type = PF_STATE_OPT_MAX_SRC_CONN_RATE;
3742 $$->data.max_src_conn_rate.limit = $2;
3743 $$->data.max_src_conn_rate.seconds = $4;
3747 | OVERLOAD '<' STRING '>' flush {
3748 if (strlen($3) >= PF_TABLE_NAME_SIZE) {
3749 yyerror("table name '%s' too long", $3);
3753 $$ = calloc(1, sizeof(struct node_state_opt));
3755 err(1, "state_opt_item: calloc");
3756 if (strlcpy($$->data.overload.tblname, $3,
3757 PF_TABLE_NAME_SIZE) >= PF_TABLE_NAME_SIZE)
3758 errx(1, "state_opt_item: strlcpy");
3760 $$->type = PF_STATE_OPT_OVERLOAD;
3761 $$->data.overload.flush = $5;
3765 | MAXSRCNODES NUMBER {
3766 if ($2 < 0 || $2 > UINT_MAX) {
3767 yyerror("only positive values permitted");
3770 $$ = calloc(1, sizeof(struct node_state_opt));
3772 err(1, "state_opt_item: calloc");
3773 $$->type = PF_STATE_OPT_MAX_SRC_NODES;
3774 $$->data.max_src_nodes = $2;
3779 $$ = calloc(1, sizeof(struct node_state_opt));
3781 err(1, "state_opt_item: calloc");
3782 $$->type = PF_STATE_OPT_SRCTRACK;
3783 $$->data.src_track = $1;
3788 $$ = calloc(1, sizeof(struct node_state_opt));
3790 err(1, "state_opt_item: calloc");
3791 $$->type = PF_STATE_OPT_STATELOCK;
3792 $$->data.statelock = $1;
3797 $$ = calloc(1, sizeof(struct node_state_opt));
3799 err(1, "state_opt_item: calloc");
3800 $$->type = PF_STATE_OPT_SLOPPY;
3807 if ($2 < 0 || $2 > UINT_MAX) {
3808 yyerror("only positive values permitted");
3811 for (i = 0; pf_timeouts[i].name &&
3812 strcmp(pf_timeouts[i].name, $1); ++i)
3814 if (!pf_timeouts[i].name) {
3815 yyerror("illegal timeout name %s", $1);
3819 if (strchr(pf_timeouts[i].name, '.') == NULL) {
3820 yyerror("illegal state timeout %s", $1);
3825 $$ = calloc(1, sizeof(struct node_state_opt));
3827 err(1, "state_opt_item: calloc");
3828 $$->type = PF_STATE_OPT_TIMEOUT;
3829 $$->data.timeout.number = pf_timeouts[i].timeout;
3830 $$->data.timeout.seconds = $2;
3836 label : LABEL STRING {
3841 qname : QUEUE STRING {
3845 | QUEUE '(' STRING ')' {
3849 | QUEUE '(' STRING comma STRING ')' {
3855 no : /* empty */ { $$ = 0; }
3859 portstar : numberstring {
3860 if (parseport($1, &$$, PPORT_RANGE|PPORT_STAR) == -1) {
3868 redirspec : host { $$ = $1; }
3869 | '{' optnl redir_host_list '}' { $$ = $3; }
3872 redir_host_list : host optnl { $$ = $1; }
3873 | redir_host_list comma host optnl {
3874 $1->tail->next = $3;
3875 $1->tail = $3->tail;
3880 redirpool : /* empty */ { $$ = NULL; }
3882 $$ = calloc(1, sizeof(struct redirection));
3884 err(1, "redirection: calloc");
3886 $$->rport.a = $$->rport.b = $$->rport.t = 0;
3888 | ARROW redirspec PORT portstar {
3889 $$ = calloc(1, sizeof(struct redirection));
3891 err(1, "redirection: calloc");
3897 hashkey : /* empty */
3899 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3901 err(1, "hashkey: calloc");
3902 $$->key32[0] = arc4random();
3903 $$->key32[1] = arc4random();
3904 $$->key32[2] = arc4random();
3905 $$->key32[3] = arc4random();
3909 if (!strncmp($1, "0x", 2)) {
3910 if (strlen($1) != 34) {
3912 yyerror("hex key must be 128 bits "
3913 "(32 hex digits) long");
3916 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3918 err(1, "hashkey: calloc");
3920 if (sscanf($1, "0x%8x%8x%8x%8x",
3921 &$$->key32[0], &$$->key32[1],
3922 &$$->key32[2], &$$->key32[3]) != 4) {
3925 yyerror("invalid hex key");
3931 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3933 err(1, "hashkey: calloc");
3935 MD5Update(&context, (unsigned char *)$1,
3937 MD5Final((unsigned char *)$$, &context);
3938 HTONL($$->key32[0]);
3939 HTONL($$->key32[1]);
3940 HTONL($$->key32[2]);
3941 HTONL($$->key32[3]);
3947 pool_opts : { bzero(&pool_opts, sizeof pool_opts); }
3951 bzero(&pool_opts, sizeof pool_opts);
3956 pool_opts_l : pool_opts_l pool_opt
3960 pool_opt : BITMASK {
3961 if (pool_opts.type) {
3962 yyerror("pool type cannot be redefined");
3965 pool_opts.type = PF_POOL_BITMASK;
3968 if (pool_opts.type) {
3969 yyerror("pool type cannot be redefined");
3972 pool_opts.type = PF_POOL_RANDOM;
3974 | SOURCEHASH hashkey {
3975 if (pool_opts.type) {
3976 yyerror("pool type cannot be redefined");
3979 pool_opts.type = PF_POOL_SRCHASH;
3983 if (pool_opts.type) {
3984 yyerror("pool type cannot be redefined");
3987 pool_opts.type = PF_POOL_ROUNDROBIN;
3990 if (pool_opts.staticport) {
3991 yyerror("static-port cannot be redefined");
3994 pool_opts.staticport = 1;
3997 if (filter_opts.marker & POM_STICKYADDRESS) {
3998 yyerror("sticky-address cannot be redefined");
4001 pool_opts.marker |= POM_STICKYADDRESS;
4002 pool_opts.opts |= PF_POOL_STICKYADDR;
4006 redirection : /* empty */ { $$ = NULL; }
4008 $$ = calloc(1, sizeof(struct redirection));
4010 err(1, "redirection: calloc");
4012 $$->rport.a = $$->rport.b = $$->rport.t = 0;
4014 | ARROW host PORT portstar {
4015 $$ = calloc(1, sizeof(struct redirection));
4017 err(1, "redirection: calloc");
4023 natpasslog : /* empty */ { $$.b1 = $$.b2 = 0; $$.w2 = 0; }
4024 | PASS { $$.b1 = 1; $$.b2 = 0; $$.w2 = 0; }
4025 | PASS log { $$.b1 = 1; $$.b2 = $2.log; $$.w2 = $2.logif; }
4026 | log { $$.b1 = 0; $$.b2 = $1.log; $$.w2 = $1.logif; }
4029 nataction : no NAT natpasslog {
4031 yyerror("\"pass\" not valid with \"no\"");
4042 | no RDR natpasslog {
4044 yyerror("\"pass\" not valid with \"no\"");
4057 natrule : nataction interface af proto fromto tag tagged rtable
4062 if (check_rulestate(PFCTL_STATE_NAT))
4065 memset(&r, 0, sizeof(r));
4074 if ($5.src.host && $5.src.host->af &&
4075 !$5.src.host->ifindex)
4076 r.af = $5.src.host->af;
4077 else if ($5.dst.host && $5.dst.host->af &&
4078 !$5.dst.host->ifindex)
4079 r.af = $5.dst.host->af;
4083 if (strlcpy(r.tagname, $6, PF_TAG_NAME_SIZE) >=
4085 yyerror("tag too long, max %u chars",
4086 PF_TAG_NAME_SIZE - 1);
4091 if (strlcpy(r.match_tagname, $7.name,
4092 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4093 yyerror("tag too long, max %u chars",
4094 PF_TAG_NAME_SIZE - 1);
4097 r.match_tag_not = $7.neg;
4100 if (r.action == PF_NONAT || r.action == PF_NORDR) {
4102 yyerror("translation rule with 'no' "
4103 "does not need '->'");
4107 if ($9 == NULL || $9->host == NULL) {
4108 yyerror("translation rule requires '-> "
4112 if (!r.af && ! $9->host->ifindex)
4113 r.af = $9->host->af;
4115 remove_invalid_hosts(&$9->host, &r.af);
4116 if (invalid_redirect($9->host, r.af))
4118 if (check_netmask($9->host, r.af))
4121 r.rpool.proxy_port[0] = ntohs($9->rport.a);
4125 if (!$9->rport.b && $9->rport.t &&
4126 $5.dst.port != NULL) {
4127 r.rpool.proxy_port[1] =
4128 ntohs($9->rport.a) +
4130 $5.dst.port->port[1]) -
4132 $5.dst.port->port[0]));
4134 r.rpool.proxy_port[1] =
4138 r.rpool.proxy_port[1] =
4140 if (!r.rpool.proxy_port[0] &&
4141 !r.rpool.proxy_port[1]) {
4142 r.rpool.proxy_port[0] =
4143 PF_NAT_PROXY_PORT_LOW;
4144 r.rpool.proxy_port[1] =
4145 PF_NAT_PROXY_PORT_HIGH;
4146 } else if (!r.rpool.proxy_port[1])
4147 r.rpool.proxy_port[1] =
4148 r.rpool.proxy_port[0];
4154 r.rpool.opts = $10.type;
4155 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
4156 PF_POOL_NONE && ($9->host->next != NULL ||
4157 $9->host->addr.type == PF_ADDR_TABLE ||
4158 DYNIF_MULTIADDR($9->host->addr)))
4159 r.rpool.opts = PF_POOL_ROUNDROBIN;
4160 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4161 PF_POOL_ROUNDROBIN &&
4162 disallow_table($9->host, "tables are only "
4163 "supported in round-robin redirection "
4166 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4167 PF_POOL_ROUNDROBIN &&
4168 disallow_alias($9->host, "interface (%s) "
4169 "is only supported in round-robin "
4170 "redirection pools"))
4172 if ($9->host->next != NULL) {
4173 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4174 PF_POOL_ROUNDROBIN) {
4175 yyerror("only round-robin "
4176 "valid for multiple "
4177 "redirection addresses");
4183 if ($10.key != NULL)
4184 memcpy(&r.rpool.key, $10.key,
4185 sizeof(struct pf_poolhashkey));
4188 r.rpool.opts |= $10.opts;
4190 if ($10.staticport) {
4191 if (r.action != PF_NAT) {
4192 yyerror("the 'static-port' option is "
4193 "only valid with nat rules");
4196 if (r.rpool.proxy_port[0] !=
4197 PF_NAT_PROXY_PORT_LOW &&
4198 r.rpool.proxy_port[1] !=
4199 PF_NAT_PROXY_PORT_HIGH) {
4200 yyerror("the 'static-port' option can't"
4201 " be used when specifying a port"
4205 r.rpool.proxy_port[0] = 0;
4206 r.rpool.proxy_port[1] = 0;
4209 expand_rule(&r, $2, $9 == NULL ? NULL : $9->host, $4,
4210 $5.src_os, $5.src.host, $5.src.port, $5.dst.host,
4211 $5.dst.port, 0, 0, 0, "");
4216 binatrule : no BINAT natpasslog interface af proto FROM ipspec toipspec tag
4217 tagged rtable redirection
4219 struct pf_rule binat;
4220 struct pf_pooladdr *pa;
4222 if (check_rulestate(PFCTL_STATE_NAT))
4224 if (disallow_urpf_failed($9, "\"urpf-failed\" is not "
4225 "permitted as a binat destination"))
4228 memset(&binat, 0, sizeof(binat));
4231 yyerror("\"pass\" not valid with \"no\"");
4235 binat.action = PF_NOBINAT;
4237 binat.action = PF_BINAT;
4238 binat.natpass = $3.b1;
4240 binat.logif = $3.w2;
4242 if (!binat.af && $8 != NULL && $8->af)
4244 if (!binat.af && $9 != NULL && $9->af)
4247 if (!binat.af && $13 != NULL && $13->host)
4248 binat.af = $13->host->af;
4250 yyerror("address family (inet/inet6) "
4256 memcpy(binat.ifname, $4->ifname,
4257 sizeof(binat.ifname));
4258 binat.ifnot = $4->not;
4263 if (strlcpy(binat.tagname, $10,
4264 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4265 yyerror("tag too long, max %u chars",
4266 PF_TAG_NAME_SIZE - 1);
4270 if (strlcpy(binat.match_tagname, $11.name,
4271 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4272 yyerror("tag too long, max %u chars",
4273 PF_TAG_NAME_SIZE - 1);
4276 binat.match_tag_not = $11.neg;
4277 binat.rtableid = $12;
4280 binat.proto = $6->proto;
4284 if ($8 != NULL && disallow_table($8, "invalid use of "
4285 "table <%s> as the source address of a binat rule"))
4287 if ($8 != NULL && disallow_alias($8, "invalid use of "
4288 "interface (%s) as the source address of a binat "
4291 if ($13 != NULL && $13->host != NULL && disallow_table(
4292 $13->host, "invalid use of table <%s> as the "
4293 "redirect address of a binat rule"))
4295 if ($13 != NULL && $13->host != NULL && disallow_alias(
4296 $13->host, "invalid use of interface (%s) as the "
4297 "redirect address of a binat rule"))
4302 yyerror("multiple binat ip addresses");
4305 if ($8->addr.type == PF_ADDR_DYNIFTL)
4307 if ($8->af != binat.af) {
4308 yyerror("binat ip versions must match");
4311 if (check_netmask($8, binat.af))
4313 memcpy(&binat.src.addr, &$8->addr,
4314 sizeof(binat.src.addr));
4319 yyerror("multiple binat ip addresses");
4322 if ($9->af != binat.af && $9->af) {
4323 yyerror("binat ip versions must match");
4326 if (check_netmask($9, binat.af))
4328 memcpy(&binat.dst.addr, &$9->addr,
4329 sizeof(binat.dst.addr));
4330 binat.dst.neg = $9->not;
4334 if (binat.action == PF_NOBINAT) {
4336 yyerror("'no binat' rule does not need"
4341 if ($13 == NULL || $13->host == NULL) {
4342 yyerror("'binat' rule requires"
4347 remove_invalid_hosts(&$13->host, &binat.af);
4348 if (invalid_redirect($13->host, binat.af))
4350 if ($13->host->next != NULL) {
4351 yyerror("binat rule must redirect to "
4352 "a single address");
4355 if (check_netmask($13->host, binat.af))
4358 if (!PF_AZERO(&binat.src.addr.v.a.mask,
4360 !PF_AEQ(&binat.src.addr.v.a.mask,
4361 &$13->host->addr.v.a.mask, binat.af)) {
4362 yyerror("'binat' source mask and "
4363 "redirect mask must be the same");
4367 TAILQ_INIT(&binat.rpool.list);
4368 pa = calloc(1, sizeof(struct pf_pooladdr));
4370 err(1, "binat: calloc");
4371 pa->addr = $13->host->addr;
4373 TAILQ_INSERT_TAIL(&binat.rpool.list,
4379 pfctl_add_rule(pf, &binat, "");
4383 tag : /* empty */ { $$ = NULL; }
4384 | TAG STRING { $$ = $2; }
4387 tagged : /* empty */ { $$.neg = 0; $$.name = NULL; }
4388 | not TAGGED string { $$.neg = $1; $$.name = $3; }
4391 rtable : /* empty */ { $$ = -1; }
4393 if ($2 < 0 || $2 > rt_tableid_max()) {
4394 yyerror("invalid rtable id");
4401 route_host : STRING {
4402 $$ = calloc(1, sizeof(struct node_host));
4404 err(1, "route_host: calloc");
4405 $$->ifname = strdup($1);
4406 set_ipmask($$, 128);
4410 | '(' STRING host ')' {
4411 struct node_host *n;
4414 for (n = $3; n != NULL; n = n->next)
4415 n->ifname = strdup($2);
4419 route_host_list : route_host optnl { $$ = $1; }
4420 | route_host_list comma route_host optnl {
4423 if ($1->af != $3->af) {
4424 yyerror("all pool addresses must be in the "
4425 "same address family");
4428 $1->tail->next = $3;
4429 $1->tail = $3->tail;
4434 routespec : route_host { $$ = $1; }
4435 | '{' optnl route_host_list '}' { $$ = $3; }
4438 route : /* empty */ {
4445 $$.rt = PF_FASTROUTE;
4448 | ROUTETO routespec pool_opts {
4451 $$.pool_opts = $3.type | $3.opts;
4455 | REPLYTO routespec pool_opts {
4458 $$.pool_opts = $3.type | $3.opts;
4462 | DUPTO routespec pool_opts {
4465 $$.pool_opts = $3.type | $3.opts;
4471 timeout_spec : STRING NUMBER
4473 if (check_rulestate(PFCTL_STATE_OPTION)) {
4477 if ($2 < 0 || $2 > UINT_MAX) {
4478 yyerror("only positive values permitted");
4481 if (pfctl_set_timeout(pf, $1, $2, 0) != 0) {
4482 yyerror("unknown timeout %s", $1);
4489 if (check_rulestate(PFCTL_STATE_OPTION))
4491 if ($2 < 0 || $2 > UINT_MAX) {
4492 yyerror("only positive values permitted");
4495 if (pfctl_set_timeout(pf, "interval", $2, 0) != 0)
4500 timeout_list : timeout_list comma timeout_spec optnl
4501 | timeout_spec optnl
4504 limit_spec : STRING NUMBER
4506 if (check_rulestate(PFCTL_STATE_OPTION)) {
4510 if ($2 < 0 || $2 > UINT_MAX) {
4511 yyerror("only positive values permitted");
4514 if (pfctl_set_limit(pf, $1, $2) != 0) {
4515 yyerror("unable to set limit %s %u", $1, $2);
4523 limit_list : limit_list comma limit_spec optnl
4531 yesno : NO { $$ = 0; }
4533 if (!strcmp($1, "yes"))
4536 yyerror("invalid value '%s', expected 'yes' "
4545 unaryop : '=' { $$ = PF_OP_EQ; }
4546 | '!' '=' { $$ = PF_OP_NE; }
4547 | '<' '=' { $$ = PF_OP_LE; }
4548 | '<' { $$ = PF_OP_LT; }
4549 | '>' '=' { $$ = PF_OP_GE; }
4550 | '>' { $$ = PF_OP_GT; }
4556 yyerror(const char *fmt, ...)
4562 fprintf(stderr, "%s:%d: ", file->name, yylval.lineno);
4563 vfprintf(stderr, fmt, ap);
4564 fprintf(stderr, "\n");
4570 disallow_table(struct node_host *h, const char *fmt)
4572 for (; h != NULL; h = h->next)
4573 if (h->addr.type == PF_ADDR_TABLE) {
4574 yyerror(fmt, h->addr.v.tblname);
4581 disallow_urpf_failed(struct node_host *h, const char *fmt)
4583 for (; h != NULL; h = h->next)
4584 if (h->addr.type == PF_ADDR_URPFFAILED) {
4592 disallow_alias(struct node_host *h, const char *fmt)
4594 for (; h != NULL; h = h->next)
4595 if (DYNIF_MULTIADDR(h->addr)) {
4596 yyerror(fmt, h->addr.v.tblname);
4603 rule_consistent(struct pf_rule *r, int anchor_call)
4607 switch (r->action) {
4612 problems = filter_consistent(r, anchor_call);
4616 problems = nat_consistent(r);
4620 problems = rdr_consistent(r);
4631 filter_consistent(struct pf_rule *r, int anchor_call)
4635 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP &&
4636 (r->src.port_op || r->dst.port_op)) {
4637 yyerror("port only applies to tcp/udp");
4640 if (r->proto != IPPROTO_ICMP && r->proto != IPPROTO_ICMPV6 &&
4641 (r->type || r->code)) {
4642 yyerror("icmp-type/code only applies to icmp");
4645 if (!r->af && (r->type || r->code)) {
4646 yyerror("must indicate address family with icmp-type/code");
4649 if (r->overload_tblname[0] &&
4650 r->max_src_conn == 0 && r->max_src_conn_rate.seconds == 0) {
4651 yyerror("'overload' requires 'max-src-conn' "
4652 "or 'max-src-conn-rate'");
4655 if ((r->proto == IPPROTO_ICMP && r->af == AF_INET6) ||
4656 (r->proto == IPPROTO_ICMPV6 && r->af == AF_INET)) {
4657 yyerror("proto %s doesn't match address family %s",
4658 r->proto == IPPROTO_ICMP ? "icmp" : "icmp6",
4659 r->af == AF_INET ? "inet" : "inet6");
4662 if (r->allow_opts && r->action != PF_PASS) {
4663 yyerror("allow-opts can only be specified for pass rules");
4666 if (r->rule_flag & PFRULE_FRAGMENT && (r->src.port_op ||
4667 r->dst.port_op || r->flagset || r->type || r->code)) {
4668 yyerror("fragments can be filtered only on IP header fields");
4671 if (r->rule_flag & PFRULE_RETURNRST && r->proto != IPPROTO_TCP) {
4672 yyerror("return-rst can only be applied to TCP rules");
4675 if (r->max_src_nodes && !(r->rule_flag & PFRULE_RULESRCTRACK)) {
4676 yyerror("max-src-nodes requires 'source-track rule'");
4679 if (r->action == PF_DROP && r->keep_state) {
4680 yyerror("keep state on block rules doesn't make sense");
4683 if (r->rule_flag & PFRULE_STATESLOPPY &&
4684 (r->keep_state == PF_STATE_MODULATE ||
4685 r->keep_state == PF_STATE_SYNPROXY)) {
4686 yyerror("sloppy state matching cannot be used with "
4687 "synproxy state or modulate state");
4694 nat_consistent(struct pf_rule *r)
4696 return (0); /* yeah! */
4700 rdr_consistent(struct pf_rule *r)
4704 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP) {
4705 if (r->src.port_op) {
4706 yyerror("src port only applies to tcp/udp");
4709 if (r->dst.port_op) {
4710 yyerror("dst port only applies to tcp/udp");
4713 if (r->rpool.proxy_port[0]) {
4714 yyerror("rpool port only applies to tcp/udp");
4718 if (r->dst.port_op &&
4719 r->dst.port_op != PF_OP_EQ && r->dst.port_op != PF_OP_RRG) {
4720 yyerror("invalid port operator for rdr destination port");
4727 process_tabledef(char *name, struct table_opts *opts)
4729 struct pfr_buffer ab;
4730 struct node_tinit *ti;
4732 bzero(&ab, sizeof(ab));
4733 ab.pfrb_type = PFRB_ADDRS;
4734 SIMPLEQ_FOREACH(ti, &opts->init_nodes, entries) {
4736 if (pfr_buf_load(&ab, ti->file, 0, append_addr)) {
4738 yyerror("cannot load \"%s\": %s",
4739 ti->file, strerror(errno));
4741 yyerror("file \"%s\" contains bad data",
4746 if (append_addr_host(&ab, ti->host, 0, 0)) {
4747 yyerror("cannot create address buffer: %s",
4752 if (pf->opts & PF_OPT_VERBOSE)
4753 print_tabledef(name, opts->flags, opts->init_addr,
4755 if (!(pf->opts & PF_OPT_NOACTION) &&
4756 pfctl_define_table(name, opts->flags, opts->init_addr,
4757 pf->anchor->name, &ab, pf->anchor->ruleset.tticket)) {
4758 yyerror("cannot define table %s: %s", name,
4759 pfr_strerror(errno));
4775 /* macro gore, but you should've seen the prior indentation nightmare... */
4777 #define FREE_LIST(T,r) \
4780 while (node != NULL) { \
4782 node = node->next; \
4787 #define LOOP_THROUGH(T,n,r,C) \
4791 r = calloc(1, sizeof(T)); \
4793 err(1, "LOOP: calloc"); \
4797 while (n != NULL) { \
4806 expand_label_str(char *label, size_t len, const char *srch, const char *repl)
4811 if ((tmp = calloc(1, len)) == NULL)
4812 err(1, "expand_label_str: calloc");
4814 while ((q = strstr(p, srch)) != NULL) {
4816 if ((strlcat(tmp, p, len) >= len) ||
4817 (strlcat(tmp, repl, len) >= len))
4818 errx(1, "expand_label: label too long");
4822 if (strlcat(tmp, p, len) >= len)
4823 errx(1, "expand_label: label too long");
4824 strlcpy(label, tmp, len); /* always fits */
4829 expand_label_if(const char *name, char *label, size_t len, const char *ifname)
4831 if (strstr(label, name) != NULL) {
4833 expand_label_str(label, len, name, "any");
4835 expand_label_str(label, len, name, ifname);
4840 expand_label_addr(const char *name, char *label, size_t len, sa_family_t af,
4841 struct node_host *h)
4843 char tmp[64], tmp_not[66];
4845 if (strstr(label, name) != NULL) {
4846 switch (h->addr.type) {
4847 case PF_ADDR_DYNIFTL:
4848 snprintf(tmp, sizeof(tmp), "(%s)", h->addr.v.ifname);
4851 snprintf(tmp, sizeof(tmp), "<%s>", h->addr.v.tblname);
4853 case PF_ADDR_NOROUTE:
4854 snprintf(tmp, sizeof(tmp), "no-route");
4856 case PF_ADDR_URPFFAILED:
4857 snprintf(tmp, sizeof(tmp), "urpf-failed");
4859 case PF_ADDR_ADDRMASK:
4860 if (!af || (PF_AZERO(&h->addr.v.a.addr, af) &&
4861 PF_AZERO(&h->addr.v.a.mask, af)))
4862 snprintf(tmp, sizeof(tmp), "any");
4867 if (inet_ntop(af, &h->addr.v.a.addr, a,
4869 snprintf(tmp, sizeof(tmp), "?");
4871 bits = unmask(&h->addr.v.a.mask, af);
4872 if ((af == AF_INET && bits < 32) ||
4873 (af == AF_INET6 && bits < 128))
4874 snprintf(tmp, sizeof(tmp),
4877 snprintf(tmp, sizeof(tmp),
4883 snprintf(tmp, sizeof(tmp), "?");
4888 snprintf(tmp_not, sizeof(tmp_not), "! %s", tmp);
4889 expand_label_str(label, len, name, tmp_not);
4891 expand_label_str(label, len, name, tmp);
4896 expand_label_port(const char *name, char *label, size_t len,
4897 struct node_port *port)
4899 char a1[6], a2[6], op[13] = "";
4901 if (strstr(label, name) != NULL) {
4902 snprintf(a1, sizeof(a1), "%u", ntohs(port->port[0]));
4903 snprintf(a2, sizeof(a2), "%u", ntohs(port->port[1]));
4906 else if (port->op == PF_OP_IRG)
4907 snprintf(op, sizeof(op), "%s><%s", a1, a2);
4908 else if (port->op == PF_OP_XRG)
4909 snprintf(op, sizeof(op), "%s<>%s", a1, a2);
4910 else if (port->op == PF_OP_EQ)
4911 snprintf(op, sizeof(op), "%s", a1);
4912 else if (port->op == PF_OP_NE)
4913 snprintf(op, sizeof(op), "!=%s", a1);
4914 else if (port->op == PF_OP_LT)
4915 snprintf(op, sizeof(op), "<%s", a1);
4916 else if (port->op == PF_OP_LE)
4917 snprintf(op, sizeof(op), "<=%s", a1);
4918 else if (port->op == PF_OP_GT)
4919 snprintf(op, sizeof(op), ">%s", a1);
4920 else if (port->op == PF_OP_GE)
4921 snprintf(op, sizeof(op), ">=%s", a1);
4922 expand_label_str(label, len, name, op);
4927 expand_label_proto(const char *name, char *label, size_t len, u_int8_t proto)
4929 struct protoent *pe;
4932 if (strstr(label, name) != NULL) {
4933 pe = getprotobynumber(proto);
4935 expand_label_str(label, len, name, pe->p_name);
4937 snprintf(n, sizeof(n), "%u", proto);
4938 expand_label_str(label, len, name, n);
4944 expand_label_nr(const char *name, char *label, size_t len)
4948 if (strstr(label, name) != NULL) {
4949 snprintf(n, sizeof(n), "%u", pf->anchor->match);
4950 expand_label_str(label, len, name, n);
4955 expand_label(char *label, size_t len, const char *ifname, sa_family_t af,
4956 struct node_host *src_host, struct node_port *src_port,
4957 struct node_host *dst_host, struct node_port *dst_port,
4960 expand_label_if("$if", label, len, ifname);
4961 expand_label_addr("$srcaddr", label, len, af, src_host);
4962 expand_label_addr("$dstaddr", label, len, af, dst_host);
4963 expand_label_port("$srcport", label, len, src_port);
4964 expand_label_port("$dstport", label, len, dst_port);
4965 expand_label_proto("$proto", label, len, proto);
4966 expand_label_nr("$nr", label, len);
4970 expand_altq(struct pf_altq *a, struct node_if *interfaces,
4971 struct node_queue *nqueues, struct node_queue_bw bwspec,
4972 struct node_queue_opt *opts)
4974 struct pf_altq pa, pb;
4975 char qname[PF_QNAME_SIZE];
4976 struct node_queue *n;
4977 struct node_queue_bw bw;
4980 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
4981 FREE_LIST(struct node_if, interfaces);
4983 FREE_LIST(struct node_queue, nqueues);
4987 LOOP_THROUGH(struct node_if, interface, interfaces,
4988 memcpy(&pa, a, sizeof(struct pf_altq));
4989 if (strlcpy(pa.ifname, interface->ifname,
4990 sizeof(pa.ifname)) >= sizeof(pa.ifname))
4991 errx(1, "expand_altq: strlcpy");
4993 if (interface->not) {
4994 yyerror("altq on ! <interface> is not supported");
4997 if (eval_pfaltq(pf, &pa, &bwspec, opts))
5000 if (pfctl_add_altq(pf, &pa))
5003 if (pf->opts & PF_OPT_VERBOSE) {
5004 print_altq(&pf->paltq->altq, 0,
5006 if (nqueues && nqueues->tail) {
5008 LOOP_THROUGH(struct node_queue, queue,
5018 if (pa.scheduler == ALTQT_CBQ ||
5019 pa.scheduler == ALTQT_HFSC) {
5020 /* now create a root queue */
5021 memset(&pb, 0, sizeof(struct pf_altq));
5022 if (strlcpy(qname, "root_", sizeof(qname)) >=
5024 errx(1, "expand_altq: strlcpy");
5025 if (strlcat(qname, interface->ifname,
5026 sizeof(qname)) >= sizeof(qname))
5027 errx(1, "expand_altq: strlcat");
5028 if (strlcpy(pb.qname, qname,
5029 sizeof(pb.qname)) >= sizeof(pb.qname))
5030 errx(1, "expand_altq: strlcpy");
5031 if (strlcpy(pb.ifname, interface->ifname,
5032 sizeof(pb.ifname)) >= sizeof(pb.ifname))
5033 errx(1, "expand_altq: strlcpy");
5034 pb.qlimit = pa.qlimit;
5035 pb.scheduler = pa.scheduler;
5036 bw.bw_absolute = pa.ifbandwidth;
5038 if (eval_pfqueue(pf, &pb, &bw, opts))
5041 if (pfctl_add_altq(pf, &pb))
5045 LOOP_THROUGH(struct node_queue, queue, nqueues,
5046 n = calloc(1, sizeof(struct node_queue));
5048 err(1, "expand_altq: calloc");
5049 if (pa.scheduler == ALTQT_CBQ ||
5050 pa.scheduler == ALTQT_HFSC)
5051 if (strlcpy(n->parent, qname,
5052 sizeof(n->parent)) >=
5054 errx(1, "expand_altq: strlcpy");
5055 if (strlcpy(n->queue, queue->queue,
5056 sizeof(n->queue)) >= sizeof(n->queue))
5057 errx(1, "expand_altq: strlcpy");
5058 if (strlcpy(n->ifname, interface->ifname,
5059 sizeof(n->ifname)) >= sizeof(n->ifname))
5060 errx(1, "expand_altq: strlcpy");
5061 n->scheduler = pa.scheduler;
5067 queues->tail->next = n;
5073 FREE_LIST(struct node_if, interfaces);
5075 FREE_LIST(struct node_queue, nqueues);
5081 expand_queue(struct pf_altq *a, struct node_if *interfaces,
5082 struct node_queue *nqueues, struct node_queue_bw bwspec,
5083 struct node_queue_opt *opts)
5085 struct node_queue *n, *nq;
5090 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
5091 FREE_LIST(struct node_queue, nqueues);
5095 if (queues == NULL) {
5096 yyerror("queue %s has no parent", a->qname);
5097 FREE_LIST(struct node_queue, nqueues);
5101 LOOP_THROUGH(struct node_if, interface, interfaces,
5102 LOOP_THROUGH(struct node_queue, tqueue, queues,
5103 if (!strncmp(a->qname, tqueue->queue, PF_QNAME_SIZE) &&
5104 (interface->ifname[0] == 0 ||
5105 (!interface->not && !strncmp(interface->ifname,
5106 tqueue->ifname, IFNAMSIZ)) ||
5107 (interface->not && strncmp(interface->ifname,
5108 tqueue->ifname, IFNAMSIZ)))) {
5109 /* found ourself in queues */
5112 memcpy(&pa, a, sizeof(struct pf_altq));
5114 if (pa.scheduler != ALTQT_NONE &&
5115 pa.scheduler != tqueue->scheduler) {
5116 yyerror("exactly one scheduler type "
5117 "per interface allowed");
5120 pa.scheduler = tqueue->scheduler;
5122 /* scheduler dependent error checking */
5123 switch (pa.scheduler) {
5125 if (nqueues != NULL) {
5126 yyerror("priq queues cannot "
5127 "have child queues");
5130 if (bwspec.bw_absolute > 0 ||
5131 bwspec.bw_percent < 100) {
5132 yyerror("priq doesn't take "
5141 if (strlcpy(pa.ifname, tqueue->ifname,
5142 sizeof(pa.ifname)) >= sizeof(pa.ifname))
5143 errx(1, "expand_queue: strlcpy");
5144 if (strlcpy(pa.parent, tqueue->parent,
5145 sizeof(pa.parent)) >= sizeof(pa.parent))
5146 errx(1, "expand_queue: strlcpy");
5148 if (eval_pfqueue(pf, &pa, &bwspec, opts))
5151 if (pfctl_add_altq(pf, &pa))
5154 for (nq = nqueues; nq != NULL; nq = nq->next) {
5155 if (!strcmp(a->qname, nq->queue)) {
5156 yyerror("queue cannot have "
5162 sizeof(struct node_queue));
5164 err(1, "expand_queue: calloc");
5165 if (strlcpy(n->parent, a->qname,
5166 sizeof(n->parent)) >=
5168 errx(1, "expand_queue strlcpy");
5169 if (strlcpy(n->queue, nq->queue,
5170 sizeof(n->queue)) >=
5172 errx(1, "expand_queue strlcpy");
5173 if (strlcpy(n->ifname, tqueue->ifname,
5174 sizeof(n->ifname)) >=
5176 errx(1, "expand_queue strlcpy");
5177 n->scheduler = tqueue->scheduler;
5183 queues->tail->next = n;
5187 if ((pf->opts & PF_OPT_VERBOSE) && (
5188 (found == 1 && interface->ifname[0] == 0) ||
5189 (found > 0 && interface->ifname[0] != 0))) {
5190 print_queue(&pf->paltq->altq, 0,
5191 &bwspec, interface->ifname[0] != 0,
5193 if (nqueues && nqueues->tail) {
5195 LOOP_THROUGH(struct node_queue,
5208 FREE_LIST(struct node_queue, nqueues);
5209 FREE_LIST(struct node_if, interfaces);
5212 yyerror("queue %s has no parent", a->qname);
5223 expand_rule(struct pf_rule *r,
5224 struct node_if *interfaces, struct node_host *rpool_hosts,
5225 struct node_proto *protos, struct node_os *src_oses,
5226 struct node_host *src_hosts, struct node_port *src_ports,
5227 struct node_host *dst_hosts, struct node_port *dst_ports,
5228 struct node_uid *uids, struct node_gid *gids, struct node_icmp *icmp_types,
5229 const char *anchor_call)
5231 sa_family_t af = r->af;
5232 int added = 0, error = 0;
5233 char ifname[IF_NAMESIZE];
5234 char label[PF_RULE_LABEL_SIZE];
5235 char tagname[PF_TAG_NAME_SIZE];
5236 char match_tagname[PF_TAG_NAME_SIZE];
5237 struct pf_pooladdr *pa;
5238 struct node_host *h;
5239 u_int8_t flags, flagset, keep_state;
5241 if (strlcpy(label, r->label, sizeof(label)) >= sizeof(label))
5242 errx(1, "expand_rule: strlcpy");
5243 if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname))
5244 errx(1, "expand_rule: strlcpy");
5245 if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >=
5246 sizeof(match_tagname))
5247 errx(1, "expand_rule: strlcpy");
5249 flagset = r->flagset;
5250 keep_state = r->keep_state;
5252 LOOP_THROUGH(struct node_if, interface, interfaces,
5253 LOOP_THROUGH(struct node_proto, proto, protos,
5254 LOOP_THROUGH(struct node_icmp, icmp_type, icmp_types,
5255 LOOP_THROUGH(struct node_host, src_host, src_hosts,
5256 LOOP_THROUGH(struct node_port, src_port, src_ports,
5257 LOOP_THROUGH(struct node_os, src_os, src_oses,
5258 LOOP_THROUGH(struct node_host, dst_host, dst_hosts,
5259 LOOP_THROUGH(struct node_port, dst_port, dst_ports,
5260 LOOP_THROUGH(struct node_uid, uid, uids,
5261 LOOP_THROUGH(struct node_gid, gid, gids,
5264 /* for link-local IPv6 address, interface must match up */
5265 if ((r->af && src_host->af && r->af != src_host->af) ||
5266 (r->af && dst_host->af && r->af != dst_host->af) ||
5267 (src_host->af && dst_host->af &&
5268 src_host->af != dst_host->af) ||
5269 (src_host->ifindex && dst_host->ifindex &&
5270 src_host->ifindex != dst_host->ifindex) ||
5271 (src_host->ifindex && *interface->ifname &&
5272 src_host->ifindex != if_nametoindex(interface->ifname)) ||
5273 (dst_host->ifindex && *interface->ifname &&
5274 dst_host->ifindex != if_nametoindex(interface->ifname)))
5276 if (!r->af && src_host->af)
5277 r->af = src_host->af;
5278 else if (!r->af && dst_host->af)
5279 r->af = dst_host->af;
5281 if (*interface->ifname)
5282 strlcpy(r->ifname, interface->ifname,
5284 else if (if_indextoname(src_host->ifindex, ifname))
5285 strlcpy(r->ifname, ifname, sizeof(r->ifname));
5286 else if (if_indextoname(dst_host->ifindex, ifname))
5287 strlcpy(r->ifname, ifname, sizeof(r->ifname));
5289 memset(r->ifname, '\0', sizeof(r->ifname));
5291 if (strlcpy(r->label, label, sizeof(r->label)) >=
5293 errx(1, "expand_rule: strlcpy");
5294 if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >=
5296 errx(1, "expand_rule: strlcpy");
5297 if (strlcpy(r->match_tagname, match_tagname,
5298 sizeof(r->match_tagname)) >= sizeof(r->match_tagname))
5299 errx(1, "expand_rule: strlcpy");
5300 expand_label(r->label, PF_RULE_LABEL_SIZE, r->ifname, r->af,
5301 src_host, src_port, dst_host, dst_port, proto->proto);
5302 expand_label(r->tagname, PF_TAG_NAME_SIZE, r->ifname, r->af,
5303 src_host, src_port, dst_host, dst_port, proto->proto);
5304 expand_label(r->match_tagname, PF_TAG_NAME_SIZE, r->ifname,
5305 r->af, src_host, src_port, dst_host, dst_port,
5308 error += check_netmask(src_host, r->af);
5309 error += check_netmask(dst_host, r->af);
5311 r->ifnot = interface->not;
5312 r->proto = proto->proto;
5313 r->src.addr = src_host->addr;
5314 r->src.neg = src_host->not;
5315 r->src.port[0] = src_port->port[0];
5316 r->src.port[1] = src_port->port[1];
5317 r->src.port_op = src_port->op;
5318 r->dst.addr = dst_host->addr;
5319 r->dst.neg = dst_host->not;
5320 r->dst.port[0] = dst_port->port[0];
5321 r->dst.port[1] = dst_port->port[1];
5322 r->dst.port_op = dst_port->op;
5323 r->uid.op = uid->op;
5324 r->uid.uid[0] = uid->uid[0];
5325 r->uid.uid[1] = uid->uid[1];
5326 r->gid.op = gid->op;
5327 r->gid.gid[0] = gid->gid[0];
5328 r->gid.gid[1] = gid->gid[1];
5329 r->type = icmp_type->type;
5330 r->code = icmp_type->code;
5332 if ((keep_state == PF_STATE_MODULATE ||
5333 keep_state == PF_STATE_SYNPROXY) &&
5334 r->proto && r->proto != IPPROTO_TCP)
5335 r->keep_state = PF_STATE_NORMAL;
5337 r->keep_state = keep_state;
5339 if (r->proto && r->proto != IPPROTO_TCP) {
5344 r->flagset = flagset;
5346 if (icmp_type->proto && r->proto != icmp_type->proto) {
5347 yyerror("icmp-type mismatch");
5351 if (src_os && src_os->os) {
5352 r->os_fingerprint = pfctl_get_fingerprint(src_os->os);
5353 if ((pf->opts & PF_OPT_VERBOSE2) &&
5354 r->os_fingerprint == PF_OSFP_NOMATCH)
5356 "warning: unknown '%s' OS fingerprint\n",
5359 r->os_fingerprint = PF_OSFP_ANY;
5362 TAILQ_INIT(&r->rpool.list);
5363 for (h = rpool_hosts; h != NULL; h = h->next) {
5364 pa = calloc(1, sizeof(struct pf_pooladdr));
5366 err(1, "expand_rule: calloc");
5368 if (h->ifname != NULL) {
5369 if (strlcpy(pa->ifname, h->ifname,
5370 sizeof(pa->ifname)) >=
5372 errx(1, "expand_rule: strlcpy");
5375 TAILQ_INSERT_TAIL(&r->rpool.list, pa, entries);
5378 if (rule_consistent(r, anchor_call[0]) < 0 || error)
5379 yyerror("skipping rule due to errors");
5381 r->nr = pf->astack[pf->asd]->match++;
5382 pfctl_add_rule(pf, r, anchor_call);
5388 FREE_LIST(struct node_if, interfaces);
5389 FREE_LIST(struct node_proto, protos);
5390 FREE_LIST(struct node_host, src_hosts);
5391 FREE_LIST(struct node_port, src_ports);
5392 FREE_LIST(struct node_os, src_oses);
5393 FREE_LIST(struct node_host, dst_hosts);
5394 FREE_LIST(struct node_port, dst_ports);
5395 FREE_LIST(struct node_uid, uids);
5396 FREE_LIST(struct node_gid, gids);
5397 FREE_LIST(struct node_icmp, icmp_types);
5398 FREE_LIST(struct node_host, rpool_hosts);
5401 yyerror("rule expands to no valid combination");
5405 expand_skip_interface(struct node_if *interfaces)
5409 if (!interfaces || (!interfaces->next && !interfaces->not &&
5410 !strcmp(interfaces->ifname, "none"))) {
5411 if (pf->opts & PF_OPT_VERBOSE)
5412 printf("set skip on none\n");
5413 errs = pfctl_set_interface_flags(pf, "", PFI_IFLAG_SKIP, 0);
5417 if (pf->opts & PF_OPT_VERBOSE)
5418 printf("set skip on {");
5419 LOOP_THROUGH(struct node_if, interface, interfaces,
5420 if (pf->opts & PF_OPT_VERBOSE)
5421 printf(" %s", interface->ifname);
5422 if (interface->not) {
5423 yyerror("skip on ! <interface> is not supported");
5426 errs += pfctl_set_interface_flags(pf,
5427 interface->ifname, PFI_IFLAG_SKIP, 1);
5429 if (pf->opts & PF_OPT_VERBOSE)
5432 FREE_LIST(struct node_if, interfaces);
5444 check_rulestate(int desired_state)
5446 if (require_order && (rulestate > desired_state)) {
5447 yyerror("Rules must be in order: options, normalization, "
5448 "queueing, translation, filtering");
5451 rulestate = desired_state;
5456 kw_cmp(const void *k, const void *e)
5458 return (strcmp(k, ((const struct keywords *)e)->k_name));
5464 /* this has to be sorted always */
5465 static const struct keywords keywords[] = {
5467 { "allow-opts", ALLOWOPTS},
5469 { "anchor", ANCHOR},
5470 { "antispoof", ANTISPOOF},
5472 { "bandwidth", BANDWIDTH},
5474 { "binat-anchor", BINATANCHOR},
5475 { "bitmask", BITMASK},
5477 { "block-policy", BLOCKPOLICY},
5478 { "buckets", BUCKETS},
5482 { "crop", FRAGCROP},
5484 { "divert-reply", DIVERTREPLY},
5485 { "divert-to", DIVERTTO},
5487 { "drop-ovl", FRAGDROP},
5489 { "fail-policy", FAILPOLICY},
5491 { "fastroute", FASTROUTE},
5492 { "file", FILENAME},
5493 { "fingerprints", FINGERPRINTS},
5495 { "floating", FLOATING},
5498 { "fragment", FRAGMENT},
5500 { "global", GLOBAL},
5504 { "hostid", HOSTID},
5505 { "icmp-type", ICMPTYPE},
5506 { "icmp6-type", ICMP6TYPE},
5507 { "if-bound", IFBOUND},
5509 { "include", INCLUDE},
5512 { "interval", INTERVAL},
5516 { "linkshare", LINKSHARE},
5519 { "loginterface", LOGINTERFACE},
5521 { "max-mss", MAXMSS},
5522 { "max-src-conn", MAXSRCCONN},
5523 { "max-src-conn-rate", MAXSRCCONNRATE},
5524 { "max-src-nodes", MAXSRCNODES},
5525 { "max-src-states", MAXSRCSTATES},
5526 { "min-ttl", MINTTL},
5527 { "modulate", MODULATE},
5529 { "nat-anchor", NATANCHOR},
5532 { "no-route", NOROUTE},
5533 { "no-sync", NOSYNC},
5535 { "optimization", OPTIMIZATION},
5538 { "overload", OVERLOAD},
5542 { "priority", PRIORITY},
5544 { "probability", PROBABILITY},
5546 { "qlimit", QLIMIT},
5549 { "random", RANDOM},
5550 { "random-id", RANDOMID},
5552 { "rdr-anchor", RDRANCHOR},
5553 { "realtime", REALTIME},
5554 { "reassemble", REASSEMBLE},
5555 { "reply-to", REPLYTO},
5556 { "require-order", REQUIREORDER},
5557 { "return", RETURN},
5558 { "return-icmp", RETURNICMP},
5559 { "return-icmp6", RETURNICMP6},
5560 { "return-rst", RETURNRST},
5561 { "round-robin", ROUNDROBIN},
5563 { "route-to", ROUTETO},
5564 { "rtable", RTABLE},
5566 { "ruleset-optimization", RULESET_OPTIMIZATION},
5569 { "set-tos", SETTOS},
5571 { "sloppy", SLOPPY},
5572 { "source-hash", SOURCEHASH},
5573 { "source-track", SOURCETRACK},
5575 { "state-defaults", STATEDEFAULTS},
5576 { "state-policy", STATEPOLICY},
5577 { "static-port", STATICPORT},
5578 { "sticky-address", STICKYADDRESS},
5579 { "synproxy", SYNPROXY},
5582 { "tagged", TAGGED},
5583 { "target", TARGET},
5584 { "tbrsize", TBRSIZE},
5585 { "timeout", TIMEOUT},
5589 { "upperlimit", UPPERLIMIT},
5590 { "urpf-failed", URPFFAILED},
5593 const struct keywords *p;
5595 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
5596 sizeof(keywords[0]), kw_cmp);
5600 fprintf(stderr, "%s: %d\n", s, p->k_val);
5604 fprintf(stderr, "string: %s\n", s);
5609 #define MAXPUSHBACK 128
5613 char pushback_buffer[MAXPUSHBACK];
5614 int pushback_index = 0;
5622 /* Read character from the parsebuffer instead of input. */
5623 if (parseindex >= 0) {
5624 c = parsebuf[parseindex++];
5633 return (pushback_buffer[--pushback_index]);
5636 if ((c = getc(file->stream)) == EOF) {
5637 yyerror("reached end of file while parsing quoted string");
5638 if (popfile() == EOF)
5645 while ((c = getc(file->stream)) == '\\') {
5646 next = getc(file->stream);
5651 yylval.lineno = file->lineno;
5656 if (popfile() == EOF)
5658 c = getc(file->stream);
5670 if (parseindex >= 0)
5673 if (pushback_index < MAXPUSHBACK-1)
5674 return (pushback_buffer[pushback_index++] = c);
5686 /* skip to either EOF or the first real EOL */
5689 c = pushback_buffer[--pushback_index];
5707 int quotec, next, c;
5712 while ((c = lgetc(0)) == ' ' || c == '\t')
5715 yylval.lineno = file->lineno;
5717 while ((c = lgetc(0)) != '\n' && c != EOF)
5719 if (c == '$' && parsebuf == NULL) {
5721 if ((c = lgetc(0)) == EOF)
5724 if (p + 1 >= buf + sizeof(buf) - 1) {
5725 yyerror("string too long");
5728 if (isalnum(c) || c == '_') {
5738 yyerror("macro '%s' not defined", buf);
5751 if ((c = lgetc(quotec)) == EOF)
5756 } else if (c == '\\') {
5757 if ((next = lgetc(quotec)) == EOF)
5759 if (next == quotec || c == ' ' || c == '\t')
5761 else if (next == '\n')
5765 } else if (c == quotec) {
5769 if (p + 1 >= buf + sizeof(buf) - 1) {
5770 yyerror("string too long");
5775 yylval.v.string = strdup(buf);
5776 if (yylval.v.string == NULL)
5777 err(1, "yylex: strdup");
5782 yylval.v.i = PF_OP_XRG;
5783 return (PORTBINARY);
5790 yylval.v.i = PF_OP_IRG;
5791 return (PORTBINARY);
5803 #define allowed_to_end_number(x) \
5804 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
5806 if (c == '-' || isdigit(c)) {
5809 if ((unsigned)(p-buf) >= sizeof(buf)) {
5810 yyerror("string too long");
5813 } while ((c = lgetc(0)) != EOF && isdigit(c));
5815 if (p == buf + 1 && buf[0] == '-')
5817 if (c == EOF || allowed_to_end_number(c)) {
5818 const char *errstr = NULL;
5821 yylval.v.number = strtonum(buf, LLONG_MIN,
5822 LLONG_MAX, &errstr);
5824 yyerror("\"%s\" invalid number: %s",
5839 #define allowed_in_string(x) \
5840 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
5841 x != '{' && x != '}' && x != '<' && x != '>' && \
5842 x != '!' && x != '=' && x != '/' && x != '#' && \
5845 if (isalnum(c) || c == ':' || c == '_') {
5848 if ((unsigned)(p-buf) >= sizeof(buf)) {
5849 yyerror("string too long");
5852 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
5855 if ((token = lookup(buf)) == STRING)
5856 if ((yylval.v.string = strdup(buf)) == NULL)
5857 err(1, "yylex: strdup");
5861 yylval.lineno = file->lineno;
5870 check_file_secrecy(int fd, const char *fname)
5874 if (fstat(fd, &st)) {
5875 warn("cannot stat %s", fname);
5878 if (st.st_uid != 0 && st.st_uid != getuid()) {
5879 warnx("%s: owner not root or current user", fname);
5882 if (st.st_mode & (S_IRWXG | S_IRWXO)) {
5883 warnx("%s: group/world readable/writeable", fname);
5890 pushfile(const char *name, int secret)
5894 if ((nfile = calloc(1, sizeof(struct file))) == NULL ||
5895 (nfile->name = strdup(name)) == NULL) {
5899 if (TAILQ_FIRST(&files) == NULL && strcmp(nfile->name, "-") == 0) {
5900 nfile->stream = stdin;
5902 if ((nfile->name = strdup("stdin")) == NULL) {
5907 } else if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
5908 warn("%s", nfile->name);
5912 } else if (secret &&
5913 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
5914 fclose(nfile->stream);
5920 TAILQ_INSERT_TAIL(&files, nfile, entry);
5929 if ((prev = TAILQ_PREV(file, files, entry)) != NULL) {
5930 prev->errors += file->errors;
5931 TAILQ_REMOVE(&files, file, entry);
5932 fclose(file->stream);
5942 parse_config(char *filename, struct pfctl *xpf)
5949 rulestate = PFCTL_STATE_NONE;
5950 returnicmpdefault = (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
5951 returnicmp6default =
5952 (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
5953 blockpolicy = PFRULE_DROP;
5954 failpolicy = PFRULE_DROP;
5957 if ((file = pushfile(filename, 0)) == NULL) {
5958 warn("cannot open the main config file!");
5963 errors = file->errors;
5966 /* Free macros and check which have not been used. */
5967 while ((sym = TAILQ_FIRST(&symhead))) {
5968 if ((pf->opts & PF_OPT_VERBOSE2) && !sym->used)
5969 fprintf(stderr, "warning: macro '%s' not "
5970 "used\n", sym->nam);
5973 TAILQ_REMOVE(&symhead, sym, entry);
5977 return (errors ? -1 : 0);
5981 symset(const char *nam, const char *val, int persist)
5985 for (sym = TAILQ_FIRST(&symhead); sym && strcmp(nam, sym->nam);
5986 sym = TAILQ_NEXT(sym, entry))
5990 if (sym->persist == 1)
5995 TAILQ_REMOVE(&symhead, sym, entry);
5999 if ((sym = calloc(1, sizeof(*sym))) == NULL)
6002 sym->nam = strdup(nam);
6003 if (sym->nam == NULL) {
6007 sym->val = strdup(val);
6008 if (sym->val == NULL) {
6014 sym->persist = persist;
6015 TAILQ_INSERT_TAIL(&symhead, sym, entry);
6020 pfctl_cmdline_symset(char *s)
6025 if ((val = strrchr(s, '=')) == NULL)
6028 if ((sym = malloc(strlen(s) - strlen(val) + 1)) == NULL)
6029 err(1, "pfctl_cmdline_symset: malloc");
6031 strlcpy(sym, s, strlen(s) - strlen(val) + 1);
6033 ret = symset(sym, val + 1, 1);
6040 symget(const char *nam)
6044 TAILQ_FOREACH(sym, &symhead, entry)
6045 if (strcmp(nam, sym->nam) == 0) {
6053 mv_rules(struct pf_ruleset *src, struct pf_ruleset *dst)
6058 for (i = 0; i < PF_RULESET_MAX; ++i) {
6059 while ((r = TAILQ_FIRST(src->rules[i].active.ptr))
6061 TAILQ_REMOVE(src->rules[i].active.ptr, r, entries);
6062 TAILQ_INSERT_TAIL(dst->rules[i].active.ptr, r, entries);
6063 dst->anchor->match++;
6065 src->anchor->match = 0;
6066 while ((r = TAILQ_FIRST(src->rules[i].inactive.ptr))
6068 TAILQ_REMOVE(src->rules[i].inactive.ptr, r, entries);
6069 TAILQ_INSERT_TAIL(dst->rules[i].inactive.ptr,
6076 decide_address_family(struct node_host *n, sa_family_t *af)
6078 if (*af != 0 || n == NULL)
6081 while ((n = n->next) != NULL) {
6090 remove_invalid_hosts(struct node_host **nh, sa_family_t *af)
6092 struct node_host *n = *nh, *prev = NULL;
6095 if (*af && n->af && n->af != *af) {
6096 /* unlink and free n */
6097 struct node_host *next = n->next;
6099 /* adjust tail pointer */
6100 if (n == (*nh)->tail)
6102 /* adjust previous node's next pointer */
6108 if (n->ifname != NULL)
6122 invalid_redirect(struct node_host *nh, sa_family_t af)
6125 struct node_host *n;
6127 /* tables and dyniftl are ok without an address family */
6128 for (n = nh; n != NULL; n = n->next) {
6129 if (n->addr.type != PF_ADDR_TABLE &&
6130 n->addr.type != PF_ADDR_DYNIFTL) {
6131 yyerror("address family not given and "
6132 "translation address expands to multiple "
6133 "address families");
6139 yyerror("no translation address with matching address family "
6147 atoul(char *s, u_long *ulvalp)
6153 ulval = strtoul(s, &ep, 0);
6154 if (s[0] == '\0' || *ep != '\0')
6156 if (errno == ERANGE && ulval == ULONG_MAX)
6168 if (atoul(n, &ulval) == 0) {
6169 if (ulval > 65535) {
6170 yyerror("illegal port value %lu", ulval);
6173 return (htons(ulval));
6175 s = getservbyname(n, "tcp");
6177 s = getservbyname(n, "udp");
6179 yyerror("unknown port %s", n);
6187 rule_label(struct pf_rule *r, char *s)
6190 if (strlcpy(r->label, s, sizeof(r->label)) >=
6192 yyerror("rule label too long (max %d chars)",
6193 sizeof(r->label)-1);
6201 parseicmpspec(char *w, sa_family_t af)
6203 const struct icmpcodeent *p;
6208 icmptype = returnicmpdefault >> 8;
6210 icmptype = returnicmp6default >> 8;
6212 if (atoul(w, &ulval) == -1) {
6213 if ((p = geticmpcodebyname(icmptype, w, af)) == NULL) {
6214 yyerror("unknown icmp code %s", w);
6220 yyerror("invalid icmp code %lu", ulval);
6223 return (icmptype << 8 | ulval);
6227 parseport(char *port, struct range *r, int extensions)
6229 char *p = strchr(port, ':');
6232 if ((r->a = getservice(port)) == -1)
6238 if ((extensions & PPORT_STAR) && !strcmp(p+1, "*")) {
6240 if ((r->a = getservice(port)) == -1)
6246 if ((extensions & PPORT_RANGE)) {
6248 if ((r->a = getservice(port)) == -1 ||
6249 (r->b = getservice(p)) == -1)
6262 pfctl_load_anchors(int dev, struct pfctl *pf, struct pfr_buffer *trans)
6264 struct loadanchors *la;
6266 TAILQ_FOREACH(la, &loadanchorshead, entries) {
6267 if (pf->opts & PF_OPT_VERBOSE)
6268 fprintf(stderr, "\nLoading anchor %s from %s\n",
6269 la->anchorname, la->filename);
6270 if (pfctl_rules(dev, la->filename, pf->opts, pf->optimize,
6271 la->anchorname, trans) == -1)
6279 rt_tableid_max(void)
6283 size_t l = sizeof(fibs);
6285 if (sysctlbyname("net.fibs", &fibs, &l, NULL, 0) == -1)
6286 fibs = 16; /* XXX RT_MAXFIBS, at least limit it some. */
6288 * As the OpenBSD code only compares > and not >= we need to adjust
6289 * here given we only accept values of 0..n and want to avoid #ifdefs
6294 return (RT_TABLEID_MAX);