blob: 01b69c994f96e590841b84605d1ffc3a6683c0d5 [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 Schuymer6622a012005-01-19 21:09:05 +000034static int get_sockfd()
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000035{
Bart De Schuymer6622a012005-01-19 21:09:05 +000036 int ret = 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000037 if (sockfd == -1) {
38 sockfd = socket(AF_INET, SOCK_RAW, PF_INET);
Bart De Schuymer6622a012005-01-19 21:09:05 +000039 if (sockfd < 0) {
Bart De Schuymer64182a32004-01-21 20:39:54 +000040 ebt_print_error("Problem getting a socket, "
41 "you probably don't have the right "
42 "permissions");
Bart De Schuymer6622a012005-01-19 21:09:05 +000043 ret = -1;
44 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000045 }
Bart De Schuymer6622a012005-01-19 21:09:05 +000046 return ret;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000047}
48
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +000049static struct ebt_replace *translate_user2kernel(struct ebt_u_replace *u_repl)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000050{
51 struct ebt_replace *new;
52 struct ebt_u_entry *e;
53 struct ebt_u_match_list *m_l;
54 struct ebt_u_watcher_list *w_l;
Bart De Schuymer60332e02002-06-23 08:01:47 +000055 struct ebt_u_entries *entries;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000056 char *p, *base;
57 int i, j;
Bart De Schuymer60332e02002-06-23 08:01:47 +000058 unsigned int entries_size = 0, *chain_offsets;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000059
60 new = (struct ebt_replace *)malloc(sizeof(struct ebt_replace));
61 if (!new)
Bart De Schuymer64182a32004-01-21 20:39:54 +000062 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000063 new->valid_hooks = u_repl->valid_hooks;
Bart De Schuymer0cff9e92002-07-25 12:29:50 +000064 strcpy(new->name, u_repl->name);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000065 new->nentries = u_repl->nentries;
66 new->num_counters = u_repl->num_counters;
Bart De Schuymerf81c2702003-07-23 21:07:04 +000067 new->counters = sparc_cast u_repl->counters;
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +000068 chain_offsets = (unsigned int *)malloc(u_repl->num_chains * sizeof(unsigned int));
Bart De Schuymer6622a012005-01-19 21:09:05 +000069 /* Determine size */
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +000070 for (i = 0; i < u_repl->num_chains; i++) {
71 if (!(entries = u_repl->chains[i]))
72 continue;
Bart De Schuymer60332e02002-06-23 08:01:47 +000073 chain_offsets[i] = entries_size;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000074 entries_size += sizeof(struct ebt_entries);
75 j = 0;
Bart De Schuymer60332e02002-06-23 08:01:47 +000076 e = entries->entries;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000077 while (e) {
78 j++;
79 entries_size += sizeof(struct ebt_entry);
80 m_l = e->m_list;
81 while (m_l) {
82 entries_size += m_l->m->match_size +
83 sizeof(struct ebt_entry_match);
84 m_l = m_l->next;
85 }
86 w_l = e->w_list;
87 while (w_l) {
88 entries_size += w_l->w->watcher_size +
89 sizeof(struct ebt_entry_watcher);
90 w_l = w_l->next;
91 }
92 entries_size += e->t->target_size +
93 sizeof(struct ebt_entry_target);
94 e = e->next;
95 }
Bart De Schuymer6622a012005-01-19 21:09:05 +000096 /* A little sanity check */
Bart De Schuymer60332e02002-06-23 08:01:47 +000097 if (j != entries->nentries)
Bart De Schuymer64182a32004-01-21 20:39:54 +000098 ebt_print_bug("Wrong nentries: %d != %d, hook = %s", j,
Bart De Schuymer60332e02002-06-23 08:01:47 +000099 entries->nentries, entries->name);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000100 }
101
102 new->entries_size = entries_size;
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000103 p = (char *)malloc(entries_size);
104 if (!p)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000105 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000106
Bart De Schuymer6622a012005-01-19 21:09:05 +0000107 /* Put everything in one block */
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000108 new->entries = sparc_cast p;
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000109 for (i = 0; i < u_repl->num_chains; i++) {
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000110 struct ebt_entries *hlp;
111
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000112 hlp = (struct ebt_entries *)p;
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000113 if (!(entries = u_repl->chains[i]))
114 continue;
115 if (i < NF_BR_NUMHOOKS)
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000116 new->hook_entry[i] = sparc_cast hlp;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000117 hlp->nentries = entries->nentries;
118 hlp->policy = entries->policy;
119 strcpy(hlp->name, entries->name);
120 hlp->counter_offset = entries->counter_offset;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000121 hlp->distinguisher = 0; /* Make the kernel see the light */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000122 p += sizeof(struct ebt_entries);
Bart De Schuymer60332e02002-06-23 08:01:47 +0000123 e = entries->entries;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000124 while (e) {
125 struct ebt_entry *tmp = (struct ebt_entry *)p;
126
127 tmp->bitmask = e->bitmask | EBT_ENTRY_OR_ENTRIES;
128 tmp->invflags = e->invflags;
129 tmp->ethproto = e->ethproto;
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000130 strcpy(tmp->in, e->in);
131 strcpy(tmp->out, e->out);
132 strcpy(tmp->logical_in, e->logical_in);
133 strcpy(tmp->logical_out, e->logical_out);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000134 memcpy(tmp->sourcemac, e->sourcemac,
135 sizeof(tmp->sourcemac));
136 memcpy(tmp->sourcemsk, e->sourcemsk,
137 sizeof(tmp->sourcemsk));
138 memcpy(tmp->destmac, e->destmac, sizeof(tmp->destmac));
139 memcpy(tmp->destmsk, e->destmsk, sizeof(tmp->destmsk));
140
141 base = p;
142 p += sizeof(struct ebt_entry);
143 m_l = e->m_list;
144 while (m_l) {
145 memcpy(p, m_l->m, m_l->m->match_size +
146 sizeof(struct ebt_entry_match));
147 p += m_l->m->match_size +
148 sizeof(struct ebt_entry_match);
149 m_l = m_l->next;
150 }
151 tmp->watchers_offset = p - base;
152 w_l = e->w_list;
153 while (w_l) {
154 memcpy(p, w_l->w, w_l->w->watcher_size +
155 sizeof(struct ebt_entry_watcher));
156 p += w_l->w->watcher_size +
157 sizeof(struct ebt_entry_watcher);
158 w_l = w_l->next;
159 }
160 tmp->target_offset = p - base;
161 memcpy(p, e->t, e->t->target_size +
162 sizeof(struct ebt_entry_target));
Bart De Schuymer60332e02002-06-23 08:01:47 +0000163 if (!strcmp(e->t->u.name, EBT_STANDARD_TARGET)) {
164 struct ebt_standard_target *st =
165 (struct ebt_standard_target *)p;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000166 /* Translate the jump to a udc */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000167 if (st->verdict >= 0)
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000168 st->verdict = chain_offsets
169 [st->verdict + NF_BR_NUMHOOKS];
Bart De Schuymer60332e02002-06-23 08:01:47 +0000170 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000171 p += e->t->target_size +
172 sizeof(struct ebt_entry_target);
173 tmp->next_offset = p - base;
174 e = e->next;
175 }
176 }
177
Bart De Schuymer6622a012005-01-19 21:09:05 +0000178 /* Sanity check */
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000179 if (p - (char *)new->entries != new->entries_size)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000180 ebt_print_bug("Entries_size bug");
Bart De Schuymer60332e02002-06-23 08:01:47 +0000181 free(chain_offsets);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000182 return new;
183}
184
Bart De Schuymer62423742002-07-14 19:06:20 +0000185static void store_table_in_file(char *filename, struct ebt_replace *repl)
186{
187 char *command, *data;
188 int size;
189 FILE *file;
190
Bart De Schuymer6622a012005-01-19 21:09:05 +0000191 /* Start from an empty file with right priviliges */
Bart De Schuymer62423742002-07-14 19:06:20 +0000192 command = (char *)malloc(strlen(filename) + 15);
193 if (!command)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000194 ebt_print_memory();
Bart De Schuymer62423742002-07-14 19:06:20 +0000195 strcpy(command, "cat /dev/null>");
196 strcpy(command + 14, filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000197 if (system(command)) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000198 ebt_print_error("Couldn't create file %s", filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000199 goto free_command;
200 }
Bart De Schuymer62423742002-07-14 19:06:20 +0000201 strcpy(command, "chmod 600 ");
202 strcpy(command + 10, filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000203 if (system(command)) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000204 ebt_print_error("Couldn't chmod file %s", filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000205 goto free_command;
206 }
Bart De Schuymer62423742002-07-14 19:06:20 +0000207
208 size = sizeof(struct ebt_replace) + repl->entries_size +
209 repl->nentries * sizeof(struct ebt_counter);
210 data = (char *)malloc(size);
211 if (!data)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000212 ebt_print_memory();
Bart De Schuymer62423742002-07-14 19:06:20 +0000213 memcpy(data, repl, sizeof(struct ebt_replace));
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000214 memcpy(data + sizeof(struct ebt_replace), (char *)repl->entries,
Bart De Schuymer62423742002-07-14 19:06:20 +0000215 repl->entries_size);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000216 /* Initialize counters to zero, deliver_counters() can update them */
Bart De Schuymer62423742002-07-14 19:06:20 +0000217 memset(data + sizeof(struct ebt_replace) + repl->entries_size,
218 0, repl->nentries * sizeof(struct ebt_counter));
Bart De Schuymer6622a012005-01-19 21:09:05 +0000219 if (!(file = fopen(filename, "wb"))) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000220 ebt_print_error("Couldn't open file %s", filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000221 goto free_data;
222 } else if (fwrite(data, sizeof(char), size, file) != size)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000223 ebt_print_error("Couldn't write everything to file %s",
224 filename);
Bart De Schuymer62423742002-07-14 19:06:20 +0000225 fclose(file);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000226free_data:
Bart De Schuymer62423742002-07-14 19:06:20 +0000227 free(data);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000228free_command:
229 free(command);
Bart De Schuymer62423742002-07-14 19:06:20 +0000230}
231
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000232void ebt_deliver_table(struct ebt_u_replace *u_repl)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000233{
234 socklen_t optlen;
235 struct ebt_replace *repl;
236
Bart De Schuymer6622a012005-01-19 21:09:05 +0000237 /* Translate the struct ebt_u_replace to a struct ebt_replace */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000238 repl = translate_user2kernel(u_repl);
Bart De Schuymer62423742002-07-14 19:06:20 +0000239 if (u_repl->filename != NULL) {
240 store_table_in_file(u_repl->filename, repl);
241 return;
242 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000243 /* Give the data to the kernel */
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000244 optlen = sizeof(struct ebt_replace) + repl->entries_size;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000245 if (get_sockfd())
246 return;
Bart De Schuymer0cb01792003-05-04 16:52:04 +0000247 if (!setsockopt(sockfd, IPPROTO_IP, EBT_SO_SET_ENTRIES, repl, optlen))
248 return;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000249 if (u_repl->command == 8) { /* The ebtables module may not
250 * yet be loaded with --atomic-commit */
Bart De Schuymer0cb01792003-05-04 16:52:04 +0000251 ebtables_insmod("ebtables");
252 if (!setsockopt(sockfd, IPPROTO_IP, EBT_SO_SET_ENTRIES,
253 repl, optlen))
254 return;
255 }
256
Bart De Schuymer64182a32004-01-21 20:39:54 +0000257 ebt_print_error("The kernel doesn't support a certain ebtables"
Bart De Schuymer0cb01792003-05-04 16:52:04 +0000258 " extension, consider recompiling your kernel or insmod"
259 " the extension");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000260}
261
Bart De Schuymer6622a012005-01-19 21:09:05 +0000262static int store_counters_in_file(char *filename, struct ebt_u_replace *repl)
Bart De Schuymer62423742002-07-14 19:06:20 +0000263{
Bart De Schuymer6622a012005-01-19 21:09:05 +0000264 int size = repl->nentries * sizeof(struct ebt_counter), ret = 0;
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000265 unsigned int entries_size;
Bart De Schuymer62423742002-07-14 19:06:20 +0000266 struct ebt_replace hlp;
267 FILE *file;
268
Bart De Schuymer6622a012005-01-19 21:09:05 +0000269 if (!(file = fopen(filename, "r+b"))) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000270 ebt_print_error("Could not open file %s", filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000271 return -1;
272 }
273 /* Find out entries_size and then set the file pointer to the
274 * counters */
Bart De Schuymer62423742002-07-14 19:06:20 +0000275 if (fseek(file, (char *)(&hlp.entries_size) - (char *)(&hlp), SEEK_SET)
276 || fread(&entries_size, sizeof(char), sizeof(unsigned int), file) !=
277 sizeof(unsigned int) ||
278 fseek(file, entries_size + sizeof(struct ebt_replace), SEEK_SET)) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000279 ebt_print_error("File %s is corrupt", filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000280 ret = -1;
281 goto close_file;
Bart De Schuymer62423742002-07-14 19:06:20 +0000282 }
283 if (fwrite(repl->counters, sizeof(char), size, file) != size) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000284 ebt_print_error("Could not write everything to file %s",
285 filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000286 ret = -1;
Bart De Schuymer62423742002-07-14 19:06:20 +0000287 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000288close_file:
Bart De Schuymer62423742002-07-14 19:06:20 +0000289 fclose(file);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000290 return 0;
Bart De Schuymer62423742002-07-14 19:06:20 +0000291}
292
Bart De Schuymer6622a012005-01-19 21:09:05 +0000293/* Gets executed after ebt_deliver_table. Delivers the counters to the kernel
294 * and resets the counterchanges to CNT_NORM */
295void ebt_deliver_counters(struct ebt_u_replace *u_repl, int exec_style)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000296{
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000297 struct ebt_counter *old, *new, *newcounters;
298 socklen_t optlen;
299 struct ebt_replace repl;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000300 struct ebt_cntchanges *cc = u_repl->counterchanges, *cc2, **cc3;
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000301 struct ebt_u_entries *entries;
302 struct ebt_u_entry *next = NULL;
303 int i, chainnr = 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000304
305 if (u_repl->nentries == 0)
306 return;
307
308 newcounters = (struct ebt_counter *)
309 malloc(u_repl->nentries * sizeof(struct ebt_counter));
310 if (!newcounters)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000311 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000312 memset(newcounters, 0, u_repl->nentries * sizeof(struct ebt_counter));
313 old = u_repl->counters;
314 new = newcounters;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000315 while (cc) {
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000316 if (!next) {
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000317 while (!(entries = u_repl->chains[chainnr++]))
Bart De Schuymer09a5ea52005-06-18 14:42:44 +0000318 if (chainnr > NF_BR_NUMHOOKS)
319 goto letscontinue;/* Prevent infinite loop for -D x:-1 */
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000320 if (!(next = entries->entries))
321 continue;
322 }
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000323 if (cc->type == CNT_NORM) {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000324 /* 'Normal' rule, meaning we didn't do anything to it
325 * So, we just copy */
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000326 *new = *old;
Bart De Schuymerab611e22005-02-14 20:20:03 +0000327 next->cnt = *new;
328 next->cnt_surplus.pcnt = next->cnt_surplus.bcnt = 0;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000329 /* We've used an old counter */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000330 old++;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000331 /* We've set a new counter */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000332 new++;
Bart De Schuymerff587202005-02-08 20:02:28 +0000333 next = next->next;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000334 } else if (cc->type == CNT_DEL) {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000335 /* Don't use this old counter */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000336 old++;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000337 } else {
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000338 if (cc->type == CNT_CHANGE) {
339 new->pcnt = old->pcnt;
340 if (cc->change % 3 == 1)
341 new->pcnt = old->pcnt + next->cnt_surplus.pcnt;
342 else if (cc->change % 3 == 2)
343 new->pcnt = old->pcnt - next->cnt_surplus.pcnt;
344 else
345 new->pcnt = next->cnt.pcnt;
346 if (cc->change / 3 == 1)
347 new->bcnt = old->bcnt + next->cnt_surplus.bcnt;
348 else if (cc->change / 3 == 2)
349 new->bcnt = old->bcnt - next->cnt_surplus.bcnt;
350 else
351 new->bcnt = next->cnt.bcnt;
Bart De Schuymerab611e22005-02-14 20:20:03 +0000352 } else
353 *new = next->cnt;
354 next->cnt = *new;
355 next->cnt_surplus.pcnt = next->cnt_surplus.bcnt = 0;
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000356 if (cc->type == CNT_ADD)
357 new++;
358 else {
359 old++;
360 new++;
361 }
Bart De Schuymerff587202005-02-08 20:02:28 +0000362 next = next->next;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000363 }
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000364 cc = cc->next;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000365 }
Bart De Schuymer09a5ea52005-06-18 14:42:44 +0000366letscontinue:
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000367
368 free(u_repl->counters);
369 u_repl->counters = newcounters;
370 u_repl->num_counters = u_repl->nentries;
Bart De Schuymer62423742002-07-14 19:06:20 +0000371 if (u_repl->filename != NULL) {
372 store_counters_in_file(u_repl->filename, u_repl);
373 return;
374 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000375 optlen = u_repl->nentries * sizeof(struct ebt_counter) +
376 sizeof(struct ebt_replace);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000377 /* Now put the stuff in the kernel's struct ebt_replace */
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000378 repl.counters = sparc_cast u_repl->counters;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000379 repl.num_counters = u_repl->num_counters;
380 memcpy(repl.name, u_repl->name, sizeof(repl.name));
381
Bart De Schuymer6622a012005-01-19 21:09:05 +0000382 if (get_sockfd())
383 return;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000384 if (setsockopt(sockfd, IPPROTO_IP, EBT_SO_SET_COUNTERS, &repl, optlen))
Bart De Schuymer64182a32004-01-21 20:39:54 +0000385 ebt_print_bug("Couldn't update kernel counters");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000386
387 if (exec_style != EXEC_STYLE_DAEMON)
388 return;
389 /* Reset the counterchanges to CNT_NORM */
390 cc = u_repl->counterchanges;
391 for (i = 0; i < u_repl->nentries; i++) {
392 cc->type = CNT_NORM;
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000393 cc->change = 0;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000394 cc3 = &cc->next;
395 cc = cc->next;
396 }
397 *cc3 = NULL;
398 while (cc) {
399 cc2 = cc->next;
400 free(cc);
401 cc = cc2;
402 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000403}
404
405static int
406ebt_translate_match(struct ebt_entry_match *m, struct ebt_u_match_list ***l)
407{
408 struct ebt_u_match_list *new;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000409 int ret = 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000410
411 new = (struct ebt_u_match_list *)
412 malloc(sizeof(struct ebt_u_match_list));
413 if (!new)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000414 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000415 new->m = (struct ebt_entry_match *)
416 malloc(m->match_size + sizeof(struct ebt_entry_match));
417 if (!new->m)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000418 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000419 memcpy(new->m, m, m->match_size + sizeof(struct ebt_entry_match));
420 new->next = NULL;
421 **l = new;
422 *l = &new->next;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000423 if (ebt_find_match(new->m->u.name) == NULL) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000424 ebt_print_error("Kernel match %s unsupported by userspace tool",
425 new->m->u.name);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000426 ret = -1;
427 }
428 return ret;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000429}
430
431static int
432ebt_translate_watcher(struct ebt_entry_watcher *w,
433 struct ebt_u_watcher_list ***l)
434{
435 struct ebt_u_watcher_list *new;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000436 int ret = 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000437
438 new = (struct ebt_u_watcher_list *)
439 malloc(sizeof(struct ebt_u_watcher_list));
440 if (!new)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000441 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000442 new->w = (struct ebt_entry_watcher *)
443 malloc(w->watcher_size + sizeof(struct ebt_entry_watcher));
444 if (!new->w)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000445 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000446 memcpy(new->w, w, w->watcher_size + sizeof(struct ebt_entry_watcher));
447 new->next = NULL;
448 **l = new;
449 *l = &new->next;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000450 if (ebt_find_watcher(new->w->u.name) == NULL) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000451 ebt_print_error("Kernel watcher %s unsupported by userspace "
452 "tool", new->w->u.name);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000453 ret = -1;
454 }
455 return ret;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000456}
457
458static int
459ebt_translate_entry(struct ebt_entry *e, unsigned int *hook, int *n, int *cnt,
460 int *totalcnt, struct ebt_u_entry ***u_e, struct ebt_u_replace *u_repl,
Bart De Schuymer60332e02002-06-23 08:01:47 +0000461 unsigned int valid_hooks, char *base)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000462{
Bart De Schuymer6622a012005-01-19 21:09:05 +0000463 /* An entry */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000464 if (e->bitmask & EBT_ENTRY_OR_ENTRIES) {
465 struct ebt_u_entry *new;
466 struct ebt_u_match_list **m_l;
467 struct ebt_u_watcher_list **w_l;
468 struct ebt_entry_target *t;
469
470 new = (struct ebt_u_entry *)malloc(sizeof(struct ebt_u_entry));
471 if (!new)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000472 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000473 new->bitmask = e->bitmask;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000474 /*
Bart De Schuymer6622a012005-01-19 21:09:05 +0000475 * Plain userspace code doesn't know about
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000476 * EBT_ENTRY_OR_ENTRIES
477 */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000478 new->bitmask &= ~EBT_ENTRY_OR_ENTRIES;
479 new->invflags = e->invflags;
480 new->ethproto = e->ethproto;
Bart De Schuymere3cceb72002-07-26 12:47:33 +0000481 strcpy(new->in, e->in);
482 strcpy(new->out, e->out);
483 strcpy(new->logical_in, e->logical_in);
484 strcpy(new->logical_out, e->logical_out);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000485 memcpy(new->sourcemac, e->sourcemac, sizeof(new->sourcemac));
486 memcpy(new->sourcemsk, e->sourcemsk, sizeof(new->sourcemsk));
487 memcpy(new->destmac, e->destmac, sizeof(new->destmac));
488 memcpy(new->destmsk, e->destmsk, sizeof(new->destmsk));
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000489 if (*totalcnt >= u_repl->nentries)
490 ebt_print_bug("*totalcnt >= u_repl->nentries");
491 new->cnt = u_repl->counters[*totalcnt];
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000492 new->cnt_surplus.pcnt = new->cnt_surplus.bcnt = 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000493 new->m_list = NULL;
494 new->w_list = NULL;
495 new->next = NULL;
496 m_l = &new->m_list;
497 EBT_MATCH_ITERATE(e, ebt_translate_match, &m_l);
498 w_l = &new->w_list;
499 EBT_WATCHER_ITERATE(e, ebt_translate_watcher, &w_l);
500
501 t = (struct ebt_entry_target *)(((char *)e) + e->target_offset);
502 new->t = (struct ebt_entry_target *)
503 malloc(t->target_size + sizeof(struct ebt_entry_target));
504 if (!new->t)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000505 ebt_print_memory();
Bart De Schuymer6622a012005-01-19 21:09:05 +0000506 if (ebt_find_target(t->u.name) == NULL) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000507 ebt_print_error("Kernel target %s unsupported by "
508 "userspace tool", t->u.name);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000509 return -1;
510 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000511 memcpy(new->t, t, t->target_size +
512 sizeof(struct ebt_entry_target));
Bart De Schuymer6622a012005-01-19 21:09:05 +0000513 /* Deal with jumps to udc */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000514 if (!strcmp(t->u.name, EBT_STANDARD_TARGET)) {
515 char *tmp = base;
516 int verdict = ((struct ebt_standard_target *)t)->verdict;
517 int i;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000518
519 if (verdict >= 0) {
520 tmp += verdict;
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000521 for (i = NF_BR_NUMHOOKS; i < u_repl->num_chains; i++)
522 if (u_repl->chains[i]->kernel_start == tmp)
523 break;
524 if (i == u_repl->num_chains)
525 ebt_print_bug("Can't find udc for jump");
Bart De Schuymer60332e02002-06-23 08:01:47 +0000526 ((struct ebt_standard_target *)new->t)->verdict = i;
527 }
528 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000529
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000530 /* I love pointers */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000531 **u_e = new;
532 *u_e = &new->next;
533 (*cnt)++;
534 (*totalcnt)++;
535 return 0;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000536 } else { /* A new chain */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000537 int i;
538 struct ebt_entries *entries = (struct ebt_entries *)e;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000539
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000540 if (*n != *cnt)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000541 ebt_print_bug("Nr of entries in the chain is wrong");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000542 *n = entries->nentries;
543 *cnt = 0;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000544 for (i = *hook + 1; i < NF_BR_NUMHOOKS; i++)
545 if (valid_hooks & (1 << i))
546 break;
547 *hook = i;
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000548 *u_e = &(u_repl->chains[*hook]->entries);
Bart De Schuymer60332e02002-06-23 08:01:47 +0000549 return 0;
550 }
551}
552
Bart De Schuymer6622a012005-01-19 21:09:05 +0000553/* Initialize all chain headers */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000554static int
555ebt_translate_chains(struct ebt_entry *e, unsigned int *hook,
556 struct ebt_u_replace *u_repl, unsigned int valid_hooks)
557{
558 int i;
559 struct ebt_entries *entries = (struct ebt_entries *)e;
560 struct ebt_u_entries *new;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000561
562 if (!(e->bitmask & EBT_ENTRY_OR_ENTRIES)) {
563 for (i = *hook + 1; i < NF_BR_NUMHOOKS; i++)
564 if (valid_hooks & (1 << i))
565 break;
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000566 new = (struct ebt_u_entries *)malloc(sizeof(struct ebt_u_entries));
567 if (!new)
568 ebt_print_memory();
569 if (i == u_repl->max_chains)
570 ebt_double_chains(u_repl);
571 u_repl->chains[i] = new;
572 if (i >= NF_BR_NUMHOOKS)
573 new->kernel_start = (char *)e;
574 *hook = i;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000575 new->nentries = entries->nentries;
576 new->policy = entries->policy;
577 new->entries = NULL;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000578 new->counter_offset = entries->counter_offset;
579 strcpy(new->name, entries->name);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000580 }
Bart De Schuymer60332e02002-06-23 08:01:47 +0000581 return 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000582}
583
Bart De Schuymer6622a012005-01-19 21:09:05 +0000584static int retrieve_from_file(char *filename, struct ebt_replace *repl,
Bart De Schuymer62423742002-07-14 19:06:20 +0000585 char command)
586{
587 FILE *file;
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000588 char *hlp = NULL, *entries;
589 struct ebt_counter *counters;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000590 int size, ret = 0;
Bart De Schuymer62423742002-07-14 19:06:20 +0000591
Bart De Schuymer6622a012005-01-19 21:09:05 +0000592 if (!(file = fopen(filename, "r+b"))) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000593 ebt_print_error("Could not open file %s", filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000594 return -1;
595 }
596 /* Make sure table name is right if command isn't -L or --atomic-commit */
Bart De Schuymer62423742002-07-14 19:06:20 +0000597 if (command != 'L' && command != 8) {
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000598 hlp = (char *)malloc(strlen(repl->name) + 1);
Bart De Schuymer62423742002-07-14 19:06:20 +0000599 if (!hlp)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000600 ebt_print_memory();
Bart De Schuymer62423742002-07-14 19:06:20 +0000601 strcpy(hlp, repl->name);
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000602 }
Bart De Schuymer62423742002-07-14 19:06:20 +0000603 if (fread(repl, sizeof(char), sizeof(struct ebt_replace), file)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000604 != sizeof(struct ebt_replace)) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000605 ebt_print_error("File %s is corrupt", filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000606 ret = -1;
607 goto close_file;
608 }
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000609 if (command != 'L' && command != 8 && strcmp(hlp, repl->name)) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000610 ebt_print_error("File %s contains wrong table name or is "
611 "corrupt", filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000612 ret = -1;
613 goto close_file;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000614 } else if (!ebt_find_table(repl->name)) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000615 ebt_print_error("File %s contains invalid table name",
616 filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000617 ret = -1;
618 goto close_file;
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000619 }
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000620
Bart De Schuymer62423742002-07-14 19:06:20 +0000621 size = sizeof(struct ebt_replace) +
622 repl->nentries * sizeof(struct ebt_counter) + repl->entries_size;
623 fseek(file, 0, SEEK_END);
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000624 if (size != ftell(file)) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000625 ebt_print_error("File %s has wrong size", filename);
Bart De Schuymer2f7e8d12005-01-19 21:23:02 +0000626 ret = -1;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000627 goto close_file;
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000628 }
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000629 entries = (char *)malloc(repl->entries_size);
630 if (!entries)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000631 ebt_print_memory();
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000632 repl->entries = sparc_cast entries;
Bart De Schuymer62423742002-07-14 19:06:20 +0000633 if (repl->nentries) {
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000634 counters = (struct ebt_counter *)
Bart De Schuymer62423742002-07-14 19:06:20 +0000635 malloc(repl->nentries * sizeof(struct ebt_counter));
Bart De Schuymer8e96bee2003-08-27 16:59:39 +0000636 repl->counters = sparc_cast counters;
Bart De Schuymer62423742002-07-14 19:06:20 +0000637 if (!repl->counters)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000638 ebt_print_memory();
Bart De Schuymer62423742002-07-14 19:06:20 +0000639 } else
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000640 repl->counters = sparc_cast NULL;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000641 /* Copy entries and counters */
Bart De Schuymer62423742002-07-14 19:06:20 +0000642 if (fseek(file, sizeof(struct ebt_replace), SEEK_SET) ||
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000643 fread((char *)repl->entries, sizeof(char), repl->entries_size, file)
Bart De Schuymer62423742002-07-14 19:06:20 +0000644 != repl->entries_size ||
Bart De Schuymer64182a32004-01-21 20:39:54 +0000645 fseek(file, sizeof(struct ebt_replace) + repl->entries_size,
646 SEEK_SET)
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000647 || fread((char *)repl->counters, sizeof(char),
Bart De Schuymer62423742002-07-14 19:06:20 +0000648 repl->nentries * sizeof(struct ebt_counter), file)
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000649 != repl->nentries * sizeof(struct ebt_counter)) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000650 ebt_print_error("File %s is corrupt", filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000651 free(entries);
652 repl->entries = NULL;
653 ret = -1;
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000654 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000655close_file:
Bart De Schuymer62423742002-07-14 19:06:20 +0000656 fclose(file);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000657 free(hlp);
658 return ret;
Bart De Schuymer62423742002-07-14 19:06:20 +0000659}
660
Bart De Schuymer64182a32004-01-21 20:39:54 +0000661static int retrieve_from_kernel(struct ebt_replace *repl, char command,
662 int init)
Bart De Schuymer62423742002-07-14 19:06:20 +0000663{
664 socklen_t optlen;
665 int optname;
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000666 char *entries;
Bart De Schuymer62423742002-07-14 19:06:20 +0000667
668 optlen = sizeof(struct ebt_replace);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000669 if (get_sockfd())
670 return -1;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000671 /* --atomic-init || --init-table */
Bart De Schuymer64182a32004-01-21 20:39:54 +0000672 if (init)
Bart De Schuymer62423742002-07-14 19:06:20 +0000673 optname = EBT_SO_GET_INIT_INFO;
674 else
675 optname = EBT_SO_GET_INFO;
676 if (getsockopt(sockfd, IPPROTO_IP, optname, repl, &optlen))
677 return -1;
678
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000679 if ( !(entries = (char *)malloc(repl->entries_size)) )
Bart De Schuymer64182a32004-01-21 20:39:54 +0000680 ebt_print_memory();
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000681 repl->entries = sparc_cast entries;
Bart De Schuymer62423742002-07-14 19:06:20 +0000682 if (repl->nentries) {
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000683 struct ebt_counter *counters;
684
685 if (!(counters = (struct ebt_counter *)
Bart De Schuymer62423742002-07-14 19:06:20 +0000686 malloc(repl->nentries * sizeof(struct ebt_counter))) )
Bart De Schuymer64182a32004-01-21 20:39:54 +0000687 ebt_print_memory();
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000688 repl->counters = sparc_cast counters;
Bart De Schuymer62423742002-07-14 19:06:20 +0000689 }
690 else
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000691 repl->counters = sparc_cast NULL;
Bart De Schuymer62423742002-07-14 19:06:20 +0000692
Bart De Schuymer6622a012005-01-19 21:09:05 +0000693 /* We want to receive the counters */
Bart De Schuymer62423742002-07-14 19:06:20 +0000694 repl->num_counters = repl->nentries;
695 optlen += repl->entries_size + repl->num_counters *
696 sizeof(struct ebt_counter);
Bart De Schuymer64182a32004-01-21 20:39:54 +0000697 if (init)
Bart De Schuymer62423742002-07-14 19:06:20 +0000698 optname = EBT_SO_GET_INIT_ENTRIES;
699 else
700 optname = EBT_SO_GET_ENTRIES;
701 if (getsockopt(sockfd, IPPROTO_IP, optname, repl, &optlen))
Bart De Schuymer6622a012005-01-19 21:09:05 +0000702 ebt_print_bug("Hmm, what is wrong??? bug#1");
Bart De Schuymer62423742002-07-14 19:06:20 +0000703
704 return 0;
705}
706
Bart De Schuymer64182a32004-01-21 20:39:54 +0000707int ebt_get_table(struct ebt_u_replace *u_repl, int init)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000708{
709 int i, j, k, hook;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000710 struct ebt_replace repl;
711 struct ebt_u_entry **u_e;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000712 struct ebt_cntchanges *new_cc;
713 struct ebt_cntchanges **prev_cc = &(u_repl->counterchanges);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000714
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000715 strcpy(repl.name, u_repl->name);
Bart De Schuymer62a7fdb2002-08-24 21:01:21 +0000716 if (u_repl->filename != NULL) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000717 if (init)
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000718 ebt_print_bug("Getting initial table data from a file is impossible");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000719 if (retrieve_from_file(u_repl->filename, &repl, u_repl->command))
720 return -1;
721 /* -L with a wrong table name should be dealt with silently */
Bart De Schuymer62a7fdb2002-08-24 21:01:21 +0000722 strcpy(u_repl->name, repl.name);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000723 } else if (retrieve_from_kernel(&repl, u_repl->command, init))
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000724 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000725
Bart De Schuymer6622a012005-01-19 21:09:05 +0000726 /* Translate the struct ebt_replace to a struct ebt_u_replace */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000727 u_repl->valid_hooks = repl.valid_hooks;
728 u_repl->nentries = repl.nentries;
729 u_repl->num_counters = repl.num_counters;
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000730 u_repl->counters = repl.counters;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000731 u_repl->counterchanges = NULL;
732 for (i = 0; i < repl.nentries; i++) {
733 new_cc = (struct ebt_cntchanges *)
734 malloc(sizeof(struct ebt_cntchanges));
735 if (!new_cc)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000736 ebt_print_memory();
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000737 new_cc->type = CNT_NORM;
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000738 new_cc->change = 0;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000739 new_cc->next = NULL;
740 *prev_cc = new_cc;
741 prev_cc = &(new_cc->next);
742 }
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000743 u_repl->chains = (struct ebt_u_entries **)calloc(EBT_ORI_MAX_CHAINS, sizeof(void *));
744 u_repl->max_chains = EBT_ORI_MAX_CHAINS;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000745 hook = -1;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000746 /* FIXME: Clean up when an error is encountered */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000747 EBT_ENTRY_ITERATE(repl.entries, repl.entries_size, ebt_translate_chains,
748 &hook, u_repl, u_repl->valid_hooks);
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000749 if (hook >= NF_BR_NUMHOOKS)
750 u_repl->num_chains = hook + 1;
751 else
752 u_repl->num_chains = NF_BR_NUMHOOKS;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000753 i = 0; /* Holds the expected nr. of entries for the chain */
754 j = 0; /* Holds the up to now counted entries for the chain */
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000755 k = 0; /* Holds the total nr. of entries, should equal u_repl->nentries afterwards */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000756 hook = -1;
Bart De Schuymer64182a32004-01-21 20:39:54 +0000757 EBT_ENTRY_ITERATE((char *)repl.entries, repl.entries_size,
758 ebt_translate_entry, &hook, &i, &j, &k, &u_e, u_repl,
759 u_repl->valid_hooks, (char *)repl.entries);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000760 if (k != u_repl->nentries)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000761 ebt_print_bug("Wrong total nentries");
Bart De Schuymer9ce6ee92002-06-14 21:56:35 +0000762 return 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000763}