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 "libiptc/libiptc.h"
32 #else
33 # include <libiptc/libiptc.h>
34 #endif
36 /*
37 * (Module-)Global variables
38 */
40 /*
41 * Config format should be `Chain table chainname',
42 * e. g. `Chain mangle incoming'
43 */
44 static const char *config_keys[] =
45 {
46 "Chain",
47 NULL
48 };
49 static int config_keys_num = 1;
50 /*
51 Each table/chain combo that will be queried goes into this list
52 */
53 #ifndef XT_TABLE_MAXNAMELEN
54 # define XT_TABLE_MAXNAMELEN 32
55 #endif
56 typedef struct {
57 char table[XT_TABLE_MAXNAMELEN];
58 char chain[XT_TABLE_MAXNAMELEN];
59 union
60 {
61 int num;
62 char *comment;
63 } rule;
64 enum
65 {
66 RTYPE_NUM,
67 RTYPE_COMMENT,
68 RTYPE_COMMENT_ALL
69 } rule_type;
70 char name[64];
71 } ip_chain_t;
73 static ip_chain_t **chain_list = NULL;
74 static int chain_num = 0;
76 static int iptables_config (const char *key, const char *value)
77 {
78 if (strcasecmp (key, "Chain") == 0)
79 {
80 ip_chain_t temp, *final, **list;
81 char *table;
82 int table_len;
83 char *chain;
84 int chain_len;
86 char *value_copy;
87 char *fields[4];
88 int fields_num;
90 memset (&temp, 0, sizeof (temp));
92 value_copy = strdup (value);
93 if (value_copy == NULL)
94 {
95 char errbuf[1024];
96 ERROR ("strdup failed: %s",
97 sstrerror (errno, errbuf, sizeof (errbuf)));
98 return (1);
99 }
101 /* Chain <table> <chain> [<comment|num> [name]] */
102 fields_num = strsplit (value_copy, fields, 4);
103 if (fields_num < 2)
104 {
105 free (value_copy);
106 return (1);
107 }
109 table = fields[0];
110 chain = fields[1];
112 table_len = strlen (table) + 1;
113 if ((unsigned int)table_len > sizeof(temp.table))
114 {
115 ERROR ("Table `%s' too long.", table);
116 free (value_copy);
117 return (1);
118 }
119 sstrncpy (temp.table, table, table_len);
121 chain_len = strlen (chain) + 1;
122 if ((unsigned int)chain_len > sizeof(temp.chain))
123 {
124 ERROR ("Chain `%s' too long.", chain);
125 free (value_copy);
126 return (1);
127 }
128 sstrncpy (temp.chain, chain, chain_len);
130 if (fields_num >= 3)
131 {
132 char *comment = fields[2];
133 int rule = atoi (comment);
135 if (rule)
136 {
137 temp.rule.num = rule;
138 temp.rule_type = RTYPE_NUM;
139 }
140 else
141 {
142 temp.rule.comment = strdup (comment);
143 if (temp.rule.comment == NULL)
144 {
145 free (value_copy);
146 return (1);
147 }
148 temp.rule_type = RTYPE_COMMENT;
149 }
150 }
151 else
152 {
153 temp.rule_type = RTYPE_COMMENT_ALL;
154 }
156 if (fields_num >= 4)
157 sstrncpy (temp.name, fields[3], sizeof (temp.name));
159 free (value_copy);
160 value_copy = NULL;
161 table = NULL;
162 chain = NULL;
164 list = (ip_chain_t **) realloc (chain_list, (chain_num + 1) * sizeof (ip_chain_t *));
165 if (list == NULL)
166 {
167 char errbuf[1024];
168 ERROR ("realloc failed: %s",
169 sstrerror (errno, errbuf, sizeof (errbuf)));
170 return (1);
171 }
173 chain_list = list;
174 final = (ip_chain_t *) malloc( sizeof(temp) );
175 if (final == NULL)
176 {
177 char errbuf[1024];
178 ERROR ("malloc failed: %s",
179 sstrerror (errno, errbuf, sizeof (errbuf)));
180 return (1);
181 }
182 memcpy (final, &temp, sizeof (temp));
183 chain_list[chain_num] = final;
184 chain_num++;
186 DEBUG ("Chain #%i: table = %s; chain = %s;", chain_num, final->table, final->chain);
187 }
188 else
189 {
190 return (-1);
191 }
193 return (0);
194 } /* int iptables_config */
196 /* This needs to return `int' for IPT_MATCH_ITERATE to work. */
197 static int submit_match (const struct ipt_entry_match *match,
198 const struct ipt_entry *entry,
199 const ip_chain_t *chain,
200 int rule_num)
201 {
202 int status;
203 value_t values[1];
204 value_list_t vl = VALUE_LIST_INIT;
206 /* Select the rules to collect */
207 if (chain->rule_type == RTYPE_NUM)
208 {
209 if (chain->rule.num != rule_num)
210 return (0);
211 }
212 else
213 {
214 if (strcmp (match->u.user.name, "comment") != 0)
215 return (0);
216 if ((chain->rule_type == RTYPE_COMMENT)
217 && (strcmp (chain->rule.comment, (char *) match->data) != 0))
218 return (0);
219 }
221 vl.values = values;
222 vl.values_len = 1;
223 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
224 sstrncpy (vl.plugin, "iptables", sizeof (vl.plugin));
226 status = ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
227 "%s-%s", chain->table, chain->chain);
228 if ((status < 1) || ((unsigned int)status >= sizeof (vl.plugin_instance)))
229 return (0);
231 if (chain->name[0] != '\0')
232 {
233 sstrncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
234 }
235 else
236 {
237 if (chain->rule_type == RTYPE_NUM)
238 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
239 "%i", chain->rule.num);
240 else
241 sstrncpy (vl.type_instance, (char *) match->data,
242 sizeof (vl.type_instance));
243 }
245 sstrncpy (vl.type, "ipt_bytes", sizeof (vl.type));
246 values[0].counter = (counter_t) entry->counters.bcnt;
247 plugin_dispatch_values (&vl);
249 sstrncpy (vl.type, "ipt_packets", sizeof (vl.type));
250 values[0].counter = (counter_t) entry->counters.pcnt;
251 plugin_dispatch_values (&vl);
253 return (0);
254 } /* void submit_match */
256 static void submit_chain( iptc_handle_t *handle, ip_chain_t *chain ) {
257 const struct ipt_entry *entry;
258 int rule_num;
260 /* Find first rule for chain and use the iterate macro */
261 entry = iptc_first_rule( chain->chain, handle );
262 if (entry == NULL)
263 {
264 DEBUG ("iptc_first_rule failed: %s", iptc_strerror (errno));
265 return;
266 }
268 rule_num = 1;
269 while (entry)
270 {
271 if (chain->rule_type == RTYPE_NUM)
272 {
273 submit_match (NULL, entry, chain, rule_num);
274 }
275 else
276 {
277 IPT_MATCH_ITERATE( entry, submit_match, entry, chain, rule_num );
278 }
280 entry = iptc_next_rule( entry, handle );
281 rule_num++;
282 } /* while (entry) */
283 }
286 static int iptables_read (void)
287 {
288 int i;
289 int num_failures = 0;
291 /* Init the iptc handle structure and query the correct table */
292 for (i = 0; i < chain_num; i++)
293 {
294 iptc_handle_t handle;
295 ip_chain_t *chain;
297 chain = chain_list[i];
298 if (!chain)
299 {
300 DEBUG ("iptables plugin: chain == NULL");
301 continue;
302 }
304 handle = iptc_init (chain->table);
305 if (!handle)
306 {
307 ERROR ("iptables plugin: iptc_init (%s) failed: %s",
308 chain->table, iptc_strerror (errno));
309 num_failures++;
310 continue;
311 }
313 submit_chain (&handle, chain);
314 iptc_free (&handle);
315 } /* for (i = 0 .. chain_num) */
317 return ((num_failures < chain_num) ? 0 : -1);
318 } /* int iptables_read */
320 static int iptables_shutdown (void)
321 {
322 int i;
324 for (i = 0; i < chain_num; i++)
325 {
326 if ((chain_list[i] != NULL) && (chain_list[i]->rule_type == RTYPE_COMMENT))
327 {
328 sfree (chain_list[i]->rule.comment);
329 }
330 sfree (chain_list[i]);
331 }
332 sfree (chain_list);
334 return (0);
335 } /* int iptables_shutdown */
337 void module_register (void)
338 {
339 plugin_register_config ("iptables", iptables_config,
340 config_keys, config_keys_num);
341 plugin_register_read ("iptables", iptables_read);
342 plugin_register_shutdown ("iptables", iptables_shutdown);
343 } /* void module_register */
345 /*
346 * vim:shiftwidth=4:softtabstop=4:tabstop=8
347 */