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