c1f42d73aec4d842ea548f64472a7cc7144e73d9
[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
29 #include "common.h"
30 #include "plugin.h"
31
32 #include <libiptc/libip6tc.h>
33 #include <libiptc/libiptc.h>
34
35 #ifdef HAVE_SYS_CAPABILITY_H
36 #include <sys/capability.h>
37 #endif
38
39 /*
40  * iptc_handle_t was available before libiptc was officially available as a
41  * shared library. Note, that when the shared lib was introduced, the API and
42  * ABI have changed slightly:
43  * 'iptc_handle_t' used to be 'struct iptc_handle *' and most functions used
44  * 'iptc_handle_t *' as an argument. Now, most functions use 'struct
45  * iptc_handle *' (thus removing one level of pointer indirection).
46  *
47  * HAVE_IPTC_HANDLE_T is used to determine which API ought to be used. While
48  * this is somewhat hacky, I didn't find better way to solve that :-/
49  * -tokkee
50  */
51 #ifndef HAVE_IPTC_HANDLE_T
52 typedef struct iptc_handle iptc_handle_t;
53 #endif
54 #ifndef HAVE_IP6TC_HANDLE_T
55 typedef struct ip6tc_handle ip6tc_handle_t;
56 #endif
57
58 /*
59  * (Module-)Global variables
60  */
61
62 /*
63  * Config format should be `Chain table chainname',
64  * e. g. `Chain mangle incoming'
65  */
66 static const char *config_keys[] = {"Chain", "Chain6"};
67 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
68 enum protocol_version_e { IPV4, IPV6 };
69 typedef enum protocol_version_e protocol_version_t;
70
71 /*
72  * Each table/chain combo that will be queried goes into this list
73  */
74 #ifndef XT_TABLE_MAXNAMELEN
75 #define XT_TABLE_MAXNAMELEN 32
76 #endif
77 typedef struct {
78   protocol_version_t ip_version;
79   char table[XT_TABLE_MAXNAMELEN];
80   char chain[XT_TABLE_MAXNAMELEN];
81   union {
82     int num;
83     char *comment;
84   } rule;
85   enum { RTYPE_NUM, RTYPE_COMMENT, RTYPE_COMMENT_ALL } rule_type;
86   char name[64];
87 } ip_chain_t;
88
89 static ip_chain_t **chain_list;
90 static int chain_num = 0;
91
92 static int iptables_config(const char *key, const char *value) {
93   /* int ip_value; */
94   protocol_version_t ip_version = 0;
95
96   if (strcasecmp(key, "Chain") == 0)
97     ip_version = IPV4;
98   else if (strcasecmp(key, "Chain6") == 0)
99     ip_version = IPV6;
100   else
101     return 1;
102
103   ip_chain_t temp = {0};
104   ip_chain_t *final, **list;
105   char *table;
106   int table_len;
107   char *chain;
108   int chain_len;
109
110   char *value_copy;
111   char *fields[4];
112   int fields_num;
113
114   value_copy = strdup(value);
115   if (value_copy == NULL) {
116     ERROR("strdup failed: %s", STRERRNO);
117     return 1;
118   }
119
120   /*
121    *  Time to fill the temp element
122    *  Examine value string, it should look like:
123    *  Chain[6] <table> <chain> [<comment|num> [name]]
124    */
125
126   /* set IPv4 or IPv6 */
127   temp.ip_version = ip_version;
128
129   /* Chain <table> <chain> [<comment|num> [name]] */
130   fields_num = strsplit(value_copy, fields, 4);
131   if (fields_num < 2) {
132     free(value_copy);
133     return 1;
134   }
135
136   table = fields[0];
137   chain = fields[1];
138
139   table_len = strlen(table) + 1;
140   if ((unsigned int)table_len > sizeof(temp.table)) {
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     ERROR("Chain `%s' too long.", chain);
150     free(value_copy);
151     return 1;
152   }
153   sstrncpy(temp.chain, chain, chain_len);
154
155   if (fields_num >= 3) {
156     char *comment = fields[2];
157     int rule = atoi(comment);
158
159     if (rule) {
160       temp.rule.num = rule;
161       temp.rule_type = RTYPE_NUM;
162     } else {
163       temp.rule.comment = strdup(comment);
164       if (temp.rule.comment == NULL) {
165         free(value_copy);
166         return 1;
167       }
168       temp.rule_type = RTYPE_COMMENT;
169     }
170   } else {
171     temp.rule_type = RTYPE_COMMENT_ALL;
172   }
173
174   if (fields_num >= 4)
175     sstrncpy(temp.name, fields[3], sizeof(temp.name));
176
177   free(value_copy);
178   value_copy = NULL;
179   table = NULL;
180   chain = NULL;
181
182   list = realloc(chain_list, (chain_num + 1) * sizeof(ip_chain_t *));
183   if (list == NULL) {
184     ERROR("realloc failed: %s", STRERRNO);
185     sfree(temp.rule.comment);
186     return 1;
187   }
188
189   chain_list = list;
190   final = malloc(sizeof(*final));
191   if (final == NULL) {
192     ERROR("malloc failed: %s", STRERRNO);
193     sfree(temp.rule.comment);
194     return 1;
195   }
196   memcpy(final, &temp, sizeof(temp));
197   chain_list[chain_num] = final;
198   chain_num++;
199
200   DEBUG("Chain #%i: table = %s; chain = %s;", chain_num, final->table,
201         final->chain);
202
203   return 0;
204 } /* int iptables_config */
205
206 static int submit6_match(const struct ip6t_entry_match *match,
207                          const struct ip6t_entry *entry,
208                          const ip_chain_t *chain, int rule_num) {
209   int status;
210   value_list_t vl = VALUE_LIST_INIT;
211
212   /* Select the rules to collect */
213   if (chain->rule_type == RTYPE_NUM) {
214     if (chain->rule.num != rule_num)
215       return 0;
216   } else {
217     if (strcmp(match->u.user.name, "comment") != 0)
218       return 0;
219     if ((chain->rule_type == RTYPE_COMMENT) &&
220         (strcmp(chain->rule.comment, (char *)match->data) != 0))
221       return 0;
222   }
223
224   sstrncpy(vl.plugin, "ip6tables", sizeof(vl.plugin));
225
226   status = snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s-%s",
227                     chain->table, chain->chain);
228   if ((status < 1) || ((unsigned int)status >= sizeof(vl.plugin_instance)))
229     return 0;
230
231   if (chain->name[0] != '\0') {
232     sstrncpy(vl.type_instance, chain->name, sizeof(vl.type_instance));
233   } else {
234     if (chain->rule_type == RTYPE_NUM)
235       snprintf(vl.type_instance, sizeof(vl.type_instance), "%i",
236                chain->rule.num);
237     else
238       sstrncpy(vl.type_instance, (char *)match->data, sizeof(vl.type_instance));
239   }
240
241   sstrncpy(vl.type, "ipt_bytes", sizeof(vl.type));
242   vl.values = &(value_t){.derive = (derive_t)entry->counters.bcnt};
243   vl.values_len = 1;
244   plugin_dispatch_values(&vl);
245
246   sstrncpy(vl.type, "ipt_packets", sizeof(vl.type));
247   vl.values = &(value_t){.derive = (derive_t)entry->counters.pcnt};
248   plugin_dispatch_values(&vl);
249
250   return 0;
251 } /* int submit6_match */
252
253 /* This needs to return `int' for IPT_MATCH_ITERATE to work. */
254 static int submit_match(const struct ipt_entry_match *match,
255                         const struct ipt_entry *entry, const ip_chain_t *chain,
256                         int rule_num) {
257   int status;
258   value_list_t vl = VALUE_LIST_INIT;
259
260   /* Select the rules to collect */
261   if (chain->rule_type == RTYPE_NUM) {
262     if (chain->rule.num != rule_num)
263       return 0;
264   } else {
265     if (strcmp(match->u.user.name, "comment") != 0)
266       return 0;
267     if ((chain->rule_type == RTYPE_COMMENT) &&
268         (strcmp(chain->rule.comment, (char *)match->data) != 0))
269       return 0;
270   }
271
272   sstrncpy(vl.plugin, "iptables", sizeof(vl.plugin));
273
274   status = snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s-%s",
275                     chain->table, chain->chain);
276   if ((status < 1) || ((unsigned int)status >= sizeof(vl.plugin_instance)))
277     return 0;
278
279   if (chain->name[0] != '\0') {
280     sstrncpy(vl.type_instance, chain->name, sizeof(vl.type_instance));
281   } else {
282     if (chain->rule_type == RTYPE_NUM)
283       snprintf(vl.type_instance, sizeof(vl.type_instance), "%i",
284                chain->rule.num);
285     else
286       sstrncpy(vl.type_instance, (char *)match->data, sizeof(vl.type_instance));
287   }
288
289   sstrncpy(vl.type, "ipt_bytes", sizeof(vl.type));
290   vl.values = &(value_t){.derive = (derive_t)entry->counters.bcnt};
291   vl.values_len = 1;
292   plugin_dispatch_values(&vl);
293
294   sstrncpy(vl.type, "ipt_packets", sizeof(vl.type));
295   vl.values = &(value_t){.derive = (derive_t)entry->counters.pcnt};
296   plugin_dispatch_values(&vl);
297
298   return 0;
299 } /* int submit_match */
300
301 /* ipv6 submit_chain */
302 static void submit6_chain(ip6tc_handle_t *handle, ip_chain_t *chain) {
303   const struct ip6t_entry *entry;
304   int rule_num;
305
306   /* Find first rule for chain and use the iterate macro */
307   entry = ip6tc_first_rule(chain->chain, handle);
308   if (entry == NULL) {
309     DEBUG("ip6tc_first_rule failed: %s", ip6tc_strerror(errno));
310     return;
311   }
312
313   rule_num = 1;
314   while (entry) {
315     if (chain->rule_type == RTYPE_NUM) {
316       submit6_match(NULL, entry, chain, rule_num);
317     } else {
318       IP6T_MATCH_ITERATE(entry, submit6_match, entry, chain, rule_num);
319     }
320
321     entry = ip6tc_next_rule(entry, handle);
322     rule_num++;
323   } /* while (entry) */
324 }
325
326 /* ipv4 submit_chain */
327 static void submit_chain(iptc_handle_t *handle, ip_chain_t *chain) {
328   const struct ipt_entry *entry;
329   int rule_num;
330
331   /* Find first rule for chain and use the iterate macro */
332   entry = iptc_first_rule(chain->chain, handle);
333   if (entry == NULL) {
334     DEBUG("iptc_first_rule failed: %s", iptc_strerror(errno));
335     return;
336   }
337
338   rule_num = 1;
339   while (entry) {
340     if (chain->rule_type == RTYPE_NUM) {
341       submit_match(NULL, entry, chain, rule_num);
342     } else {
343       IPT_MATCH_ITERATE(entry, submit_match, entry, chain, rule_num);
344     }
345
346     entry = iptc_next_rule(entry, handle);
347     rule_num++;
348   } /* while (entry) */
349 }
350
351 static int iptables_read(void) {
352   int num_failures = 0;
353   ip_chain_t *chain;
354
355   /* Init the iptc handle structure and query the correct table */
356   for (int i = 0; i < chain_num; i++) {
357     chain = chain_list[i];
358
359     if (!chain) {
360       DEBUG("iptables plugin: chain == NULL");
361       continue;
362     }
363
364     if (chain->ip_version == IPV4) {
365 #ifdef HAVE_IPTC_HANDLE_T
366       iptc_handle_t _handle;
367       iptc_handle_t *handle = &_handle;
368
369       *handle = iptc_init(chain->table);
370 #else
371       iptc_handle_t *handle;
372       handle = iptc_init(chain->table);
373 #endif
374
375       if (!handle) {
376         ERROR("iptables plugin: iptc_init (%s) failed: %s", chain->table,
377               iptc_strerror(errno));
378         num_failures++;
379         continue;
380       }
381
382       submit_chain(handle, chain);
383       iptc_free(handle);
384     } else if (chain->ip_version == IPV6) {
385 #ifdef HAVE_IP6TC_HANDLE_T
386       ip6tc_handle_t _handle;
387       ip6tc_handle_t *handle = &_handle;
388
389       *handle = ip6tc_init(chain->table);
390 #else
391       ip6tc_handle_t *handle;
392       handle = ip6tc_init(chain->table);
393 #endif
394       if (!handle) {
395         ERROR("iptables plugin: ip6tc_init (%s) failed: %s", chain->table,
396               ip6tc_strerror(errno));
397         num_failures++;
398         continue;
399       }
400
401       submit6_chain(handle, chain);
402       ip6tc_free(handle);
403     } else
404       num_failures++;
405   } /* for (i = 0 .. chain_num) */
406
407   return (num_failures < chain_num) ? 0 : -1;
408 } /* int iptables_read */
409
410 static int iptables_shutdown(void) {
411   for (int i = 0; i < chain_num; i++) {
412     if ((chain_list[i] != NULL) && (chain_list[i]->rule_type == RTYPE_COMMENT))
413       sfree(chain_list[i]->rule.comment);
414     sfree(chain_list[i]);
415   }
416   sfree(chain_list);
417
418   return 0;
419 } /* int iptables_shutdown */
420
421 static int iptables_init(void) {
422 #if defined(HAVE_SYS_CAPABILITY_H) && defined(CAP_NET_ADMIN)
423   if (check_capability(CAP_NET_ADMIN) != 0) {
424     if (getuid() == 0)
425       WARNING("iptables plugin: Running collectd as root, but the "
426               "CAP_NET_ADMIN capability is missing. The plugin's read "
427               "function will probably fail. Is your init system dropping "
428               "capabilities?");
429     else
430       WARNING("iptables plugin: collectd doesn't have the CAP_NET_ADMIN "
431               "capability. If you don't want to run collectd as root, try "
432               "running \"setcap cap_net_admin=ep\" on the collectd binary.");
433   }
434 #endif
435   return 0;
436 } /* int iptables_init */
437
438 void module_register(void) {
439   plugin_register_config("iptables", iptables_config, config_keys,
440                          config_keys_num);
441   plugin_register_init("iptables", iptables_init);
442   plugin_register_read("iptables", iptables_read);
443   plugin_register_shutdown("iptables", iptables_shutdown);
444 } /* void module_register */