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