iptables: Update entries offsets
[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 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
230 static void update_offsets(struct connman_iptables *table)
231 {
232         GList *list, *prev;
233         struct connman_iptables_entry *entry, *prev_entry;
234
235         for (list = table->entries; list; list = list->next) {
236                 entry = list->data;
237
238                 if (list == table->entries) {
239                         entry->offset = 0;
240
241                         continue;
242                 }
243
244                 prev = list->prev;
245                 prev_entry = prev->data;
246
247                 entry->offset = prev_entry->offset +
248                                         prev_entry->entry->next_offset;
249         }
250 }
251
252 static int iptables_add_entry(struct connman_iptables *table,
253                                 struct ipt_entry *entry, GList *before)
254 {
255         struct connman_iptables_entry *e;
256
257         if (table == NULL)
258                 return -1;
259
260         e = g_try_malloc0(sizeof(struct connman_iptables_entry));
261         if (e == NULL)
262                 return -1;
263
264         e->entry = entry;
265
266         table->entries = g_list_insert_before(table->entries, before, e);
267         table->num_entries++;
268         table->size += entry->next_offset;
269
270         update_offsets(table);
271
272         return 0;
273 }
274
275 static int iptables_add_chain(struct connman_iptables *table,
276                                         char *name)
277 {
278         GList *last;
279         struct ipt_entry *entry_head;
280         struct ipt_entry *entry_return;
281         struct ipt_error_target *error;
282         struct ipt_standard_target *standard;
283         u_int16_t entry_head_size, entry_return_size;
284
285         last = g_list_last(table->entries);
286
287         /*
288          * An empty chain is composed of:
289          * - A head entry, with no match and an error target.
290          *   The error target data is the chain name.
291          * - A tail entry, with no match and a standard target.
292          *   The standard target verdict is XT_RETURN (return to the
293          *   caller).
294          */
295
296         /* head entry */
297         entry_head_size = sizeof(struct ipt_entry) +
298                                 sizeof(struct ipt_error_target);
299         entry_head = g_try_malloc0(entry_head_size);
300         if (entry_head == NULL)
301                 goto err;
302
303         memset(entry_head, 0, entry_head_size);
304
305         entry_head->target_offset = sizeof(struct ipt_entry);
306         entry_head->next_offset = entry_head_size;
307
308         error = (struct ipt_error_target *) entry_head->elems;
309         strcpy(error->t.u.user.name, IPT_ERROR_TARGET);
310         error->t.u.user.target_size = ALIGN(sizeof(struct ipt_error_target));
311         strcpy(error->error, name);
312
313         if (iptables_add_entry(table, entry_head, last) < 0)
314                 goto err;
315
316         /* tail entry */
317         entry_return_size = sizeof(struct ipt_entry) +
318                                 sizeof(struct ipt_standard_target);
319         entry_return = g_try_malloc0(entry_return_size);
320         if (entry_return == NULL)
321                 goto err;
322
323         memset(entry_return, 0, entry_return_size);
324
325         entry_return->target_offset = sizeof(struct ipt_entry);
326         entry_return->next_offset = entry_return_size;
327
328         standard = (struct ipt_standard_target *) entry_return->elems;
329         standard->target.u.user.target_size =
330                                 ALIGN(sizeof(struct ipt_standard_target));
331         standard->verdict = XT_RETURN;
332
333         if (iptables_add_entry(table, entry_return, last) < 0)
334                 goto err;
335
336         return 0;
337
338 err:
339         g_free(entry_head);
340         g_free(entry_return);
341
342         return -ENOMEM;
343 }
344
345 static struct ipt_entry *
346 new_rule(char *target_name, struct xtables_target *xt_t,
347                 char *match_name, struct xtables_match *xt_m)
348 {
349         struct ipt_entry *new_entry;
350         size_t match_size, target_size;
351         int is_builtin = is_builtin_target(target_name);
352
353         if (xt_m)
354                 match_size = xt_m->m->u.match_size;
355         else
356                 match_size = 0;
357
358         if (xt_t)
359                 target_size = ALIGN(xt_t->t->u.target_size);
360         else
361                 target_size = 0;
362
363         new_entry = g_try_malloc0(sizeof(struct ipt_entry) + target_size +
364                                                                 match_size);
365         if (new_entry == NULL)
366                 return NULL;
367
368         new_entry->target_offset = sizeof(struct ipt_entry) + match_size;
369         new_entry->next_offset = sizeof(struct ipt_entry) + target_size +
370                                                                 match_size;
371         if (xt_m) {
372                 struct xt_entry_match *entry_match;
373
374                 entry_match = (struct xt_entry_match *)new_entry->elems;
375                 memcpy(entry_match, xt_m->m, match_size);
376         }
377
378         if (xt_t) {
379                 struct xt_entry_target *entry_target;
380
381                 if (is_builtin) {
382                         struct xt_standard_target *target;
383
384                         target = (struct xt_standard_target *)(xt_t->t);
385                         strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
386                         target->verdict = target_to_verdict(target_name);
387                 }
388
389                 entry_target = ipt_get_target(new_entry);
390                 memcpy(entry_target, xt_t->t, target_size);
391         }
392
393         return new_entry;
394 }
395
396 static int
397 iptables_add_rule(struct connman_iptables *table, char *chain_name,
398                                 char *target_name, struct xtables_target *xt_t,
399                                 char *match_name, struct xtables_match *xt_m)
400 {
401         GList *chain_tail;
402         struct ipt_entry *new_entry;
403
404         chain_tail = find_chain_tail(table, chain_name);
405         if (chain_tail == NULL)
406                 return -EINVAL;
407
408         new_entry = new_rule(target_name, xt_t,
409                                 match_name, xt_m);
410         if (new_entry == NULL)
411                 return -EINVAL;
412
413         return iptables_add_entry(table, new_entry, chain_tail->prev);
414 }
415
416 static struct ipt_replace *
417 iptables_blob(struct connman_iptables *table)
418 {
419         struct ipt_replace *r;
420         GList *list;
421         struct connman_iptables_entry *e;
422         unsigned char *entry_index;
423
424         r = g_try_malloc0(sizeof(struct ipt_replace) + table->size);
425         if (r == NULL)
426                 return NULL;
427
428         memset(r, 0, sizeof(*r) + table->size);
429
430         r->counters = g_try_malloc0(sizeof(struct xt_counters)
431                                 * table->num_entries);
432         if (r->counters == NULL) {
433                 g_free(r);
434                 return NULL;
435         }
436
437         strcpy(r->name, table->info->name);
438         r->num_entries = table->num_entries;
439         r->size = table->size;
440
441         r->num_counters = table->old_entries;
442         r->valid_hooks  = table->info->valid_hooks;
443
444         memcpy(r->hook_entry, table->info->hook_entry,
445                                 sizeof(table->info->hook_entry));
446         memcpy(r->underflow, table->info->underflow,
447                                 sizeof(table->info->underflow));
448
449         entry_index = (unsigned char *)r->entries;
450         for (list = table->entries; list; list = list->next) {
451                 e = list->data;
452
453                 memcpy(entry_index, e->entry, e->entry->next_offset);
454                 entry_index += e->entry->next_offset;
455         }
456
457         return r;
458 }
459
460 static void dump_target(struct connman_iptables *table,
461                                 struct ipt_entry *entry)
462
463 {
464         struct xtables_target *xt_t;
465         struct xt_entry_target *target;
466
467         target = ipt_get_target(entry);
468
469         if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
470                 struct xt_standard_target *t;
471
472                 t = (struct xt_standard_target *)target;
473
474                 switch (t->verdict) {
475                 case XT_RETURN:
476                         connman_info("\ttarget RETURN");
477                         break;
478
479                 case -NF_ACCEPT - 1:
480                         connman_info("\ttarget ACCEPT");
481                         break;
482
483                 case -NF_DROP - 1:
484                         connman_info("\ttarget DROP");
485                         break;
486
487                 case -NF_QUEUE - 1:
488                         connman_info("\ttarget QUEUE");
489                         break;
490
491                 case -NF_STOP - 1:
492                         connman_info("\ttarget STOP");
493                         break;
494
495                 default:
496                         connman_info("\tJUMP @%p (0x%x)",
497                                 (char*)table->blob_entries->entrytable +
498                                 t->verdict, t->verdict);
499                         break;
500                 }
501
502                 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
503                                                 XTF_LOAD_MUST_SUCCEED);
504
505                 if(xt_t->print != NULL)
506                         xt_t->print(NULL, target, 1);
507         } else {
508                 xt_t = xtables_find_target(target->u.user.name, XTF_TRY_LOAD);
509                 if (xt_t == NULL) {
510                         connman_info("\ttarget %s", target->u.user.name);
511                         return;
512                 }
513
514                 if(xt_t->print != NULL) {
515                         connman_info("\ttarget ");
516                         xt_t->print(NULL, target, 1);
517                 }
518         }
519 }
520
521 static void dump_match(struct connman_iptables *table, struct ipt_entry *entry)
522 {
523         struct xtables_match *xt_m;
524         struct xt_entry_match *match;
525
526         if (entry->elems == (unsigned char *)entry + entry->target_offset)
527                 return;
528
529         match = (struct xt_entry_match *) entry->elems;
530
531         if (!strlen(match->u.user.name))
532                 return;
533
534         xt_m = xtables_find_match(match->u.user.name, XTF_TRY_LOAD, NULL);
535         if (xt_m == NULL)
536                 goto out;
537
538         if(xt_m->print != NULL) {
539                 connman_info("\tmatch ");
540                 xt_m->print(NULL, match, 1);
541
542                 return;
543         }
544
545 out:
546         connman_info("\tmatch %s", match->u.user.name);
547
548 }
549
550 static int dump_entry(struct ipt_entry *entry,
551                                 struct connman_iptables *table)
552 {
553         struct xt_entry_target *target;
554         unsigned int offset;
555         int builtin;
556
557         offset = (char *)entry - (char *)table->blob_entries->entrytable;
558         target = ipt_get_target(entry);
559         builtin = is_hook_entry(table, entry);
560
561         if (entry_to_offset(table, entry) + entry->next_offset ==
562                                         table->blob_entries->size) {
563                 connman_info("End of CHAIN 0x%x", offset);
564                 return 0;
565         }
566
567         if (!strcmp(target->u.user.name, IPT_ERROR_TARGET)) {
568                 connman_info("USER CHAIN (%s) %p  match %p  target %p  size %d",
569                         target->data, entry, entry->elems,
570                         (char *)entry + entry->target_offset,
571                                 entry->next_offset);
572
573                 return 0;
574         } else if (builtin >= 0) {
575                 connman_info("CHAIN (%s) %p  match %p  target %p  size %d",
576                         hooknames[builtin], entry, entry->elems,
577                         (char *)entry + entry->target_offset,
578                                 entry->next_offset);
579         } else {
580                 connman_info("RULE %p  match %p  target %p  size %d", entry,
581                         entry->elems,
582                         (char *)entry + entry->target_offset,
583                                 entry->next_offset);
584         }
585
586         dump_match(table, entry);
587         dump_target(table, entry);
588
589         return 0;
590 }
591
592 static void iptables_dump(struct connman_iptables *table)
593 {
594         connman_info("%s valid_hooks=0x%08x, num_entries=%u, size=%u",
595                         table->info->name,
596                         table->info->valid_hooks, table->info->num_entries,
597                                 table->info->size);
598
599         ENTRY_ITERATE(table->blob_entries->entrytable,
600                         table->blob_entries->size,
601                         dump_entry, table);
602
603 }
604
605 static int iptables_get_entries(struct connman_iptables *table)
606 {
607         socklen_t entry_size;
608
609         entry_size = sizeof(struct ipt_get_entries) + table->info->size;
610
611         return getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_ENTRIES,
612                                 table->blob_entries, &entry_size);
613 }
614
615 static int iptables_replace(struct connman_iptables *table,
616                                         struct ipt_replace *r)
617 {
618         return setsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_SET_REPLACE, r,
619                          sizeof(*r) + r->size);
620 }
621
622 static int add_entry(struct ipt_entry *entry, struct connman_iptables *table)
623 {
624         struct ipt_entry *new_entry;
625
626         new_entry = g_try_malloc0(entry->next_offset);
627         if (new_entry == NULL)
628                 return -ENOMEM;
629
630         memcpy(new_entry, entry, entry->next_offset);
631
632         return iptables_add_entry(table, new_entry, NULL);
633 }
634
635 static void table_cleanup(struct connman_iptables *table)
636 {
637         GList *list;
638         struct connman_iptables_entry *entry;
639
640         close(table->ipt_sock);
641
642         for (list = table->entries; list; list = list->next) {
643                 entry = list->data;
644
645                 g_free(entry->entry);
646         }
647
648         g_list_free(table->entries);
649         g_free(table->info);
650         g_free(table->blob_entries);
651         g_free(table);
652 }
653
654 static struct connman_iptables *iptables_init(char *table_name)
655 {
656         struct connman_iptables *table;
657         socklen_t s;
658
659         table = g_hash_table_lookup(table_hash, table_name);
660         if (table != NULL)
661                 return table;
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 (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         g_hash_table_insert(table_hash, table_name, table);
701
702         return table;
703
704 err:
705
706         table_cleanup(table);
707
708         return NULL;
709 }
710
711 static struct option 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 iptables_globals = {
724         .option_offset = 0,
725         .opts = iptables_opts,
726         .orig_opts = iptables_opts,
727 };
728
729 static int iptables_command(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, ret;
736         size_t size;
737         gboolean dump, invert;
738
739         if (argc == 0)
740                 return -EINVAL;
741
742         dump = FALSE;
743         invert = FALSE;
744         table_name = chain = new_chain = match_name = target_name = NULL;
745         table = NULL;
746         xt_m = NULL;
747         xt_t = NULL;
748         ret = 0;
749
750         optind = 0;
751
752         while ((c = getopt_long(argc, argv,
753            "-A:L::N:j:i:m:o:t:", iptables_globals.opts, NULL)) != -1) {
754                 switch (c) {
755                 case 'A':
756                         chain = optarg;
757                         break;
758
759                 case 'L':
760                         dump = TRUE;
761                         break;
762
763                 case 'N':
764                         new_chain = optarg;
765                         break;
766
767                 case 'j':
768                         target_name = optarg;
769                         xt_t = xtables_find_target(target_name, XTF_TRY_LOAD);
770
771                         if (xt_t == NULL)
772                                 break;
773
774                         size = ALIGN(sizeof(struct ipt_entry_target)) +
775                                                                 xt_t->size;
776
777                         xt_t->t = g_try_malloc0(size);
778                         if (xt_t->t == NULL)
779                                 goto out;
780                         xt_t->t->u.target_size = size;
781                         strcpy(xt_t->t->u.user.name, target_name);
782                         xt_t->t->u.user.revision = xt_t->revision;
783                         if (xt_t->init != NULL)
784                                 xt_t->init(xt_t->t);
785                         iptables_globals.opts =
786                                 xtables_merge_options(iptables_globals.opts,
787                                                      xt_t->extra_opts,
788                                                      &xt_t->option_offset);
789                         if (iptables_globals.opts == NULL)
790                                 goto out;
791
792                         break;
793
794                 case 'i':
795                         break;
796
797                 case 'm':
798                         match_name = optarg;
799
800                         xt_m = xtables_find_match(optarg, XTF_LOAD_MUST_SUCCEED, NULL);
801                         size = ALIGN(sizeof(struct ipt_entry_match)) +
802                                                                 xt_m->size;
803                         xt_m->m = g_try_malloc0(size);
804                         if (xt_m == NULL)
805                                 goto out;
806                         xt_m->m->u.match_size = size;
807                         strcpy(xt_m->m->u.user.name, xt_m->name);
808                         xt_m->m->u.user.revision = xt_m->revision;
809                         if (xt_m->init != NULL)
810                                 xt_m->init(xt_m->m);
811                         if (xt_m != xt_m->next) {
812                                 iptables_globals.opts =
813                                 xtables_merge_options(iptables_globals.opts,
814                                                 xt_m->extra_opts,
815                                                 &xt_m->option_offset);
816                                 if (iptables_globals.opts == NULL)
817                                         goto out;
818                         }
819
820                         break;
821
822                 case 'o':
823                         break;
824
825                 case 't':
826                         table_name = optarg;
827                         break;
828
829                 case 1:
830                         if (optarg[0] == '!' && optarg[1] == '\0') {
831                                 invert = TRUE;
832                                 optarg[0] = '\0';
833                                 continue;
834                         }
835
836                         connman_error("Invalid option");
837
838                         ret = -EINVAL;
839                         goto out;
840
841                 default:
842                         if (xt_t == NULL || xt_t->parse == NULL ||
843                             !xt_t->parse(c - xt_t->option_offset, argv, invert,
844                                         &xt_t->tflags, NULL, &xt_t->t)) {
845                                 if (xt_m == NULL || xt_m->parse == NULL)
846                                         break;
847
848                                 xt_m->parse(c - xt_m->option_offset, argv,
849                                         invert, &xt_m->mflags, NULL, &xt_m->m);
850                         }
851
852                         break;
853                 }
854         }
855
856         if (table_name == NULL)
857                 table_name = "filter";
858
859         table = iptables_init(table_name);
860         if (table == NULL) {
861                 ret = -EINVAL;
862                 goto out;
863         }
864
865         if (dump) {
866                 iptables_dump(table);
867
868                 ret = 0;
869                 goto out;
870         }
871
872         if (chain && new_chain) {
873                 ret = -EINVAL;
874                 goto out;
875         }
876
877         if (new_chain) {
878                 DBG("New chain %s", new_chain);
879
880                 ret = iptables_add_chain(table, new_chain);
881                 goto out;
882         }
883
884         if (chain) {
885                 if (target_name == NULL)
886                         return -1;
887
888                 DBG("Adding %s to %s (match %s)",
889                                 target_name, chain, match_name);
890
891                 ret = iptables_add_rule(table, chain, target_name, xt_t,
892                                         match_name, xt_m);
893
894                 goto out;
895         }
896
897 out:
898         if (xt_t)
899                 g_free(xt_t->t);
900
901         if (xt_m)
902                 g_free(xt_m->m);
903
904         return ret;
905 }
906
907 int __connman_iptables_command(const char *format, ...)
908 {
909         char **argv, **arguments, *command;
910         int argc, i, ret;
911         va_list args;
912
913         if (format == NULL)
914                 return -EINVAL;
915
916         va_start(args, format);
917
918         command = g_strdup_vprintf(format, args);
919
920         va_end(args);
921
922         if (command == NULL)
923                 return -ENOMEM;
924
925         arguments = g_strsplit_set(command, " ", -1);
926
927         for (argc = 0; arguments[argc]; argc++);
928
929         DBG("command %s argc %d", command, ++argc);
930
931         argv = g_try_malloc0(argc * sizeof(char *));
932         if (argv == NULL) {
933                 g_free(command);
934                 g_strfreev(arguments);
935                 return -ENOMEM;
936         }
937
938         argv[0] = "iptables";
939         for (i = 1; i < argc; i++)
940                 argv[i] = arguments[i - 1];
941
942         ret = iptables_command(argc, argv);
943
944         g_free(command);
945         g_strfreev(arguments);
946         g_free(argv);
947
948         return ret;
949 }
950
951
952 int __connman_iptables_commit(const char *table_name)
953 {
954         struct connman_iptables *table;
955         struct ipt_replace *repl;
956
957         table = g_hash_table_lookup(table_hash, table_name);
958         if (table == NULL)
959                 return -EINVAL;
960
961         repl = iptables_blob(table);
962
963         return iptables_replace(table, repl);
964 }
965
966 static void remove_table(gpointer user_data)
967 {
968         struct connman_iptables *table = user_data;
969
970         table_cleanup(table);
971 }
972
973 int __connman_iptables_init(void)
974 {
975         DBG("");
976
977         table_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
978                                                 NULL, remove_table);
979
980         xtables_init_all(&iptables_globals, NFPROTO_IPV4);
981
982         return 0;
983
984 }
985
986 void __connman_iptables_cleanup(void)
987 {
988         g_hash_table_destroy(table_hash);
989
990         xtables_free_opts(1);
991 }