blob: 31fbe07643a984c910678708ccedb1e26110f650 [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
Harald Welte1cef74d2001-01-05 15:22:59 +000078#define TC_ZERO_COUNTER ip6tc_zero_counter
79#define TC_READ_COUNTER ip6tc_read_counter
80#define TC_SET_COUNTER ip6tc_set_counter
Rusty Russell79dee072000-05-02 16:45:16 +000081#define TC_CREATE_CHAIN ip6tc_create_chain
82#define TC_GET_REFERENCES ip6tc_get_references
83#define TC_DELETE_CHAIN ip6tc_delete_chain
84#define TC_RENAME_CHAIN ip6tc_rename_chain
85#define TC_SET_POLICY ip6tc_set_policy
86#define TC_GET_RAW_SOCKET ip6tc_get_raw_socket
87#define TC_INIT ip6tc_init
88#define TC_COMMIT ip6tc_commit
89#define TC_STRERROR ip6tc_strerror
90
91#define TC_AF AF_INET6
92#define TC_IPPROTO IPPROTO_IPV6
93
94#define SO_SET_REPLACE IP6T_SO_SET_REPLACE
95#define SO_SET_ADD_COUNTERS IP6T_SO_SET_ADD_COUNTERS
96#define SO_GET_INFO IP6T_SO_GET_INFO
97#define SO_GET_ENTRIES IP6T_SO_GET_ENTRIES
98#define SO_GET_VERSION IP6T_SO_GET_VERSION
99
100#define STANDARD_TARGET IP6T_STANDARD_TARGET
101#define LABEL_RETURN IP6TC_LABEL_RETURN
102#define LABEL_ACCEPT IP6TC_LABEL_ACCEPT
103#define LABEL_DROP IP6TC_LABEL_DROP
Philip Blundell88eb8352000-05-10 00:25:04 +0000104#define LABEL_QUEUE IP6TC_LABEL_QUEUE
Rusty Russell79dee072000-05-02 16:45:16 +0000105
106#define ALIGN IP6T_ALIGN
107#define RETURN IP6T_RETURN
108
109#include "libiptc.c"
110
111#define BIT6(a, l) \
112 (((a->in6_u.u6_addr32[(l) / 32]) >> ((l) & 31)) & 1)
113
114int
115ipv6_prefix_length(const struct in6_addr *a)
116{
117 int l, i;
118 for (l = 0; l < 128; l++) {
119 if (BIT6(a, l) == 0)
120 break;
121 }
122 for (i = l + 1; i < 128; i++) {
123 if (BIT6(a, i) == 1)
124 return -1;
125 }
126 return l;
127}
128
129static int
130dump_entry(struct ip6t_entry *e, const ip6tc_handle_t handle)
131{
132 size_t i;
133 char buf[40];
134 int len;
135 struct ip6t_entry_target *t;
136
137 printf("Entry %u (%lu):\n", entry2index(handle, e),
138 entry2offset(handle, e));
139 puts("SRC IP: ");
140 inet_ntop(AF_INET6, &e->ipv6.src, buf, sizeof buf);
141 puts(buf);
142 putchar('/');
143 len = ipv6_prefix_length(&e->ipv6.smsk);
144 if (len != -1)
145 printf("%d", len);
146 else {
147 inet_ntop(AF_INET6, &e->ipv6.smsk, buf, sizeof buf);
148 puts(buf);
149 }
150 putchar('\n');
151
152 puts("DST IP: ");
153 inet_ntop(AF_INET6, &e->ipv6.dst, buf, sizeof buf);
154 puts(buf);
155 putchar('/');
156 len = ipv6_prefix_length(&e->ipv6.dmsk);
157 if (len != -1)
158 printf("%d", len);
159 else {
160 inet_ntop(AF_INET6, &e->ipv6.dmsk, buf, sizeof buf);
161 puts(buf);
162 }
163 putchar('\n');
164
165 printf("Interface: `%s'/", e->ipv6.iniface);
166 for (i = 0; i < IFNAMSIZ; i++)
167 printf("%c", e->ipv6.iniface_mask[i] ? 'X' : '.');
168 printf("to `%s'/", e->ipv6.outiface);
169 for (i = 0; i < IFNAMSIZ; i++)
170 printf("%c", e->ipv6.outiface_mask[i] ? 'X' : '.');
171 printf("\nProtocol: %u\n", e->ipv6.proto);
172 if (e->ipv6.flags & IP6T_F_TOS)
173 printf("TOS: %u\n", e->ipv6.tos);
174 printf("Flags: %02X\n", e->ipv6.flags);
175 printf("Invflags: %02X\n", e->ipv6.invflags);
176 printf("Counters: %llu packets, %llu bytes\n",
177 e->counters.pcnt, e->counters.bcnt);
178 printf("Cache: %08X ", e->nfcache);
179 if (e->nfcache & NFC_ALTERED) printf("ALTERED ");
180 if (e->nfcache & NFC_UNKNOWN) printf("UNKNOWN ");
181 if (e->nfcache & NFC_IP6_SRC) printf("IP6_SRC ");
182 if (e->nfcache & NFC_IP6_DST) printf("IP6_DST ");
183 if (e->nfcache & NFC_IP6_IF_IN) printf("IP6_IF_IN ");
184 if (e->nfcache & NFC_IP6_IF_OUT) printf("IP6_IF_OUT ");
185 if (e->nfcache & NFC_IP6_TOS) printf("IP6_TOS ");
186 if (e->nfcache & NFC_IP6_PROTO) printf("IP6_PROTO ");
187 if (e->nfcache & NFC_IP6_OPTIONS) printf("IP6_OPTIONS ");
188 if (e->nfcache & NFC_IP6_TCPFLAGS) printf("IP6_TCPFLAGS ");
189 if (e->nfcache & NFC_IP6_SRC_PT) printf("IP6_SRC_PT ");
190 if (e->nfcache & NFC_IP6_DST_PT) printf("IP6_DST_PT ");
191 if (e->nfcache & NFC_IP6_PROTO_UNKNOWN) printf("IP6_PROTO_UNKNOWN ");
192 printf("\n");
193
194 IP6T_MATCH_ITERATE(e, print_match);
195
196 t = ip6t_get_target(e);
Philip Blundell88eb8352000-05-10 00:25:04 +0000197 printf("Target name: `%s' [%u]\n", t->u.user.name, t->u.target_size);
198 if (strcmp(t->u.user.name, IP6T_STANDARD_TARGET) == 0) {
Rusty Russell79dee072000-05-02 16:45:16 +0000199 int pos = *(int *)t->data;
200 if (pos < 0)
201 printf("verdict=%s\n",
202 pos == -NF_ACCEPT-1 ? "NF_ACCEPT"
203 : pos == -NF_DROP-1 ? "NF_DROP"
204 : pos == IP6T_RETURN ? "RETURN"
205 : "UNKNOWN");
206 else
207 printf("verdict=%u\n", pos);
Philip Blundell88eb8352000-05-10 00:25:04 +0000208 } else if (strcmp(t->u.user.name, IP6T_ERROR_TARGET) == 0)
Rusty Russell79dee072000-05-02 16:45:16 +0000209 printf("error=`%s'\n", t->data);
210
211 printf("\n");
212 return 0;
213}
214
Philip Blundell88eb8352000-05-10 00:25:04 +0000215static int
216is_same(const STRUCT_ENTRY *a, const STRUCT_ENTRY *b,
Rusty Russell79dee072000-05-02 16:45:16 +0000217 unsigned char *matchmask)
218{
219 unsigned int i;
Philip Blundell88eb8352000-05-10 00:25:04 +0000220 STRUCT_ENTRY_TARGET *ta, *tb;
Rusty Russell79dee072000-05-02 16:45:16 +0000221 unsigned char *mptr;
222
223 /* Always compare head structures: ignore mask here. */
224 if (memcmp(&a->ipv6.src, &b->ipv6.src, sizeof(struct in6_addr))
225 || memcmp(&a->ipv6.dst, &b->ipv6.dst, sizeof(struct in6_addr))
226 || memcmp(&a->ipv6.smsk, &b->ipv6.smsk, sizeof(struct in6_addr))
227 || memcmp(&a->ipv6.dmsk, &b->ipv6.dmsk, sizeof(struct in6_addr))
228 || a->ipv6.proto != b->ipv6.proto
229 || a->ipv6.tos != b->ipv6.tos
230 || a->ipv6.flags != b->ipv6.flags
231 || a->ipv6.invflags != b->ipv6.invflags)
232 return 0;
233
234 for (i = 0; i < IFNAMSIZ; i++) {
235 if (a->ipv6.iniface_mask[i] != b->ipv6.iniface_mask[i])
236 return 0;
237 if ((a->ipv6.iniface[i] & a->ipv6.iniface_mask[i])
238 != (b->ipv6.iniface[i] & b->ipv6.iniface_mask[i]))
239 return 0;
240 if (a->ipv6.outiface_mask[i] != b->ipv6.outiface_mask[i])
241 return 0;
242 if ((a->ipv6.outiface[i] & a->ipv6.outiface_mask[i])
243 != (b->ipv6.outiface[i] & b->ipv6.outiface_mask[i]))
244 return 0;
245 }
246
247 if (a->nfcache != b->nfcache
248 || a->target_offset != b->target_offset
249 || a->next_offset != b->next_offset)
250 return 0;
251
Philip Blundell88eb8352000-05-10 00:25:04 +0000252 mptr = matchmask + sizeof(STRUCT_ENTRY);
253 if (IP6T_MATCH_ITERATE(a, match_different, a->elems, b->elems, &mptr))
Rusty Russell79dee072000-05-02 16:45:16 +0000254 return 0;
255
Philip Blundell88eb8352000-05-10 00:25:04 +0000256 ta = GET_TARGET((STRUCT_ENTRY *)a);
257 tb = GET_TARGET((STRUCT_ENTRY *)b);
258 if (ta->u.target_size != tb->u.target_size)
Rusty Russell79dee072000-05-02 16:45:16 +0000259 return 0;
Philip Blundell88eb8352000-05-10 00:25:04 +0000260 if (strcmp(ta->u.user.name, tb->u.user.name) != 0)
Rusty Russell79dee072000-05-02 16:45:16 +0000261 return 0;
262 mptr += sizeof(*ta);
263
264 if (target_different(ta->data, tb->data,
265 ta->u.target_size - sizeof(*ta), mptr))
266 return 0;
267
268 return 1;
269}
Philip Blundell8c700902000-05-15 02:17:52 +0000270
Rusty Russell5eed48a2000-06-02 20:12:24 +0000271/* All zeroes == unconditional rule. */
272static inline int
273unconditional(const struct ip6t_ip6 *ipv6)
274{
275 unsigned int i;
276
277 for (i = 0; i < sizeof(*ipv6); i++)
278 if (((char *)ipv6)[i])
279 break;
280
281 return (i == sizeof(*ipv6));
282}
283
Philip Blundell8c700902000-05-15 02:17:52 +0000284#ifndef NDEBUG
285/* Do every conceivable sanity check on the handle */
286static void
287do_check(TC_HANDLE_T h, unsigned int line)
288{
289 unsigned int i, n;
290 unsigned int user_offset; /* Offset of first user chain */
291 int was_return;
292
293 assert(h->changed == 0 || h->changed == 1);
294 if (strcmp(h->info.name, "filter") == 0) {
295 assert(h->info.valid_hooks
296 == (1 << NF_IP6_LOCAL_IN
297 | 1 << NF_IP6_FORWARD
298 | 1 << NF_IP6_LOCAL_OUT));
299
300 /* Hooks should be first three */
301 assert(h->info.hook_entry[NF_IP6_LOCAL_IN] == 0);
302
303 n = get_chain_end(h, 0);
304 n += get_entry(h, n)->next_offset;
305 assert(h->info.hook_entry[NF_IP6_FORWARD] == n);
306
307 n = get_chain_end(h, n);
308 n += get_entry(h, n)->next_offset;
309 assert(h->info.hook_entry[NF_IP6_LOCAL_OUT] == n);
310
311 user_offset = h->info.hook_entry[NF_IP6_LOCAL_OUT];
312 } else if (strcmp(h->info.name, "nat") == 0) {
313 assert(h->info.valid_hooks
314 == (1 << NF_IP6_PRE_ROUTING
315 | 1 << NF_IP6_POST_ROUTING
316 | 1 << NF_IP6_LOCAL_OUT));
317
318 assert(h->info.hook_entry[NF_IP6_PRE_ROUTING] == 0);
319
320 n = get_chain_end(h, 0);
321 n += get_entry(h, n)->next_offset;
322 assert(h->info.hook_entry[NF_IP6_POST_ROUTING] == n);
323
324 n = get_chain_end(h, n);
325 n += get_entry(h, n)->next_offset;
326 assert(h->info.hook_entry[NF_IP6_LOCAL_OUT] == n);
327
328 user_offset = h->info.hook_entry[NF_IP6_LOCAL_OUT];
329 } else if (strcmp(h->info.name, "mangle") == 0) {
330 assert(h->info.valid_hooks
331 == (1 << NF_IP6_PRE_ROUTING
332 | 1 << NF_IP6_LOCAL_OUT));
333
334 /* Hooks should be first three */
335 assert(h->info.hook_entry[NF_IP6_PRE_ROUTING] == 0);
336
337 n = get_chain_end(h, 0);
338 n += get_entry(h, n)->next_offset;
339 assert(h->info.hook_entry[NF_IP6_LOCAL_OUT] == n);
340
341 user_offset = h->info.hook_entry[NF_IP6_LOCAL_OUT];
342 } else
343 abort();
344
345 /* User chain == end of last builtin + policy entry */
346 user_offset = get_chain_end(h, user_offset);
347 user_offset += get_entry(h, user_offset)->next_offset;
348
349 /* Overflows should be end of entry chains, and unconditional
350 policy nodes. */
351 for (i = 0; i < NUMHOOKS; i++) {
352 STRUCT_ENTRY *e;
353 STRUCT_STANDARD_TARGET *t;
354
355 if (!(h->info.valid_hooks & (1 << i)))
356 continue;
357 assert(h->info.underflow[i]
358 == get_chain_end(h, h->info.hook_entry[i]));
359
360 e = get_entry(h, get_chain_end(h, h->info.hook_entry[i]));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000361 assert(unconditional(&e->ipv6));
Philip Blundell8c700902000-05-15 02:17:52 +0000362 assert(e->target_offset == sizeof(*e));
363 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
364 assert(t->target.u.target_size == ALIGN(sizeof(*t)));
365 assert(e->next_offset == sizeof(*e) + ALIGN(sizeof(*t)));
366
367 assert(strcmp(t->target.u.user.name, STANDARD_TARGET)==0);
368 assert(t->verdict == -NF_DROP-1 || t->verdict == -NF_ACCEPT-1);
369
370 /* Hooks and underflows must be valid entries */
371 entry2index(h, get_entry(h, h->info.hook_entry[i]));
372 entry2index(h, get_entry(h, h->info.underflow[i]));
373 }
374
375 assert(h->info.size
376 >= h->info.num_entries * (sizeof(STRUCT_ENTRY)
377 +sizeof(STRUCT_STANDARD_TARGET)));
378
379 assert(h->entries.size
380 >= (h->new_number
381 * (sizeof(STRUCT_ENTRY)
382 + sizeof(STRUCT_STANDARD_TARGET))));
383 assert(strcmp(h->info.name, h->entries.name) == 0);
384
385 i = 0; n = 0;
386 was_return = 0;
387
Rusty Russell5eed48a2000-06-02 20:12:24 +0000388#if 0
Philip Blundell8c700902000-05-15 02:17:52 +0000389 /* Check all the entries. */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000390 ENTRY_ITERATE(h->entries.entries, h->entries.size,
391 check_entry, &i, &n, user_offset, &was_return, h);
Philip Blundell8c700902000-05-15 02:17:52 +0000392
393 assert(i == h->new_number);
394 assert(n == h->entries.size);
395
396 /* Final entry must be error node */
397 assert(strcmp(GET_TARGET(index2entry(h, h->new_number-1))
398 ->u.user.name,
399 ERROR_TARGET) == 0);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000400#endif
Philip Blundell8c700902000-05-15 02:17:52 +0000401}
402#endif /*NDEBUG*/