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
|
// SPDX-License-Identifier: GPL-3.0-or-later
#include "plugin_freebsd.h"
#include <netinet/ip_fw.h>
#define FREE_MEM_THRESHOLD 10000 // number of unused chunks that trigger memory freeing
#define COMMON_IPFW_ERROR() error("DISABLED: ipfw.packets chart"); \
error("DISABLED: ipfw.bytes chart"); \
error("DISABLED: ipfw.dyn_active chart"); \
error("DISABLED: ipfw.dyn_expired chart"); \
error("DISABLED: ipfw.mem chart");
// --------------------------------------------------------------------------------------------------------------------
// ipfw
int do_ipfw(int update_every, usec_t dt) {
(void)dt;
#if __FreeBSD__ >= 11
static int do_static = -1, do_dynamic = -1, do_mem = -1;
if (unlikely(do_static == -1)) {
do_static = config_get_boolean("plugin:freebsd:ipfw", "counters for static rules", 1);
do_dynamic = config_get_boolean("plugin:freebsd:ipfw", "number of dynamic rules", 1);
do_mem = config_get_boolean("plugin:freebsd:ipfw", "allocated memory", 1);
}
// variables for getting ipfw configuration
int error;
static int ipfw_socket = -1;
static ipfw_cfg_lheader *cfg = NULL;
ip_fw3_opheader *op3 = NULL;
static socklen_t *optlen = NULL, cfg_size = 0;
// variables for static rules handling
ipfw_obj_ctlv *ctlv = NULL;
ipfw_obj_tlv *rbase = NULL;
int rcnt = 0;
int n, seen;
struct ip_fw_rule *rule;
struct ip_fw_bcounter *cntr;
int c = 0;
char rule_num_str[12];
// variables for dynamic rules handling
caddr_t dynbase = NULL;
size_t dynsz = 0;
size_t readsz = sizeof(*cfg);;
int ttype = 0;
ipfw_obj_tlv *tlv;
ipfw_dyn_rule *dyn_rule;
uint16_t rulenum, prev_rulenum = IPFW_DEFAULT_RULE;
unsigned srn, static_rules_num = 0;
static size_t dyn_rules_num_size = 0;
static struct dyn_rule_num {
uint16_t rule_num;
uint32_t active_rules;
uint32_t expired_rules;
} *dyn_rules_num = NULL;
uint32_t *dyn_rules_counter;
if (likely(do_static | do_dynamic | do_mem)) {
// initialize the smallest ipfw_cfg_lheader possible
if (unlikely((optlen == NULL) || (cfg == NULL))) {
optlen = reallocz(optlen, sizeof(socklen_t));
*optlen = cfg_size = 32;
cfg = reallocz(cfg, *optlen);
}
// get socket descriptor and initialize ipfw_cfg_lheader structure
if (unlikely(ipfw_socket == -1))
ipfw_socket = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
if (unlikely(ipfw_socket == -1)) {
error("FREEBSD: can't get socket for ipfw configuration");
error("FREEBSD: run netdata as root to get access to ipfw data");
COMMON_IPFW_ERROR();
return 1;
}
bzero(cfg, 32);
cfg->flags = IPFW_CFG_GET_STATIC | IPFW_CFG_GET_COUNTERS | IPFW_CFG_GET_STATES;
op3 = &cfg->opheader;
op3->opcode = IP_FW_XGET;
// get ifpw configuration size than get configuration
*optlen = cfg_size;
error = getsockopt(ipfw_socket, IPPROTO_IP, IP_FW3, op3, optlen);
if (error)
if (errno != ENOMEM) {
error("FREEBSD: ipfw socket reading error");
COMMON_IPFW_ERROR();
return 1;
}
if ((cfg->size > cfg_size) || ((cfg_size - cfg->size) > sizeof(struct dyn_rule_num) * FREE_MEM_THRESHOLD)) {
*optlen = cfg_size = cfg->size;
cfg = reallocz(cfg, *optlen);
bzero(cfg, 32);
cfg->flags = IPFW_CFG_GET_STATIC | IPFW_CFG_GET_COUNTERS | IPFW_CFG_GET_STATES;
op3 = &cfg->opheader;
op3->opcode = IP_FW_XGET;
error = getsockopt(ipfw_socket, IPPROTO_IP, IP_FW3, op3, optlen);
if (error) {
error("FREEBSD: ipfw socket reading error");
COMMON_IPFW_ERROR();
return 1;
}
}
// go through static rules configuration structures
ctlv = (ipfw_obj_ctlv *) (cfg + 1);
if (cfg->flags & IPFW_CFG_GET_STATIC) {
/* We've requested static rules */
if (ctlv->head.type == IPFW_TLV_TBLNAME_LIST) {
readsz += ctlv->head.length;
ctlv = (ipfw_obj_ctlv *) ((caddr_t) ctlv +
ctlv->head.length);
}
if (ctlv->head.type == IPFW_TLV_RULE_LIST) {
rbase = (ipfw_obj_tlv *) (ctlv + 1);
rcnt = ctlv->count;
readsz += ctlv->head.length;
ctlv = (ipfw_obj_ctlv *) ((caddr_t) ctlv + ctlv->head.length);
}
}
if ((cfg->flags & IPFW_CFG_GET_STATES) && (readsz != *optlen)) {
/* We may have some dynamic states */
dynsz = *optlen - readsz;
/* Skip empty header */
if (dynsz != sizeof(ipfw_obj_ctlv))
dynbase = (caddr_t) ctlv;
else
dynsz = 0;
}
// --------------------------------------------------------------------
if (likely(do_mem)) {
static RRDSET *st_mem = NULL;
static RRDDIM *rd_dyn_mem = NULL;
static RRDDIM *rd_stat_mem = NULL;
if (unlikely(!st_mem)) {
st_mem = rrdset_create_localhost("ipfw",
"mem",
NULL,
"memory allocated",
NULL,
"Memory allocated by rules",
"bytes",
"freebsd.plugin",
"ipfw",
NETDATA_CHART_PRIO_IPFW_MEM,
update_every,
RRDSET_TYPE_STACKED
);
rrdset_flag_set(st_mem, RRDSET_FLAG_DETAIL);
rd_dyn_mem = rrddim_add(st_mem, "dynamic", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
rd_stat_mem = rrddim_add(st_mem, "static", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
} else
rrdset_next(st_mem);
rrddim_set_by_pointer(st_mem, rd_dyn_mem, dynsz);
rrddim_set_by_pointer(st_mem, rd_stat_mem, *optlen - dynsz);
rrdset_done(st_mem);
}
// --------------------------------------------------------------------
static RRDSET *st_packets = NULL, *st_bytes = NULL;
RRDDIM *rd_packets = NULL, *rd_bytes = NULL;
if (likely(do_static || do_dynamic)) {
if (likely(do_static)) {
if (unlikely(!st_packets))
st_packets = rrdset_create_localhost("ipfw",
"packets",
NULL,
"static rules",
NULL,
"Packets",
"packets/s",
"freebsd.plugin",
"ipfw",
NETDATA_CHART_PRIO_IPFW_PACKETS,
update_every,
RRDSET_TYPE_STACKED
);
else
rrdset_next(st_packets);
if (unlikely(!st_bytes))
st_bytes = rrdset_create_localhost("ipfw",
"bytes",
NULL,
"static rules",
NULL,
"Bytes"
|