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