Merge pull request #1821 from rubenk/memset
[collectd.git] / src / iptables.c
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  **/
26
27 #include "collectd.h"
28 #include "common.h"
29 #include "plugin.h"
30 #include "configfile.h"
31
32 #include <libiptc/libiptc.h>
33 #include <libiptc/libip6tc.h>
34
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
53
54 /*
55  * (Module-)Global variables
56  */
57
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 enum protocol_version_e
69 {
70     IPV4,
71     IPV6
72 };
73 typedef enum protocol_version_e protocol_version_t;
74
75 /*
76  * Each table/chain combo that will be queried goes into this list
77  */
78 #ifndef XT_TABLE_MAXNAMELEN
79 # define XT_TABLE_MAXNAMELEN 32
80 #endif
81 typedef struct {
82     protocol_version_t ip_version;
83     char table[XT_TABLE_MAXNAMELEN];
84     char chain[XT_TABLE_MAXNAMELEN];
85     union
86     {
87         int   num;
88         char *comment;
89     } rule;
90     enum
91     {
92         RTYPE_NUM,
93         RTYPE_COMMENT,
94         RTYPE_COMMENT_ALL
95     } rule_type;
96     char name[64];
97 } ip_chain_t;
98
99 static ip_chain_t **chain_list = NULL;
100 static int chain_num = 0;
101
102 static int iptables_config (const char *key, const char *value)
103 {
104     /* int ip_value; */
105     protocol_version_t ip_version = 0;
106
107     if (strcasecmp (key, "Chain") == 0)
108         ip_version = IPV4;
109     else if (strcasecmp (key, "Chain6") == 0)
110         ip_version = IPV6;
111     else
112         return (1);
113
114     ip_chain_t  temp = { 0 };
115     ip_chain_t *final, **list;
116     char *table;
117     int   table_len;
118     char *chain;
119     int   chain_len;
120
121     char *value_copy;
122     char *fields[4];
123     int   fields_num;
124
125     value_copy = strdup (value);
126     if (value_copy == NULL)
127     {
128         char errbuf[1024];
129         ERROR ("strdup failed: %s",
130                 sstrerror (errno, errbuf, sizeof (errbuf)));
131         return (1);
132     }
133
134     /*
135      *  Time to fill the temp element
136      *  Examine value string, it should look like:
137      *  Chain[6] <table> <chain> [<comment|num> [name]]
138      */
139
140     /* set IPv4 or IPv6 */
141     temp.ip_version = ip_version;
142
143     /* Chain <table> <chain> [<comment|num> [name]] */
144     fields_num = strsplit (value_copy, fields, 4);
145     if (fields_num < 2)
146     {
147         free (value_copy);
148         return (1);
149     }
150
151     table = fields[0];
152     chain = fields[1];
153
154     table_len = strlen (table) + 1;
155     if ((unsigned int)table_len > sizeof(temp.table))
156     {
157         ERROR ("Table `%s' too long.", table);
158         free (value_copy);
159         return (1);
160     }
161     sstrncpy (temp.table, table, table_len);
162
163     chain_len = strlen (chain) + 1;
164     if ((unsigned int)chain_len > sizeof(temp.chain))
165     {
166         ERROR ("Chain `%s' too long.", chain);
167         free (value_copy);
168         return (1);
169     }
170     sstrncpy (temp.chain, chain, chain_len);
171
172     if (fields_num >= 3)
173     {
174         char *comment = fields[2];
175         int   rule = atoi (comment);
176
177         if (rule)
178         {
179             temp.rule.num = rule;
180             temp.rule_type = RTYPE_NUM;
181         }
182         else
183         {
184             temp.rule.comment = strdup (comment);
185             if (temp.rule.comment == NULL)
186             {
187                 free (value_copy);
188                 return (1);
189             }
190             temp.rule_type = RTYPE_COMMENT;
191         }
192     }
193     else
194     {
195         temp.rule_type = RTYPE_COMMENT_ALL;
196     }
197
198     if (fields_num >= 4)
199         sstrncpy (temp.name, fields[3], sizeof (temp.name));
200
201     free (value_copy);
202     value_copy = NULL;
203     table = NULL;
204     chain = NULL;
205
206     list = realloc (chain_list, (chain_num + 1) * sizeof (ip_chain_t *));
207     if (list == NULL)
208     {
209         char errbuf[1024];
210         ERROR ("realloc failed: %s",
211                 sstrerror (errno, errbuf, sizeof (errbuf)));
212         sfree (temp.rule.comment);
213         return (1);
214     }
215
216     chain_list = list;
217     final = malloc(sizeof (*final));
218     if (final == NULL)
219     {
220         char errbuf[1024];
221         ERROR ("malloc failed: %s",
222                 sstrerror (errno, errbuf, sizeof (errbuf)));
223         sfree (temp.rule.comment);
224         return (1);
225     }
226     memcpy (final, &temp, sizeof (temp));
227     chain_list[chain_num] = final;
228     chain_num++;
229
230     DEBUG ("Chain #%i: table = %s; chain = %s;", chain_num, final->table, final->chain);
231
232     return (0);
233 } /* int iptables_config */
234
235 static int submit6_match (const struct ip6t_entry_match *match,
236                           const struct ip6t_entry *entry,
237                           const ip_chain_t *chain,
238                           int rule_num)
239 {
240     int status;
241     value_t values[1];
242     value_list_t vl = VALUE_LIST_INIT;
243
244     /* Select the rules to collect */
245     if (chain->rule_type == RTYPE_NUM)
246     {
247         if (chain->rule.num != rule_num)
248             return (0);
249     }
250     else
251     {
252         if (strcmp (match->u.user.name, "comment") != 0)
253             return (0);
254         if ((chain->rule_type == RTYPE_COMMENT)
255              && (strcmp (chain->rule.comment, (char *) match->data) != 0))
256             return (0);
257     }
258
259     vl.values = values;
260     vl.values_len = 1;
261     sstrncpy (vl.host, hostname_g, sizeof (vl.host));
262     sstrncpy (vl.plugin, "ip6tables", sizeof (vl.plugin));
263
264     status = ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
265                         "%s-%s", chain->table, chain->chain);
266     if ((status < 1) || ((unsigned int)status >= sizeof (vl.plugin_instance)))
267         return (0);
268
269     if (chain->name[0] != '\0')
270     {
271         sstrncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
272     }
273     else
274     {
275         if (chain->rule_type == RTYPE_NUM)
276             ssnprintf (vl.type_instance, sizeof (vl.type_instance),
277                        "%i", chain->rule.num);
278         else
279             sstrncpy (vl.type_instance, (char *) match->data,
280                       sizeof (vl.type_instance));
281     }
282
283     sstrncpy (vl.type, "ipt_bytes", sizeof (vl.type));
284     values[0].derive = (derive_t) entry->counters.bcnt;
285     plugin_dispatch_values (&vl);
286
287     sstrncpy (vl.type, "ipt_packets", sizeof (vl.type));
288     values[0].derive = (derive_t) entry->counters.pcnt;
289     plugin_dispatch_values (&vl);
290
291     return (0);
292 } /* int submit_match */
293
294
295 /* This needs to return `int' for IPT_MATCH_ITERATE to work. */
296 static int submit_match (const struct ipt_entry_match *match,
297                          const struct ipt_entry *entry,
298                          const ip_chain_t *chain,
299                          int rule_num)
300 {
301     int status;
302     value_t values[1];
303     value_list_t vl = VALUE_LIST_INIT;
304
305     /* Select the rules to collect */
306     if (chain->rule_type == RTYPE_NUM)
307     {
308         if (chain->rule.num != rule_num)
309             return (0);
310     }
311     else
312     {
313         if (strcmp (match->u.user.name, "comment") != 0)
314             return (0);
315         if ((chain->rule_type == RTYPE_COMMENT)
316              && (strcmp (chain->rule.comment, (char *) match->data) != 0))
317             return (0);
318     }
319
320     vl.values = values;
321     vl.values_len = 1;
322     sstrncpy (vl.host, hostname_g, sizeof (vl.host));
323     sstrncpy (vl.plugin, "iptables", sizeof (vl.plugin));
324
325     status = ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
326                         "%s-%s", chain->table, chain->chain);
327     if ((status < 1) || ((unsigned int)status >= sizeof (vl.plugin_instance)))
328         return (0);
329
330     if (chain->name[0] != '\0')
331     {
332         sstrncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
333     }
334     else
335     {
336         if (chain->rule_type == RTYPE_NUM)
337             ssnprintf (vl.type_instance, sizeof (vl.type_instance),
338                        "%i", chain->rule.num);
339         else
340             sstrncpy (vl.type_instance, (char *) match->data,
341                       sizeof (vl.type_instance));
342     }
343
344     sstrncpy (vl.type, "ipt_bytes", sizeof (vl.type));
345     values[0].derive = (derive_t) entry->counters.bcnt;
346     plugin_dispatch_values (&vl);
347
348     sstrncpy (vl.type, "ipt_packets", sizeof (vl.type));
349     values[0].derive = (derive_t) entry->counters.pcnt;
350     plugin_dispatch_values (&vl);
351
352     return (0);
353 } /* int submit_match */
354
355
356 /* ipv6 submit_chain */
357 static void submit6_chain (ip6tc_handle_t *handle, ip_chain_t *chain)
358 {
359     const struct ip6t_entry *entry;
360     int rule_num;
361
362     /* Find first rule for chain and use the iterate macro */
363     entry = ip6tc_first_rule( chain->chain, handle );
364     if (entry == NULL)
365     {
366         DEBUG ("ip6tc_first_rule failed: %s", ip6tc_strerror (errno));
367         return;
368     }
369
370     rule_num = 1;
371     while (entry)
372     {
373         if (chain->rule_type == RTYPE_NUM)
374         {
375             submit6_match (NULL, entry, chain, rule_num);
376         }
377         else
378         {
379             IP6T_MATCH_ITERATE( entry, submit6_match, entry, chain, rule_num );
380         }
381
382         entry = ip6tc_next_rule( entry, handle );
383         rule_num++;
384     } /* while (entry) */
385 }
386
387
388 /* ipv4 submit_chain */
389 static void submit_chain (iptc_handle_t *handle, ip_chain_t *chain)
390 {
391     const struct ipt_entry *entry;
392     int rule_num;
393
394     /* Find first rule for chain and use the iterate macro */
395     entry = iptc_first_rule( chain->chain, handle );
396     if (entry == NULL)
397     {
398         DEBUG ("iptc_first_rule failed: %s", iptc_strerror (errno));
399         return;
400     }
401
402     rule_num = 1;
403     while (entry)
404     {
405         if (chain->rule_type == RTYPE_NUM)
406         {
407             submit_match (NULL, entry, chain, rule_num);
408         }
409         else
410         {
411             IPT_MATCH_ITERATE( entry, submit_match, entry, chain, rule_num );
412         }
413
414         entry = iptc_next_rule( entry, handle );
415         rule_num++;
416     } /* while (entry) */
417 }
418
419
420 static int iptables_read (void)
421 {
422     int i;
423     int num_failures = 0;
424     ip_chain_t *chain;
425
426     /* Init the iptc handle structure and query the correct table */
427     for (i = 0; i < chain_num; i++)
428     {
429         chain = chain_list[i];
430
431         if (!chain)
432         {
433             DEBUG ("iptables plugin: chain == NULL");
434             continue;
435         }
436
437         if ( chain->ip_version == IPV4 )
438         {
439 #ifdef HAVE_IPTC_HANDLE_T
440             iptc_handle_t _handle;
441             iptc_handle_t *handle = &_handle;
442
443             *handle = iptc_init (chain->table);
444 #else
445             iptc_handle_t *handle;
446             handle = iptc_init (chain->table);
447 #endif
448
449             if (!handle)
450             {
451                 ERROR ("iptables plugin: iptc_init (%s) failed: %s",
452                         chain->table, iptc_strerror (errno));
453                 num_failures++;
454                 continue;
455             }
456
457             submit_chain (handle, chain);
458             iptc_free (handle);
459         }
460         else if ( chain->ip_version == IPV6 )
461         {
462 #ifdef HAVE_IP6TC_HANDLE_T
463             ip6tc_handle_t _handle;
464             ip6tc_handle_t *handle = &_handle;
465
466             *handle = ip6tc_init (chain->table);
467 #else
468             ip6tc_handle_t *handle;
469             handle = ip6tc_init (chain->table);
470 #endif
471             if (!handle)
472             {
473                 ERROR ("iptables plugin: ip6tc_init (%s) failed: %s",
474                         chain->table, ip6tc_strerror (errno));
475                 num_failures++;
476                 continue;
477             }
478
479             submit6_chain (handle, chain);
480             ip6tc_free (handle);
481         }
482         else
483             num_failures++;
484     } /* for (i = 0 .. chain_num) */
485
486     return ((num_failures < chain_num) ? 0 : -1);
487 } /* int iptables_read */
488
489 static int iptables_shutdown (void)
490 {
491     int i;
492
493     for (i = 0; i < chain_num; i++)
494     {
495         if ((chain_list[i] != NULL) && (chain_list[i]->rule_type == RTYPE_COMMENT))
496             sfree (chain_list[i]->rule.comment);
497         sfree (chain_list[i]);
498     }
499     sfree (chain_list);
500
501     return (0);
502 } /* int iptables_shutdown */
503
504 void module_register (void)
505 {
506     plugin_register_config ("iptables", iptables_config,
507                              config_keys, config_keys_num);
508     plugin_register_read ("iptables", iptables_read);
509     plugin_register_shutdown ("iptables", iptables_shutdown);
510 } /* void module_register */
511