1 /**
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
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 collectd.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 #include <libiptc/libiptc.h>
35 #include <libiptc/libip6tc.h>
37 /*
38 * iptc_handle_t was available before libiptc was officially available as a
39 * shared library. Note, that when the shared lib was introduced, the API and
40 * ABI have changed slightly:
41 * 'iptc_handle_t' used to be 'struct iptc_handle *' and most functions used
42 * 'iptc_handle_t *' as an argument. Now, most functions use 'struct
43 * iptc_handle *' (thus removing one level of pointer indirection).
44 *
45 * HAVE_IPTC_HANDLE_T is used to determine which API ought to be used. While
46 * this is somewhat hacky, I didn't find better way to solve that :-/
47 * -tokkee
48 */
49 #ifndef HAVE_IPTC_HANDLE_T
50 typedef struct iptc_handle iptc_handle_t;
51 #endif
52 #ifndef HAVE_IP6TC_HANDLE_T
53 typedef struct ip6tc_handle ip6tc_handle_t;
54 #endif
56 /*
57 * (Module-)Global variables
58 */
60 /*
61 * Config format should be `Chain table chainname',
62 * e. g. `Chain mangle incoming'
63 */
64 static const char *config_keys[] =
65 {
66 "Chain",
67 "Chain6"
68 };
69 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
70 /*
71 Each table/chain combo that will be queried goes into this list
72 */
74 enum protocol_version_e
75 {
76 IPV4,
77 IPV6
78 };
79 typedef enum protocol_version_e protocol_version_t;
81 #ifndef XT_TABLE_MAXNAMELEN
82 # define XT_TABLE_MAXNAMELEN 32
83 #endif
84 typedef struct {
85 protocol_version_t ip_version;
86 char table[XT_TABLE_MAXNAMELEN];
87 char chain[XT_TABLE_MAXNAMELEN];
88 union
89 {
90 int num;
91 char *comment;
92 } rule;
93 enum
94 {
95 RTYPE_NUM,
96 RTYPE_COMMENT,
97 RTYPE_COMMENT_ALL
98 } rule_type;
99 char name[64];
100 } ip_chain_t;
102 static ip_chain_t **chain_list = NULL;
103 static int chain_num = 0;
105 static int iptables_config (const char *key, const char *value)
106 {
107 /* int ip_value; */
108 protocol_version_t ip_version = 0;
110 if (strcasecmp (key, "Chain") == 0)
111 ip_version = IPV4;
112 else if (strcasecmp (key, "Chain6") == 0)
113 ip_version = IPV6;
115 if (( ip_version == IPV4 ) || ( ip_version == IPV6 ))
116 {
117 ip_chain_t temp, *final, **list;
118 char *table;
119 int table_len;
120 char *chain;
121 int chain_len;
123 char *value_copy;
124 char *fields[4];
125 int fields_num;
127 memset (&temp, 0, sizeof (temp));
129 value_copy = strdup (value);
130 if (value_copy == NULL)
131 {
132 char errbuf[1024];
133 ERROR ("strdup failed: %s",
134 sstrerror (errno, errbuf, sizeof (errbuf)));
135 return (1);
136 }
138 /*
139 * Time to fill the temp element
140 * Examine value string, it should look like:
141 * Chain[6] <table> <chain> [<comment|num> [name]]
142 */
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);
149 if (fields_num < 2)
150 {
151 free (value_copy);
152 return (1);
153 }
155 table = fields[0];
156 chain = fields[1];
158 table_len = strlen (table) + 1;
159 if ((unsigned int)table_len > sizeof(temp.table))
160 {
161 ERROR ("Table `%s' too long.", table);
162 free (value_copy);
163 return (1);
164 }
165 sstrncpy (temp.table, table, table_len);
167 chain_len = strlen (chain) + 1;
168 if ((unsigned int)chain_len > sizeof(temp.chain))
169 {
170 ERROR ("Chain `%s' too long.", chain);
171 free (value_copy);
172 return (1);
173 }
174 sstrncpy (temp.chain, chain, chain_len);
176 if (fields_num >= 3)
177 {
178 char *comment = fields[2];
179 int rule = atoi (comment);
181 if (rule)
182 {
183 temp.rule.num = rule;
184 temp.rule_type = RTYPE_NUM;
185 }
186 else
187 {
188 temp.rule.comment = strdup (comment);
189 if (temp.rule.comment == NULL)
190 {
191 free (value_copy);
192 return (1);
193 }
194 temp.rule_type = RTYPE_COMMENT;
195 }
196 }
197 else
198 {
199 temp.rule_type = RTYPE_COMMENT_ALL;
200 }
202 if (fields_num >= 4)
203 sstrncpy (temp.name, fields[3], sizeof (temp.name));
205 free (value_copy);
206 value_copy = NULL;
207 table = NULL;
208 chain = NULL;
210 list = (ip_chain_t **) realloc (chain_list, (chain_num + 1) * sizeof (ip_chain_t *));
211 if (list == NULL)
212 {
213 char errbuf[1024];
214 ERROR ("realloc failed: %s",
215 sstrerror (errno, errbuf, sizeof (errbuf)));
216 sfree (temp.rule.comment);
217 return (1);
218 }
220 chain_list = list;
221 final = (ip_chain_t *) malloc( sizeof(temp) );
222 if (final == NULL)
223 {
224 char errbuf[1024];
225 ERROR ("malloc failed: %s",
226 sstrerror (errno, errbuf, sizeof (errbuf)));
227 sfree (temp.rule.comment);
228 return (1);
229 }
230 memcpy (final, &temp, sizeof (temp));
231 chain_list[chain_num] = final;
232 chain_num++;
234 DEBUG ("Chain #%i: table = %s; chain = %s;", chain_num, final->table, final->chain);
235 }
236 else
237 {
238 return (-1);
239 }
241 return (0);
242 } /* int iptables_config */
244 static int submit6_match (const struct ip6t_entry_match *match,
245 const struct ip6t_entry *entry,
246 const ip_chain_t *chain,
247 int rule_num)
248 {
249 int status;
250 value_t values[1];
251 value_list_t vl = VALUE_LIST_INIT;
253 /* Select the rules to collect */
254 if (chain->rule_type == RTYPE_NUM)
255 {
256 if (chain->rule.num != rule_num)
257 return (0);
258 }
259 else
260 {
261 if (strcmp (match->u.user.name, "comment") != 0)
262 return (0);
263 if ((chain->rule_type == RTYPE_COMMENT)
264 && (strcmp (chain->rule.comment, (char *) match->data) != 0))
265 return (0);
266 }
268 vl.values = values;
269 vl.values_len = 1;
270 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
271 sstrncpy (vl.plugin, "ip6tables", sizeof (vl.plugin));
273 status = ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
274 "%s-%s", chain->table, chain->chain);
275 if ((status < 1) || ((unsigned int)status >= sizeof (vl.plugin_instance)))
276 return (0);
278 if (chain->name[0] != '\0')
279 {
280 sstrncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
281 }
282 else
283 {
284 if (chain->rule_type == RTYPE_NUM)
285 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
286 "%i", chain->rule.num);
287 else
288 sstrncpy (vl.type_instance, (char *) match->data,
289 sizeof (vl.type_instance));
290 }
292 sstrncpy (vl.type, "ipt_bytes", sizeof (vl.type));
293 values[0].derive = (derive_t) entry->counters.bcnt;
294 plugin_dispatch_values (&vl);
296 sstrncpy (vl.type, "ipt_packets", sizeof (vl.type));
297 values[0].derive = (derive_t) entry->counters.pcnt;
298 plugin_dispatch_values (&vl);
300 return (0);
301 } /* int submit_match */
304 /* This needs to return `int' for IPT_MATCH_ITERATE to work. */
305 static int submit_match (const struct ipt_entry_match *match,
306 const struct ipt_entry *entry,
307 const ip_chain_t *chain,
308 int rule_num)
309 {
310 int status;
311 value_t values[1];
312 value_list_t vl = VALUE_LIST_INIT;
314 /* Select the rules to collect */
315 if (chain->rule_type == RTYPE_NUM)
316 {
317 if (chain->rule.num != rule_num)
318 return (0);
319 }
320 else
321 {
322 if (strcmp (match->u.user.name, "comment") != 0)
323 return (0);
324 if ((chain->rule_type == RTYPE_COMMENT)
325 && (strcmp (chain->rule.comment, (char *) match->data) != 0))
326 return (0);
327 }
329 vl.values = values;
330 vl.values_len = 1;
331 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
332 sstrncpy (vl.plugin, "iptables", sizeof (vl.plugin));
334 status = ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
335 "%s-%s", chain->table, chain->chain);
336 if ((status < 1) || ((unsigned int)status >= sizeof (vl.plugin_instance)))
337 return (0);
339 if (chain->name[0] != '\0')
340 {
341 sstrncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
342 }
343 else
344 {
345 if (chain->rule_type == RTYPE_NUM)
346 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
347 "%i", chain->rule.num);
348 else
349 sstrncpy (vl.type_instance, (char *) match->data,
350 sizeof (vl.type_instance));
351 }
353 sstrncpy (vl.type, "ipt_bytes", sizeof (vl.type));
354 values[0].derive = (derive_t) entry->counters.bcnt;
355 plugin_dispatch_values (&vl);
357 sstrncpy (vl.type, "ipt_packets", sizeof (vl.type));
358 values[0].derive = (derive_t) entry->counters.pcnt;
359 plugin_dispatch_values (&vl);
361 return (0);
362 } /* int submit_match */
365 /* ipv6 submit_chain */
366 static void submit6_chain( ip6tc_handle_t *handle, ip_chain_t *chain )
367 {
368 const struct ip6t_entry *entry;
369 int rule_num;
371 /* Find first rule for chain and use the iterate macro */
372 entry = ip6tc_first_rule( chain->chain, handle );
373 if (entry == NULL)
374 {
375 DEBUG ("ip6tc_first_rule failed: %s", ip6tc_strerror (errno));
376 return;
377 }
379 rule_num = 1;
380 while (entry)
381 {
382 if (chain->rule_type == RTYPE_NUM)
383 {
384 submit6_match (NULL, entry, chain, rule_num);
385 }
386 else
387 {
388 IP6T_MATCH_ITERATE( entry, submit6_match, entry, chain, rule_num );
389 }
391 entry = ip6tc_next_rule( entry, handle );
392 rule_num++;
393 } /* while (entry) */
394 }
397 /* ipv4 submit_chain */
398 static void submit_chain( iptc_handle_t *handle, ip_chain_t *chain )
399 {
400 const struct ipt_entry *entry;
401 int rule_num;
403 /* Find first rule for chain and use the iterate macro */
404 entry = iptc_first_rule( chain->chain, handle );
405 if (entry == NULL)
406 {
407 DEBUG ("iptc_first_rule failed: %s", iptc_strerror (errno));
408 return;
409 }
411 rule_num = 1;
412 while (entry)
413 {
414 if (chain->rule_type == RTYPE_NUM)
415 {
416 submit_match (NULL, entry, chain, rule_num);
417 }
418 else
419 {
420 IPT_MATCH_ITERATE( entry, submit_match, entry, chain, rule_num );
421 }
423 entry = iptc_next_rule( entry, handle );
424 rule_num++;
425 } /* while (entry) */
426 }
429 static int iptables_read (void)
430 {
431 int i;
432 int num_failures = 0;
433 ip_chain_t *chain;
435 /* Init the iptc handle structure and query the correct table */
436 for (i = 0; i < chain_num; i++)
437 {
438 chain = chain_list[i];
440 if (!chain)
441 {
442 DEBUG ("iptables plugin: chain == NULL");
443 continue;
444 }
446 if ( chain->ip_version == IPV4 )
447 {
448 #ifdef HAVE_IPTC_HANDLE_T
449 iptc_handle_t _handle;
450 iptc_handle_t *handle = &_handle;
452 *handle = iptc_init (chain->table);
453 #else
454 iptc_handle_t *handle;
455 handle = iptc_init (chain->table);
456 #endif
458 if (!handle)
459 {
460 ERROR ("iptables plugin: iptc_init (%s) failed: %s",
461 chain->table, iptc_strerror (errno));
462 num_failures++;
463 continue;
464 }
466 submit_chain (handle, chain);
467 iptc_free (handle);
468 }
469 else if ( chain->ip_version == IPV6 )
470 {
471 #ifdef HAVE_IP6TC_HANDLE_T
472 ip6tc_handle_t _handle;
473 ip6tc_handle_t *handle = &_handle;
475 *handle = ip6tc_init (chain->table);
476 #else
477 ip6tc_handle_t *handle;
478 handle = ip6tc_init (chain->table);
479 #endif
481 if (!handle)
482 {
483 ERROR ("iptables plugin: ip6tc_init (%s) failed: %s",
484 chain->table, ip6tc_strerror (errno));
485 num_failures++;
486 continue;
487 }
489 submit6_chain (handle, chain);
490 ip6tc_free (handle);
491 }
492 else num_failures++;
494 } /* for (i = 0 .. chain_num) */
496 return ((num_failures < chain_num) ? 0 : -1);
497 } /* int iptables_read */
499 static int iptables_shutdown (void)
500 {
501 int i;
503 for (i = 0; i < chain_num; i++)
504 {
505 if ((chain_list[i] != NULL) && (chain_list[i]->rule_type == RTYPE_COMMENT))
506 {
507 sfree (chain_list[i]->rule.comment);
508 }
509 sfree (chain_list[i]);
510 }
511 sfree (chain_list);
513 return (0);
514 } /* int iptables_shutdown */
516 void module_register (void)
517 {
518 plugin_register_config ("iptables", iptables_config,
519 config_keys, config_keys_num);
520 plugin_register_read ("iptables", iptables_read);
521 plugin_register_shutdown ("iptables", iptables_shutdown);
522 } /* void module_register */
524 /*
525 * vim:shiftwidth=4:softtabstop=4:tabstop=8
526 */