blob: 7f8c5319652bcaec92d9887387157cff8a5bad09 [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 Schuymer8339ff12004-01-14 20:05:27 +0000258void ebt_deliver_table(struct ebt_u_replace *u_repl)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000259{
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 Schuymer8339ff12004-01-14 20:05:27 +0000314/* gets executed after ebt_deliver_table */
315void ebt_deliver_counters(struct ebt_u_replace *u_repl)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000316{
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000317 struct ebt_counter *old, *new, *newcounters;
318 socklen_t optlen;
319 struct ebt_replace repl;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000320 struct ebt_cntchanges *cc = u_repl->counterchanges;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000321
322 if (u_repl->nentries == 0)
323 return;
324
325 newcounters = (struct ebt_counter *)
326 malloc(u_repl->nentries * sizeof(struct ebt_counter));
327 if (!newcounters)
328 print_memory();
329 memset(newcounters, 0, u_repl->nentries * sizeof(struct ebt_counter));
330 old = u_repl->counters;
331 new = newcounters;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000332 while (cc) {
333 if (cc->type == CNT_NORM) {
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000334 /*
335 *'normal' rule, meaning we didn't do anything to it
336 * So, we just copy
337 */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000338 new->pcnt = old->pcnt;
Bart De Schuymer6d06d262003-05-01 19:38:54 +0000339 new->bcnt = old->bcnt;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000340 /* we've used an old counter */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000341 old++;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000342 /* we've set a new counter */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000343 new++;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000344 } else if (cc->type == CNT_DEL) {
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000345 /* don't use this old counter */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000346 old++;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000347 } else if (cc->type == CNT_ADD) {
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000348 /* new counter, let it stay 0 */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000349 new++;
350 } else {
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000351 /* zero it (let it stay 0) */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000352 old++;
353 new++;
354 }
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000355 cc = cc->next;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000356 }
357
358 free(u_repl->counters);
359 u_repl->counters = newcounters;
360 u_repl->num_counters = u_repl->nentries;
Bart De Schuymer62423742002-07-14 19:06:20 +0000361 if (u_repl->filename != NULL) {
362 store_counters_in_file(u_repl->filename, u_repl);
363 return;
364 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000365 optlen = u_repl->nentries * sizeof(struct ebt_counter) +
366 sizeof(struct ebt_replace);
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000367 /* now put the stuff in the kernel's struct ebt_replace */
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000368 repl.counters = sparc_cast u_repl->counters;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000369 repl.num_counters = u_repl->num_counters;
370 memcpy(repl.name, u_repl->name, sizeof(repl.name));
371
372 get_sockfd();
373 if (setsockopt(sockfd, IPPROTO_IP, EBT_SO_SET_COUNTERS, &repl, optlen))
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000374 print_bug("Couldn't update kernel counters");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000375}
376
377static int
378ebt_translate_match(struct ebt_entry_match *m, struct ebt_u_match_list ***l)
379{
380 struct ebt_u_match_list *new;
381
382 new = (struct ebt_u_match_list *)
383 malloc(sizeof(struct ebt_u_match_list));
384 if (!new)
385 print_memory();
386 new->m = (struct ebt_entry_match *)
387 malloc(m->match_size + sizeof(struct ebt_entry_match));
388 if (!new->m)
389 print_memory();
390 memcpy(new->m, m, m->match_size + sizeof(struct ebt_entry_match));
391 new->next = NULL;
392 **l = new;
393 *l = &new->next;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000394 if (ebt_find_match(new->m->u.name) == NULL)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000395 print_error("Kernel match %s unsupported by userspace tool",
396 new->m->u.name);
397 return 0;
398}
399
400static int
401ebt_translate_watcher(struct ebt_entry_watcher *w,
402 struct ebt_u_watcher_list ***l)
403{
404 struct ebt_u_watcher_list *new;
405
406 new = (struct ebt_u_watcher_list *)
407 malloc(sizeof(struct ebt_u_watcher_list));
408 if (!new)
409 print_memory();
410 new->w = (struct ebt_entry_watcher *)
411 malloc(w->watcher_size + sizeof(struct ebt_entry_watcher));
412 if (!new->w)
413 print_memory();
414 memcpy(new->w, w, w->watcher_size + sizeof(struct ebt_entry_watcher));
415 new->next = NULL;
416 **l = new;
417 *l = &new->next;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000418 if (ebt_find_watcher(new->w->u.name) == NULL)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000419 print_error("Kernel watcher %s unsupported by userspace tool",
420 new->w->u.name);
421 return 0;
422}
423
424static int
425ebt_translate_entry(struct ebt_entry *e, unsigned int *hook, int *n, int *cnt,
426 int *totalcnt, struct ebt_u_entry ***u_e, struct ebt_u_replace *u_repl,
Bart De Schuymer60332e02002-06-23 08:01:47 +0000427 unsigned int valid_hooks, char *base)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000428{
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000429 /* an entry */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000430 if (e->bitmask & EBT_ENTRY_OR_ENTRIES) {
431 struct ebt_u_entry *new;
432 struct ebt_u_match_list **m_l;
433 struct ebt_u_watcher_list **w_l;
434 struct ebt_entry_target *t;
435
436 new = (struct ebt_u_entry *)malloc(sizeof(struct ebt_u_entry));
437 if (!new)
438 print_memory();
439 new->bitmask = e->bitmask;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000440 /*
441 * plain userspace code doesn't know about
442 * EBT_ENTRY_OR_ENTRIES
443 */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000444 new->bitmask &= ~EBT_ENTRY_OR_ENTRIES;
445 new->invflags = e->invflags;
446 new->ethproto = e->ethproto;
Bart De Schuymere3cceb72002-07-26 12:47:33 +0000447 strcpy(new->in, e->in);
448 strcpy(new->out, e->out);
449 strcpy(new->logical_in, e->logical_in);
450 strcpy(new->logical_out, e->logical_out);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000451 memcpy(new->sourcemac, e->sourcemac, sizeof(new->sourcemac));
452 memcpy(new->sourcemsk, e->sourcemsk, sizeof(new->sourcemsk));
453 memcpy(new->destmac, e->destmac, sizeof(new->destmac));
454 memcpy(new->destmsk, e->destmsk, sizeof(new->destmsk));
455 new->m_list = NULL;
456 new->w_list = NULL;
457 new->next = NULL;
458 m_l = &new->m_list;
459 EBT_MATCH_ITERATE(e, ebt_translate_match, &m_l);
460 w_l = &new->w_list;
461 EBT_WATCHER_ITERATE(e, ebt_translate_watcher, &w_l);
462
463 t = (struct ebt_entry_target *)(((char *)e) + e->target_offset);
464 new->t = (struct ebt_entry_target *)
465 malloc(t->target_size + sizeof(struct ebt_entry_target));
466 if (!new->t)
467 print_memory();
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000468 if (ebt_find_target(t->u.name) == NULL)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000469 print_error("Kernel target %s unsupported by "
470 "userspace tool", t->u.name);
471 memcpy(new->t, t, t->target_size +
472 sizeof(struct ebt_entry_target));
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000473 /* deal with jumps to udc */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000474 if (!strcmp(t->u.name, EBT_STANDARD_TARGET)) {
475 char *tmp = base;
476 int verdict = ((struct ebt_standard_target *)t)->verdict;
477 int i;
478 struct ebt_u_chain_list *cl;
479
480 if (verdict >= 0) {
481 tmp += verdict;
482 cl = u_repl->udc;
483 i = 0;
484 while (cl && cl->kernel_start != tmp) {
485 i++;
486 cl = cl->next;
487 }
488 if (!cl)
489 print_bug("can't find udc for jump");
490 ((struct ebt_standard_target *)new->t)->verdict = i;
491 }
492 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000493
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000494 /* I love pointers */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000495 **u_e = new;
496 *u_e = &new->next;
497 (*cnt)++;
498 (*totalcnt)++;
499 return 0;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000500 } else { /* a new chain */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000501 int i;
502 struct ebt_entries *entries = (struct ebt_entries *)e;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000503 struct ebt_u_chain_list *cl;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000504
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000505 if (*n != *cnt)
506 print_bug("Nr of entries in the chain is wrong");
507 *n = entries->nentries;
508 *cnt = 0;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000509 for (i = *hook + 1; i < NF_BR_NUMHOOKS; i++)
510 if (valid_hooks & (1 << i))
511 break;
512 *hook = i;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000513 /* makes use of fact that standard chains come before udc */
514 if (i >= NF_BR_NUMHOOKS) { /* udc */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000515 i -= NF_BR_NUMHOOKS;
516 cl = u_repl->udc;
517 while (i-- > 0)
518 cl = cl->next;
519 *u_e = &(cl->udc->entries);
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000520 } else
Bart De Schuymer60332e02002-06-23 08:01:47 +0000521 *u_e = &(u_repl->hook_entry[*hook]->entries);
Bart De Schuymer60332e02002-06-23 08:01:47 +0000522 return 0;
523 }
524}
525
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000526/* initialize all chain headers */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000527static int
528ebt_translate_chains(struct ebt_entry *e, unsigned int *hook,
529 struct ebt_u_replace *u_repl, unsigned int valid_hooks)
530{
531 int i;
532 struct ebt_entries *entries = (struct ebt_entries *)e;
533 struct ebt_u_entries *new;
534 struct ebt_u_chain_list **chain_list;
535
536 if (!(e->bitmask & EBT_ENTRY_OR_ENTRIES)) {
537 for (i = *hook + 1; i < NF_BR_NUMHOOKS; i++)
538 if (valid_hooks & (1 << i))
539 break;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000540 /* makes use of fact that standard chains come before udc */
541 if (i >= NF_BR_NUMHOOKS) { /* udc */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000542 chain_list = &u_repl->udc;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000543 /* add in the back */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000544 while (*chain_list)
545 chain_list = &((*chain_list)->next);
546 *chain_list = (struct ebt_u_chain_list *)
547 malloc(sizeof(struct ebt_u_chain_list));
548 if (!(*chain_list))
549 print_memory();
550 (*chain_list)->next = NULL;
551 (*chain_list)->udc = (struct ebt_u_entries *)
552 malloc(sizeof(struct ebt_u_entries));
553 if (!((*chain_list)->udc))
554 print_memory();
555 new = (*chain_list)->udc;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000556 /*
557 * ebt_translate_entry depends on this for knowing
558 * to which chain is being jumped
559 */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000560 (*chain_list)->kernel_start = (char *)e;
561 } else {
562 *hook = i;
563 new = (struct ebt_u_entries *)
564 malloc(sizeof(struct ebt_u_entries));
565 if (!new)
566 print_memory();
567 u_repl->hook_entry[*hook] = new;
568 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000569 new->nentries = entries->nentries;
570 new->policy = entries->policy;
571 new->entries = NULL;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000572 new->counter_offset = entries->counter_offset;
573 strcpy(new->name, entries->name);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000574 }
Bart De Schuymer60332e02002-06-23 08:01:47 +0000575 return 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000576}
577
Bart De Schuymer62423742002-07-14 19:06:20 +0000578static void retrieve_from_file(char *filename, struct ebt_replace *repl,
579 char command)
580{
581 FILE *file;
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000582 char *hlp = NULL, *entries;
583 struct ebt_counter *counters;
Bart De Schuymer62423742002-07-14 19:06:20 +0000584 int size;
585
586 if (!(file = fopen(filename, "r+b")))
587 print_error("Could not open file %s", filename);
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000588 /*
589 * make sure table name is right if command isn't -L or --atomic-commit
590 */
Bart De Schuymer62423742002-07-14 19:06:20 +0000591 if (command != 'L' && command != 8) {
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000592 hlp = (char *)malloc(strlen(repl->name) + 1);
Bart De Schuymer62423742002-07-14 19:06:20 +0000593 if (!hlp)
594 print_memory();
595 strcpy(hlp, repl->name);
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000596 }
Bart De Schuymer62423742002-07-14 19:06:20 +0000597 if (fread(repl, sizeof(char), sizeof(struct ebt_replace), file)
598 != sizeof(struct ebt_replace))
599 print_error("File %s is corrupt", filename);
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000600 if (command != 'L' && command != 8 && strcmp(hlp, repl->name)) {
601 fclose(file);
Bart De Schuymer62423742002-07-14 19:06:20 +0000602 print_error("File %s contains wrong table name or is corrupt",
603 filename);
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000604 free(hlp);
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000605 } else if (!ebt_find_table(repl->name)) {
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000606 fclose(file);
607 print_error("File %s contains invalid table name", filename);
608 }
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000609
Bart De Schuymer62423742002-07-14 19:06:20 +0000610 size = sizeof(struct ebt_replace) +
611 repl->nentries * sizeof(struct ebt_counter) + repl->entries_size;
612 fseek(file, 0, SEEK_END);
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000613 if (size != ftell(file)) {
614 fclose(file);
Bart De Schuymer62423742002-07-14 19:06:20 +0000615 print_error("File %s has wrong size", filename);
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000616 }
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000617 entries = (char *)malloc(repl->entries_size);
618 if (!entries)
Bart De Schuymer62423742002-07-14 19:06:20 +0000619 print_memory();
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000620 repl->entries = sparc_cast entries;
Bart De Schuymer62423742002-07-14 19:06:20 +0000621 if (repl->nentries) {
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000622 counters = (struct ebt_counter *)
Bart De Schuymer62423742002-07-14 19:06:20 +0000623 malloc(repl->nentries * sizeof(struct ebt_counter));
Bart De Schuymer8e96bee2003-08-27 16:59:39 +0000624 repl->counters = sparc_cast counters;
Bart De Schuymer62423742002-07-14 19:06:20 +0000625 if (!repl->counters)
626 print_memory();
627 } else
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000628 repl->counters = sparc_cast NULL;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000629 /* copy entries and counters */
Bart De Schuymer62423742002-07-14 19:06:20 +0000630 if (fseek(file, sizeof(struct ebt_replace), SEEK_SET) ||
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000631 fread((char *)repl->entries, sizeof(char), repl->entries_size, file)
Bart De Schuymer62423742002-07-14 19:06:20 +0000632 != repl->entries_size ||
633 fseek(file, sizeof(struct ebt_replace) + repl->entries_size, SEEK_SET)
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000634 || fread((char *)repl->counters, sizeof(char),
Bart De Schuymer62423742002-07-14 19:06:20 +0000635 repl->nentries * sizeof(struct ebt_counter), file)
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000636 != repl->nentries * sizeof(struct ebt_counter)) {
637 fclose(file);
Bart De Schuymer62423742002-07-14 19:06:20 +0000638 print_error("File %s is corrupt", filename);
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000639 }
Bart De Schuymer62423742002-07-14 19:06:20 +0000640 fclose(file);
641}
642
643static int retrieve_from_kernel(struct ebt_replace *repl, char command)
644{
645 socklen_t optlen;
646 int optname;
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000647 char *entries;
Bart De Schuymer62423742002-07-14 19:06:20 +0000648
649 optlen = sizeof(struct ebt_replace);
650 get_sockfd();
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000651 /* --atomic-init || --init-table */
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000652 if (command == 7 || command == 11)
Bart De Schuymer62423742002-07-14 19:06:20 +0000653 optname = EBT_SO_GET_INIT_INFO;
654 else
655 optname = EBT_SO_GET_INFO;
656 if (getsockopt(sockfd, IPPROTO_IP, optname, repl, &optlen))
657 return -1;
658
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000659 if ( !(entries = (char *)malloc(repl->entries_size)) )
Bart De Schuymer62423742002-07-14 19:06:20 +0000660 print_memory();
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000661 repl->entries = sparc_cast entries;
Bart De Schuymer62423742002-07-14 19:06:20 +0000662 if (repl->nentries) {
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000663 struct ebt_counter *counters;
664
665 if (!(counters = (struct ebt_counter *)
Bart De Schuymer62423742002-07-14 19:06:20 +0000666 malloc(repl->nentries * sizeof(struct ebt_counter))) )
667 print_memory();
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000668 repl->counters = sparc_cast counters;
Bart De Schuymer62423742002-07-14 19:06:20 +0000669 }
670 else
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000671 repl->counters = sparc_cast NULL;
Bart De Schuymer62423742002-07-14 19:06:20 +0000672
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000673 /* we want to receive the counters */
Bart De Schuymer62423742002-07-14 19:06:20 +0000674 repl->num_counters = repl->nentries;
675 optlen += repl->entries_size + repl->num_counters *
676 sizeof(struct ebt_counter);
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000677 if (command == 7 || command == 11)
Bart De Schuymer62423742002-07-14 19:06:20 +0000678 optname = EBT_SO_GET_INIT_ENTRIES;
679 else
680 optname = EBT_SO_GET_ENTRIES;
681 if (getsockopt(sockfd, IPPROTO_IP, optname, repl, &optlen))
682 print_bug("hmm, what is wrong??? bug#1");
683
684 return 0;
685}
686
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000687int ebt_get_table(struct ebt_u_replace *u_repl)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000688{
689 int i, j, k, hook;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000690 struct ebt_replace repl;
691 struct ebt_u_entry **u_e;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000692 struct ebt_cntchanges *new_cc;
693 struct ebt_cntchanges **prev_cc = &(u_repl->counterchanges);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000694
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 Schuymer8339ff12004-01-14 20:05:27 +0000711 u_repl->counterchanges = NULL;
712 for (i = 0; i < repl.nentries; i++) {
713 new_cc = (struct ebt_cntchanges *)
714 malloc(sizeof(struct ebt_cntchanges));
715 if (!new_cc)
716 print_memory();
717 new_cc->type = CNT_NORM;
718 new_cc->next = NULL;
719 *prev_cc = new_cc;
720 prev_cc = &(new_cc->next);
721 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000722 hook = -1;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000723 EBT_ENTRY_ITERATE(repl.entries, repl.entries_size, ebt_translate_chains,
724 &hook, u_repl, u_repl->valid_hooks);
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000725 i = 0; /* holds the expected nr. of entries for the chain */
726 j = 0; /* holds the up to now counted entries for the chain */
727 /*
728 * holds the total nr. of entries,
729 * should equal u_repl->nentries afterwards
730 */
731 k = 0;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000732 hook = -1;
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000733 EBT_ENTRY_ITERATE((char *)repl.entries, repl.entries_size, ebt_translate_entry,
734 &hook, &i, &j, &k, &u_e, u_repl, u_repl->valid_hooks, (char *)repl.entries);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000735 if (k != u_repl->nentries)
736 print_bug("Wrong total nentries");
Bart De Schuymer9ce6ee92002-06-14 21:56:35 +0000737 return 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000738}