1 /* $OpenBSD: parse.y,v 1.554 2008/10/17 12:59:53 henning Exp $ */
2
3 /*-
4 * SPDX-License-Identifier: BSD-2-Clause
5 *
6 * Copyright (c) 2001 Markus Friedl. All rights reserved.
7 * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
8 * Copyright (c) 2001 Theo de Raadt. All rights reserved.
9 * Copyright (c) 2002,2003 Henning Brauer. All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 %{
32 #include <sys/cdefs.h>
33 #define PFIOC_USE_LATEST
34
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <sys/stat.h>
38 #ifdef __FreeBSD__
39 #include <sys/sysctl.h>
40 #endif
41 #include <net/if.h>
42 #include <netinet/in.h>
43 #include <netinet/in_systm.h>
44 #include <netinet/ip.h>
45 #include <netinet/ip_icmp.h>
46 #include <netinet/icmp6.h>
47 #include <net/pfvar.h>
48 #include <arpa/inet.h>
49 #include <net/altq/altq.h>
50 #include <net/altq/altq_cbq.h>
51 #include <net/altq/altq_codel.h>
52 #include <net/altq/altq_priq.h>
53 #include <net/altq/altq_hfsc.h>
54 #include <net/altq/altq_fairq.h>
55
56 #include <assert.h>
57 #include <stdio.h>
58 #include <unistd.h>
59 #include <stdlib.h>
60 #include <netdb.h>
61 #include <stdarg.h>
62 #include <errno.h>
63 #include <string.h>
64 #include <ctype.h>
65 #include <math.h>
66 #include <err.h>
67 #include <limits.h>
68 #include <pwd.h>
69 #include <grp.h>
70 #include <md5.h>
71
72 #include "pfctl_parser.h"
73 #include "pfctl.h"
74
75 static struct pfctl *pf = NULL;
76 static int debug = 0;
77 static int rulestate = 0;
78 static u_int16_t returnicmpdefault =
79 (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
80 static u_int16_t returnicmp6default =
81 (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
82 static int blockpolicy = PFRULE_DROP;
83 static int failpolicy = PFRULE_DROP;
84 static int require_order = 1;
85 static int default_statelock;
86
87 static TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
88 static struct file {
89 TAILQ_ENTRY(file) entry;
90 FILE *stream;
91 char *name;
92 int lineno;
93 int errors;
94 } *file;
95 struct file *pushfile(const char *, int);
96 int popfile(void);
97 int check_file_secrecy(int, const char *);
98 int yyparse(void);
99 int yylex(void);
100 int yyerror(const char *, ...);
101 int kw_cmp(const void *, const void *);
102 int lookup(char *);
103 int lgetc(int);
104 int lungetc(int);
105 int findeol(void);
106
107 static TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
108 struct sym {
109 TAILQ_ENTRY(sym) entry;
110 int used;
111 int persist;
112 char *nam;
113 char *val;
114 };
115 int symset(const char *, const char *, int);
116 char *symget(const char *);
117
118 int atoul(char *, u_long *);
119
120 enum {
121 PFCTL_STATE_NONE,
122 PFCTL_STATE_OPTION,
123 PFCTL_STATE_ETHER,
124 PFCTL_STATE_SCRUB,
125 PFCTL_STATE_QUEUE,
126 PFCTL_STATE_NAT,
127 PFCTL_STATE_FILTER
128 };
129
130 struct node_etherproto {
131 u_int16_t proto;
132 struct node_etherproto *next;
133 struct node_etherproto *tail;
134 };
135
136 struct node_proto {
137 u_int8_t proto;
138 struct node_proto *next;
139 struct node_proto *tail;
140 };
141
142 struct node_port {
143 u_int16_t port[2];
144 u_int8_t op;
145 struct node_port *next;
146 struct node_port *tail;
147 };
148
149 struct node_uid {
150 uid_t uid[2];
151 u_int8_t op;
152 struct node_uid *next;
153 struct node_uid *tail;
154 };
155
156 struct node_gid {
157 gid_t gid[2];
158 u_int8_t op;
159 struct node_gid *next;
160 struct node_gid *tail;
161 };
162
163 struct node_icmp {
164 u_int8_t code;
165 u_int8_t type;
166 u_int8_t proto;
167 struct node_icmp *next;
168 struct node_icmp *tail;
169 };
170
171 enum { PF_STATE_OPT_MAX, PF_STATE_OPT_NOSYNC, PF_STATE_OPT_SRCTRACK,
172 PF_STATE_OPT_MAX_SRC_STATES, PF_STATE_OPT_MAX_SRC_CONN,
173 PF_STATE_OPT_MAX_SRC_CONN_RATE, PF_STATE_OPT_MAX_SRC_NODES,
174 PF_STATE_OPT_OVERLOAD, PF_STATE_OPT_STATELOCK,
175 PF_STATE_OPT_TIMEOUT, PF_STATE_OPT_SLOPPY,
176 PF_STATE_OPT_PFLOW, PF_STATE_OPT_ALLOW_RELATED };
177
178 enum { PF_SRCTRACK_NONE, PF_SRCTRACK, PF_SRCTRACK_GLOBAL, PF_SRCTRACK_RULE };
179
180 struct node_state_opt {
181 int type;
182 union {
183 u_int32_t max_states;
184 u_int32_t max_src_states;
185 u_int32_t max_src_conn;
186 struct {
187 u_int32_t limit;
188 u_int32_t seconds;
189 } max_src_conn_rate;
190 struct {
191 u_int8_t flush;
192 char tblname[PF_TABLE_NAME_SIZE];
193 } overload;
194 u_int32_t max_src_nodes;
195 u_int8_t src_track;
196 u_int32_t statelock;
197 struct {
198 int number;
199 u_int32_t seconds;
200 } timeout;
201 } data;
202 struct node_state_opt *next;
203 struct node_state_opt *tail;
204 };
205
206 struct peer {
207 struct node_host *host;
208 struct node_port *port;
209 };
210
211 static struct node_queue {
212 char queue[PF_QNAME_SIZE];
213 char parent[PF_QNAME_SIZE];
214 char ifname[IFNAMSIZ];
215 int scheduler;
216 struct node_queue *next;
217 struct node_queue *tail;
218 } *queues = NULL;
219
220 struct node_qassign {
221 char *qname;
222 char *pqname;
223 };
224
225 struct range {
226 int a;
227 int b;
228 int t;
229 };
230 struct redirection {
231 struct node_host *host;
232 struct range rport;
233 };
234
235 static struct pool_opts {
236 int marker;
237 #define POM_TYPE 0x01
238 #define POM_STICKYADDRESS 0x02
239 #define POM_ENDPI 0x04
240 u_int8_t opts;
241 int type;
242 int staticport;
243 struct pf_poolhashkey *key;
244 struct pf_mape_portset mape;
245 } pool_opts;
246
247 struct redirspec {
248 struct redirection *rdr;
249 struct pool_opts pool_opts;
250 int af;
251 };
252
253 static struct filter_opts {
254 int marker;
255 #define FOM_FLAGS 0x0001
256 #define FOM_ICMP 0x0002
257 #define FOM_TOS 0x0004
258 #define FOM_KEEP 0x0008
259 #define FOM_SRCTRACK 0x0010
260 #define FOM_MINTTL 0x0020
261 #define FOM_MAXMSS 0x0040
262 #define FOM_AFTO 0x0080
263 #define FOM_SETTOS 0x0100
264 #define FOM_SCRUB_TCP 0x0200
265 #define FOM_SETPRIO 0x0400
266 #define FOM_ONCE 0x1000 /* not yet implemmented */
267 #define FOM_PRIO 0x2000
268 #define FOM_SETDELAY 0x4000
269 #define FOM_FRAGCACHE 0x8000 /* does not exist in OpenBSD */
270 struct node_uid *uid;
271 struct node_gid *gid;
272 struct node_if *rcv;
273 struct {
274 u_int8_t b1;
275 u_int8_t b2;
276 u_int16_t w;
277 u_int16_t w2;
278 } flags;
279 struct node_icmp *icmpspec;
280 u_int32_t tos;
281 u_int32_t prob;
282 u_int32_t ridentifier;
283 struct {
284 int action;
285 struct node_state_opt *options;
286 } keep;
287 int fragment;
288 int allowopts;
289 char *label[PF_RULE_MAX_LABEL_COUNT];
290 int labelcount;
291 struct node_qassign queues;
292 char *tag;
293 char *match_tag;
294 u_int8_t match_tag_not;
295 u_int16_t dnpipe;
296 u_int16_t dnrpipe;
297 u_int32_t free_flags;
298 u_int rtableid;
299 u_int8_t prio;
300 u_int8_t set_prio[2];
301 struct {
302 struct node_host *addr;
303 u_int16_t port;
304 } divert;
305 struct redirspec nat;
306 struct redirspec rdr;
307 struct redirspec rroute;
308 /* new-style scrub opts */
309 int nodf;
310 int minttl;
311 int settos;
312 int randomid;
313 int max_mss;
314 } filter_opts;
315
316 static struct antispoof_opts {
317 char *label[PF_RULE_MAX_LABEL_COUNT];
318 int labelcount;
319 u_int32_t ridentifier;
320 u_int rtableid;
321 } antispoof_opts;
322
323 static struct scrub_opts {
324 int marker;
325 int nodf;
326 int minttl;
327 int maxmss;
328 int settos;
329 int fragcache;
330 int randomid;
331 int reassemble_tcp;
332 char *match_tag;
333 u_int8_t match_tag_not;
334 u_int rtableid;
335 } scrub_opts;
336
337 static struct queue_opts {
338 int marker;
339 #define QOM_BWSPEC 0x01
340 #define QOM_SCHEDULER 0x02
341 #define QOM_PRIORITY 0x04
342 #define QOM_TBRSIZE 0x08
343 #define QOM_QLIMIT 0x10
344 struct node_queue_bw queue_bwspec;
345 struct node_queue_opt scheduler;
346 int priority;
347 unsigned int tbrsize;
348 int qlimit;
349 } queue_opts;
350
351 static struct table_opts {
352 int flags;
353 int init_addr;
354 struct node_tinithead init_nodes;
355 } table_opts;
356
357 static struct codel_opts codel_opts;
358 static struct node_hfsc_opts hfsc_opts;
359 static struct node_fairq_opts fairq_opts;
360 static struct node_state_opt *keep_state_defaults = NULL;
361 static struct pfctl_watermarks syncookie_opts;
362
363 int disallow_table(struct node_host *, const char *);
364 int disallow_urpf_failed(struct node_host *, const char *);
365 int disallow_alias(struct node_host *, const char *);
366 int rule_consistent(struct pfctl_rule *, int);
367 int filter_consistent(struct pfctl_rule *, int);
368 int nat_consistent(struct pfctl_rule *);
369 int rdr_consistent(struct pfctl_rule *);
370 int process_tabledef(char *, struct table_opts *);
371 void expand_label_str(char *, size_t, const char *, const char *);
372 void expand_label_if(const char *, char *, size_t, const char *);
373 void expand_label_addr(const char *, char *, size_t, sa_family_t,
374 struct pf_rule_addr *);
375 void expand_label_port(const char *, char *, size_t,
376 struct pf_rule_addr *);
377 void expand_label_proto(const char *, char *, size_t, u_int8_t);
378 void expand_label_nr(const char *, char *, size_t,
379 struct pfctl_rule *);
380 void expand_eth_rule(struct pfctl_eth_rule *,
381 struct node_if *, struct node_etherproto *,
382 struct node_mac *, struct node_mac *,
383 struct node_host *, struct node_host *, const char *,
384 const char *);
385 void expand_rule(struct pfctl_rule *, struct node_if *,
386 struct redirspec *, struct redirspec *, struct redirspec *,
387 struct node_host *, struct node_host *, struct node_host *,
388 struct node_proto *, struct node_os *, struct node_host *,
389 struct node_port *, struct node_host *, struct node_port *,
390 struct node_uid *, struct node_gid *, struct node_if *,
391 struct node_icmp *, const char *);
392 int expand_altq(struct pf_altq *, struct node_if *,
393 struct node_queue *, struct node_queue_bw bwspec,
394 struct node_queue_opt *);
395 int expand_queue(struct pf_altq *, struct node_if *,
396 struct node_queue *, struct node_queue_bw,
397 struct node_queue_opt *);
398 int expand_skip_interface(struct node_if *);
399
400 int check_rulestate(int);
401 int getservice(char *);
402 int rule_label(struct pfctl_rule *, char *s[PF_RULE_MAX_LABEL_COUNT]);
403 int eth_rule_label(struct pfctl_eth_rule *, char *s[PF_RULE_MAX_LABEL_COUNT]);
404 int rt_tableid_max(void);
405
406 void mv_rules(struct pfctl_ruleset *, struct pfctl_ruleset *);
407 void mv_eth_rules(struct pfctl_eth_ruleset *, struct pfctl_eth_ruleset *);
408 void decide_address_family(struct node_host *, sa_family_t *);
409 void remove_invalid_hosts(struct node_host **, sa_family_t *);
410 int invalid_redirect(struct node_host *, sa_family_t);
411 u_int16_t parseicmpspec(char *, sa_family_t);
412 int kw_casecmp(const void *, const void *);
413 int map_tos(char *string, int *);
414 struct node_mac* node_mac_from_string(const char *);
415 struct node_mac* node_mac_from_string_masklen(const char *, int);
416 struct node_mac* node_mac_from_string_mask(const char *, const char *);
417
418 static TAILQ_HEAD(loadanchorshead, loadanchors)
419 loadanchorshead = TAILQ_HEAD_INITIALIZER(loadanchorshead);
420
421 struct loadanchors {
422 TAILQ_ENTRY(loadanchors) entries;
423 char *anchorname;
424 char *filename;
425 };
426
427 typedef struct {
428 union {
429 int64_t number;
430 double probability;
431 int i;
432 char *string;
433 u_int rtableid;
434 struct {
435 u_int8_t b1;
436 u_int8_t b2;
437 u_int16_t w;
438 u_int16_t w2;
439 } b;
440 struct range range;
441 struct node_if *interface;
442 struct node_proto *proto;
443 struct node_etherproto *etherproto;
444 struct node_icmp *icmp;
445 struct node_host *host;
446 struct node_os *os;
447 struct node_port *port;
448 struct node_uid *uid;
449 struct node_gid *gid;
450 struct node_state_opt *state_opt;
451 struct peer peer;
452 struct {
453 struct peer src, dst;
454 struct node_os *src_os;
455 } fromto;
456 struct {
457 struct node_mac *src;
458 struct node_mac *dst;
459 } etherfromto;
460 struct node_mac *mac;
461 struct {
462 struct node_mac *mac;
463 } etheraddr;
464 char *bridge_to;
465 struct {
466 struct node_host *host;
467 u_int8_t rt;
468 u_int8_t pool_opts;
469 sa_family_t af;
470 struct pf_poolhashkey *key;
471 } route;
472 struct redirection *redirection;
473 struct {
474 int action;
475 struct node_state_opt *options;
476 } keep_state;
477 struct {
478 u_int8_t log;
479 u_int8_t logif;
480 u_int8_t quick;
481 } logquick;
482 struct {
483 int neg;
484 char *name;
485 } tagged;
486 struct pf_poolhashkey *hashkey;
487 struct node_queue *queue;
488 struct node_queue_opt queue_options;
489 struct node_queue_bw queue_bwspec;
490 struct node_qassign qassign;
491 struct filter_opts filter_opts;
492 struct antispoof_opts antispoof_opts;
493 struct queue_opts queue_opts;
494 struct scrub_opts scrub_opts;
495 struct table_opts table_opts;
496 struct pool_opts pool_opts;
497 struct node_hfsc_opts hfsc_opts;
498 struct node_fairq_opts fairq_opts;
499 struct codel_opts codel_opts;
500 struct pfctl_watermarks *watermarks;
501 } v;
502 int lineno;
503 } YYSTYPE;
504
505 #define PPORT_RANGE 1
506 #define PPORT_STAR 2
507 int parseport(char *, struct range *r, int);
508
509 #define DYNIF_MULTIADDR(addr) ((addr).type == PF_ADDR_DYNIFTL && \
510 (!((addr).iflags & PFI_AFLAG_NOALIAS) || \
511 !isdigit((addr).v.ifname[strlen((addr).v.ifname)-1])))
512
513 %}
514
515 %token PASS BLOCK MATCH SCRUB RETURN IN OS OUT LOG QUICK ON FROM TO FLAGS
516 %token RETURNRST RETURNICMP RETURNICMP6 PROTO INET INET6 ALL ANY ICMPTYPE
517 %token ICMP6TYPE CODE KEEP MODULATE STATE PORT RDR NAT BINAT ARROW NODF
518 %token MINTTL ERROR ALLOWOPTS FASTROUTE FILENAME ROUTETO DUPTO REPLYTO NO LABEL
519 %token NOROUTE URPFFAILED FRAGMENT USER GROUP MAXMSS MAXIMUM TTL TOS DROP TABLE
520 %token REASSEMBLE ANCHOR NATANCHOR RDRANCHOR BINATANCHOR
521 %token SET OPTIMIZATION TIMEOUT LIMIT LOGINTERFACE BLOCKPOLICY FAILPOLICY
522 %token RANDOMID REQUIREORDER SYNPROXY FINGERPRINTS NOSYNC DEBUG SKIP HOSTID
523 %token ANTISPOOF FOR INCLUDE KEEPCOUNTERS SYNCOOKIES L3 MATCHES
524 %token ETHER
525 %token BITMASK RANDOM SOURCEHASH ROUNDROBIN STATICPORT PROBABILITY MAPEPORTSET
526 %token ALTQ CBQ CODEL PRIQ HFSC FAIRQ BANDWIDTH TBRSIZE LINKSHARE REALTIME
527 %token UPPERLIMIT QUEUE PRIORITY QLIMIT HOGS BUCKETS RTABLE TARGET INTERVAL
528 %token DNPIPE DNQUEUE RIDENTIFIER
529 %token LOAD RULESET_OPTIMIZATION PRIO
530 %token STICKYADDRESS ENDPI MAXSRCSTATES MAXSRCNODES SOURCETRACK GLOBAL RULE
531 %token MAXSRCCONN MAXSRCCONNRATE OVERLOAD FLUSH SLOPPY PFLOW ALLOW_RELATED
532 %token TAGGED TAG IFBOUND FLOATING STATEPOLICY STATEDEFAULTS ROUTE SETTOS
533 %token DIVERTTO DIVERTREPLY BRIDGE_TO RECEIVEDON NE LE GE AFTO
534 %token <v.string> STRING
535 %token <v.number> NUMBER
536 %token <v.i> PORTBINARY
537 %type <v.interface> interface if_list if_item_not if_item
538 %type <v.number> number icmptype icmp6type uid gid
539 %type <v.number> tos not yesno optnodf
540 %type <v.probability> probability
541 %type <v.i> no dir af fragcache optimizer syncookie_val
542 %type <v.i> sourcetrack flush unaryop statelock
543 %type <v.i> etherprotoval
544 %type <v.b> action nataction natpasslog scrubaction
545 %type <v.b> flags flag blockspec prio
546 %type <v.range> portplain portstar portrange
547 %type <v.hashkey> hashkey
548 %type <v.proto> proto proto_list proto_item
549 %type <v.number> protoval
550 %type <v.icmp> icmpspec
551 %type <v.icmp> icmp_list icmp_item
552 %type <v.icmp> icmp6_list icmp6_item
553 %type <v.number> reticmpspec reticmp6spec
554 %type <v.fromto> fromto l3fromto
555 %type <v.peer> ipportspec from to
556 %type <v.host> ipspec toipspec xhost host dynaddr host_list
557 %type <v.host> redir_host_list redirspec
558 %type <v.host> route_host route_host_list routespec
559 %type <v.os> os xos os_list
560 %type <v.port> portspec port_list port_item
561 %type <v.uid> uids uid_list uid_item
562 %type <v.gid> gids gid_list gid_item
563 %type <v.route> route
564 %type <v.redirection> redirection redirpool
565 %type <v.string> label stringall tag anchorname
566 %type <v.string> string varstring numberstring
567 %type <v.keep_state> keep
568 %type <v.state_opt> state_opt_spec state_opt_list state_opt_item
569 %type <v.logquick> logquick quick log logopts logopt
570 %type <v.interface> antispoof_ifspc antispoof_iflst antispoof_if
571 %type <v.qassign> qname etherqname
572 %type <v.queue> qassign qassign_list qassign_item
573 %type <v.queue_options> scheduler
574 %type <v.number> cbqflags_list cbqflags_item
575 %type <v.number> priqflags_list priqflags_item
576 %type <v.hfsc_opts> hfscopts_list hfscopts_item hfsc_opts
577 %type <v.fairq_opts> fairqopts_list fairqopts_item fairq_opts
578 %type <v.codel_opts> codelopts_list codelopts_item codel_opts
579 %type <v.queue_bwspec> bandwidth
580 %type <v.filter_opts> filter_opts filter_opt filter_opts_l etherfilter_opts etherfilter_opt etherfilter_opts_l
581 %type <v.filter_opts> filter_sets filter_set filter_sets_l
582 %type <v.antispoof_opts> antispoof_opts antispoof_opt antispoof_opts_l
583 %type <v.queue_opts> queue_opts queue_opt queue_opts_l
584 %type <v.scrub_opts> scrub_opts scrub_opt scrub_opts_l
585 %type <v.table_opts> table_opts table_opt table_opts_l
586 %type <v.pool_opts> pool_opts pool_opt pool_opts_l
587 %type <v.tagged> tagged
588 %type <v.rtableid> rtable
589 %type <v.watermarks> syncookie_opts
590 %type <v.etherproto> etherproto etherproto_list etherproto_item
591 %type <v.etherfromto> etherfromto
592 %type <v.etheraddr> etherfrom etherto
593 %type <v.bridge_to> bridge
594 %type <v.mac> xmac mac mac_list macspec
595 %%
596
597 ruleset : /* empty */
598 | ruleset include '\n'
599 | ruleset '\n'
600 | ruleset option '\n'
601 | ruleset etherrule '\n'
602 | ruleset etheranchorrule '\n'
603 | ruleset scrubrule '\n'
604 | ruleset natrule '\n'
605 | ruleset binatrule '\n'
606 | ruleset pfrule '\n'
607 | ruleset anchorrule '\n'
608 | ruleset loadrule '\n'
609 | ruleset altqif '\n'
610 | ruleset queuespec '\n'
611 | ruleset varset '\n'
612 | ruleset antispoof '\n'
613 | ruleset tabledef '\n'
614 | '{' fakeanchor '}' '\n';
615 | ruleset error '\n' { file->errors++; }
616 ;
617
618 include : INCLUDE STRING {
619 struct file *nfile;
620
621 if ((nfile = pushfile($2, 0)) == NULL) {
622 yyerror("failed to include file %s", $2);
623 free($2);
624 YYERROR;
625 }
626 free($2);
627
628 file = nfile;
629 lungetc('\n');
630 }
631 ;
632
633 /*
634 * apply to previouslys specified rule: must be careful to note
635 * what that is: pf or nat or binat or rdr
636 */
637 fakeanchor : fakeanchor '\n'
638 | fakeanchor anchorrule '\n'
639 | fakeanchor binatrule '\n'
640 | fakeanchor natrule '\n'
641 | fakeanchor pfrule '\n'
642 | fakeanchor error '\n'
643 ;
644
645 optimizer : string {
646 if (!strcmp($1, "none"))
647 $$ = 0;
648 else if (!strcmp($1, "basic"))
649 $$ = PF_OPTIMIZE_BASIC;
650 else if (!strcmp($1, "profile"))
651 $$ = PF_OPTIMIZE_BASIC | PF_OPTIMIZE_PROFILE;
652 else {
653 yyerror("unknown ruleset-optimization %s", $1);
654 YYERROR;
655 }
656 }
657 ;
658
659 optnodf : /* empty */ { $$ = 0; }
660 | NODF { $$ = 1; }
661 ;
662
663 option : SET REASSEMBLE yesno optnodf {
664 if (check_rulestate(PFCTL_STATE_OPTION))
665 YYERROR;
666 pfctl_set_reassembly(pf, $3, $4);
667 }
668 | SET OPTIMIZATION STRING {
669 if (check_rulestate(PFCTL_STATE_OPTION)) {
670 free($3);
671 YYERROR;
672 }
673 if (pfctl_set_optimization(pf, $3) != 0) {
674 yyerror("unknown optimization %s", $3);
675 free($3);
676 YYERROR;
677 }
678 free($3);
679 }
680 | SET RULESET_OPTIMIZATION optimizer {
681 if (!(pf->opts & PF_OPT_OPTIMIZE)) {
682 pf->opts |= PF_OPT_OPTIMIZE;
683 pf->optimize = $3;
684 }
685 }
686 | SET TIMEOUT timeout_spec
687 | SET TIMEOUT '{' optnl timeout_list '}'
688 | SET LIMIT limit_spec
689 | SET LIMIT '{' optnl limit_list '}'
690 | SET LOGINTERFACE stringall {
691 if (check_rulestate(PFCTL_STATE_OPTION)) {
692 free($3);
693 YYERROR;
694 }
695 if (pfctl_set_logif(pf, $3) != 0) {
696 yyerror("error setting loginterface %s", $3);
697 free($3);
698 YYERROR;
699 }
700 free($3);
701 }
702 | SET HOSTID number {
703 if ($3 == 0 || $3 > UINT_MAX) {
704 yyerror("hostid must be non-zero");
705 YYERROR;
706 }
707 pfctl_set_hostid(pf, $3);
708 }
709 | SET BLOCKPOLICY DROP {
710 if (pf->opts & PF_OPT_VERBOSE)
711 printf("set block-policy drop\n");
712 if (check_rulestate(PFCTL_STATE_OPTION))
713 YYERROR;
714 blockpolicy = PFRULE_DROP;
715 }
716 | SET BLOCKPOLICY RETURN {
717 if (pf->opts & PF_OPT_VERBOSE)
718 printf("set block-policy return\n");
719 if (check_rulestate(PFCTL_STATE_OPTION))
720 YYERROR;
721 blockpolicy = PFRULE_RETURN;
722 }
723 | SET FAILPOLICY DROP {
724 if (pf->opts & PF_OPT_VERBOSE)
725 printf("set fail-policy drop\n");
726 if (check_rulestate(PFCTL_STATE_OPTION))
727 YYERROR;
728 failpolicy = PFRULE_DROP;
729 }
730 | SET FAILPOLICY RETURN {
731 if (pf->opts & PF_OPT_VERBOSE)
732 printf("set fail-policy return\n");
733 if (check_rulestate(PFCTL_STATE_OPTION))
734 YYERROR;
735 failpolicy = PFRULE_RETURN;
736 }
737 | SET REQUIREORDER yesno {
738 if (pf->opts & PF_OPT_VERBOSE)
739 printf("set require-order %s\n",
740 $3 == 1 ? "yes" : "no");
741 require_order = $3;
742 }
743 | SET FINGERPRINTS STRING {
744 if (pf->opts & PF_OPT_VERBOSE)
745 printf("set fingerprints \"%s\"\n", $3);
746 if (check_rulestate(PFCTL_STATE_OPTION)) {
747 free($3);
748 YYERROR;
749 }
750 if (!pf->anchor->name[0]) {
751 if (pfctl_file_fingerprints(pf->dev,
752 pf->opts, $3)) {
753 yyerror("error loading "
754 "fingerprints %s", $3);
755 free($3);
756 YYERROR;
757 }
758 }
759 free($3);
760 }
761 | SET STATEPOLICY statelock {
762 if (pf->opts & PF_OPT_VERBOSE)
763 switch ($3) {
764 case 0:
765 printf("set state-policy floating\n");
766 break;
767 case PFRULE_IFBOUND:
768 printf("set state-policy if-bound\n");
769 break;
770 }
771 default_statelock = $3;
772 }
773 | SET DEBUG STRING {
774 if (check_rulestate(PFCTL_STATE_OPTION)) {
775 free($3);
776 YYERROR;
777 }
778 if (pfctl_do_set_debug(pf, $3) != 0) {
779 yyerror("error setting debuglevel %s", $3);
780 free($3);
781 YYERROR;
782 }
783 free($3);
784 }
785 | SET SKIP interface {
786 if (expand_skip_interface($3) != 0) {
787 yyerror("error setting skip interface(s)");
788 YYERROR;
789 }
790 }
791 | SET STATEDEFAULTS state_opt_list {
792 if (keep_state_defaults != NULL) {
793 yyerror("cannot redefine state-defaults");
794 YYERROR;
795 }
796 keep_state_defaults = $3;
797 }
798 | SET KEEPCOUNTERS {
799 pf->keep_counters = true;
800 }
801 | SET SYNCOOKIES syncookie_val syncookie_opts {
802 if (pfctl_cfg_syncookies(pf, $3, $4)) {
803 yyerror("error setting syncookies");
804 YYERROR;
805 }
806 }
807 ;
808
809 syncookie_val : STRING {
810 if (!strcmp($1, "never"))
811 $$ = PFCTL_SYNCOOKIES_NEVER;
812 else if (!strcmp($1, "adaptive"))
813 $$ = PFCTL_SYNCOOKIES_ADAPTIVE;
814 else if (!strcmp($1, "always"))
815 $$ = PFCTL_SYNCOOKIES_ALWAYS;
816 else {
817 yyerror("illegal value for syncookies");
818 YYERROR;
819 }
820 }
821 ;
822 syncookie_opts : /* empty */ { $$ = NULL; }
823 | {
824 memset(&syncookie_opts, 0, sizeof(syncookie_opts));
825 } '(' syncookie_opt_l ')' { $$ = &syncookie_opts; }
826 ;
827
828 syncookie_opt_l : syncookie_opt_l comma syncookie_opt
829 | syncookie_opt
830 ;
831
832 syncookie_opt : STRING STRING {
833 double val;
834 char *cp;
835
836 val = strtod($2, &cp);
837 if (cp == NULL || strcmp(cp, "%"))
838 YYERROR;
839 if (val <= 0 || val > 100) {
840 yyerror("illegal percentage value");
841 YYERROR;
842 }
843 if (!strcmp($1, "start")) {
844 syncookie_opts.hi = val;
845 } else if (!strcmp($1, "end")) {
846 syncookie_opts.lo = val;
847 } else {
848 yyerror("illegal syncookie option");
849 YYERROR;
850 }
851 }
852 ;
853
854 stringall : STRING { $$ = $1; }
855 | ALL {
856 if (($$ = strdup("all")) == NULL) {
857 err(1, "stringall: strdup");
858 }
859 }
860 ;
861
862 string : STRING string {
863 if (asprintf(&$$, "%s %s", $1, $2) == -1)
864 err(1, "string: asprintf");
865 free($1);
866 free($2);
867 }
868 | STRING
869 ;
870
871 varstring : numberstring varstring {
872 if (asprintf(&$$, "%s %s", $1, $2) == -1)
873 err(1, "string: asprintf");
874 free($1);
875 free($2);
876 }
877 | numberstring
878 ;
879
880 numberstring : NUMBER {
881 char *s;
882 if (asprintf(&s, "%lld", (long long)$1) == -1) {
883 yyerror("string: asprintf");
884 YYERROR;
885 }
886 $$ = s;
887 }
888 | STRING
889 ;
890
891 varset : STRING '=' varstring {
892 char *s = $1;
893 if (pf->opts & PF_OPT_VERBOSE)
894 printf("%s = \"%s\"\n", $1, $3);
895 while (*s++) {
896 if (isspace((unsigned char)*s)) {
897 yyerror("macro name cannot contain "
898 "whitespace");
899 YYERROR;
900 }
901 }
902 if (symset($1, $3, 0) == -1)
903 err(1, "cannot store variable %s", $1);
904 free($1);
905 free($3);
906 }
907 ;
908
909 anchorname : STRING { $$ = $1; }
910 | /* empty */ { $$ = NULL; }
911 ;
912
913 pfa_anchorlist : /* empty */
914 | pfa_anchorlist '\n'
915 | pfa_anchorlist pfrule '\n'
916 | pfa_anchorlist anchorrule '\n'
917 ;
918
919 pfa_anchor : '{'
920 {
921 char ta[PF_ANCHOR_NAME_SIZE];
922 struct pfctl_ruleset *rs;
923
924 /* stepping into a brace anchor */
925 pf->asd++;
926 pf->bn++;
927
928 /*
929 * Anchor contents are parsed before the anchor rule
930 * production completes, so we don't know the real
931 * location yet. Create a holding ruleset in the root;
932 * contents will be moved afterwards.
933 */
934 snprintf(ta, PF_ANCHOR_NAME_SIZE, "_%d", pf->bn);
935 rs = pf_find_or_create_ruleset(ta);
936 if (rs == NULL)
937 err(1, "pfa_anchor: pf_find_or_create_ruleset");
938 pf->astack[pf->asd] = rs->anchor;
939 pf->anchor = rs->anchor;
940 } '\n' pfa_anchorlist '}'
941 {
942 pf->alast = pf->anchor;
943 pf->asd--;
944 pf->anchor = pf->astack[pf->asd];
945 }
946 | /* empty */
947 ;
948
949 anchorrule : ANCHOR anchorname dir quick interface af proto fromto
950 filter_opts pfa_anchor
951 {
952 struct pfctl_rule r;
953 struct node_proto *proto;
954
955 if (check_rulestate(PFCTL_STATE_FILTER)) {
956 if ($2)
957 free($2);
958 YYERROR;
959 }
960
961 if ($2 && ($2[0] == '_' || strstr($2, "/_") != NULL)) {
962 free($2);
963 yyerror("anchor names beginning with '_' "
964 "are reserved for internal use");
965 YYERROR;
966 }
967
968 memset(&r, 0, sizeof(r));
969 if (pf->astack[pf->asd + 1]) {
970 if ($2 && strchr($2, '/') != NULL) {
971 free($2);
972 yyerror("anchor paths containing '/' "
973 "cannot be used for inline anchors.");
974 YYERROR;
975 }
976
977 /* Move inline rules into relative location. */
978 pfctl_anchor_setup(&r,
979 &pf->astack[pf->asd]->ruleset,
980 $2 ? $2 : pf->alast->name);
981
982 if (r.anchor == NULL)
983 err(1, "anchorrule: unable to "
984 "create ruleset");
985
986 if (pf->alast != r.anchor) {
987 if (r.anchor->match) {
988 yyerror("inline anchor '%s' "
989 "already exists",
990 r.anchor->name);
991 YYERROR;
992 }
993 mv_rules(&pf->alast->ruleset,
994 &r.anchor->ruleset);
995 }
996 pf_remove_if_empty_ruleset(&pf->alast->ruleset);
997 pf->alast = r.anchor;
998 } else {
999 if (!$2) {
1000 yyerror("anchors without explicit "
1001 "rules must specify a name");
1002 YYERROR;
1003 }
1004 }
1005 r.direction = $3;
1006 r.quick = $4.quick;
1007 r.af = $6;
1008 r.prob = $9.prob;
1009 r.rtableid = $9.rtableid;
1010 r.ridentifier = $9.ridentifier;
1011
1012 if ($9.tag)
1013 if (strlcpy(r.tagname, $9.tag,
1014 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1015 yyerror("tag too long, max %u chars",
1016 PF_TAG_NAME_SIZE - 1);
1017 YYERROR;
1018 }
1019 if ($9.match_tag)
1020 if (strlcpy(r.match_tagname, $9.match_tag,
1021 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1022 yyerror("tag too long, max %u chars",
1023 PF_TAG_NAME_SIZE - 1);
1024 YYERROR;
1025 }
1026 r.match_tag_not = $9.match_tag_not;
1027 if (rule_label(&r, $9.label))
1028 YYERROR;
1029 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++)
1030 free($9.label[i]);
1031 r.flags = $9.flags.b1;
1032 r.flagset = $9.flags.b2;
1033 if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
1034 yyerror("flags always false");
1035 YYERROR;
1036 }
1037 if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
1038 for (proto = $7; proto != NULL &&
1039 proto->proto != IPPROTO_TCP;
1040 proto = proto->next)
1041 ; /* nothing */
1042 if (proto == NULL && $7 != NULL) {
1043 if ($9.flags.b1 || $9.flags.b2)
1044 yyerror(
1045 "flags only apply to tcp");
1046 if ($8.src_os)
1047 yyerror(
1048 "OS fingerprinting only "
1049 "applies to tcp");
1050 YYERROR;
1051 }
1052 }
1053
1054 r.tos = $9.tos;
1055
1056 if ($9.keep.action) {
1057 yyerror("cannot specify state handling "
1058 "on anchors");
1059 YYERROR;
1060 }
1061
1062 if ($9.match_tag)
1063 if (strlcpy(r.match_tagname, $9.match_tag,
1064 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1065 yyerror("tag too long, max %u chars",
1066 PF_TAG_NAME_SIZE - 1);
1067 YYERROR;
1068 }
1069 r.match_tag_not = $9.match_tag_not;
1070 if ($9.marker & FOM_PRIO) {
1071 if ($9.prio == 0)
1072 r.prio = PF_PRIO_ZERO;
1073 else
1074 r.prio = $9.prio;
1075 }
1076 if ($9.marker & FOM_SETPRIO) {
1077 r.set_prio[0] = $9.set_prio[0];
1078 r.set_prio[1] = $9.set_prio[1];
1079 r.scrub_flags |= PFSTATE_SETPRIO;
1080 }
1081
1082 decide_address_family($8.src.host, &r.af);
1083 decide_address_family($8.dst.host, &r.af);
1084
1085 expand_rule(&r, $5, NULL, NULL, NULL, NULL, NULL, NULL,
1086 $7, $8.src_os, $8.src.host, $8.src.port, $8.dst.host,
1087 $8.dst.port, $9.uid, $9.gid, $9.rcv, $9.icmpspec,
1088 pf->astack[pf->asd + 1] ? pf->alast->name : $2);
1089 free($2);
1090 pf->astack[pf->asd + 1] = NULL;
1091 }
1092 | NATANCHOR string interface af proto fromto rtable {
1093 struct pfctl_rule r;
1094
1095 if (check_rulestate(PFCTL_STATE_NAT)) {
1096 free($2);
1097 YYERROR;
1098 }
1099
1100 memset(&r, 0, sizeof(r));
1101 r.action = PF_NAT;
1102 r.af = $4;
1103 r.rtableid = $7;
1104
1105 decide_address_family($6.src.host, &r.af);
1106 decide_address_family($6.dst.host, &r.af);
1107
1108 expand_rule(&r, $3, NULL, NULL, NULL, NULL, NULL, NULL,
1109 $5, $6.src_os, $6.src.host, $6.src.port, $6.dst.host,
1110 $6.dst.port, 0, 0, 0, 0, $2);
1111 free($2);
1112 }
1113 | RDRANCHOR string interface af proto fromto rtable {
1114 struct pfctl_rule r;
1115
1116 if (check_rulestate(PFCTL_STATE_NAT)) {
1117 free($2);
1118 YYERROR;
1119 }
1120
1121 memset(&r, 0, sizeof(r));
1122 r.action = PF_RDR;
1123 r.af = $4;
1124 r.rtableid = $7;
1125
1126 decide_address_family($6.src.host, &r.af);
1127 decide_address_family($6.dst.host, &r.af);
1128
1129 if ($6.src.port != NULL) {
1130 yyerror("source port parameter not supported"
1131 " in rdr-anchor");
1132 YYERROR;
1133 }
1134 if ($6.dst.port != NULL) {
1135 if ($6.dst.port->next != NULL) {
1136 yyerror("destination port list "
1137 "expansion not supported in "
1138 "rdr-anchor");
1139 YYERROR;
1140 } else if ($6.dst.port->op != PF_OP_EQ) {
1141 yyerror("destination port operators"
1142 " not supported in rdr-anchor");
1143 YYERROR;
1144 }
1145 r.dst.port[0] = $6.dst.port->port[0];
1146 r.dst.port[1] = $6.dst.port->port[1];
1147 r.dst.port_op = $6.dst.port->op;
1148 }
1149
1150 expand_rule(&r, $3, NULL, NULL, NULL, NULL, NULL, NULL,
1151 $5, $6.src_os, $6.src.host, $6.src.port, $6.dst.host,
1152 $6.dst.port, 0, 0, 0, 0, $2);
1153 free($2);
1154 }
1155 | BINATANCHOR string interface af proto fromto rtable {
1156 struct pfctl_rule r;
1157
1158 if (check_rulestate(PFCTL_STATE_NAT)) {
1159 free($2);
1160 YYERROR;
1161 }
1162
1163 memset(&r, 0, sizeof(r));
1164 r.action = PF_BINAT;
1165 r.af = $4;
1166 r.rtableid = $7;
1167 if ($5 != NULL) {
1168 if ($5->next != NULL) {
1169 yyerror("proto list expansion"
1170 " not supported in binat-anchor");
1171 YYERROR;
1172 }
1173 r.proto = $5->proto;
1174 free($5);
1175 }
1176
1177 if ($6.src.host != NULL || $6.src.port != NULL ||
1178 $6.dst.host != NULL || $6.dst.port != NULL) {
1179 yyerror("fromto parameter not supported"
1180 " in binat-anchor");
1181 YYERROR;
1182 }
1183
1184 decide_address_family($6.src.host, &r.af);
1185 decide_address_family($6.dst.host, &r.af);
1186
1187 pfctl_append_rule(pf, &r, $2);
1188 free($2);
1189 }
1190 ;
1191
1192 loadrule : LOAD ANCHOR string FROM string {
1193 struct loadanchors *loadanchor;
1194
1195 if (strlen(pf->anchor->name) + 1 +
1196 strlen($3) >= MAXPATHLEN) {
1197 yyerror("anchorname %s too long, max %u\n",
1198 $3, MAXPATHLEN - 1);
1199 free($3);
1200 YYERROR;
1201 }
1202 loadanchor = calloc(1, sizeof(struct loadanchors));
1203 if (loadanchor == NULL)
1204 err(1, "loadrule: calloc");
1205 if ((loadanchor->anchorname = malloc(MAXPATHLEN)) ==
1206 NULL)
1207 err(1, "loadrule: malloc");
1208 if (pf->anchor->name[0])
1209 snprintf(loadanchor->anchorname, MAXPATHLEN,
1210 "%s/%s", pf->anchor->name, $3);
1211 else
1212 strlcpy(loadanchor->anchorname, $3, MAXPATHLEN);
1213 if ((loadanchor->filename = strdup($5)) == NULL)
1214 err(1, "loadrule: strdup");
1215
1216 TAILQ_INSERT_TAIL(&loadanchorshead, loadanchor,
1217 entries);
1218
1219 free($3);
1220 free($5);
1221 };
1222
1223 scrubaction : no SCRUB {
1224 $$.b2 = $$.w = 0;
1225 if ($1)
1226 $$.b1 = PF_NOSCRUB;
1227 else
1228 $$.b1 = PF_SCRUB;
1229 }
1230 ;
1231
1232 etherrule : ETHER action dir quick interface bridge etherproto etherfromto l3fromto etherfilter_opts
1233 {
1234 struct pfctl_eth_rule r;
1235
1236 bzero(&r, sizeof(r));
1237
1238 if (check_rulestate(PFCTL_STATE_ETHER))
1239 YYERROR;
1240
1241 r.action = $2.b1;
1242 r.direction = $3;
1243 r.quick = $4.quick;
1244 if ($10.tag != NULL)
1245 strlcpy(r.tagname, $10.tag, sizeof(r.tagname));
1246 if ($10.match_tag)
1247 if (strlcpy(r.match_tagname, $10.match_tag,
1248 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1249 yyerror("tag too long, max %u chars",
1250 PF_TAG_NAME_SIZE - 1);
1251 YYERROR;
1252 }
1253 r.match_tag_not = $10.match_tag_not;
1254 if ($10.queues.qname != NULL)
1255 strlcpy(r.qname, $10.queues.qname, sizeof(r.qname));
1256 r.dnpipe = $10.dnpipe;
1257 r.dnflags = $10.free_flags;
1258 if (eth_rule_label(&r, $10.label))
1259 YYERROR;
1260 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++)
1261 free($10.label[i]);
1262 r.ridentifier = $10.ridentifier;
1263
1264 expand_eth_rule(&r, $5, $7, $8.src, $8.dst,
1265 $9.src.host, $9.dst.host, $6, "");
1266 }
1267 ;
1268
1269 etherpfa_anchorlist : /* empty */
1270 | etherpfa_anchorlist '\n'
1271 | etherpfa_anchorlist etherrule '\n'
1272 | etherpfa_anchorlist etheranchorrule '\n'
1273 ;
1274
1275 etherpfa_anchor : '{'
1276 {
1277 char ta[PF_ANCHOR_NAME_SIZE];
1278 struct pfctl_eth_ruleset *rs;
1279
1280 /* steping into a brace anchor */
1281 pf->asd++;
1282 pf->bn++;
1283
1284 /* create a holding ruleset in the root */
1285 snprintf(ta, PF_ANCHOR_NAME_SIZE, "_%d", pf->bn);
1286 rs = pf_find_or_create_eth_ruleset(ta);
1287 if (rs == NULL)
1288 err(1, "etherpfa_anchor: pf_find_or_create_eth_ruleset");
1289 pf->eastack[pf->asd] = rs->anchor;
1290 pf->eanchor = rs->anchor;
1291 } '\n' etherpfa_anchorlist '}'
1292 {
1293 pf->ealast = pf->eanchor;
1294 pf->asd--;
1295 pf->eanchor = pf->eastack[pf->asd];
1296 }
1297 | /* empty */
1298 ;
1299
1300 etheranchorrule : ETHER ANCHOR anchorname dir quick interface etherproto etherfromto l3fromto etherpfa_anchor
1301 {
1302 struct pfctl_eth_rule r;
1303
1304 if (check_rulestate(PFCTL_STATE_ETHER)) {
1305 free($3);
1306 YYERROR;
1307 }
1308
1309 if ($3 && ($3[0] == '_' || strstr($3, "/_") != NULL)) {
1310 free($3);
1311 yyerror("anchor names beginning with '_' "
1312 "are reserved for internal use");
1313 YYERROR;
1314 }
1315
1316 memset(&r, 0, sizeof(r));
1317 if (pf->eastack[pf->asd + 1]) {
1318 if ($3 && strchr($3, '/') != NULL) {
1319 free($3);
1320 yyerror("anchor paths containing '/' "
1321 "cannot be used for inline anchors.");
1322 YYERROR;
1323 }
1324
1325 /* Move inline rules into relative location. */
1326 pfctl_eth_anchor_setup(pf, &r,
1327 &pf->eastack[pf->asd]->ruleset,
1328 $3 ? $3 : pf->ealast->name);
1329 if (r.anchor == NULL)
1330 err(1, "etheranchorrule: unable to "
1331 "create ruleset");
1332
1333 if (pf->ealast != r.anchor) {
1334 if (r.anchor->match) {
1335 yyerror("inline anchor '%s' "
1336 "already exists",
1337 r.anchor->name);
1338 YYERROR;
1339 }
1340 mv_eth_rules(&pf->ealast->ruleset,
1341 &r.anchor->ruleset);
1342 }
1343 pf_remove_if_empty_eth_ruleset(&pf->ealast->ruleset);
1344 pf->ealast = r.anchor;
1345 } else {
1346 if (!$3) {
1347 yyerror("anchors without explicit "
1348 "rules must specify a name");
1349 YYERROR;
1350 }
1351 }
1352
1353 r.direction = $4;
1354 r.quick = $5.quick;
1355
1356 expand_eth_rule(&r, $6, $7, $8.src, $8.dst,
1357 $9.src.host, $9.dst.host, NULL,
1358 pf->eastack[pf->asd + 1] ? pf->ealast->name : $3);
1359
1360 free($3);
1361 pf->eastack[pf->asd + 1] = NULL;
1362 }
1363 ;
1364
1365 etherfilter_opts : {
1366 bzero(&filter_opts, sizeof filter_opts);
1367 }
1368 etherfilter_opts_l
1369 { $$ = filter_opts; }
1370 | /* empty */ {
1371 bzero(&filter_opts, sizeof filter_opts);
1372 $$ = filter_opts;
1373 }
1374 ;
1375
1376 etherfilter_opts_l : etherfilter_opts_l etherfilter_opt
1377 | etherfilter_opt
1378
1379 etherfilter_opt : etherqname {
1380 if (filter_opts.queues.qname) {
1381 yyerror("queue cannot be redefined");
1382 YYERROR;
1383 }
1384 filter_opts.queues = $1;
1385 }
1386 | RIDENTIFIER number {
1387 filter_opts.ridentifier = $2;
1388 }
1389 | label {
1390 if (filter_opts.labelcount >= PF_RULE_MAX_LABEL_COUNT) {
1391 yyerror("label can only be used %d times", PF_RULE_MAX_LABEL_COUNT);
1392 YYERROR;
1393 }
1394 filter_opts.label[filter_opts.labelcount++] = $1;
1395 }
1396 | TAG string {
1397 filter_opts.tag = $2;
1398 }
1399 | not TAGGED string {
1400 filter_opts.match_tag = $3;
1401 filter_opts.match_tag_not = $1;
1402 }
1403 | DNPIPE number {
1404 filter_opts.dnpipe = $2;
1405 filter_opts.free_flags |= PFRULE_DN_IS_PIPE;
1406 }
1407 | DNQUEUE number {
1408 filter_opts.dnpipe = $2;
1409 filter_opts.free_flags |= PFRULE_DN_IS_QUEUE;
1410 }
1411 ;
1412
1413 bridge : /* empty */ {
1414 $$ = NULL;
1415 }
1416 | BRIDGE_TO STRING {
1417 $$ = strdup($2);
1418 }
1419 ;
1420
1421 scrubrule : scrubaction dir logquick interface af proto fromto scrub_opts
1422 {
1423 struct pfctl_rule r;
1424
1425 if (check_rulestate(PFCTL_STATE_SCRUB))
1426 YYERROR;
1427
1428 memset(&r, 0, sizeof(r));
1429
1430 r.action = $1.b1;
1431 r.direction = $2;
1432
1433 r.log = $3.log;
1434 r.logif = $3.logif;
1435 if ($3.quick) {
1436 yyerror("scrub rules do not support 'quick'");
1437 YYERROR;
1438 }
1439
1440 r.af = $5;
1441 if ($8.nodf)
1442 r.rule_flag |= PFRULE_NODF;
1443 if ($8.randomid)
1444 r.rule_flag |= PFRULE_RANDOMID;
1445 if ($8.reassemble_tcp) {
1446 if (r.direction != PF_INOUT) {
1447 yyerror("reassemble tcp rules can not "
1448 "specify direction");
1449 YYERROR;
1450 }
1451 r.rule_flag |= PFRULE_REASSEMBLE_TCP;
1452 }
1453 if ($8.minttl)
1454 r.min_ttl = $8.minttl;
1455 if ($8.maxmss)
1456 r.max_mss = $8.maxmss;
1457 if ($8.marker & FOM_SETTOS) {
1458 r.rule_flag |= PFRULE_SET_TOS;
1459 r.set_tos = $8.settos;
1460 }
1461 if ($8.fragcache)
1462 r.rule_flag |= $8.fragcache;
1463 if ($8.match_tag)
1464 if (strlcpy(r.match_tagname, $8.match_tag,
1465 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
1466 yyerror("tag too long, max %u chars",
1467 PF_TAG_NAME_SIZE - 1);
1468 YYERROR;
1469 }
1470 r.match_tag_not = $8.match_tag_not;
1471 r.rtableid = $8.rtableid;
1472
1473 expand_rule(&r, $4, NULL, NULL, NULL, NULL, NULL, NULL,
1474 $6, $7.src_os, $7.src.host, $7.src.port, $7.dst.host,
1475 $7.dst.port, NULL, NULL, NULL, NULL, "");
1476 }
1477 ;
1478
1479 scrub_opts : {
1480 bzero(&scrub_opts, sizeof scrub_opts);
1481 scrub_opts.rtableid = -1;
1482 }
1483 scrub_opts_l
1484 { $$ = scrub_opts; }
1485 | /* empty */ {
1486 bzero(&scrub_opts, sizeof scrub_opts);
1487 scrub_opts.rtableid = -1;
1488 $$ = scrub_opts;
1489 }
1490 ;
1491
1492 scrub_opts_l : scrub_opts_l comma scrub_opt
1493 | scrub_opt
1494 ;
1495
1496 scrub_opt : NODF {
1497 if (scrub_opts.nodf) {
1498 yyerror("no-df cannot be respecified");
1499 YYERROR;
1500 }
1501 scrub_opts.nodf = 1;
1502 }
1503 | MINTTL NUMBER {
1504 if (scrub_opts.marker & FOM_MINTTL) {
1505 yyerror("min-ttl cannot be respecified");
1506 YYERROR;
1507 }
1508 if ($2 < 0 || $2 > 255) {
1509 yyerror("illegal min-ttl value %d", $2);
1510 YYERROR;
1511 }
1512 scrub_opts.marker |= FOM_MINTTL;
1513 scrub_opts.minttl = $2;
1514 }
1515 | MAXMSS NUMBER {
1516 if (scrub_opts.marker & FOM_MAXMSS) {
1517 yyerror("max-mss cannot be respecified");
1518 YYERROR;
1519 }
1520 if ($2 < 0 || $2 > 65535) {
1521 yyerror("illegal max-mss value %d", $2);
1522 YYERROR;
1523 }
1524 scrub_opts.marker |= FOM_MAXMSS;
1525 scrub_opts.maxmss = $2;
1526 }
1527 | SETTOS tos {
1528 if (scrub_opts.marker & FOM_SETTOS) {
1529 yyerror("set-tos cannot be respecified");
1530 YYERROR;
1531 }
1532 scrub_opts.marker |= FOM_SETTOS;
1533 scrub_opts.settos = $2;
1534 }
1535 | fragcache {
1536 if (scrub_opts.marker & FOM_FRAGCACHE) {
1537 yyerror("fragcache cannot be respecified");
1538 YYERROR;
1539 }
1540 scrub_opts.marker |= FOM_FRAGCACHE;
1541 scrub_opts.fragcache = $1;
1542 }
1543 | REASSEMBLE STRING {
1544 if (strcasecmp($2, "tcp") != 0) {
1545 yyerror("scrub reassemble supports only tcp, "
1546 "not '%s'", $2);
1547 free($2);
1548 YYERROR;
1549 }
1550 free($2);
1551 if (scrub_opts.reassemble_tcp) {
1552 yyerror("reassemble tcp cannot be respecified");
1553 YYERROR;
1554 }
1555 scrub_opts.reassemble_tcp = 1;
1556 }
1557 | RANDOMID {
1558 if (scrub_opts.randomid) {
1559 yyerror("random-id cannot be respecified");
1560 YYERROR;
1561 }
1562 scrub_opts.randomid = 1;
1563 }
1564 | RTABLE NUMBER {
1565 if ($2 < 0 || $2 > rt_tableid_max()) {
1566 yyerror("invalid rtable id");
1567 YYERROR;
1568 }
1569 scrub_opts.rtableid = $2;
1570 }
1571 | not TAGGED string {
1572 scrub_opts.match_tag = $3;
1573 scrub_opts.match_tag_not = $1;
1574 }
1575 ;
1576
1577 fragcache : FRAGMENT REASSEMBLE { $$ = 0; /* default */ }
1578 | FRAGMENT NO REASSEMBLE { $$ = PFRULE_FRAGMENT_NOREASS; }
1579 ;
1580
1581 antispoof : ANTISPOOF logquick antispoof_ifspc af antispoof_opts {
1582 struct pfctl_rule r;
1583 struct node_host *h = NULL, *hh;
1584 struct node_if *i, *j;
1585
1586 if (check_rulestate(PFCTL_STATE_FILTER))
1587 YYERROR;
1588
1589 for (i = $3; i; i = i->next) {
1590 bzero(&r, sizeof(r));
1591
1592 r.action = PF_DROP;
1593 r.direction = PF_IN;
1594 r.log = $2.log;
1595 r.logif = $2.logif;
1596 r.quick = $2.quick;
1597 r.af = $4;
1598 r.ridentifier = $5.ridentifier;
1599 if (rule_label(&r, $5.label))
1600 YYERROR;
1601 r.rtableid = $5.rtableid;
1602 j = calloc(1, sizeof(struct node_if));
1603 if (j == NULL)
1604 err(1, "antispoof: calloc");
1605 if (strlcpy(j->ifname, i->ifname,
1606 sizeof(j->ifname)) >= sizeof(j->ifname)) {
1607 free(j);
1608 yyerror("interface name too long");
1609 YYERROR;
1610 }
1611 j->not = 1;
1612 if (i->dynamic) {
1613 h = calloc(1, sizeof(*h));
1614 if (h == NULL)
1615 err(1, "address: calloc");
1616 h->addr.type = PF_ADDR_DYNIFTL;
1617 set_ipmask(h, 128);
1618 if (strlcpy(h->addr.v.ifname, i->ifname,
1619 sizeof(h->addr.v.ifname)) >=
1620 sizeof(h->addr.v.ifname)) {
1621 free(h);
1622 yyerror(
1623 "interface name too long");
1624 YYERROR;
1625 }
1626 hh = malloc(sizeof(*hh));
1627 if (hh == NULL)
1628 err(1, "address: malloc");
1629 bcopy(h, hh, sizeof(*hh));
1630 h->addr.iflags = PFI_AFLAG_NETWORK;
1631 } else {
1632 h = ifa_lookup(j->ifname,
1633 PFI_AFLAG_NETWORK);
1634 hh = NULL;
1635 }
1636
1637 if (h != NULL)
1638 expand_rule(&r, j, NULL, NULL, NULL, NULL, NULL, NULL,
1639 NULL, NULL, h, NULL, NULL, NULL, NULL, NULL,
1640 NULL, NULL, "");
1641
1642 if ((i->ifa_flags & IFF_LOOPBACK) == 0) {
1643 bzero(&r, sizeof(r));
1644
1645 r.action = PF_DROP;
1646 r.direction = PF_IN;
1647 r.log = $2.log;
1648 r.logif = $2.logif;
1649 r.quick = $2.quick;
1650 r.af = $4;
1651 r.ridentifier = $5.ridentifier;
1652 if (rule_label(&r, $5.label))
1653 YYERROR;
1654 r.rtableid = $5.rtableid;
1655 if (hh != NULL)
1656 h = hh;
1657 else
1658 h = ifa_lookup(i->ifname, 0);
1659 if (h != NULL)
1660 expand_rule(&r, NULL, NULL, NULL, NULL, NULL,
1661 NULL, NULL, NULL, NULL, h, NULL, NULL,
1662 NULL, NULL, NULL, NULL, NULL, "");
1663 } else
1664 free(hh);
1665 }
1666 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++)
1667 free($5.label[i]);
1668 }
1669 ;
1670
1671 antispoof_ifspc : FOR antispoof_if { $$ = $2; }
1672 | FOR '{' optnl antispoof_iflst '}' { $$ = $4; }
1673 ;
1674
1675 antispoof_iflst : antispoof_if optnl { $$ = $1; }
1676 | antispoof_iflst comma antispoof_if optnl {
1677 $1->tail->next = $3;
1678 $1->tail = $3;
1679 $$ = $1;
1680 }
1681 ;
1682
1683 antispoof_if : if_item { $$ = $1; }
1684 | '(' if_item ')' {
1685 $2->dynamic = 1;
1686 $$ = $2;
1687 }
1688 ;
1689
1690 antispoof_opts : {
1691 bzero(&antispoof_opts, sizeof antispoof_opts);
1692 antispoof_opts.rtableid = -1;
1693 }
1694 antispoof_opts_l
1695 { $$ = antispoof_opts; }
1696 | /* empty */ {
1697 bzero(&antispoof_opts, sizeof antispoof_opts);
1698 antispoof_opts.rtableid = -1;
1699 $$ = antispoof_opts;
1700 }
1701 ;
1702
1703 antispoof_opts_l : antispoof_opts_l antispoof_opt
1704 | antispoof_opt
1705 ;
1706
1707 antispoof_opt : label {
1708 if (antispoof_opts.labelcount >= PF_RULE_MAX_LABEL_COUNT) {
1709 yyerror("label can only be used %d times", PF_RULE_MAX_LABEL_COUNT);
1710 YYERROR;
1711 }
1712 antispoof_opts.label[antispoof_opts.labelcount++] = $1;
1713 }
1714 | RIDENTIFIER number {
1715 antispoof_opts.ridentifier = $2;
1716 }
1717 | RTABLE NUMBER {
1718 if ($2 < 0 || $2 > rt_tableid_max()) {
1719 yyerror("invalid rtable id");
1720 YYERROR;
1721 }
1722 antispoof_opts.rtableid = $2;
1723 }
1724 ;
1725
1726 not : '!' { $$ = 1; }
1727 | /* empty */ { $$ = 0; }
1728 ;
1729
1730 tabledef : TABLE '<' STRING '>' table_opts {
1731 struct node_host *h, *nh;
1732 struct node_tinit *ti, *nti;
1733
1734 if (strlen($3) >= PF_TABLE_NAME_SIZE) {
1735 yyerror("table name too long, max %d chars",
1736 PF_TABLE_NAME_SIZE - 1);
1737 free($3);
1738 YYERROR;
1739 }
1740 if (pf->loadopt & PFCTL_FLAG_TABLE)
1741 if (process_tabledef($3, &$5)) {
1742 free($3);
1743 YYERROR;
1744 }
1745 free($3);
1746 for (ti = SIMPLEQ_FIRST(&$5.init_nodes);
1747 ti != SIMPLEQ_END(&$5.init_nodes); ti = nti) {
1748 if (ti->file)
1749 free(ti->file);
1750 for (h = ti->host; h != NULL; h = nh) {
1751 nh = h->next;
1752 free(h);
1753 }
1754 nti = SIMPLEQ_NEXT(ti, entries);
1755 free(ti);
1756 }
1757 }
1758 ;
1759
1760 table_opts : {
1761 bzero(&table_opts, sizeof table_opts);
1762 SIMPLEQ_INIT(&table_opts.init_nodes);
1763 }
1764 table_opts_l
1765 { $$ = table_opts; }
1766 | /* empty */
1767 {
1768 bzero(&table_opts, sizeof table_opts);
1769 SIMPLEQ_INIT(&table_opts.init_nodes);
1770 $$ = table_opts;
1771 }
1772 ;
1773
1774 table_opts_l : table_opts_l table_opt
1775 | table_opt
1776 ;
1777
1778 table_opt : STRING {
1779 if (!strcmp($1, "const"))
1780 table_opts.flags |= PFR_TFLAG_CONST;
1781 else if (!strcmp($1, "persist"))
1782 table_opts.flags |= PFR_TFLAG_PERSIST;
1783 else if (!strcmp($1, "counters"))
1784 table_opts.flags |= PFR_TFLAG_COUNTERS;
1785 else {
1786 yyerror("invalid table option '%s'", $1);
1787 free($1);
1788 YYERROR;
1789 }
1790 free($1);
1791 }
1792 | '{' optnl '}' { table_opts.init_addr = 1; }
1793 | '{' optnl host_list '}' {
1794 struct node_host *n;
1795 struct node_tinit *ti;
1796
1797 for (n = $3; n != NULL; n = n->next) {
1798 switch (n->addr.type) {
1799 case PF_ADDR_ADDRMASK:
1800 continue; /* ok */
1801 case PF_ADDR_RANGE:
1802 yyerror("address ranges are not "
1803 "permitted inside tables");
1804 break;
1805 case PF_ADDR_DYNIFTL:
1806 yyerror("dynamic addresses are not "
1807 "permitted inside tables");
1808 break;
1809 case PF_ADDR_TABLE:
1810 yyerror("tables cannot contain tables");
1811 break;
1812 case PF_ADDR_NOROUTE:
1813 yyerror("\"no-route\" is not permitted "
1814 "inside tables");
1815 break;
1816 case PF_ADDR_URPFFAILED:
1817 yyerror("\"urpf-failed\" is not "
1818 "permitted inside tables");
1819 break;
1820 default:
1821 yyerror("unknown address type %d",
1822 n->addr.type);
1823 }
1824 YYERROR;
1825 }
1826 if (!(ti = calloc(1, sizeof(*ti))))
1827 err(1, "table_opt: calloc");
1828 ti->host = $3;
1829 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1830 entries);
1831 table_opts.init_addr = 1;
1832 }
1833 | FILENAME STRING {
1834 struct node_tinit *ti;
1835
1836 if (!(ti = calloc(1, sizeof(*ti))))
1837 err(1, "table_opt: calloc");
1838 ti->file = $2;
1839 SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
1840 entries);
1841 table_opts.init_addr = 1;
1842 }
1843 ;
1844
1845 altqif : ALTQ interface queue_opts QUEUE qassign {
1846 struct pf_altq a;
1847
1848 if (check_rulestate(PFCTL_STATE_QUEUE))
1849 YYERROR;
1850
1851 memset(&a, 0, sizeof(a));
1852 if ($3.scheduler.qtype == ALTQT_NONE) {
1853 yyerror("no scheduler specified!");
1854 YYERROR;
1855 }
1856 a.scheduler = $3.scheduler.qtype;
1857 a.qlimit = $3.qlimit;
1858 a.tbrsize = $3.tbrsize;
1859 if ($5 == NULL && $3.scheduler.qtype != ALTQT_CODEL) {
1860 yyerror("no child queues specified");
1861 YYERROR;
1862 }
1863 if (expand_altq(&a, $2, $5, $3.queue_bwspec,
1864 &$3.scheduler))
1865 YYERROR;
1866 }
1867 ;
1868
1869 queuespec : QUEUE STRING interface queue_opts qassign {
1870 struct pf_altq a;
1871
1872 if (check_rulestate(PFCTL_STATE_QUEUE)) {
1873 free($2);
1874 YYERROR;
1875 }
1876
1877 memset(&a, 0, sizeof(a));
1878
1879 if (strlcpy(a.qname, $2, sizeof(a.qname)) >=
1880 sizeof(a.qname)) {
1881 yyerror("queue name too long (max "
1882 "%d chars)", PF_QNAME_SIZE-1);
1883 free($2);
1884 YYERROR;
1885 }
1886 free($2);
1887 if ($4.tbrsize) {
1888 yyerror("cannot specify tbrsize for queue");
1889 YYERROR;
1890 }
1891 if ($4.priority > 255) {
1892 yyerror("priority out of range: max 255");
1893 YYERROR;
1894 }
1895 a.priority = $4.priority;
1896 a.qlimit = $4.qlimit;
1897 a.scheduler = $4.scheduler.qtype;
1898 if (expand_queue(&a, $3, $5, $4.queue_bwspec,
1899 &$4.scheduler)) {
1900 yyerror("errors in queue definition");
1901 YYERROR;
1902 }
1903 }
1904 ;
1905
1906 queue_opts : {
1907 bzero(&queue_opts, sizeof queue_opts);
1908 queue_opts.priority = DEFAULT_PRIORITY;
1909 queue_opts.qlimit = DEFAULT_QLIMIT;
1910 queue_opts.scheduler.qtype = ALTQT_NONE;
1911 queue_opts.queue_bwspec.bw_percent = 100;
1912 }
1913 queue_opts_l
1914 { $$ = queue_opts; }
1915 | /* empty */ {
1916 bzero(&queue_opts, sizeof queue_opts);
1917 queue_opts.priority = DEFAULT_PRIORITY;
1918 queue_opts.qlimit = DEFAULT_QLIMIT;
1919 queue_opts.scheduler.qtype = ALTQT_NONE;
1920 queue_opts.queue_bwspec.bw_percent = 100;
1921 $$ = queue_opts;
1922 }
1923 ;
1924
1925 queue_opts_l : queue_opts_l queue_opt
1926 | queue_opt
1927 ;
1928
1929 queue_opt : BANDWIDTH bandwidth {
1930 if (queue_opts.marker & QOM_BWSPEC) {
1931 yyerror("bandwidth cannot be respecified");
1932 YYERROR;
1933 }
1934 queue_opts.marker |= QOM_BWSPEC;
1935 queue_opts.queue_bwspec = $2;
1936 }
1937 | PRIORITY NUMBER {
1938 if (queue_opts.marker & QOM_PRIORITY) {
1939 yyerror("priority cannot be respecified");
1940 YYERROR;
1941 }
1942 if ($2 < 0 || $2 > 255) {
1943 yyerror("priority out of range: max 255");
1944 YYERROR;
1945 }
1946 queue_opts.marker |= QOM_PRIORITY;
1947 queue_opts.priority = $2;
1948 }
1949 | QLIMIT NUMBER {
1950 if (queue_opts.marker & QOM_QLIMIT) {
1951 yyerror("qlimit cannot be respecified");
1952 YYERROR;
1953 }
1954 if ($2 < 0 || $2 > 65535) {
1955 yyerror("qlimit out of range: max 65535");
1956 YYERROR;
1957 }
1958 queue_opts.marker |= QOM_QLIMIT;
1959 queue_opts.qlimit = $2;
1960 }
1961 | scheduler {
1962 if (queue_opts.marker & QOM_SCHEDULER) {
1963 yyerror("scheduler cannot be respecified");
1964 YYERROR;
1965 }
1966 queue_opts.marker |= QOM_SCHEDULER;
1967 queue_opts.scheduler = $1;
1968 }
1969 | TBRSIZE NUMBER {
1970 if (queue_opts.marker & QOM_TBRSIZE) {
1971 yyerror("tbrsize cannot be respecified");
1972 YYERROR;
1973 }
1974 if ($2 < 0 || $2 > UINT_MAX) {
1975 yyerror("tbrsize too big: max %u", UINT_MAX);
1976 YYERROR;
1977 }
1978 queue_opts.marker |= QOM_TBRSIZE;
1979 queue_opts.tbrsize = $2;
1980 }
1981 ;
1982
1983 bandwidth : STRING {
1984 double bps;
1985 char *cp;
1986
1987 $$.bw_percent = 0;
1988
1989 bps = strtod($1, &cp);
1990 if (cp != NULL) {
1991 if (strlen(cp) > 1) {
1992 char *cu = cp + 1;
1993 if (!strcmp(cu, "Bit") ||
1994 !strcmp(cu, "B") ||
1995 !strcmp(cu, "bit") ||
1996 !strcmp(cu, "b")) {
1997 *cu = 0;
1998 }
1999 }
2000 if (!strcmp(cp, "b"))
2001 ; /* nothing */
2002 else if (!strcmp(cp, "K"))
2003 bps *= 1000;
2004 else if (!strcmp(cp, "M"))
2005 bps *= 1000 * 1000;
2006 else if (!strcmp(cp, "G"))
2007 bps *= 1000 * 1000 * 1000;
2008 else if (!strcmp(cp, "%")) {
2009 if (bps < 0 || bps > 100) {
2010 yyerror("bandwidth spec "
2011 "out of range");
2012 free($1);
2013 YYERROR;
2014 }
2015 $$.bw_percent = bps;
2016 bps = 0;
2017 } else {
2018 yyerror("unknown unit %s", cp);
2019 free($1);
2020 YYERROR;
2021 }
2022 }
2023 free($1);
2024 $$.bw_absolute = (u_int64_t)bps;
2025 }
2026 | NUMBER {
2027 if ($1 < 0 || $1 >= LLONG_MAX) {
2028 yyerror("bandwidth number too big");
2029 YYERROR;
2030 }
2031 $$.bw_percent = 0;
2032 $$.bw_absolute = $1;
2033 }
2034 ;
2035
2036 scheduler : CBQ {
2037 $$.qtype = ALTQT_CBQ;
2038 $$.data.cbq_opts.flags = 0;
2039 }
2040 | CBQ '(' cbqflags_list ')' {
2041 $$.qtype = ALTQT_CBQ;
2042 $$.data.cbq_opts.flags = $3;
2043 }
2044 | PRIQ {
2045 $$.qtype = ALTQT_PRIQ;
2046 $$.data.priq_opts.flags = 0;
2047 }
2048 | PRIQ '(' priqflags_list ')' {
2049 $$.qtype = ALTQT_PRIQ;
2050 $$.data.priq_opts.flags = $3;
2051 }
2052 | HFSC {
2053 $$.qtype = ALTQT_HFSC;
2054 bzero(&$$.data.hfsc_opts,
2055 sizeof(struct node_hfsc_opts));
2056 }
2057 | HFSC '(' hfsc_opts ')' {
2058 $$.qtype = ALTQT_HFSC;
2059 $$.data.hfsc_opts = $3;
2060 }
2061 | FAIRQ {
2062 $$.qtype = ALTQT_FAIRQ;
2063 bzero(&$$.data.fairq_opts,
2064 sizeof(struct node_fairq_opts));
2065 }
2066 | FAIRQ '(' fairq_opts ')' {
2067 $$.qtype = ALTQT_FAIRQ;
2068 $$.data.fairq_opts = $3;
2069 }
2070 | CODEL {
2071 $$.qtype = ALTQT_CODEL;
2072 bzero(&$$.data.codel_opts,
2073 sizeof(struct codel_opts));
2074 }
2075 | CODEL '(' codel_opts ')' {
2076 $$.qtype = ALTQT_CODEL;
2077 $$.data.codel_opts = $3;
2078 }
2079 ;
2080
2081 cbqflags_list : cbqflags_item { $$ |= $1; }
2082 | cbqflags_list comma cbqflags_item { $$ |= $3; }
2083 ;
2084
2085 cbqflags_item : STRING {
2086 if (!strcmp($1, "default"))
2087 $$ = CBQCLF_DEFCLASS;
2088 else if (!strcmp($1, "borrow"))
2089 $$ = CBQCLF_BORROW;
2090 else if (!strcmp($1, "red"))
2091 $$ = CBQCLF_RED;
2092 else if (!strcmp($1, "ecn"))
2093 $$ = CBQCLF_RED|CBQCLF_ECN;
2094 else if (!strcmp($1, "rio"))
2095 $$ = CBQCLF_RIO;
2096 else if (!strcmp($1, "codel"))
2097 $$ = CBQCLF_CODEL;
2098 else {
2099 yyerror("unknown cbq flag \"%s\"", $1);
2100 free($1);
2101 YYERROR;
2102 }
2103 free($1);
2104 }
2105 ;
2106
2107 priqflags_list : priqflags_item { $$ |= $1; }
2108 | priqflags_list comma priqflags_item { $$ |= $3; }
2109 ;
2110
2111 priqflags_item : STRING {
2112 if (!strcmp($1, "default"))
2113 $$ = PRCF_DEFAULTCLASS;
2114 else if (!strcmp($1, "red"))
2115 $$ = PRCF_RED;
2116 else if (!strcmp($1, "ecn"))
2117 $$ = PRCF_RED|PRCF_ECN;
2118 else if (!strcmp($1, "rio"))
2119 $$ = PRCF_RIO;
2120 else if (!strcmp($1, "codel"))
2121 $$ = PRCF_CODEL;
2122 else {
2123 yyerror("unknown priq flag \"%s\"", $1);
2124 free($1);
2125 YYERROR;
2126 }
2127 free($1);
2128 }
2129 ;
2130
2131 hfsc_opts : {
2132 bzero(&hfsc_opts,
2133 sizeof(struct node_hfsc_opts));
2134 }
2135 hfscopts_list {
2136 $$ = hfsc_opts;
2137 }
2138 ;
2139
2140 hfscopts_list : hfscopts_item
2141 | hfscopts_list comma hfscopts_item
2142 ;
2143
2144 hfscopts_item : LINKSHARE bandwidth {
2145 if (hfsc_opts.linkshare.used) {
2146 yyerror("linkshare already specified");
2147 YYERROR;
2148 }
2149 hfsc_opts.linkshare.m2 = $2;
2150 hfsc_opts.linkshare.used = 1;
2151 }
2152 | LINKSHARE '(' bandwidth comma NUMBER comma bandwidth ')'
2153 {
2154 if ($5 < 0 || $5 > INT_MAX) {
2155 yyerror("timing in curve out of range");
2156 YYERROR;
2157 }
2158 if (hfsc_opts.linkshare.used) {
2159 yyerror("linkshare already specified");
2160 YYERROR;
2161 }
2162 hfsc_opts.linkshare.m1 = $3;
2163 hfsc_opts.linkshare.d = $5;
2164 hfsc_opts.linkshare.m2 = $7;
2165 hfsc_opts.linkshare.used = 1;
2166 }
2167 | REALTIME bandwidth {
2168 if (hfsc_opts.realtime.used) {
2169 yyerror("realtime already specified");
2170 YYERROR;
2171 }
2172 hfsc_opts.realtime.m2 = $2;
2173 hfsc_opts.realtime.used = 1;
2174 }
2175 | REALTIME '(' bandwidth comma NUMBER comma bandwidth ')'
2176 {
2177 if ($5 < 0 || $5 > INT_MAX) {
2178 yyerror("timing in curve out of range");
2179 YYERROR;
2180 }
2181 if (hfsc_opts.realtime.used) {
2182 yyerror("realtime already specified");
2183 YYERROR;
2184 }
2185 hfsc_opts.realtime.m1 = $3;
2186 hfsc_opts.realtime.d = $5;
2187 hfsc_opts.realtime.m2 = $7;
2188 hfsc_opts.realtime.used = 1;
2189 }
2190 | UPPERLIMIT bandwidth {
2191 if (hfsc_opts.upperlimit.used) {
2192 yyerror("upperlimit already specified");
2193 YYERROR;
2194 }
2195 hfsc_opts.upperlimit.m2 = $2;
2196 hfsc_opts.upperlimit.used = 1;
2197 }
2198 | UPPERLIMIT '(' bandwidth comma NUMBER comma bandwidth ')'
2199 {
2200 if ($5 < 0 || $5 > INT_MAX) {
2201 yyerror("timing in curve out of range");
2202 YYERROR;
2203 }
2204 if (hfsc_opts.upperlimit.used) {
2205 yyerror("upperlimit already specified");
2206 YYERROR;
2207 }
2208 hfsc_opts.upperlimit.m1 = $3;
2209 hfsc_opts.upperlimit.d = $5;
2210 hfsc_opts.upperlimit.m2 = $7;
2211 hfsc_opts.upperlimit.used = 1;
2212 }
2213 | STRING {
2214 if (!strcmp($1, "default"))
2215 hfsc_opts.flags |= HFCF_DEFAULTCLASS;
2216 else if (!strcmp($1, "red"))
2217 hfsc_opts.flags |= HFCF_RED;
2218 else if (!strcmp($1, "ecn"))
2219 hfsc_opts.flags |= HFCF_RED|HFCF_ECN;
2220 else if (!strcmp($1, "rio"))
2221 hfsc_opts.flags |= HFCF_RIO;
2222 else if (!strcmp($1, "codel"))
2223 hfsc_opts.flags |= HFCF_CODEL;
2224 else {
2225 yyerror("unknown hfsc flag \"%s\"", $1);
2226 free($1);
2227 YYERROR;
2228 }
2229 free($1);
2230 }
2231 ;
2232
2233 fairq_opts : {
2234 bzero(&fairq_opts,
2235 sizeof(struct node_fairq_opts));
2236 }
2237 fairqopts_list {
2238 $$ = fairq_opts;
2239 }
2240 ;
2241
2242 fairqopts_list : fairqopts_item
2243 | fairqopts_list comma fairqopts_item
2244 ;
2245
2246 fairqopts_item : LINKSHARE bandwidth {
2247 if (fairq_opts.linkshare.used) {
2248 yyerror("linkshare already specified");
2249 YYERROR;
2250 }
2251 fairq_opts.linkshare.m2 = $2;
2252 fairq_opts.linkshare.used = 1;
2253 }
2254 | LINKSHARE '(' bandwidth number bandwidth ')' {
2255 if (fairq_opts.linkshare.used) {
2256 yyerror("linkshare already specified");
2257 YYERROR;
2258 }
2259 fairq_opts.linkshare.m1 = $3;
2260 fairq_opts.linkshare.d = $4;
2261 fairq_opts.linkshare.m2 = $5;
2262 fairq_opts.linkshare.used = 1;
2263 }
2264 | HOGS bandwidth {
2265 fairq_opts.hogs_bw = $2;
2266 }
2267 | BUCKETS number {
2268 fairq_opts.nbuckets = $2;
2269 }
2270 | STRING {
2271 if (!strcmp($1, "default"))
2272 fairq_opts.flags |= FARF_DEFAULTCLASS;
2273 else if (!strcmp($1, "red"))
2274 fairq_opts.flags |= FARF_RED;
2275 else if (!strcmp($1, "ecn"))
2276 fairq_opts.flags |= FARF_RED|FARF_ECN;
2277 else if (!strcmp($1, "rio"))
2278 fairq_opts.flags |= FARF_RIO;
2279 else if (!strcmp($1, "codel"))
2280 fairq_opts.flags |= FARF_CODEL;
2281 else {
2282 yyerror("unknown fairq flag \"%s\"", $1);
2283 free($1);
2284 YYERROR;
2285 }
2286 free($1);
2287 }
2288 ;
2289
2290 codel_opts : {
2291 bzero(&codel_opts,
2292 sizeof(struct codel_opts));
2293 }
2294 codelopts_list {
2295 $$ = codel_opts;
2296 }
2297 ;
2298
2299 codelopts_list : codelopts_item
2300 | codelopts_list comma codelopts_item
2301 ;
2302
2303 codelopts_item : INTERVAL number {
2304 if (codel_opts.interval) {
2305 yyerror("interval already specified");
2306 YYERROR;
2307 }
2308 codel_opts.interval = $2;
2309 }
2310 | TARGET number {
2311 if (codel_opts.target) {
2312 yyerror("target already specified");
2313 YYERROR;
2314 }
2315 codel_opts.target = $2;
2316 }
2317 | STRING {
2318 if (!strcmp($1, "ecn"))
2319 codel_opts.ecn = 1;
2320 else {
2321 yyerror("unknown codel option \"%s\"", $1);
2322 free($1);
2323 YYERROR;
2324 }
2325 free($1);
2326 }
2327 ;
2328
2329 qassign : /* empty */ { $$ = NULL; }
2330 | qassign_item { $$ = $1; }
2331 | '{' optnl qassign_list '}' { $$ = $3; }
2332 ;
2333
2334 qassign_list : qassign_item optnl { $$ = $1; }
2335 | qassign_list comma qassign_item optnl {
2336 $1->tail->next = $3;
2337 $1->tail = $3;
2338 $$ = $1;
2339 }
2340 ;
2341
2342 qassign_item : STRING {
2343 $$ = calloc(1, sizeof(struct node_queue));
2344 if ($$ == NULL)
2345 err(1, "qassign_item: calloc");
2346 if (strlcpy($$->queue, $1, sizeof($$->queue)) >=
2347 sizeof($$->queue)) {
2348 yyerror("queue name '%s' too long (max "
2349 "%d chars)", $1, sizeof($$->queue)-1);
2350 free($1);
2351 free($$);
2352 YYERROR;
2353 }
2354 free($1);
2355 $$->next = NULL;
2356 $$->tail = $$;
2357 }
2358 ;
2359
2360 pfrule : action dir logquick interface route af proto fromto
2361 filter_opts
2362 {
2363 struct pfctl_rule r;
2364 struct node_state_opt *o;
2365 struct node_proto *proto;
2366 int srctrack = 0;
2367 int statelock = 0;
2368 int adaptive = 0;
2369 int defaults = 0;
2370
2371 if (check_rulestate(PFCTL_STATE_FILTER))
2372 YYERROR;
2373
2374 memset(&r, 0, sizeof(r));
2375
2376 r.action = $1.b1;
2377 switch ($1.b2) {
2378 case PFRULE_RETURNRST:
2379 r.rule_flag |= PFRULE_RETURNRST;
2380 r.return_ttl = $1.w;
2381 break;
2382 case PFRULE_RETURNICMP:
2383 r.rule_flag |= PFRULE_RETURNICMP;
2384 r.return_icmp = $1.w;
2385 r.return_icmp6 = $1.w2;
2386 break;
2387 case PFRULE_RETURN:
2388 r.rule_flag |= PFRULE_RETURN;
2389 r.return_icmp = $1.w;
2390 r.return_icmp6 = $1.w2;
2391 break;
2392 }
2393 r.direction = $2;
2394 r.log = $3.log;
2395 r.logif = $3.logif;
2396 r.quick = $3.quick;
2397 r.prob = $9.prob;
2398 r.rtableid = $9.rtableid;
2399
2400 if ($9.nodf)
2401 r.scrub_flags |= PFSTATE_NODF;
2402 if ($9.randomid)
2403 r.scrub_flags |= PFSTATE_RANDOMID;
2404 if ($9.minttl)
2405 r.min_ttl = $9.minttl;
2406 if ($9.max_mss)
2407 r.max_mss = $9.max_mss;
2408 if ($9.marker & FOM_SETTOS) {
2409 r.scrub_flags |= PFSTATE_SETTOS;
2410 r.set_tos = $9.settos;
2411 }
2412 if ($9.marker & FOM_SCRUB_TCP)
2413 r.scrub_flags |= PFSTATE_SCRUB_TCP;
2414
2415 if ($9.marker & FOM_PRIO) {
2416 if ($9.prio == 0)
2417 r.prio = PF_PRIO_ZERO;
2418 else
2419 r.prio = $9.prio;
2420 }
2421 if ($9.marker & FOM_SETPRIO) {
2422 r.set_prio[0] = $9.set_prio[0];
2423 r.set_prio[1] = $9.set_prio[1];
2424 r.scrub_flags |= PFSTATE_SETPRIO;
2425 }
2426
2427 if ($9.marker & FOM_AFTO)
2428 r.rule_flag |= PFRULE_AFTO;
2429
2430 r.af = $6;
2431 if ($9.tag)
2432 if (strlcpy(r.tagname, $9.tag,
2433 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
2434 yyerror("tag too long, max %u chars",
2435 PF_TAG_NAME_SIZE - 1);
2436 YYERROR;
2437 }
2438 if ($9.match_tag)
2439 if (strlcpy(r.match_tagname, $9.match_tag,
2440 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
2441 yyerror("tag too long, max %u chars",
2442 PF_TAG_NAME_SIZE - 1);
2443 YYERROR;
2444 }
2445 r.match_tag_not = $9.match_tag_not;
2446 if (rule_label(&r, $9.label))
2447 YYERROR;
2448 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++)
2449 free($9.label[i]);
2450 r.ridentifier = $9.ridentifier;
2451 r.flags = $9.flags.b1;
2452 r.flagset = $9.flags.b2;
2453 if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
2454 yyerror("flags always false");
2455 YYERROR;
2456 }
2457 if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
2458 for (proto = $7; proto != NULL &&
2459 proto->proto != IPPROTO_TCP;
2460 proto = proto->next)
2461 ; /* nothing */
2462 if (proto == NULL && $7 != NULL) {
2463 if ($9.flags.b1 || $9.flags.b2)
2464 yyerror(
2465 "flags only apply to tcp");
2466 if ($8.src_os)
2467 yyerror(
2468 "OS fingerprinting only "
2469 "apply to tcp");
2470 YYERROR;
2471 }
2472 #if 0
2473 if (($9.flags.b1 & parse_flags("S")) == 0 &&
2474 $8.src_os) {
2475 yyerror("OS fingerprinting requires "
2476 "the SYN TCP flag (flags S/SA)");
2477 YYERROR;
2478 }
2479 #endif
2480 }
2481
2482 r.tos = $9.tos;
2483 r.keep_state = $9.keep.action;
2484 o = $9.keep.options;
2485
2486 /* 'keep state' by default on pass rules. */
2487 if (!r.keep_state && !r.action &&
2488 !($9.marker & FOM_KEEP)) {
2489 r.keep_state = PF_STATE_NORMAL;
2490 o = keep_state_defaults;
2491 defaults = 1;
2492 }
2493
2494 while (o) {
2495 struct node_state_opt *p = o;
2496
2497 switch (o->type) {
2498 case PF_STATE_OPT_MAX:
2499 if (r.max_states) {
2500 yyerror("state option 'max' "
2501 "multiple definitions");
2502 YYERROR;
2503 }
2504 r.max_states = o->data.max_states;
2505 break;
2506 case PF_STATE_OPT_NOSYNC:
2507 if (r.rule_flag & PFRULE_NOSYNC) {
2508 yyerror("state option 'sync' "
2509 "multiple definitions");
2510 YYERROR;
2511 }
2512 r.rule_flag |= PFRULE_NOSYNC;
2513 break;
2514 case PF_STATE_OPT_SRCTRACK:
2515 if (srctrack) {
2516 yyerror("state option "
2517 "'source-track' "
2518 "multiple definitions");
2519 YYERROR;
2520 }
2521 srctrack = o->data.src_track;
2522 r.rule_flag |= PFRULE_SRCTRACK;
2523 break;
2524 case PF_STATE_OPT_MAX_SRC_STATES:
2525 if (r.max_src_states) {
2526 yyerror("state option "
2527 "'max-src-states' "
2528 "multiple definitions");
2529 YYERROR;
2530 }
2531 if (o->data.max_src_states == 0) {
2532 yyerror("'max-src-states' must "
2533 "be > 0");
2534 YYERROR;
2535 }
2536 r.max_src_states =
2537 o->data.max_src_states;
2538 r.rule_flag |= PFRULE_SRCTRACK;
2539 break;
2540 case PF_STATE_OPT_OVERLOAD:
2541 if (r.overload_tblname[0]) {
2542 yyerror("multiple 'overload' "
2543 "table definitions");
2544 YYERROR;
2545 }
2546 if (strlcpy(r.overload_tblname,
2547 o->data.overload.tblname,
2548 PF_TABLE_NAME_SIZE) >=
2549 PF_TABLE_NAME_SIZE) {
2550 yyerror("state option: "
2551 "strlcpy");
2552 YYERROR;
2553 }
2554 r.flush = o->data.overload.flush;
2555 break;
2556 case PF_STATE_OPT_MAX_SRC_CONN:
2557 if (r.max_src_conn) {
2558 yyerror("state option "
2559 "'max-src-conn' "
2560 "multiple definitions");
2561 YYERROR;
2562 }
2563 if (o->data.max_src_conn == 0) {
2564 yyerror("'max-src-conn' "
2565 "must be > 0");
2566 YYERROR;
2567 }
2568 r.max_src_conn =
2569 o->data.max_src_conn;
2570 r.rule_flag |= PFRULE_SRCTRACK |
2571 PFRULE_RULESRCTRACK;
2572 break;
2573 case PF_STATE_OPT_MAX_SRC_CONN_RATE:
2574 if (r.max_src_conn_rate.limit) {
2575 yyerror("state option "
2576 "'max-src-conn-rate' "
2577 "multiple definitions");
2578 YYERROR;
2579 }
2580 if (!o->data.max_src_conn_rate.limit ||
2581 !o->data.max_src_conn_rate.seconds) {
2582 yyerror("'max-src-conn-rate' "
2583 "values must be > 0");
2584 YYERROR;
2585 }
2586 if (o->data.max_src_conn_rate.limit >
2587 PF_THRESHOLD_MAX) {
2588 yyerror("'max-src-conn-rate' "
2589 "maximum rate must be < %u",
2590 PF_THRESHOLD_MAX);
2591 YYERROR;
2592 }
2593 r.max_src_conn_rate.limit =
2594 o->data.max_src_conn_rate.limit;
2595 r.max_src_conn_rate.seconds =
2596 o->data.max_src_conn_rate.seconds;
2597 r.rule_flag |= PFRULE_SRCTRACK |
2598 PFRULE_RULESRCTRACK;
2599 break;
2600 case PF_STATE_OPT_MAX_SRC_NODES:
2601 if (r.max_src_nodes) {
2602 yyerror("state option "
2603 "'max-src-nodes' "
2604 "multiple definitions");
2605 YYERROR;
2606 }
2607 if (o->data.max_src_nodes == 0) {
2608 yyerror("'max-src-nodes' must "
2609 "be > 0");
2610 YYERROR;
2611 }
2612 r.max_src_nodes =
2613 o->data.max_src_nodes;
2614 r.rule_flag |= PFRULE_SRCTRACK |
2615 PFRULE_RULESRCTRACK;
2616 break;
2617 case PF_STATE_OPT_STATELOCK:
2618 if (statelock) {
2619 yyerror("state locking option: "
2620 "multiple definitions");
2621 YYERROR;
2622 }
2623 statelock = 1;
2624 r.rule_flag |= o->data.statelock;
2625 break;
2626 case PF_STATE_OPT_SLOPPY:
2627 if (r.rule_flag & PFRULE_STATESLOPPY) {
2628 yyerror("state sloppy option: "
2629 "multiple definitions");
2630 YYERROR;
2631 }
2632 r.rule_flag |= PFRULE_STATESLOPPY;
2633 break;
2634 case PF_STATE_OPT_PFLOW:
2635 if (r.rule_flag & PFRULE_PFLOW) {
2636 yyerror("state pflow option: "
2637 "multiple definitions");
2638 YYERROR;
2639 }
2640 r.rule_flag |= PFRULE_PFLOW;
2641 break;
2642 case PF_STATE_OPT_ALLOW_RELATED:
2643 if (r.rule_flag & PFRULE_ALLOW_RELATED) {
2644 yyerror("state allow-related option: "
2645 "multiple definitions");
2646 YYERROR;
2647 }
2648 r.rule_flag |= PFRULE_ALLOW_RELATED;
2649 break;
2650 case PF_STATE_OPT_TIMEOUT:
2651 if (o->data.timeout.number ==
2652 PFTM_ADAPTIVE_START ||
2653 o->data.timeout.number ==
2654 PFTM_ADAPTIVE_END)
2655 adaptive = 1;
2656 if (r.timeout[o->data.timeout.number]) {
2657 yyerror("state timeout %s "
2658 "multiple definitions",
2659 pf_timeouts[o->data.
2660 timeout.number].name);
2661 YYERROR;
2662 }
2663 r.timeout[o->data.timeout.number] =
2664 o->data.timeout.seconds;
2665 }
2666 o = o->next;
2667 if (!defaults)
2668 free(p);
2669 }
2670
2671 /* 'flags S/SA' by default on stateful rules */
2672 if (!r.action && !r.flags && !r.flagset &&
2673 !$9.fragment && !($9.marker & FOM_FLAGS) &&
2674 r.keep_state) {
2675 r.flags = parse_flags("S");
2676 r.flagset = parse_flags("SA");
2677 }
2678 if (!adaptive && r.max_states) {
2679 r.timeout[PFTM_ADAPTIVE_START] =
2680 (r.max_states / 10) * 6;
2681 r.timeout[PFTM_ADAPTIVE_END] =
2682 (r.max_states / 10) * 12;
2683 }
2684 if (r.rule_flag & PFRULE_SRCTRACK) {
2685 if (srctrack == PF_SRCTRACK_GLOBAL &&
2686 r.max_src_nodes) {
2687 yyerror("'max-src-nodes' is "
2688 "incompatible with "
2689 "'source-track global'");
2690 YYERROR;
2691 }
2692 if (srctrack == PF_SRCTRACK_GLOBAL &&
2693 r.max_src_conn) {
2694 yyerror("'max-src-conn' is "
2695 "incompatible with "
2696 "'source-track global'");
2697 YYERROR;
2698 }
2699 if (srctrack == PF_SRCTRACK_GLOBAL &&
2700 r.max_src_conn_rate.seconds) {
2701 yyerror("'max-src-conn-rate' is "
2702 "incompatible with "
2703 "'source-track global'");
2704 YYERROR;
2705 }
2706 if (r.timeout[PFTM_SRC_NODE] <
2707 r.max_src_conn_rate.seconds)
2708 r.timeout[PFTM_SRC_NODE] =
2709 r.max_src_conn_rate.seconds;
2710 r.rule_flag |= PFRULE_SRCTRACK;
2711 if (srctrack == PF_SRCTRACK_RULE)
2712 r.rule_flag |= PFRULE_RULESRCTRACK;
2713 }
2714 if (r.keep_state && !statelock)
2715 r.rule_flag |= default_statelock;
2716
2717 if ($9.fragment)
2718 r.rule_flag |= PFRULE_FRAGMENT;
2719 r.allow_opts = $9.allowopts;
2720
2721 decide_address_family($8.src.host, &r.af);
2722 decide_address_family($8.dst.host, &r.af);
2723
2724 if ($5.rt) {
2725 if (!r.direction) {
2726 yyerror("direction must be explicit "
2727 "with rules that specify routing");
2728 YYERROR;
2729 }
2730 r.rt = $5.rt;
2731 r.route.opts = $5.pool_opts;
2732 if ($5.key != NULL)
2733 memcpy(&r.route.key, $5.key,
2734 sizeof(struct pf_poolhashkey));
2735 }
2736 if (r.rt) {
2737 decide_address_family($5.host, &r.af);
2738 if (!(r.rule_flag & PFRULE_AFTO))
2739 remove_invalid_hosts(&$5.host, &r.af);
2740 if ($5.host == NULL) {
2741 yyerror("no routing address with "
2742 "matching address family found.");
2743 YYERROR;
2744 }
2745 if ((r.route.opts & PF_POOL_TYPEMASK) ==
2746 PF_POOL_NONE && ($5.host->next != NULL ||
2747 $5.host->addr.type == PF_ADDR_TABLE ||
2748 DYNIF_MULTIADDR($5.host->addr)))
2749 r.route.opts |= PF_POOL_ROUNDROBIN;
2750 if ((r.route.opts & PF_POOL_TYPEMASK) !=
2751 PF_POOL_ROUNDROBIN &&
2752 disallow_table($5.host, "tables are only "
2753 "supported in round-robin routing pools"))
2754 YYERROR;
2755 if ((r.route.opts & PF_POOL_TYPEMASK) !=
2756 PF_POOL_ROUNDROBIN &&
2757 disallow_alias($5.host, "interface (%s) "
2758 "is only supported in round-robin "
2759 "routing pools"))
2760 YYERROR;
2761 if ($5.host->next != NULL) {
2762 if ((r.route.opts & PF_POOL_TYPEMASK) !=
2763 PF_POOL_ROUNDROBIN) {
2764 yyerror("r.route.opts must "
2765 "be PF_POOL_ROUNDROBIN");
2766 YYERROR;
2767 }
2768 }
2769 }
2770 if ($9.queues.qname != NULL) {
2771 if (strlcpy(r.qname, $9.queues.qname,
2772 sizeof(r.qname)) >= sizeof(r.qname)) {
2773 yyerror("rule qname too long (max "
2774 "%d chars)", sizeof(r.qname)-1);
2775 YYERROR;
2776 }
2777 free($9.queues.qname);
2778 }
2779 if ($9.queues.pqname != NULL) {
2780 if (strlcpy(r.pqname, $9.queues.pqname,
2781 sizeof(r.pqname)) >= sizeof(r.pqname)) {
2782 yyerror("rule pqname too long (max "
2783 "%d chars)", sizeof(r.pqname)-1);
2784 YYERROR;
2785 }
2786 free($9.queues.pqname);
2787 }
2788 #ifdef __FreeBSD__
2789 r.divert.port = $9.divert.port;
2790 #else
2791 if ((r.divert.port = $9.divert.port)) {
2792 if (r.direction == PF_OUT) {
2793 if ($9.divert.addr) {
2794 yyerror("address specified "
2795 "for outgoing divert");
2796 YYERROR;
2797 }
2798 bzero(&r.divert.addr,
2799 sizeof(r.divert.addr));
2800 } else {
2801 if (!$9.divert.addr) {
2802 yyerror("no address specified "
2803 "for incoming divert");
2804 YYERROR;
2805 }
2806 if ($9.divert.addr->af != r.af) {
2807 yyerror("address family "
2808 "mismatch for divert");
2809 YYERROR;
2810 }
2811 r.divert.addr =
2812 $9.divert.addr->addr.v.a.addr;
2813 }
2814 }
2815 #endif
2816
2817 if ($9.dnpipe || $9.dnrpipe) {
2818 r.dnpipe = $9.dnpipe;
2819 r.dnrpipe = $9.dnrpipe;
2820 if ($9.free_flags & PFRULE_DN_IS_PIPE)
2821 r.free_flags |= PFRULE_DN_IS_PIPE;
2822 else
2823 r.free_flags |= PFRULE_DN_IS_QUEUE;
2824 }
2825
2826 if ($9.marker & FOM_AFTO) {
2827 r.naf = $9.nat.af;
2828
2829 r.nat.opts = $9.nat.pool_opts.type;
2830 r.nat.opts |= $9.nat.pool_opts.opts;
2831
2832 if ((r.nat.opts & PF_POOL_TYPEMASK) !=
2833 PF_POOL_ROUNDROBIN &&
2834 disallow_table($9.nat.rdr->host, "tables are only "
2835 "supported in round-robin pools"))
2836 YYERROR;
2837 }
2838
2839 expand_rule(&r, $4, &$9.nat, &$9.rdr, &$9.rroute,
2840 NULL, $9.nat.rdr ? $9.nat.rdr->host : NULL, $5.host,
2841 $7, $8.src_os, $8.src.host, $8.src.port, $8.dst.host,
2842 $8.dst.port, $9.uid, $9.gid, $9.rcv, $9.icmpspec, "");
2843 }
2844 ;
2845
2846 filter_opts : {
2847 bzero(&filter_opts, sizeof filter_opts);
2848 filter_opts.rtableid = -1;
2849 }
2850 filter_opts_l
2851 { $$ = filter_opts; }
2852 | /* empty */ {
2853 bzero(&filter_opts, sizeof filter_opts);
2854 filter_opts.rtableid = -1;
2855 $$ = filter_opts;
2856 }
2857 ;
2858
2859 filter_opts_l : filter_opts_l filter_opt
2860 | filter_opt
2861 ;
2862
2863 filter_opt : USER uids {
2864 if (filter_opts.uid)
2865 $2->tail->next = filter_opts.uid;
2866 filter_opts.uid = $2;
2867 }
2868 | GROUP gids {
2869 if (filter_opts.gid)
2870 $2->tail->next = filter_opts.gid;
2871 filter_opts.gid = $2;
2872 }
2873 | flags {
2874 if (filter_opts.marker & FOM_FLAGS) {
2875 yyerror("flags cannot be redefined");
2876 YYERROR;
2877 }
2878 filter_opts.marker |= FOM_FLAGS;
2879 filter_opts.flags.b1 |= $1.b1;
2880 filter_opts.flags.b2 |= $1.b2;
2881 filter_opts.flags.w |= $1.w;
2882 filter_opts.flags.w2 |= $1.w2;
2883 }
2884 | icmpspec {
2885 if (filter_opts.marker & FOM_ICMP) {
2886 yyerror("icmp-type cannot be redefined");
2887 YYERROR;
2888 }
2889 filter_opts.marker |= FOM_ICMP;
2890 filter_opts.icmpspec = $1;
2891 }
2892 | PRIO NUMBER {
2893 if (filter_opts.marker & FOM_PRIO) {
2894 yyerror("prio cannot be redefined");
2895 YYERROR;
2896 }
2897 if ($2 < 0 || $2 > PF_PRIO_MAX) {
2898 yyerror("prio must be 0 - %u", PF_PRIO_MAX);
2899 YYERROR;
2900 }
2901 filter_opts.marker |= FOM_PRIO;
2902 filter_opts.prio = $2;
2903 }
2904 | TOS tos {
2905 if (filter_opts.marker & FOM_TOS) {
2906 yyerror("tos cannot be redefined");
2907 YYERROR;
2908 }
2909 filter_opts.marker |= FOM_TOS;
2910 filter_opts.tos = $2;
2911 }
2912 | keep {
2913 if (filter_opts.marker & FOM_KEEP) {
2914 yyerror("modulate or keep cannot be redefined");
2915 YYERROR;
2916 }
2917 filter_opts.marker |= FOM_KEEP;
2918 filter_opts.keep.action = $1.action;
2919 filter_opts.keep.options = $1.options;
2920 }
2921 | RIDENTIFIER number {
2922 filter_opts.ridentifier = $2;
2923 }
2924 | FRAGMENT {
2925 filter_opts.fragment = 1;
2926 }
2927 | ALLOWOPTS {
2928 filter_opts.allowopts = 1;
2929 }
2930 | label {
2931 if (filter_opts.labelcount >= PF_RULE_MAX_LABEL_COUNT) {
2932 yyerror("label can only be used %d times", PF_RULE_MAX_LABEL_COUNT);
2933 YYERROR;
2934 }
2935 filter_opts.label[filter_opts.labelcount++] = $1;
2936 }
2937 | qname {
2938 if (filter_opts.queues.qname) {
2939 yyerror("queue cannot be redefined");
2940 YYERROR;
2941 }
2942 filter_opts.queues = $1;
2943 }
2944 | DNPIPE number {
2945 filter_opts.dnpipe = $2;
2946 filter_opts.free_flags |= PFRULE_DN_IS_PIPE;
2947 }
2948 | DNPIPE '(' number ')' {
2949 filter_opts.dnpipe = $3;
2950 filter_opts.free_flags |= PFRULE_DN_IS_PIPE;
2951 }
2952 | DNPIPE '(' number comma number ')' {
2953 filter_opts.dnrpipe = $5;
2954 filter_opts.dnpipe = $3;
2955 filter_opts.free_flags |= PFRULE_DN_IS_PIPE;
2956 }
2957 | DNQUEUE number {
2958 filter_opts.dnpipe = $2;
2959 filter_opts.free_flags |= PFRULE_DN_IS_QUEUE;
2960 }
2961 | DNQUEUE '(' number comma number ')' {
2962 filter_opts.dnrpipe = $5;
2963 filter_opts.dnpipe = $3;
2964 filter_opts.free_flags |= PFRULE_DN_IS_QUEUE;
2965 }
2966 | DNQUEUE '(' number ')' {
2967 filter_opts.dnpipe = $3;
2968 filter_opts.free_flags |= PFRULE_DN_IS_QUEUE;
2969 }
2970 | TAG string {
2971 filter_opts.tag = $2;
2972 }
2973 | not TAGGED string {
2974 filter_opts.match_tag = $3;
2975 filter_opts.match_tag_not = $1;
2976 }
2977 | RECEIVEDON if_item {
2978 if (filter_opts.rcv) {
2979 yyerror("cannot respecify received-on");
2980 YYERROR;
2981 }
2982 filter_opts.rcv = $2;
2983 }
2984 | PROBABILITY probability {
2985 double p;
2986
2987 p = floor($2 * UINT_MAX + 0.5);
2988 if (p < 0.0 || p > UINT_MAX) {
2989 yyerror("invalid probability: %lf", p);
2990 YYERROR;
2991 }
2992 filter_opts.prob = (u_int32_t)p;
2993 if (filter_opts.prob == 0)
2994 filter_opts.prob = 1;
2995 }
2996 | RTABLE NUMBER {
2997 if ($2 < 0 || $2 > rt_tableid_max()) {
2998 yyerror("invalid rtable id");
2999 YYERROR;
3000 }
3001 filter_opts.rtableid = $2;
3002 }
3003 | DIVERTTO portplain {
3004 #ifdef __FreeBSD__
3005 filter_opts.divert.port = $2.a;
3006 if (!filter_opts.divert.port) {
3007 yyerror("invalid divert port: %u", ntohs($2.a));
3008 YYERROR;
3009 }
3010 #endif
3011 }
3012 | DIVERTTO STRING PORT portplain {
3013 #ifndef __FreeBSD__
3014 if ((filter_opts.divert.addr = host($2)) == NULL) {
3015 yyerror("could not parse divert address: %s",
3016 $2);
3017 free($2);
3018 YYERROR;
3019 }
3020 #else
3021 if ($2)
3022 #endif
3023 free($2);
3024 filter_opts.divert.port = $4.a;
3025 if (!filter_opts.divert.port) {
3026 yyerror("invalid divert port: %u", ntohs($4.a));
3027 YYERROR;
3028 }
3029 }
3030 | DIVERTREPLY {
3031 #ifdef __FreeBSD__
3032 yyerror("divert-reply has no meaning in FreeBSD pf(4)");
3033 YYERROR;
3034 #else
3035 filter_opts.divert.port = 1; /* some random value */
3036 #endif
3037 }
3038 | SCRUB '(' scrub_opts ')' {
3039 filter_opts.nodf = $3.nodf;
3040 filter_opts.minttl = $3.minttl;
3041 if ($3.marker & FOM_SETTOS) {
3042 /* Old style rules are "scrub set-tos 0x42"
3043 * New style are "set tos 0x42 scrub (...)"
3044 * What is in "scrub(...)"" is unfortunately the
3045 * original scrub syntax so it would overwrite
3046 * "set tos" of a pass/match rule.
3047 */
3048 filter_opts.settos = $3.settos;
3049 }
3050 filter_opts.randomid = $3.randomid;
3051 filter_opts.max_mss = $3.maxmss;
3052 if ($3.reassemble_tcp)
3053 filter_opts.marker |= FOM_SCRUB_TCP;
3054 filter_opts.marker |= $3.marker;
3055 }
3056 | AFTO af FROM redirspec pool_opts {
3057 if (filter_opts.nat.rdr) {
3058 yyerror("cannot respecify af-to");
3059 YYERROR;
3060 }
3061 if ($2 == 0) {
3062 yyerror("no address family specified");
3063 YYERROR;
3064 }
3065 if ($4->af && $4->af != $2) {
3066 yyerror("af-to addresses must be in the "
3067 "target address family");
3068 YYERROR;
3069 }
3070 filter_opts.nat.af = $2;
3071 filter_opts.nat.rdr = calloc(1, sizeof(struct redirection));
3072 if (filter_opts.nat.rdr == NULL)
3073 err(1, "af-to: calloc");
3074 filter_opts.nat.rdr->host = $4;
3075 memcpy(&filter_opts.nat.pool_opts, &$5,
3076 sizeof(filter_opts.nat.pool_opts));
3077 filter_opts.rdr.rdr =
3078 calloc(1, sizeof(struct redirection));
3079 bzero(&filter_opts.rdr.pool_opts,
3080 sizeof(filter_opts.rdr.pool_opts));
3081 filter_opts.marker |= FOM_AFTO;
3082 }
3083 | AFTO af FROM redirspec pool_opts TO redirspec pool_opts {
3084 if (filter_opts.nat.rdr) {
3085 yyerror("cannot respecify af-to");
3086 YYERROR;
3087 }
3088 if ($2 == 0) {
3089 yyerror("no address family specified");
3090 YYERROR;
3091 }
3092 if (($4->af && $4->af != $2) ||
3093 ($7->af && $7->af != $2)) {
3094 yyerror("af-to addresses must be in the "
3095 "target address family");
3096 YYERROR;
3097 }
3098 filter_opts.nat.af = $2;
3099 filter_opts.nat.rdr = calloc(1, sizeof(struct redirection));
3100 if (filter_opts.nat.rdr == NULL)
3101 err(1, "af-to: calloc");
3102 filter_opts.nat.rdr->host = $4;
3103 memcpy(&filter_opts.nat.pool_opts, &$5,
3104 sizeof(filter_opts.nat.pool_opts));
3105 filter_opts.rdr.af = $2;
3106 filter_opts.rdr.rdr = calloc(1, sizeof(struct redirection));
3107 if (filter_opts.rdr.rdr == NULL)
3108 err(1, "af-to: calloc");
3109 filter_opts.rdr.rdr->host = $7;
3110 memcpy(&filter_opts.nat.pool_opts, &$8,
3111 sizeof(filter_opts.nat.pool_opts));
3112 filter_opts.marker |= FOM_AFTO;
3113 }
3114 | filter_sets
3115 ;
3116
3117 filter_sets : SET '(' filter_sets_l ')' { $$ = filter_opts; }
3118 | SET filter_set { $$ = filter_opts; }
3119 ;
3120
3121 filter_sets_l : filter_sets_l comma filter_set
3122 | filter_set
3123 ;
3124
3125 filter_set : prio {
3126 if (filter_opts.marker & FOM_SETPRIO) {
3127 yyerror("prio cannot be redefined");
3128 YYERROR;
3129 }
3130 filter_opts.marker |= FOM_SETPRIO;
3131 filter_opts.set_prio[0] = $1.b1;
3132 filter_opts.set_prio[1] = $1.b2;
3133 }
3134 | TOS tos {
3135 if (filter_opts.marker & FOM_SETTOS) {
3136 yyerror("tos cannot be respecified");
3137 YYERROR;
3138 }
3139 filter_opts.marker |= FOM_SETTOS;
3140 filter_opts.settos = $2;
3141 }
3142 prio : PRIO NUMBER {
3143 if ($2 < 0 || $2 > PF_PRIO_MAX) {
3144 yyerror("prio must be 0 - %u", PF_PRIO_MAX);
3145 YYERROR;
3146 }
3147 $$.b1 = $$.b2 = $2;
3148 }
3149 | PRIO '(' NUMBER comma NUMBER ')' {
3150 if ($3 < 0 || $3 > PF_PRIO_MAX ||
3151 $5 < 0 || $5 > PF_PRIO_MAX) {
3152 yyerror("prio must be 0 - %u", PF_PRIO_MAX);
3153 YYERROR;
3154 }
3155 $$.b1 = $3;
3156 $$.b2 = $5;
3157 }
3158 ;
3159
3160 probability : STRING {
3161 char *e;
3162 double p = strtod($1, &e);
3163
3164 if (*e == '%') {
3165 p *= 0.01;
3166 e++;
3167 }
3168 if (*e) {
3169 yyerror("invalid probability: %s", $1);
3170 free($1);
3171 YYERROR;
3172 }
3173 free($1);
3174 $$ = p;
3175 }
3176 | NUMBER {
3177 $$ = (double)$1;
3178 }
3179 ;
3180
3181
3182 action : PASS {
3183 $$.b1 = PF_PASS;
3184 $$.b2 = failpolicy;
3185 $$.w = returnicmpdefault;
3186 $$.w2 = returnicmp6default;
3187 }
3188 | MATCH { $$.b1 = PF_MATCH; $$.b2 = $$.w = 0; }
3189 | BLOCK blockspec { $$ = $2; $$.b1 = PF_DROP; }
3190 ;
3191
3192 blockspec : /* empty */ {
3193 $$.b2 = blockpolicy;
3194 $$.w = returnicmpdefault;
3195 $$.w2 = returnicmp6default;
3196 }
3197 | DROP {
3198 $$.b2 = PFRULE_DROP;
3199 $$.w = 0;
3200 $$.w2 = 0;
3201 }
3202 | RETURNRST {
3203 $$.b2 = PFRULE_RETURNRST;
3204 $$.w = 0;
3205 $$.w2 = 0;
3206 }
3207 | RETURNRST '(' TTL NUMBER ')' {
3208 if ($4 < 0 || $4 > 255) {
3209 yyerror("illegal ttl value %d", $4);
3210 YYERROR;
3211 }
3212 $$.b2 = PFRULE_RETURNRST;
3213 $$.w = $4;
3214 $$.w2 = 0;
3215 }
3216 | RETURNICMP {
3217 $$.b2 = PFRULE_RETURNICMP;
3218 $$.w = returnicmpdefault;
3219 $$.w2 = returnicmp6default;
3220 }
3221 | RETURNICMP6 {
3222 $$.b2 = PFRULE_RETURNICMP;
3223 $$.w = returnicmpdefault;
3224 $$.w2 = returnicmp6default;
3225 }
3226 | RETURNICMP '(' reticmpspec ')' {
3227 $$.b2 = PFRULE_RETURNICMP;
3228 $$.w = $3;
3229 $$.w2 = returnicmpdefault;
3230 }
3231 | RETURNICMP6 '(' reticmp6spec ')' {
3232 $$.b2 = PFRULE_RETURNICMP;
3233 $$.w = returnicmpdefault;
3234 $$.w2 = $3;
3235 }
3236 | RETURNICMP '(' reticmpspec comma reticmp6spec ')' {
3237 $$.b2 = PFRULE_RETURNICMP;
3238 $$.w = $3;
3239 $$.w2 = $5;
3240 }
3241 | RETURN {
3242 $$.b2 = PFRULE_RETURN;
3243 $$.w = returnicmpdefault;
3244 $$.w2 = returnicmp6default;
3245 }
3246 ;
3247
3248 reticmpspec : STRING {
3249 if (!($$ = parseicmpspec($1, AF_INET))) {
3250 free($1);
3251 YYERROR;
3252 }
3253 free($1);
3254 }
3255 | NUMBER {
3256 u_int8_t icmptype;
3257
3258 if ($1 < 0 || $1 > 255) {
3259 yyerror("invalid icmp code %lu", $1);
3260 YYERROR;
3261 }
3262 icmptype = returnicmpdefault >> 8;
3263 $$ = (icmptype << 8 | $1);
3264 }
3265 ;
3266
3267 reticmp6spec : STRING {
3268 if (!($$ = parseicmpspec($1, AF_INET6))) {
3269 free($1);
3270 YYERROR;
3271 }
3272 free($1);
3273 }
3274 | NUMBER {
3275 u_int8_t icmptype;
3276
3277 if ($1 < 0 || $1 > 255) {
3278 yyerror("invalid icmp code %lu", $1);
3279 YYERROR;
3280 }
3281 icmptype = returnicmp6default >> 8;
3282 $$ = (icmptype << 8 | $1);
3283 }
3284 ;
3285
3286 dir : /* empty */ { $$ = PF_INOUT; }
3287 | IN { $$ = PF_IN; }
3288 | OUT { $$ = PF_OUT; }
3289 ;
3290
3291 quick : /* empty */ { $$.quick = 0; }
3292 | QUICK { $$.quick = 1; }
3293 ;
3294
3295 logquick : /* empty */ { $$.log = 0; $$.quick = 0; $$.logif = 0; }
3296 | log { $$ = $1; $$.quick = 0; }
3297 | QUICK { $$.quick = 1; $$.log = 0; $$.logif = 0; }
3298 | log QUICK { $$ = $1; $$.quick = 1; }
3299 | QUICK log { $$ = $2; $$.quick = 1; }
3300 ;
3301
3302 log : LOG { $$.log = PF_LOG; $$.logif = 0; }
3303 | LOG '(' logopts ')' {
3304 $$.log = PF_LOG | $3.log;
3305 $$.logif = $3.logif;
3306 }
3307 ;
3308
3309 logopts : logopt { $$ = $1; }
3310 | logopts comma logopt {
3311 $$.log = $1.log | $3.log;
3312 $$.logif = $3.logif;
3313 if ($$.logif == 0)
3314 $$.logif = $1.logif;
3315 }
3316 ;
3317
3318 logopt : ALL { $$.log = PF_LOG_ALL; $$.logif = 0; }
3319 | MATCHES { $$.log = PF_LOG_MATCHES; $$.logif = 0; }
3320 | USER { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
3321 | GROUP { $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
3322 | TO string {
3323 const char *errstr;
3324 u_int i;
3325
3326 $$.log = 0;
3327 if (strncmp($2, "pflog", 5)) {
3328 yyerror("%s: should be a pflog interface", $2);
3329 free($2);
3330 YYERROR;
3331 }
3332 i = strtonum($2 + 5, 0, 255, &errstr);
3333 if (errstr) {
3334 yyerror("%s: %s", $2, errstr);
3335 free($2);
3336 YYERROR;
3337 }
3338 free($2);
3339 $$.logif = i;
3340 }
3341 ;
3342
3343 interface : /* empty */ { $$ = NULL; }
3344 | ON if_item_not { $$ = $2; }
3345 | ON '{' optnl if_list '}' { $$ = $4; }
3346 ;
3347
3348 if_list : if_item_not optnl { $$ = $1; }
3349 | if_list comma if_item_not optnl {
3350 $1->tail->next = $3;
3351 $1->tail = $3;
3352 $$ = $1;
3353 }
3354 ;
3355
3356 if_item_not : not if_item { $$ = $2; $$->not = $1; }
3357 ;
3358
3359 if_item : STRING {
3360 struct node_host *n;
3361
3362 $$ = calloc(1, sizeof(struct node_if));
3363 if ($$ == NULL)
3364 err(1, "if_item: calloc");
3365 if (strlcpy($$->ifname, $1, sizeof($$->ifname)) >=
3366 sizeof($$->ifname)) {
3367 free($1);
3368 free($$);
3369 yyerror("interface name too long");
3370 YYERROR;
3371 }
3372
3373 if ((n = ifa_exists($1)) != NULL)
3374 $$->ifa_flags = n->ifa_flags;
3375
3376 free($1);
3377 $$->not = 0;
3378 $$->next = NULL;
3379 $$->tail = $$;
3380 }
3381 ;
3382
3383 af : /* empty */ { $$ = 0; }
3384 | INET { $$ = AF_INET; }
3385 | INET6 { $$ = AF_INET6; }
3386 ;
3387
3388 etherproto : /* empty */ { $$ = NULL; }
3389 | PROTO etherproto_item { $$ = $2; }
3390 | PROTO '{' optnl etherproto_list '}' { $$ = $4; }
3391 ;
3392
3393 etherproto_list : etherproto_item optnl { $$ = $1; }
3394 | etherproto_list comma etherproto_item optnl {
3395 $1->tail->next = $3;
3396 $1->tail = $3;
3397 $$ = $1;
3398 }
3399 ;
3400
3401 etherproto_item : etherprotoval {
3402 u_int16_t pr;
3403
3404 pr = (u_int16_t)$1;
3405 if (pr == 0) {
3406 yyerror("proto 0 cannot be used");
3407 YYERROR;
3408 }
3409 $$ = calloc(1, sizeof(struct node_proto));
3410 if ($$ == NULL)
3411 err(1, "proto_item: calloc");
3412 $$->proto = pr;
3413 $$->next = NULL;
3414 $$->tail = $$;
3415 }
3416 ;
3417
3418 etherprotoval : NUMBER {
3419 if ($1 < 0 || $1 > 65565) {
3420 yyerror("protocol outside range");
3421 YYERROR;
3422 }
3423 }
3424 | STRING
3425 {
3426 if (!strncmp($1, "0x", 2)) {
3427 if (sscanf($1, "0x%4x", &$$) != 1) {
3428 free($1);
3429 yyerror("invalid EtherType hex");
3430 YYERROR;
3431 }
3432 } else {
3433 yyerror("Symbolic EtherType not yet supported");
3434 }
3435 }
3436 ;
3437
3438 proto : /* empty */ { $$ = NULL; }
3439 | PROTO proto_item { $$ = $2; }
3440 | PROTO '{' optnl proto_list '}' { $$ = $4; }
3441 ;
3442
3443 proto_list : proto_item optnl { $$ = $1; }
3444 | proto_list comma proto_item optnl {
3445 $1->tail->next = $3;
3446 $1->tail = $3;
3447 $$ = $1;
3448 }
3449 ;
3450
3451 proto_item : protoval {
3452 u_int8_t pr;
3453
3454 pr = (u_int8_t)$1;
3455 if (pr == 0) {
3456 yyerror("proto 0 cannot be used");
3457 YYERROR;
3458 }
3459 $$ = calloc(1, sizeof(struct node_proto));
3460 if ($$ == NULL)
3461 err(1, "proto_item: calloc");
3462 $$->proto = pr;
3463 $$->next = NULL;
3464 $$->tail = $$;
3465 }
3466 ;
3467
3468 protoval : STRING {
3469 struct protoent *p;
3470
3471 p = getprotobyname($1);
3472 if (p == NULL) {
3473 yyerror("unknown protocol %s", $1);
3474 free($1);
3475 YYERROR;
3476 }
3477 $$ = p->p_proto;
3478 free($1);
3479 }
3480 | NUMBER {
3481 if ($1 < 0 || $1 > 255) {
3482 yyerror("protocol outside range");
3483 YYERROR;
3484 }
3485 }
3486 ;
3487
3488 l3fromto : /* empty */ {
3489 bzero(&$$, sizeof($$));
3490 }
3491 | L3 fromto {
3492 if ($2.src.host != NULL &&
3493 $2.src.host->addr.type != PF_ADDR_ADDRMASK &&
3494 $2.src.host->addr.type != PF_ADDR_TABLE) {
3495 yyerror("from must be an address or table");
3496 YYERROR;
3497 }
3498 if ($2.dst.host != NULL &&
3499 $2.dst.host->addr.type != PF_ADDR_ADDRMASK &&
3500 $2.dst.host->addr.type != PF_ADDR_TABLE) {
3501 yyerror("to must be an address or table");
3502 YYERROR;
3503 }
3504 $$ = $2;
3505 }
3506 ;
3507 etherfromto : ALL {
3508 $$.src = NULL;
3509 $$.dst = NULL;
3510 }
3511 | etherfrom etherto {
3512 $$.src = $1.mac;
3513 $$.dst = $2.mac;
3514 }
3515 ;
3516
3517 etherfrom : /* emtpy */ {
3518 bzero(&$$, sizeof($$));
3519 }
3520 | FROM macspec {
3521 $$.mac = $2;
3522 }
3523 ;
3524
3525 etherto : /* empty */ {
3526 bzero(&$$, sizeof($$));
3527 }
3528 | TO macspec {
3529 $$.mac = $2;
3530 }
3531 ;
3532
3533 mac : string '/' NUMBER {
3534 $$ = node_mac_from_string_masklen($1, $3);
3535 free($1);
3536 if ($$ == NULL)
3537 YYERROR;
3538 }
3539 | string {
3540 if (strchr($1, '&')) {
3541 /* mac&mask */
3542 char *mac = strtok($1, "&");
3543 char *mask = strtok(NULL, "&");
3544 $$ = node_mac_from_string_mask(mac, mask);
3545 } else {
3546 $$ = node_mac_from_string($1);
3547 }
3548 free($1);
3549 if ($$ == NULL)
3550 YYERROR;
3551
3552 }
3553 xmac : not mac {
3554 struct node_mac *n;
3555
3556 for (n = $2; n != NULL; n = n->next)
3557 n->neg = $1;
3558 $$ = $2;
3559 }
3560 ;
3561 macspec : xmac {
3562 $$ = $1;
3563 }
3564 | '{' optnl mac_list '}'
3565 {
3566 $$ = $3;
3567 }
3568 ;
3569 mac_list : xmac optnl {
3570 $$ = $1;
3571 }
3572 | mac_list comma xmac {
3573 if ($3 == NULL)
3574 $$ = $1;
3575 else if ($1 == NULL)
3576 $$ = $3;
3577 else {
3578 $1->tail->next = $3;
3579 $1->tail = $3->tail;
3580 $$ = $1;
3581 }
3582 }
3583
3584 fromto : ALL {
3585 $$.src.host = NULL;
3586 $$.src.port = NULL;
3587 $$.dst.host = NULL;
3588 $$.dst.port = NULL;
3589 $$.src_os = NULL;
3590 }
3591 | from os to {
3592 $$.src = $1;
3593 $$.src_os = $2;
3594 $$.dst = $3;
3595 }
3596 ;
3597
3598 os : /* empty */ { $$ = NULL; }
3599 | OS xos { $$ = $2; }
3600 | OS '{' optnl os_list '}' { $$ = $4; }
3601 ;
3602
3603 xos : STRING {
3604 $$ = calloc(1, sizeof(struct node_os));
3605 if ($$ == NULL)
3606 err(1, "os: calloc");
3607 $$->os = $1;
3608 $$->tail = $$;
3609 }
3610 ;
3611
3612 os_list : xos optnl { $$ = $1; }
3613 | os_list comma xos optnl {
3614 $1->tail->next = $3;
3615 $1->tail = $3;
3616 $$ = $1;
3617 }
3618 ;
3619
3620 from : /* empty */ {
3621 $$.host = NULL;
3622 $$.port = NULL;
3623 }
3624 | FROM ipportspec {
3625 $$ = $2;
3626 }
3627 ;
3628
3629 to : /* empty */ {
3630 $$.host = NULL;
3631 $$.port = NULL;
3632 }
3633 | TO ipportspec {
3634 if (disallow_urpf_failed($2.host, "\"urpf-failed\" is "
3635 "not permitted in a destination address"))
3636 YYERROR;
3637 $$ = $2;
3638 }
3639 ;
3640
3641 ipportspec : ipspec {
3642 $$.host = $1;
3643 $$.port = NULL;
3644 }
3645 | ipspec PORT portspec {
3646 $$.host = $1;
3647 $$.port = $3;
3648 }
3649 | PORT portspec {
3650 $$.host = NULL;
3651 $$.port = $2;
3652 }
3653 ;
3654
3655 optnl : '\n' optnl
3656 |
3657 ;
3658
3659 ipspec : ANY { $$ = NULL; }
3660 | xhost { $$ = $1; }
3661 | '{' optnl host_list '}' { $$ = $3; }
3662 ;
3663
3664 toipspec : TO ipspec { $$ = $2; }
3665 | /* empty */ { $$ = NULL; }
3666 ;
3667
3668 host_list : ipspec optnl { $$ = $1; }
3669 | host_list comma ipspec optnl {
3670 if ($1 == NULL) {
3671 freehostlist($3);
3672 $$ = $1;
3673 } else if ($3 == NULL) {
3674 freehostlist($1);
3675 $$ = $3;
3676 } else {
3677 $1->tail->next = $3;
3678 $1->tail = $3->tail;
3679 $$ = $1;
3680 }
3681 }
3682 ;
3683
3684 xhost : not host {
3685 struct node_host *n;
3686
3687 for (n = $2; n != NULL; n = n->next)
3688 n->not = $1;
3689 $$ = $2;
3690 }
3691 | not NOROUTE {
3692 $$ = calloc(1, sizeof(struct node_host));
3693 if ($$ == NULL)
3694 err(1, "xhost: calloc");
3695 $$->addr.type = PF_ADDR_NOROUTE;
3696 $$->next = NULL;
3697 $$->not = $1;
3698 $$->tail = $$;
3699 }
3700 | not URPFFAILED {
3701 $$ = calloc(1, sizeof(struct node_host));
3702 if ($$ == NULL)
3703 err(1, "xhost: calloc");
3704 $$->addr.type = PF_ADDR_URPFFAILED;
3705 $$->next = NULL;
3706 $$->not = $1;
3707 $$->tail = $$;
3708 }
3709 ;
3710
3711 host : STRING {
3712 if (($$ = host($1)) == NULL) {
3713 /* error. "any" is handled elsewhere */
3714 free($1);
3715 yyerror("could not parse host specification");
3716 YYERROR;
3717 }
3718 free($1);
3719
3720 }
3721 | STRING '-' STRING {
3722 struct node_host *b, *e;
3723
3724 if ((b = host($1)) == NULL || (e = host($3)) == NULL) {
3725 free($1);
3726 free($3);
3727 yyerror("could not parse host specification");
3728 YYERROR;
3729 }
3730 if (b->af != e->af ||
3731 b->addr.type != PF_ADDR_ADDRMASK ||
3732 e->addr.type != PF_ADDR_ADDRMASK ||
3733 unmask(&b->addr.v.a.mask, b->af) !=
3734 (b->af == AF_INET ? 32 : 128) ||
3735 unmask(&e->addr.v.a.mask, e->af) !=
3736 (e->af == AF_INET ? 32 : 128) ||
3737 b->next != NULL || b->not ||
3738 e->next != NULL || e->not) {
3739 free(b);
3740 free(e);
3741 free($1);
3742 free($3);
3743 yyerror("invalid address range");
3744 YYERROR;
3745 }
3746 memcpy(&b->addr.v.a.mask, &e->addr.v.a.addr,
3747 sizeof(b->addr.v.a.mask));
3748 b->addr.type = PF_ADDR_RANGE;
3749 $$ = b;
3750 free(e);
3751 free($1);
3752 free($3);
3753 }
3754 | STRING '/' NUMBER {
3755 char *buf;
3756
3757 if (asprintf(&buf, "%s/%lld", $1, (long long)$3) == -1)
3758 err(1, "host: asprintf");
3759 free($1);
3760 if (($$ = host(buf)) == NULL) {
3761 /* error. "any" is handled elsewhere */
3762 free(buf);
3763 yyerror("could not parse host specification");
3764 YYERROR;
3765 }
3766 free(buf);
3767 }
3768 | NUMBER '/' NUMBER {
3769 char *buf;
3770
3771 /* ie. for 10/8 parsing */
3772 #ifdef __FreeBSD__
3773 if (asprintf(&buf, "%lld/%lld", (long long)$1, (long long)$3) == -1)
3774 #else
3775 if (asprintf(&buf, "%lld/%lld", $1, $3) == -1)
3776 #endif
3777 err(1, "host: asprintf");
3778 if (($$ = host(buf)) == NULL) {
3779 /* error. "any" is handled elsewhere */
3780 free(buf);
3781 yyerror("could not parse host specification");
3782 YYERROR;
3783 }
3784 free(buf);
3785 }
3786 | dynaddr
3787 | dynaddr '/' NUMBER {
3788 struct node_host *n;
3789
3790 if ($3 < 0 || $3 > 128) {
3791 yyerror("bit number too big");
3792 YYERROR;
3793 }
3794 $$ = $1;
3795 for (n = $1; n != NULL; n = n->next)
3796 set_ipmask(n, $3);
3797 }
3798 | '<' STRING '>' {
3799 if (strlen($2) >= PF_TABLE_NAME_SIZE) {
3800 yyerror("table name '%s' too long", $2);
3801 free($2);
3802 YYERROR;
3803 }
3804 $$ = calloc(1, sizeof(struct node_host));
3805 if ($$ == NULL)
3806 err(1, "host: calloc");
3807 $$->addr.type = PF_ADDR_TABLE;
3808 if (strlcpy($$->addr.v.tblname, $2,
3809 sizeof($$->addr.v.tblname)) >=
3810 sizeof($$->addr.v.tblname))
3811 errx(1, "host: strlcpy");
3812 free($2);
3813 $$->next = NULL;
3814 $$->tail = $$;
3815 }
3816 ;
3817
3818 number : NUMBER
3819 | STRING {
3820 u_long ulval;
3821
3822 if (atoul($1, &ulval) == -1) {
3823 yyerror("%s is not a number", $1);
3824 free($1);
3825 YYERROR;
3826 } else
3827 $$ = ulval;
3828 free($1);
3829 }
3830 ;
3831
3832 dynaddr : '(' STRING ')' {
3833 int flags = 0;
3834 char *p, *op;
3835
3836 op = $2;
3837 if (!isalpha(op[0])) {
3838 yyerror("invalid interface name '%s'", op);
3839 free(op);
3840 YYERROR;
3841 }
3842 while ((p = strrchr($2, ':')) != NULL) {
3843 if (!strcmp(p+1, "network"))
3844 flags |= PFI_AFLAG_NETWORK;
3845 else if (!strcmp(p+1, "broadcast"))
3846 flags |= PFI_AFLAG_BROADCAST;
3847 else if (!strcmp(p+1, "peer"))
3848 flags |= PFI_AFLAG_PEER;
3849 else if (!strcmp(p+1, "0"))
3850 flags |= PFI_AFLAG_NOALIAS;
3851 else {
3852 yyerror("interface %s has bad modifier",
3853 $2);
3854 free(op);
3855 YYERROR;
3856 }
3857 *p = '\0';
3858 }
3859 if (flags & (flags - 1) & PFI_AFLAG_MODEMASK) {
3860 free(op);
3861 yyerror("illegal combination of "
3862 "interface modifiers");
3863 YYERROR;
3864 }
3865 $$ = calloc(1, sizeof(struct node_host));
3866 if ($$ == NULL)
3867 err(1, "address: calloc");
3868 $$->af = 0;
3869 set_ipmask($$, 128);
3870 $$->addr.type = PF_ADDR_DYNIFTL;
3871 $$->addr.iflags = flags;
3872 if (strlcpy($$->addr.v.ifname, $2,
3873 sizeof($$->addr.v.ifname)) >=
3874 sizeof($$->addr.v.ifname)) {
3875 free(op);
3876 free($$);
3877 yyerror("interface name too long");
3878 YYERROR;
3879 }
3880 free(op);
3881 $$->next = NULL;
3882 $$->tail = $$;
3883 }
3884 ;
3885
3886 portspec : port_item { $$ = $1; }
3887 | '{' optnl port_list '}' { $$ = $3; }
3888 ;
3889
3890 port_list : port_item optnl { $$ = $1; }
3891 | port_list comma port_item optnl {
3892 $1->tail->next = $3;
3893 $1->tail = $3;
3894 $$ = $1;
3895 }
3896 ;
3897
3898 port_item : portrange {
3899 $$ = calloc(1, sizeof(struct node_port));
3900 if ($$ == NULL)
3901 err(1, "port_item: calloc");
3902 $$->port[0] = $1.a;
3903 $$->port[1] = $1.b;
3904 if ($1.t)
3905 $$->op = PF_OP_RRG;
3906 else
3907 $$->op = PF_OP_EQ;
3908 $$->next = NULL;
3909 $$->tail = $$;
3910 }
3911 | unaryop portrange {
3912 if ($2.t) {
3913 yyerror("':' cannot be used with an other "
3914 "port operator");
3915 YYERROR;
3916 }
3917 $$ = calloc(1, sizeof(struct node_port));
3918 if ($$ == NULL)
3919 err(1, "port_item: calloc");
3920 $$->port[0] = $2.a;
3921 $$->port[1] = $2.b;
3922 $$->op = $1;
3923 $$->next = NULL;
3924 $$->tail = $$;
3925 }
3926 | portrange PORTBINARY portrange {
3927 if ($1.t || $3.t) {
3928 yyerror("':' cannot be used with an other "
3929 "port operator");
3930 YYERROR;
3931 }
3932 $$ = calloc(1, sizeof(struct node_port));
3933 if ($$ == NULL)
3934 err(1, "port_item: calloc");
3935 $$->port[0] = $1.a;
3936 $$->port[1] = $3.a;
3937 $$->op = $2;
3938 $$->next = NULL;
3939 $$->tail = $$;
3940 }
3941 ;
3942
3943 portplain : numberstring {
3944 if (parseport($1, &$$, 0) == -1) {
3945 free($1);
3946 YYERROR;
3947 }
3948 free($1);
3949 }
3950 ;
3951
3952 portrange : numberstring {
3953 if (parseport($1, &$$, PPORT_RANGE) == -1) {
3954 free($1);
3955 YYERROR;
3956 }
3957 free($1);
3958 }
3959 ;
3960
3961 uids : uid_item { $$ = $1; }
3962 | '{' optnl uid_list '}' { $$ = $3; }
3963 ;
3964
3965 uid_list : uid_item optnl { $$ = $1; }
3966 | uid_list comma uid_item optnl {
3967 $1->tail->next = $3;
3968 $1->tail = $3;
3969 $$ = $1;
3970 }
3971 ;
3972
3973 uid_item : uid {
3974 $$ = calloc(1, sizeof(struct node_uid));
3975 if ($$ == NULL)
3976 err(1, "uid_item: calloc");
3977 $$->uid[0] = $1;
3978 $$->uid[1] = $1;
3979 $$->op = PF_OP_EQ;
3980 $$->next = NULL;
3981 $$->tail = $$;
3982 }
3983 | unaryop uid {
3984 if ($2 == UID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
3985 yyerror("user unknown requires operator = or "
3986 "!=");
3987 YYERROR;
3988 }
3989 $$ = calloc(1, sizeof(struct node_uid));
3990 if ($$ == NULL)
3991 err(1, "uid_item: calloc");
3992 $$->uid[0] = $2;
3993 $$->uid[1] = $2;
3994 $$->op = $1;
3995 $$->next = NULL;
3996 $$->tail = $$;
3997 }
3998 | uid PORTBINARY uid {
3999 if ($1 == UID_MAX || $3 == UID_MAX) {
4000 yyerror("user unknown requires operator = or "
4001 "!=");
4002 YYERROR;
4003 }
4004 $$ = calloc(1, sizeof(struct node_uid));
4005 if ($$ == NULL)
4006 err(1, "uid_item: calloc");
4007 $$->uid[0] = $1;
4008 $$->uid[1] = $3;
4009 $$->op = $2;
4010 $$->next = NULL;
4011 $$->tail = $$;
4012 }
4013 ;
4014
4015 uid : STRING {
4016 if (!strcmp($1, "unknown"))
4017 $$ = UID_MAX;
4018 else {
4019 struct passwd *pw;
4020
4021 if ((pw = getpwnam($1)) == NULL) {
4022 yyerror("unknown user %s", $1);
4023 free($1);
4024 YYERROR;
4025 }
4026 $$ = pw->pw_uid;
4027 }
4028 free($1);
4029 }
4030 | NUMBER {
4031 if ($1 < 0 || $1 >= UID_MAX) {
4032 yyerror("illegal uid value %lu", $1);
4033 YYERROR;
4034 }
4035 $$ = $1;
4036 }
4037 ;
4038
4039 gids : gid_item { $$ = $1; }
4040 | '{' optnl gid_list '}' { $$ = $3; }
4041 ;
4042
4043 gid_list : gid_item optnl { $$ = $1; }
4044 | gid_list comma gid_item optnl {
4045 $1->tail->next = $3;
4046 $1->tail = $3;
4047 $$ = $1;
4048 }
4049 ;
4050
4051 gid_item : gid {
4052 $$ = calloc(1, sizeof(struct node_gid));
4053 if ($$ == NULL)
4054 err(1, "gid_item: calloc");
4055 $$->gid[0] = $1;
4056 $$->gid[1] = $1;
4057 $$->op = PF_OP_EQ;
4058 $$->next = NULL;
4059 $$->tail = $$;
4060 }
4061 | unaryop gid {
4062 if ($2 == GID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
4063 yyerror("group unknown requires operator = or "
4064 "!=");
4065 YYERROR;
4066 }
4067 $$ = calloc(1, sizeof(struct node_gid));
4068 if ($$ == NULL)
4069 err(1, "gid_item: calloc");
4070 $$->gid[0] = $2;
4071 $$->gid[1] = $2;
4072 $$->op = $1;
4073 $$->next = NULL;
4074 $$->tail = $$;
4075 }
4076 | gid PORTBINARY gid {
4077 if ($1 == GID_MAX || $3 == GID_MAX) {
4078 yyerror("group unknown requires operator = or "
4079 "!=");
4080 YYERROR;
4081 }
4082 $$ = calloc(1, sizeof(struct node_gid));
4083 if ($$ == NULL)
4084 err(1, "gid_item: calloc");
4085 $$->gid[0] = $1;
4086 $$->gid[1] = $3;
4087 $$->op = $2;
4088 $$->next = NULL;
4089 $$->tail = $$;
4090 }
4091 ;
4092
4093 gid : STRING {
4094 if (!strcmp($1, "unknown"))
4095 $$ = GID_MAX;
4096 else {
4097 struct group *grp;
4098
4099 if ((grp = getgrnam($1)) == NULL) {
4100 yyerror("unknown group %s", $1);
4101 free($1);
4102 YYERROR;
4103 }
4104 $$ = grp->gr_gid;
4105 }
4106 free($1);
4107 }
4108 | NUMBER {
4109 if ($1 < 0 || $1 >= GID_MAX) {
4110 yyerror("illegal gid value %lu", $1);
4111 YYERROR;
4112 }
4113 $$ = $1;
4114 }
4115 ;
4116
4117 flag : STRING {
4118 int f;
4119
4120 if ((f = parse_flags($1)) < 0) {
4121 yyerror("bad flags %s", $1);
4122 free($1);
4123 YYERROR;
4124 }
4125 free($1);
4126 $$.b1 = f;
4127 }
4128 ;
4129
4130 flags : FLAGS flag '/' flag { $$.b1 = $2.b1; $$.b2 = $4.b1; }
4131 | FLAGS '/' flag { $$.b1 = 0; $$.b2 = $3.b1; }
4132 | FLAGS ANY { $$.b1 = 0; $$.b2 = 0; }
4133 ;
4134
4135 icmpspec : ICMPTYPE icmp_item { $$ = $2; }
4136 | ICMPTYPE '{' optnl icmp_list '}' { $$ = $4; }
4137 | ICMP6TYPE icmp6_item { $$ = $2; }
4138 | ICMP6TYPE '{' optnl icmp6_list '}' { $$ = $4; }
4139 ;
4140
4141 icmp_list : icmp_item optnl { $$ = $1; }
4142 | icmp_list comma icmp_item optnl {
4143 $1->tail->next = $3;
4144 $1->tail = $3;
4145 $$ = $1;
4146 }
4147 ;
4148
4149 icmp6_list : icmp6_item optnl { $$ = $1; }
4150 | icmp6_list comma icmp6_item optnl {
4151 $1->tail->next = $3;
4152 $1->tail = $3;
4153 $$ = $1;
4154 }
4155 ;
4156
4157 icmp_item : icmptype {
4158 $$ = calloc(1, sizeof(struct node_icmp));
4159 if ($$ == NULL)
4160 err(1, "icmp_item: calloc");
4161 $$->type = $1;
4162 $$->code = 0;
4163 $$->proto = IPPROTO_ICMP;
4164 $$->next = NULL;
4165 $$->tail = $$;
4166 }
4167 | icmptype CODE STRING {
4168 const struct icmpcodeent *p;
4169
4170 if ((p = geticmpcodebyname($1-1, $3, AF_INET)) == NULL) {
4171 yyerror("unknown icmp-code %s", $3);
4172 free($3);
4173 YYERROR;
4174 }
4175
4176 free($3);
4177 $$ = calloc(1, sizeof(struct node_icmp));
4178 if ($$ == NULL)
4179 err(1, "icmp_item: calloc");
4180 $$->type = $1;
4181 $$->code = p->code + 1;
4182 $$->proto = IPPROTO_ICMP;
4183 $$->next = NULL;
4184 $$->tail = $$;
4185 }
4186 | icmptype CODE NUMBER {
4187 if ($3 < 0 || $3 > 255) {
4188 yyerror("illegal icmp-code %lu", $3);
4189 YYERROR;
4190 }
4191 $$ = calloc(1, sizeof(struct node_icmp));
4192 if ($$ == NULL)
4193 err(1, "icmp_item: calloc");
4194 $$->type = $1;
4195 $$->code = $3 + 1;
4196 $$->proto = IPPROTO_ICMP;
4197 $$->next = NULL;
4198 $$->tail = $$;
4199 }
4200 ;
4201
4202 icmp6_item : icmp6type {
4203 $$ = calloc(1, sizeof(struct node_icmp));
4204 if ($$ == NULL)
4205 err(1, "icmp_item: calloc");
4206 $$->type = $1;
4207 $$->code = 0;
4208 $$->proto = IPPROTO_ICMPV6;
4209 $$->next = NULL;
4210 $$->tail = $$;
4211 }
4212 | icmp6type CODE STRING {
4213 const struct icmpcodeent *p;
4214
4215 if ((p = geticmpcodebyname($1-1, $3, AF_INET6)) == NULL) {
4216 yyerror("unknown icmp6-code %s", $3);
4217 free($3);
4218 YYERROR;
4219 }
4220 free($3);
4221
4222 $$ = calloc(1, sizeof(struct node_icmp));
4223 if ($$ == NULL)
4224 err(1, "icmp_item: calloc");
4225 $$->type = $1;
4226 $$->code = p->code + 1;
4227 $$->proto = IPPROTO_ICMPV6;
4228 $$->next = NULL;
4229 $$->tail = $$;
4230 }
4231 | icmp6type CODE NUMBER {
4232 if ($3 < 0 || $3 > 255) {
4233 yyerror("illegal icmp-code %lu", $3);
4234 YYERROR;
4235 }
4236 $$ = calloc(1, sizeof(struct node_icmp));
4237 if ($$ == NULL)
4238 err(1, "icmp_item: calloc");
4239 $$->type = $1;
4240 $$->code = $3 + 1;
4241 $$->proto = IPPROTO_ICMPV6;
4242 $$->next = NULL;
4243 $$->tail = $$;
4244 }
4245 ;
4246
4247 icmptype : STRING {
4248 const struct icmptypeent *p;
4249
4250 if ((p = geticmptypebyname($1, AF_INET)) == NULL) {
4251 yyerror("unknown icmp-type %s", $1);
4252 free($1);
4253 YYERROR;
4254 }
4255 $$ = p->type + 1;
4256 free($1);
4257 }
4258 | NUMBER {
4259 if ($1 < 0 || $1 > 255) {
4260 yyerror("illegal icmp-type %lu", $1);
4261 YYERROR;
4262 }
4263 $$ = $1 + 1;
4264 }
4265 ;
4266
4267 icmp6type : STRING {
4268 const struct icmptypeent *p;
4269
4270 if ((p = geticmptypebyname($1, AF_INET6)) ==
4271 NULL) {
4272 yyerror("unknown icmp6-type %s", $1);
4273 free($1);
4274 YYERROR;
4275 }
4276 $$ = p->type + 1;
4277 free($1);
4278 }
4279 | NUMBER {
4280 if ($1 < 0 || $1 > 255) {
4281 yyerror("illegal icmp6-type %lu", $1);
4282 YYERROR;
4283 }
4284 $$ = $1 + 1;
4285 }
4286 ;
4287
4288 tos : STRING {
4289 int val;
4290 char *end;
4291
4292 if (map_tos($1, &val))
4293 $$ = val;
4294 else if ($1[0] == '0' && $1[1] == 'x') {
4295 errno = 0;
4296 $$ = strtoul($1, &end, 16);
4297 if (errno || *end != '\0')
4298 $$ = 256;
4299 } else
4300 $$ = 256; /* flag bad argument */
4301 if ($$ < 0 || $$ > 255) {
4302 yyerror("illegal tos value %s", $1);
4303 free($1);
4304 YYERROR;
4305 }
4306 free($1);
4307 }
4308 | NUMBER {
4309 $$ = $1;
4310 if ($$ < 0 || $$ > 255) {
4311 yyerror("illegal tos value %lu", $1);
4312 YYERROR;
4313 }
4314 }
4315 ;
4316
4317 sourcetrack : SOURCETRACK { $$ = PF_SRCTRACK; }
4318 | SOURCETRACK GLOBAL { $$ = PF_SRCTRACK_GLOBAL; }
4319 | SOURCETRACK RULE { $$ = PF_SRCTRACK_RULE; }
4320 ;
4321
4322 statelock : IFBOUND {
4323 $$ = PFRULE_IFBOUND;
4324 }
4325 | FLOATING {
4326 $$ = 0;
4327 }
4328 ;
4329
4330 keep : NO STATE {
4331 $$.action = 0;
4332 $$.options = NULL;
4333 }
4334 | KEEP STATE state_opt_spec {
4335 $$.action = PF_STATE_NORMAL;
4336 $$.options = $3;
4337 }
4338 | MODULATE STATE state_opt_spec {
4339 $$.action = PF_STATE_MODULATE;
4340 $$.options = $3;
4341 }
4342 | SYNPROXY STATE state_opt_spec {
4343 $$.action = PF_STATE_SYNPROXY;
4344 $$.options = $3;
4345 }
4346 ;
4347
4348 flush : /* empty */ { $$ = 0; }
4349 | FLUSH { $$ = PF_FLUSH; }
4350 | FLUSH GLOBAL {
4351 $$ = PF_FLUSH | PF_FLUSH_GLOBAL;
4352 }
4353 ;
4354
4355 state_opt_spec : '(' state_opt_list ')' { $$ = $2; }
4356 | /* empty */ { $$ = NULL; }
4357 ;
4358
4359 state_opt_list : state_opt_item { $$ = $1; }
4360 | state_opt_list comma state_opt_item {
4361 $1->tail->next = $3;
4362 $1->tail = $3;
4363 $$ = $1;
4364 }
4365 ;
4366
4367 state_opt_item : MAXIMUM NUMBER {
4368 if ($2 < 0 || $2 > UINT_MAX) {
4369 yyerror("only positive values permitted");
4370 YYERROR;
4371 }
4372 $$ = calloc(1, sizeof(struct node_state_opt));
4373 if ($$ == NULL)
4374 err(1, "state_opt_item: calloc");
4375 $$->type = PF_STATE_OPT_MAX;
4376 $$->data.max_states = $2;
4377 $$->next = NULL;
4378 $$->tail = $$;
4379 }
4380 | NOSYNC {
4381 $$ = calloc(1, sizeof(struct node_state_opt));
4382 if ($$ == NULL)
4383 err(1, "state_opt_item: calloc");
4384 $$->type = PF_STATE_OPT_NOSYNC;
4385 $$->next = NULL;
4386 $$->tail = $$;
4387 }
4388 | MAXSRCSTATES NUMBER {
4389 if ($2 < 0 || $2 > UINT_MAX) {
4390 yyerror("only positive values permitted");
4391 YYERROR;
4392 }
4393 $$ = calloc(1, sizeof(struct node_state_opt));
4394 if ($$ == NULL)
4395 err(1, "state_opt_item: calloc");
4396 $$->type = PF_STATE_OPT_MAX_SRC_STATES;
4397 $$->data.max_src_states = $2;
4398 $$->next = NULL;
4399 $$->tail = $$;
4400 }
4401 | MAXSRCCONN NUMBER {
4402 if ($2 < 0 || $2 > UINT_MAX) {
4403 yyerror("only positive values permitted");
4404 YYERROR;
4405 }
4406 $$ = calloc(1, sizeof(struct node_state_opt));
4407 if ($$ == NULL)
4408 err(1, "state_opt_item: calloc");
4409 $$->type = PF_STATE_OPT_MAX_SRC_CONN;
4410 $$->data.max_src_conn = $2;
4411 $$->next = NULL;
4412 $$->tail = $$;
4413 }
4414 | MAXSRCCONNRATE NUMBER '/' NUMBER {
4415 if ($2 < 0 || $2 > UINT_MAX ||
4416 $4 < 0 || $4 > UINT_MAX) {
4417 yyerror("only positive values permitted");
4418 YYERROR;
4419 }
4420 $$ = calloc(1, sizeof(struct node_state_opt));
4421 if ($$ == NULL)
4422 err(1, "state_opt_item: calloc");
4423 $$->type = PF_STATE_OPT_MAX_SRC_CONN_RATE;
4424 $$->data.max_src_conn_rate.limit = $2;
4425 $$->data.max_src_conn_rate.seconds = $4;
4426 $$->next = NULL;
4427 $$->tail = $$;
4428 }
4429 | OVERLOAD '<' STRING '>' flush {
4430 if (strlen($3) >= PF_TABLE_NAME_SIZE) {
4431 yyerror("table name '%s' too long", $3);
4432 free($3);
4433 YYERROR;
4434 }
4435 $$ = calloc(1, sizeof(struct node_state_opt));
4436 if ($$ == NULL)
4437 err(1, "state_opt_item: calloc");
4438 if (strlcpy($$->data.overload.tblname, $3,
4439 PF_TABLE_NAME_SIZE) >= PF_TABLE_NAME_SIZE)
4440 errx(1, "state_opt_item: strlcpy");
4441 free($3);
4442 $$->type = PF_STATE_OPT_OVERLOAD;
4443 $$->data.overload.flush = $5;
4444 $$->next = NULL;
4445 $$->tail = $$;
4446 }
4447 | MAXSRCNODES NUMBER {
4448 if ($2 < 0 || $2 > UINT_MAX) {
4449 yyerror("only positive values permitted");
4450 YYERROR;
4451 }
4452 $$ = calloc(1, sizeof(struct node_state_opt));
4453 if ($$ == NULL)
4454 err(1, "state_opt_item: calloc");
4455 $$->type = PF_STATE_OPT_MAX_SRC_NODES;
4456 $$->data.max_src_nodes = $2;
4457 $$->next = NULL;
4458 $$->tail = $$;
4459 }
4460 | sourcetrack {
4461 $$ = calloc(1, sizeof(struct node_state_opt));
4462 if ($$ == NULL)
4463 err(1, "state_opt_item: calloc");
4464 $$->type = PF_STATE_OPT_SRCTRACK;
4465 $$->data.src_track = $1;
4466 $$->next = NULL;
4467 $$->tail = $$;
4468 }
4469 | statelock {
4470 $$ = calloc(1, sizeof(struct node_state_opt));
4471 if ($$ == NULL)
4472 err(1, "state_opt_item: calloc");
4473 $$->type = PF_STATE_OPT_STATELOCK;
4474 $$->data.statelock = $1;
4475 $$->next = NULL;
4476 $$->tail = $$;
4477 }
4478 | SLOPPY {
4479 $$ = calloc(1, sizeof(struct node_state_opt));
4480 if ($$ == NULL)
4481 err(1, "state_opt_item: calloc");
4482 $$->type = PF_STATE_OPT_SLOPPY;
4483 $$->next = NULL;
4484 $$->tail = $$;
4485 }
4486 | PFLOW {
4487 $$ = calloc(1, sizeof(struct node_state_opt));
4488 if ($$ == NULL)
4489 err(1, "state_opt_item: calloc");
4490 $$->type = PF_STATE_OPT_PFLOW;
4491 $$->next = NULL;
4492 $$->tail = $$;
4493 }
4494 | ALLOW_RELATED {
4495 $$ = calloc(1, sizeof(struct node_state_opt));
4496 if ($$ == NULL)
4497 err(1, "state_opt_item: calloc");
4498 $$->type = PF_STATE_OPT_ALLOW_RELATED;
4499 $$->next = NULL;
4500 $$->tail = $$;
4501 }
4502 | STRING NUMBER {
4503 int i;
4504
4505 if ($2 < 0 || $2 > UINT_MAX) {
4506 yyerror("only positive values permitted");
4507 YYERROR;
4508 }
4509 for (i = 0; pf_timeouts[i].name &&
4510 strcmp(pf_timeouts[i].name, $1); ++i)
4511 ; /* nothing */
4512 if (!pf_timeouts[i].name) {
4513 yyerror("illegal timeout name %s", $1);
4514 free($1);
4515 YYERROR;
4516 }
4517 if (strchr(pf_timeouts[i].name, '.') == NULL) {
4518 yyerror("illegal state timeout %s", $1);
4519 free($1);
4520 YYERROR;
4521 }
4522 free($1);
4523 $$ = calloc(1, sizeof(struct node_state_opt));
4524 if ($$ == NULL)
4525 err(1, "state_opt_item: calloc");
4526 $$->type = PF_STATE_OPT_TIMEOUT;
4527 $$->data.timeout.number = pf_timeouts[i].timeout;
4528 $$->data.timeout.seconds = $2;
4529 $$->next = NULL;
4530 $$->tail = $$;
4531 }
4532 ;
4533
4534 label : LABEL STRING {
4535 $$ = $2;
4536 }
4537 ;
4538
4539 etherqname : QUEUE STRING {
4540 $$.qname = $2;
4541 }
4542 | QUEUE '(' STRING ')' {
4543 $$.qname = $3;
4544 }
4545 ;
4546
4547 qname : QUEUE STRING {
4548 $$.qname = $2;
4549 $$.pqname = NULL;
4550 }
4551 | QUEUE '(' STRING ')' {
4552 $$.qname = $3;
4553 $$.pqname = NULL;
4554 }
4555 | QUEUE '(' STRING comma STRING ')' {
4556 $$.qname = $3;
4557 $$.pqname = $5;
4558 }
4559 ;
4560
4561 no : /* empty */ { $$ = 0; }
4562 | NO { $$ = 1; }
4563 ;
4564
4565 portstar : numberstring {
4566 if (parseport($1, &$$, PPORT_RANGE|PPORT_STAR) == -1) {
4567 free($1);
4568 YYERROR;
4569 }
4570 free($1);
4571 }
4572 ;
4573
4574 redirspec : host { $$ = $1; }
4575 | '{' optnl redir_host_list '}' { $$ = $3; }
4576 ;
4577
4578 redir_host_list : host optnl { $$ = $1; }
4579 | redir_host_list comma host optnl {
4580 $1->tail->next = $3;
4581 $1->tail = $3->tail;
4582 $$ = $1;
4583 }
4584 ;
4585
4586 redirpool : /* empty */ { $$ = NULL; }
4587 | ARROW redirspec {
4588 $$ = calloc(1, sizeof(struct redirection));
4589 if ($$ == NULL)
4590 err(1, "redirection: calloc");
4591 $$->host = $2;
4592 $$->rport.a = $$->rport.b = $$->rport.t = 0;
4593 }
4594 | ARROW redirspec PORT portstar {
4595 $$ = calloc(1, sizeof(struct redirection));
4596 if ($$ == NULL)
4597 err(1, "redirection: calloc");
4598 $$->host = $2;
4599 $$->rport = $4;
4600 }
4601 ;
4602
4603 hashkey : /* empty */
4604 {
4605 $$ = calloc(1, sizeof(struct pf_poolhashkey));
4606 if ($$ == NULL)
4607 err(1, "hashkey: calloc");
4608 $$->key32[0] = arc4random();
4609 $$->key32[1] = arc4random();
4610 $$->key32[2] = arc4random();
4611 $$->key32[3] = arc4random();
4612 }
4613 | string
4614 {
4615 if (!strncmp($1, "0x", 2)) {
4616 if (strlen($1) != 34) {
4617 free($1);
4618 yyerror("hex key must be 128 bits "
4619 "(32 hex digits) long");
4620 YYERROR;
4621 }
4622 $$ = calloc(1, sizeof(struct pf_poolhashkey));
4623 if ($$ == NULL)
4624 err(1, "hashkey: calloc");
4625
4626 if (sscanf($1, "0x%8x%8x%8x%8x",
4627 &$$->key32[0], &$$->key32[1],
4628 &$$->key32[2], &$$->key32[3]) != 4) {
4629 free($$);
4630 free($1);
4631 yyerror("invalid hex key");
4632 YYERROR;
4633 }
4634 } else {
4635 MD5_CTX context;
4636
4637 $$ = calloc(1, sizeof(struct pf_poolhashkey));
4638 if ($$ == NULL)
4639 err(1, "hashkey: calloc");
4640 MD5Init(&context);
4641 MD5Update(&context, (unsigned char *)$1,
4642 strlen($1));
4643 MD5Final((unsigned char *)$$, &context);
4644 HTONL($$->key32[0]);
4645 HTONL($$->key32[1]);
4646 HTONL($$->key32[2]);
4647 HTONL($$->key32[3]);
4648 }
4649 free($1);
4650 }
4651 ;
4652
4653 pool_opts : { bzero(&pool_opts, sizeof pool_opts); }
4654 pool_opts_l
4655 { $$ = pool_opts; }
4656 | /* empty */ {
4657 bzero(&pool_opts, sizeof pool_opts);
4658 $$ = pool_opts;
4659 }
4660 ;
4661
4662 pool_opts_l : pool_opts_l pool_opt
4663 | pool_opt
4664 ;
4665
4666 pool_opt : BITMASK {
4667 if (pool_opts.type) {
4668 yyerror("pool type cannot be redefined");
4669 YYERROR;
4670 }
4671 pool_opts.type = PF_POOL_BITMASK;
4672 }
4673 | RANDOM {
4674 if (pool_opts.type) {
4675 yyerror("pool type cannot be redefined");
4676 YYERROR;
4677 }
4678 pool_opts.type = PF_POOL_RANDOM;
4679 }
4680 | SOURCEHASH hashkey {
4681 if (pool_opts.type) {
4682 yyerror("pool type cannot be redefined");
4683 YYERROR;
4684 }
4685 pool_opts.type = PF_POOL_SRCHASH;
4686 pool_opts.key = $2;
4687 }
4688 | ROUNDROBIN {
4689 if (pool_opts.type) {
4690 yyerror("pool type cannot be redefined");
4691 YYERROR;
4692 }
4693 pool_opts.type = PF_POOL_ROUNDROBIN;
4694 }
4695 | STATICPORT {
4696 if (pool_opts.staticport) {
4697 yyerror("static-port cannot be redefined");
4698 YYERROR;
4699 }
4700 pool_opts.staticport = 1;
4701 }
4702 | STICKYADDRESS {
4703 if (pool_opts.marker & POM_STICKYADDRESS) {
4704 yyerror("sticky-address cannot be redefined");
4705 YYERROR;
4706 }
4707 pool_opts.marker |= POM_STICKYADDRESS;
4708 pool_opts.opts |= PF_POOL_STICKYADDR;
4709 }
4710 | ENDPI {
4711 if (pool_opts.marker & POM_ENDPI) {
4712 yyerror("endpoint-independent cannot be redefined");
4713 YYERROR;
4714 }
4715 pool_opts.marker |= POM_ENDPI;
4716 pool_opts.opts |= PF_POOL_ENDPI;
4717 }
4718 | MAPEPORTSET number '/' number '/' number {
4719 if (pool_opts.mape.offset) {
4720 yyerror("map-e-portset cannot be redefined");
4721 YYERROR;
4722 }
4723 if (pool_opts.type) {
4724 yyerror("map-e-portset cannot be used with "
4725 "address pools");
4726 YYERROR;
4727 }
4728 if ($2 <= 0 || $2 >= 16) {
4729 yyerror("MAP-E PSID offset must be 1-15");
4730 YYERROR;
4731 }
4732 if ($4 < 0 || $4 >= 16 || $2 + $4 > 16) {
4733 yyerror("Invalid MAP-E PSID length");
4734 YYERROR;
4735 } else if ($4 == 0) {
4736 yyerror("PSID Length = 0: this means"
4737 " you do not need MAP-E");
4738 YYERROR;
4739 }
4740 if ($6 < 0 || $6 > 65535) {
4741 yyerror("Invalid MAP-E PSID");
4742 YYERROR;
4743 }
4744 pool_opts.mape.offset = $2;
4745 pool_opts.mape.psidlen = $4;
4746 pool_opts.mape.psid = $6;
4747 }
4748 ;
4749
4750 redirection : /* empty */ { $$ = NULL; }
4751 | ARROW host {
4752 $$ = calloc(1, sizeof(struct redirection));
4753 if ($$ == NULL)
4754 err(1, "redirection: calloc");
4755 $$->host = $2;
4756 $$->rport.a = $$->rport.b = $$->rport.t = 0;
4757 }
4758 | ARROW host PORT portstar {
4759 $$ = calloc(1, sizeof(struct redirection));
4760 if ($$ == NULL)
4761 err(1, "redirection: calloc");
4762 $$->host = $2;
4763 $$->rport = $4;
4764 }
4765 ;
4766
4767 natpasslog : /* empty */ { $$.b1 = $$.b2 = 0; $$.w2 = 0; }
4768 | PASS { $$.b1 = 1; $$.b2 = 0; $$.w2 = 0; }
4769 | PASS log { $$.b1 = 1; $$.b2 = $2.log; $$.w2 = $2.logif; }
4770 | log { $$.b1 = 0; $$.b2 = $1.log; $$.w2 = $1.logif; }
4771 ;
4772
4773 nataction : no NAT natpasslog {
4774 if ($1 && $3.b1) {
4775 yyerror("\"pass\" not valid with \"no\"");
4776 YYERROR;
4777 }
4778 if ($1)
4779 $$.b1 = PF_NONAT;
4780 else
4781 $$.b1 = PF_NAT;
4782 $$.b2 = $3.b1;
4783 $$.w = $3.b2;
4784 $$.w2 = $3.w2;
4785 }
4786 | no RDR natpasslog {
4787 if ($1 && $3.b1) {
4788 yyerror("\"pass\" not valid with \"no\"");
4789 YYERROR;
4790 }
4791 if ($1)
4792 $$.b1 = PF_NORDR;
4793 else
4794 $$.b1 = PF_RDR;
4795 $$.b2 = $3.b1;
4796 $$.w = $3.b2;
4797 $$.w2 = $3.w2;
4798 }
4799 ;
4800
4801 natrule : nataction interface af proto fromto tag tagged rtable
4802 redirpool pool_opts
4803 {
4804 struct pfctl_rule r;
4805 struct node_state_opt *o;
4806
4807 if (check_rulestate(PFCTL_STATE_NAT))
4808 YYERROR;
4809
4810 memset(&r, 0, sizeof(r));
4811
4812 r.action = $1.b1;
4813 r.natpass = $1.b2;
4814 r.log = $1.w;
4815 r.logif = $1.w2;
4816 r.af = $3;
4817
4818 if (!r.af) {
4819 if ($5.src.host && $5.src.host->af &&
4820 !$5.src.host->ifindex)
4821 r.af = $5.src.host->af;
4822 else if ($5.dst.host && $5.dst.host->af &&
4823 !$5.dst.host->ifindex)
4824 r.af = $5.dst.host->af;
4825 }
4826
4827 if ($6 != NULL)
4828 if (strlcpy(r.tagname, $6, PF_TAG_NAME_SIZE) >=
4829 PF_TAG_NAME_SIZE) {
4830 yyerror("tag too long, max %u chars",
4831 PF_TAG_NAME_SIZE - 1);
4832 YYERROR;
4833 }
4834
4835 if ($7.name)
4836 if (strlcpy(r.match_tagname, $7.name,
4837 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4838 yyerror("tag too long, max %u chars",
4839 PF_TAG_NAME_SIZE - 1);
4840 YYERROR;
4841 }
4842 r.match_tag_not = $7.neg;
4843 r.rtableid = $8;
4844
4845 if (r.action == PF_NONAT || r.action == PF_NORDR) {
4846 if ($9 != NULL) {
4847 yyerror("translation rule with 'no' "
4848 "does not need '->'");
4849 YYERROR;
4850 }
4851 } else {
4852 if ($9 == NULL || $9->host == NULL) {
4853 yyerror("translation rule requires '-> "
4854 "address'");
4855 YYERROR;
4856 }
4857 if (!r.af && ! $9->host->ifindex)
4858 r.af = $9->host->af;
4859
4860 remove_invalid_hosts(&$9->host, &r.af);
4861 if (invalid_redirect($9->host, r.af))
4862 YYERROR;
4863 if ($9->host->addr.type == PF_ADDR_DYNIFTL) {
4864 if (($9->host = gen_dynnode($9->host, r.af)) == NULL)
4865 err(1, "calloc");
4866 }
4867 if (check_netmask($9->host, r.af))
4868 YYERROR;
4869
4870 r.rdr.proxy_port[0] = ntohs($9->rport.a);
4871
4872 switch (r.action) {
4873 case PF_RDR:
4874 if (!$9->rport.b && $9->rport.t &&
4875 $5.dst.port != NULL) {
4876 r.rdr.proxy_port[1] =
4877 ntohs($9->rport.a) +
4878 (ntohs(
4879 $5.dst.port->port[1]) -
4880 ntohs(
4881 $5.dst.port->port[0]));
4882 } else
4883 r.rdr.proxy_port[1] =
4884 ntohs($9->rport.b);
4885 break;
4886 case PF_NAT:
4887 r.rdr.proxy_port[1] =
4888 ntohs($9->rport.b);
4889 if (!r.rdr.proxy_port[0] &&
4890 !r.rdr.proxy_port[1]) {
4891 r.rdr.proxy_port[0] =
4892 PF_NAT_PROXY_PORT_LOW;
4893 r.rdr.proxy_port[1] =
4894 PF_NAT_PROXY_PORT_HIGH;
4895 } else if (!r.rdr.proxy_port[1])
4896 r.rdr.proxy_port[1] =
4897 r.rdr.proxy_port[0];
4898 break;
4899 default:
4900 break;
4901 }
4902
4903 r.rdr.opts = $10.type;
4904 if ((r.rdr.opts & PF_POOL_TYPEMASK) ==
4905 PF_POOL_NONE && ($9->host->next != NULL ||
4906 $9->host->addr.type == PF_ADDR_TABLE ||
4907 DYNIF_MULTIADDR($9->host->addr)))
4908 r.rdr.opts = PF_POOL_ROUNDROBIN;
4909 if ((r.rdr.opts & PF_POOL_TYPEMASK) !=
4910 PF_POOL_ROUNDROBIN &&
4911 disallow_table($9->host, "tables are only "
4912 "supported in round-robin redirection "
4913 "pools"))
4914 YYERROR;
4915 if ((r.rdr.opts & PF_POOL_TYPEMASK) !=
4916 PF_POOL_ROUNDROBIN &&
4917 disallow_alias($9->host, "interface (%s) "
4918 "is only supported in round-robin "
4919 "redirection pools"))
4920 YYERROR;
4921 if ($9->host->next != NULL) {
4922 if ((r.rdr.opts & PF_POOL_TYPEMASK) !=
4923 PF_POOL_ROUNDROBIN) {
4924 yyerror("only round-robin "
4925 "valid for multiple "
4926 "redirection addresses");
4927 YYERROR;
4928 }
4929 }
4930 }
4931
4932 if ($10.key != NULL)
4933 memcpy(&r.rdr.key, $10.key,
4934 sizeof(struct pf_poolhashkey));
4935
4936 if ($10.opts)
4937 r.rdr.opts |= $10.opts;
4938
4939 if ($10.staticport) {
4940 if (r.action != PF_NAT) {
4941 yyerror("the 'static-port' option is "
4942 "only valid with nat rules");
4943 YYERROR;
4944 }
4945 if (r.rdr.proxy_port[0] !=
4946 PF_NAT_PROXY_PORT_LOW &&
4947 r.rdr.proxy_port[1] !=
4948 PF_NAT_PROXY_PORT_HIGH) {
4949 yyerror("the 'static-port' option can't"
4950 " be used when specifying a port"
4951 " range");
4952 YYERROR;
4953 }
4954 r.rdr.proxy_port[0] = 0;
4955 r.rdr.proxy_port[1] = 0;
4956 }
4957
4958 if ($10.mape.offset) {
4959 if (r.action != PF_NAT) {
4960 yyerror("the 'map-e-portset' option is"
4961 " only valid with nat rules");
4962 YYERROR;
4963 }
4964 if ($10.staticport) {
4965 yyerror("the 'map-e-portset' option"
4966 " can't be used 'static-port'");
4967 YYERROR;
4968 }
4969 if (r.rdr.proxy_port[0] !=
4970 PF_NAT_PROXY_PORT_LOW &&
4971 r.rdr.proxy_port[1] !=
4972 PF_NAT_PROXY_PORT_HIGH) {
4973 yyerror("the 'map-e-portset' option"
4974 " can't be used when specifying"
4975 " a port range");
4976 YYERROR;
4977 }
4978 r.rdr.mape = $10.mape;
4979 }
4980
4981 o = keep_state_defaults;
4982 while (o) {
4983 switch (o->type) {
4984 case PF_STATE_OPT_PFLOW:
4985 if (r.rule_flag & PFRULE_PFLOW) {
4986 yyerror("state pflow option: "
4987 "multiple definitions");
4988 YYERROR;
4989 }
4990 r.rule_flag |= PFRULE_PFLOW;
4991 break;
4992 }
4993 o = o->next;
4994 }
4995
4996 expand_rule(&r, $2, NULL, NULL, NULL,
4997 $9 == NULL ? NULL : $9->host, NULL, NULL, $4,
4998 $5.src_os, $5.src.host, $5.src.port, $5.dst.host,
4999 $5.dst.port, 0, 0, 0, 0, "");
5000 free($9);
5001 }
5002 ;
5003
5004 binatrule : no BINAT natpasslog interface af proto FROM ipspec toipspec tag
5005 tagged rtable redirection
5006 {
5007 struct pfctl_rule binat;
5008 struct pf_pooladdr *pa;
5009
5010 if (check_rulestate(PFCTL_STATE_NAT))
5011 YYERROR;
5012 if (disallow_urpf_failed($9, "\"urpf-failed\" is not "
5013 "permitted as a binat destination"))
5014 YYERROR;
5015
5016 memset(&binat, 0, sizeof(binat));
5017
5018 if ($1 && $3.b1) {
5019 yyerror("\"pass\" not valid with \"no\"");
5020 YYERROR;
5021 }
5022 if ($1)
5023 binat.action = PF_NOBINAT;
5024 else
5025 binat.action = PF_BINAT;
5026 binat.natpass = $3.b1;
5027 binat.log = $3.b2;
5028 binat.logif = $3.w2;
5029 binat.af = $5;
5030 if (!binat.af && $8 != NULL && $8->af)
5031 binat.af = $8->af;
5032 if (!binat.af && $9 != NULL && $9->af)
5033 binat.af = $9->af;
5034
5035 if (!binat.af && $13 != NULL && $13->host)
5036 binat.af = $13->host->af;
5037 if (!binat.af) {
5038 yyerror("address family (inet/inet6) "
5039 "undefined");
5040 YYERROR;
5041 }
5042
5043 if ($4 != NULL) {
5044 memcpy(binat.ifname, $4->ifname,
5045 sizeof(binat.ifname));
5046 binat.ifnot = $4->not;
5047 free($4);
5048 }
5049
5050 if ($10 != NULL)
5051 if (strlcpy(binat.tagname, $10,
5052 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
5053 yyerror("tag too long, max %u chars",
5054 PF_TAG_NAME_SIZE - 1);
5055 YYERROR;
5056 }
5057 if ($11.name)
5058 if (strlcpy(binat.match_tagname, $11.name,
5059 PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
5060 yyerror("tag too long, max %u chars",
5061 PF_TAG_NAME_SIZE - 1);
5062 YYERROR;
5063 }
5064 binat.match_tag_not = $11.neg;
5065 binat.rtableid = $12;
5066
5067 if ($6 != NULL) {
5068 binat.proto = $6->proto;
5069 free($6);
5070 }
5071
5072 if ($8 != NULL && disallow_table($8, "invalid use of "
5073 "table <%s> as the source address of a binat rule"))
5074 YYERROR;
5075 if ($8 != NULL && disallow_alias($8, "invalid use of "
5076 "interface (%s) as the source address of a binat "
5077 "rule"))
5078 YYERROR;
5079 if ($13 != NULL && $13->host != NULL && disallow_table(
5080 $13->host, "invalid use of table <%s> as the "
5081 "redirect address of a binat rule"))
5082 YYERROR;
5083 if ($13 != NULL && $13->host != NULL && disallow_alias(
5084 $13->host, "invalid use of interface (%s) as the "
5085 "redirect address of a binat rule"))
5086 YYERROR;
5087
5088 if ($8 != NULL) {
5089 if ($8->next) {
5090 yyerror("multiple binat ip addresses");
5091 YYERROR;
5092 }
5093 if ($8->addr.type == PF_ADDR_DYNIFTL)
5094 $8->af = binat.af;
5095 if ($8->af != binat.af) {
5096 yyerror("binat ip versions must match");
5097 YYERROR;
5098 }
5099 if ($8->addr.type == PF_ADDR_DYNIFTL) {
5100 if (($8 = gen_dynnode($8, binat.af)) == NULL)
5101 err(1, "calloc");
5102 }
5103 if (check_netmask($8, binat.af))
5104 YYERROR;
5105 memcpy(&binat.src.addr, &$8->addr,
5106 sizeof(binat.src.addr));
5107 free($8);
5108 }
5109 if ($9 != NULL) {
5110 if ($9->next) {
5111 yyerror("multiple binat ip addresses");
5112 YYERROR;
5113 }
5114 if ($9->af != binat.af && $9->af) {
5115 yyerror("binat ip versions must match");
5116 YYERROR;
5117 }
5118 if ($9->addr.type == PF_ADDR_DYNIFTL) {
5119 if (($9 = gen_dynnode($9, binat.af)) == NULL)
5120 err(1, "calloc");
5121 }
5122 if (check_netmask($9, binat.af))
5123 YYERROR;
5124 memcpy(&binat.dst.addr, &$9->addr,
5125 sizeof(binat.dst.addr));
5126 binat.dst.neg = $9->not;
5127 free($9);
5128 }
5129
5130 if (binat.action == PF_NOBINAT) {
5131 if ($13 != NULL) {
5132 yyerror("'no binat' rule does not need"
5133 " '->'");
5134 YYERROR;
5135 }
5136 } else {
5137 if ($13 == NULL || $13->host == NULL) {
5138 yyerror("'binat' rule requires"
5139 " '-> address'");
5140 YYERROR;
5141 }
5142
5143 remove_invalid_hosts(&$13->host, &binat.af);
5144 if (invalid_redirect($13->host, binat.af))
5145 YYERROR;
5146 if ($13->host->next != NULL) {
5147 yyerror("binat rule must redirect to "
5148 "a single address");
5149 YYERROR;
5150 }
5151 if ($13->host->addr.type == PF_ADDR_DYNIFTL) {
5152 if (($13->host = gen_dynnode($13->host, binat.af)) == NULL)
5153 err(1, "calloc");
5154 }
5155 if (check_netmask($13->host, binat.af))
5156 YYERROR;
5157
5158 if (!PF_AZERO(&binat.src.addr.v.a.mask,
5159 binat.af) &&
5160 !PF_AEQ(&binat.src.addr.v.a.mask,
5161 &$13->host->addr.v.a.mask, binat.af)) {
5162 yyerror("'binat' source mask and "
5163 "redirect mask must be the same");
5164 YYERROR;
5165 }
5166
5167 TAILQ_INIT(&binat.rdr.list);
5168 TAILQ_INIT(&binat.nat.list);
5169 pa = calloc(1, sizeof(struct pf_pooladdr));
5170 if (pa == NULL)
5171 err(1, "binat: calloc");
5172 pa->addr = $13->host->addr;
5173 pa->ifname[0] = 0;
5174 TAILQ_INSERT_TAIL(&binat.rdr.list,
5175 pa, entries);
5176
5177 free($13);
5178 }
5179
5180 pfctl_append_rule(pf, &binat, "");
5181 }
5182 ;
5183
5184 tag : /* empty */ { $$ = NULL; }
5185 | TAG STRING { $$ = $2; }
5186 ;
5187
5188 tagged : /* empty */ { $$.neg = 0; $$.name = NULL; }
5189 | not TAGGED string { $$.neg = $1; $$.name = $3; }
5190 ;
5191
5192 rtable : /* empty */ { $$ = -1; }
5193 | RTABLE NUMBER {
5194 if ($2 < 0 || $2 > rt_tableid_max()) {
5195 yyerror("invalid rtable id");
5196 YYERROR;
5197 }
5198 $$ = $2;
5199 }
5200 ;
5201
5202 route_host : STRING {
5203 $$ = calloc(1, sizeof(struct node_host));
5204 if ($$ == NULL)
5205 err(1, "route_host: calloc");
5206 if (strlen($1) >= IFNAMSIZ) {
5207 yyerror("interface name too long");
5208 YYERROR;
5209 }
5210 $$->ifname = strdup($1);
5211 set_ipmask($$, 128);
5212 $$->next = NULL;
5213 $$->tail = $$;
5214 }
5215 | '(' STRING host ')' {
5216 struct node_host *n;
5217
5218 $$ = $3;
5219 for (n = $3; n != NULL; n = n->next) {
5220 if (strlen($2) >= IFNAMSIZ) {
5221 yyerror("interface name too long");
5222 YYERROR;
5223 }
5224 n->ifname = strdup($2);
5225 }
5226 }
5227 ;
5228
5229 route_host_list : route_host optnl { $$ = $1; }
5230 | route_host_list comma route_host optnl {
5231 if ($1->af == 0)
5232 $1->af = $3->af;
5233 if ($1->af != $3->af) {
5234 yyerror("all pool addresses must be in the "
5235 "same address family");
5236 YYERROR;
5237 }
5238 $1->tail->next = $3;
5239 $1->tail = $3->tail;
5240 $$ = $1;
5241 }
5242 ;
5243
5244 routespec : route_host { $$ = $1; }
5245 | '{' optnl route_host_list '}' { $$ = $3; }
5246 ;
5247
5248 route : /* empty */ {
5249 $$.host = NULL;
5250 $$.rt = 0;
5251 $$.pool_opts = 0;
5252 }
5253 | FASTROUTE {
5254 /* backwards-compat */
5255 $$.host = NULL;
5256 $$.rt = 0;
5257 $$.pool_opts = 0;
5258 }
5259 | ROUTETO routespec pool_opts {
5260 $$.host = $2;
5261 $$.rt = PF_ROUTETO;
5262 $$.pool_opts = $3.type | $3.opts;
5263 if ($3.key != NULL)
5264 $$.key = $3.key;
5265 }
5266 | REPLYTO routespec pool_opts {
5267 $$.host = $2;
5268 $$.rt = PF_REPLYTO;
5269 $$.pool_opts = $3.type | $3.opts;
5270 if ($3.key != NULL)
5271 $$.key = $3.key;
5272 }
5273 | DUPTO routespec pool_opts {
5274 $$.host = $2;
5275 $$.rt = PF_DUPTO;
5276 $$.pool_opts = $3.type | $3.opts;
5277 if ($3.key != NULL)
5278 $$.key = $3.key;
5279 }
5280 ;
5281
5282 timeout_spec : STRING NUMBER
5283 {
5284 if (check_rulestate(PFCTL_STATE_OPTION)) {
5285 free($1);
5286 YYERROR;
5287 }
5288 if ($2 < 0 || $2 > UINT_MAX) {
5289 yyerror("only positive values permitted");
5290 YYERROR;
5291 }
5292 if (pfctl_apply_timeout(pf, $1, $2, 0) != 0) {
5293 yyerror("unknown timeout %s", $1);
5294 free($1);
5295 YYERROR;
5296 }
5297 free($1);
5298 }
5299 | INTERVAL NUMBER {
5300 if (check_rulestate(PFCTL_STATE_OPTION))
5301 YYERROR;
5302 if ($2 < 0 || $2 > UINT_MAX) {
5303 yyerror("only positive values permitted");
5304 YYERROR;
5305 }
5306 if (pfctl_apply_timeout(pf, "interval", $2, 0) != 0)
5307 YYERROR;
5308 }
5309 ;
5310
5311 timeout_list : timeout_list comma timeout_spec optnl
5312 | timeout_spec optnl
5313 ;
5314
5315 limit_spec : STRING NUMBER
5316 {
5317 if (check_rulestate(PFCTL_STATE_OPTION)) {
5318 free($1);
5319 YYERROR;
5320 }
5321 if ($2 < 0 || $2 > UINT_MAX) {
5322 yyerror("only positive values permitted");
5323 YYERROR;
5324 }
5325 if (pfctl_apply_limit(pf, $1, $2) != 0) {
5326 yyerror("unable to set limit %s %u", $1, $2);
5327 free($1);
5328 YYERROR;
5329 }
5330 free($1);
5331 }
5332 ;
5333
5334 limit_list : limit_list comma limit_spec optnl
5335 | limit_spec optnl
5336 ;
5337
5338 comma : ','
5339 | /* empty */
5340 ;
5341
5342 yesno : NO { $$ = 0; }
5343 | STRING {
5344 if (!strcmp($1, "yes"))
5345 $$ = 1;
5346 else {
5347 yyerror("invalid value '%s', expected 'yes' "
5348 "or 'no'", $1);
5349 free($1);
5350 YYERROR;
5351 }
5352 free($1);
5353 }
5354 ;
5355
5356 unaryop : '=' { $$ = PF_OP_EQ; }
5357 | NE { $$ = PF_OP_NE; }
5358 | LE { $$ = PF_OP_LE; }
5359 | '<' { $$ = PF_OP_LT; }
5360 | GE { $$ = PF_OP_GE; }
5361 | '>' { $$ = PF_OP_GT; }
5362 ;
5363
5364 %%
5365
5366 int
5367 yyerror(const char *fmt, ...)
5368 {
5369 va_list ap;
5370
5371 file->errors++;
5372 va_start(ap, fmt);
5373 fprintf(stderr, "%s:%d: ", file->name, yylval.lineno);
5374 vfprintf(stderr, fmt, ap);
5375 fprintf(stderr, "\n");
5376 va_end(ap);
5377 return (0);
5378 }
5379
5380 int
disallow_table(struct node_host * h,const char * fmt)5381 disallow_table(struct node_host *h, const char *fmt)
5382 {
5383 for (; h != NULL; h = h->next)
5384 if (h->addr.type == PF_ADDR_TABLE) {
5385 yyerror(fmt, h->addr.v.tblname);
5386 return (1);
5387 }
5388 return (0);
5389 }
5390
5391 int
disallow_urpf_failed(struct node_host * h,const char * fmt)5392 disallow_urpf_failed(struct node_host *h, const char *fmt)
5393 {
5394 for (; h != NULL; h = h->next)
5395 if (h->addr.type == PF_ADDR_URPFFAILED) {
5396 yyerror(fmt);
5397 return (1);
5398 }
5399 return (0);
5400 }
5401
5402 int
disallow_alias(struct node_host * h,const char * fmt)5403 disallow_alias(struct node_host *h, const char *fmt)
5404 {
5405 for (; h != NULL; h = h->next)
5406 if (DYNIF_MULTIADDR(h->addr)) {
5407 yyerror(fmt, h->addr.v.tblname);
5408 return (1);
5409 }
5410 return (0);
5411 }
5412
5413 int
rule_consistent(struct pfctl_rule * r,int anchor_call)5414 rule_consistent(struct pfctl_rule *r, int anchor_call)
5415 {
5416 int problems = 0;
5417
5418 switch (r->action) {
5419 case PF_PASS:
5420 case PF_MATCH:
5421 case PF_DROP:
5422 case PF_SCRUB:
5423 case PF_NOSCRUB:
5424 problems = filter_consistent(r, anchor_call);
5425 break;
5426 case PF_NAT:
5427 case PF_NONAT:
5428 problems = nat_consistent(r);
5429 break;
5430 case PF_RDR:
5431 case PF_NORDR:
5432 problems = rdr_consistent(r);
5433 break;
5434 case PF_BINAT:
5435 case PF_NOBINAT:
5436 default:
5437 break;
5438 }
5439 return (problems);
5440 }
5441
5442 int
filter_consistent(struct pfctl_rule * r,int anchor_call)5443 filter_consistent(struct pfctl_rule *r, int anchor_call)
5444 {
5445 int problems = 0;
5446
5447 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP &&
5448 r->proto != IPPROTO_SCTP &&
5449 (r->src.port_op || r->dst.port_op)) {
5450 yyerror("port only applies to tcp/udp/sctp");
5451 problems++;
5452 }
5453 if (r->proto != IPPROTO_ICMP && r->proto != IPPROTO_ICMPV6 &&
5454 (r->type || r->code)) {
5455 yyerror("icmp-type/code only applies to icmp");
5456 problems++;
5457 }
5458 if (!r->af && (r->type || r->code)) {
5459 yyerror("must indicate address family with icmp-type/code");
5460 problems++;
5461 }
5462 if (r->rule_flag & PFRULE_AFTO && r->af == r->naf) {
5463 yyerror("must indicate different address family with af-to");
5464 problems++;
5465 }
5466 if (r->overload_tblname[0] &&
5467 r->max_src_conn == 0 && r->max_src_conn_rate.seconds == 0) {
5468 yyerror("'overload' requires 'max-src-conn' "
5469 "or 'max-src-conn-rate'");
5470 problems++;
5471 }
5472 if ((r->proto == IPPROTO_ICMP && r->af == AF_INET6) ||
5473 (r->proto == IPPROTO_ICMPV6 && r->af == AF_INET)) {
5474 yyerror("proto %s doesn't match address family %s",
5475 r->proto == IPPROTO_ICMP ? "icmp" : "icmp6",
5476 r->af == AF_INET ? "inet" : "inet6");
5477 problems++;
5478 }
5479 if (r->allow_opts && r->action != PF_PASS) {
5480 yyerror("allow-opts can only be specified for pass rules");
5481 problems++;
5482 }
5483 if (r->rule_flag & PFRULE_FRAGMENT && (r->src.port_op ||
5484 r->dst.port_op || r->flagset || r->type || r->code)) {
5485 yyerror("fragments can be filtered only on IP header fields");
5486 problems++;
5487 }
5488 if (r->rule_flag & PFRULE_RETURNRST && r->proto != IPPROTO_TCP) {
5489 yyerror("return-rst can only be applied to TCP rules");
5490 problems++;
5491 }
5492 if (r->max_src_nodes && !(r->rule_flag & PFRULE_RULESRCTRACK)) {
5493 yyerror("max-src-nodes requires 'source-track rule'");
5494 problems++;
5495 }
5496 if (r->action != PF_PASS && r->keep_state) {
5497 yyerror("keep state is great, but only for pass rules");
5498 problems++;
5499 }
5500 if (r->rule_flag & PFRULE_STATESLOPPY &&
5501 (r->keep_state == PF_STATE_MODULATE ||
5502 r->keep_state == PF_STATE_SYNPROXY)) {
5503 yyerror("sloppy state matching cannot be used with "
5504 "synproxy state or modulate state");
5505 problems++;
5506 }
5507 if (r->rule_flag & PFRULE_AFTO && r->rt) {
5508 if (r->rt != PF_ROUTETO && r->rt != PF_REPLYTO) {
5509 yyerror("dup-to "
5510 "must not be used on af-to rules");
5511 problems++;
5512 }
5513 }
5514 /* match rules rules */
5515 if (r->action == PF_MATCH) {
5516 if (r->divert.port) {
5517 yyerror("divert is not supported on match rules");
5518 problems++;
5519 }
5520 if (r->rt) {
5521 yyerror("route-to, reply-to, dup-to and fastroute "
5522 "must not be used on match rules");
5523 problems++;
5524 }
5525 if (r->rule_flag & PFRULE_AFTO) {
5526 yyerror("af-to is not supported on match rules");
5527 problems++;
5528 }
5529 }
5530 if (r->rdr.opts & PF_POOL_STICKYADDR && !r->keep_state) {
5531 yyerror("'sticky-address' requires 'keep state'");
5532 problems++;
5533 }
5534 return (-problems);
5535 }
5536
5537 int
nat_consistent(struct pfctl_rule * r)5538 nat_consistent(struct pfctl_rule *r)
5539 {
5540 return (0); /* yeah! */
5541 }
5542
5543 int
rdr_consistent(struct pfctl_rule * r)5544 rdr_consistent(struct pfctl_rule *r)
5545 {
5546 int problems = 0;
5547
5548 if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP &&
5549 r->proto != IPPROTO_SCTP) {
5550 if (r->src.port_op) {
5551 yyerror("src port only applies to tcp/udp/sctp");
5552 problems++;
5553 }
5554 if (r->dst.port_op) {
5555 yyerror("dst port only applies to tcp/udp/sctp");
5556 problems++;
5557 }
5558 if (r->rdr.proxy_port[0]) {
5559 yyerror("rdr port only applies to tcp/udp/sctp");
5560 problems++;
5561 }
5562 }
5563 if (r->dst.port_op &&
5564 r->dst.port_op != PF_OP_EQ && r->dst.port_op != PF_OP_RRG) {
5565 yyerror("invalid port operator for rdr destination port");
5566 problems++;
5567 }
5568 return (-problems);
5569 }
5570
5571 int
process_tabledef(char * name,struct table_opts * opts)5572 process_tabledef(char *name, struct table_opts *opts)
5573 {
5574 struct pfr_buffer ab;
5575 struct node_tinit *ti;
5576 unsigned long maxcount;
5577 size_t s = sizeof(maxcount);
5578
5579 bzero(&ab, sizeof(ab));
5580 ab.pfrb_type = PFRB_ADDRS;
5581 SIMPLEQ_FOREACH(ti, &opts->init_nodes, entries) {
5582 if (ti->file)
5583 if (pfr_buf_load(&ab, ti->file, 0, append_addr)) {
5584 if (errno)
5585 yyerror("cannot load \"%s\": %s",
5586 ti->file, strerror(errno));
5587 else
5588 yyerror("file \"%s\" contains bad data",
5589 ti->file);
5590 goto _error;
5591 }
5592 if (ti->host)
5593 if (append_addr_host(&ab, ti->host, 0, 0)) {
5594 yyerror("cannot create address buffer: %s",
5595 strerror(errno));
5596 goto _error;
5597 }
5598 }
5599 if (pf->opts & PF_OPT_VERBOSE)
5600 print_tabledef(name, opts->flags, opts->init_addr,
5601 &opts->init_nodes);
5602 if (!(pf->opts & PF_OPT_NOACTION) &&
5603 pfctl_define_table(name, opts->flags, opts->init_addr,
5604 pf->anchor->name, &ab, pf->anchor->ruleset.tticket)) {
5605
5606 if (sysctlbyname("net.pf.request_maxcount", &maxcount, &s,
5607 NULL, 0) == -1)
5608 maxcount = 65535;
5609
5610 if (ab.pfrb_size > maxcount)
5611 yyerror("cannot define table %s: too many elements.\n"
5612 "Consider increasing net.pf.request_maxcount.",
5613 name);
5614 else
5615 yyerror("cannot define table %s: %s", name,
5616 pfr_strerror(errno));
5617
5618 goto _error;
5619 }
5620 pf->tdirty = 1;
5621 pfr_buf_clear(&ab);
5622 return (0);
5623 _error:
5624 pfr_buf_clear(&ab);
5625 return (-1);
5626 }
5627
5628 struct keywords {
5629 const char *k_name;
5630 int k_val;
5631 };
5632
5633 /* macro gore, but you should've seen the prior indentation nightmare... */
5634
5635 #define FREE_LIST(T,r) \
5636 do { \
5637 T *p, *node = r; \
5638 while (node != NULL) { \
5639 p = node; \
5640 node = node->next; \
5641 free(p); \
5642 } \
5643 } while (0)
5644
5645 #define LOOP_THROUGH(T,n,r,C) \
5646 do { \
5647 T *n; \
5648 if (r == NULL) { \
5649 r = calloc(1, sizeof(T)); \
5650 if (r == NULL) \
5651 err(1, "LOOP: calloc"); \
5652 r->next = NULL; \
5653 } \
5654 n = r; \
5655 while (n != NULL) { \
5656 do { \
5657 C; \
5658 } while (0); \
5659 n = n->next; \
5660 } \
5661 } while (0)
5662
5663 void
expand_label_str(char * label,size_t len,const char * srch,const char * repl)5664 expand_label_str(char *label, size_t len, const char *srch, const char *repl)
5665 {
5666 char *tmp;
5667 char *p, *q;
5668
5669 if ((tmp = calloc(1, len)) == NULL)
5670 err(1, "expand_label_str: calloc");
5671 p = q = label;
5672 while ((q = strstr(p, srch)) != NULL) {
5673 *q = '\0';
5674 if ((strlcat(tmp, p, len) >= len) ||
5675 (strlcat(tmp, repl, len) >= len))
5676 errx(1, "expand_label: label too long");
5677 q += strlen(srch);
5678 p = q;
5679 }
5680 if (strlcat(tmp, p, len) >= len)
5681 errx(1, "expand_label: label too long");
5682 strlcpy(label, tmp, len); /* always fits */
5683 free(tmp);
5684 }
5685
5686 void
expand_label_if(const char * name,char * label,size_t len,const char * ifname)5687 expand_label_if(const char *name, char *label, size_t len, const char *ifname)
5688 {
5689 if (strstr(label, name) != NULL) {
5690 if (!*ifname)
5691 expand_label_str(label, len, name, "any");
5692 else
5693 expand_label_str(label, len, name, ifname);
5694 }
5695 }
5696
5697 void
expand_label_addr(const char * name,char * label,size_t len,sa_family_t af,struct pf_rule_addr * addr)5698 expand_label_addr(const char *name, char *label, size_t len, sa_family_t af,
5699 struct pf_rule_addr *addr)
5700 {
5701 char tmp[64], tmp_not[66];
5702
5703 if (strstr(label, name) != NULL) {
5704 switch (addr->addr.type) {
5705 case PF_ADDR_DYNIFTL:
5706 snprintf(tmp, sizeof(tmp), "(%s)", addr->addr.v.ifname);
5707 break;
5708 case PF_ADDR_TABLE:
5709 snprintf(tmp, sizeof(tmp), "<%s>", addr->addr.v.tblname);
5710 break;
5711 case PF_ADDR_NOROUTE:
5712 snprintf(tmp, sizeof(tmp), "no-route");
5713 break;
5714 case PF_ADDR_URPFFAILED:
5715 snprintf(tmp, sizeof(tmp), "urpf-failed");
5716 break;
5717 case PF_ADDR_ADDRMASK:
5718 if (!af || (PF_AZERO(&addr->addr.v.a.addr, af) &&
5719 PF_AZERO(&addr->addr.v.a.mask, af)))
5720 snprintf(tmp, sizeof(tmp), "any");
5721 else {
5722 char a[48];
5723 int bits;
5724
5725 if (inet_ntop(af, &addr->addr.v.a.addr, a,
5726 sizeof(a)) == NULL)
5727 snprintf(tmp, sizeof(tmp), "?");
5728 else {
5729 bits = unmask(&addr->addr.v.a.mask, af);
5730 if ((af == AF_INET && bits < 32) ||
5731 (af == AF_INET6 && bits < 128))
5732 snprintf(tmp, sizeof(tmp),
5733 "%s/%d", a, bits);
5734 else
5735 snprintf(tmp, sizeof(tmp),
5736 "%s", a);
5737 }
5738 }
5739 break;
5740 default:
5741 snprintf(tmp, sizeof(tmp), "?");
5742 break;
5743 }
5744
5745 if (addr->neg) {
5746 snprintf(tmp_not, sizeof(tmp_not), "! %s", tmp);
5747 expand_label_str(label, len, name, tmp_not);
5748 } else
5749 expand_label_str(label, len, name, tmp);
5750 }
5751 }
5752
5753 void
expand_label_port(const char * name,char * label,size_t len,struct pf_rule_addr * addr)5754 expand_label_port(const char *name, char *label, size_t len,
5755 struct pf_rule_addr *addr)
5756 {
5757 char a1[6], a2[6], op[13] = "";
5758
5759 if (strstr(label, name) != NULL) {
5760 snprintf(a1, sizeof(a1), "%u", ntohs(addr->port[0]));
5761 snprintf(a2, sizeof(a2), "%u", ntohs(addr->port[1]));
5762 if (!addr->port_op)
5763 ;
5764 else if (addr->port_op == PF_OP_IRG)
5765 snprintf(op, sizeof(op), "%s><%s", a1, a2);
5766 else if (addr->port_op == PF_OP_XRG)
5767 snprintf(op, sizeof(op), "%s<>%s", a1, a2);
5768 else if (addr->port_op == PF_OP_EQ)
5769 snprintf(op, sizeof(op), "%s", a1);
5770 else if (addr->port_op == PF_OP_NE)
5771 snprintf(op, sizeof(op), "!=%s", a1);
5772 else if (addr->port_op == PF_OP_LT)
5773 snprintf(op, sizeof(op), "<%s", a1);
5774 else if (addr->port_op == PF_OP_LE)
5775 snprintf(op, sizeof(op), "<=%s", a1);
5776 else if (addr->port_op == PF_OP_GT)
5777 snprintf(op, sizeof(op), ">%s", a1);
5778 else if (addr->port_op == PF_OP_GE)
5779 snprintf(op, sizeof(op), ">=%s", a1);
5780 expand_label_str(label, len, name, op);
5781 }
5782 }
5783
5784 void
expand_label_proto(const char * name,char * label,size_t len,u_int8_t proto)5785 expand_label_proto(const char *name, char *label, size_t len, u_int8_t proto)
5786 {
5787 const char *protoname;
5788 char n[4];
5789
5790 if (strstr(label, name) != NULL) {
5791 protoname = pfctl_proto2name(proto);
5792 if (protoname != NULL)
5793 expand_label_str(label, len, name, protoname);
5794 else {
5795 snprintf(n, sizeof(n), "%u", proto);
5796 expand_label_str(label, len, name, n);
5797 }
5798 }
5799 }
5800
5801 void
expand_label_nr(const char * name,char * label,size_t len,struct pfctl_rule * r)5802 expand_label_nr(const char *name, char *label, size_t len,
5803 struct pfctl_rule *r)
5804 {
5805 char n[11];
5806
5807 if (strstr(label, name) != NULL) {
5808 snprintf(n, sizeof(n), "%u", r->nr);
5809 expand_label_str(label, len, name, n);
5810 }
5811 }
5812
5813 void
expand_label(char * label,size_t len,struct pfctl_rule * r)5814 expand_label(char *label, size_t len, struct pfctl_rule *r)
5815 {
5816 expand_label_if("$if", label, len, r->ifname);
5817 expand_label_addr("$srcaddr", label, len, r->af, &r->src);
5818 expand_label_addr("$dstaddr", label, len, r->af, &r->dst);
5819 expand_label_port("$srcport", label, len, &r->src);
5820 expand_label_port("$dstport", label, len, &r->dst);
5821 expand_label_proto("$proto", label, len, r->proto);
5822 expand_label_nr("$nr", label, len, r);
5823 }
5824
5825 int
expand_altq(struct pf_altq * a,struct node_if * interfaces,struct node_queue * nqueues,struct node_queue_bw bwspec,struct node_queue_opt * opts)5826 expand_altq(struct pf_altq *a, struct node_if *interfaces,
5827 struct node_queue *nqueues, struct node_queue_bw bwspec,
5828 struct node_queue_opt *opts)
5829 {
5830 struct pf_altq pa, pb;
5831 char qname[PF_QNAME_SIZE];
5832 struct node_queue *n;
5833 struct node_queue_bw bw;
5834 int errs = 0;
5835
5836 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
5837 FREE_LIST(struct node_if, interfaces);
5838 if (nqueues)
5839 FREE_LIST(struct node_queue, nqueues);
5840 return (0);
5841 }
5842
5843 LOOP_THROUGH(struct node_if, interface, interfaces,
5844 memcpy(&pa, a, sizeof(struct pf_altq));
5845 if (strlcpy(pa.ifname, interface->ifname,
5846 sizeof(pa.ifname)) >= sizeof(pa.ifname))
5847 errx(1, "expand_altq: strlcpy");
5848
5849 if (interface->not) {
5850 yyerror("altq on ! <interface> is not supported");
5851 errs++;
5852 } else {
5853 if (eval_pfaltq(pf, &pa, &bwspec, opts))
5854 errs++;
5855 else
5856 if (pfctl_add_altq(pf, &pa))
5857 errs++;
5858
5859 if (pf->opts & PF_OPT_VERBOSE) {
5860 print_altq(&pf->paltq->altq, 0,
5861 &bwspec, opts);
5862 if (nqueues && nqueues->tail) {
5863 printf("queue { ");
5864 LOOP_THROUGH(struct node_queue, queue,
5865 nqueues,
5866 printf("%s ",
5867 queue->queue);
5868 );
5869 printf("}");
5870 }
5871 printf("\n");
5872 }
5873
5874 if (pa.scheduler == ALTQT_CBQ ||
5875 pa.scheduler == ALTQT_HFSC ||
5876 pa.scheduler == ALTQT_FAIRQ) {
5877 /* now create a root queue */
5878 memset(&pb, 0, sizeof(struct pf_altq));
5879 if (strlcpy(qname, "root_", sizeof(qname)) >=
5880 sizeof(qname))
5881 errx(1, "expand_altq: strlcpy");
5882 if (strlcat(qname, interface->ifname,
5883 sizeof(qname)) >= sizeof(qname))
5884 errx(1, "expand_altq: strlcat");
5885 if (strlcpy(pb.qname, qname,
5886 sizeof(pb.qname)) >= sizeof(pb.qname))
5887 errx(1, "expand_altq: strlcpy");
5888 if (strlcpy(pb.ifname, interface->ifname,
5889 sizeof(pb.ifname)) >= sizeof(pb.ifname))
5890 errx(1, "expand_altq: strlcpy");
5891 pb.qlimit = pa.qlimit;
5892 pb.scheduler = pa.scheduler;
5893 bw.bw_absolute = pa.ifbandwidth;
5894 bw.bw_percent = 0;
5895 if (eval_pfqueue(pf, &pb, &bw, opts))
5896 errs++;
5897 else
5898 if (pfctl_add_altq(pf, &pb))
5899 errs++;
5900 }
5901
5902 LOOP_THROUGH(struct node_queue, queue, nqueues,
5903 n = calloc(1, sizeof(struct node_queue));
5904 if (n == NULL)
5905 err(1, "expand_altq: calloc");
5906 if (pa.scheduler == ALTQT_CBQ ||
5907 pa.scheduler == ALTQT_HFSC ||
5908 pa.scheduler == ALTQT_FAIRQ)
5909 if (strlcpy(n->parent, qname,
5910 sizeof(n->parent)) >=
5911 sizeof(n->parent))
5912 errx(1, "expand_altq: strlcpy");
5913 if (strlcpy(n->queue, queue->queue,
5914 sizeof(n->queue)) >= sizeof(n->queue))
5915 errx(1, "expand_altq: strlcpy");
5916 if (strlcpy(n->ifname, interface->ifname,
5917 sizeof(n->ifname)) >= sizeof(n->ifname))
5918 errx(1, "expand_altq: strlcpy");
5919 n->scheduler = pa.scheduler;
5920 n->next = NULL;
5921 n->tail = n;
5922 if (queues == NULL)
5923 queues = n;
5924 else {
5925 queues->tail->next = n;
5926 queues->tail = n;
5927 }
5928 );
5929 }
5930 );
5931 FREE_LIST(struct node_if, interfaces);
5932 if (nqueues)
5933 FREE_LIST(struct node_queue, nqueues);
5934
5935 return (errs);
5936 }
5937
5938 int
expand_queue(struct pf_altq * a,struct node_if * interfaces,struct node_queue * nqueues,struct node_queue_bw bwspec,struct node_queue_opt * opts)5939 expand_queue(struct pf_altq *a, struct node_if *interfaces,
5940 struct node_queue *nqueues, struct node_queue_bw bwspec,
5941 struct node_queue_opt *opts)
5942 {
5943 struct node_queue *n, *nq;
5944 struct pf_altq pa;
5945 u_int8_t found = 0;
5946 u_int8_t errs = 0;
5947
5948 if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
5949 FREE_LIST(struct node_queue, nqueues);
5950 return (0);
5951 }
5952
5953 if (queues == NULL) {
5954 yyerror("queue %s has no parent", a->qname);
5955 FREE_LIST(struct node_queue, nqueues);
5956 return (1);
5957 }
5958
5959 LOOP_THROUGH(struct node_if, interface, interfaces,
5960 LOOP_THROUGH(struct node_queue, tqueue, queues,
5961 if (!strncmp(a->qname, tqueue->queue, PF_QNAME_SIZE) &&
5962 (interface->ifname[0] == 0 ||
5963 (!interface->not && !strncmp(interface->ifname,
5964 tqueue->ifname, IFNAMSIZ)) ||
5965 (interface->not && strncmp(interface->ifname,
5966 tqueue->ifname, IFNAMSIZ)))) {
5967 /* found ourself in queues */
5968 found++;
5969
5970 memcpy(&pa, a, sizeof(struct pf_altq));
5971
5972 if (pa.scheduler != ALTQT_NONE &&
5973 pa.scheduler != tqueue->scheduler) {
5974 yyerror("exactly one scheduler type "
5975 "per interface allowed");
5976 return (1);
5977 }
5978 pa.scheduler = tqueue->scheduler;
5979
5980 /* scheduler dependent error checking */
5981 switch (pa.scheduler) {
5982 case ALTQT_PRIQ:
5983 if (nqueues != NULL) {
5984 yyerror("priq queues cannot "
5985 "have child queues");
5986 return (1);
5987 }
5988 if (bwspec.bw_absolute > 0 ||
5989 bwspec.bw_percent < 100) {
5990 yyerror("priq doesn't take "
5991 "bandwidth");
5992 return (1);
5993 }
5994 break;
5995 default:
5996 break;
5997 }
5998
5999 if (strlcpy(pa.ifname, tqueue->ifname,
6000 sizeof(pa.ifname)) >= sizeof(pa.ifname))
6001 errx(1, "expand_queue: strlcpy");
6002 if (strlcpy(pa.parent, tqueue->parent,
6003 sizeof(pa.parent)) >= sizeof(pa.parent))
6004 errx(1, "expand_queue: strlcpy");
6005
6006 if (eval_pfqueue(pf, &pa, &bwspec, opts))
6007 errs++;
6008 else
6009 if (pfctl_add_altq(pf, &pa))
6010 errs++;
6011
6012 for (nq = nqueues; nq != NULL; nq = nq->next) {
6013 if (!strcmp(a->qname, nq->queue)) {
6014 yyerror("queue cannot have "
6015 "itself as child");
6016 errs++;
6017 continue;
6018 }
6019 n = calloc(1,
6020 sizeof(struct node_queue));
6021 if (n == NULL)
6022 err(1, "expand_queue: calloc");
6023 if (strlcpy(n->parent, a->qname,
6024 sizeof(n->parent)) >=
6025 sizeof(n->parent))
6026 errx(1, "expand_queue strlcpy");
6027 if (strlcpy(n->queue, nq->queue,
6028 sizeof(n->queue)) >=
6029 sizeof(n->queue))
6030 errx(1, "expand_queue strlcpy");
6031 if (strlcpy(n->ifname, tqueue->ifname,
6032 sizeof(n->ifname)) >=
6033 sizeof(n->ifname))
6034 errx(1, "expand_queue strlcpy");
6035 n->scheduler = tqueue->scheduler;
6036 n->next = NULL;
6037 n->tail = n;
6038 if (queues == NULL)
6039 queues = n;
6040 else {
6041 queues->tail->next = n;
6042 queues->tail = n;
6043 }
6044 }
6045 if ((pf->opts & PF_OPT_VERBOSE) && (
6046 (found == 1 && interface->ifname[0] == 0) ||
6047 (found > 0 && interface->ifname[0] != 0))) {
6048 print_queue(&pf->paltq->altq, 0,
6049 &bwspec, interface->ifname[0] != 0,
6050 opts);
6051 if (nqueues && nqueues->tail) {
6052 printf("{ ");
6053 LOOP_THROUGH(struct node_queue,
6054 queue, nqueues,
6055 printf("%s ",
6056 queue->queue);
6057 );
6058 printf("}");
6059 }
6060 printf("\n");
6061 }
6062 }
6063 );
6064 );
6065
6066 FREE_LIST(struct node_queue, nqueues);
6067 FREE_LIST(struct node_if, interfaces);
6068
6069 if (!found) {
6070 yyerror("queue %s has no parent", a->qname);
6071 errs++;
6072 }
6073
6074 if (errs)
6075 return (1);
6076 else
6077 return (0);
6078 }
6079
6080 static int
pf_af_to_proto(sa_family_t af)6081 pf_af_to_proto(sa_family_t af)
6082 {
6083 if (af == AF_INET)
6084 return (ETHERTYPE_IP);
6085 if (af == AF_INET6)
6086 return (ETHERTYPE_IPV6);
6087
6088 return (0);
6089 }
6090
6091 void
expand_eth_rule(struct pfctl_eth_rule * r,struct node_if * interfaces,struct node_etherproto * protos,struct node_mac * srcs,struct node_mac * dsts,struct node_host * ipsrcs,struct node_host * ipdsts,const char * bridge_to,const char * anchor_call)6092 expand_eth_rule(struct pfctl_eth_rule *r,
6093 struct node_if *interfaces, struct node_etherproto *protos,
6094 struct node_mac *srcs, struct node_mac *dsts,
6095 struct node_host *ipsrcs, struct node_host *ipdsts,
6096 const char *bridge_to, const char *anchor_call)
6097 {
6098 char tagname[PF_TAG_NAME_SIZE];
6099 char match_tagname[PF_TAG_NAME_SIZE];
6100 char qname[PF_QNAME_SIZE];
6101
6102 if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname))
6103 errx(1, "expand_eth_rule: tagname");
6104 if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >=
6105 sizeof(match_tagname))
6106 errx(1, "expand_eth_rule: match_tagname");
6107 if (strlcpy(qname, r->qname, sizeof(qname)) >= sizeof(qname))
6108 errx(1, "expand_eth_rule: qname");
6109
6110 LOOP_THROUGH(struct node_if, interface, interfaces,
6111 LOOP_THROUGH(struct node_etherproto, proto, protos,
6112 LOOP_THROUGH(struct node_mac, src, srcs,
6113 LOOP_THROUGH(struct node_mac, dst, dsts,
6114 LOOP_THROUGH(struct node_host, ipsrc, ipsrcs,
6115 LOOP_THROUGH(struct node_host, ipdst, ipdsts,
6116 strlcpy(r->ifname, interface->ifname,
6117 sizeof(r->ifname));
6118 r->ifnot = interface->not;
6119 r->proto = proto->proto;
6120 if (!r->proto && ipsrc->af)
6121 r->proto = pf_af_to_proto(ipsrc->af);
6122 else if (!r->proto && ipdst->af)
6123 r->proto = pf_af_to_proto(ipdst->af);
6124 bcopy(src->mac, r->src.addr, ETHER_ADDR_LEN);
6125 bcopy(src->mask, r->src.mask, ETHER_ADDR_LEN);
6126 r->src.neg = src->neg;
6127 r->src.isset = src->isset;
6128 r->ipsrc.addr = ipsrc->addr;
6129 r->ipsrc.neg = ipsrc->not;
6130 r->ipdst.addr = ipdst->addr;
6131 r->ipdst.neg = ipdst->not;
6132 bcopy(dst->mac, r->dst.addr, ETHER_ADDR_LEN);
6133 bcopy(dst->mask, r->dst.mask, ETHER_ADDR_LEN);
6134 r->dst.neg = dst->neg;
6135 r->dst.isset = dst->isset;
6136 r->nr = pf->eastack[pf->asd]->match++;
6137
6138 if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >=
6139 sizeof(r->tagname))
6140 errx(1, "expand_eth_rule: r->tagname");
6141 if (strlcpy(r->match_tagname, match_tagname,
6142 sizeof(r->match_tagname)) >= sizeof(r->match_tagname))
6143 errx(1, "expand_eth_rule: r->match_tagname");
6144 if (strlcpy(r->qname, qname, sizeof(r->qname)) >= sizeof(r->qname))
6145 errx(1, "expand_eth_rule: r->qname");
6146
6147 if (bridge_to)
6148 strlcpy(r->bridge_to, bridge_to, sizeof(r->bridge_to));
6149
6150 pfctl_append_eth_rule(pf, r, anchor_call);
6151 ))))));
6152
6153 FREE_LIST(struct node_if, interfaces);
6154 FREE_LIST(struct node_etherproto, protos);
6155 FREE_LIST(struct node_mac, srcs);
6156 FREE_LIST(struct node_mac, dsts);
6157 FREE_LIST(struct node_host, ipsrcs);
6158 FREE_LIST(struct node_host, ipdsts);
6159 }
6160
6161 void
expand_rule(struct pfctl_rule * r,struct node_if * interfaces,struct redirspec * nat,struct redirspec * rdr,struct redirspec * route,struct node_host * rdr_hosts,struct node_host * nat_hosts,struct node_host * route_hosts,struct node_proto * protos,struct node_os * src_oses,struct node_host * src_hosts,struct node_port * src_ports,struct node_host * dst_hosts,struct node_port * dst_ports,struct node_uid * uids,struct node_gid * gids,struct node_if * rcv,struct node_icmp * icmp_types,const char * anchor_call)6162 expand_rule(struct pfctl_rule *r,
6163 struct node_if *interfaces, struct redirspec *nat,
6164 struct redirspec *rdr, struct redirspec *route,
6165 struct node_host *rdr_hosts, struct node_host *nat_hosts,
6166 struct node_host *route_hosts, struct node_proto *protos,
6167 struct node_os *src_oses, struct node_host *src_hosts,
6168 struct node_port *src_ports, struct node_host *dst_hosts,
6169 struct node_port *dst_ports, struct node_uid *uids, struct node_gid *gids,
6170 struct node_if *rcv, struct node_icmp *icmp_types, const char *anchor_call)
6171 {
6172 sa_family_t af = r->af;
6173 int added = 0, error = 0;
6174 char ifname[IF_NAMESIZE];
6175 char label[PF_RULE_MAX_LABEL_COUNT][PF_RULE_LABEL_SIZE];
6176 char tagname[PF_TAG_NAME_SIZE];
6177 char match_tagname[PF_TAG_NAME_SIZE];
6178 struct pf_pooladdr *pa;
6179 struct node_host *h, *osrch, *odsth;
6180 u_int8_t flags, flagset, keep_state;
6181
6182 memcpy(label, r->label, sizeof(r->label));
6183 assert(sizeof(r->label) == sizeof(label));
6184 if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname))
6185 errx(1, "expand_rule: strlcpy");
6186 if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >=
6187 sizeof(match_tagname))
6188 errx(1, "expand_rule: strlcpy");
6189 flags = r->flags;
6190 flagset = r->flagset;
6191 keep_state = r->keep_state;
6192
6193 LOOP_THROUGH(struct node_if, interface, interfaces,
6194 LOOP_THROUGH(struct node_proto, proto, protos,
6195 LOOP_THROUGH(struct node_icmp, icmp_type, icmp_types,
6196 LOOP_THROUGH(struct node_host, src_host, src_hosts,
6197 LOOP_THROUGH(struct node_host, dst_host, dst_hosts,
6198 LOOP_THROUGH(struct node_port, src_port, src_ports,
6199 LOOP_THROUGH(struct node_port, dst_port, dst_ports,
6200 LOOP_THROUGH(struct node_os, src_os, src_oses,
6201 LOOP_THROUGH(struct node_uid, uid, uids,
6202 LOOP_THROUGH(struct node_gid, gid, gids,
6203
6204 r->af = af;
6205
6206 if (r->rule_flag & PFRULE_AFTO) {
6207 assert(nat != NULL);
6208 r->naf = nat->af;
6209 }
6210
6211 /* for link-local IPv6 address, interface must match up */
6212 if ((r->af && src_host->af && r->af != src_host->af) ||
6213 (r->af && dst_host->af && r->af != dst_host->af) ||
6214 (src_host->af && dst_host->af &&
6215 src_host->af != dst_host->af) ||
6216 (src_host->ifindex && dst_host->ifindex &&
6217 src_host->ifindex != dst_host->ifindex) ||
6218 (src_host->ifindex && *interface->ifname &&
6219 src_host->ifindex != if_nametoindex(interface->ifname)) ||
6220 (dst_host->ifindex && *interface->ifname &&
6221 dst_host->ifindex != if_nametoindex(interface->ifname)))
6222 continue;
6223 if (!r->af && src_host->af)
6224 r->af = src_host->af;
6225 else if (!r->af && dst_host->af)
6226 r->af = dst_host->af;
6227
6228 if (*interface->ifname)
6229 strlcpy(r->ifname, interface->ifname,
6230 sizeof(r->ifname));
6231 else if (if_indextoname(src_host->ifindex, ifname))
6232 strlcpy(r->ifname, ifname, sizeof(r->ifname));
6233 else if (if_indextoname(dst_host->ifindex, ifname))
6234 strlcpy(r->ifname, ifname, sizeof(r->ifname));
6235 else
6236 memset(r->ifname, '\0', sizeof(r->ifname));
6237
6238 memcpy(r->label, label, sizeof(r->label));
6239 if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >=
6240 sizeof(r->tagname))
6241 errx(1, "expand_rule: strlcpy");
6242 if (strlcpy(r->match_tagname, match_tagname,
6243 sizeof(r->match_tagname)) >= sizeof(r->match_tagname))
6244 errx(1, "expand_rule: strlcpy");
6245
6246 osrch = odsth = NULL;
6247 if (src_host->addr.type == PF_ADDR_DYNIFTL) {
6248 osrch = src_host;
6249 if ((src_host = gen_dynnode(src_host, r->af)) == NULL)
6250 err(1, "expand_rule: calloc");
6251 }
6252 if (dst_host->addr.type == PF_ADDR_DYNIFTL) {
6253 odsth = dst_host;
6254 if ((dst_host = gen_dynnode(dst_host, r->af)) == NULL)
6255 err(1, "expand_rule: calloc");
6256 }
6257
6258 error += check_netmask(src_host, r->af);
6259 error += check_netmask(dst_host, r->af);
6260
6261 r->ifnot = interface->not;
6262 r->proto = proto->proto;
6263 r->src.addr = src_host->addr;
6264 r->src.neg = src_host->not;
6265 r->src.port[0] = src_port->port[0];
6266 r->src.port[1] = src_port->port[1];
6267 r->src.port_op = src_port->op;
6268 r->dst.addr = dst_host->addr;
6269 r->dst.neg = dst_host->not;
6270 r->dst.port[0] = dst_port->port[0];
6271 r->dst.port[1] = dst_port->port[1];
6272 r->dst.port_op = dst_port->op;
6273 r->uid.op = uid->op;
6274 r->uid.uid[0] = uid->uid[0];
6275 r->uid.uid[1] = uid->uid[1];
6276 r->gid.op = gid->op;
6277 r->gid.gid[0] = gid->gid[0];
6278 r->gid.gid[1] = gid->gid[1];
6279 if (rcv) {
6280 strlcpy(r->rcv_ifname, rcv->ifname,
6281 sizeof(r->rcv_ifname));
6282 }
6283 r->type = icmp_type->type;
6284 r->code = icmp_type->code;
6285
6286 if ((keep_state == PF_STATE_MODULATE ||
6287 keep_state == PF_STATE_SYNPROXY) &&
6288 r->proto && r->proto != IPPROTO_TCP)
6289 r->keep_state = PF_STATE_NORMAL;
6290 else
6291 r->keep_state = keep_state;
6292
6293 if (r->proto && r->proto != IPPROTO_TCP) {
6294 r->flags = 0;
6295 r->flagset = 0;
6296 } else {
6297 r->flags = flags;
6298 r->flagset = flagset;
6299 }
6300 if (icmp_type->proto && r->proto != icmp_type->proto) {
6301 yyerror("icmp-type mismatch");
6302 error++;
6303 }
6304
6305 if (src_os && src_os->os) {
6306 r->os_fingerprint = pfctl_get_fingerprint(src_os->os);
6307 if ((pf->opts & PF_OPT_VERBOSE2) &&
6308 r->os_fingerprint == PF_OSFP_NOMATCH)
6309 fprintf(stderr,
6310 "warning: unknown '%s' OS fingerprint\n",
6311 src_os->os);
6312 } else {
6313 r->os_fingerprint = PF_OSFP_ANY;
6314 }
6315
6316 TAILQ_INIT(&r->rdr.list);
6317 for (h = rdr_hosts; h != NULL; h = h->next) {
6318 pa = calloc(1, sizeof(struct pf_pooladdr));
6319 if (pa == NULL)
6320 err(1, "expand_rule: calloc");
6321 pa->addr = h->addr;
6322 if (h->ifname != NULL) {
6323 if (strlcpy(pa->ifname, h->ifname,
6324 sizeof(pa->ifname)) >=
6325 sizeof(pa->ifname))
6326 errx(1, "expand_rule: strlcpy");
6327 } else
6328 pa->ifname[0] = 0;
6329 TAILQ_INSERT_TAIL(&r->rdr.list, pa, entries);
6330 }
6331 TAILQ_INIT(&r->nat.list);
6332 for (h = nat_hosts; h != NULL; h = h->next) {
6333 pa = calloc(1, sizeof(struct pf_pooladdr));
6334 if (pa == NULL)
6335 err(1, "expand_rule: calloc");
6336 pa->addr = h->addr;
6337 if (h->ifname != NULL) {
6338 if (strlcpy(pa->ifname, h->ifname,
6339 sizeof(pa->ifname)) >=
6340 sizeof(pa->ifname))
6341 errx(1, "expand_rule: strlcpy");
6342 } else
6343 pa->ifname[0] = 0;
6344 TAILQ_INSERT_TAIL(&r->nat.list, pa, entries);
6345 }
6346 TAILQ_INIT(&r->route.list);
6347 for (h = route_hosts; h != NULL; h = h->next) {
6348 pa = calloc(1, sizeof(struct pf_pooladdr));
6349 if (pa == NULL)
6350 err(1, "expand_rule: calloc");
6351 pa->addr = h->addr;
6352 if (h->ifname != NULL) {
6353 if (strlcpy(pa->ifname, h->ifname,
6354 sizeof(pa->ifname)) >=
6355 sizeof(pa->ifname))
6356 errx(1, "expand_rule: strlcpy");
6357 } else
6358 pa->ifname[0] = 0;
6359 TAILQ_INSERT_TAIL(&r->route.list, pa, entries);
6360 }
6361
6362 r->nat.proxy_port[0] = PF_NAT_PROXY_PORT_LOW;
6363 r->nat.proxy_port[1] = PF_NAT_PROXY_PORT_HIGH;
6364
6365 if (rule_consistent(r, anchor_call[0]) < 0 || error)
6366 yyerror("skipping rule due to errors");
6367 else {
6368 r->nr = pf->astack[pf->asd]->match++;
6369 pfctl_append_rule(pf, r, anchor_call);
6370 added++;
6371 }
6372
6373 if (osrch && src_host->addr.type == PF_ADDR_DYNIFTL) {
6374 free(src_host);
6375 src_host = osrch;
6376 }
6377 if (odsth && dst_host->addr.type == PF_ADDR_DYNIFTL) {
6378 free(dst_host);
6379 dst_host = odsth;
6380 }
6381
6382 ))))))))));
6383
6384 FREE_LIST(struct node_if, interfaces);
6385 FREE_LIST(struct node_proto, protos);
6386 FREE_LIST(struct node_host, src_hosts);
6387 FREE_LIST(struct node_port, src_ports);
6388 FREE_LIST(struct node_os, src_oses);
6389 FREE_LIST(struct node_host, dst_hosts);
6390 FREE_LIST(struct node_port, dst_ports);
6391 FREE_LIST(struct node_uid, uids);
6392 FREE_LIST(struct node_gid, gids);
6393 FREE_LIST(struct node_icmp, icmp_types);
6394 FREE_LIST(struct node_host, rdr_hosts);
6395 FREE_LIST(struct node_host, nat_hosts);
6396
6397 if (!added)
6398 yyerror("rule expands to no valid combination");
6399 }
6400
6401 int
expand_skip_interface(struct node_if * interfaces)6402 expand_skip_interface(struct node_if *interfaces)
6403 {
6404 int errs = 0;
6405
6406 if (!interfaces || (!interfaces->next && !interfaces->not &&
6407 !strcmp(interfaces->ifname, "none"))) {
6408 if (pf->opts & PF_OPT_VERBOSE)
6409 printf("set skip on none\n");
6410 errs = pfctl_set_interface_flags(pf, "", PFI_IFLAG_SKIP, 0);
6411 return (errs);
6412 }
6413
6414 if (pf->opts & PF_OPT_VERBOSE)
6415 printf("set skip on {");
6416 LOOP_THROUGH(struct node_if, interface, interfaces,
6417 if (pf->opts & PF_OPT_VERBOSE)
6418 printf(" %s", interface->ifname);
6419 if (interface->not) {
6420 yyerror("skip on ! <interface> is not supported");
6421 errs++;
6422 } else
6423 errs += pfctl_set_interface_flags(pf,
6424 interface->ifname, PFI_IFLAG_SKIP, 1);
6425 );
6426 if (pf->opts & PF_OPT_VERBOSE)
6427 printf(" }\n");
6428
6429 FREE_LIST(struct node_if, interfaces);
6430
6431 if (errs)
6432 return (1);
6433 else
6434 return (0);
6435 }
6436
6437 void
freehostlist(struct node_host * h)6438 freehostlist(struct node_host *h)
6439 {
6440 FREE_LIST(struct node_host, h);
6441 }
6442
6443 #undef FREE_LIST
6444 #undef LOOP_THROUGH
6445
6446 int
check_rulestate(int desired_state)6447 check_rulestate(int desired_state)
6448 {
6449 if (require_order && (rulestate > desired_state)) {
6450 yyerror("Rules must be in order: options, ethernet, "
6451 "normalization, queueing, translation, filtering");
6452 return (1);
6453 }
6454 rulestate = desired_state;
6455 return (0);
6456 }
6457
6458 int
kw_cmp(const void * k,const void * e)6459 kw_cmp(const void *k, const void *e)
6460 {
6461 return (strcmp(k, ((const struct keywords *)e)->k_name));
6462 }
6463
6464 int
lookup(char * s)6465 lookup(char *s)
6466 {
6467 /* this has to be sorted always */
6468 static const struct keywords keywords[] = {
6469 { "af-to", AFTO},
6470 { "all", ALL},
6471 { "allow-opts", ALLOWOPTS},
6472 { "allow-related", ALLOW_RELATED},
6473 { "altq", ALTQ},
6474 { "anchor", ANCHOR},
6475 { "antispoof", ANTISPOOF},
6476 { "any", ANY},
6477 { "bandwidth", BANDWIDTH},
6478 { "binat", BINAT},
6479 { "binat-anchor", BINATANCHOR},
6480 { "bitmask", BITMASK},
6481 { "block", BLOCK},
6482 { "block-policy", BLOCKPOLICY},
6483 { "bridge-to", BRIDGE_TO},
6484 { "buckets", BUCKETS},
6485 { "cbq", CBQ},
6486 { "code", CODE},
6487 { "codelq", CODEL},
6488 { "debug", DEBUG},
6489 { "divert-reply", DIVERTREPLY},
6490 { "divert-to", DIVERTTO},
6491 { "dnpipe", DNPIPE},
6492 { "dnqueue", DNQUEUE},
6493 { "drop", DROP},
6494 { "dup-to", DUPTO},
6495 { "endpoint-independent", ENDPI},
6496 { "ether", ETHER},
6497 { "fail-policy", FAILPOLICY},
6498 { "fairq", FAIRQ},
6499 { "fastroute", FASTROUTE},
6500 { "file", FILENAME},
6501 { "fingerprints", FINGERPRINTS},
6502 { "flags", FLAGS},
6503 { "floating", FLOATING},
6504 { "flush", FLUSH},
6505 { "for", FOR},
6506 { "fragment", FRAGMENT},
6507 { "from", FROM},
6508 { "global", GLOBAL},
6509 { "group", GROUP},
6510 { "hfsc", HFSC},
6511 { "hogs", HOGS},
6512 { "hostid", HOSTID},
6513 { "icmp-type", ICMPTYPE},
6514 { "icmp6-type", ICMP6TYPE},
6515 { "if-bound", IFBOUND},
6516 { "in", IN},
6517 { "include", INCLUDE},
6518 { "inet", INET},
6519 { "inet6", INET6},
6520 { "interval", INTERVAL},
6521 { "keep", KEEP},
6522 { "keepcounters", KEEPCOUNTERS},
6523 { "l3", L3},
6524 { "label", LABEL},
6525 { "limit", LIMIT},
6526 { "linkshare", LINKSHARE},
6527 { "load", LOAD},
6528 { "log", LOG},
6529 { "loginterface", LOGINTERFACE},
6530 { "map-e-portset", MAPEPORTSET},
6531 { "match", MATCH},
6532 { "matches", MATCHES},
6533 { "max", MAXIMUM},
6534 { "max-mss", MAXMSS},
6535 { "max-src-conn", MAXSRCCONN},
6536 { "max-src-conn-rate", MAXSRCCONNRATE},
6537 { "max-src-nodes", MAXSRCNODES},
6538 { "max-src-states", MAXSRCSTATES},
6539 { "min-ttl", MINTTL},
6540 { "modulate", MODULATE},
6541 { "nat", NAT},
6542 { "nat-anchor", NATANCHOR},
6543 { "no", NO},
6544 { "no-df", NODF},
6545 { "no-route", NOROUTE},
6546 { "no-sync", NOSYNC},
6547 { "on", ON},
6548 { "optimization", OPTIMIZATION},
6549 { "os", OS},
6550 { "out", OUT},
6551 { "overload", OVERLOAD},
6552 { "pass", PASS},
6553 { "pflow", PFLOW},
6554 { "port", PORT},
6555 { "prio", PRIO},
6556 { "priority", PRIORITY},
6557 { "priq", PRIQ},
6558 { "probability", PROBABILITY},
6559 { "proto", PROTO},
6560 { "qlimit", QLIMIT},
6561 { "queue", QUEUE},
6562 { "quick", QUICK},
6563 { "random", RANDOM},
6564 { "random-id", RANDOMID},
6565 { "rdr", RDR},
6566 { "rdr-anchor", RDRANCHOR},
6567 { "realtime", REALTIME},
6568 { "reassemble", REASSEMBLE},
6569 { "received-on", RECEIVEDON},
6570 { "reply-to", REPLYTO},
6571 { "require-order", REQUIREORDER},
6572 { "return", RETURN},
6573 { "return-icmp", RETURNICMP},
6574 { "return-icmp6", RETURNICMP6},
6575 { "return-rst", RETURNRST},
6576 { "ridentifier", RIDENTIFIER},
6577 { "round-robin", ROUNDROBIN},
6578 { "route", ROUTE},
6579 { "route-to", ROUTETO},
6580 { "rtable", RTABLE},
6581 { "rule", RULE},
6582 { "ruleset-optimization", RULESET_OPTIMIZATION},
6583 { "scrub", SCRUB},
6584 { "set", SET},
6585 { "set-tos", SETTOS},
6586 { "skip", SKIP},
6587 { "sloppy", SLOPPY},
6588 { "source-hash", SOURCEHASH},
6589 { "source-track", SOURCETRACK},
6590 { "state", STATE},
6591 { "state-defaults", STATEDEFAULTS},
6592 { "state-policy", STATEPOLICY},
6593 { "static-port", STATICPORT},
6594 { "sticky-address", STICKYADDRESS},
6595 { "syncookies", SYNCOOKIES},
6596 { "synproxy", SYNPROXY},
6597 { "table", TABLE},
6598 { "tag", TAG},
6599 { "tagged", TAGGED},
6600 { "target", TARGET},
6601 { "tbrsize", TBRSIZE},
6602 { "timeout", TIMEOUT},
6603 { "to", TO},
6604 { "tos", TOS},
6605 { "ttl", TTL},
6606 { "upperlimit", UPPERLIMIT},
6607 { "urpf-failed", URPFFAILED},
6608 { "user", USER},
6609 };
6610 const struct keywords *p;
6611
6612 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
6613 sizeof(keywords[0]), kw_cmp);
6614
6615 if (p) {
6616 if (debug > 1)
6617 fprintf(stderr, "%s: %d\n", s, p->k_val);
6618 return (p->k_val);
6619 } else {
6620 if (debug > 1)
6621 fprintf(stderr, "string: %s\n", s);
6622 return (STRING);
6623 }
6624 }
6625
6626 #define MAXPUSHBACK 128
6627
6628 static char *parsebuf;
6629 static int parseindex;
6630 static char pushback_buffer[MAXPUSHBACK];
6631 static int pushback_index = 0;
6632
6633 int
lgetc(int quotec)6634 lgetc(int quotec)
6635 {
6636 int c, next;
6637
6638 if (parsebuf) {
6639 /* Read character from the parsebuffer instead of input. */
6640 if (parseindex >= 0) {
6641 c = parsebuf[parseindex++];
6642 if (c != '\0')
6643 return (c);
6644 parsebuf = NULL;
6645 } else
6646 parseindex++;
6647 }
6648
6649 if (pushback_index)
6650 return (pushback_buffer[--pushback_index]);
6651
6652 if (quotec) {
6653 if ((c = getc(file->stream)) == EOF) {
6654 yyerror("reached end of file while parsing quoted string");
6655 if (popfile() == EOF)
6656 return (EOF);
6657 return (quotec);
6658 }
6659 return (c);
6660 }
6661
6662 while ((c = getc(file->stream)) == '\\') {
6663 next = getc(file->stream);
6664 if (next != '\n') {
6665 c = next;
6666 break;
6667 }
6668 yylval.lineno = file->lineno;
6669 file->lineno++;
6670 }
6671
6672 while (c == EOF) {
6673 if (popfile() == EOF)
6674 return (EOF);
6675 c = getc(file->stream);
6676 }
6677 return (c);
6678 }
6679
6680 int
lungetc(int c)6681 lungetc(int c)
6682 {
6683 if (c == EOF)
6684 return (EOF);
6685 if (parsebuf) {
6686 parseindex--;
6687 if (parseindex >= 0)
6688 return (c);
6689 }
6690 if (pushback_index < MAXPUSHBACK-1)
6691 return (pushback_buffer[pushback_index++] = c);
6692 else
6693 return (EOF);
6694 }
6695
6696 int
findeol(void)6697 findeol(void)
6698 {
6699 int c;
6700
6701 parsebuf = NULL;
6702
6703 /* skip to either EOF or the first real EOL */
6704 while (1) {
6705 if (pushback_index)
6706 c = pushback_buffer[--pushback_index];
6707 else
6708 c = lgetc(0);
6709 if (c == '\n') {
6710 file->lineno++;
6711 break;
6712 }
6713 if (c == EOF)
6714 break;
6715 }
6716 return (ERROR);
6717 }
6718
6719 int
yylex(void)6720 yylex(void)
6721 {
6722 char buf[8096];
6723 char *p, *val;
6724 int quotec, next, c;
6725 int token;
6726
6727 top:
6728 p = buf;
6729 while ((c = lgetc(0)) == ' ' || c == '\t')
6730 ; /* nothing */
6731
6732 yylval.lineno = file->lineno;
6733 if (c == '#')
6734 while ((c = lgetc(0)) != '\n' && c != EOF)
6735 ; /* nothing */
6736 if (c == '$' && parsebuf == NULL) {
6737 while (1) {
6738 if ((c = lgetc(0)) == EOF)
6739 return (0);
6740
6741 if (p + 1 >= buf + sizeof(buf) - 1) {
6742 yyerror("string too long");
6743 return (findeol());
6744 }
6745 if (isalnum(c) || c == '_') {
6746 *p++ = (char)c;
6747 continue;
6748 }
6749 *p = '\0';
6750 lungetc(c);
6751 break;
6752 }
6753 val = symget(buf);
6754 if (val == NULL) {
6755 yyerror("macro '%s' not defined", buf);
6756 return (findeol());
6757 }
6758 parsebuf = val;
6759 parseindex = 0;
6760 goto top;
6761 }
6762
6763 switch (c) {
6764 case '\'':
6765 case '"':
6766 quotec = c;
6767 while (1) {
6768 if ((c = lgetc(quotec)) == EOF)
6769 return (0);
6770 if (c == '\n') {
6771 file->lineno++;
6772 continue;
6773 } else if (c == '\\') {
6774 if ((next = lgetc(quotec)) == EOF)
6775 return (0);
6776 if (next == quotec || c == ' ' || c == '\t')
6777 c = next;
6778 else if (next == '\n') {
6779 file->lineno++;
6780 continue;
6781 }
6782 else
6783 lungetc(next);
6784 } else if (c == quotec) {
6785 *p = '\0';
6786 break;
6787 }
6788 if (p + 1 >= buf + sizeof(buf) - 1) {
6789 yyerror("string too long");
6790 return (findeol());
6791 }
6792 *p++ = (char)c;
6793 }
6794 yylval.v.string = strdup(buf);
6795 if (yylval.v.string == NULL)
6796 err(1, "yylex: strdup");
6797 return (STRING);
6798 case '!':
6799 next = lgetc(0);
6800 if (next == '=')
6801 return (NE);
6802 lungetc(next);
6803 break;
6804 case '<':
6805 next = lgetc(0);
6806 if (next == '>') {
6807 yylval.v.i = PF_OP_XRG;
6808 return (PORTBINARY);
6809 } else if (next == '=')
6810 return (LE);
6811 lungetc(next);
6812 break;
6813 case '>':
6814 next = lgetc(0);
6815 if (next == '<') {
6816 yylval.v.i = PF_OP_IRG;
6817 return (PORTBINARY);
6818 } else if (next == '=')
6819 return (GE);
6820 lungetc(next);
6821 break;
6822 case '-':
6823 next = lgetc(0);
6824 if (next == '>')
6825 return (ARROW);
6826 lungetc(next);
6827 break;
6828 }
6829
6830 #define allowed_to_end_number(x) \
6831 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
6832
6833 if (c == '-' || isdigit(c)) {
6834 do {
6835 *p++ = c;
6836 if ((unsigned)(p-buf) >= sizeof(buf)) {
6837 yyerror("string too long");
6838 return (findeol());
6839 }
6840 } while ((c = lgetc(0)) != EOF && isdigit(c));
6841 lungetc(c);
6842 if (p == buf + 1 && buf[0] == '-')
6843 goto nodigits;
6844 if (c == EOF || allowed_to_end_number(c)) {
6845 const char *errstr = NULL;
6846
6847 *p = '\0';
6848 yylval.v.number = strtonum(buf, LLONG_MIN,
6849 LLONG_MAX, &errstr);
6850 if (errstr) {
6851 yyerror("\"%s\" invalid number: %s",
6852 buf, errstr);
6853 return (findeol());
6854 }
6855 return (NUMBER);
6856 } else {
6857 nodigits:
6858 while (p > buf + 1)
6859 lungetc(*--p);
6860 c = *--p;
6861 if (c == '-')
6862 return (c);
6863 }
6864 }
6865
6866 #define allowed_in_string(x) \
6867 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
6868 x != '{' && x != '}' && x != '<' && x != '>' && \
6869 x != '!' && x != '=' && x != '/' && x != '#' && \
6870 x != ','))
6871
6872 if (isalnum(c) || c == ':' || c == '_') {
6873 do {
6874 *p++ = c;
6875 if ((unsigned)(p-buf) >= sizeof(buf)) {
6876 yyerror("string too long");
6877 return (findeol());
6878 }
6879 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
6880 lungetc(c);
6881 *p = '\0';
6882 if ((token = lookup(buf)) == STRING)
6883 if ((yylval.v.string = strdup(buf)) == NULL)
6884 err(1, "yylex: strdup");
6885 return (token);
6886 }
6887 if (c == '\n') {
6888 yylval.lineno = file->lineno;
6889 file->lineno++;
6890 }
6891 if (c == EOF)
6892 return (0);
6893 return (c);
6894 }
6895
6896 int
check_file_secrecy(int fd,const char * fname)6897 check_file_secrecy(int fd, const char *fname)
6898 {
6899 struct stat st;
6900
6901 if (fstat(fd, &st)) {
6902 warn("cannot stat %s", fname);
6903 return (-1);
6904 }
6905 if (st.st_uid != 0 && st.st_uid != getuid()) {
6906 warnx("%s: owner not root or current user", fname);
6907 return (-1);
6908 }
6909 if (st.st_mode & (S_IRWXG | S_IRWXO)) {
6910 warnx("%s: group/world readable/writeable", fname);
6911 return (-1);
6912 }
6913 return (0);
6914 }
6915
6916 struct file *
pushfile(const char * name,int secret)6917 pushfile(const char *name, int secret)
6918 {
6919 struct file *nfile;
6920
6921 if ((nfile = calloc(1, sizeof(struct file))) == NULL ||
6922 (nfile->name = strdup(name)) == NULL) {
6923 warn("malloc");
6924 return (NULL);
6925 }
6926 if (TAILQ_FIRST(&files) == NULL && strcmp(nfile->name, "-") == 0) {
6927 nfile->stream = stdin;
6928 free(nfile->name);
6929 if ((nfile->name = strdup("stdin")) == NULL) {
6930 warn("strdup");
6931 free(nfile);
6932 return (NULL);
6933 }
6934 } else if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
6935 warn("%s", nfile->name);
6936 free(nfile->name);
6937 free(nfile);
6938 return (NULL);
6939 } else if (secret &&
6940 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
6941 fclose(nfile->stream);
6942 free(nfile->name);
6943 free(nfile);
6944 return (NULL);
6945 }
6946 nfile->lineno = 1;
6947 TAILQ_INSERT_TAIL(&files, nfile, entry);
6948 return (nfile);
6949 }
6950
6951 int
popfile(void)6952 popfile(void)
6953 {
6954 struct file *prev;
6955
6956 if ((prev = TAILQ_PREV(file, files, entry)) != NULL) {
6957 prev->errors += file->errors;
6958 TAILQ_REMOVE(&files, file, entry);
6959 fclose(file->stream);
6960 free(file->name);
6961 free(file);
6962 file = prev;
6963 return (0);
6964 }
6965 return (EOF);
6966 }
6967
6968 int
parse_config(char * filename,struct pfctl * xpf)6969 parse_config(char *filename, struct pfctl *xpf)
6970 {
6971 int errors = 0;
6972 struct sym *sym;
6973
6974 pf = xpf;
6975 errors = 0;
6976 rulestate = PFCTL_STATE_NONE;
6977 returnicmpdefault = (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
6978 returnicmp6default =
6979 (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
6980 blockpolicy = PFRULE_DROP;
6981 failpolicy = PFRULE_DROP;
6982 require_order = 1;
6983
6984 if ((file = pushfile(filename, 0)) == NULL) {
6985 warn("cannot open the main config file!");
6986 return (-1);
6987 }
6988
6989 yyparse();
6990 errors = file->errors;
6991 popfile();
6992
6993 /* Free macros and check which have not been used. */
6994 while ((sym = TAILQ_FIRST(&symhead))) {
6995 if ((pf->opts & PF_OPT_VERBOSE2) && !sym->used)
6996 fprintf(stderr, "warning: macro '%s' not "
6997 "used\n", sym->nam);
6998 free(sym->nam);
6999 free(sym->val);
7000 TAILQ_REMOVE(&symhead, sym, entry);
7001 free(sym);
7002 }
7003
7004 return (errors ? -1 : 0);
7005 }
7006
7007 int
symset(const char * nam,const char * val,int persist)7008 symset(const char *nam, const char *val, int persist)
7009 {
7010 struct sym *sym;
7011
7012 for (sym = TAILQ_FIRST(&symhead); sym && strcmp(nam, sym->nam);
7013 sym = TAILQ_NEXT(sym, entry))
7014 ; /* nothing */
7015
7016 if (sym != NULL) {
7017 if (sym->persist == 1)
7018 return (0);
7019 else {
7020 free(sym->nam);
7021 free(sym->val);
7022 TAILQ_REMOVE(&symhead, sym, entry);
7023 free(sym);
7024 }
7025 }
7026 if ((sym = calloc(1, sizeof(*sym))) == NULL)
7027 return (-1);
7028
7029 sym->nam = strdup(nam);
7030 if (sym->nam == NULL) {
7031 free(sym);
7032 return (-1);
7033 }
7034 sym->val = strdup(val);
7035 if (sym->val == NULL) {
7036 free(sym->nam);
7037 free(sym);
7038 return (-1);
7039 }
7040 sym->used = 0;
7041 sym->persist = persist;
7042 TAILQ_INSERT_TAIL(&symhead, sym, entry);
7043 return (0);
7044 }
7045
7046 int
pfctl_cmdline_symset(char * s)7047 pfctl_cmdline_symset(char *s)
7048 {
7049 char *sym, *val;
7050 int ret;
7051
7052 if ((val = strrchr(s, '=')) == NULL)
7053 return (-1);
7054
7055 if ((sym = malloc(strlen(s) - strlen(val) + 1)) == NULL)
7056 err(1, "pfctl_cmdline_symset: malloc");
7057
7058 strlcpy(sym, s, strlen(s) - strlen(val) + 1);
7059
7060 ret = symset(sym, val + 1, 1);
7061 free(sym);
7062
7063 return (ret);
7064 }
7065
7066 char *
symget(const char * nam)7067 symget(const char *nam)
7068 {
7069 struct sym *sym;
7070
7071 TAILQ_FOREACH(sym, &symhead, entry)
7072 if (strcmp(nam, sym->nam) == 0) {
7073 sym->used = 1;
7074 return (sym->val);
7075 }
7076 return (NULL);
7077 }
7078
7079 void
mv_rules(struct pfctl_ruleset * src,struct pfctl_ruleset * dst)7080 mv_rules(struct pfctl_ruleset *src, struct pfctl_ruleset *dst)
7081 {
7082 int i;
7083 struct pfctl_rule *r;
7084
7085 for (i = 0; i < PF_RULESET_MAX; ++i) {
7086 while ((r = TAILQ_FIRST(src->rules[i].active.ptr))
7087 != NULL) {
7088 TAILQ_REMOVE(src->rules[i].active.ptr, r, entries);
7089 TAILQ_INSERT_TAIL(dst->rules[i].active.ptr, r, entries);
7090 dst->anchor->match++;
7091 }
7092 src->anchor->match = 0;
7093 while ((r = TAILQ_FIRST(src->rules[i].inactive.ptr))
7094 != NULL) {
7095 TAILQ_REMOVE(src->rules[i].inactive.ptr, r, entries);
7096 TAILQ_INSERT_TAIL(dst->rules[i].inactive.ptr,
7097 r, entries);
7098 }
7099 }
7100 }
7101
7102 void
mv_eth_rules(struct pfctl_eth_ruleset * src,struct pfctl_eth_ruleset * dst)7103 mv_eth_rules(struct pfctl_eth_ruleset *src, struct pfctl_eth_ruleset *dst)
7104 {
7105 struct pfctl_eth_rule *r;
7106
7107 while ((r = TAILQ_FIRST(&src->rules)) != NULL) {
7108 TAILQ_REMOVE(&src->rules, r, entries);
7109 TAILQ_INSERT_TAIL(&dst->rules, r, entries);
7110 dst->anchor->match++;
7111 }
7112 src->anchor->match = 0;
7113 }
7114
7115 void
decide_address_family(struct node_host * n,sa_family_t * af)7116 decide_address_family(struct node_host *n, sa_family_t *af)
7117 {
7118 if (*af != 0 || n == NULL)
7119 return;
7120 *af = n->af;
7121 while ((n = n->next) != NULL) {
7122 if (n->af != *af) {
7123 *af = 0;
7124 return;
7125 }
7126 }
7127 }
7128
7129 void
remove_invalid_hosts(struct node_host ** nh,sa_family_t * af)7130 remove_invalid_hosts(struct node_host **nh, sa_family_t *af)
7131 {
7132 struct node_host *n = *nh, *prev = NULL;
7133
7134 while (n != NULL) {
7135 if (*af && n->af && n->af != *af) {
7136 /* unlink and free n */
7137 struct node_host *next = n->next;
7138
7139 /* adjust tail pointer */
7140 if (n == (*nh)->tail)
7141 (*nh)->tail = prev;
7142 /* adjust previous node's next pointer */
7143 if (prev == NULL)
7144 *nh = next;
7145 else
7146 prev->next = next;
7147 /* free node */
7148 if (n->ifname != NULL)
7149 free(n->ifname);
7150 free(n);
7151 n = next;
7152 } else {
7153 if (n->af && !*af)
7154 *af = n->af;
7155 prev = n;
7156 n = n->next;
7157 }
7158 }
7159 }
7160
7161 int
invalid_redirect(struct node_host * nh,sa_family_t af)7162 invalid_redirect(struct node_host *nh, sa_family_t af)
7163 {
7164 if (!af) {
7165 struct node_host *n;
7166
7167 /* tables and dyniftl are ok without an address family */
7168 for (n = nh; n != NULL; n = n->next) {
7169 if (n->addr.type != PF_ADDR_TABLE &&
7170 n->addr.type != PF_ADDR_DYNIFTL) {
7171 yyerror("address family not given and "
7172 "translation address expands to multiple "
7173 "address families");
7174 return (1);
7175 }
7176 }
7177 }
7178 if (nh == NULL) {
7179 yyerror("no translation address with matching address family "
7180 "found.");
7181 return (1);
7182 }
7183 return (0);
7184 }
7185
7186 int
atoul(char * s,u_long * ulvalp)7187 atoul(char *s, u_long *ulvalp)
7188 {
7189 u_long ulval;
7190 char *ep;
7191
7192 errno = 0;
7193 ulval = strtoul(s, &ep, 0);
7194 if (s[0] == '\0' || *ep != '\0')
7195 return (-1);
7196 if (errno == ERANGE && ulval == ULONG_MAX)
7197 return (-1);
7198 *ulvalp = ulval;
7199 return (0);
7200 }
7201
7202 int
getservice(char * n)7203 getservice(char *n)
7204 {
7205 struct servent *s;
7206 u_long ulval;
7207
7208 if (atoul(n, &ulval) == 0) {
7209 if (ulval > 65535) {
7210 yyerror("illegal port value %lu", ulval);
7211 return (-1);
7212 }
7213 return (htons(ulval));
7214 } else {
7215 s = getservbyname(n, "tcp");
7216 if (s == NULL)
7217 s = getservbyname(n, "udp");
7218 if (s == NULL)
7219 s = getservbyname(n, "sctp");
7220 if (s == NULL) {
7221 yyerror("unknown port %s", n);
7222 return (-1);
7223 }
7224 return (s->s_port);
7225 }
7226 }
7227
7228 int
rule_label(struct pfctl_rule * r,char * s[PF_RULE_MAX_LABEL_COUNT])7229 rule_label(struct pfctl_rule *r, char *s[PF_RULE_MAX_LABEL_COUNT])
7230 {
7231 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++) {
7232 if (s[i] == NULL)
7233 return (0);
7234
7235 if (strlcpy(r->label[i], s[i], sizeof(r->label[0])) >=
7236 sizeof(r->label[0])) {
7237 yyerror("rule label too long (max %d chars)",
7238 sizeof(r->label[0])-1);
7239 return (-1);
7240 }
7241 }
7242 return (0);
7243 }
7244
7245 int
eth_rule_label(struct pfctl_eth_rule * r,char * s[PF_RULE_MAX_LABEL_COUNT])7246 eth_rule_label(struct pfctl_eth_rule *r, char *s[PF_RULE_MAX_LABEL_COUNT])
7247 {
7248 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++) {
7249 if (s[i] == NULL)
7250 return (0);
7251
7252 if (strlcpy(r->label[i], s[i], sizeof(r->label[0])) >=
7253 sizeof(r->label[0])) {
7254 yyerror("rule label too long (max %d chars)",
7255 sizeof(r->label[0])-1);
7256 return (-1);
7257 }
7258 }
7259 return (0);
7260 }
7261
7262 u_int16_t
parseicmpspec(char * w,sa_family_t af)7263 parseicmpspec(char *w, sa_family_t af)
7264 {
7265 const struct icmpcodeent *p;
7266 u_long ulval;
7267 u_int8_t icmptype;
7268
7269 if (af == AF_INET)
7270 icmptype = returnicmpdefault >> 8;
7271 else
7272 icmptype = returnicmp6default >> 8;
7273
7274 if (atoul(w, &ulval) == -1) {
7275 if ((p = geticmpcodebyname(icmptype, w, af)) == NULL) {
7276 yyerror("unknown icmp code %s", w);
7277 return (0);
7278 }
7279 ulval = p->code;
7280 }
7281 if (ulval > 255) {
7282 yyerror("invalid icmp code %lu", ulval);
7283 return (0);
7284 }
7285 return (icmptype << 8 | ulval);
7286 }
7287
7288 int
parseport(char * port,struct range * r,int extensions)7289 parseport(char *port, struct range *r, int extensions)
7290 {
7291 char *p = strchr(port, ':');
7292
7293 if (p == NULL) {
7294 if ((r->a = getservice(port)) == -1)
7295 return (-1);
7296 r->b = 0;
7297 r->t = PF_OP_NONE;
7298 return (0);
7299 }
7300 if ((extensions & PPORT_STAR) && !strcmp(p+1, "*")) {
7301 *p = 0;
7302 if ((r->a = getservice(port)) == -1)
7303 return (-1);
7304 r->b = 0;
7305 r->t = PF_OP_IRG;
7306 return (0);
7307 }
7308 if ((extensions & PPORT_RANGE)) {
7309 *p++ = 0;
7310 if ((r->a = getservice(port)) == -1 ||
7311 (r->b = getservice(p)) == -1)
7312 return (-1);
7313 if (r->a == r->b) {
7314 r->b = 0;
7315 r->t = PF_OP_NONE;
7316 } else
7317 r->t = PF_OP_RRG;
7318 return (0);
7319 }
7320 return (-1);
7321 }
7322
7323 int
pfctl_load_anchors(int dev,struct pfctl * pf,struct pfr_buffer * trans)7324 pfctl_load_anchors(int dev, struct pfctl *pf, struct pfr_buffer *trans)
7325 {
7326 struct loadanchors *la;
7327
7328 TAILQ_FOREACH(la, &loadanchorshead, entries) {
7329 if (pf->opts & PF_OPT_VERBOSE)
7330 fprintf(stderr, "\nLoading anchor %s from %s\n",
7331 la->anchorname, la->filename);
7332 if (pfctl_rules(dev, la->filename, pf->opts, pf->optimize,
7333 la->anchorname, trans) == -1)
7334 return (-1);
7335 }
7336
7337 return (0);
7338 }
7339
7340 int
kw_casecmp(const void * k,const void * e)7341 kw_casecmp(const void *k, const void *e)
7342 {
7343 return (strcasecmp(k, ((const struct keywords *)e)->k_name));
7344 }
7345
7346 int
map_tos(char * s,int * val)7347 map_tos(char *s, int *val)
7348 {
7349 /* DiffServ Codepoints and other TOS mappings */
7350 const struct keywords toswords[] = {
7351 { "af11", IPTOS_DSCP_AF11 },
7352 { "af12", IPTOS_DSCP_AF12 },
7353 { "af13", IPTOS_DSCP_AF13 },
7354 { "af21", IPTOS_DSCP_AF21 },
7355 { "af22", IPTOS_DSCP_AF22 },
7356 { "af23", IPTOS_DSCP_AF23 },
7357 { "af31", IPTOS_DSCP_AF31 },
7358 { "af32", IPTOS_DSCP_AF32 },
7359 { "af33", IPTOS_DSCP_AF33 },
7360 { "af41", IPTOS_DSCP_AF41 },
7361 { "af42", IPTOS_DSCP_AF42 },
7362 { "af43", IPTOS_DSCP_AF43 },
7363 { "critical", IPTOS_PREC_CRITIC_ECP },
7364 { "cs0", IPTOS_DSCP_CS0 },
7365 { "cs1", IPTOS_DSCP_CS1 },
7366 { "cs2", IPTOS_DSCP_CS2 },
7367 { "cs3", IPTOS_DSCP_CS3 },
7368 { "cs4", IPTOS_DSCP_CS4 },
7369 { "cs5", IPTOS_DSCP_CS5 },
7370 { "cs6", IPTOS_DSCP_CS6 },
7371 { "cs7", IPTOS_DSCP_CS7 },
7372 { "ef", IPTOS_DSCP_EF },
7373 { "inetcontrol", IPTOS_PREC_INTERNETCONTROL },
7374 { "lowdelay", IPTOS_LOWDELAY },
7375 { "netcontrol", IPTOS_PREC_NETCONTROL },
7376 { "reliability", IPTOS_RELIABILITY },
7377 { "throughput", IPTOS_THROUGHPUT },
7378 { "va", IPTOS_DSCP_VA }
7379 };
7380 const struct keywords *p;
7381
7382 p = bsearch(s, toswords, sizeof(toswords)/sizeof(toswords[0]),
7383 sizeof(toswords[0]), kw_casecmp);
7384
7385 if (p) {
7386 *val = p->k_val;
7387 return (1);
7388 }
7389 return (0);
7390 }
7391
7392 int
rt_tableid_max(void)7393 rt_tableid_max(void)
7394 {
7395 #ifdef __FreeBSD__
7396 int fibs;
7397 size_t l = sizeof(fibs);
7398
7399 if (sysctlbyname("net.fibs", &fibs, &l, NULL, 0) == -1)
7400 fibs = 16; /* XXX RT_MAXFIBS, at least limit it some. */
7401 /*
7402 * As the OpenBSD code only compares > and not >= we need to adjust
7403 * here given we only accept values of 0..n and want to avoid #ifdefs
7404 * in the grammar.
7405 */
7406 return (fibs - 1);
7407 #else
7408 return (RT_TABLEID_MAX);
7409 #endif
7410 }
7411
7412 struct node_mac*
node_mac_from_string(const char * str)7413 node_mac_from_string(const char *str)
7414 {
7415 struct node_mac *m;
7416
7417 m = calloc(1, sizeof(struct node_mac));
7418 if (m == NULL)
7419 err(1, "mac: calloc");
7420
7421 if (sscanf(str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
7422 &m->mac[0], &m->mac[1], &m->mac[2], &m->mac[3], &m->mac[4],
7423 &m->mac[5]) != 6) {
7424 free(m);
7425 yyerror("invalid MAC address");
7426 return (NULL);
7427 }
7428
7429 memset(m->mask, 0xff, ETHER_ADDR_LEN);
7430 m->isset = true;
7431 m->next = NULL;
7432 m->tail = m;
7433
7434 return (m);
7435 }
7436
7437 struct node_mac*
node_mac_from_string_masklen(const char * str,int masklen)7438 node_mac_from_string_masklen(const char *str, int masklen)
7439 {
7440 struct node_mac *m;
7441
7442 if (masklen < 0 || masklen > (ETHER_ADDR_LEN * 8)) {
7443 yyerror("invalid MAC mask length");
7444 return (NULL);
7445 }
7446
7447 m = node_mac_from_string(str);
7448 if (m == NULL)
7449 return (NULL);
7450
7451 memset(m->mask, 0, ETHER_ADDR_LEN);
7452 for (int i = 0; i < masklen; i++)
7453 m->mask[i / 8] |= 1 << (i % 8);
7454
7455 return (m);
7456 }
7457
7458 struct node_mac*
node_mac_from_string_mask(const char * str,const char * mask)7459 node_mac_from_string_mask(const char *str, const char *mask)
7460 {
7461 struct node_mac *m;
7462
7463 m = node_mac_from_string(str);
7464 if (m == NULL)
7465 return (NULL);
7466
7467 if (sscanf(mask, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
7468 &m->mask[0], &m->mask[1], &m->mask[2], &m->mask[3], &m->mask[4],
7469 &m->mask[5]) != 6) {
7470 free(m);
7471 yyerror("invalid MAC mask");
7472 return (NULL);
7473 }
7474
7475 return (m);
7476 }
7477