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