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