1 /**
2 * collectd - src/iptables.c
3 * Copyright (C) 2007 Sjoerd van der Berg
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Sjoerd van der Berg <harekiet at users.sourceforge.net>
21 **/
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26 #include "configfile.h"
27 #include "utils_debug.h"
29 #if HAVE_LIBIPTC_LIBIPTC_H
30 # include <libiptc/libiptc.h>
31 #endif
33 #if HAVE_LIBIPTC_LIBIPTC_H
34 # define IPTABLES_HAVE_READ 1
35 #else
36 # define IPTABLES_HAVE_READ 0
37 #endif
39 #define MODULE_NAME "iptables"
40 #define BUFSIZE 512
42 /*
43 * (Module-)Global variables
44 */
46 /*
47 * Removed packet count for now, should have config option if you want to save
48 * them Although other collectd models don't seem to care much for options
49 * eitherway for what to log
50 */
51 /* Limit to ~125MByte/s (~1GBit/s) */
52 static char *ds_def[] =
53 {
54 "DS:value:COUNTER:"COLLECTD_HEARTBEAT":0:134217728",
55 NULL
56 };
57 static int ds_num = 1;
59 #if IPTABLES_HAVE_READ
60 /*
61 * Config format should be `Chain table chainname',
62 * e. g. `Chain mangle incoming'
63 */
64 static char *config_keys[] =
65 {
66 "Chain",
67 NULL
68 };
69 static int config_keys_num = 1;
70 /*
71 Each table/chain combo that will be queried goes into this list
72 */
73 #ifndef XT_TABLE_MAXNAMELEN
74 # define XT_TABLE_MAXNAMELEN 32
75 #endif
76 typedef struct {
77 char table[XT_TABLE_MAXNAMELEN];
78 char chain[XT_TABLE_MAXNAMELEN];
79 union
80 {
81 int num;
82 char *comment;
83 } rule;
84 enum
85 {
86 RTYPE_NUM,
87 RTYPE_COMMENT,
88 RTYPE_COMMENT_ALL
89 } rule_type;
90 char name[64];
91 } ip_chain_t;
93 static ip_chain_t **chain_list = NULL;
94 static int chain_num = 0;
96 static int iptables_config (char *key, char *value)
97 {
98 if (strcasecmp (key, "Chain") == 0)
99 {
100 ip_chain_t temp, *final, **list;
101 char *table;
102 int table_len;
103 char *chain;
104 int chain_len;
106 char *value_copy;
107 char *fields[4];
108 int fields_num;
110 memset (&temp, 0, sizeof (temp));
112 value_copy = strdup (value);
113 if (value_copy == NULL)
114 {
115 syslog (LOG_ERR, "strdup failed: %s", strerror (errno));
116 return (1);
117 }
119 /* Chain <table> <chain> [<comment|num> [name]] */
120 fields_num = strsplit (value_copy, fields, 4);
121 if (fields_num < 2)
122 {
123 free (value_copy);
124 return (1);
125 }
127 table = fields[0];
128 chain = fields[1];
130 table_len = strlen (table);
131 if (table_len >= sizeof(temp.table))
132 {
133 syslog (LOG_ERR, "Table `%s' too long.", table);
134 free (value_copy);
135 return (1);
136 }
137 strncpy (temp.table, table, table_len);
138 temp.table[table_len] = '\0';
140 chain_len = strlen (chain);
141 if (chain_len >= sizeof(temp.chain))
142 {
143 syslog (LOG_ERR, "Chain `%s' too long.", chain);
144 free (value_copy);
145 return (1);
146 }
147 strncpy (temp.chain, chain, chain_len);
148 temp.chain[chain_len] = '\0';
150 if (fields_num >= 3)
151 {
152 char *comment = fields[2];
153 int rule = atoi (comment);
155 if (rule)
156 {
157 temp.rule.num = rule;
158 temp.rule_type = RTYPE_NUM;
159 }
160 else
161 {
162 strncpy (temp.rule.comment, comment,
163 sizeof (temp.rule.comment) - 1);
164 temp.rule_type = RTYPE_COMMENT;
165 }
166 }
167 else
168 {
169 temp.rule_type = RTYPE_COMMENT_ALL;
170 }
172 if (fields_num >= 4)
173 strncpy (temp.name, fields[3], sizeof (temp.name) - 1);
175 free (value_copy);
176 value_copy = NULL;
177 table = NULL;
178 chain = NULL;
180 list = (ip_chain_t **) realloc (chain_list, (chain_num + 1) * sizeof (ip_chain_t *));
181 if (list == NULL)
182 {
183 syslog (LOG_ERR, "realloc failed: %s", strerror (errno));
184 return (1);
185 }
187 chain_list = list;
188 final = (ip_chain_t *) malloc( sizeof(temp) );
189 if (final == NULL)
190 {
191 syslog (LOG_ERR, "malloc failed: %s", strerror (errno));
192 return (1);
193 }
194 memcpy (final, &temp, sizeof (temp));
195 chain_list[chain_num] = final;
196 chain_num++;
198 DBG ("Chain #%i: table = %s; chain = %s;", chain_num, final->table, final->chain);
199 }
200 else
201 {
202 return (-1);
203 }
205 return (0);
206 }
207 #endif /* IPTABLES_HAVE_READ */
209 static void iptables_init (void)
210 {
211 return;
212 }
214 static void iptables_write (char *host, char *orig_inst, char *val, char *type)
215 {
216 char *table;
217 char *inst;
218 char file[256];
219 int status;
221 table = strdup (orig_inst);
222 if (table == NULL)
223 return;
224 inst = strchr (table, ',');
225 if (inst == NULL)
226 {
227 free (table);
228 return;
229 }
231 *inst = '\0';
232 inst++;
233 if (*inst == '\0')
234 {
235 free (table);
236 return;
237 }
239 status = snprintf (file, sizeof (file), "iptables-%s/%s-%s.rrd",
240 table, type, inst);
241 free (table);
242 if ((status >= sizeof (file)) || (status < 1))
243 return;
245 rrd_update_file (host, file, val, ds_def, ds_num);
246 } /* void iptables_write */
248 static void iptables_write_bytes (char *host, char *inst, char *val)
249 {
250 iptables_write (host, inst, val, "ipt_bytes");
251 }
253 static void iptables_write_packets (char *host, char *inst, char *val)
254 {
255 iptables_write (host, inst, val, "ipt_packets");
256 }
258 #if IPTABLES_HAVE_READ
259 static int submit_match (const struct ipt_entry_match *match,
260 const struct ipt_entry *entry,
261 const ip_chain_t *chain,
262 int rule_num)
263 {
264 char inst[64];
265 char value[64];
266 int status;
268 /* Only log rules that have a comment, although could probably also do
269 * numerical targets sometime */
270 if (chain->rule_type == RTYPE_NUM)
271 {
272 if (chain->rule.num != rule_num)
273 return (0);
274 }
275 else
276 {
277 if (strcmp (match->u.user.name, "comment") != 0)
278 return 0;
279 if ((chain->rule_type == RTYPE_COMMENT)
280 && (strcmp (chain->rule.comment, (char *) match->data) != 0))
281 return (0);
282 }
284 if (chain->name[0] != '\0')
285 {
286 status = snprintf (inst, sizeof (inst), "%s-%s,%s",
287 chain->table, chain->chain, chain->name);
288 }
289 else
290 {
291 if (chain->rule_type == RTYPE_NUM)
292 status = snprintf (inst, sizeof (inst), "%s-%s,%i",
293 chain->table, chain->chain, chain->rule.num);
294 else
295 status = snprintf (inst, sizeof (inst), "%s-%s,%s",
296 chain->table, chain->chain, match->data);
298 if ((status >= sizeof (inst)) || (status < 1))
299 return (0);
300 }
302 status = snprintf (value, sizeof (value), "%u:%lld",
303 (unsigned int) curtime,
304 entry->counters.bcnt);
305 if ((status >= sizeof (value)) || (status < 1))
306 return 0;
307 plugin_submit ("ipt_bytes", inst, value);
309 status = snprintf (value, sizeof (value), "%u:%lld",
310 (unsigned int) curtime,
311 entry->counters.pcnt);
312 if ((status >= sizeof (value)) || (status < 1))
313 return 0;
314 plugin_submit ("ipt_packets", inst, value);
316 return 0;
317 } /* int submit_match */
319 static void submit_chain( iptc_handle_t *handle, ip_chain_t *chain ) {
320 const struct ipt_entry *entry;
321 int rule_num;
323 /* Find first rule for chain and use the iterate macro */
324 entry = iptc_first_rule( chain->chain, handle );
325 if (entry == NULL)
326 {
327 DBG ("iptc_first_rule failed: %s", iptc_strerror (errno));
328 return;
329 }
331 rule_num = 1;
332 while (entry)
333 {
334 if (chain->rule_type == RTYPE_NUM)
335 {
336 submit_match (NULL, entry, chain, rule_num);
337 }
338 else
339 {
340 IPT_MATCH_ITERATE( entry, submit_match, entry, chain, rule_num );
341 }
343 entry = iptc_next_rule( entry, handle );
344 rule_num++;
345 } /* while (entry) */
346 }
349 static void iptables_read (void)
350 {
351 int i;
352 static complain_t complaint;
354 /* Init the iptc handle structure and query the correct table */
355 for (i = 0; i < chain_num; i++)
356 {
357 iptc_handle_t handle;
358 ip_chain_t *chain;
360 chain = chain_list[i];
361 if (!chain)
362 {
363 DBG ("chain == NULL");
364 continue;
365 }
367 handle = iptc_init( chain->table );
368 if (!handle)
369 {
370 DBG ("iptc_init (%s) failed: %s", chain->table, iptc_strerror (errno));
371 plugin_complain (LOG_ERR, &complaint, "iptc_init (%s) failed: %s",
372 chain->table, iptc_strerror (errno));
373 continue;
374 }
375 plugin_relief (LOG_INFO, &complaint, "iptc_init (%s) succeeded",
376 chain->table);
378 submit_chain (&handle, chain);
379 iptc_free (&handle);
380 }
381 }
382 #else /* !IPTABLES_HAVE_READ */
383 # define iptables_read NULL
384 #endif
386 void module_register (void)
387 {
388 plugin_register ("ipt_bytes", NULL, NULL, iptables_write_bytes);
389 plugin_register ("ipt_packets", NULL, NULL, iptables_write_packets);
390 #if IPTABLES_HAVE_READ
391 plugin_register (MODULE_NAME, iptables_init, iptables_read, NULL);
392 cf_register (MODULE_NAME, iptables_config, config_keys, config_keys_num);
393 #endif
394 }
396 #undef BUFSIZE
397 #undef MODULE_NAME
399 /*
400 * vim:shiftwidth=4:softtabstop=4:tabstop=8
401 */