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 require_order = 1;
83 static int default_statelock;
85 static TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
87 TAILQ_ENTRY(file) entry;
93 struct file *pushfile(const char *, int);
95 int check_file_secrecy(int, const char *);
98 int yyerror(const char *, ...);
99 int kw_cmp(const void *, const void *);
105 static TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
107 TAILQ_ENTRY(sym) entry;
113 int symset(const char *, const char *, int);
114 char *symget(const char *);
116 int atoul(char *, u_long *);
129 struct node_proto *next;
130 struct node_proto *tail;
136 struct node_port *next;
137 struct node_port *tail;
143 struct node_uid *next;
144 struct node_uid *tail;
150 struct node_gid *next;
151 struct node_gid *tail;
158 struct node_icmp *next;
159 struct node_icmp *tail;
162 enum { PF_STATE_OPT_MAX, PF_STATE_OPT_NOSYNC, PF_STATE_OPT_SRCTRACK,
163 PF_STATE_OPT_MAX_SRC_STATES, PF_STATE_OPT_MAX_SRC_CONN,
164 PF_STATE_OPT_MAX_SRC_CONN_RATE, PF_STATE_OPT_MAX_SRC_NODES,
165 PF_STATE_OPT_OVERLOAD, PF_STATE_OPT_STATELOCK,
166 PF_STATE_OPT_TIMEOUT, PF_STATE_OPT_SLOPPY, };
168 enum { PF_SRCTRACK_NONE, PF_SRCTRACK, PF_SRCTRACK_GLOBAL, PF_SRCTRACK_RULE };
170 struct node_state_opt {
173 u_int32_t max_states;
174 u_int32_t max_src_states;
175 u_int32_t max_src_conn;
182 char tblname[PF_TABLE_NAME_SIZE];
184 u_int32_t max_src_nodes;
192 struct node_state_opt *next;
193 struct node_state_opt *tail;
197 struct node_host *host;
198 struct node_port *port;
201 static struct node_queue {
202 char queue[PF_QNAME_SIZE];
203 char parent[PF_QNAME_SIZE];
204 char ifname[IFNAMSIZ];
206 struct node_queue *next;
207 struct node_queue *tail;
210 struct node_qassign {
215 static struct filter_opts {
217 #define FOM_FLAGS 0x01
218 #define FOM_ICMP 0x02
220 #define FOM_KEEP 0x08
221 #define FOM_SRCTRACK 0x10
222 #define FOM_SETPRIO 0x0400
223 #define FOM_PRIO 0x2000
224 struct node_uid *uid;
225 struct node_gid *gid;
232 struct node_icmp *icmpspec;
237 struct node_state_opt *options;
242 struct node_qassign queues;
245 u_int8_t match_tag_not;
248 u_int8_t set_prio[2];
250 struct node_host *addr;
255 static struct antispoof_opts {
260 static struct scrub_opts {
262 #define SOM_MINTTL 0x01
263 #define SOM_MAXMSS 0x02
264 #define SOM_FRAGCACHE 0x04
265 #define SOM_SETTOS 0x08
274 u_int8_t match_tag_not;
278 static struct queue_opts {
280 #define QOM_BWSPEC 0x01
281 #define QOM_SCHEDULER 0x02
282 #define QOM_PRIORITY 0x04
283 #define QOM_TBRSIZE 0x08
284 #define QOM_QLIMIT 0x10
285 struct node_queue_bw queue_bwspec;
286 struct node_queue_opt scheduler;
292 static struct table_opts {
295 struct node_tinithead init_nodes;
298 static struct pool_opts {
300 #define POM_TYPE 0x01
301 #define POM_STICKYADDRESS 0x02
305 struct pf_poolhashkey *key;
309 static struct codel_opts codel_opts;
310 static struct node_hfsc_opts hfsc_opts;
311 static struct node_fairq_opts fairq_opts;
312 static struct node_state_opt *keep_state_defaults = NULL;
314 int disallow_table(struct node_host *, const char *);
315 int disallow_urpf_failed(struct node_host *, const char *);
316 int disallow_alias(struct node_host *, const char *);
317 int rule_consistent(struct pf_rule *, int);
318 int filter_consistent(struct pf_rule *, int);
319 int nat_consistent(struct pf_rule *);
320 int rdr_consistent(struct pf_rule *);
321 int process_tabledef(char *, struct table_opts *);
322 void expand_label_str(char *, size_t, const char *, const char *);
323 void expand_label_if(const char *, char *, size_t, const char *);
324 void expand_label_addr(const char *, char *, size_t, u_int8_t,
326 void expand_label_port(const char *, char *, size_t,
328 void expand_label_proto(const char *, char *, size_t, u_int8_t);
329 void expand_label_nr(const char *, char *, size_t);
330 void expand_label(char *, size_t, const char *, u_int8_t,
331 struct node_host *, struct node_port *, struct node_host *,
332 struct node_port *, u_int8_t);
333 void expand_rule(struct pf_rule *, struct node_if *,
334 struct node_host *, struct node_proto *, struct node_os *,
335 struct node_host *, struct node_port *, struct node_host *,
336 struct node_port *, struct node_uid *, struct node_gid *,
337 struct node_icmp *, const char *);
338 int expand_altq(struct pf_altq *, struct node_if *,
339 struct node_queue *, struct node_queue_bw bwspec,
340 struct node_queue_opt *);
341 int expand_queue(struct pf_altq *, struct node_if *,
342 struct node_queue *, struct node_queue_bw,
343 struct node_queue_opt *);
344 int expand_skip_interface(struct node_if *);
346 int check_rulestate(int);
347 int getservice(char *);
348 int rule_label(struct pf_rule *, char *);
349 int rt_tableid_max(void);
351 void mv_rules(struct pf_ruleset *, struct pf_ruleset *);
352 void decide_address_family(struct node_host *, sa_family_t *);
353 void remove_invalid_hosts(struct node_host **, sa_family_t *);
354 int invalid_redirect(struct node_host *, sa_family_t);
355 u_int16_t parseicmpspec(char *, sa_family_t);
356 int kw_casecmp(const void *, const void *);
357 int map_tos(char *string, int *);
359 static TAILQ_HEAD(loadanchorshead, loadanchors)
360 loadanchorshead = TAILQ_HEAD_INITIALIZER(loadanchorshead);
363 TAILQ_ENTRY(loadanchors) entries;
386 struct node_if *interface;
387 struct node_proto *proto;
388 struct node_icmp *icmp;
389 struct node_host *host;
391 struct node_port *port;
392 struct node_uid *uid;
393 struct node_gid *gid;
394 struct node_state_opt *state_opt;
397 struct peer src, dst;
398 struct node_os *src_os;
401 struct node_host *host;
405 struct pf_poolhashkey *key;
408 struct node_host *host;
413 struct node_state_opt *options;
424 struct pf_poolhashkey *hashkey;
425 struct node_queue *queue;
426 struct node_queue_opt queue_options;
427 struct node_queue_bw queue_bwspec;
428 struct node_qassign qassign;
429 struct filter_opts filter_opts;
430 struct antispoof_opts antispoof_opts;
431 struct queue_opts queue_opts;
432 struct scrub_opts scrub_opts;
433 struct table_opts table_opts;
434 struct pool_opts pool_opts;
435 struct node_hfsc_opts hfsc_opts;
436 struct node_fairq_opts fairq_opts;
437 struct codel_opts codel_opts;
442 #define PPORT_RANGE 1
444 int parseport(char *, struct range *r, int);
446 #define DYNIF_MULTIADDR(addr) ((addr).type == PF_ADDR_DYNIFTL && \
447 (!((addr).iflags & PFI_AFLAG_NOALIAS) || \
448 !isdigit((addr).v.ifname[strlen((addr).v.ifname)-1])))
452 %token PASS BLOCK SCRUB RETURN IN OS OUT LOG QUICK ON FROM TO FLAGS
453 %token RETURNRST RETURNICMP RETURNICMP6 PROTO INET INET6 ALL ANY ICMPTYPE
454 %token ICMP6TYPE CODE KEEP MODULATE STATE PORT RDR NAT BINAT ARROW NODF
455 %token MINTTL ERROR ALLOWOPTS FASTROUTE FILENAME ROUTETO DUPTO REPLYTO NO LABEL
456 %token NOROUTE URPFFAILED FRAGMENT USER GROUP MAXMSS MAXIMUM TTL TOS DROP TABLE
457 %token REASSEMBLE FRAGDROP FRAGCROP ANCHOR NATANCHOR RDRANCHOR BINATANCHOR
458 %token SET OPTIMIZATION TIMEOUT LIMIT LOGINTERFACE BLOCKPOLICY RANDOMID
459 %token REQUIREORDER SYNPROXY FINGERPRINTS NOSYNC DEBUG SKIP HOSTID
460 %token ANTISPOOF FOR INCLUDE
461 %token BITMASK RANDOM SOURCEHASH ROUNDROBIN STATICPORT PROBABILITY
462 %token ALTQ CBQ CODEL PRIQ HFSC FAIRQ BANDWIDTH TBRSIZE LINKSHARE REALTIME
463 %token UPPERLIMIT QUEUE PRIORITY QLIMIT HOGS BUCKETS RTABLE TARGET INTERVAL
464 %token LOAD RULESET_OPTIMIZATION PRIO
465 %token STICKYADDRESS MAXSRCSTATES MAXSRCNODES SOURCETRACK GLOBAL RULE
466 %token MAXSRCCONN MAXSRCCONNRATE OVERLOAD FLUSH SLOPPY
467 %token TAGGED TAG IFBOUND FLOATING STATEPOLICY STATEDEFAULTS ROUTE SETTOS
468 %token DIVERTTO DIVERTREPLY
469 %token <v.string> STRING
470 %token <v.number> NUMBER
471 %token <v.i> PORTBINARY
472 %type <v.interface> interface if_list if_item_not if_item
473 %type <v.number> number icmptype icmp6type uid gid
474 %type <v.number> tos not yesno
475 %type <v.probability> probability
476 %type <v.i> no dir af fragcache optimizer
477 %type <v.i> sourcetrack flush unaryop statelock
478 %type <v.b> action nataction natpasslog scrubaction
479 %type <v.b> flags flag blockspec prio
480 %type <v.range> portplain portstar portrange
481 %type <v.hashkey> hashkey
482 %type <v.proto> proto proto_list proto_item
483 %type <v.number> protoval
484 %type <v.icmp> icmpspec
485 %type <v.icmp> icmp_list icmp_item
486 %type <v.icmp> icmp6_list icmp6_item
487 %type <v.number> reticmpspec reticmp6spec
488 %type <v.fromto> fromto
489 %type <v.peer> ipportspec from to
490 %type <v.host> ipspec toipspec xhost host dynaddr host_list
491 %type <v.host> redir_host_list redirspec
492 %type <v.host> route_host route_host_list routespec
493 %type <v.os> os xos os_list
494 %type <v.port> portspec port_list port_item
495 %type <v.uid> uids uid_list uid_item
496 %type <v.gid> gids gid_list gid_item
497 %type <v.route> route
498 %type <v.redirection> redirection redirpool
499 %type <v.string> label stringall tag anchorname
500 %type <v.string> string varstring numberstring
501 %type <v.keep_state> keep
502 %type <v.state_opt> state_opt_spec state_opt_list state_opt_item
503 %type <v.logquick> logquick quick log logopts logopt
504 %type <v.interface> antispoof_ifspc antispoof_iflst antispoof_if
505 %type <v.qassign> qname
506 %type <v.queue> qassign qassign_list qassign_item
507 %type <v.queue_options> scheduler
508 %type <v.number> cbqflags_list cbqflags_item
509 %type <v.number> priqflags_list priqflags_item
510 %type <v.hfsc_opts> hfscopts_list hfscopts_item hfsc_opts
511 %type <v.fairq_opts> fairqopts_list fairqopts_item fairq_opts
512 %type <v.codel_opts> codelopts_list codelopts_item codel_opts
513 %type <v.queue_bwspec> bandwidth
514 %type <v.filter_opts> filter_opts filter_opt filter_opts_l
515 %type <v.filter_opts> filter_sets filter_set filter_sets_l
516 %type <v.antispoof_opts> antispoof_opts antispoof_opt antispoof_opts_l
517 %type <v.queue_opts> queue_opts queue_opt queue_opts_l
518 %type <v.scrub_opts> scrub_opts scrub_opt scrub_opts_l
519 %type <v.table_opts> table_opts table_opt table_opts_l
520 %type <v.pool_opts> pool_opts pool_opt pool_opts_l
521 %type <v.tagged> tagged
522 %type <v.rtableid> rtable
525 ruleset : /* empty */
526 | ruleset include '\n'
528 | ruleset option '\n'
529 | ruleset scrubrule '\n'
530 | ruleset natrule '\n'
531 | ruleset binatrule '\n'
532 | ruleset pfrule '\n'
533 | ruleset anchorrule '\n'
534 | ruleset loadrule '\n'
535 | ruleset altqif '\n'
536 | ruleset queuespec '\n'
537 | ruleset varset '\n'
538 | ruleset antispoof '\n'
539 | ruleset tabledef '\n'
540 | '{' fakeanchor '}' '\n';
541 | ruleset error '\n' { file->errors++; }
544 include : INCLUDE STRING {
547 if ((nfile = pushfile($2, 0)) == NULL) {
548 yyerror("failed to include file %s", $2);
560 * apply to previouslys specified rule: must be careful to note
561 * what that is: pf or nat or binat or rdr
563 fakeanchor : fakeanchor '\n'
564 | fakeanchor anchorrule '\n'
565 | fakeanchor binatrule '\n'
566 | fakeanchor natrule '\n'
567 | fakeanchor pfrule '\n'
568 | fakeanchor error '\n'
572 if (!strcmp($1, "none"))
574 else if (!strcmp($1, "basic"))
575 $$ = PF_OPTIMIZE_BASIC;
576 else if (!strcmp($1, "profile"))
577 $$ = PF_OPTIMIZE_BASIC | PF_OPTIMIZE_PROFILE;
579 yyerror("unknown ruleset-optimization %s", $1);
585 option : SET OPTIMIZATION STRING {
586 if (check_rulestate(PFCTL_STATE_OPTION)) {
590 if (pfctl_set_optimization(pf, $3) != 0) {
591 yyerror("unknown optimization %s", $3);
597 | SET RULESET_OPTIMIZATION optimizer {
598 if (!(pf->opts & PF_OPT_OPTIMIZE)) {
599 pf->opts |= PF_OPT_OPTIMIZE;
603 | SET TIMEOUT timeout_spec
604 | SET TIMEOUT '{' optnl timeout_list '}'
605 | SET LIMIT limit_spec
606 | SET LIMIT '{' optnl limit_list '}'
607 | SET LOGINTERFACE stringall {
608 if (check_rulestate(PFCTL_STATE_OPTION)) {
612 if (pfctl_set_logif(pf, $3) != 0) {
613 yyerror("error setting loginterface %s", $3);
619 | SET HOSTID number {
620 if ($3 == 0 || $3 > UINT_MAX) {
621 yyerror("hostid must be non-zero");
624 if (pfctl_set_hostid(pf, $3) != 0) {
625 yyerror("error setting hostid %08x", $3);
629 | SET BLOCKPOLICY DROP {
630 if (pf->opts & PF_OPT_VERBOSE)
631 printf("set block-policy drop\n");
632 if (check_rulestate(PFCTL_STATE_OPTION))
634 blockpolicy = PFRULE_DROP;
636 | SET BLOCKPOLICY RETURN {
637 if (pf->opts & PF_OPT_VERBOSE)
638 printf("set block-policy return\n");
639 if (check_rulestate(PFCTL_STATE_OPTION))
641 blockpolicy = PFRULE_RETURN;
643 | SET REQUIREORDER yesno {
644 if (pf->opts & PF_OPT_VERBOSE)
645 printf("set require-order %s\n",
646 $3 == 1 ? "yes" : "no");
649 | SET FINGERPRINTS STRING {
650 if (pf->opts & PF_OPT_VERBOSE)
651 printf("set fingerprints \"%s\"\n", $3);
652 if (check_rulestate(PFCTL_STATE_OPTION)) {
656 if (!pf->anchor->name[0]) {
657 if (pfctl_file_fingerprints(pf->dev,
659 yyerror("error loading "
660 "fingerprints %s", $3);
667 | SET STATEPOLICY statelock {
668 if (pf->opts & PF_OPT_VERBOSE)
671 printf("set state-policy floating\n");
674 printf("set state-policy if-bound\n");
677 default_statelock = $3;
680 if (check_rulestate(PFCTL_STATE_OPTION)) {
684 if (pfctl_set_debug(pf, $3) != 0) {
685 yyerror("error setting debuglevel %s", $3);
691 | SET SKIP interface {
692 if (expand_skip_interface($3) != 0) {
693 yyerror("error setting skip interface(s)");
697 | SET STATEDEFAULTS state_opt_list {
698 if (keep_state_defaults != NULL) {
699 yyerror("cannot redefine state-defaults");
702 keep_state_defaults = $3;
706 stringall : STRING { $$ = $1; }
708 if (($$ = strdup("all")) == NULL) {
709 err(1, "stringall: strdup");
714 string : STRING string {
715 if (asprintf(&$$, "%s %s", $1, $2) == -1)
716 err(1, "string: asprintf");
723 varstring : numberstring varstring {
724 if (asprintf(&$$, "%s %s", $1, $2) == -1)
725 err(1, "string: asprintf");
732 numberstring : NUMBER {
734 if (asprintf(&s, "%lld", (long long)$1) == -1) {
735 yyerror("string: asprintf");
743 varset : STRING '=' varstring {
744 if (pf->opts & PF_OPT_VERBOSE)
745 printf("%s = \"%s\"\n", $1, $3);
746 if (symset($1, $3, 0) == -1)
747 err(1, "cannot store variable %s", $1);
753 anchorname : STRING { $$ = $1; }
754 | /* empty */ { $$ = NULL; }
757 pfa_anchorlist : /* empty */
758 | pfa_anchorlist '\n'
759 | pfa_anchorlist pfrule '\n'
760 | pfa_anchorlist anchorrule '\n'
765 char ta[PF_ANCHOR_NAME_SIZE];
766 struct pf_ruleset *rs;
768 /* steping into a brace anchor */
773 /* create a holding ruleset in the root */
774 snprintf(ta, PF_ANCHOR_NAME_SIZE, "_%d", pf->bn);
775 rs = pf_find_or_create_ruleset(ta);
777 err(1, "pfa_anchor: pf_find_or_create_ruleset");
778 pf->astack[pf->asd] = rs->anchor;
779 pf->anchor = rs->anchor;
780 } '\n' pfa_anchorlist '}'
782 pf->alast = pf->anchor;
784 pf->anchor = pf->astack[pf->asd];
789 anchorrule : ANCHOR anchorname dir quick interface af proto fromto
790 filter_opts pfa_anchor
793 struct node_proto *proto;
795 if (check_rulestate(PFCTL_STATE_FILTER)) {
801 if ($2 && ($2[0] == '_' || strstr($2, "/_") != NULL)) {
803 yyerror("anchor names beginning with '_' "
804 "are reserved for internal use");
808 memset(&r, 0, sizeof(r));
809 if (pf->astack[pf->asd + 1]) {
810 /* move inline rules into relative location */
812 &pf->astack[pf->asd]->ruleset,
813 $2 ? $2 : pf->alast->name);
815 if (r.anchor == NULL)
816 err(1, "anchorrule: unable to "
819 if (pf->alast != r.anchor) {
820 if (r.anchor->match) {
821 yyerror("inline anchor '%s' "
826 mv_rules(&pf->alast->ruleset,
829 pf_remove_if_empty_ruleset(&pf->alast->ruleset);
830 pf->alast = r.anchor;
833 yyerror("anchors without explicit "
834 "rules must specify a name");
842 r.rtableid = $9.rtableid;
845 if (strlcpy(r.tagname, $9.tag,
846 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
847 yyerror("tag too long, max %u chars",
848 PF_TAG_NAME_SIZE - 1);
852 if (strlcpy(r.match_tagname, $9.match_tag,
853 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
854 yyerror("tag too long, max %u chars",
855 PF_TAG_NAME_SIZE - 1);
858 r.match_tag_not = $9.match_tag_not;
859 if (rule_label(&r, $9.label))
862 r.flags = $9.flags.b1;
863 r.flagset = $9.flags.b2;
864 if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
865 yyerror("flags always false");
868 if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
869 for (proto = $7; proto != NULL &&
870 proto->proto != IPPROTO_TCP;
873 if (proto == NULL && $7 != NULL) {
874 if ($9.flags.b1 || $9.flags.b2)
876 "flags only apply to tcp");
879 "OS fingerprinting only "
887 if ($9.keep.action) {
888 yyerror("cannot specify state handling "
894 if (strlcpy(r.match_tagname, $9.match_tag,
895 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
896 yyerror("tag too long, max %u chars",
897 PF_TAG_NAME_SIZE - 1);
900 r.match_tag_not = $9.match_tag_not;
901 if ($9.marker & FOM_PRIO) {
903 r.prio = PF_PRIO_ZERO;
907 if ($9.marker & FOM_SETPRIO) {
908 r.set_prio[0] = $9.set_prio[0];
909 r.set_prio[1] = $9.set_prio[1];
910 r.scrub_flags |= PFSTATE_SETPRIO;
913 decide_address_family($8.src.host, &r.af);
914 decide_address_family($8.dst.host, &r.af);
916 expand_rule(&r, $5, NULL, $7, $8.src_os,
917 $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
918 $9.uid, $9.gid, $9.icmpspec,
919 pf->astack[pf->asd + 1] ? pf->alast->name : $2);
921 pf->astack[pf->asd + 1] = NULL;
923 | NATANCHOR string interface af proto fromto rtable {
926 if (check_rulestate(PFCTL_STATE_NAT)) {
931 memset(&r, 0, sizeof(r));
936 decide_address_family($6.src.host, &r.af);
937 decide_address_family($6.dst.host, &r.af);
939 expand_rule(&r, $3, NULL, $5, $6.src_os,
940 $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
944 | RDRANCHOR string interface af proto fromto rtable {
947 if (check_rulestate(PFCTL_STATE_NAT)) {
952 memset(&r, 0, sizeof(r));
957 decide_address_family($6.src.host, &r.af);
958 decide_address_family($6.dst.host, &r.af);
960 if ($6.src.port != NULL) {
961 yyerror("source port parameter not supported"
965 if ($6.dst.port != NULL) {
966 if ($6.dst.port->next != NULL) {
967 yyerror("destination port list "
968 "expansion not supported in "
971 } else if ($6.dst.port->op != PF_OP_EQ) {
972 yyerror("destination port operators"
973 " not supported in rdr-anchor");
976 r.dst.port[0] = $6.dst.port->port[0];
977 r.dst.port[1] = $6.dst.port->port[1];
978 r.dst.port_op = $6.dst.port->op;
981 expand_rule(&r, $3, NULL, $5, $6.src_os,
982 $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
986 | BINATANCHOR string interface af proto fromto rtable {
989 if (check_rulestate(PFCTL_STATE_NAT)) {
994 memset(&r, 0, sizeof(r));
999 if ($5->next != NULL) {
1000 yyerror("proto list expansion"
1001 " not supported in binat-anchor");
1004 r.proto = $5->proto;
1008 if ($6.src.host != NULL || $6.src.port != NULL ||
1009 $6.dst.host != NULL || $6.dst.port != NULL) {
1010 yyerror("fromto parameter not supported"
1011 " in binat-anchor");
1015 decide_address_family($6.src.host, &r.af);
1016 decide_address_family($6.dst.host, &r.af);
1018 pfctl_add_rule(pf, &r, $2);
1023 loadrule : LOAD ANCHOR string FROM string {
1024 struct loadanchors *loadanchor;
1026 if (strlen(pf->anchor->name) + 1 +
1027 strlen($3) >= MAXPATHLEN) {
1028 yyerror("anchorname %s too long, max %u\n",
1029 $3, MAXPATHLEN - 1);
1033 loadanchor = calloc(1, sizeof(struct loadanchors));
1034 if (loadanchor == NULL)
1035 err(1, "loadrule: calloc");
1036 if ((loadanchor->anchorname = malloc(MAXPATHLEN)) ==
1038 err(1, "loadrule: malloc");
1039 if (pf->anchor->name[0])
1040 snprintf(loadanchor->anchorname, MAXPATHLEN,
1041 "%s/%s", pf->anchor->name, $3);
1043 strlcpy(loadanchor->anchorname, $3, MAXPATHLEN);
1044 if ((loadanchor->filename = strdup($5)) == NULL)
1045 err(1, "loadrule: strdup");
1047 TAILQ_INSERT_TAIL(&loadanchorshead, loadanchor,
1054 scrubaction : no SCRUB {
1063 scrubrule : scrubaction dir logquick interface af proto fromto scrub_opts
1067 if (check_rulestate(PFCTL_STATE_SCRUB))
1070 memset(&r, 0, sizeof(r));
1078 yyerror("scrub rules do not support 'quick'");
1084 r.rule_flag |= PFRULE_NODF;
1086 r.rule_flag |= PFRULE_RANDOMID;
1087 if ($8.reassemble_tcp) {
1088 if (r.direction != PF_INOUT) {
1089 yyerror("reassemble tcp rules can not "
1090 "specify direction");
1093 r.rule_flag |= PFRULE_REASSEMBLE_TCP;
1096 r.min_ttl = $8.minttl;
1098 r.max_mss = $8.maxmss;
1099 if ($8.marker & SOM_SETTOS) {
1100 r.rule_flag |= PFRULE_SET_TOS;
1101 r.set_tos = $8.settos;
1104 r.rule_flag |= $8.fragcache;
1106 if (strlcpy(r.match_tagname, $8.match_tag,
1107 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1108 yyerror("tag too long, max %u chars",
1109 PF_TAG_NAME_SIZE - 1);
1112 r.match_tag_not = $8.match_tag_not;
1113 r.rtableid = $8.rtableid;
1115 expand_rule(&r, $4, NULL, $6, $7.src_os,
1116 $7.src.host, $7.src.port, $7.dst.host, $7.dst.port,
1117 NULL, NULL, NULL, "");
1122 bzero(&scrub_opts, sizeof scrub_opts);
1123 scrub_opts.rtableid = -1;
1126 { $$ = scrub_opts; }
1128 bzero(&scrub_opts, sizeof scrub_opts);
1129 scrub_opts.rtableid = -1;
1134 scrub_opts_l : scrub_opts_l scrub_opt
1139 if (scrub_opts.nodf) {
1140 yyerror("no-df cannot be respecified");
1143 scrub_opts.nodf = 1;
1146 if (scrub_opts.marker & SOM_MINTTL) {
1147 yyerror("min-ttl cannot be respecified");
1150 if ($2 < 0 || $2 > 255) {
1151 yyerror("illegal min-ttl value %d", $2);
1154 scrub_opts.marker |= SOM_MINTTL;
1155 scrub_opts.minttl = $2;
1158 if (scrub_opts.marker & SOM_MAXMSS) {
1159 yyerror("max-mss cannot be respecified");
1162 if ($2 < 0 || $2 > 65535) {
1163 yyerror("illegal max-mss value %d", $2);
1166 scrub_opts.marker |= SOM_MAXMSS;
1167 scrub_opts.maxmss = $2;
1170 if (scrub_opts.marker & SOM_SETTOS) {
1171 yyerror("set-tos cannot be respecified");
1174 scrub_opts.marker |= SOM_SETTOS;
1175 scrub_opts.settos = $2;
1178 if (scrub_opts.marker & SOM_FRAGCACHE) {
1179 yyerror("fragcache cannot be respecified");
1182 scrub_opts.marker |= SOM_FRAGCACHE;
1183 scrub_opts.fragcache = $1;
1185 | REASSEMBLE STRING {
1186 if (strcasecmp($2, "tcp") != 0) {
1187 yyerror("scrub reassemble supports only tcp, "
1193 if (scrub_opts.reassemble_tcp) {
1194 yyerror("reassemble tcp cannot be respecified");
1197 scrub_opts.reassemble_tcp = 1;
1200 if (scrub_opts.randomid) {
1201 yyerror("random-id cannot be respecified");
1204 scrub_opts.randomid = 1;
1207 if ($2 < 0 || $2 > rt_tableid_max()) {
1208 yyerror("invalid rtable id");
1211 scrub_opts.rtableid = $2;
1213 | not TAGGED string {
1214 scrub_opts.match_tag = $3;
1215 scrub_opts.match_tag_not = $1;
1219 fragcache : FRAGMENT REASSEMBLE { $$ = 0; /* default */ }
1220 | FRAGMENT FRAGCROP { $$ = 0; }
1221 | FRAGMENT FRAGDROP { $$ = 0; }
1224 antispoof : ANTISPOOF logquick antispoof_ifspc af antispoof_opts {
1226 struct node_host *h = NULL, *hh;
1227 struct node_if *i, *j;
1229 if (check_rulestate(PFCTL_STATE_FILTER))
1232 for (i = $3; i; i = i->next) {
1233 bzero(&r, sizeof(r));
1236 r.direction = PF_IN;
1241 if (rule_label(&r, $5.label))
1243 r.rtableid = $5.rtableid;
1244 j = calloc(1, sizeof(struct node_if));
1246 err(1, "antispoof: calloc");
1247 if (strlcpy(j->ifname, i->ifname,
1248 sizeof(j->ifname)) >= sizeof(j->ifname)) {
1250 yyerror("interface name too long");
1255 h = calloc(1, sizeof(*h));
1257 err(1, "address: calloc");
1258 h->addr.type = PF_ADDR_DYNIFTL;
1260 if (strlcpy(h->addr.v.ifname, i->ifname,
1261 sizeof(h->addr.v.ifname)) >=
1262 sizeof(h->addr.v.ifname)) {
1265 "interface name too long");
1268 hh = malloc(sizeof(*hh));
1270 err(1, "address: malloc");
1271 bcopy(h, hh, sizeof(*hh));
1272 h->addr.iflags = PFI_AFLAG_NETWORK;
1274 h = ifa_lookup(j->ifname,
1280 expand_rule(&r, j, NULL, NULL, NULL, h,
1281 NULL, NULL, NULL, NULL, NULL,
1284 if ((i->ifa_flags & IFF_LOOPBACK) == 0) {
1285 bzero(&r, sizeof(r));
1288 r.direction = PF_IN;
1293 if (rule_label(&r, $5.label))
1295 r.rtableid = $5.rtableid;
1299 h = ifa_lookup(i->ifname, 0);
1301 expand_rule(&r, NULL, NULL,
1302 NULL, NULL, h, NULL, NULL,
1303 NULL, NULL, NULL, NULL, "");
1311 antispoof_ifspc : FOR antispoof_if { $$ = $2; }
1312 | FOR '{' optnl antispoof_iflst '}' { $$ = $4; }
1315 antispoof_iflst : antispoof_if optnl { $$ = $1; }
1316 | antispoof_iflst comma antispoof_if optnl {
1317 $1->tail->next = $3;
1323 antispoof_if : if_item { $$ = $1; }
1331 bzero(&antispoof_opts, sizeof antispoof_opts);
1332 antispoof_opts.rtableid = -1;
1335 { $$ = antispoof_opts; }
1337 bzero(&antispoof_opts, sizeof antispoof_opts);
1338 antispoof_opts.rtableid = -1;
1339 $$ = antispoof_opts;
1343 antispoof_opts_l : antispoof_opts_l antispoof_opt
1347 antispoof_opt : label {
1348 if (antispoof_opts.label) {
1349 yyerror("label cannot be redefined");
1352 antispoof_opts.label = $1;
1355 if ($2 < 0 || $2 > rt_tableid_max()) {
1356 yyerror("invalid rtable id");
1359 antispoof_opts.rtableid = $2;
1363 not : '!' { $$ = 1; }
1364 | /* empty */ { $$ = 0; }
1367 tabledef : TABLE '<' STRING '>' table_opts {
1368 struct node_host *h, *nh;
1369 struct node_tinit *ti, *nti;
1371 if (strlen($3) >= PF_TABLE_NAME_SIZE) {
1372 yyerror("table name too long, max %d chars",
1373 PF_TABLE_NAME_SIZE - 1);
1377 if (pf->loadopt & PFCTL_FLAG_TABLE)
1378 if (process_tabledef($3, &$5)) {
1383 for (ti = SIMPLEQ_FIRST(&$5.init_nodes);
1384 ti != SIMPLEQ_END(&$5.init_nodes); ti = nti) {
1387 for (h = ti->host; h != NULL; h = nh) {
1391 nti = SIMPLEQ_NEXT(ti, entries);
1398 bzero(&table_opts, sizeof table_opts);
1399 SIMPLEQ_INIT(&table_opts.init_nodes);
1402 { $$ = table_opts; }
1405 bzero(&table_opts, sizeof table_opts);
1406 SIMPLEQ_INIT(&table_opts.init_nodes);
1411 table_opts_l : table_opts_l table_opt
1415 table_opt : STRING {
1416 if (!strcmp($1, "const"))
1417 table_opts.flags |= PFR_TFLAG_CONST;
1418 else if (!strcmp($1, "persist"))
1419 table_opts.flags |= PFR_TFLAG_PERSIST;
1420 else if (!strcmp($1, "counters"))
1421 table_opts.flags |= PFR_TFLAG_COUNTERS;
1423 yyerror("invalid table option '%s'", $1);
1429 | '{' optnl '}' { table_opts.init_addr = 1; }
1430 | '{' optnl host_list '}' {
1431 struct node_host *n;
1432 struct node_tinit *ti;
1434 for (n = $3; n != NULL; n = n->next) {
1435 switch (n->addr.type) {
1436 case PF_ADDR_ADDRMASK:
1439 yyerror("address ranges are not "
1440 "permitted inside tables");
1442 case PF_ADDR_DYNIFTL:
1443 yyerror("dynamic addresses are not "
1444 "permitted inside tables");
1447 yyerror("tables cannot contain tables");
1449 case PF_ADDR_NOROUTE:
1450 yyerror("\"no-route\" is not permitted "
1453 case PF_ADDR_URPFFAILED:
1454 yyerror("\"urpf-failed\" is not "
1455 "permitted inside tables");
1458 yyerror("unknown address type %d",
1463 if (!(ti = calloc(1, sizeof(*ti))))
1464 err(1, "table_opt: calloc");
1466 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1468 table_opts.init_addr = 1;
1471 struct node_tinit *ti;
1473 if (!(ti = calloc(1, sizeof(*ti))))
1474 err(1, "table_opt: calloc");
1476 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1478 table_opts.init_addr = 1;
1482 altqif : ALTQ interface queue_opts QUEUE qassign {
1485 if (check_rulestate(PFCTL_STATE_QUEUE))
1488 memset(&a, 0, sizeof(a));
1489 if ($3.scheduler.qtype == ALTQT_NONE) {
1490 yyerror("no scheduler specified!");
1493 a.scheduler = $3.scheduler.qtype;
1494 a.qlimit = $3.qlimit;
1495 a.tbrsize = $3.tbrsize;
1496 if ($5 == NULL && $3.scheduler.qtype != ALTQT_CODEL) {
1497 yyerror("no child queues specified");
1500 if (expand_altq(&a, $2, $5, $3.queue_bwspec,
1506 queuespec : QUEUE STRING interface queue_opts qassign {
1509 if (check_rulestate(PFCTL_STATE_QUEUE)) {
1514 memset(&a, 0, sizeof(a));
1516 if (strlcpy(a.qname, $2, sizeof(a.qname)) >=
1518 yyerror("queue name too long (max "
1519 "%d chars)", PF_QNAME_SIZE-1);
1525 yyerror("cannot specify tbrsize for queue");
1528 if ($4.priority > 255) {
1529 yyerror("priority out of range: max 255");
1532 a.priority = $4.priority;
1533 a.qlimit = $4.qlimit;
1534 a.scheduler = $4.scheduler.qtype;
1535 if (expand_queue(&a, $3, $5, $4.queue_bwspec,
1537 yyerror("errors in queue definition");
1544 bzero(&queue_opts, sizeof queue_opts);
1545 queue_opts.priority = DEFAULT_PRIORITY;
1546 queue_opts.qlimit = DEFAULT_QLIMIT;
1547 queue_opts.scheduler.qtype = ALTQT_NONE;
1548 queue_opts.queue_bwspec.bw_percent = 100;
1551 { $$ = queue_opts; }
1553 bzero(&queue_opts, sizeof queue_opts);
1554 queue_opts.priority = DEFAULT_PRIORITY;
1555 queue_opts.qlimit = DEFAULT_QLIMIT;
1556 queue_opts.scheduler.qtype = ALTQT_NONE;
1557 queue_opts.queue_bwspec.bw_percent = 100;
1562 queue_opts_l : queue_opts_l queue_opt
1566 queue_opt : BANDWIDTH bandwidth {
1567 if (queue_opts.marker & QOM_BWSPEC) {
1568 yyerror("bandwidth cannot be respecified");
1571 queue_opts.marker |= QOM_BWSPEC;
1572 queue_opts.queue_bwspec = $2;
1575 if (queue_opts.marker & QOM_PRIORITY) {
1576 yyerror("priority cannot be respecified");
1579 if ($2 < 0 || $2 > 255) {
1580 yyerror("priority out of range: max 255");
1583 queue_opts.marker |= QOM_PRIORITY;
1584 queue_opts.priority = $2;
1587 if (queue_opts.marker & QOM_QLIMIT) {
1588 yyerror("qlimit cannot be respecified");
1591 if ($2 < 0 || $2 > 65535) {
1592 yyerror("qlimit out of range: max 65535");
1595 queue_opts.marker |= QOM_QLIMIT;
1596 queue_opts.qlimit = $2;
1599 if (queue_opts.marker & QOM_SCHEDULER) {
1600 yyerror("scheduler cannot be respecified");
1603 queue_opts.marker |= QOM_SCHEDULER;
1604 queue_opts.scheduler = $1;
1607 if (queue_opts.marker & QOM_TBRSIZE) {
1608 yyerror("tbrsize cannot be respecified");
1611 if ($2 < 0 || $2 > 65535) {
1612 yyerror("tbrsize too big: max 65535");
1615 queue_opts.marker |= QOM_TBRSIZE;
1616 queue_opts.tbrsize = $2;
1620 bandwidth : STRING {
1626 bps = strtod($1, &cp);
1628 if (strlen(cp) > 1) {
1630 if (!strcmp(cu, "Bit") ||
1632 !strcmp(cu, "bit") ||
1637 if (!strcmp(cp, "b"))
1639 else if (!strcmp(cp, "K"))
1641 else if (!strcmp(cp, "M"))
1643 else if (!strcmp(cp, "G"))
1644 bps *= 1000 * 1000 * 1000;
1645 else if (!strcmp(cp, "%")) {
1646 if (bps < 0 || bps > 100) {
1647 yyerror("bandwidth spec "
1652 $$.bw_percent = bps;
1655 yyerror("unknown unit %s", cp);
1661 $$.bw_absolute = (u_int32_t)bps;
1664 if ($1 < 0 || $1 > UINT_MAX) {
1665 yyerror("bandwidth number too big");
1669 $$.bw_absolute = $1;
1674 $$.qtype = ALTQT_CBQ;
1675 $$.data.cbq_opts.flags = 0;
1677 | CBQ '(' cbqflags_list ')' {
1678 $$.qtype = ALTQT_CBQ;
1679 $$.data.cbq_opts.flags = $3;
1682 $$.qtype = ALTQT_PRIQ;
1683 $$.data.priq_opts.flags = 0;
1685 | PRIQ '(' priqflags_list ')' {
1686 $$.qtype = ALTQT_PRIQ;
1687 $$.data.priq_opts.flags = $3;
1690 $$.qtype = ALTQT_HFSC;
1691 bzero(&$$.data.hfsc_opts,
1692 sizeof(struct node_hfsc_opts));
1694 | HFSC '(' hfsc_opts ')' {
1695 $$.qtype = ALTQT_HFSC;
1696 $$.data.hfsc_opts = $3;
1699 $$.qtype = ALTQT_FAIRQ;
1700 bzero(&$$.data.fairq_opts,
1701 sizeof(struct node_fairq_opts));
1703 | FAIRQ '(' fairq_opts ')' {
1704 $$.qtype = ALTQT_FAIRQ;
1705 $$.data.fairq_opts = $3;
1708 $$.qtype = ALTQT_CODEL;
1709 bzero(&$$.data.codel_opts,
1710 sizeof(struct codel_opts));
1712 | CODEL '(' codel_opts ')' {
1713 $$.qtype = ALTQT_CODEL;
1714 $$.data.codel_opts = $3;
1718 cbqflags_list : cbqflags_item { $$ |= $1; }
1719 | cbqflags_list comma cbqflags_item { $$ |= $3; }
1722 cbqflags_item : STRING {
1723 if (!strcmp($1, "default"))
1724 $$ = CBQCLF_DEFCLASS;
1725 else if (!strcmp($1, "borrow"))
1727 else if (!strcmp($1, "red"))
1729 else if (!strcmp($1, "ecn"))
1730 $$ = CBQCLF_RED|CBQCLF_ECN;
1731 else if (!strcmp($1, "rio"))
1733 else if (!strcmp($1, "codel"))
1736 yyerror("unknown cbq flag \"%s\"", $1);
1744 priqflags_list : priqflags_item { $$ |= $1; }
1745 | priqflags_list comma priqflags_item { $$ |= $3; }
1748 priqflags_item : STRING {
1749 if (!strcmp($1, "default"))
1750 $$ = PRCF_DEFAULTCLASS;
1751 else if (!strcmp($1, "red"))
1753 else if (!strcmp($1, "ecn"))
1754 $$ = PRCF_RED|PRCF_ECN;
1755 else if (!strcmp($1, "rio"))
1757 else if (!strcmp($1, "codel"))
1760 yyerror("unknown priq flag \"%s\"", $1);
1770 sizeof(struct node_hfsc_opts));
1777 hfscopts_list : hfscopts_item
1778 | hfscopts_list comma hfscopts_item
1781 hfscopts_item : LINKSHARE bandwidth {
1782 if (hfsc_opts.linkshare.used) {
1783 yyerror("linkshare already specified");
1786 hfsc_opts.linkshare.m2 = $2;
1787 hfsc_opts.linkshare.used = 1;
1789 | LINKSHARE '(' bandwidth comma NUMBER comma bandwidth ')'
1791 if ($5 < 0 || $5 > INT_MAX) {
1792 yyerror("timing in curve out of range");
1795 if (hfsc_opts.linkshare.used) {
1796 yyerror("linkshare already specified");
1799 hfsc_opts.linkshare.m1 = $3;
1800 hfsc_opts.linkshare.d = $5;
1801 hfsc_opts.linkshare.m2 = $7;
1802 hfsc_opts.linkshare.used = 1;
1804 | REALTIME bandwidth {
1805 if (hfsc_opts.realtime.used) {
1806 yyerror("realtime already specified");
1809 hfsc_opts.realtime.m2 = $2;
1810 hfsc_opts.realtime.used = 1;
1812 | REALTIME '(' bandwidth comma NUMBER comma bandwidth ')'
1814 if ($5 < 0 || $5 > INT_MAX) {
1815 yyerror("timing in curve out of range");
1818 if (hfsc_opts.realtime.used) {
1819 yyerror("realtime already specified");
1822 hfsc_opts.realtime.m1 = $3;
1823 hfsc_opts.realtime.d = $5;
1824 hfsc_opts.realtime.m2 = $7;
1825 hfsc_opts.realtime.used = 1;
1827 | UPPERLIMIT bandwidth {
1828 if (hfsc_opts.upperlimit.used) {
1829 yyerror("upperlimit already specified");
1832 hfsc_opts.upperlimit.m2 = $2;
1833 hfsc_opts.upperlimit.used = 1;
1835 | UPPERLIMIT '(' bandwidth comma NUMBER comma bandwidth ')'
1837 if ($5 < 0 || $5 > INT_MAX) {
1838 yyerror("timing in curve out of range");
1841 if (hfsc_opts.upperlimit.used) {
1842 yyerror("upperlimit already specified");
1845 hfsc_opts.upperlimit.m1 = $3;
1846 hfsc_opts.upperlimit.d = $5;
1847 hfsc_opts.upperlimit.m2 = $7;
1848 hfsc_opts.upperlimit.used = 1;
1851 if (!strcmp($1, "default"))
1852 hfsc_opts.flags |= HFCF_DEFAULTCLASS;
1853 else if (!strcmp($1, "red"))
1854 hfsc_opts.flags |= HFCF_RED;
1855 else if (!strcmp($1, "ecn"))
1856 hfsc_opts.flags |= HFCF_RED|HFCF_ECN;
1857 else if (!strcmp($1, "rio"))
1858 hfsc_opts.flags |= HFCF_RIO;
1859 else if (!strcmp($1, "codel"))
1860 hfsc_opts.flags |= HFCF_CODEL;
1862 yyerror("unknown hfsc flag \"%s\"", $1);
1872 sizeof(struct node_fairq_opts));
1879 fairqopts_list : fairqopts_item
1880 | fairqopts_list comma fairqopts_item
1883 fairqopts_item : LINKSHARE bandwidth {
1884 if (fairq_opts.linkshare.used) {
1885 yyerror("linkshare already specified");
1888 fairq_opts.linkshare.m2 = $2;
1889 fairq_opts.linkshare.used = 1;
1891 | LINKSHARE '(' bandwidth number bandwidth ')' {
1892 if (fairq_opts.linkshare.used) {
1893 yyerror("linkshare already specified");
1896 fairq_opts.linkshare.m1 = $3;
1897 fairq_opts.linkshare.d = $4;
1898 fairq_opts.linkshare.m2 = $5;
1899 fairq_opts.linkshare.used = 1;
1902 fairq_opts.hogs_bw = $2;
1905 fairq_opts.nbuckets = $2;
1908 if (!strcmp($1, "default"))
1909 fairq_opts.flags |= FARF_DEFAULTCLASS;
1910 else if (!strcmp($1, "red"))
1911 fairq_opts.flags |= FARF_RED;
1912 else if (!strcmp($1, "ecn"))
1913 fairq_opts.flags |= FARF_RED|FARF_ECN;
1914 else if (!strcmp($1, "rio"))
1915 fairq_opts.flags |= FARF_RIO;
1916 else if (!strcmp($1, "codel"))
1917 fairq_opts.flags |= FARF_CODEL;
1919 yyerror("unknown fairq flag \"%s\"", $1);
1929 sizeof(struct codel_opts));
1936 codelopts_list : codelopts_item
1937 | codelopts_list comma codelopts_item
1940 codelopts_item : INTERVAL number {
1941 if (codel_opts.interval) {
1942 yyerror("interval already specified");
1945 codel_opts.interval = $2;
1948 if (codel_opts.target) {
1949 yyerror("target already specified");
1952 codel_opts.target = $2;
1955 if (!strcmp($1, "ecn"))
1958 yyerror("unknown codel option \"%s\"", $1);
1966 qassign : /* empty */ { $$ = NULL; }
1967 | qassign_item { $$ = $1; }
1968 | '{' optnl qassign_list '}' { $$ = $3; }
1971 qassign_list : qassign_item optnl { $$ = $1; }
1972 | qassign_list comma qassign_item optnl {
1973 $1->tail->next = $3;
1979 qassign_item : STRING {
1980 $$ = calloc(1, sizeof(struct node_queue));
1982 err(1, "qassign_item: calloc");
1983 if (strlcpy($$->queue, $1, sizeof($$->queue)) >=
1984 sizeof($$->queue)) {
1985 yyerror("queue name '%s' too long (max "
1986 "%d chars)", $1, sizeof($$->queue)-1);
1997 pfrule : action dir logquick interface route af proto fromto
2001 struct node_state_opt *o;
2002 struct node_proto *proto;
2008 if (check_rulestate(PFCTL_STATE_FILTER))
2011 memset(&r, 0, sizeof(r));
2015 case PFRULE_RETURNRST:
2016 r.rule_flag |= PFRULE_RETURNRST;
2017 r.return_ttl = $1.w;
2019 case PFRULE_RETURNICMP:
2020 r.rule_flag |= PFRULE_RETURNICMP;
2021 r.return_icmp = $1.w;
2022 r.return_icmp6 = $1.w2;
2025 r.rule_flag |= PFRULE_RETURN;
2026 r.return_icmp = $1.w;
2027 r.return_icmp6 = $1.w2;
2035 r.rtableid = $9.rtableid;
2037 if ($9.marker & FOM_PRIO) {
2039 r.prio = PF_PRIO_ZERO;
2043 if ($9.marker & FOM_SETPRIO) {
2044 r.set_prio[0] = $9.set_prio[0];
2045 r.set_prio[1] = $9.set_prio[1];
2046 r.scrub_flags |= PFSTATE_SETPRIO;
2051 if (strlcpy(r.tagname, $9.tag,
2052 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
2053 yyerror("tag too long, max %u chars",
2054 PF_TAG_NAME_SIZE - 1);
2058 if (strlcpy(r.match_tagname, $9.match_tag,
2059 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
2060 yyerror("tag too long, max %u chars",
2061 PF_TAG_NAME_SIZE - 1);
2064 r.match_tag_not = $9.match_tag_not;
2065 if (rule_label(&r, $9.label))
2068 r.flags = $9.flags.b1;
2069 r.flagset = $9.flags.b2;
2070 if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
2071 yyerror("flags always false");
2074 if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
2075 for (proto = $7; proto != NULL &&
2076 proto->proto != IPPROTO_TCP;
2077 proto = proto->next)
2079 if (proto == NULL && $7 != NULL) {
2080 if ($9.flags.b1 || $9.flags.b2)
2082 "flags only apply to tcp");
2085 "OS fingerprinting only "
2090 if (($9.flags.b1 & parse_flags("S")) == 0 &&
2092 yyerror("OS fingerprinting requires "
2093 "the SYN TCP flag (flags S/SA)");
2100 r.keep_state = $9.keep.action;
2101 o = $9.keep.options;
2103 /* 'keep state' by default on pass rules. */
2104 if (!r.keep_state && !r.action &&
2105 !($9.marker & FOM_KEEP)) {
2106 r.keep_state = PF_STATE_NORMAL;
2107 o = keep_state_defaults;
2112 struct node_state_opt *p = o;
2115 case PF_STATE_OPT_MAX:
2117 yyerror("state option 'max' "
2118 "multiple definitions");
2121 r.max_states = o->data.max_states;
2123 case PF_STATE_OPT_NOSYNC:
2124 if (r.rule_flag & PFRULE_NOSYNC) {
2125 yyerror("state option 'sync' "
2126 "multiple definitions");
2129 r.rule_flag |= PFRULE_NOSYNC;
2131 case PF_STATE_OPT_SRCTRACK:
2133 yyerror("state option "
2135 "multiple definitions");
2138 srctrack = o->data.src_track;
2139 r.rule_flag |= PFRULE_SRCTRACK;
2141 case PF_STATE_OPT_MAX_SRC_STATES:
2142 if (r.max_src_states) {
2143 yyerror("state option "
2145 "multiple definitions");
2148 if (o->data.max_src_states == 0) {
2149 yyerror("'max-src-states' must "
2154 o->data.max_src_states;
2155 r.rule_flag |= PFRULE_SRCTRACK;
2157 case PF_STATE_OPT_OVERLOAD:
2158 if (r.overload_tblname[0]) {
2159 yyerror("multiple 'overload' "
2160 "table definitions");
2163 if (strlcpy(r.overload_tblname,
2164 o->data.overload.tblname,
2165 PF_TABLE_NAME_SIZE) >=
2166 PF_TABLE_NAME_SIZE) {
2167 yyerror("state option: "
2171 r.flush = o->data.overload.flush;
2173 case PF_STATE_OPT_MAX_SRC_CONN:
2174 if (r.max_src_conn) {
2175 yyerror("state option "
2177 "multiple definitions");
2180 if (o->data.max_src_conn == 0) {
2181 yyerror("'max-src-conn' "
2186 o->data.max_src_conn;
2187 r.rule_flag |= PFRULE_SRCTRACK |
2188 PFRULE_RULESRCTRACK;
2190 case PF_STATE_OPT_MAX_SRC_CONN_RATE:
2191 if (r.max_src_conn_rate.limit) {
2192 yyerror("state option "
2193 "'max-src-conn-rate' "
2194 "multiple definitions");
2197 if (!o->data.max_src_conn_rate.limit ||
2198 !o->data.max_src_conn_rate.seconds) {
2199 yyerror("'max-src-conn-rate' "
2200 "values must be > 0");
2203 if (o->data.max_src_conn_rate.limit >
2205 yyerror("'max-src-conn-rate' "
2206 "maximum rate must be < %u",
2210 r.max_src_conn_rate.limit =
2211 o->data.max_src_conn_rate.limit;
2212 r.max_src_conn_rate.seconds =
2213 o->data.max_src_conn_rate.seconds;
2214 r.rule_flag |= PFRULE_SRCTRACK |
2215 PFRULE_RULESRCTRACK;
2217 case PF_STATE_OPT_MAX_SRC_NODES:
2218 if (r.max_src_nodes) {
2219 yyerror("state option "
2221 "multiple definitions");
2224 if (o->data.max_src_nodes == 0) {
2225 yyerror("'max-src-nodes' must "
2230 o->data.max_src_nodes;
2231 r.rule_flag |= PFRULE_SRCTRACK |
2232 PFRULE_RULESRCTRACK;
2234 case PF_STATE_OPT_STATELOCK:
2236 yyerror("state locking option: "
2237 "multiple definitions");
2241 r.rule_flag |= o->data.statelock;
2243 case PF_STATE_OPT_SLOPPY:
2244 if (r.rule_flag & PFRULE_STATESLOPPY) {
2245 yyerror("state sloppy option: "
2246 "multiple definitions");
2249 r.rule_flag |= PFRULE_STATESLOPPY;
2251 case PF_STATE_OPT_TIMEOUT:
2252 if (o->data.timeout.number ==
2253 PFTM_ADAPTIVE_START ||
2254 o->data.timeout.number ==
2257 if (r.timeout[o->data.timeout.number]) {
2258 yyerror("state timeout %s "
2259 "multiple definitions",
2260 pf_timeouts[o->data.
2261 timeout.number].name);
2264 r.timeout[o->data.timeout.number] =
2265 o->data.timeout.seconds;
2272 /* 'flags S/SA' by default on stateful rules */
2273 if (!r.action && !r.flags && !r.flagset &&
2274 !$9.fragment && !($9.marker & FOM_FLAGS) &&
2276 r.flags = parse_flags("S");
2277 r.flagset = parse_flags("SA");
2279 if (!adaptive && r.max_states) {
2280 r.timeout[PFTM_ADAPTIVE_START] =
2281 (r.max_states / 10) * 6;
2282 r.timeout[PFTM_ADAPTIVE_END] =
2283 (r.max_states / 10) * 12;
2285 if (r.rule_flag & PFRULE_SRCTRACK) {
2286 if (srctrack == PF_SRCTRACK_GLOBAL &&
2288 yyerror("'max-src-nodes' is "
2289 "incompatible with "
2290 "'source-track global'");
2293 if (srctrack == PF_SRCTRACK_GLOBAL &&
2295 yyerror("'max-src-conn' is "
2296 "incompatible with "
2297 "'source-track global'");
2300 if (srctrack == PF_SRCTRACK_GLOBAL &&
2301 r.max_src_conn_rate.seconds) {
2302 yyerror("'max-src-conn-rate' is "
2303 "incompatible with "
2304 "'source-track global'");
2307 if (r.timeout[PFTM_SRC_NODE] <
2308 r.max_src_conn_rate.seconds)
2309 r.timeout[PFTM_SRC_NODE] =
2310 r.max_src_conn_rate.seconds;
2311 r.rule_flag |= PFRULE_SRCTRACK;
2312 if (srctrack == PF_SRCTRACK_RULE)
2313 r.rule_flag |= PFRULE_RULESRCTRACK;
2315 if (r.keep_state && !statelock)
2316 r.rule_flag |= default_statelock;
2319 r.rule_flag |= PFRULE_FRAGMENT;
2320 r.allow_opts = $9.allowopts;
2322 decide_address_family($8.src.host, &r.af);
2323 decide_address_family($8.dst.host, &r.af);
2327 yyerror("direction must be explicit "
2328 "with rules that specify routing");
2332 r.rpool.opts = $5.pool_opts;
2334 memcpy(&r.rpool.key, $5.key,
2335 sizeof(struct pf_poolhashkey));
2338 decide_address_family($5.host, &r.af);
2339 remove_invalid_hosts(&$5.host, &r.af);
2340 if ($5.host == NULL) {
2341 yyerror("no routing address with "
2342 "matching address family found.");
2345 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
2346 PF_POOL_NONE && ($5.host->next != NULL ||
2347 $5.host->addr.type == PF_ADDR_TABLE ||
2348 DYNIF_MULTIADDR($5.host->addr)))
2349 r.rpool.opts |= PF_POOL_ROUNDROBIN;
2350 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2351 PF_POOL_ROUNDROBIN &&
2352 disallow_table($5.host, "tables are only "
2353 "supported in round-robin routing pools"))
2355 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2356 PF_POOL_ROUNDROBIN &&
2357 disallow_alias($5.host, "interface (%s) "
2358 "is only supported in round-robin "
2361 if ($5.host->next != NULL) {
2362 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2363 PF_POOL_ROUNDROBIN) {
2364 yyerror("r.rpool.opts must "
2365 "be PF_POOL_ROUNDROBIN");
2370 if ($9.queues.qname != NULL) {
2371 if (strlcpy(r.qname, $9.queues.qname,
2372 sizeof(r.qname)) >= sizeof(r.qname)) {
2373 yyerror("rule qname too long (max "
2374 "%d chars)", sizeof(r.qname)-1);
2377 free($9.queues.qname);
2379 if ($9.queues.pqname != NULL) {
2380 if (strlcpy(r.pqname, $9.queues.pqname,
2381 sizeof(r.pqname)) >= sizeof(r.pqname)) {
2382 yyerror("rule pqname too long (max "
2383 "%d chars)", sizeof(r.pqname)-1);
2386 free($9.queues.pqname);
2389 r.divert.port = $9.divert.port;
2391 if ((r.divert.port = $9.divert.port)) {
2392 if (r.direction == PF_OUT) {
2393 if ($9.divert.addr) {
2394 yyerror("address specified "
2395 "for outgoing divert");
2398 bzero(&r.divert.addr,
2399 sizeof(r.divert.addr));
2401 if (!$9.divert.addr) {
2402 yyerror("no address specified "
2403 "for incoming divert");
2406 if ($9.divert.addr->af != r.af) {
2407 yyerror("address family "
2408 "mismatch for divert");
2412 $9.divert.addr->addr.v.a.addr;
2417 expand_rule(&r, $4, $5.host, $7, $8.src_os,
2418 $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
2419 $9.uid, $9.gid, $9.icmpspec, "");
2424 bzero(&filter_opts, sizeof filter_opts);
2425 filter_opts.rtableid = -1;
2428 { $$ = filter_opts; }
2430 bzero(&filter_opts, sizeof filter_opts);
2431 filter_opts.rtableid = -1;
2436 filter_opts_l : filter_opts_l filter_opt
2440 filter_opt : USER uids {
2441 if (filter_opts.uid)
2442 $2->tail->next = filter_opts.uid;
2443 filter_opts.uid = $2;
2446 if (filter_opts.gid)
2447 $2->tail->next = filter_opts.gid;
2448 filter_opts.gid = $2;
2451 if (filter_opts.marker & FOM_FLAGS) {
2452 yyerror("flags cannot be redefined");
2455 filter_opts.marker |= FOM_FLAGS;
2456 filter_opts.flags.b1 |= $1.b1;
2457 filter_opts.flags.b2 |= $1.b2;
2458 filter_opts.flags.w |= $1.w;
2459 filter_opts.flags.w2 |= $1.w2;
2462 if (filter_opts.marker & FOM_ICMP) {
2463 yyerror("icmp-type cannot be redefined");
2466 filter_opts.marker |= FOM_ICMP;
2467 filter_opts.icmpspec = $1;
2470 if (filter_opts.marker & FOM_PRIO) {
2471 yyerror("prio cannot be redefined");
2474 if ($2 < 0 || $2 > PF_PRIO_MAX) {
2475 yyerror("prio must be 0 - %u", PF_PRIO_MAX);
2478 filter_opts.marker |= FOM_PRIO;
2479 filter_opts.prio = $2;
2482 if (filter_opts.marker & FOM_TOS) {
2483 yyerror("tos cannot be redefined");
2486 filter_opts.marker |= FOM_TOS;
2487 filter_opts.tos = $2;
2490 if (filter_opts.marker & FOM_KEEP) {
2491 yyerror("modulate or keep cannot be redefined");
2494 filter_opts.marker |= FOM_KEEP;
2495 filter_opts.keep.action = $1.action;
2496 filter_opts.keep.options = $1.options;
2499 filter_opts.fragment = 1;
2502 filter_opts.allowopts = 1;
2505 if (filter_opts.label) {
2506 yyerror("label cannot be redefined");
2509 filter_opts.label = $1;
2512 if (filter_opts.queues.qname) {
2513 yyerror("queue cannot be redefined");
2516 filter_opts.queues = $1;
2519 filter_opts.tag = $2;
2521 | not TAGGED string {
2522 filter_opts.match_tag = $3;
2523 filter_opts.match_tag_not = $1;
2525 | PROBABILITY probability {
2528 p = floor($2 * UINT_MAX + 0.5);
2529 if (p < 0.0 || p > UINT_MAX) {
2530 yyerror("invalid probability: %lf", p);
2533 filter_opts.prob = (u_int32_t)p;
2534 if (filter_opts.prob == 0)
2535 filter_opts.prob = 1;
2538 if ($2 < 0 || $2 > rt_tableid_max()) {
2539 yyerror("invalid rtable id");
2542 filter_opts.rtableid = $2;
2544 | DIVERTTO portplain {
2546 filter_opts.divert.port = $2.a;
2547 if (!filter_opts.divert.port) {
2548 yyerror("invalid divert port: %u", ntohs($2.a));
2553 | DIVERTTO STRING PORT portplain {
2555 if ((filter_opts.divert.addr = host($2)) == NULL) {
2556 yyerror("could not parse divert address: %s",
2565 filter_opts.divert.port = $4.a;
2566 if (!filter_opts.divert.port) {
2567 yyerror("invalid divert port: %u", ntohs($4.a));
2573 yyerror("divert-reply has no meaning in FreeBSD pf(4)");
2576 filter_opts.divert.port = 1; /* some random value */
2582 filter_sets : SET '(' filter_sets_l ')' { $$ = filter_opts; }
2583 | SET filter_set { $$ = filter_opts; }
2586 filter_sets_l : filter_sets_l comma filter_set
2591 if (filter_opts.marker & FOM_SETPRIO) {
2592 yyerror("prio cannot be redefined");
2595 filter_opts.marker |= FOM_SETPRIO;
2596 filter_opts.set_prio[0] = $1.b1;
2597 filter_opts.set_prio[1] = $1.b2;
2599 prio : PRIO NUMBER {
2600 if ($2 < 0 || $2 > PF_PRIO_MAX) {
2601 yyerror("prio must be 0 - %u", PF_PRIO_MAX);
2606 | PRIO '(' NUMBER comma NUMBER ')' {
2607 if ($3 < 0 || $3 > PF_PRIO_MAX ||
2608 $5 < 0 || $5 > PF_PRIO_MAX) {
2609 yyerror("prio must be 0 - %u", PF_PRIO_MAX);
2617 probability : STRING {
2619 double p = strtod($1, &e);
2626 yyerror("invalid probability: %s", $1);
2639 action : PASS { $$.b1 = PF_PASS; $$.b2 = $$.w = 0; }
2640 | BLOCK blockspec { $$ = $2; $$.b1 = PF_DROP; }
2643 blockspec : /* empty */ {
2644 $$.b2 = blockpolicy;
2645 $$.w = returnicmpdefault;
2646 $$.w2 = returnicmp6default;
2649 $$.b2 = PFRULE_DROP;
2654 $$.b2 = PFRULE_RETURNRST;
2658 | RETURNRST '(' TTL NUMBER ')' {
2659 if ($4 < 0 || $4 > 255) {
2660 yyerror("illegal ttl value %d", $4);
2663 $$.b2 = PFRULE_RETURNRST;
2668 $$.b2 = PFRULE_RETURNICMP;
2669 $$.w = returnicmpdefault;
2670 $$.w2 = returnicmp6default;
2673 $$.b2 = PFRULE_RETURNICMP;
2674 $$.w = returnicmpdefault;
2675 $$.w2 = returnicmp6default;
2677 | RETURNICMP '(' reticmpspec ')' {
2678 $$.b2 = PFRULE_RETURNICMP;
2680 $$.w2 = returnicmpdefault;
2682 | RETURNICMP6 '(' reticmp6spec ')' {
2683 $$.b2 = PFRULE_RETURNICMP;
2684 $$.w = returnicmpdefault;
2687 | RETURNICMP '(' reticmpspec comma reticmp6spec ')' {
2688 $$.b2 = PFRULE_RETURNICMP;
2693 $$.b2 = PFRULE_RETURN;
2694 $$.w = returnicmpdefault;
2695 $$.w2 = returnicmp6default;
2699 reticmpspec : STRING {
2700 if (!($$ = parseicmpspec($1, AF_INET))) {
2709 if ($1 < 0 || $1 > 255) {
2710 yyerror("invalid icmp code %lu", $1);
2713 icmptype = returnicmpdefault >> 8;
2714 $$ = (icmptype << 8 | $1);
2718 reticmp6spec : STRING {
2719 if (!($$ = parseicmpspec($1, AF_INET6))) {
2728 if ($1 < 0 || $1 > 255) {
2729 yyerror("invalid icmp code %lu", $1);
2732 icmptype = returnicmp6default >> 8;
2733 $$ = (icmptype << 8 | $1);
2737 dir : /* empty */ { $$ = PF_INOUT; }
2738 | IN { $$ = PF_IN; }
2739 | OUT { $$ = PF_OUT; }
2742 quick : /* empty */ { $$.quick = 0; }
2743 | QUICK { $$.quick = 1; }
2746 logquick : /* empty */ { $$.log = 0; $$.quick = 0; $$.logif = 0; }
2747 | log { $$ = $1; $$.quick = 0; }
2748 | QUICK { $$.quick = 1; $$.log = 0; $$.logif = 0; }
2749 | log QUICK { $$ = $1; $$.quick = 1; }
2750 | QUICK log { $$ = $2; $$.quick = 1; }
2753 log : LOG { $$.log = PF_LOG; $$.logif = 0; }
2754 | LOG '(' logopts ')' {
2755 $$.log = PF_LOG | $3.log;
2756 $$.logif = $3.logif;
2760 logopts : logopt { $$ = $1; }
2761 | logopts comma logopt {
2762 $$.log = $1.log | $3.log;
2763 $$.logif = $3.logif;
2765 $$.logif = $1.logif;
2769 logopt : ALL { $$.log = PF_LOG_ALL; $$.logif = 0; }
2770 | USER { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
2771 | GROUP { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
2777 if (strncmp($2, "pflog", 5)) {
2778 yyerror("%s: should be a pflog interface", $2);
2782 i = strtonum($2 + 5, 0, 255, &errstr);
2784 yyerror("%s: %s", $2, errstr);
2793 interface : /* empty */ { $$ = NULL; }
2794 | ON if_item_not { $$ = $2; }
2795 | ON '{' optnl if_list '}' { $$ = $4; }
2798 if_list : if_item_not optnl { $$ = $1; }
2799 | if_list comma if_item_not optnl {
2800 $1->tail->next = $3;
2806 if_item_not : not if_item { $$ = $2; $$->not = $1; }
2810 struct node_host *n;
2812 $$ = calloc(1, sizeof(struct node_if));
2814 err(1, "if_item: calloc");
2815 if (strlcpy($$->ifname, $1, sizeof($$->ifname)) >=
2816 sizeof($$->ifname)) {
2819 yyerror("interface name too long");
2823 if ((n = ifa_exists($1)) != NULL)
2824 $$->ifa_flags = n->ifa_flags;
2833 af : /* empty */ { $$ = 0; }
2834 | INET { $$ = AF_INET; }
2835 | INET6 { $$ = AF_INET6; }
2838 proto : /* empty */ { $$ = NULL; }
2839 | PROTO proto_item { $$ = $2; }
2840 | PROTO '{' optnl proto_list '}' { $$ = $4; }
2843 proto_list : proto_item optnl { $$ = $1; }
2844 | proto_list comma proto_item optnl {
2845 $1->tail->next = $3;
2851 proto_item : protoval {
2856 yyerror("proto 0 cannot be used");
2859 $$ = calloc(1, sizeof(struct node_proto));
2861 err(1, "proto_item: calloc");
2871 p = getprotobyname($1);
2873 yyerror("unknown protocol %s", $1);
2881 if ($1 < 0 || $1 > 255) {
2882 yyerror("protocol outside range");
2902 os : /* empty */ { $$ = NULL; }
2903 | OS xos { $$ = $2; }
2904 | OS '{' optnl os_list '}' { $$ = $4; }
2908 $$ = calloc(1, sizeof(struct node_os));
2910 err(1, "os: calloc");
2916 os_list : xos optnl { $$ = $1; }
2917 | os_list comma xos optnl {
2918 $1->tail->next = $3;
2924 from : /* empty */ {
2938 if (disallow_urpf_failed($2.host, "\"urpf-failed\" is "
2939 "not permitted in a destination address"))
2945 ipportspec : ipspec {
2949 | ipspec PORT portspec {
2963 ipspec : ANY { $$ = NULL; }
2964 | xhost { $$ = $1; }
2965 | '{' optnl host_list '}' { $$ = $3; }
2968 toipspec : TO ipspec { $$ = $2; }
2969 | /* empty */ { $$ = NULL; }
2972 host_list : ipspec optnl { $$ = $1; }
2973 | host_list comma ipspec optnl {
2976 else if ($1 == NULL)
2979 $1->tail->next = $3;
2980 $1->tail = $3->tail;
2987 struct node_host *n;
2989 for (n = $2; n != NULL; n = n->next)
2994 $$ = calloc(1, sizeof(struct node_host));
2996 err(1, "xhost: calloc");
2997 $$->addr.type = PF_ADDR_NOROUTE;
3003 $$ = calloc(1, sizeof(struct node_host));
3005 err(1, "xhost: calloc");
3006 $$->addr.type = PF_ADDR_URPFFAILED;
3014 if (($$ = host($1)) == NULL) {
3015 /* error. "any" is handled elsewhere */
3017 yyerror("could not parse host specification");
3023 | STRING '-' STRING {
3024 struct node_host *b, *e;
3026 if ((b = host($1)) == NULL || (e = host($3)) == NULL) {
3029 yyerror("could not parse host specification");
3032 if (b->af != e->af ||
3033 b->addr.type != PF_ADDR_ADDRMASK ||
3034 e->addr.type != PF_ADDR_ADDRMASK ||
3035 unmask(&b->addr.v.a.mask, b->af) !=
3036 (b->af == AF_INET ? 32 : 128) ||
3037 unmask(&e->addr.v.a.mask, e->af) !=
3038 (e->af == AF_INET ? 32 : 128) ||
3039 b->next != NULL || b->not ||
3040 e->next != NULL || e->not) {
3045 yyerror("invalid address range");
3048 memcpy(&b->addr.v.a.mask, &e->addr.v.a.addr,
3049 sizeof(b->addr.v.a.mask));
3050 b->addr.type = PF_ADDR_RANGE;
3056 | STRING '/' NUMBER {
3059 if (asprintf(&buf, "%s/%lld", $1, (long long)$3) == -1)
3060 err(1, "host: asprintf");
3062 if (($$ = host(buf)) == NULL) {
3063 /* error. "any" is handled elsewhere */
3065 yyerror("could not parse host specification");
3070 | NUMBER '/' NUMBER {
3073 /* ie. for 10/8 parsing */
3075 if (asprintf(&buf, "%lld/%lld", (long long)$1, (long long)$3) == -1)
3077 if (asprintf(&buf, "%lld/%lld", $1, $3) == -1)
3079 err(1, "host: asprintf");
3080 if (($$ = host(buf)) == NULL) {
3081 /* error. "any" is handled elsewhere */
3083 yyerror("could not parse host specification");
3089 | dynaddr '/' NUMBER {
3090 struct node_host *n;
3092 if ($3 < 0 || $3 > 128) {
3093 yyerror("bit number too big");
3097 for (n = $1; n != NULL; n = n->next)
3101 if (strlen($2) >= PF_TABLE_NAME_SIZE) {
3102 yyerror("table name '%s' too long", $2);
3106 $$ = calloc(1, sizeof(struct node_host));
3108 err(1, "host: calloc");
3109 $$->addr.type = PF_ADDR_TABLE;
3110 if (strlcpy($$->addr.v.tblname, $2,
3111 sizeof($$->addr.v.tblname)) >=
3112 sizeof($$->addr.v.tblname))
3113 errx(1, "host: strlcpy");
3124 if (atoul($1, &ulval) == -1) {
3125 yyerror("%s is not a number", $1);
3134 dynaddr : '(' STRING ')' {
3139 if (!isalpha(op[0])) {
3140 yyerror("invalid interface name '%s'", op);
3144 while ((p = strrchr($2, ':')) != NULL) {
3145 if (!strcmp(p+1, "network"))
3146 flags |= PFI_AFLAG_NETWORK;
3147 else if (!strcmp(p+1, "broadcast"))
3148 flags |= PFI_AFLAG_BROADCAST;
3149 else if (!strcmp(p+1, "peer"))
3150 flags |= PFI_AFLAG_PEER;
3151 else if (!strcmp(p+1, "0"))
3152 flags |= PFI_AFLAG_NOALIAS;
3154 yyerror("interface %s has bad modifier",
3161 if (flags & (flags - 1) & PFI_AFLAG_MODEMASK) {
3163 yyerror("illegal combination of "
3164 "interface modifiers");
3167 $$ = calloc(1, sizeof(struct node_host));
3169 err(1, "address: calloc");
3171 set_ipmask($$, 128);
3172 $$->addr.type = PF_ADDR_DYNIFTL;
3173 $$->addr.iflags = flags;
3174 if (strlcpy($$->addr.v.ifname, $2,
3175 sizeof($$->addr.v.ifname)) >=
3176 sizeof($$->addr.v.ifname)) {
3179 yyerror("interface name too long");
3188 portspec : port_item { $$ = $1; }
3189 | '{' optnl port_list '}' { $$ = $3; }
3192 port_list : port_item optnl { $$ = $1; }
3193 | port_list comma port_item optnl {
3194 $1->tail->next = $3;
3200 port_item : portrange {
3201 $$ = calloc(1, sizeof(struct node_port));
3203 err(1, "port_item: calloc");
3213 | unaryop portrange {
3215 yyerror("':' cannot be used with an other "
3219 $$ = calloc(1, sizeof(struct node_port));
3221 err(1, "port_item: calloc");
3228 | portrange PORTBINARY portrange {
3230 yyerror("':' cannot be used with an other "
3234 $$ = calloc(1, sizeof(struct node_port));
3236 err(1, "port_item: calloc");
3245 portplain : numberstring {
3246 if (parseport($1, &$$, 0) == -1) {
3254 portrange : numberstring {
3255 if (parseport($1, &$$, PPORT_RANGE) == -1) {
3263 uids : uid_item { $$ = $1; }
3264 | '{' optnl uid_list '}' { $$ = $3; }
3267 uid_list : uid_item optnl { $$ = $1; }
3268 | uid_list comma uid_item optnl {
3269 $1->tail->next = $3;
3276 $$ = calloc(1, sizeof(struct node_uid));
3278 err(1, "uid_item: calloc");
3286 if ($2 == UID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
3287 yyerror("user unknown requires operator = or "
3291 $$ = calloc(1, sizeof(struct node_uid));
3293 err(1, "uid_item: calloc");
3300 | uid PORTBINARY uid {
3301 if ($1 == UID_MAX || $3 == UID_MAX) {
3302 yyerror("user unknown requires operator = or "
3306 $$ = calloc(1, sizeof(struct node_uid));
3308 err(1, "uid_item: calloc");
3318 if (!strcmp($1, "unknown"))
3323 if ((pw = getpwnam($1)) == NULL) {
3324 yyerror("unknown user %s", $1);
3333 if ($1 < 0 || $1 >= UID_MAX) {
3334 yyerror("illegal uid value %lu", $1);
3341 gids : gid_item { $$ = $1; }
3342 | '{' optnl gid_list '}' { $$ = $3; }
3345 gid_list : gid_item optnl { $$ = $1; }
3346 | gid_list comma gid_item optnl {
3347 $1->tail->next = $3;
3354 $$ = calloc(1, sizeof(struct node_gid));
3356 err(1, "gid_item: calloc");
3364 if ($2 == GID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
3365 yyerror("group unknown requires operator = or "
3369 $$ = calloc(1, sizeof(struct node_gid));
3371 err(1, "gid_item: calloc");
3378 | gid PORTBINARY gid {
3379 if ($1 == GID_MAX || $3 == GID_MAX) {
3380 yyerror("group unknown requires operator = or "
3384 $$ = calloc(1, sizeof(struct node_gid));
3386 err(1, "gid_item: calloc");
3396 if (!strcmp($1, "unknown"))
3401 if ((grp = getgrnam($1)) == NULL) {
3402 yyerror("unknown group %s", $1);
3411 if ($1 < 0 || $1 >= GID_MAX) {
3412 yyerror("illegal gid value %lu", $1);
3422 if ((f = parse_flags($1)) < 0) {
3423 yyerror("bad flags %s", $1);
3432 flags : FLAGS flag '/' flag { $$.b1 = $2.b1; $$.b2 = $4.b1; }
3433 | FLAGS '/' flag { $$.b1 = 0; $$.b2 = $3.b1; }
3434 | FLAGS ANY { $$.b1 = 0; $$.b2 = 0; }
3437 icmpspec : ICMPTYPE icmp_item { $$ = $2; }
3438 | ICMPTYPE '{' optnl icmp_list '}' { $$ = $4; }
3439 | ICMP6TYPE icmp6_item { $$ = $2; }
3440 | ICMP6TYPE '{' optnl icmp6_list '}' { $$ = $4; }
3443 icmp_list : icmp_item optnl { $$ = $1; }
3444 | icmp_list comma icmp_item optnl {
3445 $1->tail->next = $3;
3451 icmp6_list : icmp6_item optnl { $$ = $1; }
3452 | icmp6_list comma icmp6_item optnl {
3453 $1->tail->next = $3;
3459 icmp_item : icmptype {
3460 $$ = calloc(1, sizeof(struct node_icmp));
3462 err(1, "icmp_item: calloc");
3465 $$->proto = IPPROTO_ICMP;
3469 | icmptype CODE STRING {
3470 const struct icmpcodeent *p;
3472 if ((p = geticmpcodebyname($1-1, $3, AF_INET)) == NULL) {
3473 yyerror("unknown icmp-code %s", $3);
3479 $$ = calloc(1, sizeof(struct node_icmp));
3481 err(1, "icmp_item: calloc");
3483 $$->code = p->code + 1;
3484 $$->proto = IPPROTO_ICMP;
3488 | icmptype CODE NUMBER {
3489 if ($3 < 0 || $3 > 255) {
3490 yyerror("illegal icmp-code %lu", $3);
3493 $$ = calloc(1, sizeof(struct node_icmp));
3495 err(1, "icmp_item: calloc");
3498 $$->proto = IPPROTO_ICMP;
3504 icmp6_item : icmp6type {
3505 $$ = calloc(1, sizeof(struct node_icmp));
3507 err(1, "icmp_item: calloc");
3510 $$->proto = IPPROTO_ICMPV6;
3514 | icmp6type CODE STRING {
3515 const struct icmpcodeent *p;
3517 if ((p = geticmpcodebyname($1-1, $3, AF_INET6)) == NULL) {
3518 yyerror("unknown icmp6-code %s", $3);
3524 $$ = calloc(1, sizeof(struct node_icmp));
3526 err(1, "icmp_item: calloc");
3528 $$->code = p->code + 1;
3529 $$->proto = IPPROTO_ICMPV6;
3533 | icmp6type CODE NUMBER {
3534 if ($3 < 0 || $3 > 255) {
3535 yyerror("illegal icmp-code %lu", $3);
3538 $$ = calloc(1, sizeof(struct node_icmp));
3540 err(1, "icmp_item: calloc");
3543 $$->proto = IPPROTO_ICMPV6;
3550 const struct icmptypeent *p;
3552 if ((p = geticmptypebyname($1, AF_INET)) == NULL) {
3553 yyerror("unknown icmp-type %s", $1);
3561 if ($1 < 0 || $1 > 255) {
3562 yyerror("illegal icmp-type %lu", $1);
3569 icmp6type : STRING {
3570 const struct icmptypeent *p;
3572 if ((p = geticmptypebyname($1, AF_INET6)) ==
3574 yyerror("unknown icmp6-type %s", $1);
3582 if ($1 < 0 || $1 > 255) {
3583 yyerror("illegal icmp6-type %lu", $1);
3594 if (map_tos($1, &val))
3596 else if ($1[0] == '0' && $1[1] == 'x') {
3598 $$ = strtoul($1, &end, 16);
3599 if (errno || *end != '\0')
3602 $$ = 256; /* flag bad argument */
3603 if ($$ < 0 || $$ > 255) {
3604 yyerror("illegal tos value %s", $1);
3612 if ($$ < 0 || $$ > 255) {
3613 yyerror("illegal tos value %s", $1);
3619 sourcetrack : SOURCETRACK { $$ = PF_SRCTRACK; }
3620 | SOURCETRACK GLOBAL { $$ = PF_SRCTRACK_GLOBAL; }
3621 | SOURCETRACK RULE { $$ = PF_SRCTRACK_RULE; }
3624 statelock : IFBOUND {
3625 $$ = PFRULE_IFBOUND;
3636 | KEEP STATE state_opt_spec {
3637 $$.action = PF_STATE_NORMAL;
3640 | MODULATE STATE state_opt_spec {
3641 $$.action = PF_STATE_MODULATE;
3644 | SYNPROXY STATE state_opt_spec {
3645 $$.action = PF_STATE_SYNPROXY;
3650 flush : /* empty */ { $$ = 0; }
3651 | FLUSH { $$ = PF_FLUSH; }
3653 $$ = PF_FLUSH | PF_FLUSH_GLOBAL;
3657 state_opt_spec : '(' state_opt_list ')' { $$ = $2; }
3658 | /* empty */ { $$ = NULL; }
3661 state_opt_list : state_opt_item { $$ = $1; }
3662 | state_opt_list comma state_opt_item {
3663 $1->tail->next = $3;
3669 state_opt_item : MAXIMUM NUMBER {
3670 if ($2 < 0 || $2 > UINT_MAX) {
3671 yyerror("only positive values permitted");
3674 $$ = calloc(1, sizeof(struct node_state_opt));
3676 err(1, "state_opt_item: calloc");
3677 $$->type = PF_STATE_OPT_MAX;
3678 $$->data.max_states = $2;
3683 $$ = calloc(1, sizeof(struct node_state_opt));
3685 err(1, "state_opt_item: calloc");
3686 $$->type = PF_STATE_OPT_NOSYNC;
3690 | MAXSRCSTATES NUMBER {
3691 if ($2 < 0 || $2 > UINT_MAX) {
3692 yyerror("only positive values permitted");
3695 $$ = calloc(1, sizeof(struct node_state_opt));
3697 err(1, "state_opt_item: calloc");
3698 $$->type = PF_STATE_OPT_MAX_SRC_STATES;
3699 $$->data.max_src_states = $2;
3703 | MAXSRCCONN NUMBER {
3704 if ($2 < 0 || $2 > UINT_MAX) {
3705 yyerror("only positive values permitted");
3708 $$ = calloc(1, sizeof(struct node_state_opt));
3710 err(1, "state_opt_item: calloc");
3711 $$->type = PF_STATE_OPT_MAX_SRC_CONN;
3712 $$->data.max_src_conn = $2;
3716 | MAXSRCCONNRATE NUMBER '/' NUMBER {
3717 if ($2 < 0 || $2 > UINT_MAX ||
3718 $4 < 0 || $4 > UINT_MAX) {
3719 yyerror("only positive values permitted");
3722 $$ = calloc(1, sizeof(struct node_state_opt));
3724 err(1, "state_opt_item: calloc");
3725 $$->type = PF_STATE_OPT_MAX_SRC_CONN_RATE;
3726 $$->data.max_src_conn_rate.limit = $2;
3727 $$->data.max_src_conn_rate.seconds = $4;
3731 | OVERLOAD '<' STRING '>' flush {
3732 if (strlen($3) >= PF_TABLE_NAME_SIZE) {
3733 yyerror("table name '%s' too long", $3);
3737 $$ = calloc(1, sizeof(struct node_state_opt));
3739 err(1, "state_opt_item: calloc");
3740 if (strlcpy($$->data.overload.tblname, $3,
3741 PF_TABLE_NAME_SIZE) >= PF_TABLE_NAME_SIZE)
3742 errx(1, "state_opt_item: strlcpy");
3744 $$->type = PF_STATE_OPT_OVERLOAD;
3745 $$->data.overload.flush = $5;
3749 | MAXSRCNODES NUMBER {
3750 if ($2 < 0 || $2 > UINT_MAX) {
3751 yyerror("only positive values permitted");
3754 $$ = calloc(1, sizeof(struct node_state_opt));
3756 err(1, "state_opt_item: calloc");
3757 $$->type = PF_STATE_OPT_MAX_SRC_NODES;
3758 $$->data.max_src_nodes = $2;
3763 $$ = calloc(1, sizeof(struct node_state_opt));
3765 err(1, "state_opt_item: calloc");
3766 $$->type = PF_STATE_OPT_SRCTRACK;
3767 $$->data.src_track = $1;
3772 $$ = calloc(1, sizeof(struct node_state_opt));
3774 err(1, "state_opt_item: calloc");
3775 $$->type = PF_STATE_OPT_STATELOCK;
3776 $$->data.statelock = $1;
3781 $$ = calloc(1, sizeof(struct node_state_opt));
3783 err(1, "state_opt_item: calloc");
3784 $$->type = PF_STATE_OPT_SLOPPY;
3791 if ($2 < 0 || $2 > UINT_MAX) {
3792 yyerror("only positive values permitted");
3795 for (i = 0; pf_timeouts[i].name &&
3796 strcmp(pf_timeouts[i].name, $1); ++i)
3798 if (!pf_timeouts[i].name) {
3799 yyerror("illegal timeout name %s", $1);
3803 if (strchr(pf_timeouts[i].name, '.') == NULL) {
3804 yyerror("illegal state timeout %s", $1);
3809 $$ = calloc(1, sizeof(struct node_state_opt));
3811 err(1, "state_opt_item: calloc");
3812 $$->type = PF_STATE_OPT_TIMEOUT;
3813 $$->data.timeout.number = pf_timeouts[i].timeout;
3814 $$->data.timeout.seconds = $2;
3820 label : LABEL STRING {
3825 qname : QUEUE STRING {
3829 | QUEUE '(' STRING ')' {
3833 | QUEUE '(' STRING comma STRING ')' {
3839 no : /* empty */ { $$ = 0; }
3843 portstar : numberstring {
3844 if (parseport($1, &$$, PPORT_RANGE|PPORT_STAR) == -1) {
3852 redirspec : host { $$ = $1; }
3853 | '{' optnl redir_host_list '}' { $$ = $3; }
3856 redir_host_list : host optnl { $$ = $1; }
3857 | redir_host_list comma host optnl {
3858 $1->tail->next = $3;
3859 $1->tail = $3->tail;
3864 redirpool : /* empty */ { $$ = NULL; }
3866 $$ = calloc(1, sizeof(struct redirection));
3868 err(1, "redirection: calloc");
3870 $$->rport.a = $$->rport.b = $$->rport.t = 0;
3872 | ARROW redirspec PORT portstar {
3873 $$ = calloc(1, sizeof(struct redirection));
3875 err(1, "redirection: calloc");
3881 hashkey : /* empty */
3883 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3885 err(1, "hashkey: calloc");
3886 $$->key32[0] = arc4random();
3887 $$->key32[1] = arc4random();
3888 $$->key32[2] = arc4random();
3889 $$->key32[3] = arc4random();
3893 if (!strncmp($1, "0x", 2)) {
3894 if (strlen($1) != 34) {
3896 yyerror("hex key must be 128 bits "
3897 "(32 hex digits) long");
3900 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3902 err(1, "hashkey: calloc");
3904 if (sscanf($1, "0x%8x%8x%8x%8x",
3905 &$$->key32[0], &$$->key32[1],
3906 &$$->key32[2], &$$->key32[3]) != 4) {
3909 yyerror("invalid hex key");
3915 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3917 err(1, "hashkey: calloc");
3919 MD5Update(&context, (unsigned char *)$1,
3921 MD5Final((unsigned char *)$$, &context);
3922 HTONL($$->key32[0]);
3923 HTONL($$->key32[1]);
3924 HTONL($$->key32[2]);
3925 HTONL($$->key32[3]);
3931 pool_opts : { bzero(&pool_opts, sizeof pool_opts); }
3935 bzero(&pool_opts, sizeof pool_opts);
3940 pool_opts_l : pool_opts_l pool_opt
3944 pool_opt : BITMASK {
3945 if (pool_opts.type) {
3946 yyerror("pool type cannot be redefined");
3949 pool_opts.type = PF_POOL_BITMASK;
3952 if (pool_opts.type) {
3953 yyerror("pool type cannot be redefined");
3956 pool_opts.type = PF_POOL_RANDOM;
3958 | SOURCEHASH hashkey {
3959 if (pool_opts.type) {
3960 yyerror("pool type cannot be redefined");
3963 pool_opts.type = PF_POOL_SRCHASH;
3967 if (pool_opts.type) {
3968 yyerror("pool type cannot be redefined");
3971 pool_opts.type = PF_POOL_ROUNDROBIN;
3974 if (pool_opts.staticport) {
3975 yyerror("static-port cannot be redefined");
3978 pool_opts.staticport = 1;
3981 if (filter_opts.marker & POM_STICKYADDRESS) {
3982 yyerror("sticky-address cannot be redefined");
3985 pool_opts.marker |= POM_STICKYADDRESS;
3986 pool_opts.opts |= PF_POOL_STICKYADDR;
3990 redirection : /* empty */ { $$ = NULL; }
3992 $$ = calloc(1, sizeof(struct redirection));
3994 err(1, "redirection: calloc");
3996 $$->rport.a = $$->rport.b = $$->rport.t = 0;
3998 | ARROW host PORT portstar {
3999 $$ = calloc(1, sizeof(struct redirection));
4001 err(1, "redirection: calloc");
4007 natpasslog : /* empty */ { $$.b1 = $$.b2 = 0; $$.w2 = 0; }
4008 | PASS { $$.b1 = 1; $$.b2 = 0; $$.w2 = 0; }
4009 | PASS log { $$.b1 = 1; $$.b2 = $2.log; $$.w2 = $2.logif; }
4010 | log { $$.b1 = 0; $$.b2 = $1.log; $$.w2 = $1.logif; }
4013 nataction : no NAT natpasslog {
4015 yyerror("\"pass\" not valid with \"no\"");
4026 | no RDR natpasslog {
4028 yyerror("\"pass\" not valid with \"no\"");
4041 natrule : nataction interface af proto fromto tag tagged rtable
4046 if (check_rulestate(PFCTL_STATE_NAT))
4049 memset(&r, 0, sizeof(r));
4058 if ($5.src.host && $5.src.host->af &&
4059 !$5.src.host->ifindex)
4060 r.af = $5.src.host->af;
4061 else if ($5.dst.host && $5.dst.host->af &&
4062 !$5.dst.host->ifindex)
4063 r.af = $5.dst.host->af;
4067 if (strlcpy(r.tagname, $6, PF_TAG_NAME_SIZE) >=
4069 yyerror("tag too long, max %u chars",
4070 PF_TAG_NAME_SIZE - 1);
4075 if (strlcpy(r.match_tagname, $7.name,
4076 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4077 yyerror("tag too long, max %u chars",
4078 PF_TAG_NAME_SIZE - 1);
4081 r.match_tag_not = $7.neg;
4084 if (r.action == PF_NONAT || r.action == PF_NORDR) {
4086 yyerror("translation rule with 'no' "
4087 "does not need '->'");
4091 if ($9 == NULL || $9->host == NULL) {
4092 yyerror("translation rule requires '-> "
4096 if (!r.af && ! $9->host->ifindex)
4097 r.af = $9->host->af;
4099 remove_invalid_hosts(&$9->host, &r.af);
4100 if (invalid_redirect($9->host, r.af))
4102 if (check_netmask($9->host, r.af))
4105 r.rpool.proxy_port[0] = ntohs($9->rport.a);
4109 if (!$9->rport.b && $9->rport.t &&
4110 $5.dst.port != NULL) {
4111 r.rpool.proxy_port[1] =
4112 ntohs($9->rport.a) +
4114 $5.dst.port->port[1]) -
4116 $5.dst.port->port[0]));
4118 r.rpool.proxy_port[1] =
4122 r.rpool.proxy_port[1] =
4124 if (!r.rpool.proxy_port[0] &&
4125 !r.rpool.proxy_port[1]) {
4126 r.rpool.proxy_port[0] =
4127 PF_NAT_PROXY_PORT_LOW;
4128 r.rpool.proxy_port[1] =
4129 PF_NAT_PROXY_PORT_HIGH;
4130 } else if (!r.rpool.proxy_port[1])
4131 r.rpool.proxy_port[1] =
4132 r.rpool.proxy_port[0];
4138 r.rpool.opts = $10.type;
4139 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
4140 PF_POOL_NONE && ($9->host->next != NULL ||
4141 $9->host->addr.type == PF_ADDR_TABLE ||
4142 DYNIF_MULTIADDR($9->host->addr)))
4143 r.rpool.opts = PF_POOL_ROUNDROBIN;
4144 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4145 PF_POOL_ROUNDROBIN &&
4146 disallow_table($9->host, "tables are only "
4147 "supported in round-robin redirection "
4150 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4151 PF_POOL_ROUNDROBIN &&
4152 disallow_alias($9->host, "interface (%s) "
4153 "is only supported in round-robin "
4154 "redirection pools"))
4156 if ($9->host->next != NULL) {
4157 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4158 PF_POOL_ROUNDROBIN) {
4159 yyerror("only round-robin "
4160 "valid for multiple "
4161 "redirection addresses");
4167 if ($10.key != NULL)
4168 memcpy(&r.rpool.key, $10.key,
4169 sizeof(struct pf_poolhashkey));
4172 r.rpool.opts |= $10.opts;
4174 if ($10.staticport) {
4175 if (r.action != PF_NAT) {
4176 yyerror("the 'static-port' option is "
4177 "only valid with nat rules");
4180 if (r.rpool.proxy_port[0] !=
4181 PF_NAT_PROXY_PORT_LOW &&
4182 r.rpool.proxy_port[1] !=
4183 PF_NAT_PROXY_PORT_HIGH) {
4184 yyerror("the 'static-port' option can't"
4185 " be used when specifying a port"
4189 r.rpool.proxy_port[0] = 0;
4190 r.rpool.proxy_port[1] = 0;
4193 expand_rule(&r, $2, $9 == NULL ? NULL : $9->host, $4,
4194 $5.src_os, $5.src.host, $5.src.port, $5.dst.host,
4195 $5.dst.port, 0, 0, 0, "");
4200 binatrule : no BINAT natpasslog interface af proto FROM ipspec toipspec tag
4201 tagged rtable redirection
4203 struct pf_rule binat;
4204 struct pf_pooladdr *pa;
4206 if (check_rulestate(PFCTL_STATE_NAT))
4208 if (disallow_urpf_failed($9, "\"urpf-failed\" is not "
4209 "permitted as a binat destination"))
4212 memset(&binat, 0, sizeof(binat));
4215 yyerror("\"pass\" not valid with \"no\"");
4219 binat.action = PF_NOBINAT;
4221 binat.action = PF_BINAT;
4222 binat.natpass = $3.b1;
4224 binat.logif = $3.w2;
4226 if (!binat.af && $8 != NULL && $8->af)
4228 if (!binat.af && $9 != NULL && $9->af)
4231 if (!binat.af && $13 != NULL && $13->host)
4232 binat.af = $13->host->af;
4234 yyerror("address family (inet/inet6) "
4240 memcpy(binat.ifname, $4->ifname,
4241 sizeof(binat.ifname));
4242 binat.ifnot = $4->not;
4247 if (strlcpy(binat.tagname, $10,
4248 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4249 yyerror("tag too long, max %u chars",
4250 PF_TAG_NAME_SIZE - 1);
4254 if (strlcpy(binat.match_tagname, $11.name,
4255 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4256 yyerror("tag too long, max %u chars",
4257 PF_TAG_NAME_SIZE - 1);
4260 binat.match_tag_not = $11.neg;
4261 binat.rtableid = $12;
4264 binat.proto = $6->proto;
4268 if ($8 != NULL && disallow_table($8, "invalid use of "
4269 "table <%s> as the source address of a binat rule"))
4271 if ($8 != NULL && disallow_alias($8, "invalid use of "
4272 "interface (%s) as the source address of a binat "
4275 if ($13 != NULL && $13->host != NULL && disallow_table(
4276 $13->host, "invalid use of table <%s> as the "
4277 "redirect address of a binat rule"))
4279 if ($13 != NULL && $13->host != NULL && disallow_alias(
4280 $13->host, "invalid use of interface (%s) as the "
4281 "redirect address of a binat rule"))
4286 yyerror("multiple binat ip addresses");
4289 if ($8->addr.type == PF_ADDR_DYNIFTL)
4291 if ($8->af != binat.af) {
4292 yyerror("binat ip versions must match");
4295 if (check_netmask($8, binat.af))
4297 memcpy(&binat.src.addr, &$8->addr,
4298 sizeof(binat.src.addr));
4303 yyerror("multiple binat ip addresses");
4306 if ($9->af != binat.af && $9->af) {
4307 yyerror("binat ip versions must match");
4310 if (check_netmask($9, binat.af))
4312 memcpy(&binat.dst.addr, &$9->addr,
4313 sizeof(binat.dst.addr));
4314 binat.dst.neg = $9->not;
4318 if (binat.action == PF_NOBINAT) {
4320 yyerror("'no binat' rule does not need"
4325 if ($13 == NULL || $13->host == NULL) {
4326 yyerror("'binat' rule requires"
4331 remove_invalid_hosts(&$13->host, &binat.af);
4332 if (invalid_redirect($13->host, binat.af))
4334 if ($13->host->next != NULL) {
4335 yyerror("binat rule must redirect to "
4336 "a single address");
4339 if (check_netmask($13->host, binat.af))
4342 if (!PF_AZERO(&binat.src.addr.v.a.mask,
4344 !PF_AEQ(&binat.src.addr.v.a.mask,
4345 &$13->host->addr.v.a.mask, binat.af)) {
4346 yyerror("'binat' source mask and "
4347 "redirect mask must be the same");
4351 TAILQ_INIT(&binat.rpool.list);
4352 pa = calloc(1, sizeof(struct pf_pooladdr));
4354 err(1, "binat: calloc");
4355 pa->addr = $13->host->addr;
4357 TAILQ_INSERT_TAIL(&binat.rpool.list,
4363 pfctl_add_rule(pf, &binat, "");
4367 tag : /* empty */ { $$ = NULL; }
4368 | TAG STRING { $$ = $2; }
4371 tagged : /* empty */ { $$.neg = 0; $$.name = NULL; }
4372 | not TAGGED string { $$.neg = $1; $$.name = $3; }
4375 rtable : /* empty */ { $$ = -1; }
4377 if ($2 < 0 || $2 > rt_tableid_max()) {
4378 yyerror("invalid rtable id");
4385 route_host : STRING {
4386 $$ = calloc(1, sizeof(struct node_host));
4388 err(1, "route_host: calloc");
4390 set_ipmask($$, 128);
4394 | '(' STRING host ')' {
4395 struct node_host *n;
4398 for (n = $3; n != NULL; n = n->next)
4403 route_host_list : route_host optnl { $$ = $1; }
4404 | route_host_list comma route_host optnl {
4407 if ($1->af != $3->af) {
4408 yyerror("all pool addresses must be in the "
4409 "same address family");
4412 $1->tail->next = $3;
4413 $1->tail = $3->tail;
4418 routespec : route_host { $$ = $1; }
4419 | '{' optnl route_host_list '}' { $$ = $3; }
4422 route : /* empty */ {
4428 /* backwards-compat */
4433 | ROUTETO routespec pool_opts {
4436 $$.pool_opts = $3.type | $3.opts;
4440 | REPLYTO routespec pool_opts {
4443 $$.pool_opts = $3.type | $3.opts;
4447 | DUPTO routespec pool_opts {
4450 $$.pool_opts = $3.type | $3.opts;
4456 timeout_spec : STRING NUMBER
4458 if (check_rulestate(PFCTL_STATE_OPTION)) {
4462 if ($2 < 0 || $2 > UINT_MAX) {
4463 yyerror("only positive values permitted");
4466 if (pfctl_set_timeout(pf, $1, $2, 0) != 0) {
4467 yyerror("unknown timeout %s", $1);
4474 if (check_rulestate(PFCTL_STATE_OPTION))
4476 if ($2 < 0 || $2 > UINT_MAX) {
4477 yyerror("only positive values permitted");
4480 if (pfctl_set_timeout(pf, "interval", $2, 0) != 0)
4485 timeout_list : timeout_list comma timeout_spec optnl
4486 | timeout_spec optnl
4489 limit_spec : STRING NUMBER
4491 if (check_rulestate(PFCTL_STATE_OPTION)) {
4495 if ($2 < 0 || $2 > UINT_MAX) {
4496 yyerror("only positive values permitted");
4499 if (pfctl_set_limit(pf, $1, $2) != 0) {
4500 yyerror("unable to set limit %s %u", $1, $2);
4508 limit_list : limit_list comma limit_spec optnl
4516 yesno : NO { $$ = 0; }
4518 if (!strcmp($1, "yes"))
4521 yyerror("invalid value '%s', expected 'yes' "
4530 unaryop : '=' { $$ = PF_OP_EQ; }
4531 | '!' '=' { $$ = PF_OP_NE; }
4532 | '<' '=' { $$ = PF_OP_LE; }
4533 | '<' { $$ = PF_OP_LT; }
4534 | '>' '=' { $$ = PF_OP_GE; }
4535 | '>' { $$ = PF_OP_GT; }
4541 yyerror(const char *fmt, ...)
4547 fprintf(stderr, "%s:%d: ", file->name, yylval.lineno);
4548 vfprintf(stderr, fmt, ap);
4549 fprintf(stderr, "\n");
4555 disallow_table(struct node_host *h, const char *fmt)
4557 for (; h != NULL; h = h->next)
4558 if (h->addr.type == PF_ADDR_TABLE) {
4559 yyerror(fmt, h->addr.v.tblname);
4566 disallow_urpf_failed(struct node_host *h, const char *fmt)
4568 for (; h != NULL; h = h->next)
4569 if (h->addr.type == PF_ADDR_URPFFAILED) {
4577 disallow_alias(struct node_host *h, const char *fmt)
4579 for (; h != NULL; h = h->next)
4580 if (DYNIF_MULTIADDR(h->addr)) {
4581 yyerror(fmt, h->addr.v.tblname);
4588 rule_consistent(struct pf_rule *r, int anchor_call)
4592 switch (r->action) {
4597 problems = filter_consistent(r, anchor_call);
4601 problems = nat_consistent(r);
4605 problems = rdr_consistent(r);
4616 filter_consistent(struct pf_rule *r, int anchor_call)
4620 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP &&
4621 (r->src.port_op || r->dst.port_op)) {
4622 yyerror("port only applies to tcp/udp");
4625 if (r->proto != IPPROTO_ICMP && r->proto != IPPROTO_ICMPV6 &&
4626 (r->type || r->code)) {
4627 yyerror("icmp-type/code only applies to icmp");
4630 if (!r->af && (r->type || r->code)) {
4631 yyerror("must indicate address family with icmp-type/code");
4634 if (r->overload_tblname[0] &&
4635 r->max_src_conn == 0 && r->max_src_conn_rate.seconds == 0) {
4636 yyerror("'overload' requires 'max-src-conn' "
4637 "or 'max-src-conn-rate'");
4640 if ((r->proto == IPPROTO_ICMP && r->af == AF_INET6) ||
4641 (r->proto == IPPROTO_ICMPV6 && r->af == AF_INET)) {
4642 yyerror("proto %s doesn't match address family %s",
4643 r->proto == IPPROTO_ICMP ? "icmp" : "icmp6",
4644 r->af == AF_INET ? "inet" : "inet6");
4647 if (r->allow_opts && r->action != PF_PASS) {
4648 yyerror("allow-opts can only be specified for pass rules");
4651 if (r->rule_flag & PFRULE_FRAGMENT && (r->src.port_op ||
4652 r->dst.port_op || r->flagset || r->type || r->code)) {
4653 yyerror("fragments can be filtered only on IP header fields");
4656 if (r->rule_flag & PFRULE_RETURNRST && r->proto != IPPROTO_TCP) {
4657 yyerror("return-rst can only be applied to TCP rules");
4660 if (r->max_src_nodes && !(r->rule_flag & PFRULE_RULESRCTRACK)) {
4661 yyerror("max-src-nodes requires 'source-track rule'");
4664 if (r->action == PF_DROP && r->keep_state) {
4665 yyerror("keep state on block rules doesn't make sense");
4668 if (r->rule_flag & PFRULE_STATESLOPPY &&
4669 (r->keep_state == PF_STATE_MODULATE ||
4670 r->keep_state == PF_STATE_SYNPROXY)) {
4671 yyerror("sloppy state matching cannot be used with "
4672 "synproxy state or modulate state");
4679 nat_consistent(struct pf_rule *r)
4681 return (0); /* yeah! */
4685 rdr_consistent(struct pf_rule *r)
4689 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP) {
4690 if (r->src.port_op) {
4691 yyerror("src port only applies to tcp/udp");
4694 if (r->dst.port_op) {
4695 yyerror("dst port only applies to tcp/udp");
4698 if (r->rpool.proxy_port[0]) {
4699 yyerror("rpool port only applies to tcp/udp");
4703 if (r->dst.port_op &&
4704 r->dst.port_op != PF_OP_EQ && r->dst.port_op != PF_OP_RRG) {
4705 yyerror("invalid port operator for rdr destination port");
4712 process_tabledef(char *name, struct table_opts *opts)
4714 struct pfr_buffer ab;
4715 struct node_tinit *ti;
4717 bzero(&ab, sizeof(ab));
4718 ab.pfrb_type = PFRB_ADDRS;
4719 SIMPLEQ_FOREACH(ti, &opts->init_nodes, entries) {
4721 if (pfr_buf_load(&ab, ti->file, 0, append_addr)) {
4723 yyerror("cannot load \"%s\": %s",
4724 ti->file, strerror(errno));
4726 yyerror("file \"%s\" contains bad data",
4731 if (append_addr_host(&ab, ti->host, 0, 0)) {
4732 yyerror("cannot create address buffer: %s",
4737 if (pf->opts & PF_OPT_VERBOSE)
4738 print_tabledef(name, opts->flags, opts->init_addr,
4740 if (!(pf->opts & PF_OPT_NOACTION) &&
4741 pfctl_define_table(name, opts->flags, opts->init_addr,
4742 pf->anchor->name, &ab, pf->anchor->ruleset.tticket)) {
4743 yyerror("cannot define table %s: %s", name,
4744 pfr_strerror(errno));
4760 /* macro gore, but you should've seen the prior indentation nightmare... */
4762 #define FREE_LIST(T,r) \
4765 while (node != NULL) { \
4767 node = node->next; \
4772 #define LOOP_THROUGH(T,n,r,C) \
4776 r = calloc(1, sizeof(T)); \
4778 err(1, "LOOP: calloc"); \
4782 while (n != NULL) { \
4791 expand_label_str(char *label, size_t len, const char *srch, const char *repl)
4796 if ((tmp = calloc(1, len)) == NULL)
4797 err(1, "expand_label_str: calloc");
4799 while ((q = strstr(p, srch)) != NULL) {
4801 if ((strlcat(tmp, p, len) >= len) ||
4802 (strlcat(tmp, repl, len) >= len))
4803 errx(1, "expand_label: label too long");
4807 if (strlcat(tmp, p, len) >= len)
4808 errx(1, "expand_label: label too long");
4809 strlcpy(label, tmp, len); /* always fits */
4814 expand_label_if(const char *name, char *label, size_t len, const char *ifname)
4816 if (strstr(label, name) != NULL) {
4818 expand_label_str(label, len, name, "any");
4820 expand_label_str(label, len, name, ifname);
4825 expand_label_addr(const char *name, char *label, size_t len, sa_family_t af,
4826 struct node_host *h)
4828 char tmp[64], tmp_not[66];
4830 if (strstr(label, name) != NULL) {
4831 switch (h->addr.type) {
4832 case PF_ADDR_DYNIFTL:
4833 snprintf(tmp, sizeof(tmp), "(%s)", h->addr.v.ifname);
4836 snprintf(tmp, sizeof(tmp), "<%s>", h->addr.v.tblname);
4838 case PF_ADDR_NOROUTE:
4839 snprintf(tmp, sizeof(tmp), "no-route");
4841 case PF_ADDR_URPFFAILED:
4842 snprintf(tmp, sizeof(tmp), "urpf-failed");
4844 case PF_ADDR_ADDRMASK:
4845 if (!af || (PF_AZERO(&h->addr.v.a.addr, af) &&
4846 PF_AZERO(&h->addr.v.a.mask, af)))
4847 snprintf(tmp, sizeof(tmp), "any");
4852 if (inet_ntop(af, &h->addr.v.a.addr, a,
4854 snprintf(tmp, sizeof(tmp), "?");
4856 bits = unmask(&h->addr.v.a.mask, af);
4857 if ((af == AF_INET && bits < 32) ||
4858 (af == AF_INET6 && bits < 128))
4859 snprintf(tmp, sizeof(tmp),
4862 snprintf(tmp, sizeof(tmp),
4868 snprintf(tmp, sizeof(tmp), "?");
4873 snprintf(tmp_not, sizeof(tmp_not), "! %s", tmp);
4874 expand_label_str(label, len, name, tmp_not);
4876 expand_label_str(label, len, name, tmp);
4881 expand_label_port(const char *name, char *label, size_t len,
4882 struct node_port *port)
4884 char a1[6], a2[6], op[13] = "";
4886 if (strstr(label, name) != NULL) {
4887 snprintf(a1, sizeof(a1), "%u", ntohs(port->port[0]));
4888 snprintf(a2, sizeof(a2), "%u", ntohs(port->port[1]));
4891 else if (port->op == PF_OP_IRG)
4892 snprintf(op, sizeof(op), "%s><%s", a1, a2);
4893 else if (port->op == PF_OP_XRG)
4894 snprintf(op, sizeof(op), "%s<>%s", a1, a2);
4895 else if (port->op == PF_OP_EQ)
4896 snprintf(op, sizeof(op), "%s", a1);
4897 else if (port->op == PF_OP_NE)
4898 snprintf(op, sizeof(op), "!=%s", a1);
4899 else if (port->op == PF_OP_LT)
4900 snprintf(op, sizeof(op), "<%s", a1);
4901 else if (port->op == PF_OP_LE)
4902 snprintf(op, sizeof(op), "<=%s", a1);
4903 else if (port->op == PF_OP_GT)
4904 snprintf(op, sizeof(op), ">%s", a1);
4905 else if (port->op == PF_OP_GE)
4906 snprintf(op, sizeof(op), ">=%s", a1);
4907 expand_label_str(label, len, name, op);
4912 expand_label_proto(const char *name, char *label, size_t len, u_int8_t proto)
4914 struct protoent *pe;
4917 if (strstr(label, name) != NULL) {
4918 pe = getprotobynumber(proto);
4920 expand_label_str(label, len, name, pe->p_name);
4922 snprintf(n, sizeof(n), "%u", proto);
4923 expand_label_str(label, len, name, n);
4929 expand_label_nr(const char *name, char *label, size_t len)
4933 if (strstr(label, name) != NULL) {
4934 snprintf(n, sizeof(n), "%u", pf->anchor->match);
4935 expand_label_str(label, len, name, n);
4940 expand_label(char *label, size_t len, const char *ifname, sa_family_t af,
4941 struct node_host *src_host, struct node_port *src_port,
4942 struct node_host *dst_host, struct node_port *dst_port,
4945 expand_label_if("$if", label, len, ifname);
4946 expand_label_addr("$srcaddr", label, len, af, src_host);
4947 expand_label_addr("$dstaddr", label, len, af, dst_host);
4948 expand_label_port("$srcport", label, len, src_port);
4949 expand_label_port("$dstport", label, len, dst_port);
4950 expand_label_proto("$proto", label, len, proto);
4951 expand_label_nr("$nr", label, len);
4955 expand_altq(struct pf_altq *a, struct node_if *interfaces,
4956 struct node_queue *nqueues, struct node_queue_bw bwspec,
4957 struct node_queue_opt *opts)
4959 struct pf_altq pa, pb;
4960 char qname[PF_QNAME_SIZE];
4961 struct node_queue *n;
4962 struct node_queue_bw bw;
4965 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
4966 FREE_LIST(struct node_if, interfaces);
4968 FREE_LIST(struct node_queue, nqueues);
4972 LOOP_THROUGH(struct node_if, interface, interfaces,
4973 memcpy(&pa, a, sizeof(struct pf_altq));
4974 if (strlcpy(pa.ifname, interface->ifname,
4975 sizeof(pa.ifname)) >= sizeof(pa.ifname))
4976 errx(1, "expand_altq: strlcpy");
4978 if (interface->not) {
4979 yyerror("altq on ! <interface> is not supported");
4982 if (eval_pfaltq(pf, &pa, &bwspec, opts))
4985 if (pfctl_add_altq(pf, &pa))
4988 if (pf->opts & PF_OPT_VERBOSE) {
4989 print_altq(&pf->paltq->altq, 0,
4991 if (nqueues && nqueues->tail) {
4993 LOOP_THROUGH(struct node_queue, queue,
5003 if (pa.scheduler == ALTQT_CBQ ||
5004 pa.scheduler == ALTQT_HFSC) {
5005 /* now create a root queue */
5006 memset(&pb, 0, sizeof(struct pf_altq));
5007 if (strlcpy(qname, "root_", sizeof(qname)) >=
5009 errx(1, "expand_altq: strlcpy");
5010 if (strlcat(qname, interface->ifname,
5011 sizeof(qname)) >= sizeof(qname))
5012 errx(1, "expand_altq: strlcat");
5013 if (strlcpy(pb.qname, qname,
5014 sizeof(pb.qname)) >= sizeof(pb.qname))
5015 errx(1, "expand_altq: strlcpy");
5016 if (strlcpy(pb.ifname, interface->ifname,
5017 sizeof(pb.ifname)) >= sizeof(pb.ifname))
5018 errx(1, "expand_altq: strlcpy");
5019 pb.qlimit = pa.qlimit;
5020 pb.scheduler = pa.scheduler;
5021 bw.bw_absolute = pa.ifbandwidth;
5023 if (eval_pfqueue(pf, &pb, &bw, opts))
5026 if (pfctl_add_altq(pf, &pb))
5030 LOOP_THROUGH(struct node_queue, queue, nqueues,
5031 n = calloc(1, sizeof(struct node_queue));
5033 err(1, "expand_altq: calloc");
5034 if (pa.scheduler == ALTQT_CBQ ||
5035 pa.scheduler == ALTQT_HFSC)
5036 if (strlcpy(n->parent, qname,
5037 sizeof(n->parent)) >=
5039 errx(1, "expand_altq: strlcpy");
5040 if (strlcpy(n->queue, queue->queue,
5041 sizeof(n->queue)) >= sizeof(n->queue))
5042 errx(1, "expand_altq: strlcpy");
5043 if (strlcpy(n->ifname, interface->ifname,
5044 sizeof(n->ifname)) >= sizeof(n->ifname))
5045 errx(1, "expand_altq: strlcpy");
5046 n->scheduler = pa.scheduler;
5052 queues->tail->next = n;
5058 FREE_LIST(struct node_if, interfaces);
5060 FREE_LIST(struct node_queue, nqueues);
5066 expand_queue(struct pf_altq *a, struct node_if *interfaces,
5067 struct node_queue *nqueues, struct node_queue_bw bwspec,
5068 struct node_queue_opt *opts)
5070 struct node_queue *n, *nq;
5075 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
5076 FREE_LIST(struct node_queue, nqueues);
5080 if (queues == NULL) {
5081 yyerror("queue %s has no parent", a->qname);
5082 FREE_LIST(struct node_queue, nqueues);
5086 LOOP_THROUGH(struct node_if, interface, interfaces,
5087 LOOP_THROUGH(struct node_queue, tqueue, queues,
5088 if (!strncmp(a->qname, tqueue->queue, PF_QNAME_SIZE) &&
5089 (interface->ifname[0] == 0 ||
5090 (!interface->not && !strncmp(interface->ifname,
5091 tqueue->ifname, IFNAMSIZ)) ||
5092 (interface->not && strncmp(interface->ifname,
5093 tqueue->ifname, IFNAMSIZ)))) {
5094 /* found ourself in queues */
5097 memcpy(&pa, a, sizeof(struct pf_altq));
5099 if (pa.scheduler != ALTQT_NONE &&
5100 pa.scheduler != tqueue->scheduler) {
5101 yyerror("exactly one scheduler type "
5102 "per interface allowed");
5105 pa.scheduler = tqueue->scheduler;
5107 /* scheduler dependent error checking */
5108 switch (pa.scheduler) {
5110 if (nqueues != NULL) {
5111 yyerror("priq queues cannot "
5112 "have child queues");
5115 if (bwspec.bw_absolute > 0 ||
5116 bwspec.bw_percent < 100) {
5117 yyerror("priq doesn't take "
5126 if (strlcpy(pa.ifname, tqueue->ifname,
5127 sizeof(pa.ifname)) >= sizeof(pa.ifname))
5128 errx(1, "expand_queue: strlcpy");
5129 if (strlcpy(pa.parent, tqueue->parent,
5130 sizeof(pa.parent)) >= sizeof(pa.parent))
5131 errx(1, "expand_queue: strlcpy");
5133 if (eval_pfqueue(pf, &pa, &bwspec, opts))
5136 if (pfctl_add_altq(pf, &pa))
5139 for (nq = nqueues; nq != NULL; nq = nq->next) {
5140 if (!strcmp(a->qname, nq->queue)) {
5141 yyerror("queue cannot have "
5147 sizeof(struct node_queue));
5149 err(1, "expand_queue: calloc");
5150 if (strlcpy(n->parent, a->qname,
5151 sizeof(n->parent)) >=
5153 errx(1, "expand_queue strlcpy");
5154 if (strlcpy(n->queue, nq->queue,
5155 sizeof(n->queue)) >=
5157 errx(1, "expand_queue strlcpy");
5158 if (strlcpy(n->ifname, tqueue->ifname,
5159 sizeof(n->ifname)) >=
5161 errx(1, "expand_queue strlcpy");
5162 n->scheduler = tqueue->scheduler;
5168 queues->tail->next = n;
5172 if ((pf->opts & PF_OPT_VERBOSE) && (
5173 (found == 1 && interface->ifname[0] == 0) ||
5174 (found > 0 && interface->ifname[0] != 0))) {
5175 print_queue(&pf->paltq->altq, 0,
5176 &bwspec, interface->ifname[0] != 0,
5178 if (nqueues && nqueues->tail) {
5180 LOOP_THROUGH(struct node_queue,
5193 FREE_LIST(struct node_queue, nqueues);
5194 FREE_LIST(struct node_if, interfaces);
5197 yyerror("queue %s has no parent", a->qname);
5208 expand_rule(struct pf_rule *r,
5209 struct node_if *interfaces, struct node_host *rpool_hosts,
5210 struct node_proto *protos, struct node_os *src_oses,
5211 struct node_host *src_hosts, struct node_port *src_ports,
5212 struct node_host *dst_hosts, struct node_port *dst_ports,
5213 struct node_uid *uids, struct node_gid *gids, struct node_icmp *icmp_types,
5214 const char *anchor_call)
5216 sa_family_t af = r->af;
5217 int added = 0, error = 0;
5218 char ifname[IF_NAMESIZE];
5219 char label[PF_RULE_LABEL_SIZE];
5220 char tagname[PF_TAG_NAME_SIZE];
5221 char match_tagname[PF_TAG_NAME_SIZE];
5222 struct pf_pooladdr *pa;
5223 struct node_host *h;
5224 u_int8_t flags, flagset, keep_state;
5226 if (strlcpy(label, r->label, sizeof(label)) >= sizeof(label))
5227 errx(1, "expand_rule: strlcpy");
5228 if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname))
5229 errx(1, "expand_rule: strlcpy");
5230 if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >=
5231 sizeof(match_tagname))
5232 errx(1, "expand_rule: strlcpy");
5234 flagset = r->flagset;
5235 keep_state = r->keep_state;
5237 LOOP_THROUGH(struct node_if, interface, interfaces,
5238 LOOP_THROUGH(struct node_proto, proto, protos,
5239 LOOP_THROUGH(struct node_icmp, icmp_type, icmp_types,
5240 LOOP_THROUGH(struct node_host, src_host, src_hosts,
5241 LOOP_THROUGH(struct node_port, src_port, src_ports,
5242 LOOP_THROUGH(struct node_os, src_os, src_oses,
5243 LOOP_THROUGH(struct node_host, dst_host, dst_hosts,
5244 LOOP_THROUGH(struct node_port, dst_port, dst_ports,
5245 LOOP_THROUGH(struct node_uid, uid, uids,
5246 LOOP_THROUGH(struct node_gid, gid, gids,
5249 /* for link-local IPv6 address, interface must match up */
5250 if ((r->af && src_host->af && r->af != src_host->af) ||
5251 (r->af && dst_host->af && r->af != dst_host->af) ||
5252 (src_host->af && dst_host->af &&
5253 src_host->af != dst_host->af) ||
5254 (src_host->ifindex && dst_host->ifindex &&
5255 src_host->ifindex != dst_host->ifindex) ||
5256 (src_host->ifindex && *interface->ifname &&
5257 src_host->ifindex != if_nametoindex(interface->ifname)) ||
5258 (dst_host->ifindex && *interface->ifname &&
5259 dst_host->ifindex != if_nametoindex(interface->ifname)))
5261 if (!r->af && src_host->af)
5262 r->af = src_host->af;
5263 else if (!r->af && dst_host->af)
5264 r->af = dst_host->af;
5266 if (*interface->ifname)
5267 strlcpy(r->ifname, interface->ifname,
5269 else if (if_indextoname(src_host->ifindex, ifname))
5270 strlcpy(r->ifname, ifname, sizeof(r->ifname));
5271 else if (if_indextoname(dst_host->ifindex, ifname))
5272 strlcpy(r->ifname, ifname, sizeof(r->ifname));
5274 memset(r->ifname, '\0', sizeof(r->ifname));
5276 if (strlcpy(r->label, label, sizeof(r->label)) >=
5278 errx(1, "expand_rule: strlcpy");
5279 if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >=
5281 errx(1, "expand_rule: strlcpy");
5282 if (strlcpy(r->match_tagname, match_tagname,
5283 sizeof(r->match_tagname)) >= sizeof(r->match_tagname))
5284 errx(1, "expand_rule: strlcpy");
5285 expand_label(r->label, PF_RULE_LABEL_SIZE, r->ifname, r->af,
5286 src_host, src_port, dst_host, dst_port, proto->proto);
5287 expand_label(r->tagname, PF_TAG_NAME_SIZE, r->ifname, r->af,
5288 src_host, src_port, dst_host, dst_port, proto->proto);
5289 expand_label(r->match_tagname, PF_TAG_NAME_SIZE, r->ifname,
5290 r->af, src_host, src_port, dst_host, dst_port,
5293 error += check_netmask(src_host, r->af);
5294 error += check_netmask(dst_host, r->af);
5296 r->ifnot = interface->not;
5297 r->proto = proto->proto;
5298 r->src.addr = src_host->addr;
5299 r->src.neg = src_host->not;
5300 r->src.port[0] = src_port->port[0];
5301 r->src.port[1] = src_port->port[1];
5302 r->src.port_op = src_port->op;
5303 r->dst.addr = dst_host->addr;
5304 r->dst.neg = dst_host->not;
5305 r->dst.port[0] = dst_port->port[0];
5306 r->dst.port[1] = dst_port->port[1];
5307 r->dst.port_op = dst_port->op;
5308 r->uid.op = uid->op;
5309 r->uid.uid[0] = uid->uid[0];
5310 r->uid.uid[1] = uid->uid[1];
5311 r->gid.op = gid->op;
5312 r->gid.gid[0] = gid->gid[0];
5313 r->gid.gid[1] = gid->gid[1];
5314 r->type = icmp_type->type;
5315 r->code = icmp_type->code;
5317 if ((keep_state == PF_STATE_MODULATE ||
5318 keep_state == PF_STATE_SYNPROXY) &&
5319 r->proto && r->proto != IPPROTO_TCP)
5320 r->keep_state = PF_STATE_NORMAL;
5322 r->keep_state = keep_state;
5324 if (r->proto && r->proto != IPPROTO_TCP) {
5329 r->flagset = flagset;
5331 if (icmp_type->proto && r->proto != icmp_type->proto) {
5332 yyerror("icmp-type mismatch");
5336 if (src_os && src_os->os) {
5337 r->os_fingerprint = pfctl_get_fingerprint(src_os->os);
5338 if ((pf->opts & PF_OPT_VERBOSE2) &&
5339 r->os_fingerprint == PF_OSFP_NOMATCH)
5341 "warning: unknown '%s' OS fingerprint\n",
5344 r->os_fingerprint = PF_OSFP_ANY;
5347 TAILQ_INIT(&r->rpool.list);
5348 for (h = rpool_hosts; h != NULL; h = h->next) {
5349 pa = calloc(1, sizeof(struct pf_pooladdr));
5351 err(1, "expand_rule: calloc");
5353 if (h->ifname != NULL) {
5354 if (strlcpy(pa->ifname, h->ifname,
5355 sizeof(pa->ifname)) >=
5357 errx(1, "expand_rule: strlcpy");
5360 TAILQ_INSERT_TAIL(&r->rpool.list, pa, entries);
5363 if (rule_consistent(r, anchor_call[0]) < 0 || error)
5364 yyerror("skipping rule due to errors");
5366 r->nr = pf->astack[pf->asd]->match++;
5367 pfctl_add_rule(pf, r, anchor_call);
5373 FREE_LIST(struct node_if, interfaces);
5374 FREE_LIST(struct node_proto, protos);
5375 FREE_LIST(struct node_host, src_hosts);
5376 FREE_LIST(struct node_port, src_ports);
5377 FREE_LIST(struct node_os, src_oses);
5378 FREE_LIST(struct node_host, dst_hosts);
5379 FREE_LIST(struct node_port, dst_ports);
5380 FREE_LIST(struct node_uid, uids);
5381 FREE_LIST(struct node_gid, gids);
5382 FREE_LIST(struct node_icmp, icmp_types);
5383 FREE_LIST(struct node_host, rpool_hosts);
5386 yyerror("rule expands to no valid combination");
5390 expand_skip_interface(struct node_if *interfaces)
5394 if (!interfaces || (!interfaces->next && !interfaces->not &&
5395 !strcmp(interfaces->ifname, "none"))) {
5396 if (pf->opts & PF_OPT_VERBOSE)
5397 printf("set skip on none\n");
5398 errs = pfctl_set_interface_flags(pf, "", PFI_IFLAG_SKIP, 0);
5402 if (pf->opts & PF_OPT_VERBOSE)
5403 printf("set skip on {");
5404 LOOP_THROUGH(struct node_if, interface, interfaces,
5405 if (pf->opts & PF_OPT_VERBOSE)
5406 printf(" %s", interface->ifname);
5407 if (interface->not) {
5408 yyerror("skip on ! <interface> is not supported");
5411 errs += pfctl_set_interface_flags(pf,
5412 interface->ifname, PFI_IFLAG_SKIP, 1);
5414 if (pf->opts & PF_OPT_VERBOSE)
5417 FREE_LIST(struct node_if, interfaces);
5429 check_rulestate(int desired_state)
5431 if (require_order && (rulestate > desired_state)) {
5432 yyerror("Rules must be in order: options, normalization, "
5433 "queueing, translation, filtering");
5436 rulestate = desired_state;
5441 kw_cmp(const void *k, const void *e)
5443 return (strcmp(k, ((const struct keywords *)e)->k_name));
5449 /* this has to be sorted always */
5450 static const struct keywords keywords[] = {
5452 { "allow-opts", ALLOWOPTS},
5454 { "anchor", ANCHOR},
5455 { "antispoof", ANTISPOOF},
5457 { "bandwidth", BANDWIDTH},
5459 { "binat-anchor", BINATANCHOR},
5460 { "bitmask", BITMASK},
5462 { "block-policy", BLOCKPOLICY},
5463 { "buckets", BUCKETS},
5467 { "crop", FRAGCROP},
5469 { "divert-reply", DIVERTREPLY},
5470 { "divert-to", DIVERTTO},
5472 { "drop-ovl", FRAGDROP},
5475 { "fastroute", FASTROUTE},
5476 { "file", FILENAME},
5477 { "fingerprints", FINGERPRINTS},
5479 { "floating", FLOATING},
5482 { "fragment", FRAGMENT},
5484 { "global", GLOBAL},
5488 { "hostid", HOSTID},
5489 { "icmp-type", ICMPTYPE},
5490 { "icmp6-type", ICMP6TYPE},
5491 { "if-bound", IFBOUND},
5493 { "include", INCLUDE},
5496 { "interval", INTERVAL},
5500 { "linkshare", LINKSHARE},
5503 { "loginterface", LOGINTERFACE},
5505 { "max-mss", MAXMSS},
5506 { "max-src-conn", MAXSRCCONN},
5507 { "max-src-conn-rate", MAXSRCCONNRATE},
5508 { "max-src-nodes", MAXSRCNODES},
5509 { "max-src-states", MAXSRCSTATES},
5510 { "min-ttl", MINTTL},
5511 { "modulate", MODULATE},
5513 { "nat-anchor", NATANCHOR},
5516 { "no-route", NOROUTE},
5517 { "no-sync", NOSYNC},
5519 { "optimization", OPTIMIZATION},
5522 { "overload", OVERLOAD},
5526 { "priority", PRIORITY},
5528 { "probability", PROBABILITY},
5530 { "qlimit", QLIMIT},
5533 { "random", RANDOM},
5534 { "random-id", RANDOMID},
5536 { "rdr-anchor", RDRANCHOR},
5537 { "realtime", REALTIME},
5538 { "reassemble", REASSEMBLE},
5539 { "reply-to", REPLYTO},
5540 { "require-order", REQUIREORDER},
5541 { "return", RETURN},
5542 { "return-icmp", RETURNICMP},
5543 { "return-icmp6", RETURNICMP6},
5544 { "return-rst", RETURNRST},
5545 { "round-robin", ROUNDROBIN},
5547 { "route-to", ROUTETO},
5548 { "rtable", RTABLE},
5550 { "ruleset-optimization", RULESET_OPTIMIZATION},
5553 { "set-tos", SETTOS},
5555 { "sloppy", SLOPPY},
5556 { "source-hash", SOURCEHASH},
5557 { "source-track", SOURCETRACK},
5559 { "state-defaults", STATEDEFAULTS},
5560 { "state-policy", STATEPOLICY},
5561 { "static-port", STATICPORT},
5562 { "sticky-address", STICKYADDRESS},
5563 { "synproxy", SYNPROXY},
5566 { "tagged", TAGGED},
5567 { "target", TARGET},
5568 { "tbrsize", TBRSIZE},
5569 { "timeout", TIMEOUT},
5573 { "upperlimit", UPPERLIMIT},
5574 { "urpf-failed", URPFFAILED},
5577 const struct keywords *p;
5579 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
5580 sizeof(keywords[0]), kw_cmp);
5584 fprintf(stderr, "%s: %d\n", s, p->k_val);
5588 fprintf(stderr, "string: %s\n", s);
5593 #define MAXPUSHBACK 128
5595 static char *parsebuf;
5596 static int parseindex;
5597 static char pushback_buffer[MAXPUSHBACK];
5598 static int pushback_index = 0;
5606 /* Read character from the parsebuffer instead of input. */
5607 if (parseindex >= 0) {
5608 c = parsebuf[parseindex++];
5617 return (pushback_buffer[--pushback_index]);
5620 if ((c = getc(file->stream)) == EOF) {
5621 yyerror("reached end of file while parsing quoted string");
5622 if (popfile() == EOF)
5629 while ((c = getc(file->stream)) == '\\') {
5630 next = getc(file->stream);
5635 yylval.lineno = file->lineno;
5640 if (popfile() == EOF)
5642 c = getc(file->stream);
5654 if (parseindex >= 0)
5657 if (pushback_index < MAXPUSHBACK-1)
5658 return (pushback_buffer[pushback_index++] = c);
5670 /* skip to either EOF or the first real EOL */
5673 c = pushback_buffer[--pushback_index];
5691 int quotec, next, c;
5696 while ((c = lgetc(0)) == ' ' || c == '\t')
5699 yylval.lineno = file->lineno;
5701 while ((c = lgetc(0)) != '\n' && c != EOF)
5703 if (c == '$' && parsebuf == NULL) {
5705 if ((c = lgetc(0)) == EOF)
5708 if (p + 1 >= buf + sizeof(buf) - 1) {
5709 yyerror("string too long");
5712 if (isalnum(c) || c == '_') {
5722 yyerror("macro '%s' not defined", buf);
5735 if ((c = lgetc(quotec)) == EOF)
5740 } else if (c == '\\') {
5741 if ((next = lgetc(quotec)) == EOF)
5743 if (next == quotec || c == ' ' || c == '\t')
5745 else if (next == '\n')
5749 } else if (c == quotec) {
5753 if (p + 1 >= buf + sizeof(buf) - 1) {
5754 yyerror("string too long");
5759 yylval.v.string = strdup(buf);
5760 if (yylval.v.string == NULL)
5761 err(1, "yylex: strdup");
5766 yylval.v.i = PF_OP_XRG;
5767 return (PORTBINARY);
5774 yylval.v.i = PF_OP_IRG;
5775 return (PORTBINARY);
5787 #define allowed_to_end_number(x) \
5788 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
5790 if (c == '-' || isdigit(c)) {
5793 if ((unsigned)(p-buf) >= sizeof(buf)) {
5794 yyerror("string too long");
5797 } while ((c = lgetc(0)) != EOF && isdigit(c));
5799 if (p == buf + 1 && buf[0] == '-')
5801 if (c == EOF || allowed_to_end_number(c)) {
5802 const char *errstr = NULL;
5805 yylval.v.number = strtonum(buf, LLONG_MIN,
5806 LLONG_MAX, &errstr);
5808 yyerror("\"%s\" invalid number: %s",
5823 #define allowed_in_string(x) \
5824 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
5825 x != '{' && x != '}' && x != '<' && x != '>' && \
5826 x != '!' && x != '=' && x != '/' && x != '#' && \
5829 if (isalnum(c) || c == ':' || c == '_') {
5832 if ((unsigned)(p-buf) >= sizeof(buf)) {
5833 yyerror("string too long");
5836 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
5839 if ((token = lookup(buf)) == STRING)
5840 if ((yylval.v.string = strdup(buf)) == NULL)
5841 err(1, "yylex: strdup");
5845 yylval.lineno = file->lineno;
5854 check_file_secrecy(int fd, const char *fname)
5858 if (fstat(fd, &st)) {
5859 warn("cannot stat %s", fname);
5862 if (st.st_uid != 0 && st.st_uid != getuid()) {
5863 warnx("%s: owner not root or current user", fname);
5866 if (st.st_mode & (S_IRWXG | S_IRWXO)) {
5867 warnx("%s: group/world readable/writeable", fname);
5874 pushfile(const char *name, int secret)
5878 if ((nfile = calloc(1, sizeof(struct file))) == NULL ||
5879 (nfile->name = strdup(name)) == NULL) {
5883 if (TAILQ_FIRST(&files) == NULL && strcmp(nfile->name, "-") == 0) {
5884 nfile->stream = stdin;
5886 if ((nfile->name = strdup("stdin")) == NULL) {
5891 } else if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
5892 warn("%s", nfile->name);
5896 } else if (secret &&
5897 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
5898 fclose(nfile->stream);
5904 TAILQ_INSERT_TAIL(&files, nfile, entry);
5913 if ((prev = TAILQ_PREV(file, files, entry)) != NULL) {
5914 prev->errors += file->errors;
5915 TAILQ_REMOVE(&files, file, entry);
5916 fclose(file->stream);
5926 parse_config(char *filename, struct pfctl *xpf)
5933 rulestate = PFCTL_STATE_NONE;
5934 returnicmpdefault = (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
5935 returnicmp6default =
5936 (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
5937 blockpolicy = PFRULE_DROP;
5940 if ((file = pushfile(filename, 0)) == NULL) {
5941 warn("cannot open the main config file!");
5946 errors = file->errors;
5949 /* Free macros and check which have not been used. */
5950 while ((sym = TAILQ_FIRST(&symhead))) {
5951 if ((pf->opts & PF_OPT_VERBOSE2) && !sym->used)
5952 fprintf(stderr, "warning: macro '%s' not "
5953 "used\n", sym->nam);
5956 TAILQ_REMOVE(&symhead, sym, entry);
5960 return (errors ? -1 : 0);
5964 symset(const char *nam, const char *val, int persist)
5968 for (sym = TAILQ_FIRST(&symhead); sym && strcmp(nam, sym->nam);
5969 sym = TAILQ_NEXT(sym, entry))
5973 if (sym->persist == 1)
5978 TAILQ_REMOVE(&symhead, sym, entry);
5982 if ((sym = calloc(1, sizeof(*sym))) == NULL)
5985 sym->nam = strdup(nam);
5986 if (sym->nam == NULL) {
5990 sym->val = strdup(val);
5991 if (sym->val == NULL) {
5997 sym->persist = persist;
5998 TAILQ_INSERT_TAIL(&symhead, sym, entry);
6003 pfctl_cmdline_symset(char *s)
6008 if ((val = strrchr(s, '=')) == NULL)
6011 if ((sym = malloc(strlen(s) - strlen(val) + 1)) == NULL)
6012 err(1, "pfctl_cmdline_symset: malloc");
6014 strlcpy(sym, s, strlen(s) - strlen(val) + 1);
6016 ret = symset(sym, val + 1, 1);
6023 symget(const char *nam)
6027 TAILQ_FOREACH(sym, &symhead, entry)
6028 if (strcmp(nam, sym->nam) == 0) {
6036 mv_rules(struct pf_ruleset *src, struct pf_ruleset *dst)
6041 for (i = 0; i < PF_RULESET_MAX; ++i) {
6042 while ((r = TAILQ_FIRST(src->rules[i].active.ptr))
6044 TAILQ_REMOVE(src->rules[i].active.ptr, r, entries);
6045 TAILQ_INSERT_TAIL(dst->rules[i].active.ptr, r, entries);
6046 dst->anchor->match++;
6048 src->anchor->match = 0;
6049 while ((r = TAILQ_FIRST(src->rules[i].inactive.ptr))
6051 TAILQ_REMOVE(src->rules[i].inactive.ptr, r, entries);
6052 TAILQ_INSERT_TAIL(dst->rules[i].inactive.ptr,
6059 decide_address_family(struct node_host *n, sa_family_t *af)
6061 if (*af != 0 || n == NULL)
6064 while ((n = n->next) != NULL) {
6073 remove_invalid_hosts(struct node_host **nh, sa_family_t *af)
6075 struct node_host *n = *nh, *prev = NULL;
6078 if (*af && n->af && n->af != *af) {
6079 /* unlink and free n */
6080 struct node_host *next = n->next;
6082 /* adjust tail pointer */
6083 if (n == (*nh)->tail)
6085 /* adjust previous node's next pointer */
6091 if (n->ifname != NULL)
6105 invalid_redirect(struct node_host *nh, sa_family_t af)
6108 struct node_host *n;
6110 /* tables and dyniftl are ok without an address family */
6111 for (n = nh; n != NULL; n = n->next) {
6112 if (n->addr.type != PF_ADDR_TABLE &&
6113 n->addr.type != PF_ADDR_DYNIFTL) {
6114 yyerror("address family not given and "
6115 "translation address expands to multiple "
6116 "address families");
6122 yyerror("no translation address with matching address family "
6130 atoul(char *s, u_long *ulvalp)
6136 ulval = strtoul(s, &ep, 0);
6137 if (s[0] == '\0' || *ep != '\0')
6139 if (errno == ERANGE && ulval == ULONG_MAX)
6151 if (atoul(n, &ulval) == 0) {
6152 if (ulval > 65535) {
6153 yyerror("illegal port value %lu", ulval);
6156 return (htons(ulval));
6158 s = getservbyname(n, "tcp");
6160 s = getservbyname(n, "udp");
6162 yyerror("unknown port %s", n);
6170 rule_label(struct pf_rule *r, char *s)
6173 if (strlcpy(r->label, s, sizeof(r->label)) >=
6175 yyerror("rule label too long (max %d chars)",
6176 sizeof(r->label)-1);
6184 parseicmpspec(char *w, sa_family_t af)
6186 const struct icmpcodeent *p;
6191 icmptype = returnicmpdefault >> 8;
6193 icmptype = returnicmp6default >> 8;
6195 if (atoul(w, &ulval) == -1) {
6196 if ((p = geticmpcodebyname(icmptype, w, af)) == NULL) {
6197 yyerror("unknown icmp code %s", w);
6203 yyerror("invalid icmp code %lu", ulval);
6206 return (icmptype << 8 | ulval);
6210 parseport(char *port, struct range *r, int extensions)
6212 char *p = strchr(port, ':');
6215 if ((r->a = getservice(port)) == -1)
6221 if ((extensions & PPORT_STAR) && !strcmp(p+1, "*")) {
6223 if ((r->a = getservice(port)) == -1)
6229 if ((extensions & PPORT_RANGE)) {
6231 if ((r->a = getservice(port)) == -1 ||
6232 (r->b = getservice(p)) == -1)
6245 pfctl_load_anchors(int dev, struct pfctl *pf, struct pfr_buffer *trans)
6247 struct loadanchors *la;
6249 TAILQ_FOREACH(la, &loadanchorshead, entries) {
6250 if (pf->opts & PF_OPT_VERBOSE)
6251 fprintf(stderr, "\nLoading anchor %s from %s\n",
6252 la->anchorname, la->filename);
6253 if (pfctl_rules(dev, la->filename, pf->opts, pf->optimize,
6254 la->anchorname, trans) == -1)
6262 kw_casecmp(const void *k, const void *e)
6264 return (strcasecmp(k, ((const struct keywords *)e)->k_name));
6268 map_tos(char *s, int *val)
6270 /* DiffServ Codepoints and other TOS mappings */
6271 const struct keywords toswords[] = {
6272 { "af11", IPTOS_DSCP_AF11 },
6273 { "af12", IPTOS_DSCP_AF12 },
6274 { "af13", IPTOS_DSCP_AF13 },
6275 { "af21", IPTOS_DSCP_AF21 },
6276 { "af22", IPTOS_DSCP_AF22 },
6277 { "af23", IPTOS_DSCP_AF23 },
6278 { "af31", IPTOS_DSCP_AF31 },
6279 { "af32", IPTOS_DSCP_AF32 },
6280 { "af33", IPTOS_DSCP_AF33 },
6281 { "af41", IPTOS_DSCP_AF41 },
6282 { "af42", IPTOS_DSCP_AF42 },
6283 { "af43", IPTOS_DSCP_AF43 },
6284 { "critical", IPTOS_PREC_CRITIC_ECP },
6285 { "cs0", IPTOS_DSCP_CS0 },
6286 { "cs1", IPTOS_DSCP_CS1 },
6287 { "cs2", IPTOS_DSCP_CS2 },
6288 { "cs3", IPTOS_DSCP_CS3 },
6289 { "cs4", IPTOS_DSCP_CS4 },
6290 { "cs5", IPTOS_DSCP_CS5 },
6291 { "cs6", IPTOS_DSCP_CS6 },
6292 { "cs7", IPTOS_DSCP_CS7 },
6293 { "ef", IPTOS_DSCP_EF },
6294 { "inetcontrol", IPTOS_PREC_INTERNETCONTROL },
6295 { "lowdelay", IPTOS_LOWDELAY },
6296 { "netcontrol", IPTOS_PREC_NETCONTROL },
6297 { "reliability", IPTOS_RELIABILITY },
6298 { "throughput", IPTOS_THROUGHPUT }
6300 const struct keywords *p;
6302 p = bsearch(s, toswords, sizeof(toswords)/sizeof(toswords[0]),
6303 sizeof(toswords[0]), kw_casecmp);
6313 rt_tableid_max(void)
6317 size_t l = sizeof(fibs);
6319 if (sysctlbyname("net.fibs", &fibs, &l, NULL, 0) == -1)
6320 fibs = 16; /* XXX RT_MAXFIBS, at least limit it some. */
6322 * As the OpenBSD code only compares > and not >= we need to adjust
6323 * here given we only accept values of 0..n and want to avoid #ifdefs
6328 return (RT_TABLEID_MAX);