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