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 /*
33 * (Module-)Global variables
34 */
36 /*
37 * Config format should be `Chain table chainname',
38 * e. g. `Chain mangle incoming'
39 */
40 static const char *config_keys[] =
41 {
42 "Chain",
43 NULL
44 };
45 static int config_keys_num = 1;
46 /*
47 Each table/chain combo that will be queried goes into this list
48 */
49 #ifndef XT_TABLE_MAXNAMELEN
50 # define XT_TABLE_MAXNAMELEN 32
51 #endif
52 typedef struct {
53 char table[XT_TABLE_MAXNAMELEN];
54 char chain[XT_TABLE_MAXNAMELEN];
55 union
56 {
57 int num;
58 char *comment;
59 } rule;
60 enum
61 {
62 RTYPE_NUM,
63 RTYPE_COMMENT,
64 RTYPE_COMMENT_ALL
65 } rule_type;
66 char name[64];
67 } ip_chain_t;
69 static ip_chain_t **chain_list = NULL;
70 static int chain_num = 0;
72 static int iptables_config (const char *key, const char *value)
73 {
74 if (strcasecmp (key, "Chain") == 0)
75 {
76 ip_chain_t temp, *final, **list;
77 char *table;
78 int table_len;
79 char *chain;
80 int chain_len;
82 char *value_copy;
83 char *fields[4];
84 int fields_num;
86 memset (&temp, 0, sizeof (temp));
88 value_copy = strdup (value);
89 if (value_copy == NULL)
90 {
91 char errbuf[1024];
92 ERROR ("strdup failed: %s",
93 sstrerror (errno, errbuf, sizeof (errbuf)));
94 return (1);
95 }
97 /* Chain <table> <chain> [<comment|num> [name]] */
98 fields_num = strsplit (value_copy, fields, 4);
99 if (fields_num < 2)
100 {
101 free (value_copy);
102 return (1);
103 }
105 table = fields[0];
106 chain = fields[1];
108 table_len = strlen (table);
109 if (table_len >= sizeof(temp.table))
110 {
111 ERROR ("Table `%s' too long.", table);
112 free (value_copy);
113 return (1);
114 }
115 strncpy (temp.table, table, table_len);
116 temp.table[table_len] = '\0';
118 chain_len = strlen (chain);
119 if (chain_len >= sizeof(temp.chain))
120 {
121 ERROR ("Chain `%s' too long.", chain);
122 free (value_copy);
123 return (1);
124 }
125 strncpy (temp.chain, chain, chain_len);
126 temp.chain[chain_len] = '\0';
128 if (fields_num >= 3)
129 {
130 char *comment = fields[2];
131 int rule = atoi (comment);
133 if (rule)
134 {
135 temp.rule.num = rule;
136 temp.rule_type = RTYPE_NUM;
137 }
138 else
139 {
140 temp.rule.comment = strdup (comment);
141 if (temp.rule.comment == NULL)
142 {
143 free (value_copy);
144 return (1);
145 }
146 temp.rule_type = RTYPE_COMMENT;
147 }
148 }
149 else
150 {
151 temp.rule_type = RTYPE_COMMENT_ALL;
152 }
154 if (fields_num >= 4)
155 strncpy (temp.name, fields[3], sizeof (temp.name) - 1);
157 free (value_copy);
158 value_copy = NULL;
159 table = NULL;
160 chain = NULL;
162 list = (ip_chain_t **) realloc (chain_list, (chain_num + 1) * sizeof (ip_chain_t *));
163 if (list == NULL)
164 {
165 char errbuf[1024];
166 ERROR ("realloc failed: %s",
167 sstrerror (errno, errbuf, sizeof (errbuf)));
168 return (1);
169 }
171 chain_list = list;
172 final = (ip_chain_t *) malloc( sizeof(temp) );
173 if (final == NULL)
174 {
175 char errbuf[1024];
176 ERROR ("malloc failed: %s",
177 sstrerror (errno, errbuf, sizeof (errbuf)));
178 return (1);
179 }
180 memcpy (final, &temp, sizeof (temp));
181 chain_list[chain_num] = final;
182 chain_num++;
184 DEBUG ("Chain #%i: table = %s; chain = %s;", chain_num, final->table, final->chain);
185 }
186 else
187 {
188 return (-1);
189 }
191 return (0);
192 } /* int iptables_config */
194 /* This needs to return `int' for IPT_MATCH_ITERATE to work. */
195 static int submit_match (const struct ipt_entry_match *match,
196 const struct ipt_entry *entry,
197 const ip_chain_t *chain,
198 int rule_num)
199 {
200 int status;
201 value_t values[1];
202 value_list_t vl = VALUE_LIST_INIT;
204 /* Select the rules to collect */
205 if (chain->rule_type == RTYPE_NUM)
206 {
207 if (chain->rule.num != rule_num)
208 return (0);
209 }
210 else
211 {
212 if (strcmp (match->u.user.name, "comment") != 0)
213 return (0);
214 if ((chain->rule_type == RTYPE_COMMENT)
215 && (strcmp (chain->rule.comment, (char *) match->data) != 0))
216 return (0);
217 }
219 vl.values = values;
220 vl.values_len = 1;
221 vl.time = time (NULL);
222 strcpy (vl.host, hostname_g);
223 strcpy (vl.plugin, "iptables");
225 status = snprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
226 "%s-%s", chain->table, chain->chain);
227 if ((status >= sizeof (vl.plugin_instance)) || (status < 1))
228 return (0);
230 if (chain->name[0] != '\0')
231 {
232 strncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
233 }
234 else
235 {
236 if (chain->rule_type == RTYPE_NUM)
237 snprintf (vl.type_instance, sizeof (vl.type_instance),
238 "%i", chain->rule.num);
239 else
240 strncpy (vl.type_instance, (char *) match->data,
241 sizeof (vl.type_instance));
242 }
243 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
245 values[0].counter = (counter_t) entry->counters.bcnt;
246 plugin_dispatch_values ("ipt_bytes", &vl);
248 values[0].counter = (counter_t) entry->counters.pcnt;
249 plugin_dispatch_values ("ipt_packets", &vl);
251 return (0);
252 } /* void submit_match */
254 static void submit_chain( iptc_handle_t *handle, ip_chain_t *chain ) {
255 const struct ipt_entry *entry;
256 int rule_num;
258 /* Find first rule for chain and use the iterate macro */
259 entry = iptc_first_rule( chain->chain, handle );
260 if (entry == NULL)
261 {
262 DEBUG ("iptc_first_rule failed: %s", iptc_strerror (errno));
263 return;
264 }
266 rule_num = 1;
267 while (entry)
268 {
269 if (chain->rule_type == RTYPE_NUM)
270 {
271 submit_match (NULL, entry, chain, rule_num);
272 }
273 else
274 {
275 IPT_MATCH_ITERATE( entry, submit_match, entry, chain, rule_num );
276 }
278 entry = iptc_next_rule( entry, handle );
279 rule_num++;
280 } /* while (entry) */
281 }
284 static int iptables_read (void)
285 {
286 int i;
287 static complain_t complaint;
289 /* Init the iptc handle structure and query the correct table */
290 for (i = 0; i < chain_num; i++)
291 {
292 iptc_handle_t handle;
293 ip_chain_t *chain;
295 chain = chain_list[i];
296 if (!chain)
297 {
298 DEBUG ("chain == NULL");
299 continue;
300 }
302 handle = iptc_init( chain->table );
303 if (!handle)
304 {
305 DEBUG ("iptc_init (%s) failed: %s", chain->table, iptc_strerror (errno));
306 plugin_complain (LOG_ERR, &complaint, "iptc_init (%s) failed: %s",
307 chain->table, iptc_strerror (errno));
308 continue;
309 }
310 plugin_relief (LOG_INFO, &complaint, "iptc_init (%s) succeeded",
311 chain->table);
313 submit_chain (&handle, chain);
314 iptc_free (&handle);
315 }
317 return (0);
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 */