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));
1201 if (strlcpy(r.match_tagname, $9.match_tag,
1202 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1203 yyerror("tag too long, max %u chars",
1204 PF_TAG_NAME_SIZE - 1);
1207 r.match_tag_not = $9.match_tag_not;
1208 if ($9.queues.qname != NULL)
1209 memcpy(&r.qname, $9.queues.qname, sizeof(r.qname));
1210 r.dnpipe = $9.dnpipe;
1211 r.dnflags = $9.free_flags;
1213 expand_eth_rule(&r, $5, $6, $7.src, $7.dst,
1214 $8.src.host, $8.dst.host, "");
1218 etherpfa_anchorlist : /* empty */
1219 | etherpfa_anchorlist '\n'
1220 | etherpfa_anchorlist etherrule '\n'
1221 | etherpfa_anchorlist etheranchorrule '\n'
1224 etherpfa_anchor : '{'
1226 char ta[PF_ANCHOR_NAME_SIZE];
1227 struct pfctl_eth_ruleset *rs;
1229 /* steping into a brace anchor */
1233 /* create a holding ruleset in the root */
1234 snprintf(ta, PF_ANCHOR_NAME_SIZE, "_%d", pf->bn);
1235 rs = pf_find_or_create_eth_ruleset(ta);
1237 err(1, "etherpfa_anchor: pf_find_or_create_eth_ruleset");
1238 pf->eastack[pf->asd] = rs->anchor;
1239 pf->eanchor = rs->anchor;
1240 } '\n' etherpfa_anchorlist '}'
1242 pf->ealast = pf->eanchor;
1244 pf->eanchor = pf->eastack[pf->asd];
1249 etheranchorrule : ETHER ANCHOR anchorname dir quick interface etherproto etherfromto l3fromto etherpfa_anchor
1251 struct pfctl_eth_rule r;
1253 if (check_rulestate(PFCTL_STATE_ETHER)) {
1258 if ($3 && ($3[0] == '_' || strstr($3, "/_") != NULL)) {
1260 yyerror("anchor names beginning with '_' "
1261 "are reserved for internal use");
1265 memset(&r, 0, sizeof(r));
1266 if (pf->eastack[pf->asd + 1]) {
1267 /* move inline rules into relative location */
1268 pfctl_eth_anchor_setup(pf, &r,
1269 &pf->eastack[pf->asd]->ruleset,
1270 $3 ? $3 : pf->ealast->name);
1271 if (r.anchor == NULL)
1272 err(1, "etheranchorrule: unable to "
1275 if (pf->ealast != r.anchor) {
1276 if (r.anchor->match) {
1277 yyerror("inline anchor '%s' "
1282 mv_eth_rules(&pf->ealast->ruleset,
1283 &r.anchor->ruleset);
1285 pf_remove_if_empty_eth_ruleset(&pf->ealast->ruleset);
1286 pf->ealast = r.anchor;
1289 yyerror("anchors without explicit "
1290 "rules must specify a name");
1298 expand_eth_rule(&r, $6, $7, $8.src, $8.dst,
1299 $9.src.host, $9.dst.host,
1300 pf->eastack[pf->asd + 1] ? pf->ealast->name : $3);
1303 pf->eastack[pf->asd + 1] = NULL;
1307 etherfilter_opts : {
1308 bzero(&filter_opts, sizeof filter_opts);
1311 { $$ = filter_opts; }
1313 bzero(&filter_opts, sizeof filter_opts);
1318 etherfilter_opts_l : etherfilter_opts_l etherfilter_opt
1321 etherfilter_opt : etherqname {
1322 if (filter_opts.queues.qname) {
1323 yyerror("queue cannot be redefined");
1326 filter_opts.queues = $1;
1329 filter_opts.tag = $2;
1331 | not TAGGED string {
1332 filter_opts.match_tag = $3;
1333 filter_opts.match_tag_not = $1;
1336 filter_opts.dnpipe = $2;
1337 filter_opts.free_flags |= PFRULE_DN_IS_PIPE;
1340 filter_opts.dnpipe = $2;
1341 filter_opts.free_flags |= PFRULE_DN_IS_QUEUE;
1345 scrubrule : scrubaction dir logquick interface af proto fromto scrub_opts
1347 struct pfctl_rule r;
1349 if (check_rulestate(PFCTL_STATE_SCRUB))
1352 memset(&r, 0, sizeof(r));
1360 yyerror("scrub rules do not support 'quick'");
1366 r.rule_flag |= PFRULE_NODF;
1368 r.rule_flag |= PFRULE_RANDOMID;
1369 if ($8.reassemble_tcp) {
1370 if (r.direction != PF_INOUT) {
1371 yyerror("reassemble tcp rules can not "
1372 "specify direction");
1375 r.rule_flag |= PFRULE_REASSEMBLE_TCP;
1378 r.min_ttl = $8.minttl;
1380 r.max_mss = $8.maxmss;
1381 if ($8.marker & SOM_SETTOS) {
1382 r.rule_flag |= PFRULE_SET_TOS;
1383 r.set_tos = $8.settos;
1386 r.rule_flag |= $8.fragcache;
1388 if (strlcpy(r.match_tagname, $8.match_tag,
1389 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1390 yyerror("tag too long, max %u chars",
1391 PF_TAG_NAME_SIZE - 1);
1394 r.match_tag_not = $8.match_tag_not;
1395 r.rtableid = $8.rtableid;
1397 expand_rule(&r, $4, NULL, $6, $7.src_os,
1398 $7.src.host, $7.src.port, $7.dst.host, $7.dst.port,
1399 NULL, NULL, NULL, "");
1404 bzero(&scrub_opts, sizeof scrub_opts);
1405 scrub_opts.rtableid = -1;
1408 { $$ = scrub_opts; }
1410 bzero(&scrub_opts, sizeof scrub_opts);
1411 scrub_opts.rtableid = -1;
1416 scrub_opts_l : scrub_opts_l scrub_opt
1421 if (scrub_opts.nodf) {
1422 yyerror("no-df cannot be respecified");
1425 scrub_opts.nodf = 1;
1428 if (scrub_opts.marker & SOM_MINTTL) {
1429 yyerror("min-ttl cannot be respecified");
1432 if ($2 < 0 || $2 > 255) {
1433 yyerror("illegal min-ttl value %d", $2);
1436 scrub_opts.marker |= SOM_MINTTL;
1437 scrub_opts.minttl = $2;
1440 if (scrub_opts.marker & SOM_MAXMSS) {
1441 yyerror("max-mss cannot be respecified");
1444 if ($2 < 0 || $2 > 65535) {
1445 yyerror("illegal max-mss value %d", $2);
1448 scrub_opts.marker |= SOM_MAXMSS;
1449 scrub_opts.maxmss = $2;
1452 if (scrub_opts.marker & SOM_SETTOS) {
1453 yyerror("set-tos cannot be respecified");
1456 scrub_opts.marker |= SOM_SETTOS;
1457 scrub_opts.settos = $2;
1460 if (scrub_opts.marker & SOM_FRAGCACHE) {
1461 yyerror("fragcache cannot be respecified");
1464 scrub_opts.marker |= SOM_FRAGCACHE;
1465 scrub_opts.fragcache = $1;
1467 | REASSEMBLE STRING {
1468 if (strcasecmp($2, "tcp") != 0) {
1469 yyerror("scrub reassemble supports only tcp, "
1475 if (scrub_opts.reassemble_tcp) {
1476 yyerror("reassemble tcp cannot be respecified");
1479 scrub_opts.reassemble_tcp = 1;
1482 if (scrub_opts.randomid) {
1483 yyerror("random-id cannot be respecified");
1486 scrub_opts.randomid = 1;
1489 if ($2 < 0 || $2 > rt_tableid_max()) {
1490 yyerror("invalid rtable id");
1493 scrub_opts.rtableid = $2;
1495 | not TAGGED string {
1496 scrub_opts.match_tag = $3;
1497 scrub_opts.match_tag_not = $1;
1501 fragcache : FRAGMENT REASSEMBLE { $$ = 0; /* default */ }
1502 | FRAGMENT FRAGCROP { $$ = 0; }
1503 | FRAGMENT FRAGDROP { $$ = 0; }
1506 antispoof : ANTISPOOF logquick antispoof_ifspc af antispoof_opts {
1507 struct pfctl_rule r;
1508 struct node_host *h = NULL, *hh;
1509 struct node_if *i, *j;
1511 if (check_rulestate(PFCTL_STATE_FILTER))
1514 for (i = $3; i; i = i->next) {
1515 bzero(&r, sizeof(r));
1518 r.direction = PF_IN;
1523 r.ridentifier = $5.ridentifier;
1524 if (rule_label(&r, $5.label))
1526 r.rtableid = $5.rtableid;
1527 j = calloc(1, sizeof(struct node_if));
1529 err(1, "antispoof: calloc");
1530 if (strlcpy(j->ifname, i->ifname,
1531 sizeof(j->ifname)) >= sizeof(j->ifname)) {
1533 yyerror("interface name too long");
1538 h = calloc(1, sizeof(*h));
1540 err(1, "address: calloc");
1541 h->addr.type = PF_ADDR_DYNIFTL;
1543 if (strlcpy(h->addr.v.ifname, i->ifname,
1544 sizeof(h->addr.v.ifname)) >=
1545 sizeof(h->addr.v.ifname)) {
1548 "interface name too long");
1551 hh = malloc(sizeof(*hh));
1553 err(1, "address: malloc");
1554 bcopy(h, hh, sizeof(*hh));
1555 h->addr.iflags = PFI_AFLAG_NETWORK;
1557 h = ifa_lookup(j->ifname,
1563 expand_rule(&r, j, NULL, NULL, NULL, h,
1564 NULL, NULL, NULL, NULL, NULL,
1567 if ((i->ifa_flags & IFF_LOOPBACK) == 0) {
1568 bzero(&r, sizeof(r));
1571 r.direction = PF_IN;
1576 r.ridentifier = $5.ridentifier;
1577 if (rule_label(&r, $5.label))
1579 r.rtableid = $5.rtableid;
1583 h = ifa_lookup(i->ifname, 0);
1585 expand_rule(&r, NULL, NULL,
1586 NULL, NULL, h, NULL, NULL,
1587 NULL, NULL, NULL, NULL, "");
1591 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++)
1596 antispoof_ifspc : FOR antispoof_if { $$ = $2; }
1597 | FOR '{' optnl antispoof_iflst '}' { $$ = $4; }
1600 antispoof_iflst : antispoof_if optnl { $$ = $1; }
1601 | antispoof_iflst comma antispoof_if optnl {
1602 $1->tail->next = $3;
1608 antispoof_if : if_item { $$ = $1; }
1616 bzero(&antispoof_opts, sizeof antispoof_opts);
1617 antispoof_opts.rtableid = -1;
1620 { $$ = antispoof_opts; }
1622 bzero(&antispoof_opts, sizeof antispoof_opts);
1623 antispoof_opts.rtableid = -1;
1624 $$ = antispoof_opts;
1628 antispoof_opts_l : antispoof_opts_l antispoof_opt
1632 antispoof_opt : label {
1633 if (antispoof_opts.labelcount >= PF_RULE_MAX_LABEL_COUNT) {
1634 yyerror("label can only be used %d times", PF_RULE_MAX_LABEL_COUNT);
1637 antispoof_opts.label[antispoof_opts.labelcount++] = $1;
1639 | RIDENTIFIER number {
1640 antispoof_opts.ridentifier = $2;
1643 if ($2 < 0 || $2 > rt_tableid_max()) {
1644 yyerror("invalid rtable id");
1647 antispoof_opts.rtableid = $2;
1651 not : '!' { $$ = 1; }
1652 | /* empty */ { $$ = 0; }
1655 tabledef : TABLE '<' STRING '>' table_opts {
1656 struct node_host *h, *nh;
1657 struct node_tinit *ti, *nti;
1659 if (strlen($3) >= PF_TABLE_NAME_SIZE) {
1660 yyerror("table name too long, max %d chars",
1661 PF_TABLE_NAME_SIZE - 1);
1665 if (pf->loadopt & PFCTL_FLAG_TABLE)
1666 if (process_tabledef($3, &$5)) {
1671 for (ti = SIMPLEQ_FIRST(&$5.init_nodes);
1672 ti != SIMPLEQ_END(&$5.init_nodes); ti = nti) {
1675 for (h = ti->host; h != NULL; h = nh) {
1679 nti = SIMPLEQ_NEXT(ti, entries);
1686 bzero(&table_opts, sizeof table_opts);
1687 SIMPLEQ_INIT(&table_opts.init_nodes);
1690 { $$ = table_opts; }
1693 bzero(&table_opts, sizeof table_opts);
1694 SIMPLEQ_INIT(&table_opts.init_nodes);
1699 table_opts_l : table_opts_l table_opt
1703 table_opt : STRING {
1704 if (!strcmp($1, "const"))
1705 table_opts.flags |= PFR_TFLAG_CONST;
1706 else if (!strcmp($1, "persist"))
1707 table_opts.flags |= PFR_TFLAG_PERSIST;
1708 else if (!strcmp($1, "counters"))
1709 table_opts.flags |= PFR_TFLAG_COUNTERS;
1711 yyerror("invalid table option '%s'", $1);
1717 | '{' optnl '}' { table_opts.init_addr = 1; }
1718 | '{' optnl host_list '}' {
1719 struct node_host *n;
1720 struct node_tinit *ti;
1722 for (n = $3; n != NULL; n = n->next) {
1723 switch (n->addr.type) {
1724 case PF_ADDR_ADDRMASK:
1727 yyerror("address ranges are not "
1728 "permitted inside tables");
1730 case PF_ADDR_DYNIFTL:
1731 yyerror("dynamic addresses are not "
1732 "permitted inside tables");
1735 yyerror("tables cannot contain tables");
1737 case PF_ADDR_NOROUTE:
1738 yyerror("\"no-route\" is not permitted "
1741 case PF_ADDR_URPFFAILED:
1742 yyerror("\"urpf-failed\" is not "
1743 "permitted inside tables");
1746 yyerror("unknown address type %d",
1751 if (!(ti = calloc(1, sizeof(*ti))))
1752 err(1, "table_opt: calloc");
1754 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1756 table_opts.init_addr = 1;
1759 struct node_tinit *ti;
1761 if (!(ti = calloc(1, sizeof(*ti))))
1762 err(1, "table_opt: calloc");
1764 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1766 table_opts.init_addr = 1;
1770 altqif : ALTQ interface queue_opts QUEUE qassign {
1773 if (check_rulestate(PFCTL_STATE_QUEUE))
1776 memset(&a, 0, sizeof(a));
1777 if ($3.scheduler.qtype == ALTQT_NONE) {
1778 yyerror("no scheduler specified!");
1781 a.scheduler = $3.scheduler.qtype;
1782 a.qlimit = $3.qlimit;
1783 a.tbrsize = $3.tbrsize;
1784 if ($5 == NULL && $3.scheduler.qtype != ALTQT_CODEL) {
1785 yyerror("no child queues specified");
1788 if (expand_altq(&a, $2, $5, $3.queue_bwspec,
1794 queuespec : QUEUE STRING interface queue_opts qassign {
1797 if (check_rulestate(PFCTL_STATE_QUEUE)) {
1802 memset(&a, 0, sizeof(a));
1804 if (strlcpy(a.qname, $2, sizeof(a.qname)) >=
1806 yyerror("queue name too long (max "
1807 "%d chars)", PF_QNAME_SIZE-1);
1813 yyerror("cannot specify tbrsize for queue");
1816 if ($4.priority > 255) {
1817 yyerror("priority out of range: max 255");
1820 a.priority = $4.priority;
1821 a.qlimit = $4.qlimit;
1822 a.scheduler = $4.scheduler.qtype;
1823 if (expand_queue(&a, $3, $5, $4.queue_bwspec,
1825 yyerror("errors in queue definition");
1832 bzero(&queue_opts, sizeof queue_opts);
1833 queue_opts.priority = DEFAULT_PRIORITY;
1834 queue_opts.qlimit = DEFAULT_QLIMIT;
1835 queue_opts.scheduler.qtype = ALTQT_NONE;
1836 queue_opts.queue_bwspec.bw_percent = 100;
1839 { $$ = queue_opts; }
1841 bzero(&queue_opts, sizeof queue_opts);
1842 queue_opts.priority = DEFAULT_PRIORITY;
1843 queue_opts.qlimit = DEFAULT_QLIMIT;
1844 queue_opts.scheduler.qtype = ALTQT_NONE;
1845 queue_opts.queue_bwspec.bw_percent = 100;
1850 queue_opts_l : queue_opts_l queue_opt
1854 queue_opt : BANDWIDTH bandwidth {
1855 if (queue_opts.marker & QOM_BWSPEC) {
1856 yyerror("bandwidth cannot be respecified");
1859 queue_opts.marker |= QOM_BWSPEC;
1860 queue_opts.queue_bwspec = $2;
1863 if (queue_opts.marker & QOM_PRIORITY) {
1864 yyerror("priority cannot be respecified");
1867 if ($2 < 0 || $2 > 255) {
1868 yyerror("priority out of range: max 255");
1871 queue_opts.marker |= QOM_PRIORITY;
1872 queue_opts.priority = $2;
1875 if (queue_opts.marker & QOM_QLIMIT) {
1876 yyerror("qlimit cannot be respecified");
1879 if ($2 < 0 || $2 > 65535) {
1880 yyerror("qlimit out of range: max 65535");
1883 queue_opts.marker |= QOM_QLIMIT;
1884 queue_opts.qlimit = $2;
1887 if (queue_opts.marker & QOM_SCHEDULER) {
1888 yyerror("scheduler cannot be respecified");
1891 queue_opts.marker |= QOM_SCHEDULER;
1892 queue_opts.scheduler = $1;
1895 if (queue_opts.marker & QOM_TBRSIZE) {
1896 yyerror("tbrsize cannot be respecified");
1899 if ($2 < 0 || $2 > UINT_MAX) {
1900 yyerror("tbrsize too big: max %u", UINT_MAX);
1903 queue_opts.marker |= QOM_TBRSIZE;
1904 queue_opts.tbrsize = $2;
1908 bandwidth : STRING {
1914 bps = strtod($1, &cp);
1916 if (strlen(cp) > 1) {
1918 if (!strcmp(cu, "Bit") ||
1920 !strcmp(cu, "bit") ||
1925 if (!strcmp(cp, "b"))
1927 else if (!strcmp(cp, "K"))
1929 else if (!strcmp(cp, "M"))
1931 else if (!strcmp(cp, "G"))
1932 bps *= 1000 * 1000 * 1000;
1933 else if (!strcmp(cp, "%")) {
1934 if (bps < 0 || bps > 100) {
1935 yyerror("bandwidth spec "
1940 $$.bw_percent = bps;
1943 yyerror("unknown unit %s", cp);
1949 $$.bw_absolute = (u_int64_t)bps;
1952 if ($1 < 0 || $1 >= LLONG_MAX) {
1953 yyerror("bandwidth number too big");
1957 $$.bw_absolute = $1;
1962 $$.qtype = ALTQT_CBQ;
1963 $$.data.cbq_opts.flags = 0;
1965 | CBQ '(' cbqflags_list ')' {
1966 $$.qtype = ALTQT_CBQ;
1967 $$.data.cbq_opts.flags = $3;
1970 $$.qtype = ALTQT_PRIQ;
1971 $$.data.priq_opts.flags = 0;
1973 | PRIQ '(' priqflags_list ')' {
1974 $$.qtype = ALTQT_PRIQ;
1975 $$.data.priq_opts.flags = $3;
1978 $$.qtype = ALTQT_HFSC;
1979 bzero(&$$.data.hfsc_opts,
1980 sizeof(struct node_hfsc_opts));
1982 | HFSC '(' hfsc_opts ')' {
1983 $$.qtype = ALTQT_HFSC;
1984 $$.data.hfsc_opts = $3;
1987 $$.qtype = ALTQT_FAIRQ;
1988 bzero(&$$.data.fairq_opts,
1989 sizeof(struct node_fairq_opts));
1991 | FAIRQ '(' fairq_opts ')' {
1992 $$.qtype = ALTQT_FAIRQ;
1993 $$.data.fairq_opts = $3;
1996 $$.qtype = ALTQT_CODEL;
1997 bzero(&$$.data.codel_opts,
1998 sizeof(struct codel_opts));
2000 | CODEL '(' codel_opts ')' {
2001 $$.qtype = ALTQT_CODEL;
2002 $$.data.codel_opts = $3;
2006 cbqflags_list : cbqflags_item { $$ |= $1; }
2007 | cbqflags_list comma cbqflags_item { $$ |= $3; }
2010 cbqflags_item : STRING {
2011 if (!strcmp($1, "default"))
2012 $$ = CBQCLF_DEFCLASS;
2013 else if (!strcmp($1, "borrow"))
2015 else if (!strcmp($1, "red"))
2017 else if (!strcmp($1, "ecn"))
2018 $$ = CBQCLF_RED|CBQCLF_ECN;
2019 else if (!strcmp($1, "rio"))
2021 else if (!strcmp($1, "codel"))
2024 yyerror("unknown cbq flag \"%s\"", $1);
2032 priqflags_list : priqflags_item { $$ |= $1; }
2033 | priqflags_list comma priqflags_item { $$ |= $3; }
2036 priqflags_item : STRING {
2037 if (!strcmp($1, "default"))
2038 $$ = PRCF_DEFAULTCLASS;
2039 else if (!strcmp($1, "red"))
2041 else if (!strcmp($1, "ecn"))
2042 $$ = PRCF_RED|PRCF_ECN;
2043 else if (!strcmp($1, "rio"))
2045 else if (!strcmp($1, "codel"))
2048 yyerror("unknown priq flag \"%s\"", $1);
2058 sizeof(struct node_hfsc_opts));
2065 hfscopts_list : hfscopts_item
2066 | hfscopts_list comma hfscopts_item
2069 hfscopts_item : LINKSHARE bandwidth {
2070 if (hfsc_opts.linkshare.used) {
2071 yyerror("linkshare already specified");
2074 hfsc_opts.linkshare.m2 = $2;
2075 hfsc_opts.linkshare.used = 1;
2077 | LINKSHARE '(' bandwidth comma NUMBER comma bandwidth ')'
2079 if ($5 < 0 || $5 > INT_MAX) {
2080 yyerror("timing in curve out of range");
2083 if (hfsc_opts.linkshare.used) {
2084 yyerror("linkshare already specified");
2087 hfsc_opts.linkshare.m1 = $3;
2088 hfsc_opts.linkshare.d = $5;
2089 hfsc_opts.linkshare.m2 = $7;
2090 hfsc_opts.linkshare.used = 1;
2092 | REALTIME bandwidth {
2093 if (hfsc_opts.realtime.used) {
2094 yyerror("realtime already specified");
2097 hfsc_opts.realtime.m2 = $2;
2098 hfsc_opts.realtime.used = 1;
2100 | REALTIME '(' bandwidth comma NUMBER comma bandwidth ')'
2102 if ($5 < 0 || $5 > INT_MAX) {
2103 yyerror("timing in curve out of range");
2106 if (hfsc_opts.realtime.used) {
2107 yyerror("realtime already specified");
2110 hfsc_opts.realtime.m1 = $3;
2111 hfsc_opts.realtime.d = $5;
2112 hfsc_opts.realtime.m2 = $7;
2113 hfsc_opts.realtime.used = 1;
2115 | UPPERLIMIT bandwidth {
2116 if (hfsc_opts.upperlimit.used) {
2117 yyerror("upperlimit already specified");
2120 hfsc_opts.upperlimit.m2 = $2;
2121 hfsc_opts.upperlimit.used = 1;
2123 | UPPERLIMIT '(' bandwidth comma NUMBER comma bandwidth ')'
2125 if ($5 < 0 || $5 > INT_MAX) {
2126 yyerror("timing in curve out of range");
2129 if (hfsc_opts.upperlimit.used) {
2130 yyerror("upperlimit already specified");
2133 hfsc_opts.upperlimit.m1 = $3;
2134 hfsc_opts.upperlimit.d = $5;
2135 hfsc_opts.upperlimit.m2 = $7;
2136 hfsc_opts.upperlimit.used = 1;
2139 if (!strcmp($1, "default"))
2140 hfsc_opts.flags |= HFCF_DEFAULTCLASS;
2141 else if (!strcmp($1, "red"))
2142 hfsc_opts.flags |= HFCF_RED;
2143 else if (!strcmp($1, "ecn"))
2144 hfsc_opts.flags |= HFCF_RED|HFCF_ECN;
2145 else if (!strcmp($1, "rio"))
2146 hfsc_opts.flags |= HFCF_RIO;
2147 else if (!strcmp($1, "codel"))
2148 hfsc_opts.flags |= HFCF_CODEL;
2150 yyerror("unknown hfsc flag \"%s\"", $1);
2160 sizeof(struct node_fairq_opts));
2167 fairqopts_list : fairqopts_item
2168 | fairqopts_list comma fairqopts_item
2171 fairqopts_item : LINKSHARE bandwidth {
2172 if (fairq_opts.linkshare.used) {
2173 yyerror("linkshare already specified");
2176 fairq_opts.linkshare.m2 = $2;
2177 fairq_opts.linkshare.used = 1;
2179 | LINKSHARE '(' bandwidth number bandwidth ')' {
2180 if (fairq_opts.linkshare.used) {
2181 yyerror("linkshare already specified");
2184 fairq_opts.linkshare.m1 = $3;
2185 fairq_opts.linkshare.d = $4;
2186 fairq_opts.linkshare.m2 = $5;
2187 fairq_opts.linkshare.used = 1;
2190 fairq_opts.hogs_bw = $2;
2193 fairq_opts.nbuckets = $2;
2196 if (!strcmp($1, "default"))
2197 fairq_opts.flags |= FARF_DEFAULTCLASS;
2198 else if (!strcmp($1, "red"))
2199 fairq_opts.flags |= FARF_RED;
2200 else if (!strcmp($1, "ecn"))
2201 fairq_opts.flags |= FARF_RED|FARF_ECN;
2202 else if (!strcmp($1, "rio"))
2203 fairq_opts.flags |= FARF_RIO;
2204 else if (!strcmp($1, "codel"))
2205 fairq_opts.flags |= FARF_CODEL;
2207 yyerror("unknown fairq flag \"%s\"", $1);
2217 sizeof(struct codel_opts));
2224 codelopts_list : codelopts_item
2225 | codelopts_list comma codelopts_item
2228 codelopts_item : INTERVAL number {
2229 if (codel_opts.interval) {
2230 yyerror("interval already specified");
2233 codel_opts.interval = $2;
2236 if (codel_opts.target) {
2237 yyerror("target already specified");
2240 codel_opts.target = $2;
2243 if (!strcmp($1, "ecn"))
2246 yyerror("unknown codel option \"%s\"", $1);
2254 qassign : /* empty */ { $$ = NULL; }
2255 | qassign_item { $$ = $1; }
2256 | '{' optnl qassign_list '}' { $$ = $3; }
2259 qassign_list : qassign_item optnl { $$ = $1; }
2260 | qassign_list comma qassign_item optnl {
2261 $1->tail->next = $3;
2267 qassign_item : STRING {
2268 $$ = calloc(1, sizeof(struct node_queue));
2270 err(1, "qassign_item: calloc");
2271 if (strlcpy($$->queue, $1, sizeof($$->queue)) >=
2272 sizeof($$->queue)) {
2273 yyerror("queue name '%s' too long (max "
2274 "%d chars)", $1, sizeof($$->queue)-1);
2285 pfrule : action dir logquick interface route af proto fromto
2288 struct pfctl_rule r;
2289 struct node_state_opt *o;
2290 struct node_proto *proto;
2296 if (check_rulestate(PFCTL_STATE_FILTER))
2299 memset(&r, 0, sizeof(r));
2303 case PFRULE_RETURNRST:
2304 r.rule_flag |= PFRULE_RETURNRST;
2305 r.return_ttl = $1.w;
2307 case PFRULE_RETURNICMP:
2308 r.rule_flag |= PFRULE_RETURNICMP;
2309 r.return_icmp = $1.w;
2310 r.return_icmp6 = $1.w2;
2313 r.rule_flag |= PFRULE_RETURN;
2314 r.return_icmp = $1.w;
2315 r.return_icmp6 = $1.w2;
2323 r.rtableid = $9.rtableid;
2325 if ($9.marker & FOM_PRIO) {
2327 r.prio = PF_PRIO_ZERO;
2331 if ($9.marker & FOM_SETPRIO) {
2332 r.set_prio[0] = $9.set_prio[0];
2333 r.set_prio[1] = $9.set_prio[1];
2334 r.scrub_flags |= PFSTATE_SETPRIO;
2339 if (strlcpy(r.tagname, $9.tag,
2340 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
2341 yyerror("tag too long, max %u chars",
2342 PF_TAG_NAME_SIZE - 1);
2346 if (strlcpy(r.match_tagname, $9.match_tag,
2347 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
2348 yyerror("tag too long, max %u chars",
2349 PF_TAG_NAME_SIZE - 1);
2352 r.match_tag_not = $9.match_tag_not;
2353 if (rule_label(&r, $9.label))
2355 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++)
2357 r.ridentifier = $9.ridentifier;
2358 r.flags = $9.flags.b1;
2359 r.flagset = $9.flags.b2;
2360 if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
2361 yyerror("flags always false");
2364 if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
2365 for (proto = $7; proto != NULL &&
2366 proto->proto != IPPROTO_TCP;
2367 proto = proto->next)
2369 if (proto == NULL && $7 != NULL) {
2370 if ($9.flags.b1 || $9.flags.b2)
2372 "flags only apply to tcp");
2375 "OS fingerprinting only "
2380 if (($9.flags.b1 & parse_flags("S")) == 0 &&
2382 yyerror("OS fingerprinting requires "
2383 "the SYN TCP flag (flags S/SA)");
2390 r.keep_state = $9.keep.action;
2391 o = $9.keep.options;
2393 /* 'keep state' by default on pass rules. */
2394 if (!r.keep_state && !r.action &&
2395 !($9.marker & FOM_KEEP)) {
2396 r.keep_state = PF_STATE_NORMAL;
2397 o = keep_state_defaults;
2402 struct node_state_opt *p = o;
2405 case PF_STATE_OPT_MAX:
2407 yyerror("state option 'max' "
2408 "multiple definitions");
2411 r.max_states = o->data.max_states;
2413 case PF_STATE_OPT_NOSYNC:
2414 if (r.rule_flag & PFRULE_NOSYNC) {
2415 yyerror("state option 'sync' "
2416 "multiple definitions");
2419 r.rule_flag |= PFRULE_NOSYNC;
2421 case PF_STATE_OPT_SRCTRACK:
2423 yyerror("state option "
2425 "multiple definitions");
2428 srctrack = o->data.src_track;
2429 r.rule_flag |= PFRULE_SRCTRACK;
2431 case PF_STATE_OPT_MAX_SRC_STATES:
2432 if (r.max_src_states) {
2433 yyerror("state option "
2435 "multiple definitions");
2438 if (o->data.max_src_states == 0) {
2439 yyerror("'max-src-states' must "
2444 o->data.max_src_states;
2445 r.rule_flag |= PFRULE_SRCTRACK;
2447 case PF_STATE_OPT_OVERLOAD:
2448 if (r.overload_tblname[0]) {
2449 yyerror("multiple 'overload' "
2450 "table definitions");
2453 if (strlcpy(r.overload_tblname,
2454 o->data.overload.tblname,
2455 PF_TABLE_NAME_SIZE) >=
2456 PF_TABLE_NAME_SIZE) {
2457 yyerror("state option: "
2461 r.flush = o->data.overload.flush;
2463 case PF_STATE_OPT_MAX_SRC_CONN:
2464 if (r.max_src_conn) {
2465 yyerror("state option "
2467 "multiple definitions");
2470 if (o->data.max_src_conn == 0) {
2471 yyerror("'max-src-conn' "
2476 o->data.max_src_conn;
2477 r.rule_flag |= PFRULE_SRCTRACK |
2478 PFRULE_RULESRCTRACK;
2480 case PF_STATE_OPT_MAX_SRC_CONN_RATE:
2481 if (r.max_src_conn_rate.limit) {
2482 yyerror("state option "
2483 "'max-src-conn-rate' "
2484 "multiple definitions");
2487 if (!o->data.max_src_conn_rate.limit ||
2488 !o->data.max_src_conn_rate.seconds) {
2489 yyerror("'max-src-conn-rate' "
2490 "values must be > 0");
2493 if (o->data.max_src_conn_rate.limit >
2495 yyerror("'max-src-conn-rate' "
2496 "maximum rate must be < %u",
2500 r.max_src_conn_rate.limit =
2501 o->data.max_src_conn_rate.limit;
2502 r.max_src_conn_rate.seconds =
2503 o->data.max_src_conn_rate.seconds;
2504 r.rule_flag |= PFRULE_SRCTRACK |
2505 PFRULE_RULESRCTRACK;
2507 case PF_STATE_OPT_MAX_SRC_NODES:
2508 if (r.max_src_nodes) {
2509 yyerror("state option "
2511 "multiple definitions");
2514 if (o->data.max_src_nodes == 0) {
2515 yyerror("'max-src-nodes' must "
2520 o->data.max_src_nodes;
2521 r.rule_flag |= PFRULE_SRCTRACK |
2522 PFRULE_RULESRCTRACK;
2524 case PF_STATE_OPT_STATELOCK:
2526 yyerror("state locking option: "
2527 "multiple definitions");
2531 r.rule_flag |= o->data.statelock;
2533 case PF_STATE_OPT_SLOPPY:
2534 if (r.rule_flag & PFRULE_STATESLOPPY) {
2535 yyerror("state sloppy option: "
2536 "multiple definitions");
2539 r.rule_flag |= PFRULE_STATESLOPPY;
2541 case PF_STATE_OPT_TIMEOUT:
2542 if (o->data.timeout.number ==
2543 PFTM_ADAPTIVE_START ||
2544 o->data.timeout.number ==
2547 if (r.timeout[o->data.timeout.number]) {
2548 yyerror("state timeout %s "
2549 "multiple definitions",
2550 pf_timeouts[o->data.
2551 timeout.number].name);
2554 r.timeout[o->data.timeout.number] =
2555 o->data.timeout.seconds;
2562 /* 'flags S/SA' by default on stateful rules */
2563 if (!r.action && !r.flags && !r.flagset &&
2564 !$9.fragment && !($9.marker & FOM_FLAGS) &&
2566 r.flags = parse_flags("S");
2567 r.flagset = parse_flags("SA");
2569 if (!adaptive && r.max_states) {
2570 r.timeout[PFTM_ADAPTIVE_START] =
2571 (r.max_states / 10) * 6;
2572 r.timeout[PFTM_ADAPTIVE_END] =
2573 (r.max_states / 10) * 12;
2575 if (r.rule_flag & PFRULE_SRCTRACK) {
2576 if (srctrack == PF_SRCTRACK_GLOBAL &&
2578 yyerror("'max-src-nodes' is "
2579 "incompatible with "
2580 "'source-track global'");
2583 if (srctrack == PF_SRCTRACK_GLOBAL &&
2585 yyerror("'max-src-conn' is "
2586 "incompatible with "
2587 "'source-track global'");
2590 if (srctrack == PF_SRCTRACK_GLOBAL &&
2591 r.max_src_conn_rate.seconds) {
2592 yyerror("'max-src-conn-rate' is "
2593 "incompatible with "
2594 "'source-track global'");
2597 if (r.timeout[PFTM_SRC_NODE] <
2598 r.max_src_conn_rate.seconds)
2599 r.timeout[PFTM_SRC_NODE] =
2600 r.max_src_conn_rate.seconds;
2601 r.rule_flag |= PFRULE_SRCTRACK;
2602 if (srctrack == PF_SRCTRACK_RULE)
2603 r.rule_flag |= PFRULE_RULESRCTRACK;
2605 if (r.keep_state && !statelock)
2606 r.rule_flag |= default_statelock;
2609 r.rule_flag |= PFRULE_FRAGMENT;
2610 r.allow_opts = $9.allowopts;
2612 decide_address_family($8.src.host, &r.af);
2613 decide_address_family($8.dst.host, &r.af);
2617 yyerror("direction must be explicit "
2618 "with rules that specify routing");
2622 r.rpool.opts = $5.pool_opts;
2624 memcpy(&r.rpool.key, $5.key,
2625 sizeof(struct pf_poolhashkey));
2628 decide_address_family($5.host, &r.af);
2629 remove_invalid_hosts(&$5.host, &r.af);
2630 if ($5.host == NULL) {
2631 yyerror("no routing address with "
2632 "matching address family found.");
2635 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
2636 PF_POOL_NONE && ($5.host->next != NULL ||
2637 $5.host->addr.type == PF_ADDR_TABLE ||
2638 DYNIF_MULTIADDR($5.host->addr)))
2639 r.rpool.opts |= PF_POOL_ROUNDROBIN;
2640 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2641 PF_POOL_ROUNDROBIN &&
2642 disallow_table($5.host, "tables are only "
2643 "supported in round-robin routing pools"))
2645 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2646 PF_POOL_ROUNDROBIN &&
2647 disallow_alias($5.host, "interface (%s) "
2648 "is only supported in round-robin "
2651 if ($5.host->next != NULL) {
2652 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2653 PF_POOL_ROUNDROBIN) {
2654 yyerror("r.rpool.opts must "
2655 "be PF_POOL_ROUNDROBIN");
2660 if ($9.queues.qname != NULL) {
2661 if (strlcpy(r.qname, $9.queues.qname,
2662 sizeof(r.qname)) >= sizeof(r.qname)) {
2663 yyerror("rule qname too long (max "
2664 "%d chars)", sizeof(r.qname)-1);
2667 free($9.queues.qname);
2669 if ($9.queues.pqname != NULL) {
2670 if (strlcpy(r.pqname, $9.queues.pqname,
2671 sizeof(r.pqname)) >= sizeof(r.pqname)) {
2672 yyerror("rule pqname too long (max "
2673 "%d chars)", sizeof(r.pqname)-1);
2676 free($9.queues.pqname);
2679 r.divert.port = $9.divert.port;
2681 if ((r.divert.port = $9.divert.port)) {
2682 if (r.direction == PF_OUT) {
2683 if ($9.divert.addr) {
2684 yyerror("address specified "
2685 "for outgoing divert");
2688 bzero(&r.divert.addr,
2689 sizeof(r.divert.addr));
2691 if (!$9.divert.addr) {
2692 yyerror("no address specified "
2693 "for incoming divert");
2696 if ($9.divert.addr->af != r.af) {
2697 yyerror("address family "
2698 "mismatch for divert");
2702 $9.divert.addr->addr.v.a.addr;
2707 if ($9.dnpipe || $9.dnrpipe) {
2708 r.dnpipe = $9.dnpipe;
2709 r.dnrpipe = $9.dnrpipe;
2710 if ($9.free_flags & PFRULE_DN_IS_PIPE)
2711 r.free_flags |= PFRULE_DN_IS_PIPE;
2713 r.free_flags |= PFRULE_DN_IS_QUEUE;
2716 expand_rule(&r, $4, $5.host, $7, $8.src_os,
2717 $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
2718 $9.uid, $9.gid, $9.icmpspec, "");
2723 bzero(&filter_opts, sizeof filter_opts);
2724 filter_opts.rtableid = -1;
2727 { $$ = filter_opts; }
2729 bzero(&filter_opts, sizeof filter_opts);
2730 filter_opts.rtableid = -1;
2735 filter_opts_l : filter_opts_l filter_opt
2739 filter_opt : USER uids {
2740 if (filter_opts.uid)
2741 $2->tail->next = filter_opts.uid;
2742 filter_opts.uid = $2;
2745 if (filter_opts.gid)
2746 $2->tail->next = filter_opts.gid;
2747 filter_opts.gid = $2;
2750 if (filter_opts.marker & FOM_FLAGS) {
2751 yyerror("flags cannot be redefined");
2754 filter_opts.marker |= FOM_FLAGS;
2755 filter_opts.flags.b1 |= $1.b1;
2756 filter_opts.flags.b2 |= $1.b2;
2757 filter_opts.flags.w |= $1.w;
2758 filter_opts.flags.w2 |= $1.w2;
2761 if (filter_opts.marker & FOM_ICMP) {
2762 yyerror("icmp-type cannot be redefined");
2765 filter_opts.marker |= FOM_ICMP;
2766 filter_opts.icmpspec = $1;
2769 if (filter_opts.marker & FOM_PRIO) {
2770 yyerror("prio cannot be redefined");
2773 if ($2 < 0 || $2 > PF_PRIO_MAX) {
2774 yyerror("prio must be 0 - %u", PF_PRIO_MAX);
2777 filter_opts.marker |= FOM_PRIO;
2778 filter_opts.prio = $2;
2781 if (filter_opts.marker & FOM_TOS) {
2782 yyerror("tos cannot be redefined");
2785 filter_opts.marker |= FOM_TOS;
2786 filter_opts.tos = $2;
2789 if (filter_opts.marker & FOM_KEEP) {
2790 yyerror("modulate or keep cannot be redefined");
2793 filter_opts.marker |= FOM_KEEP;
2794 filter_opts.keep.action = $1.action;
2795 filter_opts.keep.options = $1.options;
2797 | RIDENTIFIER number {
2798 filter_opts.ridentifier = $2;
2801 filter_opts.fragment = 1;
2804 filter_opts.allowopts = 1;
2807 if (filter_opts.labelcount >= PF_RULE_MAX_LABEL_COUNT) {
2808 yyerror("label can only be used %d times", PF_RULE_MAX_LABEL_COUNT);
2811 filter_opts.label[filter_opts.labelcount++] = $1;
2814 if (filter_opts.queues.qname) {
2815 yyerror("queue cannot be redefined");
2818 filter_opts.queues = $1;
2821 filter_opts.dnpipe = $2;
2822 filter_opts.free_flags |= PFRULE_DN_IS_PIPE;
2824 | DNPIPE '(' number ')' {
2825 filter_opts.dnpipe = $3;
2826 filter_opts.free_flags |= PFRULE_DN_IS_PIPE;
2828 | DNPIPE '(' number comma number ')' {
2829 filter_opts.dnrpipe = $5;
2830 filter_opts.dnpipe = $3;
2831 filter_opts.free_flags |= PFRULE_DN_IS_PIPE;
2834 filter_opts.dnpipe = $2;
2835 filter_opts.free_flags |= PFRULE_DN_IS_QUEUE;
2837 | DNQUEUE '(' number comma number ')' {
2838 filter_opts.dnrpipe = $5;
2839 filter_opts.dnpipe = $3;
2840 filter_opts.free_flags |= PFRULE_DN_IS_QUEUE;
2842 | DNQUEUE '(' number ')' {
2843 filter_opts.dnpipe = $3;
2844 filter_opts.free_flags |= PFRULE_DN_IS_QUEUE;
2847 filter_opts.tag = $2;
2849 | not TAGGED string {
2850 filter_opts.match_tag = $3;
2851 filter_opts.match_tag_not = $1;
2853 | PROBABILITY probability {
2856 p = floor($2 * UINT_MAX + 0.5);
2857 if (p < 0.0 || p > UINT_MAX) {
2858 yyerror("invalid probability: %lf", p);
2861 filter_opts.prob = (u_int32_t)p;
2862 if (filter_opts.prob == 0)
2863 filter_opts.prob = 1;
2866 if ($2 < 0 || $2 > rt_tableid_max()) {
2867 yyerror("invalid rtable id");
2870 filter_opts.rtableid = $2;
2872 | DIVERTTO portplain {
2874 filter_opts.divert.port = $2.a;
2875 if (!filter_opts.divert.port) {
2876 yyerror("invalid divert port: %u", ntohs($2.a));
2881 | DIVERTTO STRING PORT portplain {
2883 if ((filter_opts.divert.addr = host($2)) == NULL) {
2884 yyerror("could not parse divert address: %s",
2893 filter_opts.divert.port = $4.a;
2894 if (!filter_opts.divert.port) {
2895 yyerror("invalid divert port: %u", ntohs($4.a));
2901 yyerror("divert-reply has no meaning in FreeBSD pf(4)");
2904 filter_opts.divert.port = 1; /* some random value */
2910 filter_sets : SET '(' filter_sets_l ')' { $$ = filter_opts; }
2911 | SET filter_set { $$ = filter_opts; }
2914 filter_sets_l : filter_sets_l comma filter_set
2919 if (filter_opts.marker & FOM_SETPRIO) {
2920 yyerror("prio cannot be redefined");
2923 filter_opts.marker |= FOM_SETPRIO;
2924 filter_opts.set_prio[0] = $1.b1;
2925 filter_opts.set_prio[1] = $1.b2;
2927 prio : PRIO NUMBER {
2928 if ($2 < 0 || $2 > PF_PRIO_MAX) {
2929 yyerror("prio must be 0 - %u", PF_PRIO_MAX);
2934 | PRIO '(' NUMBER comma NUMBER ')' {
2935 if ($3 < 0 || $3 > PF_PRIO_MAX ||
2936 $5 < 0 || $5 > PF_PRIO_MAX) {
2937 yyerror("prio must be 0 - %u", PF_PRIO_MAX);
2945 probability : STRING {
2947 double p = strtod($1, &e);
2954 yyerror("invalid probability: %s", $1);
2970 $$.w = returnicmpdefault;
2971 $$.w2 = returnicmp6default;
2973 | MATCH { $$.b1 = PF_MATCH; $$.b2 = $$.w = 0; }
2974 | BLOCK blockspec { $$ = $2; $$.b1 = PF_DROP; }
2977 blockspec : /* empty */ {
2978 $$.b2 = blockpolicy;
2979 $$.w = returnicmpdefault;
2980 $$.w2 = returnicmp6default;
2983 $$.b2 = PFRULE_DROP;
2988 $$.b2 = PFRULE_RETURNRST;
2992 | RETURNRST '(' TTL NUMBER ')' {
2993 if ($4 < 0 || $4 > 255) {
2994 yyerror("illegal ttl value %d", $4);
2997 $$.b2 = PFRULE_RETURNRST;
3002 $$.b2 = PFRULE_RETURNICMP;
3003 $$.w = returnicmpdefault;
3004 $$.w2 = returnicmp6default;
3007 $$.b2 = PFRULE_RETURNICMP;
3008 $$.w = returnicmpdefault;
3009 $$.w2 = returnicmp6default;
3011 | RETURNICMP '(' reticmpspec ')' {
3012 $$.b2 = PFRULE_RETURNICMP;
3014 $$.w2 = returnicmpdefault;
3016 | RETURNICMP6 '(' reticmp6spec ')' {
3017 $$.b2 = PFRULE_RETURNICMP;
3018 $$.w = returnicmpdefault;
3021 | RETURNICMP '(' reticmpspec comma reticmp6spec ')' {
3022 $$.b2 = PFRULE_RETURNICMP;
3027 $$.b2 = PFRULE_RETURN;
3028 $$.w = returnicmpdefault;
3029 $$.w2 = returnicmp6default;
3033 reticmpspec : STRING {
3034 if (!($$ = parseicmpspec($1, AF_INET))) {
3043 if ($1 < 0 || $1 > 255) {
3044 yyerror("invalid icmp code %lu", $1);
3047 icmptype = returnicmpdefault >> 8;
3048 $$ = (icmptype << 8 | $1);
3052 reticmp6spec : STRING {
3053 if (!($$ = parseicmpspec($1, AF_INET6))) {
3062 if ($1 < 0 || $1 > 255) {
3063 yyerror("invalid icmp code %lu", $1);
3066 icmptype = returnicmp6default >> 8;
3067 $$ = (icmptype << 8 | $1);
3071 dir : /* empty */ { $$ = PF_INOUT; }
3072 | IN { $$ = PF_IN; }
3073 | OUT { $$ = PF_OUT; }
3076 quick : /* empty */ { $$.quick = 0; }
3077 | QUICK { $$.quick = 1; }
3080 logquick : /* empty */ { $$.log = 0; $$.quick = 0; $$.logif = 0; }
3081 | log { $$ = $1; $$.quick = 0; }
3082 | QUICK { $$.quick = 1; $$.log = 0; $$.logif = 0; }
3083 | log QUICK { $$ = $1; $$.quick = 1; }
3084 | QUICK log { $$ = $2; $$.quick = 1; }
3087 log : LOG { $$.log = PF_LOG; $$.logif = 0; }
3088 | LOG '(' logopts ')' {
3089 $$.log = PF_LOG | $3.log;
3090 $$.logif = $3.logif;
3094 logopts : logopt { $$ = $1; }
3095 | logopts comma logopt {
3096 $$.log = $1.log | $3.log;
3097 $$.logif = $3.logif;
3099 $$.logif = $1.logif;
3103 logopt : ALL { $$.log = PF_LOG_ALL; $$.logif = 0; }
3104 | USER { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
3105 | GROUP { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
3111 if (strncmp($2, "pflog", 5)) {
3112 yyerror("%s: should be a pflog interface", $2);
3116 i = strtonum($2 + 5, 0, 255, &errstr);
3118 yyerror("%s: %s", $2, errstr);
3127 interface : /* empty */ { $$ = NULL; }
3128 | ON if_item_not { $$ = $2; }
3129 | ON '{' optnl if_list '}' { $$ = $4; }
3132 if_list : if_item_not optnl { $$ = $1; }
3133 | if_list comma if_item_not optnl {
3134 $1->tail->next = $3;
3140 if_item_not : not if_item { $$ = $2; $$->not = $1; }
3144 struct node_host *n;
3146 $$ = calloc(1, sizeof(struct node_if));
3148 err(1, "if_item: calloc");
3149 if (strlcpy($$->ifname, $1, sizeof($$->ifname)) >=
3150 sizeof($$->ifname)) {
3153 yyerror("interface name too long");
3157 if ((n = ifa_exists($1)) != NULL)
3158 $$->ifa_flags = n->ifa_flags;
3167 af : /* empty */ { $$ = 0; }
3168 | INET { $$ = AF_INET; }
3169 | INET6 { $$ = AF_INET6; }
3172 etherproto : /* empty */ { $$ = NULL; }
3173 | PROTO etherproto_item { $$ = $2; }
3174 | PROTO '{' optnl etherproto_list '}' { $$ = $4; }
3177 etherproto_list : etherproto_item optnl { $$ = $1; }
3178 | etherproto_list comma etherproto_item optnl {
3179 $1->tail->next = $3;
3185 etherproto_item : etherprotoval {
3190 yyerror("proto 0 cannot be used");
3193 $$ = calloc(1, sizeof(struct node_proto));
3195 err(1, "proto_item: calloc");
3202 etherprotoval : NUMBER {
3203 if ($1 < 0 || $1 > 65565) {
3204 yyerror("protocol outside range");
3210 if (!strncmp($1, "0x", 2)) {
3211 if (sscanf($1, "0x%4x", &$$) != 1) {
3213 yyerror("invalid EtherType hex");
3217 yyerror("Symbolic EtherType not yet supported");
3222 proto : /* empty */ { $$ = NULL; }
3223 | PROTO proto_item { $$ = $2; }
3224 | PROTO '{' optnl proto_list '}' { $$ = $4; }
3227 proto_list : proto_item optnl { $$ = $1; }
3228 | proto_list comma proto_item optnl {
3229 $1->tail->next = $3;
3235 proto_item : protoval {
3240 yyerror("proto 0 cannot be used");
3243 $$ = calloc(1, sizeof(struct node_proto));
3245 err(1, "proto_item: calloc");
3255 p = getprotobyname($1);
3257 yyerror("unknown protocol %s", $1);
3265 if ($1 < 0 || $1 > 255) {
3266 yyerror("protocol outside range");
3272 l3fromto : /* empty */ {
3273 bzero(&$$, sizeof($$));
3276 if ($2.src.host != NULL &&
3277 $2.src.host->addr.type != PF_ADDR_ADDRMASK &&
3278 $2.src.host->addr.type != PF_ADDR_TABLE) {
3279 yyerror("from must be an address or table");
3282 if ($2.dst.host != NULL &&
3283 $2.dst.host->addr.type != PF_ADDR_ADDRMASK &&
3284 $2.dst.host->addr.type != PF_ADDR_TABLE) {
3285 yyerror("to must be an address or table");
3295 | etherfrom etherto {
3301 etherfrom : /* emtpy */ {
3302 bzero(&$$, sizeof($$));
3309 etherto : /* empty */ {
3310 bzero(&$$, sizeof($$));
3317 mac : string '/' NUMBER {
3318 $$ = node_mac_from_string_masklen($1, $3);
3324 if (strchr($1, '&')) {
3326 char *mac = strtok($1, "&");
3327 char *mask = strtok(NULL, "&");
3328 $$ = node_mac_from_string_mask(mac, mask);
3330 $$ = node_mac_from_string($1);
3340 for (n = $2; n != NULL; n = n->next)
3348 | '{' optnl mac_list '}'
3353 mac_list : xmac optnl {
3356 | mac_list comma xmac {
3359 else if ($1 == NULL)
3362 $1->tail->next = $3;
3363 $1->tail = $3->tail;
3382 os : /* empty */ { $$ = NULL; }
3383 | OS xos { $$ = $2; }
3384 | OS '{' optnl os_list '}' { $$ = $4; }
3388 $$ = calloc(1, sizeof(struct node_os));
3390 err(1, "os: calloc");
3396 os_list : xos optnl { $$ = $1; }
3397 | os_list comma xos optnl {
3398 $1->tail->next = $3;
3404 from : /* empty */ {
3418 if (disallow_urpf_failed($2.host, "\"urpf-failed\" is "
3419 "not permitted in a destination address"))
3425 ipportspec : ipspec {
3429 | ipspec PORT portspec {
3443 ipspec : ANY { $$ = NULL; }
3444 | xhost { $$ = $1; }
3445 | '{' optnl host_list '}' { $$ = $3; }
3448 toipspec : TO ipspec { $$ = $2; }
3449 | /* empty */ { $$ = NULL; }
3452 host_list : ipspec optnl { $$ = $1; }
3453 | host_list comma ipspec optnl {
3456 else if ($1 == NULL)
3459 $1->tail->next = $3;
3460 $1->tail = $3->tail;
3467 struct node_host *n;
3469 for (n = $2; n != NULL; n = n->next)
3474 $$ = calloc(1, sizeof(struct node_host));
3476 err(1, "xhost: calloc");
3477 $$->addr.type = PF_ADDR_NOROUTE;
3483 $$ = calloc(1, sizeof(struct node_host));
3485 err(1, "xhost: calloc");
3486 $$->addr.type = PF_ADDR_URPFFAILED;
3494 if (($$ = host($1)) == NULL) {
3495 /* error. "any" is handled elsewhere */
3497 yyerror("could not parse host specification");
3503 | STRING '-' STRING {
3504 struct node_host *b, *e;
3506 if ((b = host($1)) == NULL || (e = host($3)) == NULL) {
3509 yyerror("could not parse host specification");
3512 if (b->af != e->af ||
3513 b->addr.type != PF_ADDR_ADDRMASK ||
3514 e->addr.type != PF_ADDR_ADDRMASK ||
3515 unmask(&b->addr.v.a.mask, b->af) !=
3516 (b->af == AF_INET ? 32 : 128) ||
3517 unmask(&e->addr.v.a.mask, e->af) !=
3518 (e->af == AF_INET ? 32 : 128) ||
3519 b->next != NULL || b->not ||
3520 e->next != NULL || e->not) {
3525 yyerror("invalid address range");
3528 memcpy(&b->addr.v.a.mask, &e->addr.v.a.addr,
3529 sizeof(b->addr.v.a.mask));
3530 b->addr.type = PF_ADDR_RANGE;
3536 | STRING '/' NUMBER {
3539 if (asprintf(&buf, "%s/%lld", $1, (long long)$3) == -1)
3540 err(1, "host: asprintf");
3542 if (($$ = host(buf)) == NULL) {
3543 /* error. "any" is handled elsewhere */
3545 yyerror("could not parse host specification");
3550 | NUMBER '/' NUMBER {
3553 /* ie. for 10/8 parsing */
3555 if (asprintf(&buf, "%lld/%lld", (long long)$1, (long long)$3) == -1)
3557 if (asprintf(&buf, "%lld/%lld", $1, $3) == -1)
3559 err(1, "host: asprintf");
3560 if (($$ = host(buf)) == NULL) {
3561 /* error. "any" is handled elsewhere */
3563 yyerror("could not parse host specification");
3569 | dynaddr '/' NUMBER {
3570 struct node_host *n;
3572 if ($3 < 0 || $3 > 128) {
3573 yyerror("bit number too big");
3577 for (n = $1; n != NULL; n = n->next)
3581 if (strlen($2) >= PF_TABLE_NAME_SIZE) {
3582 yyerror("table name '%s' too long", $2);
3586 $$ = calloc(1, sizeof(struct node_host));
3588 err(1, "host: calloc");
3589 $$->addr.type = PF_ADDR_TABLE;
3590 if (strlcpy($$->addr.v.tblname, $2,
3591 sizeof($$->addr.v.tblname)) >=
3592 sizeof($$->addr.v.tblname))
3593 errx(1, "host: strlcpy");
3604 if (atoul($1, &ulval) == -1) {
3605 yyerror("%s is not a number", $1);
3614 dynaddr : '(' STRING ')' {
3619 if (!isalpha(op[0])) {
3620 yyerror("invalid interface name '%s'", op);
3624 while ((p = strrchr($2, ':')) != NULL) {
3625 if (!strcmp(p+1, "network"))
3626 flags |= PFI_AFLAG_NETWORK;
3627 else if (!strcmp(p+1, "broadcast"))
3628 flags |= PFI_AFLAG_BROADCAST;
3629 else if (!strcmp(p+1, "peer"))
3630 flags |= PFI_AFLAG_PEER;
3631 else if (!strcmp(p+1, "0"))
3632 flags |= PFI_AFLAG_NOALIAS;
3634 yyerror("interface %s has bad modifier",
3641 if (flags & (flags - 1) & PFI_AFLAG_MODEMASK) {
3643 yyerror("illegal combination of "
3644 "interface modifiers");
3647 $$ = calloc(1, sizeof(struct node_host));
3649 err(1, "address: calloc");
3651 set_ipmask($$, 128);
3652 $$->addr.type = PF_ADDR_DYNIFTL;
3653 $$->addr.iflags = flags;
3654 if (strlcpy($$->addr.v.ifname, $2,
3655 sizeof($$->addr.v.ifname)) >=
3656 sizeof($$->addr.v.ifname)) {
3659 yyerror("interface name too long");
3668 portspec : port_item { $$ = $1; }
3669 | '{' optnl port_list '}' { $$ = $3; }
3672 port_list : port_item optnl { $$ = $1; }
3673 | port_list comma port_item optnl {
3674 $1->tail->next = $3;
3680 port_item : portrange {
3681 $$ = calloc(1, sizeof(struct node_port));
3683 err(1, "port_item: calloc");
3693 | unaryop portrange {
3695 yyerror("':' cannot be used with an other "
3699 $$ = calloc(1, sizeof(struct node_port));
3701 err(1, "port_item: calloc");
3708 | portrange PORTBINARY portrange {
3710 yyerror("':' cannot be used with an other "
3714 $$ = calloc(1, sizeof(struct node_port));
3716 err(1, "port_item: calloc");
3725 portplain : numberstring {
3726 if (parseport($1, &$$, 0) == -1) {
3734 portrange : numberstring {
3735 if (parseport($1, &$$, PPORT_RANGE) == -1) {
3743 uids : uid_item { $$ = $1; }
3744 | '{' optnl uid_list '}' { $$ = $3; }
3747 uid_list : uid_item optnl { $$ = $1; }
3748 | uid_list comma uid_item optnl {
3749 $1->tail->next = $3;
3756 $$ = calloc(1, sizeof(struct node_uid));
3758 err(1, "uid_item: calloc");
3766 if ($2 == UID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
3767 yyerror("user unknown requires operator = or "
3771 $$ = calloc(1, sizeof(struct node_uid));
3773 err(1, "uid_item: calloc");
3780 | uid PORTBINARY uid {
3781 if ($1 == UID_MAX || $3 == UID_MAX) {
3782 yyerror("user unknown requires operator = or "
3786 $$ = calloc(1, sizeof(struct node_uid));
3788 err(1, "uid_item: calloc");
3798 if (!strcmp($1, "unknown"))
3803 if ((pw = getpwnam($1)) == NULL) {
3804 yyerror("unknown user %s", $1);
3813 if ($1 < 0 || $1 >= UID_MAX) {
3814 yyerror("illegal uid value %lu", $1);
3821 gids : gid_item { $$ = $1; }
3822 | '{' optnl gid_list '}' { $$ = $3; }
3825 gid_list : gid_item optnl { $$ = $1; }
3826 | gid_list comma gid_item optnl {
3827 $1->tail->next = $3;
3834 $$ = calloc(1, sizeof(struct node_gid));
3836 err(1, "gid_item: calloc");
3844 if ($2 == GID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
3845 yyerror("group unknown requires operator = or "
3849 $$ = calloc(1, sizeof(struct node_gid));
3851 err(1, "gid_item: calloc");
3858 | gid PORTBINARY gid {
3859 if ($1 == GID_MAX || $3 == GID_MAX) {
3860 yyerror("group unknown requires operator = or "
3864 $$ = calloc(1, sizeof(struct node_gid));
3866 err(1, "gid_item: calloc");
3876 if (!strcmp($1, "unknown"))
3881 if ((grp = getgrnam($1)) == NULL) {
3882 yyerror("unknown group %s", $1);
3891 if ($1 < 0 || $1 >= GID_MAX) {
3892 yyerror("illegal gid value %lu", $1);
3902 if ((f = parse_flags($1)) < 0) {
3903 yyerror("bad flags %s", $1);
3912 flags : FLAGS flag '/' flag { $$.b1 = $2.b1; $$.b2 = $4.b1; }
3913 | FLAGS '/' flag { $$.b1 = 0; $$.b2 = $3.b1; }
3914 | FLAGS ANY { $$.b1 = 0; $$.b2 = 0; }
3917 icmpspec : ICMPTYPE icmp_item { $$ = $2; }
3918 | ICMPTYPE '{' optnl icmp_list '}' { $$ = $4; }
3919 | ICMP6TYPE icmp6_item { $$ = $2; }
3920 | ICMP6TYPE '{' optnl icmp6_list '}' { $$ = $4; }
3923 icmp_list : icmp_item optnl { $$ = $1; }
3924 | icmp_list comma icmp_item optnl {
3925 $1->tail->next = $3;
3931 icmp6_list : icmp6_item optnl { $$ = $1; }
3932 | icmp6_list comma icmp6_item optnl {
3933 $1->tail->next = $3;
3939 icmp_item : icmptype {
3940 $$ = calloc(1, sizeof(struct node_icmp));
3942 err(1, "icmp_item: calloc");
3945 $$->proto = IPPROTO_ICMP;
3949 | icmptype CODE STRING {
3950 const struct icmpcodeent *p;
3952 if ((p = geticmpcodebyname($1-1, $3, AF_INET)) == NULL) {
3953 yyerror("unknown icmp-code %s", $3);
3959 $$ = calloc(1, sizeof(struct node_icmp));
3961 err(1, "icmp_item: calloc");
3963 $$->code = p->code + 1;
3964 $$->proto = IPPROTO_ICMP;
3968 | icmptype CODE NUMBER {
3969 if ($3 < 0 || $3 > 255) {
3970 yyerror("illegal icmp-code %lu", $3);
3973 $$ = calloc(1, sizeof(struct node_icmp));
3975 err(1, "icmp_item: calloc");
3978 $$->proto = IPPROTO_ICMP;
3984 icmp6_item : icmp6type {
3985 $$ = calloc(1, sizeof(struct node_icmp));
3987 err(1, "icmp_item: calloc");
3990 $$->proto = IPPROTO_ICMPV6;
3994 | icmp6type CODE STRING {
3995 const struct icmpcodeent *p;
3997 if ((p = geticmpcodebyname($1-1, $3, AF_INET6)) == NULL) {
3998 yyerror("unknown icmp6-code %s", $3);
4004 $$ = calloc(1, sizeof(struct node_icmp));
4006 err(1, "icmp_item: calloc");
4008 $$->code = p->code + 1;
4009 $$->proto = IPPROTO_ICMPV6;
4013 | icmp6type CODE NUMBER {
4014 if ($3 < 0 || $3 > 255) {
4015 yyerror("illegal icmp-code %lu", $3);
4018 $$ = calloc(1, sizeof(struct node_icmp));
4020 err(1, "icmp_item: calloc");
4023 $$->proto = IPPROTO_ICMPV6;
4030 const struct icmptypeent *p;
4032 if ((p = geticmptypebyname($1, AF_INET)) == NULL) {
4033 yyerror("unknown icmp-type %s", $1);
4041 if ($1 < 0 || $1 > 255) {
4042 yyerror("illegal icmp-type %lu", $1);
4049 icmp6type : STRING {
4050 const struct icmptypeent *p;
4052 if ((p = geticmptypebyname($1, AF_INET6)) ==
4054 yyerror("unknown icmp6-type %s", $1);
4062 if ($1 < 0 || $1 > 255) {
4063 yyerror("illegal icmp6-type %lu", $1);
4074 if (map_tos($1, &val))
4076 else if ($1[0] == '0' && $1[1] == 'x') {
4078 $$ = strtoul($1, &end, 16);
4079 if (errno || *end != '\0')
4082 $$ = 256; /* flag bad argument */
4083 if ($$ < 0 || $$ > 255) {
4084 yyerror("illegal tos value %s", $1);
4092 if ($$ < 0 || $$ > 255) {
4093 yyerror("illegal tos value %s", $1);
4099 sourcetrack : SOURCETRACK { $$ = PF_SRCTRACK; }
4100 | SOURCETRACK GLOBAL { $$ = PF_SRCTRACK_GLOBAL; }
4101 | SOURCETRACK RULE { $$ = PF_SRCTRACK_RULE; }
4104 statelock : IFBOUND {
4105 $$ = PFRULE_IFBOUND;
4116 | KEEP STATE state_opt_spec {
4117 $$.action = PF_STATE_NORMAL;
4120 | MODULATE STATE state_opt_spec {
4121 $$.action = PF_STATE_MODULATE;
4124 | SYNPROXY STATE state_opt_spec {
4125 $$.action = PF_STATE_SYNPROXY;
4130 flush : /* empty */ { $$ = 0; }
4131 | FLUSH { $$ = PF_FLUSH; }
4133 $$ = PF_FLUSH | PF_FLUSH_GLOBAL;
4137 state_opt_spec : '(' state_opt_list ')' { $$ = $2; }
4138 | /* empty */ { $$ = NULL; }
4141 state_opt_list : state_opt_item { $$ = $1; }
4142 | state_opt_list comma state_opt_item {
4143 $1->tail->next = $3;
4149 state_opt_item : MAXIMUM NUMBER {
4150 if ($2 < 0 || $2 > UINT_MAX) {
4151 yyerror("only positive values permitted");
4154 $$ = calloc(1, sizeof(struct node_state_opt));
4156 err(1, "state_opt_item: calloc");
4157 $$->type = PF_STATE_OPT_MAX;
4158 $$->data.max_states = $2;
4163 $$ = calloc(1, sizeof(struct node_state_opt));
4165 err(1, "state_opt_item: calloc");
4166 $$->type = PF_STATE_OPT_NOSYNC;
4170 | MAXSRCSTATES NUMBER {
4171 if ($2 < 0 || $2 > UINT_MAX) {
4172 yyerror("only positive values permitted");
4175 $$ = calloc(1, sizeof(struct node_state_opt));
4177 err(1, "state_opt_item: calloc");
4178 $$->type = PF_STATE_OPT_MAX_SRC_STATES;
4179 $$->data.max_src_states = $2;
4183 | MAXSRCCONN NUMBER {
4184 if ($2 < 0 || $2 > UINT_MAX) {
4185 yyerror("only positive values permitted");
4188 $$ = calloc(1, sizeof(struct node_state_opt));
4190 err(1, "state_opt_item: calloc");
4191 $$->type = PF_STATE_OPT_MAX_SRC_CONN;
4192 $$->data.max_src_conn = $2;
4196 | MAXSRCCONNRATE NUMBER '/' NUMBER {
4197 if ($2 < 0 || $2 > UINT_MAX ||
4198 $4 < 0 || $4 > UINT_MAX) {
4199 yyerror("only positive values permitted");
4202 $$ = calloc(1, sizeof(struct node_state_opt));
4204 err(1, "state_opt_item: calloc");
4205 $$->type = PF_STATE_OPT_MAX_SRC_CONN_RATE;
4206 $$->data.max_src_conn_rate.limit = $2;
4207 $$->data.max_src_conn_rate.seconds = $4;
4211 | OVERLOAD '<' STRING '>' flush {
4212 if (strlen($3) >= PF_TABLE_NAME_SIZE) {
4213 yyerror("table name '%s' too long", $3);
4217 $$ = calloc(1, sizeof(struct node_state_opt));
4219 err(1, "state_opt_item: calloc");
4220 if (strlcpy($$->data.overload.tblname, $3,
4221 PF_TABLE_NAME_SIZE) >= PF_TABLE_NAME_SIZE)
4222 errx(1, "state_opt_item: strlcpy");
4224 $$->type = PF_STATE_OPT_OVERLOAD;
4225 $$->data.overload.flush = $5;
4229 | MAXSRCNODES NUMBER {
4230 if ($2 < 0 || $2 > UINT_MAX) {
4231 yyerror("only positive values permitted");
4234 $$ = calloc(1, sizeof(struct node_state_opt));
4236 err(1, "state_opt_item: calloc");
4237 $$->type = PF_STATE_OPT_MAX_SRC_NODES;
4238 $$->data.max_src_nodes = $2;
4243 $$ = calloc(1, sizeof(struct node_state_opt));
4245 err(1, "state_opt_item: calloc");
4246 $$->type = PF_STATE_OPT_SRCTRACK;
4247 $$->data.src_track = $1;
4252 $$ = calloc(1, sizeof(struct node_state_opt));
4254 err(1, "state_opt_item: calloc");
4255 $$->type = PF_STATE_OPT_STATELOCK;
4256 $$->data.statelock = $1;
4261 $$ = calloc(1, sizeof(struct node_state_opt));
4263 err(1, "state_opt_item: calloc");
4264 $$->type = PF_STATE_OPT_SLOPPY;
4271 if ($2 < 0 || $2 > UINT_MAX) {
4272 yyerror("only positive values permitted");
4275 for (i = 0; pf_timeouts[i].name &&
4276 strcmp(pf_timeouts[i].name, $1); ++i)
4278 if (!pf_timeouts[i].name) {
4279 yyerror("illegal timeout name %s", $1);
4283 if (strchr(pf_timeouts[i].name, '.') == NULL) {
4284 yyerror("illegal state timeout %s", $1);
4289 $$ = calloc(1, sizeof(struct node_state_opt));
4291 err(1, "state_opt_item: calloc");
4292 $$->type = PF_STATE_OPT_TIMEOUT;
4293 $$->data.timeout.number = pf_timeouts[i].timeout;
4294 $$->data.timeout.seconds = $2;
4300 label : LABEL STRING {
4305 etherqname : QUEUE STRING {
4308 | QUEUE '(' STRING ')' {
4313 qname : QUEUE STRING {
4317 | QUEUE '(' STRING ')' {
4321 | QUEUE '(' STRING comma STRING ')' {
4327 no : /* empty */ { $$ = 0; }
4331 portstar : numberstring {
4332 if (parseport($1, &$$, PPORT_RANGE|PPORT_STAR) == -1) {
4340 redirspec : host { $$ = $1; }
4341 | '{' optnl redir_host_list '}' { $$ = $3; }
4344 redir_host_list : host optnl { $$ = $1; }
4345 | redir_host_list comma host optnl {
4346 $1->tail->next = $3;
4347 $1->tail = $3->tail;
4352 redirpool : /* empty */ { $$ = NULL; }
4354 $$ = calloc(1, sizeof(struct redirection));
4356 err(1, "redirection: calloc");
4358 $$->rport.a = $$->rport.b = $$->rport.t = 0;
4360 | ARROW redirspec PORT portstar {
4361 $$ = calloc(1, sizeof(struct redirection));
4363 err(1, "redirection: calloc");
4369 hashkey : /* empty */
4371 $$ = calloc(1, sizeof(struct pf_poolhashkey));
4373 err(1, "hashkey: calloc");
4374 $$->key32[0] = arc4random();
4375 $$->key32[1] = arc4random();
4376 $$->key32[2] = arc4random();
4377 $$->key32[3] = arc4random();
4381 if (!strncmp($1, "0x", 2)) {
4382 if (strlen($1) != 34) {
4384 yyerror("hex key must be 128 bits "
4385 "(32 hex digits) long");
4388 $$ = calloc(1, sizeof(struct pf_poolhashkey));
4390 err(1, "hashkey: calloc");
4392 if (sscanf($1, "0x%8x%8x%8x%8x",
4393 &$$->key32[0], &$$->key32[1],
4394 &$$->key32[2], &$$->key32[3]) != 4) {
4397 yyerror("invalid hex key");
4403 $$ = calloc(1, sizeof(struct pf_poolhashkey));
4405 err(1, "hashkey: calloc");
4407 MD5Update(&context, (unsigned char *)$1,
4409 MD5Final((unsigned char *)$$, &context);
4410 HTONL($$->key32[0]);
4411 HTONL($$->key32[1]);
4412 HTONL($$->key32[2]);
4413 HTONL($$->key32[3]);
4419 pool_opts : { bzero(&pool_opts, sizeof pool_opts); }
4423 bzero(&pool_opts, sizeof pool_opts);
4428 pool_opts_l : pool_opts_l pool_opt
4432 pool_opt : BITMASK {
4433 if (pool_opts.type) {
4434 yyerror("pool type cannot be redefined");
4437 pool_opts.type = PF_POOL_BITMASK;
4440 if (pool_opts.type) {
4441 yyerror("pool type cannot be redefined");
4444 pool_opts.type = PF_POOL_RANDOM;
4446 | SOURCEHASH hashkey {
4447 if (pool_opts.type) {
4448 yyerror("pool type cannot be redefined");
4451 pool_opts.type = PF_POOL_SRCHASH;
4455 if (pool_opts.type) {
4456 yyerror("pool type cannot be redefined");
4459 pool_opts.type = PF_POOL_ROUNDROBIN;
4462 if (pool_opts.staticport) {
4463 yyerror("static-port cannot be redefined");
4466 pool_opts.staticport = 1;
4469 if (filter_opts.marker & POM_STICKYADDRESS) {
4470 yyerror("sticky-address cannot be redefined");
4473 pool_opts.marker |= POM_STICKYADDRESS;
4474 pool_opts.opts |= PF_POOL_STICKYADDR;
4476 | MAPEPORTSET number '/' number '/' number {
4477 if (pool_opts.mape.offset) {
4478 yyerror("map-e-portset cannot be redefined");
4481 if (pool_opts.type) {
4482 yyerror("map-e-portset cannot be used with "
4486 if ($2 <= 0 || $2 >= 16) {
4487 yyerror("MAP-E PSID offset must be 1-15");
4490 if ($4 < 0 || $4 >= 16 || $2 + $4 > 16) {
4491 yyerror("Invalid MAP-E PSID length");
4493 } else if ($4 == 0) {
4494 yyerror("PSID Length = 0: this means"
4495 " you do not need MAP-E");
4498 if ($6 < 0 || $6 > 65535) {
4499 yyerror("Invalid MAP-E PSID");
4502 pool_opts.mape.offset = $2;
4503 pool_opts.mape.psidlen = $4;
4504 pool_opts.mape.psid = $6;
4508 redirection : /* empty */ { $$ = NULL; }
4510 $$ = calloc(1, sizeof(struct redirection));
4512 err(1, "redirection: calloc");
4514 $$->rport.a = $$->rport.b = $$->rport.t = 0;
4516 | ARROW host PORT portstar {
4517 $$ = calloc(1, sizeof(struct redirection));
4519 err(1, "redirection: calloc");
4525 natpasslog : /* empty */ { $$.b1 = $$.b2 = 0; $$.w2 = 0; }
4526 | PASS { $$.b1 = 1; $$.b2 = 0; $$.w2 = 0; }
4527 | PASS log { $$.b1 = 1; $$.b2 = $2.log; $$.w2 = $2.logif; }
4528 | log { $$.b1 = 0; $$.b2 = $1.log; $$.w2 = $1.logif; }
4531 nataction : no NAT natpasslog {
4533 yyerror("\"pass\" not valid with \"no\"");
4544 | no RDR natpasslog {
4546 yyerror("\"pass\" not valid with \"no\"");
4559 natrule : nataction interface af proto fromto tag tagged rtable
4562 struct pfctl_rule r;
4564 if (check_rulestate(PFCTL_STATE_NAT))
4567 memset(&r, 0, sizeof(r));
4576 if ($5.src.host && $5.src.host->af &&
4577 !$5.src.host->ifindex)
4578 r.af = $5.src.host->af;
4579 else if ($5.dst.host && $5.dst.host->af &&
4580 !$5.dst.host->ifindex)
4581 r.af = $5.dst.host->af;
4585 if (strlcpy(r.tagname, $6, PF_TAG_NAME_SIZE) >=
4587 yyerror("tag too long, max %u chars",
4588 PF_TAG_NAME_SIZE - 1);
4593 if (strlcpy(r.match_tagname, $7.name,
4594 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4595 yyerror("tag too long, max %u chars",
4596 PF_TAG_NAME_SIZE - 1);
4599 r.match_tag_not = $7.neg;
4602 if (r.action == PF_NONAT || r.action == PF_NORDR) {
4604 yyerror("translation rule with 'no' "
4605 "does not need '->'");
4609 if ($9 == NULL || $9->host == NULL) {
4610 yyerror("translation rule requires '-> "
4614 if (!r.af && ! $9->host->ifindex)
4615 r.af = $9->host->af;
4617 remove_invalid_hosts(&$9->host, &r.af);
4618 if (invalid_redirect($9->host, r.af))
4620 if (check_netmask($9->host, r.af))
4623 r.rpool.proxy_port[0] = ntohs($9->rport.a);
4627 if (!$9->rport.b && $9->rport.t &&
4628 $5.dst.port != NULL) {
4629 r.rpool.proxy_port[1] =
4630 ntohs($9->rport.a) +
4632 $5.dst.port->port[1]) -
4634 $5.dst.port->port[0]));
4636 r.rpool.proxy_port[1] =
4640 r.rpool.proxy_port[1] =
4642 if (!r.rpool.proxy_port[0] &&
4643 !r.rpool.proxy_port[1]) {
4644 r.rpool.proxy_port[0] =
4645 PF_NAT_PROXY_PORT_LOW;
4646 r.rpool.proxy_port[1] =
4647 PF_NAT_PROXY_PORT_HIGH;
4648 } else if (!r.rpool.proxy_port[1])
4649 r.rpool.proxy_port[1] =
4650 r.rpool.proxy_port[0];
4656 r.rpool.opts = $10.type;
4657 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
4658 PF_POOL_NONE && ($9->host->next != NULL ||
4659 $9->host->addr.type == PF_ADDR_TABLE ||
4660 DYNIF_MULTIADDR($9->host->addr)))
4661 r.rpool.opts = PF_POOL_ROUNDROBIN;
4662 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4663 PF_POOL_ROUNDROBIN &&
4664 disallow_table($9->host, "tables are only "
4665 "supported in round-robin redirection "
4668 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4669 PF_POOL_ROUNDROBIN &&
4670 disallow_alias($9->host, "interface (%s) "
4671 "is only supported in round-robin "
4672 "redirection pools"))
4674 if ($9->host->next != NULL) {
4675 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4676 PF_POOL_ROUNDROBIN) {
4677 yyerror("only round-robin "
4678 "valid for multiple "
4679 "redirection addresses");
4685 if ($10.key != NULL)
4686 memcpy(&r.rpool.key, $10.key,
4687 sizeof(struct pf_poolhashkey));
4690 r.rpool.opts |= $10.opts;
4692 if ($10.staticport) {
4693 if (r.action != PF_NAT) {
4694 yyerror("the 'static-port' option is "
4695 "only valid with nat rules");
4698 if (r.rpool.proxy_port[0] !=
4699 PF_NAT_PROXY_PORT_LOW &&
4700 r.rpool.proxy_port[1] !=
4701 PF_NAT_PROXY_PORT_HIGH) {
4702 yyerror("the 'static-port' option can't"
4703 " be used when specifying a port"
4707 r.rpool.proxy_port[0] = 0;
4708 r.rpool.proxy_port[1] = 0;
4711 if ($10.mape.offset) {
4712 if (r.action != PF_NAT) {
4713 yyerror("the 'map-e-portset' option is"
4714 " only valid with nat rules");
4717 if ($10.staticport) {
4718 yyerror("the 'map-e-portset' option"
4719 " can't be used 'static-port'");
4722 if (r.rpool.proxy_port[0] !=
4723 PF_NAT_PROXY_PORT_LOW &&
4724 r.rpool.proxy_port[1] !=
4725 PF_NAT_PROXY_PORT_HIGH) {
4726 yyerror("the 'map-e-portset' option"
4727 " can't be used when specifying"
4731 r.rpool.mape = $10.mape;
4734 expand_rule(&r, $2, $9 == NULL ? NULL : $9->host, $4,
4735 $5.src_os, $5.src.host, $5.src.port, $5.dst.host,
4736 $5.dst.port, 0, 0, 0, "");
4741 binatrule : no BINAT natpasslog interface af proto FROM ipspec toipspec tag
4742 tagged rtable redirection
4744 struct pfctl_rule binat;
4745 struct pf_pooladdr *pa;
4747 if (check_rulestate(PFCTL_STATE_NAT))
4749 if (disallow_urpf_failed($9, "\"urpf-failed\" is not "
4750 "permitted as a binat destination"))
4753 memset(&binat, 0, sizeof(binat));
4756 yyerror("\"pass\" not valid with \"no\"");
4760 binat.action = PF_NOBINAT;
4762 binat.action = PF_BINAT;
4763 binat.natpass = $3.b1;
4765 binat.logif = $3.w2;
4767 if (!binat.af && $8 != NULL && $8->af)
4769 if (!binat.af && $9 != NULL && $9->af)
4772 if (!binat.af && $13 != NULL && $13->host)
4773 binat.af = $13->host->af;
4775 yyerror("address family (inet/inet6) "
4781 memcpy(binat.ifname, $4->ifname,
4782 sizeof(binat.ifname));
4783 binat.ifnot = $4->not;
4788 if (strlcpy(binat.tagname, $10,
4789 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4790 yyerror("tag too long, max %u chars",
4791 PF_TAG_NAME_SIZE - 1);
4795 if (strlcpy(binat.match_tagname, $11.name,
4796 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4797 yyerror("tag too long, max %u chars",
4798 PF_TAG_NAME_SIZE - 1);
4801 binat.match_tag_not = $11.neg;
4802 binat.rtableid = $12;
4805 binat.proto = $6->proto;
4809 if ($8 != NULL && disallow_table($8, "invalid use of "
4810 "table <%s> as the source address of a binat rule"))
4812 if ($8 != NULL && disallow_alias($8, "invalid use of "
4813 "interface (%s) as the source address of a binat "
4816 if ($13 != NULL && $13->host != NULL && disallow_table(
4817 $13->host, "invalid use of table <%s> as the "
4818 "redirect address of a binat rule"))
4820 if ($13 != NULL && $13->host != NULL && disallow_alias(
4821 $13->host, "invalid use of interface (%s) as the "
4822 "redirect address of a binat rule"))
4827 yyerror("multiple binat ip addresses");
4830 if ($8->addr.type == PF_ADDR_DYNIFTL)
4832 if ($8->af != binat.af) {
4833 yyerror("binat ip versions must match");
4836 if (check_netmask($8, binat.af))
4838 memcpy(&binat.src.addr, &$8->addr,
4839 sizeof(binat.src.addr));
4844 yyerror("multiple binat ip addresses");
4847 if ($9->af != binat.af && $9->af) {
4848 yyerror("binat ip versions must match");
4851 if (check_netmask($9, binat.af))
4853 memcpy(&binat.dst.addr, &$9->addr,
4854 sizeof(binat.dst.addr));
4855 binat.dst.neg = $9->not;
4859 if (binat.action == PF_NOBINAT) {
4861 yyerror("'no binat' rule does not need"
4866 if ($13 == NULL || $13->host == NULL) {
4867 yyerror("'binat' rule requires"
4872 remove_invalid_hosts(&$13->host, &binat.af);
4873 if (invalid_redirect($13->host, binat.af))
4875 if ($13->host->next != NULL) {
4876 yyerror("binat rule must redirect to "
4877 "a single address");
4880 if (check_netmask($13->host, binat.af))
4883 if (!PF_AZERO(&binat.src.addr.v.a.mask,
4885 !PF_AEQ(&binat.src.addr.v.a.mask,
4886 &$13->host->addr.v.a.mask, binat.af)) {
4887 yyerror("'binat' source mask and "
4888 "redirect mask must be the same");
4892 TAILQ_INIT(&binat.rpool.list);
4893 pa = calloc(1, sizeof(struct pf_pooladdr));
4895 err(1, "binat: calloc");
4896 pa->addr = $13->host->addr;
4898 TAILQ_INSERT_TAIL(&binat.rpool.list,
4904 pfctl_append_rule(pf, &binat, "");
4908 tag : /* empty */ { $$ = NULL; }
4909 | TAG STRING { $$ = $2; }
4912 tagged : /* empty */ { $$.neg = 0; $$.name = NULL; }
4913 | not TAGGED string { $$.neg = $1; $$.name = $3; }
4916 rtable : /* empty */ { $$ = -1; }
4918 if ($2 < 0 || $2 > rt_tableid_max()) {
4919 yyerror("invalid rtable id");
4926 route_host : STRING {
4927 $$ = calloc(1, sizeof(struct node_host));
4929 err(1, "route_host: calloc");
4930 if (strlen($1) >= IFNAMSIZ) {
4931 yyerror("interface name too long");
4934 $$->ifname = strdup($1);
4935 set_ipmask($$, 128);
4939 | '(' STRING host ')' {
4940 struct node_host *n;
4943 for (n = $3; n != NULL; n = n->next) {
4944 if (strlen($2) >= IFNAMSIZ) {
4945 yyerror("interface name too long");
4948 n->ifname = strdup($2);
4953 route_host_list : route_host optnl { $$ = $1; }
4954 | route_host_list comma route_host optnl {
4957 if ($1->af != $3->af) {
4958 yyerror("all pool addresses must be in the "
4959 "same address family");
4962 $1->tail->next = $3;
4963 $1->tail = $3->tail;
4968 routespec : route_host { $$ = $1; }
4969 | '{' optnl route_host_list '}' { $$ = $3; }
4972 route : /* empty */ {
4978 /* backwards-compat */
4983 | ROUTETO routespec pool_opts {
4986 $$.pool_opts = $3.type | $3.opts;
4990 | REPLYTO routespec pool_opts {
4993 $$.pool_opts = $3.type | $3.opts;
4997 | DUPTO routespec pool_opts {
5000 $$.pool_opts = $3.type | $3.opts;
5006 timeout_spec : STRING NUMBER
5008 if (check_rulestate(PFCTL_STATE_OPTION)) {
5012 if ($2 < 0 || $2 > UINT_MAX) {
5013 yyerror("only positive values permitted");
5016 if (pfctl_set_timeout(pf, $1, $2, 0) != 0) {
5017 yyerror("unknown timeout %s", $1);
5024 if (check_rulestate(PFCTL_STATE_OPTION))
5026 if ($2 < 0 || $2 > UINT_MAX) {
5027 yyerror("only positive values permitted");
5030 if (pfctl_set_timeout(pf, "interval", $2, 0) != 0)
5035 timeout_list : timeout_list comma timeout_spec optnl
5036 | timeout_spec optnl
5039 limit_spec : STRING NUMBER
5041 if (check_rulestate(PFCTL_STATE_OPTION)) {
5045 if ($2 < 0 || $2 > UINT_MAX) {
5046 yyerror("only positive values permitted");
5049 if (pfctl_set_limit(pf, $1, $2) != 0) {
5050 yyerror("unable to set limit %s %u", $1, $2);
5058 limit_list : limit_list comma limit_spec optnl
5066 yesno : NO { $$ = 0; }
5068 if (!strcmp($1, "yes"))
5071 yyerror("invalid value '%s', expected 'yes' "
5080 unaryop : '=' { $$ = PF_OP_EQ; }
5081 | '!' '=' { $$ = PF_OP_NE; }
5082 | '<' '=' { $$ = PF_OP_LE; }
5083 | '<' { $$ = PF_OP_LT; }
5084 | '>' '=' { $$ = PF_OP_GE; }
5085 | '>' { $$ = PF_OP_GT; }
5091 yyerror(const char *fmt, ...)
5097 fprintf(stderr, "%s:%d: ", file->name, yylval.lineno);
5098 vfprintf(stderr, fmt, ap);
5099 fprintf(stderr, "\n");
5105 disallow_table(struct node_host *h, const char *fmt)
5107 for (; h != NULL; h = h->next)
5108 if (h->addr.type == PF_ADDR_TABLE) {
5109 yyerror(fmt, h->addr.v.tblname);
5116 disallow_urpf_failed(struct node_host *h, const char *fmt)
5118 for (; h != NULL; h = h->next)
5119 if (h->addr.type == PF_ADDR_URPFFAILED) {
5127 disallow_alias(struct node_host *h, const char *fmt)
5129 for (; h != NULL; h = h->next)
5130 if (DYNIF_MULTIADDR(h->addr)) {
5131 yyerror(fmt, h->addr.v.tblname);
5138 rule_consistent(struct pfctl_rule *r, int anchor_call)
5142 switch (r->action) {
5147 problems = filter_consistent(r, anchor_call);
5151 problems = nat_consistent(r);
5155 problems = rdr_consistent(r);
5166 filter_consistent(struct pfctl_rule *r, int anchor_call)
5170 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP &&
5171 (r->src.port_op || r->dst.port_op)) {
5172 yyerror("port only applies to tcp/udp");
5175 if (r->proto != IPPROTO_ICMP && r->proto != IPPROTO_ICMPV6 &&
5176 (r->type || r->code)) {
5177 yyerror("icmp-type/code only applies to icmp");
5180 if (!r->af && (r->type || r->code)) {
5181 yyerror("must indicate address family with icmp-type/code");
5184 if (r->overload_tblname[0] &&
5185 r->max_src_conn == 0 && r->max_src_conn_rate.seconds == 0) {
5186 yyerror("'overload' requires 'max-src-conn' "
5187 "or 'max-src-conn-rate'");
5190 if ((r->proto == IPPROTO_ICMP && r->af == AF_INET6) ||
5191 (r->proto == IPPROTO_ICMPV6 && r->af == AF_INET)) {
5192 yyerror("proto %s doesn't match address family %s",
5193 r->proto == IPPROTO_ICMP ? "icmp" : "icmp6",
5194 r->af == AF_INET ? "inet" : "inet6");
5197 if (r->allow_opts && r->action != PF_PASS) {
5198 yyerror("allow-opts can only be specified for pass rules");
5201 if (r->rule_flag & PFRULE_FRAGMENT && (r->src.port_op ||
5202 r->dst.port_op || r->flagset || r->type || r->code)) {
5203 yyerror("fragments can be filtered only on IP header fields");
5206 if (r->rule_flag & PFRULE_RETURNRST && r->proto != IPPROTO_TCP) {
5207 yyerror("return-rst can only be applied to TCP rules");
5210 if (r->max_src_nodes && !(r->rule_flag & PFRULE_RULESRCTRACK)) {
5211 yyerror("max-src-nodes requires 'source-track rule'");
5214 if (r->action == PF_DROP && r->keep_state) {
5215 yyerror("keep state on block rules doesn't make sense");
5218 if (r->rule_flag & PFRULE_STATESLOPPY &&
5219 (r->keep_state == PF_STATE_MODULATE ||
5220 r->keep_state == PF_STATE_SYNPROXY)) {
5221 yyerror("sloppy state matching cannot be used with "
5222 "synproxy state or modulate state");
5229 nat_consistent(struct pfctl_rule *r)
5231 return (0); /* yeah! */
5235 rdr_consistent(struct pfctl_rule *r)
5239 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP) {
5240 if (r->src.port_op) {
5241 yyerror("src port only applies to tcp/udp");
5244 if (r->dst.port_op) {
5245 yyerror("dst port only applies to tcp/udp");
5248 if (r->rpool.proxy_port[0]) {
5249 yyerror("rpool port only applies to tcp/udp");
5253 if (r->dst.port_op &&
5254 r->dst.port_op != PF_OP_EQ && r->dst.port_op != PF_OP_RRG) {
5255 yyerror("invalid port operator for rdr destination port");
5262 process_tabledef(char *name, struct table_opts *opts)
5264 struct pfr_buffer ab;
5265 struct node_tinit *ti;
5266 unsigned long maxcount;
5267 size_t s = sizeof(maxcount);
5269 bzero(&ab, sizeof(ab));
5270 ab.pfrb_type = PFRB_ADDRS;
5271 SIMPLEQ_FOREACH(ti, &opts->init_nodes, entries) {
5273 if (pfr_buf_load(&ab, ti->file, 0, append_addr)) {
5275 yyerror("cannot load \"%s\": %s",
5276 ti->file, strerror(errno));
5278 yyerror("file \"%s\" contains bad data",
5283 if (append_addr_host(&ab, ti->host, 0, 0)) {
5284 yyerror("cannot create address buffer: %s",
5289 if (pf->opts & PF_OPT_VERBOSE)
5290 print_tabledef(name, opts->flags, opts->init_addr,
5292 if (!(pf->opts & PF_OPT_NOACTION) &&
5293 pfctl_define_table(name, opts->flags, opts->init_addr,
5294 pf->anchor->name, &ab, pf->anchor->ruleset.tticket)) {
5296 if (sysctlbyname("net.pf.request_maxcount", &maxcount, &s,
5300 if (ab.pfrb_size > maxcount)
5301 yyerror("cannot define table %s: too many elements.\n"
5302 "Consider increasing net.pf.request_maxcount.",
5305 yyerror("cannot define table %s: %s", name,
5306 pfr_strerror(errno));
5323 /* macro gore, but you should've seen the prior indentation nightmare... */
5325 #define FREE_LIST(T,r) \
5328 while (node != NULL) { \
5330 node = node->next; \
5335 #define LOOP_THROUGH(T,n,r,C) \
5339 r = calloc(1, sizeof(T)); \
5341 err(1, "LOOP: calloc"); \
5345 while (n != NULL) { \
5354 expand_label_str(char *label, size_t len, const char *srch, const char *repl)
5359 if ((tmp = calloc(1, len)) == NULL)
5360 err(1, "expand_label_str: calloc");
5362 while ((q = strstr(p, srch)) != NULL) {
5364 if ((strlcat(tmp, p, len) >= len) ||
5365 (strlcat(tmp, repl, len) >= len))
5366 errx(1, "expand_label: label too long");
5370 if (strlcat(tmp, p, len) >= len)
5371 errx(1, "expand_label: label too long");
5372 strlcpy(label, tmp, len); /* always fits */
5377 expand_label_if(const char *name, char *label, size_t len, const char *ifname)
5379 if (strstr(label, name) != NULL) {
5381 expand_label_str(label, len, name, "any");
5383 expand_label_str(label, len, name, ifname);
5388 expand_label_addr(const char *name, char *label, size_t len, sa_family_t af,
5389 struct pf_rule_addr *addr)
5391 char tmp[64], tmp_not[66];
5393 if (strstr(label, name) != NULL) {
5394 switch (addr->addr.type) {
5395 case PF_ADDR_DYNIFTL:
5396 snprintf(tmp, sizeof(tmp), "(%s)", addr->addr.v.ifname);
5399 snprintf(tmp, sizeof(tmp), "<%s>", addr->addr.v.tblname);
5401 case PF_ADDR_NOROUTE:
5402 snprintf(tmp, sizeof(tmp), "no-route");
5404 case PF_ADDR_URPFFAILED:
5405 snprintf(tmp, sizeof(tmp), "urpf-failed");
5407 case PF_ADDR_ADDRMASK:
5408 if (!af || (PF_AZERO(&addr->addr.v.a.addr, af) &&
5409 PF_AZERO(&addr->addr.v.a.mask, af)))
5410 snprintf(tmp, sizeof(tmp), "any");
5415 if (inet_ntop(af, &addr->addr.v.a.addr, a,
5417 snprintf(tmp, sizeof(tmp), "?");
5419 bits = unmask(&addr->addr.v.a.mask, af);
5420 if ((af == AF_INET && bits < 32) ||
5421 (af == AF_INET6 && bits < 128))
5422 snprintf(tmp, sizeof(tmp),
5425 snprintf(tmp, sizeof(tmp),
5431 snprintf(tmp, sizeof(tmp), "?");
5436 snprintf(tmp_not, sizeof(tmp_not), "! %s", tmp);
5437 expand_label_str(label, len, name, tmp_not);
5439 expand_label_str(label, len, name, tmp);
5444 expand_label_port(const char *name, char *label, size_t len,
5445 struct pf_rule_addr *addr)
5447 char a1[6], a2[6], op[13] = "";
5449 if (strstr(label, name) != NULL) {
5450 snprintf(a1, sizeof(a1), "%u", ntohs(addr->port[0]));
5451 snprintf(a2, sizeof(a2), "%u", ntohs(addr->port[1]));
5454 else if (addr->port_op == PF_OP_IRG)
5455 snprintf(op, sizeof(op), "%s><%s", a1, a2);
5456 else if (addr->port_op == PF_OP_XRG)
5457 snprintf(op, sizeof(op), "%s<>%s", a1, a2);
5458 else if (addr->port_op == PF_OP_EQ)
5459 snprintf(op, sizeof(op), "%s", a1);
5460 else if (addr->port_op == PF_OP_NE)
5461 snprintf(op, sizeof(op), "!=%s", a1);
5462 else if (addr->port_op == PF_OP_LT)
5463 snprintf(op, sizeof(op), "<%s", a1);
5464 else if (addr->port_op == PF_OP_LE)
5465 snprintf(op, sizeof(op), "<=%s", a1);
5466 else if (addr->port_op == PF_OP_GT)
5467 snprintf(op, sizeof(op), ">%s", a1);
5468 else if (addr->port_op == PF_OP_GE)
5469 snprintf(op, sizeof(op), ">=%s", a1);
5470 expand_label_str(label, len, name, op);
5475 expand_label_proto(const char *name, char *label, size_t len, u_int8_t proto)
5477 const char *protoname;
5480 if (strstr(label, name) != NULL) {
5481 protoname = pfctl_proto2name(proto);
5482 if (protoname != NULL)
5483 expand_label_str(label, len, name, protoname);
5485 snprintf(n, sizeof(n), "%u", proto);
5486 expand_label_str(label, len, name, n);
5492 expand_label_nr(const char *name, char *label, size_t len,
5493 struct pfctl_rule *r)
5497 if (strstr(label, name) != NULL) {
5498 snprintf(n, sizeof(n), "%u", r->nr);
5499 expand_label_str(label, len, name, n);
5504 expand_label(char *label, size_t len, struct pfctl_rule *r)
5506 expand_label_if("$if", label, len, r->ifname);
5507 expand_label_addr("$srcaddr", label, len, r->af, &r->src);
5508 expand_label_addr("$dstaddr", label, len, r->af, &r->dst);
5509 expand_label_port("$srcport", label, len, &r->src);
5510 expand_label_port("$dstport", label, len, &r->dst);
5511 expand_label_proto("$proto", label, len, r->proto);
5512 expand_label_nr("$nr", label, len, r);
5516 expand_altq(struct pf_altq *a, struct node_if *interfaces,
5517 struct node_queue *nqueues, struct node_queue_bw bwspec,
5518 struct node_queue_opt *opts)
5520 struct pf_altq pa, pb;
5521 char qname[PF_QNAME_SIZE];
5522 struct node_queue *n;
5523 struct node_queue_bw bw;
5526 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
5527 FREE_LIST(struct node_if, interfaces);
5529 FREE_LIST(struct node_queue, nqueues);
5533 LOOP_THROUGH(struct node_if, interface, interfaces,
5534 memcpy(&pa, a, sizeof(struct pf_altq));
5535 if (strlcpy(pa.ifname, interface->ifname,
5536 sizeof(pa.ifname)) >= sizeof(pa.ifname))
5537 errx(1, "expand_altq: strlcpy");
5539 if (interface->not) {
5540 yyerror("altq on ! <interface> is not supported");
5543 if (eval_pfaltq(pf, &pa, &bwspec, opts))
5546 if (pfctl_add_altq(pf, &pa))
5549 if (pf->opts & PF_OPT_VERBOSE) {
5550 print_altq(&pf->paltq->altq, 0,
5552 if (nqueues && nqueues->tail) {
5554 LOOP_THROUGH(struct node_queue, queue,
5564 if (pa.scheduler == ALTQT_CBQ ||
5565 pa.scheduler == ALTQT_HFSC ||
5566 pa.scheduler == ALTQT_FAIRQ) {
5567 /* now create a root queue */
5568 memset(&pb, 0, sizeof(struct pf_altq));
5569 if (strlcpy(qname, "root_", sizeof(qname)) >=
5571 errx(1, "expand_altq: strlcpy");
5572 if (strlcat(qname, interface->ifname,
5573 sizeof(qname)) >= sizeof(qname))
5574 errx(1, "expand_altq: strlcat");
5575 if (strlcpy(pb.qname, qname,
5576 sizeof(pb.qname)) >= sizeof(pb.qname))
5577 errx(1, "expand_altq: strlcpy");
5578 if (strlcpy(pb.ifname, interface->ifname,
5579 sizeof(pb.ifname)) >= sizeof(pb.ifname))
5580 errx(1, "expand_altq: strlcpy");
5581 pb.qlimit = pa.qlimit;
5582 pb.scheduler = pa.scheduler;
5583 bw.bw_absolute = pa.ifbandwidth;
5585 if (eval_pfqueue(pf, &pb, &bw, opts))
5588 if (pfctl_add_altq(pf, &pb))
5592 LOOP_THROUGH(struct node_queue, queue, nqueues,
5593 n = calloc(1, sizeof(struct node_queue));
5595 err(1, "expand_altq: calloc");
5596 if (pa.scheduler == ALTQT_CBQ ||
5597 pa.scheduler == ALTQT_HFSC ||
5598 pa.scheduler == ALTQT_FAIRQ)
5599 if (strlcpy(n->parent, qname,
5600 sizeof(n->parent)) >=
5602 errx(1, "expand_altq: strlcpy");
5603 if (strlcpy(n->queue, queue->queue,
5604 sizeof(n->queue)) >= sizeof(n->queue))
5605 errx(1, "expand_altq: strlcpy");
5606 if (strlcpy(n->ifname, interface->ifname,
5607 sizeof(n->ifname)) >= sizeof(n->ifname))
5608 errx(1, "expand_altq: strlcpy");
5609 n->scheduler = pa.scheduler;
5615 queues->tail->next = n;
5621 FREE_LIST(struct node_if, interfaces);
5623 FREE_LIST(struct node_queue, nqueues);
5629 expand_queue(struct pf_altq *a, struct node_if *interfaces,
5630 struct node_queue *nqueues, struct node_queue_bw bwspec,
5631 struct node_queue_opt *opts)
5633 struct node_queue *n, *nq;
5638 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
5639 FREE_LIST(struct node_queue, nqueues);
5643 if (queues == NULL) {
5644 yyerror("queue %s has no parent", a->qname);
5645 FREE_LIST(struct node_queue, nqueues);
5649 LOOP_THROUGH(struct node_if, interface, interfaces,
5650 LOOP_THROUGH(struct node_queue, tqueue, queues,
5651 if (!strncmp(a->qname, tqueue->queue, PF_QNAME_SIZE) &&
5652 (interface->ifname[0] == 0 ||
5653 (!interface->not && !strncmp(interface->ifname,
5654 tqueue->ifname, IFNAMSIZ)) ||
5655 (interface->not && strncmp(interface->ifname,
5656 tqueue->ifname, IFNAMSIZ)))) {
5657 /* found ourself in queues */
5660 memcpy(&pa, a, sizeof(struct pf_altq));
5662 if (pa.scheduler != ALTQT_NONE &&
5663 pa.scheduler != tqueue->scheduler) {
5664 yyerror("exactly one scheduler type "
5665 "per interface allowed");
5668 pa.scheduler = tqueue->scheduler;
5670 /* scheduler dependent error checking */
5671 switch (pa.scheduler) {
5673 if (nqueues != NULL) {
5674 yyerror("priq queues cannot "
5675 "have child queues");
5678 if (bwspec.bw_absolute > 0 ||
5679 bwspec.bw_percent < 100) {
5680 yyerror("priq doesn't take "
5689 if (strlcpy(pa.ifname, tqueue->ifname,
5690 sizeof(pa.ifname)) >= sizeof(pa.ifname))
5691 errx(1, "expand_queue: strlcpy");
5692 if (strlcpy(pa.parent, tqueue->parent,
5693 sizeof(pa.parent)) >= sizeof(pa.parent))
5694 errx(1, "expand_queue: strlcpy");
5696 if (eval_pfqueue(pf, &pa, &bwspec, opts))
5699 if (pfctl_add_altq(pf, &pa))
5702 for (nq = nqueues; nq != NULL; nq = nq->next) {
5703 if (!strcmp(a->qname, nq->queue)) {
5704 yyerror("queue cannot have "
5710 sizeof(struct node_queue));
5712 err(1, "expand_queue: calloc");
5713 if (strlcpy(n->parent, a->qname,
5714 sizeof(n->parent)) >=
5716 errx(1, "expand_queue strlcpy");
5717 if (strlcpy(n->queue, nq->queue,
5718 sizeof(n->queue)) >=
5720 errx(1, "expand_queue strlcpy");
5721 if (strlcpy(n->ifname, tqueue->ifname,
5722 sizeof(n->ifname)) >=
5724 errx(1, "expand_queue strlcpy");
5725 n->scheduler = tqueue->scheduler;
5731 queues->tail->next = n;
5735 if ((pf->opts & PF_OPT_VERBOSE) && (
5736 (found == 1 && interface->ifname[0] == 0) ||
5737 (found > 0 && interface->ifname[0] != 0))) {
5738 print_queue(&pf->paltq->altq, 0,
5739 &bwspec, interface->ifname[0] != 0,
5741 if (nqueues && nqueues->tail) {
5743 LOOP_THROUGH(struct node_queue,
5756 FREE_LIST(struct node_queue, nqueues);
5757 FREE_LIST(struct node_if, interfaces);
5760 yyerror("queue %s has no parent", a->qname);
5771 pf_af_to_proto(sa_family_t af)
5774 return (ETHERTYPE_IP);
5776 return (ETHERTYPE_IPV6);
5782 expand_eth_rule(struct pfctl_eth_rule *r,
5783 struct node_if *interfaces, struct node_etherproto *protos,
5784 struct node_mac *srcs, struct node_mac *dsts,
5785 struct node_host *ipsrcs, struct node_host *ipdsts, const char *anchor_call)
5787 char tagname[PF_TAG_NAME_SIZE];
5788 char match_tagname[PF_TAG_NAME_SIZE];
5789 char qname[PF_QNAME_SIZE];
5791 if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname))
5792 errx(1, "expand_eth_rule: tagname");
5793 if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >=
5794 sizeof(match_tagname))
5795 errx(1, "expand_eth_rule: match_tagname");
5796 if (strlcpy(qname, r->qname, sizeof(qname)) >= sizeof(qname))
5797 errx(1, "expand_eth_rule: qname");
5799 LOOP_THROUGH(struct node_if, interface, interfaces,
5800 LOOP_THROUGH(struct node_etherproto, proto, protos,
5801 LOOP_THROUGH(struct node_mac, src, srcs,
5802 LOOP_THROUGH(struct node_mac, dst, dsts,
5803 LOOP_THROUGH(struct node_host, ipsrc, ipsrcs,
5804 LOOP_THROUGH(struct node_host, ipdst, ipdsts,
5805 strlcpy(r->ifname, interface->ifname,
5807 r->ifnot = interface->not;
5808 r->proto = proto->proto;
5809 if (!r->proto && ipsrc->af)
5810 r->proto = pf_af_to_proto(ipsrc->af);
5811 else if (!r->proto && ipdst->af)
5812 r->proto = pf_af_to_proto(ipdst->af);
5813 bcopy(src->mac, r->src.addr, ETHER_ADDR_LEN);
5814 bcopy(src->mask, r->src.mask, ETHER_ADDR_LEN);
5815 r->src.neg = src->neg;
5816 r->src.isset = src->isset;
5817 r->ipsrc.addr = ipsrc->addr;
5818 r->ipsrc.neg = ipsrc->not;
5819 r->ipdst.addr = ipdst->addr;
5820 r->ipdst.neg = ipdst->not;
5821 bcopy(dst->mac, r->dst.addr, ETHER_ADDR_LEN);
5822 bcopy(dst->mask, r->dst.mask, ETHER_ADDR_LEN);
5823 r->dst.neg = dst->neg;
5824 r->dst.isset = dst->isset;
5825 r->nr = pf->eastack[pf->asd]->match++;
5827 if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >=
5829 errx(1, "expand_eth_rule: r->tagname");
5830 if (strlcpy(r->match_tagname, match_tagname,
5831 sizeof(r->match_tagname)) >= sizeof(r->match_tagname))
5832 errx(1, "expand_eth_rule: r->match_tagname");
5833 if (strlcpy(r->qname, qname, sizeof(r->qname)) >= sizeof(r->qname))
5834 errx(1, "expand_eth_rule: r->qname");
5836 pfctl_append_eth_rule(pf, r, anchor_call);
5839 FREE_LIST(struct node_if, interfaces);
5840 FREE_LIST(struct node_etherproto, protos);
5841 FREE_LIST(struct node_mac, srcs);
5842 FREE_LIST(struct node_mac, dsts);
5843 FREE_LIST(struct node_host, ipsrcs);
5844 FREE_LIST(struct node_host, ipdsts);
5848 expand_rule(struct pfctl_rule *r,
5849 struct node_if *interfaces, struct node_host *rpool_hosts,
5850 struct node_proto *protos, struct node_os *src_oses,
5851 struct node_host *src_hosts, struct node_port *src_ports,
5852 struct node_host *dst_hosts, struct node_port *dst_ports,
5853 struct node_uid *uids, struct node_gid *gids, struct node_icmp *icmp_types,
5854 const char *anchor_call)
5856 sa_family_t af = r->af;
5857 int added = 0, error = 0;
5858 char ifname[IF_NAMESIZE];
5859 char label[PF_RULE_MAX_LABEL_COUNT][PF_RULE_LABEL_SIZE];
5860 char tagname[PF_TAG_NAME_SIZE];
5861 char match_tagname[PF_TAG_NAME_SIZE];
5862 struct pf_pooladdr *pa;
5863 struct node_host *h;
5864 u_int8_t flags, flagset, keep_state;
5866 memcpy(label, r->label, sizeof(r->label));
5867 assert(sizeof(r->label) == sizeof(label));
5868 if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname))
5869 errx(1, "expand_rule: strlcpy");
5870 if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >=
5871 sizeof(match_tagname))
5872 errx(1, "expand_rule: strlcpy");
5874 flagset = r->flagset;
5875 keep_state = r->keep_state;
5877 LOOP_THROUGH(struct node_if, interface, interfaces,
5878 LOOP_THROUGH(struct node_proto, proto, protos,
5879 LOOP_THROUGH(struct node_icmp, icmp_type, icmp_types,
5880 LOOP_THROUGH(struct node_host, src_host, src_hosts,
5881 LOOP_THROUGH(struct node_port, src_port, src_ports,
5882 LOOP_THROUGH(struct node_os, src_os, src_oses,
5883 LOOP_THROUGH(struct node_host, dst_host, dst_hosts,
5884 LOOP_THROUGH(struct node_port, dst_port, dst_ports,
5885 LOOP_THROUGH(struct node_uid, uid, uids,
5886 LOOP_THROUGH(struct node_gid, gid, gids,
5889 /* for link-local IPv6 address, interface must match up */
5890 if ((r->af && src_host->af && r->af != src_host->af) ||
5891 (r->af && dst_host->af && r->af != dst_host->af) ||
5892 (src_host->af && dst_host->af &&
5893 src_host->af != dst_host->af) ||
5894 (src_host->ifindex && dst_host->ifindex &&
5895 src_host->ifindex != dst_host->ifindex) ||
5896 (src_host->ifindex && *interface->ifname &&
5897 src_host->ifindex != if_nametoindex(interface->ifname)) ||
5898 (dst_host->ifindex && *interface->ifname &&
5899 dst_host->ifindex != if_nametoindex(interface->ifname)))
5901 if (!r->af && src_host->af)
5902 r->af = src_host->af;
5903 else if (!r->af && dst_host->af)
5904 r->af = dst_host->af;
5906 if (*interface->ifname)
5907 strlcpy(r->ifname, interface->ifname,
5909 else if (if_indextoname(src_host->ifindex, ifname))
5910 strlcpy(r->ifname, ifname, sizeof(r->ifname));
5911 else if (if_indextoname(dst_host->ifindex, ifname))
5912 strlcpy(r->ifname, ifname, sizeof(r->ifname));
5914 memset(r->ifname, '\0', sizeof(r->ifname));
5916 memcpy(r->label, label, sizeof(r->label));
5917 if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >=
5919 errx(1, "expand_rule: strlcpy");
5920 if (strlcpy(r->match_tagname, match_tagname,
5921 sizeof(r->match_tagname)) >= sizeof(r->match_tagname))
5922 errx(1, "expand_rule: strlcpy");
5924 error += check_netmask(src_host, r->af);
5925 error += check_netmask(dst_host, r->af);
5927 r->ifnot = interface->not;
5928 r->proto = proto->proto;
5929 r->src.addr = src_host->addr;
5930 r->src.neg = src_host->not;
5931 r->src.port[0] = src_port->port[0];
5932 r->src.port[1] = src_port->port[1];
5933 r->src.port_op = src_port->op;
5934 r->dst.addr = dst_host->addr;
5935 r->dst.neg = dst_host->not;
5936 r->dst.port[0] = dst_port->port[0];
5937 r->dst.port[1] = dst_port->port[1];
5938 r->dst.port_op = dst_port->op;
5939 r->uid.op = uid->op;
5940 r->uid.uid[0] = uid->uid[0];
5941 r->uid.uid[1] = uid->uid[1];
5942 r->gid.op = gid->op;
5943 r->gid.gid[0] = gid->gid[0];
5944 r->gid.gid[1] = gid->gid[1];
5945 r->type = icmp_type->type;
5946 r->code = icmp_type->code;
5948 if ((keep_state == PF_STATE_MODULATE ||
5949 keep_state == PF_STATE_SYNPROXY) &&
5950 r->proto && r->proto != IPPROTO_TCP)
5951 r->keep_state = PF_STATE_NORMAL;
5953 r->keep_state = keep_state;
5955 if (r->proto && r->proto != IPPROTO_TCP) {
5960 r->flagset = flagset;
5962 if (icmp_type->proto && r->proto != icmp_type->proto) {
5963 yyerror("icmp-type mismatch");
5967 if (src_os && src_os->os) {
5968 r->os_fingerprint = pfctl_get_fingerprint(src_os->os);
5969 if ((pf->opts & PF_OPT_VERBOSE2) &&
5970 r->os_fingerprint == PF_OSFP_NOMATCH)
5972 "warning: unknown '%s' OS fingerprint\n",
5975 r->os_fingerprint = PF_OSFP_ANY;
5978 TAILQ_INIT(&r->rpool.list);
5979 for (h = rpool_hosts; h != NULL; h = h->next) {
5980 pa = calloc(1, sizeof(struct pf_pooladdr));
5982 err(1, "expand_rule: calloc");
5984 if (h->ifname != NULL) {
5985 if (strlcpy(pa->ifname, h->ifname,
5986 sizeof(pa->ifname)) >=
5988 errx(1, "expand_rule: strlcpy");
5991 TAILQ_INSERT_TAIL(&r->rpool.list, pa, entries);
5994 if (rule_consistent(r, anchor_call[0]) < 0 || error)
5995 yyerror("skipping rule due to errors");
5997 r->nr = pf->astack[pf->asd]->match++;
5998 pfctl_append_rule(pf, r, anchor_call);
6004 FREE_LIST(struct node_if, interfaces);
6005 FREE_LIST(struct node_proto, protos);
6006 FREE_LIST(struct node_host, src_hosts);
6007 FREE_LIST(struct node_port, src_ports);
6008 FREE_LIST(struct node_os, src_oses);
6009 FREE_LIST(struct node_host, dst_hosts);
6010 FREE_LIST(struct node_port, dst_ports);
6011 FREE_LIST(struct node_uid, uids);
6012 FREE_LIST(struct node_gid, gids);
6013 FREE_LIST(struct node_icmp, icmp_types);
6014 FREE_LIST(struct node_host, rpool_hosts);
6017 yyerror("rule expands to no valid combination");
6021 expand_skip_interface(struct node_if *interfaces)
6025 if (!interfaces || (!interfaces->next && !interfaces->not &&
6026 !strcmp(interfaces->ifname, "none"))) {
6027 if (pf->opts & PF_OPT_VERBOSE)
6028 printf("set skip on none\n");
6029 errs = pfctl_set_interface_flags(pf, "", PFI_IFLAG_SKIP, 0);
6033 if (pf->opts & PF_OPT_VERBOSE)
6034 printf("set skip on {");
6035 LOOP_THROUGH(struct node_if, interface, interfaces,
6036 if (pf->opts & PF_OPT_VERBOSE)
6037 printf(" %s", interface->ifname);
6038 if (interface->not) {
6039 yyerror("skip on ! <interface> is not supported");
6042 errs += pfctl_set_interface_flags(pf,
6043 interface->ifname, PFI_IFLAG_SKIP, 1);
6045 if (pf->opts & PF_OPT_VERBOSE)
6048 FREE_LIST(struct node_if, interfaces);
6060 check_rulestate(int desired_state)
6062 if (require_order && (rulestate > desired_state)) {
6063 yyerror("Rules must be in order: options, ethernet, "
6064 "normalization, queueing, translation, filtering");
6067 rulestate = desired_state;
6072 kw_cmp(const void *k, const void *e)
6074 return (strcmp(k, ((const struct keywords *)e)->k_name));
6080 /* this has to be sorted always */
6081 static const struct keywords keywords[] = {
6083 { "allow-opts", ALLOWOPTS},
6085 { "anchor", ANCHOR},
6086 { "antispoof", ANTISPOOF},
6088 { "bandwidth", BANDWIDTH},
6090 { "binat-anchor", BINATANCHOR},
6091 { "bitmask", BITMASK},
6093 { "block-policy", BLOCKPOLICY},
6094 { "buckets", BUCKETS},
6098 { "crop", FRAGCROP},
6100 { "divert-reply", DIVERTREPLY},
6101 { "divert-to", DIVERTTO},
6102 { "dnpipe", DNPIPE},
6103 { "dnqueue", DNQUEUE},
6105 { "drop-ovl", FRAGDROP},
6108 { "fail-policy", FAILPOLICY},
6110 { "fastroute", FASTROUTE},
6111 { "file", FILENAME},
6112 { "fingerprints", FINGERPRINTS},
6114 { "floating", FLOATING},
6117 { "fragment", FRAGMENT},
6119 { "global", GLOBAL},
6123 { "hostid", HOSTID},
6124 { "icmp-type", ICMPTYPE},
6125 { "icmp6-type", ICMP6TYPE},
6126 { "if-bound", IFBOUND},
6128 { "include", INCLUDE},
6131 { "interval", INTERVAL},
6133 { "keepcounters", KEEPCOUNTERS},
6137 { "linkshare", LINKSHARE},
6140 { "loginterface", LOGINTERFACE},
6141 { "map-e-portset", MAPEPORTSET},
6144 { "max-mss", MAXMSS},
6145 { "max-src-conn", MAXSRCCONN},
6146 { "max-src-conn-rate", MAXSRCCONNRATE},
6147 { "max-src-nodes", MAXSRCNODES},
6148 { "max-src-states", MAXSRCSTATES},
6149 { "min-ttl", MINTTL},
6150 { "modulate", MODULATE},
6152 { "nat-anchor", NATANCHOR},
6155 { "no-route", NOROUTE},
6156 { "no-sync", NOSYNC},
6158 { "optimization", OPTIMIZATION},
6161 { "overload", OVERLOAD},
6165 { "priority", PRIORITY},
6167 { "probability", PROBABILITY},
6169 { "qlimit", QLIMIT},
6172 { "random", RANDOM},
6173 { "random-id", RANDOMID},
6175 { "rdr-anchor", RDRANCHOR},
6176 { "realtime", REALTIME},
6177 { "reassemble", REASSEMBLE},
6178 { "reply-to", REPLYTO},
6179 { "require-order", REQUIREORDER},
6180 { "return", RETURN},
6181 { "return-icmp", RETURNICMP},
6182 { "return-icmp6", RETURNICMP6},
6183 { "return-rst", RETURNRST},
6184 { "ridentifier", RIDENTIFIER},
6185 { "round-robin", ROUNDROBIN},
6187 { "route-to", ROUTETO},
6188 { "rtable", RTABLE},
6190 { "ruleset-optimization", RULESET_OPTIMIZATION},
6193 { "set-tos", SETTOS},
6195 { "sloppy", SLOPPY},
6196 { "source-hash", SOURCEHASH},
6197 { "source-track", SOURCETRACK},
6199 { "state-defaults", STATEDEFAULTS},
6200 { "state-policy", STATEPOLICY},
6201 { "static-port", STATICPORT},
6202 { "sticky-address", STICKYADDRESS},
6203 { "syncookies", SYNCOOKIES},
6204 { "synproxy", SYNPROXY},
6207 { "tagged", TAGGED},
6208 { "target", TARGET},
6209 { "tbrsize", TBRSIZE},
6210 { "timeout", TIMEOUT},
6214 { "upperlimit", UPPERLIMIT},
6215 { "urpf-failed", URPFFAILED},
6218 const struct keywords *p;
6220 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
6221 sizeof(keywords[0]), kw_cmp);
6225 fprintf(stderr, "%s: %d\n", s, p->k_val);
6229 fprintf(stderr, "string: %s\n", s);
6234 #define MAXPUSHBACK 128
6236 static char *parsebuf;
6237 static int parseindex;
6238 static char pushback_buffer[MAXPUSHBACK];
6239 static int pushback_index = 0;
6247 /* Read character from the parsebuffer instead of input. */
6248 if (parseindex >= 0) {
6249 c = parsebuf[parseindex++];
6258 return (pushback_buffer[--pushback_index]);
6261 if ((c = getc(file->stream)) == EOF) {
6262 yyerror("reached end of file while parsing quoted string");
6263 if (popfile() == EOF)
6270 while ((c = getc(file->stream)) == '\\') {
6271 next = getc(file->stream);
6276 yylval.lineno = file->lineno;
6281 if (popfile() == EOF)
6283 c = getc(file->stream);
6295 if (parseindex >= 0)
6298 if (pushback_index < MAXPUSHBACK-1)
6299 return (pushback_buffer[pushback_index++] = c);
6311 /* skip to either EOF or the first real EOL */
6314 c = pushback_buffer[--pushback_index];
6332 int quotec, next, c;
6337 while ((c = lgetc(0)) == ' ' || c == '\t')
6340 yylval.lineno = file->lineno;
6342 while ((c = lgetc(0)) != '\n' && c != EOF)
6344 if (c == '$' && parsebuf == NULL) {
6346 if ((c = lgetc(0)) == EOF)
6349 if (p + 1 >= buf + sizeof(buf) - 1) {
6350 yyerror("string too long");
6353 if (isalnum(c) || c == '_') {
6363 yyerror("macro '%s' not defined", buf);
6376 if ((c = lgetc(quotec)) == EOF)
6381 } else if (c == '\\') {
6382 if ((next = lgetc(quotec)) == EOF)
6384 if (next == quotec || c == ' ' || c == '\t')
6386 else if (next == '\n') {
6392 } else if (c == quotec) {
6396 if (p + 1 >= buf + sizeof(buf) - 1) {
6397 yyerror("string too long");
6402 yylval.v.string = strdup(buf);
6403 if (yylval.v.string == NULL)
6404 err(1, "yylex: strdup");
6409 yylval.v.i = PF_OP_XRG;
6410 return (PORTBINARY);
6417 yylval.v.i = PF_OP_IRG;
6418 return (PORTBINARY);
6430 #define allowed_to_end_number(x) \
6431 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
6433 if (c == '-' || isdigit(c)) {
6436 if ((unsigned)(p-buf) >= sizeof(buf)) {
6437 yyerror("string too long");
6440 } while ((c = lgetc(0)) != EOF && isdigit(c));
6442 if (p == buf + 1 && buf[0] == '-')
6444 if (c == EOF || allowed_to_end_number(c)) {
6445 const char *errstr = NULL;
6448 yylval.v.number = strtonum(buf, LLONG_MIN,
6449 LLONG_MAX, &errstr);
6451 yyerror("\"%s\" invalid number: %s",
6466 #define allowed_in_string(x) \
6467 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
6468 x != '{' && x != '}' && x != '<' && x != '>' && \
6469 x != '!' && x != '=' && x != '/' && x != '#' && \
6472 if (isalnum(c) || c == ':' || c == '_') {
6475 if ((unsigned)(p-buf) >= sizeof(buf)) {
6476 yyerror("string too long");
6479 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
6482 if ((token = lookup(buf)) == STRING)
6483 if ((yylval.v.string = strdup(buf)) == NULL)
6484 err(1, "yylex: strdup");
6488 yylval.lineno = file->lineno;
6497 check_file_secrecy(int fd, const char *fname)
6501 if (fstat(fd, &st)) {
6502 warn("cannot stat %s", fname);
6505 if (st.st_uid != 0 && st.st_uid != getuid()) {
6506 warnx("%s: owner not root or current user", fname);
6509 if (st.st_mode & (S_IRWXG | S_IRWXO)) {
6510 warnx("%s: group/world readable/writeable", fname);
6517 pushfile(const char *name, int secret)
6521 if ((nfile = calloc(1, sizeof(struct file))) == NULL ||
6522 (nfile->name = strdup(name)) == NULL) {
6526 if (TAILQ_FIRST(&files) == NULL && strcmp(nfile->name, "-") == 0) {
6527 nfile->stream = stdin;
6529 if ((nfile->name = strdup("stdin")) == NULL) {
6534 } else if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
6535 warn("%s", nfile->name);
6539 } else if (secret &&
6540 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
6541 fclose(nfile->stream);
6547 TAILQ_INSERT_TAIL(&files, nfile, entry);
6556 if ((prev = TAILQ_PREV(file, files, entry)) != NULL) {
6557 prev->errors += file->errors;
6558 TAILQ_REMOVE(&files, file, entry);
6559 fclose(file->stream);
6569 parse_config(char *filename, struct pfctl *xpf)
6576 rulestate = PFCTL_STATE_NONE;
6577 returnicmpdefault = (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
6578 returnicmp6default =
6579 (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
6580 blockpolicy = PFRULE_DROP;
6581 failpolicy = PFRULE_DROP;
6584 if ((file = pushfile(filename, 0)) == NULL) {
6585 warn("cannot open the main config file!");
6590 errors = file->errors;
6593 /* Free macros and check which have not been used. */
6594 while ((sym = TAILQ_FIRST(&symhead))) {
6595 if ((pf->opts & PF_OPT_VERBOSE2) && !sym->used)
6596 fprintf(stderr, "warning: macro '%s' not "
6597 "used\n", sym->nam);
6600 TAILQ_REMOVE(&symhead, sym, entry);
6604 return (errors ? -1 : 0);
6608 symset(const char *nam, const char *val, int persist)
6612 for (sym = TAILQ_FIRST(&symhead); sym && strcmp(nam, sym->nam);
6613 sym = TAILQ_NEXT(sym, entry))
6617 if (sym->persist == 1)
6622 TAILQ_REMOVE(&symhead, sym, entry);
6626 if ((sym = calloc(1, sizeof(*sym))) == NULL)
6629 sym->nam = strdup(nam);
6630 if (sym->nam == NULL) {
6634 sym->val = strdup(val);
6635 if (sym->val == NULL) {
6641 sym->persist = persist;
6642 TAILQ_INSERT_TAIL(&symhead, sym, entry);
6647 pfctl_cmdline_symset(char *s)
6652 if ((val = strrchr(s, '=')) == NULL)
6655 if ((sym = malloc(strlen(s) - strlen(val) + 1)) == NULL)
6656 err(1, "pfctl_cmdline_symset: malloc");
6658 strlcpy(sym, s, strlen(s) - strlen(val) + 1);
6660 ret = symset(sym, val + 1, 1);
6667 symget(const char *nam)
6671 TAILQ_FOREACH(sym, &symhead, entry)
6672 if (strcmp(nam, sym->nam) == 0) {
6680 mv_rules(struct pfctl_ruleset *src, struct pfctl_ruleset *dst)
6683 struct pfctl_rule *r;
6685 for (i = 0; i < PF_RULESET_MAX; ++i) {
6686 while ((r = TAILQ_FIRST(src->rules[i].active.ptr))
6688 TAILQ_REMOVE(src->rules[i].active.ptr, r, entries);
6689 TAILQ_INSERT_TAIL(dst->rules[i].active.ptr, r, entries);
6690 dst->anchor->match++;
6692 src->anchor->match = 0;
6693 while ((r = TAILQ_FIRST(src->rules[i].inactive.ptr))
6695 TAILQ_REMOVE(src->rules[i].inactive.ptr, r, entries);
6696 TAILQ_INSERT_TAIL(dst->rules[i].inactive.ptr,
6703 mv_eth_rules(struct pfctl_eth_ruleset *src, struct pfctl_eth_ruleset *dst)
6705 struct pfctl_eth_rule *r;
6707 while ((r = TAILQ_FIRST(&src->rules)) != NULL) {
6708 TAILQ_REMOVE(&src->rules, r, entries);
6709 TAILQ_INSERT_TAIL(&dst->rules, r, entries);
6710 dst->anchor->match++;
6712 src->anchor->match = 0;
6716 decide_address_family(struct node_host *n, sa_family_t *af)
6718 if (*af != 0 || n == NULL)
6721 while ((n = n->next) != NULL) {
6730 remove_invalid_hosts(struct node_host **nh, sa_family_t *af)
6732 struct node_host *n = *nh, *prev = NULL;
6735 if (*af && n->af && n->af != *af) {
6736 /* unlink and free n */
6737 struct node_host *next = n->next;
6739 /* adjust tail pointer */
6740 if (n == (*nh)->tail)
6742 /* adjust previous node's next pointer */
6748 if (n->ifname != NULL)
6762 invalid_redirect(struct node_host *nh, sa_family_t af)
6765 struct node_host *n;
6767 /* tables and dyniftl are ok without an address family */
6768 for (n = nh; n != NULL; n = n->next) {
6769 if (n->addr.type != PF_ADDR_TABLE &&
6770 n->addr.type != PF_ADDR_DYNIFTL) {
6771 yyerror("address family not given and "
6772 "translation address expands to multiple "
6773 "address families");
6779 yyerror("no translation address with matching address family "
6787 atoul(char *s, u_long *ulvalp)
6793 ulval = strtoul(s, &ep, 0);
6794 if (s[0] == '\0' || *ep != '\0')
6796 if (errno == ERANGE && ulval == ULONG_MAX)
6808 if (atoul(n, &ulval) == 0) {
6809 if (ulval > 65535) {
6810 yyerror("illegal port value %lu", ulval);
6813 return (htons(ulval));
6815 s = getservbyname(n, "tcp");
6817 s = getservbyname(n, "udp");
6819 yyerror("unknown port %s", n);
6827 rule_label(struct pfctl_rule *r, char *s[PF_RULE_MAX_LABEL_COUNT])
6829 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++) {
6833 if (strlcpy(r->label[i], s[i], sizeof(r->label[0])) >=
6834 sizeof(r->label[0])) {
6835 yyerror("rule label too long (max %d chars)",
6836 sizeof(r->label[0])-1);
6844 parseicmpspec(char *w, sa_family_t af)
6846 const struct icmpcodeent *p;
6851 icmptype = returnicmpdefault >> 8;
6853 icmptype = returnicmp6default >> 8;
6855 if (atoul(w, &ulval) == -1) {
6856 if ((p = geticmpcodebyname(icmptype, w, af)) == NULL) {
6857 yyerror("unknown icmp code %s", w);
6863 yyerror("invalid icmp code %lu", ulval);
6866 return (icmptype << 8 | ulval);
6870 parseport(char *port, struct range *r, int extensions)
6872 char *p = strchr(port, ':');
6875 if ((r->a = getservice(port)) == -1)
6881 if ((extensions & PPORT_STAR) && !strcmp(p+1, "*")) {
6883 if ((r->a = getservice(port)) == -1)
6889 if ((extensions & PPORT_RANGE)) {
6891 if ((r->a = getservice(port)) == -1 ||
6892 (r->b = getservice(p)) == -1)
6905 pfctl_load_anchors(int dev, struct pfctl *pf, struct pfr_buffer *trans)
6907 struct loadanchors *la;
6909 TAILQ_FOREACH(la, &loadanchorshead, entries) {
6910 if (pf->opts & PF_OPT_VERBOSE)
6911 fprintf(stderr, "\nLoading anchor %s from %s\n",
6912 la->anchorname, la->filename);
6913 if (pfctl_rules(dev, la->filename, pf->opts, pf->optimize,
6914 la->anchorname, trans) == -1)
6922 kw_casecmp(const void *k, const void *e)
6924 return (strcasecmp(k, ((const struct keywords *)e)->k_name));
6928 map_tos(char *s, int *val)
6930 /* DiffServ Codepoints and other TOS mappings */
6931 const struct keywords toswords[] = {
6932 { "af11", IPTOS_DSCP_AF11 },
6933 { "af12", IPTOS_DSCP_AF12 },
6934 { "af13", IPTOS_DSCP_AF13 },
6935 { "af21", IPTOS_DSCP_AF21 },
6936 { "af22", IPTOS_DSCP_AF22 },
6937 { "af23", IPTOS_DSCP_AF23 },
6938 { "af31", IPTOS_DSCP_AF31 },
6939 { "af32", IPTOS_DSCP_AF32 },
6940 { "af33", IPTOS_DSCP_AF33 },
6941 { "af41", IPTOS_DSCP_AF41 },
6942 { "af42", IPTOS_DSCP_AF42 },
6943 { "af43", IPTOS_DSCP_AF43 },
6944 { "critical", IPTOS_PREC_CRITIC_ECP },
6945 { "cs0", IPTOS_DSCP_CS0 },
6946 { "cs1", IPTOS_DSCP_CS1 },
6947 { "cs2", IPTOS_DSCP_CS2 },
6948 { "cs3", IPTOS_DSCP_CS3 },
6949 { "cs4", IPTOS_DSCP_CS4 },
6950 { "cs5", IPTOS_DSCP_CS5 },
6951 { "cs6", IPTOS_DSCP_CS6 },
6952 { "cs7", IPTOS_DSCP_CS7 },
6953 { "ef", IPTOS_DSCP_EF },
6954 { "inetcontrol", IPTOS_PREC_INTERNETCONTROL },
6955 { "lowdelay", IPTOS_LOWDELAY },
6956 { "netcontrol", IPTOS_PREC_NETCONTROL },
6957 { "reliability", IPTOS_RELIABILITY },
6958 { "throughput", IPTOS_THROUGHPUT },
6959 { "va", IPTOS_DSCP_VA }
6961 const struct keywords *p;
6963 p = bsearch(s, toswords, sizeof(toswords)/sizeof(toswords[0]),
6964 sizeof(toswords[0]), kw_casecmp);
6974 rt_tableid_max(void)
6978 size_t l = sizeof(fibs);
6980 if (sysctlbyname("net.fibs", &fibs, &l, NULL, 0) == -1)
6981 fibs = 16; /* XXX RT_MAXFIBS, at least limit it some. */
6983 * As the OpenBSD code only compares > and not >= we need to adjust
6984 * here given we only accept values of 0..n and want to avoid #ifdefs
6989 return (RT_TABLEID_MAX);
6994 node_mac_from_string(const char *str)
6998 m = calloc(1, sizeof(struct node_mac));
7000 err(1, "mac: calloc");
7002 if (sscanf(str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
7003 &m->mac[0], &m->mac[1], &m->mac[2], &m->mac[3], &m->mac[4],
7006 yyerror("invalid MAC address");
7010 memset(m->mask, 0xff, ETHER_ADDR_LEN);
7019 node_mac_from_string_masklen(const char *str, int masklen)
7023 if (masklen < 0 || masklen > (ETHER_ADDR_LEN * 8)) {
7024 yyerror("invalid MAC mask length");
7028 m = node_mac_from_string(str);
7032 memset(m->mask, 0, ETHER_ADDR_LEN);
7033 for (int i = 0; i < masklen; i++)
7034 m->mask[i / 8] |= 1 << (i % 8);
7040 node_mac_from_string_mask(const char *str, const char *mask)
7044 m = node_mac_from_string(str);
7048 if (sscanf(mask, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
7049 &m->mask[0], &m->mask[1], &m->mask[2], &m->mask[3], &m->mask[4],
7050 &m->mask[5]) != 6) {
7052 yyerror("invalid MAC mask");