1 /* $OpenBSD: parse.y,v 1.554 2008/10/17 12:59:53 henning Exp $ */
4 * Copyright (c) 2001 Markus Friedl. All rights reserved.
5 * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
6 * Copyright (c) 2001 Theo de Raadt. All rights reserved.
7 * Copyright (c) 2002,2003 Henning Brauer. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/types.h>
34 #include <sys/socket.h>
37 #include <sys/sysctl.h>
40 #include <netinet/in.h>
41 #include <netinet/in_systm.h>
42 #include <netinet/ip.h>
43 #include <netinet/ip_icmp.h>
44 #include <netinet/icmp6.h>
45 #include <net/pfvar.h>
46 #include <arpa/inet.h>
47 #include <net/altq/altq.h>
48 #include <net/altq/altq_cbq.h>
49 #include <net/altq/altq_codel.h>
50 #include <net/altq/altq_priq.h>
51 #include <net/altq/altq_hfsc.h>
52 #include <net/altq/altq_fairq.h>
69 #include "pfctl_parser.h"
72 static struct pfctl *pf = NULL;
74 static int rulestate = 0;
75 static u_int16_t returnicmpdefault =
76 (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
77 static u_int16_t returnicmp6default =
78 (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
79 static int blockpolicy = PFRULE_DROP;
80 static int require_order = 1;
81 static int default_statelock;
83 static TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
85 TAILQ_ENTRY(file) entry;
91 struct file *pushfile(const char *, int);
93 int check_file_secrecy(int, const char *);
96 int yyerror(const char *, ...);
97 int kw_cmp(const void *, const void *);
103 static TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
105 TAILQ_ENTRY(sym) entry;
111 int symset(const char *, const char *, int);
112 char *symget(const char *);
114 int atoul(char *, u_long *);
127 struct node_proto *next;
128 struct node_proto *tail;
134 struct node_port *next;
135 struct node_port *tail;
141 struct node_uid *next;
142 struct node_uid *tail;
148 struct node_gid *next;
149 struct node_gid *tail;
156 struct node_icmp *next;
157 struct node_icmp *tail;
160 enum { PF_STATE_OPT_MAX, PF_STATE_OPT_NOSYNC, PF_STATE_OPT_SRCTRACK,
161 PF_STATE_OPT_MAX_SRC_STATES, PF_STATE_OPT_MAX_SRC_CONN,
162 PF_STATE_OPT_MAX_SRC_CONN_RATE, PF_STATE_OPT_MAX_SRC_NODES,
163 PF_STATE_OPT_OVERLOAD, PF_STATE_OPT_STATELOCK,
164 PF_STATE_OPT_TIMEOUT, PF_STATE_OPT_SLOPPY, };
166 enum { PF_SRCTRACK_NONE, PF_SRCTRACK, PF_SRCTRACK_GLOBAL, PF_SRCTRACK_RULE };
168 struct node_state_opt {
171 u_int32_t max_states;
172 u_int32_t max_src_states;
173 u_int32_t max_src_conn;
180 char tblname[PF_TABLE_NAME_SIZE];
182 u_int32_t max_src_nodes;
190 struct node_state_opt *next;
191 struct node_state_opt *tail;
195 struct node_host *host;
196 struct node_port *port;
199 static struct node_queue {
200 char queue[PF_QNAME_SIZE];
201 char parent[PF_QNAME_SIZE];
202 char ifname[IFNAMSIZ];
204 struct node_queue *next;
205 struct node_queue *tail;
208 struct node_qassign {
213 static struct filter_opts {
215 #define FOM_FLAGS 0x01
216 #define FOM_ICMP 0x02
218 #define FOM_KEEP 0x08
219 #define FOM_SRCTRACK 0x10
220 #define FOM_SETPRIO 0x0400
221 #define FOM_PRIO 0x2000
222 struct node_uid *uid;
223 struct node_gid *gid;
230 struct node_icmp *icmpspec;
235 struct node_state_opt *options;
240 struct node_qassign queues;
243 u_int8_t match_tag_not;
246 u_int8_t set_prio[2];
248 struct node_host *addr;
253 static struct antispoof_opts {
258 static struct scrub_opts {
260 #define SOM_MINTTL 0x01
261 #define SOM_MAXMSS 0x02
262 #define SOM_FRAGCACHE 0x04
263 #define SOM_SETTOS 0x08
272 u_int8_t match_tag_not;
276 static struct queue_opts {
278 #define QOM_BWSPEC 0x01
279 #define QOM_SCHEDULER 0x02
280 #define QOM_PRIORITY 0x04
281 #define QOM_TBRSIZE 0x08
282 #define QOM_QLIMIT 0x10
283 struct node_queue_bw queue_bwspec;
284 struct node_queue_opt scheduler;
290 static struct table_opts {
293 struct node_tinithead init_nodes;
296 static struct pool_opts {
298 #define POM_TYPE 0x01
299 #define POM_STICKYADDRESS 0x02
303 struct pf_poolhashkey *key;
307 static struct codel_opts codel_opts;
308 static struct node_hfsc_opts hfsc_opts;
309 static struct node_fairq_opts fairq_opts;
310 static struct node_state_opt *keep_state_defaults = NULL;
312 int disallow_table(struct node_host *, const char *);
313 int disallow_urpf_failed(struct node_host *, const char *);
314 int disallow_alias(struct node_host *, const char *);
315 int rule_consistent(struct pf_rule *, int);
316 int filter_consistent(struct pf_rule *, int);
317 int nat_consistent(struct pf_rule *);
318 int rdr_consistent(struct pf_rule *);
319 int process_tabledef(char *, struct table_opts *);
320 void expand_label_str(char *, size_t, const char *, const char *);
321 void expand_label_if(const char *, char *, size_t, const char *);
322 void expand_label_addr(const char *, char *, size_t, u_int8_t,
324 void expand_label_port(const char *, char *, size_t,
326 void expand_label_proto(const char *, char *, size_t, u_int8_t);
327 void expand_label_nr(const char *, char *, size_t);
328 void expand_label(char *, size_t, const char *, u_int8_t,
329 struct node_host *, struct node_port *, struct node_host *,
330 struct node_port *, u_int8_t);
331 void expand_rule(struct pf_rule *, struct node_if *,
332 struct node_host *, struct node_proto *, struct node_os *,
333 struct node_host *, struct node_port *, struct node_host *,
334 struct node_port *, struct node_uid *, struct node_gid *,
335 struct node_icmp *, const char *);
336 int expand_altq(struct pf_altq *, struct node_if *,
337 struct node_queue *, struct node_queue_bw bwspec,
338 struct node_queue_opt *);
339 int expand_queue(struct pf_altq *, struct node_if *,
340 struct node_queue *, struct node_queue_bw,
341 struct node_queue_opt *);
342 int expand_skip_interface(struct node_if *);
344 int check_rulestate(int);
345 int getservice(char *);
346 int rule_label(struct pf_rule *, char *);
347 int rt_tableid_max(void);
349 void mv_rules(struct pf_ruleset *, struct pf_ruleset *);
350 void decide_address_family(struct node_host *, sa_family_t *);
351 void remove_invalid_hosts(struct node_host **, sa_family_t *);
352 int invalid_redirect(struct node_host *, sa_family_t);
353 u_int16_t parseicmpspec(char *, sa_family_t);
354 int kw_casecmp(const void *, const void *);
355 int map_tos(char *string, int *);
357 static TAILQ_HEAD(loadanchorshead, loadanchors)
358 loadanchorshead = TAILQ_HEAD_INITIALIZER(loadanchorshead);
361 TAILQ_ENTRY(loadanchors) entries;
384 struct node_if *interface;
385 struct node_proto *proto;
386 struct node_icmp *icmp;
387 struct node_host *host;
389 struct node_port *port;
390 struct node_uid *uid;
391 struct node_gid *gid;
392 struct node_state_opt *state_opt;
395 struct peer src, dst;
396 struct node_os *src_os;
399 struct node_host *host;
403 struct pf_poolhashkey *key;
406 struct node_host *host;
411 struct node_state_opt *options;
422 struct pf_poolhashkey *hashkey;
423 struct node_queue *queue;
424 struct node_queue_opt queue_options;
425 struct node_queue_bw queue_bwspec;
426 struct node_qassign qassign;
427 struct filter_opts filter_opts;
428 struct antispoof_opts antispoof_opts;
429 struct queue_opts queue_opts;
430 struct scrub_opts scrub_opts;
431 struct table_opts table_opts;
432 struct pool_opts pool_opts;
433 struct node_hfsc_opts hfsc_opts;
434 struct node_fairq_opts fairq_opts;
435 struct codel_opts codel_opts;
440 #define PPORT_RANGE 1
442 int parseport(char *, struct range *r, int);
444 #define DYNIF_MULTIADDR(addr) ((addr).type == PF_ADDR_DYNIFTL && \
445 (!((addr).iflags & PFI_AFLAG_NOALIAS) || \
446 !isdigit((addr).v.ifname[strlen((addr).v.ifname)-1])))
450 %token PASS BLOCK SCRUB RETURN IN OS OUT LOG QUICK ON FROM TO FLAGS
451 %token RETURNRST RETURNICMP RETURNICMP6 PROTO INET INET6 ALL ANY ICMPTYPE
452 %token ICMP6TYPE CODE KEEP MODULATE STATE PORT RDR NAT BINAT ARROW NODF
453 %token MINTTL ERROR ALLOWOPTS FASTROUTE FILENAME ROUTETO DUPTO REPLYTO NO LABEL
454 %token NOROUTE URPFFAILED FRAGMENT USER GROUP MAXMSS MAXIMUM TTL TOS DROP TABLE
455 %token REASSEMBLE FRAGDROP FRAGCROP ANCHOR NATANCHOR RDRANCHOR BINATANCHOR
456 %token SET OPTIMIZATION TIMEOUT LIMIT LOGINTERFACE BLOCKPOLICY RANDOMID
457 %token REQUIREORDER SYNPROXY FINGERPRINTS NOSYNC DEBUG SKIP HOSTID
458 %token ANTISPOOF FOR INCLUDE
459 %token BITMASK RANDOM SOURCEHASH ROUNDROBIN STATICPORT PROBABILITY
460 %token ALTQ CBQ CODEL PRIQ HFSC FAIRQ BANDWIDTH TBRSIZE LINKSHARE REALTIME
461 %token UPPERLIMIT QUEUE PRIORITY QLIMIT HOGS BUCKETS RTABLE TARGET INTERVAL
462 %token LOAD RULESET_OPTIMIZATION PRIO
463 %token STICKYADDRESS MAXSRCSTATES MAXSRCNODES SOURCETRACK GLOBAL RULE
464 %token MAXSRCCONN MAXSRCCONNRATE OVERLOAD FLUSH SLOPPY
465 %token TAGGED TAG IFBOUND FLOATING STATEPOLICY STATEDEFAULTS ROUTE SETTOS
466 %token DIVERTTO DIVERTREPLY
467 %token <v.string> STRING
468 %token <v.number> NUMBER
469 %token <v.i> PORTBINARY
470 %type <v.interface> interface if_list if_item_not if_item
471 %type <v.number> number icmptype icmp6type uid gid
472 %type <v.number> tos not yesno
473 %type <v.probability> probability
474 %type <v.i> no dir af fragcache optimizer
475 %type <v.i> sourcetrack flush unaryop statelock
476 %type <v.b> action nataction natpasslog scrubaction
477 %type <v.b> flags flag blockspec prio
478 %type <v.range> portplain portstar portrange
479 %type <v.hashkey> hashkey
480 %type <v.proto> proto proto_list proto_item
481 %type <v.number> protoval
482 %type <v.icmp> icmpspec
483 %type <v.icmp> icmp_list icmp_item
484 %type <v.icmp> icmp6_list icmp6_item
485 %type <v.number> reticmpspec reticmp6spec
486 %type <v.fromto> fromto
487 %type <v.peer> ipportspec from to
488 %type <v.host> ipspec toipspec xhost host dynaddr host_list
489 %type <v.host> redir_host_list redirspec
490 %type <v.host> route_host route_host_list routespec
491 %type <v.os> os xos os_list
492 %type <v.port> portspec port_list port_item
493 %type <v.uid> uids uid_list uid_item
494 %type <v.gid> gids gid_list gid_item
495 %type <v.route> route
496 %type <v.redirection> redirection redirpool
497 %type <v.string> label stringall tag anchorname
498 %type <v.string> string varstring numberstring
499 %type <v.keep_state> keep
500 %type <v.state_opt> state_opt_spec state_opt_list state_opt_item
501 %type <v.logquick> logquick quick log logopts logopt
502 %type <v.interface> antispoof_ifspc antispoof_iflst antispoof_if
503 %type <v.qassign> qname
504 %type <v.queue> qassign qassign_list qassign_item
505 %type <v.queue_options> scheduler
506 %type <v.number> cbqflags_list cbqflags_item
507 %type <v.number> priqflags_list priqflags_item
508 %type <v.hfsc_opts> hfscopts_list hfscopts_item hfsc_opts
509 %type <v.fairq_opts> fairqopts_list fairqopts_item fairq_opts
510 %type <v.codel_opts> codelopts_list codelopts_item codel_opts
511 %type <v.queue_bwspec> bandwidth
512 %type <v.filter_opts> filter_opts filter_opt filter_opts_l
513 %type <v.filter_opts> filter_sets filter_set filter_sets_l
514 %type <v.antispoof_opts> antispoof_opts antispoof_opt antispoof_opts_l
515 %type <v.queue_opts> queue_opts queue_opt queue_opts_l
516 %type <v.scrub_opts> scrub_opts scrub_opt scrub_opts_l
517 %type <v.table_opts> table_opts table_opt table_opts_l
518 %type <v.pool_opts> pool_opts pool_opt pool_opts_l
519 %type <v.tagged> tagged
520 %type <v.rtableid> rtable
523 ruleset : /* empty */
524 | ruleset include '\n'
526 | ruleset option '\n'
527 | ruleset scrubrule '\n'
528 | ruleset natrule '\n'
529 | ruleset binatrule '\n'
530 | ruleset pfrule '\n'
531 | ruleset anchorrule '\n'
532 | ruleset loadrule '\n'
533 | ruleset altqif '\n'
534 | ruleset queuespec '\n'
535 | ruleset varset '\n'
536 | ruleset antispoof '\n'
537 | ruleset tabledef '\n'
538 | '{' fakeanchor '}' '\n';
539 | ruleset error '\n' { file->errors++; }
542 include : INCLUDE STRING {
545 if ((nfile = pushfile($2, 0)) == NULL) {
546 yyerror("failed to include file %s", $2);
558 * apply to previouslys specified rule: must be careful to note
559 * what that is: pf or nat or binat or rdr
561 fakeanchor : fakeanchor '\n'
562 | fakeanchor anchorrule '\n'
563 | fakeanchor binatrule '\n'
564 | fakeanchor natrule '\n'
565 | fakeanchor pfrule '\n'
566 | fakeanchor error '\n'
570 if (!strcmp($1, "none"))
572 else if (!strcmp($1, "basic"))
573 $$ = PF_OPTIMIZE_BASIC;
574 else if (!strcmp($1, "profile"))
575 $$ = PF_OPTIMIZE_BASIC | PF_OPTIMIZE_PROFILE;
577 yyerror("unknown ruleset-optimization %s", $1);
583 option : SET OPTIMIZATION STRING {
584 if (check_rulestate(PFCTL_STATE_OPTION)) {
588 if (pfctl_set_optimization(pf, $3) != 0) {
589 yyerror("unknown optimization %s", $3);
595 | SET RULESET_OPTIMIZATION optimizer {
596 if (!(pf->opts & PF_OPT_OPTIMIZE)) {
597 pf->opts |= PF_OPT_OPTIMIZE;
601 | SET TIMEOUT timeout_spec
602 | SET TIMEOUT '{' optnl timeout_list '}'
603 | SET LIMIT limit_spec
604 | SET LIMIT '{' optnl limit_list '}'
605 | SET LOGINTERFACE stringall {
606 if (check_rulestate(PFCTL_STATE_OPTION)) {
610 if (pfctl_set_logif(pf, $3) != 0) {
611 yyerror("error setting loginterface %s", $3);
617 | SET HOSTID number {
618 if ($3 == 0 || $3 > UINT_MAX) {
619 yyerror("hostid must be non-zero");
622 if (pfctl_set_hostid(pf, $3) != 0) {
623 yyerror("error setting hostid %08x", $3);
627 | SET BLOCKPOLICY DROP {
628 if (pf->opts & PF_OPT_VERBOSE)
629 printf("set block-policy drop\n");
630 if (check_rulestate(PFCTL_STATE_OPTION))
632 blockpolicy = PFRULE_DROP;
634 | SET BLOCKPOLICY RETURN {
635 if (pf->opts & PF_OPT_VERBOSE)
636 printf("set block-policy return\n");
637 if (check_rulestate(PFCTL_STATE_OPTION))
639 blockpolicy = PFRULE_RETURN;
641 | SET REQUIREORDER yesno {
642 if (pf->opts & PF_OPT_VERBOSE)
643 printf("set require-order %s\n",
644 $3 == 1 ? "yes" : "no");
647 | SET FINGERPRINTS STRING {
648 if (pf->opts & PF_OPT_VERBOSE)
649 printf("set fingerprints \"%s\"\n", $3);
650 if (check_rulestate(PFCTL_STATE_OPTION)) {
654 if (!pf->anchor->name[0]) {
655 if (pfctl_file_fingerprints(pf->dev,
657 yyerror("error loading "
658 "fingerprints %s", $3);
665 | SET STATEPOLICY statelock {
666 if (pf->opts & PF_OPT_VERBOSE)
669 printf("set state-policy floating\n");
672 printf("set state-policy if-bound\n");
675 default_statelock = $3;
678 if (check_rulestate(PFCTL_STATE_OPTION)) {
682 if (pfctl_set_debug(pf, $3) != 0) {
683 yyerror("error setting debuglevel %s", $3);
689 | SET SKIP interface {
690 if (expand_skip_interface($3) != 0) {
691 yyerror("error setting skip interface(s)");
695 | SET STATEDEFAULTS state_opt_list {
696 if (keep_state_defaults != NULL) {
697 yyerror("cannot redefine state-defaults");
700 keep_state_defaults = $3;
704 stringall : STRING { $$ = $1; }
706 if (($$ = strdup("all")) == NULL) {
707 err(1, "stringall: strdup");
712 string : STRING string {
713 if (asprintf(&$$, "%s %s", $1, $2) == -1)
714 err(1, "string: asprintf");
721 varstring : numberstring varstring {
722 if (asprintf(&$$, "%s %s", $1, $2) == -1)
723 err(1, "string: asprintf");
730 numberstring : NUMBER {
732 if (asprintf(&s, "%lld", (long long)$1) == -1) {
733 yyerror("string: asprintf");
741 varset : STRING '=' varstring {
742 if (pf->opts & PF_OPT_VERBOSE)
743 printf("%s = \"%s\"\n", $1, $3);
744 if (symset($1, $3, 0) == -1)
745 err(1, "cannot store variable %s", $1);
751 anchorname : STRING { $$ = $1; }
752 | /* empty */ { $$ = NULL; }
755 pfa_anchorlist : /* empty */
756 | pfa_anchorlist '\n'
757 | pfa_anchorlist pfrule '\n'
758 | pfa_anchorlist anchorrule '\n'
763 char ta[PF_ANCHOR_NAME_SIZE];
764 struct pf_ruleset *rs;
766 /* steping into a brace anchor */
771 /* create a holding ruleset in the root */
772 snprintf(ta, PF_ANCHOR_NAME_SIZE, "_%d", pf->bn);
773 rs = pf_find_or_create_ruleset(ta);
775 err(1, "pfa_anchor: pf_find_or_create_ruleset");
776 pf->astack[pf->asd] = rs->anchor;
777 pf->anchor = rs->anchor;
778 } '\n' pfa_anchorlist '}'
780 pf->alast = pf->anchor;
782 pf->anchor = pf->astack[pf->asd];
787 anchorrule : ANCHOR anchorname dir quick interface af proto fromto
788 filter_opts pfa_anchor
791 struct node_proto *proto;
793 if (check_rulestate(PFCTL_STATE_FILTER)) {
799 if ($2 && ($2[0] == '_' || strstr($2, "/_") != NULL)) {
801 yyerror("anchor names beginning with '_' "
802 "are reserved for internal use");
806 memset(&r, 0, sizeof(r));
807 if (pf->astack[pf->asd + 1]) {
808 /* move inline rules into relative location */
810 &pf->astack[pf->asd]->ruleset,
811 $2 ? $2 : pf->alast->name);
813 if (r.anchor == NULL)
814 err(1, "anchorrule: unable to "
817 if (pf->alast != r.anchor) {
818 if (r.anchor->match) {
819 yyerror("inline anchor '%s' "
824 mv_rules(&pf->alast->ruleset,
827 pf_remove_if_empty_ruleset(&pf->alast->ruleset);
828 pf->alast = r.anchor;
831 yyerror("anchors without explicit "
832 "rules must specify a name");
840 r.rtableid = $9.rtableid;
843 if (strlcpy(r.tagname, $9.tag,
844 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
845 yyerror("tag too long, max %u chars",
846 PF_TAG_NAME_SIZE - 1);
850 if (strlcpy(r.match_tagname, $9.match_tag,
851 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
852 yyerror("tag too long, max %u chars",
853 PF_TAG_NAME_SIZE - 1);
856 r.match_tag_not = $9.match_tag_not;
857 if (rule_label(&r, $9.label))
860 r.flags = $9.flags.b1;
861 r.flagset = $9.flags.b2;
862 if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
863 yyerror("flags always false");
866 if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
867 for (proto = $7; proto != NULL &&
868 proto->proto != IPPROTO_TCP;
871 if (proto == NULL && $7 != NULL) {
872 if ($9.flags.b1 || $9.flags.b2)
874 "flags only apply to tcp");
877 "OS fingerprinting only "
885 if ($9.keep.action) {
886 yyerror("cannot specify state handling "
892 if (strlcpy(r.match_tagname, $9.match_tag,
893 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
894 yyerror("tag too long, max %u chars",
895 PF_TAG_NAME_SIZE - 1);
898 r.match_tag_not = $9.match_tag_not;
899 if ($9.marker & FOM_PRIO) {
901 r.prio = PF_PRIO_ZERO;
905 if ($9.marker & FOM_SETPRIO) {
906 r.set_prio[0] = $9.set_prio[0];
907 r.set_prio[1] = $9.set_prio[1];
908 r.scrub_flags |= PFSTATE_SETPRIO;
911 decide_address_family($8.src.host, &r.af);
912 decide_address_family($8.dst.host, &r.af);
914 expand_rule(&r, $5, NULL, $7, $8.src_os,
915 $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
916 $9.uid, $9.gid, $9.icmpspec,
917 pf->astack[pf->asd + 1] ? pf->alast->name : $2);
919 pf->astack[pf->asd + 1] = NULL;
921 | NATANCHOR string interface af proto fromto rtable {
924 if (check_rulestate(PFCTL_STATE_NAT)) {
929 memset(&r, 0, sizeof(r));
934 decide_address_family($6.src.host, &r.af);
935 decide_address_family($6.dst.host, &r.af);
937 expand_rule(&r, $3, NULL, $5, $6.src_os,
938 $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
942 | RDRANCHOR string interface af proto fromto rtable {
945 if (check_rulestate(PFCTL_STATE_NAT)) {
950 memset(&r, 0, sizeof(r));
955 decide_address_family($6.src.host, &r.af);
956 decide_address_family($6.dst.host, &r.af);
958 if ($6.src.port != NULL) {
959 yyerror("source port parameter not supported"
963 if ($6.dst.port != NULL) {
964 if ($6.dst.port->next != NULL) {
965 yyerror("destination port list "
966 "expansion not supported in "
969 } else if ($6.dst.port->op != PF_OP_EQ) {
970 yyerror("destination port operators"
971 " not supported in rdr-anchor");
974 r.dst.port[0] = $6.dst.port->port[0];
975 r.dst.port[1] = $6.dst.port->port[1];
976 r.dst.port_op = $6.dst.port->op;
979 expand_rule(&r, $3, NULL, $5, $6.src_os,
980 $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
984 | BINATANCHOR string interface af proto fromto rtable {
987 if (check_rulestate(PFCTL_STATE_NAT)) {
992 memset(&r, 0, sizeof(r));
997 if ($5->next != NULL) {
998 yyerror("proto list expansion"
999 " not supported in binat-anchor");
1002 r.proto = $5->proto;
1006 if ($6.src.host != NULL || $6.src.port != NULL ||
1007 $6.dst.host != NULL || $6.dst.port != NULL) {
1008 yyerror("fromto parameter not supported"
1009 " in binat-anchor");
1013 decide_address_family($6.src.host, &r.af);
1014 decide_address_family($6.dst.host, &r.af);
1016 pfctl_add_rule(pf, &r, $2);
1021 loadrule : LOAD ANCHOR string FROM string {
1022 struct loadanchors *loadanchor;
1024 if (strlen(pf->anchor->name) + 1 +
1025 strlen($3) >= MAXPATHLEN) {
1026 yyerror("anchorname %s too long, max %u\n",
1027 $3, MAXPATHLEN - 1);
1031 loadanchor = calloc(1, sizeof(struct loadanchors));
1032 if (loadanchor == NULL)
1033 err(1, "loadrule: calloc");
1034 if ((loadanchor->anchorname = malloc(MAXPATHLEN)) ==
1036 err(1, "loadrule: malloc");
1037 if (pf->anchor->name[0])
1038 snprintf(loadanchor->anchorname, MAXPATHLEN,
1039 "%s/%s", pf->anchor->name, $3);
1041 strlcpy(loadanchor->anchorname, $3, MAXPATHLEN);
1042 if ((loadanchor->filename = strdup($5)) == NULL)
1043 err(1, "loadrule: strdup");
1045 TAILQ_INSERT_TAIL(&loadanchorshead, loadanchor,
1052 scrubaction : no SCRUB {
1061 scrubrule : scrubaction dir logquick interface af proto fromto scrub_opts
1065 if (check_rulestate(PFCTL_STATE_SCRUB))
1068 memset(&r, 0, sizeof(r));
1076 yyerror("scrub rules do not support 'quick'");
1082 r.rule_flag |= PFRULE_NODF;
1084 r.rule_flag |= PFRULE_RANDOMID;
1085 if ($8.reassemble_tcp) {
1086 if (r.direction != PF_INOUT) {
1087 yyerror("reassemble tcp rules can not "
1088 "specify direction");
1091 r.rule_flag |= PFRULE_REASSEMBLE_TCP;
1094 r.min_ttl = $8.minttl;
1096 r.max_mss = $8.maxmss;
1097 if ($8.marker & SOM_SETTOS) {
1098 r.rule_flag |= PFRULE_SET_TOS;
1099 r.set_tos = $8.settos;
1102 r.rule_flag |= $8.fragcache;
1104 if (strlcpy(r.match_tagname, $8.match_tag,
1105 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1106 yyerror("tag too long, max %u chars",
1107 PF_TAG_NAME_SIZE - 1);
1110 r.match_tag_not = $8.match_tag_not;
1111 r.rtableid = $8.rtableid;
1113 expand_rule(&r, $4, NULL, $6, $7.src_os,
1114 $7.src.host, $7.src.port, $7.dst.host, $7.dst.port,
1115 NULL, NULL, NULL, "");
1120 bzero(&scrub_opts, sizeof scrub_opts);
1121 scrub_opts.rtableid = -1;
1124 { $$ = scrub_opts; }
1126 bzero(&scrub_opts, sizeof scrub_opts);
1127 scrub_opts.rtableid = -1;
1132 scrub_opts_l : scrub_opts_l scrub_opt
1137 if (scrub_opts.nodf) {
1138 yyerror("no-df cannot be respecified");
1141 scrub_opts.nodf = 1;
1144 if (scrub_opts.marker & SOM_MINTTL) {
1145 yyerror("min-ttl cannot be respecified");
1148 if ($2 < 0 || $2 > 255) {
1149 yyerror("illegal min-ttl value %d", $2);
1152 scrub_opts.marker |= SOM_MINTTL;
1153 scrub_opts.minttl = $2;
1156 if (scrub_opts.marker & SOM_MAXMSS) {
1157 yyerror("max-mss cannot be respecified");
1160 if ($2 < 0 || $2 > 65535) {
1161 yyerror("illegal max-mss value %d", $2);
1164 scrub_opts.marker |= SOM_MAXMSS;
1165 scrub_opts.maxmss = $2;
1168 if (scrub_opts.marker & SOM_SETTOS) {
1169 yyerror("set-tos cannot be respecified");
1172 scrub_opts.marker |= SOM_SETTOS;
1173 scrub_opts.settos = $2;
1176 if (scrub_opts.marker & SOM_FRAGCACHE) {
1177 yyerror("fragcache cannot be respecified");
1180 scrub_opts.marker |= SOM_FRAGCACHE;
1181 scrub_opts.fragcache = $1;
1183 | REASSEMBLE STRING {
1184 if (strcasecmp($2, "tcp") != 0) {
1185 yyerror("scrub reassemble supports only tcp, "
1191 if (scrub_opts.reassemble_tcp) {
1192 yyerror("reassemble tcp cannot be respecified");
1195 scrub_opts.reassemble_tcp = 1;
1198 if (scrub_opts.randomid) {
1199 yyerror("random-id cannot be respecified");
1202 scrub_opts.randomid = 1;
1205 if ($2 < 0 || $2 > rt_tableid_max()) {
1206 yyerror("invalid rtable id");
1209 scrub_opts.rtableid = $2;
1211 | not TAGGED string {
1212 scrub_opts.match_tag = $3;
1213 scrub_opts.match_tag_not = $1;
1217 fragcache : FRAGMENT REASSEMBLE { $$ = 0; /* default */ }
1218 | FRAGMENT FRAGCROP { $$ = 0; }
1219 | FRAGMENT FRAGDROP { $$ = 0; }
1222 antispoof : ANTISPOOF logquick antispoof_ifspc af antispoof_opts {
1224 struct node_host *h = NULL, *hh;
1225 struct node_if *i, *j;
1227 if (check_rulestate(PFCTL_STATE_FILTER))
1230 for (i = $3; i; i = i->next) {
1231 bzero(&r, sizeof(r));
1234 r.direction = PF_IN;
1239 if (rule_label(&r, $5.label))
1241 r.rtableid = $5.rtableid;
1242 j = calloc(1, sizeof(struct node_if));
1244 err(1, "antispoof: calloc");
1245 if (strlcpy(j->ifname, i->ifname,
1246 sizeof(j->ifname)) >= sizeof(j->ifname)) {
1248 yyerror("interface name too long");
1253 h = calloc(1, sizeof(*h));
1255 err(1, "address: calloc");
1256 h->addr.type = PF_ADDR_DYNIFTL;
1258 if (strlcpy(h->addr.v.ifname, i->ifname,
1259 sizeof(h->addr.v.ifname)) >=
1260 sizeof(h->addr.v.ifname)) {
1263 "interface name too long");
1266 hh = malloc(sizeof(*hh));
1268 err(1, "address: malloc");
1269 bcopy(h, hh, sizeof(*hh));
1270 h->addr.iflags = PFI_AFLAG_NETWORK;
1272 h = ifa_lookup(j->ifname,
1278 expand_rule(&r, j, NULL, NULL, NULL, h,
1279 NULL, NULL, NULL, NULL, NULL,
1282 if ((i->ifa_flags & IFF_LOOPBACK) == 0) {
1283 bzero(&r, sizeof(r));
1286 r.direction = PF_IN;
1291 if (rule_label(&r, $5.label))
1293 r.rtableid = $5.rtableid;
1297 h = ifa_lookup(i->ifname, 0);
1299 expand_rule(&r, NULL, NULL,
1300 NULL, NULL, h, NULL, NULL,
1301 NULL, NULL, NULL, NULL, "");
1309 antispoof_ifspc : FOR antispoof_if { $$ = $2; }
1310 | FOR '{' optnl antispoof_iflst '}' { $$ = $4; }
1313 antispoof_iflst : antispoof_if optnl { $$ = $1; }
1314 | antispoof_iflst comma antispoof_if optnl {
1315 $1->tail->next = $3;
1321 antispoof_if : if_item { $$ = $1; }
1329 bzero(&antispoof_opts, sizeof antispoof_opts);
1330 antispoof_opts.rtableid = -1;
1333 { $$ = antispoof_opts; }
1335 bzero(&antispoof_opts, sizeof antispoof_opts);
1336 antispoof_opts.rtableid = -1;
1337 $$ = antispoof_opts;
1341 antispoof_opts_l : antispoof_opts_l antispoof_opt
1345 antispoof_opt : label {
1346 if (antispoof_opts.label) {
1347 yyerror("label cannot be redefined");
1350 antispoof_opts.label = $1;
1353 if ($2 < 0 || $2 > rt_tableid_max()) {
1354 yyerror("invalid rtable id");
1357 antispoof_opts.rtableid = $2;
1361 not : '!' { $$ = 1; }
1362 | /* empty */ { $$ = 0; }
1365 tabledef : TABLE '<' STRING '>' table_opts {
1366 struct node_host *h, *nh;
1367 struct node_tinit *ti, *nti;
1369 if (strlen($3) >= PF_TABLE_NAME_SIZE) {
1370 yyerror("table name too long, max %d chars",
1371 PF_TABLE_NAME_SIZE - 1);
1375 if (pf->loadopt & PFCTL_FLAG_TABLE)
1376 if (process_tabledef($3, &$5)) {
1381 for (ti = SIMPLEQ_FIRST(&$5.init_nodes);
1382 ti != SIMPLEQ_END(&$5.init_nodes); ti = nti) {
1385 for (h = ti->host; h != NULL; h = nh) {
1389 nti = SIMPLEQ_NEXT(ti, entries);
1396 bzero(&table_opts, sizeof table_opts);
1397 SIMPLEQ_INIT(&table_opts.init_nodes);
1400 { $$ = table_opts; }
1403 bzero(&table_opts, sizeof table_opts);
1404 SIMPLEQ_INIT(&table_opts.init_nodes);
1409 table_opts_l : table_opts_l table_opt
1413 table_opt : STRING {
1414 if (!strcmp($1, "const"))
1415 table_opts.flags |= PFR_TFLAG_CONST;
1416 else if (!strcmp($1, "persist"))
1417 table_opts.flags |= PFR_TFLAG_PERSIST;
1418 else if (!strcmp($1, "counters"))
1419 table_opts.flags |= PFR_TFLAG_COUNTERS;
1421 yyerror("invalid table option '%s'", $1);
1427 | '{' optnl '}' { table_opts.init_addr = 1; }
1428 | '{' optnl host_list '}' {
1429 struct node_host *n;
1430 struct node_tinit *ti;
1432 for (n = $3; n != NULL; n = n->next) {
1433 switch (n->addr.type) {
1434 case PF_ADDR_ADDRMASK:
1437 yyerror("address ranges are not "
1438 "permitted inside tables");
1440 case PF_ADDR_DYNIFTL:
1441 yyerror("dynamic addresses are not "
1442 "permitted inside tables");
1445 yyerror("tables cannot contain tables");
1447 case PF_ADDR_NOROUTE:
1448 yyerror("\"no-route\" is not permitted "
1451 case PF_ADDR_URPFFAILED:
1452 yyerror("\"urpf-failed\" is not "
1453 "permitted inside tables");
1456 yyerror("unknown address type %d",
1461 if (!(ti = calloc(1, sizeof(*ti))))
1462 err(1, "table_opt: calloc");
1464 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1466 table_opts.init_addr = 1;
1469 struct node_tinit *ti;
1471 if (!(ti = calloc(1, sizeof(*ti))))
1472 err(1, "table_opt: calloc");
1474 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1476 table_opts.init_addr = 1;
1480 altqif : ALTQ interface queue_opts QUEUE qassign {
1483 if (check_rulestate(PFCTL_STATE_QUEUE))
1486 memset(&a, 0, sizeof(a));
1487 if ($3.scheduler.qtype == ALTQT_NONE) {
1488 yyerror("no scheduler specified!");
1491 a.scheduler = $3.scheduler.qtype;
1492 a.qlimit = $3.qlimit;
1493 a.tbrsize = $3.tbrsize;
1494 if ($5 == NULL && $3.scheduler.qtype != ALTQT_CODEL) {
1495 yyerror("no child queues specified");
1498 if (expand_altq(&a, $2, $5, $3.queue_bwspec,
1504 queuespec : QUEUE STRING interface queue_opts qassign {
1507 if (check_rulestate(PFCTL_STATE_QUEUE)) {
1512 memset(&a, 0, sizeof(a));
1514 if (strlcpy(a.qname, $2, sizeof(a.qname)) >=
1516 yyerror("queue name too long (max "
1517 "%d chars)", PF_QNAME_SIZE-1);
1523 yyerror("cannot specify tbrsize for queue");
1526 if ($4.priority > 255) {
1527 yyerror("priority out of range: max 255");
1530 a.priority = $4.priority;
1531 a.qlimit = $4.qlimit;
1532 a.scheduler = $4.scheduler.qtype;
1533 if (expand_queue(&a, $3, $5, $4.queue_bwspec,
1535 yyerror("errors in queue definition");
1542 bzero(&queue_opts, sizeof queue_opts);
1543 queue_opts.priority = DEFAULT_PRIORITY;
1544 queue_opts.qlimit = DEFAULT_QLIMIT;
1545 queue_opts.scheduler.qtype = ALTQT_NONE;
1546 queue_opts.queue_bwspec.bw_percent = 100;
1549 { $$ = queue_opts; }
1551 bzero(&queue_opts, sizeof queue_opts);
1552 queue_opts.priority = DEFAULT_PRIORITY;
1553 queue_opts.qlimit = DEFAULT_QLIMIT;
1554 queue_opts.scheduler.qtype = ALTQT_NONE;
1555 queue_opts.queue_bwspec.bw_percent = 100;
1560 queue_opts_l : queue_opts_l queue_opt
1564 queue_opt : BANDWIDTH bandwidth {
1565 if (queue_opts.marker & QOM_BWSPEC) {
1566 yyerror("bandwidth cannot be respecified");
1569 queue_opts.marker |= QOM_BWSPEC;
1570 queue_opts.queue_bwspec = $2;
1573 if (queue_opts.marker & QOM_PRIORITY) {
1574 yyerror("priority cannot be respecified");
1577 if ($2 < 0 || $2 > 255) {
1578 yyerror("priority out of range: max 255");
1581 queue_opts.marker |= QOM_PRIORITY;
1582 queue_opts.priority = $2;
1585 if (queue_opts.marker & QOM_QLIMIT) {
1586 yyerror("qlimit cannot be respecified");
1589 if ($2 < 0 || $2 > 65535) {
1590 yyerror("qlimit out of range: max 65535");
1593 queue_opts.marker |= QOM_QLIMIT;
1594 queue_opts.qlimit = $2;
1597 if (queue_opts.marker & QOM_SCHEDULER) {
1598 yyerror("scheduler cannot be respecified");
1601 queue_opts.marker |= QOM_SCHEDULER;
1602 queue_opts.scheduler = $1;
1605 if (queue_opts.marker & QOM_TBRSIZE) {
1606 yyerror("tbrsize cannot be respecified");
1609 if ($2 < 0 || $2 > 65535) {
1610 yyerror("tbrsize too big: max 65535");
1613 queue_opts.marker |= QOM_TBRSIZE;
1614 queue_opts.tbrsize = $2;
1618 bandwidth : STRING {
1624 bps = strtod($1, &cp);
1626 if (strlen(cp) > 1) {
1628 if (!strcmp(cu, "Bit") ||
1630 !strcmp(cu, "bit") ||
1635 if (!strcmp(cp, "b"))
1637 else if (!strcmp(cp, "K"))
1639 else if (!strcmp(cp, "M"))
1641 else if (!strcmp(cp, "G"))
1642 bps *= 1000 * 1000 * 1000;
1643 else if (!strcmp(cp, "%")) {
1644 if (bps < 0 || bps > 100) {
1645 yyerror("bandwidth spec "
1650 $$.bw_percent = bps;
1653 yyerror("unknown unit %s", cp);
1659 $$.bw_absolute = (u_int32_t)bps;
1662 if ($1 < 0 || $1 > UINT_MAX) {
1663 yyerror("bandwidth number too big");
1667 $$.bw_absolute = $1;
1672 $$.qtype = ALTQT_CBQ;
1673 $$.data.cbq_opts.flags = 0;
1675 | CBQ '(' cbqflags_list ')' {
1676 $$.qtype = ALTQT_CBQ;
1677 $$.data.cbq_opts.flags = $3;
1680 $$.qtype = ALTQT_PRIQ;
1681 $$.data.priq_opts.flags = 0;
1683 | PRIQ '(' priqflags_list ')' {
1684 $$.qtype = ALTQT_PRIQ;
1685 $$.data.priq_opts.flags = $3;
1688 $$.qtype = ALTQT_HFSC;
1689 bzero(&$$.data.hfsc_opts,
1690 sizeof(struct node_hfsc_opts));
1692 | HFSC '(' hfsc_opts ')' {
1693 $$.qtype = ALTQT_HFSC;
1694 $$.data.hfsc_opts = $3;
1697 $$.qtype = ALTQT_FAIRQ;
1698 bzero(&$$.data.fairq_opts,
1699 sizeof(struct node_fairq_opts));
1701 | FAIRQ '(' fairq_opts ')' {
1702 $$.qtype = ALTQT_FAIRQ;
1703 $$.data.fairq_opts = $3;
1706 $$.qtype = ALTQT_CODEL;
1707 bzero(&$$.data.codel_opts,
1708 sizeof(struct codel_opts));
1710 | CODEL '(' codel_opts ')' {
1711 $$.qtype = ALTQT_CODEL;
1712 $$.data.codel_opts = $3;
1716 cbqflags_list : cbqflags_item { $$ |= $1; }
1717 | cbqflags_list comma cbqflags_item { $$ |= $3; }
1720 cbqflags_item : STRING {
1721 if (!strcmp($1, "default"))
1722 $$ = CBQCLF_DEFCLASS;
1723 else if (!strcmp($1, "borrow"))
1725 else if (!strcmp($1, "red"))
1727 else if (!strcmp($1, "ecn"))
1728 $$ = CBQCLF_RED|CBQCLF_ECN;
1729 else if (!strcmp($1, "rio"))
1731 else if (!strcmp($1, "codel"))
1734 yyerror("unknown cbq flag \"%s\"", $1);
1742 priqflags_list : priqflags_item { $$ |= $1; }
1743 | priqflags_list comma priqflags_item { $$ |= $3; }
1746 priqflags_item : STRING {
1747 if (!strcmp($1, "default"))
1748 $$ = PRCF_DEFAULTCLASS;
1749 else if (!strcmp($1, "red"))
1751 else if (!strcmp($1, "ecn"))
1752 $$ = PRCF_RED|PRCF_ECN;
1753 else if (!strcmp($1, "rio"))
1755 else if (!strcmp($1, "codel"))
1758 yyerror("unknown priq flag \"%s\"", $1);
1768 sizeof(struct node_hfsc_opts));
1775 hfscopts_list : hfscopts_item
1776 | hfscopts_list comma hfscopts_item
1779 hfscopts_item : LINKSHARE bandwidth {
1780 if (hfsc_opts.linkshare.used) {
1781 yyerror("linkshare already specified");
1784 hfsc_opts.linkshare.m2 = $2;
1785 hfsc_opts.linkshare.used = 1;
1787 | LINKSHARE '(' bandwidth comma NUMBER comma bandwidth ')'
1789 if ($5 < 0 || $5 > INT_MAX) {
1790 yyerror("timing in curve out of range");
1793 if (hfsc_opts.linkshare.used) {
1794 yyerror("linkshare already specified");
1797 hfsc_opts.linkshare.m1 = $3;
1798 hfsc_opts.linkshare.d = $5;
1799 hfsc_opts.linkshare.m2 = $7;
1800 hfsc_opts.linkshare.used = 1;
1802 | REALTIME bandwidth {
1803 if (hfsc_opts.realtime.used) {
1804 yyerror("realtime already specified");
1807 hfsc_opts.realtime.m2 = $2;
1808 hfsc_opts.realtime.used = 1;
1810 | REALTIME '(' bandwidth comma NUMBER comma bandwidth ')'
1812 if ($5 < 0 || $5 > INT_MAX) {
1813 yyerror("timing in curve out of range");
1816 if (hfsc_opts.realtime.used) {
1817 yyerror("realtime already specified");
1820 hfsc_opts.realtime.m1 = $3;
1821 hfsc_opts.realtime.d = $5;
1822 hfsc_opts.realtime.m2 = $7;
1823 hfsc_opts.realtime.used = 1;
1825 | UPPERLIMIT bandwidth {
1826 if (hfsc_opts.upperlimit.used) {
1827 yyerror("upperlimit already specified");
1830 hfsc_opts.upperlimit.m2 = $2;
1831 hfsc_opts.upperlimit.used = 1;
1833 | UPPERLIMIT '(' bandwidth comma NUMBER comma bandwidth ')'
1835 if ($5 < 0 || $5 > INT_MAX) {
1836 yyerror("timing in curve out of range");
1839 if (hfsc_opts.upperlimit.used) {
1840 yyerror("upperlimit already specified");
1843 hfsc_opts.upperlimit.m1 = $3;
1844 hfsc_opts.upperlimit.d = $5;
1845 hfsc_opts.upperlimit.m2 = $7;
1846 hfsc_opts.upperlimit.used = 1;
1849 if (!strcmp($1, "default"))
1850 hfsc_opts.flags |= HFCF_DEFAULTCLASS;
1851 else if (!strcmp($1, "red"))
1852 hfsc_opts.flags |= HFCF_RED;
1853 else if (!strcmp($1, "ecn"))
1854 hfsc_opts.flags |= HFCF_RED|HFCF_ECN;
1855 else if (!strcmp($1, "rio"))
1856 hfsc_opts.flags |= HFCF_RIO;
1857 else if (!strcmp($1, "codel"))
1858 hfsc_opts.flags |= HFCF_CODEL;
1860 yyerror("unknown hfsc flag \"%s\"", $1);
1870 sizeof(struct node_fairq_opts));
1877 fairqopts_list : fairqopts_item
1878 | fairqopts_list comma fairqopts_item
1881 fairqopts_item : LINKSHARE bandwidth {
1882 if (fairq_opts.linkshare.used) {
1883 yyerror("linkshare already specified");
1886 fairq_opts.linkshare.m2 = $2;
1887 fairq_opts.linkshare.used = 1;
1889 | LINKSHARE '(' bandwidth number bandwidth ')' {
1890 if (fairq_opts.linkshare.used) {
1891 yyerror("linkshare already specified");
1894 fairq_opts.linkshare.m1 = $3;
1895 fairq_opts.linkshare.d = $4;
1896 fairq_opts.linkshare.m2 = $5;
1897 fairq_opts.linkshare.used = 1;
1900 fairq_opts.hogs_bw = $2;
1903 fairq_opts.nbuckets = $2;
1906 if (!strcmp($1, "default"))
1907 fairq_opts.flags |= FARF_DEFAULTCLASS;
1908 else if (!strcmp($1, "red"))
1909 fairq_opts.flags |= FARF_RED;
1910 else if (!strcmp($1, "ecn"))
1911 fairq_opts.flags |= FARF_RED|FARF_ECN;
1912 else if (!strcmp($1, "rio"))
1913 fairq_opts.flags |= FARF_RIO;
1914 else if (!strcmp($1, "codel"))
1915 fairq_opts.flags |= FARF_CODEL;
1917 yyerror("unknown fairq flag \"%s\"", $1);
1927 sizeof(struct codel_opts));
1934 codelopts_list : codelopts_item
1935 | codelopts_list comma codelopts_item
1938 codelopts_item : INTERVAL number {
1939 if (codel_opts.interval) {
1940 yyerror("interval already specified");
1943 codel_opts.interval = $2;
1946 if (codel_opts.target) {
1947 yyerror("target already specified");
1950 codel_opts.target = $2;
1953 if (!strcmp($1, "ecn"))
1956 yyerror("unknown codel option \"%s\"", $1);
1964 qassign : /* empty */ { $$ = NULL; }
1965 | qassign_item { $$ = $1; }
1966 | '{' optnl qassign_list '}' { $$ = $3; }
1969 qassign_list : qassign_item optnl { $$ = $1; }
1970 | qassign_list comma qassign_item optnl {
1971 $1->tail->next = $3;
1977 qassign_item : STRING {
1978 $$ = calloc(1, sizeof(struct node_queue));
1980 err(1, "qassign_item: calloc");
1981 if (strlcpy($$->queue, $1, sizeof($$->queue)) >=
1982 sizeof($$->queue)) {
1983 yyerror("queue name '%s' too long (max "
1984 "%d chars)", $1, sizeof($$->queue)-1);
1995 pfrule : action dir logquick interface route af proto fromto
1999 struct node_state_opt *o;
2000 struct node_proto *proto;
2006 if (check_rulestate(PFCTL_STATE_FILTER))
2009 memset(&r, 0, sizeof(r));
2013 case PFRULE_RETURNRST:
2014 r.rule_flag |= PFRULE_RETURNRST;
2015 r.return_ttl = $1.w;
2017 case PFRULE_RETURNICMP:
2018 r.rule_flag |= PFRULE_RETURNICMP;
2019 r.return_icmp = $1.w;
2020 r.return_icmp6 = $1.w2;
2023 r.rule_flag |= PFRULE_RETURN;
2024 r.return_icmp = $1.w;
2025 r.return_icmp6 = $1.w2;
2033 r.rtableid = $9.rtableid;
2035 if ($9.marker & FOM_PRIO) {
2037 r.prio = PF_PRIO_ZERO;
2041 if ($9.marker & FOM_SETPRIO) {
2042 r.set_prio[0] = $9.set_prio[0];
2043 r.set_prio[1] = $9.set_prio[1];
2044 r.scrub_flags |= PFSTATE_SETPRIO;
2049 if (strlcpy(r.tagname, $9.tag,
2050 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
2051 yyerror("tag too long, max %u chars",
2052 PF_TAG_NAME_SIZE - 1);
2056 if (strlcpy(r.match_tagname, $9.match_tag,
2057 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
2058 yyerror("tag too long, max %u chars",
2059 PF_TAG_NAME_SIZE - 1);
2062 r.match_tag_not = $9.match_tag_not;
2063 if (rule_label(&r, $9.label))
2066 r.flags = $9.flags.b1;
2067 r.flagset = $9.flags.b2;
2068 if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
2069 yyerror("flags always false");
2072 if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
2073 for (proto = $7; proto != NULL &&
2074 proto->proto != IPPROTO_TCP;
2075 proto = proto->next)
2077 if (proto == NULL && $7 != NULL) {
2078 if ($9.flags.b1 || $9.flags.b2)
2080 "flags only apply to tcp");
2083 "OS fingerprinting only "
2088 if (($9.flags.b1 & parse_flags("S")) == 0 &&
2090 yyerror("OS fingerprinting requires "
2091 "the SYN TCP flag (flags S/SA)");
2098 r.keep_state = $9.keep.action;
2099 o = $9.keep.options;
2101 /* 'keep state' by default on pass rules. */
2102 if (!r.keep_state && !r.action &&
2103 !($9.marker & FOM_KEEP)) {
2104 r.keep_state = PF_STATE_NORMAL;
2105 o = keep_state_defaults;
2110 struct node_state_opt *p = o;
2113 case PF_STATE_OPT_MAX:
2115 yyerror("state option 'max' "
2116 "multiple definitions");
2119 r.max_states = o->data.max_states;
2121 case PF_STATE_OPT_NOSYNC:
2122 if (r.rule_flag & PFRULE_NOSYNC) {
2123 yyerror("state option 'sync' "
2124 "multiple definitions");
2127 r.rule_flag |= PFRULE_NOSYNC;
2129 case PF_STATE_OPT_SRCTRACK:
2131 yyerror("state option "
2133 "multiple definitions");
2136 srctrack = o->data.src_track;
2137 r.rule_flag |= PFRULE_SRCTRACK;
2139 case PF_STATE_OPT_MAX_SRC_STATES:
2140 if (r.max_src_states) {
2141 yyerror("state option "
2143 "multiple definitions");
2146 if (o->data.max_src_states == 0) {
2147 yyerror("'max-src-states' must "
2152 o->data.max_src_states;
2153 r.rule_flag |= PFRULE_SRCTRACK;
2155 case PF_STATE_OPT_OVERLOAD:
2156 if (r.overload_tblname[0]) {
2157 yyerror("multiple 'overload' "
2158 "table definitions");
2161 if (strlcpy(r.overload_tblname,
2162 o->data.overload.tblname,
2163 PF_TABLE_NAME_SIZE) >=
2164 PF_TABLE_NAME_SIZE) {
2165 yyerror("state option: "
2169 r.flush = o->data.overload.flush;
2171 case PF_STATE_OPT_MAX_SRC_CONN:
2172 if (r.max_src_conn) {
2173 yyerror("state option "
2175 "multiple definitions");
2178 if (o->data.max_src_conn == 0) {
2179 yyerror("'max-src-conn' "
2184 o->data.max_src_conn;
2185 r.rule_flag |= PFRULE_SRCTRACK |
2186 PFRULE_RULESRCTRACK;
2188 case PF_STATE_OPT_MAX_SRC_CONN_RATE:
2189 if (r.max_src_conn_rate.limit) {
2190 yyerror("state option "
2191 "'max-src-conn-rate' "
2192 "multiple definitions");
2195 if (!o->data.max_src_conn_rate.limit ||
2196 !o->data.max_src_conn_rate.seconds) {
2197 yyerror("'max-src-conn-rate' "
2198 "values must be > 0");
2201 if (o->data.max_src_conn_rate.limit >
2203 yyerror("'max-src-conn-rate' "
2204 "maximum rate must be < %u",
2208 r.max_src_conn_rate.limit =
2209 o->data.max_src_conn_rate.limit;
2210 r.max_src_conn_rate.seconds =
2211 o->data.max_src_conn_rate.seconds;
2212 r.rule_flag |= PFRULE_SRCTRACK |
2213 PFRULE_RULESRCTRACK;
2215 case PF_STATE_OPT_MAX_SRC_NODES:
2216 if (r.max_src_nodes) {
2217 yyerror("state option "
2219 "multiple definitions");
2222 if (o->data.max_src_nodes == 0) {
2223 yyerror("'max-src-nodes' must "
2228 o->data.max_src_nodes;
2229 r.rule_flag |= PFRULE_SRCTRACK |
2230 PFRULE_RULESRCTRACK;
2232 case PF_STATE_OPT_STATELOCK:
2234 yyerror("state locking option: "
2235 "multiple definitions");
2239 r.rule_flag |= o->data.statelock;
2241 case PF_STATE_OPT_SLOPPY:
2242 if (r.rule_flag & PFRULE_STATESLOPPY) {
2243 yyerror("state sloppy option: "
2244 "multiple definitions");
2247 r.rule_flag |= PFRULE_STATESLOPPY;
2249 case PF_STATE_OPT_TIMEOUT:
2250 if (o->data.timeout.number ==
2251 PFTM_ADAPTIVE_START ||
2252 o->data.timeout.number ==
2255 if (r.timeout[o->data.timeout.number]) {
2256 yyerror("state timeout %s "
2257 "multiple definitions",
2258 pf_timeouts[o->data.
2259 timeout.number].name);
2262 r.timeout[o->data.timeout.number] =
2263 o->data.timeout.seconds;
2270 /* 'flags S/SA' by default on stateful rules */
2271 if (!r.action && !r.flags && !r.flagset &&
2272 !$9.fragment && !($9.marker & FOM_FLAGS) &&
2274 r.flags = parse_flags("S");
2275 r.flagset = parse_flags("SA");
2277 if (!adaptive && r.max_states) {
2278 r.timeout[PFTM_ADAPTIVE_START] =
2279 (r.max_states / 10) * 6;
2280 r.timeout[PFTM_ADAPTIVE_END] =
2281 (r.max_states / 10) * 12;
2283 if (r.rule_flag & PFRULE_SRCTRACK) {
2284 if (srctrack == PF_SRCTRACK_GLOBAL &&
2286 yyerror("'max-src-nodes' is "
2287 "incompatible with "
2288 "'source-track global'");
2291 if (srctrack == PF_SRCTRACK_GLOBAL &&
2293 yyerror("'max-src-conn' is "
2294 "incompatible with "
2295 "'source-track global'");
2298 if (srctrack == PF_SRCTRACK_GLOBAL &&
2299 r.max_src_conn_rate.seconds) {
2300 yyerror("'max-src-conn-rate' is "
2301 "incompatible with "
2302 "'source-track global'");
2305 if (r.timeout[PFTM_SRC_NODE] <
2306 r.max_src_conn_rate.seconds)
2307 r.timeout[PFTM_SRC_NODE] =
2308 r.max_src_conn_rate.seconds;
2309 r.rule_flag |= PFRULE_SRCTRACK;
2310 if (srctrack == PF_SRCTRACK_RULE)
2311 r.rule_flag |= PFRULE_RULESRCTRACK;
2313 if (r.keep_state && !statelock)
2314 r.rule_flag |= default_statelock;
2317 r.rule_flag |= PFRULE_FRAGMENT;
2318 r.allow_opts = $9.allowopts;
2320 decide_address_family($8.src.host, &r.af);
2321 decide_address_family($8.dst.host, &r.af);
2325 yyerror("direction must be explicit "
2326 "with rules that specify routing");
2330 r.rpool.opts = $5.pool_opts;
2332 memcpy(&r.rpool.key, $5.key,
2333 sizeof(struct pf_poolhashkey));
2336 decide_address_family($5.host, &r.af);
2337 remove_invalid_hosts(&$5.host, &r.af);
2338 if ($5.host == NULL) {
2339 yyerror("no routing address with "
2340 "matching address family found.");
2343 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
2344 PF_POOL_NONE && ($5.host->next != NULL ||
2345 $5.host->addr.type == PF_ADDR_TABLE ||
2346 DYNIF_MULTIADDR($5.host->addr)))
2347 r.rpool.opts |= PF_POOL_ROUNDROBIN;
2348 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2349 PF_POOL_ROUNDROBIN &&
2350 disallow_table($5.host, "tables are only "
2351 "supported in round-robin routing pools"))
2353 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2354 PF_POOL_ROUNDROBIN &&
2355 disallow_alias($5.host, "interface (%s) "
2356 "is only supported in round-robin "
2359 if ($5.host->next != NULL) {
2360 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2361 PF_POOL_ROUNDROBIN) {
2362 yyerror("r.rpool.opts must "
2363 "be PF_POOL_ROUNDROBIN");
2368 if ($9.queues.qname != NULL) {
2369 if (strlcpy(r.qname, $9.queues.qname,
2370 sizeof(r.qname)) >= sizeof(r.qname)) {
2371 yyerror("rule qname too long (max "
2372 "%d chars)", sizeof(r.qname)-1);
2375 free($9.queues.qname);
2377 if ($9.queues.pqname != NULL) {
2378 if (strlcpy(r.pqname, $9.queues.pqname,
2379 sizeof(r.pqname)) >= sizeof(r.pqname)) {
2380 yyerror("rule pqname too long (max "
2381 "%d chars)", sizeof(r.pqname)-1);
2384 free($9.queues.pqname);
2387 r.divert.port = $9.divert.port;
2389 if ((r.divert.port = $9.divert.port)) {
2390 if (r.direction == PF_OUT) {
2391 if ($9.divert.addr) {
2392 yyerror("address specified "
2393 "for outgoing divert");
2396 bzero(&r.divert.addr,
2397 sizeof(r.divert.addr));
2399 if (!$9.divert.addr) {
2400 yyerror("no address specified "
2401 "for incoming divert");
2404 if ($9.divert.addr->af != r.af) {
2405 yyerror("address family "
2406 "mismatch for divert");
2410 $9.divert.addr->addr.v.a.addr;
2415 expand_rule(&r, $4, $5.host, $7, $8.src_os,
2416 $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
2417 $9.uid, $9.gid, $9.icmpspec, "");
2422 bzero(&filter_opts, sizeof filter_opts);
2423 filter_opts.rtableid = -1;
2426 { $$ = filter_opts; }
2428 bzero(&filter_opts, sizeof filter_opts);
2429 filter_opts.rtableid = -1;
2434 filter_opts_l : filter_opts_l filter_opt
2438 filter_opt : USER uids {
2439 if (filter_opts.uid)
2440 $2->tail->next = filter_opts.uid;
2441 filter_opts.uid = $2;
2444 if (filter_opts.gid)
2445 $2->tail->next = filter_opts.gid;
2446 filter_opts.gid = $2;
2449 if (filter_opts.marker & FOM_FLAGS) {
2450 yyerror("flags cannot be redefined");
2453 filter_opts.marker |= FOM_FLAGS;
2454 filter_opts.flags.b1 |= $1.b1;
2455 filter_opts.flags.b2 |= $1.b2;
2456 filter_opts.flags.w |= $1.w;
2457 filter_opts.flags.w2 |= $1.w2;
2460 if (filter_opts.marker & FOM_ICMP) {
2461 yyerror("icmp-type cannot be redefined");
2464 filter_opts.marker |= FOM_ICMP;
2465 filter_opts.icmpspec = $1;
2468 if (filter_opts.marker & FOM_PRIO) {
2469 yyerror("prio cannot be redefined");
2472 if ($2 < 0 || $2 > PF_PRIO_MAX) {
2473 yyerror("prio must be 0 - %u", PF_PRIO_MAX);
2476 filter_opts.marker |= FOM_PRIO;
2477 filter_opts.prio = $2;
2480 if (filter_opts.marker & FOM_TOS) {
2481 yyerror("tos cannot be redefined");
2484 filter_opts.marker |= FOM_TOS;
2485 filter_opts.tos = $2;
2488 if (filter_opts.marker & FOM_KEEP) {
2489 yyerror("modulate or keep cannot be redefined");
2492 filter_opts.marker |= FOM_KEEP;
2493 filter_opts.keep.action = $1.action;
2494 filter_opts.keep.options = $1.options;
2497 filter_opts.fragment = 1;
2500 filter_opts.allowopts = 1;
2503 if (filter_opts.label) {
2504 yyerror("label cannot be redefined");
2507 filter_opts.label = $1;
2510 if (filter_opts.queues.qname) {
2511 yyerror("queue cannot be redefined");
2514 filter_opts.queues = $1;
2517 filter_opts.tag = $2;
2519 | not TAGGED string {
2520 filter_opts.match_tag = $3;
2521 filter_opts.match_tag_not = $1;
2523 | PROBABILITY probability {
2526 p = floor($2 * UINT_MAX + 0.5);
2527 if (p < 0.0 || p > UINT_MAX) {
2528 yyerror("invalid probability: %lf", p);
2531 filter_opts.prob = (u_int32_t)p;
2532 if (filter_opts.prob == 0)
2533 filter_opts.prob = 1;
2536 if ($2 < 0 || $2 > rt_tableid_max()) {
2537 yyerror("invalid rtable id");
2540 filter_opts.rtableid = $2;
2542 | DIVERTTO portplain {
2544 filter_opts.divert.port = $2.a;
2545 if (!filter_opts.divert.port) {
2546 yyerror("invalid divert port: %u", ntohs($2.a));
2551 | DIVERTTO STRING PORT portplain {
2553 if ((filter_opts.divert.addr = host($2)) == NULL) {
2554 yyerror("could not parse divert address: %s",
2563 filter_opts.divert.port = $4.a;
2564 if (!filter_opts.divert.port) {
2565 yyerror("invalid divert port: %u", ntohs($4.a));
2571 yyerror("divert-reply has no meaning in FreeBSD pf(4)");
2574 filter_opts.divert.port = 1; /* some random value */
2580 filter_sets : SET '(' filter_sets_l ')' { $$ = filter_opts; }
2581 | SET filter_set { $$ = filter_opts; }
2584 filter_sets_l : filter_sets_l comma filter_set
2589 if (filter_opts.marker & FOM_SETPRIO) {
2590 yyerror("prio cannot be redefined");
2593 filter_opts.marker |= FOM_SETPRIO;
2594 filter_opts.set_prio[0] = $1.b1;
2595 filter_opts.set_prio[1] = $1.b2;
2597 prio : PRIO NUMBER {
2598 if ($2 < 0 || $2 > PF_PRIO_MAX) {
2599 yyerror("prio must be 0 - %u", PF_PRIO_MAX);
2604 | PRIO '(' NUMBER comma NUMBER ')' {
2605 if ($3 < 0 || $3 > PF_PRIO_MAX ||
2606 $5 < 0 || $5 > PF_PRIO_MAX) {
2607 yyerror("prio must be 0 - %u", PF_PRIO_MAX);
2615 probability : STRING {
2617 double p = strtod($1, &e);
2624 yyerror("invalid probability: %s", $1);
2637 action : PASS { $$.b1 = PF_PASS; $$.b2 = $$.w = 0; }
2638 | BLOCK blockspec { $$ = $2; $$.b1 = PF_DROP; }
2641 blockspec : /* empty */ {
2642 $$.b2 = blockpolicy;
2643 $$.w = returnicmpdefault;
2644 $$.w2 = returnicmp6default;
2647 $$.b2 = PFRULE_DROP;
2652 $$.b2 = PFRULE_RETURNRST;
2656 | RETURNRST '(' TTL NUMBER ')' {
2657 if ($4 < 0 || $4 > 255) {
2658 yyerror("illegal ttl value %d", $4);
2661 $$.b2 = PFRULE_RETURNRST;
2666 $$.b2 = PFRULE_RETURNICMP;
2667 $$.w = returnicmpdefault;
2668 $$.w2 = returnicmp6default;
2671 $$.b2 = PFRULE_RETURNICMP;
2672 $$.w = returnicmpdefault;
2673 $$.w2 = returnicmp6default;
2675 | RETURNICMP '(' reticmpspec ')' {
2676 $$.b2 = PFRULE_RETURNICMP;
2678 $$.w2 = returnicmpdefault;
2680 | RETURNICMP6 '(' reticmp6spec ')' {
2681 $$.b2 = PFRULE_RETURNICMP;
2682 $$.w = returnicmpdefault;
2685 | RETURNICMP '(' reticmpspec comma reticmp6spec ')' {
2686 $$.b2 = PFRULE_RETURNICMP;
2691 $$.b2 = PFRULE_RETURN;
2692 $$.w = returnicmpdefault;
2693 $$.w2 = returnicmp6default;
2697 reticmpspec : STRING {
2698 if (!($$ = parseicmpspec($1, AF_INET))) {
2707 if ($1 < 0 || $1 > 255) {
2708 yyerror("invalid icmp code %lu", $1);
2711 icmptype = returnicmpdefault >> 8;
2712 $$ = (icmptype << 8 | $1);
2716 reticmp6spec : STRING {
2717 if (!($$ = parseicmpspec($1, AF_INET6))) {
2726 if ($1 < 0 || $1 > 255) {
2727 yyerror("invalid icmp code %lu", $1);
2730 icmptype = returnicmp6default >> 8;
2731 $$ = (icmptype << 8 | $1);
2735 dir : /* empty */ { $$ = PF_INOUT; }
2736 | IN { $$ = PF_IN; }
2737 | OUT { $$ = PF_OUT; }
2740 quick : /* empty */ { $$.quick = 0; }
2741 | QUICK { $$.quick = 1; }
2744 logquick : /* empty */ { $$.log = 0; $$.quick = 0; $$.logif = 0; }
2745 | log { $$ = $1; $$.quick = 0; }
2746 | QUICK { $$.quick = 1; $$.log = 0; $$.logif = 0; }
2747 | log QUICK { $$ = $1; $$.quick = 1; }
2748 | QUICK log { $$ = $2; $$.quick = 1; }
2751 log : LOG { $$.log = PF_LOG; $$.logif = 0; }
2752 | LOG '(' logopts ')' {
2753 $$.log = PF_LOG | $3.log;
2754 $$.logif = $3.logif;
2758 logopts : logopt { $$ = $1; }
2759 | logopts comma logopt {
2760 $$.log = $1.log | $3.log;
2761 $$.logif = $3.logif;
2763 $$.logif = $1.logif;
2767 logopt : ALL { $$.log = PF_LOG_ALL; $$.logif = 0; }
2768 | USER { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
2769 | GROUP { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
2775 if (strncmp($2, "pflog", 5)) {
2776 yyerror("%s: should be a pflog interface", $2);
2780 i = strtonum($2 + 5, 0, 255, &errstr);
2782 yyerror("%s: %s", $2, errstr);
2791 interface : /* empty */ { $$ = NULL; }
2792 | ON if_item_not { $$ = $2; }
2793 | ON '{' optnl if_list '}' { $$ = $4; }
2796 if_list : if_item_not optnl { $$ = $1; }
2797 | if_list comma if_item_not optnl {
2798 $1->tail->next = $3;
2804 if_item_not : not if_item { $$ = $2; $$->not = $1; }
2808 struct node_host *n;
2810 $$ = calloc(1, sizeof(struct node_if));
2812 err(1, "if_item: calloc");
2813 if (strlcpy($$->ifname, $1, sizeof($$->ifname)) >=
2814 sizeof($$->ifname)) {
2817 yyerror("interface name too long");
2821 if ((n = ifa_exists($1)) != NULL)
2822 $$->ifa_flags = n->ifa_flags;
2831 af : /* empty */ { $$ = 0; }
2832 | INET { $$ = AF_INET; }
2833 | INET6 { $$ = AF_INET6; }
2836 proto : /* empty */ { $$ = NULL; }
2837 | PROTO proto_item { $$ = $2; }
2838 | PROTO '{' optnl proto_list '}' { $$ = $4; }
2841 proto_list : proto_item optnl { $$ = $1; }
2842 | proto_list comma proto_item optnl {
2843 $1->tail->next = $3;
2849 proto_item : protoval {
2854 yyerror("proto 0 cannot be used");
2857 $$ = calloc(1, sizeof(struct node_proto));
2859 err(1, "proto_item: calloc");
2869 p = getprotobyname($1);
2871 yyerror("unknown protocol %s", $1);
2879 if ($1 < 0 || $1 > 255) {
2880 yyerror("protocol outside range");
2900 os : /* empty */ { $$ = NULL; }
2901 | OS xos { $$ = $2; }
2902 | OS '{' optnl os_list '}' { $$ = $4; }
2906 $$ = calloc(1, sizeof(struct node_os));
2908 err(1, "os: calloc");
2914 os_list : xos optnl { $$ = $1; }
2915 | os_list comma xos optnl {
2916 $1->tail->next = $3;
2922 from : /* empty */ {
2936 if (disallow_urpf_failed($2.host, "\"urpf-failed\" is "
2937 "not permitted in a destination address"))
2943 ipportspec : ipspec {
2947 | ipspec PORT portspec {
2961 ipspec : ANY { $$ = NULL; }
2962 | xhost { $$ = $1; }
2963 | '{' optnl host_list '}' { $$ = $3; }
2966 toipspec : TO ipspec { $$ = $2; }
2967 | /* empty */ { $$ = NULL; }
2970 host_list : ipspec optnl { $$ = $1; }
2971 | host_list comma ipspec optnl {
2974 else if ($1 == NULL)
2977 $1->tail->next = $3;
2978 $1->tail = $3->tail;
2985 struct node_host *n;
2987 for (n = $2; n != NULL; n = n->next)
2992 $$ = calloc(1, sizeof(struct node_host));
2994 err(1, "xhost: calloc");
2995 $$->addr.type = PF_ADDR_NOROUTE;
3001 $$ = calloc(1, sizeof(struct node_host));
3003 err(1, "xhost: calloc");
3004 $$->addr.type = PF_ADDR_URPFFAILED;
3012 if (($$ = host($1)) == NULL) {
3013 /* error. "any" is handled elsewhere */
3015 yyerror("could not parse host specification");
3021 | STRING '-' STRING {
3022 struct node_host *b, *e;
3024 if ((b = host($1)) == NULL || (e = host($3)) == NULL) {
3027 yyerror("could not parse host specification");
3030 if (b->af != e->af ||
3031 b->addr.type != PF_ADDR_ADDRMASK ||
3032 e->addr.type != PF_ADDR_ADDRMASK ||
3033 unmask(&b->addr.v.a.mask, b->af) !=
3034 (b->af == AF_INET ? 32 : 128) ||
3035 unmask(&e->addr.v.a.mask, e->af) !=
3036 (e->af == AF_INET ? 32 : 128) ||
3037 b->next != NULL || b->not ||
3038 e->next != NULL || e->not) {
3043 yyerror("invalid address range");
3046 memcpy(&b->addr.v.a.mask, &e->addr.v.a.addr,
3047 sizeof(b->addr.v.a.mask));
3048 b->addr.type = PF_ADDR_RANGE;
3054 | STRING '/' NUMBER {
3057 if (asprintf(&buf, "%s/%lld", $1, (long long)$3) == -1)
3058 err(1, "host: asprintf");
3060 if (($$ = host(buf)) == NULL) {
3061 /* error. "any" is handled elsewhere */
3063 yyerror("could not parse host specification");
3068 | NUMBER '/' NUMBER {
3071 /* ie. for 10/8 parsing */
3073 if (asprintf(&buf, "%lld/%lld", (long long)$1, (long long)$3) == -1)
3075 if (asprintf(&buf, "%lld/%lld", $1, $3) == -1)
3077 err(1, "host: asprintf");
3078 if (($$ = host(buf)) == NULL) {
3079 /* error. "any" is handled elsewhere */
3081 yyerror("could not parse host specification");
3087 | dynaddr '/' NUMBER {
3088 struct node_host *n;
3090 if ($3 < 0 || $3 > 128) {
3091 yyerror("bit number too big");
3095 for (n = $1; n != NULL; n = n->next)
3099 if (strlen($2) >= PF_TABLE_NAME_SIZE) {
3100 yyerror("table name '%s' too long", $2);
3104 $$ = calloc(1, sizeof(struct node_host));
3106 err(1, "host: calloc");
3107 $$->addr.type = PF_ADDR_TABLE;
3108 if (strlcpy($$->addr.v.tblname, $2,
3109 sizeof($$->addr.v.tblname)) >=
3110 sizeof($$->addr.v.tblname))
3111 errx(1, "host: strlcpy");
3122 if (atoul($1, &ulval) == -1) {
3123 yyerror("%s is not a number", $1);
3132 dynaddr : '(' STRING ')' {
3137 if (!isalpha(op[0])) {
3138 yyerror("invalid interface name '%s'", op);
3142 while ((p = strrchr($2, ':')) != NULL) {
3143 if (!strcmp(p+1, "network"))
3144 flags |= PFI_AFLAG_NETWORK;
3145 else if (!strcmp(p+1, "broadcast"))
3146 flags |= PFI_AFLAG_BROADCAST;
3147 else if (!strcmp(p+1, "peer"))
3148 flags |= PFI_AFLAG_PEER;
3149 else if (!strcmp(p+1, "0"))
3150 flags |= PFI_AFLAG_NOALIAS;
3152 yyerror("interface %s has bad modifier",
3159 if (flags & (flags - 1) & PFI_AFLAG_MODEMASK) {
3161 yyerror("illegal combination of "
3162 "interface modifiers");
3165 $$ = calloc(1, sizeof(struct node_host));
3167 err(1, "address: calloc");
3169 set_ipmask($$, 128);
3170 $$->addr.type = PF_ADDR_DYNIFTL;
3171 $$->addr.iflags = flags;
3172 if (strlcpy($$->addr.v.ifname, $2,
3173 sizeof($$->addr.v.ifname)) >=
3174 sizeof($$->addr.v.ifname)) {
3177 yyerror("interface name too long");
3186 portspec : port_item { $$ = $1; }
3187 | '{' optnl port_list '}' { $$ = $3; }
3190 port_list : port_item optnl { $$ = $1; }
3191 | port_list comma port_item optnl {
3192 $1->tail->next = $3;
3198 port_item : portrange {
3199 $$ = calloc(1, sizeof(struct node_port));
3201 err(1, "port_item: calloc");
3211 | unaryop portrange {
3213 yyerror("':' cannot be used with an other "
3217 $$ = calloc(1, sizeof(struct node_port));
3219 err(1, "port_item: calloc");
3226 | portrange PORTBINARY portrange {
3228 yyerror("':' cannot be used with an other "
3232 $$ = calloc(1, sizeof(struct node_port));
3234 err(1, "port_item: calloc");
3243 portplain : numberstring {
3244 if (parseport($1, &$$, 0) == -1) {
3252 portrange : numberstring {
3253 if (parseport($1, &$$, PPORT_RANGE) == -1) {
3261 uids : uid_item { $$ = $1; }
3262 | '{' optnl uid_list '}' { $$ = $3; }
3265 uid_list : uid_item optnl { $$ = $1; }
3266 | uid_list comma uid_item optnl {
3267 $1->tail->next = $3;
3274 $$ = calloc(1, sizeof(struct node_uid));
3276 err(1, "uid_item: calloc");
3284 if ($2 == UID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
3285 yyerror("user unknown requires operator = or "
3289 $$ = calloc(1, sizeof(struct node_uid));
3291 err(1, "uid_item: calloc");
3298 | uid PORTBINARY uid {
3299 if ($1 == UID_MAX || $3 == UID_MAX) {
3300 yyerror("user unknown requires operator = or "
3304 $$ = calloc(1, sizeof(struct node_uid));
3306 err(1, "uid_item: calloc");
3316 if (!strcmp($1, "unknown"))
3321 if ((pw = getpwnam($1)) == NULL) {
3322 yyerror("unknown user %s", $1);
3331 if ($1 < 0 || $1 >= UID_MAX) {
3332 yyerror("illegal uid value %lu", $1);
3339 gids : gid_item { $$ = $1; }
3340 | '{' optnl gid_list '}' { $$ = $3; }
3343 gid_list : gid_item optnl { $$ = $1; }
3344 | gid_list comma gid_item optnl {
3345 $1->tail->next = $3;
3352 $$ = calloc(1, sizeof(struct node_gid));
3354 err(1, "gid_item: calloc");
3362 if ($2 == GID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
3363 yyerror("group unknown requires operator = or "
3367 $$ = calloc(1, sizeof(struct node_gid));
3369 err(1, "gid_item: calloc");
3376 | gid PORTBINARY gid {
3377 if ($1 == GID_MAX || $3 == GID_MAX) {
3378 yyerror("group unknown requires operator = or "
3382 $$ = calloc(1, sizeof(struct node_gid));
3384 err(1, "gid_item: calloc");
3394 if (!strcmp($1, "unknown"))
3399 if ((grp = getgrnam($1)) == NULL) {
3400 yyerror("unknown group %s", $1);
3409 if ($1 < 0 || $1 >= GID_MAX) {
3410 yyerror("illegal gid value %lu", $1);
3420 if ((f = parse_flags($1)) < 0) {
3421 yyerror("bad flags %s", $1);
3430 flags : FLAGS flag '/' flag { $$.b1 = $2.b1; $$.b2 = $4.b1; }
3431 | FLAGS '/' flag { $$.b1 = 0; $$.b2 = $3.b1; }
3432 | FLAGS ANY { $$.b1 = 0; $$.b2 = 0; }
3435 icmpspec : ICMPTYPE icmp_item { $$ = $2; }
3436 | ICMPTYPE '{' optnl icmp_list '}' { $$ = $4; }
3437 | ICMP6TYPE icmp6_item { $$ = $2; }
3438 | ICMP6TYPE '{' optnl icmp6_list '}' { $$ = $4; }
3441 icmp_list : icmp_item optnl { $$ = $1; }
3442 | icmp_list comma icmp_item optnl {
3443 $1->tail->next = $3;
3449 icmp6_list : icmp6_item optnl { $$ = $1; }
3450 | icmp6_list comma icmp6_item optnl {
3451 $1->tail->next = $3;
3457 icmp_item : icmptype {
3458 $$ = calloc(1, sizeof(struct node_icmp));
3460 err(1, "icmp_item: calloc");
3463 $$->proto = IPPROTO_ICMP;
3467 | icmptype CODE STRING {
3468 const struct icmpcodeent *p;
3470 if ((p = geticmpcodebyname($1-1, $3, AF_INET)) == NULL) {
3471 yyerror("unknown icmp-code %s", $3);
3477 $$ = calloc(1, sizeof(struct node_icmp));
3479 err(1, "icmp_item: calloc");
3481 $$->code = p->code + 1;
3482 $$->proto = IPPROTO_ICMP;
3486 | icmptype CODE NUMBER {
3487 if ($3 < 0 || $3 > 255) {
3488 yyerror("illegal icmp-code %lu", $3);
3491 $$ = calloc(1, sizeof(struct node_icmp));
3493 err(1, "icmp_item: calloc");
3496 $$->proto = IPPROTO_ICMP;
3502 icmp6_item : icmp6type {
3503 $$ = calloc(1, sizeof(struct node_icmp));
3505 err(1, "icmp_item: calloc");
3508 $$->proto = IPPROTO_ICMPV6;
3512 | icmp6type CODE STRING {
3513 const struct icmpcodeent *p;
3515 if ((p = geticmpcodebyname($1-1, $3, AF_INET6)) == NULL) {
3516 yyerror("unknown icmp6-code %s", $3);
3522 $$ = calloc(1, sizeof(struct node_icmp));
3524 err(1, "icmp_item: calloc");
3526 $$->code = p->code + 1;
3527 $$->proto = IPPROTO_ICMPV6;
3531 | icmp6type CODE NUMBER {
3532 if ($3 < 0 || $3 > 255) {
3533 yyerror("illegal icmp-code %lu", $3);
3536 $$ = calloc(1, sizeof(struct node_icmp));
3538 err(1, "icmp_item: calloc");
3541 $$->proto = IPPROTO_ICMPV6;
3548 const struct icmptypeent *p;
3550 if ((p = geticmptypebyname($1, AF_INET)) == NULL) {
3551 yyerror("unknown icmp-type %s", $1);
3559 if ($1 < 0 || $1 > 255) {
3560 yyerror("illegal icmp-type %lu", $1);
3567 icmp6type : STRING {
3568 const struct icmptypeent *p;
3570 if ((p = geticmptypebyname($1, AF_INET6)) ==
3572 yyerror("unknown icmp6-type %s", $1);
3580 if ($1 < 0 || $1 > 255) {
3581 yyerror("illegal icmp6-type %lu", $1);
3592 if (map_tos($1, &val))
3594 else if ($1[0] == '0' && $1[1] == 'x') {
3596 $$ = strtoul($1, &end, 16);
3597 if (errno || *end != '\0')
3600 $$ = 256; /* flag bad argument */
3601 if ($$ < 0 || $$ > 255) {
3602 yyerror("illegal tos value %s", $1);
3610 if ($$ < 0 || $$ > 255) {
3611 yyerror("illegal tos value %s", $1);
3617 sourcetrack : SOURCETRACK { $$ = PF_SRCTRACK; }
3618 | SOURCETRACK GLOBAL { $$ = PF_SRCTRACK_GLOBAL; }
3619 | SOURCETRACK RULE { $$ = PF_SRCTRACK_RULE; }
3622 statelock : IFBOUND {
3623 $$ = PFRULE_IFBOUND;
3634 | KEEP STATE state_opt_spec {
3635 $$.action = PF_STATE_NORMAL;
3638 | MODULATE STATE state_opt_spec {
3639 $$.action = PF_STATE_MODULATE;
3642 | SYNPROXY STATE state_opt_spec {
3643 $$.action = PF_STATE_SYNPROXY;
3648 flush : /* empty */ { $$ = 0; }
3649 | FLUSH { $$ = PF_FLUSH; }
3651 $$ = PF_FLUSH | PF_FLUSH_GLOBAL;
3655 state_opt_spec : '(' state_opt_list ')' { $$ = $2; }
3656 | /* empty */ { $$ = NULL; }
3659 state_opt_list : state_opt_item { $$ = $1; }
3660 | state_opt_list comma state_opt_item {
3661 $1->tail->next = $3;
3667 state_opt_item : MAXIMUM NUMBER {
3668 if ($2 < 0 || $2 > UINT_MAX) {
3669 yyerror("only positive values permitted");
3672 $$ = calloc(1, sizeof(struct node_state_opt));
3674 err(1, "state_opt_item: calloc");
3675 $$->type = PF_STATE_OPT_MAX;
3676 $$->data.max_states = $2;
3681 $$ = calloc(1, sizeof(struct node_state_opt));
3683 err(1, "state_opt_item: calloc");
3684 $$->type = PF_STATE_OPT_NOSYNC;
3688 | MAXSRCSTATES NUMBER {
3689 if ($2 < 0 || $2 > UINT_MAX) {
3690 yyerror("only positive values permitted");
3693 $$ = calloc(1, sizeof(struct node_state_opt));
3695 err(1, "state_opt_item: calloc");
3696 $$->type = PF_STATE_OPT_MAX_SRC_STATES;
3697 $$->data.max_src_states = $2;
3701 | MAXSRCCONN NUMBER {
3702 if ($2 < 0 || $2 > UINT_MAX) {
3703 yyerror("only positive values permitted");
3706 $$ = calloc(1, sizeof(struct node_state_opt));
3708 err(1, "state_opt_item: calloc");
3709 $$->type = PF_STATE_OPT_MAX_SRC_CONN;
3710 $$->data.max_src_conn = $2;
3714 | MAXSRCCONNRATE NUMBER '/' NUMBER {
3715 if ($2 < 0 || $2 > UINT_MAX ||
3716 $4 < 0 || $4 > UINT_MAX) {
3717 yyerror("only positive values permitted");
3720 $$ = calloc(1, sizeof(struct node_state_opt));
3722 err(1, "state_opt_item: calloc");
3723 $$->type = PF_STATE_OPT_MAX_SRC_CONN_RATE;
3724 $$->data.max_src_conn_rate.limit = $2;
3725 $$->data.max_src_conn_rate.seconds = $4;
3729 | OVERLOAD '<' STRING '>' flush {
3730 if (strlen($3) >= PF_TABLE_NAME_SIZE) {
3731 yyerror("table name '%s' too long", $3);
3735 $$ = calloc(1, sizeof(struct node_state_opt));
3737 err(1, "state_opt_item: calloc");
3738 if (strlcpy($$->data.overload.tblname, $3,
3739 PF_TABLE_NAME_SIZE) >= PF_TABLE_NAME_SIZE)
3740 errx(1, "state_opt_item: strlcpy");
3742 $$->type = PF_STATE_OPT_OVERLOAD;
3743 $$->data.overload.flush = $5;
3747 | MAXSRCNODES NUMBER {
3748 if ($2 < 0 || $2 > UINT_MAX) {
3749 yyerror("only positive values permitted");
3752 $$ = calloc(1, sizeof(struct node_state_opt));
3754 err(1, "state_opt_item: calloc");
3755 $$->type = PF_STATE_OPT_MAX_SRC_NODES;
3756 $$->data.max_src_nodes = $2;
3761 $$ = calloc(1, sizeof(struct node_state_opt));
3763 err(1, "state_opt_item: calloc");
3764 $$->type = PF_STATE_OPT_SRCTRACK;
3765 $$->data.src_track = $1;
3770 $$ = calloc(1, sizeof(struct node_state_opt));
3772 err(1, "state_opt_item: calloc");
3773 $$->type = PF_STATE_OPT_STATELOCK;
3774 $$->data.statelock = $1;
3779 $$ = calloc(1, sizeof(struct node_state_opt));
3781 err(1, "state_opt_item: calloc");
3782 $$->type = PF_STATE_OPT_SLOPPY;
3789 if ($2 < 0 || $2 > UINT_MAX) {
3790 yyerror("only positive values permitted");
3793 for (i = 0; pf_timeouts[i].name &&
3794 strcmp(pf_timeouts[i].name, $1); ++i)
3796 if (!pf_timeouts[i].name) {
3797 yyerror("illegal timeout name %s", $1);
3801 if (strchr(pf_timeouts[i].name, '.') == NULL) {
3802 yyerror("illegal state timeout %s", $1);
3807 $$ = calloc(1, sizeof(struct node_state_opt));
3809 err(1, "state_opt_item: calloc");
3810 $$->type = PF_STATE_OPT_TIMEOUT;
3811 $$->data.timeout.number = pf_timeouts[i].timeout;
3812 $$->data.timeout.seconds = $2;
3818 label : LABEL STRING {
3823 qname : QUEUE STRING {
3827 | QUEUE '(' STRING ')' {
3831 | QUEUE '(' STRING comma STRING ')' {
3837 no : /* empty */ { $$ = 0; }
3841 portstar : numberstring {
3842 if (parseport($1, &$$, PPORT_RANGE|PPORT_STAR) == -1) {
3850 redirspec : host { $$ = $1; }
3851 | '{' optnl redir_host_list '}' { $$ = $3; }
3854 redir_host_list : host optnl { $$ = $1; }
3855 | redir_host_list comma host optnl {
3856 $1->tail->next = $3;
3857 $1->tail = $3->tail;
3862 redirpool : /* empty */ { $$ = NULL; }
3864 $$ = calloc(1, sizeof(struct redirection));
3866 err(1, "redirection: calloc");
3868 $$->rport.a = $$->rport.b = $$->rport.t = 0;
3870 | ARROW redirspec PORT portstar {
3871 $$ = calloc(1, sizeof(struct redirection));
3873 err(1, "redirection: calloc");
3879 hashkey : /* empty */
3881 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3883 err(1, "hashkey: calloc");
3884 $$->key32[0] = arc4random();
3885 $$->key32[1] = arc4random();
3886 $$->key32[2] = arc4random();
3887 $$->key32[3] = arc4random();
3891 if (!strncmp($1, "0x", 2)) {
3892 if (strlen($1) != 34) {
3894 yyerror("hex key must be 128 bits "
3895 "(32 hex digits) long");
3898 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3900 err(1, "hashkey: calloc");
3902 if (sscanf($1, "0x%8x%8x%8x%8x",
3903 &$$->key32[0], &$$->key32[1],
3904 &$$->key32[2], &$$->key32[3]) != 4) {
3907 yyerror("invalid hex key");
3913 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3915 err(1, "hashkey: calloc");
3917 MD5Update(&context, (unsigned char *)$1,
3919 MD5Final((unsigned char *)$$, &context);
3920 HTONL($$->key32[0]);
3921 HTONL($$->key32[1]);
3922 HTONL($$->key32[2]);
3923 HTONL($$->key32[3]);
3929 pool_opts : { bzero(&pool_opts, sizeof pool_opts); }
3933 bzero(&pool_opts, sizeof pool_opts);
3938 pool_opts_l : pool_opts_l pool_opt
3942 pool_opt : BITMASK {
3943 if (pool_opts.type) {
3944 yyerror("pool type cannot be redefined");
3947 pool_opts.type = PF_POOL_BITMASK;
3950 if (pool_opts.type) {
3951 yyerror("pool type cannot be redefined");
3954 pool_opts.type = PF_POOL_RANDOM;
3956 | SOURCEHASH hashkey {
3957 if (pool_opts.type) {
3958 yyerror("pool type cannot be redefined");
3961 pool_opts.type = PF_POOL_SRCHASH;
3965 if (pool_opts.type) {
3966 yyerror("pool type cannot be redefined");
3969 pool_opts.type = PF_POOL_ROUNDROBIN;
3972 if (pool_opts.staticport) {
3973 yyerror("static-port cannot be redefined");
3976 pool_opts.staticport = 1;
3979 if (filter_opts.marker & POM_STICKYADDRESS) {
3980 yyerror("sticky-address cannot be redefined");
3983 pool_opts.marker |= POM_STICKYADDRESS;
3984 pool_opts.opts |= PF_POOL_STICKYADDR;
3988 redirection : /* empty */ { $$ = NULL; }
3990 $$ = calloc(1, sizeof(struct redirection));
3992 err(1, "redirection: calloc");
3994 $$->rport.a = $$->rport.b = $$->rport.t = 0;
3996 | ARROW host PORT portstar {
3997 $$ = calloc(1, sizeof(struct redirection));
3999 err(1, "redirection: calloc");
4005 natpasslog : /* empty */ { $$.b1 = $$.b2 = 0; $$.w2 = 0; }
4006 | PASS { $$.b1 = 1; $$.b2 = 0; $$.w2 = 0; }
4007 | PASS log { $$.b1 = 1; $$.b2 = $2.log; $$.w2 = $2.logif; }
4008 | log { $$.b1 = 0; $$.b2 = $1.log; $$.w2 = $1.logif; }
4011 nataction : no NAT natpasslog {
4013 yyerror("\"pass\" not valid with \"no\"");
4024 | no RDR natpasslog {
4026 yyerror("\"pass\" not valid with \"no\"");
4039 natrule : nataction interface af proto fromto tag tagged rtable
4044 if (check_rulestate(PFCTL_STATE_NAT))
4047 memset(&r, 0, sizeof(r));
4056 if ($5.src.host && $5.src.host->af &&
4057 !$5.src.host->ifindex)
4058 r.af = $5.src.host->af;
4059 else if ($5.dst.host && $5.dst.host->af &&
4060 !$5.dst.host->ifindex)
4061 r.af = $5.dst.host->af;
4065 if (strlcpy(r.tagname, $6, PF_TAG_NAME_SIZE) >=
4067 yyerror("tag too long, max %u chars",
4068 PF_TAG_NAME_SIZE - 1);
4073 if (strlcpy(r.match_tagname, $7.name,
4074 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4075 yyerror("tag too long, max %u chars",
4076 PF_TAG_NAME_SIZE - 1);
4079 r.match_tag_not = $7.neg;
4082 if (r.action == PF_NONAT || r.action == PF_NORDR) {
4084 yyerror("translation rule with 'no' "
4085 "does not need '->'");
4089 if ($9 == NULL || $9->host == NULL) {
4090 yyerror("translation rule requires '-> "
4094 if (!r.af && ! $9->host->ifindex)
4095 r.af = $9->host->af;
4097 remove_invalid_hosts(&$9->host, &r.af);
4098 if (invalid_redirect($9->host, r.af))
4100 if (check_netmask($9->host, r.af))
4103 r.rpool.proxy_port[0] = ntohs($9->rport.a);
4107 if (!$9->rport.b && $9->rport.t &&
4108 $5.dst.port != NULL) {
4109 r.rpool.proxy_port[1] =
4110 ntohs($9->rport.a) +
4112 $5.dst.port->port[1]) -
4114 $5.dst.port->port[0]));
4116 r.rpool.proxy_port[1] =
4120 r.rpool.proxy_port[1] =
4122 if (!r.rpool.proxy_port[0] &&
4123 !r.rpool.proxy_port[1]) {
4124 r.rpool.proxy_port[0] =
4125 PF_NAT_PROXY_PORT_LOW;
4126 r.rpool.proxy_port[1] =
4127 PF_NAT_PROXY_PORT_HIGH;
4128 } else if (!r.rpool.proxy_port[1])
4129 r.rpool.proxy_port[1] =
4130 r.rpool.proxy_port[0];
4136 r.rpool.opts = $10.type;
4137 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
4138 PF_POOL_NONE && ($9->host->next != NULL ||
4139 $9->host->addr.type == PF_ADDR_TABLE ||
4140 DYNIF_MULTIADDR($9->host->addr)))
4141 r.rpool.opts = PF_POOL_ROUNDROBIN;
4142 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4143 PF_POOL_ROUNDROBIN &&
4144 disallow_table($9->host, "tables are only "
4145 "supported in round-robin redirection "
4148 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4149 PF_POOL_ROUNDROBIN &&
4150 disallow_alias($9->host, "interface (%s) "
4151 "is only supported in round-robin "
4152 "redirection pools"))
4154 if ($9->host->next != NULL) {
4155 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4156 PF_POOL_ROUNDROBIN) {
4157 yyerror("only round-robin "
4158 "valid for multiple "
4159 "redirection addresses");
4165 if ($10.key != NULL)
4166 memcpy(&r.rpool.key, $10.key,
4167 sizeof(struct pf_poolhashkey));
4170 r.rpool.opts |= $10.opts;
4172 if ($10.staticport) {
4173 if (r.action != PF_NAT) {
4174 yyerror("the 'static-port' option is "
4175 "only valid with nat rules");
4178 if (r.rpool.proxy_port[0] !=
4179 PF_NAT_PROXY_PORT_LOW &&
4180 r.rpool.proxy_port[1] !=
4181 PF_NAT_PROXY_PORT_HIGH) {
4182 yyerror("the 'static-port' option can't"
4183 " be used when specifying a port"
4187 r.rpool.proxy_port[0] = 0;
4188 r.rpool.proxy_port[1] = 0;
4191 expand_rule(&r, $2, $9 == NULL ? NULL : $9->host, $4,
4192 $5.src_os, $5.src.host, $5.src.port, $5.dst.host,
4193 $5.dst.port, 0, 0, 0, "");
4198 binatrule : no BINAT natpasslog interface af proto FROM ipspec toipspec tag
4199 tagged rtable redirection
4201 struct pf_rule binat;
4202 struct pf_pooladdr *pa;
4204 if (check_rulestate(PFCTL_STATE_NAT))
4206 if (disallow_urpf_failed($9, "\"urpf-failed\" is not "
4207 "permitted as a binat destination"))
4210 memset(&binat, 0, sizeof(binat));
4213 yyerror("\"pass\" not valid with \"no\"");
4217 binat.action = PF_NOBINAT;
4219 binat.action = PF_BINAT;
4220 binat.natpass = $3.b1;
4222 binat.logif = $3.w2;
4224 if (!binat.af && $8 != NULL && $8->af)
4226 if (!binat.af && $9 != NULL && $9->af)
4229 if (!binat.af && $13 != NULL && $13->host)
4230 binat.af = $13->host->af;
4232 yyerror("address family (inet/inet6) "
4238 memcpy(binat.ifname, $4->ifname,
4239 sizeof(binat.ifname));
4240 binat.ifnot = $4->not;
4245 if (strlcpy(binat.tagname, $10,
4246 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4247 yyerror("tag too long, max %u chars",
4248 PF_TAG_NAME_SIZE - 1);
4252 if (strlcpy(binat.match_tagname, $11.name,
4253 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4254 yyerror("tag too long, max %u chars",
4255 PF_TAG_NAME_SIZE - 1);
4258 binat.match_tag_not = $11.neg;
4259 binat.rtableid = $12;
4262 binat.proto = $6->proto;
4266 if ($8 != NULL && disallow_table($8, "invalid use of "
4267 "table <%s> as the source address of a binat rule"))
4269 if ($8 != NULL && disallow_alias($8, "invalid use of "
4270 "interface (%s) as the source address of a binat "
4273 if ($13 != NULL && $13->host != NULL && disallow_table(
4274 $13->host, "invalid use of table <%s> as the "
4275 "redirect address of a binat rule"))
4277 if ($13 != NULL && $13->host != NULL && disallow_alias(
4278 $13->host, "invalid use of interface (%s) as the "
4279 "redirect address of a binat rule"))
4284 yyerror("multiple binat ip addresses");
4287 if ($8->addr.type == PF_ADDR_DYNIFTL)
4289 if ($8->af != binat.af) {
4290 yyerror("binat ip versions must match");
4293 if (check_netmask($8, binat.af))
4295 memcpy(&binat.src.addr, &$8->addr,
4296 sizeof(binat.src.addr));
4301 yyerror("multiple binat ip addresses");
4304 if ($9->af != binat.af && $9->af) {
4305 yyerror("binat ip versions must match");
4308 if (check_netmask($9, binat.af))
4310 memcpy(&binat.dst.addr, &$9->addr,
4311 sizeof(binat.dst.addr));
4312 binat.dst.neg = $9->not;
4316 if (binat.action == PF_NOBINAT) {
4318 yyerror("'no binat' rule does not need"
4323 if ($13 == NULL || $13->host == NULL) {
4324 yyerror("'binat' rule requires"
4329 remove_invalid_hosts(&$13->host, &binat.af);
4330 if (invalid_redirect($13->host, binat.af))
4332 if ($13->host->next != NULL) {
4333 yyerror("binat rule must redirect to "
4334 "a single address");
4337 if (check_netmask($13->host, binat.af))
4340 if (!PF_AZERO(&binat.src.addr.v.a.mask,
4342 !PF_AEQ(&binat.src.addr.v.a.mask,
4343 &$13->host->addr.v.a.mask, binat.af)) {
4344 yyerror("'binat' source mask and "
4345 "redirect mask must be the same");
4349 TAILQ_INIT(&binat.rpool.list);
4350 pa = calloc(1, sizeof(struct pf_pooladdr));
4352 err(1, "binat: calloc");
4353 pa->addr = $13->host->addr;
4355 TAILQ_INSERT_TAIL(&binat.rpool.list,
4361 pfctl_add_rule(pf, &binat, "");
4365 tag : /* empty */ { $$ = NULL; }
4366 | TAG STRING { $$ = $2; }
4369 tagged : /* empty */ { $$.neg = 0; $$.name = NULL; }
4370 | not TAGGED string { $$.neg = $1; $$.name = $3; }
4373 rtable : /* empty */ { $$ = -1; }
4375 if ($2 < 0 || $2 > rt_tableid_max()) {
4376 yyerror("invalid rtable id");
4383 route_host : STRING {
4384 $$ = calloc(1, sizeof(struct node_host));
4386 err(1, "route_host: calloc");
4388 set_ipmask($$, 128);
4392 | '(' STRING host ')' {
4398 route_host_list : route_host optnl { $$ = $1; }
4399 | route_host_list comma route_host optnl {
4402 if ($1->af != $3->af) {
4403 yyerror("all pool addresses must be in the "
4404 "same address family");
4407 $1->tail->next = $3;
4408 $1->tail = $3->tail;
4413 routespec : route_host { $$ = $1; }
4414 | '{' optnl route_host_list '}' { $$ = $3; }
4417 route : /* empty */ {
4423 /* backwards-compat */
4428 | ROUTETO routespec pool_opts {
4431 $$.pool_opts = $3.type | $3.opts;
4435 | REPLYTO routespec pool_opts {
4438 $$.pool_opts = $3.type | $3.opts;
4442 | DUPTO routespec pool_opts {
4445 $$.pool_opts = $3.type | $3.opts;
4451 timeout_spec : STRING NUMBER
4453 if (check_rulestate(PFCTL_STATE_OPTION)) {
4457 if ($2 < 0 || $2 > UINT_MAX) {
4458 yyerror("only positive values permitted");
4461 if (pfctl_set_timeout(pf, $1, $2, 0) != 0) {
4462 yyerror("unknown timeout %s", $1);
4469 if (check_rulestate(PFCTL_STATE_OPTION))
4471 if ($2 < 0 || $2 > UINT_MAX) {
4472 yyerror("only positive values permitted");
4475 if (pfctl_set_timeout(pf, "interval", $2, 0) != 0)
4480 timeout_list : timeout_list comma timeout_spec optnl
4481 | timeout_spec optnl
4484 limit_spec : STRING NUMBER
4486 if (check_rulestate(PFCTL_STATE_OPTION)) {
4490 if ($2 < 0 || $2 > UINT_MAX) {
4491 yyerror("only positive values permitted");
4494 if (pfctl_set_limit(pf, $1, $2) != 0) {
4495 yyerror("unable to set limit %s %u", $1, $2);
4503 limit_list : limit_list comma limit_spec optnl
4511 yesno : NO { $$ = 0; }
4513 if (!strcmp($1, "yes"))
4516 yyerror("invalid value '%s', expected 'yes' "
4525 unaryop : '=' { $$ = PF_OP_EQ; }
4526 | '!' '=' { $$ = PF_OP_NE; }
4527 | '<' '=' { $$ = PF_OP_LE; }
4528 | '<' { $$ = PF_OP_LT; }
4529 | '>' '=' { $$ = PF_OP_GE; }
4530 | '>' { $$ = PF_OP_GT; }
4536 yyerror(const char *fmt, ...)
4542 fprintf(stderr, "%s:%d: ", file->name, yylval.lineno);
4543 vfprintf(stderr, fmt, ap);
4544 fprintf(stderr, "\n");
4550 disallow_table(struct node_host *h, const char *fmt)
4552 for (; h != NULL; h = h->next)
4553 if (h->addr.type == PF_ADDR_TABLE) {
4554 yyerror(fmt, h->addr.v.tblname);
4561 disallow_urpf_failed(struct node_host *h, const char *fmt)
4563 for (; h != NULL; h = h->next)
4564 if (h->addr.type == PF_ADDR_URPFFAILED) {
4572 disallow_alias(struct node_host *h, const char *fmt)
4574 for (; h != NULL; h = h->next)
4575 if (DYNIF_MULTIADDR(h->addr)) {
4576 yyerror(fmt, h->addr.v.tblname);
4583 rule_consistent(struct pf_rule *r, int anchor_call)
4587 switch (r->action) {
4592 problems = filter_consistent(r, anchor_call);
4596 problems = nat_consistent(r);
4600 problems = rdr_consistent(r);
4611 filter_consistent(struct pf_rule *r, int anchor_call)
4615 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP &&
4616 (r->src.port_op || r->dst.port_op)) {
4617 yyerror("port only applies to tcp/udp");
4620 if (r->proto != IPPROTO_ICMP && r->proto != IPPROTO_ICMPV6 &&
4621 (r->type || r->code)) {
4622 yyerror("icmp-type/code only applies to icmp");
4625 if (!r->af && (r->type || r->code)) {
4626 yyerror("must indicate address family with icmp-type/code");
4629 if (r->overload_tblname[0] &&
4630 r->max_src_conn == 0 && r->max_src_conn_rate.seconds == 0) {
4631 yyerror("'overload' requires 'max-src-conn' "
4632 "or 'max-src-conn-rate'");
4635 if ((r->proto == IPPROTO_ICMP && r->af == AF_INET6) ||
4636 (r->proto == IPPROTO_ICMPV6 && r->af == AF_INET)) {
4637 yyerror("proto %s doesn't match address family %s",
4638 r->proto == IPPROTO_ICMP ? "icmp" : "icmp6",
4639 r->af == AF_INET ? "inet" : "inet6");
4642 if (r->allow_opts && r->action != PF_PASS) {
4643 yyerror("allow-opts can only be specified for pass rules");
4646 if (r->rule_flag & PFRULE_FRAGMENT && (r->src.port_op ||
4647 r->dst.port_op || r->flagset || r->type || r->code)) {
4648 yyerror("fragments can be filtered only on IP header fields");
4651 if (r->rule_flag & PFRULE_RETURNRST && r->proto != IPPROTO_TCP) {
4652 yyerror("return-rst can only be applied to TCP rules");
4655 if (r->max_src_nodes && !(r->rule_flag & PFRULE_RULESRCTRACK)) {
4656 yyerror("max-src-nodes requires 'source-track rule'");
4659 if (r->action == PF_DROP && r->keep_state) {
4660 yyerror("keep state on block rules doesn't make sense");
4663 if (r->rule_flag & PFRULE_STATESLOPPY &&
4664 (r->keep_state == PF_STATE_MODULATE ||
4665 r->keep_state == PF_STATE_SYNPROXY)) {
4666 yyerror("sloppy state matching cannot be used with "
4667 "synproxy state or modulate state");
4674 nat_consistent(struct pf_rule *r)
4676 return (0); /* yeah! */
4680 rdr_consistent(struct pf_rule *r)
4684 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP) {
4685 if (r->src.port_op) {
4686 yyerror("src port only applies to tcp/udp");
4689 if (r->dst.port_op) {
4690 yyerror("dst port only applies to tcp/udp");
4693 if (r->rpool.proxy_port[0]) {
4694 yyerror("rpool port only applies to tcp/udp");
4698 if (r->dst.port_op &&
4699 r->dst.port_op != PF_OP_EQ && r->dst.port_op != PF_OP_RRG) {
4700 yyerror("invalid port operator for rdr destination port");
4707 process_tabledef(char *name, struct table_opts *opts)
4709 struct pfr_buffer ab;
4710 struct node_tinit *ti;
4712 bzero(&ab, sizeof(ab));
4713 ab.pfrb_type = PFRB_ADDRS;
4714 SIMPLEQ_FOREACH(ti, &opts->init_nodes, entries) {
4716 if (pfr_buf_load(&ab, ti->file, 0, append_addr)) {
4718 yyerror("cannot load \"%s\": %s",
4719 ti->file, strerror(errno));
4721 yyerror("file \"%s\" contains bad data",
4726 if (append_addr_host(&ab, ti->host, 0, 0)) {
4727 yyerror("cannot create address buffer: %s",
4732 if (pf->opts & PF_OPT_VERBOSE)
4733 print_tabledef(name, opts->flags, opts->init_addr,
4735 if (!(pf->opts & PF_OPT_NOACTION) &&
4736 pfctl_define_table(name, opts->flags, opts->init_addr,
4737 pf->anchor->name, &ab, pf->anchor->ruleset.tticket)) {
4738 yyerror("cannot define table %s: %s", name,
4739 pfr_strerror(errno));
4755 /* macro gore, but you should've seen the prior indentation nightmare... */
4757 #define FREE_LIST(T,r) \
4760 while (node != NULL) { \
4762 node = node->next; \
4767 #define LOOP_THROUGH(T,n,r,C) \
4771 r = calloc(1, sizeof(T)); \
4773 err(1, "LOOP: calloc"); \
4777 while (n != NULL) { \
4786 expand_label_str(char *label, size_t len, const char *srch, const char *repl)
4791 if ((tmp = calloc(1, len)) == NULL)
4792 err(1, "expand_label_str: calloc");
4794 while ((q = strstr(p, srch)) != NULL) {
4796 if ((strlcat(tmp, p, len) >= len) ||
4797 (strlcat(tmp, repl, len) >= len))
4798 errx(1, "expand_label: label too long");
4802 if (strlcat(tmp, p, len) >= len)
4803 errx(1, "expand_label: label too long");
4804 strlcpy(label, tmp, len); /* always fits */
4809 expand_label_if(const char *name, char *label, size_t len, const char *ifname)
4811 if (strstr(label, name) != NULL) {
4813 expand_label_str(label, len, name, "any");
4815 expand_label_str(label, len, name, ifname);
4820 expand_label_addr(const char *name, char *label, size_t len, sa_family_t af,
4821 struct node_host *h)
4823 char tmp[64], tmp_not[66];
4825 if (strstr(label, name) != NULL) {
4826 switch (h->addr.type) {
4827 case PF_ADDR_DYNIFTL:
4828 snprintf(tmp, sizeof(tmp), "(%s)", h->addr.v.ifname);
4831 snprintf(tmp, sizeof(tmp), "<%s>", h->addr.v.tblname);
4833 case PF_ADDR_NOROUTE:
4834 snprintf(tmp, sizeof(tmp), "no-route");
4836 case PF_ADDR_URPFFAILED:
4837 snprintf(tmp, sizeof(tmp), "urpf-failed");
4839 case PF_ADDR_ADDRMASK:
4840 if (!af || (PF_AZERO(&h->addr.v.a.addr, af) &&
4841 PF_AZERO(&h->addr.v.a.mask, af)))
4842 snprintf(tmp, sizeof(tmp), "any");
4847 if (inet_ntop(af, &h->addr.v.a.addr, a,
4849 snprintf(tmp, sizeof(tmp), "?");
4851 bits = unmask(&h->addr.v.a.mask, af);
4852 if ((af == AF_INET && bits < 32) ||
4853 (af == AF_INET6 && bits < 128))
4854 snprintf(tmp, sizeof(tmp),
4857 snprintf(tmp, sizeof(tmp),
4863 snprintf(tmp, sizeof(tmp), "?");
4868 snprintf(tmp_not, sizeof(tmp_not), "! %s", tmp);
4869 expand_label_str(label, len, name, tmp_not);
4871 expand_label_str(label, len, name, tmp);
4876 expand_label_port(const char *name, char *label, size_t len,
4877 struct node_port *port)
4879 char a1[6], a2[6], op[13] = "";
4881 if (strstr(label, name) != NULL) {
4882 snprintf(a1, sizeof(a1), "%u", ntohs(port->port[0]));
4883 snprintf(a2, sizeof(a2), "%u", ntohs(port->port[1]));
4886 else if (port->op == PF_OP_IRG)
4887 snprintf(op, sizeof(op), "%s><%s", a1, a2);
4888 else if (port->op == PF_OP_XRG)
4889 snprintf(op, sizeof(op), "%s<>%s", a1, a2);
4890 else if (port->op == PF_OP_EQ)
4891 snprintf(op, sizeof(op), "%s", a1);
4892 else if (port->op == PF_OP_NE)
4893 snprintf(op, sizeof(op), "!=%s", a1);
4894 else if (port->op == PF_OP_LT)
4895 snprintf(op, sizeof(op), "<%s", a1);
4896 else if (port->op == PF_OP_LE)
4897 snprintf(op, sizeof(op), "<=%s", a1);
4898 else if (port->op == PF_OP_GT)
4899 snprintf(op, sizeof(op), ">%s", a1);
4900 else if (port->op == PF_OP_GE)
4901 snprintf(op, sizeof(op), ">=%s", a1);
4902 expand_label_str(label, len, name, op);
4907 expand_label_proto(const char *name, char *label, size_t len, u_int8_t proto)
4909 struct protoent *pe;
4912 if (strstr(label, name) != NULL) {
4913 pe = getprotobynumber(proto);
4915 expand_label_str(label, len, name, pe->p_name);
4917 snprintf(n, sizeof(n), "%u", proto);
4918 expand_label_str(label, len, name, n);
4924 expand_label_nr(const char *name, char *label, size_t len)
4928 if (strstr(label, name) != NULL) {
4929 snprintf(n, sizeof(n), "%u", pf->anchor->match);
4930 expand_label_str(label, len, name, n);
4935 expand_label(char *label, size_t len, const char *ifname, sa_family_t af,
4936 struct node_host *src_host, struct node_port *src_port,
4937 struct node_host *dst_host, struct node_port *dst_port,
4940 expand_label_if("$if", label, len, ifname);
4941 expand_label_addr("$srcaddr", label, len, af, src_host);
4942 expand_label_addr("$dstaddr", label, len, af, dst_host);
4943 expand_label_port("$srcport", label, len, src_port);
4944 expand_label_port("$dstport", label, len, dst_port);
4945 expand_label_proto("$proto", label, len, proto);
4946 expand_label_nr("$nr", label, len);
4950 expand_altq(struct pf_altq *a, struct node_if *interfaces,
4951 struct node_queue *nqueues, struct node_queue_bw bwspec,
4952 struct node_queue_opt *opts)
4954 struct pf_altq pa, pb;
4955 char qname[PF_QNAME_SIZE];
4956 struct node_queue *n;
4957 struct node_queue_bw bw;
4960 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
4961 FREE_LIST(struct node_if, interfaces);
4963 FREE_LIST(struct node_queue, nqueues);
4967 LOOP_THROUGH(struct node_if, interface, interfaces,
4968 memcpy(&pa, a, sizeof(struct pf_altq));
4969 if (strlcpy(pa.ifname, interface->ifname,
4970 sizeof(pa.ifname)) >= sizeof(pa.ifname))
4971 errx(1, "expand_altq: strlcpy");
4973 if (interface->not) {
4974 yyerror("altq on ! <interface> is not supported");
4977 if (eval_pfaltq(pf, &pa, &bwspec, opts))
4980 if (pfctl_add_altq(pf, &pa))
4983 if (pf->opts & PF_OPT_VERBOSE) {
4984 print_altq(&pf->paltq->altq, 0,
4986 if (nqueues && nqueues->tail) {
4988 LOOP_THROUGH(struct node_queue, queue,
4998 if (pa.scheduler == ALTQT_CBQ ||
4999 pa.scheduler == ALTQT_HFSC) {
5000 /* now create a root queue */
5001 memset(&pb, 0, sizeof(struct pf_altq));
5002 if (strlcpy(qname, "root_", sizeof(qname)) >=
5004 errx(1, "expand_altq: strlcpy");
5005 if (strlcat(qname, interface->ifname,
5006 sizeof(qname)) >= sizeof(qname))
5007 errx(1, "expand_altq: strlcat");
5008 if (strlcpy(pb.qname, qname,
5009 sizeof(pb.qname)) >= sizeof(pb.qname))
5010 errx(1, "expand_altq: strlcpy");
5011 if (strlcpy(pb.ifname, interface->ifname,
5012 sizeof(pb.ifname)) >= sizeof(pb.ifname))
5013 errx(1, "expand_altq: strlcpy");
5014 pb.qlimit = pa.qlimit;
5015 pb.scheduler = pa.scheduler;
5016 bw.bw_absolute = pa.ifbandwidth;
5018 if (eval_pfqueue(pf, &pb, &bw, opts))
5021 if (pfctl_add_altq(pf, &pb))
5025 LOOP_THROUGH(struct node_queue, queue, nqueues,
5026 n = calloc(1, sizeof(struct node_queue));
5028 err(1, "expand_altq: calloc");
5029 if (pa.scheduler == ALTQT_CBQ ||
5030 pa.scheduler == ALTQT_HFSC)
5031 if (strlcpy(n->parent, qname,
5032 sizeof(n->parent)) >=
5034 errx(1, "expand_altq: strlcpy");
5035 if (strlcpy(n->queue, queue->queue,
5036 sizeof(n->queue)) >= sizeof(n->queue))
5037 errx(1, "expand_altq: strlcpy");
5038 if (strlcpy(n->ifname, interface->ifname,
5039 sizeof(n->ifname)) >= sizeof(n->ifname))
5040 errx(1, "expand_altq: strlcpy");
5041 n->scheduler = pa.scheduler;
5047 queues->tail->next = n;
5053 FREE_LIST(struct node_if, interfaces);
5055 FREE_LIST(struct node_queue, nqueues);
5061 expand_queue(struct pf_altq *a, struct node_if *interfaces,
5062 struct node_queue *nqueues, struct node_queue_bw bwspec,
5063 struct node_queue_opt *opts)
5065 struct node_queue *n, *nq;
5070 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
5071 FREE_LIST(struct node_queue, nqueues);
5075 if (queues == NULL) {
5076 yyerror("queue %s has no parent", a->qname);
5077 FREE_LIST(struct node_queue, nqueues);
5081 LOOP_THROUGH(struct node_if, interface, interfaces,
5082 LOOP_THROUGH(struct node_queue, tqueue, queues,
5083 if (!strncmp(a->qname, tqueue->queue, PF_QNAME_SIZE) &&
5084 (interface->ifname[0] == 0 ||
5085 (!interface->not && !strncmp(interface->ifname,
5086 tqueue->ifname, IFNAMSIZ)) ||
5087 (interface->not && strncmp(interface->ifname,
5088 tqueue->ifname, IFNAMSIZ)))) {
5089 /* found ourself in queues */
5092 memcpy(&pa, a, sizeof(struct pf_altq));
5094 if (pa.scheduler != ALTQT_NONE &&
5095 pa.scheduler != tqueue->scheduler) {
5096 yyerror("exactly one scheduler type "
5097 "per interface allowed");
5100 pa.scheduler = tqueue->scheduler;
5102 /* scheduler dependent error checking */
5103 switch (pa.scheduler) {
5105 if (nqueues != NULL) {
5106 yyerror("priq queues cannot "
5107 "have child queues");
5110 if (bwspec.bw_absolute > 0 ||
5111 bwspec.bw_percent < 100) {
5112 yyerror("priq doesn't take "
5121 if (strlcpy(pa.ifname, tqueue->ifname,
5122 sizeof(pa.ifname)) >= sizeof(pa.ifname))
5123 errx(1, "expand_queue: strlcpy");
5124 if (strlcpy(pa.parent, tqueue->parent,
5125 sizeof(pa.parent)) >= sizeof(pa.parent))
5126 errx(1, "expand_queue: strlcpy");
5128 if (eval_pfqueue(pf, &pa, &bwspec, opts))
5131 if (pfctl_add_altq(pf, &pa))
5134 for (nq = nqueues; nq != NULL; nq = nq->next) {
5135 if (!strcmp(a->qname, nq->queue)) {
5136 yyerror("queue cannot have "
5142 sizeof(struct node_queue));
5144 err(1, "expand_queue: calloc");
5145 if (strlcpy(n->parent, a->qname,
5146 sizeof(n->parent)) >=
5148 errx(1, "expand_queue strlcpy");
5149 if (strlcpy(n->queue, nq->queue,
5150 sizeof(n->queue)) >=
5152 errx(1, "expand_queue strlcpy");
5153 if (strlcpy(n->ifname, tqueue->ifname,
5154 sizeof(n->ifname)) >=
5156 errx(1, "expand_queue strlcpy");
5157 n->scheduler = tqueue->scheduler;
5163 queues->tail->next = n;
5167 if ((pf->opts & PF_OPT_VERBOSE) && (
5168 (found == 1 && interface->ifname[0] == 0) ||
5169 (found > 0 && interface->ifname[0] != 0))) {
5170 print_queue(&pf->paltq->altq, 0,
5171 &bwspec, interface->ifname[0] != 0,
5173 if (nqueues && nqueues->tail) {
5175 LOOP_THROUGH(struct node_queue,
5188 FREE_LIST(struct node_queue, nqueues);
5189 FREE_LIST(struct node_if, interfaces);
5192 yyerror("queue %s has no parent", a->qname);
5203 expand_rule(struct pf_rule *r,
5204 struct node_if *interfaces, struct node_host *rpool_hosts,
5205 struct node_proto *protos, struct node_os *src_oses,
5206 struct node_host *src_hosts, struct node_port *src_ports,
5207 struct node_host *dst_hosts, struct node_port *dst_ports,
5208 struct node_uid *uids, struct node_gid *gids, struct node_icmp *icmp_types,
5209 const char *anchor_call)
5211 sa_family_t af = r->af;
5212 int added = 0, error = 0;
5213 char ifname[IF_NAMESIZE];
5214 char label[PF_RULE_LABEL_SIZE];
5215 char tagname[PF_TAG_NAME_SIZE];
5216 char match_tagname[PF_TAG_NAME_SIZE];
5217 struct pf_pooladdr *pa;
5218 struct node_host *h;
5219 u_int8_t flags, flagset, keep_state;
5221 if (strlcpy(label, r->label, sizeof(label)) >= sizeof(label))
5222 errx(1, "expand_rule: strlcpy");
5223 if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname))
5224 errx(1, "expand_rule: strlcpy");
5225 if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >=
5226 sizeof(match_tagname))
5227 errx(1, "expand_rule: strlcpy");
5229 flagset = r->flagset;
5230 keep_state = r->keep_state;
5232 LOOP_THROUGH(struct node_if, interface, interfaces,
5233 LOOP_THROUGH(struct node_proto, proto, protos,
5234 LOOP_THROUGH(struct node_icmp, icmp_type, icmp_types,
5235 LOOP_THROUGH(struct node_host, src_host, src_hosts,
5236 LOOP_THROUGH(struct node_port, src_port, src_ports,
5237 LOOP_THROUGH(struct node_os, src_os, src_oses,
5238 LOOP_THROUGH(struct node_host, dst_host, dst_hosts,
5239 LOOP_THROUGH(struct node_port, dst_port, dst_ports,
5240 LOOP_THROUGH(struct node_uid, uid, uids,
5241 LOOP_THROUGH(struct node_gid, gid, gids,
5244 /* for link-local IPv6 address, interface must match up */
5245 if ((r->af && src_host->af && r->af != src_host->af) ||
5246 (r->af && dst_host->af && r->af != dst_host->af) ||
5247 (src_host->af && dst_host->af &&
5248 src_host->af != dst_host->af) ||
5249 (src_host->ifindex && dst_host->ifindex &&
5250 src_host->ifindex != dst_host->ifindex) ||
5251 (src_host->ifindex && *interface->ifname &&
5252 src_host->ifindex != if_nametoindex(interface->ifname)) ||
5253 (dst_host->ifindex && *interface->ifname &&
5254 dst_host->ifindex != if_nametoindex(interface->ifname)))
5256 if (!r->af && src_host->af)
5257 r->af = src_host->af;
5258 else if (!r->af && dst_host->af)
5259 r->af = dst_host->af;
5261 if (*interface->ifname)
5262 strlcpy(r->ifname, interface->ifname,
5264 else if (if_indextoname(src_host->ifindex, ifname))
5265 strlcpy(r->ifname, ifname, sizeof(r->ifname));
5266 else if (if_indextoname(dst_host->ifindex, ifname))
5267 strlcpy(r->ifname, ifname, sizeof(r->ifname));
5269 memset(r->ifname, '\0', sizeof(r->ifname));
5271 if (strlcpy(r->label, label, sizeof(r->label)) >=
5273 errx(1, "expand_rule: strlcpy");
5274 if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >=
5276 errx(1, "expand_rule: strlcpy");
5277 if (strlcpy(r->match_tagname, match_tagname,
5278 sizeof(r->match_tagname)) >= sizeof(r->match_tagname))
5279 errx(1, "expand_rule: strlcpy");
5280 expand_label(r->label, PF_RULE_LABEL_SIZE, r->ifname, r->af,
5281 src_host, src_port, dst_host, dst_port, proto->proto);
5282 expand_label(r->tagname, PF_TAG_NAME_SIZE, r->ifname, r->af,
5283 src_host, src_port, dst_host, dst_port, proto->proto);
5284 expand_label(r->match_tagname, PF_TAG_NAME_SIZE, r->ifname,
5285 r->af, src_host, src_port, dst_host, dst_port,
5288 error += check_netmask(src_host, r->af);
5289 error += check_netmask(dst_host, r->af);
5291 r->ifnot = interface->not;
5292 r->proto = proto->proto;
5293 r->src.addr = src_host->addr;
5294 r->src.neg = src_host->not;
5295 r->src.port[0] = src_port->port[0];
5296 r->src.port[1] = src_port->port[1];
5297 r->src.port_op = src_port->op;
5298 r->dst.addr = dst_host->addr;
5299 r->dst.neg = dst_host->not;
5300 r->dst.port[0] = dst_port->port[0];
5301 r->dst.port[1] = dst_port->port[1];
5302 r->dst.port_op = dst_port->op;
5303 r->uid.op = uid->op;
5304 r->uid.uid[0] = uid->uid[0];
5305 r->uid.uid[1] = uid->uid[1];
5306 r->gid.op = gid->op;
5307 r->gid.gid[0] = gid->gid[0];
5308 r->gid.gid[1] = gid->gid[1];
5309 r->type = icmp_type->type;
5310 r->code = icmp_type->code;
5312 if ((keep_state == PF_STATE_MODULATE ||
5313 keep_state == PF_STATE_SYNPROXY) &&
5314 r->proto && r->proto != IPPROTO_TCP)
5315 r->keep_state = PF_STATE_NORMAL;
5317 r->keep_state = keep_state;
5319 if (r->proto && r->proto != IPPROTO_TCP) {
5324 r->flagset = flagset;
5326 if (icmp_type->proto && r->proto != icmp_type->proto) {
5327 yyerror("icmp-type mismatch");
5331 if (src_os && src_os->os) {
5332 r->os_fingerprint = pfctl_get_fingerprint(src_os->os);
5333 if ((pf->opts & PF_OPT_VERBOSE2) &&
5334 r->os_fingerprint == PF_OSFP_NOMATCH)
5336 "warning: unknown '%s' OS fingerprint\n",
5339 r->os_fingerprint = PF_OSFP_ANY;
5342 TAILQ_INIT(&r->rpool.list);
5343 for (h = rpool_hosts; h != NULL; h = h->next) {
5344 pa = calloc(1, sizeof(struct pf_pooladdr));
5346 err(1, "expand_rule: calloc");
5348 if (h->ifname != NULL) {
5349 if (strlcpy(pa->ifname, h->ifname,
5350 sizeof(pa->ifname)) >=
5352 errx(1, "expand_rule: strlcpy");
5355 TAILQ_INSERT_TAIL(&r->rpool.list, pa, entries);
5358 if (rule_consistent(r, anchor_call[0]) < 0 || error)
5359 yyerror("skipping rule due to errors");
5361 r->nr = pf->astack[pf->asd]->match++;
5362 pfctl_add_rule(pf, r, anchor_call);
5368 FREE_LIST(struct node_if, interfaces);
5369 FREE_LIST(struct node_proto, protos);
5370 FREE_LIST(struct node_host, src_hosts);
5371 FREE_LIST(struct node_port, src_ports);
5372 FREE_LIST(struct node_os, src_oses);
5373 FREE_LIST(struct node_host, dst_hosts);
5374 FREE_LIST(struct node_port, dst_ports);
5375 FREE_LIST(struct node_uid, uids);
5376 FREE_LIST(struct node_gid, gids);
5377 FREE_LIST(struct node_icmp, icmp_types);
5378 FREE_LIST(struct node_host, rpool_hosts);
5381 yyerror("rule expands to no valid combination");
5385 expand_skip_interface(struct node_if *interfaces)
5389 if (!interfaces || (!interfaces->next && !interfaces->not &&
5390 !strcmp(interfaces->ifname, "none"))) {
5391 if (pf->opts & PF_OPT_VERBOSE)
5392 printf("set skip on none\n");
5393 errs = pfctl_set_interface_flags(pf, "", PFI_IFLAG_SKIP, 0);
5397 if (pf->opts & PF_OPT_VERBOSE)
5398 printf("set skip on {");
5399 LOOP_THROUGH(struct node_if, interface, interfaces,
5400 if (pf->opts & PF_OPT_VERBOSE)
5401 printf(" %s", interface->ifname);
5402 if (interface->not) {
5403 yyerror("skip on ! <interface> is not supported");
5406 errs += pfctl_set_interface_flags(pf,
5407 interface->ifname, PFI_IFLAG_SKIP, 1);
5409 if (pf->opts & PF_OPT_VERBOSE)
5412 FREE_LIST(struct node_if, interfaces);
5424 check_rulestate(int desired_state)
5426 if (require_order && (rulestate > desired_state)) {
5427 yyerror("Rules must be in order: options, normalization, "
5428 "queueing, translation, filtering");
5431 rulestate = desired_state;
5436 kw_cmp(const void *k, const void *e)
5438 return (strcmp(k, ((const struct keywords *)e)->k_name));
5444 /* this has to be sorted always */
5445 static const struct keywords keywords[] = {
5447 { "allow-opts", ALLOWOPTS},
5449 { "anchor", ANCHOR},
5450 { "antispoof", ANTISPOOF},
5452 { "bandwidth", BANDWIDTH},
5454 { "binat-anchor", BINATANCHOR},
5455 { "bitmask", BITMASK},
5457 { "block-policy", BLOCKPOLICY},
5458 { "buckets", BUCKETS},
5462 { "crop", FRAGCROP},
5464 { "divert-reply", DIVERTREPLY},
5465 { "divert-to", DIVERTTO},
5467 { "drop-ovl", FRAGDROP},
5470 { "fastroute", FASTROUTE},
5471 { "file", FILENAME},
5472 { "fingerprints", FINGERPRINTS},
5474 { "floating", FLOATING},
5477 { "fragment", FRAGMENT},
5479 { "global", GLOBAL},
5483 { "hostid", HOSTID},
5484 { "icmp-type", ICMPTYPE},
5485 { "icmp6-type", ICMP6TYPE},
5486 { "if-bound", IFBOUND},
5488 { "include", INCLUDE},
5491 { "interval", INTERVAL},
5495 { "linkshare", LINKSHARE},
5498 { "loginterface", LOGINTERFACE},
5500 { "max-mss", MAXMSS},
5501 { "max-src-conn", MAXSRCCONN},
5502 { "max-src-conn-rate", MAXSRCCONNRATE},
5503 { "max-src-nodes", MAXSRCNODES},
5504 { "max-src-states", MAXSRCSTATES},
5505 { "min-ttl", MINTTL},
5506 { "modulate", MODULATE},
5508 { "nat-anchor", NATANCHOR},
5511 { "no-route", NOROUTE},
5512 { "no-sync", NOSYNC},
5514 { "optimization", OPTIMIZATION},
5517 { "overload", OVERLOAD},
5521 { "priority", PRIORITY},
5523 { "probability", PROBABILITY},
5525 { "qlimit", QLIMIT},
5528 { "random", RANDOM},
5529 { "random-id", RANDOMID},
5531 { "rdr-anchor", RDRANCHOR},
5532 { "realtime", REALTIME},
5533 { "reassemble", REASSEMBLE},
5534 { "reply-to", REPLYTO},
5535 { "require-order", REQUIREORDER},
5536 { "return", RETURN},
5537 { "return-icmp", RETURNICMP},
5538 { "return-icmp6", RETURNICMP6},
5539 { "return-rst", RETURNRST},
5540 { "round-robin", ROUNDROBIN},
5542 { "route-to", ROUTETO},
5543 { "rtable", RTABLE},
5545 { "ruleset-optimization", RULESET_OPTIMIZATION},
5548 { "set-tos", SETTOS},
5550 { "sloppy", SLOPPY},
5551 { "source-hash", SOURCEHASH},
5552 { "source-track", SOURCETRACK},
5554 { "state-defaults", STATEDEFAULTS},
5555 { "state-policy", STATEPOLICY},
5556 { "static-port", STATICPORT},
5557 { "sticky-address", STICKYADDRESS},
5558 { "synproxy", SYNPROXY},
5561 { "tagged", TAGGED},
5562 { "target", TARGET},
5563 { "tbrsize", TBRSIZE},
5564 { "timeout", TIMEOUT},
5568 { "upperlimit", UPPERLIMIT},
5569 { "urpf-failed", URPFFAILED},
5572 const struct keywords *p;
5574 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
5575 sizeof(keywords[0]), kw_cmp);
5579 fprintf(stderr, "%s: %d\n", s, p->k_val);
5583 fprintf(stderr, "string: %s\n", s);
5588 #define MAXPUSHBACK 128
5590 static char *parsebuf;
5591 static int parseindex;
5592 static char pushback_buffer[MAXPUSHBACK];
5593 static int pushback_index = 0;
5601 /* Read character from the parsebuffer instead of input. */
5602 if (parseindex >= 0) {
5603 c = parsebuf[parseindex++];
5612 return (pushback_buffer[--pushback_index]);
5615 if ((c = getc(file->stream)) == EOF) {
5616 yyerror("reached end of file while parsing quoted string");
5617 if (popfile() == EOF)
5624 while ((c = getc(file->stream)) == '\\') {
5625 next = getc(file->stream);
5630 yylval.lineno = file->lineno;
5635 if (popfile() == EOF)
5637 c = getc(file->stream);
5649 if (parseindex >= 0)
5652 if (pushback_index < MAXPUSHBACK-1)
5653 return (pushback_buffer[pushback_index++] = c);
5665 /* skip to either EOF or the first real EOL */
5668 c = pushback_buffer[--pushback_index];
5686 int quotec, next, c;
5691 while ((c = lgetc(0)) == ' ' || c == '\t')
5694 yylval.lineno = file->lineno;
5696 while ((c = lgetc(0)) != '\n' && c != EOF)
5698 if (c == '$' && parsebuf == NULL) {
5700 if ((c = lgetc(0)) == EOF)
5703 if (p + 1 >= buf + sizeof(buf) - 1) {
5704 yyerror("string too long");
5707 if (isalnum(c) || c == '_') {
5717 yyerror("macro '%s' not defined", buf);
5730 if ((c = lgetc(quotec)) == EOF)
5735 } else if (c == '\\') {
5736 if ((next = lgetc(quotec)) == EOF)
5738 if (next == quotec || c == ' ' || c == '\t')
5740 else if (next == '\n')
5744 } else if (c == quotec) {
5748 if (p + 1 >= buf + sizeof(buf) - 1) {
5749 yyerror("string too long");
5754 yylval.v.string = strdup(buf);
5755 if (yylval.v.string == NULL)
5756 err(1, "yylex: strdup");
5761 yylval.v.i = PF_OP_XRG;
5762 return (PORTBINARY);
5769 yylval.v.i = PF_OP_IRG;
5770 return (PORTBINARY);
5782 #define allowed_to_end_number(x) \
5783 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
5785 if (c == '-' || isdigit(c)) {
5788 if ((unsigned)(p-buf) >= sizeof(buf)) {
5789 yyerror("string too long");
5792 } while ((c = lgetc(0)) != EOF && isdigit(c));
5794 if (p == buf + 1 && buf[0] == '-')
5796 if (c == EOF || allowed_to_end_number(c)) {
5797 const char *errstr = NULL;
5800 yylval.v.number = strtonum(buf, LLONG_MIN,
5801 LLONG_MAX, &errstr);
5803 yyerror("\"%s\" invalid number: %s",
5818 #define allowed_in_string(x) \
5819 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
5820 x != '{' && x != '}' && x != '<' && x != '>' && \
5821 x != '!' && x != '=' && x != '/' && x != '#' && \
5824 if (isalnum(c) || c == ':' || c == '_') {
5827 if ((unsigned)(p-buf) >= sizeof(buf)) {
5828 yyerror("string too long");
5831 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
5834 if ((token = lookup(buf)) == STRING)
5835 if ((yylval.v.string = strdup(buf)) == NULL)
5836 err(1, "yylex: strdup");
5840 yylval.lineno = file->lineno;
5849 check_file_secrecy(int fd, const char *fname)
5853 if (fstat(fd, &st)) {
5854 warn("cannot stat %s", fname);
5857 if (st.st_uid != 0 && st.st_uid != getuid()) {
5858 warnx("%s: owner not root or current user", fname);
5861 if (st.st_mode & (S_IRWXG | S_IRWXO)) {
5862 warnx("%s: group/world readable/writeable", fname);
5869 pushfile(const char *name, int secret)
5873 if ((nfile = calloc(1, sizeof(struct file))) == NULL ||
5874 (nfile->name = strdup(name)) == NULL) {
5878 if (TAILQ_FIRST(&files) == NULL && strcmp(nfile->name, "-") == 0) {
5879 nfile->stream = stdin;
5881 if ((nfile->name = strdup("stdin")) == NULL) {
5886 } else if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
5887 warn("%s", nfile->name);
5891 } else if (secret &&
5892 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
5893 fclose(nfile->stream);
5899 TAILQ_INSERT_TAIL(&files, nfile, entry);
5908 if ((prev = TAILQ_PREV(file, files, entry)) != NULL) {
5909 prev->errors += file->errors;
5910 TAILQ_REMOVE(&files, file, entry);
5911 fclose(file->stream);
5921 parse_config(char *filename, struct pfctl *xpf)
5928 rulestate = PFCTL_STATE_NONE;
5929 returnicmpdefault = (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
5930 returnicmp6default =
5931 (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
5932 blockpolicy = PFRULE_DROP;
5935 if ((file = pushfile(filename, 0)) == NULL) {
5936 warn("cannot open the main config file!");
5941 errors = file->errors;
5944 /* Free macros and check which have not been used. */
5945 while ((sym = TAILQ_FIRST(&symhead))) {
5946 if ((pf->opts & PF_OPT_VERBOSE2) && !sym->used)
5947 fprintf(stderr, "warning: macro '%s' not "
5948 "used\n", sym->nam);
5951 TAILQ_REMOVE(&symhead, sym, entry);
5955 return (errors ? -1 : 0);
5959 symset(const char *nam, const char *val, int persist)
5963 for (sym = TAILQ_FIRST(&symhead); sym && strcmp(nam, sym->nam);
5964 sym = TAILQ_NEXT(sym, entry))
5968 if (sym->persist == 1)
5973 TAILQ_REMOVE(&symhead, sym, entry);
5977 if ((sym = calloc(1, sizeof(*sym))) == NULL)
5980 sym->nam = strdup(nam);
5981 if (sym->nam == NULL) {
5985 sym->val = strdup(val);
5986 if (sym->val == NULL) {
5992 sym->persist = persist;
5993 TAILQ_INSERT_TAIL(&symhead, sym, entry);
5998 pfctl_cmdline_symset(char *s)
6003 if ((val = strrchr(s, '=')) == NULL)
6006 if ((sym = malloc(strlen(s) - strlen(val) + 1)) == NULL)
6007 err(1, "pfctl_cmdline_symset: malloc");
6009 strlcpy(sym, s, strlen(s) - strlen(val) + 1);
6011 ret = symset(sym, val + 1, 1);
6018 symget(const char *nam)
6022 TAILQ_FOREACH(sym, &symhead, entry)
6023 if (strcmp(nam, sym->nam) == 0) {
6031 mv_rules(struct pf_ruleset *src, struct pf_ruleset *dst)
6036 for (i = 0; i < PF_RULESET_MAX; ++i) {
6037 while ((r = TAILQ_FIRST(src->rules[i].active.ptr))
6039 TAILQ_REMOVE(src->rules[i].active.ptr, r, entries);
6040 TAILQ_INSERT_TAIL(dst->rules[i].active.ptr, r, entries);
6041 dst->anchor->match++;
6043 src->anchor->match = 0;
6044 while ((r = TAILQ_FIRST(src->rules[i].inactive.ptr))
6046 TAILQ_REMOVE(src->rules[i].inactive.ptr, r, entries);
6047 TAILQ_INSERT_TAIL(dst->rules[i].inactive.ptr,
6054 decide_address_family(struct node_host *n, sa_family_t *af)
6056 if (*af != 0 || n == NULL)
6059 while ((n = n->next) != NULL) {
6068 remove_invalid_hosts(struct node_host **nh, sa_family_t *af)
6070 struct node_host *n = *nh, *prev = NULL;
6073 if (*af && n->af && n->af != *af) {
6074 /* unlink and free n */
6075 struct node_host *next = n->next;
6077 /* adjust tail pointer */
6078 if (n == (*nh)->tail)
6080 /* adjust previous node's next pointer */
6086 if (n->ifname != NULL)
6100 invalid_redirect(struct node_host *nh, sa_family_t af)
6103 struct node_host *n;
6105 /* tables and dyniftl are ok without an address family */
6106 for (n = nh; n != NULL; n = n->next) {
6107 if (n->addr.type != PF_ADDR_TABLE &&
6108 n->addr.type != PF_ADDR_DYNIFTL) {
6109 yyerror("address family not given and "
6110 "translation address expands to multiple "
6111 "address families");
6117 yyerror("no translation address with matching address family "
6125 atoul(char *s, u_long *ulvalp)
6131 ulval = strtoul(s, &ep, 0);
6132 if (s[0] == '\0' || *ep != '\0')
6134 if (errno == ERANGE && ulval == ULONG_MAX)
6146 if (atoul(n, &ulval) == 0) {
6147 if (ulval > 65535) {
6148 yyerror("illegal port value %lu", ulval);
6151 return (htons(ulval));
6153 s = getservbyname(n, "tcp");
6155 s = getservbyname(n, "udp");
6157 yyerror("unknown port %s", n);
6165 rule_label(struct pf_rule *r, char *s)
6168 if (strlcpy(r->label, s, sizeof(r->label)) >=
6170 yyerror("rule label too long (max %d chars)",
6171 sizeof(r->label)-1);
6179 parseicmpspec(char *w, sa_family_t af)
6181 const struct icmpcodeent *p;
6186 icmptype = returnicmpdefault >> 8;
6188 icmptype = returnicmp6default >> 8;
6190 if (atoul(w, &ulval) == -1) {
6191 if ((p = geticmpcodebyname(icmptype, w, af)) == NULL) {
6192 yyerror("unknown icmp code %s", w);
6198 yyerror("invalid icmp code %lu", ulval);
6201 return (icmptype << 8 | ulval);
6205 parseport(char *port, struct range *r, int extensions)
6207 char *p = strchr(port, ':');
6210 if ((r->a = getservice(port)) == -1)
6216 if ((extensions & PPORT_STAR) && !strcmp(p+1, "*")) {
6218 if ((r->a = getservice(port)) == -1)
6224 if ((extensions & PPORT_RANGE)) {
6226 if ((r->a = getservice(port)) == -1 ||
6227 (r->b = getservice(p)) == -1)
6240 pfctl_load_anchors(int dev, struct pfctl *pf, struct pfr_buffer *trans)
6242 struct loadanchors *la;
6244 TAILQ_FOREACH(la, &loadanchorshead, entries) {
6245 if (pf->opts & PF_OPT_VERBOSE)
6246 fprintf(stderr, "\nLoading anchor %s from %s\n",
6247 la->anchorname, la->filename);
6248 if (pfctl_rules(dev, la->filename, pf->opts, pf->optimize,
6249 la->anchorname, trans) == -1)
6257 kw_casecmp(const void *k, const void *e)
6259 return (strcasecmp(k, ((const struct keywords *)e)->k_name));
6263 map_tos(char *s, int *val)
6265 /* DiffServ Codepoints and other TOS mappings */
6266 const struct keywords toswords[] = {
6267 { "af11", IPTOS_DSCP_AF11 },
6268 { "af12", IPTOS_DSCP_AF12 },
6269 { "af13", IPTOS_DSCP_AF13 },
6270 { "af21", IPTOS_DSCP_AF21 },
6271 { "af22", IPTOS_DSCP_AF22 },
6272 { "af23", IPTOS_DSCP_AF23 },
6273 { "af31", IPTOS_DSCP_AF31 },
6274 { "af32", IPTOS_DSCP_AF32 },
6275 { "af33", IPTOS_DSCP_AF33 },
6276 { "af41", IPTOS_DSCP_AF41 },
6277 { "af42", IPTOS_DSCP_AF42 },
6278 { "af43", IPTOS_DSCP_AF43 },
6279 { "critical", IPTOS_PREC_CRITIC_ECP },
6280 { "cs0", IPTOS_DSCP_CS0 },
6281 { "cs1", IPTOS_DSCP_CS1 },
6282 { "cs2", IPTOS_DSCP_CS2 },
6283 { "cs3", IPTOS_DSCP_CS3 },
6284 { "cs4", IPTOS_DSCP_CS4 },
6285 { "cs5", IPTOS_DSCP_CS5 },
6286 { "cs6", IPTOS_DSCP_CS6 },
6287 { "cs7", IPTOS_DSCP_CS7 },
6288 { "ef", IPTOS_DSCP_EF },
6289 { "inetcontrol", IPTOS_PREC_INTERNETCONTROL },
6290 { "lowdelay", IPTOS_LOWDELAY },
6291 { "netcontrol", IPTOS_PREC_NETCONTROL },
6292 { "reliability", IPTOS_RELIABILITY },
6293 { "throughput", IPTOS_THROUGHPUT }
6295 const struct keywords *p;
6297 p = bsearch(s, toswords, sizeof(toswords)/sizeof(toswords[0]),
6298 sizeof(toswords[0]), kw_casecmp);
6308 rt_tableid_max(void)
6312 size_t l = sizeof(fibs);
6314 if (sysctlbyname("net.fibs", &fibs, &l, NULL, 0) == -1)
6315 fibs = 16; /* XXX RT_MAXFIBS, at least limit it some. */
6317 * As the OpenBSD code only compares > and not >= we need to adjust
6318 * here given we only accept values of 0..n and want to avoid #ifdefs
6323 return (RT_TABLEID_MAX);