Bring the 'probability' keyword into PF from NetBSD. This feature allows
[dragonfly.git] / usr.sbin / pfctl / parse.y
1 /*      $OpenBSD: parse.y,v 1.449 2004/03/20 23:20:20 david Exp $       */
2 /*      $DragonFly: src/usr.sbin/pfctl/parse.y,v 1.4 2008/04/06 21:12:40 dillon Exp $ */
3
4 /*
5  * Copyright (c) 2001 Markus Friedl.  All rights reserved.
6  * Copyright (c) 2001 Daniel Hartmeier.  All rights reserved.
7  * Copyright (c) 2001 Theo de Raadt.  All rights reserved.
8  * Copyright (c) 2002,2003 Henning Brauer. All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 %{
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <net/if.h>
34 #include <netinet/in.h>
35 #include <netinet/in_systm.h>
36 #include <netinet/ip.h>
37 #include <netinet/ip_icmp.h>
38 #include <netinet/icmp6.h>
39 #include <net/pf/pfvar.h>
40 #include <arpa/inet.h>
41 #include <net/altq/altq.h>
42 #include <net/altq/altq_cbq.h>
43 #include <net/altq/altq_priq.h>
44 #include <net/altq/altq_hfsc.h>
45 #include <net/altq/altq_fairq.h>
46
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <netdb.h>
50 #include <stdarg.h>
51 #include <errno.h>
52 #include <string.h>
53 #include <ctype.h>
54 #include <math.h>
55 #include <err.h>
56 #include <limits.h>
57 #include <pwd.h>
58 #include <grp.h>
59 #include <md5.h>
60
61 #include "pfctl_parser.h"
62 #include "pfctl.h"
63
64 static struct pfctl     *pf = NULL;
65 static FILE             *fin = NULL;
66 static int               debug = 0;
67 static int               lineno = 1;
68 static int               errors = 0;
69 static int               rulestate = 0;
70 static u_int16_t         returnicmpdefault =
71                             (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
72 static u_int16_t         returnicmp6default =
73                             (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
74 static int               blockpolicy = PFRULE_DROP;
75 static int               require_order = 1;
76 static int               default_statelock;
77
78 enum {
79         PFCTL_STATE_NONE,
80         PFCTL_STATE_OPTION,
81         PFCTL_STATE_SCRUB,
82         PFCTL_STATE_QUEUE,
83         PFCTL_STATE_NAT,
84         PFCTL_STATE_FILTER
85 };
86
87 struct node_proto {
88         u_int8_t                 proto;
89         struct node_proto       *next;
90         struct node_proto       *tail;
91 };
92
93 struct node_port {
94         u_int16_t                port[2];
95         u_int8_t                 op;
96         struct node_port        *next;
97         struct node_port        *tail;
98 };
99
100 struct node_uid {
101         uid_t                    uid[2];
102         u_int8_t                 op;
103         struct node_uid         *next;
104         struct node_uid         *tail;
105 };
106
107 struct node_gid {
108         gid_t                    gid[2];
109         u_int8_t                 op;
110         struct node_gid         *next;
111         struct node_gid         *tail;
112 };
113
114 struct node_icmp {
115         u_int8_t                 code;
116         u_int8_t                 type;
117         u_int8_t                 proto;
118         struct node_icmp        *next;
119         struct node_icmp        *tail;
120 };
121
122 enum    { PF_STATE_OPT_MAX, PF_STATE_OPT_NOSYNC, PF_STATE_OPT_SRCTRACK,
123             PF_STATE_OPT_MAX_SRC_STATES, PF_STATE_OPT_MAX_SRC_NODES,
124             PF_STATE_OPT_STATELOCK, PF_STATE_OPT_TIMEOUT };
125
126 enum    { PF_SRCTRACK_NONE, PF_SRCTRACK, PF_SRCTRACK_GLOBAL, PF_SRCTRACK_RULE };
127
128 struct node_state_opt {
129         int                      type;
130         union {
131                 u_int32_t        max_states;
132                 u_int32_t        max_src_states;
133                 u_int32_t        max_src_nodes;
134                 u_int8_t         src_track;
135                 u_int32_t        statelock;
136                 struct {
137                         int             number;
138                         u_int32_t       seconds;
139                 }                timeout;
140         }                        data;
141         struct node_state_opt   *next;
142         struct node_state_opt   *tail;
143 };
144
145 struct peer {
146         struct node_host        *host;
147         struct node_port        *port;
148 };
149
150 struct node_queue {
151         char                     queue[PF_QNAME_SIZE];
152         char                     parent[PF_QNAME_SIZE];
153         char                     ifname[IFNAMSIZ];
154         int                      scheduler;
155         struct node_queue       *next;
156         struct node_queue       *tail;
157 }       *queues = NULL;
158
159 struct node_qassign {
160         char            *qname;
161         char            *pqname;
162 };
163
164 struct filter_opts {
165         int                      marker;
166 #define FOM_FLAGS       0x01
167 #define FOM_ICMP        0x02
168 #define FOM_TOS         0x04
169 #define FOM_KEEP        0x08
170 #define FOM_SRCTRACK    0x10
171         struct node_uid         *uid;
172         struct node_gid         *gid;
173         struct {
174                 u_int8_t         b1;
175                 u_int8_t         b2;
176                 u_int16_t        w;
177                 u_int16_t        w2;
178         } flags;
179         struct node_icmp        *icmpspec;
180         u_int32_t                tos;
181         u_int32_t                prob;
182         struct {
183                 int                      action;
184                 struct node_state_opt   *options;
185         } keep;
186         int                      fragment;
187         int                      allowopts;
188         char                    *label;
189         struct node_qassign      queues;
190         char                    *tag;
191         char                    *match_tag;
192         u_int8_t                 match_tag_not;
193 } filter_opts;
194
195 struct antispoof_opts {
196         char                    *label;
197 } antispoof_opts;
198
199 struct scrub_opts {
200         int                     marker;
201 #define SOM_MINTTL      0x01
202 #define SOM_MAXMSS      0x02
203 #define SOM_FRAGCACHE   0x04
204         int                     nodf;
205         int                     minttl;
206         int                     maxmss;
207         int                     fragcache;
208         int                     randomid;
209         int                     reassemble_tcp;
210 } scrub_opts;
211
212 struct queue_opts {
213         int                     marker;
214 #define QOM_BWSPEC      0x01
215 #define QOM_SCHEDULER   0x02
216 #define QOM_PRIORITY    0x04
217 #define QOM_TBRSIZE     0x08
218 #define QOM_QLIMIT      0x10
219         struct node_queue_bw    queue_bwspec;
220         struct node_queue_opt   scheduler;
221         int                     priority;
222         int                     tbrsize;
223         int                     qlimit;
224 } queue_opts;
225
226 struct table_opts {
227         int                     flags;
228         int                     init_addr;
229         struct node_tinithead   init_nodes;
230 } table_opts;
231
232 struct pool_opts {
233         int                      marker;
234 #define POM_TYPE                0x01
235 #define POM_STICKYADDRESS       0x02
236         u_int8_t                 opts;
237         int                      type;
238         int                      staticport;
239         struct pf_poolhashkey   *key;
240
241 } pool_opts;
242
243
244 struct node_hfsc_opts   hfsc_opts;
245 struct node_fairq_opts  fairq_opts;
246
247 int     yyerror(const char *, ...);
248 int     disallow_table(struct node_host *, const char *);
249 int     disallow_alias(struct node_host *, const char *);
250 int     rule_consistent(struct pf_rule *);
251 int     filter_consistent(struct pf_rule *);
252 int     nat_consistent(struct pf_rule *);
253 int     rdr_consistent(struct pf_rule *);
254 int     process_tabledef(char *, struct table_opts *);
255 int     yyparse(void);
256 void    expand_label_str(char *, size_t, const char *, const char *);
257 void    expand_label_if(const char *, char *, size_t, const char *);
258 void    expand_label_addr(const char *, char *, size_t, u_int8_t,
259             struct node_host *);
260 void    expand_label_port(const char *, char *, size_t, struct node_port *);
261 void    expand_label_proto(const char *, char *, size_t, u_int8_t);
262 void    expand_label_nr(const char *, char *, size_t);
263 void    expand_label(char *, size_t, const char *, u_int8_t, struct node_host *,
264             struct node_port *, struct node_host *, struct node_port *,
265             u_int8_t);
266 void    expand_rule(struct pf_rule *, struct node_if *, struct node_host *,
267             struct node_proto *, struct node_os*, struct node_host *,
268             struct node_port *, struct node_host *, struct node_port *,
269             struct node_uid *, struct node_gid *, struct node_icmp *);
270 int     expand_altq(struct pf_altq *, struct node_if *, struct node_queue *,
271             struct node_queue_bw bwspec, struct node_queue_opt *);
272 int     expand_queue(struct pf_altq *, struct node_if *, struct node_queue *,
273             struct node_queue_bw, struct node_queue_opt *);
274
275 int      check_rulestate(int);
276 int      kw_cmp(const void *, const void *);
277 int      lookup(char *);
278 int      lgetc(FILE *);
279 int      lungetc(int);
280 int      findeol(void);
281 int      yylex(void);
282 int      atoul(char *, u_long *);
283 int      getservice(char *);
284 int      rule_label(struct pf_rule *, char *);
285
286 TAILQ_HEAD(symhead, sym)         symhead = TAILQ_HEAD_INITIALIZER(symhead);
287 struct sym {
288         TAILQ_ENTRY(sym)         entries;
289         int                      used;
290         int                      persist;
291         char                    *nam;
292         char                    *val;
293 };
294
295
296 int      symset(const char *, const char *, int);
297 char    *symget(const char *);
298
299 void     decide_address_family(struct node_host *, sa_family_t *);
300 void     remove_invalid_hosts(struct node_host **, sa_family_t *);
301 int      invalid_redirect(struct node_host *, sa_family_t);
302 u_int16_t parseicmpspec(char *, sa_family_t);
303
304 TAILQ_HEAD(loadanchorshead, loadanchors)
305     loadanchorshead = TAILQ_HEAD_INITIALIZER(loadanchorshead);
306
307 struct loadanchors {
308         TAILQ_ENTRY(loadanchors)         entries;
309         char                            *anchorname;
310         char                            *rulesetname;
311         char                            *filename;
312 };
313
314 typedef struct {
315         union {
316                 u_int32_t                number;
317                 int                      i;
318                 char                    *string;
319                 struct {
320                         u_int8_t         b1;
321                         u_int8_t         b2;
322                         u_int16_t        w;
323                         u_int16_t        w2;
324                 }                        b;
325                 struct range {
326                         int              a;
327                         int              b;
328                         int              t;
329                 }                        range;
330                 struct node_if          *interface;
331                 struct node_proto       *proto;
332                 struct node_icmp        *icmp;
333                 struct node_host        *host;
334                 struct node_os          *os;
335                 struct node_port        *port;
336                 struct node_uid         *uid;
337                 struct node_gid         *gid;
338                 struct node_state_opt   *state_opt;
339                 struct peer              peer;
340                 struct {
341                         struct peer      src, dst;
342                         struct node_os  *src_os;
343                 }                        fromto;
344                 struct {
345                         struct node_host        *host;
346                         u_int8_t                 rt;
347                         u_int8_t                 pool_opts;
348                         sa_family_t              af;
349                         struct pf_poolhashkey   *key;
350                 }                        route;
351                 struct redirection {
352                         struct node_host        *host;
353                         struct range             rport;
354                 }                       *redirection;
355                 struct {
356                         int                      action;
357                         struct node_state_opt   *options;
358                 }                        keep_state;
359                 struct {
360                         u_int8_t         log;
361                         u_int8_t         quick;
362                 }                        logquick;
363                 struct pf_poolhashkey   *hashkey;
364                 struct node_queue       *queue;
365                 struct node_queue_opt    queue_options;
366                 struct node_queue_bw     queue_bwspec;
367                 struct node_qassign      qassign;
368                 struct filter_opts       filter_opts;
369                 struct antispoof_opts    antispoof_opts;
370                 struct queue_opts        queue_opts;
371                 struct scrub_opts        scrub_opts;
372                 struct table_opts        table_opts;
373                 struct pool_opts         pool_opts;
374                 struct node_hfsc_opts    hfsc_opts;
375                 struct node_fairq_opts   fairq_opts;
376         } v;
377         int lineno;
378 } YYSTYPE;
379
380 #define PREPARE_ANCHOR_RULE(r, a)                               \
381         do {                                                    \
382                 memset(&(r), 0, sizeof(r));                     \
383                 if (strlcpy(r.anchorname, (a),                  \
384                     sizeof(r.anchorname)) >=                    \
385                     sizeof(r.anchorname)) {                     \
386                         yyerror("anchor name '%s' too long",    \
387                             (a));                               \
388                         YYERROR;                                \
389                 }                                               \
390         } while (0)
391
392 #define DYNIF_MULTIADDR(addr) ((addr).type == PF_ADDR_DYNIFTL && \
393         (!((addr).iflags & PFI_AFLAG_NOALIAS) ||                 \
394         !isdigit((addr).v.ifname[strlen((addr).v.ifname)-1])))
395
396 extern char     *infile;
397
398 %}
399
400 %token  PASS BLOCK SCRUB RETURN IN OS OUT LOG LOGALL QUICK ON FROM TO FLAGS
401 %token  RETURNRST RETURNICMP RETURNICMP6 PROTO INET INET6 ALL ANY ICMPTYPE
402 %token  ICMP6TYPE CODE KEEP MODULATE STATE PORT RDR NAT BINAT ARROW NODF
403 %token  MINTTL ERROR ALLOWOPTS FASTROUTE FILENAME ROUTETO DUPTO REPLYTO NO LABEL
404 %token  NOROUTE FRAGMENT USER GROUP MAXMSS MAXIMUM TTL TOS DROP TABLE
405 %token  REASSEMBLE FRAGDROP FRAGCROP ANCHOR NATANCHOR RDRANCHOR BINATANCHOR
406 %token  SET OPTIMIZATION TIMEOUT LIMIT LOGINTERFACE BLOCKPOLICY RANDOMID
407 %token  REQUIREORDER SYNPROXY FINGERPRINTS NOSYNC DEBUG HOSTID
408 %token  ANTISPOOF FOR
409 %token  BITMASK RANDOM SOURCEHASH ROUNDROBIN STATICPORT PROBABILITY
410 %token  ALTQ CBQ PRIQ HFSC FAIRQ BANDWIDTH TBRSIZE LINKSHARE REALTIME UPPERLIMIT
411 %token  QUEUE PRIORITY QLIMIT HOGS BUCKETS
412 %token  LOAD
413 %token  STICKYADDRESS MAXSRCSTATES MAXSRCNODES SOURCETRACK GLOBAL RULE
414 %token  TAGGED TAG IFBOUND GRBOUND FLOATING STATEPOLICY
415 %token  <v.string>              STRING
416 %token  <v.i>                   PORTBINARY
417 %type   <v.interface>           interface if_list if_item_not if_item
418 %type   <v.number>              number icmptype icmp6type uid gid
419 %type   <v.number>              tos not yesno natpass
420 %type   <v.i>                   no dir log af fragcache sourcetrack
421 %type   <v.i>                   unaryop statelock
422 %type   <v.b>                   action nataction flags flag blockspec
423 %type   <v.range>               port rport
424 %type   <v.hashkey>             hashkey
425 %type   <v.proto>               proto proto_list proto_item
426 %type   <v.icmp>                icmpspec
427 %type   <v.icmp>                icmp_list icmp_item
428 %type   <v.icmp>                icmp6_list icmp6_item
429 %type   <v.fromto>              fromto
430 %type   <v.peer>                ipportspec from to
431 %type   <v.host>                ipspec xhost host dynaddr host_list
432 %type   <v.host>                redir_host_list redirspec
433 %type   <v.host>                route_host route_host_list routespec
434 %type   <v.os>                  os xos os_list
435 %type   <v.port>                portspec port_list port_item
436 %type   <v.uid>                 uids uid_list uid_item
437 %type   <v.gid>                 gids gid_list gid_item
438 %type   <v.route>               route
439 %type   <v.redirection>         redirection redirpool
440 %type   <v.string>              label string tag
441 %type   <v.keep_state>          keep
442 %type   <v.state_opt>           state_opt_spec state_opt_list state_opt_item
443 %type   <v.logquick>            logquick
444 %type   <v.interface>           antispoof_ifspc antispoof_iflst
445 %type   <v.qassign>             qname
446 %type   <v.queue>               qassign qassign_list qassign_item
447 %type   <v.queue_options>       scheduler
448 %type   <v.number>              cbqflags_list cbqflags_item
449 %type   <v.number>              priqflags_list priqflags_item
450 %type   <v.hfsc_opts>           hfscopts_list hfscopts_item hfsc_opts
451 %type   <v.fairq_opts>          fairqopts_list fairqopts_item fairq_opts
452 %type   <v.queue_bwspec>        bandwidth
453 %type   <v.filter_opts>         filter_opts filter_opt filter_opts_l
454 %type   <v.antispoof_opts>      antispoof_opts antispoof_opt antispoof_opts_l
455 %type   <v.queue_opts>          queue_opts queue_opt queue_opts_l
456 %type   <v.scrub_opts>          scrub_opts scrub_opt scrub_opts_l
457 %type   <v.table_opts>          table_opts table_opt table_opts_l
458 %type   <v.pool_opts>           pool_opts pool_opt pool_opts_l
459 %%
460
461 ruleset         : /* empty */
462                 | ruleset '\n'
463                 | ruleset option '\n'
464                 | ruleset scrubrule '\n'
465                 | ruleset natrule '\n'
466                 | ruleset binatrule '\n'
467                 | ruleset pfrule '\n'
468                 | ruleset anchorrule '\n'
469                 | ruleset loadrule '\n'
470                 | ruleset altqif '\n'
471                 | ruleset queuespec '\n'
472                 | ruleset varset '\n'
473                 | ruleset antispoof '\n'
474                 | ruleset tabledef '\n'
475                 | ruleset error '\n'            { errors++; }
476                 ;
477
478 option          : SET OPTIMIZATION STRING               {
479                         if (check_rulestate(PFCTL_STATE_OPTION)) {
480                                 free($3);
481                                 YYERROR;
482                         }
483                         if (pfctl_set_optimization(pf, $3) != 0) {
484                                 yyerror("unknown optimization %s", $3);
485                                 free($3);
486                                 YYERROR;
487                         }
488                         free ($3);
489                 }
490                 | SET TIMEOUT timeout_spec
491                 | SET TIMEOUT '{' timeout_list '}'
492                 | SET LIMIT limit_spec
493                 | SET LIMIT '{' limit_list '}'
494                 | SET LOGINTERFACE STRING               {
495                         if (check_rulestate(PFCTL_STATE_OPTION)) {
496                                 free($3);
497                                 YYERROR;
498                         }
499                         if ((ifa_exists($3, 0) == NULL) && strcmp($3, "none")) {
500                                 yyerror("interface %s doesn't exist", $3);
501                                 free($3);
502                                 YYERROR;
503                         }
504                         if (pfctl_set_logif(pf, $3) != 0) {
505                                 yyerror("error setting loginterface %s", $3);
506                                 free($3);
507                                 YYERROR;
508                         }
509                         free($3);
510                 }
511                 | SET HOSTID number {
512                         if ($3 == 0) {
513                                 yyerror("hostid must be non-zero");
514                                 YYERROR;
515                         }
516                         if (pfctl_set_hostid(pf, $3) != 0) {
517                                 yyerror("error setting loginterface %08x", $3);
518                                 YYERROR;
519                         }
520                 }
521                 | SET BLOCKPOLICY DROP  {
522                         if (pf->opts & PF_OPT_VERBOSE)
523                                 printf("set block-policy drop\n");
524                         if (check_rulestate(PFCTL_STATE_OPTION))
525                                 YYERROR;
526                         blockpolicy = PFRULE_DROP;
527                 }
528                 | SET BLOCKPOLICY RETURN {
529                         if (pf->opts & PF_OPT_VERBOSE)
530                                 printf("set block-policy return\n");
531                         if (check_rulestate(PFCTL_STATE_OPTION))
532                                 YYERROR;
533                         blockpolicy = PFRULE_RETURN;
534                 }
535                 | SET REQUIREORDER yesno {
536                         if (pf->opts & PF_OPT_VERBOSE)
537                                 printf("set require-order %s\n",
538                                     $3 == 1 ? "yes" : "no");
539                         require_order = $3;
540                 }
541                 | SET FINGERPRINTS STRING {
542                         if (pf->opts & PF_OPT_VERBOSE)
543                                 printf("fingerprints %s\n", $3);
544                         if (check_rulestate(PFCTL_STATE_OPTION)) {
545                                 free($3);
546                                 YYERROR;
547                         }
548                         if (pfctl_file_fingerprints(pf->dev, pf->opts, $3)) {
549                                 yyerror("error loading fingerprints %s", $3);
550                                 free($3);
551                                 YYERROR;
552                         }
553                         free($3);
554                 }
555                 | SET STATEPOLICY statelock {
556                         if (pf->opts & PF_OPT_VERBOSE)
557                                 switch ($3) {
558                                 case 0:
559                                         printf("set state-policy floating\n");
560                                         break;
561                                 case PFRULE_IFBOUND:
562                                         printf("set state-policy if-bound\n");
563                                         break;
564                                 case PFRULE_GRBOUND:
565                                         printf("set state-policy "
566                                             "group-bound\n");
567                                         break;
568                                 }
569                         default_statelock = $3;
570                 }
571                 | SET DEBUG STRING {
572                         if (check_rulestate(PFCTL_STATE_OPTION)) {
573                                 free($3);
574                                 YYERROR;
575                         }
576                         if (pfctl_set_debug(pf, $3) != 0) {
577                                 yyerror("error setting debuglevel %s", $3);
578                                 free($3);
579                                 YYERROR;
580                         }
581                         free($3);
582                 }
583                 ;
584
585 string          : string STRING                         {
586                         if (asprintf(&$$, "%s %s", $1, $2) == -1)
587                                 err(1, "string: asprintf");
588                         free($1);
589                         free($2);
590                 }
591                 | STRING
592                 ;
593
594 varset          : STRING '=' string             {
595                         if (pf->opts & PF_OPT_VERBOSE)
596                                 printf("%s = \"%s\"\n", $1, $3);
597                         if (symset($1, $3, 0) == -1)
598                                 err(1, "cannot store variable %s", $1);
599                         free($1);
600                         free($3);
601                 }
602                 ;
603
604 anchorrule      : ANCHOR string dir interface af proto fromto filter_opts {
605                         struct pf_rule  r;
606
607                         if (check_rulestate(PFCTL_STATE_FILTER)) {
608                                 free($2);
609                                 YYERROR;
610                         }
611
612                         PREPARE_ANCHOR_RULE(r, $2);
613                         r.direction = $3;
614                         r.af = $5;
615                         r.prob = $8.prob;
616
617                         if ($8.match_tag)
618                                 if (strlcpy(r.match_tagname, $8.match_tag,
619                                     PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
620                                         yyerror("tag too long, max %u chars",
621                                             PF_TAG_NAME_SIZE - 1);
622                                         YYERROR;
623                                 }
624                         r.match_tag_not = $8.match_tag_not;
625
626                         decide_address_family($7.src.host, &r.af);
627                         decide_address_family($7.dst.host, &r.af);
628
629                         expand_rule(&r, $4, NULL, $6, $7.src_os,
630                             $7.src.host, $7.src.port, $7.dst.host, $7.dst.port,
631                             0, 0, 0);
632                 }
633                 | NATANCHOR string interface af proto fromto {
634                         struct pf_rule  r;
635
636                         if (check_rulestate(PFCTL_STATE_NAT)) {
637                                 free($2);
638                                 YYERROR;
639                         }
640
641                         PREPARE_ANCHOR_RULE(r, $2);
642                         free($2);
643                         r.action = PF_NAT;
644                         r.af = $4;
645
646                         decide_address_family($6.src.host, &r.af);
647                         decide_address_family($6.dst.host, &r.af);
648
649                         expand_rule(&r, $3, NULL, $5, $6.src_os,
650                             $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
651                             0, 0, 0);
652                 }
653                 | RDRANCHOR string interface af proto fromto {
654                         struct pf_rule  r;
655
656                         if (check_rulestate(PFCTL_STATE_NAT)) {
657                                 free($2);
658                                 YYERROR;
659                         }
660
661                         PREPARE_ANCHOR_RULE(r, $2);
662                         free($2);
663                         r.action = PF_RDR;
664                         r.af = $4;
665
666                         decide_address_family($6.src.host, &r.af);
667                         decide_address_family($6.dst.host, &r.af);
668
669                         if ($6.src.port != NULL) {
670                                 yyerror("source port parameter not supported"
671                                     " in rdr-anchor");
672                                 YYERROR;
673                         }
674                         if ($6.dst.port != NULL) {
675                                 if ($6.dst.port->next != NULL) {
676                                         yyerror("destination port list "
677                                             "expansion not supported in "
678                                             "rdr-anchor");
679                                         YYERROR;
680                                 } else if ($6.dst.port->op != PF_OP_EQ) {
681                                         yyerror("destination port operators"
682                                             " not supported in rdr-anchor");
683                                         YYERROR;
684                                 }
685                                 r.dst.port[0] = $6.dst.port->port[0];
686                                 r.dst.port[1] = $6.dst.port->port[1];
687                                 r.dst.port_op = $6.dst.port->op;
688                         }
689
690                         expand_rule(&r, $3, NULL, $5, $6.src_os,
691                             $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
692                             0, 0, 0);
693                 }
694                 | BINATANCHOR string interface af proto fromto {
695                         struct pf_rule  r;
696
697                         if (check_rulestate(PFCTL_STATE_NAT)) {
698                                 free($2);
699                                 YYERROR;
700                         }
701
702                         PREPARE_ANCHOR_RULE(r, $2);
703                         free($2);
704                         r.action = PF_BINAT;
705                         r.af = $4;
706                         if ($5 != NULL) {
707                                 if ($5->next != NULL) {
708                                         yyerror("proto list expansion"
709                                             " not supported in binat-anchor");
710                                         YYERROR;
711                                 }
712                                 r.proto = $5->proto;
713                                 free($5);
714                         }
715
716                         if ($6.src.host != NULL || $6.src.port != NULL ||
717                             $6.dst.host != NULL || $6.dst.port != NULL) {
718                                 yyerror("fromto parameter not supported"
719                                     " in binat-anchor");
720                                 YYERROR;
721                         }
722
723                         decide_address_family($6.src.host, &r.af);
724                         decide_address_family($6.dst.host, &r.af);
725
726                         pfctl_add_rule(pf, &r);
727                 }
728                 ;
729
730 loadrule        : LOAD ANCHOR string FROM string        {
731                         char                    *t;
732                         struct loadanchors      *loadanchor;
733
734                         t = strsep(&$3, ":");
735                         if (*t == '\0' || $3 == NULL || *$3 == '\0') {
736                                 yyerror("anchor '%s' invalid\n", $3);
737                                 free(t);
738                                 YYERROR;
739                         }
740                         if (strlen(t) >= PF_ANCHOR_NAME_SIZE) {
741                                 yyerror("anchorname %s too long, max %u\n",
742                                     t, PF_ANCHOR_NAME_SIZE - 1);
743                                 free(t);
744                                 YYERROR;
745                         }
746                         if (strlen($3) >= PF_RULESET_NAME_SIZE) {
747                                 yyerror("rulesetname %s too long, max %u\n",
748                                     $3, PF_RULESET_NAME_SIZE - 1);
749                                 free(t);
750                                 YYERROR;
751                         }
752
753                         loadanchor = calloc(1, sizeof(struct loadanchors));
754                         if (loadanchor == NULL)
755                                 err(1, "loadrule: calloc");
756                         if ((loadanchor->anchorname = strdup(t)) == NULL)
757                                 err(1, "loadrule: strdup");
758                         if ((loadanchor->rulesetname = strdup($3)) == NULL)
759                                 err(1, "loadrule: strdup");
760                         if ((loadanchor->filename = strdup($5)) == NULL)
761                                 err(1, "loadrule: strdup");
762
763                         TAILQ_INSERT_TAIL(&loadanchorshead, loadanchor,
764                             entries);
765
766                         free(t); /* not $3 */
767                         free($5);
768                 };
769
770 scrubrule       : SCRUB dir logquick interface af proto fromto scrub_opts
771                 {
772                         struct pf_rule  r;
773
774                         if (check_rulestate(PFCTL_STATE_SCRUB))
775                                 YYERROR;
776
777                         memset(&r, 0, sizeof(r));
778
779                         r.action = PF_SCRUB;
780                         r.direction = $2;
781
782                         r.log = $3.log;
783                         if ($3.quick) {
784                                 yyerror("scrub rules do not support 'quick'");
785                                 YYERROR;
786                         }
787
788                         r.af = $5;
789                         if ($8.nodf)
790                                 r.rule_flag |= PFRULE_NODF;
791                         if ($8.randomid)
792                                 r.rule_flag |= PFRULE_RANDOMID;
793                         if ($8.reassemble_tcp) {
794                                 if (r.direction != PF_INOUT) {
795                                         yyerror("reassemble tcp rules can not "
796                                             "specify direction");
797                                         YYERROR;
798                                 }
799                                 r.rule_flag |= PFRULE_REASSEMBLE_TCP;
800                         }
801                         if ($8.minttl)
802                                 r.min_ttl = $8.minttl;
803                         if ($8.maxmss)
804                                 r.max_mss = $8.maxmss;
805                         if ($8.fragcache)
806                                 r.rule_flag |= $8.fragcache;
807
808                         expand_rule(&r, $4, NULL, $6, $7.src_os,
809                             $7.src.host, $7.src.port, $7.dst.host, $7.dst.port,
810                             NULL, NULL, NULL);
811                 }
812                 ;
813
814 scrub_opts      :       {
815                         bzero(&scrub_opts, sizeof scrub_opts);
816                 }
817                     scrub_opts_l
818                         { $$ = scrub_opts; }
819                 | /* empty */ {
820                         bzero(&scrub_opts, sizeof scrub_opts);
821                         $$ = scrub_opts;
822                 }
823                 ;
824
825 scrub_opts_l    : scrub_opts_l scrub_opt
826                 | scrub_opt
827                 ;
828
829 scrub_opt       : NODF  {
830                         if (scrub_opts.nodf) {
831                                 yyerror("no-df cannot be respecified");
832                                 YYERROR;
833                         }
834                         scrub_opts.nodf = 1;
835                 }
836                 | MINTTL number {
837                         if (scrub_opts.marker & SOM_MINTTL) {
838                                 yyerror("min-ttl cannot be respecified");
839                                 YYERROR;
840                         }
841                         if ($2 > 255) {
842                                 yyerror("illegal min-ttl value %d", $2);
843                                 YYERROR;
844                         }
845                         scrub_opts.marker |= SOM_MINTTL;
846                         scrub_opts.minttl = $2;
847                 }
848                 | MAXMSS number {
849                         if (scrub_opts.marker & SOM_MAXMSS) {
850                                 yyerror("max-mss cannot be respecified");
851                                 YYERROR;
852                         }
853                         if ($2 > 65535) {
854                                 yyerror("illegal max-mss value %d", $2);
855                                 YYERROR;
856                         }
857                         scrub_opts.marker |= SOM_MAXMSS;
858                         scrub_opts.maxmss = $2;
859                 }
860                 | fragcache {
861                         if (scrub_opts.marker & SOM_FRAGCACHE) {
862                                 yyerror("fragcache cannot be respecified");
863                                 YYERROR;
864                         }
865                         scrub_opts.marker |= SOM_FRAGCACHE;
866                         scrub_opts.fragcache = $1;
867                 }
868                 | REASSEMBLE STRING {
869                         if (strcasecmp($2, "tcp") != 0) {
870                                 free($2);
871                                 YYERROR;
872                         }
873                         free($2);
874                         if (scrub_opts.reassemble_tcp) {
875                                 yyerror("reassemble tcp cannot be respecified");
876                                 YYERROR;
877                         }
878                         scrub_opts.reassemble_tcp = 1;
879                 }
880                 | RANDOMID {
881                         if (scrub_opts.randomid) {
882                                 yyerror("random-id cannot be respecified");
883                                 YYERROR;
884                         }
885                         scrub_opts.randomid = 1;
886                 }
887                 ;
888
889 fragcache       : FRAGMENT REASSEMBLE   { $$ = 0; /* default */ }
890                 | FRAGMENT FRAGCROP     { $$ = PFRULE_FRAGCROP; }
891                 | FRAGMENT FRAGDROP     { $$ = PFRULE_FRAGDROP; }
892                 ;
893
894 antispoof       : ANTISPOOF logquick antispoof_ifspc af antispoof_opts {
895                         struct pf_rule           r;
896                         struct node_host        *h = NULL;
897                         struct node_if          *i, *j;
898
899                         if (check_rulestate(PFCTL_STATE_FILTER))
900                                 YYERROR;
901
902                         for (i = $3; i; i = i->next) {
903                                 bzero(&r, sizeof(r));
904
905                                 r.action = PF_DROP;
906                                 r.direction = PF_IN;
907                                 r.log = $2.log;
908                                 r.quick = $2.quick;
909                                 r.af = $4;
910                                 if (rule_label(&r, $5.label))
911                                         YYERROR;
912                                 j = calloc(1, sizeof(struct node_if));
913                                 if (j == NULL)
914                                         err(1, "antispoof: calloc");
915                                 if (strlcpy(j->ifname, i->ifname,
916                                     sizeof(j->ifname)) >= sizeof(j->ifname)) {
917                                         free(j);
918                                         yyerror("interface name too long");
919                                         YYERROR;
920                                 }
921                                 j->not = 1;
922                                 h = ifa_lookup(j->ifname, PFI_AFLAG_NETWORK);
923
924                                 if (h != NULL)
925                                         expand_rule(&r, j, NULL, NULL, NULL, h,
926                                             NULL, NULL, NULL, NULL, NULL, NULL);
927
928                                 if ((i->ifa_flags & IFF_LOOPBACK) == 0) {
929                                         bzero(&r, sizeof(r));
930
931                                         r.action = PF_DROP;
932                                         r.direction = PF_IN;
933                                         r.log = $2.log;
934                                         r.quick = $2.quick;
935                                         r.af = $4;
936                                         if (rule_label(&r, $5.label))
937                                                 YYERROR;
938                                         h = ifa_lookup(i->ifname, 0);
939                                         if (h != NULL)
940                                                 expand_rule(&r, NULL, NULL,
941                                                     NULL, NULL, h, NULL, NULL,
942                                                     NULL, NULL, NULL, NULL);
943                                 }
944                         }
945                         free($5.label);
946                 }
947                 ;
948
949 antispoof_ifspc : FOR if_item                   { $$ = $2; }
950                 | FOR '{' antispoof_iflst '}'   { $$ = $3; }
951                 ;
952
953 antispoof_iflst : if_item                       { $$ = $1; }
954                 | antispoof_iflst comma if_item {
955                         $1->tail->next = $3;
956                         $1->tail = $3;
957                         $$ = $1;
958                 }
959                 ;
960
961 antispoof_opts  :       { bzero(&antispoof_opts, sizeof antispoof_opts); }
962                     antispoof_opts_l
963                         { $$ = antispoof_opts; }
964                 | /* empty */   {
965                         bzero(&antispoof_opts, sizeof antispoof_opts);
966                         $$ = antispoof_opts;
967                 }
968                 ;
969
970 antispoof_opts_l        : antispoof_opts_l antispoof_opt
971                         | antispoof_opt
972                         ;
973
974 antispoof_opt   : label {
975                         if (antispoof_opts.label) {
976                                 yyerror("label cannot be redefined");
977                                 YYERROR;
978                         }
979                         antispoof_opts.label = $1;
980                 }
981                 ;
982
983 not             : '!'           { $$ = 1; }
984                 | /* empty */   { $$ = 0; }
985                 ;
986
987 tabledef        : TABLE '<' STRING '>' table_opts {
988                         struct node_host         *h, *nh;
989                         struct node_tinit        *ti, *nti;
990
991                         if (strlen($3) >= PF_TABLE_NAME_SIZE) {
992                                 yyerror("table name too long, max %d chars",
993                                     PF_TABLE_NAME_SIZE - 1);
994                                 free($3);
995                                 YYERROR;
996                         }
997                         if (pf->loadopt & PFCTL_FLAG_TABLE)
998                                 if (process_tabledef($3, &$5)) {
999                                         free($3);
1000                                         YYERROR;
1001                                 }
1002                         free($3);
1003                         for (ti = SIMPLEQ_FIRST(&$5.init_nodes);
1004                             ti != SIMPLEQ_END(&$5.init_nodes); ti = nti) {
1005                                 if (ti->file)
1006                                         free(ti->file);
1007                                 for (h = ti->host; h != NULL; h = nh) {
1008                                         nh = h->next;
1009                                         free(h);
1010                                 }
1011                                 nti = SIMPLEQ_NEXT(ti, entries);
1012                                 free(ti);
1013                         }
1014                 }
1015                 ;
1016
1017 table_opts      :       {
1018                         bzero(&table_opts, sizeof table_opts);
1019                         SIMPLEQ_INIT(&table_opts.init_nodes);
1020                 }
1021                     table_opts_l
1022                         { $$ = table_opts; }
1023                 | /* empty */
1024                         {
1025                         bzero(&table_opts, sizeof table_opts);
1026                         SIMPLEQ_INIT(&table_opts.init_nodes);
1027                         $$ = table_opts;
1028                 }
1029                 ;
1030
1031 table_opts_l    : table_opts_l table_opt
1032                 | table_opt
1033                 ;
1034
1035 table_opt       : STRING                {
1036                         if (!strcmp($1, "const"))
1037                                 table_opts.flags |= PFR_TFLAG_CONST;
1038                         else if (!strcmp($1, "persist"))
1039                                 table_opts.flags |= PFR_TFLAG_PERSIST;
1040                         else {
1041                                 free($1);
1042                                 YYERROR;
1043                         }
1044                         free($1);
1045                 }
1046                 | '{' '}'               { table_opts.init_addr = 1; }
1047                 | '{' host_list '}'     {
1048                         struct node_host        *n;
1049                         struct node_tinit       *ti;
1050
1051                         for (n = $2; n != NULL; n = n->next) {
1052                                 switch (n->addr.type) {
1053                                 case PF_ADDR_ADDRMASK:
1054                                         continue; /* ok */
1055                                 case PF_ADDR_DYNIFTL:
1056                                         yyerror("dynamic addresses are not "
1057                                             "permitted inside tables");
1058                                         break;
1059                                 case PF_ADDR_TABLE:
1060                                         yyerror("tables cannot contain tables");
1061                                         break;
1062                                 case PF_ADDR_NOROUTE:
1063                                         yyerror("\"no-route\" is not permitted "
1064                                             "inside tables");
1065                                         break;
1066                                 default:
1067                                         yyerror("unknown address type %d",
1068                                             n->addr.type);
1069                                 }
1070                                 YYERROR;
1071                         }
1072                         if (!(ti = calloc(1, sizeof(*ti))))
1073                                 err(1, "table_opt: calloc");
1074                         ti->host = $2;
1075                         SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1076                             entries);
1077                         table_opts.init_addr = 1;
1078                 }
1079                 | FILENAME STRING       {
1080                         struct node_tinit       *ti;
1081
1082                         if (!(ti = calloc(1, sizeof(*ti))))
1083                                 err(1, "table_opt: calloc");
1084                         ti->file = $2;
1085                         SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1086                             entries);
1087                         table_opts.init_addr = 1;
1088                 }
1089                 ;
1090
1091 altqif          : ALTQ interface queue_opts QUEUE qassign {
1092                         struct pf_altq  a;
1093
1094                         if (check_rulestate(PFCTL_STATE_QUEUE))
1095                                 YYERROR;
1096
1097                         memset(&a, 0, sizeof(a));
1098                         if ($3.scheduler.qtype == ALTQT_NONE) {
1099                                 yyerror("no scheduler specified!");
1100                                 YYERROR;
1101                         }
1102                         a.scheduler = $3.scheduler.qtype;
1103                         a.qlimit = $3.qlimit;
1104                         a.tbrsize = $3.tbrsize;
1105                         if ($5 == NULL) {
1106                                 yyerror("no child queues specified");
1107                                 YYERROR;
1108                         }
1109                         if (expand_altq(&a, $2, $5, $3.queue_bwspec,
1110                             &$3.scheduler))
1111                                 YYERROR;
1112                 }
1113                 ;
1114
1115 queuespec       : QUEUE STRING interface queue_opts qassign {
1116                         struct pf_altq  a;
1117
1118                         if (check_rulestate(PFCTL_STATE_QUEUE)) {
1119                                 free($2);
1120                                 YYERROR;
1121                         }
1122
1123                         memset(&a, 0, sizeof(a));
1124
1125                         if (strlcpy(a.qname, $2, sizeof(a.qname)) >=
1126                             sizeof(a.qname)) {
1127                                 yyerror("queue name too long (max "
1128                                     "%d chars)", PF_QNAME_SIZE-1);
1129                                 free($2);
1130                                 YYERROR;
1131                         }
1132                         free($2);
1133                         if ($4.tbrsize) {
1134                                 yyerror("cannot specify tbrsize for queue");
1135                                 YYERROR;
1136                         }
1137                         if ($4.priority > 255) {
1138                                 yyerror("priority out of range: max 255");
1139                                 YYERROR;
1140                         }
1141                         a.priority = $4.priority;
1142                         a.qlimit = $4.qlimit;
1143                         a.scheduler = $4.scheduler.qtype;
1144                         if (expand_queue(&a, $3, $5, $4.queue_bwspec,
1145                             &$4.scheduler)) {
1146                                 yyerror("errors in queue definition");
1147                                 YYERROR;
1148                         }
1149                 }
1150                 ;
1151
1152 queue_opts      :       {
1153                         bzero(&queue_opts, sizeof queue_opts);
1154                         queue_opts.priority = DEFAULT_PRIORITY;
1155                         queue_opts.qlimit = DEFAULT_QLIMIT;
1156                         queue_opts.scheduler.qtype = ALTQT_NONE;
1157                         queue_opts.queue_bwspec.bw_percent = 100;
1158                 }
1159                     queue_opts_l
1160                         { $$ = queue_opts; }
1161                 | /* empty */ {
1162                         bzero(&queue_opts, sizeof queue_opts);
1163                         queue_opts.priority = DEFAULT_PRIORITY;
1164                         queue_opts.qlimit = DEFAULT_QLIMIT;
1165                         queue_opts.scheduler.qtype = ALTQT_NONE;
1166                         queue_opts.queue_bwspec.bw_percent = 100;
1167                         $$ = queue_opts;
1168                 }
1169                 ;
1170
1171 queue_opts_l    : queue_opts_l queue_opt
1172                 | queue_opt
1173                 ;
1174
1175 queue_opt       : BANDWIDTH bandwidth   {
1176                         if (queue_opts.marker & QOM_BWSPEC) {
1177                                 yyerror("bandwidth cannot be respecified");
1178                                 YYERROR;
1179                         }
1180                         queue_opts.marker |= QOM_BWSPEC;
1181                         queue_opts.queue_bwspec = $2;
1182                 }
1183                 | PRIORITY number       {
1184                         if (queue_opts.marker & QOM_PRIORITY) {
1185                                 yyerror("priority cannot be respecified");
1186                                 YYERROR;
1187                         }
1188                         if ($2 > 255) {
1189                                 yyerror("priority out of range: max 255");
1190                                 YYERROR;
1191                         }
1192                         queue_opts.marker |= QOM_PRIORITY;
1193                         queue_opts.priority = $2;
1194                 }
1195                 | QLIMIT number {
1196                         if (queue_opts.marker & QOM_QLIMIT) {
1197                                 yyerror("qlimit cannot be respecified");
1198                                 YYERROR;
1199                         }
1200                         if ($2 > 65535) {
1201                                 yyerror("qlimit out of range: max 65535");
1202                                 YYERROR;
1203                         }
1204                         queue_opts.marker |= QOM_QLIMIT;
1205                         queue_opts.qlimit = $2;
1206                 }
1207                 | scheduler     {
1208                         if (queue_opts.marker & QOM_SCHEDULER) {
1209                                 yyerror("scheduler cannot be respecified");
1210                                 YYERROR;
1211                         }
1212                         queue_opts.marker |= QOM_SCHEDULER;
1213                         queue_opts.scheduler = $1;
1214                 }
1215                 | TBRSIZE number        {
1216                         if (queue_opts.marker & QOM_TBRSIZE) {
1217                                 yyerror("tbrsize cannot be respecified");
1218                                 YYERROR;
1219                         }
1220                         if ($2 > 65535) {
1221                                 yyerror("tbrsize too big: max 65535");
1222                                 YYERROR;
1223                         }
1224                         queue_opts.marker |= QOM_TBRSIZE;
1225                         queue_opts.tbrsize = $2;
1226                 }
1227                 ;
1228
1229 bandwidth       : STRING {
1230                         double   bps;
1231                         char    *cp;
1232
1233                         $$.bw_percent = 0;
1234
1235                         bps = strtod($1, &cp);
1236                         if (cp != NULL) {
1237                                 if (!strcmp(cp, "b"))
1238                                         ; /* nothing */
1239                                 else if (!strcmp(cp, "Kb"))
1240                                         bps *= 1000;
1241                                 else if (!strcmp(cp, "Mb"))
1242                                         bps *= 1000 * 1000;
1243                                 else if (!strcmp(cp, "Gb"))
1244                                         bps *= 1000 * 1000 * 1000;
1245                                 else if (!strcmp(cp, "%")) {
1246                                         if (bps < 0 || bps > 100) {
1247                                                 yyerror("bandwidth spec "
1248                                                     "out of range");
1249                                                 free($1);
1250                                                 YYERROR;
1251                                         }
1252                                         $$.bw_percent = bps;
1253                                         bps = 0;
1254                                 } else {
1255                                         yyerror("unknown unit %s", cp);
1256                                         free($1);
1257                                         YYERROR;
1258                                 }
1259                         }
1260                         free($1);
1261                         $$.bw_absolute = (u_int32_t)bps;
1262                 }
1263                 ;
1264
1265 scheduler       : CBQ                           {
1266                         $$.qtype = ALTQT_CBQ;
1267                         $$.data.cbq_opts.flags = 0;
1268                 }
1269                 | CBQ '(' cbqflags_list ')'     {
1270                         $$.qtype = ALTQT_CBQ;
1271                         $$.data.cbq_opts.flags = $3;
1272                 }
1273                 | PRIQ                          {
1274                         $$.qtype = ALTQT_PRIQ;
1275                         $$.data.priq_opts.flags = 0;
1276                 }
1277                 | PRIQ '(' priqflags_list ')'   {
1278                         $$.qtype = ALTQT_PRIQ;
1279                         $$.data.priq_opts.flags = $3;
1280                 }
1281                 | HFSC                          {
1282                         $$.qtype = ALTQT_HFSC;
1283                         bzero(&$$.data.hfsc_opts,
1284                             sizeof(struct node_hfsc_opts));
1285                 }
1286                 | HFSC '(' hfsc_opts ')'        {
1287                         $$.qtype = ALTQT_HFSC;
1288                         $$.data.hfsc_opts = $3;
1289                 }
1290                 | FAIRQ                         {
1291                         $$.qtype = ALTQT_FAIRQ;
1292                         bzero(&$$.data.fairq_opts,
1293                             sizeof(struct node_fairq_opts));
1294                 }
1295                 | FAIRQ '(' fairq_opts ')'      {
1296                         $$.qtype = ALTQT_FAIRQ;
1297                         $$.data.fairq_opts = $3;
1298                 }
1299                 ;
1300
1301 cbqflags_list   : cbqflags_item                         { $$ |= $1; }
1302                 | cbqflags_list comma cbqflags_item     { $$ |= $3; }
1303                 ;
1304
1305 cbqflags_item   : STRING        {
1306                         if (!strcmp($1, "default"))
1307                                 $$ = CBQCLF_DEFCLASS;
1308                         else if (!strcmp($1, "borrow"))
1309                                 $$ = CBQCLF_BORROW;
1310                         else if (!strcmp($1, "red"))
1311                                 $$ = CBQCLF_RED;
1312                         else if (!strcmp($1, "ecn"))
1313                                 $$ = CBQCLF_RED|CBQCLF_ECN;
1314                         else if (!strcmp($1, "rio"))
1315                                 $$ = CBQCLF_RIO;
1316                         else {
1317                                 yyerror("unknown cbq flag \"%s\"", $1);
1318                                 free($1);
1319                                 YYERROR;
1320                         }
1321                         free($1);
1322                 }
1323                 ;
1324
1325 priqflags_list  : priqflags_item                        { $$ |= $1; }
1326                 | priqflags_list comma priqflags_item   { $$ |= $3; }
1327                 ;
1328
1329 priqflags_item  : STRING        {
1330                         if (!strcmp($1, "default"))
1331                                 $$ = PRCF_DEFAULTCLASS;
1332                         else if (!strcmp($1, "red"))
1333                                 $$ = PRCF_RED;
1334                         else if (!strcmp($1, "ecn"))
1335                                 $$ = PRCF_RED|PRCF_ECN;
1336                         else if (!strcmp($1, "rio"))
1337                                 $$ = PRCF_RIO;
1338                         else {
1339                                 yyerror("unknown priq flag \"%s\"", $1);
1340                                 free($1);
1341                                 YYERROR;
1342                         }
1343                         free($1);
1344                 }
1345                 ;
1346
1347 hfsc_opts       :       {
1348                                 bzero(&hfsc_opts,
1349                                     sizeof(struct node_hfsc_opts));
1350                         }
1351                     hfscopts_list                               {
1352                         $$ = hfsc_opts;
1353                 }
1354                 ;
1355
1356 hfscopts_list   : hfscopts_item
1357                 | hfscopts_list comma hfscopts_item
1358                 ;
1359
1360 hfscopts_item   : LINKSHARE bandwidth                           {
1361                         if (hfsc_opts.linkshare.used) {
1362                                 yyerror("linkshare already specified");
1363                                 YYERROR;
1364                         }
1365                         hfsc_opts.linkshare.m2 = $2;
1366                         hfsc_opts.linkshare.used = 1;
1367                 }
1368                 | LINKSHARE '(' bandwidth number bandwidth ')'  {
1369                         if (hfsc_opts.linkshare.used) {
1370                                 yyerror("linkshare already specified");
1371                                 YYERROR;
1372                         }
1373                         hfsc_opts.linkshare.m1 = $3;
1374                         hfsc_opts.linkshare.d = $4;
1375                         hfsc_opts.linkshare.m2 = $5;
1376                         hfsc_opts.linkshare.used = 1;
1377                 }
1378                 | REALTIME bandwidth                            {
1379                         if (hfsc_opts.realtime.used) {
1380                                 yyerror("realtime already specified");
1381                                 YYERROR;
1382                         }
1383                         hfsc_opts.realtime.m2 = $2;
1384                         hfsc_opts.realtime.used = 1;
1385                 }
1386                 | REALTIME '(' bandwidth number bandwidth ')'   {
1387                         if (hfsc_opts.realtime.used) {
1388                                 yyerror("realtime already specified");
1389                                 YYERROR;
1390                         }
1391                         hfsc_opts.realtime.m1 = $3;
1392                         hfsc_opts.realtime.d = $4;
1393                         hfsc_opts.realtime.m2 = $5;
1394                         hfsc_opts.realtime.used = 1;
1395                 }
1396                 | UPPERLIMIT bandwidth                          {
1397                         if (hfsc_opts.upperlimit.used) {
1398                                 yyerror("upperlimit already specified");
1399                                 YYERROR;
1400                         }
1401                         hfsc_opts.upperlimit.m2 = $2;
1402                         hfsc_opts.upperlimit.used = 1;
1403                 }
1404                 | UPPERLIMIT '(' bandwidth number bandwidth ')' {
1405                         if (hfsc_opts.upperlimit.used) {
1406                                 yyerror("upperlimit already specified");
1407                                 YYERROR;
1408                         }
1409                         hfsc_opts.upperlimit.m1 = $3;
1410                         hfsc_opts.upperlimit.d = $4;
1411                         hfsc_opts.upperlimit.m2 = $5;
1412                         hfsc_opts.upperlimit.used = 1;
1413                 }
1414                 | STRING        {
1415                         if (!strcmp($1, "default"))
1416                                 hfsc_opts.flags |= HFCF_DEFAULTCLASS;
1417                         else if (!strcmp($1, "red"))
1418                                 hfsc_opts.flags |= HFCF_RED;
1419                         else if (!strcmp($1, "ecn"))
1420                                 hfsc_opts.flags |= HFCF_RED|HFCF_ECN;
1421                         else if (!strcmp($1, "rio"))
1422                                 hfsc_opts.flags |= HFCF_RIO;
1423                         else {
1424                                 yyerror("unknown hfsc flag \"%s\"", $1);
1425                                 free($1);
1426                                 YYERROR;
1427                         }
1428                         free($1);
1429                 }
1430                 ;
1431
1432 fairq_opts      :       {
1433                                 bzero(&fairq_opts,
1434                                     sizeof(struct node_fairq_opts));
1435                         }
1436                     fairqopts_list                              {
1437                         $$ = fairq_opts;
1438                 }
1439                 ;
1440
1441 fairqopts_list  : fairqopts_item
1442                 | fairqopts_list comma fairqopts_item
1443                 ;
1444
1445 fairqopts_item  : LINKSHARE bandwidth                           {
1446                         if (fairq_opts.linkshare.used) {
1447                                 yyerror("linkshare already specified");
1448                                 YYERROR;
1449                         }
1450                         fairq_opts.linkshare.m2 = $2;
1451                         fairq_opts.linkshare.used = 1;
1452                 }
1453                 | LINKSHARE '(' bandwidth number bandwidth ')'  {
1454                         if (fairq_opts.linkshare.used) {
1455                                 yyerror("linkshare already specified");
1456                                 YYERROR;
1457                         }
1458                         fairq_opts.linkshare.m1 = $3;
1459                         fairq_opts.linkshare.d = $4;
1460                         fairq_opts.linkshare.m2 = $5;
1461                         fairq_opts.linkshare.used = 1;
1462                 }
1463                 | HOGS bandwidth {
1464                         fairq_opts.hogs_bw = $2;
1465                 }
1466                 | BUCKETS number {
1467                         fairq_opts.nbuckets = $2;
1468                 }
1469                 | STRING        {
1470                         if (!strcmp($1, "default"))
1471                                 fairq_opts.flags |= FARF_DEFAULTCLASS;
1472                         else if (!strcmp($1, "red"))
1473                                 fairq_opts.flags |= FARF_RED;
1474                         else if (!strcmp($1, "ecn"))
1475                                 fairq_opts.flags |= FARF_RED|FARF_ECN;
1476                         else if (!strcmp($1, "rio"))
1477                                 fairq_opts.flags |= FARF_RIO;
1478                         else {
1479                                 yyerror("unknown fairq flag \"%s\"", $1);
1480                                 free($1);
1481                                 YYERROR;
1482                         }
1483                         free($1);
1484                 }
1485                 ;
1486
1487 qassign         : /* empty */           { $$ = NULL; }
1488                 | qassign_item          { $$ = $1; }
1489                 | '{' qassign_list '}'  { $$ = $2; }
1490                 ;
1491
1492 qassign_list    : qassign_item                  { $$ = $1; }
1493                 | qassign_list comma qassign_item       {
1494                         $1->tail->next = $3;
1495                         $1->tail = $3;
1496                         $$ = $1;
1497                 }
1498                 ;
1499
1500 qassign_item    : STRING                        {
1501                         $$ = calloc(1, sizeof(struct node_queue));
1502                         if ($$ == NULL)
1503                                 err(1, "qassign_item: calloc");
1504                         if (strlcpy($$->queue, $1, sizeof($$->queue)) >=
1505                             sizeof($$->queue)) {
1506                                 yyerror("queue name '%s' too long (max "
1507                                     "%d chars)", $1, sizeof($$->queue)-1);
1508                                 free($1);
1509                                 free($$);
1510                                 YYERROR;
1511                         }
1512                         free($1);
1513                         $$->next = NULL;
1514                         $$->tail = $$;
1515                 }
1516                 ;
1517
1518 pfrule          : action dir logquick interface route af proto fromto
1519                     filter_opts
1520                 {
1521                         struct pf_rule           r;
1522                         struct node_state_opt   *o;
1523                         struct node_proto       *proto;
1524                         int                      srctrack = 0;
1525                         int                      statelock = 0;
1526
1527                         if (check_rulestate(PFCTL_STATE_FILTER))
1528                                 YYERROR;
1529
1530                         memset(&r, 0, sizeof(r));
1531
1532                         r.action = $1.b1;
1533                         switch ($1.b2) {
1534                         case PFRULE_RETURNRST:
1535                                 r.rule_flag |= PFRULE_RETURNRST;
1536                                 r.return_ttl = $1.w;
1537                                 break;
1538                         case PFRULE_RETURNICMP:
1539                                 r.rule_flag |= PFRULE_RETURNICMP;
1540                                 r.return_icmp = $1.w;
1541                                 r.return_icmp6 = $1.w2;
1542                                 break;
1543                         case PFRULE_RETURN:
1544                                 r.rule_flag |= PFRULE_RETURN;
1545                                 r.return_icmp = $1.w;
1546                                 r.return_icmp6 = $1.w2;
1547                                 break;
1548                         }
1549                         r.direction = $2;
1550                         r.log = $3.log;
1551                         r.quick = $3.quick;
1552                         r.prob = $9.prob;
1553
1554                         r.af = $6;
1555                         if ($9.tag)
1556                                 if (strlcpy(r.tagname, $9.tag,
1557                                     PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1558                                         yyerror("tag too long, max %u chars",
1559                                             PF_TAG_NAME_SIZE - 1);
1560                                         YYERROR;
1561                                 }
1562                         if ($9.match_tag)
1563                                 if (strlcpy(r.match_tagname, $9.match_tag,
1564                                     PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1565                                         yyerror("tag too long, max %u chars",
1566                                             PF_TAG_NAME_SIZE - 1);
1567                                         YYERROR;
1568                                 }
1569                         r.match_tag_not = $9.match_tag_not;
1570                         r.flags = $9.flags.b1;
1571                         r.flagset = $9.flags.b2;
1572                         if (rule_label(&r, $9.label))
1573                                 YYERROR;
1574                         free($9.label);
1575                         if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
1576                                 for (proto = $7; proto != NULL &&
1577                                     proto->proto != IPPROTO_TCP;
1578                                     proto = proto->next)
1579                                         ;       /* nothing */
1580                                 if (proto == NULL && $7 != NULL) {
1581                                         if ($9.flags.b1 || $9.flags.b2)
1582                                                 yyerror(
1583                                                     "flags only apply to tcp");
1584                                         if ($8.src_os)
1585                                                 yyerror(
1586                                                     "OS fingerprinting only "
1587                                                     "apply to tcp");
1588                                         YYERROR;
1589                                 }
1590 #if 0
1591                                 if (($9.flags.b1 & parse_flags("S")) == 0 &&
1592                                     $8.src_os) {
1593                                         yyerror("OS fingerprinting requires "
1594                                             "the SYN TCP flag (flags S/SA)");
1595                                         YYERROR;
1596                                 }
1597 #endif
1598                         }
1599
1600                         r.tos = $9.tos;
1601                         r.keep_state = $9.keep.action;
1602                         o = $9.keep.options;
1603                         while (o) {
1604                                 struct node_state_opt   *p = o;
1605
1606                                 switch (o->type) {
1607                                 case PF_STATE_OPT_MAX:
1608                                         if (r.max_states) {
1609                                                 yyerror("state option 'max' "
1610                                                     "multiple definitions");
1611                                                 YYERROR;
1612                                         }
1613                                         r.max_states = o->data.max_states;
1614                                         break;
1615                                 case PF_STATE_OPT_NOSYNC:
1616                                         if (r.rule_flag & PFRULE_NOSYNC) {
1617                                                 yyerror("state option 'sync' "
1618                                                     "multiple definitions");
1619                                                 YYERROR;
1620                                         }
1621                                         r.rule_flag |= PFRULE_NOSYNC;
1622                                         break;
1623                                 case PF_STATE_OPT_SRCTRACK:
1624                                         if (srctrack) {
1625                                                 yyerror("state option "
1626                                                     "'source-track' "
1627                                                     "multiple definitions");
1628                                                 YYERROR;
1629                                         }
1630                                         srctrack =  o->data.src_track;
1631                                         break;
1632                                 case PF_STATE_OPT_MAX_SRC_STATES:
1633                                         if (r.max_src_states) {
1634                                                 yyerror("state option "
1635                                                     "'max-src-states' "
1636                                                     "multiple definitions");
1637                                                 YYERROR;
1638                                         }
1639                                         if (o->data.max_src_nodes == 0) {
1640                                                 yyerror("'max-src-states' must "
1641                                                     "be > 0");
1642                                                 YYERROR;
1643                                         }
1644                                         r.max_src_states =
1645                                             o->data.max_src_states;
1646                                         r.rule_flag |= PFRULE_SRCTRACK;
1647                                         break;
1648                                 case PF_STATE_OPT_MAX_SRC_NODES:
1649                                         if (r.max_src_nodes) {
1650                                                 yyerror("state option "
1651                                                     "'max-src-nodes' "
1652                                                     "multiple definitions");
1653                                                 YYERROR;
1654                                         }
1655                                         if (o->data.max_src_nodes == 0) {
1656                                                 yyerror("'max-src-nodes' must "
1657                                                     "be > 0");
1658                                                 YYERROR;
1659                                         }
1660                                         r.max_src_nodes =
1661                                             o->data.max_src_nodes;
1662                                         r.rule_flag |= PFRULE_SRCTRACK |
1663                                             PFRULE_RULESRCTRACK;
1664                                         break;
1665                                 case PF_STATE_OPT_STATELOCK:
1666                                         if (statelock) {
1667                                                 yyerror("state locking option: "
1668                                                     "multiple definitions");
1669                                                 YYERROR;
1670                                         }
1671                                         statelock = 1;
1672                                         r.rule_flag |= o->data.statelock;
1673                                         break;
1674                                 case PF_STATE_OPT_TIMEOUT:
1675                                         if (r.timeout[o->data.timeout.number]) {
1676                                                 yyerror("state timeout %s "
1677                                                     "multiple definitions",
1678                                                     pf_timeouts[o->data.
1679                                                     timeout.number].name);
1680                                                 YYERROR;
1681                                         }
1682                                         r.timeout[o->data.timeout.number] =
1683                                             o->data.timeout.seconds;
1684                                 }
1685                                 o = o->next;
1686                                 free(p);
1687                         }
1688                         if (srctrack) {
1689                                 if (srctrack == PF_SRCTRACK_GLOBAL &&
1690                                     r.max_src_nodes) {
1691                                         yyerror("'max-src-nodes' is "
1692                                             "incompatible with "
1693                                             "'source-track global'");
1694                                         YYERROR;
1695                                 }
1696                                 r.rule_flag |= PFRULE_SRCTRACK;
1697                                 if (srctrack == PF_SRCTRACK_RULE)
1698                                         r.rule_flag |= PFRULE_RULESRCTRACK;
1699                         }
1700                         if (r.keep_state && !statelock)
1701                                 r.rule_flag |= default_statelock;
1702
1703                         if ($9.fragment)
1704                                 r.rule_flag |= PFRULE_FRAGMENT;
1705                         r.allow_opts = $9.allowopts;
1706
1707                         decide_address_family($8.src.host, &r.af);
1708                         decide_address_family($8.dst.host, &r.af);
1709
1710                         if ($5.rt) {
1711                                 if (!r.direction) {
1712                                         yyerror("direction must be explicit "
1713                                             "with rules that specify routing");
1714                                         YYERROR;
1715                                 }
1716                                 r.rt = $5.rt;
1717                                 r.rpool.opts = $5.pool_opts;
1718                                 if ($5.key != NULL)
1719                                         memcpy(&r.rpool.key, $5.key,
1720                                             sizeof(struct pf_poolhashkey));
1721                         }
1722                         if (r.rt && r.rt != PF_FASTROUTE) {
1723                                 decide_address_family($5.host, &r.af);
1724                                 remove_invalid_hosts(&$5.host, &r.af);
1725                                 if ($5.host == NULL) {
1726                                         yyerror("no routing address with "
1727                                             "matching address family found.");
1728                                         YYERROR;
1729                                 }
1730                                 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
1731                                     PF_POOL_NONE && ($5.host->next != NULL ||
1732                                     $5.host->addr.type == PF_ADDR_TABLE ||
1733                                     DYNIF_MULTIADDR($5.host->addr)))
1734                                         r.rpool.opts |= PF_POOL_ROUNDROBIN;
1735                                 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
1736                                     PF_POOL_ROUNDROBIN &&
1737                                     disallow_table($5.host, "tables are only "
1738                                     "supported in round-robin routing pools"))
1739                                         YYERROR;
1740                                 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
1741                                     PF_POOL_ROUNDROBIN &&
1742                                     disallow_alias($5.host, "interface (%s) "
1743                                     "is only supported in round-robin "
1744                                     "routing pools"))
1745                                         YYERROR;
1746                                 if ($5.host->next != NULL) {
1747                                         if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
1748                                             PF_POOL_ROUNDROBIN) {
1749                                                 yyerror("r.rpool.opts must "
1750                                                     "be PF_POOL_ROUNDROBIN");
1751                                                 YYERROR;
1752                                         }
1753                                 }
1754                         }
1755                         if ($9.queues.qname != NULL) {
1756                                 if (strlcpy(r.qname, $9.queues.qname,
1757                                     sizeof(r.qname)) >= sizeof(r.qname)) {
1758                                         yyerror("rule qname too long (max "
1759                                             "%d chars)", sizeof(r.qname)-1);
1760                                         YYERROR;
1761                                 }
1762                                 free($9.queues.qname);
1763                         }
1764                         if ($9.queues.pqname != NULL) {
1765                                 if (strlcpy(r.pqname, $9.queues.pqname,
1766                                     sizeof(r.pqname)) >= sizeof(r.pqname)) {
1767                                         yyerror("rule pqname too long (max "
1768                                             "%d chars)", sizeof(r.pqname)-1);
1769                                         YYERROR;
1770                                 }
1771                                 free($9.queues.pqname);
1772                         }
1773
1774                         expand_rule(&r, $4, $5.host, $7, $8.src_os,
1775                             $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
1776                             $9.uid, $9.gid, $9.icmpspec);
1777                 }
1778                 ;
1779
1780 filter_opts     :       { bzero(&filter_opts, sizeof filter_opts); }
1781                     filter_opts_l
1782                         { $$ = filter_opts; }
1783                 | /* empty */   {
1784                         bzero(&filter_opts, sizeof filter_opts);
1785                         $$ = filter_opts;
1786                 }
1787                 ;
1788
1789 filter_opts_l   : filter_opts_l filter_opt
1790                 | filter_opt
1791                 ;
1792
1793 filter_opt      : USER uids {
1794                         if (filter_opts.uid)
1795                                 $2->tail->next = filter_opts.uid;
1796                         filter_opts.uid = $2;
1797                 }
1798                 | GROUP gids {
1799                         if (filter_opts.gid)
1800                                 $2->tail->next = filter_opts.gid;
1801                         filter_opts.gid = $2;
1802                 }
1803                 | flags {
1804                         if (filter_opts.marker & FOM_FLAGS) {
1805                                 yyerror("flags cannot be redefined");
1806                                 YYERROR;
1807                         }
1808                         filter_opts.marker |= FOM_FLAGS;
1809                         filter_opts.flags.b1 |= $1.b1;
1810                         filter_opts.flags.b2 |= $1.b2;
1811                         filter_opts.flags.w |= $1.w;
1812                         filter_opts.flags.w2 |= $1.w2;
1813                 }
1814                 | icmpspec {
1815                         if (filter_opts.marker & FOM_ICMP) {
1816                                 yyerror("icmp-type cannot be redefined");
1817                                 YYERROR;
1818                         }
1819                         filter_opts.marker |= FOM_ICMP;
1820                         filter_opts.icmpspec = $1;
1821                 }
1822                 | tos {
1823                         if (filter_opts.marker & FOM_TOS) {
1824                                 yyerror("tos cannot be redefined");
1825                                 YYERROR;
1826                         }
1827                         filter_opts.marker |= FOM_TOS;
1828                         filter_opts.tos = $1;
1829                 }
1830                 | keep {
1831                         if (filter_opts.marker & FOM_KEEP) {
1832                                 yyerror("modulate or keep cannot be redefined");
1833                                 YYERROR;
1834                         }
1835                         filter_opts.marker |= FOM_KEEP;
1836                         filter_opts.keep.action = $1.action;
1837                         filter_opts.keep.options = $1.options;
1838                 }
1839                 | FRAGMENT {
1840                         filter_opts.fragment = 1;
1841                 }
1842                 | ALLOWOPTS {
1843                         filter_opts.allowopts = 1;
1844                 }
1845                 | label {
1846                         if (filter_opts.label) {
1847                                 yyerror("label cannot be redefined");
1848                                 YYERROR;
1849                         }
1850                         filter_opts.label = $1;
1851                 }
1852                 | qname {
1853                         if (filter_opts.queues.qname) {
1854                                 yyerror("queue cannot be redefined");
1855                                 YYERROR;
1856                         }
1857                         filter_opts.queues = $1;
1858                 }
1859                 | TAG string                            {
1860                         filter_opts.tag = $2;
1861                 }
1862                 | not TAGGED string                     {
1863                         filter_opts.match_tag = $3;
1864                         filter_opts.match_tag_not = $1;
1865                 }
1866                 | PROBABILITY STRING                    {
1867                         char    *e;
1868                         double   p = strtod($2, &e);
1869
1870                         if (*e == '%') {
1871                                 p *= 0.01;
1872                                 e++;
1873                         }
1874                         if (*e) {
1875                                 yyerror("invalid probability: %s", $2);
1876                                 free($2);
1877                                 YYERROR;
1878                         }
1879                         p = floor(p * (UINT_MAX+1.0) + 0.5);
1880                         if (p < 1.0 || p >= (UINT_MAX+1.0)) {
1881                                 yyerror("invalid probability: %s", $2);
1882                                 free($2);
1883                                 YYERROR;
1884                         }
1885                         filter_opts.prob = (u_int32_t)p;
1886                         free($2);
1887                 }
1888                 ;
1889
1890 action          : PASS                  { $$.b1 = PF_PASS; $$.b2 = $$.w = 0; }
1891                 | BLOCK blockspec       { $$ = $2; $$.b1 = PF_DROP; }
1892                 ;
1893
1894 blockspec       : /* empty */           {
1895                         $$.b2 = blockpolicy;
1896                         $$.w = returnicmpdefault;
1897                         $$.w2 = returnicmp6default;
1898                 }
1899                 | DROP                  {
1900                         $$.b2 = PFRULE_DROP;
1901                         $$.w = 0;
1902                         $$.w2 = 0;
1903                 }
1904                 | RETURNRST             {
1905                         $$.b2 = PFRULE_RETURNRST;
1906                         $$.w = 0;
1907                         $$.w2 = 0;
1908                 }
1909                 | RETURNRST '(' TTL number ')'  {
1910                         if ($4 > 255) {
1911                                 yyerror("illegal ttl value %d", $4);
1912                                 YYERROR;
1913                         }
1914                         $$.b2 = PFRULE_RETURNRST;
1915                         $$.w = $4;
1916                         $$.w2 = 0;
1917                 }
1918                 | RETURNICMP            {
1919                         $$.b2 = PFRULE_RETURNICMP;
1920                         $$.w = returnicmpdefault;
1921                         $$.w2 = returnicmp6default;
1922                 }
1923                 | RETURNICMP6           {
1924                         $$.b2 = PFRULE_RETURNICMP;
1925                         $$.w = returnicmpdefault;
1926                         $$.w2 = returnicmp6default;
1927                 }
1928                 | RETURNICMP '(' STRING ')'     {
1929                         $$.b2 = PFRULE_RETURNICMP;
1930                         if (!($$.w = parseicmpspec($3, AF_INET))) {
1931                                 free($3);
1932                                 YYERROR;
1933                         }
1934                         free($3);
1935                         $$.w2 = returnicmp6default;
1936                 }
1937                 | RETURNICMP6 '(' STRING ')'    {
1938                         $$.b2 = PFRULE_RETURNICMP;
1939                         $$.w = returnicmpdefault;
1940                         if (!($$.w2 = parseicmpspec($3, AF_INET6))) {
1941                                 free($3);
1942                                 YYERROR;
1943                         }
1944                         free($3);
1945                 }
1946                 | RETURNICMP '(' STRING comma STRING ')' {
1947                         $$.b2 = PFRULE_RETURNICMP;
1948                         if (!($$.w = parseicmpspec($3, AF_INET)) ||
1949                             !($$.w2 = parseicmpspec($5, AF_INET6))) {
1950                                 free($3);
1951                                 free($5);
1952                                 YYERROR;
1953                         }
1954                         free($3);
1955                         free($5);
1956                 }
1957                 | RETURN {
1958                         $$.b2 = PFRULE_RETURN;
1959                         $$.w = returnicmpdefault;
1960                         $$.w2 = returnicmp6default;
1961                 }
1962                 ;
1963
1964 dir             : /* empty */                   { $$ = 0; }
1965                 | IN                            { $$ = PF_IN; }
1966                 | OUT                           { $$ = PF_OUT; }
1967                 ;
1968
1969 logquick        : /* empty */                   { $$.log = 0; $$.quick = 0; }
1970                 | log                           { $$.log = $1; $$.quick = 0; }
1971                 | QUICK                         { $$.log = 0; $$.quick = 1; }
1972                 | log QUICK                     { $$.log = $1; $$.quick = 1; }
1973                 | QUICK log                     { $$.log = $2; $$.quick = 1; }
1974                 ;
1975
1976 log             : LOG                           { $$ = 1; }
1977                 | LOGALL                        { $$ = 2; }
1978                 ;
1979
1980 interface       : /* empty */                   { $$ = NULL; }
1981                 | ON if_item_not                { $$ = $2; }
1982                 | ON '{' if_list '}'            { $$ = $3; }
1983                 ;
1984
1985 if_list         : if_item_not                   { $$ = $1; }
1986                 | if_list comma if_item_not     {
1987                         $1->tail->next = $3;
1988                         $1->tail = $3;
1989                         $$ = $1;
1990                 }
1991                 ;
1992
1993 if_item_not     : not if_item                   { $$ = $2; $$->not = $1; }
1994                 ;
1995
1996 if_item         : STRING                        {
1997                         struct node_host        *n;
1998
1999                         if ((n = ifa_exists($1, 1)) == NULL) {
2000                                 yyerror("unknown interface %s", $1);
2001                                 free($1);
2002                                 YYERROR;
2003                         }
2004                         $$ = calloc(1, sizeof(struct node_if));
2005                         if ($$ == NULL)
2006                                 err(1, "if_item: calloc");
2007                         if (strlcpy($$->ifname, $1, sizeof($$->ifname)) >=
2008                             sizeof($$->ifname)) {
2009                                 free($1);
2010                                 free($$);
2011                                 yyerror("interface name too long");
2012                                 YYERROR;
2013                         }
2014                         free($1);
2015                         $$->ifa_flags = n->ifa_flags;
2016                         $$->not = 0;
2017                         $$->next = NULL;
2018                         $$->tail = $$;
2019                 }
2020                 ;
2021
2022 af              : /* empty */                   { $$ = 0; }
2023                 | INET                          { $$ = AF_INET; }
2024                 | INET6                         { $$ = AF_INET6; }
2025                 ;
2026
2027 proto           : /* empty */                   { $$ = NULL; }
2028                 | PROTO proto_item              { $$ = $2; }
2029                 | PROTO '{' proto_list '}'      { $$ = $3; }
2030                 ;
2031
2032 proto_list      : proto_item                    { $$ = $1; }
2033                 | proto_list comma proto_item   {
2034                         $1->tail->next = $3;
2035                         $1->tail = $3;
2036                         $$ = $1;
2037                 }
2038                 ;
2039
2040 proto_item      : STRING                        {
2041                         u_int8_t        pr;
2042                         u_long          ulval;
2043
2044                         if (atoul($1, &ulval) == 0) {
2045                                 if (ulval > 255) {
2046                                         yyerror("protocol outside range");
2047                                         free($1);
2048                                         YYERROR;
2049                                 }
2050                                 pr = (u_int8_t)ulval;
2051                         } else {
2052                                 struct protoent *p;
2053
2054                                 p = getprotobyname($1);
2055                                 if (p == NULL) {
2056                                         yyerror("unknown protocol %s", $1);
2057                                         free($1);
2058                                         YYERROR;
2059                                 }
2060                                 pr = p->p_proto;
2061                         }
2062                         free($1);
2063                         if (pr == 0) {
2064                                 yyerror("proto 0 cannot be used");
2065                                 YYERROR;
2066                         }
2067                         $$ = calloc(1, sizeof(struct node_proto));
2068                         if ($$ == NULL)
2069                                 err(1, "proto_item: calloc");
2070                         $$->proto = pr;
2071                         $$->next = NULL;
2072                         $$->tail = $$;
2073                 }
2074                 ;
2075
2076 fromto          : ALL                           {
2077                         $$.src.host = NULL;
2078                         $$.src.port = NULL;
2079                         $$.dst.host = NULL;
2080                         $$.dst.port = NULL;
2081                         $$.src_os = NULL;
2082                 }
2083                 | from os to                    {
2084                         $$.src = $1;
2085                         $$.src_os = $2;
2086                         $$.dst = $3;
2087                 }
2088                 ;
2089
2090 os              : /* empty */                   { $$ = NULL; }
2091                 | OS xos                        { $$ = $2; }
2092                 | OS '{' os_list '}'            { $$ = $3; }
2093                 ;
2094
2095 xos             : STRING {
2096                         $$ = calloc(1, sizeof(struct node_os));
2097                         if ($$ == NULL)
2098                                 err(1, "os: calloc");
2099                         $$->os = $1;
2100                         $$->tail = $$;
2101                 }
2102                 ;
2103
2104 os_list         : xos                           { $$ = $1; }
2105                 | os_list comma xos             {
2106                         $1->tail->next = $3;
2107                         $1->tail = $3;
2108                         $$ = $1;
2109                 }
2110                 ;
2111
2112 from            : /* empty */                   {
2113                         $$.host = NULL;
2114                         $$.port = NULL;
2115                 }
2116                 | FROM ipportspec               {
2117                         $$ = $2;
2118                 }
2119                 ;
2120
2121 to              : /* empty */                   {
2122                         $$.host = NULL;
2123                         $$.port = NULL;
2124                 }
2125                 | TO ipportspec         {
2126                         $$ = $2;
2127                 }
2128                 ;
2129
2130 ipportspec      : ipspec                        {
2131                         $$.host = $1;
2132                         $$.port = NULL;
2133                 }
2134                 | ipspec PORT portspec          {
2135                         $$.host = $1;
2136                         $$.port = $3;
2137                 }
2138                 | PORT portspec                 {
2139                         $$.host = NULL;
2140                         $$.port = $2;
2141                 }
2142                 ;
2143
2144 ipspec          : ANY                           { $$ = NULL; }
2145                 | xhost                         { $$ = $1; }
2146                 | '{' host_list '}'             { $$ = $2; }
2147                 ;
2148
2149 host_list       : xhost                         { $$ = $1; }
2150                 | host_list comma xhost         {
2151                         if ($3 == NULL)
2152                                 $$ = $1;
2153                         else if ($1 == NULL)
2154                                 $$ = $3;
2155                         else {
2156                                 $1->tail->next = $3;
2157                                 $1->tail = $3->tail;
2158                                 $$ = $1;
2159                         }
2160                 }
2161                 ;
2162
2163 xhost           : not host                      {
2164                         struct node_host        *n;
2165
2166                         for (n = $2; n != NULL; n = n->next)
2167                                 n->not = $1;
2168                         $$ = $2;
2169                 }
2170                 | NOROUTE                       {
2171                         $$ = calloc(1, sizeof(struct node_host));
2172                         if ($$ == NULL)
2173                                 err(1, "xhost: calloc");
2174                         $$->addr.type = PF_ADDR_NOROUTE;
2175                         $$->next = NULL;
2176                         $$->tail = $$;
2177                 }
2178                 ;
2179
2180 host            : STRING                        {
2181                         if (($$ = host($1)) == NULL)    {
2182                                 /* error. "any" is handled elsewhere */
2183                                 free($1);
2184                                 yyerror("could not parse host specification");
2185                                 YYERROR;
2186                         }
2187                         free($1);
2188
2189                 }
2190                 | STRING '/' number             {
2191                         char    *buf;
2192
2193                         if (asprintf(&buf, "%s/%u", $1, $3) == -1)
2194                                 err(1, "host: asprintf");
2195                         free($1);
2196                         if (($$ = host(buf)) == NULL)   {
2197                                 /* error. "any" is handled elsewhere */
2198                                 free(buf);
2199                                 yyerror("could not parse host specification");
2200                                 YYERROR;
2201                         }
2202                         free(buf);
2203                 }
2204                 | dynaddr
2205                 | dynaddr '/' number            {
2206                         struct node_host        *n;
2207
2208                         $$ = $1;
2209                         for (n = $1; n != NULL; n = n->next)
2210                                 set_ipmask(n, $3);
2211                 }
2212                 | '<' STRING '>'        {
2213                         if (strlen($2) >= PF_TABLE_NAME_SIZE) {
2214                                 yyerror("table name '%s' too long", $2);
2215                                 free($2);
2216                                 YYERROR;
2217                         }
2218                         $$ = calloc(1, sizeof(struct node_host));
2219                         if ($$ == NULL)
2220                                 err(1, "host: calloc");
2221                         $$->addr.type = PF_ADDR_TABLE;
2222                         if (strlcpy($$->addr.v.tblname, $2,
2223                             sizeof($$->addr.v.tblname)) >=
2224                             sizeof($$->addr.v.tblname))
2225                                 errx(1, "host: strlcpy");
2226                         free($2);
2227                         $$->next = NULL;
2228                         $$->tail = $$;
2229                 }
2230                 ;
2231
2232 number          : STRING                        {
2233                         u_long  ulval;
2234
2235                         if (atoul($1, &ulval) == -1) {
2236                                 yyerror("%s is not a number", $1);
2237                                 free($1);
2238                                 YYERROR;
2239                         } else
2240                                 $$ = ulval;
2241                         free($1);
2242                 }
2243                 ;
2244
2245 dynaddr         : '(' STRING ')'                {
2246                         int      flags = 0;
2247                         char    *p, *op;
2248
2249                         op = $2;
2250                         while ((p = strrchr($2, ':')) != NULL) {
2251                                 if (!strcmp(p+1, "network"))
2252                                         flags |= PFI_AFLAG_NETWORK;
2253                                 else if (!strcmp(p+1, "broadcast"))
2254                                         flags |= PFI_AFLAG_BROADCAST;
2255                                 else if (!strcmp(p+1, "peer"))
2256                                         flags |= PFI_AFLAG_PEER;
2257                                 else if (!strcmp(p+1, "0"))
2258                                         flags |= PFI_AFLAG_NOALIAS;
2259                                 else {
2260                                         yyerror("interface %s has bad modifier",
2261                                             $2);
2262                                         free(op);
2263                                         YYERROR;
2264                                 }
2265                                 *p = '\0';
2266                         }
2267                         if (flags & (flags - 1) & PFI_AFLAG_MODEMASK) {
2268                                 free(op);
2269                                 yyerror("illegal combination of "
2270                                     "interface modifiers");
2271                                 YYERROR;
2272                         }
2273                         if (ifa_exists($2, 1) == NULL && strcmp($2, "self")) {
2274                                 yyerror("interface %s does not exist", $2);
2275                                 free(op);
2276                                 YYERROR;
2277                         }
2278                         $$ = calloc(1, sizeof(struct node_host));
2279                         if ($$ == NULL)
2280                                 err(1, "address: calloc");
2281                         $$->af = 0;
2282                         set_ipmask($$, 128);
2283                         $$->addr.type = PF_ADDR_DYNIFTL;
2284                         $$->addr.iflags = flags;
2285                         if (strlcpy($$->addr.v.ifname, $2,
2286                             sizeof($$->addr.v.ifname)) >=
2287                             sizeof($$->addr.v.ifname)) {
2288                                 free(op);
2289                                 free($$);
2290                                 yyerror("interface name too long");
2291                                 YYERROR;
2292                         }
2293                         free(op);
2294                         $$->next = NULL;
2295                         $$->tail = $$;
2296                 }
2297                 ;
2298
2299 portspec        : port_item                     { $$ = $1; }
2300                 | '{' port_list '}'             { $$ = $2; }
2301                 ;
2302
2303 port_list       : port_item                     { $$ = $1; }
2304                 | port_list comma port_item     {
2305                         $1->tail->next = $3;
2306                         $1->tail = $3;
2307                         $$ = $1;
2308                 }
2309                 ;
2310
2311 port_item       : port                          {
2312                         $$ = calloc(1, sizeof(struct node_port));
2313                         if ($$ == NULL)
2314                                 err(1, "port_item: calloc");
2315                         $$->port[0] = $1.a;
2316                         $$->port[1] = $1.b;
2317                         if ($1.t)
2318                                 $$->op = PF_OP_RRG;
2319                         else
2320                                 $$->op = PF_OP_EQ;
2321                         $$->next = NULL;
2322                         $$->tail = $$;
2323                 }
2324                 | unaryop port          {
2325                         if ($2.t) {
2326                                 yyerror("':' cannot be used with an other "
2327                                     "port operator");
2328                                 YYERROR;
2329                         }
2330                         $$ = calloc(1, sizeof(struct node_port));
2331                         if ($$ == NULL)
2332                                 err(1, "port_item: calloc");
2333                         $$->port[0] = $2.a;
2334                         $$->port[1] = $2.b;
2335                         $$->op = $1;
2336                         $$->next = NULL;
2337                         $$->tail = $$;
2338                 }
2339                 | port PORTBINARY port          {
2340                         if ($1.t || $3.t) {
2341                                 yyerror("':' cannot be used with an other "
2342                                     "port operator");
2343                                 YYERROR;
2344                         }
2345                         $$ = calloc(1, sizeof(struct node_port));
2346                         if ($$ == NULL)
2347                                 err(1, "port_item: calloc");
2348                         $$->port[0] = $1.a;
2349                         $$->port[1] = $3.a;
2350                         $$->op = $2;
2351                         $$->next = NULL;
2352                         $$->tail = $$;
2353                 }
2354                 ;
2355
2356 port            : STRING                        {
2357                         char    *p = strchr($1, ':');
2358                         struct servent  *s = NULL;
2359                         u_long           ulval;
2360
2361                         if (p == NULL) {
2362                                 if (atoul($1, &ulval) == 0) {
2363                                         if (ulval > 65535) {
2364                                                 free($1);
2365                                                 yyerror("illegal port value %d",
2366                                                     ulval);
2367                                                 YYERROR;
2368                                         }
2369                                         $$.a = htons(ulval);
2370                                 } else {
2371                                         s = getservbyname($1, "tcp");
2372                                         if (s == NULL)
2373                                                 s = getservbyname($1, "udp");
2374                                         if (s == NULL) {
2375                                                 yyerror("unknown port %s", $1);
2376                                                 free($1);
2377                                                 YYERROR;
2378                                         }
2379                                         $$.a = s->s_port;
2380                                 }
2381                                 $$.b = 0;
2382                                 $$.t = 0;
2383                         } else {
2384                                 int port[2];
2385
2386                                 *p++ = 0;
2387                                 if ((port[0] = getservice($1)) == -1 ||
2388                                     (port[1] = getservice(p)) == -1) {
2389                                         free($1);
2390                                         YYERROR;
2391                                 }
2392                                 $$.a = port[0];
2393                                 $$.b = port[1];
2394                                 $$.t = PF_OP_RRG;
2395                         }
2396                         free($1);
2397                 }
2398                 ;
2399
2400 uids            : uid_item                      { $$ = $1; }
2401                 | '{' uid_list '}'              { $$ = $2; }
2402                 ;
2403
2404 uid_list        : uid_item                      { $$ = $1; }
2405                 | uid_list comma uid_item       {
2406                         $1->tail->next = $3;
2407                         $1->tail = $3;
2408                         $$ = $1;
2409                 }
2410                 ;
2411
2412 uid_item        : uid                           {
2413                         $$ = calloc(1, sizeof(struct node_uid));
2414                         if ($$ == NULL)
2415                                 err(1, "uid_item: calloc");
2416                         $$->uid[0] = $1;
2417                         $$->uid[1] = $1;
2418                         $$->op = PF_OP_EQ;
2419                         $$->next = NULL;
2420                         $$->tail = $$;
2421                 }
2422                 | unaryop uid                   {
2423                         if ($2 == UID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
2424                                 yyerror("user unknown requires operator = or "
2425                                     "!=");
2426                                 YYERROR;
2427                         }
2428                         $$ = calloc(1, sizeof(struct node_uid));
2429                         if ($$ == NULL)
2430                                 err(1, "uid_item: calloc");
2431                         $$->uid[0] = $2;
2432                         $$->uid[1] = $2;
2433                         $$->op = $1;
2434                         $$->next = NULL;
2435                         $$->tail = $$;
2436                 }
2437                 | uid PORTBINARY uid            {
2438                         if ($1 == UID_MAX || $3 == UID_MAX) {
2439                                 yyerror("user unknown requires operator = or "
2440                                     "!=");
2441                                 YYERROR;
2442                         }
2443                         $$ = calloc(1, sizeof(struct node_uid));
2444                         if ($$ == NULL)
2445                                 err(1, "uid_item: calloc");
2446                         $$->uid[0] = $1;
2447                         $$->uid[1] = $3;
2448                         $$->op = $2;
2449                         $$->next = NULL;
2450                         $$->tail = $$;
2451                 }
2452                 ;
2453
2454 uid             : STRING                        {
2455                         u_long  ulval;
2456
2457                         if (atoul($1, &ulval) == -1) {
2458                                 if (!strcmp($1, "unknown"))
2459                                         $$ = UID_MAX;
2460                                 else {
2461                                         struct passwd   *pw;
2462
2463                                         if ((pw = getpwnam($1)) == NULL) {
2464                                                 yyerror("unknown user %s", $1);
2465                                                 free($1);
2466                                                 YYERROR;
2467                                         }
2468                                         $$ = pw->pw_uid;
2469                                 }
2470                         } else {
2471                                 if (ulval >= UID_MAX) {
2472                                         free($1);
2473                                         yyerror("illegal uid value %lu", ulval);
2474                                         YYERROR;
2475                                 }
2476                                 $$ = ulval;
2477                         }
2478                         free($1);
2479                 }
2480                 ;
2481
2482 gids            : gid_item                      { $$ = $1; }
2483                 | '{' gid_list '}'              { $$ = $2; }
2484                 ;
2485
2486 gid_list        : gid_item                      { $$ = $1; }
2487                 | gid_list comma gid_item       {
2488                         $1->tail->next = $3;
2489                         $1->tail = $3;
2490                         $$ = $1;
2491                 }
2492                 ;
2493
2494 gid_item        : gid                           {
2495                         $$ = calloc(1, sizeof(struct node_gid));
2496                         if ($$ == NULL)
2497                                 err(1, "gid_item: calloc");
2498                         $$->gid[0] = $1;
2499                         $$->gid[1] = $1;
2500                         $$->op = PF_OP_EQ;
2501                         $$->next = NULL;
2502                         $$->tail = $$;
2503                 }
2504                 | unaryop gid                   {
2505                         if ($2 == GID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
2506                                 yyerror("group unknown requires operator = or "
2507                                     "!=");
2508                                 YYERROR;
2509                         }
2510                         $$ = calloc(1, sizeof(struct node_gid));
2511                         if ($$ == NULL)
2512                                 err(1, "gid_item: calloc");
2513                         $$->gid[0] = $2;
2514                         $$->gid[1] = $2;
2515                         $$->op = $1;
2516                         $$->next = NULL;
2517                         $$->tail = $$;
2518                 }
2519                 | gid PORTBINARY gid            {
2520                         if ($1 == GID_MAX || $3 == GID_MAX) {
2521                                 yyerror("group unknown requires operator = or "
2522                                     "!=");
2523                                 YYERROR;
2524                         }
2525                         $$ = calloc(1, sizeof(struct node_gid));
2526                         if ($$ == NULL)
2527                                 err(1, "gid_item: calloc");
2528                         $$->gid[0] = $1;
2529                         $$->gid[1] = $3;
2530                         $$->op = $2;
2531                         $$->next = NULL;
2532                         $$->tail = $$;
2533                 }
2534                 ;
2535
2536 gid             : STRING                        {
2537                         u_long  ulval;
2538
2539                         if (atoul($1, &ulval) == -1) {
2540                                 if (!strcmp($1, "unknown"))
2541                                         $$ = GID_MAX;
2542                                 else {
2543                                         struct group    *grp;
2544
2545                                         if ((grp = getgrnam($1)) == NULL) {
2546                                                 yyerror("unknown group %s", $1);
2547                                                 free($1);
2548                                                 YYERROR;
2549                                         }
2550                                         $$ = grp->gr_gid;
2551                                 }
2552                         } else {
2553                                 if (ulval >= GID_MAX) {
2554                                         yyerror("illegal gid value %lu", ulval);
2555                                         free($1);
2556                                         YYERROR;
2557                                 }
2558                                 $$ = ulval;
2559                         }
2560                         free($1);
2561                 }
2562                 ;
2563
2564 flag            : STRING                        {
2565                         int     f;
2566
2567                         if ((f = parse_flags($1)) < 0) {
2568                                 yyerror("bad flags %s", $1);
2569                                 free($1);
2570                                 YYERROR;
2571                         }
2572                         free($1);
2573                         $$.b1 = f;
2574                 }
2575                 ;
2576
2577 flags           : FLAGS flag '/' flag   { $$.b1 = $2.b1; $$.b2 = $4.b1; }
2578                 | FLAGS '/' flag        { $$.b1 = 0; $$.b2 = $3.b1; }
2579                 ;
2580
2581 icmpspec        : ICMPTYPE icmp_item            { $$ = $2; }
2582                 | ICMPTYPE '{' icmp_list '}'    { $$ = $3; }
2583                 | ICMP6TYPE icmp6_item          { $$ = $2; }
2584                 | ICMP6TYPE '{' icmp6_list '}'  { $$ = $3; }
2585                 ;
2586
2587 icmp_list       : icmp_item                     { $$ = $1; }
2588                 | icmp_list comma icmp_item     {
2589                         $1->tail->next = $3;
2590                         $1->tail = $3;
2591                         $$ = $1;
2592                 }
2593                 ;
2594
2595 icmp6_list      : icmp6_item                    { $$ = $1; }
2596                 | icmp6_list comma icmp6_item   {
2597                         $1->tail->next = $3;
2598                         $1->tail = $3;
2599                         $$ = $1;
2600                 }
2601                 ;
2602
2603 icmp_item       : icmptype              {
2604                         $$ = calloc(1, sizeof(struct node_icmp));
2605                         if ($$ == NULL)
2606                                 err(1, "icmp_item: calloc");
2607                         $$->type = $1;
2608                         $$->code = 0;
2609                         $$->proto = IPPROTO_ICMP;
2610                         $$->next = NULL;
2611                         $$->tail = $$;
2612                 }
2613                 | icmptype CODE STRING  {
2614                         const struct icmpcodeent        *p;
2615                         u_long                           ulval;
2616
2617                         if (atoul($3, &ulval) == 0) {
2618                                 if (ulval > 255) {
2619                                         free($3);
2620                                         yyerror("illegal icmp-code %d", ulval);
2621                                         YYERROR;
2622                                 }
2623                         } else {
2624                                 if ((p = geticmpcodebyname($1-1, $3,
2625                                     AF_INET)) == NULL) {
2626                                         yyerror("unknown icmp-code %s", $3);
2627                                         free($3);
2628                                         YYERROR;
2629                                 }
2630                                 ulval = p->code;
2631                         }
2632                         free($3);
2633                         $$ = calloc(1, sizeof(struct node_icmp));
2634                         if ($$ == NULL)
2635                                 err(1, "icmp_item: calloc");
2636                         $$->type = $1;
2637                         $$->code = ulval + 1;
2638                         $$->proto = IPPROTO_ICMP;
2639                         $$->next = NULL;
2640                         $$->tail = $$;
2641                 }
2642                 ;
2643
2644 icmp6_item      : icmp6type             {
2645                         $$ = calloc(1, sizeof(struct node_icmp));
2646                         if ($$ == NULL)
2647                                 err(1, "icmp_item: calloc");
2648                         $$->type = $1;
2649                         $$->code = 0;
2650                         $$->proto = IPPROTO_ICMPV6;
2651                         $$->next = NULL;
2652                         $$->tail = $$;
2653                 }
2654                 | icmp6type CODE STRING {
2655                         const struct icmpcodeent        *p;
2656                         u_long                           ulval;
2657
2658                         if (atoul($3, &ulval) == 0) {
2659                                 if (ulval > 255) {
2660                                         yyerror("illegal icmp6-code %ld",
2661                                             ulval);
2662                                         free($3);
2663                                         YYERROR;
2664                                 }
2665                         } else {
2666                                 if ((p = geticmpcodebyname($1-1, $3,
2667                                     AF_INET6)) == NULL) {
2668                                         yyerror("unknown icmp6-code %s", $3);
2669                                         free($3);
2670                                         YYERROR;
2671                                 }
2672                                 ulval = p->code;
2673                         }
2674                         free($3);
2675                         $$ = calloc(1, sizeof(struct node_icmp));
2676                         if ($$ == NULL)
2677                                 err(1, "icmp_item: calloc");
2678                         $$->type = $1;
2679                         $$->code = ulval + 1;
2680                         $$->proto = IPPROTO_ICMPV6;
2681                         $$->next = NULL;
2682                         $$->tail = $$;
2683                 }
2684                 ;
2685
2686 icmptype        : STRING                        {
2687                         const struct icmptypeent        *p;
2688                         u_long                           ulval;
2689
2690                         if (atoul($1, &ulval) == 0) {
2691                                 if (ulval > 255) {
2692                                         yyerror("illegal icmp-type %d", ulval);
2693                                         free($1);
2694                                         YYERROR;
2695                                 }
2696                                 $$ = ulval + 1;
2697                         } else {
2698                                 if ((p = geticmptypebyname($1, AF_INET)) ==
2699                                     NULL) {
2700                                         yyerror("unknown icmp-type %s", $1);
2701                                         free($1);
2702                                         YYERROR;
2703                                 }
2704                                 $$ = p->type + 1;
2705                         }
2706                         free($1);
2707                 }
2708                 ;
2709
2710 icmp6type       : STRING                        {
2711                         const struct icmptypeent        *p;
2712                         u_long                           ulval;
2713
2714                         if (atoul($1, &ulval) == 0) {
2715                                 if (ulval > 255) {
2716                                         yyerror("illegal icmp6-type %d", ulval);
2717                                         free($1);
2718                                         YYERROR;
2719                                 }
2720                                 $$ = ulval + 1;
2721                         } else {
2722                                 if ((p = geticmptypebyname($1, AF_INET6)) ==
2723                                     NULL) {
2724                                         yyerror("unknown icmp6-type %s", $1);
2725                                         free($1);
2726                                         YYERROR;
2727                                 }
2728                                 $$ = p->type + 1;
2729                         }
2730                         free($1);
2731                 }
2732                 ;
2733
2734 tos             : TOS STRING                    {
2735                         if (!strcmp($2, "lowdelay"))
2736                                 $$ = IPTOS_LOWDELAY;
2737                         else if (!strcmp($2, "throughput"))
2738                                 $$ = IPTOS_THROUGHPUT;
2739                         else if (!strcmp($2, "reliability"))
2740                                 $$ = IPTOS_RELIABILITY;
2741                         else if ($2[0] == '0' && $2[1] == 'x')
2742                                 $$ = strtoul($2, NULL, 16);
2743                         else
2744                                 $$ = strtoul($2, NULL, 10);
2745                         if (!$$ || $$ > 255) {
2746                                 yyerror("illegal tos value %s", $2);
2747                                 free($2);
2748                                 YYERROR;
2749                         }
2750                         free($2);
2751                 }
2752                 ;
2753
2754 sourcetrack     : SOURCETRACK           { $$ = PF_SRCTRACK; }
2755                 | SOURCETRACK GLOBAL    { $$ = PF_SRCTRACK_GLOBAL; }
2756                 | SOURCETRACK RULE      { $$ = PF_SRCTRACK_RULE; }
2757                 ;
2758
2759 statelock       : IFBOUND {
2760                         $$ = PFRULE_IFBOUND;
2761                 }
2762                 | GRBOUND {
2763                         $$ = PFRULE_GRBOUND;
2764                 }
2765                 | FLOATING {
2766                         $$ = 0;
2767                 }
2768                 ;
2769
2770 keep            : KEEP STATE state_opt_spec     {
2771                         $$.action = PF_STATE_NORMAL;
2772                         $$.options = $3;
2773                 }
2774                 | MODULATE STATE state_opt_spec {
2775                         $$.action = PF_STATE_MODULATE;
2776                         $$.options = $3;
2777                 }
2778                 | SYNPROXY STATE state_opt_spec {
2779                         $$.action = PF_STATE_SYNPROXY;
2780                         $$.options = $3;
2781                 }
2782                 ;
2783
2784 state_opt_spec  : '(' state_opt_list ')'        { $$ = $2; }
2785                 | /* empty */                   { $$ = NULL; }
2786                 ;
2787
2788 state_opt_list  : state_opt_item                { $$ = $1; }
2789                 | state_opt_list comma state_opt_item {
2790                         $1->tail->next = $3;
2791                         $1->tail = $3;
2792                         $$ = $1;
2793                 }
2794                 ;
2795
2796 state_opt_item  : MAXIMUM number                {
2797                         $$ = calloc(1, sizeof(struct node_state_opt));
2798                         if ($$ == NULL)
2799                                 err(1, "state_opt_item: calloc");
2800                         $$->type = PF_STATE_OPT_MAX;
2801                         $$->data.max_states = $2;
2802                         $$->next = NULL;
2803                         $$->tail = $$;
2804                 }
2805                 | NOSYNC                                {
2806                         $$ = calloc(1, sizeof(struct node_state_opt));
2807                         if ($$ == NULL)
2808                                 err(1, "state_opt_item: calloc");
2809                         $$->type = PF_STATE_OPT_NOSYNC;
2810                         $$->next = NULL;
2811                         $$->tail = $$;
2812                 }
2813                 | MAXSRCSTATES number                   {
2814                         $$ = calloc(1, sizeof(struct node_state_opt));
2815                         if ($$ == NULL)
2816                                 err(1, "state_opt_item: calloc");
2817                         $$->type = PF_STATE_OPT_MAX_SRC_STATES;
2818                         $$->data.max_src_states = $2;
2819                         $$->next = NULL;
2820                         $$->tail = $$;
2821                 }
2822                 | MAXSRCNODES number                    {
2823                         $$ = calloc(1, sizeof(struct node_state_opt));
2824                         if ($$ == NULL)
2825                                 err(1, "state_opt_item: calloc");
2826                         $$->type = PF_STATE_OPT_MAX_SRC_NODES;
2827                         $$->data.max_src_nodes = $2;
2828                         $$->next = NULL;
2829                         $$->tail = $$;
2830                 }
2831                 | sourcetrack {
2832                         $$ = calloc(1, sizeof(struct node_state_opt));
2833                         if ($$ == NULL)
2834                                 err(1, "state_opt_item: calloc");
2835                         $$->type = PF_STATE_OPT_SRCTRACK;
2836                         $$->data.src_track = $1;
2837                         $$->next = NULL;
2838                         $$->tail = $$;
2839                 }
2840                 | statelock {
2841                         $$ = calloc(1, sizeof(struct node_state_opt));
2842                         if ($$ == NULL)
2843                                 err(1, "state_opt_item: calloc");
2844                         $$->type = PF_STATE_OPT_STATELOCK;
2845                         $$->data.statelock = $1;
2846                         $$->next = NULL;
2847                         $$->tail = $$;
2848                 }
2849                 | STRING number                 {
2850                         int     i;
2851
2852                         for (i = 0; pf_timeouts[i].name &&
2853                             strcmp(pf_timeouts[i].name, $1); ++i)
2854                                 ;       /* nothing */
2855                         if (!pf_timeouts[i].name) {
2856                                 yyerror("illegal timeout name %s", $1);
2857                                 free($1);
2858                                 YYERROR;
2859                         }
2860                         if (strchr(pf_timeouts[i].name, '.') == NULL) {
2861                                 yyerror("illegal state timeout %s", $1);
2862                                 free($1);
2863                                 YYERROR;
2864                         }
2865                         free($1);
2866                         $$ = calloc(1, sizeof(struct node_state_opt));
2867                         if ($$ == NULL)
2868                                 err(1, "state_opt_item: calloc");
2869                         $$->type = PF_STATE_OPT_TIMEOUT;
2870                         $$->data.timeout.number = pf_timeouts[i].timeout;
2871                         $$->data.timeout.seconds = $2;
2872                         $$->next = NULL;
2873                         $$->tail = $$;
2874                 }
2875                 ;
2876
2877 label           : LABEL STRING                  {
2878                         $$ = $2;
2879                 }
2880                 ;
2881
2882 qname           : QUEUE STRING                          {
2883                         $$.qname = $2;
2884                 }
2885                 | QUEUE '(' STRING ')'                  {
2886                         $$.qname = $3;
2887                 }
2888                 | QUEUE '(' STRING comma STRING ')'     {
2889                         $$.qname = $3;
2890                         $$.pqname = $5;
2891                 }
2892                 ;
2893
2894 no              : /* empty */                   { $$ = 0; }
2895                 | NO                            { $$ = 1; }
2896                 ;
2897
2898 rport           : STRING                        {
2899                         char    *p = strchr($1, ':');
2900
2901                         if (p == NULL) {
2902                                 if (($$.a = getservice($1)) == -1) {
2903                                         free($1);
2904                                         YYERROR;
2905                                 }
2906                                 $$.b = $$.t = 0;
2907                         } else if (!strcmp(p+1, "*")) {
2908                                 *p = 0;
2909                                 if (($$.a = getservice($1)) == -1) {
2910                                         free($1);
2911                                         YYERROR;
2912                                 }
2913                                 $$.b = 0;
2914                                 $$.t = 1;
2915                         } else {
2916                                 *p++ = 0;
2917                                 if (($$.a = getservice($1)) == -1 ||
2918                                     ($$.b = getservice(p)) == -1) {
2919                                         free($1);
2920                                         YYERROR;
2921                                 }
2922                                 if ($$.a == $$.b)
2923                                         $$.b = 0;
2924                                 $$.t = 0;
2925                         }
2926                         free($1);
2927                 }
2928                 ;
2929
2930 redirspec       : host                          { $$ = $1; }
2931                 | '{' redir_host_list '}'       { $$ = $2; }
2932                 ;
2933
2934 redir_host_list : host                          { $$ = $1; }
2935                 | redir_host_list comma host    {
2936                         $1->tail->next = $3;
2937                         $1->tail = $3->tail;
2938                         $$ = $1;
2939                 }
2940                 ;
2941
2942 redirpool       : /* empty */                   { $$ = NULL; }
2943                 | ARROW redirspec               {
2944                         $$ = calloc(1, sizeof(struct redirection));
2945                         if ($$ == NULL)
2946                                 err(1, "redirection: calloc");
2947                         $$->host = $2;
2948                         $$->rport.a = $$->rport.b = $$->rport.t = 0;
2949                 }
2950                 | ARROW redirspec PORT rport    {
2951                         $$ = calloc(1, sizeof(struct redirection));
2952                         if ($$ == NULL)
2953                                 err(1, "redirection: calloc");
2954                         $$->host = $2;
2955                         $$->rport = $4;
2956                 }
2957                 ;
2958
2959 hashkey         : /* empty */
2960                 {
2961                         $$ = calloc(1, sizeof(struct pf_poolhashkey));
2962                         if ($$ == NULL)
2963                                 err(1, "hashkey: calloc");
2964                         $$->key32[0] = arc4random();
2965                         $$->key32[1] = arc4random();
2966                         $$->key32[2] = arc4random();
2967                         $$->key32[3] = arc4random();
2968                 }
2969                 | string
2970                 {
2971                         if (!strncmp($1, "0x", 2)) {
2972                                 if (strlen($1) != 34) {
2973                                         free($1);
2974                                         yyerror("hex key must be 128 bits "
2975                                                 "(32 hex digits) long");
2976                                         YYERROR;
2977                                 }
2978                                 $$ = calloc(1, sizeof(struct pf_poolhashkey));
2979                                 if ($$ == NULL)
2980                                         err(1, "hashkey: calloc");
2981
2982                                 if (sscanf($1, "0x%8x%8x%8x%8x",
2983                                     &$$->key32[0], &$$->key32[1],
2984                                     &$$->key32[2], &$$->key32[3]) != 4) {
2985                                         free($$);
2986                                         free($1);
2987                                         yyerror("invalid hex key");
2988                                         YYERROR;
2989                                 }
2990                         } else {
2991                                 MD5_CTX context;
2992
2993                                 $$ = calloc(1, sizeof(struct pf_poolhashkey));
2994                                 if ($$ == NULL)
2995                                         err(1, "hashkey: calloc");
2996                                 MD5Init(&context);
2997                                 MD5Update(&context, (unsigned char *)$1,
2998                                     strlen($1));
2999                                 MD5Final((unsigned char *)$$, &context);
3000                                 $$->key32[0] = htonl($$->key32[0]);
3001                                 $$->key32[1] = htonl($$->key32[1]);
3002                                 $$->key32[2] = htonl($$->key32[2]);
3003                                 $$->key32[3] = htonl($$->key32[3]);
3004                         }
3005                         free($1);
3006                 }
3007                 ;
3008
3009 pool_opts       :       { bzero(&pool_opts, sizeof pool_opts); }
3010                     pool_opts_l
3011                         { $$ = pool_opts; }
3012                 | /* empty */   {
3013                         bzero(&pool_opts, sizeof pool_opts);
3014                         $$ = pool_opts;
3015                 }
3016                 ;
3017
3018 pool_opts_l     : pool_opts_l pool_opt
3019                 | pool_opt
3020                 ;
3021
3022 pool_opt        : BITMASK       {
3023                         if (pool_opts.type) {
3024                                 yyerror("pool type cannot be redefined");
3025                                 YYERROR;
3026                         }
3027                         pool_opts.type =  PF_POOL_BITMASK;
3028                 }
3029                 | RANDOM        {
3030                         if (pool_opts.type) {
3031                                 yyerror("pool type cannot be redefined");
3032                                 YYERROR;
3033                         }
3034                         pool_opts.type = PF_POOL_RANDOM;
3035                 }
3036                 | SOURCEHASH hashkey {
3037                         if (pool_opts.type) {
3038                                 yyerror("pool type cannot be redefined");
3039                                 YYERROR;
3040                         }
3041                         pool_opts.type = PF_POOL_SRCHASH;
3042                         pool_opts.key = $2;
3043                 }
3044                 | ROUNDROBIN    {
3045                         if (pool_opts.type) {
3046                                 yyerror("pool type cannot be redefined");
3047                                 YYERROR;
3048                         }
3049                         pool_opts.type = PF_POOL_ROUNDROBIN;
3050                 }
3051                 | STATICPORT    {
3052                         if (pool_opts.staticport) {
3053                                 yyerror("static-port cannot be redefined");
3054                                 YYERROR;
3055                         }
3056                         pool_opts.staticport = 1;
3057                 }
3058                 | STICKYADDRESS {
3059                         if (filter_opts.marker & POM_STICKYADDRESS) {
3060                                 yyerror("sticky-address cannot be redefined");
3061                                 YYERROR;
3062                         }
3063                         pool_opts.marker |= POM_STICKYADDRESS;
3064                         pool_opts.opts |= PF_POOL_STICKYADDR;
3065                 }
3066                 ;
3067
3068 redirection     : /* empty */                   { $$ = NULL; }
3069                 | ARROW host                    {
3070                         $$ = calloc(1, sizeof(struct redirection));
3071                         if ($$ == NULL)
3072                                 err(1, "redirection: calloc");
3073                         $$->host = $2;
3074                         $$->rport.a = $$->rport.b = $$->rport.t = 0;
3075                 }
3076                 | ARROW host PORT rport {
3077                         $$ = calloc(1, sizeof(struct redirection));
3078                         if ($$ == NULL)
3079                                 err(1, "redirection: calloc");
3080                         $$->host = $2;
3081                         $$->rport = $4;
3082                 }
3083                 ;
3084
3085 natpass         : /* empty */   { $$ = 0; }
3086                 | PASS          { $$ = 1; }
3087                 ;
3088
3089 nataction       : no NAT natpass {
3090                         $$.b2 = $$.w = 0;
3091                         if ($1)
3092                                 $$.b1 = PF_NONAT;
3093                         else
3094                                 $$.b1 = PF_NAT;
3095                         $$.b2 = $3;
3096                 }
3097                 | no RDR natpass {
3098                         $$.b2 = $$.w = 0;
3099                         if ($1)
3100                                 $$.b1 = PF_NORDR;
3101                         else
3102                                 $$.b1 = PF_RDR;
3103                         $$.b2 = $3;
3104                 }
3105                 ;
3106
3107 natrule         : nataction interface af proto fromto tag redirpool pool_opts
3108                 {
3109                         struct pf_rule  r;
3110
3111                         if (check_rulestate(PFCTL_STATE_NAT))
3112                                 YYERROR;
3113
3114                         memset(&r, 0, sizeof(r));
3115
3116                         r.action = $1.b1;
3117                         r.natpass = $1.b2;
3118                         r.af = $3;
3119
3120                         if (!r.af) {
3121                                 if ($5.src.host && $5.src.host->af &&
3122                                     !$5.src.host->ifindex)
3123                                         r.af = $5.src.host->af;
3124                                 else if ($5.dst.host && $5.dst.host->af &&
3125                                     !$5.dst.host->ifindex)
3126                                         r.af = $5.dst.host->af;
3127                         }
3128
3129                         if ($6 != NULL)
3130                                 if (strlcpy(r.tagname, $6, PF_TAG_NAME_SIZE) >=
3131                                     PF_TAG_NAME_SIZE) {
3132                                         yyerror("tag too long, max %u chars",
3133                                             PF_TAG_NAME_SIZE - 1);
3134                                         YYERROR;
3135                                 }
3136
3137                         if (r.action == PF_NONAT || r.action == PF_NORDR) {
3138                                 if ($7 != NULL) {
3139                                         yyerror("translation rule with 'no' "
3140                                             "does not need '->'");
3141                                         YYERROR;
3142                                 }
3143                         } else {
3144                                 if ($7 == NULL || $7->host == NULL) {
3145                                         yyerror("translation rule requires '-> "
3146                                             "address'");
3147                                         YYERROR;
3148                                 }
3149                                 if (!r.af && ! $7->host->ifindex)
3150                                         r.af = $7->host->af;
3151
3152                                 remove_invalid_hosts(&$7->host, &r.af);
3153                                 if (invalid_redirect($7->host, r.af))
3154                                         YYERROR;
3155                                 if (check_netmask($7->host, r.af))
3156                                         YYERROR;
3157
3158                                 r.rpool.proxy_port[0] = ntohs($7->rport.a);
3159
3160                                 switch (r.action) {
3161                                 case PF_RDR:
3162                                         if (!$7->rport.b && $7->rport.t &&
3163                                             $5.dst.port != NULL) {
3164                                                 r.rpool.proxy_port[1] =
3165                                                     ntohs($7->rport.a) +
3166                                                     (ntohs(
3167                                                     $5.dst.port->port[1]) -
3168                                                     ntohs(