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