1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
|
/* $OpenBSD: pfctl.c,v 1.35 2001/08/19 19:03:58 dhartmei Exp $ */
/*
* Copyright (c) 2001, Daniel Hartmeier
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <net/pfvar.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <err.h>
#include "pfctl_parser.h"
#define PF_OPT_DISABLE 0x0001
#define PF_OPT_ENABLE 0x0002
#define PF_OPT_VERBOSE 0x0004
#define PF_OPT_NOACTION 0x0008
#define PF_OPT_QUIET 0x0010
void usage(void);
int pfctl_enable(int, int);
int pfctl_disable(int, int);
int pfctl_clear_stats(int, int);
int pfctl_clear_rules(int, int);
int pfctl_clear_nat(int, int);
int pfctl_clear_states(int, int);
int pfctl_show_rules(int, int);
int pfctl_show_nat(int);
int pfctl_show_states(int, u_int8_t);
int pfctl_show_status(int);
int pfctl_rules(int, char *, int);
int pfctl_nat(int, char *, int);
int pfctl_log(int, char *, int);
int pfctl_debug(int, u_int32_t, int);
int opts = 0;
char *clearopt;
char *logopt;
char *natopt;
char *rulesopt;
char *showopt;
char *debugopt;
char *infile;
void
usage()
{
extern char *__progname;
fprintf(stderr, "usage: %s [-dehnqv] [-F set] [-l interface] ",
__progname);
fprintf(stderr, "[-N file] [-R file] [-s set] [-x level]\n");
exit(1);
}
int
pfctl_enable(int dev, int opts)
{
if (ioctl(dev, DIOCSTART)) {
if (errno == EEXIST)
errx(1, "pf already enabled");
else
err(1, "DIOCSTART");
}
if ((opts & PF_OPT_QUIET) == 0)
printf("pf enabled\n");
return (0);
}
int
pfctl_disable(int dev, int opts)
{
if (ioctl(dev, DIOCSTOP)) {
if (errno == ENOENT)
errx(1, "pf not enabled");
else
err(1, "DIOCSTOP");
}
if ((opts & PF_OPT_QUIET) == 0)
printf("pf disabled\n");
return (0);
}
int
pfctl_clear_stats(int dev, int opts)
{
if (ioctl(dev, DIOCCLRSTATUS))
err(1, "DIOCCLRSTATUS");
if ((opts & PF_OPT_QUIET) == 0)
printf("pf: statistics cleared\n");
return (0);
}
int
pfctl_clear_rules(int dev, int opts)
{
struct pfioc_rule pr;
if (ioctl(dev, DIOCBEGINRULES, &pr.ticket))
err(1, "DIOCBEGINRULES");
else if (ioctl(dev, DIOCCOMMITRULES, &pr.ticket))
err(1, "DIOCCOMMITRULES");
if ((opts & PF_OPT_QUIET) == 0)
printf("rules cleared\n");
return (0);
}
int
pfctl_clear_nat(int dev, int opts)
{
struct pfioc_nat pn;
struct pfioc_rdr pr;
if (ioctl(dev, DIOCBEGINNATS, &pn.ticket))
err(1, "DIOCBEGINNATS");
else if (ioctl(dev, DIOCCOMMITNATS, &pn.ticket))
err(1, "DIOCCOMMITNATS");
else if (ioctl(dev, DIOCBEGINRDRS, &pr.ticket))
err(1, "DIOCBEGINRDRS");
else if (ioctl(dev, DIOCCOMMITRDRS, &pr.ticket))
err(1, "DIOCCOMMITRDRS");
if ((opts & PF_OPT_QUIET) == 0)
printf("nat cleared\n");
return (0);
}
int
pfctl_clear_states(int dev, int opts)
{
if (ioctl(dev, DIOCCLRSTATES))
err(1, "DIOCCLRSTATES");
if ((opts & PF_OPT_QUIET) == 0)
printf("states cleared\n");
return (0);
}
int
pfctl_show_rules(int dev, int opts)
{
struct pfioc_rule pr;
u_int32_t nr, mnr;
if (ioctl(dev, DIOCGETRULES, &pr))
err(1, "DIOCGETRULES");
mnr = pr.nr;
for (nr = 0; nr < mnr; ++nr) {
pr.nr = nr;
if (ioctl(dev, DIOCGETRULE, &pr))
err(1, "DIOCGETRULE");
print_rule(&pr.rule);
if (opts & PF_OPT_VERBOSE)
printf("[ Evaluations: %-10llu Packets: %-10llu "
"Bytes: %-10llu ]\n\n", pr.rule.evaluations,
pr.rule.packets, pr.rule.bytes);
}
return (0);
}
int
pfctl_show_nat(int dev)
{
struct pfioc_nat pn;
struct pfioc_rdr pr;
u_int32_t mnr, nr;
if (ioctl(dev, DIOCGETNATS, &pn))
err(1, "DIOCGETNATS");
mnr = pn.nr;
for (nr = 0; nr < mnr; ++nr) {
pn.nr = nr;
if (ioctl(dev, DIOCGETNAT, &pn))
err(1, "DIOCGETNAT");
print_nat(&pn.nat);
}
if (ioctl(dev, DIOCGETRDRS, &pr))
err(1, "DIOCGETRDRS");
mnr = pr.nr;
for (nr = 0; nr < mnr; ++nr) {
pr.nr = nr;
if (ioctl(dev, DIOCGETRDR, &pr))
err(1, "DIOCGETRDR");
print_rdr(&pr.rdr);
}
return (0);
}
int
pfctl_show_states(int dev, u_int8_t proto)
{
struct pfioc_states ps;
struct pf_state *p;
char *inbuf = NULL;
int i, len = 0;
while (1) {
ps.ps_len = len;
if (len) {
ps.ps_buf = inbuf = realloc(inbuf, len);
if (inbuf == NULL)
err(1, "malloc");
}
if (ioctl(dev, DIOCGETSTATES, &ps) < 0)
err(1, "DIOCGETSTATES");
if (ps.ps_len + sizeof(struct pfioc_state) < len)
break;
if (len == 0 && ps.ps_len != 0)
len = ps.ps_len;
len *= 2;
}
p = ps.ps_states;
for (i = 0; i < ps.ps_len; i += sizeof(*p)) {
if (!proto || (p->proto == proto))
print_state(p);
p++;
}
return (0);
}
int
pfctl_show_status(int dev)
{
struct pf_status status;
if (ioctl(dev, DIOCGETSTATUS, &status))
err(1, "DIOCGETSTATUS");
print_status(&status);
return (0);
}
/* callbacks for rule/nat/rdr */
int
pfctl_add_rule(struct pfctl *pf, struct pf_rule *r)
{
memcpy(&pf->prule->rule, r, sizeof(pf->prule->rule));
if ((pf->opts & PF_OPT_NOACTION) == 0) {
if (ioctl(pf->dev, DIOCADDRULE, pf->prule))
err(1, "DIOCADDRULE");
}
if (pf->opts & PF_OPT_VERBOSE)
print_rule(&pf->prule->rule);
return 0;
}
int
pfctl_add_nat(struct pfctl *pf, struct pf_nat *n)
{
memcpy(&pf->pnat->nat, n, sizeof(pf->pnat->nat));
if ((pf->opts & PF_OPT_NOACTION) == 0) {
if (ioctl(pf->dev, DIOCADDNAT, pf->pnat))
err(1, "DIOCADDNAT");
}
if (pf->opts & PF_OPT_VERBOSE)
print_nat(&pf->pnat->nat);
return 0;
}
int
pfctl_add_rdr(struct pfctl *pf, struct pf_rdr *r)
{
memcpy(&pf->prdr->rdr, r, sizeof(pf->prdr->rdr));
if ((pf->opts & PF_OPT_NOACTION) == 0) {
if (ioctl(pf->dev, DIOCADDRDR, pf->prdr))
err(1, "DIOCADDRDR");
}
if (pf->opts & PF_OPT_VERBOSE)
print_rdr(&pf->prdr->rdr);
return 0;
}
int
pfctl_rules(int dev, char *filename, int opts)
{
FILE *fin;
struct pfioc_rule pr;
struct pfctl pf;
if (strcmp(filename, "-") == 0) {
infile = "stdin";
fin = stdin;
} else {
fin = fopen(filename, "r");
infile = filename;
}
if (fin == NULL)
return (1);
if ((opts & PF_OPT_NOACTION) == 0) {
if (ioctl(dev, DIOCBEGINRULES, &pr.ticket))
err(1, "DIOCBEGINRULES");
}
/* fill in callback data */
pf.dev = dev;
pf.opts = opts;
pf.prule = ≺
if (parse_rules(fin, &pf) < 0)
errx(1, "syntax error in rule file: pf rules not loaded");
if ((opts & PF_OPT_NOACTION) == 0) {
if (ioctl(dev, DIOCCOMMITRULES, &pr.ticket))
err(1, "DIOCCOMMITRULES");
#if 0
if ((opts & PF_OPT_QUIET) == 0)
printf("%u rules loaded\n", n);
#endif
}
if (fin != stdin)
fclose(fin);
return (0);
}
int
pfctl_nat(int dev, char *filename, int opts)
{
FILE *fin;
struct pfioc_nat pn;
struct pfioc_rdr pr;
struct pfctl pf;
if (strcmp(filename, "-") == 0) {
fin = stdin;
infile = "stdin";
} else {
fin = fopen(filename, "r");
infile = filename;
}
if (fin == NULL)
return (1);
if ((opts & PF_OPT_NOACTION) == 0) {
if (ioctl(dev, DIOCBEGINNATS, &pn.ticket))
err(1, "DIOCBEGINNATS");
if (ioctl(dev, DIOCBEGINRDRS, &pr.ticket))
err(1, "DIOCBEGINRDRS");
}
/* fill in callback data */
pf.dev = dev;
pf.opts = opts;
pf.pnat = &pn;
pf.prdr = ≺
if (parse_nat(fin, &pf) < 0)
errx(1, "syntax error in file: nat rules not loaded");
if ((opts & PF_OPT_NOACTION) == 0) {
if (ioctl(dev, DIOCCOMMITNATS, &pn.ticket))
err(1, "DIOCCOMMITNATS");
if (ioctl(dev, DIOCCOMMITRDRS, &pr.ticket))
err(1, "DIOCCOMMITRDRS");
#if 0
if ((opts & PF_OPT_QUIET) == 0) {
printf("%u nat entries loaded\n", n);
printf("%u rdr entries loaded\n", r);
}
#endif
}
if (fin != stdin)
fclose(fin);
return (0);
}
int
pfctl_log(int dev, char *ifname, int opts)
{
struct pfioc_if pi;
strlcpy(pi.ifname, ifname, sizeof(pi.ifname));
if (ioctl(dev, DIOCSETSTATUSIF, &pi))
err(1, "DIOCSETSTATUSIF");
if ((opts & PF_OPT_QUIET) == 0)
printf("now logging %s\n", pi.ifname);
return (0);
}
int
pfctl_debug(int dev, u_int32_t level, int opts)
{
if (ioctl(dev, DIOCSETDEBUG, &level))
err(1, "DIOCSETDEBUG");
if ((opts & PF_OPT_QUIET) == 0) {
printf("debug level set to '");
switch (level) {
case PF_DEBUG_NONE:
printf("none");
break;
case PF_DEBUG_URGENT:
printf("urgent");
break;
case PF_DEBUG_MISC:
printf("misc");
break;
default:
printf("<invalid>");
break;
}
printf("'\n");
}
return (0);
}
int
main(int argc, char *argv[])
{
extern char *optarg;
extern int optind;
int error = 0;
int dev = -1;
int ch;
if (argc < 2)
usage();
while ((ch = getopt(argc, argv, "deqF:hl:nN:R:s:vx:")) != -1) {
switch (ch) {
case 'd':
opts |= PF_OPT_DISABLE;
break;
case 'e':
opts |= PF_OPT_ENABLE;
break;
case 'q':
opts |= PF_OPT_QUIET;
break;
case 'F':
clearopt = optarg;
break;
case 'l':
logopt = optarg;
break;
case 'n':
opts |= PF_OPT_NOACTION;
break;
case 'N':
natopt = optarg;
break;
case 'R':
rulesopt = optarg;
break;
case 's':
showopt = optarg;
break;
case 'v':
opts |= PF_OPT_VERBOSE;
break;
case 'x':
debugopt = optarg;
break;
case 'h':
default:
usage();
/* NOTREACHED */
}
}
if (argc != optind) {
warnx("unknown command line argument: %s ...", argv[optind]);
usage();
/* NOTREACHED */
}
if ((opts & PF_OPT_NOACTION) == 0) {
dev = open("/dev/pf", O_RDWR);
if (dev == -1)
err(1, "open(\"/dev/pf\")");
} else {
/* turn off options */
opts &= ~ (PF_OPT_DISABLE | PF_OPT_ENABLE);
clearopt = logopt = showopt = debugopt = NULL;
}
if (opts & PF_OPT_DISABLE)
if (pfctl_disable(dev, opts))
error = 1;
if (clearopt != NULL) {
switch (*clearopt) {
case 'r':
pfctl_clear_rules(dev, opts);
break;
case 'n':
pfctl_clear_nat(dev, opts);
break;
case 's':
pfctl_clear_states(dev, opts);
break;
case 'i':
pfctl_clear_stats(dev, opts);
break;
case 'a':
pfctl_clear_rules(dev, opts);
pfctl_clear_nat(dev, opts);
pfctl_clear_states(dev, opts);
pfctl_clear_stats(dev, opts);
break;
default:
warnx("Unknown flush modifier '%s'", clearopt);
error = 1;
}
}
if (rulesopt != NULL)
if (pfctl_rules(dev, rulesopt, opts))
error = 1;
if (natopt != NULL)
if (pfctl_nat(dev, natopt, opts))
error = 1;
if (showopt != NULL) {
switch (*showopt) {
case 'r':
pfctl_show_rules(dev, opts);
break;
case 'n':
pfctl_show_nat(dev);
break;
case 's':
pfctl_show_states(dev, 0);
break;
case 'i':
pfctl_show_status(dev);
break;
case 'a':
pfctl_show_rules(dev, opts);
pfctl_show_nat(dev);
pfctl_show_states(dev, 0);
pfctl_show_status(dev);
break;
default:
warnx("Unknown show modifier '%s'", showopt);
error = 1;
}
}
if (logopt != NULL)
if (pfctl_log(dev, logopt, opts))
error = 1;
if (opts & PF_OPT_ENABLE)
if (pfctl_enable(dev, opts))
error = 1;
if (debugopt != NULL) {
switch (*debugopt) {
case 'n':
pfctl_debug(dev, PF_DEBUG_NONE, opts);
break;
case 'u':
pfctl_debug(dev, PF_DEBUG_URGENT, opts);
break;
case 'm':
pfctl_debug(dev, PF_DEBUG_MISC, opts);
break;
default:
warnx("Unknown debug level '%s'", debugopt);
error = 1;
}
}
close(dev);
exit(error);
}
|