blob: 4e47e69838e8d6af064584c8890560c1efbc0cb0 [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#include <arpa/inet.h>
21
22#ifdef DEBUG_CONNTRACK
23#define inline
24#endif
25
JP Abgrall16bd81b2011-05-18 23:22:23 -070026#if !defined(__ANDROID__) && (!defined(__GLIBC__) || (__GLIBC__ < 2))
Rusty Russell79dee072000-05-02 16:45:16 +000027typedef unsigned int socklen_t;
28#endif
29
30#include "libiptc/libip6tc.h"
31
32#define HOOK_PRE_ROUTING NF_IP6_PRE_ROUTING
33#define HOOK_LOCAL_IN NF_IP6_LOCAL_IN
34#define HOOK_FORWARD NF_IP6_FORWARD
35#define HOOK_LOCAL_OUT NF_IP6_LOCAL_OUT
36#define HOOK_POST_ROUTING NF_IP6_POST_ROUTING
37
Jan Engelhardt14da5672011-08-27 09:56:16 +020038#define STRUCT_ENTRY_TARGET struct xt_entry_target
Rusty Russell79dee072000-05-02 16:45:16 +000039#define STRUCT_ENTRY struct ip6t_entry
Jan Engelhardt14da5672011-08-27 09:56:16 +020040#define STRUCT_ENTRY_MATCH struct xt_entry_match
Rusty Russell79dee072000-05-02 16:45:16 +000041#define STRUCT_GETINFO struct ip6t_getinfo
42#define STRUCT_GET_ENTRIES struct ip6t_get_entries
Jan Engelhardt14da5672011-08-27 09:56:16 +020043#define STRUCT_COUNTERS struct xt_counters
44#define STRUCT_COUNTERS_INFO struct xt_counters_info
45#define STRUCT_STANDARD_TARGET struct xt_standard_target
Rusty Russell79dee072000-05-02 16:45:16 +000046#define STRUCT_REPLACE struct ip6t_replace
47
Rusty Russell79dee072000-05-02 16:45:16 +000048#define ENTRY_ITERATE IP6T_ENTRY_ITERATE
Jan Engelhardt14da5672011-08-27 09:56:16 +020049#define TABLE_MAXNAMELEN XT_TABLE_MAXNAMELEN
50#define FUNCTION_MAXNAMELEN XT_FUNCTION_MAXNAMELEN
Rusty Russell79dee072000-05-02 16:45:16 +000051
52#define GET_TARGET ip6t_get_target
53
Jan Engelhardt14da5672011-08-27 09:56:16 +020054#define ERROR_TARGET XT_ERROR_TARGET
Rusty Russell79dee072000-05-02 16:45:16 +000055#define NUMHOOKS NF_IP6_NUMHOOKS
56
Jan Engelhardt7e5e8662011-08-27 11:16:16 +020057#define IPT_CHAINLABEL xt_chainlabel
Rusty Russell79dee072000-05-02 16:45:16 +000058
59#define TC_DUMP_ENTRIES dump_entries6
60#define TC_IS_CHAIN ip6tc_is_chain
Philip Blundell8c700902000-05-15 02:17:52 +000061#define TC_FIRST_CHAIN ip6tc_first_chain
Rusty Russell79dee072000-05-02 16:45:16 +000062#define TC_NEXT_CHAIN ip6tc_next_chain
Philip Blundell8c700902000-05-15 02:17:52 +000063#define TC_FIRST_RULE ip6tc_first_rule
64#define TC_NEXT_RULE ip6tc_next_rule
Rusty Russell79dee072000-05-02 16:45:16 +000065#define TC_GET_TARGET ip6tc_get_target
66#define TC_BUILTIN ip6tc_builtin
67#define TC_GET_POLICY ip6tc_get_policy
68#define TC_INSERT_ENTRY ip6tc_insert_entry
69#define TC_REPLACE_ENTRY ip6tc_replace_entry
70#define TC_APPEND_ENTRY ip6tc_append_entry
Stefan Tomanekd59b9db2011-03-08 22:42:51 +010071#define TC_CHECK_ENTRY ip6tc_check_entry
Rusty Russell79dee072000-05-02 16:45:16 +000072#define TC_DELETE_ENTRY ip6tc_delete_entry
73#define TC_DELETE_NUM_ENTRY ip6tc_delete_num_entry
Rusty Russell79dee072000-05-02 16:45:16 +000074#define TC_FLUSH_ENTRIES ip6tc_flush_entries
75#define TC_ZERO_ENTRIES ip6tc_zero_entries
Harald Welte1cef74d2001-01-05 15:22:59 +000076#define TC_ZERO_COUNTER ip6tc_zero_counter
77#define TC_READ_COUNTER ip6tc_read_counter
78#define TC_SET_COUNTER ip6tc_set_counter
Rusty Russell79dee072000-05-02 16:45:16 +000079#define TC_CREATE_CHAIN ip6tc_create_chain
80#define TC_GET_REFERENCES ip6tc_get_references
81#define TC_DELETE_CHAIN ip6tc_delete_chain
82#define TC_RENAME_CHAIN ip6tc_rename_chain
83#define TC_SET_POLICY ip6tc_set_policy
84#define TC_GET_RAW_SOCKET ip6tc_get_raw_socket
85#define TC_INIT ip6tc_init
Martin Josefsson841e4ae2003-05-02 15:30:11 +000086#define TC_FREE ip6tc_free
Rusty Russell79dee072000-05-02 16:45:16 +000087#define TC_COMMIT ip6tc_commit
88#define TC_STRERROR ip6tc_strerror
Phil Oester70067292006-07-05 09:31:45 +000089#define TC_NUM_RULES ip6tc_num_rules
90#define TC_GET_RULE ip6tc_get_rule
Jan Engelhardtde4d2d32011-08-27 12:50:32 +020091#define TC_OPS ip6tc_ops
Rusty Russell79dee072000-05-02 16:45:16 +000092
93#define TC_AF AF_INET6
94#define TC_IPPROTO IPPROTO_IPV6
95
96#define SO_SET_REPLACE IP6T_SO_SET_REPLACE
97#define SO_SET_ADD_COUNTERS IP6T_SO_SET_ADD_COUNTERS
98#define SO_GET_INFO IP6T_SO_GET_INFO
99#define SO_GET_ENTRIES IP6T_SO_GET_ENTRIES
100#define SO_GET_VERSION IP6T_SO_GET_VERSION
101
Jan Engelhardt14da5672011-08-27 09:56:16 +0200102#define STANDARD_TARGET XT_STANDARD_TARGET
Rusty Russell79dee072000-05-02 16:45:16 +0000103#define LABEL_RETURN IP6TC_LABEL_RETURN
104#define LABEL_ACCEPT IP6TC_LABEL_ACCEPT
105#define LABEL_DROP IP6TC_LABEL_DROP
Philip Blundell88eb8352000-05-10 00:25:04 +0000106#define LABEL_QUEUE IP6TC_LABEL_QUEUE
Rusty Russell79dee072000-05-02 16:45:16 +0000107
Jan Engelhardtdcd1ad82011-05-09 19:32:05 +0200108#define ALIGN XT_ALIGN
Jan Engelhardt14da5672011-08-27 09:56:16 +0200109#define RETURN XT_RETURN
Rusty Russell79dee072000-05-02 16:45:16 +0000110
111#include "libiptc.c"
112
113#define BIT6(a, l) \
Yasuyuki Kozakai5a2208c2008-06-04 15:16:03 +0200114 ((ntohl(a->s6_addr32[(l) / 32]) >> (31 - ((l) & 31))) & 1)
Rusty Russell79dee072000-05-02 16:45:16 +0000115
Pablo Neira Ayusocccfff92013-03-16 12:11:07 +0100116static int
Rusty Russell79dee072000-05-02 16:45:16 +0000117ipv6_prefix_length(const struct in6_addr *a)
118{
119 int l, i;
120 for (l = 0; l < 128; l++) {
121 if (BIT6(a, l) == 0)
122 break;
123 }
124 for (i = l + 1; i < 128; i++) {
125 if (BIT6(a, i) == 1)
126 return -1;
127 }
128 return l;
129}
130
131static int
Jan Engelhardt1639fe82011-08-27 11:39:52 +0200132dump_entry(struct ip6t_entry *e, struct xtc_handle *const handle)
Rusty Russell79dee072000-05-02 16:45:16 +0000133{
134 size_t i;
135 char buf[40];
136 int len;
Jan Engelhardt14da5672011-08-27 09:56:16 +0200137 struct xt_entry_target *t;
Rusty Russell79dee072000-05-02 16:45:16 +0000138
Harald Welteaae69be2004-08-29 23:32:14 +0000139 printf("Entry %u (%lu):\n", iptcb_entry2index(handle, e),
140 iptcb_entry2offset(handle, e));
Rusty Russell79dee072000-05-02 16:45:16 +0000141 puts("SRC IP: ");
142 inet_ntop(AF_INET6, &e->ipv6.src, buf, sizeof buf);
143 puts(buf);
144 putchar('/');
145 len = ipv6_prefix_length(&e->ipv6.smsk);
146 if (len != -1)
147 printf("%d", len);
148 else {
149 inet_ntop(AF_INET6, &e->ipv6.smsk, buf, sizeof buf);
150 puts(buf);
151 }
152 putchar('\n');
153
154 puts("DST IP: ");
155 inet_ntop(AF_INET6, &e->ipv6.dst, buf, sizeof buf);
156 puts(buf);
157 putchar('/');
158 len = ipv6_prefix_length(&e->ipv6.dmsk);
159 if (len != -1)
160 printf("%d", len);
161 else {
162 inet_ntop(AF_INET6, &e->ipv6.dmsk, buf, sizeof buf);
163 puts(buf);
164 }
165 putchar('\n');
166
167 printf("Interface: `%s'/", e->ipv6.iniface);
168 for (i = 0; i < IFNAMSIZ; i++)
169 printf("%c", e->ipv6.iniface_mask[i] ? 'X' : '.');
170 printf("to `%s'/", e->ipv6.outiface);
171 for (i = 0; i < IFNAMSIZ; i++)
172 printf("%c", e->ipv6.outiface_mask[i] ? 'X' : '.');
173 printf("\nProtocol: %u\n", e->ipv6.proto);
174 if (e->ipv6.flags & IP6T_F_TOS)
175 printf("TOS: %u\n", e->ipv6.tos);
176 printf("Flags: %02X\n", e->ipv6.flags);
177 printf("Invflags: %02X\n", e->ipv6.invflags);
178 printf("Counters: %llu packets, %llu bytes\n",
Martin Josefssona28d4952004-05-26 16:04:48 +0000179 (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
Peter Rileyea146a92007-09-02 13:09:07 +0000180 printf("Cache: %08X\n", e->nfcache);
Rusty Russell79dee072000-05-02 16:45:16 +0000181
182 IP6T_MATCH_ITERATE(e, print_match);
183
184 t = ip6t_get_target(e);
Philip Blundell88eb8352000-05-10 00:25:04 +0000185 printf("Target name: `%s' [%u]\n", t->u.user.name, t->u.target_size);
Jan Engelhardt14da5672011-08-27 09:56:16 +0200186 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0) {
Jan Engelhardt51651b62009-10-23 23:35:49 +0200187 const unsigned char *data = t->data;
188 int pos = *(const int *)data;
Rusty Russell79dee072000-05-02 16:45:16 +0000189 if (pos < 0)
190 printf("verdict=%s\n",
191 pos == -NF_ACCEPT-1 ? "NF_ACCEPT"
192 : pos == -NF_DROP-1 ? "NF_DROP"
Jan Engelhardt14da5672011-08-27 09:56:16 +0200193 : pos == XT_RETURN ? "RETURN"
Rusty Russell79dee072000-05-02 16:45:16 +0000194 : "UNKNOWN");
195 else
196 printf("verdict=%u\n", pos);
Jan Engelhardt14da5672011-08-27 09:56:16 +0200197 } else if (strcmp(t->u.user.name, XT_ERROR_TARGET) == 0)
Rusty Russell79dee072000-05-02 16:45:16 +0000198 printf("error=`%s'\n", t->data);
199
200 printf("\n");
201 return 0;
202}
203
Rusty Russell733e54b2004-12-16 14:22:23 +0000204static unsigned char *
Philip Blundell88eb8352000-05-10 00:25:04 +0000205is_same(const STRUCT_ENTRY *a, const STRUCT_ENTRY *b,
Rusty Russell79dee072000-05-02 16:45:16 +0000206 unsigned char *matchmask)
207{
208 unsigned int i;
Rusty Russell79dee072000-05-02 16:45:16 +0000209 unsigned char *mptr;
210
211 /* Always compare head structures: ignore mask here. */
212 if (memcmp(&a->ipv6.src, &b->ipv6.src, sizeof(struct in6_addr))
213 || memcmp(&a->ipv6.dst, &b->ipv6.dst, sizeof(struct in6_addr))
214 || memcmp(&a->ipv6.smsk, &b->ipv6.smsk, sizeof(struct in6_addr))
215 || memcmp(&a->ipv6.dmsk, &b->ipv6.dmsk, sizeof(struct in6_addr))
216 || a->ipv6.proto != b->ipv6.proto
217 || a->ipv6.tos != b->ipv6.tos
218 || a->ipv6.flags != b->ipv6.flags
219 || a->ipv6.invflags != b->ipv6.invflags)
Rusty Russell733e54b2004-12-16 14:22:23 +0000220 return NULL;
Rusty Russell79dee072000-05-02 16:45:16 +0000221
222 for (i = 0; i < IFNAMSIZ; i++) {
223 if (a->ipv6.iniface_mask[i] != b->ipv6.iniface_mask[i])
Rusty Russell733e54b2004-12-16 14:22:23 +0000224 return NULL;
Rusty Russell79dee072000-05-02 16:45:16 +0000225 if ((a->ipv6.iniface[i] & a->ipv6.iniface_mask[i])
226 != (b->ipv6.iniface[i] & b->ipv6.iniface_mask[i]))
Rusty Russell733e54b2004-12-16 14:22:23 +0000227 return NULL;
Rusty Russell79dee072000-05-02 16:45:16 +0000228 if (a->ipv6.outiface_mask[i] != b->ipv6.outiface_mask[i])
Rusty Russell733e54b2004-12-16 14:22:23 +0000229 return NULL;
Rusty Russell79dee072000-05-02 16:45:16 +0000230 if ((a->ipv6.outiface[i] & a->ipv6.outiface_mask[i])
231 != (b->ipv6.outiface[i] & b->ipv6.outiface_mask[i]))
Rusty Russell733e54b2004-12-16 14:22:23 +0000232 return NULL;
Rusty Russell79dee072000-05-02 16:45:16 +0000233 }
234
Peter Rileyea146a92007-09-02 13:09:07 +0000235 if (a->target_offset != b->target_offset
Rusty Russell79dee072000-05-02 16:45:16 +0000236 || a->next_offset != b->next_offset)
Rusty Russell733e54b2004-12-16 14:22:23 +0000237 return NULL;
Rusty Russell79dee072000-05-02 16:45:16 +0000238
Philip Blundell88eb8352000-05-10 00:25:04 +0000239 mptr = matchmask + sizeof(STRUCT_ENTRY);
240 if (IP6T_MATCH_ITERATE(a, match_different, a->elems, b->elems, &mptr))
Rusty Russell733e54b2004-12-16 14:22:23 +0000241 return NULL;
Jan Engelhardt14da5672011-08-27 09:56:16 +0200242 mptr += XT_ALIGN(sizeof(struct xt_entry_target));
Rusty Russell79dee072000-05-02 16:45:16 +0000243
Rusty Russell733e54b2004-12-16 14:22:23 +0000244 return mptr;
Rusty Russell79dee072000-05-02 16:45:16 +0000245}
Philip Blundell8c700902000-05-15 02:17:52 +0000246
Rusty Russell5eed48a2000-06-02 20:12:24 +0000247/* All zeroes == unconditional rule. */
248static inline int
249unconditional(const struct ip6t_ip6 *ipv6)
250{
251 unsigned int i;
252
253 for (i = 0; i < sizeof(*ipv6); i++)
254 if (((char *)ipv6)[i])
255 break;
256
257 return (i == sizeof(*ipv6));
258}
259
Harald Welte380ba5f2002-02-13 16:19:55 +0000260#ifdef IPTC_DEBUG
Philip Blundell8c700902000-05-15 02:17:52 +0000261/* Do every conceivable sanity check on the handle */
262static void
Jan Engelhardtfd187312008-11-10 16:59:27 +0100263do_check(struct xtc_handle *h, unsigned int line)
Philip Blundell8c700902000-05-15 02:17:52 +0000264{
265 unsigned int i, n;
266 unsigned int user_offset; /* Offset of first user chain */
267 int was_return;
268
269 assert(h->changed == 0 || h->changed == 1);
270 if (strcmp(h->info.name, "filter") == 0) {
271 assert(h->info.valid_hooks
272 == (1 << NF_IP6_LOCAL_IN
273 | 1 << NF_IP6_FORWARD
274 | 1 << NF_IP6_LOCAL_OUT));
275
276 /* Hooks should be first three */
277 assert(h->info.hook_entry[NF_IP6_LOCAL_IN] == 0);
278
279 n = get_chain_end(h, 0);
280 n += get_entry(h, n)->next_offset;
281 assert(h->info.hook_entry[NF_IP6_FORWARD] == n);
282
283 n = get_chain_end(h, n);
284 n += get_entry(h, n)->next_offset;
285 assert(h->info.hook_entry[NF_IP6_LOCAL_OUT] == n);
286
287 user_offset = h->info.hook_entry[NF_IP6_LOCAL_OUT];
288 } else if (strcmp(h->info.name, "nat") == 0) {
Harald Welte95df8e72002-02-13 23:13:23 +0000289 assert((h->info.valid_hooks
290 == (1 << NF_IP6_PRE_ROUTING
291 | 1 << NF_IP6_LOCAL_OUT
292 | 1 << NF_IP6_POST_ROUTING)) ||
293 (h->info.valid_hooks
294 == (1 << NF_IP6_PRE_ROUTING
295 | 1 << NF_IP6_LOCAL_IN
296 | 1 << NF_IP6_LOCAL_OUT
297 | 1 << NF_IP6_POST_ROUTING)));
Philip Blundell8c700902000-05-15 02:17:52 +0000298
299 assert(h->info.hook_entry[NF_IP6_PRE_ROUTING] == 0);
300
301 n = get_chain_end(h, 0);
Harald Welte95df8e72002-02-13 23:13:23 +0000302
Philip Blundell8c700902000-05-15 02:17:52 +0000303 n += get_entry(h, n)->next_offset;
304 assert(h->info.hook_entry[NF_IP6_POST_ROUTING] == n);
Philip Blundell8c700902000-05-15 02:17:52 +0000305 n = get_chain_end(h, n);
Harald Welte95df8e72002-02-13 23:13:23 +0000306
Philip Blundell8c700902000-05-15 02:17:52 +0000307 n += get_entry(h, n)->next_offset;
308 assert(h->info.hook_entry[NF_IP6_LOCAL_OUT] == n);
Philip Blundell8c700902000-05-15 02:17:52 +0000309 user_offset = h->info.hook_entry[NF_IP6_LOCAL_OUT];
Harald Welte95df8e72002-02-13 23:13:23 +0000310
311 if (h->info.valid_hooks & (1 << NF_IP6_LOCAL_IN)) {
312 n = get_chain_end(h, n);
313 n += get_entry(h, n)->next_offset;
314 assert(h->info.hook_entry[NF_IP6_LOCAL_IN] == n);
315 user_offset = h->info.hook_entry[NF_IP6_LOCAL_IN];
316 }
317
Philip Blundell8c700902000-05-15 02:17:52 +0000318 } else if (strcmp(h->info.name, "mangle") == 0) {
Harald Welte596707c2002-02-13 16:35:39 +0000319 /* This code is getting ugly because linux < 2.4.18-pre6 had
320 * two mangle hooks, linux >= 2.4.18-pre6 has five mangle hooks
321 * */
Harald Welte95df8e72002-02-13 23:13:23 +0000322 assert((h->info.valid_hooks
323 == (1 << NF_IP6_PRE_ROUTING
324 | 1 << NF_IP6_LOCAL_OUT)) ||
325 (h->info.valid_hooks
326 == (1 << NF_IP6_PRE_ROUTING
327 | 1 << NF_IP6_LOCAL_IN
328 | 1 << NF_IP6_FORWARD
329 | 1 << NF_IP6_LOCAL_OUT
330 | 1 << NF_IP6_POST_ROUTING)));
Philip Blundell8c700902000-05-15 02:17:52 +0000331
Harald Welte380ba5f2002-02-13 16:19:55 +0000332 /* Hooks should be first five */
Philip Blundell8c700902000-05-15 02:17:52 +0000333 assert(h->info.hook_entry[NF_IP6_PRE_ROUTING] == 0);
334
335 n = get_chain_end(h, 0);
Harald Welte380ba5f2002-02-13 16:19:55 +0000336
Harald Weltea540b1b2002-02-13 22:42:52 +0000337 if (h->info.valid_hooks & (1 << NF_IP6_LOCAL_IN)) {
Harald Welte596707c2002-02-13 16:35:39 +0000338 n += get_entry(h, n)->next_offset;
339 assert(h->info.hook_entry[NF_IP6_LOCAL_IN] == n);
340 n = get_chain_end(h, n);
341 }
Harald Welte380ba5f2002-02-13 16:19:55 +0000342
Harald Weltea540b1b2002-02-13 22:42:52 +0000343 if (h->info.valid_hooks & (1 << NF_IP6_FORWARD)) {
Harald Welte596707c2002-02-13 16:35:39 +0000344 n += get_entry(h, n)->next_offset;
345 assert(h->info.hook_entry[NF_IP6_FORWARD] == n);
346 n = get_chain_end(h, n);
347 }
348
Harald Welte380ba5f2002-02-13 16:19:55 +0000349 n += get_entry(h, n)->next_offset;
Philip Blundell8c700902000-05-15 02:17:52 +0000350 assert(h->info.hook_entry[NF_IP6_LOCAL_OUT] == n);
Harald Welte596707c2002-02-13 16:35:39 +0000351 user_offset = h->info.hook_entry[NF_IP6_LOCAL_OUT];
Philip Blundell8c700902000-05-15 02:17:52 +0000352
Harald Weltea540b1b2002-02-13 22:42:52 +0000353 if (h->info.valid_hooks & (1 << NF_IP6_POST_ROUTING)) {
Harald Welte596707c2002-02-13 16:35:39 +0000354 n = get_chain_end(h, n);
355 n += get_entry(h, n)->next_offset;
356 assert(h->info.hook_entry[NF_IP6_POST_ROUTING] == n);
357 user_offset = h->info.hook_entry[NF_IP6_POST_ROUTING];
358 }
Harald Welte50fceae2003-10-07 22:12:31 +0000359 } else if (strcmp(h->info.name, "raw") == 0) {
360 assert(h->info.valid_hooks
361 == (1 << NF_IP6_PRE_ROUTING
362 | 1 << NF_IP6_LOCAL_OUT));
363
364 /* Hooks should be first three */
365 assert(h->info.hook_entry[NF_IP6_PRE_ROUTING] == 0);
366
367 n = get_chain_end(h, n);
368 n += get_entry(h, n)->next_offset;
369 assert(h->info.hook_entry[NF_IP6_LOCAL_OUT] == n);
370
371 user_offset = h->info.hook_entry[NF_IP6_LOCAL_OUT];
Harald Weltea540b1b2002-02-13 22:42:52 +0000372 } else {
373 fprintf(stderr, "Unknown table `%s'\n", h->info.name);
Philip Blundell8c700902000-05-15 02:17:52 +0000374 abort();
Harald Weltea540b1b2002-02-13 22:42:52 +0000375 }
Philip Blundell8c700902000-05-15 02:17:52 +0000376
377 /* User chain == end of last builtin + policy entry */
378 user_offset = get_chain_end(h, user_offset);
379 user_offset += get_entry(h, user_offset)->next_offset;
380
381 /* Overflows should be end of entry chains, and unconditional
382 policy nodes. */
383 for (i = 0; i < NUMHOOKS; i++) {
384 STRUCT_ENTRY *e;
385 STRUCT_STANDARD_TARGET *t;
386
387 if (!(h->info.valid_hooks & (1 << i)))
388 continue;
389 assert(h->info.underflow[i]
390 == get_chain_end(h, h->info.hook_entry[i]));
391
392 e = get_entry(h, get_chain_end(h, h->info.hook_entry[i]));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000393 assert(unconditional(&e->ipv6));
Philip Blundell8c700902000-05-15 02:17:52 +0000394 assert(e->target_offset == sizeof(*e));
395 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
Harald Weltea540b1b2002-02-13 22:42:52 +0000396 printf("target_size=%u, align=%u\n",
397 t->target.u.target_size, ALIGN(sizeof(*t)));
Philip Blundell8c700902000-05-15 02:17:52 +0000398 assert(t->target.u.target_size == ALIGN(sizeof(*t)));
399 assert(e->next_offset == sizeof(*e) + ALIGN(sizeof(*t)));
400
401 assert(strcmp(t->target.u.user.name, STANDARD_TARGET)==0);
402 assert(t->verdict == -NF_DROP-1 || t->verdict == -NF_ACCEPT-1);
403
404 /* Hooks and underflows must be valid entries */
Harald Welteaae69be2004-08-29 23:32:14 +0000405 iptcb_entry2index(h, get_entry(h, h->info.hook_entry[i]));
406 iptcb_entry2index(h, get_entry(h, h->info.underflow[i]));
Philip Blundell8c700902000-05-15 02:17:52 +0000407 }
408
409 assert(h->info.size
410 >= h->info.num_entries * (sizeof(STRUCT_ENTRY)
411 +sizeof(STRUCT_STANDARD_TARGET)));
412
413 assert(h->entries.size
414 >= (h->new_number
415 * (sizeof(STRUCT_ENTRY)
416 + sizeof(STRUCT_STANDARD_TARGET))));
417 assert(strcmp(h->info.name, h->entries.name) == 0);
418
419 i = 0; n = 0;
420 was_return = 0;
421
Rusty Russell5eed48a2000-06-02 20:12:24 +0000422#if 0
Philip Blundell8c700902000-05-15 02:17:52 +0000423 /* Check all the entries. */
Harald Weltea540b1b2002-02-13 22:42:52 +0000424 ENTRY_ITERATE(h->entries.entrytable, h->entries.size,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000425 check_entry, &i, &n, user_offset, &was_return, h);
Philip Blundell8c700902000-05-15 02:17:52 +0000426
427 assert(i == h->new_number);
428 assert(n == h->entries.size);
429
430 /* Final entry must be error node */
431 assert(strcmp(GET_TARGET(index2entry(h, h->new_number-1))
432 ->u.user.name,
433 ERROR_TARGET) == 0);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000434#endif
Philip Blundell8c700902000-05-15 02:17:52 +0000435}
Harald Welte380ba5f2002-02-13 16:19:55 +0000436#endif /*IPTC_DEBUG*/