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>
32 #include <libiptc/libiptc.h>
33 #include <libiptc/libip6tc.h>
35 #ifdef HAVE_SYS_CAPABILITY_H
36 # include <sys/capability.h>
40 * iptc_handle_t was available before libiptc was officially available as a
41 * shared library. Note, that when the shared lib was introduced, the API and
42 * ABI have changed slightly:
43 * 'iptc_handle_t' used to be 'struct iptc_handle *' and most functions used
44 * 'iptc_handle_t *' as an argument. Now, most functions use 'struct
45 * iptc_handle *' (thus removing one level of pointer indirection).
47 * HAVE_IPTC_HANDLE_T is used to determine which API ought to be used. While
48 * this is somewhat hacky, I didn't find better way to solve that :-/
51 #ifndef HAVE_IPTC_HANDLE_T
52 typedef struct iptc_handle iptc_handle_t;
54 #ifndef HAVE_IP6TC_HANDLE_T
55 typedef struct ip6tc_handle ip6tc_handle_t;
59 * (Module-)Global variables
63 * Config format should be `Chain table chainname',
64 * e. g. `Chain mangle incoming'
66 static const char *config_keys[] =
71 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
72 enum protocol_version_e
77 typedef enum protocol_version_e protocol_version_t;
80 * Each table/chain combo that will be queried goes into this list
82 #ifndef XT_TABLE_MAXNAMELEN
83 # define XT_TABLE_MAXNAMELEN 32
86 protocol_version_t ip_version;
87 char table[XT_TABLE_MAXNAMELEN];
88 char chain[XT_TABLE_MAXNAMELEN];
103 static ip_chain_t **chain_list = NULL;
104 static int chain_num = 0;
106 static int iptables_config (const char *key, const char *value)
109 protocol_version_t ip_version = 0;
111 if (strcasecmp (key, "Chain") == 0)
113 else if (strcasecmp (key, "Chain6") == 0)
118 ip_chain_t temp = { 0 };
119 ip_chain_t *final, **list;
129 value_copy = strdup (value);
130 if (value_copy == NULL)
133 ERROR ("strdup failed: %s",
134 sstrerror (errno, errbuf, sizeof (errbuf)));
139 * Time to fill the temp element
140 * Examine value string, it should look like:
141 * Chain[6] <table> <chain> [<comment|num> [name]]
144 /* set IPv4 or IPv6 */
145 temp.ip_version = ip_version;
147 /* Chain <table> <chain> [<comment|num> [name]] */
148 fields_num = strsplit (value_copy, fields, 4);
158 table_len = strlen (table) + 1;
159 if ((unsigned int)table_len > sizeof(temp.table))
161 ERROR ("Table `%s' too long.", table);
165 sstrncpy (temp.table, table, table_len);
167 chain_len = strlen (chain) + 1;
168 if ((unsigned int)chain_len > sizeof(temp.chain))
170 ERROR ("Chain `%s' too long.", chain);
174 sstrncpy (temp.chain, chain, chain_len);
178 char *comment = fields[2];
179 int rule = atoi (comment);
183 temp.rule.num = rule;
184 temp.rule_type = RTYPE_NUM;
188 temp.rule.comment = strdup (comment);
189 if (temp.rule.comment == NULL)
194 temp.rule_type = RTYPE_COMMENT;
199 temp.rule_type = RTYPE_COMMENT_ALL;
203 sstrncpy (temp.name, fields[3], sizeof (temp.name));
210 list = realloc (chain_list, (chain_num + 1) * sizeof (ip_chain_t *));
214 ERROR ("realloc failed: %s",
215 sstrerror (errno, errbuf, sizeof (errbuf)));
216 sfree (temp.rule.comment);
221 final = malloc(sizeof (*final));
225 ERROR ("malloc failed: %s",
226 sstrerror (errno, errbuf, sizeof (errbuf)));
227 sfree (temp.rule.comment);
230 memcpy (final, &temp, sizeof (temp));
231 chain_list[chain_num] = final;
234 DEBUG ("Chain #%i: table = %s; chain = %s;", chain_num, final->table, final->chain);
237 } /* int iptables_config */
239 static int submit6_match (const struct ip6t_entry_match *match,
240 const struct ip6t_entry *entry,
241 const ip_chain_t *chain,
245 value_list_t vl = VALUE_LIST_INIT;
247 /* Select the rules to collect */
248 if (chain->rule_type == RTYPE_NUM)
250 if (chain->rule.num != rule_num)
255 if (strcmp (match->u.user.name, "comment") != 0)
257 if ((chain->rule_type == RTYPE_COMMENT)
258 && (strcmp (chain->rule.comment, (char *) match->data) != 0))
262 sstrncpy (vl.plugin, "ip6tables", sizeof (vl.plugin));
264 status = ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
265 "%s-%s", chain->table, chain->chain);
266 if ((status < 1) || ((unsigned int)status >= sizeof (vl.plugin_instance)))
269 if (chain->name[0] != '\0')
271 sstrncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
275 if (chain->rule_type == RTYPE_NUM)
276 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
277 "%i", chain->rule.num);
279 sstrncpy (vl.type_instance, (char *) match->data,
280 sizeof (vl.type_instance));
283 sstrncpy (vl.type, "ipt_bytes", sizeof (vl.type));
284 vl.values = &(value_t) { .derive = (derive_t) entry->counters.bcnt };
286 plugin_dispatch_values (&vl);
288 sstrncpy (vl.type, "ipt_packets", sizeof (vl.type));
289 vl.values = &(value_t) { .derive = (derive_t) entry->counters.pcnt };
290 plugin_dispatch_values (&vl);
293 } /* int submit6_match */
295 /* This needs to return `int' for IPT_MATCH_ITERATE to work. */
296 static int submit_match (const struct ipt_entry_match *match,
297 const struct ipt_entry *entry,
298 const ip_chain_t *chain,
302 value_list_t vl = VALUE_LIST_INIT;
304 /* Select the rules to collect */
305 if (chain->rule_type == RTYPE_NUM)
307 if (chain->rule.num != rule_num)
312 if (strcmp (match->u.user.name, "comment") != 0)
314 if ((chain->rule_type == RTYPE_COMMENT)
315 && (strcmp (chain->rule.comment, (char *) match->data) != 0))
319 sstrncpy (vl.plugin, "iptables", sizeof (vl.plugin));
321 status = ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
322 "%s-%s", chain->table, chain->chain);
323 if ((status < 1) || ((unsigned int)status >= sizeof (vl.plugin_instance)))
326 if (chain->name[0] != '\0')
328 sstrncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
332 if (chain->rule_type == RTYPE_NUM)
333 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
334 "%i", chain->rule.num);
336 sstrncpy (vl.type_instance, (char *) match->data,
337 sizeof (vl.type_instance));
340 sstrncpy (vl.type, "ipt_bytes", sizeof (vl.type));
341 vl.values = &(value_t) { .derive = (derive_t) entry->counters.bcnt };
343 plugin_dispatch_values (&vl);
345 sstrncpy (vl.type, "ipt_packets", sizeof (vl.type));
346 vl.values = &(value_t) { .derive = (derive_t) entry->counters.pcnt };
347 plugin_dispatch_values (&vl);
350 } /* int submit_match */
353 /* ipv6 submit_chain */
354 static void submit6_chain (ip6tc_handle_t *handle, ip_chain_t *chain)
356 const struct ip6t_entry *entry;
359 /* Find first rule for chain and use the iterate macro */
360 entry = ip6tc_first_rule( chain->chain, handle );
363 DEBUG ("ip6tc_first_rule failed: %s", ip6tc_strerror (errno));
370 if (chain->rule_type == RTYPE_NUM)
372 submit6_match (NULL, entry, chain, rule_num);
376 IP6T_MATCH_ITERATE( entry, submit6_match, entry, chain, rule_num );
379 entry = ip6tc_next_rule( entry, handle );
381 } /* while (entry) */
385 /* ipv4 submit_chain */
386 static void submit_chain (iptc_handle_t *handle, ip_chain_t *chain)
388 const struct ipt_entry *entry;
391 /* Find first rule for chain and use the iterate macro */
392 entry = iptc_first_rule( chain->chain, handle );
395 DEBUG ("iptc_first_rule failed: %s", iptc_strerror (errno));
402 if (chain->rule_type == RTYPE_NUM)
404 submit_match (NULL, entry, chain, rule_num);
408 IPT_MATCH_ITERATE( entry, submit_match, entry, chain, rule_num );
411 entry = iptc_next_rule( entry, handle );
413 } /* while (entry) */
417 static int iptables_read (void)
419 int num_failures = 0;
422 /* Init the iptc handle structure and query the correct table */
423 for (int i = 0; i < chain_num; i++)
425 chain = chain_list[i];
429 DEBUG ("iptables plugin: chain == NULL");
433 if ( chain->ip_version == IPV4 )
435 #ifdef HAVE_IPTC_HANDLE_T
436 iptc_handle_t _handle;
437 iptc_handle_t *handle = &_handle;
439 *handle = iptc_init (chain->table);
441 iptc_handle_t *handle;
442 handle = iptc_init (chain->table);
447 ERROR ("iptables plugin: iptc_init (%s) failed: %s",
448 chain->table, iptc_strerror (errno));
453 submit_chain (handle, chain);
456 else if ( chain->ip_version == IPV6 )
458 #ifdef HAVE_IP6TC_HANDLE_T
459 ip6tc_handle_t _handle;
460 ip6tc_handle_t *handle = &_handle;
462 *handle = ip6tc_init (chain->table);
464 ip6tc_handle_t *handle;
465 handle = ip6tc_init (chain->table);
469 ERROR ("iptables plugin: ip6tc_init (%s) failed: %s",
470 chain->table, ip6tc_strerror (errno));
475 submit6_chain (handle, chain);
480 } /* for (i = 0 .. chain_num) */
482 return ((num_failures < chain_num) ? 0 : -1);
483 } /* int iptables_read */
485 static int iptables_shutdown (void)
487 for (int i = 0; i < chain_num; i++)
489 if ((chain_list[i] != NULL) && (chain_list[i]->rule_type == RTYPE_COMMENT))
490 sfree (chain_list[i]->rule.comment);
491 sfree (chain_list[i]);
496 } /* int iptables_shutdown */
498 static int iptables_init (void)
500 #if defined(HAVE_SYS_CAPABILITY_H) && defined(CAP_NET_ADMIN)
501 if (check_capability (CAP_NET_ADMIN) != 0)
504 WARNING ("iptables plugin: Running collectd as root, but the "
505 "CAP_NET_ADMIN capability is missing. The plugin's read "
506 "function will probably fail. Is your init system dropping "
509 WARNING ("iptables plugin: collectd doesn't have the CAP_NET_ADMIN "
510 "capability. If you don't want to run collectd as root, try "
511 "running \"setcap cap_net_admin=ep\" on the collectd binary.");
515 } /* int iptables_init */
517 void module_register (void)
519 plugin_register_config ("iptables", iptables_config,
520 config_keys, config_keys_num);
521 plugin_register_init ("iptables", iptables_init);
522 plugin_register_read ("iptables", iptables_read);
523 plugin_register_shutdown ("iptables", iptables_shutdown);
524 } /* void module_register */