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