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"
32 #include <sys/socket.h>
35 # include "libiptc/libiptc.h"
36 # include "libiptc/libip6tc.h"
38 # include <libiptc/libiptc.h>
39 # include <libiptc/libip6tc.h>
43 * (Module-)Global variables
47 * Config format should be `Chain table chainname',
48 * e. g. `Chain mangle incoming'
50 static const char *config_keys[] =
55 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
57 Each table/chain combo that will be queried goes into this list
60 enum protocol_version_e
65 typedef enum protocol_version_e protocol_version_t;
67 #ifndef XT_TABLE_MAXNAMELEN
68 # define XT_TABLE_MAXNAMELEN 32
71 protocol_version_t ip_version;
72 char table[XT_TABLE_MAXNAMELEN];
73 char chain[XT_TABLE_MAXNAMELEN];
88 static ip_chain_t **chain_list = NULL;
89 static int chain_num = 0;
91 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)
101 if (( ip_version == IPV4 ) || ( ip_version == IPV6 ))
103 ip_chain_t temp, *final, **list;
113 memset (&temp, 0, sizeof (temp));
115 value_copy = strdup (value);
116 if (value_copy == NULL)
119 ERROR ("strdup failed: %s",
120 sstrerror (errno, errbuf, sizeof (errbuf)));
125 * Time to fill the temp element
126 * Examine value string, it should look like:
127 * Chain[6] <table> <chain> [<comment|num> [name]]
130 /* set IPv4 or IPv6 */
131 temp.ip_version = ip_version;
133 /* Chain <table> <chain> [<comment|num> [name]] */
134 fields_num = strsplit (value_copy, fields, 4);
144 table_len = strlen (table) + 1;
145 if ((unsigned int)table_len > sizeof(temp.table))
147 ERROR ("Table `%s' too long.", table);
151 sstrncpy (temp.table, table, table_len);
153 chain_len = strlen (chain) + 1;
154 if ((unsigned int)chain_len > sizeof(temp.chain))
156 ERROR ("Chain `%s' too long.", chain);
160 sstrncpy (temp.chain, chain, chain_len);
164 char *comment = fields[2];
165 int rule = atoi (comment);
169 temp.rule.num = rule;
170 temp.rule_type = RTYPE_NUM;
174 temp.rule.comment = strdup (comment);
175 if (temp.rule.comment == NULL)
180 temp.rule_type = RTYPE_COMMENT;
185 temp.rule_type = RTYPE_COMMENT_ALL;
189 sstrncpy (temp.name, fields[3], sizeof (temp.name));
196 list = (ip_chain_t **) realloc (chain_list, (chain_num + 1) * sizeof (ip_chain_t *));
200 ERROR ("realloc failed: %s",
201 sstrerror (errno, errbuf, sizeof (errbuf)));
206 final = (ip_chain_t *) malloc( sizeof(temp) );
210 ERROR ("malloc failed: %s",
211 sstrerror (errno, errbuf, sizeof (errbuf)));
214 memcpy (final, &temp, sizeof (temp));
215 chain_list[chain_num] = final;
218 DEBUG ("Chain #%i: table = %s; chain = %s;", chain_num, final->table, final->chain);
226 } /* int iptables_config */
228 static int submit6_match (const struct ip6t_entry_match *match,
229 const struct ip6t_entry *entry,
230 const ip_chain_t *chain,
235 value_list_t vl = VALUE_LIST_INIT;
237 /* Select the rules to collect */
238 if (chain->rule_type == RTYPE_NUM)
240 if (chain->rule.num != rule_num)
245 if (strcmp (match->u.user.name, "comment") != 0)
247 if ((chain->rule_type == RTYPE_COMMENT)
248 && (strcmp (chain->rule.comment, (char *) match->data) != 0))
254 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
255 sstrncpy (vl.plugin, "ip6tables", sizeof (vl.plugin));
257 status = ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
258 "%s-%s", chain->table, chain->chain);
259 if ((status < 1) || ((unsigned int)status >= sizeof (vl.plugin_instance)))
262 if (chain->name[0] != '\0')
264 sstrncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
268 if (chain->rule_type == RTYPE_NUM)
269 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
270 "%i", chain->rule.num);
272 sstrncpy (vl.type_instance, (char *) match->data,
273 sizeof (vl.type_instance));
276 sstrncpy (vl.type, "ipt_bytes", sizeof (vl.type));
277 values[0].counter = (counter_t) entry->counters.bcnt;
278 plugin_dispatch_values (&vl);
280 sstrncpy (vl.type, "ipt_packets", sizeof (vl.type));
281 values[0].counter = (counter_t) entry->counters.pcnt;
282 plugin_dispatch_values (&vl);
285 } /* int submit_match */
288 /* This needs to return `int' for IPT_MATCH_ITERATE to work. */
289 static int submit_match (const struct ipt_entry_match *match,
290 const struct ipt_entry *entry,
291 const ip_chain_t *chain,
296 value_list_t vl = VALUE_LIST_INIT;
298 /* Select the rules to collect */
299 if (chain->rule_type == RTYPE_NUM)
301 if (chain->rule.num != rule_num)
306 if (strcmp (match->u.user.name, "comment") != 0)
308 if ((chain->rule_type == RTYPE_COMMENT)
309 && (strcmp (chain->rule.comment, (char *) match->data) != 0))
315 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
316 sstrncpy (vl.plugin, "iptables", sizeof (vl.plugin));
318 status = ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
319 "%s-%s", chain->table, chain->chain);
320 if ((status < 1) || ((unsigned int)status >= sizeof (vl.plugin_instance)))
323 if (chain->name[0] != '\0')
325 sstrncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
329 if (chain->rule_type == RTYPE_NUM)
330 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
331 "%i", chain->rule.num);
333 sstrncpy (vl.type_instance, (char *) match->data,
334 sizeof (vl.type_instance));
337 sstrncpy (vl.type, "ipt_bytes", sizeof (vl.type));
338 values[0].counter = (counter_t) entry->counters.bcnt;
339 plugin_dispatch_values (&vl);
341 sstrncpy (vl.type, "ipt_packets", sizeof (vl.type));
342 values[0].counter = (counter_t) entry->counters.pcnt;
343 plugin_dispatch_values (&vl);
346 } /* int submit_match */
349 /* ipv6 submit_chain */
350 static void submit6_chain( ip6tc_handle_t *handle, ip_chain_t *chain )
352 const struct ip6t_entry *entry;
355 /* Find first rule for chain and use the iterate macro */
356 entry = ip6tc_first_rule( chain->chain, handle );
359 DEBUG ("ip6tc_first_rule failed: %s", ip6tc_strerror (errno));
366 if (chain->rule_type == RTYPE_NUM)
368 submit6_match (NULL, entry, chain, rule_num);
372 IP6T_MATCH_ITERATE( entry, submit6_match, entry, chain, rule_num );
375 entry = ip6tc_next_rule( entry, handle );
377 } /* while (entry) */
381 /* ipv4 submit_chain */
382 static void submit_chain( iptc_handle_t *handle, ip_chain_t *chain )
384 const struct ipt_entry *entry;
387 /* Find first rule for chain and use the iterate macro */
388 entry = iptc_first_rule( chain->chain, handle );
391 DEBUG ("iptc_first_rule failed: %s", iptc_strerror (errno));
398 if (chain->rule_type == RTYPE_NUM)
400 submit_match (NULL, entry, chain, rule_num);
404 IPT_MATCH_ITERATE( entry, submit_match, entry, chain, rule_num );
407 entry = iptc_next_rule( entry, handle );
409 } /* while (entry) */
413 static int iptables_read (void)
416 int num_failures = 0;
419 /* Init the iptc handle structure and query the correct table */
420 for (i = 0; i < chain_num; i++)
422 chain = chain_list[i];
426 DEBUG ("iptables plugin: chain == NULL");
430 if ( chain->ip_version == IPV4 )
432 iptc_handle_t handle;
433 handle = iptc_init (chain->table);
437 ERROR ("iptables plugin: iptc_init (%s) failed: %s",
438 chain->table, iptc_strerror (errno));
443 submit_chain (&handle, chain);
446 else if ( chain->ip_version == IPV6 )
448 ip6tc_handle_t handle;
449 handle = ip6tc_init (chain->table);
453 ERROR ("iptables plugin: ip6tc_init (%s) failed: %s",
454 chain->table, ip6tc_strerror (errno));
459 submit6_chain (&handle, chain);
460 ip6tc_free (&handle);
464 } /* for (i = 0 .. chain_num) */
466 return ((num_failures < chain_num) ? 0 : -1);
467 } /* int iptables_read */
469 static int iptables_shutdown (void)
473 for (i = 0; i < chain_num; i++)
475 if ((chain_list[i] != NULL) && (chain_list[i]->rule_type == RTYPE_COMMENT))
477 sfree (chain_list[i]->rule.comment);
479 sfree (chain_list[i]);
484 } /* int iptables_shutdown */
486 void module_register (void)
488 plugin_register_config ("iptables", iptables_config,
489 config_keys, config_keys_num);
490 plugin_register_read ("iptables", iptables_read);
491 plugin_register_shutdown ("iptables", iptables_shutdown);
492 } /* void module_register */
495 * vim:shiftwidth=4:softtabstop=4:tabstop=8