blob: cd35bbe95fe5d8c4d76d79880d84343096d9f821 [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>
Marc Boucher1afc3b62002-01-19 12:46:39 +000019#include <unistd.h>
Rusty Russell79dee072000-05-02 16:45:16 +000020#include <arpa/inet.h>
21
22#ifdef DEBUG_CONNTRACK
23#define inline
24#endif
25
26#if !defined(__GLIBC__) || (__GLIBC__ < 2)
27typedef unsigned int socklen_t;
28#endif
29
30#include "libiptc/libip6tc.h"
31
32#define HOOK_PRE_ROUTING NF_IP6_PRE_ROUTING
33#define HOOK_LOCAL_IN NF_IP6_LOCAL_IN
34#define HOOK_FORWARD NF_IP6_FORWARD
35#define HOOK_LOCAL_OUT NF_IP6_LOCAL_OUT
36#define HOOK_POST_ROUTING NF_IP6_POST_ROUTING
37
38#define STRUCT_ENTRY_TARGET struct ip6t_entry_target
39#define STRUCT_ENTRY struct ip6t_entry
40#define STRUCT_ENTRY_MATCH struct ip6t_entry_match
41#define STRUCT_GETINFO struct ip6t_getinfo
42#define STRUCT_GET_ENTRIES struct ip6t_get_entries
43#define STRUCT_COUNTERS struct ip6t_counters
44#define STRUCT_COUNTERS_INFO struct ip6t_counters_info
45#define STRUCT_STANDARD_TARGET struct ip6t_standard_target
46#define STRUCT_REPLACE struct ip6t_replace
47
48#define STRUCT_TC_HANDLE struct ip6tc_handle
49#define TC_HANDLE_T ip6tc_handle_t
50
51#define ENTRY_ITERATE IP6T_ENTRY_ITERATE
52#define TABLE_MAXNAMELEN IP6T_TABLE_MAXNAMELEN
53#define FUNCTION_MAXNAMELEN IP6T_FUNCTION_MAXNAMELEN
54
55#define GET_TARGET ip6t_get_target
56
57#define ERROR_TARGET IP6T_ERROR_TARGET
58#define NUMHOOKS NF_IP6_NUMHOOKS
59
60#define IPT_CHAINLABEL ip6t_chainlabel
61
62#define TC_DUMP_ENTRIES dump_entries6
63#define TC_IS_CHAIN ip6tc_is_chain
Philip Blundell8c700902000-05-15 02:17:52 +000064#define TC_FIRST_CHAIN ip6tc_first_chain
Rusty Russell79dee072000-05-02 16:45:16 +000065#define TC_NEXT_CHAIN ip6tc_next_chain
Philip Blundell8c700902000-05-15 02:17:52 +000066#define TC_FIRST_RULE ip6tc_first_rule
67#define TC_NEXT_RULE ip6tc_next_rule
Rusty Russell79dee072000-05-02 16:45:16 +000068#define TC_GET_TARGET ip6tc_get_target
69#define TC_BUILTIN ip6tc_builtin
70#define TC_GET_POLICY ip6tc_get_policy
71#define TC_INSERT_ENTRY ip6tc_insert_entry
72#define TC_REPLACE_ENTRY ip6tc_replace_entry
73#define TC_APPEND_ENTRY ip6tc_append_entry
74#define TC_DELETE_ENTRY ip6tc_delete_entry
75#define TC_DELETE_NUM_ENTRY ip6tc_delete_num_entry
76#define TC_CHECK_PACKET ip6tc_check_packet
77#define TC_FLUSH_ENTRIES ip6tc_flush_entries
78#define TC_ZERO_ENTRIES ip6tc_zero_entries
Harald Welte1cef74d2001-01-05 15:22:59 +000079#define TC_ZERO_COUNTER ip6tc_zero_counter
80#define TC_READ_COUNTER ip6tc_read_counter
81#define TC_SET_COUNTER ip6tc_set_counter
Rusty Russell79dee072000-05-02 16:45:16 +000082#define TC_CREATE_CHAIN ip6tc_create_chain
83#define TC_GET_REFERENCES ip6tc_get_references
84#define TC_DELETE_CHAIN ip6tc_delete_chain
85#define TC_RENAME_CHAIN ip6tc_rename_chain
86#define TC_SET_POLICY ip6tc_set_policy
87#define TC_GET_RAW_SOCKET ip6tc_get_raw_socket
88#define TC_INIT ip6tc_init
89#define TC_COMMIT ip6tc_commit
90#define TC_STRERROR ip6tc_strerror
91
92#define TC_AF AF_INET6
93#define TC_IPPROTO IPPROTO_IPV6
94
95#define SO_SET_REPLACE IP6T_SO_SET_REPLACE
96#define SO_SET_ADD_COUNTERS IP6T_SO_SET_ADD_COUNTERS
97#define SO_GET_INFO IP6T_SO_GET_INFO
98#define SO_GET_ENTRIES IP6T_SO_GET_ENTRIES
99#define SO_GET_VERSION IP6T_SO_GET_VERSION
100
101#define STANDARD_TARGET IP6T_STANDARD_TARGET
102#define LABEL_RETURN IP6TC_LABEL_RETURN
103#define LABEL_ACCEPT IP6TC_LABEL_ACCEPT
104#define LABEL_DROP IP6TC_LABEL_DROP
Philip Blundell88eb8352000-05-10 00:25:04 +0000105#define LABEL_QUEUE IP6TC_LABEL_QUEUE
Rusty Russell79dee072000-05-02 16:45:16 +0000106
107#define ALIGN IP6T_ALIGN
108#define RETURN IP6T_RETURN
109
110#include "libiptc.c"
111
112#define BIT6(a, l) \
113 (((a->in6_u.u6_addr32[(l) / 32]) >> ((l) & 31)) & 1)
114
115int
116ipv6_prefix_length(const struct in6_addr *a)
117{
118 int l, i;
119 for (l = 0; l < 128; l++) {
120 if (BIT6(a, l) == 0)
121 break;
122 }
123 for (i = l + 1; i < 128; i++) {
124 if (BIT6(a, i) == 1)
125 return -1;
126 }
127 return l;
128}
129
130static int
131dump_entry(struct ip6t_entry *e, const ip6tc_handle_t handle)
132{
133 size_t i;
134 char buf[40];
135 int len;
136 struct ip6t_entry_target *t;
137
138 printf("Entry %u (%lu):\n", entry2index(handle, e),
139 entry2offset(handle, e));
140 puts("SRC IP: ");
141 inet_ntop(AF_INET6, &e->ipv6.src, buf, sizeof buf);
142 puts(buf);
143 putchar('/');
144 len = ipv6_prefix_length(&e->ipv6.smsk);
145 if (len != -1)
146 printf("%d", len);
147 else {
148 inet_ntop(AF_INET6, &e->ipv6.smsk, buf, sizeof buf);
149 puts(buf);
150 }
151 putchar('\n');
152
153 puts("DST IP: ");
154 inet_ntop(AF_INET6, &e->ipv6.dst, buf, sizeof buf);
155 puts(buf);
156 putchar('/');
157 len = ipv6_prefix_length(&e->ipv6.dmsk);
158 if (len != -1)
159 printf("%d", len);
160 else {
161 inet_ntop(AF_INET6, &e->ipv6.dmsk, buf, sizeof buf);
162 puts(buf);
163 }
164 putchar('\n');
165
166 printf("Interface: `%s'/", e->ipv6.iniface);
167 for (i = 0; i < IFNAMSIZ; i++)
168 printf("%c", e->ipv6.iniface_mask[i] ? 'X' : '.');
169 printf("to `%s'/", e->ipv6.outiface);
170 for (i = 0; i < IFNAMSIZ; i++)
171 printf("%c", e->ipv6.outiface_mask[i] ? 'X' : '.');
172 printf("\nProtocol: %u\n", e->ipv6.proto);
173 if (e->ipv6.flags & IP6T_F_TOS)
174 printf("TOS: %u\n", e->ipv6.tos);
175 printf("Flags: %02X\n", e->ipv6.flags);
176 printf("Invflags: %02X\n", e->ipv6.invflags);
177 printf("Counters: %llu packets, %llu bytes\n",
178 e->counters.pcnt, e->counters.bcnt);
179 printf("Cache: %08X ", e->nfcache);
180 if (e->nfcache & NFC_ALTERED) printf("ALTERED ");
181 if (e->nfcache & NFC_UNKNOWN) printf("UNKNOWN ");
182 if (e->nfcache & NFC_IP6_SRC) printf("IP6_SRC ");
183 if (e->nfcache & NFC_IP6_DST) printf("IP6_DST ");
184 if (e->nfcache & NFC_IP6_IF_IN) printf("IP6_IF_IN ");
185 if (e->nfcache & NFC_IP6_IF_OUT) printf("IP6_IF_OUT ");
186 if (e->nfcache & NFC_IP6_TOS) printf("IP6_TOS ");
187 if (e->nfcache & NFC_IP6_PROTO) printf("IP6_PROTO ");
188 if (e->nfcache & NFC_IP6_OPTIONS) printf("IP6_OPTIONS ");
189 if (e->nfcache & NFC_IP6_TCPFLAGS) printf("IP6_TCPFLAGS ");
190 if (e->nfcache & NFC_IP6_SRC_PT) printf("IP6_SRC_PT ");
191 if (e->nfcache & NFC_IP6_DST_PT) printf("IP6_DST_PT ");
192 if (e->nfcache & NFC_IP6_PROTO_UNKNOWN) printf("IP6_PROTO_UNKNOWN ");
193 printf("\n");
194
195 IP6T_MATCH_ITERATE(e, print_match);
196
197 t = ip6t_get_target(e);
Philip Blundell88eb8352000-05-10 00:25:04 +0000198 printf("Target name: `%s' [%u]\n", t->u.user.name, t->u.target_size);
199 if (strcmp(t->u.user.name, IP6T_STANDARD_TARGET) == 0) {
Rusty Russell79dee072000-05-02 16:45:16 +0000200 int pos = *(int *)t->data;
201 if (pos < 0)
202 printf("verdict=%s\n",
203 pos == -NF_ACCEPT-1 ? "NF_ACCEPT"
204 : pos == -NF_DROP-1 ? "NF_DROP"
205 : pos == IP6T_RETURN ? "RETURN"
206 : "UNKNOWN");
207 else
208 printf("verdict=%u\n", pos);
Philip Blundell88eb8352000-05-10 00:25:04 +0000209 } else if (strcmp(t->u.user.name, IP6T_ERROR_TARGET) == 0)
Rusty Russell79dee072000-05-02 16:45:16 +0000210 printf("error=`%s'\n", t->data);
211
212 printf("\n");
213 return 0;
214}
215
Philip Blundell88eb8352000-05-10 00:25:04 +0000216static int
217is_same(const STRUCT_ENTRY *a, const STRUCT_ENTRY *b,
Rusty Russell79dee072000-05-02 16:45:16 +0000218 unsigned char *matchmask)
219{
220 unsigned int i;
Philip Blundell88eb8352000-05-10 00:25:04 +0000221 STRUCT_ENTRY_TARGET *ta, *tb;
Rusty Russell79dee072000-05-02 16:45:16 +0000222 unsigned char *mptr;
223
224 /* Always compare head structures: ignore mask here. */
225 if (memcmp(&a->ipv6.src, &b->ipv6.src, sizeof(struct in6_addr))
226 || memcmp(&a->ipv6.dst, &b->ipv6.dst, sizeof(struct in6_addr))
227 || memcmp(&a->ipv6.smsk, &b->ipv6.smsk, sizeof(struct in6_addr))
228 || memcmp(&a->ipv6.dmsk, &b->ipv6.dmsk, sizeof(struct in6_addr))
229 || a->ipv6.proto != b->ipv6.proto
230 || a->ipv6.tos != b->ipv6.tos
231 || a->ipv6.flags != b->ipv6.flags
232 || a->ipv6.invflags != b->ipv6.invflags)
233 return 0;
234
235 for (i = 0; i < IFNAMSIZ; i++) {
236 if (a->ipv6.iniface_mask[i] != b->ipv6.iniface_mask[i])
237 return 0;
238 if ((a->ipv6.iniface[i] & a->ipv6.iniface_mask[i])
239 != (b->ipv6.iniface[i] & b->ipv6.iniface_mask[i]))
240 return 0;
241 if (a->ipv6.outiface_mask[i] != b->ipv6.outiface_mask[i])
242 return 0;
243 if ((a->ipv6.outiface[i] & a->ipv6.outiface_mask[i])
244 != (b->ipv6.outiface[i] & b->ipv6.outiface_mask[i]))
245 return 0;
246 }
247
248 if (a->nfcache != b->nfcache
249 || a->target_offset != b->target_offset
250 || a->next_offset != b->next_offset)
251 return 0;
252
Philip Blundell88eb8352000-05-10 00:25:04 +0000253 mptr = matchmask + sizeof(STRUCT_ENTRY);
254 if (IP6T_MATCH_ITERATE(a, match_different, a->elems, b->elems, &mptr))
Rusty Russell79dee072000-05-02 16:45:16 +0000255 return 0;
256
Philip Blundell88eb8352000-05-10 00:25:04 +0000257 ta = GET_TARGET((STRUCT_ENTRY *)a);
258 tb = GET_TARGET((STRUCT_ENTRY *)b);
259 if (ta->u.target_size != tb->u.target_size)
Rusty Russell79dee072000-05-02 16:45:16 +0000260 return 0;
Philip Blundell88eb8352000-05-10 00:25:04 +0000261 if (strcmp(ta->u.user.name, tb->u.user.name) != 0)
Rusty Russell79dee072000-05-02 16:45:16 +0000262 return 0;
263 mptr += sizeof(*ta);
264
265 if (target_different(ta->data, tb->data,
266 ta->u.target_size - sizeof(*ta), mptr))
267 return 0;
268
269 return 1;
270}
Philip Blundell8c700902000-05-15 02:17:52 +0000271
Rusty Russell5eed48a2000-06-02 20:12:24 +0000272/* All zeroes == unconditional rule. */
273static inline int
274unconditional(const struct ip6t_ip6 *ipv6)
275{
276 unsigned int i;
277
278 for (i = 0; i < sizeof(*ipv6); i++)
279 if (((char *)ipv6)[i])
280 break;
281
282 return (i == sizeof(*ipv6));
283}
284
Harald Welte380ba5f2002-02-13 16:19:55 +0000285#ifdef IPTC_DEBUG
Philip Blundell8c700902000-05-15 02:17:52 +0000286/* Do every conceivable sanity check on the handle */
287static void
288do_check(TC_HANDLE_T h, unsigned int line)
289{
290 unsigned int i, n;
291 unsigned int user_offset; /* Offset of first user chain */
292 int was_return;
293
294 assert(h->changed == 0 || h->changed == 1);
295 if (strcmp(h->info.name, "filter") == 0) {
296 assert(h->info.valid_hooks
297 == (1 << NF_IP6_LOCAL_IN
298 | 1 << NF_IP6_FORWARD
299 | 1 << NF_IP6_LOCAL_OUT));
300
301 /* Hooks should be first three */
302 assert(h->info.hook_entry[NF_IP6_LOCAL_IN] == 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_FORWARD] == 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, "nat") == 0) {
314 assert(h->info.valid_hooks
315 == (1 << NF_IP6_PRE_ROUTING
316 | 1 << NF_IP6_POST_ROUTING
317 | 1 << NF_IP6_LOCAL_OUT));
318
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_POST_ROUTING] == n);
324
325 n = get_chain_end(h, n);
326 n += get_entry(h, n)->next_offset;
327 assert(h->info.hook_entry[NF_IP6_LOCAL_OUT] == n);
328
329 user_offset = h->info.hook_entry[NF_IP6_LOCAL_OUT];
330 } else if (strcmp(h->info.name, "mangle") == 0) {
Harald Welte596707c2002-02-13 16:35:39 +0000331 /* This code is getting ugly because linux < 2.4.18-pre6 had
332 * two mangle hooks, linux >= 2.4.18-pre6 has five mangle hooks
333 * */
334 assert((h->info.valid_hooks &
335 ~(1 << NF_IP6_LOCAL_IN
336 | 1 << NF_IP6_FORWARD
337 | 1 << NF_IP6_POST_ROUTING))
Philip Blundell8c700902000-05-15 02:17:52 +0000338 == (1 << NF_IP6_PRE_ROUTING
Harald Welte596707c2002-02-13 16:35:39 +0000339 | 1 << NF_IP6_LOCAL_OUT));
Philip Blundell8c700902000-05-15 02:17:52 +0000340
Harald Welte380ba5f2002-02-13 16:19:55 +0000341 /* Hooks should be first five */
Philip Blundell8c700902000-05-15 02:17:52 +0000342 assert(h->info.hook_entry[NF_IP6_PRE_ROUTING] == 0);
343
344 n = get_chain_end(h, 0);
Harald Welte380ba5f2002-02-13 16:19:55 +0000345
Harald Welte596707c2002-02-13 16:35:39 +0000346 if (h->info.valid_hooks & NF_IP6_LOCAL_IN) {
347 n += get_entry(h, n)->next_offset;
348 assert(h->info.hook_entry[NF_IP6_LOCAL_IN] == n);
349 n = get_chain_end(h, n);
350 }
Harald Welte380ba5f2002-02-13 16:19:55 +0000351
Harald Welte596707c2002-02-13 16:35:39 +0000352 if (h->info.valid_hooks & NF_IP6_FORWARD) {
353 n += get_entry(h, n)->next_offset;
354 assert(h->info.hook_entry[NF_IP6_FORWARD] == n);
355 n = get_chain_end(h, n);
356 }
357
Harald Welte380ba5f2002-02-13 16:19:55 +0000358 n += get_entry(h, n)->next_offset;
Philip Blundell8c700902000-05-15 02:17:52 +0000359 assert(h->info.hook_entry[NF_IP6_LOCAL_OUT] == n);
Harald Welte596707c2002-02-13 16:35:39 +0000360 user_offset = h->info.hook_entry[NF_IP6_LOCAL_OUT];
Philip Blundell8c700902000-05-15 02:17:52 +0000361
Harald Welte596707c2002-02-13 16:35:39 +0000362 if (h->info.valid_hooks & NF_IP6_POST_ROUTING) {
363 n = get_chain_end(h, n);
364 n += get_entry(h, n)->next_offset;
365 assert(h->info.hook_entry[NF_IP6_POST_ROUTING] == n);
366 user_offset = h->info.hook_entry[NF_IP6_POST_ROUTING];
367 }
Philip Blundell8c700902000-05-15 02:17:52 +0000368 } else
369 abort();
370
371 /* User chain == end of last builtin + policy entry */
372 user_offset = get_chain_end(h, user_offset);
373 user_offset += get_entry(h, user_offset)->next_offset;
374
375 /* Overflows should be end of entry chains, and unconditional
376 policy nodes. */
377 for (i = 0; i < NUMHOOKS; i++) {
378 STRUCT_ENTRY *e;
379 STRUCT_STANDARD_TARGET *t;
380
381 if (!(h->info.valid_hooks & (1 << i)))
382 continue;
383 assert(h->info.underflow[i]
384 == get_chain_end(h, h->info.hook_entry[i]));
385
386 e = get_entry(h, get_chain_end(h, h->info.hook_entry[i]));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000387 assert(unconditional(&e->ipv6));
Philip Blundell8c700902000-05-15 02:17:52 +0000388 assert(e->target_offset == sizeof(*e));
389 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
390 assert(t->target.u.target_size == ALIGN(sizeof(*t)));
391 assert(e->next_offset == sizeof(*e) + ALIGN(sizeof(*t)));
392
393 assert(strcmp(t->target.u.user.name, STANDARD_TARGET)==0);
394 assert(t->verdict == -NF_DROP-1 || t->verdict == -NF_ACCEPT-1);
395
396 /* Hooks and underflows must be valid entries */
397 entry2index(h, get_entry(h, h->info.hook_entry[i]));
398 entry2index(h, get_entry(h, h->info.underflow[i]));
399 }
400
401 assert(h->info.size
402 >= h->info.num_entries * (sizeof(STRUCT_ENTRY)
403 +sizeof(STRUCT_STANDARD_TARGET)));
404
405 assert(h->entries.size
406 >= (h->new_number
407 * (sizeof(STRUCT_ENTRY)
408 + sizeof(STRUCT_STANDARD_TARGET))));
409 assert(strcmp(h->info.name, h->entries.name) == 0);
410
411 i = 0; n = 0;
412 was_return = 0;
413
Rusty Russell5eed48a2000-06-02 20:12:24 +0000414#if 0
Philip Blundell8c700902000-05-15 02:17:52 +0000415 /* Check all the entries. */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000416 ENTRY_ITERATE(h->entries.entries, h->entries.size,
417 check_entry, &i, &n, user_offset, &was_return, h);
Philip Blundell8c700902000-05-15 02:17:52 +0000418
419 assert(i == h->new_number);
420 assert(n == h->entries.size);
421
422 /* Final entry must be error node */
423 assert(strcmp(GET_TARGET(index2entry(h, h->new_number-1))
424 ->u.user.name,
425 ERROR_TARGET) == 0);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000426#endif
Philip Blundell8c700902000-05-15 02:17:52 +0000427}
Harald Welte380ba5f2002-02-13 16:19:55 +0000428#endif /*IPTC_DEBUG*/