bc15559317c3f533c9bca9dcf5e9e306e87f5747
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 HAVE_LIBIPTC_LIBIPTC_H
29 # include <libiptc/libiptc.h>
30 #endif
32 #if HAVE_LIBIPTC_LIBIPTC_H
33 # define IPTABLES_HAVE_READ 1
34 #else
35 # define IPTABLES_HAVE_READ 0
36 #endif
38 #define MODULE_NAME "iptables"
39 #define BUFSIZE 512
41 /*
42 * (Module-)Global variables
43 */
45 /*
46 * Removed packet count for now, should have config option if you want to save
47 * them Although other collectd models don't seem to care much for options
48 * eitherway for what to log
49 */
50 /* Limit to ~125MByte/s (~1GBit/s) */
51 static data_source_t dsrc[1] =
52 {
53 {"value", DS_TYPE_COUNTER, 0.0, 134217728.0}
54 };
56 static data_set_t ipt_bytes_ds =
57 {
58 "ipt_bytes", 1, dsrc
59 };
61 static data_set_t ipt_packets_ds =
62 {
63 "ipt_packets", 1, dsrc
64 };
66 #if IPTABLES_HAVE_READ
67 /*
68 * Config format should be `Chain table chainname',
69 * e. g. `Chain mangle incoming'
70 */
71 static const char *config_keys[] =
72 {
73 "Chain",
74 NULL
75 };
76 static int config_keys_num = 1;
77 /*
78 Each table/chain combo that will be queried goes into this list
79 */
80 #ifndef XT_TABLE_MAXNAMELEN
81 # define XT_TABLE_MAXNAMELEN 32
82 #endif
83 typedef struct {
84 char table[XT_TABLE_MAXNAMELEN];
85 char chain[XT_TABLE_MAXNAMELEN];
86 union
87 {
88 int num;
89 char *comment;
90 } rule;
91 enum
92 {
93 RTYPE_NUM,
94 RTYPE_COMMENT,
95 RTYPE_COMMENT_ALL
96 } rule_type;
97 char name[64];
98 } ip_chain_t;
100 static ip_chain_t **chain_list = NULL;
101 static int chain_num = 0;
103 static int iptables_config (const char *key, const char *value)
104 {
105 if (strcasecmp (key, "Chain") == 0)
106 {
107 ip_chain_t temp, *final, **list;
108 char *table;
109 int table_len;
110 char *chain;
111 int chain_len;
113 char *value_copy;
114 char *fields[4];
115 int fields_num;
117 memset (&temp, 0, sizeof (temp));
119 value_copy = strdup (value);
120 if (value_copy == NULL)
121 {
122 ERROR ("strdup failed: %s", strerror (errno));
123 return (1);
124 }
126 /* Chain <table> <chain> [<comment|num> [name]] */
127 fields_num = strsplit (value_copy, fields, 4);
128 if (fields_num < 2)
129 {
130 free (value_copy);
131 return (1);
132 }
134 table = fields[0];
135 chain = fields[1];
137 table_len = strlen (table);
138 if (table_len >= sizeof(temp.table))
139 {
140 ERROR ("Table `%s' too long.", table);
141 free (value_copy);
142 return (1);
143 }
144 strncpy (temp.table, table, table_len);
145 temp.table[table_len] = '\0';
147 chain_len = strlen (chain);
148 if (chain_len >= sizeof(temp.chain))
149 {
150 ERROR ("Chain `%s' too long.", chain);
151 free (value_copy);
152 return (1);
153 }
154 strncpy (temp.chain, chain, chain_len);
155 temp.chain[chain_len] = '\0';
157 if (fields_num >= 3)
158 {
159 char *comment = fields[2];
160 int rule = atoi (comment);
162 if (rule)
163 {
164 temp.rule.num = rule;
165 temp.rule_type = RTYPE_NUM;
166 }
167 else
168 {
169 strncpy (temp.rule.comment, comment,
170 sizeof (temp.rule.comment) - 1);
171 temp.rule_type = RTYPE_COMMENT;
172 }
173 }
174 else
175 {
176 temp.rule_type = RTYPE_COMMENT_ALL;
177 }
179 if (fields_num >= 4)
180 strncpy (temp.name, fields[3], sizeof (temp.name) - 1);
182 free (value_copy);
183 value_copy = NULL;
184 table = NULL;
185 chain = NULL;
187 list = (ip_chain_t **) realloc (chain_list, (chain_num + 1) * sizeof (ip_chain_t *));
188 if (list == NULL)
189 {
190 ERROR ("realloc failed: %s", strerror (errno));
191 return (1);
192 }
194 chain_list = list;
195 final = (ip_chain_t *) malloc( sizeof(temp) );
196 if (final == NULL)
197 {
198 ERROR ("malloc failed: %s", strerror (errno));
199 return (1);
200 }
201 memcpy (final, &temp, sizeof (temp));
202 chain_list[chain_num] = final;
203 chain_num++;
205 DEBUG ("Chain #%i: table = %s; chain = %s;", chain_num, final->table, final->chain);
206 }
207 else
208 {
209 return (-1);
210 }
212 return (0);
213 } /* int iptables_config */
214 #endif /* IPTABLES_HAVE_READ */
216 #if IPTABLES_HAVE_READ
217 /* This needs to return `int' for IPT_MATCH_ITERATE to work. */
218 static int submit_match (const struct ipt_entry_match *match,
219 const struct ipt_entry *entry,
220 const ip_chain_t *chain,
221 int rule_num)
222 {
223 int status;
224 value_t values[1];
225 value_list_t vl = VALUE_LIST_INIT;
227 /* Select the rules to collect */
228 if (chain->rule_type == RTYPE_NUM)
229 {
230 if (chain->rule.num != rule_num)
231 return (0);
232 }
233 else
234 {
235 if (strcmp (match->u.user.name, "comment") != 0)
236 return (0);
237 if ((chain->rule_type == RTYPE_COMMENT)
238 && (strcmp (chain->rule.comment, (char *) match->data) != 0))
239 return (0);
240 }
242 vl.values = values;
243 vl.values_len = 1;
244 vl.time = time (NULL);
245 strcpy (vl.host, hostname_g);
246 strcpy (vl.plugin, "iptables");
248 status = snprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
249 "%s-%s", chain->table, chain->chain);
250 if ((status >= sizeof (vl.plugin_instance)) || (status < 1))
251 return (0);
253 if (chain->name[0] != '\0')
254 {
255 strncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
256 }
257 else
258 {
259 if (chain->rule_type == RTYPE_NUM)
260 snprintf (vl.type_instance, sizeof (vl.type_instance),
261 "%i", chain->rule.num);
262 else
263 strncpy (vl.type_instance, (char *) match->data,
264 sizeof (vl.type_instance));
265 }
266 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
268 values[0].counter = (counter_t) entry->counters.bcnt;
269 plugin_dispatch_values ("ipt_bytes", &vl);
271 values[0].counter = (counter_t) entry->counters.pcnt;
272 plugin_dispatch_values ("ipt_packets", &vl);
274 return (0);
275 } /* void submit_match */
277 static void submit_chain( iptc_handle_t *handle, ip_chain_t *chain ) {
278 const struct ipt_entry *entry;
279 int rule_num;
281 /* Find first rule for chain and use the iterate macro */
282 entry = iptc_first_rule( chain->chain, handle );
283 if (entry == NULL)
284 {
285 DEBUG ("iptc_first_rule failed: %s", iptc_strerror (errno));
286 return;
287 }
289 rule_num = 1;
290 while (entry)
291 {
292 if (chain->rule_type == RTYPE_NUM)
293 {
294 submit_match (NULL, entry, chain, rule_num);
295 }
296 else
297 {
298 IPT_MATCH_ITERATE( entry, submit_match, entry, chain, rule_num );
299 }
301 entry = iptc_next_rule( entry, handle );
302 rule_num++;
303 } /* while (entry) */
304 }
307 static int iptables_read (void)
308 {
309 int i;
310 static complain_t complaint;
312 /* Init the iptc handle structure and query the correct table */
313 for (i = 0; i < chain_num; i++)
314 {
315 iptc_handle_t handle;
316 ip_chain_t *chain;
318 chain = chain_list[i];
319 if (!chain)
320 {
321 DEBUG ("chain == NULL");
322 continue;
323 }
325 handle = iptc_init( chain->table );
326 if (!handle)
327 {
328 DEBUG ("iptc_init (%s) failed: %s", chain->table, iptc_strerror (errno));
329 plugin_complain (LOG_ERR, &complaint, "iptc_init (%s) failed: %s",
330 chain->table, iptc_strerror (errno));
331 continue;
332 }
333 plugin_relief (LOG_INFO, &complaint, "iptc_init (%s) succeeded",
334 chain->table);
336 submit_chain (&handle, chain);
337 iptc_free (&handle);
338 }
340 return (0);
341 } /* int iptables_read */
343 static int iptables_shutdown (void)
344 {
345 int i;
347 for (i = 0; i < chain_num; i++)
348 sfree (chain_list[i]);
349 sfree (chain_list);
351 return (0);
352 } /* int iptables_shutdown */
353 #endif /* IPTABLES_HAVE_READ */
355 void module_register (void)
356 {
357 plugin_register_data_set (&ipt_bytes_ds);
358 plugin_register_data_set (&ipt_packets_ds);
360 #if IPTABLES_HAVE_READ
361 plugin_register_config ("iptables", iptables_config,
362 config_keys, config_keys_num);
363 plugin_register_read ("iptables", iptables_read);
364 plugin_register_shutdown ("iptables", iptables_shutdown);
365 #endif
366 }
368 #undef BUFSIZE
369 #undef MODULE_NAME
371 /*
372 * vim:shiftwidth=4:softtabstop=4:tabstop=8
373 */