X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fiptables.c;h=49454f050d755283b5cec6094a003116983e21fb;hb=b5230d45188e81ae58c889fa69e7e87b79359482;hp=bc035d9f9e8eb7f42b0d56cb5c5af3f10906d1f6;hpb=cd4542addb1847673e2cf8791081c86ff87c6be4;p=collectd.git diff --git a/src/iptables.c b/src/iptables.c index bc035d9f..49454f05 100644 --- a/src/iptables.c +++ b/src/iptables.c @@ -1,6 +1,8 @@ /** * collectd - src/iptables.c - * Copyright (C) 2007 Sjoerd van der Berg + * Copyright (C) 2007 Sjoerd van der Berg + * Copyright (C) 2007-2010 Florian octo Forster + * Copyright (C) 2009 Marco Chiappero * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the @@ -18,62 +20,69 @@ * * Authors: * Sjoerd van der Berg + * Florian Forster + * Marco Chiappero **/ #include "collectd.h" #include "common.h" #include "plugin.h" #include "configfile.h" -#include "utils_debug.h" -#if HAVE_LIBIPTC_LIBIPTC_H -# include -#endif - -#if HAVE_LIBIPTC_LIBIPTC_H -# define IPTABLES_HAVE_READ 1 -#else -# define IPTABLES_HAVE_READ 0 -#endif +#include -#define MODULE_NAME "iptables" -#define BUFSIZE 512 +#include +#include /* - * (Module-)Global variables + * iptc_handle_t was available before libiptc was officially available as a + * shared library. Note, that when the shared lib was introduced, the API and + * ABI have changed slightly: + * 'iptc_handle_t' used to be 'struct iptc_handle *' and most functions used + * 'iptc_handle_t *' as an argument. Now, most functions use 'struct + * iptc_handle *' (thus removing one level of pointer indirection). + * + * HAVE_IPTC_HANDLE_T is used to determine which API ought to be used. While + * this is somewhat hacky, I didn't find better way to solve that :-/ + * -tokkee */ +#ifndef HAVE_IPTC_HANDLE_T +typedef struct iptc_handle iptc_handle_t; +#endif +#ifndef HAVE_IP6TC_HANDLE_T +typedef struct ip6tc_handle ip6tc_handle_t; +#endif /* - * Removed packet count for now, should have config option if you want to save - * them Although other collectd models don't seem to care much for options - * eitherway for what to log + * (Module-)Global variables */ -/* Limit to ~125MByte/s (~1GBit/s) */ -static char *ds_def[] = -{ - "DS:value:COUNTER:"COLLECTD_HEARTBEAT":0:134217728", - NULL -}; -static int ds_num = 1; -#if IPTABLES_HAVE_READ /* * Config format should be `Chain table chainname', * e. g. `Chain mangle incoming' */ -static char *config_keys[] = +static const char *config_keys[] = { "Chain", - NULL + "Chain6" }; -static int config_keys_num = 1; +static int config_keys_num = STATIC_ARRAY_SIZE (config_keys); /* Each table/chain combo that will be queried goes into this list */ + +enum protocol_version_e +{ + IPV4, + IPV6 +}; +typedef enum protocol_version_e protocol_version_t; + #ifndef XT_TABLE_MAXNAMELEN # define XT_TABLE_MAXNAMELEN 32 #endif typedef struct { + protocol_version_t ip_version; char table[XT_TABLE_MAXNAMELEN]; char chain[XT_TABLE_MAXNAMELEN]; union @@ -93,9 +102,17 @@ typedef struct { static ip_chain_t **chain_list = NULL; static int chain_num = 0; -static int iptables_config (char *key, char *value) +static int iptables_config (const char *key, const char *value) { + /* int ip_value; */ + protocol_version_t ip_version = 0; + if (strcasecmp (key, "Chain") == 0) + ip_version = IPV4; + else if (strcasecmp (key, "Chain6") == 0) + ip_version = IPV6; + + if (( ip_version == IPV4 ) || ( ip_version == IPV6 )) { ip_chain_t temp, *final, **list; char *table; @@ -112,10 +129,21 @@ static int iptables_config (char *key, char *value) value_copy = strdup (value); if (value_copy == NULL) { - syslog (LOG_ERR, "strdup failed: %s", strerror (errno)); + char errbuf[1024]; + ERROR ("strdup failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); return (1); } + /* + * Time to fill the temp element + * Examine value string, it should look like: + * Chain[6] [ [name]] + */ + + /* set IPv4 or IPv6 */ + temp.ip_version = ip_version; + /* Chain
[ [name]] */ fields_num = strsplit (value_copy, fields, 4); if (fields_num < 2) @@ -127,25 +155,23 @@ static int iptables_config (char *key, char *value) table = fields[0]; chain = fields[1]; - table_len = strlen (table); - if (table_len >= sizeof(temp.table)) + table_len = strlen (table) + 1; + if ((unsigned int)table_len > sizeof(temp.table)) { - syslog (LOG_ERR, "Table `%s' too long.", table); + ERROR ("Table `%s' too long.", table); free (value_copy); return (1); } - strncpy (temp.table, table, table_len); - temp.table[table_len] = '\0'; + sstrncpy (temp.table, table, table_len); - chain_len = strlen (chain); - if (chain_len >= sizeof(temp.chain)) + chain_len = strlen (chain) + 1; + if ((unsigned int)chain_len > sizeof(temp.chain)) { - syslog (LOG_ERR, "Chain `%s' too long.", chain); + ERROR ("Chain `%s' too long.", chain); free (value_copy); return (1); } - strncpy (temp.chain, chain, chain_len); - temp.chain[chain_len] = '\0'; + sstrncpy (temp.chain, chain, chain_len); if (fields_num >= 3) { @@ -159,8 +185,12 @@ static int iptables_config (char *key, char *value) } else { - strncpy (temp.rule.comment, comment, - sizeof (temp.rule.comment) - 1); + temp.rule.comment = strdup (comment); + if (temp.rule.comment == NULL) + { + free (value_copy); + return (1); + } temp.rule_type = RTYPE_COMMENT; } } @@ -170,7 +200,7 @@ static int iptables_config (char *key, char *value) } if (fields_num >= 4) - strncpy (temp.name, fields[3], sizeof (temp.name) - 1); + sstrncpy (temp.name, fields[3], sizeof (temp.name)); free (value_copy); value_copy = NULL; @@ -180,22 +210,26 @@ static int iptables_config (char *key, char *value) list = (ip_chain_t **) realloc (chain_list, (chain_num + 1) * sizeof (ip_chain_t *)); if (list == NULL) { - syslog (LOG_ERR, "realloc failed: %s", strerror (errno)); - return (1); + char errbuf[1024]; + ERROR ("realloc failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); + return (1); } chain_list = list; final = (ip_chain_t *) malloc( sizeof(temp) ); if (final == NULL) { - syslog (LOG_ERR, "malloc failed: %s", strerror (errno)); - return (1); + char errbuf[1024]; + ERROR ("malloc failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); + return (1); } memcpy (final, &temp, sizeof (temp)); chain_list[chain_num] = final; chain_num++; - DBG ("Chain #%i: table = %s; chain = %s;", chain_num, final->table, final->chain); + DEBUG ("Chain #%i: table = %s; chain = %s;", chain_num, final->table, final->chain); } else { @@ -203,70 +237,79 @@ static int iptables_config (char *key, char *value) } return (0); -} -#endif /* IPTABLES_HAVE_READ */ +} /* int iptables_config */ -static void iptables_init (void) -{ - return; -} - -static void iptables_write (char *host, char *orig_inst, char *val, char *type) +static int submit6_match (const struct ip6t_entry_match *match, + const struct ip6t_entry *entry, + const ip_chain_t *chain, + int rule_num) { - char *table; - char *inst; - char file[256]; int status; + value_t values[1]; + value_list_t vl = VALUE_LIST_INIT; - table = strdup (orig_inst); - if (table == NULL) - return; - inst = strchr (table, ','); - if (inst == NULL) + /* Select the rules to collect */ + if (chain->rule_type == RTYPE_NUM) { - free (table); - return; + if (chain->rule.num != rule_num) + return (0); + } + else + { + if (strcmp (match->u.user.name, "comment") != 0) + return (0); + if ((chain->rule_type == RTYPE_COMMENT) + && (strcmp (chain->rule.comment, (char *) match->data) != 0)) + return (0); } - *inst = '\0'; - inst++; - if (*inst == '\0') + vl.values = values; + vl.values_len = 1; + sstrncpy (vl.host, hostname_g, sizeof (vl.host)); + sstrncpy (vl.plugin, "ip6tables", sizeof (vl.plugin)); + + status = ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance), + "%s-%s", chain->table, chain->chain); + if ((status < 1) || ((unsigned int)status >= sizeof (vl.plugin_instance))) + return (0); + + if (chain->name[0] != '\0') { - free (table); - return; + sstrncpy (vl.type_instance, chain->name, sizeof (vl.type_instance)); + } + else + { + if (chain->rule_type == RTYPE_NUM) + ssnprintf (vl.type_instance, sizeof (vl.type_instance), + "%i", chain->rule.num); + else + sstrncpy (vl.type_instance, (char *) match->data, + sizeof (vl.type_instance)); } - status = snprintf (file, sizeof (file), "iptables-%s/%s-%s.rrd", - table, type, inst); - free (table); - if ((status >= sizeof (file)) || (status < 1)) - return; + sstrncpy (vl.type, "ipt_bytes", sizeof (vl.type)); + values[0].derive = (derive_t) entry->counters.bcnt; + plugin_dispatch_values (&vl); - rrd_update_file (host, file, val, ds_def, ds_num); -} /* void iptables_write */ + sstrncpy (vl.type, "ipt_packets", sizeof (vl.type)); + values[0].derive = (derive_t) entry->counters.pcnt; + plugin_dispatch_values (&vl); -static void iptables_write_bytes (char *host, char *inst, char *val) -{ - iptables_write (host, inst, val, "ipt_bytes"); -} + return (0); +} /* int submit_match */ -static void iptables_write_packets (char *host, char *inst, char *val) -{ - iptables_write (host, inst, val, "ipt_packets"); -} -#if IPTABLES_HAVE_READ +/* This needs to return `int' for IPT_MATCH_ITERATE to work. */ static int submit_match (const struct ipt_entry_match *match, const struct ipt_entry *entry, const ip_chain_t *chain, int rule_num) { - char inst[64]; - char value[64]; int status; + value_t values[1]; + value_list_t vl = VALUE_LIST_INIT; - /* Only log rules that have a comment, although could probably also do - * numerical targets sometime */ + /* Select the rules to collect */ if (chain->rule_type == RTYPE_NUM) { if (chain->rule.num != rule_num) @@ -275,48 +318,83 @@ static int submit_match (const struct ipt_entry_match *match, else { if (strcmp (match->u.user.name, "comment") != 0) - return 0; + return (0); if ((chain->rule_type == RTYPE_COMMENT) && (strcmp (chain->rule.comment, (char *) match->data) != 0)) return (0); } + vl.values = values; + vl.values_len = 1; + sstrncpy (vl.host, hostname_g, sizeof (vl.host)); + sstrncpy (vl.plugin, "iptables", sizeof (vl.plugin)); + + status = ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance), + "%s-%s", chain->table, chain->chain); + if ((status < 1) || ((unsigned int)status >= sizeof (vl.plugin_instance))) + return (0); + if (chain->name[0] != '\0') { - status = snprintf (inst, sizeof (inst), "%s-%s,%s", - chain->table, chain->chain, chain->name); + sstrncpy (vl.type_instance, chain->name, sizeof (vl.type_instance)); } else { if (chain->rule_type == RTYPE_NUM) - status = snprintf (inst, sizeof (inst), "%s-%s,%i", - chain->table, chain->chain, chain->rule.num); + ssnprintf (vl.type_instance, sizeof (vl.type_instance), + "%i", chain->rule.num); else - status = snprintf (inst, sizeof (inst), "%s-%s,%s", - chain->table, chain->chain, match->data); - - if ((status >= sizeof (inst)) || (status < 1)) - return (0); + sstrncpy (vl.type_instance, (char *) match->data, + sizeof (vl.type_instance)); } - status = snprintf (value, sizeof (value), "%u:%lld", - (unsigned int) curtime, - entry->counters.bcnt); - if ((status >= sizeof (value)) || (status < 1)) - return 0; - plugin_submit ("ipt_bytes", inst, value); - - status = snprintf (value, sizeof (value), "%u:%lld", - (unsigned int) curtime, - entry->counters.pcnt); - if ((status >= sizeof (value)) || (status < 1)) - return 0; - plugin_submit ("ipt_packets", inst, value); - - return 0; + sstrncpy (vl.type, "ipt_bytes", sizeof (vl.type)); + values[0].derive = (derive_t) entry->counters.bcnt; + plugin_dispatch_values (&vl); + + sstrncpy (vl.type, "ipt_packets", sizeof (vl.type)); + values[0].derive = (derive_t) entry->counters.pcnt; + plugin_dispatch_values (&vl); + + return (0); } /* int submit_match */ -static void submit_chain( iptc_handle_t *handle, ip_chain_t *chain ) { + +/* ipv6 submit_chain */ +static void submit6_chain( ip6tc_handle_t *handle, ip_chain_t *chain ) +{ + const struct ip6t_entry *entry; + int rule_num; + + /* Find first rule for chain and use the iterate macro */ + entry = ip6tc_first_rule( chain->chain, handle ); + if (entry == NULL) + { + DEBUG ("ip6tc_first_rule failed: %s", ip6tc_strerror (errno)); + return; + } + + rule_num = 1; + while (entry) + { + if (chain->rule_type == RTYPE_NUM) + { + submit6_match (NULL, entry, chain, rule_num); + } + else + { + IP6T_MATCH_ITERATE( entry, submit6_match, entry, chain, rule_num ); + } + + entry = ip6tc_next_rule( entry, handle ); + rule_num++; + } /* while (entry) */ +} + + +/* ipv4 submit_chain */ +static void submit_chain( iptc_handle_t *handle, ip_chain_t *chain ) +{ const struct ipt_entry *entry; int rule_num; @@ -324,7 +402,7 @@ static void submit_chain( iptc_handle_t *handle, ip_chain_t *chain ) { entry = iptc_first_rule( chain->chain, handle ); if (entry == NULL) { - DBG ("iptc_first_rule failed: %s", iptc_strerror (errno)); + DEBUG ("iptc_first_rule failed: %s", iptc_strerror (errno)); return; } @@ -346,55 +424,100 @@ static void submit_chain( iptc_handle_t *handle, ip_chain_t *chain ) { } -static void iptables_read (void) +static int iptables_read (void) { int i; - static complain_t complaint; + int num_failures = 0; + ip_chain_t *chain; /* Init the iptc handle structure and query the correct table */ for (i = 0; i < chain_num; i++) { - iptc_handle_t handle; - ip_chain_t *chain; - chain = chain_list[i]; + if (!chain) { - DBG ("chain == NULL"); + DEBUG ("iptables plugin: chain == NULL"); continue; } - handle = iptc_init( chain->table ); - if (!handle) + if ( chain->ip_version == IPV4 ) + { +#ifdef HAVE_IPTC_HANDLE_T + iptc_handle_t _handle; + iptc_handle_t *handle = &_handle; + + *handle = iptc_init (chain->table); +#else + iptc_handle_t *handle; + handle = iptc_init (chain->table); +#endif + + if (!handle) + { + ERROR ("iptables plugin: iptc_init (%s) failed: %s", + chain->table, iptc_strerror (errno)); + num_failures++; + continue; + } + + submit_chain (handle, chain); + iptc_free (handle); + } + else if ( chain->ip_version == IPV6 ) + { +#ifdef HAVE_IP6TC_HANDLE_T + ip6tc_handle_t _handle; + ip6tc_handle_t *handle = &_handle; + + *handle = ip6tc_init (chain->table); +#else + ip6tc_handle_t *handle; + handle = ip6tc_init (chain->table); +#endif + + if (!handle) + { + ERROR ("iptables plugin: ip6tc_init (%s) failed: %s", + chain->table, ip6tc_strerror (errno)); + num_failures++; + continue; + } + + submit6_chain (handle, chain); + ip6tc_free (handle); + } + else num_failures++; + + } /* for (i = 0 .. chain_num) */ + + return ((num_failures < chain_num) ? 0 : -1); +} /* int iptables_read */ + +static int iptables_shutdown (void) +{ + int i; + + for (i = 0; i < chain_num; i++) + { + if ((chain_list[i] != NULL) && (chain_list[i]->rule_type == RTYPE_COMMENT)) { - DBG ("iptc_init (%s) failed: %s", chain->table, iptc_strerror (errno)); - plugin_complain (LOG_ERR, &complaint, "iptc_init (%s) failed: %s", - chain->table, iptc_strerror (errno)); - continue; + sfree (chain_list[i]->rule.comment); } - plugin_relief (LOG_INFO, &complaint, "iptc_init (%s) succeeded", - chain->table); - - submit_chain (&handle, chain); - iptc_free (&handle); + sfree (chain_list[i]); } -} -#else /* !IPTABLES_HAVE_READ */ -# define iptables_read NULL -#endif + sfree (chain_list); + + return (0); +} /* int iptables_shutdown */ void module_register (void) { - plugin_register ("ipt_bytes", NULL, NULL, iptables_write_bytes); - plugin_register ("ipt_packets", NULL, NULL, iptables_write_packets); -#if IPTABLES_HAVE_READ - plugin_register (MODULE_NAME, iptables_init, iptables_read, NULL); - cf_register (MODULE_NAME, iptables_config, config_keys, config_keys_num); -#endif -} - -#undef BUFSIZE -#undef MODULE_NAME + plugin_register_config ("iptables", iptables_config, + config_keys, config_keys_num); + plugin_register_read ("iptables", iptables_read); + plugin_register_shutdown ("iptables", iptables_shutdown); +} /* void module_register */ /* * vim:shiftwidth=4:softtabstop=4:tabstop=8