Skip to content

Commit

Permalink
FreeBSD's PF has a new interface so leverage libpfctl to access it so…
Browse files Browse the repository at this point in the history
… the right interface is used depending on the version
  • Loading branch information
brd committed Nov 27, 2023
1 parent a380f7e commit 57a1b64
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/pf.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
#endif

#include <net/pfvar.h>
#ifdef __FreeBSD__
#include <libpfctl.h>
#endif

#ifndef FCNT_NAMES
#if FCNT_MAX != 3
Expand Down Expand Up @@ -76,6 +79,56 @@ static void pf_submit(char const *type, char const *type_instance, uint64_t val,
plugin_dispatch_values(&vl);
} /* void pf_submit */

#ifdef __FreeBSD__
static int pf_read(void) {
struct pfctl_status *state;
int fd;

fd = open(pf_device, O_RDONLY);
if (fd < 0) {
ERROR("pf plugin: Unable to open %s: %s", pf_device, STRERRNO);
return -1;
}

if ((state = pfctl_get_status(fd)) == NULL) {
ERROR("pf plugin: ioctl(DIOCGETSTATUS) failed: %s", STRERRNO);
close(fd);
return -1;
}

close(fd);

if (!state->running) {
pfctl_free_status(state);
WARNING("pf plugin: PF is not running.");
return -1;
}

for (int i = 0; i < PFRES_MAX; i++) {
pf_submit("pf_counters", pf_reasons[i], pfctl_status_counter(state, i),
/* is gauge = */ false);
}
for (int i = 0; i < LCNT_MAX; i++) {
pf_submit("pf_limits", pf_lcounters[i], pfctl_status_lcounter(state, i),
/* is gauge = */ false);
}
for (int i = 0; i < FCNT_MAX; i++) {
pf_submit("pf_state", pf_fcounters[i], pfctl_status_fcounter(state, i),
/* is gauge = */ false);
}
for (int i = 0; i < SCNT_MAX; i++) {
pf_submit("pf_source", pf_scounters[i], pfctl_status_scounter(state, i),
/* is gauge = */ false);
}

pf_submit("pf_states", "current", (uint32_t)state->states,
/* is gauge = */ true);

pfctl_free_status(state);

return 0;
} /* int pf_read */
#else
static int pf_read(void) {
struct pf_status state;
int fd;
Expand Down Expand Up @@ -119,5 +172,6 @@ static int pf_read(void) {

return 0;
} /* int pf_read */
#endif

void module_register(void) { plugin_register_read("pf", pf_read); }

0 comments on commit 57a1b64

Please sign in to comment.