iptables-test: Inversion 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, struct xtables_match *xt_m)
317 {
318         struct ipt_entry *new_entry;
319         size_t match_size, target_size;
320         struct xt_entry_match *entry_match;
321         struct xt_standard_target *target;
322
323
324         if (xt_m)
325                 match_size = xt_m->m->u.match_size;
326         else
327                 match_size = 0;
328
329         target_size = ALIGN(sizeof(struct xt_standard_target));
330
331         new_entry = g_try_malloc0(sizeof(struct ipt_entry) + target_size +
332                                                                 match_size);
333         if (new_entry == NULL)
334                 return NULL;
335
336         new_entry->target_offset = sizeof(struct ipt_entry) + match_size;
337         new_entry->next_offset = sizeof(struct ipt_entry) + target_size +
338                                                                 match_size;
339
340         if (xt_m) {
341                 entry_match = (struct xt_entry_match *)new_entry->elems;
342                 memcpy(entry_match, xt_m->m, match_size);
343         }
344
345         target = (struct xt_standard_target *)(new_entry->elems + match_size);
346         strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
347         target->target.u.user.target_size =
348                                 ALIGN(sizeof(struct ipt_standard_target));
349         target->verdict = target_to_verdict(target_name);
350
351         return new_entry;
352 }
353
354 static struct ipt_entry *
355 new_custom_rule(struct xtables_target *xt_t, struct xtables_match *xt_m)
356 {
357         return NULL;
358 }
359
360 static struct ipt_entry *
361 new_rule(char *target_name, struct xtables_target *xt_t,
362                 char *match_name, struct xtables_match *xt_m)
363 {
364         struct ipt_entry *new_entry;
365
366         if (is_builtin_target(target_name))
367                 new_entry = new_builtin_rule(target_name, xt_m);
368         else
369                 new_entry = new_custom_rule(xt_t, xt_m);
370
371         return new_entry;
372 }
373
374 static int
375 connman_iptables_add_rule(struct connman_iptables *table, char *chain_name,
376                                 char *target_name, struct xtables_target *xt_t,
377                                 char *match_name, struct xtables_match *xt_m)
378 {
379         GList *chain_tail;
380         struct ipt_entry *new_entry;
381
382         chain_tail = find_chain_tail(table, chain_name);
383         if (chain_tail == NULL)
384                 return -EINVAL;
385
386         new_entry = new_rule(target_name, xt_t,
387                                 match_name, xt_m);
388         if (new_entry == NULL)
389                 return -EINVAL;
390
391         return connman_add_entry(table, new_entry, chain_tail->prev);
392 }
393
394 static struct ipt_replace *
395 connman_iptables_blob(struct connman_iptables *table)
396 {
397         struct ipt_replace *r;
398         GList *list;
399         struct connman_iptables_entry *e;
400         unsigned char *entry_index;
401
402         r = g_try_malloc0(sizeof(struct ipt_replace) + table->size);
403         if (r == NULL)
404                 return NULL;
405
406         memset(r, 0, sizeof(*r) + table->size);
407
408         r->counters = g_try_malloc0(sizeof(struct xt_counters)
409                                 * table->num_entries);
410         if (r->counters == NULL) {
411                 g_free(r);
412                 return NULL;
413         }
414
415         strcpy(r->name, table->info->name);
416         r->num_entries = table->num_entries;
417         r->size = table->size;
418
419         r->num_counters = table->old_entries;
420         r->valid_hooks  = table->info->valid_hooks;
421
422         memcpy(r->hook_entry, table->info->hook_entry,
423                                 sizeof(table->info->hook_entry));
424         memcpy(r->underflow, table->info->underflow,
425                                 sizeof(table->info->underflow));
426
427         entry_index = (unsigned char *)r->entries;
428         for (list = table->entries; list; list = list->next) {
429                 e = list->data;
430
431                 memcpy(entry_index, e->entry, e->entry->next_offset);
432                 entry_index += e->entry->next_offset;
433         }
434
435         return r;
436 }
437
438 static void dump_target(struct connman_iptables *table,
439                                 struct ipt_entry *entry)
440
441 {
442         struct xtables_target *xt_t;
443         struct xt_entry_target *target;
444
445         target = ipt_get_target(entry);
446
447         if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
448                 struct xt_standard_target *t;
449
450                 t = (struct xt_standard_target *)target;
451
452                 switch (t->verdict) {
453                 case XT_RETURN:
454                         printf("\ttarget RETURN\n");
455                         break;
456
457                 case -NF_ACCEPT - 1:
458                         printf("\ttarget ACCEPT\n");
459                         break;
460
461                 case -NF_DROP - 1:
462                         printf("\ttarget DROP\n");
463                         break;
464
465                 case -NF_QUEUE - 1:
466                         printf("\ttarget QUEUE\n");
467                         break;
468
469                 case -NF_STOP - 1:
470                         printf("\ttarget STOP\n");
471                         break;
472
473                 default:
474                         printf("\tJUMP @%p (0x%x)\n",
475                                 (char*)table->blob_entries->entrytable +
476                                 t->verdict, t->verdict);
477                         break;
478                 }
479
480                 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
481                                                 XTF_LOAD_MUST_SUCCEED);
482
483                 if(xt_t->print != NULL)
484                         xt_t->print(NULL, target, 1);
485         } else {
486                 printf("\ttarget %s\n", target->u.user.name);
487
488                 xt_t = xtables_find_target(target->u.user.name, XTF_TRY_LOAD);
489                 if (xt_t == NULL)
490                         return;
491
492                 if(xt_t->print != NULL)
493                         xt_t->print(NULL, target, 1);
494         }
495 }
496
497 static void dump_match(struct connman_iptables *table, struct ipt_entry *entry)
498 {
499         struct xtables_match *xt_m;
500         struct xt_entry_match *match;
501
502         match = (struct xt_entry_match *) entry->elems;
503
504         if (!strlen(match->u.user.name))
505                 return;
506
507         xt_m = xtables_find_match(match->u.user.name, XTF_TRY_LOAD, NULL);
508         if (xt_m == NULL)
509                 goto out;
510
511         if(xt_m->print != NULL) {
512                 printf("\tmatch ");
513                 xt_m->print(NULL, match, 1);
514                 printf("\n");
515
516                 return;
517         }
518
519 out:
520         printf("\tmatch %s\n", match->u.user.name);
521
522 }
523
524 static int connman_iptables_dump_entry(struct ipt_entry *entry,
525                                         struct connman_iptables *table)
526 {
527         struct xt_entry_target *target;
528         unsigned int offset;
529         int builtin;
530
531         offset = (char *)entry - (char *)table->blob_entries->entrytable;
532         target = ipt_get_target(entry);
533         builtin = is_hook_entry(table, entry);
534
535         if (entry_to_offset(table, entry) + entry->next_offset ==
536                                         table->blob_entries->size) {
537                 printf("End of CHAIN 0x%x\n", offset);
538                 return 0;
539         }
540
541         if (!strcmp(target->u.user.name, IPT_ERROR_TARGET)) {
542                 printf("USER CHAIN (%s) %p  match %p  target %p  size %d\n",
543                         target->data, entry, entry->elems,
544                         (char *)entry + entry->target_offset,
545                                 entry->next_offset);
546
547                 return 0;
548         } else if (builtin >= 0) {
549                 printf("CHAIN (%s) %p  match %p  target %p  size %d\n",
550                         hooknames[builtin], entry, entry->elems,
551                         (char *)entry + entry->target_offset,
552                                 entry->next_offset);
553         } else {
554                 printf("RULE %p  match %p  target %p  size %d\n", entry,
555                         entry->elems,
556                         (char *)entry + entry->target_offset,
557                                 entry->next_offset);
558         }
559
560         dump_match(table, entry);
561         dump_target(table, entry);
562
563         return 0;
564 }
565
566 static void connman_iptables_dump(struct connman_iptables *table)
567 {
568         printf("%s valid_hooks=0x%08x, num_entries=%u, size=%u\n",
569                 table->info->name,
570                 table->info->valid_hooks, table->info->num_entries,
571                 table->info->size);
572
573         ENTRY_ITERATE(table->blob_entries->entrytable,
574                         table->blob_entries->size,
575                         connman_iptables_dump_entry, table);
576
577 }
578
579 static int connman_iptables_get_entries(struct connman_iptables *table)
580 {
581         socklen_t entry_size;
582
583         entry_size = sizeof(struct ipt_get_entries) + table->info->size;
584
585         return getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_ENTRIES,
586                                 table->blob_entries, &entry_size);
587 }
588
589 static int connman_iptables_replace(struct connman_iptables *table,
590                                         struct ipt_replace *r)
591 {
592         return setsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_SET_REPLACE, r,
593                          sizeof(*r) + r->size);
594 }
595
596 static void connman_iptables_cleanup(struct connman_iptables *table)
597 {
598         close(table->ipt_sock);
599         g_free(table->info);
600         g_free(table->blob_entries);
601         g_free(table);
602
603         xtables_free_opts(1);
604 }
605
606 static int connman_iptables_commit(struct connman_iptables *table)
607 {
608         struct ipt_replace *repl;
609
610         repl = connman_iptables_blob(table);
611
612         return connman_iptables_replace(table, repl);
613 }
614
615 static int add_entry(struct ipt_entry *entry, struct connman_iptables *table)
616 {
617         return connman_add_entry(table, entry, NULL);
618 }
619
620 static struct connman_iptables *connman_iptables_init(const char *table_name)
621 {
622         struct connman_iptables *table;
623         socklen_t s;
624
625         table =  g_try_new0(struct connman_iptables, 1);
626         if (table == NULL)
627                 return NULL;
628
629         table->info =  g_try_new0(struct ipt_getinfo, 1);
630         if (table->info == NULL)
631                 goto err;
632
633         table->ipt_sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
634         if (table->ipt_sock < 0)
635                 goto err;
636
637         s = sizeof(*table->info);
638         strcpy(table->info->name, table_name);
639         if (getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_INFO,
640                                                 table->info, &s) < 0)
641                 goto err;
642
643         table->blob_entries = g_try_malloc0(sizeof(struct ipt_get_entries) +
644                                                 table->info->size);
645         if (table->blob_entries == NULL)
646                 goto err;
647
648         strcpy(table->blob_entries->name, table_name);
649         table->blob_entries->size = table->info->size;
650
651         if (connman_iptables_get_entries(table) < 0)
652                 goto err;
653
654         table->num_entries = 0;
655         table->old_entries = table->info->num_entries;
656         table->size = 0;
657
658         ENTRY_ITERATE(table->blob_entries->entrytable,
659                         table->blob_entries->size,
660                                 add_entry, table);
661
662
663         return table;
664
665 err:
666
667         connman_iptables_cleanup(table);
668
669         return NULL;
670 }
671
672
673 static struct option connman_iptables_opts[] = {
674         {.name = "append",        .has_arg = 1, .val = 'A'},
675         {.name = "list",          .has_arg = 2, .val = 'L'},
676         {.name = "new-chain",     .has_arg = 1, .val = 'N'},
677         {.name = "in-interface",  .has_arg = 1, .val = 'i'},
678         {.name = "jump",          .has_arg = 1, .val = 'j'},
679         {.name = "match",         .has_arg = 1, .val = 'm'},
680         {.name = "out-interface", .has_arg = 1, .val = 'o'},
681         {.name = "table",         .has_arg = 1, .val = 't'},
682         {NULL},
683 };
684
685 struct xtables_globals connman_iptables_globals = {
686         .option_offset = 0,
687         .opts = connman_iptables_opts,
688         .orig_opts = connman_iptables_opts,
689 };
690
691 int main(int argc, char *argv[])
692 {
693         struct connman_iptables *table;
694         struct xtables_match *xt_m;
695         struct xtables_target *xt_t;
696         char *table_name, *chain, *new_chain, *match_name, *target_name;
697         int c;
698         size_t size;
699         gboolean dump, invert;
700
701         xtables_init_all(&connman_iptables_globals, NFPROTO_IPV4);
702
703         dump = FALSE;
704         invert = FALSE;
705         table_name = chain = new_chain = match_name = target_name = NULL;
706         table = NULL;
707         xt_m = NULL;
708         xt_t = NULL;
709
710         while ((c = getopt_long(argc, argv,
711            "-A:L::N:j:i:m:o:t:", connman_iptables_globals.opts, NULL)) != -1) {
712                 switch (c) {
713                 case 'A':
714                         chain = optarg;
715                         break;
716
717                 case 'L':
718                         dump = TRUE;
719                         break;
720
721                 case 'N':
722                         new_chain = optarg;
723                         break;
724
725                 case 'j':
726                         target_name = optarg;
727                         xt_t = xtables_find_target(target_name, XTF_TRY_LOAD);
728
729                         if (xt_t == NULL)
730                                 break;
731
732                         size = ALIGN(sizeof(struct ipt_entry_target)) + xt_t->size;
733
734                         xt_t->t = g_try_malloc0(size);
735                         if (xt_t->t == NULL)
736                                 goto out;
737                         xt_t->t->u.target_size = size;
738                         strcpy(xt_t->t->u.user.name, target_name);
739                         xt_t->t->u.user.revision = xt_t->revision;
740                         if (xt_t->init != NULL)
741                                 xt_t->init(xt_t->t);
742                         connman_iptables_globals.opts =
743                                 xtables_merge_options(connman_iptables_globals.opts,
744                                                      xt_t->extra_opts,
745                                                      &xt_t->option_offset);
746                         if (connman_iptables_globals.opts == NULL)
747                                 goto out;
748
749                         break;
750
751                 case 'i':
752                         break;
753
754                 case 'm':
755                         match_name = optarg;
756
757                         xt_m = xtables_find_match(optarg, XTF_LOAD_MUST_SUCCEED, NULL);
758                         size = ALIGN(sizeof(struct ipt_entry_match)) + xt_m->size;
759                         xt_m->m = g_try_malloc0(size);
760                         if (xt_m == NULL)
761                                 goto out;
762                         xt_m->m->u.match_size = size;
763                         strcpy(xt_m->m->u.user.name, xt_m->name);
764                         xt_m->m->u.user.revision = xt_m->revision;
765                         if (xt_m->init != NULL)
766                                 xt_m->init(xt_m->m);
767                         if (xt_m != xt_m->next) {
768                                 connman_iptables_globals.opts =
769                                         xtables_merge_options(connman_iptables_globals.opts,
770                                                 xt_m->extra_opts,
771                                                 &xt_m->option_offset);
772                                 if (connman_iptables_globals.opts == NULL)
773                                         goto out;
774                         }
775
776                         break;
777
778                 case 'o':
779                         break;
780
781                 case 't':
782                         table_name = optarg;
783                         break;
784
785                 case 1:
786                         if (optarg[0] == '!' && optarg[1] == '\0') {
787                                 if (invert)
788                                         printf("Consecutive ! not allowed\n");
789
790                                 invert = TRUE;
791                                 optarg[0] = '\0';
792                                 continue;
793                         }
794
795                         printf("Invalid option\n");
796
797                         return -1;
798
799                 default:
800                         if (xt_t == NULL || xt_t->parse == NULL ||
801                             !xt_t->parse(c - xt_t->option_offset, argv, invert,
802                                         &xt_t->tflags, NULL, &xt_t->t)) {
803                                 if (xt_m == NULL || xt_m->parse == NULL)
804                                         break;
805
806                                 xt_m->parse(c - xt_m->option_offset, argv,
807                                         invert, &xt_m->mflags, NULL, &xt_m->m);
808                         }
809
810                         break;
811                 }
812         }
813
814         if (table_name == NULL)
815                 table_name = "filter";
816
817         table = connman_iptables_init(table_name);
818         if (table == NULL)
819                 return -1;
820
821         if (dump) {
822                 connman_iptables_dump(table);
823
824                 return 0;
825         }
826
827         if (chain && new_chain)
828                 return -1;
829
830         if (new_chain) {
831                 printf("New chain %s\n", new_chain);
832
833                 connman_iptables_add_chain(table, new_chain);
834
835                 goto commit;
836         }
837
838         if (chain) {
839                 if (target_name == NULL)
840                         return -1;
841
842                 printf("Adding %s to %s (match %s)\n", target_name, chain, match_name);
843
844                 connman_iptables_add_rule(table, chain, target_name, xt_t,
845                                         match_name, xt_m);
846
847                 goto commit;
848         }
849
850 commit:
851
852         connman_iptables_commit(table);
853
854 out:
855         connman_iptables_cleanup(table);
856
857         if (xt_t)
858                 g_free(xt_t->t);
859
860         if (xt_m)
861                 g_free(xt_m->m);
862
863         return 0;
864 }