2 * collectd - src/iptables.c
3 * Copyright (C) 2007 Sjoerd van der Berg
4 * Copyright (C) 2007 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 verplant.org>
24 * Marco Chiappero <marco at absence.it>
30 #include "configfile.h"
33 # include "libiptc/libiptc.h"
34 # include "libiptc/libip6tc.h"
36 # include <libiptc/libiptc.h>
37 # include <libiptc/libip6tc.h>
41 * (Module-)Global variables
45 * Config format should be `Chain table chainname',
46 * e. g. `Chain mangle incoming'
48 static const char *config_keys[] =
53 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
55 Each table/chain combo that will be queried goes into this list
58 enum protocol_version_e
63 typedef enum protocol_version_e protocol_version_t;
65 #ifndef XT_TABLE_MAXNAMELEN
66 # define XT_TABLE_MAXNAMELEN 32
69 protocol_version_t ip_version;
70 char table[XT_TABLE_MAXNAMELEN];
71 char chain[XT_TABLE_MAXNAMELEN];
86 static ip_chain_t **chain_list = NULL;
87 static int chain_num = 0;
89 static int iptables_config (const char *key, const char *value)
92 protocol_version_t ip_version = 0;
94 if (strcasecmp (key, "Chain") == 0)
96 else if (strcasecmp (key, "Chain6") == 0)
99 if (( ip_version == IPV4 ) || ( ip_version == IPV6 ))
101 ip_chain_t temp, *final, **list;
111 memset (&temp, 0, sizeof (temp));
113 value_copy = strdup (value);
114 if (value_copy == NULL)
117 ERROR ("strdup failed: %s",
118 sstrerror (errno, errbuf, sizeof (errbuf)));
123 * Time to fill the temp element
124 * Examine value string, it should look like:
125 * Chain[6] <table> <chain> [<comment|num> [name]]
128 /* set IPv4 or IPv6 */
129 temp.ip_version = ip_version;
131 /* Chain <table> <chain> [<comment|num> [name]] */
132 fields_num = strsplit (value_copy, fields, 4);
142 table_len = strlen (table) + 1;
143 if ((unsigned int)table_len > sizeof(temp.table))
145 ERROR ("Table `%s' too long.", table);
149 sstrncpy (temp.table, table, table_len);
151 chain_len = strlen (chain) + 1;
152 if ((unsigned int)chain_len > sizeof(temp.chain))
154 ERROR ("Chain `%s' too long.", chain);
158 sstrncpy (temp.chain, chain, chain_len);
162 char *comment = fields[2];
163 int rule = atoi (comment);
167 temp.rule.num = rule;
168 temp.rule_type = RTYPE_NUM;
172 temp.rule.comment = strdup (comment);
173 if (temp.rule.comment == NULL)
178 temp.rule_type = RTYPE_COMMENT;
183 temp.rule_type = RTYPE_COMMENT_ALL;
187 sstrncpy (temp.name, fields[3], sizeof (temp.name));
194 list = (ip_chain_t **) realloc (chain_list, (chain_num + 1) * sizeof (ip_chain_t *));
198 ERROR ("realloc failed: %s",
199 sstrerror (errno, errbuf, sizeof (errbuf)));
204 final = (ip_chain_t *) malloc( sizeof(temp) );
208 ERROR ("malloc failed: %s",
209 sstrerror (errno, errbuf, sizeof (errbuf)));
212 memcpy (final, &temp, sizeof (temp));
213 chain_list[chain_num] = final;
216 DEBUG ("Chain #%i: table = %s; chain = %s;", chain_num, final->table, final->chain);
224 } /* int iptables_config */
226 static int submit6_match (const struct ip6t_entry_match *match,
227 const struct ip6t_entry *entry,
228 const ip_chain_t *chain,
233 value_list_t vl = VALUE_LIST_INIT;
235 /* Select the rules to collect */
236 if (chain->rule_type == RTYPE_NUM)
238 if (chain->rule.num != rule_num)
243 if (strcmp (match->u.user.name, "comment") != 0)
245 if ((chain->rule_type == RTYPE_COMMENT)
246 && (strcmp (chain->rule.comment, (char *) match->data) != 0))
252 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
253 sstrncpy (vl.plugin, "ip6tables", sizeof (vl.plugin));
255 status = ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
256 "%s-%s", chain->table, chain->chain);
257 if ((status < 1) || ((unsigned int)status >= sizeof (vl.plugin_instance)))
260 if (chain->name[0] != '\0')
262 sstrncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
266 if (chain->rule_type == RTYPE_NUM)
267 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
268 "%i", chain->rule.num);
270 sstrncpy (vl.type_instance, (char *) match->data,
271 sizeof (vl.type_instance));
274 sstrncpy (vl.type, "ipt_bytes", sizeof (vl.type));
275 values[0].counter = (counter_t) entry->counters.bcnt;
276 plugin_dispatch_values (&vl);
278 sstrncpy (vl.type, "ipt_packets", sizeof (vl.type));
279 values[0].counter = (counter_t) entry->counters.pcnt;
280 plugin_dispatch_values (&vl);
283 } /* int submit_match */
286 /* This needs to return `int' for IPT_MATCH_ITERATE to work. */
287 static int submit_match (const struct ipt_entry_match *match,
288 const struct ipt_entry *entry,
289 const ip_chain_t *chain,
294 value_list_t vl = VALUE_LIST_INIT;
296 /* Select the rules to collect */
297 if (chain->rule_type == RTYPE_NUM)
299 if (chain->rule.num != rule_num)
304 if (strcmp (match->u.user.name, "comment") != 0)
306 if ((chain->rule_type == RTYPE_COMMENT)
307 && (strcmp (chain->rule.comment, (char *) match->data) != 0))
313 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
314 sstrncpy (vl.plugin, "iptables", sizeof (vl.plugin));
316 status = ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
317 "%s-%s", chain->table, chain->chain);
318 if ((status < 1) || ((unsigned int)status >= sizeof (vl.plugin_instance)))
321 if (chain->name[0] != '\0')
323 sstrncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
327 if (chain->rule_type == RTYPE_NUM)
328 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
329 "%i", chain->rule.num);
331 sstrncpy (vl.type_instance, (char *) match->data,
332 sizeof (vl.type_instance));
335 sstrncpy (vl.type, "ipt_bytes", sizeof (vl.type));
336 values[0].counter = (counter_t) entry->counters.bcnt;
337 plugin_dispatch_values (&vl);
339 sstrncpy (vl.type, "ipt_packets", sizeof (vl.type));
340 values[0].counter = (counter_t) entry->counters.pcnt;
341 plugin_dispatch_values (&vl);
344 } /* int submit_match */
347 /* ipv6 submit_chain */
348 static void submit6_chain( ip6tc_handle_t *handle, ip_chain_t *chain )
350 const struct ip6t_entry *entry;
353 /* Find first rule for chain and use the iterate macro */
354 entry = ip6tc_first_rule( chain->chain, handle );
357 DEBUG ("ip6tc_first_rule failed: %s", ip6tc_strerror (errno));
364 if (chain->rule_type == RTYPE_NUM)
366 submit6_match (NULL, entry, chain, rule_num);
370 IP6T_MATCH_ITERATE( entry, submit6_match, entry, chain, rule_num );
373 entry = ip6tc_next_rule( entry, handle );
375 } /* while (entry) */
379 /* ipv4 submit_chain */
380 static void submit_chain( iptc_handle_t *handle, ip_chain_t *chain )
382 const struct ipt_entry *entry;
385 /* Find first rule for chain and use the iterate macro */
386 entry = iptc_first_rule( chain->chain, handle );
389 DEBUG ("iptc_first_rule failed: %s", iptc_strerror (errno));
396 if (chain->rule_type == RTYPE_NUM)
398 submit_match (NULL, entry, chain, rule_num);
402 IPT_MATCH_ITERATE( entry, submit_match, entry, chain, rule_num );
405 entry = iptc_next_rule( entry, handle );
407 } /* while (entry) */
411 static int iptables_read (void)
414 int num_failures = 0;
417 /* Init the iptc handle structure and query the correct table */
418 for (i = 0; i < chain_num; i++)
420 chain = chain_list[i];
424 DEBUG ("iptables plugin: chain == NULL");
428 if ( chain->ip_version == IPV4 )
430 iptc_handle_t handle;
431 handle = iptc_init (chain->table);
435 ERROR ("iptables plugin: iptc_init (%s) failed: %s",
436 chain->table, iptc_strerror (errno));
441 submit_chain (&handle, chain);
444 else if ( chain->ip_version == IPV6 )
446 ip6tc_handle_t handle;
447 handle = ip6tc_init (chain->table);
451 ERROR ("iptables plugin: ip6tc_init (%s) failed: %s",
452 chain->table, ip6tc_strerror (errno));
457 submit6_chain (&handle, chain);
458 ip6tc_free (&handle);
462 } /* for (i = 0 .. chain_num) */
464 return ((num_failures < chain_num) ? 0 : -1);
465 } /* int iptables_read */
467 static int iptables_shutdown (void)
471 for (i = 0; i < chain_num; i++)
473 if ((chain_list[i] != NULL) && (chain_list[i]->rule_type == RTYPE_COMMENT))
475 sfree (chain_list[i]->rule.comment);
477 sfree (chain_list[i]);
482 } /* int iptables_shutdown */
484 void module_register (void)
486 plugin_register_config ("iptables", iptables_config,
487 config_keys, config_keys_num);
488 plugin_register_read ("iptables", iptables_read);
489 plugin_register_shutdown ("iptables", iptables_shutdown);
490 } /* void module_register */
493 * vim:shiftwidth=4:softtabstop=4:tabstop=8