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