• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* 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>
19 #include <unistd.h>
20 #include <netinet/in.h>
21 
22 #ifdef DEBUG_CONNTRACK
23 #define inline
24 #endif
25 
26 #include "libiptc/libiptc.h"
27 
28 #define IP_VERSION	4
29 #define IP_OFFSET	0x1FFF
30 
31 #define HOOK_PRE_ROUTING	NF_IP_PRE_ROUTING
32 #define HOOK_LOCAL_IN		NF_IP_LOCAL_IN
33 #define HOOK_FORWARD		NF_IP_FORWARD
34 #define HOOK_LOCAL_OUT		NF_IP_LOCAL_OUT
35 #define HOOK_POST_ROUTING	NF_IP_POST_ROUTING
36 #ifdef NF_IP_DROPPING
37 #define HOOK_DROPPING		NF_IP_DROPPING
38 #endif
39 
40 #define STRUCT_ENTRY_TARGET	struct ipt_entry_target
41 #define STRUCT_ENTRY		struct ipt_entry
42 #define STRUCT_ENTRY_MATCH	struct ipt_entry_match
43 #define STRUCT_GETINFO		struct ipt_getinfo
44 #define STRUCT_GET_ENTRIES	struct ipt_get_entries
45 #define STRUCT_COUNTERS		struct ipt_counters
46 #define STRUCT_COUNTERS_INFO	struct ipt_counters_info
47 #define STRUCT_STANDARD_TARGET	struct ipt_standard_target
48 #define STRUCT_REPLACE		struct ipt_replace
49 
50 #define STRUCT_TC_HANDLE	struct iptc_handle
51 #define TC_HANDLE_T		iptc_handle_t
52 
53 #define ENTRY_ITERATE		IPT_ENTRY_ITERATE
54 #define TABLE_MAXNAMELEN	IPT_TABLE_MAXNAMELEN
55 #define FUNCTION_MAXNAMELEN	IPT_FUNCTION_MAXNAMELEN
56 
57 #define GET_TARGET		ipt_get_target
58 
59 #define ERROR_TARGET		IPT_ERROR_TARGET
60 #define NUMHOOKS		NF_IP_NUMHOOKS
61 
62 #define IPT_CHAINLABEL		ipt_chainlabel
63 
64 #define TC_DUMP_ENTRIES		dump_entries
65 #define TC_IS_CHAIN		iptc_is_chain
66 #define TC_FIRST_CHAIN		iptc_first_chain
67 #define TC_NEXT_CHAIN		iptc_next_chain
68 #define TC_FIRST_RULE		iptc_first_rule
69 #define TC_NEXT_RULE		iptc_next_rule
70 #define TC_GET_TARGET		iptc_get_target
71 #define TC_BUILTIN		iptc_builtin
72 #define TC_GET_POLICY		iptc_get_policy
73 #define TC_INSERT_ENTRY		iptc_insert_entry
74 #define TC_REPLACE_ENTRY	iptc_replace_entry
75 #define TC_APPEND_ENTRY		iptc_append_entry
76 #define TC_DELETE_ENTRY		iptc_delete_entry
77 #define TC_DELETE_NUM_ENTRY	iptc_delete_num_entry
78 #define TC_CHECK_PACKET		iptc_check_packet
79 #define TC_FLUSH_ENTRIES	iptc_flush_entries
80 #define TC_ZERO_ENTRIES		iptc_zero_entries
81 #define TC_READ_COUNTER		iptc_read_counter
82 #define TC_ZERO_COUNTER		iptc_zero_counter
83 #define TC_SET_COUNTER		iptc_set_counter
84 #define TC_CREATE_CHAIN		iptc_create_chain
85 #define TC_GET_REFERENCES	iptc_get_references
86 #define TC_DELETE_CHAIN		iptc_delete_chain
87 #define TC_RENAME_CHAIN		iptc_rename_chain
88 #define TC_SET_POLICY		iptc_set_policy
89 #define TC_GET_RAW_SOCKET	iptc_get_raw_socket
90 #define TC_INIT			iptc_init
91 #define TC_FREE			iptc_free
92 #define TC_COMMIT		iptc_commit
93 #define TC_STRERROR		iptc_strerror
94 #define TC_NUM_RULES		iptc_num_rules
95 #define TC_GET_RULE		iptc_get_rule
96 
97 #define TC_AF			AF_INET
98 #define TC_IPPROTO		IPPROTO_IP
99 
100 #define SO_SET_REPLACE		IPT_SO_SET_REPLACE
101 #define SO_SET_ADD_COUNTERS	IPT_SO_SET_ADD_COUNTERS
102 #define SO_GET_INFO		IPT_SO_GET_INFO
103 #define SO_GET_ENTRIES		IPT_SO_GET_ENTRIES
104 #define SO_GET_VERSION		IPT_SO_GET_VERSION
105 
106 #define STANDARD_TARGET		IPT_STANDARD_TARGET
107 #define LABEL_RETURN		IPTC_LABEL_RETURN
108 #define LABEL_ACCEPT		IPTC_LABEL_ACCEPT
109 #define LABEL_DROP		IPTC_LABEL_DROP
110 #define LABEL_QUEUE		IPTC_LABEL_QUEUE
111 
112 #define ALIGN			IPT_ALIGN
113 #define RETURN			IPT_RETURN
114 
115 #include "libiptc.c"
116 
117 #define IP_PARTS_NATIVE(n)			\
118 (unsigned int)((n)>>24)&0xFF,			\
119 (unsigned int)((n)>>16)&0xFF,			\
120 (unsigned int)((n)>>8)&0xFF,			\
121 (unsigned int)((n)&0xFF)
122 
123 #define IP_PARTS(n) IP_PARTS_NATIVE(ntohl(n))
124 
125 int
dump_entry(STRUCT_ENTRY * e,const TC_HANDLE_T handle)126 dump_entry(STRUCT_ENTRY *e, const TC_HANDLE_T handle)
127 {
128 	size_t i;
129 	STRUCT_ENTRY_TARGET *t;
130 
131 	printf("Entry %u (%lu):\n", iptcb_entry2index(handle, e),
132 	       iptcb_entry2offset(handle, e));
133 	printf("SRC IP: %u.%u.%u.%u/%u.%u.%u.%u\n",
134 	       IP_PARTS(e->ip.src.s_addr),IP_PARTS(e->ip.smsk.s_addr));
135 	printf("DST IP: %u.%u.%u.%u/%u.%u.%u.%u\n",
136 	       IP_PARTS(e->ip.dst.s_addr),IP_PARTS(e->ip.dmsk.s_addr));
137 	printf("Interface: `%s'/", e->ip.iniface);
138 	for (i = 0; i < IFNAMSIZ; i++)
139 		printf("%c", e->ip.iniface_mask[i] ? 'X' : '.');
140 	printf("to `%s'/", e->ip.outiface);
141 	for (i = 0; i < IFNAMSIZ; i++)
142 		printf("%c", e->ip.outiface_mask[i] ? 'X' : '.');
143 	printf("\nProtocol: %u\n", e->ip.proto);
144 	printf("Flags: %02X\n", e->ip.flags);
145 	printf("Invflags: %02X\n", e->ip.invflags);
146 	printf("Counters: %llu packets, %llu bytes\n",
147 	       (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
148 	printf("Cache: %08X ", e->nfcache);
149 	if (e->nfcache & NFC_ALTERED) printf("ALTERED ");
150 	if (e->nfcache & NFC_UNKNOWN) printf("UNKNOWN ");
151 	printf("\n");
152 
153 	IPT_MATCH_ITERATE(e, print_match);
154 
155 	t = GET_TARGET(e);
156 	printf("Target name: `%s' [%u]\n", t->u.user.name, t->u.target_size);
157 	if (strcmp(t->u.user.name, STANDARD_TARGET) == 0) {
158 		int pos = *(int *)t->data;
159 		if (pos < 0)
160 			printf("verdict=%s\n",
161 			       pos == -NF_ACCEPT-1 ? "NF_ACCEPT"
162 			       : pos == -NF_DROP-1 ? "NF_DROP"
163 			       : pos == -NF_QUEUE-1 ? "NF_QUEUE"
164 			       : pos == RETURN ? "RETURN"
165 			       : "UNKNOWN");
166 		else
167 			printf("verdict=%u\n", pos);
168 	} else if (strcmp(t->u.user.name, IPT_ERROR_TARGET) == 0)
169 		printf("error=`%s'\n", t->data);
170 
171 	printf("\n");
172 	return 0;
173 }
174 
175 static unsigned char *
is_same(const STRUCT_ENTRY * a,const STRUCT_ENTRY * b,unsigned char * matchmask)176 is_same(const STRUCT_ENTRY *a, const STRUCT_ENTRY *b, unsigned char *matchmask)
177 {
178 	unsigned int i;
179 	unsigned char *mptr;
180 
181 	/* Always compare head structures: ignore mask here. */
182 	if (a->ip.src.s_addr != b->ip.src.s_addr
183 	    || a->ip.dst.s_addr != b->ip.dst.s_addr
184 	    || a->ip.smsk.s_addr != b->ip.smsk.s_addr
185 	    || a->ip.dmsk.s_addr != b->ip.dmsk.s_addr
186 	    || a->ip.proto != b->ip.proto
187 	    || a->ip.flags != b->ip.flags
188 	    || a->ip.invflags != b->ip.invflags)
189 		return NULL;
190 
191 	for (i = 0; i < IFNAMSIZ; i++) {
192 		if (a->ip.iniface_mask[i] != b->ip.iniface_mask[i])
193 			return NULL;
194 		if ((a->ip.iniface[i] & a->ip.iniface_mask[i])
195 		    != (b->ip.iniface[i] & b->ip.iniface_mask[i]))
196 			return NULL;
197 		if (a->ip.outiface_mask[i] != b->ip.outiface_mask[i])
198 			return NULL;
199 		if ((a->ip.outiface[i] & a->ip.outiface_mask[i])
200 		    != (b->ip.outiface[i] & b->ip.outiface_mask[i]))
201 			return NULL;
202 	}
203 
204 	if (a->nfcache != b->nfcache
205 	    || a->target_offset != b->target_offset
206 	    || a->next_offset != b->next_offset)
207 		return NULL;
208 
209 	mptr = matchmask + sizeof(STRUCT_ENTRY);
210 	if (IPT_MATCH_ITERATE(a, match_different, a->elems, b->elems, &mptr))
211 		return NULL;
212 	mptr += IPT_ALIGN(sizeof(struct ipt_entry_target));
213 
214 	return mptr;
215 }
216 
217 #if 0
218 /***************************** DEBUGGING ********************************/
219 static inline int
220 unconditional(const struct ipt_ip *ip)
221 {
222 	unsigned int i;
223 
224 	for (i = 0; i < sizeof(*ip)/sizeof(u_int32_t); i++)
225 		if (((u_int32_t *)ip)[i])
226 			return 0;
227 
228 	return 1;
229 }
230 
231 static inline int
232 check_match(const STRUCT_ENTRY_MATCH *m, unsigned int *off)
233 {
234 	assert(m->u.match_size >= sizeof(STRUCT_ENTRY_MATCH));
235 	assert(ALIGN(m->u.match_size) == m->u.match_size);
236 
237 	(*off) += m->u.match_size;
238 	return 0;
239 }
240 
241 static inline int
242 check_entry(const STRUCT_ENTRY *e, unsigned int *i, unsigned int *off,
243 	    unsigned int user_offset, int *was_return,
244 	    TC_HANDLE_T h)
245 {
246 	unsigned int toff;
247 	STRUCT_STANDARD_TARGET *t;
248 
249 	assert(e->target_offset >= sizeof(STRUCT_ENTRY));
250 	assert(e->next_offset >= e->target_offset
251 	       + sizeof(STRUCT_ENTRY_TARGET));
252 	toff = sizeof(STRUCT_ENTRY);
253 	IPT_MATCH_ITERATE(e, check_match, &toff);
254 
255 	assert(toff == e->target_offset);
256 
257 	t = (STRUCT_STANDARD_TARGET *)
258 		GET_TARGET((STRUCT_ENTRY *)e);
259 	/* next_offset will have to be multiple of entry alignment. */
260 	assert(e->next_offset == ALIGN(e->next_offset));
261 	assert(e->target_offset == ALIGN(e->target_offset));
262 	assert(t->target.u.target_size == ALIGN(t->target.u.target_size));
263 	assert(!TC_IS_CHAIN(t->target.u.user.name, h));
264 
265 	if (strcmp(t->target.u.user.name, STANDARD_TARGET) == 0) {
266 		assert(t->target.u.target_size
267 		       == ALIGN(sizeof(STRUCT_STANDARD_TARGET)));
268 
269 		assert(t->verdict == -NF_DROP-1
270 		       || t->verdict == -NF_ACCEPT-1
271 		       || t->verdict == RETURN
272 		       || t->verdict < (int)h->entries->size);
273 
274 		if (t->verdict >= 0) {
275 			STRUCT_ENTRY *te = get_entry(h, t->verdict);
276 			int idx;
277 
278 			idx = iptcb_entry2index(h, te);
279 			assert(strcmp(GET_TARGET(te)->u.user.name,
280 				      IPT_ERROR_TARGET)
281 			       != 0);
282 			assert(te != e);
283 
284 			/* Prior node must be error node, or this node. */
285 			assert(t->verdict == iptcb_entry2offset(h, e)+e->next_offset
286 			       || strcmp(GET_TARGET(index2entry(h, idx-1))
287 					 ->u.user.name, IPT_ERROR_TARGET)
288 			       == 0);
289 		}
290 
291 		if (t->verdict == RETURN
292 		    && unconditional(&e->ip)
293 		    && e->target_offset == sizeof(*e))
294 			*was_return = 1;
295 		else
296 			*was_return = 0;
297 	} else if (strcmp(t->target.u.user.name, IPT_ERROR_TARGET) == 0) {
298 		assert(t->target.u.target_size
299 		       == ALIGN(sizeof(struct ipt_error_target)));
300 
301 		/* If this is in user area, previous must have been return */
302 		if (*off > user_offset)
303 			assert(*was_return);
304 
305 		*was_return = 0;
306 	}
307 	else *was_return = 0;
308 
309 	if (*off == user_offset)
310 		assert(strcmp(t->target.u.user.name, IPT_ERROR_TARGET) == 0);
311 
312 	(*off) += e->next_offset;
313 	(*i)++;
314 	return 0;
315 }
316 
317 #ifdef IPTC_DEBUG
318 /* Do every conceivable sanity check on the handle */
319 static void
320 do_check(TC_HANDLE_T h, unsigned int line)
321 {
322 	unsigned int i, n;
323 	unsigned int user_offset; /* Offset of first user chain */
324 	int was_return;
325 
326 	assert(h->changed == 0 || h->changed == 1);
327 	if (strcmp(h->info.name, "filter") == 0) {
328 		assert(h->info.valid_hooks
329 		       == (1 << NF_IP_LOCAL_IN
330 			   | 1 << NF_IP_FORWARD
331 			   | 1 << NF_IP_LOCAL_OUT));
332 
333 		/* Hooks should be first three */
334 		assert(h->info.hook_entry[NF_IP_LOCAL_IN] == 0);
335 
336 		n = get_chain_end(h, 0);
337 		n += get_entry(h, n)->next_offset;
338 		assert(h->info.hook_entry[NF_IP_FORWARD] == n);
339 
340 		n = get_chain_end(h, n);
341 		n += get_entry(h, n)->next_offset;
342 		assert(h->info.hook_entry[NF_IP_LOCAL_OUT] == n);
343 
344 		user_offset = h->info.hook_entry[NF_IP_LOCAL_OUT];
345 	} else if (strcmp(h->info.name, "nat") == 0) {
346 		assert((h->info.valid_hooks
347 		        == (1 << NF_IP_PRE_ROUTING
348 			    | 1 << NF_IP_POST_ROUTING
349 			    | 1 << NF_IP_LOCAL_OUT)) ||
350 		       (h->info.valid_hooks
351 			== (1 << NF_IP_PRE_ROUTING
352 			    | 1 << NF_IP_LOCAL_IN
353 			    | 1 << NF_IP_POST_ROUTING
354 			    | 1 << NF_IP_LOCAL_OUT)));
355 
356 		assert(h->info.hook_entry[NF_IP_PRE_ROUTING] == 0);
357 
358 		n = get_chain_end(h, 0);
359 
360 		n += get_entry(h, n)->next_offset;
361 		assert(h->info.hook_entry[NF_IP_POST_ROUTING] == n);
362 		n = get_chain_end(h, n);
363 
364 		n += get_entry(h, n)->next_offset;
365 		assert(h->info.hook_entry[NF_IP_LOCAL_OUT] == n);
366 		user_offset = h->info.hook_entry[NF_IP_LOCAL_OUT];
367 
368 		if (h->info.valid_hooks & (1 << NF_IP_LOCAL_IN)) {
369 			n = get_chain_end(h, n);
370 			n += get_entry(h, n)->next_offset;
371 			assert(h->info.hook_entry[NF_IP_LOCAL_IN] == n);
372 			user_offset = h->info.hook_entry[NF_IP_LOCAL_IN];
373 		}
374 
375 	} else if (strcmp(h->info.name, "mangle") == 0) {
376 		/* This code is getting ugly because linux < 2.4.18-pre6 had
377 		 * two mangle hooks, linux >= 2.4.18-pre6 has five mangle hooks
378 		 * */
379 		assert((h->info.valid_hooks
380 			== (1 << NF_IP_PRE_ROUTING
381 			    | 1 << NF_IP_LOCAL_OUT)) ||
382 		       (h->info.valid_hooks
383 			== (1 << NF_IP_PRE_ROUTING
384 			    | 1 << NF_IP_LOCAL_IN
385 			    | 1 << NF_IP_FORWARD
386 			    | 1 << NF_IP_LOCAL_OUT
387 			    | 1 << NF_IP_POST_ROUTING)));
388 
389 		/* Hooks should be first five */
390 		assert(h->info.hook_entry[NF_IP_PRE_ROUTING] == 0);
391 
392 		n = get_chain_end(h, 0);
393 
394 		if (h->info.valid_hooks & (1 << NF_IP_LOCAL_IN)) {
395 			n += get_entry(h, n)->next_offset;
396 			assert(h->info.hook_entry[NF_IP_LOCAL_IN] == n);
397 			n = get_chain_end(h, n);
398 		}
399 
400 		if (h->info.valid_hooks & (1 << NF_IP_FORWARD)) {
401 			n += get_entry(h, n)->next_offset;
402 			assert(h->info.hook_entry[NF_IP_FORWARD] == n);
403 			n = get_chain_end(h, n);
404 		}
405 
406 		n += get_entry(h, n)->next_offset;
407 		assert(h->info.hook_entry[NF_IP_LOCAL_OUT] == n);
408 		user_offset = h->info.hook_entry[NF_IP_LOCAL_OUT];
409 
410 		if (h->info.valid_hooks & (1 << NF_IP_POST_ROUTING)) {
411 			n = get_chain_end(h, n);
412 			n += get_entry(h, n)->next_offset;
413 			assert(h->info.hook_entry[NF_IP_POST_ROUTING] == n);
414 			user_offset = h->info.hook_entry[NF_IP_POST_ROUTING];
415 		}
416 	} else if (strcmp(h->info.name, "raw") == 0) {
417 		assert(h->info.valid_hooks
418 		       == (1 << NF_IP_PRE_ROUTING
419 			   | 1 << NF_IP_LOCAL_OUT));
420 
421 		/* Hooks should be first three */
422 		assert(h->info.hook_entry[NF_IP_PRE_ROUTING] == 0);
423 
424 		n = get_chain_end(h, n);
425 		n += get_entry(h, n)->next_offset;
426 		assert(h->info.hook_entry[NF_IP_LOCAL_OUT] == n);
427 
428 		user_offset = h->info.hook_entry[NF_IP_LOCAL_OUT];
429 
430 #ifdef NF_IP_DROPPING
431 	} else if (strcmp(h->info.name, "drop") == 0) {
432 		assert(h->info.valid_hooks == (1 << NF_IP_DROPPING));
433 
434 		/* Hook should be first */
435 		assert(h->info.hook_entry[NF_IP_DROPPING] == 0);
436 		user_offset = 0;
437 #endif
438 	} else {
439 		fprintf(stderr, "Unknown table `%s'\n", h->info.name);
440 		abort();
441 	}
442 
443 	/* User chain == end of last builtin + policy entry */
444 	user_offset = get_chain_end(h, user_offset);
445 	user_offset += get_entry(h, user_offset)->next_offset;
446 
447 	/* Overflows should be end of entry chains, and unconditional
448            policy nodes. */
449 	for (i = 0; i < NUMHOOKS; i++) {
450 		STRUCT_ENTRY *e;
451 		STRUCT_STANDARD_TARGET *t;
452 
453 		if (!(h->info.valid_hooks & (1 << i)))
454 			continue;
455 		assert(h->info.underflow[i]
456 		       == get_chain_end(h, h->info.hook_entry[i]));
457 
458 		e = get_entry(h, get_chain_end(h, h->info.hook_entry[i]));
459 		assert(unconditional(&e->ip));
460 		assert(e->target_offset == sizeof(*e));
461 		t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
462 		assert(t->target.u.target_size == ALIGN(sizeof(*t)));
463 		assert(e->next_offset == sizeof(*e) + ALIGN(sizeof(*t)));
464 
465 		assert(strcmp(t->target.u.user.name, STANDARD_TARGET)==0);
466 		assert(t->verdict == -NF_DROP-1 || t->verdict == -NF_ACCEPT-1);
467 
468 		/* Hooks and underflows must be valid entries */
469 		entry2index(h, get_entry(h, h->info.hook_entry[i]));
470 		entry2index(h, get_entry(h, h->info.underflow[i]));
471 	}
472 
473 	assert(h->info.size
474 	       >= h->info.num_entries * (sizeof(STRUCT_ENTRY)
475 					 +sizeof(STRUCT_STANDARD_TARGET)));
476 
477 	assert(h->entries.size
478 	       >= (h->new_number
479 		   * (sizeof(STRUCT_ENTRY)
480 		      + sizeof(STRUCT_STANDARD_TARGET))));
481 	assert(strcmp(h->info.name, h->entries.name) == 0);
482 
483 	i = 0; n = 0;
484 	was_return = 0;
485 	/* Check all the entries. */
486 	ENTRY_ITERATE(h->entries.entrytable, h->entries.size,
487 		      check_entry, &i, &n, user_offset, &was_return, h);
488 
489 	assert(i == h->new_number);
490 	assert(n == h->entries.size);
491 
492 	/* Final entry must be error node */
493 	assert(strcmp(GET_TARGET(index2entry(h, h->new_number-1))
494 		      ->u.user.name,
495 		      ERROR_TARGET) == 0);
496 }
497 #endif /*IPTC_DEBUG*/
498 
499 #endif
500