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