a81dfd7e709dc9f8c707a01514ff8d3f5e35b8a4
1 /**
2 * collectd - src/iptables.c
3 * Copyright (C) 2007 Sjoerd van der Berg
4 * Copyright (C) 2007 Florian octo Forster
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * Authors:
21 * Sjoerd van der Berg <harekiet at users.sourceforge.net>
22 * Florian Forster <octo at verplant.org>
23 **/
25 #include "collectd.h"
26 #include "common.h"
27 #include "plugin.h"
28 #include "configfile.h"
30 #if OWN_LIBIPTC
31 # include "owniptc/libiptc.h"
32 #else
33 # include <libiptc/libiptc.h>
34 #endif
36 /*
37 * iptc_handle_t was available before libiptc was officially available as a
38 * shared library. Note, that when the shared lib was introduced, the API and
39 * ABI have changed slightly:
40 * 'iptc_handle_t' used to be 'struct iptc_handle *' and most functions used
41 * 'iptc_handle_t *' as an argument. Now, most functions use 'struct
42 * iptc_handle *' (thus removing one level of pointer indirection).
43 *
44 * HAVE_IPTC_HANDLE_T is used to determine which API ought to be used. While
45 * this is somewhat hacky, I didn't find better way to solve that :-/
46 * -tokkee
47 */
48 #ifndef HAVE_IPTC_HANDLE_T
49 typedef struct iptc_handle iptc_handle_t;
50 #endif
52 /*
53 * (Module-)Global variables
54 */
56 /*
57 * Config format should be `Chain table chainname',
58 * e. g. `Chain mangle incoming'
59 */
60 static const char *config_keys[] =
61 {
62 "Chain",
63 NULL
64 };
65 static int config_keys_num = 1;
66 /*
67 Each table/chain combo that will be queried goes into this list
68 */
69 #ifndef XT_TABLE_MAXNAMELEN
70 # define XT_TABLE_MAXNAMELEN 32
71 #endif
72 typedef struct {
73 char table[XT_TABLE_MAXNAMELEN];
74 char chain[XT_TABLE_MAXNAMELEN];
75 union
76 {
77 int num;
78 char *comment;
79 } rule;
80 enum
81 {
82 RTYPE_NUM,
83 RTYPE_COMMENT,
84 RTYPE_COMMENT_ALL
85 } rule_type;
86 char name[64];
87 } ip_chain_t;
89 static ip_chain_t **chain_list = NULL;
90 static int chain_num = 0;
92 static int iptables_config (const char *key, const char *value)
93 {
94 if (strcasecmp (key, "Chain") == 0)
95 {
96 ip_chain_t temp, *final, **list;
97 char *table;
98 int table_len;
99 char *chain;
100 int chain_len;
102 char *value_copy;
103 char *fields[4];
104 int fields_num;
106 memset (&temp, 0, sizeof (temp));
108 value_copy = strdup (value);
109 if (value_copy == NULL)
110 {
111 char errbuf[1024];
112 ERROR ("strdup failed: %s",
113 sstrerror (errno, errbuf, sizeof (errbuf)));
114 return (1);
115 }
117 /* Chain <table> <chain> [<comment|num> [name]] */
118 fields_num = strsplit (value_copy, fields, 4);
119 if (fields_num < 2)
120 {
121 free (value_copy);
122 return (1);
123 }
125 table = fields[0];
126 chain = fields[1];
128 table_len = strlen (table) + 1;
129 if ((unsigned int)table_len > sizeof(temp.table))
130 {
131 ERROR ("Table `%s' too long.", table);
132 free (value_copy);
133 return (1);
134 }
135 sstrncpy (temp.table, table, table_len);
137 chain_len = strlen (chain) + 1;
138 if ((unsigned int)chain_len > sizeof(temp.chain))
139 {
140 ERROR ("Chain `%s' too long.", chain);
141 free (value_copy);
142 return (1);
143 }
144 sstrncpy (temp.chain, chain, chain_len);
146 if (fields_num >= 3)
147 {
148 char *comment = fields[2];
149 int rule = atoi (comment);
151 if (rule)
152 {
153 temp.rule.num = rule;
154 temp.rule_type = RTYPE_NUM;
155 }
156 else
157 {
158 temp.rule.comment = strdup (comment);
159 if (temp.rule.comment == NULL)
160 {
161 free (value_copy);
162 return (1);
163 }
164 temp.rule_type = RTYPE_COMMENT;
165 }
166 }
167 else
168 {
169 temp.rule_type = RTYPE_COMMENT_ALL;
170 }
172 if (fields_num >= 4)
173 sstrncpy (temp.name, fields[3], sizeof (temp.name));
175 free (value_copy);
176 value_copy = NULL;
177 table = NULL;
178 chain = NULL;
180 list = (ip_chain_t **) realloc (chain_list, (chain_num + 1) * sizeof (ip_chain_t *));
181 if (list == NULL)
182 {
183 char errbuf[1024];
184 ERROR ("realloc failed: %s",
185 sstrerror (errno, errbuf, sizeof (errbuf)));
186 return (1);
187 }
189 chain_list = list;
190 final = (ip_chain_t *) malloc( sizeof(temp) );
191 if (final == NULL)
192 {
193 char errbuf[1024];
194 ERROR ("malloc failed: %s",
195 sstrerror (errno, errbuf, sizeof (errbuf)));
196 return (1);
197 }
198 memcpy (final, &temp, sizeof (temp));
199 chain_list[chain_num] = final;
200 chain_num++;
202 DEBUG ("Chain #%i: table = %s; chain = %s;", chain_num, final->table, final->chain);
203 }
204 else
205 {
206 return (-1);
207 }
209 return (0);
210 } /* int iptables_config */
212 /* This needs to return `int' for IPT_MATCH_ITERATE to work. */
213 static int submit_match (const struct ipt_entry_match *match,
214 const struct ipt_entry *entry,
215 const ip_chain_t *chain,
216 int rule_num)
217 {
218 int status;
219 value_t values[1];
220 value_list_t vl = VALUE_LIST_INIT;
222 /* Select the rules to collect */
223 if (chain->rule_type == RTYPE_NUM)
224 {
225 if (chain->rule.num != rule_num)
226 return (0);
227 }
228 else
229 {
230 if (strcmp (match->u.user.name, "comment") != 0)
231 return (0);
232 if ((chain->rule_type == RTYPE_COMMENT)
233 && (strcmp (chain->rule.comment, (char *) match->data) != 0))
234 return (0);
235 }
237 vl.values = values;
238 vl.values_len = 1;
239 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
240 sstrncpy (vl.plugin, "iptables", sizeof (vl.plugin));
242 status = ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
243 "%s-%s", chain->table, chain->chain);
244 if ((status < 1) || ((unsigned int)status >= sizeof (vl.plugin_instance)))
245 return (0);
247 if (chain->name[0] != '\0')
248 {
249 sstrncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
250 }
251 else
252 {
253 if (chain->rule_type == RTYPE_NUM)
254 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
255 "%i", chain->rule.num);
256 else
257 sstrncpy (vl.type_instance, (char *) match->data,
258 sizeof (vl.type_instance));
259 }
261 sstrncpy (vl.type, "ipt_bytes", sizeof (vl.type));
262 values[0].counter = (counter_t) entry->counters.bcnt;
263 plugin_dispatch_values (&vl);
265 sstrncpy (vl.type, "ipt_packets", sizeof (vl.type));
266 values[0].counter = (counter_t) entry->counters.pcnt;
267 plugin_dispatch_values (&vl);
269 return (0);
270 } /* void submit_match */
272 static void submit_chain( iptc_handle_t *handle, ip_chain_t *chain ) {
273 const struct ipt_entry *entry;
274 int rule_num;
276 /* Find first rule for chain and use the iterate macro */
277 entry = iptc_first_rule( chain->chain, handle );
278 if (entry == NULL)
279 {
280 DEBUG ("iptc_first_rule failed: %s", iptc_strerror (errno));
281 return;
282 }
284 rule_num = 1;
285 while (entry)
286 {
287 if (chain->rule_type == RTYPE_NUM)
288 {
289 submit_match (NULL, entry, chain, rule_num);
290 }
291 else
292 {
293 IPT_MATCH_ITERATE( entry, submit_match, entry, chain, rule_num );
294 }
296 entry = iptc_next_rule( entry, handle );
297 rule_num++;
298 } /* while (entry) */
299 }
302 static int iptables_read (void)
303 {
304 int i;
305 int num_failures = 0;
307 /* Init the iptc handle structure and query the correct table */
308 for (i = 0; i < chain_num; i++)
309 {
310 #ifdef HAVE_IPTC_HANDLE_T
311 iptc_handle_t _handle;
312 iptc_handle_t *handle = &_handle;
313 #else
314 iptc_handle_t *handle;
315 #endif
316 ip_chain_t *chain;
318 chain = chain_list[i];
319 if (!chain)
320 {
321 DEBUG ("iptables plugin: chain == NULL");
322 continue;
323 }
325 #ifdef HAVE_IPTC_HANDLE_T
326 *handle = iptc_init (chain->table);
327 #else
328 handle = iptc_init (chain->table);
329 #endif
330 if (!handle)
331 {
332 ERROR ("iptables plugin: iptc_init (%s) failed: %s",
333 chain->table, iptc_strerror (errno));
334 num_failures++;
335 continue;
336 }
338 submit_chain (handle, chain);
339 iptc_free (handle);
340 } /* for (i = 0 .. chain_num) */
342 return ((num_failures < chain_num) ? 0 : -1);
343 } /* int iptables_read */
345 static int iptables_shutdown (void)
346 {
347 int i;
349 for (i = 0; i < chain_num; i++)
350 {
351 if ((chain_list[i] != NULL) && (chain_list[i]->rule_type == RTYPE_COMMENT))
352 {
353 sfree (chain_list[i]->rule.comment);
354 }
355 sfree (chain_list[i]);
356 }
357 sfree (chain_list);
359 return (0);
360 } /* int iptables_shutdown */
362 void module_register (void)
363 {
364 plugin_register_config ("iptables", iptables_config,
365 config_keys, config_keys_num);
366 plugin_register_read ("iptables", iptables_read);
367 plugin_register_shutdown ("iptables", iptables_shutdown);
368 } /* void module_register */
370 /*
371 * vim:shiftwidth=4:softtabstop=4:tabstop=8
372 */