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 # define HAVE_IPTC_HANDLE_T 1
39 # define HAVE_IP6TC_HANDLE_T 1
41 #else /* if !OWN_LIBIPTC */
42 # include <libiptc/libiptc.h>
43 # include <libiptc/libip6tc.h>
46 * iptc_handle_t was available before libiptc was officially available as a
47 * shared library. Note, that when the shared lib was introduced, the API and
48 * ABI have changed slightly:
49 * 'iptc_handle_t' used to be 'struct iptc_handle *' and most functions used
50 * 'iptc_handle_t *' as an argument. Now, most functions use 'struct
51 * iptc_handle *' (thus removing one level of pointer indirection).
53 * HAVE_IPTC_HANDLE_T is used to determine which API ought to be used. While
54 * this is somewhat hacky, I didn't find better way to solve that :-/
57 # ifndef HAVE_IPTC_HANDLE_T
58 typedef struct iptc_handle iptc_handle_t;
60 # ifndef HAVE_IP6TC_HANDLE_T
61 typedef struct ip6tc_handle ip6tc_handle_t;
63 #endif /* !OWN_LIBIPTC */
66 * (Module-)Global variables
70 * Config format should be `Chain table chainname',
71 * e. g. `Chain mangle incoming'
73 static const char *config_keys[] =
78 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
80 Each table/chain combo that will be queried goes into this list
83 enum protocol_version_e
88 typedef enum protocol_version_e protocol_version_t;
90 #ifndef XT_TABLE_MAXNAMELEN
91 # define XT_TABLE_MAXNAMELEN 32
94 protocol_version_t ip_version;
95 char table[XT_TABLE_MAXNAMELEN];
96 char chain[XT_TABLE_MAXNAMELEN];
111 static ip_chain_t **chain_list = NULL;
112 static int chain_num = 0;
114 static int iptables_config (const char *key, const char *value)
117 protocol_version_t ip_version = 0;
119 if (strcasecmp (key, "Chain") == 0)
121 else if (strcasecmp (key, "Chain6") == 0)
124 if (( ip_version == IPV4 ) || ( ip_version == IPV6 ))
126 ip_chain_t temp, *final, **list;
136 memset (&temp, 0, sizeof (temp));
138 value_copy = strdup (value);
139 if (value_copy == NULL)
142 ERROR ("strdup failed: %s",
143 sstrerror (errno, errbuf, sizeof (errbuf)));
148 * Time to fill the temp element
149 * Examine value string, it should look like:
150 * Chain[6] <table> <chain> [<comment|num> [name]]
153 /* set IPv4 or IPv6 */
154 temp.ip_version = ip_version;
156 /* Chain <table> <chain> [<comment|num> [name]] */
157 fields_num = strsplit (value_copy, fields, 4);
167 table_len = strlen (table) + 1;
168 if ((unsigned int)table_len > sizeof(temp.table))
170 ERROR ("Table `%s' too long.", table);
174 sstrncpy (temp.table, table, table_len);
176 chain_len = strlen (chain) + 1;
177 if ((unsigned int)chain_len > sizeof(temp.chain))
179 ERROR ("Chain `%s' too long.", chain);
183 sstrncpy (temp.chain, chain, chain_len);
187 char *comment = fields[2];
188 int rule = atoi (comment);
192 temp.rule.num = rule;
193 temp.rule_type = RTYPE_NUM;
197 temp.rule.comment = strdup (comment);
198 if (temp.rule.comment == NULL)
203 temp.rule_type = RTYPE_COMMENT;
208 temp.rule_type = RTYPE_COMMENT_ALL;
212 sstrncpy (temp.name, fields[3], sizeof (temp.name));
219 list = (ip_chain_t **) realloc (chain_list, (chain_num + 1) * sizeof (ip_chain_t *));
223 ERROR ("realloc failed: %s",
224 sstrerror (errno, errbuf, sizeof (errbuf)));
229 final = (ip_chain_t *) malloc( sizeof(temp) );
233 ERROR ("malloc failed: %s",
234 sstrerror (errno, errbuf, sizeof (errbuf)));
237 memcpy (final, &temp, sizeof (temp));
238 chain_list[chain_num] = final;
241 DEBUG ("Chain #%i: table = %s; chain = %s;", chain_num, final->table, final->chain);
249 } /* int iptables_config */
251 static int submit6_match (const struct ip6t_entry_match *match,
252 const struct ip6t_entry *entry,
253 const ip_chain_t *chain,
258 value_list_t vl = VALUE_LIST_INIT;
260 /* Select the rules to collect */
261 if (chain->rule_type == RTYPE_NUM)
263 if (chain->rule.num != rule_num)
268 if (strcmp (match->u.user.name, "comment") != 0)
270 if ((chain->rule_type == RTYPE_COMMENT)
271 && (strcmp (chain->rule.comment, (char *) match->data) != 0))
277 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
278 sstrncpy (vl.plugin, "ip6tables", sizeof (vl.plugin));
280 status = ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
281 "%s-%s", chain->table, chain->chain);
282 if ((status < 1) || ((unsigned int)status >= sizeof (vl.plugin_instance)))
285 if (chain->name[0] != '\0')
287 sstrncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
291 if (chain->rule_type == RTYPE_NUM)
292 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
293 "%i", chain->rule.num);
295 sstrncpy (vl.type_instance, (char *) match->data,
296 sizeof (vl.type_instance));
299 sstrncpy (vl.type, "ipt_bytes", sizeof (vl.type));
300 values[0].counter = (counter_t) entry->counters.bcnt;
301 plugin_dispatch_values (&vl);
303 sstrncpy (vl.type, "ipt_packets", sizeof (vl.type));
304 values[0].counter = (counter_t) entry->counters.pcnt;
305 plugin_dispatch_values (&vl);
308 } /* int submit_match */
311 /* This needs to return `int' for IPT_MATCH_ITERATE to work. */
312 static int submit_match (const struct ipt_entry_match *match,
313 const struct ipt_entry *entry,
314 const ip_chain_t *chain,
319 value_list_t vl = VALUE_LIST_INIT;
321 /* Select the rules to collect */
322 if (chain->rule_type == RTYPE_NUM)
324 if (chain->rule.num != rule_num)
329 if (strcmp (match->u.user.name, "comment") != 0)
331 if ((chain->rule_type == RTYPE_COMMENT)
332 && (strcmp (chain->rule.comment, (char *) match->data) != 0))
338 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
339 sstrncpy (vl.plugin, "iptables", sizeof (vl.plugin));
341 status = ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
342 "%s-%s", chain->table, chain->chain);
343 if ((status < 1) || ((unsigned int)status >= sizeof (vl.plugin_instance)))
346 if (chain->name[0] != '\0')
348 sstrncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
352 if (chain->rule_type == RTYPE_NUM)
353 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
354 "%i", chain->rule.num);
356 sstrncpy (vl.type_instance, (char *) match->data,
357 sizeof (vl.type_instance));
360 sstrncpy (vl.type, "ipt_bytes", sizeof (vl.type));
361 values[0].counter = (counter_t) entry->counters.bcnt;
362 plugin_dispatch_values (&vl);
364 sstrncpy (vl.type, "ipt_packets", sizeof (vl.type));
365 values[0].counter = (counter_t) entry->counters.pcnt;
366 plugin_dispatch_values (&vl);
369 } /* int submit_match */
372 /* ipv6 submit_chain */
373 static void submit6_chain( ip6tc_handle_t *handle, ip_chain_t *chain )
375 const struct ip6t_entry *entry;
378 /* Find first rule for chain and use the iterate macro */
379 entry = ip6tc_first_rule( chain->chain, handle );
382 DEBUG ("ip6tc_first_rule failed: %s", ip6tc_strerror (errno));
389 if (chain->rule_type == RTYPE_NUM)
391 submit6_match (NULL, entry, chain, rule_num);
395 IP6T_MATCH_ITERATE( entry, submit6_match, entry, chain, rule_num );
398 entry = ip6tc_next_rule( entry, handle );
400 } /* while (entry) */
404 /* ipv4 submit_chain */
405 static void submit_chain( iptc_handle_t *handle, ip_chain_t *chain )
407 const struct ipt_entry *entry;
410 /* Find first rule for chain and use the iterate macro */
411 entry = iptc_first_rule( chain->chain, handle );
414 DEBUG ("iptc_first_rule failed: %s", iptc_strerror (errno));
421 if (chain->rule_type == RTYPE_NUM)
423 submit_match (NULL, entry, chain, rule_num);
427 IPT_MATCH_ITERATE( entry, submit_match, entry, chain, rule_num );
430 entry = iptc_next_rule( entry, handle );
432 } /* while (entry) */
436 static int iptables_read (void)
439 int num_failures = 0;
442 /* Init the iptc handle structure and query the correct table */
443 for (i = 0; i < chain_num; i++)
445 chain = chain_list[i];
449 DEBUG ("iptables plugin: chain == NULL");
453 if ( chain->ip_version == IPV4 )
455 #ifdef HAVE_IPTC_HANDLE_T
456 iptc_handle_t _handle;
457 iptc_handle_t *handle = &_handle;
459 *handle = iptc_init (chain->table);
461 iptc_handle_t *handle;
462 handle = iptc_init (chain->table);
467 ERROR ("iptables plugin: iptc_init (%s) failed: %s",
468 chain->table, iptc_strerror (errno));
473 submit_chain (handle, chain);
476 else if ( chain->ip_version == IPV6 )
478 #ifdef HAVE_IP6TC_HANDLE_T
479 ip6tc_handle_t _handle;
480 ip6tc_handle_t *handle = &_handle;
482 *handle = ip6tc_init (chain->table);
484 ip6tc_handle_t *handle;
485 handle = ip6tc_init (chain->table);
490 ERROR ("iptables plugin: ip6tc_init (%s) failed: %s",
491 chain->table, ip6tc_strerror (errno));
496 submit6_chain (handle, chain);
501 } /* for (i = 0 .. chain_num) */
503 return ((num_failures < chain_num) ? 0 : -1);
504 } /* int iptables_read */
506 static int iptables_shutdown (void)
510 for (i = 0; i < chain_num; i++)
512 if ((chain_list[i] != NULL) && (chain_list[i]->rule_type == RTYPE_COMMENT))
514 sfree (chain_list[i]->rule.comment);
516 sfree (chain_list[i]);
521 } /* int iptables_shutdown */
523 void module_register (void)
525 plugin_register_config ("iptables", iptables_config,
526 config_keys, config_keys_num);
527 plugin_register_read ("iptables", iptables_read);
528 plugin_register_shutdown ("iptables", iptables_shutdown);
529 } /* void module_register */
532 * vim:shiftwidth=4:softtabstop=4:tabstop=8