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