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);
69 Each table/chain combo that will be queried goes into this list
72 enum protocol_version_e
77 typedef enum protocol_version_e protocol_version_t;
79 #ifndef XT_TABLE_MAXNAMELEN
80 # define XT_TABLE_MAXNAMELEN 32
83 protocol_version_t ip_version;
84 char table[XT_TABLE_MAXNAMELEN];
85 char chain[XT_TABLE_MAXNAMELEN];
100 static ip_chain_t **chain_list = NULL;
101 static int chain_num = 0;
103 static int iptables_config (const char *key, const char *value)
106 protocol_version_t ip_version = 0;
108 if (strcasecmp (key, "Chain") == 0)
110 else if (strcasecmp (key, "Chain6") == 0)
115 ip_chain_t temp, *final, **list;
125 memset (&temp, 0, sizeof (temp));
127 value_copy = strdup (value);
128 if (value_copy == NULL)
131 ERROR ("strdup failed: %s",
132 sstrerror (errno, errbuf, sizeof (errbuf)));
137 * Time to fill the temp element
138 * Examine value string, it should look like:
139 * Chain[6] <table> <chain> [<comment|num> [name]]
142 /* set IPv4 or IPv6 */
143 temp.ip_version = ip_version;
145 /* Chain <table> <chain> [<comment|num> [name]] */
146 fields_num = strsplit (value_copy, fields, 4);
156 table_len = strlen (table) + 1;
157 if ((unsigned int)table_len > sizeof(temp.table))
159 ERROR ("Table `%s' too long.", table);
163 sstrncpy (temp.table, table, table_len);
165 chain_len = strlen (chain) + 1;
166 if ((unsigned int)chain_len > sizeof(temp.chain))
168 ERROR ("Chain `%s' too long.", chain);
172 sstrncpy (temp.chain, chain, chain_len);
176 char *comment = fields[2];
177 int rule = atoi (comment);
181 temp.rule.num = rule;
182 temp.rule_type = RTYPE_NUM;
186 temp.rule.comment = strdup (comment);
187 if (temp.rule.comment == NULL)
192 temp.rule_type = RTYPE_COMMENT;
197 temp.rule_type = RTYPE_COMMENT_ALL;
201 sstrncpy (temp.name, fields[3], sizeof (temp.name));
208 list = (ip_chain_t **) realloc (chain_list, (chain_num + 1) * sizeof (ip_chain_t *));
212 ERROR ("realloc failed: %s",
213 sstrerror (errno, errbuf, sizeof (errbuf)));
214 sfree (temp.rule.comment);
219 final = (ip_chain_t *) malloc( sizeof(temp) );
223 ERROR ("malloc failed: %s",
224 sstrerror (errno, errbuf, sizeof (errbuf)));
225 sfree (temp.rule.comment);
228 memcpy (final, &temp, sizeof (temp));
229 chain_list[chain_num] = final;
232 DEBUG ("Chain #%i: table = %s; chain = %s;", chain_num, final->table, final->chain);
235 } /* int iptables_config */
237 static int submit6_match (const struct ip6t_entry_match *match,
238 const struct ip6t_entry *entry,
239 const ip_chain_t *chain,
244 value_list_t vl = VALUE_LIST_INIT;
246 /* Select the rules to collect */
247 if (chain->rule_type == RTYPE_NUM)
249 if (chain->rule.num != rule_num)
254 if (strcmp (match->u.user.name, "comment") != 0)
256 if ((chain->rule_type == RTYPE_COMMENT)
257 && (strcmp (chain->rule.comment, (char *) match->data) != 0))
263 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
264 sstrncpy (vl.plugin, "ip6tables", sizeof (vl.plugin));
266 status = ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
267 "%s-%s", chain->table, chain->chain);
268 if ((status < 1) || ((unsigned int)status >= sizeof (vl.plugin_instance)))
271 if (chain->name[0] != '\0')
273 sstrncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
277 if (chain->rule_type == RTYPE_NUM)
278 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
279 "%i", chain->rule.num);
281 sstrncpy (vl.type_instance, (char *) match->data,
282 sizeof (vl.type_instance));
285 sstrncpy (vl.type, "ipt_bytes", sizeof (vl.type));
286 values[0].derive = (derive_t) entry->counters.bcnt;
287 plugin_dispatch_values (&vl);
289 sstrncpy (vl.type, "ipt_packets", sizeof (vl.type));
290 values[0].derive = (derive_t) entry->counters.pcnt;
291 plugin_dispatch_values (&vl);
294 } /* int submit_match */
297 /* This needs to return `int' for IPT_MATCH_ITERATE to work. */
298 static int submit_match (const struct ipt_entry_match *match,
299 const struct ipt_entry *entry,
300 const ip_chain_t *chain,
305 value_list_t vl = VALUE_LIST_INIT;
307 /* Select the rules to collect */
308 if (chain->rule_type == RTYPE_NUM)
310 if (chain->rule.num != rule_num)
315 if (strcmp (match->u.user.name, "comment") != 0)
317 if ((chain->rule_type == RTYPE_COMMENT)
318 && (strcmp (chain->rule.comment, (char *) match->data) != 0))
324 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
325 sstrncpy (vl.plugin, "iptables", sizeof (vl.plugin));
327 status = ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
328 "%s-%s", chain->table, chain->chain);
329 if ((status < 1) || ((unsigned int)status >= sizeof (vl.plugin_instance)))
332 if (chain->name[0] != '\0')
334 sstrncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
338 if (chain->rule_type == RTYPE_NUM)
339 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
340 "%i", chain->rule.num);
342 sstrncpy (vl.type_instance, (char *) match->data,
343 sizeof (vl.type_instance));
346 sstrncpy (vl.type, "ipt_bytes", sizeof (vl.type));
347 values[0].derive = (derive_t) entry->counters.bcnt;
348 plugin_dispatch_values (&vl);
350 sstrncpy (vl.type, "ipt_packets", sizeof (vl.type));
351 values[0].derive = (derive_t) entry->counters.pcnt;
352 plugin_dispatch_values (&vl);
355 } /* int submit_match */
358 /* ipv6 submit_chain */
359 static void submit6_chain( ip6tc_handle_t *handle, ip_chain_t *chain )
361 const struct ip6t_entry *entry;
364 /* Find first rule for chain and use the iterate macro */
365 entry = ip6tc_first_rule( chain->chain, handle );
368 DEBUG ("ip6tc_first_rule failed: %s", ip6tc_strerror (errno));
375 if (chain->rule_type == RTYPE_NUM)
377 submit6_match (NULL, entry, chain, rule_num);
381 IP6T_MATCH_ITERATE( entry, submit6_match, entry, chain, rule_num );
384 entry = ip6tc_next_rule( entry, handle );
386 } /* while (entry) */
390 /* ipv4 submit_chain */
391 static void submit_chain( iptc_handle_t *handle, ip_chain_t *chain )
393 const struct ipt_entry *entry;
396 /* Find first rule for chain and use the iterate macro */
397 entry = iptc_first_rule( chain->chain, handle );
400 DEBUG ("iptc_first_rule failed: %s", iptc_strerror (errno));
407 if (chain->rule_type == RTYPE_NUM)
409 submit_match (NULL, entry, chain, rule_num);
413 IPT_MATCH_ITERATE( entry, submit_match, entry, chain, rule_num );
416 entry = iptc_next_rule( entry, handle );
418 } /* while (entry) */
422 static int iptables_read (void)
425 int num_failures = 0;
428 /* Init the iptc handle structure and query the correct table */
429 for (i = 0; i < chain_num; i++)
431 chain = chain_list[i];
435 DEBUG ("iptables plugin: chain == NULL");
439 if ( chain->ip_version == IPV4 )
441 #ifdef HAVE_IPTC_HANDLE_T
442 iptc_handle_t _handle;
443 iptc_handle_t *handle = &_handle;
445 *handle = iptc_init (chain->table);
447 iptc_handle_t *handle;
448 handle = iptc_init (chain->table);
453 ERROR ("iptables plugin: iptc_init (%s) failed: %s",
454 chain->table, iptc_strerror (errno));
459 submit_chain (handle, chain);
462 else if ( chain->ip_version == IPV6 )
464 #ifdef HAVE_IP6TC_HANDLE_T
465 ip6tc_handle_t _handle;
466 ip6tc_handle_t *handle = &_handle;
468 *handle = ip6tc_init (chain->table);
470 ip6tc_handle_t *handle;
471 handle = ip6tc_init (chain->table);
476 ERROR ("iptables plugin: ip6tc_init (%s) failed: %s",
477 chain->table, ip6tc_strerror (errno));
482 submit6_chain (handle, chain);
487 } /* for (i = 0 .. chain_num) */
489 return ((num_failures < chain_num) ? 0 : -1);
490 } /* int iptables_read */
492 static int iptables_shutdown (void)
496 for (i = 0; i < chain_num; i++)
498 if ((chain_list[i] != NULL) && (chain_list[i]->rule_type == RTYPE_COMMENT))
500 sfree (chain_list[i]->rule.comment);
502 sfree (chain_list[i]);
507 } /* int iptables_shutdown */
509 void module_register (void)
511 plugin_register_config ("iptables", iptables_config,
512 config_keys, config_keys_num);
513 plugin_register_read ("iptables", iptables_read);
514 plugin_register_shutdown ("iptables", iptables_shutdown);
515 } /* void module_register */
518 * vim:shiftwidth=4:softtabstop=4:tabstop=8