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