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