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 /* stepping 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($$));
3264 if ($2.src.host != NULL &&
3265 $2.src.host->addr.type != PF_ADDR_ADDRMASK &&
3266 $2.src.host->addr.type != PF_ADDR_TABLE) {
3267 yyerror("from must be an address or table");
3270 if ($2.dst.host != NULL &&
3271 $2.dst.host->addr.type != PF_ADDR_ADDRMASK &&
3272 $2.dst.host->addr.type != PF_ADDR_TABLE) {
3273 yyerror("to must be an address or table");
3283 | etherfrom etherto {
3289 etherfrom : /* emtpy */ {
3290 bzero(&$$, sizeof($$));
3297 etherto : /* empty */ {
3298 bzero(&$$, sizeof($$));
3305 mac : string '/' NUMBER {
3306 $$ = node_mac_from_string_masklen($1, $3);
3312 if (strchr($1, '&')) {
3314 char *mac = strtok($1, "&");
3315 char *mask = strtok(NULL, "&");
3316 $$ = node_mac_from_string_mask(mac, mask);
3318 $$ = node_mac_from_string($1);
3328 for (n = $2; n != NULL; n = n->next)
3336 | '{' optnl mac_list '}'
3341 mac_list : xmac optnl {
3344 | mac_list comma xmac {
3347 else if ($1 == NULL)
3350 $1->tail->next = $3;
3351 $1->tail = $3->tail;
3370 os : /* empty */ { $$ = NULL; }
3371 | OS xos { $$ = $2; }
3372 | OS '{' optnl os_list '}' { $$ = $4; }
3376 $$ = calloc(1, sizeof(struct node_os));
3378 err(1, "os: calloc");
3384 os_list : xos optnl { $$ = $1; }
3385 | os_list comma xos optnl {
3386 $1->tail->next = $3;
3392 from : /* empty */ {
3406 if (disallow_urpf_failed($2.host, "\"urpf-failed\" is "
3407 "not permitted in a destination address"))
3413 ipportspec : ipspec {
3417 | ipspec PORT portspec {
3431 ipspec : ANY { $$ = NULL; }
3432 | xhost { $$ = $1; }
3433 | '{' optnl host_list '}' { $$ = $3; }
3436 toipspec : TO ipspec { $$ = $2; }
3437 | /* empty */ { $$ = NULL; }
3440 host_list : ipspec optnl { $$ = $1; }
3441 | host_list comma ipspec optnl {
3444 else if ($1 == NULL)
3447 $1->tail->next = $3;
3448 $1->tail = $3->tail;
3455 struct node_host *n;
3457 for (n = $2; n != NULL; n = n->next)
3462 $$ = calloc(1, sizeof(struct node_host));
3464 err(1, "xhost: calloc");
3465 $$->addr.type = PF_ADDR_NOROUTE;
3471 $$ = calloc(1, sizeof(struct node_host));
3473 err(1, "xhost: calloc");
3474 $$->addr.type = PF_ADDR_URPFFAILED;
3482 if (($$ = host($1)) == NULL) {
3483 /* error. "any" is handled elsewhere */
3485 yyerror("could not parse host specification");
3491 | STRING '-' STRING {
3492 struct node_host *b, *e;
3494 if ((b = host($1)) == NULL || (e = host($3)) == NULL) {
3497 yyerror("could not parse host specification");
3500 if (b->af != e->af ||
3501 b->addr.type != PF_ADDR_ADDRMASK ||
3502 e->addr.type != PF_ADDR_ADDRMASK ||
3503 unmask(&b->addr.v.a.mask, b->af) !=
3504 (b->af == AF_INET ? 32 : 128) ||
3505 unmask(&e->addr.v.a.mask, e->af) !=
3506 (e->af == AF_INET ? 32 : 128) ||
3507 b->next != NULL || b->not ||
3508 e->next != NULL || e->not) {
3513 yyerror("invalid address range");
3516 memcpy(&b->addr.v.a.mask, &e->addr.v.a.addr,
3517 sizeof(b->addr.v.a.mask));
3518 b->addr.type = PF_ADDR_RANGE;
3524 | STRING '/' NUMBER {
3527 if (asprintf(&buf, "%s/%lld", $1, (long long)$3) == -1)
3528 err(1, "host: asprintf");
3530 if (($$ = host(buf)) == NULL) {
3531 /* error. "any" is handled elsewhere */
3533 yyerror("could not parse host specification");
3538 | NUMBER '/' NUMBER {
3541 /* ie. for 10/8 parsing */
3543 if (asprintf(&buf, "%lld/%lld", (long long)$1, (long long)$3) == -1)
3545 if (asprintf(&buf, "%lld/%lld", $1, $3) == -1)
3547 err(1, "host: asprintf");
3548 if (($$ = host(buf)) == NULL) {
3549 /* error. "any" is handled elsewhere */
3551 yyerror("could not parse host specification");
3557 | dynaddr '/' NUMBER {
3558 struct node_host *n;
3560 if ($3 < 0 || $3 > 128) {
3561 yyerror("bit number too big");
3565 for (n = $1; n != NULL; n = n->next)
3569 if (strlen($2) >= PF_TABLE_NAME_SIZE) {
3570 yyerror("table name '%s' too long", $2);
3574 $$ = calloc(1, sizeof(struct node_host));
3576 err(1, "host: calloc");
3577 $$->addr.type = PF_ADDR_TABLE;
3578 if (strlcpy($$->addr.v.tblname, $2,
3579 sizeof($$->addr.v.tblname)) >=
3580 sizeof($$->addr.v.tblname))
3581 errx(1, "host: strlcpy");
3592 if (atoul($1, &ulval) == -1) {
3593 yyerror("%s is not a number", $1);
3602 dynaddr : '(' STRING ')' {
3607 if (!isalpha(op[0])) {
3608 yyerror("invalid interface name '%s'", op);
3612 while ((p = strrchr($2, ':')) != NULL) {
3613 if (!strcmp(p+1, "network"))
3614 flags |= PFI_AFLAG_NETWORK;
3615 else if (!strcmp(p+1, "broadcast"))
3616 flags |= PFI_AFLAG_BROADCAST;
3617 else if (!strcmp(p+1, "peer"))
3618 flags |= PFI_AFLAG_PEER;
3619 else if (!strcmp(p+1, "0"))
3620 flags |= PFI_AFLAG_NOALIAS;
3622 yyerror("interface %s has bad modifier",
3629 if (flags & (flags - 1) & PFI_AFLAG_MODEMASK) {
3631 yyerror("illegal combination of "
3632 "interface modifiers");
3635 $$ = calloc(1, sizeof(struct node_host));
3637 err(1, "address: calloc");
3639 set_ipmask($$, 128);
3640 $$->addr.type = PF_ADDR_DYNIFTL;
3641 $$->addr.iflags = flags;
3642 if (strlcpy($$->addr.v.ifname, $2,
3643 sizeof($$->addr.v.ifname)) >=
3644 sizeof($$->addr.v.ifname)) {
3647 yyerror("interface name too long");
3656 portspec : port_item { $$ = $1; }
3657 | '{' optnl port_list '}' { $$ = $3; }
3660 port_list : port_item optnl { $$ = $1; }
3661 | port_list comma port_item optnl {
3662 $1->tail->next = $3;
3668 port_item : portrange {
3669 $$ = calloc(1, sizeof(struct node_port));
3671 err(1, "port_item: calloc");
3681 | unaryop portrange {
3683 yyerror("':' cannot be used with an other "
3687 $$ = calloc(1, sizeof(struct node_port));
3689 err(1, "port_item: calloc");
3696 | portrange PORTBINARY portrange {
3698 yyerror("':' cannot be used with an other "
3702 $$ = calloc(1, sizeof(struct node_port));
3704 err(1, "port_item: calloc");
3713 portplain : numberstring {
3714 if (parseport($1, &$$, 0) == -1) {
3722 portrange : numberstring {
3723 if (parseport($1, &$$, PPORT_RANGE) == -1) {
3731 uids : uid_item { $$ = $1; }
3732 | '{' optnl uid_list '}' { $$ = $3; }
3735 uid_list : uid_item optnl { $$ = $1; }
3736 | uid_list comma uid_item optnl {
3737 $1->tail->next = $3;
3744 $$ = calloc(1, sizeof(struct node_uid));
3746 err(1, "uid_item: calloc");
3754 if ($2 == UID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
3755 yyerror("user unknown requires operator = or "
3759 $$ = calloc(1, sizeof(struct node_uid));
3761 err(1, "uid_item: calloc");
3768 | uid PORTBINARY uid {
3769 if ($1 == UID_MAX || $3 == UID_MAX) {
3770 yyerror("user unknown requires operator = or "
3774 $$ = calloc(1, sizeof(struct node_uid));
3776 err(1, "uid_item: calloc");
3786 if (!strcmp($1, "unknown"))
3791 if ((pw = getpwnam($1)) == NULL) {
3792 yyerror("unknown user %s", $1);
3801 if ($1 < 0 || $1 >= UID_MAX) {
3802 yyerror("illegal uid value %lu", $1);
3809 gids : gid_item { $$ = $1; }
3810 | '{' optnl gid_list '}' { $$ = $3; }
3813 gid_list : gid_item optnl { $$ = $1; }
3814 | gid_list comma gid_item optnl {
3815 $1->tail->next = $3;
3822 $$ = calloc(1, sizeof(struct node_gid));
3824 err(1, "gid_item: calloc");
3832 if ($2 == GID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
3833 yyerror("group unknown requires operator = or "
3837 $$ = calloc(1, sizeof(struct node_gid));
3839 err(1, "gid_item: calloc");
3846 | gid PORTBINARY gid {
3847 if ($1 == GID_MAX || $3 == GID_MAX) {
3848 yyerror("group unknown requires operator = or "
3852 $$ = calloc(1, sizeof(struct node_gid));
3854 err(1, "gid_item: calloc");
3864 if (!strcmp($1, "unknown"))
3869 if ((grp = getgrnam($1)) == NULL) {
3870 yyerror("unknown group %s", $1);
3879 if ($1 < 0 || $1 >= GID_MAX) {
3880 yyerror("illegal gid value %lu", $1);
3890 if ((f = parse_flags($1)) < 0) {
3891 yyerror("bad flags %s", $1);
3900 flags : FLAGS flag '/' flag { $$.b1 = $2.b1; $$.b2 = $4.b1; }
3901 | FLAGS '/' flag { $$.b1 = 0; $$.b2 = $3.b1; }
3902 | FLAGS ANY { $$.b1 = 0; $$.b2 = 0; }
3905 icmpspec : ICMPTYPE icmp_item { $$ = $2; }
3906 | ICMPTYPE '{' optnl icmp_list '}' { $$ = $4; }
3907 | ICMP6TYPE icmp6_item { $$ = $2; }
3908 | ICMP6TYPE '{' optnl icmp6_list '}' { $$ = $4; }
3911 icmp_list : icmp_item optnl { $$ = $1; }
3912 | icmp_list comma icmp_item optnl {
3913 $1->tail->next = $3;
3919 icmp6_list : icmp6_item optnl { $$ = $1; }
3920 | icmp6_list comma icmp6_item optnl {
3921 $1->tail->next = $3;
3927 icmp_item : icmptype {
3928 $$ = calloc(1, sizeof(struct node_icmp));
3930 err(1, "icmp_item: calloc");
3933 $$->proto = IPPROTO_ICMP;
3937 | icmptype CODE STRING {
3938 const struct icmpcodeent *p;
3940 if ((p = geticmpcodebyname($1-1, $3, AF_INET)) == NULL) {
3941 yyerror("unknown icmp-code %s", $3);
3947 $$ = calloc(1, sizeof(struct node_icmp));
3949 err(1, "icmp_item: calloc");
3951 $$->code = p->code + 1;
3952 $$->proto = IPPROTO_ICMP;
3956 | icmptype CODE NUMBER {
3957 if ($3 < 0 || $3 > 255) {
3958 yyerror("illegal icmp-code %lu", $3);
3961 $$ = calloc(1, sizeof(struct node_icmp));
3963 err(1, "icmp_item: calloc");
3966 $$->proto = IPPROTO_ICMP;
3972 icmp6_item : icmp6type {
3973 $$ = calloc(1, sizeof(struct node_icmp));
3975 err(1, "icmp_item: calloc");
3978 $$->proto = IPPROTO_ICMPV6;
3982 | icmp6type CODE STRING {
3983 const struct icmpcodeent *p;
3985 if ((p = geticmpcodebyname($1-1, $3, AF_INET6)) == NULL) {
3986 yyerror("unknown icmp6-code %s", $3);
3992 $$ = calloc(1, sizeof(struct node_icmp));
3994 err(1, "icmp_item: calloc");
3996 $$->code = p->code + 1;
3997 $$->proto = IPPROTO_ICMPV6;
4001 | icmp6type CODE NUMBER {
4002 if ($3 < 0 || $3 > 255) {
4003 yyerror("illegal icmp-code %lu", $3);
4006 $$ = calloc(1, sizeof(struct node_icmp));
4008 err(1, "icmp_item: calloc");
4011 $$->proto = IPPROTO_ICMPV6;
4018 const struct icmptypeent *p;
4020 if ((p = geticmptypebyname($1, AF_INET)) == NULL) {
4021 yyerror("unknown icmp-type %s", $1);
4029 if ($1 < 0 || $1 > 255) {
4030 yyerror("illegal icmp-type %lu", $1);
4037 icmp6type : STRING {
4038 const struct icmptypeent *p;
4040 if ((p = geticmptypebyname($1, AF_INET6)) ==
4042 yyerror("unknown icmp6-type %s", $1);
4050 if ($1 < 0 || $1 > 255) {
4051 yyerror("illegal icmp6-type %lu", $1);
4062 if (map_tos($1, &val))
4064 else if ($1[0] == '0' && $1[1] == 'x') {
4066 $$ = strtoul($1, &end, 16);
4067 if (errno || *end != '\0')
4070 $$ = 256; /* flag bad argument */
4071 if ($$ < 0 || $$ > 255) {
4072 yyerror("illegal tos value %s", $1);
4080 if ($$ < 0 || $$ > 255) {
4081 yyerror("illegal tos value %s", $1);
4087 sourcetrack : SOURCETRACK { $$ = PF_SRCTRACK; }
4088 | SOURCETRACK GLOBAL { $$ = PF_SRCTRACK_GLOBAL; }
4089 | SOURCETRACK RULE { $$ = PF_SRCTRACK_RULE; }
4092 statelock : IFBOUND {
4093 $$ = PFRULE_IFBOUND;
4104 | KEEP STATE state_opt_spec {
4105 $$.action = PF_STATE_NORMAL;
4108 | MODULATE STATE state_opt_spec {
4109 $$.action = PF_STATE_MODULATE;
4112 | SYNPROXY STATE state_opt_spec {
4113 $$.action = PF_STATE_SYNPROXY;
4118 flush : /* empty */ { $$ = 0; }
4119 | FLUSH { $$ = PF_FLUSH; }
4121 $$ = PF_FLUSH | PF_FLUSH_GLOBAL;
4125 state_opt_spec : '(' state_opt_list ')' { $$ = $2; }
4126 | /* empty */ { $$ = NULL; }
4129 state_opt_list : state_opt_item { $$ = $1; }
4130 | state_opt_list comma state_opt_item {
4131 $1->tail->next = $3;
4137 state_opt_item : MAXIMUM NUMBER {
4138 if ($2 < 0 || $2 > UINT_MAX) {
4139 yyerror("only positive values permitted");
4142 $$ = calloc(1, sizeof(struct node_state_opt));
4144 err(1, "state_opt_item: calloc");
4145 $$->type = PF_STATE_OPT_MAX;
4146 $$->data.max_states = $2;
4151 $$ = calloc(1, sizeof(struct node_state_opt));
4153 err(1, "state_opt_item: calloc");
4154 $$->type = PF_STATE_OPT_NOSYNC;
4158 | MAXSRCSTATES NUMBER {
4159 if ($2 < 0 || $2 > UINT_MAX) {
4160 yyerror("only positive values permitted");
4163 $$ = calloc(1, sizeof(struct node_state_opt));
4165 err(1, "state_opt_item: calloc");
4166 $$->type = PF_STATE_OPT_MAX_SRC_STATES;
4167 $$->data.max_src_states = $2;
4171 | MAXSRCCONN NUMBER {
4172 if ($2 < 0 || $2 > UINT_MAX) {
4173 yyerror("only positive values permitted");
4176 $$ = calloc(1, sizeof(struct node_state_opt));
4178 err(1, "state_opt_item: calloc");
4179 $$->type = PF_STATE_OPT_MAX_SRC_CONN;
4180 $$->data.max_src_conn = $2;
4184 | MAXSRCCONNRATE NUMBER '/' NUMBER {
4185 if ($2 < 0 || $2 > UINT_MAX ||
4186 $4 < 0 || $4 > UINT_MAX) {
4187 yyerror("only positive values permitted");
4190 $$ = calloc(1, sizeof(struct node_state_opt));
4192 err(1, "state_opt_item: calloc");
4193 $$->type = PF_STATE_OPT_MAX_SRC_CONN_RATE;
4194 $$->data.max_src_conn_rate.limit = $2;
4195 $$->data.max_src_conn_rate.seconds = $4;
4199 | OVERLOAD '<' STRING '>' flush {
4200 if (strlen($3) >= PF_TABLE_NAME_SIZE) {
4201 yyerror("table name '%s' too long", $3);
4205 $$ = calloc(1, sizeof(struct node_state_opt));
4207 err(1, "state_opt_item: calloc");
4208 if (strlcpy($$->data.overload.tblname, $3,
4209 PF_TABLE_NAME_SIZE) >= PF_TABLE_NAME_SIZE)
4210 errx(1, "state_opt_item: strlcpy");
4212 $$->type = PF_STATE_OPT_OVERLOAD;
4213 $$->data.overload.flush = $5;
4217 | MAXSRCNODES NUMBER {
4218 if ($2 < 0 || $2 > UINT_MAX) {
4219 yyerror("only positive values permitted");
4222 $$ = calloc(1, sizeof(struct node_state_opt));
4224 err(1, "state_opt_item: calloc");
4225 $$->type = PF_STATE_OPT_MAX_SRC_NODES;
4226 $$->data.max_src_nodes = $2;
4231 $$ = calloc(1, sizeof(struct node_state_opt));
4233 err(1, "state_opt_item: calloc");
4234 $$->type = PF_STATE_OPT_SRCTRACK;
4235 $$->data.src_track = $1;
4240 $$ = calloc(1, sizeof(struct node_state_opt));
4242 err(1, "state_opt_item: calloc");
4243 $$->type = PF_STATE_OPT_STATELOCK;
4244 $$->data.statelock = $1;
4249 $$ = calloc(1, sizeof(struct node_state_opt));
4251 err(1, "state_opt_item: calloc");
4252 $$->type = PF_STATE_OPT_SLOPPY;
4259 if ($2 < 0 || $2 > UINT_MAX) {
4260 yyerror("only positive values permitted");
4263 for (i = 0; pf_timeouts[i].name &&
4264 strcmp(pf_timeouts[i].name, $1); ++i)
4266 if (!pf_timeouts[i].name) {
4267 yyerror("illegal timeout name %s", $1);
4271 if (strchr(pf_timeouts[i].name, '.') == NULL) {
4272 yyerror("illegal state timeout %s", $1);
4277 $$ = calloc(1, sizeof(struct node_state_opt));
4279 err(1, "state_opt_item: calloc");
4280 $$->type = PF_STATE_OPT_TIMEOUT;
4281 $$->data.timeout.number = pf_timeouts[i].timeout;
4282 $$->data.timeout.seconds = $2;
4288 label : LABEL STRING {
4293 etherqname : QUEUE STRING {
4296 | QUEUE '(' STRING ')' {
4301 qname : QUEUE STRING {
4305 | QUEUE '(' STRING ')' {
4309 | QUEUE '(' STRING comma STRING ')' {
4315 no : /* empty */ { $$ = 0; }
4319 portstar : numberstring {
4320 if (parseport($1, &$$, PPORT_RANGE|PPORT_STAR) == -1) {
4328 redirspec : host { $$ = $1; }
4329 | '{' optnl redir_host_list '}' { $$ = $3; }
4332 redir_host_list : host optnl { $$ = $1; }
4333 | redir_host_list comma host optnl {
4334 $1->tail->next = $3;
4335 $1->tail = $3->tail;
4340 redirpool : /* empty */ { $$ = NULL; }
4342 $$ = calloc(1, sizeof(struct redirection));
4344 err(1, "redirection: calloc");
4346 $$->rport.a = $$->rport.b = $$->rport.t = 0;
4348 | ARROW redirspec PORT portstar {
4349 $$ = calloc(1, sizeof(struct redirection));
4351 err(1, "redirection: calloc");
4357 hashkey : /* empty */
4359 $$ = calloc(1, sizeof(struct pf_poolhashkey));
4361 err(1, "hashkey: calloc");
4362 $$->key32[0] = arc4random();
4363 $$->key32[1] = arc4random();
4364 $$->key32[2] = arc4random();
4365 $$->key32[3] = arc4random();
4369 if (!strncmp($1, "0x", 2)) {
4370 if (strlen($1) != 34) {
4372 yyerror("hex key must be 128 bits "
4373 "(32 hex digits) long");
4376 $$ = calloc(1, sizeof(struct pf_poolhashkey));
4378 err(1, "hashkey: calloc");
4380 if (sscanf($1, "0x%8x%8x%8x%8x",
4381 &$$->key32[0], &$$->key32[1],
4382 &$$->key32[2], &$$->key32[3]) != 4) {
4385 yyerror("invalid hex key");
4391 $$ = calloc(1, sizeof(struct pf_poolhashkey));
4393 err(1, "hashkey: calloc");
4395 MD5Update(&context, (unsigned char *)$1,
4397 MD5Final((unsigned char *)$$, &context);
4398 HTONL($$->key32[0]);
4399 HTONL($$->key32[1]);
4400 HTONL($$->key32[2]);
4401 HTONL($$->key32[3]);
4407 pool_opts : { bzero(&pool_opts, sizeof pool_opts); }
4411 bzero(&pool_opts, sizeof pool_opts);
4416 pool_opts_l : pool_opts_l pool_opt
4420 pool_opt : BITMASK {
4421 if (pool_opts.type) {
4422 yyerror("pool type cannot be redefined");
4425 pool_opts.type = PF_POOL_BITMASK;
4428 if (pool_opts.type) {
4429 yyerror("pool type cannot be redefined");
4432 pool_opts.type = PF_POOL_RANDOM;
4434 | SOURCEHASH hashkey {
4435 if (pool_opts.type) {
4436 yyerror("pool type cannot be redefined");
4439 pool_opts.type = PF_POOL_SRCHASH;
4443 if (pool_opts.type) {
4444 yyerror("pool type cannot be redefined");
4447 pool_opts.type = PF_POOL_ROUNDROBIN;
4450 if (pool_opts.staticport) {
4451 yyerror("static-port cannot be redefined");
4454 pool_opts.staticport = 1;
4457 if (filter_opts.marker & POM_STICKYADDRESS) {
4458 yyerror("sticky-address cannot be redefined");
4461 pool_opts.marker |= POM_STICKYADDRESS;
4462 pool_opts.opts |= PF_POOL_STICKYADDR;
4464 | MAPEPORTSET number '/' number '/' number {
4465 if (pool_opts.mape.offset) {
4466 yyerror("map-e-portset cannot be redefined");
4469 if (pool_opts.type) {
4470 yyerror("map-e-portset cannot be used with "
4474 if ($2 <= 0 || $2 >= 16) {
4475 yyerror("MAP-E PSID offset must be 1-15");
4478 if ($4 < 0 || $4 >= 16 || $2 + $4 > 16) {
4479 yyerror("Invalid MAP-E PSID length");
4481 } else if ($4 == 0) {
4482 yyerror("PSID Length = 0: this means"
4483 " you do not need MAP-E");
4486 if ($6 < 0 || $6 > 65535) {
4487 yyerror("Invalid MAP-E PSID");
4490 pool_opts.mape.offset = $2;
4491 pool_opts.mape.psidlen = $4;
4492 pool_opts.mape.psid = $6;
4496 redirection : /* empty */ { $$ = NULL; }
4498 $$ = calloc(1, sizeof(struct redirection));
4500 err(1, "redirection: calloc");
4502 $$->rport.a = $$->rport.b = $$->rport.t = 0;
4504 | ARROW host PORT portstar {
4505 $$ = calloc(1, sizeof(struct redirection));
4507 err(1, "redirection: calloc");
4513 natpasslog : /* empty */ { $$.b1 = $$.b2 = 0; $$.w2 = 0; }
4514 | PASS { $$.b1 = 1; $$.b2 = 0; $$.w2 = 0; }
4515 | PASS log { $$.b1 = 1; $$.b2 = $2.log; $$.w2 = $2.logif; }
4516 | log { $$.b1 = 0; $$.b2 = $1.log; $$.w2 = $1.logif; }
4519 nataction : no NAT natpasslog {
4521 yyerror("\"pass\" not valid with \"no\"");
4532 | no RDR natpasslog {
4534 yyerror("\"pass\" not valid with \"no\"");
4547 natrule : nataction interface af proto fromto tag tagged rtable
4550 struct pfctl_rule r;
4552 if (check_rulestate(PFCTL_STATE_NAT))
4555 memset(&r, 0, sizeof(r));
4564 if ($5.src.host && $5.src.host->af &&
4565 !$5.src.host->ifindex)
4566 r.af = $5.src.host->af;
4567 else if ($5.dst.host && $5.dst.host->af &&
4568 !$5.dst.host->ifindex)
4569 r.af = $5.dst.host->af;
4573 if (strlcpy(r.tagname, $6, PF_TAG_NAME_SIZE) >=
4575 yyerror("tag too long, max %u chars",
4576 PF_TAG_NAME_SIZE - 1);
4581 if (strlcpy(r.match_tagname, $7.name,
4582 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4583 yyerror("tag too long, max %u chars",
4584 PF_TAG_NAME_SIZE - 1);
4587 r.match_tag_not = $7.neg;
4590 if (r.action == PF_NONAT || r.action == PF_NORDR) {
4592 yyerror("translation rule with 'no' "
4593 "does not need '->'");
4597 if ($9 == NULL || $9->host == NULL) {
4598 yyerror("translation rule requires '-> "
4602 if (!r.af && ! $9->host->ifindex)
4603 r.af = $9->host->af;
4605 remove_invalid_hosts(&$9->host, &r.af);
4606 if (invalid_redirect($9->host, r.af))
4608 if (check_netmask($9->host, r.af))
4611 r.rpool.proxy_port[0] = ntohs($9->rport.a);
4615 if (!$9->rport.b && $9->rport.t &&
4616 $5.dst.port != NULL) {
4617 r.rpool.proxy_port[1] =
4618 ntohs($9->rport.a) +
4620 $5.dst.port->port[1]) -
4622 $5.dst.port->port[0]));
4624 r.rpool.proxy_port[1] =
4628 r.rpool.proxy_port[1] =
4630 if (!r.rpool.proxy_port[0] &&
4631 !r.rpool.proxy_port[1]) {
4632 r.rpool.proxy_port[0] =
4633 PF_NAT_PROXY_PORT_LOW;
4634 r.rpool.proxy_port[1] =
4635 PF_NAT_PROXY_PORT_HIGH;
4636 } else if (!r.rpool.proxy_port[1])
4637 r.rpool.proxy_port[1] =
4638 r.rpool.proxy_port[0];
4644 r.rpool.opts = $10.type;
4645 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
4646 PF_POOL_NONE && ($9->host->next != NULL ||
4647 $9->host->addr.type == PF_ADDR_TABLE ||
4648 DYNIF_MULTIADDR($9->host->addr)))
4649 r.rpool.opts = PF_POOL_ROUNDROBIN;
4650 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4651 PF_POOL_ROUNDROBIN &&
4652 disallow_table($9->host, "tables are only "
4653 "supported in round-robin redirection "
4656 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4657 PF_POOL_ROUNDROBIN &&
4658 disallow_alias($9->host, "interface (%s) "
4659 "is only supported in round-robin "
4660 "redirection pools"))
4662 if ($9->host->next != NULL) {
4663 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4664 PF_POOL_ROUNDROBIN) {
4665 yyerror("only round-robin "
4666 "valid for multiple "
4667 "redirection addresses");
4673 if ($10.key != NULL)
4674 memcpy(&r.rpool.key, $10.key,
4675 sizeof(struct pf_poolhashkey));
4678 r.rpool.opts |= $10.opts;
4680 if ($10.staticport) {
4681 if (r.action != PF_NAT) {
4682 yyerror("the 'static-port' option is "
4683 "only valid with nat rules");
4686 if (r.rpool.proxy_port[0] !=
4687 PF_NAT_PROXY_PORT_LOW &&
4688 r.rpool.proxy_port[1] !=
4689 PF_NAT_PROXY_PORT_HIGH) {
4690 yyerror("the 'static-port' option can't"
4691 " be used when specifying a port"
4695 r.rpool.proxy_port[0] = 0;
4696 r.rpool.proxy_port[1] = 0;
4699 if ($10.mape.offset) {
4700 if (r.action != PF_NAT) {
4701 yyerror("the 'map-e-portset' option is"
4702 " only valid with nat rules");
4705 if ($10.staticport) {
4706 yyerror("the 'map-e-portset' option"
4707 " can't be used 'static-port'");
4710 if (r.rpool.proxy_port[0] !=
4711 PF_NAT_PROXY_PORT_LOW &&
4712 r.rpool.proxy_port[1] !=
4713 PF_NAT_PROXY_PORT_HIGH) {
4714 yyerror("the 'map-e-portset' option"
4715 " can't be used when specifying"
4719 r.rpool.mape = $10.mape;
4722 expand_rule(&r, $2, $9 == NULL ? NULL : $9->host, $4,
4723 $5.src_os, $5.src.host, $5.src.port, $5.dst.host,
4724 $5.dst.port, 0, 0, 0, "");
4729 binatrule : no BINAT natpasslog interface af proto FROM ipspec toipspec tag
4730 tagged rtable redirection
4732 struct pfctl_rule binat;
4733 struct pf_pooladdr *pa;
4735 if (check_rulestate(PFCTL_STATE_NAT))
4737 if (disallow_urpf_failed($9, "\"urpf-failed\" is not "
4738 "permitted as a binat destination"))
4741 memset(&binat, 0, sizeof(binat));
4744 yyerror("\"pass\" not valid with \"no\"");
4748 binat.action = PF_NOBINAT;
4750 binat.action = PF_BINAT;
4751 binat.natpass = $3.b1;
4753 binat.logif = $3.w2;
4755 if (!binat.af && $8 != NULL && $8->af)
4757 if (!binat.af && $9 != NULL && $9->af)
4760 if (!binat.af && $13 != NULL && $13->host)
4761 binat.af = $13->host->af;
4763 yyerror("address family (inet/inet6) "
4769 memcpy(binat.ifname, $4->ifname,
4770 sizeof(binat.ifname));
4771 binat.ifnot = $4->not;
4776 if (strlcpy(binat.tagname, $10,
4777 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4778 yyerror("tag too long, max %u chars",
4779 PF_TAG_NAME_SIZE - 1);
4783 if (strlcpy(binat.match_tagname, $11.name,
4784 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4785 yyerror("tag too long, max %u chars",
4786 PF_TAG_NAME_SIZE - 1);
4789 binat.match_tag_not = $11.neg;
4790 binat.rtableid = $12;
4793 binat.proto = $6->proto;
4797 if ($8 != NULL && disallow_table($8, "invalid use of "
4798 "table <%s> as the source address of a binat rule"))
4800 if ($8 != NULL && disallow_alias($8, "invalid use of "
4801 "interface (%s) as the source address of a binat "
4804 if ($13 != NULL && $13->host != NULL && disallow_table(
4805 $13->host, "invalid use of table <%s> as the "
4806 "redirect address of a binat rule"))
4808 if ($13 != NULL && $13->host != NULL && disallow_alias(
4809 $13->host, "invalid use of interface (%s) as the "
4810 "redirect address of a binat rule"))
4815 yyerror("multiple binat ip addresses");
4818 if ($8->addr.type == PF_ADDR_DYNIFTL)
4820 if ($8->af != binat.af) {
4821 yyerror("binat ip versions must match");
4824 if (check_netmask($8, binat.af))
4826 memcpy(&binat.src.addr, &$8->addr,
4827 sizeof(binat.src.addr));
4832 yyerror("multiple binat ip addresses");
4835 if ($9->af != binat.af && $9->af) {
4836 yyerror("binat ip versions must match");
4839 if (check_netmask($9, binat.af))
4841 memcpy(&binat.dst.addr, &$9->addr,
4842 sizeof(binat.dst.addr));
4843 binat.dst.neg = $9->not;
4847 if (binat.action == PF_NOBINAT) {
4849 yyerror("'no binat' rule does not need"
4854 if ($13 == NULL || $13->host == NULL) {
4855 yyerror("'binat' rule requires"
4860 remove_invalid_hosts(&$13->host, &binat.af);
4861 if (invalid_redirect($13->host, binat.af))
4863 if ($13->host->next != NULL) {
4864 yyerror("binat rule must redirect to "
4865 "a single address");
4868 if (check_netmask($13->host, binat.af))
4871 if (!PF_AZERO(&binat.src.addr.v.a.mask,
4873 !PF_AEQ(&binat.src.addr.v.a.mask,
4874 &$13->host->addr.v.a.mask, binat.af)) {
4875 yyerror("'binat' source mask and "
4876 "redirect mask must be the same");
4880 TAILQ_INIT(&binat.rpool.list);
4881 pa = calloc(1, sizeof(struct pf_pooladdr));
4883 err(1, "binat: calloc");
4884 pa->addr = $13->host->addr;
4886 TAILQ_INSERT_TAIL(&binat.rpool.list,
4892 pfctl_append_rule(pf, &binat, "");
4896 tag : /* empty */ { $$ = NULL; }
4897 | TAG STRING { $$ = $2; }
4900 tagged : /* empty */ { $$.neg = 0; $$.name = NULL; }
4901 | not TAGGED string { $$.neg = $1; $$.name = $3; }
4904 rtable : /* empty */ { $$ = -1; }
4906 if ($2 < 0 || $2 > rt_tableid_max()) {
4907 yyerror("invalid rtable id");
4914 route_host : STRING {
4915 $$ = calloc(1, sizeof(struct node_host));
4917 err(1, "route_host: calloc");
4918 if (strlen($1) >= IFNAMSIZ) {
4919 yyerror("interface name too long");
4922 $$->ifname = strdup($1);
4923 set_ipmask($$, 128);
4927 | '(' STRING host ')' {
4928 struct node_host *n;
4931 for (n = $3; n != NULL; n = n->next) {
4932 if (strlen($2) >= IFNAMSIZ) {
4933 yyerror("interface name too long");
4936 n->ifname = strdup($2);
4941 route_host_list : route_host optnl { $$ = $1; }
4942 | route_host_list comma route_host optnl {
4945 if ($1->af != $3->af) {
4946 yyerror("all pool addresses must be in the "
4947 "same address family");
4950 $1->tail->next = $3;
4951 $1->tail = $3->tail;
4956 routespec : route_host { $$ = $1; }
4957 | '{' optnl route_host_list '}' { $$ = $3; }
4960 route : /* empty */ {
4966 /* backwards-compat */
4971 | ROUTETO routespec pool_opts {
4974 $$.pool_opts = $3.type | $3.opts;
4978 | REPLYTO routespec pool_opts {
4981 $$.pool_opts = $3.type | $3.opts;
4985 | DUPTO routespec pool_opts {
4988 $$.pool_opts = $3.type | $3.opts;
4994 timeout_spec : STRING NUMBER
4996 if (check_rulestate(PFCTL_STATE_OPTION)) {
5000 if ($2 < 0 || $2 > UINT_MAX) {
5001 yyerror("only positive values permitted");
5004 if (pfctl_set_timeout(pf, $1, $2, 0) != 0) {
5005 yyerror("unknown timeout %s", $1);
5012 if (check_rulestate(PFCTL_STATE_OPTION))
5014 if ($2 < 0 || $2 > UINT_MAX) {
5015 yyerror("only positive values permitted");
5018 if (pfctl_set_timeout(pf, "interval", $2, 0) != 0)
5023 timeout_list : timeout_list comma timeout_spec optnl
5024 | timeout_spec optnl
5027 limit_spec : STRING NUMBER
5029 if (check_rulestate(PFCTL_STATE_OPTION)) {
5033 if ($2 < 0 || $2 > UINT_MAX) {
5034 yyerror("only positive values permitted");
5037 if (pfctl_set_limit(pf, $1, $2) != 0) {
5038 yyerror("unable to set limit %s %u", $1, $2);
5046 limit_list : limit_list comma limit_spec optnl
5054 yesno : NO { $$ = 0; }
5056 if (!strcmp($1, "yes"))
5059 yyerror("invalid value '%s', expected 'yes' "
5068 unaryop : '=' { $$ = PF_OP_EQ; }
5069 | '!' '=' { $$ = PF_OP_NE; }
5070 | '<' '=' { $$ = PF_OP_LE; }
5071 | '<' { $$ = PF_OP_LT; }
5072 | '>' '=' { $$ = PF_OP_GE; }
5073 | '>' { $$ = PF_OP_GT; }
5079 yyerror(const char *fmt, ...)
5085 fprintf(stderr, "%s:%d: ", file->name, yylval.lineno);
5086 vfprintf(stderr, fmt, ap);
5087 fprintf(stderr, "\n");
5093 disallow_table(struct node_host *h, const char *fmt)
5095 for (; h != NULL; h = h->next)
5096 if (h->addr.type == PF_ADDR_TABLE) {
5097 yyerror(fmt, h->addr.v.tblname);
5104 disallow_urpf_failed(struct node_host *h, const char *fmt)
5106 for (; h != NULL; h = h->next)
5107 if (h->addr.type == PF_ADDR_URPFFAILED) {
5115 disallow_alias(struct node_host *h, const char *fmt)
5117 for (; h != NULL; h = h->next)
5118 if (DYNIF_MULTIADDR(h->addr)) {
5119 yyerror(fmt, h->addr.v.tblname);
5126 rule_consistent(struct pfctl_rule *r, int anchor_call)
5130 switch (r->action) {
5135 problems = filter_consistent(r, anchor_call);
5139 problems = nat_consistent(r);
5143 problems = rdr_consistent(r);
5154 filter_consistent(struct pfctl_rule *r, int anchor_call)
5158 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP &&
5159 (r->src.port_op || r->dst.port_op)) {
5160 yyerror("port only applies to tcp/udp");
5163 if (r->proto != IPPROTO_ICMP && r->proto != IPPROTO_ICMPV6 &&
5164 (r->type || r->code)) {
5165 yyerror("icmp-type/code only applies to icmp");
5168 if (!r->af && (r->type || r->code)) {
5169 yyerror("must indicate address family with icmp-type/code");
5172 if (r->overload_tblname[0] &&
5173 r->max_src_conn == 0 && r->max_src_conn_rate.seconds == 0) {
5174 yyerror("'overload' requires 'max-src-conn' "
5175 "or 'max-src-conn-rate'");
5178 if ((r->proto == IPPROTO_ICMP && r->af == AF_INET6) ||
5179 (r->proto == IPPROTO_ICMPV6 && r->af == AF_INET)) {
5180 yyerror("proto %s doesn't match address family %s",
5181 r->proto == IPPROTO_ICMP ? "icmp" : "icmp6",
5182 r->af == AF_INET ? "inet" : "inet6");
5185 if (r->allow_opts && r->action != PF_PASS) {
5186 yyerror("allow-opts can only be specified for pass rules");
5189 if (r->rule_flag & PFRULE_FRAGMENT && (r->src.port_op ||
5190 r->dst.port_op || r->flagset || r->type || r->code)) {
5191 yyerror("fragments can be filtered only on IP header fields");
5194 if (r->rule_flag & PFRULE_RETURNRST && r->proto != IPPROTO_TCP) {
5195 yyerror("return-rst can only be applied to TCP rules");
5198 if (r->max_src_nodes && !(r->rule_flag & PFRULE_RULESRCTRACK)) {
5199 yyerror("max-src-nodes requires 'source-track rule'");
5202 if (r->action == PF_DROP && r->keep_state) {
5203 yyerror("keep state on block rules doesn't make sense");
5206 if (r->rule_flag & PFRULE_STATESLOPPY &&
5207 (r->keep_state == PF_STATE_MODULATE ||
5208 r->keep_state == PF_STATE_SYNPROXY)) {
5209 yyerror("sloppy state matching cannot be used with "
5210 "synproxy state or modulate state");
5217 nat_consistent(struct pfctl_rule *r)
5219 return (0); /* yeah! */
5223 rdr_consistent(struct pfctl_rule *r)
5227 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP) {
5228 if (r->src.port_op) {
5229 yyerror("src port only applies to tcp/udp");
5232 if (r->dst.port_op) {
5233 yyerror("dst port only applies to tcp/udp");
5236 if (r->rpool.proxy_port[0]) {
5237 yyerror("rpool port only applies to tcp/udp");
5241 if (r->dst.port_op &&
5242 r->dst.port_op != PF_OP_EQ && r->dst.port_op != PF_OP_RRG) {
5243 yyerror("invalid port operator for rdr destination port");
5250 process_tabledef(char *name, struct table_opts *opts)
5252 struct pfr_buffer ab;
5253 struct node_tinit *ti;
5254 unsigned long maxcount;
5255 size_t s = sizeof(maxcount);
5257 bzero(&ab, sizeof(ab));
5258 ab.pfrb_type = PFRB_ADDRS;
5259 SIMPLEQ_FOREACH(ti, &opts->init_nodes, entries) {
5261 if (pfr_buf_load(&ab, ti->file, 0, append_addr)) {
5263 yyerror("cannot load \"%s\": %s",
5264 ti->file, strerror(errno));
5266 yyerror("file \"%s\" contains bad data",
5271 if (append_addr_host(&ab, ti->host, 0, 0)) {
5272 yyerror("cannot create address buffer: %s",
5277 if (pf->opts & PF_OPT_VERBOSE)
5278 print_tabledef(name, opts->flags, opts->init_addr,
5280 if (!(pf->opts & PF_OPT_NOACTION) &&
5281 pfctl_define_table(name, opts->flags, opts->init_addr,
5282 pf->anchor->name, &ab, pf->anchor->ruleset.tticket)) {
5284 if (sysctlbyname("net.pf.request_maxcount", &maxcount, &s,
5288 if (ab.pfrb_size > maxcount)
5289 yyerror("cannot define table %s: too many elements.\n"
5290 "Consider increasing net.pf.request_maxcount.",
5293 yyerror("cannot define table %s: %s", name,
5294 pfr_strerror(errno));
5311 /* macro gore, but you should've seen the prior indentation nightmare... */
5313 #define FREE_LIST(T,r) \
5316 while (node != NULL) { \
5318 node = node->next; \
5323 #define LOOP_THROUGH(T,n,r,C) \
5327 r = calloc(1, sizeof(T)); \
5329 err(1, "LOOP: calloc"); \
5333 while (n != NULL) { \
5342 expand_label_str(char *label, size_t len, const char *srch, const char *repl)
5347 if ((tmp = calloc(1, len)) == NULL)
5348 err(1, "expand_label_str: calloc");
5350 while ((q = strstr(p, srch)) != NULL) {
5352 if ((strlcat(tmp, p, len) >= len) ||
5353 (strlcat(tmp, repl, len) >= len))
5354 errx(1, "expand_label: label too long");
5358 if (strlcat(tmp, p, len) >= len)
5359 errx(1, "expand_label: label too long");
5360 strlcpy(label, tmp, len); /* always fits */
5365 expand_label_if(const char *name, char *label, size_t len, const char *ifname)
5367 if (strstr(label, name) != NULL) {
5369 expand_label_str(label, len, name, "any");
5371 expand_label_str(label, len, name, ifname);
5376 expand_label_addr(const char *name, char *label, size_t len, sa_family_t af,
5377 struct pf_rule_addr *addr)
5379 char tmp[64], tmp_not[66];
5381 if (strstr(label, name) != NULL) {
5382 switch (addr->addr.type) {
5383 case PF_ADDR_DYNIFTL:
5384 snprintf(tmp, sizeof(tmp), "(%s)", addr->addr.v.ifname);
5387 snprintf(tmp, sizeof(tmp), "<%s>", addr->addr.v.tblname);
5389 case PF_ADDR_NOROUTE:
5390 snprintf(tmp, sizeof(tmp), "no-route");
5392 case PF_ADDR_URPFFAILED:
5393 snprintf(tmp, sizeof(tmp), "urpf-failed");
5395 case PF_ADDR_ADDRMASK:
5396 if (!af || (PF_AZERO(&addr->addr.v.a.addr, af) &&
5397 PF_AZERO(&addr->addr.v.a.mask, af)))
5398 snprintf(tmp, sizeof(tmp), "any");
5403 if (inet_ntop(af, &addr->addr.v.a.addr, a,
5405 snprintf(tmp, sizeof(tmp), "?");
5407 bits = unmask(&addr->addr.v.a.mask, af);
5408 if ((af == AF_INET && bits < 32) ||
5409 (af == AF_INET6 && bits < 128))
5410 snprintf(tmp, sizeof(tmp),
5413 snprintf(tmp, sizeof(tmp),
5419 snprintf(tmp, sizeof(tmp), "?");
5424 snprintf(tmp_not, sizeof(tmp_not), "! %s", tmp);
5425 expand_label_str(label, len, name, tmp_not);
5427 expand_label_str(label, len, name, tmp);
5432 expand_label_port(const char *name, char *label, size_t len,
5433 struct pf_rule_addr *addr)
5435 char a1[6], a2[6], op[13] = "";
5437 if (strstr(label, name) != NULL) {
5438 snprintf(a1, sizeof(a1), "%u", ntohs(addr->port[0]));
5439 snprintf(a2, sizeof(a2), "%u", ntohs(addr->port[1]));
5442 else if (addr->port_op == PF_OP_IRG)
5443 snprintf(op, sizeof(op), "%s><%s", a1, a2);
5444 else if (addr->port_op == PF_OP_XRG)
5445 snprintf(op, sizeof(op), "%s<>%s", a1, a2);
5446 else if (addr->port_op == PF_OP_EQ)
5447 snprintf(op, sizeof(op), "%s", a1);
5448 else if (addr->port_op == PF_OP_NE)
5449 snprintf(op, sizeof(op), "!=%s", a1);
5450 else if (addr->port_op == PF_OP_LT)
5451 snprintf(op, sizeof(op), "<%s", a1);
5452 else if (addr->port_op == PF_OP_LE)
5453 snprintf(op, sizeof(op), "<=%s", a1);
5454 else if (addr->port_op == PF_OP_GT)
5455 snprintf(op, sizeof(op), ">%s", a1);
5456 else if (addr->port_op == PF_OP_GE)
5457 snprintf(op, sizeof(op), ">=%s", a1);
5458 expand_label_str(label, len, name, op);
5463 expand_label_proto(const char *name, char *label, size_t len, u_int8_t proto)
5465 const char *protoname;
5468 if (strstr(label, name) != NULL) {
5469 protoname = pfctl_proto2name(proto);
5470 if (protoname != NULL)
5471 expand_label_str(label, len, name, protoname);
5473 snprintf(n, sizeof(n), "%u", proto);
5474 expand_label_str(label, len, name, n);
5480 expand_label_nr(const char *name, char *label, size_t len,
5481 struct pfctl_rule *r)
5485 if (strstr(label, name) != NULL) {
5486 snprintf(n, sizeof(n), "%u", r->nr);
5487 expand_label_str(label, len, name, n);
5492 expand_label(char *label, size_t len, struct pfctl_rule *r)
5494 expand_label_if("$if", label, len, r->ifname);
5495 expand_label_addr("$srcaddr", label, len, r->af, &r->src);
5496 expand_label_addr("$dstaddr", label, len, r->af, &r->dst);
5497 expand_label_port("$srcport", label, len, &r->src);
5498 expand_label_port("$dstport", label, len, &r->dst);
5499 expand_label_proto("$proto", label, len, r->proto);
5500 expand_label_nr("$nr", label, len, r);
5504 expand_altq(struct pf_altq *a, struct node_if *interfaces,
5505 struct node_queue *nqueues, struct node_queue_bw bwspec,
5506 struct node_queue_opt *opts)
5508 struct pf_altq pa, pb;
5509 char qname[PF_QNAME_SIZE];
5510 struct node_queue *n;
5511 struct node_queue_bw bw;
5514 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
5515 FREE_LIST(struct node_if, interfaces);
5517 FREE_LIST(struct node_queue, nqueues);
5521 LOOP_THROUGH(struct node_if, interface, interfaces,
5522 memcpy(&pa, a, sizeof(struct pf_altq));
5523 if (strlcpy(pa.ifname, interface->ifname,
5524 sizeof(pa.ifname)) >= sizeof(pa.ifname))
5525 errx(1, "expand_altq: strlcpy");
5527 if (interface->not) {
5528 yyerror("altq on ! <interface> is not supported");
5531 if (eval_pfaltq(pf, &pa, &bwspec, opts))
5534 if (pfctl_add_altq(pf, &pa))
5537 if (pf->opts & PF_OPT_VERBOSE) {
5538 print_altq(&pf->paltq->altq, 0,
5540 if (nqueues && nqueues->tail) {
5542 LOOP_THROUGH(struct node_queue, queue,
5552 if (pa.scheduler == ALTQT_CBQ ||
5553 pa.scheduler == ALTQT_HFSC ||
5554 pa.scheduler == ALTQT_FAIRQ) {
5555 /* now create a root queue */
5556 memset(&pb, 0, sizeof(struct pf_altq));
5557 if (strlcpy(qname, "root_", sizeof(qname)) >=
5559 errx(1, "expand_altq: strlcpy");
5560 if (strlcat(qname, interface->ifname,
5561 sizeof(qname)) >= sizeof(qname))
5562 errx(1, "expand_altq: strlcat");
5563 if (strlcpy(pb.qname, qname,
5564 sizeof(pb.qname)) >= sizeof(pb.qname))
5565 errx(1, "expand_altq: strlcpy");
5566 if (strlcpy(pb.ifname, interface->ifname,
5567 sizeof(pb.ifname)) >= sizeof(pb.ifname))
5568 errx(1, "expand_altq: strlcpy");
5569 pb.qlimit = pa.qlimit;
5570 pb.scheduler = pa.scheduler;
5571 bw.bw_absolute = pa.ifbandwidth;
5573 if (eval_pfqueue(pf, &pb, &bw, opts))
5576 if (pfctl_add_altq(pf, &pb))
5580 LOOP_THROUGH(struct node_queue, queue, nqueues,
5581 n = calloc(1, sizeof(struct node_queue));
5583 err(1, "expand_altq: calloc");
5584 if (pa.scheduler == ALTQT_CBQ ||
5585 pa.scheduler == ALTQT_HFSC ||
5586 pa.scheduler == ALTQT_FAIRQ)
5587 if (strlcpy(n->parent, qname,
5588 sizeof(n->parent)) >=
5590 errx(1, "expand_altq: strlcpy");
5591 if (strlcpy(n->queue, queue->queue,
5592 sizeof(n->queue)) >= sizeof(n->queue))
5593 errx(1, "expand_altq: strlcpy");
5594 if (strlcpy(n->ifname, interface->ifname,
5595 sizeof(n->ifname)) >= sizeof(n->ifname))
5596 errx(1, "expand_altq: strlcpy");
5597 n->scheduler = pa.scheduler;
5603 queues->tail->next = n;
5609 FREE_LIST(struct node_if, interfaces);
5611 FREE_LIST(struct node_queue, nqueues);
5617 expand_queue(struct pf_altq *a, struct node_if *interfaces,
5618 struct node_queue *nqueues, struct node_queue_bw bwspec,
5619 struct node_queue_opt *opts)
5621 struct node_queue *n, *nq;
5626 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
5627 FREE_LIST(struct node_queue, nqueues);
5631 if (queues == NULL) {
5632 yyerror("queue %s has no parent", a->qname);
5633 FREE_LIST(struct node_queue, nqueues);
5637 LOOP_THROUGH(struct node_if, interface, interfaces,
5638 LOOP_THROUGH(struct node_queue, tqueue, queues,
5639 if (!strncmp(a->qname, tqueue->queue, PF_QNAME_SIZE) &&
5640 (interface->ifname[0] == 0 ||
5641 (!interface->not && !strncmp(interface->ifname,
5642 tqueue->ifname, IFNAMSIZ)) ||
5643 (interface->not && strncmp(interface->ifname,
5644 tqueue->ifname, IFNAMSIZ)))) {
5645 /* found ourself in queues */
5648 memcpy(&pa, a, sizeof(struct pf_altq));
5650 if (pa.scheduler != ALTQT_NONE &&
5651 pa.scheduler != tqueue->scheduler) {
5652 yyerror("exactly one scheduler type "
5653 "per interface allowed");
5656 pa.scheduler = tqueue->scheduler;
5658 /* scheduler dependent error checking */
5659 switch (pa.scheduler) {
5661 if (nqueues != NULL) {
5662 yyerror("priq queues cannot "
5663 "have child queues");
5666 if (bwspec.bw_absolute > 0 ||
5667 bwspec.bw_percent < 100) {
5668 yyerror("priq doesn't take "
5677 if (strlcpy(pa.ifname, tqueue->ifname,
5678 sizeof(pa.ifname)) >= sizeof(pa.ifname))
5679 errx(1, "expand_queue: strlcpy");
5680 if (strlcpy(pa.parent, tqueue->parent,
5681 sizeof(pa.parent)) >= sizeof(pa.parent))
5682 errx(1, "expand_queue: strlcpy");
5684 if (eval_pfqueue(pf, &pa, &bwspec, opts))
5687 if (pfctl_add_altq(pf, &pa))
5690 for (nq = nqueues; nq != NULL; nq = nq->next) {
5691 if (!strcmp(a->qname, nq->queue)) {
5692 yyerror("queue cannot have "
5698 sizeof(struct node_queue));
5700 err(1, "expand_queue: calloc");
5701 if (strlcpy(n->parent, a->qname,
5702 sizeof(n->parent)) >=
5704 errx(1, "expand_queue strlcpy");
5705 if (strlcpy(n->queue, nq->queue,
5706 sizeof(n->queue)) >=
5708 errx(1, "expand_queue strlcpy");
5709 if (strlcpy(n->ifname, tqueue->ifname,
5710 sizeof(n->ifname)) >=
5712 errx(1, "expand_queue strlcpy");
5713 n->scheduler = tqueue->scheduler;
5719 queues->tail->next = n;
5723 if ((pf->opts & PF_OPT_VERBOSE) && (
5724 (found == 1 && interface->ifname[0] == 0) ||
5725 (found > 0 && interface->ifname[0] != 0))) {
5726 print_queue(&pf->paltq->altq, 0,
5727 &bwspec, interface->ifname[0] != 0,
5729 if (nqueues && nqueues->tail) {
5731 LOOP_THROUGH(struct node_queue,
5744 FREE_LIST(struct node_queue, nqueues);
5745 FREE_LIST(struct node_if, interfaces);
5748 yyerror("queue %s has no parent", a->qname);
5759 pf_af_to_proto(sa_family_t af)
5762 return (ETHERTYPE_IP);
5764 return (ETHERTYPE_IPV6);
5770 expand_eth_rule(struct pfctl_eth_rule *r,
5771 struct node_if *interfaces, struct node_etherproto *protos,
5772 struct node_mac *srcs, struct node_mac *dsts,
5773 struct node_host *ipsrcs, struct node_host *ipdsts, const char *anchor_call)
5775 LOOP_THROUGH(struct node_if, interface, interfaces,
5776 LOOP_THROUGH(struct node_etherproto, proto, protos,
5777 LOOP_THROUGH(struct node_mac, src, srcs,
5778 LOOP_THROUGH(struct node_mac, dst, dsts,
5779 LOOP_THROUGH(struct node_host, ipsrc, ipsrcs,
5780 LOOP_THROUGH(struct node_host, ipdst, ipdsts,
5781 strlcpy(r->ifname, interface->ifname,
5783 r->ifnot = interface->not;
5784 r->proto = proto->proto;
5785 if (!r->proto && ipsrc->af)
5786 r->proto = pf_af_to_proto(ipsrc->af);
5787 else if (!r->proto && ipdst->af)
5788 r->proto = pf_af_to_proto(ipdst->af);
5789 bcopy(src->mac, r->src.addr, ETHER_ADDR_LEN);
5790 bcopy(src->mask, r->src.mask, ETHER_ADDR_LEN);
5791 r->src.neg = src->neg;
5792 r->src.isset = src->isset;
5793 r->ipsrc.addr = ipsrc->addr;
5794 r->ipsrc.neg = ipsrc->not;
5795 r->ipdst.addr = ipdst->addr;
5796 r->ipdst.neg = ipdst->not;
5797 bcopy(dst->mac, r->dst.addr, ETHER_ADDR_LEN);
5798 bcopy(dst->mask, r->dst.mask, ETHER_ADDR_LEN);
5799 r->dst.neg = dst->neg;
5800 r->dst.isset = dst->isset;
5801 r->nr = pf->eastack[pf->asd]->match++;
5803 pfctl_append_eth_rule(pf, r, anchor_call);
5806 FREE_LIST(struct node_if, interfaces);
5807 FREE_LIST(struct node_etherproto, protos);
5808 FREE_LIST(struct node_mac, srcs);
5809 FREE_LIST(struct node_mac, dsts);
5810 FREE_LIST(struct node_host, ipsrcs);
5811 FREE_LIST(struct node_host, ipdsts);
5815 expand_rule(struct pfctl_rule *r,
5816 struct node_if *interfaces, struct node_host *rpool_hosts,
5817 struct node_proto *protos, struct node_os *src_oses,
5818 struct node_host *src_hosts, struct node_port *src_ports,
5819 struct node_host *dst_hosts, struct node_port *dst_ports,
5820 struct node_uid *uids, struct node_gid *gids, struct node_icmp *icmp_types,
5821 const char *anchor_call)
5823 sa_family_t af = r->af;
5824 int added = 0, error = 0;
5825 char ifname[IF_NAMESIZE];
5826 char label[PF_RULE_MAX_LABEL_COUNT][PF_RULE_LABEL_SIZE];
5827 char tagname[PF_TAG_NAME_SIZE];
5828 char match_tagname[PF_TAG_NAME_SIZE];
5829 struct pf_pooladdr *pa;
5830 struct node_host *h;
5831 u_int8_t flags, flagset, keep_state;
5833 memcpy(label, r->label, sizeof(r->label));
5834 assert(sizeof(r->label) == sizeof(label));
5835 if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname))
5836 errx(1, "expand_rule: strlcpy");
5837 if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >=
5838 sizeof(match_tagname))
5839 errx(1, "expand_rule: strlcpy");
5841 flagset = r->flagset;
5842 keep_state = r->keep_state;
5844 LOOP_THROUGH(struct node_if, interface, interfaces,
5845 LOOP_THROUGH(struct node_proto, proto, protos,
5846 LOOP_THROUGH(struct node_icmp, icmp_type, icmp_types,
5847 LOOP_THROUGH(struct node_host, src_host, src_hosts,
5848 LOOP_THROUGH(struct node_port, src_port, src_ports,
5849 LOOP_THROUGH(struct node_os, src_os, src_oses,
5850 LOOP_THROUGH(struct node_host, dst_host, dst_hosts,
5851 LOOP_THROUGH(struct node_port, dst_port, dst_ports,
5852 LOOP_THROUGH(struct node_uid, uid, uids,
5853 LOOP_THROUGH(struct node_gid, gid, gids,
5856 /* for link-local IPv6 address, interface must match up */
5857 if ((r->af && src_host->af && r->af != src_host->af) ||
5858 (r->af && dst_host->af && r->af != dst_host->af) ||
5859 (src_host->af && dst_host->af &&
5860 src_host->af != dst_host->af) ||
5861 (src_host->ifindex && dst_host->ifindex &&
5862 src_host->ifindex != dst_host->ifindex) ||
5863 (src_host->ifindex && *interface->ifname &&
5864 src_host->ifindex != if_nametoindex(interface->ifname)) ||
5865 (dst_host->ifindex && *interface->ifname &&
5866 dst_host->ifindex != if_nametoindex(interface->ifname)))
5868 if (!r->af && src_host->af)
5869 r->af = src_host->af;
5870 else if (!r->af && dst_host->af)
5871 r->af = dst_host->af;
5873 if (*interface->ifname)
5874 strlcpy(r->ifname, interface->ifname,
5876 else if (if_indextoname(src_host->ifindex, ifname))
5877 strlcpy(r->ifname, ifname, sizeof(r->ifname));
5878 else if (if_indextoname(dst_host->ifindex, ifname))
5879 strlcpy(r->ifname, ifname, sizeof(r->ifname));
5881 memset(r->ifname, '\0', sizeof(r->ifname));
5883 memcpy(r->label, label, sizeof(r->label));
5884 if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >=
5886 errx(1, "expand_rule: strlcpy");
5887 if (strlcpy(r->match_tagname, match_tagname,
5888 sizeof(r->match_tagname)) >= sizeof(r->match_tagname))
5889 errx(1, "expand_rule: strlcpy");
5891 error += check_netmask(src_host, r->af);
5892 error += check_netmask(dst_host, r->af);
5894 r->ifnot = interface->not;
5895 r->proto = proto->proto;
5896 r->src.addr = src_host->addr;
5897 r->src.neg = src_host->not;
5898 r->src.port[0] = src_port->port[0];
5899 r->src.port[1] = src_port->port[1];
5900 r->src.port_op = src_port->op;
5901 r->dst.addr = dst_host->addr;
5902 r->dst.neg = dst_host->not;
5903 r->dst.port[0] = dst_port->port[0];
5904 r->dst.port[1] = dst_port->port[1];
5905 r->dst.port_op = dst_port->op;
5906 r->uid.op = uid->op;
5907 r->uid.uid[0] = uid->uid[0];
5908 r->uid.uid[1] = uid->uid[1];
5909 r->gid.op = gid->op;
5910 r->gid.gid[0] = gid->gid[0];
5911 r->gid.gid[1] = gid->gid[1];
5912 r->type = icmp_type->type;
5913 r->code = icmp_type->code;
5915 if ((keep_state == PF_STATE_MODULATE ||
5916 keep_state == PF_STATE_SYNPROXY) &&
5917 r->proto && r->proto != IPPROTO_TCP)
5918 r->keep_state = PF_STATE_NORMAL;
5920 r->keep_state = keep_state;
5922 if (r->proto && r->proto != IPPROTO_TCP) {
5927 r->flagset = flagset;
5929 if (icmp_type->proto && r->proto != icmp_type->proto) {
5930 yyerror("icmp-type mismatch");
5934 if (src_os && src_os->os) {
5935 r->os_fingerprint = pfctl_get_fingerprint(src_os->os);
5936 if ((pf->opts & PF_OPT_VERBOSE2) &&
5937 r->os_fingerprint == PF_OSFP_NOMATCH)
5939 "warning: unknown '%s' OS fingerprint\n",
5942 r->os_fingerprint = PF_OSFP_ANY;
5945 TAILQ_INIT(&r->rpool.list);
5946 for (h = rpool_hosts; h != NULL; h = h->next) {
5947 pa = calloc(1, sizeof(struct pf_pooladdr));
5949 err(1, "expand_rule: calloc");
5951 if (h->ifname != NULL) {
5952 if (strlcpy(pa->ifname, h->ifname,
5953 sizeof(pa->ifname)) >=
5955 errx(1, "expand_rule: strlcpy");
5958 TAILQ_INSERT_TAIL(&r->rpool.list, pa, entries);
5961 if (rule_consistent(r, anchor_call[0]) < 0 || error)
5962 yyerror("skipping rule due to errors");
5964 r->nr = pf->astack[pf->asd]->match++;
5965 pfctl_append_rule(pf, r, anchor_call);
5971 FREE_LIST(struct node_if, interfaces);
5972 FREE_LIST(struct node_proto, protos);
5973 FREE_LIST(struct node_host, src_hosts);
5974 FREE_LIST(struct node_port, src_ports);
5975 FREE_LIST(struct node_os, src_oses);
5976 FREE_LIST(struct node_host, dst_hosts);
5977 FREE_LIST(struct node_port, dst_ports);
5978 FREE_LIST(struct node_uid, uids);
5979 FREE_LIST(struct node_gid, gids);
5980 FREE_LIST(struct node_icmp, icmp_types);
5981 FREE_LIST(struct node_host, rpool_hosts);
5984 yyerror("rule expands to no valid combination");
5988 expand_skip_interface(struct node_if *interfaces)
5992 if (!interfaces || (!interfaces->next && !interfaces->not &&
5993 !strcmp(interfaces->ifname, "none"))) {
5994 if (pf->opts & PF_OPT_VERBOSE)
5995 printf("set skip on none\n");
5996 errs = pfctl_set_interface_flags(pf, "", PFI_IFLAG_SKIP, 0);
6000 if (pf->opts & PF_OPT_VERBOSE)
6001 printf("set skip on {");
6002 LOOP_THROUGH(struct node_if, interface, interfaces,
6003 if (pf->opts & PF_OPT_VERBOSE)
6004 printf(" %s", interface->ifname);
6005 if (interface->not) {
6006 yyerror("skip on ! <interface> is not supported");
6009 errs += pfctl_set_interface_flags(pf,
6010 interface->ifname, PFI_IFLAG_SKIP, 1);
6012 if (pf->opts & PF_OPT_VERBOSE)
6015 FREE_LIST(struct node_if, interfaces);
6027 check_rulestate(int desired_state)
6029 if (require_order && (rulestate > desired_state)) {
6030 yyerror("Rules must be in order: options, ethernet, "
6031 "normalization, queueing, translation, filtering");
6034 rulestate = desired_state;
6039 kw_cmp(const void *k, const void *e)
6041 return (strcmp(k, ((const struct keywords *)e)->k_name));
6047 /* this has to be sorted always */
6048 static const struct keywords keywords[] = {
6050 { "allow-opts", ALLOWOPTS},
6052 { "anchor", ANCHOR},
6053 { "antispoof", ANTISPOOF},
6055 { "bandwidth", BANDWIDTH},
6057 { "binat-anchor", BINATANCHOR},
6058 { "bitmask", BITMASK},
6060 { "block-policy", BLOCKPOLICY},
6061 { "buckets", BUCKETS},
6065 { "crop", FRAGCROP},
6067 { "divert-reply", DIVERTREPLY},
6068 { "divert-to", DIVERTTO},
6069 { "dnpipe", DNPIPE},
6070 { "dnqueue", DNQUEUE},
6072 { "drop-ovl", FRAGDROP},
6075 { "fail-policy", FAILPOLICY},
6077 { "fastroute", FASTROUTE},
6078 { "file", FILENAME},
6079 { "fingerprints", FINGERPRINTS},
6081 { "floating", FLOATING},
6084 { "fragment", FRAGMENT},
6086 { "global", GLOBAL},
6090 { "hostid", HOSTID},
6091 { "icmp-type", ICMPTYPE},
6092 { "icmp6-type", ICMP6TYPE},
6093 { "if-bound", IFBOUND},
6095 { "include", INCLUDE},
6098 { "interval", INTERVAL},
6100 { "keepcounters", KEEPCOUNTERS},
6104 { "linkshare", LINKSHARE},
6107 { "loginterface", LOGINTERFACE},
6108 { "map-e-portset", MAPEPORTSET},
6111 { "max-mss", MAXMSS},
6112 { "max-src-conn", MAXSRCCONN},
6113 { "max-src-conn-rate", MAXSRCCONNRATE},
6114 { "max-src-nodes", MAXSRCNODES},
6115 { "max-src-states", MAXSRCSTATES},
6116 { "min-ttl", MINTTL},
6117 { "modulate", MODULATE},
6119 { "nat-anchor", NATANCHOR},
6122 { "no-route", NOROUTE},
6123 { "no-sync", NOSYNC},
6125 { "optimization", OPTIMIZATION},
6128 { "overload", OVERLOAD},
6132 { "priority", PRIORITY},
6134 { "probability", PROBABILITY},
6136 { "qlimit", QLIMIT},
6139 { "random", RANDOM},
6140 { "random-id", RANDOMID},
6142 { "rdr-anchor", RDRANCHOR},
6143 { "realtime", REALTIME},
6144 { "reassemble", REASSEMBLE},
6145 { "reply-to", REPLYTO},
6146 { "require-order", REQUIREORDER},
6147 { "return", RETURN},
6148 { "return-icmp", RETURNICMP},
6149 { "return-icmp6", RETURNICMP6},
6150 { "return-rst", RETURNRST},
6151 { "ridentifier", RIDENTIFIER},
6152 { "round-robin", ROUNDROBIN},
6154 { "route-to", ROUTETO},
6155 { "rtable", RTABLE},
6157 { "ruleset-optimization", RULESET_OPTIMIZATION},
6160 { "set-tos", SETTOS},
6162 { "sloppy", SLOPPY},
6163 { "source-hash", SOURCEHASH},
6164 { "source-track", SOURCETRACK},
6166 { "state-defaults", STATEDEFAULTS},
6167 { "state-policy", STATEPOLICY},
6168 { "static-port", STATICPORT},
6169 { "sticky-address", STICKYADDRESS},
6170 { "syncookies", SYNCOOKIES},
6171 { "synproxy", SYNPROXY},
6174 { "tagged", TAGGED},
6175 { "target", TARGET},
6176 { "tbrsize", TBRSIZE},
6177 { "timeout", TIMEOUT},
6181 { "upperlimit", UPPERLIMIT},
6182 { "urpf-failed", URPFFAILED},
6185 const struct keywords *p;
6187 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
6188 sizeof(keywords[0]), kw_cmp);
6192 fprintf(stderr, "%s: %d\n", s, p->k_val);
6196 fprintf(stderr, "string: %s\n", s);
6201 #define MAXPUSHBACK 128
6203 static char *parsebuf;
6204 static int parseindex;
6205 static char pushback_buffer[MAXPUSHBACK];
6206 static int pushback_index = 0;
6214 /* Read character from the parsebuffer instead of input. */
6215 if (parseindex >= 0) {
6216 c = parsebuf[parseindex++];
6225 return (pushback_buffer[--pushback_index]);
6228 if ((c = getc(file->stream)) == EOF) {
6229 yyerror("reached end of file while parsing quoted string");
6230 if (popfile() == EOF)
6237 while ((c = getc(file->stream)) == '\\') {
6238 next = getc(file->stream);
6243 yylval.lineno = file->lineno;
6248 if (popfile() == EOF)
6250 c = getc(file->stream);
6262 if (parseindex >= 0)
6265 if (pushback_index < MAXPUSHBACK-1)
6266 return (pushback_buffer[pushback_index++] = c);
6278 /* skip to either EOF or the first real EOL */
6281 c = pushback_buffer[--pushback_index];
6299 int quotec, next, c;
6304 while ((c = lgetc(0)) == ' ' || c == '\t')
6307 yylval.lineno = file->lineno;
6309 while ((c = lgetc(0)) != '\n' && c != EOF)
6311 if (c == '$' && parsebuf == NULL) {
6313 if ((c = lgetc(0)) == EOF)
6316 if (p + 1 >= buf + sizeof(buf) - 1) {
6317 yyerror("string too long");
6320 if (isalnum(c) || c == '_') {
6330 yyerror("macro '%s' not defined", buf);
6343 if ((c = lgetc(quotec)) == EOF)
6348 } else if (c == '\\') {
6349 if ((next = lgetc(quotec)) == EOF)
6351 if (next == quotec || c == ' ' || c == '\t')
6353 else if (next == '\n') {
6359 } else if (c == quotec) {
6363 if (p + 1 >= buf + sizeof(buf) - 1) {
6364 yyerror("string too long");
6369 yylval.v.string = strdup(buf);
6370 if (yylval.v.string == NULL)
6371 err(1, "yylex: strdup");
6376 yylval.v.i = PF_OP_XRG;
6377 return (PORTBINARY);
6384 yylval.v.i = PF_OP_IRG;
6385 return (PORTBINARY);
6397 #define allowed_to_end_number(x) \
6398 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
6400 if (c == '-' || isdigit(c)) {
6403 if ((unsigned)(p-buf) >= sizeof(buf)) {
6404 yyerror("string too long");
6407 } while ((c = lgetc(0)) != EOF && isdigit(c));
6409 if (p == buf + 1 && buf[0] == '-')
6411 if (c == EOF || allowed_to_end_number(c)) {
6412 const char *errstr = NULL;
6415 yylval.v.number = strtonum(buf, LLONG_MIN,
6416 LLONG_MAX, &errstr);
6418 yyerror("\"%s\" invalid number: %s",
6433 #define allowed_in_string(x) \
6434 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
6435 x != '{' && x != '}' && x != '<' && x != '>' && \
6436 x != '!' && x != '=' && x != '/' && x != '#' && \
6439 if (isalnum(c) || c == ':' || c == '_') {
6442 if ((unsigned)(p-buf) >= sizeof(buf)) {
6443 yyerror("string too long");
6446 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
6449 if ((token = lookup(buf)) == STRING)
6450 if ((yylval.v.string = strdup(buf)) == NULL)
6451 err(1, "yylex: strdup");
6455 yylval.lineno = file->lineno;
6464 check_file_secrecy(int fd, const char *fname)
6468 if (fstat(fd, &st)) {
6469 warn("cannot stat %s", fname);
6472 if (st.st_uid != 0 && st.st_uid != getuid()) {
6473 warnx("%s: owner not root or current user", fname);
6476 if (st.st_mode & (S_IRWXG | S_IRWXO)) {
6477 warnx("%s: group/world readable/writeable", fname);
6484 pushfile(const char *name, int secret)
6488 if ((nfile = calloc(1, sizeof(struct file))) == NULL ||
6489 (nfile->name = strdup(name)) == NULL) {
6493 if (TAILQ_FIRST(&files) == NULL && strcmp(nfile->name, "-") == 0) {
6494 nfile->stream = stdin;
6496 if ((nfile->name = strdup("stdin")) == NULL) {
6501 } else if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
6502 warn("%s", nfile->name);
6506 } else if (secret &&
6507 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
6508 fclose(nfile->stream);
6514 TAILQ_INSERT_TAIL(&files, nfile, entry);
6523 if ((prev = TAILQ_PREV(file, files, entry)) != NULL) {
6524 prev->errors += file->errors;
6525 TAILQ_REMOVE(&files, file, entry);
6526 fclose(file->stream);
6536 parse_config(char *filename, struct pfctl *xpf)
6543 rulestate = PFCTL_STATE_NONE;
6544 returnicmpdefault = (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
6545 returnicmp6default =
6546 (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
6547 blockpolicy = PFRULE_DROP;
6548 failpolicy = PFRULE_DROP;
6551 if ((file = pushfile(filename, 0)) == NULL) {
6552 warn("cannot open the main config file!");
6557 errors = file->errors;
6560 /* Free macros and check which have not been used. */
6561 while ((sym = TAILQ_FIRST(&symhead))) {
6562 if ((pf->opts & PF_OPT_VERBOSE2) && !sym->used)
6563 fprintf(stderr, "warning: macro '%s' not "
6564 "used\n", sym->nam);
6567 TAILQ_REMOVE(&symhead, sym, entry);
6571 return (errors ? -1 : 0);
6575 symset(const char *nam, const char *val, int persist)
6579 for (sym = TAILQ_FIRST(&symhead); sym && strcmp(nam, sym->nam);
6580 sym = TAILQ_NEXT(sym, entry))
6584 if (sym->persist == 1)
6589 TAILQ_REMOVE(&symhead, sym, entry);
6593 if ((sym = calloc(1, sizeof(*sym))) == NULL)
6596 sym->nam = strdup(nam);
6597 if (sym->nam == NULL) {
6601 sym->val = strdup(val);
6602 if (sym->val == NULL) {
6608 sym->persist = persist;
6609 TAILQ_INSERT_TAIL(&symhead, sym, entry);
6614 pfctl_cmdline_symset(char *s)
6619 if ((val = strrchr(s, '=')) == NULL)
6622 if ((sym = malloc(strlen(s) - strlen(val) + 1)) == NULL)
6623 err(1, "pfctl_cmdline_symset: malloc");
6625 strlcpy(sym, s, strlen(s) - strlen(val) + 1);
6627 ret = symset(sym, val + 1, 1);
6634 symget(const char *nam)
6638 TAILQ_FOREACH(sym, &symhead, entry)
6639 if (strcmp(nam, sym->nam) == 0) {
6647 mv_rules(struct pfctl_ruleset *src, struct pfctl_ruleset *dst)
6650 struct pfctl_rule *r;
6652 for (i = 0; i < PF_RULESET_MAX; ++i) {
6653 while ((r = TAILQ_FIRST(src->rules[i].active.ptr))
6655 TAILQ_REMOVE(src->rules[i].active.ptr, r, entries);
6656 TAILQ_INSERT_TAIL(dst->rules[i].active.ptr, r, entries);
6657 dst->anchor->match++;
6659 src->anchor->match = 0;
6660 while ((r = TAILQ_FIRST(src->rules[i].inactive.ptr))
6662 TAILQ_REMOVE(src->rules[i].inactive.ptr, r, entries);
6663 TAILQ_INSERT_TAIL(dst->rules[i].inactive.ptr,
6670 mv_eth_rules(struct pfctl_eth_ruleset *src, struct pfctl_eth_ruleset *dst)
6672 struct pfctl_eth_rule *r;
6674 while ((r = TAILQ_FIRST(&src->rules)) != NULL) {
6675 TAILQ_REMOVE(&src->rules, r, entries);
6676 TAILQ_INSERT_TAIL(&dst->rules, r, entries);
6677 dst->anchor->match++;
6679 src->anchor->match = 0;
6683 decide_address_family(struct node_host *n, sa_family_t *af)
6685 if (*af != 0 || n == NULL)
6688 while ((n = n->next) != NULL) {
6697 remove_invalid_hosts(struct node_host **nh, sa_family_t *af)
6699 struct node_host *n = *nh, *prev = NULL;
6702 if (*af && n->af && n->af != *af) {
6703 /* unlink and free n */
6704 struct node_host *next = n->next;
6706 /* adjust tail pointer */
6707 if (n == (*nh)->tail)
6709 /* adjust previous node's next pointer */
6715 if (n->ifname != NULL)
6729 invalid_redirect(struct node_host *nh, sa_family_t af)
6732 struct node_host *n;
6734 /* tables and dyniftl are ok without an address family */
6735 for (n = nh; n != NULL; n = n->next) {
6736 if (n->addr.type != PF_ADDR_TABLE &&
6737 n->addr.type != PF_ADDR_DYNIFTL) {
6738 yyerror("address family not given and "
6739 "translation address expands to multiple "
6740 "address families");
6746 yyerror("no translation address with matching address family "
6754 atoul(char *s, u_long *ulvalp)
6760 ulval = strtoul(s, &ep, 0);
6761 if (s[0] == '\0' || *ep != '\0')
6763 if (errno == ERANGE && ulval == ULONG_MAX)
6775 if (atoul(n, &ulval) == 0) {
6776 if (ulval > 65535) {
6777 yyerror("illegal port value %lu", ulval);
6780 return (htons(ulval));
6782 s = getservbyname(n, "tcp");
6784 s = getservbyname(n, "udp");
6786 yyerror("unknown port %s", n);
6794 rule_label(struct pfctl_rule *r, char *s[PF_RULE_MAX_LABEL_COUNT])
6796 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++) {
6800 if (strlcpy(r->label[i], s[i], sizeof(r->label[0])) >=
6801 sizeof(r->label[0])) {
6802 yyerror("rule label too long (max %d chars)",
6803 sizeof(r->label[0])-1);
6811 parseicmpspec(char *w, sa_family_t af)
6813 const struct icmpcodeent *p;
6818 icmptype = returnicmpdefault >> 8;
6820 icmptype = returnicmp6default >> 8;
6822 if (atoul(w, &ulval) == -1) {
6823 if ((p = geticmpcodebyname(icmptype, w, af)) == NULL) {
6824 yyerror("unknown icmp code %s", w);
6830 yyerror("invalid icmp code %lu", ulval);
6833 return (icmptype << 8 | ulval);
6837 parseport(char *port, struct range *r, int extensions)
6839 char *p = strchr(port, ':');
6842 if ((r->a = getservice(port)) == -1)
6848 if ((extensions & PPORT_STAR) && !strcmp(p+1, "*")) {
6850 if ((r->a = getservice(port)) == -1)
6856 if ((extensions & PPORT_RANGE)) {
6858 if ((r->a = getservice(port)) == -1 ||
6859 (r->b = getservice(p)) == -1)
6872 pfctl_load_anchors(int dev, struct pfctl *pf, struct pfr_buffer *trans)
6874 struct loadanchors *la;
6876 TAILQ_FOREACH(la, &loadanchorshead, entries) {
6877 if (pf->opts & PF_OPT_VERBOSE)
6878 fprintf(stderr, "\nLoading anchor %s from %s\n",
6879 la->anchorname, la->filename);
6880 if (pfctl_rules(dev, la->filename, pf->opts, pf->optimize,
6881 la->anchorname, trans) == -1)
6889 kw_casecmp(const void *k, const void *e)
6891 return (strcasecmp(k, ((const struct keywords *)e)->k_name));
6895 map_tos(char *s, int *val)
6897 /* DiffServ Codepoints and other TOS mappings */
6898 const struct keywords toswords[] = {
6899 { "af11", IPTOS_DSCP_AF11 },
6900 { "af12", IPTOS_DSCP_AF12 },
6901 { "af13", IPTOS_DSCP_AF13 },
6902 { "af21", IPTOS_DSCP_AF21 },
6903 { "af22", IPTOS_DSCP_AF22 },
6904 { "af23", IPTOS_DSCP_AF23 },
6905 { "af31", IPTOS_DSCP_AF31 },
6906 { "af32", IPTOS_DSCP_AF32 },
6907 { "af33", IPTOS_DSCP_AF33 },
6908 { "af41", IPTOS_DSCP_AF41 },
6909 { "af42", IPTOS_DSCP_AF42 },
6910 { "af43", IPTOS_DSCP_AF43 },
6911 { "critical", IPTOS_PREC_CRITIC_ECP },
6912 { "cs0", IPTOS_DSCP_CS0 },
6913 { "cs1", IPTOS_DSCP_CS1 },
6914 { "cs2", IPTOS_DSCP_CS2 },
6915 { "cs3", IPTOS_DSCP_CS3 },
6916 { "cs4", IPTOS_DSCP_CS4 },
6917 { "cs5", IPTOS_DSCP_CS5 },
6918 { "cs6", IPTOS_DSCP_CS6 },
6919 { "cs7", IPTOS_DSCP_CS7 },
6920 { "ef", IPTOS_DSCP_EF },
6921 { "inetcontrol", IPTOS_PREC_INTERNETCONTROL },
6922 { "lowdelay", IPTOS_LOWDELAY },
6923 { "netcontrol", IPTOS_PREC_NETCONTROL },
6924 { "reliability", IPTOS_RELIABILITY },
6925 { "throughput", IPTOS_THROUGHPUT },
6926 { "va", IPTOS_DSCP_VA }
6928 const struct keywords *p;
6930 p = bsearch(s, toswords, sizeof(toswords)/sizeof(toswords[0]),
6931 sizeof(toswords[0]), kw_casecmp);
6941 rt_tableid_max(void)
6945 size_t l = sizeof(fibs);
6947 if (sysctlbyname("net.fibs", &fibs, &l, NULL, 0) == -1)
6948 fibs = 16; /* XXX RT_MAXFIBS, at least limit it some. */
6950 * As the OpenBSD code only compares > and not >= we need to adjust
6951 * here given we only accept values of 0..n and want to avoid #ifdefs
6956 return (RT_TABLEID_MAX);
6961 node_mac_from_string(const char *str)
6965 m = calloc(1, sizeof(struct node_mac));
6967 err(1, "mac: calloc");
6969 if (sscanf(str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
6970 &m->mac[0], &m->mac[1], &m->mac[2], &m->mac[3], &m->mac[4],
6973 yyerror("invalid MAC address");
6977 memset(m->mask, 0xff, ETHER_ADDR_LEN);
6986 node_mac_from_string_masklen(const char *str, int masklen)
6990 if (masklen < 0 || masklen > (ETHER_ADDR_LEN * 8)) {
6991 yyerror("invalid MAC mask length");
6995 m = node_mac_from_string(str);
6999 memset(m->mask, 0, ETHER_ADDR_LEN);
7000 for (int i = 0; i < masklen; i++)
7001 m->mask[i / 8] |= 1 << (i % 8);
7007 node_mac_from_string_mask(const char *str, const char *mask)
7011 m = node_mac_from_string(str);
7015 if (sscanf(mask, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
7016 &m->mask[0], &m->mask[1], &m->mask[2], &m->mask[3], &m->mask[4],
7017 &m->mask[5]) != 6) {
7019 yyerror("invalid MAC mask");