Fix warnings from byacc import.
[dragonfly.git] / usr.sbin / pfctl / parse.y
1 /*      $OpenBSD: parse.y,v 1.549 2008/07/03 16:09:34 deraadt Exp $     */
2
3 /*
4  * Copyright (c) 2001 Markus Friedl.  All rights reserved.
5  * Copyright (c) 2001 Daniel Hartmeier.  All rights reserved.
6  * Copyright (c) 2001 Theo de Raadt.  All rights reserved.
7  * Copyright (c) 2002,2003 Henning Brauer. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 %{
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <sys/stat.h>
33 #include <net/if.h>
34 #include <netinet/in.h>
35 #include <netinet/in_systm.h>
36 #include <netinet/ip.h>
37 #include <netinet/ip_icmp.h>
38 #include <netinet/icmp6.h>
39 #include <net/pf/pfvar.h>
40 #include <arpa/inet.h>
41 #include <net/altq/altq.h>
42 #include <net/altq/altq_cbq.h>
43 #include <net/altq/altq_priq.h>
44 #include <net/altq/altq_hfsc.h>
45 #include <net/altq/altq_fairq.h>
46
47 #include <stdio.h>
48 #include <unistd.h>
49 #include <stdlib.h>
50 #include <netdb.h>
51 #include <stdarg.h>
52 #include <errno.h>
53 #include <string.h>
54 #include <ctype.h>
55 #include <math.h>
56 #include <err.h>
57 #include <limits.h>
58 #include <pwd.h>
59 #include <grp.h>
60 #include <md5.h>
61 #include <inttypes.h>
62
63 #include "pfctl_parser.h"
64 #include "pfctl.h"
65
66 static struct pfctl     *pf = NULL;
67 static int               debug = 0;
68 static int               rulestate = 0;
69 static u_int16_t         returnicmpdefault =
70                             (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
71 static u_int16_t         returnicmp6default =
72                             (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
73 static int               blockpolicy = PFRULE_DROP;
74 static int               require_order = 1;
75 static int               default_statelock;
76 static int               default_keeppolicy_action;
77 static struct node_state_opt *default_keeppolicy_options;
78
79 TAILQ_HEAD(files, file)          files = TAILQ_HEAD_INITIALIZER(files);
80 static struct file {
81         TAILQ_ENTRY(file)        entry;
82         FILE                    *stream;
83         char                    *name;
84         int                      lineno;
85         int                      errors;
86 } *file;
87 struct file     *pushfile(const char *, int);
88 int              popfile(void);
89 int              check_file_secrecy(int, const char *);
90 int              yylex(void);
91 int              yyerror(const char *, ...) __printflike(1, 2);
92 int              kw_cmp(const void *, const void *);
93 int              lookup(char *);
94 int              lgetc(int);
95 int              lungetc(int);
96 int              findeol(void);
97
98 TAILQ_HEAD(symhead, sym)         symhead = TAILQ_HEAD_INITIALIZER(symhead);
99 struct sym {
100         TAILQ_ENTRY(sym)         entry;
101         int                      used;
102         int                      persist;
103         char                    *nam;
104         char                    *val;
105 };
106 int              symset(const char *, const char *, int);
107 char            *symget(const char *);
108
109 int              atoul(char *, u_long *);
110
111 enum {
112         PFCTL_STATE_NONE,
113         PFCTL_STATE_OPTION,
114         PFCTL_STATE_SCRUB,
115         PFCTL_STATE_QUEUE,
116         PFCTL_STATE_NAT,
117         PFCTL_STATE_FILTER
118 };
119
120 struct node_proto {
121         u_int8_t                 proto;
122         struct node_proto       *next;
123         struct node_proto       *tail;
124 };
125
126 struct node_port {
127         u_int16_t                port[2];
128         u_int8_t                 op;
129         struct node_port        *next;
130         struct node_port        *tail;
131 };
132
133 struct node_uid {
134         uid_t                    uid[2];
135         u_int8_t                 op;
136         struct node_uid         *next;
137         struct node_uid         *tail;
138 };
139
140 struct node_gid {
141         gid_t                    gid[2];
142         u_int8_t                 op;
143         struct node_gid         *next;
144         struct node_gid         *tail;
145 };
146
147 struct node_icmp {
148         u_int8_t                 code;
149         u_int8_t                 type;
150         u_int8_t                 proto;
151         struct node_icmp        *next;
152         struct node_icmp        *tail;
153 };
154
155 enum    { PF_STATE_OPT_MAX, PF_STATE_OPT_NOSYNC, PF_STATE_OPT_SRCTRACK,
156             PF_STATE_OPT_MAX_SRC_STATES, PF_STATE_OPT_MAX_SRC_CONN,
157             PF_STATE_OPT_MAX_SRC_CONN_RATE, PF_STATE_OPT_MAX_SRC_NODES,
158             PF_STATE_OPT_OVERLOAD, PF_STATE_OPT_STATELOCK,
159             PF_STATE_OPT_TIMEOUT, PF_STATE_OPT_SLOPPY, PF_STATE_OPT_PICKUPS };
160
161 enum    { PF_SRCTRACK_NONE, PF_SRCTRACK, PF_SRCTRACK_GLOBAL, PF_SRCTRACK_RULE };
162
163 struct node_state_opt {
164         int                      type;
165         union {
166                 u_int32_t        max_states;
167                 u_int32_t        max_src_states;
168                 u_int32_t        max_src_conn;
169                 struct {
170                         u_int32_t       limit;
171                         u_int32_t       seconds;
172                 }                max_src_conn_rate;
173                 struct {
174                         u_int8_t        flush;
175                         char            tblname[PF_TABLE_NAME_SIZE];
176                 }                overload;
177                 u_int32_t        max_src_nodes;
178                 u_int8_t         src_track;
179                 u_int8_t         pickup_mode;
180                 u_int32_t        statelock;
181                 struct {
182                         int             number;
183                         u_int32_t       seconds;
184                 }                timeout;
185         }                        data;
186         struct node_state_opt   *next;
187         struct node_state_opt   *tail;
188 };
189
190 struct peer {
191         struct node_host        *host;
192         struct node_port        *port;
193 };
194
195 struct node_queue {
196         char                     queue[PF_QNAME_SIZE];
197         char                     parent[PF_QNAME_SIZE];
198         char                     ifname[IFNAMSIZ];
199         int                      scheduler;
200         struct node_queue       *next;
201         struct node_queue       *tail;
202 }       *queues = NULL;
203
204 struct node_qassign {
205         char            *qname;
206         char            *pqname;
207 };
208
209 struct filter_opts {
210         int                      marker;
211 #define FOM_FLAGS       0x01
212 #define FOM_ICMP        0x02
213 #define FOM_TOS         0x04
214 #define FOM_KEEP        0x08
215 #define FOM_SRCTRACK    0x10
216         struct node_uid         *uid;
217         struct node_gid         *gid;
218         struct {
219                 u_int8_t         b1;
220                 u_int8_t         b2;
221                 u_int16_t        w;
222                 u_int16_t        w2;
223         } flags;
224         struct node_icmp        *icmpspec;
225         u_int32_t                tos;
226         u_int32_t                prob;
227         struct {
228                 int                      action;
229                 struct node_state_opt   *options;
230         } keep;
231         int                      fragment;
232         int                      allowopts;
233         char                    *label;
234         struct node_qassign      queues;
235         char                    *tag;
236         char                    *match_tag;
237         u_int8_t                 match_tag_not;
238         u_int                    rtableid;
239         struct {
240                 struct node_host        *addr;
241                 u_int16_t               port;
242         }                        divert;
243 } filter_opts;
244
245 struct antispoof_opts {
246         char                    *label;
247         u_int                    rtableid;
248 } antispoof_opts;
249
250 struct scrub_opts {
251         int                     marker;
252 #define SOM_MINTTL      0x01
253 #define SOM_MAXMSS      0x02
254 #define SOM_FRAGCACHE   0x04
255 #define SOM_SETTOS      0x08
256         int                     nodf;
257         int                     minttl;
258         int                     maxmss;
259         int                     settos;
260         int                     fragcache;
261         int                     randomid;
262         int                     reassemble_tcp;
263         char                   *match_tag;
264         u_int8_t                match_tag_not;
265         u_int                   rtableid;
266 } scrub_opts;
267
268 struct queue_opts {
269         int                     marker;
270 #define QOM_BWSPEC      0x01
271 #define QOM_SCHEDULER   0x02
272 #define QOM_PRIORITY    0x04
273 #define QOM_TBRSIZE     0x08
274 #define QOM_QLIMIT      0x10
275         struct node_queue_bw    queue_bwspec;
276         struct node_queue_opt   scheduler;
277         int                     priority;
278         int                     tbrsize;
279         int                     qlimit;
280 } queue_opts;
281
282 struct table_opts {
283         int                     flags;
284         int                     init_addr;
285         struct node_tinithead   init_nodes;
286 } table_opts;
287
288 struct pool_opts {
289         int                      marker;
290 #define POM_TYPE                0x01
291 #define POM_STICKYADDRESS       0x02
292         u_int8_t                 opts;
293         int                      type;
294         int                      staticport;
295         struct pf_poolhashkey   *key;
296
297 } pool_opts;
298
299
300 struct node_hfsc_opts   hfsc_opts;
301 struct node_fairq_opts  fairq_opts;
302
303 int              disallow_table(struct node_host *, const char *);
304 int              disallow_urpf_failed(struct node_host *, const char *);
305 int              disallow_alias(struct node_host *, const char *);
306 int              rule_consistent(struct pf_rule *, int);
307 int              filter_consistent(struct pf_rule *, int);
308 int              nat_consistent(struct pf_rule *);
309 int              rdr_consistent(struct pf_rule *);
310 int              process_tabledef(char *, struct table_opts *);
311 void             expand_label_str(char *, size_t, const char *, const char *);
312 void             expand_label_if(const char *, char *, size_t, const char *);
313 void             expand_label_addr(const char *, char *, size_t, u_int8_t,
314                     struct node_host *);
315 void             expand_label_port(const char *, char *, size_t,
316                     struct node_port *);
317 void             expand_label_proto(const char *, char *, size_t, u_int8_t);
318 void             expand_label_nr(const char *, char *, size_t);
319 void             expand_label(char *, size_t, const char *, u_int8_t,
320                     struct node_host *, struct node_port *, struct node_host *,
321                     struct node_port *, u_int8_t);
322 void             expand_rule(struct pf_rule *, struct node_if *,
323                     struct node_host *, struct node_proto *, struct node_os *,
324                     struct node_host *, struct node_port *, struct node_host *,
325                     struct node_port *, struct node_uid *, struct node_gid *,
326                     struct node_icmp *, const char *);
327 int              expand_altq(struct pf_altq *, struct node_if *,
328                     struct node_queue *, struct node_queue_bw bwspec,
329                     struct node_queue_opt *);
330 int              expand_queue(struct pf_altq *, struct node_if *,
331                     struct node_queue *, struct node_queue_bw,
332                     struct node_queue_opt *);
333 int              expand_skip_interface(struct node_if *);
334
335 int      check_rulestate(int);
336 int      getservice(char *);
337 int      rule_label(struct pf_rule *, char *);
338
339 void     mv_rules(struct pf_ruleset *, struct pf_ruleset *);
340 void     decide_address_family(struct node_host *, sa_family_t *);
341 void     remove_invalid_hosts(struct node_host **, sa_family_t *);
342 int      invalid_redirect(struct node_host *, sa_family_t);
343 u_int16_t parseicmpspec(char *, sa_family_t);
344
345 TAILQ_HEAD(loadanchorshead, loadanchors)
346     loadanchorshead = TAILQ_HEAD_INITIALIZER(loadanchorshead);
347
348 struct loadanchors {
349         TAILQ_ENTRY(loadanchors)         entries;
350         char                            *anchorname;
351         char                            *filename;
352 };
353
354 typedef struct {
355         union {
356                 int64_t                  number;
357                 double                   probability;
358                 int                      i;
359                 char                    *string;
360                 u_int                    rtableid;
361                 struct {
362                         u_int8_t         b1;
363                         u_int8_t         b2;
364                         u_int16_t        w;
365                         u_int16_t        w2;
366                 }                        b;
367                 struct range {
368                         int              a;
369                         int              b;
370                         int              t;
371                 }                        range;
372                 struct node_if          *interface;
373                 struct node_proto       *proto;
374                 struct node_icmp        *icmp;
375                 struct node_host        *host;
376                 struct node_os          *os;
377                 struct node_port        *port;
378                 struct node_uid         *uid;
379                 struct node_gid         *gid;
380                 struct node_state_opt   *state_opt;
381                 struct peer              peer;
382                 struct {
383                         struct peer      src, dst;
384                         struct node_os  *src_os;
385                 }                        fromto;
386                 struct {
387                         struct node_host        *host;
388                         u_int8_t                 rt;
389                         u_int8_t                 pool_opts;
390                         sa_family_t              af;
391                         struct pf_poolhashkey   *key;
392                 }                        route;
393                 struct redirection {
394                         struct node_host        *host;
395                         struct range             rport;
396                 }                       *redirection;
397                 struct {
398                         int                      action;
399                         struct node_state_opt   *options;
400                 }                        keep_state;
401                 struct {
402                         u_int8_t         log;
403                         u_int8_t         logif;
404                         u_int8_t         quick;
405                 }                        logquick;
406                 struct {
407                         int              neg;
408                         char            *name;
409                 }                        tagged;
410                 struct pf_poolhashkey   *hashkey;
411                 struct node_queue       *queue;
412                 struct node_queue_opt    queue_options;
413                 struct node_queue_bw     queue_bwspec;
414                 struct node_qassign      qassign;
415                 struct filter_opts       filter_opts;
416                 struct antispoof_opts    antispoof_opts;
417                 struct queue_opts        queue_opts;
418                 struct scrub_opts        scrub_opts;
419                 struct table_opts        table_opts;
420                 struct pool_opts         pool_opts;
421                 struct node_hfsc_opts    hfsc_opts;
422                 struct node_fairq_opts   fairq_opts;
423         } v;
424         int lineno;
425 } YYSTYPE;
426
427 #define PPORT_RANGE     1
428 #define PPORT_STAR      2
429 int     parseport(char *, struct range *r, int);
430
431 #define DYNIF_MULTIADDR(addr) ((addr).type == PF_ADDR_DYNIFTL && \
432         (!((addr).iflags & PFI_AFLAG_NOALIAS) ||                 \
433         !isdigit((addr).v.ifname[strlen((addr).v.ifname)-1])))
434
435 extern char     *infile;
436
437 %}
438
439 %token  PASS BLOCK SCRUB RETURN IN OS OUT LOG QUICK ON FROM TO FLAGS
440 %token  RETURNRST RETURNICMP RETURNICMP6 PROTO INET INET6 ALL ANY ICMPTYPE
441 %token  ICMP6TYPE CODE KEEP MODULATE STATE PORT RDR NAT BINAT ARROW NODF
442 %token  MINTTL ERROR ALLOWOPTS FASTROUTE FILENAME ROUTETO DUPTO REPLYTO NO LABEL
443 %token  NOROUTE URPFFAILED FRAGMENT USER GROUP MAXMSS MAXIMUM TTL TOS DROP TABLE
444 %token  REASSEMBLE FRAGDROP FRAGCROP ANCHOR NATANCHOR RDRANCHOR BINATANCHOR
445 %token  SET OPTIMIZATION TIMEOUT LIMIT LOGINTERFACE BLOCKPOLICY RANDOMID
446 %token  REQUIREORDER SYNPROXY FINGERPRINTS NOSYNC DEBUG SKIP HOSTID
447 %token  ANTISPOOF FOR INCLUDE
448 %token  BITMASK RANDOM SOURCEHASH ROUNDROBIN STATICPORT PROBABILITY
449 %token  ALTQ CBQ PRIQ HFSC FAIRQ BANDWIDTH TBRSIZE LINKSHARE REALTIME UPPERLIMIT
450 %token  QUEUE PRIORITY QLIMIT RTABLE HOGS BUCKETS
451 %token  LOAD RULESET_OPTIMIZATION
452 %token  PICKUPS NOPICKUPS HASHONLY
453 %token  STICKYADDRESS MAXSRCSTATES MAXSRCNODES SOURCETRACK GLOBAL RULE
454 %token  MAXSRCCONN MAXSRCCONNRATE OVERLOAD FLUSH SLOPPY
455 %token  TAGGED TAG IFBOUND FLOATING STATEPOLICY ROUTE SETTOS KEEPPOLICY
456 %token  DIVERTTO DIVERTREPLY
457 %token  <v.string>              STRING
458 %token  <v.number>              NUMBER
459 %token  <v.i>                   PORTBINARY
460 %type   <v.interface>           interface if_list if_item_not if_item
461 %type   <v.number>              number icmptype icmp6type uid gid
462 %type   <v.number>              tos not yesno
463 %type   <v.probability>         probability
464 %type   <v.i>                   no dir af fragcache optimizer
465 %type   <v.i>                   sourcetrack flush unaryop statelock
466 %type   <v.b>                   action nataction natpasslog scrubaction
467 %type   <v.b>                   flags flag blockspec
468 %type   <v.range>               portplain portstar portrange
469 %type   <v.hashkey>             hashkey
470 %type   <v.proto>               proto proto_list proto_item
471 %type   <v.number>              protoval
472 %type   <v.icmp>                icmpspec
473 %type   <v.icmp>                icmp_list icmp_item
474 %type   <v.icmp>                icmp6_list icmp6_item
475 %type   <v.number>              reticmpspec reticmp6spec
476 %type   <v.fromto>              fromto
477 %type   <v.peer>                ipportspec from to
478 %type   <v.host>                ipspec toipspec xhost host dynaddr host_list
479 %type   <v.host>                redir_host_list redirspec
480 %type   <v.host>                route_host route_host_list routespec
481 %type   <v.os>                  os xos os_list
482 %type   <v.port>                portspec port_list port_item
483 %type   <v.uid>                 uids uid_list uid_item
484 %type   <v.gid>                 gids gid_list gid_item
485 %type   <v.route>               route
486 %type   <v.redirection>         redirection redirpool
487 %type   <v.string>              label stringall tag anchorname
488 %type   <v.string>              string varstring numberstring
489 %type   <v.keep_state>          keep
490 %type   <v.state_opt>           state_opt_spec state_opt_list state_opt_item
491 %type   <v.logquick>            logquick quick log logopts logopt
492 %type   <v.interface>           antispoof_ifspc antispoof_iflst antispoof_if
493 %type   <v.qassign>             qname
494 %type   <v.queue>               qassign qassign_list qassign_item
495 %type   <v.queue_options>       scheduler
496 %type   <v.number>              cbqflags_list cbqflags_item
497 %type   <v.number>              priqflags_list priqflags_item
498 %type   <v.hfsc_opts>           hfscopts_list hfscopts_item hfsc_opts
499 %type   <v.fairq_opts>          fairqopts_list fairqopts_item fairq_opts
500 %type   <v.queue_bwspec>        bandwidth
501 %type   <v.filter_opts>         filter_opts filter_opt filter_opts_l
502 %type   <v.antispoof_opts>      antispoof_opts antispoof_opt antispoof_opts_l
503 %type   <v.queue_opts>          queue_opts queue_opt queue_opts_l
504 %type   <v.scrub_opts>          scrub_opts scrub_opt scrub_opts_l
505 %type   <v.table_opts>          table_opts table_opt table_opts_l
506 %type   <v.pool_opts>           pool_opts pool_opt pool_opts_l
507 %type   <v.tagged>              tagged
508 %type   <v.rtableid>            rtable
509 %%
510
511 ruleset         : /* empty */
512                 | ruleset include '\n'
513                 | ruleset '\n'
514                 | ruleset option '\n'
515                 | ruleset scrubrule '\n'
516                 | ruleset natrule '\n'
517                 | ruleset binatrule '\n'
518                 | ruleset pfrule '\n'
519                 | ruleset anchorrule '\n'
520                 | ruleset loadrule '\n'
521                 | ruleset altqif '\n'
522                 | ruleset queuespec '\n'
523                 | ruleset varset '\n'
524                 | ruleset antispoof '\n'
525                 | ruleset tabledef '\n'
526                 | '{' fakeanchor '}' '\n';
527                 | ruleset error '\n'            { file->errors++; }
528                 ;
529
530 include         : INCLUDE STRING                {
531                         struct file     *nfile;
532
533                         if ((nfile = pushfile($2, 0)) == NULL) {
534                                 yyerror("failed to include file %s", $2);
535                                 free($2);
536                                 YYERROR;
537                         }
538                         free($2);
539
540                         file = nfile;
541                         lungetc('\n');
542                 }
543                 ;
544
545 /*
546  * apply to previouslys specified rule: must be careful to note
547  * what that is: pf or nat or binat or rdr
548  */
549 fakeanchor      : fakeanchor '\n'
550                 | fakeanchor anchorrule '\n'
551                 | fakeanchor binatrule '\n'
552                 | fakeanchor natrule '\n'
553                 | fakeanchor pfrule '\n'
554                 | fakeanchor error '\n'
555                 ;
556
557 optimizer       : string        {
558                         if (!strcmp($1, "none"))
559                                 $$ = 0;
560                         else if (!strcmp($1, "basic"))
561                                 $$ = PF_OPTIMIZE_BASIC;
562                         else if (!strcmp($1, "profile"))
563                                 $$ = PF_OPTIMIZE_BASIC | PF_OPTIMIZE_PROFILE;
564                         else {
565                                 yyerror("unknown ruleset-optimization %s", $1);
566                                 YYERROR;
567                         }
568                 }
569                 ;
570
571 option          : SET OPTIMIZATION STRING               {
572                         if (check_rulestate(PFCTL_STATE_OPTION)) {
573                                 free($3);
574                                 YYERROR;
575                         }
576                         if (pfctl_set_optimization(pf, $3) != 0) {
577                                 yyerror("unknown optimization %s", $3);
578                                 free($3);
579                                 YYERROR;
580                         }
581                         free($3);
582                 }
583                 | SET RULESET_OPTIMIZATION optimizer {
584                         if (!(pf->opts & PF_OPT_OPTIMIZE)) {
585                                 pf->opts |= PF_OPT_OPTIMIZE;
586                                 pf->optimize = $3;
587                         }
588                 }
589                 | SET TIMEOUT timeout_spec
590                 | SET TIMEOUT '{' optnl timeout_list '}'
591                 | SET LIMIT limit_spec
592                 | SET LIMIT '{' optnl limit_list '}'
593                 | SET LOGINTERFACE stringall            {
594                         if (check_rulestate(PFCTL_STATE_OPTION)) {
595                                 free($3);
596                                 YYERROR;
597                         }
598                         if (pfctl_set_logif(pf, $3) != 0) {
599                                 yyerror("error setting loginterface %s", $3);
600                                 free($3);
601                                 YYERROR;
602                         }
603                         free($3);
604                 }
605                 | SET HOSTID number {
606                         if ($3 == 0 || $3 > UINT_MAX) {
607                                 yyerror("hostid must be non-zero");
608                                 YYERROR;
609                         }
610                         if (pfctl_set_hostid(pf, $3) != 0) {
611                                 yyerror("error setting hostid %08" PRId64, $3);
612                                 YYERROR;
613                         }
614                 }
615                 | SET BLOCKPOLICY DROP  {
616                         if (pf->opts & PF_OPT_VERBOSE)
617                                 printf("set block-policy drop\n");
618                         if (check_rulestate(PFCTL_STATE_OPTION))
619                                 YYERROR;
620                         blockpolicy = PFRULE_DROP;
621                 }
622                 | SET BLOCKPOLICY RETURN {
623                         if (pf->opts & PF_OPT_VERBOSE)
624                                 printf("set block-policy return\n");
625                         if (check_rulestate(PFCTL_STATE_OPTION))
626                                 YYERROR;
627                         blockpolicy = PFRULE_RETURN;
628                 }
629                 | SET REQUIREORDER yesno {
630                         if (pf->opts & PF_OPT_VERBOSE)
631                                 printf("set require-order %s\n",
632                                     $3 == 1 ? "yes" : "no");
633                         require_order = $3;
634                 }
635                 | SET FINGERPRINTS STRING {
636                         if (pf->opts & PF_OPT_VERBOSE)
637                                 printf("set fingerprints \"%s\"\n", $3);
638                         if (check_rulestate(PFCTL_STATE_OPTION)) {
639                                 free($3);
640                                 YYERROR;
641                         }
642                         if (!pf->anchor->name[0]) {
643                                 if (pfctl_file_fingerprints(pf->dev,
644                                     pf->opts, $3)) {
645                                         yyerror("error loading "
646                                             "fingerprints %s", $3);
647                                         free($3);
648                                         YYERROR;
649                                 }
650                         }
651                         free($3);
652                 }
653                 | SET STATEPOLICY statelock {
654                         if (pf->opts & PF_OPT_VERBOSE)
655                                 switch ($3) {
656                                 case 0:
657                                         printf("set state-policy floating\n");
658                                         break;
659                                 case PFRULE_IFBOUND:
660                                         printf("set state-policy if-bound\n");
661                                         break;
662                                 }
663                         default_statelock = $3;
664                 }
665                 | SET KEEPPOLICY keep {
666                         if (pf->opts & PF_OPT_VERBOSE)
667                                 printf("A default keeppolicy was set\n");
668                         default_keeppolicy_action  = $3.action;
669                         default_keeppolicy_options = $3.options;
670                 }
671                 | SET DEBUG STRING {
672                         if (check_rulestate(PFCTL_STATE_OPTION)) {
673                                 free($3);
674                                 YYERROR;
675                         }
676                         if (pfctl_set_debug(pf, $3) != 0) {
677                                 yyerror("error setting debuglevel %s", $3);
678                                 free($3);
679                                 YYERROR;
680                         }
681                         free($3);
682                 }
683                 | SET SKIP interface {
684                         if (expand_skip_interface($3) != 0) {
685                                 yyerror("error setting skip interface(s)");
686                                 YYERROR;
687                         }
688                 }
689                 ;
690
691 stringall       : STRING        { $$ = $1; }
692                 | ALL           {
693                         if (($$ = strdup("all")) == NULL) {
694                                 err(1, "stringall: strdup");
695                         }
696                 }
697                 ;
698
699 string          : STRING string                         {
700                         if (asprintf(&$$, "%s %s", $1, $2) == -1)
701                                 err(1, "string: asprintf");
702                         free($1);
703                         free($2);
704                 }
705                 | STRING
706                 ;
707
708 varstring       : numberstring varstring                {
709                         if (asprintf(&$$, "%s %s", $1, $2) == -1)
710                                 err(1, "string: asprintf");
711                         free($1);
712                         free($2);
713                 }
714                 | numberstring
715                 ;
716
717 numberstring    : NUMBER                                {
718                         char    *s;
719                         if (asprintf(&s, "%" PRId64, $1) == -1) {
720                                 yyerror("string: asprintf");
721                                 YYERROR;
722                         }
723                         $$ = s;
724                 }
725                 | STRING
726                 ;
727
728 varset          : STRING '=' varstring  {
729                         if (pf->opts & PF_OPT_VERBOSE)
730                                 printf("%s = \"%s\"\n", $1, $3);
731                         if (symset($1, $3, 0) == -1)
732                                 err(1, "cannot store variable %s", $1);
733                         free($1);
734                         free($3);
735                 }
736                 ;
737
738 anchorname      : STRING                        { $$ = $1; }
739                 | /* empty */                   { $$ = NULL; }
740                 ;
741
742 pfa_anchorlist  : /* empty */
743                 | pfa_anchorlist '\n'
744                 | pfa_anchorlist pfrule '\n'
745                 | pfa_anchorlist anchorrule '\n'
746                 ;
747
748 pfa_anchor      : '{'
749                 {
750                         char ta[PF_ANCHOR_NAME_SIZE];
751                         struct pf_ruleset *rs;
752
753                         /* steping into a brace anchor */
754                         pf->asd++;
755                         pf->bn++;
756                         pf->brace = 1;
757
758                         /* create a holding ruleset in the root */
759                         snprintf(ta, PF_ANCHOR_NAME_SIZE, "_%d", pf->bn);
760                         rs = pf_find_or_create_ruleset(ta);
761                         if (rs == NULL)
762                                 err(1, "pfa_anchor: pf_find_or_create_ruleset");
763                         pf->astack[pf->asd] = rs->anchor;
764                         pf->anchor = rs->anchor;
765                 } '\n' pfa_anchorlist '}'
766                 {
767                         pf->alast = pf->anchor;
768                         pf->asd--;
769                         pf->anchor = pf->astack[pf->asd];
770                 }
771                 | /* empty */
772                 ;
773
774 anchorrule      : ANCHOR anchorname dir quick interface af proto fromto
775                     filter_opts pfa_anchor
776                 {
777                         struct pf_rule  r;
778                         struct node_proto       *proto;
779
780                         if (check_rulestate(PFCTL_STATE_FILTER)) {
781                                 if ($2)
782                                         free($2);
783                                 YYERROR;
784                         }
785
786                         if ($2 && ($2[0] == '_' || strstr($2, "/_") != NULL)) {
787                                 free($2);
788                                 yyerror("anchor names beginning with '_' "
789                                     "are reserved for internal use");
790                                 YYERROR;
791                         }
792
793                         memset(&r, 0, sizeof(r));
794                         if (pf->astack[pf->asd + 1]) {
795                                 /* move inline rules into relative location */
796                                 pf_anchor_setup(&r,
797                                     &pf->astack[pf->asd]->ruleset,
798                                     $2 ? $2 : pf->alast->name);
799                 
800                                 if (r.anchor == NULL)
801                                         err(1, "anchorrule: unable to "
802                                             "create ruleset");
803
804                                 if (pf->alast != r.anchor) {
805                                         if (r.anchor->match) {
806                                                 yyerror("inline anchor '%s' "
807                                                     "already exists",
808                                                     r.anchor->name);
809                                                 YYERROR;
810                                         }
811                                         mv_rules(&pf->alast->ruleset,
812                                             &r.anchor->ruleset);
813                                 }
814                                 pf_remove_if_empty_ruleset(&pf->alast->ruleset);
815                                 pf->alast = r.anchor;
816                         } else {
817                                 if (!$2) {
818                                         yyerror("anchors without explicit "
819                                             "rules must specify a name");
820                                         YYERROR;
821                                 }
822                         }
823                         r.direction = $3;
824                         r.quick = $4.quick;
825                         r.af = $6;
826                         r.prob = $9.prob;
827                         r.rtableid = $9.rtableid;
828
829                         if ($9.tag)
830                                 if (strlcpy(r.tagname, $9.tag,
831                                     PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
832                                         yyerror("tag too long, max %u chars",
833                                             PF_TAG_NAME_SIZE - 1);
834                                         YYERROR;
835                                 }
836                         if ($9.match_tag)
837                                 if (strlcpy(r.match_tagname, $9.match_tag,
838                                     PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
839                                         yyerror("tag too long, max %u chars",
840                                             PF_TAG_NAME_SIZE - 1);
841                                         YYERROR;
842                                 }
843                         r.match_tag_not = $9.match_tag_not;
844                         if (rule_label(&r, $9.label))
845                                 YYERROR;
846                         free($9.label);
847                         r.flags = $9.flags.b1;
848                         r.flagset = $9.flags.b2;
849                         if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
850                                 yyerror("flags always false");
851                                 YYERROR;
852                         }
853                         if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
854                                 for (proto = $7; proto != NULL &&
855                                     proto->proto != IPPROTO_TCP;
856                                     proto = proto->next)
857                                         ;       /* nothing */
858                                 if (proto == NULL && $7 != NULL) {
859                                         if ($9.flags.b1 || $9.flags.b2)
860                                                 yyerror(
861                                                     "flags only apply to tcp");
862                                         if ($8.src_os)
863                                                 yyerror(
864                                                     "OS fingerprinting only "
865                                                     "applies to tcp");
866                                         YYERROR;
867                                 }
868                         }
869
870                         r.tos = $9.tos;
871
872                         if ($9.keep.action) {
873                                 yyerror("cannot specify state handling "
874                                     "on anchors");
875                                 YYERROR;
876                         }
877
878                         if ($9.match_tag)
879                                 if (strlcpy(r.match_tagname, $9.match_tag,
880                                     PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
881                                         yyerror("tag too long, max %u chars",
882                                             PF_TAG_NAME_SIZE - 1);
883                                         YYERROR;
884                                 }
885                         r.match_tag_not = $9.match_tag_not;
886
887                         decide_address_family($8.src.host, &r.af);
888                         decide_address_family($8.dst.host, &r.af);
889
890                         expand_rule(&r, $5, NULL, $7, $8.src_os,
891                             $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
892                             $9.uid, $9.gid, $9.icmpspec,
893                             pf->astack[pf->asd + 1] ? pf->alast->name : $2);
894                         free($2);
895                         pf->astack[pf->asd + 1] = NULL;
896                 }
897                 | NATANCHOR string interface af proto fromto rtable {
898                         struct pf_rule  r;
899
900                         if (check_rulestate(PFCTL_STATE_NAT)) {
901                                 free($2);
902                                 YYERROR;
903                         }
904
905                         memset(&r, 0, sizeof(r));
906                         r.action = PF_NAT;
907                         r.af = $4;
908                         r.rtableid = $7;
909
910                         decide_address_family($6.src.host, &r.af);
911                         decide_address_family($6.dst.host, &r.af);
912
913                         expand_rule(&r, $3, NULL, $5, $6.src_os,
914                             $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
915                             0, 0, 0, $2);
916                         free($2);
917                 }
918                 | RDRANCHOR string interface af proto fromto rtable {
919                         struct pf_rule  r;
920
921                         if (check_rulestate(PFCTL_STATE_NAT)) {
922                                 free($2);
923                                 YYERROR;
924                         }
925
926                         memset(&r, 0, sizeof(r));
927                         r.action = PF_RDR;
928                         r.af = $4;
929                         r.rtableid = $7;
930
931                         decide_address_family($6.src.host, &r.af);
932                         decide_address_family($6.dst.host, &r.af);
933
934                         if ($6.src.port != NULL) {
935                                 yyerror("source port parameter not supported"
936                                     " in rdr-anchor");
937                                 YYERROR;
938                         }
939                         if ($6.dst.port != NULL) {
940                                 if ($6.dst.port->next != NULL) {
941                                         yyerror("destination port list "
942                                             "expansion not supported in "
943                                             "rdr-anchor");
944                                         YYERROR;
945                                 } else if ($6.dst.port->op != PF_OP_EQ) {
946                                         yyerror("destination port operators"
947                                             " not supported in rdr-anchor");
948                                         YYERROR;
949                                 }
950                                 r.dst.port[0] = $6.dst.port->port[0];
951                                 r.dst.port[1] = $6.dst.port->port[1];
952                                 r.dst.port_op = $6.dst.port->op;
953                         }
954
955                         expand_rule(&r, $3, NULL, $5, $6.src_os,
956                             $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
957                             0, 0, 0, $2);
958                         free($2);
959                 }
960                 | BINATANCHOR string interface af proto fromto rtable {
961                         struct pf_rule  r;
962
963                         if (check_rulestate(PFCTL_STATE_NAT)) {
964                                 free($2);
965                                 YYERROR;
966                         }
967
968                         memset(&r, 0, sizeof(r));
969                         r.action = PF_BINAT;
970                         r.af = $4;
971                         r.rtableid = $7;
972                         if ($5 != NULL) {
973                                 if ($5->next != NULL) {
974                                         yyerror("proto list expansion"
975                                             " not supported in binat-anchor");
976                                         YYERROR;
977                                 }
978                                 r.proto = $5->proto;
979                                 free($5);
980                         }
981
982                         if ($6.src.host != NULL || $6.src.port != NULL ||
983                             $6.dst.host != NULL || $6.dst.port != NULL) {
984                                 yyerror("fromto parameter not supported"
985                                     " in binat-anchor");
986                                 YYERROR;
987                         }
988
989                         decide_address_family($6.src.host, &r.af);
990                         decide_address_family($6.dst.host, &r.af);
991
992                         pfctl_add_rule(pf, &r, $2);
993                         free($2);
994                 }
995                 ;
996
997 loadrule        : LOAD ANCHOR string FROM string        {
998                         struct loadanchors      *loadanchor;
999
1000                         if (strlen(pf->anchor->name) + 1 +
1001                             strlen($3) >= MAXPATHLEN) {
1002                                 yyerror("anchorname %s too long, max %u\n",
1003                                     $3, MAXPATHLEN - 1);
1004                                 free($3);
1005                                 YYERROR;
1006                         }
1007                         loadanchor = calloc(1, sizeof(struct loadanchors));
1008                         if (loadanchor == NULL)
1009                                 err(1, "loadrule: calloc");
1010                         if ((loadanchor->anchorname = malloc(MAXPATHLEN)) ==
1011                             NULL)
1012                                 err(1, "loadrule: malloc");
1013                         if (pf->anchor->name[0])
1014                                 snprintf(loadanchor->anchorname, MAXPATHLEN,
1015                                     "%s/%s", pf->anchor->name, $3);
1016                         else
1017                                 strlcpy(loadanchor->anchorname, $3, MAXPATHLEN);
1018                         if ((loadanchor->filename = strdup($5)) == NULL)
1019                                 err(1, "loadrule: strdup");
1020
1021                         TAILQ_INSERT_TAIL(&loadanchorshead, loadanchor,
1022                             entries);
1023
1024                         free($3);
1025                         free($5);
1026                 };
1027
1028 scrubaction     : no SCRUB {
1029                         $$.b2 = $$.w = 0;
1030                         if ($1)
1031                                 $$.b1 = PF_NOSCRUB;
1032                         else
1033                                 $$.b1 = PF_SCRUB;
1034                 }
1035                 ;
1036
1037 scrubrule       : scrubaction dir logquick interface af proto fromto scrub_opts
1038                 {
1039                         struct pf_rule  r;
1040
1041                         if (check_rulestate(PFCTL_STATE_SCRUB))
1042                                 YYERROR;
1043
1044                         memset(&r, 0, sizeof(r));
1045
1046                         r.action = $1.b1;
1047                         r.direction = $2;
1048
1049                         r.log = $3.log;
1050                         r.logif = $3.logif;
1051                         if ($3.quick) {
1052                                 yyerror("scrub rules do not support 'quick'");
1053                                 YYERROR;
1054                         }
1055
1056                         r.af = $5;
1057                         if ($8.nodf)
1058                                 r.rule_flag |= PFRULE_NODF;
1059                         if ($8.randomid)
1060                                 r.rule_flag |= PFRULE_RANDOMID;
1061                         if ($8.reassemble_tcp) {
1062                                 if (r.direction != PF_INOUT) {
1063                                         yyerror("reassemble tcp rules can not "
1064                                             "specify direction");
1065                                         YYERROR;
1066                                 }
1067                                 r.rule_flag |= PFRULE_REASSEMBLE_TCP;
1068                         }
1069                         if ($8.minttl)
1070                                 r.min_ttl = $8.minttl;
1071                         if ($8.maxmss)
1072                                 r.max_mss = $8.maxmss;
1073                         if ($8.marker & SOM_SETTOS) {
1074                                 r.rule_flag |= PFRULE_SET_TOS;
1075                                 r.set_tos = $8.settos;
1076                         }
1077                         if ($8.fragcache)
1078                                 r.rule_flag |= $8.fragcache;
1079                         if ($8.match_tag)
1080                                 if (strlcpy(r.match_tagname, $8.match_tag,
1081                                     PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1082                                         yyerror("tag too long, max %u chars",
1083                                             PF_TAG_NAME_SIZE - 1);
1084                                         YYERROR;
1085                                 }
1086                         r.match_tag_not = $8.match_tag_not;
1087                         r.rtableid = $8.rtableid;
1088
1089                         expand_rule(&r, $4, NULL, $6, $7.src_os,
1090                             $7.src.host, $7.src.port, $7.dst.host, $7.dst.port,
1091                             NULL, NULL, NULL, "");
1092                 }
1093                 ;
1094
1095 scrub_opts      :       {
1096                                 bzero(&scrub_opts, sizeof scrub_opts);
1097                                 scrub_opts.rtableid = -1;
1098                         }
1099                     scrub_opts_l
1100                         { $$ = scrub_opts; }
1101                 | /* empty */ {
1102                         bzero(&scrub_opts, sizeof scrub_opts);
1103                         scrub_opts.rtableid = -1;
1104                         $$ = scrub_opts;
1105                 }
1106                 ;
1107
1108 scrub_opts_l    : scrub_opts_l scrub_opt
1109                 | scrub_opt
1110                 ;
1111
1112 scrub_opt       : NODF  {
1113                         if (scrub_opts.nodf) {
1114                                 yyerror("no-df cannot be respecified");
1115                                 YYERROR;
1116                         }
1117                         scrub_opts.nodf = 1;
1118                 }
1119                 | MINTTL NUMBER {
1120                         if (scrub_opts.marker & SOM_MINTTL) {
1121                                 yyerror("min-ttl cannot be respecified");
1122                                 YYERROR;
1123                         }
1124                         if ($2 < 0 || $2 > 255) {
1125                                 yyerror("illegal min-ttl value %" PRId64, $2);
1126                                 YYERROR;
1127                         }
1128                         scrub_opts.marker |= SOM_MINTTL;
1129                         scrub_opts.minttl = $2;
1130                 }
1131                 | MAXMSS NUMBER {
1132                         if (scrub_opts.marker & SOM_MAXMSS) {
1133                                 yyerror("max-mss cannot be respecified");
1134                                 YYERROR;
1135                         }
1136                         if ($2 < 0 || $2 > 65535) {
1137                                 yyerror("illegal max-mss value %" PRId64, $2);
1138                                 YYERROR;
1139                         }
1140                         scrub_opts.marker |= SOM_MAXMSS;
1141                         scrub_opts.maxmss = $2;
1142                 }
1143                 | SETTOS tos {
1144                         if (scrub_opts.marker & SOM_SETTOS) {
1145                                 yyerror("set-tos cannot be respecified");
1146                                 YYERROR;
1147                         }
1148                         scrub_opts.marker |= SOM_SETTOS;
1149                         scrub_opts.settos = $2;
1150                 }
1151                 | fragcache {
1152                         if (scrub_opts.marker & SOM_FRAGCACHE) {
1153                                 yyerror("fragcache cannot be respecified");
1154                                 YYERROR;
1155                         }
1156                         scrub_opts.marker |= SOM_FRAGCACHE;
1157                         scrub_opts.fragcache = $1;
1158                 }
1159                 | REASSEMBLE STRING {
1160                         if (strcasecmp($2, "tcp") != 0) {
1161                                 yyerror("scrub reassemble supports only tcp, "
1162                                     "not '%s'", $2);
1163                                 free($2);
1164                                 YYERROR;
1165                         }
1166                         free($2);
1167                         if (scrub_opts.reassemble_tcp) {
1168                                 yyerror("reassemble tcp cannot be respecified");
1169                                 YYERROR;
1170                         }
1171                         scrub_opts.reassemble_tcp = 1;
1172                 }
1173                 | RANDOMID {
1174                         if (scrub_opts.randomid) {
1175                                 yyerror("random-id cannot be respecified");
1176                                 YYERROR;
1177                         }
1178                         scrub_opts.randomid = 1;
1179                 }
1180                 | RTABLE NUMBER                         {
1181                         scrub_opts.rtableid = $2;
1182                 }
1183                 | not TAGGED string                     {
1184                         scrub_opts.match_tag = $3;
1185                         scrub_opts.match_tag_not = $1;
1186                 }
1187                 ;
1188
1189 fragcache       : FRAGMENT REASSEMBLE   { $$ = 0; /* default */ }
1190                 | FRAGMENT FRAGCROP     { $$ = PFRULE_FRAGCROP; }
1191                 | FRAGMENT FRAGDROP     { $$ = PFRULE_FRAGDROP; }
1192                 ;
1193
1194 antispoof       : ANTISPOOF logquick antispoof_ifspc af antispoof_opts {
1195                         struct pf_rule           r;
1196                         struct node_host        *h = NULL, *hh;
1197                         struct node_if          *i, *j;
1198
1199                         if (check_rulestate(PFCTL_STATE_FILTER))
1200                                 YYERROR;
1201
1202                         for (i = $3; i; i = i->next) {
1203                                 bzero(&r, sizeof(r));
1204
1205                                 r.action = PF_DROP;
1206                                 r.direction = PF_IN;
1207                                 r.log = $2.log;
1208                                 r.logif = $2.logif;
1209                                 r.quick = $2.quick;
1210                                 r.af = $4;
1211                                 if (rule_label(&r, $5.label))
1212                                         YYERROR;
1213                                 r.rtableid = $5.rtableid;
1214                                 j = calloc(1, sizeof(struct node_if));
1215                                 if (j == NULL)
1216                                         err(1, "antispoof: calloc");
1217                                 if (strlcpy(j->ifname, i->ifname,
1218                                     sizeof(j->ifname)) >= sizeof(j->ifname)) {
1219                                         free(j);
1220                                         yyerror("interface name too long");
1221                                         YYERROR;
1222                                 }
1223                                 j->not = 1;
1224                                 if (i->dynamic) {
1225                                         h = calloc(1, sizeof(*h));
1226                                         if (h == NULL)
1227                                                 err(1, "address: calloc");
1228                                         h->addr.type = PF_ADDR_DYNIFTL;
1229                                         set_ipmask(h, 128);
1230                                         if (strlcpy(h->addr.v.ifname, i->ifname,
1231                                             sizeof(h->addr.v.ifname)) >=
1232                                             sizeof(h->addr.v.ifname)) {
1233                                                 free(h);
1234                                                 yyerror(
1235                                                     "interface name too long");
1236                                                 YYERROR;
1237                                         }
1238                                         hh = malloc(sizeof(*hh));
1239                                         if (hh == NULL)
1240                                                  err(1, "address: malloc");
1241                                         bcopy(h, hh, sizeof(*hh));
1242                                         h->addr.iflags = PFI_AFLAG_NETWORK;
1243                                 } else {
1244                                         h = ifa_lookup(j->ifname,
1245                                             PFI_AFLAG_NETWORK);
1246                                         hh = NULL;
1247                                 }
1248
1249                                 if (h != NULL)
1250                                         expand_rule(&r, j, NULL, NULL, NULL, h,
1251                                             NULL, NULL, NULL, NULL, NULL,
1252                                             NULL, "");
1253
1254                                 if ((i->ifa_flags & IFF_LOOPBACK) == 0) {
1255                                         bzero(&r, sizeof(r));
1256
1257                                         r.action = PF_DROP;
1258                                         r.direction = PF_IN;
1259                                         r.log = $2.log;
1260                                         r.quick = $2.quick;
1261                                         r.af = $4;
1262                                         if (rule_label(&r, $5.label))
1263                                                 YYERROR;
1264                                         r.rtableid = $5.rtableid;
1265                                         if (hh != NULL)
1266                                                 h = hh;
1267                                         else
1268                                                 h = ifa_lookup(i->ifname, 0);
1269                                         if (h != NULL)
1270                                                 expand_rule(&r, NULL, NULL,
1271                                                     NULL, NULL, h, NULL, NULL,
1272                                                     NULL, NULL, NULL, NULL, "");
1273                                 } else
1274                                         free(hh);
1275                         }
1276                         free($5.label);
1277                 }
1278                 ;
1279
1280 antispoof_ifspc : FOR antispoof_if              { $$ = $2; }
1281                 | FOR '{' optnl antispoof_iflst '}'     { $$ = $4; }
1282                 ;
1283
1284 antispoof_iflst : antispoof_if optnl                    { $$ = $1; }
1285                 | antispoof_iflst comma antispoof_if optnl {
1286                         $1->tail->next = $3;
1287                         $1->tail = $3;
1288                         $$ = $1;
1289                 }
1290                 ;
1291
1292 antispoof_if  : if_item                         { $$ = $1; }
1293                 | '(' if_item ')'               {
1294                         $2->dynamic = 1;
1295                         $$ = $2;
1296                 }
1297                 ;
1298
1299 antispoof_opts  :       {
1300                                 bzero(&antispoof_opts, sizeof antispoof_opts);
1301                                 antispoof_opts.rtableid = -1;
1302                         }
1303                     antispoof_opts_l
1304                         { $$ = antispoof_opts; }
1305                 | /* empty */   {
1306                         bzero(&antispoof_opts, sizeof antispoof_opts);
1307                         antispoof_opts.rtableid = -1;
1308                         $$ = antispoof_opts;
1309                 }
1310                 ;
1311
1312 antispoof_opts_l        : antispoof_opts_l antispoof_opt
1313                         | antispoof_opt
1314                         ;
1315
1316 antispoof_opt   : label {
1317                         if (antispoof_opts.label) {
1318                                 yyerror("label cannot be redefined");
1319                                 YYERROR;
1320                         }
1321                         antispoof_opts.label = $1;
1322                 }
1323                 | RTABLE NUMBER                         {
1324                         antispoof_opts.rtableid = $2;
1325                 }
1326                 ;
1327
1328 not             : '!'           { $$ = 1; }
1329                 | /* empty */   { $$ = 0; }
1330                 ;
1331
1332 tabledef        : TABLE '<' STRING '>' table_opts {
1333                         struct node_host         *h, *nh;
1334                         struct node_tinit        *ti, *nti;
1335
1336                         if (strlen($3) >= PF_TABLE_NAME_SIZE) {
1337                                 yyerror("table name too long, max %d chars",
1338                                     PF_TABLE_NAME_SIZE - 1);
1339                                 free($3);
1340                                 YYERROR;
1341                         }
1342                         if (pf->loadopt & PFCTL_FLAG_TABLE)
1343                                 if (process_tabledef($3, &$5)) {
1344                                         free($3);
1345                                         YYERROR;
1346                                 }
1347                         free($3);
1348                         for (ti = SIMPLEQ_FIRST(&$5.init_nodes);
1349                             ti != SIMPLEQ_END(&$5.init_nodes); ti = nti) {
1350                                 if (ti->file)
1351                                         free(ti->file);
1352                                 for (h = ti->host; h != NULL; h = nh) {
1353                                         nh = h->next;
1354                                         free(h);
1355                                 }
1356                                 nti = SIMPLEQ_NEXT(ti, entries);
1357                                 free(ti);
1358                         }
1359                 }
1360                 ;
1361
1362 table_opts      :       {
1363                         bzero(&table_opts, sizeof table_opts);
1364                         SIMPLEQ_INIT(&table_opts.init_nodes);
1365                 }
1366                     table_opts_l
1367                         { $$ = table_opts; }
1368                 | /* empty */
1369                         {
1370                         bzero(&table_opts, sizeof table_opts);
1371                         SIMPLEQ_INIT(&table_opts.init_nodes);
1372                         $$ = table_opts;
1373                 }
1374                 ;
1375
1376 table_opts_l    : table_opts_l table_opt
1377                 | table_opt
1378                 ;
1379
1380 table_opt       : STRING                {
1381                         if (!strcmp($1, "const"))
1382                                 table_opts.flags |= PFR_TFLAG_CONST;
1383                         else if (!strcmp($1, "persist"))
1384                                 table_opts.flags |= PFR_TFLAG_PERSIST;
1385                         else if (!strcmp($1, "counters"))
1386                                 table_opts.flags |= PFR_TFLAG_COUNTERS;
1387                         else {
1388                                 yyerror("invalid table option '%s'", $1);
1389                                 free($1);
1390                                 YYERROR;
1391                         }
1392                         free($1);
1393                 }
1394                 | '{' optnl '}'         { table_opts.init_addr = 1; }
1395                 | '{' optnl host_list '}'       {
1396                         struct node_host        *n;
1397                         struct node_tinit       *ti;
1398
1399                         for (n = $3; n != NULL; n = n->next) {
1400                                 switch (n->addr.type) {
1401                                 case PF_ADDR_ADDRMASK:
1402                                         continue; /* ok */
1403                                 case PF_ADDR_RANGE:
1404                                         yyerror("address ranges are not "
1405                                             "permitted inside tables");
1406                                         break;
1407                                 case PF_ADDR_DYNIFTL:
1408                                         yyerror("dynamic addresses are not "
1409                                             "permitted inside tables");
1410                                         break;
1411                                 case PF_ADDR_TABLE:
1412                                         yyerror("tables cannot contain tables");
1413                                         break;
1414                                 case PF_ADDR_NOROUTE:
1415                                         yyerror("\"no-route\" is not permitted "
1416                                             "inside tables");
1417                                         break;
1418                                 case PF_ADDR_URPFFAILED:
1419                                         yyerror("\"urpf-failed\" is not "
1420                                             "permitted inside tables");
1421                                         break;
1422                                 default:
1423                                         yyerror("unknown address type %d",
1424                                             n->addr.type);
1425                                 }
1426                                 YYERROR;
1427                         }
1428                         if (!(ti = calloc(1, sizeof(*ti))))
1429                                 err(1, "table_opt: calloc");
1430                         ti->host = $3;
1431                         SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1432                             entries);
1433                         table_opts.init_addr = 1;
1434                 }
1435                 | FILENAME STRING       {
1436                         struct node_tinit       *ti;
1437
1438                         if (!(ti = calloc(1, sizeof(*ti))))
1439                                 err(1, "table_opt: calloc");
1440                         ti->file = $2;
1441                         SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1442                             entries);
1443                         table_opts.init_addr = 1;
1444                 }
1445                 ;
1446
1447 altqif          : ALTQ interface queue_opts QUEUE qassign {
1448                         struct pf_altq  a;
1449
1450                         if (check_rulestate(PFCTL_STATE_QUEUE))
1451                                 YYERROR;
1452
1453                         memset(&a, 0, sizeof(a));
1454                         if ($3.scheduler.qtype == ALTQT_NONE) {
1455                                 yyerror("no scheduler specified!");
1456                                 YYERROR;
1457                         }
1458                         a.scheduler = $3.scheduler.qtype;
1459                         a.qlimit = $3.qlimit;
1460                         a.tbrsize = $3.tbrsize;
1461                         if ($5 == NULL) {
1462                                 yyerror("no child queues specified");
1463                                 YYERROR;
1464                         }
1465                         if (expand_altq(&a, $2, $5, $3.queue_bwspec,
1466                             &$3.scheduler))
1467                                 YYERROR;
1468                 }
1469                 ;
1470
1471 queuespec       : QUEUE STRING interface queue_opts qassign {
1472                         struct pf_altq  a;
1473
1474                         if (check_rulestate(PFCTL_STATE_QUEUE)) {
1475                                 free($2);
1476                                 YYERROR;
1477                         }
1478
1479                         memset(&a, 0, sizeof(a));
1480
1481                         if (strlcpy(a.qname, $2, sizeof(a.qname)) >=
1482                             sizeof(a.qname)) {
1483                                 yyerror("queue name too long (max "
1484                                     "%d chars)", PF_QNAME_SIZE-1);
1485                                 free($2);
1486                                 YYERROR;
1487                         }
1488                         free($2);
1489                         if ($4.tbrsize) {
1490                                 yyerror("cannot specify tbrsize for queue");
1491                                 YYERROR;
1492                         }
1493                         if ($4.priority > 255) {
1494                                 yyerror("priority out of range: max 255");
1495                                 YYERROR;
1496                         }
1497                         a.priority = $4.priority;
1498                         a.qlimit = $4.qlimit;
1499                         a.scheduler = $4.scheduler.qtype;
1500                         if (expand_queue(&a, $3, $5, $4.queue_bwspec,
1501                             &$4.scheduler)) {
1502                                 yyerror("errors in queue definition");
1503                                 YYERROR;
1504                         }
1505                 }
1506                 ;
1507
1508 queue_opts      :       {
1509                         bzero(&queue_opts, sizeof queue_opts);
1510                         queue_opts.priority = DEFAULT_PRIORITY;
1511                         queue_opts.qlimit = DEFAULT_QLIMIT;
1512                         queue_opts.scheduler.qtype = ALTQT_NONE;
1513                         queue_opts.queue_bwspec.bw_percent = 100;
1514                 }
1515                     queue_opts_l
1516                         { $$ = queue_opts; }
1517                 | /* empty */ {
1518                         bzero(&queue_opts, sizeof queue_opts);
1519                         queue_opts.priority = DEFAULT_PRIORITY;
1520                         queue_opts.qlimit = DEFAULT_QLIMIT;
1521                         queue_opts.scheduler.qtype = ALTQT_NONE;
1522                         queue_opts.queue_bwspec.bw_percent = 100;
1523                         $$ = queue_opts;
1524                 }
1525                 ;
1526
1527 queue_opts_l    : queue_opts_l queue_opt
1528                 | queue_opt
1529                 ;
1530
1531 queue_opt       : BANDWIDTH bandwidth   {
1532                         if (queue_opts.marker & QOM_BWSPEC) {
1533                                 yyerror("bandwidth cannot be respecified");
1534                                 YYERROR;
1535                         }
1536                         queue_opts.marker |= QOM_BWSPEC;
1537                         queue_opts.queue_bwspec = $2;
1538                 }
1539                 | PRIORITY NUMBER       {
1540                         if (queue_opts.marker & QOM_PRIORITY) {
1541                                 yyerror("priority cannot be respecified");
1542                                 YYERROR;
1543                         }
1544                         if ($2 < 0 || $2 > 255) {
1545                                 yyerror("priority out of range: max 255");
1546                                 YYERROR;
1547                         }
1548                         queue_opts.marker |= QOM_PRIORITY;
1549                         queue_opts.priority = $2;
1550                 }
1551                 | QLIMIT NUMBER {
1552                         if (queue_opts.marker & QOM_QLIMIT) {
1553                                 yyerror("qlimit cannot be respecified");
1554                                 YYERROR;
1555                         }
1556                         if ($2 < 0 || $2 > 65535) {
1557                                 yyerror("qlimit out of range: max 65535");
1558                                 YYERROR;
1559                         }
1560                         queue_opts.marker |= QOM_QLIMIT;
1561                         queue_opts.qlimit = $2;
1562                 }
1563                 | scheduler     {
1564                         if (queue_opts.marker & QOM_SCHEDULER) {
1565                                 yyerror("scheduler cannot be respecified");
1566                                 YYERROR;
1567                         }
1568                         queue_opts.marker |= QOM_SCHEDULER;
1569                         queue_opts.scheduler = $1;
1570                 }
1571                 | TBRSIZE NUMBER        {
1572                         if (queue_opts.marker & QOM_TBRSIZE) {
1573                                 yyerror("tbrsize cannot be respecified");
1574                                 YYERROR;
1575                         }
1576                         if ($2 < 0 || $2 > 65535) {
1577                                 yyerror("tbrsize too big: max 65535");
1578                                 YYERROR;
1579                         }
1580                         queue_opts.marker |= QOM_TBRSIZE;
1581                         queue_opts.tbrsize = $2;
1582                 }
1583                 ;
1584
1585 bandwidth       : STRING {
1586                         double   bps;
1587                         char    *cp;
1588
1589                         $$.bw_percent = 0;
1590
1591                         bps = strtod($1, &cp);
1592                         if (cp != NULL) {
1593                                 if (!strcmp(cp, "b"))
1594                                         ; /* nothing */
1595                                 else if (!strcmp(cp, "Kb"))
1596                                         bps *= 1000;
1597                                 else if (!strcmp(cp, "Mb"))
1598                                         bps *= 1000 * 1000;
1599                                 else if (!strcmp(cp, "Gb"))
1600                                         bps *= 1000 * 1000 * 1000;
1601                                 else if (!strcmp(cp, "%")) {
1602                                         if (bps < 0 || bps > 100) {
1603                                                 yyerror("bandwidth spec "
1604                                                     "out of range");
1605                                                 free($1);
1606                                                 YYERROR;
1607                                         }
1608                                         $$.bw_percent = bps;
1609                                         bps = 0;
1610                                 } else {
1611                                         yyerror("unknown unit %s", cp);
1612                                         free($1);
1613                                         YYERROR;
1614                                 }
1615                         }
1616                         free($1);
1617                         $$.bw_absolute = (u_int32_t)bps;
1618                 }
1619                 | NUMBER {
1620                         if ($1 < 0 || $1 > UINT_MAX) {
1621                                 yyerror("bandwidth number too big");
1622                                 YYERROR;
1623                         }
1624                         $$.bw_percent = 0;
1625                         $$.bw_absolute = $1;
1626                 }
1627                 ;
1628
1629 scheduler       : CBQ                           {
1630                         $$.qtype = ALTQT_CBQ;
1631                         $$.data.cbq_opts.flags = 0;
1632                 }
1633                 | CBQ '(' cbqflags_list ')'     {
1634                         $$.qtype = ALTQT_CBQ;
1635                         $$.data.cbq_opts.flags = $3;
1636                 }
1637                 | PRIQ                          {
1638                         $$.qtype = ALTQT_PRIQ;
1639                         $$.data.priq_opts.flags = 0;
1640                 }
1641                 | PRIQ '(' priqflags_list ')'   {
1642                         $$.qtype = ALTQT_PRIQ;
1643                         $$.data.priq_opts.flags = $3;
1644                 }
1645                 | HFSC                          {
1646                         $$.qtype = ALTQT_HFSC;
1647                         bzero(&$$.data.hfsc_opts,
1648                             sizeof(struct node_hfsc_opts));
1649                 }
1650                 | HFSC '(' hfsc_opts ')'        {
1651                         $$.qtype = ALTQT_HFSC;
1652                         $$.data.hfsc_opts = $3;
1653                 }
1654                 | FAIRQ                         {
1655                         $$.qtype = ALTQT_FAIRQ;
1656                         bzero(&$$.data.fairq_opts,
1657                             sizeof(struct node_fairq_opts));
1658                 }
1659                 | FAIRQ '(' fairq_opts ')'      {
1660                         $$.qtype = ALTQT_FAIRQ;
1661                         $$.data.fairq_opts = $3;
1662                 }
1663                 ;
1664
1665 cbqflags_list   : cbqflags_item                         { $$ |= $1; }
1666                 | cbqflags_list comma cbqflags_item     { $$ |= $3; }
1667                 ;
1668
1669 cbqflags_item   : STRING        {
1670                         if (!strcmp($1, "default"))
1671                                 $$ = CBQCLF_DEFCLASS;
1672                         else if (!strcmp($1, "borrow"))
1673                                 $$ = CBQCLF_BORROW;
1674                         else if (!strcmp($1, "red"))
1675                                 $$ = CBQCLF_RED;
1676                         else if (!strcmp($1, "ecn"))
1677                                 $$ = CBQCLF_RED|CBQCLF_ECN;
1678                         else if (!strcmp($1, "rio"))
1679                                 $$ = CBQCLF_RIO;
1680                         else {
1681                                 yyerror("unknown cbq flag \"%s\"", $1);
1682                                 free($1);
1683                                 YYERROR;
1684                         }
1685                         free($1);
1686                 }
1687                 ;
1688
1689 priqflags_list  : priqflags_item                        { $$ |= $1; }
1690                 | priqflags_list comma priqflags_item   { $$ |= $3; }
1691                 ;
1692
1693 priqflags_item  : STRING        {
1694                         if (!strcmp($1, "default"))
1695                                 $$ = PRCF_DEFAULTCLASS;
1696                         else if (!strcmp($1, "red"))
1697                                 $$ = PRCF_RED;
1698                         else if (!strcmp($1, "ecn"))
1699                                 $$ = PRCF_RED|PRCF_ECN;
1700                         else if (!strcmp($1, "rio"))
1701                                 $$ = PRCF_RIO;
1702                         else {
1703                                 yyerror("unknown priq flag \"%s\"", $1);
1704                                 free($1);
1705                                 YYERROR;
1706                         }
1707                         free($1);
1708                 }
1709                 ;
1710
1711 hfsc_opts       :       {
1712                                 bzero(&hfsc_opts,
1713                                     sizeof(struct node_hfsc_opts));
1714                         }
1715                     hfscopts_list                               {
1716                         $$ = hfsc_opts;
1717                 }
1718                 ;
1719
1720 hfscopts_list   : hfscopts_item
1721                 | hfscopts_list comma hfscopts_item
1722                 ;
1723
1724 hfscopts_item   : LINKSHARE bandwidth                           {
1725                         if (hfsc_opts.linkshare.used) {
1726                                 yyerror("linkshare already specified");
1727                                 YYERROR;
1728                         }
1729                         hfsc_opts.linkshare.m2 = $2;
1730                         hfsc_opts.linkshare.used = 1;
1731                 }
1732                 | LINKSHARE '(' bandwidth comma NUMBER comma bandwidth ')'
1733                     {
1734                         if ($5 < 0 || $5 > INT_MAX) {
1735                                 yyerror("timing in curve out of range");
1736                                 YYERROR;
1737                         }
1738                         if (hfsc_opts.linkshare.used) {
1739                                 yyerror("linkshare already specified");
1740                                 YYERROR;
1741                         }
1742                         hfsc_opts.linkshare.m1 = $3;
1743                         hfsc_opts.linkshare.d = $5;
1744                         hfsc_opts.linkshare.m2 = $7;
1745                         hfsc_opts.linkshare.used = 1;
1746                 }
1747                 | REALTIME bandwidth                            {
1748                         if (hfsc_opts.realtime.used) {
1749                                 yyerror("realtime already specified");
1750                                 YYERROR;
1751                         }
1752                         hfsc_opts.realtime.m2 = $2;
1753                         hfsc_opts.realtime.used = 1;
1754                 }
1755                 | REALTIME '(' bandwidth comma NUMBER comma bandwidth ')'
1756                     {
1757                         if ($5 < 0 || $5 > INT_MAX) {
1758                                 yyerror("timing in curve out of range");
1759                                 YYERROR;
1760                         }
1761                         if (hfsc_opts.realtime.used) {
1762                                 yyerror("realtime already specified");
1763                                 YYERROR;
1764                         }
1765                         hfsc_opts.realtime.m1 = $3;
1766                         hfsc_opts.realtime.d = $5;
1767                         hfsc_opts.realtime.m2 = $7;
1768                         hfsc_opts.realtime.used = 1;
1769                 }
1770                 | UPPERLIMIT bandwidth                          {
1771                         if (hfsc_opts.upperlimit.used) {
1772                                 yyerror("upperlimit already specified");
1773                                 YYERROR;
1774                         }
1775                         hfsc_opts.upperlimit.m2 = $2;
1776                         hfsc_opts.upperlimit.used = 1;
1777                 }
1778                 | UPPERLIMIT '(' bandwidth comma NUMBER comma bandwidth ')'
1779                     {
1780                         if ($5 < 0 || $5 > INT_MAX) {
1781                                 yyerror("timing in curve out of range");
1782                                 YYERROR;
1783                         }
1784                         if (hfsc_opts.upperlimit.used) {
1785                                 yyerror("upperlimit already specified");
1786                                 YYERROR;
1787                         }
1788                         hfsc_opts.upperlimit.m1 = $3;
1789                         hfsc_opts.upperlimit.d = $5;
1790                         hfsc_opts.upperlimit.m2 = $7;
1791                         hfsc_opts.upperlimit.used = 1;
1792                 }
1793                 | STRING        {
1794                         if (!strcmp($1, "default"))
1795                                 hfsc_opts.flags |= HFCF_DEFAULTCLASS;
1796                         else if (!strcmp($1, "red"))
1797                                 hfsc_opts.flags |= HFCF_RED;
1798                         else if (!strcmp($1, "ecn"))
1799                                 hfsc_opts.flags |= HFCF_RED|HFCF_ECN;
1800                         else if (!strcmp($1, "rio"))
1801                                 hfsc_opts.flags |= HFCF_RIO;
1802                         else {
1803                                 yyerror("unknown hfsc flag \"%s\"", $1);
1804                                 free($1);
1805                                 YYERROR;
1806                         }
1807                         free($1);
1808                 }
1809                 ;
1810
1811 fairq_opts      :       {
1812                                 bzero(&fairq_opts,
1813                                     sizeof(struct node_fairq_opts));
1814                         }
1815                     fairqopts_list                              {
1816                         $$ = fairq_opts;
1817                 }
1818                 ;
1819
1820 fairqopts_list  : fairqopts_item
1821                 | fairqopts_list comma fairqopts_item
1822                 ;
1823
1824 fairqopts_item  : LINKSHARE bandwidth                           {
1825                         if (fairq_opts.linkshare.used) {
1826                                 yyerror("linkshare already specified");
1827                                 YYERROR;
1828                         }
1829                         fairq_opts.linkshare.m2 = $2;
1830                         fairq_opts.linkshare.used = 1;
1831                 }
1832                 | LINKSHARE '(' bandwidth number bandwidth ')'  {
1833                         if (fairq_opts.linkshare.used) {
1834                                 yyerror("linkshare already specified");
1835                                 YYERROR;
1836                         }
1837                         fairq_opts.linkshare.m1 = $3;
1838                         fairq_opts.linkshare.d = $4;
1839                         fairq_opts.linkshare.m2 = $5;
1840                         fairq_opts.linkshare.used = 1;
1841                 }
1842                 | HOGS bandwidth {
1843                         fairq_opts.hogs_bw = $2;
1844                 }
1845                 | BUCKETS number {
1846                         fairq_opts.nbuckets = $2;
1847                 }
1848                 | STRING        {
1849                         if (!strcmp($1, "default"))
1850                                 fairq_opts.flags |= FARF_DEFAULTCLASS;
1851                         else if (!strcmp($1, "red"))
1852                                 fairq_opts.flags |= FARF_RED;
1853                         else if (!strcmp($1, "ecn"))
1854                                 fairq_opts.flags |= FARF_RED|FARF_ECN;
1855                         else if (!strcmp($1, "rio"))
1856                                 fairq_opts.flags |= FARF_RIO;
1857                         else {
1858                                 yyerror("unknown fairq flag \"%s\"", $1);
1859                                 free($1);
1860                                 YYERROR;
1861                         }
1862                         free($1);
1863                 }
1864                 ;
1865
1866 qassign         : /* empty */           { $$ = NULL; }
1867                 | qassign_item          { $$ = $1; }
1868                 | '{' optnl qassign_list '}'    { $$ = $3; }
1869                 ;
1870
1871 qassign_list    : qassign_item optnl            { $$ = $1; }
1872                 | qassign_list comma qassign_item optnl {
1873                         $1->tail->next = $3;
1874                         $1->tail = $3;
1875                         $$ = $1;
1876                 }
1877                 ;
1878
1879 qassign_item    : STRING                        {
1880                         $$ = calloc(1, sizeof(struct node_queue));
1881                         if ($$ == NULL)
1882                                 err(1, "qassign_item: calloc");
1883                         if (strlcpy($$->queue, $1, sizeof($$->queue)) >=
1884                             sizeof($$->queue)) {
1885                                 yyerror("queue name '%s' too long (max "
1886                                     "%zd chars)", $1, sizeof($$->queue)-1);
1887                                 free($1);
1888                                 free($$);
1889                                 YYERROR;
1890                         }
1891                         free($1);
1892                         $$->next = NULL;
1893                         $$->tail = $$;
1894                 }
1895                 ;
1896
1897 pfrule          : action dir logquick interface route af proto fromto
1898                     filter_opts
1899                 {
1900                         struct pf_rule           r;
1901                         struct node_state_opt   *o;
1902                         struct node_proto       *proto;
1903                         int                      srctrack = 0;
1904                         int                      statelock = 0;
1905                         int                      adaptive = 0;
1906                         int                      dofree;
1907
1908                         if (check_rulestate(PFCTL_STATE_FILTER))
1909                                 YYERROR;
1910
1911                         memset(&r, 0, sizeof(r));
1912
1913                         r.action = $1.b1;
1914                         switch ($1.b2) {
1915                         case PFRULE_RETURNRST:
1916                                 r.rule_flag |= PFRULE_RETURNRST;
1917                                 r.return_ttl = $1.w;
1918                                 break;
1919                         case PFRULE_RETURNICMP:
1920                                 r.rule_flag |= PFRULE_RETURNICMP;
1921                                 r.return_icmp = $1.w;
1922                                 r.return_icmp6 = $1.w2;
1923                                 break;
1924                         case PFRULE_RETURN:
1925                                 r.rule_flag |= PFRULE_RETURN;
1926                                 r.return_icmp = $1.w;
1927                                 r.return_icmp6 = $1.w2;
1928                                 break;
1929                         }
1930                         r.direction = $2;
1931                         r.log = $3.log;
1932                         r.logif = $3.logif;
1933                         r.quick = $3.quick;
1934                         r.prob = $9.prob;
1935                         r.rtableid = $9.rtableid;
1936
1937                         r.af = $6;
1938                         if ($9.tag)
1939                                 if (strlcpy(r.tagname, $9.tag,
1940                                     PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1941                                         yyerror("tag too long, max %u chars",
1942                                             PF_TAG_NAME_SIZE - 1);
1943                                         YYERROR;
1944                                 }
1945                         if ($9.match_tag)
1946                                 if (strlcpy(r.match_tagname, $9.match_tag,
1947                                     PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1948                                         yyerror("tag too long, max %u chars",
1949                                             PF_TAG_NAME_SIZE - 1);
1950                                         YYERROR;
1951                                 }
1952                         r.match_tag_not = $9.match_tag_not;
1953                         if (rule_label(&r, $9.label))
1954                                 YYERROR;
1955                         free($9.label);
1956                         r.flags = $9.flags.b1;
1957                         r.flagset = $9.flags.b2;
1958                         if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
1959                                 yyerror("flags always false");
1960                                 YYERROR;
1961                         }
1962                         if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
1963                                 for (proto = $7; proto != NULL &&
1964                                     proto->proto != IPPROTO_TCP;
1965                                     proto = proto->next)
1966                                         ;       /* nothing */
1967                                 if (proto == NULL && $7 != NULL) {
1968                                         if ($9.flags.b1 || $9.flags.b2)
1969                                                 yyerror(
1970                                                     "flags only apply to tcp");
1971                                         if ($8.src_os)
1972                                                 yyerror(
1973                                                     "OS fingerprinting only "
1974                                                     "apply to tcp");
1975                                         YYERROR;
1976                                 }
1977 #if 0
1978                                 if (($9.flags.b1 & parse_flags("S")) == 0 &&
1979                                     $8.src_os) {
1980                                         yyerror("OS fingerprinting requires "
1981                                             "the SYN TCP flag (flags S/SA)");
1982                                         YYERROR;
1983                                 }
1984 #endif
1985                         }
1986
1987                         r.tos = $9.tos;
1988
1989                         if (filter_opts.marker & FOM_KEEP) {
1990                                 r.keep_state = $9.keep.action;
1991                                 o = $9.keep.options;
1992                                 dofree = 1;
1993                         } else if (r.action == PF_PASS) {
1994                                 r.keep_state = default_keeppolicy_action;
1995                                 o = default_keeppolicy_options;
1996                                 dofree = 0;
1997                         } else {
1998                                 o = NULL;
1999                                 dofree = 0;
2000                         }
2001                         while (o) {
2002                                 struct node_state_opt   *p = o;
2003
2004                                 switch (o->type) {
2005                                 case PF_STATE_OPT_MAX:
2006                                         if (r.max_states) {
2007                                                 yyerror("state option 'max' "
2008                                                     "multiple definitions");
2009                                                 YYERROR;
2010                                         }
2011                                         r.max_states = o->data.max_states;
2012                                         break;
2013                                 case PF_STATE_OPT_NOSYNC:
2014                                         if (r.rule_flag & PFRULE_NOSYNC) {
2015                                                 yyerror("state option 'sync' "
2016                                                     "multiple definitions");
2017                                                 YYERROR;
2018                                         }
2019                                         r.rule_flag |= PFRULE_NOSYNC;
2020                                         break;
2021                                 case PF_STATE_OPT_SRCTRACK:
2022                                         if (srctrack) {
2023                                                 yyerror("state option "
2024                                                     "'source-track' "
2025                                                     "multiple definitions");
2026                                                 YYERROR;
2027                                         }
2028                                         srctrack =  o->data.src_track;
2029                                         r.rule_flag |= PFRULE_SRCTRACK;
2030                                         break;
2031                                 case PF_STATE_OPT_MAX_SRC_STATES:
2032                                         if (r.max_src_states) {
2033                                                 yyerror("state option "
2034                                                     "'max-src-states' "
2035                                                     "multiple definitions");
2036                                                 YYERROR;
2037                                         }
2038                                         if (o->data.max_src_states == 0) {
2039                                                 yyerror("'max-src-states' must "
2040                                                     "be > 0");
2041                                                 YYERROR;
2042                                         }
2043                                         r.max_src_states =
2044                                             o->data.max_src_states;
2045                                         r.rule_flag |= PFRULE_SRCTRACK;
2046                                         break;
2047                                 case PF_STATE_OPT_OVERLOAD:
2048                                         if (r.overload_tblname[0]) {
2049                                                 yyerror("multiple 'overload' "
2050                                                     "table definitions");
2051                                                 YYERROR;
2052                                         }
2053                                         if (strlcpy(r.overload_tblname,
2054                                             o->data.overload.tblname,
2055                                             PF_TABLE_NAME_SIZE) >=
2056                                             PF_TABLE_NAME_SIZE) {
2057                                                 yyerror("state option: "
2058                                                     "strlcpy");
2059                                                 YYERROR;
2060                                         }
2061                                         r.flush = o->data.overload.flush;
2062                                         break;
2063                                 case PF_STATE_OPT_MAX_SRC_CONN:
2064                                         if (r.max_src_conn) {
2065                                                 yyerror("state option "
2066                                                     "'max-src-conn' "
2067                                                     "multiple definitions");
2068                                                 YYERROR;
2069                                         }
2070                                         if (o->data.max_src_conn == 0) {
2071                                                 yyerror("'max-src-conn' "
2072                                                     "must be > 0");
2073                                                 YYERROR;
2074                                         }
2075                                         r.max_src_conn =
2076                                             o->data.max_src_conn;
2077                                         r.rule_flag |= PFRULE_SRCTRACK |
2078                                             PFRULE_RULESRCTRACK;
2079                                         break;
2080                                 case PF_STATE_OPT_MAX_SRC_CONN_RATE:
2081                                         if (r.max_src_conn_rate.limit) {
2082                                                 yyerror("state option "
2083                                                     "'max-src-conn-rate' "
2084                                                     "multiple definitions");
2085                                                 YYERROR;
2086                                         }
2087                                         if (!o->data.max_src_conn_rate.limit ||
2088                                             !o->data.max_src_conn_rate.seconds) {
2089                                                 yyerror("'max-src-conn-rate' "
2090                                                     "values must be > 0");
2091                                                 YYERROR;
2092                                         }
2093                                         if (o->data.max_src_conn_rate.limit >
2094                                             PF_THRESHOLD_MAX) {
2095                                                 yyerror("'max-src-conn-rate' "
2096                                                     "maximum rate must be < %u",
2097                                                     PF_THRESHOLD_MAX);
2098                                                 YYERROR;
2099                                         }
2100                                         r.max_src_conn_rate.limit =
2101                                             o->data.max_src_conn_rate.limit;
2102                                         r.max_src_conn_rate.seconds =
2103                                             o->data.max_src_conn_rate.seconds;
2104                                         r.rule_flag |= PFRULE_SRCTRACK |
2105                                             PFRULE_RULESRCTRACK;
2106                                         break;
2107                                 case PF_STATE_OPT_MAX_SRC_NODES:
2108                                         if (r.max_src_nodes) {
2109                                                 yyerror("state option "
2110                                                     "'max-src-nodes' "
2111                                                     "multiple definitions");
2112                                                 YYERROR;
2113                                         }
2114                                         if (o->data.max_src_nodes == 0) {
2115                                                 yyerror("'max-src-nodes' must "
2116                                                     "be > 0");
2117                                                 YYERROR;
2118                                         }
2119                                         r.max_src_nodes =
2120                                             o->data.max_src_nodes;
2121                                         r.rule_flag |= PFRULE_SRCTRACK |
2122                                             PFRULE_RULESRCTRACK;
2123                                         break;
2124                                 case PF_STATE_OPT_STATELOCK:
2125                                         if (statelock) {
2126                                                 yyerror("state locking option: "
2127                                                     "multiple definitions");
2128                                                 YYERROR;
2129                                         }
2130                                         statelock = 1;
2131                                         r.rule_flag |= o->data.statelock;
2132                                         break;
2133                                 case PF_STATE_OPT_SLOPPY:
2134                                         if (r.rule_flag & PFRULE_STATESLOPPY) {
2135                                                 yyerror("state sloppy option: "
2136                                                     "multiple definitions");
2137                                                 YYERROR;
2138                                         }
2139                                         r.rule_flag |= PFRULE_STATESLOPPY;
2140                                         break;
2141                                 case PF_STATE_OPT_TIMEOUT:
2142                                         if (o->data.timeout.number ==
2143                                             PFTM_ADAPTIVE_START ||
2144                                             o->data.timeout.number ==
2145                                             PFTM_ADAPTIVE_END)
2146                                                 adaptive = 1;
2147                                         if (r.timeout[o->data.timeout.number]) {
2148                                                 yyerror("state timeout %s "
2149                                                     "multiple definitions",
2150                                                     pf_timeouts[o->data.
2151                                                     timeout.number].name);
2152                                                 YYERROR;
2153                                         }
2154                                         r.timeout[o->data.timeout.number] =
2155                                             o->data.timeout.seconds;
2156                                         break;
2157                                 case PF_STATE_OPT_PICKUPS:
2158                                         r.pickup_mode = o->data.pickup_mode;
2159                                         break;
2160                                 }
2161                                 o = o->next;
2162                                 if (dofree)
2163                                         free(p);
2164                         }
2165
2166 #if 0
2167                         /* 'flags S/SA' by default on stateful rules */
2168                         if (!r.action && !r.flags && !r.flagset &&
2169                             !$9.fragment && !($9.marker & FOM_FLAGS) &&
2170                             r.keep_state) {
2171                                 r.flags = parse_flags("S");
2172                                 r.flagset =  parse_flags("SA");
2173                         }
2174 #endif
2175                         if (!adaptive && r.max_states) {
2176                                 r.timeout[PFTM_ADAPTIVE_START] =
2177                                     (r.max_states / 10) * 6;
2178                                 r.timeout[PFTM_ADAPTIVE_END] =
2179                                     (r.max_states / 10) * 12;
2180                         }
2181                         if (r.rule_flag & PFRULE_SRCTRACK) {
2182                                 if (srctrack == PF_SRCTRACK_GLOBAL &&
2183                                     r.max_src_nodes) {
2184                                         yyerror("'max-src-nodes' is "
2185                                             "incompatible with "
2186                                             "'source-track global'");
2187                                         YYERROR;
2188                                 }
2189                                 if (srctrack == PF_SRCTRACK_GLOBAL &&
2190                                     r.max_src_conn) {
2191                                         yyerror("'max-src-conn' is "
2192                                             "incompatible with "
2193                                             "'source-track global'");
2194                                         YYERROR;
2195                                 }
2196                                 if (srctrack == PF_SRCTRACK_GLOBAL &&
2197                                     r.max_src_conn_rate.seconds) {
2198                                         yyerror("'max-src-conn-rate' is "
2199                                             "incompatible with "
2200                                             "'source-track global'");
2201                                         YYERROR;
2202                                 }
2203                                 if (r.timeout[PFTM_SRC_NODE] <
2204                                     r.max_src_conn_rate.seconds)
2205                                         r.timeout[PFTM_SRC_NODE] =
2206                                             r.max_src_conn_rate.seconds;
2207                                 r.rule_flag |= PFRULE_SRCTRACK;
2208                                 if (srctrack == PF_SRCTRACK_RULE)
2209                                         r.rule_flag |= PFRULE_RULESRCTRACK;
2210                         }
2211                         if (r.keep_state && !statelock)
2212                                 r.rule_flag |= default_statelock;
2213
2214                         if ($9.fragment)
2215                                 r.rule_flag |= PFRULE_FRAGMENT;
2216                         r.allow_opts = $9.allowopts;
2217
2218                         decide_address_family($8.src.host, &r.af);
2219                         decide_address_family($8.dst.host, &r.af);
2220
2221                         if ($5.rt) {
2222                                 if (!r.direction) {
2223                                         yyerror("direction must be explicit "
2224                                             "with rules that specify routing");
2225                                         YYERROR;
2226                                 }
2227                                 r.rt = $5.rt;
2228                                 r.rpool.opts = $5.pool_opts;
2229                                 if ($5.key != NULL)
2230                                         memcpy(&r.rpool.key, $5.key,
2231                                             sizeof(struct pf_poolhashkey));
2232                         }
2233                         if (r.rt && r.rt != PF_FASTROUTE) {
2234                                 decide_address_family($5.host, &r.af);
2235                                 remove_invalid_hosts(&$5.host, &r.af);
2236                                 if ($5.host == NULL) {
2237                                         yyerror("no routing address with "
2238                                             "matching address family found.");
2239                                         YYERROR;
2240                                 }
2241                                 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
2242                                     PF_POOL_NONE && ($5.host->next != NULL ||
2243                                     $5.host->addr.type == PF_ADDR_TABLE ||
2244                                     DYNIF_MULTIADDR($5.host->addr)))
2245                                         r.rpool.opts |= PF_POOL_ROUNDROBIN;
2246                                 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2247                                     PF_POOL_ROUNDROBIN &&
2248                                     disallow_table($5.host, "tables are only "
2249                                     "supported in round-robin routing pools"))
2250                                         YYERROR;
2251                                 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2252                                     PF_POOL_ROUNDROBIN &&
2253                                     disallow_alias($5.host, "interface (%s) "
2254                                     "is only supported in round-robin "
2255                                     "routing pools"))
2256                                         YYERROR;
2257                                 if ($5.host->next != NULL) {
2258                                         if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
2259                                             PF_POOL_ROUNDROBIN) {
2260                                                 yyerror("r.rpool.opts must "
2261                                                     "be PF_POOL_ROUNDROBIN");
2262                                                 YYERROR;
2263                                         }
2264                                 }
2265                         }
2266                         if ($9.queues.qname != NULL) {
2267                                 if (strlcpy(r.qname, $9.queues.qname,
2268                                     sizeof(r.qname)) >= sizeof(r.qname)) {
2269                                         yyerror("rule qname too long (max "
2270                                             "%zd chars)", sizeof(r.qname)-1);
2271                                         YYERROR;
2272                                 }
2273                                 free($9.queues.qname);
2274                         }
2275                         if ($9.queues.pqname != NULL) {
2276                                 if (strlcpy(r.pqname, $9.queues.pqname,
2277                                     sizeof(r.pqname)) >= sizeof(r.pqname)) {
2278                                         yyerror("rule pqname too long (max "
2279                                             "%zd chars)", sizeof(r.pqname)-1);
2280                                         YYERROR;
2281                                 }
2282                                 free($9.queues.pqname);
2283                         }
2284                         if ((r.divert.port = $9.divert.port)) {
2285                                 if (r.direction == PF_OUT) {
2286                                         if ($9.divert.addr) {
2287                                                 yyerror("address specified "
2288                                                     "for outgoing divert");
2289                                                 YYERROR;
2290                                         }
2291                                         bzero(&r.divert.addr,
2292                                             sizeof(r.divert.addr));
2293                                 } else {
2294                                         if (!$9.divert.addr) {
2295                                                 yyerror("no address specified "
2296                                                     "for incoming divert");
2297                                                 YYERROR;
2298                                         }
2299                                         if ($9.divert.addr->af != r.af) {
2300                                                 yyerror("address family "
2301                                                     "mismatch for divert");
2302                                                 YYERROR;
2303                                         }
2304                                         r.divert.addr =
2305                                             $9.divert.addr->addr.v.a.addr;
2306                                 }
2307                         }       
2308
2309                         expand_rule(&r, $4, $5.host, $7, $8.src_os,
2310                             $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
2311                             $9.uid, $9.gid, $9.icmpspec, "");
2312                 }
2313                 ;
2314
2315 filter_opts     :       {
2316                                 bzero(&filter_opts, sizeof filter_opts);
2317                                 filter_opts.rtableid = -1;
2318                         }
2319                     filter_opts_l
2320                         { $$ = filter_opts; }
2321                 | /* empty */   {
2322                         bzero(&filter_opts, sizeof filter_opts);
2323                         filter_opts.rtableid = -1;
2324                         $$ = filter_opts;
2325                 }
2326                 ;
2327
2328 filter_opts_l   : filter_opts_l filter_opt
2329                 | filter_opt
2330                 ;
2331
2332 filter_opt      : USER uids {
2333                         if (filter_opts.uid)
2334                                 $2->tail->next = filter_opts.uid;
2335                         filter_opts.uid = $2;
2336                 }
2337                 | GROUP gids {
2338                         if (filter_opts.gid)
2339                                 $2->tail->next = filter_opts.gid;
2340                         filter_opts.gid = $2;
2341                 }
2342                 | flags {
2343                         if (filter_opts.marker & FOM_FLAGS) {
2344                                 yyerror("flags cannot be redefined");
2345                                 YYERROR;
2346                         }
2347                         filter_opts.marker |= FOM_FLAGS;
2348                         filter_opts.flags.b1 |= $1.b1;
2349                         filter_opts.flags.b2 |= $1.b2;
2350                         filter_opts.flags.w |= $1.w;
2351                         filter_opts.flags.w2 |= $1.w2;
2352                 }
2353                 | icmpspec {
2354                         if (filter_opts.marker & FOM_ICMP) {
2355                                 yyerror("icmp-type cannot be redefined");
2356                                 YYERROR;
2357                         }
2358                         filter_opts.marker |= FOM_ICMP;
2359                         filter_opts.icmpspec = $1;
2360                 }
2361                 | TOS tos {
2362                         if (filter_opts.marker & FOM_TOS) {
2363                                 yyerror("tos cannot be redefined");
2364                                 YYERROR;
2365                         }
2366                         filter_opts.marker |= FOM_TOS;
2367                         filter_opts.tos = $2;
2368                 }
2369                 | keep {
2370                         if (filter_opts.marker & FOM_KEEP) {
2371                                 yyerror("modulate or keep cannot be redefined");
2372                                 YYERROR;
2373                         }
2374                         filter_opts.marker |= FOM_KEEP;
2375                         filter_opts.keep.action = $1.action;
2376                         filter_opts.keep.options = $1.options;
2377                 }
2378                 | FRAGMENT {
2379                         filter_opts.fragment = 1;
2380                 }
2381                 | ALLOWOPTS {
2382                         filter_opts.allowopts = 1;
2383                 }
2384                 | label {
2385                         if (filter_opts.label) {
2386                                 yyerror("label cannot be redefined");
2387                                 YYERROR;
2388                         }
2389                         filter_opts.label = $1;
2390                 }
2391                 | qname {
2392                         if (filter_opts.queues.qname) {
2393                                 yyerror("queue cannot be redefined");
2394                                 YYERROR;
2395                         }
2396                         filter_opts.queues = $1;
2397                 }
2398                 | TAG string                            {
2399                         filter_opts.tag = $2;
2400                 }
2401                 | not TAGGED string                     {
2402                         filter_opts.match_tag = $3;
2403                         filter_opts.match_tag_not = $1;
2404                 }
2405                 | PROBABILITY probability               {
2406                         double  p;
2407
2408                         p = floor($2 * UINT_MAX + 0.5);
2409                         if (p < 0.0 || p > UINT_MAX) {
2410                                 yyerror("invalid probability: %lf", p);
2411                                 YYERROR;
2412                         }
2413                         filter_opts.prob = (u_int32_t)p;
2414                         if (filter_opts.prob == 0)
2415                                 filter_opts.prob = 1;
2416                 }
2417                 | RTABLE NUMBER                         {
2418                         filter_opts.rtableid = $2;
2419                 }
2420                 | DIVERTTO STRING PORT portplain {
2421                         if ((filter_opts.divert.addr = host($2)) == NULL) {
2422                                 yyerror("could not parse divert address: %s",
2423                                     $2);
2424                                 free($2);
2425                                 YYERROR;
2426                         }
2427                         free($2);
2428                         filter_opts.divert.port = $4.a;
2429                         if (!filter_opts.divert.port) {
2430                                 yyerror("invalid divert port: %u", ntohs($4.a));
2431                                 YYERROR;
2432                         }
2433                 }
2434                 | DIVERTREPLY {
2435                         filter_opts.divert.port = 1;    /* some random value */
2436                 }
2437                 ;
2438
2439 probability     : STRING                                {
2440                         char    *e;
2441                         double   p = strtod($1, &e);
2442
2443                         if (*e == '%') {
2444                                 p *= 0.01;
2445                                 e++;
2446                         }
2447                         if (*e) {
2448                                 yyerror("invalid probability: %s", $1);
2449                                 free($1);
2450                                 YYERROR;
2451                         }
2452                         free($1);
2453                         $$ = p;
2454                 }
2455                 | NUMBER                                {
2456                         $$ = (double)$1;
2457                 }
2458                 ;
2459
2460
2461 action          : PASS                  { $$.b1 = PF_PASS; $$.b2 = $$.w = 0; }
2462                 | BLOCK blockspec       { $$ = $2; $$.b1 = PF_DROP; }
2463                 ;
2464
2465 blockspec       : /* empty */           {
2466                         $$.b2 = blockpolicy;
2467                         $$.w = returnicmpdefault;
2468                         $$.w2 = returnicmp6default;
2469                 }
2470                 | DROP                  {
2471                         $$.b2 = PFRULE_DROP;
2472                         $$.w = 0;
2473                         $$.w2 = 0;
2474                 }
2475                 | RETURNRST             {
2476                         $$.b2 = PFRULE_RETURNRST;
2477                         $$.w = 0;
2478                         $$.w2 = 0;
2479                 }
2480                 | RETURNRST '(' TTL NUMBER ')'  {
2481                         if ($4 < 0 || $4 > 255) {
2482                                 yyerror("illegal ttl value %" PRId64, $4);
2483                                 YYERROR;
2484                         }
2485                         $$.b2 = PFRULE_RETURNRST;
2486                         $$.w = $4;
2487                         $$.w2 = 0;
2488                 }
2489                 | RETURNICMP            {
2490                         $$.b2 = PFRULE_RETURNICMP;
2491                         $$.w = returnicmpdefault;
2492                         $$.w2 = returnicmp6default;
2493                 }
2494                 | RETURNICMP6           {
2495                         $$.b2 = PFRULE_RETURNICMP;
2496                         $$.w = returnicmpdefault;
2497                         $$.w2 = returnicmp6default;
2498                 }
2499                 | RETURNICMP '(' reticmpspec ')'        {
2500                         $$.b2 = PFRULE_RETURNICMP;
2501                         $$.w = $3;
2502                         $$.w2 = returnicmpdefault;
2503                 }
2504                 | RETURNICMP6 '(' reticmp6spec ')'      {
2505                         $$.b2 = PFRULE_RETURNICMP;
2506                         $$.w = returnicmpdefault;
2507                         $$.w2 = $3;
2508                 }
2509                 | RETURNICMP '(' reticmpspec comma reticmp6spec ')' {
2510                         $$.b2 = PFRULE_RETURNICMP;
2511                         $$.w = $3;
2512                         $$.w2 = $5;
2513                 }
2514                 | RETURN {
2515                         $$.b2 = PFRULE_RETURN;
2516                         $$.w = returnicmpdefault;
2517                         $$.w2 = returnicmp6default;
2518                 }
2519                 ;
2520
2521 reticmpspec     : STRING                        {
2522                         if (!($$ = parseicmpspec($1, AF_INET))) {
2523                                 free($1);
2524                                 YYERROR;
2525                         }
2526                         free($1);
2527                 }
2528                 | NUMBER                        {
2529                         u_int8_t                icmptype;
2530
2531                         if ($1 < 0 || $1 > 255) {
2532                                 yyerror("invalid icmp code %" PRId64, $1);
2533                                 YYERROR;
2534                         }
2535                         icmptype = returnicmpdefault >> 8;
2536                         $$ = (icmptype << 8 | $1);
2537                 }
2538                 ;
2539
2540 reticmp6spec    : STRING                        {
2541                         if (!($$ = parseicmpspec($1, AF_INET6))) {
2542                                 free($1);
2543                                 YYERROR;
2544                         }
2545                         free($1);
2546                 }
2547                 | NUMBER                        {
2548                         u_int8_t                icmptype;
2549
2550                         if ($1 < 0 || $1 > 255) {
2551                                 yyerror("invalid icmp code %" PRId64, $1);
2552                                 YYERROR;
2553                         }
2554                         icmptype = returnicmp6default >> 8;
2555                         $$ = (icmptype << 8 | $1);
2556                 }
2557                 ;
2558
2559 dir             : /* empty */                   { $$ = PF_INOUT; }
2560                 | IN                            { $$ = PF_IN; }
2561                 | OUT                           { $$ = PF_OUT; }
2562                 ;
2563
2564 quick           : /* empty */                   { $$.quick = 0; }
2565                 | QUICK                         { $$.quick = 1; }
2566                 ;
2567
2568 logquick        : /* empty */   { $$.log = 0; $$.quick = 0; $$.logif = 0; }
2569                 | log           { $$ = $1; $$.quick = 0; }
2570                 | QUICK         { $$.quick = 1; $$.log = 0; $$.logif = 0; }
2571                 | log QUICK     { $$ = $1; $$.quick = 1; }
2572                 | QUICK log     { $$ = $2; $$.quick = 1; }
2573                 ;
2574
2575 log             : LOG                   { $$.log = PF_LOG; $$.logif = 0; }
2576                 | LOG '(' logopts ')'   {
2577                         $$.log = PF_LOG | $3.log;
2578                         $$.logif = $3.logif;
2579                 }
2580                 ;
2581
2582 logopts         : logopt                        { $$ = $1; }
2583                 | logopts comma logopt          {
2584                         $$.log = $1.log | $3.log;
2585                         $$.logif = $3.logif;
2586                         if ($$.logif == 0)
2587                                 $$.logif = $1.logif;
2588                 }
2589                 ;
2590
2591 logopt          : ALL           { $$.log = PF_LOG_ALL; $$.logif = 0; }
2592                 | USER          { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
2593                 | GROUP         { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
2594                 | TO string     {
2595                         const char      *errstr;
2596                         u_int            i;
2597
2598                         $$.log = 0;
2599                         if (strncmp($2, "pflog", 5)) {
2600                                 yyerror("%s: should be a pflog interface", $2);
2601                                 free($2);
2602                                 YYERROR;
2603                         }
2604                         i = strtonum($2 + 5, 0, 255, &errstr);
2605                         if (errstr) {
2606                                 yyerror("%s: %s", $2, errstr);
2607                                 free($2);
2608                                 YYERROR;
2609                         }
2610                         free($2);
2611                         $$.logif = i;
2612                 }
2613                 ;
2614
2615 interface       : /* empty */                   { $$ = NULL; }
2616                 | ON if_item_not                { $$ = $2; }
2617                 | ON '{' optnl if_list '}'      { $$ = $4; }
2618                 ;
2619
2620 if_list         : if_item_not optnl             { $$ = $1; }
2621                 | if_list comma if_item_not optnl       {
2622                         $1->tail->next = $3;
2623                         $1->tail = $3;
2624                         $$ = $1;
2625                 }
2626                 ;
2627
2628 if_item_not     : not if_item                   { $$ = $2; $$->not = $1; }
2629                 ;
2630
2631 if_item         : STRING                        {
2632                         struct node_host        *n;
2633
2634                         $$ = calloc(1, sizeof(struct node_if));
2635                         if ($$ == NULL)
2636                                 err(1, "if_item: calloc");
2637                         if (strlcpy($$->ifname, $1, sizeof($$->ifname)) >=
2638                             sizeof($$->ifname)) {
2639                                 free($1);
2640                                 free($$);
2641                                 yyerror("interface name too long");
2642                                 YYERROR;
2643                         }
2644
2645                         if ((n = ifa_exists($1)) != NULL)
2646                                 $$->ifa_flags = n->ifa_flags;
2647
2648                         free($1);
2649                         $$->not = 0;
2650                         $$->next = NULL;
2651                         $$->tail = $$;
2652                 }
2653                 ;
2654
2655 af              : /* empty */                   { $$ = 0; }
2656                 | INET                          { $$ = AF_INET; }
2657                 | INET6                         { $$ = AF_INET6; }
2658                 ;
2659
2660 proto           : /* empty */                           { $$ = NULL; }
2661                 | PROTO proto_item                      { $$ = $2; }
2662                 | PROTO '{' optnl proto_list '}'        { $$ = $4; }
2663                 ;
2664
2665 proto_list      : proto_item optnl              { $$ = $1; }
2666                 | proto_list comma proto_item optnl     {
2667                         $1->tail->next = $3;
2668                         $1->tail = $3;
2669                         $$ = $1;
2670                 }
2671                 ;
2672
2673 proto_item      : protoval                      {
2674                         u_int8_t        pr;
2675
2676                         pr = (u_int8_t)$1;
2677                         if (pr == 0) {
2678                                 yyerror("proto 0 cannot be used");
2679                                 YYERROR;
2680                         }
2681                         $$ = calloc(1, sizeof(struct node_proto));
2682                         if ($$ == NULL)
2683                                 err(1, "proto_item: calloc");
2684                         $$->proto = pr;
2685                         $$->next = NULL;
2686                         $$->tail = $$;
2687                 }
2688                 ;
2689
2690 protoval        : STRING                        {
2691                         struct protoent *p;
2692
2693                         p = getprotobyname($1);
2694                         if (p == NULL) {
2695                                 yyerror("unknown protocol %s", $1);
2696                                 free($1);
2697                                 YYERROR;
2698                         }
2699                         $$ = p->p_proto;
2700                         free($1);
2701                 }
2702                 | NUMBER                        {
2703                         if ($1 < 0 || $1 > 255) {
2704                                 yyerror("protocol outside range");
2705                                 YYERROR;
2706                         }
2707                 }
2708                 ;
2709
2710 fromto          : ALL                           {
2711                         $$.src.host = NULL;
2712                         $$.src.port = NULL;
2713                         $$.dst.host = NULL;
2714                         $$.dst.port = NULL;
2715                         $$.src_os = NULL;
2716                 }
2717                 | from os to                    {
2718                         $$.src = $1;
2719                         $$.src_os = $2;
2720                         $$.dst = $3;
2721                 }
2722                 ;
2723
2724 os              : /* empty */                   { $$ = NULL; }
2725                 | OS xos                        { $$ = $2; }
2726                 | OS '{' optnl os_list '}'      { $$ = $4; }
2727                 ;
2728
2729 xos             : STRING {
2730                         $$ = calloc(1, sizeof(struct node_os));
2731                         if ($$ == NULL)
2732                                 err(1, "os: calloc");
2733                         $$->os = $1;
2734                         $$->tail = $$;
2735                 }
2736                 ;
2737
2738 os_list         : xos optnl                     { $$ = $1; }
2739                 | os_list comma xos optnl       {
2740                         $1->tail->next = $3;
2741                         $1->tail = $3;
2742                         $$ = $1;
2743                 }
2744                 ;
2745
2746 from            : /* empty */                   {
2747                         $$.host = NULL;
2748                         $$.port = NULL;
2749                 }
2750                 | FROM ipportspec               {
2751                         $$ = $2;
2752                 }
2753                 ;
2754
2755 to              : /* empty */                   {
2756                         $$.host = NULL;
2757                         $$.port = NULL;
2758                 }
2759                 | TO ipportspec         {
2760                         if (disallow_urpf_failed($2.host, "\"urpf-failed\" is "
2761                             "not permitted in a destination address"))
2762                                 YYERROR;
2763                         $$ = $2;
2764                 }
2765                 ;
2766
2767 ipportspec      : ipspec                        {
2768                         $$.host = $1;
2769                         $$.port = NULL;
2770                 }
2771                 | ipspec PORT portspec          {
2772                         $$.host = $1;
2773                         $$.port = $3;
2774                 }
2775                 | PORT portspec                 {
2776                         $$.host = NULL;
2777                         $$.port = $2;
2778                 }
2779                 ;
2780
2781 optnl           : '\n' optnl
2782                 |
2783                 ;
2784
2785 ipspec          : ANY                           { $$ = NULL; }
2786                 | xhost                         { $$ = $1; }
2787                 | '{' optnl host_list '}'       { $$ = $3; }
2788                 ;
2789
2790 toipspec        : TO ipspec                     { $$ = $2; }
2791                 | /* empty */                   { $$ = NULL; }
2792                 ;
2793
2794 host_list       : ipspec optnl                  { $$ = $1; }
2795                 | host_list comma ipspec optnl  {
2796                         if ($3 == NULL)
2797                                 $$ = $1;
2798                         else if ($1 == NULL)
2799                                 $$ = $3;
2800                         else {
2801                                 $1->tail->next = $3;
2802                                 $1->tail = $3->tail;
2803                                 $$ = $1;
2804                         }
2805                 }
2806                 ;
2807
2808 xhost           : not host                      {
2809                         struct node_host        *n;
2810
2811                         for (n = $2; n != NULL; n = n->next)
2812                                 n->not = $1;
2813                         $$ = $2;
2814                 }
2815                 | not NOROUTE                   {
2816                         $$ = calloc(1, sizeof(struct node_host));
2817                         if ($$ == NULL)
2818                                 err(1, "xhost: calloc");
2819                         $$->addr.type = PF_ADDR_NOROUTE;
2820                         $$->next = NULL;
2821                         $$->not = $1;
2822                         $$->tail = $$;
2823                 }
2824                 | not URPFFAILED                {
2825                         $$ = calloc(1, sizeof(struct node_host));
2826                         if ($$ == NULL)
2827                                 err(1, "xhost: calloc");
2828                         $$->addr.type = PF_ADDR_URPFFAILED;
2829                         $$->next = NULL;
2830                         $$->not = $1;
2831                         $$->tail = $$;
2832                 }
2833                 ;
2834
2835 host            : STRING                        {
2836                         if (($$ = host($1)) == NULL)    {
2837                                 /* error. "any" is handled elsewhere */
2838                                 free($1);
2839                                 yyerror("could not parse host specification");
2840                                 YYERROR;
2841                         }
2842                         free($1);
2843
2844                 }
2845                 | STRING '-' STRING             {
2846                         struct node_host *b, *e;
2847
2848                         if ((b = host($1)) == NULL || (e = host($3)) == NULL) {
2849                                 free($1);
2850                                 free($3);
2851                                 yyerror("could not parse host specification");
2852                                 YYERROR;
2853                         }
2854                         if (b->af != e->af ||
2855                             b->addr.type != PF_ADDR_ADDRMASK ||
2856                             e->addr.type != PF_ADDR_ADDRMASK ||
2857                             unmask(&b->addr.v.a.mask, b->af) !=
2858                             (b->af == AF_INET ? 32 : 128) ||
2859                             unmask(&e->addr.v.a.mask, e->af) !=
2860                             (e->af == AF_INET ? 32 : 128) ||
2861                             b->next != NULL || b->not ||
2862                             e->next != NULL || e->not) {
2863                                 free(b);
2864                                 free(e);
2865                                 free($1);
2866                                 free($3);
2867                                 yyerror("invalid address range");
2868                                 YYERROR;
2869                         }
2870                         memcpy(&b->addr.v.a.mask, &e->addr.v.a.addr,
2871                             sizeof(b->addr.v.a.mask));
2872                         b->addr.type = PF_ADDR_RANGE;
2873                         $$ = b;
2874                         free(e);
2875                         free($1);
2876                         free($3);
2877                 }
2878                 | STRING '/' NUMBER             {
2879                         char    *buf;
2880
2881                         if (asprintf(&buf, "%s/%" PRId64, $1, $3) == -1)
2882                                 err(1, "host: asprintf");
2883                         free($1);
2884                         if (($$ = host(buf)) == NULL)   {
2885                                 /* error. "any" is handled elsewhere */
2886                                 free(buf);
2887                                 yyerror("could not parse host specification");
2888                                 YYERROR;
2889                         }
2890                         free(buf);
2891                 }
2892                 | NUMBER '/' NUMBER             {
2893                         char    *buf;
2894
2895                         /* ie. for 10/8 parsing */
2896                         if (asprintf(&buf, "%" PRId64 "/%" PRId64, $1, $3) == -1)
2897                                 err(1, "host: asprintf");
2898                         if (($$ = host(buf)) == NULL)   {
2899                                 /* error. "any" is handled elsewhere */
2900                                 free(buf);
2901                                 yyerror("could not parse host specification");
2902                                 YYERROR;
2903                         }
2904                         free(buf);
2905                 }
2906                 | dynaddr
2907                 | dynaddr '/' NUMBER            {
2908                         struct node_host        *n;
2909
2910                         if ($3 < 0 || $3 > 128) {
2911                                 yyerror("bit number too big");
2912                                 YYERROR;
2913                         }
2914                         $$ = $1;
2915                         for (n = $1; n != NULL; n = n->next)
2916                                 set_ipmask(n, $3);
2917                 }
2918                 | '<' STRING '>'        {
2919                         if (strlen($2) >= PF_TABLE_NAME_SIZE) {
2920                                 yyerror("table name '%s' too long", $2);
2921                                 free($2);
2922                                 YYERROR;
2923                         }
2924                         $$ = calloc(1, sizeof(struct node_host));
2925                         if ($$ == NULL)
2926                                 err(1, "host: calloc");
2927                         $$->addr.type = PF_ADDR_TABLE;
2928                         if (strlcpy($$->addr.v.tblname, $2,
2929                             sizeof($$->addr.v.tblname)) >=
2930                             sizeof($$->addr.v.tblname))
2931                                 errx(1, "host: strlcpy");
2932                         free($2);
2933                         $$->next = NULL;
2934                         $$->tail = $$;
2935                 }
2936                 | ROUTE STRING          {
2937                         $$ = calloc(1, sizeof(struct node_host));
2938                         if ($$ == NULL) {
2939                                 free($2);
2940                                 err(1, "host: calloc");
2941                         }
2942                         $$->addr.type = PF_ADDR_RTLABEL;
2943                         if (strlcpy($$->addr.v.rtlabelname, $2,
2944                             sizeof($$->addr.v.rtlabelname)) >=
2945                             sizeof($$->addr.v.rtlabelname)) {
2946                                 yyerror("route label too long, max %zd chars",
2947                                     sizeof($$->addr.v.rtlabelname) - 1);
2948                                 free($2);
2949                                 free($$);
2950                                 YYERROR;
2951                         }
2952                         $$->next = NULL;
2953                         $$->tail = $$;
2954                         free($2);
2955                 }
2956                 ;
2957
2958 number          : NUMBER
2959                 | STRING                {
2960                         u_long  ulval;
2961
2962                         if (atoul($1, &ulval) == -1) {
2963                                 yyerror("%s is not a number", $1);
2964                                 free($1);
2965                                 YYERROR;
2966                         } else
2967                                 $$ = ulval;
2968                         free($1);
2969                 }
2970                 ;
2971
2972 dynaddr         : '(' STRING ')'                {
2973                         int      flags = 0;
2974                         char    *p, *op;
2975
2976                         op = $2;
2977                         if (!isalpha(op[0])) {
2978                                 yyerror("invalid interface name '%s'", op);
2979                                 free(op);
2980                                 YYERROR;
2981                         }
2982                         while ((p = strrchr($2, ':')) != NULL) {
2983                                 if (!strcmp(p+1, "network"))
2984                                         flags |= PFI_AFLAG_NETWORK;
2985                                 else if (!strcmp(p+1, "broadcast"))
2986                                         flags |= PFI_AFLAG_BROADCAST;
2987                                 else if (!strcmp(p+1, "peer"))
2988                                         flags |= PFI_AFLAG_PEER;
2989                                 else if (!strcmp(p+1, "0"))
2990                                         flags |= PFI_AFLAG_NOALIAS;
2991                                 else {
2992                                         yyerror("interface %s has bad modifier",
2993                                             $2);
2994                                         free(op);
2995                                         YYERROR;
2996                                 }
2997                                 *p = '\0';
2998                         }
2999                         if (flags & (flags - 1) & PFI_AFLAG_MODEMASK) {
3000                                 free(op);
3001                                 yyerror("illegal combination of "
3002                                     "interface modifiers");
3003                                 YYERROR;
3004                         }
3005                         $$ = calloc(1, sizeof(struct node_host));
3006                         if ($$ == NULL)
3007                                 err(1, "address: calloc");
3008                         $$->af = 0;
3009                         set_ipmask($$, 128);
3010                         $$->addr.type = PF_ADDR_DYNIFTL;
3011                         $$->addr.iflags = flags;
3012                         if (strlcpy($$->addr.v.ifname, $2,
3013                             sizeof($$->addr.v.ifname)) >=
3014                             sizeof($$->addr.v.ifname)) {
3015                                 free(op);
3016                                 free($$);
3017                                 yyerror("interface name too long");
3018                                 YYERROR;
3019                         }
3020                         free(op);
3021                         $$->next = NULL;
3022                         $$->tail = $$;
3023                 }
3024                 ;
3025
3026 portspec        : port_item                     { $$ = $1; }
3027                 | '{' optnl port_list '}'       { $$ = $3; }
3028                 ;
3029
3030 port_list       : port_item optnl               { $$ = $1; }
3031                 | port_list comma port_item optnl       {
3032                         $1->tail->next = $3;
3033                         $1->tail = $3;
3034                         $$ = $1;
3035                 }
3036                 ;
3037
3038 port_item       : portrange                     {
3039                         $$ = calloc(1, sizeof(struct node_port));
3040                         if ($$ == NULL)
3041                                 err(1, "port_item: calloc");
3042                         $$->port[0] = $1.a;
3043                         $$->port[1] = $1.b;
3044                         if ($1.t)
3045                                 $$->op = PF_OP_RRG;
3046                         else
3047                                 $$->op = PF_OP_EQ;
3048                         $$->next = NULL;
3049                         $$->tail = $$;
3050                 }
3051                 | unaryop portrange     {
3052                         if ($2.t) {
3053                                 yyerror("':' cannot be used with an other "
3054                                     "port operator");
3055                                 YYERROR;
3056                         }
3057                         $$ = calloc(1, sizeof(struct node_port));
3058                         if ($$ == NULL)
3059                                 err(1, "port_item: calloc");
3060                         $$->port[0] = $2.a;
3061                         $$->port[1] = $2.b;
3062                         $$->op = $1;
3063                         $$->next = NULL;
3064                         $$->tail = $$;
3065                 }
3066                 | portrange PORTBINARY portrange        {
3067                         if ($1.t || $3.t) {
3068                                 yyerror("':' cannot be used with an other "
3069                                     "port operator");
3070                                 YYERROR;
3071                         }
3072                         $$ = calloc(1, sizeof(struct node_port));
3073                         if ($$ == NULL)
3074                                 err(1, "port_item: calloc");
3075                         $$->port[0] = $1.a;
3076                         $$->port[1] = $3.a;
3077                         $$->op = $2;
3078                         $$->next = NULL;
3079                         $$->tail = $$;
3080                 }
3081                 ;
3082
3083 portplain       : numberstring                  {
3084                         if (parseport($1, &$$, 0) == -1) {
3085                                 free($1);
3086                                 YYERROR;
3087                         }
3088                         free($1);
3089                 }
3090                 ;
3091
3092 portrange       : numberstring                  {
3093                         if (parseport($1, &$$, PPORT_RANGE) == -1) {
3094                                 free($1);
3095                                 YYERROR;
3096                         }
3097                         free($1);
3098                 }
3099                 ;
3100
3101 uids            : uid_item                      { $$ = $1; }
3102                 | '{' optnl uid_list '}'        { $$ = $3; }
3103                 ;
3104
3105 uid_list        : uid_item optnl                { $$ = $1; }
3106                 | uid_list comma uid_item optnl {
3107                         $1->tail->next = $3;
3108                         $1->tail = $3;
3109                         $$ = $1;
3110                 }
3111                 ;
3112
3113 uid_item        : uid                           {
3114                         $$ = calloc(1, sizeof(struct node_uid));
3115                         if ($$ == NULL)
3116                                 err(1, "uid_item: calloc");
3117                         $$->uid[0] = $1;
3118                         $$->uid[1] = $1;
3119                         $$->op = PF_OP_EQ;
3120                         $$->next = NULL;
3121                         $$->tail = $$;
3122                 }
3123                 | unaryop uid                   {
3124                         if ($2 == UID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
3125                                 yyerror("user unknown requires operator = or "
3126                                     "!=");
3127                                 YYERROR;
3128                         }
3129                         $$ = calloc(1, sizeof(struct node_uid));
3130                         if ($$ == NULL)
3131                                 err(1, "uid_item: calloc");
3132                         $$->uid[0] = $2;
3133                         $$->uid[1] = $2;
3134                         $$->op = $1;
3135                         $$->next = NULL;
3136           &