libip4tc.c revision 14da56743c6cdf25da35b7b5ca7a5d201771990d
19e49fb63d355446b91d20ff78ad78b297e89a50dcaryclark@google.com/* Library which manipulates firewall rules.  Version 0.1. */
29e49fb63d355446b91d20ff78ad78b297e89a50dcaryclark@google.com
39e49fb63d355446b91d20ff78ad78b297e89a50dcaryclark@google.com/* Architecture of firewall rules is as follows:
49e49fb63d355446b91d20ff78ad78b297e89a50dcaryclark@google.com *
59e49fb63d355446b91d20ff78ad78b297e89a50dcaryclark@google.com * Chains go INPUT, FORWARD, OUTPUT then user chains.
69e49fb63d355446b91d20ff78ad78b297e89a50dcaryclark@google.com * Each user chain starts with an ERROR node.
7c682590538a27d73489bc91c098e000fdfb07ccfcaryclark@google.com * Every chain ends with an unconditional jump: a RETURN for user chains,
827accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com * and a POLICY for built-ins.
927accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com */
1027accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com
1127accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com/* (C)1999 Paul ``Rusty'' Russell - Placed under the GNU GPL (See
1227accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com   COPYING for details). */
1327accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com
1427accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#include <assert.h>
1527accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#include <string.h>
1627accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#include <errno.h>
1727accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#include <stdlib.h>
1827accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#include <stdio.h>
1927accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#include <unistd.h>
2027accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com
2127accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#ifdef DEBUG_CONNTRACK
2227accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#define inline
2327accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#endif
2427accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com
2527accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#if !defined(__GLIBC__) || (__GLIBC__ < 2)
2627accef223a27fba437f5e825d99edbae20a045bcaryclark@google.comtypedef unsigned int socklen_t;
2727accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#endif
2827accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com
2927accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#include "libiptc/libiptc.h"
3027accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com
3127accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#define IP_VERSION	4
3227accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#define IP_OFFSET	0x1FFF
3327accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com
3427accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#define HOOK_PRE_ROUTING	NF_IP_PRE_ROUTING
3527accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#define HOOK_LOCAL_IN		NF_IP_LOCAL_IN
36aa35831d1d0e4c798a63fe772430adc4f3a038cdcaryclark@google.com#define HOOK_FORWARD		NF_IP_FORWARD
37aa35831d1d0e4c798a63fe772430adc4f3a038cdcaryclark@google.com#define HOOK_LOCAL_OUT		NF_IP_LOCAL_OUT
38aa35831d1d0e4c798a63fe772430adc4f3a038cdcaryclark@google.com#define HOOK_POST_ROUTING	NF_IP_POST_ROUTING
39aa35831d1d0e4c798a63fe772430adc4f3a038cdcaryclark@google.com
40aa35831d1d0e4c798a63fe772430adc4f3a038cdcaryclark@google.com#define STRUCT_ENTRY_TARGET	struct xt_entry_target
41aa35831d1d0e4c798a63fe772430adc4f3a038cdcaryclark@google.com#define STRUCT_ENTRY		struct ipt_entry
42aa35831d1d0e4c798a63fe772430adc4f3a038cdcaryclark@google.com#define STRUCT_ENTRY_MATCH	struct xt_entry_match
43aa35831d1d0e4c798a63fe772430adc4f3a038cdcaryclark@google.com#define STRUCT_GETINFO		struct ipt_getinfo
44aa35831d1d0e4c798a63fe772430adc4f3a038cdcaryclark@google.com#define STRUCT_GET_ENTRIES	struct ipt_get_entries
45aa35831d1d0e4c798a63fe772430adc4f3a038cdcaryclark@google.com#define STRUCT_COUNTERS		struct xt_counters
46d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com#define STRUCT_COUNTERS_INFO	struct xt_counters_info
4727accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#define STRUCT_STANDARD_TARGET	struct xt_standard_target
4827accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#define STRUCT_REPLACE		struct ipt_replace
4947d73daa7a971e7eee5822def7922f7d43b2dc47caryclark@google.com
5047d73daa7a971e7eee5822def7922f7d43b2dc47caryclark@google.com#define ENTRY_ITERATE		IPT_ENTRY_ITERATE
5127accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#define TABLE_MAXNAMELEN	XT_TABLE_MAXNAMELEN
52aa35831d1d0e4c798a63fe772430adc4f3a038cdcaryclark@google.com#define FUNCTION_MAXNAMELEN	XT_FUNCTION_MAXNAMELEN
5327accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com
5427accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#define GET_TARGET		ipt_get_target
5527accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com
5627accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#define ERROR_TARGET		XT_ERROR_TARGET
5747d73daa7a971e7eee5822def7922f7d43b2dc47caryclark@google.com#define NUMHOOKS		NF_IP_NUMHOOKS
5847d73daa7a971e7eee5822def7922f7d43b2dc47caryclark@google.com
5927accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#define IPT_CHAINLABEL		xt_chainlabel
60aa35831d1d0e4c798a63fe772430adc4f3a038cdcaryclark@google.com
6127accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#define TC_DUMP_ENTRIES		dump_entries
6227accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#define TC_IS_CHAIN		iptc_is_chain
6327accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#define TC_FIRST_CHAIN		iptc_first_chain
6427accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#define TC_NEXT_CHAIN		iptc_next_chain
6547d73daa7a971e7eee5822def7922f7d43b2dc47caryclark@google.com#define TC_FIRST_RULE		iptc_first_rule
6647d73daa7a971e7eee5822def7922f7d43b2dc47caryclark@google.com#define TC_NEXT_RULE		iptc_next_rule
6727accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#define TC_GET_TARGET		iptc_get_target
68aa35831d1d0e4c798a63fe772430adc4f3a038cdcaryclark@google.com#define TC_BUILTIN		iptc_builtin
6927accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#define TC_GET_POLICY		iptc_get_policy
7027accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#define TC_INSERT_ENTRY		iptc_insert_entry
7127accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#define TC_REPLACE_ENTRY	iptc_replace_entry
7227accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#define TC_APPEND_ENTRY		iptc_append_entry
7347d73daa7a971e7eee5822def7922f7d43b2dc47caryclark@google.com#define TC_CHECK_ENTRY		iptc_check_entry
7447d73daa7a971e7eee5822def7922f7d43b2dc47caryclark@google.com#define TC_DELETE_ENTRY		iptc_delete_entry
7527accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#define TC_DELETE_NUM_ENTRY	iptc_delete_num_entry
76aa35831d1d0e4c798a63fe772430adc4f3a038cdcaryclark@google.com#define TC_FLUSH_ENTRIES	iptc_flush_entries
7727accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#define TC_ZERO_ENTRIES		iptc_zero_entries
7827accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#define TC_READ_COUNTER		iptc_read_counter
7927accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#define TC_ZERO_COUNTER		iptc_zero_counter
8027accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#define TC_SET_COUNTER		iptc_set_counter
8147d73daa7a971e7eee5822def7922f7d43b2dc47caryclark@google.com#define TC_CREATE_CHAIN		iptc_create_chain
8247d73daa7a971e7eee5822def7922f7d43b2dc47caryclark@google.com#define TC_GET_REFERENCES	iptc_get_references
8327accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#define TC_DELETE_CHAIN		iptc_delete_chain
84aa35831d1d0e4c798a63fe772430adc4f3a038cdcaryclark@google.com#define TC_RENAME_CHAIN		iptc_rename_chain
8527accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#define TC_SET_POLICY		iptc_set_policy
8627accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#define TC_GET_RAW_SOCKET	iptc_get_raw_socket
8727accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#define TC_INIT			iptc_init
8827accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#define TC_FREE			iptc_free
8947d73daa7a971e7eee5822def7922f7d43b2dc47caryclark@google.com#define TC_COMMIT		iptc_commit
9047d73daa7a971e7eee5822def7922f7d43b2dc47caryclark@google.com#define TC_STRERROR		iptc_strerror
9127accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#define TC_NUM_RULES		iptc_num_rules
92aa35831d1d0e4c798a63fe772430adc4f3a038cdcaryclark@google.com#define TC_GET_RULE		iptc_get_rule
9327accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com
9427accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#define TC_AF			AF_INET
9527accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#define TC_IPPROTO		IPPROTO_IP
9627accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com
9747d73daa7a971e7eee5822def7922f7d43b2dc47caryclark@google.com#define SO_SET_REPLACE		IPT_SO_SET_REPLACE
9847d73daa7a971e7eee5822def7922f7d43b2dc47caryclark@google.com#define SO_SET_ADD_COUNTERS	IPT_SO_SET_ADD_COUNTERS
9927accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#define SO_GET_INFO		IPT_SO_GET_INFO
100aa35831d1d0e4c798a63fe772430adc4f3a038cdcaryclark@google.com#define SO_GET_ENTRIES		IPT_SO_GET_ENTRIES
10127accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#define SO_GET_VERSION		IPT_SO_GET_VERSION
10227accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com
10327accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#define STANDARD_TARGET		XT_STANDARD_TARGET
10427accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#define LABEL_RETURN		IPTC_LABEL_RETURN
10527accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#define LABEL_ACCEPT		IPTC_LABEL_ACCEPT
10627accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#define LABEL_DROP		IPTC_LABEL_DROP
10747d73daa7a971e7eee5822def7922f7d43b2dc47caryclark@google.com#define LABEL_QUEUE		IPTC_LABEL_QUEUE
10847d73daa7a971e7eee5822def7922f7d43b2dc47caryclark@google.com
10927accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#define ALIGN			XT_ALIGN
110aa35831d1d0e4c798a63fe772430adc4f3a038cdcaryclark@google.com#define RETURN			XT_RETURN
11127accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com
11227accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#include "libiptc.c"
11327accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com
11427accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com#define IP_PARTS_NATIVE(n)			\
11527accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com(unsigned int)((n)>>24)&0xFF,			\
11627accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com(unsigned int)((n)>>16)&0xFF,			\
11747d73daa7a971e7eee5822def7922f7d43b2dc47caryclark@google.com(unsigned int)((n)>>8)&0xFF,			\
11847d73daa7a971e7eee5822def7922f7d43b2dc47caryclark@google.com(unsigned int)((n)&0xFF)
11927accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com
120aa35831d1d0e4c798a63fe772430adc4f3a038cdcaryclark@google.com#define IP_PARTS(n) IP_PARTS_NATIVE(ntohl(n))
12127accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com
12227accef223a27fba437f5e825d99edbae20a045bcaryclark@google.comstatic int
123d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.comdump_entry(struct ipt_entry *e, struct xtc_handle *const handle)
12427accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com{
12527accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com	size_t i;
12627accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com	STRUCT_ENTRY_TARGET *t;
127d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
12847d73daa7a971e7eee5822def7922f7d43b2dc47caryclark@google.com	printf("Entry %u (%lu):\n", iptcb_entry2index(handle, e),
12947d73daa7a971e7eee5822def7922f7d43b2dc47caryclark@google.com	       iptcb_entry2offset(handle, e));
13027accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com	printf("SRC IP: %u.%u.%u.%u/%u.%u.%u.%u\n",
131aa35831d1d0e4c798a63fe772430adc4f3a038cdcaryclark@google.com	       IP_PARTS(e->ip.src.s_addr),IP_PARTS(e->ip.smsk.s_addr));
13227accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com	printf("DST IP: %u.%u.%u.%u/%u.%u.%u.%u\n",
13327accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com	       IP_PARTS(e->ip.dst.s_addr),IP_PARTS(e->ip.dmsk.s_addr));
1349f3e9a5a17a7546e1c5bfde1eb7e6e9c11870333caryclark@google.com	printf("Interface: `%s'/", e->ip.iniface);
1359f3e9a5a17a7546e1c5bfde1eb7e6e9c11870333caryclark@google.com	for (i = 0; i < IFNAMSIZ; i++)
1369f3e9a5a17a7546e1c5bfde1eb7e6e9c11870333caryclark@google.com		printf("%c", e->ip.iniface_mask[i] ? 'X' : '.');
1379f3e9a5a17a7546e1c5bfde1eb7e6e9c11870333caryclark@google.com	printf("to `%s'/", e->ip.outiface);
138aa35831d1d0e4c798a63fe772430adc4f3a038cdcaryclark@google.com	for (i = 0; i < IFNAMSIZ; i++)
13927accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com		printf("%c", e->ip.outiface_mask[i] ? 'X' : '.');
14027accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com	printf("\nProtocol: %u\n", e->ip.proto);
14127accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com	printf("Flags: %02X\n", e->ip.flags);
14227accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com	printf("Invflags: %02X\n", e->ip.invflags);
14327accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com	printf("Counters: %llu packets, %llu bytes\n",
14427accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com	       (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
1456d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com	printf("Cache: %08X\n", e->nfcache);
1466d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com
1476d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com	IPT_MATCH_ITERATE(e, print_match);
1486d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com
149aa35831d1d0e4c798a63fe772430adc4f3a038cdcaryclark@google.com	t = GET_TARGET(e);
15027accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com	printf("Target name: `%s' [%u]\n", t->u.user.name, t->u.target_size);
151d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com	if (strcmp(t->u.user.name, STANDARD_TARGET) == 0) {
15227accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com		const unsigned char *data = t->data;
15327accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com		int pos = *(const int *)data;
15427accef223a27fba437f5e825d99edbae20a045bcaryclark@google.com		if (pos < 0)
155			printf("verdict=%s\n",
156			       pos == -NF_ACCEPT-1 ? "NF_ACCEPT"
157			       : pos == -NF_DROP-1 ? "NF_DROP"
158			       : pos == -NF_QUEUE-1 ? "NF_QUEUE"
159			       : pos == RETURN ? "RETURN"
160			       : "UNKNOWN");
161		else
162			printf("verdict=%u\n", pos);
163	} else if (strcmp(t->u.user.name, XT_ERROR_TARGET) == 0)
164		printf("error=`%s'\n", t->data);
165
166	printf("\n");
167	return 0;
168}
169
170static unsigned char *
171is_same(const STRUCT_ENTRY *a, const STRUCT_ENTRY *b, unsigned char *matchmask)
172{
173	unsigned int i;
174	unsigned char *mptr;
175
176	/* Always compare head structures: ignore mask here. */
177	if (a->ip.src.s_addr != b->ip.src.s_addr
178	    || a->ip.dst.s_addr != b->ip.dst.s_addr
179	    || a->ip.smsk.s_addr != b->ip.smsk.s_addr
180	    || a->ip.dmsk.s_addr != b->ip.dmsk.s_addr
181	    || a->ip.proto != b->ip.proto
182	    || a->ip.flags != b->ip.flags
183	    || a->ip.invflags != b->ip.invflags)
184		return NULL;
185
186	for (i = 0; i < IFNAMSIZ; i++) {
187		if (a->ip.iniface_mask[i] != b->ip.iniface_mask[i])
188			return NULL;
189		if ((a->ip.iniface[i] & a->ip.iniface_mask[i])
190		    != (b->ip.iniface[i] & b->ip.iniface_mask[i]))
191			return NULL;
192		if (a->ip.outiface_mask[i] != b->ip.outiface_mask[i])
193			return NULL;
194		if ((a->ip.outiface[i] & a->ip.outiface_mask[i])
195		    != (b->ip.outiface[i] & b->ip.outiface_mask[i]))
196			return NULL;
197	}
198
199	if (a->target_offset != b->target_offset
200	    || a->next_offset != b->next_offset)
201		return NULL;
202
203	mptr = matchmask + sizeof(STRUCT_ENTRY);
204	if (IPT_MATCH_ITERATE(a, match_different, a->elems, b->elems, &mptr))
205		return NULL;
206	mptr += XT_ALIGN(sizeof(struct xt_entry_target));
207
208	return mptr;
209}
210
211#if 0
212/***************************** DEBUGGING ********************************/
213static inline int
214unconditional(const struct ipt_ip *ip)
215{
216	unsigned int i;
217
218	for (i = 0; i < sizeof(*ip)/sizeof(uint32_t); i++)
219		if (((uint32_t *)ip)[i])
220			return 0;
221
222	return 1;
223}
224
225static inline int
226check_match(const STRUCT_ENTRY_MATCH *m, unsigned int *off)
227{
228	assert(m->u.match_size >= sizeof(STRUCT_ENTRY_MATCH));
229	assert(ALIGN(m->u.match_size) == m->u.match_size);
230
231	(*off) += m->u.match_size;
232	return 0;
233}
234
235static inline int
236check_entry(const STRUCT_ENTRY *e, unsigned int *i, unsigned int *off,
237	    unsigned int user_offset, int *was_return,
238	    struct xtc_handle *h)
239{
240	unsigned int toff;
241	STRUCT_STANDARD_TARGET *t;
242
243	assert(e->target_offset >= sizeof(STRUCT_ENTRY));
244	assert(e->next_offset >= e->target_offset
245	       + sizeof(STRUCT_ENTRY_TARGET));
246	toff = sizeof(STRUCT_ENTRY);
247	IPT_MATCH_ITERATE(e, check_match, &toff);
248
249	assert(toff == e->target_offset);
250
251	t = (STRUCT_STANDARD_TARGET *)
252		GET_TARGET((STRUCT_ENTRY *)e);
253	/* next_offset will have to be multiple of entry alignment. */
254	assert(e->next_offset == ALIGN(e->next_offset));
255	assert(e->target_offset == ALIGN(e->target_offset));
256	assert(t->target.u.target_size == ALIGN(t->target.u.target_size));
257	assert(!TC_IS_CHAIN(t->target.u.user.name, h));
258
259	if (strcmp(t->target.u.user.name, STANDARD_TARGET) == 0) {
260		assert(t->target.u.target_size
261		       == ALIGN(sizeof(STRUCT_STANDARD_TARGET)));
262
263		assert(t->verdict == -NF_DROP-1
264		       || t->verdict == -NF_ACCEPT-1
265		       || t->verdict == RETURN
266		       || t->verdict < (int)h->entries->size);
267
268		if (t->verdict >= 0) {
269			STRUCT_ENTRY *te = get_entry(h, t->verdict);
270			int idx;
271
272			idx = iptcb_entry2index(h, te);
273			assert(strcmp(GET_TARGET(te)->u.user.name,
274				      XT_ERROR_TARGET)
275			       != 0);
276			assert(te != e);
277
278			/* Prior node must be error node, or this node. */
279			assert(t->verdict == iptcb_entry2offset(h, e)+e->next_offset
280			       || strcmp(GET_TARGET(index2entry(h, idx-1))
281					 ->u.user.name, XT_ERROR_TARGET)
282			       == 0);
283		}
284
285		if (t->verdict == RETURN
286		    && unconditional(&e->ip)
287		    && e->target_offset == sizeof(*e))
288			*was_return = 1;
289		else
290			*was_return = 0;
291	} else if (strcmp(t->target.u.user.name, XT_ERROR_TARGET) == 0) {
292		assert(t->target.u.target_size
293		       == ALIGN(sizeof(struct ipt_error_target)));
294
295		/* If this is in user area, previous must have been return */
296		if (*off > user_offset)
297			assert(*was_return);
298
299		*was_return = 0;
300	}
301	else *was_return = 0;
302
303	if (*off == user_offset)
304		assert(strcmp(t->target.u.user.name, XT_ERROR_TARGET) == 0);
305
306	(*off) += e->next_offset;
307	(*i)++;
308	return 0;
309}
310
311#ifdef IPTC_DEBUG
312/* Do every conceivable sanity check on the handle */
313static void
314do_check(struct xtc_handle *h, unsigned int line)
315{
316	unsigned int i, n;
317	unsigned int user_offset; /* Offset of first user chain */
318	int was_return;
319
320	assert(h->changed == 0 || h->changed == 1);
321	if (strcmp(h->info.name, "filter") == 0) {
322		assert(h->info.valid_hooks
323		       == (1 << NF_IP_LOCAL_IN
324			   | 1 << NF_IP_FORWARD
325			   | 1 << NF_IP_LOCAL_OUT));
326
327		/* Hooks should be first three */
328		assert(h->info.hook_entry[NF_IP_LOCAL_IN] == 0);
329
330		n = get_chain_end(h, 0);
331		n += get_entry(h, n)->next_offset;
332		assert(h->info.hook_entry[NF_IP_FORWARD] == n);
333
334		n = get_chain_end(h, n);
335		n += get_entry(h, n)->next_offset;
336		assert(h->info.hook_entry[NF_IP_LOCAL_OUT] == n);
337
338		user_offset = h->info.hook_entry[NF_IP_LOCAL_OUT];
339	} else if (strcmp(h->info.name, "nat") == 0) {
340		assert((h->info.valid_hooks
341		        == (1 << NF_IP_PRE_ROUTING
342			    | 1 << NF_IP_POST_ROUTING
343			    | 1 << NF_IP_LOCAL_OUT)) ||
344		       (h->info.valid_hooks
345			== (1 << NF_IP_PRE_ROUTING
346			    | 1 << NF_IP_LOCAL_IN
347			    | 1 << NF_IP_POST_ROUTING
348			    | 1 << NF_IP_LOCAL_OUT)));
349
350		assert(h->info.hook_entry[NF_IP_PRE_ROUTING] == 0);
351
352		n = get_chain_end(h, 0);
353
354		n += get_entry(h, n)->next_offset;
355		assert(h->info.hook_entry[NF_IP_POST_ROUTING] == n);
356		n = get_chain_end(h, n);
357
358		n += get_entry(h, n)->next_offset;
359		assert(h->info.hook_entry[NF_IP_LOCAL_OUT] == n);
360		user_offset = h->info.hook_entry[NF_IP_LOCAL_OUT];
361
362		if (h->info.valid_hooks & (1 << NF_IP_LOCAL_IN)) {
363			n = get_chain_end(h, n);
364			n += get_entry(h, n)->next_offset;
365			assert(h->info.hook_entry[NF_IP_LOCAL_IN] == n);
366			user_offset = h->info.hook_entry[NF_IP_LOCAL_IN];
367		}
368
369	} else if (strcmp(h->info.name, "mangle") == 0) {
370		/* This code is getting ugly because linux < 2.4.18-pre6 had
371		 * two mangle hooks, linux >= 2.4.18-pre6 has five mangle hooks
372		 * */
373		assert((h->info.valid_hooks
374			== (1 << NF_IP_PRE_ROUTING
375			    | 1 << NF_IP_LOCAL_OUT)) ||
376		       (h->info.valid_hooks
377			== (1 << NF_IP_PRE_ROUTING
378			    | 1 << NF_IP_LOCAL_IN
379			    | 1 << NF_IP_FORWARD
380			    | 1 << NF_IP_LOCAL_OUT
381			    | 1 << NF_IP_POST_ROUTING)));
382
383		/* Hooks should be first five */
384		assert(h->info.hook_entry[NF_IP_PRE_ROUTING] == 0);
385
386		n = get_chain_end(h, 0);
387
388		if (h->info.valid_hooks & (1 << NF_IP_LOCAL_IN)) {
389			n += get_entry(h, n)->next_offset;
390			assert(h->info.hook_entry[NF_IP_LOCAL_IN] == n);
391			n = get_chain_end(h, n);
392		}
393
394		if (h->info.valid_hooks & (1 << NF_IP_FORWARD)) {
395			n += get_entry(h, n)->next_offset;
396			assert(h->info.hook_entry[NF_IP_FORWARD] == n);
397			n = get_chain_end(h, n);
398		}
399
400		n += get_entry(h, n)->next_offset;
401		assert(h->info.hook_entry[NF_IP_LOCAL_OUT] == n);
402		user_offset = h->info.hook_entry[NF_IP_LOCAL_OUT];
403
404		if (h->info.valid_hooks & (1 << NF_IP_POST_ROUTING)) {
405			n = get_chain_end(h, n);
406			n += get_entry(h, n)->next_offset;
407			assert(h->info.hook_entry[NF_IP_POST_ROUTING] == n);
408			user_offset = h->info.hook_entry[NF_IP_POST_ROUTING];
409		}
410	} else if (strcmp(h->info.name, "raw") == 0) {
411		assert(h->info.valid_hooks
412		       == (1 << NF_IP_PRE_ROUTING
413			   | 1 << NF_IP_LOCAL_OUT));
414
415		/* Hooks should be first three */
416		assert(h->info.hook_entry[NF_IP_PRE_ROUTING] == 0);
417
418		n = get_chain_end(h, n);
419		n += get_entry(h, n)->next_offset;
420		assert(h->info.hook_entry[NF_IP_LOCAL_OUT] == n);
421
422		user_offset = h->info.hook_entry[NF_IP_LOCAL_OUT];
423	} else {
424		fprintf(stderr, "Unknown table `%s'\n", h->info.name);
425		abort();
426	}
427
428	/* User chain == end of last builtin + policy entry */
429	user_offset = get_chain_end(h, user_offset);
430	user_offset += get_entry(h, user_offset)->next_offset;
431
432	/* Overflows should be end of entry chains, and unconditional
433           policy nodes. */
434	for (i = 0; i < NUMHOOKS; i++) {
435		STRUCT_ENTRY *e;
436		STRUCT_STANDARD_TARGET *t;
437
438		if (!(h->info.valid_hooks & (1 << i)))
439			continue;
440		assert(h->info.underflow[i]
441		       == get_chain_end(h, h->info.hook_entry[i]));
442
443		e = get_entry(h, get_chain_end(h, h->info.hook_entry[i]));
444		assert(unconditional(&e->ip));
445		assert(e->target_offset == sizeof(*e));
446		t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
447		assert(t->target.u.target_size == ALIGN(sizeof(*t)));
448		assert(e->next_offset == sizeof(*e) + ALIGN(sizeof(*t)));
449
450		assert(strcmp(t->target.u.user.name, STANDARD_TARGET)==0);
451		assert(t->verdict == -NF_DROP-1 || t->verdict == -NF_ACCEPT-1);
452
453		/* Hooks and underflows must be valid entries */
454		entry2index(h, get_entry(h, h->info.hook_entry[i]));
455		entry2index(h, get_entry(h, h->info.underflow[i]));
456	}
457
458	assert(h->info.size
459	       >= h->info.num_entries * (sizeof(STRUCT_ENTRY)
460					 +sizeof(STRUCT_STANDARD_TARGET)));
461
462	assert(h->entries.size
463	       >= (h->new_number
464		   * (sizeof(STRUCT_ENTRY)
465		      + sizeof(STRUCT_STANDARD_TARGET))));
466	assert(strcmp(h->info.name, h->entries.name) == 0);
467
468	i = 0; n = 0;
469	was_return = 0;
470	/* Check all the entries. */
471	ENTRY_ITERATE(h->entries.entrytable, h->entries.size,
472		      check_entry, &i, &n, user_offset, &was_return, h);
473
474	assert(i == h->new_number);
475	assert(n == h->entries.size);
476
477	/* Final entry must be error node */
478	assert(strcmp(GET_TARGET(index2entry(h, h->new_number-1))
479		      ->u.user.name,
480		      ERROR_TARGET) == 0);
481}
482#endif /*IPTC_DEBUG*/
483
484#endif
485