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