blob: e7711e430646ee73095fa3c4746f882d65045340 [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 Schuymere94eaf72005-08-28 16:06:22 +000076 e = entries->entries->next;
77 while (e != entries->entries) {
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000078 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 Schuymere94eaf72005-08-28 16:06:22 +0000123 e = entries->entries->next;
124 while (e != entries->entries) {
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000125 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 Schuymer482fbaf2005-08-28 13:16:25 +0000300 struct ebt_cntchanges *cc = u_repl->cc->next, *cc2;
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 Schuymer482fbaf2005-08-28 13:16:25 +0000315 while (cc != u_repl->cc) {
Bart De Schuymere94eaf72005-08-28 16:06:22 +0000316 if (!next || next == entries->entries) {
317 while (chainnr < u_repl->num_chains && (!(entries = u_repl->chains[chainnr++]) ||
318 (next = entries->entries->next) == entries->entries));
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000319 if (chainnr == u_repl->num_chains)
320 break;
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000321 }
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000322 if (cc->type == CNT_NORM) {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000323 /* 'Normal' rule, meaning we didn't do anything to it
324 * So, we just copy */
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000325 *new = *old;
Bart De Schuymerab611e22005-02-14 20:20:03 +0000326 next->cnt = *new;
327 next->cnt_surplus.pcnt = next->cnt_surplus.bcnt = 0;
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000328 old++; /* We've used an old counter */
329 new++; /* We've set a new counter */
Bart De Schuymerff587202005-02-08 20:02:28 +0000330 next = next->next;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000331 } else if (cc->type == CNT_DEL) {
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000332 old++; /* Don't use this old counter */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000333 } else {
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000334 if (cc->type == CNT_CHANGE) {
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000335 if (cc->change % 3 == 1)
336 new->pcnt = old->pcnt + next->cnt_surplus.pcnt;
337 else if (cc->change % 3 == 2)
338 new->pcnt = old->pcnt - next->cnt_surplus.pcnt;
339 else
340 new->pcnt = next->cnt.pcnt;
341 if (cc->change / 3 == 1)
342 new->bcnt = old->bcnt + next->cnt_surplus.bcnt;
343 else if (cc->change / 3 == 2)
344 new->bcnt = old->bcnt - next->cnt_surplus.bcnt;
345 else
346 new->bcnt = next->cnt.bcnt;
Bart De Schuymerab611e22005-02-14 20:20:03 +0000347 } else
348 *new = next->cnt;
349 next->cnt = *new;
350 next->cnt_surplus.pcnt = next->cnt_surplus.bcnt = 0;
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000351 if (cc->type == CNT_ADD)
352 new++;
353 else {
354 old++;
355 new++;
356 }
Bart De Schuymerff587202005-02-08 20:02:28 +0000357 next = next->next;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000358 }
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000359 cc = cc->next;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000360 }
361
362 free(u_repl->counters);
363 u_repl->counters = newcounters;
364 u_repl->num_counters = u_repl->nentries;
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000365 /* Reset the counterchanges to CNT_NORM and delete the unused cc */
366 i = 0;
367 cc = u_repl->cc->next;
368 while (cc != u_repl->cc) {
369 if (cc->type == CNT_DEL) {
370 cc->prev->next = cc->next;
371 cc->next->prev = cc->prev;
372 cc2 = cc->next;
373 free(cc);
374 cc = cc2;
375 } else {
376 cc->type = CNT_NORM;
377 cc->change = 0;
378 i++;
379 cc = cc->next;
380 }
381 }
382 if (i != u_repl->nentries)
383 ebt_print_bug("i != u_repl->nentries");
Bart De Schuymer62423742002-07-14 19:06:20 +0000384 if (u_repl->filename != NULL) {
385 store_counters_in_file(u_repl->filename, u_repl);
386 return;
387 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000388 optlen = u_repl->nentries * sizeof(struct ebt_counter) +
389 sizeof(struct ebt_replace);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000390 /* Now put the stuff in the kernel's struct ebt_replace */
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000391 repl.counters = sparc_cast u_repl->counters;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000392 repl.num_counters = u_repl->num_counters;
393 memcpy(repl.name, u_repl->name, sizeof(repl.name));
394
Bart De Schuymer6622a012005-01-19 21:09:05 +0000395 if (get_sockfd())
396 return;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000397 if (setsockopt(sockfd, IPPROTO_IP, EBT_SO_SET_COUNTERS, &repl, optlen))
Bart De Schuymer64182a32004-01-21 20:39:54 +0000398 ebt_print_bug("Couldn't update kernel counters");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000399
400 if (exec_style != EXEC_STYLE_DAEMON)
401 return;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000402}
403
404static int
405ebt_translate_match(struct ebt_entry_match *m, struct ebt_u_match_list ***l)
406{
407 struct ebt_u_match_list *new;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000408 int ret = 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000409
410 new = (struct ebt_u_match_list *)
411 malloc(sizeof(struct ebt_u_match_list));
412 if (!new)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000413 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000414 new->m = (struct ebt_entry_match *)
415 malloc(m->match_size + sizeof(struct ebt_entry_match));
416 if (!new->m)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000417 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000418 memcpy(new->m, m, m->match_size + sizeof(struct ebt_entry_match));
419 new->next = NULL;
420 **l = new;
421 *l = &new->next;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000422 if (ebt_find_match(new->m->u.name) == NULL) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000423 ebt_print_error("Kernel match %s unsupported by userspace tool",
424 new->m->u.name);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000425 ret = -1;
426 }
427 return ret;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000428}
429
430static int
431ebt_translate_watcher(struct ebt_entry_watcher *w,
432 struct ebt_u_watcher_list ***l)
433{
434 struct ebt_u_watcher_list *new;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000435 int ret = 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000436
437 new = (struct ebt_u_watcher_list *)
438 malloc(sizeof(struct ebt_u_watcher_list));
439 if (!new)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000440 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000441 new->w = (struct ebt_entry_watcher *)
442 malloc(w->watcher_size + sizeof(struct ebt_entry_watcher));
443 if (!new->w)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000444 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000445 memcpy(new->w, w, w->watcher_size + sizeof(struct ebt_entry_watcher));
446 new->next = NULL;
447 **l = new;
448 *l = &new->next;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000449 if (ebt_find_watcher(new->w->u.name) == NULL) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000450 ebt_print_error("Kernel watcher %s unsupported by userspace "
451 "tool", new->w->u.name);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000452 ret = -1;
453 }
454 return ret;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000455}
456
457static int
458ebt_translate_entry(struct ebt_entry *e, unsigned int *hook, int *n, int *cnt,
Bart De Schuymere94eaf72005-08-28 16:06:22 +0000459 int *totalcnt, struct ebt_u_entry **u_e, struct ebt_u_replace *u_repl,
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000460 unsigned int valid_hooks, char *base, struct ebt_cntchanges **cc)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000461{
Bart De Schuymer6622a012005-01-19 21:09:05 +0000462 /* An entry */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000463 if (e->bitmask & EBT_ENTRY_OR_ENTRIES) {
464 struct ebt_u_entry *new;
465 struct ebt_u_match_list **m_l;
466 struct ebt_u_watcher_list **w_l;
467 struct ebt_entry_target *t;
468
469 new = (struct ebt_u_entry *)malloc(sizeof(struct ebt_u_entry));
470 if (!new)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000471 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000472 new->bitmask = e->bitmask;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000473 /*
Bart De Schuymer6622a012005-01-19 21:09:05 +0000474 * Plain userspace code doesn't know about
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000475 * EBT_ENTRY_OR_ENTRIES
476 */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000477 new->bitmask &= ~EBT_ENTRY_OR_ENTRIES;
478 new->invflags = e->invflags;
479 new->ethproto = e->ethproto;
Bart De Schuymere3cceb72002-07-26 12:47:33 +0000480 strcpy(new->in, e->in);
481 strcpy(new->out, e->out);
482 strcpy(new->logical_in, e->logical_in);
483 strcpy(new->logical_out, e->logical_out);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000484 memcpy(new->sourcemac, e->sourcemac, sizeof(new->sourcemac));
485 memcpy(new->sourcemsk, e->sourcemsk, sizeof(new->sourcemsk));
486 memcpy(new->destmac, e->destmac, sizeof(new->destmac));
487 memcpy(new->destmsk, e->destmsk, sizeof(new->destmsk));
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000488 if (*totalcnt >= u_repl->nentries)
489 ebt_print_bug("*totalcnt >= u_repl->nentries");
490 new->cnt = u_repl->counters[*totalcnt];
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000491 new->cnt_surplus.pcnt = new->cnt_surplus.bcnt = 0;
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000492 new->cc = *cc;
493 *cc = (*cc)->next;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000494 new->m_list = NULL;
495 new->w_list = NULL;
Bart De Schuymere94eaf72005-08-28 16:06:22 +0000496 new->next = (*u_e)->next;
497 new->next->prev = new;
498 (*u_e)->next = new;
499 new->prev = *u_e;
500 *u_e = new;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000501 m_l = &new->m_list;
502 EBT_MATCH_ITERATE(e, ebt_translate_match, &m_l);
503 w_l = &new->w_list;
504 EBT_WATCHER_ITERATE(e, ebt_translate_watcher, &w_l);
505
506 t = (struct ebt_entry_target *)(((char *)e) + e->target_offset);
507 new->t = (struct ebt_entry_target *)
508 malloc(t->target_size + sizeof(struct ebt_entry_target));
509 if (!new->t)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000510 ebt_print_memory();
Bart De Schuymer6622a012005-01-19 21:09:05 +0000511 if (ebt_find_target(t->u.name) == NULL) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000512 ebt_print_error("Kernel target %s unsupported by "
513 "userspace tool", t->u.name);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000514 return -1;
515 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000516 memcpy(new->t, t, t->target_size +
517 sizeof(struct ebt_entry_target));
Bart De Schuymer6622a012005-01-19 21:09:05 +0000518 /* Deal with jumps to udc */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000519 if (!strcmp(t->u.name, EBT_STANDARD_TARGET)) {
520 char *tmp = base;
521 int verdict = ((struct ebt_standard_target *)t)->verdict;
522 int i;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000523
524 if (verdict >= 0) {
525 tmp += verdict;
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000526 for (i = NF_BR_NUMHOOKS; i < u_repl->num_chains; i++)
527 if (u_repl->chains[i]->kernel_start == tmp)
528 break;
529 if (i == u_repl->num_chains)
530 ebt_print_bug("Can't find udc for jump");
Bart De Schuymer60332e02002-06-23 08:01:47 +0000531 ((struct ebt_standard_target *)new->t)->verdict = i;
532 }
533 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000534
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000535 (*cnt)++;
536 (*totalcnt)++;
537 return 0;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000538 } else { /* A new chain */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000539 int i;
540 struct ebt_entries *entries = (struct ebt_entries *)e;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000541
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000542 if (*n != *cnt)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000543 ebt_print_bug("Nr of entries in the chain is wrong");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000544 *n = entries->nentries;
545 *cnt = 0;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000546 for (i = *hook + 1; i < NF_BR_NUMHOOKS; i++)
547 if (valid_hooks & (1 << i))
548 break;
549 *hook = i;
Bart De Schuymere94eaf72005-08-28 16:06:22 +0000550 *u_e = u_repl->chains[*hook]->entries;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000551 return 0;
552 }
553}
554
Bart De Schuymer6622a012005-01-19 21:09:05 +0000555/* Initialize all chain headers */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000556static int
557ebt_translate_chains(struct ebt_entry *e, unsigned int *hook,
558 struct ebt_u_replace *u_repl, unsigned int valid_hooks)
559{
560 int i;
561 struct ebt_entries *entries = (struct ebt_entries *)e;
562 struct ebt_u_entries *new;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000563
564 if (!(e->bitmask & EBT_ENTRY_OR_ENTRIES)) {
565 for (i = *hook + 1; i < NF_BR_NUMHOOKS; i++)
566 if (valid_hooks & (1 << i))
567 break;
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000568 new = (struct ebt_u_entries *)malloc(sizeof(struct ebt_u_entries));
569 if (!new)
570 ebt_print_memory();
571 if (i == u_repl->max_chains)
572 ebt_double_chains(u_repl);
573 u_repl->chains[i] = new;
574 if (i >= NF_BR_NUMHOOKS)
575 new->kernel_start = (char *)e;
576 *hook = i;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000577 new->nentries = entries->nentries;
578 new->policy = entries->policy;
Bart De Schuymere94eaf72005-08-28 16:06:22 +0000579 new->entries = (struct ebt_u_entry *)malloc(sizeof(struct ebt_u_entry));
580 if (!new->entries)
581 ebt_print_memory();
582 new->entries->next = new->entries->prev = new->entries;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000583 new->counter_offset = entries->counter_offset;
584 strcpy(new->name, entries->name);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000585 }
Bart De Schuymer60332e02002-06-23 08:01:47 +0000586 return 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000587}
588
Bart De Schuymer6622a012005-01-19 21:09:05 +0000589static int retrieve_from_file(char *filename, struct ebt_replace *repl,
Bart De Schuymer62423742002-07-14 19:06:20 +0000590 char command)
591{
592 FILE *file;
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000593 char *hlp = NULL, *entries;
594 struct ebt_counter *counters;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000595 int size, ret = 0;
Bart De Schuymer62423742002-07-14 19:06:20 +0000596
Bart De Schuymer6622a012005-01-19 21:09:05 +0000597 if (!(file = fopen(filename, "r+b"))) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000598 ebt_print_error("Could not open file %s", filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000599 return -1;
600 }
601 /* Make sure table name is right if command isn't -L or --atomic-commit */
Bart De Schuymer62423742002-07-14 19:06:20 +0000602 if (command != 'L' && command != 8) {
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000603 hlp = (char *)malloc(strlen(repl->name) + 1);
Bart De Schuymer62423742002-07-14 19:06:20 +0000604 if (!hlp)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000605 ebt_print_memory();
Bart De Schuymer62423742002-07-14 19:06:20 +0000606 strcpy(hlp, repl->name);
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000607 }
Bart De Schuymer62423742002-07-14 19:06:20 +0000608 if (fread(repl, sizeof(char), sizeof(struct ebt_replace), file)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000609 != sizeof(struct ebt_replace)) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000610 ebt_print_error("File %s is corrupt", filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000611 ret = -1;
612 goto close_file;
613 }
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000614 if (command != 'L' && command != 8 && strcmp(hlp, repl->name)) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000615 ebt_print_error("File %s contains wrong table name or is "
616 "corrupt", filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000617 ret = -1;
618 goto close_file;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000619 } else if (!ebt_find_table(repl->name)) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000620 ebt_print_error("File %s contains invalid table name",
621 filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000622 ret = -1;
623 goto close_file;
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000624 }
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000625
Bart De Schuymer62423742002-07-14 19:06:20 +0000626 size = sizeof(struct ebt_replace) +
627 repl->nentries * sizeof(struct ebt_counter) + repl->entries_size;
628 fseek(file, 0, SEEK_END);
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000629 if (size != ftell(file)) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000630 ebt_print_error("File %s has wrong size", filename);
Bart De Schuymer2f7e8d12005-01-19 21:23:02 +0000631 ret = -1;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000632 goto close_file;
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000633 }
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000634 entries = (char *)malloc(repl->entries_size);
635 if (!entries)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000636 ebt_print_memory();
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000637 repl->entries = sparc_cast entries;
Bart De Schuymer62423742002-07-14 19:06:20 +0000638 if (repl->nentries) {
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000639 counters = (struct ebt_counter *)
Bart De Schuymer62423742002-07-14 19:06:20 +0000640 malloc(repl->nentries * sizeof(struct ebt_counter));
Bart De Schuymer8e96bee2003-08-27 16:59:39 +0000641 repl->counters = sparc_cast counters;
Bart De Schuymer62423742002-07-14 19:06:20 +0000642 if (!repl->counters)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000643 ebt_print_memory();
Bart De Schuymer62423742002-07-14 19:06:20 +0000644 } else
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000645 repl->counters = sparc_cast NULL;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000646 /* Copy entries and counters */
Bart De Schuymer62423742002-07-14 19:06:20 +0000647 if (fseek(file, sizeof(struct ebt_replace), SEEK_SET) ||
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000648 fread((char *)repl->entries, sizeof(char), repl->entries_size, file)
Bart De Schuymer62423742002-07-14 19:06:20 +0000649 != repl->entries_size ||
Bart De Schuymer64182a32004-01-21 20:39:54 +0000650 fseek(file, sizeof(struct ebt_replace) + repl->entries_size,
651 SEEK_SET)
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000652 || fread((char *)repl->counters, sizeof(char),
Bart De Schuymer62423742002-07-14 19:06:20 +0000653 repl->nentries * sizeof(struct ebt_counter), file)
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000654 != repl->nentries * sizeof(struct ebt_counter)) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000655 ebt_print_error("File %s is corrupt", filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000656 free(entries);
657 repl->entries = NULL;
658 ret = -1;
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000659 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000660close_file:
Bart De Schuymer62423742002-07-14 19:06:20 +0000661 fclose(file);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000662 free(hlp);
663 return ret;
Bart De Schuymer62423742002-07-14 19:06:20 +0000664}
665
Bart De Schuymer64182a32004-01-21 20:39:54 +0000666static int retrieve_from_kernel(struct ebt_replace *repl, char command,
667 int init)
Bart De Schuymer62423742002-07-14 19:06:20 +0000668{
669 socklen_t optlen;
670 int optname;
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000671 char *entries;
Bart De Schuymer62423742002-07-14 19:06:20 +0000672
673 optlen = sizeof(struct ebt_replace);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000674 if (get_sockfd())
675 return -1;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000676 /* --atomic-init || --init-table */
Bart De Schuymer64182a32004-01-21 20:39:54 +0000677 if (init)
Bart De Schuymer62423742002-07-14 19:06:20 +0000678 optname = EBT_SO_GET_INIT_INFO;
679 else
680 optname = EBT_SO_GET_INFO;
681 if (getsockopt(sockfd, IPPROTO_IP, optname, repl, &optlen))
682 return -1;
683
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000684 if ( !(entries = (char *)malloc(repl->entries_size)) )
Bart De Schuymer64182a32004-01-21 20:39:54 +0000685 ebt_print_memory();
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000686 repl->entries = sparc_cast entries;
Bart De Schuymer62423742002-07-14 19:06:20 +0000687 if (repl->nentries) {
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000688 struct ebt_counter *counters;
689
690 if (!(counters = (struct ebt_counter *)
Bart De Schuymer62423742002-07-14 19:06:20 +0000691 malloc(repl->nentries * sizeof(struct ebt_counter))) )
Bart De Schuymer64182a32004-01-21 20:39:54 +0000692 ebt_print_memory();
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000693 repl->counters = sparc_cast counters;
Bart De Schuymer62423742002-07-14 19:06:20 +0000694 }
695 else
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000696 repl->counters = sparc_cast NULL;
Bart De Schuymer62423742002-07-14 19:06:20 +0000697
Bart De Schuymer6622a012005-01-19 21:09:05 +0000698 /* We want to receive the counters */
Bart De Schuymer62423742002-07-14 19:06:20 +0000699 repl->num_counters = repl->nentries;
700 optlen += repl->entries_size + repl->num_counters *
701 sizeof(struct ebt_counter);
Bart De Schuymer64182a32004-01-21 20:39:54 +0000702 if (init)
Bart De Schuymer62423742002-07-14 19:06:20 +0000703 optname = EBT_SO_GET_INIT_ENTRIES;
704 else
705 optname = EBT_SO_GET_ENTRIES;
706 if (getsockopt(sockfd, IPPROTO_IP, optname, repl, &optlen))
Bart De Schuymer6622a012005-01-19 21:09:05 +0000707 ebt_print_bug("Hmm, what is wrong??? bug#1");
Bart De Schuymer62423742002-07-14 19:06:20 +0000708
709 return 0;
710}
711
Bart De Schuymer64182a32004-01-21 20:39:54 +0000712int ebt_get_table(struct ebt_u_replace *u_repl, int init)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000713{
714 int i, j, k, hook;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000715 struct ebt_replace repl;
Bart De Schuymere94eaf72005-08-28 16:06:22 +0000716 struct ebt_u_entry *u_e;
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000717 struct ebt_cntchanges *new_cc, *cc;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000718
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000719 strcpy(repl.name, u_repl->name);
Bart De Schuymer62a7fdb2002-08-24 21:01:21 +0000720 if (u_repl->filename != NULL) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000721 if (init)
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000722 ebt_print_bug("Getting initial table data from a file is impossible");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000723 if (retrieve_from_file(u_repl->filename, &repl, u_repl->command))
724 return -1;
725 /* -L with a wrong table name should be dealt with silently */
Bart De Schuymer62a7fdb2002-08-24 21:01:21 +0000726 strcpy(u_repl->name, repl.name);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000727 } else if (retrieve_from_kernel(&repl, u_repl->command, init))
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000728 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000729
Bart De Schuymer6622a012005-01-19 21:09:05 +0000730 /* Translate the struct ebt_replace to a struct ebt_u_replace */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000731 u_repl->valid_hooks = repl.valid_hooks;
732 u_repl->nentries = repl.nentries;
733 u_repl->num_counters = repl.num_counters;
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000734 u_repl->counters = repl.counters;
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000735 u_repl->cc = (struct ebt_cntchanges *)malloc(sizeof(struct ebt_cntchanges));
736 if (!u_repl->cc)
737 ebt_print_memory();
738 u_repl->cc->next = u_repl->cc->prev = u_repl->cc;
739 cc = u_repl->cc;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000740 for (i = 0; i < repl.nentries; i++) {
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000741 new_cc = (struct ebt_cntchanges *)malloc(sizeof(struct ebt_cntchanges));
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000742 if (!new_cc)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000743 ebt_print_memory();
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000744 new_cc->type = CNT_NORM;
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000745 new_cc->change = 0;
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000746 new_cc->prev = cc;
747 cc->next = new_cc;
748 cc = new_cc;
749 }
750 if (repl.nentries) {
751 new_cc->next = u_repl->cc;
752 u_repl->cc->prev = new_cc;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000753 }
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000754 u_repl->chains = (struct ebt_u_entries **)calloc(EBT_ORI_MAX_CHAINS, sizeof(void *));
755 u_repl->max_chains = EBT_ORI_MAX_CHAINS;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000756 hook = -1;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000757 /* FIXME: Clean up when an error is encountered */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000758 EBT_ENTRY_ITERATE(repl.entries, repl.entries_size, ebt_translate_chains,
759 &hook, u_repl, u_repl->valid_hooks);
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000760 if (hook >= NF_BR_NUMHOOKS)
761 u_repl->num_chains = hook + 1;
762 else
763 u_repl->num_chains = NF_BR_NUMHOOKS;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000764 i = 0; /* Holds the expected nr. of entries for the chain */
765 j = 0; /* Holds the up to now counted entries for the chain */
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000766 k = 0; /* Holds the total nr. of entries, should equal u_repl->nentries afterwards */
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000767 cc = u_repl->cc->next;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000768 hook = -1;
Bart De Schuymer64182a32004-01-21 20:39:54 +0000769 EBT_ENTRY_ITERATE((char *)repl.entries, repl.entries_size,
770 ebt_translate_entry, &hook, &i, &j, &k, &u_e, u_repl,
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000771 u_repl->valid_hooks, (char *)repl.entries, &cc);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000772 if (k != u_repl->nentries)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000773 ebt_print_bug("Wrong total nentries");
Bart De Schuymer9ce6ee92002-06-14 21:56:35 +0000774 return 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000775}