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
|
/* $OpenBSD: pf.c,v 1.12 2020/05/15 00:56:03 cheloha Exp $ */
/*
* Copyright (c) 2001, 2007 Can Erkin Acar <canacar@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/sysctl.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/signal.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <net/pfvar.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <err.h>
#include <unistd.h>
#include <syslog.h>
#include "pfctl_parser.h"
#include "systat.h"
void print_pf(void);
int read_pf(void);
int select_pf(void);
void print_fld_double(field_def *, double);
const char *pf_reasons[PFRES_MAX+1] = PFRES_NAMES;
const char *pf_lcounters[LCNT_MAX+1] = LCNT_NAMES;
const char *pf_fcounters[FCNT_MAX+1] = FCNT_NAMES;
const char *pf_scounters[FCNT_MAX+1] = FCNT_NAMES;
static struct pf_status status;
int num_pf = 0;
field_def fields_pf[] = {
{"TYPE", 13, 16, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0},
{"NAME", 12, 24, 1, FLD_ALIGN_LEFT, -1, 0, 0, 0},
{"VALUE", 8, 10, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0},
{"RATE", 8, 10, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 60},
{"NOTES", 10, 20, 1, FLD_ALIGN_LEFT, -1, 0, 0, 60},
};
#define FLD_PF_TYPE FIELD_ADDR(fields_pf,0)
#define FLD_PF_NAME FIELD_ADDR(fields_pf,1)
#define FLD_PF_VALUE FIELD_ADDR(fields_pf,2)
#define FLD_PF_RATE FIELD_ADDR(fields_pf,3)
#define FLD_PF_DESC FIELD_ADDR(fields_pf,4)
/* Define views */
field_def *view_pf_0[] = {
FLD_PF_TYPE, FLD_PF_NAME, FLD_PF_VALUE, FLD_PF_RATE, FLD_PF_DESC, NULL
};
/* Define view managers */
struct view_manager pf_mgr = {
"PF", select_pf, read_pf, NULL, print_header,
print_pf, keyboard_callback, NULL, NULL
};
field_view views_pf[] = {
{view_pf_0, "pf", 'P', &pf_mgr},
{NULL, NULL, 0, NULL}
};
int
select_pf(void)
{
return (0);
}
int
read_pf(void)
{
size_t size = sizeof(status);
int mib[3] = { CTL_KERN, KERN_PFSTATUS };
if (sysctl(mib, 2, &status, &size, NULL, 0) == -1) {
error("sysctl(PFCTL_STATUS): %s", strerror(errno));
return (-1);
}
num_disp = 4;
if (status.ifname[0] != 0)
num_disp += 13;
num_disp += FCNT_MAX + 2;
num_disp += SCNT_MAX + 2;
num_disp += PFRES_MAX + 1;
num_disp += LCNT_MAX + 1;
return (0);
}
int
initpf(void)
{
field_view *v;
for (v = views_pf; v->name != NULL; v++)
add_view(v);
return(1);
}
void
print_fld_double(field_def *fld, double val)
{
int len;
if (fld == NULL)
return;
len = fld->width;
if (len < 1)
return;
tb_start();
if (tbprintf("%.2f", val) > len)
print_fld_str(fld, "*");
else
print_fld_tb(fld);
tb_end();
}
#define ADD_LINE_A(t, n, v) \
do { \
if (cur >= dispstart && cur < end) { \
print_fld_str(FLD_PF_TYPE, (t)); \
print_fld_str(FLD_PF_NAME, (n)); \
print_fld_age(FLD_PF_VALUE, (v)); \
end_line(); \
} \
if (++cur >= end) \
return; \
} while (0)
#define ADD_EMPTY_LINE \
do { \
if (cur >= dispstart && cur < end) \
end_line(); \
if (++cur >= end) \
return; \
} while (0)
#define ADD_LINE_S(t, n, v) \
do { \
if (cur >= dispstart && cur < end) { \
print_fld_str(FLD_PF_TYPE, (t)); \
print_fld_str(FLD_PF_NAME, (n)); \
print_fld_str(FLD_PF_VALUE, (v)); \
end_line(); \
} \
if (++cur >= end) \
return; \
} while (0)
#define ADD_LINE_V(t, n, v) \
do { \
if (cur >= dispstart && cur < end) { \
print_fld_str(FLD_PF_TYPE, (t)); \
print_fld_str(FLD_PF_NAME, (n)); \
print_fld_size(FLD_PF_VALUE, (v)); \
end_line(); \
} \
if (++cur >= end) \
return; \
} while (0)
#define ADD_LINE_VD(t, n, v, d) \
do { \
if (cur >= dispstart && cur < end) { \
print_fld_str(FLD_PF_TYPE, (t)); \
print_fld_str(FLD_PF_NAME, (n)); \
print_fld_size(FLD_PF_VALUE, (v)); \
print_fld_str(FLD_PF_DESC, (d)); \
end_line(); \
} \
if (++cur >= end) \
return; \
} while (0)
#define ADD_LINE_VR(t, n, v, r) \
do { \
if (cur >= dispstart && cur < end) { \
print_fld_str(FLD_PF_TYPE, (t)); \
print_fld_str(FLD_PF_NAME, (n)); \
print_fld_size(FLD_PF_VALUE, (v)); \
print_fld_double(FLD_PF_RATE, (r)); \
end_line(); \
} \
if (++cur >= end) \
return; \
} while (0)
void
print_pf(void)
{
char *debug;
time_t tm = 0;
struct timespec uptime;
int i;
struct pf_status *s = &status;
int cur = 0;
int end = dispstart + maxprint;
if (end > num_disp)
end = num_disp;
if (!clock_gettime(CLOCK_BOOTTIME, &uptime))
tm = uptime.tv_sec - s->since;
ADD_LINE_S("pf", "Status", s->running ? "Enabled" : "Disabled");
ADD_LINE_A("pf", "Since", tm);
switch (s->debug) {
case LOG_EMERG:
debug = "emerg";
break;
case LOG_ALERT:
debug = "alert";
break;
case LOG_CRIT:
debug = "crit";
break;
case LOG_ERR:
debug = "err";
break;
case LOG_WARNING:
debug = "warning";
break;
case LOG_NOTICE:
debug = "notice";
break;
case LOG_INFO:
debug = "info";
break;
case LOG_DEBUG:
debug = "debug";
break;
default:
debug = "unknown";
break;
}
ADD_LINE_S("pf", "Debug", debug);
tb_start();
tbprintf("0x%08x\n", ntohl(s->hostid));
tb_end();
ADD_LINE_S("pf", "Hostid", tmp_buf);
if (s->ifname[0] != 0) {
ADD_EMPTY_LINE;
ADD_LINE_VD(s->ifname, "Bytes In", s->bcounters[0][0], "IPv4");
ADD_LINE_VD(s->ifname, "Bytes In", s->bcounters[1][0], "IPv6");
ADD_LINE_VD(s->ifname, "Bytes Out", s->bcounters[0][1], "IPv4");
ADD_LINE_VD(s->ifname, "Bytes Out", s->bcounters[1][1], "IPv6");
ADD_LINE_VD(s->ifname, "Packets In", s->pcounters[0][0][PF_PASS], "IPv4, Passed");
ADD_LINE_VD(s->ifname, "Packets In", s->pcounters[1][0][PF_PASS], "IPv6, Passed");
ADD_LINE_VD(s->ifname, "Packets In", s->pcounters[0][0][PF_DROP], "IPv4, Blocked");
ADD_LINE_VD(s->ifname, "Packets In", s->pcounters[1][0][PF_DROP], "IPv6, Blocked");
ADD_LINE_VD(s->ifname, "Packets Out", s->pcounters[0][1][PF_PASS], "IPv4, Passed");
ADD_LINE_VD(s->ifname, "Packets Out", s->pcounters[1][1][PF_PASS], "IPv6, Passed");
ADD_LINE_VD(s->ifname, "Packets Out", s->pcounters[0][1][PF_DROP], "IPv4, Blocked");
ADD_LINE_VD(s->ifname, "Packets Out", s->pcounters[1][1][PF_DROP], "IPv6, Blocked");
}
ADD_EMPTY_LINE;
ADD_LINE_V("state", "Count", s->states);
for (i = 0; i < FCNT_MAX; i++) {
if (tm > 0)
ADD_LINE_VR("state", pf_fcounters[i], s->fcounters[i],
(double)s->fcounters[i] / (double)tm);
else
ADD_LINE_V("state", pf_fcounters[i], s->fcounters[i]);
}
ADD_EMPTY_LINE;
ADD_LINE_V("src track", "Count", s->src_nodes);
for (i = 0; i < SCNT_MAX; i++) {
if (tm > 0)
ADD_LINE_VR("src track", pf_scounters[i], s->scounters[i],
(double)s->scounters[i] / (double)tm);
else
ADD_LINE_V("src track", pf_scounters[i], s->scounters[i]);
}
ADD_EMPTY_LINE;
for (i = 0; i < PFRES_MAX; i++) {
if (tm > 0)
ADD_LINE_VR("counter", pf_reasons[i], s->counters[i],
(double)s->counters[i] / (double)tm);
else
ADD_LINE_V("counter", pf_reasons[i], s->counters[i]);
}
ADD_EMPTY_LINE;
for (i = 0; i < LCNT_MAX; i++) {
if (tm > 0)
ADD_LINE_VR("limit counter", pf_lcounters[i], s->lcounters[i],
(double)s->lcounters[i] / (double)tm);
else
ADD_LINE_V("limit counter", pf_lcounters[i], s->lcounters[i]);
}
}
|