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