Bump version to 4.10.9; Update ChangeLog.
[collectd.git] / src / owniptc / libip6tc.c
1 /**
2  * This file was imported from the iptables sources.
3  * Copyright (C) 1999-2008 Netfilter Core Team
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  */
18
19 /* Library which manipulates firewall rules.  Version 0.1. */
20
21 /* Architecture of firewall rules is as follows:
22  *
23  * Chains go INPUT, FORWARD, OUTPUT then user chains.
24  * Each user chain starts with an ERROR node.
25  * Every chain ends with an unconditional jump: a RETURN for user chains,
26  * and a POLICY for built-ins.
27  */
28
29 /* (C)1999 Paul ``Rusty'' Russell - Placed under the GNU GPL (See
30    COPYING for details). */
31
32 #include <assert.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <unistd.h>
38 #include <arpa/inet.h>
39
40 #ifdef DEBUG_CONNTRACK
41 #define inline
42 #endif
43
44 #if !defined(__GLIBC__) || (__GLIBC__ < 2)
45 typedef unsigned int socklen_t;
46 #endif
47
48 #include "libip6tc.h"
49
50 #define HOOK_PRE_ROUTING        NF_IP6_PRE_ROUTING
51 #define HOOK_LOCAL_IN           NF_IP6_LOCAL_IN
52 #define HOOK_FORWARD            NF_IP6_FORWARD
53 #define HOOK_LOCAL_OUT          NF_IP6_LOCAL_OUT
54 #define HOOK_POST_ROUTING       NF_IP6_POST_ROUTING
55
56 #define STRUCT_ENTRY_TARGET     struct ip6t_entry_target
57 #define STRUCT_ENTRY            struct ip6t_entry
58 #define STRUCT_ENTRY_MATCH      struct ip6t_entry_match
59 #define STRUCT_GETINFO          struct ip6t_getinfo
60 #define STRUCT_GET_ENTRIES      struct ip6t_get_entries
61 #define STRUCT_COUNTERS         struct ip6t_counters
62 #define STRUCT_COUNTERS_INFO    struct ip6t_counters_info
63 #define STRUCT_STANDARD_TARGET  struct ip6t_standard_target
64 #define STRUCT_REPLACE          struct ip6t_replace
65
66 #define STRUCT_TC_HANDLE        struct ip6tc_handle
67 #define TC_HANDLE_T             ip6tc_handle_t
68
69 #define ENTRY_ITERATE           IP6T_ENTRY_ITERATE
70 #define TABLE_MAXNAMELEN        IP6T_TABLE_MAXNAMELEN
71 #define FUNCTION_MAXNAMELEN     IP6T_FUNCTION_MAXNAMELEN
72
73 #define GET_TARGET              ip6t_get_target
74
75 #define ERROR_TARGET            IP6T_ERROR_TARGET
76 #define NUMHOOKS                NF_IP6_NUMHOOKS
77
78 #define IPT_CHAINLABEL          ip6t_chainlabel
79
80 #define TC_DUMP_ENTRIES         dump_entries6
81 #define TC_IS_CHAIN             ip6tc_is_chain
82 #define TC_FIRST_CHAIN          ip6tc_first_chain
83 #define TC_NEXT_CHAIN           ip6tc_next_chain
84 #define TC_FIRST_RULE           ip6tc_first_rule
85 #define TC_NEXT_RULE            ip6tc_next_rule
86 #define TC_GET_TARGET           ip6tc_get_target
87 #define TC_BUILTIN              ip6tc_builtin
88 #define TC_GET_POLICY           ip6tc_get_policy
89 #define TC_INSERT_ENTRY         ip6tc_insert_entry
90 #define TC_REPLACE_ENTRY        ip6tc_replace_entry
91 #define TC_APPEND_ENTRY         ip6tc_append_entry
92 #define TC_DELETE_ENTRY         ip6tc_delete_entry
93 #define TC_DELETE_NUM_ENTRY     ip6tc_delete_num_entry
94 #define TC_CHECK_PACKET         ip6tc_check_packet
95 #define TC_FLUSH_ENTRIES        ip6tc_flush_entries
96 #define TC_ZERO_ENTRIES         ip6tc_zero_entries
97 #define TC_ZERO_COUNTER         ip6tc_zero_counter
98 #define TC_READ_COUNTER         ip6tc_read_counter
99 #define TC_SET_COUNTER          ip6tc_set_counter
100 #define TC_CREATE_CHAIN         ip6tc_create_chain
101 #define TC_GET_REFERENCES       ip6tc_get_references
102 #define TC_DELETE_CHAIN         ip6tc_delete_chain
103 #define TC_RENAME_CHAIN         ip6tc_rename_chain
104 #define TC_SET_POLICY           ip6tc_set_policy
105 #define TC_GET_RAW_SOCKET       ip6tc_get_raw_socket
106 #define TC_INIT                 ip6tc_init
107 #define TC_FREE                 ip6tc_free
108 #define TC_COMMIT               ip6tc_commit
109 #define TC_STRERROR             ip6tc_strerror
110 #define TC_NUM_RULES            ip6tc_num_rules
111 #define TC_GET_RULE             ip6tc_get_rule
112
113 #define TC_AF                   AF_INET6
114 #define TC_IPPROTO              IPPROTO_IPV6
115
116 #define SO_SET_REPLACE          IP6T_SO_SET_REPLACE
117 #define SO_SET_ADD_COUNTERS     IP6T_SO_SET_ADD_COUNTERS
118 #define SO_GET_INFO             IP6T_SO_GET_INFO
119 #define SO_GET_ENTRIES          IP6T_SO_GET_ENTRIES
120 #define SO_GET_VERSION          IP6T_SO_GET_VERSION
121
122 #define STANDARD_TARGET         IP6T_STANDARD_TARGET
123 #define LABEL_RETURN            IP6TC_LABEL_RETURN
124 #define LABEL_ACCEPT            IP6TC_LABEL_ACCEPT
125 #define LABEL_DROP              IP6TC_LABEL_DROP
126 #define LABEL_QUEUE             IP6TC_LABEL_QUEUE
127
128 #define ALIGN                   IP6T_ALIGN
129 #define RETURN                  IP6T_RETURN
130
131 #include "libiptc.c"
132
133 #define BIT6(a, l) \
134  ((ntohl(a->s6_addr32[(l) / 32]) >> (31 - ((l) & 31))) & 1)
135
136 int
137 ipv6_prefix_length(const struct in6_addr *a)
138 {
139         int l, i;
140         for (l = 0; l < 128; l++) {
141                 if (BIT6(a, l) == 0)
142                         break;
143         }
144         for (i = l + 1; i < 128; i++) {
145                 if (BIT6(a, i) == 1)
146                         return -1;
147         }
148         return l;
149 }
150
151 static int
152 dump_entry(struct ip6t_entry *e, const ip6tc_handle_t handle)
153 {
154         size_t i;
155         char buf[40];
156         int len;
157         struct ip6t_entry_target *t;
158         
159         printf("Entry %u (%lu):\n", iptcb_entry2index(handle, e),
160                iptcb_entry2offset(handle, e));
161         puts("SRC IP: ");
162         inet_ntop(AF_INET6, &e->ipv6.src, buf, sizeof buf);
163         puts(buf);
164         putchar('/');
165         len = ipv6_prefix_length(&e->ipv6.smsk);
166         if (len != -1)
167                 printf("%d", len);
168         else {
169                 inet_ntop(AF_INET6, &e->ipv6.smsk, buf, sizeof buf);
170                 puts(buf);
171         }
172         putchar('\n');
173         
174         puts("DST IP: ");
175         inet_ntop(AF_INET6, &e->ipv6.dst, buf, sizeof buf);
176         puts(buf);
177         putchar('/');
178         len = ipv6_prefix_length(&e->ipv6.dmsk);
179         if (len != -1)
180                 printf("%d", len);
181         else {
182                 inet_ntop(AF_INET6, &e->ipv6.dmsk, buf, sizeof buf);
183                 puts(buf);
184         }
185         putchar('\n');
186         
187         printf("Interface: `%s'/", e->ipv6.iniface);
188         for (i = 0; i < IFNAMSIZ; i++)
189                 printf("%c", e->ipv6.iniface_mask[i] ? 'X' : '.');
190         printf("to `%s'/", e->ipv6.outiface);
191         for (i = 0; i < IFNAMSIZ; i++)
192                 printf("%c", e->ipv6.outiface_mask[i] ? 'X' : '.');
193         printf("\nProtocol: %u\n", e->ipv6.proto);
194         if (e->ipv6.flags & IP6T_F_TOS)
195                 printf("TOS: %u\n", e->ipv6.tos);
196         printf("Flags: %02X\n", e->ipv6.flags);
197         printf("Invflags: %02X\n", e->ipv6.invflags);
198         printf("Counters: %llu packets, %llu bytes\n",
199                (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
200         printf("Cache: %08X\n", e->nfcache);
201         
202         IP6T_MATCH_ITERATE(e, print_match);
203
204         t = ip6t_get_target(e);
205         printf("Target name: `%s' [%u]\n", t->u.user.name, t->u.target_size);
206         if (strcmp(t->u.user.name, IP6T_STANDARD_TARGET) == 0) {
207                 const unsigned char *data = t->data;
208                 int pos = *(const int *)data;
209                 if (pos < 0)
210                         printf("verdict=%s\n",
211                                pos == -NF_ACCEPT-1 ? "NF_ACCEPT"
212                                : pos == -NF_DROP-1 ? "NF_DROP"
213                                : pos == IP6T_RETURN ? "RETURN"
214                                : "UNKNOWN");
215                 else
216                         printf("verdict=%u\n", pos);
217         } else if (strcmp(t->u.user.name, IP6T_ERROR_TARGET) == 0)
218                 printf("error=`%s'\n", t->data);
219
220         printf("\n");
221         return 0;
222 }
223
224 static unsigned char *
225 is_same(const STRUCT_ENTRY *a, const STRUCT_ENTRY *b,
226         unsigned char *matchmask)
227 {
228         unsigned int i;
229         unsigned char *mptr;
230
231         /* Always compare head structures: ignore mask here. */
232         if (memcmp(&a->ipv6.src, &b->ipv6.src, sizeof(struct in6_addr))
233             || memcmp(&a->ipv6.dst, &b->ipv6.dst, sizeof(struct in6_addr))
234             || memcmp(&a->ipv6.smsk, &b->ipv6.smsk, sizeof(struct in6_addr))
235             || memcmp(&a->ipv6.dmsk, &b->ipv6.dmsk, sizeof(struct in6_addr))
236             || a->ipv6.proto != b->ipv6.proto
237             || a->ipv6.tos != b->ipv6.tos
238             || a->ipv6.flags != b->ipv6.flags
239             || a->ipv6.invflags != b->ipv6.invflags)
240                 return NULL;
241
242         for (i = 0; i < IFNAMSIZ; i++) {
243                 if (a->ipv6.iniface_mask[i] != b->ipv6.iniface_mask[i])
244                         return NULL;
245                 if ((a->ipv6.iniface[i] & a->ipv6.iniface_mask[i])
246                     != (b->ipv6.iniface[i] & b->ipv6.iniface_mask[i]))
247                         return NULL;
248                 if (a->ipv6.outiface_mask[i] != b->ipv6.outiface_mask[i])
249                         return NULL;
250                 if ((a->ipv6.outiface[i] & a->ipv6.outiface_mask[i])
251                     != (b->ipv6.outiface[i] & b->ipv6.outiface_mask[i]))
252                         return NULL;
253         }
254
255         if (a->target_offset != b->target_offset
256             || a->next_offset != b->next_offset)
257                 return NULL;
258
259         mptr = matchmask + sizeof(STRUCT_ENTRY);
260         if (IP6T_MATCH_ITERATE(a, match_different, a->elems, b->elems, &mptr))
261                 return NULL;
262         mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_target));
263
264         return mptr;
265 }
266
267 /* All zeroes == unconditional rule. */
268 static inline int
269 unconditional(const struct ip6t_ip6 *ipv6)
270 {
271         unsigned int i;
272
273         for (i = 0; i < sizeof(*ipv6); i++)
274                 if (((char *)ipv6)[i])
275                         break;
276
277         return (i == sizeof(*ipv6));
278 }
279
280 #ifdef IPTC_DEBUG
281 /* Do every conceivable sanity check on the handle */
282 static void
283 do_check(TC_HANDLE_T h, unsigned int line)
284 {
285         unsigned int i, n;
286         unsigned int user_offset; /* Offset of first user chain */
287         int was_return;
288
289         assert(h->changed == 0 || h->changed == 1);
290         if (strcmp(h->info.name, "filter") == 0) {
291                 assert(h->info.valid_hooks
292                        == (1 << NF_IP6_LOCAL_IN
293                            | 1 << NF_IP6_FORWARD
294                            | 1 << NF_IP6_LOCAL_OUT));
295
296                 /* Hooks should be first three */
297                 assert(h->info.hook_entry[NF_IP6_LOCAL_IN] == 0);
298
299                 n = get_chain_end(h, 0);
300                 n += get_entry(h, n)->next_offset;
301                 assert(h->info.hook_entry[NF_IP6_FORWARD] == n);
302
303                 n = get_chain_end(h, n);
304                 n += get_entry(h, n)->next_offset;
305                 assert(h->info.hook_entry[NF_IP6_LOCAL_OUT] == n);
306
307                 user_offset = h->info.hook_entry[NF_IP6_LOCAL_OUT];
308         } else if (strcmp(h->info.name, "nat") == 0) {
309                 assert((h->info.valid_hooks
310                         == (1 << NF_IP6_PRE_ROUTING
311                             | 1 << NF_IP6_LOCAL_OUT
312                             | 1 << NF_IP6_POST_ROUTING)) ||
313                        (h->info.valid_hooks
314                         == (1 << NF_IP6_PRE_ROUTING
315                             | 1 << NF_IP6_LOCAL_IN
316                             | 1 << NF_IP6_LOCAL_OUT
317                             | 1 << NF_IP6_POST_ROUTING)));
318
319                 assert(h->info.hook_entry[NF_IP6_PRE_ROUTING] == 0);
320
321                 n = get_chain_end(h, 0);
322
323                 n += get_entry(h, n)->next_offset;
324                 assert(h->info.hook_entry[NF_IP6_POST_ROUTING] == n);
325                 n = get_chain_end(h, n);
326
327                 n += get_entry(h, n)->next_offset;
328                 assert(h->info.hook_entry[NF_IP6_LOCAL_OUT] == n);
329                 user_offset = h->info.hook_entry[NF_IP6_LOCAL_OUT];
330
331                 if (h->info.valid_hooks & (1 << NF_IP6_LOCAL_IN)) {
332                         n = get_chain_end(h, n);
333                         n += get_entry(h, n)->next_offset;
334                         assert(h->info.hook_entry[NF_IP6_LOCAL_IN] == n);
335                         user_offset = h->info.hook_entry[NF_IP6_LOCAL_IN];
336                 }
337
338         } else if (strcmp(h->info.name, "mangle") == 0) {
339                 /* This code is getting ugly because linux < 2.4.18-pre6 had
340                  * two mangle hooks, linux >= 2.4.18-pre6 has five mangle hooks
341                  * */
342                 assert((h->info.valid_hooks
343                         == (1 << NF_IP6_PRE_ROUTING
344                             | 1 << NF_IP6_LOCAL_OUT)) ||
345                        (h->info.valid_hooks
346                         == (1 << NF_IP6_PRE_ROUTING
347                             | 1 << NF_IP6_LOCAL_IN
348                             | 1 << NF_IP6_FORWARD
349                             | 1 << NF_IP6_LOCAL_OUT
350                             | 1 << NF_IP6_POST_ROUTING)));
351
352                 /* Hooks should be first five */
353                 assert(h->info.hook_entry[NF_IP6_PRE_ROUTING] == 0);
354
355                 n = get_chain_end(h, 0);
356
357                 if (h->info.valid_hooks & (1 << NF_IP6_LOCAL_IN)) {
358                         n += get_entry(h, n)->next_offset;
359                         assert(h->info.hook_entry[NF_IP6_LOCAL_IN] == n);
360                         n = get_chain_end(h, n);
361                 }
362
363                 if (h->info.valid_hooks & (1 << NF_IP6_FORWARD)) {
364                         n += get_entry(h, n)->next_offset;
365                         assert(h->info.hook_entry[NF_IP6_FORWARD] == n);
366                         n = get_chain_end(h, n);
367                 }
368
369                 n += get_entry(h, n)->next_offset;
370                 assert(h->info.hook_entry[NF_IP6_LOCAL_OUT] == n);
371                 user_offset = h->info.hook_entry[NF_IP6_LOCAL_OUT];
372
373                 if (h->info.valid_hooks & (1 << NF_IP6_POST_ROUTING)) {
374                         n = get_chain_end(h, n);
375                         n += get_entry(h, n)->next_offset;
376                         assert(h->info.hook_entry[NF_IP6_POST_ROUTING] == n);
377                         user_offset = h->info.hook_entry[NF_IP6_POST_ROUTING];
378                 }
379         } else if (strcmp(h->info.name, "raw") == 0) {
380                 assert(h->info.valid_hooks
381                        == (1 << NF_IP6_PRE_ROUTING
382                            | 1 << NF_IP6_LOCAL_OUT));
383
384                 /* Hooks should be first three */
385                 assert(h->info.hook_entry[NF_IP6_PRE_ROUTING] == 0);
386
387                 n = get_chain_end(h, n);
388                 n += get_entry(h, n)->next_offset;
389                 assert(h->info.hook_entry[NF_IP6_LOCAL_OUT] == n);
390
391                 user_offset = h->info.hook_entry[NF_IP6_LOCAL_OUT];
392         } else {
393                 fprintf(stderr, "Unknown table `%s'\n", h->info.name);
394                 abort();
395         }
396
397         /* User chain == end of last builtin + policy entry */
398         user_offset = get_chain_end(h, user_offset);
399         user_offset += get_entry(h, user_offset)->next_offset;
400
401         /* Overflows should be end of entry chains, and unconditional
402            policy nodes. */
403         for (i = 0; i < NUMHOOKS; i++) {
404                 STRUCT_ENTRY *e;
405                 STRUCT_STANDARD_TARGET *t;
406
407                 if (!(h->info.valid_hooks & (1 << i)))
408                         continue;
409                 assert(h->info.underflow[i]
410                        == get_chain_end(h, h->info.hook_entry[i]));
411
412                 e = get_entry(h, get_chain_end(h, h->info.hook_entry[i]));
413                 assert(unconditional(&e->ipv6));
414                 assert(e->target_offset == sizeof(*e));
415                 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
416                 printf("target_size=%u, align=%u\n",
417                         t->target.u.target_size, ALIGN(sizeof(*t)));
418                 assert(t->target.u.target_size == ALIGN(sizeof(*t)));
419                 assert(e->next_offset == sizeof(*e) + ALIGN(sizeof(*t)));
420
421                 assert(strcmp(t->target.u.user.name, STANDARD_TARGET)==0);
422                 assert(t->verdict == -NF_DROP-1 || t->verdict == -NF_ACCEPT-1);
423
424                 /* Hooks and underflows must be valid entries */
425                 iptcb_entry2index(h, get_entry(h, h->info.hook_entry[i]));
426                 iptcb_entry2index(h, get_entry(h, h->info.underflow[i]));
427         }
428
429         assert(h->info.size
430                >= h->info.num_entries * (sizeof(STRUCT_ENTRY)
431                                          +sizeof(STRUCT_STANDARD_TARGET)));
432
433         assert(h->entries.size
434                >= (h->new_number
435                    * (sizeof(STRUCT_ENTRY)
436                       + sizeof(STRUCT_STANDARD_TARGET))));
437         assert(strcmp(h->info.name, h->entries.name) == 0);
438
439         i = 0; n = 0;
440         was_return = 0;
441
442 #if 0
443         /* Check all the entries. */
444         ENTRY_ITERATE(h->entries.entrytable, h->entries.size,
445                       check_entry, &i, &n, user_offset, &was_return, h);
446
447         assert(i == h->new_number);
448         assert(n == h->entries.size);
449
450         /* Final entry must be error node */
451         assert(strcmp(GET_TARGET(index2entry(h, h->new_number-1))
452                       ->u.user.name,
453                       ERROR_TARGET) == 0);
454 #endif
455 }
456 #endif /*IPTC_DEBUG*/