blob: b558c9c15bd07a890bb07b248fd8875390effb06 [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);
Bart De Schuymer80c82bb2004-01-14 20:06:44 +000037static int iterate_entries(struct ebt_u_replace *replace, int type);
38
Bart De Schuymer6622a012005-01-19 21:09:05 +000039/* The standard names */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +000040const char *ebt_hooknames[NF_BR_NUMHOOKS] =
41{
42 [NF_BR_PRE_ROUTING]"PREROUTING",
43 [NF_BR_LOCAL_IN]"INPUT",
44 [NF_BR_FORWARD]"FORWARD",
45 [NF_BR_LOCAL_OUT]"OUTPUT",
46 [NF_BR_POST_ROUTING]"POSTROUTING",
47 [NF_BR_BROUTING]"BROUTING"
48};
49
Bart De Schuymer6622a012005-01-19 21:09:05 +000050/* The four target names */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +000051const char* ebt_standard_targets[NUM_STANDARD_TARGETS] =
52{
53 "ACCEPT",
54 "DROP",
55 "CONTINUE",
56 "RETURN",
57};
58
Bart De Schuymer6622a012005-01-19 21:09:05 +000059/* The lists of supported tables, matches, watchers and targets */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +000060struct ebt_u_table *ebt_tables;
61struct ebt_u_match *ebt_matches;
62struct ebt_u_watcher *ebt_watchers;
63struct ebt_u_target *ebt_targets;
64
Bart De Schuymer6622a012005-01-19 21:09:05 +000065/* Find the right structure belonging to a name */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +000066struct ebt_u_target *ebt_find_target(const char *name)
67{
68 struct ebt_u_target *t = ebt_targets;
69
Bart De Schuymer6622a012005-01-19 21:09:05 +000070 while (t && strcmp(t->name, name))
Bart De Schuymer80c82bb2004-01-14 20:06:44 +000071 t = t->next;
72 return t;
73}
74
75struct ebt_u_match *ebt_find_match(const char *name)
76{
77 struct ebt_u_match *m = ebt_matches;
78
Bart De Schuymer6622a012005-01-19 21:09:05 +000079 while (m && strcmp(m->name, name))
Bart De Schuymer80c82bb2004-01-14 20:06:44 +000080 m = m->next;
81 return m;
82}
83
84struct ebt_u_watcher *ebt_find_watcher(const char *name)
85{
86 struct ebt_u_watcher *w = ebt_watchers;
87
Bart De Schuymer6622a012005-01-19 21:09:05 +000088 while (w && strcmp(w->name, name))
Bart De Schuymer80c82bb2004-01-14 20:06:44 +000089 w = w->next;
90 return w;
91}
92
93struct ebt_u_table *ebt_find_table(const char *name)
94{
95 struct ebt_u_table *t = ebt_tables;
96
97 while (t && strcmp(t->name, name))
98 t = t->next;
99 return t;
100}
101
Bart De Schuymer6622a012005-01-19 21:09:05 +0000102/* Prints all registered extensions */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000103void ebt_list_extensions()
104{
105 struct ebt_u_table *tbl = ebt_tables;
106 struct ebt_u_target *t = ebt_targets;
107 struct ebt_u_match *m = ebt_matches;
108 struct ebt_u_watcher *w = ebt_watchers;
109
110 PRINT_VERSION;
Bart De Schuymerd5dc87d2005-07-16 22:35:32 +0000111 printf("Loaded userspace extensions:\n\nLoaded tables:\n");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000112 while (tbl) {
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000113 printf("%s\n", tbl->name);
114 tbl = tbl->next;
115 }
Bart De Schuymerd5dc87d2005-07-16 22:35:32 +0000116 printf("\nLoaded targets:\n");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000117 while (t) {
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000118 printf("%s\n", t->name);
119 t = t->next;
120 }
Bart De Schuymerd5dc87d2005-07-16 22:35:32 +0000121 printf("\nLoaded matches:\n");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000122 while (m) {
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000123 printf("%s\n", m->name);
124 m = m->next;
125 }
Bart De Schuymerd5dc87d2005-07-16 22:35:32 +0000126 printf("\nLoaded watchers:\n");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000127 while (w) {
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000128 printf("%s\n", w->name);
129 w = w->next;
130 }
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000131}
132
Bart De Schuymer6622a012005-01-19 21:09:05 +0000133/* Get the table from the kernel or from a binary file
Bart De Schuymer64182a32004-01-21 20:39:54 +0000134 * init: 1 = ask the kernel for the initial contents of a table, i.e. the
135 * way it looks when the table is insmod'ed
Bart De Schuymer6622a012005-01-19 21:09:05 +0000136 * 0 = get the current data in the table */
137int ebt_get_kernel_table(struct ebt_u_replace *replace, int init)
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000138{
Bart De Schuymer6622a012005-01-19 21:09:05 +0000139 if (!ebt_find_table(replace->name)) {
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000140 ebt_print_error("Bad table name '%s'", replace->name);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000141 return -1;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000142 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000143 /* Get the kernel's information */
144 if (ebt_get_table(replace, init)) {
145 if (ebt_errormsg[0] != '\0')
146 return -1;
147 ebtables_insmod("ebtables");
148 if (ebt_get_table(replace, init)) {
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000149 ebt_print_error("The kernel doesn't support the ebtables '%s' table", replace->name);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000150 return -1;
151 }
152 }
153 return 0;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000154}
155
Bart De Schuymer6622a012005-01-19 21:09:05 +0000156/* Put sane values into a new entry */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000157void ebt_initialize_entry(struct ebt_u_entry *e)
158{
159 e->bitmask = EBT_NOPROTO;
160 e->invflags = 0;
161 e->ethproto = 0;
162 strcpy(e->in, "");
163 strcpy(e->out, "");
164 strcpy(e->logical_in, "");
165 strcpy(e->logical_out, "");
166 e->m_list = NULL;
167 e->w_list = NULL;
168 e->t = (struct ebt_entry_target *)ebt_find_target(EBT_STANDARD_TARGET);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000169 ebt_find_target(EBT_STANDARD_TARGET)->used = 1;
Bart De Schuymerab611e22005-02-14 20:20:03 +0000170 e->cnt.pcnt = e->cnt.bcnt = e->cnt_surplus.pcnt = e->cnt_surplus.bcnt = 0;
Bart De Schuymer64182a32004-01-21 20:39:54 +0000171
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000172 if (!e->t)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000173 ebt_print_bug("Couldn't load standard target");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000174 ((struct ebt_standard_target *)((struct ebt_u_target *)e->t)->t)->verdict = EBT_CONTINUE;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000175}
176
Bart De Schuymer6622a012005-01-19 21:09:05 +0000177/* Free up the memory of the table held in userspace, *replace can be reused */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000178void ebt_cleanup_replace(struct ebt_u_replace *replace)
179{
180 int i;
181 struct ebt_u_entries *entries;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000182 struct ebt_cntchanges *cc1, *cc2;
183 struct ebt_u_entry *u_e1, *u_e2;
184
185 replace->name[0] = '\0';
186 replace->valid_hooks = 0;
187 replace->nentries = 0;
188 replace->num_counters = 0;
189 replace->flags = 0;
190 replace->command = 0;
191 replace->selected_chain = -1;
Bart De Schuymerd5dc87d2005-07-16 22:35:32 +0000192 free(replace->filename);
193 replace->filename = NULL;
194 free(replace->counters);
195 replace->counters = NULL;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000196
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000197 for (i = 0; i < replace->num_chains; i++) {
198 if (!(entries = replace->chains[i]))
199 continue;
Bart De Schuymere94eaf72005-08-28 16:06:22 +0000200 u_e1 = entries->entries->next;
201 while (u_e1 != entries->entries) {
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000202 ebt_free_u_entry(u_e1);
203 u_e2 = u_e1->next;
204 free(u_e1);
205 u_e1 = u_e2;
206 }
Bart De Schuymere94eaf72005-08-28 16:06:22 +0000207 free(entries->entries);
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000208 free(entries);
209 replace->chains[i] = NULL;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000210 }
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000211 cc1 = replace->cc->next;
212 while (cc1 != replace->cc) {
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000213 cc2 = cc1->next;
Bart De Schuymerd5dc87d2005-07-16 22:35:32 +0000214 free(cc1);
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000215 cc1 = cc2;
216 }
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000217 replace->cc->next = replace->cc->prev = replace->cc;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000218}
219
Bart De Schuymer6622a012005-01-19 21:09:05 +0000220/* Should be called, e.g., between 2 rule adds */
221void ebt_reinit_extensions()
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000222{
223 struct ebt_u_match *m;
224 struct ebt_u_watcher *w;
225 struct ebt_u_target *t;
Bart De Schuymer64182a32004-01-21 20:39:54 +0000226 int size;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000227
228 /* The init functions should determine by themselves whether they are
229 * called for the first time or not (when necessary). */
230 for (m = ebt_matches; m; m = m->next) {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000231 if (m->used) {
Bart De Schuymerd5dc87d2005-07-16 22:35:32 +0000232 size = EBT_ALIGN(m->size) + sizeof(struct ebt_entry_match);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000233 m->m = (struct ebt_entry_match *)malloc(size);
234 if (!m->m)
235 ebt_print_memory();
236 strcpy(m->m->u.name, m->name);
237 m->m->match_size = EBT_ALIGN(m->size);
238 m->used = 0;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000239 }
Bart De Schuymerff587202005-02-08 20:02:28 +0000240 m->flags = 0; /* An error can occur before used is set, while flags is changed. */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000241 m->init(m->m);
242 }
243 for (w = ebt_watchers; w; w = w->next) {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000244 if (w->used) {
Bart De Schuymerd5dc87d2005-07-16 22:35:32 +0000245 size = EBT_ALIGN(w->size) + sizeof(struct ebt_entry_watcher);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000246 w->w = (struct ebt_entry_watcher *)malloc(size);
247 if (!w->w)
248 ebt_print_memory();
249 strcpy(w->w->u.name, w->name);
250 w->w->watcher_size = EBT_ALIGN(w->size);
251 w->used = 0;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000252 }
Bart De Schuymerff587202005-02-08 20:02:28 +0000253 w->flags = 0;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000254 w->init(w->w);
255 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000256 for (t = ebt_targets; t; t = t->next) {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000257 if (t->used) {
Bart De Schuymerd5dc87d2005-07-16 22:35:32 +0000258 size = EBT_ALIGN(t->size) + sizeof(struct ebt_entry_target);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000259 t->t = (struct ebt_entry_target *)malloc(size);
260 if (!t->t)
261 ebt_print_memory();
262 strcpy(t->t->u.name, t->name);
263 t->t->target_size = EBT_ALIGN(t->size);
264 t->used = 0;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000265 }
Bart De Schuymerff587202005-02-08 20:02:28 +0000266 t->flags = 0;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000267 t->init(t->t);
268 }
269}
270
Bart De Schuymer6622a012005-01-19 21:09:05 +0000271/* This doesn't free e, because the calling function might need e->next */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000272void ebt_free_u_entry(struct ebt_u_entry *e)
273{
274 struct ebt_u_match_list *m_l, *m_l2;
275 struct ebt_u_watcher_list *w_l, *w_l2;
276
277 m_l = e->m_list;
278 while (m_l) {
279 m_l2 = m_l->next;
280 free(m_l->m);
281 free(m_l);
282 m_l = m_l2;
283 }
284 w_l = e->w_list;
285 while (w_l) {
286 w_l2 = w_l->next;
287 free(w_l->w);
288 free(w_l);
289 w_l = w_l2;
290 }
291 free(e->t);
292}
293
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000294static char *get_modprobe(void)
295{
296 int procfile;
297 char *ret;
298
299 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
300 if (procfile < 0)
301 return NULL;
302
303 ret = malloc(1024);
304 if (ret) {
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000305 if (read(procfile, ret, 1024) == -1)
306 goto fail;
Bart De Schuymerd5dc87d2005-07-16 22:35:32 +0000307 /* The kernel adds a '\n' */
308 ret[1023] = '\n';
309 *strchr(ret, '\n') = '\0';
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000310 close(procfile);
311 return ret;
312 }
313 fail:
314 free(ret);
315 close(procfile);
316 return NULL;
317}
318
319char *ebt_modprobe;
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000320/* Try to load the kernel module, analogous to ip_tables.c */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000321int ebtables_insmod(const char *modname)
322{
323 char *buf = NULL;
324 char *argv[3];
325
Bart De Schuymerd5dc87d2005-07-16 22:35:32 +0000326 /* If they don't explicitly set it, read out of /proc */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000327 if (!ebt_modprobe) {
328 buf = get_modprobe();
329 if (!buf)
330 return -1;
Bart De Schuymerd5dc87d2005-07-16 22:35:32 +0000331 ebt_modprobe = buf; /* Keep the value for possible later use */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000332 }
333
334 switch (fork()) {
335 case 0:
336 argv[0] = (char *)ebt_modprobe;
337 argv[1] = (char *)modname;
338 argv[2] = NULL;
339 execv(argv[0], argv);
340
Bart De Schuymer6622a012005-01-19 21:09:05 +0000341 /* Not usually reached */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000342 exit(0);
343 case -1:
344 return -1;
345
Bart De Schuymer6622a012005-01-19 21:09:05 +0000346 default: /* Parent */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000347 wait(NULL);
348 }
349
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000350 return 0;
351}
352
Bart De Schuymer6622a012005-01-19 21:09:05 +0000353/* Parse the chain name and return a pointer to the chain base.
354 * Returns NULL on failure. */
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000355struct ebt_u_entries *ebt_name_to_chain(const struct ebt_u_replace *replace, const char* arg)
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000356{
357 int i;
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000358 struct ebt_u_entries *chain;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000359
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000360 for (i = 0; i < replace->num_chains; i++) {
361 if (!(chain = replace->chains[i]))
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000362 continue;
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000363 if (!strcmp(arg, chain->name))
364 return chain;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000365 }
366 return NULL;
367}
368
Bart De Schuymer6622a012005-01-19 21:09:05 +0000369/* Parse the chain name and return the corresponding chain nr
370 * returns -1 on failure */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000371int ebt_get_chainnr(const struct ebt_u_replace *replace, const char* arg)
372{
373 int i;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000374
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000375 for (i = 0; i < replace->num_chains; i++) {
376 if (!replace->chains[i])
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000377 continue;
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000378 if (!strcmp(arg, replace->chains[i]->name))
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000379 return i;
380 }
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000381 return -1;
382}
383
384 /*
385************
386************
387**COMMANDS**
388************
389************
390 */
391
Bart De Schuymer6622a012005-01-19 21:09:05 +0000392/* Change the policy of selected_chain.
393 * Handing a bad policy to this function is a bug. */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000394void ebt_change_policy(struct ebt_u_replace *replace, int policy)
395{
396 struct ebt_u_entries *entries = ebt_to_chain(replace);
397
398 if (policy < -NUM_STANDARD_TARGETS || policy == EBT_CONTINUE)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000399 ebt_print_bug("Wrong policy: %d", policy);
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000400 entries->policy = policy;
401}
402
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000403void ebt_delete_cc(struct ebt_cntchanges *cc)
404{
405 if (cc->type == CNT_ADD) {
406 cc->prev->next = cc->next;
407 cc->next->prev = cc->prev;
408 free(cc);
409 }
410 cc->type = CNT_DEL;
411}
412
Bart De Schuymere94eaf72005-08-28 16:06:22 +0000413void ebt_empty_chain(struct ebt_u_entries *entries)
414{
415 struct ebt_u_entry *u_e = entries->entries->next, *tmp;
416 while (u_e != entries->entries) {
417 ebt_delete_cc(u_e->cc);
418 ebt_free_u_entry(u_e);
419 tmp = u_e->next;
420 free(u_e);
421 u_e = tmp;
422 }
423 entries->entries->next = entries->entries->prev = entries->entries;
424 entries->nentries = 0;
425}
426
Bart De Schuymer6622a012005-01-19 21:09:05 +0000427/* Flush one chain or the complete table
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000428 * If selected_chain == -1 then flush the complete table */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000429void ebt_flush_chains(struct ebt_u_replace *replace)
430{
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000431 int i, numdel;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000432 struct ebt_u_entries *entries = ebt_to_chain(replace);
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000433
Bart De Schuymer6622a012005-01-19 21:09:05 +0000434 /* Flush whole table */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000435 if (!entries) {
436 if (replace->nentries == 0)
437 return;
438 replace->nentries = 0;
439
Bart De Schuymer6622a012005-01-19 21:09:05 +0000440 /* Free everything and zero (n)entries */
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000441 for (i = 0; i < replace->num_chains; i++) {
442 if (!(entries = replace->chains[i]))
443 continue;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000444 entries->counter_offset = 0;
Bart De Schuymere94eaf72005-08-28 16:06:22 +0000445 ebt_empty_chain(entries);
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000446 }
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000447 return;
448 }
449
450 if (entries->nentries == 0)
451 return;
452 replace->nentries -= entries->nentries;
453 numdel = entries->nentries;
454
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000455 /* Update counter_offset */
456 for (i = replace->selected_chain+1; i < replace->num_chains; i++) {
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000457 if (!(entries = replace->chains[i]))
458 continue;
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000459 entries->counter_offset -= numdel;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000460 }
461
462 entries = ebt_to_chain(replace);
Bart De Schuymere94eaf72005-08-28 16:06:22 +0000463 ebt_empty_chain(entries);
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000464}
465
Bart De Schuymerab611e22005-02-14 20:20:03 +0000466#define OPT_COUNT 0x1000 /* This value is also defined in ebtables.c */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000467/* Returns the rule number on success (starting from 0), -1 on failure
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000468 *
469 * This function expects the ebt_{match,watcher,target} members of new_entry
Bart De Schuymer6622a012005-01-19 21:09:05 +0000470 * to contain pointers to ebt_u_{match,watcher,target} */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000471int ebt_check_rule_exists(struct ebt_u_replace *replace,
472 struct ebt_u_entry *new_entry)
473{
474 struct ebt_u_entry *u_e;
475 struct ebt_u_match_list *m_l, *m_l2;
476 struct ebt_u_match *m;
477 struct ebt_u_watcher_list *w_l, *w_l2;
478 struct ebt_u_watcher *w;
479 struct ebt_u_target *t = (struct ebt_u_target *)new_entry->t;
480 struct ebt_u_entries *entries = ebt_to_chain(replace);
481 int i, j, k;
482
Bart De Schuymere94eaf72005-08-28 16:06:22 +0000483 u_e = entries->entries->next;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000484 /* Check for an existing rule (if there are duplicate rules,
485 * take the first occurance) */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000486 for (i = 0; i < entries->nentries; i++, u_e = u_e->next) {
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000487 if (u_e->ethproto != new_entry->ethproto)
488 continue;
489 if (strcmp(u_e->in, new_entry->in))
490 continue;
491 if (strcmp(u_e->out, new_entry->out))
492 continue;
493 if (strcmp(u_e->logical_in, new_entry->logical_in))
494 continue;
495 if (strcmp(u_e->logical_out, new_entry->logical_out))
496 continue;
497 if (new_entry->bitmask & EBT_SOURCEMAC &&
Bart De Schuymer6622a012005-01-19 21:09:05 +0000498 memcmp(u_e->sourcemac, new_entry->sourcemac, ETH_ALEN))
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000499 continue;
500 if (new_entry->bitmask & EBT_DESTMAC &&
Bart De Schuymer6622a012005-01-19 21:09:05 +0000501 memcmp(u_e->destmac, new_entry->destmac, ETH_ALEN))
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000502 continue;
503 if (new_entry->bitmask != u_e->bitmask ||
Bart De Schuymer6622a012005-01-19 21:09:05 +0000504 new_entry->invflags != u_e->invflags)
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000505 continue;
Bart De Schuymerab611e22005-02-14 20:20:03 +0000506 if (replace->flags & OPT_COUNT && (new_entry->cnt.pcnt !=
507 u_e->cnt.pcnt || new_entry->cnt.bcnt != u_e->cnt.bcnt))
508 continue;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000509 /* Compare all matches */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000510 m_l = new_entry->m_list;
511 j = 0;
512 while (m_l) {
513 m = (struct ebt_u_match *)(m_l->m);
514 m_l2 = u_e->m_list;
515 while (m_l2 && strcmp(m_l2->m->u.name, m->m->u.name))
516 m_l2 = m_l2->next;
517 if (!m_l2 || !m->compare(m->m, m_l2->m))
518 goto letscontinue;
519 j++;
520 m_l = m_l->next;
521 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000522 /* Now be sure they have the same nr of matches */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000523 k = 0;
524 m_l = u_e->m_list;
525 while (m_l) {
526 k++;
527 m_l = m_l->next;
528 }
529 if (j != k)
530 continue;
531
Bart De Schuymer6622a012005-01-19 21:09:05 +0000532 /* Compare all watchers */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000533 w_l = new_entry->w_list;
534 j = 0;
535 while (w_l) {
536 w = (struct ebt_u_watcher *)(w_l->w);
537 w_l2 = u_e->w_list;
538 while (w_l2 && strcmp(w_l2->w->u.name, w->w->u.name))
539 w_l2 = w_l2->next;
540 if (!w_l2 || !w->compare(w->w, w_l2->w))
541 goto letscontinue;
542 j++;
543 w_l = w_l->next;
544 }
545 k = 0;
546 w_l = u_e->w_list;
547 while (w_l) {
548 k++;
549 w_l = w_l->next;
550 }
551 if (j != k)
552 continue;
553 if (strcmp(t->t->u.name, u_e->t->u.name))
554 continue;
555 if (!t->compare(t->t, u_e->t))
556 continue;
557 return i;
Bart De Schuymeraef08942004-09-09 21:41:29 +0000558letscontinue:;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000559 }
560 return -1;
561}
562
563/* Add a rule, rule_nr is the rule to update
564 * rule_nr specifies where the rule should be inserted
565 * rule_nr > 0 : insert the rule right before the rule_nr'th rule
566 * (the first rule is rule 1)
567 * rule_nr < 0 : insert the rule right before the (n+rule_nr+1)'th rule,
Bart De Schuymer6622a012005-01-19 21:09:05 +0000568 * where n denotes the number of rules in the chain
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000569 * rule_nr == 0: add a new rule at the end of the chain
570 *
571 * This function expects the ebt_{match,watcher,target} members of new_entry
572 * to contain pointers to ebt_u_{match,watcher,target} and updates these
Bart De Schuymer6622a012005-01-19 21:09:05 +0000573 * pointers so that they point to ebt_{match,watcher,target}, before adding
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000574 * the rule to the chain. Don't free() the ebt_{match,watcher,target} and
575 * don't reuse the new_entry after a successful call to ebt_add_rule() */
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000576void ebt_add_rule(struct ebt_u_replace *replace, struct ebt_u_entry *new_entry, int rule_nr)
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000577{
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000578 int i;
Bart De Schuymere94eaf72005-08-28 16:06:22 +0000579 struct ebt_u_entry *u_e;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000580 struct ebt_u_match_list *m_l;
581 struct ebt_u_watcher_list *w_l;
582 struct ebt_u_entries *entries = ebt_to_chain(replace);
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000583 struct ebt_cntchanges *cc, *new_cc;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000584
585 if (rule_nr <= 0)
586 rule_nr += entries->nentries;
587 else
588 rule_nr--;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000589 if (rule_nr > entries->nentries || rule_nr < 0) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000590 ebt_print_error("The specified rule number is incorrect");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000591 return;
592 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000593 /* Go to the right position in the chain */
Bart De Schuymera0d0a0a2005-08-30 21:51:09 +0000594 if (rule_nr == entries->nentries)
Bart De Schuymere94eaf72005-08-28 16:06:22 +0000595 u_e = entries->entries;
596 else {
597 u_e = entries->entries->next;
598 for (i = 0; i < rule_nr; i++)
599 u_e = u_e->next;
600 }
Bart De Schuymera0d0a0a2005-08-30 21:51:09 +0000601 /* We're adding one rule */
602 replace->nentries++;
603 entries->nentries++;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000604 /* Insert the rule */
Bart De Schuymere94eaf72005-08-28 16:06:22 +0000605 new_entry->next = u_e;
606 new_entry->prev = u_e->prev;
607 u_e->prev->next = new_entry;
608 u_e->prev = new_entry;
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000609 new_cc = (struct ebt_cntchanges *)malloc(sizeof(struct ebt_cntchanges));
610 if (!new_cc)
611 ebt_print_memory();
612 new_cc->type = CNT_ADD;
613 new_cc->change = 0;
Bart De Schuymere94eaf72005-08-28 16:06:22 +0000614 if (new_entry->next == entries->entries) {
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000615 for (i = replace->selected_chain+1; i < replace->num_chains; i++)
616 if (!replace->chains[i] || replace->chains[i]->nentries == 0)
617 continue;
618 else
619 break;
620 if (i == replace->num_chains)
621 cc = replace->cc;
622 else
Bart De Schuymera0d0a0a2005-08-30 21:51:09 +0000623 cc = replace->chains[i]->entries->next->cc;
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000624 } else
625 cc = new_entry->next->cc;
626 new_cc->next = cc;
627 new_cc->prev = cc->prev;
628 cc->prev->next = new_cc;
629 cc->prev = new_cc;
Bart De Schuymer7ac91522005-09-01 20:34:29 +0000630 new_entry->cc = new_cc;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000631
Bart De Schuymer6622a012005-01-19 21:09:05 +0000632 /* Put the ebt_{match, watcher, target} pointers in place */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000633 m_l = new_entry->m_list;
634 while (m_l) {
635 m_l->m = ((struct ebt_u_match *)m_l->m)->m;
636 m_l = m_l->next;
637 }
638 w_l = new_entry->w_list;
639 while (w_l) {
640 w_l->w = ((struct ebt_u_watcher *)w_l->w)->w;
641 w_l = w_l->next;
642 }
643 new_entry->t = ((struct ebt_u_target *)new_entry->t)->t;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000644 /* Update the counter_offset of chains behind this one */
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000645 for (i = replace->selected_chain+1; i < replace->num_chains; i++) {
646 entries = replace->chains[i];
647 if (!(entries = replace->chains[i]))
648 continue;
649 entries->counter_offset++;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000650 }
651}
652
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000653/* If *begin==*end==0 then find the rule corresponding to new_entry,
Bart De Schuymerd5dc87d2005-07-16 22:35:32 +0000654 * else make the rule numbers positive (starting from 0) and check
655 * for bad rule numbers. */
Bart De Schuymerff587202005-02-08 20:02:28 +0000656static int check_and_change_rule_number(struct ebt_u_replace *replace,
657 struct ebt_u_entry *new_entry, int *begin, int *end)
658{
659 struct ebt_u_entries *entries = ebt_to_chain(replace);
660
661 if (*begin < 0)
662 *begin += entries->nentries + 1;
663 if (*end < 0)
664 *end += entries->nentries + 1;
665
666 if (*begin < 0 || *begin > *end || *end > entries->nentries) {
667 ebt_print_error("Sorry, wrong rule numbers");
668 return -1;
669 }
670
671 if ((*begin * *end == 0) && (*begin + *end != 0))
672 ebt_print_bug("begin and end should be either both zero, "
673 "either both non-zero");
Bart De Schuymerd5dc87d2005-07-16 22:35:32 +0000674 if (*begin != 0) {
Bart De Schuymerff587202005-02-08 20:02:28 +0000675 (*begin)--;
676 (*end)--;
677 } else {
678 *begin = ebt_check_rule_exists(replace, new_entry);
679 *end = *begin;
680 if (*begin == -1) {
681 ebt_print_error("Sorry, rule does not exist");
682 return -1;
683 }
684 }
685 return 0;
686}
687
Bart De Schuymer6622a012005-01-19 21:09:05 +0000688/* Delete a rule or rules
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000689 * begin == end == 0: delete the rule corresponding to new_entry
690 *
Bart De Schuymer6622a012005-01-19 21:09:05 +0000691 * The first rule has rule nr 1, the last rule has rule nr -1, etc.
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000692 * This function expects the ebt_{match,watcher,target} members of new_entry
Bart De Schuymer6622a012005-01-19 21:09:05 +0000693 * to contain pointers to ebt_u_{match,watcher,target}. */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000694void ebt_delete_rule(struct ebt_u_replace *replace,
695 struct ebt_u_entry *new_entry, int begin, int end)
696{
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000697 int i, nr_deletes;
Bart De Schuymere94eaf72005-08-28 16:06:22 +0000698 struct ebt_u_entry *u_e, *u_e2;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000699 struct ebt_u_entries *entries = ebt_to_chain(replace);
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000700
Bart De Schuymerff587202005-02-08 20:02:28 +0000701 if (check_and_change_rule_number(replace, new_entry, &begin, &end))
Bart De Schuymer6622a012005-01-19 21:09:05 +0000702 return;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000703 /* We're deleting rules */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000704 nr_deletes = end - begin + 1;
705 replace->nentries -= nr_deletes;
706 entries->nentries -= nr_deletes;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000707 /* Go to the right position in the chain */
Bart De Schuymere94eaf72005-08-28 16:06:22 +0000708 u_e = entries->entries->next;
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000709 for (i = 0; i < begin; i++)
Bart De Schuymere94eaf72005-08-28 16:06:22 +0000710 u_e = u_e->next;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000711 /* Remove the rules */
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000712 for (i = 0; i < nr_deletes; i++) {
Bart De Schuymere94eaf72005-08-28 16:06:22 +0000713 u_e2 = u_e;
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000714 ebt_delete_cc(u_e2->cc);
Bart De Schuymere94eaf72005-08-28 16:06:22 +0000715 u_e = u_e->next;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000716 /* Free everything */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000717 ebt_free_u_entry(u_e2);
718 free(u_e2);
719 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000720 /* Update the counter_offset of chains behind this one */
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000721 for (i = replace->selected_chain+1; i < replace->num_chains; i++) {
722 if (!(entries = replace->chains[i]))
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000723 continue;
724 entries->counter_offset -= nr_deletes;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000725 }
726}
727
Bart De Schuymerff587202005-02-08 20:02:28 +0000728/* Change the counters of a rule or rules
729 * begin == end == 0: change counters of the rule corresponding to new_entry
730 *
731 * The first rule has rule nr 1, the last rule has rule nr -1, etc.
732 * This function expects the ebt_{match,watcher,target} members of new_entry
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000733 * to contain pointers to ebt_u_{match,watcher,target}.
734 * The mask denotes the following:
735 * pcnt: mask % 3 = 0 : change; = 1: increment; = 2: decrement
736 * bcnt: mask / 3 = 0 : change; = 1: increment = 2: increment
737 * In daemon mode, mask==0 must hold */
Bart De Schuymerff587202005-02-08 20:02:28 +0000738void ebt_change_counters(struct ebt_u_replace *replace,
739 struct ebt_u_entry *new_entry, int begin, int end,
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000740 struct ebt_counter *cnt, int mask)
Bart De Schuymerff587202005-02-08 20:02:28 +0000741{
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000742 int i;
Bart De Schuymerff587202005-02-08 20:02:28 +0000743 struct ebt_u_entry *u_e;
744 struct ebt_u_entries *entries = ebt_to_chain(replace);
Bart De Schuymerff587202005-02-08 20:02:28 +0000745
746 if (check_and_change_rule_number(replace, new_entry, &begin, &end))
747 return;
Bart De Schuymere94eaf72005-08-28 16:06:22 +0000748 u_e = entries->entries->next;
Bart De Schuymerff587202005-02-08 20:02:28 +0000749 for (i = 0; i < begin; i++)
750 u_e = u_e->next;
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000751 for (i = end-begin+1; i > 0; i--) {
752 if (mask % 3 == 0) {
753 u_e->cnt.pcnt = (*cnt).pcnt;
754 u_e->cnt_surplus.pcnt = 0;
755 } else {
Bart De Schuymer0fbb3f92005-08-12 16:47:02 +0000756#ifdef EBT_DEBUG
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000757 if (u_e->cc->type != CNT_NORM)
758 ebt_print_bug("cc->type != CNT_NORM");
Bart De Schuymer0fbb3f92005-08-12 16:47:02 +0000759#endif
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000760 u_e->cnt_surplus.pcnt = (*cnt).pcnt;
Bart De Schuymerab611e22005-02-14 20:20:03 +0000761 }
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000762
763 if (mask / 3 == 0) {
764 u_e->cnt.bcnt = (*cnt).bcnt;
765 u_e->cnt_surplus.bcnt = 0;
766 } else {
767#ifdef EBT_DEBUG
768 if (u_e->cc->type != CNT_NORM)
769 ebt_print_bug("cc->type != CNT_NORM");
770#endif
771 u_e->cnt_surplus.bcnt = (*cnt).bcnt;
772 }
773 if (u_e->cc->type != CNT_ADD)
774 u_e->cc->type = CNT_CHANGE;
775 u_e->cc->change = mask;
776 u_e = u_e->next;
Bart De Schuymerff587202005-02-08 20:02:28 +0000777 }
778}
779
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000780/* If selected_chain == -1 then zero all counters,
781 * otherwise, zero the counters of selected_chain */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000782void ebt_zero_counters(struct ebt_u_replace *replace)
783{
784 struct ebt_u_entries *entries = ebt_to_chain(replace);
Bart De Schuymer93f36ba2005-01-24 21:11:24 +0000785 struct ebt_u_entry *next;
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000786 int i;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000787
788 if (!entries) {
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000789 for (i = 0; i < replace->num_chains; i++) {
790 if (!(entries = replace->chains[i]))
Bart De Schuymer93f36ba2005-01-24 21:11:24 +0000791 continue;
Bart De Schuymere94eaf72005-08-28 16:06:22 +0000792 next = entries->entries->next;
793 while (next != entries->entries) {
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000794 if (next->cc->type == CNT_NORM)
795 next->cc->type = CNT_CHANGE;
Bart De Schuymer93f36ba2005-01-24 21:11:24 +0000796 next->cnt.bcnt = next->cnt.pcnt = 0;
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000797 next->cc->change = 0;
Bart De Schuymer93f36ba2005-01-24 21:11:24 +0000798 next = next->next;
799 }
800 }
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000801 } else {
802 if (entries->nentries == 0)
803 return;
804
Bart De Schuymere94eaf72005-08-28 16:06:22 +0000805 next = entries->entries->next;
806 while (next != entries->entries) {
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000807 if (next->cc->type == CNT_NORM)
808 next->cc->type = CNT_CHANGE;
Bart De Schuymer93f36ba2005-01-24 21:11:24 +0000809 next->cnt.bcnt = next->cnt.pcnt = 0;
810 next = next->next;
811 }
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000812 }
813}
814
Bart De Schuymer6622a012005-01-19 21:09:05 +0000815/* Add a new chain and specify its policy */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000816void ebt_new_chain(struct ebt_u_replace *replace, const char *name, int policy)
817{
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000818 struct ebt_u_entries *new;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000819
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000820 if (replace->num_chains == replace->max_chains)
821 ebt_double_chains(replace);
822 new = (struct ebt_u_entries *)malloc(sizeof(struct ebt_u_entries));
823 if (!new)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000824 ebt_print_memory();
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000825 replace->chains[replace->num_chains++] = new;
826 new->nentries = 0;
827 new->policy = policy;
828 new->counter_offset = replace->nentries;
829 new->hook_mask = 0;
830 strcpy(new->name, name);
Bart De Schuymere94eaf72005-08-28 16:06:22 +0000831 new->entries = (struct ebt_u_entry *)malloc(sizeof(struct ebt_u_entry));
832 if (!new->entries)
833 ebt_print_memory();
834 new->entries->next = new->entries->prev = new->entries;
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000835 new->kernel_start = NULL;
836}
837
838static void ebt_delete_a_chain(struct ebt_u_replace *replace, int chain, int print_err)
839{
840 int tmp = replace->selected_chain;
841 /* If the chain is referenced, don't delete it,
842 * also decrement jumps to a chain behind the
843 * one we're deleting */
844 replace->selected_chain = chain;
845 if (ebt_check_for_references(replace, print_err))
846 return;
847 decrease_chain_jumps(replace);
848 ebt_flush_chains(replace);
849 replace->selected_chain = tmp;
Bart De Schuymere94eaf72005-08-28 16:06:22 +0000850 free(replace->chains[chain]->entries);
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000851 free(replace->chains[chain]);
852 memmove(replace->chains+chain, replace->chains+chain+1, (replace->num_chains-chain-1)*sizeof(void *));
853 replace->num_chains--;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000854}
855
Bart De Schuymer6622a012005-01-19 21:09:05 +0000856/* Selected_chain == -1: delete all non-referenced udc
857 * selected_chain < NF_BR_NUMHOOKS is illegal */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000858void ebt_delete_chain(struct ebt_u_replace *replace)
859{
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000860 int i;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000861
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000862 if (replace->selected_chain != -1 && replace->selected_chain < NF_BR_NUMHOOKS)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000863 ebt_print_bug("You can't remove a standard chain");
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000864 if (replace->selected_chain == -1)
865 for (i = NF_BR_NUMHOOKS; i < replace->num_chains; i++)
866 ebt_delete_a_chain(replace, i, 0);
867 else
868 ebt_delete_a_chain(replace, replace->selected_chain, 1);
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000869}
870
Bart De Schuymer6622a012005-01-19 21:09:05 +0000871/* Rename an existing chain. */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000872void ebt_rename_chain(struct ebt_u_replace *replace, const char *name)
873{
874 struct ebt_u_entries *entries = ebt_to_chain(replace);
875
876 if (!entries)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000877 ebt_print_bug("ebt_rename_chain: entries == NULL");
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000878 strcpy(entries->name, name);
879}
880
881
882 /*
883*************************
884*************************
885**SPECIALIZED*FUNCTIONS**
886*************************
887*************************
888 */
889
890
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000891void ebt_double_chains(struct ebt_u_replace *replace)
892{
893 struct ebt_u_entries **new;
894
895 replace->max_chains *= 2;
896 new = (struct ebt_u_entries **)malloc(replace->max_chains*sizeof(void *));
897 if (!new)
898 ebt_print_memory();
899 memcpy(new, replace->chains, replace->max_chains/2*sizeof(void *));
900 free(replace->chains);
901 replace->chains = new;
902}
903
Bart De Schuymer6622a012005-01-19 21:09:05 +0000904/* Executes the final_check() function for all extensions used by the rule
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000905 * ebt_check_for_loops should have been executed earlier, to make sure the
Bart De Schuymer6622a012005-01-19 21:09:05 +0000906 * hook_mask is correct. The time argument to final_check() is set to 1,
907 * meaning it's the second time the final_check() function is executed. */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000908void ebt_do_final_checks(struct ebt_u_replace *replace, struct ebt_u_entry *e,
909 struct ebt_u_entries *entries)
910{
911 struct ebt_u_match_list *m_l;
912 struct ebt_u_watcher_list *w_l;
913 struct ebt_u_target *t;
914 struct ebt_u_match *m;
915 struct ebt_u_watcher *w;
916
917 m_l = e->m_list;
918 w_l = e->w_list;
919 while (m_l) {
920 m = ebt_find_match(m_l->m->u.name);
921 m->final_check(e, m_l->m, replace->name,
922 entries->hook_mask, 1);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000923 if (ebt_errormsg[0] != '\0')
924 return;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000925 m_l = m_l->next;
926 }
927 while (w_l) {
928 w = ebt_find_watcher(w_l->w->u.name);
929 w->final_check(e, w_l->w, replace->name,
930 entries->hook_mask, 1);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000931 if (ebt_errormsg[0] != '\0')
932 return;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000933 w_l = w_l->next;
934 }
935 t = ebt_find_target(e->t->u.name);
936 t->final_check(e, e->t, replace->name,
937 entries->hook_mask, 1);
938}
939
Bart De Schuymer6622a012005-01-19 21:09:05 +0000940/* Returns 1 (if it returns) when the chain is referenced, 0 when it isn't.
941 * print_err: 0 (resp. 1) = don't (resp. do) print error when referenced */
942int ebt_check_for_references(struct ebt_u_replace *replace, int print_err)
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000943{
Bart De Schuymer6622a012005-01-19 21:09:05 +0000944 if (print_err)
945 return iterate_entries(replace, 1);
946 else
947 return iterate_entries(replace, 2);
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000948}
949
Bart De Schuymer6622a012005-01-19 21:09:05 +0000950/* chain_nr: nr of the udc (>= NF_BR_NUMHOOKS)
951 * Returns 1 (if it returns) when the chain is referenced, 0 when it isn't.
952 * print_err: 0 (resp. 1) = don't (resp. do) print error when referenced */
953int ebt_check_for_references2(struct ebt_u_replace *replace, int chain_nr,
954 int print_err)
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000955{
956 int tmp = replace->selected_chain, ret;
957
958 replace->selected_chain = chain_nr;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000959 if (print_err)
960 ret = iterate_entries(replace, 1);
961 else
962 ret = iterate_entries(replace, 2);
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000963 replace->selected_chain = tmp;
964 return ret;
965}
966
967struct ebt_u_stack
968{
969 int chain_nr;
970 int n;
971 struct ebt_u_entry *e;
972 struct ebt_u_entries *entries;
973};
974
Bart De Schuymer6622a012005-01-19 21:09:05 +0000975/* Checks for loops
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000976 * As a by-product, the hook_mask member of each chain is filled in
977 * correctly. The check functions of the extensions need this hook_mask
Bart De Schuymer6622a012005-01-19 21:09:05 +0000978 * to know from which standard chains they can be called. */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000979void ebt_check_for_loops(struct ebt_u_replace *replace)
980{
981 int chain_nr , i, j , k, sp = 0, verdict;
982 struct ebt_u_entries *entries, *entries2;
983 struct ebt_u_stack *stack = NULL;
984 struct ebt_u_entry *e;
985
Bart De Schuymer6622a012005-01-19 21:09:05 +0000986 /* Initialize hook_mask to 0 */
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000987 for (i = 0; i < replace->num_chains; i++) {
988 if (!(entries = replace->chains[i]))
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000989 continue;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000990 entries->hook_mask = 0;
991 }
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000992 if (replace->num_chains == NF_BR_NUMHOOKS)
993 return;
994 stack = (struct ebt_u_stack *)malloc((replace->num_chains - NF_BR_NUMHOOKS) * sizeof(struct ebt_u_stack));
995 if (!stack)
996 ebt_print_memory();
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000997
Bart De Schuymer6622a012005-01-19 21:09:05 +0000998 /* Check for loops, starting from every base chain */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +0000999 for (i = 0; i < NF_BR_NUMHOOKS; i++) {
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +00001000 if (!(entries = replace->chains[i]))
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001001 continue;
Bart De Schuymer6622a012005-01-19 21:09:05 +00001002 /* (1 << NF_BR_NUMHOOKS) implies it's a standard chain
1003 * (usefull in the final_check() funtions) */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001004 entries->hook_mask = (1 << i) | (1 << NF_BR_NUMHOOKS);
1005 chain_nr = i;
1006
Bart De Schuymere94eaf72005-08-28 16:06:22 +00001007 e = entries->entries->next;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001008 for (j = 0; j < entries->nentries; j++) {
1009 if (strcmp(e->t->u.name, EBT_STANDARD_TARGET))
1010 goto letscontinue;
1011 verdict = ((struct ebt_standard_target *)(e->t))->verdict;
1012 if (verdict < 0)
1013 goto letscontinue;
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +00001014 entries2 = replace->chains[verdict + NF_BR_NUMHOOKS];
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001015 entries2->hook_mask |= entries->hook_mask;
Bart De Schuymer6622a012005-01-19 21:09:05 +00001016 /* Now see if we've been here before */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001017 for (k = 0; k < sp; k++)
Bart De Schuymer6622a012005-01-19 21:09:05 +00001018 if (stack[k].chain_nr == verdict + NF_BR_NUMHOOKS) {
1019 ebt_print_error("Loop from chain '%s' to chain '%s'",
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +00001020 replace->chains[chain_nr]->name,
1021 replace->chains[stack[k].chain_nr]->name);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001022 goto free_stack;
1023 }
1024 /* Jump to the chain, make sure we know how to get back */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001025 stack[sp].chain_nr = chain_nr;
1026 stack[sp].n = j;
1027 stack[sp].entries = entries;
1028 stack[sp].e = e;
1029 sp++;
1030 j = -1;
1031 e = entries2->entries;
1032 chain_nr = verdict + NF_BR_NUMHOOKS;
1033 entries = entries2;
1034 continue;
1035letscontinue:
1036 e = e->next;
1037 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001038 /* We are at the end of a standard chain */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001039 if (sp == 0)
1040 continue;
Bart De Schuymer6622a012005-01-19 21:09:05 +00001041 /* Go back to the chain one level higher */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001042 sp--;
1043 j = stack[sp].n;
1044 chain_nr = stack[sp].chain_nr;
1045 e = stack[sp].e;
1046 entries = stack[sp].entries;
1047 goto letscontinue;
1048 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001049free_stack:
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001050 free(stack);
1051 return;
1052}
1053
Bart De Schuymer6622a012005-01-19 21:09:05 +00001054/* The user will use the match, so put it in new_entry. The ebt_u_match
1055 * pointer is put in the ebt_entry_match pointer. ebt_add_rule will
1056 * fill in the final value for new->m. Unless the rule is added to a chain,
1057 * the pointer will keep pointing to the ebt_u_match (until the new_entry
1058 * is freed). I know, I should use a union for these 2 pointer types... */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001059void ebt_add_match(struct ebt_u_entry *new_entry, struct ebt_u_match *m)
1060{
1061 struct ebt_u_match_list **m_list, *new;
1062
1063 for (m_list = &new_entry->m_list; *m_list; m_list = &(*m_list)->next);
1064 new = (struct ebt_u_match_list *)
1065 malloc(sizeof(struct ebt_u_match_list));
1066 if (!new)
Bart De Schuymer64182a32004-01-21 20:39:54 +00001067 ebt_print_memory();
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001068 *m_list = new;
1069 new->next = NULL;
1070 new->m = (struct ebt_entry_match *)m;
1071}
1072
1073void ebt_add_watcher(struct ebt_u_entry *new_entry, struct ebt_u_watcher *w)
1074{
1075 struct ebt_u_watcher_list **w_list;
1076 struct ebt_u_watcher_list *new;
1077
1078 for (w_list = &new_entry->w_list; *w_list; w_list = &(*w_list)->next);
1079 new = (struct ebt_u_watcher_list *)
1080 malloc(sizeof(struct ebt_u_watcher_list));
1081 if (!new)
Bart De Schuymer64182a32004-01-21 20:39:54 +00001082 ebt_print_memory();
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001083 *w_list = new;
1084 new->next = NULL;
1085 new->w = (struct ebt_entry_watcher *)w;
1086}
1087
1088
1089 /*
1090*******************
1091*******************
1092**OTHER*FUNCTIONS**
1093*******************
1094*******************
1095 */
1096
1097
Bart De Schuymer6622a012005-01-19 21:09:05 +00001098/* type = 0 => update chain jumps
1099 * type = 1 => check for reference, print error when referenced
1100 * type = 2 => check for reference, don't print error when referenced
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001101 *
Bart De Schuymer6622a012005-01-19 21:09:05 +00001102 * Returns 1 when type == 1 and the chain is referenced
1103 * returns 0 otherwise */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001104static int iterate_entries(struct ebt_u_replace *replace, int type)
1105{
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +00001106 int i, j, chain_nr = replace->selected_chain - NF_BR_NUMHOOKS;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001107 struct ebt_u_entries *entries;
1108 struct ebt_u_entry *e;
1109
1110 if (chain_nr < 0)
Bart De Schuymer64182a32004-01-21 20:39:54 +00001111 ebt_print_bug("iterate_entries: udc = %d < 0", chain_nr);
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +00001112 for (i = 0; i < replace->num_chains; i++) {
1113 if (!(entries = replace->chains[i]))
1114 continue;
Bart De Schuymere94eaf72005-08-28 16:06:22 +00001115 e = entries->entries->next;
1116 for (j = 0; j < entries->nentries; j++) {
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001117 int chain_jmp;
1118
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001119 if (strcmp(e->t->u.name, EBT_STANDARD_TARGET)) {
1120 e = e->next;
1121 continue;
1122 }
Bart De Schuymer64182a32004-01-21 20:39:54 +00001123 chain_jmp = ((struct ebt_standard_target *)e->t)->
1124 verdict;
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001125 switch (type) {
1126 case 1:
Bart De Schuymer6622a012005-01-19 21:09:05 +00001127 case 2:
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001128 if (chain_jmp == chain_nr) {
Bart De Schuymer6622a012005-01-19 21:09:05 +00001129 if (type == 2)
1130 return 1;
1131 ebt_print_error("Can't delete the chain '%s', it's referenced in chain '%s', rule %d",
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +00001132 replace->chains[chain_nr + NF_BR_NUMHOOKS]->name, entries->name, j);
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001133 return 1;
1134 }
1135 break;
1136 case 0:
Bart De Schuymer6622a012005-01-19 21:09:05 +00001137 /* Adjust the chain jumps when necessary */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001138 if (chain_jmp > chain_nr)
1139 ((struct ebt_standard_target *)e->t)->verdict--;
1140 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +00001141 } /* End switch */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001142 e = e->next;
1143 }
1144 }
1145 return 0;
1146}
1147
1148static void decrease_chain_jumps(struct ebt_u_replace *replace)
1149{
1150 iterate_entries(replace, 0);
1151}
1152
Bart De Schuymer6622a012005-01-19 21:09:05 +00001153/* Used in initialization code of modules */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001154void ebt_register_match(struct ebt_u_match *m)
1155{
1156 int size = EBT_ALIGN(m->size) + sizeof(struct ebt_entry_match);
1157 struct ebt_u_match **i;
1158
1159 m->m = (struct ebt_entry_match *)malloc(size);
1160 if (!m->m)
Bart De Schuymer64182a32004-01-21 20:39:54 +00001161 ebt_print_memory();
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001162 strcpy(m->m->u.name, m->name);
1163 m->m->match_size = EBT_ALIGN(m->size);
1164 m->init(m->m);
1165
1166 for (i = &ebt_matches; *i; i = &((*i)->next));
1167 m->next = NULL;
1168 *i = m;
1169}
1170
1171void ebt_register_watcher(struct ebt_u_watcher *w)
1172{
1173 int size = EBT_ALIGN(w->size) + sizeof(struct ebt_entry_watcher);
1174 struct ebt_u_watcher **i;
1175
1176 w->w = (struct ebt_entry_watcher *)malloc(size);
1177 if (!w->w)
Bart De Schuymer64182a32004-01-21 20:39:54 +00001178 ebt_print_memory();
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001179 strcpy(w->w->u.name, w->name);
1180 w->w->watcher_size = EBT_ALIGN(w->size);
1181 w->init(w->w);
1182
1183 for (i = &ebt_watchers; *i; i = &((*i)->next));
1184 w->next = NULL;
1185 *i = w;
1186}
1187
1188void ebt_register_target(struct ebt_u_target *t)
1189{
1190 int size = EBT_ALIGN(t->size) + sizeof(struct ebt_entry_target);
1191 struct ebt_u_target **i;
1192
1193 t->t = (struct ebt_entry_target *)malloc(size);
1194 if (!t->t)
Bart De Schuymer64182a32004-01-21 20:39:54 +00001195 ebt_print_memory();
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001196 strcpy(t->t->u.name, t->name);
1197 t->t->target_size = EBT_ALIGN(t->size);
1198 t->init(t->t);
1199
1200 for (i = &ebt_targets; *i; i = &((*i)->next));
1201 t->next = NULL;
1202 *i = t;
1203}
1204
1205void ebt_register_table(struct ebt_u_table *t)
1206{
1207 t->next = ebt_tables;
1208 ebt_tables = t;
1209}
1210
1211void ebt_iterate_matches(void (*f)(struct ebt_u_match *))
1212{
1213 struct ebt_u_match *i;
1214
1215 for (i = ebt_matches; i; i = i->next)
1216 f(i);
1217}
1218
1219void ebt_iterate_watchers(void (*f)(struct ebt_u_watcher *))
1220{
1221 struct ebt_u_watcher *i;
1222
1223 for (i = ebt_watchers; i; i = i->next)
1224 f(i);
1225}
1226
1227void ebt_iterate_targets(void (*f)(struct ebt_u_target *))
1228{
1229 struct ebt_u_target *i;
1230
1231 for (i = ebt_targets; i; i = i->next)
1232 f(i);
1233}
1234
Bart De Schuymer6622a012005-01-19 21:09:05 +00001235/* Don't use this function, use ebt_print_bug() */
Bart De Schuymer64182a32004-01-21 20:39:54 +00001236void __ebt_print_bug(char *file, int line, char *format, ...)
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001237{
1238 va_list l;
1239
1240 va_start(l, format);
1241 printf(PROGNAME" v"PROGVERSION":%s:%d:--BUG--: \n", file, line);
1242 vprintf(format, l);
1243 printf("\n");
1244 va_end(l);
1245 exit (-1);
1246}
1247
Bart De Schuymer6622a012005-01-19 21:09:05 +00001248/* The error messages are put in here when ebt_silent == 1
1249 * ebt_errormsg[0] == '\0' implies there was no error */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001250char ebt_errormsg[ERRORMSG_MAXLEN];
Bart De Schuymer6622a012005-01-19 21:09:05 +00001251/* When error messages should not be printed on the screen, after which
1252 * the program exit()s, set ebt_silent to 1. */
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001253int ebt_silent;
Bart De Schuymer6622a012005-01-19 21:09:05 +00001254/* Don't use this function, use ebt_print_error() */
Bart De Schuymer64182a32004-01-21 20:39:54 +00001255void __ebt_print_error(char *format, ...)
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001256{
1257 va_list l;
1258
1259 va_start(l, format);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001260 if (ebt_silent && ebt_errormsg[0] == '\0') {
1261 vsnprintf(ebt_errormsg, ERRORMSG_MAXLEN, format, l);
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001262 va_end(l);
1263 } else {
Bart De Schuymer83a1b0f2005-09-28 19:36:17 +00001264 vfprintf(stderr, format, l);
1265 fprintf(stderr, ".\n");
Bart De Schuymer80c82bb2004-01-14 20:06:44 +00001266 va_end(l);
1267 exit (-1);
1268 }
1269}