blob: 4d02b054af654217c47d4bce4bb75fdec450fbe7 [file] [log] [blame]
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001/*
Bart De Schuymer0cff9e92002-07-25 12:29:50 +00002 * communication.c, v2.0 July 2002
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00003 *
4 * Author: Bart De Schuymer
5 *
6 */
7
Bart De Schuymer9895a8e2003-01-11 10:14:24 +00008/*
9 * All the userspace/kernel communication is in this file.
10 * The other code should not have to know anything about the way the
11 * kernel likes the structure of the table data.
12 * The other code works with linked lists, lots of linked lists.
13 * So, the translation is done here.
14 */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000015
16#include <getopt.h>
17#include <string.h>
18#include <errno.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <sys/socket.h>
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000022#include "include/ebtables_u.h"
23
24extern char* hooknames[NF_BR_NUMHOOKS];
25
Bart De Schuymerf81c2702003-07-23 21:07:04 +000026#ifdef KERNEL_64_USERSPACE_32
27#define sparc_cast (uint64_t)
28#else
29#define sparc_cast
30#endif
31
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000032int sockfd = -1;
33
Bart De Schuymer62423742002-07-14 19:06:20 +000034static void get_sockfd()
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000035{
36 if (sockfd == -1) {
37 sockfd = socket(AF_INET, SOCK_RAW, PF_INET);
38 if (sockfd < 0)
Bart De Schuymer0cff9e92002-07-25 12:29:50 +000039 print_error("Problem getting a socket, "
Bart De Schuymer06b4fd22002-08-01 15:08:28 +000040 "you probably don't have the right "
41 "permissions");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000042 }
43}
44
45static struct ebt_replace * translate_user2kernel(struct ebt_u_replace *u_repl)
46{
47 struct ebt_replace *new;
48 struct ebt_u_entry *e;
49 struct ebt_u_match_list *m_l;
50 struct ebt_u_watcher_list *w_l;
Bart De Schuymer60332e02002-06-23 08:01:47 +000051 struct ebt_u_chain_list *cl;
52 struct ebt_u_entries *entries;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000053 char *p, *base;
54 int i, j;
Bart De Schuymer60332e02002-06-23 08:01:47 +000055 unsigned int entries_size = 0, *chain_offsets;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000056
57 new = (struct ebt_replace *)malloc(sizeof(struct ebt_replace));
58 if (!new)
59 print_memory();
60 new->valid_hooks = u_repl->valid_hooks;
Bart De Schuymer0cff9e92002-07-25 12:29:50 +000061 strcpy(new->name, u_repl->name);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000062 new->nentries = u_repl->nentries;
63 new->num_counters = u_repl->num_counters;
Bart De Schuymerf81c2702003-07-23 21:07:04 +000064 new->counters = sparc_cast u_repl->counters;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +000065 /* determine nr of udc */
Bart De Schuymer60332e02002-06-23 08:01:47 +000066 i = 0;
67 cl = u_repl->udc;
68 while (cl) {
69 i++;
70 cl = cl->next;
71 }
72 i += NF_BR_NUMHOOKS;
73 chain_offsets = (unsigned int *)malloc(i * sizeof(unsigned int));
Bart De Schuymer9895a8e2003-01-11 10:14:24 +000074 /* determine size */
Bart De Schuymer60332e02002-06-23 08:01:47 +000075 i = 0;
76 cl = u_repl->udc;
77 while (1) {
78 if (i < NF_BR_NUMHOOKS) {
79 if (!(new->valid_hooks & (1 << i))) {
80 i++;
81 continue;
82 }
83 entries = u_repl->hook_entry[i];
84 } else {
85 if (!cl)
86 break;
87 entries = cl->udc;
88 }
89 chain_offsets[i] = entries_size;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000090 entries_size += sizeof(struct ebt_entries);
91 j = 0;
Bart De Schuymer60332e02002-06-23 08:01:47 +000092 e = entries->entries;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000093 while (e) {
94 j++;
95 entries_size += sizeof(struct ebt_entry);
96 m_l = e->m_list;
97 while (m_l) {
98 entries_size += m_l->m->match_size +
99 sizeof(struct ebt_entry_match);
100 m_l = m_l->next;
101 }
102 w_l = e->w_list;
103 while (w_l) {
104 entries_size += w_l->w->watcher_size +
105 sizeof(struct ebt_entry_watcher);
106 w_l = w_l->next;
107 }
108 entries_size += e->t->target_size +
109 sizeof(struct ebt_entry_target);
110 e = e->next;
111 }
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000112 /* a little sanity check */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000113 if (j != entries->nentries)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000114 print_bug("Wrong nentries: %d != %d, hook = %s", j,
Bart De Schuymer60332e02002-06-23 08:01:47 +0000115 entries->nentries, entries->name);
116 if (i >= NF_BR_NUMHOOKS)
117 cl = cl->next;
118 i++;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000119 }
120
121 new->entries_size = entries_size;
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000122 p = (char *)malloc(entries_size);
123 if (!p)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000124 print_memory();
125
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000126 /* put everything in one block */
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000127 new->entries = sparc_cast p;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000128 i = 0;
129 cl = u_repl->udc;
130 while (1) {
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000131 struct ebt_entries *hlp;
132
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000133 hlp = (struct ebt_entries *)p;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000134 if (i < NF_BR_NUMHOOKS) {
135 if (!(new->valid_hooks & (1 << i))) {
136 i++;
137 continue;
138 }
139 entries = u_repl->hook_entry[i];
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000140 new->hook_entry[i] = sparc_cast hlp;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000141 } else {
142 if (!cl)
143 break;
144 entries = cl->udc;
145 }
146 hlp->nentries = entries->nentries;
147 hlp->policy = entries->policy;
148 strcpy(hlp->name, entries->name);
149 hlp->counter_offset = entries->counter_offset;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000150 hlp->distinguisher = 0; /* make the kernel see the light */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000151 p += sizeof(struct ebt_entries);
Bart De Schuymer60332e02002-06-23 08:01:47 +0000152 e = entries->entries;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000153 while (e) {
154 struct ebt_entry *tmp = (struct ebt_entry *)p;
155
156 tmp->bitmask = e->bitmask | EBT_ENTRY_OR_ENTRIES;
157 tmp->invflags = e->invflags;
158 tmp->ethproto = e->ethproto;
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000159 strcpy(tmp->in, e->in);
160 strcpy(tmp->out, e->out);
161 strcpy(tmp->logical_in, e->logical_in);
162 strcpy(tmp->logical_out, e->logical_out);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000163 memcpy(tmp->sourcemac, e->sourcemac,
164 sizeof(tmp->sourcemac));
165 memcpy(tmp->sourcemsk, e->sourcemsk,
166 sizeof(tmp->sourcemsk));
167 memcpy(tmp->destmac, e->destmac, sizeof(tmp->destmac));
168 memcpy(tmp->destmsk, e->destmsk, sizeof(tmp->destmsk));
169
170 base = p;
171 p += sizeof(struct ebt_entry);
172 m_l = e->m_list;
173 while (m_l) {
174 memcpy(p, m_l->m, m_l->m->match_size +
175 sizeof(struct ebt_entry_match));
176 p += m_l->m->match_size +
177 sizeof(struct ebt_entry_match);
178 m_l = m_l->next;
179 }
180 tmp->watchers_offset = p - base;
181 w_l = e->w_list;
182 while (w_l) {
183 memcpy(p, w_l->w, w_l->w->watcher_size +
184 sizeof(struct ebt_entry_watcher));
185 p += w_l->w->watcher_size +
186 sizeof(struct ebt_entry_watcher);
187 w_l = w_l->next;
188 }
189 tmp->target_offset = p - base;
190 memcpy(p, e->t, e->t->target_size +
191 sizeof(struct ebt_entry_target));
Bart De Schuymer60332e02002-06-23 08:01:47 +0000192 if (!strcmp(e->t->u.name, EBT_STANDARD_TARGET)) {
193 struct ebt_standard_target *st =
194 (struct ebt_standard_target *)p;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000195 /* translate the jump to a udc */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000196 if (st->verdict >= 0)
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000197 st->verdict = chain_offsets
198 [st->verdict + NF_BR_NUMHOOKS];
Bart De Schuymer60332e02002-06-23 08:01:47 +0000199 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000200 p += e->t->target_size +
201 sizeof(struct ebt_entry_target);
202 tmp->next_offset = p - base;
203 e = e->next;
204 }
Bart De Schuymer60332e02002-06-23 08:01:47 +0000205 if (i >= NF_BR_NUMHOOKS)
206 cl = cl->next;
207 i++;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000208 }
209
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000210 /* sanity check */
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000211 if (p - (char *)new->entries != new->entries_size)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000212 print_bug("Entries_size bug");
Bart De Schuymer60332e02002-06-23 08:01:47 +0000213 free(chain_offsets);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000214 return new;
215}
216
Bart De Schuymer62423742002-07-14 19:06:20 +0000217static void store_table_in_file(char *filename, struct ebt_replace *repl)
218{
219 char *command, *data;
220 int size;
221 FILE *file;
222
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000223 /* start from an empty file with right priviliges */
Bart De Schuymer62423742002-07-14 19:06:20 +0000224 command = (char *)malloc(strlen(filename) + 15);
225 if (!command)
226 print_memory();
227 strcpy(command, "cat /dev/null>");
228 strcpy(command + 14, filename);
229 if (system(command))
230 print_error("Couldn't create file %s", filename);
231 strcpy(command, "chmod 600 ");
232 strcpy(command + 10, filename);
233 if (system(command))
234 print_error("Couldn't chmod file %s", filename);
235 free(command);
236
237 size = sizeof(struct ebt_replace) + repl->entries_size +
238 repl->nentries * sizeof(struct ebt_counter);
239 data = (char *)malloc(size);
240 if (!data)
241 print_memory();
242 memcpy(data, repl, sizeof(struct ebt_replace));
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000243 memcpy(data + sizeof(struct ebt_replace), (char *)repl->entries,
Bart De Schuymer62423742002-07-14 19:06:20 +0000244 repl->entries_size);
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000245 /* initialize counters to zero, deliver_counters() can update them */
Bart De Schuymer62423742002-07-14 19:06:20 +0000246 memset(data + sizeof(struct ebt_replace) + repl->entries_size,
247 0, repl->nentries * sizeof(struct ebt_counter));
248 if (!(file = fopen(filename, "wb")))
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000249 print_error("Couldn't open file %s", filename);
Bart De Schuymer62423742002-07-14 19:06:20 +0000250 if (fwrite(data, sizeof(char), size, file) != size) {
251 fclose(file);
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000252 print_error("Couldn't write everything to file %s", filename);
Bart De Schuymer62423742002-07-14 19:06:20 +0000253 }
254 fclose(file);
255 free(data);
256}
257
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000258void deliver_table(struct ebt_u_replace *u_repl)
259{
260 socklen_t optlen;
261 struct ebt_replace *repl;
262
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000263 /* translate the struct ebt_u_replace to a struct ebt_replace */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000264 repl = translate_user2kernel(u_repl);
Bart De Schuymer62423742002-07-14 19:06:20 +0000265 if (u_repl->filename != NULL) {
266 store_table_in_file(u_repl->filename, repl);
267 return;
268 }
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000269 /* give the data to the kernel */
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000270 optlen = sizeof(struct ebt_replace) + repl->entries_size;
Bart De Schuymer62423742002-07-14 19:06:20 +0000271 get_sockfd();
Bart De Schuymer0cb01792003-05-04 16:52:04 +0000272 if (!setsockopt(sockfd, IPPROTO_IP, EBT_SO_SET_ENTRIES, repl, optlen))
273 return;
274 if (u_repl->command == 8) { /* the ebtables module may not
275 * yet be loaded with --atomic-commit */
276 ebtables_insmod("ebtables");
277 if (!setsockopt(sockfd, IPPROTO_IP, EBT_SO_SET_ENTRIES,
278 repl, optlen))
279 return;
280 }
281
282 print_error("The kernel doesn't support a certain ebtables"
283 " extension, consider recompiling your kernel or insmod"
284 " the extension");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000285}
286
Bart De Schuymer62423742002-07-14 19:06:20 +0000287static void store_counters_in_file(char *filename, struct ebt_u_replace *repl)
288{
289 int size = repl->nentries * sizeof(struct ebt_counter);
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000290 unsigned int entries_size;
Bart De Schuymer62423742002-07-14 19:06:20 +0000291 struct ebt_replace hlp;
292 FILE *file;
293
294 if (!(file = fopen(filename, "r+b")))
295 print_error("Could not open file %s", filename);
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000296 /*
297 * find out entries_size and then set the file pointer to the
298 * counters
299 */
Bart De Schuymer62423742002-07-14 19:06:20 +0000300 if (fseek(file, (char *)(&hlp.entries_size) - (char *)(&hlp), SEEK_SET)
301 || fread(&entries_size, sizeof(char), sizeof(unsigned int), file) !=
302 sizeof(unsigned int) ||
303 fseek(file, entries_size + sizeof(struct ebt_replace), SEEK_SET)) {
304 fclose(file);
305 print_error("File %s is corrupt", filename);
306 }
307 if (fwrite(repl->counters, sizeof(char), size, file) != size) {
308 fclose(file);
309 print_error("Could not write everything to file %s", filename);
310 }
311 fclose(file);
312}
313
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000314/* gets executed after deliver_table */
315void deliver_counters(struct ebt_u_replace *u_repl)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000316{
317 unsigned short *point;
318 struct ebt_counter *old, *new, *newcounters;
319 socklen_t optlen;
320 struct ebt_replace repl;
Bart De Schuymered053432002-07-21 19:35:39 +0000321 unsigned short *counterchanges = u_repl->counterchanges;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000322
323 if (u_repl->nentries == 0)
324 return;
325
326 newcounters = (struct ebt_counter *)
327 malloc(u_repl->nentries * sizeof(struct ebt_counter));
328 if (!newcounters)
329 print_memory();
330 memset(newcounters, 0, u_repl->nentries * sizeof(struct ebt_counter));
331 old = u_repl->counters;
332 new = newcounters;
333 point = counterchanges;
334 while (*point != CNT_END) {
335 if (*point == CNT_NORM) {
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000336 /*
337 *'normal' rule, meaning we didn't do anything to it
338 * So, we just copy
339 */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000340 new->pcnt = old->pcnt;
Bart De Schuymer6d06d262003-05-01 19:38:54 +0000341 new->bcnt = old->bcnt;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000342 /* we've used an old counter */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000343 old++;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000344 /* we've set a new counter */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000345 new++;
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000346 } else if (*point == CNT_DEL) {
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000347 /* don't use this old counter */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000348 old++;
349 } else if (*point == CNT_ADD) {
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000350 /* new counter, let it stay 0 */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000351 new++;
352 } else {
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000353 /* zero it (let it stay 0) */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000354 old++;
355 new++;
356 }
357 point++;
358 }
359
360 free(u_repl->counters);
361 u_repl->counters = newcounters;
362 u_repl->num_counters = u_repl->nentries;
Bart De Schuymer62423742002-07-14 19:06:20 +0000363 if (u_repl->filename != NULL) {
364 store_counters_in_file(u_repl->filename, u_repl);
365 return;
366 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000367 optlen = u_repl->nentries * sizeof(struct ebt_counter) +
368 sizeof(struct ebt_replace);
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000369 /* now put the stuff in the kernel's struct ebt_replace */
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000370 repl.counters = sparc_cast u_repl->counters;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000371 repl.num_counters = u_repl->num_counters;
372 memcpy(repl.name, u_repl->name, sizeof(repl.name));
373
374 get_sockfd();
375 if (setsockopt(sockfd, IPPROTO_IP, EBT_SO_SET_COUNTERS, &repl, optlen))
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000376 print_bug("Couldn't update kernel counters");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000377}
378
379static int
380ebt_translate_match(struct ebt_entry_match *m, struct ebt_u_match_list ***l)
381{
382 struct ebt_u_match_list *new;
383
384 new = (struct ebt_u_match_list *)
385 malloc(sizeof(struct ebt_u_match_list));
386 if (!new)
387 print_memory();
388 new->m = (struct ebt_entry_match *)
389 malloc(m->match_size + sizeof(struct ebt_entry_match));
390 if (!new->m)
391 print_memory();
392 memcpy(new->m, m, m->match_size + sizeof(struct ebt_entry_match));
393 new->next = NULL;
394 **l = new;
395 *l = &new->next;
396 if (find_match(new->m->u.name) == NULL)
397 print_error("Kernel match %s unsupported by userspace tool",
398 new->m->u.name);
399 return 0;
400}
401
402static int
403ebt_translate_watcher(struct ebt_entry_watcher *w,
404 struct ebt_u_watcher_list ***l)
405{
406 struct ebt_u_watcher_list *new;
407
408 new = (struct ebt_u_watcher_list *)
409 malloc(sizeof(struct ebt_u_watcher_list));
410 if (!new)
411 print_memory();
412 new->w = (struct ebt_entry_watcher *)
413 malloc(w->watcher_size + sizeof(struct ebt_entry_watcher));
414 if (!new->w)
415 print_memory();
416 memcpy(new->w, w, w->watcher_size + sizeof(struct ebt_entry_watcher));
417 new->next = NULL;
418 **l = new;
419 *l = &new->next;
420 if (find_watcher(new->w->u.name) == NULL)
421 print_error("Kernel watcher %s unsupported by userspace tool",
422 new->w->u.name);
423 return 0;
424}
425
426static int
427ebt_translate_entry(struct ebt_entry *e, unsigned int *hook, int *n, int *cnt,
428 int *totalcnt, struct ebt_u_entry ***u_e, struct ebt_u_replace *u_repl,
Bart De Schuymer60332e02002-06-23 08:01:47 +0000429 unsigned int valid_hooks, char *base)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000430{
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000431 /* an entry */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000432 if (e->bitmask & EBT_ENTRY_OR_ENTRIES) {
433 struct ebt_u_entry *new;
434 struct ebt_u_match_list **m_l;
435 struct ebt_u_watcher_list **w_l;
436 struct ebt_entry_target *t;
437
438 new = (struct ebt_u_entry *)malloc(sizeof(struct ebt_u_entry));
439 if (!new)
440 print_memory();
441 new->bitmask = e->bitmask;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000442 /*
443 * plain userspace code doesn't know about
444 * EBT_ENTRY_OR_ENTRIES
445 */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000446 new->bitmask &= ~EBT_ENTRY_OR_ENTRIES;
447 new->invflags = e->invflags;
448 new->ethproto = e->ethproto;
Bart De Schuymere3cceb72002-07-26 12:47:33 +0000449 strcpy(new->in, e->in);
450 strcpy(new->out, e->out);
451 strcpy(new->logical_in, e->logical_in);
452 strcpy(new->logical_out, e->logical_out);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000453 memcpy(new->sourcemac, e->sourcemac, sizeof(new->sourcemac));
454 memcpy(new->sourcemsk, e->sourcemsk, sizeof(new->sourcemsk));
455 memcpy(new->destmac, e->destmac, sizeof(new->destmac));
456 memcpy(new->destmsk, e->destmsk, sizeof(new->destmsk));
457 new->m_list = NULL;
458 new->w_list = NULL;
459 new->next = NULL;
460 m_l = &new->m_list;
461 EBT_MATCH_ITERATE(e, ebt_translate_match, &m_l);
462 w_l = &new->w_list;
463 EBT_WATCHER_ITERATE(e, ebt_translate_watcher, &w_l);
464
465 t = (struct ebt_entry_target *)(((char *)e) + e->target_offset);
466 new->t = (struct ebt_entry_target *)
467 malloc(t->target_size + sizeof(struct ebt_entry_target));
468 if (!new->t)
469 print_memory();
470 if (find_target(t->u.name) == NULL)
471 print_error("Kernel target %s unsupported by "
472 "userspace tool", t->u.name);
473 memcpy(new->t, t, t->target_size +
474 sizeof(struct ebt_entry_target));
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000475 /* deal with jumps to udc */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000476 if (!strcmp(t->u.name, EBT_STANDARD_TARGET)) {
477 char *tmp = base;
478 int verdict = ((struct ebt_standard_target *)t)->verdict;
479 int i;
480 struct ebt_u_chain_list *cl;
481
482 if (verdict >= 0) {
483 tmp += verdict;
484 cl = u_repl->udc;
485 i = 0;
486 while (cl && cl->kernel_start != tmp) {
487 i++;
488 cl = cl->next;
489 }
490 if (!cl)
491 print_bug("can't find udc for jump");
492 ((struct ebt_standard_target *)new->t)->verdict = i;
493 }
494 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000495
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000496 /* I love pointers */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000497 **u_e = new;
498 *u_e = &new->next;
499 (*cnt)++;
500 (*totalcnt)++;
501 return 0;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000502 } else { /* a new chain */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000503 int i;
504 struct ebt_entries *entries = (struct ebt_entries *)e;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000505 struct ebt_u_chain_list *cl;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000506
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000507 if (*n != *cnt)
508 print_bug("Nr of entries in the chain is wrong");
509 *n = entries->nentries;
510 *cnt = 0;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000511 for (i = *hook + 1; i < NF_BR_NUMHOOKS; i++)
512 if (valid_hooks & (1 << i))
513 break;
514 *hook = i;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000515 /* makes use of fact that standard chains come before udc */
516 if (i >= NF_BR_NUMHOOKS) { /* udc */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000517 i -= NF_BR_NUMHOOKS;
518 cl = u_repl->udc;
519 while (i-- > 0)
520 cl = cl->next;
521 *u_e = &(cl->udc->entries);
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000522 } else
Bart De Schuymer60332e02002-06-23 08:01:47 +0000523 *u_e = &(u_repl->hook_entry[*hook]->entries);
Bart De Schuymer60332e02002-06-23 08:01:47 +0000524 return 0;
525 }
526}
527
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000528/* initialize all chain headers */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000529static int
530ebt_translate_chains(struct ebt_entry *e, unsigned int *hook,
531 struct ebt_u_replace *u_repl, unsigned int valid_hooks)
532{
533 int i;
534 struct ebt_entries *entries = (struct ebt_entries *)e;
535 struct ebt_u_entries *new;
536 struct ebt_u_chain_list **chain_list;
537
538 if (!(e->bitmask & EBT_ENTRY_OR_ENTRIES)) {
539 for (i = *hook + 1; i < NF_BR_NUMHOOKS; i++)
540 if (valid_hooks & (1 << i))
541 break;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000542 /* makes use of fact that standard chains come before udc */
543 if (i >= NF_BR_NUMHOOKS) { /* udc */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000544 chain_list = &u_repl->udc;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000545 /* add in the back */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000546 while (*chain_list)
547 chain_list = &((*chain_list)->next);
548 *chain_list = (struct ebt_u_chain_list *)
549 malloc(sizeof(struct ebt_u_chain_list));
550 if (!(*chain_list))
551 print_memory();
552 (*chain_list)->next = NULL;
553 (*chain_list)->udc = (struct ebt_u_entries *)
554 malloc(sizeof(struct ebt_u_entries));
555 if (!((*chain_list)->udc))
556 print_memory();
557 new = (*chain_list)->udc;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000558 /*
559 * ebt_translate_entry depends on this for knowing
560 * to which chain is being jumped
561 */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000562 (*chain_list)->kernel_start = (char *)e;
563 } else {
564 *hook = i;
565 new = (struct ebt_u_entries *)
566 malloc(sizeof(struct ebt_u_entries));
567 if (!new)
568 print_memory();
569 u_repl->hook_entry[*hook] = new;
570 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000571 new->nentries = entries->nentries;
572 new->policy = entries->policy;
573 new->entries = NULL;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000574 new->counter_offset = entries->counter_offset;
575 strcpy(new->name, entries->name);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000576 }
Bart De Schuymer60332e02002-06-23 08:01:47 +0000577 return 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000578}
579
Bart De Schuymer62423742002-07-14 19:06:20 +0000580static void retrieve_from_file(char *filename, struct ebt_replace *repl,
581 char command)
582{
583 FILE *file;
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000584 char *hlp = NULL, *entries;
585 struct ebt_counter *counters;
Bart De Schuymer62423742002-07-14 19:06:20 +0000586 int size;
587
588 if (!(file = fopen(filename, "r+b")))
589 print_error("Could not open file %s", filename);
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000590 /*
591 * make sure table name is right if command isn't -L or --atomic-commit
592 */
Bart De Schuymer62423742002-07-14 19:06:20 +0000593 if (command != 'L' && command != 8) {
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000594 hlp = (char *)malloc(strlen(repl->name) + 1);
Bart De Schuymer62423742002-07-14 19:06:20 +0000595 if (!hlp)
596 print_memory();
597 strcpy(hlp, repl->name);
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000598 }
Bart De Schuymer62423742002-07-14 19:06:20 +0000599 if (fread(repl, sizeof(char), sizeof(struct ebt_replace), file)
600 != sizeof(struct ebt_replace))
601 print_error("File %s is corrupt", filename);
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000602 if (command != 'L' && command != 8 && strcmp(hlp, repl->name)) {
603 fclose(file);
Bart De Schuymer62423742002-07-14 19:06:20 +0000604 print_error("File %s contains wrong table name or is corrupt",
605 filename);
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000606 free(hlp);
607 } else if (!find_table(repl->name)) {
608 fclose(file);
609 print_error("File %s contains invalid table name", filename);
610 }
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000611
Bart De Schuymer62423742002-07-14 19:06:20 +0000612 size = sizeof(struct ebt_replace) +
613 repl->nentries * sizeof(struct ebt_counter) + repl->entries_size;
614 fseek(file, 0, SEEK_END);
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000615 if (size != ftell(file)) {
616 fclose(file);
Bart De Schuymer62423742002-07-14 19:06:20 +0000617 print_error("File %s has wrong size", filename);
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000618 }
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000619 entries = (char *)malloc(repl->entries_size);
620 if (!entries)
Bart De Schuymer62423742002-07-14 19:06:20 +0000621 print_memory();
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000622 repl->entries = sparc_cast entries;
Bart De Schuymer62423742002-07-14 19:06:20 +0000623 if (repl->nentries) {
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000624 counters = (struct ebt_counter *)
Bart De Schuymer62423742002-07-14 19:06:20 +0000625 malloc(repl->nentries * sizeof(struct ebt_counter));
Bart De Schuymer8e96bee2003-08-27 16:59:39 +0000626 repl->counters = sparc_cast counters;
Bart De Schuymer62423742002-07-14 19:06:20 +0000627 if (!repl->counters)
628 print_memory();
629 } else
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000630 repl->counters = sparc_cast NULL;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000631 /* copy entries and counters */
Bart De Schuymer62423742002-07-14 19:06:20 +0000632 if (fseek(file, sizeof(struct ebt_replace), SEEK_SET) ||
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000633 fread((char *)repl->entries, sizeof(char), repl->entries_size, file)
Bart De Schuymer62423742002-07-14 19:06:20 +0000634 != repl->entries_size ||
635 fseek(file, sizeof(struct ebt_replace) + repl->entries_size, SEEK_SET)
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000636 || fread((char *)repl->counters, sizeof(char),
Bart De Schuymer62423742002-07-14 19:06:20 +0000637 repl->nentries * sizeof(struct ebt_counter), file)
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000638 != repl->nentries * sizeof(struct ebt_counter)) {
639 fclose(file);
Bart De Schuymer62423742002-07-14 19:06:20 +0000640 print_error("File %s is corrupt", filename);
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000641 }
Bart De Schuymer62423742002-07-14 19:06:20 +0000642 fclose(file);
643}
644
645static int retrieve_from_kernel(struct ebt_replace *repl, char command)
646{
647 socklen_t optlen;
648 int optname;
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000649 char *entries;
Bart De Schuymer62423742002-07-14 19:06:20 +0000650
651 optlen = sizeof(struct ebt_replace);
652 get_sockfd();
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000653 /* --atomic-init || --init-table */
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000654 if (command == 7 || command == 11)
Bart De Schuymer62423742002-07-14 19:06:20 +0000655 optname = EBT_SO_GET_INIT_INFO;
656 else
657 optname = EBT_SO_GET_INFO;
658 if (getsockopt(sockfd, IPPROTO_IP, optname, repl, &optlen))
659 return -1;
660
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000661 if ( !(entries = (char *)malloc(repl->entries_size)) )
Bart De Schuymer62423742002-07-14 19:06:20 +0000662 print_memory();
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000663 repl->entries = sparc_cast entries;
Bart De Schuymer62423742002-07-14 19:06:20 +0000664 if (repl->nentries) {
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000665 struct ebt_counter *counters;
666
667 if (!(counters = (struct ebt_counter *)
Bart De Schuymer62423742002-07-14 19:06:20 +0000668 malloc(repl->nentries * sizeof(struct ebt_counter))) )
669 print_memory();
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000670 repl->counters = sparc_cast counters;
Bart De Schuymer62423742002-07-14 19:06:20 +0000671 }
672 else
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000673 repl->counters = sparc_cast NULL;
Bart De Schuymer62423742002-07-14 19:06:20 +0000674
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000675 /* we want to receive the counters */
Bart De Schuymer62423742002-07-14 19:06:20 +0000676 repl->num_counters = repl->nentries;
677 optlen += repl->entries_size + repl->num_counters *
678 sizeof(struct ebt_counter);
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000679 if (command == 7 || command == 11)
Bart De Schuymer62423742002-07-14 19:06:20 +0000680 optname = EBT_SO_GET_INIT_ENTRIES;
681 else
682 optname = EBT_SO_GET_ENTRIES;
683 if (getsockopt(sockfd, IPPROTO_IP, optname, repl, &optlen))
684 print_bug("hmm, what is wrong??? bug#1");
685
686 return 0;
687}
688
Bart De Schuymer9ce6ee92002-06-14 21:56:35 +0000689int get_table(struct ebt_u_replace *u_repl)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000690{
691 int i, j, k, hook;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000692 struct ebt_replace repl;
693 struct ebt_u_entry **u_e;
694
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000695 strcpy(repl.name, u_repl->name);
Bart De Schuymer62a7fdb2002-08-24 21:01:21 +0000696 if (u_repl->filename != NULL) {
Bart De Schuymer62423742002-07-14 19:06:20 +0000697 retrieve_from_file(u_repl->filename, &repl, u_repl->command);
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000698 /*
699 * -L with a wrong table name should be dealt with silently
700 */
Bart De Schuymer62a7fdb2002-08-24 21:01:21 +0000701 strcpy(u_repl->name, repl.name);
702 } else if (retrieve_from_kernel(&repl, u_repl->command) == -1)
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000703 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000704
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000705 /* translate the struct ebt_replace to a struct ebt_u_replace */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000706 u_repl->valid_hooks = repl.valid_hooks;
707 u_repl->nentries = repl.nentries;
708 u_repl->num_counters = repl.num_counters;
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000709 u_repl->counters = (struct ebt_counter *)repl.counters;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000710 u_repl->udc = NULL;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000711 hook = -1;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000712 EBT_ENTRY_ITERATE(repl.entries, repl.entries_size, ebt_translate_chains,
713 &hook, u_repl, u_repl->valid_hooks);
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000714 i = 0; /* holds the expected nr. of entries for the chain */
715 j = 0; /* holds the up to now counted entries for the chain */
716 /*
717 * holds the total nr. of entries,
718 * should equal u_repl->nentries afterwards
719 */
720 k = 0;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000721 hook = -1;
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000722 EBT_ENTRY_ITERATE((char *)repl.entries, repl.entries_size, ebt_translate_entry,
723 &hook, &i, &j, &k, &u_e, u_repl, u_repl->valid_hooks, (char *)repl.entries);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000724 if (k != u_repl->nentries)
725 print_bug("Wrong total nentries");
Bart De Schuymer9ce6ee92002-06-14 21:56:35 +0000726 return 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000727}