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