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