blob: 8216332e36889c60e8900fe5dd8f312330817ec6 [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
49static struct ebt_replace * translate_user2kernel(struct ebt_u_replace *u_repl)
50{
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_chain_list *cl;
56 struct ebt_u_entries *entries;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000057 char *p, *base;
58 int i, j;
Bart De Schuymer60332e02002-06-23 08:01:47 +000059 unsigned int entries_size = 0, *chain_offsets;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000060
61 new = (struct ebt_replace *)malloc(sizeof(struct ebt_replace));
62 if (!new)
Bart De Schuymer64182a32004-01-21 20:39:54 +000063 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000064 new->valid_hooks = u_repl->valid_hooks;
Bart De Schuymer0cff9e92002-07-25 12:29:50 +000065 strcpy(new->name, u_repl->name);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000066 new->nentries = u_repl->nentries;
67 new->num_counters = u_repl->num_counters;
Bart De Schuymerf81c2702003-07-23 21:07:04 +000068 new->counters = sparc_cast u_repl->counters;
Bart De Schuymer6622a012005-01-19 21:09:05 +000069 /* Determine nr of udc */
Bart De Schuymer60332e02002-06-23 08:01:47 +000070 i = 0;
71 cl = u_repl->udc;
72 while (cl) {
73 i++;
74 cl = cl->next;
75 }
76 i += NF_BR_NUMHOOKS;
77 chain_offsets = (unsigned int *)malloc(i * sizeof(unsigned int));
Bart De Schuymer6622a012005-01-19 21:09:05 +000078 /* Determine size */
Bart De Schuymer60332e02002-06-23 08:01:47 +000079 i = 0;
80 cl = u_repl->udc;
81 while (1) {
82 if (i < NF_BR_NUMHOOKS) {
83 if (!(new->valid_hooks & (1 << i))) {
84 i++;
85 continue;
86 }
87 entries = u_repl->hook_entry[i];
88 } else {
89 if (!cl)
90 break;
91 entries = cl->udc;
92 }
93 chain_offsets[i] = entries_size;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000094 entries_size += sizeof(struct ebt_entries);
95 j = 0;
Bart De Schuymer60332e02002-06-23 08:01:47 +000096 e = entries->entries;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000097 while (e) {
98 j++;
99 entries_size += sizeof(struct ebt_entry);
100 m_l = e->m_list;
101 while (m_l) {
102 entries_size += m_l->m->match_size +
103 sizeof(struct ebt_entry_match);
104 m_l = m_l->next;
105 }
106 w_l = e->w_list;
107 while (w_l) {
108 entries_size += w_l->w->watcher_size +
109 sizeof(struct ebt_entry_watcher);
110 w_l = w_l->next;
111 }
112 entries_size += e->t->target_size +
113 sizeof(struct ebt_entry_target);
114 e = e->next;
115 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000116 /* A little sanity check */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000117 if (j != entries->nentries)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000118 ebt_print_bug("Wrong nentries: %d != %d, hook = %s", j,
Bart De Schuymer60332e02002-06-23 08:01:47 +0000119 entries->nentries, entries->name);
120 if (i >= NF_BR_NUMHOOKS)
121 cl = cl->next;
122 i++;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000123 }
124
125 new->entries_size = entries_size;
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000126 p = (char *)malloc(entries_size);
127 if (!p)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000128 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000129
Bart De Schuymer6622a012005-01-19 21:09:05 +0000130 /* Put everything in one block */
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000131 new->entries = sparc_cast p;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000132 i = 0;
133 cl = u_repl->udc;
134 while (1) {
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000135 struct ebt_entries *hlp;
136
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000137 hlp = (struct ebt_entries *)p;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000138 if (i < NF_BR_NUMHOOKS) {
139 if (!(new->valid_hooks & (1 << i))) {
140 i++;
141 continue;
142 }
143 entries = u_repl->hook_entry[i];
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000144 new->hook_entry[i] = sparc_cast hlp;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000145 } else {
146 if (!cl)
147 break;
148 entries = cl->udc;
149 }
150 hlp->nentries = entries->nentries;
151 hlp->policy = entries->policy;
152 strcpy(hlp->name, entries->name);
153 hlp->counter_offset = entries->counter_offset;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000154 hlp->distinguisher = 0; /* Make the kernel see the light */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000155 p += sizeof(struct ebt_entries);
Bart De Schuymer60332e02002-06-23 08:01:47 +0000156 e = entries->entries;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000157 while (e) {
158 struct ebt_entry *tmp = (struct ebt_entry *)p;
159
160 tmp->bitmask = e->bitmask | EBT_ENTRY_OR_ENTRIES;
161 tmp->invflags = e->invflags;
162 tmp->ethproto = e->ethproto;
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000163 strcpy(tmp->in, e->in);
164 strcpy(tmp->out, e->out);
165 strcpy(tmp->logical_in, e->logical_in);
166 strcpy(tmp->logical_out, e->logical_out);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000167 memcpy(tmp->sourcemac, e->sourcemac,
168 sizeof(tmp->sourcemac));
169 memcpy(tmp->sourcemsk, e->sourcemsk,
170 sizeof(tmp->sourcemsk));
171 memcpy(tmp->destmac, e->destmac, sizeof(tmp->destmac));
172 memcpy(tmp->destmsk, e->destmsk, sizeof(tmp->destmsk));
173
174 base = p;
175 p += sizeof(struct ebt_entry);
176 m_l = e->m_list;
177 while (m_l) {
178 memcpy(p, m_l->m, m_l->m->match_size +
179 sizeof(struct ebt_entry_match));
180 p += m_l->m->match_size +
181 sizeof(struct ebt_entry_match);
182 m_l = m_l->next;
183 }
184 tmp->watchers_offset = p - base;
185 w_l = e->w_list;
186 while (w_l) {
187 memcpy(p, w_l->w, w_l->w->watcher_size +
188 sizeof(struct ebt_entry_watcher));
189 p += w_l->w->watcher_size +
190 sizeof(struct ebt_entry_watcher);
191 w_l = w_l->next;
192 }
193 tmp->target_offset = p - base;
194 memcpy(p, e->t, e->t->target_size +
195 sizeof(struct ebt_entry_target));
Bart De Schuymer60332e02002-06-23 08:01:47 +0000196 if (!strcmp(e->t->u.name, EBT_STANDARD_TARGET)) {
197 struct ebt_standard_target *st =
198 (struct ebt_standard_target *)p;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000199 /* Translate the jump to a udc */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000200 if (st->verdict >= 0)
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000201 st->verdict = chain_offsets
202 [st->verdict + NF_BR_NUMHOOKS];
Bart De Schuymer60332e02002-06-23 08:01:47 +0000203 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000204 p += e->t->target_size +
205 sizeof(struct ebt_entry_target);
206 tmp->next_offset = p - base;
207 e = e->next;
208 }
Bart De Schuymer60332e02002-06-23 08:01:47 +0000209 if (i >= NF_BR_NUMHOOKS)
210 cl = cl->next;
211 i++;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000212 }
213
Bart De Schuymer6622a012005-01-19 21:09:05 +0000214 /* Sanity check */
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000215 if (p - (char *)new->entries != new->entries_size)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000216 ebt_print_bug("Entries_size bug");
Bart De Schuymer60332e02002-06-23 08:01:47 +0000217 free(chain_offsets);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000218 return new;
219}
220
Bart De Schuymer62423742002-07-14 19:06:20 +0000221static void store_table_in_file(char *filename, struct ebt_replace *repl)
222{
223 char *command, *data;
224 int size;
225 FILE *file;
226
Bart De Schuymer6622a012005-01-19 21:09:05 +0000227 /* Start from an empty file with right priviliges */
Bart De Schuymer62423742002-07-14 19:06:20 +0000228 command = (char *)malloc(strlen(filename) + 15);
229 if (!command)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000230 ebt_print_memory();
Bart De Schuymer62423742002-07-14 19:06:20 +0000231 strcpy(command, "cat /dev/null>");
232 strcpy(command + 14, filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000233 if (system(command)) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000234 ebt_print_error("Couldn't create file %s", filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000235 goto free_command;
236 }
Bart De Schuymer62423742002-07-14 19:06:20 +0000237 strcpy(command, "chmod 600 ");
238 strcpy(command + 10, filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000239 if (system(command)) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000240 ebt_print_error("Couldn't chmod file %s", filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000241 goto free_command;
242 }
Bart De Schuymer62423742002-07-14 19:06:20 +0000243
244 size = sizeof(struct ebt_replace) + repl->entries_size +
245 repl->nentries * sizeof(struct ebt_counter);
246 data = (char *)malloc(size);
247 if (!data)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000248 ebt_print_memory();
Bart De Schuymer62423742002-07-14 19:06:20 +0000249 memcpy(data, repl, sizeof(struct ebt_replace));
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000250 memcpy(data + sizeof(struct ebt_replace), (char *)repl->entries,
Bart De Schuymer62423742002-07-14 19:06:20 +0000251 repl->entries_size);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000252 /* Initialize counters to zero, deliver_counters() can update them */
Bart De Schuymer62423742002-07-14 19:06:20 +0000253 memset(data + sizeof(struct ebt_replace) + repl->entries_size,
254 0, repl->nentries * sizeof(struct ebt_counter));
Bart De Schuymer6622a012005-01-19 21:09:05 +0000255 if (!(file = fopen(filename, "wb"))) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000256 ebt_print_error("Couldn't open file %s", filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000257 goto free_data;
258 } else if (fwrite(data, sizeof(char), size, file) != size)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000259 ebt_print_error("Couldn't write everything to file %s",
260 filename);
Bart De Schuymer62423742002-07-14 19:06:20 +0000261 fclose(file);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000262free_data:
Bart De Schuymer62423742002-07-14 19:06:20 +0000263 free(data);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000264free_command:
265 free(command);
Bart De Schuymer62423742002-07-14 19:06:20 +0000266}
267
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000268void ebt_deliver_table(struct ebt_u_replace *u_repl)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000269{
270 socklen_t optlen;
271 struct ebt_replace *repl;
272
Bart De Schuymer6622a012005-01-19 21:09:05 +0000273 /* Translate the struct ebt_u_replace to a struct ebt_replace */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000274 repl = translate_user2kernel(u_repl);
Bart De Schuymer62423742002-07-14 19:06:20 +0000275 if (u_repl->filename != NULL) {
276 store_table_in_file(u_repl->filename, repl);
277 return;
278 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000279 /* Give the data to the kernel */
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000280 optlen = sizeof(struct ebt_replace) + repl->entries_size;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000281 if (get_sockfd())
282 return;
Bart De Schuymer0cb01792003-05-04 16:52:04 +0000283 if (!setsockopt(sockfd, IPPROTO_IP, EBT_SO_SET_ENTRIES, repl, optlen))
284 return;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000285 if (u_repl->command == 8) { /* The ebtables module may not
286 * yet be loaded with --atomic-commit */
Bart De Schuymer0cb01792003-05-04 16:52:04 +0000287 ebtables_insmod("ebtables");
288 if (!setsockopt(sockfd, IPPROTO_IP, EBT_SO_SET_ENTRIES,
289 repl, optlen))
290 return;
291 }
292
Bart De Schuymer64182a32004-01-21 20:39:54 +0000293 ebt_print_error("The kernel doesn't support a certain ebtables"
Bart De Schuymer0cb01792003-05-04 16:52:04 +0000294 " extension, consider recompiling your kernel or insmod"
295 " the extension");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000296}
297
Bart De Schuymer6622a012005-01-19 21:09:05 +0000298static int store_counters_in_file(char *filename, struct ebt_u_replace *repl)
Bart De Schuymer62423742002-07-14 19:06:20 +0000299{
Bart De Schuymer6622a012005-01-19 21:09:05 +0000300 int size = repl->nentries * sizeof(struct ebt_counter), ret = 0;
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000301 unsigned int entries_size;
Bart De Schuymer62423742002-07-14 19:06:20 +0000302 struct ebt_replace hlp;
303 FILE *file;
304
Bart De Schuymer6622a012005-01-19 21:09:05 +0000305 if (!(file = fopen(filename, "r+b"))) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000306 ebt_print_error("Could not open file %s", filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000307 return -1;
308 }
309 /* Find out entries_size and then set the file pointer to the
310 * counters */
Bart De Schuymer62423742002-07-14 19:06:20 +0000311 if (fseek(file, (char *)(&hlp.entries_size) - (char *)(&hlp), SEEK_SET)
312 || fread(&entries_size, sizeof(char), sizeof(unsigned int), file) !=
313 sizeof(unsigned int) ||
314 fseek(file, entries_size + sizeof(struct ebt_replace), SEEK_SET)) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000315 ebt_print_error("File %s is corrupt", filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000316 ret = -1;
317 goto close_file;
Bart De Schuymer62423742002-07-14 19:06:20 +0000318 }
319 if (fwrite(repl->counters, sizeof(char), size, file) != size) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000320 ebt_print_error("Could not write everything to file %s",
321 filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000322 ret = -1;
Bart De Schuymer62423742002-07-14 19:06:20 +0000323 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000324close_file:
Bart De Schuymer62423742002-07-14 19:06:20 +0000325 fclose(file);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000326 return 0;
Bart De Schuymer62423742002-07-14 19:06:20 +0000327}
328
Bart De Schuymer6622a012005-01-19 21:09:05 +0000329/* Gets executed after ebt_deliver_table. Delivers the counters to the kernel
330 * and resets the counterchanges to CNT_NORM */
331void ebt_deliver_counters(struct ebt_u_replace *u_repl, int exec_style)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000332{
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000333 struct ebt_counter *old, *new, *newcounters;
334 socklen_t optlen;
335 struct ebt_replace repl;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000336 struct ebt_cntchanges *cc = u_repl->counterchanges, *cc2, **cc3;
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000337 struct ebt_u_entries *entries;
338 struct ebt_u_entry *next = NULL;
339 int i, chainnr = 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000340
341 if (u_repl->nentries == 0)
342 return;
343
344 newcounters = (struct ebt_counter *)
345 malloc(u_repl->nentries * sizeof(struct ebt_counter));
346 if (!newcounters)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000347 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000348 memset(newcounters, 0, u_repl->nentries * sizeof(struct ebt_counter));
349 old = u_repl->counters;
350 new = newcounters;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000351 while (cc) {
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000352 if (!next) {
353 while (!(entries = ebt_nr_to_chain(u_repl, chainnr++)));
354 if (!(next = entries->entries))
355 continue;
356 }
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000357 if (cc->type == CNT_NORM) {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000358 /* 'Normal' rule, meaning we didn't do anything to it
359 * So, we just copy */
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000360 *new = *old;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000361 /* We've used an old counter */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000362 old++;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000363 /* We've set a new counter */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000364 new++;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000365 } else if (cc->type == CNT_DEL) {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000366 /* Don't use this old counter */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000367 old++;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000368 } else {
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000369 *new = next->cnt;
370 if (cc->type == CNT_ADD)
371 new++;
372 else {
373 old++;
374 new++;
375 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000376 }
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000377 cc = cc->next;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000378 }
379
380 free(u_repl->counters);
381 u_repl->counters = newcounters;
382 u_repl->num_counters = u_repl->nentries;
Bart De Schuymer62423742002-07-14 19:06:20 +0000383 if (u_repl->filename != NULL) {
384 store_counters_in_file(u_repl->filename, u_repl);
385 return;
386 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000387 optlen = u_repl->nentries * sizeof(struct ebt_counter) +
388 sizeof(struct ebt_replace);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000389 /* Now put the stuff in the kernel's struct ebt_replace */
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000390 repl.counters = sparc_cast u_repl->counters;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000391 repl.num_counters = u_repl->num_counters;
392 memcpy(repl.name, u_repl->name, sizeof(repl.name));
393
Bart De Schuymer6622a012005-01-19 21:09:05 +0000394 if (get_sockfd())
395 return;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000396 if (setsockopt(sockfd, IPPROTO_IP, EBT_SO_SET_COUNTERS, &repl, optlen))
Bart De Schuymer64182a32004-01-21 20:39:54 +0000397 ebt_print_bug("Couldn't update kernel counters");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000398
399 if (exec_style != EXEC_STYLE_DAEMON)
400 return;
401 /* Reset the counterchanges to CNT_NORM */
402 cc = u_repl->counterchanges;
403 for (i = 0; i < u_repl->nentries; i++) {
404 cc->type = CNT_NORM;
405 cc3 = &cc->next;
406 cc = cc->next;
407 }
408 *cc3 = NULL;
409 while (cc) {
410 cc2 = cc->next;
411 free(cc);
412 cc = cc2;
413 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000414}
415
416static int
417ebt_translate_match(struct ebt_entry_match *m, struct ebt_u_match_list ***l)
418{
419 struct ebt_u_match_list *new;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000420 int ret = 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000421
422 new = (struct ebt_u_match_list *)
423 malloc(sizeof(struct ebt_u_match_list));
424 if (!new)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000425 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000426 new->m = (struct ebt_entry_match *)
427 malloc(m->match_size + sizeof(struct ebt_entry_match));
428 if (!new->m)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000429 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000430 memcpy(new->m, m, m->match_size + sizeof(struct ebt_entry_match));
431 new->next = NULL;
432 **l = new;
433 *l = &new->next;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000434 if (ebt_find_match(new->m->u.name) == NULL) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000435 ebt_print_error("Kernel match %s unsupported by userspace tool",
436 new->m->u.name);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000437 ret = -1;
438 }
439 return ret;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000440}
441
442static int
443ebt_translate_watcher(struct ebt_entry_watcher *w,
444 struct ebt_u_watcher_list ***l)
445{
446 struct ebt_u_watcher_list *new;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000447 int ret = 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000448
449 new = (struct ebt_u_watcher_list *)
450 malloc(sizeof(struct ebt_u_watcher_list));
451 if (!new)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000452 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000453 new->w = (struct ebt_entry_watcher *)
454 malloc(w->watcher_size + sizeof(struct ebt_entry_watcher));
455 if (!new->w)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000456 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000457 memcpy(new->w, w, w->watcher_size + sizeof(struct ebt_entry_watcher));
458 new->next = NULL;
459 **l = new;
460 *l = &new->next;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000461 if (ebt_find_watcher(new->w->u.name) == NULL) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000462 ebt_print_error("Kernel watcher %s unsupported by userspace "
463 "tool", new->w->u.name);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000464 ret = -1;
465 }
466 return ret;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000467}
468
469static int
470ebt_translate_entry(struct ebt_entry *e, unsigned int *hook, int *n, int *cnt,
471 int *totalcnt, struct ebt_u_entry ***u_e, struct ebt_u_replace *u_repl,
Bart De Schuymer60332e02002-06-23 08:01:47 +0000472 unsigned int valid_hooks, char *base)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000473{
Bart De Schuymer6622a012005-01-19 21:09:05 +0000474 /* An entry */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000475 if (e->bitmask & EBT_ENTRY_OR_ENTRIES) {
476 struct ebt_u_entry *new;
477 struct ebt_u_match_list **m_l;
478 struct ebt_u_watcher_list **w_l;
479 struct ebt_entry_target *t;
480
481 new = (struct ebt_u_entry *)malloc(sizeof(struct ebt_u_entry));
482 if (!new)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000483 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000484 new->bitmask = e->bitmask;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000485 /*
Bart De Schuymer6622a012005-01-19 21:09:05 +0000486 * Plain userspace code doesn't know about
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000487 * EBT_ENTRY_OR_ENTRIES
488 */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000489 new->bitmask &= ~EBT_ENTRY_OR_ENTRIES;
490 new->invflags = e->invflags;
491 new->ethproto = e->ethproto;
Bart De Schuymere3cceb72002-07-26 12:47:33 +0000492 strcpy(new->in, e->in);
493 strcpy(new->out, e->out);
494 strcpy(new->logical_in, e->logical_in);
495 strcpy(new->logical_out, e->logical_out);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000496 memcpy(new->sourcemac, e->sourcemac, sizeof(new->sourcemac));
497 memcpy(new->sourcemsk, e->sourcemsk, sizeof(new->sourcemsk));
498 memcpy(new->destmac, e->destmac, sizeof(new->destmac));
499 memcpy(new->destmsk, e->destmsk, sizeof(new->destmsk));
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000500 if (*totalcnt >= u_repl->nentries)
501 ebt_print_bug("*totalcnt >= u_repl->nentries");
502 new->cnt = u_repl->counters[*totalcnt];
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000503 new->m_list = NULL;
504 new->w_list = NULL;
505 new->next = NULL;
506 m_l = &new->m_list;
507 EBT_MATCH_ITERATE(e, ebt_translate_match, &m_l);
508 w_l = &new->w_list;
509 EBT_WATCHER_ITERATE(e, ebt_translate_watcher, &w_l);
510
511 t = (struct ebt_entry_target *)(((char *)e) + e->target_offset);
512 new->t = (struct ebt_entry_target *)
513 malloc(t->target_size + sizeof(struct ebt_entry_target));
514 if (!new->t)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000515 ebt_print_memory();
Bart De Schuymer6622a012005-01-19 21:09:05 +0000516 if (ebt_find_target(t->u.name) == NULL) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000517 ebt_print_error("Kernel target %s unsupported by "
518 "userspace tool", t->u.name);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000519 return -1;
520 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000521 memcpy(new->t, t, t->target_size +
522 sizeof(struct ebt_entry_target));
Bart De Schuymer6622a012005-01-19 21:09:05 +0000523 /* Deal with jumps to udc */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000524 if (!strcmp(t->u.name, EBT_STANDARD_TARGET)) {
525 char *tmp = base;
526 int verdict = ((struct ebt_standard_target *)t)->verdict;
527 int i;
528 struct ebt_u_chain_list *cl;
529
530 if (verdict >= 0) {
531 tmp += verdict;
532 cl = u_repl->udc;
533 i = 0;
534 while (cl && cl->kernel_start != tmp) {
535 i++;
536 cl = cl->next;
537 }
538 if (!cl)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000539 ebt_print_bug("Can't find udc for "
Bart De Schuymer64182a32004-01-21 20:39:54 +0000540 "jump");
Bart De Schuymer60332e02002-06-23 08:01:47 +0000541 ((struct ebt_standard_target *)new->t)->verdict = i;
542 }
543 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000544
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000545 /* I love pointers */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000546 **u_e = new;
547 *u_e = &new->next;
548 (*cnt)++;
549 (*totalcnt)++;
550 return 0;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000551 } else { /* A new chain */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000552 int i;
553 struct ebt_entries *entries = (struct ebt_entries *)e;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000554 struct ebt_u_chain_list *cl;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000555
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000556 if (*n != *cnt)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000557 ebt_print_bug("Nr of entries in the chain is wrong");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000558 *n = entries->nentries;
559 *cnt = 0;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000560 for (i = *hook + 1; i < NF_BR_NUMHOOKS; i++)
561 if (valid_hooks & (1 << i))
562 break;
563 *hook = i;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000564 /* Makes use of fact that standard chains come before udc */
565 if (i >= NF_BR_NUMHOOKS) { /* Udc */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000566 i -= NF_BR_NUMHOOKS;
567 cl = u_repl->udc;
568 while (i-- > 0)
569 cl = cl->next;
570 *u_e = &(cl->udc->entries);
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000571 } else
Bart De Schuymer60332e02002-06-23 08:01:47 +0000572 *u_e = &(u_repl->hook_entry[*hook]->entries);
Bart De Schuymer60332e02002-06-23 08:01:47 +0000573 return 0;
574 }
575}
576
Bart De Schuymer6622a012005-01-19 21:09:05 +0000577/* Initialize all chain headers */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000578static int
579ebt_translate_chains(struct ebt_entry *e, unsigned int *hook,
580 struct ebt_u_replace *u_repl, unsigned int valid_hooks)
581{
582 int i;
583 struct ebt_entries *entries = (struct ebt_entries *)e;
584 struct ebt_u_entries *new;
585 struct ebt_u_chain_list **chain_list;
586
587 if (!(e->bitmask & EBT_ENTRY_OR_ENTRIES)) {
588 for (i = *hook + 1; i < NF_BR_NUMHOOKS; i++)
589 if (valid_hooks & (1 << i))
590 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000591 /* Makes use of fact that standard chains come before udc */
592 if (i >= NF_BR_NUMHOOKS) { /* Udc */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000593 chain_list = &u_repl->udc;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000594 /* Add in the back */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000595 while (*chain_list)
596 chain_list = &((*chain_list)->next);
597 *chain_list = (struct ebt_u_chain_list *)
598 malloc(sizeof(struct ebt_u_chain_list));
599 if (!(*chain_list))
Bart De Schuymer64182a32004-01-21 20:39:54 +0000600 ebt_print_memory();
Bart De Schuymer60332e02002-06-23 08:01:47 +0000601 (*chain_list)->next = NULL;
602 (*chain_list)->udc = (struct ebt_u_entries *)
603 malloc(sizeof(struct ebt_u_entries));
604 if (!((*chain_list)->udc))
Bart De Schuymer64182a32004-01-21 20:39:54 +0000605 ebt_print_memory();
Bart De Schuymer60332e02002-06-23 08:01:47 +0000606 new = (*chain_list)->udc;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000607 /* ebt_translate_entry depends on this for knowing
608 * to which chain is being jumped */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000609 (*chain_list)->kernel_start = (char *)e;
610 } else {
611 *hook = i;
612 new = (struct ebt_u_entries *)
613 malloc(sizeof(struct ebt_u_entries));
614 if (!new)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000615 ebt_print_memory();
Bart De Schuymer60332e02002-06-23 08:01:47 +0000616 u_repl->hook_entry[*hook] = new;
617 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000618 new->nentries = entries->nentries;
619 new->policy = entries->policy;
620 new->entries = NULL;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000621 new->counter_offset = entries->counter_offset;
622 strcpy(new->name, entries->name);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000623 }
Bart De Schuymer60332e02002-06-23 08:01:47 +0000624 return 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000625}
626
Bart De Schuymer6622a012005-01-19 21:09:05 +0000627static int retrieve_from_file(char *filename, struct ebt_replace *repl,
Bart De Schuymer62423742002-07-14 19:06:20 +0000628 char command)
629{
630 FILE *file;
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000631 char *hlp = NULL, *entries;
632 struct ebt_counter *counters;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000633 int size, ret = 0;
Bart De Schuymer62423742002-07-14 19:06:20 +0000634
Bart De Schuymer6622a012005-01-19 21:09:05 +0000635 if (!(file = fopen(filename, "r+b"))) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000636 ebt_print_error("Could not open file %s", filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000637 return -1;
638 }
639 /* Make sure table name is right if command isn't -L or --atomic-commit */
Bart De Schuymer62423742002-07-14 19:06:20 +0000640 if (command != 'L' && command != 8) {
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000641 hlp = (char *)malloc(strlen(repl->name) + 1);
Bart De Schuymer62423742002-07-14 19:06:20 +0000642 if (!hlp)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000643 ebt_print_memory();
Bart De Schuymer62423742002-07-14 19:06:20 +0000644 strcpy(hlp, repl->name);
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000645 }
Bart De Schuymer62423742002-07-14 19:06:20 +0000646 if (fread(repl, sizeof(char), sizeof(struct ebt_replace), file)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000647 != sizeof(struct ebt_replace)) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000648 ebt_print_error("File %s is corrupt", filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000649 ret = -1;
650 goto close_file;
651 }
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000652 if (command != 'L' && command != 8 && strcmp(hlp, repl->name)) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000653 ebt_print_error("File %s contains wrong table name or is "
654 "corrupt", filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000655 ret = -1;
656 goto close_file;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000657 } else if (!ebt_find_table(repl->name)) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000658 ebt_print_error("File %s contains invalid table name",
659 filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000660 ret = -1;
661 goto close_file;
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000662 }
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000663
Bart De Schuymer62423742002-07-14 19:06:20 +0000664 size = sizeof(struct ebt_replace) +
665 repl->nentries * sizeof(struct ebt_counter) + repl->entries_size;
666 fseek(file, 0, SEEK_END);
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000667 if (size != ftell(file)) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000668 ebt_print_error("File %s has wrong size", filename);
Bart De Schuymer2f7e8d12005-01-19 21:23:02 +0000669 ret = -1;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000670 goto close_file;
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000671 }
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000672 entries = (char *)malloc(repl->entries_size);
673 if (!entries)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000674 ebt_print_memory();
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000675 repl->entries = sparc_cast entries;
Bart De Schuymer62423742002-07-14 19:06:20 +0000676 if (repl->nentries) {
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000677 counters = (struct ebt_counter *)
Bart De Schuymer62423742002-07-14 19:06:20 +0000678 malloc(repl->nentries * sizeof(struct ebt_counter));
Bart De Schuymer8e96bee2003-08-27 16:59:39 +0000679 repl->counters = sparc_cast counters;
Bart De Schuymer62423742002-07-14 19:06:20 +0000680 if (!repl->counters)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000681 ebt_print_memory();
Bart De Schuymer62423742002-07-14 19:06:20 +0000682 } else
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000683 repl->counters = sparc_cast NULL;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000684 /* Copy entries and counters */
Bart De Schuymer62423742002-07-14 19:06:20 +0000685 if (fseek(file, sizeof(struct ebt_replace), SEEK_SET) ||
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000686 fread((char *)repl->entries, sizeof(char), repl->entries_size, file)
Bart De Schuymer62423742002-07-14 19:06:20 +0000687 != repl->entries_size ||
Bart De Schuymer64182a32004-01-21 20:39:54 +0000688 fseek(file, sizeof(struct ebt_replace) + repl->entries_size,
689 SEEK_SET)
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000690 || fread((char *)repl->counters, sizeof(char),
Bart De Schuymer62423742002-07-14 19:06:20 +0000691 repl->nentries * sizeof(struct ebt_counter), file)
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000692 != repl->nentries * sizeof(struct ebt_counter)) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000693 ebt_print_error("File %s is corrupt", filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000694 free(entries);
695 repl->entries = NULL;
696 ret = -1;
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000697 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000698close_file:
Bart De Schuymer62423742002-07-14 19:06:20 +0000699 fclose(file);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000700 free(hlp);
701 return ret;
Bart De Schuymer62423742002-07-14 19:06:20 +0000702}
703
Bart De Schuymer64182a32004-01-21 20:39:54 +0000704static int retrieve_from_kernel(struct ebt_replace *repl, char command,
705 int init)
Bart De Schuymer62423742002-07-14 19:06:20 +0000706{
707 socklen_t optlen;
708 int optname;
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000709 char *entries;
Bart De Schuymer62423742002-07-14 19:06:20 +0000710
711 optlen = sizeof(struct ebt_replace);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000712 if (get_sockfd())
713 return -1;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000714 /* --atomic-init || --init-table */
Bart De Schuymer64182a32004-01-21 20:39:54 +0000715 if (init)
Bart De Schuymer62423742002-07-14 19:06:20 +0000716 optname = EBT_SO_GET_INIT_INFO;
717 else
718 optname = EBT_SO_GET_INFO;
719 if (getsockopt(sockfd, IPPROTO_IP, optname, repl, &optlen))
720 return -1;
721
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000722 if ( !(entries = (char *)malloc(repl->entries_size)) )
Bart De Schuymer64182a32004-01-21 20:39:54 +0000723 ebt_print_memory();
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000724 repl->entries = sparc_cast entries;
Bart De Schuymer62423742002-07-14 19:06:20 +0000725 if (repl->nentries) {
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000726 struct ebt_counter *counters;
727
728 if (!(counters = (struct ebt_counter *)
Bart De Schuymer62423742002-07-14 19:06:20 +0000729 malloc(repl->nentries * sizeof(struct ebt_counter))) )
Bart De Schuymer64182a32004-01-21 20:39:54 +0000730 ebt_print_memory();
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000731 repl->counters = sparc_cast counters;
Bart De Schuymer62423742002-07-14 19:06:20 +0000732 }
733 else
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000734 repl->counters = sparc_cast NULL;
Bart De Schuymer62423742002-07-14 19:06:20 +0000735
Bart De Schuymer6622a012005-01-19 21:09:05 +0000736 /* We want to receive the counters */
Bart De Schuymer62423742002-07-14 19:06:20 +0000737 repl->num_counters = repl->nentries;
738 optlen += repl->entries_size + repl->num_counters *
739 sizeof(struct ebt_counter);
Bart De Schuymer64182a32004-01-21 20:39:54 +0000740 if (init)
Bart De Schuymer62423742002-07-14 19:06:20 +0000741 optname = EBT_SO_GET_INIT_ENTRIES;
742 else
743 optname = EBT_SO_GET_ENTRIES;
744 if (getsockopt(sockfd, IPPROTO_IP, optname, repl, &optlen))
Bart De Schuymer6622a012005-01-19 21:09:05 +0000745 ebt_print_bug("Hmm, what is wrong??? bug#1");
Bart De Schuymer62423742002-07-14 19:06:20 +0000746
747 return 0;
748}
749
Bart De Schuymer64182a32004-01-21 20:39:54 +0000750int ebt_get_table(struct ebt_u_replace *u_repl, int init)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000751{
752 int i, j, k, hook;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000753 struct ebt_replace repl;
754 struct ebt_u_entry **u_e;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000755 struct ebt_cntchanges *new_cc;
756 struct ebt_cntchanges **prev_cc = &(u_repl->counterchanges);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000757
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000758 strcpy(repl.name, u_repl->name);
Bart De Schuymer62a7fdb2002-08-24 21:01:21 +0000759 if (u_repl->filename != NULL) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000760 if (init)
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000761 ebt_print_bug("Getting initial table data from a file is impossible");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000762 if (retrieve_from_file(u_repl->filename, &repl, u_repl->command))
763 return -1;
764 /* -L with a wrong table name should be dealt with silently */
Bart De Schuymer62a7fdb2002-08-24 21:01:21 +0000765 strcpy(u_repl->name, repl.name);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000766 } else if (retrieve_from_kernel(&repl, u_repl->command, init))
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000767 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000768
Bart De Schuymer6622a012005-01-19 21:09:05 +0000769 /* Translate the struct ebt_replace to a struct ebt_u_replace */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000770 u_repl->valid_hooks = repl.valid_hooks;
771 u_repl->nentries = repl.nentries;
772 u_repl->num_counters = repl.num_counters;
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000773 u_repl->counters = repl.counters;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000774 u_repl->udc = NULL;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000775 u_repl->counterchanges = NULL;
776 for (i = 0; i < repl.nentries; i++) {
777 new_cc = (struct ebt_cntchanges *)
778 malloc(sizeof(struct ebt_cntchanges));
779 if (!new_cc)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000780 ebt_print_memory();
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000781 new_cc->type = CNT_NORM;
782 new_cc->next = NULL;
783 *prev_cc = new_cc;
784 prev_cc = &(new_cc->next);
785 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000786 hook = -1;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000787 /* FIXME: Clean up when an error is encountered */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000788 EBT_ENTRY_ITERATE(repl.entries, repl.entries_size, ebt_translate_chains,
789 &hook, u_repl, u_repl->valid_hooks);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000790 i = 0; /* Holds the expected nr. of entries for the chain */
791 j = 0; /* Holds the up to now counted entries for the chain */
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000792 k = 0; /* Holds the total nr. of entries, should equal u_repl->nentries afterwards */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000793 hook = -1;
Bart De Schuymer64182a32004-01-21 20:39:54 +0000794 EBT_ENTRY_ITERATE((char *)repl.entries, repl.entries_size,
795 ebt_translate_entry, &hook, &i, &j, &k, &u_e, u_repl,
796 u_repl->valid_hooks, (char *)repl.entries);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000797 if (k != u_repl->nentries)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000798 ebt_print_bug("Wrong total nentries");
Bart De Schuymer9ce6ee92002-06-14 21:56:35 +0000799 return 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000800}