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