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