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