blob: dae85eb5829a95bd5433c0646c3b76aed8aafa5e [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#include <arpa/inet.h>
20
21#ifdef DEBUG_CONNTRACK
22#define inline
23#endif
24
25#if !defined(__GLIBC__) || (__GLIBC__ < 2)
26typedef unsigned int socklen_t;
27#endif
28
29#include "libiptc/libip6tc.h"
30
31#define HOOK_PRE_ROUTING NF_IP6_PRE_ROUTING
32#define HOOK_LOCAL_IN NF_IP6_LOCAL_IN
33#define HOOK_FORWARD NF_IP6_FORWARD
34#define HOOK_LOCAL_OUT NF_IP6_LOCAL_OUT
35#define HOOK_POST_ROUTING NF_IP6_POST_ROUTING
36
37#define STRUCT_ENTRY_TARGET struct ip6t_entry_target
38#define STRUCT_ENTRY struct ip6t_entry
39#define STRUCT_ENTRY_MATCH struct ip6t_entry_match
40#define STRUCT_GETINFO struct ip6t_getinfo
41#define STRUCT_GET_ENTRIES struct ip6t_get_entries
42#define STRUCT_COUNTERS struct ip6t_counters
43#define STRUCT_COUNTERS_INFO struct ip6t_counters_info
44#define STRUCT_STANDARD_TARGET struct ip6t_standard_target
45#define STRUCT_REPLACE struct ip6t_replace
46
47#define STRUCT_TC_HANDLE struct ip6tc_handle
48#define TC_HANDLE_T ip6tc_handle_t
49
50#define ENTRY_ITERATE IP6T_ENTRY_ITERATE
51#define TABLE_MAXNAMELEN IP6T_TABLE_MAXNAMELEN
52#define FUNCTION_MAXNAMELEN IP6T_FUNCTION_MAXNAMELEN
53
54#define GET_TARGET ip6t_get_target
55
56#define ERROR_TARGET IP6T_ERROR_TARGET
57#define NUMHOOKS NF_IP6_NUMHOOKS
58
59#define IPT_CHAINLABEL ip6t_chainlabel
60
61#define TC_DUMP_ENTRIES dump_entries6
62#define TC_IS_CHAIN ip6tc_is_chain
Philip Blundell8c700902000-05-15 02:17:52 +000063#define TC_FIRST_CHAIN ip6tc_first_chain
Rusty Russell79dee072000-05-02 16:45:16 +000064#define TC_NEXT_CHAIN ip6tc_next_chain
Philip Blundell8c700902000-05-15 02:17:52 +000065#define TC_FIRST_RULE ip6tc_first_rule
66#define TC_NEXT_RULE ip6tc_next_rule
Rusty Russell79dee072000-05-02 16:45:16 +000067#define TC_GET_TARGET ip6tc_get_target
68#define TC_BUILTIN ip6tc_builtin
69#define TC_GET_POLICY ip6tc_get_policy
70#define TC_INSERT_ENTRY ip6tc_insert_entry
71#define TC_REPLACE_ENTRY ip6tc_replace_entry
72#define TC_APPEND_ENTRY ip6tc_append_entry
73#define TC_DELETE_ENTRY ip6tc_delete_entry
74#define TC_DELETE_NUM_ENTRY ip6tc_delete_num_entry
75#define TC_CHECK_PACKET ip6tc_check_packet
76#define TC_FLUSH_ENTRIES ip6tc_flush_entries
77#define TC_ZERO_ENTRIES ip6tc_zero_entries
78#define TC_CREATE_CHAIN ip6tc_create_chain
79#define TC_GET_REFERENCES ip6tc_get_references
80#define TC_DELETE_CHAIN ip6tc_delete_chain
81#define TC_RENAME_CHAIN ip6tc_rename_chain
82#define TC_SET_POLICY ip6tc_set_policy
83#define TC_GET_RAW_SOCKET ip6tc_get_raw_socket
84#define TC_INIT ip6tc_init
85#define TC_COMMIT ip6tc_commit
86#define TC_STRERROR ip6tc_strerror
87
88#define TC_AF AF_INET6
89#define TC_IPPROTO IPPROTO_IPV6
90
91#define SO_SET_REPLACE IP6T_SO_SET_REPLACE
92#define SO_SET_ADD_COUNTERS IP6T_SO_SET_ADD_COUNTERS
93#define SO_GET_INFO IP6T_SO_GET_INFO
94#define SO_GET_ENTRIES IP6T_SO_GET_ENTRIES
95#define SO_GET_VERSION IP6T_SO_GET_VERSION
96
97#define STANDARD_TARGET IP6T_STANDARD_TARGET
98#define LABEL_RETURN IP6TC_LABEL_RETURN
99#define LABEL_ACCEPT IP6TC_LABEL_ACCEPT
100#define LABEL_DROP IP6TC_LABEL_DROP
Philip Blundell88eb8352000-05-10 00:25:04 +0000101#define LABEL_QUEUE IP6TC_LABEL_QUEUE
Rusty Russell79dee072000-05-02 16:45:16 +0000102
103#define ALIGN IP6T_ALIGN
104#define RETURN IP6T_RETURN
105
106#include "libiptc.c"
107
108#define BIT6(a, l) \
109 (((a->in6_u.u6_addr32[(l) / 32]) >> ((l) & 31)) & 1)
110
111int
112ipv6_prefix_length(const struct in6_addr *a)
113{
114 int l, i;
115 for (l = 0; l < 128; l++) {
116 if (BIT6(a, l) == 0)
117 break;
118 }
119 for (i = l + 1; i < 128; i++) {
120 if (BIT6(a, i) == 1)
121 return -1;
122 }
123 return l;
124}
125
126static int
127dump_entry(struct ip6t_entry *e, const ip6tc_handle_t handle)
128{
129 size_t i;
130 char buf[40];
131 int len;
132 struct ip6t_entry_target *t;
133
134 printf("Entry %u (%lu):\n", entry2index(handle, e),
135 entry2offset(handle, e));
136 puts("SRC IP: ");
137 inet_ntop(AF_INET6, &e->ipv6.src, buf, sizeof buf);
138 puts(buf);
139 putchar('/');
140 len = ipv6_prefix_length(&e->ipv6.smsk);
141 if (len != -1)
142 printf("%d", len);
143 else {
144 inet_ntop(AF_INET6, &e->ipv6.smsk, buf, sizeof buf);
145 puts(buf);
146 }
147 putchar('\n');
148
149 puts("DST IP: ");
150 inet_ntop(AF_INET6, &e->ipv6.dst, buf, sizeof buf);
151 puts(buf);
152 putchar('/');
153 len = ipv6_prefix_length(&e->ipv6.dmsk);
154 if (len != -1)
155 printf("%d", len);
156 else {
157 inet_ntop(AF_INET6, &e->ipv6.dmsk, buf, sizeof buf);
158 puts(buf);
159 }
160 putchar('\n');
161
162 printf("Interface: `%s'/", e->ipv6.iniface);
163 for (i = 0; i < IFNAMSIZ; i++)
164 printf("%c", e->ipv6.iniface_mask[i] ? 'X' : '.');
165 printf("to `%s'/", e->ipv6.outiface);
166 for (i = 0; i < IFNAMSIZ; i++)
167 printf("%c", e->ipv6.outiface_mask[i] ? 'X' : '.');
168 printf("\nProtocol: %u\n", e->ipv6.proto);
169 if (e->ipv6.flags & IP6T_F_TOS)
170 printf("TOS: %u\n", e->ipv6.tos);
171 printf("Flags: %02X\n", e->ipv6.flags);
172 printf("Invflags: %02X\n", e->ipv6.invflags);
173 printf("Counters: %llu packets, %llu bytes\n",
174 e->counters.pcnt, e->counters.bcnt);
175 printf("Cache: %08X ", e->nfcache);
176 if (e->nfcache & NFC_ALTERED) printf("ALTERED ");
177 if (e->nfcache & NFC_UNKNOWN) printf("UNKNOWN ");
178 if (e->nfcache & NFC_IP6_SRC) printf("IP6_SRC ");
179 if (e->nfcache & NFC_IP6_DST) printf("IP6_DST ");
180 if (e->nfcache & NFC_IP6_IF_IN) printf("IP6_IF_IN ");
181 if (e->nfcache & NFC_IP6_IF_OUT) printf("IP6_IF_OUT ");
182 if (e->nfcache & NFC_IP6_TOS) printf("IP6_TOS ");
183 if (e->nfcache & NFC_IP6_PROTO) printf("IP6_PROTO ");
184 if (e->nfcache & NFC_IP6_OPTIONS) printf("IP6_OPTIONS ");
185 if (e->nfcache & NFC_IP6_TCPFLAGS) printf("IP6_TCPFLAGS ");
186 if (e->nfcache & NFC_IP6_SRC_PT) printf("IP6_SRC_PT ");
187 if (e->nfcache & NFC_IP6_DST_PT) printf("IP6_DST_PT ");
188 if (e->nfcache & NFC_IP6_PROTO_UNKNOWN) printf("IP6_PROTO_UNKNOWN ");
189 printf("\n");
190
191 IP6T_MATCH_ITERATE(e, print_match);
192
193 t = ip6t_get_target(e);
Philip Blundell88eb8352000-05-10 00:25:04 +0000194 printf("Target name: `%s' [%u]\n", t->u.user.name, t->u.target_size);
195 if (strcmp(t->u.user.name, IP6T_STANDARD_TARGET) == 0) {
Rusty Russell79dee072000-05-02 16:45:16 +0000196 int pos = *(int *)t->data;
197 if (pos < 0)
198 printf("verdict=%s\n",
199 pos == -NF_ACCEPT-1 ? "NF_ACCEPT"
200 : pos == -NF_DROP-1 ? "NF_DROP"
201 : pos == IP6T_RETURN ? "RETURN"
202 : "UNKNOWN");
203 else
204 printf("verdict=%u\n", pos);
Philip Blundell88eb8352000-05-10 00:25:04 +0000205 } else if (strcmp(t->u.user.name, IP6T_ERROR_TARGET) == 0)
Rusty Russell79dee072000-05-02 16:45:16 +0000206 printf("error=`%s'\n", t->data);
207
208 printf("\n");
209 return 0;
210}
211
Philip Blundell88eb8352000-05-10 00:25:04 +0000212static int
213is_same(const STRUCT_ENTRY *a, const STRUCT_ENTRY *b,
Rusty Russell79dee072000-05-02 16:45:16 +0000214 unsigned char *matchmask)
215{
216 unsigned int i;
Philip Blundell88eb8352000-05-10 00:25:04 +0000217 STRUCT_ENTRY_TARGET *ta, *tb;
Rusty Russell79dee072000-05-02 16:45:16 +0000218 unsigned char *mptr;
219
220 /* Always compare head structures: ignore mask here. */
221 if (memcmp(&a->ipv6.src, &b->ipv6.src, sizeof(struct in6_addr))
222 || memcmp(&a->ipv6.dst, &b->ipv6.dst, sizeof(struct in6_addr))
223 || memcmp(&a->ipv6.smsk, &b->ipv6.smsk, sizeof(struct in6_addr))
224 || memcmp(&a->ipv6.dmsk, &b->ipv6.dmsk, sizeof(struct in6_addr))
225 || a->ipv6.proto != b->ipv6.proto
226 || a->ipv6.tos != b->ipv6.tos
227 || a->ipv6.flags != b->ipv6.flags
228 || a->ipv6.invflags != b->ipv6.invflags)
229 return 0;
230
231 for (i = 0; i < IFNAMSIZ; i++) {
232 if (a->ipv6.iniface_mask[i] != b->ipv6.iniface_mask[i])
233 return 0;
234 if ((a->ipv6.iniface[i] & a->ipv6.iniface_mask[i])
235 != (b->ipv6.iniface[i] & b->ipv6.iniface_mask[i]))
236 return 0;
237 if (a->ipv6.outiface_mask[i] != b->ipv6.outiface_mask[i])
238 return 0;
239 if ((a->ipv6.outiface[i] & a->ipv6.outiface_mask[i])
240 != (b->ipv6.outiface[i] & b->ipv6.outiface_mask[i]))
241 return 0;
242 }
243
244 if (a->nfcache != b->nfcache
245 || a->target_offset != b->target_offset
246 || a->next_offset != b->next_offset)
247 return 0;
248
Philip Blundell88eb8352000-05-10 00:25:04 +0000249 mptr = matchmask + sizeof(STRUCT_ENTRY);
250 if (IP6T_MATCH_ITERATE(a, match_different, a->elems, b->elems, &mptr))
Rusty Russell79dee072000-05-02 16:45:16 +0000251 return 0;
252
Philip Blundell88eb8352000-05-10 00:25:04 +0000253 ta = GET_TARGET((STRUCT_ENTRY *)a);
254 tb = GET_TARGET((STRUCT_ENTRY *)b);
255 if (ta->u.target_size != tb->u.target_size)
Rusty Russell79dee072000-05-02 16:45:16 +0000256 return 0;
Philip Blundell88eb8352000-05-10 00:25:04 +0000257 if (strcmp(ta->u.user.name, tb->u.user.name) != 0)
Rusty Russell79dee072000-05-02 16:45:16 +0000258 return 0;
259 mptr += sizeof(*ta);
260
261 if (target_different(ta->data, tb->data,
262 ta->u.target_size - sizeof(*ta), mptr))
263 return 0;
264
265 return 1;
266}
Philip Blundell8c700902000-05-15 02:17:52 +0000267
268#ifndef NDEBUG
269/* Do every conceivable sanity check on the handle */
270static void
271do_check(TC_HANDLE_T h, unsigned int line)
272{
273 unsigned int i, n;
274 unsigned int user_offset; /* Offset of first user chain */
275 int was_return;
276
277 assert(h->changed == 0 || h->changed == 1);
278 if (strcmp(h->info.name, "filter") == 0) {
279 assert(h->info.valid_hooks
280 == (1 << NF_IP6_LOCAL_IN
281 | 1 << NF_IP6_FORWARD
282 | 1 << NF_IP6_LOCAL_OUT));
283
284 /* Hooks should be first three */
285 assert(h->info.hook_entry[NF_IP6_LOCAL_IN] == 0);
286
287 n = get_chain_end(h, 0);
288 n += get_entry(h, n)->next_offset;
289 assert(h->info.hook_entry[NF_IP6_FORWARD] == n);
290
291 n = get_chain_end(h, n);
292 n += get_entry(h, n)->next_offset;
293 assert(h->info.hook_entry[NF_IP6_LOCAL_OUT] == n);
294
295 user_offset = h->info.hook_entry[NF_IP6_LOCAL_OUT];
296 } else if (strcmp(h->info.name, "nat") == 0) {
297 assert(h->info.valid_hooks
298 == (1 << NF_IP6_PRE_ROUTING
299 | 1 << NF_IP6_POST_ROUTING
300 | 1 << NF_IP6_LOCAL_OUT));
301
302 assert(h->info.hook_entry[NF_IP6_PRE_ROUTING] == 0);
303
304 n = get_chain_end(h, 0);
305 n += get_entry(h, n)->next_offset;
306 assert(h->info.hook_entry[NF_IP6_POST_ROUTING] == n);
307
308 n = get_chain_end(h, n);
309 n += get_entry(h, n)->next_offset;
310 assert(h->info.hook_entry[NF_IP6_LOCAL_OUT] == n);
311
312 user_offset = h->info.hook_entry[NF_IP6_LOCAL_OUT];
313 } else if (strcmp(h->info.name, "mangle") == 0) {
314 assert(h->info.valid_hooks
315 == (1 << NF_IP6_PRE_ROUTING
316 | 1 << NF_IP6_LOCAL_OUT));
317
318 /* Hooks should be first three */
319 assert(h->info.hook_entry[NF_IP6_PRE_ROUTING] == 0);
320
321 n = get_chain_end(h, 0);
322 n += get_entry(h, n)->next_offset;
323 assert(h->info.hook_entry[NF_IP6_LOCAL_OUT] == n);
324
325 user_offset = h->info.hook_entry[NF_IP6_LOCAL_OUT];
326 } else
327 abort();
328
329 /* User chain == end of last builtin + policy entry */
330 user_offset = get_chain_end(h, user_offset);
331 user_offset += get_entry(h, user_offset)->next_offset;
332
333 /* Overflows should be end of entry chains, and unconditional
334 policy nodes. */
335 for (i = 0; i < NUMHOOKS; i++) {
336 STRUCT_ENTRY *e;
337 STRUCT_STANDARD_TARGET *t;
338
339 if (!(h->info.valid_hooks & (1 << i)))
340 continue;
341 assert(h->info.underflow[i]
342 == get_chain_end(h, h->info.hook_entry[i]));
343
344 e = get_entry(h, get_chain_end(h, h->info.hook_entry[i]));
345// assert(unconditional(&e->ipv6));
346 assert(e->target_offset == sizeof(*e));
347 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
348 assert(t->target.u.target_size == ALIGN(sizeof(*t)));
349 assert(e->next_offset == sizeof(*e) + ALIGN(sizeof(*t)));
350
351 assert(strcmp(t->target.u.user.name, STANDARD_TARGET)==0);
352 assert(t->verdict == -NF_DROP-1 || t->verdict == -NF_ACCEPT-1);
353
354 /* Hooks and underflows must be valid entries */
355 entry2index(h, get_entry(h, h->info.hook_entry[i]));
356 entry2index(h, get_entry(h, h->info.underflow[i]));
357 }
358
359 assert(h->info.size
360 >= h->info.num_entries * (sizeof(STRUCT_ENTRY)
361 +sizeof(STRUCT_STANDARD_TARGET)));
362
363 assert(h->entries.size
364 >= (h->new_number
365 * (sizeof(STRUCT_ENTRY)
366 + sizeof(STRUCT_STANDARD_TARGET))));
367 assert(strcmp(h->info.name, h->entries.name) == 0);
368
369 i = 0; n = 0;
370 was_return = 0;
371
372 /* Check all the entries. */
373// ENTRY_ITERATE(h->entries.entries, h->entries.size,
374// check_entry, &i, &n, user_offset, &was_return, h);
375
376 assert(i == h->new_number);
377 assert(n == h->entries.size);
378
379 /* Final entry must be error node */
380 assert(strcmp(GET_TARGET(index2entry(h, h->new_number-1))
381 ->u.user.name,
382 ERROR_TARGET) == 0);
383}
384#endif /*NDEBUG*/