blob: e93217a43e0633ad27b6c55ff2d1fa24f4526ad1 [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 Schuymerff587202005-02-08 20:02:28 +0000365 next = next->next;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000366 } else if (cc->type == CNT_DEL) {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000367 /* Don't use this old counter */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000368 old++;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000369 } else {
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000370 *new = next->cnt;
371 if (cc->type == CNT_ADD)
372 new++;
373 else {
374 old++;
375 new++;
376 }
Bart De Schuymerff587202005-02-08 20:02:28 +0000377 next = next->next;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000378 }
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000379 cc = cc->next;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000380 }
381
382 free(u_repl->counters);
383 u_repl->counters = newcounters;
384 u_repl->num_counters = u_repl->nentries;
Bart De Schuymer62423742002-07-14 19:06:20 +0000385 if (u_repl->filename != NULL) {
386 store_counters_in_file(u_repl->filename, u_repl);
387 return;
388 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000389 optlen = u_repl->nentries * sizeof(struct ebt_counter) +
390 sizeof(struct ebt_replace);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000391 /* Now put the stuff in the kernel's struct ebt_replace */
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000392 repl.counters = sparc_cast u_repl->counters;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000393 repl.num_counters = u_repl->num_counters;
394 memcpy(repl.name, u_repl->name, sizeof(repl.name));
395
Bart De Schuymer6622a012005-01-19 21:09:05 +0000396 if (get_sockfd())
397 return;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000398 if (setsockopt(sockfd, IPPROTO_IP, EBT_SO_SET_COUNTERS, &repl, optlen))
Bart De Schuymer64182a32004-01-21 20:39:54 +0000399 ebt_print_bug("Couldn't update kernel counters");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000400
401 if (exec_style != EXEC_STYLE_DAEMON)
402 return;
403 /* Reset the counterchanges to CNT_NORM */
404 cc = u_repl->counterchanges;
405 for (i = 0; i < u_repl->nentries; i++) {
406 cc->type = CNT_NORM;
407 cc3 = &cc->next;
408 cc = cc->next;
409 }
410 *cc3 = NULL;
411 while (cc) {
412 cc2 = cc->next;
413 free(cc);
414 cc = cc2;
415 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000416}
417
418static int
419ebt_translate_match(struct ebt_entry_match *m, struct ebt_u_match_list ***l)
420{
421 struct ebt_u_match_list *new;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000422 int ret = 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000423
424 new = (struct ebt_u_match_list *)
425 malloc(sizeof(struct ebt_u_match_list));
426 if (!new)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000427 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000428 new->m = (struct ebt_entry_match *)
429 malloc(m->match_size + sizeof(struct ebt_entry_match));
430 if (!new->m)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000431 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000432 memcpy(new->m, m, m->match_size + sizeof(struct ebt_entry_match));
433 new->next = NULL;
434 **l = new;
435 *l = &new->next;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000436 if (ebt_find_match(new->m->u.name) == NULL) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000437 ebt_print_error("Kernel match %s unsupported by userspace tool",
438 new->m->u.name);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000439 ret = -1;
440 }
441 return ret;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000442}
443
444static int
445ebt_translate_watcher(struct ebt_entry_watcher *w,
446 struct ebt_u_watcher_list ***l)
447{
448 struct ebt_u_watcher_list *new;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000449 int ret = 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000450
451 new = (struct ebt_u_watcher_list *)
452 malloc(sizeof(struct ebt_u_watcher_list));
453 if (!new)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000454 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000455 new->w = (struct ebt_entry_watcher *)
456 malloc(w->watcher_size + sizeof(struct ebt_entry_watcher));
457 if (!new->w)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000458 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000459 memcpy(new->w, w, w->watcher_size + sizeof(struct ebt_entry_watcher));
460 new->next = NULL;
461 **l = new;
462 *l = &new->next;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000463 if (ebt_find_watcher(new->w->u.name) == NULL) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000464 ebt_print_error("Kernel watcher %s unsupported by userspace "
465 "tool", new->w->u.name);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000466 ret = -1;
467 }
468 return ret;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000469}
470
471static int
472ebt_translate_entry(struct ebt_entry *e, unsigned int *hook, int *n, int *cnt,
473 int *totalcnt, struct ebt_u_entry ***u_e, struct ebt_u_replace *u_repl,
Bart De Schuymer60332e02002-06-23 08:01:47 +0000474 unsigned int valid_hooks, char *base)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000475{
Bart De Schuymer6622a012005-01-19 21:09:05 +0000476 /* An entry */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000477 if (e->bitmask & EBT_ENTRY_OR_ENTRIES) {
478 struct ebt_u_entry *new;
479 struct ebt_u_match_list **m_l;
480 struct ebt_u_watcher_list **w_l;
481 struct ebt_entry_target *t;
482
483 new = (struct ebt_u_entry *)malloc(sizeof(struct ebt_u_entry));
484 if (!new)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000485 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000486 new->bitmask = e->bitmask;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000487 /*
Bart De Schuymer6622a012005-01-19 21:09:05 +0000488 * Plain userspace code doesn't know about
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000489 * EBT_ENTRY_OR_ENTRIES
490 */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000491 new->bitmask &= ~EBT_ENTRY_OR_ENTRIES;
492 new->invflags = e->invflags;
493 new->ethproto = e->ethproto;
Bart De Schuymere3cceb72002-07-26 12:47:33 +0000494 strcpy(new->in, e->in);
495 strcpy(new->out, e->out);
496 strcpy(new->logical_in, e->logical_in);
497 strcpy(new->logical_out, e->logical_out);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000498 memcpy(new->sourcemac, e->sourcemac, sizeof(new->sourcemac));
499 memcpy(new->sourcemsk, e->sourcemsk, sizeof(new->sourcemsk));
500 memcpy(new->destmac, e->destmac, sizeof(new->destmac));
501 memcpy(new->destmsk, e->destmsk, sizeof(new->destmsk));
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000502 if (*totalcnt >= u_repl->nentries)
503 ebt_print_bug("*totalcnt >= u_repl->nentries");
504 new->cnt = u_repl->counters[*totalcnt];
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000505 new->m_list = NULL;
506 new->w_list = NULL;
507 new->next = NULL;
508 m_l = &new->m_list;
509 EBT_MATCH_ITERATE(e, ebt_translate_match, &m_l);
510 w_l = &new->w_list;
511 EBT_WATCHER_ITERATE(e, ebt_translate_watcher, &w_l);
512
513 t = (struct ebt_entry_target *)(((char *)e) + e->target_offset);
514 new->t = (struct ebt_entry_target *)
515 malloc(t->target_size + sizeof(struct ebt_entry_target));
516 if (!new->t)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000517 ebt_print_memory();
Bart De Schuymer6622a012005-01-19 21:09:05 +0000518 if (ebt_find_target(t->u.name) == NULL) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000519 ebt_print_error("Kernel target %s unsupported by "
520 "userspace tool", t->u.name);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000521 return -1;
522 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000523 memcpy(new->t, t, t->target_size +
524 sizeof(struct ebt_entry_target));
Bart De Schuymer6622a012005-01-19 21:09:05 +0000525 /* Deal with jumps to udc */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000526 if (!strcmp(t->u.name, EBT_STANDARD_TARGET)) {
527 char *tmp = base;
528 int verdict = ((struct ebt_standard_target *)t)->verdict;
529 int i;
530 struct ebt_u_chain_list *cl;
531
532 if (verdict >= 0) {
533 tmp += verdict;
534 cl = u_repl->udc;
535 i = 0;
536 while (cl && cl->kernel_start != tmp) {
537 i++;
538 cl = cl->next;
539 }
540 if (!cl)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000541 ebt_print_bug("Can't find udc for "
Bart De Schuymer64182a32004-01-21 20:39:54 +0000542 "jump");
Bart De Schuymer60332e02002-06-23 08:01:47 +0000543 ((struct ebt_standard_target *)new->t)->verdict = i;
544 }
545 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000546
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000547 /* I love pointers */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000548 **u_e = new;
549 *u_e = &new->next;
550 (*cnt)++;
551 (*totalcnt)++;
552 return 0;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000553 } else { /* A new chain */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000554 int i;
555 struct ebt_entries *entries = (struct ebt_entries *)e;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000556 struct ebt_u_chain_list *cl;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000557
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000558 if (*n != *cnt)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000559 ebt_print_bug("Nr of entries in the chain is wrong");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000560 *n = entries->nentries;
561 *cnt = 0;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000562 for (i = *hook + 1; i < NF_BR_NUMHOOKS; i++)
563 if (valid_hooks & (1 << i))
564 break;
565 *hook = i;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000566 /* Makes use of fact that standard chains come before udc */
567 if (i >= NF_BR_NUMHOOKS) { /* Udc */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000568 i -= NF_BR_NUMHOOKS;
569 cl = u_repl->udc;
570 while (i-- > 0)
571 cl = cl->next;
572 *u_e = &(cl->udc->entries);
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000573 } else
Bart De Schuymer60332e02002-06-23 08:01:47 +0000574 *u_e = &(u_repl->hook_entry[*hook]->entries);
Bart De Schuymer60332e02002-06-23 08:01:47 +0000575 return 0;
576 }
577}
578
Bart De Schuymer6622a012005-01-19 21:09:05 +0000579/* Initialize all chain headers */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000580static int
581ebt_translate_chains(struct ebt_entry *e, unsigned int *hook,
582 struct ebt_u_replace *u_repl, unsigned int valid_hooks)
583{
584 int i;
585 struct ebt_entries *entries = (struct ebt_entries *)e;
586 struct ebt_u_entries *new;
587 struct ebt_u_chain_list **chain_list;
588
589 if (!(e->bitmask & EBT_ENTRY_OR_ENTRIES)) {
590 for (i = *hook + 1; i < NF_BR_NUMHOOKS; i++)
591 if (valid_hooks & (1 << i))
592 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000593 /* Makes use of fact that standard chains come before udc */
594 if (i >= NF_BR_NUMHOOKS) { /* Udc */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000595 chain_list = &u_repl->udc;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000596 /* Add in the back */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000597 while (*chain_list)
598 chain_list = &((*chain_list)->next);
599 *chain_list = (struct ebt_u_chain_list *)
600 malloc(sizeof(struct ebt_u_chain_list));
601 if (!(*chain_list))
Bart De Schuymer64182a32004-01-21 20:39:54 +0000602 ebt_print_memory();
Bart De Schuymer60332e02002-06-23 08:01:47 +0000603 (*chain_list)->next = NULL;
604 (*chain_list)->udc = (struct ebt_u_entries *)
605 malloc(sizeof(struct ebt_u_entries));
606 if (!((*chain_list)->udc))
Bart De Schuymer64182a32004-01-21 20:39:54 +0000607 ebt_print_memory();
Bart De Schuymer60332e02002-06-23 08:01:47 +0000608 new = (*chain_list)->udc;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000609 /* ebt_translate_entry depends on this for knowing
610 * to which chain is being jumped */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000611 (*chain_list)->kernel_start = (char *)e;
612 } else {
613 *hook = i;
614 new = (struct ebt_u_entries *)
615 malloc(sizeof(struct ebt_u_entries));
616 if (!new)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000617 ebt_print_memory();
Bart De Schuymer60332e02002-06-23 08:01:47 +0000618 u_repl->hook_entry[*hook] = new;
619 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000620 new->nentries = entries->nentries;
621 new->policy = entries->policy;
622 new->entries = NULL;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000623 new->counter_offset = entries->counter_offset;
624 strcpy(new->name, entries->name);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000625 }
Bart De Schuymer60332e02002-06-23 08:01:47 +0000626 return 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000627}
628
Bart De Schuymer6622a012005-01-19 21:09:05 +0000629static int retrieve_from_file(char *filename, struct ebt_replace *repl,
Bart De Schuymer62423742002-07-14 19:06:20 +0000630 char command)
631{
632 FILE *file;
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000633 char *hlp = NULL, *entries;
634 struct ebt_counter *counters;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000635 int size, ret = 0;
Bart De Schuymer62423742002-07-14 19:06:20 +0000636
Bart De Schuymer6622a012005-01-19 21:09:05 +0000637 if (!(file = fopen(filename, "r+b"))) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000638 ebt_print_error("Could not open file %s", filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000639 return -1;
640 }
641 /* Make sure table name is right if command isn't -L or --atomic-commit */
Bart De Schuymer62423742002-07-14 19:06:20 +0000642 if (command != 'L' && command != 8) {
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000643 hlp = (char *)malloc(strlen(repl->name) + 1);
Bart De Schuymer62423742002-07-14 19:06:20 +0000644 if (!hlp)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000645 ebt_print_memory();
Bart De Schuymer62423742002-07-14 19:06:20 +0000646 strcpy(hlp, repl->name);
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000647 }
Bart De Schuymer62423742002-07-14 19:06:20 +0000648 if (fread(repl, sizeof(char), sizeof(struct ebt_replace), file)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000649 != sizeof(struct ebt_replace)) {
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 ret = -1;
652 goto close_file;
653 }
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000654 if (command != 'L' && command != 8 && strcmp(hlp, repl->name)) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000655 ebt_print_error("File %s contains wrong table name or is "
656 "corrupt", filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000657 ret = -1;
658 goto close_file;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000659 } else if (!ebt_find_table(repl->name)) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000660 ebt_print_error("File %s contains invalid table name",
661 filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000662 ret = -1;
663 goto close_file;
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000664 }
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000665
Bart De Schuymer62423742002-07-14 19:06:20 +0000666 size = sizeof(struct ebt_replace) +
667 repl->nentries * sizeof(struct ebt_counter) + repl->entries_size;
668 fseek(file, 0, SEEK_END);
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000669 if (size != ftell(file)) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000670 ebt_print_error("File %s has wrong size", filename);
Bart De Schuymer2f7e8d12005-01-19 21:23:02 +0000671 ret = -1;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000672 goto close_file;
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000673 }
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000674 entries = (char *)malloc(repl->entries_size);
675 if (!entries)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000676 ebt_print_memory();
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000677 repl->entries = sparc_cast entries;
Bart De Schuymer62423742002-07-14 19:06:20 +0000678 if (repl->nentries) {
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000679 counters = (struct ebt_counter *)
Bart De Schuymer62423742002-07-14 19:06:20 +0000680 malloc(repl->nentries * sizeof(struct ebt_counter));
Bart De Schuymer8e96bee2003-08-27 16:59:39 +0000681 repl->counters = sparc_cast counters;
Bart De Schuymer62423742002-07-14 19:06:20 +0000682 if (!repl->counters)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000683 ebt_print_memory();
Bart De Schuymer62423742002-07-14 19:06:20 +0000684 } else
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000685 repl->counters = sparc_cast NULL;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000686 /* Copy entries and counters */
Bart De Schuymer62423742002-07-14 19:06:20 +0000687 if (fseek(file, sizeof(struct ebt_replace), SEEK_SET) ||
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000688 fread((char *)repl->entries, sizeof(char), repl->entries_size, file)
Bart De Schuymer62423742002-07-14 19:06:20 +0000689 != repl->entries_size ||
Bart De Schuymer64182a32004-01-21 20:39:54 +0000690 fseek(file, sizeof(struct ebt_replace) + repl->entries_size,
691 SEEK_SET)
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000692 || fread((char *)repl->counters, sizeof(char),
Bart De Schuymer62423742002-07-14 19:06:20 +0000693 repl->nentries * sizeof(struct ebt_counter), file)
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000694 != repl->nentries * sizeof(struct ebt_counter)) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000695 ebt_print_error("File %s is corrupt", filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000696 free(entries);
697 repl->entries = NULL;
698 ret = -1;
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000699 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000700close_file:
Bart De Schuymer62423742002-07-14 19:06:20 +0000701 fclose(file);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000702 free(hlp);
703 return ret;
Bart De Schuymer62423742002-07-14 19:06:20 +0000704}
705
Bart De Schuymer64182a32004-01-21 20:39:54 +0000706static int retrieve_from_kernel(struct ebt_replace *repl, char command,
707 int init)
Bart De Schuymer62423742002-07-14 19:06:20 +0000708{
709 socklen_t optlen;
710 int optname;
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000711 char *entries;
Bart De Schuymer62423742002-07-14 19:06:20 +0000712
713 optlen = sizeof(struct ebt_replace);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000714 if (get_sockfd())
715 return -1;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000716 /* --atomic-init || --init-table */
Bart De Schuymer64182a32004-01-21 20:39:54 +0000717 if (init)
Bart De Schuymer62423742002-07-14 19:06:20 +0000718 optname = EBT_SO_GET_INIT_INFO;
719 else
720 optname = EBT_SO_GET_INFO;
721 if (getsockopt(sockfd, IPPROTO_IP, optname, repl, &optlen))
722 return -1;
723
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000724 if ( !(entries = (char *)malloc(repl->entries_size)) )
Bart De Schuymer64182a32004-01-21 20:39:54 +0000725 ebt_print_memory();
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000726 repl->entries = sparc_cast entries;
Bart De Schuymer62423742002-07-14 19:06:20 +0000727 if (repl->nentries) {
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000728 struct ebt_counter *counters;
729
730 if (!(counters = (struct ebt_counter *)
Bart De Schuymer62423742002-07-14 19:06:20 +0000731 malloc(repl->nentries * sizeof(struct ebt_counter))) )
Bart De Schuymer64182a32004-01-21 20:39:54 +0000732 ebt_print_memory();
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000733 repl->counters = sparc_cast counters;
Bart De Schuymer62423742002-07-14 19:06:20 +0000734 }
735 else
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000736 repl->counters = sparc_cast NULL;
Bart De Schuymer62423742002-07-14 19:06:20 +0000737
Bart De Schuymer6622a012005-01-19 21:09:05 +0000738 /* We want to receive the counters */
Bart De Schuymer62423742002-07-14 19:06:20 +0000739 repl->num_counters = repl->nentries;
740 optlen += repl->entries_size + repl->num_counters *
741 sizeof(struct ebt_counter);
Bart De Schuymer64182a32004-01-21 20:39:54 +0000742 if (init)
Bart De Schuymer62423742002-07-14 19:06:20 +0000743 optname = EBT_SO_GET_INIT_ENTRIES;
744 else
745 optname = EBT_SO_GET_ENTRIES;
746 if (getsockopt(sockfd, IPPROTO_IP, optname, repl, &optlen))
Bart De Schuymer6622a012005-01-19 21:09:05 +0000747 ebt_print_bug("Hmm, what is wrong??? bug#1");
Bart De Schuymer62423742002-07-14 19:06:20 +0000748
749 return 0;
750}
751
Bart De Schuymer64182a32004-01-21 20:39:54 +0000752int ebt_get_table(struct ebt_u_replace *u_repl, int init)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000753{
754 int i, j, k, hook;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000755 struct ebt_replace repl;
756 struct ebt_u_entry **u_e;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000757 struct ebt_cntchanges *new_cc;
758 struct ebt_cntchanges **prev_cc = &(u_repl->counterchanges);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000759
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000760 strcpy(repl.name, u_repl->name);
Bart De Schuymer62a7fdb2002-08-24 21:01:21 +0000761 if (u_repl->filename != NULL) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000762 if (init)
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000763 ebt_print_bug("Getting initial table data from a file is impossible");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000764 if (retrieve_from_file(u_repl->filename, &repl, u_repl->command))
765 return -1;
766 /* -L with a wrong table name should be dealt with silently */
Bart De Schuymer62a7fdb2002-08-24 21:01:21 +0000767 strcpy(u_repl->name, repl.name);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000768 } else if (retrieve_from_kernel(&repl, u_repl->command, init))
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000769 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000770
Bart De Schuymer6622a012005-01-19 21:09:05 +0000771 /* Translate the struct ebt_replace to a struct ebt_u_replace */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000772 u_repl->valid_hooks = repl.valid_hooks;
773 u_repl->nentries = repl.nentries;
774 u_repl->num_counters = repl.num_counters;
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000775 u_repl->counters = repl.counters;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000776 u_repl->udc = NULL;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000777 u_repl->counterchanges = NULL;
778 for (i = 0; i < repl.nentries; i++) {
779 new_cc = (struct ebt_cntchanges *)
780 malloc(sizeof(struct ebt_cntchanges));
781 if (!new_cc)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000782 ebt_print_memory();
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000783 new_cc->type = CNT_NORM;
784 new_cc->next = NULL;
785 *prev_cc = new_cc;
786 prev_cc = &(new_cc->next);
787 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000788 hook = -1;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000789 /* FIXME: Clean up when an error is encountered */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000790 EBT_ENTRY_ITERATE(repl.entries, repl.entries_size, ebt_translate_chains,
791 &hook, u_repl, u_repl->valid_hooks);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000792 i = 0; /* Holds the expected nr. of entries for the chain */
793 j = 0; /* Holds the up to now counted entries for the chain */
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000794 k = 0; /* Holds the total nr. of entries, should equal u_repl->nentries afterwards */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000795 hook = -1;
Bart De Schuymer64182a32004-01-21 20:39:54 +0000796 EBT_ENTRY_ITERATE((char *)repl.entries, repl.entries_size,
797 ebt_translate_entry, &hook, &i, &j, &k, &u_e, u_repl,
798 u_repl->valid_hooks, (char *)repl.entries);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000799 if (k != u_repl->nentries)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000800 ebt_print_bug("Wrong total nentries");
Bart De Schuymer9ce6ee92002-06-14 21:56:35 +0000801 return 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000802}