iptables-test: Add -t option support
[framework/connectivity/connman.git] / tools / iptables-test.c
1 /*
2  *  Connection Manager
3  *
4  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License version 2 as
8  *  published by the Free Software Foundation.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  */
20
21 #include <getopt.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/errno.h>
27 #include <sys/socket.h>
28 #include <xtables.h>
29
30 #include <linux/netfilter_ipv4/ip_tables.h>
31
32 #include <glib.h>
33
34 static const char *hooknames[] = {
35         [NF_IP_PRE_ROUTING]     = "PREROUTING",
36         [NF_IP_LOCAL_IN]        = "INPUT",
37         [NF_IP_FORWARD]         = "FORWARD",
38         [NF_IP_LOCAL_OUT]       = "OUTPUT",
39         [NF_IP_POST_ROUTING]    = "POSTROUTING",
40 };
41
42 #define LABEL_ACCEPT  "ACCEPT"
43 #define LABEL_DROP    "DROP"
44 #define LABEL_QUEUE   "QUEUE"
45 #define LABEL_RETURN  "RETURN"
46
47 /* fn returns 0 to continue iteration */
48 #define _XT_ENTRY_ITERATE_CONTINUE(type, entries, size, n, fn, args...) \
49 ({                                                              \
50         unsigned int __i;                                       \
51         int __n;                                                \
52         int __ret = 0;                                          \
53         type *__entry;                                          \
54                                                                 \
55         for (__i = 0, __n = 0; __i < (size);                    \
56              __i += __entry->next_offset, __n++) {              \
57                 __entry = (void *)(entries) + __i;              \
58                 if (__n < n)                                    \
59                         continue;                               \
60                                                                 \
61                 __ret = fn(__entry,  ## args);                  \
62                 if (__ret != 0)                                 \
63                         break;                                  \
64         }                                                       \
65         __ret;                                                  \
66 })
67
68 /* fn returns 0 to continue iteration */
69 #define _XT_ENTRY_ITERATE(type, entries, size, fn, args...) \
70         _XT_ENTRY_ITERATE_CONTINUE(type, entries, size, 0, fn, args)
71
72 #define ENTRY_ITERATE(entries, size, fn, args...) \
73         _XT_ENTRY_ITERATE(struct ipt_entry, entries, size, fn, ## args)
74
75 #define MIN_ALIGN (__alignof__(struct ipt_entry))
76
77 #define ALIGN(s) (((s) + ((MIN_ALIGN)-1)) & ~((MIN_ALIGN)-1))
78
79 struct ipt_error_target {
80         struct xt_entry_target t;
81         char error[IPT_TABLE_MAXNAMELEN];
82 };
83
84 struct connman_iptables_entry {
85         int builtin;
86         int std_target;
87         int jump_offset;
88
89         struct ipt_entry *entry;
90 };
91
92 struct connman_iptables {
93         int ipt_sock;
94
95         struct ipt_getinfo *info;
96         struct ipt_get_entries *blob_entries;
97
98         unsigned int num_entries;
99         unsigned int old_entries;
100         unsigned int size;
101
102         GList *entries;
103 };
104
105
106 static struct ipt_entry *get_entry(struct connman_iptables *table,
107                                         unsigned int offset)
108 {
109         return (struct ipt_entry *)((char *)table->blob_entries->entrytable +
110                                                                         offset);
111 }
112
113 static int is_hook_entry(struct connman_iptables *table,
114                                 struct ipt_entry *entry)
115 {
116         unsigned int i;
117
118         for (i = 0; i < NF_INET_NUMHOOKS; i++) {
119                 if ((table->info->valid_hooks & (1 << i))
120                 && get_entry(table, table->info->hook_entry[i]) == entry)
121                         return i;
122         }
123
124         return -1;
125 }
126
127 static unsigned long entry_to_offset(struct connman_iptables *table,
128                                         struct ipt_entry *entry)
129 {
130         return (void *)entry - (void *)table->blob_entries->entrytable;
131 }
132
133 static int target_to_verdict(char *target_name)
134 {
135         if (!strcmp(target_name, LABEL_ACCEPT))
136                 return -NF_ACCEPT - 1;
137
138         if (!strcmp(target_name, LABEL_DROP))
139                 return -NF_DROP - 1;
140
141         if (!strcmp(target_name, LABEL_QUEUE))
142                 return -NF_QUEUE - 1;
143
144         if (!strcmp(target_name, LABEL_RETURN))
145                 return XT_RETURN;
146
147         return 0;
148 }
149
150 static gboolean is_builtin_target(char *target_name)
151 {
152         if (!strcmp(target_name, LABEL_ACCEPT) ||
153                 !strcmp(target_name, LABEL_DROP) ||
154                 !strcmp(target_name, LABEL_QUEUE) ||
155                 !strcmp(target_name, LABEL_RETURN))
156                 return TRUE;
157
158         return FALSE;
159 }
160
161 static gboolean is_chain(struct connman_iptables *table,
162                                 struct connman_iptables_entry *e)
163 {
164         int builtin;
165         struct ipt_entry *entry;
166         struct xt_entry_target *target;
167
168         entry = e->entry;
169         builtin = is_hook_entry(table, entry);
170         if (builtin >= 0)
171                 return TRUE;
172
173         target = ipt_get_target(entry);
174         if (!strcmp(target->u.user.name, IPT_ERROR_TARGET))
175                 return TRUE;
176
177         return FALSE;
178 }
179
180 static GList *find_chain_tail(struct connman_iptables *table,
181                                 char *chain_name)
182 {
183         GList *chain_head, *list;
184         struct connman_iptables_entry *head, *tail;
185         struct ipt_entry *entry;
186         struct xt_entry_target *target;
187         int builtin;
188
189         /* First we look for the head */
190         for (list = table->entries; list; list = list->next) {
191                 head = list->data;
192                 entry = head->entry;
193
194                 /* Buit-in chain */
195                 builtin = is_hook_entry(table, entry);
196                 if (builtin >= 0 && !strcmp(hooknames[builtin], chain_name))
197                         break;
198
199                 /* User defined chain */
200                 target = ipt_get_target(entry);
201                 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET) &&
202                     !strcmp((char *)target->data, chain_name))
203                         break;
204         }
205
206         if (list == NULL)
207                 return NULL;
208
209         chain_head = list;
210
211         /* Then we look for the next chain */
212         for (list = chain_head->next; list; list = list->next) {
213                 tail = list->data;
214                 entry = tail->entry;
215
216                 if (is_chain(table, tail))
217                         return list;
218         }
219
220         /* Nothing found, we return the table end */
221         return g_list_last(table->entries);
222 }
223
224 static int connman_add_entry(struct connman_iptables *table,
225                                 struct ipt_entry *entry, GList *before)
226 {
227         struct connman_iptables_entry *e;
228
229         if (table == NULL)
230                 return -1;
231
232         e = g_try_malloc0(sizeof(struct connman_iptables_entry));
233         if (e == NULL)
234                 return -1;
235
236         e->entry = entry;
237
238         table->entries = g_list_insert_before(table->entries, before, e);
239         table->num_entries++;
240         table->size += entry->next_offset;
241
242         return 0;
243 }
244
245 static int connman_iptables_add_chain(struct connman_iptables *table,
246                                         char *name)
247 {
248         GList *last;
249         struct ipt_entry *entry_head;
250         struct ipt_entry *entry_return;
251         struct ipt_error_target *error;
252         struct ipt_standard_target *standard;
253         u_int16_t entry_head_size, entry_return_size;
254
255         last = g_list_last(table->entries);
256
257         /*
258          * An empty chain is composed of:
259          * - A head entry, with no match and an error target.
260          *   The error target data is the chain name.
261          * - A tail entry, with no match and a standard target.
262          *   The standard target verdict is XT_RETURN (return to the
263          *   caller).
264          */
265
266         /* head entry */
267         entry_head_size = sizeof(struct ipt_entry) +
268                                 sizeof(struct ipt_error_target);
269         entry_head = g_try_malloc0(entry_head_size);
270         if (entry_head == NULL)
271                 goto err;
272
273         memset(entry_head, 0, entry_head_size);
274
275         entry_head->target_offset = sizeof(struct ipt_entry);
276         entry_head->next_offset = entry_head_size;
277
278         error = (struct ipt_error_target *) entry_head->elems;
279         strcpy(error->t.u.user.name, IPT_ERROR_TARGET);
280         error->t.u.user.target_size = ALIGN(sizeof(struct ipt_error_target));
281         strcpy(error->error, name);
282
283         if (connman_add_entry(table, entry_head, last) < 0)
284                 goto err;
285
286         /* tail entry */
287         entry_return_size = sizeof(struct ipt_entry) +
288                                 sizeof(struct ipt_standard_target);
289         entry_return = g_try_malloc0(entry_return_size);
290         if (entry_return == NULL)
291                 goto err;
292
293         memset(entry_return, 0, entry_return_size);
294
295         entry_return->target_offset = sizeof(struct ipt_entry);
296         entry_return->next_offset = entry_return_size;
297
298         standard = (struct ipt_standard_target *) entry_return->elems;
299         standard->target.u.user.target_size =
300                                 ALIGN(sizeof(struct ipt_standard_target));
301         standard->verdict = XT_RETURN;
302
303         if (connman_add_entry(table, entry_return, last) < 0)
304                 goto err;
305
306         return 0;
307
308 err:
309         g_free(entry_head);
310         g_free(entry_return);
311
312         return -ENOMEM;
313 }
314
315 static struct ipt_entry *
316 new_builtin_rule(char *target_name,
317                 char *match_name, int match_argc, char **match_argv)
318 {
319         struct ipt_entry *new_entry;
320         size_t match_size, target_size;
321         struct xtables_match *xt_m;
322         struct xt_standard_target *target;
323
324         xt_m = NULL;
325         match_size = 0;
326
327         if (match_name) {
328                 xt_m = xtables_find_match(match_name, XTF_TRY_LOAD, NULL);
329                 if (xt_m == NULL)
330                         return NULL;
331
332                 match_size = ALIGN(sizeof(struct xt_entry_match)) + xt_m->size;
333         }
334
335         target_size = ALIGN(sizeof(struct xt_standard_target));
336
337         new_entry = g_try_malloc0(sizeof(struct ipt_entry) + target_size +
338                                                                 match_size);
339         if (new_entry == NULL)
340                 return NULL;
341
342         new_entry->target_offset = sizeof(struct ipt_entry) + match_size;
343         new_entry->next_offset = sizeof(struct ipt_entry) + target_size +
344                                                                 match_size;
345
346         if (xt_m) {
347                 struct xt_entry_match *entry_match;
348
349                 entry_match = (struct xt_entry_match *)new_entry->elems;
350                 entry_match->u.match_size = match_size;
351                 strcpy(entry_match->u.user.name, xt_m->name);
352                 entry_match->u.user.revision = xt_m->revision;
353                 if (xt_m->init != NULL)
354                         xt_m->init(entry_match);
355         }
356
357         target = (struct xt_standard_target *)(new_entry->elems + match_size);
358         strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
359         target->target.u.user.target_size =
360                                 ALIGN(sizeof(struct ipt_standard_target));
361         target->verdict = target_to_verdict(target_name);
362
363         return new_entry;
364 }
365
366 static struct ipt_entry *
367 new_custom_rule(char *target_name, int target_argc, char **target_argv,
368                 char *match_name, int match_argc, char **match_argv)
369 {
370         return NULL;
371 }
372
373 static struct ipt_entry *
374 new_rule(char *target_name, int target_argc, char **target_argv,
375                 char *match_name, int match_argc, char **match_argv)
376 {
377         struct ipt_entry *new_entry;
378
379         if (is_builtin_target(target_name))
380                 new_entry = new_builtin_rule(target_name,
381                                         match_name, match_argc, match_argv);
382         else
383                 new_entry = new_custom_rule(target_name,
384                                         target_argc, target_argv,
385                                         match_name, match_argc, match_argv);
386
387         return new_entry;
388 }
389
390 static int
391 connman_iptables_add_rule(struct connman_iptables *table, char *chain_name,
392                         char *target_name, int target_argc, char **target_argv,
393                         char *match_name, int match_argc, char **match_argv)
394 {
395         GList *chain_tail;
396         struct ipt_entry *new_entry;
397
398         chain_tail = find_chain_tail(table, chain_name);
399         if (chain_tail == NULL)
400                 return -EINVAL;
401
402         new_entry = new_rule(target_name, target_argc, target_argv,
403                                 match_name, match_argc, match_argv);
404         if (new_entry == NULL)
405                 return -EINVAL;
406
407         return connman_add_entry(table, new_entry, chain_tail->prev);
408 }
409
410 static struct ipt_replace *
411 connman_iptables_blob(struct connman_iptables *table)
412 {
413         struct ipt_replace *r;
414         GList *list;
415         struct connman_iptables_entry *e;
416         unsigned char *entry_index;
417
418         r = g_try_malloc0(sizeof(struct ipt_replace) + table->size);
419         if (r == NULL)
420                 return NULL;
421
422         memset(r, 0, sizeof(*r) + table->size);
423
424         r->counters = g_try_malloc0(sizeof(struct xt_counters)
425                                 * table->num_entries);
426         if (r->counters == NULL) {
427                 g_free(r);
428                 return NULL;
429         }
430
431         strcpy(r->name, table->info->name);
432         r->num_entries = table->num_entries;
433         r->size = table->size;
434
435         r->num_counters = table->old_entries;
436         r->valid_hooks  = table->info->valid_hooks;
437
438         memcpy(r->hook_entry, table->info->hook_entry,
439                                 sizeof(table->info->hook_entry));
440         memcpy(r->underflow, table->info->underflow,
441                                 sizeof(table->info->underflow));
442
443         entry_index = (unsigned char *)r->entries;
444         for (list = table->entries; list; list = list->next) {
445                 e = list->data;
446
447                 memcpy(entry_index, e->entry, e->entry->next_offset);
448                 entry_index += e->entry->next_offset;
449         }
450
451         return r;
452 }
453
454 static void dump_target(struct connman_iptables *table,
455                                 struct ipt_entry *entry)
456
457 {
458         struct xtables_target *xt_t;
459         struct xt_entry_target *target;
460
461         target = ipt_get_target(entry);
462
463         if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
464                 struct xt_standard_target *t;
465
466                 t = (struct xt_standard_target *)target;
467
468                 switch (t->verdict) {
469                 case XT_RETURN:
470                         printf("\ttarget RETURN\n");
471                         break;
472
473                 case -NF_ACCEPT - 1:
474                         printf("\ttarget ACCEPT\n");
475                         break;
476
477                 case -NF_DROP - 1:
478                         printf("\ttarget DROP\n");
479                         break;
480
481                 case -NF_QUEUE - 1:
482                         printf("\ttarget QUEUE\n");
483                         break;
484
485                 case -NF_STOP - 1:
486                         printf("\ttarget STOP\n");
487                         break;
488
489                 default:
490                         printf("\tJUMP @%p (0x%x)\n",
491                                 (char*)table->blob_entries->entrytable +
492                                 t->verdict, t->verdict);
493                         break;
494                 }
495
496                 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
497                                                 XTF_LOAD_MUST_SUCCEED);
498
499                 if(xt_t->print != NULL)
500                         xt_t->print(NULL, target, 1);
501         } else {
502                 printf("\ttarget %s\n", target->u.user.name);
503
504                 xt_t = xtables_find_target(target->u.user.name, XTF_TRY_LOAD);
505                 if (xt_t == NULL)
506                         return;
507
508                 if(xt_t->print != NULL)
509                         xt_t->print(NULL, target, 1);
510         }
511 }
512
513 static void dump_match(struct connman_iptables *table, struct ipt_entry *entry)
514 {
515         struct xtables_match *xt_m;
516         struct xt_entry_match *match;
517
518         match = (struct xt_entry_match *) entry->elems;
519
520         if (!strlen(match->u.user.name))
521                 return;
522
523         xt_m = xtables_find_match(match->u.user.name, XTF_TRY_LOAD, NULL);
524         if (xt_m == NULL)
525                 goto out;
526
527         if(xt_m->print != NULL) {
528                 printf("\tmatch ");
529                 xt_m->print(NULL, match, 1);
530                 printf("\n");
531
532                 return;
533         }
534
535 out:
536         printf("\tmatch %s\n", match->u.user.name);
537
538 }
539
540 static int connman_iptables_dump_entry(struct ipt_entry *entry,
541                                         struct connman_iptables *table)
542 {
543         struct xt_entry_target *target;
544         unsigned int offset;
545         int builtin;
546
547         offset = (char *)entry - (char *)table->blob_entries->entrytable;
548         target = ipt_get_target(entry);
549         builtin = is_hook_entry(table, entry);
550
551         if (entry_to_offset(table, entry) + entry->next_offset ==
552                                         table->blob_entries->size) {
553                 printf("End of CHAIN 0x%x\n", offset);
554                 return 0;
555         }
556
557         if (!strcmp(target->u.user.name, IPT_ERROR_TARGET)) {
558                 printf("USER CHAIN (%s) %p  match %p  target %p  size %d\n",
559                         target->data, entry, entry->elems,
560                         (char *)entry + entry->target_offset,
561                                 entry->next_offset);
562
563                 return 0;
564         } else if (builtin >= 0) {
565                 printf("CHAIN (%s) %p  match %p  target %p  size %d\n",
566                         hooknames[builtin], entry, entry->elems,
567                         (char *)entry + entry->target_offset,
568                                 entry->next_offset);
569         } else {
570                 printf("RULE %p  match %p  target %p  size %d\n", entry,
571                         entry->elems,
572                         (char *)entry + entry->target_offset,
573                                 entry->next_offset);
574         }
575
576         dump_match(table, entry);
577         dump_target(table, entry);
578
579         return 0;
580 }
581
582 static void connman_iptables_dump(struct connman_iptables *table)
583 {
584         printf("%s valid_hooks=0x%08x, num_entries=%u, size=%u\n",
585                 table->info->name,
586                 table->info->valid_hooks, table->info->num_entries,
587                 table->info->size);
588
589         ENTRY_ITERATE(table->blob_entries->entrytable,
590                         table->blob_entries->size,
591                         connman_iptables_dump_entry, table);
592
593 }
594
595 static int connman_iptables_get_entries(struct connman_iptables *table)
596 {
597         socklen_t entry_size;
598
599         entry_size = sizeof(struct ipt_get_entries) + table->info->size;
600
601         return getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_ENTRIES,
602                                 table->blob_entries, &entry_size);
603 }
604
605 static int connman_iptables_replace(struct connman_iptables *table,
606                                         struct ipt_replace *r)
607 {
608         return setsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_SET_REPLACE, r,
609                          sizeof(*r) + r->size);
610 }
611
612 static void connman_iptables_cleanup(struct connman_iptables *table)
613 {
614         close(table->ipt_sock);
615         g_free(table->info);
616         g_free(table->blob_entries);
617         g_free(table);
618 }
619
620 static int connman_iptables_commit(struct connman_iptables *table)
621 {
622         struct ipt_replace *repl;
623
624         repl = connman_iptables_blob(table);
625
626         return connman_iptables_replace(table, repl);
627 }
628
629 static int add_entry(struct ipt_entry *entry, struct connman_iptables *table)
630 {
631         return connman_add_entry(table, entry, NULL);
632 }
633
634 static struct connman_iptables *connman_iptables_init(const char *table_name)
635 {
636         struct connman_iptables *table;
637         socklen_t s;
638
639         table =  g_try_new0(struct connman_iptables, 1);
640         if (table == NULL)
641                 return NULL;
642
643         table->info =  g_try_new0(struct ipt_getinfo, 1);
644         if (table->info == NULL)
645                 goto err;
646
647         table->ipt_sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
648         if (table->ipt_sock < 0)
649                 goto err;
650
651         s = sizeof(*table->info);
652         strcpy(table->info->name, table_name);
653         if (getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_INFO,
654                                                 table->info, &s) < 0)
655                 goto err;
656
657         table->blob_entries = g_try_malloc0(sizeof(struct ipt_get_entries) +
658                                                 table->info->size);
659         if (table->blob_entries == NULL)
660                 goto err;
661
662         strcpy(table->blob_entries->name, table_name);
663         table->blob_entries->size = table->info->size;
664
665         if (connman_iptables_get_entries(table) < 0)
666                 goto err;
667
668         table->num_entries = 0;
669         table->old_entries = table->info->num_entries;
670         table->size = 0;
671
672         ENTRY_ITERATE(table->blob_entries->entrytable,
673                         table->blob_entries->size,
674                                 add_entry, table);
675
676
677         return table;
678
679 err:
680
681         connman_iptables_cleanup(table);
682
683         return NULL;
684 }
685
686
687 static struct option connman_iptables_opts[] = {
688         {.name = "append",        .has_arg = 1, .val = 'A'},
689         {.name = "list",          .has_arg = 2, .val = 'L'},
690         {.name = "new-chain",     .has_arg = 1, .val = 'N'},
691         {.name = "in-interface",  .has_arg = 1, .val = 'i'},
692         {.name = "jump",          .has_arg = 1, .val = 'j'},
693         {.name = "match",         .has_arg = 1, .val = 'm'},
694         {.name = "out-interface", .has_arg = 1, .val = 'o'},
695         {.name = "table",         .has_arg = 1, .val = 't'},
696         {NULL},
697 };
698
699 int main(int argc, char *argv[])
700 {
701         struct connman_iptables *table;
702         char *table_name, *chain, *new_chain, *match_name, *target_name;
703         int c;
704         gboolean dump;
705
706         xtables_init();
707         xtables_set_nfproto(NFPROTO_IPV4);
708
709         dump = FALSE;
710         table_name = chain = new_chain = match_name = target_name = NULL;
711
712         while ((c = getopt_long(argc, argv,
713            "-A:L::N:j:i:m:o:t:", connman_iptables_opts, NULL)) != -1) {
714                 switch (c) {
715                 case 'A':
716                         chain = optarg;
717                         break;
718
719                 case 'L':
720                         dump = TRUE;
721                         break;
722
723                 case 'N':
724                         new_chain = optarg;
725                         break;
726
727                 case 'j':
728                         target_name = optarg;
729                         break;
730
731                 case 'i':
732                         break;
733
734                 case 'm':
735                         match_name = optarg;
736                         break;
737
738                 case 'o':
739                         break;
740
741                 case 't':
742                         table_name = optarg;
743                         break;
744
745                 default:
746                         printf("default %s\n", optarg);
747
748                 }
749         }
750
751         if (table_name == NULL)
752                 table_name = "filter";
753
754         table = connman_iptables_init(table_name);
755         if (table == NULL)
756                 return -1;
757
758         if (dump) {
759                 connman_iptables_dump(table);
760
761                 return 0;
762         }
763
764         if (chain && new_chain)
765                 return -1;
766
767         if (new_chain) {
768                 printf("New chain %s\n", new_chain);
769
770                 connman_iptables_add_chain(table, new_chain);
771
772                 goto commit;
773         }
774
775         if (chain) {
776                 if (target_name == NULL)
777                         return -1;
778
779                 printf("Adding %s to %s (match %s)\n", target_name, chain, match_name);
780
781                 connman_iptables_add_rule(table, chain,
782                                         target_name, 0, NULL,
783                                         match_name, 0, NULL);
784
785                 goto commit;
786         }
787
788 commit:
789
790         connman_iptables_commit(table);
791
792         connman_iptables_cleanup(table);
793
794         return 0;
795 }