This commit was manufactured by cvs2svn to create branch
[iptables.git] / libiptc2 / libip6tc.c
1 /* 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 <unistd.h>
20 #include <arpa/inet.h>
21
22 #ifdef DEBUG_CONNTRACK
23 #define inline
24 #endif
25
26 #if !defined(__GLIBC__) || (__GLIBC__ < 2)
27 typedef 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
64 #define TC_FIRST_CHAIN          ip6tc_first_chain
65 #define TC_NEXT_CHAIN           ip6tc_next_chain
66 #define TC_FIRST_RULE           ip6tc_first_rule
67 #define TC_NEXT_RULE            ip6tc_next_rule
68 #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
79 #define TC_ZERO_COUNTER         ip6tc_zero_counter
80 #define TC_READ_COUNTER         ip6tc_read_counter
81 #define TC_SET_COUNTER          ip6tc_set_counter
82 #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_FREE                 ip6tc_free
90 #define TC_COMMIT               ip6tc_commit
91 #define TC_STRERROR             ip6tc_strerror
92
93 #define TC_AF                   AF_INET6
94 #define TC_IPPROTO              IPPROTO_IPV6
95
96 #define SO_SET_REPLACE          IP6T_SO_SET_REPLACE
97 #define SO_SET_ADD_COUNTERS     IP6T_SO_SET_ADD_COUNTERS
98 #define SO_GET_INFO             IP6T_SO_GET_INFO
99 #define SO_GET_ENTRIES          IP6T_SO_GET_ENTRIES
100 #define SO_GET_VERSION          IP6T_SO_GET_VERSION
101
102 #define STANDARD_TARGET         IP6T_STANDARD_TARGET
103 #define LABEL_RETURN            IP6TC_LABEL_RETURN
104 #define LABEL_ACCEPT            IP6TC_LABEL_ACCEPT
105 #define LABEL_DROP              IP6TC_LABEL_DROP
106 #define LABEL_QUEUE             IP6TC_LABEL_QUEUE
107
108 #define ALIGN                   IP6T_ALIGN
109 #define RETURN                  IP6T_RETURN
110
111 #include "libiptc.c"
112
113 #define BIT6(a, l) \
114  ((ntohl(a->in6_u.u6_addr32[(l) / 32]) >> (31 - ((l) & 31))) & 1)
115
116 int
117 ipv6_prefix_length(const struct in6_addr *a)
118 {
119         int l, i;
120         for (l = 0; l < 128; l++) {
121                 if (BIT6(a, l) == 0)
122                         break;
123         }
124         for (i = l + 1; i < 128; i++) {
125                 if (BIT6(a, i) == 1)
126                         return -1;
127         }
128         return l;
129 }
130
131 static int
132 dump_entry(struct ip6t_entry *e, const ip6tc_handle_t handle)
133 {
134         size_t i;
135         char buf[40];
136         int len;
137         struct ip6t_entry_target *t;
138         
139         printf("Entry %u (%lu):\n", entry2index(handle, e),
140                entry2offset(handle, e));
141         puts("SRC IP: ");
142         inet_ntop(AF_INET6, &e->ipv6.src, buf, sizeof buf);
143         puts(buf);
144         putchar('/');
145         len = ipv6_prefix_length(&e->ipv6.smsk);
146         if (len != -1)
147                 printf("%d", len);
148         else {
149                 inet_ntop(AF_INET6, &e->ipv6.smsk, buf, sizeof buf);
150                 puts(buf);
151         }
152         putchar('\n');
153         
154         puts("DST IP: ");
155         inet_ntop(AF_INET6, &e->ipv6.dst, buf, sizeof buf);
156         puts(buf);
157         putchar('/');
158         len = ipv6_prefix_length(&e->ipv6.dmsk);
159         if (len != -1)
160                 printf("%d", len);
161         else {
162                 inet_ntop(AF_INET6, &e->ipv6.dmsk, buf, sizeof buf);
163                 puts(buf);
164         }
165         putchar('\n');
166         
167         printf("Interface: `%s'/", e->ipv6.iniface);
168         for (i = 0; i < IFNAMSIZ; i++)
169                 printf("%c", e->ipv6.iniface_mask[i] ? 'X' : '.');
170         printf("to `%s'/", e->ipv6.outiface);
171         for (i = 0; i < IFNAMSIZ; i++)
172                 printf("%c", e->ipv6.outiface_mask[i] ? 'X' : '.');
173         printf("\nProtocol: %u\n", e->ipv6.proto);
174         if (e->ipv6.flags & IP6T_F_TOS)
175                 printf("TOS: %u\n", e->ipv6.tos);
176         printf("Flags: %02X\n", e->ipv6.flags);
177         printf("Invflags: %02X\n", e->ipv6.invflags);
178         printf("Counters: %llu packets, %llu bytes\n",
179                e->counters.pcnt, e->counters.bcnt);
180         printf("Cache: %08X ", e->nfcache);
181         if (e->nfcache & NFC_ALTERED) printf("ALTERED ");
182         if (e->nfcache & NFC_UNKNOWN) printf("UNKNOWN ");
183         if (e->nfcache & NFC_IP6_SRC) printf("IP6_SRC ");
184         if (e->nfcache & NFC_IP6_DST) printf("IP6_DST ");
185         if (e->nfcache & NFC_IP6_IF_IN) printf("IP6_IF_IN ");
186         if (e->nfcache & NFC_IP6_IF_OUT) printf("IP6_IF_OUT ");
187         if (e->nfcache & NFC_IP6_TOS) printf("IP6_TOS ");
188         if (e->nfcache & NFC_IP6_PROTO) printf("IP6_PROTO ");
189         if (e->nfcache & NFC_IP6_OPTIONS) printf("IP6_OPTIONS ");
190         if (e->nfcache & NFC_IP6_TCPFLAGS) printf("IP6_TCPFLAGS ");
191         if (e->nfcache & NFC_IP6_SRC_PT) printf("IP6_SRC_PT ");
192         if (e->nfcache & NFC_IP6_DST_PT) printf("IP6_DST_PT ");
193         if (e->nfcache & NFC_IP6_PROTO_UNKNOWN) printf("IP6_PROTO_UNKNOWN ");
194         printf("\n");
195         
196         IP6T_MATCH_ITERATE(e, print_match);
197
198         t = ip6t_get_target(e);
199         printf("Target name: `%s' [%u]\n", t->u.user.name, t->u.target_size);
200         if (strcmp(t->u.user.name, IP6T_STANDARD_TARGET) == 0) {
201                 int pos = *(int *)t->data;
202                 if (pos < 0)
203                         printf("verdict=%s\n",
204                                pos == -NF_ACCEPT-1 ? "NF_ACCEPT"
205                                : pos == -NF_DROP-1 ? "NF_DROP"
206                                : pos == IP6T_RETURN ? "RETURN"
207                                : "UNKNOWN");
208                 else
209                         printf("verdict=%u\n", pos);
210         } else if (strcmp(t->u.user.name, IP6T_ERROR_TARGET) == 0)
211                 printf("error=`%s'\n", t->data);
212
213         printf("\n");
214         return 0;
215 }
216
217 static int
218 is_same(const STRUCT_ENTRY *a, const STRUCT_ENTRY *b,
219         unsigned char *matchmask)
220 {
221         unsigned int i;
222         STRUCT_ENTRY_TARGET *ta, *tb;
223         unsigned char *mptr;
224
225         /* Always compare head structures: ignore mask here. */
226         if (memcmp(&a->ipv6.src, &b->ipv6.src, sizeof(struct in6_addr))
227             || memcmp(&a->ipv6.dst, &b->ipv6.dst, sizeof(struct in6_addr))
228             || memcmp(&a->ipv6.smsk, &b->ipv6.smsk, sizeof(struct in6_addr))
229             || memcmp(&a->ipv6.dmsk, &b->ipv6.dmsk, sizeof(struct in6_addr))
230             || a->ipv6.proto != b->ipv6.proto
231             || a->ipv6.tos != b->ipv6.tos
232             || a->ipv6.flags != b->ipv6.flags
233             || a->ipv6.invflags != b->ipv6.invflags)
234                 return 0;
235
236         for (i = 0; i < IFNAMSIZ; i++) {
237                 if (a->ipv6.iniface_mask[i] != b->ipv6.iniface_mask[i])
238                         return 0;
239                 if ((a->ipv6.iniface[i] & a->ipv6.iniface_mask[i])
240                     != (b->ipv6.iniface[i] & b->ipv6.iniface_mask[i]))
241                         return 0;
242                 if (a->ipv6.outiface_mask[i] != b->ipv6.outiface_mask[i])
243                         return 0;
244                 if ((a->ipv6.outiface[i] & a->ipv6.outiface_mask[i])
245                     != (b->ipv6.outiface[i] & b->ipv6.outiface_mask[i]))
246                         return 0;
247         }
248
249         if (a->nfcache != b->nfcache
250             || a->target_offset != b->target_offset
251             || a->next_offset != b->next_offset)
252                 return 0;
253
254         mptr = matchmask + sizeof(STRUCT_ENTRY);
255         if (IP6T_MATCH_ITERATE(a, match_different, a->elems, b->elems, &mptr))
256                 return 0;
257
258         ta = GET_TARGET((STRUCT_ENTRY *)a);
259         tb = GET_TARGET((STRUCT_ENTRY *)b);
260         if (ta->u.target_size != tb->u.target_size)
261                 return 0;
262         if (strcmp(ta->u.user.name, tb->u.user.name) != 0)
263                 return 0;
264         mptr += sizeof(*ta);
265
266         if (target_different(ta->data, tb->data,
267                              ta->u.target_size - sizeof(*ta), mptr))
268                 return 0;
269
270         return 1;
271 }
272
273 /* All zeroes == unconditional rule. */
274 static inline int
275 unconditional(const struct ip6t_ip6 *ipv6)
276 {
277         unsigned int i;
278
279         for (i = 0; i < sizeof(*ipv6); i++)
280                 if (((char *)ipv6)[i])
281                         break;
282
283         return (i == sizeof(*ipv6));
284 }
285
286 #ifdef IPTC_DEBUG
287 /* Do every conceivable sanity check on the handle */
288 static void
289 do_check(TC_HANDLE_T h, unsigned int line)
290 {
291         unsigned int i, n;
292         unsigned int user_offset; /* Offset of first user chain */
293         int was_return;
294
295         assert(h->changed == 0 || h->changed == 1);
296         if (strcmp(h->info.name, "filter") == 0) {
297                 assert(h->info.valid_hooks
298                        == (1 << NF_IP6_LOCAL_IN
299                            | 1 << NF_IP6_FORWARD
300                            | 1 << NF_IP6_LOCAL_OUT));
301
302                 /* Hooks should be first three */
303                 assert(h->info.hook_entry[NF_IP6_LOCAL_IN] == 0);
304
305                 n = get_chain_end(h, 0);
306                 n += get_entry(h, n)->next_offset;
307                 assert(h->info.hook_entry[NF_IP6_FORWARD] == n);
308
309                 n = get_chain_end(h, n);
310                 n += get_entry(h, n)->next_offset;
311                 assert(h->info.hook_entry[NF_IP6_LOCAL_OUT] == n);
312
313                 user_offset = h->info.hook_entry[NF_IP6_LOCAL_OUT];
314         } else if (strcmp(h->info.name, "nat") == 0) {
315                 assert((h->info.valid_hooks
316                         == (1 << NF_IP6_PRE_ROUTING
317                             | 1 << NF_IP6_LOCAL_OUT
318                             | 1 << NF_IP6_POST_ROUTING)) ||
319                        (h->info.valid_hooks
320                         == (1 << NF_IP6_PRE_ROUTING
321                             | 1 << NF_IP6_LOCAL_IN
322                             | 1 << NF_IP6_LOCAL_OUT
323                             | 1 << NF_IP6_POST_ROUTING)));
324
325                 assert(h->info.hook_entry[NF_IP6_PRE_ROUTING] == 0);
326
327                 n = get_chain_end(h, 0);
328
329                 n += get_entry(h, n)->next_offset;
330                 assert(h->info.hook_entry[NF_IP6_POST_ROUTING] == n);
331                 n = get_chain_end(h, n);
332
333                 n += get_entry(h, n)->next_offset;
334                 assert(h->info.hook_entry[NF_IP6_LOCAL_OUT] == n);
335                 user_offset = h->info.hook_entry[NF_IP6_LOCAL_OUT];
336
337                 if (h->info.valid_hooks & (1 << NF_IP6_LOCAL_IN)) {
338                         n = get_chain_end(h, n);
339                         n += get_entry(h, n)->next_offset;
340                         assert(h->info.hook_entry[NF_IP6_LOCAL_IN] == n);
341                         user_offset = h->info.hook_entry[NF_IP6_LOCAL_IN];
342                 }
343
344         } else if (strcmp(h->info.name, "mangle") == 0) {
345                 /* This code is getting ugly because linux < 2.4.18-pre6 had
346                  * two mangle hooks, linux >= 2.4.18-pre6 has five mangle hooks
347                  * */
348                 assert((h->info.valid_hooks
349                         == (1 << NF_IP6_PRE_ROUTING
350                             | 1 << NF_IP6_LOCAL_OUT)) ||
351                        (h->info.valid_hooks
352                         == (1 << NF_IP6_PRE_ROUTING
353                             | 1 << NF_IP6_LOCAL_IN
354                             | 1 << NF_IP6_FORWARD
355                             | 1 << NF_IP6_LOCAL_OUT
356                             | 1 << NF_IP6_POST_ROUTING)));
357
358                 /* Hooks should be first five */
359                 assert(h->info.hook_entry[NF_IP6_PRE_ROUTING] == 0);
360
361                 n = get_chain_end(h, 0);
362
363                 if (h->info.valid_hooks & (1 << NF_IP6_LOCAL_IN)) {
364                         n += get_entry(h, n)->next_offset;
365                         assert(h->info.hook_entry[NF_IP6_LOCAL_IN] == n);
366                         n = get_chain_end(h, n);
367                 }
368
369                 if (h->info.valid_hooks & (1 << NF_IP6_FORWARD)) {
370                         n += get_entry(h, n)->next_offset;
371                         assert(h->info.hook_entry[NF_IP6_FORWARD] == n);
372                         n = get_chain_end(h, n);
373                 }
374
375                 n += get_entry(h, n)->next_offset;
376                 assert(h->info.hook_entry[NF_IP6_LOCAL_OUT] == n);
377                 user_offset = h->info.hook_entry[NF_IP6_LOCAL_OUT];
378
379                 if (h->info.valid_hooks & (1 << NF_IP6_POST_ROUTING)) {
380                         n = get_chain_end(h, n);
381                         n += get_entry(h, n)->next_offset;
382                         assert(h->info.hook_entry[NF_IP6_POST_ROUTING] == n);
383                         user_offset = h->info.hook_entry[NF_IP6_POST_ROUTING];
384                 }
385         } else {
386                 fprintf(stderr, "Unknown table `%s'\n", h->info.name);
387                 abort();
388         }
389
390         /* User chain == end of last builtin + policy entry */
391         user_offset = get_chain_end(h, user_offset);
392         user_offset += get_entry(h, user_offset)->next_offset;
393
394         /* Overflows should be end of entry chains, and unconditional
395            policy nodes. */
396         for (i = 0; i < NUMHOOKS; i++) {
397                 STRUCT_ENTRY *e;
398                 STRUCT_STANDARD_TARGET *t;
399
400                 if (!(h->info.valid_hooks & (1 << i)))
401                         continue;
402                 assert(h->info.underflow[i]
403                        == get_chain_end(h, h->info.hook_entry[i]));
404
405                 e = get_entry(h, get_chain_end(h, h->info.hook_entry[i]));
406                 assert(unconditional(&e->ipv6));
407                 assert(e->target_offset == sizeof(*e));
408                 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
409                 printf("target_size=%u, align=%u\n",
410                         t->target.u.target_size, ALIGN(sizeof(*t)));
411                 assert(t->target.u.target_size == ALIGN(sizeof(*t)));
412                 assert(e->next_offset == sizeof(*e) + ALIGN(sizeof(*t)));
413
414                 assert(strcmp(t->target.u.user.name, STANDARD_TARGET)==0);
415                 assert(t->verdict == -NF_DROP-1 || t->verdict == -NF_ACCEPT-1);
416
417                 /* Hooks and underflows must be valid entries */
418                 entry2index(h, get_entry(h, h->info.hook_entry[i]));
419                 entry2index(h, get_entry(h, h->info.underflow[i]));
420         }
421
422         assert(h->info.size
423                >= h->info.num_entries * (sizeof(STRUCT_ENTRY)
424                                          +sizeof(STRUCT_STANDARD_TARGET)));
425
426         assert(h->entries.size
427                >= (h->new_number
428                    * (sizeof(STRUCT_ENTRY)
429                       + sizeof(STRUCT_STANDARD_TARGET))));
430         assert(strcmp(h->info.name, h->entries.name) == 0);
431
432         i = 0; n = 0;
433         was_return = 0;
434
435 #if 0
436         /* Check all the entries. */
437         ENTRY_ITERATE(h->entries.entrytable, h->entries.size,
438                       check_entry, &i, &n, user_offset, &was_return, h);
439
440         assert(i == h->new_number);
441         assert(n == h->entries.size);
442
443         /* Final entry must be error node */
444         assert(strcmp(GET_TARGET(index2entry(h, h->new_number-1))
445                       ->u.user.name,
446                       ERROR_TARGET) == 0);
447 #endif
448 }
449 #endif /*IPTC_DEBUG*/