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 static 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 static 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;
202 static struct node_queue {
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 {
216 static struct filter_opts {
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 static struct antispoof_opts {
261 static struct scrub_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;
279 static struct queue_opts {
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;
293 static struct table_opts {
296 struct node_tinithead init_nodes;
299 static struct pool_opts {
301 #define POM_TYPE 0x01
302 #define POM_STICKYADDRESS 0x02
306 struct pf_poolhashkey *key;
310 static struct codel_opts codel_opts;
311 static struct node_hfsc_opts hfsc_opts;
312 static struct node_fairq_opts fairq_opts;
313 static 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);
357 int kw_casecmp(const void *, const void *);
358 int map_tos(char *string, int *);
360 static TAILQ_HEAD(loadanchorshead, loadanchors)
361 loadanchorshead = TAILQ_HEAD_INITIALIZER(loadanchorshead);
364 TAILQ_ENTRY(loadanchors) entries;
387 struct node_if *interface;
388 struct node_proto *proto;
389 struct node_icmp *icmp;
390 struct node_host *host;
392 struct node_port *port;
393 struct node_uid *uid;
394 struct node_gid *gid;
395 struct node_state_opt *state_opt;
398 struct peer src, dst;
399 struct node_os *src_os;
402 struct node_host *host;
406 struct pf_poolhashkey *key;
409 struct node_host *host;
414 struct node_state_opt *options;
425 struct pf_poolhashkey *hashkey;
426 struct node_queue *queue;
427 struct node_queue_opt queue_options;
428 struct node_queue_bw queue_bwspec;
429 struct node_qassign qassign;
430 struct filter_opts filter_opts;
431 struct antispoof_opts antispoof_opts;
432 struct queue_opts queue_opts;
433 struct scrub_opts scrub_opts;
434 struct table_opts table_opts;
435 struct pool_opts pool_opts;
436 struct node_hfsc_opts hfsc_opts;
437 struct node_fairq_opts fairq_opts;
438 struct codel_opts codel_opts;
443 #define PPORT_RANGE 1
445 int parseport(char *, struct range *r, int);
447 #define DYNIF_MULTIADDR(addr) ((addr).type == PF_ADDR_DYNIFTL && \
448 (!((addr).iflags & PFI_AFLAG_NOALIAS) || \
449 !isdigit((addr).v.ifname[strlen((addr).v.ifname)-1])))
453 %token PASS BLOCK SCRUB RETURN IN OS OUT LOG QUICK ON FROM TO FLAGS
454 %token RETURNRST RETURNICMP RETURNICMP6 PROTO INET INET6 ALL ANY ICMPTYPE
455 %token ICMP6TYPE CODE KEEP MODULATE STATE PORT RDR NAT BINAT ARROW NODF
456 %token MINTTL ERROR ALLOWOPTS FASTROUTE FILENAME ROUTETO DUPTO REPLYTO NO LABEL
457 %token NOROUTE URPFFAILED FRAGMENT USER GROUP MAXMSS MAXIMUM TTL TOS DROP TABLE
458 %token REASSEMBLE FRAGDROP FRAGCROP ANCHOR NATANCHOR RDRANCHOR BINATANCHOR
459 %token SET OPTIMIZATION TIMEOUT LIMIT LOGINTERFACE BLOCKPOLICY FAILPOLICY
460 %token RANDOMID REQUIREORDER SYNPROXY FINGERPRINTS NOSYNC DEBUG SKIP HOSTID
461 %token ANTISPOOF FOR INCLUDE
462 %token BITMASK RANDOM SOURCEHASH ROUNDROBIN STATICPORT PROBABILITY
463 %token ALTQ CBQ CODEL PRIQ HFSC FAIRQ BANDWIDTH TBRSIZE LINKSHARE REALTIME
464 %token UPPERLIMIT QUEUE PRIORITY QLIMIT HOGS BUCKETS RTABLE TARGET INTERVAL
465 %token LOAD RULESET_OPTIMIZATION PRIO
466 %token STICKYADDRESS MAXSRCSTATES MAXSRCNODES SOURCETRACK GLOBAL RULE
467 %token MAXSRCCONN MAXSRCCONNRATE OVERLOAD FLUSH SLOPPY
468 %token TAGGED TAG IFBOUND FLOATING STATEPOLICY STATEDEFAULTS ROUTE SETTOS
469 %token DIVERTTO DIVERTREPLY
470 %token <v.string> STRING
471 %token <v.number> NUMBER
472 %token <v.i> PORTBINARY
473 %type <v.interface> interface if_list if_item_not if_item
474 %type <v.number> number icmptype icmp6type uid gid
475 %type <v.number> tos not yesno
476 %type <v.probability> probability
477 %type <v.i> no dir af fragcache optimizer
478 %type <v.i> sourcetrack flush unaryop statelock
479 %type <v.b> action nataction natpasslog scrubaction
480 %type <v.b> flags flag blockspec prio
481 %type <v.range> portplain portstar portrange
482 %type <v.hashkey> hashkey
483 %type <v.proto> proto proto_list proto_item
484 %type <v.number> protoval
485 %type <v.icmp> icmpspec
486 %type <v.icmp> icmp_list icmp_item
487 %type <v.icmp> icmp6_list icmp6_item
488 %type <v.number> reticmpspec reticmp6spec
489 %type <v.fromto> fromto
490 %type <v.peer> ipportspec from to
491 %type <v.host> ipspec toipspec xhost host dynaddr host_list
492 %type <v.host> redir_host_list redirspec
493 %type <v.host> route_host route_host_list routespec
494 %type <v.os> os xos os_list
495 %type <v.port> portspec port_list port_item
496 %type <v.uid> uids uid_list uid_item
497 %type <v.gid> gids gid_list gid_item
498 %type <v.route> route
499 %type <v.redirection> redirection redirpool
500 %type <v.string> label stringall tag anchorname
501 %type <v.string> string varstring numberstring
502 %type <v.keep_state> keep
503 %type <v.state_opt> state_opt_spec state_opt_list state_opt_item
504 %type <v.logquick> logquick quick log logopts logopt
505 %type <v.interface> antispoof_ifspc antispoof_iflst antispoof_if
506 %type <v.qassign> qname
507 %type <v.queue> qassign qassign_list qassign_item
508 %type <v.queue_options> scheduler
509 %type <v.number> cbqflags_list cbqflags_item
510 %type <v.number> priqflags_list priqflags_item
511 %type <v.hfsc_opts> hfscopts_list hfscopts_item hfsc_opts
512 %type <v.fairq_opts> fairqopts_list fairqopts_item fairq_opts
513 %type <v.codel_opts> codelopts_list codelopts_item codel_opts
514 %type <v.queue_bwspec> bandwidth
515 %type <v.filter_opts> filter_opts filter_opt filter_opts_l
516 %type <v.filter_opts> filter_sets filter_set filter_sets_l
517 %type <v.antispoof_opts> antispoof_opts antispoof_opt antispoof_opts_l
518 %type <v.queue_opts> queue_opts queue_opt queue_opts_l
519 %type <v.scrub_opts> scrub_opts scrub_opt scrub_opts_l
520 %type <v.table_opts> table_opts table_opt table_opts_l
521 %type <v.pool_opts> pool_opts pool_opt pool_opts_l
522 %type <v.tagged> tagged
523 %type <v.rtableid> rtable
526 ruleset : /* empty */
527 | ruleset include '\n'
529 | ruleset option '\n'
530 | ruleset scrubrule '\n'
531 | ruleset natrule '\n'
532 | ruleset binatrule '\n'
533 | ruleset pfrule '\n'
534 | ruleset anchorrule '\n'
535 | ruleset loadrule '\n'
536 | ruleset altqif '\n'
537 | ruleset queuespec '\n'
538 | ruleset varset '\n'
539 | ruleset antispoof '\n'
540 | ruleset tabledef '\n'
541 | '{' fakeanchor '}' '\n';
542 | ruleset error '\n' { file->errors++; }
545 include : INCLUDE STRING {
548 if ((nfile = pushfile($2, 0)) == NULL) {
549 yyerror("failed to include file %s", $2);
561 * apply to previouslys specified rule: must be careful to note
562 * what that is: pf or nat or binat or rdr
564 fakeanchor : fakeanchor '\n'
565 | fakeanchor anchorrule '\n'
566 | fakeanchor binatrule '\n'
567 | fakeanchor natrule '\n'
568 | fakeanchor pfrule '\n'
569 | fakeanchor error '\n'
573 if (!strcmp($1, "none"))
575 else if (!strcmp($1, "basic"))
576 $$ = PF_OPTIMIZE_BASIC;
577 else if (!strcmp($1, "profile"))
578 $$ = PF_OPTIMIZE_BASIC | PF_OPTIMIZE_PROFILE;
580 yyerror("unknown ruleset-optimization %s", $1);
586 option : SET OPTIMIZATION STRING {
587 if (check_rulestate(PFCTL_STATE_OPTION)) {
591 if (pfctl_set_optimization(pf, $3) != 0) {
592 yyerror("unknown optimization %s", $3);
598 | SET RULESET_OPTIMIZATION optimizer {
599 if (!(pf->opts & PF_OPT_OPTIMIZE)) {
600 pf->opts |= PF_OPT_OPTIMIZE;
604 | SET TIMEOUT timeout_spec
605 | SET TIMEOUT '{' optnl timeout_list '}'
606 | SET LIMIT limit_spec
607 | SET LIMIT '{' optnl limit_list '}'
608 | SET LOGINTERFACE stringall {
609 if (check_rulestate(PFCTL_STATE_OPTION)) {
613 if (pfctl_set_logif(pf, $3) != 0) {
614 yyerror("error setting loginterface %s", $3);
620 | SET HOSTID number {
621 if ($3 == 0 || $3 > UINT_MAX) {
622 yyerror("hostid must be non-zero");
625 if (pfctl_set_hostid(pf, $3) != 0) {
626 yyerror("error setting hostid %08x", $3);
630 | SET BLOCKPOLICY DROP {
631 if (pf->opts & PF_OPT_VERBOSE)
632 printf("set block-policy drop\n");
633 if (check_rulestate(PFCTL_STATE_OPTION))
635 blockpolicy = PFRULE_DROP;
637 | SET BLOCKPOLICY RETURN {
638 if (pf->opts & PF_OPT_VERBOSE)
639 printf("set block-policy return\n");
640 if (check_rulestate(PFCTL_STATE_OPTION))
642 blockpolicy = PFRULE_RETURN;
644 | SET FAILPOLICY DROP {
645 if (pf->opts & PF_OPT_VERBOSE)
646 printf("set fail-policy drop\n");
647 if (check_rulestate(PFCTL_STATE_OPTION))
649 failpolicy = PFRULE_DROP;
651 | SET FAILPOLICY RETURN {
652 if (pf->opts & PF_OPT_VERBOSE)
653 printf("set fail-policy return\n");
654 if (check_rulestate(PFCTL_STATE_OPTION))
656 failpolicy = PFRULE_RETURN;
658 | SET REQUIREORDER yesno {
659 if (pf->opts & PF_OPT_VERBOSE)
660 printf("set require-order %s\n",
661 $3 == 1 ? "yes" : "no");
664 | SET FINGERPRINTS STRING {
665 if (pf->opts & PF_OPT_VERBOSE)
666 printf("set fingerprints \"%s\"\n", $3);
667 if (check_rulestate(PFCTL_STATE_OPTION)) {
671 if (!pf->anchor->name[0]) {
672 if (pfctl_file_fingerprints(pf->dev,
674 yyerror("error loading "
675 "fingerprints %s", $3);
682 | SET STATEPOLICY statelock {
683 if (pf->opts & PF_OPT_VERBOSE)
686 printf("set state-policy floating\n");
689 printf("set state-policy if-bound\n");
692 default_statelock = $3;
695 if (check_rulestate(PFCTL_STATE_OPTION)) {
699 if (pfctl_set_debug(pf, $3) != 0) {
700 yyerror("error setting debuglevel %s", $3);
706 | SET SKIP interface {
707 if (expand_skip_interface($3) != 0) {
708 yyerror("error setting skip interface(s)");
712 | SET STATEDEFAULTS state_opt_list {
713 if (keep_state_defaults != NULL) {
714 yyerror("cannot redefine state-defaults");
717 keep_state_defaults = $3;
721 stringall : STRING { $$ = $1; }
723 if (($$ = strdup("all")) == NULL) {
724 err(1, "stringall: strdup");
729 string : STRING string {
730 if (asprintf(&$$, "%s %s", $1, $2) == -1)
731 err(1, "string: asprintf");
738 varstring : numberstring varstring {
739 if (asprintf(&$$, "%s %s", $1, $2) == -1)
740 err(1, "string: asprintf");
747 numberstring : NUMBER {
749 if (asprintf(&s, "%lld", (long long)$1) == -1) {
750 yyerror("string: asprintf");
758 varset : STRING '=' varstring {
759 if (pf->opts & PF_OPT_VERBOSE)
760 printf("%s = \"%s\"\n", $1, $3);
761 if (symset($1, $3, 0) == -1)
762 err(1, "cannot store variable %s", $1);
768 anchorname : STRING { $$ = $1; }
769 | /* empty */ { $$ = NULL; }
772 pfa_anchorlist : /* empty */
773 | pfa_anchorlist '\n'
774 | pfa_anchorlist pfrule '\n'
775 | pfa_anchorlist anchorrule '\n'
780 char ta[PF_ANCHOR_NAME_SIZE];
781 struct pf_ruleset *rs;
783 /* steping into a brace anchor */
788 /* create a holding ruleset in the root */
789 snprintf(ta, PF_ANCHOR_NAME_SIZE, "_%d", pf->bn);
790 rs = pf_find_or_create_ruleset(ta);
792 err(1, "pfa_anchor: pf_find_or_create_ruleset");
793 pf->astack[pf->asd] = rs->anchor;
794 pf->anchor = rs->anchor;
795 } '\n' pfa_anchorlist '}'
797 pf->alast = pf->anchor;
799 pf->anchor = pf->astack[pf->asd];
804 anchorrule : ANCHOR anchorname dir quick interface af proto fromto
805 filter_opts pfa_anchor
808 struct node_proto *proto;
810 if (check_rulestate(PFCTL_STATE_FILTER)) {
816 if ($2 && ($2[0] == '_' || strstr($2, "/_") != NULL)) {
818 yyerror("anchor names beginning with '_' "
819 "are reserved for internal use");
823 memset(&r, 0, sizeof(r));
824 if (pf->astack[pf->asd + 1]) {
825 /* move inline rules into relative location */
827 &pf->astack[pf->asd]->ruleset,
828 $2 ? $2 : pf->alast->name);
830 if (r.anchor == NULL)
831 err(1, "anchorrule: unable to "
834 if (pf->alast != r.anchor) {
835 if (r.anchor->match) {
836 yyerror("inline anchor '%s' "
841 mv_rules(&pf->alast->ruleset,
844 pf_remove_if_empty_ruleset(&pf->alast->ruleset);
845 pf->alast = r.anchor;
848 yyerror("anchors without explicit "
849 "rules must specify a name");
857 r.rtableid = $9.rtableid;
860 if (strlcpy(r.tagname, $9.tag,
861 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
862 yyerror("tag too long, max %u chars",
863 PF_TAG_NAME_SIZE - 1);
867 if (strlcpy(r.match_tagname, $9.match_tag,
868 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
869 yyerror("tag too long, max %u chars",
870 PF_TAG_NAME_SIZE - 1);
873 r.match_tag_not = $9.match_tag_not;
874 if (rule_label(&r, $9.label))
877 r.flags = $9.flags.b1;
878 r.flagset = $9.flags.b2;
879 if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
880 yyerror("flags always false");
883 if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
884 for (proto = $7; proto != NULL &&
885 proto->proto != IPPROTO_TCP;
888 if (proto == NULL && $7 != NULL) {
889 if ($9.flags.b1 || $9.flags.b2)
891 "flags only apply to tcp");
894 "OS fingerprinting only "
902 if ($9.keep.action) {
903 yyerror("cannot specify state handling "
909 if (strlcpy(r.match_tagname, $9.match_tag,
910 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
911 yyerror("tag too long, max %u chars",
912 PF_TAG_NAME_SIZE - 1);
915 r.match_tag_not = $9.match_tag_not;
916 if ($9.marker & FOM_PRIO) {
918 r.prio = PF_PRIO_ZERO;
922 if ($9.marker & FOM_SETPRIO) {
923 r.set_prio[0] = $9.set_prio[0];
924 r.set_prio[1] = $9.set_prio[1];
925 r.scrub_flags |= PFSTATE_SETPRIO;
928 decide_address_family($8.src.host, &r.af);
929 decide_address_family($8.dst.host, &r.af);
931 expand_rule(&r, $5, NULL, $7, $8.src_os,
932 $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
933 $9.uid, $9.gid, $9.icmpspec,
934 pf->astack[pf->asd + 1] ? pf->alast->name : $2);
936 pf->astack[pf->asd + 1] = NULL;
938 | NATANCHOR string interface af proto fromto rtable {
941 if (check_rulestate(PFCTL_STATE_NAT)) {
946 memset(&r, 0, sizeof(r));
951 decide_address_family($6.src.host, &r.af);
952 decide_address_family($6.dst.host, &r.af);
954 expand_rule(&r, $3, NULL, $5, $6.src_os,
955 $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
959 | RDRANCHOR string interface af proto fromto rtable {
962 if (check_rulestate(PFCTL_STATE_NAT)) {
967 memset(&r, 0, sizeof(r));
972 decide_address_family($6.src.host, &r.af);
973 decide_address_family($6.dst.host, &r.af);
975 if ($6.src.port != NULL) {
976 yyerror("source port parameter not supported"
980 if ($6.dst.port != NULL) {
981 if ($6.dst.port->next != NULL) {
982 yyerror("destination port list "
983 "expansion not supported in "
986 } else if ($6.dst.port->op != PF_OP_EQ) {
987 yyerror("destination port operators"
988 " not supported in rdr-anchor");
991 r.dst.port[0] = $6.dst.port->port[0];
992 r.dst.port[1] = $6.dst.port->port[1];
993 r.dst.port_op = $6.dst.port->op;
996 expand_rule(&r, $3, NULL, $5, $6.src_os,
997 $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
1001 | BINATANCHOR string interface af proto fromto rtable {
1004 if (check_rulestate(PFCTL_STATE_NAT)) {
1009 memset(&r, 0, sizeof(r));
1010 r.action = PF_BINAT;
1014 if ($5->next != NULL) {
1015 yyerror("proto list expansion"
1016 " not supported in binat-anchor");
1019 r.proto = $5->proto;
1023 if ($6.src.host != NULL || $6.src.port != NULL ||
1024 $6.dst.host != NULL || $6.dst.port != NULL) {
1025 yyerror("fromto parameter not supported"
1026 " in binat-anchor");
1030 decide_address_family($6.src.host, &r.af);
1031 decide_address_family($6.dst.host, &r.af);
1033 pfctl_add_rule(pf, &r, $2);
1038 loadrule : LOAD ANCHOR string FROM string {
1039 struct loadanchors *loadanchor;
1041 if (strlen(pf->anchor->name) + 1 +
1042 strlen($3) >= MAXPATHLEN) {
1043 yyerror("anchorname %s too long, max %u\n",
1044 $3, MAXPATHLEN - 1);
1048 loadanchor = calloc(1, sizeof(struct loadanchors));
1049 if (loadanchor == NULL)
1050 err(1, "loadrule: calloc");
1051 if ((loadanchor->anchorname = malloc(MAXPATHLEN)) ==
1053 err(1, "loadrule: malloc");
1054 if (pf->anchor->name[0])
1055 snprintf(loadanchor->anchorname, MAXPATHLEN,
1056 "%s/%s", pf->anchor->name, $3);
1058 strlcpy(loadanchor->anchorname, $3, MAXPATHLEN);
1059 if ((loadanchor->filename = strdup($5)) == NULL)
1060 err(1, "loadrule: strdup");
1062 TAILQ_INSERT_TAIL(&loadanchorshead, loadanchor,
1069 scrubaction : no SCRUB {
1078 scrubrule : scrubaction dir logquick interface af proto fromto scrub_opts
1082 if (check_rulestate(PFCTL_STATE_SCRUB))
1085 memset(&r, 0, sizeof(r));
1093 yyerror("scrub rules do not support 'quick'");
1099 r.rule_flag |= PFRULE_NODF;
1101 r.rule_flag |= PFRULE_RANDOMID;
1102 if ($8.reassemble_tcp) {
1103 if (r.direction != PF_INOUT) {
1104 yyerror("reassemble tcp rules can not "
1105 "specify direction");
1108 r.rule_flag |= PFRULE_REASSEMBLE_TCP;
1111 r.min_ttl = $8.minttl;
1113 r.max_mss = $8.maxmss;
1114 if ($8.marker & SOM_SETTOS) {
1115 r.rule_flag |= PFRULE_SET_TOS;
1116 r.set_tos = $8.settos;
1119 r.rule_flag |= $8.fragcache;
1121 if (strlcpy(r.match_tagname, $8.match_tag,
1122 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1123 yyerror("tag too long, max %u chars",
1124 PF_TAG_NAME_SIZE - 1);
1127 r.match_tag_not = $8.match_tag_not;
1128 r.rtableid = $8.rtableid;
1130 expand_rule(&r, $4, NULL, $6, $7.src_os,
1131 $7.src.host, $7.src.port, $7.dst.host, $7.dst.port,
1132 NULL, NULL, NULL, "");
1137 bzero(&scrub_opts, sizeof scrub_opts);
1138 scrub_opts.rtableid = -1;
1141 { $$ = scrub_opts; }
1143 bzero(&scrub_opts, sizeof scrub_opts);
1144 scrub_opts.rtableid = -1;
1149 scrub_opts_l : scrub_opts_l scrub_opt
1154 if (scrub_opts.nodf) {
1155 yyerror("no-df cannot be respecified");
1158 scrub_opts.nodf = 1;
1161 if (scrub_opts.marker & SOM_MINTTL) {
1162 yyerror("min-ttl cannot be respecified");
1165 if ($2 < 0 || $2 > 255) {
1166 yyerror("illegal min-ttl value %d", $2);
1169 scrub_opts.marker |= SOM_MINTTL;
1170 scrub_opts.minttl = $2;
1173 if (scrub_opts.marker & SOM_MAXMSS) {
1174 yyerror("max-mss cannot be respecified");
1177 if ($2 < 0 || $2 > 65535) {
1178 yyerror("illegal max-mss value %d", $2);
1181 scrub_opts.marker |= SOM_MAXMSS;
1182 scrub_opts.maxmss = $2;
1185 if (scrub_opts.marker & SOM_SETTOS) {
1186 yyerror("set-tos cannot be respecified");
1189 scrub_opts.marker |= SOM_SETTOS;
1190 scrub_opts.settos = $2;
1193 if (scrub_opts.marker & SOM_FRAGCACHE) {
1194 yyerror("fragcache cannot be respecified");
1197 scrub_opts.marker |= SOM_FRAGCACHE;
1198 scrub_opts.fragcache = $1;
1200 | REASSEMBLE STRING {
1201 if (strcasecmp($2, "tcp") != 0) {
1202 yyerror("scrub reassemble supports only tcp, "
1208 if (scrub_opts.reassemble_tcp) {
1209 yyerror("reassemble tcp cannot be respecified");
1212 scrub_opts.reassemble_tcp = 1;
1215 if (scrub_opts.randomid) {
1216 yyerror("random-id cannot be respecified");
1219 scrub_opts.randomid = 1;
1222 if ($2 < 0 || $2 > rt_tableid_max()) {
1223 yyerror("invalid rtable id");
1226 scrub_opts.rtableid = $2;
1228 | not TAGGED string {
1229 scrub_opts.match_tag = $3;
1230 scrub_opts.match_tag_not = $1;
1234 fragcache : FRAGMENT REASSEMBLE { $$ = 0; /* default */ }
1235 | FRAGMENT FRAGCROP { $$ = 0; }
1236 | FRAGMENT FRAGDROP { $$ = 0; }
1239 antispoof : ANTISPOOF logquick antispoof_ifspc af antispoof_opts {
1241 struct node_host *h = NULL, *hh;
1242 struct node_if *i, *j;
1244 if (check_rulestate(PFCTL_STATE_FILTER))
1247 for (i = $3; i; i = i->next) {
1248 bzero(&r, sizeof(r));
1251 r.direction = PF_IN;
1256 if (rule_label(&r, $5.label))
1258 r.rtableid = $5.rtableid;
1259 j = calloc(1, sizeof(struct node_if));
1261 err(1, "antispoof: calloc");
1262 if (strlcpy(j->ifname, i->ifname,
1263 sizeof(j->ifname)) >= sizeof(j->ifname)) {
1265 yyerror("interface name too long");
1270 h = calloc(1, sizeof(*h));
1272 err(1, "address: calloc");
1273 h->addr.type = PF_ADDR_DYNIFTL;
1275 if (strlcpy(h->addr.v.ifname, i->ifname,
1276 sizeof(h->addr.v.ifname)) >=
1277 sizeof(h->addr.v.ifname)) {
1280 "interface name too long");
1283 hh = malloc(sizeof(*hh));
1285 err(1, "address: malloc");
1286 bcopy(h, hh, sizeof(*hh));
1287 h->addr.iflags = PFI_AFLAG_NETWORK;
1289 h = ifa_lookup(j->ifname,
1295 expand_rule(&r, j, NULL, NULL, NULL, h,
1296 NULL, NULL, NULL, NULL, NULL,
1299 if ((i->ifa_flags & IFF_LOOPBACK) == 0) {
1300 bzero(&r, sizeof(r));
1303 r.direction = PF_IN;
1308 if (rule_label(&r, $5.label))
1310 r.rtableid = $5.rtableid;
1314 h = ifa_lookup(i->ifname, 0);
1316 expand_rule(&r, NULL, NULL,
1317 NULL, NULL, h, NULL, NULL,
1318 NULL, NULL, NULL, NULL, "");
1326 antispoof_ifspc : FOR antispoof_if { $$ = $2; }
1327 | FOR '{' optnl antispoof_iflst '}' { $$ = $4; }
1330 antispoof_iflst : antispoof_if optnl { $$ = $1; }
1331 | antispoof_iflst comma antispoof_if optnl {
1332 $1->tail->next = $3;
1338 antispoof_if : if_item { $$ = $1; }
1346 bzero(&antispoof_opts, sizeof antispoof_opts);
1347 antispoof_opts.rtableid = -1;
1350 { $$ = antispoof_opts; }
1352 bzero(&antispoof_opts, sizeof antispoof_opts);
1353 antispoof_opts.rtableid = -1;
1354 $$ = antispoof_opts;
1358 antispoof_opts_l : antispoof_opts_l antispoof_opt
1362 antispoof_opt : label {
1363 if (antispoof_opts.label) {
1364 yyerror("label cannot be redefined");
1367 antispoof_opts.label = $1;
1370 if ($2 < 0 || $2 > rt_tableid_max()) {
1371 yyerror("invalid rtable id");
1374 antispoof_opts.rtableid = $2;
1378 not : '!' { $$ = 1; }
1379 | /* empty */ { $$ = 0; }
1382 tabledef : TABLE '<' STRING '>' table_opts {
1383 struct node_host *h, *nh;
1384 struct node_tinit *ti, *nti;
1386 if (strlen($3) >= PF_TABLE_NAME_SIZE) {
1387 yyerror("table name too long, max %d chars",
1388 PF_TABLE_NAME_SIZE - 1);
1392 if (pf->loadopt & PFCTL_FLAG_TABLE)
1393 if (process_tabledef($3, &$5)) {
1398 for (ti = SIMPLEQ_FIRST(&$5.init_nodes);
1399 ti != SIMPLEQ_END(&$5.init_nodes); ti = nti) {
1402 for (h = ti->host; h != NULL; h = nh) {
1406 nti = SIMPLEQ_NEXT(ti, entries);
1413 bzero(&table_opts, sizeof table_opts);
1414 SIMPLEQ_INIT(&table_opts.init_nodes);
1417 { $$ = table_opts; }
1420 bzero(&table_opts, sizeof table_opts);
1421 SIMPLEQ_INIT(&table_opts.init_nodes);
1426 table_opts_l : table_opts_l table_opt
1430 table_opt : STRING {
1431 if (!strcmp($1, "const"))
1432 table_opts.flags |= PFR_TFLAG_CONST;
1433 else if (!strcmp($1, "persist"))
1434 table_opts.flags |= PFR_TFLAG_PERSIST;
1435 else if (!strcmp($1, "counters"))
1436 table_opts.flags |= PFR_TFLAG_COUNTERS;
1438 yyerror("invalid table option '%s'", $1);
1444 | '{' optnl '}' { table_opts.init_addr = 1; }
1445 | '{' optnl host_list '}' {
1446 struct node_host *n;
1447 struct node_tinit *ti;
1449 for (n = $3; n != NULL; n = n->next) {
1450 switch (n->addr.type) {
1451 case PF_ADDR_ADDRMASK:
1454 yyerror("address ranges are not "
1455 "permitted inside tables");
1457 case PF_ADDR_DYNIFTL:
1458 yyerror("dynamic addresses are not "
1459 "permitted inside tables");
1462 yyerror("tables cannot contain tables");
1464 case PF_ADDR_NOROUTE:
1465 yyerror("\"no-route\" is not permitted "
1468 case PF_ADDR_URPFFAILED:
1469 yyerror("\"urpf-failed\" is not "
1470 "permitted inside tables");
1473 yyerror("unknown address type %d",
1478 if (!(ti = calloc(1, sizeof(*ti))))
1479 err(1, "table_opt: calloc");
1481 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1483 table_opts.init_addr = 1;
1486 struct node_tinit *ti;
1488 if (!(ti = calloc(1, sizeof(*ti))))
1489 err(1, "table_opt: calloc");
1491 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1493 table_opts.init_addr = 1;
1497 altqif : ALTQ interface queue_opts QUEUE qassign {
1500 if (check_rulestate(PFCTL_STATE_QUEUE))
1503 memset(&a, 0, sizeof(a));
1504 if ($3.scheduler.qtype == ALTQT_NONE) {
1505 yyerror("no scheduler specified!");
1508 a.scheduler = $3.scheduler.qtype;
1509 a.qlimit = $3.qlimit;
1510 a.tbrsize = $3.tbrsize;
1511 if ($5 == NULL && $3.scheduler.qtype != ALTQT_CODEL) {
1512 yyerror("no child queues specified");
1515 if (expand_altq(&a, $2, $5, $3.queue_bwspec,
1521 queuespec : QUEUE STRING interface queue_opts qassign {
1524 if (check_rulestate(PFCTL_STATE_QUEUE)) {
1529 memset(&a, 0, sizeof(a));
1531 if (strlcpy(a.qname, $2, sizeof(a.qname)) >=
1533 yyerror("queue name too long (max "
1534 "%d chars)", PF_QNAME_SIZE-1);
1540 yyerror("cannot specify tbrsize for queue");
1543 if ($4.priority > 255) {
1544 yyerror("priority out of range: max 255");
1547 a.priority = $4.priority;
1548 a.qlimit = $4.qlimit;
1549 a.scheduler = $4.scheduler.qtype;
1550 if (expand_queue(&a, $3, $5, $4.queue_bwspec,
1552 yyerror("errors in queue definition");
1559 bzero(&queue_opts, sizeof queue_opts);
1560 queue_opts.priority = DEFAULT_PRIORITY;
1561 queue_opts.qlimit = DEFAULT_QLIMIT;
1562 queue_opts.scheduler.qtype = ALTQT_NONE;
1563 queue_opts.queue_bwspec.bw_percent = 100;
1566 { $$ = queue_opts; }
1568 bzero(&queue_opts, sizeof queue_opts);
1569 queue_opts.priority = DEFAULT_PRIORITY;
1570 queue_opts.qlimit = DEFAULT_QLIMIT;
1571 queue_opts.scheduler.qtype = ALTQT_NONE;
1572 queue_opts.queue_bwspec.bw_percent = 100;
1577 queue_opts_l : queue_opts_l queue_opt
1581 queue_opt : BANDWIDTH bandwidth {
1582 if (queue_opts.marker & QOM_BWSPEC) {
1583 yyerror("bandwidth cannot be respecified");
1586 queue_opts.marker |= QOM_BWSPEC;
1587 queue_opts.queue_bwspec = $2;
1590 if (queue_opts.marker & QOM_PRIORITY) {
1591 yyerror("priority cannot be respecified");
1594 if ($2 < 0 || $2 > 255) {
1595 yyerror("priority out of range: max 255");
1598 queue_opts.marker |= QOM_PRIORITY;
1599 queue_opts.priority = $2;
1602 if (queue_opts.marker & QOM_QLIMIT) {
1603 yyerror("qlimit cannot be respecified");
1606 if ($2 < 0 || $2 > 65535) {
1607 yyerror("qlimit out of range: max 65535");
1610 queue_opts.marker |= QOM_QLIMIT;
1611 queue_opts.qlimit = $2;
1614 if (queue_opts.marker & QOM_SCHEDULER) {
1615 yyerror("scheduler cannot be respecified");
1618 queue_opts.marker |= QOM_SCHEDULER;
1619 queue_opts.scheduler = $1;
1622 if (queue_opts.marker & QOM_TBRSIZE) {
1623 yyerror("tbrsize cannot be respecified");
1626 if ($2 < 0 || $2 > 65535) {
1627 yyerror("tbrsize too big: max 65535");
1630 queue_opts.marker |= QOM_TBRSIZE;
1631 queue_opts.tbrsize = $2;
1635 bandwidth : STRING {
1641 bps = strtod($1, &cp);
1643 if (strlen(cp) > 1) {
1645 if (!strcmp(cu, "Bit") ||
1647 !strcmp(cu, "bit") ||
1652 if (!strcmp(cp, "b"))
1654 else if (!strcmp(cp, "K"))
1656 else if (!strcmp(cp, "M"))
1658 else if (!strcmp(cp, "G"))
1659 bps *= 1000 * 1000 * 1000;
1660 else if (!strcmp(cp, "%")) {
1661 if (bps < 0 || bps > 100) {
1662 yyerror("bandwidth spec "
1667 $$.bw_percent = bps;
1670 yyerror("unknown unit %s", cp);
1676 $$.bw_absolute = (u_int32_t)bps;
1679 if ($1 < 0 || $1 > UINT_MAX) {
1680 yyerror("bandwidth number too big");
1684 $$.bw_absolute = $1;
1689 $$.qtype = ALTQT_CBQ;
1690 $$.data.cbq_opts.flags = 0;
1692 | CBQ '(' cbqflags_list ')' {
1693 $$.qtype = ALTQT_CBQ;
1694 $$.data.cbq_opts.flags = $3;
1697 $$.qtype = ALTQT_PRIQ;
1698 $$.data.priq_opts.flags = 0;
1700 | PRIQ '(' priqflags_list ')' {
1701 $$.qtype = ALTQT_PRIQ;
1702 $$.data.priq_opts.flags = $3;
1705 $$.qtype = ALTQT_HFSC;
1706 bzero(&$$.data.hfsc_opts,
1707 sizeof(struct node_hfsc_opts));
1709 | HFSC '(' hfsc_opts ')' {
1710 $$.qtype = ALTQT_HFSC;
1711 $$.data.hfsc_opts = $3;
1714 $$.qtype = ALTQT_FAIRQ;
1715 bzero(&$$.data.fairq_opts,
1716 sizeof(struct node_fairq_opts));
1718 | FAIRQ '(' fairq_opts ')' {
1719 $$.qtype = ALTQT_FAIRQ;
1720 $$.data.fairq_opts = $3;
1723 $$.qtype = ALTQT_CODEL;
1724 bzero(&$$.data.codel_opts,
1725 sizeof(struct codel_opts));
1727 | CODEL '(' codel_opts ')' {
1728 $$.qtype = ALTQT_CODEL;
1729 $$.data.codel_opts = $3;
1733 cbqflags_list : cbqflags_item { $$ |= $1; }
1734 | cbqflags_list comma cbqflags_item { $$ |= $3; }
1737 cbqflags_item : STRING {
1738 if (!strcmp($1, "default"))
1739 $$ = CBQCLF_DEFCLASS;
1740 else if (!strcmp($1, "borrow"))
1742 else if (!strcmp($1, "red"))
1744 else if (!strcmp($1, "ecn"))
1745 $$ = CBQCLF_RED|CBQCLF_ECN;
1746 else if (!strcmp($1, "rio"))
1748 else if (!strcmp($1, "codel"))
1751 yyerror("unknown cbq flag \"%s\"", $1);
1759 priqflags_list : priqflags_item { $$ |= $1; }
1760 | priqflags_list comma priqflags_item { $$ |= $3; }
1763 priqflags_item : STRING {
1764 if (!strcmp($1, "default"))
1765 $$ = PRCF_DEFAULTCLASS;
1766 else if (!strcmp($1, "red"))
1768 else if (!strcmp($1, "ecn"))
1769 $$ = PRCF_RED|PRCF_ECN;
1770 else if (!strcmp($1, "rio"))
1772 else if (!strcmp($1, "codel"))
1775 yyerror("unknown priq flag \"%s\"", $1);
1785 sizeof(struct node_hfsc_opts));
1792 hfscopts_list : hfscopts_item
1793 | hfscopts_list comma hfscopts_item
1796 hfscopts_item : LINKSHARE bandwidth {
1797 if (hfsc_opts.linkshare.used) {
1798 yyerror("linkshare already specified");
1801 hfsc_opts.linkshare.m2 = $2;
1802 hfsc_opts.linkshare.used = 1;
1804 | LINKSHARE '(' bandwidth comma NUMBER comma bandwidth ')'
1806 if ($5 < 0 || $5 > INT_MAX) {
1807 yyerror("timing in curve out of range");
1810 if (hfsc_opts.linkshare.used) {
1811 yyerror("linkshare already specified");
1814 hfsc_opts.linkshare.m1 = $3;
1815 hfsc_opts.linkshare.d = $5;
1816 hfsc_opts.linkshare.m2 = $7;
1817 hfsc_opts.linkshare.used = 1;
1819 | REALTIME bandwidth {
1820 if (hfsc_opts.realtime.used) {
1821 yyerror("realtime already specified");
1824 hfsc_opts.realtime.m2 = $2;
1825 hfsc_opts.realtime.used = 1;
1827 | REALTIME '(' bandwidth comma NUMBER comma bandwidth ')'
1829 if ($5 < 0 || $5 > INT_MAX) {
1830 yyerror("timing in curve out of range");
1833 if (hfsc_opts.realtime.used) {
1834 yyerror("realtime already specified");
1837 hfsc_opts.realtime.m1 = $3;
1838 hfsc_opts.realtime.d = $5;
1839 hfsc_opts.realtime.m2 = $7;
1840 hfsc_opts.realtime.used = 1;
1842 | UPPERLIMIT bandwidth {
1843 if (hfsc_opts.upperlimit.used) {
1844 yyerror("upperlimit already specified");
1847 hfsc_opts.upperlimit.m2 = $2;
1848 hfsc_opts.upperlimit.used = 1;
1850 | UPPERLIMIT '(' bandwidth comma NUMBER comma bandwidth ')'
1852 if ($5 < 0 || $5 > INT_MAX) {
1853 yyerror("timing in curve out of range");
1856 if (hfsc_opts.upperlimit.used) {
1857 yyerror("upperlimit already specified");
1860 hfsc_opts.upperlimit.m1 = $3;
1861 hfsc_opts.upperlimit.d = $5;
1862 hfsc_opts.upperlimit.m2 = $7;
1863 hfsc_opts.upperlimit.used = 1;
1866 if (!strcmp($1, "default"))
1867 hfsc_opts.flags |= HFCF_DEFAULTCLASS;
1868 else if (!strcmp($1, "red"))
1869 hfsc_opts.flags |= HFCF_RED;
1870 else if (!strcmp($1, "ecn"))
1871 hfsc_opts.flags |= HFCF_RED|HFCF_ECN;
1872 else if (!strcmp($1, "rio"))
1873 hfsc_opts.flags |= HFCF_RIO;
1874 else if (!strcmp($1, "codel"))
1875 hfsc_opts.flags |= HFCF_CODEL;
1877 yyerror("unknown hfsc flag \"%s\"", $1);
1887 sizeof(struct node_fairq_opts));
1894 fairqopts_list : fairqopts_item
1895 | fairqopts_list comma fairqopts_item
1898 fairqopts_item : LINKSHARE bandwidth {
1899 if (fairq_opts.linkshare.used) {
1900 yyerror("linkshare already specified");
1903 fairq_opts.linkshare.m2 = $2;
1904 fairq_opts.linkshare.used = 1;
1906 | LINKSHARE '(' bandwidth number bandwidth ')' {
1907 if (fairq_opts.linkshare.used) {
1908 yyerror("linkshare already specified");
1911 fairq_opts.linkshare.m1 = $3;
1912 fairq_opts.linkshare.d = $4;
1913 fairq_opts.linkshare.m2 = $5;
1914 fairq_opts.linkshare.used = 1;
1917 fairq_opts.hogs_bw = $2;
1920 fairq_opts.nbuckets = $2;
1923 if (!strcmp($1, "default"))
1924 fairq_opts.flags |= FARF_DEFAULTCLASS;
1925 else if (!strcmp($1, "red"))
1926 fairq_opts.flags |= FARF_RED;
1927 else if (!strcmp($1, "ecn"))
1928 fairq_opts.flags |= FARF_RED|FARF_ECN;
1929 else if (!strcmp($1, "rio"))
1930 fairq_opts.flags |= FARF_RIO;
1931 else if (!strcmp($1, "codel"))
1932 fairq_opts.flags |= FARF_CODEL;
1934 yyerror("unknown fairq flag \"%s\"", $1);
1944 sizeof(struct codel_opts));
1951 codelopts_list : codelopts_item
1952 | codelopts_list comma codelopts_item
1955 codelopts_item : INTERVAL number {
1956 if (codel_opts.interval) {
1957 yyerror("interval already specified");
1960 codel_opts.interval = $2;
1963 if (codel_opts.target) {
1964 yyerror("target already specified");
1967 codel_opts.target = $2;
1970 if (!strcmp($1, "ecn"))
1973 yyerror("unknown codel option \"%s\"", $1);
1981 qassign : /* empty */ { $$ = NULL; }
1982 | qassign_item { $$ = $1; }
1983 | '{' optnl qassign_list '}' { $$ = $3; }
1986 qassign_list : qassign_item optnl { $$ = $1; }
1987 | qassign_list comma qassign_item optnl {
1988 $1->tail->next = $3;
1994 qassign_item : STRING {
1995 $$ = calloc(1, sizeof(struct node_queue));
1997 err(1, "qassign_item: calloc");
1998 if (strlcpy($$->queue, $1, sizeof($$->queue)) >=
1999 sizeof($$->queue)) {
2000 yyerror("queue name '%s' too long (max "
2001 "%d chars)", $1, sizeof($$->queue)-1);
2012 pfrule : action dir logquick interface route af proto fromto
2016 struct node_state_opt *o;
2017 struct node_proto *proto;
2023 if (check_rulestate(PFCTL_STATE_FILTER))
2026 memset(&r, 0, sizeof(r));
2030 case PFRULE_RETURNRST:
2031 r.rule_flag |= PFRULE_RETURNRST;
2032 r.return_ttl = $1.w;
2034 case PFRULE_RETURNICMP:
2035 r.rule_flag |= PFRULE_RETURNICMP;
2036 r.return_icmp = $1.w;
2037 r.return_icmp6 = $1.w2;
2040 r.rule_flag |= PFRULE_RETURN;
2041 r.return_icmp = $1.w;
2042 r.return_icmp6 = $1.w2;
2050 r.rtableid = $9.rtableid;
2052 if ($9.marker & FOM_PRIO) {
2054 r.prio = PF_PRIO_ZERO;
2058 if ($9.marker & FOM_SETPRIO) {
2059 r.set_prio[0] = $9.set_prio[0];
2060 r.set_prio[1] = $9.set_prio[1];
2061 r.scrub_flags |= PFSTATE_SETPRIO;
2066 if (strlcpy(r.tagname, $9.tag,
2067 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
2068 yyerror("tag too long, max %u chars",
2069 PF_TAG_NAME_SIZE - 1);
2073 if (strlcpy(r.match_tagname, $9.match_tag,
2074 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
2075 yyerror("tag too long, max %u chars",
2076 PF_TAG_NAME_SIZE - 1);
2079 r.match_tag_not = $9.match_tag_not;
2080 if (rule_label(&r, $9.label))
2083 r.flags = $9.flags.b1;
2084 r.flagset = $9.flags.b2;
2085 if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
2086 yyerror("flags always false");
2089 if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
2090 for (proto = $7; proto != NULL &&
2091 proto->proto != IPPROTO_TCP;
2092 proto = proto->next)
2094 if (proto == NULL && $7 != NULL) {
2095 if ($9.flags.b1 || $9.flags.b2)
2097 "flags only apply to tcp");
2100 "OS fingerprinting only "
2105 if (($9.flags.b1 & parse_flags("S")) == 0 &&
2107 yyerror("OS fingerprinting requires "
2108 "the SYN TCP flag (flags S/SA)");
2115 r.keep_state = $9.keep.action;
2116 o = $9.keep.options;
2118 /* 'keep state' by default on pass rules. */
2119 if (!r.keep_state && !r.action &&
2120 !($9.marker & FOM_KEEP)) {
2121 r.keep_state = PF_STATE_NORMAL;
2122 o = keep_state_defaults;
2127 struct node_state_opt *p = o;
2130 case PF_STATE_OPT_MAX:
2132 yyerror("state option 'max' "
2133 "multiple definitions");
2136 r.max_states = o->data.max_states;
2138 case PF_STATE_OPT_NOSYNC:
2139 if (r.rule_flag & PFRULE_NOSYNC) {
2140 yyerror("state option 'sync' "
2141 "multiple definitions");
2144 r.rule_flag |= PFRULE_NOSYNC;
2146 case PF_STATE_OPT_SRCTRACK:
2148 yyerror("state option "
2150 "multiple definitions");
2153 srctrack = o->data.src_track;
2154 r.rule_flag |= PFRULE_SRCTRACK;
2156 case PF_STATE_OPT_MAX_SRC_STATES:
2157 if (r.max_src_states) {
2158 yyerror("state option "
2160 "multiple definitions");
2163 if (o->data.max_src_states == 0) {
2164 yyerror("'max-src-states' must "
2169 o->data.max_src_states;
2170 r.rule_flag |= PFRULE_SRCTRACK;
2172 case PF_STATE_OPT_OVERLOAD:
2173 if (r.overload_tblname[0]) {
2174 yyerror("multiple 'overload' "
2175 "table definitions");
2178 if (strlcpy(r.overload_tblname,
2179 o->data.overload.tblname,
2180 PF_TABLE_NAME_SIZE) >=
2181 PF_TABLE_NAME_SIZE) {
2182 yyerror("state option: "
2186 r.flush = o->data.overload.flush;
2188 case PF_STATE_OPT_MAX_SRC_CONN:
2189 if (r.max_src_conn) {
2190 yyerror("state option "
2192 "multiple definitions");
2195 if (o->data.max_src_conn == 0) {
2196 yyerror("'max-src-conn' "
2201 o->data.max_src_conn;
2202 r.rule_flag |= PFRULE_SRCTRACK |
2203 PFRULE_RULESRCTRACK;
2205 case PF_STATE_OPT_MAX_SRC_CONN_RATE:
2206 if (r.max_src_conn_rate.limit) {
2207 yyerror("state option "
2208 "'max-src-conn-rate' "
2209 "multiple definitions");
2212 if (!o->data.max_src_conn_rate.limit ||
2213 !o->data.max_src_conn_rate.seconds) {
2214 yyerror("'max-src-conn-rate' "
2215 "values must be > 0");
2218 if (o->data.max_src_conn_rate.limit >
2220 yyerror("'max-src-conn-rate' "
2221 "maximum rate must be < %u",
2225 r.max_src_conn_rate.limit =
2226 o->data.max_src_conn_rate.limit;
2227 r.max_src_conn_rate.seconds =
2228 o->data.max_src_conn_rate.seconds;
2229 r.rule_flag |= PFRULE_SRCTRACK |
2230 PFRULE_RULESRCTRACK;
2232 case PF_STATE_OPT_MAX_SRC_NODES:
2233 if (r.max_src_nodes) {
2234 yyerror("state option "
2236 "multiple definitions");
2239 if (o->data.max_src_nodes == 0) {
2240 yyerror("'max-src-nodes' must "
2245 o->data.max_src_nodes;
2246 r.rule_flag |= PFRULE_SRCTRACK |
2247 PFRULE_RULESRCTRACK;
2249 case PF_STATE_OPT_STATELOCK:
2251 yyerror("state locking option: "
2252 "multiple definitions");
2256 r.rule_flag |= o->data.statelock;
2258 case PF_STATE_OPT_SLOPPY:
2259 if (r.rule_flag & PFRULE_STATESLOPPY) {
2260 yyerror("state sloppy option: "
2261 "multiple definitions");
2264 r.rule_flag |= PFRULE_STATESLOPPY;
2266 case PF_STATE_OPT_TIMEOUT:
2267 if (o->data.timeout.number ==
2268 PFTM_ADAPTIVE_START ||
2269 o->data.timeout.number ==
2272 if (r.timeout[o->data.timeout.number]) {
2273 yyerror("state timeout %s "
2274 "multiple definitions",
2275 pf_timeouts[o->data.
2276 timeout.number].name);
2279 r.timeout[o->data.timeout.number] =
2280 o->data.timeout.seconds;
2287 /* 'flags S/SA' by default on stateful rules */
2288 if (!r.action && !r.flags && !r.flagset &&
2289 !$9.fragment && !($9.marker & FOM_FLAGS) &&
2291 r.flags = parse_flags("S");
2292 r.flagset = parse_flags("SA");
2294 if (!adaptive && r.max_states) {
2295 r.timeout[PFTM_ADAPTIVE_START] =
2296 (r.max_states / 10) * 6;
2297 r.timeout[PFTM_ADAPTIVE_END] =
2298 (r.max_states / 10) * 12;
2300 if (r.rule_flag & PFRULE_SRCTRACK) {
2301 if (srctrack == PF_SRCTRACK_GLOBAL &&
2303 yyerror("'max-src-nodes' is "
2304 "incompatible with "
2305 "'source-track global'");
2308 if (srctrack == PF_SRCTRACK_GLOBAL &&
2310 yyerror("'max-src-conn' is "
2311 "incompatible with "
2312 "'source-track global'");
2315 if (srctrack == PF_SRCTRACK_GLOBAL &&
2316 r.max_src_conn_rate.seconds) {
2317 yyerror("'max-src-conn-rate' is "
2318 "incompatible with "
2319 "'source-track global'");
2322 if (r.timeout[PFTM_SRC_NODE] <
2323 r.max_src_conn_rate.seconds)
2324 r.timeout[PFTM_SRC_NODE] =
2325 r.max_src_conn_rate.seconds;
2326 r.rule_flag |= PFRULE_SRCTRACK;
2327 if (srctrack == PF_SRCTRACK_RULE)
2328 r.rule_flag |= PFRULE_RULESRCTRACK;
2330 if (r.keep_state && !statelock)
2331 r.rule_flag |= default_statelock;
2334 r.rule_flag |= PFRULE_FRAGMENT;
2335 r.allow_opts = $9.allowopts;
2337 decide_address_family($8.src.host, &r.af);
2338 decide_address_family($8.dst.host, &r.af);
2342 yyerror("direction must be explicit "
2343 "with rules that specify routing");
2347 r.rpool.opts = $5.pool_opts;
2349 memcpy(&r.rpool.key, $5.key,
2350 sizeof(struct pf_poolhashkey));
2353 decide_address_family($5.host, &r.af);
2354 remove_invalid_hosts(&$5.host, &r.af);
2355 if ($5.host == NULL) {
2356 yyerror("no routing address with "
2357 "matching address family found.");
2360 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
2361 PF_POOL_NONE && ($5.host->next != NULL ||
2362 $5.host->addr.type == PF_ADDR_TABLE ||
2363 DYNIF_MULTIADDR($5.host->addr)))
2364 r.rpool.opts |= PF_POOL_ROUNDROBIN;
2365 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2366 PF_POOL_ROUNDROBIN &&
2367 disallow_table($5.host, "tables are only "
2368 "supported in round-robin routing pools"))
2370 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2371 PF_POOL_ROUNDROBIN &&
2372 disallow_alias($5.host, "interface (%s) "
2373 "is only supported in round-robin "
2376 if ($5.host->next != NULL) {
2377 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2378 PF_POOL_ROUNDROBIN) {
2379 yyerror("r.rpool.opts must "
2380 "be PF_POOL_ROUNDROBIN");
2385 if ($9.queues.qname != NULL) {
2386 if (strlcpy(r.qname, $9.queues.qname,
2387 sizeof(r.qname)) >= sizeof(r.qname)) {
2388 yyerror("rule qname too long (max "
2389 "%d chars)", sizeof(r.qname)-1);
2392 free($9.queues.qname);
2394 if ($9.queues.pqname != NULL) {
2395 if (strlcpy(r.pqname, $9.queues.pqname,
2396 sizeof(r.pqname)) >= sizeof(r.pqname)) {
2397 yyerror("rule pqname too long (max "
2398 "%d chars)", sizeof(r.pqname)-1);
2401 free($9.queues.pqname);
2404 r.divert.port = $9.divert.port;
2406 if ((r.divert.port = $9.divert.port)) {
2407 if (r.direction == PF_OUT) {
2408 if ($9.divert.addr) {
2409 yyerror("address specified "
2410 "for outgoing divert");
2413 bzero(&r.divert.addr,
2414 sizeof(r.divert.addr));
2416 if (!$9.divert.addr) {
2417 yyerror("no address specified "
2418 "for incoming divert");
2421 if ($9.divert.addr->af != r.af) {
2422 yyerror("address family "
2423 "mismatch for divert");
2427 $9.divert.addr->addr.v.a.addr;
2432 expand_rule(&r, $4, $5.host, $7, $8.src_os,
2433 $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
2434 $9.uid, $9.gid, $9.icmpspec, "");
2439 bzero(&filter_opts, sizeof filter_opts);
2440 filter_opts.rtableid = -1;
2443 { $$ = filter_opts; }
2445 bzero(&filter_opts, sizeof filter_opts);
2446 filter_opts.rtableid = -1;
2451 filter_opts_l : filter_opts_l filter_opt
2455 filter_opt : USER uids {
2456 if (filter_opts.uid)
2457 $2->tail->next = filter_opts.uid;
2458 filter_opts.uid = $2;
2461 if (filter_opts.gid)
2462 $2->tail->next = filter_opts.gid;
2463 filter_opts.gid = $2;
2466 if (filter_opts.marker & FOM_FLAGS) {
2467 yyerror("flags cannot be redefined");
2470 filter_opts.marker |= FOM_FLAGS;
2471 filter_opts.flags.b1 |= $1.b1;
2472 filter_opts.flags.b2 |= $1.b2;
2473 filter_opts.flags.w |= $1.w;
2474 filter_opts.flags.w2 |= $1.w2;
2477 if (filter_opts.marker & FOM_ICMP) {
2478 yyerror("icmp-type cannot be redefined");
2481 filter_opts.marker |= FOM_ICMP;
2482 filter_opts.icmpspec = $1;
2485 if (filter_opts.marker & FOM_PRIO) {
2486 yyerror("prio cannot be redefined");
2489 if ($2 < 0 || $2 > PF_PRIO_MAX) {
2490 yyerror("prio must be 0 - %u", PF_PRIO_MAX);
2493 filter_opts.marker |= FOM_PRIO;
2494 filter_opts.prio = $2;
2497 if (filter_opts.marker & FOM_TOS) {
2498 yyerror("tos cannot be redefined");
2501 filter_opts.marker |= FOM_TOS;
2502 filter_opts.tos = $2;
2505 if (filter_opts.marker & FOM_KEEP) {
2506 yyerror("modulate or keep cannot be redefined");
2509 filter_opts.marker |= FOM_KEEP;
2510 filter_opts.keep.action = $1.action;
2511 filter_opts.keep.options = $1.options;
2514 filter_opts.fragment = 1;
2517 filter_opts.allowopts = 1;
2520 if (filter_opts.label) {
2521 yyerror("label cannot be redefined");
2524 filter_opts.label = $1;
2527 if (filter_opts.queues.qname) {
2528 yyerror("queue cannot be redefined");
2531 filter_opts.queues = $1;
2534 filter_opts.tag = $2;
2536 | not TAGGED string {
2537 filter_opts.match_tag = $3;
2538 filter_opts.match_tag_not = $1;
2540 | PROBABILITY probability {
2543 p = floor($2 * UINT_MAX + 0.5);
2544 if (p < 0.0 || p > UINT_MAX) {
2545 yyerror("invalid probability: %lf", p);
2548 filter_opts.prob = (u_int32_t)p;
2549 if (filter_opts.prob == 0)
2550 filter_opts.prob = 1;
2553 if ($2 < 0 || $2 > rt_tableid_max()) {
2554 yyerror("invalid rtable id");
2557 filter_opts.rtableid = $2;
2559 | DIVERTTO portplain {
2561 filter_opts.divert.port = $2.a;
2562 if (!filter_opts.divert.port) {
2563 yyerror("invalid divert port: %u", ntohs($2.a));
2568 | DIVERTTO STRING PORT portplain {
2570 if ((filter_opts.divert.addr = host($2)) == NULL) {
2571 yyerror("could not parse divert address: %s",
2580 filter_opts.divert.port = $4.a;
2581 if (!filter_opts.divert.port) {
2582 yyerror("invalid divert port: %u", ntohs($4.a));
2588 yyerror("divert-reply has no meaning in FreeBSD pf(4)");
2591 filter_opts.divert.port = 1; /* some random value */
2597 filter_sets : SET '(' filter_sets_l ')' { $$ = filter_opts; }
2598 | SET filter_set { $$ = filter_opts; }
2601 filter_sets_l : filter_sets_l comma filter_set
2606 if (filter_opts.marker & FOM_SETPRIO) {
2607 yyerror("prio cannot be redefined");
2610 filter_opts.marker |= FOM_SETPRIO;
2611 filter_opts.set_prio[0] = $1.b1;
2612 filter_opts.set_prio[1] = $1.b2;
2614 prio : PRIO NUMBER {
2615 if ($2 < 0 || $2 > PF_PRIO_MAX) {
2616 yyerror("prio must be 0 - %u", PF_PRIO_MAX);
2621 | PRIO '(' NUMBER comma NUMBER ')' {
2622 if ($3 < 0 || $3 > PF_PRIO_MAX ||
2623 $5 < 0 || $5 > PF_PRIO_MAX) {
2624 yyerror("prio must be 0 - %u", PF_PRIO_MAX);
2632 probability : STRING {
2634 double p = strtod($1, &e);
2641 yyerror("invalid probability: %s", $1);
2657 $$.w = returnicmpdefault;
2658 $$.w2 = returnicmp6default;
2660 | BLOCK blockspec { $$ = $2; $$.b1 = PF_DROP; }
2663 blockspec : /* empty */ {
2664 $$.b2 = blockpolicy;
2665 $$.w = returnicmpdefault;
2666 $$.w2 = returnicmp6default;
2669 $$.b2 = PFRULE_DROP;
2674 $$.b2 = PFRULE_RETURNRST;
2678 | RETURNRST '(' TTL NUMBER ')' {
2679 if ($4 < 0 || $4 > 255) {
2680 yyerror("illegal ttl value %d", $4);
2683 $$.b2 = PFRULE_RETURNRST;
2688 $$.b2 = PFRULE_RETURNICMP;
2689 $$.w = returnicmpdefault;
2690 $$.w2 = returnicmp6default;
2693 $$.b2 = PFRULE_RETURNICMP;
2694 $$.w = returnicmpdefault;
2695 $$.w2 = returnicmp6default;
2697 | RETURNICMP '(' reticmpspec ')' {
2698 $$.b2 = PFRULE_RETURNICMP;
2700 $$.w2 = returnicmpdefault;
2702 | RETURNICMP6 '(' reticmp6spec ')' {
2703 $$.b2 = PFRULE_RETURNICMP;
2704 $$.w = returnicmpdefault;
2707 | RETURNICMP '(' reticmpspec comma reticmp6spec ')' {
2708 $$.b2 = PFRULE_RETURNICMP;
2713 $$.b2 = PFRULE_RETURN;
2714 $$.w = returnicmpdefault;
2715 $$.w2 = returnicmp6default;
2719 reticmpspec : STRING {
2720 if (!($$ = parseicmpspec($1, AF_INET))) {
2729 if ($1 < 0 || $1 > 255) {
2730 yyerror("invalid icmp code %lu", $1);
2733 icmptype = returnicmpdefault >> 8;
2734 $$ = (icmptype << 8 | $1);
2738 reticmp6spec : STRING {
2739 if (!($$ = parseicmpspec($1, AF_INET6))) {
2748 if ($1 < 0 || $1 > 255) {
2749 yyerror("invalid icmp code %lu", $1);
2752 icmptype = returnicmp6default >> 8;
2753 $$ = (icmptype << 8 | $1);
2757 dir : /* empty */ { $$ = PF_INOUT; }
2758 | IN { $$ = PF_IN; }
2759 | OUT { $$ = PF_OUT; }
2762 quick : /* empty */ { $$.quick = 0; }
2763 | QUICK { $$.quick = 1; }
2766 logquick : /* empty */ { $$.log = 0; $$.quick = 0; $$.logif = 0; }
2767 | log { $$ = $1; $$.quick = 0; }
2768 | QUICK { $$.quick = 1; $$.log = 0; $$.logif = 0; }
2769 | log QUICK { $$ = $1; $$.quick = 1; }
2770 | QUICK log { $$ = $2; $$.quick = 1; }
2773 log : LOG { $$.log = PF_LOG; $$.logif = 0; }
2774 | LOG '(' logopts ')' {
2775 $$.log = PF_LOG | $3.log;
2776 $$.logif = $3.logif;
2780 logopts : logopt { $$ = $1; }
2781 | logopts comma logopt {
2782 $$.log = $1.log | $3.log;
2783 $$.logif = $3.logif;
2785 $$.logif = $1.logif;
2789 logopt : ALL { $$.log = PF_LOG_ALL; $$.logif = 0; }
2790 | USER { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
2791 | GROUP { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
2797 if (strncmp($2, "pflog", 5)) {
2798 yyerror("%s: should be a pflog interface", $2);
2802 i = strtonum($2 + 5, 0, 255, &errstr);
2804 yyerror("%s: %s", $2, errstr);
2813 interface : /* empty */ { $$ = NULL; }
2814 | ON if_item_not { $$ = $2; }
2815 | ON '{' optnl if_list '}' { $$ = $4; }
2818 if_list : if_item_not optnl { $$ = $1; }
2819 | if_list comma if_item_not optnl {
2820 $1->tail->next = $3;
2826 if_item_not : not if_item { $$ = $2; $$->not = $1; }
2830 struct node_host *n;
2832 $$ = calloc(1, sizeof(struct node_if));
2834 err(1, "if_item: calloc");
2835 if (strlcpy($$->ifname, $1, sizeof($$->ifname)) >=
2836 sizeof($$->ifname)) {
2839 yyerror("interface name too long");
2843 if ((n = ifa_exists($1)) != NULL)
2844 $$->ifa_flags = n->ifa_flags;
2853 af : /* empty */ { $$ = 0; }
2854 | INET { $$ = AF_INET; }
2855 | INET6 { $$ = AF_INET6; }
2858 proto : /* empty */ { $$ = NULL; }
2859 | PROTO proto_item { $$ = $2; }
2860 | PROTO '{' optnl proto_list '}' { $$ = $4; }
2863 proto_list : proto_item optnl { $$ = $1; }
2864 | proto_list comma proto_item optnl {
2865 $1->tail->next = $3;
2871 proto_item : protoval {
2876 yyerror("proto 0 cannot be used");
2879 $$ = calloc(1, sizeof(struct node_proto));
2881 err(1, "proto_item: calloc");
2891 p = getprotobyname($1);
2893 yyerror("unknown protocol %s", $1);
2901 if ($1 < 0 || $1 > 255) {
2902 yyerror("protocol outside range");
2922 os : /* empty */ { $$ = NULL; }
2923 | OS xos { $$ = $2; }
2924 | OS '{' optnl os_list '}' { $$ = $4; }
2928 $$ = calloc(1, sizeof(struct node_os));
2930 err(1, "os: calloc");
2936 os_list : xos optnl { $$ = $1; }
2937 | os_list comma xos optnl {
2938 $1->tail->next = $3;
2944 from : /* empty */ {
2958 if (disallow_urpf_failed($2.host, "\"urpf-failed\" is "
2959 "not permitted in a destination address"))
2965 ipportspec : ipspec {
2969 | ipspec PORT portspec {
2983 ipspec : ANY { $$ = NULL; }
2984 | xhost { $$ = $1; }
2985 | '{' optnl host_list '}' { $$ = $3; }
2988 toipspec : TO ipspec { $$ = $2; }
2989 | /* empty */ { $$ = NULL; }
2992 host_list : ipspec optnl { $$ = $1; }
2993 | host_list comma ipspec optnl {
2996 else if ($1 == NULL)
2999 $1->tail->next = $3;
3000 $1->tail = $3->tail;
3007 struct node_host *n;
3009 for (n = $2; n != NULL; n = n->next)
3014 $$ = calloc(1, sizeof(struct node_host));
3016 err(1, "xhost: calloc");
3017 $$->addr.type = PF_ADDR_NOROUTE;
3023 $$ = calloc(1, sizeof(struct node_host));
3025 err(1, "xhost: calloc");
3026 $$->addr.type = PF_ADDR_URPFFAILED;
3034 if (($$ = host($1)) == NULL) {
3035 /* error. "any" is handled elsewhere */
3037 yyerror("could not parse host specification");
3043 | STRING '-' STRING {
3044 struct node_host *b, *e;
3046 if ((b = host($1)) == NULL || (e = host($3)) == NULL) {
3049 yyerror("could not parse host specification");
3052 if (b->af != e->af ||
3053 b->addr.type != PF_ADDR_ADDRMASK ||
3054 e->addr.type != PF_ADDR_ADDRMASK ||
3055 unmask(&b->addr.v.a.mask, b->af) !=
3056 (b->af == AF_INET ? 32 : 128) ||
3057 unmask(&e->addr.v.a.mask, e->af) !=
3058 (e->af == AF_INET ? 32 : 128) ||
3059 b->next != NULL || b->not ||
3060 e->next != NULL || e->not) {
3065 yyerror("invalid address range");
3068 memcpy(&b->addr.v.a.mask, &e->addr.v.a.addr,
3069 sizeof(b->addr.v.a.mask));
3070 b->addr.type = PF_ADDR_RANGE;
3076 | STRING '/' NUMBER {
3079 if (asprintf(&buf, "%s/%lld", $1, (long long)$3) == -1)
3080 err(1, "host: asprintf");
3082 if (($$ = host(buf)) == NULL) {
3083 /* error. "any" is handled elsewhere */
3085 yyerror("could not parse host specification");
3090 | NUMBER '/' NUMBER {
3093 /* ie. for 10/8 parsing */
3095 if (asprintf(&buf, "%lld/%lld", (long long)$1, (long long)$3) == -1)
3097 if (asprintf(&buf, "%lld/%lld", $1, $3) == -1)
3099 err(1, "host: asprintf");
3100 if (($$ = host(buf)) == NULL) {
3101 /* error. "any" is handled elsewhere */
3103 yyerror("could not parse host specification");
3109 | dynaddr '/' NUMBER {
3110 struct node_host *n;
3112 if ($3 < 0 || $3 > 128) {
3113 yyerror("bit number too big");
3117 for (n = $1; n != NULL; n = n->next)
3121 if (strlen($2) >= PF_TABLE_NAME_SIZE) {
3122 yyerror("table name '%s' too long", $2);
3126 $$ = calloc(1, sizeof(struct node_host));
3128 err(1, "host: calloc");
3129 $$->addr.type = PF_ADDR_TABLE;
3130 if (strlcpy($$->addr.v.tblname, $2,
3131 sizeof($$->addr.v.tblname)) >=
3132 sizeof($$->addr.v.tblname))
3133 errx(1, "host: strlcpy");
3144 if (atoul($1, &ulval) == -1) {
3145 yyerror("%s is not a number", $1);
3154 dynaddr : '(' STRING ')' {
3159 if (!isalpha(op[0])) {
3160 yyerror("invalid interface name '%s'", op);
3164 while ((p = strrchr($2, ':')) != NULL) {
3165 if (!strcmp(p+1, "network"))
3166 flags |= PFI_AFLAG_NETWORK;
3167 else if (!strcmp(p+1, "broadcast"))
3168 flags |= PFI_AFLAG_BROADCAST;
3169 else if (!strcmp(p+1, "peer"))
3170 flags |= PFI_AFLAG_PEER;
3171 else if (!strcmp(p+1, "0"))
3172 flags |= PFI_AFLAG_NOALIAS;
3174 yyerror("interface %s has bad modifier",
3181 if (flags & (flags - 1) & PFI_AFLAG_MODEMASK) {
3183 yyerror("illegal combination of "
3184 "interface modifiers");
3187 $$ = calloc(1, sizeof(struct node_host));
3189 err(1, "address: calloc");
3191 set_ipmask($$, 128);
3192 $$->addr.type = PF_ADDR_DYNIFTL;
3193 $$->addr.iflags = flags;
3194 if (strlcpy($$->addr.v.ifname, $2,
3195 sizeof($$->addr.v.ifname)) >=
3196 sizeof($$->addr.v.ifname)) {
3199 yyerror("interface name too long");
3208 portspec : port_item { $$ = $1; }
3209 | '{' optnl port_list '}' { $$ = $3; }
3212 port_list : port_item optnl { $$ = $1; }
3213 | port_list comma port_item optnl {
3214 $1->tail->next = $3;
3220 port_item : portrange {
3221 $$ = calloc(1, sizeof(struct node_port));
3223 err(1, "port_item: calloc");
3233 | unaryop portrange {
3235 yyerror("':' cannot be used with an other "
3239 $$ = calloc(1, sizeof(struct node_port));
3241 err(1, "port_item: calloc");
3248 | portrange PORTBINARY portrange {
3250 yyerror("':' cannot be used with an other "
3254 $$ = calloc(1, sizeof(struct node_port));
3256 err(1, "port_item: calloc");
3265 portplain : numberstring {
3266 if (parseport($1, &$$, 0) == -1) {
3274 portrange : numberstring {
3275 if (parseport($1, &$$, PPORT_RANGE) == -1) {
3283 uids : uid_item { $$ = $1; }
3284 | '{' optnl uid_list '}' { $$ = $3; }
3287 uid_list : uid_item optnl { $$ = $1; }
3288 | uid_list comma uid_item optnl {
3289 $1->tail->next = $3;
3296 $$ = calloc(1, sizeof(struct node_uid));
3298 err(1, "uid_item: calloc");
3306 if ($2 == UID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
3307 yyerror("user unknown requires operator = or "
3311 $$ = calloc(1, sizeof(struct node_uid));
3313 err(1, "uid_item: calloc");
3320 | uid PORTBINARY uid {
3321 if ($1 == UID_MAX || $3 == UID_MAX) {
3322 yyerror("user unknown requires operator = or "
3326 $$ = calloc(1, sizeof(struct node_uid));
3328 err(1, "uid_item: calloc");
3338 if (!strcmp($1, "unknown"))
3343 if ((pw = getpwnam($1)) == NULL) {
3344 yyerror("unknown user %s", $1);
3353 if ($1 < 0 || $1 >= UID_MAX) {
3354 yyerror("illegal uid value %lu", $1);
3361 gids : gid_item { $$ = $1; }
3362 | '{' optnl gid_list '}' { $$ = $3; }
3365 gid_list : gid_item optnl { $$ = $1; }
3366 | gid_list comma gid_item optnl {
3367 $1->tail->next = $3;
3374 $$ = calloc(1, sizeof(struct node_gid));
3376 err(1, "gid_item: calloc");
3384 if ($2 == GID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
3385 yyerror("group unknown requires operator = or "
3389 $$ = calloc(1, sizeof(struct node_gid));
3391 err(1, "gid_item: calloc");
3398 | gid PORTBINARY gid {
3399 if ($1 == GID_MAX || $3 == GID_MAX) {
3400 yyerror("group unknown requires operator = or "
3404 $$ = calloc(1, sizeof(struct node_gid));
3406 err(1, "gid_item: calloc");
3416 if (!strcmp($1, "unknown"))
3421 if ((grp = getgrnam($1)) == NULL) {
3422 yyerror("unknown group %s", $1);
3431 if ($1 < 0 || $1 >= GID_MAX) {
3432 yyerror("illegal gid value %lu", $1);
3442 if ((f = parse_flags($1)) < 0) {
3443 yyerror("bad flags %s", $1);
3452 flags : FLAGS flag '/' flag { $$.b1 = $2.b1; $$.b2 = $4.b1; }
3453 | FLAGS '/' flag { $$.b1 = 0; $$.b2 = $3.b1; }
3454 | FLAGS ANY { $$.b1 = 0; $$.b2 = 0; }
3457 icmpspec : ICMPTYPE icmp_item { $$ = $2; }
3458 | ICMPTYPE '{' optnl icmp_list '}' { $$ = $4; }
3459 | ICMP6TYPE icmp6_item { $$ = $2; }
3460 | ICMP6TYPE '{' optnl icmp6_list '}' { $$ = $4; }
3463 icmp_list : icmp_item optnl { $$ = $1; }
3464 | icmp_list comma icmp_item optnl {
3465 $1->tail->next = $3;
3471 icmp6_list : icmp6_item optnl { $$ = $1; }
3472 | icmp6_list comma icmp6_item optnl {
3473 $1->tail->next = $3;
3479 icmp_item : icmptype {
3480 $$ = calloc(1, sizeof(struct node_icmp));
3482 err(1, "icmp_item: calloc");
3485 $$->proto = IPPROTO_ICMP;
3489 | icmptype CODE STRING {
3490 const struct icmpcodeent *p;
3492 if ((p = geticmpcodebyname($1-1, $3, AF_INET)) == NULL) {
3493 yyerror("unknown icmp-code %s", $3);
3499 $$ = calloc(1, sizeof(struct node_icmp));
3501 err(1, "icmp_item: calloc");
3503 $$->code = p->code + 1;
3504 $$->proto = IPPROTO_ICMP;
3508 | icmptype CODE NUMBER {
3509 if ($3 < 0 || $3 > 255) {
3510 yyerror("illegal icmp-code %lu", $3);
3513 $$ = calloc(1, sizeof(struct node_icmp));
3515 err(1, "icmp_item: calloc");
3518 $$->proto = IPPROTO_ICMP;
3524 icmp6_item : icmp6type {
3525 $$ = calloc(1, sizeof(struct node_icmp));
3527 err(1, "icmp_item: calloc");
3530 $$->proto = IPPROTO_ICMPV6;
3534 | icmp6type CODE STRING {
3535 const struct icmpcodeent *p;
3537 if ((p = geticmpcodebyname($1-1, $3, AF_INET6)) == NULL) {
3538 yyerror("unknown icmp6-code %s", $3);
3544 $$ = calloc(1, sizeof(struct node_icmp));
3546 err(1, "icmp_item: calloc");
3548 $$->code = p->code + 1;
3549 $$->proto = IPPROTO_ICMPV6;
3553 | icmp6type CODE NUMBER {
3554 if ($3 < 0 || $3 > 255) {
3555 yyerror("illegal icmp-code %lu", $3);
3558 $$ = calloc(1, sizeof(struct node_icmp));
3560 err(1, "icmp_item: calloc");
3563 $$->proto = IPPROTO_ICMPV6;
3570 const struct icmptypeent *p;
3572 if ((p = geticmptypebyname($1, AF_INET)) == NULL) {
3573 yyerror("unknown icmp-type %s", $1);
3581 if ($1 < 0 || $1 > 255) {
3582 yyerror("illegal icmp-type %lu", $1);
3589 icmp6type : STRING {
3590 const struct icmptypeent *p;
3592 if ((p = geticmptypebyname($1, AF_INET6)) ==
3594 yyerror("unknown icmp6-type %s", $1);
3602 if ($1 < 0 || $1 > 255) {
3603 yyerror("illegal icmp6-type %lu", $1);
3614 if (map_tos($1, &val))
3616 else if ($1[0] == '0' && $1[1] == 'x') {
3618 $$ = strtoul($1, &end, 16);
3619 if (errno || *end != '\0')
3622 $$ = 256; /* flag bad argument */
3623 if ($$ < 0 || $$ > 255) {
3624 yyerror("illegal tos value %s", $1);
3632 if ($$ < 0 || $$ > 255) {
3633 yyerror("illegal tos value %s", $1);
3639 sourcetrack : SOURCETRACK { $$ = PF_SRCTRACK; }
3640 | SOURCETRACK GLOBAL { $$ = PF_SRCTRACK_GLOBAL; }
3641 | SOURCETRACK RULE { $$ = PF_SRCTRACK_RULE; }
3644 statelock : IFBOUND {
3645 $$ = PFRULE_IFBOUND;
3656 | KEEP STATE state_opt_spec {
3657 $$.action = PF_STATE_NORMAL;
3660 | MODULATE STATE state_opt_spec {
3661 $$.action = PF_STATE_MODULATE;
3664 | SYNPROXY STATE state_opt_spec {
3665 $$.action = PF_STATE_SYNPROXY;
3670 flush : /* empty */ { $$ = 0; }
3671 | FLUSH { $$ = PF_FLUSH; }
3673 $$ = PF_FLUSH | PF_FLUSH_GLOBAL;
3677 state_opt_spec : '(' state_opt_list ')' { $$ = $2; }
3678 | /* empty */ { $$ = NULL; }
3681 state_opt_list : state_opt_item { $$ = $1; }
3682 | state_opt_list comma state_opt_item {
3683 $1->tail->next = $3;
3689 state_opt_item : MAXIMUM NUMBER {
3690 if ($2 < 0 || $2 > UINT_MAX) {
3691 yyerror("only positive values permitted");
3694 $$ = calloc(1, sizeof(struct node_state_opt));
3696 err(1, "state_opt_item: calloc");
3697 $$->type = PF_STATE_OPT_MAX;
3698 $$->data.max_states = $2;
3703 $$ = calloc(1, sizeof(struct node_state_opt));
3705 err(1, "state_opt_item: calloc");
3706 $$->type = PF_STATE_OPT_NOSYNC;
3710 | MAXSRCSTATES NUMBER {
3711 if ($2 < 0 || $2 > UINT_MAX) {
3712 yyerror("only positive values permitted");
3715 $$ = calloc(1, sizeof(struct node_state_opt));
3717 err(1, "state_opt_item: calloc");
3718 $$->type = PF_STATE_OPT_MAX_SRC_STATES;
3719 $$->data.max_src_states = $2;
3723 | MAXSRCCONN NUMBER {
3724 if ($2 < 0 || $2 > UINT_MAX) {
3725 yyerror("only positive values permitted");
3728 $$ = calloc(1, sizeof(struct node_state_opt));
3730 err(1, "state_opt_item: calloc");
3731 $$->type = PF_STATE_OPT_MAX_SRC_CONN;
3732 $$->data.max_src_conn = $2;
3736 | MAXSRCCONNRATE NUMBER '/' NUMBER {
3737 if ($2 < 0 || $2 > UINT_MAX ||
3738 $4 < 0 || $4 > UINT_MAX) {
3739 yyerror("only positive values permitted");
3742 $$ = calloc(1, sizeof(struct node_state_opt));
3744 err(1, "state_opt_item: calloc");
3745 $$->type = PF_STATE_OPT_MAX_SRC_CONN_RATE;
3746 $$->data.max_src_conn_rate.limit = $2;
3747 $$->data.max_src_conn_rate.seconds = $4;
3751 | OVERLOAD '<' STRING '>' flush {
3752 if (strlen($3) >= PF_TABLE_NAME_SIZE) {
3753 yyerror("table name '%s' too long", $3);
3757 $$ = calloc(1, sizeof(struct node_state_opt));
3759 err(1, "state_opt_item: calloc");
3760 if (strlcpy($$->data.overload.tblname, $3,
3761 PF_TABLE_NAME_SIZE) >= PF_TABLE_NAME_SIZE)
3762 errx(1, "state_opt_item: strlcpy");
3764 $$->type = PF_STATE_OPT_OVERLOAD;
3765 $$->data.overload.flush = $5;
3769 | MAXSRCNODES NUMBER {
3770 if ($2 < 0 || $2 > UINT_MAX) {
3771 yyerror("only positive values permitted");
3774 $$ = calloc(1, sizeof(struct node_state_opt));
3776 err(1, "state_opt_item: calloc");
3777 $$->type = PF_STATE_OPT_MAX_SRC_NODES;
3778 $$->data.max_src_nodes = $2;
3783 $$ = calloc(1, sizeof(struct node_state_opt));
3785 err(1, "state_opt_item: calloc");
3786 $$->type = PF_STATE_OPT_SRCTRACK;
3787 $$->data.src_track = $1;
3792 $$ = calloc(1, sizeof(struct node_state_opt));
3794 err(1, "state_opt_item: calloc");
3795 $$->type = PF_STATE_OPT_STATELOCK;
3796 $$->data.statelock = $1;
3801 $$ = calloc(1, sizeof(struct node_state_opt));
3803 err(1, "state_opt_item: calloc");
3804 $$->type = PF_STATE_OPT_SLOPPY;
3811 if ($2 < 0 || $2 > UINT_MAX) {
3812 yyerror("only positive values permitted");
3815 for (i = 0; pf_timeouts[i].name &&
3816 strcmp(pf_timeouts[i].name, $1); ++i)
3818 if (!pf_timeouts[i].name) {
3819 yyerror("illegal timeout name %s", $1);
3823 if (strchr(pf_timeouts[i].name, '.') == NULL) {
3824 yyerror("illegal state timeout %s", $1);
3829 $$ = calloc(1, sizeof(struct node_state_opt));
3831 err(1, "state_opt_item: calloc");
3832 $$->type = PF_STATE_OPT_TIMEOUT;
3833 $$->data.timeout.number = pf_timeouts[i].timeout;
3834 $$->data.timeout.seconds = $2;
3840 label : LABEL STRING {
3845 qname : QUEUE STRING {
3849 | QUEUE '(' STRING ')' {
3853 | QUEUE '(' STRING comma STRING ')' {
3859 no : /* empty */ { $$ = 0; }
3863 portstar : numberstring {
3864 if (parseport($1, &$$, PPORT_RANGE|PPORT_STAR) == -1) {
3872 redirspec : host { $$ = $1; }
3873 | '{' optnl redir_host_list '}' { $$ = $3; }
3876 redir_host_list : host optnl { $$ = $1; }
3877 | redir_host_list comma host optnl {
3878 $1->tail->next = $3;
3879 $1->tail = $3->tail;
3884 redirpool : /* empty */ { $$ = NULL; }
3886 $$ = calloc(1, sizeof(struct redirection));
3888 err(1, "redirection: calloc");
3890 $$->rport.a = $$->rport.b = $$->rport.t = 0;
3892 | ARROW redirspec PORT portstar {
3893 $$ = calloc(1, sizeof(struct redirection));
3895 err(1, "redirection: calloc");
3901 hashkey : /* empty */
3903 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3905 err(1, "hashkey: calloc");
3906 $$->key32[0] = arc4random();
3907 $$->key32[1] = arc4random();
3908 $$->key32[2] = arc4random();
3909 $$->key32[3] = arc4random();
3913 if (!strncmp($1, "0x", 2)) {
3914 if (strlen($1) != 34) {
3916 yyerror("hex key must be 128 bits "
3917 "(32 hex digits) long");
3920 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3922 err(1, "hashkey: calloc");
3924 if (sscanf($1, "0x%8x%8x%8x%8x",
3925 &$$->key32[0], &$$->key32[1],
3926 &$$->key32[2], &$$->key32[3]) != 4) {
3929 yyerror("invalid hex key");
3935 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3937 err(1, "hashkey: calloc");
3939 MD5Update(&context, (unsigned char *)$1,
3941 MD5Final((unsigned char *)$$, &context);
3942 HTONL($$->key32[0]);
3943 HTONL($$->key32[1]);
3944 HTONL($$->key32[2]);
3945 HTONL($$->key32[3]);
3951 pool_opts : { bzero(&pool_opts, sizeof pool_opts); }
3955 bzero(&pool_opts, sizeof pool_opts);
3960 pool_opts_l : pool_opts_l pool_opt
3964 pool_opt : BITMASK {
3965 if (pool_opts.type) {
3966 yyerror("pool type cannot be redefined");
3969 pool_opts.type = PF_POOL_BITMASK;
3972 if (pool_opts.type) {
3973 yyerror("pool type cannot be redefined");
3976 pool_opts.type = PF_POOL_RANDOM;
3978 | SOURCEHASH hashkey {
3979 if (pool_opts.type) {
3980 yyerror("pool type cannot be redefined");
3983 pool_opts.type = PF_POOL_SRCHASH;
3987 if (pool_opts.type) {
3988 yyerror("pool type cannot be redefined");
3991 pool_opts.type = PF_POOL_ROUNDROBIN;
3994 if (pool_opts.staticport) {
3995 yyerror("static-port cannot be redefined");
3998 pool_opts.staticport = 1;
4001 if (filter_opts.marker & POM_STICKYADDRESS) {
4002 yyerror("sticky-address cannot be redefined");
4005 pool_opts.marker |= POM_STICKYADDRESS;
4006 pool_opts.opts |= PF_POOL_STICKYADDR;
4010 redirection : /* empty */ { $$ = NULL; }
4012 $$ = calloc(1, sizeof(struct redirection));
4014 err(1, "redirection: calloc");
4016 $$->rport.a = $$->rport.b = $$->rport.t = 0;
4018 | ARROW host PORT portstar {
4019 $$ = calloc(1, sizeof(struct redirection));
4021 err(1, "redirection: calloc");
4027 natpasslog : /* empty */ { $$.b1 = $$.b2 = 0; $$.w2 = 0; }
4028 | PASS { $$.b1 = 1; $$.b2 = 0; $$.w2 = 0; }
4029 | PASS log { $$.b1 = 1; $$.b2 = $2.log; $$.w2 = $2.logif; }
4030 | log { $$.b1 = 0; $$.b2 = $1.log; $$.w2 = $1.logif; }
4033 nataction : no NAT natpasslog {
4035 yyerror("\"pass\" not valid with \"no\"");
4046 | no RDR natpasslog {
4048 yyerror("\"pass\" not valid with \"no\"");
4061 natrule : nataction interface af proto fromto tag tagged rtable
4066 if (check_rulestate(PFCTL_STATE_NAT))
4069 memset(&r, 0, sizeof(r));
4078 if ($5.src.host && $5.src.host->af &&
4079 !$5.src.host->ifindex)
4080 r.af = $5.src.host->af;
4081 else if ($5.dst.host && $5.dst.host->af &&
4082 !$5.dst.host->ifindex)
4083 r.af = $5.dst.host->af;
4087 if (strlcpy(r.tagname, $6, PF_TAG_NAME_SIZE) >=
4089 yyerror("tag too long, max %u chars",
4090 PF_TAG_NAME_SIZE - 1);
4095 if (strlcpy(r.match_tagname, $7.name,
4096 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4097 yyerror("tag too long, max %u chars",
4098 PF_TAG_NAME_SIZE - 1);
4101 r.match_tag_not = $7.neg;
4104 if (r.action == PF_NONAT || r.action == PF_NORDR) {
4106 yyerror("translation rule with 'no' "
4107 "does not need '->'");
4111 if ($9 == NULL || $9->host == NULL) {
4112 yyerror("translation rule requires '-> "
4116 if (!r.af && ! $9->host->ifindex)
4117 r.af = $9->host->af;
4119 remove_invalid_hosts(&$9->host, &r.af);
4120 if (invalid_redirect($9->host, r.af))
4122 if (check_netmask($9->host, r.af))
4125 r.rpool.proxy_port[0] = ntohs($9->rport.a);
4129 if (!$9->rport.b && $9->rport.t &&
4130 $5.dst.port != NULL) {
4131 r.rpool.proxy_port[1] =
4132 ntohs($9->rport.a) +
4134 $5.dst.port->port[1]) -
4136 $5.dst.port->port[0]));
4138 r.rpool.proxy_port[1] =
4142 r.rpool.proxy_port[1] =
4144 if (!r.rpool.proxy_port[0] &&
4145 !r.rpool.proxy_port[1]) {
4146 r.rpool.proxy_port[0] =
4147 PF_NAT_PROXY_PORT_LOW;
4148 r.rpool.proxy_port[1] =
4149 PF_NAT_PROXY_PORT_HIGH;
4150 } else if (!r.rpool.proxy_port[1])
4151 r.rpool.proxy_port[1] =
4152 r.rpool.proxy_port[0];
4158 r.rpool.opts = $10.type;
4159 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
4160 PF_POOL_NONE && ($9->host->next != NULL ||
4161 $9->host->addr.type == PF_ADDR_TABLE ||
4162 DYNIF_MULTIADDR($9->host->addr)))
4163 r.rpool.opts = PF_POOL_ROUNDROBIN;
4164 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4165 PF_POOL_ROUNDROBIN &&
4166 disallow_table($9->host, "tables are only "
4167 "supported in round-robin redirection "
4170 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4171 PF_POOL_ROUNDROBIN &&
4172 disallow_alias($9->host, "interface (%s) "
4173 "is only supported in round-robin "
4174 "redirection pools"))
4176 if ($9->host->next != NULL) {
4177 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4178 PF_POOL_ROUNDROBIN) {
4179 yyerror("only round-robin "
4180 "valid for multiple "
4181 "redirection addresses");
4187 if ($10.key != NULL)
4188 memcpy(&r.rpool.key, $10.key,
4189 sizeof(struct pf_poolhashkey));
4192 r.rpool.opts |= $10.opts;
4194 if ($10.staticport) {
4195 if (r.action != PF_NAT) {
4196 yyerror("the 'static-port' option is "
4197 "only valid with nat rules");
4200 if (r.rpool.proxy_port[0] !=
4201 PF_NAT_PROXY_PORT_LOW &&
4202 r.rpool.proxy_port[1] !=
4203 PF_NAT_PROXY_PORT_HIGH) {
4204 yyerror("the 'static-port' option can't"
4205 " be used when specifying a port"
4209 r.rpool.proxy_port[0] = 0;
4210 r.rpool.proxy_port[1] = 0;
4213 expand_rule(&r, $2, $9 == NULL ? NULL : $9->host, $4,
4214 $5.src_os, $5.src.host, $5.src.port, $5.dst.host,
4215 $5.dst.port, 0, 0, 0, "");
4220 binatrule : no BINAT natpasslog interface af proto FROM ipspec toipspec tag
4221 tagged rtable redirection
4223 struct pf_rule binat;
4224 struct pf_pooladdr *pa;
4226 if (check_rulestate(PFCTL_STATE_NAT))
4228 if (disallow_urpf_failed($9, "\"urpf-failed\" is not "
4229 "permitted as a binat destination"))
4232 memset(&binat, 0, sizeof(binat));
4235 yyerror("\"pass\" not valid with \"no\"");
4239 binat.action = PF_NOBINAT;
4241 binat.action = PF_BINAT;
4242 binat.natpass = $3.b1;
4244 binat.logif = $3.w2;
4246 if (!binat.af && $8 != NULL && $8->af)
4248 if (!binat.af && $9 != NULL && $9->af)
4251 if (!binat.af && $13 != NULL && $13->host)
4252 binat.af = $13->host->af;
4254 yyerror("address family (inet/inet6) "
4260 memcpy(binat.ifname, $4->ifname,
4261 sizeof(binat.ifname));
4262 binat.ifnot = $4->not;
4267 if (strlcpy(binat.tagname, $10,
4268 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4269 yyerror("tag too long, max %u chars",
4270 PF_TAG_NAME_SIZE - 1);
4274 if (strlcpy(binat.match_tagname, $11.name,
4275 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4276 yyerror("tag too long, max %u chars",
4277 PF_TAG_NAME_SIZE - 1);
4280 binat.match_tag_not = $11.neg;
4281 binat.rtableid = $12;
4284 binat.proto = $6->proto;
4288 if ($8 != NULL && disallow_table($8, "invalid use of "
4289 "table <%s> as the source address of a binat rule"))
4291 if ($8 != NULL && disallow_alias($8, "invalid use of "
4292 "interface (%s) as the source address of a binat "
4295 if ($13 != NULL && $13->host != NULL && disallow_table(
4296 $13->host, "invalid use of table <%s> as the "
4297 "redirect address of a binat rule"))
4299 if ($13 != NULL && $13->host != NULL && disallow_alias(
4300 $13->host, "invalid use of interface (%s) as the "
4301 "redirect address of a binat rule"))
4306 yyerror("multiple binat ip addresses");
4309 if ($8->addr.type == PF_ADDR_DYNIFTL)
4311 if ($8->af != binat.af) {
4312 yyerror("binat ip versions must match");
4315 if (check_netmask($8, binat.af))
4317 memcpy(&binat.src.addr, &$8->addr,
4318 sizeof(binat.src.addr));
4323 yyerror("multiple binat ip addresses");
4326 if ($9->af != binat.af && $9->af) {
4327 yyerror("binat ip versions must match");
4330 if (check_netmask($9, binat.af))
4332 memcpy(&binat.dst.addr, &$9->addr,
4333 sizeof(binat.dst.addr));
4334 binat.dst.neg = $9->not;
4338 if (binat.action == PF_NOBINAT) {
4340 yyerror("'no binat' rule does not need"
4345 if ($13 == NULL || $13->host == NULL) {
4346 yyerror("'binat' rule requires"
4351 remove_invalid_hosts(&$13->host, &binat.af);
4352 if (invalid_redirect($13->host, binat.af))
4354 if ($13->host->next != NULL) {
4355 yyerror("binat rule must redirect to "
4356 "a single address");
4359 if (check_netmask($13->host, binat.af))
4362 if (!PF_AZERO(&binat.src.addr.v.a.mask,
4364 !PF_AEQ(&binat.src.addr.v.a.mask,
4365 &$13->host->addr.v.a.mask, binat.af)) {
4366 yyerror("'binat' source mask and "
4367 "redirect mask must be the same");
4371 TAILQ_INIT(&binat.rpool.list);
4372 pa = calloc(1, sizeof(struct pf_pooladdr));
4374 err(1, "binat: calloc");
4375 pa->addr = $13->host->addr;
4377 TAILQ_INSERT_TAIL(&binat.rpool.list,
4383 pfctl_add_rule(pf, &binat, "");
4387 tag : /* empty */ { $$ = NULL; }
4388 | TAG STRING { $$ = $2; }
4391 tagged : /* empty */ { $$.neg = 0; $$.name = NULL; }
4392 | not TAGGED string { $$.neg = $1; $$.name = $3; }
4395 rtable : /* empty */ { $$ = -1; }
4397 if ($2 < 0 || $2 > rt_tableid_max()) {
4398 yyerror("invalid rtable id");
4405 route_host : STRING {
4406 $$ = calloc(1, sizeof(struct node_host));
4408 err(1, "route_host: calloc");
4410 set_ipmask($$, 128);
4414 | '(' STRING host ')' {
4415 struct node_host *n;
4418 for (n = $3; n != NULL; n = n->next)
4423 route_host_list : route_host optnl { $$ = $1; }
4424 | route_host_list comma route_host optnl {
4427 if ($1->af != $3->af) {
4428 yyerror("all pool addresses must be in the "
4429 "same address family");
4432 $1->tail->next = $3;
4433 $1->tail = $3->tail;
4438 routespec : route_host { $$ = $1; }
4439 | '{' optnl route_host_list '}' { $$ = $3; }
4442 route : /* empty */ {
4448 /* backwards-compat */
4453 | ROUTETO routespec pool_opts {
4456 $$.pool_opts = $3.type | $3.opts;
4460 | REPLYTO routespec pool_opts {
4463 $$.pool_opts = $3.type | $3.opts;
4467 | DUPTO routespec pool_opts {
4470 $$.pool_opts = $3.type | $3.opts;
4476 timeout_spec : STRING NUMBER
4478 if (check_rulestate(PFCTL_STATE_OPTION)) {
4482 if ($2 < 0 || $2 > UINT_MAX) {
4483 yyerror("only positive values permitted");
4486 if (pfctl_set_timeout(pf, $1, $2, 0) != 0) {
4487 yyerror("unknown timeout %s", $1);
4494 if (check_rulestate(PFCTL_STATE_OPTION))
4496 if ($2 < 0 || $2 > UINT_MAX) {
4497 yyerror("only positive values permitted");
4500 if (pfctl_set_timeout(pf, "interval", $2, 0) != 0)
4505 timeout_list : timeout_list comma timeout_spec optnl
4506 | timeout_spec optnl
4509 limit_spec : STRING NUMBER
4511 if (check_rulestate(PFCTL_STATE_OPTION)) {
4515 if ($2 < 0 || $2 > UINT_MAX) {
4516 yyerror("only positive values permitted");
4519 if (pfctl_set_limit(pf, $1, $2) != 0) {
4520 yyerror("unable to set limit %s %u", $1, $2);
4528 limit_list : limit_list comma limit_spec optnl
4536 yesno : NO { $$ = 0; }
4538 if (!strcmp($1, "yes"))
4541 yyerror("invalid value '%s', expected 'yes' "
4550 unaryop : '=' { $$ = PF_OP_EQ; }
4551 | '!' '=' { $$ = PF_OP_NE; }
4552 | '<' '=' { $$ = PF_OP_LE; }
4553 | '<' { $$ = PF_OP_LT; }
4554 | '>' '=' { $$ = PF_OP_GE; }
4555 | '>' { $$ = PF_OP_GT; }
4561 yyerror(const char *fmt, ...)
4567 fprintf(stderr, "%s:%d: ", file->name, yylval.lineno);
4568 vfprintf(stderr, fmt, ap);
4569 fprintf(stderr, "\n");
4575 disallow_table(struct node_host *h, const char *fmt)
4577 for (; h != NULL; h = h->next)
4578 if (h->addr.type == PF_ADDR_TABLE) {
4579 yyerror(fmt, h->addr.v.tblname);
4586 disallow_urpf_failed(struct node_host *h, const char *fmt)
4588 for (; h != NULL; h = h->next)
4589 if (h->addr.type == PF_ADDR_URPFFAILED) {
4597 disallow_alias(struct node_host *h, const char *fmt)
4599 for (; h != NULL; h = h->next)
4600 if (DYNIF_MULTIADDR(h->addr)) {
4601 yyerror(fmt, h->addr.v.tblname);
4608 rule_consistent(struct pf_rule *r, int anchor_call)
4612 switch (r->action) {
4617 problems = filter_consistent(r, anchor_call);
4621 problems = nat_consistent(r);
4625 problems = rdr_consistent(r);
4636 filter_consistent(struct pf_rule *r, int anchor_call)
4640 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP &&
4641 (r->src.port_op || r->dst.port_op)) {
4642 yyerror("port only applies to tcp/udp");
4645 if (r->proto != IPPROTO_ICMP && r->proto != IPPROTO_ICMPV6 &&
4646 (r->type || r->code)) {
4647 yyerror("icmp-type/code only applies to icmp");
4650 if (!r->af && (r->type || r->code)) {
4651 yyerror("must indicate address family with icmp-type/code");
4654 if (r->overload_tblname[0] &&
4655 r->max_src_conn == 0 && r->max_src_conn_rate.seconds == 0) {
4656 yyerror("'overload' requires 'max-src-conn' "
4657 "or 'max-src-conn-rate'");
4660 if ((r->proto == IPPROTO_ICMP && r->af == AF_INET6) ||
4661 (r->proto == IPPROTO_ICMPV6 && r->af == AF_INET)) {
4662 yyerror("proto %s doesn't match address family %s",
4663 r->proto == IPPROTO_ICMP ? "icmp" : "icmp6",
4664 r->af == AF_INET ? "inet" : "inet6");
4667 if (r->allow_opts && r->action != PF_PASS) {
4668 yyerror("allow-opts can only be specified for pass rules");
4671 if (r->rule_flag & PFRULE_FRAGMENT && (r->src.port_op ||
4672 r->dst.port_op || r->flagset || r->type || r->code)) {
4673 yyerror("fragments can be filtered only on IP header fields");
4676 if (r->rule_flag & PFRULE_RETURNRST && r->proto != IPPROTO_TCP) {
4677 yyerror("return-rst can only be applied to TCP rules");
4680 if (r->max_src_nodes && !(r->rule_flag & PFRULE_RULESRCTRACK)) {
4681 yyerror("max-src-nodes requires 'source-track rule'");
4684 if (r->action == PF_DROP && r->keep_state) {
4685 yyerror("keep state on block rules doesn't make sense");
4688 if (r->rule_flag & PFRULE_STATESLOPPY &&
4689 (r->keep_state == PF_STATE_MODULATE ||
4690 r->keep_state == PF_STATE_SYNPROXY)) {
4691 yyerror("sloppy state matching cannot be used with "
4692 "synproxy state or modulate state");
4699 nat_consistent(struct pf_rule *r)
4701 return (0); /* yeah! */
4705 rdr_consistent(struct pf_rule *r)
4709 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP) {
4710 if (r->src.port_op) {
4711 yyerror("src port only applies to tcp/udp");
4714 if (r->dst.port_op) {
4715 yyerror("dst port only applies to tcp/udp");
4718 if (r->rpool.proxy_port[0]) {
4719 yyerror("rpool port only applies to tcp/udp");
4723 if (r->dst.port_op &&
4724 r->dst.port_op != PF_OP_EQ && r->dst.port_op != PF_OP_RRG) {
4725 yyerror("invalid port operator for rdr destination port");
4732 process_tabledef(char *name, struct table_opts *opts)
4734 struct pfr_buffer ab;
4735 struct node_tinit *ti;
4737 bzero(&ab, sizeof(ab));
4738 ab.pfrb_type = PFRB_ADDRS;
4739 SIMPLEQ_FOREACH(ti, &opts->init_nodes, entries) {
4741 if (pfr_buf_load(&ab, ti->file, 0, append_addr)) {
4743 yyerror("cannot load \"%s\": %s",
4744 ti->file, strerror(errno));
4746 yyerror("file \"%s\" contains bad data",
4751 if (append_addr_host(&ab, ti->host, 0, 0)) {
4752 yyerror("cannot create address buffer: %s",
4757 if (pf->opts & PF_OPT_VERBOSE)
4758 print_tabledef(name, opts->flags, opts->init_addr,
4760 if (!(pf->opts & PF_OPT_NOACTION) &&
4761 pfctl_define_table(name, opts->flags, opts->init_addr,
4762 pf->anchor->name, &ab, pf->anchor->ruleset.tticket)) {
4763 yyerror("cannot define table %s: %s", name,
4764 pfr_strerror(errno));
4780 /* macro gore, but you should've seen the prior indentation nightmare... */
4782 #define FREE_LIST(T,r) \
4785 while (node != NULL) { \
4787 node = node->next; \
4792 #define LOOP_THROUGH(T,n,r,C) \
4796 r = calloc(1, sizeof(T)); \
4798 err(1, "LOOP: calloc"); \
4802 while (n != NULL) { \
4811 expand_label_str(char *label, size_t len, const char *srch, const char *repl)
4816 if ((tmp = calloc(1, len)) == NULL)
4817 err(1, "expand_label_str: calloc");
4819 while ((q = strstr(p, srch)) != NULL) {
4821 if ((strlcat(tmp, p, len) >= len) ||
4822 (strlcat(tmp, repl, len) >= len))
4823 errx(1, "expand_label: label too long");
4827 if (strlcat(tmp, p, len) >= len)
4828 errx(1, "expand_label: label too long");
4829 strlcpy(label, tmp, len); /* always fits */
4834 expand_label_if(const char *name, char *label, size_t len, const char *ifname)
4836 if (strstr(label, name) != NULL) {
4838 expand_label_str(label, len, name, "any");
4840 expand_label_str(label, len, name, ifname);
4845 expand_label_addr(const char *name, char *label, size_t len, sa_family_t af,
4846 struct node_host *h)
4848 char tmp[64], tmp_not[66];
4850 if (strstr(label, name) != NULL) {
4851 switch (h->addr.type) {
4852 case PF_ADDR_DYNIFTL:
4853 snprintf(tmp, sizeof(tmp), "(%s)", h->addr.v.ifname);
4856 snprintf(tmp, sizeof(tmp), "<%s>", h->addr.v.tblname);
4858 case PF_ADDR_NOROUTE:
4859 snprintf(tmp, sizeof(tmp), "no-route");
4861 case PF_ADDR_URPFFAILED:
4862 snprintf(tmp, sizeof(tmp), "urpf-failed");
4864 case PF_ADDR_ADDRMASK:
4865 if (!af || (PF_AZERO(&h->addr.v.a.addr, af) &&
4866 PF_AZERO(&h->addr.v.a.mask, af)))
4867 snprintf(tmp, sizeof(tmp), "any");
4872 if (inet_ntop(af, &h->addr.v.a.addr, a,
4874 snprintf(tmp, sizeof(tmp), "?");
4876 bits = unmask(&h->addr.v.a.mask, af);
4877 if ((af == AF_INET && bits < 32) ||
4878 (af == AF_INET6 && bits < 128))
4879 snprintf(tmp, sizeof(tmp),
4882 snprintf(tmp, sizeof(tmp),
4888 snprintf(tmp, sizeof(tmp), "?");
4893 snprintf(tmp_not, sizeof(tmp_not), "! %s", tmp);
4894 expand_label_str(label, len, name, tmp_not);
4896 expand_label_str(label, len, name, tmp);
4901 expand_label_port(const char *name, char *label, size_t len,
4902 struct node_port *port)
4904 char a1[6], a2[6], op[13] = "";
4906 if (strstr(label, name) != NULL) {
4907 snprintf(a1, sizeof(a1), "%u", ntohs(port->port[0]));
4908 snprintf(a2, sizeof(a2), "%u", ntohs(port->port[1]));
4911 else if (port->op == PF_OP_IRG)
4912 snprintf(op, sizeof(op), "%s><%s", a1, a2);
4913 else if (port->op == PF_OP_XRG)
4914 snprintf(op, sizeof(op), "%s<>%s", a1, a2);
4915 else if (port->op == PF_OP_EQ)
4916 snprintf(op, sizeof(op), "%s", a1);
4917 else if (port->op == PF_OP_NE)
4918 snprintf(op, sizeof(op), "!=%s", a1);
4919 else if (port->op == PF_OP_LT)
4920 snprintf(op, sizeof(op), "<%s", a1);
4921 else if (port->op == PF_OP_LE)
4922 snprintf(op, sizeof(op), "<=%s", a1);
4923 else if (port->op == PF_OP_GT)
4924 snprintf(op, sizeof(op), ">%s", a1);
4925 else if (port->op == PF_OP_GE)
4926 snprintf(op, sizeof(op), ">=%s", a1);
4927 expand_label_str(label, len, name, op);
4932 expand_label_proto(const char *name, char *label, size_t len, u_int8_t proto)
4934 struct protoent *pe;
4937 if (strstr(label, name) != NULL) {
4938 pe = getprotobynumber(proto);
4940 expand_label_str(label, len, name, pe->p_name);
4942 snprintf(n, sizeof(n), "%u", proto);
4943 expand_label_str(label, len, name, n);
4949 expand_label_nr(const char *name, char *label, size_t len)
4953 if (strstr(label, name) != NULL) {
4954 snprintf(n, sizeof(n), "%u", pf->anchor->match);
4955 expand_label_str(label, len, name, n);
4960 expand_label(char *label, size_t len, const char *ifname, sa_family_t af,
4961 struct node_host *src_host, struct node_port *src_port,
4962 struct node_host *dst_host, struct node_port *dst_port,
4965 expand_label_if("$if", label, len, ifname);
4966 expand_label_addr("$srcaddr", label, len, af, src_host);
4967 expand_label_addr("$dstaddr", label, len, af, dst_host);
4968 expand_label_port("$srcport", label, len, src_port);
4969 expand_label_port("$dstport", label, len, dst_port);
4970 expand_label_proto("$proto", label, len, proto);
4971 expand_label_nr("$nr", label, len);
4975 expand_altq(struct pf_altq *a, struct node_if *interfaces,
4976 struct node_queue *nqueues, struct node_queue_bw bwspec,
4977 struct node_queue_opt *opts)
4979 struct pf_altq pa, pb;
4980 char qname[PF_QNAME_SIZE];
4981 struct node_queue *n;
4982 struct node_queue_bw bw;
4985 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
4986 FREE_LIST(struct node_if, interfaces);
4988 FREE_LIST(struct node_queue, nqueues);
4992 LOOP_THROUGH(struct node_if, interface, interfaces,
4993 memcpy(&pa, a, sizeof(struct pf_altq));
4994 if (strlcpy(pa.ifname, interface->ifname,
4995 sizeof(pa.ifname)) >= sizeof(pa.ifname))
4996 errx(1, "expand_altq: strlcpy");
4998 if (interface->not) {
4999 yyerror("altq on ! <interface> is not supported");
5002 if (eval_pfaltq(pf, &pa, &bwspec, opts))
5005 if (pfctl_add_altq(pf, &pa))
5008 if (pf->opts & PF_OPT_VERBOSE) {
5009 print_altq(&pf->paltq->altq, 0,
5011 if (nqueues && nqueues->tail) {
5013 LOOP_THROUGH(struct node_queue, queue,
5023 if (pa.scheduler == ALTQT_CBQ ||
5024 pa.scheduler == ALTQT_HFSC) {
5025 /* now create a root queue */
5026 memset(&pb, 0, sizeof(struct pf_altq));
5027 if (strlcpy(qname, "root_", sizeof(qname)) >=
5029 errx(1, "expand_altq: strlcpy");
5030 if (strlcat(qname, interface->ifname,
5031 sizeof(qname)) >= sizeof(qname))
5032 errx(1, "expand_altq: strlcat");
5033 if (strlcpy(pb.qname, qname,
5034 sizeof(pb.qname)) >= sizeof(pb.qname))
5035 errx(1, "expand_altq: strlcpy");
5036 if (strlcpy(pb.ifname, interface->ifname,
5037 sizeof(pb.ifname)) >= sizeof(pb.ifname))
5038 errx(1, "expand_altq: strlcpy");
5039 pb.qlimit = pa.qlimit;
5040 pb.scheduler = pa.scheduler;
5041 bw.bw_absolute = pa.ifbandwidth;
5043 if (eval_pfqueue(pf, &pb, &bw, opts))
5046 if (pfctl_add_altq(pf, &pb))
5050 LOOP_THROUGH(struct node_queue, queue, nqueues,
5051 n = calloc(1, sizeof(struct node_queue));
5053 err(1, "expand_altq: calloc");
5054 if (pa.scheduler == ALTQT_CBQ ||
5055 pa.scheduler == ALTQT_HFSC)
5056 if (strlcpy(n->parent, qname,
5057 sizeof(n->parent)) >=
5059 errx(1, "expand_altq: strlcpy");
5060 if (strlcpy(n->queue, queue->queue,
5061 sizeof(n->queue)) >= sizeof(n->queue))
5062 errx(1, "expand_altq: strlcpy");
5063 if (strlcpy(n->ifname, interface->ifname,
5064 sizeof(n->ifname)) >= sizeof(n->ifname))
5065 errx(1, "expand_altq: strlcpy");
5066 n->scheduler = pa.scheduler;
5072 queues->tail->next = n;
5078 FREE_LIST(struct node_if, interfaces);
5080 FREE_LIST(struct node_queue, nqueues);
5086 expand_queue(struct pf_altq *a, struct node_if *interfaces,
5087 struct node_queue *nqueues, struct node_queue_bw bwspec,
5088 struct node_queue_opt *opts)
5090 struct node_queue *n, *nq;
5095 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
5096 FREE_LIST(struct node_queue, nqueues);
5100 if (queues == NULL) {
5101 yyerror("queue %s has no parent", a->qname);
5102 FREE_LIST(struct node_queue, nqueues);
5106 LOOP_THROUGH(struct node_if, interface, interfaces,
5107 LOOP_THROUGH(struct node_queue, tqueue, queues,
5108 if (!strncmp(a->qname, tqueue->queue, PF_QNAME_SIZE) &&
5109 (interface->ifname[0] == 0 ||
5110 (!interface->not && !strncmp(interface->ifname,
5111 tqueue->ifname, IFNAMSIZ)) ||
5112 (interface->not && strncmp(interface->ifname,
5113 tqueue->ifname, IFNAMSIZ)))) {
5114 /* found ourself in queues */
5117 memcpy(&pa, a, sizeof(struct pf_altq));
5119 if (pa.scheduler != ALTQT_NONE &&
5120 pa.scheduler != tqueue->scheduler) {
5121 yyerror("exactly one scheduler type "
5122 "per interface allowed");
5125 pa.scheduler = tqueue->scheduler;
5127 /* scheduler dependent error checking */
5128 switch (pa.scheduler) {
5130 if (nqueues != NULL) {
5131 yyerror("priq queues cannot "
5132 "have child queues");
5135 if (bwspec.bw_absolute > 0 ||
5136 bwspec.bw_percent < 100) {
5137 yyerror("priq doesn't take "
5146 if (strlcpy(pa.ifname, tqueue->ifname,
5147 sizeof(pa.ifname)) >= sizeof(pa.ifname))
5148 errx(1, "expand_queue: strlcpy");
5149 if (strlcpy(pa.parent, tqueue->parent,
5150 sizeof(pa.parent)) >= sizeof(pa.parent))
5151 errx(1, "expand_queue: strlcpy");
5153 if (eval_pfqueue(pf, &pa, &bwspec, opts))
5156 if (pfctl_add_altq(pf, &pa))
5159 for (nq = nqueues; nq != NULL; nq = nq->next) {
5160 if (!strcmp(a->qname, nq->queue)) {
5161 yyerror("queue cannot have "
5167 sizeof(struct node_queue));
5169 err(1, "expand_queue: calloc");
5170 if (strlcpy(n->parent, a->qname,
5171 sizeof(n->parent)) >=
5173 errx(1, "expand_queue strlcpy");
5174 if (strlcpy(n->queue, nq->queue,
5175 sizeof(n->queue)) >=
5177 errx(1, "expand_queue strlcpy");
5178 if (strlcpy(n->ifname, tqueue->ifname,
5179 sizeof(n->ifname)) >=
5181 errx(1, "expand_queue strlcpy");
5182 n->scheduler = tqueue->scheduler;
5188 queues->tail->next = n;
5192 if ((pf->opts & PF_OPT_VERBOSE) && (
5193 (found == 1 && interface->ifname[0] == 0) ||
5194 (found > 0 && interface->ifname[0] != 0))) {
5195 print_queue(&pf->paltq->altq, 0,
5196 &bwspec, interface->ifname[0] != 0,
5198 if (nqueues && nqueues->tail) {
5200 LOOP_THROUGH(struct node_queue,
5213 FREE_LIST(struct node_queue, nqueues);
5214 FREE_LIST(struct node_if, interfaces);
5217 yyerror("queue %s has no parent", a->qname);
5228 expand_rule(struct pf_rule *r,
5229 struct node_if *interfaces, struct node_host *rpool_hosts,
5230 struct node_proto *protos, struct node_os *src_oses,
5231 struct node_host *src_hosts, struct node_port *src_ports,
5232 struct node_host *dst_hosts, struct node_port *dst_ports,
5233 struct node_uid *uids, struct node_gid *gids, struct node_icmp *icmp_types,
5234 const char *anchor_call)
5236 sa_family_t af = r->af;
5237 int added = 0, error = 0;
5238 char ifname[IF_NAMESIZE];
5239 char label[PF_RULE_LABEL_SIZE];
5240 char tagname[PF_TAG_NAME_SIZE];
5241 char match_tagname[PF_TAG_NAME_SIZE];
5242 struct pf_pooladdr *pa;
5243 struct node_host *h;
5244 u_int8_t flags, flagset, keep_state;
5246 if (strlcpy(label, r->label, sizeof(label)) >= sizeof(label))
5247 errx(1, "expand_rule: strlcpy");
5248 if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname))
5249 errx(1, "expand_rule: strlcpy");
5250 if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >=
5251 sizeof(match_tagname))
5252 errx(1, "expand_rule: strlcpy");
5254 flagset = r->flagset;
5255 keep_state = r->keep_state;
5257 LOOP_THROUGH(struct node_if, interface, interfaces,
5258 LOOP_THROUGH(struct node_proto, proto, protos,
5259 LOOP_THROUGH(struct node_icmp, icmp_type, icmp_types,
5260 LOOP_THROUGH(struct node_host, src_host, src_hosts,
5261 LOOP_THROUGH(struct node_port, src_port, src_ports,
5262 LOOP_THROUGH(struct node_os, src_os, src_oses,
5263 LOOP_THROUGH(struct node_host, dst_host, dst_hosts,
5264 LOOP_THROUGH(struct node_port, dst_port, dst_ports,
5265 LOOP_THROUGH(struct node_uid, uid, uids,
5266 LOOP_THROUGH(struct node_gid, gid, gids,
5269 /* for link-local IPv6 address, interface must match up */
5270 if ((r->af && src_host->af && r->af != src_host->af) ||
5271 (r->af && dst_host->af && r->af != dst_host->af) ||
5272 (src_host->af && dst_host->af &&
5273 src_host->af != dst_host->af) ||
5274 (src_host->ifindex && dst_host->ifindex &&
5275 src_host->ifindex != dst_host->ifindex) ||
5276 (src_host->ifindex && *interface->ifname &&
5277 src_host->ifindex != if_nametoindex(interface->ifname)) ||
5278 (dst_host->ifindex && *interface->ifname &&
5279 dst_host->ifindex != if_nametoindex(interface->ifname)))
5281 if (!r->af && src_host->af)
5282 r->af = src_host->af;
5283 else if (!r->af && dst_host->af)
5284 r->af = dst_host->af;
5286 if (*interface->ifname)
5287 strlcpy(r->ifname, interface->ifname,
5289 else if (if_indextoname(src_host->ifindex, ifname))
5290 strlcpy(r->ifname, ifname, sizeof(r->ifname));
5291 else if (if_indextoname(dst_host->ifindex, ifname))
5292 strlcpy(r->ifname, ifname, sizeof(r->ifname));
5294 memset(r->ifname, '\0', sizeof(r->ifname));
5296 if (strlcpy(r->label, label, sizeof(r->label)) >=
5298 errx(1, "expand_rule: strlcpy");
5299 if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >=
5301 errx(1, "expand_rule: strlcpy");
5302 if (strlcpy(r->match_tagname, match_tagname,
5303 sizeof(r->match_tagname)) >= sizeof(r->match_tagname))
5304 errx(1, "expand_rule: strlcpy");
5305 expand_label(r->label, PF_RULE_LABEL_SIZE, r->ifname, r->af,
5306 src_host, src_port, dst_host, dst_port, proto->proto);
5307 expand_label(r->tagname, PF_TAG_NAME_SIZE, r->ifname, r->af,
5308 src_host, src_port, dst_host, dst_port, proto->proto);
5309 expand_label(r->match_tagname, PF_TAG_NAME_SIZE, r->ifname,
5310 r->af, src_host, src_port, dst_host, dst_port,
5313 error += check_netmask(src_host, r->af);
5314 error += check_netmask(dst_host, r->af);
5316 r->ifnot = interface->not;
5317 r->proto = proto->proto;
5318 r->src.addr = src_host->addr;
5319 r->src.neg = src_host->not;
5320 r->src.port[0] = src_port->port[0];
5321 r->src.port[1] = src_port->port[1];
5322 r->src.port_op = src_port->op;
5323 r->dst.addr = dst_host->addr;
5324 r->dst.neg = dst_host->not;
5325 r->dst.port[0] = dst_port->port[0];
5326 r->dst.port[1] = dst_port->port[1];
5327 r->dst.port_op = dst_port->op;
5328 r->uid.op = uid->op;
5329 r->uid.uid[0] = uid->uid[0];
5330 r->uid.uid[1] = uid->uid[1];
5331 r->gid.op = gid->op;
5332 r->gid.gid[0] = gid->gid[0];
5333 r->gid.gid[1] = gid->gid[1];
5334 r->type = icmp_type->type;
5335 r->code = icmp_type->code;
5337 if ((keep_state == PF_STATE_MODULATE ||
5338 keep_state == PF_STATE_SYNPROXY) &&
5339 r->proto && r->proto != IPPROTO_TCP)
5340 r->keep_state = PF_STATE_NORMAL;
5342 r->keep_state = keep_state;
5344 if (r->proto && r->proto != IPPROTO_TCP) {
5349 r->flagset = flagset;
5351 if (icmp_type->proto && r->proto != icmp_type->proto) {
5352 yyerror("icmp-type mismatch");
5356 if (src_os && src_os->os) {
5357 r->os_fingerprint = pfctl_get_fingerprint(src_os->os);
5358 if ((pf->opts & PF_OPT_VERBOSE2) &&
5359 r->os_fingerprint == PF_OSFP_NOMATCH)
5361 "warning: unknown '%s' OS fingerprint\n",
5364 r->os_fingerprint = PF_OSFP_ANY;
5367 TAILQ_INIT(&r->rpool.list);
5368 for (h = rpool_hosts; h != NULL; h = h->next) {
5369 pa = calloc(1, sizeof(struct pf_pooladdr));
5371 err(1, "expand_rule: calloc");
5373 if (h->ifname != NULL) {
5374 if (strlcpy(pa->ifname, h->ifname,
5375 sizeof(pa->ifname)) >=
5377 errx(1, "expand_rule: strlcpy");
5380 TAILQ_INSERT_TAIL(&r->rpool.list, pa, entries);
5383 if (rule_consistent(r, anchor_call[0]) < 0 || error)
5384 yyerror("skipping rule due to errors");
5386 r->nr = pf->astack[pf->asd]->match++;
5387 pfctl_add_rule(pf, r, anchor_call);
5393 FREE_LIST(struct node_if, interfaces);
5394 FREE_LIST(struct node_proto, protos);
5395 FREE_LIST(struct node_host, src_hosts);
5396 FREE_LIST(struct node_port, src_ports);
5397 FREE_LIST(struct node_os, src_oses);
5398 FREE_LIST(struct node_host, dst_hosts);
5399 FREE_LIST(struct node_port, dst_ports);
5400 FREE_LIST(struct node_uid, uids);
5401 FREE_LIST(struct node_gid, gids);
5402 FREE_LIST(struct node_icmp, icmp_types);
5403 FREE_LIST(struct node_host, rpool_hosts);
5406 yyerror("rule expands to no valid combination");
5410 expand_skip_interface(struct node_if *interfaces)
5414 if (!interfaces || (!interfaces->next && !interfaces->not &&
5415 !strcmp(interfaces->ifname, "none"))) {
5416 if (pf->opts & PF_OPT_VERBOSE)
5417 printf("set skip on none\n");
5418 errs = pfctl_set_interface_flags(pf, "", PFI_IFLAG_SKIP, 0);
5422 if (pf->opts & PF_OPT_VERBOSE)
5423 printf("set skip on {");
5424 LOOP_THROUGH(struct node_if, interface, interfaces,
5425 if (pf->opts & PF_OPT_VERBOSE)
5426 printf(" %s", interface->ifname);
5427 if (interface->not) {
5428 yyerror("skip on ! <interface> is not supported");
5431 errs += pfctl_set_interface_flags(pf,
5432 interface->ifname, PFI_IFLAG_SKIP, 1);
5434 if (pf->opts & PF_OPT_VERBOSE)
5437 FREE_LIST(struct node_if, interfaces);
5449 check_rulestate(int desired_state)
5451 if (require_order && (rulestate > desired_state)) {
5452 yyerror("Rules must be in order: options, normalization, "
5453 "queueing, translation, filtering");
5456 rulestate = desired_state;
5461 kw_cmp(const void *k, const void *e)
5463 return (strcmp(k, ((const struct keywords *)e)->k_name));
5469 /* this has to be sorted always */
5470 static const struct keywords keywords[] = {
5472 { "allow-opts", ALLOWOPTS},
5474 { "anchor", ANCHOR},
5475 { "antispoof", ANTISPOOF},
5477 { "bandwidth", BANDWIDTH},
5479 { "binat-anchor", BINATANCHOR},
5480 { "bitmask", BITMASK},
5482 { "block-policy", BLOCKPOLICY},
5483 { "buckets", BUCKETS},
5487 { "crop", FRAGCROP},
5489 { "divert-reply", DIVERTREPLY},
5490 { "divert-to", DIVERTTO},
5492 { "drop-ovl", FRAGDROP},
5494 { "fail-policy", FAILPOLICY},
5496 { "fastroute", FASTROUTE},
5497 { "file", FILENAME},
5498 { "fingerprints", FINGERPRINTS},
5500 { "floating", FLOATING},
5503 { "fragment", FRAGMENT},
5505 { "global", GLOBAL},
5509 { "hostid", HOSTID},
5510 { "icmp-type", ICMPTYPE},
5511 { "icmp6-type", ICMP6TYPE},
5512 { "if-bound", IFBOUND},
5514 { "include", INCLUDE},
5517 { "interval", INTERVAL},
5521 { "linkshare", LINKSHARE},
5524 { "loginterface", LOGINTERFACE},
5526 { "max-mss", MAXMSS},
5527 { "max-src-conn", MAXSRCCONN},
5528 { "max-src-conn-rate", MAXSRCCONNRATE},
5529 { "max-src-nodes", MAXSRCNODES},
5530 { "max-src-states", MAXSRCSTATES},
5531 { "min-ttl", MINTTL},
5532 { "modulate", MODULATE},
5534 { "nat-anchor", NATANCHOR},
5537 { "no-route", NOROUTE},
5538 { "no-sync", NOSYNC},
5540 { "optimization", OPTIMIZATION},
5543 { "overload", OVERLOAD},
5547 { "priority", PRIORITY},
5549 { "probability", PROBABILITY},
5551 { "qlimit", QLIMIT},
5554 { "random", RANDOM},
5555 { "random-id", RANDOMID},
5557 { "rdr-anchor", RDRANCHOR},
5558 { "realtime", REALTIME},
5559 { "reassemble", REASSEMBLE},
5560 { "reply-to", REPLYTO},
5561 { "require-order", REQUIREORDER},
5562 { "return", RETURN},
5563 { "return-icmp", RETURNICMP},
5564 { "return-icmp6", RETURNICMP6},
5565 { "return-rst", RETURNRST},
5566 { "round-robin", ROUNDROBIN},
5568 { "route-to", ROUTETO},
5569 { "rtable", RTABLE},
5571 { "ruleset-optimization", RULESET_OPTIMIZATION},
5574 { "set-tos", SETTOS},
5576 { "sloppy", SLOPPY},
5577 { "source-hash", SOURCEHASH},
5578 { "source-track", SOURCETRACK},
5580 { "state-defaults", STATEDEFAULTS},
5581 { "state-policy", STATEPOLICY},
5582 { "static-port", STATICPORT},
5583 { "sticky-address", STICKYADDRESS},
5584 { "synproxy", SYNPROXY},
5587 { "tagged", TAGGED},
5588 { "target", TARGET},
5589 { "tbrsize", TBRSIZE},
5590 { "timeout", TIMEOUT},
5594 { "upperlimit", UPPERLIMIT},
5595 { "urpf-failed", URPFFAILED},
5598 const struct keywords *p;
5600 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
5601 sizeof(keywords[0]), kw_cmp);
5605 fprintf(stderr, "%s: %d\n", s, p->k_val);
5609 fprintf(stderr, "string: %s\n", s);
5614 #define MAXPUSHBACK 128
5616 static char *parsebuf;
5617 static int parseindex;
5618 static char pushback_buffer[MAXPUSHBACK];
5619 static int pushback_index = 0;
5627 /* Read character from the parsebuffer instead of input. */
5628 if (parseindex >= 0) {
5629 c = parsebuf[parseindex++];
5638 return (pushback_buffer[--pushback_index]);
5641 if ((c = getc(file->stream)) == EOF) {
5642 yyerror("reached end of file while parsing quoted string");
5643 if (popfile() == EOF)
5650 while ((c = getc(file->stream)) == '\\') {
5651 next = getc(file->stream);
5656 yylval.lineno = file->lineno;
5661 if (popfile() == EOF)
5663 c = getc(file->stream);
5675 if (parseindex >= 0)
5678 if (pushback_index < MAXPUSHBACK-1)
5679 return (pushback_buffer[pushback_index++] = c);
5691 /* skip to either EOF or the first real EOL */
5694 c = pushback_buffer[--pushback_index];
5712 int quotec, next, c;
5717 while ((c = lgetc(0)) == ' ' || c == '\t')
5720 yylval.lineno = file->lineno;
5722 while ((c = lgetc(0)) != '\n' && c != EOF)
5724 if (c == '$' && parsebuf == NULL) {
5726 if ((c = lgetc(0)) == EOF)
5729 if (p + 1 >= buf + sizeof(buf) - 1) {
5730 yyerror("string too long");
5733 if (isalnum(c) || c == '_') {
5743 yyerror("macro '%s' not defined", buf);
5756 if ((c = lgetc(quotec)) == EOF)
5761 } else if (c == '\\') {
5762 if ((next = lgetc(quotec)) == EOF)
5764 if (next == quotec || c == ' ' || c == '\t')
5766 else if (next == '\n')
5770 } else if (c == quotec) {
5774 if (p + 1 >= buf + sizeof(buf) - 1) {
5775 yyerror("string too long");
5780 yylval.v.string = strdup(buf);
5781 if (yylval.v.string == NULL)
5782 err(1, "yylex: strdup");
5787 yylval.v.i = PF_OP_XRG;
5788 return (PORTBINARY);
5795 yylval.v.i = PF_OP_IRG;
5796 return (PORTBINARY);
5808 #define allowed_to_end_number(x) \
5809 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
5811 if (c == '-' || isdigit(c)) {
5814 if ((unsigned)(p-buf) >= sizeof(buf)) {
5815 yyerror("string too long");
5818 } while ((c = lgetc(0)) != EOF && isdigit(c));
5820 if (p == buf + 1 && buf[0] == '-')
5822 if (c == EOF || allowed_to_end_number(c)) {
5823 const char *errstr = NULL;
5826 yylval.v.number = strtonum(buf, LLONG_MIN,
5827 LLONG_MAX, &errstr);
5829 yyerror("\"%s\" invalid number: %s",
5844 #define allowed_in_string(x) \
5845 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
5846 x != '{' && x != '}' && x != '<' && x != '>' && \
5847 x != '!' && x != '=' && x != '/' && x != '#' && \
5850 if (isalnum(c) || c == ':' || c == '_') {
5853 if ((unsigned)(p-buf) >= sizeof(buf)) {
5854 yyerror("string too long");
5857 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
5860 if ((token = lookup(buf)) == STRING)
5861 if ((yylval.v.string = strdup(buf)) == NULL)
5862 err(1, "yylex: strdup");
5866 yylval.lineno = file->lineno;
5875 check_file_secrecy(int fd, const char *fname)
5879 if (fstat(fd, &st)) {
5880 warn("cannot stat %s", fname);
5883 if (st.st_uid != 0 && st.st_uid != getuid()) {
5884 warnx("%s: owner not root or current user", fname);
5887 if (st.st_mode & (S_IRWXG | S_IRWXO)) {
5888 warnx("%s: group/world readable/writeable", fname);
5895 pushfile(const char *name, int secret)
5899 if ((nfile = calloc(1, sizeof(struct file))) == NULL ||
5900 (nfile->name = strdup(name)) == NULL) {
5904 if (TAILQ_FIRST(&files) == NULL && strcmp(nfile->name, "-") == 0) {
5905 nfile->stream = stdin;
5907 if ((nfile->name = strdup("stdin")) == NULL) {
5912 } else if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
5913 warn("%s", nfile->name);
5917 } else if (secret &&
5918 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
5919 fclose(nfile->stream);
5925 TAILQ_INSERT_TAIL(&files, nfile, entry);
5934 if ((prev = TAILQ_PREV(file, files, entry)) != NULL) {
5935 prev->errors += file->errors;
5936 TAILQ_REMOVE(&files, file, entry);
5937 fclose(file->stream);
5947 parse_config(char *filename, struct pfctl *xpf)
5954 rulestate = PFCTL_STATE_NONE;
5955 returnicmpdefault = (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
5956 returnicmp6default =
5957 (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
5958 blockpolicy = PFRULE_DROP;
5959 failpolicy = PFRULE_DROP;
5962 if ((file = pushfile(filename, 0)) == NULL) {
5963 warn("cannot open the main config file!");
5968 errors = file->errors;
5971 /* Free macros and check which have not been used. */
5972 while ((sym = TAILQ_FIRST(&symhead))) {
5973 if ((pf->opts & PF_OPT_VERBOSE2) && !sym->used)
5974 fprintf(stderr, "warning: macro '%s' not "
5975 "used\n", sym->nam);
5978 TAILQ_REMOVE(&symhead, sym, entry);
5982 return (errors ? -1 : 0);
5986 symset(const char *nam, const char *val, int persist)
5990 for (sym = TAILQ_FIRST(&symhead); sym && strcmp(nam, sym->nam);
5991 sym = TAILQ_NEXT(sym, entry))
5995 if (sym->persist == 1)
6000 TAILQ_REMOVE(&symhead, sym, entry);
6004 if ((sym = calloc(1, sizeof(*sym))) == NULL)
6007 sym->nam = strdup(nam);
6008 if (sym->nam == NULL) {
6012 sym->val = strdup(val);
6013 if (sym->val == NULL) {
6019 sym->persist = persist;
6020 TAILQ_INSERT_TAIL(&symhead, sym, entry);
6025 pfctl_cmdline_symset(char *s)
6030 if ((val = strrchr(s, '=')) == NULL)
6033 if ((sym = malloc(strlen(s) - strlen(val) + 1)) == NULL)
6034 err(1, "pfctl_cmdline_symset: malloc");
6036 strlcpy(sym, s, strlen(s) - strlen(val) + 1);
6038 ret = symset(sym, val + 1, 1);
6045 symget(const char *nam)
6049 TAILQ_FOREACH(sym, &symhead, entry)
6050 if (strcmp(nam, sym->nam) == 0) {
6058 mv_rules(struct pf_ruleset *src, struct pf_ruleset *dst)
6063 for (i = 0; i < PF_RULESET_MAX; ++i) {
6064 while ((r = TAILQ_FIRST(src->rules[i].active.ptr))
6066 TAILQ_REMOVE(src->rules[i].active.ptr, r, entries);
6067 TAILQ_INSERT_TAIL(dst->rules[i].active.ptr, r, entries);
6068 dst->anchor->match++;
6070 src->anchor->match = 0;
6071 while ((r = TAILQ_FIRST(src->rules[i].inactive.ptr))
6073 TAILQ_REMOVE(src->rules[i].inactive.ptr, r, entries);
6074 TAILQ_INSERT_TAIL(dst->rules[i].inactive.ptr,
6081 decide_address_family(struct node_host *n, sa_family_t *af)
6083 if (*af != 0 || n == NULL)
6086 while ((n = n->next) != NULL) {
6095 remove_invalid_hosts(struct node_host **nh, sa_family_t *af)
6097 struct node_host *n = *nh, *prev = NULL;
6100 if (*af && n->af && n->af != *af) {
6101 /* unlink and free n */
6102 struct node_host *next = n->next;
6104 /* adjust tail pointer */
6105 if (n == (*nh)->tail)
6107 /* adjust previous node's next pointer */
6113 if (n->ifname != NULL)
6127 invalid_redirect(struct node_host *nh, sa_family_t af)
6130 struct node_host *n;
6132 /* tables and dyniftl are ok without an address family */
6133 for (n = nh; n != NULL; n = n->next) {
6134 if (n->addr.type != PF_ADDR_TABLE &&
6135 n->addr.type != PF_ADDR_DYNIFTL) {
6136 yyerror("address family not given and "
6137 "translation address expands to multiple "
6138 "address families");
6144 yyerror("no translation address with matching address family "
6152 atoul(char *s, u_long *ulvalp)
6158 ulval = strtoul(s, &ep, 0);
6159 if (s[0] == '\0' || *ep != '\0')
6161 if (errno == ERANGE && ulval == ULONG_MAX)
6173 if (atoul(n, &ulval) == 0) {
6174 if (ulval > 65535) {
6175 yyerror("illegal port value %lu", ulval);
6178 return (htons(ulval));
6180 s = getservbyname(n, "tcp");
6182 s = getservbyname(n, "udp");
6184 yyerror("unknown port %s", n);
6192 rule_label(struct pf_rule *r, char *s)
6195 if (strlcpy(r->label, s, sizeof(r->label)) >=
6197 yyerror("rule label too long (max %d chars)",
6198 sizeof(r->label)-1);
6206 parseicmpspec(char *w, sa_family_t af)
6208 const struct icmpcodeent *p;
6213 icmptype = returnicmpdefault >> 8;
6215 icmptype = returnicmp6default >> 8;
6217 if (atoul(w, &ulval) == -1) {
6218 if ((p = geticmpcodebyname(icmptype, w, af)) == NULL) {
6219 yyerror("unknown icmp code %s", w);
6225 yyerror("invalid icmp code %lu", ulval);
6228 return (icmptype << 8 | ulval);
6232 parseport(char *port, struct range *r, int extensions)
6234 char *p = strchr(port, ':');
6237 if ((r->a = getservice(port)) == -1)
6243 if ((extensions & PPORT_STAR) && !strcmp(p+1, "*")) {
6245 if ((r->a = getservice(port)) == -1)
6251 if ((extensions & PPORT_RANGE)) {
6253 if ((r->a = getservice(port)) == -1 ||
6254 (r->b = getservice(p)) == -1)
6267 pfctl_load_anchors(int dev, struct pfctl *pf, struct pfr_buffer *trans)
6269 struct loadanchors *la;
6271 TAILQ_FOREACH(la, &loadanchorshead, entries) {
6272 if (pf->opts & PF_OPT_VERBOSE)
6273 fprintf(stderr, "\nLoading anchor %s from %s\n",
6274 la->anchorname, la->filename);
6275 if (pfctl_rules(dev, la->filename, pf->opts, pf->optimize,
6276 la->anchorname, trans) == -1)
6284 kw_casecmp(const void *k, const void *e)
6286 return (strcasecmp(k, ((const struct keywords *)e)->k_name));
6290 map_tos(char *s, int *val)
6292 /* DiffServ Codepoints and other TOS mappings */
6293 const struct keywords toswords[] = {
6294 { "af11", IPTOS_DSCP_AF11 },
6295 { "af12", IPTOS_DSCP_AF12 },
6296 { "af13", IPTOS_DSCP_AF13 },
6297 { "af21", IPTOS_DSCP_AF21 },
6298 { "af22", IPTOS_DSCP_AF22 },
6299 { "af23", IPTOS_DSCP_AF23 },
6300 { "af31", IPTOS_DSCP_AF31 },
6301 { "af32", IPTOS_DSCP_AF32 },
6302 { "af33", IPTOS_DSCP_AF33 },
6303 { "af41", IPTOS_DSCP_AF41 },
6304 { "af42", IPTOS_DSCP_AF42 },
6305 { "af43", IPTOS_DSCP_AF43 },
6306 { "critical", IPTOS_PREC_CRITIC_ECP },
6307 { "cs0", IPTOS_DSCP_CS0 },
6308 { "cs1", IPTOS_DSCP_CS1 },
6309 { "cs2", IPTOS_DSCP_CS2 },
6310 { "cs3", IPTOS_DSCP_CS3 },
6311 { "cs4", IPTOS_DSCP_CS4 },
6312 { "cs5", IPTOS_DSCP_CS5 },
6313 { "cs6", IPTOS_DSCP_CS6 },
6314 { "cs7", IPTOS_DSCP_CS7 },
6315 { "ef", IPTOS_DSCP_EF },
6316 { "inetcontrol", IPTOS_PREC_INTERNETCONTROL },
6317 { "lowdelay", IPTOS_LOWDELAY },
6318 { "netcontrol", IPTOS_PREC_NETCONTROL },
6319 { "reliability", IPTOS_RELIABILITY },
6320 { "throughput", IPTOS_THROUGHPUT }
6322 const struct keywords *p;
6324 p = bsearch(s, toswords, sizeof(toswords)/sizeof(toswords[0]),
6325 sizeof(toswords[0]), kw_casecmp);
6335 rt_tableid_max(void)
6339 size_t l = sizeof(fibs);
6341 if (sysctlbyname("net.fibs", &fibs, &l, NULL, 0) == -1)
6342 fibs = 16; /* XXX RT_MAXFIBS, at least limit it some. */
6344 * As the OpenBSD code only compares > and not >= we need to adjust
6345 * here given we only accept values of 0..n and want to avoid #ifdefs
6350 return (RT_TABLEID_MAX);