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 "owniptc/libiptc.h"
36 # include "owniptc/libip6tc.h"
38 # include <libiptc/libiptc.h>
39 # include <libiptc/libip6tc.h>
43 * iptc_handle_t was available before libiptc was officially available as a
44 * shared library. Note, that when the shared lib was introduced, the API and
45 * ABI have changed slightly:
46 * 'iptc_handle_t' used to be 'struct iptc_handle *' and most functions used
47 * 'iptc_handle_t *' as an argument. Now, most functions use 'struct
48 * iptc_handle *' (thus removing one level of pointer indirection).
50 * HAVE_IPTC_HANDLE_T is used to determine which API ought to be used. While
51 * this is somewhat hacky, I didn't find better way to solve that :-/
54 #ifndef HAVE_IPTC_HANDLE_T
55 typedef struct iptc_handle iptc_handle_t;
57 #ifndef HAVE_IP6TC_HANDLE_T
58 typedef struct ip6tc_handle ip6tc_handle_t;
62 * (Module-)Global variables
66 * Config format should be `Chain table chainname',
67 * e. g. `Chain mangle incoming'
69 static const char *config_keys[] =
74 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
76 Each table/chain combo that will be queried goes into this list
79 enum protocol_version_e
84 typedef enum protocol_version_e protocol_version_t;
86 #ifndef XT_TABLE_MAXNAMELEN
87 # define XT_TABLE_MAXNAMELEN 32
90 protocol_version_t ip_version;
91 char table[XT_TABLE_MAXNAMELEN];
92 char chain[XT_TABLE_MAXNAMELEN];
107 static ip_chain_t **chain_list = NULL;
108 static int chain_num = 0;
110 static int iptables_config (const char *key, const char *value)
113 protocol_version_t ip_version = 0;
115 if (strcasecmp (key, "Chain") == 0)
117 else if (strcasecmp (key, "Chain6") == 0)
120 if (( ip_version == IPV4 ) || ( ip_version == IPV6 ))
122 ip_chain_t temp, *final, **list;
132 memset (&temp, 0, sizeof (temp));
134 value_copy = strdup (value);
135 if (value_copy == NULL)
138 ERROR ("strdup failed: %s",
139 sstrerror (errno, errbuf, sizeof (errbuf)));
144 * Time to fill the temp element
145 * Examine value string, it should look like:
146 * Chain[6] <table> <chain> [<comment|num> [name]]
149 /* set IPv4 or IPv6 */
150 temp.ip_version = ip_version;
152 /* Chain <table> <chain> [<comment|num> [name]] */
153 fields_num = strsplit (value_copy, fields, 4);
163 table_len = strlen (table) + 1;
164 if ((unsigned int)table_len > sizeof(temp.table))
166 ERROR ("Table `%s' too long.", table);
170 sstrncpy (temp.table, table, table_len);
172 chain_len = strlen (chain) + 1;
173 if ((unsigned int)chain_len > sizeof(temp.chain))
175 ERROR ("Chain `%s' too long.", chain);
179 sstrncpy (temp.chain, chain, chain_len);
183 char *comment = fields[2];
184 int rule = atoi (comment);
188 temp.rule.num = rule;
189 temp.rule_type = RTYPE_NUM;
193 temp.rule.comment = strdup (comment);
194 if (temp.rule.comment == NULL)
199 temp.rule_type = RTYPE_COMMENT;
204 temp.rule_type = RTYPE_COMMENT_ALL;
208 sstrncpy (temp.name, fields[3], sizeof (temp.name));
215 list = (ip_chain_t **) realloc (chain_list, (chain_num + 1) * sizeof (ip_chain_t *));
219 ERROR ("realloc failed: %s",
220 sstrerror (errno, errbuf, sizeof (errbuf)));
225 final = (ip_chain_t *) malloc( sizeof(temp) );
229 ERROR ("malloc failed: %s",
230 sstrerror (errno, errbuf, sizeof (errbuf)));
233 memcpy (final, &temp, sizeof (temp));
234 chain_list[chain_num] = final;
237 DEBUG ("Chain #%i: table = %s; chain = %s;", chain_num, final->table, final->chain);
245 } /* int iptables_config */
247 static int submit6_match (const struct ip6t_entry_match *match,
248 const struct ip6t_entry *entry,
249 const ip_chain_t *chain,
254 value_list_t vl = VALUE_LIST_INIT;
256 /* Select the rules to collect */
257 if (chain->rule_type == RTYPE_NUM)
259 if (chain->rule.num != rule_num)
264 if (strcmp (match->u.user.name, "comment") != 0)
266 if ((chain->rule_type == RTYPE_COMMENT)
267 && (strcmp (chain->rule.comment, (char *) match->data) != 0))
273 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
274 sstrncpy (vl.plugin, "ip6tables", sizeof (vl.plugin));
276 status = ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
277 "%s-%s", chain->table, chain->chain);
278 if ((status < 1) || ((unsigned int)status >= sizeof (vl.plugin_instance)))
281 if (chain->name[0] != '\0')
283 sstrncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
287 if (chain->rule_type == RTYPE_NUM)
288 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
289 "%i", chain->rule.num);
291 sstrncpy (vl.type_instance, (char *) match->data,
292 sizeof (vl.type_instance));
295 sstrncpy (vl.type, "ipt_bytes", sizeof (vl.type));
296 values[0].counter = (counter_t) entry->counters.bcnt;
297 plugin_dispatch_values (&vl);
299 sstrncpy (vl.type, "ipt_packets", sizeof (vl.type));
300 values[0].counter = (counter_t) entry->counters.pcnt;
301 plugin_dispatch_values (&vl);
304 } /* int submit_match */
307 /* This needs to return `int' for IPT_MATCH_ITERATE to work. */
308 static int submit_match (const struct ipt_entry_match *match,
309 const struct ipt_entry *entry,
310 const ip_chain_t *chain,
315 value_list_t vl = VALUE_LIST_INIT;
317 /* Select the rules to collect */
318 if (chain->rule_type == RTYPE_NUM)
320 if (chain->rule.num != rule_num)
325 if (strcmp (match->u.user.name, "comment") != 0)
327 if ((chain->rule_type == RTYPE_COMMENT)
328 && (strcmp (chain->rule.comment, (char *) match->data) != 0))
334 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
335 sstrncpy (vl.plugin, "iptables", sizeof (vl.plugin));
337 status = ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
338 "%s-%s", chain->table, chain->chain);
339 if ((status < 1) || ((unsigned int)status >= sizeof (vl.plugin_instance)))
342 if (chain->name[0] != '\0')
344 sstrncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
348 if (chain->rule_type == RTYPE_NUM)
349 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
350 "%i", chain->rule.num);
352 sstrncpy (vl.type_instance, (char *) match->data,
353 sizeof (vl.type_instance));
356 sstrncpy (vl.type, "ipt_bytes", sizeof (vl.type));
357 values[0].counter = (counter_t) entry->counters.bcnt;
358 plugin_dispatch_values (&vl);
360 sstrncpy (vl.type, "ipt_packets", sizeof (vl.type));
361 values[0].counter = (counter_t) entry->counters.pcnt;
362 plugin_dispatch_values (&vl);
365 } /* int submit_match */
368 /* ipv6 submit_chain */
369 static void submit6_chain( ip6tc_handle_t *handle, ip_chain_t *chain )
371 const struct ip6t_entry *entry;
374 /* Find first rule for chain and use the iterate macro */
375 entry = ip6tc_first_rule( chain->chain, handle );
378 DEBUG ("ip6tc_first_rule failed: %s", ip6tc_strerror (errno));
385 if (chain->rule_type == RTYPE_NUM)
387 submit6_match (NULL, entry, chain, rule_num);
391 IP6T_MATCH_ITERATE( entry, submit6_match, entry, chain, rule_num );
394 entry = ip6tc_next_rule( entry, handle );
396 } /* while (entry) */
400 /* ipv4 submit_chain */
401 static void submit_chain( iptc_handle_t *handle, ip_chain_t *chain )
403 const struct ipt_entry *entry;
406 /* Find first rule for chain and use the iterate macro */
407 entry = iptc_first_rule( chain->chain, handle );
410 DEBUG ("iptc_first_rule failed: %s", iptc_strerror (errno));
417 if (chain->rule_type == RTYPE_NUM)
419 submit_match (NULL, entry, chain, rule_num);
423 IPT_MATCH_ITERATE( entry, submit_match, entry, chain, rule_num );
426 entry = iptc_next_rule( entry, handle );
428 } /* while (entry) */
432 static int iptables_read (void)
435 int num_failures = 0;
438 /* Init the iptc handle structure and query the correct table */
439 for (i = 0; i < chain_num; i++)
441 chain = chain_list[i];
445 DEBUG ("iptables plugin: chain == NULL");
449 if ( chain->ip_version == IPV4 )
451 #ifdef HAVE_IPTC_HANDLE_T
452 iptc_handle_t _handle;
453 iptc_handle_t *handle = &_handle;
455 *handle = iptc_init (chain->table);
457 iptc_handle_t *handle;
458 handle = iptc_init (chain->table);
463 ERROR ("iptables plugin: iptc_init (%s) failed: %s",
464 chain->table, iptc_strerror (errno));
469 submit_chain (handle, chain);
472 else if ( chain->ip_version == IPV6 )
474 #ifdef HAVE_IP6TC_HANDLE_T
475 ip6tc_handle_t _handle;
476 ip6tc_handle_t *handle = &_handle;
478 *handle = ip6tc_init (chain->table);
480 ip6tc_handle_t *handle;
481 handle = ip6tc_init (chain->table);
486 ERROR ("iptables plugin: ip6tc_init (%s) failed: %s",
487 chain->table, ip6tc_strerror (errno));
492 submit6_chain (handle, chain);
497 } /* for (i = 0 .. chain_num) */
499 return ((num_failures < chain_num) ? 0 : -1);
500 } /* int iptables_read */
502 static int iptables_shutdown (void)
506 for (i = 0; i < chain_num; i++)
508 if ((chain_list[i] != NULL) && (chain_list[i]->rule_type == RTYPE_COMMENT))
510 sfree (chain_list[i]->rule.comment);
512 sfree (chain_list[i]);
517 } /* int iptables_shutdown */
519 void module_register (void)
521 plugin_register_config ("iptables", iptables_config,
522 config_keys, config_keys_num);
523 plugin_register_read ("iptables", iptables_read);
524 plugin_register_shutdown ("iptables", iptables_shutdown);
525 } /* void module_register */
528 * vim:shiftwidth=4:softtabstop=4:tabstop=8