Merge branch 'pull/collectd-4' into collectd-4
[collectd.git] / src / iptables.c
1 /**
2  * collectd - src/iptables.c
3  * Copyright (C) 2007 Sjoerd van der Berg
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  *
19  * Authors:
20  *  Sjoerd van der Berg <harekiet at users.sourceforge.net>
21  **/
22
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26 #include "configfile.h"
27
28 #if HAVE_LIBIPTC_LIBIPTC_H
29 # include <libiptc/libiptc.h>
30 #endif
31
32 #if HAVE_LIBIPTC_LIBIPTC_H
33 # define IPTABLES_HAVE_READ 1
34 #else
35 # define IPTABLES_HAVE_READ 0
36 #endif
37
38 #define MODULE_NAME "iptables"
39 #define BUFSIZE 512
40
41 /*
42  * (Module-)Global variables
43  */
44
45 /*
46  * Removed packet count for now, should have config option if you want to save
47  * them Although other collectd models don't seem to care much for options
48  * eitherway for what to log
49  */
50 /* Limit to ~125MByte/s (~1GBit/s) */
51 static data_source_t dsrc[1] =
52 {
53         {"value",  DS_TYPE_COUNTER, 0.0, 134217728.0}
54 };
55
56 static data_set_t ipt_bytes_ds =
57 {
58         "ipt_bytes", 1, dsrc
59 };
60
61 static data_set_t ipt_packets_ds =
62 {
63         "ipt_packets", 1, dsrc
64 };
65
66 #if IPTABLES_HAVE_READ
67 /*
68  * Config format should be `Chain table chainname',
69  * e. g. `Chain mangle incoming'
70  */
71 static const char *config_keys[] =
72 {
73         "Chain",
74         NULL
75 };
76 static int config_keys_num = 1;
77 /*
78     Each table/chain combo that will be queried goes into this list
79 */
80 #ifndef XT_TABLE_MAXNAMELEN
81 # define XT_TABLE_MAXNAMELEN 32
82 #endif
83 typedef struct {
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         if (strcasecmp (key, "Chain") == 0)
106         {
107                 ip_chain_t temp, *final, **list;
108                 char *table;
109                 int   table_len;
110                 char *chain;
111                 int   chain_len;
112
113                 char *value_copy;
114                 char *fields[4];
115                 int   fields_num;
116                 
117                 memset (&temp, 0, sizeof (temp));
118
119                 value_copy = strdup (value);
120                 if (value_copy == NULL)
121                 {
122                     char errbuf[1024];
123                     ERROR ("strdup failed: %s",
124                             sstrerror (errno, errbuf, sizeof (errbuf)));
125                     return (1);
126                 }
127
128                 /* Chain <table> <chain> [<comment|num> [name]] */
129                 fields_num = strsplit (value_copy, fields, 4);
130                 if (fields_num < 2)
131                 {
132                     free (value_copy);
133                     return (1);
134                 }
135
136                 table = fields[0];
137                 chain = fields[1];
138
139                 table_len = strlen (table);
140                 if (table_len >= sizeof(temp.table))
141                 {
142                         ERROR ("Table `%s' too long.", table);
143                         free (value_copy);
144                         return (1);
145                 }
146                 strncpy (temp.table, table, table_len);
147                 temp.table[table_len] = '\0';
148
149                 chain_len = strlen (chain);
150                 if (chain_len >= sizeof(temp.chain))
151                 {
152                         ERROR ("Chain `%s' too long.", chain);
153                         free (value_copy);
154                         return (1);
155                 }
156                 strncpy (temp.chain, chain, chain_len);
157                 temp.chain[chain_len] = '\0'; 
158
159                 if (fields_num >= 3)
160                 {
161                     char *comment = fields[2];
162                     int   rule = atoi (comment);
163
164                     if (rule)
165                     {
166                         temp.rule.num = rule;
167                         temp.rule_type = RTYPE_NUM;
168                     }
169                     else
170                     {
171                         strncpy (temp.rule.comment, comment,
172                                 sizeof (temp.rule.comment) - 1);
173                         temp.rule_type = RTYPE_COMMENT;
174                     }
175                 }
176                 else
177                 {
178                     temp.rule_type = RTYPE_COMMENT_ALL;
179                 }
180
181                 if (fields_num >= 4)
182                     strncpy (temp.name, fields[3], sizeof (temp.name) - 1);
183
184                 free (value_copy);
185                 value_copy = NULL;
186                 table = NULL;
187                 chain = NULL;
188
189                 list = (ip_chain_t **) realloc (chain_list, (chain_num + 1) * sizeof (ip_chain_t *));
190                 if (list == NULL)
191                 {
192                     char errbuf[1024];
193                     ERROR ("realloc failed: %s",
194                             sstrerror (errno, errbuf, sizeof (errbuf)));
195                     return (1);
196                 }
197
198                 chain_list = list;
199                 final = (ip_chain_t *) malloc( sizeof(temp) );
200                 if (final == NULL) 
201                 {
202                     char errbuf[1024];
203                     ERROR ("malloc failed: %s",
204                             sstrerror (errno, errbuf, sizeof (errbuf)));
205                     return (1);
206                 }
207                 memcpy (final, &temp, sizeof (temp));
208                 chain_list[chain_num] = final;
209                 chain_num++;
210
211                 DEBUG ("Chain #%i: table = %s; chain = %s;", chain_num, final->table, final->chain);
212         }
213         else 
214         {
215                 return (-1);
216         }
217
218         return (0);
219 } /* int iptables_config */
220 #endif /* IPTABLES_HAVE_READ */
221
222 #if IPTABLES_HAVE_READ
223 /* This needs to return `int' for IPT_MATCH_ITERATE to work. */
224 static int submit_match (const struct ipt_entry_match *match,
225                 const struct ipt_entry *entry,
226                 const ip_chain_t *chain,
227                 int rule_num) 
228 {
229     int status;
230     value_t values[1];
231     value_list_t vl = VALUE_LIST_INIT;
232
233     /* Select the rules to collect */
234     if (chain->rule_type == RTYPE_NUM)
235     {
236         if (chain->rule.num != rule_num)
237             return (0);
238     }
239     else
240     {
241         if (strcmp (match->u.user.name, "comment") != 0)
242             return (0);
243         if ((chain->rule_type == RTYPE_COMMENT)
244                 && (strcmp (chain->rule.comment, (char *) match->data) != 0))
245             return (0);
246     }
247
248     vl.values = values;
249     vl.values_len = 1;
250     vl.time = time (NULL);
251     strcpy (vl.host, hostname_g);
252     strcpy (vl.plugin, "iptables");
253
254     status = snprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
255             "%s-%s", chain->table, chain->chain);
256     if ((status >= sizeof (vl.plugin_instance)) || (status < 1))
257         return (0);
258
259     if (chain->name[0] != '\0')
260     {
261         strncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
262     }
263     else
264     {
265         if (chain->rule_type == RTYPE_NUM)
266             snprintf (vl.type_instance, sizeof (vl.type_instance),
267                     "%i", chain->rule.num);
268         else
269             strncpy (vl.type_instance, (char *) match->data,
270                     sizeof (vl.type_instance));
271     }
272     vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
273
274     values[0].counter = (counter_t) entry->counters.bcnt;
275     plugin_dispatch_values ("ipt_bytes", &vl);
276
277     values[0].counter = (counter_t) entry->counters.pcnt;
278     plugin_dispatch_values ("ipt_packets", &vl);
279
280     return (0);
281 } /* void submit_match */
282
283 static void submit_chain( iptc_handle_t *handle, ip_chain_t *chain ) {
284     const struct ipt_entry *entry;
285     int rule_num;
286
287     /* Find first rule for chain and use the iterate macro */    
288     entry = iptc_first_rule( chain->chain, handle );
289     if (entry == NULL)
290     {
291         DEBUG ("iptc_first_rule failed: %s", iptc_strerror (errno));
292         return;
293     }
294
295     rule_num = 1;
296     while (entry)
297     {
298         if (chain->rule_type == RTYPE_NUM)
299         {
300             submit_match (NULL, entry, chain, rule_num);
301         }
302         else
303         {
304             IPT_MATCH_ITERATE( entry, submit_match, entry, chain, rule_num );
305         }
306
307         entry = iptc_next_rule( entry, handle );
308         rule_num++;
309     } /* while (entry) */
310 }
311
312
313 static int iptables_read (void)
314 {
315     int i;
316     static complain_t complaint;
317
318     /* Init the iptc handle structure and query the correct table */    
319     for (i = 0; i < chain_num; i++)
320     {
321         iptc_handle_t handle;
322         ip_chain_t *chain;
323         
324         chain = chain_list[i];
325         if (!chain)
326         {
327             DEBUG ("chain == NULL");
328             continue;
329         }
330
331         handle = iptc_init( chain->table );
332         if (!handle)
333         {
334             DEBUG ("iptc_init (%s) failed: %s", chain->table, iptc_strerror (errno));
335             plugin_complain (LOG_ERR, &complaint, "iptc_init (%s) failed: %s",
336                     chain->table, iptc_strerror (errno));
337             continue;
338         }
339         plugin_relief (LOG_INFO, &complaint, "iptc_init (%s) succeeded",
340                 chain->table);
341
342         submit_chain (&handle, chain);
343         iptc_free (&handle);
344     }
345
346     return (0);
347 } /* int iptables_read */
348
349 static int iptables_shutdown (void)
350 {
351     int i;
352
353     for (i = 0; i < chain_num; i++)
354         sfree (chain_list[i]);
355     sfree (chain_list);
356
357     return (0);
358 } /* int iptables_shutdown */
359 #endif /* IPTABLES_HAVE_READ */
360
361 void module_register (modreg_e load)
362 {
363     if (load & MR_DATASETS)
364     {
365         plugin_register_data_set (&ipt_bytes_ds);
366         plugin_register_data_set (&ipt_packets_ds);
367     }
368
369 #if IPTABLES_HAVE_READ
370     if (load & MR_READ)
371     {
372         plugin_register_config ("iptables", iptables_config,
373                 config_keys, config_keys_num);
374         plugin_register_read ("iptables", iptables_read);
375         plugin_register_shutdown ("iptables", iptables_shutdown);
376     }
377 #endif
378 } /* void module_register */
379
380 #undef BUFSIZE
381 #undef MODULE_NAME
382
383 /*
384  * vim:shiftwidth=4:softtabstop=4:tabstop=8
385  */