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