Add fairq to altq. Fairq is a fair queueing algorithm with bandwidth
[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                                         } else if (!r.rpool.proxy_port[1])
3158                                                 r.rpool.proxy_port[1] =
3159                                                     r.rpool.proxy_port[0];
3160                                         break;
3161                                 default:
3162                                         break;
3163                                 }
3164
3165                                 r.rpool.opts = $8.type;
3166                                 if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
3167                                     PF_POOL_NONE && ($7->host->next != NULL ||
3168                                     $7->host->addr.type == PF_ADDR_TABLE ||
3169                                     DYNIF_MULTIADDR($7->host->addr)))
3170                                         r.rpool.opts = PF_POOL_ROUNDROBIN;
3171                                 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
3172                                     PF_POOL_ROUNDROBIN &&
3173                                     disallow_table($7->host, "tables are only "
3174                                     "supported in round-robin redirection "
3175                                     "pools"))
3176                                         YYERROR;
3177                                 if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
3178                                     PF_POOL_ROUNDROBIN &&
3179                                     disallow_alias($7->host, "interface (%s) "
3180                                     "is only supported in round-robin "
3181                                     "redirection pools"))
3182                                         YYERROR;
3183                                 if ($7->host->next != NULL) {
3184                                         if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
3185                                             PF_POOL_ROUNDROBIN) {
3186                                                 yyerror("only round-robin "
3187                                                     "valid for multiple "
3188                                                     "redirection addresses");
3189                                                 YYERROR;
3190                                         }
3191                                 }
3192                         }
3193
3194                         if ($8.key != NULL)
3195                                 memcpy(&r.rpool.key, $8.key,
3196                                     sizeof(struct pf_poolhashkey));
3197
3198                          if ($8.opts)
3199                                 r.rpool.opts |= $8.opts;
3200
3201                         if ($8.staticport) {
3202                                 if (r.action != PF_NAT) {
3203                                         yyerror("the 'static-port' option is "
3204                                             "only valid with nat rules");
3205                                         YYERROR;
3206                                 }
3207                                 if (r.rpool.proxy_port[0] !=
3208                                     PF_NAT_PROXY_PORT_LOW &&
3209                                     r.rpool.proxy_port[1] !=
3210                                     PF_NAT_PROXY_PORT_HIGH) {
3211                                         yyerror("the 'static-port' option can't"
3212                                             " be used when specifying a port"
3213                                             " range");
3214                                         YYERROR;
3215                                 }
3216                                 r.rpool.proxy_port[0] = 0;
3217                                 r.rpool.proxy_port[1] = 0;
3218                         }
3219
3220                         expand_rule(&r, $2, $7 == NULL ? NULL : $7->host, $4,
3221                             $5.src_os, $5.src.host, $5.src.port, $5.dst.host,
3222                             $5.dst.port, 0, 0, 0);
3223                         free($7);
3224                 }
3225                 ;
3226
3227 binatrule       : no BINAT natpass interface af proto FROM host TO ipspec tag
3228                     redirection
3229                 {
3230                         struct pf_rule          binat;
3231                         struct pf_pooladdr      *pa;
3232
3233                         if (check_rulestate(PFCTL_STATE_NAT))
3234                                 YYERROR;
3235
3236                         memset(&binat, 0, sizeof(binat));
3237
3238                         if ($1)
3239                                 binat.action = PF_NOBINAT;
3240                         else
3241                                 binat.action = PF_BINAT;
3242                         binat.natpass = $3;
3243                         binat.af = $5;
3244                         if (!binat.af && $8 != NULL && $8->af)
3245                                 binat.af = $8->af;
3246                         if (!binat.af && $10 != NULL && $10->af)
3247                                 binat.af = $10->af;
3248                         if (!binat.af && $12 != NULL && $12->host)
3249                                 binat.af = $12->host->af;
3250                         if (!binat.af) {
3251                                 yyerror("address family (inet/inet6) "
3252                                     "undefined");
3253                                 YYERROR;
3254                         }
3255
3256                         if ($4 != NULL) {
3257                                 memcpy(binat.ifname, $4->ifname,
3258                                     sizeof(binat.ifname));
3259                                 binat.ifnot = $4->not;
3260                                 free($4);
3261                         }
3262                         if ($11 != NULL)
3263                                 if (strlcpy(binat.tagname, $11,
3264                                     PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
3265                                         yyerror("tag too long, max %u chars",
3266                                             PF_TAG_NAME_SIZE - 1);
3267                                         YYERROR;
3268                                 }
3269
3270                         if ($6 != NULL) {
3271                                 binat.proto = $6->proto;
3272                                 free($6);
3273                         }
3274
3275                         if ($8 != NULL && disallow_table($8, "invalid use of "
3276                             "table <%s> as the source address of a binat rule"))
3277                                 YYERROR;
3278                         if ($8 != NULL && disallow_alias($8, "invalid use of "
3279                             "interface (%s) as the source address of a binat "
3280                             "rule"))
3281                                 YYERROR;
3282                         if ($12 != NULL && $12->host != NULL && disallow_table(
3283                             $12->host, "invalid use of table <%s> as the "
3284                             "redirect address of a binat rule"))
3285                                 YYERROR;
3286                         if ($12 != NULL && $12->host != NULL && disallow_alias(
3287                             $12->host, "invalid use of interface (%s) as the "
3288                             "redirect address of a binat rule"))
3289                                 YYERROR;
3290
3291                         if ($8 != NULL) {
3292                                 if ($8->next) {
3293                                         yyerror("multiple binat ip addresses");
3294                                         YYERROR;
3295                                 }
3296                                 if ($8->addr.type == PF_ADDR_DYNIFTL)
3297                                         $8->af = binat.af;
3298                                 if ($8->af != binat.af) {
3299                                         yyerror("binat ip versions must match");
3300                                         YYERROR;
3301                                 }
3302                                 if (check_netmask($8, binat.af))
3303                                         YYERROR;
3304                                 memcpy(&binat.src.addr, &$8->addr,
3305                                     sizeof(binat.src.addr));
3306                                 free($8);
3307                         }
3308                         if ($10 != NULL) {
3309                                 if ($10->next) {
3310                                         yyerror("multiple binat ip addresses");
3311                                         YYERROR;
3312                                 }
3313                                 if ($10->af != binat.af && $10->af) {
3314                                         yyerror("binat ip versions must match");
3315                                         YYERROR;
3316                                 }
3317                                 if (check_netmask($10, binat.af))
3318                                         YYERROR;
3319                                 memcpy(&binat.dst.addr, &$10->addr,
3320                                     sizeof(binat.dst.addr));
3321                                 binat.dst.not = $10->not;
3322                                 free($10);
3323                         }
3324
3325                         if (binat.action == PF_NOBINAT) {
3326                                 if ($12 != NULL) {
3327                                         yyerror("'no binat' rule does not need"
3328                                             " '->'");
3329                                         YYERROR;
3330                                 }
3331                         } else {
3332                                 if ($12 == NULL || $12->host == NULL) {
3333                                         yyerror("'binat' rule requires"
3334                                             " '-> address'");
3335                                         YYERROR;
3336                                 }
3337
3338                                 remove_invalid_hosts(&$12->host, &binat.af);
3339                                 if (invalid_redirect($12->host, binat.af))
3340                                         YYERROR;
3341                                 if ($12->host->next != NULL) {
3342                                         yyerror("binat rule must redirect to "
3343                                             "a single address");
3344                                         YYERROR;
3345                                 }
3346                                 if (check_netmask($12->host, binat.af))
3347                                         YYERROR;
3348
3349                                 if (!PF_AZERO(&binat.src.addr.v.a.mask,
3350                                     binat.af) &&
3351                                     !PF_AEQ(&binat.src.addr.v.a.mask,
3352                                     &$12->host->addr.v.a.mask, binat.af)) {
3353                                         yyerror("'binat' source mask and "
3354                                             "redirect mask must be the same");
3355                                         YYERROR;
3356                                 }
3357
3358                                 TAILQ_INIT(&binat.rpool.list);
3359                                 pa = calloc(1, sizeof(struct pf_pooladdr));
3360                                 if (pa == NULL)
3361                                         err(1, "binat: calloc");
3362                                 pa->addr = $12->host->addr;
3363                                 pa->ifname[0] = 0;
3364                                 TAILQ_INSERT_TAIL(&binat.rpool.list,
3365                                     pa, entries);
3366
3367                                 free($12);
3368                         }
3369
3370                         pfctl_add_rule(pf, &binat);
3371                 }
3372                 ;
3373
3374 tag             : /* empty */           { $$ = NULL; }
3375                 | TAG STRING            { $$ = $2; }
3376                 ;
3377
3378 route_host      : STRING                        {
3379                         $$ = calloc(1, sizeof(struct node_host));
3380                         if ($$ == NULL)
3381                                 err(1, "route_host: calloc");
3382                         $$->ifname = $1;
3383                         if (ifa_exists($$->ifname, 0) == NULL) {
3384                                 yyerror("routeto: unknown interface %s",
3385                                     $$->ifname);
3386                                 free($1);
3387                                 free($$);
3388                                 YYERROR;
3389                         }
3390                         set_ipmask($$, 128);
3391                         $$->next = NULL;
3392                         $$->tail = $$;
3393                 }
3394                 | '(' STRING host ')'           {
3395                         $$ = $3;
3396                         $$->ifname = $2;
3397                         if (ifa_exists($$->ifname, 0) == NULL) {
3398                                 yyerror("routeto: unknown interface %s",
3399                                     $$->ifname);
3400                                 YYERROR;
3401                         }
3402                 }
3403                 ;
3404
3405 route_host_list : route_host                            { $$ = $1; }
3406                 | route_host_list comma route_host      {
3407                         if ($1->af == 0)
3408                                 $1->af = $3->af;
3409                         if ($1->af != $3->af) {
3410                                 yyerror("all pool addresses must be in the "
3411                                     "same address family");
3412                                 YYERROR;
3413                         }
3414                         $1->tail->next = $3;
3415                         $1->tail = $3->tail;
3416                         $$ = $1;
3417                 }
3418                 ;
3419
3420 routespec       : route_host                    { $$ = $1; }
3421                 | '{' route_host_list '}'       { $$ = $2; }
3422                 ;
3423
3424 route           : /* empty */                   {
3425                         $$.host = NULL;
3426                         $$.rt = 0;
3427                         $$.pool_opts = 0;
3428                 }
3429                 | FASTROUTE {
3430                         $$.host = NULL;
3431                         $$.rt = PF_FASTROUTE;
3432                         $$.pool_opts = 0;
3433                 }
3434                 | ROUTETO routespec pool_opts {
3435                         $$.host = $2;
3436                         $$.rt = PF_ROUTETO;
3437                         $$.pool_opts = $3.type | $3.opts;
3438                         if ($3.key != NULL)
3439                                 $$.key = $3.key;
3440                 }
3441                 | REPLYTO routespec pool_opts {
3442                         $$.host = $2;
3443                         $$.rt = PF_REPLYTO;
3444                         $$.pool_opts = $3.type | $3.opts;
3445                         if ($3.key != NULL)
3446                                 $$.key = $3.key;
3447                 }
3448                 | DUPTO routespec pool_opts {
3449                         $$.host = $2;
3450                         $$.rt = PF_DUPTO;
3451                         $$.pool_opts = $3.type | $3.opts;
3452                         if ($3.key != NULL)
3453                                 $$.key = $3.key;
3454                 }
3455                 ;
3456
3457 timeout_spec    : STRING number
3458                 {
3459                         if (check_rulestate(PFCTL_STATE_OPTION)) {
3460                                 free($1);
3461                                 YYERROR;
3462                         }
3463                         if (pfctl_set_timeout(pf, $1, $2, 0) != 0) {
3464                                 yyerror("unknown timeout %s", $1);
3465                                 free($1);
3466                                 YYERROR;
3467                         }
3468                         free($1);
3469                 }
3470                 ;
3471
3472 timeout_list    : timeout_list comma timeout_spec
3473                 | timeout_spec
3474                 ;
3475
3476 limit_spec      : STRING number
3477                 {
3478                         if (check_rulestate(PFCTL_STATE_OPTION)) {
3479                                 free($1);
3480                                 YYERROR;
3481                         }
3482                         if (pfctl_set_limit(pf, $1, $2) != 0) {
3483                                 yyerror("unable to set limit %s %u", $1, $2);
3484                                 free($1);
3485                                 YYERROR;
3486                         }
3487                         free($1);
3488                 }
3489                 ;
3490
3491 limit_list      : limit_list comma limit_spec
3492                 | limit_spec
3493                 ;
3494
3495 comma           : ','
3496                 | /* empty */
3497                 ;
3498
3499 yesno           : NO                    { $$ = 0; }
3500                 | STRING                {
3501                         if (!strcmp($1, "yes"))
3502                                 $$ = 1;
3503                         else {
3504                                 free($1);
3505                                 YYERROR;
3506                         }
3507                         free($1);
3508                 }
3509                 ;
3510
3511 unaryop         : '='           { $$ = PF_OP_EQ; }
3512                 | '!' '='       { $$ = PF_OP_NE; }
3513                 | '<' '='       { $$ = PF_OP_LE; }
3514                 | '<'           { $$ = PF_OP_LT; }
3515                 | '>' '='       { $$ = PF_OP_GE; }
3516                 | '>'           { $$ = PF_OP_GT; }
3517                 ;
3518
3519 %%
3520
3521 int
3522 yyerror(const char *fmt, ...)
3523 {
3524         va_list          ap;
3525
3526         errors = 1;
3527         va_start(ap, fmt);
3528         fprintf(stderr, "%s:%d: ", infile, yylval.lineno);
3529         vfprintf(stderr, fmt, ap);
3530         fprintf(stderr, "\n");
3531         va_end(ap);
3532         return (0);
3533 }
3534
3535 int
3536 disallow_table(struct node_host *h, const char *fmt)
3537 {
3538         for (; h != NULL; h = h->next)
3539                 if (h->addr.type == PF_ADDR_TABLE) {
3540                         yyerror(fmt, h->addr.v.tblname);
3541                         return (1);
3542                 }
3543         return (0);
3544 }
3545
3546 int
3547 disallow_alias(struct node_host *h, const char *fmt)
3548 {
3549         for (; h != NULL; h = h->next)
3550                 if (DYNIF_MULTIADDR(h->addr)) {
3551                         yyerror(fmt, h->addr.v.tblname);
3552                         return (1);
3553                 }
3554         return (0);
3555 }
3556
3557 int
3558 rule_consistent(struct pf_rule *r)
3559 {
3560         int     problems = 0;
3561
3562         switch (r->action) {
3563         case PF_PASS:
3564         case PF_DROP:
3565         case PF_SCRUB:
3566                 problems = filter_consistent(r);
3567                 break;
3568         case PF_NAT:
3569         case PF_NONAT:
3570                 problems = nat_consistent(r);
3571                 break;
3572         case PF_RDR:
3573         case PF_NORDR:
3574                 problems = rdr_consistent(r);
3575                 break;
3576         case PF_BINAT:
3577         case PF_NOBINAT:
3578         default:
3579                 break;
3580         }
3581         return (problems);
3582 }
3583
3584 int
3585 filter_consistent(struct pf_rule *r)
3586 {
3587         int     problems = 0;
3588
3589         if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP &&
3590             (r->src.port_op || r->dst.port_op)) {
3591                 yyerror("port only applies to tcp/udp");
3592                 problems++;
3593         }
3594         if (r->proto != IPPROTO_ICMP && r->proto != IPPROTO_ICMPV6 &&
3595             (r->type || r->code)) {
3596                 yyerror("icmp-type/code only applies to icmp");
3597                 problems++;
3598         }
3599         if (!r->af && (r->type || r->code)) {
3600                 yyerror("must indicate address family with icmp-type/code");
3601                 problems++;
3602         }
3603         if ((r->proto == IPPROTO_ICMP && r->af == AF_INET6) ||
3604             (r->proto == IPPROTO_ICMPV6 && r->af == AF_INET)) {
3605                 yyerror("proto %s doesn't match address family %s",
3606                     r->proto == IPPROTO_ICMP ? "icmp" : "icmp6",
3607                     r->af == AF_INET ? "inet" : "inet6");
3608                 problems++;
3609         }
3610         if (r->allow_opts && r->action != PF_PASS) {
3611                 yyerror("allow-opts can only be specified for pass rules");
3612                 problems++;
3613         }
3614         if (r->rule_flag & PFRULE_FRAGMENT && (r->src.port_op ||
3615             r->dst.port_op || r->flagset || r->type || r->code)) {
3616                 yyerror("fragments can be filtered only on IP header fields");
3617                 problems++;
3618         }
3619         if (r->rule_flag & PFRULE_RETURNRST && r->proto != IPPROTO_TCP) {
3620                 yyerror("return-rst can only be applied to TCP rules");
3621                 problems++;
3622         }
3623         if (r->max_src_nodes && !(r->rule_flag & PFRULE_RULESRCTRACK)) {
3624                 yyerror("max-src-nodes requires 'source-track rule'");
3625                 problems++;
3626         }
3627         if (r->action == PF_DROP && r->keep_state) {
3628                 yyerror("keep state on block rules doesn't make sense");
3629                 problems++;
3630         }
3631         if ((r->tagname[0] || r->match_tagname[0]) && !r->keep_state &&
3632             r->action == PF_PASS && !r->anchorname[0]) {
3633                 yyerror("tags cannot be used without keep state");
3634                 problems++;
3635         }
3636         return (-problems);
3637 }
3638
3639 int
3640 nat_consistent(struct pf_rule *r __unused)
3641 {
3642         return (0);     /* yeah! */
3643 }
3644
3645 int
3646 rdr_consistent(struct pf_rule *r)
3647 {
3648         int                      problems = 0;
3649
3650         if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP) {
3651                 if (r->src.port_op) {
3652                         yyerror("src port only applies to tcp/udp");
3653                         problems++;
3654                 }
3655                 if (r->dst.port_op) {
3656                         yyerror("dst port only applies to tcp/udp");
3657                         problems++;
3658                 }
3659                 if (r->rpool.proxy_port[0]) {
3660                         yyerror("rpool port only applies to tcp/udp");
3661                         problems++;
3662                 }
3663         }
3664         if (r->dst.port_op &&
3665             r->dst.port_op != PF_OP_EQ && r->dst.port_op != PF_OP_RRG) {
3666                 yyerror("invalid port operator for rdr destination port");
3667                 problems++;
3668         }
3669         return (-problems);
3670 }
3671
3672 int
3673 process_tabledef(char *name, struct table_opts *opts)
3674 {
3675         struct pfr_buffer        ab;
3676         struct node_tinit       *ti;
3677
3678         bzero(&ab, sizeof(ab));
3679         ab.pfrb_type = PFRB_ADDRS;
3680         SIMPLEQ_FOREACH(ti, &opts->init_nodes, entries) {
3681                 if (ti->file)
3682                         if (pfr_buf_load(&ab, ti->file, 0, append_addr)) {
3683                                 if (errno)
3684                                         yyerror("cannot load \"%s\": %s",
3685                                             ti->file, strerror(errno));
3686                                 else
3687                                         yyerror("file \"%s\" contains bad data",
3688                                             ti->file);
3689                                 goto _error;
3690                         }
3691                 if (ti->host)
3692                         if (append_addr_host(&ab, ti->host, 0, 0)) {
3693                                 yyerror("cannot create address buffer: %s",
3694                                     strerror(errno));
3695                                 goto _error;
3696                         }
3697         }
3698         if (pf->opts & PF_OPT_VERBOSE)
3699                 print_tabledef(name, opts->flags, opts->init_addr,
3700                     &opts->init_nodes);
3701         if (!(pf->opts & PF_OPT_NOACTION) &&
3702             pfctl_define_table(name, opts->flags, opts->init_addr,
3703             pf->anchor, pf->ruleset, &ab, pf->tticket)) {
3704                 yyerror("cannot define table %s: %s", name,
3705                     pfr_strerror(errno));
3706                 goto _error;
3707         }
3708         pf->tdirty = 1;
3709         pfr_buf_clear(&ab);
3710         return (0);
3711 _error:
3712         pfr_buf_clear(&ab);
3713         return (-1);
3714 }
3715
3716 struct keywords {
3717         const char      *k_name;
3718         int              k_val;
3719 };
3720
3721 /* macro gore, but you should've seen the prior indentation nightmare... */
3722
3723 #define FREE_LIST(T,r) \
3724         do { \
3725                 T *p, *node = r; \
3726                 while (node != NULL) { \
3727                         p = node; \
3728                         node = node->next; \
3729                         free(p); \
3730                 } \
3731         } while (0)
3732
3733 #define LOOP_THROUGH(T,n,r,C) \
3734         do { \
3735                 T *n; \
3736                 if (r == NULL) { \
3737                         r = calloc(1, sizeof(T)); \
3738                         if (r == NULL) \
3739                                 err(1, "LOOP: calloc"); \
3740                         r->next = NULL; \
3741                 } \
3742                 n = r; \
3743                 while (n != NULL) { \
3744                         do { \
3745                                 C; \
3746                         } while (0); \
3747                         n = n->next; \
3748                 } \
3749         } while (0)
3750
3751 void
3752 expand_label_str(char *label, size_t len, const char *srch, const char *repl)
3753 {
3754         char *tmp;
3755         char *p, *q;
3756
3757         if ((tmp = calloc(1, len)) == NULL)
3758                 err(1, "expand_label_str: calloc");
3759         p = q = label;
3760         while ((q = strstr(p, srch)) != NULL) {
3761                 *q = '\0';
3762                 if ((strlcat(tmp, p, len) >= len) ||
3763                     (strlcat(tmp, repl, len) >= len))
3764                         errx(1, "expand_label: label too long");
3765                 q += strlen(srch);
3766                 p = q;
3767         }
3768         if (strlcat(tmp, p, len) >= len)
3769                 errx(1, "expand_label: label too long");
3770         strlcpy(label, tmp, len);       /* always fits */
3771         free(tmp);
3772 }
3773
3774 void
3775 expand_label_if(const char *name, char *label, size_t len, const char *ifname)
3776 {
3777         if (strstr(label, name) != NULL) {
3778                 if (!*ifname)
3779                         expand_label_str(label, len, name, "any");
3780                 else
3781                         expand_label_str(label, len, name, ifname);
3782         }
3783 }
3784
3785 void
3786 expand_label_addr(const char *name, char *label, size_t len, sa_family_t af,
3787     struct node_host *h)
3788 {
3789         char tmp[64], tmp_not[66];
3790
3791         if (strstr(label, name) != NULL) {
3792                 switch (h->addr.type) {
3793                 case PF_ADDR_DYNIFTL:
3794                         snprintf(tmp, sizeof(tmp), "(%s)", h->addr.v.ifname);
3795                         break;
3796                 case PF_ADDR_TABLE:
3797                         snprintf(tmp, sizeof(tmp), "<%s>", h->addr.v.tblname);
3798                         break;
3799                 case PF_ADDR_NOROUTE:
3800                         snprintf(tmp, sizeof(tmp), "no-route");
3801                         break;
3802                 case PF_ADDR_ADDRMASK:
3803                         if (!af || (PF_AZERO(&h->addr.v.a.addr, af) &&
3804                             PF_AZERO(&h->addr.v.a.mask, af)))
3805                                 snprintf(tmp, sizeof(tmp), "any");
3806                         else {
3807                                 char    a[48];
3808                                 int     bits;
3809
3810                                 if (inet_ntop(af, &h->addr.v.a.addr, a,
3811                                     sizeof(a)) == NULL)
3812                                         snprintf(tmp, sizeof(tmp), "?");
3813                                 else {
3814                                         bits = unmask(&h->addr.v.a.mask, af);
3815                                         if ((af == AF_INET && bits < 32) ||
3816                                             (af == AF_INET6 && bits < 128))
3817                                                 snprintf(tmp, sizeof(tmp),
3818                                                     "%s/%d", a, bits);
3819                                         else
3820                                                 snprintf(tmp, sizeof(tmp),
3821                                                     "%s", a);
3822                                 }
3823                         }
3824                         break;
3825                 default:
3826                         snprintf(tmp, sizeof(tmp), "?");
3827                         break;
3828                 }
3829
3830                 if (h->not) {
3831                         snprintf(tmp_not, sizeof(tmp_not), "! %s", tmp);
3832                         expand_label_str(label, len, name, tmp_not);
3833                 } else
3834                         expand_label_str(label, len, name, tmp);
3835         }
3836 }
3837
3838 void
3839 expand_label_port(const char *name, char *label, size_t len,
3840     struct node_port *port)
3841 {
3842         char     a1[6], a2[6], op[13] = "";
3843
3844         if (strstr(label, name) != NULL) {
3845                 snprintf(a1, sizeof(a1), "%u", ntohs(port->port[0]));
3846                 snprintf(a2, sizeof(a2), "%u", ntohs(port->port[1]));
3847                 if (!port->op)
3848                         ;
3849                 else if (port->op == PF_OP_IRG)
3850                         snprintf(op, sizeof(op), "%s><%s", a1, a2);
3851                 else if (port->op == PF_OP_XRG)
3852                         snprintf(op, sizeof(op), "%s<>%s", a1, a2);
3853                 else if (port->op == PF_OP_EQ)
3854                         snprintf(op, sizeof(op), "%s", a1);
3855                 else if (port->op == PF_OP_NE)
3856                         snprintf(op, sizeof(op), "!=%s", a1);
3857                 else if (port->op == PF_OP_LT)
3858                         snprintf(op, sizeof(op), "<%s", a1);
3859                 else if (port->op == PF_OP_LE)
3860                         snprintf(op, sizeof(op), "<=%s", a1);
3861                 else if (port->op == PF_OP_GT)
3862                         snprintf(op, sizeof(op), ">%s", a1);
3863                 else if (port->op == PF_OP_GE)
3864                         snprintf(op, sizeof(op), ">=%s", a1);
3865                 expand_label_str(label, len, name, op);
3866         }
3867 }
3868
3869 void
3870 expand_label_proto(const char *name, char *label, size_t len, u_int8_t proto)
3871 {
3872         struct protoent *pe;
3873         char n[4];
3874
3875         if (strstr(label, name) != NULL) {
3876                 pe = getprotobynumber(proto);
3877                 if (pe != NULL)
3878                         expand_label_str(label, len, name, pe->p_name);
3879                 else {
3880                         snprintf(n, sizeof(n), "%u", proto);
3881                         expand_label_str(label, len, name, n);
3882                 }
3883         }
3884 }
3885
3886 void
3887 expand_label_nr(const char *name, char *label, size_t len)
3888 {
3889         char n[11];
3890
3891         if (strstr(label, name) != NULL) {
3892                 snprintf(n, sizeof(n), "%u", pf->rule_nr);
3893                 expand_label_str(label, len, name, n);
3894         }
3895 }
3896
3897 void
3898 expand_label(char *label, size_t len, const char *ifname, sa_family_t af,
3899     struct node_host *src_host, struct node_port *src_port,
3900     struct node_host *dst_host, struct node_port *dst_port,
3901     u_int8_t proto)
3902 {
3903         expand_label_if("$if", label, len, ifname);
3904         expand_label_addr("$srcaddr", label, len, af, src_host);
3905         expand_label_addr("$dstaddr", label, len, af, dst_host);
3906         expand_label_port("$srcport", label, len, src_port);
3907         expand_label_port("$dstport", label, len, dst_port);
3908         expand_label_proto("$proto", label, len, proto);
3909         expand_label_nr("$nr", label, len);
3910 }
3911
3912 int
3913 expand_altq(struct pf_altq *a, struct node_if *interfaces,
3914     struct node_queue *nqueues, struct node_queue_bw bwspec,
3915     struct node_queue_opt *opts)
3916 {
3917         struct pf_altq           pa, pb;
3918         char                     qname[PF_QNAME_SIZE];
3919         struct node_queue       *n;
3920         struct node_queue_bw     bw;
3921         int                      errs = 0;
3922
3923         if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
3924                 FREE_LIST(struct node_if, interfaces);
3925                 FREE_LIST(struct node_queue, nqueues);
3926                 return (0);
3927         }
3928
3929         LOOP_THROUGH(struct node_if, interface, interfaces,
3930                 memcpy(&pa, a, sizeof(struct pf_altq));
3931                 if (strlcpy(pa.ifname, interface->ifname,
3932                     sizeof(pa.ifname)) >= sizeof(pa.ifname))
3933                         errx(1, "expand_altq: strlcpy");
3934
3935                 if (interface->not) {
3936                         yyerror("altq on ! <interface> is not supported");
3937                         errs++;
3938                 } else {
3939                         if (eval_pfaltq(pf, &pa, &bwspec, opts))
3940                                 errs++;
3941                         else
3942                                 if (pfctl_add_altq(pf, &pa))
3943                                         errs++;
3944
3945                         if (pf->opts & PF_OPT_VERBOSE) {
3946                                 print_altq(&pf->paltq->altq, 0,
3947                                     &bwspec, opts);
3948                                 if (nqueues && nqueues->tail) {
3949                                         printf("queue { ");
3950                                         LOOP_THROUGH(struct node_queue, queue,
3951                                             nqueues,
3952                                                 printf("%s ",
3953                                                     queue->queue);
3954                                         );
3955                                         printf("}");
3956                                 }
3957                                 printf("\n");
3958                         }
3959
3960                         if (pa.scheduler == ALTQT_CBQ ||
3961                             pa.scheduler == ALTQT_HFSC) {
3962                                 /* now create a root queue */
3963                                 memset(&pb, 0, sizeof(struct pf_altq));
3964                                 if (strlcpy(qname, "root_", sizeof(qname)) >=
3965                                     sizeof(qname))
3966                                         errx(1, "expand_altq: strlcpy");
3967                                 if (strlcat(qname, interface->ifname,
3968                                     sizeof(qname)) >= sizeof(qname))
3969                                         errx(1, "expand_altq: strlcat");
3970                                 if (strlcpy(pb.qname, qname,
3971                                     sizeof(pb.qname)) >= sizeof(pb.qname))
3972                                         errx(1, "expand_altq: strlcpy");
3973                                 if (strlcpy(pb.ifname, interface->ifname,
3974                                     sizeof(pb.ifname)) >= sizeof(pb.ifname))
3975                                         errx(1, "expand_altq: strlcpy");
3976                                 pb.qlimit = pa.qlimit;
3977                                 pb.scheduler = pa.scheduler;
3978                                 bw.bw_absolute = pa.ifbandwidth;
3979                                 bw.bw_percent = 0;
3980                                 if (eval_pfqueue(pf, &pb, &bw, opts))
3981                                         errs++;
3982                                 else
3983                                         if (pfctl_add_altq(pf, &pb))
3984                                                 errs++;
3985                         }
3986
3987                         LOOP_THROUGH(struct node_queue, queue, nqueues,
3988                                 n = calloc(1, sizeof(struct node_queue));
3989                                 if (n == NULL)
3990                                         err(1, "expand_altq: calloc");
3991                                 if (pa.scheduler == ALTQT_CBQ ||
3992                                     pa.scheduler == ALTQT_HFSC /*||
3993                                     pa.scheduler == ALTQT_FAIRQ*/)
3994                                         if (strlcpy(n->parent, qname,
3995                                             sizeof(n->parent)) >=
3996                                             sizeof(n->parent))
3997                                                 errx(1, "expand_altq: strlcpy");
3998                                 if (strlcpy(n->queue, queue->queue,
3999                                     sizeof(n->queue)) >= sizeof(n->queue))
4000                                         errx(1, "expand_altq: strlcpy");
4001                                 if (strlcpy(n->ifname, interface->ifname,
4002                                     sizeof(n->ifname)) >= sizeof(n->ifname))
4003                                         errx(1, "expand_altq: strlcpy");
4004                                 n->scheduler = pa.scheduler;
4005                                 n->next = NULL;
4006                                 n->tail = n;
4007                                 if (queues == NULL)
4008                                         queues = n;
4009                                 else {
4010                                         queues->tail->next = n;
4011                                         queues->tail = n;
4012                                 }
4013                         );
4014                 }
4015         );
4016         FREE_LIST(struct node_if, interfaces);
4017         FREE_LIST(struct node_queue, nqueues);
4018
4019         return (errs);
4020 }
4021
4022 int
4023 expand_queue(struct pf_altq *a, struct node_if *interfaces,
4024     struct node_queue *nqueues, struct node_queue_bw bwspec,
4025     struct node_queue_opt *opts)
4026 {
4027         struct node_queue       *n, *nq;
4028         struct pf_altq           pa;
4029         u_int8_t                 found = 0;
4030         u_int8_t                 errs = 0;
4031
4032         if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
4033                 FREE_LIST(struct node_queue, nqueues);
4034                 return (0);
4035         }
4036
4037         if (queues == NULL) {
4038                 yyerror("queue %s has no parent", a->qname);
4039                 FREE_LIST(struct node_queue, nqueues);
4040                 return (1);
4041         }
4042
4043         LOOP_THROUGH(struct node_if, interface, interfaces,
4044                 LOOP_THROUGH(struct node_queue, tqueue, queues,
4045                         if (!strncmp(a->qname, tqueue->queue, PF_QNAME_SIZE) &&
4046                             (interface->ifname[0] == 0 ||
4047                             (!interface->not && !strncmp(interface->ifname,
4048                             tqueue->ifname, IFNAMSIZ)) ||
4049                             (interface->not && strncmp(interface->ifname,
4050                             tqueue->ifname, IFNAMSIZ)))) {
4051                                 /* found ourself in queues */
4052                                 found++;
4053
4054                                 memcpy(&pa, a, sizeof(struct pf_altq));
4055
4056                                 if (pa.scheduler != ALTQT_NONE &&
4057                                     pa.scheduler != tqueue->scheduler) {
4058                                         yyerror("exactly one scheduler type "
4059                                             "per interface allowed");
4060                                         return (1);
4061                                 }
4062                                 pa.scheduler = tqueue->scheduler;
4063
4064                                 /* scheduler dependent error checking */
4065                                 switch (pa.scheduler) {
4066                                 case ALTQT_PRIQ:
4067                                         if (nqueues != NULL) {
4068                                                 yyerror("priq queues cannot "
4069                                                     "have child queues");
4070                                                 return (1);
4071                                         }
4072                                         if (bwspec.bw_absolute > 0 ||
4073                                             bwspec.bw_percent < 100) {
4074                                                 yyerror("priq doesn't take "
4075                                                     "bandwidth");
4076                                                 return (1);
4077                                         }
4078                                         break;
4079                                 default:
4080                                         break;
4081                                 }
4082
4083                                 if (strlcpy(pa.ifname, tqueue->ifname,
4084                                     sizeof(pa.ifname)) >= sizeof(pa.ifname))
4085                                         errx(1, "expand_queue: strlcpy");
4086                                 if (strlcpy(pa.parent, tqueue->parent,
4087                                     sizeof(pa.parent)) >= sizeof(pa.parent))
4088                                         errx(1, "expand_queue: strlcpy");
4089
4090                                 if (eval_pfqueue(pf, &pa, &bwspec, opts))
4091                                         errs++;
4092                                 else
4093                                         if (pfctl_add_altq(pf, &pa))
4094                                                 errs++;
4095
4096                                 for (nq = nqueues; nq != NULL; nq = nq->next) {
4097                                         if (!strcmp(a->qname, nq->queue)) {
4098                                                 yyerror("queue cannot have "
4099                                                     "itself as child");
4100                                                 errs++;
4101                                                 continue;
4102                                         }
4103                                         n = calloc(1,
4104                                             sizeof(struct node_queue));
4105                                         if (n == NULL)
4106                                                 err(1, "expand_queue: calloc");
4107                                         if (strlcpy(n->parent, a->qname,
4108                                             sizeof(n->parent)) >=
4109                                             sizeof(n->parent))
4110                                                 errx(1, "expand_queue strlcpy");
4111                                         if (strlcpy(n->queue, nq->queue,
4112                                             sizeof(n->queue)) >=
4113                                             sizeof(n->queue))
4114                                                 errx(1, "expand_queue strlcpy");
4115                                         if (strlcpy(n->ifname, tqueue->ifname,
4116                                             sizeof(n->ifname)) >=
4117                                             sizeof(n->ifname))
4118                                                 errx(1, "expand_queue strlcpy");
4119                                         n->scheduler = tqueue->scheduler;
4120                                         n->next = NULL;
4121                                         n->tail = n;
4122                                         if (queues == NULL)
4123                                                 queues = n;
4124                                         else {
4125                                                 queues->tail->next = n;
4126                                                 queues->tail = n;
4127                                         }
4128                                 }
4129                                 if ((pf->opts & PF_OPT_VERBOSE) && (
4130                                     (found == 1 && interface->ifname[0] == 0) ||
4131                                     (found > 0 && interface->ifname[0] != 0))) {
4132                                         print_queue(&pf->paltq->altq, 0,
4133                                             &bwspec, interface->ifname[0] != 0,
4134                                             opts);
4135                                         if (nqueues && nqueues->tail) {
4136                                                 printf("{ ");
4137                                                 LOOP_THROUGH(struct node_queue,
4138                                                     queue, nqueues,
4139                                                         printf("%s ",
4140                                                             queue->queue);
4141                                                 );
4142                                                 printf("}");
4143                                         }
4144                                         printf("\n");
4145                                 }
4146                         }
4147                 );
4148         );
4149
4150         FREE_LIST(struct node_queue, nqueues);
4151         FREE_LIST(struct node_if, interfaces);
4152
4153         if (!found) {
4154                 yyerror("queue %s has no parent", a->qname);
4155                 errs++;
4156         }
4157
4158         if (errs)
4159                 return (1);
4160         else
4161                 return (0);
4162 }
4163
4164 void
4165 expand_rule(struct pf_rule *r,
4166     struct node_if *interfaces, struct node_host *rpool_hosts,
4167     struct node_proto *protos, struct node_os *src_oses,
4168     struct node_host *src_hosts, struct node_port *src_ports,
4169     struct node_host *dst_hosts, struct node_port *dst_ports,
4170     struct node_uid *uids, struct node_gid *gids, struct node_icmp *icmp_types)
4171 {
4172         sa_family_t              af = r->af;
4173         int                      added = 0, error = 0;
4174         char                     ifname[IF_NAMESIZE];
4175         char                     label[PF_RULE_LABEL_SIZE];
4176         char                     tagname[PF_TAG_NAME_SIZE];
4177         char                     match_tagname[PF_TAG_NAME_SIZE];
4178         struct pf_pooladdr      *pa;
4179         struct node_host        *h;
4180         u_int8_t                 flags, flagset, keep_state;
4181
4182         if (strlcpy(label, r->label, sizeof(label)) >= sizeof(label))
4183                 errx(1, "expand_rule: strlcpy");
4184         if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname))
4185                 errx(1, "expand_rule: strlcpy");
4186         if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >=
4187             sizeof(match_tagname))
4188                 errx(1, "expand_rule: strlcpy");
4189         flags = r->flags;
4190         flagset = r->flagset;
4191         keep_state = r->keep_state;
4192
4193         LOOP_THROUGH(struct node_if, interface, interfaces,
4194         LOOP_THROUGH(struct node_proto, proto, protos,
4195         LOOP_THROUGH(struct node_icmp, icmp_type, icmp_types,
4196         LOOP_THROUGH(struct node_host, src_host, src_hosts,
4197         LOOP_THROUGH(struct node_port, src_port, src_ports,
4198         LOOP_THROUGH(struct node_os, src_os, src_oses,
4199         LOOP_THROUGH(struct node_host, dst_host, dst_hosts,
4200         LOOP_THROUGH(struct node_port, dst_port, dst_ports,
4201         LOOP_THROUGH(struct node_uid, uid, uids,
4202         LOOP_THROUGH(struct node_gid, gid, gids,
4203
4204                 r->af = af;
4205                 /* for link-local IPv6 address, interface must match up */
4206                 if ((r->af && src_host->af && r->af != src_host->af) ||
4207                     (r->af && dst_host->af && r->af != dst_host->af) ||
4208                     (src_host->af && dst_host->af &&
4209                     src_host->af != dst_host->af) ||
4210                     (src_host->ifindex && dst_host->ifindex &&
4211                     src_host->ifindex != dst_host->ifindex) ||
4212                     (src_host->ifindex && *interface->ifname &&
4213                     src_host->ifindex != if_nametoindex(interface->ifname)) ||
4214                     (dst_host->ifindex && *interface->ifname &&
4215                     dst_host->ifindex != if_nametoindex(interface->ifname)))
4216                         continue;
4217                 if (!r->af && src_host->af)
4218                         r->af = src_host->af;
4219                 else if (!r->af && dst_host->af)
4220                         r->af = dst_host->af;
4221
4222                 if (*interface->ifname)
4223                         memcpy(r->ifname, interface->ifname, sizeof(r->ifname));
4224                 else if (if_indextoname(src_host->ifindex, ifname))
4225                         memcpy(r->ifname, ifname, sizeof(r->ifname));
4226                 else if (if_indextoname(dst_host->ifindex, ifname))
4227                         memcpy(r->ifname, ifname, sizeof(r->ifname));
4228                 else
4229                         memset(r->ifname, '\0', sizeof(r->ifname));
4230
4231                 if (strlcpy(r->label, label, sizeof(r->label)) >=
4232                     sizeof(r->label))
4233                         errx(1, "expand_rule: strlcpy");
4234                 if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >=
4235                     sizeof(r->tagname))
4236                         errx(1, "expand_rule: strlcpy");
4237                 if (strlcpy(r->match_tagname, match_tagname,
4238                     sizeof(r->match_tagname)) >= sizeof(r->match_tagname))
4239                         errx(1, "expand_rule: strlcpy");
4240                 expand_label(r->label, PF_RULE_LABEL_SIZE, r->ifname, r->af,
4241                     src_host, src_port, dst_host, dst_port, proto->proto);
4242                 expand_label(r->tagname, PF_TAG_NAME_SIZE, r->ifname, r->af,
4243                     src_host, src_port, dst_host, dst_port, proto->proto);
4244                 expand_label(r->match_tagname, PF_TAG_NAME_SIZE, r->ifname,
4245                     r->af, src_host, src_port, dst_host, dst_port,
4246                     proto->proto);
4247
4248                 error += check_netmask(src_host, r->af);
4249                 error += check_netmask(dst_host, r->af);
4250
4251                 r->ifnot = interface->not;
4252                 r->proto = proto->proto;
4253                 r->src.addr = src_host->addr;
4254                 r->src.not = src_host->not;
4255                 r->src.port[0] = src_port->port[0];
4256                 r->src.port[1] = src_port->port[1];
4257                 r->src.port_op = src_port->op;
4258                 r->dst.addr = dst_host->addr;
4259                 r->dst.not = dst_host->not;
4260                 r->dst.port[0] = dst_port->port[0];
4261                 r->dst.port[1] = dst_port->port[1];
4262                 r->dst.port_op = dst_port->op;
4263                 r->uid.op = uid->op;
4264                 r->uid.uid[0] = uid->uid[0];
4265                 r->uid.uid[1] = uid->uid[1];
4266                 r->gid.op = gid->op;
4267                 r->gid.gid[0] = gid->gid[0];
4268                 r->gid.gid[1] = gid->gid[1];
4269                 r->type = icmp_type->type;
4270                 r->code = icmp_type->code;
4271
4272                 if ((keep_state == PF_STATE_MODULATE ||
4273                     keep_state == PF_STATE_SYNPROXY) &&
4274                     r->proto && r->proto != IPPROTO_TCP)
4275                         r->keep_state = PF_STATE_NORMAL;
4276                 else
4277                         r->keep_state = keep_state;
4278
4279                 if (r->proto && r->proto != IPPROTO_TCP) {
4280                         r->flags = 0;
4281                         r->flagset = 0;
4282                 } else {
4283                         r->flags = flags;
4284                         r->flagset = flagset;
4285                 }
4286                 if (icmp_type->proto && r->proto != icmp_type->proto) {
4287                         yyerror("icmp-type mismatch");
4288                         error++;
4289                 }
4290
4291                 if (src_os && src_os->os) {
4292                         r->os_fingerprint = pfctl_get_fingerprint(src_os->os);
4293                         if ((pf->opts & PF_OPT_VERBOSE2) &&
4294                             r->os_fingerprint == PF_OSFP_NOMATCH)
4295                                 fprintf(stderr,
4296                                     "warning: unknown '%s' OS fingerprint\n",
4297                                     src_os->os);
4298                 } else {
4299                         r->os_fingerprint = PF_OSFP_ANY;
4300                 }
4301
4302                 TAILQ_INIT(&r->rpool.list);
4303                 for (h = rpool_hosts; h != NULL; h = h->next) {
4304                         pa = calloc(1, sizeof(struct pf_pooladdr));
4305                         if (pa == NULL)
4306                                 err(1, "expand_rule: calloc");
4307                         pa->addr = h->addr;
4308                         if (h->ifname != NULL) {
4309                                 if (strlcpy(pa->ifname, h->ifname,
4310                                     sizeof(pa->ifname)) >=
4311                                     sizeof(pa->ifname))
4312                                         errx(1, "expand_rule: strlcpy");
4313                         } else
4314                                 pa->ifname[0] = 0;
4315                         TAILQ_INSERT_TAIL(&r->rpool.list, pa, entries);
4316                 }
4317
4318                 if (rule_consistent(r) < 0 || error)
4319                         yyerror("skipping rule due to errors");
4320                 else {
4321                         r->nr = pf->rule_nr++;
4322                         pfctl_add_rule(pf, r);
4323                         added++;
4324                 }
4325
4326         ))))))))));
4327
4328         FREE_LIST(struct node_if, interfaces);
4329         FREE_LIST(struct node_proto, protos);
4330         FREE_LIST(struct node_host, src_hosts);
4331         FREE_LIST(struct node_port, src_ports);
4332         FREE_LIST(struct node_os, src_oses);
4333         FREE_LIST(struct node_host, dst_hosts);
4334         FREE_LIST(struct node_port, dst_ports);
4335         FREE_LIST(struct node_uid, uids);
4336         FREE_LIST(struct node_gid, gids);
4337         FREE_LIST(struct node_icmp, icmp_types);
4338         FREE_LIST(struct node_host, rpool_hosts);
4339
4340         if (!added)
4341                 yyerror("rule expands to no valid combination");
4342 }
4343
4344 #undef FREE_LIST
4345 #undef LOOP_THROUGH
4346
4347 int
4348 check_rulestate(int desired_state)
4349 {
4350         if (require_order && (rulestate > desired_state)) {
4351                 yyerror("Rules must be in order: options, normalization, "
4352                     "queueing, translation, filtering");
4353                 return (1);
4354         }
4355         rulestate = desired_state;
4356         return (0);
4357 }
4358
4359 int
4360 kw_cmp(const void *k, const void *e)
4361 {
4362         return (strcmp(k, ((const struct keywords *)e)->k_name));
4363 }
4364
4365 int
4366 lookup(char *s)
4367 {
4368         /* this has to be sorted always */
4369         static const struct keywords keywords[] = {
4370                 { "all",                ALL},
4371                 { "allow-opts",         ALLOWOPTS},
4372                 { "altq",               ALTQ},
4373                 { "anchor",             ANCHOR},
4374                 { "antispoof",          ANTISPOOF},
4375                 { "any",                ANY},
4376                 { "bandwidth",          BANDWIDTH},
4377                 { "binat",              BINAT},
4378                 { "binat-anchor",       BINATANCHOR},
4379                 { "bitmask",            BITMASK},
4380                 { "block",              BLOCK},
4381                 { "block-policy",       BLOCKPOLICY},
4382                 { "buckets",            BUCKETS},
4383                 { "cbq",                CBQ},
4384                 { "code",               CODE},
4385                 { "crop",               FRAGCROP},
4386                 { "debug",              DEBUG},
4387                 { "drop",               DROP},
4388                 { "drop-ovl",           FRAGDROP},
4389                 { "dup-to",             DUPTO},
4390                 { "fairq",              FAIRQ},
4391                 { "fastroute",          FASTROUTE},
4392                 { "file",               FILENAME},
4393                 { "fingerprints",       FINGERPRINTS},
4394                 { "flags",              FLAGS},
4395                 { "floating",           FLOATING},
4396                 { "for",                FOR},
4397                 { "fragment",           FRAGMENT},
4398                 { "from",               FROM},
4399                 { "global",             GLOBAL},
4400                 { "group",              GROUP},
4401                 { "group-bound",        GRBOUND},
4402                 { "hfsc",               HFSC},
4403                 { "hogs",               HOGS},
4404                 { "hostid",             HOSTID},
4405                 { "icmp-type",          ICMPTYPE},
4406                 { "icmp6-type",         ICMP6TYPE},
4407                 { "if-bound",           IFBOUND},
4408                 { "in",                 IN},
4409                 { "inet",               INET},
4410                 { "inet6",              INET6},
4411                 { "keep",               KEEP},
4412                 { "label",              LABEL},
4413                 { "limit",              LIMIT},
4414                 { "linkshare",          LINKSHARE},
4415                 { "load",               LOAD},
4416                 { "log",                LOG},
4417                 { "log-all",            LOGALL},
4418                 { "loginterface",       LOGINTERFACE},
4419                 { "max",                MAXIMUM},
4420                 { "max-mss",            MAXMSS},
4421                 { "max-src-nodes",      MAXSRCNODES},
4422                 { "max-src-states",     MAXSRCSTATES},
4423                 { "min-ttl",            MINTTL},
4424                 { "modulate",           MODULATE},
4425                 { "nat",                NAT},
4426                 { "nat-anchor",         NATANCHOR},
4427                 { "no",                 NO},
4428                 { "no-df",              NODF},
4429                 { "no-route",           NOROUTE},
4430                 { "no-sync",            NOSYNC},
4431                 { "on",                 ON},
4432                 { "optimization",       OPTIMIZATION},
4433                 { "os",                 OS},
4434                 { "out",                OUT},
4435                 { "pass",               PASS},
4436                 { "port",               PORT},
4437                 { "priority",           PRIORITY},
4438                 { "priq",               PRIQ},
4439                 { "proto",              PROTO},
4440                 { "qlimit",             QLIMIT},
4441                 { "queue",              QUEUE},
4442                 { "quick",              QUICK},
4443                 { "random",             RANDOM},
4444                 { "random-id",          RANDOMID},
4445                 { "rdr",                RDR},
4446                 { "rdr-anchor",         RDRANCHOR},
4447                 { "realtime",           REALTIME},
4448                 { "reassemble",         REASSEMBLE},
4449                 { "reply-to",           REPLYTO},
4450                 { "require-order",      REQUIREORDER},
4451                 { "return",             RETURN},
4452                 { "return-icmp",        RETURNICMP},
4453                 { "return-icmp6",       RETURNICMP6},
4454                 { "return-rst",         RETURNRST},
4455                 { "round-robin",        ROUNDROBIN},
4456                 { "route-to",           ROUTETO},
4457                 { "rule",               RULE},
4458                 { "scrub",              SCRUB},
4459                 { "set",                SET},
4460                 { "source-hash",        SOURCEHASH},
4461                 { "source-track",       SOURCETRACK},
4462                 { "state",              STATE},
4463                 { "state-policy",       STATEPOLICY},
4464                 { "static-port",        STATICPORT},
4465                 { "sticky-address",     STICKYADDRESS},
4466                 { "synproxy",           SYNPROXY},
4467                 { "table",              TABLE},
4468                 { "tag",                TAG},
4469                 { "tagged",             TAGGED},
4470                 { "tbrsize",            TBRSIZE},
4471                 { "timeout",            TIMEOUT},
4472                 { "to",                 TO},
4473                 { "tos",                TOS},
4474                 { "ttl",                TTL},
4475                 { "upperlimit",         UPPERLIMIT},
4476                 { "user",               USER},
4477         };
4478         const struct keywords   *p;
4479
4480         p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
4481             sizeof(keywords[0]), kw_cmp);
4482
4483         if (p) {
4484                 if (debug > 1)
4485                         fprintf(stderr, "%s: %d\n", s, p->k_val);
4486                 return (p->k_val);
4487         } else {
4488                 if (debug > 1)
4489                         fprintf(stderr, "string: %s\n", s);
4490                 return (STRING);
4491         }
4492 }
4493
4494 #define MAXPUSHBACK     128
4495
4496 char    *parsebuf;
4497 int      parseindex;
4498 char     pushback_buffer[MAXPUSHBACK];
4499 int      pushback_index = 0;
4500
4501 int
4502 lgetc(FILE *f)
4503 {
4504         int     c, next;
4505
4506         if (parsebuf) {
4507                 /* Read character from the parsebuffer instead of input. */
4508                 if (parseindex >= 0) {
4509                         c = parsebuf[parseindex++];
4510                         if (c != '\0')
4511                                 return (c);
4512                         parsebuf = NULL;
4513                 } else
4514                         parseindex++;
4515         }
4516
4517         if (pushback_index)
4518                 return (pushback_buffer[--pushback_index]);
4519
4520         while ((c = getc(f)) == '\\') {
4521                 next = getc(f);
4522                 if (next != '\n') {
4523                         if (isspace(next))
4524                                 yyerror("whitespace after \\");
4525                         ungetc(next, f);
4526                         break;
4527                 }
4528                 yylval.lineno = lineno;
4529                 lineno++;
4530         }
4531         if (c == '\t' || c == ' ') {
4532                 /* Compress blanks to a single space. */
4533                 do {
4534                         c = getc(f);
4535                 } while (c == '\t' || c == ' ');
4536                 ungetc(c, f);
4537                 c = ' ';
4538         }
4539
4540         return (c);
4541 }
4542
4543 int
4544 lungetc(int c)
4545 {
4546         if (c == EOF)
4547                 return (EOF);
4548         if (parsebuf) {
4549                 parseindex--;
4550                 if (parseindex >= 0)
4551                         return (c);
4552         }
4553         if (pushback_index < MAXPUSHBACK-1)
4554                 return (pushback_buffer[pushback_index++] = c);
4555         else
4556                 return (EOF);
4557 }
4558
4559 int
4560 findeol(void)
4561 {
4562         int     c;
4563
4564         parsebuf = NULL;
4565         pushback_index = 0;
4566
4567         /* skip to either EOF or the first real EOL */
4568         while (1) {
4569                 c = lgetc(fin);
4570                 if (c == '\n') {
4571                         lineno++;
4572                         break;
4573                 }
4574                 if (c == EOF)
4575                         break;
4576         }
4577         return (ERROR);
4578 }
4579
4580 int
4581 yylex(void)
4582 {
4583         char     buf[8096];
4584         char    *p, *val;
4585         int      endc, c, next;
4586         int      token;
4587
4588 top:
4589         p = buf;
4590         while ((c = lgetc(fin)) == ' ')
4591                 ; /* nothing */
4592
4593         yylval.lineno = lineno;
4594         if (c == '#')
4595                 while ((c = lgetc(fin)) != '\n' && c != EOF)
4596                         ; /* nothing */
4597         if (c == '$' && parsebuf == NULL) {
4598                 while (1) {
4599                         if ((c = lgetc(fin)) == EOF)
4600                                 return (0);
4601
4602                         if (p + 1 >= buf + sizeof(buf) - 1) {
4603                                 yyerror("string too long");
4604                                 return (findeol());
4605                         }
4606                         if (isalnum(c) || c == '_') {
4607                                 *p++ = (char)c;
4608                                 continue;
4609                         }
4610                         *p = '\0';
4611                         lungetc(c);
4612                         break;
4613                 }
4614                 val = symget(buf);
4615                 if (val == NULL) {
4616                         yyerror("macro '%s' not defined", buf);
4617                         return (findeol());
4618                 }
4619                 parsebuf = val;
4620                 parseindex = 0;
4621                 goto top;
4622         }
4623
4624         switch (c) {
4625         case '\'':
4626         case '"':
4627                 endc = c;
4628                 while (1) {
4629                         if ((c = lgetc(fin)) == EOF)
4630                                 return (0);
4631                         if (c == endc) {
4632                                 *p = '\0';
4633                                 break;
4634                         }
4635                         if (c == '\n') {
4636                                 lineno++;
4637                                 continue;
4638                         }
4639                         if (p + 1 >= buf + sizeof(buf) - 1) {
4640                                 yyerror("string too long");
4641                                 return (findeol());
4642                         }
4643                         *p++ = (char)c;
4644                 }
4645                 yylval.v.string = strdup(buf);
4646                 if (yylval.v.string == NULL)
4647                         err(1, "yylex: strdup");
4648                 return (STRING);
4649         case '<':
4650                 next = lgetc(fin);
4651                 if (next == '>') {
4652                         yylval.v.i = PF_OP_XRG;
4653                         return (PORTBINARY);
4654                 }
4655                 lungetc(next);
4656                 break;
4657         case '>':
4658                 next = lgetc(fin);
4659                 if (next == '<') {
4660                         yylval.v.i = PF_OP_IRG;
4661                         return (PORTBINARY);
4662                 }
4663                 lungetc(next);
4664                 break;
4665         case '-':
4666                 next = lgetc(fin);
4667                 if (next == '>')
4668                         return (ARROW);
4669                 lungetc(next);
4670                 break;
4671         }
4672
4673 #define allowed_in_string(x) \
4674         (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
4675         x != '{' && x != '}' && x != '<' && x != '>' && \
4676         x != '!' && x != '=' && x != '/' && x != '#' && \
4677         x != ','))
4678
4679         if (isalnum(c) || c == ':' || c == '_') {
4680                 do {
4681                         *p++ = c;
4682                         if ((unsigned)(p-buf) >= sizeof(buf)) {
4683                                 yyerror("string too long");
4684                                 return (findeol());
4685                         }
4686                 } while ((c = lgetc(fin)) != EOF && (allowed_in_string(c)));
4687                 lungetc(c);
4688                 *p = '\0';
4689                 if ((token = lookup(buf)) == STRING)
4690                         if ((yylval.v.string = strdup(buf)) == NULL)
4691                                 err(1, "yylex: strdup");
4692                 return (token);
4693         }
4694         if (c == '\n') {
4695                 yylval.lineno = lineno;
4696                 lineno++;
4697         }
4698         if (c == EOF)
4699                 return (0);
4700         return (c);
4701 }
4702
4703 int
4704 parse_rules(FILE *input, struct pfctl *xpf)
4705 {
4706         struct sym      *sym, *next;
4707
4708         fin = input;
4709         pf = xpf;
4710         lineno = 1;
4711         errors = 0;
4712         rulestate = PFCTL_STATE_NONE;
4713         returnicmpdefault = (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
4714         returnicmp6default =
4715             (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
4716         blockpolicy = PFRULE_DROP;
4717         require_order = 1;
4718
4719         yyparse();
4720
4721         /* Free macros and check which have not been used. */
4722         for (sym = TAILQ_FIRST(&symhead); sym != NULL; sym = next) {
4723                 next = TAILQ_NEXT(sym, entries);
4724                 if ((pf->opts & PF_OPT_VERBOSE2) && !sym->used)
4725                         fprintf(stderr, "warning: macro '%s' not "
4726                             "used\n", sym->nam);
4727                 free(sym->nam);
4728                 free(sym->val);
4729                 TAILQ_REMOVE(&symhead, sym, entries);
4730                 free(sym);
4731         }
4732
4733         return (errors ? -1 : 0);
4734 }
4735
4736 /*
4737  * Over-designed efficiency is a French and German concept, so how about
4738  * we wait until they discover this ugliness and make it all fancy.
4739  */
4740 int
4741 symset(const char *nam, const char *val, int persist)
4742 {
4743         struct sym      *sym;
4744
4745         for (sym = TAILQ_FIRST(&symhead); sym && strcmp(nam, sym->nam);
4746             sym = TAILQ_NEXT(sym, entries))
4747                 ;       /* nothing */
4748
4749         if (sym != NULL) {
4750                 if (sym->persist == 1)
4751                         return (0);
4752                 else {
4753                         free(sym->nam);
4754                         free(sym->val);
4755                         TAILQ_REMOVE(&symhead, sym, entries);
4756                         free(sym);
4757                 }
4758         }
4759         if ((sym = calloc(1, sizeof(*sym))) == NULL)
4760                 return (-1);
4761
4762         sym->nam = strdup(nam);
4763         if (sym->nam == NULL) {
4764                 free(sym);
4765                 return (-1);
4766         }
4767         sym->val = strdup(val);
4768         if (sym->val == NULL) {
4769                 free(sym->nam);
4770                 free(sym);
4771                 return (-1);
4772         }
4773         sym->used = 0;
4774         sym->persist = persist;
4775         TAILQ_INSERT_TAIL(&symhead, sym, entries);
4776         return (0);
4777 }
4778
4779 int
4780 pfctl_cmdline_symset(char *s)
4781 {
4782         char    *sym, *val;
4783         int      ret;
4784
4785         if ((val = strrchr(s, '=')) == NULL)
4786                 return (-1);
4787
4788         if ((sym = malloc(strlen(s) - strlen(val) + 1)) == NULL)
4789                 err(1, "pfctl_cmdline_symset: malloc");
4790
4791         strlcpy(sym, s, strlen(s) - strlen(val) + 1);
4792
4793         ret = symset(sym, val + 1, 1);
4794         free(sym);
4795
4796         return (ret);
4797 }
4798
4799 char *
4800 symget(const char *nam)
4801 {
4802         struct sym      *sym;
4803
4804         TAILQ_FOREACH(sym, &symhead, entries)
4805                 if (strcmp(nam, sym->nam) == 0) {
4806                         sym->used = 1;
4807                         return (sym->val);
4808                 }
4809         return (NULL);
4810 }
4811
4812 void
4813 decide_address_family(struct node_host *n, sa_family_t *af)
4814 {
4815         sa_family_t     target_af = 0;
4816
4817         while (!*af && n != NULL) {
4818                 if (n->af) {
4819                         if (target_af == 0)
4820                                 target_af = n->af;
4821                         if (target_af != n->af)
4822                                 return;
4823                 }
4824                 n = n->next;
4825         }
4826         if (!*af && target_af)
4827                 *af = target_af;
4828 }
4829
4830 void
4831 remove_invalid_hosts(struct node_host **nh, sa_family_t *af)
4832 {
4833         struct node_host        *n = *nh, *prev = NULL;
4834
4835         while (n != NULL) {
4836                 if (*af && n->af && n->af != *af) {
4837                         /* unlink and free n */
4838                         struct node_host *next = n->next;
4839
4840                         /* adjust tail pointer */
4841                         if (n == (*nh)->tail)
4842                                 (*nh)->tail = prev;
4843                         /* adjust previous node's next pointer */
4844                         if (prev == NULL)
4845                                 *nh = next;
4846                         else
4847                                 prev->next = next;
4848                         /* free node */
4849                         if (n->ifname != NULL)
4850                                 free(n->ifname);
4851                         free(n);
4852                         n = next;
4853                 } else {
4854                         if (n->af && !*af)
4855                                 *af = n->af;
4856                         prev = n;
4857                         n = n->next;
4858                 }
4859         }
4860 }
4861
4862 int
4863 invalid_redirect(struct node_host *nh, sa_family_t af)
4864 {
4865         if (!af) {
4866                 struct node_host *n;
4867
4868                 /* tables and dyniftl are ok without an address family */
4869                 for (n = nh; n != NULL; n = n->next) {
4870                         if (n->addr.type != PF_ADDR_TABLE &&
4871                             n->addr.type != PF_ADDR_DYNIFTL) {
4872                                 yyerror("address family not given and "
4873                                     "translation address expands to multiple "
4874                                     "address families");
4875                                 return (1);
4876                         }
4877                 }
4878         }
4879         if (nh == NULL) {
4880                 yyerror("no translation address with matching address family "
4881                     "found.");
4882                 return (1);
4883         }
4884         return (0);
4885 }
4886
4887 int
4888 atoul(char *s, u_long *ulvalp)
4889 {
4890         u_long   ulval;
4891         char    *ep;
4892
4893         errno = 0;
4894         ulval = strtoul(s, &ep, 0);
4895         if (s[0] == '\0' || *ep != '\0')
4896                 return (-1);
4897         if (errno == ERANGE && ulval == ULONG_MAX)
4898                 return (-1);
4899         *ulvalp = ulval;
4900         return (0);
4901 }
4902
4903 int
4904 getservice(char *n)
4905 {
4906         struct servent  *s;
4907         u_long           ulval;
4908
4909         if (atoul(n, &ulval) == 0) {
4910                 if (ulval > 65535) {
4911                         yyerror("illegal port value %d", ulval);
4912                         return (-1);
4913                 }
4914                 return (htons(ulval));
4915         } else {
4916                 s = getservbyname(n, "tcp");
4917                 if (s == NULL)
4918                         s = getservbyname(n, "udp");
4919                 if (s == NULL) {
4920                         yyerror("unknown port %s", n);
4921                         return (-1);
4922                 }
4923                 return (s->s_port);
4924         }
4925 }
4926
4927 int
4928 rule_label(struct pf_rule *r, char *s)
4929 {
4930         if (s) {
4931                 if (strlcpy(r->label, s, sizeof(r->label)) >=
4932                     sizeof(r->label)) {
4933                         yyerror("rule label too long (max %d chars)",
4934                             sizeof(r->label)-1);
4935                         return (-1);
4936                 }
4937         }
4938         return (0);
4939 }
4940
4941 u_int16_t
4942 parseicmpspec(char *w, sa_family_t af)
4943 {
4944         const struct icmpcodeent        *p;
4945         u_long                           ulval;
4946         u_int8_t                         icmptype;
4947
4948         if (af == AF_INET)
4949                 icmptype = returnicmpdefault >> 8;
4950         else
4951                 icmptype = returnicmp6default >> 8;
4952
4953         if (atoul(w, &ulval) == -1) {
4954                 if ((p = geticmpcodebyname(icmptype, w, af)) == NULL) {
4955                         yyerror("unknown icmp code %s", w);
4956                         return (0);
4957                 }
4958                 ulval = p->code;
4959         }
4960         if (ulval > 255) {
4961                 yyerror("invalid icmp code %ld", ulval);
4962                 return (0);
4963         }
4964         return (icmptype << 8 | ulval);
4965 }
4966
4967 int
4968 pfctl_load_anchors(int dev, int opts, struct pfr_buffer *trans)
4969 {
4970         struct loadanchors      *la;
4971
4972         TAILQ_FOREACH(la, &loadanchorshead, entries) {
4973                 if (opts & PF_OPT_VERBOSE)
4974                         fprintf(stderr, "\nLoading anchor %s:%s from %s\n",
4975                             la->anchorname, la->rulesetname, la->filename);
4976                 if (pfctl_rules(dev, la->filename, opts, la->anchorname,
4977                     la->rulesetname, trans) == -1)
4978                         return (-1);
4979         }
4980
4981         return (0);
4982 }
4983