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>
31 #include "configfile.h"
33 #include <libiptc/libiptc.h>
34 #include <libiptc/libip6tc.h>
36 #ifdef HAVE_SYS_CAPABILITY_H
37 # include <sys/capability.h>
41 * iptc_handle_t was available before libiptc was officially available as a
42 * shared library. Note, that when the shared lib was introduced, the API and
43 * ABI have changed slightly:
44 * 'iptc_handle_t' used to be 'struct iptc_handle *' and most functions used
45 * 'iptc_handle_t *' as an argument. Now, most functions use 'struct
46 * iptc_handle *' (thus removing one level of pointer indirection).
48 * HAVE_IPTC_HANDLE_T is used to determine which API ought to be used. While
49 * this is somewhat hacky, I didn't find better way to solve that :-/
52 #ifndef HAVE_IPTC_HANDLE_T
53 typedef struct iptc_handle iptc_handle_t;
55 #ifndef HAVE_IP6TC_HANDLE_T
56 typedef struct ip6tc_handle ip6tc_handle_t;
60 * (Module-)Global variables
64 * Config format should be `Chain table chainname',
65 * e. g. `Chain mangle incoming'
67 static const char *config_keys[] =
72 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
73 enum protocol_version_e
78 typedef enum protocol_version_e protocol_version_t;
81 * Each table/chain combo that will be queried goes into this list
83 #ifndef XT_TABLE_MAXNAMELEN
84 # define XT_TABLE_MAXNAMELEN 32
87 protocol_version_t ip_version;
88 char table[XT_TABLE_MAXNAMELEN];
89 char chain[XT_TABLE_MAXNAMELEN];
104 static ip_chain_t **chain_list = NULL;
105 static int chain_num = 0;
107 static int iptables_config (const char *key, const char *value)
110 protocol_version_t ip_version = 0;
112 if (strcasecmp (key, "Chain") == 0)
114 else if (strcasecmp (key, "Chain6") == 0)
119 ip_chain_t temp = { 0 };
120 ip_chain_t *final, **list;
130 value_copy = strdup (value);
131 if (value_copy == NULL)
134 ERROR ("strdup failed: %s",
135 sstrerror (errno, errbuf, sizeof (errbuf)));
140 * Time to fill the temp element
141 * Examine value string, it should look like:
142 * Chain[6] <table> <chain> [<comment|num> [name]]
145 /* set IPv4 or IPv6 */
146 temp.ip_version = ip_version;
148 /* Chain <table> <chain> [<comment|num> [name]] */
149 fields_num = strsplit (value_copy, fields, 4);
159 table_len = strlen (table) + 1;
160 if ((unsigned int)table_len > sizeof(temp.table))
162 ERROR ("Table `%s' too long.", table);
166 sstrncpy (temp.table, table, table_len);
168 chain_len = strlen (chain) + 1;
169 if ((unsigned int)chain_len > sizeof(temp.chain))
171 ERROR ("Chain `%s' too long.", chain);
175 sstrncpy (temp.chain, chain, chain_len);
179 char *comment = fields[2];
180 int rule = atoi (comment);
184 temp.rule.num = rule;
185 temp.rule_type = RTYPE_NUM;
189 temp.rule.comment = strdup (comment);
190 if (temp.rule.comment == NULL)
195 temp.rule_type = RTYPE_COMMENT;
200 temp.rule_type = RTYPE_COMMENT_ALL;
204 sstrncpy (temp.name, fields[3], sizeof (temp.name));
211 list = realloc (chain_list, (chain_num + 1) * sizeof (ip_chain_t *));
215 ERROR ("realloc failed: %s",
216 sstrerror (errno, errbuf, sizeof (errbuf)));
217 sfree (temp.rule.comment);
222 final = malloc(sizeof (*final));
226 ERROR ("malloc failed: %s",
227 sstrerror (errno, errbuf, sizeof (errbuf)));
228 sfree (temp.rule.comment);
231 memcpy (final, &temp, sizeof (temp));
232 chain_list[chain_num] = final;
235 DEBUG ("Chain #%i: table = %s; chain = %s;", chain_num, final->table, final->chain);
238 } /* int iptables_config */
240 static int submit6_match (const struct ip6t_entry_match *match,
241 const struct ip6t_entry *entry,
242 const ip_chain_t *chain,
247 value_list_t vl = VALUE_LIST_INIT;
249 /* Select the rules to collect */
250 if (chain->rule_type == RTYPE_NUM)
252 if (chain->rule.num != rule_num)
257 if (strcmp (match->u.user.name, "comment") != 0)
259 if ((chain->rule_type == RTYPE_COMMENT)
260 && (strcmp (chain->rule.comment, (char *) match->data) != 0))
266 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
267 sstrncpy (vl.plugin, "ip6tables", sizeof (vl.plugin));
269 status = ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
270 "%s-%s", chain->table, chain->chain);
271 if ((status < 1) || ((unsigned int)status >= sizeof (vl.plugin_instance)))
274 if (chain->name[0] != '\0')
276 sstrncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
280 if (chain->rule_type == RTYPE_NUM)
281 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
282 "%i", chain->rule.num);
284 sstrncpy (vl.type_instance, (char *) match->data,
285 sizeof (vl.type_instance));
288 sstrncpy (vl.type, "ipt_bytes", sizeof (vl.type));
289 values[0].derive = (derive_t) entry->counters.bcnt;
290 plugin_dispatch_values (&vl);
292 sstrncpy (vl.type, "ipt_packets", sizeof (vl.type));
293 values[0].derive = (derive_t) entry->counters.pcnt;
294 plugin_dispatch_values (&vl);
297 } /* int submit_match */
300 /* This needs to return `int' for IPT_MATCH_ITERATE to work. */
301 static int submit_match (const struct ipt_entry_match *match,
302 const struct ipt_entry *entry,
303 const ip_chain_t *chain,
308 value_list_t vl = VALUE_LIST_INIT;
310 /* Select the rules to collect */
311 if (chain->rule_type == RTYPE_NUM)
313 if (chain->rule.num != rule_num)
318 if (strcmp (match->u.user.name, "comment") != 0)
320 if ((chain->rule_type == RTYPE_COMMENT)
321 && (strcmp (chain->rule.comment, (char *) match->data) != 0))
327 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
328 sstrncpy (vl.plugin, "iptables", sizeof (vl.plugin));
330 status = ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
331 "%s-%s", chain->table, chain->chain);
332 if ((status < 1) || ((unsigned int)status >= sizeof (vl.plugin_instance)))
335 if (chain->name[0] != '\0')
337 sstrncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
341 if (chain->rule_type == RTYPE_NUM)
342 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
343 "%i", chain->rule.num);
345 sstrncpy (vl.type_instance, (char *) match->data,
346 sizeof (vl.type_instance));
349 sstrncpy (vl.type, "ipt_bytes", sizeof (vl.type));
350 values[0].derive = (derive_t) entry->counters.bcnt;
351 plugin_dispatch_values (&vl);
353 sstrncpy (vl.type, "ipt_packets", sizeof (vl.type));
354 values[0].derive = (derive_t) entry->counters.pcnt;
355 plugin_dispatch_values (&vl);
358 } /* int submit_match */
361 /* ipv6 submit_chain */
362 static void submit6_chain (ip6tc_handle_t *handle, ip_chain_t *chain)
364 const struct ip6t_entry *entry;
367 /* Find first rule for chain and use the iterate macro */
368 entry = ip6tc_first_rule( chain->chain, handle );
371 DEBUG ("ip6tc_first_rule failed: %s", ip6tc_strerror (errno));
378 if (chain->rule_type == RTYPE_NUM)
380 submit6_match (NULL, entry, chain, rule_num);
384 IP6T_MATCH_ITERATE( entry, submit6_match, entry, chain, rule_num );
387 entry = ip6tc_next_rule( entry, handle );
389 } /* while (entry) */
393 /* ipv4 submit_chain */
394 static void submit_chain (iptc_handle_t *handle, ip_chain_t *chain)
396 const struct ipt_entry *entry;
399 /* Find first rule for chain and use the iterate macro */
400 entry = iptc_first_rule( chain->chain, handle );
403 DEBUG ("iptc_first_rule failed: %s", iptc_strerror (errno));
410 if (chain->rule_type == RTYPE_NUM)
412 submit_match (NULL, entry, chain, rule_num);
416 IPT_MATCH_ITERATE( entry, submit_match, entry, chain, rule_num );
419 entry = iptc_next_rule( entry, handle );
421 } /* while (entry) */
425 static int iptables_read (void)
427 int num_failures = 0;
430 /* Init the iptc handle structure and query the correct table */
431 for (int i = 0; i < chain_num; i++)
433 chain = chain_list[i];
437 DEBUG ("iptables plugin: chain == NULL");
441 if ( chain->ip_version == IPV4 )
443 #ifdef HAVE_IPTC_HANDLE_T
444 iptc_handle_t _handle;
445 iptc_handle_t *handle = &_handle;
447 *handle = iptc_init (chain->table);
449 iptc_handle_t *handle;
450 handle = iptc_init (chain->table);
455 ERROR ("iptables plugin: iptc_init (%s) failed: %s",
456 chain->table, iptc_strerror (errno));
461 submit_chain (handle, chain);
464 else if ( chain->ip_version == IPV6 )
466 #ifdef HAVE_IP6TC_HANDLE_T
467 ip6tc_handle_t _handle;
468 ip6tc_handle_t *handle = &_handle;
470 *handle = ip6tc_init (chain->table);
472 ip6tc_handle_t *handle;
473 handle = ip6tc_init (chain->table);
477 ERROR ("iptables plugin: ip6tc_init (%s) failed: %s",
478 chain->table, ip6tc_strerror (errno));
483 submit6_chain (handle, chain);
488 } /* for (i = 0 .. chain_num) */
490 return ((num_failures < chain_num) ? 0 : -1);
491 } /* int iptables_read */
493 static int iptables_shutdown (void)
495 for (int i = 0; i < chain_num; i++)
497 if ((chain_list[i] != NULL) && (chain_list[i]->rule_type == RTYPE_COMMENT))
498 sfree (chain_list[i]->rule.comment);
499 sfree (chain_list[i]);
504 } /* int iptables_shutdown */
506 static int iptables_init (void)
508 #if defined(HAVE_SYS_CAPABILITY_H) && defined(CAP_NET_ADMIN)
509 if (check_capability (CAP_NET_ADMIN) != 0)
512 WARNING ("iptables plugin: Running collectd as root, but the "
513 "CAP_NET_ADMIN capability is missing. The plugin's read "
514 "function will probably fail. Is your init system dropping "
517 WARNING ("iptables plugin: collectd doesn't have the CAP_NET_ADMIN "
518 "capability. If you don't want to run collectd as root, try "
519 "running \"setcap cap_net_admin=ep\" on the collectd binary.");
523 } /* int iptables_init */
525 void module_register (void)
527 plugin_register_config ("iptables", iptables_config,
528 config_keys, config_keys_num);
529 plugin_register_init ("iptables", iptables_init);
530 plugin_register_read ("iptables", iptables_read);
531 plugin_register_shutdown ("iptables", iptables_shutdown);
532 } /* void module_register */