1 /**
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
6 *
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.
11 *
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.
16 *
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
20 *
21 * Authors:
22 * Sjoerd van der Berg <harekiet at users.sourceforge.net>
23 * Florian Forster <octo at verplant.org>
24 * Marco Chiappero <marco at absence.it>
25 **/
27 #include "collectd.h"
28 #include "common.h"
29 #include "plugin.h"
30 #include "configfile.h"
32 #include <sys/socket.h>
34 #if OWN_LIBIPTC
35 # include "libiptc/libiptc.h"
36 # include "libiptc/libip6tc.h"
37 #else
38 # include <libiptc/libiptc.h>
39 # include <libiptc/libip6tc.h>
40 #endif
42 /*
43 * (Module-)Global variables
44 */
46 /*
47 * Config format should be `Chain table chainname',
48 * e. g. `Chain mangle incoming'
49 */
50 static const char *config_keys[] =
51 {
52 "Chain",
53 "Chain6"
54 };
55 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
56 /*
57 Each table/chain combo that will be queried goes into this list
58 */
60 enum protocol_version_e
61 {
62 IPV4,
63 IPV6
64 };
65 typedef enum protocol_version_e protocol_version_t;
67 #ifndef XT_TABLE_MAXNAMELEN
68 # define XT_TABLE_MAXNAMELEN 32
69 #endif
70 typedef struct {
71 protocol_version_t ip_version;
72 char table[XT_TABLE_MAXNAMELEN];
73 char chain[XT_TABLE_MAXNAMELEN];
74 union
75 {
76 int num;
77 char *comment;
78 } rule;
79 enum
80 {
81 RTYPE_NUM,
82 RTYPE_COMMENT,
83 RTYPE_COMMENT_ALL
84 } rule_type;
85 char name[64];
86 } ip_chain_t;
88 static ip_chain_t **chain_list = NULL;
89 static int chain_num = 0;
91 static int iptables_config (const char *key, const char *value)
92 {
93 /* int ip_value; */
94 protocol_version_t ip_version = 0;
96 if (strcasecmp (key, "Chain") == 0)
97 ip_version = IPV4;
98 else if (strcasecmp (key, "Chain6") == 0)
99 ip_version = IPV6;
101 if (( ip_version == IPV4 ) || ( ip_version == IPV6 ))
102 {
103 ip_chain_t temp, *final, **list;
104 char *table;
105 int table_len;
106 char *chain;
107 int chain_len;
109 char *value_copy;
110 char *fields[4];
111 int fields_num;
113 memset (&temp, 0, sizeof (temp));
115 value_copy = strdup (value);
116 if (value_copy == NULL)
117 {
118 char errbuf[1024];
119 ERROR ("strdup failed: %s",
120 sstrerror (errno, errbuf, sizeof (errbuf)));
121 return (1);
122 }
124 /*
125 * Time to fill the temp element
126 * Examine value string, it should look like:
127 * Chain[6] <table> <chain> [<comment|num> [name]]
128 */
130 /* set IPv4 or IPv6 */
131 temp.ip_version = ip_version;
133 /* Chain <table> <chain> [<comment|num> [name]] */
134 fields_num = strsplit (value_copy, fields, 4);
135 if (fields_num < 2)
136 {
137 free (value_copy);
138 return (1);
139 }
141 table = fields[0];
142 chain = fields[1];
144 table_len = strlen (table) + 1;
145 if ((unsigned int)table_len > sizeof(temp.table))
146 {
147 ERROR ("Table `%s' too long.", table);
148 free (value_copy);
149 return (1);
150 }
151 sstrncpy (temp.table, table, table_len);
153 chain_len = strlen (chain) + 1;
154 if ((unsigned int)chain_len > sizeof(temp.chain))
155 {
156 ERROR ("Chain `%s' too long.", chain);
157 free (value_copy);
158 return (1);
159 }
160 sstrncpy (temp.chain, chain, chain_len);
162 if (fields_num >= 3)
163 {
164 char *comment = fields[2];
165 int rule = atoi (comment);
167 if (rule)
168 {
169 temp.rule.num = rule;
170 temp.rule_type = RTYPE_NUM;
171 }
172 else
173 {
174 temp.rule.comment = strdup (comment);
175 if (temp.rule.comment == NULL)
176 {
177 free (value_copy);
178 return (1);
179 }
180 temp.rule_type = RTYPE_COMMENT;
181 }
182 }
183 else
184 {
185 temp.rule_type = RTYPE_COMMENT_ALL;
186 }
188 if (fields_num >= 4)
189 sstrncpy (temp.name, fields[3], sizeof (temp.name));
191 free (value_copy);
192 value_copy = NULL;
193 table = NULL;
194 chain = NULL;
196 list = (ip_chain_t **) realloc (chain_list, (chain_num + 1) * sizeof (ip_chain_t *));
197 if (list == NULL)
198 {
199 char errbuf[1024];
200 ERROR ("realloc failed: %s",
201 sstrerror (errno, errbuf, sizeof (errbuf)));
202 return (1);
203 }
205 chain_list = list;
206 final = (ip_chain_t *) malloc( sizeof(temp) );
207 if (final == NULL)
208 {
209 char errbuf[1024];
210 ERROR ("malloc failed: %s",
211 sstrerror (errno, errbuf, sizeof (errbuf)));
212 return (1);
213 }
214 memcpy (final, &temp, sizeof (temp));
215 chain_list[chain_num] = final;
216 chain_num++;
218 DEBUG ("Chain #%i: table = %s; chain = %s;", chain_num, final->table, final->chain);
219 }
220 else
221 {
222 return (-1);
223 }
225 return (0);
226 } /* int iptables_config */
228 static int submit6_match (const struct ip6t_entry_match *match,
229 const struct ip6t_entry *entry,
230 const ip_chain_t *chain,
231 int rule_num)
232 {
233 int status;
234 value_t values[1];
235 value_list_t vl = VALUE_LIST_INIT;
237 /* Select the rules to collect */
238 if (chain->rule_type == RTYPE_NUM)
239 {
240 if (chain->rule.num != rule_num)
241 return (0);
242 }
243 else
244 {
245 if (strcmp (match->u.user.name, "comment") != 0)
246 return (0);
247 if ((chain->rule_type == RTYPE_COMMENT)
248 && (strcmp (chain->rule.comment, (char *) match->data) != 0))
249 return (0);
250 }
252 vl.values = values;
253 vl.values_len = 1;
254 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
255 sstrncpy (vl.plugin, "ip6tables", sizeof (vl.plugin));
257 status = ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
258 "%s-%s", chain->table, chain->chain);
259 if ((status < 1) || ((unsigned int)status >= sizeof (vl.plugin_instance)))
260 return (0);
262 if (chain->name[0] != '\0')
263 {
264 sstrncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
265 }
266 else
267 {
268 if (chain->rule_type == RTYPE_NUM)
269 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
270 "%i", chain->rule.num);
271 else
272 sstrncpy (vl.type_instance, (char *) match->data,
273 sizeof (vl.type_instance));
274 }
276 sstrncpy (vl.type, "ipt_bytes", sizeof (vl.type));
277 values[0].counter = (counter_t) entry->counters.bcnt;
278 plugin_dispatch_values (&vl);
280 sstrncpy (vl.type, "ipt_packets", sizeof (vl.type));
281 values[0].counter = (counter_t) entry->counters.pcnt;
282 plugin_dispatch_values (&vl);
284 return (0);
285 } /* int submit_match */
288 /* This needs to return `int' for IPT_MATCH_ITERATE to work. */
289 static int submit_match (const struct ipt_entry_match *match,
290 const struct ipt_entry *entry,
291 const ip_chain_t *chain,
292 int rule_num)
293 {
294 int status;
295 value_t values[1];
296 value_list_t vl = VALUE_LIST_INIT;
298 /* Select the rules to collect */
299 if (chain->rule_type == RTYPE_NUM)
300 {
301 if (chain->rule.num != rule_num)
302 return (0);
303 }
304 else
305 {
306 if (strcmp (match->u.user.name, "comment") != 0)
307 return (0);
308 if ((chain->rule_type == RTYPE_COMMENT)
309 && (strcmp (chain->rule.comment, (char *) match->data) != 0))
310 return (0);
311 }
313 vl.values = values;
314 vl.values_len = 1;
315 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
316 sstrncpy (vl.plugin, "iptables", sizeof (vl.plugin));
318 status = ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
319 "%s-%s", chain->table, chain->chain);
320 if ((status < 1) || ((unsigned int)status >= sizeof (vl.plugin_instance)))
321 return (0);
323 if (chain->name[0] != '\0')
324 {
325 sstrncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
326 }
327 else
328 {
329 if (chain->rule_type == RTYPE_NUM)
330 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
331 "%i", chain->rule.num);
332 else
333 sstrncpy (vl.type_instance, (char *) match->data,
334 sizeof (vl.type_instance));
335 }
337 sstrncpy (vl.type, "ipt_bytes", sizeof (vl.type));
338 values[0].counter = (counter_t) entry->counters.bcnt;
339 plugin_dispatch_values (&vl);
341 sstrncpy (vl.type, "ipt_packets", sizeof (vl.type));
342 values[0].counter = (counter_t) entry->counters.pcnt;
343 plugin_dispatch_values (&vl);
345 return (0);
346 } /* int submit_match */
349 /* ipv6 submit_chain */
350 static void submit6_chain( ip6tc_handle_t *handle, ip_chain_t *chain )
351 {
352 const struct ip6t_entry *entry;
353 int rule_num;
355 /* Find first rule for chain and use the iterate macro */
356 entry = ip6tc_first_rule( chain->chain, handle );
357 if (entry == NULL)
358 {
359 DEBUG ("ip6tc_first_rule failed: %s", ip6tc_strerror (errno));
360 return;
361 }
363 rule_num = 1;
364 while (entry)
365 {
366 if (chain->rule_type == RTYPE_NUM)
367 {
368 submit6_match (NULL, entry, chain, rule_num);
369 }
370 else
371 {
372 IP6T_MATCH_ITERATE( entry, submit6_match, entry, chain, rule_num );
373 }
375 entry = ip6tc_next_rule( entry, handle );
376 rule_num++;
377 } /* while (entry) */
378 }
381 /* ipv4 submit_chain */
382 static void submit_chain( iptc_handle_t *handle, ip_chain_t *chain )
383 {
384 const struct ipt_entry *entry;
385 int rule_num;
387 /* Find first rule for chain and use the iterate macro */
388 entry = iptc_first_rule( chain->chain, handle );
389 if (entry == NULL)
390 {
391 DEBUG ("iptc_first_rule failed: %s", iptc_strerror (errno));
392 return;
393 }
395 rule_num = 1;
396 while (entry)
397 {
398 if (chain->rule_type == RTYPE_NUM)
399 {
400 submit_match (NULL, entry, chain, rule_num);
401 }
402 else
403 {
404 IPT_MATCH_ITERATE( entry, submit_match, entry, chain, rule_num );
405 }
407 entry = iptc_next_rule( entry, handle );
408 rule_num++;
409 } /* while (entry) */
410 }
413 static int iptables_read (void)
414 {
415 int i;
416 int num_failures = 0;
417 ip_chain_t *chain;
419 /* Init the iptc handle structure and query the correct table */
420 for (i = 0; i < chain_num; i++)
421 {
422 chain = chain_list[i];
424 if (!chain)
425 {
426 DEBUG ("iptables plugin: chain == NULL");
427 continue;
428 }
430 if ( chain->ip_version == IPV4 )
431 {
432 iptc_handle_t handle;
433 handle = iptc_init (chain->table);
435 if (!handle)
436 {
437 ERROR ("iptables plugin: iptc_init (%s) failed: %s",
438 chain->table, iptc_strerror (errno));
439 num_failures++;
440 continue;
441 }
443 submit_chain (&handle, chain);
444 iptc_free (&handle);
445 }
446 else if ( chain->ip_version == IPV6 )
447 {
448 ip6tc_handle_t handle;
449 handle = ip6tc_init (chain->table);
451 if (!handle)
452 {
453 ERROR ("iptables plugin: ip6tc_init (%s) failed: %s",
454 chain->table, ip6tc_strerror (errno));
455 num_failures++;
456 continue;
457 }
459 submit6_chain (&handle, chain);
460 ip6tc_free (&handle);
461 }
462 else num_failures++;
464 } /* for (i = 0 .. chain_num) */
466 return ((num_failures < chain_num) ? 0 : -1);
467 } /* int iptables_read */
469 static int iptables_shutdown (void)
470 {
471 int i;
473 for (i = 0; i < chain_num; i++)
474 {
475 if ((chain_list[i] != NULL) && (chain_list[i]->rule_type == RTYPE_COMMENT))
476 {
477 sfree (chain_list[i]->rule.comment);
478 }
479 sfree (chain_list[i]);
480 }
481 sfree (chain_list);
483 return (0);
484 } /* int iptables_shutdown */
486 void module_register (void)
487 {
488 plugin_register_config ("iptables", iptables_config,
489 config_keys, config_keys_num);
490 plugin_register_read ("iptables", iptables_read);
491 plugin_register_shutdown ("iptables", iptables_shutdown);
492 } /* void module_register */
494 /*
495 * vim:shiftwidth=4:softtabstop=4:tabstop=8
496 */