Merging with changes made by Florian Foster.
[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 /*
69     Each table/chain combo that will be queried goes into this list
70 */
71
72 enum protocol_version_e
73 {
74     IPV4,
75     IPV6
76 };
77 typedef enum protocol_version_e protocol_version_t;
78
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;
99
100 static ip_chain_t **chain_list = NULL;
101 static int chain_num = 0;
102
103 static int iptables_config (const char *key, const char *value)
104 {
105         /* int ip_value; */
106         protocol_version_t ip_version = 0;
107
108         if (strcasecmp (key, "Chain") == 0)
109                 ip_version = IPV4;
110         else if (strcasecmp (key, "Chain6") == 0)
111                 ip_version = IPV6;
112
113         if (( ip_version == IPV4 ) || ( ip_version == IPV6 ))
114         {
115                 ip_chain_t temp, *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                 memset (&temp, 0, sizeof (temp));
126
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                 }
135
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                  */
141
142                 /* set IPv4 or IPv6 */
143                 temp.ip_version = ip_version;
144
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                 }
152
153                 table = fields[0];
154                 chain = fields[1];
155
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);
164
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);
173
174                 if (fields_num >= 3)
175                 {
176                     char *comment = fields[2];
177                     int   rule = atoi (comment);
178
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                 }
199
200                 if (fields_num >= 4)
201                     sstrncpy (temp.name, fields[3], sizeof (temp.name));
202
203                 free (value_copy);
204                 value_copy = NULL;
205                 table = NULL;
206                 chain = NULL;
207
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                 }
217
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++;
231
232                 DEBUG ("Chain #%i: table = %s; chain = %s;", chain_num, final->table, final->chain);
233         }
234         else 
235         {
236                 return (-1);
237         }
238
239         return (0);
240 } /* int iptables_config */
241
242 static int submit6_match (const struct ip6t_entry_match *match,
243                 const struct ip6t_entry *entry,
244                 const ip_chain_t *chain,
245                 int rule_num)
246 {
247     int status;
248     value_t values[1];
249     value_list_t vl = VALUE_LIST_INIT;
250
251     /* Select the rules to collect */
252     if (chain->rule_type == RTYPE_NUM)
253     {
254         if (chain->rule.num != rule_num)
255             return (0);
256     }
257     else
258     {
259         if (strcmp (match->u.user.name, "comment") != 0)
260             return (0);
261         if ((chain->rule_type == RTYPE_COMMENT)
262                 && (strcmp (chain->rule.comment, (char *) match->data) != 0))
263             return (0);
264     }
265
266     vl.values = values;
267     vl.values_len = 1;
268     sstrncpy (vl.host, hostname_g, sizeof (vl.host));
269     sstrncpy (vl.plugin, "ip6tables", sizeof (vl.plugin));
270
271     status = ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
272             "%s-%s", chain->table, chain->chain);
273     if ((status < 1) || ((unsigned int)status >= sizeof (vl.plugin_instance)))
274         return (0);
275
276     if (chain->name[0] != '\0')
277     {
278         sstrncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
279     }
280     else
281     {
282         if (chain->rule_type == RTYPE_NUM)
283             ssnprintf (vl.type_instance, sizeof (vl.type_instance),
284                     "%i", chain->rule.num);
285         else
286             sstrncpy (vl.type_instance, (char *) match->data,
287                     sizeof (vl.type_instance));
288     }
289
290     sstrncpy (vl.type, "ipt_bytes", sizeof (vl.type));
291     values[0].derive = (derive_t) entry->counters.bcnt;
292     plugin_dispatch_values (&vl);
293
294     sstrncpy (vl.type, "ipt_packets", sizeof (vl.type));
295     values[0].derive = (derive_t) entry->counters.pcnt;
296     plugin_dispatch_values (&vl);
297
298     return (0);
299 } /* int submit_match */
300
301
302 /* This needs to return `int' for IPT_MATCH_ITERATE to work. */
303 static int submit_match (const struct ipt_entry_match *match,
304                 const struct ipt_entry *entry,
305                 const ip_chain_t *chain,
306                 int rule_num) 
307 {
308     int status;
309     value_t values[1];
310     value_list_t vl = VALUE_LIST_INIT;
311
312     /* Select the rules to collect */
313     if (chain->rule_type == RTYPE_NUM)
314     {
315         if (chain->rule.num != rule_num)
316             return (0);
317     }
318     else
319     {
320         if (strcmp (match->u.user.name, "comment") != 0)
321             return (0);
322         if ((chain->rule_type == RTYPE_COMMENT)
323                 && (strcmp (chain->rule.comment, (char *) match->data) != 0))
324             return (0);
325     }
326
327     vl.values = values;
328     vl.values_len = 1;
329     sstrncpy (vl.host, hostname_g, sizeof (vl.host));
330     sstrncpy (vl.plugin, "iptables", sizeof (vl.plugin));
331
332     status = ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
333             "%s-%s", chain->table, chain->chain);
334     if ((status < 1) || ((unsigned int)status >= sizeof (vl.plugin_instance)))
335         return (0);
336
337     if (chain->name[0] != '\0')
338     {
339         sstrncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
340     }
341     else
342     {
343         if (chain->rule_type == RTYPE_NUM)
344             ssnprintf (vl.type_instance, sizeof (vl.type_instance),
345                     "%i", chain->rule.num);
346         else
347             sstrncpy (vl.type_instance, (char *) match->data,
348                     sizeof (vl.type_instance));
349     }
350
351     sstrncpy (vl.type, "ipt_bytes", sizeof (vl.type));
352     values[0].derive = (derive_t) entry->counters.bcnt;
353     plugin_dispatch_values (&vl);
354
355     sstrncpy (vl.type, "ipt_packets", sizeof (vl.type));
356     values[0].derive = (derive_t) entry->counters.pcnt;
357     plugin_dispatch_values (&vl);
358
359     return (0);
360 } /* int submit_match */
361
362
363 /* ipv6 submit_chain */
364 static void submit6_chain( ip6tc_handle_t *handle, ip_chain_t *chain )
365 {
366     const struct ip6t_entry *entry;
367     int rule_num;
368
369     /* Find first rule for chain and use the iterate macro */
370     entry = ip6tc_first_rule( chain->chain, handle );
371     if (entry == NULL)
372     {
373         DEBUG ("ip6tc_first_rule failed: %s", ip6tc_strerror (errno));
374         return;
375     }
376
377     rule_num = 1;
378     while (entry)
379     {
380         if (chain->rule_type == RTYPE_NUM)
381         {
382             submit6_match (NULL, entry, chain, rule_num);
383         }
384         else
385         {
386             IP6T_MATCH_ITERATE( entry, submit6_match, entry, chain, rule_num );
387         }
388
389         entry = ip6tc_next_rule( entry, handle );
390         rule_num++;
391     } /* while (entry) */
392 }
393
394
395 /* ipv4 submit_chain */
396 static void submit_chain( iptc_handle_t *handle, ip_chain_t *chain )
397 {
398     const struct ipt_entry *entry;
399     int rule_num;
400
401     /* Find first rule for chain and use the iterate macro */    
402     entry = iptc_first_rule( chain->chain, handle );
403     if (entry == NULL)
404     {
405         DEBUG ("iptc_first_rule failed: %s", iptc_strerror (errno));
406         return;
407     }
408
409     rule_num = 1;
410     while (entry)
411     {
412         if (chain->rule_type == RTYPE_NUM)
413         {
414             submit_match (NULL, entry, chain, rule_num);
415         }
416         else
417         {
418             IPT_MATCH_ITERATE( entry, submit_match, entry, chain, rule_num );
419         }
420
421         entry = iptc_next_rule( entry, handle );
422         rule_num++;
423     } /* while (entry) */
424 }
425
426
427 static int iptables_read (void)
428 {
429     int i;
430     int num_failures = 0;
431     ip_chain_t *chain;
432
433     /* Init the iptc handle structure and query the correct table */    
434     for (i = 0; i < chain_num; i++)
435     {
436         chain = chain_list[i];
437         
438         if (!chain)
439         {
440             DEBUG ("iptables plugin: chain == NULL");
441             continue;
442         }
443
444         if ( chain->ip_version == IPV4 )
445         {
446 #ifdef HAVE_IPTC_HANDLE_T
447                 iptc_handle_t _handle;
448                 iptc_handle_t *handle = &_handle;
449
450                 *handle = iptc_init (chain->table);
451 #else
452                 iptc_handle_t *handle;
453                 handle = iptc_init (chain->table);
454 #endif
455
456                 if (!handle)
457                 {
458                         ERROR ("iptables plugin: iptc_init (%s) failed: %s",
459                                 chain->table, iptc_strerror (errno));
460                         num_failures++;
461                         continue;
462                 }
463
464                 submit_chain (handle, chain);
465                 iptc_free (handle);
466         }
467         else if ( chain->ip_version == IPV6 )
468         {
469 #ifdef HAVE_IP6TC_HANDLE_T
470                 ip6tc_handle_t _handle;
471                 ip6tc_handle_t *handle = &_handle;
472
473                 *handle = ip6tc_init (chain->table);
474 #else
475                 ip6tc_handle_t *handle;
476                 handle = ip6tc_init (chain->table);
477 #endif
478
479                 if (!handle)
480                 {
481                         ERROR ("iptables plugin: ip6tc_init (%s) failed: %s",
482                                 chain->table, ip6tc_strerror (errno));
483                         num_failures++;
484                         continue;
485                 }
486
487                 submit6_chain (handle, chain);
488                 ip6tc_free (handle);
489         }
490         else num_failures++;
491
492     } /* for (i = 0 .. chain_num) */
493
494     return ((num_failures < chain_num) ? 0 : -1);
495 } /* int iptables_read */
496
497 static int iptables_shutdown (void)
498 {
499     int i;
500
501     for (i = 0; i < chain_num; i++)
502     {
503         if ((chain_list[i] != NULL) && (chain_list[i]->rule_type == RTYPE_COMMENT))
504         {
505             sfree (chain_list[i]->rule.comment);
506         }
507         sfree (chain_list[i]);
508     }
509     sfree (chain_list);
510
511     return (0);
512 } /* int iptables_shutdown */
513
514 void module_register (void)
515 {
516     plugin_register_config ("iptables", iptables_config,
517             config_keys, config_keys_num);
518     plugin_register_read ("iptables", iptables_read);
519     plugin_register_shutdown ("iptables", iptables_shutdown);
520 } /* void module_register */
521
522 /*
523  * vim:shiftwidth=4:softtabstop=4:tabstop=8
524  */