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