66abb44c2f3d1718b05956b23b9aac95183e9653
[collectd.git] / src / libiptc / libip4tc.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
39 #ifdef DEBUG_CONNTRACK
40 #define inline
41 #endif
42
43 #if !defined(__GLIBC__) || (__GLIBC__ < 2)
44 typedef unsigned int socklen_t;
45 #endif
46
47 #include "libiptc.h"
48
49 #define IP_VERSION      4
50 #define IP_OFFSET       0x1FFF
51
52 #define HOOK_PRE_ROUTING        NF_IP_PRE_ROUTING
53 #define HOOK_LOCAL_IN           NF_IP_LOCAL_IN
54 #define HOOK_FORWARD            NF_IP_FORWARD
55 #define HOOK_LOCAL_OUT          NF_IP_LOCAL_OUT
56 #define HOOK_POST_ROUTING       NF_IP_POST_ROUTING
57 #ifdef NF_IP_DROPPING
58 #define HOOK_DROPPING           NF_IP_DROPPING
59 #endif
60
61 #define STRUCT_ENTRY_TARGET     struct ipt_entry_target
62 #define STRUCT_ENTRY            struct ipt_entry
63 #define STRUCT_ENTRY_MATCH      struct ipt_entry_match
64 #define STRUCT_GETINFO          struct ipt_getinfo
65 #define STRUCT_GET_ENTRIES      struct ipt_get_entries
66 #define STRUCT_COUNTERS         struct ipt_counters
67 #define STRUCT_COUNTERS_INFO    struct ipt_counters_info
68 #define STRUCT_STANDARD_TARGET  struct ipt_standard_target
69 #define STRUCT_REPLACE          struct ipt_replace
70
71 #define STRUCT_TC_HANDLE        struct iptc_handle
72 #define TC_HANDLE_T             iptc_handle_t
73
74 #define ENTRY_ITERATE           IPT_ENTRY_ITERATE
75 #define TABLE_MAXNAMELEN        IPT_TABLE_MAXNAMELEN
76 #define FUNCTION_MAXNAMELEN     IPT_FUNCTION_MAXNAMELEN
77
78 #define GET_TARGET              ipt_get_target
79
80 #define ERROR_TARGET            IPT_ERROR_TARGET
81 #define NUMHOOKS                NF_IP_NUMHOOKS
82
83 #define IPT_CHAINLABEL          ipt_chainlabel
84
85 #define TC_DUMP_ENTRIES         dump_entries
86 #define TC_IS_CHAIN             iptc_is_chain
87 #define TC_FIRST_CHAIN          iptc_first_chain
88 #define TC_NEXT_CHAIN           iptc_next_chain
89 #define TC_FIRST_RULE           iptc_first_rule
90 #define TC_NEXT_RULE            iptc_next_rule
91 #define TC_GET_TARGET           iptc_get_target
92 #define TC_BUILTIN              iptc_builtin
93 #define TC_GET_POLICY           iptc_get_policy
94 #define TC_INSERT_ENTRY         iptc_insert_entry
95 #define TC_REPLACE_ENTRY        iptc_replace_entry
96 #define TC_APPEND_ENTRY         iptc_append_entry
97 #define TC_DELETE_ENTRY         iptc_delete_entry
98 #define TC_DELETE_NUM_ENTRY     iptc_delete_num_entry
99 #define TC_CHECK_PACKET         iptc_check_packet
100 #define TC_FLUSH_ENTRIES        iptc_flush_entries
101 #define TC_ZERO_ENTRIES         iptc_zero_entries
102 #define TC_READ_COUNTER         iptc_read_counter
103 #define TC_ZERO_COUNTER         iptc_zero_counter
104 #define TC_SET_COUNTER          iptc_set_counter
105 #define TC_CREATE_CHAIN         iptc_create_chain
106 #define TC_GET_REFERENCES       iptc_get_references
107 #define TC_DELETE_CHAIN         iptc_delete_chain
108 #define TC_RENAME_CHAIN         iptc_rename_chain
109 #define TC_SET_POLICY           iptc_set_policy
110 #define TC_GET_RAW_SOCKET       iptc_get_raw_socket
111 #define TC_INIT                 iptc_init
112 #define TC_FREE                 iptc_free
113 #define TC_COMMIT               iptc_commit
114 #define TC_STRERROR             iptc_strerror
115 #define TC_NUM_RULES            iptc_num_rules
116 #define TC_GET_RULE             iptc_get_rule
117
118 #define TC_AF                   AF_INET
119 #define TC_IPPROTO              IPPROTO_IP
120
121 #define SO_SET_REPLACE          IPT_SO_SET_REPLACE
122 #define SO_SET_ADD_COUNTERS     IPT_SO_SET_ADD_COUNTERS
123 #define SO_GET_INFO             IPT_SO_GET_INFO
124 #define SO_GET_ENTRIES          IPT_SO_GET_ENTRIES
125 #define SO_GET_VERSION          IPT_SO_GET_VERSION
126
127 #define STANDARD_TARGET         IPT_STANDARD_TARGET
128 #define LABEL_RETURN            IPTC_LABEL_RETURN
129 #define LABEL_ACCEPT            IPTC_LABEL_ACCEPT
130 #define LABEL_DROP              IPTC_LABEL_DROP
131 #define LABEL_QUEUE             IPTC_LABEL_QUEUE
132
133 #define ALIGN                   IPT_ALIGN
134 #define RETURN                  IPT_RETURN
135
136 #include "libiptc.c"
137
138 #define IP_PARTS_NATIVE(n)                      \
139 (unsigned int)((n)>>24)&0xFF,                   \
140 (unsigned int)((n)>>16)&0xFF,                   \
141 (unsigned int)((n)>>8)&0xFF,                    \
142 (unsigned int)((n)&0xFF)
143
144 #define IP_PARTS(n) IP_PARTS_NATIVE(ntohl(n))
145
146 int
147 dump_entry(STRUCT_ENTRY *e, const TC_HANDLE_T handle)
148 {
149         size_t i;
150         STRUCT_ENTRY_TARGET *t;
151
152         printf("Entry %u (%lu):\n", iptcb_entry2index(handle, e),
153                iptcb_entry2offset(handle, e));
154         printf("SRC IP: %u.%u.%u.%u/%u.%u.%u.%u\n",
155                IP_PARTS(e->ip.src.s_addr),IP_PARTS(e->ip.smsk.s_addr));
156         printf("DST IP: %u.%u.%u.%u/%u.%u.%u.%u\n",
157                IP_PARTS(e->ip.dst.s_addr),IP_PARTS(e->ip.dmsk.s_addr));
158         printf("Interface: `%s'/", e->ip.iniface);
159         for (i = 0; i < IFNAMSIZ; i++)
160                 printf("%c", e->ip.iniface_mask[i] ? 'X' : '.');
161         printf("to `%s'/", e->ip.outiface);
162         for (i = 0; i < IFNAMSIZ; i++)
163                 printf("%c", e->ip.outiface_mask[i] ? 'X' : '.');
164         printf("\nProtocol: %u\n", e->ip.proto);
165         printf("Flags: %02X\n", e->ip.flags);
166         printf("Invflags: %02X\n", e->ip.invflags);
167         printf("Counters: %llu packets, %llu bytes\n",
168                (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
169         printf("Cache: %08X\n", e->nfcache);
170
171         IPT_MATCH_ITERATE(e, print_match);
172
173         t = GET_TARGET(e);
174         printf("Target name: `%s' [%u]\n", t->u.user.name, t->u.target_size);
175         if (strcmp(t->u.user.name, STANDARD_TARGET) == 0) {
176                 int pos = *(int *)t->data;
177                 if (pos < 0)
178                         printf("verdict=%s\n",
179                                pos == -NF_ACCEPT-1 ? "NF_ACCEPT"
180                                : pos == -NF_DROP-1 ? "NF_DROP"
181                                : pos == -NF_QUEUE-1 ? "NF_QUEUE"
182                                : pos == RETURN ? "RETURN"
183                                : "UNKNOWN");
184                 else
185                         printf("verdict=%u\n", pos);
186         } else if (strcmp(t->u.user.name, IPT_ERROR_TARGET) == 0)
187                 printf("error=`%s'\n", t->data);
188
189         printf("\n");
190         return 0;
191 }
192
193 static unsigned char *
194 is_same(const STRUCT_ENTRY *a, const STRUCT_ENTRY *b, unsigned char *matchmask)
195 {
196         unsigned int i;
197         unsigned char *mptr;
198
199         /* Always compare head structures: ignore mask here. */
200         if (a->ip.src.s_addr != b->ip.src.s_addr
201             || a->ip.dst.s_addr != b->ip.dst.s_addr
202             || a->ip.smsk.s_addr != b->ip.smsk.s_addr
203             || a->ip.dmsk.s_addr != b->ip.dmsk.s_addr
204             || a->ip.proto != b->ip.proto
205             || a->ip.flags != b->ip.flags
206             || a->ip.invflags != b->ip.invflags)
207                 return NULL;
208
209         for (i = 0; i < IFNAMSIZ; i++) {
210                 if (a->ip.iniface_mask[i] != b->ip.iniface_mask[i])
211                         return NULL;
212                 if ((a->ip.iniface[i] & a->ip.iniface_mask[i])
213                     != (b->ip.iniface[i] & b->ip.iniface_mask[i]))
214                         return NULL;
215                 if (a->ip.outiface_mask[i] != b->ip.outiface_mask[i])
216                         return NULL;
217                 if ((a->ip.outiface[i] & a->ip.outiface_mask[i])
218                     != (b->ip.outiface[i] & b->ip.outiface_mask[i]))
219                         return NULL;
220         }
221
222         if (a->target_offset != b->target_offset
223             || a->next_offset != b->next_offset)
224                 return NULL;
225
226         mptr = matchmask + sizeof(STRUCT_ENTRY);
227         if (IPT_MATCH_ITERATE(a, match_different, a->elems, b->elems, &mptr))
228                 return NULL;
229         mptr += IPT_ALIGN(sizeof(struct ipt_entry_target));
230
231         return mptr;
232 }
233
234 #if 0
235 /***************************** DEBUGGING ********************************/
236 static inline int
237 unconditional(const struct ipt_ip *ip)
238 {
239         unsigned int i;
240
241         for (i = 0; i < sizeof(*ip)/sizeof(u_int32_t); i++)
242                 if (((u_int32_t *)ip)[i])
243                         return 0;
244
245         return 1;
246 }
247
248 static inline int
249 check_match(const STRUCT_ENTRY_MATCH *m, unsigned int *off)
250 {
251         assert(m->u.match_size >= sizeof(STRUCT_ENTRY_MATCH));
252         assert(ALIGN(m->u.match_size) == m->u.match_size);
253
254         (*off) += m->u.match_size;
255         return 0;
256 }
257
258 static inline int
259 check_entry(const STRUCT_ENTRY *e, unsigned int *i, unsigned int *off,
260             unsigned int user_offset, int *was_return,
261             TC_HANDLE_T h)
262 {
263         unsigned int toff;
264         STRUCT_STANDARD_TARGET *t;
265
266         assert(e->target_offset >= sizeof(STRUCT_ENTRY));
267         assert(e->next_offset >= e->target_offset
268                + sizeof(STRUCT_ENTRY_TARGET));
269         toff = sizeof(STRUCT_ENTRY);
270         IPT_MATCH_ITERATE(e, check_match, &toff);
271
272         assert(toff == e->target_offset);
273
274         t = (STRUCT_STANDARD_TARGET *)
275                 GET_TARGET((STRUCT_ENTRY *)e);
276         /* next_offset will have to be multiple of entry alignment. */
277         assert(e->next_offset == ALIGN(e->next_offset));
278         assert(e->target_offset == ALIGN(e->target_offset));
279         assert(t->target.u.target_size == ALIGN(t->target.u.target_size));
280         assert(!TC_IS_CHAIN(t->target.u.user.name, h));
281
282         if (strcmp(t->target.u.user.name, STANDARD_TARGET) == 0) {
283                 assert(t->target.u.target_size
284                        == ALIGN(sizeof(STRUCT_STANDARD_TARGET)));
285
286                 assert(t->verdict == -NF_DROP-1
287                        || t->verdict == -NF_ACCEPT-1
288                        || t->verdict == RETURN
289                        || t->verdict < (int)h->entries->size);
290
291                 if (t->verdict >= 0) {
292                         STRUCT_ENTRY *te = get_entry(h, t->verdict);
293                         int idx;
294
295                         idx = iptcb_entry2index(h, te);
296                         assert(strcmp(GET_TARGET(te)->u.user.name,
297                                       IPT_ERROR_TARGET)
298                                != 0);
299                         assert(te != e);
300
301                         /* Prior node must be error node, or this node. */
302                         assert(t->verdict == iptcb_entry2offset(h, e)+e->next_offset
303                                || strcmp(GET_TARGET(index2entry(h, idx-1))
304                                          ->u.user.name, IPT_ERROR_TARGET)
305                                == 0);
306                 }
307
308                 if (t->verdict == RETURN
309                     && unconditional(&e->ip)
310                     && e->target_offset == sizeof(*e))
311                         *was_return = 1;
312                 else
313                         *was_return = 0;
314         } else if (strcmp(t->target.u.user.name, IPT_ERROR_TARGET) == 0) {
315                 assert(t->target.u.target_size
316                        == ALIGN(sizeof(struct ipt_error_target)));
317
318                 /* If this is in user area, previous must have been return */
319                 if (*off > user_offset)
320                         assert(*was_return);
321
322                 *was_return = 0;
323         }
324         else *was_return = 0;
325
326         if (*off == user_offset)
327                 assert(strcmp(t->target.u.user.name, IPT_ERROR_TARGET) == 0);
328
329         (*off) += e->next_offset;
330         (*i)++;
331         return 0;
332 }
333
334 #ifdef IPTC_DEBUG
335 /* Do every conceivable sanity check on the handle */
336 static void
337 do_check(TC_HANDLE_T h, unsigned int line)
338 {
339         unsigned int i, n;
340         unsigned int user_offset; /* Offset of first user chain */
341         int was_return;
342
343         assert(h->changed == 0 || h->changed == 1);
344         if (strcmp(h->info.name, "filter") == 0) {
345                 assert(h->info.valid_hooks
346                        == (1 << NF_IP_LOCAL_IN
347                            | 1 << NF_IP_FORWARD
348                            | 1 << NF_IP_LOCAL_OUT));
349
350                 /* Hooks should be first three */
351                 assert(h->info.hook_entry[NF_IP_LOCAL_IN] == 0);
352
353                 n = get_chain_end(h, 0);
354                 n += get_entry(h, n)->next_offset;
355                 assert(h->info.hook_entry[NF_IP_FORWARD] == n);
356
357                 n = get_chain_end(h, n);
358                 n += get_entry(h, n)->next_offset;
359                 assert(h->info.hook_entry[NF_IP_LOCAL_OUT] == n);
360
361                 user_offset = h->info.hook_entry[NF_IP_LOCAL_OUT];
362         } else if (strcmp(h->info.name, "nat") == 0) {
363                 assert((h->info.valid_hooks
364                         == (1 << NF_IP_PRE_ROUTING
365                             | 1 << NF_IP_POST_ROUTING
366                             | 1 << NF_IP_LOCAL_OUT)) ||
367                        (h->info.valid_hooks
368                         == (1 << NF_IP_PRE_ROUTING
369                             | 1 << NF_IP_LOCAL_IN
370                             | 1 << NF_IP_POST_ROUTING
371                             | 1 << NF_IP_LOCAL_OUT)));
372
373                 assert(h->info.hook_entry[NF_IP_PRE_ROUTING] == 0);
374
375                 n = get_chain_end(h, 0);
376
377                 n += get_entry(h, n)->next_offset;
378                 assert(h->info.hook_entry[NF_IP_POST_ROUTING] == n);
379                 n = get_chain_end(h, n);
380
381                 n += get_entry(h, n)->next_offset;
382                 assert(h->info.hook_entry[NF_IP_LOCAL_OUT] == n);
383                 user_offset = h->info.hook_entry[NF_IP_LOCAL_OUT];
384
385                 if (h->info.valid_hooks & (1 << NF_IP_LOCAL_IN)) {
386                         n = get_chain_end(h, n);
387                         n += get_entry(h, n)->next_offset;
388                         assert(h->info.hook_entry[NF_IP_LOCAL_IN] == n);
389                         user_offset = h->info.hook_entry[NF_IP_LOCAL_IN];
390                 }
391
392         } else if (strcmp(h->info.name, "mangle") == 0) {
393                 /* This code is getting ugly because linux < 2.4.18-pre6 had
394                  * two mangle hooks, linux >= 2.4.18-pre6 has five mangle hooks
395                  * */
396                 assert((h->info.valid_hooks
397                         == (1 << NF_IP_PRE_ROUTING
398                             | 1 << NF_IP_LOCAL_OUT)) || 
399                        (h->info.valid_hooks
400                         == (1 << NF_IP_PRE_ROUTING
401                             | 1 << NF_IP_LOCAL_IN
402                             | 1 << NF_IP_FORWARD
403                             | 1 << NF_IP_LOCAL_OUT
404                             | 1 << NF_IP_POST_ROUTING)));
405
406                 /* Hooks should be first five */
407                 assert(h->info.hook_entry[NF_IP_PRE_ROUTING] == 0);
408
409                 n = get_chain_end(h, 0);
410
411                 if (h->info.valid_hooks & (1 << NF_IP_LOCAL_IN)) {
412                         n += get_entry(h, n)->next_offset;
413                         assert(h->info.hook_entry[NF_IP_LOCAL_IN] == n);
414                         n = get_chain_end(h, n);
415                 }
416
417                 if (h->info.valid_hooks & (1 << NF_IP_FORWARD)) {
418                         n += get_entry(h, n)->next_offset;
419                         assert(h->info.hook_entry[NF_IP_FORWARD] == n);
420                         n = get_chain_end(h, n);
421                 }
422
423                 n += get_entry(h, n)->next_offset;
424                 assert(h->info.hook_entry[NF_IP_LOCAL_OUT] == n);
425                 user_offset = h->info.hook_entry[NF_IP_LOCAL_OUT];
426
427                 if (h->info.valid_hooks & (1 << NF_IP_POST_ROUTING)) {
428                         n = get_chain_end(h, n);
429                         n += get_entry(h, n)->next_offset;
430                         assert(h->info.hook_entry[NF_IP_POST_ROUTING] == n);
431                         user_offset = h->info.hook_entry[NF_IP_POST_ROUTING];
432                 }
433         } else if (strcmp(h->info.name, "raw") == 0) {
434                 assert(h->info.valid_hooks
435                        == (1 << NF_IP_PRE_ROUTING
436                            | 1 << NF_IP_LOCAL_OUT));
437
438                 /* Hooks should be first three */
439                 assert(h->info.hook_entry[NF_IP_PRE_ROUTING] == 0);
440
441                 n = get_chain_end(h, n);
442                 n += get_entry(h, n)->next_offset;
443                 assert(h->info.hook_entry[NF_IP_LOCAL_OUT] == n);
444
445                 user_offset = h->info.hook_entry[NF_IP_LOCAL_OUT];
446
447 #ifdef NF_IP_DROPPING
448         } else if (strcmp(h->info.name, "drop") == 0) {
449                 assert(h->info.valid_hooks == (1 << NF_IP_DROPPING));
450
451                 /* Hook should be first */
452                 assert(h->info.hook_entry[NF_IP_DROPPING] == 0);
453                 user_offset = 0;
454 #endif
455         } else {
456                 fprintf(stderr, "Unknown table `%s'\n", h->info.name);
457                 abort();
458         }
459
460         /* User chain == end of last builtin + policy entry */
461         user_offset = get_chain_end(h, user_offset);
462         user_offset += get_entry(h, user_offset)->next_offset;
463
464         /* Overflows should be end of entry chains, and unconditional
465            policy nodes. */
466         for (i = 0; i < NUMHOOKS; i++) {
467                 STRUCT_ENTRY *e;
468                 STRUCT_STANDARD_TARGET *t;
469
470                 if (!(h->info.valid_hooks & (1 << i)))
471                         continue;
472                 assert(h->info.underflow[i]
473                        == get_chain_end(h, h->info.hook_entry[i]));
474
475                 e = get_entry(h, get_chain_end(h, h->info.hook_entry[i]));
476                 assert(unconditional(&e->ip));
477                 assert(e->target_offset == sizeof(*e));
478                 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
479                 assert(t->target.u.target_size == ALIGN(sizeof(*t)));
480                 assert(e->next_offset == sizeof(*e) + ALIGN(sizeof(*t)));
481
482                 assert(strcmp(t->target.u.user.name, STANDARD_TARGET)==0);
483                 assert(t->verdict == -NF_DROP-1 || t->verdict == -NF_ACCEPT-1);
484
485                 /* Hooks and underflows must be valid entries */
486                 entry2index(h, get_entry(h, h->info.hook_entry[i]));
487                 entry2index(h, get_entry(h, h->info.underflow[i]));
488         }
489
490         assert(h->info.size
491                >= h->info.num_entries * (sizeof(STRUCT_ENTRY)
492                                          +sizeof(STRUCT_STANDARD_TARGET)));
493
494         assert(h->entries.size
495                >= (h->new_number
496                    * (sizeof(STRUCT_ENTRY)
497                       + sizeof(STRUCT_STANDARD_TARGET))));
498         assert(strcmp(h->info.name, h->entries.name) == 0);
499
500         i = 0; n = 0;
501         was_return = 0;
502         /* Check all the entries. */
503         ENTRY_ITERATE(h->entries.entrytable, h->entries.size,
504                       check_entry, &i, &n, user_offset, &was_return, h);
505
506         assert(i == h->new_number);
507         assert(n == h->entries.size);
508
509         /* Final entry must be error node */
510         assert(strcmp(GET_TARGET(index2entry(h, h->new_number-1))
511                       ->u.user.name,
512                       ERROR_TARGET) == 0);
513 }
514 #endif /*IPTC_DEBUG*/
515
516 #endif