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