blob: 6760dbcf3e1b962b8e5134ceaa8d035a6eabb9a3 [file] [log] [blame]
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001/*
2 * libebtc.c, January 2004
3 *
4 * Contains the functions with which to make a table in userspace.
5 *
6 * Author: Bart De Schuymer
7 *
8 * This code is stongly inspired on the iptables code which is
9 * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of the
14 * License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
Bart De Schuymer80c82bb2004-01-14 20:06:44 +000026#include <string.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include <stdarg.h>
Bart De Schuymer80c82bb2004-01-14 20:06:44 +000030#include "include/ebtables_u.h"
31#include "include/ethernetdb.h"
32#include <unistd.h>
33#include <fcntl.h>
34#include <sys/wait.h>
35
36static void decrease_chain_jumps(struct ebt_u_replace *replace);
37static void remove_udc(struct ebt_u_replace *replace);
38static int iterate_entries(struct ebt_u_replace *replace, int type);
39
Bart De Schuymer6622a012005-01-19 21:09:05 +000040/* The standard names */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +000041const char *ebt_hooknames[NF_BR_NUMHOOKS] =
42{
43 [NF_BR_PRE_ROUTING]"PREROUTING",
44 [NF_BR_LOCAL_IN]"INPUT",
45 [NF_BR_FORWARD]"FORWARD",
46 [NF_BR_LOCAL_OUT]"OUTPUT",
47 [NF_BR_POST_ROUTING]"POSTROUTING",
48 [NF_BR_BROUTING]"BROUTING"
49};
50
Bart De Schuymer6622a012005-01-19 21:09:05 +000051/* The four target names */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +000052const char* ebt_standard_targets[NUM_STANDARD_TARGETS] =
53{
54 "ACCEPT",
55 "DROP",
56 "CONTINUE",
57 "RETURN",
58};
59
Bart De Schuymer6622a012005-01-19 21:09:05 +000060/* The lists of supported tables, matches, watchers and targets */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +000061struct ebt_u_table *ebt_tables;
62struct ebt_u_match *ebt_matches;
63struct ebt_u_watcher *ebt_watchers;
64struct ebt_u_target *ebt_targets;
65
Bart De Schuymer6622a012005-01-19 21:09:05 +000066/* Find the right structure belonging to a name */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +000067struct ebt_u_target *ebt_find_target(const char *name)
68{
69 struct ebt_u_target *t = ebt_targets;
70
Bart De Schuymer6622a012005-01-19 21:09:05 +000071 while (t && strcmp(t->name, name))
Bart De Schuymer80c82bb2004-01-14 20:06:44 +000072 t = t->next;
73 return t;
74}
75
76struct ebt_u_match *ebt_find_match(const char *name)
77{
78 struct ebt_u_match *m = ebt_matches;
79
Bart De Schuymer6622a012005-01-19 21:09:05 +000080 while (m && strcmp(m->name, name))
Bart De Schuymer80c82bb2004-01-14 20:06:44 +000081 m = m->next;
82 return m;
83}
84
85struct ebt_u_watcher *ebt_find_watcher(const char *name)
86{
87 struct ebt_u_watcher *w = ebt_watchers;
88
Bart De Schuymer6622a012005-01-19 21:09:05 +000089 while (w && strcmp(w->name, name))
Bart De Schuymer80c82bb2004-01-14 20:06:44 +000090 w = w->next;
91 return w;
92}
93
94struct ebt_u_table *ebt_find_table(const char *name)
95{
96 struct ebt_u_table *t = ebt_tables;
97
98 while (t && strcmp(t->name, name))
99 t = t->next;
100 return t;
101}
102
Bart De Schuymer6622a012005-01-19 21:09:05 +0000103/* Prints all registered extensions */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000104void ebt_list_extensions()
105{
106 struct ebt_u_table *tbl = ebt_tables;
107 struct ebt_u_target *t = ebt_targets;
108 struct ebt_u_match *m = ebt_matches;
109 struct ebt_u_watcher *w = ebt_watchers;
110
111 PRINT_VERSION;
112 printf("Supported userspace extensions:\n\nSupported tables:\n");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000113 while (tbl) {
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000114 printf("%s\n", tbl->name);
115 tbl = tbl->next;
116 }
117 printf("\nSupported targets:\n");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000118 while (t) {
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000119 printf("%s\n", t->name);
120 t = t->next;
121 }
122 printf("\nSupported matches:\n");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000123 while (m) {
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000124 printf("%s\n", m->name);
125 m = m->next;
126 }
127 printf("\nSupported watchers:\n");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000128 while (w) {
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000129 printf("%s\n", w->name);
130 w = w->next;
131 }
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000132}
133
Bart De Schuymer6622a012005-01-19 21:09:05 +0000134/* Get the table from the kernel or from a binary file
Bart De Schuymer64182a32004-01-21 20:39:54 +0000135 * init: 1 = ask the kernel for the initial contents of a table, i.e. the
136 * way it looks when the table is insmod'ed
Bart De Schuymer6622a012005-01-19 21:09:05 +0000137 * 0 = get the current data in the table */
138int ebt_get_kernel_table(struct ebt_u_replace *replace, int init)
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000139{
Bart De Schuymer6622a012005-01-19 21:09:05 +0000140 if (!ebt_find_table(replace->name)) {
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000141 ebt_print_error("Bad table name '%s'", replace->name);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000142 return -1;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000143 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000144 /* Get the kernel's information */
145 if (ebt_get_table(replace, init)) {
146 if (ebt_errormsg[0] != '\0')
147 return -1;
148 ebtables_insmod("ebtables");
149 if (ebt_get_table(replace, init)) {
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000150 ebt_print_error("The kernel doesn't support the ebtables '%s' table", replace->name);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000151 return -1;
152 }
153 }
154 return 0;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000155}
156
Bart De Schuymer6622a012005-01-19 21:09:05 +0000157/* Put sane values into a new entry */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000158void ebt_initialize_entry(struct ebt_u_entry *e)
159{
160 e->bitmask = EBT_NOPROTO;
161 e->invflags = 0;
162 e->ethproto = 0;
163 strcpy(e->in, "");
164 strcpy(e->out, "");
165 strcpy(e->logical_in, "");
166 strcpy(e->logical_out, "");
167 e->m_list = NULL;
168 e->w_list = NULL;
169 e->t = (struct ebt_entry_target *)ebt_find_target(EBT_STANDARD_TARGET);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000170 ebt_find_target(EBT_STANDARD_TARGET)->used = 1;
Bart De Schuymerab611e22005-02-14 20:20:03 +0000171 e->cnt.pcnt = e->cnt.bcnt = e->cnt_surplus.pcnt = e->cnt_surplus.bcnt = 0;
Bart De Schuymer64182a32004-01-21 20:39:54 +0000172
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000173 if (!e->t)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000174 ebt_print_bug("Couldn't load standard target");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000175 ((struct ebt_standard_target *)((struct ebt_u_target *)e->t)->t)->verdict = EBT_CONTINUE;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000176}
177
Bart De Schuymer6622a012005-01-19 21:09:05 +0000178/* Free up the memory of the table held in userspace, *replace can be reused */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000179void ebt_cleanup_replace(struct ebt_u_replace *replace)
180{
181 int i;
182 struct ebt_u_entries *entries;
183 struct ebt_u_chain_list *udc1, *udc2;
184 struct ebt_cntchanges *cc1, *cc2;
185 struct ebt_u_entry *u_e1, *u_e2;
186
187 replace->name[0] = '\0';
188 replace->valid_hooks = 0;
189 replace->nentries = 0;
190 replace->num_counters = 0;
191 replace->flags = 0;
192 replace->command = 0;
193 replace->selected_chain = -1;
194 if (replace->filename) {
195 free(replace->filename);
196 replace->filename = NULL;
197 }
198 if (replace->counters) {
199 free(replace->counters);
200 replace->counters = NULL;
201 }
202
203 i = -1;
204 while (1) {
205 i++;
206 entries = ebt_nr_to_chain(replace, i);
207 if (!entries) {
208 if (i < NF_BR_NUMHOOKS)
209 continue;
210 else
211 break;
212 }
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000213 u_e1 = entries->entries;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000214 while (u_e1) {
215 ebt_free_u_entry(u_e1);
216 u_e2 = u_e1->next;
217 free(u_e1);
218 u_e1 = u_e2;
219 }
220 }
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000221 for (i = 0; i < NF_BR_NUMHOOKS; i++)
222 if (replace->hook_entry[i]) {
223 free(replace->hook_entry[i]);
224 replace->hook_entry[i] = NULL;
225 }
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000226 udc1 = replace->udc;
227 while (udc1) {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000228 free(udc1->udc);
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000229 udc2 = udc1->next;
230 free(udc1);
231 udc1 = udc2;
232 }
233 replace->udc = NULL;
234 cc1 = replace->counterchanges;
235 while (cc1) {
236 cc2 = cc1->next;
237 free(cc2);
238 cc1 = cc2;
239 }
240 replace->counterchanges = NULL;
241}
242
Bart De Schuymer6622a012005-01-19 21:09:05 +0000243/* Should be called, e.g., between 2 rule adds */
244void ebt_reinit_extensions()
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000245{
246 struct ebt_u_match *m;
247 struct ebt_u_watcher *w;
248 struct ebt_u_target *t;
Bart De Schuymer64182a32004-01-21 20:39:54 +0000249 int size;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000250
251 /* The init functions should determine by themselves whether they are
252 * called for the first time or not (when necessary). */
253 for (m = ebt_matches; m; m = m->next) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000254 size = EBT_ALIGN(m->size) + sizeof(struct ebt_entry_match);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000255 if (m->used) {
256 m->m = (struct ebt_entry_match *)malloc(size);
257 if (!m->m)
258 ebt_print_memory();
259 strcpy(m->m->u.name, m->name);
260 m->m->match_size = EBT_ALIGN(m->size);
261 m->used = 0;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000262 }
Bart De Schuymerff587202005-02-08 20:02:28 +0000263 m->flags = 0; /* An error can occur before used is set, while flags is changed. */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000264 m->init(m->m);
265 }
266 for (w = ebt_watchers; w; w = w->next) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000267 size = EBT_ALIGN(w->size) + sizeof(struct ebt_entry_watcher);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000268 if (w->used) {
269 w->w = (struct ebt_entry_watcher *)malloc(size);
270 if (!w->w)
271 ebt_print_memory();
272 strcpy(w->w->u.name, w->name);
273 w->w->watcher_size = EBT_ALIGN(w->size);
274 w->used = 0;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000275 }
Bart De Schuymerff587202005-02-08 20:02:28 +0000276 w->flags = 0;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000277 w->init(w->w);
278 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000279 for (t = ebt_targets; t; t = t->next) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000280 size = EBT_ALIGN(t->size) + sizeof(struct ebt_entry_target);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000281 if (t->used) {
282 t->t = (struct ebt_entry_target *)malloc(size);
283 if (!t->t)
284 ebt_print_memory();
285 strcpy(t->t->u.name, t->name);
286 t->t->target_size = EBT_ALIGN(t->size);
287 t->used = 0;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000288 }
Bart De Schuymerff587202005-02-08 20:02:28 +0000289 t->flags = 0;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000290 t->init(t->t);
291 }
292}
293
Bart De Schuymer6622a012005-01-19 21:09:05 +0000294/* This doesn't free e, because the calling function might need e->next */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000295void ebt_free_u_entry(struct ebt_u_entry *e)
296{
297 struct ebt_u_match_list *m_l, *m_l2;
298 struct ebt_u_watcher_list *w_l, *w_l2;
299
300 m_l = e->m_list;
301 while (m_l) {
302 m_l2 = m_l->next;
303 free(m_l->m);
304 free(m_l);
305 m_l = m_l2;
306 }
307 w_l = e->w_list;
308 while (w_l) {
309 w_l2 = w_l->next;
310 free(w_l->w);
311 free(w_l);
312 w_l = w_l2;
313 }
314 free(e->t);
315}
316
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000317static char *get_modprobe(void)
318{
319 int procfile;
320 char *ret;
321
322 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
323 if (procfile < 0)
324 return NULL;
325
326 ret = malloc(1024);
327 if (ret) {
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000328 if (read(procfile, ret, 1024) == -1)
329 goto fail;
330 ret[1023] = '\0';
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000331 close(procfile);
332 return ret;
333 }
334 fail:
335 free(ret);
336 close(procfile);
337 return NULL;
338}
339
340char *ebt_modprobe;
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000341/* Try to load the kernel module, analogous to ip_tables.c */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000342int ebtables_insmod(const char *modname)
343{
344 char *buf = NULL;
345 char *argv[3];
346
347 /* If they don't explicitly set it, read out of kernel */
348 if (!ebt_modprobe) {
349 buf = get_modprobe();
350 if (!buf)
351 return -1;
352 ebt_modprobe = buf;
353 }
354
355 switch (fork()) {
356 case 0:
357 argv[0] = (char *)ebt_modprobe;
358 argv[1] = (char *)modname;
359 argv[2] = NULL;
360 execv(argv[0], argv);
361
Bart De Schuymer6622a012005-01-19 21:09:05 +0000362 /* Not usually reached */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000363 exit(0);
364 case -1:
365 return -1;
366
Bart De Schuymer6622a012005-01-19 21:09:05 +0000367 default: /* Parent */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000368 wait(NULL);
369 }
370
371 free(buf);
372 return 0;
373}
374
Bart De Schuymer6622a012005-01-19 21:09:05 +0000375/* Gives back a pointer to the chain base, based on nr.
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000376 * If nr >= NF_BR_NUMHOOKS you'll get back a user-defined chain.
Bart De Schuymer6622a012005-01-19 21:09:05 +0000377 * Returns NULL on failure. */
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000378struct ebt_u_entries *ebt_nr_to_chain(const struct ebt_u_replace *replace, int nr)
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000379{
380 if (nr == -1)
381 return NULL;
382 if (nr < NF_BR_NUMHOOKS)
383 return replace->hook_entry[nr];
384 else {
385 int i;
386 struct ebt_u_chain_list *cl = replace->udc;
387
388 i = nr - NF_BR_NUMHOOKS;
389 while (i > 0 && cl) {
390 cl = cl->next;
391 i--;
392 }
393 if (cl)
394 return cl->udc;
395 else
396 return NULL;
397 }
398}
399
Bart De Schuymer6622a012005-01-19 21:09:05 +0000400/* Gives back a pointer to the chain base of selected_chain */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000401struct ebt_u_entries *ebt_to_chain(const struct ebt_u_replace *replace)
402{
403 return ebt_nr_to_chain(replace, replace->selected_chain);
404}
405
Bart De Schuymer6622a012005-01-19 21:09:05 +0000406/* Parse the chain name and return a pointer to the chain base.
407 * Returns NULL on failure. */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000408struct ebt_u_entries *ebt_name_to_chain(const struct ebt_u_replace *replace,
Bart De Schuymer6622a012005-01-19 21:09:05 +0000409 const char* arg)
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000410{
411 int i;
412 struct ebt_u_chain_list *cl = replace->udc;
413
414 for (i = 0; i < NF_BR_NUMHOOKS; i++) {
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000415 if (!replace->hook_entry[i])
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000416 continue;
417 if (!strcmp(arg, replace->hook_entry[i]->name))
418 return replace->hook_entry[i];
419 }
420 while(cl) {
421 if (!strcmp(arg, cl->udc->name))
422 return cl->udc;
423 cl = cl->next;
424 }
425 return NULL;
426}
427
Bart De Schuymer6622a012005-01-19 21:09:05 +0000428/* Parse the chain name and return the corresponding chain nr
429 * returns -1 on failure */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000430int ebt_get_chainnr(const struct ebt_u_replace *replace, const char* arg)
431{
432 int i;
433 struct ebt_u_chain_list *cl = replace->udc;
434
435 for (i = 0; i < NF_BR_NUMHOOKS; i++) {
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000436 if (!replace->hook_entry[i])
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000437 continue;
438 if (!strcmp(arg, replace->hook_entry[i]->name))
439 return i;
440 }
441 while(cl) {
442 if (!strcmp(arg, cl->udc->name))
443 return i;
444 i++;
445 cl = cl->next;
446 }
447 return -1;
448}
449
450 /*
451************
452************
453**COMMANDS**
454************
455************
456 */
457
Bart De Schuymer6622a012005-01-19 21:09:05 +0000458/* Change the policy of selected_chain.
459 * Handing a bad policy to this function is a bug. */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000460void ebt_change_policy(struct ebt_u_replace *replace, int policy)
461{
462 struct ebt_u_entries *entries = ebt_to_chain(replace);
463
464 if (policy < -NUM_STANDARD_TARGETS || policy == EBT_CONTINUE)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000465 ebt_print_bug("Wrong policy: %d", policy);
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000466 entries->policy = policy;
467}
468
Bart De Schuymer6622a012005-01-19 21:09:05 +0000469/* Flush one chain or the complete table
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000470 * If selected_chain == -1 then flush the complete table */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000471void ebt_flush_chains(struct ebt_u_replace *replace)
472{
473 int i, j, numdel;
474 struct ebt_u_entry *u_e, *tmp;
475 struct ebt_u_entries *entries = ebt_to_chain(replace);
476 struct ebt_cntchanges *cc = replace->counterchanges;
477 struct ebt_cntchanges **prev_cc = &(replace->counterchanges);
478
Bart De Schuymer6622a012005-01-19 21:09:05 +0000479 /* Flush whole table */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000480 if (!entries) {
481 if (replace->nentries == 0)
482 return;
483 replace->nentries = 0;
484
Bart De Schuymer6622a012005-01-19 21:09:05 +0000485 /* Free everything and zero (n)entries */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000486 i = -1;
487 while (1) {
488 i++;
489 entries = ebt_nr_to_chain(replace, i);
490 if (!entries) {
491 if (i < NF_BR_NUMHOOKS)
492 continue;
493 else
494 break;
495 }
496 entries->nentries = 0;
497 entries->counter_offset = 0;
498 u_e = entries->entries;
499 entries->entries = NULL;
500 while (u_e) {
501 ebt_free_u_entry(u_e);
502 tmp = u_e->next;
503 free(u_e);
504 u_e = tmp;
505 }
506 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000507 /* Update the counters */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000508 while (cc) {
509 if (cc->type == CNT_ADD) {
510 *prev_cc = cc->next;
511 free(cc);
512 cc = *prev_cc;
513 continue;
514 }
515 cc->type = CNT_DEL;
516 prev_cc = &(cc->next);
517 cc = cc->next;
518 }
519 return;
520 }
521
522 if (entries->nentries == 0)
523 return;
524 replace->nentries -= entries->nentries;
525 numdel = entries->nentries;
526
Bart De Schuymer6622a012005-01-19 21:09:05 +0000527 /* Delete the counters belonging to the specified chain,
528 * update counter_offset */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000529 i = -1;
530 while (1) {
531 i++;
532 entries = ebt_nr_to_chain(replace, i);
533 if (!entries) {
534 if (i < NF_BR_NUMHOOKS)
535 continue;
536 else
537 break;
538 }
539 if (i > replace->selected_chain) {
540 entries->counter_offset -= numdel;
541 continue;
542 }
543 j = entries->nentries;
544 while (j) {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000545 /* Don't count deleted entries */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000546 if (cc->type == CNT_DEL)
547 goto letscontinue;
548 if (i == replace->selected_chain) {
549 if (cc->type == CNT_ADD) {
550 *prev_cc = cc->next;
551 free(cc);
552 cc = *prev_cc;
553 j--;
554 continue;
555 }
556 cc->type = CNT_DEL;
557 }
558 j--;
559letscontinue:
560 prev_cc = &(cc->next);
561 cc = cc->next;
562 }
563 }
564
565 entries = ebt_to_chain(replace);
566 entries->nentries = 0;
567 u_e = entries->entries;
568 while (u_e) {
569 ebt_free_u_entry(u_e);
570 tmp = u_e->next;
571 free(u_e);
572 u_e = tmp;
573 }
574 entries->entries = NULL;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000575}
576
Bart De Schuymerab611e22005-02-14 20:20:03 +0000577#define OPT_COUNT 0x1000 /* This value is also defined in ebtables.c */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000578/* Returns the rule number on success (starting from 0), -1 on failure
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000579 *
580 * This function expects the ebt_{match,watcher,target} members of new_entry
Bart De Schuymer6622a012005-01-19 21:09:05 +0000581 * to contain pointers to ebt_u_{match,watcher,target} */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000582int ebt_check_rule_exists(struct ebt_u_replace *replace,
583 struct ebt_u_entry *new_entry)
584{
585 struct ebt_u_entry *u_e;
586 struct ebt_u_match_list *m_l, *m_l2;
587 struct ebt_u_match *m;
588 struct ebt_u_watcher_list *w_l, *w_l2;
589 struct ebt_u_watcher *w;
590 struct ebt_u_target *t = (struct ebt_u_target *)new_entry->t;
591 struct ebt_u_entries *entries = ebt_to_chain(replace);
592 int i, j, k;
593
594 u_e = entries->entries;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000595 /* Check for an existing rule (if there are duplicate rules,
596 * take the first occurance) */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000597 for (i = 0; i < entries->nentries; i++, u_e = u_e->next) {
598 if (!u_e)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000599 ebt_print_bug("Hmm, trouble");
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000600 if (u_e->ethproto != new_entry->ethproto)
601 continue;
602 if (strcmp(u_e->in, new_entry->in))
603 continue;
604 if (strcmp(u_e->out, new_entry->out))
605 continue;
606 if (strcmp(u_e->logical_in, new_entry->logical_in))
607 continue;
608 if (strcmp(u_e->logical_out, new_entry->logical_out))
609 continue;
610 if (new_entry->bitmask & EBT_SOURCEMAC &&
Bart De Schuymer6622a012005-01-19 21:09:05 +0000611 memcmp(u_e->sourcemac, new_entry->sourcemac, ETH_ALEN))
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000612 continue;
613 if (new_entry->bitmask & EBT_DESTMAC &&
Bart De Schuymer6622a012005-01-19 21:09:05 +0000614 memcmp(u_e->destmac, new_entry->destmac, ETH_ALEN))
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000615 continue;
616 if (new_entry->bitmask != u_e->bitmask ||
Bart De Schuymer6622a012005-01-19 21:09:05 +0000617 new_entry->invflags != u_e->invflags)
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000618 continue;
Bart De Schuymerab611e22005-02-14 20:20:03 +0000619 if (replace->flags & OPT_COUNT && (new_entry->cnt.pcnt !=
620 u_e->cnt.pcnt || new_entry->cnt.bcnt != u_e->cnt.bcnt))
621 continue;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000622 /* Compare all matches */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000623 m_l = new_entry->m_list;
624 j = 0;
625 while (m_l) {
626 m = (struct ebt_u_match *)(m_l->m);
627 m_l2 = u_e->m_list;
628 while (m_l2 && strcmp(m_l2->m->u.name, m->m->u.name))
629 m_l2 = m_l2->next;
630 if (!m_l2 || !m->compare(m->m, m_l2->m))
631 goto letscontinue;
632 j++;
633 m_l = m_l->next;
634 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000635 /* Now be sure they have the same nr of matches */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000636 k = 0;
637 m_l = u_e->m_list;
638 while (m_l) {
639 k++;
640 m_l = m_l->next;
641 }
642 if (j != k)
643 continue;
644
Bart De Schuymer6622a012005-01-19 21:09:05 +0000645 /* Compare all watchers */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000646 w_l = new_entry->w_list;
647 j = 0;
648 while (w_l) {
649 w = (struct ebt_u_watcher *)(w_l->w);
650 w_l2 = u_e->w_list;
651 while (w_l2 && strcmp(w_l2->w->u.name, w->w->u.name))
652 w_l2 = w_l2->next;
653 if (!w_l2 || !w->compare(w->w, w_l2->w))
654 goto letscontinue;
655 j++;
656 w_l = w_l->next;
657 }
658 k = 0;
659 w_l = u_e->w_list;
660 while (w_l) {
661 k++;
662 w_l = w_l->next;
663 }
664 if (j != k)
665 continue;
666 if (strcmp(t->t->u.name, u_e->t->u.name))
667 continue;
668 if (!t->compare(t->t, u_e->t))
669 continue;
670 return i;
Bart De Schuymeraef08942004-09-09 21:41:29 +0000671letscontinue:;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000672 }
673 return -1;
674}
675
676/* Add a rule, rule_nr is the rule to update
677 * rule_nr specifies where the rule should be inserted
678 * rule_nr > 0 : insert the rule right before the rule_nr'th rule
679 * (the first rule is rule 1)
680 * rule_nr < 0 : insert the rule right before the (n+rule_nr+1)'th rule,
Bart De Schuymer6622a012005-01-19 21:09:05 +0000681 * where n denotes the number of rules in the chain
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000682 * rule_nr == 0: add a new rule at the end of the chain
683 *
684 * This function expects the ebt_{match,watcher,target} members of new_entry
685 * to contain pointers to ebt_u_{match,watcher,target} and updates these
Bart De Schuymer6622a012005-01-19 21:09:05 +0000686 * pointers so that they point to ebt_{match,watcher,target}, before adding
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000687 * the rule to the chain. Don't free() the ebt_{match,watcher,target} and
688 * don't reuse the new_entry after a successful call to ebt_add_rule() */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000689void ebt_add_rule(struct ebt_u_replace *replace, struct ebt_u_entry *new_entry,
690 int rule_nr)
691{
692 int i, j;
693 struct ebt_u_entry **u_e;
694 struct ebt_u_match_list *m_l;
695 struct ebt_u_watcher_list *w_l;
696 struct ebt_u_entries *entries = ebt_to_chain(replace);
697 struct ebt_cntchanges *cc = replace->counterchanges, *new_cc;
698 struct ebt_cntchanges **prev_cc = &(replace->counterchanges);
699
700 if (rule_nr <= 0)
701 rule_nr += entries->nentries;
702 else
703 rule_nr--;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000704 if (rule_nr > entries->nentries || rule_nr < 0) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000705 ebt_print_error("The specified rule number is incorrect");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000706 return;
707 }
708 /* We're adding one rule */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000709 replace->nentries++;
710 entries->nentries++;
711
Bart De Schuymer6622a012005-01-19 21:09:05 +0000712 /* Handle counter stuff */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000713 for (i = 0; i < replace->selected_chain; i++) {
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000714 if (i < NF_BR_NUMHOOKS && !(replace->hook_entry[i]))
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000715 continue;
716 j = ebt_nr_to_chain(replace, i)->nentries;
717 while (j) {
718 if (cc->type != CNT_DEL)
719 j--;
720 prev_cc = &(cc->next);
721 cc = cc->next;
722 }
723 }
724 j = rule_nr;
725 while (j) {
726 if (cc->type != CNT_DEL)
727 j--;
728 prev_cc = &(cc->next);
729 cc = cc->next;
730 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000731 if (cc && cc->type == CNT_DEL)
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000732 cc->type = CNT_OWRITE;
733 else {
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000734 new_cc = (struct ebt_cntchanges *)malloc(sizeof(struct ebt_cntchanges));
Bart De Schuymer6622a012005-01-19 21:09:05 +0000735 if (!new_cc)
736 ebt_print_memory();
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000737 new_cc->type = CNT_ADD;
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000738 new_cc->change = 0;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000739 new_cc->next = cc;
740 *prev_cc = new_cc;
741 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000742 /* Go to the right position in the chain */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000743 u_e = &entries->entries;
744 for (i = 0; i < rule_nr; i++)
745 u_e = &(*u_e)->next;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000746 /* Insert the rule */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000747 new_entry->next = *u_e;
748 *u_e = new_entry;
749
Bart De Schuymer6622a012005-01-19 21:09:05 +0000750 /* Put the ebt_{match, watcher, target} pointers in place */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000751 m_l = new_entry->m_list;
752 while (m_l) {
753 m_l->m = ((struct ebt_u_match *)m_l->m)->m;
754 m_l = m_l->next;
755 }
756 w_l = new_entry->w_list;
757 while (w_l) {
758 w_l->w = ((struct ebt_u_watcher *)w_l->w)->w;
759 w_l = w_l->next;
760 }
761 new_entry->t = ((struct ebt_u_target *)new_entry->t)->t;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000762 /* Update the counter_offset of chains behind this one */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000763 i = replace->selected_chain;
764 while (1) {
765 i++;
766 entries = ebt_nr_to_chain(replace, i);
767 if (!entries) {
768 if (i < NF_BR_NUMHOOKS)
769 continue;
770 else
771 break;
772 } else
773 entries->counter_offset++;
774 }
775}
776
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000777/* If *begin==*end==0 then find the rule corresponding to new_entry,
778 * else make the rule numbers positive and check for bad rule numbers. */
Bart De Schuymerff587202005-02-08 20:02:28 +0000779static int check_and_change_rule_number(struct ebt_u_replace *replace,
780 struct ebt_u_entry *new_entry, int *begin, int *end)
781{
782 struct ebt_u_entries *entries = ebt_to_chain(replace);
783
784 if (*begin < 0)
785 *begin += entries->nentries + 1;
786 if (*end < 0)
787 *end += entries->nentries + 1;
788
789 if (*begin < 0 || *begin > *end || *end > entries->nentries) {
790 ebt_print_error("Sorry, wrong rule numbers");
791 return -1;
792 }
793
794 if ((*begin * *end == 0) && (*begin + *end != 0))
795 ebt_print_bug("begin and end should be either both zero, "
796 "either both non-zero");
797 if (*begin != 0 && *end != 0) {
798 (*begin)--;
799 (*end)--;
800 } else {
801 *begin = ebt_check_rule_exists(replace, new_entry);
802 *end = *begin;
803 if (*begin == -1) {
804 ebt_print_error("Sorry, rule does not exist");
805 return -1;
806 }
807 }
808 return 0;
809}
810
Bart De Schuymer6622a012005-01-19 21:09:05 +0000811/* Delete a rule or rules
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000812 * begin == end == 0: delete the rule corresponding to new_entry
813 *
Bart De Schuymer6622a012005-01-19 21:09:05 +0000814 * The first rule has rule nr 1, the last rule has rule nr -1, etc.
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000815 * This function expects the ebt_{match,watcher,target} members of new_entry
Bart De Schuymer6622a012005-01-19 21:09:05 +0000816 * to contain pointers to ebt_u_{match,watcher,target}. */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000817void ebt_delete_rule(struct ebt_u_replace *replace,
818 struct ebt_u_entry *new_entry, int begin, int end)
819{
820 int i, j, nr_deletes;
821 struct ebt_u_entry **u_e, *u_e2;
822 struct ebt_u_entries *entries = ebt_to_chain(replace);
823 struct ebt_cntchanges *cc = replace->counterchanges;
824 struct ebt_cntchanges **prev_cc = &(replace->counterchanges);
825
Bart De Schuymerff587202005-02-08 20:02:28 +0000826 if (check_and_change_rule_number(replace, new_entry, &begin, &end))
Bart De Schuymer6622a012005-01-19 21:09:05 +0000827 return;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000828
Bart De Schuymer6622a012005-01-19 21:09:05 +0000829 /* We're deleting rules */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000830 nr_deletes = end - begin + 1;
831 replace->nentries -= nr_deletes;
832 entries->nentries -= nr_deletes;
833
Bart De Schuymer6622a012005-01-19 21:09:05 +0000834 /* Handle counter stuff */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000835 for (i = 0; i < replace->selected_chain; i++) {
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000836 if (i < NF_BR_NUMHOOKS && !(replace->hook_entry[i]))
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000837 continue;
838 j = ebt_nr_to_chain(replace, i)->nentries;
839 while (j) {
840 if (cc->type != CNT_DEL)
841 j--;
842 prev_cc = &(cc->next);
843 cc = cc->next;
844 }
845 }
846 j = begin;
847 while (j) {
848 if (cc->type != CNT_DEL)
849 j--;
850 prev_cc = &(cc->next);
851 cc = cc->next;
852 }
853 j = nr_deletes;
854 while (j) {
855 if (cc->type != CNT_DEL) {
856 j--;
857 if (cc->type == CNT_ADD) {
858 *prev_cc = cc->next;
859 free(cc);
860 cc = *prev_cc;
861 continue;
862 }
863 cc->type = CNT_DEL;
864 }
865 prev_cc = &(cc->next);
866 cc = cc->next;
867 }
868
Bart De Schuymer6622a012005-01-19 21:09:05 +0000869 /* Go to the right position in the chain */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000870 u_e = &entries->entries;
871 for (j = 0; j < begin; j++)
872 u_e = &(*u_e)->next;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000873 /* Remove the rules */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000874 j = nr_deletes;
875 while(j--) {
876 u_e2 = *u_e;
877 *u_e = (*u_e)->next;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000878 /* Free everything */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000879 ebt_free_u_entry(u_e2);
880 free(u_e2);
881 }
882
Bart De Schuymer6622a012005-01-19 21:09:05 +0000883 /* Update the counter_offset of chains behind this one */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000884 j = replace->selected_chain;
885 while (1) {
886 j++;
887 entries = ebt_nr_to_chain(replace, j);
888 if (!entries) {
889 if (j < NF_BR_NUMHOOKS)
890 continue;
891 else
892 break;
893 } else
894 entries->counter_offset -= nr_deletes;
895 }
896}
897
Bart De Schuymerff587202005-02-08 20:02:28 +0000898/* Change the counters of a rule or rules
899 * begin == end == 0: change counters of the rule corresponding to new_entry
900 *
901 * The first rule has rule nr 1, the last rule has rule nr -1, etc.
902 * This function expects the ebt_{match,watcher,target} members of new_entry
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000903 * to contain pointers to ebt_u_{match,watcher,target}.
904 * The mask denotes the following:
905 * pcnt: mask % 3 = 0 : change; = 1: increment; = 2: decrement
906 * bcnt: mask / 3 = 0 : change; = 1: increment = 2: increment
907 * In daemon mode, mask==0 must hold */
Bart De Schuymerff587202005-02-08 20:02:28 +0000908void ebt_change_counters(struct ebt_u_replace *replace,
909 struct ebt_u_entry *new_entry, int begin, int end,
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000910 struct ebt_counter *cnt, int mask)
Bart De Schuymerff587202005-02-08 20:02:28 +0000911{
912 int i, j;
913 struct ebt_u_entry *u_e;
914 struct ebt_u_entries *entries = ebt_to_chain(replace);
915 struct ebt_cntchanges *cc = replace->counterchanges;
Bart De Schuymerff587202005-02-08 20:02:28 +0000916
917 if (check_and_change_rule_number(replace, new_entry, &begin, &end))
918 return;
919
920 for (i = 0; i < replace->selected_chain; i++) {
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000921 if (i < NF_BR_NUMHOOKS && !(replace->hook_entry[i]))
Bart De Schuymerff587202005-02-08 20:02:28 +0000922 continue;
923 j = ebt_nr_to_chain(replace, i)->nentries;
924 while (j) {
925 if (cc->type != CNT_DEL)
926 j--;
Bart De Schuymerff587202005-02-08 20:02:28 +0000927 cc = cc->next;
928 }
929 }
930 i = begin;
931 while (i) {
932 if (cc->type != CNT_DEL)
933 i--;
Bart De Schuymerff587202005-02-08 20:02:28 +0000934 cc = cc->next;
935 }
936 u_e = entries->entries;
937 for (i = 0; i < begin; i++)
938 u_e = u_e->next;
939 i = end - begin + 1;
Bart De Schuymerab611e22005-02-14 20:20:03 +0000940 while (i) {
941 if (cc->type != CNT_DEL) {
942 i--;
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000943 if (mask % 3 == 0) {
944 u_e->cnt.pcnt = (*cnt).pcnt;
945 u_e->cnt_surplus.pcnt = 0;
946 } else {
947 if (cc->type != CNT_NORM)
948 ebt_print_bug("cc->type != CNT_NORM");
949 u_e->cnt_surplus.pcnt = (*cnt).pcnt;
Bart De Schuymerab611e22005-02-14 20:20:03 +0000950 }
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000951
952 if (mask / 3 == 0) {
953 u_e->cnt.bcnt = (*cnt).bcnt;
954 u_e->cnt_surplus.bcnt = 0;
955 } else {
956 if (cc->type != CNT_NORM)
957 ebt_print_bug("cc->type != CNT_NORM");
958 u_e->cnt_surplus.bcnt = (*cnt).bcnt;
959 }
960 if (cc->type == CNT_NORM || cc->type == CNT_ZERO)
961 cc->type = CNT_CHANGE;
962 cc->change = mask;
Bart De Schuymerab611e22005-02-14 20:20:03 +0000963 u_e = u_e->next;
964 }
965 cc = cc->next;
Bart De Schuymerff587202005-02-08 20:02:28 +0000966 }
967}
968
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000969/* If selected_chain == -1 then zero all counters,
970 * otherwise, zero the counters of selected_chain */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000971void ebt_zero_counters(struct ebt_u_replace *replace)
972{
973 struct ebt_u_entries *entries = ebt_to_chain(replace);
974 struct ebt_cntchanges *cc = replace->counterchanges;
Bart De Schuymer93f36ba2005-01-24 21:11:24 +0000975 struct ebt_u_entry *next;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000976 int i, j;
977
978 if (!entries) {
979 while (cc) {
980 if (cc->type == CNT_NORM)
981 cc->type = CNT_ZERO;
982 cc = cc->next;
983 }
Bart De Schuymer93f36ba2005-01-24 21:11:24 +0000984 i = -1;
985 while (1) {
986 i++;
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000987 if (i < NF_BR_NUMHOOKS && !(replace->hook_entry[i]))
Bart De Schuymer93f36ba2005-01-24 21:11:24 +0000988 continue;
989 entries = ebt_nr_to_chain(replace, i);
990 if (!entries) {
991 if (i < NF_BR_NUMHOOKS)
992 ebt_print_bug("i < NF_BR_NUMHOOKS");
993 break;
994 }
995 next = entries->entries;
996 while (next) {
997 next->cnt.bcnt = next->cnt.pcnt = 0;
998 next = next->next;
999 }
1000 }
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001001 } else {
Bart De Schuymer93f36ba2005-01-24 21:11:24 +00001002 next = entries->entries;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001003 if (entries->nentries == 0)
1004 return;
1005
1006 for (i = 0; i < replace->selected_chain; i++) {
Bart De Schuymer0436eda2005-03-28 20:29:37 +00001007 if (i < NF_BR_NUMHOOKS && !(replace->hook_entry[i]))
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001008 continue;
1009 j = ebt_nr_to_chain(replace, i)->nentries;
1010 while (j) {
1011 if (cc->type != CNT_DEL)
1012 j--;
1013 cc = cc->next;
1014 }
1015 }
1016 j = entries->nentries;
1017 while (j) {
1018 if (cc->type != CNT_DEL) {
1019 j--;
1020 if (cc->type == CNT_NORM)
1021 cc->type = CNT_ZERO;
1022 }
1023 cc = cc->next;
1024 }
Bart De Schuymer93f36ba2005-01-24 21:11:24 +00001025 while (next) {
1026 next->cnt.bcnt = next->cnt.pcnt = 0;
1027 next = next->next;
1028 }
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001029 }
1030}
1031
Bart De Schuymer6622a012005-01-19 21:09:05 +00001032/* Add a new chain and specify its policy */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001033void ebt_new_chain(struct ebt_u_replace *replace, const char *name, int policy)
1034{
1035 struct ebt_u_chain_list *cl, **cl2;
1036
Bart De Schuymer6622a012005-01-19 21:09:05 +00001037 if (ebt_get_chainnr(replace, name) != -1) {
Bart De Schuymer64182a32004-01-21 20:39:54 +00001038 ebt_print_error("Chain %s already exists", optarg);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001039 return;
1040 } else if (ebt_find_target(name)) {
Bart De Schuymer64182a32004-01-21 20:39:54 +00001041 ebt_print_error("Target with name %s exists", optarg);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001042 return;
1043 } else if (strlen(optarg) >= EBT_CHAIN_MAXNAMELEN) {
Bart De Schuymer64182a32004-01-21 20:39:54 +00001044 ebt_print_error("Chain name length can't exceed %d",
1045 EBT_CHAIN_MAXNAMELEN - 1);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001046 return;
1047 }
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001048 cl = (struct ebt_u_chain_list *)
1049 malloc(sizeof(struct ebt_u_chain_list));
1050 if (!cl)
Bart De Schuymer64182a32004-01-21 20:39:54 +00001051 ebt_print_memory();
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001052 cl->next = NULL;
Bart De Schuymer0436eda2005-03-28 20:29:37 +00001053 cl->udc = (struct ebt_u_entries *)malloc(sizeof(struct ebt_u_entries));
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001054 if (!cl->udc)
Bart De Schuymer64182a32004-01-21 20:39:54 +00001055 ebt_print_memory();
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001056 cl->udc->nentries = 0;
1057 cl->udc->policy = policy;
1058 cl->udc->counter_offset = replace->nentries;
1059 cl->udc->hook_mask = 0;
1060 strcpy(cl->udc->name, name);
1061 cl->udc->entries = NULL;
1062 cl->kernel_start = NULL;
Bart De Schuymer6622a012005-01-19 21:09:05 +00001063 /* Put the new chain at the end */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001064 cl2 = &(replace->udc);
1065 while (*cl2)
1066 cl2 = &((*cl2)->next);
1067 *cl2 = cl;
1068}
1069
Bart De Schuymer6622a012005-01-19 21:09:05 +00001070/* Selected_chain == -1: delete all non-referenced udc
1071 * selected_chain < NF_BR_NUMHOOKS is illegal */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001072void ebt_delete_chain(struct ebt_u_replace *replace)
1073{
Bart De Schuymer6622a012005-01-19 21:09:05 +00001074 int chain_nr = replace->selected_chain, print_error = 1;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001075
1076 if (chain_nr != -1 && chain_nr < NF_BR_NUMHOOKS)
Bart De Schuymer64182a32004-01-21 20:39:54 +00001077 ebt_print_bug("You can't remove a standard chain");
Bart De Schuymer6622a012005-01-19 21:09:05 +00001078 if (chain_nr == -1) {
1079 print_error = 0;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001080 replace->selected_chain = NF_BR_NUMHOOKS;
Bart De Schuymer6622a012005-01-19 21:09:05 +00001081 }
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001082 do {
1083 if (ebt_to_chain(replace) == NULL) {
1084 if (chain_nr == -1)
1085 break;
Bart De Schuymer64182a32004-01-21 20:39:54 +00001086 ebt_print_bug("udc nr %d doesn't exist", chain_nr);
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001087 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001088 /* If the chain is referenced, don't delete it,
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001089 * also decrement jumps to a chain behind the
Bart De Schuymer6622a012005-01-19 21:09:05 +00001090 * one we're deleting */
1091 if (ebt_check_for_references(replace, print_error)) {
1092 if (chain_nr != -1)
1093 break;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001094 replace->selected_chain++;
1095 continue;
1096 }
1097 decrease_chain_jumps(replace);
1098 ebt_flush_chains(replace);
1099 remove_udc(replace);
Bart De Schuymerc6c0dc32004-12-16 19:30:32 +00001100 } while (chain_nr == -1);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001101 replace->selected_chain = chain_nr; /* Put back to -1 */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001102}
1103
Bart De Schuymer6622a012005-01-19 21:09:05 +00001104/* Rename an existing chain. */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001105void ebt_rename_chain(struct ebt_u_replace *replace, const char *name)
1106{
1107 struct ebt_u_entries *entries = ebt_to_chain(replace);
1108
1109 if (!entries)
Bart De Schuymer64182a32004-01-21 20:39:54 +00001110 ebt_print_bug("ebt_rename_chain: entries == NULL");
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001111 strcpy(entries->name, name);
1112}
1113
1114
1115 /*
1116*************************
1117*************************
1118**SPECIALIZED*FUNCTIONS**
1119*************************
1120*************************
1121 */
1122
1123
Bart De Schuymer6622a012005-01-19 21:09:05 +00001124/* Executes the final_check() function for all extensions used by the rule
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001125 * ebt_check_for_loops should have been executed earlier, to make sure the
Bart De Schuymer6622a012005-01-19 21:09:05 +00001126 * hook_mask is correct. The time argument to final_check() is set to 1,
1127 * meaning it's the second time the final_check() function is executed. */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001128void ebt_do_final_checks(struct ebt_u_replace *replace, struct ebt_u_entry *e,
1129 struct ebt_u_entries *entries)
1130{
1131 struct ebt_u_match_list *m_l;
1132 struct ebt_u_watcher_list *w_l;
1133 struct ebt_u_target *t;
1134 struct ebt_u_match *m;
1135 struct ebt_u_watcher *w;
1136
1137 m_l = e->m_list;
1138 w_l = e->w_list;
1139 while (m_l) {
1140 m = ebt_find_match(m_l->m->u.name);
1141 m->final_check(e, m_l->m, replace->name,
1142 entries->hook_mask, 1);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001143 if (ebt_errormsg[0] != '\0')
1144 return;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001145 m_l = m_l->next;
1146 }
1147 while (w_l) {
1148 w = ebt_find_watcher(w_l->w->u.name);
1149 w->final_check(e, w_l->w, replace->name,
1150 entries->hook_mask, 1);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001151 if (ebt_errormsg[0] != '\0')
1152 return;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001153 w_l = w_l->next;
1154 }
1155 t = ebt_find_target(e->t->u.name);
1156 t->final_check(e, e->t, replace->name,
1157 entries->hook_mask, 1);
1158}
1159
Bart De Schuymer6622a012005-01-19 21:09:05 +00001160/* Returns 1 (if it returns) when the chain is referenced, 0 when it isn't.
1161 * print_err: 0 (resp. 1) = don't (resp. do) print error when referenced */
1162int ebt_check_for_references(struct ebt_u_replace *replace, int print_err)
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001163{
Bart De Schuymer6622a012005-01-19 21:09:05 +00001164 if (print_err)
1165 return iterate_entries(replace, 1);
1166 else
1167 return iterate_entries(replace, 2);
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001168}
1169
Bart De Schuymer6622a012005-01-19 21:09:05 +00001170/* chain_nr: nr of the udc (>= NF_BR_NUMHOOKS)
1171 * Returns 1 (if it returns) when the chain is referenced, 0 when it isn't.
1172 * print_err: 0 (resp. 1) = don't (resp. do) print error when referenced */
1173int ebt_check_for_references2(struct ebt_u_replace *replace, int chain_nr,
1174 int print_err)
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001175{
1176 int tmp = replace->selected_chain, ret;
1177
1178 replace->selected_chain = chain_nr;
Bart De Schuymer6622a012005-01-19 21:09:05 +00001179 if (print_err)
1180 ret = iterate_entries(replace, 1);
1181 else
1182 ret = iterate_entries(replace, 2);
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001183 replace->selected_chain = tmp;
1184 return ret;
1185}
1186
1187struct ebt_u_stack
1188{
1189 int chain_nr;
1190 int n;
1191 struct ebt_u_entry *e;
1192 struct ebt_u_entries *entries;
1193};
1194
Bart De Schuymer6622a012005-01-19 21:09:05 +00001195/* Checks for loops
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001196 * As a by-product, the hook_mask member of each chain is filled in
1197 * correctly. The check functions of the extensions need this hook_mask
Bart De Schuymer6622a012005-01-19 21:09:05 +00001198 * to know from which standard chains they can be called. */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001199void ebt_check_for_loops(struct ebt_u_replace *replace)
1200{
1201 int chain_nr , i, j , k, sp = 0, verdict;
1202 struct ebt_u_entries *entries, *entries2;
1203 struct ebt_u_stack *stack = NULL;
1204 struct ebt_u_entry *e;
1205
1206 i = -1;
Bart De Schuymer6622a012005-01-19 21:09:05 +00001207 /* Initialize hook_mask to 0 */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001208 while (1) {
1209 i++;
Bart De Schuymer0436eda2005-03-28 20:29:37 +00001210 if (i < NF_BR_NUMHOOKS && !(replace->hook_entry[i]))
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001211 continue;
1212 entries = ebt_nr_to_chain(replace, i);
1213 if (!entries)
1214 break;
1215 entries->hook_mask = 0;
1216 }
1217 if (i > NF_BR_NUMHOOKS) {
Bart De Schuymer0436eda2005-03-28 20:29:37 +00001218 stack = (struct ebt_u_stack *)malloc((i - NF_BR_NUMHOOKS) * sizeof(struct ebt_u_stack));
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001219 if (!stack)
Bart De Schuymer64182a32004-01-21 20:39:54 +00001220 ebt_print_memory();
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001221 }
1222
Bart De Schuymer6622a012005-01-19 21:09:05 +00001223 /* Check for loops, starting from every base chain */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001224 for (i = 0; i < NF_BR_NUMHOOKS; i++) {
Bart De Schuymer0436eda2005-03-28 20:29:37 +00001225 if (!(replace->hook_entry[i]))
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001226 continue;
1227 entries = ebt_nr_to_chain(replace, i);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001228 /* (1 << NF_BR_NUMHOOKS) implies it's a standard chain
1229 * (usefull in the final_check() funtions) */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001230 entries->hook_mask = (1 << i) | (1 << NF_BR_NUMHOOKS);
1231 chain_nr = i;
1232
1233 e = entries->entries;
1234 for (j = 0; j < entries->nentries; j++) {
1235 if (strcmp(e->t->u.name, EBT_STANDARD_TARGET))
1236 goto letscontinue;
1237 verdict = ((struct ebt_standard_target *)(e->t))->verdict;
1238 if (verdict < 0)
1239 goto letscontinue;
Bart De Schuymer6622a012005-01-19 21:09:05 +00001240 entries2 = ebt_nr_to_chain(replace, verdict + NF_BR_NUMHOOKS);
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001241 entries2->hook_mask |= entries->hook_mask;
Bart De Schuymer6622a012005-01-19 21:09:05 +00001242 /* Now see if we've been here before */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001243 for (k = 0; k < sp; k++)
Bart De Schuymer6622a012005-01-19 21:09:05 +00001244 if (stack[k].chain_nr == verdict + NF_BR_NUMHOOKS) {
1245 ebt_print_error("Loop from chain '%s' to chain '%s'",
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001246 ebt_nr_to_chain(replace, chain_nr)->name,
1247 ebt_nr_to_chain(replace, stack[k].chain_nr)->name);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001248 goto free_stack;
1249 }
1250 /* Jump to the chain, make sure we know how to get back */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001251 stack[sp].chain_nr = chain_nr;
1252 stack[sp].n = j;
1253 stack[sp].entries = entries;
1254 stack[sp].e = e;
1255 sp++;
1256 j = -1;
1257 e = entries2->entries;
1258 chain_nr = verdict + NF_BR_NUMHOOKS;
1259 entries = entries2;
1260 continue;
1261letscontinue:
1262 e = e->next;
1263 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001264 /* We are at the end of a standard chain */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001265 if (sp == 0)
1266 continue;
Bart De Schuymer6622a012005-01-19 21:09:05 +00001267 /* Go back to the chain one level higher */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001268 sp--;
1269 j = stack[sp].n;
1270 chain_nr = stack[sp].chain_nr;
1271 e = stack[sp].e;
1272 entries = stack[sp].entries;
1273 goto letscontinue;
1274 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001275free_stack:
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001276 free(stack);
1277 return;
1278}
1279
Bart De Schuymer6622a012005-01-19 21:09:05 +00001280/* The user will use the match, so put it in new_entry. The ebt_u_match
1281 * pointer is put in the ebt_entry_match pointer. ebt_add_rule will
1282 * fill in the final value for new->m. Unless the rule is added to a chain,
1283 * the pointer will keep pointing to the ebt_u_match (until the new_entry
1284 * is freed). I know, I should use a union for these 2 pointer types... */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001285void ebt_add_match(struct ebt_u_entry *new_entry, struct ebt_u_match *m)
1286{
1287 struct ebt_u_match_list **m_list, *new;
1288
1289 for (m_list = &new_entry->m_list; *m_list; m_list = &(*m_list)->next);
1290 new = (struct ebt_u_match_list *)
1291 malloc(sizeof(struct ebt_u_match_list));
1292 if (!new)
Bart De Schuymer64182a32004-01-21 20:39:54 +00001293 ebt_print_memory();
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001294 *m_list = new;
1295 new->next = NULL;
1296 new->m = (struct ebt_entry_match *)m;
1297}
1298
1299void ebt_add_watcher(struct ebt_u_entry *new_entry, struct ebt_u_watcher *w)
1300{
1301 struct ebt_u_watcher_list **w_list;
1302 struct ebt_u_watcher_list *new;
1303
1304 for (w_list = &new_entry->w_list; *w_list; w_list = &(*w_list)->next);
1305 new = (struct ebt_u_watcher_list *)
1306 malloc(sizeof(struct ebt_u_watcher_list));
1307 if (!new)
Bart De Schuymer64182a32004-01-21 20:39:54 +00001308 ebt_print_memory();
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001309 *w_list = new;
1310 new->next = NULL;
1311 new->w = (struct ebt_entry_watcher *)w;
1312}
1313
1314
1315 /*
1316*******************
1317*******************
1318**OTHER*FUNCTIONS**
1319*******************
1320*******************
1321 */
1322
1323
Bart De Schuymer6622a012005-01-19 21:09:05 +00001324/* type = 0 => update chain jumps
1325 * type = 1 => check for reference, print error when referenced
1326 * type = 2 => check for reference, don't print error when referenced
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001327 *
Bart De Schuymer6622a012005-01-19 21:09:05 +00001328 * Returns 1 when type == 1 and the chain is referenced
1329 * returns 0 otherwise */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001330static int iterate_entries(struct ebt_u_replace *replace, int type)
1331{
1332 int i = -1, j, chain_nr = replace->selected_chain - NF_BR_NUMHOOKS;
1333 struct ebt_u_entries *entries;
1334 struct ebt_u_entry *e;
1335
1336 if (chain_nr < 0)
Bart De Schuymer64182a32004-01-21 20:39:54 +00001337 ebt_print_bug("iterate_entries: udc = %d < 0", chain_nr);
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001338 while (1) {
1339 i++;
1340 entries = ebt_nr_to_chain(replace, i);
1341 if (!entries) {
1342 if (i < NF_BR_NUMHOOKS)
1343 continue;
1344 else
1345 break;
1346 }
1347 e = entries->entries;
1348 j = 0;
1349 while (e) {
1350 int chain_jmp;
1351
1352 j++;
1353 if (strcmp(e->t->u.name, EBT_STANDARD_TARGET)) {
1354 e = e->next;
1355 continue;
1356 }
Bart De Schuymer64182a32004-01-21 20:39:54 +00001357 chain_jmp = ((struct ebt_standard_target *)e->t)->
1358 verdict;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001359 switch (type) {
1360 case 1:
Bart De Schuymer6622a012005-01-19 21:09:05 +00001361 case 2:
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001362 if (chain_jmp == chain_nr) {
Bart De Schuymer6622a012005-01-19 21:09:05 +00001363 if (type == 2)
1364 return 1;
1365 ebt_print_error("Can't delete the chain '%s', it's referenced in chain '%s', rule %d",
1366 ebt_nr_to_chain(replace, chain_nr + NF_BR_NUMHOOKS)->name, entries->name, j);
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001367 return 1;
1368 }
1369 break;
1370 case 0:
Bart De Schuymer6622a012005-01-19 21:09:05 +00001371 /* Adjust the chain jumps when necessary */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001372 if (chain_jmp > chain_nr)
1373 ((struct ebt_standard_target *)e->t)->verdict--;
1374 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +00001375 } /* End switch */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001376 e = e->next;
1377 }
1378 }
1379 return 0;
1380}
1381
1382static void decrease_chain_jumps(struct ebt_u_replace *replace)
1383{
1384 iterate_entries(replace, 0);
1385}
1386
Bart De Schuymer6622a012005-01-19 21:09:05 +00001387/* Selected_chain >= NF_BR_NUMHOOKS */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001388static void remove_udc(struct ebt_u_replace *replace)
1389{
1390 struct ebt_u_chain_list *cl, **cl2;
1391 struct ebt_u_entries *entries;
1392 struct ebt_u_entry *u_e, *tmp;
1393 int chain_nr = replace->selected_chain;
1394
1395 if (chain_nr < NF_BR_NUMHOOKS)
Bart De Schuymer64182a32004-01-21 20:39:54 +00001396 ebt_print_bug("remove_udc: chain_nr = %d < %d", chain_nr,
1397 NF_BR_NUMHOOKS);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001398 /* First free the rules */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001399 entries = ebt_nr_to_chain(replace, chain_nr);
1400 u_e = entries->entries;
1401 while (u_e) {
1402 ebt_free_u_entry(u_e);
1403 tmp = u_e->next;
1404 free(u_e);
1405 u_e = tmp;
1406 }
1407
1408 /* next, remove the chain */
1409 cl2 = &(replace->udc);
1410 while ((*cl2)->udc != entries)
1411 cl2 = &((*cl2)->next);
1412 cl = (*cl2);
1413 (*cl2) = (*cl2)->next;
1414 free(cl->udc);
1415 free(cl);
1416}
1417
Bart De Schuymer6622a012005-01-19 21:09:05 +00001418/* Used in initialization code of modules */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001419void ebt_register_match(struct ebt_u_match *m)
1420{
1421 int size = EBT_ALIGN(m->size) + sizeof(struct ebt_entry_match);
1422 struct ebt_u_match **i;
1423
1424 m->m = (struct ebt_entry_match *)malloc(size);
1425 if (!m->m)
Bart De Schuymer64182a32004-01-21 20:39:54 +00001426 ebt_print_memory();
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001427 strcpy(m->m->u.name, m->name);
1428 m->m->match_size = EBT_ALIGN(m->size);
1429 m->init(m->m);
1430
1431 for (i = &ebt_matches; *i; i = &((*i)->next));
1432 m->next = NULL;
1433 *i = m;
1434}
1435
1436void ebt_register_watcher(struct ebt_u_watcher *w)
1437{
1438 int size = EBT_ALIGN(w->size) + sizeof(struct ebt_entry_watcher);
1439 struct ebt_u_watcher **i;
1440
1441 w->w = (struct ebt_entry_watcher *)malloc(size);
1442 if (!w->w)
Bart De Schuymer64182a32004-01-21 20:39:54 +00001443 ebt_print_memory();
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001444 strcpy(w->w->u.name, w->name);
1445 w->w->watcher_size = EBT_ALIGN(w->size);
1446 w->init(w->w);
1447
1448 for (i = &ebt_watchers; *i; i = &((*i)->next));
1449 w->next = NULL;
1450 *i = w;
1451}
1452
1453void ebt_register_target(struct ebt_u_target *t)
1454{
1455 int size = EBT_ALIGN(t->size) + sizeof(struct ebt_entry_target);
1456 struct ebt_u_target **i;
1457
1458 t->t = (struct ebt_entry_target *)malloc(size);
1459 if (!t->t)
Bart De Schuymer64182a32004-01-21 20:39:54 +00001460 ebt_print_memory();
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001461 strcpy(t->t->u.name, t->name);
1462 t->t->target_size = EBT_ALIGN(t->size);
1463 t->init(t->t);
1464
1465 for (i = &ebt_targets; *i; i = &((*i)->next));
1466 t->next = NULL;
1467 *i = t;
1468}
1469
1470void ebt_register_table(struct ebt_u_table *t)
1471{
1472 t->next = ebt_tables;
1473 ebt_tables = t;
1474}
1475
1476void ebt_iterate_matches(void (*f)(struct ebt_u_match *))
1477{
1478 struct ebt_u_match *i;
1479
1480 for (i = ebt_matches; i; i = i->next)
1481 f(i);
1482}
1483
1484void ebt_iterate_watchers(void (*f)(struct ebt_u_watcher *))
1485{
1486 struct ebt_u_watcher *i;
1487
1488 for (i = ebt_watchers; i; i = i->next)
1489 f(i);
1490}
1491
1492void ebt_iterate_targets(void (*f)(struct ebt_u_target *))
1493{
1494 struct ebt_u_target *i;
1495
1496 for (i = ebt_targets; i; i = i->next)
1497 f(i);
1498}
1499
Bart De Schuymer6622a012005-01-19 21:09:05 +00001500/* Don't use this function, use ebt_print_bug() */
Bart De Schuymer64182a32004-01-21 20:39:54 +00001501void __ebt_print_bug(char *file, int line, char *format, ...)
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001502{
1503 va_list l;
1504
1505 va_start(l, format);
1506 printf(PROGNAME" v"PROGVERSION":%s:%d:--BUG--: \n", file, line);
1507 vprintf(format, l);
1508 printf("\n");
1509 va_end(l);
1510 exit (-1);
1511}
1512
Bart De Schuymer6622a012005-01-19 21:09:05 +00001513/* The error messages are put in here when ebt_silent == 1
1514 * ebt_errormsg[0] == '\0' implies there was no error */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001515char ebt_errormsg[ERRORMSG_MAXLEN];
Bart De Schuymer6622a012005-01-19 21:09:05 +00001516/* When error messages should not be printed on the screen, after which
1517 * the program exit()s, set ebt_silent to 1. */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001518int ebt_silent;
Bart De Schuymer6622a012005-01-19 21:09:05 +00001519/* Don't use this function, use ebt_print_error() */
Bart De Schuymer64182a32004-01-21 20:39:54 +00001520void __ebt_print_error(char *format, ...)
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001521{
1522 va_list l;
1523
1524 va_start(l, format);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001525 if (ebt_silent && ebt_errormsg[0] == '\0') {
1526 vsnprintf(ebt_errormsg, ERRORMSG_MAXLEN, format, l);
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001527 va_end(l);
1528 } else {
1529 vprintf(format, l);
Bart De Schuymer87889eb2005-01-24 22:25:27 +00001530 printf(".\n");
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001531 va_end(l);
1532 exit (-1);
1533 }
1534}