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