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