pf: Update packet filter to the version that comes with OpenBSD 4.1
[dragonfly.git] / usr.sbin / pfctl / parse.y
1 /*      $OpenBSD: parse.y,v 1.517 2007/02/03 23:26:40 dhartmei Exp $    */
2
3 /*
4  * Copyright (c) 2001 Markus Friedl.  All rights reserved.
5  * Copyright (c) 2001 Daniel Hartmeier.  All rights reserved.
6  * Copyright (c) 2001 Theo de Raadt.  All rights reserved.
7  * Copyright (c) 2002,2003 Henning Brauer. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 %{
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <net/if.h>
33 #include <netinet/in.h>
34 #include <netinet/in_systm.h>
35 #include <netinet/ip.h>
36 #include <netinet/ip_icmp.h>
37 #include <netinet/icmp6.h>
38 #include <net/pf/pfvar.h>
39 #include <arpa/inet.h>
40 #include <net/altq/altq.h>
41 #include <net/altq/altq_cbq.h>
42 #include <net/altq/altq_priq.h>
43 #include <net/altq/altq_hfsc.h>
44 #include <net/altq/altq_fairq.h>
45
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <netdb.h>
49 #include <stdarg.h>
50 #include <errno.h>
51 #include <string.h>
52 #include <ctype.h>
53 #include <math.h>
54 #include <err.h>
55 #include <limits.h>
56 #include <pwd.h>
57 #include <grp.h>
58 #include <md5.h>
59
60 #include "pfctl_parser.h"
61 #include "pfctl.h"
62
63 static struct pfctl     *pf = NULL;
64 static FILE             *fin = NULL;
65 static int               debug = 0;
66 static int               lineno = 1;
67 static int               errors = 0;
68 static int               rulestate = 0;
69 static u_int16_t         returnicmpdefault =
70                             (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
71 static u_int16_t         returnicmp6default =
72                             (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
73 static int               blockpolicy = PFRULE_DROP;
74 static int               require_order = 1;
75 static int               default_statelock;
76 static int               default_keeppolicy_action;
77 static struct node_state_opt *default_keeppolicy_options;
78
79 enum {
80         PFCTL_STATE_NONE,
81         PFCTL_STATE_OPTION,
82         PFCTL_STATE_SCRUB,
83         PFCTL_STATE_QUEUE,
84         PFCTL_STATE_NAT,
85         PFCTL_STATE_FILTER
86 };
87
88 struct node_proto {
89         u_int8_t                 proto;
90         struct node_proto       *next;
91         struct node_proto       *tail;
92 };
93
94 struct node_port {
95         u_int16_t                port[2];
96         u_int8_t                 op;
97         struct node_port        *next;
98         struct node_port        *tail;
99 };
100
101 struct node_uid {
102         uid_t                    uid[2];
103         u_int8_t                 op;
104         struct node_uid         *next;
105         struct node_uid         *tail;
106 };
107
108 struct node_gid {
109         gid_t                    gid[2];
110         u_int8_t                 op;
111         struct node_gid         *next;
112         struct node_gid         *tail;
113 };
114
115 struct node_icmp {
116         u_int8_t                 code;
117         u_int8_t                 type;
118         u_int8_t                 proto;
119         struct node_icmp        *next;
120         struct node_icmp        *tail;
121 };
122
123 enum    { PF_STATE_OPT_MAX, PF_STATE_OPT_NOSYNC, PF_STATE_OPT_SRCTRACK,
124             PF_STATE_OPT_MAX_SRC_STATES, PF_STATE_OPT_MAX_SRC_CONN,
125             PF_STATE_OPT_MAX_SRC_CONN_RATE, PF_STATE_OPT_MAX_SRC_NODES,
126             PF_STATE_OPT_OVERLOAD, PF_STATE_OPT_STATELOCK,
127             PF_STATE_OPT_TIMEOUT, PF_STATE_OPT_PICKUPS };
128
129 enum    { PF_SRCTRACK_NONE, PF_SRCTRACK, PF_SRCTRACK_GLOBAL, PF_SRCTRACK_RULE };
130
131 struct node_state_opt {
132         int                      type;
133         union {
134                 u_int32_t        max_states;
135                 u_int32_t        max_src_states;
136                 u_int32_t        max_src_conn;
137                 struct {
138                         u_int32_t       limit;
139                         u_int32_t       seconds;
140                 }                max_src_conn_rate;
141                 struct {
142                         u_int8_t        flush;
143                         char            tblname[PF_TABLE_NAME_SIZE];
144                 }                overload;
145                 u_int32_t        max_src_nodes;
146                 u_int8_t         src_track;
147                 u_int8_t         pickup_mode;
148                 u_int32_t        statelock;
149                 struct {
150                         int             number;
151                         u_int32_t       seconds;
152                 }                timeout;
153         }                        data;
154         struct node_state_opt   *next;
155         struct node_state_opt   *tail;
156 };
157
158 struct peer {
159         struct node_host        *host;
160         struct node_port        *port;
161 };
162
163 struct node_queue {
164         char                     queue[PF_QNAME_SIZE];
165         char                     parent[PF_QNAME_SIZE];
166         char                     ifname[IFNAMSIZ];
167         int                      scheduler;
168         struct node_queue       *next;
169         struct node_queue       *tail;
170 }       *queues = NULL;
171
172 struct node_qassign {
173         char            *qname;
174         char            *pqname;
175 };
176
177 struct filter_opts {
178         int                      marker;
179 #define FOM_FLAGS       0x01
180 #define FOM_ICMP        0x02
181 #define FOM_TOS         0x04
182 #define FOM_KEEP        0x08
183 #define FOM_SRCTRACK    0x10
184         struct node_uid         *uid;
185         struct node_gid         *gid;
186         struct {
187                 u_int8_t         b1;
188                 u_int8_t         b2;
189                 u_int16_t        w;
190                 u_int16_t        w2;
191         } flags;
192         struct node_icmp        *icmpspec;
193         u_int32_t                tos;
194         u_int32_t                prob;
195         struct {
196                 int                      action;
197                 struct node_state_opt   *options;
198         } keep;
199         int                      fragment;
200         int                      allowopts;
201         char                    *label;
202         struct node_qassign      queues;
203         char                    *tag;
204         char                    *match_tag;
205         u_int8_t                 match_tag_not;
206         int                      rtableid;
207 } filter_opts;
208
209 struct antispoof_opts {
210         char                    *label;
211         int                      rtableid;
212 } antispoof_opts;
213
214 struct scrub_opts {
215         int                     marker;
216 #define SOM_MINTTL      0x01
217 #define SOM_MAXMSS      0x02
218 #define SOM_FRAGCACHE   0x04
219         int                     nodf;
220         int                     minttl;
221         int                     maxmss;
222         int                     fragcache;
223         int                     randomid;
224         int                     reassemble_tcp;
225         int                     rtableid;
226 } scrub_opts;
227
228 struct queue_opts {
229         int                     marker;
230 #define QOM_BWSPEC      0x01
231 #define QOM_SCHEDULER   0x02
232 #define QOM_PRIORITY    0x04
233 #define QOM_TBRSIZE     0x08
234 #define QOM_QLIMIT      0x10
235         struct node_queue_bw    queue_bwspec;
236         struct node_queue_opt   scheduler;
237         int                     priority;
238         int                     tbrsize;
239         int                     qlimit;
240 } queue_opts;
241
242 struct table_opts {
243         int                     flags;
244         int                     init_addr;
245         struct node_tinithead   init_nodes;
246 } table_opts;
247
248 struct pool_opts {
249         int                      marker;
250 #define POM_TYPE                0x01
251 #define POM_STICKYADDRESS       0x02
252         u_int8_t                 opts;
253         int                      type;
254         int                      staticport;
255         struct pf_poolhashkey   *key;
256
257 } pool_opts;
258
259
260 struct node_hfsc_opts   hfsc_opts;
261 struct node_fairq_opts  fairq_opts;
262
263 int     yyerror(const char *, ...);
264 int     disallow_table(struct node_host *, const char *);
265 int     disallow_urpf_failed(struct node_host *, const char *);
266 int     disallow_alias(struct node_host *, const char *);
267 int     rule_consistent(struct pf_rule *, int);
268 int     filter_consistent(struct pf_rule *, int);
269 int     nat_consistent(struct pf_rule *);
270 int     rdr_consistent(struct pf_rule *);
271 int     process_tabledef(char *, struct table_opts *);
272 int     yyparse(void);
273 void    expand_label_str(char *, size_t, const char *, const char *);
274 void    expand_label_if(const char *, char *, size_t, const char *);
275 void    expand_label_addr(const char *, char *, size_t, u_int8_t,
276             struct node_host *);
277 void    expand_label_port(const char *, char *, size_t, struct node_port *);
278 void    expand_label_proto(const char *, char *, size_t, u_int8_t);
279 void    expand_label_nr(const char *, char *, size_t);
280 void    expand_label(char *, size_t, const char *, u_int8_t, struct node_host *,
281             struct node_port *, struct node_host *, struct node_port *,
282             u_int8_t);
283 void    expand_rule(struct pf_rule *, struct node_if *, struct node_host *,
284             struct node_proto *, struct node_os*, struct node_host *,
285             struct node_port *, struct node_host *, struct node_port *,
286             struct node_uid *, struct node_gid *, struct node_icmp *,
287             const char *);
288 int     expand_altq(struct pf_altq *, struct node_if *, struct node_queue *,
289             struct node_queue_bw bwspec, struct node_queue_opt *);
290 int     expand_queue(struct pf_altq *, struct node_if *, struct node_queue *,
291             struct node_queue_bw, struct node_queue_opt *);
292 int     expand_skip_interface(struct node_if *);
293
294 int      check_rulestate(int);
295 int      kw_cmp(const void *, const void *);
296 int      lookup(char *);
297 int      lgetc(FILE *);
298 int      lungetc(int);
299 int      findeol(void);
300 int      yylex(void);
301 int      atoul(char *, u_long *);
302 int      getservice(char *);
303 int      rule_label(struct pf_rule *, char *);
304
305 TAILQ_HEAD(symhead, sym)         symhead = TAILQ_HEAD_INITIALIZER(symhead);
306 struct sym {
307         TAILQ_ENTRY(sym)         entries;
308         int                      used;
309         int                      persist;
310         char                    *nam;
311         char                    *val;
312 };
313
314
315 int      symset(const char *, const char *, int);
316 char    *symget(const char *);
317
318 void     mv_rules(struct pf_ruleset *, struct pf_ruleset *);
319 void     decide_address_family(struct node_host *, sa_family_t *);
320 void     remove_invalid_hosts(struct node_host **, sa_family_t *);
321 int      invalid_redirect(struct node_host *, sa_family_t);
322 u_int16_t parseicmpspec(char *, sa_family_t);
323
324 TAILQ_HEAD(loadanchorshead, loadanchors)
325     loadanchorshead = TAILQ_HEAD_INITIALIZER(loadanchorshead);
326
327 struct loadanchors {
328         TAILQ_ENTRY(loadanchors)         entries;
329         char                            *anchorname;
330         char                            *filename;
331 };
332
333 typedef struct {
334         union {
335                 u_int32_t                number;
336                 int                      i;
337                 char                    *string;
338                 int                      rtableid;
339                 struct {
340                         u_int8_t         b1;
341                         u_int8_t         b2;
342                         u_int16_t        w;
343                         u_int16_t        w2;
344                 }                        b;
345                 struct range {
346                         int              a;
347                         int              b;
348                         int              t;
349                 }                        range;
350                 struct node_if          *interface;
351                 struct node_proto       *proto;
352                 struct node_icmp        *icmp;
353                 struct node_host        *host;
354                 struct node_os          *os;
355                 struct node_port        *port;
356                 struct node_uid         *uid;
357                 struct node_gid         *gid;
358                 struct node_state_opt   *state_opt;
359                 struct peer              peer;
360                 struct {
361                         struct peer      src, dst;
362                         struct node_os  *src_os;
363                 }                        fromto;
364                 struct {
365                         struct node_host        *host;
366                         u_int8_t                 rt;
367                         u_int8_t                 pool_opts;
368                         sa_family_t              af;
369                         struct pf_poolhashkey   *key;
370                 }                        route;
371                 struct redirection {
372                         struct node_host        *host;
373                         struct range             rport;
374                 }                       *redirection;
375                 struct {
376                         int                      action;
377                         struct node_state_opt   *options;
378                 }                        keep_state;
379                 struct {
380                         u_int8_t         log;
381                         u_int8_t         logif;
382                         u_int8_t         quick;
383                 }                        logquick;
384                 struct {
385                         int              neg;
386                         char            *name;
387                 }                        tagged;
388                 struct pf_poolhashkey   *hashkey;
389                 struct node_queue       *queue;
390                 struct node_queue_opt    queue_options;
391                 struct node_queue_bw     queue_bwspec;
392                 struct node_qassign      qassign;
393                 struct filter_opts       filter_opts;
394                 struct antispoof_opts    antispoof_opts;
395                 struct queue_opts        queue_opts;
396                 struct scrub_opts        scrub_opts;
397                 struct table_opts        table_opts;
398                 struct pool_opts         pool_opts;
399                 struct node_hfsc_opts    hfsc_opts;
400                 struct node_fairq_opts   fairq_opts;
401         } v;
402         int lineno;
403 } YYSTYPE;
404
405 #define DYNIF_MULTIADDR(addr) ((addr).type == PF_ADDR_DYNIFTL && \
406         (!((addr).iflags & PFI_AFLAG_NOALIAS) ||                 \
407         !isdigit((addr).v.ifname[strlen((addr).v.ifname)-1])))
408
409 extern char     *infile;
410
411 %}
412
413 %token  PASS BLOCK SCRUB RETURN IN OS OUT LOG QUICK ON FROM TO FLAGS
414 %token  RETURNRST RETURNICMP RETURNICMP6 PROTO INET INET6 ALL ANY ICMPTYPE
415 %token  ICMP6TYPE CODE KEEP MODULATE STATE PORT RDR NAT BINAT ARROW NODF
416 %token  MINTTL ERROR ALLOWOPTS FASTROUTE FILENAME ROUTETO DUPTO REPLYTO NO LABEL
417 %token  NOROUTE URPFFAILED FRAGMENT USER GROUP MAXMSS MAXIMUM TTL TOS DROP TABLE
418 %token  REASSEMBLE FRAGDROP FRAGCROP ANCHOR NATANCHOR RDRANCHOR BINATANCHOR
419 %token  SET OPTIMIZATION TIMEOUT LIMIT LOGINTERFACE BLOCKPOLICY RANDOMID
420 %token  REQUIREORDER SYNPROXY FINGERPRINTS NOSYNC DEBUG SKIP HOSTID
421 %token  ANTISPOOF FOR
422 %token  BITMASK RANDOM SOURCEHASH ROUNDROBIN STATICPORT PROBABILITY
423 %token  ALTQ CBQ PRIQ HFSC FAIRQ BANDWIDTH TBRSIZE LINKSHARE REALTIME UPPERLIMIT
424 %token  QUEUE PRIORITY QLIMIT RTABLE HOGS BUCKETS
425 %token  LOAD RULESET_OPTIMIZATION
426 %token  PICKUPS NOPICKUPS HASHONLY
427 %token  STICKYADDRESS MAXSRCSTATES MAXSRCNODES SOURCETRACK GLOBAL RULE
428 %token  MAXSRCCONN MAXSRCCONNRATE OVERLOAD FLUSH
429 %token  TAGGED TAG IFBOUND FLOATING STATEPOLICY ROUTE KEEPPOLICY
430 %token  <v.string>              STRING
431 %token  <v.i>                   PORTBINARY
432 %type   <v.interface>           interface if_list if_item_not if_item
433 %type   <v.number>              number icmptype icmp6type uid gid
434 %type   <v.number>              tos not yesno
435 %type   <v.i>                   no dir af fragcache optimizer
436 %type   <v.i>                   sourcetrack flush unaryop statelock
437 %type   <v.b>                   action nataction natpass scrubaction
438 %type   <v.b>                   flags flag blockspec
439 %type   <v.range>               port rport
440 %type   <v.hashkey>             hashkey
441 %type   <v.proto>               proto proto_list proto_item
442 %type   <v.icmp>                icmpspec
443 %type   <v.icmp>                icmp_list icmp_item
444 %type   <v.icmp>                icmp6_list icmp6_item
445 %type   <v.fromto>              fromto
446 %type   <v.peer>                ipportspec from to
447 %type   <v.host>                ipspec xhost host dynaddr host_list
448 %type   <v.host>                redir_host_list redirspec
449 %type   <v.host>                route_host route_host_list routespec
450 %type   <v.os>                  os xos os_list
451 %type   <v.port>                portspec port_list port_item
452 %type   <v.uid>                 uids uid_list uid_item
453 %type   <v.gid>                 gids gid_list gid_item
454 %type   <v.route>               route
455 %type   <v.redirection>         redirection redirpool
456 %type   <v.string>              label string tag anchorname
457 %type   <v.keep_state>          keep
458 %type   <v.state_opt>           state_opt_spec state_opt_list state_opt_item
459 %type   <v.logquick>            logquick quick log logopts logopt
460 %type   <v.interface>           antispoof_ifspc antispoof_iflst antispoof_if
461 %type   <v.qassign>             qname
462 %type   <v.queue>               qassign qassign_list qassign_item
463 %type   <v.queue_options>       scheduler
464 %type   <v.number>              cbqflags_list cbqflags_item
465 %type   <v.number>              priqflags_list priqflags_item
466 %type   <v.hfsc_opts>           hfscopts_list hfscopts_item hfsc_opts
467 %type   <v.fairq_opts>          fairqopts_list fairqopts_item fairq_opts
468 %type   <v.queue_bwspec>        bandwidth
469 %type   <v.filter_opts>         filter_opts filter_opt filter_opts_l
470 %type   <v.antispoof_opts>      antispoof_opts antispoof_opt antispoof_opts_l
471 %type   <v.queue_opts>          queue_opts queue_opt queue_opts_l
472 %type   <v.scrub_opts>          scrub_opts scrub_opt scrub_opts_l
473 %type   <v.table_opts>          table_opts table_opt table_opts_l
474 %type   <v.pool_opts>           pool_opts pool_opt pool_opts_l
475 %type   <v.tagged>              tagged
476 %type   <v.rtableid>            rtable
477 %%
478
479 ruleset         : /* empty */
480                 | ruleset '\n'
481                 | ruleset option '\n'
482                 | ruleset scrubrule '\n'
483                 | ruleset natrule '\n'
484                 | ruleset binatrule '\n'
485                 | ruleset pfrule '\n'
486                 | ruleset anchorrule '\n'
487                 | ruleset loadrule '\n'
488                 | ruleset altqif '\n'
489                 | ruleset queuespec '\n'
490                 | ruleset varset '\n'
491                 | ruleset antispoof '\n'
492                 | ruleset tabledef '\n'
493                 | '{' fakeanchor '}' '\n';
494                 | ruleset error '\n'            { errors++; }
495                 ;
496
497 /*
498  * apply to previouslys specified rule: must be careful to note
499  * what that is: pf or nat or binat or rdr
500  */
501 fakeanchor      : fakeanchor '\n'
502                 | fakeanchor anchorrule '\n'
503                 | fakeanchor binatrule '\n'
504                 | fakeanchor natrule '\n'
505                 | fakeanchor pfrule '\n'
506                 | fakeanchor error '\n'
507                 ;
508
509 optimizer       : string        {
510                         if (!strcmp($1, "none"))
511                                 $$ = 0;
512                         else if (!strcmp($1, "basic"))
513                                 $$ = PF_OPTIMIZE_BASIC;
514                         else if (!strcmp($1, "profile"))
515                                 $$ = PF_OPTIMIZE_BASIC | PF_OPTIMIZE_PROFILE;
516                         else {
517                                 yyerror("unknown ruleset-optimization %s", $$);
518                                 YYERROR;
519                         }
520                 }
521                 ;
522
523 option          : SET OPTIMIZATION STRING               {
524                         if (check_rulestate(PFCTL_STATE_OPTION)) {
525                                 free($3);
526                                 YYERROR;
527                         }
528                         if (pfctl_set_optimization(pf, $3) != 0) {
529                                 yyerror("unknown optimization %s", $3);
530                                 free($3);
531                                 YYERROR;
532                         }
533                         free($3);
534                 }
535                 | SET RULESET_OPTIMIZATION optimizer {
536                         if (!(pf->opts & PF_OPT_OPTIMIZE)) {
537                                 pf->opts |= PF_OPT_OPTIMIZE;
538                                 pf->optimize = $3;
539                         }
540                 }
541                 | SET TIMEOUT timeout_spec
542                 | SET TIMEOUT '{' timeout_list '}'
543                 | SET LIMIT limit_spec
544                 | SET LIMIT '{' limit_list '}'
545                 | SET LOGINTERFACE STRING               {
546                         if (check_rulestate(PFCTL_STATE_OPTION)) {
547                                 free($3);
548                                 YYERROR;
549                         }
550                         if (pfctl_set_logif(pf, $3) != 0) {
551                                 yyerror("error setting loginterface %s", $3);
552                                 free($3);
553                                 YYERROR;
554                         }
555                         free($3);
556                 }
557                 | SET HOSTID number {
558                         if ($3 == 0) {
559                                 yyerror("hostid must be non-zero");
560                                 YYERROR;
561                         }
562                         if (pfctl_set_hostid(pf, $3) != 0) {
563                                 yyerror("error setting hostid %08x", $3);
564                                 YYERROR;
565                         }
566                 }
567                 | SET BLOCKPOLICY DROP  {
568                         if (pf->opts & PF_OPT_VERBOSE)
569                                 printf("set block-policy drop\n");
570                         if (check_rulestate(PFCTL_STATE_OPTION))
571                                 YYERROR;
572                         blockpolicy = PFRULE_DROP;
573                 }
574                 | SET BLOCKPOLICY RETURN {
575                         if (pf->opts & PF_OPT_VERBOSE)
576                                 printf("set block-policy return\n");
577                         if (check_rulestate(PFCTL_STATE_OPTION))
578                                 YYERROR;
579                         blockpolicy = PFRULE_RETURN;
580                 }
581                 | SET REQUIREORDER yesno {
582                         if (pf->opts & PF_OPT_VERBOSE)
583                                 printf("set require-order %s\n",
584                                     $3 == 1 ? "yes" : "no");
585                         require_order = $3;
586                 }
587                 | SET FINGERPRINTS STRING {
588                         if (pf->opts & PF_OPT_VERBOSE)
589                                 printf("set fingerprints \"%s\"\n", $3);
590                         if (check_rulestate(PFCTL_STATE_OPTION)) {
591                                 free($3);
592                                 YYERROR;
593                         }
594                         if (!pf->anchor->name[0]) {
595                                 if (pfctl_file_fingerprints(pf->dev,
596                                     pf->opts, $3)) {
597                                         yyerror("error loading "
598                                             "fingerprints %s", $3);
599                                         free($3);
600                                         YYERROR;
601                                 }
602                         }
603                         free($3);
604                 }
605                 | SET STATEPOLICY statelock {
606                         if (pf->opts & PF_OPT_VERBOSE)
607                                 switch ($3) {
608                                 case 0:
609                                         printf("set state-policy floating\n");
610                                         break;
611                                 case PFRULE_IFBOUND:
612                                         printf("set state-policy if-bound\n");
613                                         break;
614                                 }
615                         default_statelock = $3;
616                 }
617                 | SET KEEPPOLICY keep {
618                         if (pf->opts & PF_OPT_VERBOSE)
619                                 printf("A default keeppolicy was set\n");
620                         default_keeppolicy_action  = $3.action;
621                         default_keeppolicy_options = $3.options;
622                 }
623                 | SET DEBUG STRING {
624                         if (check_rulestate(PFCTL_STATE_OPTION)) {
625                                 free($3);
626                                 YYERROR;
627                         }
628                         if (pfctl_set_debug(pf, $3) != 0) {
629                                 yyerror("error setting debuglevel %s", $3);
630                                 free($3);
631                                 YYERROR;
632                         }
633                         free($3);
634                 }
635                 | SET SKIP interface {
636                         if (expand_skip_interface($3) != 0) {
637                                 yyerror("error setting skip interface(s)");
638                                 YYERROR;
639                         }
640                 }
641                 ;
642
643 string          : string STRING                         {
644                         if (asprintf(&$$, "%s %s", $1, $2) == -1)
645                                 err(1, "string: asprintf");
646                         free($1);
647                         free($2);
648                 }
649                 | STRING
650                 ;
651
652 varset          : STRING '=' string             {
653                         if (pf->opts & PF_OPT_VERBOSE)
654                                 printf("%s = \"%s\"\n", $1, $3);
655                         if (symset($1, $3, 0) == -1)
656                                 err(1, "cannot store variable %s", $1);
657                         free($1);
658                         free($3);
659                 }
660                 ;
661
662 anchorname      : STRING                        { $$ = $1; }
663                 | /* empty */                   { $$ = NULL; }
664                 ;
665
666 optnl           : optnl '\n'
667                 |
668                 ;
669
670 pfa_anchorlist  : pfrule optnl
671                 | anchorrule optnl
672                 | pfa_anchorlist pfrule optnl
673                 | pfa_anchorlist anchorrule optnl
674                 ;
675
676 pfa_anchor      : '{'
677                 {
678                         char ta[PF_ANCHOR_NAME_SIZE];
679                         struct pf_ruleset *rs;
680
681                         /* steping into a brace anchor */
682                         pf->asd++;
683                         pf->bn++;
684                         pf->brace = 1;
685
686                         /* create a holding ruleset in the root */
687                         snprintf(ta, PF_ANCHOR_NAME_SIZE, "_%d", pf->bn);
688                         rs = pf_find_or_create_ruleset(ta);
689                         if (rs == NULL)
690                                 err(1, "pfa_anchor: pf_find_or_create_ruleset");
691                         pf->astack[pf->asd] = rs->anchor;
692                         pf->anchor = rs->anchor;
693                 } '\n' pfa_anchorlist '}'
694                 {
695                         pf->alast = pf->anchor;
696                         pf->asd--;
697                         pf->anchor = pf->astack[pf->asd];
698                 }
699                 | /* empty */
700                 ;
701
702 anchorrule      : ANCHOR anchorname dir quick interface af proto fromto
703                     filter_opts pfa_anchor
704                 {
705                         struct pf_rule  r;
706
707                         if (check_rulestate(PFCTL_STATE_FILTER)) {
708                                 if ($2)
709                                         free($2);
710                                 YYERROR;
711                         }
712
713                         if ($2 && ($2[0] == '_' || strstr($2, "/_") != NULL)) {
714                                 free($2);
715                                 yyerror("anchor names beginning with '_' "
716                                     "are reserved for internal use");
717                                 YYERROR;
718                         }
719
720                         memset(&r, 0, sizeof(r));
721                         if (pf->astack[pf->asd + 1]) {
722                                 /* move inline rules into relative location */
723                                 pf_anchor_setup(&r,
724                                     &pf->astack[pf->asd]->ruleset,
725                                     $2 ? $2 : pf->alast->name);
726                 
727                                 if (r.anchor == NULL)
728                                         err(1, "anchorrule: unable to "
729                                             "create ruleset");
730
731                                 if (pf->alast != r.anchor) {
732                                         if (r.anchor->match) {
733                                                 yyerror("inline anchor '%s' "
734                                                     "already exists",
735                                                     r.anchor->name);
736                                                 YYERROR;
737                                         }
738                                         mv_rules(&pf->alast->ruleset,
739                                             &r.anchor->ruleset);
740                                 }
741                                 pf_remove_if_empty_ruleset(&pf->alast->ruleset);
742                                 pf->alast = r.anchor;
743                         } else {
744                                 if (!$2) {
745                                         yyerror("anchors without explicit "
746                                             "rules must specify a name");
747                                         YYERROR;
748                                 }
749                         }
750                         r.direction = $3;
751                         r.quick = $4.quick;
752                         r.af = $6;
753                         r.prob = $9.prob;
754                         r.rtableid = $9.rtableid;
755
756                         if ($9.match_tag)
757                                 if (strlcpy(r.match_tagname, $9.match_tag,
758                                     PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
759                                         yyerror("tag too long, max %u chars",
760                                             PF_TAG_NAME_SIZE - 1);
761                                         YYERROR;
762                                 }
763                         r.match_tag_not = $9.match_tag_not;
764
765                         decide_address_family($8.src.host, &r.af);
766                         decide_address_family($8.dst.host, &r.af);
767
768                         expand_rule(&r, $5, NULL, $7, $8.src_os,
769                             $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
770                             0, 0, 0, pf->astack[pf->asd + 1] ?
771                             pf->alast->name : $2);
772                         free($2);
773                         pf->astack[pf->asd + 1] = NULL;
774                 }
775                 | NATANCHOR string interface af proto fromto rtable {
776                         struct pf_rule  r;
777
778                         if (check_rulestate(PFCTL_STATE_NAT)) {
779                                 free($2);
780                                 YYERROR;
781                         }
782
783                         memset(&r, 0, sizeof(r));
784                         r.action = PF_NAT;
785                         r.af = $4;
786                         r.rtableid = $7;
787
788                         decide_address_family($6.src.host, &r.af);
789                         decide_address_family($6.dst.host, &r.af);
790
791                         expand_rule(&r, $3, NULL, $5, $6.src_os,
792                             $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
793                             0, 0, 0, $2);
794                         free($2);
795                 }
796                 | RDRANCHOR string interface af proto fromto rtable {
797                         struct pf_rule  r;
798
799                         if (check_rulestate(PFCTL_STATE_NAT)) {
800                                 free($2);
801                                 YYERROR;
802                         }
803
804                         memset(&r, 0, sizeof(r));
805                         r.action = PF_RDR;
806                         r.af = $4;
807                         r.rtableid = $7;
808
809                         decide_address_family($6.src.host, &r.af);
810                         decide_address_family($6.dst.host, &r.af);
811
812                         if ($6.src.port != NULL) {
813                                 yyerror("source port parameter not supported"
814                                     " in rdr-anchor");
815                                 YYERROR;
816                         }
817                         if ($6.dst.port != NULL) {
818                                 if ($6.dst.port->next != NULL) {
819                                         yyerror("destination port list "
820                                             "expansion not supported in "
821                                             "rdr-anchor");
822                                         YYERROR;
823                                 } else if ($6.dst.port->op != PF_OP_EQ) {
824                                         yyerror("destination port operators"
825                                             " not supported in rdr-anchor");
826                                         YYERROR;
827                                 }
828                                 r.dst.port[0] = $6.dst.port->port[0];
829                                 r.dst.port[1] = $6.dst.port->port[1];
830                                 r.dst.port_op = $6.dst.port->op;
831                         }
832
833                         expand_rule(&r, $3, NULL, $5, $6.src_os,
834                             $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
835                             0, 0, 0, $2);
836                         free($2);
837                 }
838                 | BINATANCHOR string interface af proto fromto rtable {
839                         struct pf_rule  r;
840
841                         if (check_rulestate(PFCTL_STATE_NAT)) {
842                                 free($2);
843                                 YYERROR;
844                         }
845
846                         memset(&r, 0, sizeof(r));
847                         r.action = PF_BINAT;
848                         r.af = $4;
849                         r.rtableid = $7;
850                         if ($5 != NULL) {
851                                 if ($5->next != NULL) {
852                                         yyerror("proto list expansion"
853                                             " not supported in binat-anchor");
854                                         YYERROR;
855                                 }
856                                 r.proto = $5->proto;
857                                 free($5);
858                         }
859
860                         if ($6.src.host != NULL || $6.src.port != NULL ||
861                             $6.dst.host != NULL || $6.dst.port != NULL) {
862                                 yyerror("fromto parameter not supported"
863                                     " in binat-anchor");
864                                 YYERROR;
865                         }
866
867                         decide_address_family($6.src.host, &r.af);
868                         decide_address_family($6.dst.host, &r.af);
869
870                         pfctl_add_rule(pf, &r, $2);
871                         free($2);
872                 }
873                 ;
874
875 loadrule        : LOAD ANCHOR string FROM string        {
876                         struct loadanchors      *loadanchor;
877
878                         if (strlen(pf->anchor->name) + 1 +
879                             strlen($3) >= MAXPATHLEN) {
880                                 yyerror("anchorname %s too long, max %u\n",
881                                     $3, MAXPATHLEN - 1);
882                                 free($3);
883                                 YYERROR;
884                         }
885                         loadanchor = calloc(1, sizeof(struct loadanchors));
886                         if (loadanchor == NULL)
887                                 err(1, "loadrule: calloc");
888                         if ((loadanchor->anchorname = malloc(MAXPATHLEN)) ==
889                             NULL)
890                                 err(1, "loadrule: malloc");
891                         if (pf->anchor->name[0])
892                                 snprintf(loadanchor->anchorname, MAXPATHLEN,
893                                     "%s/%s", pf->anchor->name, $3);
894                         else
895                                 strlcpy(loadanchor->anchorname, $3, MAXPATHLEN);
896                         if ((loadanchor->filename = strdup($5)) == NULL)
897                                 err(1, "loadrule: strdup");
898
899                         TAILQ_INSERT_TAIL(&loadanchorshead, loadanchor,
900                             entries);
901
902                         free($3);
903                         free($5);
904                 };
905
906 scrubaction     : no SCRUB {
907                         $$.b2 = $$.w = 0;
908                         if ($1)
909                                 $$.b1 = PF_NOSCRUB;
910                         else
911                                 $$.b1 = PF_SCRUB;
912                 }
913                 ;
914
915 scrubrule       : scrubaction dir logquick interface af proto fromto scrub_opts
916                 {
917                         struct pf_rule  r;
918
919                         if (check_rulestate(PFCTL_STATE_SCRUB))
920                                 YYERROR;
921
922                         memset(&r, 0, sizeof(r));
923
924                         r.action = $1.b1;
925                         r.direction = $2;
926
927                         r.log = $3.log;
928                         r.logif = $3.logif;
929                         if ($3.quick) {
930                                 yyerror("scrub rules do not support 'quick'");
931                                 YYERROR;
932                         }
933
934                         r.af = $5;
935                         if ($8.nodf)
936                                 r.rule_flag |= PFRULE_NODF;
937                         if ($8.randomid)
938                                 r.rule_flag |= PFRULE_RANDOMID;
939                         if ($8.reassemble_tcp) {
940                                 if (r.direction != PF_INOUT) {
941                                         yyerror("reassemble tcp rules can not "
942                                             "specify direction");
943                                         YYERROR;
944                                 }
945                                 r.rule_flag |= PFRULE_REASSEMBLE_TCP;
946                         }
947                         if ($8.minttl)
948                                 r.min_ttl = $8.minttl;
949                         if ($8.maxmss)
950                                 r.max_mss = $8.maxmss;
951                         if ($8.fragcache)
952                                 r.rule_flag |= $8.fragcache;
953                         r.rtableid = $8.rtableid;
954
955                         expand_rule(&r, $4, NULL, $6, $7.src_os,
956                             $7.src.host, $7.src.port, $7.dst.host, $7.dst.port,
957                             NULL, NULL, NULL, "");
958                 }
959                 ;
960
961 scrub_opts      :       {
962                                 bzero(&scrub_opts, sizeof scrub_opts);
963                                 scrub_opts.rtableid = -1;
964                         }
965                     scrub_opts_l
966                         { $$ = scrub_opts; }
967                 | /* empty */ {
968                         bzero(&scrub_opts, sizeof scrub_opts);
969                         scrub_opts.rtableid = -1;
970                         $$ = scrub_opts;
971                 }
972                 ;
973
974 scrub_opts_l    : scrub_opts_l scrub_opt
975                 | scrub_opt
976                 ;
977
978 scrub_opt       : NODF  {
979                         if (scrub_opts.nodf) {
980                                 yyerror("no-df cannot be respecified");
981                                 YYERROR;
982                         }
983                         scrub_opts.nodf = 1;
984                 }
985                 | MINTTL number {
986                         if (scrub_opts.marker & SOM_MINTTL) {
987                                 yyerror("min-ttl cannot be respecified");
988                                 YYERROR;
989                         }
990                         if ($2 > 255) {
991                                 yyerror("illegal min-ttl value %d", $2);
992                                 YYERROR;
993                         }
994                         scrub_opts.marker |= SOM_MINTTL;
995                         scrub_opts.minttl = $2;
996                 }
997                 | MAXMSS number {
998                         if (scrub_opts.marker & SOM_MAXMSS) {
999                                 yyerror("max-mss cannot be respecified");
1000                                 YYERROR;
1001                         }
1002                         if ($2 > 65535) {
1003                                 yyerror("illegal max-mss value %d", $2);
1004                                 YYERROR;
1005                         }
1006                         scrub_opts.marker |= SOM_MAXMSS;
1007                         scrub_opts.maxmss = $2;
1008                 }
1009                 | fragcache {
1010                         if (scrub_opts.marker & SOM_FRAGCACHE) {
1011                                 yyerror("fragcache cannot be respecified");
1012                                 YYERROR;
1013                         }
1014                         scrub_opts.marker |= SOM_FRAGCACHE;
1015                         scrub_opts.fragcache = $1;
1016                 }
1017                 | REASSEMBLE STRING {
1018                         if (strcasecmp($2, "tcp") != 0) {
1019                                 yyerror("scrub reassemble supports only tcp, "
1020                                     "not '%s'", $2);
1021                                 free($2);
1022                                 YYERROR;
1023                         }
1024                         free($2);
1025                         if (scrub_opts.reassemble_tcp) {
1026                                 yyerror("reassemble tcp cannot be respecified");
1027                                 YYERROR;
1028                         }
1029                         scrub_opts.reassemble_tcp = 1;
1030                 }
1031                 | RANDOMID {
1032                         if (scrub_opts.randomid) {
1033                                 yyerror("random-id cannot be respecified");
1034                                 YYERROR;
1035                         }
1036                         scrub_opts.randomid = 1;
1037                 }
1038                 | RTABLE number                         {
1039                         scrub_opts.rtableid = $2;
1040                 }
1041                 ;
1042
1043 fragcache       : FRAGMENT REASSEMBLE   { $$ = 0; /* default */ }
1044                 | FRAGMENT FRAGCROP     { $$ = PFRULE_FRAGCROP; }
1045                 | FRAGMENT FRAGDROP     { $$ = PFRULE_FRAGDROP; }
1046                 ;
1047
1048 antispoof       : ANTISPOOF logquick antispoof_ifspc af antispoof_opts {
1049                         struct pf_rule           r;
1050                         struct node_host        *h = NULL, *hh;
1051                         struct node_if          *i, *j;
1052
1053                         if (check_rulestate(PFCTL_STATE_FILTER))
1054                                 YYERROR;
1055
1056                         for (i = $3; i; i = i->next) {
1057                                 bzero(&r, sizeof(r));
1058
1059                                 r.action = PF_DROP;
1060                                 r.direction = PF_IN;
1061                                 r.log = $2.log;
1062                                 r.logif = $2.logif;
1063                                 r.quick = $2.quick;
1064                                 r.af = $4;
1065                                 if (rule_label(&r, $5.label))
1066                                         YYERROR;
1067                                 r.rtableid = $5.rtableid;
1068                                 j = calloc(1, sizeof(struct node_if));
1069                                 if (j == NULL)
1070                                         err(1, "antispoof: calloc");
1071                                 if (strlcpy(j->ifname, i->ifname,
1072                                     sizeof(j->ifname)) >= sizeof(j->ifname)) {
1073                                         free(j);
1074                                         yyerror("interface name too long");
1075                                         YYERROR;
1076                                 }
1077                                 j->not = 1;
1078                                 if (i->dynamic) {
1079                                         h = calloc(1, sizeof(*h));
1080                                         if (h == NULL)
1081                                                 err(1, "address: calloc");
1082                                         h->addr.type = PF_ADDR_DYNIFTL;
1083                                         set_ipmask(h, 128);
1084                                         if (strlcpy(h->addr.v.ifname, i->ifname,
1085                                             sizeof(h->addr.v.ifname)) >=
1086                                             sizeof(h->addr.v.ifname)) {
1087                                                 free(h);
1088                                                 yyerror(
1089                                                     "interface name too long");
1090                                                 YYERROR;
1091                                         }
1092                                         hh = malloc(sizeof(*hh));
1093                                         if (hh == NULL)
1094                                                  err(1, "address: malloc");
1095                                         bcopy(h, hh, sizeof(*hh));
1096                                         h->addr.iflags = PFI_AFLAG_NETWORK;
1097                                 } else {
1098                                         h = ifa_lookup(j->ifname,
1099                                             PFI_AFLAG_NETWORK);
1100                                         hh = NULL;
1101                                 }
1102
1103                                 if (h != NULL)
1104                                         expand_rule(&r, j, NULL, NULL, NULL, h,
1105                                             NULL, NULL, NULL, NULL, NULL,
1106                                             NULL, "");
1107
1108                                 if ((i->ifa_flags & IFF_LOOPBACK) == 0) {
1109                                         bzero(&r, sizeof(r));
1110
1111                                         r.action = PF_DROP;
1112                                         r.direction = PF_IN;
1113                                         r.log = $2.log;
1114                                         r.quick = $2.quick;
1115                                         r.af = $4;
1116                                         if (rule_label(&r, $5.label))
1117                                                 YYERROR;
1118                                         r.rtableid = $5.rtableid;
1119                                         if (hh != NULL)
1120                                                 h = hh;
1121                                         else
1122                                                 h = ifa_lookup(i->ifname, 0);
1123                                         if (h != NULL)
1124                                                 expand_rule(&r, NULL, NULL,
1125                                                     NULL, NULL, h, NULL, NULL,
1126                                                     NULL, NULL, NULL, NULL, "");
1127                                 } else
1128                                         free(hh);
1129                         }
1130                         free($5.label);
1131                 }
1132                 ;
1133
1134 antispoof_ifspc : FOR antispoof_if              { $$ = $2; }
1135                 | FOR '{' antispoof_iflst '}'   { $$ = $3; }
1136                 ;
1137
1138 antispoof_iflst : antispoof_if                          { $$ = $1; }
1139                 | antispoof_iflst comma antispoof_if    {
1140                         $1->tail->next = $3;
1141                         $1->tail = $3;
1142                         $$ = $1;
1143                 }
1144                 ;
1145
1146 antispoof_if  : if_item                         { $$ = $1; }
1147                 | '(' if_item ')'               {
1148                         $2->dynamic = 1;
1149                         $$ = $2;
1150                 }
1151                 ;
1152
1153 antispoof_opts  :       {
1154                                 bzero(&antispoof_opts, sizeof antispoof_opts);
1155                                 antispoof_opts.rtableid = -1;
1156                         }
1157                     antispoof_opts_l
1158                         { $$ = antispoof_opts; }
1159                 | /* empty */   {
1160                         bzero(&antispoof_opts, sizeof antispoof_opts);
1161                         antispoof_opts.rtableid = -1;
1162                         $$ = antispoof_opts;
1163                 }
1164                 ;
1165
1166 antispoof_opts_l        : antispoof_opts_l antispoof_opt
1167                         | antispoof_opt
1168                         ;
1169
1170 antispoof_opt   : label {
1171                         if (antispoof_opts.label) {
1172                                 yyerror("label cannot be redefined");
1173                                 YYERROR;
1174                         }
1175                         antispoof_opts.label = $1;
1176                 }
1177                 | RTABLE number                         {
1178                         antispoof_opts.rtableid = $2;
1179                 }
1180                 ;
1181
1182 not             : '!'           { $$ = 1; }
1183                 | /* empty */   { $$ = 0; }
1184                 ;
1185
1186 tabledef        : TABLE '<' STRING '>' table_opts {
1187                         struct node_host         *h, *nh;
1188                         struct node_tinit        *ti, *nti;
1189
1190                         if (strlen($3) >= PF_TABLE_NAME_SIZE) {
1191                                 yyerror("table name too long, max %d chars",
1192                                     PF_TABLE_NAME_SIZE - 1);
1193                                 free($3);
1194                                 YYERROR;
1195                         }
1196                         if (pf->loadopt & PFCTL_FLAG_TABLE)
1197                                 if (process_tabledef($3, &$5)) {
1198                                         free($3);
1199                                         YYERROR;
1200                                 }
1201                         free($3);
1202                         for (ti = SIMPLEQ_FIRST(&$5.init_nodes);
1203                             ti != SIMPLEQ_END(&$5.init_nodes); ti = nti) {
1204                                 if (ti->file)
1205                                         free(ti->file);
1206                                 for (h = ti->host; h != NULL; h = nh) {
1207                                         nh = h->next;
1208                                         free(h);
1209                                 }
1210                                 nti = SIMPLEQ_NEXT(ti, entries);
1211                                 free(ti);
1212                         }
1213                 }
1214                 ;
1215
1216 table_opts      :       {
1217                         bzero(&table_opts, sizeof table_opts);
1218                         SIMPLEQ_INIT(&table_opts.init_nodes);
1219                 }
1220                     table_opts_l
1221                         { $$ = table_opts; }
1222                 | /* empty */
1223                         {
1224                         bzero(&table_opts, sizeof table_opts);
1225                         SIMPLEQ_INIT(&table_opts.init_nodes);
1226                         $$ = table_opts;
1227                 }
1228                 ;
1229
1230 table_opts_l    : table_opts_l table_opt
1231                 | table_opt
1232                 ;
1233
1234 table_opt       : STRING                {
1235                         if (!strcmp($1, "const"))
1236                                 table_opts.flags |= PFR_TFLAG_CONST;
1237                         else if (!strcmp($1, "persist"))
1238                                 table_opts.flags |= PFR_TFLAG_PERSIST;
1239                         else {
1240                                 yyerror("invalid table option '%s'", $1);
1241                                 free($1);
1242                                 YYERROR;
1243                         }
1244                         free($1);
1245                 }
1246                 | '{' '}'               { table_opts.init_addr = 1; }
1247                 | '{' host_list '}'     {
1248                         struct node_host        *n;
1249                         struct node_tinit       *ti;
1250
1251                         for (n = $2; n != NULL; n = n->next) {
1252                                 switch (n->addr.type) {
1253                                 case PF_ADDR_ADDRMASK:
1254                                         continue; /* ok */
1255                                 case PF_ADDR_DYNIFTL:
1256                                         yyerror("dynamic addresses are not "
1257                                             "permitted inside tables");
1258                                         break;
1259                                 case PF_ADDR_TABLE:
1260                                         yyerror("tables cannot contain tables");
1261                                         break;
1262                                 case PF_ADDR_NOROUTE:
1263                                         yyerror("\"no-route\" is not permitted "
1264                                             "inside tables");
1265                                         break;
1266                                 case PF_ADDR_URPFFAILED:
1267                                         yyerror("\"urpf-failed\" is not "
1268                                             "permitted inside tables");
1269                                         break;
1270                                 default:
1271                                         yyerror("unknown address type %d",
1272                                             n->addr.type);
1273                                 }
1274                                 YYERROR;
1275                         }
1276                         if (!(ti = calloc(1, sizeof(*ti))))
1277                                 err(1, "table_opt: calloc");
1278                         ti->host = $2;
1279                         SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1280                             entries);
1281                         table_opts.init_addr = 1;
1282                 }
1283                 | FILENAME STRING       {
1284                         struct node_tinit       *ti;
1285
1286                         if (!(ti = calloc(1, sizeof(*ti))))
1287                                 err(1, "table_opt: calloc");
1288                         ti->file = $2;
1289                         SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1290                             entries);
1291                         table_opts.init_addr = 1;
1292                 }
1293                 ;
1294
1295 altqif          : ALTQ interface queue_opts QUEUE qassign {
1296                         struct pf_altq  a;
1297
1298                         if (check_rulestate(PFCTL_STATE_QUEUE))
1299                                 YYERROR;
1300
1301                         memset(&a, 0, sizeof(a));
1302                         if ($3.scheduler.qtype == ALTQT_NONE) {
1303                                 yyerror("no scheduler specified!");
1304                                 YYERROR;
1305                         }
1306                         a.scheduler = $3.scheduler.qtype;
1307                         a.qlimit = $3.qlimit;
1308                         a.tbrsize = $3.tbrsize;
1309                         if ($5 == NULL) {
1310                                 yyerror("no child queues specified");
1311                                 YYERROR;
1312                         }
1313                         if (expand_altq(&a, $2, $5, $3.queue_bwspec,
1314                             &$3.scheduler))
1315                                 YYERROR;
1316                 }
1317                 ;
1318
1319 queuespec       : QUEUE STRING interface queue_opts qassign {
1320                         struct pf_altq  a;
1321
1322                         if (check_rulestate(PFCTL_STATE_QUEUE)) {
1323                                 free($2);
1324                                 YYERROR;
1325                         }
1326
1327                         memset(&a, 0, sizeof(a));
1328
1329                         if (strlcpy(a.qname, $2, sizeof(a.qname)) >=
1330                             sizeof(a.qname)) {
1331                                 yyerror("queue name too long (max "
1332                                     "%d chars)", PF_QNAME_SIZE-1);
1333                                 free($2);
1334                                 YYERROR;
1335                         }
1336                         free($2);
1337                         if ($4.tbrsize) {
1338                                 yyerror("cannot specify tbrsize for queue");
1339                                 YYERROR;
1340                         }
1341                         if ($4.priority > 255) {
1342                                 yyerror("priority out of range: max 255");
1343                                 YYERROR;
1344                         }
1345                         a.priority = $4.priority;
1346                         a.qlimit = $4.qlimit;
1347                         a.scheduler = $4.scheduler.qtype;
1348                         if (expand_queue(&a, $3, $5, $4.queue_bwspec,
1349                             &$4.scheduler)) {
1350                                 yyerror("errors in queue definition");
1351                                 YYERROR;
1352                         }
1353                 }
1354                 ;
1355
1356 queue_opts      :       {
1357                         bzero(&queue_opts, sizeof queue_opts);
1358                         queue_opts.priority = DEFAULT_PRIORITY;
1359                         queue_opts.qlimit = DEFAULT_QLIMIT;
1360                         queue_opts.scheduler.qtype = ALTQT_NONE;
1361                         queue_opts.queue_bwspec.bw_percent = 100;
1362                 }
1363                     queue_opts_l
1364                         { $$ = queue_opts; }
1365                 | /* empty */ {
1366                         bzero(&queue_opts, sizeof queue_opts);
1367                         queue_opts.priority = DEFAULT_PRIORITY;
1368                         queue_opts.qlimit = DEFAULT_QLIMIT;
1369                         queue_opts.scheduler.qtype = ALTQT_NONE;
1370                         queue_opts.queue_bwspec.bw_percent = 100;
1371                         $$ = queue_opts;
1372                 }
1373                 ;
1374
1375 queue_opts_l    : queue_opts_l queue_opt
1376                 | queue_opt
1377                 ;
1378
1379 queue_opt       : BANDWIDTH bandwidth   {
1380                         if (queue_opts.marker & QOM_BWSPEC) {
1381                                 yyerror("bandwidth cannot be respecified");
1382                                 YYERROR;
1383                         }
1384                         queue_opts.marker |= QOM_BWSPEC;
1385                         queue_opts.queue_bwspec = $2;
1386                 }
1387                 | PRIORITY number       {
1388                         if (queue_opts.marker & QOM_PRIORITY) {
1389                                 yyerror("priority cannot be respecified");
1390                                 YYERROR;
1391                         }
1392                         if ($2 > 255) {
1393                                 yyerror("priority out of range: max 255");
1394                                 YYERROR;
1395                         }
1396                         queue_opts.marker |= QOM_PRIORITY;
1397                         queue_opts.priority = $2;
1398                 }
1399                 | QLIMIT number {
1400                         if (queue_opts.marker & QOM_QLIMIT) {
1401                                 yyerror("qlimit cannot be respecified");
1402                                 YYERROR;
1403                         }
1404                         if ($2 > 65535) {
1405                                 yyerror("qlimit out of range: max 65535");
1406                                 YYERROR;
1407                         }
1408                         queue_opts.marker |= QOM_QLIMIT;
1409                         queue_opts.qlimit = $2;
1410                 }
1411                 | scheduler     {
1412                         if (queue_opts.marker & QOM_SCHEDULER) {
1413                                 yyerror("scheduler cannot be respecified");
1414                                 YYERROR;
1415                         }
1416                         queue_opts.marker |= QOM_SCHEDULER;
1417                         queue_opts.scheduler = $1;
1418                 }
1419                 | TBRSIZE number        {
1420                         if (queue_opts.marker & QOM_TBRSIZE) {
1421                                 yyerror("tbrsize cannot be respecified");
1422                                 YYERROR;
1423                         }
1424                         if ($2 > 65535) {
1425                                 yyerror("tbrsize too big: max 65535");
1426                                 YYERROR;
1427                         }
1428                         queue_opts.marker |= QOM_TBRSIZE;
1429                         queue_opts.tbrsize = $2;
1430                 }
1431                 ;
1432
1433 bandwidth       : STRING {
1434                         double   bps;
1435                         char    *cp;
1436
1437                         $$.bw_percent = 0;
1438
1439                         bps = strtod($1, &cp);
1440                         if (cp != NULL) {
1441                                 if (!strcmp(cp, "b"))
1442                                         ; /* nothing */
1443                                 else if (!strcmp(cp, "Kb"))
1444                                         bps *= 1000;
1445                                 else if (!strcmp(cp, "Mb"))
1446                                         bps *= 1000 * 1000;
1447                                 else if (!strcmp(cp, "Gb"))
1448                                         bps *= 1000 * 1000 * 1000;
1449                                 else if (!strcmp(cp, "%")) {
1450                                         if (bps < 0 || bps > 100) {
1451                                                 yyerror("bandwidth spec "
1452                                                     "out of range");
1453                                                 free($1);
1454                                                 YYERROR;
1455                                         }
1456                                         $$.bw_percent = bps;
1457                                         bps = 0;
1458                                 } else {
1459                                         yyerror("unknown unit %s", cp);
1460                                         free($1);
1461                                         YYERROR;
1462                                 }
1463                         }
1464                         free($1);
1465                         $$.bw_absolute = (u_int32_t)bps;
1466                 }
1467                 ;
1468
1469 scheduler       : CBQ                           {
1470                         $$.qtype = ALTQT_CBQ;
1471                         $$.data.cbq_opts.flags = 0;
1472                 }
1473                 | CBQ '(' cbqflags_list ')'     {
1474                         $$.qtype = ALTQT_CBQ;
1475                         $$.data.cbq_opts.flags = $3;
1476                 }
1477                 | PRIQ                          {
1478                         $$.qtype = ALTQT_PRIQ;
1479                         $$.data.priq_opts.flags = 0;
1480                 }
1481                 | PRIQ '(' priqflags_list ')'   {
1482                         $$.qtype = ALTQT_PRIQ;
1483                         $$.data.priq_opts.flags = $3;
1484                 }
1485                 | HFSC                          {
1486                         $$.qtype = ALTQT_HFSC;
1487                         bzero(&$$.data.hfsc_opts,
1488                             sizeof(struct node_hfsc_opts));
1489                 }
1490                 | HFSC '(' hfsc_opts ')'        {
1491                         $$.qtype = ALTQT_HFSC;
1492                         $$.data.hfsc_opts = $3;
1493                 }
1494                 | FAIRQ                         {
1495                         $$.qtype = ALTQT_FAIRQ;
1496                         bzero(&$$.data.fairq_opts,
1497                             sizeof(struct node_fairq_opts));
1498                 }
1499                 | FAIRQ '(' fairq_opts ')'      {
1500                         $$.qtype = ALTQT_FAIRQ;
1501                         $$.data.fairq_opts = $3;
1502                 }
1503                 ;
1504
1505 cbqflags_list   : cbqflags_item                         { $$ |= $1; }
1506                 | cbqflags_list comma cbqflags_item     { $$ |= $3; }
1507                 ;
1508
1509 cbqflags_item   : STRING        {
1510                         if (!strcmp($1, "default"))
1511                                 $$ = CBQCLF_DEFCLASS;
1512                         else if (!strcmp($1, "borrow"))
1513                                 $$ = CBQCLF_BORROW;
1514                         else if (!strcmp($1, "red"))
1515                                 $$ = CBQCLF_RED;
1516                         else if (!strcmp($1, "ecn"))
1517                                 $$ = CBQCLF_RED|CBQCLF_ECN;
1518                         else if (!strcmp($1, "rio"))
1519                                 $$ = CBQCLF_RIO;
1520                         else {
1521                                 yyerror("unknown cbq flag \"%s\"", $1);
1522                                 free($1);
1523                                 YYERROR;
1524                         }
1525                         free($1);
1526                 }
1527                 ;
1528
1529 priqflags_list  : priqflags_item                        { $$ |= $1; }
1530                 | priqflags_list comma priqflags_item   { $$ |= $3; }
1531                 ;
1532
1533 priqflags_item  : STRING        {
1534                         if (!strcmp($1, "default"))
1535                                 $$ = PRCF_DEFAULTCLASS;
1536                         else if (!strcmp($1, "red"))
1537                                 $$ = PRCF_RED;
1538                         else if (!strcmp($1, "ecn"))
1539                                 $$ = PRCF_RED|PRCF_ECN;
1540                         else if (!strcmp($1, "rio"))
1541                                 $$ = PRCF_RIO;
1542                         else {
1543                                 yyerror("unknown priq flag \"%s\"", $1);
1544                                 free($1);
1545                                 YYERROR;
1546                         }
1547                         free($1);
1548                 }
1549                 ;
1550
1551 hfsc_opts       :       {
1552                                 bzero(&hfsc_opts,
1553                                     sizeof(struct node_hfsc_opts));
1554                         }
1555                     hfscopts_list                               {
1556                         $$ = hfsc_opts;
1557                 }
1558                 ;
1559
1560 hfscopts_list   : hfscopts_item
1561                 | hfscopts_list comma hfscopts_item
1562                 ;
1563
1564 hfscopts_item   : LINKSHARE bandwidth                           {
1565                         if (hfsc_opts.linkshare.used) {
1566                                 yyerror("linkshare already specified");
1567                                 YYERROR;
1568                         }
1569                         hfsc_opts.linkshare.m2 = $2;
1570                         hfsc_opts.linkshare.used = 1;
1571                 }
1572                 | LINKSHARE '(' bandwidth comma number comma bandwidth ')'
1573                     {
1574                         if (hfsc_opts.linkshare.used) {
1575                                 yyerror("linkshare already specified");
1576                                 YYERROR;
1577                         }
1578                         hfsc_opts.linkshare.m1 = $3;
1579                         hfsc_opts.linkshare.d = $5;
1580                         hfsc_opts.linkshare.m2 = $7;
1581                         hfsc_opts.linkshare.used = 1;
1582                 }
1583                 | REALTIME bandwidth                            {
1584                         if (hfsc_opts.realtime.used) {
1585                                 yyerror("realtime already specified");
1586                                 YYERROR;
1587                         }
1588                         hfsc_opts.realtime.m2 = $2;
1589                         hfsc_opts.realtime.used = 1;
1590                 }
1591                 | REALTIME '(' bandwidth comma number comma bandwidth ')'
1592                     {
1593                         if (hfsc_opts.realtime.used) {
1594                                 yyerror("realtime already specified");
1595                                 YYERROR;
1596                         }
1597                         hfsc_opts.realtime.m1 = $3;
1598                         hfsc_opts.realtime.d = $5;
1599                         hfsc_opts.realtime.m2 = $7;
1600                         hfsc_opts.realtime.used = 1;
1601                 }
1602                 | UPPERLIMIT bandwidth                          {
1603                         if (hfsc_opts.upperlimit.used) {
1604                                 yyerror("upperlimit already specified");
1605                                 YYERROR;
1606                         }
1607                         hfsc_opts.upperlimit.m2 = $2;
1608                         hfsc_opts.upperlimit.used = 1;
1609                 }
1610                 | UPPERLIMIT '(' bandwidth comma number comma bandwidth ')'
1611                     {
1612                         if (hfsc_opts.upperlimit.used) {
1613                                 yyerror("upperlimit already specified");
1614                                 YYERROR;
1615                         }
1616                         hfsc_opts.upperlimit.m1 = $3;
1617                         hfsc_opts.upperlimit.d = $5;
1618                         hfsc_opts.upperlimit.m2 = $7;
1619                         hfsc_opts.upperlimit.used = 1;
1620                 }
1621                 | STRING        {
1622                         if (!strcmp($1, "default"))
1623                                 hfsc_opts.flags |= HFCF_DEFAULTCLASS;
1624                         else if (!strcmp($1, "red"))
1625                                 hfsc_opts.flags |= HFCF_RED;
1626                         else if (!strcmp($1, "ecn"))
1627                                 hfsc_opts.flags |= HFCF_RED|HFCF_ECN;
1628                         else if (!strcmp($1, "rio"))
1629                                 hfsc_opts.flags |= HFCF_RIO;
1630                         else {
1631                                 yyerror("unknown hfsc flag \"%s\"", $1);
1632                                 free($1);
1633                                 YYERROR;
1634                         }
1635                         free($1);
1636                 }
1637                 ;
1638
1639 fairq_opts      :       {
1640                                 bzero(&fairq_opts,
1641                                     sizeof(struct node_fairq_opts));
1642                         }
1643                     fairqopts_list                              {
1644                         $$ = fairq_opts;
1645                 }
1646                 ;
1647
1648 fairqopts_list  : fairqopts_item
1649                 | fairqopts_list comma fairqopts_item
1650                 ;
1651
1652 fairqopts_item  : LINKSHARE bandwidth                           {
1653                         if (fairq_opts.linkshare.used) {
1654                                 yyerror("linkshare already specified");
1655                                 YYERROR;
1656                         }
1657                         fairq_opts.linkshare.m2 = $2;
1658                         fairq_opts.linkshare.used = 1;
1659                 }
1660                 | LINKSHARE '(' bandwidth number bandwidth ')'  {
1661                         if (fairq_opts.linkshare.used) {
1662                                 yyerror("linkshare already specified");
1663                                 YYERROR;
1664                         }
1665                         fairq_opts.linkshare.m1 = $3;
1666                         fairq_opts.linkshare.d = $4;
1667                         fairq_opts.linkshare.m2 = $5;
1668                         fairq_opts.linkshare.used = 1;
1669                 }
1670                 | HOGS bandwidth {
1671                         fairq_opts.hogs_bw = $2;
1672                 }
1673                 | BUCKETS number {
1674                         fairq_opts.nbuckets = $2;
1675                 }
1676                 | STRING        {
1677                         if (!strcmp($1, "default"))
1678                                 fairq_opts.flags |= FARF_DEFAULTCLASS;
1679                         else if (!strcmp($1, "red"))
1680                                 fairq_opts.flags |= FARF_RED;
1681                         else if (!strcmp($1, "ecn"))
1682                                 fairq_opts.flags |= FARF_RED|FARF_ECN;
1683                         else if (!strcmp($1, "rio"))
1684                                 fairq_opts.flags |= FARF_RIO;
1685                         else {
1686                                 yyerror("unknown fairq flag \"%s\"", $1);
1687                                 free($1);
1688                                 YYERROR;
1689                         }
1690                         free($1);
1691                 }
1692                 ;
1693
1694 qassign         : /* empty */           { $$ = NULL; }
1695                 | qassign_item          { $$ = $1; }
1696                 | '{' qassign_list '}'  { $$ = $2; }
1697                 ;
1698
1699 qassign_list    : qassign_item                  { $$ = $1; }
1700                 | qassign_list comma qassign_item       {
1701                         $1->tail->next = $3;
1702                         $1->tail = $3;
1703                         $$ = $1;
1704                 }
1705                 ;
1706
1707 qassign_item    : STRING                        {
1708                         $$ = calloc(1, sizeof(struct node_queue));
1709                         if ($$ == NULL)
1710                                 err(1, "qassign_item: calloc");
1711                         if (strlcpy($$->queue, $1, sizeof($$->queue)) >=
1712                             sizeof($$->queue)) {
1713                                 yyerror("queue name '%s' too long (max "
1714                                     "%d chars)", $1, sizeof($$->queue)-1);
1715                                 free($1);
1716                                 free($$);
1717                                 YYERROR;
1718                         }
1719                         free($1);
1720                         $$->next = NULL;
1721                         $$->tail = $$;
1722                 }
1723                 ;
1724
1725 pfrule          : action dir logquick interface route af proto fromto
1726                     filter_opts
1727                 {
1728                         struct pf_rule           r;
1729                         struct node_state_opt   *o;
1730                         struct node_proto       *proto;
1731                         int                      srctrack = 0;
1732                         int                      statelock = 0;
1733                         int                      adaptive = 0;
1734                         int                      dofree;
1735
1736                         if (check_rulestate(PFCTL_STATE_FILTER))
1737                                 YYERROR;
1738
1739                         memset(&r, 0, sizeof(r));
1740
1741                         r.action = $1.b1;
1742                         switch ($1.b2) {
1743                         case PFRULE_RETURNRST:
1744                                 r.rule_flag |= PFRULE_RETURNRST;
1745                                 r.return_ttl = $1.w;
1746                                 break;
1747                         case PFRULE_RETURNICMP:
1748                                 r.rule_flag |= PFRULE_RETURNICMP;
1749                                 r.return_icmp = $1.w;
1750                                 r.return_icmp6 = $1.w2;
1751                                 break;
1752                         case PFRULE_RETURN:
1753                                 r.rule_flag |= PFRULE_RETURN;
1754                                 r.return_icmp = $1.w;
1755                                 r.return_icmp6 = $1.w2;
1756                                 break;
1757                         }
1758                         r.direction = $2;
1759                         r.log = $3.log;
1760                         r.logif = $3.logif;
1761                         r.quick = $3.quick;
1762                         r.prob = $9.prob;
1763                         r.rtableid = $9.rtableid;
1764
1765                         r.af = $6;
1766                         if ($9.tag)
1767                                 if (strlcpy(r.tagname, $9.tag,
1768                                     PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1769                                         yyerror("tag too long, max %u chars",
1770                                             PF_TAG_NAME_SIZE - 1);
1771                                         YYERROR;
1772                                 }
1773                         if ($9.match_tag)
1774                                 if (strlcpy(r.match_tagname, $9.match_tag,
1775                                     PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1776                                         yyerror("tag too long, max %u chars",
1777                                             PF_TAG_NAME_SIZE - 1);
1778                                         YYERROR;
1779                                 }
1780                         r.match_tag_not = $9.match_tag_not;
1781                         if (rule_label(&r, $9.label))
1782                                 YYERROR;
1783                         free($9.label);
1784                         r.flags = $9.flags.b1;
1785                         r.flagset = $9.flags.b2;
1786                         if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
1787                                 yyerror("flags always false");
1788                                 YYERROR;
1789                         }
1790                         if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
1791                                 for (proto = $7; proto != NULL &&
1792                                     proto->proto != IPPROTO_TCP;
1793                                     proto = proto->next)
1794                                         ;       /* nothing */
1795                                 if (proto == NULL && $7 != NULL) {
1796                                         if ($9.flags.b1 || $9.flags.b2)
1797                                                 yyerror(
1798                                                     "flags only apply to tcp");
1799                                         if ($8.src_os)
1800                                                 yyerror(
1801                                                     "OS fingerprinting only "
1802                                                     "apply to tcp");
1803                                         YYERROR;
1804                                 }
1805 #if 0
1806                                 if (($9.flags.b1 & parse_flags("S")) == 0 &&
1807                                     $8.src_os) {
1808                                         yyerror("OS fingerprinting requires "
1809                                             "the SYN TCP flag (flags S/SA)");
1810                                         YYERROR;
1811                                 }
1812 #endif
1813                         }
1814
1815                         r.tos = $9.tos;
1816
1817                         if (filter_opts.marker & FOM_KEEP) {
1818                                 r.keep_state = $9.keep.action;
1819                                 o = $9.keep.options;
1820                                 dofree = 1;
1821                         } else if (r.action == PF_PASS) {
1822                                 r.keep_state = default_keeppolicy_action;
1823                                 o = default_keeppolicy_options;
1824                                 dofree = 0;
1825                         } else {
1826                                 o = NULL;
1827                                 dofree = 0;
1828                         }
1829                         while (o) {
1830                                 struct node_state_opt   *p = o;
1831
1832                                 switch (o->type) {
1833                                 case PF_STATE_OPT_MAX:
1834                                         if (r.max_states) {
1835                                                 yyerror("state option 'max' "
1836                                                     "multiple definitions");
1837                                                 YYERROR;
1838                                         }
1839                                         r.max_states = o->data.max_states;
1840                                         break;
1841                                 case PF_STATE_OPT_NOSYNC:
1842                                         if (r.rule_flag & PFRULE_NOSYNC) {
1843                                                 yyerror("state option 'sync' "
1844                                                     "multiple definitions");
1845                                                 YYERROR;
1846                                         }
1847                                         r.rule_flag |= PFRULE_NOSYNC;
1848                                         break;
1849                                 case PF_STATE_OPT_SRCTRACK:
1850                                         if (srctrack) {
1851                                                 yyerror("state option "
1852                                                     "'source-track' "
1853                                                     "multiple definitions");
1854                                                 YYERROR;
1855                                         }
1856                                         srctrack =  o->data.src_track;
1857                                         r.rule_flag |= PFRULE_SRCTRACK;
1858                                         break;
1859                                 case PF_STATE_OPT_MAX_SRC_STATES:
1860                                         if (r.max_src_states) {
1861                                                 yyerror("state option "
1862                                                     "'max-src-states' "
1863                                                     "multiple definitions");
1864                                                 YYERROR;
1865                                         }
1866                                         if (o->data.max_src_states == 0) {
1867                                                 yyerror("'max-src-states' must "
1868                                                     "be > 0");
1869                                                 YYERROR;
1870                                         }
1871                                         r.max_src_states =
1872                                             o->data.max_src_states;
1873                                         r.rule_flag |= PFRULE_SRCTRACK;
1874                                         break;
1875                                 case PF_STATE_OPT_OVERLOAD:
1876                                         if (r.overload_tblname[0]) {
1877                                                 yyerror("multiple 'overload' "
1878                                                     "table definitions");
1879                                                 YYERROR;
1880                                         }
1881                                         if (strlcpy(r.overload_tblname,
1882                                             o->data.overload.tblname,
1883                                             PF_TABLE_NAME_SIZE) >=
1884                                             PF_TABLE_NAME_SIZE) {
1885                                                 yyerror("state option: "
1886                                                     "strlcpy");
1887                                                 YYERROR;
1888                                         }
1889                                         r.flush = o->data.overload.flush;
1890                                         break;
1891                                 case PF_STATE_OPT_MAX_SRC_CONN:
1892                                         if (r.max_src_conn) {
1893                                                 yyerror("state option "
1894                                                     "'max-src-conn' "
1895                                                     "multiple definitions");
1896                                                 YYERROR;
1897                                         }
1898                                         if (o->data.max_src_conn == 0) {
1899                                                 yyerror("'max-src-conn' "
1900                                                     "must be > 0");
1901                                                 YYERROR;
1902                                         }
1903                                         r.max_src_conn =
1904                                             o->data.max_src_conn;
1905                                         r.rule_flag |= PFRULE_SRCTRACK |
1906                                             PFRULE_RULESRCTRACK;
1907                                         break;
1908                                 case PF_STATE_OPT_MAX_SRC_CONN_RATE:
1909                                         if (r.max_src_conn_rate.limit) {
1910                                                 yyerror("state option "
1911                                                     "'max-src-conn-rate' "
1912                                                     "multiple definitions");
1913                                                 YYERROR;
1914                                         }
1915                                         if (!o->data.max_src_conn_rate.limit ||
1916                                             !o->data.max_src_conn_rate.seconds) {
1917                                                 yyerror("'max-src-conn-rate' "
1918                                                     "values must be > 0");
1919                                                 YYERROR;
1920                                         }
1921                                         if (o->data.max_src_conn_rate.limit >
1922                                             PF_THRESHOLD_MAX) {
1923                                                 yyerror("'max-src-conn-rate' "
1924                                                     "maximum rate must be < %u",
1925                                                     PF_THRESHOLD_MAX);
1926                                                 YYERROR;
1927                                         }
1928                                         r.max_src_conn_rate.limit =
1929                                             o->data.max_src_conn_rate.limit;
1930                                         r.max_src_conn_rate.seconds =
1931                                             o->data.max_src_conn_rate.seconds;
1932                                         r.rule_flag |= PFRULE_SRCTRACK |
1933                                             PFRULE_RULESRCTRACK;
1934                                         break;
1935                                 case PF_STATE_OPT_MAX_SRC_NODES:
1936                                         if (r.max_src_nodes) {
1937                                                 yyerror("state option "
1938                                                     "'max-src-nodes' "
1939                                                     "multiple definitions");
1940                                                 YYERROR;
1941                                         }
1942                                         if (o->data.max_src_nodes == 0) {
1943                                                 yyerror("'max-src-nodes' must "
1944                                                     "be > 0");
1945                                                 YYERROR;
1946                                         }
1947                                         r.max_src_nodes =
1948                                             o->data.max_src_nodes;
1949                                         r.rule_flag |= PFRULE_SRCTRACK |
1950                                             PFRULE_RULESRCTRACK;
1951                                         break;
1952                                 case PF_STATE_OPT_STATELOCK:
1953                                         if (statelock) {
1954                                                 yyerror("state locking option: "
1955                                                     "multiple definitions");
1956                                                 YYERROR;
1957                                         }
1958                                         statelock = 1;
1959                                         r.rule_flag |= o->data.statelock;
1960                                         break;
1961                                 case PF_STATE_OPT_TIMEOUT:
1962                                         if (o->data.timeout.number ==
1963                                             PFTM_ADAPTIVE_START ||
1964                                             o->data.timeout.number ==
1965                                             PFTM_ADAPTIVE_END)
1966                                                 adaptive = 1;
1967                                         if (r.timeout[o->data.timeout.number]) {
1968                                                 yyerror("state timeout %s "
1969                                                     "multiple definitions",
1970                                                     pf_timeouts[o->data.
1971                                                     timeout.number].name);
1972                                                 YYERROR;
1973                                         }
1974                                         r.timeout[o->data.timeout.number] =
1975                                             o->data.timeout.seconds;
1976                                         break;
1977                                 case PF_STATE_OPT_PICKUPS:
1978                                         r.pickup_mode = o->data.pickup_mode;
1979                                         break;
1980                                 }
1981                                 o = o->next;
1982                                 if (dofree)
1983                                         free(p);
1984                         }
1985
1986 #if 0
1987                         /* 'flags S/SA' by default on stateful rules */
1988                         if (!r.action && !r.flags && !r.flagset &&
1989                             !$9.fragment && !($9.marker & FOM_FLAGS) &&
1990                             r.keep_state) {
1991                                 r.flags = parse_flags("S");
1992                                 r.flagset =  parse_flags("SA");
1993                         }
1994 #endif
1995                         if (!adaptive && r.max_states) {
1996                                 r.timeout[PFTM_ADAPTIVE_START] =
1997                                     (r.max_states / 10) * 6;
1998                                 r.timeout[PFTM_ADAPTIVE_END] =
1999                                     (r.max_states / 10) * 12;
2000                         }
2001                         if (r.rule_flag & PFRULE_SRCTRACK) {
2002                                 if (srctrack == PF_SRCTRACK_GLOBAL &&
2003                                     r.max_src_nodes) {
2004                                         yyerror("'max-src-nodes' is "
2005                                             "incompatible with "
2006                                             "'source-track global'");
2007                                         YYERROR;
2008                                 }
2009                                 if (srctrack == PF_SRCTRACK_GLOBAL &&
2010                                     r.max_src_conn) {
2011                                         yyerror("'max-src-conn' is "
2012                                             "incompatible with "
2013                                             "'source-track global'");
2014                                         YYERROR;
2015                                 }
2016                                 if (srctrack == PF_SRCTRACK_GLOBAL &&
2017                                     r.max_src_conn_rate.seconds) {
2018                                         yyerror("'max-src-conn-rate' is "
2019                                             "incompatible with "
2020                                             "'source-track global'");
2021                                         YYERROR;
2022                                 }
2023                                 if (r.timeout[PFTM_SRC_NODE] <
2024                                     r.max_src_conn_rate.seconds)
2025                                         r.timeout[PFTM_SRC_NODE] =
2026                                             r.max_src_conn_rate.seconds;
2027                                 r.rule_flag |= PFRULE_SRCTRACK;
2028                                 if (srctrack == PF_SRCTRACK_RULE)
2029                                         r.rule_flag |= PFRULE_RULESRCTRACK;
2030                         }
2031                         if (r.keep_state && !statelock)
2032                                 r.rule_flag |= default_statelock;
2033
2034                         if ($9.fragment)
2035                                 r.rule_flag |= PFRULE_FRAGMENT;
2036                         r.allow_opts = $9.allowopts;
2037
2038                         decide_address_family($8.src.host, &r.af);
2039                         decide_address_family($8.dst.host, &r.af);
2040
2041                         if ($5.rt) {
2042                                 if (!r.direction) {
2043                                         yyerror("direction must be explicit "
2044                                             "with rules that specify routing");
2045                                         YYERROR;
2046                                 }
2047                                 r.rt = $5.rt;
2048                                 r.rpool.opts = $5.pool_opts;
2049                                 if ($5.key != NULL)
2050                                         memcpy(&r.rpool.key, $5.key,
2051                                             sizeof(struct pf_poolhashkey));
2052                         }
2053                         if (r.rt && r.rt != PF_FASTROUTE) {
2054                                 decide_address_family($5.host, &r.af);
2055                                 remove_invalid_hosts(&$5.host, &r.af);
2056                                 if ($5.host == NULL) {
2057                                         yyerror("no routing address with "
2058                                             "matching address family found.");
2059                                         YYERROR;
2060                                 }
2061                                 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
2062                                     PF_POOL_NONE && ($5.host->next != NULL ||
2063                                     $5.host->addr.type == PF_ADDR_TABLE ||
2064                                     DYNIF_MULTIADDR($5.host->addr)))
2065                                         r.rpool.opts |= PF_POOL_ROUNDROBIN;
2066                                 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2067                                     PF_POOL_ROUNDROBIN &&
2068                                     disallow_table($5.host, "tables are only "
2069                                     "supported in round-robin routing pools"))
2070                                         YYERROR;
2071                                 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2072                                     PF_POOL_ROUNDROBIN &&
2073                                     disallow_alias($5.host, "interface (%s) "
2074                                     "is only supported in round-robin "
2075                                     "routing pools"))
2076                                         YYERROR;
2077                                 if ($5.host->next != NULL) {
2078                                         if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2079                                             PF_POOL_ROUNDROBIN) {
2080                                                 yyerror("r.rpool.opts must "
2081                                                     "be PF_POOL_ROUNDROBIN");
2082                                                 YYERROR;
2083                                         }
2084                                 }
2085                         }
2086                         if ($9.queues.qname != NULL) {
2087                                 if (strlcpy(r.qname, $9.queues.qname,
2088                                     sizeof(r.qname)) >= sizeof(r.qname)) {
2089                                         yyerror("rule qname too long (max "
2090                                             "%d chars)", sizeof(r.qname)-1);
2091                                         YYERROR;
2092                                 }
2093                                 free($9.queues.qname);
2094                         }
2095                         if ($9.queues.pqname != NULL) {
2096                                 if (strlcpy(r.pqname, $9.queues.pqname,
2097                                     sizeof(r.pqname)) >= sizeof(r.pqname)) {
2098                                         yyerror("rule pqname too long (max "
2099                                             "%d chars)", sizeof(r.pqname)-1);
2100                                         YYERROR;
2101                                 }
2102                                 free($9.queues.pqname);
2103                         }
2104
2105                         expand_rule(&r, $4, $5.host, $7, $8.src_os,
2106                             $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
2107                             $9.uid, $9.gid, $9.icmpspec, "");
2108                 }
2109                 ;
2110
2111 filter_opts     :       {
2112                                 bzero(&filter_opts, sizeof filter_opts);
2113                                 filter_opts.rtableid = -1;
2114                         }
2115                     filter_opts_l
2116                         { $$ = filter_opts; }
2117                 | /* empty */   {
2118                         bzero(&filter_opts, sizeof filter_opts);
2119                         filter_opts.rtableid = -1;
2120                         $$ = filter_opts;
2121                 }
2122                 ;
2123
2124 filter_opts_l   : filter_opts_l filter_opt
2125                 | filter_opt
2126                 ;
2127
2128 filter_opt      : USER uids {
2129                         if (filter_opts.uid)
2130                                 $2->tail->next = filter_opts.uid;
2131                         filter_opts.uid = $2;
2132                 }
2133                 | GROUP gids {
2134                         if (filter_opts.gid)
2135                                 $2->tail->next = filter_opts.gid;
2136                         filter_opts.gid = $2;
2137                 }
2138                 | flags {
2139                         if (filter_opts.marker & FOM_FLAGS) {
2140                                 yyerror("flags cannot be redefined");
2141                                 YYERROR;
2142                         }
2143                         filter_opts.marker |= FOM_FLAGS;
2144                         filter_opts.flags.b1 |= $1.b1;
2145                         filter_opts.flags.b2 |= $1.b2;
2146                         filter_opts.flags.w |= $1.w;
2147                         filter_opts.flags.w2 |= $1.w2;
2148                 }
2149                 | icmpspec {
2150                         if (filter_opts.marker & FOM_ICMP) {
2151                                 yyerror("icmp-type cannot be redefined");
2152                                 YYERROR;
2153                         }
2154                         filter_opts.marker |= FOM_ICMP;
2155                         filter_opts.icmpspec = $1;
2156                 }
2157                 | tos {
2158                         if (filter_opts.marker & FOM_TOS) {
2159                                 yyerror("tos cannot be redefined");
2160                                 YYERROR;
2161                         }
2162                         filter_opts.marker |= FOM_TOS;
2163                         filter_opts.tos = $1;
2164                 }
2165                 | keep {
2166                         if (filter_opts.marker & FOM_KEEP) {
2167                                 yyerror("modulate or keep cannot be redefined");
2168                                 YYERROR;
2169                         }
2170                         filter_opts.marker |= FOM_KEEP;
2171                         filter_opts.keep.action = $1.action;
2172                         filter_opts.keep.options = $1.options;
2173                 }
2174                 | FRAGMENT {
2175                         filter_opts.fragment = 1;
2176                 }
2177                 | ALLOWOPTS {
2178                         filter_opts.allowopts = 1;
2179                 }
2180                 | label {
2181                         if (filter_opts.label) {
2182                                 yyerror("label cannot be redefined");
2183                                 YYERROR;
2184                         }
2185                         filter_opts.label = $1;
2186                 }
2187                 | qname {
2188                         if (filter_opts.queues.qname) {
2189                                 yyerror("queue cannot be redefined");
2190                                 YYERROR;
2191                         }
2192                         filter_opts.queues = $1;
2193                 }
2194                 | TAG string                            {
2195                         filter_opts.tag = $2;
2196                 }
2197                 | not TAGGED string                     {
2198                         filter_opts.match_tag = $3;
2199                         filter_opts.match_tag_not = $1;
2200                 }
2201                 | PROBABILITY STRING                    {
2202                         char    *e;
2203                         double   p = strtod($2, &e);
2204
2205                         if (*e == '%') {
2206                                 p *= 0.01;
2207                                 e++;
2208                         }
2209                         if (*e) {
2210                                 yyerror("invalid probability: %s", $2);
2211                                 free($2);
2212                                 YYERROR;
2213                         }
2214                         p = floor(p * (UINT_MAX+1.0) + 0.5);
2215                         if (p < 1.0 || p >= (UINT_MAX+1.0)) {
2216                                 yyerror("invalid probability: %s", $2);
2217                                 free($2);
2218                                 YYERROR;
2219                         }
2220                         filter_opts.prob = (u_int32_t)p;
2221                         free($2);
2222                 }
2223                 | RTABLE number                         {
2224                         filter_opts.rtableid = $2;
2225                 }
2226                 ;
2227
2228 action          : PASS                  { $$.b1 = PF_PASS; $$.b2 = $$.w = 0; }
2229                 | BLOCK blockspec       { $$ = $2; $$.b1 = PF_DROP; }
2230                 ;
2231
2232 blockspec       : /* empty */           {
2233                         $$.b2 = blockpolicy;
2234                         $$.w = returnicmpdefault;
2235                         $$.w2 = returnicmp6default;
2236                 }
2237                 | DROP                  {
2238                         $$.b2 = PFRULE_DROP;
2239                         $$.w = 0;
2240                         $$.w2 = 0;
2241                 }
2242                 | RETURNRST             {
2243                         $$.b2 = PFRULE_RETURNRST;
2244                         $$.w = 0;
2245                         $$.w2 = 0;
2246                 }
2247                 | RETURNRST '(' TTL number ')'  {
2248                         if ($4 > 255) {
2249                                 yyerror("illegal ttl value %d", $4);
2250                                 YYERROR;
2251                         }
2252                         $$.b2 = PFRULE_RETURNRST;
2253                         $$.w = $4;
2254                         $$.w2 = 0;
2255                 }
2256                 | RETURNICMP            {
2257                         $$.b2 = PFRULE_RETURNICMP;
2258                         $$.w = returnicmpdefault;
2259                         $$.w2 = returnicmp6default;
2260                 }
2261                 | RETURNICMP6           {
2262                         $$.b2 = PFRULE_RETURNICMP;
2263                         $$.w = returnicmpdefault;
2264                         $$.w2 = returnicmp6default;
2265                 }
2266                 | RETURNICMP '(' STRING ')'     {
2267                         $$.b2 = PFRULE_RETURNICMP;
2268                         if (!($$.w = parseicmpspec($3, AF_INET))) {
2269                                 free($3);
2270                                 YYERROR;
2271                         }
2272                         free($3);
2273                         $$.w2 = returnicmp6default;
2274                 }
2275                 | RETURNICMP6 '(' STRING ')'    {
2276                         $$.b2 = PFRULE_RETURNICMP;
2277                         $$.w = returnicmpdefault;
2278                         if (!($$.w2 = parseicmpspec($3, AF_INET6))) {
2279                                 free($3);
2280                                 YYERROR;
2281                         }
2282                         free($3);
2283                 }
2284                 | RETURNICMP '(' STRING comma STRING ')' {
2285                         $$.b2 = PFRULE_RETURNICMP;
2286                         if (!($$.w = parseicmpspec($3, AF_INET)) ||
2287                             !($$.w2 = parseicmpspec($5, AF_INET6))) {
2288                                 free($3);
2289                                 free($5);
2290                                 YYERROR;
2291                         }
2292                         free($3);
2293                         free($5);
2294                 }
2295                 | RETURN {
2296                         $$.b2 = PFRULE_RETURN;
2297                         $$.w = returnicmpdefault;
2298                         $$.w2 = returnicmp6default;
2299                 }
2300                 ;
2301
2302 dir             : /* empty */                   { $$ = 0; }
2303                 | IN                            { $$ = PF_IN; }
2304                 | OUT                           { $$ = PF_OUT; }
2305                 ;
2306
2307 quick           : /* empty */                   { $$.quick = 0; }
2308                 | QUICK                         { $$.quick = 1; }
2309                 ;
2310
2311 logquick        : /* empty */   { $$.log = 0; $$.quick = 0; $$.logif = 0; }
2312                 | log           { $$ = $1; $$.quick = 0; }
2313                 | QUICK         { $$.quick = 1; $$.log = 0; $$.logif = 0; }
2314                 | log QUICK     { $$ = $1; $$.quick = 1; }
2315                 | QUICK log     { $$ = $2; $$.quick = 1; }
2316                 ;
2317
2318 log             : LOG                   { $$.log = PF_LOG; $$.logif = 0; }
2319                 | LOG '(' logopts ')'   {
2320                         $$.log = PF_LOG | $3.log;
2321                         $$.logif = $3.logif;
2322                 }
2323                 ;
2324
2325 logopts         : logopt                        { $$ = $1; }
2326                 | logopts comma logopt          {
2327                         $$.log = $1.log | $3.log;
2328                         $$.logif = $3.logif;
2329                         if ($$.logif == 0)
2330                                 $$.logif = $1.logif;
2331                 }
2332                 ;
2333
2334 logopt          : ALL           { $$.log = PF_LOG_ALL; $$.logif = 0; }
2335                 | USER          { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
2336                 | GROUP         { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
2337                 | TO string     {
2338                         const char      *errstr;
2339                         u_int            i;
2340
2341                         $$.log = 0;
2342                         if (strncmp($2, "pflog", 5)) {
2343                                 yyerror("%s: should be a pflog interface", $2);
2344                                 free($2);
2345                                 YYERROR;
2346                         }
2347                         i = strtonum($2 + 5, 0, 255, &errstr);
2348                         if (errstr) {
2349                                 yyerror("%s: %s", $2, errstr);
2350                                 free($2);
2351                                 YYERROR;
2352                         }
2353                         free($2);
2354                         $$.logif = i;
2355                 }
2356                 ;
2357
2358 interface       : /* empty */                   { $$ = NULL; }
2359                 | ON if_item_not                { $$ = $2; }
2360                 | ON '{' if_list '}'            { $$ = $3; }
2361                 ;
2362
2363 if_list         : if_item_not                   { $$ = $1; }
2364                 | if_list comma if_item_not     {
2365                         $1->tail->next = $3;
2366                         $1->tail = $3;
2367                         $$ = $1;
2368                 }
2369                 ;
2370
2371 if_item_not     : not if_item                   { $$ = $2; $$->not = $1; }
2372                 ;
2373
2374 if_item         : STRING                        {
2375                         struct node_host        *n;
2376
2377                         $$ = calloc(1, sizeof(struct node_if));
2378                         if ($$ == NULL)
2379                                 err(1, "if_item: calloc");
2380                         if (strlcpy($$->ifname, $1, sizeof($$->ifname)) >=
2381                             sizeof($$->ifname)) {
2382                                 free($1);
2383                                 free($$);
2384                                 yyerror("interface name too long");
2385                                 YYERROR;
2386                         }
2387
2388                         if ((n = ifa_exists($1)) != NULL)
2389                                 $$->ifa_flags = n->ifa_flags;
2390
2391                         free($1);
2392                         $$->not = 0;
2393                         $$->next = NULL;
2394                         $$->tail = $$;
2395                 }
2396                 ;
2397
2398 af              : /* empty */                   { $$ = 0; }
2399                 | INET                          { $$ = AF_INET; }
2400                 | INET6                         { $$ = AF_INET6; }
2401                 ;
2402
2403 proto           : /* empty */                   { $$ = NULL; }
2404                 | PROTO proto_item              { $$ = $2; }
2405                 | PROTO '{' proto_list '}'      { $$ = $3; }
2406                 ;
2407
2408 proto_list      : proto_item                    { $$ = $1; }
2409                 | proto_list comma proto_item   {
2410                         $1->tail->next = $3;
2411                         $1->tail = $3;
2412                         $$ = $1;
2413                 }
2414                 ;
2415
2416 proto_item      : STRING                        {
2417                         u_int8_t        pr;
2418                         u_long          ulval;
2419
2420                         if (atoul($1, &ulval) == 0) {
2421                                 if (ulval > 255) {
2422                                         yyerror("protocol outside range");
2423                                         free($1);
2424                                         YYERROR;
2425                                 }
2426                                 pr = (u_int8_t)ulval;
2427                         } else {
2428                                 struct protoent *p;
2429
2430                                 p = getprotobyname($1);
2431                                 if (p == NULL) {
2432                                         yyerror("unknown protocol %s", $1);
2433                                         free($1);
2434                                         YYERROR;
2435                                 }
2436                                 pr = p->p_proto;
2437                         }
2438                         free($1);
2439                         if (pr == 0) {
2440                                 yyerror("proto 0 cannot be used");
2441                                 YYERROR;
2442                         }
2443                         $$ = calloc(1, sizeof(struct node_proto));
2444                         if ($$ == NULL)
2445                                 err(1, "proto_item: calloc");
2446                         $$->proto = pr;
2447                         $$->next = NULL;
2448                         $$->tail = $$;
2449                 }
2450                 ;
2451
2452 fromto          : ALL                           {
2453                         $$.src.host = NULL;
2454                         $$.src.port = NULL;
2455                         $$.dst.host = NULL;
2456                         $$.dst.port = NULL;
2457                         $$.src_os = NULL;
2458                 }
2459                 | from os to                    {
2460                         $$.src = $1;
2461                         $$.src_os = $2;
2462                         $$.dst = $3;
2463                 }
2464                 ;
2465
2466 os              : /* empty */                   { $$ = NULL; }
2467                 | OS xos                        { $$ = $2; }
2468                 | OS '{' os_list '}'            { $$ = $3; }
2469                 ;
2470
2471 xos             : STRING {
2472                         $$ = calloc(1, sizeof(struct node_os));
2473                         if ($$ == NULL)
2474                                 err(1, "os: calloc");
2475                         $$->os = $1;
2476                         $$->tail = $$;
2477                 }
2478                 ;
2479
2480 os_list         : xos                           { $$ = $1; }
2481                 | os_list comma xos             {
2482                         $1->tail->next = $3;
2483                         $1->tail = $3;
2484                         $$ = $1;
2485                 }
2486                 ;
2487
2488 from            : /* empty */                   {
2489                         $$.host = NULL;
2490                         $$.port = NULL;
2491                 }
2492                 | FROM ipportspec               {
2493                         $$ = $2;
2494                 }
2495                 ;
2496
2497 to              : /* empty */                   {
2498                         $$.host = NULL;
2499                         $$.port = NULL;
2500                 }
2501                 | TO ipportspec         {
2502                         if (disallow_urpf_failed($2.host, "\"urpf-failed\" is "
2503                             "not permitted in a destination address"))
2504                                 YYERROR;
2505                         $$ = $2;
2506                 }
2507                 ;
2508
2509 ipportspec      : ipspec                        {
2510                         $$.host = $1;
2511                         $$.port = NULL;
2512                 }
2513                 | ipspec PORT portspec          {
2514                         $$.host = $1;
2515                         $$.port = $3;
2516                 }
2517                 | PORT portspec                 {
2518                         $$.host = NULL;
2519                         $$.port = $2;
2520                 }
2521                 ;
2522
2523 ipspec          : ANY                           { $$ = NULL; }
2524                 | xhost                         { $$ = $1; }
2525                 | '{' host_list '}'             { $$ = $2; }
2526                 ;
2527
2528 host_list       : ipspec                        { $$ = $1; }
2529                 | host_list comma ipspec        {
2530                         if ($3 == NULL)
2531                                 $$ = $1;
2532                         else if ($1 == NULL)
2533                                 $$ = $3;
2534                         else {
2535                                 $1->tail->next = $3;
2536                                 $1->tail = $3->tail;
2537                                 $$ = $1;
2538                         }
2539                 }
2540                 ;
2541
2542 xhost           : not host                      {
2543                         struct node_host        *n;
2544
2545                         for (n = $2; n != NULL; n = n->next)
2546                                 n->not = $1;
2547                         $$ = $2;
2548                 }
2549                 | not NOROUTE                   {
2550                         $$ = calloc(1, sizeof(struct node_host));
2551                         if ($$ == NULL)
2552                                 err(1, "xhost: calloc");
2553                         $$->addr.type = PF_ADDR_NOROUTE;
2554                         $$->next = NULL;
2555                         $$->not = $1;
2556                         $$->tail = $$;
2557                 }
2558                 | not URPFFAILED                {
2559                         $$ = calloc(1, sizeof(struct node_host));
2560                         if ($$ == NULL)
2561                                 err(1, "xhost: calloc");
2562                         $$->addr.type = PF_ADDR_URPFFAILED;
2563                         $$->next = NULL;
2564                         $$->not = $1;
2565                         $$->tail = $$;
2566                 }
2567                 ;
2568
2569 host            : STRING                        {
2570                         if (($$ = host($1)) == NULL)    {
2571                                 /* error. "any" is handled elsewhere */
2572                                 free($1);
2573                                 yyerror("could not parse host specification");
2574                                 YYERROR;
2575                         }
2576                         free($1);
2577
2578                 }
2579                 | STRING '/' number             {
2580                         char    *buf;
2581
2582                         if (asprintf(&buf, "%s/%u", $1, $3) == -1)
2583                                 err(1, "host: asprintf");
2584                         free($1);
2585                         if (($$ = host(buf)) == NULL)   {
2586                                 /* error. "any" is handled elsewhere */
2587                                 free(buf);
2588                                 yyerror("could not parse host specification");
2589                                 YYERROR;
2590                         }
2591                         free(buf);
2592                 }
2593                 | dynaddr
2594                 | dynaddr '/' number            {
2595                         struct node_host        *n;
2596
2597                         $$ = $1;
2598                         for (n = $1; n != NULL; n = n->next)
2599                                 set_ipmask(n, $3);
2600                 }
2601                 | '<' STRING '>'        {
2602                         if (strlen($2) >= PF_TABLE_NAME_SIZE) {
2603                                 yyerror("table name '%s' too long", $2);
2604                                 free($2);
2605                                 YYERROR;
2606                         }
2607                         $$ = calloc(1, sizeof(struct node_host));
2608                         if ($$ == NULL)
2609                                 err(1, "host: calloc");
2610                         $$->addr.type = PF_ADDR_TABLE;
2611                         if (strlcpy($$->addr.v.tblname, $2,
2612                             sizeof($$->addr.v.tblname)) >=
2613                             sizeof($$->addr.v.tblname))
2614                                 errx(1, "host: strlcpy");
2615                         free($2);
2616                         $$->next = NULL;
2617                         $$->tail = $$;
2618                 }
2619                 | ROUTE STRING          {
2620                         $$ = calloc(1, sizeof(struct node_host));
2621                         if ($$ == NULL) {
2622                                 free($2);
2623                                 err(1, "host: calloc");
2624                         }
2625                         $$->addr.type = PF_ADDR_RTLABEL;
2626                         if (strlcpy($$->addr.v.rtlabelname, $2,
2627                             sizeof($$->addr.v.rtlabelname)) >=
2628                             sizeof($$->addr.v.rtlabelname)) {
2629                                 yyerror("route label too long, max %u chars",
2630                                     sizeof($$->addr.v.rtlabelname) - 1);
2631                                 free($2);
2632                                 free($$);
2633                                 YYERROR;
2634                         }
2635                         $$->next = NULL;
2636                         $$->tail = $$;
2637                         free($2);
2638                 }
2639                 ;
2640
2641 number          : STRING                        {
2642                         u_long  ulval;
2643
2644                         if (atoul($1, &ulval) == -1) {
2645                                 yyerror("%s is not a number", $1);
2646                                 free($1);
2647                                 YYERROR;
2648                         } else
2649                                 $$ = ulval;
2650                         free($1);
2651                 }
2652                 ;
2653
2654 dynaddr         : '(' STRING ')'                {
2655                         int      flags = 0;
2656                         char    *p, *op;
2657
2658                         op = $2;
2659                         if (!isalpha(op[0])) {
2660                                 yyerror("invalid interface name '%s'", op);
2661                                 free(op);
2662                                 YYERROR;
2663                         }
2664                         while ((p = strrchr($2, ':')) != NULL) {
2665                                 if (!strcmp(p+1, "network"))
2666                                         flags |= PFI_AFLAG_NETWORK;
2667                                 else if (!strcmp(p+1, "broadcast"))
2668                                         flags |= PFI_AFLAG_BROADCAST;
2669                                 else if (!strcmp(p+1, "peer"))
2670                                         flags |= PFI_AFLAG_PEER;
2671                                 else if (!strcmp(p+1, "0"))
2672                                         flags |= PFI_AFLAG_NOALIAS;
2673                                 else {
2674                                         yyerror("interface %s has bad modifier",
2675                                             $2);
2676                                         free(op);
2677                                         YYERROR;
2678                                 }
2679                                 *p = '\0';
2680                         }
2681                         if (flags & (flags - 1) & PFI_AFLAG_MODEMASK) {
2682                                 free(op);
2683                                 yyerror("illegal combination of "
2684                                     "interface modifiers");
2685                                 YYERROR;
2686                         }
2687                         $$ = calloc(1, sizeof(struct node_host));
2688                         if ($$ == NULL)
2689                                 err(1, "address: calloc");
2690                         $$->af = 0;
2691                         set_ipmask($$, 128);
2692                         $$->addr.type = PF_ADDR_DYNIFTL;
2693                         $$->addr.iflags = flags;
2694                         if (strlcpy($$->addr.v.ifname, $2,
2695                             sizeof($$->addr.v.ifname)) >=
2696                             sizeof($$->addr.v.ifname)) {
2697                                 free(op);
2698                                 free($$);
2699                                 yyerror("interface name too long");
2700                                 YYERROR;
2701                         }
2702                         free(op);
2703                         $$->next = NULL;
2704                         $$->tail = $$;
2705                 }
2706                 ;
2707
2708 portspec        : port_item                     { $$ = $1; }
2709                 | '{' port_list '}'             { $$ = $2; }
2710                 ;
2711
2712 port_list       : port_item                     { $$ = $1; }
2713                 | port_list comma port_item     {
2714                         $1->tail->next = $3;
2715                         $1->tail = $3;
2716                         $$ = $1;
2717                 }
2718                 ;
2719
2720 port_item       : port                          {
2721                         $$ = calloc(1, sizeof(struct node_port));
2722                         if ($$ == NULL)
2723                                 err(1, "port_item: calloc");
2724                         $$->port[0] = $1.a;
2725                         $$->port[1] = $1.b;
2726                         if ($1.t)
2727                                 $$->op = PF_OP_RRG;
2728                         else
2729                                 $$->op = PF_OP_EQ;
2730                         $$->next = NULL;
2731                         $$->tail = $$;
2732                 }
2733                 | unaryop port          {
2734                         if ($2.t) {
2735                                 yyerror("':' cannot be used with an other "
2736                                     "port operator");
2737                                 YYERROR;
2738                         }
2739                         $$ = calloc(1, sizeof(struct node_port));
2740                         if ($$ == NULL)
2741                                 err(1, "port_item: calloc");
2742                         $$->port[0] = $2.a;
2743                         $$->port[1] = $2.b;
2744                         $$->op = $1;
2745                         $$->next = NULL;
2746                         $$->tail = $$;
2747                 }
2748                 | port PORTBINARY port          {
2749                         if ($1.t || $3.t) {
2750                                 yyerror("':' cannot be used with an other "
2751                                     "port operator");
2752                                 YYERROR;
2753                         }
2754                         $$ = calloc(1, sizeof(struct node_port));
2755                         if ($$ == NULL)
2756                                 err(1, "port_item: calloc");
2757                         $$->port[0] = $1.a;
2758                         $$->port[1] = $3.a;
2759                         $$->op = $2;
2760                         $$->next = NULL;
2761                         $$->tail = $$;
2762                 }
2763                 ;
2764
2765 port            : STRING                        {
2766                         char    *p = strchr($1, ':');
2767
2768                         if (p == NULL) {
2769                                 if (($$.a = getservice($1)) == -1) {
2770                                         free($1);
2771                                         YYERROR;
2772                                 }
2773                                 $$.b = $$.t = 0;
2774                         } else {
2775                                 int port[2];
2776
2777                                 *p++ = 0;
2778                                 if ((port[0] = getservice($1)) == -1 ||
2779                                     (port[1] = getservice(p)) == -1) {
2780                                         free($1);
2781                                         YYERROR;
2782                                 }
2783                                 $$.a = port[0];
2784                                 $$.b = port[1];
2785                                 $$.t = PF_OP_RRG;
2786                         }
2787                         free($1);
2788                 }
2789                 ;
2790
2791 uids            : uid_item                      { $$ = $1; }
2792                 | '{' uid_list '}'              { $$ = $2; }
2793                 ;
2794
2795 uid_list        : uid_item                      { $$ = $1; }
2796                 | uid_list comma uid_item       {
2797                         $1->tail->next = $3;
2798                         $1->tail = $3;
2799                         $$ = $1;
2800                 }
2801                 ;
2802
2803 uid_item        : uid                           {
2804                         $$ = calloc(1, sizeof(struct node_uid));
2805                         if ($$ == NULL)
2806                                 err(1, "uid_item: calloc");
2807                         $$->uid[0] = $1;
2808                         $$->uid[1] = $1;
2809                         $$->op = PF_OP_EQ;
2810                         $$->next = NULL;
2811                         $$->tail = $$;
2812                 }
2813                 | unaryop uid                   {
2814                         if ($2 == UID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
2815                                 yyerror("user unknown requires operator = or "
2816                                     "!=");
2817                                 YYERROR;
2818                         }
2819                         $$ = calloc(1, sizeof(struct node_uid));
2820                         if ($$ == NULL)
2821                                 err(1, "uid_item: calloc");
2822                         $$->uid[0] = $2;
2823                         $$->uid[1] = $2;
2824                         $$->op = $1;
2825                         $$->next = NULL;
2826                         $$->tail = $$;
2827                 }
2828                 | uid PORTBINARY uid            {
2829                         if ($1 == UID_MAX || $3 == UID_MAX) {
2830                                 yyerror("user unknown requires operator = or "
2831                                     "!=");
2832                                 YYERROR;
2833                         }
2834                         $$ = calloc(1, sizeof(struct node_uid));
2835                         if ($$ == NULL)
2836                                 err(1, "uid_item: calloc");
2837                         $$->uid[0] = $1;
2838                         $$->uid[1] = $3;
2839                         $$->op = $2;
2840                         $$->next = NULL;
2841                         $$->tail = $$;
2842                 }
2843                 ;
2844
2845 uid             : STRING                        {
2846                         u_long  ulval;
2847
2848                         if (atoul($1, &ulval) == -1) {
2849                                 if (!strcmp($1, "unknown"))
2850                                         $$ = UID_MAX;
2851                                 else {
2852                                         struct passwd   *pw;
2853
2854                                         if ((pw = getpwnam($1)) == NULL) {
2855                                                 yyerror("unknown user %s", $1);
2856                                                 free($1);
2857                                                 YYERROR;
2858                                         }
2859                                         $$ = pw->pw_uid;
2860                                 }
2861                         } else {
2862                                 if (ulval >= UID_MAX) {
2863                                         free($1);
2864                                         yyerror("illegal uid value %lu", ulval);
2865                                         YYERROR;
2866                                 }
2867                                 $$ = ulval;
2868                         }
2869                         free($1);
2870                 }
2871                 ;
2872
2873 gids            : gid_item                      { $$ = $1; }
2874                 | '{' gid_list '}'              { $$ = $2; }
2875                 ;
2876
2877 gid_list        : gid_item                      { $$ = $1; }
2878                 | gid_list comma gid_item       {
2879                         $1->tail->next = $3;
2880                         $1->tail = $3;
2881                         $$ = $1;
2882                 }
2883                 ;
2884
2885 gid_item        : gid                           {
2886                         $$ = calloc(1, sizeof(struct node_gid));
2887                         if ($$ == NULL)
2888                                 err(1, "gid_item: calloc");
2889                         $$->gid[0] = $1;
2890                         $$->gid[1] = $1;
2891                         $$->op = PF_OP_EQ;
2892                         $$->next = NULL;
2893                         $$->tail = $$;
2894                 }
2895                 | unaryop gid                   {
2896                         if ($2 == GID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
2897                                 yyerror("group unknown requires operator = or "
2898                                     "!=");
2899                                 YYERROR;
2900                         }
2901                         $$ = calloc(1, sizeof(struct node_gid));
2902                         if ($$ == NULL)
2903                                 err(1, "gid_item: calloc");
2904                         $$->gid[0] = $2;
2905                         $$->gid[1] = $2;
2906                         $$->op = $1;
2907                         $$->next = NULL;
2908                         $$->tail = $$;
2909                 }
2910                 | gid PORTBINARY gid            {
2911                         if ($1 == GID_MAX || $3 == GID_MAX) {
2912                                 yyerror("group unknown requires operator = or "
2913                                     "!=");
2914                                 YYERROR;
2915                         }
2916                         $$ = calloc(1, sizeof(struct node_gid));
2917                         if ($$ == NULL)
2918                                 err(1, "gid_item: calloc");
2919                         $$->gid[0] = $1;
2920                         $$->gid[1] = $3;
2921                         $$->op = $2;
2922                         $$->next = NULL;
2923                         $$->tail = $$;
2924                 }
2925                 ;
2926
2927 gid             : STRING                        {
2928                         u_long  ulval;
2929
2930                         if (atoul($1, &ulval) == -1) {
2931                                 if (!strcmp($1, "unknown"))
2932                                         $$ = GID_MAX;
2933                                 else {
2934                                         struct group    *grp;
2935
2936                                         if ((grp = getgrnam($1)) == NULL) {
2937                                                 yyerror("unknown group %s", $1);
2938                                                 free($1);
2939                                                 YYERROR;
2940                                         }
2941                                         $$ = grp->gr_gid;
2942                                 }
2943                         } else {
2944                                 if (ulval >= GID_MAX) {
2945                                         yyerror("illegal gid value %lu", ulval);
2946                                         free($1);
2947                                         YYERROR;
2948                                 }
2949                                 $$ = ulval;
2950                         }
2951                         free($1);
2952                 }
2953                 ;
2954
2955 flag            : STRING                        {
2956                         int     f;
2957
2958                         if ((f = parse_flags($1)) < 0) {
2959                                 yyerror("bad flags %s", $1);
2960                                 free($1);
2961                                 YYERROR;
2962                         }
2963                         free($1);
2964                         $$.b1 = f;
2965                 }
2966                 ;
2967
2968 flags           : FLAGS flag '/' flag   { $$.b1 = $2.b1; $$.b2 = $4.b1; }
2969                 | FLAGS '/' flag        { $$.b1 = 0; $$.b2 = $3.b1; }
2970                 | FLAGS ANY             { $$.b1 = 0; $$.b2 = 0; }
2971                 ;
2972
2973 icmpspec        : ICMPTYPE icmp_item            { $$ = $2; }
2974                 | ICMPTYPE '{' icmp_list '}'    { $$ = $3; }
2975                 | ICMP6TYPE icmp6_item          { $$ = $2; }
2976                 | ICMP6TYPE '{' icmp6_list '}'  { $$ = $3; }
2977                 ;
2978
2979 icmp_list       : icmp_item                     { $$ = $1; }
2980                 | icmp_list comma icmp_item     {
2981                         $1->tail->next = $3;
2982                         $1->tail = $3;
2983                         $$ = $1;
2984                 }
2985                 ;
2986
2987 icmp6_list      : icmp6_item                    { $$ = $1; }
2988                 | icmp6_list comma icmp6_item   {
2989                         $1->tail->next = $3;
2990                         $1->tail = $3;
2991                         $$ = $1;
2992                 }
2993                 ;
2994
2995 icmp_item       : icmptype              {
2996                         $$ = calloc(1, sizeof(struct node_icmp));
2997                         if ($$ == NULL)
2998                                 err(1, "icmp_item: calloc");
2999                         $$->type = $1;
3000                         $$->code = 0;
3001                         $$->proto = IPPROTO_ICMP;
3002                         $$->next = NULL;
3003                         $$->tail = $$;
3004                 }
3005                 | icmptype CODE STRING  {
3006                         const struct icmpcodeent        *p;
3007                         u_long                           ulval;
3008
3009                         if (atoul($3, &ulval) == 0) {
3010                                 if (ulval > 255) {
3011                                         free($3);
3012                                         yyerror("illegal icmp-code %lu", ulval);
3013                                         YYERROR;
3014                                 }
3015                         } else {
3016                                 if ((p = geticmpcodebyname($1-1, $3,
3017                                     AF_INET)) == NULL) {
3018                                         yyerror("unknown icmp-code %s", $3);
3019                                         free($3);
3020                                         YYERROR;
3021                                 }
3022                                 ulval = p->code;
3023                         }
3024                         free($3);
3025                         $$ = calloc(1, sizeof(struct node_icmp));
3026                         if ($$ == NULL)
3027                                 err(1, "icmp_item: calloc");
3028                         $$->type = $1;
3029                         $$->code = ulval + 1;
3030                         $$->proto = IPPROTO_ICMP;
3031                         $$->next = NULL;
3032                         $$->tail = $$;
3033                 }
3034                 ;
3035
3036 icmp6_item      : icmp6type             {
3037                         $$ = calloc(1, sizeof(struct node_icmp));
3038                         if ($$ == NULL)
3039                                 err(1, "icmp_item: calloc");
3040                         $$->type = $1;
3041                         $$->code = 0;
3042                         $$->proto = IPPROTO_ICMPV6;
3043                         $$->next = NULL;
3044                         $$->tail = $$;
3045                 }
3046                 | icmp6type CODE STRING {
3047                         const struct icmpcodeent        *p;
3048                         u_long                           ulval;
3049
3050                         if (atoul($3, &ulval) == 0) {
3051                                 if (ulval > 255) {
3052                                         yyerror("illegal icmp6-code %lu",
3053                                             ulval);
3054                                         free($3);
3055                                         YYERROR;
3056                                 }
3057                         } else {
3058                                 if ((p = geticmpcodebyname($1-1, $3,
3059                                     AF_INET6)) == NULL) {
3060                                         yyerror("unknown icmp6-code %s", $3);
3061                                         free($3);
3062                                         YYERROR;
3063                                 }
3064                                 ulval = p->code;
3065                         }
3066                         free($3);
3067                         $$ = calloc(1, sizeof(struct node_icmp));
3068                         if ($$ == NULL)
3069                                 err(1, "icmp_item: calloc");
3070                         $$->type = $1;
3071                         $$->code = ulval + 1;
3072                         $$->proto = IPPROTO_ICMPV6;
3073                         $$->next = NULL;
3074                         $$->tail = $$;
3075                 }
3076                 ;
3077
3078 icmptype        : STRING                        {
3079                         const struct icmptypeent        *p;
3080                         u_long                           ulval;
3081
3082                         if (atoul($1, &ulval) == 0) {
3083                                 if (ulval > 255) {
3084                                         yyerror("illegal icmp-type %lu", ulval);
3085                                         free($1);
3086                                         YYERROR;
3087                                 }
3088                                 $$ = ulval + 1;
3089                         } else {
3090                                 if ((p = geticmptypebyname($1, AF_INET)) ==
3091                                     NULL) {
3092                                         yyerror("unknown icmp-type %s", $1);
3093                                         free($1);
3094                                         YYERROR;
3095                                 }
3096                                 $$ = p->type + 1;
3097                         }
3098                         free($1);
3099                 }
3100                 ;
3101
3102 icmp6type       : STRING                        {
3103                         const struct icmptypeent        *p;
3104                         u_long                           ulval;
3105
3106                         if (atoul($1, &ulval) == 0) {
3107                                 if (ulval > 255) {
3108                                         yyerror("illegal icmp6-type %lu",
3109                                             ulval);
3110                                         free($1);
3111                                         YYERROR;
3112                                 }
3113                                 $$ = ulval + 1;
3114                         } else {
3115                                 if ((p = geticmptypebyname($1, AF_INET6)) ==
3116                                     NULL) {
3117                                         yyerror("unknown icmp6-type %s", $1);
3118                                         free($1);
3119                                         YYERROR;
3120                                 }
3121                                 $$ = p->type + 1;
3122                         }
3123                         free($1);
3124                 }
3125                 ;
3126
3127 tos             : TOS STRING                    {
3128                         if (!strcmp($2, "lowdelay"))
3129                                 $$ = IPTOS_LOWDELAY;
3130                         else if (!strcmp($2, "throughput"))
3131                                 $$ = IPTOS_THROUGHPUT;
3132                         else if (!strcmp($2, "reliability"))
3133                                 $$ = IPTOS_RELIABILITY;
3134                         else if ($2[0] == '0' && $2[1] == 'x')
3135                                 $$ = strtoul($2, NULL, 16);
3136                         else
3137                                 $$ = strtoul($2, NULL, 10);
3138                         if (!$$ || $$ > 255) {
3139                                 yyerror("illegal tos value %s", $2);
3140                                 free($2);
3141                                 YYERROR;
3142                         }
3143                         free($2);
3144                 }
3145                 ;
3146
3147 sourcetrack     : SOURCETRACK           { $$ = PF_SRCTRACK; }
3148                 | SOURCETRACK GLOBAL    { $$ = PF_SRCTRACK_GLOBAL; }
3149                 | SOURCETRACK RULE      { $$ = PF_SRCTRACK_RULE; }
3150                 ;
3151
3152 statelock       : IFBOUND {
3153                         $$ = PFRULE_IFBOUND;
3154                 }
3155                 | FLOATING {
3156                         $$ = 0;
3157                 }
3158                 ;
3159
3160 keep            : NO STATE                      {
3161                         $$.action = 0;
3162                         $$.options = NULL;
3163                 }
3164                 | KEEP STATE state_opt_spec     {
3165                         $$.action = PF_STATE_NORMAL;
3166                         $$.options = $3;
3167                 }
3168                 | MODULATE STATE state_opt_spec {
3169                         $$.action = PF_STATE_MODULATE;
3170                         $$.options = $3;
3171                 }
3172                 | SYNPROXY STATE state_opt_spec {
3173                         $$.action = PF_STATE_SYNPROXY;
3174                         $$.options = $3;
3175                 }
3176                 ;
3177
3178 flush           : /* empty */                   { $$ = 0; }
3179                 | FLUSH                         { $$ = PF_FLUSH; }
3180                 | FLUSH GLOBAL                  {
3181                         $$ = PF_FLUSH | PF_FLUSH_GLOBAL;
3182                 }
3183                 ;
3184
3185 state_opt_spec  : '(' state_opt_list ')'        { $$ = $2; }
3186                 | /* empty */                   { $$ = NULL; }
3187                 ;
3188
3189 state_opt_list  : state_opt_item                { $$ = $1; }
3190                 | state_opt_list comma state_opt_item {
3191                         $1->tail->next = $3;
3192                         $1->tail = $3;
3193                         $$ = $1;
3194                 }
3195                 ;
3196
3197 state_opt_item  : MAXIMUM number                {
3198                         $$ = calloc(1, sizeof(struct node_state_opt));
3199                         if ($$ == NULL)
3200                                 err(1, "state_opt_item: calloc");
3201                         $$->type = PF_STATE_OPT_MAX;
3202                         $$->data.max_states = $2;
3203                         $$->next = NULL;
3204                         $$->tail = $$;
3205                 }
3206                 | NOSYNC                                {
3207                         $$ = calloc(1, sizeof(struct node_state_opt));
3208                         if ($$ == NULL)
3209                                 err(1, "state_opt_item: calloc");
3210                         $$->type = PF_STATE_OPT_NOSYNC;
3211                         $$->next = NULL;
3212                         $$->tail = $$;
3213                 }
3214                 | MAXSRCSTATES number                   {
3215                         $$ = calloc(1, sizeof(struct node_state_opt));
3216                         if ($$ == NULL)
3217                                 err(1, "state_opt_item: calloc");
3218                         $$->type = PF_STATE_OPT_MAX_SRC_STATES;
3219                         $$->data.max_src_states = $2;
3220                         $$->next = NULL;
3221                         $$->tail = $$;
3222                 }
3223                 | MAXSRCCONN number                     {
3224                         $$ = calloc(1, sizeof(struct node_state_opt));
3225                         if ($$ == NULL)
3226                                 err(1, "state_opt_item: calloc");
3227                         $$->type = PF_STATE_OPT_MAX_SRC_CONN;
3228                         $$->data.max_src_conn = $2;
3229                         $$->next = NULL;
3230                         $$->tail = $$;
3231                 }
3232                 | MAXSRCCONNRATE number '/' number      {
3233                         $$ = calloc(1, sizeof(struct node_state_opt));
3234                         if ($$ == NULL)
3235                                 err(1, "state_opt_item: calloc");
3236                         $$->type = PF_STATE_OPT_MAX_SRC_CONN_RATE;
3237                         $$->data.max_src_conn_rate.limit = $2;
3238                         $$->data.max_src_conn_rate.seconds = $4;
3239                         $$->next = NULL;
3240                         $$->tail = $$;
3241                 }
3242                 | OVERLOAD '<' STRING '>' flush         {
3243                         if (strlen($3) >= PF_TABLE_NAME_SIZE) {
3244                                 yyerror("table name '%s' too long", $3);
3245                                 free($3);
3246                                 YYERROR;
3247                         }
3248                         $$ = calloc(1, sizeof(struct node_state_opt));
3249                         if ($$ == NULL)
3250                                 err(1, "state_opt_item: calloc");
3251                         if (strlcpy($$->data.overload.tblname, $3,
3252                             PF_TABLE_NAME_SIZE) >= PF_TABLE_NAME_SIZE)
3253                                 errx(1, "state_opt_item: strlcpy");
3254                         free($3);
3255                         $$->type = PF_STATE_OPT_OVERLOAD;
3256                         $$->data.overload.flush = $5;
3257                         $$->next = NULL;
3258                         $$->tail = $$;
3259                 }
3260                 | MAXSRCNODES number                    {
3261                         $$ = calloc(1, sizeof(struct node_state_opt));
3262                         if ($$ == NULL)
3263                                 err(1, "state_opt_item: calloc");
3264                         $$->type = PF_STATE_OPT_MAX_SRC_NODES;
3265                         $$->data.max_src_nodes = $2;
3266                         $$->next = NULL;
3267                         $$->tail = $$;
3268                 }
3269                 | PICKUPS                               {
3270                         $$ = calloc(1, sizeof(struct node_state_opt));
3271                         if ($$ == NULL)
3272                                 err(1, "state_opt_item: calloc");
3273                         $$->type = PF_STATE_OPT_PICKUPS;
3274                         $$->data.pickup_mode = PF_PICKUPS_ENABLED;
3275                         $$->next = NULL;
3276                         $$->tail = $$;
3277                 }
3278                 | NOPICKUPS                             {
3279                         $$ = calloc(1, sizeof(struct node_state_opt));
3280                         if ($$ == NULL)
3281                                 err(1, "state_opt_item: calloc");
3282                         $$->type = PF_STATE_OPT_PICKUPS;
3283                         $$->data.pickup_mode = PF_PICKUPS_DISABLED;
3284                         $$->next = NULL;
3285                         $$->tail = $$;
3286                 }
3287                 | HASHONLY                              {
3288                         $$ = calloc(1, sizeof(struct node_state_opt));
3289                         if ($$ == NULL)
3290                                 err(1, "state_opt_item: calloc");
3291                         $$->type = PF_STATE_OPT_PICKUPS;
3292                         $$->data.pickup_mode = PF_PICKUPS_HASHONLY;
3293                         $$->next = NULL;
3294                         $$->tail = $$;
3295                 }
3296                 | sourcetrack {
3297                         $$ = calloc(1, sizeof(struct node_state_opt));
3298                         if ($$ == NULL)
3299                                 err(1, "state_opt_item: calloc");
3300                         $$->type = PF_STATE_OPT_SRCTRACK;
3301                         $$->data.src_track = $1;
3302                         $$->next = NULL;
3303                         $$->tail = $$;
3304                 }
3305                 | statelock {
3306                         $$ = calloc(1, sizeof(struct node_state_opt));
3307                         if ($$ == NULL)
3308                                 err(1, "state_opt_item: calloc");
3309                         $$->type = PF_STATE_OPT_STATELOCK;
3310                         $$->data.statelock = $1;
3311                         $$->next = NULL;
3312                         $$->tail = $$;
3313                 }
3314                 | STRING number                 {
3315                         int     i;
3316
3317                         for (i = 0; pf_timeouts[i].name &&
3318                             strcmp(pf_timeouts[i].name, $1); ++i)
3319                                 ;       /* nothing */
3320                         if (!pf_timeouts[i].name) {
3321                                 yyerror("illegal timeout name %s", $1);
3322                                 free($1);
3323                                 YYERROR;
3324                         }
3325                         if (strchr(pf_timeouts[i].name, '.') == NULL) {
3326                                 yyerror("illegal state timeout %s", $1);
3327                                 free($1);
3328                                 YYERROR;
3329                         }
3330                         free($1);
3331                         $$ = calloc(1, sizeof(struct node_state_opt));
3332                         if ($$ == NULL)
3333                                 err(1, "state_opt_item: calloc");
3334                         $$->type = PF_STATE_OPT_TIMEOUT;
3335                         $$->data.timeout.number = pf_timeouts[i].timeout;
3336                         $$->data.timeout.seconds = $2;
3337                         $$->next = NULL;
3338                         $$->tail = $$;
3339                 }
3340                 ;
3341
3342 label           : LABEL STRING                  {
3343                         $$ = $2;
3344                 }
3345                 ;
3346
3347 qname           : QUEUE STRING                          {
3348                         $$.qname = $2;
3349                 }
3350                 | QUEUE '(' STRING ')'                  {
3351                         $$.qname = $3;
3352                 }
3353                 | QUEUE '(' STRING comma STRING ')'     {
3354                         $$.qname = $3;
3355                         $$.pqname = $5;
3356                 }
3357                 ;
3358
3359 no              : /* empty */                   { $$ = 0; }
3360                 | NO                            { $$ = 1; }
3361                 ;
3362
3363 rport           : STRING                        {
3364                         char    *p = strchr($1, ':');
3365
3366                         if (p == NULL) {
3367                                 if (($$.a = getservice($1)) == -1) {
3368                                         free($1);
3369                                         YYERROR;
3370                                 }
3371                                 $$.b = $$.t = 0;
3372                         } else if (!strcmp(p+1, "*")) {
3373                                 *p = 0;
3374                                 if (($$.a = getservice($1)) == -1) {
3375                                         free($1);
3376                                         YYERROR;
3377                                 }
3378                                 $$.b = 0;
3379                                 $$.t = 1;
3380                         } else {
3381                                 *p++ = 0;
3382                                 if (($$.a = getservice($1)) == -1 ||
3383                                     ($$.b = getservice(p)) == -1) {
3384                                         free($1);
3385                                         YYERROR;
3386                                 }
3387                                 if ($$.a == $$.b)
3388                                         $$.b = 0;
3389                                 $$.t = 0;
3390                         }
3391                         free($1);
3392                 }
3393                 ;
3394
3395 redirspec       : host                          { $$ = $1; }
3396                 | '{' redir_host_list '}'       { $$ = $2; }
3397                 ;
3398
3399 redir_host_list : host                          { $$ = $1; }
3400                 | redir_host_list comma host    {
3401                         $1->tail->next = $3;
3402                         $1->tail = $3->tail;
3403                         $$ = $1;
3404                 }
3405                 ;
3406
3407 redirpool       : /* empty */                   { $$ = NULL; }
3408                 | ARROW redirspec               {
3409                         $$ = calloc(1, sizeof(struct redirection));
3410                         if ($$ == NULL)
3411                                 err(1, "redirection: calloc");
3412                         $$->host = $2;
3413                         $$->rport.a = $$->rport.b = $$->rport.t = 0;
3414                 }
3415                 | ARROW redirspec PORT rport    {
3416                         $$ = calloc(1, sizeof(struct redirection));
3417                         if ($$ == NULL)
3418                                 err(1, "redirection: calloc");
3419                         $$->host = $2;
3420                         $$->rport = $4;
3421                 }
3422                 ;
3423
3424 hashkey         : /* empty */
3425                 {
3426                         $$ = calloc(1, sizeof(struct pf_poolhashkey));
3427                         if ($$ == NULL)
3428                                 err(1, "hashkey: calloc");
3429                         $$->key32[0] = arc4random();
3430                         $$->key32[1] = arc4random();
3431                         $$->key32[2] = arc4random();
3432                         $$->key32[3] = arc4random();
3433                 }
3434                 | string
3435                 {
3436                         if (!strncmp($1, "0x", 2)) {
3437                                 if (strlen($1) != 34) {
3438                                         free($1);
3439                                         yyerror("hex key must be 128 bits "
3440                                                 "(32 hex digits) long");
3441                                         YYERROR;
3442                                 }
3443                                 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3444                                 if ($$ == NULL)
3445                                         err(1, "hashkey: calloc");
3446
3447                                 if (sscanf($1, "0x%8x%8x%8x%8x",
3448                                     &$$->key32[0], &$$->key32[1],
3449                                     &$$->key32[2], &$$->key32[3]) != 4) {
3450                                         free($$);
3451                                         free($1);
3452                                         yyerror("invalid hex key");
3453                                         YYERROR;
3454                                 }
3455                         } else {
3456                                 MD5_CTX context;
3457
3458                                 $$ = calloc(1, sizeof(struct pf_poolhashkey));
3459                                 if ($$ == NULL)
3460                                         err(1, "hashkey: calloc");
3461                                 MD5Init(&context);
3462                                 MD5Update(&context, (unsigned char *)$1,
3463                                     strlen($1));
3464                                 MD5Final((unsigned char *)$$, &context);
3465                                 $$->key32[0] = htonl($$->key32[0]);
3466                                 $$->key32[1] = htonl($$->key32[1]);
3467                                 $$->key32[2] = htonl($$->key32[2]);
3468                                 $$->key32[3] = htonl($$->key32[3]);
3469                         }
3470                         free($1);
3471                 }
3472                 ;
3473
3474 pool_opts       :       { bzero(&pool_opts, sizeof pool_opts); }
3475                     pool_opts_l
3476                         { $$ = pool_opts; }
3477                 | /* empty */   {
3478                         bzero(&pool_opts, sizeof pool_opts);
3479                         $$ = pool_opts;
3480                 }
3481                 ;
3482
3483 pool_opts_l     : pool_opts_l pool_opt
3484                 | pool_opt
3485                 ;
3486
3487 pool_opt        : BITMASK       {
3488                         if (pool_opts.type) {
3489                                 yyerror("pool type cannot be redefined");
3490                                 YYERROR;
3491                         }
3492                         pool_opts.type =  PF_POOL_BITMASK;
3493                 }
3494                 | RANDOM        {
3495                         if (pool_opts.type) {
3496                                 yyerror("pool type cannot be redefined");
3497                                 YYERROR;
3498                         }
3499                         pool_opts.type = PF_POOL_RANDOM;
3500                 }
3501                 | SOURCEHASH hashkey {
3502                         if (pool_opts.type) {
3503                                 yyerror("pool type cannot be redefined");
3504                                 YYERROR;
3505                         }
3506                         pool_opts.type = PF_POOL_SRCHASH;
3507                         pool_opts.key = $2;
3508                 }
3509                 | ROUNDROBIN    {
3510                         if (pool_opts.type) {
3511                                 yyerror("pool type cannot be redefined");
3512                                 YYERROR;
3513                         }
3514                         pool_opts.type = PF_POOL_ROUNDROBIN;
3515                 }
3516                 | STATICPORT    {
3517                         if (pool_opts.staticport) {
3518                                 yyerror("static-port cannot be redefined");
3519                                 YYERROR;
3520                         }
3521                         pool_opts.staticport = 1;
3522                 }
3523                 | STICKYADDRESS {
3524                         if (filter_opts.marker & POM_STICKYADDRESS) {
3525                                 yyerror("sticky-address cannot be redefined");
3526                                 YYERROR;
3527                         }
3528                         pool_opts.marker |= POM_STICKYADDRESS;
3529                         pool_opts.opts |= PF_POOL_STICKYADDR;
3530                 }
3531                 ;
3532
3533 redirection     : /* empty */                   { $$ = NULL; }
3534                 | ARROW host                    {
3535                         $$ = calloc(1, sizeof(struct redirection));
3536                         if ($$ == NULL)
3537                                 err(1, "redirection: calloc");
3538                         $$->host = $2;
3539                         $$->rport.a = $$->rport.b = $$->rport.t = 0;
3540                 }
3541                 | ARROW host PORT rport {
3542                         $$ = calloc(1, sizeof(struct redirection));
3543                         if ($$ == NULL)
3544                                 err(1, "redirection: calloc");
3545                         $$->host = $2;
3546                         $$->rport = $4;
3547                 }
3548                 ;
3549
3550 natpass         : /* empty */   { $$.b1 = $$.b2 = 0; }
3551                 | PASS          { $$.b1 = 1; $$.b2 = 0; }
3552                 | PASS log      { $$.b1 = 1; $$.b2 = $2.log; $$.w2 = $2.logif; }
3553                 ;
3554
3555 nataction       : no NAT natpass {
3556                         if ($1 && $3.b1) {
3557                                 yyerror("\"pass\" not valid with \"no\"");
3558                                 YYERROR;
3559                         }
3560                         if ($1)
3561                                 $$.b1 = PF_NONAT;
3562                         else
3563                                 $$.b1 = PF_NAT;
3564                         $$.b2 = $3.b1;
3565                         $$.w = $3.b2;
3566                         $$.w2 = $3.w2;
3567                 }
3568                 | no RDR natpass {
3569                         if ($1 && $3.b1) {
3570                                 yyerror("\"pass\" not valid with \"no\"");
3571                                 YYERROR;
3572                         }
3573                         if ($1)
3574                                 $$.b1 = PF_NORDR;
3575                         else
3576                                 $$.b1 = PF_RDR;
3577                         $$.b2 = $3.b1;
3578                         $$.w = $3.b2;
3579                         $$.w2 = $3.w2;
3580                 }
3581                 ;
3582
3583 natrule         : nataction interface af proto fromto tag tagged rtable
3584                     redirpool pool_opts
3585                 {
3586                         struct pf_rule  r;
3587
3588                         if (check_rulestate(PFCTL_STATE_NAT))
3589                                 YYERROR;
3590
3591                         memset(&r, 0, sizeof(r));
3592
3593                         r.action = $1.b1;
3594                         r.natpass = $1.b2;
3595                         r.log = $1.w;
3596                         r.logif = $1.w2;
3597                         r.af = $3;
3598
3599                         if (!r.af) {
3600                                 if ($5.src.host && $5.src.host->af &&
3601                                     !$5.src.host->ifindex)
3602                                         r.af = $5.src.host->af;
3603                                 else if ($5.dst.host && $5.dst.host->af &&
3604                                     !$5.dst.host->ifindex)
3605                                         r.af = $5.dst.host->af;
3606                         }
3607
3608                         if ($6 != NULL)
3609                                 if (strlcpy(r.tagname, $6, PF_TAG_NAME_SIZE) >=
3610                                     PF_TAG_NAME_SIZE) {
3611                                         yyerror("tag too long, max %u chars",
3612                                             PF_TAG_NAME_SIZE - 1);
3613                                         YYERROR;
3614                                 }
3615
3616                         if ($7.name)
3617                                 if (strlcpy(r.match_tagname, $7.name,
3618                                     PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
3619                                         yyerror("tag too long, max %u chars",
3620                                             PF_TAG_NAME_SIZE - 1);
3621                                         YYERROR;
3622                                 }
3623                         r.match_tag_not = $7.neg;
3624                         r.rtableid = $8;
3625
3626                         if (r.action == PF_NONAT || r.action == PF_NORDR) {
3627                                 if ($9 != NULL) {
3628                                         yyerror("translation rule with 'no' "
3629                                             "does not need '->'");
3630                                         YYERROR;
3631                                 }
3632                         } else {
3633                                 if ($9 == NULL || $9->host == NULL) {
3634                                         yyerror("translation rule requires '-> "
3635                                             "address'");
3636                                         YYERROR;
3637                                 }
3638                                 if (!r.af && ! $9->host->ifindex)
3639                                         r.af = $9->host->af;
3640
3641                                 remove_invalid_hosts(&$9->host, &r.af);
3642                                 if (invalid_redirect($9->host, r.af))
3643                                         YYERROR;
3644                                 if (check_netmask($9->host, r.af))
3645                                         YYERROR;
3646
3647                                 r.rpool.proxy_port[0] = ntohs($9->rport.a);
3648
3649                                 switch (r.action) {
3650                                 case PF_RDR:
3651                                         if (!$9->rport.b && $9->rport.t &&
3652                                             $5.dst.port != NULL) {
3653                                                 r.rpool.proxy_port[1] =
3654                                                     ntohs($9->rport.a) +
3655                                                     (ntohs(
3656                                                     $5.dst.port->port[1]) -
3657                                                     ntohs(
3658                                                     $5.dst.port->port[0]));
3659                                         } else
3660                                                 r.rpool.proxy_port[1] =
3661                                                     ntohs($9->rport.b);
3662                                         break;
3663                                 case PF_NAT:
3664                                         r.rpool.proxy_port[1] =
3665                                             ntohs($9->rport.b);
3666                                         if (!r.rpool.proxy_port[0] &&
3667                                             !r.rpool.proxy_port[1]) {
3668                                                 r.rpool.proxy_port[0] =
3669                                                     PF_NAT_PROXY_PORT_LOW;
3670                                                 r.rpool.proxy_port[1] =
3671                                                     PF_NAT_PROXY_PORT_HIGH;
3672                                         } else if (!r.rpool.proxy_port[1])
3673                                                 r.rpool.proxy_port[1] =
3674                                                     r.rpool.proxy_port[0];
3675                                         break;
3676                                 default:
3677                                         break;
3678                                 }
3679
3680                                 r.rpool.opts = $10.type;
3681                                 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
3682                                     PF_POOL_NONE && ($9->host->next != NULL ||
3683                                     $9->host->addr.type == PF_ADDR_TABLE ||
3684                                     DYNIF_MULTIADDR($9->host->addr)))
3685                                         r.rpool.opts = PF_POOL_ROUNDROBIN;
3686                                 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
3687                                     PF_POOL_ROUNDROBIN &&
3688                                     disallow_table($9->host, "tables are only "
3689                                     "supported in round-robin redirection "
3690                                     "pools"))
3691                                         YYERROR;
3692                                 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
3693                                     PF_POOL_ROUNDROBIN &&
3694                                     disallow_alias($9->host, "interface (%s) "
3695                                     "is only supported in round-robin "
3696                                     "redirection pools"))
3697                                         YYERROR;
3698                                 if ($9->host->next != NULL) {
3699                                         if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
3700                                             PF_POOL_ROUNDROBIN) {
3701                                                 yyerror("only round-robin "
3702                                                     "valid for multiple "
3703                                                     "redirection addresses");
3704                                                 YYERROR;
3705                                         }
3706                                 }
3707                         }
3708
3709                         if ($10.key != NULL)
3710                                 memcpy(&r.rpool.key, $10.key,
3711                                     sizeof(struct pf_poolhashkey));
3712
3713                          if ($10.opts)
3714                                 r.rpool.opts |= $10.opts;
3715
3716                         if ($10.staticport) {
3717                                 if (r.action != PF_NAT) {
3718                                         yyerror("the 'static-port' option is "
3719                                             "only valid with nat rules");
3720                                         YYERROR;
3721                                 }
3722                                 if (r.rpool.proxy_port[0] !=
3723                                     PF_NAT_PROXY_PORT_LOW &&
3724                                     r.rpool.proxy_port[1] !=
3725                                     PF_NAT_PROXY_PORT_HIGH) {
3726                                         yyerror("the 'static-port' option can't"
3727                                             " be used when specifying a port"
3728                                             " range");
3729                                         YYERROR;
3730                                 }
3731                                 r.rpool.proxy_port[0] = 0;
3732                                 r.rpool.proxy_port[1] = 0;
3733                         }
3734
3735                         expand_rule(&r, $2, $9 == NULL ? NULL : $9->host, $4,
3736                             $5.src_os, $5.src.host, $5.src.port, $5.dst.host,
3737                             $5.dst.port, 0, 0, 0, "");
3738                         free($9);
3739                 }
3740                 ;
3741
3742 binatrule       : no BINAT natpass interface af proto FROM host TO ipspec tag
3743                     tagged rtable redirection
3744                 {
3745                         struct pf_rule          binat;
3746                         struct pf_pooladdr      *pa;
3747
3748                         if (check_rulestate(PFCTL_STATE_NAT))
3749                                 YYERROR;
3750                         if (disallow_urpf_failed($10, "\"urpf-failed\" is not "
3751                             "permitted as a binat destination"))
3752                                 YYERROR;
3753
3754                         memset(&binat, 0, sizeof(binat));
3755
3756                         if ($1 && $3.b1) {
3757                                 yyerror("\"pass\" not valid with \"no\"");
3758                                 YYERROR;
3759                         }
3760                         if ($1)
3761                                 binat.action = PF_NOBINAT;
3762                         else
3763                                 binat.action = PF_BINAT;
3764                         binat.natpass = $3.b1;
3765                         binat.log = $3.b2;
3766                         binat.logif = $3.w2;
3767                         binat.af = $5;
3768                         if (!binat.af && $8 != NULL && $8->af)
3769                                 binat.af = $8->af;
3770                         if (!binat.af && $10 != NULL && $10->af)
3771                                 binat.af = $10->af;
3772
3773                         if (!binat.af && $14 != NULL && $14->host)
3774                                 binat.af = $14->host->af;
3775                         if (!binat.af) {
3776                                 yyerror("address family (inet/inet6) "
3777                                     "undefined");
3778                                 YYERROR;
3779                         }
3780
3781                         if ($4 != NULL) {
3782                                 memcpy(binat.ifname, $4->ifname,
3783                                     sizeof(binat.ifname));
3784                                 binat.ifnot = $4->not;
3785                                 free($4);
3786                         }
3787
3788                         if ($11 != NULL)
3789                                 if (strlcpy(binat.tagname, $11,
3790                                     PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
3791                                         yyerror("tag too long, max %u chars",
3792                                             PF_TAG_NAME_SIZE - 1);
3793                                         YYERROR;
3794                                 }
3795                         if ($12.name)
3796                                 if (strlcpy(binat.match_tagname, $12.name,
3797                                     PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
3798                                         yyerror("tag too long, max %u chars",
3799                                             PF_TAG_NAME_SIZE - 1);
3800                                         YYERROR;
3801                                 }
3802                         binat.match_tag_not = $12.neg;
3803                         binat.rtableid = $13;
3804
3805                         if ($6 != NULL) {
3806                                 binat.proto = $6->proto;
3807                                 free($6);
3808                         }
3809
3810                         if ($8 != NULL && disallow_table($8, "invalid use of "
3811                             "table <%s> as the source address of a binat rule"))
3812                                 YYERROR;
3813                         if ($8 != NULL && disallow_alias($8, "invalid use of "
3814                             "interface (%s) as the source address of a binat "
3815                             "rule"))
3816                                 YYERROR;
3817                         if ($14 != NULL && $14->host != NULL && disallow_table(
3818                             $14->host, "invalid use of table <%s> as the "
3819                             "redirect address of a binat rule"))
3820                                 YYERROR;
3821                         if ($14 != NULL && $14->host != NULL && disallow_alias(
3822                             $14->host, "invalid use of interface (%s) as the "
3823                             "redirect address of a binat rule"))
3824                                 YYERROR;
3825
3826                         if ($8 != NULL) {
3827                                 if ($8->next) {
3828                                         yyerror("multiple binat ip addresses");
3829                                         YYERROR;
3830                                 }
3831                                 if ($8->addr.type == PF_ADDR_DYNIFTL)
3832                                         $8->af = binat.af;
3833                                 if ($8->af != binat.af) {
3834                                         yyerror("binat ip versions must match");
3835                                         YYERROR;
3836                                 }
3837                                 if (check_netmask($8, binat.af))
3838                                         YYERROR;
3839                                 memcpy(&binat.src.addr, &$8->addr,
3840                                     sizeof(binat.src.addr));
3841                                 free($8);
3842                         }
3843                         if ($10 != NULL) {
3844                                 if ($10->next) {
3845                                         yyerror("multiple binat ip addresses");
3846                                         YYERROR;
3847                                 }
3848                                 if ($10->af != binat.af && $10->af) {
3849                                         yyerror("binat ip versions must match");
3850                                         YYERROR;
3851                                 }
3852                                 if (check_netmask($10, binat.af))
3853                                         YYERROR;
3854                                 memcpy(&binat.dst.addr, &$10->addr,
3855                                     sizeof(binat.dst.addr));
3856                                 binat.dst.neg = $10->not;
3857                                 free($10);
3858                         }
3859
3860                         if (binat.action == PF_NOBINAT) {
3861                                 if ($14 != NULL) {
3862                                         yyerror("'no binat' rule does not need"
3863                                             " '->'");
3864                                         YYERROR;
3865                                 }
3866                         } else {
3867                                 if ($14 == NULL || $14->host == NULL) {
3868                                         yyerror("'binat' rule requires"
3869                                             " '-> address'");
3870                                         YYERROR;
3871                                 }
3872
3873                                 remove_invalid_hosts(&$14->host, &binat.af);
3874                                 if (invalid_redirect($14->host, binat.af))
3875                                         YYERROR;
3876                                 if ($14->host->next != NULL) {
3877                                         yyerror("binat rule must redirect to "
3878                                             "a single address");
3879                                         YYERROR;
3880                                 }
3881                                 if (check_netmask($14->host, binat.af))
3882                                         YYERROR;
3883
3884                                 if (!PF_AZERO(&binat.src.addr.v.a.mask,
3885                                     binat.af) &&
3886                                     !PF_AEQ(&binat.src.addr.v.a.mask,
3887                                     &$14->host->addr.v.a.mask, binat.af)) {
3888                                         yyerror("'binat' source mask and "
3889                                             "redirect mask must be the same");
3890                                         YYERROR;
3891                                 }
3892
3893                                 TAILQ_INIT(&binat.rpool.list);
3894                                 pa = calloc(1, sizeof(struct pf_pooladdr));
3895                                 if (pa == NULL)
3896                                         err(1, "binat: calloc");
3897                                 pa->addr = $14->host->addr;
3898                                 pa->ifname[0] = 0;
3899                                 TAILQ_INSERT_TAIL(&binat.rpool.list,
3900                                     pa, entries);
3901
3902                                 free($14);
3903                         }
3904
3905                         pfctl_add_rule(pf, &binat, "");
3906                 }
3907                 ;
3908
3909 tag             : /* empty */           { $$ = NULL; }
3910                 | TAG STRING            { $$ = $2; }
3911                 ;
3912
3913 tagged          : /* empty */           { $$.neg = 0; $$.name = NULL; }
3914                 | not TAGGED string     { $$.neg = $1; $$.name = $3; }
3915                 ;
3916
3917 rtable          : /* empty */           { $$ = -1; }
3918                 | RTABLE number         {
3919                         $$ = $2;
3920                 }
3921                 ;
3922
3923 route_host      : STRING                        {
3924                         $$ = calloc(1, sizeof(struct node_host));
3925                         if ($$ == NULL)
3926                                 err(1, "route_host: calloc");
3927                         $$->ifname = $1;
3928                         set_ipmask($$, 128);
3929                         $$->next = NULL;
3930                         $$->tail = $$;
3931                 }
3932                 | '(' STRING host ')'           {
3933                         $$ = $3;
3934                         $$->ifname = $2;
3935                 }
3936                 ;
3937
3938 route_host_list : route_host                            { $$ = $1; }
3939                 | route_host_list comma route_host      {
3940                         if ($1->af == 0)
3941                                 $1->af = $3->af;
3942                         if ($1->af != $3->af) {
3943                                 yyerror("all pool addresses must be in the "
3944                                     "same address family");
3945                                 YYERROR;
3946                         }
3947                         $1->tail->next = $3;
3948                         $1->tail = $3->tail;
3949                         $$ = $1;
3950                 }
3951                 ;
3952
3953 routespec       : route_host                    { $$ = $1; }
3954                 | '{' route_host_list '}'       { $$ = $2; }
3955                 ;
3956
3957 route           : /* empty */                   {
3958                         $$.host = NULL;
3959                         $$.rt = 0;
3960                         $$.pool_opts = 0;
3961                 }
3962                 | FASTROUTE {
3963                         $$.host = NULL;
3964                         $$.rt = PF_FASTROUTE;
3965                         $$.pool_opts = 0;
3966                 }
3967                 | ROUTETO routespec pool_opts {
3968                         $$.host = $2;
3969                         $$.rt = PF_ROUTETO;
3970                         $$.pool_opts = $3.type | $3.opts;
3971                         if ($3.key != NULL)
3972                                 $$.key = $3.key;
3973                 }
3974                 | REPLYTO routespec pool_opts {
3975                         $$.host = $2;
3976                         $$.rt = PF_REPLYTO;
3977                         $$.pool_opts = $3.type | $3.opts;
3978                         if ($3.key != NULL)
3979                                 $$.key = $3.key;
3980                 }
3981                 | DUPTO routespec pool_opts {
3982                         $$.host = $2;
3983                         $$.rt = PF_DUPTO;
3984                         $$.pool_opts = $3.type | $3.opts;
3985                         if ($3.key != NULL)
3986                                 $$.key = $3.key;
3987                 }
3988                 ;
3989
3990 timeout_spec    : STRING number
3991                 {
3992                         if (check_rulestate(PFCTL_STATE_OPTION)) {
3993                                 free($1);
3994                                 YYERROR;
3995                         }
3996                         if (pfctl_set_timeout(pf, $1, $2, 0) != 0) {
3997                                 yyerror("unknown timeout %s", $1);
3998                                 free($1);
3999                                 YYERROR;
4000                         }
4001                         free($1);
4002                 }
4003                 ;
4004
4005 timeout_list    : timeout_list comma timeout_spec
4006                 | timeout_spec
4007                 ;
4008
4009 limit_spec      : STRING number
4010                 {
4011                         if (check_rulestate(PFCTL_STATE_OPTION)) {
4012                                 free($1);
4013                                 YYERROR;
4014                         }
4015                         if (pfctl_set_limit(pf, $1, $2) != 0) {
4016                                 yyerror("unable to set limit %s %u", $1, $2);
4017                                 free($1);
4018                                 YYERROR;
4019                         }
4020                         free($1);
4021                 }
4022                 ;
4023
4024 limit_list      : limit_list comma limit_spec
4025                 | limit_spec
4026                 ;
4027
4028 comma           : ','
4029                 | /* empty */
4030                 ;
4031
4032 yesno           : NO                    { $$ = 0; }
4033                 | STRING                {
4034                         if (!strcmp($1, "yes"))
4035                                 $$ = 1;
4036                         else {
4037                                 yyerror("invalid value '%s', expected 'yes' "
4038                                     "or 'no'", $1);
4039                                 free($1);
4040                                 YYERROR;
4041                         }
4042                         free($1);
4043                 }
4044                 ;
4045
4046 unaryop         : '='           { $$ = PF_OP_EQ; }
4047                 | '!' '='       { $$ = PF_OP_NE; }
4048                 | '<' '='       { $$ = PF_OP_LE; }
4049                 | '<'           { $$ = PF_OP_LT; }
4050                 | '>' '='       { $$ = PF_OP_GE; }
4051                 | '>'           { $$ = PF_OP_GT; }
4052                 ;
4053
4054 %%
4055
4056 int
4057 yyerror(const char *fmt, ...)
4058 {
4059         va_list          ap;
4060
4061         errors = 1;
4062         va_start(ap, fmt);
4063         fprintf(stderr, "%s:%d: ", infile, yylval.lineno);
4064         vfprintf(stderr, fmt, ap);
4065         fprintf(stderr, "\n");
4066         va_end(ap);
4067         return (0);
4068 }
4069
4070 int
4071 disallow_table(struct node_host *h, const char *fmt)
4072 {
4073         for (; h != NULL; h = h->next)
4074                 if (h->addr.type == PF_ADDR_TABLE) {
4075                         yyerror(fmt, h->addr.v.tblname);
4076                         return (1);
4077                 }
4078         return (0);
4079 }
4080
4081 int
4082 disallow_urpf_failed(struct node_host *h, const char *fmt)
4083 {
4084         for (; h != NULL; h = h->next)
4085                 if (h->addr.type == PF_ADDR_URPFFAILED) {
4086                         yyerror(fmt);
4087                         return (1);
4088                 }
4089         return (0);
4090 }
4091
4092 int
4093 disallow_alias(struct node_host *h, const char *fmt)
4094 {
4095         for (; h != NULL; h = h->next)
4096                 if (DYNIF_MULTIADDR(h->addr)) {
4097                         yyerror(fmt, h->addr.v.tblname);
4098                         return (1);
4099                 }
4100         return (0);
4101 }
4102
4103 int
4104 rule_consistent(struct pf_rule *r, int anchor_call __unused)
4105 {
4106         int     problems = 0;
4107
4108         switch (r->action) {
4109         case PF_PASS:
4110         case PF_DROP:
4111         case PF_SCRUB:
4112         case PF_NOSCRUB:
4113                 problems = filter_consistent(r, anchor_call);
4114                 break;
4115         case PF_NAT:
4116         case PF_NONAT:
4117                 problems = nat_consistent(r);
4118                 break;
4119         case PF_RDR:
4120         case PF_NORDR:
4121                 problems = rdr_consistent(r);
4122                 break;
4123         case PF_BINAT:
4124         case PF_NOBINAT:
4125         default:
4126                 break;
4127         }
4128         return (problems);
4129 }
4130
4131 int
4132 filter_consistent(struct pf_rule *r, int anchor_call __unused)
4133 {
4134         int     problems = 0;
4135
4136         if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP &&
4137             (r->src.port_op || r->dst.port_op)) {
4138                 yyerror("port only applies to tcp/udp");
4139                 problems++;
4140         }
4141         if (r->proto != IPPROTO_ICMP && r->proto != IPPROTO_ICMPV6 &&
4142             (r->type || r->code)) {
4143                 yyerror("icmp-type/code only applies to icmp");
4144                 problems++;
4145         }
4146         if (!r->af && (r->type || r->code)) {
4147                 yyerror("must indicate address family with icmp-type/code");
4148                 problems++;
4149         }
4150         if (r->overload_tblname[0] &&
4151             r->max_src_conn == 0 && r->max_src_conn_rate.seconds == 0) {
4152                 yyerror("'overload' requires 'max-src-conn' "
4153                     "or 'max-src-conn-rate'");
4154                 problems++;
4155         }
4156         if ((r->proto == IPPROTO_ICMP && r->af == AF_INET6) ||
4157             (r->proto == IPPROTO_ICMPV6 && r->af == AF_INET)) {
4158                 yyerror("proto %s doesn't match address family %s",
4159                     r->proto == IPPROTO_ICMP ? "icmp" : "icmp6",
4160                     r->af == AF_INET ? "inet" : "inet6");
4161                 problems++;
4162         }
4163         if (r->allow_opts && r->action != PF_PASS) {
4164                 yyerror("allow-opts can only be specified for pass rules");
4165                 problems++;
4166         }
4167         if (r->rule_flag & PFRULE_FRAGMENT && (r->src.port_op ||
4168             r->dst.port_op || r->flagset || r->type || r->code)) {
4169                 yyerror("fragments can be filtered only on IP header fields");
4170                 problems++;
4171         }
4172         if (r->rule_flag & PFRULE_RETURNRST && r->proto != IPPROTO_TCP) {
4173                 yyerror("return-rst can only be applied to TCP rules");
4174                 problems++;
4175         }
4176         if (r->max_src_nodes && !(r->rule_flag & PFRULE_RULESRCTRACK)) {
4177                 yyerror("max-src-nodes requires 'source-track rule'");
4178                 problems++;
4179         }
4180         if (r->action == PF_DROP && r->keep_state) {
4181                 yyerror("keep state on block rules doesn't make sense");
4182                 problems++;
4183         }
4184         return (-problems);
4185 }
4186
4187 int
4188 nat_consistent(struct pf_rule *r __unused)
4189 {
4190         return (0);     /* yeah! */
4191 }
4192
4193 int
4194 rdr_consistent(struct pf_rule *r)
4195 {
4196         int                      problems = 0;
4197
4198         if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP) {
4199                 if (r->src.port_op) {
4200                         yyerror("src port only applies to tcp/udp");
4201                         problems++;
4202                 }
4203                 if (r->dst.port_op) {
4204                         yyerror("dst port only applies to tcp/udp");
4205                         problems++;
4206                 }
4207                 if (r->rpool.proxy_port[0]) {
4208                         yyerror("rpool port only applies to tcp/udp");
4209                         problems++;
4210                 }
4211         }
4212         if (r->dst.port_op &&
4213             r->dst.port_op != PF_OP_EQ && r->dst.port_op != PF_OP_RRG) {
4214                 yyerror("invalid port operator for rdr destination port");
4215                 problems++;
4216         }
4217         return (-problems);
4218 }
4219
4220 int
4221 process_tabledef(char *name, struct table_opts *opts)
4222 {
4223         struct pfr_buffer        ab;
4224         struct node_tinit       *ti;
4225
4226         bzero(&ab, sizeof(ab));
4227         ab.pfrb_type = PFRB_ADDRS;
4228         SIMPLEQ_FOREACH(ti, &opts->init_nodes, entries) {
4229                 if (ti->file)
4230                         if (pfr_buf_load(&ab, ti->file, 0, append_addr)) {
4231                                 if (errno)
4232                                         yyerror("cannot load \"%s\": %s",
4233                                             ti->file, strerror(errno));
4234                                 else
4235                                         yyerror("file \"%s\" contains bad data",
4236                                             ti->file);
4237                                 goto _error;
4238                         }
4239                 if (ti->host)
4240                         if (append_addr_host(&ab, ti->host, 0, 0)) {
4241                                 yyerror("cannot create address buffer: %s",
4242                                     strerror(errno));
4243                                 goto _error;
4244                         }
4245         }
4246         if (pf->opts & PF_OPT_VERBOSE)
4247                 print_tabledef(name, opts->flags, opts->init_addr,
4248                     &opts->init_nodes);
4249         if (!(pf->opts & PF_OPT_NOACTION) &&
4250             pfctl_define_table(name, opts->flags, opts->init_addr,
4251             pf->anchor->name, &ab, pf->anchor->ruleset.tticket)) {
4252                 yyerror("cannot define table %s: %s", name,
4253                     pfr_strerror(errno));
4254                 goto _error;
4255         }
4256         pf->tdirty = 1;
4257         pfr_buf_clear(&ab);
4258         return (0);
4259 _error:
4260         pfr_buf_clear(&ab);
4261         return (-1);
4262 }
4263
4264 struct keywords {
4265         const char      *k_name;
4266         int              k_val;
4267 };
4268
4269 /* macro gore, but you should've seen the prior indentation nightmare... */
4270
4271 #define FREE_LIST(T,r) \
4272         do { \
4273                 T *p, *node = r; \
4274                 while (node != NULL) { \
4275                         p = node; \
4276                         node = node->next; \
4277                         free(p); \
4278                 } \
4279         } while (0)
4280
4281 #define LOOP_THROUGH(T,n,r,C) \
4282         do { \
4283                 T *n; \
4284                 if (r == NULL) { \
4285                         r = calloc(1, sizeof(T)); \
4286                         if (r == NULL) \
4287                                 err(1, "LOOP: calloc"); \
4288                         r->next = NULL; \
4289                 } \
4290                 n = r; \
4291                 while (n != NULL) { \
4292                         do { \
4293                                 C; \
4294                         } while (0); \
4295                         n = n->next; \
4296                 } \
4297         } while (0)
4298
4299 void
4300 expand_label_str(char *label, size_t len, const char *srch, const char *repl)
4301 {
4302         char *tmp;
4303         char *p, *q;
4304
4305         if ((tmp = calloc(1, len)) == NULL)
4306                 err(1, "expand_label_str: calloc");
4307         p = q = label;
4308         while ((q = strstr(p, srch)) != NULL) {
4309                 *q = '\0';
4310                 if ((strlcat(tmp, p, len) >= len) ||
4311                     (strlcat(tmp, repl, len) >= len))
4312                         errx(1, "expand_label: label too long");
4313                 q += strlen(srch);
4314                 p = q;
4315         }
4316         if (strlcat(tmp, p, len) >= len)
4317                 errx(1, "expand_label: label too long");
4318         strlcpy(label, tmp, len);       /* always fits */
4319         free(tmp);
4320 }
4321
4322 void
4323 expand_label_if(const char *name, char *label, size_t len, const char *ifname)
4324 {
4325         if (strstr(label, name) != NULL) {
4326                 if (!*ifname)
4327                         expand_label_str(label, len, name, "any");
4328                 else
4329                         expand_label_str(label, len, name, ifname);
4330         }
4331 }
4332
4333 void
4334 expand_label_addr(const char *name, char *label, size_t len, sa_family_t af,
4335     struct node_host *h)
4336 {
4337         char tmp[64], tmp_not[66];
4338
4339         if (strstr(label, name) != NULL) {
4340                 switch (h->addr.type) {
4341                 case PF_ADDR_DYNIFTL:
4342                         snprintf(tmp, sizeof(tmp), "(%s)", h->addr.v.ifname);
4343                         break;
4344                 case PF_ADDR_TABLE:
4345                         snprintf(tmp, sizeof(tmp), "<%s>", h->addr.v.tblname);
4346                         break;
4347                 case PF_ADDR_NOROUTE:
4348                         snprintf(tmp, sizeof(tmp), "no-route");
4349                         break;
4350                 case PF_ADDR_URPFFAILED:
4351                         snprintf(tmp, sizeof(tmp), "urpf-failed");
4352                         break;
4353                 case PF_ADDR_ADDRMASK:
4354                         if (!af || (PF_AZERO(&h->addr.v.a.addr, af) &&
4355                             PF_AZERO(&h->addr.v.a.mask, af)))
4356                                 snprintf(tmp, sizeof(tmp), "any");
4357                         else {
4358                                 char    a[48];
4359                                 int     bits;
4360
4361                                 if (inet_ntop(af, &h->addr.v.a.addr, a,
4362                                     sizeof(a)) == NULL)
4363                                         snprintf(tmp, sizeof(tmp), "?");
4364                                 else {
4365                                         bits = unmask(&h->addr.v.a.mask, af);
4366                                         if ((af == AF_INET && bits < 32) ||
4367                                             (af == AF_INET6 && bits < 128))
4368                                                 snprintf(tmp, sizeof(tmp),
4369                                                     "%s/%d", a, bits);
4370                                         else
4371                                                 snprintf(tmp, sizeof(tmp),
4372                                                     "%s", a);
4373                                 }
4374                         }
4375                         break;
4376                 default:
4377                         snprintf(tmp, sizeof(tmp), "?");
4378                         break;
4379                 }
4380
4381                 if (h->not) {
4382                         snprintf(tmp_not, sizeof(tmp_not), "! %s", tmp);
4383                         expand_label_str(label, len, name, tmp_not);
4384                 } else
4385                         expand_label_str(label, len, name, tmp);
4386         }
4387 }
4388
4389 void
4390 expand_label_port(const char *name, char *label, size_t len,
4391     struct node_port *port)
4392 {
4393         char     a1[6], a2[6], op[13] = "";
4394
4395         if (strstr(label, name) != NULL) {
4396                 snprintf(a1, sizeof(a1), "%u", ntohs(port->port[0]));
4397                 snprintf(a2, sizeof(a2), "%u", ntohs(port->port[1]));
4398                 if (!port->op)
4399                         ;
4400                 else if (port->op == PF_OP_IRG)
4401                         snprintf(op, sizeof(op), "%s><%s", a1, a2);
4402                 else if (port->op == PF_OP_XRG)
4403                         snprintf(op, sizeof(op), "%s<>%s", a1, a2);
4404                 else if (port->op == PF_OP_EQ)
4405                         snprintf(op, sizeof(op), "%s", a1);
4406                 else if (port->op == PF_OP_NE)
4407                         snprintf(op, sizeof(op), "!=%s", a1);
4408                 else if (port->op == PF_OP_LT)
4409                         snprintf(op, sizeof(op), "<%s", a1);
4410                 else if (port->op == PF_OP_LE)
4411                         snprintf(op, sizeof(op), "<=%s", a1);
4412                 else if (port->op == PF_OP_GT)
4413                         snprintf(op, sizeof(op), ">%s", a1);
4414                 else if (port->op == PF_OP_GE)
4415                         snprintf(op, sizeof(op), ">=%s", a1);
4416                 expand_label_str(label, len, name, op);
4417         }
4418 }
4419
4420 void
4421 expand_label_proto(const char *name, char *label, size_t len, u_int8_t proto)
4422 {
4423         struct protoent *pe;
4424         char n[4];
4425
4426         if (strstr(label, name) != NULL) {
4427                 pe = getprotobynumber(proto);
4428                 if (pe != NULL)
4429                         expand_label_str(label, len, name, pe->p_name);
4430                 else {
4431                         snprintf(n, sizeof(n), "%u", proto);
4432                         expand_label_str(label, len, name, n);
4433                 }
4434         }
4435 }
4436
4437 void
4438 expand_label_nr(const char *name, char *label, size_t len)
4439 {
4440         char n[11];
4441
4442         if (strstr(label, name) != NULL) {
4443                 snprintf(n, sizeof(n), "%u", pf->anchor->match);
4444                 expand_label_str(label, len, name, n);
4445         }
4446 }
4447
4448 void
4449 expand_label(char *label, size_t len, const char *ifname, sa_family_t af,
4450     struct node_host *src_host, struct node_port *src_port,
4451     struct node_host *dst_host, struct node_port *dst_port,
4452     u_int8_t proto)
4453 {
4454         expand_label_if("$if", label, len, ifname);
4455         expand_label_addr("$srcaddr", label, len, af, src_host);
4456         expand_label_addr("$dstaddr", label, len, af, dst_host);
4457         expand_label_port("$srcport", label, len, src_port);
4458         expand_label_port("$dstport", label, len, dst_port);
4459         expand_label_proto("$proto", label, len, proto);
4460         expand_label_nr("$nr", label, len);
4461 }
4462
4463 int
4464 expand_altq(struct pf_altq *a, struct node_if *interfaces,
4465     struct node_queue *nqueues, struct node_queue_bw bwspec,
4466     struct node_queue_opt *opts)
4467 {
4468         struct pf_altq           pa, pb;
4469         char                     qname[PF_QNAME_SIZE];
4470         struct node_queue       *n;
4471         struct node_queue_bw     bw;
4472         int                      errs = 0;
4473
4474         if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
4475                 FREE_LIST(struct node_if, interfaces);
4476                 FREE_LIST(struct node_queue, nqueues);
4477                 return (0);
4478         }
4479
4480         LOOP_THROUGH(struct node_if, interface, interfaces,
4481                 memcpy(&pa, a, sizeof(struct pf_altq));
4482                 if (strlcpy(pa.ifname, interface->ifname,
4483                     sizeof(pa.ifname)) >= sizeof(pa.ifname))
4484                         errx(1, "expand_altq: strlcpy");
4485
4486                 if (interface->not) {
4487                         yyerror("altq on ! <interface> is not supported");
4488                         errs++;
4489                 } else {
4490                         if (eval_pfaltq(pf, &pa, &bwspec, opts))
4491                                 errs++;
4492                         else
4493                                 if (pfctl_add_altq(pf, &pa))
4494                                         errs++;
4495
4496                         if (pf->opts & PF_OPT_VERBOSE) {
4497                                 print_altq(&pf->paltq->altq, 0,
4498                                     &bwspec, opts);
4499                                 if (nqueues && nqueues->tail) {
4500                                         printf("queue { ");
4501                                         LOOP_THROUGH(struct node_queue, queue,
4502                                             nqueues,
4503                                                 printf("%s ",
4504                                                     queue->queue);
4505                                         );
4506                                         printf("}");
4507                                 }
4508                                 printf("\n");
4509                         }
4510
4511                         if (pa.scheduler == ALTQT_CBQ ||
4512                             pa.scheduler == ALTQT_HFSC) {
4513                                 /* now create a root queue */
4514                                 memset(&pb, 0, sizeof(struct pf_altq));
4515                                 if (strlcpy(qname, "root_", sizeof(qname)) >=
4516                                     sizeof(qname))
4517                                         errx(1, "expand_altq: strlcpy");
4518                                 if (strlcat(qname, interface->ifname,
4519                                     sizeof(qname)) >= sizeof(qname))
4520                                         errx(1, "expand_altq: strlcat");
4521                                 if (strlcpy(pb.qname, qname,
4522                                     sizeof(pb.qname)) >= sizeof(pb.qname))
4523                                         errx(1, "expand_altq: strlcpy");
4524                                 if (strlcpy(pb.ifname, interface->ifname,
4525                                     sizeof(pb.ifname)) >= sizeof(pb.ifname))
4526                                         errx(1, "expand_altq: strlcpy");
4527                                 pb.qlimit = pa.qlimit;
4528                                 pb.scheduler = pa.scheduler;
4529                                 bw.bw_absolute = pa.ifbandwidth;
4530                                 bw.bw_percent = 0;
4531                                 if (eval_pfqueue(pf, &pb, &bw, opts))
4532                                         errs++;
4533                                 else
4534                                         if (pfctl_add_altq(pf, &pb))
4535                                                 errs++;
4536                         }
4537
4538                         LOOP_THROUGH(struct node_queue, queue, nqueues,
4539                                 n = calloc(1, sizeof(struct node_queue));
4540                                 if (n == NULL)
4541                                         err(1, "expand_altq: calloc");
4542                                 if (pa.scheduler == ALTQT_CBQ ||
4543                                     pa.scheduler == ALTQT_HFSC /*||
4544                                     pa.scheduler == ALTQT_FAIRQ*/)
4545                                         if (strlcpy(n->parent, qname,
4546                                             sizeof(n->parent)) >=
4547                                             sizeof(n->parent))
4548                                                 errx(1, "expand_altq: strlcpy");
4549                                 if (strlcpy(n->queue, queue->queue,
4550                                     sizeof(n->queue)) >= sizeof(n->queue))
4551                                         errx(1, "expand_altq: strlcpy");
4552                                 if (strlcpy(n->ifname, interface->ifname,
4553                                     sizeof(n->ifname)) >= sizeof(n->ifname))
4554                                         errx(1, "expand_altq: strlcpy");
4555                                 n->scheduler = pa.scheduler;
4556                                 n->next = NULL;
4557                                 n->tail = n;
4558                                 if (queues == NULL)
4559                                         queues = n;
4560                                 else {
4561                                         queues->tail->next = n;
4562                                         queues->tail = n;
4563                                 }
4564                         );
4565                 }
4566         );
4567         FREE_LIST(struct node_if, interfaces);
4568         FREE_LIST(struct node_queue, nqueues);
4569
4570         return (errs);
4571 }
4572
4573 int
4574 expand_queue(struct pf_altq *a, struct node_if *interfaces,
4575     struct node_queue *nqueues, struct node_queue_bw bwspec,
4576     struct node_queue_opt *opts)
4577 {
4578         struct node_queue       *n, *nq;
4579         struct pf_altq           pa;
4580         u_int8_t                 found = 0;
4581         u_int8_t                 errs = 0;
4582
4583         if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
4584                 FREE_LIST(struct node_queue, nqueues);
4585                 return (0);
4586         }
4587
4588         if (queues == NULL) {
4589                 yyerror("queue %s has no parent", a->qname);
4590                 FREE_LIST(struct node_queue, nqueues);
4591                 return (1);
4592         }
4593
4594         LOOP_THROUGH(struct node_if, interface, interfaces,
4595                 LOOP_THROUGH(struct node_queue, tqueue, queues,
4596                         if (!strncmp(a->qname, tqueue->queue, PF_QNAME_SIZE) &&
4597                             (interface->ifname[0] == 0 ||
4598                             (!interface->not && !strncmp(interface->ifname,
4599                             tqueue->ifname, IFNAMSIZ)) ||
4600                             (interface->not && strncmp(interface->ifname,
4601                             tqueue->ifname, IFNAMSIZ)))) {
4602                                 /* found ourself in queues */
4603                                 found++;
4604
4605                                 memcpy(&pa, a, sizeof(struct pf_altq));
4606
4607                                 if (pa.scheduler != ALTQT_NONE &&
4608                                     pa.scheduler != tqueue->scheduler) {
4609                                         yyerror("exactly one scheduler type "
4610                                             "per interface allowed");
4611                                         return (1);
4612                                 }
4613                                 pa.scheduler = tqueue->scheduler;
4614
4615                                 /* scheduler dependent error checking */
4616                                 switch (pa.scheduler) {
4617                                 case ALTQT_PRIQ:
4618                                         if (nqueues != NULL) {
4619                                                 yyerror("priq queues cannot "
4620                                                     "have child queues");
4621                                                 return (1);
4622                                         }
4623                                         if (bwspec.bw_absolute > 0 ||
4624                                             bwspec.bw_percent < 100) {
4625                                                 yyerror("priq doesn't take "
4626                                                     "bandwidth");
4627                                                 return (1);
4628                                         }
4629                                         break;
4630                                 default:
4631                                         break;
4632                                 }
4633
4634                                 if (strlcpy(pa.ifname, tqueue->ifname,
4635                                     sizeof(pa.ifname)) >= sizeof(pa.ifname))
4636                                         errx(1, "expand_queue: strlcpy");
4637                                 if (strlcpy(pa.parent, tqueue->parent,
4638                                     sizeof(pa.parent)) >= sizeof(pa.parent))
4639                                         errx(1, "expand_queue: strlcpy");
4640
4641                                 if (eval_pfqueue(pf, &pa, &bwspec, opts))
4642                                         errs++;
4643                                 else
4644                                         if (pfctl_add_altq(pf, &pa))
4645                                                 errs++;
4646
4647                                 for (nq = nqueues; nq != NULL; nq = nq->next) {
4648                                         if (!strcmp(a->qname, nq->queue)) {
4649                                                 yyerror("queue cannot have "
4650                                                     "itself as child");
4651                                                 errs++;
4652                                                 continue;
4653                                         }
4654                                         n = calloc(1,
4655                                             sizeof(struct node_queue));
4656                                         if (n == NULL)
4657                                                 err(1, "expand_queue: calloc");
4658                                         if (strlcpy(n->parent, a->qname,
4659                                             sizeof(n->parent)) >=
4660                                             sizeof(n->parent))
4661                                                 errx(1, "expand_queue strlcpy");
4662                                         if (strlcpy(n->queue, nq->queue,
4663                                             sizeof(n->queue)) >=
4664                                             sizeof(n->queue))
4665                                                 errx(1, "expand_queue strlcpy");
4666                                         if (strlcpy(n->ifname, tqueue->ifname,
4667                                             sizeof(n->ifname)) >=
4668                                             sizeof(n->ifname))
4669                                                 errx(1, "expand_queue strlcpy");
4670                                         n->scheduler = tqueue->scheduler;
4671                                         n->next = NULL;
4672                                         n->tail = n;
4673                                         if (queues == NULL)
4674                                                 queues = n;
4675                                         else {
4676                                                 queues->tail->next = n;
4677                                                 queues->tail = n;
4678                                         }
4679                                 }
4680                                 if ((pf->opts & PF_OPT_VERBOSE) && (
4681                                     (found == 1 && interface->ifname[0] == 0) ||
4682                                     (found > 0 && interface->ifname[0] != 0))) {
4683                                         print_queue(&pf->paltq->altq, 0,
4684                                             &bwspec, interface->ifname[0] != 0,
4685                                             opts);
4686                                         if (nqueues && nqueues->tail) {
4687                                                 printf("{ ");
4688                                                 LOOP_THROUGH(struct node_queue,
4689                                                     queue, nqueues,
4690                                                         printf("%s ",
4691                                                             queue->queue);
4692                                                 );
4693                                                 printf("}");
4694                                         }
4695                                         printf("\n");
4696                                 }
4697                         }
4698                 );
4699         );
4700
4701         FREE_LIST(struct node_queue, nqueues);
4702         FREE_LIST(struct node_if, interfaces);
4703
4704         if (!found) {
4705                 yyerror("queue %s has no parent", a->qname);
4706                 errs++;
4707         }
4708
4709         if (errs)
4710                 return (1);
4711         else
4712                 return (0);
4713 }
4714
4715 void
4716 expand_rule(struct pf_rule *r,
4717     struct node_if *interfaces, struct node_host *rpool_hosts,
4718     struct node_proto *protos, struct node_os *src_oses,
4719     struct node_host *src_hosts, struct node_port *src_ports,
4720     struct node_host *dst_hosts, struct node_port *dst_ports,
4721     struct node_uid *uids, struct node_gid *gids, struct node_icmp *icmp_types,
4722     const char *anchor_call)
4723 {
4724         sa_family_t              af = r->af;
4725         int                      added = 0, error = 0;
4726         char                     ifname[IF_NAMESIZE];
4727         char                     label[PF_RULE_LABEL_SIZE];
4728         char                     tagname[PF_TAG_NAME_SIZE];
4729         char                     match_tagname[PF_TAG_NAME_SIZE];
4730         struct pf_pooladdr      *pa;
4731         struct node_host        *h;
4732         u_int8_t                 flags, flagset, keep_state;
4733
4734         if (strlcpy(label, r->label, sizeof(label)) >= sizeof(label))
4735                 errx(1, "expand_rule: strlcpy");
4736         if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname))
4737                 errx(1, "expand_rule: strlcpy");
4738         if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >=
4739             sizeof(match_tagname))
4740                 errx(1, "expand_rule: strlcpy");
4741         flags = r->flags;
4742         flagset = r->flagset;
4743         keep_state = r->keep_state;
4744
4745         LOOP_THROUGH(struct node_if, interface, interfaces,
4746         LOOP_THROUGH(struct node_proto, proto, protos,
4747         LOOP_THROUGH(struct node_icmp, icmp_type, icmp_types,
4748         LOOP_THROUGH(struct node_host, src_host, src_hosts,
4749         LOOP_THROUGH(struct node_port, src_port, src_ports,
4750         LOOP_THROUGH(struct node_os, src_os, src_oses,
4751         LOOP_THROUGH(struct node_host, dst_host, dst_hosts,
4752         LOOP_THROUGH(struct node_port, dst_port, dst_ports,
4753         LOOP_THROUGH(struct node_uid, uid, uids,
4754         LOOP_THROUGH(struct node_gid, gid, gids,
4755
4756                 r->af = af;
4757                 /* for link-local IPv6 address, interface must match up */
4758                 if ((r->af && src_host->af && r->af != src_host->af) ||
4759                     (r->af && dst_host->af && r->af != dst_host->af) ||
4760                     (src_host->af && dst_host->af &&
4761                     src_host->af != dst_host->af) ||
4762                     (src_host->ifindex && dst_host->ifindex &&
4763                     src_host->ifindex != dst_host->ifindex) ||
4764                     (src_host->ifindex && *interface->ifname &&
4765                     src_host->ifindex != if_nametoindex(interface->ifname)) ||
4766                     (dst_host->ifindex && *interface->ifname &&
4767                     dst_host->ifindex != if_nametoindex(interface->ifname)))
4768                         continue;
4769                 if (!r->af && src_host->af)
4770                         r->af = src_host->af;
4771                 else if (!r->af && dst_host->af)
4772                         r->af = dst_host->af;
4773
4774                 if (*interface->ifname)
4775                         strlcpy(r->ifname, interface->ifname,
4776                             sizeof(r->ifname));
4777                 else if (if_indextoname(src_host->ifindex, ifname))
4778                         strlcpy(r->ifname, ifname, sizeof(r->ifname));
4779                 else if (if_indextoname(dst_host->ifindex, ifname))
4780                         strlcpy(r->ifname, ifname, sizeof(r->ifname));
4781                 else
4782                         memset(r->ifname, '\0', sizeof(r->ifname));
4783
4784                 if (strlcpy(r->label, label, sizeof(r->label)) >=
4785                     sizeof(r->label))
4786                         errx(1, "expand_rule: strlcpy");
4787                 if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >=
4788                     sizeof(r->tagname))
4789                         errx(1, "expand_rule: strlcpy");
4790                 if (strlcpy(r->match_tagname, match_tagname,
4791                     sizeof(r->match_tagname)) >= sizeof(r->match_tagname))
4792                         errx(1, "expand_rule: strlcpy");
4793                 expand_label(r->label, PF_RULE_LABEL_SIZE, r->ifname, r->af,
4794                     src_host, src_port, dst_host, dst_port, proto->proto);
4795                 expand_label(r->tagname, PF_TAG_NAME_SIZE, r->ifname, r->af,
4796                     src_host, src_port, dst_host, dst_port, proto->proto);
4797                 expand_label(r->match_tagname, PF_TAG_NAME_SIZE, r->ifname,
4798                     r->af, src_host, src_port, dst_host, dst_port,
4799                     proto->proto);
4800
4801                 error += check_netmask(src_host, r->af);
4802                 error += check_netmask(dst_host, r->af);
4803
4804                 r->ifnot = interface->not;
4805                 r->proto = proto->proto;
4806                 r->src.addr = src_host->addr;
4807                 r->src.neg = src_host->not;
4808                 r->src.port[0] = src_port->port[0];
4809                 r->src.port[1] = src_port->port[1];
4810                 r->src.port_op = src_port->op;
4811                 r->dst.addr = dst_host->addr;
4812                 r->dst.neg = dst_host->not;
4813                 r->dst.port[0] = dst_port->port[0];
4814                 r->dst.port[1] = dst_port->port[1];
4815                 r->dst.port_op = dst_port->op;
4816                 r->uid.op = uid->op;
4817                 r->uid.uid[0] = uid->uid[0];
4818                 r->uid.uid[1] = uid->uid[1];
4819                 r->gid.op = gid->op;
4820                 r->gid.gid[0] = gid->gid[0];
4821                 r->gid.gid[1] = gid->gid[1];
4822                 r->type = icmp_type->type;
4823                 r->code = icmp_type->code;
4824
4825                 if ((keep_state == PF_STATE_MODULATE ||
4826                     keep_state == PF_STATE_SYNPROXY) &&
4827                     r->proto && r->proto != IPPROTO_TCP)
4828                         r->keep_state = PF_STATE_NORMAL;
4829                 else
4830                         r->keep_state = keep_state;
4831
4832                 if (r->proto && r->proto != IPPROTO_TCP) {
4833                         r->flags = 0;
4834                         r->flagset = 0;
4835                 } else {
4836                         r->flags = flags;
4837                         r->flagset = flagset;
4838                 }
4839                 if (icmp_type->proto && r->proto != icmp_type->proto) {
4840                         yyerror("icmp-type mismatch");
4841                         error++;
4842                 }
4843
4844                 if (src_os && src_os->os) {
4845                         r->os_fingerprint = pfctl_get_fingerprint(src_os->os);
4846                         if ((pf->opts & PF_OPT_VERBOSE2) &&
4847                             r->os_fingerprint == PF_OSFP_NOMATCH)
4848                                 fprintf(stderr,
4849                                     "warning: unknown '%s' OS fingerprint\n",
4850                                     src_os->os);
4851                 } else {
4852                         r->os_fingerprint = PF_OSFP_ANY;
4853                 }
4854
4855                 TAILQ_INIT(&r->rpool.list);
4856                 for (h = rpool_hosts; h != NULL; h = h->next) {
4857                         pa = calloc(1, sizeof(struct pf_pooladdr));
4858                         if (pa == NULL)
4859                                 err(1, "expand_rule: calloc");
4860                         pa->addr = h->addr;
4861                         if (h->ifname != NULL) {
4862                                 if (strlcpy(pa->ifname, h->ifname,
4863                                     sizeof(pa->ifname)) >=
4864                                     sizeof(pa->ifname))
4865                                         errx(1, "expand_rule: strlcpy");
4866                         } else
4867                                 pa->ifname[0] = 0;
4868                         TAILQ_INSERT_TAIL(&r->rpool.list, pa, entries);
4869                 }
4870
4871                 if (rule_consistent(r, anchor_call[0]) < 0 || error)
4872                         yyerror("skipping rule due to errors");
4873                 else {
4874                         r->nr = pf->astack[pf->asd]->match++;
4875                         pfctl_add_rule(pf, r, anchor_call);
4876                         added++;
4877                 }
4878
4879         ))))))))));
4880
4881         FREE_LIST(struct node_if, interfaces);
4882         FREE_LIST(struct node_proto, protos);
4883         FREE_LIST(struct node_host, src_hosts);
4884         FREE_LIST(struct node_port, src_ports);
4885         FREE_LIST(struct node_os, src_oses);
4886         FREE_LIST(struct node_host, dst_hosts);
4887         FREE_LIST(struct node_port, dst_ports);
4888         FREE_LIST(struct node_uid, uids);
4889         FREE_LIST(struct node_gid, gids);
4890         FREE_LIST(struct node_icmp, icmp_types);
4891         FREE_LIST(struct node_host, rpool_hosts);
4892
4893         if (!added)
4894                 yyerror("rule expands to no valid combination");
4895 }
4896
4897 int
4898 expand_skip_interface(struct node_if *interfaces)
4899 {
4900         int     errs = 0;
4901
4902         if (!interfaces || (!interfaces->next && !interfaces->not &&
4903             !strcmp(interfaces->ifname, "none"))) {
4904                 if (pf->opts & PF_OPT_VERBOSE)
4905                         printf("set skip on none\n");
4906                 errs = pfctl_set_interface_flags(pf,  __DECONST(char *, ""), PFI_IFLAG_SKIP, 0);
4907                 return (errs);
4908         }
4909
4910         if (pf->opts & PF_OPT_VERBOSE)
4911                 printf("set skip on {");
4912         LOOP_THROUGH(struct node_if, interface, interfaces,
4913                 if (pf->opts & PF_OPT_VERBOSE)
4914                         printf(" %s", interface->ifname);
4915                 if (interface->not) {
4916                         yyerror("skip on ! <interface> is not supported");
4917                         errs++;
4918                 } else
4919                         errs += pfctl_set_interface_flags(pf,
4920                             interface->ifname, PFI_IFLAG_SKIP, 1);
4921         );
4922         if (pf->opts & PF_OPT_VERBOSE)
4923                 printf(" }\n");
4924
4925         FREE_LIST(struct node_if, interfaces);
4926
4927         if (errs)
4928                 return (1);
4929         else
4930                 return (0);
4931 }
4932
4933 #undef FREE_LIST
4934 #undef LOOP_THROUGH
4935
4936 int
4937 check_rulestate(int desired_state)
4938 {
4939         if (require_order && (rulestate > desired_state)) {
4940                 yyerror("Rules must be in order: options, normalization, "
4941                     "queueing, translation, filtering");
4942                 return (1);
4943         }
4944         rulestate = desired_state;
4945         return (0);
4946 }
4947
4948 int
4949 kw_cmp(const void *k, const void *e)
4950 {
4951         return (strcmp(k, ((const struct keywords *)e)->k_name));
4952 }
4953
4954 int
4955 lookup(char *s)
4956 {
4957         /* this has to be sorted always */
4958         static const struct keywords keywords[] = {
4959                 { "all",                ALL},
4960                 { "allow-opts",         ALLOWOPTS},
4961                 { "altq",               ALTQ},
4962                 { "anchor",             ANCHOR},
4963                 { "antispoof",          ANTISPOOF},
4964                 { "any",                ANY},
4965                 { "bandwidth",          BANDWIDTH},
4966                 { "binat",              BINAT},
4967                 { "binat-anchor",       BINATANCHOR},
4968                 { "bitmask",            BITMASK},
4969                 { "block",              BLOCK},
4970                 { "block-policy",       BLOCKPOLICY},
4971                 { "buckets",            BUCKETS},
4972                 { "cbq",                CBQ},
4973                 { "code",               CODE},
4974                 { "crop",               FRAGCROP},
4975                 { "debug",              DEBUG},
4976                 { "drop",               DROP},
4977                 { "drop-ovl",           FRAGDROP},
4978                 { "dup-to",             DUPTO},
4979                 { "fairq",              FAIRQ},
4980                 { "fastroute",          FASTROUTE},
4981                 { "file",               FILENAME},
4982                 { "fingerprints",       FINGERPRINTS},
4983                 { "flags",              FLAGS},
4984                 { "floating",           FLOATING},
4985                 { "flush",              FLUSH},
4986                 { "for",                FOR},
4987                 { "fragment",           FRAGMENT},
4988                 { "from",               FROM},
4989                 { "global",             GLOBAL},
4990                 { "group",              GROUP},
4991                 { "hash-only",          HASHONLY},
4992                 { "hfsc",               HFSC},
4993                 { "hogs",               HOGS},
4994                 { "hostid",             HOSTID},
4995                 { "icmp-type",          ICMPTYPE},
4996                 { "icmp6-type",         ICMP6TYPE},
4997                 { "if-bound",           IFBOUND},
4998                 { "in",                 IN},
4999                 { "inet",               INET},
5000                 { "inet6",              INET6},
5001                 { "keep",               KEEP},
5002                 { "keep-policy",        KEEPPOLICY},
5003                 { "label",              LABEL},
5004                 { "limit",              LIMIT},
5005                 { "linkshare",          LINKSHARE},
5006                 { "load",               LOAD},
5007                 { "log",                LOG},
5008                 { "loginterface",       LOGINTERFACE},
5009                 { "max",                MAXIMUM},
5010                 { "max-mss",            MAXMSS},
5011                 { "max-src-conn",       MAXSRCCONN},
5012                 { "max-src-conn-rate",  MAXSRCCONNRATE},
5013                 { "max-src-nodes",      MAXSRCNODES},
5014                 { "max-src-states",     MAXSRCSTATES},
5015                 { "min-ttl",            MINTTL},
5016                 { "modulate",           MODULATE},
5017                 { "nat",                NAT},
5018                 { "nat-anchor",         NATANCHOR},
5019                 { "no",                 NO},
5020                 { "no-df",              NODF},
5021                 { "no-pickups",         NOPICKUPS},
5022                 { "no-route",           NOROUTE},
5023                 { "no-sync",            NOSYNC},
5024                 { "on",                 ON},
5025                 { "optimization",       OPTIMIZATION},
5026                 { "os",                 OS},
5027                 { "out",                OUT},
5028                 { "overload",           OVERLOAD},
5029                 { "pass",               PASS},
5030                 { "pickups",            PICKUPS},
5031                 { "port",               PORT},
5032                 { "priority",           PRIORITY},
5033                 { "priq",               PRIQ},
5034                 { "probability",        PROBABILITY},
5035                 { "proto",              PROTO},
5036                 { "qlimit",             QLIMIT},
5037                 { "queue",              QUEUE},
5038                 { "quick",              QUICK},
5039                 { "random",             RANDOM},
5040                 { "random-id",          RANDOMID},
5041                 { "rdr",                RDR},
5042                 { "rdr-anchor",         RDRANCHOR},
5043                 { "realtime",           REALTIME},
5044                 { "reassemble",         REASSEMBLE},
5045                 { "reply-to",           REPLYTO},
5046                 { "require-order",      REQUIREORDER},
5047                 { "return",             RETURN},
5048                 { "return-icmp",        RETURNICMP},
5049                 { "return-icmp6",       RETURNICMP6},
5050                 { "return-rst",         RETURNRST},
5051                 { "round-robin",        ROUNDROBIN},
5052                 { "route",              ROUTE},
5053                 { "route-to",           ROUTETO},
5054                 { "rtable",             RTABLE},
5055                 { "rule",               RULE},
5056                 { "ruleset-optimization",       RULESET_OPTIMIZATION},
5057                 { "scrub",              SCRUB},
5058                 { "set",                SET},
5059                 { "skip",               SKIP},
5060                 { "source-hash",        SOURCEHASH},
5061                 { "source-track",       SOURCETRACK},
5062                 { "state",              STATE},
5063                 { "state-policy",       STATEPOLICY},
5064                 { "static-port",        STATICPORT},
5065                 { "sticky-address",     STICKYADDRESS},
5066                 { "synproxy",           SYNPROXY},
5067                 { "table",              TABLE},
5068                 { "tag",                TAG},
5069                 { "tagged",             TAGGED},
5070                 { "tbrsize",            TBRSIZE},
5071                 { "timeout",            TIMEOUT},
5072                 { "to",                 TO},
5073                 { "tos",                TOS},
5074                 { "ttl",                TTL},
5075                 { "upperlimit",         UPPERLIMIT},
5076                 { "urpf-failed",        URPFFAILED},
5077                 { "user",               USER},
5078         };
5079         const struct keywords   *p;
5080
5081         p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
5082             sizeof(keywords[0]), kw_cmp);
5083
5084         if (p) {
5085                 if (debug > 1)
5086                         fprintf(stderr, "%s: %d\n", s, p->k_val);
5087                 return (p->k_val);
5088         } else {
5089                 if (debug > 1)
5090                         fprintf(stderr, "string: %s\n", s);
5091                 return (STRING);
5092         }
5093 }
5094
5095 #define MAXPUSHBACK     128
5096
5097 char    *parsebuf;
5098 int      parseindex;
5099 char     pushback_buffer[MAXPUSHBACK];
5100 int      pushback_index = 0;
5101
5102 int
5103 lgetc(FILE *f)
5104 {
5105         int     c, next;
5106
5107         if (parsebuf) {
5108                 /* Read character from the parsebuffer instead of input. */
5109                 if (parseindex >= 0) {
5110                         c = parsebuf[parseindex++];
5111                         if (c != '\0')
5112                                 return (c);
5113                         parsebuf = NULL;
5114                 } else
5115                         parseindex++;
5116         }
5117
5118         if (pushback_index)
5119                 return (pushback_buffer[--pushback_index]);
5120
5121         while ((c = getc(f)) == '\\') {
5122                 next = getc(f);
5123                 if (next != '\n') {
5124                         c = next;
5125                         break;
5126                 }
5127                 yylval.lineno = lineno;
5128                 lineno++;
5129         }
5130         if (c == '\t' || c == ' ') {
5131                 /* Compress blanks to a single space. */
5132                 do {
5133                         c = getc(f);
5134                 } while (c == '\t' || c == ' ');
5135                 ungetc(c, f);
5136                 c = ' ';
5137         }
5138
5139         return (c);
5140 }
5141
5142 int
5143 lungetc(int c)
5144 {
5145         if (c == EOF)
5146                 return (EOF);
5147         if (parsebuf) {
5148                 parseindex--;
5149                 if (parseindex >= 0)
5150                         return (c);
5151         }
5152         if (pushback_index < MAXPUSHBACK-1)
5153                 return (pushback_buffer[pushback_index++] = c);
5154         else
5155                 return (EOF);
5156 }
5157
5158 int
5159 findeol(void)
5160 {
5161         int     c;
5162
5163         parsebuf = NULL;
5164         pushback_index = 0;
5165
5166         /* skip to either EOF or the first real EOL */
5167         while (1) {
5168                 c = lgetc(fin);
5169                 if (c == '\n') {
5170                         lineno++;
5171                         break;
5172                 }
5173                 if (c == EOF)
5174                         break;
5175         }
5176         return (ERROR);
5177 }
5178
5179 int
5180 yylex(void)
5181 {
5182         char     buf[8096];
5183         char    *p, *val;
5184         int      endc, c, next;
5185         int      token;
5186
5187 top:
5188         p = buf;
5189         while ((c = lgetc(fin)) == ' ')
5190                 ; /* nothing */
5191
5192         yylval.lineno = lineno;
5193         if (c == '#')
5194                 while ((c = lgetc(fin)) != '\n' && c != EOF)
5195                         ; /* nothing */
5196         if (c == '$' && parsebuf == NULL) {
5197                 while (1) {
5198                         if ((c = lgetc(fin)) == EOF)
5199                                 return (0);
5200
5201                         if (p + 1 >= buf + sizeof(buf) - 1) {
5202                                 yyerror("string too long");
5203                                 return (findeol());
5204                         }
5205                         if (isalnum(c) || c == '_') {
5206                                 *p++ = (char)c;
5207                                 continue;
5208                         }
5209                         *p = '\0';
5210                         lungetc(c);
5211                         break;
5212                 }
5213                 val = symget(buf);
5214                 if (val == NULL) {
5215                         yyerror("macro '%s' not defined", buf);
5216                         return (findeol());
5217                 }
5218                 parsebuf = val;
5219                 parseindex = 0;
5220                 goto top;
5221         }
5222
5223         switch (c) {
5224         case '\'':
5225         case '"':
5226                 endc = c;
5227                 while (1) {
5228                         if ((c = lgetc(fin)) == EOF)
5229                                 return (0);
5230                         if (c == endc) {
5231                                 *p = '\0';
5232                                 break;
5233                         }
5234                         if (c == '\n') {
5235                                 lineno++;
5236                                 continue;
5237                         }
5238                         if (p + 1 >= buf + sizeof(buf) - 1) {
5239                                 yyerror("string too long");
5240                                 return (findeol());
5241                         }
5242                         *p++ = (char)c;
5243                 }
5244                 yylval.v.string = strdup(buf);
5245                 if (yylval.v.string == NULL)
5246                         err(1, "yylex: strdup");
5247                 return (STRING);
5248         case '<':
5249                 next = lgetc(fin);
5250                 if (next == '>') {
5251                         yylval.v.i = PF_OP_XRG;
5252                         return (PORTBINARY);
5253                 }
5254                 lungetc(next);
5255                 break;
5256         case '>':
5257                 next = lgetc(fin);
5258                 if (next == '<') {
5259                         yylval.v.i = PF_OP_IRG;
5260                         return (PORTBINARY);
5261                 }
5262                 lungetc(next);
5263                 break;
5264         case '-':
5265                 next = lgetc(fin);
5266                 if (next == '>')
5267                         return (ARROW);
5268                 lungetc(next);
5269                 break;
5270         }
5271
5272 #define allowed_in_string(x) \
5273         (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
5274         x != '{' && x != '}' && x != '<' && x != '>' && \
5275         x != '!' && x != '=' && x != '/' && x != '#' && \
5276         x != ','))
5277
5278         if (isalnum(c) || c == ':' || c == '_') {
5279                 do {
5280                         *p++ = c;
5281                         if ((unsigned)(p-buf) >= sizeof(buf)) {
5282                                 yyerror("string too long");
5283                                 return (findeol());
5284                         }
5285                 } while ((c = lgetc(fin)) != EOF && (allowed_in_string(c)));
5286                 lungetc(c);
5287                 *p = '\0';
5288                 if ((token = lookup(buf)) == STRING)
5289                         if ((yylval.v.string = strdup(buf)) == NULL)
5290                                 err(1, "yylex: strdup");
5291                 return (token);
5292         }
5293         if (c == '\n') {
5294                 yylval.lineno = lineno;
5295                 lineno++;
5296         }
5297         if (c == EOF)
5298                 return (0);
5299         return (c);
5300 }
5301
5302 int
5303 parse_rules(FILE *input, struct pfctl *xpf)
5304 {
5305         struct sym      *sym, *next;
5306
5307         fin = input;
5308         pf = xpf;
5309         lineno = 1;
5310         errors = 0;
5311         rulestate = PFCTL_STATE_NONE;
5312         returnicmpdefault = (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
5313         returnicmp6default =
5314             (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
5315         blockpolicy = PFRULE_DROP;
5316         require_order = 1;
5317
5318         yyparse();
5319
5320         /* Free macros and check which have not been used. */
5321         for (sym = TAILQ_FIRST(&symhead); sym != NULL; sym = next) {
5322                 next = TAILQ_NEXT(sym, entries);
5323                 if ((pf->opts & PF_OPT_VERBOSE2) && !sym->used)
5324                         fprintf(stderr, "warning: macro '%s' not "
5325                             "used\n", sym->nam);
5326                 free(sym->nam);
5327                 free(sym->val);
5328                 TAILQ_REMOVE(&symhead, sym, entries);
5329                 free(sym);
5330         }
5331
5332         return (errors ? -1 : 0);
5333 }
5334
5335 /*
5336  * Over-designed efficiency is a French and German concept, so how about
5337  * we wait until they discover this ugliness and make it all fancy.
5338  */
5339 int
5340 symset(const char *nam, const char *val, int persist)
5341 {
5342         struct sym      *sym;
5343
5344         for (sym = TAILQ_FIRST(&symhead); sym && strcmp(nam, sym->nam);
5345             sym = TAILQ_NEXT(sym, entries))
5346                 ;       /* nothing */
5347
5348         if (sym != NULL) {
5349                 if (sym->persist == 1)
5350                         return (0);
5351                 else {
5352                         free(sym->nam);
5353                         free(sym->val);
5354                         TAILQ_REMOVE(&symhead, sym, entries);
5355                         free(sym);
5356                 }
5357         }
5358         if ((sym = calloc(1, sizeof(*sym))) == NULL)
5359                 return (-1);
5360
5361         sym->nam = strdup(nam);
5362         if (sym->nam == NULL) {
5363                 free(sym);
5364                 return (-1);
5365         }
5366         sym->val = strdup(val);
5367         if (sym->val == NULL) {
5368                 free(sym->nam);
5369                 free(sym);
5370                 return (-1);
5371         }
5372         sym->used = 0;
5373         sym->persist = persist;
5374         TAILQ_INSERT_TAIL(&symhead, sym, entries);
5375         return (0);
5376 }
5377
5378 int
5379 pfctl_cmdline_symset(char *s)
5380 {
5381         char    *sym, *val;
5382         int      ret;
5383
5384         if ((val = strrchr(s, '=')) == NULL)
5385                 return (-1);
5386
5387         if ((sym = malloc(strlen(s) - strlen(val) + 1)) == NULL)
5388                 err(1, "pfctl_cmdline_symset: malloc");
5389
5390         strlcpy(sym, s, strlen(s) - strlen(val) + 1);
5391
5392         ret = symset(sym, val + 1, 1);
5393         free(sym);
5394
5395         return (ret);
5396 }
5397
5398 char *
5399 symget(const char *nam)
5400 {
5401         struct sym      *sym;
5402
5403         TAILQ_FOREACH(sym, &symhead, entries)
5404                 if (strcmp(nam, sym->nam) == 0) {
5405                         sym->used = 1;
5406                         return (sym->val);
5407                 }
5408         return (NULL);
5409 }
5410
5411 void
5412 mv_rules(struct pf_ruleset *src, struct pf_ruleset *dst)
5413 {
5414         int i;
5415         struct pf_rule *r;
5416
5417         for (i = 0; i < PF_RULESET_MAX; ++i) {
5418                 while ((r = TAILQ_FIRST(src->rules[i].active.ptr))
5419                     != NULL) {
5420                         TAILQ_REMOVE(src->rules[i].active.ptr, r, entries);
5421                         TAILQ_INSERT_TAIL(dst->rules[i].active.ptr, r, entries);
5422                         dst->anchor->match++;
5423                 }
5424                 src->anchor->match = 0;
5425                 while ((r = TAILQ_FIRST(src->rules[i].inactive.ptr))
5426                     != NULL) {
5427                         TAILQ_REMOVE(src->rules[i].inactive.ptr, r, entries);
5428                         TAILQ_INSERT_TAIL(dst->rules[i].inactive.ptr,
5429                                 r, entries);
5430                 }
5431         }
5432 }
5433
5434 void
5435 decide_address_family(struct node_host *n, sa_family_t *af)
5436 {
5437         if (*af != 0 || n == NULL)
5438                 return;
5439         *af = n->af;
5440         while ((n = n->next) != NULL) {
5441                 if (n->af != *af) {
5442                         *af = 0;
5443                         return;
5444                 }
5445         }
5446 }
5447
5448 void
5449 remove_invalid_hosts(struct node_host **nh, sa_family_t *af)
5450 {
5451         struct node_host        *n = *nh, *prev = NULL;
5452
5453         while (n != NULL) {
5454                 if (*af && n->af && n->af != *af) {
5455                         /* unlink and free n */
5456                         struct node_host *next = n->next;
5457
5458                         /* adjust tail pointer */
5459                         if (n == (*nh)->tail)
5460                                 (*nh)->tail = prev;
5461                         /* adjust previous node's next pointer */
5462                         if (prev == NULL)
5463                                 *nh = next;
5464                         else
5465                                 prev->next = next;
5466                         /* free node */
5467                         if (n->ifname != NULL)
5468                                 free(n->ifname);
5469                         free(n);
5470                         n = next;
5471                 } else {
5472                         if (n->af && !*af)
5473                                 *af = n->af;
5474                         prev = n;
5475                         n = n->next;
5476                 }
5477         }
5478 }
5479
5480 int
5481 invalid_redirect(struct node_host *nh, sa_family_t af)
5482 {
5483         if (!af) {
5484                 struct node_host *n;
5485
5486                 /* tables and dyniftl are ok without an address family */
5487                 for (n = nh; n != NULL; n = n->next) {
5488                         if (n->addr.type != PF_ADDR_TABLE &&
5489                             n->addr.type != PF_ADDR_DYNIFTL) {
5490                                 yyerror("address family not given and "
5491                                     "translation address expands to multiple "
5492                                     "address families");
5493                                 return (1);
5494                         }
5495                 }
5496         }
5497         if (nh == NULL) {
5498                 yyerror("no translation address with matching address family "
5499                     "found.");
5500                 return (1);
5501         }
5502         return (0);
5503 }
5504
5505 int
5506 atoul(char *s, u_long *ulvalp)
5507 {
5508         u_long   ulval;
5509         char    *ep;
5510
5511         errno = 0;
5512         ulval = strtoul(s, &ep, 0);
5513         if (s[0] == '\0' || *ep != '\0')
5514                 return (-1);
5515         if (errno == ERANGE && ulval == ULONG_MAX)
5516                 return (-1);
5517         *ulvalp = ulval;
5518         return (0);
5519 }
5520
5521 int
5522 getservice(char *n)
5523 {
5524         struct servent  *s;
5525         u_long           ulval;
5526
5527         if (atoul(n, &ulval) == 0) {
5528                 if (ulval > 65535) {
5529                         yyerror("illegal port value %lu", ulval);
5530                         return (-1);
5531                 }
5532                 return (htons(ulval));
5533         } else {
5534                 s = getservbyname(n, "tcp");
5535                 if (s == NULL)
5536                         s = getservbyname(n, "udp");
5537                 if (s == NULL) {
5538                         yyerror("unknown port %s", n);
5539                         return (-1);
5540                 }
5541                 return (s->s_port);
5542         }
5543 }
5544
5545 int
5546 rule_label(struct pf_rule *r, char *s)
5547 {
5548         if (s) {
5549                 if (strlcpy(r->label, s, sizeof(r->label)) >=
5550                     sizeof(r->label)) {
5551                         yyerror("rule label too long (max %d chars)",
5552                             sizeof(r->label)-1);
5553                         return (-1);
5554                 }
5555         }
5556         return (0);
5557 }
5558
5559 u_int16_t
5560 parseicmpspec(char *w, sa_family_t af)
5561 {
5562         const struct icmpcodeent        *p;
5563         u_long                           ulval;
5564         u_int8_t                         icmptype;
5565
5566         if (af == AF_INET)
5567                 icmptype = returnicmpdefault >> 8;
5568         else
5569                 icmptype = returnicmp6default >> 8;
5570
5571         if (atoul(w, &ulval) == -1) {
5572                 if ((p = geticmpcodebyname(icmptype, w, af)) == NULL) {
5573                         yyerror("unknown icmp code %s", w);
5574                         return (0);
5575                 }
5576                 ulval = p->code;
5577         }
5578         if (ulval > 255) {
5579                 yyerror("invalid icmp code %lu", ulval);
5580                 return (0);
5581         }
5582         return (icmptype << 8 | ulval);
5583 }
5584
5585 int
5586 pfctl_load_anchors(int dev, struct pfctl *his_pf, struct pfr_buffer *trans)
5587 {
5588         struct loadanchors      *la;
5589         FILE                    *his_fin;
5590
5591         TAILQ_FOREACH(la, &loadanchorshead, entries) {
5592                 if (his_pf->opts & PF_OPT_VERBOSE)
5593                         fprintf(stderr, "\nLoading anchor %s from %s\n",
5594                             la->anchorname, la->filename);
5595                 if ((his_fin = pfctl_fopen(la->filename, "r")) == NULL) {
5596                         warn("%s", la->filename);
5597                         continue;
5598                 }
5599                 if (pfctl_rules(dev, la->filename, his_fin, his_pf->opts, his_pf->optimize,
5600                     la->anchorname, trans) == -1)
5601                         return (-1);
5602         }
5603
5604         return (0);
5605 }