2 * This file was imported from the iptables sources.
3 * Copyright (C) 1999-2008 Netfilter Core Team
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 /* Library which manipulates firewall rules. Version 0.1. */
21 /* Architecture of firewall rules is as follows:
23 * Chains go INPUT, FORWARD, OUTPUT then user chains.
24 * Each user chain starts with an ERROR node.
25 * Every chain ends with an unconditional jump: a RETURN for user chains,
26 * and a POLICY for built-ins.
29 /* (C)1999 Paul ``Rusty'' Russell - Placed under the GNU GPL (See
30 COPYING for details). */
39 #ifdef DEBUG_CONNTRACK
43 #if !defined(__GLIBC__) || (__GLIBC__ < 2)
44 typedef unsigned int socklen_t;
50 #define IP_OFFSET 0x1FFF
52 #define HOOK_PRE_ROUTING NF_IP_PRE_ROUTING
53 #define HOOK_LOCAL_IN NF_IP_LOCAL_IN
54 #define HOOK_FORWARD NF_IP_FORWARD
55 #define HOOK_LOCAL_OUT NF_IP_LOCAL_OUT
56 #define HOOK_POST_ROUTING NF_IP_POST_ROUTING
58 #define HOOK_DROPPING NF_IP_DROPPING
61 #define STRUCT_ENTRY_TARGET struct ipt_entry_target
62 #define STRUCT_ENTRY struct ipt_entry
63 #define STRUCT_ENTRY_MATCH struct ipt_entry_match
64 #define STRUCT_GETINFO struct ipt_getinfo
65 #define STRUCT_GET_ENTRIES struct ipt_get_entries
66 #define STRUCT_COUNTERS struct ipt_counters
67 #define STRUCT_COUNTERS_INFO struct ipt_counters_info
68 #define STRUCT_STANDARD_TARGET struct ipt_standard_target
69 #define STRUCT_REPLACE struct ipt_replace
71 #define STRUCT_TC_HANDLE struct iptc_handle
72 #define TC_HANDLE_T iptc_handle_t
74 #define ENTRY_ITERATE IPT_ENTRY_ITERATE
75 #define TABLE_MAXNAMELEN IPT_TABLE_MAXNAMELEN
76 #define FUNCTION_MAXNAMELEN IPT_FUNCTION_MAXNAMELEN
78 #define GET_TARGET ipt_get_target
80 #define ERROR_TARGET IPT_ERROR_TARGET
81 #define NUMHOOKS NF_IP_NUMHOOKS
83 #define IPT_CHAINLABEL ipt_chainlabel
85 #define TC_DUMP_ENTRIES dump_entries
86 #define TC_IS_CHAIN iptc_is_chain
87 #define TC_FIRST_CHAIN iptc_first_chain
88 #define TC_NEXT_CHAIN iptc_next_chain
89 #define TC_FIRST_RULE iptc_first_rule
90 #define TC_NEXT_RULE iptc_next_rule
91 #define TC_GET_TARGET iptc_get_target
92 #define TC_BUILTIN iptc_builtin
93 #define TC_GET_POLICY iptc_get_policy
94 #define TC_INSERT_ENTRY iptc_insert_entry
95 #define TC_REPLACE_ENTRY iptc_replace_entry
96 #define TC_APPEND_ENTRY iptc_append_entry
97 #define TC_DELETE_ENTRY iptc_delete_entry
98 #define TC_DELETE_NUM_ENTRY iptc_delete_num_entry
99 #define TC_CHECK_PACKET iptc_check_packet
100 #define TC_FLUSH_ENTRIES iptc_flush_entries
101 #define TC_ZERO_ENTRIES iptc_zero_entries
102 #define TC_READ_COUNTER iptc_read_counter
103 #define TC_ZERO_COUNTER iptc_zero_counter
104 #define TC_SET_COUNTER iptc_set_counter
105 #define TC_CREATE_CHAIN iptc_create_chain
106 #define TC_GET_REFERENCES iptc_get_references
107 #define TC_DELETE_CHAIN iptc_delete_chain
108 #define TC_RENAME_CHAIN iptc_rename_chain
109 #define TC_SET_POLICY iptc_set_policy
110 #define TC_GET_RAW_SOCKET iptc_get_raw_socket
111 #define TC_INIT iptc_init
112 #define TC_FREE iptc_free
113 #define TC_COMMIT iptc_commit
114 #define TC_STRERROR iptc_strerror
115 #define TC_NUM_RULES iptc_num_rules
116 #define TC_GET_RULE iptc_get_rule
118 #define TC_AF AF_INET
119 #define TC_IPPROTO IPPROTO_IP
121 #define SO_SET_REPLACE IPT_SO_SET_REPLACE
122 #define SO_SET_ADD_COUNTERS IPT_SO_SET_ADD_COUNTERS
123 #define SO_GET_INFO IPT_SO_GET_INFO
124 #define SO_GET_ENTRIES IPT_SO_GET_ENTRIES
125 #define SO_GET_VERSION IPT_SO_GET_VERSION
127 #define STANDARD_TARGET IPT_STANDARD_TARGET
128 #define LABEL_RETURN IPTC_LABEL_RETURN
129 #define LABEL_ACCEPT IPTC_LABEL_ACCEPT
130 #define LABEL_DROP IPTC_LABEL_DROP
131 #define LABEL_QUEUE IPTC_LABEL_QUEUE
133 #define ALIGN IPT_ALIGN
134 #define RETURN IPT_RETURN
138 #define IP_PARTS_NATIVE(n) \
139 (unsigned int)((n)>>24)&0xFF, \
140 (unsigned int)((n)>>16)&0xFF, \
141 (unsigned int)((n)>>8)&0xFF, \
142 (unsigned int)((n)&0xFF)
144 #define IP_PARTS(n) IP_PARTS_NATIVE(ntohl(n))
147 dump_entry(STRUCT_ENTRY *e, const TC_HANDLE_T handle)
150 STRUCT_ENTRY_TARGET *t;
152 printf("Entry %u (%lu):\n", iptcb_entry2index(handle, e),
153 iptcb_entry2offset(handle, e));
154 printf("SRC IP: %u.%u.%u.%u/%u.%u.%u.%u\n",
155 IP_PARTS(e->ip.src.s_addr),IP_PARTS(e->ip.smsk.s_addr));
156 printf("DST IP: %u.%u.%u.%u/%u.%u.%u.%u\n",
157 IP_PARTS(e->ip.dst.s_addr),IP_PARTS(e->ip.dmsk.s_addr));
158 printf("Interface: `%s'/", e->ip.iniface);
159 for (i = 0; i < IFNAMSIZ; i++)
160 printf("%c", e->ip.iniface_mask[i] ? 'X' : '.');
161 printf("to `%s'/", e->ip.outiface);
162 for (i = 0; i < IFNAMSIZ; i++)
163 printf("%c", e->ip.outiface_mask[i] ? 'X' : '.');
164 printf("\nProtocol: %u\n", e->ip.proto);
165 printf("Flags: %02X\n", e->ip.flags);
166 printf("Invflags: %02X\n", e->ip.invflags);
167 printf("Counters: %llu packets, %llu bytes\n",
168 (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
169 printf("Cache: %08X\n", e->nfcache);
171 IPT_MATCH_ITERATE(e, print_match);
174 printf("Target name: `%s' [%u]\n", t->u.user.name, t->u.target_size);
175 if (strcmp(t->u.user.name, STANDARD_TARGET) == 0) {
176 int pos = *(int *)t->data;
178 printf("verdict=%s\n",
179 pos == -NF_ACCEPT-1 ? "NF_ACCEPT"
180 : pos == -NF_DROP-1 ? "NF_DROP"
181 : pos == -NF_QUEUE-1 ? "NF_QUEUE"
182 : pos == RETURN ? "RETURN"
185 printf("verdict=%u\n", pos);
186 } else if (strcmp(t->u.user.name, IPT_ERROR_TARGET) == 0)
187 printf("error=`%s'\n", t->data);
193 static unsigned char *
194 is_same(const STRUCT_ENTRY *a, const STRUCT_ENTRY *b, unsigned char *matchmask)
199 /* Always compare head structures: ignore mask here. */
200 if (a->ip.src.s_addr != b->ip.src.s_addr
201 || a->ip.dst.s_addr != b->ip.dst.s_addr
202 || a->ip.smsk.s_addr != b->ip.smsk.s_addr
203 || a->ip.dmsk.s_addr != b->ip.dmsk.s_addr
204 || a->ip.proto != b->ip.proto
205 || a->ip.flags != b->ip.flags
206 || a->ip.invflags != b->ip.invflags)
209 for (i = 0; i < IFNAMSIZ; i++) {
210 if (a->ip.iniface_mask[i] != b->ip.iniface_mask[i])
212 if ((a->ip.iniface[i] & a->ip.iniface_mask[i])
213 != (b->ip.iniface[i] & b->ip.iniface_mask[i]))
215 if (a->ip.outiface_mask[i] != b->ip.outiface_mask[i])
217 if ((a->ip.outiface[i] & a->ip.outiface_mask[i])
218 != (b->ip.outiface[i] & b->ip.outiface_mask[i]))
222 if (a->target_offset != b->target_offset
223 || a->next_offset != b->next_offset)
226 mptr = matchmask + sizeof(STRUCT_ENTRY);
227 if (IPT_MATCH_ITERATE(a, match_different, a->elems, b->elems, &mptr))
229 mptr += IPT_ALIGN(sizeof(struct ipt_entry_target));
235 /***************************** DEBUGGING ********************************/
237 unconditional(const struct ipt_ip *ip)
241 for (i = 0; i < sizeof(*ip)/sizeof(u_int32_t); i++)
242 if (((u_int32_t *)ip)[i])
249 check_match(const STRUCT_ENTRY_MATCH *m, unsigned int *off)
251 assert(m->u.match_size >= sizeof(STRUCT_ENTRY_MATCH));
252 assert(ALIGN(m->u.match_size) == m->u.match_size);
254 (*off) += m->u.match_size;
259 check_entry(const STRUCT_ENTRY *e, unsigned int *i, unsigned int *off,
260 unsigned int user_offset, int *was_return,
264 STRUCT_STANDARD_TARGET *t;
266 assert(e->target_offset >= sizeof(STRUCT_ENTRY));
267 assert(e->next_offset >= e->target_offset
268 + sizeof(STRUCT_ENTRY_TARGET));
269 toff = sizeof(STRUCT_ENTRY);
270 IPT_MATCH_ITERATE(e, check_match, &toff);
272 assert(toff == e->target_offset);
274 t = (STRUCT_STANDARD_TARGET *)
275 GET_TARGET((STRUCT_ENTRY *)e);
276 /* next_offset will have to be multiple of entry alignment. */
277 assert(e->next_offset == ALIGN(e->next_offset));
278 assert(e->target_offset == ALIGN(e->target_offset));
279 assert(t->target.u.target_size == ALIGN(t->target.u.target_size));
280 assert(!TC_IS_CHAIN(t->target.u.user.name, h));
282 if (strcmp(t->target.u.user.name, STANDARD_TARGET) == 0) {
283 assert(t->target.u.target_size
284 == ALIGN(sizeof(STRUCT_STANDARD_TARGET)));
286 assert(t->verdict == -NF_DROP-1
287 || t->verdict == -NF_ACCEPT-1
288 || t->verdict == RETURN
289 || t->verdict < (int)h->entries->size);
291 if (t->verdict >= 0) {
292 STRUCT_ENTRY *te = get_entry(h, t->verdict);
295 idx = iptcb_entry2index(h, te);
296 assert(strcmp(GET_TARGET(te)->u.user.name,
301 /* Prior node must be error node, or this node. */
302 assert(t->verdict == iptcb_entry2offset(h, e)+e->next_offset
303 || strcmp(GET_TARGET(index2entry(h, idx-1))
304 ->u.user.name, IPT_ERROR_TARGET)
308 if (t->verdict == RETURN
309 && unconditional(&e->ip)
310 && e->target_offset == sizeof(*e))
314 } else if (strcmp(t->target.u.user.name, IPT_ERROR_TARGET) == 0) {
315 assert(t->target.u.target_size
316 == ALIGN(sizeof(struct ipt_error_target)));
318 /* If this is in user area, previous must have been return */
319 if (*off > user_offset)
324 else *was_return = 0;
326 if (*off == user_offset)
327 assert(strcmp(t->target.u.user.name, IPT_ERROR_TARGET) == 0);
329 (*off) += e->next_offset;
335 /* Do every conceivable sanity check on the handle */
337 do_check(TC_HANDLE_T h, unsigned int line)
340 unsigned int user_offset; /* Offset of first user chain */
343 assert(h->changed == 0 || h->changed == 1);
344 if (strcmp(h->info.name, "filter") == 0) {
345 assert(h->info.valid_hooks
346 == (1 << NF_IP_LOCAL_IN
348 | 1 << NF_IP_LOCAL_OUT));
350 /* Hooks should be first three */
351 assert(h->info.hook_entry[NF_IP_LOCAL_IN] == 0);
353 n = get_chain_end(h, 0);
354 n += get_entry(h, n)->next_offset;
355 assert(h->info.hook_entry[NF_IP_FORWARD] == n);
357 n = get_chain_end(h, n);
358 n += get_entry(h, n)->next_offset;
359 assert(h->info.hook_entry[NF_IP_LOCAL_OUT] == n);
361 user_offset = h->info.hook_entry[NF_IP_LOCAL_OUT];
362 } else if (strcmp(h->info.name, "nat") == 0) {
363 assert((h->info.valid_hooks
364 == (1 << NF_IP_PRE_ROUTING
365 | 1 << NF_IP_POST_ROUTING
366 | 1 << NF_IP_LOCAL_OUT)) ||
368 == (1 << NF_IP_PRE_ROUTING
369 | 1 << NF_IP_LOCAL_IN
370 | 1 << NF_IP_POST_ROUTING
371 | 1 << NF_IP_LOCAL_OUT)));
373 assert(h->info.hook_entry[NF_IP_PRE_ROUTING] == 0);
375 n = get_chain_end(h, 0);
377 n += get_entry(h, n)->next_offset;
378 assert(h->info.hook_entry[NF_IP_POST_ROUTING] == n);
379 n = get_chain_end(h, n);
381 n += get_entry(h, n)->next_offset;
382 assert(h->info.hook_entry[NF_IP_LOCAL_OUT] == n);
383 user_offset = h->info.hook_entry[NF_IP_LOCAL_OUT];
385 if (h->info.valid_hooks & (1 << NF_IP_LOCAL_IN)) {
386 n = get_chain_end(h, n);
387 n += get_entry(h, n)->next_offset;
388 assert(h->info.hook_entry[NF_IP_LOCAL_IN] == n);
389 user_offset = h->info.hook_entry[NF_IP_LOCAL_IN];
392 } else if (strcmp(h->info.name, "mangle") == 0) {
393 /* This code is getting ugly because linux < 2.4.18-pre6 had
394 * two mangle hooks, linux >= 2.4.18-pre6 has five mangle hooks
396 assert((h->info.valid_hooks
397 == (1 << NF_IP_PRE_ROUTING
398 | 1 << NF_IP_LOCAL_OUT)) ||
400 == (1 << NF_IP_PRE_ROUTING
401 | 1 << NF_IP_LOCAL_IN
403 | 1 << NF_IP_LOCAL_OUT
404 | 1 << NF_IP_POST_ROUTING)));
406 /* Hooks should be first five */
407 assert(h->info.hook_entry[NF_IP_PRE_ROUTING] == 0);
409 n = get_chain_end(h, 0);
411 if (h->info.valid_hooks & (1 << NF_IP_LOCAL_IN)) {
412 n += get_entry(h, n)->next_offset;
413 assert(h->info.hook_entry[NF_IP_LOCAL_IN] == n);
414 n = get_chain_end(h, n);
417 if (h->info.valid_hooks & (1 << NF_IP_FORWARD)) {
418 n += get_entry(h, n)->next_offset;
419 assert(h->info.hook_entry[NF_IP_FORWARD] == n);
420 n = get_chain_end(h, n);
423 n += get_entry(h, n)->next_offset;
424 assert(h->info.hook_entry[NF_IP_LOCAL_OUT] == n);
425 user_offset = h->info.hook_entry[NF_IP_LOCAL_OUT];
427 if (h->info.valid_hooks & (1 << NF_IP_POST_ROUTING)) {
428 n = get_chain_end(h, n);
429 n += get_entry(h, n)->next_offset;
430 assert(h->info.hook_entry[NF_IP_POST_ROUTING] == n);
431 user_offset = h->info.hook_entry[NF_IP_POST_ROUTING];
433 } else if (strcmp(h->info.name, "raw") == 0) {
434 assert(h->info.valid_hooks
435 == (1 << NF_IP_PRE_ROUTING
436 | 1 << NF_IP_LOCAL_OUT));
438 /* Hooks should be first three */
439 assert(h->info.hook_entry[NF_IP_PRE_ROUTING] == 0);
441 n = get_chain_end(h, n);
442 n += get_entry(h, n)->next_offset;
443 assert(h->info.hook_entry[NF_IP_LOCAL_OUT] == n);
445 user_offset = h->info.hook_entry[NF_IP_LOCAL_OUT];
447 #ifdef NF_IP_DROPPING
448 } else if (strcmp(h->info.name, "drop") == 0) {
449 assert(h->info.valid_hooks == (1 << NF_IP_DROPPING));
451 /* Hook should be first */
452 assert(h->info.hook_entry[NF_IP_DROPPING] == 0);
456 fprintf(stderr, "Unknown table `%s'\n", h->info.name);
460 /* User chain == end of last builtin + policy entry */
461 user_offset = get_chain_end(h, user_offset);
462 user_offset += get_entry(h, user_offset)->next_offset;
464 /* Overflows should be end of entry chains, and unconditional
466 for (i = 0; i < NUMHOOKS; i++) {
468 STRUCT_STANDARD_TARGET *t;
470 if (!(h->info.valid_hooks & (1 << i)))
472 assert(h->info.underflow[i]
473 == get_chain_end(h, h->info.hook_entry[i]));
475 e = get_entry(h, get_chain_end(h, h->info.hook_entry[i]));
476 assert(unconditional(&e->ip));
477 assert(e->target_offset == sizeof(*e));
478 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
479 assert(t->target.u.target_size == ALIGN(sizeof(*t)));
480 assert(e->next_offset == sizeof(*e) + ALIGN(sizeof(*t)));
482 assert(strcmp(t->target.u.user.name, STANDARD_TARGET)==0);
483 assert(t->verdict == -NF_DROP-1 || t->verdict == -NF_ACCEPT-1);
485 /* Hooks and underflows must be valid entries */
486 entry2index(h, get_entry(h, h->info.hook_entry[i]));
487 entry2index(h, get_entry(h, h->info.underflow[i]));
491 >= h->info.num_entries * (sizeof(STRUCT_ENTRY)
492 +sizeof(STRUCT_STANDARD_TARGET)));
494 assert(h->entries.size
496 * (sizeof(STRUCT_ENTRY)
497 + sizeof(STRUCT_STANDARD_TARGET))));
498 assert(strcmp(h->info.name, h->entries.name) == 0);
502 /* Check all the entries. */
503 ENTRY_ITERATE(h->entries.entrytable, h->entries.size,
504 check_entry, &i, &n, user_offset, &was_return, h);
506 assert(i == h->new_number);
507 assert(n == h->entries.size);
509 /* Final entry must be error node */
510 assert(strcmp(GET_TARGET(index2entry(h, h->new_number-1))
514 #endif /*IPTC_DEBUG*/