blob: 02ea1ebe24bc31547f497657d675a72741e8d95a [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) {
Bart De Schuymer97783d22009-11-04 21:39:26 +0000306 while (chainnr < u_repl->num_chains && (!(entries = u_repl->chains[chainnr]) ||
307 (next = entries->entries->next) == entries->entries))
308 chainnr++;
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000309 if (chainnr == u_repl->num_chains)
310 break;
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000311 }
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000312 if (cc->type == CNT_NORM) {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000313 /* 'Normal' rule, meaning we didn't do anything to it
314 * So, we just copy */
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000315 *new = *old;
Bart De Schuymerab611e22005-02-14 20:20:03 +0000316 next->cnt = *new;
317 next->cnt_surplus.pcnt = next->cnt_surplus.bcnt = 0;
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000318 old++; /* We've used an old counter */
319 new++; /* We've set a new counter */
Bart De Schuymerff587202005-02-08 20:02:28 +0000320 next = next->next;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000321 } else if (cc->type == CNT_DEL) {
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000322 old++; /* Don't use this old counter */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000323 } else {
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000324 if (cc->type == CNT_CHANGE) {
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000325 if (cc->change % 3 == 1)
326 new->pcnt = old->pcnt + next->cnt_surplus.pcnt;
327 else if (cc->change % 3 == 2)
328 new->pcnt = old->pcnt - next->cnt_surplus.pcnt;
329 else
330 new->pcnt = next->cnt.pcnt;
331 if (cc->change / 3 == 1)
332 new->bcnt = old->bcnt + next->cnt_surplus.bcnt;
333 else if (cc->change / 3 == 2)
334 new->bcnt = old->bcnt - next->cnt_surplus.bcnt;
335 else
336 new->bcnt = next->cnt.bcnt;
Bart De Schuymerab611e22005-02-14 20:20:03 +0000337 } else
338 *new = next->cnt;
339 next->cnt = *new;
340 next->cnt_surplus.pcnt = next->cnt_surplus.bcnt = 0;
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000341 if (cc->type == CNT_ADD)
342 new++;
343 else {
344 old++;
345 new++;
346 }
Bart De Schuymerff587202005-02-08 20:02:28 +0000347 next = next->next;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000348 }
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000349 cc = cc->next;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000350 }
351
352 free(u_repl->counters);
353 u_repl->counters = newcounters;
354 u_repl->num_counters = u_repl->nentries;
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000355 /* Reset the counterchanges to CNT_NORM and delete the unused cc */
356 i = 0;
357 cc = u_repl->cc->next;
358 while (cc != u_repl->cc) {
359 if (cc->type == CNT_DEL) {
360 cc->prev->next = cc->next;
361 cc->next->prev = cc->prev;
362 cc2 = cc->next;
363 free(cc);
364 cc = cc2;
365 } else {
366 cc->type = CNT_NORM;
367 cc->change = 0;
368 i++;
369 cc = cc->next;
370 }
371 }
372 if (i != u_repl->nentries)
373 ebt_print_bug("i != u_repl->nentries");
Bart De Schuymer62423742002-07-14 19:06:20 +0000374 if (u_repl->filename != NULL) {
375 store_counters_in_file(u_repl->filename, u_repl);
376 return;
377 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000378 optlen = u_repl->nentries * sizeof(struct ebt_counter) +
379 sizeof(struct ebt_replace);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000380 /* Now put the stuff in the kernel's struct ebt_replace */
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000381 repl.counters = sparc_cast u_repl->counters;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000382 repl.num_counters = u_repl->num_counters;
383 memcpy(repl.name, u_repl->name, sizeof(repl.name));
384
Bart De Schuymer6622a012005-01-19 21:09:05 +0000385 if (get_sockfd())
386 return;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000387 if (setsockopt(sockfd, IPPROTO_IP, EBT_SO_SET_COUNTERS, &repl, optlen))
Bart De Schuymer64182a32004-01-21 20:39:54 +0000388 ebt_print_bug("Couldn't update kernel counters");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000389}
390
391static int
392ebt_translate_match(struct ebt_entry_match *m, struct ebt_u_match_list ***l)
393{
394 struct ebt_u_match_list *new;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000395 int ret = 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000396
397 new = (struct ebt_u_match_list *)
398 malloc(sizeof(struct ebt_u_match_list));
399 if (!new)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000400 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000401 new->m = (struct ebt_entry_match *)
402 malloc(m->match_size + sizeof(struct ebt_entry_match));
403 if (!new->m)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000404 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000405 memcpy(new->m, m, m->match_size + sizeof(struct ebt_entry_match));
406 new->next = NULL;
407 **l = new;
408 *l = &new->next;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000409 if (ebt_find_match(new->m->u.name) == NULL) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000410 ebt_print_error("Kernel match %s unsupported by userspace tool",
411 new->m->u.name);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000412 ret = -1;
413 }
414 return ret;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000415}
416
417static int
418ebt_translate_watcher(struct ebt_entry_watcher *w,
419 struct ebt_u_watcher_list ***l)
420{
421 struct ebt_u_watcher_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_watcher_list *)
425 malloc(sizeof(struct ebt_u_watcher_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->w = (struct ebt_entry_watcher *)
429 malloc(w->watcher_size + sizeof(struct ebt_entry_watcher));
430 if (!new->w)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000431 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000432 memcpy(new->w, w, w->watcher_size + sizeof(struct ebt_entry_watcher));
433 new->next = NULL;
434 **l = new;
435 *l = &new->next;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000436 if (ebt_find_watcher(new->w->u.name) == NULL) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000437 ebt_print_error("Kernel watcher %s unsupported by userspace "
438 "tool", new->w->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
Bart De Schuymer510c9ce2006-01-23 18:50:54 +0000445ebt_translate_entry(struct ebt_entry *e, int *hook, int *n, int *cnt,
Bart De Schuymere94eaf72005-08-28 16:06:22 +0000446 int *totalcnt, struct ebt_u_entry **u_e, struct ebt_u_replace *u_repl,
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000447 unsigned int valid_hooks, char *base, struct ebt_cntchanges **cc)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000448{
Bart De Schuymer6622a012005-01-19 21:09:05 +0000449 /* An entry */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000450 if (e->bitmask & EBT_ENTRY_OR_ENTRIES) {
451 struct ebt_u_entry *new;
452 struct ebt_u_match_list **m_l;
453 struct ebt_u_watcher_list **w_l;
454 struct ebt_entry_target *t;
455
456 new = (struct ebt_u_entry *)malloc(sizeof(struct ebt_u_entry));
457 if (!new)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000458 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000459 new->bitmask = e->bitmask;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000460 /*
Bart De Schuymer6622a012005-01-19 21:09:05 +0000461 * Plain userspace code doesn't know about
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000462 * EBT_ENTRY_OR_ENTRIES
463 */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000464 new->bitmask &= ~EBT_ENTRY_OR_ENTRIES;
465 new->invflags = e->invflags;
466 new->ethproto = e->ethproto;
Bart De Schuymere3cceb72002-07-26 12:47:33 +0000467 strcpy(new->in, e->in);
468 strcpy(new->out, e->out);
469 strcpy(new->logical_in, e->logical_in);
470 strcpy(new->logical_out, e->logical_out);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000471 memcpy(new->sourcemac, e->sourcemac, sizeof(new->sourcemac));
472 memcpy(new->sourcemsk, e->sourcemsk, sizeof(new->sourcemsk));
473 memcpy(new->destmac, e->destmac, sizeof(new->destmac));
474 memcpy(new->destmsk, e->destmsk, sizeof(new->destmsk));
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000475 if (*totalcnt >= u_repl->nentries)
476 ebt_print_bug("*totalcnt >= u_repl->nentries");
477 new->cnt = u_repl->counters[*totalcnt];
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000478 new->cnt_surplus.pcnt = new->cnt_surplus.bcnt = 0;
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000479 new->cc = *cc;
480 *cc = (*cc)->next;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000481 new->m_list = NULL;
482 new->w_list = NULL;
Bart De Schuymere94eaf72005-08-28 16:06:22 +0000483 new->next = (*u_e)->next;
484 new->next->prev = new;
485 (*u_e)->next = new;
486 new->prev = *u_e;
487 *u_e = new;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000488 m_l = &new->m_list;
489 EBT_MATCH_ITERATE(e, ebt_translate_match, &m_l);
490 w_l = &new->w_list;
491 EBT_WATCHER_ITERATE(e, ebt_translate_watcher, &w_l);
492
493 t = (struct ebt_entry_target *)(((char *)e) + e->target_offset);
494 new->t = (struct ebt_entry_target *)
495 malloc(t->target_size + sizeof(struct ebt_entry_target));
496 if (!new->t)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000497 ebt_print_memory();
Bart De Schuymer6622a012005-01-19 21:09:05 +0000498 if (ebt_find_target(t->u.name) == NULL) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000499 ebt_print_error("Kernel target %s unsupported by "
500 "userspace tool", t->u.name);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000501 return -1;
502 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000503 memcpy(new->t, t, t->target_size +
504 sizeof(struct ebt_entry_target));
Bart De Schuymer6622a012005-01-19 21:09:05 +0000505 /* Deal with jumps to udc */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000506 if (!strcmp(t->u.name, EBT_STANDARD_TARGET)) {
507 char *tmp = base;
508 int verdict = ((struct ebt_standard_target *)t)->verdict;
509 int i;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000510
511 if (verdict >= 0) {
512 tmp += verdict;
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000513 for (i = NF_BR_NUMHOOKS; i < u_repl->num_chains; i++)
514 if (u_repl->chains[i]->kernel_start == tmp)
515 break;
516 if (i == u_repl->num_chains)
517 ebt_print_bug("Can't find udc for jump");
Bart De Schuymer201e1b72006-03-13 19:31:13 +0000518 ((struct ebt_standard_target *)new->t)->verdict = i-NF_BR_NUMHOOKS;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000519 }
520 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000521
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000522 (*cnt)++;
523 (*totalcnt)++;
524 return 0;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000525 } else { /* A new chain */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000526 int i;
527 struct ebt_entries *entries = (struct ebt_entries *)e;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000528
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000529 if (*n != *cnt)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000530 ebt_print_bug("Nr of entries in the chain is wrong");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000531 *n = entries->nentries;
532 *cnt = 0;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000533 for (i = *hook + 1; i < NF_BR_NUMHOOKS; i++)
534 if (valid_hooks & (1 << i))
535 break;
536 *hook = i;
Bart De Schuymere94eaf72005-08-28 16:06:22 +0000537 *u_e = u_repl->chains[*hook]->entries;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000538 return 0;
539 }
540}
541
Bart De Schuymer6622a012005-01-19 21:09:05 +0000542/* Initialize all chain headers */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000543static int
Bart De Schuymer510c9ce2006-01-23 18:50:54 +0000544ebt_translate_chains(struct ebt_entry *e, int *hook,
Bart De Schuymer60332e02002-06-23 08:01:47 +0000545 struct ebt_u_replace *u_repl, unsigned int valid_hooks)
546{
547 int i;
548 struct ebt_entries *entries = (struct ebt_entries *)e;
549 struct ebt_u_entries *new;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000550
551 if (!(e->bitmask & EBT_ENTRY_OR_ENTRIES)) {
552 for (i = *hook + 1; i < NF_BR_NUMHOOKS; i++)
553 if (valid_hooks & (1 << i))
554 break;
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000555 new = (struct ebt_u_entries *)malloc(sizeof(struct ebt_u_entries));
556 if (!new)
557 ebt_print_memory();
558 if (i == u_repl->max_chains)
559 ebt_double_chains(u_repl);
560 u_repl->chains[i] = new;
561 if (i >= NF_BR_NUMHOOKS)
562 new->kernel_start = (char *)e;
563 *hook = i;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000564 new->nentries = entries->nentries;
565 new->policy = entries->policy;
Bart De Schuymere94eaf72005-08-28 16:06:22 +0000566 new->entries = (struct ebt_u_entry *)malloc(sizeof(struct ebt_u_entry));
567 if (!new->entries)
568 ebt_print_memory();
569 new->entries->next = new->entries->prev = new->entries;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000570 new->counter_offset = entries->counter_offset;
571 strcpy(new->name, entries->name);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000572 }
Bart De Schuymer60332e02002-06-23 08:01:47 +0000573 return 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000574}
575
Bart De Schuymer6622a012005-01-19 21:09:05 +0000576static int retrieve_from_file(char *filename, struct ebt_replace *repl,
Bart De Schuymer62423742002-07-14 19:06:20 +0000577 char command)
578{
579 FILE *file;
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000580 char *hlp = NULL, *entries;
581 struct ebt_counter *counters;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000582 int size, ret = 0;
Bart De Schuymer62423742002-07-14 19:06:20 +0000583
Bart De Schuymer6622a012005-01-19 21:09:05 +0000584 if (!(file = fopen(filename, "r+b"))) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000585 ebt_print_error("Could not open file %s", filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000586 return -1;
587 }
588 /* Make sure table name is right if command isn't -L or --atomic-commit */
Bart De Schuymer62423742002-07-14 19:06:20 +0000589 if (command != 'L' && command != 8) {
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000590 hlp = (char *)malloc(strlen(repl->name) + 1);
Bart De Schuymer62423742002-07-14 19:06:20 +0000591 if (!hlp)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000592 ebt_print_memory();
Bart De Schuymer62423742002-07-14 19:06:20 +0000593 strcpy(hlp, repl->name);
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000594 }
Bart De Schuymer62423742002-07-14 19:06:20 +0000595 if (fread(repl, sizeof(char), sizeof(struct ebt_replace), file)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000596 != sizeof(struct ebt_replace)) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000597 ebt_print_error("File %s is corrupt", filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000598 ret = -1;
599 goto close_file;
600 }
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000601 if (command != 'L' && command != 8 && strcmp(hlp, repl->name)) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000602 ebt_print_error("File %s contains wrong table name or is "
603 "corrupt", filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000604 ret = -1;
605 goto close_file;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000606 } else if (!ebt_find_table(repl->name)) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000607 ebt_print_error("File %s contains invalid table name",
608 filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000609 ret = -1;
610 goto close_file;
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000611 }
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000612
Bart De Schuymer62423742002-07-14 19:06:20 +0000613 size = sizeof(struct ebt_replace) +
614 repl->nentries * sizeof(struct ebt_counter) + repl->entries_size;
615 fseek(file, 0, SEEK_END);
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000616 if (size != ftell(file)) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000617 ebt_print_error("File %s has wrong size", filename);
Bart De Schuymer2f7e8d12005-01-19 21:23:02 +0000618 ret = -1;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000619 goto close_file;
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000620 }
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000621 entries = (char *)malloc(repl->entries_size);
622 if (!entries)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000623 ebt_print_memory();
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000624 repl->entries = sparc_cast entries;
Bart De Schuymer62423742002-07-14 19:06:20 +0000625 if (repl->nentries) {
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000626 counters = (struct ebt_counter *)
Bart De Schuymer62423742002-07-14 19:06:20 +0000627 malloc(repl->nentries * sizeof(struct ebt_counter));
Bart De Schuymer8e96bee2003-08-27 16:59:39 +0000628 repl->counters = sparc_cast counters;
Bart De Schuymer62423742002-07-14 19:06:20 +0000629 if (!repl->counters)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000630 ebt_print_memory();
Bart De Schuymer62423742002-07-14 19:06:20 +0000631 } else
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000632 repl->counters = sparc_cast NULL;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000633 /* Copy entries and counters */
Bart De Schuymer62423742002-07-14 19:06:20 +0000634 if (fseek(file, sizeof(struct ebt_replace), SEEK_SET) ||
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000635 fread((char *)repl->entries, sizeof(char), repl->entries_size, file)
Bart De Schuymer62423742002-07-14 19:06:20 +0000636 != repl->entries_size ||
Bart De Schuymer64182a32004-01-21 20:39:54 +0000637 fseek(file, sizeof(struct ebt_replace) + repl->entries_size,
638 SEEK_SET)
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000639 || fread((char *)repl->counters, sizeof(char),
Bart De Schuymer62423742002-07-14 19:06:20 +0000640 repl->nentries * sizeof(struct ebt_counter), file)
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000641 != repl->nentries * sizeof(struct ebt_counter)) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000642 ebt_print_error("File %s is corrupt", filename);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000643 free(entries);
644 repl->entries = NULL;
645 ret = -1;
Bart De Schuymere8890ff2002-07-15 20:29:04 +0000646 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000647close_file:
Bart De Schuymer62423742002-07-14 19:06:20 +0000648 fclose(file);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000649 free(hlp);
650 return ret;
Bart De Schuymer62423742002-07-14 19:06:20 +0000651}
652
Bart De Schuymer64182a32004-01-21 20:39:54 +0000653static int retrieve_from_kernel(struct ebt_replace *repl, char command,
654 int init)
Bart De Schuymer62423742002-07-14 19:06:20 +0000655{
656 socklen_t optlen;
657 int optname;
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000658 char *entries;
Bart De Schuymer62423742002-07-14 19:06:20 +0000659
660 optlen = sizeof(struct ebt_replace);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000661 if (get_sockfd())
662 return -1;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000663 /* --atomic-init || --init-table */
Bart De Schuymer64182a32004-01-21 20:39:54 +0000664 if (init)
Bart De Schuymer62423742002-07-14 19:06:20 +0000665 optname = EBT_SO_GET_INIT_INFO;
666 else
667 optname = EBT_SO_GET_INFO;
668 if (getsockopt(sockfd, IPPROTO_IP, optname, repl, &optlen))
669 return -1;
670
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000671 if ( !(entries = (char *)malloc(repl->entries_size)) )
Bart De Schuymer64182a32004-01-21 20:39:54 +0000672 ebt_print_memory();
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000673 repl->entries = sparc_cast entries;
Bart De Schuymer62423742002-07-14 19:06:20 +0000674 if (repl->nentries) {
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000675 struct ebt_counter *counters;
676
677 if (!(counters = (struct ebt_counter *)
Bart De Schuymer62423742002-07-14 19:06:20 +0000678 malloc(repl->nentries * sizeof(struct ebt_counter))) )
Bart De Schuymer64182a32004-01-21 20:39:54 +0000679 ebt_print_memory();
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000680 repl->counters = sparc_cast counters;
Bart De Schuymer62423742002-07-14 19:06:20 +0000681 }
682 else
Bart De Schuymerf81c2702003-07-23 21:07:04 +0000683 repl->counters = sparc_cast NULL;
Bart De Schuymer62423742002-07-14 19:06:20 +0000684
Bart De Schuymer6622a012005-01-19 21:09:05 +0000685 /* We want to receive the counters */
Bart De Schuymer62423742002-07-14 19:06:20 +0000686 repl->num_counters = repl->nentries;
687 optlen += repl->entries_size + repl->num_counters *
688 sizeof(struct ebt_counter);
Bart De Schuymer64182a32004-01-21 20:39:54 +0000689 if (init)
Bart De Schuymer62423742002-07-14 19:06:20 +0000690 optname = EBT_SO_GET_INIT_ENTRIES;
691 else
692 optname = EBT_SO_GET_ENTRIES;
693 if (getsockopt(sockfd, IPPROTO_IP, optname, repl, &optlen))
Bart De Schuymer6622a012005-01-19 21:09:05 +0000694 ebt_print_bug("Hmm, what is wrong??? bug#1");
Bart De Schuymer62423742002-07-14 19:06:20 +0000695
696 return 0;
697}
698
Bart De Schuymer64182a32004-01-21 20:39:54 +0000699int ebt_get_table(struct ebt_u_replace *u_repl, int init)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000700{
701 int i, j, k, hook;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000702 struct ebt_replace repl;
Bart De Schuymere94eaf72005-08-28 16:06:22 +0000703 struct ebt_u_entry *u_e;
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000704 struct ebt_cntchanges *new_cc, *cc;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000705
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000706 strcpy(repl.name, u_repl->name);
Bart De Schuymer62a7fdb2002-08-24 21:01:21 +0000707 if (u_repl->filename != NULL) {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000708 if (init)
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000709 ebt_print_bug("Getting initial table data from a file is impossible");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000710 if (retrieve_from_file(u_repl->filename, &repl, u_repl->command))
711 return -1;
712 /* -L with a wrong table name should be dealt with silently */
Bart De Schuymer62a7fdb2002-08-24 21:01:21 +0000713 strcpy(u_repl->name, repl.name);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000714 } else if (retrieve_from_kernel(&repl, u_repl->command, init))
Bart De Schuymer0cff9e92002-07-25 12:29:50 +0000715 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000716
Bart De Schuymer6622a012005-01-19 21:09:05 +0000717 /* Translate the struct ebt_replace to a struct ebt_u_replace */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000718 u_repl->valid_hooks = repl.valid_hooks;
719 u_repl->nentries = repl.nentries;
720 u_repl->num_counters = repl.num_counters;
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000721 u_repl->counters = repl.counters;
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000722 u_repl->cc = (struct ebt_cntchanges *)malloc(sizeof(struct ebt_cntchanges));
723 if (!u_repl->cc)
724 ebt_print_memory();
725 u_repl->cc->next = u_repl->cc->prev = u_repl->cc;
726 cc = u_repl->cc;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000727 for (i = 0; i < repl.nentries; i++) {
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000728 new_cc = (struct ebt_cntchanges *)malloc(sizeof(struct ebt_cntchanges));
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000729 if (!new_cc)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000730 ebt_print_memory();
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000731 new_cc->type = CNT_NORM;
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000732 new_cc->change = 0;
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000733 new_cc->prev = cc;
734 cc->next = new_cc;
735 cc = new_cc;
736 }
737 if (repl.nentries) {
738 new_cc->next = u_repl->cc;
739 u_repl->cc->prev = new_cc;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000740 }
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000741 u_repl->chains = (struct ebt_u_entries **)calloc(EBT_ORI_MAX_CHAINS, sizeof(void *));
742 u_repl->max_chains = EBT_ORI_MAX_CHAINS;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000743 hook = -1;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000744 /* FIXME: Clean up when an error is encountered */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000745 EBT_ENTRY_ITERATE(repl.entries, repl.entries_size, ebt_translate_chains,
746 &hook, u_repl, u_repl->valid_hooks);
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000747 if (hook >= NF_BR_NUMHOOKS)
748 u_repl->num_chains = hook + 1;
749 else
750 u_repl->num_chains = NF_BR_NUMHOOKS;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000751 i = 0; /* Holds the expected nr. of entries for the chain */
752 j = 0; /* Holds the up to now counted entries for the chain */
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000753 k = 0; /* Holds the total nr. of entries, should equal u_repl->nentries afterwards */
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000754 cc = u_repl->cc->next;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000755 hook = -1;
Bart De Schuymer64182a32004-01-21 20:39:54 +0000756 EBT_ENTRY_ITERATE((char *)repl.entries, repl.entries_size,
757 ebt_translate_entry, &hook, &i, &j, &k, &u_e, u_repl,
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000758 u_repl->valid_hooks, (char *)repl.entries, &cc);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000759 if (k != u_repl->nentries)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000760 ebt_print_bug("Wrong total nentries");
Bart De Schuymer92579fe2005-09-01 20:35:43 +0000761 free(repl.entries);
Bart De Schuymer9ce6ee92002-06-14 21:56:35 +0000762 return 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000763}