1 /* $OpenBSD: parse.y,v 1.554 2008/10/17 12:59:53 henning Exp $ */
4 * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c) 2001 Markus Friedl. All rights reserved.
7 * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
8 * Copyright (c) 2001 Theo de Raadt. All rights reserved.
9 * Copyright (c) 2002,2003 Henning Brauer. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #define PFIOC_USE_LATEST
37 #include <sys/types.h>
38 #include <sys/socket.h>
41 #include <sys/sysctl.h>
44 #include <netinet/in.h>
45 #include <netinet/in_systm.h>
46 #include <netinet/ip.h>
47 #include <netinet/ip_icmp.h>
48 #include <netinet/icmp6.h>
49 #include <net/pfvar.h>
50 #include <arpa/inet.h>
51 #include <net/altq/altq.h>
52 #include <net/altq/altq_cbq.h>
53 #include <net/altq/altq_codel.h>
54 #include <net/altq/altq_priq.h>
55 #include <net/altq/altq_hfsc.h>
56 #include <net/altq/altq_fairq.h>
74 #include "pfctl_parser.h"
77 static struct pfctl *pf = NULL;
79 static int rulestate = 0;
80 static u_int16_t returnicmpdefault =
81 (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
82 static u_int16_t returnicmp6default =
83 (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
84 static int blockpolicy = PFRULE_DROP;
85 static int failpolicy = PFRULE_DROP;
86 static int require_order = 1;
87 static int default_statelock;
89 static TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
91 TAILQ_ENTRY(file) entry;
97 struct file *pushfile(const char *, int);
99 int check_file_secrecy(int, const char *);
102 int yyerror(const char *, ...);
103 int kw_cmp(const void *, const void *);
109 static TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
111 TAILQ_ENTRY(sym) entry;
117 int symset(const char *, const char *, int);
118 char *symget(const char *);
120 int atoul(char *, u_long *);
132 struct node_etherproto {
134 struct node_etherproto *next;
135 struct node_etherproto *tail;
140 struct node_proto *next;
141 struct node_proto *tail;
147 struct node_port *next;
148 struct node_port *tail;
154 struct node_uid *next;
155 struct node_uid *tail;
161 struct node_gid *next;
162 struct node_gid *tail;
169 struct node_icmp *next;
170 struct node_icmp *tail;
173 enum { PF_STATE_OPT_MAX, PF_STATE_OPT_NOSYNC, PF_STATE_OPT_SRCTRACK,
174 PF_STATE_OPT_MAX_SRC_STATES, PF_STATE_OPT_MAX_SRC_CONN,
175 PF_STATE_OPT_MAX_SRC_CONN_RATE, PF_STATE_OPT_MAX_SRC_NODES,
176 PF_STATE_OPT_OVERLOAD, PF_STATE_OPT_STATELOCK,
177 PF_STATE_OPT_TIMEOUT, PF_STATE_OPT_SLOPPY, };
179 enum { PF_SRCTRACK_NONE, PF_SRCTRACK, PF_SRCTRACK_GLOBAL, PF_SRCTRACK_RULE };
181 struct node_state_opt {
184 u_int32_t max_states;
185 u_int32_t max_src_states;
186 u_int32_t max_src_conn;
193 char tblname[PF_TABLE_NAME_SIZE];
195 u_int32_t max_src_nodes;
203 struct node_state_opt *next;
204 struct node_state_opt *tail;
208 struct node_host *host;
209 struct node_port *port;
212 static struct node_queue {
213 char queue[PF_QNAME_SIZE];
214 char parent[PF_QNAME_SIZE];
215 char ifname[IFNAMSIZ];
217 struct node_queue *next;
218 struct node_queue *tail;
221 struct node_qassign {
226 static struct filter_opts {
228 #define FOM_FLAGS 0x01
229 #define FOM_ICMP 0x02
231 #define FOM_KEEP 0x08
232 #define FOM_SRCTRACK 0x10
233 #define FOM_SETPRIO 0x0400
234 #define FOM_PRIO 0x2000
235 struct node_uid *uid;
236 struct node_gid *gid;
243 struct node_icmp *icmpspec;
246 u_int32_t ridentifier;
249 struct node_state_opt *options;
253 char *label[PF_RULE_MAX_LABEL_COUNT];
255 struct node_qassign queues;
258 u_int8_t match_tag_not;
261 u_int32_t free_flags;
264 u_int8_t set_prio[2];
266 struct node_host *addr;
271 static struct antispoof_opts {
272 char *label[PF_RULE_MAX_LABEL_COUNT];
274 u_int32_t ridentifier;
278 static struct scrub_opts {
280 #define SOM_MINTTL 0x01
281 #define SOM_MAXMSS 0x02
282 #define SOM_FRAGCACHE 0x04
283 #define SOM_SETTOS 0x08
292 u_int8_t match_tag_not;
296 static struct queue_opts {
298 #define QOM_BWSPEC 0x01
299 #define QOM_SCHEDULER 0x02
300 #define QOM_PRIORITY 0x04
301 #define QOM_TBRSIZE 0x08
302 #define QOM_QLIMIT 0x10
303 struct node_queue_bw queue_bwspec;
304 struct node_queue_opt scheduler;
306 unsigned int tbrsize;
310 static struct table_opts {
313 struct node_tinithead init_nodes;
316 static struct pool_opts {
318 #define POM_TYPE 0x01
319 #define POM_STICKYADDRESS 0x02
323 struct pf_poolhashkey *key;
324 struct pf_mape_portset mape;
328 static struct codel_opts codel_opts;
329 static struct node_hfsc_opts hfsc_opts;
330 static struct node_fairq_opts fairq_opts;
331 static struct node_state_opt *keep_state_defaults = NULL;
332 static struct pfctl_watermarks syncookie_opts;
334 int disallow_table(struct node_host *, const char *);
335 int disallow_urpf_failed(struct node_host *, const char *);
336 int disallow_alias(struct node_host *, const char *);
337 int rule_consistent(struct pfctl_rule *, int);
338 int filter_consistent(struct pfctl_rule *, int);
339 int nat_consistent(struct pfctl_rule *);
340 int rdr_consistent(struct pfctl_rule *);
341 int process_tabledef(char *, struct table_opts *);
342 void expand_label_str(char *, size_t, const char *, const char *);
343 void expand_label_if(const char *, char *, size_t, const char *);
344 void expand_label_addr(const char *, char *, size_t, u_int8_t,
345 struct pf_rule_addr *);
346 void expand_label_port(const char *, char *, size_t,
347 struct pf_rule_addr *);
348 void expand_label_proto(const char *, char *, size_t, u_int8_t);
349 void expand_label_nr(const char *, char *, size_t,
350 struct pfctl_rule *);
351 void expand_eth_rule(struct pfctl_eth_rule *,
352 struct node_if *, struct node_etherproto *,
353 struct node_mac *, struct node_mac *,
354 struct node_host *, struct node_host *, const char *);
355 void expand_rule(struct pfctl_rule *, struct node_if *,
356 struct node_host *, struct node_proto *, struct node_os *,
357 struct node_host *, struct node_port *, struct node_host *,
358 struct node_port *, struct node_uid *, struct node_gid *,
359 struct node_icmp *, const char *);
360 int expand_altq(struct pf_altq *, struct node_if *,
361 struct node_queue *, struct node_queue_bw bwspec,
362 struct node_queue_opt *);
363 int expand_queue(struct pf_altq *, struct node_if *,
364 struct node_queue *, struct node_queue_bw,
365 struct node_queue_opt *);
366 int expand_skip_interface(struct node_if *);
368 int check_rulestate(int);
369 int getservice(char *);
370 int rule_label(struct pfctl_rule *, char *s[PF_RULE_MAX_LABEL_COUNT]);
371 int rt_tableid_max(void);
373 void mv_rules(struct pfctl_ruleset *, struct pfctl_ruleset *);
374 void mv_eth_rules(struct pfctl_eth_ruleset *, struct pfctl_eth_ruleset *);
375 void decide_address_family(struct node_host *, sa_family_t *);
376 void remove_invalid_hosts(struct node_host **, sa_family_t *);
377 int invalid_redirect(struct node_host *, sa_family_t);
378 u_int16_t parseicmpspec(char *, sa_family_t);
379 int kw_casecmp(const void *, const void *);
380 int map_tos(char *string, int *);
381 struct node_mac* node_mac_from_string(const char *);
382 struct node_mac* node_mac_from_string_masklen(const char *, int);
383 struct node_mac* node_mac_from_string_mask(const char *, const char *);
385 static TAILQ_HEAD(loadanchorshead, loadanchors)
386 loadanchorshead = TAILQ_HEAD_INITIALIZER(loadanchorshead);
389 TAILQ_ENTRY(loadanchors) entries;
412 struct node_if *interface;
413 struct node_proto *proto;
414 struct node_etherproto *etherproto;
415 struct node_icmp *icmp;
416 struct node_host *host;
418 struct node_port *port;
419 struct node_uid *uid;
420 struct node_gid *gid;
421 struct node_state_opt *state_opt;
424 struct peer src, dst;
425 struct node_os *src_os;
428 struct node_mac *src;
429 struct node_mac *dst;
431 struct node_mac *mac;
433 struct node_mac *mac;
436 struct node_host *host;
440 struct pf_poolhashkey *key;
443 struct node_host *host;
448 struct node_state_opt *options;
459 struct pf_poolhashkey *hashkey;
460 struct node_queue *queue;
461 struct node_queue_opt queue_options;
462 struct node_queue_bw queue_bwspec;
463 struct node_qassign qassign;
464 struct filter_opts filter_opts;
465 struct antispoof_opts antispoof_opts;
466 struct queue_opts queue_opts;
467 struct scrub_opts scrub_opts;
468 struct table_opts table_opts;
469 struct pool_opts pool_opts;
470 struct node_hfsc_opts hfsc_opts;
471 struct node_fairq_opts fairq_opts;
472 struct codel_opts codel_opts;
473 struct pfctl_watermarks *watermarks;
478 #define PPORT_RANGE 1
480 int parseport(char *, struct range *r, int);
482 #define DYNIF_MULTIADDR(addr) ((addr).type == PF_ADDR_DYNIFTL && \
483 (!((addr).iflags & PFI_AFLAG_NOALIAS) || \
484 !isdigit((addr).v.ifname[strlen((addr).v.ifname)-1])))
488 %token PASS BLOCK MATCH SCRUB RETURN IN OS OUT LOG QUICK ON FROM TO FLAGS
489 %token RETURNRST RETURNICMP RETURNICMP6 PROTO INET INET6 ALL ANY ICMPTYPE
490 %token ICMP6TYPE CODE KEEP MODULATE STATE PORT RDR NAT BINAT ARROW NODF
491 %token MINTTL ERROR ALLOWOPTS FASTROUTE FILENAME ROUTETO DUPTO REPLYTO NO LABEL
492 %token NOROUTE URPFFAILED FRAGMENT USER GROUP MAXMSS MAXIMUM TTL TOS DROP TABLE
493 %token REASSEMBLE FRAGDROP FRAGCROP ANCHOR NATANCHOR RDRANCHOR BINATANCHOR
494 %token SET OPTIMIZATION TIMEOUT LIMIT LOGINTERFACE BLOCKPOLICY FAILPOLICY
495 %token RANDOMID REQUIREORDER SYNPROXY FINGERPRINTS NOSYNC DEBUG SKIP HOSTID
496 %token ANTISPOOF FOR INCLUDE KEEPCOUNTERS SYNCOOKIES L3
498 %token BITMASK RANDOM SOURCEHASH ROUNDROBIN STATICPORT PROBABILITY MAPEPORTSET
499 %token ALTQ CBQ CODEL PRIQ HFSC FAIRQ BANDWIDTH TBRSIZE LINKSHARE REALTIME
500 %token UPPERLIMIT QUEUE PRIORITY QLIMIT HOGS BUCKETS RTABLE TARGET INTERVAL
501 %token DNPIPE DNQUEUE RIDENTIFIER
502 %token LOAD RULESET_OPTIMIZATION PRIO
503 %token STICKYADDRESS MAXSRCSTATES MAXSRCNODES SOURCETRACK GLOBAL RULE
504 %token MAXSRCCONN MAXSRCCONNRATE OVERLOAD FLUSH SLOPPY
505 %token TAGGED TAG IFBOUND FLOATING STATEPOLICY STATEDEFAULTS ROUTE SETTOS
506 %token DIVERTTO DIVERTREPLY
507 %token <v.string> STRING
508 %token <v.number> NUMBER
509 %token <v.i> PORTBINARY
510 %type <v.interface> interface if_list if_item_not if_item
511 %type <v.number> number icmptype icmp6type uid gid
512 %type <v.number> tos not yesno
513 %type <v.probability> probability
514 %type <v.i> no dir af fragcache optimizer syncookie_val
515 %type <v.i> sourcetrack flush unaryop statelock
516 %type <v.i> etherprotoval
517 %type <v.b> action nataction natpasslog scrubaction
518 %type <v.b> flags flag blockspec prio
519 %type <v.range> portplain portstar portrange
520 %type <v.hashkey> hashkey
521 %type <v.proto> proto proto_list proto_item
522 %type <v.number> protoval
523 %type <v.icmp> icmpspec
524 %type <v.icmp> icmp_list icmp_item
525 %type <v.icmp> icmp6_list icmp6_item
526 %type <v.number> reticmpspec reticmp6spec
527 %type <v.fromto> fromto l3fromto
528 %type <v.peer> ipportspec from to
529 %type <v.host> ipspec toipspec xhost host dynaddr host_list
530 %type <v.host> redir_host_list redirspec
531 %type <v.host> route_host route_host_list routespec
532 %type <v.os> os xos os_list
533 %type <v.port> portspec port_list port_item
534 %type <v.uid> uids uid_list uid_item
535 %type <v.gid> gids gid_list gid_item
536 %type <v.route> route
537 %type <v.redirection> redirection redirpool
538 %type <v.string> label stringall tag anchorname
539 %type <v.string> string varstring numberstring
540 %type <v.keep_state> keep
541 %type <v.state_opt> state_opt_spec state_opt_list state_opt_item
542 %type <v.logquick> logquick quick log logopts logopt
543 %type <v.interface> antispoof_ifspc antispoof_iflst antispoof_if
544 %type <v.qassign> qname etherqname
545 %type <v.queue> qassign qassign_list qassign_item
546 %type <v.queue_options> scheduler
547 %type <v.number> cbqflags_list cbqflags_item
548 %type <v.number> priqflags_list priqflags_item
549 %type <v.hfsc_opts> hfscopts_list hfscopts_item hfsc_opts
550 %type <v.fairq_opts> fairqopts_list fairqopts_item fairq_opts
551 %type <v.codel_opts> codelopts_list codelopts_item codel_opts
552 %type <v.queue_bwspec> bandwidth
553 %type <v.filter_opts> filter_opts filter_opt filter_opts_l etherfilter_opts etherfilter_opt etherfilter_opts_l
554 %type <v.filter_opts> filter_sets filter_set filter_sets_l
555 %type <v.antispoof_opts> antispoof_opts antispoof_opt antispoof_opts_l
556 %type <v.queue_opts> queue_opts queue_opt queue_opts_l
557 %type <v.scrub_opts> scrub_opts scrub_opt scrub_opts_l
558 %type <v.table_opts> table_opts table_opt table_opts_l
559 %type <v.pool_opts> pool_opts pool_opt pool_opts_l
560 %type <v.tagged> tagged
561 %type <v.rtableid> rtable
562 %type <v.watermarks> syncookie_opts
563 %type <v.etherproto> etherproto etherproto_list etherproto_item
564 %type <v.etherfromto> etherfromto
565 %type <v.etheraddr> etherfrom etherto
566 %type <v.mac> xmac mac mac_list macspec
569 ruleset : /* empty */
570 | ruleset include '\n'
572 | ruleset option '\n'
573 | ruleset etherrule '\n'
574 | ruleset etheranchorrule '\n'
575 | ruleset scrubrule '\n'
576 | ruleset natrule '\n'
577 | ruleset binatrule '\n'
578 | ruleset pfrule '\n'
579 | ruleset anchorrule '\n'
580 | ruleset loadrule '\n'
581 | ruleset altqif '\n'
582 | ruleset queuespec '\n'
583 | ruleset varset '\n'
584 | ruleset antispoof '\n'
585 | ruleset tabledef '\n'
586 | '{' fakeanchor '}' '\n';
587 | ruleset error '\n' { file->errors++; }
590 include : INCLUDE STRING {
593 if ((nfile = pushfile($2, 0)) == NULL) {
594 yyerror("failed to include file %s", $2);
606 * apply to previouslys specified rule: must be careful to note
607 * what that is: pf or nat or binat or rdr
609 fakeanchor : fakeanchor '\n'
610 | fakeanchor anchorrule '\n'
611 | fakeanchor binatrule '\n'
612 | fakeanchor natrule '\n'
613 | fakeanchor pfrule '\n'
614 | fakeanchor error '\n'
618 if (!strcmp($1, "none"))
620 else if (!strcmp($1, "basic"))
621 $$ = PF_OPTIMIZE_BASIC;
622 else if (!strcmp($1, "profile"))
623 $$ = PF_OPTIMIZE_BASIC | PF_OPTIMIZE_PROFILE;
625 yyerror("unknown ruleset-optimization %s", $1);
631 option : SET OPTIMIZATION STRING {
632 if (check_rulestate(PFCTL_STATE_OPTION)) {
636 if (pfctl_set_optimization(pf, $3) != 0) {
637 yyerror("unknown optimization %s", $3);
643 | SET RULESET_OPTIMIZATION optimizer {
644 if (!(pf->opts & PF_OPT_OPTIMIZE)) {
645 pf->opts |= PF_OPT_OPTIMIZE;
649 | SET TIMEOUT timeout_spec
650 | SET TIMEOUT '{' optnl timeout_list '}'
651 | SET LIMIT limit_spec
652 | SET LIMIT '{' optnl limit_list '}'
653 | SET LOGINTERFACE stringall {
654 if (check_rulestate(PFCTL_STATE_OPTION)) {
658 if (pfctl_set_logif(pf, $3) != 0) {
659 yyerror("error setting loginterface %s", $3);
665 | SET HOSTID number {
666 if ($3 == 0 || $3 > UINT_MAX) {
667 yyerror("hostid must be non-zero");
670 if (pfctl_set_hostid(pf, $3) != 0) {
671 yyerror("error setting hostid %08x", $3);
675 | SET BLOCKPOLICY DROP {
676 if (pf->opts & PF_OPT_VERBOSE)
677 printf("set block-policy drop\n");
678 if (check_rulestate(PFCTL_STATE_OPTION))
680 blockpolicy = PFRULE_DROP;
682 | SET BLOCKPOLICY RETURN {
683 if (pf->opts & PF_OPT_VERBOSE)
684 printf("set block-policy return\n");
685 if (check_rulestate(PFCTL_STATE_OPTION))
687 blockpolicy = PFRULE_RETURN;
689 | SET FAILPOLICY DROP {
690 if (pf->opts & PF_OPT_VERBOSE)
691 printf("set fail-policy drop\n");
692 if (check_rulestate(PFCTL_STATE_OPTION))
694 failpolicy = PFRULE_DROP;
696 | SET FAILPOLICY RETURN {
697 if (pf->opts & PF_OPT_VERBOSE)
698 printf("set fail-policy return\n");
699 if (check_rulestate(PFCTL_STATE_OPTION))
701 failpolicy = PFRULE_RETURN;
703 | SET REQUIREORDER yesno {
704 if (pf->opts & PF_OPT_VERBOSE)
705 printf("set require-order %s\n",
706 $3 == 1 ? "yes" : "no");
709 | SET FINGERPRINTS STRING {
710 if (pf->opts & PF_OPT_VERBOSE)
711 printf("set fingerprints \"%s\"\n", $3);
712 if (check_rulestate(PFCTL_STATE_OPTION)) {
716 if (!pf->anchor->name[0]) {
717 if (pfctl_file_fingerprints(pf->dev,
719 yyerror("error loading "
720 "fingerprints %s", $3);
727 | SET STATEPOLICY statelock {
728 if (pf->opts & PF_OPT_VERBOSE)
731 printf("set state-policy floating\n");
734 printf("set state-policy if-bound\n");
737 default_statelock = $3;
740 if (check_rulestate(PFCTL_STATE_OPTION)) {
744 if (pfctl_set_debug(pf, $3) != 0) {
745 yyerror("error setting debuglevel %s", $3);
751 | SET SKIP interface {
752 if (expand_skip_interface($3) != 0) {
753 yyerror("error setting skip interface(s)");
757 | SET STATEDEFAULTS state_opt_list {
758 if (keep_state_defaults != NULL) {
759 yyerror("cannot redefine state-defaults");
762 keep_state_defaults = $3;
765 pf->keep_counters = true;
767 | SET SYNCOOKIES syncookie_val syncookie_opts {
768 if (pfctl_cfg_syncookies(pf, $3, $4)) {
769 yyerror("error setting syncookies");
775 syncookie_val : STRING {
776 if (!strcmp($1, "never"))
777 $$ = PFCTL_SYNCOOKIES_NEVER;
778 else if (!strcmp($1, "adaptive"))
779 $$ = PFCTL_SYNCOOKIES_ADAPTIVE;
780 else if (!strcmp($1, "always"))
781 $$ = PFCTL_SYNCOOKIES_ALWAYS;
783 yyerror("illegal value for syncookies");
788 syncookie_opts : /* empty */ { $$ = NULL; }
790 memset(&syncookie_opts, 0, sizeof(syncookie_opts));
791 } '(' syncookie_opt_l ')' { $$ = &syncookie_opts; }
794 syncookie_opt_l : syncookie_opt_l comma syncookie_opt
798 syncookie_opt : STRING STRING {
802 val = strtod($2, &cp);
803 if (cp == NULL || strcmp(cp, "%"))
805 if (val <= 0 || val > 100) {
806 yyerror("illegal percentage value");
809 if (!strcmp($1, "start")) {
810 syncookie_opts.hi = val;
811 } else if (!strcmp($1, "end")) {
812 syncookie_opts.lo = val;
814 yyerror("illegal syncookie option");
820 stringall : STRING { $$ = $1; }
822 if (($$ = strdup("all")) == NULL) {
823 err(1, "stringall: strdup");
828 string : STRING string {
829 if (asprintf(&$$, "%s %s", $1, $2) == -1)
830 err(1, "string: asprintf");
837 varstring : numberstring varstring {
838 if (asprintf(&$$, "%s %s", $1, $2) == -1)
839 err(1, "string: asprintf");
846 numberstring : NUMBER {
848 if (asprintf(&s, "%lld", (long long)$1) == -1) {
849 yyerror("string: asprintf");
857 varset : STRING '=' varstring {
859 if (pf->opts & PF_OPT_VERBOSE)
860 printf("%s = \"%s\"\n", $1, $3);
862 if (isspace((unsigned char)*s)) {
863 yyerror("macro name cannot contain "
868 if (symset($1, $3, 0) == -1)
869 err(1, "cannot store variable %s", $1);
875 anchorname : STRING { $$ = $1; }
876 | /* empty */ { $$ = NULL; }
879 pfa_anchorlist : /* empty */
880 | pfa_anchorlist '\n'
881 | pfa_anchorlist pfrule '\n'
882 | pfa_anchorlist anchorrule '\n'
887 char ta[PF_ANCHOR_NAME_SIZE];
888 struct pfctl_ruleset *rs;
890 /* steping into a brace anchor */
894 /* create a holding ruleset in the root */
895 snprintf(ta, PF_ANCHOR_NAME_SIZE, "_%d", pf->bn);
896 rs = pf_find_or_create_ruleset(ta);
898 err(1, "pfa_anchor: pf_find_or_create_ruleset");
899 pf->astack[pf->asd] = rs->anchor;
900 pf->anchor = rs->anchor;
901 } '\n' pfa_anchorlist '}'
903 pf->alast = pf->anchor;
905 pf->anchor = pf->astack[pf->asd];
910 anchorrule : ANCHOR anchorname dir quick interface af proto fromto
911 filter_opts pfa_anchor
914 struct node_proto *proto;
916 if (check_rulestate(PFCTL_STATE_FILTER)) {
922 if ($2 && ($2[0] == '_' || strstr($2, "/_") != NULL)) {
924 yyerror("anchor names beginning with '_' "
925 "are reserved for internal use");
929 memset(&r, 0, sizeof(r));
930 if (pf->astack[pf->asd + 1]) {
931 /* move inline rules into relative location */
932 pfctl_anchor_setup(&r,
933 &pf->astack[pf->asd]->ruleset,
934 $2 ? $2 : pf->alast->name);
936 if (r.anchor == NULL)
937 err(1, "anchorrule: unable to "
940 if (pf->alast != r.anchor) {
941 if (r.anchor->match) {
942 yyerror("inline anchor '%s' "
947 mv_rules(&pf->alast->ruleset,
950 pf_remove_if_empty_ruleset(&pf->alast->ruleset);
951 pf->alast = r.anchor;
954 yyerror("anchors without explicit "
955 "rules must specify a name");
963 r.rtableid = $9.rtableid;
964 r.ridentifier = $9.ridentifier;
967 if (strlcpy(r.tagname, $9.tag,
968 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
969 yyerror("tag too long, max %u chars",
970 PF_TAG_NAME_SIZE - 1);
974 if (strlcpy(r.match_tagname, $9.match_tag,
975 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
976 yyerror("tag too long, max %u chars",
977 PF_TAG_NAME_SIZE - 1);
980 r.match_tag_not = $9.match_tag_not;
981 if (rule_label(&r, $9.label))
983 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++)
985 r.flags = $9.flags.b1;
986 r.flagset = $9.flags.b2;
987 if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
988 yyerror("flags always false");
991 if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
992 for (proto = $7; proto != NULL &&
993 proto->proto != IPPROTO_TCP;
996 if (proto == NULL && $7 != NULL) {
997 if ($9.flags.b1 || $9.flags.b2)
999 "flags only apply to tcp");
1002 "OS fingerprinting only "
1010 if ($9.keep.action) {
1011 yyerror("cannot specify state handling "
1017 if (strlcpy(r.match_tagname, $9.match_tag,
1018 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1019 yyerror("tag too long, max %u chars",
1020 PF_TAG_NAME_SIZE - 1);
1023 r.match_tag_not = $9.match_tag_not;
1024 if ($9.marker & FOM_PRIO) {
1026 r.prio = PF_PRIO_ZERO;
1030 if ($9.marker & FOM_SETPRIO) {
1031 r.set_prio[0] = $9.set_prio[0];
1032 r.set_prio[1] = $9.set_prio[1];
1033 r.scrub_flags |= PFSTATE_SETPRIO;
1036 decide_address_family($8.src.host, &r.af);
1037 decide_address_family($8.dst.host, &r.af);
1039 expand_rule(&r, $5, NULL, $7, $8.src_os,
1040 $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
1041 $9.uid, $9.gid, $9.icmpspec,
1042 pf->astack[pf->asd + 1] ? pf->alast->name : $2);
1044 pf->astack[pf->asd + 1] = NULL;
1046 | NATANCHOR string interface af proto fromto rtable {
1047 struct pfctl_rule r;
1049 if (check_rulestate(PFCTL_STATE_NAT)) {
1054 memset(&r, 0, sizeof(r));
1059 decide_address_family($6.src.host, &r.af);
1060 decide_address_family($6.dst.host, &r.af);
1062 expand_rule(&r, $3, NULL, $5, $6.src_os,
1063 $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
1067 | RDRANCHOR string interface af proto fromto rtable {
1068 struct pfctl_rule r;
1070 if (check_rulestate(PFCTL_STATE_NAT)) {
1075 memset(&r, 0, sizeof(r));
1080 decide_address_family($6.src.host, &r.af);
1081 decide_address_family($6.dst.host, &r.af);
1083 if ($6.src.port != NULL) {
1084 yyerror("source port parameter not supported"
1088 if ($6.dst.port != NULL) {
1089 if ($6.dst.port->next != NULL) {
1090 yyerror("destination port list "
1091 "expansion not supported in "
1094 } else if ($6.dst.port->op != PF_OP_EQ) {
1095 yyerror("destination port operators"
1096 " not supported in rdr-anchor");
1099 r.dst.port[0] = $6.dst.port->port[0];
1100 r.dst.port[1] = $6.dst.port->port[1];
1101 r.dst.port_op = $6.dst.port->op;
1104 expand_rule(&r, $3, NULL, $5, $6.src_os,
1105 $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
1109 | BINATANCHOR string interface af proto fromto rtable {
1110 struct pfctl_rule r;
1112 if (check_rulestate(PFCTL_STATE_NAT)) {
1117 memset(&r, 0, sizeof(r));
1118 r.action = PF_BINAT;
1122 if ($5->next != NULL) {
1123 yyerror("proto list expansion"
1124 " not supported in binat-anchor");
1127 r.proto = $5->proto;
1131 if ($6.src.host != NULL || $6.src.port != NULL ||
1132 $6.dst.host != NULL || $6.dst.port != NULL) {
1133 yyerror("fromto parameter not supported"
1134 " in binat-anchor");
1138 decide_address_family($6.src.host, &r.af);
1139 decide_address_family($6.dst.host, &r.af);
1141 pfctl_append_rule(pf, &r, $2);
1146 loadrule : LOAD ANCHOR string FROM string {
1147 struct loadanchors *loadanchor;
1149 if (strlen(pf->anchor->name) + 1 +
1150 strlen($3) >= MAXPATHLEN) {
1151 yyerror("anchorname %s too long, max %u\n",
1152 $3, MAXPATHLEN - 1);
1156 loadanchor = calloc(1, sizeof(struct loadanchors));
1157 if (loadanchor == NULL)
1158 err(1, "loadrule: calloc");
1159 if ((loadanchor->anchorname = malloc(MAXPATHLEN)) ==
1161 err(1, "loadrule: malloc");
1162 if (pf->anchor->name[0])
1163 snprintf(loadanchor->anchorname, MAXPATHLEN,
1164 "%s/%s", pf->anchor->name, $3);
1166 strlcpy(loadanchor->anchorname, $3, MAXPATHLEN);
1167 if ((loadanchor->filename = strdup($5)) == NULL)
1168 err(1, "loadrule: strdup");
1170 TAILQ_INSERT_TAIL(&loadanchorshead, loadanchor,
1177 scrubaction : no SCRUB {
1186 etherrule : ETHER action dir quick interface etherproto etherfromto l3fromto etherfilter_opts
1188 struct pfctl_eth_rule r;
1190 bzero(&r, sizeof(r));
1192 if (check_rulestate(PFCTL_STATE_ETHER))
1199 memcpy(&r.tagname, $9.tag, sizeof(r.tagname));
1200 if ($9.queues.qname != NULL)
1201 memcpy(&r.qname, $9.queues.qname, sizeof(r.qname));
1202 r.dnpipe = $9.dnpipe;
1203 r.dnflags = $9.free_flags;
1205 expand_eth_rule(&r, $5, $6, $7.src, $7.dst,
1206 $8.src.host, $8.dst.host, "");
1210 etherpfa_anchorlist : /* empty */
1211 | etherpfa_anchorlist '\n'
1212 | etherpfa_anchorlist etherrule '\n'
1213 | etherpfa_anchorlist etheranchorrule '\n'
1216 etherpfa_anchor : '{'
1218 char ta[PF_ANCHOR_NAME_SIZE];
1219 struct pfctl_eth_ruleset *rs;
1221 /* steping into a brace anchor */
1225 /* create a holding ruleset in the root */
1226 snprintf(ta, PF_ANCHOR_NAME_SIZE, "_%d", pf->bn);
1227 rs = pf_find_or_create_eth_ruleset(ta);
1229 err(1, "etherpfa_anchor: pf_find_or_create_eth_ruleset");
1230 pf->eastack[pf->asd] = rs->anchor;
1231 pf->eanchor = rs->anchor;
1232 } '\n' etherpfa_anchorlist '}'
1234 pf->ealast = pf->eanchor;
1236 pf->eanchor = pf->eastack[pf->asd];
1241 etheranchorrule : ETHER ANCHOR anchorname dir quick interface etherproto etherfromto l3fromto etherpfa_anchor
1243 struct pfctl_eth_rule r;
1245 if (check_rulestate(PFCTL_STATE_ETHER)) {
1250 if ($3 && ($3[0] == '_' || strstr($3, "/_") != NULL)) {
1252 yyerror("anchor names beginning with '_' "
1253 "are reserved for internal use");
1257 memset(&r, 0, sizeof(r));
1258 if (pf->eastack[pf->asd + 1]) {
1259 /* move inline rules into relative location */
1260 pfctl_eth_anchor_setup(pf, &r,
1261 &pf->eastack[pf->asd]->ruleset,
1262 $3 ? $3 : pf->ealast->name);
1263 if (r.anchor == NULL)
1264 err(1, "etheranchorrule: unable to "
1267 if (pf->ealast != r.anchor) {
1268 if (r.anchor->match) {
1269 yyerror("inline anchor '%s' "
1274 mv_eth_rules(&pf->ealast->ruleset,
1275 &r.anchor->ruleset);
1277 pf_remove_if_empty_eth_ruleset(&pf->ealast->ruleset);
1278 pf->ealast = r.anchor;
1281 yyerror("anchors without explicit "
1282 "rules must specify a name");
1290 expand_eth_rule(&r, $6, $7, $8.src, $8.dst,
1291 $9.src.host, $9.dst.host,
1292 pf->eastack[pf->asd + 1] ? pf->ealast->name : $3);
1295 pf->eastack[pf->asd + 1] = NULL;
1299 etherfilter_opts : {
1300 bzero(&filter_opts, sizeof filter_opts);
1303 { $$ = filter_opts; }
1305 bzero(&filter_opts, sizeof filter_opts);
1310 etherfilter_opts_l : etherfilter_opts_l etherfilter_opt
1313 etherfilter_opt : etherqname {
1314 if (filter_opts.queues.qname) {
1315 yyerror("queue cannot be redefined");
1318 filter_opts.queues = $1;
1321 filter_opts.tag = $2;
1324 filter_opts.dnpipe = $2;
1325 filter_opts.free_flags |= PFRULE_DN_IS_PIPE;
1328 filter_opts.dnpipe = $2;
1329 filter_opts.free_flags |= PFRULE_DN_IS_QUEUE;
1333 scrubrule : scrubaction dir logquick interface af proto fromto scrub_opts
1335 struct pfctl_rule r;
1337 if (check_rulestate(PFCTL_STATE_SCRUB))
1340 memset(&r, 0, sizeof(r));
1348 yyerror("scrub rules do not support 'quick'");
1354 r.rule_flag |= PFRULE_NODF;
1356 r.rule_flag |= PFRULE_RANDOMID;
1357 if ($8.reassemble_tcp) {
1358 if (r.direction != PF_INOUT) {
1359 yyerror("reassemble tcp rules can not "
1360 "specify direction");
1363 r.rule_flag |= PFRULE_REASSEMBLE_TCP;
1366 r.min_ttl = $8.minttl;
1368 r.max_mss = $8.maxmss;
1369 if ($8.marker & SOM_SETTOS) {
1370 r.rule_flag |= PFRULE_SET_TOS;
1371 r.set_tos = $8.settos;
1374 r.rule_flag |= $8.fragcache;
1376 if (strlcpy(r.match_tagname, $8.match_tag,
1377 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1378 yyerror("tag too long, max %u chars",
1379 PF_TAG_NAME_SIZE - 1);
1382 r.match_tag_not = $8.match_tag_not;
1383 r.rtableid = $8.rtableid;
1385 expand_rule(&r, $4, NULL, $6, $7.src_os,
1386 $7.src.host, $7.src.port, $7.dst.host, $7.dst.port,
1387 NULL, NULL, NULL, "");
1392 bzero(&scrub_opts, sizeof scrub_opts);
1393 scrub_opts.rtableid = -1;
1396 { $$ = scrub_opts; }
1398 bzero(&scrub_opts, sizeof scrub_opts);
1399 scrub_opts.rtableid = -1;
1404 scrub_opts_l : scrub_opts_l scrub_opt
1409 if (scrub_opts.nodf) {
1410 yyerror("no-df cannot be respecified");
1413 scrub_opts.nodf = 1;
1416 if (scrub_opts.marker & SOM_MINTTL) {
1417 yyerror("min-ttl cannot be respecified");
1420 if ($2 < 0 || $2 > 255) {
1421 yyerror("illegal min-ttl value %d", $2);
1424 scrub_opts.marker |= SOM_MINTTL;
1425 scrub_opts.minttl = $2;
1428 if (scrub_opts.marker & SOM_MAXMSS) {
1429 yyerror("max-mss cannot be respecified");
1432 if ($2 < 0 || $2 > 65535) {
1433 yyerror("illegal max-mss value %d", $2);
1436 scrub_opts.marker |= SOM_MAXMSS;
1437 scrub_opts.maxmss = $2;
1440 if (scrub_opts.marker & SOM_SETTOS) {
1441 yyerror("set-tos cannot be respecified");
1444 scrub_opts.marker |= SOM_SETTOS;
1445 scrub_opts.settos = $2;
1448 if (scrub_opts.marker & SOM_FRAGCACHE) {
1449 yyerror("fragcache cannot be respecified");
1452 scrub_opts.marker |= SOM_FRAGCACHE;
1453 scrub_opts.fragcache = $1;
1455 | REASSEMBLE STRING {
1456 if (strcasecmp($2, "tcp") != 0) {
1457 yyerror("scrub reassemble supports only tcp, "
1463 if (scrub_opts.reassemble_tcp) {
1464 yyerror("reassemble tcp cannot be respecified");
1467 scrub_opts.reassemble_tcp = 1;
1470 if (scrub_opts.randomid) {
1471 yyerror("random-id cannot be respecified");
1474 scrub_opts.randomid = 1;
1477 if ($2 < 0 || $2 > rt_tableid_max()) {
1478 yyerror("invalid rtable id");
1481 scrub_opts.rtableid = $2;
1483 | not TAGGED string {
1484 scrub_opts.match_tag = $3;
1485 scrub_opts.match_tag_not = $1;
1489 fragcache : FRAGMENT REASSEMBLE { $$ = 0; /* default */ }
1490 | FRAGMENT FRAGCROP { $$ = 0; }
1491 | FRAGMENT FRAGDROP { $$ = 0; }
1494 antispoof : ANTISPOOF logquick antispoof_ifspc af antispoof_opts {
1495 struct pfctl_rule r;
1496 struct node_host *h = NULL, *hh;
1497 struct node_if *i, *j;
1499 if (check_rulestate(PFCTL_STATE_FILTER))
1502 for (i = $3; i; i = i->next) {
1503 bzero(&r, sizeof(r));
1506 r.direction = PF_IN;
1511 r.ridentifier = $5.ridentifier;
1512 if (rule_label(&r, $5.label))
1514 r.rtableid = $5.rtableid;
1515 j = calloc(1, sizeof(struct node_if));
1517 err(1, "antispoof: calloc");
1518 if (strlcpy(j->ifname, i->ifname,
1519 sizeof(j->ifname)) >= sizeof(j->ifname)) {
1521 yyerror("interface name too long");
1526 h = calloc(1, sizeof(*h));
1528 err(1, "address: calloc");
1529 h->addr.type = PF_ADDR_DYNIFTL;
1531 if (strlcpy(h->addr.v.ifname, i->ifname,
1532 sizeof(h->addr.v.ifname)) >=
1533 sizeof(h->addr.v.ifname)) {
1536 "interface name too long");
1539 hh = malloc(sizeof(*hh));
1541 err(1, "address: malloc");
1542 bcopy(h, hh, sizeof(*hh));
1543 h->addr.iflags = PFI_AFLAG_NETWORK;
1545 h = ifa_lookup(j->ifname,
1551 expand_rule(&r, j, NULL, NULL, NULL, h,
1552 NULL, NULL, NULL, NULL, NULL,
1555 if ((i->ifa_flags & IFF_LOOPBACK) == 0) {
1556 bzero(&r, sizeof(r));
1559 r.direction = PF_IN;
1564 r.ridentifier = $5.ridentifier;
1565 if (rule_label(&r, $5.label))
1567 r.rtableid = $5.rtableid;
1571 h = ifa_lookup(i->ifname, 0);
1573 expand_rule(&r, NULL, NULL,
1574 NULL, NULL, h, NULL, NULL,
1575 NULL, NULL, NULL, NULL, "");
1579 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++)
1584 antispoof_ifspc : FOR antispoof_if { $$ = $2; }
1585 | FOR '{' optnl antispoof_iflst '}' { $$ = $4; }
1588 antispoof_iflst : antispoof_if optnl { $$ = $1; }
1589 | antispoof_iflst comma antispoof_if optnl {
1590 $1->tail->next = $3;
1596 antispoof_if : if_item { $$ = $1; }
1604 bzero(&antispoof_opts, sizeof antispoof_opts);
1605 antispoof_opts.rtableid = -1;
1608 { $$ = antispoof_opts; }
1610 bzero(&antispoof_opts, sizeof antispoof_opts);
1611 antispoof_opts.rtableid = -1;
1612 $$ = antispoof_opts;
1616 antispoof_opts_l : antispoof_opts_l antispoof_opt
1620 antispoof_opt : label {
1621 if (antispoof_opts.labelcount >= PF_RULE_MAX_LABEL_COUNT) {
1622 yyerror("label can only be used %d times", PF_RULE_MAX_LABEL_COUNT);
1625 antispoof_opts.label[antispoof_opts.labelcount++] = $1;
1627 | RIDENTIFIER number {
1628 antispoof_opts.ridentifier = $2;
1631 if ($2 < 0 || $2 > rt_tableid_max()) {
1632 yyerror("invalid rtable id");
1635 antispoof_opts.rtableid = $2;
1639 not : '!' { $$ = 1; }
1640 | /* empty */ { $$ = 0; }
1643 tabledef : TABLE '<' STRING '>' table_opts {
1644 struct node_host *h, *nh;
1645 struct node_tinit *ti, *nti;
1647 if (strlen($3) >= PF_TABLE_NAME_SIZE) {
1648 yyerror("table name too long, max %d chars",
1649 PF_TABLE_NAME_SIZE - 1);
1653 if (pf->loadopt & PFCTL_FLAG_TABLE)
1654 if (process_tabledef($3, &$5)) {
1659 for (ti = SIMPLEQ_FIRST(&$5.init_nodes);
1660 ti != SIMPLEQ_END(&$5.init_nodes); ti = nti) {
1663 for (h = ti->host; h != NULL; h = nh) {
1667 nti = SIMPLEQ_NEXT(ti, entries);
1674 bzero(&table_opts, sizeof table_opts);
1675 SIMPLEQ_INIT(&table_opts.init_nodes);
1678 { $$ = table_opts; }
1681 bzero(&table_opts, sizeof table_opts);
1682 SIMPLEQ_INIT(&table_opts.init_nodes);
1687 table_opts_l : table_opts_l table_opt
1691 table_opt : STRING {
1692 if (!strcmp($1, "const"))
1693 table_opts.flags |= PFR_TFLAG_CONST;
1694 else if (!strcmp($1, "persist"))
1695 table_opts.flags |= PFR_TFLAG_PERSIST;
1696 else if (!strcmp($1, "counters"))
1697 table_opts.flags |= PFR_TFLAG_COUNTERS;
1699 yyerror("invalid table option '%s'", $1);
1705 | '{' optnl '}' { table_opts.init_addr = 1; }
1706 | '{' optnl host_list '}' {
1707 struct node_host *n;
1708 struct node_tinit *ti;
1710 for (n = $3; n != NULL; n = n->next) {
1711 switch (n->addr.type) {
1712 case PF_ADDR_ADDRMASK:
1715 yyerror("address ranges are not "
1716 "permitted inside tables");
1718 case PF_ADDR_DYNIFTL:
1719 yyerror("dynamic addresses are not "
1720 "permitted inside tables");
1723 yyerror("tables cannot contain tables");
1725 case PF_ADDR_NOROUTE:
1726 yyerror("\"no-route\" is not permitted "
1729 case PF_ADDR_URPFFAILED:
1730 yyerror("\"urpf-failed\" is not "
1731 "permitted inside tables");
1734 yyerror("unknown address type %d",
1739 if (!(ti = calloc(1, sizeof(*ti))))
1740 err(1, "table_opt: calloc");
1742 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1744 table_opts.init_addr = 1;
1747 struct node_tinit *ti;
1749 if (!(ti = calloc(1, sizeof(*ti))))
1750 err(1, "table_opt: calloc");
1752 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1754 table_opts.init_addr = 1;
1758 altqif : ALTQ interface queue_opts QUEUE qassign {
1761 if (check_rulestate(PFCTL_STATE_QUEUE))
1764 memset(&a, 0, sizeof(a));
1765 if ($3.scheduler.qtype == ALTQT_NONE) {
1766 yyerror("no scheduler specified!");
1769 a.scheduler = $3.scheduler.qtype;
1770 a.qlimit = $3.qlimit;
1771 a.tbrsize = $3.tbrsize;
1772 if ($5 == NULL && $3.scheduler.qtype != ALTQT_CODEL) {
1773 yyerror("no child queues specified");
1776 if (expand_altq(&a, $2, $5, $3.queue_bwspec,
1782 queuespec : QUEUE STRING interface queue_opts qassign {
1785 if (check_rulestate(PFCTL_STATE_QUEUE)) {
1790 memset(&a, 0, sizeof(a));
1792 if (strlcpy(a.qname, $2, sizeof(a.qname)) >=
1794 yyerror("queue name too long (max "
1795 "%d chars)", PF_QNAME_SIZE-1);
1801 yyerror("cannot specify tbrsize for queue");
1804 if ($4.priority > 255) {
1805 yyerror("priority out of range: max 255");
1808 a.priority = $4.priority;
1809 a.qlimit = $4.qlimit;
1810 a.scheduler = $4.scheduler.qtype;
1811 if (expand_queue(&a, $3, $5, $4.queue_bwspec,
1813 yyerror("errors in queue definition");
1820 bzero(&queue_opts, sizeof queue_opts);
1821 queue_opts.priority = DEFAULT_PRIORITY;
1822 queue_opts.qlimit = DEFAULT_QLIMIT;
1823 queue_opts.scheduler.qtype = ALTQT_NONE;
1824 queue_opts.queue_bwspec.bw_percent = 100;
1827 { $$ = queue_opts; }
1829 bzero(&queue_opts, sizeof queue_opts);
1830 queue_opts.priority = DEFAULT_PRIORITY;
1831 queue_opts.qlimit = DEFAULT_QLIMIT;
1832 queue_opts.scheduler.qtype = ALTQT_NONE;
1833 queue_opts.queue_bwspec.bw_percent = 100;
1838 queue_opts_l : queue_opts_l queue_opt
1842 queue_opt : BANDWIDTH bandwidth {
1843 if (queue_opts.marker & QOM_BWSPEC) {
1844 yyerror("bandwidth cannot be respecified");
1847 queue_opts.marker |= QOM_BWSPEC;
1848 queue_opts.queue_bwspec = $2;
1851 if (queue_opts.marker & QOM_PRIORITY) {
1852 yyerror("priority cannot be respecified");
1855 if ($2 < 0 || $2 > 255) {
1856 yyerror("priority out of range: max 255");
1859 queue_opts.marker |= QOM_PRIORITY;
1860 queue_opts.priority = $2;
1863 if (queue_opts.marker & QOM_QLIMIT) {
1864 yyerror("qlimit cannot be respecified");
1867 if ($2 < 0 || $2 > 65535) {
1868 yyerror("qlimit out of range: max 65535");
1871 queue_opts.marker |= QOM_QLIMIT;
1872 queue_opts.qlimit = $2;
1875 if (queue_opts.marker & QOM_SCHEDULER) {
1876 yyerror("scheduler cannot be respecified");
1879 queue_opts.marker |= QOM_SCHEDULER;
1880 queue_opts.scheduler = $1;
1883 if (queue_opts.marker & QOM_TBRSIZE) {
1884 yyerror("tbrsize cannot be respecified");
1887 if ($2 < 0 || $2 > UINT_MAX) {
1888 yyerror("tbrsize too big: max %u", UINT_MAX);
1891 queue_opts.marker |= QOM_TBRSIZE;
1892 queue_opts.tbrsize = $2;
1896 bandwidth : STRING {
1902 bps = strtod($1, &cp);
1904 if (strlen(cp) > 1) {
1906 if (!strcmp(cu, "Bit") ||
1908 !strcmp(cu, "bit") ||
1913 if (!strcmp(cp, "b"))
1915 else if (!strcmp(cp, "K"))
1917 else if (!strcmp(cp, "M"))
1919 else if (!strcmp(cp, "G"))
1920 bps *= 1000 * 1000 * 1000;
1921 else if (!strcmp(cp, "%")) {
1922 if (bps < 0 || bps > 100) {
1923 yyerror("bandwidth spec "
1928 $$.bw_percent = bps;
1931 yyerror("unknown unit %s", cp);
1937 $$.bw_absolute = (u_int64_t)bps;
1940 if ($1 < 0 || $1 >= LLONG_MAX) {
1941 yyerror("bandwidth number too big");
1945 $$.bw_absolute = $1;
1950 $$.qtype = ALTQT_CBQ;
1951 $$.data.cbq_opts.flags = 0;
1953 | CBQ '(' cbqflags_list ')' {
1954 $$.qtype = ALTQT_CBQ;
1955 $$.data.cbq_opts.flags = $3;
1958 $$.qtype = ALTQT_PRIQ;
1959 $$.data.priq_opts.flags = 0;
1961 | PRIQ '(' priqflags_list ')' {
1962 $$.qtype = ALTQT_PRIQ;
1963 $$.data.priq_opts.flags = $3;
1966 $$.qtype = ALTQT_HFSC;
1967 bzero(&$$.data.hfsc_opts,
1968 sizeof(struct node_hfsc_opts));
1970 | HFSC '(' hfsc_opts ')' {
1971 $$.qtype = ALTQT_HFSC;
1972 $$.data.hfsc_opts = $3;
1975 $$.qtype = ALTQT_FAIRQ;
1976 bzero(&$$.data.fairq_opts,
1977 sizeof(struct node_fairq_opts));
1979 | FAIRQ '(' fairq_opts ')' {
1980 $$.qtype = ALTQT_FAIRQ;
1981 $$.data.fairq_opts = $3;
1984 $$.qtype = ALTQT_CODEL;
1985 bzero(&$$.data.codel_opts,
1986 sizeof(struct codel_opts));
1988 | CODEL '(' codel_opts ')' {
1989 $$.qtype = ALTQT_CODEL;
1990 $$.data.codel_opts = $3;
1994 cbqflags_list : cbqflags_item { $$ |= $1; }
1995 | cbqflags_list comma cbqflags_item { $$ |= $3; }
1998 cbqflags_item : STRING {
1999 if (!strcmp($1, "default"))
2000 $$ = CBQCLF_DEFCLASS;
2001 else if (!strcmp($1, "borrow"))
2003 else if (!strcmp($1, "red"))
2005 else if (!strcmp($1, "ecn"))
2006 $$ = CBQCLF_RED|CBQCLF_ECN;
2007 else if (!strcmp($1, "rio"))
2009 else if (!strcmp($1, "codel"))
2012 yyerror("unknown cbq flag \"%s\"", $1);
2020 priqflags_list : priqflags_item { $$ |= $1; }
2021 | priqflags_list comma priqflags_item { $$ |= $3; }
2024 priqflags_item : STRING {
2025 if (!strcmp($1, "default"))
2026 $$ = PRCF_DEFAULTCLASS;
2027 else if (!strcmp($1, "red"))
2029 else if (!strcmp($1, "ecn"))
2030 $$ = PRCF_RED|PRCF_ECN;
2031 else if (!strcmp($1, "rio"))
2033 else if (!strcmp($1, "codel"))
2036 yyerror("unknown priq flag \"%s\"", $1);
2046 sizeof(struct node_hfsc_opts));
2053 hfscopts_list : hfscopts_item
2054 | hfscopts_list comma hfscopts_item
2057 hfscopts_item : LINKSHARE bandwidth {
2058 if (hfsc_opts.linkshare.used) {
2059 yyerror("linkshare already specified");
2062 hfsc_opts.linkshare.m2 = $2;
2063 hfsc_opts.linkshare.used = 1;
2065 | LINKSHARE '(' bandwidth comma NUMBER comma bandwidth ')'
2067 if ($5 < 0 || $5 > INT_MAX) {
2068 yyerror("timing in curve out of range");
2071 if (hfsc_opts.linkshare.used) {
2072 yyerror("linkshare already specified");
2075 hfsc_opts.linkshare.m1 = $3;
2076 hfsc_opts.linkshare.d = $5;
2077 hfsc_opts.linkshare.m2 = $7;
2078 hfsc_opts.linkshare.used = 1;
2080 | REALTIME bandwidth {
2081 if (hfsc_opts.realtime.used) {
2082 yyerror("realtime already specified");
2085 hfsc_opts.realtime.m2 = $2;
2086 hfsc_opts.realtime.used = 1;
2088 | REALTIME '(' bandwidth comma NUMBER comma bandwidth ')'
2090 if ($5 < 0 || $5 > INT_MAX) {
2091 yyerror("timing in curve out of range");
2094 if (hfsc_opts.realtime.used) {
2095 yyerror("realtime already specified");
2098 hfsc_opts.realtime.m1 = $3;
2099 hfsc_opts.realtime.d = $5;
2100 hfsc_opts.realtime.m2 = $7;
2101 hfsc_opts.realtime.used = 1;
2103 | UPPERLIMIT bandwidth {
2104 if (hfsc_opts.upperlimit.used) {
2105 yyerror("upperlimit already specified");
2108 hfsc_opts.upperlimit.m2 = $2;
2109 hfsc_opts.upperlimit.used = 1;
2111 | UPPERLIMIT '(' bandwidth comma NUMBER comma bandwidth ')'
2113 if ($5 < 0 || $5 > INT_MAX) {
2114 yyerror("timing in curve out of range");
2117 if (hfsc_opts.upperlimit.used) {
2118 yyerror("upperlimit already specified");
2121 hfsc_opts.upperlimit.m1 = $3;
2122 hfsc_opts.upperlimit.d = $5;
2123 hfsc_opts.upperlimit.m2 = $7;
2124 hfsc_opts.upperlimit.used = 1;
2127 if (!strcmp($1, "default"))
2128 hfsc_opts.flags |= HFCF_DEFAULTCLASS;
2129 else if (!strcmp($1, "red"))
2130 hfsc_opts.flags |= HFCF_RED;
2131 else if (!strcmp($1, "ecn"))
2132 hfsc_opts.flags |= HFCF_RED|HFCF_ECN;
2133 else if (!strcmp($1, "rio"))
2134 hfsc_opts.flags |= HFCF_RIO;
2135 else if (!strcmp($1, "codel"))
2136 hfsc_opts.flags |= HFCF_CODEL;
2138 yyerror("unknown hfsc flag \"%s\"", $1);
2148 sizeof(struct node_fairq_opts));
2155 fairqopts_list : fairqopts_item
2156 | fairqopts_list comma fairqopts_item
2159 fairqopts_item : LINKSHARE bandwidth {
2160 if (fairq_opts.linkshare.used) {
2161 yyerror("linkshare already specified");
2164 fairq_opts.linkshare.m2 = $2;
2165 fairq_opts.linkshare.used = 1;
2167 | LINKSHARE '(' bandwidth number bandwidth ')' {
2168 if (fairq_opts.linkshare.used) {
2169 yyerror("linkshare already specified");
2172 fairq_opts.linkshare.m1 = $3;
2173 fairq_opts.linkshare.d = $4;
2174 fairq_opts.linkshare.m2 = $5;
2175 fairq_opts.linkshare.used = 1;
2178 fairq_opts.hogs_bw = $2;
2181 fairq_opts.nbuckets = $2;
2184 if (!strcmp($1, "default"))
2185 fairq_opts.flags |= FARF_DEFAULTCLASS;
2186 else if (!strcmp($1, "red"))
2187 fairq_opts.flags |= FARF_RED;
2188 else if (!strcmp($1, "ecn"))
2189 fairq_opts.flags |= FARF_RED|FARF_ECN;
2190 else if (!strcmp($1, "rio"))
2191 fairq_opts.flags |= FARF_RIO;
2192 else if (!strcmp($1, "codel"))
2193 fairq_opts.flags |= FARF_CODEL;
2195 yyerror("unknown fairq flag \"%s\"", $1);
2205 sizeof(struct codel_opts));
2212 codelopts_list : codelopts_item
2213 | codelopts_list comma codelopts_item
2216 codelopts_item : INTERVAL number {
2217 if (codel_opts.interval) {
2218 yyerror("interval already specified");
2221 codel_opts.interval = $2;
2224 if (codel_opts.target) {
2225 yyerror("target already specified");
2228 codel_opts.target = $2;
2231 if (!strcmp($1, "ecn"))
2234 yyerror("unknown codel option \"%s\"", $1);
2242 qassign : /* empty */ { $$ = NULL; }
2243 | qassign_item { $$ = $1; }
2244 | '{' optnl qassign_list '}' { $$ = $3; }
2247 qassign_list : qassign_item optnl { $$ = $1; }
2248 | qassign_list comma qassign_item optnl {
2249 $1->tail->next = $3;
2255 qassign_item : STRING {
2256 $$ = calloc(1, sizeof(struct node_queue));
2258 err(1, "qassign_item: calloc");
2259 if (strlcpy($$->queue, $1, sizeof($$->queue)) >=
2260 sizeof($$->queue)) {
2261 yyerror("queue name '%s' too long (max "
2262 "%d chars)", $1, sizeof($$->queue)-1);
2273 pfrule : action dir logquick interface route af proto fromto
2276 struct pfctl_rule r;
2277 struct node_state_opt *o;
2278 struct node_proto *proto;
2284 if (check_rulestate(PFCTL_STATE_FILTER))
2287 memset(&r, 0, sizeof(r));
2291 case PFRULE_RETURNRST:
2292 r.rule_flag |= PFRULE_RETURNRST;
2293 r.return_ttl = $1.w;
2295 case PFRULE_RETURNICMP:
2296 r.rule_flag |= PFRULE_RETURNICMP;
2297 r.return_icmp = $1.w;
2298 r.return_icmp6 = $1.w2;
2301 r.rule_flag |= PFRULE_RETURN;
2302 r.return_icmp = $1.w;
2303 r.return_icmp6 = $1.w2;
2311 r.rtableid = $9.rtableid;
2313 if ($9.marker & FOM_PRIO) {
2315 r.prio = PF_PRIO_ZERO;
2319 if ($9.marker & FOM_SETPRIO) {
2320 r.set_prio[0] = $9.set_prio[0];
2321 r.set_prio[1] = $9.set_prio[1];
2322 r.scrub_flags |= PFSTATE_SETPRIO;
2327 if (strlcpy(r.tagname, $9.tag,
2328 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
2329 yyerror("tag too long, max %u chars",
2330 PF_TAG_NAME_SIZE - 1);
2334 if (strlcpy(r.match_tagname, $9.match_tag,
2335 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
2336 yyerror("tag too long, max %u chars",
2337 PF_TAG_NAME_SIZE - 1);
2340 r.match_tag_not = $9.match_tag_not;
2341 if (rule_label(&r, $9.label))
2343 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++)
2345 r.ridentifier = $9.ridentifier;
2346 r.flags = $9.flags.b1;
2347 r.flagset = $9.flags.b2;
2348 if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
2349 yyerror("flags always false");
2352 if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
2353 for (proto = $7; proto != NULL &&
2354 proto->proto != IPPROTO_TCP;
2355 proto = proto->next)
2357 if (proto == NULL && $7 != NULL) {
2358 if ($9.flags.b1 || $9.flags.b2)
2360 "flags only apply to tcp");
2363 "OS fingerprinting only "
2368 if (($9.flags.b1 & parse_flags("S")) == 0 &&
2370 yyerror("OS fingerprinting requires "
2371 "the SYN TCP flag (flags S/SA)");
2378 r.keep_state = $9.keep.action;
2379 o = $9.keep.options;
2381 /* 'keep state' by default on pass rules. */
2382 if (!r.keep_state && !r.action &&
2383 !($9.marker & FOM_KEEP)) {
2384 r.keep_state = PF_STATE_NORMAL;
2385 o = keep_state_defaults;
2390 struct node_state_opt *p = o;
2393 case PF_STATE_OPT_MAX:
2395 yyerror("state option 'max' "
2396 "multiple definitions");
2399 r.max_states = o->data.max_states;
2401 case PF_STATE_OPT_NOSYNC:
2402 if (r.rule_flag & PFRULE_NOSYNC) {
2403 yyerror("state option 'sync' "
2404 "multiple definitions");
2407 r.rule_flag |= PFRULE_NOSYNC;
2409 case PF_STATE_OPT_SRCTRACK:
2411 yyerror("state option "
2413 "multiple definitions");
2416 srctrack = o->data.src_track;
2417 r.rule_flag |= PFRULE_SRCTRACK;
2419 case PF_STATE_OPT_MAX_SRC_STATES:
2420 if (r.max_src_states) {
2421 yyerror("state option "
2423 "multiple definitions");
2426 if (o->data.max_src_states == 0) {
2427 yyerror("'max-src-states' must "
2432 o->data.max_src_states;
2433 r.rule_flag |= PFRULE_SRCTRACK;
2435 case PF_STATE_OPT_OVERLOAD:
2436 if (r.overload_tblname[0]) {
2437 yyerror("multiple 'overload' "
2438 "table definitions");
2441 if (strlcpy(r.overload_tblname,
2442 o->data.overload.tblname,
2443 PF_TABLE_NAME_SIZE) >=
2444 PF_TABLE_NAME_SIZE) {
2445 yyerror("state option: "
2449 r.flush = o->data.overload.flush;
2451 case PF_STATE_OPT_MAX_SRC_CONN:
2452 if (r.max_src_conn) {
2453 yyerror("state option "
2455 "multiple definitions");
2458 if (o->data.max_src_conn == 0) {
2459 yyerror("'max-src-conn' "
2464 o->data.max_src_conn;
2465 r.rule_flag |= PFRULE_SRCTRACK |
2466 PFRULE_RULESRCTRACK;
2468 case PF_STATE_OPT_MAX_SRC_CONN_RATE:
2469 if (r.max_src_conn_rate.limit) {
2470 yyerror("state option "
2471 "'max-src-conn-rate' "
2472 "multiple definitions");
2475 if (!o->data.max_src_conn_rate.limit ||
2476 !o->data.max_src_conn_rate.seconds) {
2477 yyerror("'max-src-conn-rate' "
2478 "values must be > 0");
2481 if (o->data.max_src_conn_rate.limit >
2483 yyerror("'max-src-conn-rate' "
2484 "maximum rate must be < %u",
2488 r.max_src_conn_rate.limit =
2489 o->data.max_src_conn_rate.limit;
2490 r.max_src_conn_rate.seconds =
2491 o->data.max_src_conn_rate.seconds;
2492 r.rule_flag |= PFRULE_SRCTRACK |
2493 PFRULE_RULESRCTRACK;
2495 case PF_STATE_OPT_MAX_SRC_NODES:
2496 if (r.max_src_nodes) {
2497 yyerror("state option "
2499 "multiple definitions");
2502 if (o->data.max_src_nodes == 0) {
2503 yyerror("'max-src-nodes' must "
2508 o->data.max_src_nodes;
2509 r.rule_flag |= PFRULE_SRCTRACK |
2510 PFRULE_RULESRCTRACK;
2512 case PF_STATE_OPT_STATELOCK:
2514 yyerror("state locking option: "
2515 "multiple definitions");
2519 r.rule_flag |= o->data.statelock;
2521 case PF_STATE_OPT_SLOPPY:
2522 if (r.rule_flag & PFRULE_STATESLOPPY) {
2523 yyerror("state sloppy option: "
2524 "multiple definitions");
2527 r.rule_flag |= PFRULE_STATESLOPPY;
2529 case PF_STATE_OPT_TIMEOUT:
2530 if (o->data.timeout.number ==
2531 PFTM_ADAPTIVE_START ||
2532 o->data.timeout.number ==
2535 if (r.timeout[o->data.timeout.number]) {
2536 yyerror("state timeout %s "
2537 "multiple definitions",
2538 pf_timeouts[o->data.
2539 timeout.number].name);
2542 r.timeout[o->data.timeout.number] =
2543 o->data.timeout.seconds;
2550 /* 'flags S/SA' by default on stateful rules */
2551 if (!r.action && !r.flags && !r.flagset &&
2552 !$9.fragment && !($9.marker & FOM_FLAGS) &&
2554 r.flags = parse_flags("S");
2555 r.flagset = parse_flags("SA");
2557 if (!adaptive && r.max_states) {
2558 r.timeout[PFTM_ADAPTIVE_START] =
2559 (r.max_states / 10) * 6;
2560 r.timeout[PFTM_ADAPTIVE_END] =
2561 (r.max_states / 10) * 12;
2563 if (r.rule_flag & PFRULE_SRCTRACK) {
2564 if (srctrack == PF_SRCTRACK_GLOBAL &&
2566 yyerror("'max-src-nodes' is "
2567 "incompatible with "
2568 "'source-track global'");
2571 if (srctrack == PF_SRCTRACK_GLOBAL &&
2573 yyerror("'max-src-conn' is "
2574 "incompatible with "
2575 "'source-track global'");
2578 if (srctrack == PF_SRCTRACK_GLOBAL &&
2579 r.max_src_conn_rate.seconds) {
2580 yyerror("'max-src-conn-rate' is "
2581 "incompatible with "
2582 "'source-track global'");
2585 if (r.timeout[PFTM_SRC_NODE] <
2586 r.max_src_conn_rate.seconds)
2587 r.timeout[PFTM_SRC_NODE] =
2588 r.max_src_conn_rate.seconds;
2589 r.rule_flag |= PFRULE_SRCTRACK;
2590 if (srctrack == PF_SRCTRACK_RULE)
2591 r.rule_flag |= PFRULE_RULESRCTRACK;
2593 if (r.keep_state && !statelock)
2594 r.rule_flag |= default_statelock;
2597 r.rule_flag |= PFRULE_FRAGMENT;
2598 r.allow_opts = $9.allowopts;
2600 decide_address_family($8.src.host, &r.af);
2601 decide_address_family($8.dst.host, &r.af);
2605 yyerror("direction must be explicit "
2606 "with rules that specify routing");
2610 r.rpool.opts = $5.pool_opts;
2612 memcpy(&r.rpool.key, $5.key,
2613 sizeof(struct pf_poolhashkey));
2616 decide_address_family($5.host, &r.af);
2617 remove_invalid_hosts(&$5.host, &r.af);
2618 if ($5.host == NULL) {
2619 yyerror("no routing address with "
2620 "matching address family found.");
2623 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
2624 PF_POOL_NONE && ($5.host->next != NULL ||
2625 $5.host->addr.type == PF_ADDR_TABLE ||
2626 DYNIF_MULTIADDR($5.host->addr)))
2627 r.rpool.opts |= PF_POOL_ROUNDROBIN;
2628 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2629 PF_POOL_ROUNDROBIN &&
2630 disallow_table($5.host, "tables are only "
2631 "supported in round-robin routing pools"))
2633 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2634 PF_POOL_ROUNDROBIN &&
2635 disallow_alias($5.host, "interface (%s) "
2636 "is only supported in round-robin "
2639 if ($5.host->next != NULL) {
2640 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2641 PF_POOL_ROUNDROBIN) {
2642 yyerror("r.rpool.opts must "
2643 "be PF_POOL_ROUNDROBIN");
2648 if ($9.queues.qname != NULL) {
2649 if (strlcpy(r.qname, $9.queues.qname,
2650 sizeof(r.qname)) >= sizeof(r.qname)) {
2651 yyerror("rule qname too long (max "
2652 "%d chars)", sizeof(r.qname)-1);
2655 free($9.queues.qname);
2657 if ($9.queues.pqname != NULL) {
2658 if (strlcpy(r.pqname, $9.queues.pqname,
2659 sizeof(r.pqname)) >= sizeof(r.pqname)) {
2660 yyerror("rule pqname too long (max "
2661 "%d chars)", sizeof(r.pqname)-1);
2664 free($9.queues.pqname);
2667 r.divert.port = $9.divert.port;
2669 if ((r.divert.port = $9.divert.port)) {
2670 if (r.direction == PF_OUT) {
2671 if ($9.divert.addr) {
2672 yyerror("address specified "
2673 "for outgoing divert");
2676 bzero(&r.divert.addr,
2677 sizeof(r.divert.addr));
2679 if (!$9.divert.addr) {
2680 yyerror("no address specified "
2681 "for incoming divert");
2684 if ($9.divert.addr->af != r.af) {
2685 yyerror("address family "
2686 "mismatch for divert");
2690 $9.divert.addr->addr.v.a.addr;
2695 if ($9.dnpipe || $9.dnrpipe) {
2696 r.dnpipe = $9.dnpipe;
2697 r.dnrpipe = $9.dnrpipe;
2698 if ($9.free_flags & PFRULE_DN_IS_PIPE)
2699 r.free_flags |= PFRULE_DN_IS_PIPE;
2701 r.free_flags |= PFRULE_DN_IS_QUEUE;
2704 expand_rule(&r, $4, $5.host, $7, $8.src_os,
2705 $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
2706 $9.uid, $9.gid, $9.icmpspec, "");
2711 bzero(&filter_opts, sizeof filter_opts);
2712 filter_opts.rtableid = -1;
2715 { $$ = filter_opts; }
2717 bzero(&filter_opts, sizeof filter_opts);
2718 filter_opts.rtableid = -1;
2723 filter_opts_l : filter_opts_l filter_opt
2727 filter_opt : USER uids {
2728 if (filter_opts.uid)
2729 $2->tail->next = filter_opts.uid;
2730 filter_opts.uid = $2;
2733 if (filter_opts.gid)
2734 $2->tail->next = filter_opts.gid;
2735 filter_opts.gid = $2;
2738 if (filter_opts.marker & FOM_FLAGS) {
2739 yyerror("flags cannot be redefined");
2742 filter_opts.marker |= FOM_FLAGS;
2743 filter_opts.flags.b1 |= $1.b1;
2744 filter_opts.flags.b2 |= $1.b2;
2745 filter_opts.flags.w |= $1.w;
2746 filter_opts.flags.w2 |= $1.w2;
2749 if (filter_opts.marker & FOM_ICMP) {
2750 yyerror("icmp-type cannot be redefined");
2753 filter_opts.marker |= FOM_ICMP;
2754 filter_opts.icmpspec = $1;
2757 if (filter_opts.marker & FOM_PRIO) {
2758 yyerror("prio cannot be redefined");
2761 if ($2 < 0 || $2 > PF_PRIO_MAX) {
2762 yyerror("prio must be 0 - %u", PF_PRIO_MAX);
2765 filter_opts.marker |= FOM_PRIO;
2766 filter_opts.prio = $2;
2769 if (filter_opts.marker & FOM_TOS) {
2770 yyerror("tos cannot be redefined");
2773 filter_opts.marker |= FOM_TOS;
2774 filter_opts.tos = $2;
2777 if (filter_opts.marker & FOM_KEEP) {
2778 yyerror("modulate or keep cannot be redefined");
2781 filter_opts.marker |= FOM_KEEP;
2782 filter_opts.keep.action = $1.action;
2783 filter_opts.keep.options = $1.options;
2785 | RIDENTIFIER number {
2786 filter_opts.ridentifier = $2;
2789 filter_opts.fragment = 1;
2792 filter_opts.allowopts = 1;
2795 if (filter_opts.labelcount >= PF_RULE_MAX_LABEL_COUNT) {
2796 yyerror("label can only be used %d times", PF_RULE_MAX_LABEL_COUNT);
2799 filter_opts.label[filter_opts.labelcount++] = $1;
2802 if (filter_opts.queues.qname) {
2803 yyerror("queue cannot be redefined");
2806 filter_opts.queues = $1;
2809 filter_opts.dnpipe = $2;
2810 filter_opts.free_flags |= PFRULE_DN_IS_PIPE;
2812 | DNPIPE '(' number ')' {
2813 filter_opts.dnpipe = $3;
2814 filter_opts.free_flags |= PFRULE_DN_IS_PIPE;
2816 | DNPIPE '(' number comma number ')' {
2817 filter_opts.dnrpipe = $5;
2818 filter_opts.dnpipe = $3;
2819 filter_opts.free_flags |= PFRULE_DN_IS_PIPE;
2822 filter_opts.dnpipe = $2;
2823 filter_opts.free_flags |= PFRULE_DN_IS_QUEUE;
2825 | DNQUEUE '(' number comma number ')' {
2826 filter_opts.dnrpipe = $5;
2827 filter_opts.dnpipe = $3;
2828 filter_opts.free_flags |= PFRULE_DN_IS_QUEUE;
2830 | DNQUEUE '(' number ')' {
2831 filter_opts.dnpipe = $3;
2832 filter_opts.free_flags |= PFRULE_DN_IS_QUEUE;
2835 filter_opts.tag = $2;
2837 | not TAGGED string {
2838 filter_opts.match_tag = $3;
2839 filter_opts.match_tag_not = $1;
2841 | PROBABILITY probability {
2844 p = floor($2 * UINT_MAX + 0.5);
2845 if (p < 0.0 || p > UINT_MAX) {
2846 yyerror("invalid probability: %lf", p);
2849 filter_opts.prob = (u_int32_t)p;
2850 if (filter_opts.prob == 0)
2851 filter_opts.prob = 1;
2854 if ($2 < 0 || $2 > rt_tableid_max()) {
2855 yyerror("invalid rtable id");
2858 filter_opts.rtableid = $2;
2860 | DIVERTTO portplain {
2862 filter_opts.divert.port = $2.a;
2863 if (!filter_opts.divert.port) {
2864 yyerror("invalid divert port: %u", ntohs($2.a));
2869 | DIVERTTO STRING PORT portplain {
2871 if ((filter_opts.divert.addr = host($2)) == NULL) {
2872 yyerror("could not parse divert address: %s",
2881 filter_opts.divert.port = $4.a;
2882 if (!filter_opts.divert.port) {
2883 yyerror("invalid divert port: %u", ntohs($4.a));
2889 yyerror("divert-reply has no meaning in FreeBSD pf(4)");
2892 filter_opts.divert.port = 1; /* some random value */
2898 filter_sets : SET '(' filter_sets_l ')' { $$ = filter_opts; }
2899 | SET filter_set { $$ = filter_opts; }
2902 filter_sets_l : filter_sets_l comma filter_set
2907 if (filter_opts.marker & FOM_SETPRIO) {
2908 yyerror("prio cannot be redefined");
2911 filter_opts.marker |= FOM_SETPRIO;
2912 filter_opts.set_prio[0] = $1.b1;
2913 filter_opts.set_prio[1] = $1.b2;
2915 prio : PRIO NUMBER {
2916 if ($2 < 0 || $2 > PF_PRIO_MAX) {
2917 yyerror("prio must be 0 - %u", PF_PRIO_MAX);
2922 | PRIO '(' NUMBER comma NUMBER ')' {
2923 if ($3 < 0 || $3 > PF_PRIO_MAX ||
2924 $5 < 0 || $5 > PF_PRIO_MAX) {
2925 yyerror("prio must be 0 - %u", PF_PRIO_MAX);
2933 probability : STRING {
2935 double p = strtod($1, &e);
2942 yyerror("invalid probability: %s", $1);
2958 $$.w = returnicmpdefault;
2959 $$.w2 = returnicmp6default;
2961 | MATCH { $$.b1 = PF_MATCH; $$.b2 = $$.w = 0; }
2962 | BLOCK blockspec { $$ = $2; $$.b1 = PF_DROP; }
2965 blockspec : /* empty */ {
2966 $$.b2 = blockpolicy;
2967 $$.w = returnicmpdefault;
2968 $$.w2 = returnicmp6default;
2971 $$.b2 = PFRULE_DROP;
2976 $$.b2 = PFRULE_RETURNRST;
2980 | RETURNRST '(' TTL NUMBER ')' {
2981 if ($4 < 0 || $4 > 255) {
2982 yyerror("illegal ttl value %d", $4);
2985 $$.b2 = PFRULE_RETURNRST;
2990 $$.b2 = PFRULE_RETURNICMP;
2991 $$.w = returnicmpdefault;
2992 $$.w2 = returnicmp6default;
2995 $$.b2 = PFRULE_RETURNICMP;
2996 $$.w = returnicmpdefault;
2997 $$.w2 = returnicmp6default;
2999 | RETURNICMP '(' reticmpspec ')' {
3000 $$.b2 = PFRULE_RETURNICMP;
3002 $$.w2 = returnicmpdefault;
3004 | RETURNICMP6 '(' reticmp6spec ')' {
3005 $$.b2 = PFRULE_RETURNICMP;
3006 $$.w = returnicmpdefault;
3009 | RETURNICMP '(' reticmpspec comma reticmp6spec ')' {
3010 $$.b2 = PFRULE_RETURNICMP;
3015 $$.b2 = PFRULE_RETURN;
3016 $$.w = returnicmpdefault;
3017 $$.w2 = returnicmp6default;
3021 reticmpspec : STRING {
3022 if (!($$ = parseicmpspec($1, AF_INET))) {
3031 if ($1 < 0 || $1 > 255) {
3032 yyerror("invalid icmp code %lu", $1);
3035 icmptype = returnicmpdefault >> 8;
3036 $$ = (icmptype << 8 | $1);
3040 reticmp6spec : STRING {
3041 if (!($$ = parseicmpspec($1, AF_INET6))) {
3050 if ($1 < 0 || $1 > 255) {
3051 yyerror("invalid icmp code %lu", $1);
3054 icmptype = returnicmp6default >> 8;
3055 $$ = (icmptype << 8 | $1);
3059 dir : /* empty */ { $$ = PF_INOUT; }
3060 | IN { $$ = PF_IN; }
3061 | OUT { $$ = PF_OUT; }
3064 quick : /* empty */ { $$.quick = 0; }
3065 | QUICK { $$.quick = 1; }
3068 logquick : /* empty */ { $$.log = 0; $$.quick = 0; $$.logif = 0; }
3069 | log { $$ = $1; $$.quick = 0; }
3070 | QUICK { $$.quick = 1; $$.log = 0; $$.logif = 0; }
3071 | log QUICK { $$ = $1; $$.quick = 1; }
3072 | QUICK log { $$ = $2; $$.quick = 1; }
3075 log : LOG { $$.log = PF_LOG; $$.logif = 0; }
3076 | LOG '(' logopts ')' {
3077 $$.log = PF_LOG | $3.log;
3078 $$.logif = $3.logif;
3082 logopts : logopt { $$ = $1; }
3083 | logopts comma logopt {
3084 $$.log = $1.log | $3.log;
3085 $$.logif = $3.logif;
3087 $$.logif = $1.logif;
3091 logopt : ALL { $$.log = PF_LOG_ALL; $$.logif = 0; }
3092 | USER { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
3093 | GROUP { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
3099 if (strncmp($2, "pflog", 5)) {
3100 yyerror("%s: should be a pflog interface", $2);
3104 i = strtonum($2 + 5, 0, 255, &errstr);
3106 yyerror("%s: %s", $2, errstr);
3115 interface : /* empty */ { $$ = NULL; }
3116 | ON if_item_not { $$ = $2; }
3117 | ON '{' optnl if_list '}' { $$ = $4; }
3120 if_list : if_item_not optnl { $$ = $1; }
3121 | if_list comma if_item_not optnl {
3122 $1->tail->next = $3;
3128 if_item_not : not if_item { $$ = $2; $$->not = $1; }
3132 struct node_host *n;
3134 $$ = calloc(1, sizeof(struct node_if));
3136 err(1, "if_item: calloc");
3137 if (strlcpy($$->ifname, $1, sizeof($$->ifname)) >=
3138 sizeof($$->ifname)) {
3141 yyerror("interface name too long");
3145 if ((n = ifa_exists($1)) != NULL)
3146 $$->ifa_flags = n->ifa_flags;
3155 af : /* empty */ { $$ = 0; }
3156 | INET { $$ = AF_INET; }
3157 | INET6 { $$ = AF_INET6; }
3160 etherproto : /* empty */ { $$ = NULL; }
3161 | PROTO etherproto_item { $$ = $2; }
3162 | PROTO '{' optnl etherproto_list '}' { $$ = $4; }
3165 etherproto_list : etherproto_item optnl { $$ = $1; }
3166 | etherproto_list comma etherproto_item optnl {
3167 $1->tail->next = $3;
3173 etherproto_item : etherprotoval {
3178 yyerror("proto 0 cannot be used");
3181 $$ = calloc(1, sizeof(struct node_proto));
3183 err(1, "proto_item: calloc");
3190 etherprotoval : NUMBER {
3191 if ($1 < 0 || $1 > 65565) {
3192 yyerror("protocol outside range");
3198 if (!strncmp($1, "0x", 2)) {
3199 if (sscanf($1, "0x%4x", &$$) != 1) {
3201 yyerror("invalid EtherType hex");
3205 yyerror("Symbolic EtherType not yet supported");
3210 proto : /* empty */ { $$ = NULL; }
3211 | PROTO proto_item { $$ = $2; }
3212 | PROTO '{' optnl proto_list '}' { $$ = $4; }
3215 proto_list : proto_item optnl { $$ = $1; }
3216 | proto_list comma proto_item optnl {
3217 $1->tail->next = $3;
3223 proto_item : protoval {
3228 yyerror("proto 0 cannot be used");
3231 $$ = calloc(1, sizeof(struct node_proto));
3233 err(1, "proto_item: calloc");
3243 p = getprotobyname($1);
3245 yyerror("unknown protocol %s", $1);
3253 if ($1 < 0 || $1 > 255) {
3254 yyerror("protocol outside range");
3260 l3fromto : /* empty */ {
3261 bzero(&$$, sizeof($$));
3271 | etherfrom etherto {
3277 etherfrom : /* emtpy */ {
3278 bzero(&$$, sizeof($$));
3285 etherto : /* empty */ {
3286 bzero(&$$, sizeof($$));
3293 mac : string '/' NUMBER {
3294 $$ = node_mac_from_string_masklen($1, $3);
3300 if (strchr($1, '&')) {
3302 char *mac = strtok($1, "&");
3303 char *mask = strtok(NULL, "&");
3304 $$ = node_mac_from_string_mask(mac, mask);
3306 $$ = node_mac_from_string($1);
3316 for (n = $2; n != NULL; n = n->next)
3324 | '{' optnl mac_list '}'
3329 mac_list : xmac optnl {
3332 | mac_list comma xmac {
3335 else if ($1 == NULL)
3338 $1->tail->next = $3;
3339 $1->tail = $3->tail;
3358 os : /* empty */ { $$ = NULL; }
3359 | OS xos { $$ = $2; }
3360 | OS '{' optnl os_list '}' { $$ = $4; }
3364 $$ = calloc(1, sizeof(struct node_os));
3366 err(1, "os: calloc");
3372 os_list : xos optnl { $$ = $1; }
3373 | os_list comma xos optnl {
3374 $1->tail->next = $3;
3380 from : /* empty */ {
3394 if (disallow_urpf_failed($2.host, "\"urpf-failed\" is "
3395 "not permitted in a destination address"))
3401 ipportspec : ipspec {
3405 | ipspec PORT portspec {
3419 ipspec : ANY { $$ = NULL; }
3420 | xhost { $$ = $1; }
3421 | '{' optnl host_list '}' { $$ = $3; }
3424 toipspec : TO ipspec { $$ = $2; }
3425 | /* empty */ { $$ = NULL; }
3428 host_list : ipspec optnl { $$ = $1; }
3429 | host_list comma ipspec optnl {
3432 else if ($1 == NULL)
3435 $1->tail->next = $3;
3436 $1->tail = $3->tail;
3443 struct node_host *n;
3445 for (n = $2; n != NULL; n = n->next)
3450 $$ = calloc(1, sizeof(struct node_host));
3452 err(1, "xhost: calloc");
3453 $$->addr.type = PF_ADDR_NOROUTE;
3459 $$ = calloc(1, sizeof(struct node_host));
3461 err(1, "xhost: calloc");
3462 $$->addr.type = PF_ADDR_URPFFAILED;
3470 if (($$ = host($1)) == NULL) {
3471 /* error. "any" is handled elsewhere */
3473 yyerror("could not parse host specification");
3479 | STRING '-' STRING {
3480 struct node_host *b, *e;
3482 if ((b = host($1)) == NULL || (e = host($3)) == NULL) {
3485 yyerror("could not parse host specification");
3488 if (b->af != e->af ||
3489 b->addr.type != PF_ADDR_ADDRMASK ||
3490 e->addr.type != PF_ADDR_ADDRMASK ||
3491 unmask(&b->addr.v.a.mask, b->af) !=
3492 (b->af == AF_INET ? 32 : 128) ||
3493 unmask(&e->addr.v.a.mask, e->af) !=
3494 (e->af == AF_INET ? 32 : 128) ||
3495 b->next != NULL || b->not ||
3496 e->next != NULL || e->not) {
3501 yyerror("invalid address range");
3504 memcpy(&b->addr.v.a.mask, &e->addr.v.a.addr,
3505 sizeof(b->addr.v.a.mask));
3506 b->addr.type = PF_ADDR_RANGE;
3512 | STRING '/' NUMBER {
3515 if (asprintf(&buf, "%s/%lld", $1, (long long)$3) == -1)
3516 err(1, "host: asprintf");
3518 if (($$ = host(buf)) == NULL) {
3519 /* error. "any" is handled elsewhere */
3521 yyerror("could not parse host specification");
3526 | NUMBER '/' NUMBER {
3529 /* ie. for 10/8 parsing */
3531 if (asprintf(&buf, "%lld/%lld", (long long)$1, (long long)$3) == -1)
3533 if (asprintf(&buf, "%lld/%lld", $1, $3) == -1)
3535 err(1, "host: asprintf");
3536 if (($$ = host(buf)) == NULL) {
3537 /* error. "any" is handled elsewhere */
3539 yyerror("could not parse host specification");
3545 | dynaddr '/' NUMBER {
3546 struct node_host *n;
3548 if ($3 < 0 || $3 > 128) {
3549 yyerror("bit number too big");
3553 for (n = $1; n != NULL; n = n->next)
3557 if (strlen($2) >= PF_TABLE_NAME_SIZE) {
3558 yyerror("table name '%s' too long", $2);
3562 $$ = calloc(1, sizeof(struct node_host));
3564 err(1, "host: calloc");
3565 $$->addr.type = PF_ADDR_TABLE;
3566 if (strlcpy($$->addr.v.tblname, $2,
3567 sizeof($$->addr.v.tblname)) >=
3568 sizeof($$->addr.v.tblname))
3569 errx(1, "host: strlcpy");
3580 if (atoul($1, &ulval) == -1) {
3581 yyerror("%s is not a number", $1);
3590 dynaddr : '(' STRING ')' {
3595 if (!isalpha(op[0])) {
3596 yyerror("invalid interface name '%s'", op);
3600 while ((p = strrchr($2, ':')) != NULL) {
3601 if (!strcmp(p+1, "network"))
3602 flags |= PFI_AFLAG_NETWORK;
3603 else if (!strcmp(p+1, "broadcast"))
3604 flags |= PFI_AFLAG_BROADCAST;
3605 else if (!strcmp(p+1, "peer"))
3606 flags |= PFI_AFLAG_PEER;
3607 else if (!strcmp(p+1, "0"))
3608 flags |= PFI_AFLAG_NOALIAS;
3610 yyerror("interface %s has bad modifier",
3617 if (flags & (flags - 1) & PFI_AFLAG_MODEMASK) {
3619 yyerror("illegal combination of "
3620 "interface modifiers");
3623 $$ = calloc(1, sizeof(struct node_host));
3625 err(1, "address: calloc");
3627 set_ipmask($$, 128);
3628 $$->addr.type = PF_ADDR_DYNIFTL;
3629 $$->addr.iflags = flags;
3630 if (strlcpy($$->addr.v.ifname, $2,
3631 sizeof($$->addr.v.ifname)) >=
3632 sizeof($$->addr.v.ifname)) {
3635 yyerror("interface name too long");
3644 portspec : port_item { $$ = $1; }
3645 | '{' optnl port_list '}' { $$ = $3; }
3648 port_list : port_item optnl { $$ = $1; }
3649 | port_list comma port_item optnl {
3650 $1->tail->next = $3;
3656 port_item : portrange {
3657 $$ = calloc(1, sizeof(struct node_port));
3659 err(1, "port_item: calloc");
3669 | unaryop portrange {
3671 yyerror("':' cannot be used with an other "
3675 $$ = calloc(1, sizeof(struct node_port));
3677 err(1, "port_item: calloc");
3684 | portrange PORTBINARY portrange {
3686 yyerror("':' cannot be used with an other "
3690 $$ = calloc(1, sizeof(struct node_port));
3692 err(1, "port_item: calloc");
3701 portplain : numberstring {
3702 if (parseport($1, &$$, 0) == -1) {
3710 portrange : numberstring {
3711 if (parseport($1, &$$, PPORT_RANGE) == -1) {
3719 uids : uid_item { $$ = $1; }
3720 | '{' optnl uid_list '}' { $$ = $3; }
3723 uid_list : uid_item optnl { $$ = $1; }
3724 | uid_list comma uid_item optnl {
3725 $1->tail->next = $3;
3732 $$ = calloc(1, sizeof(struct node_uid));
3734 err(1, "uid_item: calloc");
3742 if ($2 == UID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
3743 yyerror("user unknown requires operator = or "
3747 $$ = calloc(1, sizeof(struct node_uid));
3749 err(1, "uid_item: calloc");
3756 | uid PORTBINARY uid {
3757 if ($1 == UID_MAX || $3 == UID_MAX) {
3758 yyerror("user unknown requires operator = or "
3762 $$ = calloc(1, sizeof(struct node_uid));
3764 err(1, "uid_item: calloc");
3774 if (!strcmp($1, "unknown"))
3779 if ((pw = getpwnam($1)) == NULL) {
3780 yyerror("unknown user %s", $1);
3789 if ($1 < 0 || $1 >= UID_MAX) {
3790 yyerror("illegal uid value %lu", $1);
3797 gids : gid_item { $$ = $1; }
3798 | '{' optnl gid_list '}' { $$ = $3; }
3801 gid_list : gid_item optnl { $$ = $1; }
3802 | gid_list comma gid_item optnl {
3803 $1->tail->next = $3;
3810 $$ = calloc(1, sizeof(struct node_gid));
3812 err(1, "gid_item: calloc");
3820 if ($2 == GID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
3821 yyerror("group unknown requires operator = or "
3825 $$ = calloc(1, sizeof(struct node_gid));
3827 err(1, "gid_item: calloc");
3834 | gid PORTBINARY gid {
3835 if ($1 == GID_MAX || $3 == GID_MAX) {
3836 yyerror("group unknown requires operator = or "
3840 $$ = calloc(1, sizeof(struct node_gid));
3842 err(1, "gid_item: calloc");
3852 if (!strcmp($1, "unknown"))
3857 if ((grp = getgrnam($1)) == NULL) {
3858 yyerror("unknown group %s", $1);
3867 if ($1 < 0 || $1 >= GID_MAX) {
3868 yyerror("illegal gid value %lu", $1);
3878 if ((f = parse_flags($1)) < 0) {
3879 yyerror("bad flags %s", $1);
3888 flags : FLAGS flag '/' flag { $$.b1 = $2.b1; $$.b2 = $4.b1; }
3889 | FLAGS '/' flag { $$.b1 = 0; $$.b2 = $3.b1; }
3890 | FLAGS ANY { $$.b1 = 0; $$.b2 = 0; }
3893 icmpspec : ICMPTYPE icmp_item { $$ = $2; }
3894 | ICMPTYPE '{' optnl icmp_list '}' { $$ = $4; }
3895 | ICMP6TYPE icmp6_item { $$ = $2; }
3896 | ICMP6TYPE '{' optnl icmp6_list '}' { $$ = $4; }
3899 icmp_list : icmp_item optnl { $$ = $1; }
3900 | icmp_list comma icmp_item optnl {
3901 $1->tail->next = $3;
3907 icmp6_list : icmp6_item optnl { $$ = $1; }
3908 | icmp6_list comma icmp6_item optnl {
3909 $1->tail->next = $3;
3915 icmp_item : icmptype {
3916 $$ = calloc(1, sizeof(struct node_icmp));
3918 err(1, "icmp_item: calloc");
3921 $$->proto = IPPROTO_ICMP;
3925 | icmptype CODE STRING {
3926 const struct icmpcodeent *p;
3928 if ((p = geticmpcodebyname($1-1, $3, AF_INET)) == NULL) {
3929 yyerror("unknown icmp-code %s", $3);
3935 $$ = calloc(1, sizeof(struct node_icmp));
3937 err(1, "icmp_item: calloc");
3939 $$->code = p->code + 1;
3940 $$->proto = IPPROTO_ICMP;
3944 | icmptype CODE NUMBER {
3945 if ($3 < 0 || $3 > 255) {
3946 yyerror("illegal icmp-code %lu", $3);
3949 $$ = calloc(1, sizeof(struct node_icmp));
3951 err(1, "icmp_item: calloc");
3954 $$->proto = IPPROTO_ICMP;
3960 icmp6_item : icmp6type {
3961 $$ = calloc(1, sizeof(struct node_icmp));
3963 err(1, "icmp_item: calloc");
3966 $$->proto = IPPROTO_ICMPV6;
3970 | icmp6type CODE STRING {
3971 const struct icmpcodeent *p;
3973 if ((p = geticmpcodebyname($1-1, $3, AF_INET6)) == NULL) {
3974 yyerror("unknown icmp6-code %s", $3);
3980 $$ = calloc(1, sizeof(struct node_icmp));
3982 err(1, "icmp_item: calloc");
3984 $$->code = p->code + 1;
3985 $$->proto = IPPROTO_ICMPV6;
3989 | icmp6type CODE NUMBER {
3990 if ($3 < 0 || $3 > 255) {
3991 yyerror("illegal icmp-code %lu", $3);
3994 $$ = calloc(1, sizeof(struct node_icmp));
3996 err(1, "icmp_item: calloc");
3999 $$->proto = IPPROTO_ICMPV6;
4006 const struct icmptypeent *p;
4008 if ((p = geticmptypebyname($1, AF_INET)) == NULL) {
4009 yyerror("unknown icmp-type %s", $1);
4017 if ($1 < 0 || $1 > 255) {
4018 yyerror("illegal icmp-type %lu", $1);
4025 icmp6type : STRING {
4026 const struct icmptypeent *p;
4028 if ((p = geticmptypebyname($1, AF_INET6)) ==
4030 yyerror("unknown icmp6-type %s", $1);
4038 if ($1 < 0 || $1 > 255) {
4039 yyerror("illegal icmp6-type %lu", $1);
4050 if (map_tos($1, &val))
4052 else if ($1[0] == '0' && $1[1] == 'x') {
4054 $$ = strtoul($1, &end, 16);
4055 if (errno || *end != '\0')
4058 $$ = 256; /* flag bad argument */
4059 if ($$ < 0 || $$ > 255) {
4060 yyerror("illegal tos value %s", $1);
4068 if ($$ < 0 || $$ > 255) {
4069 yyerror("illegal tos value %s", $1);
4075 sourcetrack : SOURCETRACK { $$ = PF_SRCTRACK; }
4076 | SOURCETRACK GLOBAL { $$ = PF_SRCTRACK_GLOBAL; }
4077 | SOURCETRACK RULE { $$ = PF_SRCTRACK_RULE; }
4080 statelock : IFBOUND {
4081 $$ = PFRULE_IFBOUND;
4092 | KEEP STATE state_opt_spec {
4093 $$.action = PF_STATE_NORMAL;
4096 | MODULATE STATE state_opt_spec {
4097 $$.action = PF_STATE_MODULATE;
4100 | SYNPROXY STATE state_opt_spec {
4101 $$.action = PF_STATE_SYNPROXY;
4106 flush : /* empty */ { $$ = 0; }
4107 | FLUSH { $$ = PF_FLUSH; }
4109 $$ = PF_FLUSH | PF_FLUSH_GLOBAL;
4113 state_opt_spec : '(' state_opt_list ')' { $$ = $2; }
4114 | /* empty */ { $$ = NULL; }
4117 state_opt_list : state_opt_item { $$ = $1; }
4118 | state_opt_list comma state_opt_item {
4119 $1->tail->next = $3;
4125 state_opt_item : MAXIMUM NUMBER {
4126 if ($2 < 0 || $2 > UINT_MAX) {
4127 yyerror("only positive values permitted");
4130 $$ = calloc(1, sizeof(struct node_state_opt));
4132 err(1, "state_opt_item: calloc");
4133 $$->type = PF_STATE_OPT_MAX;
4134 $$->data.max_states = $2;
4139 $$ = calloc(1, sizeof(struct node_state_opt));
4141 err(1, "state_opt_item: calloc");
4142 $$->type = PF_STATE_OPT_NOSYNC;
4146 | MAXSRCSTATES NUMBER {
4147 if ($2 < 0 || $2 > UINT_MAX) {
4148 yyerror("only positive values permitted");
4151 $$ = calloc(1, sizeof(struct node_state_opt));
4153 err(1, "state_opt_item: calloc");
4154 $$->type = PF_STATE_OPT_MAX_SRC_STATES;
4155 $$->data.max_src_states = $2;
4159 | MAXSRCCONN NUMBER {
4160 if ($2 < 0 || $2 > UINT_MAX) {
4161 yyerror("only positive values permitted");
4164 $$ = calloc(1, sizeof(struct node_state_opt));
4166 err(1, "state_opt_item: calloc");
4167 $$->type = PF_STATE_OPT_MAX_SRC_CONN;
4168 $$->data.max_src_conn = $2;
4172 | MAXSRCCONNRATE NUMBER '/' NUMBER {
4173 if ($2 < 0 || $2 > UINT_MAX ||
4174 $4 < 0 || $4 > UINT_MAX) {
4175 yyerror("only positive values permitted");
4178 $$ = calloc(1, sizeof(struct node_state_opt));
4180 err(1, "state_opt_item: calloc");
4181 $$->type = PF_STATE_OPT_MAX_SRC_CONN_RATE;
4182 $$->data.max_src_conn_rate.limit = $2;
4183 $$->data.max_src_conn_rate.seconds = $4;
4187 | OVERLOAD '<' STRING '>' flush {
4188 if (strlen($3) >= PF_TABLE_NAME_SIZE) {
4189 yyerror("table name '%s' too long", $3);
4193 $$ = calloc(1, sizeof(struct node_state_opt));
4195 err(1, "state_opt_item: calloc");
4196 if (strlcpy($$->data.overload.tblname, $3,
4197 PF_TABLE_NAME_SIZE) >= PF_TABLE_NAME_SIZE)
4198 errx(1, "state_opt_item: strlcpy");
4200 $$->type = PF_STATE_OPT_OVERLOAD;
4201 $$->data.overload.flush = $5;
4205 | MAXSRCNODES NUMBER {
4206 if ($2 < 0 || $2 > UINT_MAX) {
4207 yyerror("only positive values permitted");
4210 $$ = calloc(1, sizeof(struct node_state_opt));
4212 err(1, "state_opt_item: calloc");
4213 $$->type = PF_STATE_OPT_MAX_SRC_NODES;
4214 $$->data.max_src_nodes = $2;
4219 $$ = calloc(1, sizeof(struct node_state_opt));
4221 err(1, "state_opt_item: calloc");
4222 $$->type = PF_STATE_OPT_SRCTRACK;
4223 $$->data.src_track = $1;
4228 $$ = calloc(1, sizeof(struct node_state_opt));
4230 err(1, "state_opt_item: calloc");
4231 $$->type = PF_STATE_OPT_STATELOCK;
4232 $$->data.statelock = $1;
4237 $$ = calloc(1, sizeof(struct node_state_opt));
4239 err(1, "state_opt_item: calloc");
4240 $$->type = PF_STATE_OPT_SLOPPY;
4247 if ($2 < 0 || $2 > UINT_MAX) {
4248 yyerror("only positive values permitted");
4251 for (i = 0; pf_timeouts[i].name &&
4252 strcmp(pf_timeouts[i].name, $1); ++i)
4254 if (!pf_timeouts[i].name) {
4255 yyerror("illegal timeout name %s", $1);
4259 if (strchr(pf_timeouts[i].name, '.') == NULL) {
4260 yyerror("illegal state timeout %s", $1);
4265 $$ = calloc(1, sizeof(struct node_state_opt));
4267 err(1, "state_opt_item: calloc");
4268 $$->type = PF_STATE_OPT_TIMEOUT;
4269 $$->data.timeout.number = pf_timeouts[i].timeout;
4270 $$->data.timeout.seconds = $2;
4276 label : LABEL STRING {
4281 etherqname : QUEUE STRING {
4284 | QUEUE '(' STRING ')' {
4289 qname : QUEUE STRING {
4293 | QUEUE '(' STRING ')' {
4297 | QUEUE '(' STRING comma STRING ')' {
4303 no : /* empty */ { $$ = 0; }
4307 portstar : numberstring {
4308 if (parseport($1, &$$, PPORT_RANGE|PPORT_STAR) == -1) {
4316 redirspec : host { $$ = $1; }
4317 | '{' optnl redir_host_list '}' { $$ = $3; }
4320 redir_host_list : host optnl { $$ = $1; }
4321 | redir_host_list comma host optnl {
4322 $1->tail->next = $3;
4323 $1->tail = $3->tail;
4328 redirpool : /* empty */ { $$ = NULL; }
4330 $$ = calloc(1, sizeof(struct redirection));
4332 err(1, "redirection: calloc");
4334 $$->rport.a = $$->rport.b = $$->rport.t = 0;
4336 | ARROW redirspec PORT portstar {
4337 $$ = calloc(1, sizeof(struct redirection));
4339 err(1, "redirection: calloc");
4345 hashkey : /* empty */
4347 $$ = calloc(1, sizeof(struct pf_poolhashkey));
4349 err(1, "hashkey: calloc");
4350 $$->key32[0] = arc4random();
4351 $$->key32[1] = arc4random();
4352 $$->key32[2] = arc4random();
4353 $$->key32[3] = arc4random();
4357 if (!strncmp($1, "0x", 2)) {
4358 if (strlen($1) != 34) {
4360 yyerror("hex key must be 128 bits "
4361 "(32 hex digits) long");
4364 $$ = calloc(1, sizeof(struct pf_poolhashkey));
4366 err(1, "hashkey: calloc");
4368 if (sscanf($1, "0x%8x%8x%8x%8x",
4369 &$$->key32[0], &$$->key32[1],
4370 &$$->key32[2], &$$->key32[3]) != 4) {
4373 yyerror("invalid hex key");
4379 $$ = calloc(1, sizeof(struct pf_poolhashkey));
4381 err(1, "hashkey: calloc");
4383 MD5Update(&context, (unsigned char *)$1,
4385 MD5Final((unsigned char *)$$, &context);
4386 HTONL($$->key32[0]);
4387 HTONL($$->key32[1]);
4388 HTONL($$->key32[2]);
4389 HTONL($$->key32[3]);
4395 pool_opts : { bzero(&pool_opts, sizeof pool_opts); }
4399 bzero(&pool_opts, sizeof pool_opts);
4404 pool_opts_l : pool_opts_l pool_opt
4408 pool_opt : BITMASK {
4409 if (pool_opts.type) {
4410 yyerror("pool type cannot be redefined");
4413 pool_opts.type = PF_POOL_BITMASK;
4416 if (pool_opts.type) {
4417 yyerror("pool type cannot be redefined");
4420 pool_opts.type = PF_POOL_RANDOM;
4422 | SOURCEHASH hashkey {
4423 if (pool_opts.type) {
4424 yyerror("pool type cannot be redefined");
4427 pool_opts.type = PF_POOL_SRCHASH;
4431 if (pool_opts.type) {
4432 yyerror("pool type cannot be redefined");
4435 pool_opts.type = PF_POOL_ROUNDROBIN;
4438 if (pool_opts.staticport) {
4439 yyerror("static-port cannot be redefined");
4442 pool_opts.staticport = 1;
4445 if (filter_opts.marker & POM_STICKYADDRESS) {
4446 yyerror("sticky-address cannot be redefined");
4449 pool_opts.marker |= POM_STICKYADDRESS;
4450 pool_opts.opts |= PF_POOL_STICKYADDR;
4452 | MAPEPORTSET number '/' number '/' number {
4453 if (pool_opts.mape.offset) {
4454 yyerror("map-e-portset cannot be redefined");
4457 if (pool_opts.type) {
4458 yyerror("map-e-portset cannot be used with "
4462 if ($2 <= 0 || $2 >= 16) {
4463 yyerror("MAP-E PSID offset must be 1-15");
4466 if ($4 < 0 || $4 >= 16 || $2 + $4 > 16) {
4467 yyerror("Invalid MAP-E PSID length");
4469 } else if ($4 == 0) {
4470 yyerror("PSID Length = 0: this means"
4471 " you do not need MAP-E");
4474 if ($6 < 0 || $6 > 65535) {
4475 yyerror("Invalid MAP-E PSID");
4478 pool_opts.mape.offset = $2;
4479 pool_opts.mape.psidlen = $4;
4480 pool_opts.mape.psid = $6;
4484 redirection : /* empty */ { $$ = NULL; }
4486 $$ = calloc(1, sizeof(struct redirection));
4488 err(1, "redirection: calloc");
4490 $$->rport.a = $$->rport.b = $$->rport.t = 0;
4492 | ARROW host PORT portstar {
4493 $$ = calloc(1, sizeof(struct redirection));
4495 err(1, "redirection: calloc");
4501 natpasslog : /* empty */ { $$.b1 = $$.b2 = 0; $$.w2 = 0; }
4502 | PASS { $$.b1 = 1; $$.b2 = 0; $$.w2 = 0; }
4503 | PASS log { $$.b1 = 1; $$.b2 = $2.log; $$.w2 = $2.logif; }
4504 | log { $$.b1 = 0; $$.b2 = $1.log; $$.w2 = $1.logif; }
4507 nataction : no NAT natpasslog {
4509 yyerror("\"pass\" not valid with \"no\"");
4520 | no RDR natpasslog {
4522 yyerror("\"pass\" not valid with \"no\"");
4535 natrule : nataction interface af proto fromto tag tagged rtable
4538 struct pfctl_rule r;
4540 if (check_rulestate(PFCTL_STATE_NAT))
4543 memset(&r, 0, sizeof(r));
4552 if ($5.src.host && $5.src.host->af &&
4553 !$5.src.host->ifindex)
4554 r.af = $5.src.host->af;
4555 else if ($5.dst.host && $5.dst.host->af &&
4556 !$5.dst.host->ifindex)
4557 r.af = $5.dst.host->af;
4561 if (strlcpy(r.tagname, $6, PF_TAG_NAME_SIZE) >=
4563 yyerror("tag too long, max %u chars",
4564 PF_TAG_NAME_SIZE - 1);
4569 if (strlcpy(r.match_tagname, $7.name,
4570 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4571 yyerror("tag too long, max %u chars",
4572 PF_TAG_NAME_SIZE - 1);
4575 r.match_tag_not = $7.neg;
4578 if (r.action == PF_NONAT || r.action == PF_NORDR) {
4580 yyerror("translation rule with 'no' "
4581 "does not need '->'");
4585 if ($9 == NULL || $9->host == NULL) {
4586 yyerror("translation rule requires '-> "
4590 if (!r.af && ! $9->host->ifindex)
4591 r.af = $9->host->af;
4593 remove_invalid_hosts(&$9->host, &r.af);
4594 if (invalid_redirect($9->host, r.af))
4596 if (check_netmask($9->host, r.af))
4599 r.rpool.proxy_port[0] = ntohs($9->rport.a);
4603 if (!$9->rport.b && $9->rport.t &&
4604 $5.dst.port != NULL) {
4605 r.rpool.proxy_port[1] =
4606 ntohs($9->rport.a) +
4608 $5.dst.port->port[1]) -
4610 $5.dst.port->port[0]));
4612 r.rpool.proxy_port[1] =
4616 r.rpool.proxy_port[1] =
4618 if (!r.rpool.proxy_port[0] &&
4619 !r.rpool.proxy_port[1]) {
4620 r.rpool.proxy_port[0] =
4621 PF_NAT_PROXY_PORT_LOW;
4622 r.rpool.proxy_port[1] =
4623 PF_NAT_PROXY_PORT_HIGH;
4624 } else if (!r.rpool.proxy_port[1])
4625 r.rpool.proxy_port[1] =
4626 r.rpool.proxy_port[0];
4632 r.rpool.opts = $10.type;
4633 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
4634 PF_POOL_NONE && ($9->host->next != NULL ||
4635 $9->host->addr.type == PF_ADDR_TABLE ||
4636 DYNIF_MULTIADDR($9->host->addr)))
4637 r.rpool.opts = PF_POOL_ROUNDROBIN;
4638 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4639 PF_POOL_ROUNDROBIN &&
4640 disallow_table($9->host, "tables are only "
4641 "supported in round-robin redirection "
4644 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4645 PF_POOL_ROUNDROBIN &&
4646 disallow_alias($9->host, "interface (%s) "
4647 "is only supported in round-robin "
4648 "redirection pools"))
4650 if ($9->host->next != NULL) {
4651 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4652 PF_POOL_ROUNDROBIN) {
4653 yyerror("only round-robin "
4654 "valid for multiple "
4655 "redirection addresses");
4661 if ($10.key != NULL)
4662 memcpy(&r.rpool.key, $10.key,
4663 sizeof(struct pf_poolhashkey));
4666 r.rpool.opts |= $10.opts;
4668 if ($10.staticport) {
4669 if (r.action != PF_NAT) {
4670 yyerror("the 'static-port' option is "
4671 "only valid with nat rules");
4674 if (r.rpool.proxy_port[0] !=
4675 PF_NAT_PROXY_PORT_LOW &&
4676 r.rpool.proxy_port[1] !=
4677 PF_NAT_PROXY_PORT_HIGH) {
4678 yyerror("the 'static-port' option can't"
4679 " be used when specifying a port"
4683 r.rpool.proxy_port[0] = 0;
4684 r.rpool.proxy_port[1] = 0;
4687 if ($10.mape.offset) {
4688 if (r.action != PF_NAT) {
4689 yyerror("the 'map-e-portset' option is"
4690 " only valid with nat rules");
4693 if ($10.staticport) {
4694 yyerror("the 'map-e-portset' option"
4695 " can't be used 'static-port'");
4698 if (r.rpool.proxy_port[0] !=
4699 PF_NAT_PROXY_PORT_LOW &&
4700 r.rpool.proxy_port[1] !=
4701 PF_NAT_PROXY_PORT_HIGH) {
4702 yyerror("the 'map-e-portset' option"
4703 " can't be used when specifying"
4707 r.rpool.mape = $10.mape;
4710 expand_rule(&r, $2, $9 == NULL ? NULL : $9->host, $4,
4711 $5.src_os, $5.src.host, $5.src.port, $5.dst.host,
4712 $5.dst.port, 0, 0, 0, "");
4717 binatrule : no BINAT natpasslog interface af proto FROM ipspec toipspec tag
4718 tagged rtable redirection
4720 struct pfctl_rule binat;
4721 struct pf_pooladdr *pa;
4723 if (check_rulestate(PFCTL_STATE_NAT))
4725 if (disallow_urpf_failed($9, "\"urpf-failed\" is not "
4726 "permitted as a binat destination"))
4729 memset(&binat, 0, sizeof(binat));
4732 yyerror("\"pass\" not valid with \"no\"");
4736 binat.action = PF_NOBINAT;
4738 binat.action = PF_BINAT;
4739 binat.natpass = $3.b1;
4741 binat.logif = $3.w2;
4743 if (!binat.af && $8 != NULL && $8->af)
4745 if (!binat.af && $9 != NULL && $9->af)
4748 if (!binat.af && $13 != NULL && $13->host)
4749 binat.af = $13->host->af;
4751 yyerror("address family (inet/inet6) "
4757 memcpy(binat.ifname, $4->ifname,
4758 sizeof(binat.ifname));
4759 binat.ifnot = $4->not;
4764 if (strlcpy(binat.tagname, $10,
4765 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4766 yyerror("tag too long, max %u chars",
4767 PF_TAG_NAME_SIZE - 1);
4771 if (strlcpy(binat.match_tagname, $11.name,
4772 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4773 yyerror("tag too long, max %u chars",
4774 PF_TAG_NAME_SIZE - 1);
4777 binat.match_tag_not = $11.neg;
4778 binat.rtableid = $12;
4781 binat.proto = $6->proto;
4785 if ($8 != NULL && disallow_table($8, "invalid use of "
4786 "table <%s> as the source address of a binat rule"))
4788 if ($8 != NULL && disallow_alias($8, "invalid use of "
4789 "interface (%s) as the source address of a binat "
4792 if ($13 != NULL && $13->host != NULL && disallow_table(
4793 $13->host, "invalid use of table <%s> as the "
4794 "redirect address of a binat rule"))
4796 if ($13 != NULL && $13->host != NULL && disallow_alias(
4797 $13->host, "invalid use of interface (%s) as the "
4798 "redirect address of a binat rule"))
4803 yyerror("multiple binat ip addresses");
4806 if ($8->addr.type == PF_ADDR_DYNIFTL)
4808 if ($8->af != binat.af) {
4809 yyerror("binat ip versions must match");
4812 if (check_netmask($8, binat.af))
4814 memcpy(&binat.src.addr, &$8->addr,
4815 sizeof(binat.src.addr));
4820 yyerror("multiple binat ip addresses");
4823 if ($9->af != binat.af && $9->af) {
4824 yyerror("binat ip versions must match");
4827 if (check_netmask($9, binat.af))
4829 memcpy(&binat.dst.addr, &$9->addr,
4830 sizeof(binat.dst.addr));
4831 binat.dst.neg = $9->not;
4835 if (binat.action == PF_NOBINAT) {
4837 yyerror("'no binat' rule does not need"
4842 if ($13 == NULL || $13->host == NULL) {
4843 yyerror("'binat' rule requires"
4848 remove_invalid_hosts(&$13->host, &binat.af);
4849 if (invalid_redirect($13->host, binat.af))
4851 if ($13->host->next != NULL) {
4852 yyerror("binat rule must redirect to "
4853 "a single address");
4856 if (check_netmask($13->host, binat.af))
4859 if (!PF_AZERO(&binat.src.addr.v.a.mask,
4861 !PF_AEQ(&binat.src.addr.v.a.mask,
4862 &$13->host->addr.v.a.mask, binat.af)) {
4863 yyerror("'binat' source mask and "
4864 "redirect mask must be the same");
4868 TAILQ_INIT(&binat.rpool.list);
4869 pa = calloc(1, sizeof(struct pf_pooladdr));
4871 err(1, "binat: calloc");
4872 pa->addr = $13->host->addr;
4874 TAILQ_INSERT_TAIL(&binat.rpool.list,
4880 pfctl_append_rule(pf, &binat, "");
4884 tag : /* empty */ { $$ = NULL; }
4885 | TAG STRING { $$ = $2; }
4888 tagged : /* empty */ { $$.neg = 0; $$.name = NULL; }
4889 | not TAGGED string { $$.neg = $1; $$.name = $3; }
4892 rtable : /* empty */ { $$ = -1; }
4894 if ($2 < 0 || $2 > rt_tableid_max()) {
4895 yyerror("invalid rtable id");
4902 route_host : STRING {
4903 $$ = calloc(1, sizeof(struct node_host));
4905 err(1, "route_host: calloc");
4906 if (strlen($1) >= IFNAMSIZ) {
4907 yyerror("interface name too long");
4910 $$->ifname = strdup($1);
4911 set_ipmask($$, 128);
4915 | '(' STRING host ')' {
4916 struct node_host *n;
4919 for (n = $3; n != NULL; n = n->next) {
4920 if (strlen($2) >= IFNAMSIZ) {
4921 yyerror("interface name too long");
4924 n->ifname = strdup($2);
4929 route_host_list : route_host optnl { $$ = $1; }
4930 | route_host_list comma route_host optnl {
4933 if ($1->af != $3->af) {
4934 yyerror("all pool addresses must be in the "
4935 "same address family");
4938 $1->tail->next = $3;
4939 $1->tail = $3->tail;
4944 routespec : route_host { $$ = $1; }
4945 | '{' optnl route_host_list '}' { $$ = $3; }
4948 route : /* empty */ {
4954 /* backwards-compat */
4959 | ROUTETO routespec pool_opts {
4962 $$.pool_opts = $3.type | $3.opts;
4966 | REPLYTO routespec pool_opts {
4969 $$.pool_opts = $3.type | $3.opts;
4973 | DUPTO routespec pool_opts {
4976 $$.pool_opts = $3.type | $3.opts;
4982 timeout_spec : STRING NUMBER
4984 if (check_rulestate(PFCTL_STATE_OPTION)) {
4988 if ($2 < 0 || $2 > UINT_MAX) {
4989 yyerror("only positive values permitted");
4992 if (pfctl_set_timeout(pf, $1, $2, 0) != 0) {
4993 yyerror("unknown timeout %s", $1);
5000 if (check_rulestate(PFCTL_STATE_OPTION))
5002 if ($2 < 0 || $2 > UINT_MAX) {
5003 yyerror("only positive values permitted");
5006 if (pfctl_set_timeout(pf, "interval", $2, 0) != 0)
5011 timeout_list : timeout_list comma timeout_spec optnl
5012 | timeout_spec optnl
5015 limit_spec : STRING NUMBER
5017 if (check_rulestate(PFCTL_STATE_OPTION)) {
5021 if ($2 < 0 || $2 > UINT_MAX) {
5022 yyerror("only positive values permitted");
5025 if (pfctl_set_limit(pf, $1, $2) != 0) {
5026 yyerror("unable to set limit %s %u", $1, $2);
5034 limit_list : limit_list comma limit_spec optnl
5042 yesno : NO { $$ = 0; }
5044 if (!strcmp($1, "yes"))
5047 yyerror("invalid value '%s', expected 'yes' "
5056 unaryop : '=' { $$ = PF_OP_EQ; }
5057 | '!' '=' { $$ = PF_OP_NE; }
5058 | '<' '=' { $$ = PF_OP_LE; }
5059 | '<' { $$ = PF_OP_LT; }
5060 | '>' '=' { $$ = PF_OP_GE; }
5061 | '>' { $$ = PF_OP_GT; }
5067 yyerror(const char *fmt, ...)
5073 fprintf(stderr, "%s:%d: ", file->name, yylval.lineno);
5074 vfprintf(stderr, fmt, ap);
5075 fprintf(stderr, "\n");
5081 disallow_table(struct node_host *h, const char *fmt)
5083 for (; h != NULL; h = h->next)
5084 if (h->addr.type == PF_ADDR_TABLE) {
5085 yyerror(fmt, h->addr.v.tblname);
5092 disallow_urpf_failed(struct node_host *h, const char *fmt)
5094 for (; h != NULL; h = h->next)
5095 if (h->addr.type == PF_ADDR_URPFFAILED) {
5103 disallow_alias(struct node_host *h, const char *fmt)
5105 for (; h != NULL; h = h->next)
5106 if (DYNIF_MULTIADDR(h->addr)) {
5107 yyerror(fmt, h->addr.v.tblname);
5114 rule_consistent(struct pfctl_rule *r, int anchor_call)
5118 switch (r->action) {
5123 problems = filter_consistent(r, anchor_call);
5127 problems = nat_consistent(r);
5131 problems = rdr_consistent(r);
5142 filter_consistent(struct pfctl_rule *r, int anchor_call)
5146 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP &&
5147 (r->src.port_op || r->dst.port_op)) {
5148 yyerror("port only applies to tcp/udp");
5151 if (r->proto != IPPROTO_ICMP && r->proto != IPPROTO_ICMPV6 &&
5152 (r->type || r->code)) {
5153 yyerror("icmp-type/code only applies to icmp");
5156 if (!r->af && (r->type || r->code)) {
5157 yyerror("must indicate address family with icmp-type/code");
5160 if (r->overload_tblname[0] &&
5161 r->max_src_conn == 0 && r->max_src_conn_rate.seconds == 0) {
5162 yyerror("'overload' requires 'max-src-conn' "
5163 "or 'max-src-conn-rate'");
5166 if ((r->proto == IPPROTO_ICMP && r->af == AF_INET6) ||
5167 (r->proto == IPPROTO_ICMPV6 && r->af == AF_INET)) {
5168 yyerror("proto %s doesn't match address family %s",
5169 r->proto == IPPROTO_ICMP ? "icmp" : "icmp6",
5170 r->af == AF_INET ? "inet" : "inet6");
5173 if (r->allow_opts && r->action != PF_PASS) {
5174 yyerror("allow-opts can only be specified for pass rules");
5177 if (r->rule_flag & PFRULE_FRAGMENT && (r->src.port_op ||
5178 r->dst.port_op || r->flagset || r->type || r->code)) {
5179 yyerror("fragments can be filtered only on IP header fields");
5182 if (r->rule_flag & PFRULE_RETURNRST && r->proto != IPPROTO_TCP) {
5183 yyerror("return-rst can only be applied to TCP rules");
5186 if (r->max_src_nodes && !(r->rule_flag & PFRULE_RULESRCTRACK)) {
5187 yyerror("max-src-nodes requires 'source-track rule'");
5190 if (r->action == PF_DROP && r->keep_state) {
5191 yyerror("keep state on block rules doesn't make sense");
5194 if (r->rule_flag & PFRULE_STATESLOPPY &&
5195 (r->keep_state == PF_STATE_MODULATE ||
5196 r->keep_state == PF_STATE_SYNPROXY)) {
5197 yyerror("sloppy state matching cannot be used with "
5198 "synproxy state or modulate state");
5205 nat_consistent(struct pfctl_rule *r)
5207 return (0); /* yeah! */
5211 rdr_consistent(struct pfctl_rule *r)
5215 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP) {
5216 if (r->src.port_op) {
5217 yyerror("src port only applies to tcp/udp");
5220 if (r->dst.port_op) {
5221 yyerror("dst port only applies to tcp/udp");
5224 if (r->rpool.proxy_port[0]) {
5225 yyerror("rpool port only applies to tcp/udp");
5229 if (r->dst.port_op &&
5230 r->dst.port_op != PF_OP_EQ && r->dst.port_op != PF_OP_RRG) {
5231 yyerror("invalid port operator for rdr destination port");
5238 process_tabledef(char *name, struct table_opts *opts)
5240 struct pfr_buffer ab;
5241 struct node_tinit *ti;
5242 unsigned long maxcount;
5243 size_t s = sizeof(maxcount);
5245 bzero(&ab, sizeof(ab));
5246 ab.pfrb_type = PFRB_ADDRS;
5247 SIMPLEQ_FOREACH(ti, &opts->init_nodes, entries) {
5249 if (pfr_buf_load(&ab, ti->file, 0, append_addr)) {
5251 yyerror("cannot load \"%s\": %s",
5252 ti->file, strerror(errno));
5254 yyerror("file \"%s\" contains bad data",
5259 if (append_addr_host(&ab, ti->host, 0, 0)) {
5260 yyerror("cannot create address buffer: %s",
5265 if (pf->opts & PF_OPT_VERBOSE)
5266 print_tabledef(name, opts->flags, opts->init_addr,
5268 if (!(pf->opts & PF_OPT_NOACTION) &&
5269 pfctl_define_table(name, opts->flags, opts->init_addr,
5270 pf->anchor->name, &ab, pf->anchor->ruleset.tticket)) {
5272 if (sysctlbyname("net.pf.request_maxcount", &maxcount, &s,
5276 if (ab.pfrb_size > maxcount)
5277 yyerror("cannot define table %s: too many elements.\n"
5278 "Consider increasing net.pf.request_maxcount.",
5281 yyerror("cannot define table %s: %s", name,
5282 pfr_strerror(errno));
5299 /* macro gore, but you should've seen the prior indentation nightmare... */
5301 #define FREE_LIST(T,r) \
5304 while (node != NULL) { \
5306 node = node->next; \
5311 #define LOOP_THROUGH(T,n,r,C) \
5315 r = calloc(1, sizeof(T)); \
5317 err(1, "LOOP: calloc"); \
5321 while (n != NULL) { \
5330 expand_label_str(char *label, size_t len, const char *srch, const char *repl)
5335 if ((tmp = calloc(1, len)) == NULL)
5336 err(1, "expand_label_str: calloc");
5338 while ((q = strstr(p, srch)) != NULL) {
5340 if ((strlcat(tmp, p, len) >= len) ||
5341 (strlcat(tmp, repl, len) >= len))
5342 errx(1, "expand_label: label too long");
5346 if (strlcat(tmp, p, len) >= len)
5347 errx(1, "expand_label: label too long");
5348 strlcpy(label, tmp, len); /* always fits */
5353 expand_label_if(const char *name, char *label, size_t len, const char *ifname)
5355 if (strstr(label, name) != NULL) {
5357 expand_label_str(label, len, name, "any");
5359 expand_label_str(label, len, name, ifname);
5364 expand_label_addr(const char *name, char *label, size_t len, sa_family_t af,
5365 struct pf_rule_addr *addr)
5367 char tmp[64], tmp_not[66];
5369 if (strstr(label, name) != NULL) {
5370 switch (addr->addr.type) {
5371 case PF_ADDR_DYNIFTL:
5372 snprintf(tmp, sizeof(tmp), "(%s)", addr->addr.v.ifname);
5375 snprintf(tmp, sizeof(tmp), "<%s>", addr->addr.v.tblname);
5377 case PF_ADDR_NOROUTE:
5378 snprintf(tmp, sizeof(tmp), "no-route");
5380 case PF_ADDR_URPFFAILED:
5381 snprintf(tmp, sizeof(tmp), "urpf-failed");
5383 case PF_ADDR_ADDRMASK:
5384 if (!af || (PF_AZERO(&addr->addr.v.a.addr, af) &&
5385 PF_AZERO(&addr->addr.v.a.mask, af)))
5386 snprintf(tmp, sizeof(tmp), "any");
5391 if (inet_ntop(af, &addr->addr.v.a.addr, a,
5393 snprintf(tmp, sizeof(tmp), "?");
5395 bits = unmask(&addr->addr.v.a.mask, af);
5396 if ((af == AF_INET && bits < 32) ||
5397 (af == AF_INET6 && bits < 128))
5398 snprintf(tmp, sizeof(tmp),
5401 snprintf(tmp, sizeof(tmp),
5407 snprintf(tmp, sizeof(tmp), "?");
5412 snprintf(tmp_not, sizeof(tmp_not), "! %s", tmp);
5413 expand_label_str(label, len, name, tmp_not);
5415 expand_label_str(label, len, name, tmp);
5420 expand_label_port(const char *name, char *label, size_t len,
5421 struct pf_rule_addr *addr)
5423 char a1[6], a2[6], op[13] = "";
5425 if (strstr(label, name) != NULL) {
5426 snprintf(a1, sizeof(a1), "%u", ntohs(addr->port[0]));
5427 snprintf(a2, sizeof(a2), "%u", ntohs(addr->port[1]));
5430 else if (addr->port_op == PF_OP_IRG)
5431 snprintf(op, sizeof(op), "%s><%s", a1, a2);
5432 else if (addr->port_op == PF_OP_XRG)
5433 snprintf(op, sizeof(op), "%s<>%s", a1, a2);
5434 else if (addr->port_op == PF_OP_EQ)
5435 snprintf(op, sizeof(op), "%s", a1);
5436 else if (addr->port_op == PF_OP_NE)
5437 snprintf(op, sizeof(op), "!=%s", a1);
5438 else if (addr->port_op == PF_OP_LT)
5439 snprintf(op, sizeof(op), "<%s", a1);
5440 else if (addr->port_op == PF_OP_LE)
5441 snprintf(op, sizeof(op), "<=%s", a1);
5442 else if (addr->port_op == PF_OP_GT)
5443 snprintf(op, sizeof(op), ">%s", a1);
5444 else if (addr->port_op == PF_OP_GE)
5445 snprintf(op, sizeof(op), ">=%s", a1);
5446 expand_label_str(label, len, name, op);
5451 expand_label_proto(const char *name, char *label, size_t len, u_int8_t proto)
5453 const char *protoname;
5456 if (strstr(label, name) != NULL) {
5457 protoname = pfctl_proto2name(proto);
5458 if (protoname != NULL)
5459 expand_label_str(label, len, name, protoname);
5461 snprintf(n, sizeof(n), "%u", proto);
5462 expand_label_str(label, len, name, n);
5468 expand_label_nr(const char *name, char *label, size_t len,
5469 struct pfctl_rule *r)
5473 if (strstr(label, name) != NULL) {
5474 snprintf(n, sizeof(n), "%u", r->nr);
5475 expand_label_str(label, len, name, n);
5480 expand_label(char *label, size_t len, struct pfctl_rule *r)
5482 expand_label_if("$if", label, len, r->ifname);
5483 expand_label_addr("$srcaddr", label, len, r->af, &r->src);
5484 expand_label_addr("$dstaddr", label, len, r->af, &r->dst);
5485 expand_label_port("$srcport", label, len, &r->src);
5486 expand_label_port("$dstport", label, len, &r->dst);
5487 expand_label_proto("$proto", label, len, r->proto);
5488 expand_label_nr("$nr", label, len, r);
5492 expand_altq(struct pf_altq *a, struct node_if *interfaces,
5493 struct node_queue *nqueues, struct node_queue_bw bwspec,
5494 struct node_queue_opt *opts)
5496 struct pf_altq pa, pb;
5497 char qname[PF_QNAME_SIZE];
5498 struct node_queue *n;
5499 struct node_queue_bw bw;
5502 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
5503 FREE_LIST(struct node_if, interfaces);
5505 FREE_LIST(struct node_queue, nqueues);
5509 LOOP_THROUGH(struct node_if, interface, interfaces,
5510 memcpy(&pa, a, sizeof(struct pf_altq));
5511 if (strlcpy(pa.ifname, interface->ifname,
5512 sizeof(pa.ifname)) >= sizeof(pa.ifname))
5513 errx(1, "expand_altq: strlcpy");
5515 if (interface->not) {
5516 yyerror("altq on ! <interface> is not supported");
5519 if (eval_pfaltq(pf, &pa, &bwspec, opts))
5522 if (pfctl_add_altq(pf, &pa))
5525 if (pf->opts & PF_OPT_VERBOSE) {
5526 print_altq(&pf->paltq->altq, 0,
5528 if (nqueues && nqueues->tail) {
5530 LOOP_THROUGH(struct node_queue, queue,
5540 if (pa.scheduler == ALTQT_CBQ ||
5541 pa.scheduler == ALTQT_HFSC ||
5542 pa.scheduler == ALTQT_FAIRQ) {
5543 /* now create a root queue */
5544 memset(&pb, 0, sizeof(struct pf_altq));
5545 if (strlcpy(qname, "root_", sizeof(qname)) >=
5547 errx(1, "expand_altq: strlcpy");
5548 if (strlcat(qname, interface->ifname,
5549 sizeof(qname)) >= sizeof(qname))
5550 errx(1, "expand_altq: strlcat");
5551 if (strlcpy(pb.qname, qname,
5552 sizeof(pb.qname)) >= sizeof(pb.qname))
5553 errx(1, "expand_altq: strlcpy");
5554 if (strlcpy(pb.ifname, interface->ifname,
5555 sizeof(pb.ifname)) >= sizeof(pb.ifname))
5556 errx(1, "expand_altq: strlcpy");
5557 pb.qlimit = pa.qlimit;
5558 pb.scheduler = pa.scheduler;
5559 bw.bw_absolute = pa.ifbandwidth;
5561 if (eval_pfqueue(pf, &pb, &bw, opts))
5564 if (pfctl_add_altq(pf, &pb))
5568 LOOP_THROUGH(struct node_queue, queue, nqueues,
5569 n = calloc(1, sizeof(struct node_queue));
5571 err(1, "expand_altq: calloc");
5572 if (pa.scheduler == ALTQT_CBQ ||
5573 pa.scheduler == ALTQT_HFSC ||
5574 pa.scheduler == ALTQT_FAIRQ)
5575 if (strlcpy(n->parent, qname,
5576 sizeof(n->parent)) >=
5578 errx(1, "expand_altq: strlcpy");
5579 if (strlcpy(n->queue, queue->queue,
5580 sizeof(n->queue)) >= sizeof(n->queue))
5581 errx(1, "expand_altq: strlcpy");
5582 if (strlcpy(n->ifname, interface->ifname,
5583 sizeof(n->ifname)) >= sizeof(n->ifname))
5584 errx(1, "expand_altq: strlcpy");
5585 n->scheduler = pa.scheduler;
5591 queues->tail->next = n;
5597 FREE_LIST(struct node_if, interfaces);
5599 FREE_LIST(struct node_queue, nqueues);
5605 expand_queue(struct pf_altq *a, struct node_if *interfaces,
5606 struct node_queue *nqueues, struct node_queue_bw bwspec,
5607 struct node_queue_opt *opts)
5609 struct node_queue *n, *nq;
5614 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
5615 FREE_LIST(struct node_queue, nqueues);
5619 if (queues == NULL) {
5620 yyerror("queue %s has no parent", a->qname);
5621 FREE_LIST(struct node_queue, nqueues);
5625 LOOP_THROUGH(struct node_if, interface, interfaces,
5626 LOOP_THROUGH(struct node_queue, tqueue, queues,
5627 if (!strncmp(a->qname, tqueue->queue, PF_QNAME_SIZE) &&
5628 (interface->ifname[0] == 0 ||
5629 (!interface->not && !strncmp(interface->ifname,
5630 tqueue->ifname, IFNAMSIZ)) ||
5631 (interface->not && strncmp(interface->ifname,
5632 tqueue->ifname, IFNAMSIZ)))) {
5633 /* found ourself in queues */
5636 memcpy(&pa, a, sizeof(struct pf_altq));
5638 if (pa.scheduler != ALTQT_NONE &&
5639 pa.scheduler != tqueue->scheduler) {
5640 yyerror("exactly one scheduler type "
5641 "per interface allowed");
5644 pa.scheduler = tqueue->scheduler;
5646 /* scheduler dependent error checking */
5647 switch (pa.scheduler) {
5649 if (nqueues != NULL) {
5650 yyerror("priq queues cannot "
5651 "have child queues");
5654 if (bwspec.bw_absolute > 0 ||
5655 bwspec.bw_percent < 100) {
5656 yyerror("priq doesn't take "
5665 if (strlcpy(pa.ifname, tqueue->ifname,
5666 sizeof(pa.ifname)) >= sizeof(pa.ifname))
5667 errx(1, "expand_queue: strlcpy");
5668 if (strlcpy(pa.parent, tqueue->parent,
5669 sizeof(pa.parent)) >= sizeof(pa.parent))
5670 errx(1, "expand_queue: strlcpy");
5672 if (eval_pfqueue(pf, &pa, &bwspec, opts))
5675 if (pfctl_add_altq(pf, &pa))
5678 for (nq = nqueues; nq != NULL; nq = nq->next) {
5679 if (!strcmp(a->qname, nq->queue)) {
5680 yyerror("queue cannot have "
5686 sizeof(struct node_queue));
5688 err(1, "expand_queue: calloc");
5689 if (strlcpy(n->parent, a->qname,
5690 sizeof(n->parent)) >=
5692 errx(1, "expand_queue strlcpy");
5693 if (strlcpy(n->queue, nq->queue,
5694 sizeof(n->queue)) >=
5696 errx(1, "expand_queue strlcpy");
5697 if (strlcpy(n->ifname, tqueue->ifname,
5698 sizeof(n->ifname)) >=
5700 errx(1, "expand_queue strlcpy");
5701 n->scheduler = tqueue->scheduler;
5707 queues->tail->next = n;
5711 if ((pf->opts & PF_OPT_VERBOSE) && (
5712 (found == 1 && interface->ifname[0] == 0) ||
5713 (found > 0 && interface->ifname[0] != 0))) {
5714 print_queue(&pf->paltq->altq, 0,
5715 &bwspec, interface->ifname[0] != 0,
5717 if (nqueues && nqueues->tail) {
5719 LOOP_THROUGH(struct node_queue,
5732 FREE_LIST(struct node_queue, nqueues);
5733 FREE_LIST(struct node_if, interfaces);
5736 yyerror("queue %s has no parent", a->qname);
5747 pf_af_to_proto(sa_family_t af)
5750 return (ETHERTYPE_IP);
5752 return (ETHERTYPE_IPV6);
5758 expand_eth_rule(struct pfctl_eth_rule *r,
5759 struct node_if *interfaces, struct node_etherproto *protos,
5760 struct node_mac *srcs, struct node_mac *dsts,
5761 struct node_host *ipsrcs, struct node_host *ipdsts, const char *anchor_call)
5763 LOOP_THROUGH(struct node_if, interface, interfaces,
5764 LOOP_THROUGH(struct node_etherproto, proto, protos,
5765 LOOP_THROUGH(struct node_mac, src, srcs,
5766 LOOP_THROUGH(struct node_mac, dst, dsts,
5767 LOOP_THROUGH(struct node_host, ipsrc, ipsrcs,
5768 LOOP_THROUGH(struct node_host, ipdst, ipdsts,
5769 strlcpy(r->ifname, interface->ifname,
5771 r->ifnot = interface->not;
5772 r->proto = proto->proto;
5773 if (!r->proto && ipsrc->af)
5774 r->proto = pf_af_to_proto(ipsrc->af);
5775 else if (!r->proto && ipdst->af)
5776 r->proto = pf_af_to_proto(ipdst->af);
5777 bcopy(src->mac, r->src.addr, ETHER_ADDR_LEN);
5778 bcopy(src->mask, r->src.mask, ETHER_ADDR_LEN);
5779 r->src.neg = src->neg;
5780 r->src.isset = src->isset;
5781 r->ipsrc.addr = ipsrc->addr;
5782 r->ipsrc.neg = ipsrc->not;
5783 r->ipdst.addr = ipdst->addr;
5784 r->ipdst.neg = ipdst->not;
5785 bcopy(dst->mac, r->dst.addr, ETHER_ADDR_LEN);
5786 bcopy(dst->mask, r->dst.mask, ETHER_ADDR_LEN);
5787 r->dst.neg = dst->neg;
5788 r->dst.isset = dst->isset;
5789 r->nr = pf->eastack[pf->asd]->match++;
5791 pfctl_append_eth_rule(pf, r, anchor_call);
5794 FREE_LIST(struct node_if, interfaces);
5795 FREE_LIST(struct node_etherproto, protos);
5796 FREE_LIST(struct node_mac, srcs);
5797 FREE_LIST(struct node_mac, dsts);
5798 FREE_LIST(struct node_host, ipsrcs);
5799 FREE_LIST(struct node_host, ipdsts);
5803 expand_rule(struct pfctl_rule *r,
5804 struct node_if *interfaces, struct node_host *rpool_hosts,
5805 struct node_proto *protos, struct node_os *src_oses,
5806 struct node_host *src_hosts, struct node_port *src_ports,
5807 struct node_host *dst_hosts, struct node_port *dst_ports,
5808 struct node_uid *uids, struct node_gid *gids, struct node_icmp *icmp_types,
5809 const char *anchor_call)
5811 sa_family_t af = r->af;
5812 int added = 0, error = 0;
5813 char ifname[IF_NAMESIZE];
5814 char label[PF_RULE_MAX_LABEL_COUNT][PF_RULE_LABEL_SIZE];
5815 char tagname[PF_TAG_NAME_SIZE];
5816 char match_tagname[PF_TAG_NAME_SIZE];
5817 struct pf_pooladdr *pa;
5818 struct node_host *h;
5819 u_int8_t flags, flagset, keep_state;
5821 memcpy(label, r->label, sizeof(r->label));
5822 assert(sizeof(r->label) == sizeof(label));
5823 if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname))
5824 errx(1, "expand_rule: strlcpy");
5825 if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >=
5826 sizeof(match_tagname))
5827 errx(1, "expand_rule: strlcpy");
5829 flagset = r->flagset;
5830 keep_state = r->keep_state;
5832 LOOP_THROUGH(struct node_if, interface, interfaces,
5833 LOOP_THROUGH(struct node_proto, proto, protos,
5834 LOOP_THROUGH(struct node_icmp, icmp_type, icmp_types,
5835 LOOP_THROUGH(struct node_host, src_host, src_hosts,
5836 LOOP_THROUGH(struct node_port, src_port, src_ports,
5837 LOOP_THROUGH(struct node_os, src_os, src_oses,
5838 LOOP_THROUGH(struct node_host, dst_host, dst_hosts,
5839 LOOP_THROUGH(struct node_port, dst_port, dst_ports,
5840 LOOP_THROUGH(struct node_uid, uid, uids,
5841 LOOP_THROUGH(struct node_gid, gid, gids,
5844 /* for link-local IPv6 address, interface must match up */
5845 if ((r->af && src_host->af && r->af != src_host->af) ||
5846 (r->af && dst_host->af && r->af != dst_host->af) ||
5847 (src_host->af && dst_host->af &&
5848 src_host->af != dst_host->af) ||
5849 (src_host->ifindex && dst_host->ifindex &&
5850 src_host->ifindex != dst_host->ifindex) ||
5851 (src_host->ifindex && *interface->ifname &&
5852 src_host->ifindex != if_nametoindex(interface->ifname)) ||
5853 (dst_host->ifindex && *interface->ifname &&
5854 dst_host->ifindex != if_nametoindex(interface->ifname)))
5856 if (!r->af && src_host->af)
5857 r->af = src_host->af;
5858 else if (!r->af && dst_host->af)
5859 r->af = dst_host->af;
5861 if (*interface->ifname)
5862 strlcpy(r->ifname, interface->ifname,
5864 else if (if_indextoname(src_host->ifindex, ifname))
5865 strlcpy(r->ifname, ifname, sizeof(r->ifname));
5866 else if (if_indextoname(dst_host->ifindex, ifname))
5867 strlcpy(r->ifname, ifname, sizeof(r->ifname));
5869 memset(r->ifname, '\0', sizeof(r->ifname));
5871 memcpy(r->label, label, sizeof(r->label));
5872 if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >=
5874 errx(1, "expand_rule: strlcpy");
5875 if (strlcpy(r->match_tagname, match_tagname,
5876 sizeof(r->match_tagname)) >= sizeof(r->match_tagname))
5877 errx(1, "expand_rule: strlcpy");
5879 error += check_netmask(src_host, r->af);
5880 error += check_netmask(dst_host, r->af);
5882 r->ifnot = interface->not;
5883 r->proto = proto->proto;
5884 r->src.addr = src_host->addr;
5885 r->src.neg = src_host->not;
5886 r->src.port[0] = src_port->port[0];
5887 r->src.port[1] = src_port->port[1];
5888 r->src.port_op = src_port->op;
5889 r->dst.addr = dst_host->addr;
5890 r->dst.neg = dst_host->not;
5891 r->dst.port[0] = dst_port->port[0];
5892 r->dst.port[1] = dst_port->port[1];
5893 r->dst.port_op = dst_port->op;
5894 r->uid.op = uid->op;
5895 r->uid.uid[0] = uid->uid[0];
5896 r->uid.uid[1] = uid->uid[1];
5897 r->gid.op = gid->op;
5898 r->gid.gid[0] = gid->gid[0];
5899 r->gid.gid[1] = gid->gid[1];
5900 r->type = icmp_type->type;
5901 r->code = icmp_type->code;
5903 if ((keep_state == PF_STATE_MODULATE ||
5904 keep_state == PF_STATE_SYNPROXY) &&
5905 r->proto && r->proto != IPPROTO_TCP)
5906 r->keep_state = PF_STATE_NORMAL;
5908 r->keep_state = keep_state;
5910 if (r->proto && r->proto != IPPROTO_TCP) {
5915 r->flagset = flagset;
5917 if (icmp_type->proto && r->proto != icmp_type->proto) {
5918 yyerror("icmp-type mismatch");
5922 if (src_os && src_os->os) {
5923 r->os_fingerprint = pfctl_get_fingerprint(src_os->os);
5924 if ((pf->opts & PF_OPT_VERBOSE2) &&
5925 r->os_fingerprint == PF_OSFP_NOMATCH)
5927 "warning: unknown '%s' OS fingerprint\n",
5930 r->os_fingerprint = PF_OSFP_ANY;
5933 TAILQ_INIT(&r->rpool.list);
5934 for (h = rpool_hosts; h != NULL; h = h->next) {
5935 pa = calloc(1, sizeof(struct pf_pooladdr));
5937 err(1, "expand_rule: calloc");
5939 if (h->ifname != NULL) {
5940 if (strlcpy(pa->ifname, h->ifname,
5941 sizeof(pa->ifname)) >=
5943 errx(1, "expand_rule: strlcpy");
5946 TAILQ_INSERT_TAIL(&r->rpool.list, pa, entries);
5949 if (rule_consistent(r, anchor_call[0]) < 0 || error)
5950 yyerror("skipping rule due to errors");
5952 r->nr = pf->astack[pf->asd]->match++;
5953 pfctl_append_rule(pf, r, anchor_call);
5959 FREE_LIST(struct node_if, interfaces);
5960 FREE_LIST(struct node_proto, protos);
5961 FREE_LIST(struct node_host, src_hosts);
5962 FREE_LIST(struct node_port, src_ports);
5963 FREE_LIST(struct node_os, src_oses);
5964 FREE_LIST(struct node_host, dst_hosts);
5965 FREE_LIST(struct node_port, dst_ports);
5966 FREE_LIST(struct node_uid, uids);
5967 FREE_LIST(struct node_gid, gids);
5968 FREE_LIST(struct node_icmp, icmp_types);
5969 FREE_LIST(struct node_host, rpool_hosts);
5972 yyerror("rule expands to no valid combination");
5976 expand_skip_interface(struct node_if *interfaces)
5980 if (!interfaces || (!interfaces->next && !interfaces->not &&
5981 !strcmp(interfaces->ifname, "none"))) {
5982 if (pf->opts & PF_OPT_VERBOSE)
5983 printf("set skip on none\n");
5984 errs = pfctl_set_interface_flags(pf, "", PFI_IFLAG_SKIP, 0);
5988 if (pf->opts & PF_OPT_VERBOSE)
5989 printf("set skip on {");
5990 LOOP_THROUGH(struct node_if, interface, interfaces,
5991 if (pf->opts & PF_OPT_VERBOSE)
5992 printf(" %s", interface->ifname);
5993 if (interface->not) {
5994 yyerror("skip on ! <interface> is not supported");
5997 errs += pfctl_set_interface_flags(pf,
5998 interface->ifname, PFI_IFLAG_SKIP, 1);
6000 if (pf->opts & PF_OPT_VERBOSE)
6003 FREE_LIST(struct node_if, interfaces);
6015 check_rulestate(int desired_state)
6017 if (require_order && (rulestate > desired_state)) {
6018 yyerror("Rules must be in order: options, ethernet, "
6019 "normalization, queueing, translation, filtering");
6022 rulestate = desired_state;
6027 kw_cmp(const void *k, const void *e)
6029 return (strcmp(k, ((const struct keywords *)e)->k_name));
6035 /* this has to be sorted always */
6036 static const struct keywords keywords[] = {
6038 { "allow-opts", ALLOWOPTS},
6040 { "anchor", ANCHOR},
6041 { "antispoof", ANTISPOOF},
6043 { "bandwidth", BANDWIDTH},
6045 { "binat-anchor", BINATANCHOR},
6046 { "bitmask", BITMASK},
6048 { "block-policy", BLOCKPOLICY},
6049 { "buckets", BUCKETS},
6053 { "crop", FRAGCROP},
6055 { "divert-reply", DIVERTREPLY},
6056 { "divert-to", DIVERTTO},
6057 { "dnpipe", DNPIPE},
6058 { "dnqueue", DNQUEUE},
6060 { "drop-ovl", FRAGDROP},
6063 { "fail-policy", FAILPOLICY},
6065 { "fastroute", FASTROUTE},
6066 { "file", FILENAME},
6067 { "fingerprints", FINGERPRINTS},
6069 { "floating", FLOATING},
6072 { "fragment", FRAGMENT},
6074 { "global", GLOBAL},
6078 { "hostid", HOSTID},
6079 { "icmp-type", ICMPTYPE},
6080 { "icmp6-type", ICMP6TYPE},
6081 { "if-bound", IFBOUND},
6083 { "include", INCLUDE},
6086 { "interval", INTERVAL},
6088 { "keepcounters", KEEPCOUNTERS},
6092 { "linkshare", LINKSHARE},
6095 { "loginterface", LOGINTERFACE},
6096 { "map-e-portset", MAPEPORTSET},
6099 { "max-mss", MAXMSS},
6100 { "max-src-conn", MAXSRCCONN},
6101 { "max-src-conn-rate", MAXSRCCONNRATE},
6102 { "max-src-nodes", MAXSRCNODES},
6103 { "max-src-states", MAXSRCSTATES},
6104 { "min-ttl", MINTTL},
6105 { "modulate", MODULATE},
6107 { "nat-anchor", NATANCHOR},
6110 { "no-route", NOROUTE},
6111 { "no-sync", NOSYNC},
6113 { "optimization", OPTIMIZATION},
6116 { "overload", OVERLOAD},
6120 { "priority", PRIORITY},
6122 { "probability", PROBABILITY},
6124 { "qlimit", QLIMIT},
6127 { "random", RANDOM},
6128 { "random-id", RANDOMID},
6130 { "rdr-anchor", RDRANCHOR},
6131 { "realtime", REALTIME},
6132 { "reassemble", REASSEMBLE},
6133 { "reply-to", REPLYTO},
6134 { "require-order", REQUIREORDER},
6135 { "return", RETURN},
6136 { "return-icmp", RETURNICMP},
6137 { "return-icmp6", RETURNICMP6},
6138 { "return-rst", RETURNRST},
6139 { "ridentifier", RIDENTIFIER},
6140 { "round-robin", ROUNDROBIN},
6142 { "route-to", ROUTETO},
6143 { "rtable", RTABLE},
6145 { "ruleset-optimization", RULESET_OPTIMIZATION},
6148 { "set-tos", SETTOS},
6150 { "sloppy", SLOPPY},
6151 { "source-hash", SOURCEHASH},
6152 { "source-track", SOURCETRACK},
6154 { "state-defaults", STATEDEFAULTS},
6155 { "state-policy", STATEPOLICY},
6156 { "static-port", STATICPORT},
6157 { "sticky-address", STICKYADDRESS},
6158 { "syncookies", SYNCOOKIES},
6159 { "synproxy", SYNPROXY},
6162 { "tagged", TAGGED},
6163 { "target", TARGET},
6164 { "tbrsize", TBRSIZE},
6165 { "timeout", TIMEOUT},
6169 { "upperlimit", UPPERLIMIT},
6170 { "urpf-failed", URPFFAILED},
6173 const struct keywords *p;
6175 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
6176 sizeof(keywords[0]), kw_cmp);
6180 fprintf(stderr, "%s: %d\n", s, p->k_val);
6184 fprintf(stderr, "string: %s\n", s);
6189 #define MAXPUSHBACK 128
6191 static char *parsebuf;
6192 static int parseindex;
6193 static char pushback_buffer[MAXPUSHBACK];
6194 static int pushback_index = 0;
6202 /* Read character from the parsebuffer instead of input. */
6203 if (parseindex >= 0) {
6204 c = parsebuf[parseindex++];
6213 return (pushback_buffer[--pushback_index]);
6216 if ((c = getc(file->stream)) == EOF) {
6217 yyerror("reached end of file while parsing quoted string");
6218 if (popfile() == EOF)
6225 while ((c = getc(file->stream)) == '\\') {
6226 next = getc(file->stream);
6231 yylval.lineno = file->lineno;
6236 if (popfile() == EOF)
6238 c = getc(file->stream);
6250 if (parseindex >= 0)
6253 if (pushback_index < MAXPUSHBACK-1)
6254 return (pushback_buffer[pushback_index++] = c);
6266 /* skip to either EOF or the first real EOL */
6269 c = pushback_buffer[--pushback_index];
6287 int quotec, next, c;
6292 while ((c = lgetc(0)) == ' ' || c == '\t')
6295 yylval.lineno = file->lineno;
6297 while ((c = lgetc(0)) != '\n' && c != EOF)
6299 if (c == '$' && parsebuf == NULL) {
6301 if ((c = lgetc(0)) == EOF)
6304 if (p + 1 >= buf + sizeof(buf) - 1) {
6305 yyerror("string too long");
6308 if (isalnum(c) || c == '_') {
6318 yyerror("macro '%s' not defined", buf);
6331 if ((c = lgetc(quotec)) == EOF)
6336 } else if (c == '\\') {
6337 if ((next = lgetc(quotec)) == EOF)
6339 if (next == quotec || c == ' ' || c == '\t')
6341 else if (next == '\n') {
6347 } else if (c == quotec) {
6351 if (p + 1 >= buf + sizeof(buf) - 1) {
6352 yyerror("string too long");
6357 yylval.v.string = strdup(buf);
6358 if (yylval.v.string == NULL)
6359 err(1, "yylex: strdup");
6364 yylval.v.i = PF_OP_XRG;
6365 return (PORTBINARY);
6372 yylval.v.i = PF_OP_IRG;
6373 return (PORTBINARY);
6385 #define allowed_to_end_number(x) \
6386 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
6388 if (c == '-' || isdigit(c)) {
6391 if ((unsigned)(p-buf) >= sizeof(buf)) {
6392 yyerror("string too long");
6395 } while ((c = lgetc(0)) != EOF && isdigit(c));
6397 if (p == buf + 1 && buf[0] == '-')
6399 if (c == EOF || allowed_to_end_number(c)) {
6400 const char *errstr = NULL;
6403 yylval.v.number = strtonum(buf, LLONG_MIN,
6404 LLONG_MAX, &errstr);
6406 yyerror("\"%s\" invalid number: %s",
6421 #define allowed_in_string(x) \
6422 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
6423 x != '{' && x != '}' && x != '<' && x != '>' && \
6424 x != '!' && x != '=' && x != '/' && x != '#' && \
6427 if (isalnum(c) || c == ':' || c == '_') {
6430 if ((unsigned)(p-buf) >= sizeof(buf)) {
6431 yyerror("string too long");
6434 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
6437 if ((token = lookup(buf)) == STRING)
6438 if ((yylval.v.string = strdup(buf)) == NULL)
6439 err(1, "yylex: strdup");
6443 yylval.lineno = file->lineno;
6452 check_file_secrecy(int fd, const char *fname)
6456 if (fstat(fd, &st)) {
6457 warn("cannot stat %s", fname);
6460 if (st.st_uid != 0 && st.st_uid != getuid()) {
6461 warnx("%s: owner not root or current user", fname);
6464 if (st.st_mode & (S_IRWXG | S_IRWXO)) {
6465 warnx("%s: group/world readable/writeable", fname);
6472 pushfile(const char *name, int secret)
6476 if ((nfile = calloc(1, sizeof(struct file))) == NULL ||
6477 (nfile->name = strdup(name)) == NULL) {
6481 if (TAILQ_FIRST(&files) == NULL && strcmp(nfile->name, "-") == 0) {
6482 nfile->stream = stdin;
6484 if ((nfile->name = strdup("stdin")) == NULL) {
6489 } else if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
6490 warn("%s", nfile->name);
6494 } else if (secret &&
6495 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
6496 fclose(nfile->stream);
6502 TAILQ_INSERT_TAIL(&files, nfile, entry);
6511 if ((prev = TAILQ_PREV(file, files, entry)) != NULL) {
6512 prev->errors += file->errors;
6513 TAILQ_REMOVE(&files, file, entry);
6514 fclose(file->stream);
6524 parse_config(char *filename, struct pfctl *xpf)
6531 rulestate = PFCTL_STATE_NONE;
6532 returnicmpdefault = (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
6533 returnicmp6default =
6534 (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
6535 blockpolicy = PFRULE_DROP;
6536 failpolicy = PFRULE_DROP;
6539 if ((file = pushfile(filename, 0)) == NULL) {
6540 warn("cannot open the main config file!");
6545 errors = file->errors;
6548 /* Free macros and check which have not been used. */
6549 while ((sym = TAILQ_FIRST(&symhead))) {
6550 if ((pf->opts & PF_OPT_VERBOSE2) && !sym->used)
6551 fprintf(stderr, "warning: macro '%s' not "
6552 "used\n", sym->nam);
6555 TAILQ_REMOVE(&symhead, sym, entry);
6559 return (errors ? -1 : 0);
6563 symset(const char *nam, const char *val, int persist)
6567 for (sym = TAILQ_FIRST(&symhead); sym && strcmp(nam, sym->nam);
6568 sym = TAILQ_NEXT(sym, entry))
6572 if (sym->persist == 1)
6577 TAILQ_REMOVE(&symhead, sym, entry);
6581 if ((sym = calloc(1, sizeof(*sym))) == NULL)
6584 sym->nam = strdup(nam);
6585 if (sym->nam == NULL) {
6589 sym->val = strdup(val);
6590 if (sym->val == NULL) {
6596 sym->persist = persist;
6597 TAILQ_INSERT_TAIL(&symhead, sym, entry);
6602 pfctl_cmdline_symset(char *s)
6607 if ((val = strrchr(s, '=')) == NULL)
6610 if ((sym = malloc(strlen(s) - strlen(val) + 1)) == NULL)
6611 err(1, "pfctl_cmdline_symset: malloc");
6613 strlcpy(sym, s, strlen(s) - strlen(val) + 1);
6615 ret = symset(sym, val + 1, 1);
6622 symget(const char *nam)
6626 TAILQ_FOREACH(sym, &symhead, entry)
6627 if (strcmp(nam, sym->nam) == 0) {
6635 mv_rules(struct pfctl_ruleset *src, struct pfctl_ruleset *dst)
6638 struct pfctl_rule *r;
6640 for (i = 0; i < PF_RULESET_MAX; ++i) {
6641 while ((r = TAILQ_FIRST(src->rules[i].active.ptr))
6643 TAILQ_REMOVE(src->rules[i].active.ptr, r, entries);
6644 TAILQ_INSERT_TAIL(dst->rules[i].active.ptr, r, entries);
6645 dst->anchor->match++;
6647 src->anchor->match = 0;
6648 while ((r = TAILQ_FIRST(src->rules[i].inactive.ptr))
6650 TAILQ_REMOVE(src->rules[i].inactive.ptr, r, entries);
6651 TAILQ_INSERT_TAIL(dst->rules[i].inactive.ptr,
6658 mv_eth_rules(struct pfctl_eth_ruleset *src, struct pfctl_eth_ruleset *dst)
6660 struct pfctl_eth_rule *r;
6662 while ((r = TAILQ_FIRST(&src->rules)) != NULL) {
6663 TAILQ_REMOVE(&src->rules, r, entries);
6664 TAILQ_INSERT_TAIL(&dst->rules, r, entries);
6665 dst->anchor->match++;
6667 src->anchor->match = 0;
6671 decide_address_family(struct node_host *n, sa_family_t *af)
6673 if (*af != 0 || n == NULL)
6676 while ((n = n->next) != NULL) {
6685 remove_invalid_hosts(struct node_host **nh, sa_family_t *af)
6687 struct node_host *n = *nh, *prev = NULL;
6690 if (*af && n->af && n->af != *af) {
6691 /* unlink and free n */
6692 struct node_host *next = n->next;
6694 /* adjust tail pointer */
6695 if (n == (*nh)->tail)
6697 /* adjust previous node's next pointer */
6703 if (n->ifname != NULL)
6717 invalid_redirect(struct node_host *nh, sa_family_t af)
6720 struct node_host *n;
6722 /* tables and dyniftl are ok without an address family */
6723 for (n = nh; n != NULL; n = n->next) {
6724 if (n->addr.type != PF_ADDR_TABLE &&
6725 n->addr.type != PF_ADDR_DYNIFTL) {
6726 yyerror("address family not given and "
6727 "translation address expands to multiple "
6728 "address families");
6734 yyerror("no translation address with matching address family "
6742 atoul(char *s, u_long *ulvalp)
6748 ulval = strtoul(s, &ep, 0);
6749 if (s[0] == '\0' || *ep != '\0')
6751 if (errno == ERANGE && ulval == ULONG_MAX)
6763 if (atoul(n, &ulval) == 0) {
6764 if (ulval > 65535) {
6765 yyerror("illegal port value %lu", ulval);
6768 return (htons(ulval));
6770 s = getservbyname(n, "tcp");
6772 s = getservbyname(n, "udp");
6774 yyerror("unknown port %s", n);
6782 rule_label(struct pfctl_rule *r, char *s[PF_RULE_MAX_LABEL_COUNT])
6784 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++) {
6788 if (strlcpy(r->label[i], s[i], sizeof(r->label[0])) >=
6789 sizeof(r->label[0])) {
6790 yyerror("rule label too long (max %d chars)",
6791 sizeof(r->label[0])-1);
6799 parseicmpspec(char *w, sa_family_t af)
6801 const struct icmpcodeent *p;
6806 icmptype = returnicmpdefault >> 8;
6808 icmptype = returnicmp6default >> 8;
6810 if (atoul(w, &ulval) == -1) {
6811 if ((p = geticmpcodebyname(icmptype, w, af)) == NULL) {
6812 yyerror("unknown icmp code %s", w);
6818 yyerror("invalid icmp code %lu", ulval);
6821 return (icmptype << 8 | ulval);
6825 parseport(char *port, struct range *r, int extensions)
6827 char *p = strchr(port, ':');
6830 if ((r->a = getservice(port)) == -1)
6836 if ((extensions & PPORT_STAR) && !strcmp(p+1, "*")) {
6838 if ((r->a = getservice(port)) == -1)
6844 if ((extensions & PPORT_RANGE)) {
6846 if ((r->a = getservice(port)) == -1 ||
6847 (r->b = getservice(p)) == -1)
6860 pfctl_load_anchors(int dev, struct pfctl *pf, struct pfr_buffer *trans)
6862 struct loadanchors *la;
6864 TAILQ_FOREACH(la, &loadanchorshead, entries) {
6865 if (pf->opts & PF_OPT_VERBOSE)
6866 fprintf(stderr, "\nLoading anchor %s from %s\n",
6867 la->anchorname, la->filename);
6868 if (pfctl_rules(dev, la->filename, pf->opts, pf->optimize,
6869 la->anchorname, trans) == -1)
6877 kw_casecmp(const void *k, const void *e)
6879 return (strcasecmp(k, ((const struct keywords *)e)->k_name));
6883 map_tos(char *s, int *val)
6885 /* DiffServ Codepoints and other TOS mappings */
6886 const struct keywords toswords[] = {
6887 { "af11", IPTOS_DSCP_AF11 },
6888 { "af12", IPTOS_DSCP_AF12 },
6889 { "af13", IPTOS_DSCP_AF13 },
6890 { "af21", IPTOS_DSCP_AF21 },
6891 { "af22", IPTOS_DSCP_AF22 },
6892 { "af23", IPTOS_DSCP_AF23 },
6893 { "af31", IPTOS_DSCP_AF31 },
6894 { "af32", IPTOS_DSCP_AF32 },
6895 { "af33", IPTOS_DSCP_AF33 },
6896 { "af41", IPTOS_DSCP_AF41 },
6897 { "af42", IPTOS_DSCP_AF42 },
6898 { "af43", IPTOS_DSCP_AF43 },
6899 { "critical", IPTOS_PREC_CRITIC_ECP },
6900 { "cs0", IPTOS_DSCP_CS0 },
6901 { "cs1", IPTOS_DSCP_CS1 },
6902 { "cs2", IPTOS_DSCP_CS2 },
6903 { "cs3", IPTOS_DSCP_CS3 },
6904 { "cs4", IPTOS_DSCP_CS4 },
6905 { "cs5", IPTOS_DSCP_CS5 },
6906 { "cs6", IPTOS_DSCP_CS6 },
6907 { "cs7", IPTOS_DSCP_CS7 },
6908 { "ef", IPTOS_DSCP_EF },
6909 { "inetcontrol", IPTOS_PREC_INTERNETCONTROL },
6910 { "lowdelay", IPTOS_LOWDELAY },
6911 { "netcontrol", IPTOS_PREC_NETCONTROL },
6912 { "reliability", IPTOS_RELIABILITY },
6913 { "throughput", IPTOS_THROUGHPUT },
6914 { "va", IPTOS_DSCP_VA }
6916 const struct keywords *p;
6918 p = bsearch(s, toswords, sizeof(toswords)/sizeof(toswords[0]),
6919 sizeof(toswords[0]), kw_casecmp);
6929 rt_tableid_max(void)
6933 size_t l = sizeof(fibs);
6935 if (sysctlbyname("net.fibs", &fibs, &l, NULL, 0) == -1)
6936 fibs = 16; /* XXX RT_MAXFIBS, at least limit it some. */
6938 * As the OpenBSD code only compares > and not >= we need to adjust
6939 * here given we only accept values of 0..n and want to avoid #ifdefs
6944 return (RT_TABLEID_MAX);
6949 node_mac_from_string(const char *str)
6953 m = calloc(1, sizeof(struct node_mac));
6955 err(1, "mac: calloc");
6957 if (sscanf(str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
6958 &m->mac[0], &m->mac[1], &m->mac[2], &m->mac[3], &m->mac[4],
6961 yyerror("invalid MAC address");
6965 memset(m->mask, 0xff, ETHER_ADDR_LEN);
6974 node_mac_from_string_masklen(const char *str, int masklen)
6978 if (masklen < 0 || masklen > (ETHER_ADDR_LEN * 8)) {
6979 yyerror("invalid MAC mask length");
6983 m = node_mac_from_string(str);
6987 memset(m->mask, 0, ETHER_ADDR_LEN);
6988 for (int i = 0; i < masklen; i++)
6989 m->mask[i / 8] |= 1 << (i % 8);
6995 node_mac_from_string_mask(const char *str, const char *mask)
6999 m = node_mac_from_string(str);
7003 if (sscanf(mask, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
7004 &m->mask[0], &m->mask[1], &m->mask[2], &m->mask[3], &m->mask[4],
7005 &m->mask[5]) != 6) {
7007 yyerror("invalid MAC mask");