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
|
/* $OpenBSD: pfctl.c,v 1.15 2001/06/26 22:18:17 provos 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"
void usage(void);
char *load_file(char *, size_t *);
int pfctl_enable(int);
int pfctl_disable(int);
int pfctl_clear_rules(int);
int pfctl_clear_nat(int);
int pfctl_clear_states(int);
int pfctl_show_rules(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 pfctl_nat(int, char *);
int pfctl_log(int, char *);
int dflag;
int eflag;
char *clearopt;
char *logopt;
char *natopt;
char *rulesopt;
char *showopt;
void
usage()
{
extern char *__progname;
fprintf(stderr, "usage: %s [-de] [-c set] [-l interface]", __progname);
fprintf(stderr, " [-n file] [-r file] [-s set]\n");
exit(1);
}
char *
load_file(char *name, size_t *len)
{
FILE *file = fopen(name, "r");
char *buf = 0;
*len = 0;
if (file == NULL) {
fprintf(stderr, "ERROR: couldn't open file %s (%s)\n",
name, strerror(errno));
return (0);
}
fseek(file, 0, SEEK_END);
*len = ftell(file);
fseek(file, 0, SEEK_SET);
buf = malloc(*len);
if (buf == NULL) {
fclose(file);
fprintf(stderr, "ERROR: malloc() failed\n");
return (0);
}
if (fread(buf, 1, *len, file) != *len) {
free(buf);
fclose(file);
fprintf(stderr, "ERROR: fread() failed\n");
return (0);
}
fclose(file);
return (buf);
}
int
pfctl_enable(int dev)
{
if (ioctl(dev, DIOCSTART)) {
errx(1, "DIOCSTART");
return (1);
}
printf("pf enabled\n");
return (0);
}
int
pfctl_disable(int dev)
{
if (ioctl(dev, DIOCSTOP)) {
errx(1, "DIOCSTOP");
return (1);
}
printf("pf disabled\n");
return (0);
}
int
pfctl_clear_rules(int dev)
{
struct pfioc_rule pr;
if (ioctl(dev, DIOCBEGINRULES, &pr.ticket)) {
errx(1, "DIOCBEGINRULES");
return (1);
} else if (ioctl(dev, DIOCCOMMITRULES, &pr.ticket)) {
errx(1, "DIOCCOMMITRULES");
return (1);
}
printf("rules cleared\n");
return (0);
}
int
pfctl_clear_nat(int dev)
{
struct pfioc_nat pn;
struct pfioc_rdr pr;
if (ioctl(dev, DIOCBEGINNATS, &pn.ticket)) {
errx(1, "DIOCBEGINNATS");
return (1);
} else if (ioctl(dev, DIOCCOMMITNATS, &pn.ticket)) {
errx(1, "DIOCCOMMITNATS");
return (1);
} else if (ioctl(dev, DIOCBEGINRDRS, &pr.ticket)) {
errx(1, "DIOCBEGINRDRS");
return (1);
} else if (ioctl(dev, DIOCCOMMITRDRS, &pr.ticket)) {
errx(1, "DIOCCOMMITRDRS");
return (1);
}
printf("nat cleared\n");
return (0);
}
int
pfctl_clear_states(int dev)
{
if (ioctl(dev, DIOCCLRSTATES)) {
errx(1, "DIOCCLRSTATES");
return (1);
}
printf("states cleared\n");
return (0);
}
int
pfctl_show_rules(int dev)
{
struct pfioc_rule pr;
u_int32_t nr, mnr;
if (ioctl(dev, DIOCGETRULES, &pr)) {
errx(1, "DIOCGETRULES");
return (1);
}
mnr = pr.nr;
for (nr = 0; nr < mnr; ++nr) {
pr.nr = nr;
if (ioctl(dev, DIOCGETRULE, &pr)) {
errx(1, "DIOCGETRULE");
return (1);
}
print_rule(&pr.rule);
}
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)) {
errx(1, "DIOCGETNATS");
return (1);
}
mnr = pn.nr;
for (nr = 0; nr < mnr; ++nr) {
pn.nr = nr;
if (ioctl(dev, DIOCGETNAT, &pn)) {
errx(1, "DIOCGETNAT");
return (1);
}
print_nat(&pn.nat);
}
if (ioctl(dev, DIOCGETRDRS, &pr)) {
errx(1, "DIOCGETRDRS");
return (1);
}
mnr = pr.nr;
for (nr = 0; nr < mnr; ++nr) {
pr.nr = nr;
if (ioctl(dev, DIOCGETRDR, &pr)) {
errx(1, "DIOCGETRDR");
return (1);
}
print_rdr(&pr.rdr);
}
return (0);
}
int
pfctl_show_states(int dev, u_int8_t proto)
{
struct pfioc_state ps;
ps.nr = 0;
while (!ioctl(dev, DIOCGETSTATE, &ps)) {
if (!proto || (ps.state.proto == proto))
print_state(&ps.state);
ps.nr++;
}
return (0);
}
int
pfctl_show_status(int dev)
{
struct pf_status status;
if (ioctl(dev, DIOCGETSTATUS, &status)) {
errx(1, "DIOCGETSTATUS");
return (1);
}
print_status(&status);
return (0);
}
int
pfctl_rules(int dev, char *filename)
{
struct pfioc_rule pr;
char *buf, *s;
size_t len;
unsigned n, nr;
buf = load_file(filename, &len);
if (buf == NULL)
return (1);
if (ioctl(dev, DIOCBEGINRULES, &pr.ticket)) {
errx(1, "DIOCBEGINRULES");
free(buf);
return (1);
}
n = 0;
nr = 0;
s = buf;
do {
char *line = next_line(&s);
nr++;
if (*line && (*line != '#'))
if (parse_rule(nr, line, &pr.rule)) {
if (ioctl(dev, DIOCADDRULE, &pr)) {
errx(1, "DIOCADDRULE");
free(buf);
return (1);
}
n++;
}
} while (s < (buf + len));
free(buf);
if (ioctl(dev, DIOCCOMMITRULES, &pr.ticket)) {
errx(1, "DIOCCOMMITRULES");
return (1);
}
printf("%u rules loaded\n", n);
return (0);
}
int
pfctl_nat(int dev, char *filename)
{
struct pfioc_nat pn;
struct pfioc_rdr pr;
char *buf, *s;
size_t len;
unsigned n, nr;
if (ioctl(dev, DIOCBEGINNATS, &pn.ticket)) {
errx(1, "DIOCBEGINNATS");
return (1);
}
buf = load_file(filename, &len);
if (buf == NULL)
return (1);
n = 0;
nr = 0;
s = buf;
do {
char *line = next_line(&s);
nr++;
if (*line && (*line == 'n'))
if (parse_nat(nr, line, &pn.nat)) {
if (ioctl(dev, DIOCADDNAT, &pn)) {
errx(1, "DIOCADDNAT");
free(buf);
return (1);
}
n++;
}
} while (s < (buf + len));
free(buf);
if (ioctl(dev, DIOCCOMMITNATS, &pn.ticket)) {
errx(1, "DIOCCOMMITNATS");
return (1);
}
printf("%u nat entries loaded\n", n);
if (ioctl(dev, DIOCBEGINRDRS, &pr.ticket)) {
errx(1, "DIOCBEGINRDRS");
return 1;
}
buf = load_file(filename, &len);
if (buf == NULL)
return (1);
n = 0;
nr = 0;
s = buf;
do {
char *line = next_line(&s);
nr++;
if (*line && (*line == 'r'))
if (parse_rdr(nr, line, &pr.rdr)) {
if (ioctl(dev, DIOCADDRDR, &pr)) {
errx(1, "DIOCADDRDR");
free(buf);
return (1);
}
n++;
}
} while (s < (buf + len));
free(buf);
if (ioctl(dev, DIOCCOMMITRDRS, &pr.ticket)) {
errx(1, "DIOCCOMMITRDRS");
return (1);
}
printf("%u rdr entries loaded\n", n);
return (0);
}
int
pfctl_log(int dev, char *ifname)
{
struct pfioc_if pi;
strncpy(pi.ifname, ifname, 16);
if (ioctl(dev, DIOCSETSTATUSIF, &pi)) {
errx(1, "DIOCSETSTATUSIF");
return (1);
}
printf("now logging %s\n", pi.ifname);
return (0);
}
int
main(int argc, char *argv[])
{
extern char *optarg;
extern int optind;
int error = 0;
int dev;
int ch;
if (argc < 2)
usage();
while ((ch = getopt(argc, argv, "c:del:n:r:s:")) != -1) {
switch (ch) {
case 'c':
clearopt = optarg;
break;
case 'd':
dflag++;
break;
case 'e':
eflag++;
break;
case 'l':
logopt = optarg;
break;
case 'n':
natopt = optarg;
break;
case 'r':
rulesopt = optarg;
break;
case 's':
showopt = optarg;
break;
default:
usage();
/* NOTREACHED */
}
}
dev = open("/dev/pf", O_RDWR);
if (dev == -1) {
errx(1, "/dev/pf");
return (1);
}
if (dflag)
if (pfctl_disable(dev))
error = 1;
if (clearopt != NULL) {
if (!strcmp(clearopt, "rules")) {
if (pfctl_clear_rules(dev))
error = 1;
} else if (!strcmp(clearopt, "nat")) {
if (pfctl_clear_nat(dev))
error = 1;
} else if (!strcmp(clearopt, "states")) {
if (pfctl_clear_states(dev))
error = 1;
} else {
warnx("Unknown keyword '%s'", clearopt);
error = 1;
}
}
if (rulesopt != NULL)
if (pfctl_rules(dev, rulesopt))
error = 1;
if (natopt != NULL)
if (pfctl_nat(dev, natopt))
error = 1;
if (showopt != NULL) {
if (!strcmp(showopt, "rules")) {
if (pfctl_show_rules(dev))
error = 1;
} else if (!strcmp(showopt, "nat")) {
if (pfctl_show_nat(dev))
error = 1;
} else if (!strcmp(showopt, "states")) {
if (pfctl_show_states(dev, 0))
error = 1;
} else if (!strcmp(showopt, "status")) {
if (pfctl_show_status(dev))
error = 1;
} else {
warnx("Unknown keyword '%s'", showopt);
error = 1;
}
}
if (logopt != NULL)
if (pfctl_log(dev, logopt))
error = 1;
if (eflag)
if (pfctl_enable(dev))
error = 1;
close(dev);
exit(error);
}
|