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). */
38 #include <arpa/inet.h>
40 #ifdef DEBUG_CONNTRACK
44 #if !defined(__GLIBC__) || (__GLIBC__ < 2)
45 typedef unsigned int socklen_t;
50 #define HOOK_PRE_ROUTING NF_IP6_PRE_ROUTING
51 #define HOOK_LOCAL_IN NF_IP6_LOCAL_IN
52 #define HOOK_FORWARD NF_IP6_FORWARD
53 #define HOOK_LOCAL_OUT NF_IP6_LOCAL_OUT
54 #define HOOK_POST_ROUTING NF_IP6_POST_ROUTING
56 #define STRUCT_ENTRY_TARGET struct ip6t_entry_target
57 #define STRUCT_ENTRY struct ip6t_entry
58 #define STRUCT_ENTRY_MATCH struct ip6t_entry_match
59 #define STRUCT_GETINFO struct ip6t_getinfo
60 #define STRUCT_GET_ENTRIES struct ip6t_get_entries
61 #define STRUCT_COUNTERS struct ip6t_counters
62 #define STRUCT_COUNTERS_INFO struct ip6t_counters_info
63 #define STRUCT_STANDARD_TARGET struct ip6t_standard_target
64 #define STRUCT_REPLACE struct ip6t_replace
66 #define STRUCT_TC_HANDLE struct ip6tc_handle
67 #define TC_HANDLE_T ip6tc_handle_t
69 #define ENTRY_ITERATE IP6T_ENTRY_ITERATE
70 #define TABLE_MAXNAMELEN IP6T_TABLE_MAXNAMELEN
71 #define FUNCTION_MAXNAMELEN IP6T_FUNCTION_MAXNAMELEN
73 #define GET_TARGET ip6t_get_target
75 #define ERROR_TARGET IP6T_ERROR_TARGET
76 #define NUMHOOKS NF_IP6_NUMHOOKS
78 #define IPT_CHAINLABEL ip6t_chainlabel
80 #define TC_DUMP_ENTRIES dump_entries6
81 #define TC_IS_CHAIN ip6tc_is_chain
82 #define TC_FIRST_CHAIN ip6tc_first_chain
83 #define TC_NEXT_CHAIN ip6tc_next_chain
84 #define TC_FIRST_RULE ip6tc_first_rule
85 #define TC_NEXT_RULE ip6tc_next_rule
86 #define TC_GET_TARGET ip6tc_get_target
87 #define TC_BUILTIN ip6tc_builtin
88 #define TC_GET_POLICY ip6tc_get_policy
89 #define TC_INSERT_ENTRY ip6tc_insert_entry
90 #define TC_REPLACE_ENTRY ip6tc_replace_entry
91 #define TC_APPEND_ENTRY ip6tc_append_entry
92 #define TC_DELETE_ENTRY ip6tc_delete_entry
93 #define TC_DELETE_NUM_ENTRY ip6tc_delete_num_entry
94 #define TC_CHECK_PACKET ip6tc_check_packet
95 #define TC_FLUSH_ENTRIES ip6tc_flush_entries
96 #define TC_ZERO_ENTRIES ip6tc_zero_entries
97 #define TC_ZERO_COUNTER ip6tc_zero_counter
98 #define TC_READ_COUNTER ip6tc_read_counter
99 #define TC_SET_COUNTER ip6tc_set_counter
100 #define TC_CREATE_CHAIN ip6tc_create_chain
101 #define TC_GET_REFERENCES ip6tc_get_references
102 #define TC_DELETE_CHAIN ip6tc_delete_chain
103 #define TC_RENAME_CHAIN ip6tc_rename_chain
104 #define TC_SET_POLICY ip6tc_set_policy
105 #define TC_GET_RAW_SOCKET ip6tc_get_raw_socket
106 #define TC_INIT ip6tc_init
107 #define TC_FREE ip6tc_free
108 #define TC_COMMIT ip6tc_commit
109 #define TC_STRERROR ip6tc_strerror
110 #define TC_NUM_RULES ip6tc_num_rules
111 #define TC_GET_RULE ip6tc_get_rule
113 #define TC_AF AF_INET6
114 #define TC_IPPROTO IPPROTO_IPV6
116 #define SO_SET_REPLACE IP6T_SO_SET_REPLACE
117 #define SO_SET_ADD_COUNTERS IP6T_SO_SET_ADD_COUNTERS
118 #define SO_GET_INFO IP6T_SO_GET_INFO
119 #define SO_GET_ENTRIES IP6T_SO_GET_ENTRIES
120 #define SO_GET_VERSION IP6T_SO_GET_VERSION
122 #define STANDARD_TARGET IP6T_STANDARD_TARGET
123 #define LABEL_RETURN IP6TC_LABEL_RETURN
124 #define LABEL_ACCEPT IP6TC_LABEL_ACCEPT
125 #define LABEL_DROP IP6TC_LABEL_DROP
126 #define LABEL_QUEUE IP6TC_LABEL_QUEUE
128 #define ALIGN IP6T_ALIGN
129 #define RETURN IP6T_RETURN
134 ((ntohl(a->s6_addr32[(l) / 32]) >> (31 - ((l) & 31))) & 1)
137 ipv6_prefix_length(const struct in6_addr *a)
140 for (l = 0; l < 128; l++) {
144 for (i = l + 1; i < 128; i++) {
152 dump_entry(struct ip6t_entry *e, const ip6tc_handle_t handle)
157 struct ip6t_entry_target *t;
159 printf("Entry %u (%lu):\n", iptcb_entry2index(handle, e),
160 iptcb_entry2offset(handle, e));
162 inet_ntop(AF_INET6, &e->ipv6.src, buf, sizeof buf);
165 len = ipv6_prefix_length(&e->ipv6.smsk);
169 inet_ntop(AF_INET6, &e->ipv6.smsk, buf, sizeof buf);
175 inet_ntop(AF_INET6, &e->ipv6.dst, buf, sizeof buf);
178 len = ipv6_prefix_length(&e->ipv6.dmsk);
182 inet_ntop(AF_INET6, &e->ipv6.dmsk, buf, sizeof buf);
187 printf("Interface: `%s'/", e->ipv6.iniface);
188 for (i = 0; i < IFNAMSIZ; i++)
189 printf("%c", e->ipv6.iniface_mask[i] ? 'X' : '.');
190 printf("to `%s'/", e->ipv6.outiface);
191 for (i = 0; i < IFNAMSIZ; i++)
192 printf("%c", e->ipv6.outiface_mask[i] ? 'X' : '.');
193 printf("\nProtocol: %u\n", e->ipv6.proto);
194 if (e->ipv6.flags & IP6T_F_TOS)
195 printf("TOS: %u\n", e->ipv6.tos);
196 printf("Flags: %02X\n", e->ipv6.flags);
197 printf("Invflags: %02X\n", e->ipv6.invflags);
198 printf("Counters: %llu packets, %llu bytes\n",
199 (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
200 printf("Cache: %08X\n", e->nfcache);
202 IP6T_MATCH_ITERATE(e, print_match);
204 t = ip6t_get_target(e);
205 printf("Target name: `%s' [%u]\n", t->u.user.name, t->u.target_size);
206 if (strcmp(t->u.user.name, IP6T_STANDARD_TARGET) == 0) {
207 const unsigned char *data = t->data;
208 int pos = *(const int *)data;
210 printf("verdict=%s\n",
211 pos == -NF_ACCEPT-1 ? "NF_ACCEPT"
212 : pos == -NF_DROP-1 ? "NF_DROP"
213 : pos == IP6T_RETURN ? "RETURN"
216 printf("verdict=%u\n", pos);
217 } else if (strcmp(t->u.user.name, IP6T_ERROR_TARGET) == 0)
218 printf("error=`%s'\n", t->data);
224 static unsigned char *
225 is_same(const STRUCT_ENTRY *a, const STRUCT_ENTRY *b,
226 unsigned char *matchmask)
231 /* Always compare head structures: ignore mask here. */
232 if (memcmp(&a->ipv6.src, &b->ipv6.src, sizeof(struct in6_addr))
233 || memcmp(&a->ipv6.dst, &b->ipv6.dst, sizeof(struct in6_addr))
234 || memcmp(&a->ipv6.smsk, &b->ipv6.smsk, sizeof(struct in6_addr))
235 || memcmp(&a->ipv6.dmsk, &b->ipv6.dmsk, sizeof(struct in6_addr))
236 || a->ipv6.proto != b->ipv6.proto
237 || a->ipv6.tos != b->ipv6.tos
238 || a->ipv6.flags != b->ipv6.flags
239 || a->ipv6.invflags != b->ipv6.invflags)
242 for (i = 0; i < IFNAMSIZ; i++) {
243 if (a->ipv6.iniface_mask[i] != b->ipv6.iniface_mask[i])
245 if ((a->ipv6.iniface[i] & a->ipv6.iniface_mask[i])
246 != (b->ipv6.iniface[i] & b->ipv6.iniface_mask[i]))
248 if (a->ipv6.outiface_mask[i] != b->ipv6.outiface_mask[i])
250 if ((a->ipv6.outiface[i] & a->ipv6.outiface_mask[i])
251 != (b->ipv6.outiface[i] & b->ipv6.outiface_mask[i]))
255 if (a->target_offset != b->target_offset
256 || a->next_offset != b->next_offset)
259 mptr = matchmask + sizeof(STRUCT_ENTRY);
260 if (IP6T_MATCH_ITERATE(a, match_different, a->elems, b->elems, &mptr))
262 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_target));
267 /* All zeroes == unconditional rule. */
269 unconditional(const struct ip6t_ip6 *ipv6)
273 for (i = 0; i < sizeof(*ipv6); i++)
274 if (((char *)ipv6)[i])
277 return (i == sizeof(*ipv6));
281 /* Do every conceivable sanity check on the handle */
283 do_check(TC_HANDLE_T h, unsigned int line)
286 unsigned int user_offset; /* Offset of first user chain */
289 assert(h->changed == 0 || h->changed == 1);
290 if (strcmp(h->info.name, "filter") == 0) {
291 assert(h->info.valid_hooks
292 == (1 << NF_IP6_LOCAL_IN
293 | 1 << NF_IP6_FORWARD
294 | 1 << NF_IP6_LOCAL_OUT));
296 /* Hooks should be first three */
297 assert(h->info.hook_entry[NF_IP6_LOCAL_IN] == 0);
299 n = get_chain_end(h, 0);
300 n += get_entry(h, n)->next_offset;
301 assert(h->info.hook_entry[NF_IP6_FORWARD] == n);
303 n = get_chain_end(h, n);
304 n += get_entry(h, n)->next_offset;
305 assert(h->info.hook_entry[NF_IP6_LOCAL_OUT] == n);
307 user_offset = h->info.hook_entry[NF_IP6_LOCAL_OUT];
308 } else if (strcmp(h->info.name, "nat") == 0) {
309 assert((h->info.valid_hooks
310 == (1 << NF_IP6_PRE_ROUTING
311 | 1 << NF_IP6_LOCAL_OUT
312 | 1 << NF_IP6_POST_ROUTING)) ||
314 == (1 << NF_IP6_PRE_ROUTING
315 | 1 << NF_IP6_LOCAL_IN
316 | 1 << NF_IP6_LOCAL_OUT
317 | 1 << NF_IP6_POST_ROUTING)));
319 assert(h->info.hook_entry[NF_IP6_PRE_ROUTING] == 0);
321 n = get_chain_end(h, 0);
323 n += get_entry(h, n)->next_offset;
324 assert(h->info.hook_entry[NF_IP6_POST_ROUTING] == n);
325 n = get_chain_end(h, n);
327 n += get_entry(h, n)->next_offset;
328 assert(h->info.hook_entry[NF_IP6_LOCAL_OUT] == n);
329 user_offset = h->info.hook_entry[NF_IP6_LOCAL_OUT];
331 if (h->info.valid_hooks & (1 << NF_IP6_LOCAL_IN)) {
332 n = get_chain_end(h, n);
333 n += get_entry(h, n)->next_offset;
334 assert(h->info.hook_entry[NF_IP6_LOCAL_IN] == n);
335 user_offset = h->info.hook_entry[NF_IP6_LOCAL_IN];
338 } else if (strcmp(h->info.name, "mangle") == 0) {
339 /* This code is getting ugly because linux < 2.4.18-pre6 had
340 * two mangle hooks, linux >= 2.4.18-pre6 has five mangle hooks
342 assert((h->info.valid_hooks
343 == (1 << NF_IP6_PRE_ROUTING
344 | 1 << NF_IP6_LOCAL_OUT)) ||
346 == (1 << NF_IP6_PRE_ROUTING
347 | 1 << NF_IP6_LOCAL_IN
348 | 1 << NF_IP6_FORWARD
349 | 1 << NF_IP6_LOCAL_OUT
350 | 1 << NF_IP6_POST_ROUTING)));
352 /* Hooks should be first five */
353 assert(h->info.hook_entry[NF_IP6_PRE_ROUTING] == 0);
355 n = get_chain_end(h, 0);
357 if (h->info.valid_hooks & (1 << NF_IP6_LOCAL_IN)) {
358 n += get_entry(h, n)->next_offset;
359 assert(h->info.hook_entry[NF_IP6_LOCAL_IN] == n);
360 n = get_chain_end(h, n);
363 if (h->info.valid_hooks & (1 << NF_IP6_FORWARD)) {
364 n += get_entry(h, n)->next_offset;
365 assert(h->info.hook_entry[NF_IP6_FORWARD] == n);
366 n = get_chain_end(h, n);
369 n += get_entry(h, n)->next_offset;
370 assert(h->info.hook_entry[NF_IP6_LOCAL_OUT] == n);
371 user_offset = h->info.hook_entry[NF_IP6_LOCAL_OUT];
373 if (h->info.valid_hooks & (1 << NF_IP6_POST_ROUTING)) {
374 n = get_chain_end(h, n);
375 n += get_entry(h, n)->next_offset;
376 assert(h->info.hook_entry[NF_IP6_POST_ROUTING] == n);
377 user_offset = h->info.hook_entry[NF_IP6_POST_ROUTING];
379 } else if (strcmp(h->info.name, "raw") == 0) {
380 assert(h->info.valid_hooks
381 == (1 << NF_IP6_PRE_ROUTING
382 | 1 << NF_IP6_LOCAL_OUT));
384 /* Hooks should be first three */
385 assert(h->info.hook_entry[NF_IP6_PRE_ROUTING] == 0);
387 n = get_chain_end(h, n);
388 n += get_entry(h, n)->next_offset;
389 assert(h->info.hook_entry[NF_IP6_LOCAL_OUT] == n);
391 user_offset = h->info.hook_entry[NF_IP6_LOCAL_OUT];
393 fprintf(stderr, "Unknown table `%s'\n", h->info.name);
397 /* User chain == end of last builtin + policy entry */
398 user_offset = get_chain_end(h, user_offset);
399 user_offset += get_entry(h, user_offset)->next_offset;
401 /* Overflows should be end of entry chains, and unconditional
403 for (i = 0; i < NUMHOOKS; i++) {
405 STRUCT_STANDARD_TARGET *t;
407 if (!(h->info.valid_hooks & (1 << i)))
409 assert(h->info.underflow[i]
410 == get_chain_end(h, h->info.hook_entry[i]));
412 e = get_entry(h, get_chain_end(h, h->info.hook_entry[i]));
413 assert(unconditional(&e->ipv6));
414 assert(e->target_offset == sizeof(*e));
415 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
416 printf("target_size=%u, align=%u\n",
417 t->target.u.target_size, ALIGN(sizeof(*t)));
418 assert(t->target.u.target_size == ALIGN(sizeof(*t)));
419 assert(e->next_offset == sizeof(*e) + ALIGN(sizeof(*t)));
421 assert(strcmp(t->target.u.user.name, STANDARD_TARGET)==0);
422 assert(t->verdict == -NF_DROP-1 || t->verdict == -NF_ACCEPT-1);
424 /* Hooks and underflows must be valid entries */
425 iptcb_entry2index(h, get_entry(h, h->info.hook_entry[i]));
426 iptcb_entry2index(h, get_entry(h, h->info.underflow[i]));
430 >= h->info.num_entries * (sizeof(STRUCT_ENTRY)
431 +sizeof(STRUCT_STANDARD_TARGET)));
433 assert(h->entries.size
435 * (sizeof(STRUCT_ENTRY)
436 + sizeof(STRUCT_STANDARD_TARGET))));
437 assert(strcmp(h->info.name, h->entries.name) == 0);
443 /* Check all the entries. */
444 ENTRY_ITERATE(h->entries.entrytable, h->entries.size,
445 check_entry, &i, &n, user_offset, &was_return, h);
447 assert(i == h->new_number);
448 assert(n == h->entries.size);
450 /* Final entry must be error node */
451 assert(strcmp(GET_TARGET(index2entry(h, h->new_number-1))
456 #endif /*IPTC_DEBUG*/