iptables-test: Update entries offsets properly
[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 offset;
86
87         struct ipt_entry *entry;
88 };
89
90 struct connman_iptables {
91         int ipt_sock;
92
93         struct ipt_getinfo *info;
94         struct ipt_get_entries *blob_entries;
95
96         unsigned int num_entries;
97         unsigned int old_entries;
98         unsigned int size;
99
100         GList *entries;
101 };
102
103
104 static struct ipt_entry *get_entry(struct connman_iptables *table,
105                                         unsigned int offset)
106 {
107         return (struct ipt_entry *)((char *)table->blob_entries->entrytable +
108                                                                         offset);
109 }
110
111 static int is_hook_entry(struct connman_iptables *table,
112                                 struct ipt_entry *entry)
113 {
114         unsigned int i;
115
116         for (i = 0; i < NF_INET_NUMHOOKS; i++) {
117                 if ((table->info->valid_hooks & (1 << i))
118                 && get_entry(table, table->info->hook_entry[i]) == entry)
119                         return i;
120         }
121
122         return -1;
123 }
124
125 static unsigned long entry_to_offset(struct connman_iptables *table,
126                                         struct ipt_entry *entry)
127 {
128         return (void *)entry - (void *)table->blob_entries->entrytable;
129 }
130
131 static int target_to_verdict(char *target_name)
132 {
133         if (!strcmp(target_name, LABEL_ACCEPT))
134                 return -NF_ACCEPT - 1;
135
136         if (!strcmp(target_name, LABEL_DROP))
137                 return -NF_DROP - 1;
138
139         if (!strcmp(target_name, LABEL_QUEUE))
140                 return -NF_QUEUE - 1;
141
142         if (!strcmp(target_name, LABEL_RETURN))
143                 return XT_RETURN;
144
145         return 0;
146 }
147
148 static gboolean is_builtin_target(char *target_name)
149 {
150         if (!strcmp(target_name, LABEL_ACCEPT) ||
151                 !strcmp(target_name, LABEL_DROP) ||
152                 !strcmp(target_name, LABEL_QUEUE) ||
153                 !strcmp(target_name, LABEL_RETURN))
154                 return TRUE;
155
156         return FALSE;
157 }
158
159 static gboolean is_chain(struct connman_iptables *table,
160                                 struct connman_iptables_entry *e)
161 {
162         int builtin;
163         struct ipt_entry *entry;
164         struct xt_entry_target *target;
165
166         entry = e->entry;
167         builtin = is_hook_entry(table, entry);
168         if (builtin >= 0)
169                 return TRUE;
170
171         target = ipt_get_target(entry);
172         if (!strcmp(target->u.user.name, IPT_ERROR_TARGET))
173                 return TRUE;
174
175         return FALSE;
176 }
177
178 static GList *find_chain_tail(struct connman_iptables *table,
179                                 char *chain_name)
180 {
181         GList *chain_head, *list;
182         struct connman_iptables_entry *head, *tail;
183         struct ipt_entry *entry;
184         struct xt_entry_target *target;
185         int builtin;
186
187         /* First we look for the head */
188         for (list = table->entries; list; list = list->next) {
189                 head = list->data;
190                 entry = head->entry;
191
192                 /* Buit-in chain */
193                 builtin = is_hook_entry(table, entry);
194                 if (builtin >= 0 && !strcmp(hooknames[builtin], chain_name))
195                         break;
196
197                 /* User defined chain */
198                 target = ipt_get_target(entry);
199                 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET) &&
200                     !strcmp((char *)target->data, chain_name))
201                         break;
202         }
203
204         if (list == NULL)
205                 return NULL;
206
207         chain_head = list;
208
209         /* Then we look for the next chain */
210         for (list = chain_head->next; list; list = list->next) {
211                 tail = list->data;
212                 entry = tail->entry;
213
214                 if (is_chain(table, tail))
215                         return list;
216         }
217
218         /* Nothing found, we return the table end */
219         return g_list_last(table->entries);
220 }
221
222 static void update_offsets(struct connman_iptables *table)
223 {
224         GList *list, *prev;
225         struct connman_iptables_entry *entry, *prev_entry;
226
227         for (list = table->entries; list; list = list->next) {
228                 entry = list->data;
229
230                 if (list == table->entries) {
231                         entry->offset = 0;
232
233                         continue;
234                 }
235
236                 prev = list->prev;
237                 prev_entry = prev->data;
238
239                 entry->offset = prev_entry->offset +
240                                         prev_entry->entry->next_offset;
241         }
242 }
243
244 static int connman_add_entry(struct connman_iptables *table,
245                                 struct ipt_entry *entry, GList *before)
246 {
247         struct connman_iptables_entry *e;
248
249         if (table == NULL)
250                 return -1;
251
252         e = g_try_malloc0(sizeof(struct connman_iptables_entry));
253         if (e == NULL)
254                 return -1;
255
256         e->entry = entry;
257
258         table->entries = g_list_insert_before(table->entries, before, e);
259         table->num_entries++;
260         table->size += entry->next_offset;
261
262         update_offsets(table);
263
264         return 0;
265 }
266
267 static int connman_iptables_add_chain(struct connman_iptables *table,
268                                         char *name)
269 {
270         GList *last;
271         struct ipt_entry *entry_head;
272         struct ipt_entry *entry_return;
273         struct ipt_error_target *error;
274         struct ipt_standard_target *standard;
275         u_int16_t entry_head_size, entry_return_size;
276
277         last = g_list_last(table->entries);
278
279         /*
280          * An empty chain is composed of:
281          * - A head entry, with no match and an error target.
282          *   The error target data is the chain name.
283          * - A tail entry, with no match and a standard target.
284          *   The standard target verdict is XT_RETURN (return to the
285          *   caller).
286          */
287
288         /* head entry */
289         entry_head_size = sizeof(struct ipt_entry) +
290                                 sizeof(struct ipt_error_target);
291         entry_head = g_try_malloc0(entry_head_size);
292         if (entry_head == NULL)
293                 goto err;
294
295         memset(entry_head, 0, entry_head_size);
296
297         entry_head->target_offset = sizeof(struct ipt_entry);
298         entry_head->next_offset = entry_head_size;
299
300         error = (struct ipt_error_target *) entry_head->elems;
301         strcpy(error->t.u.user.name, IPT_ERROR_TARGET);
302         error->t.u.user.target_size = ALIGN(sizeof(struct ipt_error_target));
303         strcpy(error->error, name);
304
305         if (connman_add_entry(table, entry_head, last) < 0)
306                 goto err;
307
308         /* tail entry */
309         entry_return_size = sizeof(struct ipt_entry) +
310                                 sizeof(struct ipt_standard_target);
311         entry_return = g_try_malloc0(entry_return_size);
312         if (entry_return == NULL)
313                 goto err;
314
315         memset(entry_return, 0, entry_return_size);
316
317         entry_return->target_offset = sizeof(struct ipt_entry);
318         entry_return->next_offset = entry_return_size;
319
320         standard = (struct ipt_standard_target *) entry_return->elems;
321         standard->target.u.user.target_size =
322                                 ALIGN(sizeof(struct ipt_standard_target));
323         standard->verdict = XT_RETURN;
324
325         if (connman_add_entry(table, entry_return, last) < 0)
326                 goto err;
327
328         return 0;
329
330 err:
331         g_free(entry_head);
332         g_free(entry_return);
333
334         return -ENOMEM;
335 }
336
337 static struct ipt_entry *
338 new_rule(char *target_name, struct xtables_target *xt_t,
339                 char *match_name, struct xtables_match *xt_m)
340 {
341         struct ipt_entry *new_entry;
342         size_t match_size, target_size;
343         int is_builtin = is_builtin_target(target_name);
344
345         if (xt_m)
346                 match_size = xt_m->m->u.match_size;
347         else
348                 match_size = 0;
349
350         if (xt_t)
351                 target_size = ALIGN(xt_t->t->u.target_size);
352         else
353                 target_size = 0;
354
355         new_entry = g_try_malloc0(sizeof(struct ipt_entry) + target_size +
356                                                                 match_size);
357         if (new_entry == NULL)
358                 return NULL;
359
360         new_entry->target_offset = sizeof(struct ipt_entry) + match_size;
361         new_entry->next_offset = sizeof(struct ipt_entry) + target_size +
362                                                                 match_size;
363         if (xt_m) {
364                 struct xt_entry_match *entry_match;
365
366                 entry_match = (struct xt_entry_match *)new_entry->elems;
367                 memcpy(entry_match, xt_m->m, match_size);
368         }
369
370         if (xt_t) {
371                 struct xt_entry_target *entry_target;
372
373                 if (is_builtin) {
374                         struct xt_standard_target *target;
375
376                         target = (struct xt_standard_target *)(xt_t->t);
377                         strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
378                         target->verdict = target_to_verdict(target_name);
379                 }
380
381                 entry_target = ipt_get_target(new_entry);
382                 memcpy(entry_target, xt_t->t, target_size);
383         }
384
385         return new_entry;
386 }
387
388 static int
389 connman_iptables_add_rule(struct connman_iptables *table, char *chain_name,
390                                 char *target_name, struct xtables_target *xt_t,
391                                 char *match_name, struct xtables_match *xt_m)
392 {
393         GList *chain_tail;
394         struct ipt_entry *new_entry;
395
396         chain_tail = find_chain_tail(table, chain_name);
397         if (chain_tail == NULL)
398                 return -EINVAL;
399
400         new_entry = new_rule(target_name, xt_t,
401                                 match_name, xt_m);
402         if (new_entry == NULL)
403                 return -EINVAL;
404
405         return connman_add_entry(table, new_entry, chain_tail->prev);
406 }
407
408 static struct ipt_replace *
409 connman_iptables_blob(struct connman_iptables *table)
410 {
411         struct ipt_replace *r;
412         GList *list;
413         struct connman_iptables_entry *e;
414         unsigned char *entry_index;
415
416         r = g_try_malloc0(sizeof(struct ipt_replace) + table->size);
417         if (r == NULL)
418                 return NULL;
419
420         memset(r, 0, sizeof(*r) + table->size);
421
422         r->counters = g_try_malloc0(sizeof(struct xt_counters)
423                                 * table->num_entries);
424         if (r->counters == NULL) {
425                 g_free(r);
426                 return NULL;
427         }
428
429         strcpy(r->name, table->info->name);
430         r->num_entries = table->num_entries;
431         r->size = table->size;
432
433         r->num_counters = table->old_entries;
434         r->valid_hooks  = table->info->valid_hooks;
435
436         memcpy(r->hook_entry, table->info->hook_entry,
437                                 sizeof(table->info->hook_entry));
438         memcpy(r->underflow, table->info->underflow,
439                                 sizeof(table->info->underflow));
440
441         entry_index = (unsigned char *)r->entries;
442         for (list = table->entries; list; list = list->next) {
443                 e = list->data;
444
445                 memcpy(entry_index, e->entry, e->entry->next_offset);
446                 entry_index += e->entry->next_offset;
447         }
448
449         return r;
450 }
451
452 static void dump_target(struct connman_iptables *table,
453                                 struct ipt_entry *entry)
454
455 {
456         struct xtables_target *xt_t;
457         struct xt_entry_target *target;
458
459         target = ipt_get_target(entry);
460
461         if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
462                 struct xt_standard_target *t;
463
464                 t = (struct xt_standard_target *)target;
465
466                 switch (t->verdict) {
467                 case XT_RETURN:
468                         printf("\ttarget RETURN\n");
469                         break;
470
471                 case -NF_ACCEPT - 1:
472                         printf("\ttarget ACCEPT\n");
473                         break;
474
475                 case -NF_DROP - 1:
476                         printf("\ttarget DROP\n");
477                         break;
478
479                 case -NF_QUEUE - 1:
480                         printf("\ttarget QUEUE\n");
481                         break;
482
483                 case -NF_STOP - 1:
484                         printf("\ttarget STOP\n");
485                         break;
486
487                 default:
488                         printf("\tJUMP @%p (0x%x)\n",
489                                 (char*)table->blob_entries->entrytable +
490                                 t->verdict, t->verdict);
491                         break;
492                 }
493
494                 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
495                                                 XTF_LOAD_MUST_SUCCEED);
496
497                 if(xt_t->print != NULL)
498                         xt_t->print(NULL, target, 1);
499         } else {
500                 xt_t = xtables_find_target(target->u.user.name, XTF_TRY_LOAD);
501                 if (xt_t == NULL) {
502                         printf("\ttarget %s\n", target->u.user.name);
503                         return;
504                 }
505
506                 if(xt_t->print != NULL) {
507                         printf("\ttarget ");
508                         xt_t->print(NULL, target, 1);
509                         printf("\n");
510                 }
511         }
512 }
513
514 static void dump_match(struct connman_iptables *table, struct ipt_entry *entry)
515 {
516         struct xtables_match *xt_m;
517         struct xt_entry_match *match;
518
519         if (entry->elems == (unsigned char *)entry + entry->target_offset)
520                 return;
521
522         match = (struct xt_entry_match *) entry->elems;
523
524         if (!strlen(match->u.user.name))
525                 return;
526
527         xt_m = xtables_find_match(match->u.user.name, XTF_TRY_LOAD, NULL);
528         if (xt_m == NULL)
529                 goto out;
530
531         if(xt_m->print != NULL) {
532                 printf("\tmatch ");
533                 xt_m->print(NULL, match, 1);
534                 printf("\n");
535
536                 return;
537         }
538
539 out:
540         printf("\tmatch %s\n", match->u.user.name);
541
542 }
543
544 static int connman_iptables_dump_entry(struct ipt_entry *entry,
545                                         struct connman_iptables *table)
546 {
547         struct xt_entry_target *target;
548         unsigned int offset;
549         int builtin;
550
551         offset = (char *)entry - (char *)table->blob_entries->entrytable;
552         target = ipt_get_target(entry);
553         builtin = is_hook_entry(table, entry);
554
555         if (entry_to_offset(table, entry) + entry->next_offset ==
556                                         table->blob_entries->size) {
557                 printf("End of CHAIN 0x%x\n", offset);
558                 return 0;
559         }
560
561         if (!strcmp(target->u.user.name, IPT_ERROR_TARGET)) {
562                 printf("USER CHAIN (%s) %p  match %p  target %p  size %d\n",
563                         target->data, entry, entry->elems,
564                         (char *)entry + entry->target_offset,
565                                 entry->next_offset);
566
567                 return 0;
568         } else if (builtin >= 0) {
569                 printf("CHAIN (%s) %p  match %p  target %p  size %d\n",
570                         hooknames[builtin], entry, entry->elems,
571                         (char *)entry + entry->target_offset,
572                                 entry->next_offset);
573         } else {
574                 printf("RULE %p  match %p  target %p  size %d\n", entry,
575                         entry->elems,
576                         (char *)entry + entry->target_offset,
577                                 entry->next_offset);
578         }
579
580         dump_match(table, entry);
581         dump_target(table, entry);
582
583         return 0;
584 }
585
586 static void connman_iptables_dump(struct connman_iptables *table)
587 {
588         printf("%s valid_hooks=0x%08x, num_entries=%u, size=%u\n",
589                 table->info->name,
590                 table->info->valid_hooks, table->info->num_entries,
591                 table->info->size);
592
593         ENTRY_ITERATE(table->blob_entries->entrytable,
594                         table->blob_entries->size,
595                         connman_iptables_dump_entry, table);
596
597 }
598
599 static int connman_iptables_get_entries(struct connman_iptables *table)
600 {
601         socklen_t entry_size;
602
603         entry_size = sizeof(struct ipt_get_entries) + table->info->size;
604
605         return getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_ENTRIES,
606                                 table->blob_entries, &entry_size);
607 }
608
609 static int connman_iptables_replace(struct connman_iptables *table,
610                                         struct ipt_replace *r)
611 {
612         return setsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_SET_REPLACE, r,
613                          sizeof(*r) + r->size);
614 }
615
616 static void connman_iptables_cleanup(struct connman_iptables *table)
617 {
618         close(table->ipt_sock);
619         g_free(table->info);
620         g_free(table->blob_entries);
621         g_free(table);
622
623         xtables_free_opts(1);
624 }
625
626 static int connman_iptables_commit(struct connman_iptables *table)
627 {
628         struct ipt_replace *repl;
629
630         repl = connman_iptables_blob(table);
631
632         return connman_iptables_replace(table, repl);
633 }
634
635 static int add_entry(struct ipt_entry *entry, struct connman_iptables *table)
636 {
637         return connman_add_entry(table, entry, NULL);
638 }
639
640 static struct connman_iptables *connman_iptables_init(const char *table_name)
641 {
642         struct connman_iptables *table;
643         socklen_t s;
644
645         table =  g_try_new0(struct connman_iptables, 1);
646         if (table == NULL)
647                 return NULL;
648
649         table->info =  g_try_new0(struct ipt_getinfo, 1);
650         if (table->info == NULL)
651                 goto err;
652
653         table->ipt_sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
654         if (table->ipt_sock < 0)
655                 goto err;
656
657         s = sizeof(*table->info);
658         strcpy(table->info->name, table_name);
659         if (getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_INFO,
660                                                 table->info, &s) < 0)
661                 goto err;
662
663         table->blob_entries = g_try_malloc0(sizeof(struct ipt_get_entries) +
664                                                 table->info->size);
665         if (table->blob_entries == NULL)
666                 goto err;
667
668         strcpy(table->blob_entries->name, table_name);
669         table->blob_entries->size = table->info->size;
670
671         if (connman_iptables_get_entries(table) < 0)
672                 goto err;
673
674         table->num_entries = 0;
675         table->old_entries = table->info->num_entries;
676         table->size = 0;
677
678         ENTRY_ITERATE(table->blob_entries->entrytable,
679                         table->blob_entries->size,
680                                 add_entry, table);
681
682
683         return table;
684
685 err:
686
687         connman_iptables_cleanup(table);
688
689         return NULL;
690 }
691
692
693 static struct option connman_iptables_opts[] = {
694         {.name = "append",        .has_arg = 1, .val = 'A'},
695         {.name = "list",          .has_arg = 2, .val = 'L'},
696         {.name = "new-chain",     .has_arg = 1, .val = 'N'},
697         {.name = "in-interface",  .has_arg = 1, .val = 'i'},
698         {.name = "jump",          .has_arg = 1, .val = 'j'},
699         {.name = "match",         .has_arg = 1, .val = 'm'},
700         {.name = "out-interface", .has_arg = 1, .val = 'o'},
701         {.name = "table",         .has_arg = 1, .val = 't'},
702         {NULL},
703 };
704
705 struct xtables_globals connman_iptables_globals = {
706         .option_offset = 0,
707         .opts = connman_iptables_opts,
708         .orig_opts = connman_iptables_opts,
709 };
710
711 int main(int argc, char *argv[])
712 {
713         struct connman_iptables *table;
714         struct xtables_match *xt_m;
715         struct xtables_target *xt_t;
716         char *table_name, *chain, *new_chain, *match_name, *target_name;
717         int c;
718         size_t size;
719         gboolean dump, invert;
720
721         xtables_init_all(&connman_iptables_globals, NFPROTO_IPV4);
722
723         dump = FALSE;
724         invert = FALSE;
725         table_name = chain = new_chain = match_name = target_name = NULL;
726         table = NULL;
727         xt_m = NULL;
728         xt_t = NULL;
729
730         while ((c = getopt_long(argc, argv,
731            "-A:L::N:j:i:m:o:t:", connman_iptables_globals.opts, NULL)) != -1) {
732                 switch (c) {
733                 case 'A':
734                         chain = optarg;
735                         break;
736
737                 case 'L':
738                         dump = TRUE;
739                         break;
740
741                 case 'N':
742                         new_chain = optarg;
743                         break;
744
745                 case 'j':
746                         target_name = optarg;
747                         xt_t = xtables_find_target(target_name, XTF_TRY_LOAD);
748
749                         if (xt_t == NULL)
750                                 break;
751
752                         size = ALIGN(sizeof(struct ipt_entry_target)) + xt_t->size;
753
754                         xt_t->t = g_try_malloc0(size);
755                         if (xt_t->t == NULL)
756                                 goto out;
757                         xt_t->t->u.target_size = size;
758                         strcpy(xt_t->t->u.user.name, target_name);
759                         xt_t->t->u.user.revision = xt_t->revision;
760                         if (xt_t->init != NULL)
761                                 xt_t->init(xt_t->t);
762                         connman_iptables_globals.opts =
763                                 xtables_merge_options(connman_iptables_globals.opts,
764                                                      xt_t->extra_opts,
765                                                      &xt_t->option_offset);
766                         if (connman_iptables_globals.opts == NULL)
767                                 goto out;
768
769                         break;
770
771                 case 'i':
772                         break;
773
774                 case 'm':
775                         match_name = optarg;
776
777                         xt_m = xtables_find_match(optarg, XTF_LOAD_MUST_SUCCEED, NULL);
778                         size = ALIGN(sizeof(struct ipt_entry_match)) + xt_m->size;
779                         xt_m->m = g_try_malloc0(size);
780                         if (xt_m == NULL)
781                                 goto out;
782                         xt_m->m->u.match_size = size;
783                         strcpy(xt_m->m->u.user.name, xt_m->name);
784                         xt_m->m->u.user.revision = xt_m->revision;
785                         if (xt_m->init != NULL)
786                                 xt_m->init(xt_m->m);
787                         if (xt_m != xt_m->next) {
788                                 connman_iptables_globals.opts =
789                                         xtables_merge_options(connman_iptables_globals.opts,
790                                                 xt_m->extra_opts,
791                                                 &xt_m->option_offset);
792                                 if (connman_iptables_globals.opts == NULL)
793                                         goto out;
794                         }
795
796                         break;
797
798                 case 'o':
799                         break;
800
801                 case 't':
802                         table_name = optarg;
803                         break;
804
805                 case 1:
806                         if (optarg[0] == '!' && optarg[1] == '\0') {
807                                 if (invert)
808                                         printf("Consecutive ! not allowed\n");
809
810                                 invert = TRUE;
811                                 optarg[0] = '\0';
812                                 continue;
813                         }
814
815                         printf("Invalid option\n");
816
817                         return -1;
818
819                 default:
820                         if (xt_t == NULL || xt_t->parse == NULL ||
821                             !xt_t->parse(c - xt_t->option_offset, argv, invert,
822                                         &xt_t->tflags, NULL, &xt_t->t)) {
823                                 if (xt_m == NULL || xt_m->parse == NULL)
824                                         break;
825
826                                 xt_m->parse(c - xt_m->option_offset, argv,
827                                         invert, &xt_m->mflags, NULL, &xt_m->m);
828                         }
829
830                         break;
831                 }
832         }
833
834         if (table_name == NULL)
835                 table_name = "filter";
836
837         table = connman_iptables_init(table_name);
838         if (table == NULL)
839                 return -1;
840
841         if (dump) {
842                 connman_iptables_dump(table);
843
844                 return 0;
845         }
846
847         if (chain && new_chain)
848                 return -1;
849
850         if (new_chain) {
851                 printf("New chain %s\n", new_chain);
852
853                 connman_iptables_add_chain(table, new_chain);
854
855                 goto commit;
856         }
857
858         if (chain) {
859                 if (target_name == NULL)
860                         return -1;
861
862                 printf("Adding %s to %s (match %s)\n", target_name, chain, match_name);
863
864                 connman_iptables_add_rule(table, chain, target_name, xt_t,
865                                         match_name, xt_m);
866
867                 goto commit;
868         }
869
870 commit:
871
872         connman_iptables_commit(table);
873
874 out:
875         connman_iptables_cleanup(table);
876
877         if (xt_t)
878                 g_free(xt_t->t);
879
880         if (xt_m)
881                 g_free(xt_m->m);
882
883         return 0;
884 }