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
57 #ifndef XT_TABLE_MAXNAMELEN
58 # define XT_TABLE_MAXNAMELEN 32
66 char table[XT_TABLE_MAXNAMELEN];
67 char chain[XT_TABLE_MAXNAMELEN];
82 static ip_chain_t **chain_list = NULL;
83 static int chain_num = 0;
85 static int iptables_config (const char *key, const char *value)
88 enum { IPV4, IPV6 } ip_protocol;
90 if (strcasecmp (key, "Chain") == 0)
92 else if (strcasecmp (key, "Chain6") == 0)
95 if (( ip_protocol == IPV4 ) || ( ip_protocol == IPV6 ))
97 ip_chain_t temp, *final, **list;
107 memset (&temp, 0, sizeof (temp));
109 value_copy = strdup (value);
110 if (value_copy == NULL)
113 ERROR ("strdup failed: %s",
114 sstrerror (errno, errbuf, sizeof (errbuf)));
119 * Time to fill the temp element
120 * Examine value string, it should look like:
121 * Chain[6] <table> <chain> [<comment|num> [name]]
124 /* set IPv4 or IPv6 */
125 temp.ip_version = ip_protocol;
127 /* Chain <table> <chain> [<comment|num> [name]] */
128 fields_num = strsplit (value_copy, fields, 4);
138 table_len = strlen (table) + 1;
139 if ((unsigned int)table_len > sizeof(temp.table))
141 ERROR ("Table `%s' too long.", table);
145 sstrncpy (temp.table, table, table_len);
147 chain_len = strlen (chain) + 1;
148 if ((unsigned int)chain_len > sizeof(temp.chain))
150 ERROR ("Chain `%s' too long.", chain);
154 sstrncpy (temp.chain, chain, chain_len);
158 char *comment = fields[2];
159 int rule = atoi (comment);
163 temp.rule.num = rule;
164 temp.rule_type = RTYPE_NUM;
168 temp.rule.comment = strdup (comment);
169 if (temp.rule.comment == NULL)
174 temp.rule_type = RTYPE_COMMENT;
179 temp.rule_type = RTYPE_COMMENT_ALL;
183 sstrncpy (temp.name, fields[3], sizeof (temp.name));
190 list = (ip_chain_t **) realloc (chain_list, (chain_num + 1) * sizeof (ip_chain_t *));
194 ERROR ("realloc failed: %s",
195 sstrerror (errno, errbuf, sizeof (errbuf)));
200 final = (ip_chain_t *) malloc( sizeof(temp) );
204 ERROR ("malloc failed: %s",
205 sstrerror (errno, errbuf, sizeof (errbuf)));
208 memcpy (final, &temp, sizeof (temp));
209 chain_list[chain_num] = final;
212 DEBUG ("Chain #%i: table = %s; chain = %s;", chain_num, final->table, final->chain);
220 } /* int iptables_config */
222 static int submit6_match (const struct ip6t_entry_match *match,
223 const struct ip6t_entry *entry,
224 const ip_chain_t *chain,
229 value_list_t vl = VALUE_LIST_INIT;
231 /* Select the rules to collect */
232 if (chain->rule_type == RTYPE_NUM)
234 if (chain->rule.num != rule_num)
239 if (strcmp (match->u.user.name, "comment") != 0)
241 if ((chain->rule_type == RTYPE_COMMENT)
242 && (strcmp (chain->rule.comment, (char *) match->data) != 0))
248 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
249 sstrncpy (vl.plugin, "ip6tables", sizeof (vl.plugin));
251 status = ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
252 "%s-%s", chain->table, chain->chain);
253 if ((status < 1) || ((unsigned int)status >= sizeof (vl.plugin_instance)))
256 if (chain->name[0] != '\0')
258 sstrncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
262 if (chain->rule_type == RTYPE_NUM)
263 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
264 "%i", chain->rule.num);
266 sstrncpy (vl.type_instance, (char *) match->data,
267 sizeof (vl.type_instance));
270 sstrncpy (vl.type, "ipt_bytes", sizeof (vl.type));
271 values[0].counter = (counter_t) entry->counters.bcnt;
272 plugin_dispatch_values (&vl);
274 sstrncpy (vl.type, "ipt_packets", sizeof (vl.type));
275 values[0].counter = (counter_t) entry->counters.pcnt;
276 plugin_dispatch_values (&vl);
279 } /* int submit_match */
282 /* This needs to return `int' for IPT_MATCH_ITERATE to work. */
283 static int submit_match (const struct ipt_entry_match *match,
284 const struct ipt_entry *entry,
285 const ip_chain_t *chain,
290 value_list_t vl = VALUE_LIST_INIT;
292 /* Select the rules to collect */
293 if (chain->rule_type == RTYPE_NUM)
295 if (chain->rule.num != rule_num)
300 if (strcmp (match->u.user.name, "comment") != 0)
302 if ((chain->rule_type == RTYPE_COMMENT)
303 && (strcmp (chain->rule.comment, (char *) match->data) != 0))
309 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
310 sstrncpy (vl.plugin, "iptables", sizeof (vl.plugin));
312 status = ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
313 "%s-%s", chain->table, chain->chain);
314 if ((status < 1) || ((unsigned int)status >= sizeof (vl.plugin_instance)))
317 if (chain->name[0] != '\0')
319 sstrncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
323 if (chain->rule_type == RTYPE_NUM)
324 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
325 "%i", chain->rule.num);
327 sstrncpy (vl.type_instance, (char *) match->data,
328 sizeof (vl.type_instance));
331 sstrncpy (vl.type, "ipt_bytes", sizeof (vl.type));
332 values[0].counter = (counter_t) entry->counters.bcnt;
333 plugin_dispatch_values (&vl);
335 sstrncpy (vl.type, "ipt_packets", sizeof (vl.type));
336 values[0].counter = (counter_t) entry->counters.pcnt;
337 plugin_dispatch_values (&vl);
340 } /* int submit_match */
343 /* ipv6 submit_chain */
344 static void submit6_chain( ip6tc_handle_t *handle, ip_chain_t *chain )
346 const struct ip6t_entry *entry;
349 /* Find first rule for chain and use the iterate macro */
350 entry = ip6tc_first_rule( chain->chain, handle );
353 DEBUG ("ip6tc_first_rule failed: %s", ip6tc_strerror (errno));
360 if (chain->rule_type == RTYPE_NUM)
362 submit6_match (NULL, entry, chain, rule_num);
366 IP6T_MATCH_ITERATE( entry, submit6_match, entry, chain, rule_num );
369 entry = ip6tc_next_rule( entry, handle );
371 } /* while (entry) */
375 /* ipv4 submit_chain */
376 static void submit_chain( iptc_handle_t *handle, ip_chain_t *chain )
378 const struct ipt_entry *entry;
381 /* Find first rule for chain and use the iterate macro */
382 entry = iptc_first_rule( chain->chain, handle );
385 DEBUG ("iptc_first_rule failed: %s", iptc_strerror (errno));
392 if (chain->rule_type == RTYPE_NUM)
394 submit_match (NULL, entry, chain, rule_num);
398 IPT_MATCH_ITERATE( entry, submit_match, entry, chain, rule_num );
401 entry = iptc_next_rule( entry, handle );
403 } /* while (entry) */
407 static int iptables_read (void)
410 int num_failures = 0;
413 /* Init the iptc handle structure and query the correct table */
414 for (i = 0; i < chain_num; i++)
416 chain = chain_list[i];
420 DEBUG ("iptables plugin: chain == NULL");
424 if ( chain->ip_version == IPV4 )
426 iptc_handle_t handle;
427 handle = iptc_init (chain->table);
431 ERROR ("iptables plugin: iptc_init (%s) failed: %s",
432 chain->table, iptc_strerror (errno));
437 submit_chain (&handle, chain);
440 else if ( chain->ip_version == IPV6 )
442 ip6tc_handle_t handle;
443 handle = ip6tc_init (chain->table);
447 ERROR ("iptables plugin: ip6tc_init (%s) failed: %s",
448 chain->table, ip6tc_strerror (errno));
453 submit6_chain (&handle, chain);
454 ip6tc_free (&handle);
458 } /* for (i = 0 .. chain_num) */
460 return ((num_failures < chain_num) ? 0 : -1);
461 } /* int iptables_read */
463 static int iptables_shutdown (void)
467 for (i = 0; i < chain_num; i++)
469 if ((chain_list[i] != NULL) && (chain_list[i]->rule_type == RTYPE_COMMENT))
471 sfree (chain_list[i]->rule.comment);
473 sfree (chain_list[i]);
478 } /* int iptables_shutdown */
480 void module_register (void)
482 plugin_register_config ("iptables", iptables_config,
483 config_keys, config_keys_num);
484 plugin_register_read ("iptables", iptables_read);
485 plugin_register_shutdown ("iptables", iptables_shutdown);
486 } /* void module_register */
489 * vim:shiftwidth=4:softtabstop=4:tabstop=8