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;
114 else
115 return (1);
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);
236 return (0);
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,
242 int rule_num)
243 {
244 int status;
245 value_t values[1];
246 value_list_t vl = VALUE_LIST_INIT;
248 /* Select the rules to collect */
249 if (chain->rule_type == RTYPE_NUM)
250 {
251 if (chain->rule.num != rule_num)
252 return (0);
253 }
254 else
255 {
256 if (strcmp (match->u.user.name, "comment") != 0)
257 return (0);
258 if ((chain->rule_type == RTYPE_COMMENT)
259 && (strcmp (chain->rule.comment, (char *) match->data) != 0))
260 return (0);
261 }
263 vl.values = values;
264 vl.values_len = 1;
265 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
266 sstrncpy (vl.plugin, "ip6tables", sizeof (vl.plugin));
268 status = ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
269 "%s-%s", chain->table, chain->chain);
270 if ((status < 1) || ((unsigned int)status >= sizeof (vl.plugin_instance)))
271 return (0);
273 if (chain->name[0] != '\0')
274 {
275 sstrncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
276 }
277 else
278 {
279 if (chain->rule_type == RTYPE_NUM)
280 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
281 "%i", chain->rule.num);
282 else
283 sstrncpy (vl.type_instance, (char *) match->data,
284 sizeof (vl.type_instance));
285 }
287 sstrncpy (vl.type, "ipt_bytes", sizeof (vl.type));
288 values[0].derive = (derive_t) entry->counters.bcnt;
289 plugin_dispatch_values (&vl);
291 sstrncpy (vl.type, "ipt_packets", sizeof (vl.type));
292 values[0].derive = (derive_t) entry->counters.pcnt;
293 plugin_dispatch_values (&vl);
295 return (0);
296 } /* int submit_match */
299 /* This needs to return `int' for IPT_MATCH_ITERATE to work. */
300 static int submit_match (const struct ipt_entry_match *match,
301 const struct ipt_entry *entry,
302 const ip_chain_t *chain,
303 int rule_num)
304 {
305 int status;
306 value_t values[1];
307 value_list_t vl = VALUE_LIST_INIT;
309 /* Select the rules to collect */
310 if (chain->rule_type == RTYPE_NUM)
311 {
312 if (chain->rule.num != rule_num)
313 return (0);
314 }
315 else
316 {
317 if (strcmp (match->u.user.name, "comment") != 0)
318 return (0);
319 if ((chain->rule_type == RTYPE_COMMENT)
320 && (strcmp (chain->rule.comment, (char *) match->data) != 0))
321 return (0);
322 }
324 vl.values = values;
325 vl.values_len = 1;
326 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
327 sstrncpy (vl.plugin, "iptables", sizeof (vl.plugin));
329 status = ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
330 "%s-%s", chain->table, chain->chain);
331 if ((status < 1) || ((unsigned int)status >= sizeof (vl.plugin_instance)))
332 return (0);
334 if (chain->name[0] != '\0')
335 {
336 sstrncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
337 }
338 else
339 {
340 if (chain->rule_type == RTYPE_NUM)
341 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
342 "%i", chain->rule.num);
343 else
344 sstrncpy (vl.type_instance, (char *) match->data,
345 sizeof (vl.type_instance));
346 }
348 sstrncpy (vl.type, "ipt_bytes", sizeof (vl.type));
349 values[0].derive = (derive_t) entry->counters.bcnt;
350 plugin_dispatch_values (&vl);
352 sstrncpy (vl.type, "ipt_packets", sizeof (vl.type));
353 values[0].derive = (derive_t) entry->counters.pcnt;
354 plugin_dispatch_values (&vl);
356 return (0);
357 } /* int submit_match */
360 /* ipv6 submit_chain */
361 static void submit6_chain( ip6tc_handle_t *handle, ip_chain_t *chain )
362 {
363 const struct ip6t_entry *entry;
364 int rule_num;
366 /* Find first rule for chain and use the iterate macro */
367 entry = ip6tc_first_rule( chain->chain, handle );
368 if (entry == NULL)
369 {
370 DEBUG ("ip6tc_first_rule failed: %s", ip6tc_strerror (errno));
371 return;
372 }
374 rule_num = 1;
375 while (entry)
376 {
377 if (chain->rule_type == RTYPE_NUM)
378 {
379 submit6_match (NULL, entry, chain, rule_num);
380 }
381 else
382 {
383 IP6T_MATCH_ITERATE( entry, submit6_match, entry, chain, rule_num );
384 }
386 entry = ip6tc_next_rule( entry, handle );
387 rule_num++;
388 } /* while (entry) */
389 }
392 /* ipv4 submit_chain */
393 static void submit_chain( iptc_handle_t *handle, ip_chain_t *chain )
394 {
395 const struct ipt_entry *entry;
396 int rule_num;
398 /* Find first rule for chain and use the iterate macro */
399 entry = iptc_first_rule( chain->chain, handle );
400 if (entry == NULL)
401 {
402 DEBUG ("iptc_first_rule failed: %s", iptc_strerror (errno));
403 return;
404 }
406 rule_num = 1;
407 while (entry)
408 {
409 if (chain->rule_type == RTYPE_NUM)
410 {
411 submit_match (NULL, entry, chain, rule_num);
412 }
413 else
414 {
415 IPT_MATCH_ITERATE( entry, submit_match, entry, chain, rule_num );
416 }
418 entry = iptc_next_rule( entry, handle );
419 rule_num++;
420 } /* while (entry) */
421 }
424 static int iptables_read (void)
425 {
426 int i;
427 int num_failures = 0;
428 ip_chain_t *chain;
430 /* Init the iptc handle structure and query the correct table */
431 for (i = 0; i < chain_num; i++)
432 {
433 chain = chain_list[i];
435 if (!chain)
436 {
437 DEBUG ("iptables plugin: chain == NULL");
438 continue;
439 }
441 if ( chain->ip_version == IPV4 )
442 {
443 #ifdef HAVE_IPTC_HANDLE_T
444 iptc_handle_t _handle;
445 iptc_handle_t *handle = &_handle;
447 *handle = iptc_init (chain->table);
448 #else
449 iptc_handle_t *handle;
450 handle = iptc_init (chain->table);
451 #endif
453 if (!handle)
454 {
455 ERROR ("iptables plugin: iptc_init (%s) failed: %s",
456 chain->table, iptc_strerror (errno));
457 num_failures++;
458 continue;
459 }
461 submit_chain (handle, chain);
462 iptc_free (handle);
463 }
464 else if ( chain->ip_version == IPV6 )
465 {
466 #ifdef HAVE_IP6TC_HANDLE_T
467 ip6tc_handle_t _handle;
468 ip6tc_handle_t *handle = &_handle;
470 *handle = ip6tc_init (chain->table);
471 #else
472 ip6tc_handle_t *handle;
473 handle = ip6tc_init (chain->table);
474 #endif
476 if (!handle)
477 {
478 ERROR ("iptables plugin: ip6tc_init (%s) failed: %s",
479 chain->table, ip6tc_strerror (errno));
480 num_failures++;
481 continue;
482 }
484 submit6_chain (handle, chain);
485 ip6tc_free (handle);
486 }
487 else num_failures++;
489 } /* for (i = 0 .. chain_num) */
491 return ((num_failures < chain_num) ? 0 : -1);
492 } /* int iptables_read */
494 static int iptables_shutdown (void)
495 {
496 int i;
498 for (i = 0; i < chain_num; i++)
499 {
500 if ((chain_list[i] != NULL) && (chain_list[i]->rule_type == RTYPE_COMMENT))
501 {
502 sfree (chain_list[i]->rule.comment);
503 }
504 sfree (chain_list[i]);
505 }
506 sfree (chain_list);
508 return (0);
509 } /* int iptables_shutdown */
511 void module_register (void)
512 {
513 plugin_register_config ("iptables", iptables_config,
514 config_keys, config_keys_num);
515 plugin_register_read ("iptables", iptables_read);
516 plugin_register_shutdown ("iptables", iptables_shutdown);
517 } /* void module_register */
519 /*
520 * vim:shiftwidth=4:softtabstop=4:tabstop=8
521 */