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