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);
355 static TAILQ_HEAD(loadanchorshead, loadanchors)
356 loadanchorshead = TAILQ_HEAD_INITIALIZER(loadanchorshead);
359 TAILQ_ENTRY(loadanchors) entries;
382 struct node_if *interface;
383 struct node_proto *proto;
384 struct node_icmp *icmp;
385 struct node_host *host;
387 struct node_port *port;
388 struct node_uid *uid;
389 struct node_gid *gid;
390 struct node_state_opt *state_opt;
393 struct peer src, dst;
394 struct node_os *src_os;
397 struct node_host *host;
401 struct pf_poolhashkey *key;
404 struct node_host *host;
409 struct node_state_opt *options;
420 struct pf_poolhashkey *hashkey;
421 struct node_queue *queue;
422 struct node_queue_opt queue_options;
423 struct node_queue_bw queue_bwspec;
424 struct node_qassign qassign;
425 struct filter_opts filter_opts;
426 struct antispoof_opts antispoof_opts;
427 struct queue_opts queue_opts;
428 struct scrub_opts scrub_opts;
429 struct table_opts table_opts;
430 struct pool_opts pool_opts;
431 struct node_hfsc_opts hfsc_opts;
432 struct node_fairq_opts fairq_opts;
433 struct codel_opts codel_opts;
438 #define PPORT_RANGE 1
440 int parseport(char *, struct range *r, int);
442 #define DYNIF_MULTIADDR(addr) ((addr).type == PF_ADDR_DYNIFTL && \
443 (!((addr).iflags & PFI_AFLAG_NOALIAS) || \
444 !isdigit((addr).v.ifname[strlen((addr).v.ifname)-1])))
448 %token PASS BLOCK SCRUB RETURN IN OS OUT LOG QUICK ON FROM TO FLAGS
449 %token RETURNRST RETURNICMP RETURNICMP6 PROTO INET INET6 ALL ANY ICMPTYPE
450 %token ICMP6TYPE CODE KEEP MODULATE STATE PORT RDR NAT BINAT ARROW NODF
451 %token MINTTL ERROR ALLOWOPTS FASTROUTE FILENAME ROUTETO DUPTO REPLYTO NO LABEL
452 %token NOROUTE URPFFAILED FRAGMENT USER GROUP MAXMSS MAXIMUM TTL TOS DROP TABLE
453 %token REASSEMBLE FRAGDROP FRAGCROP ANCHOR NATANCHOR RDRANCHOR BINATANCHOR
454 %token SET OPTIMIZATION TIMEOUT LIMIT LOGINTERFACE BLOCKPOLICY RANDOMID
455 %token REQUIREORDER SYNPROXY FINGERPRINTS NOSYNC DEBUG SKIP HOSTID
456 %token ANTISPOOF FOR INCLUDE
457 %token BITMASK RANDOM SOURCEHASH ROUNDROBIN STATICPORT PROBABILITY
458 %token ALTQ CBQ CODEL PRIQ HFSC FAIRQ BANDWIDTH TBRSIZE LINKSHARE REALTIME
459 %token UPPERLIMIT QUEUE PRIORITY QLIMIT HOGS BUCKETS RTABLE TARGET INTERVAL
460 %token LOAD RULESET_OPTIMIZATION PRIO
461 %token STICKYADDRESS MAXSRCSTATES MAXSRCNODES SOURCETRACK GLOBAL RULE
462 %token MAXSRCCONN MAXSRCCONNRATE OVERLOAD FLUSH SLOPPY
463 %token TAGGED TAG IFBOUND FLOATING STATEPOLICY STATEDEFAULTS ROUTE SETTOS
464 %token DIVERTTO DIVERTREPLY
465 %token <v.string> STRING
466 %token <v.number> NUMBER
467 %token <v.i> PORTBINARY
468 %type <v.interface> interface if_list if_item_not if_item
469 %type <v.number> number icmptype icmp6type uid gid
470 %type <v.number> tos not yesno
471 %type <v.probability> probability
472 %type <v.i> no dir af fragcache optimizer
473 %type <v.i> sourcetrack flush unaryop statelock
474 %type <v.b> action nataction natpasslog scrubaction
475 %type <v.b> flags flag blockspec prio
476 %type <v.range> portplain portstar portrange
477 %type <v.hashkey> hashkey
478 %type <v.proto> proto proto_list proto_item
479 %type <v.number> protoval
480 %type <v.icmp> icmpspec
481 %type <v.icmp> icmp_list icmp_item
482 %type <v.icmp> icmp6_list icmp6_item
483 %type <v.number> reticmpspec reticmp6spec
484 %type <v.fromto> fromto
485 %type <v.peer> ipportspec from to
486 %type <v.host> ipspec toipspec xhost host dynaddr host_list
487 %type <v.host> redir_host_list redirspec
488 %type <v.host> route_host route_host_list routespec
489 %type <v.os> os xos os_list
490 %type <v.port> portspec port_list port_item
491 %type <v.uid> uids uid_list uid_item
492 %type <v.gid> gids gid_list gid_item
493 %type <v.route> route
494 %type <v.redirection> redirection redirpool
495 %type <v.string> label stringall tag anchorname
496 %type <v.string> string varstring numberstring
497 %type <v.keep_state> keep
498 %type <v.state_opt> state_opt_spec state_opt_list state_opt_item
499 %type <v.logquick> logquick quick log logopts logopt
500 %type <v.interface> antispoof_ifspc antispoof_iflst antispoof_if
501 %type <v.qassign> qname
502 %type <v.queue> qassign qassign_list qassign_item
503 %type <v.queue_options> scheduler
504 %type <v.number> cbqflags_list cbqflags_item
505 %type <v.number> priqflags_list priqflags_item
506 %type <v.hfsc_opts> hfscopts_list hfscopts_item hfsc_opts
507 %type <v.fairq_opts> fairqopts_list fairqopts_item fairq_opts
508 %type <v.codel_opts> codelopts_list codelopts_item codel_opts
509 %type <v.queue_bwspec> bandwidth
510 %type <v.filter_opts> filter_opts filter_opt filter_opts_l
511 %type <v.filter_opts> filter_sets filter_set filter_sets_l
512 %type <v.antispoof_opts> antispoof_opts antispoof_opt antispoof_opts_l
513 %type <v.queue_opts> queue_opts queue_opt queue_opts_l
514 %type <v.scrub_opts> scrub_opts scrub_opt scrub_opts_l
515 %type <v.table_opts> table_opts table_opt table_opts_l
516 %type <v.pool_opts> pool_opts pool_opt pool_opts_l
517 %type <v.tagged> tagged
518 %type <v.rtableid> rtable
521 ruleset : /* empty */
522 | ruleset include '\n'
524 | ruleset option '\n'
525 | ruleset scrubrule '\n'
526 | ruleset natrule '\n'
527 | ruleset binatrule '\n'
528 | ruleset pfrule '\n'
529 | ruleset anchorrule '\n'
530 | ruleset loadrule '\n'
531 | ruleset altqif '\n'
532 | ruleset queuespec '\n'
533 | ruleset varset '\n'
534 | ruleset antispoof '\n'
535 | ruleset tabledef '\n'
536 | '{' fakeanchor '}' '\n';
537 | ruleset error '\n' { file->errors++; }
540 include : INCLUDE STRING {
543 if ((nfile = pushfile($2, 0)) == NULL) {
544 yyerror("failed to include file %s", $2);
556 * apply to previouslys specified rule: must be careful to note
557 * what that is: pf or nat or binat or rdr
559 fakeanchor : fakeanchor '\n'
560 | fakeanchor anchorrule '\n'
561 | fakeanchor binatrule '\n'
562 | fakeanchor natrule '\n'
563 | fakeanchor pfrule '\n'
564 | fakeanchor error '\n'
568 if (!strcmp($1, "none"))
570 else if (!strcmp($1, "basic"))
571 $$ = PF_OPTIMIZE_BASIC;
572 else if (!strcmp($1, "profile"))
573 $$ = PF_OPTIMIZE_BASIC | PF_OPTIMIZE_PROFILE;
575 yyerror("unknown ruleset-optimization %s", $1);
581 option : SET OPTIMIZATION STRING {
582 if (check_rulestate(PFCTL_STATE_OPTION)) {
586 if (pfctl_set_optimization(pf, $3) != 0) {
587 yyerror("unknown optimization %s", $3);
593 | SET RULESET_OPTIMIZATION optimizer {
594 if (!(pf->opts & PF_OPT_OPTIMIZE)) {
595 pf->opts |= PF_OPT_OPTIMIZE;
599 | SET TIMEOUT timeout_spec
600 | SET TIMEOUT '{' optnl timeout_list '}'
601 | SET LIMIT limit_spec
602 | SET LIMIT '{' optnl limit_list '}'
603 | SET LOGINTERFACE stringall {
604 if (check_rulestate(PFCTL_STATE_OPTION)) {
608 if (pfctl_set_logif(pf, $3) != 0) {
609 yyerror("error setting loginterface %s", $3);
615 | SET HOSTID number {
616 if ($3 == 0 || $3 > UINT_MAX) {
617 yyerror("hostid must be non-zero");
620 if (pfctl_set_hostid(pf, $3) != 0) {
621 yyerror("error setting hostid %08x", $3);
625 | SET BLOCKPOLICY DROP {
626 if (pf->opts & PF_OPT_VERBOSE)
627 printf("set block-policy drop\n");
628 if (check_rulestate(PFCTL_STATE_OPTION))
630 blockpolicy = PFRULE_DROP;
632 | SET BLOCKPOLICY RETURN {
633 if (pf->opts & PF_OPT_VERBOSE)
634 printf("set block-policy return\n");
635 if (check_rulestate(PFCTL_STATE_OPTION))
637 blockpolicy = PFRULE_RETURN;
639 | SET REQUIREORDER yesno {
640 if (pf->opts & PF_OPT_VERBOSE)
641 printf("set require-order %s\n",
642 $3 == 1 ? "yes" : "no");
645 | SET FINGERPRINTS STRING {
646 if (pf->opts & PF_OPT_VERBOSE)
647 printf("set fingerprints \"%s\"\n", $3);
648 if (check_rulestate(PFCTL_STATE_OPTION)) {
652 if (!pf->anchor->name[0]) {
653 if (pfctl_file_fingerprints(pf->dev,
655 yyerror("error loading "
656 "fingerprints %s", $3);
663 | SET STATEPOLICY statelock {
664 if (pf->opts & PF_OPT_VERBOSE)
667 printf("set state-policy floating\n");
670 printf("set state-policy if-bound\n");
673 default_statelock = $3;
676 if (check_rulestate(PFCTL_STATE_OPTION)) {
680 if (pfctl_set_debug(pf, $3) != 0) {
681 yyerror("error setting debuglevel %s", $3);
687 | SET SKIP interface {
688 if (expand_skip_interface($3) != 0) {
689 yyerror("error setting skip interface(s)");
693 | SET STATEDEFAULTS state_opt_list {
694 if (keep_state_defaults != NULL) {
695 yyerror("cannot redefine state-defaults");
698 keep_state_defaults = $3;
702 stringall : STRING { $$ = $1; }
704 if (($$ = strdup("all")) == NULL) {
705 err(1, "stringall: strdup");
710 string : STRING string {
711 if (asprintf(&$$, "%s %s", $1, $2) == -1)
712 err(1, "string: asprintf");
719 varstring : numberstring varstring {
720 if (asprintf(&$$, "%s %s", $1, $2) == -1)
721 err(1, "string: asprintf");
728 numberstring : NUMBER {
730 if (asprintf(&s, "%lld", (long long)$1) == -1) {
731 yyerror("string: asprintf");
739 varset : STRING '=' varstring {
740 if (pf->opts & PF_OPT_VERBOSE)
741 printf("%s = \"%s\"\n", $1, $3);
742 if (symset($1, $3, 0) == -1)
743 err(1, "cannot store variable %s", $1);
749 anchorname : STRING { $$ = $1; }
750 | /* empty */ { $$ = NULL; }
753 pfa_anchorlist : /* empty */
754 | pfa_anchorlist '\n'
755 | pfa_anchorlist pfrule '\n'
756 | pfa_anchorlist anchorrule '\n'
761 char ta[PF_ANCHOR_NAME_SIZE];
762 struct pf_ruleset *rs;
764 /* steping into a brace anchor */
769 /* create a holding ruleset in the root */
770 snprintf(ta, PF_ANCHOR_NAME_SIZE, "_%d", pf->bn);
771 rs = pf_find_or_create_ruleset(ta);
773 err(1, "pfa_anchor: pf_find_or_create_ruleset");
774 pf->astack[pf->asd] = rs->anchor;
775 pf->anchor = rs->anchor;
776 } '\n' pfa_anchorlist '}'
778 pf->alast = pf->anchor;
780 pf->anchor = pf->astack[pf->asd];
785 anchorrule : ANCHOR anchorname dir quick interface af proto fromto
786 filter_opts pfa_anchor
789 struct node_proto *proto;
791 if (check_rulestate(PFCTL_STATE_FILTER)) {
797 if ($2 && ($2[0] == '_' || strstr($2, "/_") != NULL)) {
799 yyerror("anchor names beginning with '_' "
800 "are reserved for internal use");
804 memset(&r, 0, sizeof(r));
805 if (pf->astack[pf->asd + 1]) {
806 /* move inline rules into relative location */
808 &pf->astack[pf->asd]->ruleset,
809 $2 ? $2 : pf->alast->name);
811 if (r.anchor == NULL)
812 err(1, "anchorrule: unable to "
815 if (pf->alast != r.anchor) {
816 if (r.anchor->match) {
817 yyerror("inline anchor '%s' "
822 mv_rules(&pf->alast->ruleset,
825 pf_remove_if_empty_ruleset(&pf->alast->ruleset);
826 pf->alast = r.anchor;
829 yyerror("anchors without explicit "
830 "rules must specify a name");
838 r.rtableid = $9.rtableid;
841 if (strlcpy(r.tagname, $9.tag,
842 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
843 yyerror("tag too long, max %u chars",
844 PF_TAG_NAME_SIZE - 1);
848 if (strlcpy(r.match_tagname, $9.match_tag,
849 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
850 yyerror("tag too long, max %u chars",
851 PF_TAG_NAME_SIZE - 1);
854 r.match_tag_not = $9.match_tag_not;
855 if (rule_label(&r, $9.label))
858 r.flags = $9.flags.b1;
859 r.flagset = $9.flags.b2;
860 if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
861 yyerror("flags always false");
864 if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
865 for (proto = $7; proto != NULL &&
866 proto->proto != IPPROTO_TCP;
869 if (proto == NULL && $7 != NULL) {
870 if ($9.flags.b1 || $9.flags.b2)
872 "flags only apply to tcp");
875 "OS fingerprinting only "
883 if ($9.keep.action) {
884 yyerror("cannot specify state handling "
890 if (strlcpy(r.match_tagname, $9.match_tag,
891 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
892 yyerror("tag too long, max %u chars",
893 PF_TAG_NAME_SIZE - 1);
896 r.match_tag_not = $9.match_tag_not;
897 if ($9.marker & FOM_PRIO) {
899 r.prio = PF_PRIO_ZERO;
903 if ($9.marker & FOM_SETPRIO) {
904 r.set_prio[0] = $9.set_prio[0];
905 r.set_prio[1] = $9.set_prio[1];
906 r.scrub_flags |= PFSTATE_SETPRIO;
909 decide_address_family($8.src.host, &r.af);
910 decide_address_family($8.dst.host, &r.af);
912 expand_rule(&r, $5, NULL, $7, $8.src_os,
913 $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
914 $9.uid, $9.gid, $9.icmpspec,
915 pf->astack[pf->asd + 1] ? pf->alast->name : $2);
917 pf->astack[pf->asd + 1] = NULL;
919 | NATANCHOR string interface af proto fromto rtable {
922 if (check_rulestate(PFCTL_STATE_NAT)) {
927 memset(&r, 0, sizeof(r));
932 decide_address_family($6.src.host, &r.af);
933 decide_address_family($6.dst.host, &r.af);
935 expand_rule(&r, $3, NULL, $5, $6.src_os,
936 $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
940 | RDRANCHOR string interface af proto fromto rtable {
943 if (check_rulestate(PFCTL_STATE_NAT)) {
948 memset(&r, 0, sizeof(r));
953 decide_address_family($6.src.host, &r.af);
954 decide_address_family($6.dst.host, &r.af);
956 if ($6.src.port != NULL) {
957 yyerror("source port parameter not supported"
961 if ($6.dst.port != NULL) {
962 if ($6.dst.port->next != NULL) {
963 yyerror("destination port list "
964 "expansion not supported in "
967 } else if ($6.dst.port->op != PF_OP_EQ) {
968 yyerror("destination port operators"
969 " not supported in rdr-anchor");
972 r.dst.port[0] = $6.dst.port->port[0];
973 r.dst.port[1] = $6.dst.port->port[1];
974 r.dst.port_op = $6.dst.port->op;
977 expand_rule(&r, $3, NULL, $5, $6.src_os,
978 $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
982 | BINATANCHOR string interface af proto fromto rtable {
985 if (check_rulestate(PFCTL_STATE_NAT)) {
990 memset(&r, 0, sizeof(r));
995 if ($5->next != NULL) {
996 yyerror("proto list expansion"
997 " not supported in binat-anchor");
1000 r.proto = $5->proto;
1004 if ($6.src.host != NULL || $6.src.port != NULL ||
1005 $6.dst.host != NULL || $6.dst.port != NULL) {
1006 yyerror("fromto parameter not supported"
1007 " in binat-anchor");
1011 decide_address_family($6.src.host, &r.af);
1012 decide_address_family($6.dst.host, &r.af);
1014 pfctl_add_rule(pf, &r, $2);
1019 loadrule : LOAD ANCHOR string FROM string {
1020 struct loadanchors *loadanchor;
1022 if (strlen(pf->anchor->name) + 1 +
1023 strlen($3) >= MAXPATHLEN) {
1024 yyerror("anchorname %s too long, max %u\n",
1025 $3, MAXPATHLEN - 1);
1029 loadanchor = calloc(1, sizeof(struct loadanchors));
1030 if (loadanchor == NULL)
1031 err(1, "loadrule: calloc");
1032 if ((loadanchor->anchorname = malloc(MAXPATHLEN)) ==
1034 err(1, "loadrule: malloc");
1035 if (pf->anchor->name[0])
1036 snprintf(loadanchor->anchorname, MAXPATHLEN,
1037 "%s/%s", pf->anchor->name, $3);
1039 strlcpy(loadanchor->anchorname, $3, MAXPATHLEN);
1040 if ((loadanchor->filename = strdup($5)) == NULL)
1041 err(1, "loadrule: strdup");
1043 TAILQ_INSERT_TAIL(&loadanchorshead, loadanchor,
1050 scrubaction : no SCRUB {
1059 scrubrule : scrubaction dir logquick interface af proto fromto scrub_opts
1063 if (check_rulestate(PFCTL_STATE_SCRUB))
1066 memset(&r, 0, sizeof(r));
1074 yyerror("scrub rules do not support 'quick'");
1080 r.rule_flag |= PFRULE_NODF;
1082 r.rule_flag |= PFRULE_RANDOMID;
1083 if ($8.reassemble_tcp) {
1084 if (r.direction != PF_INOUT) {
1085 yyerror("reassemble tcp rules can not "
1086 "specify direction");
1089 r.rule_flag |= PFRULE_REASSEMBLE_TCP;
1092 r.min_ttl = $8.minttl;
1094 r.max_mss = $8.maxmss;
1095 if ($8.marker & SOM_SETTOS) {
1096 r.rule_flag |= PFRULE_SET_TOS;
1097 r.set_tos = $8.settos;
1100 r.rule_flag |= $8.fragcache;
1102 if (strlcpy(r.match_tagname, $8.match_tag,
1103 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1104 yyerror("tag too long, max %u chars",
1105 PF_TAG_NAME_SIZE - 1);
1108 r.match_tag_not = $8.match_tag_not;
1109 r.rtableid = $8.rtableid;
1111 expand_rule(&r, $4, NULL, $6, $7.src_os,
1112 $7.src.host, $7.src.port, $7.dst.host, $7.dst.port,
1113 NULL, NULL, NULL, "");
1118 bzero(&scrub_opts, sizeof scrub_opts);
1119 scrub_opts.rtableid = -1;
1122 { $$ = scrub_opts; }
1124 bzero(&scrub_opts, sizeof scrub_opts);
1125 scrub_opts.rtableid = -1;
1130 scrub_opts_l : scrub_opts_l scrub_opt
1135 if (scrub_opts.nodf) {
1136 yyerror("no-df cannot be respecified");
1139 scrub_opts.nodf = 1;
1142 if (scrub_opts.marker & SOM_MINTTL) {
1143 yyerror("min-ttl cannot be respecified");
1146 if ($2 < 0 || $2 > 255) {
1147 yyerror("illegal min-ttl value %d", $2);
1150 scrub_opts.marker |= SOM_MINTTL;
1151 scrub_opts.minttl = $2;
1154 if (scrub_opts.marker & SOM_MAXMSS) {
1155 yyerror("max-mss cannot be respecified");
1158 if ($2 < 0 || $2 > 65535) {
1159 yyerror("illegal max-mss value %d", $2);
1162 scrub_opts.marker |= SOM_MAXMSS;
1163 scrub_opts.maxmss = $2;
1166 if (scrub_opts.marker & SOM_SETTOS) {
1167 yyerror("set-tos cannot be respecified");
1170 scrub_opts.marker |= SOM_SETTOS;
1171 scrub_opts.settos = $2;
1174 if (scrub_opts.marker & SOM_FRAGCACHE) {
1175 yyerror("fragcache cannot be respecified");
1178 scrub_opts.marker |= SOM_FRAGCACHE;
1179 scrub_opts.fragcache = $1;
1181 | REASSEMBLE STRING {
1182 if (strcasecmp($2, "tcp") != 0) {
1183 yyerror("scrub reassemble supports only tcp, "
1189 if (scrub_opts.reassemble_tcp) {
1190 yyerror("reassemble tcp cannot be respecified");
1193 scrub_opts.reassemble_tcp = 1;
1196 if (scrub_opts.randomid) {
1197 yyerror("random-id cannot be respecified");
1200 scrub_opts.randomid = 1;
1203 if ($2 < 0 || $2 > rt_tableid_max()) {
1204 yyerror("invalid rtable id");
1207 scrub_opts.rtableid = $2;
1209 | not TAGGED string {
1210 scrub_opts.match_tag = $3;
1211 scrub_opts.match_tag_not = $1;
1215 fragcache : FRAGMENT REASSEMBLE { $$ = 0; /* default */ }
1216 | FRAGMENT FRAGCROP { $$ = 0; }
1217 | FRAGMENT FRAGDROP { $$ = 0; }
1220 antispoof : ANTISPOOF logquick antispoof_ifspc af antispoof_opts {
1222 struct node_host *h = NULL, *hh;
1223 struct node_if *i, *j;
1225 if (check_rulestate(PFCTL_STATE_FILTER))
1228 for (i = $3; i; i = i->next) {
1229 bzero(&r, sizeof(r));
1232 r.direction = PF_IN;
1237 if (rule_label(&r, $5.label))
1239 r.rtableid = $5.rtableid;
1240 j = calloc(1, sizeof(struct node_if));
1242 err(1, "antispoof: calloc");
1243 if (strlcpy(j->ifname, i->ifname,
1244 sizeof(j->ifname)) >= sizeof(j->ifname)) {
1246 yyerror("interface name too long");
1251 h = calloc(1, sizeof(*h));
1253 err(1, "address: calloc");
1254 h->addr.type = PF_ADDR_DYNIFTL;
1256 if (strlcpy(h->addr.v.ifname, i->ifname,
1257 sizeof(h->addr.v.ifname)) >=
1258 sizeof(h->addr.v.ifname)) {
1261 "interface name too long");
1264 hh = malloc(sizeof(*hh));
1266 err(1, "address: malloc");
1267 bcopy(h, hh, sizeof(*hh));
1268 h->addr.iflags = PFI_AFLAG_NETWORK;
1270 h = ifa_lookup(j->ifname,
1276 expand_rule(&r, j, NULL, NULL, NULL, h,
1277 NULL, NULL, NULL, NULL, NULL,
1280 if ((i->ifa_flags & IFF_LOOPBACK) == 0) {
1281 bzero(&r, sizeof(r));
1284 r.direction = PF_IN;
1289 if (rule_label(&r, $5.label))
1291 r.rtableid = $5.rtableid;
1295 h = ifa_lookup(i->ifname, 0);
1297 expand_rule(&r, NULL, NULL,
1298 NULL, NULL, h, NULL, NULL,
1299 NULL, NULL, NULL, NULL, "");
1307 antispoof_ifspc : FOR antispoof_if { $$ = $2; }
1308 | FOR '{' optnl antispoof_iflst '}' { $$ = $4; }
1311 antispoof_iflst : antispoof_if optnl { $$ = $1; }
1312 | antispoof_iflst comma antispoof_if optnl {
1313 $1->tail->next = $3;
1319 antispoof_if : if_item { $$ = $1; }
1327 bzero(&antispoof_opts, sizeof antispoof_opts);
1328 antispoof_opts.rtableid = -1;
1331 { $$ = antispoof_opts; }
1333 bzero(&antispoof_opts, sizeof antispoof_opts);
1334 antispoof_opts.rtableid = -1;
1335 $$ = antispoof_opts;
1339 antispoof_opts_l : antispoof_opts_l antispoof_opt
1343 antispoof_opt : label {
1344 if (antispoof_opts.label) {
1345 yyerror("label cannot be redefined");
1348 antispoof_opts.label = $1;
1351 if ($2 < 0 || $2 > rt_tableid_max()) {
1352 yyerror("invalid rtable id");
1355 antispoof_opts.rtableid = $2;
1359 not : '!' { $$ = 1; }
1360 | /* empty */ { $$ = 0; }
1363 tabledef : TABLE '<' STRING '>' table_opts {
1364 struct node_host *h, *nh;
1365 struct node_tinit *ti, *nti;
1367 if (strlen($3) >= PF_TABLE_NAME_SIZE) {
1368 yyerror("table name too long, max %d chars",
1369 PF_TABLE_NAME_SIZE - 1);
1373 if (pf->loadopt & PFCTL_FLAG_TABLE)
1374 if (process_tabledef($3, &$5)) {
1379 for (ti = SIMPLEQ_FIRST(&$5.init_nodes);
1380 ti != SIMPLEQ_END(&$5.init_nodes); ti = nti) {
1383 for (h = ti->host; h != NULL; h = nh) {
1387 nti = SIMPLEQ_NEXT(ti, entries);
1394 bzero(&table_opts, sizeof table_opts);
1395 SIMPLEQ_INIT(&table_opts.init_nodes);
1398 { $$ = table_opts; }
1401 bzero(&table_opts, sizeof table_opts);
1402 SIMPLEQ_INIT(&table_opts.init_nodes);
1407 table_opts_l : table_opts_l table_opt
1411 table_opt : STRING {
1412 if (!strcmp($1, "const"))
1413 table_opts.flags |= PFR_TFLAG_CONST;
1414 else if (!strcmp($1, "persist"))
1415 table_opts.flags |= PFR_TFLAG_PERSIST;
1416 else if (!strcmp($1, "counters"))
1417 table_opts.flags |= PFR_TFLAG_COUNTERS;
1419 yyerror("invalid table option '%s'", $1);
1425 | '{' optnl '}' { table_opts.init_addr = 1; }
1426 | '{' optnl host_list '}' {
1427 struct node_host *n;
1428 struct node_tinit *ti;
1430 for (n = $3; n != NULL; n = n->next) {
1431 switch (n->addr.type) {
1432 case PF_ADDR_ADDRMASK:
1435 yyerror("address ranges are not "
1436 "permitted inside tables");
1438 case PF_ADDR_DYNIFTL:
1439 yyerror("dynamic addresses are not "
1440 "permitted inside tables");
1443 yyerror("tables cannot contain tables");
1445 case PF_ADDR_NOROUTE:
1446 yyerror("\"no-route\" is not permitted "
1449 case PF_ADDR_URPFFAILED:
1450 yyerror("\"urpf-failed\" is not "
1451 "permitted inside tables");
1454 yyerror("unknown address type %d",
1459 if (!(ti = calloc(1, sizeof(*ti))))
1460 err(1, "table_opt: calloc");
1462 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1464 table_opts.init_addr = 1;
1467 struct node_tinit *ti;
1469 if (!(ti = calloc(1, sizeof(*ti))))
1470 err(1, "table_opt: calloc");
1472 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1474 table_opts.init_addr = 1;
1478 altqif : ALTQ interface queue_opts QUEUE qassign {
1481 if (check_rulestate(PFCTL_STATE_QUEUE))
1484 memset(&a, 0, sizeof(a));
1485 if ($3.scheduler.qtype == ALTQT_NONE) {
1486 yyerror("no scheduler specified!");
1489 a.scheduler = $3.scheduler.qtype;
1490 a.qlimit = $3.qlimit;
1491 a.tbrsize = $3.tbrsize;
1492 if ($5 == NULL && $3.scheduler.qtype != ALTQT_CODEL) {
1493 yyerror("no child queues specified");
1496 if (expand_altq(&a, $2, $5, $3.queue_bwspec,
1502 queuespec : QUEUE STRING interface queue_opts qassign {
1505 if (check_rulestate(PFCTL_STATE_QUEUE)) {
1510 memset(&a, 0, sizeof(a));
1512 if (strlcpy(a.qname, $2, sizeof(a.qname)) >=
1514 yyerror("queue name too long (max "
1515 "%d chars)", PF_QNAME_SIZE-1);
1521 yyerror("cannot specify tbrsize for queue");
1524 if ($4.priority > 255) {
1525 yyerror("priority out of range: max 255");
1528 a.priority = $4.priority;
1529 a.qlimit = $4.qlimit;
1530 a.scheduler = $4.scheduler.qtype;
1531 if (expand_queue(&a, $3, $5, $4.queue_bwspec,
1533 yyerror("errors in queue definition");
1540 bzero(&queue_opts, sizeof queue_opts);
1541 queue_opts.priority = DEFAULT_PRIORITY;
1542 queue_opts.qlimit = DEFAULT_QLIMIT;
1543 queue_opts.scheduler.qtype = ALTQT_NONE;
1544 queue_opts.queue_bwspec.bw_percent = 100;
1547 { $$ = queue_opts; }
1549 bzero(&queue_opts, sizeof queue_opts);
1550 queue_opts.priority = DEFAULT_PRIORITY;
1551 queue_opts.qlimit = DEFAULT_QLIMIT;
1552 queue_opts.scheduler.qtype = ALTQT_NONE;
1553 queue_opts.queue_bwspec.bw_percent = 100;
1558 queue_opts_l : queue_opts_l queue_opt
1562 queue_opt : BANDWIDTH bandwidth {
1563 if (queue_opts.marker & QOM_BWSPEC) {
1564 yyerror("bandwidth cannot be respecified");
1567 queue_opts.marker |= QOM_BWSPEC;
1568 queue_opts.queue_bwspec = $2;
1571 if (queue_opts.marker & QOM_PRIORITY) {
1572 yyerror("priority cannot be respecified");
1575 if ($2 < 0 || $2 > 255) {
1576 yyerror("priority out of range: max 255");
1579 queue_opts.marker |= QOM_PRIORITY;
1580 queue_opts.priority = $2;
1583 if (queue_opts.marker & QOM_QLIMIT) {
1584 yyerror("qlimit cannot be respecified");
1587 if ($2 < 0 || $2 > 65535) {
1588 yyerror("qlimit out of range: max 65535");
1591 queue_opts.marker |= QOM_QLIMIT;
1592 queue_opts.qlimit = $2;
1595 if (queue_opts.marker & QOM_SCHEDULER) {
1596 yyerror("scheduler cannot be respecified");
1599 queue_opts.marker |= QOM_SCHEDULER;
1600 queue_opts.scheduler = $1;
1603 if (queue_opts.marker & QOM_TBRSIZE) {
1604 yyerror("tbrsize cannot be respecified");
1607 if ($2 < 0 || $2 > 65535) {
1608 yyerror("tbrsize too big: max 65535");
1611 queue_opts.marker |= QOM_TBRSIZE;
1612 queue_opts.tbrsize = $2;
1616 bandwidth : STRING {
1622 bps = strtod($1, &cp);
1624 if (strlen(cp) > 1) {
1626 if (!strcmp(cu, "Bit") ||
1628 !strcmp(cu, "bit") ||
1633 if (!strcmp(cp, "b"))
1635 else if (!strcmp(cp, "K"))
1637 else if (!strcmp(cp, "M"))
1639 else if (!strcmp(cp, "G"))
1640 bps *= 1000 * 1000 * 1000;
1641 else if (!strcmp(cp, "%")) {
1642 if (bps < 0 || bps > 100) {
1643 yyerror("bandwidth spec "
1648 $$.bw_percent = bps;
1651 yyerror("unknown unit %s", cp);
1657 $$.bw_absolute = (u_int32_t)bps;
1660 if ($1 < 0 || $1 > UINT_MAX) {
1661 yyerror("bandwidth number too big");
1665 $$.bw_absolute = $1;
1670 $$.qtype = ALTQT_CBQ;
1671 $$.data.cbq_opts.flags = 0;
1673 | CBQ '(' cbqflags_list ')' {
1674 $$.qtype = ALTQT_CBQ;
1675 $$.data.cbq_opts.flags = $3;
1678 $$.qtype = ALTQT_PRIQ;
1679 $$.data.priq_opts.flags = 0;
1681 | PRIQ '(' priqflags_list ')' {
1682 $$.qtype = ALTQT_PRIQ;
1683 $$.data.priq_opts.flags = $3;
1686 $$.qtype = ALTQT_HFSC;
1687 bzero(&$$.data.hfsc_opts,
1688 sizeof(struct node_hfsc_opts));
1690 | HFSC '(' hfsc_opts ')' {
1691 $$.qtype = ALTQT_HFSC;
1692 $$.data.hfsc_opts = $3;
1695 $$.qtype = ALTQT_FAIRQ;
1696 bzero(&$$.data.fairq_opts,
1697 sizeof(struct node_fairq_opts));
1699 | FAIRQ '(' fairq_opts ')' {
1700 $$.qtype = ALTQT_FAIRQ;
1701 $$.data.fairq_opts = $3;
1704 $$.qtype = ALTQT_CODEL;
1705 bzero(&$$.data.codel_opts,
1706 sizeof(struct codel_opts));
1708 | CODEL '(' codel_opts ')' {
1709 $$.qtype = ALTQT_CODEL;
1710 $$.data.codel_opts = $3;
1714 cbqflags_list : cbqflags_item { $$ |= $1; }
1715 | cbqflags_list comma cbqflags_item { $$ |= $3; }
1718 cbqflags_item : STRING {
1719 if (!strcmp($1, "default"))
1720 $$ = CBQCLF_DEFCLASS;
1721 else if (!strcmp($1, "borrow"))
1723 else if (!strcmp($1, "red"))
1725 else if (!strcmp($1, "ecn"))
1726 $$ = CBQCLF_RED|CBQCLF_ECN;
1727 else if (!strcmp($1, "rio"))
1729 else if (!strcmp($1, "codel"))
1732 yyerror("unknown cbq flag \"%s\"", $1);
1740 priqflags_list : priqflags_item { $$ |= $1; }
1741 | priqflags_list comma priqflags_item { $$ |= $3; }
1744 priqflags_item : STRING {
1745 if (!strcmp($1, "default"))
1746 $$ = PRCF_DEFAULTCLASS;
1747 else if (!strcmp($1, "red"))
1749 else if (!strcmp($1, "ecn"))
1750 $$ = PRCF_RED|PRCF_ECN;
1751 else if (!strcmp($1, "rio"))
1753 else if (!strcmp($1, "codel"))
1756 yyerror("unknown priq flag \"%s\"", $1);
1766 sizeof(struct node_hfsc_opts));
1773 hfscopts_list : hfscopts_item
1774 | hfscopts_list comma hfscopts_item
1777 hfscopts_item : LINKSHARE bandwidth {
1778 if (hfsc_opts.linkshare.used) {
1779 yyerror("linkshare already specified");
1782 hfsc_opts.linkshare.m2 = $2;
1783 hfsc_opts.linkshare.used = 1;
1785 | LINKSHARE '(' bandwidth comma NUMBER comma bandwidth ')'
1787 if ($5 < 0 || $5 > INT_MAX) {
1788 yyerror("timing in curve out of range");
1791 if (hfsc_opts.linkshare.used) {
1792 yyerror("linkshare already specified");
1795 hfsc_opts.linkshare.m1 = $3;
1796 hfsc_opts.linkshare.d = $5;
1797 hfsc_opts.linkshare.m2 = $7;
1798 hfsc_opts.linkshare.used = 1;
1800 | REALTIME bandwidth {
1801 if (hfsc_opts.realtime.used) {
1802 yyerror("realtime already specified");
1805 hfsc_opts.realtime.m2 = $2;
1806 hfsc_opts.realtime.used = 1;
1808 | REALTIME '(' bandwidth comma NUMBER comma bandwidth ')'
1810 if ($5 < 0 || $5 > INT_MAX) {
1811 yyerror("timing in curve out of range");
1814 if (hfsc_opts.realtime.used) {
1815 yyerror("realtime already specified");
1818 hfsc_opts.realtime.m1 = $3;
1819 hfsc_opts.realtime.d = $5;
1820 hfsc_opts.realtime.m2 = $7;
1821 hfsc_opts.realtime.used = 1;
1823 | UPPERLIMIT bandwidth {
1824 if (hfsc_opts.upperlimit.used) {
1825 yyerror("upperlimit already specified");
1828 hfsc_opts.upperlimit.m2 = $2;
1829 hfsc_opts.upperlimit.used = 1;
1831 | UPPERLIMIT '(' bandwidth comma NUMBER comma bandwidth ')'
1833 if ($5 < 0 || $5 > INT_MAX) {
1834 yyerror("timing in curve out of range");
1837 if (hfsc_opts.upperlimit.used) {
1838 yyerror("upperlimit already specified");
1841 hfsc_opts.upperlimit.m1 = $3;
1842 hfsc_opts.upperlimit.d = $5;
1843 hfsc_opts.upperlimit.m2 = $7;
1844 hfsc_opts.upperlimit.used = 1;
1847 if (!strcmp($1, "default"))
1848 hfsc_opts.flags |= HFCF_DEFAULTCLASS;
1849 else if (!strcmp($1, "red"))
1850 hfsc_opts.flags |= HFCF_RED;
1851 else if (!strcmp($1, "ecn"))
1852 hfsc_opts.flags |= HFCF_RED|HFCF_ECN;
1853 else if (!strcmp($1, "rio"))
1854 hfsc_opts.flags |= HFCF_RIO;
1855 else if (!strcmp($1, "codel"))
1856 hfsc_opts.flags |= HFCF_CODEL;
1858 yyerror("unknown hfsc flag \"%s\"", $1);
1868 sizeof(struct node_fairq_opts));
1875 fairqopts_list : fairqopts_item
1876 | fairqopts_list comma fairqopts_item
1879 fairqopts_item : LINKSHARE bandwidth {
1880 if (fairq_opts.linkshare.used) {
1881 yyerror("linkshare already specified");
1884 fairq_opts.linkshare.m2 = $2;
1885 fairq_opts.linkshare.used = 1;
1887 | LINKSHARE '(' bandwidth number bandwidth ')' {
1888 if (fairq_opts.linkshare.used) {
1889 yyerror("linkshare already specified");
1892 fairq_opts.linkshare.m1 = $3;
1893 fairq_opts.linkshare.d = $4;
1894 fairq_opts.linkshare.m2 = $5;
1895 fairq_opts.linkshare.used = 1;
1898 fairq_opts.hogs_bw = $2;
1901 fairq_opts.nbuckets = $2;
1904 if (!strcmp($1, "default"))
1905 fairq_opts.flags |= FARF_DEFAULTCLASS;
1906 else if (!strcmp($1, "red"))
1907 fairq_opts.flags |= FARF_RED;
1908 else if (!strcmp($1, "ecn"))
1909 fairq_opts.flags |= FARF_RED|FARF_ECN;
1910 else if (!strcmp($1, "rio"))
1911 fairq_opts.flags |= FARF_RIO;
1912 else if (!strcmp($1, "codel"))
1913 fairq_opts.flags |= FARF_CODEL;
1915 yyerror("unknown fairq flag \"%s\"", $1);
1925 sizeof(struct codel_opts));
1932 codelopts_list : codelopts_item
1933 | codelopts_list comma codelopts_item
1936 codelopts_item : INTERVAL number {
1937 if (codel_opts.interval) {
1938 yyerror("interval already specified");
1941 codel_opts.interval = $2;
1944 if (codel_opts.target) {
1945 yyerror("target already specified");
1948 codel_opts.target = $2;
1951 if (!strcmp($1, "ecn"))
1954 yyerror("unknown codel option \"%s\"", $1);
1962 qassign : /* empty */ { $$ = NULL; }
1963 | qassign_item { $$ = $1; }
1964 | '{' optnl qassign_list '}' { $$ = $3; }
1967 qassign_list : qassign_item optnl { $$ = $1; }
1968 | qassign_list comma qassign_item optnl {
1969 $1->tail->next = $3;
1975 qassign_item : STRING {
1976 $$ = calloc(1, sizeof(struct node_queue));
1978 err(1, "qassign_item: calloc");
1979 if (strlcpy($$->queue, $1, sizeof($$->queue)) >=
1980 sizeof($$->queue)) {
1981 yyerror("queue name '%s' too long (max "
1982 "%d chars)", $1, sizeof($$->queue)-1);
1993 pfrule : action dir logquick interface route af proto fromto
1997 struct node_state_opt *o;
1998 struct node_proto *proto;
2004 if (check_rulestate(PFCTL_STATE_FILTER))
2007 memset(&r, 0, sizeof(r));
2011 case PFRULE_RETURNRST:
2012 r.rule_flag |= PFRULE_RETURNRST;
2013 r.return_ttl = $1.w;
2015 case PFRULE_RETURNICMP:
2016 r.rule_flag |= PFRULE_RETURNICMP;
2017 r.return_icmp = $1.w;
2018 r.return_icmp6 = $1.w2;
2021 r.rule_flag |= PFRULE_RETURN;
2022 r.return_icmp = $1.w;
2023 r.return_icmp6 = $1.w2;
2031 r.rtableid = $9.rtableid;
2033 if ($9.marker & FOM_PRIO) {
2035 r.prio = PF_PRIO_ZERO;
2039 if ($9.marker & FOM_SETPRIO) {
2040 r.set_prio[0] = $9.set_prio[0];
2041 r.set_prio[1] = $9.set_prio[1];
2042 r.scrub_flags |= PFSTATE_SETPRIO;
2047 if (strlcpy(r.tagname, $9.tag,
2048 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
2049 yyerror("tag too long, max %u chars",
2050 PF_TAG_NAME_SIZE - 1);
2054 if (strlcpy(r.match_tagname, $9.match_tag,
2055 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
2056 yyerror("tag too long, max %u chars",
2057 PF_TAG_NAME_SIZE - 1);
2060 r.match_tag_not = $9.match_tag_not;
2061 if (rule_label(&r, $9.label))
2064 r.flags = $9.flags.b1;
2065 r.flagset = $9.flags.b2;
2066 if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
2067 yyerror("flags always false");
2070 if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
2071 for (proto = $7; proto != NULL &&
2072 proto->proto != IPPROTO_TCP;
2073 proto = proto->next)
2075 if (proto == NULL && $7 != NULL) {
2076 if ($9.flags.b1 || $9.flags.b2)
2078 "flags only apply to tcp");
2081 "OS fingerprinting only "
2086 if (($9.flags.b1 & parse_flags("S")) == 0 &&
2088 yyerror("OS fingerprinting requires "
2089 "the SYN TCP flag (flags S/SA)");
2096 r.keep_state = $9.keep.action;
2097 o = $9.keep.options;
2099 /* 'keep state' by default on pass rules. */
2100 if (!r.keep_state && !r.action &&
2101 !($9.marker & FOM_KEEP)) {
2102 r.keep_state = PF_STATE_NORMAL;
2103 o = keep_state_defaults;
2108 struct node_state_opt *p = o;
2111 case PF_STATE_OPT_MAX:
2113 yyerror("state option 'max' "
2114 "multiple definitions");
2117 r.max_states = o->data.max_states;
2119 case PF_STATE_OPT_NOSYNC:
2120 if (r.rule_flag & PFRULE_NOSYNC) {
2121 yyerror("state option 'sync' "
2122 "multiple definitions");
2125 r.rule_flag |= PFRULE_NOSYNC;
2127 case PF_STATE_OPT_SRCTRACK:
2129 yyerror("state option "
2131 "multiple definitions");
2134 srctrack = o->data.src_track;
2135 r.rule_flag |= PFRULE_SRCTRACK;
2137 case PF_STATE_OPT_MAX_SRC_STATES:
2138 if (r.max_src_states) {
2139 yyerror("state option "
2141 "multiple definitions");
2144 if (o->data.max_src_states == 0) {
2145 yyerror("'max-src-states' must "
2150 o->data.max_src_states;
2151 r.rule_flag |= PFRULE_SRCTRACK;
2153 case PF_STATE_OPT_OVERLOAD:
2154 if (r.overload_tblname[0]) {
2155 yyerror("multiple 'overload' "
2156 "table definitions");
2159 if (strlcpy(r.overload_tblname,
2160 o->data.overload.tblname,
2161 PF_TABLE_NAME_SIZE) >=
2162 PF_TABLE_NAME_SIZE) {
2163 yyerror("state option: "
2167 r.flush = o->data.overload.flush;
2169 case PF_STATE_OPT_MAX_SRC_CONN:
2170 if (r.max_src_conn) {
2171 yyerror("state option "
2173 "multiple definitions");
2176 if (o->data.max_src_conn == 0) {
2177 yyerror("'max-src-conn' "
2182 o->data.max_src_conn;
2183 r.rule_flag |= PFRULE_SRCTRACK |
2184 PFRULE_RULESRCTRACK;
2186 case PF_STATE_OPT_MAX_SRC_CONN_RATE:
2187 if (r.max_src_conn_rate.limit) {
2188 yyerror("state option "
2189 "'max-src-conn-rate' "
2190 "multiple definitions");
2193 if (!o->data.max_src_conn_rate.limit ||
2194 !o->data.max_src_conn_rate.seconds) {
2195 yyerror("'max-src-conn-rate' "
2196 "values must be > 0");
2199 if (o->data.max_src_conn_rate.limit >
2201 yyerror("'max-src-conn-rate' "
2202 "maximum rate must be < %u",
2206 r.max_src_conn_rate.limit =
2207 o->data.max_src_conn_rate.limit;
2208 r.max_src_conn_rate.seconds =
2209 o->data.max_src_conn_rate.seconds;
2210 r.rule_flag |= PFRULE_SRCTRACK |
2211 PFRULE_RULESRCTRACK;
2213 case PF_STATE_OPT_MAX_SRC_NODES:
2214 if (r.max_src_nodes) {
2215 yyerror("state option "
2217 "multiple definitions");
2220 if (o->data.max_src_nodes == 0) {
2221 yyerror("'max-src-nodes' must "
2226 o->data.max_src_nodes;
2227 r.rule_flag |= PFRULE_SRCTRACK |
2228 PFRULE_RULESRCTRACK;
2230 case PF_STATE_OPT_STATELOCK:
2232 yyerror("state locking option: "
2233 "multiple definitions");
2237 r.rule_flag |= o->data.statelock;
2239 case PF_STATE_OPT_SLOPPY:
2240 if (r.rule_flag & PFRULE_STATESLOPPY) {
2241 yyerror("state sloppy option: "
2242 "multiple definitions");
2245 r.rule_flag |= PFRULE_STATESLOPPY;
2247 case PF_STATE_OPT_TIMEOUT:
2248 if (o->data.timeout.number ==
2249 PFTM_ADAPTIVE_START ||
2250 o->data.timeout.number ==
2253 if (r.timeout[o->data.timeout.number]) {
2254 yyerror("state timeout %s "
2255 "multiple definitions",
2256 pf_timeouts[o->data.
2257 timeout.number].name);
2260 r.timeout[o->data.timeout.number] =
2261 o->data.timeout.seconds;
2268 /* 'flags S/SA' by default on stateful rules */
2269 if (!r.action && !r.flags && !r.flagset &&
2270 !$9.fragment && !($9.marker & FOM_FLAGS) &&
2272 r.flags = parse_flags("S");
2273 r.flagset = parse_flags("SA");
2275 if (!adaptive && r.max_states) {
2276 r.timeout[PFTM_ADAPTIVE_START] =
2277 (r.max_states / 10) * 6;
2278 r.timeout[PFTM_ADAPTIVE_END] =
2279 (r.max_states / 10) * 12;
2281 if (r.rule_flag & PFRULE_SRCTRACK) {
2282 if (srctrack == PF_SRCTRACK_GLOBAL &&
2284 yyerror("'max-src-nodes' is "
2285 "incompatible with "
2286 "'source-track global'");
2289 if (srctrack == PF_SRCTRACK_GLOBAL &&
2291 yyerror("'max-src-conn' is "
2292 "incompatible with "
2293 "'source-track global'");
2296 if (srctrack == PF_SRCTRACK_GLOBAL &&
2297 r.max_src_conn_rate.seconds) {
2298 yyerror("'max-src-conn-rate' is "
2299 "incompatible with "
2300 "'source-track global'");
2303 if (r.timeout[PFTM_SRC_NODE] <
2304 r.max_src_conn_rate.seconds)
2305 r.timeout[PFTM_SRC_NODE] =
2306 r.max_src_conn_rate.seconds;
2307 r.rule_flag |= PFRULE_SRCTRACK;
2308 if (srctrack == PF_SRCTRACK_RULE)
2309 r.rule_flag |= PFRULE_RULESRCTRACK;
2311 if (r.keep_state && !statelock)
2312 r.rule_flag |= default_statelock;
2315 r.rule_flag |= PFRULE_FRAGMENT;
2316 r.allow_opts = $9.allowopts;
2318 decide_address_family($8.src.host, &r.af);
2319 decide_address_family($8.dst.host, &r.af);
2323 yyerror("direction must be explicit "
2324 "with rules that specify routing");
2328 r.rpool.opts = $5.pool_opts;
2330 memcpy(&r.rpool.key, $5.key,
2331 sizeof(struct pf_poolhashkey));
2333 if (r.rt && r.rt != PF_FASTROUTE) {
2334 decide_address_family($5.host, &r.af);
2335 remove_invalid_hosts(&$5.host, &r.af);
2336 if ($5.host == NULL) {
2337 yyerror("no routing address with "
2338 "matching address family found.");
2341 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
2342 PF_POOL_NONE && ($5.host->next != NULL ||
2343 $5.host->addr.type == PF_ADDR_TABLE ||
2344 DYNIF_MULTIADDR($5.host->addr)))
2345 r.rpool.opts |= PF_POOL_ROUNDROBIN;
2346 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2347 PF_POOL_ROUNDROBIN &&
2348 disallow_table($5.host, "tables are only "
2349 "supported in round-robin routing pools"))
2351 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2352 PF_POOL_ROUNDROBIN &&
2353 disallow_alias($5.host, "interface (%s) "
2354 "is only supported in round-robin "
2357 if ($5.host->next != NULL) {
2358 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2359 PF_POOL_ROUNDROBIN) {
2360 yyerror("r.rpool.opts must "
2361 "be PF_POOL_ROUNDROBIN");
2366 if ($9.queues.qname != NULL) {
2367 if (strlcpy(r.qname, $9.queues.qname,
2368 sizeof(r.qname)) >= sizeof(r.qname)) {
2369 yyerror("rule qname too long (max "
2370 "%d chars)", sizeof(r.qname)-1);
2373 free($9.queues.qname);
2375 if ($9.queues.pqname != NULL) {
2376 if (strlcpy(r.pqname, $9.queues.pqname,
2377 sizeof(r.pqname)) >= sizeof(r.pqname)) {
2378 yyerror("rule pqname too long (max "
2379 "%d chars)", sizeof(r.pqname)-1);
2382 free($9.queues.pqname);
2385 r.divert.port = $9.divert.port;
2387 if ((r.divert.port = $9.divert.port)) {
2388 if (r.direction == PF_OUT) {
2389 if ($9.divert.addr) {
2390 yyerror("address specified "
2391 "for outgoing divert");
2394 bzero(&r.divert.addr,
2395 sizeof(r.divert.addr));
2397 if (!$9.divert.addr) {
2398 yyerror("no address specified "
2399 "for incoming divert");
2402 if ($9.divert.addr->af != r.af) {
2403 yyerror("address family "
2404 "mismatch for divert");
2408 $9.divert.addr->addr.v.a.addr;
2413 expand_rule(&r, $4, $5.host, $7, $8.src_os,
2414 $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
2415 $9.uid, $9.gid, $9.icmpspec, "");
2420 bzero(&filter_opts, sizeof filter_opts);
2421 filter_opts.rtableid = -1;
2424 { $$ = filter_opts; }
2426 bzero(&filter_opts, sizeof filter_opts);
2427 filter_opts.rtableid = -1;
2432 filter_opts_l : filter_opts_l filter_opt
2436 filter_opt : USER uids {
2437 if (filter_opts.uid)
2438 $2->tail->next = filter_opts.uid;
2439 filter_opts.uid = $2;
2442 if (filter_opts.gid)
2443 $2->tail->next = filter_opts.gid;
2444 filter_opts.gid = $2;
2447 if (filter_opts.marker & FOM_FLAGS) {
2448 yyerror("flags cannot be redefined");
2451 filter_opts.marker |= FOM_FLAGS;
2452 filter_opts.flags.b1 |= $1.b1;
2453 filter_opts.flags.b2 |= $1.b2;
2454 filter_opts.flags.w |= $1.w;
2455 filter_opts.flags.w2 |= $1.w2;
2458 if (filter_opts.marker & FOM_ICMP) {
2459 yyerror("icmp-type cannot be redefined");
2462 filter_opts.marker |= FOM_ICMP;
2463 filter_opts.icmpspec = $1;
2466 if (filter_opts.marker & FOM_PRIO) {
2467 yyerror("prio cannot be redefined");
2470 if ($2 < 0 || $2 > PF_PRIO_MAX) {
2471 yyerror("prio must be 0 - %u", PF_PRIO_MAX);
2474 filter_opts.marker |= FOM_PRIO;
2475 filter_opts.prio = $2;
2478 if (filter_opts.marker & FOM_TOS) {
2479 yyerror("tos cannot be redefined");
2482 filter_opts.marker |= FOM_TOS;
2483 filter_opts.tos = $2;
2486 if (filter_opts.marker & FOM_KEEP) {
2487 yyerror("modulate or keep cannot be redefined");
2490 filter_opts.marker |= FOM_KEEP;
2491 filter_opts.keep.action = $1.action;
2492 filter_opts.keep.options = $1.options;
2495 filter_opts.fragment = 1;
2498 filter_opts.allowopts = 1;
2501 if (filter_opts.label) {
2502 yyerror("label cannot be redefined");
2505 filter_opts.label = $1;
2508 if (filter_opts.queues.qname) {
2509 yyerror("queue cannot be redefined");
2512 filter_opts.queues = $1;
2515 filter_opts.tag = $2;
2517 | not TAGGED string {
2518 filter_opts.match_tag = $3;
2519 filter_opts.match_tag_not = $1;
2521 | PROBABILITY probability {
2524 p = floor($2 * UINT_MAX + 0.5);
2525 if (p < 0.0 || p > UINT_MAX) {
2526 yyerror("invalid probability: %lf", p);
2529 filter_opts.prob = (u_int32_t)p;
2530 if (filter_opts.prob == 0)
2531 filter_opts.prob = 1;
2534 if ($2 < 0 || $2 > rt_tableid_max()) {
2535 yyerror("invalid rtable id");
2538 filter_opts.rtableid = $2;
2540 | DIVERTTO portplain {
2542 filter_opts.divert.port = $2.a;
2543 if (!filter_opts.divert.port) {
2544 yyerror("invalid divert port: %u", ntohs($2.a));
2549 | DIVERTTO STRING PORT portplain {
2551 if ((filter_opts.divert.addr = host($2)) == NULL) {
2552 yyerror("could not parse divert address: %s",
2561 filter_opts.divert.port = $4.a;
2562 if (!filter_opts.divert.port) {
2563 yyerror("invalid divert port: %u", ntohs($4.a));
2569 yyerror("divert-reply has no meaning in FreeBSD pf(4)");
2572 filter_opts.divert.port = 1; /* some random value */
2578 filter_sets : SET '(' filter_sets_l ')' { $$ = filter_opts; }
2579 | SET filter_set { $$ = filter_opts; }
2582 filter_sets_l : filter_sets_l comma filter_set
2587 if (filter_opts.marker & FOM_SETPRIO) {
2588 yyerror("prio cannot be redefined");
2591 filter_opts.marker |= FOM_SETPRIO;
2592 filter_opts.set_prio[0] = $1.b1;
2593 filter_opts.set_prio[1] = $1.b2;
2595 prio : PRIO NUMBER {
2596 if ($2 < 0 || $2 > PF_PRIO_MAX) {
2597 yyerror("prio must be 0 - %u", PF_PRIO_MAX);
2602 | PRIO '(' NUMBER comma NUMBER ')' {
2603 if ($3 < 0 || $3 > PF_PRIO_MAX ||
2604 $5 < 0 || $5 > PF_PRIO_MAX) {
2605 yyerror("prio must be 0 - %u", PF_PRIO_MAX);
2613 probability : STRING {
2615 double p = strtod($1, &e);
2622 yyerror("invalid probability: %s", $1);
2635 action : PASS { $$.b1 = PF_PASS; $$.b2 = $$.w = 0; }
2636 | BLOCK blockspec { $$ = $2; $$.b1 = PF_DROP; }
2639 blockspec : /* empty */ {
2640 $$.b2 = blockpolicy;
2641 $$.w = returnicmpdefault;
2642 $$.w2 = returnicmp6default;
2645 $$.b2 = PFRULE_DROP;
2650 $$.b2 = PFRULE_RETURNRST;
2654 | RETURNRST '(' TTL NUMBER ')' {
2655 if ($4 < 0 || $4 > 255) {
2656 yyerror("illegal ttl value %d", $4);
2659 $$.b2 = PFRULE_RETURNRST;
2664 $$.b2 = PFRULE_RETURNICMP;
2665 $$.w = returnicmpdefault;
2666 $$.w2 = returnicmp6default;
2669 $$.b2 = PFRULE_RETURNICMP;
2670 $$.w = returnicmpdefault;
2671 $$.w2 = returnicmp6default;
2673 | RETURNICMP '(' reticmpspec ')' {
2674 $$.b2 = PFRULE_RETURNICMP;
2676 $$.w2 = returnicmpdefault;
2678 | RETURNICMP6 '(' reticmp6spec ')' {
2679 $$.b2 = PFRULE_RETURNICMP;
2680 $$.w = returnicmpdefault;
2683 | RETURNICMP '(' reticmpspec comma reticmp6spec ')' {
2684 $$.b2 = PFRULE_RETURNICMP;
2689 $$.b2 = PFRULE_RETURN;
2690 $$.w = returnicmpdefault;
2691 $$.w2 = returnicmp6default;
2695 reticmpspec : STRING {
2696 if (!($$ = parseicmpspec($1, AF_INET))) {
2705 if ($1 < 0 || $1 > 255) {
2706 yyerror("invalid icmp code %lu", $1);
2709 icmptype = returnicmpdefault >> 8;
2710 $$ = (icmptype << 8 | $1);
2714 reticmp6spec : STRING {
2715 if (!($$ = parseicmpspec($1, AF_INET6))) {
2724 if ($1 < 0 || $1 > 255) {
2725 yyerror("invalid icmp code %lu", $1);
2728 icmptype = returnicmp6default >> 8;
2729 $$ = (icmptype << 8 | $1);
2733 dir : /* empty */ { $$ = PF_INOUT; }
2734 | IN { $$ = PF_IN; }
2735 | OUT { $$ = PF_OUT; }
2738 quick : /* empty */ { $$.quick = 0; }
2739 | QUICK { $$.quick = 1; }
2742 logquick : /* empty */ { $$.log = 0; $$.quick = 0; $$.logif = 0; }
2743 | log { $$ = $1; $$.quick = 0; }
2744 | QUICK { $$.quick = 1; $$.log = 0; $$.logif = 0; }
2745 | log QUICK { $$ = $1; $$.quick = 1; }
2746 | QUICK log { $$ = $2; $$.quick = 1; }
2749 log : LOG { $$.log = PF_LOG; $$.logif = 0; }
2750 | LOG '(' logopts ')' {
2751 $$.log = PF_LOG | $3.log;
2752 $$.logif = $3.logif;
2756 logopts : logopt { $$ = $1; }
2757 | logopts comma logopt {
2758 $$.log = $1.log | $3.log;
2759 $$.logif = $3.logif;
2761 $$.logif = $1.logif;
2765 logopt : ALL { $$.log = PF_LOG_ALL; $$.logif = 0; }
2766 | USER { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
2767 | GROUP { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
2773 if (strncmp($2, "pflog", 5)) {
2774 yyerror("%s: should be a pflog interface", $2);
2778 i = strtonum($2 + 5, 0, 255, &errstr);
2780 yyerror("%s: %s", $2, errstr);
2789 interface : /* empty */ { $$ = NULL; }
2790 | ON if_item_not { $$ = $2; }
2791 | ON '{' optnl if_list '}' { $$ = $4; }
2794 if_list : if_item_not optnl { $$ = $1; }
2795 | if_list comma if_item_not optnl {
2796 $1->tail->next = $3;
2802 if_item_not : not if_item { $$ = $2; $$->not = $1; }
2806 struct node_host *n;
2808 $$ = calloc(1, sizeof(struct node_if));
2810 err(1, "if_item: calloc");
2811 if (strlcpy($$->ifname, $1, sizeof($$->ifname)) >=
2812 sizeof($$->ifname)) {
2815 yyerror("interface name too long");
2819 if ((n = ifa_exists($1)) != NULL)
2820 $$->ifa_flags = n->ifa_flags;
2829 af : /* empty */ { $$ = 0; }
2830 | INET { $$ = AF_INET; }
2831 | INET6 { $$ = AF_INET6; }
2834 proto : /* empty */ { $$ = NULL; }
2835 | PROTO proto_item { $$ = $2; }
2836 | PROTO '{' optnl proto_list '}' { $$ = $4; }
2839 proto_list : proto_item optnl { $$ = $1; }
2840 | proto_list comma proto_item optnl {
2841 $1->tail->next = $3;
2847 proto_item : protoval {
2852 yyerror("proto 0 cannot be used");
2855 $$ = calloc(1, sizeof(struct node_proto));
2857 err(1, "proto_item: calloc");
2867 p = getprotobyname($1);
2869 yyerror("unknown protocol %s", $1);
2877 if ($1 < 0 || $1 > 255) {
2878 yyerror("protocol outside range");
2898 os : /* empty */ { $$ = NULL; }
2899 | OS xos { $$ = $2; }
2900 | OS '{' optnl os_list '}' { $$ = $4; }
2904 $$ = calloc(1, sizeof(struct node_os));
2906 err(1, "os: calloc");
2912 os_list : xos optnl { $$ = $1; }
2913 | os_list comma xos optnl {
2914 $1->tail->next = $3;
2920 from : /* empty */ {
2934 if (disallow_urpf_failed($2.host, "\"urpf-failed\" is "
2935 "not permitted in a destination address"))
2941 ipportspec : ipspec {
2945 | ipspec PORT portspec {
2959 ipspec : ANY { $$ = NULL; }
2960 | xhost { $$ = $1; }
2961 | '{' optnl host_list '}' { $$ = $3; }
2964 toipspec : TO ipspec { $$ = $2; }
2965 | /* empty */ { $$ = NULL; }
2968 host_list : ipspec optnl { $$ = $1; }
2969 | host_list comma ipspec optnl {
2972 else if ($1 == NULL)
2975 $1->tail->next = $3;
2976 $1->tail = $3->tail;
2983 struct node_host *n;
2985 for (n = $2; n != NULL; n = n->next)
2990 $$ = calloc(1, sizeof(struct node_host));
2992 err(1, "xhost: calloc");
2993 $$->addr.type = PF_ADDR_NOROUTE;
2999 $$ = calloc(1, sizeof(struct node_host));
3001 err(1, "xhost: calloc");
3002 $$->addr.type = PF_ADDR_URPFFAILED;
3010 if (($$ = host($1)) == NULL) {
3011 /* error. "any" is handled elsewhere */
3013 yyerror("could not parse host specification");
3019 | STRING '-' STRING {
3020 struct node_host *b, *e;
3022 if ((b = host($1)) == NULL || (e = host($3)) == NULL) {
3025 yyerror("could not parse host specification");
3028 if (b->af != e->af ||
3029 b->addr.type != PF_ADDR_ADDRMASK ||
3030 e->addr.type != PF_ADDR_ADDRMASK ||
3031 unmask(&b->addr.v.a.mask, b->af) !=
3032 (b->af == AF_INET ? 32 : 128) ||
3033 unmask(&e->addr.v.a.mask, e->af) !=
3034 (e->af == AF_INET ? 32 : 128) ||
3035 b->next != NULL || b->not ||
3036 e->next != NULL || e->not) {
3041 yyerror("invalid address range");
3044 memcpy(&b->addr.v.a.mask, &e->addr.v.a.addr,
3045 sizeof(b->addr.v.a.mask));
3046 b->addr.type = PF_ADDR_RANGE;
3052 | STRING '/' NUMBER {
3055 if (asprintf(&buf, "%s/%lld", $1, (long long)$3) == -1)
3056 err(1, "host: asprintf");
3058 if (($$ = host(buf)) == NULL) {
3059 /* error. "any" is handled elsewhere */
3061 yyerror("could not parse host specification");
3066 | NUMBER '/' NUMBER {
3069 /* ie. for 10/8 parsing */
3071 if (asprintf(&buf, "%lld/%lld", (long long)$1, (long long)$3) == -1)
3073 if (asprintf(&buf, "%lld/%lld", $1, $3) == -1)
3075 err(1, "host: asprintf");
3076 if (($$ = host(buf)) == NULL) {
3077 /* error. "any" is handled elsewhere */
3079 yyerror("could not parse host specification");
3085 | dynaddr '/' NUMBER {
3086 struct node_host *n;
3088 if ($3 < 0 || $3 > 128) {
3089 yyerror("bit number too big");
3093 for (n = $1; n != NULL; n = n->next)
3097 if (strlen($2) >= PF_TABLE_NAME_SIZE) {
3098 yyerror("table name '%s' too long", $2);
3102 $$ = calloc(1, sizeof(struct node_host));
3104 err(1, "host: calloc");
3105 $$->addr.type = PF_ADDR_TABLE;
3106 if (strlcpy($$->addr.v.tblname, $2,
3107 sizeof($$->addr.v.tblname)) >=
3108 sizeof($$->addr.v.tblname))
3109 errx(1, "host: strlcpy");
3120 if (atoul($1, &ulval) == -1) {
3121 yyerror("%s is not a number", $1);
3130 dynaddr : '(' STRING ')' {
3135 if (!isalpha(op[0])) {
3136 yyerror("invalid interface name '%s'", op);
3140 while ((p = strrchr($2, ':')) != NULL) {
3141 if (!strcmp(p+1, "network"))
3142 flags |= PFI_AFLAG_NETWORK;
3143 else if (!strcmp(p+1, "broadcast"))
3144 flags |= PFI_AFLAG_BROADCAST;
3145 else if (!strcmp(p+1, "peer"))
3146 flags |= PFI_AFLAG_PEER;
3147 else if (!strcmp(p+1, "0"))
3148 flags |= PFI_AFLAG_NOALIAS;
3150 yyerror("interface %s has bad modifier",
3157 if (flags & (flags - 1) & PFI_AFLAG_MODEMASK) {
3159 yyerror("illegal combination of "
3160 "interface modifiers");
3163 $$ = calloc(1, sizeof(struct node_host));
3165 err(1, "address: calloc");
3167 set_ipmask($$, 128);
3168 $$->addr.type = PF_ADDR_DYNIFTL;
3169 $$->addr.iflags = flags;
3170 if (strlcpy($$->addr.v.ifname, $2,
3171 sizeof($$->addr.v.ifname)) >=
3172 sizeof($$->addr.v.ifname)) {
3175 yyerror("interface name too long");
3184 portspec : port_item { $$ = $1; }
3185 | '{' optnl port_list '}' { $$ = $3; }
3188 port_list : port_item optnl { $$ = $1; }
3189 | port_list comma port_item optnl {
3190 $1->tail->next = $3;
3196 port_item : portrange {
3197 $$ = calloc(1, sizeof(struct node_port));
3199 err(1, "port_item: calloc");
3209 | unaryop portrange {
3211 yyerror("':' cannot be used with an other "
3215 $$ = calloc(1, sizeof(struct node_port));
3217 err(1, "port_item: calloc");
3224 | portrange PORTBINARY portrange {
3226 yyerror("':' cannot be used with an other "
3230 $$ = calloc(1, sizeof(struct node_port));
3232 err(1, "port_item: calloc");
3241 portplain : numberstring {
3242 if (parseport($1, &$$, 0) == -1) {
3250 portrange : numberstring {
3251 if (parseport($1, &$$, PPORT_RANGE) == -1) {
3259 uids : uid_item { $$ = $1; }
3260 | '{' optnl uid_list '}' { $$ = $3; }
3263 uid_list : uid_item optnl { $$ = $1; }
3264 | uid_list comma uid_item optnl {
3265 $1->tail->next = $3;
3272 $$ = calloc(1, sizeof(struct node_uid));
3274 err(1, "uid_item: calloc");
3282 if ($2 == UID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
3283 yyerror("user unknown requires operator = or "
3287 $$ = calloc(1, sizeof(struct node_uid));
3289 err(1, "uid_item: calloc");
3296 | uid PORTBINARY uid {
3297 if ($1 == UID_MAX || $3 == UID_MAX) {
3298 yyerror("user unknown requires operator = or "
3302 $$ = calloc(1, sizeof(struct node_uid));
3304 err(1, "uid_item: calloc");
3314 if (!strcmp($1, "unknown"))
3319 if ((pw = getpwnam($1)) == NULL) {
3320 yyerror("unknown user %s", $1);
3329 if ($1 < 0 || $1 >= UID_MAX) {
3330 yyerror("illegal uid value %lu", $1);
3337 gids : gid_item { $$ = $1; }
3338 | '{' optnl gid_list '}' { $$ = $3; }
3341 gid_list : gid_item optnl { $$ = $1; }
3342 | gid_list comma gid_item optnl {
3343 $1->tail->next = $3;
3350 $$ = calloc(1, sizeof(struct node_gid));
3352 err(1, "gid_item: calloc");
3360 if ($2 == GID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
3361 yyerror("group unknown requires operator = or "
3365 $$ = calloc(1, sizeof(struct node_gid));
3367 err(1, "gid_item: calloc");
3374 | gid PORTBINARY gid {
3375 if ($1 == GID_MAX || $3 == GID_MAX) {
3376 yyerror("group unknown requires operator = or "
3380 $$ = calloc(1, sizeof(struct node_gid));
3382 err(1, "gid_item: calloc");
3392 if (!strcmp($1, "unknown"))
3397 if ((grp = getgrnam($1)) == NULL) {
3398 yyerror("unknown group %s", $1);
3407 if ($1 < 0 || $1 >= GID_MAX) {
3408 yyerror("illegal gid value %lu", $1);
3418 if ((f = parse_flags($1)) < 0) {
3419 yyerror("bad flags %s", $1);
3428 flags : FLAGS flag '/' flag { $$.b1 = $2.b1; $$.b2 = $4.b1; }
3429 | FLAGS '/' flag { $$.b1 = 0; $$.b2 = $3.b1; }
3430 | FLAGS ANY { $$.b1 = 0; $$.b2 = 0; }
3433 icmpspec : ICMPTYPE icmp_item { $$ = $2; }
3434 | ICMPTYPE '{' optnl icmp_list '}' { $$ = $4; }
3435 | ICMP6TYPE icmp6_item { $$ = $2; }
3436 | ICMP6TYPE '{' optnl icmp6_list '}' { $$ = $4; }
3439 icmp_list : icmp_item optnl { $$ = $1; }
3440 | icmp_list comma icmp_item optnl {
3441 $1->tail->next = $3;
3447 icmp6_list : icmp6_item optnl { $$ = $1; }
3448 | icmp6_list comma icmp6_item optnl {
3449 $1->tail->next = $3;
3455 icmp_item : icmptype {
3456 $$ = calloc(1, sizeof(struct node_icmp));
3458 err(1, "icmp_item: calloc");
3461 $$->proto = IPPROTO_ICMP;
3465 | icmptype CODE STRING {
3466 const struct icmpcodeent *p;
3468 if ((p = geticmpcodebyname($1-1, $3, AF_INET)) == NULL) {
3469 yyerror("unknown icmp-code %s", $3);
3475 $$ = calloc(1, sizeof(struct node_icmp));
3477 err(1, "icmp_item: calloc");
3479 $$->code = p->code + 1;
3480 $$->proto = IPPROTO_ICMP;
3484 | icmptype CODE NUMBER {
3485 if ($3 < 0 || $3 > 255) {
3486 yyerror("illegal icmp-code %lu", $3);
3489 $$ = calloc(1, sizeof(struct node_icmp));
3491 err(1, "icmp_item: calloc");
3494 $$->proto = IPPROTO_ICMP;
3500 icmp6_item : icmp6type {
3501 $$ = calloc(1, sizeof(struct node_icmp));
3503 err(1, "icmp_item: calloc");
3506 $$->proto = IPPROTO_ICMPV6;
3510 | icmp6type CODE STRING {
3511 const struct icmpcodeent *p;
3513 if ((p = geticmpcodebyname($1-1, $3, AF_INET6)) == NULL) {
3514 yyerror("unknown icmp6-code %s", $3);
3520 $$ = calloc(1, sizeof(struct node_icmp));
3522 err(1, "icmp_item: calloc");
3524 $$->code = p->code + 1;
3525 $$->proto = IPPROTO_ICMPV6;
3529 | icmp6type CODE NUMBER {
3530 if ($3 < 0 || $3 > 255) {
3531 yyerror("illegal icmp-code %lu", $3);
3534 $$ = calloc(1, sizeof(struct node_icmp));
3536 err(1, "icmp_item: calloc");
3539 $$->proto = IPPROTO_ICMPV6;
3546 const struct icmptypeent *p;
3548 if ((p = geticmptypebyname($1, AF_INET)) == NULL) {
3549 yyerror("unknown icmp-type %s", $1);
3557 if ($1 < 0 || $1 > 255) {
3558 yyerror("illegal icmp-type %lu", $1);
3565 icmp6type : STRING {
3566 const struct icmptypeent *p;
3568 if ((p = geticmptypebyname($1, AF_INET6)) ==
3570 yyerror("unknown icmp6-type %s", $1);
3578 if ($1 < 0 || $1 > 255) {
3579 yyerror("illegal icmp6-type %lu", $1);
3587 if (!strcmp($1, "lowdelay"))
3588 $$ = IPTOS_LOWDELAY;
3589 else if (!strcmp($1, "throughput"))
3590 $$ = IPTOS_THROUGHPUT;
3591 else if (!strcmp($1, "reliability"))
3592 $$ = IPTOS_RELIABILITY;
3593 else if ($1[0] == '0' && $1[1] == 'x')
3594 $$ = strtoul($1, NULL, 16);
3596 $$ = 256; /* flag bad argument */
3597 if ($$ < 0 || $$ > 255) {
3598 yyerror("illegal tos value %s", $1);
3606 if ($$ < 0 || $$ > 255) {
3607 yyerror("illegal tos value %s", $1);
3613 sourcetrack : SOURCETRACK { $$ = PF_SRCTRACK; }
3614 | SOURCETRACK GLOBAL { $$ = PF_SRCTRACK_GLOBAL; }
3615 | SOURCETRACK RULE { $$ = PF_SRCTRACK_RULE; }
3618 statelock : IFBOUND {
3619 $$ = PFRULE_IFBOUND;
3630 | KEEP STATE state_opt_spec {
3631 $$.action = PF_STATE_NORMAL;
3634 | MODULATE STATE state_opt_spec {
3635 $$.action = PF_STATE_MODULATE;
3638 | SYNPROXY STATE state_opt_spec {
3639 $$.action = PF_STATE_SYNPROXY;
3644 flush : /* empty */ { $$ = 0; }
3645 | FLUSH { $$ = PF_FLUSH; }
3647 $$ = PF_FLUSH | PF_FLUSH_GLOBAL;
3651 state_opt_spec : '(' state_opt_list ')' { $$ = $2; }
3652 | /* empty */ { $$ = NULL; }
3655 state_opt_list : state_opt_item { $$ = $1; }
3656 | state_opt_list comma state_opt_item {
3657 $1->tail->next = $3;
3663 state_opt_item : MAXIMUM NUMBER {
3664 if ($2 < 0 || $2 > UINT_MAX) {
3665 yyerror("only positive values permitted");
3668 $$ = calloc(1, sizeof(struct node_state_opt));
3670 err(1, "state_opt_item: calloc");
3671 $$->type = PF_STATE_OPT_MAX;
3672 $$->data.max_states = $2;
3677 $$ = calloc(1, sizeof(struct node_state_opt));
3679 err(1, "state_opt_item: calloc");
3680 $$->type = PF_STATE_OPT_NOSYNC;
3684 | MAXSRCSTATES NUMBER {
3685 if ($2 < 0 || $2 > UINT_MAX) {
3686 yyerror("only positive values permitted");
3689 $$ = calloc(1, sizeof(struct node_state_opt));
3691 err(1, "state_opt_item: calloc");
3692 $$->type = PF_STATE_OPT_MAX_SRC_STATES;
3693 $$->data.max_src_states = $2;
3697 | MAXSRCCONN NUMBER {
3698 if ($2 < 0 || $2 > UINT_MAX) {
3699 yyerror("only positive values permitted");
3702 $$ = calloc(1, sizeof(struct node_state_opt));
3704 err(1, "state_opt_item: calloc");
3705 $$->type = PF_STATE_OPT_MAX_SRC_CONN;
3706 $$->data.max_src_conn = $2;
3710 | MAXSRCCONNRATE NUMBER '/' NUMBER {
3711 if ($2 < 0 || $2 > UINT_MAX ||
3712 $4 < 0 || $4 > UINT_MAX) {
3713 yyerror("only positive values permitted");
3716 $$ = calloc(1, sizeof(struct node_state_opt));
3718 err(1, "state_opt_item: calloc");
3719 $$->type = PF_STATE_OPT_MAX_SRC_CONN_RATE;
3720 $$->data.max_src_conn_rate.limit = $2;
3721 $$->data.max_src_conn_rate.seconds = $4;
3725 | OVERLOAD '<' STRING '>' flush {
3726 if (strlen($3) >= PF_TABLE_NAME_SIZE) {
3727 yyerror("table name '%s' too long", $3);
3731 $$ = calloc(1, sizeof(struct node_state_opt));
3733 err(1, "state_opt_item: calloc");
3734 if (strlcpy($$->data.overload.tblname, $3,
3735 PF_TABLE_NAME_SIZE) >= PF_TABLE_NAME_SIZE)
3736 errx(1, "state_opt_item: strlcpy");
3738 $$->type = PF_STATE_OPT_OVERLOAD;
3739 $$->data.overload.flush = $5;
3743 | MAXSRCNODES NUMBER {
3744 if ($2 < 0 || $2 > UINT_MAX) {
3745 yyerror("only positive values permitted");
3748 $$ = calloc(1, sizeof(struct node_state_opt));
3750 err(1, "state_opt_item: calloc");
3751 $$->type = PF_STATE_OPT_MAX_SRC_NODES;
3752 $$->data.max_src_nodes = $2;
3757 $$ = calloc(1, sizeof(struct node_state_opt));
3759 err(1, "state_opt_item: calloc");
3760 $$->type = PF_STATE_OPT_SRCTRACK;
3761 $$->data.src_track = $1;
3766 $$ = calloc(1, sizeof(struct node_state_opt));
3768 err(1, "state_opt_item: calloc");
3769 $$->type = PF_STATE_OPT_STATELOCK;
3770 $$->data.statelock = $1;
3775 $$ = calloc(1, sizeof(struct node_state_opt));
3777 err(1, "state_opt_item: calloc");
3778 $$->type = PF_STATE_OPT_SLOPPY;
3785 if ($2 < 0 || $2 > UINT_MAX) {
3786 yyerror("only positive values permitted");
3789 for (i = 0; pf_timeouts[i].name &&
3790 strcmp(pf_timeouts[i].name, $1); ++i)
3792 if (!pf_timeouts[i].name) {
3793 yyerror("illegal timeout name %s", $1);
3797 if (strchr(pf_timeouts[i].name, '.') == NULL) {
3798 yyerror("illegal state timeout %s", $1);
3803 $$ = calloc(1, sizeof(struct node_state_opt));
3805 err(1, "state_opt_item: calloc");
3806 $$->type = PF_STATE_OPT_TIMEOUT;
3807 $$->data.timeout.number = pf_timeouts[i].timeout;
3808 $$->data.timeout.seconds = $2;
3814 label : LABEL STRING {
3819 qname : QUEUE STRING {
3823 | QUEUE '(' STRING ')' {
3827 | QUEUE '(' STRING comma STRING ')' {
3833 no : /* empty */ { $$ = 0; }
3837 portstar : numberstring {
3838 if (parseport($1, &$$, PPORT_RANGE|PPORT_STAR) == -1) {
3846 redirspec : host { $$ = $1; }
3847 | '{' optnl redir_host_list '}' { $$ = $3; }
3850 redir_host_list : host optnl { $$ = $1; }
3851 | redir_host_list comma host optnl {
3852 $1->tail->next = $3;
3853 $1->tail = $3->tail;
3858 redirpool : /* empty */ { $$ = NULL; }
3860 $$ = calloc(1, sizeof(struct redirection));
3862 err(1, "redirection: calloc");
3864 $$->rport.a = $$->rport.b = $$->rport.t = 0;
3866 | ARROW redirspec PORT portstar {
3867 $$ = calloc(1, sizeof(struct redirection));
3869 err(1, "redirection: calloc");
3875 hashkey : /* empty */
3877 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3879 err(1, "hashkey: calloc");
3880 $$->key32[0] = arc4random();
3881 $$->key32[1] = arc4random();
3882 $$->key32[2] = arc4random();
3883 $$->key32[3] = arc4random();
3887 if (!strncmp($1, "0x", 2)) {
3888 if (strlen($1) != 34) {
3890 yyerror("hex key must be 128 bits "
3891 "(32 hex digits) long");
3894 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3896 err(1, "hashkey: calloc");
3898 if (sscanf($1, "0x%8x%8x%8x%8x",
3899 &$$->key32[0], &$$->key32[1],
3900 &$$->key32[2], &$$->key32[3]) != 4) {
3903 yyerror("invalid hex key");
3909 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3911 err(1, "hashkey: calloc");
3913 MD5Update(&context, (unsigned char *)$1,
3915 MD5Final((unsigned char *)$$, &context);
3916 HTONL($$->key32[0]);
3917 HTONL($$->key32[1]);
3918 HTONL($$->key32[2]);
3919 HTONL($$->key32[3]);
3925 pool_opts : { bzero(&pool_opts, sizeof pool_opts); }
3929 bzero(&pool_opts, sizeof pool_opts);
3934 pool_opts_l : pool_opts_l pool_opt
3938 pool_opt : BITMASK {
3939 if (pool_opts.type) {
3940 yyerror("pool type cannot be redefined");
3943 pool_opts.type = PF_POOL_BITMASK;
3946 if (pool_opts.type) {
3947 yyerror("pool type cannot be redefined");
3950 pool_opts.type = PF_POOL_RANDOM;
3952 | SOURCEHASH hashkey {
3953 if (pool_opts.type) {
3954 yyerror("pool type cannot be redefined");
3957 pool_opts.type = PF_POOL_SRCHASH;
3961 if (pool_opts.type) {
3962 yyerror("pool type cannot be redefined");
3965 pool_opts.type = PF_POOL_ROUNDROBIN;
3968 if (pool_opts.staticport) {
3969 yyerror("static-port cannot be redefined");
3972 pool_opts.staticport = 1;
3975 if (filter_opts.marker & POM_STICKYADDRESS) {
3976 yyerror("sticky-address cannot be redefined");
3979 pool_opts.marker |= POM_STICKYADDRESS;
3980 pool_opts.opts |= PF_POOL_STICKYADDR;
3984 redirection : /* empty */ { $$ = NULL; }
3986 $$ = calloc(1, sizeof(struct redirection));
3988 err(1, "redirection: calloc");
3990 $$->rport.a = $$->rport.b = $$->rport.t = 0;
3992 | ARROW host PORT portstar {
3993 $$ = calloc(1, sizeof(struct redirection));
3995 err(1, "redirection: calloc");
4001 natpasslog : /* empty */ { $$.b1 = $$.b2 = 0; $$.w2 = 0; }
4002 | PASS { $$.b1 = 1; $$.b2 = 0; $$.w2 = 0; }
4003 | PASS log { $$.b1 = 1; $$.b2 = $2.log; $$.w2 = $2.logif; }
4004 | log { $$.b1 = 0; $$.b2 = $1.log; $$.w2 = $1.logif; }
4007 nataction : no NAT natpasslog {
4009 yyerror("\"pass\" not valid with \"no\"");
4020 | no RDR natpasslog {
4022 yyerror("\"pass\" not valid with \"no\"");
4035 natrule : nataction interface af proto fromto tag tagged rtable
4040 if (check_rulestate(PFCTL_STATE_NAT))
4043 memset(&r, 0, sizeof(r));
4052 if ($5.src.host && $5.src.host->af &&
4053 !$5.src.host->ifindex)
4054 r.af = $5.src.host->af;
4055 else if ($5.dst.host && $5.dst.host->af &&
4056 !$5.dst.host->ifindex)
4057 r.af = $5.dst.host->af;
4061 if (strlcpy(r.tagname, $6, PF_TAG_NAME_SIZE) >=
4063 yyerror("tag too long, max %u chars",
4064 PF_TAG_NAME_SIZE - 1);
4069 if (strlcpy(r.match_tagname, $7.name,
4070 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4071 yyerror("tag too long, max %u chars",
4072 PF_TAG_NAME_SIZE - 1);
4075 r.match_tag_not = $7.neg;
4078 if (r.action == PF_NONAT || r.action == PF_NORDR) {
4080 yyerror("translation rule with 'no' "
4081 "does not need '->'");
4085 if ($9 == NULL || $9->host == NULL) {
4086 yyerror("translation rule requires '-> "
4090 if (!r.af && ! $9->host->ifindex)
4091 r.af = $9->host->af;
4093 remove_invalid_hosts(&$9->host, &r.af);
4094 if (invalid_redirect($9->host, r.af))
4096 if (check_netmask($9->host, r.af))
4099 r.rpool.proxy_port[0] = ntohs($9->rport.a);
4103 if (!$9->rport.b && $9->rport.t &&
4104 $5.dst.port != NULL) {
4105 r.rpool.proxy_port[1] =
4106 ntohs($9->rport.a) +
4108 $5.dst.port->port[1]) -
4110 $5.dst.port->port[0]));
4112 r.rpool.proxy_port[1] =
4116 r.rpool.proxy_port[1] =
4118 if (!r.rpool.proxy_port[0] &&
4119 !r.rpool.proxy_port[1]) {
4120 r.rpool.proxy_port[0] =
4121 PF_NAT_PROXY_PORT_LOW;
4122 r.rpool.proxy_port[1] =
4123 PF_NAT_PROXY_PORT_HIGH;
4124 } else if (!r.rpool.proxy_port[1])
4125 r.rpool.proxy_port[1] =
4126 r.rpool.proxy_port[0];
4132 r.rpool.opts = $10.type;
4133 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
4134 PF_POOL_NONE && ($9->host->next != NULL ||
4135 $9->host->addr.type == PF_ADDR_TABLE ||
4136 DYNIF_MULTIADDR($9->host->addr)))
4137 r.rpool.opts = PF_POOL_ROUNDROBIN;
4138 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4139 PF_POOL_ROUNDROBIN &&
4140 disallow_table($9->host, "tables are only "
4141 "supported in round-robin redirection "
4144 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4145 PF_POOL_ROUNDROBIN &&
4146 disallow_alias($9->host, "interface (%s) "
4147 "is only supported in round-robin "
4148 "redirection pools"))
4150 if ($9->host->next != NULL) {
4151 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4152 PF_POOL_ROUNDROBIN) {
4153 yyerror("only round-robin "
4154 "valid for multiple "
4155 "redirection addresses");
4161 if ($10.key != NULL)
4162 memcpy(&r.rpool.key, $10.key,
4163 sizeof(struct pf_poolhashkey));
4166 r.rpool.opts |= $10.opts;
4168 if ($10.staticport) {
4169 if (r.action != PF_NAT) {
4170 yyerror("the 'static-port' option is "
4171 "only valid with nat rules");
4174 if (r.rpool.proxy_port[0] !=
4175 PF_NAT_PROXY_PORT_LOW &&
4176 r.rpool.proxy_port[1] !=
4177 PF_NAT_PROXY_PORT_HIGH) {
4178 yyerror("the 'static-port' option can't"
4179 " be used when specifying a port"
4183 r.rpool.proxy_port[0] = 0;
4184 r.rpool.proxy_port[1] = 0;
4187 expand_rule(&r, $2, $9 == NULL ? NULL : $9->host, $4,
4188 $5.src_os, $5.src.host, $5.src.port, $5.dst.host,
4189 $5.dst.port, 0, 0, 0, "");
4194 binatrule : no BINAT natpasslog interface af proto FROM host toipspec tag
4195 tagged rtable redirection
4197 struct pf_rule binat;
4198 struct pf_pooladdr *pa;
4200 if (check_rulestate(PFCTL_STATE_NAT))
4202 if (disallow_urpf_failed($9, "\"urpf-failed\" is not "
4203 "permitted as a binat destination"))
4206 memset(&binat, 0, sizeof(binat));
4209 yyerror("\"pass\" not valid with \"no\"");
4213 binat.action = PF_NOBINAT;
4215 binat.action = PF_BINAT;
4216 binat.natpass = $3.b1;
4218 binat.logif = $3.w2;
4220 if (!binat.af && $8 != NULL && $8->af)
4222 if (!binat.af && $9 != NULL && $9->af)
4225 if (!binat.af && $13 != NULL && $13->host)
4226 binat.af = $13->host->af;
4228 yyerror("address family (inet/inet6) "
4234 memcpy(binat.ifname, $4->ifname,
4235 sizeof(binat.ifname));
4236 binat.ifnot = $4->not;
4241 if (strlcpy(binat.tagname, $10,
4242 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4243 yyerror("tag too long, max %u chars",
4244 PF_TAG_NAME_SIZE - 1);
4248 if (strlcpy(binat.match_tagname, $11.name,
4249 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4250 yyerror("tag too long, max %u chars",
4251 PF_TAG_NAME_SIZE - 1);
4254 binat.match_tag_not = $11.neg;
4255 binat.rtableid = $12;
4258 binat.proto = $6->proto;
4262 if ($8 != NULL && disallow_table($8, "invalid use of "
4263 "table <%s> as the source address of a binat rule"))
4265 if ($8 != NULL && disallow_alias($8, "invalid use of "
4266 "interface (%s) as the source address of a binat "
4269 if ($13 != NULL && $13->host != NULL && disallow_table(
4270 $13->host, "invalid use of table <%s> as the "
4271 "redirect address of a binat rule"))
4273 if ($13 != NULL && $13->host != NULL && disallow_alias(
4274 $13->host, "invalid use of interface (%s) as the "
4275 "redirect address of a binat rule"))
4280 yyerror("multiple binat ip addresses");
4283 if ($8->addr.type == PF_ADDR_DYNIFTL)
4285 if ($8->af != binat.af) {
4286 yyerror("binat ip versions must match");
4289 if (check_netmask($8, binat.af))
4291 memcpy(&binat.src.addr, &$8->addr,
4292 sizeof(binat.src.addr));
4297 yyerror("multiple binat ip addresses");
4300 if ($9->af != binat.af && $9->af) {
4301 yyerror("binat ip versions must match");
4304 if (check_netmask($9, binat.af))
4306 memcpy(&binat.dst.addr, &$9->addr,
4307 sizeof(binat.dst.addr));
4308 binat.dst.neg = $9->not;
4312 if (binat.action == PF_NOBINAT) {
4314 yyerror("'no binat' rule does not need"
4319 if ($13 == NULL || $13->host == NULL) {
4320 yyerror("'binat' rule requires"
4325 remove_invalid_hosts(&$13->host, &binat.af);
4326 if (invalid_redirect($13->host, binat.af))
4328 if ($13->host->next != NULL) {
4329 yyerror("binat rule must redirect to "
4330 "a single address");
4333 if (check_netmask($13->host, binat.af))
4336 if (!PF_AZERO(&binat.src.addr.v.a.mask,
4338 !PF_AEQ(&binat.src.addr.v.a.mask,
4339 &$13->host->addr.v.a.mask, binat.af)) {
4340 yyerror("'binat' source mask and "
4341 "redirect mask must be the same");
4345 TAILQ_INIT(&binat.rpool.list);
4346 pa = calloc(1, sizeof(struct pf_pooladdr));
4348 err(1, "binat: calloc");
4349 pa->addr = $13->host->addr;
4351 TAILQ_INSERT_TAIL(&binat.rpool.list,
4357 pfctl_add_rule(pf, &binat, "");
4361 tag : /* empty */ { $$ = NULL; }
4362 | TAG STRING { $$ = $2; }
4365 tagged : /* empty */ { $$.neg = 0; $$.name = NULL; }
4366 | not TAGGED string { $$.neg = $1; $$.name = $3; }
4369 rtable : /* empty */ { $$ = -1; }
4371 if ($2 < 0 || $2 > rt_tableid_max()) {
4372 yyerror("invalid rtable id");
4379 route_host : STRING {
4380 $$ = calloc(1, sizeof(struct node_host));
4382 err(1, "route_host: calloc");
4384 set_ipmask($$, 128);
4388 | '(' STRING host ')' {
4394 route_host_list : route_host optnl { $$ = $1; }
4395 | route_host_list comma route_host optnl {
4398 if ($1->af != $3->af) {
4399 yyerror("all pool addresses must be in the "
4400 "same address family");
4403 $1->tail->next = $3;
4404 $1->tail = $3->tail;
4409 routespec : route_host { $$ = $1; }
4410 | '{' optnl route_host_list '}' { $$ = $3; }
4413 route : /* empty */ {
4420 $$.rt = PF_FASTROUTE;
4423 | ROUTETO routespec pool_opts {
4426 $$.pool_opts = $3.type | $3.opts;
4430 | REPLYTO routespec pool_opts {
4433 $$.pool_opts = $3.type | $3.opts;
4437 | DUPTO routespec pool_opts {
4440 $$.pool_opts = $3.type | $3.opts;
4446 timeout_spec : STRING NUMBER
4448 if (check_rulestate(PFCTL_STATE_OPTION)) {
4452 if ($2 < 0 || $2 > UINT_MAX) {
4453 yyerror("only positive values permitted");
4456 if (pfctl_set_timeout(pf, $1, $2, 0) != 0) {
4457 yyerror("unknown timeout %s", $1);
4464 if (check_rulestate(PFCTL_STATE_OPTION))
4466 if ($2 < 0 || $2 > UINT_MAX) {
4467 yyerror("only positive values permitted");
4470 if (pfctl_set_timeout(pf, "interval", $2, 0) != 0)
4475 timeout_list : timeout_list comma timeout_spec optnl
4476 | timeout_spec optnl
4479 limit_spec : STRING NUMBER
4481 if (check_rulestate(PFCTL_STATE_OPTION)) {
4485 if ($2 < 0 || $2 > UINT_MAX) {
4486 yyerror("only positive values permitted");
4489 if (pfctl_set_limit(pf, $1, $2) != 0) {
4490 yyerror("unable to set limit %s %u", $1, $2);
4498 limit_list : limit_list comma limit_spec optnl
4506 yesno : NO { $$ = 0; }
4508 if (!strcmp($1, "yes"))
4511 yyerror("invalid value '%s', expected 'yes' "
4520 unaryop : '=' { $$ = PF_OP_EQ; }
4521 | '!' '=' { $$ = PF_OP_NE; }
4522 | '<' '=' { $$ = PF_OP_LE; }
4523 | '<' { $$ = PF_OP_LT; }
4524 | '>' '=' { $$ = PF_OP_GE; }
4525 | '>' { $$ = PF_OP_GT; }
4531 yyerror(const char *fmt, ...)
4537 fprintf(stderr, "%s:%d: ", file->name, yylval.lineno);
4538 vfprintf(stderr, fmt, ap);
4539 fprintf(stderr, "\n");
4545 disallow_table(struct node_host *h, const char *fmt)
4547 for (; h != NULL; h = h->next)
4548 if (h->addr.type == PF_ADDR_TABLE) {
4549 yyerror(fmt, h->addr.v.tblname);
4556 disallow_urpf_failed(struct node_host *h, const char *fmt)
4558 for (; h != NULL; h = h->next)
4559 if (h->addr.type == PF_ADDR_URPFFAILED) {
4567 disallow_alias(struct node_host *h, const char *fmt)
4569 for (; h != NULL; h = h->next)
4570 if (DYNIF_MULTIADDR(h->addr)) {
4571 yyerror(fmt, h->addr.v.tblname);
4578 rule_consistent(struct pf_rule *r, int anchor_call)
4582 switch (r->action) {
4587 problems = filter_consistent(r, anchor_call);
4591 problems = nat_consistent(r);
4595 problems = rdr_consistent(r);
4606 filter_consistent(struct pf_rule *r, int anchor_call)
4610 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP &&
4611 (r->src.port_op || r->dst.port_op)) {
4612 yyerror("port only applies to tcp/udp");
4615 if (r->proto != IPPROTO_ICMP && r->proto != IPPROTO_ICMPV6 &&
4616 (r->type || r->code)) {
4617 yyerror("icmp-type/code only applies to icmp");
4620 if (!r->af && (r->type || r->code)) {
4621 yyerror("must indicate address family with icmp-type/code");
4624 if (r->overload_tblname[0] &&
4625 r->max_src_conn == 0 && r->max_src_conn_rate.seconds == 0) {
4626 yyerror("'overload' requires 'max-src-conn' "
4627 "or 'max-src-conn-rate'");
4630 if ((r->proto == IPPROTO_ICMP && r->af == AF_INET6) ||
4631 (r->proto == IPPROTO_ICMPV6 && r->af == AF_INET)) {
4632 yyerror("proto %s doesn't match address family %s",
4633 r->proto == IPPROTO_ICMP ? "icmp" : "icmp6",
4634 r->af == AF_INET ? "inet" : "inet6");
4637 if (r->allow_opts && r->action != PF_PASS) {
4638 yyerror("allow-opts can only be specified for pass rules");
4641 if (r->rule_flag & PFRULE_FRAGMENT && (r->src.port_op ||
4642 r->dst.port_op || r->flagset || r->type || r->code)) {
4643 yyerror("fragments can be filtered only on IP header fields");
4646 if (r->rule_flag & PFRULE_RETURNRST && r->proto != IPPROTO_TCP) {
4647 yyerror("return-rst can only be applied to TCP rules");
4650 if (r->max_src_nodes && !(r->rule_flag & PFRULE_RULESRCTRACK)) {
4651 yyerror("max-src-nodes requires 'source-track rule'");
4654 if (r->action == PF_DROP && r->keep_state) {
4655 yyerror("keep state on block rules doesn't make sense");
4658 if (r->rule_flag & PFRULE_STATESLOPPY &&
4659 (r->keep_state == PF_STATE_MODULATE ||
4660 r->keep_state == PF_STATE_SYNPROXY)) {
4661 yyerror("sloppy state matching cannot be used with "
4662 "synproxy state or modulate state");
4669 nat_consistent(struct pf_rule *r)
4671 return (0); /* yeah! */
4675 rdr_consistent(struct pf_rule *r)
4679 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP) {
4680 if (r->src.port_op) {
4681 yyerror("src port only applies to tcp/udp");
4684 if (r->dst.port_op) {
4685 yyerror("dst port only applies to tcp/udp");
4688 if (r->rpool.proxy_port[0]) {
4689 yyerror("rpool port only applies to tcp/udp");
4693 if (r->dst.port_op &&
4694 r->dst.port_op != PF_OP_EQ && r->dst.port_op != PF_OP_RRG) {
4695 yyerror("invalid port operator for rdr destination port");
4702 process_tabledef(char *name, struct table_opts *opts)
4704 struct pfr_buffer ab;
4705 struct node_tinit *ti;
4707 bzero(&ab, sizeof(ab));
4708 ab.pfrb_type = PFRB_ADDRS;
4709 SIMPLEQ_FOREACH(ti, &opts->init_nodes, entries) {
4711 if (pfr_buf_load(&ab, ti->file, 0, append_addr)) {
4713 yyerror("cannot load \"%s\": %s",
4714 ti->file, strerror(errno));
4716 yyerror("file \"%s\" contains bad data",
4721 if (append_addr_host(&ab, ti->host, 0, 0)) {
4722 yyerror("cannot create address buffer: %s",
4727 if (pf->opts & PF_OPT_VERBOSE)
4728 print_tabledef(name, opts->flags, opts->init_addr,
4730 if (!(pf->opts & PF_OPT_NOACTION) &&
4731 pfctl_define_table(name, opts->flags, opts->init_addr,
4732 pf->anchor->name, &ab, pf->anchor->ruleset.tticket)) {
4733 yyerror("cannot define table %s: %s", name,
4734 pfr_strerror(errno));
4750 /* macro gore, but you should've seen the prior indentation nightmare... */
4752 #define FREE_LIST(T,r) \
4755 while (node != NULL) { \
4757 node = node->next; \
4762 #define LOOP_THROUGH(T,n,r,C) \
4766 r = calloc(1, sizeof(T)); \
4768 err(1, "LOOP: calloc"); \
4772 while (n != NULL) { \
4781 expand_label_str(char *label, size_t len, const char *srch, const char *repl)
4786 if ((tmp = calloc(1, len)) == NULL)
4787 err(1, "expand_label_str: calloc");
4789 while ((q = strstr(p, srch)) != NULL) {
4791 if ((strlcat(tmp, p, len) >= len) ||
4792 (strlcat(tmp, repl, len) >= len))
4793 errx(1, "expand_label: label too long");
4797 if (strlcat(tmp, p, len) >= len)
4798 errx(1, "expand_label: label too long");
4799 strlcpy(label, tmp, len); /* always fits */
4804 expand_label_if(const char *name, char *label, size_t len, const char *ifname)
4806 if (strstr(label, name) != NULL) {
4808 expand_label_str(label, len, name, "any");
4810 expand_label_str(label, len, name, ifname);
4815 expand_label_addr(const char *name, char *label, size_t len, sa_family_t af,
4816 struct node_host *h)
4818 char tmp[64], tmp_not[66];
4820 if (strstr(label, name) != NULL) {
4821 switch (h->addr.type) {
4822 case PF_ADDR_DYNIFTL:
4823 snprintf(tmp, sizeof(tmp), "(%s)", h->addr.v.ifname);
4826 snprintf(tmp, sizeof(tmp), "<%s>", h->addr.v.tblname);
4828 case PF_ADDR_NOROUTE:
4829 snprintf(tmp, sizeof(tmp), "no-route");
4831 case PF_ADDR_URPFFAILED:
4832 snprintf(tmp, sizeof(tmp), "urpf-failed");
4834 case PF_ADDR_ADDRMASK:
4835 if (!af || (PF_AZERO(&h->addr.v.a.addr, af) &&
4836 PF_AZERO(&h->addr.v.a.mask, af)))
4837 snprintf(tmp, sizeof(tmp), "any");
4842 if (inet_ntop(af, &h->addr.v.a.addr, a,
4844 snprintf(tmp, sizeof(tmp), "?");
4846 bits = unmask(&h->addr.v.a.mask, af);
4847 if ((af == AF_INET && bits < 32) ||
4848 (af == AF_INET6 && bits < 128))
4849 snprintf(tmp, sizeof(tmp),
4852 snprintf(tmp, sizeof(tmp),
4858 snprintf(tmp, sizeof(tmp), "?");
4863 snprintf(tmp_not, sizeof(tmp_not), "! %s", tmp);
4864 expand_label_str(label, len, name, tmp_not);
4866 expand_label_str(label, len, name, tmp);
4871 expand_label_port(const char *name, char *label, size_t len,
4872 struct node_port *port)
4874 char a1[6], a2[6], op[13] = "";
4876 if (strstr(label, name) != NULL) {
4877 snprintf(a1, sizeof(a1), "%u", ntohs(port->port[0]));
4878 snprintf(a2, sizeof(a2), "%u", ntohs(port->port[1]));
4881 else if (port->op == PF_OP_IRG)
4882 snprintf(op, sizeof(op), "%s><%s", a1, a2);
4883 else if (port->op == PF_OP_XRG)
4884 snprintf(op, sizeof(op), "%s<>%s", a1, a2);
4885 else if (port->op == PF_OP_EQ)
4886 snprintf(op, sizeof(op), "%s", a1);
4887 else if (port->op == PF_OP_NE)
4888 snprintf(op, sizeof(op), "!=%s", a1);
4889 else if (port->op == PF_OP_LT)
4890 snprintf(op, sizeof(op), "<%s", a1);
4891 else if (port->op == PF_OP_LE)
4892 snprintf(op, sizeof(op), "<=%s", a1);
4893 else if (port->op == PF_OP_GT)
4894 snprintf(op, sizeof(op), ">%s", a1);
4895 else if (port->op == PF_OP_GE)
4896 snprintf(op, sizeof(op), ">=%s", a1);
4897 expand_label_str(label, len, name, op);
4902 expand_label_proto(const char *name, char *label, size_t len, u_int8_t proto)
4904 struct protoent *pe;
4907 if (strstr(label, name) != NULL) {
4908 pe = getprotobynumber(proto);
4910 expand_label_str(label, len, name, pe->p_name);
4912 snprintf(n, sizeof(n), "%u", proto);
4913 expand_label_str(label, len, name, n);
4919 expand_label_nr(const char *name, char *label, size_t len)
4923 if (strstr(label, name) != NULL) {
4924 snprintf(n, sizeof(n), "%u", pf->anchor->match);
4925 expand_label_str(label, len, name, n);
4930 expand_label(char *label, size_t len, const char *ifname, sa_family_t af,
4931 struct node_host *src_host, struct node_port *src_port,
4932 struct node_host *dst_host, struct node_port *dst_port,
4935 expand_label_if("$if", label, len, ifname);
4936 expand_label_addr("$srcaddr", label, len, af, src_host);
4937 expand_label_addr("$dstaddr", label, len, af, dst_host);
4938 expand_label_port("$srcport", label, len, src_port);
4939 expand_label_port("$dstport", label, len, dst_port);
4940 expand_label_proto("$proto", label, len, proto);
4941 expand_label_nr("$nr", label, len);
4945 expand_altq(struct pf_altq *a, struct node_if *interfaces,
4946 struct node_queue *nqueues, struct node_queue_bw bwspec,
4947 struct node_queue_opt *opts)
4949 struct pf_altq pa, pb;
4950 char qname[PF_QNAME_SIZE];
4951 struct node_queue *n;
4952 struct node_queue_bw bw;
4955 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
4956 FREE_LIST(struct node_if, interfaces);
4958 FREE_LIST(struct node_queue, nqueues);
4962 LOOP_THROUGH(struct node_if, interface, interfaces,
4963 memcpy(&pa, a, sizeof(struct pf_altq));
4964 if (strlcpy(pa.ifname, interface->ifname,
4965 sizeof(pa.ifname)) >= sizeof(pa.ifname))
4966 errx(1, "expand_altq: strlcpy");
4968 if (interface->not) {
4969 yyerror("altq on ! <interface> is not supported");
4972 if (eval_pfaltq(pf, &pa, &bwspec, opts))
4975 if (pfctl_add_altq(pf, &pa))
4978 if (pf->opts & PF_OPT_VERBOSE) {
4979 print_altq(&pf->paltq->altq, 0,
4981 if (nqueues && nqueues->tail) {
4983 LOOP_THROUGH(struct node_queue, queue,
4993 if (pa.scheduler == ALTQT_CBQ ||
4994 pa.scheduler == ALTQT_HFSC) {
4995 /* now create a root queue */
4996 memset(&pb, 0, sizeof(struct pf_altq));
4997 if (strlcpy(qname, "root_", sizeof(qname)) >=
4999 errx(1, "expand_altq: strlcpy");
5000 if (strlcat(qname, interface->ifname,
5001 sizeof(qname)) >= sizeof(qname))
5002 errx(1, "expand_altq: strlcat");
5003 if (strlcpy(pb.qname, qname,
5004 sizeof(pb.qname)) >= sizeof(pb.qname))
5005 errx(1, "expand_altq: strlcpy");
5006 if (strlcpy(pb.ifname, interface->ifname,
5007 sizeof(pb.ifname)) >= sizeof(pb.ifname))
5008 errx(1, "expand_altq: strlcpy");
5009 pb.qlimit = pa.qlimit;
5010 pb.scheduler = pa.scheduler;
5011 bw.bw_absolute = pa.ifbandwidth;
5013 if (eval_pfqueue(pf, &pb, &bw, opts))
5016 if (pfctl_add_altq(pf, &pb))
5020 LOOP_THROUGH(struct node_queue, queue, nqueues,
5021 n = calloc(1, sizeof(struct node_queue));
5023 err(1, "expand_altq: calloc");
5024 if (pa.scheduler == ALTQT_CBQ ||
5025 pa.scheduler == ALTQT_HFSC)
5026 if (strlcpy(n->parent, qname,
5027 sizeof(n->parent)) >=
5029 errx(1, "expand_altq: strlcpy");
5030 if (strlcpy(n->queue, queue->queue,
5031 sizeof(n->queue)) >= sizeof(n->queue))
5032 errx(1, "expand_altq: strlcpy");
5033 if (strlcpy(n->ifname, interface->ifname,
5034 sizeof(n->ifname)) >= sizeof(n->ifname))
5035 errx(1, "expand_altq: strlcpy");
5036 n->scheduler = pa.scheduler;
5042 queues->tail->next = n;
5048 FREE_LIST(struct node_if, interfaces);
5050 FREE_LIST(struct node_queue, nqueues);
5056 expand_queue(struct pf_altq *a, struct node_if *interfaces,
5057 struct node_queue *nqueues, struct node_queue_bw bwspec,
5058 struct node_queue_opt *opts)
5060 struct node_queue *n, *nq;
5065 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
5066 FREE_LIST(struct node_queue, nqueues);
5070 if (queues == NULL) {
5071 yyerror("queue %s has no parent", a->qname);
5072 FREE_LIST(struct node_queue, nqueues);
5076 LOOP_THROUGH(struct node_if, interface, interfaces,
5077 LOOP_THROUGH(struct node_queue, tqueue, queues,
5078 if (!strncmp(a->qname, tqueue->queue, PF_QNAME_SIZE) &&
5079 (interface->ifname[0] == 0 ||
5080 (!interface->not && !strncmp(interface->ifname,
5081 tqueue->ifname, IFNAMSIZ)) ||
5082 (interface->not && strncmp(interface->ifname,
5083 tqueue->ifname, IFNAMSIZ)))) {
5084 /* found ourself in queues */
5087 memcpy(&pa, a, sizeof(struct pf_altq));
5089 if (pa.scheduler != ALTQT_NONE &&
5090 pa.scheduler != tqueue->scheduler) {
5091 yyerror("exactly one scheduler type "
5092 "per interface allowed");
5095 pa.scheduler = tqueue->scheduler;
5097 /* scheduler dependent error checking */
5098 switch (pa.scheduler) {
5100 if (nqueues != NULL) {
5101 yyerror("priq queues cannot "
5102 "have child queues");
5105 if (bwspec.bw_absolute > 0 ||
5106 bwspec.bw_percent < 100) {
5107 yyerror("priq doesn't take "
5116 if (strlcpy(pa.ifname, tqueue->ifname,
5117 sizeof(pa.ifname)) >= sizeof(pa.ifname))
5118 errx(1, "expand_queue: strlcpy");
5119 if (strlcpy(pa.parent, tqueue->parent,
5120 sizeof(pa.parent)) >= sizeof(pa.parent))
5121 errx(1, "expand_queue: strlcpy");
5123 if (eval_pfqueue(pf, &pa, &bwspec, opts))
5126 if (pfctl_add_altq(pf, &pa))
5129 for (nq = nqueues; nq != NULL; nq = nq->next) {
5130 if (!strcmp(a->qname, nq->queue)) {
5131 yyerror("queue cannot have "
5137 sizeof(struct node_queue));
5139 err(1, "expand_queue: calloc");
5140 if (strlcpy(n->parent, a->qname,
5141 sizeof(n->parent)) >=
5143 errx(1, "expand_queue strlcpy");
5144 if (strlcpy(n->queue, nq->queue,
5145 sizeof(n->queue)) >=
5147 errx(1, "expand_queue strlcpy");
5148 if (strlcpy(n->ifname, tqueue->ifname,
5149 sizeof(n->ifname)) >=
5151 errx(1, "expand_queue strlcpy");
5152 n->scheduler = tqueue->scheduler;
5158 queues->tail->next = n;
5162 if ((pf->opts & PF_OPT_VERBOSE) && (
5163 (found == 1 && interface->ifname[0] == 0) ||
5164 (found > 0 && interface->ifname[0] != 0))) {
5165 print_queue(&pf->paltq->altq, 0,
5166 &bwspec, interface->ifname[0] != 0,
5168 if (nqueues && nqueues->tail) {
5170 LOOP_THROUGH(struct node_queue,
5183 FREE_LIST(struct node_queue, nqueues);
5184 FREE_LIST(struct node_if, interfaces);
5187 yyerror("queue %s has no parent", a->qname);
5198 expand_rule(struct pf_rule *r,
5199 struct node_if *interfaces, struct node_host *rpool_hosts,
5200 struct node_proto *protos, struct node_os *src_oses,
5201 struct node_host *src_hosts, struct node_port *src_ports,
5202 struct node_host *dst_hosts, struct node_port *dst_ports,
5203 struct node_uid *uids, struct node_gid *gids, struct node_icmp *icmp_types,
5204 const char *anchor_call)
5206 sa_family_t af = r->af;
5207 int added = 0, error = 0;
5208 char ifname[IF_NAMESIZE];
5209 char label[PF_RULE_LABEL_SIZE];
5210 char tagname[PF_TAG_NAME_SIZE];
5211 char match_tagname[PF_TAG_NAME_SIZE];
5212 struct pf_pooladdr *pa;
5213 struct node_host *h;
5214 u_int8_t flags, flagset, keep_state;
5216 if (strlcpy(label, r->label, sizeof(label)) >= sizeof(label))
5217 errx(1, "expand_rule: strlcpy");
5218 if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname))
5219 errx(1, "expand_rule: strlcpy");
5220 if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >=
5221 sizeof(match_tagname))
5222 errx(1, "expand_rule: strlcpy");
5224 flagset = r->flagset;
5225 keep_state = r->keep_state;
5227 LOOP_THROUGH(struct node_if, interface, interfaces,
5228 LOOP_THROUGH(struct node_proto, proto, protos,
5229 LOOP_THROUGH(struct node_icmp, icmp_type, icmp_types,
5230 LOOP_THROUGH(struct node_host, src_host, src_hosts,
5231 LOOP_THROUGH(struct node_port, src_port, src_ports,
5232 LOOP_THROUGH(struct node_os, src_os, src_oses,
5233 LOOP_THROUGH(struct node_host, dst_host, dst_hosts,
5234 LOOP_THROUGH(struct node_port, dst_port, dst_ports,
5235 LOOP_THROUGH(struct node_uid, uid, uids,
5236 LOOP_THROUGH(struct node_gid, gid, gids,
5239 /* for link-local IPv6 address, interface must match up */
5240 if ((r->af && src_host->af && r->af != src_host->af) ||
5241 (r->af && dst_host->af && r->af != dst_host->af) ||
5242 (src_host->af && dst_host->af &&
5243 src_host->af != dst_host->af) ||
5244 (src_host->ifindex && dst_host->ifindex &&
5245 src_host->ifindex != dst_host->ifindex) ||
5246 (src_host->ifindex && *interface->ifname &&
5247 src_host->ifindex != if_nametoindex(interface->ifname)) ||
5248 (dst_host->ifindex && *interface->ifname &&
5249 dst_host->ifindex != if_nametoindex(interface->ifname)))
5251 if (!r->af && src_host->af)
5252 r->af = src_host->af;
5253 else if (!r->af && dst_host->af)
5254 r->af = dst_host->af;
5256 if (*interface->ifname)
5257 strlcpy(r->ifname, interface->ifname,
5259 else if (if_indextoname(src_host->ifindex, ifname))
5260 strlcpy(r->ifname, ifname, sizeof(r->ifname));
5261 else if (if_indextoname(dst_host->ifindex, ifname))
5262 strlcpy(r->ifname, ifname, sizeof(r->ifname));
5264 memset(r->ifname, '\0', sizeof(r->ifname));
5266 if (strlcpy(r->label, label, sizeof(r->label)) >=
5268 errx(1, "expand_rule: strlcpy");
5269 if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >=
5271 errx(1, "expand_rule: strlcpy");
5272 if (strlcpy(r->match_tagname, match_tagname,
5273 sizeof(r->match_tagname)) >= sizeof(r->match_tagname))
5274 errx(1, "expand_rule: strlcpy");
5275 expand_label(r->label, PF_RULE_LABEL_SIZE, r->ifname, r->af,
5276 src_host, src_port, dst_host, dst_port, proto->proto);
5277 expand_label(r->tagname, PF_TAG_NAME_SIZE, r->ifname, r->af,
5278 src_host, src_port, dst_host, dst_port, proto->proto);
5279 expand_label(r->match_tagname, PF_TAG_NAME_SIZE, r->ifname,
5280 r->af, src_host, src_port, dst_host, dst_port,
5283 error += check_netmask(src_host, r->af);
5284 error += check_netmask(dst_host, r->af);
5286 r->ifnot = interface->not;
5287 r->proto = proto->proto;
5288 r->src.addr = src_host->addr;
5289 r->src.neg = src_host->not;
5290 r->src.port[0] = src_port->port[0];
5291 r->src.port[1] = src_port->port[1];
5292 r->src.port_op = src_port->op;
5293 r->dst.addr = dst_host->addr;
5294 r->dst.neg = dst_host->not;
5295 r->dst.port[0] = dst_port->port[0];
5296 r->dst.port[1] = dst_port->port[1];
5297 r->dst.port_op = dst_port->op;
5298 r->uid.op = uid->op;
5299 r->uid.uid[0] = uid->uid[0];
5300 r->uid.uid[1] = uid->uid[1];
5301 r->gid.op = gid->op;
5302 r->gid.gid[0] = gid->gid[0];
5303 r->gid.gid[1] = gid->gid[1];
5304 r->type = icmp_type->type;
5305 r->code = icmp_type->code;
5307 if ((keep_state == PF_STATE_MODULATE ||
5308 keep_state == PF_STATE_SYNPROXY) &&
5309 r->proto && r->proto != IPPROTO_TCP)
5310 r->keep_state = PF_STATE_NORMAL;
5312 r->keep_state = keep_state;
5314 if (r->proto && r->proto != IPPROTO_TCP) {
5319 r->flagset = flagset;
5321 if (icmp_type->proto && r->proto != icmp_type->proto) {
5322 yyerror("icmp-type mismatch");
5326 if (src_os && src_os->os) {
5327 r->os_fingerprint = pfctl_get_fingerprint(src_os->os);
5328 if ((pf->opts & PF_OPT_VERBOSE2) &&
5329 r->os_fingerprint == PF_OSFP_NOMATCH)
5331 "warning: unknown '%s' OS fingerprint\n",
5334 r->os_fingerprint = PF_OSFP_ANY;
5337 TAILQ_INIT(&r->rpool.list);
5338 for (h = rpool_hosts; h != NULL; h = h->next) {
5339 pa = calloc(1, sizeof(struct pf_pooladdr));
5341 err(1, "expand_rule: calloc");
5343 if (h->ifname != NULL) {
5344 if (strlcpy(pa->ifname, h->ifname,
5345 sizeof(pa->ifname)) >=
5347 errx(1, "expand_rule: strlcpy");
5350 TAILQ_INSERT_TAIL(&r->rpool.list, pa, entries);
5353 if (rule_consistent(r, anchor_call[0]) < 0 || error)
5354 yyerror("skipping rule due to errors");
5356 r->nr = pf->astack[pf->asd]->match++;
5357 pfctl_add_rule(pf, r, anchor_call);
5363 FREE_LIST(struct node_if, interfaces);
5364 FREE_LIST(struct node_proto, protos);
5365 FREE_LIST(struct node_host, src_hosts);
5366 FREE_LIST(struct node_port, src_ports);
5367 FREE_LIST(struct node_os, src_oses);
5368 FREE_LIST(struct node_host, dst_hosts);
5369 FREE_LIST(struct node_port, dst_ports);
5370 FREE_LIST(struct node_uid, uids);
5371 FREE_LIST(struct node_gid, gids);
5372 FREE_LIST(struct node_icmp, icmp_types);
5373 FREE_LIST(struct node_host, rpool_hosts);
5376 yyerror("rule expands to no valid combination");
5380 expand_skip_interface(struct node_if *interfaces)
5384 if (!interfaces || (!interfaces->next && !interfaces->not &&
5385 !strcmp(interfaces->ifname, "none"))) {
5386 if (pf->opts & PF_OPT_VERBOSE)
5387 printf("set skip on none\n");
5388 errs = pfctl_set_interface_flags(pf, "", PFI_IFLAG_SKIP, 0);
5392 if (pf->opts & PF_OPT_VERBOSE)
5393 printf("set skip on {");
5394 LOOP_THROUGH(struct node_if, interface, interfaces,
5395 if (pf->opts & PF_OPT_VERBOSE)
5396 printf(" %s", interface->ifname);
5397 if (interface->not) {
5398 yyerror("skip on ! <interface> is not supported");
5401 errs += pfctl_set_interface_flags(pf,
5402 interface->ifname, PFI_IFLAG_SKIP, 1);
5404 if (pf->opts & PF_OPT_VERBOSE)
5407 FREE_LIST(struct node_if, interfaces);
5419 check_rulestate(int desired_state)
5421 if (require_order && (rulestate > desired_state)) {
5422 yyerror("Rules must be in order: options, normalization, "
5423 "queueing, translation, filtering");
5426 rulestate = desired_state;
5431 kw_cmp(const void *k, const void *e)
5433 return (strcmp(k, ((const struct keywords *)e)->k_name));
5439 /* this has to be sorted always */
5440 static const struct keywords keywords[] = {
5442 { "allow-opts", ALLOWOPTS},
5444 { "anchor", ANCHOR},
5445 { "antispoof", ANTISPOOF},
5447 { "bandwidth", BANDWIDTH},
5449 { "binat-anchor", BINATANCHOR},
5450 { "bitmask", BITMASK},
5452 { "block-policy", BLOCKPOLICY},
5453 { "buckets", BUCKETS},
5457 { "crop", FRAGCROP},
5459 { "divert-reply", DIVERTREPLY},
5460 { "divert-to", DIVERTTO},
5462 { "drop-ovl", FRAGDROP},
5465 { "fastroute", FASTROUTE},
5466 { "file", FILENAME},
5467 { "fingerprints", FINGERPRINTS},
5469 { "floating", FLOATING},
5472 { "fragment", FRAGMENT},
5474 { "global", GLOBAL},
5478 { "hostid", HOSTID},
5479 { "icmp-type", ICMPTYPE},
5480 { "icmp6-type", ICMP6TYPE},
5481 { "if-bound", IFBOUND},
5483 { "include", INCLUDE},
5486 { "interval", INTERVAL},
5490 { "linkshare", LINKSHARE},
5493 { "loginterface", LOGINTERFACE},
5495 { "max-mss", MAXMSS},
5496 { "max-src-conn", MAXSRCCONN},
5497 { "max-src-conn-rate", MAXSRCCONNRATE},
5498 { "max-src-nodes", MAXSRCNODES},
5499 { "max-src-states", MAXSRCSTATES},
5500 { "min-ttl", MINTTL},
5501 { "modulate", MODULATE},
5503 { "nat-anchor", NATANCHOR},
5506 { "no-route", NOROUTE},
5507 { "no-sync", NOSYNC},
5509 { "optimization", OPTIMIZATION},
5512 { "overload", OVERLOAD},
5516 { "priority", PRIORITY},
5518 { "probability", PROBABILITY},
5520 { "qlimit", QLIMIT},
5523 { "random", RANDOM},
5524 { "random-id", RANDOMID},
5526 { "rdr-anchor", RDRANCHOR},
5527 { "realtime", REALTIME},
5528 { "reassemble", REASSEMBLE},
5529 { "reply-to", REPLYTO},
5530 { "require-order", REQUIREORDER},
5531 { "return", RETURN},
5532 { "return-icmp", RETURNICMP},
5533 { "return-icmp6", RETURNICMP6},
5534 { "return-rst", RETURNRST},
5535 { "round-robin", ROUNDROBIN},
5537 { "route-to", ROUTETO},
5538 { "rtable", RTABLE},
5540 { "ruleset-optimization", RULESET_OPTIMIZATION},
5543 { "set-tos", SETTOS},
5545 { "sloppy", SLOPPY},
5546 { "source-hash", SOURCEHASH},
5547 { "source-track", SOURCETRACK},
5549 { "state-defaults", STATEDEFAULTS},
5550 { "state-policy", STATEPOLICY},
5551 { "static-port", STATICPORT},
5552 { "sticky-address", STICKYADDRESS},
5553 { "synproxy", SYNPROXY},
5556 { "tagged", TAGGED},
5557 { "target", TARGET},
5558 { "tbrsize", TBRSIZE},
5559 { "timeout", TIMEOUT},
5563 { "upperlimit", UPPERLIMIT},
5564 { "urpf-failed", URPFFAILED},
5567 const struct keywords *p;
5569 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
5570 sizeof(keywords[0]), kw_cmp);
5574 fprintf(stderr, "%s: %d\n", s, p->k_val);
5578 fprintf(stderr, "string: %s\n", s);
5583 #define MAXPUSHBACK 128
5585 static char *parsebuf;
5586 static int parseindex;
5587 static char pushback_buffer[MAXPUSHBACK];
5588 static int pushback_index = 0;
5596 /* Read character from the parsebuffer instead of input. */
5597 if (parseindex >= 0) {
5598 c = parsebuf[parseindex++];
5607 return (pushback_buffer[--pushback_index]);
5610 if ((c = getc(file->stream)) == EOF) {
5611 yyerror("reached end of file while parsing quoted string");
5612 if (popfile() == EOF)
5619 while ((c = getc(file->stream)) == '\\') {
5620 next = getc(file->stream);
5625 yylval.lineno = file->lineno;
5630 if (popfile() == EOF)
5632 c = getc(file->stream);
5644 if (parseindex >= 0)
5647 if (pushback_index < MAXPUSHBACK-1)
5648 return (pushback_buffer[pushback_index++] = c);
5660 /* skip to either EOF or the first real EOL */
5663 c = pushback_buffer[--pushback_index];
5681 int quotec, next, c;
5686 while ((c = lgetc(0)) == ' ' || c == '\t')
5689 yylval.lineno = file->lineno;
5691 while ((c = lgetc(0)) != '\n' && c != EOF)
5693 if (c == '$' && parsebuf == NULL) {
5695 if ((c = lgetc(0)) == EOF)
5698 if (p + 1 >= buf + sizeof(buf) - 1) {
5699 yyerror("string too long");
5702 if (isalnum(c) || c == '_') {
5712 yyerror("macro '%s' not defined", buf);
5725 if ((c = lgetc(quotec)) == EOF)
5730 } else if (c == '\\') {
5731 if ((next = lgetc(quotec)) == EOF)
5733 if (next == quotec || c == ' ' || c == '\t')
5735 else if (next == '\n')
5739 } else if (c == quotec) {
5743 if (p + 1 >= buf + sizeof(buf) - 1) {
5744 yyerror("string too long");
5749 yylval.v.string = strdup(buf);
5750 if (yylval.v.string == NULL)
5751 err(1, "yylex: strdup");
5756 yylval.v.i = PF_OP_XRG;
5757 return (PORTBINARY);
5764 yylval.v.i = PF_OP_IRG;
5765 return (PORTBINARY);
5777 #define allowed_to_end_number(x) \
5778 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
5780 if (c == '-' || isdigit(c)) {
5783 if ((unsigned)(p-buf) >= sizeof(buf)) {
5784 yyerror("string too long");
5787 } while ((c = lgetc(0)) != EOF && isdigit(c));
5789 if (p == buf + 1 && buf[0] == '-')
5791 if (c == EOF || allowed_to_end_number(c)) {
5792 const char *errstr = NULL;
5795 yylval.v.number = strtonum(buf, LLONG_MIN,
5796 LLONG_MAX, &errstr);
5798 yyerror("\"%s\" invalid number: %s",
5813 #define allowed_in_string(x) \
5814 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
5815 x != '{' && x != '}' && x != '<' && x != '>' && \
5816 x != '!' && x != '=' && x != '/' && x != '#' && \
5819 if (isalnum(c) || c == ':' || c == '_') {
5822 if ((unsigned)(p-buf) >= sizeof(buf)) {
5823 yyerror("string too long");
5826 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
5829 if ((token = lookup(buf)) == STRING)
5830 if ((yylval.v.string = strdup(buf)) == NULL)
5831 err(1, "yylex: strdup");
5835 yylval.lineno = file->lineno;
5844 check_file_secrecy(int fd, const char *fname)
5848 if (fstat(fd, &st)) {
5849 warn("cannot stat %s", fname);
5852 if (st.st_uid != 0 && st.st_uid != getuid()) {
5853 warnx("%s: owner not root or current user", fname);
5856 if (st.st_mode & (S_IRWXG | S_IRWXO)) {
5857 warnx("%s: group/world readable/writeable", fname);
5864 pushfile(const char *name, int secret)
5868 if ((nfile = calloc(1, sizeof(struct file))) == NULL ||
5869 (nfile->name = strdup(name)) == NULL) {
5873 if (TAILQ_FIRST(&files) == NULL && strcmp(nfile->name, "-") == 0) {
5874 nfile->stream = stdin;
5876 if ((nfile->name = strdup("stdin")) == NULL) {
5881 } else if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
5882 warn("%s", nfile->name);
5886 } else if (secret &&
5887 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
5888 fclose(nfile->stream);
5894 TAILQ_INSERT_TAIL(&files, nfile, entry);
5903 if ((prev = TAILQ_PREV(file, files, entry)) != NULL) {
5904 prev->errors += file->errors;
5905 TAILQ_REMOVE(&files, file, entry);
5906 fclose(file->stream);
5916 parse_config(char *filename, struct pfctl *xpf)
5923 rulestate = PFCTL_STATE_NONE;
5924 returnicmpdefault = (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
5925 returnicmp6default =
5926 (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
5927 blockpolicy = PFRULE_DROP;
5930 if ((file = pushfile(filename, 0)) == NULL) {
5931 warn("cannot open the main config file!");
5936 errors = file->errors;
5939 /* Free macros and check which have not been used. */
5940 while ((sym = TAILQ_FIRST(&symhead))) {
5941 if ((pf->opts & PF_OPT_VERBOSE2) && !sym->used)
5942 fprintf(stderr, "warning: macro '%s' not "
5943 "used\n", sym->nam);
5946 TAILQ_REMOVE(&symhead, sym, entry);
5950 return (errors ? -1 : 0);
5954 symset(const char *nam, const char *val, int persist)
5958 for (sym = TAILQ_FIRST(&symhead); sym && strcmp(nam, sym->nam);
5959 sym = TAILQ_NEXT(sym, entry))
5963 if (sym->persist == 1)
5968 TAILQ_REMOVE(&symhead, sym, entry);
5972 if ((sym = calloc(1, sizeof(*sym))) == NULL)
5975 sym->nam = strdup(nam);
5976 if (sym->nam == NULL) {
5980 sym->val = strdup(val);
5981 if (sym->val == NULL) {
5987 sym->persist = persist;
5988 TAILQ_INSERT_TAIL(&symhead, sym, entry);
5993 pfctl_cmdline_symset(char *s)
5998 if ((val = strrchr(s, '=')) == NULL)
6001 if ((sym = malloc(strlen(s) - strlen(val) + 1)) == NULL)
6002 err(1, "pfctl_cmdline_symset: malloc");
6004 strlcpy(sym, s, strlen(s) - strlen(val) + 1);
6006 ret = symset(sym, val + 1, 1);
6013 symget(const char *nam)
6017 TAILQ_FOREACH(sym, &symhead, entry)
6018 if (strcmp(nam, sym->nam) == 0) {
6026 mv_rules(struct pf_ruleset *src, struct pf_ruleset *dst)
6031 for (i = 0; i < PF_RULESET_MAX; ++i) {
6032 while ((r = TAILQ_FIRST(src->rules[i].active.ptr))
6034 TAILQ_REMOVE(src->rules[i].active.ptr, r, entries);
6035 TAILQ_INSERT_TAIL(dst->rules[i].active.ptr, r, entries);
6036 dst->anchor->match++;
6038 src->anchor->match = 0;
6039 while ((r = TAILQ_FIRST(src->rules[i].inactive.ptr))
6041 TAILQ_REMOVE(src->rules[i].inactive.ptr, r, entries);
6042 TAILQ_INSERT_TAIL(dst->rules[i].inactive.ptr,
6049 decide_address_family(struct node_host *n, sa_family_t *af)
6051 if (*af != 0 || n == NULL)
6054 while ((n = n->next) != NULL) {
6063 remove_invalid_hosts(struct node_host **nh, sa_family_t *af)
6065 struct node_host *n = *nh, *prev = NULL;
6068 if (*af && n->af && n->af != *af) {
6069 /* unlink and free n */
6070 struct node_host *next = n->next;
6072 /* adjust tail pointer */
6073 if (n == (*nh)->tail)
6075 /* adjust previous node's next pointer */
6081 if (n->ifname != NULL)
6095 invalid_redirect(struct node_host *nh, sa_family_t af)
6098 struct node_host *n;
6100 /* tables and dyniftl are ok without an address family */
6101 for (n = nh; n != NULL; n = n->next) {
6102 if (n->addr.type != PF_ADDR_TABLE &&
6103 n->addr.type != PF_ADDR_DYNIFTL) {
6104 yyerror("address family not given and "
6105 "translation address expands to multiple "
6106 "address families");
6112 yyerror("no translation address with matching address family "
6120 atoul(char *s, u_long *ulvalp)
6126 ulval = strtoul(s, &ep, 0);
6127 if (s[0] == '\0' || *ep != '\0')
6129 if (errno == ERANGE && ulval == ULONG_MAX)
6141 if (atoul(n, &ulval) == 0) {
6142 if (ulval > 65535) {
6143 yyerror("illegal port value %lu", ulval);
6146 return (htons(ulval));
6148 s = getservbyname(n, "tcp");
6150 s = getservbyname(n, "udp");
6152 yyerror("unknown port %s", n);
6160 rule_label(struct pf_rule *r, char *s)
6163 if (strlcpy(r->label, s, sizeof(r->label)) >=
6165 yyerror("rule label too long (max %d chars)",
6166 sizeof(r->label)-1);
6174 parseicmpspec(char *w, sa_family_t af)
6176 const struct icmpcodeent *p;
6181 icmptype = returnicmpdefault >> 8;
6183 icmptype = returnicmp6default >> 8;
6185 if (atoul(w, &ulval) == -1) {
6186 if ((p = geticmpcodebyname(icmptype, w, af)) == NULL) {
6187 yyerror("unknown icmp code %s", w);
6193 yyerror("invalid icmp code %lu", ulval);
6196 return (icmptype << 8 | ulval);
6200 parseport(char *port, struct range *r, int extensions)
6202 char *p = strchr(port, ':');
6205 if ((r->a = getservice(port)) == -1)
6211 if ((extensions & PPORT_STAR) && !strcmp(p+1, "*")) {
6213 if ((r->a = getservice(port)) == -1)
6219 if ((extensions & PPORT_RANGE)) {
6221 if ((r->a = getservice(port)) == -1 ||
6222 (r->b = getservice(p)) == -1)
6235 pfctl_load_anchors(int dev, struct pfctl *pf, struct pfr_buffer *trans)
6237 struct loadanchors *la;
6239 TAILQ_FOREACH(la, &loadanchorshead, entries) {
6240 if (pf->opts & PF_OPT_VERBOSE)
6241 fprintf(stderr, "\nLoading anchor %s from %s\n",
6242 la->anchorname, la->filename);
6243 if (pfctl_rules(dev, la->filename, pf->opts, pf->optimize,
6244 la->anchorname, trans) == -1)
6252 rt_tableid_max(void)
6256 size_t l = sizeof(fibs);
6258 if (sysctlbyname("net.fibs", &fibs, &l, NULL, 0) == -1)
6259 fibs = 16; /* XXX RT_MAXFIBS, at least limit it some. */
6261 * As the OpenBSD code only compares > and not >= we need to adjust
6262 * here given we only accept values of 0..n and want to avoid #ifdefs
6267 return (RT_TABLEID_MAX);