blob: 964fa8e917fc300c1eafd523e7061072a34b27fc [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
26#include <getopt.h>
27#include <string.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <stdarg.h>
31#include <netinet/ether.h>
32#include "include/ebtables_u.h"
33#include "include/ethernetdb.h"
34#include <unistd.h>
35#include <fcntl.h>
36#include <sys/wait.h>
37
38static void decrease_chain_jumps(struct ebt_u_replace *replace);
39static void remove_udc(struct ebt_u_replace *replace);
40static int iterate_entries(struct ebt_u_replace *replace, int type);
41
Bart De Schuymer6622a012005-01-19 21:09:05 +000042/* The standard names */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +000043const char *ebt_hooknames[NF_BR_NUMHOOKS] =
44{
45 [NF_BR_PRE_ROUTING]"PREROUTING",
46 [NF_BR_LOCAL_IN]"INPUT",
47 [NF_BR_FORWARD]"FORWARD",
48 [NF_BR_LOCAL_OUT]"OUTPUT",
49 [NF_BR_POST_ROUTING]"POSTROUTING",
50 [NF_BR_BROUTING]"BROUTING"
51};
52
Bart De Schuymer6622a012005-01-19 21:09:05 +000053/* The four target names */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +000054const char* ebt_standard_targets[NUM_STANDARD_TARGETS] =
55{
56 "ACCEPT",
57 "DROP",
58 "CONTINUE",
59 "RETURN",
60};
61
Bart De Schuymer6622a012005-01-19 21:09:05 +000062/* The lists of supported tables, matches, watchers and targets */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +000063struct ebt_u_table *ebt_tables;
64struct ebt_u_match *ebt_matches;
65struct ebt_u_watcher *ebt_watchers;
66struct ebt_u_target *ebt_targets;
67
Bart De Schuymer6622a012005-01-19 21:09:05 +000068/* Find the right structure belonging to a name */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +000069struct ebt_u_target *ebt_find_target(const char *name)
70{
71 struct ebt_u_target *t = ebt_targets;
72
Bart De Schuymer6622a012005-01-19 21:09:05 +000073 while (t && strcmp(t->name, name))
Bart De Schuymer80c82bb2004-01-14 20:06:44 +000074 t = t->next;
75 return t;
76}
77
78struct ebt_u_match *ebt_find_match(const char *name)
79{
80 struct ebt_u_match *m = ebt_matches;
81
Bart De Schuymer6622a012005-01-19 21:09:05 +000082 while (m && strcmp(m->name, name))
Bart De Schuymer80c82bb2004-01-14 20:06:44 +000083 m = m->next;
84 return m;
85}
86
87struct ebt_u_watcher *ebt_find_watcher(const char *name)
88{
89 struct ebt_u_watcher *w = ebt_watchers;
90
Bart De Schuymer6622a012005-01-19 21:09:05 +000091 while (w && strcmp(w->name, name))
Bart De Schuymer80c82bb2004-01-14 20:06:44 +000092 w = w->next;
93 return w;
94}
95
96struct ebt_u_table *ebt_find_table(const char *name)
97{
98 struct ebt_u_table *t = ebt_tables;
99
100 while (t && strcmp(t->name, name))
101 t = t->next;
102 return t;
103}
104
Bart De Schuymer6622a012005-01-19 21:09:05 +0000105/* Prints all registered extensions */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000106void ebt_list_extensions()
107{
108 struct ebt_u_table *tbl = ebt_tables;
109 struct ebt_u_target *t = ebt_targets;
110 struct ebt_u_match *m = ebt_matches;
111 struct ebt_u_watcher *w = ebt_watchers;
112
113 PRINT_VERSION;
114 printf("Supported userspace extensions:\n\nSupported tables:\n");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000115 while (tbl) {
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000116 printf("%s\n", tbl->name);
117 tbl = tbl->next;
118 }
119 printf("\nSupported targets:\n");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000120 while (t) {
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000121 printf("%s\n", t->name);
122 t = t->next;
123 }
124 printf("\nSupported matches:\n");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000125 while (m) {
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000126 printf("%s\n", m->name);
127 m = m->next;
128 }
129 printf("\nSupported watchers:\n");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000130 while (w) {
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000131 printf("%s\n", w->name);
132 w = w->next;
133 }
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000134}
135
Bart De Schuymer6622a012005-01-19 21:09:05 +0000136/* Get the table from the kernel or from a binary file
Bart De Schuymer64182a32004-01-21 20:39:54 +0000137 * init: 1 = ask the kernel for the initial contents of a table, i.e. the
138 * way it looks when the table is insmod'ed
Bart De Schuymer6622a012005-01-19 21:09:05 +0000139 * 0 = get the current data in the table */
140int ebt_get_kernel_table(struct ebt_u_replace *replace, int init)
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000141{
Bart De Schuymer6622a012005-01-19 21:09:05 +0000142 if (!ebt_find_table(replace->name)) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000143 ebt_print_error("Bad table name");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000144 return -1;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000145 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000146 /* Get the kernel's information */
147 if (ebt_get_table(replace, init)) {
148 if (ebt_errormsg[0] != '\0')
149 return -1;
150 ebtables_insmod("ebtables");
151 if (ebt_get_table(replace, init)) {
152 ebt_print_error("The kernel doesn't support the ebtables %s table", replace->name);
153 return -1;
154 }
155 }
156 return 0;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000157}
158
Bart De Schuymer6622a012005-01-19 21:09:05 +0000159/* Put sane values into a new entry */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000160void ebt_initialize_entry(struct ebt_u_entry *e)
161{
162 e->bitmask = EBT_NOPROTO;
163 e->invflags = 0;
164 e->ethproto = 0;
165 strcpy(e->in, "");
166 strcpy(e->out, "");
167 strcpy(e->logical_in, "");
168 strcpy(e->logical_out, "");
169 e->m_list = NULL;
170 e->w_list = NULL;
171 e->t = (struct ebt_entry_target *)ebt_find_target(EBT_STANDARD_TARGET);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000172 ebt_find_target(EBT_STANDARD_TARGET)->used = 1;
Bart De Schuymerab611e22005-02-14 20:20:03 +0000173 e->cnt.pcnt = e->cnt.bcnt = e->cnt_surplus.pcnt = e->cnt_surplus.bcnt = 0;
Bart De Schuymer64182a32004-01-21 20:39:54 +0000174
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000175 if (!e->t)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000176 ebt_print_bug("Couldn't load standard target");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000177 ((struct ebt_standard_target *)((struct ebt_u_target *)e->t)->t)->verdict = EBT_CONTINUE;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000178}
179
Bart De Schuymer6622a012005-01-19 21:09:05 +0000180/* Free up the memory of the table held in userspace, *replace can be reused */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000181void ebt_cleanup_replace(struct ebt_u_replace *replace)
182{
183 int i;
184 struct ebt_u_entries *entries;
185 struct ebt_u_chain_list *udc1, *udc2;
186 struct ebt_cntchanges *cc1, *cc2;
187 struct ebt_u_entry *u_e1, *u_e2;
188
189 replace->name[0] = '\0';
190 replace->valid_hooks = 0;
191 replace->nentries = 0;
192 replace->num_counters = 0;
193 replace->flags = 0;
194 replace->command = 0;
195 replace->selected_chain = -1;
196 if (replace->filename) {
197 free(replace->filename);
198 replace->filename = NULL;
199 }
200 if (replace->counters) {
201 free(replace->counters);
202 replace->counters = NULL;
203 }
204
205 i = -1;
206 while (1) {
207 i++;
208 entries = ebt_nr_to_chain(replace, i);
209 if (!entries) {
210 if (i < NF_BR_NUMHOOKS)
211 continue;
212 else
213 break;
214 }
215 entries->nentries = 0;
216 entries->counter_offset = 0;
217 u_e1 = entries->entries;
218 entries->entries = NULL;
219 while (u_e1) {
220 ebt_free_u_entry(u_e1);
221 u_e2 = u_e1->next;
222 free(u_e1);
223 u_e1 = u_e2;
224 }
225 }
226 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 Schuymer6622a012005-01-19 21:09:05 +0000317/* Blatently stolen (again) from iptables.c userspace program
318 * find out where the modprobe utility is located */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000319static char *get_modprobe(void)
320{
321 int procfile;
322 char *ret;
323
324 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
325 if (procfile < 0)
326 return NULL;
327
328 ret = malloc(1024);
329 if (ret) {
330 switch (read(procfile, ret, 1024)) {
331 case -1: goto fail;
332 case 1024: goto fail; /* Partial read. Wierd */
333 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000334 if (ret[strlen(ret)-1] == '\n')
335 ret[strlen(ret)-1] = 0;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000336 close(procfile);
337 return ret;
338 }
339 fail:
340 free(ret);
341 close(procfile);
342 return NULL;
343}
344
345char *ebt_modprobe;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000346/* Try to load the kernel module */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000347int ebtables_insmod(const char *modname)
348{
349 char *buf = NULL;
350 char *argv[3];
351
352 /* If they don't explicitly set it, read out of kernel */
353 if (!ebt_modprobe) {
354 buf = get_modprobe();
355 if (!buf)
356 return -1;
357 ebt_modprobe = buf;
358 }
359
360 switch (fork()) {
361 case 0:
362 argv[0] = (char *)ebt_modprobe;
363 argv[1] = (char *)modname;
364 argv[2] = NULL;
365 execv(argv[0], argv);
366
Bart De Schuymer6622a012005-01-19 21:09:05 +0000367 /* Not usually reached */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000368 exit(0);
369 case -1:
370 return -1;
371
Bart De Schuymer6622a012005-01-19 21:09:05 +0000372 default: /* Parent */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000373 wait(NULL);
374 }
375
376 free(buf);
377 return 0;
378}
379
Bart De Schuymer6622a012005-01-19 21:09:05 +0000380/* Gives back a pointer to the chain base, based on nr.
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000381 * If nr >= NF_BR_NUMHOOKS you'll get back a user-defined chain.
Bart De Schuymer6622a012005-01-19 21:09:05 +0000382 * Returns NULL on failure. */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000383struct ebt_u_entries *ebt_nr_to_chain(const struct ebt_u_replace *replace,
384 int nr)
385{
386 if (nr == -1)
387 return NULL;
388 if (nr < NF_BR_NUMHOOKS)
389 return replace->hook_entry[nr];
390 else {
391 int i;
392 struct ebt_u_chain_list *cl = replace->udc;
393
394 i = nr - NF_BR_NUMHOOKS;
395 while (i > 0 && cl) {
396 cl = cl->next;
397 i--;
398 }
399 if (cl)
400 return cl->udc;
401 else
402 return NULL;
403 }
404}
405
Bart De Schuymer6622a012005-01-19 21:09:05 +0000406/* Gives back a pointer to the chain base of selected_chain */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000407struct ebt_u_entries *ebt_to_chain(const struct ebt_u_replace *replace)
408{
409 return ebt_nr_to_chain(replace, replace->selected_chain);
410}
411
Bart De Schuymer6622a012005-01-19 21:09:05 +0000412/* Parse the chain name and return a pointer to the chain base.
413 * Returns NULL on failure. */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000414struct ebt_u_entries *ebt_name_to_chain(const struct ebt_u_replace *replace,
Bart De Schuymer6622a012005-01-19 21:09:05 +0000415 const char* arg)
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000416{
417 int i;
418 struct ebt_u_chain_list *cl = replace->udc;
419
420 for (i = 0; i < NF_BR_NUMHOOKS; i++) {
421 if (!(replace->valid_hooks & (1 << i)))
422 continue;
423 if (!strcmp(arg, replace->hook_entry[i]->name))
424 return replace->hook_entry[i];
425 }
426 while(cl) {
427 if (!strcmp(arg, cl->udc->name))
428 return cl->udc;
429 cl = cl->next;
430 }
431 return NULL;
432}
433
Bart De Schuymer6622a012005-01-19 21:09:05 +0000434/* Parse the chain name and return the corresponding chain nr
435 * returns -1 on failure */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000436int ebt_get_chainnr(const struct ebt_u_replace *replace, const char* arg)
437{
438 int i;
439 struct ebt_u_chain_list *cl = replace->udc;
440
441 for (i = 0; i < NF_BR_NUMHOOKS; i++) {
442 if (!(replace->valid_hooks & (1 << i)))
443 continue;
444 if (!strcmp(arg, replace->hook_entry[i]->name))
445 return i;
446 }
447 while(cl) {
448 if (!strcmp(arg, cl->udc->name))
449 return i;
450 i++;
451 cl = cl->next;
452 }
453 return -1;
454}
455
456 /*
457************
458************
459**COMMANDS**
460************
461************
462 */
463
Bart De Schuymer6622a012005-01-19 21:09:05 +0000464/* Change the policy of selected_chain.
465 * Handing a bad policy to this function is a bug. */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000466void ebt_change_policy(struct ebt_u_replace *replace, int policy)
467{
468 struct ebt_u_entries *entries = ebt_to_chain(replace);
469
470 if (policy < -NUM_STANDARD_TARGETS || policy == EBT_CONTINUE)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000471 ebt_print_bug("Wrong policy: %d", policy);
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000472 entries->policy = policy;
473}
474
Bart De Schuymer6622a012005-01-19 21:09:05 +0000475/* Flush one chain or the complete table
476 * If selected_chain == -1: flush the complete table */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000477void ebt_flush_chains(struct ebt_u_replace *replace)
478{
479 int i, j, numdel;
480 struct ebt_u_entry *u_e, *tmp;
481 struct ebt_u_entries *entries = ebt_to_chain(replace);
482 struct ebt_cntchanges *cc = replace->counterchanges;
483 struct ebt_cntchanges **prev_cc = &(replace->counterchanges);
484
Bart De Schuymer6622a012005-01-19 21:09:05 +0000485 /* Flush whole table */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000486 if (!entries) {
487 if (replace->nentries == 0)
488 return;
489 replace->nentries = 0;
490
Bart De Schuymer6622a012005-01-19 21:09:05 +0000491 /* Free everything and zero (n)entries */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000492 i = -1;
493 while (1) {
494 i++;
495 entries = ebt_nr_to_chain(replace, i);
496 if (!entries) {
497 if (i < NF_BR_NUMHOOKS)
498 continue;
499 else
500 break;
501 }
502 entries->nentries = 0;
503 entries->counter_offset = 0;
504 u_e = entries->entries;
505 entries->entries = NULL;
506 while (u_e) {
507 ebt_free_u_entry(u_e);
508 tmp = u_e->next;
509 free(u_e);
510 u_e = tmp;
511 }
512 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000513 /* Update the counters */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000514 while (cc) {
515 if (cc->type == CNT_ADD) {
516 *prev_cc = cc->next;
517 free(cc);
518 cc = *prev_cc;
519 continue;
520 }
521 cc->type = CNT_DEL;
522 prev_cc = &(cc->next);
523 cc = cc->next;
524 }
525 return;
526 }
527
528 if (entries->nentries == 0)
529 return;
530 replace->nentries -= entries->nentries;
531 numdel = entries->nentries;
532
Bart De Schuymer6622a012005-01-19 21:09:05 +0000533 /* Delete the counters belonging to the specified chain,
534 * update counter_offset */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000535 i = -1;
536 while (1) {
537 i++;
538 entries = ebt_nr_to_chain(replace, i);
539 if (!entries) {
540 if (i < NF_BR_NUMHOOKS)
541 continue;
542 else
543 break;
544 }
545 if (i > replace->selected_chain) {
546 entries->counter_offset -= numdel;
547 continue;
548 }
549 j = entries->nentries;
550 while (j) {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000551 /* Don't count deleted entries */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000552 if (cc->type == CNT_DEL)
553 goto letscontinue;
554 if (i == replace->selected_chain) {
555 if (cc->type == CNT_ADD) {
556 *prev_cc = cc->next;
557 free(cc);
558 cc = *prev_cc;
559 j--;
560 continue;
561 }
562 cc->type = CNT_DEL;
563 }
564 j--;
565letscontinue:
566 prev_cc = &(cc->next);
567 cc = cc->next;
568 }
569 }
570
571 entries = ebt_to_chain(replace);
572 entries->nentries = 0;
573 u_e = entries->entries;
574 while (u_e) {
575 ebt_free_u_entry(u_e);
576 tmp = u_e->next;
577 free(u_e);
578 u_e = tmp;
579 }
580 entries->entries = NULL;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000581}
582
Bart De Schuymerab611e22005-02-14 20:20:03 +0000583#define OPT_COUNT 0x1000 /* This value is also defined in ebtables.c */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000584/* Returns the rule number on success (starting from 0), -1 on failure
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000585 *
586 * This function expects the ebt_{match,watcher,target} members of new_entry
Bart De Schuymer6622a012005-01-19 21:09:05 +0000587 * to contain pointers to ebt_u_{match,watcher,target} */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000588int ebt_check_rule_exists(struct ebt_u_replace *replace,
589 struct ebt_u_entry *new_entry)
590{
591 struct ebt_u_entry *u_e;
592 struct ebt_u_match_list *m_l, *m_l2;
593 struct ebt_u_match *m;
594 struct ebt_u_watcher_list *w_l, *w_l2;
595 struct ebt_u_watcher *w;
596 struct ebt_u_target *t = (struct ebt_u_target *)new_entry->t;
597 struct ebt_u_entries *entries = ebt_to_chain(replace);
598 int i, j, k;
599
600 u_e = entries->entries;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000601 /* Check for an existing rule (if there are duplicate rules,
602 * take the first occurance) */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000603 for (i = 0; i < entries->nentries; i++, u_e = u_e->next) {
604 if (!u_e)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000605 ebt_print_bug("Hmm, trouble");
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000606 if (u_e->ethproto != new_entry->ethproto)
607 continue;
608 if (strcmp(u_e->in, new_entry->in))
609 continue;
610 if (strcmp(u_e->out, new_entry->out))
611 continue;
612 if (strcmp(u_e->logical_in, new_entry->logical_in))
613 continue;
614 if (strcmp(u_e->logical_out, new_entry->logical_out))
615 continue;
616 if (new_entry->bitmask & EBT_SOURCEMAC &&
Bart De Schuymer6622a012005-01-19 21:09:05 +0000617 memcmp(u_e->sourcemac, new_entry->sourcemac, ETH_ALEN))
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000618 continue;
619 if (new_entry->bitmask & EBT_DESTMAC &&
Bart De Schuymer6622a012005-01-19 21:09:05 +0000620 memcmp(u_e->destmac, new_entry->destmac, ETH_ALEN))
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000621 continue;
622 if (new_entry->bitmask != u_e->bitmask ||
Bart De Schuymer6622a012005-01-19 21:09:05 +0000623 new_entry->invflags != u_e->invflags)
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000624 continue;
Bart De Schuymerab611e22005-02-14 20:20:03 +0000625 if (replace->flags & OPT_COUNT && (new_entry->cnt.pcnt !=
626 u_e->cnt.pcnt || new_entry->cnt.bcnt != u_e->cnt.bcnt))
627 continue;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000628 /* Compare all matches */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000629 m_l = new_entry->m_list;
630 j = 0;
631 while (m_l) {
632 m = (struct ebt_u_match *)(m_l->m);
633 m_l2 = u_e->m_list;
634 while (m_l2 && strcmp(m_l2->m->u.name, m->m->u.name))
635 m_l2 = m_l2->next;
636 if (!m_l2 || !m->compare(m->m, m_l2->m))
637 goto letscontinue;
638 j++;
639 m_l = m_l->next;
640 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000641 /* Now be sure they have the same nr of matches */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000642 k = 0;
643 m_l = u_e->m_list;
644 while (m_l) {
645 k++;
646 m_l = m_l->next;
647 }
648 if (j != k)
649 continue;
650
Bart De Schuymer6622a012005-01-19 21:09:05 +0000651 /* Compare all watchers */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000652 w_l = new_entry->w_list;
653 j = 0;
654 while (w_l) {
655 w = (struct ebt_u_watcher *)(w_l->w);
656 w_l2 = u_e->w_list;
657 while (w_l2 && strcmp(w_l2->w->u.name, w->w->u.name))
658 w_l2 = w_l2->next;
659 if (!w_l2 || !w->compare(w->w, w_l2->w))
660 goto letscontinue;
661 j++;
662 w_l = w_l->next;
663 }
664 k = 0;
665 w_l = u_e->w_list;
666 while (w_l) {
667 k++;
668 w_l = w_l->next;
669 }
670 if (j != k)
671 continue;
672 if (strcmp(t->t->u.name, u_e->t->u.name))
673 continue;
674 if (!t->compare(t->t, u_e->t))
675 continue;
676 return i;
Bart De Schuymeraef08942004-09-09 21:41:29 +0000677letscontinue:;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000678 }
679 return -1;
680}
681
682/* Add a rule, rule_nr is the rule to update
683 * rule_nr specifies where the rule should be inserted
684 * rule_nr > 0 : insert the rule right before the rule_nr'th rule
685 * (the first rule is rule 1)
686 * rule_nr < 0 : insert the rule right before the (n+rule_nr+1)'th rule,
Bart De Schuymer6622a012005-01-19 21:09:05 +0000687 * where n denotes the number of rules in the chain
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000688 * rule_nr == 0: add a new rule at the end of the chain
689 *
690 * This function expects the ebt_{match,watcher,target} members of new_entry
691 * to contain pointers to ebt_u_{match,watcher,target} and updates these
Bart De Schuymer6622a012005-01-19 21:09:05 +0000692 * pointers so that they point to ebt_{match,watcher,target}, before adding
693 * the rule to the chain. Don't free() the ebt_{match,watcher,target} after a
694 * successful call to ebt_add_rule() */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000695void ebt_add_rule(struct ebt_u_replace *replace, struct ebt_u_entry *new_entry,
696 int rule_nr)
697{
698 int i, j;
699 struct ebt_u_entry **u_e;
700 struct ebt_u_match_list *m_l;
701 struct ebt_u_watcher_list *w_l;
702 struct ebt_u_entries *entries = ebt_to_chain(replace);
703 struct ebt_cntchanges *cc = replace->counterchanges, *new_cc;
704 struct ebt_cntchanges **prev_cc = &(replace->counterchanges);
705
706 if (rule_nr <= 0)
707 rule_nr += entries->nentries;
708 else
709 rule_nr--;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000710 if (rule_nr > entries->nentries || rule_nr < 0) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000711 ebt_print_error("The specified rule number is incorrect");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000712 return;
713 }
714 /* We're adding one rule */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000715 replace->nentries++;
716 entries->nentries++;
717
Bart De Schuymer6622a012005-01-19 21:09:05 +0000718 /* Handle counter stuff */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000719 for (i = 0; i < replace->selected_chain; i++) {
720 if (i < NF_BR_NUMHOOKS && !(replace->valid_hooks & (1 << i)))
721 continue;
722 j = ebt_nr_to_chain(replace, i)->nentries;
723 while (j) {
724 if (cc->type != CNT_DEL)
725 j--;
726 prev_cc = &(cc->next);
727 cc = cc->next;
728 }
729 }
730 j = rule_nr;
731 while (j) {
732 if (cc->type != CNT_DEL)
733 j--;
734 prev_cc = &(cc->next);
735 cc = cc->next;
736 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000737 if (cc && cc->type == CNT_DEL)
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000738 cc->type = CNT_OWRITE;
739 else {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000740 new_cc = (struct ebt_cntchanges *)
741 malloc(sizeof(struct ebt_cntchanges));
Bart De Schuymer6622a012005-01-19 21:09:05 +0000742 if (!new_cc)
743 ebt_print_memory();
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000744 new_cc->type = CNT_ADD;
745 new_cc->next = cc;
746 *prev_cc = new_cc;
747 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000748 /* Go to the right position in the chain */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000749 u_e = &entries->entries;
750 for (i = 0; i < rule_nr; i++)
751 u_e = &(*u_e)->next;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000752 /* Insert the rule */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000753 new_entry->next = *u_e;
754 *u_e = new_entry;
755
Bart De Schuymer6622a012005-01-19 21:09:05 +0000756 /* Put the ebt_{match, watcher, target} pointers in place */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000757 m_l = new_entry->m_list;
758 while (m_l) {
759 m_l->m = ((struct ebt_u_match *)m_l->m)->m;
760 m_l = m_l->next;
761 }
762 w_l = new_entry->w_list;
763 while (w_l) {
764 w_l->w = ((struct ebt_u_watcher *)w_l->w)->w;
765 w_l = w_l->next;
766 }
767 new_entry->t = ((struct ebt_u_target *)new_entry->t)->t;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000768 /* Update the counter_offset of chains behind this one */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000769 i = replace->selected_chain;
770 while (1) {
771 i++;
772 entries = ebt_nr_to_chain(replace, i);
773 if (!entries) {
774 if (i < NF_BR_NUMHOOKS)
775 continue;
776 else
777 break;
778 } else
779 entries->counter_offset++;
780 }
781}
782
Bart De Schuymerff587202005-02-08 20:02:28 +0000783static int check_and_change_rule_number(struct ebt_u_replace *replace,
784 struct ebt_u_entry *new_entry, int *begin, int *end)
785{
786 struct ebt_u_entries *entries = ebt_to_chain(replace);
787
788 if (*begin < 0)
789 *begin += entries->nentries + 1;
790 if (*end < 0)
791 *end += entries->nentries + 1;
792
793 if (*begin < 0 || *begin > *end || *end > entries->nentries) {
794 ebt_print_error("Sorry, wrong rule numbers");
795 return -1;
796 }
797
798 if ((*begin * *end == 0) && (*begin + *end != 0))
799 ebt_print_bug("begin and end should be either both zero, "
800 "either both non-zero");
801 if (*begin != 0 && *end != 0) {
802 (*begin)--;
803 (*end)--;
804 } else {
805 *begin = ebt_check_rule_exists(replace, new_entry);
806 *end = *begin;
807 if (*begin == -1) {
808 ebt_print_error("Sorry, rule does not exist");
809 return -1;
810 }
811 }
812 return 0;
813}
814
Bart De Schuymer6622a012005-01-19 21:09:05 +0000815/* Delete a rule or rules
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000816 * begin == end == 0: delete the rule corresponding to new_entry
817 *
Bart De Schuymer6622a012005-01-19 21:09:05 +0000818 * The first rule has rule nr 1, the last rule has rule nr -1, etc.
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000819 * This function expects the ebt_{match,watcher,target} members of new_entry
Bart De Schuymer6622a012005-01-19 21:09:05 +0000820 * to contain pointers to ebt_u_{match,watcher,target}. */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000821void ebt_delete_rule(struct ebt_u_replace *replace,
822 struct ebt_u_entry *new_entry, int begin, int end)
823{
824 int i, j, nr_deletes;
825 struct ebt_u_entry **u_e, *u_e2;
826 struct ebt_u_entries *entries = ebt_to_chain(replace);
827 struct ebt_cntchanges *cc = replace->counterchanges;
828 struct ebt_cntchanges **prev_cc = &(replace->counterchanges);
829
Bart De Schuymerff587202005-02-08 20:02:28 +0000830 if (check_and_change_rule_number(replace, new_entry, &begin, &end))
Bart De Schuymer6622a012005-01-19 21:09:05 +0000831 return;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000832
Bart De Schuymer6622a012005-01-19 21:09:05 +0000833 /* We're deleting rules */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000834 nr_deletes = end - begin + 1;
835 replace->nentries -= nr_deletes;
836 entries->nentries -= nr_deletes;
837
Bart De Schuymer6622a012005-01-19 21:09:05 +0000838 /* Handle counter stuff */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000839 for (i = 0; i < replace->selected_chain; i++) {
840 if (i < NF_BR_NUMHOOKS && !(replace->valid_hooks & (1 << i)))
841 continue;
842 j = ebt_nr_to_chain(replace, i)->nentries;
843 while (j) {
844 if (cc->type != CNT_DEL)
845 j--;
846 prev_cc = &(cc->next);
847 cc = cc->next;
848 }
849 }
850 j = begin;
851 while (j) {
852 if (cc->type != CNT_DEL)
853 j--;
854 prev_cc = &(cc->next);
855 cc = cc->next;
856 }
857 j = nr_deletes;
858 while (j) {
859 if (cc->type != CNT_DEL) {
860 j--;
861 if (cc->type == CNT_ADD) {
862 *prev_cc = cc->next;
863 free(cc);
864 cc = *prev_cc;
865 continue;
866 }
867 cc->type = CNT_DEL;
868 }
869 prev_cc = &(cc->next);
870 cc = cc->next;
871 }
872
Bart De Schuymer6622a012005-01-19 21:09:05 +0000873 /* Go to the right position in the chain */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000874 u_e = &entries->entries;
875 for (j = 0; j < begin; j++)
876 u_e = &(*u_e)->next;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000877 /* Remove the rules */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000878 j = nr_deletes;
879 while(j--) {
880 u_e2 = *u_e;
881 *u_e = (*u_e)->next;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000882 /* Free everything */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000883 ebt_free_u_entry(u_e2);
884 free(u_e2);
885 }
886
Bart De Schuymer6622a012005-01-19 21:09:05 +0000887 /* Update the counter_offset of chains behind this one */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000888 j = replace->selected_chain;
889 while (1) {
890 j++;
891 entries = ebt_nr_to_chain(replace, j);
892 if (!entries) {
893 if (j < NF_BR_NUMHOOKS)
894 continue;
895 else
896 break;
897 } else
898 entries->counter_offset -= nr_deletes;
899 }
900}
901
Bart De Schuymerab611e22005-02-14 20:20:03 +0000902#define OPT_CNT_INCR 0x2000 /* This value is also defined in ebtables.c */
903#define OPT_CNT_DECR 0x4000 /* This value is also defined in ebtables.c */
Bart De Schuymerff587202005-02-08 20:02:28 +0000904/* Change the counters of a rule or rules
905 * begin == end == 0: change counters of the rule corresponding to new_entry
906 *
907 * The first rule has rule nr 1, the last rule has rule nr -1, etc.
908 * This function expects the ebt_{match,watcher,target} members of new_entry
909 * to contain pointers to ebt_u_{match,watcher,target}. */
910void ebt_change_counters(struct ebt_u_replace *replace,
911 struct ebt_u_entry *new_entry, int begin, int end,
912 struct ebt_counter *cnt)
913{
914 int i, j;
915 struct ebt_u_entry *u_e;
916 struct ebt_u_entries *entries = ebt_to_chain(replace);
917 struct ebt_cntchanges *cc = replace->counterchanges;
Bart De Schuymerff587202005-02-08 20:02:28 +0000918
919 if (check_and_change_rule_number(replace, new_entry, &begin, &end))
920 return;
921
922 for (i = 0; i < replace->selected_chain; i++) {
923 if (i < NF_BR_NUMHOOKS && !(replace->valid_hooks & (1 << i)))
924 continue;
925 j = ebt_nr_to_chain(replace, i)->nentries;
926 while (j) {
927 if (cc->type != CNT_DEL)
928 j--;
Bart De Schuymerff587202005-02-08 20:02:28 +0000929 cc = cc->next;
930 }
931 }
932 i = begin;
933 while (i) {
934 if (cc->type != CNT_DEL)
935 i--;
Bart De Schuymerff587202005-02-08 20:02:28 +0000936 cc = cc->next;
937 }
938 u_e = entries->entries;
939 for (i = 0; i < begin; i++)
940 u_e = u_e->next;
941 i = end - begin + 1;
Bart De Schuymerab611e22005-02-14 20:20:03 +0000942 while (i) {
943 if (cc->type != CNT_DEL) {
944 i--;
945 if (cc->type != CNT_ADD && cc->type != CNT_OWRITE) {
946 /* In daemon mode, we want to make sure we don't lose
947 * any counted packets/bytes (the kernel is still counting
948 * while we are changing things in userspace). Therefore,
949 * we need to add the amount to the counters in the kernel,
950 * not the counters copied earlier from the kernel. */
951 u_e->cnt_surplus = *cnt;
952 if (replace->flags & OPT_CNT_INCR) {
953 cc->type = CNT_INCR;
954 } else if (replace->flags & OPT_CNT_DECR)
955 cc->type = CNT_DECR;
956 else {
957 cc->type = CNT_CHANGE;
958 u_e->cnt = *cnt;
959 u_e->cnt_surplus.pcnt = u_e->cnt_surplus.bcnt = 0;
960 }
961 } else { /* A -C after a -A remains a -A */
962 if (replace->flags & OPT_CNT_INCR) {
963 u_e->cnt.pcnt += cnt->pcnt;
964 u_e->cnt.bcnt += cnt->bcnt;
965 } else if (replace->flags & OPT_CNT_DECR) {
966 u_e->cnt.pcnt -= cnt->pcnt;
967 u_e->cnt.bcnt -= cnt->bcnt;
968 } else
969 u_e->cnt = *cnt;
970 }
971 u_e = u_e->next;
972 }
973 cc = cc->next;
Bart De Schuymerff587202005-02-08 20:02:28 +0000974 }
975}
976
Bart De Schuymer6622a012005-01-19 21:09:05 +0000977/* Selected_chain == -1 : zero all counters
978 * Otherwise, zero the counters of selected_chain */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000979void ebt_zero_counters(struct ebt_u_replace *replace)
980{
981 struct ebt_u_entries *entries = ebt_to_chain(replace);
982 struct ebt_cntchanges *cc = replace->counterchanges;
Bart De Schuymer93f36ba2005-01-24 21:11:24 +0000983 struct ebt_u_entry *next;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000984 int i, j;
985
986 if (!entries) {
987 while (cc) {
988 if (cc->type == CNT_NORM)
989 cc->type = CNT_ZERO;
990 cc = cc->next;
991 }
Bart De Schuymer93f36ba2005-01-24 21:11:24 +0000992 i = -1;
993 while (1) {
994 i++;
995 if (i < NF_BR_NUMHOOKS && !(replace->valid_hooks & (1 << i)))
996 continue;
997 entries = ebt_nr_to_chain(replace, i);
998 if (!entries) {
999 if (i < NF_BR_NUMHOOKS)
1000 ebt_print_bug("i < NF_BR_NUMHOOKS");
1001 break;
1002 }
1003 next = entries->entries;
1004 while (next) {
1005 next->cnt.bcnt = next->cnt.pcnt = 0;
1006 next = next->next;
1007 }
1008 }
1009
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001010 } else {
Bart De Schuymer93f36ba2005-01-24 21:11:24 +00001011 next = entries->entries;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001012 if (entries->nentries == 0)
1013 return;
1014
1015 for (i = 0; i < replace->selected_chain; i++) {
Bart De Schuymer6622a012005-01-19 21:09:05 +00001016 if (i < NF_BR_NUMHOOKS && !(replace->valid_hooks & (1 << i)))
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001017 continue;
1018 j = ebt_nr_to_chain(replace, i)->nentries;
1019 while (j) {
1020 if (cc->type != CNT_DEL)
1021 j--;
1022 cc = cc->next;
1023 }
1024 }
1025 j = entries->nentries;
1026 while (j) {
1027 if (cc->type != CNT_DEL) {
1028 j--;
1029 if (cc->type == CNT_NORM)
1030 cc->type = CNT_ZERO;
1031 }
1032 cc = cc->next;
1033 }
Bart De Schuymer93f36ba2005-01-24 21:11:24 +00001034 while (next) {
1035 next->cnt.bcnt = next->cnt.pcnt = 0;
1036 next = next->next;
1037 }
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001038 }
1039}
1040
Bart De Schuymer6622a012005-01-19 21:09:05 +00001041/* Add a new chain and specify its policy */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001042void ebt_new_chain(struct ebt_u_replace *replace, const char *name, int policy)
1043{
1044 struct ebt_u_chain_list *cl, **cl2;
1045
Bart De Schuymer6622a012005-01-19 21:09:05 +00001046 if (ebt_get_chainnr(replace, name) != -1) {
Bart De Schuymer64182a32004-01-21 20:39:54 +00001047 ebt_print_error("Chain %s already exists", optarg);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001048 return;
1049 } else if (ebt_find_target(name)) {
Bart De Schuymer64182a32004-01-21 20:39:54 +00001050 ebt_print_error("Target with name %s exists", optarg);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001051 return;
1052 } else if (strlen(optarg) >= EBT_CHAIN_MAXNAMELEN) {
Bart De Schuymer64182a32004-01-21 20:39:54 +00001053 ebt_print_error("Chain name length can't exceed %d",
1054 EBT_CHAIN_MAXNAMELEN - 1);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001055 return;
1056 }
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001057 cl = (struct ebt_u_chain_list *)
1058 malloc(sizeof(struct ebt_u_chain_list));
1059 if (!cl)
Bart De Schuymer64182a32004-01-21 20:39:54 +00001060 ebt_print_memory();
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001061 cl->next = NULL;
1062 cl->udc = (struct ebt_u_entries *)
1063 malloc(sizeof(struct ebt_u_entries));
1064 if (!cl->udc)
Bart De Schuymer64182a32004-01-21 20:39:54 +00001065 ebt_print_memory();
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001066 cl->udc->nentries = 0;
1067 cl->udc->policy = policy;
1068 cl->udc->counter_offset = replace->nentries;
1069 cl->udc->hook_mask = 0;
1070 strcpy(cl->udc->name, name);
1071 cl->udc->entries = NULL;
1072 cl->kernel_start = NULL;
Bart De Schuymer6622a012005-01-19 21:09:05 +00001073 /* Put the new chain at the end */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001074 cl2 = &(replace->udc);
1075 while (*cl2)
1076 cl2 = &((*cl2)->next);
1077 *cl2 = cl;
1078}
1079
Bart De Schuymer6622a012005-01-19 21:09:05 +00001080/* Selected_chain == -1: delete all non-referenced udc
1081 * selected_chain < NF_BR_NUMHOOKS is illegal */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001082void ebt_delete_chain(struct ebt_u_replace *replace)
1083{
Bart De Schuymer6622a012005-01-19 21:09:05 +00001084 int chain_nr = replace->selected_chain, print_error = 1;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001085
1086 if (chain_nr != -1 && chain_nr < NF_BR_NUMHOOKS)
Bart De Schuymer64182a32004-01-21 20:39:54 +00001087 ebt_print_bug("You can't remove a standard chain");
Bart De Schuymer6622a012005-01-19 21:09:05 +00001088 if (chain_nr == -1) {
1089 print_error = 0;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001090 replace->selected_chain = NF_BR_NUMHOOKS;
Bart De Schuymer6622a012005-01-19 21:09:05 +00001091 }
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001092 do {
1093 if (ebt_to_chain(replace) == NULL) {
1094 if (chain_nr == -1)
1095 break;
Bart De Schuymer64182a32004-01-21 20:39:54 +00001096 ebt_print_bug("udc nr %d doesn't exist", chain_nr);
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001097 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001098 /* If the chain is referenced, don't delete it,
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001099 * also decrement jumps to a chain behind the
Bart De Schuymer6622a012005-01-19 21:09:05 +00001100 * one we're deleting */
1101 if (ebt_check_for_references(replace, print_error)) {
1102 if (chain_nr != -1)
1103 break;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001104 replace->selected_chain++;
1105 continue;
1106 }
1107 decrease_chain_jumps(replace);
1108 ebt_flush_chains(replace);
1109 remove_udc(replace);
Bart De Schuymerc6c0dc32004-12-16 19:30:32 +00001110 } while (chain_nr == -1);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001111 replace->selected_chain = chain_nr; /* Put back to -1 */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001112}
1113
Bart De Schuymer6622a012005-01-19 21:09:05 +00001114/* Rename an existing chain. */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001115void ebt_rename_chain(struct ebt_u_replace *replace, const char *name)
1116{
1117 struct ebt_u_entries *entries = ebt_to_chain(replace);
1118
1119 if (!entries)
Bart De Schuymer64182a32004-01-21 20:39:54 +00001120 ebt_print_bug("ebt_rename_chain: entries == NULL");
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001121 strcpy(entries->name, name);
1122}
1123
1124
1125 /*
1126*************************
1127*************************
1128**SPECIALIZED*FUNCTIONS**
1129*************************
1130*************************
1131 */
1132
1133
Bart De Schuymer6622a012005-01-19 21:09:05 +00001134/* Executes the final_check() function for all extensions used by the rule
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001135 * ebt_check_for_loops should have been executed earlier, to make sure the
Bart De Schuymer6622a012005-01-19 21:09:05 +00001136 * hook_mask is correct. The time argument to final_check() is set to 1,
1137 * meaning it's the second time the final_check() function is executed. */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001138void ebt_do_final_checks(struct ebt_u_replace *replace, struct ebt_u_entry *e,
1139 struct ebt_u_entries *entries)
1140{
1141 struct ebt_u_match_list *m_l;
1142 struct ebt_u_watcher_list *w_l;
1143 struct ebt_u_target *t;
1144 struct ebt_u_match *m;
1145 struct ebt_u_watcher *w;
1146
1147 m_l = e->m_list;
1148 w_l = e->w_list;
1149 while (m_l) {
1150 m = ebt_find_match(m_l->m->u.name);
1151 m->final_check(e, m_l->m, replace->name,
1152 entries->hook_mask, 1);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001153 if (ebt_errormsg[0] != '\0')
1154 return;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001155 m_l = m_l->next;
1156 }
1157 while (w_l) {
1158 w = ebt_find_watcher(w_l->w->u.name);
1159 w->final_check(e, w_l->w, replace->name,
1160 entries->hook_mask, 1);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001161 if (ebt_errormsg[0] != '\0')
1162 return;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001163 w_l = w_l->next;
1164 }
1165 t = ebt_find_target(e->t->u.name);
1166 t->final_check(e, e->t, replace->name,
1167 entries->hook_mask, 1);
1168}
1169
Bart De Schuymer6622a012005-01-19 21:09:05 +00001170/* Returns 1 (if it returns) when the chain is referenced, 0 when it isn't.
1171 * print_err: 0 (resp. 1) = don't (resp. do) print error when referenced */
1172int ebt_check_for_references(struct ebt_u_replace *replace, int print_err)
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001173{
Bart De Schuymer6622a012005-01-19 21:09:05 +00001174 if (print_err)
1175 return iterate_entries(replace, 1);
1176 else
1177 return iterate_entries(replace, 2);
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001178}
1179
Bart De Schuymer6622a012005-01-19 21:09:05 +00001180/* chain_nr: nr of the udc (>= NF_BR_NUMHOOKS)
1181 * Returns 1 (if it returns) when the chain is referenced, 0 when it isn't.
1182 * print_err: 0 (resp. 1) = don't (resp. do) print error when referenced */
1183int ebt_check_for_references2(struct ebt_u_replace *replace, int chain_nr,
1184 int print_err)
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001185{
1186 int tmp = replace->selected_chain, ret;
1187
1188 replace->selected_chain = chain_nr;
Bart De Schuymer6622a012005-01-19 21:09:05 +00001189 if (print_err)
1190 ret = iterate_entries(replace, 1);
1191 else
1192 ret = iterate_entries(replace, 2);
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001193 replace->selected_chain = tmp;
1194 return ret;
1195}
1196
1197struct ebt_u_stack
1198{
1199 int chain_nr;
1200 int n;
1201 struct ebt_u_entry *e;
1202 struct ebt_u_entries *entries;
1203};
1204
Bart De Schuymer6622a012005-01-19 21:09:05 +00001205/* Checks for loops
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001206 * As a by-product, the hook_mask member of each chain is filled in
1207 * correctly. The check functions of the extensions need this hook_mask
Bart De Schuymer6622a012005-01-19 21:09:05 +00001208 * to know from which standard chains they can be called. */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001209void ebt_check_for_loops(struct ebt_u_replace *replace)
1210{
1211 int chain_nr , i, j , k, sp = 0, verdict;
1212 struct ebt_u_entries *entries, *entries2;
1213 struct ebt_u_stack *stack = NULL;
1214 struct ebt_u_entry *e;
1215
1216 i = -1;
Bart De Schuymer6622a012005-01-19 21:09:05 +00001217 /* Initialize hook_mask to 0 */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001218 while (1) {
1219 i++;
1220 if (i < NF_BR_NUMHOOKS && !(replace->valid_hooks & (1 << i)))
1221 continue;
1222 entries = ebt_nr_to_chain(replace, i);
1223 if (!entries)
1224 break;
1225 entries->hook_mask = 0;
1226 }
1227 if (i > NF_BR_NUMHOOKS) {
1228 stack = (struct ebt_u_stack *)malloc((i - NF_BR_NUMHOOKS) *
1229 sizeof(struct ebt_u_stack));
1230 if (!stack)
Bart De Schuymer64182a32004-01-21 20:39:54 +00001231 ebt_print_memory();
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001232 }
1233
Bart De Schuymer6622a012005-01-19 21:09:05 +00001234 /* Check for loops, starting from every base chain */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001235 for (i = 0; i < NF_BR_NUMHOOKS; i++) {
1236 if (!(replace->valid_hooks & (1 << i)))
1237 continue;
1238 entries = ebt_nr_to_chain(replace, i);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001239 /* (1 << NF_BR_NUMHOOKS) implies it's a standard chain
1240 * (usefull in the final_check() funtions) */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001241 entries->hook_mask = (1 << i) | (1 << NF_BR_NUMHOOKS);
1242 chain_nr = i;
1243
1244 e = entries->entries;
1245 for (j = 0; j < entries->nentries; j++) {
1246 if (strcmp(e->t->u.name, EBT_STANDARD_TARGET))
1247 goto letscontinue;
1248 verdict = ((struct ebt_standard_target *)(e->t))->verdict;
1249 if (verdict < 0)
1250 goto letscontinue;
Bart De Schuymer6622a012005-01-19 21:09:05 +00001251 entries2 = ebt_nr_to_chain(replace, verdict + NF_BR_NUMHOOKS);
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001252 entries2->hook_mask |= entries->hook_mask;
Bart De Schuymer6622a012005-01-19 21:09:05 +00001253 /* Now see if we've been here before */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001254 for (k = 0; k < sp; k++)
Bart De Schuymer6622a012005-01-19 21:09:05 +00001255 if (stack[k].chain_nr == verdict + NF_BR_NUMHOOKS) {
1256 ebt_print_error("Loop from chain '%s' to chain '%s'",
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001257 ebt_nr_to_chain(replace, chain_nr)->name,
1258 ebt_nr_to_chain(replace, stack[k].chain_nr)->name);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001259 goto free_stack;
1260 }
1261 /* Jump to the chain, make sure we know how to get back */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001262 stack[sp].chain_nr = chain_nr;
1263 stack[sp].n = j;
1264 stack[sp].entries = entries;
1265 stack[sp].e = e;
1266 sp++;
1267 j = -1;
1268 e = entries2->entries;
1269 chain_nr = verdict + NF_BR_NUMHOOKS;
1270 entries = entries2;
1271 continue;
1272letscontinue:
1273 e = e->next;
1274 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001275 /* We are at the end of a standard chain */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001276 if (sp == 0)
1277 continue;
Bart De Schuymer6622a012005-01-19 21:09:05 +00001278 /* Go back to the chain one level higher */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001279 sp--;
1280 j = stack[sp].n;
1281 chain_nr = stack[sp].chain_nr;
1282 e = stack[sp].e;
1283 entries = stack[sp].entries;
1284 goto letscontinue;
1285 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001286free_stack:
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001287 free(stack);
1288 return;
1289}
1290
Bart De Schuymer6622a012005-01-19 21:09:05 +00001291/* The user will use the match, so put it in new_entry. The ebt_u_match
1292 * pointer is put in the ebt_entry_match pointer. ebt_add_rule will
1293 * fill in the final value for new->m. Unless the rule is added to a chain,
1294 * the pointer will keep pointing to the ebt_u_match (until the new_entry
1295 * is freed). I know, I should use a union for these 2 pointer types... */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001296void ebt_add_match(struct ebt_u_entry *new_entry, struct ebt_u_match *m)
1297{
1298 struct ebt_u_match_list **m_list, *new;
1299
1300 for (m_list = &new_entry->m_list; *m_list; m_list = &(*m_list)->next);
1301 new = (struct ebt_u_match_list *)
1302 malloc(sizeof(struct ebt_u_match_list));
1303 if (!new)
Bart De Schuymer64182a32004-01-21 20:39:54 +00001304 ebt_print_memory();
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001305 *m_list = new;
1306 new->next = NULL;
1307 new->m = (struct ebt_entry_match *)m;
1308}
1309
1310void ebt_add_watcher(struct ebt_u_entry *new_entry, struct ebt_u_watcher *w)
1311{
1312 struct ebt_u_watcher_list **w_list;
1313 struct ebt_u_watcher_list *new;
1314
1315 for (w_list = &new_entry->w_list; *w_list; w_list = &(*w_list)->next);
1316 new = (struct ebt_u_watcher_list *)
1317 malloc(sizeof(struct ebt_u_watcher_list));
1318 if (!new)
Bart De Schuymer64182a32004-01-21 20:39:54 +00001319 ebt_print_memory();
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001320 *w_list = new;
1321 new->next = NULL;
1322 new->w = (struct ebt_entry_watcher *)w;
1323}
1324
1325
1326 /*
1327*******************
1328*******************
1329**OTHER*FUNCTIONS**
1330*******************
1331*******************
1332 */
1333
1334
Bart De Schuymer6622a012005-01-19 21:09:05 +00001335/* type = 0 => update chain jumps
1336 * type = 1 => check for reference, print error when referenced
1337 * type = 2 => check for reference, don't print error when referenced
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001338 *
Bart De Schuymer6622a012005-01-19 21:09:05 +00001339 * Returns 1 when type == 1 and the chain is referenced
1340 * returns 0 otherwise */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001341static int iterate_entries(struct ebt_u_replace *replace, int type)
1342{
1343 int i = -1, j, chain_nr = replace->selected_chain - NF_BR_NUMHOOKS;
1344 struct ebt_u_entries *entries;
1345 struct ebt_u_entry *e;
1346
1347 if (chain_nr < 0)
Bart De Schuymer64182a32004-01-21 20:39:54 +00001348 ebt_print_bug("iterate_entries: udc = %d < 0", chain_nr);
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001349 while (1) {
1350 i++;
1351 entries = ebt_nr_to_chain(replace, i);
1352 if (!entries) {
1353 if (i < NF_BR_NUMHOOKS)
1354 continue;
1355 else
1356 break;
1357 }
1358 e = entries->entries;
1359 j = 0;
1360 while (e) {
1361 int chain_jmp;
1362
1363 j++;
1364 if (strcmp(e->t->u.name, EBT_STANDARD_TARGET)) {
1365 e = e->next;
1366 continue;
1367 }
Bart De Schuymer64182a32004-01-21 20:39:54 +00001368 chain_jmp = ((struct ebt_standard_target *)e->t)->
1369 verdict;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001370 switch (type) {
1371 case 1:
Bart De Schuymer6622a012005-01-19 21:09:05 +00001372 case 2:
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001373 if (chain_jmp == chain_nr) {
Bart De Schuymer6622a012005-01-19 21:09:05 +00001374 if (type == 2)
1375 return 1;
1376 ebt_print_error("Can't delete the chain '%s', it's referenced in chain '%s', rule %d",
1377 ebt_nr_to_chain(replace, chain_nr + NF_BR_NUMHOOKS)->name, entries->name, j);
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001378 return 1;
1379 }
1380 break;
1381 case 0:
Bart De Schuymer6622a012005-01-19 21:09:05 +00001382 /* Adjust the chain jumps when necessary */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001383 if (chain_jmp > chain_nr)
1384 ((struct ebt_standard_target *)e->t)->verdict--;
1385 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +00001386 } /* End switch */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001387 e = e->next;
1388 }
1389 }
1390 return 0;
1391}
1392
1393static void decrease_chain_jumps(struct ebt_u_replace *replace)
1394{
1395 iterate_entries(replace, 0);
1396}
1397
Bart De Schuymer6622a012005-01-19 21:09:05 +00001398/* Selected_chain >= NF_BR_NUMHOOKS */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001399static void remove_udc(struct ebt_u_replace *replace)
1400{
1401 struct ebt_u_chain_list *cl, **cl2;
1402 struct ebt_u_entries *entries;
1403 struct ebt_u_entry *u_e, *tmp;
1404 int chain_nr = replace->selected_chain;
1405
1406 if (chain_nr < NF_BR_NUMHOOKS)
Bart De Schuymer64182a32004-01-21 20:39:54 +00001407 ebt_print_bug("remove_udc: chain_nr = %d < %d", chain_nr,
1408 NF_BR_NUMHOOKS);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001409 /* First free the rules */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001410 entries = ebt_nr_to_chain(replace, chain_nr);
1411 u_e = entries->entries;
1412 while (u_e) {
1413 ebt_free_u_entry(u_e);
1414 tmp = u_e->next;
1415 free(u_e);
1416 u_e = tmp;
1417 }
1418
1419 /* next, remove the chain */
1420 cl2 = &(replace->udc);
1421 while ((*cl2)->udc != entries)
1422 cl2 = &((*cl2)->next);
1423 cl = (*cl2);
1424 (*cl2) = (*cl2)->next;
1425 free(cl->udc);
1426 free(cl);
1427}
1428
Bart De Schuymer6622a012005-01-19 21:09:05 +00001429/* Used in initialization code of modules */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001430void ebt_register_match(struct ebt_u_match *m)
1431{
1432 int size = EBT_ALIGN(m->size) + sizeof(struct ebt_entry_match);
1433 struct ebt_u_match **i;
1434
1435 m->m = (struct ebt_entry_match *)malloc(size);
1436 if (!m->m)
Bart De Schuymer64182a32004-01-21 20:39:54 +00001437 ebt_print_memory();
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001438 strcpy(m->m->u.name, m->name);
1439 m->m->match_size = EBT_ALIGN(m->size);
1440 m->init(m->m);
1441
1442 for (i = &ebt_matches; *i; i = &((*i)->next));
1443 m->next = NULL;
1444 *i = m;
1445}
1446
1447void ebt_register_watcher(struct ebt_u_watcher *w)
1448{
1449 int size = EBT_ALIGN(w->size) + sizeof(struct ebt_entry_watcher);
1450 struct ebt_u_watcher **i;
1451
1452 w->w = (struct ebt_entry_watcher *)malloc(size);
1453 if (!w->w)
Bart De Schuymer64182a32004-01-21 20:39:54 +00001454 ebt_print_memory();
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001455 strcpy(w->w->u.name, w->name);
1456 w->w->watcher_size = EBT_ALIGN(w->size);
1457 w->init(w->w);
1458
1459 for (i = &ebt_watchers; *i; i = &((*i)->next));
1460 w->next = NULL;
1461 *i = w;
1462}
1463
1464void ebt_register_target(struct ebt_u_target *t)
1465{
1466 int size = EBT_ALIGN(t->size) + sizeof(struct ebt_entry_target);
1467 struct ebt_u_target **i;
1468
1469 t->t = (struct ebt_entry_target *)malloc(size);
1470 if (!t->t)
Bart De Schuymer64182a32004-01-21 20:39:54 +00001471 ebt_print_memory();
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001472 strcpy(t->t->u.name, t->name);
1473 t->t->target_size = EBT_ALIGN(t->size);
1474 t->init(t->t);
1475
1476 for (i = &ebt_targets; *i; i = &((*i)->next));
1477 t->next = NULL;
1478 *i = t;
1479}
1480
1481void ebt_register_table(struct ebt_u_table *t)
1482{
1483 t->next = ebt_tables;
1484 ebt_tables = t;
1485}
1486
1487void ebt_iterate_matches(void (*f)(struct ebt_u_match *))
1488{
1489 struct ebt_u_match *i;
1490
1491 for (i = ebt_matches; i; i = i->next)
1492 f(i);
1493}
1494
1495void ebt_iterate_watchers(void (*f)(struct ebt_u_watcher *))
1496{
1497 struct ebt_u_watcher *i;
1498
1499 for (i = ebt_watchers; i; i = i->next)
1500 f(i);
1501}
1502
1503void ebt_iterate_targets(void (*f)(struct ebt_u_target *))
1504{
1505 struct ebt_u_target *i;
1506
1507 for (i = ebt_targets; i; i = i->next)
1508 f(i);
1509}
1510
Bart De Schuymer6622a012005-01-19 21:09:05 +00001511/* Don't use this function, use ebt_print_bug() */
Bart De Schuymer64182a32004-01-21 20:39:54 +00001512void __ebt_print_bug(char *file, int line, char *format, ...)
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001513{
1514 va_list l;
1515
1516 va_start(l, format);
1517 printf(PROGNAME" v"PROGVERSION":%s:%d:--BUG--: \n", file, line);
1518 vprintf(format, l);
1519 printf("\n");
1520 va_end(l);
1521 exit (-1);
1522}
1523
Bart De Schuymer6622a012005-01-19 21:09:05 +00001524/* The error messages are put in here when ebt_silent == 1
1525 * ebt_errormsg[0] == '\0' implies there was no error */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001526char ebt_errormsg[ERRORMSG_MAXLEN];
Bart De Schuymer6622a012005-01-19 21:09:05 +00001527/* When error messages should not be printed on the screen, after which
1528 * the program exit()s, set ebt_silent to 1. */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001529int ebt_silent;
Bart De Schuymer6622a012005-01-19 21:09:05 +00001530/* Don't use this function, use ebt_print_error() */
Bart De Schuymer64182a32004-01-21 20:39:54 +00001531void __ebt_print_error(char *format, ...)
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001532{
1533 va_list l;
1534
1535 va_start(l, format);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001536 if (ebt_silent && ebt_errormsg[0] == '\0') {
1537 vsnprintf(ebt_errormsg, ERRORMSG_MAXLEN, format, l);
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001538 va_end(l);
1539 } else {
1540 vprintf(format, l);
Bart De Schuymer87889eb2005-01-24 22:25:27 +00001541 printf(".\n");
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001542 va_end(l);
1543 exit (-1);
1544 }
1545}