2 * collectd - src/iptables.c
3 * Copyright (C) 2007 Sjoerd van der Berg
4 * Copyright (C) 2007 Florian octo Forster
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 * Sjoerd van der Berg <harekiet at users.sourceforge.net>
22 * Florian Forster <octo at verplant.org>
28 #include "configfile.h"
31 # include "owniptc/libiptc.h"
33 # include <libiptc/libiptc.h>
37 * iptc_handle_t was available before libiptc was officially available as a
38 * shared library. Note, that when the shared lib was introduced, the API and
39 * ABI have changed slightly:
40 * 'iptc_handle_t' used to be 'struct iptc_handle *' and most functions used
41 * 'iptc_handle_t *' as an argument. Now, most functions use 'struct
42 * iptc_handle *' (thus removing one level of pointer indirection).
44 * HAVE_IPTC_HANDLE_T is used to determine which API ought to be used. While
45 * this is somewhat hacky, I didn't find better way to solve that :-/
48 #ifndef HAVE_IPTC_HANDLE_T
49 typedef struct iptc_handle iptc_handle_t;
53 * (Module-)Global variables
57 * Config format should be `Chain table chainname',
58 * e. g. `Chain mangle incoming'
60 static const char *config_keys[] =
65 static int config_keys_num = 1;
67 Each table/chain combo that will be queried goes into this list
69 #ifndef XT_TABLE_MAXNAMELEN
70 # define XT_TABLE_MAXNAMELEN 32
73 char table[XT_TABLE_MAXNAMELEN];
74 char chain[XT_TABLE_MAXNAMELEN];
89 static ip_chain_t **chain_list = NULL;
90 static int chain_num = 0;
92 static int iptables_config (const char *key, const char *value)
94 if (strcasecmp (key, "Chain") == 0)
96 ip_chain_t temp, *final, **list;
106 memset (&temp, 0, sizeof (temp));
108 value_copy = strdup (value);
109 if (value_copy == NULL)
112 ERROR ("strdup failed: %s",
113 sstrerror (errno, errbuf, sizeof (errbuf)));
117 /* Chain <table> <chain> [<comment|num> [name]] */
118 fields_num = strsplit (value_copy, fields, 4);
128 table_len = strlen (table) + 1;
129 if ((unsigned int)table_len > sizeof(temp.table))
131 ERROR ("Table `%s' too long.", table);
135 sstrncpy (temp.table, table, table_len);
137 chain_len = strlen (chain) + 1;
138 if ((unsigned int)chain_len > sizeof(temp.chain))
140 ERROR ("Chain `%s' too long.", chain);
144 sstrncpy (temp.chain, chain, chain_len);
148 char *comment = fields[2];
149 int rule = atoi (comment);
153 temp.rule.num = rule;
154 temp.rule_type = RTYPE_NUM;
158 temp.rule.comment = strdup (comment);
159 if (temp.rule.comment == NULL)
164 temp.rule_type = RTYPE_COMMENT;
169 temp.rule_type = RTYPE_COMMENT_ALL;
173 sstrncpy (temp.name, fields[3], sizeof (temp.name));
180 list = (ip_chain_t **) realloc (chain_list, (chain_num + 1) * sizeof (ip_chain_t *));
184 ERROR ("realloc failed: %s",
185 sstrerror (errno, errbuf, sizeof (errbuf)));
190 final = (ip_chain_t *) malloc( sizeof(temp) );
194 ERROR ("malloc failed: %s",
195 sstrerror (errno, errbuf, sizeof (errbuf)));
198 memcpy (final, &temp, sizeof (temp));
199 chain_list[chain_num] = final;
202 DEBUG ("Chain #%i: table = %s; chain = %s;", chain_num, final->table, final->chain);
210 } /* int iptables_config */
212 /* This needs to return `int' for IPT_MATCH_ITERATE to work. */
213 static int submit_match (const struct ipt_entry_match *match,
214 const struct ipt_entry *entry,
215 const ip_chain_t *chain,
220 value_list_t vl = VALUE_LIST_INIT;
222 /* Select the rules to collect */
223 if (chain->rule_type == RTYPE_NUM)
225 if (chain->rule.num != rule_num)
230 if (strcmp (match->u.user.name, "comment") != 0)
232 if ((chain->rule_type == RTYPE_COMMENT)
233 && (strcmp (chain->rule.comment, (char *) match->data) != 0))
239 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
240 sstrncpy (vl.plugin, "iptables", sizeof (vl.plugin));
242 status = ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
243 "%s-%s", chain->table, chain->chain);
244 if ((status < 1) || ((unsigned int)status >= sizeof (vl.plugin_instance)))
247 if (chain->name[0] != '\0')
249 sstrncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
253 if (chain->rule_type == RTYPE_NUM)
254 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
255 "%i", chain->rule.num);
257 sstrncpy (vl.type_instance, (char *) match->data,
258 sizeof (vl.type_instance));
261 sstrncpy (vl.type, "ipt_bytes", sizeof (vl.type));
262 values[0].counter = (counter_t) entry->counters.bcnt;
263 plugin_dispatch_values (&vl);
265 sstrncpy (vl.type, "ipt_packets", sizeof (vl.type));
266 values[0].counter = (counter_t) entry->counters.pcnt;
267 plugin_dispatch_values (&vl);
270 } /* void submit_match */
272 static void submit_chain( iptc_handle_t *handle, ip_chain_t *chain ) {
273 const struct ipt_entry *entry;
276 /* Find first rule for chain and use the iterate macro */
277 entry = iptc_first_rule( chain->chain, handle );
280 DEBUG ("iptc_first_rule failed: %s", iptc_strerror (errno));
287 if (chain->rule_type == RTYPE_NUM)
289 submit_match (NULL, entry, chain, rule_num);
293 IPT_MATCH_ITERATE( entry, submit_match, entry, chain, rule_num );
296 entry = iptc_next_rule( entry, handle );
298 } /* while (entry) */
302 static int iptables_read (void)
305 int num_failures = 0;
307 /* Init the iptc handle structure and query the correct table */
308 for (i = 0; i < chain_num; i++)
310 #ifdef HAVE_IPTC_HANDLE_T
311 iptc_handle_t _handle;
312 iptc_handle_t *handle = &_handle;
314 iptc_handle_t *handle;
318 chain = chain_list[i];
321 DEBUG ("iptables plugin: chain == NULL");
325 #ifdef HAVE_IPTC_HANDLE_T
326 *handle = iptc_init (chain->table);
328 handle = iptc_init (chain->table);
332 ERROR ("iptables plugin: iptc_init (%s) failed: %s",
333 chain->table, iptc_strerror (errno));
338 submit_chain (handle, chain);
340 } /* for (i = 0 .. chain_num) */
342 return ((num_failures < chain_num) ? 0 : -1);
343 } /* int iptables_read */
345 static int iptables_shutdown (void)
349 for (i = 0; i < chain_num; i++)
351 if ((chain_list[i] != NULL) && (chain_list[i]->rule_type == RTYPE_COMMENT))
353 sfree (chain_list[i]->rule.comment);
355 sfree (chain_list[i]);
360 } /* int iptables_shutdown */
362 void module_register (void)
364 plugin_register_config ("iptables", iptables_config,
365 config_keys, config_keys_num);
366 plugin_register_read ("iptables", iptables_read);
367 plugin_register_shutdown ("iptables", iptables_shutdown);
368 } /* void module_register */
371 * vim:shiftwidth=4:softtabstop=4:tabstop=8