blob: a0cdc2f8ecc7b81a25ac04efb443f3bbdc36dd3d [file] [log] [blame]
Rusty Russell79dee072000-05-02 16:45:16 +00001/* Library which manipulates firewall rules. Version 0.1. */
2
3/* Architecture of firewall rules is as follows:
4 *
5 * Chains go INPUT, FORWARD, OUTPUT then user chains.
6 * Each user chain starts with an ERROR node.
7 * Every chain ends with an unconditional jump: a RETURN for user chains,
8 * and a POLICY for built-ins.
9 */
10
11/* (C)1999 Paul ``Rusty'' Russell - Placed under the GNU GPL (See
12 COPYING for details). */
13
14#include <assert.h>
15#include <string.h>
16#include <errno.h>
17#include <stdlib.h>
18#include <stdio.h>
Marc Boucher1afc3b62002-01-19 12:46:39 +000019#include <unistd.h>
Rusty Russell79dee072000-05-02 16:45:16 +000020
21#ifdef DEBUG_CONNTRACK
22#define inline
23#endif
24
25#if !defined(__GLIBC__) || (__GLIBC__ < 2)
26typedef unsigned int socklen_t;
27#endif
28
29#include "libiptc/libiptc.h"
30
31#define IP_VERSION 4
32#define IP_OFFSET 0x1FFF
33
34#define HOOK_PRE_ROUTING NF_IP_PRE_ROUTING
35#define HOOK_LOCAL_IN NF_IP_LOCAL_IN
36#define HOOK_FORWARD NF_IP_FORWARD
37#define HOOK_LOCAL_OUT NF_IP_LOCAL_OUT
38#define HOOK_POST_ROUTING NF_IP_POST_ROUTING
Rusty Russell10758b72000-09-14 07:37:33 +000039#ifdef NF_IP_DROPPING
40#define HOOK_DROPPING NF_IP_DROPPING
41#endif
Rusty Russell79dee072000-05-02 16:45:16 +000042
43#define STRUCT_ENTRY_TARGET struct ipt_entry_target
44#define STRUCT_ENTRY struct ipt_entry
45#define STRUCT_ENTRY_MATCH struct ipt_entry_match
46#define STRUCT_GETINFO struct ipt_getinfo
47#define STRUCT_GET_ENTRIES struct ipt_get_entries
48#define STRUCT_COUNTERS struct ipt_counters
49#define STRUCT_COUNTERS_INFO struct ipt_counters_info
50#define STRUCT_STANDARD_TARGET struct ipt_standard_target
51#define STRUCT_REPLACE struct ipt_replace
52
53#define STRUCT_TC_HANDLE struct iptc_handle
54#define TC_HANDLE_T iptc_handle_t
55
56#define ENTRY_ITERATE IPT_ENTRY_ITERATE
57#define TABLE_MAXNAMELEN IPT_TABLE_MAXNAMELEN
58#define FUNCTION_MAXNAMELEN IPT_FUNCTION_MAXNAMELEN
59
60#define GET_TARGET ipt_get_target
61
62#define ERROR_TARGET IPT_ERROR_TARGET
63#define NUMHOOKS NF_IP_NUMHOOKS
64
65#define IPT_CHAINLABEL ipt_chainlabel
66
67#define TC_DUMP_ENTRIES dump_entries
68#define TC_IS_CHAIN iptc_is_chain
Philip Blundell8c700902000-05-15 02:17:52 +000069#define TC_FIRST_CHAIN iptc_first_chain
Rusty Russell79dee072000-05-02 16:45:16 +000070#define TC_NEXT_CHAIN iptc_next_chain
Philip Blundell8c700902000-05-15 02:17:52 +000071#define TC_FIRST_RULE iptc_first_rule
72#define TC_NEXT_RULE iptc_next_rule
Rusty Russell79dee072000-05-02 16:45:16 +000073#define TC_GET_TARGET iptc_get_target
74#define TC_BUILTIN iptc_builtin
75#define TC_GET_POLICY iptc_get_policy
76#define TC_INSERT_ENTRY iptc_insert_entry
77#define TC_REPLACE_ENTRY iptc_replace_entry
78#define TC_APPEND_ENTRY iptc_append_entry
79#define TC_DELETE_ENTRY iptc_delete_entry
80#define TC_DELETE_NUM_ENTRY iptc_delete_num_entry
81#define TC_CHECK_PACKET iptc_check_packet
82#define TC_FLUSH_ENTRIES iptc_flush_entries
83#define TC_ZERO_ENTRIES iptc_zero_entries
Harald Welte1cef74d2001-01-05 15:22:59 +000084#define TC_READ_COUNTER iptc_read_counter
85#define TC_ZERO_COUNTER iptc_zero_counter
86#define TC_SET_COUNTER iptc_set_counter
Rusty Russell79dee072000-05-02 16:45:16 +000087#define TC_CREATE_CHAIN iptc_create_chain
88#define TC_GET_REFERENCES iptc_get_references
89#define TC_DELETE_CHAIN iptc_delete_chain
90#define TC_RENAME_CHAIN iptc_rename_chain
91#define TC_SET_POLICY iptc_set_policy
92#define TC_GET_RAW_SOCKET iptc_get_raw_socket
93#define TC_INIT iptc_init
Martin Josefsson841e4ae2003-05-02 15:30:11 +000094#define TC_FREE iptc_free
Rusty Russell79dee072000-05-02 16:45:16 +000095#define TC_COMMIT iptc_commit
96#define TC_STRERROR iptc_strerror
Phil Oester70067292006-07-05 09:31:45 +000097#define TC_NUM_RULES iptc_num_rules
98#define TC_GET_RULE iptc_get_rule
Rusty Russell79dee072000-05-02 16:45:16 +000099
100#define TC_AF AF_INET
101#define TC_IPPROTO IPPROTO_IP
102
103#define SO_SET_REPLACE IPT_SO_SET_REPLACE
104#define SO_SET_ADD_COUNTERS IPT_SO_SET_ADD_COUNTERS
105#define SO_GET_INFO IPT_SO_GET_INFO
106#define SO_GET_ENTRIES IPT_SO_GET_ENTRIES
107#define SO_GET_VERSION IPT_SO_GET_VERSION
108
109#define STANDARD_TARGET IPT_STANDARD_TARGET
110#define LABEL_RETURN IPTC_LABEL_RETURN
111#define LABEL_ACCEPT IPTC_LABEL_ACCEPT
112#define LABEL_DROP IPTC_LABEL_DROP
Rusty Russell3eee0102000-05-10 00:24:01 +0000113#define LABEL_QUEUE IPTC_LABEL_QUEUE
Rusty Russell79dee072000-05-02 16:45:16 +0000114
115#define ALIGN IPT_ALIGN
116#define RETURN IPT_RETURN
117
118#include "libiptc.c"
119
120#define IP_PARTS_NATIVE(n) \
121(unsigned int)((n)>>24)&0xFF, \
122(unsigned int)((n)>>16)&0xFF, \
123(unsigned int)((n)>>8)&0xFF, \
124(unsigned int)((n)&0xFF)
125
126#define IP_PARTS(n) IP_PARTS_NATIVE(ntohl(n))
127
128int
129dump_entry(STRUCT_ENTRY *e, const TC_HANDLE_T handle)
130{
131 size_t i;
132 STRUCT_ENTRY_TARGET *t;
133
Harald Welteaae69be2004-08-29 23:32:14 +0000134 printf("Entry %u (%lu):\n", iptcb_entry2index(handle, e),
135 iptcb_entry2offset(handle, e));
Rusty Russell79dee072000-05-02 16:45:16 +0000136 printf("SRC IP: %u.%u.%u.%u/%u.%u.%u.%u\n",
137 IP_PARTS(e->ip.src.s_addr),IP_PARTS(e->ip.smsk.s_addr));
138 printf("DST IP: %u.%u.%u.%u/%u.%u.%u.%u\n",
139 IP_PARTS(e->ip.dst.s_addr),IP_PARTS(e->ip.dmsk.s_addr));
140 printf("Interface: `%s'/", e->ip.iniface);
141 for (i = 0; i < IFNAMSIZ; i++)
142 printf("%c", e->ip.iniface_mask[i] ? 'X' : '.');
143 printf("to `%s'/", e->ip.outiface);
144 for (i = 0; i < IFNAMSIZ; i++)
145 printf("%c", e->ip.outiface_mask[i] ? 'X' : '.');
146 printf("\nProtocol: %u\n", e->ip.proto);
147 printf("Flags: %02X\n", e->ip.flags);
148 printf("Invflags: %02X\n", e->ip.invflags);
149 printf("Counters: %llu packets, %llu bytes\n",
Martin Josefssona28d4952004-05-26 16:04:48 +0000150 (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
Rusty Russell79dee072000-05-02 16:45:16 +0000151 printf("Cache: %08X ", e->nfcache);
152 if (e->nfcache & NFC_ALTERED) printf("ALTERED ");
153 if (e->nfcache & NFC_UNKNOWN) printf("UNKNOWN ");
Rusty Russell79dee072000-05-02 16:45:16 +0000154 printf("\n");
155
156 IPT_MATCH_ITERATE(e, print_match);
157
158 t = GET_TARGET(e);
159 printf("Target name: `%s' [%u]\n", t->u.user.name, t->u.target_size);
160 if (strcmp(t->u.user.name, STANDARD_TARGET) == 0) {
161 int pos = *(int *)t->data;
162 if (pos < 0)
163 printf("verdict=%s\n",
164 pos == -NF_ACCEPT-1 ? "NF_ACCEPT"
165 : pos == -NF_DROP-1 ? "NF_DROP"
166 : pos == -NF_QUEUE-1 ? "NF_QUEUE"
167 : pos == RETURN ? "RETURN"
168 : "UNKNOWN");
169 else
170 printf("verdict=%u\n", pos);
171 } else if (strcmp(t->u.user.name, IPT_ERROR_TARGET) == 0)
172 printf("error=`%s'\n", t->data);
173
174 printf("\n");
175 return 0;
176}
177
Rusty Russell733e54b2004-12-16 14:22:23 +0000178static unsigned char *
Rusty Russell79dee072000-05-02 16:45:16 +0000179is_same(const STRUCT_ENTRY *a, const STRUCT_ENTRY *b, unsigned char *matchmask)
180{
181 unsigned int i;
Rusty Russell79dee072000-05-02 16:45:16 +0000182 unsigned char *mptr;
183
184 /* Always compare head structures: ignore mask here. */
185 if (a->ip.src.s_addr != b->ip.src.s_addr
186 || a->ip.dst.s_addr != b->ip.dst.s_addr
187 || a->ip.smsk.s_addr != b->ip.smsk.s_addr
Marc Boucher4f8d2d92002-06-12 19:22:29 +0000188 || a->ip.dmsk.s_addr != b->ip.dmsk.s_addr
Rusty Russell79dee072000-05-02 16:45:16 +0000189 || a->ip.proto != b->ip.proto
190 || a->ip.flags != b->ip.flags
191 || a->ip.invflags != b->ip.invflags)
Rusty Russell733e54b2004-12-16 14:22:23 +0000192 return NULL;
Rusty Russell79dee072000-05-02 16:45:16 +0000193
194 for (i = 0; i < IFNAMSIZ; i++) {
195 if (a->ip.iniface_mask[i] != b->ip.iniface_mask[i])
Rusty Russell733e54b2004-12-16 14:22:23 +0000196 return NULL;
Rusty Russell79dee072000-05-02 16:45:16 +0000197 if ((a->ip.iniface[i] & a->ip.iniface_mask[i])
198 != (b->ip.iniface[i] & b->ip.iniface_mask[i]))
Rusty Russell733e54b2004-12-16 14:22:23 +0000199 return NULL;
Rusty Russell79dee072000-05-02 16:45:16 +0000200 if (a->ip.outiface_mask[i] != b->ip.outiface_mask[i])
Rusty Russell733e54b2004-12-16 14:22:23 +0000201 return NULL;
Rusty Russell79dee072000-05-02 16:45:16 +0000202 if ((a->ip.outiface[i] & a->ip.outiface_mask[i])
203 != (b->ip.outiface[i] & b->ip.outiface_mask[i]))
Rusty Russell733e54b2004-12-16 14:22:23 +0000204 return NULL;
Rusty Russell79dee072000-05-02 16:45:16 +0000205 }
206
207 if (a->nfcache != b->nfcache
208 || a->target_offset != b->target_offset
209 || a->next_offset != b->next_offset)
Rusty Russell733e54b2004-12-16 14:22:23 +0000210 return NULL;
Rusty Russell79dee072000-05-02 16:45:16 +0000211
212 mptr = matchmask + sizeof(STRUCT_ENTRY);
213 if (IPT_MATCH_ITERATE(a, match_different, a->elems, b->elems, &mptr))
Rusty Russell733e54b2004-12-16 14:22:23 +0000214 return NULL;
Pablo Neira5ee88622005-06-23 08:51:18 +0000215 mptr += IPT_ALIGN(sizeof(struct ipt_entry_target));
Rusty Russell79dee072000-05-02 16:45:16 +0000216
Rusty Russell733e54b2004-12-16 14:22:23 +0000217 return mptr;
Rusty Russell79dee072000-05-02 16:45:16 +0000218}
219
Harald Welteaae69be2004-08-29 23:32:14 +0000220#if 0
Rusty Russell79dee072000-05-02 16:45:16 +0000221/***************************** DEBUGGING ********************************/
222static inline int
223unconditional(const struct ipt_ip *ip)
224{
225 unsigned int i;
226
227 for (i = 0; i < sizeof(*ip)/sizeof(u_int32_t); i++)
228 if (((u_int32_t *)ip)[i])
229 return 0;
230
231 return 1;
232}
233
234static inline int
235check_match(const STRUCT_ENTRY_MATCH *m, unsigned int *off)
236{
237 assert(m->u.match_size >= sizeof(STRUCT_ENTRY_MATCH));
238 assert(ALIGN(m->u.match_size) == m->u.match_size);
239
240 (*off) += m->u.match_size;
241 return 0;
242}
243
244static inline int
245check_entry(const STRUCT_ENTRY *e, unsigned int *i, unsigned int *off,
246 unsigned int user_offset, int *was_return,
247 TC_HANDLE_T h)
248{
249 unsigned int toff;
250 STRUCT_STANDARD_TARGET *t;
251
252 assert(e->target_offset >= sizeof(STRUCT_ENTRY));
253 assert(e->next_offset >= e->target_offset
254 + sizeof(STRUCT_ENTRY_TARGET));
255 toff = sizeof(STRUCT_ENTRY);
256 IPT_MATCH_ITERATE(e, check_match, &toff);
257
258 assert(toff == e->target_offset);
259
260 t = (STRUCT_STANDARD_TARGET *)
261 GET_TARGET((STRUCT_ENTRY *)e);
262 /* next_offset will have to be multiple of entry alignment. */
263 assert(e->next_offset == ALIGN(e->next_offset));
264 assert(e->target_offset == ALIGN(e->target_offset));
265 assert(t->target.u.target_size == ALIGN(t->target.u.target_size));
266 assert(!TC_IS_CHAIN(t->target.u.user.name, h));
267
268 if (strcmp(t->target.u.user.name, STANDARD_TARGET) == 0) {
269 assert(t->target.u.target_size
270 == ALIGN(sizeof(STRUCT_STANDARD_TARGET)));
271
272 assert(t->verdict == -NF_DROP-1
273 || t->verdict == -NF_ACCEPT-1
274 || t->verdict == RETURN
Harald Welteaae69be2004-08-29 23:32:14 +0000275 || t->verdict < (int)h->entries->size);
Rusty Russell79dee072000-05-02 16:45:16 +0000276
277 if (t->verdict >= 0) {
278 STRUCT_ENTRY *te = get_entry(h, t->verdict);
279 int idx;
280
Harald Welteaae69be2004-08-29 23:32:14 +0000281 idx = iptcb_entry2index(h, te);
Rusty Russell79dee072000-05-02 16:45:16 +0000282 assert(strcmp(GET_TARGET(te)->u.user.name,
283 IPT_ERROR_TARGET)
284 != 0);
285 assert(te != e);
286
287 /* Prior node must be error node, or this node. */
Harald Welteaae69be2004-08-29 23:32:14 +0000288 assert(t->verdict == iptcb_entry2offset(h, e)+e->next_offset
Rusty Russell79dee072000-05-02 16:45:16 +0000289 || strcmp(GET_TARGET(index2entry(h, idx-1))
290 ->u.user.name, IPT_ERROR_TARGET)
291 == 0);
292 }
293
294 if (t->verdict == RETURN
295 && unconditional(&e->ip)
296 && e->target_offset == sizeof(*e))
297 *was_return = 1;
298 else
299 *was_return = 0;
300 } else if (strcmp(t->target.u.user.name, IPT_ERROR_TARGET) == 0) {
301 assert(t->target.u.target_size
302 == ALIGN(sizeof(struct ipt_error_target)));
303
304 /* If this is in user area, previous must have been return */
305 if (*off > user_offset)
306 assert(*was_return);
307
308 *was_return = 0;
309 }
310 else *was_return = 0;
311
312 if (*off == user_offset)
313 assert(strcmp(t->target.u.user.name, IPT_ERROR_TARGET) == 0);
314
315 (*off) += e->next_offset;
316 (*i)++;
317 return 0;
318}
319
Harald Welte380ba5f2002-02-13 16:19:55 +0000320#ifdef IPTC_DEBUG
Rusty Russell79dee072000-05-02 16:45:16 +0000321/* Do every conceivable sanity check on the handle */
322static void
323do_check(TC_HANDLE_T h, unsigned int line)
324{
325 unsigned int i, n;
326 unsigned int user_offset; /* Offset of first user chain */
327 int was_return;
328
329 assert(h->changed == 0 || h->changed == 1);
330 if (strcmp(h->info.name, "filter") == 0) {
331 assert(h->info.valid_hooks
332 == (1 << NF_IP_LOCAL_IN
333 | 1 << NF_IP_FORWARD
334 | 1 << NF_IP_LOCAL_OUT));
335
336 /* Hooks should be first three */
337 assert(h->info.hook_entry[NF_IP_LOCAL_IN] == 0);
338
339 n = get_chain_end(h, 0);
340 n += get_entry(h, n)->next_offset;
341 assert(h->info.hook_entry[NF_IP_FORWARD] == n);
342
343 n = get_chain_end(h, n);
344 n += get_entry(h, n)->next_offset;
345 assert(h->info.hook_entry[NF_IP_LOCAL_OUT] == n);
346
347 user_offset = h->info.hook_entry[NF_IP_LOCAL_OUT];
348 } else if (strcmp(h->info.name, "nat") == 0) {
Harald Welte95df8e72002-02-13 23:13:23 +0000349 assert((h->info.valid_hooks
350 == (1 << NF_IP_PRE_ROUTING
351 | 1 << NF_IP_POST_ROUTING
352 | 1 << NF_IP_LOCAL_OUT)) ||
353 (h->info.valid_hooks
354 == (1 << NF_IP_PRE_ROUTING
355 | 1 << NF_IP_LOCAL_IN
356 | 1 << NF_IP_POST_ROUTING
357 | 1 << NF_IP_LOCAL_OUT)));
Rusty Russell79dee072000-05-02 16:45:16 +0000358
359 assert(h->info.hook_entry[NF_IP_PRE_ROUTING] == 0);
360
361 n = get_chain_end(h, 0);
Harald Welte95df8e72002-02-13 23:13:23 +0000362
Rusty Russell79dee072000-05-02 16:45:16 +0000363 n += get_entry(h, n)->next_offset;
364 assert(h->info.hook_entry[NF_IP_POST_ROUTING] == n);
Rusty Russell79dee072000-05-02 16:45:16 +0000365 n = get_chain_end(h, n);
Harald Welte95df8e72002-02-13 23:13:23 +0000366
Rusty Russell79dee072000-05-02 16:45:16 +0000367 n += get_entry(h, n)->next_offset;
368 assert(h->info.hook_entry[NF_IP_LOCAL_OUT] == n);
Rusty Russell79dee072000-05-02 16:45:16 +0000369 user_offset = h->info.hook_entry[NF_IP_LOCAL_OUT];
Harald Welte95df8e72002-02-13 23:13:23 +0000370
371 if (h->info.valid_hooks & (1 << NF_IP_LOCAL_IN)) {
372 n = get_chain_end(h, n);
373 n += get_entry(h, n)->next_offset;
374 assert(h->info.hook_entry[NF_IP_LOCAL_IN] == n);
375 user_offset = h->info.hook_entry[NF_IP_LOCAL_IN];
376 }
377
Rusty Russell79dee072000-05-02 16:45:16 +0000378 } else if (strcmp(h->info.name, "mangle") == 0) {
Harald Welte596707c2002-02-13 16:35:39 +0000379 /* This code is getting ugly because linux < 2.4.18-pre6 had
380 * two mangle hooks, linux >= 2.4.18-pre6 has five mangle hooks
381 * */
Harald Welte95df8e72002-02-13 23:13:23 +0000382 assert((h->info.valid_hooks
383 == (1 << NF_IP_PRE_ROUTING
384 | 1 << NF_IP_LOCAL_OUT)) ||
385 (h->info.valid_hooks
386 == (1 << NF_IP_PRE_ROUTING
387 | 1 << NF_IP_LOCAL_IN
388 | 1 << NF_IP_FORWARD
389 | 1 << NF_IP_LOCAL_OUT
390 | 1 << NF_IP_POST_ROUTING)));
Rusty Russell79dee072000-05-02 16:45:16 +0000391
Harald Welte380ba5f2002-02-13 16:19:55 +0000392 /* Hooks should be first five */
Rusty Russell79dee072000-05-02 16:45:16 +0000393 assert(h->info.hook_entry[NF_IP_PRE_ROUTING] == 0);
394
395 n = get_chain_end(h, 0);
Harald Welte380ba5f2002-02-13 16:19:55 +0000396
Harald Weltea540b1b2002-02-13 22:42:52 +0000397 if (h->info.valid_hooks & (1 << NF_IP_LOCAL_IN)) {
Harald Welte596707c2002-02-13 16:35:39 +0000398 n += get_entry(h, n)->next_offset;
399 assert(h->info.hook_entry[NF_IP_LOCAL_IN] == n);
400 n = get_chain_end(h, n);
401 }
Harald Welte380ba5f2002-02-13 16:19:55 +0000402
Harald Weltea540b1b2002-02-13 22:42:52 +0000403 if (h->info.valid_hooks & (1 << NF_IP_FORWARD)) {
Harald Welte596707c2002-02-13 16:35:39 +0000404 n += get_entry(h, n)->next_offset;
405 assert(h->info.hook_entry[NF_IP_FORWARD] == n);
406 n = get_chain_end(h, n);
407 }
408
Harald Welte380ba5f2002-02-13 16:19:55 +0000409 n += get_entry(h, n)->next_offset;
Rusty Russell79dee072000-05-02 16:45:16 +0000410 assert(h->info.hook_entry[NF_IP_LOCAL_OUT] == n);
Harald Welte596707c2002-02-13 16:35:39 +0000411 user_offset = h->info.hook_entry[NF_IP_LOCAL_OUT];
Rusty Russell79dee072000-05-02 16:45:16 +0000412
Harald Weltea540b1b2002-02-13 22:42:52 +0000413 if (h->info.valid_hooks & (1 << NF_IP_POST_ROUTING)) {
Harald Welte596707c2002-02-13 16:35:39 +0000414 n = get_chain_end(h, n);
415 n += get_entry(h, n)->next_offset;
416 assert(h->info.hook_entry[NF_IP_POST_ROUTING] == n);
417 user_offset = h->info.hook_entry[NF_IP_POST_ROUTING];
418 }
Harald Welte4dc734c2003-10-07 18:55:13 +0000419 } else if (strcmp(h->info.name, "raw") == 0) {
420 assert(h->info.valid_hooks
421 == (1 << NF_IP_PRE_ROUTING
422 | 1 << NF_IP_LOCAL_OUT));
423
424 /* Hooks should be first three */
425 assert(h->info.hook_entry[NF_IP_PRE_ROUTING] == 0);
426
427 n = get_chain_end(h, n);
428 n += get_entry(h, n)->next_offset;
429 assert(h->info.hook_entry[NF_IP_LOCAL_OUT] == n);
430
431 user_offset = h->info.hook_entry[NF_IP_LOCAL_OUT];
Harald Welte380ba5f2002-02-13 16:19:55 +0000432
Rusty Russellf92ba9b2000-09-14 11:08:55 +0000433#ifdef NF_IP_DROPPING
434 } else if (strcmp(h->info.name, "drop") == 0) {
435 assert(h->info.valid_hooks == (1 << NF_IP_DROPPING));
436
437 /* Hook should be first */
438 assert(h->info.hook_entry[NF_IP_DROPPING] == 0);
439 user_offset = 0;
440#endif
441 } else {
Rusty Russelle9b48532000-09-14 11:09:40 +0000442 fprintf(stderr, "Unknown table `%s'\n", h->info.name);
Rusty Russell79dee072000-05-02 16:45:16 +0000443 abort();
Rusty Russellf92ba9b2000-09-14 11:08:55 +0000444 }
Rusty Russell79dee072000-05-02 16:45:16 +0000445
446 /* User chain == end of last builtin + policy entry */
447 user_offset = get_chain_end(h, user_offset);
448 user_offset += get_entry(h, user_offset)->next_offset;
449
450 /* Overflows should be end of entry chains, and unconditional
451 policy nodes. */
452 for (i = 0; i < NUMHOOKS; i++) {
453 STRUCT_ENTRY *e;
454 STRUCT_STANDARD_TARGET *t;
455
456 if (!(h->info.valid_hooks & (1 << i)))
457 continue;
458 assert(h->info.underflow[i]
459 == get_chain_end(h, h->info.hook_entry[i]));
460
461 e = get_entry(h, get_chain_end(h, h->info.hook_entry[i]));
462 assert(unconditional(&e->ip));
463 assert(e->target_offset == sizeof(*e));
Rusty Russell79dee072000-05-02 16:45:16 +0000464 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
Harald Weltea540b1b2002-02-13 22:42:52 +0000465 assert(t->target.u.target_size == ALIGN(sizeof(*t)));
466 assert(e->next_offset == sizeof(*e) + ALIGN(sizeof(*t)));
Rusty Russell79dee072000-05-02 16:45:16 +0000467
468 assert(strcmp(t->target.u.user.name, STANDARD_TARGET)==0);
469 assert(t->verdict == -NF_DROP-1 || t->verdict == -NF_ACCEPT-1);
470
471 /* Hooks and underflows must be valid entries */
472 entry2index(h, get_entry(h, h->info.hook_entry[i]));
473 entry2index(h, get_entry(h, h->info.underflow[i]));
474 }
475
476 assert(h->info.size
477 >= h->info.num_entries * (sizeof(STRUCT_ENTRY)
478 +sizeof(STRUCT_STANDARD_TARGET)));
479
480 assert(h->entries.size
481 >= (h->new_number
482 * (sizeof(STRUCT_ENTRY)
483 + sizeof(STRUCT_STANDARD_TARGET))));
484 assert(strcmp(h->info.name, h->entries.name) == 0);
485
486 i = 0; n = 0;
487 was_return = 0;
488 /* Check all the entries. */
Rusty Russell725d97a2000-07-07 08:54:22 +0000489 ENTRY_ITERATE(h->entries.entrytable, h->entries.size,
Rusty Russell79dee072000-05-02 16:45:16 +0000490 check_entry, &i, &n, user_offset, &was_return, h);
491
492 assert(i == h->new_number);
493 assert(n == h->entries.size);
494
495 /* Final entry must be error node */
496 assert(strcmp(GET_TARGET(index2entry(h, h->new_number-1))
497 ->u.user.name,
Harald Weltea540b1b2002-02-13 22:42:52 +0000498 ERROR_TARGET) == 0);
Rusty Russell79dee072000-05-02 16:45:16 +0000499}
Harald Welte380ba5f2002-02-13 16:19:55 +0000500#endif /*IPTC_DEBUG*/
Harald Welteaae69be2004-08-29 23:32:14 +0000501
502#endif