2 * collectd - src/iptables.c
3 * Copyright (C) 2007 Sjoerd van der Berg
4 * Copyright (C) 2007-2010 Florian octo Forster
5 * Copyright (C) 2009 Marco Chiappero
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 * Sjoerd van der Berg <harekiet at users.sourceforge.net>
23 * Florian Forster <octo at collectd.org>
24 * Marco Chiappero <marco at absence.it>
32 #include <libiptc/libip6tc.h>
33 #include <libiptc/libiptc.h>
35 #ifdef HAVE_SYS_CAPABILITY_H
36 #include <sys/capability.h>
40 * iptc_handle_t was available before libiptc was officially available as a
41 * shared library. Note, that when the shared lib was introduced, the API and
42 * ABI have changed slightly:
43 * 'iptc_handle_t' used to be 'struct iptc_handle *' and most functions used
44 * 'iptc_handle_t *' as an argument. Now, most functions use 'struct
45 * iptc_handle *' (thus removing one level of pointer indirection).
47 * HAVE_IPTC_HANDLE_T is used to determine which API ought to be used. While
48 * this is somewhat hacky, I didn't find better way to solve that :-/
51 #ifndef HAVE_IPTC_HANDLE_T
52 typedef struct iptc_handle iptc_handle_t;
54 #ifndef HAVE_IP6TC_HANDLE_T
55 typedef struct ip6tc_handle ip6tc_handle_t;
59 * (Module-)Global variables
63 * Config format should be `Chain table chainname',
64 * e. g. `Chain mangle incoming'
66 static const char *config_keys[] = {"Chain", "Chain6"};
67 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
68 enum protocol_version_e { IPV4, IPV6 };
69 typedef enum protocol_version_e protocol_version_t;
72 * Each table/chain combo that will be queried goes into this list
74 #ifndef XT_TABLE_MAXNAMELEN
75 #define XT_TABLE_MAXNAMELEN 32
78 protocol_version_t ip_version;
79 char table[XT_TABLE_MAXNAMELEN];
80 char chain[XT_TABLE_MAXNAMELEN];
85 enum { RTYPE_NUM, RTYPE_COMMENT, RTYPE_COMMENT_ALL } rule_type;
89 static ip_chain_t **chain_list = NULL;
90 static int chain_num = 0;
92 static int iptables_config(const char *key, const char *value) {
94 protocol_version_t ip_version = 0;
96 if (strcasecmp(key, "Chain") == 0)
98 else if (strcasecmp(key, "Chain6") == 0)
103 ip_chain_t temp = {0};
104 ip_chain_t *final, **list;
114 value_copy = strdup(value);
115 if (value_copy == NULL) {
117 ERROR("strdup failed: %s", sstrerror(errno, errbuf, sizeof(errbuf)));
122 * Time to fill the temp element
123 * Examine value string, it should look like:
124 * Chain[6] <table> <chain> [<comment|num> [name]]
127 /* set IPv4 or IPv6 */
128 temp.ip_version = ip_version;
130 /* Chain <table> <chain> [<comment|num> [name]] */
131 fields_num = strsplit(value_copy, fields, 4);
132 if (fields_num < 2) {
140 table_len = strlen(table) + 1;
141 if ((unsigned int)table_len > sizeof(temp.table)) {
142 ERROR("Table `%s' too long.", table);
146 sstrncpy(temp.table, table, table_len);
148 chain_len = strlen(chain) + 1;
149 if ((unsigned int)chain_len > sizeof(temp.chain)) {
150 ERROR("Chain `%s' too long.", chain);
154 sstrncpy(temp.chain, chain, chain_len);
156 if (fields_num >= 3) {
157 char *comment = fields[2];
158 int rule = atoi(comment);
161 temp.rule.num = rule;
162 temp.rule_type = RTYPE_NUM;
164 temp.rule.comment = strdup(comment);
165 if (temp.rule.comment == NULL) {
169 temp.rule_type = RTYPE_COMMENT;
172 temp.rule_type = RTYPE_COMMENT_ALL;
176 sstrncpy(temp.name, fields[3], sizeof(temp.name));
183 list = realloc(chain_list, (chain_num + 1) * sizeof(ip_chain_t *));
186 ERROR("realloc failed: %s", sstrerror(errno, errbuf, sizeof(errbuf)));
187 sfree(temp.rule.comment);
192 final = malloc(sizeof(*final));
195 ERROR("malloc failed: %s", sstrerror(errno, errbuf, sizeof(errbuf)));
196 sfree(temp.rule.comment);
199 memcpy(final, &temp, sizeof(temp));
200 chain_list[chain_num] = final;
203 DEBUG("Chain #%i: table = %s; chain = %s;", chain_num, final->table,
207 } /* int iptables_config */
209 static int submit6_match(const struct ip6t_entry_match *match,
210 const struct ip6t_entry *entry,
211 const ip_chain_t *chain, int rule_num) {
213 value_list_t vl = VALUE_LIST_INIT;
215 /* Select the rules to collect */
216 if (chain->rule_type == RTYPE_NUM) {
217 if (chain->rule.num != rule_num)
220 if (strcmp(match->u.user.name, "comment") != 0)
222 if ((chain->rule_type == RTYPE_COMMENT) &&
223 (strcmp(chain->rule.comment, (char *)match->data) != 0))
227 sstrncpy(vl.plugin, "ip6tables", sizeof(vl.plugin));
229 status = snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s-%s",
230 chain->table, chain->chain);
231 if ((status < 1) || ((unsigned int)status >= sizeof(vl.plugin_instance)))
234 if (chain->name[0] != '\0') {
235 sstrncpy(vl.type_instance, chain->name, sizeof(vl.type_instance));
237 if (chain->rule_type == RTYPE_NUM)
238 snprintf(vl.type_instance, sizeof(vl.type_instance), "%i",
241 sstrncpy(vl.type_instance, (char *)match->data, sizeof(vl.type_instance));
244 sstrncpy(vl.type, "ipt_bytes", sizeof(vl.type));
245 vl.values = &(value_t){.derive = (derive_t)entry->counters.bcnt};
247 plugin_dispatch_values(&vl);
249 sstrncpy(vl.type, "ipt_packets", sizeof(vl.type));
250 vl.values = &(value_t){.derive = (derive_t)entry->counters.pcnt};
251 plugin_dispatch_values(&vl);
254 } /* int submit6_match */
256 /* This needs to return `int' for IPT_MATCH_ITERATE to work. */
257 static int submit_match(const struct ipt_entry_match *match,
258 const struct ipt_entry *entry, const ip_chain_t *chain,
261 value_list_t vl = VALUE_LIST_INIT;
263 /* Select the rules to collect */
264 if (chain->rule_type == RTYPE_NUM) {
265 if (chain->rule.num != rule_num)
268 if (strcmp(match->u.user.name, "comment") != 0)
270 if ((chain->rule_type == RTYPE_COMMENT) &&
271 (strcmp(chain->rule.comment, (char *)match->data) != 0))
275 sstrncpy(vl.plugin, "iptables", sizeof(vl.plugin));
277 status = snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s-%s",
278 chain->table, chain->chain);
279 if ((status < 1) || ((unsigned int)status >= sizeof(vl.plugin_instance)))
282 if (chain->name[0] != '\0') {
283 sstrncpy(vl.type_instance, chain->name, sizeof(vl.type_instance));
285 if (chain->rule_type == RTYPE_NUM)
286 snprintf(vl.type_instance, sizeof(vl.type_instance), "%i",
289 sstrncpy(vl.type_instance, (char *)match->data, sizeof(vl.type_instance));
292 sstrncpy(vl.type, "ipt_bytes", sizeof(vl.type));
293 vl.values = &(value_t){.derive = (derive_t)entry->counters.bcnt};
295 plugin_dispatch_values(&vl);
297 sstrncpy(vl.type, "ipt_packets", sizeof(vl.type));
298 vl.values = &(value_t){.derive = (derive_t)entry->counters.pcnt};
299 plugin_dispatch_values(&vl);
302 } /* int submit_match */
304 /* ipv6 submit_chain */
305 static void submit6_chain(ip6tc_handle_t *handle, ip_chain_t *chain) {
306 const struct ip6t_entry *entry;
309 /* Find first rule for chain and use the iterate macro */
310 entry = ip6tc_first_rule(chain->chain, handle);
312 DEBUG("ip6tc_first_rule failed: %s", ip6tc_strerror(errno));
318 if (chain->rule_type == RTYPE_NUM) {
319 submit6_match(NULL, entry, chain, rule_num);
321 IP6T_MATCH_ITERATE(entry, submit6_match, entry, chain, rule_num);
324 entry = ip6tc_next_rule(entry, handle);
326 } /* while (entry) */
329 /* ipv4 submit_chain */
330 static void submit_chain(iptc_handle_t *handle, ip_chain_t *chain) {
331 const struct ipt_entry *entry;
334 /* Find first rule for chain and use the iterate macro */
335 entry = iptc_first_rule(chain->chain, handle);
337 DEBUG("iptc_first_rule failed: %s", iptc_strerror(errno));
343 if (chain->rule_type == RTYPE_NUM) {
344 submit_match(NULL, entry, chain, rule_num);
346 IPT_MATCH_ITERATE(entry, submit_match, entry, chain, rule_num);
349 entry = iptc_next_rule(entry, handle);
351 } /* while (entry) */
354 static int iptables_read(void) {
355 int num_failures = 0;
358 /* Init the iptc handle structure and query the correct table */
359 for (int i = 0; i < chain_num; i++) {
360 chain = chain_list[i];
363 DEBUG("iptables plugin: chain == NULL");
367 if (chain->ip_version == IPV4) {
368 #ifdef HAVE_IPTC_HANDLE_T
369 iptc_handle_t _handle;
370 iptc_handle_t *handle = &_handle;
372 *handle = iptc_init(chain->table);
374 iptc_handle_t *handle;
375 handle = iptc_init(chain->table);
379 ERROR("iptables plugin: iptc_init (%s) failed: %s", chain->table,
380 iptc_strerror(errno));
385 submit_chain(handle, chain);
387 } else if (chain->ip_version == IPV6) {
388 #ifdef HAVE_IP6TC_HANDLE_T
389 ip6tc_handle_t _handle;
390 ip6tc_handle_t *handle = &_handle;
392 *handle = ip6tc_init(chain->table);
394 ip6tc_handle_t *handle;
395 handle = ip6tc_init(chain->table);
398 ERROR("iptables plugin: ip6tc_init (%s) failed: %s", chain->table,
399 ip6tc_strerror(errno));
404 submit6_chain(handle, chain);
408 } /* for (i = 0 .. chain_num) */
410 return (num_failures < chain_num) ? 0 : -1;
411 } /* int iptables_read */
413 static int iptables_shutdown(void) {
414 for (int i = 0; i < chain_num; i++) {
415 if ((chain_list[i] != NULL) && (chain_list[i]->rule_type == RTYPE_COMMENT))
416 sfree(chain_list[i]->rule.comment);
417 sfree(chain_list[i]);
422 } /* int iptables_shutdown */
424 static int iptables_init(void) {
425 #if defined(HAVE_SYS_CAPABILITY_H) && defined(CAP_NET_ADMIN)
426 if (check_capability(CAP_NET_ADMIN) != 0) {
428 WARNING("iptables plugin: Running collectd as root, but the "
429 "CAP_NET_ADMIN capability is missing. The plugin's read "
430 "function will probably fail. Is your init system dropping "
433 WARNING("iptables plugin: collectd doesn't have the CAP_NET_ADMIN "
434 "capability. If you don't want to run collectd as root, try "
435 "running \"setcap cap_net_admin=ep\" on the collectd binary.");
439 } /* int iptables_init */
441 void module_register(void) {
442 plugin_register_config("iptables", iptables_config, config_keys,
444 plugin_register_init("iptables", iptables_init);
445 plugin_register_read("iptables", iptables_read);
446 plugin_register_shutdown("iptables", iptables_shutdown);
447 } /* void module_register */