summaryrefslogtreecommitdiffstats
path: root/src/freebsd_ipfw.c
blob: 81264b3f3d4f16d5c111698a61ac7f6244cb0799 (plain)
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
#include "common.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",
                                                 "ipfw",
                                                 3005,
                                                 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",
                                                         "ipfw",
                                                         3001,
                                                         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",
                                                       "bytes/s",
                                                       "freebsd",
                                                       "ipfw",
                                                       3002,
                                                       update_every,
                                                       RRDSET_TYPE_STACKED
                    );
                else
                    rrdset_next(st_bytes);
            }

            for (n = seen = 0; n < rcnt; n++, rbase = (ipfw_obj_tlv *) ((caddr_t) rbase + rbase->length)) {
                cntr = (struct ip_fw_bcounter *) (rbase + 1);
                rule = (struct ip_fw_rule *) ((caddr_t) cntr + cntr->size);
                if (rule->rulenum != prev_rulenum)
                    static_rules_num++;
                if (rule->rulenum > IPFW_DEFAULT_RULE)
                    break;

                if (likely(do_static)) {
                    sprintf(rule_num_str, "%d_%d", rule->rulenum, rule->id);

                    rd_packets = rrddim_find(st_packets, rule_num_str);
                    if (unlikely(!rd_packets))
                        rd_packets = rrddim_add(st_packets, rule_num_str, NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
                    rrddim_set_by_pointer(st_packets, rd_packets, cntr->pcnt);

                    rd_bytes = rrddim_find(st_bytes, rule_num_str);
                    if (unlikely(!rd_bytes))
                        rd_bytes = rrddim_add(st_bytes, rule_num_str, NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
                    rrddim_set_by_pointer(st_bytes, rd_bytes, cntr->bcnt);
                }

                c += rbase->length;
                seen++;
            }

            if (likely(do_static)) {
                rrdset_done(st_packets);
                rrdset_done(st_bytes);
            }
        }

        // --------------------------------------------------------------------

        // go through dynamic rules configuration structures

        if (likely(do_dynamic && (dynsz > 0))) {
            if ((dyn_rules_num_size < sizeof(struct dyn_rule_num) * static_rules_num) ||
                ((dyn_rules_num_size - sizeof(struct dyn_rule_num) * static_rules_num) >
                 sizeof(struct dyn_rule_num) * FREE_MEM_THRESHOLD)) {
                dyn_rules_num_size = sizeof(struct dyn_rule_num) * static_rules_num;
                dyn_rules_num = reallocz(dyn_rules_num, dyn_rules_num_size);
            }
            bzero(dyn_rules_num, sizeof(struct dyn_rule_num) * static_rules_num);
            dyn_rules_num->rule_num = IPFW_DEFAULT_RULE;

            if (dynsz > 0 && ctlv->head.type == IPFW_TLV_DYNSTATE_LIST) {
                dynbase += sizeof(*ctlv);
                dynsz -= sizeof(*ctlv);
                ttype = IPFW_TLV_DYN_ENT;
            }

            while (dynsz > 0) {
                tlv = (ipfw_obj_tlv *) dynbase;
                if (tlv->type != ttype)
                    break;

                dyn_rule = (ipfw_dyn_rule *) (tlv + 1);
                bcopy(&dyn_rule->rule, &rulenum, sizeof(rulenum));

                for (srn = 0; srn < (static_rules_num - 1); srn++) {
                    if (dyn_rule->expire > 0)
                        dyn_rules_counter = &dyn_rules_num[srn].active_rules;
                    else
                        dyn_rules_counter = &dyn_rules_num[srn].expired_rules;
                    if (dyn_rules_num[srn].rule_num == rulenum) {
                        (*dyn_rules_counter)++;
                        break;
                    }
                    if (dyn_rules_num[srn].rule_num == IPFW_DEFAULT_RULE) {
                        dyn_rules_num[srn].rule_num = rulenum;
                        dyn_rules_num[srn + 1].rule_num = IPFW_DEFAULT_RULE;
                        (*dyn_rules_counter)++;
                        break;
                    }
                }

                dynsz -= tlv->length;
                dynbase += tlv->length;
            }

            // --------------------------------------------------------------------

            static RRDSET *st_active = NULL, *st_expired = NULL;
            RRDDIM *rd_active = NULL, *rd_expired = NULL;

            if (unlikely(!st_active))
                st_active = rrdset_create_localhost("ipfw",
                                                    "active",
                                                    NULL,
                                                    "dynamic_rules",
                                                    NULL,
                                                    "Active rules",
                                                    "rules",
                                                    "freebsd",
                                                    "ipfw",
                                                    3003,
                                                    update_every,
                                                    RRDSET_TYPE_STACKED
                );
            else
                rrdset_next(st_active);

            if (unlikely(!st_expired))
                st_expired = rrdset_create_localhost("ipfw",
                                                     "expired",
                                                     NULL,
                                                     "dynamic_rules",
                                                     NULL,
                                                     "Expired rules",
                                                     "rules",
                                                     "freebsd",
                                                     "ipfw",
                                                     3004,
                                                     update_every,
                                                     RRDSET_TYPE_STACKED
                );
            else
                rrdset_next(st_expired);

            for (srn = 0; (srn < (static_rules_num - 1)) && (dyn_rules_num[srn].rule_num != IPFW_DEFAULT_RULE); srn++) {
                sprintf(rule_num_str, "%d", dyn_rules_num[srn].rule_num);

                rd_active = rrddim_find(st_active, rule_num_str);
                if (unlikely(!rd_active))
                    rd_active = rrddim_add(st_active, rule_num_str, NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
                rrddim_set_by_pointer(st_active, rd_active, dyn_rules_num[srn].active_rules);

                rd_expired = rrddim_find(st_expired, rule_num_str);
                if (unlikely(!rd_expired))
                    rd_expired = rrddim_add(st_expired, rule_num_str, NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
                rrddim_set_by_pointer(st_expired, rd_expired, dyn_rules_num[srn].expired_rules);
            }

            rrdset_done(st_active);
            rrdset_done(st_expired);
        }
    }

    return 0;
#else
    error("FREEBSD: ipfw charts supported for FreeBSD 11.0 and newer releases only");
    COMMON_IPFW_ERROR();
    return 1;
#endif
}