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