1 /**
2 * collectd - src/iptables.c
3 * Copyright (C) 2007 Sjoerd van der Berg
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Sjoerd van der Berg <harekiet at users.sourceforge.net>
21 **/
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26 #include "configfile.h"
28 #if OWN_LIBIPTC
29 # include "libiptc/libiptc.h"
30 #else
31 # include <libiptc/libiptc.h>
32 #endif
34 /*
35 * (Module-)Global variables
36 */
38 /*
39 * Config format should be `Chain table chainname',
40 * e. g. `Chain mangle incoming'
41 */
42 static const char *config_keys[] =
43 {
44 "Chain",
45 NULL
46 };
47 static int config_keys_num = 1;
48 /*
49 Each table/chain combo that will be queried goes into this list
50 */
51 #ifndef XT_TABLE_MAXNAMELEN
52 # define XT_TABLE_MAXNAMELEN 32
53 #endif
54 typedef struct {
55 char table[XT_TABLE_MAXNAMELEN];
56 char chain[XT_TABLE_MAXNAMELEN];
57 union
58 {
59 int num;
60 char *comment;
61 } rule;
62 enum
63 {
64 RTYPE_NUM,
65 RTYPE_COMMENT,
66 RTYPE_COMMENT_ALL
67 } rule_type;
68 char name[64];
69 } ip_chain_t;
71 static ip_chain_t **chain_list = NULL;
72 static int chain_num = 0;
74 static int iptables_config (const char *key, const char *value)
75 {
76 if (strcasecmp (key, "Chain") == 0)
77 {
78 ip_chain_t temp, *final, **list;
79 char *table;
80 int table_len;
81 char *chain;
82 int chain_len;
84 char *value_copy;
85 char *fields[4];
86 int fields_num;
88 memset (&temp, 0, sizeof (temp));
90 value_copy = strdup (value);
91 if (value_copy == NULL)
92 {
93 char errbuf[1024];
94 ERROR ("strdup failed: %s",
95 sstrerror (errno, errbuf, sizeof (errbuf)));
96 return (1);
97 }
99 /* Chain <table> <chain> [<comment|num> [name]] */
100 fields_num = strsplit (value_copy, fields, 4);
101 if (fields_num < 2)
102 {
103 free (value_copy);
104 return (1);
105 }
107 table = fields[0];
108 chain = fields[1];
110 table_len = strlen (table);
111 if ((unsigned int)table_len >= sizeof(temp.table))
112 {
113 ERROR ("Table `%s' too long.", table);
114 free (value_copy);
115 return (1);
116 }
117 strncpy (temp.table, table, table_len);
118 temp.table[table_len] = '\0';
120 chain_len = strlen (chain);
121 if ((unsigned int)chain_len >= sizeof(temp.chain))
122 {
123 ERROR ("Chain `%s' too long.", chain);
124 free (value_copy);
125 return (1);
126 }
127 strncpy (temp.chain, chain, chain_len);
128 temp.chain[chain_len] = '\0';
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 strncpy (temp.name, fields[3], sizeof (temp.name) - 1);
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 vl.time = time (NULL);
224 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
225 sstrncpy (vl.plugin, "iptables", sizeof (vl.plugin));
227 status = snprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
228 "%s-%s", chain->table, chain->chain);
229 if ((status < 1) || ((unsigned int)status >= sizeof (vl.plugin_instance)))
230 return (0);
232 if (chain->name[0] != '\0')
233 {
234 strncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
235 }
236 else
237 {
238 if (chain->rule_type == RTYPE_NUM)
239 snprintf (vl.type_instance, sizeof (vl.type_instance),
240 "%i", chain->rule.num);
241 else
242 strncpy (vl.type_instance, (char *) match->data,
243 sizeof (vl.type_instance));
244 }
245 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
247 values[0].counter = (counter_t) entry->counters.bcnt;
248 plugin_dispatch_values ("ipt_bytes", &vl);
250 values[0].counter = (counter_t) entry->counters.pcnt;
251 plugin_dispatch_values ("ipt_packets", &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 */