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