blob: 6db6858ca8e321a8e65fc02272bd763cd080120b [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 Schuymerab611e22005-02-14 20:20:03 +0000361 next->cnt = *new;
362 next->cnt_surplus.pcnt = next->cnt_surplus.bcnt = 0;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000363 /* We've used an old counter */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000364 old++;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000365 /* We've set a new counter */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000366 new++;
Bart De Schuymerff587202005-02-08 20:02:28 +0000367 next = next->next;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000368 } else if (cc->type == CNT_DEL) {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000369 /* Don't use this old counter */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000370 old++;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000371 } else {
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000372 if (cc->type == CNT_CHANGE) {
373 new->pcnt = old->pcnt;
374 if (cc->change % 3 == 1)
375 new->pcnt = old->pcnt + next->cnt_surplus.pcnt;
376 else if (cc->change % 3 == 2)
377 new->pcnt = old->pcnt - next->cnt_surplus.pcnt;
378 else
379 new->pcnt = next->cnt.pcnt;
380 if (cc->change / 3 == 1)
381 new->bcnt = old->bcnt + next->cnt_surplus.bcnt;
382 else if (cc->change / 3 == 2)
383 new->bcnt = old->bcnt - next->cnt_surplus.bcnt;
384 else
385 new->bcnt = next->cnt.bcnt;
Bart De Schuymerab611e22005-02-14 20:20:03 +0000386 } else
387 *new = next->cnt;
388 next->cnt = *new;
389 next->cnt_surplus.pcnt = next->cnt_surplus.bcnt = 0;
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000390 if (cc->type == CNT_ADD)
391 new++;
392 else {
393 old++;
394 new++;
395 }
Bart De Schuymerff587202005-02-08 20:02:28 +0000396 next = next->next;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000397 }
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000398 cc = cc->next;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000399 }
400
401 free(u_repl->counters);
402 u_repl->counters = newcounters;
403 u_repl->num_counters = u_repl->nentries;
Bart De Schuymer62423742002-07-14 19:06:20 +0000404 if (u_repl->filename != NULL) {
405 store_counters_in_file(u_repl->filename, u_repl);
406 return;
407 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000408 optlen = u_repl->nentries * sizeof(struct ebt_counter) +
409 sizeof(struct ebt_replace);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000410 /* Now put the stuff in the kernel's struct ebt_replace */
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000411 repl.counters = sparc_cast u_repl->counters;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000412 repl.num_counters = u_repl->num_counters;
413 memcpy(repl.name, u_repl->name, sizeof(repl.name));
414
Bart De Schuymer6622a012005-01-19 21:09:05 +0000415 if (get_sockfd())
416 return;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000417 if (setsockopt(sockfd, IPPROTO_IP, EBT_SO_SET_COUNTERS, &repl, optlen))
Bart De Schuymer64182a32004-01-21 20:39:54 +0000418 ebt_print_bug("Couldn't update kernel counters");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000419
420 if (exec_style != EXEC_STYLE_DAEMON)
421 return;
422 /* Reset the counterchanges to CNT_NORM */
423 cc = u_repl->counterchanges;
424 for (i = 0; i < u_repl->nentries; i++) {
425 cc->type = CNT_NORM;
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000426 cc->change = 0;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000427 cc3 = &cc->next;
428 cc = cc->next;
429 }
430 *cc3 = NULL;
431 while (cc) {
432 cc2 = cc->next;
433 free(cc);
434 cc = cc2;
435 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000436}
437
438static int
439ebt_translate_match(struct ebt_entry_match *m, struct ebt_u_match_list ***l)
440{
441 struct ebt_u_match_list *new;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000442 int ret = 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000443
444 new = (struct ebt_u_match_list *)
445 malloc(sizeof(struct ebt_u_match_list));
446 if (!new)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000447 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000448 new->m = (struct ebt_entry_match *)
449 malloc(m->match_size + sizeof(struct ebt_entry_match));
450 if (!new->m)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000451 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000452 memcpy(new->m, m, m->match_size + sizeof(struct ebt_entry_match));
453 new->next = NULL;
454 **l = new;
455 *l = &new->next;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000456 if (ebt_find_match(new->m->u.name) == NULL) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000457 ebt_print_error("Kernel match %s unsupported by userspace tool",
458 new->m->u.name);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000459 ret = -1;
460 }
461 return ret;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000462}
463
464static int
465ebt_translate_watcher(struct ebt_entry_watcher *w,
466 struct ebt_u_watcher_list ***l)
467{
468 struct ebt_u_watcher_list *new;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000469 int ret = 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000470
471 new = (struct ebt_u_watcher_list *)
472 malloc(sizeof(struct ebt_u_watcher_list));
473 if (!new)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000474 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000475 new->w = (struct ebt_entry_watcher *)
476 malloc(w->watcher_size + sizeof(struct ebt_entry_watcher));
477 if (!new->w)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000478 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000479 memcpy(new->w, w, w->watcher_size + sizeof(struct ebt_entry_watcher));
480 new->next = NULL;
481 **l = new;
482 *l = &new->next;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000483 if (ebt_find_watcher(new->w->u.name) == NULL) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000484 ebt_print_error("Kernel watcher %s unsupported by userspace "
485 "tool", new->w->u.name);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000486 ret = -1;
487 }
488 return ret;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000489}
490
491static int
492ebt_translate_entry(struct ebt_entry *e, unsigned int *hook, int *n, int *cnt,
493 int *totalcnt, struct ebt_u_entry ***u_e, struct ebt_u_replace *u_repl,
Bart De Schuymer60332e02002-06-23 08:01:47 +0000494 unsigned int valid_hooks, char *base)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000495{
Bart De Schuymer6622a012005-01-19 21:09:05 +0000496 /* An entry */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000497 if (e->bitmask & EBT_ENTRY_OR_ENTRIES) {
498 struct ebt_u_entry *new;
499 struct ebt_u_match_list **m_l;
500 struct ebt_u_watcher_list **w_l;
501 struct ebt_entry_target *t;
502
503 new = (struct ebt_u_entry *)malloc(sizeof(struct ebt_u_entry));
504 if (!new)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000505 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000506 new->bitmask = e->bitmask;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000507 /*
Bart De Schuymer6622a012005-01-19 21:09:05 +0000508 * Plain userspace code doesn't know about
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000509 * EBT_ENTRY_OR_ENTRIES
510 */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000511 new->bitmask &= ~EBT_ENTRY_OR_ENTRIES;
512 new->invflags = e->invflags;
513 new->ethproto = e->ethproto;
Bart De Schuymere3cceb72002-07-26 12:47:33 +0000514 strcpy(new->in, e->in);
515 strcpy(new->out, e->out);
516 strcpy(new->logical_in, e->logical_in);
517 strcpy(new->logical_out, e->logical_out);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000518 memcpy(new->sourcemac, e->sourcemac, sizeof(new->sourcemac));
519 memcpy(new->sourcemsk, e->sourcemsk, sizeof(new->sourcemsk));
520 memcpy(new->destmac, e->destmac, sizeof(new->destmac));
521 memcpy(new->destmsk, e->destmsk, sizeof(new->destmsk));
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000522 if (*totalcnt >= u_repl->nentries)
523 ebt_print_bug("*totalcnt >= u_repl->nentries");
524 new->cnt = u_repl->counters[*totalcnt];
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000525 new->cnt_surplus.pcnt = new->cnt_surplus.bcnt = 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000526 new->m_list = NULL;
527 new->w_list = NULL;
528 new->next = NULL;
529 m_l = &new->m_list;
530 EBT_MATCH_ITERATE(e, ebt_translate_match, &m_l);
531 w_l = &new->w_list;
532 EBT_WATCHER_ITERATE(e, ebt_translate_watcher, &w_l);
533
534 t = (struct ebt_entry_target *)(((char *)e) + e->target_offset);
535 new->t = (struct ebt_entry_target *)
536 malloc(t->target_size + sizeof(struct ebt_entry_target));
537 if (!new->t)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000538 ebt_print_memory();
Bart De Schuymer6622a012005-01-19 21:09:05 +0000539 if (ebt_find_target(t->u.name) == NULL) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000540 ebt_print_error("Kernel target %s unsupported by "
541 "userspace tool", t->u.name);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000542 return -1;
543 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000544 memcpy(new->t, t, t->target_size +
545 sizeof(struct ebt_entry_target));
Bart De Schuymer6622a012005-01-19 21:09:05 +0000546 /* Deal with jumps to udc */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000547 if (!strcmp(t->u.name, EBT_STANDARD_TARGET)) {
548 char *tmp = base;
549 int verdict = ((struct ebt_standard_target *)t)->verdict;
550 int i;
551 struct ebt_u_chain_list *cl;
552
553 if (verdict >= 0) {
554 tmp += verdict;
555 cl = u_repl->udc;
556 i = 0;
557 while (cl && cl->kernel_start != tmp) {
558 i++;
559 cl = cl->next;
560 }
561 if (!cl)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000562 ebt_print_bug("Can't find udc for "
Bart De Schuymer64182a32004-01-21 20:39:54 +0000563 "jump");
Bart De Schuymer60332e02002-06-23 08:01:47 +0000564 ((struct ebt_standard_target *)new->t)->verdict = i;
565 }
566 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000567
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000568 /* I love pointers */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000569 **u_e = new;
570 *u_e = &new->next;
571 (*cnt)++;
572 (*totalcnt)++;
573 return 0;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000574 } else { /* A new chain */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000575 int i;
576 struct ebt_entries *entries = (struct ebt_entries *)e;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000577 struct ebt_u_chain_list *cl;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000578
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000579 if (*n != *cnt)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000580 ebt_print_bug("Nr of entries in the chain is wrong");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000581 *n = entries->nentries;
582 *cnt = 0;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000583 for (i = *hook + 1; i < NF_BR_NUMHOOKS; i++)
584 if (valid_hooks & (1 << i))
585 break;
586 *hook = i;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000587 /* Makes use of fact that standard chains come before udc */
588 if (i >= NF_BR_NUMHOOKS) { /* Udc */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000589 i -= NF_BR_NUMHOOKS;
590 cl = u_repl->udc;
591 while (i-- > 0)
592 cl = cl->next;
593 *u_e = &(cl->udc->entries);
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000594 } else
Bart De Schuymer60332e02002-06-23 08:01:47 +0000595 *u_e = &(u_repl->hook_entry[*hook]->entries);
Bart De Schuymer60332e02002-06-23 08:01:47 +0000596 return 0;
597 }
598}
599
Bart De Schuymer6622a012005-01-19 21:09:05 +0000600/* Initialize all chain headers */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000601static int
602ebt_translate_chains(struct ebt_entry *e, unsigned int *hook,
603 struct ebt_u_replace *u_repl, unsigned int valid_hooks)
604{
605 int i;
606 struct ebt_entries *entries = (struct ebt_entries *)e;
607 struct ebt_u_entries *new;
608 struct ebt_u_chain_list **chain_list;
609
610 if (!(e->bitmask & EBT_ENTRY_OR_ENTRIES)) {
611 for (i = *hook + 1; i < NF_BR_NUMHOOKS; i++)
612 if (valid_hooks & (1 << i))
613 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000614 /* Makes use of fact that standard chains come before udc */
615 if (i >= NF_BR_NUMHOOKS) { /* Udc */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000616 chain_list = &u_repl->udc;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000617 /* Add in the back */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000618 while (*chain_list)
619 chain_list = &((*chain_list)->next);
620 *chain_list = (struct ebt_u_chain_list *)
621 malloc(sizeof(struct ebt_u_chain_list));
622 if (!(*chain_list))
Bart De Schuymer64182a32004-01-21 20:39:54 +0000623 ebt_print_memory();
Bart De Schuymer60332e02002-06-23 08:01:47 +0000624 (*chain_list)->next = NULL;
625 (*chain_list)->udc = (struct ebt_u_entries *)
626 malloc(sizeof(struct ebt_u_entries));
627 if (!((*chain_list)->udc))
Bart De Schuymer64182a32004-01-21 20:39:54 +0000628 ebt_print_memory();
Bart De Schuymer60332e02002-06-23 08:01:47 +0000629 new = (*chain_list)->udc;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000630 /* ebt_translate_entry depends on this for knowing
631 * to which chain is being jumped */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000632 (*chain_list)->kernel_start = (char *)e;
633 } else {
634 *hook = i;
635 new = (struct ebt_u_entries *)
636 malloc(sizeof(struct ebt_u_entries));
637 if (!new)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000638 ebt_print_memory();
Bart De Schuymer60332e02002-06-23 08:01:47 +0000639 u_repl->hook_entry[*hook] = new;
640 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000641 new->nentries = entries->nentries;
642 new->policy = entries->policy;
643 new->entries = NULL;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000644 new->counter_offset = entries->counter_offset;
645 strcpy(new->name, entries->name);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000646 }
Bart De Schuymer60332e02002-06-23 08:01:47 +0000647 return 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000648}
649
Bart De Schuymer6622a012005-01-19 21:09:05 +0000650static int retrieve_from_file(char *filename, struct ebt_replace *repl,
Bart De Schuymer62423742002-07-14 19:06:20 +0000651 char command)
652{
653 FILE *file;
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000654 char *hlp = NULL, *entries;
655 struct ebt_counter *counters;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000656 int size, ret = 0;
Bart De Schuymer62423742002-07-14 19:06:20 +0000657
Bart De Schuymer6622a012005-01-19 21:09:05 +0000658 if (!(file = fopen(filename, "r+b"))) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000659 ebt_print_error("Could not open file %s", filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000660 return -1;
661 }
662 /* Make sure table name is right if command isn't -L or --atomic-commit */
Bart De Schuymer62423742002-07-14 19:06:20 +0000663 if (command != 'L' && command != 8) {
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000664 hlp = (char *)malloc(strlen(repl->name) + 1);
Bart De Schuymer62423742002-07-14 19:06:20 +0000665 if (!hlp)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000666 ebt_print_memory();
Bart De Schuymer62423742002-07-14 19:06:20 +0000667 strcpy(hlp, repl->name);
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000668 }
Bart De Schuymer62423742002-07-14 19:06:20 +0000669 if (fread(repl, sizeof(char), sizeof(struct ebt_replace), file)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000670 != sizeof(struct ebt_replace)) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000671 ebt_print_error("File %s is corrupt", filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000672 ret = -1;
673 goto close_file;
674 }
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000675 if (command != 'L' && command != 8 && strcmp(hlp, repl->name)) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000676 ebt_print_error("File %s contains wrong table name or is "
677 "corrupt", filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000678 ret = -1;
679 goto close_file;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000680 } else if (!ebt_find_table(repl->name)) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000681 ebt_print_error("File %s contains invalid table name",
682 filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000683 ret = -1;
684 goto close_file;
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000685 }
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000686
Bart De Schuymer62423742002-07-14 19:06:20 +0000687 size = sizeof(struct ebt_replace) +
688 repl->nentries * sizeof(struct ebt_counter) + repl->entries_size;
689 fseek(file, 0, SEEK_END);
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000690 if (size != ftell(file)) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000691 ebt_print_error("File %s has wrong size", filename);
Bart De Schuymer2f7e8d12005-01-19 21:23:02 +0000692 ret = -1;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000693 goto close_file;
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000694 }
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000695 entries = (char *)malloc(repl->entries_size);
696 if (!entries)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000697 ebt_print_memory();
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000698 repl->entries = sparc_cast entries;
Bart De Schuymer62423742002-07-14 19:06:20 +0000699 if (repl->nentries) {
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000700 counters = (struct ebt_counter *)
Bart De Schuymer62423742002-07-14 19:06:20 +0000701 malloc(repl->nentries * sizeof(struct ebt_counter));
Bart De Schuymer8e96bee2003-08-27 16:59:39 +0000702 repl->counters = sparc_cast counters;
Bart De Schuymer62423742002-07-14 19:06:20 +0000703 if (!repl->counters)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000704 ebt_print_memory();
Bart De Schuymer62423742002-07-14 19:06:20 +0000705 } else
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000706 repl->counters = sparc_cast NULL;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000707 /* Copy entries and counters */
Bart De Schuymer62423742002-07-14 19:06:20 +0000708 if (fseek(file, sizeof(struct ebt_replace), SEEK_SET) ||
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000709 fread((char *)repl->entries, sizeof(char), repl->entries_size, file)
Bart De Schuymer62423742002-07-14 19:06:20 +0000710 != repl->entries_size ||
Bart De Schuymer64182a32004-01-21 20:39:54 +0000711 fseek(file, sizeof(struct ebt_replace) + repl->entries_size,
712 SEEK_SET)
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000713 || fread((char *)repl->counters, sizeof(char),
Bart De Schuymer62423742002-07-14 19:06:20 +0000714 repl->nentries * sizeof(struct ebt_counter), file)
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000715 != repl->nentries * sizeof(struct ebt_counter)) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000716 ebt_print_error("File %s is corrupt", filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000717 free(entries);
718 repl->entries = NULL;
719 ret = -1;
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000720 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000721close_file:
Bart De Schuymer62423742002-07-14 19:06:20 +0000722 fclose(file);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000723 free(hlp);
724 return ret;
Bart De Schuymer62423742002-07-14 19:06:20 +0000725}
726
Bart De Schuymer64182a32004-01-21 20:39:54 +0000727static int retrieve_from_kernel(struct ebt_replace *repl, char command,
728 int init)
Bart De Schuymer62423742002-07-14 19:06:20 +0000729{
730 socklen_t optlen;
731 int optname;
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000732 char *entries;
Bart De Schuymer62423742002-07-14 19:06:20 +0000733
734 optlen = sizeof(struct ebt_replace);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000735 if (get_sockfd())
736 return -1;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000737 /* --atomic-init || --init-table */
Bart De Schuymer64182a32004-01-21 20:39:54 +0000738 if (init)
Bart De Schuymer62423742002-07-14 19:06:20 +0000739 optname = EBT_SO_GET_INIT_INFO;
740 else
741 optname = EBT_SO_GET_INFO;
742 if (getsockopt(sockfd, IPPROTO_IP, optname, repl, &optlen))
743 return -1;
744
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000745 if ( !(entries = (char *)malloc(repl->entries_size)) )
Bart De Schuymer64182a32004-01-21 20:39:54 +0000746 ebt_print_memory();
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000747 repl->entries = sparc_cast entries;
Bart De Schuymer62423742002-07-14 19:06:20 +0000748 if (repl->nentries) {
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000749 struct ebt_counter *counters;
750
751 if (!(counters = (struct ebt_counter *)
Bart De Schuymer62423742002-07-14 19:06:20 +0000752 malloc(repl->nentries * sizeof(struct ebt_counter))) )
Bart De Schuymer64182a32004-01-21 20:39:54 +0000753 ebt_print_memory();
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000754 repl->counters = sparc_cast counters;
Bart De Schuymer62423742002-07-14 19:06:20 +0000755 }
756 else
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000757 repl->counters = sparc_cast NULL;
Bart De Schuymer62423742002-07-14 19:06:20 +0000758
Bart De Schuymer6622a012005-01-19 21:09:05 +0000759 /* We want to receive the counters */
Bart De Schuymer62423742002-07-14 19:06:20 +0000760 repl->num_counters = repl->nentries;
761 optlen += repl->entries_size + repl->num_counters *
762 sizeof(struct ebt_counter);
Bart De Schuymer64182a32004-01-21 20:39:54 +0000763 if (init)
Bart De Schuymer62423742002-07-14 19:06:20 +0000764 optname = EBT_SO_GET_INIT_ENTRIES;
765 else
766 optname = EBT_SO_GET_ENTRIES;
767 if (getsockopt(sockfd, IPPROTO_IP, optname, repl, &optlen))
Bart De Schuymer6622a012005-01-19 21:09:05 +0000768 ebt_print_bug("Hmm, what is wrong??? bug#1");
Bart De Schuymer62423742002-07-14 19:06:20 +0000769
770 return 0;
771}
772
Bart De Schuymer64182a32004-01-21 20:39:54 +0000773int ebt_get_table(struct ebt_u_replace *u_repl, int init)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000774{
775 int i, j, k, hook;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000776 struct ebt_replace repl;
777 struct ebt_u_entry **u_e;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000778 struct ebt_cntchanges *new_cc;
779 struct ebt_cntchanges **prev_cc = &(u_repl->counterchanges);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000780
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000781 strcpy(repl.name, u_repl->name);
Bart De Schuymer62a7fdb2002-08-24 21:01:21 +0000782 if (u_repl->filename != NULL) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000783 if (init)
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000784 ebt_print_bug("Getting initial table data from a file is impossible");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000785 if (retrieve_from_file(u_repl->filename, &repl, u_repl->command))
786 return -1;
787 /* -L with a wrong table name should be dealt with silently */
Bart De Schuymer62a7fdb2002-08-24 21:01:21 +0000788 strcpy(u_repl->name, repl.name);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000789 } else if (retrieve_from_kernel(&repl, u_repl->command, init))
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000790 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000791
Bart De Schuymer6622a012005-01-19 21:09:05 +0000792 /* Translate the struct ebt_replace to a struct ebt_u_replace */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000793 u_repl->valid_hooks = repl.valid_hooks;
794 u_repl->nentries = repl.nentries;
795 u_repl->num_counters = repl.num_counters;
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000796 u_repl->counters = repl.counters;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000797 u_repl->udc = NULL;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000798 u_repl->counterchanges = NULL;
799 for (i = 0; i < repl.nentries; i++) {
800 new_cc = (struct ebt_cntchanges *)
801 malloc(sizeof(struct ebt_cntchanges));
802 if (!new_cc)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000803 ebt_print_memory();
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000804 new_cc->type = CNT_NORM;
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000805 new_cc->change = 0;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000806 new_cc->next = NULL;
807 *prev_cc = new_cc;
808 prev_cc = &(new_cc->next);
809 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000810 hook = -1;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000811 /* FIXME: Clean up when an error is encountered */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000812 EBT_ENTRY_ITERATE(repl.entries, repl.entries_size, ebt_translate_chains,
813 &hook, u_repl, u_repl->valid_hooks);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000814 i = 0; /* Holds the expected nr. of entries for the chain */
815 j = 0; /* Holds the up to now counted entries for the chain */
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000816 k = 0; /* Holds the total nr. of entries, should equal u_repl->nentries afterwards */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000817 hook = -1;
Bart De Schuymer64182a32004-01-21 20:39:54 +0000818 EBT_ENTRY_ITERATE((char *)repl.entries, repl.entries_size,
819 ebt_translate_entry, &hook, &i, &j, &k, &u_e, u_repl,
820 u_repl->valid_hooks, (char *)repl.entries);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000821 if (k != u_repl->nentries)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000822 ebt_print_bug("Wrong total nentries");
Bart De Schuymer9ce6ee92002-06-14 21:56:35 +0000823 return 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000824}