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>
30 #include "configfile.h"
32 #include <libiptc/libiptc.h>
33 #include <libiptc/libip6tc.h>
36 * iptc_handle_t was available before libiptc was officially available as a
37 * shared library. Note, that when the shared lib was introduced, the API and
38 * ABI have changed slightly:
39 * 'iptc_handle_t' used to be 'struct iptc_handle *' and most functions used
40 * 'iptc_handle_t *' as an argument. Now, most functions use 'struct
41 * iptc_handle *' (thus removing one level of pointer indirection).
43 * HAVE_IPTC_HANDLE_T is used to determine which API ought to be used. While
44 * this is somewhat hacky, I didn't find better way to solve that :-/
47 #ifndef HAVE_IPTC_HANDLE_T
48 typedef struct iptc_handle iptc_handle_t;
50 #ifndef HAVE_IP6TC_HANDLE_T
51 typedef struct ip6tc_handle ip6tc_handle_t;
55 * (Module-)Global variables
59 * Config format should be `Chain table chainname',
60 * e. g. `Chain mangle incoming'
62 static const char *config_keys[] =
67 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
68 enum protocol_version_e
73 typedef enum protocol_version_e protocol_version_t;
76 * Each table/chain combo that will be queried goes into this list
78 #ifndef XT_TABLE_MAXNAMELEN
79 # define XT_TABLE_MAXNAMELEN 32
82 protocol_version_t ip_version;
83 char table[XT_TABLE_MAXNAMELEN];
84 char chain[XT_TABLE_MAXNAMELEN];
99 static ip_chain_t **chain_list = NULL;
100 static int chain_num = 0;
102 static int iptables_config (const char *key, const char *value)
105 protocol_version_t ip_version = 0;
107 if (strcasecmp (key, "Chain") == 0)
109 else if (strcasecmp (key, "Chain6") == 0)
114 ip_chain_t temp = { 0 };
115 ip_chain_t *final, **list;
125 value_copy = strdup (value);
126 if (value_copy == NULL)
129 ERROR ("strdup failed: %s",
130 sstrerror (errno, errbuf, sizeof (errbuf)));
135 * Time to fill the temp element
136 * Examine value string, it should look like:
137 * Chain[6] <table> <chain> [<comment|num> [name]]
140 /* set IPv4 or IPv6 */
141 temp.ip_version = ip_version;
143 /* Chain <table> <chain> [<comment|num> [name]] */
144 fields_num = strsplit (value_copy, fields, 4);
154 table_len = strlen (table) + 1;
155 if ((unsigned int)table_len > sizeof(temp.table))
157 ERROR ("Table `%s' too long.", table);
161 sstrncpy (temp.table, table, table_len);
163 chain_len = strlen (chain) + 1;
164 if ((unsigned int)chain_len > sizeof(temp.chain))
166 ERROR ("Chain `%s' too long.", chain);
170 sstrncpy (temp.chain, chain, chain_len);
174 char *comment = fields[2];
175 int rule = atoi (comment);
179 temp.rule.num = rule;
180 temp.rule_type = RTYPE_NUM;
184 temp.rule.comment = strdup (comment);
185 if (temp.rule.comment == NULL)
190 temp.rule_type = RTYPE_COMMENT;
195 temp.rule_type = RTYPE_COMMENT_ALL;
199 sstrncpy (temp.name, fields[3], sizeof (temp.name));
206 list = realloc (chain_list, (chain_num + 1) * sizeof (ip_chain_t *));
210 ERROR ("realloc failed: %s",
211 sstrerror (errno, errbuf, sizeof (errbuf)));
212 sfree (temp.rule.comment);
217 final = malloc(sizeof (*final));
221 ERROR ("malloc failed: %s",
222 sstrerror (errno, errbuf, sizeof (errbuf)));
223 sfree (temp.rule.comment);
226 memcpy (final, &temp, sizeof (temp));
227 chain_list[chain_num] = final;
230 DEBUG ("Chain #%i: table = %s; chain = %s;", chain_num, final->table, final->chain);
233 } /* int iptables_config */
235 static int submit6_match (const struct ip6t_entry_match *match,
236 const struct ip6t_entry *entry,
237 const ip_chain_t *chain,
242 value_list_t vl = VALUE_LIST_INIT;
244 /* Select the rules to collect */
245 if (chain->rule_type == RTYPE_NUM)
247 if (chain->rule.num != rule_num)
252 if (strcmp (match->u.user.name, "comment") != 0)
254 if ((chain->rule_type == RTYPE_COMMENT)
255 && (strcmp (chain->rule.comment, (char *) match->data) != 0))
261 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
262 sstrncpy (vl.plugin, "ip6tables", sizeof (vl.plugin));
264 status = ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
265 "%s-%s", chain->table, chain->chain);
266 if ((status < 1) || ((unsigned int)status >= sizeof (vl.plugin_instance)))
269 if (chain->name[0] != '\0')
271 sstrncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
275 if (chain->rule_type == RTYPE_NUM)
276 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
277 "%i", chain->rule.num);
279 sstrncpy (vl.type_instance, (char *) match->data,
280 sizeof (vl.type_instance));
283 sstrncpy (vl.type, "ipt_bytes", sizeof (vl.type));
284 values[0].derive = (derive_t) entry->counters.bcnt;
285 plugin_dispatch_values (&vl);
287 sstrncpy (vl.type, "ipt_packets", sizeof (vl.type));
288 values[0].derive = (derive_t) entry->counters.pcnt;
289 plugin_dispatch_values (&vl);
292 } /* int submit_match */
295 /* This needs to return `int' for IPT_MATCH_ITERATE to work. */
296 static int submit_match (const struct ipt_entry_match *match,
297 const struct ipt_entry *entry,
298 const ip_chain_t *chain,
303 value_list_t vl = VALUE_LIST_INIT;
305 /* Select the rules to collect */
306 if (chain->rule_type == RTYPE_NUM)
308 if (chain->rule.num != rule_num)
313 if (strcmp (match->u.user.name, "comment") != 0)
315 if ((chain->rule_type == RTYPE_COMMENT)
316 && (strcmp (chain->rule.comment, (char *) match->data) != 0))
322 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
323 sstrncpy (vl.plugin, "iptables", sizeof (vl.plugin));
325 status = ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
326 "%s-%s", chain->table, chain->chain);
327 if ((status < 1) || ((unsigned int)status >= sizeof (vl.plugin_instance)))
330 if (chain->name[0] != '\0')
332 sstrncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
336 if (chain->rule_type == RTYPE_NUM)
337 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
338 "%i", chain->rule.num);
340 sstrncpy (vl.type_instance, (char *) match->data,
341 sizeof (vl.type_instance));
344 sstrncpy (vl.type, "ipt_bytes", sizeof (vl.type));
345 values[0].derive = (derive_t) entry->counters.bcnt;
346 plugin_dispatch_values (&vl);
348 sstrncpy (vl.type, "ipt_packets", sizeof (vl.type));
349 values[0].derive = (derive_t) entry->counters.pcnt;
350 plugin_dispatch_values (&vl);
353 } /* int submit_match */
356 /* ipv6 submit_chain */
357 static void submit6_chain (ip6tc_handle_t *handle, ip_chain_t *chain)
359 const struct ip6t_entry *entry;
362 /* Find first rule for chain and use the iterate macro */
363 entry = ip6tc_first_rule( chain->chain, handle );
366 DEBUG ("ip6tc_first_rule failed: %s", ip6tc_strerror (errno));
373 if (chain->rule_type == RTYPE_NUM)
375 submit6_match (NULL, entry, chain, rule_num);
379 IP6T_MATCH_ITERATE( entry, submit6_match, entry, chain, rule_num );
382 entry = ip6tc_next_rule( entry, handle );
384 } /* while (entry) */
388 /* ipv4 submit_chain */
389 static void submit_chain (iptc_handle_t *handle, ip_chain_t *chain)
391 const struct ipt_entry *entry;
394 /* Find first rule for chain and use the iterate macro */
395 entry = iptc_first_rule( chain->chain, handle );
398 DEBUG ("iptc_first_rule failed: %s", iptc_strerror (errno));
405 if (chain->rule_type == RTYPE_NUM)
407 submit_match (NULL, entry, chain, rule_num);
411 IPT_MATCH_ITERATE( entry, submit_match, entry, chain, rule_num );
414 entry = iptc_next_rule( entry, handle );
416 } /* while (entry) */
420 static int iptables_read (void)
423 int num_failures = 0;
426 /* Init the iptc handle structure and query the correct table */
427 for (i = 0; i < chain_num; i++)
429 chain = chain_list[i];
433 DEBUG ("iptables plugin: chain == NULL");
437 if ( chain->ip_version == IPV4 )
439 #ifdef HAVE_IPTC_HANDLE_T
440 iptc_handle_t _handle;
441 iptc_handle_t *handle = &_handle;
443 *handle = iptc_init (chain->table);
445 iptc_handle_t *handle;
446 handle = iptc_init (chain->table);
451 ERROR ("iptables plugin: iptc_init (%s) failed: %s",
452 chain->table, iptc_strerror (errno));
457 submit_chain (handle, chain);
460 else if ( chain->ip_version == IPV6 )
462 #ifdef HAVE_IP6TC_HANDLE_T
463 ip6tc_handle_t _handle;
464 ip6tc_handle_t *handle = &_handle;
466 *handle = ip6tc_init (chain->table);
468 ip6tc_handle_t *handle;
469 handle = ip6tc_init (chain->table);
473 ERROR ("iptables plugin: ip6tc_init (%s) failed: %s",
474 chain->table, ip6tc_strerror (errno));
479 submit6_chain (handle, chain);
484 } /* for (i = 0 .. chain_num) */
486 return ((num_failures < chain_num) ? 0 : -1);
487 } /* int iptables_read */
489 static int iptables_shutdown (void)
493 for (i = 0; i < chain_num; i++)
495 if ((chain_list[i] != NULL) && (chain_list[i]->rule_type == RTYPE_COMMENT))
496 sfree (chain_list[i]->rule.comment);
497 sfree (chain_list[i]);
502 } /* int iptables_shutdown */
504 void module_register (void)
506 plugin_register_config ("iptables", iptables_config,
507 config_keys, config_keys_num);
508 plugin_register_read ("iptables", iptables_read);
509 plugin_register_shutdown ("iptables", iptables_shutdown);
510 } /* void module_register */