iptables-1.3.2-20050720
[iptables.git] / libiptc / 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", iptcb_entry2index(handle, e),
140                iptcb_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                (unsigned long long)e->counters.pcnt, (unsigned long long)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         printf("\n");
184         
185         IP6T_MATCH_ITERATE(e, print_match);
186
187         t = ip6t_get_target(e);
188         printf("Target name: `%s' [%u]\n", t->u.user.name, t->u.target_size);
189         if (strcmp(t->u.user.name, IP6T_STANDARD_TARGET) == 0) {
190                 int pos = *(int *)t->data;
191                 if (pos < 0)
192                         printf("verdict=%s\n",
193                                pos == -NF_ACCEPT-1 ? "NF_ACCEPT"
194                                : pos == -NF_DROP-1 ? "NF_DROP"
195                                : pos == IP6T_RETURN ? "RETURN"
196                                : "UNKNOWN");
197                 else
198                         printf("verdict=%u\n", pos);
199         } else if (strcmp(t->u.user.name, IP6T_ERROR_TARGET) == 0)
200                 printf("error=`%s'\n", t->data);
201
202         printf("\n");
203         return 0;
204 }
205
206 static unsigned char *
207 is_same(const STRUCT_ENTRY *a, const STRUCT_ENTRY *b,
208         unsigned char *matchmask)
209 {
210         unsigned int i;
211         unsigned char *mptr;
212
213         /* Always compare head structures: ignore mask here. */
214         if (memcmp(&a->ipv6.src, &b->ipv6.src, sizeof(struct in6_addr))
215             || memcmp(&a->ipv6.dst, &b->ipv6.dst, sizeof(struct in6_addr))
216             || memcmp(&a->ipv6.smsk, &b->ipv6.smsk, sizeof(struct in6_addr))
217             || memcmp(&a->ipv6.dmsk, &b->ipv6.dmsk, sizeof(struct in6_addr))
218             || a->ipv6.proto != b->ipv6.proto
219             || a->ipv6.tos != b->ipv6.tos
220             || a->ipv6.flags != b->ipv6.flags
221             || a->ipv6.invflags != b->ipv6.invflags)
222                 return NULL;
223
224         for (i = 0; i < IFNAMSIZ; i++) {
225                 if (a->ipv6.iniface_mask[i] != b->ipv6.iniface_mask[i])
226                         return NULL;
227                 if ((a->ipv6.iniface[i] & a->ipv6.iniface_mask[i])
228                     != (b->ipv6.iniface[i] & b->ipv6.iniface_mask[i]))
229                         return NULL;
230                 if (a->ipv6.outiface_mask[i] != b->ipv6.outiface_mask[i])
231                         return NULL;
232                 if ((a->ipv6.outiface[i] & a->ipv6.outiface_mask[i])
233                     != (b->ipv6.outiface[i] & b->ipv6.outiface_mask[i]))
234                         return NULL;
235         }
236
237         if (a->nfcache != b->nfcache
238             || a->target_offset != b->target_offset
239             || a->next_offset != b->next_offset)
240                 return NULL;
241
242         mptr = matchmask + sizeof(STRUCT_ENTRY);
243         if (IP6T_MATCH_ITERATE(a, match_different, a->elems, b->elems, &mptr))
244                 return NULL;
245         mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_target));
246
247         return mptr;
248 }
249
250 /* All zeroes == unconditional rule. */
251 static inline int
252 unconditional(const struct ip6t_ip6 *ipv6)
253 {
254         unsigned int i;
255
256         for (i = 0; i < sizeof(*ipv6); i++)
257                 if (((char *)ipv6)[i])
258                         break;
259
260         return (i == sizeof(*ipv6));
261 }
262
263 #ifdef IPTC_DEBUG
264 /* Do every conceivable sanity check on the handle */
265 static void
266 do_check(TC_HANDLE_T h, unsigned int line)
267 {
268         unsigned int i, n;
269         unsigned int user_offset; /* Offset of first user chain */
270         int was_return;
271
272         assert(h->changed == 0 || h->changed == 1);
273         if (strcmp(h->info.name, "filter") == 0) {
274                 assert(h->info.valid_hooks
275                        == (1 << NF_IP6_LOCAL_IN
276                            | 1 << NF_IP6_FORWARD
277                            | 1 << NF_IP6_LOCAL_OUT));
278
279                 /* Hooks should be first three */
280                 assert(h->info.hook_entry[NF_IP6_LOCAL_IN] == 0);
281
282                 n = get_chain_end(h, 0);
283                 n += get_entry(h, n)->next_offset;
284                 assert(h->info.hook_entry[NF_IP6_FORWARD] == n);
285
286                 n = get_chain_end(h, n);
287                 n += get_entry(h, n)->next_offset;
288                 assert(h->info.hook_entry[NF_IP6_LOCAL_OUT] == n);
289
290                 user_offset = h->info.hook_entry[NF_IP6_LOCAL_OUT];
291         } else if (strcmp(h->info.name, "nat") == 0) {
292                 assert((h->info.valid_hooks
293                         == (1 << NF_IP6_PRE_ROUTING
294                             | 1 << NF_IP6_LOCAL_OUT
295                             | 1 << NF_IP6_POST_ROUTING)) ||
296                        (h->info.valid_hooks
297                         == (1 << NF_IP6_PRE_ROUTING
298                             | 1 << NF_IP6_LOCAL_IN
299                             | 1 << NF_IP6_LOCAL_OUT
300                             | 1 << NF_IP6_POST_ROUTING)));
301
302                 assert(h->info.hook_entry[NF_IP6_PRE_ROUTING] == 0);
303
304                 n = get_chain_end(h, 0);
305
306                 n += get_entry(h, n)->next_offset;
307                 assert(h->info.hook_entry[NF_IP6_POST_ROUTING] == n);
308                 n = get_chain_end(h, n);
309
310                 n += get_entry(h, n)->next_offset;
311                 assert(h->info.hook_entry[NF_IP6_LOCAL_OUT] == n);
312                 user_offset = h->info.hook_entry[NF_IP6_LOCAL_OUT];
313
314                 if (h->info.valid_hooks & (1 << NF_IP6_LOCAL_IN)) {
315                         n = get_chain_end(h, n);
316                         n += get_entry(h, n)->next_offset;
317                         assert(h->info.hook_entry[NF_IP6_LOCAL_IN] == n);
318                         user_offset = h->info.hook_entry[NF_IP6_LOCAL_IN];
319                 }
320
321         } else if (strcmp(h->info.name, "mangle") == 0) {
322                 /* This code is getting ugly because linux < 2.4.18-pre6 had
323                  * two mangle hooks, linux >= 2.4.18-pre6 has five mangle hooks
324                  * */
325                 assert((h->info.valid_hooks
326                         == (1 << NF_IP6_PRE_ROUTING
327                             | 1 << NF_IP6_LOCAL_OUT)) ||
328                        (h->info.valid_hooks
329                         == (1 << NF_IP6_PRE_ROUTING
330                             | 1 << NF_IP6_LOCAL_IN
331                             | 1 << NF_IP6_FORWARD
332                             | 1 << NF_IP6_LOCAL_OUT
333                             | 1 << NF_IP6_POST_ROUTING)));
334
335                 /* Hooks should be first five */
336                 assert(h->info.hook_entry[NF_IP6_PRE_ROUTING] == 0);
337
338                 n = get_chain_end(h, 0);
339
340                 if (h->info.valid_hooks & (1 << NF_IP6_LOCAL_IN)) {
341                         n += get_entry(h, n)->next_offset;
342                         assert(h->info.hook_entry[NF_IP6_LOCAL_IN] == n);
343                         n = get_chain_end(h, n);
344                 }
345
346                 if (h->info.valid_hooks & (1 << NF_IP6_FORWARD)) {
347                         n += get_entry(h, n)->next_offset;
348                         assert(h->info.hook_entry[NF_IP6_FORWARD] == n);
349                         n = get_chain_end(h, n);
350                 }
351
352                 n += get_entry(h, n)->next_offset;
353                 assert(h->info.hook_entry[NF_IP6_LOCAL_OUT] == n);
354                 user_offset = h->info.hook_entry[NF_IP6_LOCAL_OUT];
355
356                 if (h->info.valid_hooks & (1 << NF_IP6_POST_ROUTING)) {
357                         n = get_chain_end(h, n);
358                         n += get_entry(h, n)->next_offset;
359                         assert(h->info.hook_entry[NF_IP6_POST_ROUTING] == n);
360                         user_offset = h->info.hook_entry[NF_IP6_POST_ROUTING];
361                 }
362         } else if (strcmp(h->info.name, "raw") == 0) {
363                 assert(h->info.valid_hooks
364                        == (1 << NF_IP6_PRE_ROUTING
365                            | 1 << NF_IP6_LOCAL_OUT));
366
367                 /* Hooks should be first three */
368                 assert(h->info.hook_entry[NF_IP6_PRE_ROUTING] == 0);
369
370                 n = get_chain_end(h, n);
371                 n += get_entry(h, n)->next_offset;
372                 assert(h->info.hook_entry[NF_IP6_LOCAL_OUT] == n);
373
374                 user_offset = h->info.hook_entry[NF_IP6_LOCAL_OUT];
375         } else {
376                 fprintf(stderr, "Unknown table `%s'\n", h->info.name);
377                 abort();
378         }
379
380         /* User chain == end of last builtin + policy entry */
381         user_offset = get_chain_end(h, user_offset);
382         user_offset += get_entry(h, user_offset)->next_offset;
383
384         /* Overflows should be end of entry chains, and unconditional
385            policy nodes. */
386         for (i = 0; i < NUMHOOKS; i++) {
387                 STRUCT_ENTRY *e;
388                 STRUCT_STANDARD_TARGET *t;
389
390                 if (!(h->info.valid_hooks & (1 << i)))
391                         continue;
392                 assert(h->info.underflow[i]
393                        == get_chain_end(h, h->info.hook_entry[i]));
394
395                 e = get_entry(h, get_chain_end(h, h->info.hook_entry[i]));
396                 assert(unconditional(&e->ipv6));
397                 assert(e->target_offset == sizeof(*e));
398                 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
399                 printf("target_size=%u, align=%u\n",
400                         t->target.u.target_size, ALIGN(sizeof(*t)));
401                 assert(t->target.u.target_size == ALIGN(sizeof(*t)));
402                 assert(e->next_offset == sizeof(*e) + ALIGN(sizeof(*t)));
403
404                 assert(strcmp(t->target.u.user.name, STANDARD_TARGET)==0);
405                 assert(t->verdict == -NF_DROP-1 || t->verdict == -NF_ACCEPT-1);
406
407                 /* Hooks and underflows must be valid entries */
408                 iptcb_entry2index(h, get_entry(h, h->info.hook_entry[i]));
409                 iptcb_entry2index(h, get_entry(h, h->info.underflow[i]));
410         }
411
412         assert(h->info.size
413                >= h->info.num_entries * (sizeof(STRUCT_ENTRY)
414                                          +sizeof(STRUCT_STANDARD_TARGET)));
415
416         assert(h->entries.size
417                >= (h->new_number
418                    * (sizeof(STRUCT_ENTRY)
419                       + sizeof(STRUCT_STANDARD_TARGET))));
420         assert(strcmp(h->info.name, h->entries.name) == 0);
421
422         i = 0; n = 0;
423         was_return = 0;
424
425 #if 0
426         /* Check all the entries. */
427         ENTRY_ITERATE(h->entries.entrytable, h->entries.size,
428                       check_entry, &i, &n, user_offset, &was_return, h);
429
430         assert(i == h->new_number);
431         assert(n == h->entries.size);
432
433         /* Final entry must be error node */
434         assert(strcmp(GET_TARGET(index2entry(h, h->new_number-1))
435                       ->u.user.name,
436                       ERROR_TARGET) == 0);
437 #endif
438 }
439 #endif /*IPTC_DEBUG*/