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, *final, **list;
124 memset (&temp, 0, sizeof (temp));
126 value_copy = strdup (value);
127 if (value_copy == NULL)
130 ERROR ("strdup failed: %s",
131 sstrerror (errno, errbuf, sizeof (errbuf)));
136 * Time to fill the temp element
137 * Examine value string, it should look like:
138 * Chain[6] <table> <chain> [<comment|num> [name]]
141 /* set IPv4 or IPv6 */
142 temp.ip_version = ip_version;
144 /* Chain <table> <chain> [<comment|num> [name]] */
145 fields_num = strsplit (value_copy, fields, 4);
155 table_len = strlen (table) + 1;
156 if ((unsigned int)table_len > sizeof(temp.table))
158 ERROR ("Table `%s' too long.", table);
162 sstrncpy (temp.table, table, table_len);
164 chain_len = strlen (chain) + 1;
165 if ((unsigned int)chain_len > sizeof(temp.chain))
167 ERROR ("Chain `%s' too long.", chain);
171 sstrncpy (temp.chain, chain, chain_len);
175 char *comment = fields[2];
176 int rule = atoi (comment);
180 temp.rule.num = rule;
181 temp.rule_type = RTYPE_NUM;
185 temp.rule.comment = strdup (comment);
186 if (temp.rule.comment == NULL)
191 temp.rule_type = RTYPE_COMMENT;
196 temp.rule_type = RTYPE_COMMENT_ALL;
200 sstrncpy (temp.name, fields[3], sizeof (temp.name));
207 list = (ip_chain_t **) realloc (chain_list, (chain_num + 1) * sizeof (ip_chain_t *));
211 ERROR ("realloc failed: %s",
212 sstrerror (errno, errbuf, sizeof (errbuf)));
213 sfree (temp.rule.comment);
218 final = (ip_chain_t *) malloc( sizeof(temp) );
222 ERROR ("malloc failed: %s",
223 sstrerror (errno, errbuf, sizeof (errbuf)));
224 sfree (temp.rule.comment);
227 memcpy (final, &temp, sizeof (temp));
228 chain_list[chain_num] = final;
231 DEBUG ("Chain #%i: table = %s; chain = %s;", chain_num, final->table, final->chain);
234 } /* int iptables_config */
236 static int submit6_match (const struct ip6t_entry_match *match,
237 const struct ip6t_entry *entry,
238 const ip_chain_t *chain,
243 value_list_t vl = VALUE_LIST_INIT;
245 /* Select the rules to collect */
246 if (chain->rule_type == RTYPE_NUM)
248 if (chain->rule.num != rule_num)
253 if (strcmp (match->u.user.name, "comment") != 0)
255 if ((chain->rule_type == RTYPE_COMMENT)
256 && (strcmp (chain->rule.comment, (char *) match->data) != 0))
262 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
263 sstrncpy (vl.plugin, "ip6tables", sizeof (vl.plugin));
265 status = ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
266 "%s-%s", chain->table, chain->chain);
267 if ((status < 1) || ((unsigned int)status >= sizeof (vl.plugin_instance)))
270 if (chain->name[0] != '\0')
272 sstrncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
276 if (chain->rule_type == RTYPE_NUM)
277 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
278 "%i", chain->rule.num);
280 sstrncpy (vl.type_instance, (char *) match->data,
281 sizeof (vl.type_instance));
284 sstrncpy (vl.type, "ipt_bytes", sizeof (vl.type));
285 values[0].derive = (derive_t) entry->counters.bcnt;
286 plugin_dispatch_values (&vl);
288 sstrncpy (vl.type, "ipt_packets", sizeof (vl.type));
289 values[0].derive = (derive_t) entry->counters.pcnt;
290 plugin_dispatch_values (&vl);
293 } /* int submit_match */
296 /* This needs to return `int' for IPT_MATCH_ITERATE to work. */
297 static int submit_match (const struct ipt_entry_match *match,
298 const struct ipt_entry *entry,
299 const ip_chain_t *chain,
304 value_list_t vl = VALUE_LIST_INIT;
306 /* Select the rules to collect */
307 if (chain->rule_type == RTYPE_NUM)
309 if (chain->rule.num != rule_num)
314 if (strcmp (match->u.user.name, "comment") != 0)
316 if ((chain->rule_type == RTYPE_COMMENT)
317 && (strcmp (chain->rule.comment, (char *) match->data) != 0))
323 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
324 sstrncpy (vl.plugin, "iptables", sizeof (vl.plugin));
326 status = ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
327 "%s-%s", chain->table, chain->chain);
328 if ((status < 1) || ((unsigned int)status >= sizeof (vl.plugin_instance)))
331 if (chain->name[0] != '\0')
333 sstrncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
337 if (chain->rule_type == RTYPE_NUM)
338 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
339 "%i", chain->rule.num);
341 sstrncpy (vl.type_instance, (char *) match->data,
342 sizeof (vl.type_instance));
345 sstrncpy (vl.type, "ipt_bytes", sizeof (vl.type));
346 values[0].derive = (derive_t) entry->counters.bcnt;
347 plugin_dispatch_values (&vl);
349 sstrncpy (vl.type, "ipt_packets", sizeof (vl.type));
350 values[0].derive = (derive_t) entry->counters.pcnt;
351 plugin_dispatch_values (&vl);
354 } /* int submit_match */
357 /* ipv6 submit_chain */
358 static void submit6_chain (ip6tc_handle_t *handle, ip_chain_t *chain)
360 const struct ip6t_entry *entry;
363 /* Find first rule for chain and use the iterate macro */
364 entry = ip6tc_first_rule( chain->chain, handle );
367 DEBUG ("ip6tc_first_rule failed: %s", ip6tc_strerror (errno));
374 if (chain->rule_type == RTYPE_NUM)
376 submit6_match (NULL, entry, chain, rule_num);
380 IP6T_MATCH_ITERATE( entry, submit6_match, entry, chain, rule_num );
383 entry = ip6tc_next_rule( entry, handle );
385 } /* while (entry) */
389 /* ipv4 submit_chain */
390 static void submit_chain (iptc_handle_t *handle, ip_chain_t *chain)
392 const struct ipt_entry *entry;
395 /* Find first rule for chain and use the iterate macro */
396 entry = iptc_first_rule( chain->chain, handle );
399 DEBUG ("iptc_first_rule failed: %s", iptc_strerror (errno));
406 if (chain->rule_type == RTYPE_NUM)
408 submit_match (NULL, entry, chain, rule_num);
412 IPT_MATCH_ITERATE( entry, submit_match, entry, chain, rule_num );
415 entry = iptc_next_rule( entry, handle );
417 } /* while (entry) */
421 static int iptables_read (void)
424 int num_failures = 0;
427 /* Init the iptc handle structure and query the correct table */
428 for (i = 0; i < chain_num; i++)
430 chain = chain_list[i];
434 DEBUG ("iptables plugin: chain == NULL");
438 if ( chain->ip_version == IPV4 )
440 #ifdef HAVE_IPTC_HANDLE_T
441 iptc_handle_t _handle;
442 iptc_handle_t *handle = &_handle;
444 *handle = iptc_init (chain->table);
446 iptc_handle_t *handle;
447 handle = iptc_init (chain->table);
452 ERROR ("iptables plugin: iptc_init (%s) failed: %s",
453 chain->table, iptc_strerror (errno));
458 submit_chain (handle, chain);
461 else if ( chain->ip_version == IPV6 )
463 #ifdef HAVE_IP6TC_HANDLE_T
464 ip6tc_handle_t _handle;
465 ip6tc_handle_t *handle = &_handle;
467 *handle = ip6tc_init (chain->table);
469 ip6tc_handle_t *handle;
470 handle = ip6tc_init (chain->table);
474 ERROR ("iptables plugin: ip6tc_init (%s) failed: %s",
475 chain->table, ip6tc_strerror (errno));
480 submit6_chain (handle, chain);
485 } /* for (i = 0 .. chain_num) */
487 return ((num_failures < chain_num) ? 0 : -1);
488 } /* int iptables_read */
490 static int iptables_shutdown (void)
494 for (i = 0; i < chain_num; i++)
496 if ((chain_list[i] != NULL) && (chain_list[i]->rule_type == RTYPE_COMMENT))
497 sfree (chain_list[i]->rule.comment);
498 sfree (chain_list[i]);
503 } /* int iptables_shutdown */
505 void module_register (void)
507 plugin_register_config ("iptables", iptables_config,
508 config_keys, config_keys_num);
509 plugin_register_read ("iptables", iptables_read);
510 plugin_register_shutdown ("iptables", iptables_shutdown);
511 } /* void module_register */