blob: dcfd8dc84ac771dbcf4b966867c855741e15655d [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.
Bart De Schuymer6bdb9f42005-11-09 22:39:03 +000012 * The other code works with linked lists. So, the translation is done here.
Bart De Schuymer9895a8e2003-01-11 10:14:24 +000013 */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000014
15#include <getopt.h>
16#include <string.h>
17#include <errno.h>
18#include <stdio.h>
19#include <stdlib.h>
Bart De Schuymer6bdb9f42005-11-09 22:39:03 +000020#include <fcntl.h>
21#include <unistd.h>
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000022#include <sys/socket.h>
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000023#include "include/ebtables_u.h"
24
25extern char* hooknames[NF_BR_NUMHOOKS];
26
Bart De Schuymerf81c2702003-07-23 21:07:04 +000027#ifdef KERNEL_64_USERSPACE_32
28#define sparc_cast (uint64_t)
29#else
30#define sparc_cast
31#endif
32
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000033int sockfd = -1;
34
Bart De Schuymer6622a012005-01-19 21:09:05 +000035static int get_sockfd()
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000036{
Bart De Schuymer6622a012005-01-19 21:09:05 +000037 int ret = 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000038 if (sockfd == -1) {
39 sockfd = socket(AF_INET, SOCK_RAW, PF_INET);
Bart De Schuymer6622a012005-01-19 21:09:05 +000040 if (sockfd < 0) {
Bart De Schuymer64182a32004-01-21 20:39:54 +000041 ebt_print_error("Problem getting a socket, "
42 "you probably don't have the right "
43 "permissions");
Bart De Schuymer6622a012005-01-19 21:09:05 +000044 ret = -1;
45 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000046 }
Bart De Schuymer6622a012005-01-19 21:09:05 +000047 return ret;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000048}
49
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +000050static struct ebt_replace *translate_user2kernel(struct ebt_u_replace *u_repl)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000051{
52 struct ebt_replace *new;
53 struct ebt_u_entry *e;
54 struct ebt_u_match_list *m_l;
55 struct ebt_u_watcher_list *w_l;
Bart De Schuymer60332e02002-06-23 08:01:47 +000056 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 Schuymer9bfcfd82005-08-27 16:52:19 +000069 chain_offsets = (unsigned int *)malloc(u_repl->num_chains * sizeof(unsigned int));
Bart De Schuymer6622a012005-01-19 21:09:05 +000070 /* Determine size */
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +000071 for (i = 0; i < u_repl->num_chains; i++) {
72 if (!(entries = u_repl->chains[i]))
73 continue;
Bart De Schuymer60332e02002-06-23 08:01:47 +000074 chain_offsets[i] = entries_size;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000075 entries_size += sizeof(struct ebt_entries);
76 j = 0;
Bart De Schuymere94eaf72005-08-28 16:06:22 +000077 e = entries->entries->next;
78 while (e != entries->entries) {
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000079 j++;
80 entries_size += sizeof(struct ebt_entry);
81 m_l = e->m_list;
82 while (m_l) {
83 entries_size += m_l->m->match_size +
84 sizeof(struct ebt_entry_match);
85 m_l = m_l->next;
86 }
87 w_l = e->w_list;
88 while (w_l) {
89 entries_size += w_l->w->watcher_size +
90 sizeof(struct ebt_entry_watcher);
91 w_l = w_l->next;
92 }
93 entries_size += e->t->target_size +
94 sizeof(struct ebt_entry_target);
95 e = e->next;
96 }
Bart De Schuymer6622a012005-01-19 21:09:05 +000097 /* A little sanity check */
Bart De Schuymer60332e02002-06-23 08:01:47 +000098 if (j != entries->nentries)
Bart De Schuymer64182a32004-01-21 20:39:54 +000099 ebt_print_bug("Wrong nentries: %d != %d, hook = %s", j,
Bart De Schuymer60332e02002-06-23 08:01:47 +0000100 entries->nentries, entries->name);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000101 }
102
103 new->entries_size = entries_size;
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000104 p = (char *)malloc(entries_size);
105 if (!p)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000106 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000107
Bart De Schuymer6622a012005-01-19 21:09:05 +0000108 /* Put everything in one block */
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000109 new->entries = sparc_cast p;
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000110 for (i = 0; i < u_repl->num_chains; i++) {
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000111 struct ebt_entries *hlp;
112
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000113 hlp = (struct ebt_entries *)p;
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000114 if (!(entries = u_repl->chains[i]))
115 continue;
116 if (i < NF_BR_NUMHOOKS)
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000117 new->hook_entry[i] = sparc_cast hlp;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000118 hlp->nentries = entries->nentries;
119 hlp->policy = entries->policy;
120 strcpy(hlp->name, entries->name);
121 hlp->counter_offset = entries->counter_offset;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000122 hlp->distinguisher = 0; /* Make the kernel see the light */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000123 p += sizeof(struct ebt_entries);
Bart De Schuymere94eaf72005-08-28 16:06:22 +0000124 e = entries->entries->next;
125 while (e != entries->entries) {
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000126 struct ebt_entry *tmp = (struct ebt_entry *)p;
127
128 tmp->bitmask = e->bitmask | EBT_ENTRY_OR_ENTRIES;
129 tmp->invflags = e->invflags;
130 tmp->ethproto = e->ethproto;
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000131 strcpy(tmp->in, e->in);
132 strcpy(tmp->out, e->out);
133 strcpy(tmp->logical_in, e->logical_in);
134 strcpy(tmp->logical_out, e->logical_out);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000135 memcpy(tmp->sourcemac, e->sourcemac,
136 sizeof(tmp->sourcemac));
137 memcpy(tmp->sourcemsk, e->sourcemsk,
138 sizeof(tmp->sourcemsk));
139 memcpy(tmp->destmac, e->destmac, sizeof(tmp->destmac));
140 memcpy(tmp->destmsk, e->destmsk, sizeof(tmp->destmsk));
141
142 base = p;
143 p += sizeof(struct ebt_entry);
144 m_l = e->m_list;
145 while (m_l) {
146 memcpy(p, m_l->m, m_l->m->match_size +
147 sizeof(struct ebt_entry_match));
148 p += m_l->m->match_size +
149 sizeof(struct ebt_entry_match);
150 m_l = m_l->next;
151 }
152 tmp->watchers_offset = p - base;
153 w_l = e->w_list;
154 while (w_l) {
155 memcpy(p, w_l->w, w_l->w->watcher_size +
156 sizeof(struct ebt_entry_watcher));
157 p += w_l->w->watcher_size +
158 sizeof(struct ebt_entry_watcher);
159 w_l = w_l->next;
160 }
161 tmp->target_offset = p - base;
162 memcpy(p, e->t, e->t->target_size +
163 sizeof(struct ebt_entry_target));
Bart De Schuymer60332e02002-06-23 08:01:47 +0000164 if (!strcmp(e->t->u.name, EBT_STANDARD_TARGET)) {
165 struct ebt_standard_target *st =
166 (struct ebt_standard_target *)p;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000167 /* Translate the jump to a udc */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000168 if (st->verdict >= 0)
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000169 st->verdict = chain_offsets
170 [st->verdict + NF_BR_NUMHOOKS];
Bart De Schuymer60332e02002-06-23 08:01:47 +0000171 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000172 p += e->t->target_size +
173 sizeof(struct ebt_entry_target);
174 tmp->next_offset = p - base;
175 e = e->next;
176 }
177 }
178
Bart De Schuymer6622a012005-01-19 21:09:05 +0000179 /* Sanity check */
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000180 if (p - (char *)new->entries != new->entries_size)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000181 ebt_print_bug("Entries_size bug");
Bart De Schuymer60332e02002-06-23 08:01:47 +0000182 free(chain_offsets);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000183 return new;
184}
185
Bart De Schuymer62423742002-07-14 19:06:20 +0000186static void store_table_in_file(char *filename, struct ebt_replace *repl)
187{
Bart De Schuymer6bdb9f42005-11-09 22:39:03 +0000188 char *data;
Bart De Schuymer62423742002-07-14 19:06:20 +0000189 int size;
Bart De Schuymer6bdb9f42005-11-09 22:39:03 +0000190 int fd;
Bart De Schuymer62423742002-07-14 19:06:20 +0000191
Bart De Schuymer6622a012005-01-19 21:09:05 +0000192 /* Start from an empty file with right priviliges */
Bart De Schuymer6bdb9f42005-11-09 22:39:03 +0000193 if (!(fd = creat(filename, 0600))) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000194 ebt_print_error("Couldn't create file %s", filename);
Bart De Schuymer6bdb9f42005-11-09 22:39:03 +0000195 return;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000196 }
Bart De Schuymer62423742002-07-14 19:06:20 +0000197
198 size = sizeof(struct ebt_replace) + repl->entries_size +
199 repl->nentries * sizeof(struct ebt_counter);
200 data = (char *)malloc(size);
201 if (!data)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000202 ebt_print_memory();
Bart De Schuymer62423742002-07-14 19:06:20 +0000203 memcpy(data, repl, sizeof(struct ebt_replace));
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000204 memcpy(data + sizeof(struct ebt_replace), (char *)repl->entries,
Bart De Schuymer62423742002-07-14 19:06:20 +0000205 repl->entries_size);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000206 /* Initialize counters to zero, deliver_counters() can update them */
Bart De Schuymer62423742002-07-14 19:06:20 +0000207 memset(data + sizeof(struct ebt_replace) + repl->entries_size,
208 0, repl->nentries * sizeof(struct ebt_counter));
Bart De Schuymer6bdb9f42005-11-09 22:39:03 +0000209 if (write(fd, data, size) != size)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000210 ebt_print_error("Couldn't write everything to file %s",
211 filename);
Bart De Schuymer6bdb9f42005-11-09 22:39:03 +0000212 close(fd);
Bart De Schuymer62423742002-07-14 19:06:20 +0000213 free(data);
214}
215
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000216void ebt_deliver_table(struct ebt_u_replace *u_repl)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000217{
218 socklen_t optlen;
219 struct ebt_replace *repl;
220
Bart De Schuymer6622a012005-01-19 21:09:05 +0000221 /* Translate the struct ebt_u_replace to a struct ebt_replace */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000222 repl = translate_user2kernel(u_repl);
Bart De Schuymer62423742002-07-14 19:06:20 +0000223 if (u_repl->filename != NULL) {
224 store_table_in_file(u_repl->filename, repl);
Bart De Schuymer92579fe2005-09-01 20:35:43 +0000225 goto free_repl;
Bart De Schuymer62423742002-07-14 19:06:20 +0000226 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000227 /* Give the data to the kernel */
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000228 optlen = sizeof(struct ebt_replace) + repl->entries_size;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000229 if (get_sockfd())
Bart De Schuymer92579fe2005-09-01 20:35:43 +0000230 goto free_repl;
Bart De Schuymer0cb01792003-05-04 16:52:04 +0000231 if (!setsockopt(sockfd, IPPROTO_IP, EBT_SO_SET_ENTRIES, repl, optlen))
Bart De Schuymer92579fe2005-09-01 20:35:43 +0000232 goto free_repl;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000233 if (u_repl->command == 8) { /* The ebtables module may not
234 * yet be loaded with --atomic-commit */
Bart De Schuymer0cb01792003-05-04 16:52:04 +0000235 ebtables_insmod("ebtables");
236 if (!setsockopt(sockfd, IPPROTO_IP, EBT_SO_SET_ENTRIES,
237 repl, optlen))
Bart De Schuymer92579fe2005-09-01 20:35:43 +0000238 goto free_repl;
Bart De Schuymer0cb01792003-05-04 16:52:04 +0000239 }
240
Bart De Schuymer64182a32004-01-21 20:39:54 +0000241 ebt_print_error("The kernel doesn't support a certain ebtables"
Bart De Schuymer0cb01792003-05-04 16:52:04 +0000242 " extension, consider recompiling your kernel or insmod"
243 " the extension");
Bart De Schuymer92579fe2005-09-01 20:35:43 +0000244free_repl:
245 if (repl) {
246 free(repl->entries);
247 free(repl);
248 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000249}
250
Bart De Schuymer6622a012005-01-19 21:09:05 +0000251static int store_counters_in_file(char *filename, struct ebt_u_replace *repl)
Bart De Schuymer62423742002-07-14 19:06:20 +0000252{
Bart De Schuymer6622a012005-01-19 21:09:05 +0000253 int size = repl->nentries * sizeof(struct ebt_counter), ret = 0;
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000254 unsigned int entries_size;
Bart De Schuymer62423742002-07-14 19:06:20 +0000255 struct ebt_replace hlp;
256 FILE *file;
257
Bart De Schuymer6622a012005-01-19 21:09:05 +0000258 if (!(file = fopen(filename, "r+b"))) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000259 ebt_print_error("Could not open file %s", filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000260 return -1;
261 }
262 /* Find out entries_size and then set the file pointer to the
263 * counters */
Bart De Schuymer62423742002-07-14 19:06:20 +0000264 if (fseek(file, (char *)(&hlp.entries_size) - (char *)(&hlp), SEEK_SET)
265 || fread(&entries_size, sizeof(char), sizeof(unsigned int), file) !=
266 sizeof(unsigned int) ||
267 fseek(file, entries_size + sizeof(struct ebt_replace), SEEK_SET)) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000268 ebt_print_error("File %s is corrupt", filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000269 ret = -1;
270 goto close_file;
Bart De Schuymer62423742002-07-14 19:06:20 +0000271 }
272 if (fwrite(repl->counters, sizeof(char), size, file) != size) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000273 ebt_print_error("Could not write everything to file %s",
274 filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000275 ret = -1;
Bart De Schuymer62423742002-07-14 19:06:20 +0000276 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000277close_file:
Bart De Schuymer62423742002-07-14 19:06:20 +0000278 fclose(file);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000279 return 0;
Bart De Schuymer62423742002-07-14 19:06:20 +0000280}
281
Bart De Schuymer6622a012005-01-19 21:09:05 +0000282/* Gets executed after ebt_deliver_table. Delivers the counters to the kernel
283 * and resets the counterchanges to CNT_NORM */
Bart De Schuymer83a1b0f2005-09-28 19:36:17 +0000284void ebt_deliver_counters(struct ebt_u_replace *u_repl)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000285{
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000286 struct ebt_counter *old, *new, *newcounters;
287 socklen_t optlen;
288 struct ebt_replace repl;
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000289 struct ebt_cntchanges *cc = u_repl->cc->next, *cc2;
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000290 struct ebt_u_entries *entries;
291 struct ebt_u_entry *next = NULL;
292 int i, chainnr = 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000293
294 if (u_repl->nentries == 0)
295 return;
296
297 newcounters = (struct ebt_counter *)
298 malloc(u_repl->nentries * sizeof(struct ebt_counter));
299 if (!newcounters)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000300 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000301 memset(newcounters, 0, u_repl->nentries * sizeof(struct ebt_counter));
302 old = u_repl->counters;
303 new = newcounters;
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000304 while (cc != u_repl->cc) {
Bart De Schuymere94eaf72005-08-28 16:06:22 +0000305 if (!next || next == entries->entries) {
306 while (chainnr < u_repl->num_chains && (!(entries = u_repl->chains[chainnr++]) ||
307 (next = entries->entries->next) == entries->entries));
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000308 if (chainnr == u_repl->num_chains)
309 break;
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000310 }
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000311 if (cc->type == CNT_NORM) {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000312 /* 'Normal' rule, meaning we didn't do anything to it
313 * So, we just copy */
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000314 *new = *old;
Bart De Schuymerab611e22005-02-14 20:20:03 +0000315 next->cnt = *new;
316 next->cnt_surplus.pcnt = next->cnt_surplus.bcnt = 0;
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000317 old++; /* We've used an old counter */
318 new++; /* We've set a new counter */
Bart De Schuymerff587202005-02-08 20:02:28 +0000319 next = next->next;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000320 } else if (cc->type == CNT_DEL) {
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000321 old++; /* Don't use this old counter */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000322 } else {
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000323 if (cc->type == CNT_CHANGE) {
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000324 if (cc->change % 3 == 1)
325 new->pcnt = old->pcnt + next->cnt_surplus.pcnt;
326 else if (cc->change % 3 == 2)
327 new->pcnt = old->pcnt - next->cnt_surplus.pcnt;
328 else
329 new->pcnt = next->cnt.pcnt;
330 if (cc->change / 3 == 1)
331 new->bcnt = old->bcnt + next->cnt_surplus.bcnt;
332 else if (cc->change / 3 == 2)
333 new->bcnt = old->bcnt - next->cnt_surplus.bcnt;
334 else
335 new->bcnt = next->cnt.bcnt;
Bart De Schuymerab611e22005-02-14 20:20:03 +0000336 } else
337 *new = next->cnt;
338 next->cnt = *new;
339 next->cnt_surplus.pcnt = next->cnt_surplus.bcnt = 0;
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000340 if (cc->type == CNT_ADD)
341 new++;
342 else {
343 old++;
344 new++;
345 }
Bart De Schuymerff587202005-02-08 20:02:28 +0000346 next = next->next;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000347 }
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000348 cc = cc->next;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000349 }
350
351 free(u_repl->counters);
352 u_repl->counters = newcounters;
353 u_repl->num_counters = u_repl->nentries;
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000354 /* Reset the counterchanges to CNT_NORM and delete the unused cc */
355 i = 0;
356 cc = u_repl->cc->next;
357 while (cc != u_repl->cc) {
358 if (cc->type == CNT_DEL) {
359 cc->prev->next = cc->next;
360 cc->next->prev = cc->prev;
361 cc2 = cc->next;
362 free(cc);
363 cc = cc2;
364 } else {
365 cc->type = CNT_NORM;
366 cc->change = 0;
367 i++;
368 cc = cc->next;
369 }
370 }
371 if (i != u_repl->nentries)
372 ebt_print_bug("i != u_repl->nentries");
Bart De Schuymer62423742002-07-14 19:06:20 +0000373 if (u_repl->filename != NULL) {
374 store_counters_in_file(u_repl->filename, u_repl);
375 return;
376 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000377 optlen = u_repl->nentries * sizeof(struct ebt_counter) +
378 sizeof(struct ebt_replace);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000379 /* Now put the stuff in the kernel's struct ebt_replace */
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000380 repl.counters = sparc_cast u_repl->counters;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000381 repl.num_counters = u_repl->num_counters;
382 memcpy(repl.name, u_repl->name, sizeof(repl.name));
383
Bart De Schuymer6622a012005-01-19 21:09:05 +0000384 if (get_sockfd())
385 return;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000386 if (setsockopt(sockfd, IPPROTO_IP, EBT_SO_SET_COUNTERS, &repl, optlen))
Bart De Schuymer64182a32004-01-21 20:39:54 +0000387 ebt_print_bug("Couldn't update kernel counters");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000388}
389
390static int
391ebt_translate_match(struct ebt_entry_match *m, struct ebt_u_match_list ***l)
392{
393 struct ebt_u_match_list *new;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000394 int ret = 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000395
396 new = (struct ebt_u_match_list *)
397 malloc(sizeof(struct ebt_u_match_list));
398 if (!new)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000399 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000400 new->m = (struct ebt_entry_match *)
401 malloc(m->match_size + sizeof(struct ebt_entry_match));
402 if (!new->m)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000403 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000404 memcpy(new->m, m, m->match_size + sizeof(struct ebt_entry_match));
405 new->next = NULL;
406 **l = new;
407 *l = &new->next;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000408 if (ebt_find_match(new->m->u.name) == NULL) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000409 ebt_print_error("Kernel match %s unsupported by userspace tool",
410 new->m->u.name);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000411 ret = -1;
412 }
413 return ret;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000414}
415
416static int
417ebt_translate_watcher(struct ebt_entry_watcher *w,
418 struct ebt_u_watcher_list ***l)
419{
420 struct ebt_u_watcher_list *new;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000421 int ret = 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000422
423 new = (struct ebt_u_watcher_list *)
424 malloc(sizeof(struct ebt_u_watcher_list));
425 if (!new)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000426 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000427 new->w = (struct ebt_entry_watcher *)
428 malloc(w->watcher_size + sizeof(struct ebt_entry_watcher));
429 if (!new->w)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000430 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000431 memcpy(new->w, w, w->watcher_size + sizeof(struct ebt_entry_watcher));
432 new->next = NULL;
433 **l = new;
434 *l = &new->next;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000435 if (ebt_find_watcher(new->w->u.name) == NULL) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000436 ebt_print_error("Kernel watcher %s unsupported by userspace "
437 "tool", new->w->u.name);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000438 ret = -1;
439 }
440 return ret;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000441}
442
443static int
444ebt_translate_entry(struct ebt_entry *e, unsigned int *hook, int *n, int *cnt,
Bart De Schuymere94eaf72005-08-28 16:06:22 +0000445 int *totalcnt, struct ebt_u_entry **u_e, struct ebt_u_replace *u_repl,
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000446 unsigned int valid_hooks, char *base, struct ebt_cntchanges **cc)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000447{
Bart De Schuymer6622a012005-01-19 21:09:05 +0000448 /* An entry */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000449 if (e->bitmask & EBT_ENTRY_OR_ENTRIES) {
450 struct ebt_u_entry *new;
451 struct ebt_u_match_list **m_l;
452 struct ebt_u_watcher_list **w_l;
453 struct ebt_entry_target *t;
454
455 new = (struct ebt_u_entry *)malloc(sizeof(struct ebt_u_entry));
456 if (!new)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000457 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000458 new->bitmask = e->bitmask;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000459 /*
Bart De Schuymer6622a012005-01-19 21:09:05 +0000460 * Plain userspace code doesn't know about
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000461 * EBT_ENTRY_OR_ENTRIES
462 */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000463 new->bitmask &= ~EBT_ENTRY_OR_ENTRIES;
464 new->invflags = e->invflags;
465 new->ethproto = e->ethproto;
Bart De Schuymere3cceb72002-07-26 12:47:33 +0000466 strcpy(new->in, e->in);
467 strcpy(new->out, e->out);
468 strcpy(new->logical_in, e->logical_in);
469 strcpy(new->logical_out, e->logical_out);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000470 memcpy(new->sourcemac, e->sourcemac, sizeof(new->sourcemac));
471 memcpy(new->sourcemsk, e->sourcemsk, sizeof(new->sourcemsk));
472 memcpy(new->destmac, e->destmac, sizeof(new->destmac));
473 memcpy(new->destmsk, e->destmsk, sizeof(new->destmsk));
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000474 if (*totalcnt >= u_repl->nentries)
475 ebt_print_bug("*totalcnt >= u_repl->nentries");
476 new->cnt = u_repl->counters[*totalcnt];
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000477 new->cnt_surplus.pcnt = new->cnt_surplus.bcnt = 0;
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000478 new->cc = *cc;
479 *cc = (*cc)->next;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000480 new->m_list = NULL;
481 new->w_list = NULL;
Bart De Schuymere94eaf72005-08-28 16:06:22 +0000482 new->next = (*u_e)->next;
483 new->next->prev = new;
484 (*u_e)->next = new;
485 new->prev = *u_e;
486 *u_e = new;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000487 m_l = &new->m_list;
488 EBT_MATCH_ITERATE(e, ebt_translate_match, &m_l);
489 w_l = &new->w_list;
490 EBT_WATCHER_ITERATE(e, ebt_translate_watcher, &w_l);
491
492 t = (struct ebt_entry_target *)(((char *)e) + e->target_offset);
493 new->t = (struct ebt_entry_target *)
494 malloc(t->target_size + sizeof(struct ebt_entry_target));
495 if (!new->t)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000496 ebt_print_memory();
Bart De Schuymer6622a012005-01-19 21:09:05 +0000497 if (ebt_find_target(t->u.name) == NULL) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000498 ebt_print_error("Kernel target %s unsupported by "
499 "userspace tool", t->u.name);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000500 return -1;
501 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000502 memcpy(new->t, t, t->target_size +
503 sizeof(struct ebt_entry_target));
Bart De Schuymer6622a012005-01-19 21:09:05 +0000504 /* Deal with jumps to udc */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000505 if (!strcmp(t->u.name, EBT_STANDARD_TARGET)) {
506 char *tmp = base;
507 int verdict = ((struct ebt_standard_target *)t)->verdict;
508 int i;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000509
510 if (verdict >= 0) {
511 tmp += verdict;
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000512 for (i = NF_BR_NUMHOOKS; i < u_repl->num_chains; i++)
513 if (u_repl->chains[i]->kernel_start == tmp)
514 break;
515 if (i == u_repl->num_chains)
516 ebt_print_bug("Can't find udc for jump");
Bart De Schuymer60332e02002-06-23 08:01:47 +0000517 ((struct ebt_standard_target *)new->t)->verdict = i;
518 }
519 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000520
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000521 (*cnt)++;
522 (*totalcnt)++;
523 return 0;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000524 } else { /* A new chain */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000525 int i;
526 struct ebt_entries *entries = (struct ebt_entries *)e;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000527
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000528 if (*n != *cnt)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000529 ebt_print_bug("Nr of entries in the chain is wrong");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000530 *n = entries->nentries;
531 *cnt = 0;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000532 for (i = *hook + 1; i < NF_BR_NUMHOOKS; i++)
533 if (valid_hooks & (1 << i))
534 break;
535 *hook = i;
Bart De Schuymere94eaf72005-08-28 16:06:22 +0000536 *u_e = u_repl->chains[*hook]->entries;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000537 return 0;
538 }
539}
540
Bart De Schuymer6622a012005-01-19 21:09:05 +0000541/* Initialize all chain headers */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000542static int
543ebt_translate_chains(struct ebt_entry *e, unsigned int *hook,
544 struct ebt_u_replace *u_repl, unsigned int valid_hooks)
545{
546 int i;
547 struct ebt_entries *entries = (struct ebt_entries *)e;
548 struct ebt_u_entries *new;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000549
550 if (!(e->bitmask & EBT_ENTRY_OR_ENTRIES)) {
551 for (i = *hook + 1; i < NF_BR_NUMHOOKS; i++)
552 if (valid_hooks & (1 << i))
553 break;
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000554 new = (struct ebt_u_entries *)malloc(sizeof(struct ebt_u_entries));
555 if (!new)
556 ebt_print_memory();
557 if (i == u_repl->max_chains)
558 ebt_double_chains(u_repl);
559 u_repl->chains[i] = new;
560 if (i >= NF_BR_NUMHOOKS)
561 new->kernel_start = (char *)e;
562 *hook = i;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000563 new->nentries = entries->nentries;
564 new->policy = entries->policy;
Bart De Schuymere94eaf72005-08-28 16:06:22 +0000565 new->entries = (struct ebt_u_entry *)malloc(sizeof(struct ebt_u_entry));
566 if (!new->entries)
567 ebt_print_memory();
568 new->entries->next = new->entries->prev = new->entries;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000569 new->counter_offset = entries->counter_offset;
570 strcpy(new->name, entries->name);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000571 }
Bart De Schuymer60332e02002-06-23 08:01:47 +0000572 return 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000573}
574
Bart De Schuymer6622a012005-01-19 21:09:05 +0000575static int retrieve_from_file(char *filename, struct ebt_replace *repl,
Bart De Schuymer62423742002-07-14 19:06:20 +0000576 char command)
577{
578 FILE *file;
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000579 char *hlp = NULL, *entries;
580 struct ebt_counter *counters;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000581 int size, ret = 0;
Bart De Schuymer62423742002-07-14 19:06:20 +0000582
Bart De Schuymer6622a012005-01-19 21:09:05 +0000583 if (!(file = fopen(filename, "r+b"))) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000584 ebt_print_error("Could not open file %s", filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000585 return -1;
586 }
587 /* Make sure table name is right if command isn't -L or --atomic-commit */
Bart De Schuymer62423742002-07-14 19:06:20 +0000588 if (command != 'L' && command != 8) {
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000589 hlp = (char *)malloc(strlen(repl->name) + 1);
Bart De Schuymer62423742002-07-14 19:06:20 +0000590 if (!hlp)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000591 ebt_print_memory();
Bart De Schuymer62423742002-07-14 19:06:20 +0000592 strcpy(hlp, repl->name);
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000593 }
Bart De Schuymer62423742002-07-14 19:06:20 +0000594 if (fread(repl, sizeof(char), sizeof(struct ebt_replace), file)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000595 != sizeof(struct ebt_replace)) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000596 ebt_print_error("File %s is corrupt", filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000597 ret = -1;
598 goto close_file;
599 }
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000600 if (command != 'L' && command != 8 && strcmp(hlp, repl->name)) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000601 ebt_print_error("File %s contains wrong table name or is "
602 "corrupt", filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000603 ret = -1;
604 goto close_file;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000605 } else if (!ebt_find_table(repl->name)) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000606 ebt_print_error("File %s contains invalid table name",
607 filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000608 ret = -1;
609 goto close_file;
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000610 }
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000611
Bart De Schuymer62423742002-07-14 19:06:20 +0000612 size = sizeof(struct ebt_replace) +
613 repl->nentries * sizeof(struct ebt_counter) + repl->entries_size;
614 fseek(file, 0, SEEK_END);
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000615 if (size != ftell(file)) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000616 ebt_print_error("File %s has wrong size", filename);
Bart De Schuymer2f7e8d12005-01-19 21:23:02 +0000617 ret = -1;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000618 goto close_file;
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000619 }
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000620 entries = (char *)malloc(repl->entries_size);
621 if (!entries)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000622 ebt_print_memory();
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000623 repl->entries = sparc_cast entries;
Bart De Schuymer62423742002-07-14 19:06:20 +0000624 if (repl->nentries) {
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000625 counters = (struct ebt_counter *)
Bart De Schuymer62423742002-07-14 19:06:20 +0000626 malloc(repl->nentries * sizeof(struct ebt_counter));
Bart De Schuymer8e96bee2003-08-27 16:59:39 +0000627 repl->counters = sparc_cast counters;
Bart De Schuymer62423742002-07-14 19:06:20 +0000628 if (!repl->counters)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000629 ebt_print_memory();
Bart De Schuymer62423742002-07-14 19:06:20 +0000630 } else
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000631 repl->counters = sparc_cast NULL;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000632 /* Copy entries and counters */
Bart De Schuymer62423742002-07-14 19:06:20 +0000633 if (fseek(file, sizeof(struct ebt_replace), SEEK_SET) ||
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000634 fread((char *)repl->entries, sizeof(char), repl->entries_size, file)
Bart De Schuymer62423742002-07-14 19:06:20 +0000635 != repl->entries_size ||
Bart De Schuymer64182a32004-01-21 20:39:54 +0000636 fseek(file, sizeof(struct ebt_replace) + repl->entries_size,
637 SEEK_SET)
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000638 || fread((char *)repl->counters, sizeof(char),
Bart De Schuymer62423742002-07-14 19:06:20 +0000639 repl->nentries * sizeof(struct ebt_counter), file)
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000640 != repl->nentries * sizeof(struct ebt_counter)) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000641 ebt_print_error("File %s is corrupt", filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000642 free(entries);
643 repl->entries = NULL;
644 ret = -1;
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000645 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000646close_file:
Bart De Schuymer62423742002-07-14 19:06:20 +0000647 fclose(file);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000648 free(hlp);
649 return ret;
Bart De Schuymer62423742002-07-14 19:06:20 +0000650}
651
Bart De Schuymer64182a32004-01-21 20:39:54 +0000652static int retrieve_from_kernel(struct ebt_replace *repl, char command,
653 int init)
Bart De Schuymer62423742002-07-14 19:06:20 +0000654{
655 socklen_t optlen;
656 int optname;
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000657 char *entries;
Bart De Schuymer62423742002-07-14 19:06:20 +0000658
659 optlen = sizeof(struct ebt_replace);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000660 if (get_sockfd())
661 return -1;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000662 /* --atomic-init || --init-table */
Bart De Schuymer64182a32004-01-21 20:39:54 +0000663 if (init)
Bart De Schuymer62423742002-07-14 19:06:20 +0000664 optname = EBT_SO_GET_INIT_INFO;
665 else
666 optname = EBT_SO_GET_INFO;
667 if (getsockopt(sockfd, IPPROTO_IP, optname, repl, &optlen))
668 return -1;
669
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000670 if ( !(entries = (char *)malloc(repl->entries_size)) )
Bart De Schuymer64182a32004-01-21 20:39:54 +0000671 ebt_print_memory();
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000672 repl->entries = sparc_cast entries;
Bart De Schuymer62423742002-07-14 19:06:20 +0000673 if (repl->nentries) {
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000674 struct ebt_counter *counters;
675
676 if (!(counters = (struct ebt_counter *)
Bart De Schuymer62423742002-07-14 19:06:20 +0000677 malloc(repl->nentries * sizeof(struct ebt_counter))) )
Bart De Schuymer64182a32004-01-21 20:39:54 +0000678 ebt_print_memory();
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000679 repl->counters = sparc_cast counters;
Bart De Schuymer62423742002-07-14 19:06:20 +0000680 }
681 else
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000682 repl->counters = sparc_cast NULL;
Bart De Schuymer62423742002-07-14 19:06:20 +0000683
Bart De Schuymer6622a012005-01-19 21:09:05 +0000684 /* We want to receive the counters */
Bart De Schuymer62423742002-07-14 19:06:20 +0000685 repl->num_counters = repl->nentries;
686 optlen += repl->entries_size + repl->num_counters *
687 sizeof(struct ebt_counter);
Bart De Schuymer64182a32004-01-21 20:39:54 +0000688 if (init)
Bart De Schuymer62423742002-07-14 19:06:20 +0000689 optname = EBT_SO_GET_INIT_ENTRIES;
690 else
691 optname = EBT_SO_GET_ENTRIES;
692 if (getsockopt(sockfd, IPPROTO_IP, optname, repl, &optlen))
Bart De Schuymer6622a012005-01-19 21:09:05 +0000693 ebt_print_bug("Hmm, what is wrong??? bug#1");
Bart De Schuymer62423742002-07-14 19:06:20 +0000694
695 return 0;
696}
697
Bart De Schuymer64182a32004-01-21 20:39:54 +0000698int ebt_get_table(struct ebt_u_replace *u_repl, int init)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000699{
700 int i, j, k, hook;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000701 struct ebt_replace repl;
Bart De Schuymere94eaf72005-08-28 16:06:22 +0000702 struct ebt_u_entry *u_e;
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000703 struct ebt_cntchanges *new_cc, *cc;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000704
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000705 strcpy(repl.name, u_repl->name);
Bart De Schuymer62a7fdb2002-08-24 21:01:21 +0000706 if (u_repl->filename != NULL) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000707 if (init)
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000708 ebt_print_bug("Getting initial table data from a file is impossible");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000709 if (retrieve_from_file(u_repl->filename, &repl, u_repl->command))
710 return -1;
711 /* -L with a wrong table name should be dealt with silently */
Bart De Schuymer62a7fdb2002-08-24 21:01:21 +0000712 strcpy(u_repl->name, repl.name);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000713 } else if (retrieve_from_kernel(&repl, u_repl->command, init))
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000714 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000715
Bart De Schuymer6622a012005-01-19 21:09:05 +0000716 /* Translate the struct ebt_replace to a struct ebt_u_replace */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000717 u_repl->valid_hooks = repl.valid_hooks;
718 u_repl->nentries = repl.nentries;
719 u_repl->num_counters = repl.num_counters;
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000720 u_repl->counters = repl.counters;
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000721 u_repl->cc = (struct ebt_cntchanges *)malloc(sizeof(struct ebt_cntchanges));
722 if (!u_repl->cc)
723 ebt_print_memory();
724 u_repl->cc->next = u_repl->cc->prev = u_repl->cc;
725 cc = u_repl->cc;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000726 for (i = 0; i < repl.nentries; i++) {
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000727 new_cc = (struct ebt_cntchanges *)malloc(sizeof(struct ebt_cntchanges));
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000728 if (!new_cc)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000729 ebt_print_memory();
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000730 new_cc->type = CNT_NORM;
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000731 new_cc->change = 0;
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000732 new_cc->prev = cc;
733 cc->next = new_cc;
734 cc = new_cc;
735 }
736 if (repl.nentries) {
737 new_cc->next = u_repl->cc;
738 u_repl->cc->prev = new_cc;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000739 }
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000740 u_repl->chains = (struct ebt_u_entries **)calloc(EBT_ORI_MAX_CHAINS, sizeof(void *));
741 u_repl->max_chains = EBT_ORI_MAX_CHAINS;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000742 hook = -1;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000743 /* FIXME: Clean up when an error is encountered */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000744 EBT_ENTRY_ITERATE(repl.entries, repl.entries_size, ebt_translate_chains,
745 &hook, u_repl, u_repl->valid_hooks);
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000746 if (hook >= NF_BR_NUMHOOKS)
747 u_repl->num_chains = hook + 1;
748 else
749 u_repl->num_chains = NF_BR_NUMHOOKS;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000750 i = 0; /* Holds the expected nr. of entries for the chain */
751 j = 0; /* Holds the up to now counted entries for the chain */
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000752 k = 0; /* Holds the total nr. of entries, should equal u_repl->nentries afterwards */
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000753 cc = u_repl->cc->next;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000754 hook = -1;
Bart De Schuymer64182a32004-01-21 20:39:54 +0000755 EBT_ENTRY_ITERATE((char *)repl.entries, repl.entries_size,
756 ebt_translate_entry, &hook, &i, &j, &k, &u_e, u_repl,
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000757 u_repl->valid_hooks, (char *)repl.entries, &cc);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000758 if (k != u_repl->nentries)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000759 ebt_print_bug("Wrong total nentries");
Bart De Schuymer92579fe2005-09-01 20:35:43 +0000760 free(repl.entries);
Bart De Schuymer9ce6ee92002-06-14 21:56:35 +0000761 return 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000762}