iptables: Remove unused __connman_iptables_command()
[platform/upstream/connman.git] / src / iptables.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2012  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 /*
41  * Some comments on how the iptables API works (some of them from the
42  * source code from iptables and the kernel):
43  *
44  * - valid_hooks: bit indicates valid IDs for hook_entry
45  * - hook_entry[ID] offset to the chain start
46  * - overflows should be end of entry chains, and uncodintional policy nodes.
47  * - policy entry: last entry in a chain
48  * - user chain: end of last builtin + policy entry
49  * - final entry must be error node
50  * - Underflows must be unconditional and use the STANDARD target with
51  *   ACCEPT/DROP
52  * - IPT_SO_GET_INFO and IPT_SO_GET_ENTRIES are used to read a table
53  * - IPT_SO_GET_INFO: struct ipt_getinfo (note the lack of table content)
54  * - IPT_SO_GET_ENTRIES: struct ipt_get_entries (contains only parts of the
55  *   table header/meta info. The table is appended after the header. The entries
56  *   are of the type struct ipt_entry.
57  * - After the ipt_entry the matches are appended. After the matches
58  *   the target is appended.
59  * - ipt_entry->target_offset =  Size of ipt_entry + matches
60  * - ipt_entry->next_offset =  Size of ipt_entry + matches + target
61  * - IPT_SO_SET_REPLACE is used to write a table (contains the complete
62  * - hook_entry and overflow mark the begining and the end of a chain, e.g
63  *     entry hook: pre/in/fwd/out/post -1/0/352/504/-1
64  *     underflow:  pre/in/fwd/out/post -1/200/352/904/-1
65  *   means that INPUT starts at offset 0 and ends at 200 (the start offset to
66  *   the last element). FORWARD has one entry starting/ending at 352. The entry
67  *   has a size of 152. 352 + 152 = 504 which is the start of the OUTPUT chain
68  *   which then ends at 904. PREROUTING and POSTROUTING are invalid hooks in
69  *   the filter table.
70  * - 'iptables -t filter -A INPUT -m mark --mark 999 -j LOG'
71  *   writing that table looks like this:
72  *
73  *   filter valid_hooks 0x0000000e  num_entries 5  size 856
74  *   entry hook: pre/in/fwd/out/post -1/0/376/528/-1
75  *   underflow:  pre/in/fwd/out/post -1/224/376/528/-1
76  *   entry 0x699d30  offset 0  size 224
77  *     RULE  match 0x699da0  target 0x699dd0
78  *             match  mark match 0x3e7
79  *             target  LOG flags 0 level 4
80  *             src 0.0.0.0/0.0.0.0
81  *             dst 0.0.0.0/0.0.0.0
82  *   entry 0x699e10  offset 224  size 152
83  *     RULE  match 0x699e80  target 0x699e80
84  *             target ACCEPT
85  *             src 0.0.0.0/0.0.0.0
86  *             dst 0.0.0.0/0.0.0.0
87  *   entry 0x699ea8  offset 376  size 152
88  *     RULE  match 0x699f18  target 0x699f18
89  *             target ACCEPT
90  *             src 0.0.0.0/0.0.0.0
91  *             dst 0.0.0.0/0.0.0.0
92  *   entry 0x699f40  offset 528  size 152
93  *     RULE  match 0x699fb0  target 0x699fb0
94  *             target ACCEPT
95  *             src 0.0.0.0/0.0.0.0
96  *             dst 0.0.0.0/0.0.0.0
97  *   entry 0x699fd8  offset 680  size 176
98  *     USER CHAIN (ERROR)  match 0x69a048  target 0x69a048
99  *
100  *   Reading the filter table looks like this:
101  *
102  *   filter valid_hooks 0x0000000e  num_entries 5  size 856
103  *   entry hook: pre/in/fwd/out/post -1/0/376/528/-1
104  *   underflow:  pre/in/fwd/out/post -1/224/376/528/-1
105  *   entry 0x25fec28  offset 0  size 224
106  *     CHAIN (INPUT)  match 0x25fec98  target 0x25fecc8
107  *             match  mark match 0x3e7
108  *             target  LOG flags 0 level 4
109  *             src 0.0.0.0/0.0.0.0
110  *             dst 0.0.0.0/0.0.0.0
111  *   entry 0x25fed08  offset 224  size 152
112  *     RULE  match 0x25fed78  target 0x25fed78
113  *             target ACCEPT
114  *             src 0.0.0.0/0.0.0.0
115  *             dst 0.0.0.0/0.0.0.0
116  *   entry 0x25feda0  offset 376  size 152
117  *     CHAIN (FORWARD)  match 0x25fee10  target 0x25fee10
118  *             target ACCEPT
119  *             src 0.0.0.0/0.0.0.0
120  *             dst 0.0.0.0/0.0.0.0
121  *   entry 0x25fee38  offset 528  size 152
122  *     CHAIN (OUTPUT)  match 0x25feea8  target 0x25feea8
123  *             target ACCEPT
124  *             src 0.0.0.0/0.0.0.0
125  *             dst 0.0.0.0/0.0.0.0
126  *   entry 0x25feed0  offset 680  size 176
127  *     End of CHAIN
128  */
129
130 static const char *hooknames[] = {
131         [NF_IP_PRE_ROUTING]     = "PREROUTING",
132         [NF_IP_LOCAL_IN]        = "INPUT",
133         [NF_IP_FORWARD]         = "FORWARD",
134         [NF_IP_LOCAL_OUT]       = "OUTPUT",
135         [NF_IP_POST_ROUTING]    = "POSTROUTING",
136 };
137
138 #define LABEL_ACCEPT  "ACCEPT"
139 #define LABEL_DROP    "DROP"
140 #define LABEL_QUEUE   "QUEUE"
141 #define LABEL_RETURN  "RETURN"
142
143 #define XT_OPTION_OFFSET_SCALE 256
144
145 #define MIN_ALIGN (__alignof__(struct ipt_entry))
146
147 #define ALIGN(s) (((s) + ((MIN_ALIGN)-1)) & ~((MIN_ALIGN)-1))
148
149 struct error_target {
150         struct xt_entry_target t;
151         char error[IPT_TABLE_MAXNAMELEN];
152 };
153
154 struct connman_iptables_entry {
155         int offset;
156         int builtin;
157
158         struct ipt_entry *entry;
159 };
160
161 struct connman_iptables {
162         int ipt_sock;
163
164         struct ipt_getinfo *info;
165         struct ipt_get_entries *blob_entries;
166
167         unsigned int num_entries;
168         unsigned int old_entries;
169         unsigned int size;
170
171         unsigned int underflow[NF_INET_NUMHOOKS];
172         unsigned int hook_entry[NF_INET_NUMHOOKS];
173
174         GList *entries;
175 };
176
177 static GHashTable *table_hash = NULL;
178 static gboolean debug_enabled = FALSE;
179
180 typedef int (*iterate_entries_cb_t)(struct ipt_entry *entry, int builtin,
181                                         unsigned int hook,size_t size,
182                                         unsigned int offset, void *user_data);
183
184 static int iterate_entries(struct ipt_entry *entries,
185                                 unsigned int valid_hooks,
186                                 unsigned int *hook_entry,
187                                 size_t size, iterate_entries_cb_t cb,
188                                 void *user_data)
189 {
190         unsigned int i, h;
191         int builtin, err;
192         struct ipt_entry *entry;
193
194         if (valid_hooks != 0)
195                 h = __builtin_ffs(valid_hooks) - 1;
196         else
197                 h = NF_INET_NUMHOOKS;
198
199         for (i = 0, entry = entries; i < size;
200                         i += entry->next_offset) {
201                 builtin = -1;
202                 entry = (void *)entries + i;
203
204                 /*
205                  * Find next valid hook which offset is higher
206                  * or equal with the current offset.
207                  */
208                 if (h < NF_INET_NUMHOOKS) {
209                         if (hook_entry[h] < i) {
210                                 valid_hooks ^= (1 << h);
211
212                                 if (valid_hooks != 0)
213                                         h = __builtin_ffs(valid_hooks) - 1;
214                                 else
215                                         h = NF_INET_NUMHOOKS;
216                         }
217
218                         if (hook_entry[h] == i)
219                                 builtin = h;
220                 }
221
222                 err = cb(entry, builtin, h, size, i, user_data);
223                 if (err < 0)
224                         return err;
225
226         }
227
228         return 0;
229 }
230
231 static int print_entry(struct ipt_entry *entry, int builtin, unsigned int hook,
232                                         size_t size, unsigned int offset,
233                                         void *user_data)
234 {
235         iterate_entries_cb_t cb = user_data;
236
237         DBG("entry %p  hook %d  offset %d  size %d", entry, hook,
238                         offset, entry->next_offset);
239
240         return cb(entry, builtin, hook, size, offset, NULL);
241 }
242
243 static int target_to_verdict(const char *target_name)
244 {
245         if (!strcmp(target_name, LABEL_ACCEPT))
246                 return -NF_ACCEPT - 1;
247
248         if (!strcmp(target_name, LABEL_DROP))
249                 return -NF_DROP - 1;
250
251         if (!strcmp(target_name, LABEL_QUEUE))
252                 return -NF_QUEUE - 1;
253
254         if (!strcmp(target_name, LABEL_RETURN))
255                 return XT_RETURN;
256
257         return 0;
258 }
259
260 static gboolean is_builtin_target(const char *target_name)
261 {
262         if (!strcmp(target_name, LABEL_ACCEPT) ||
263                 !strcmp(target_name, LABEL_DROP) ||
264                 !strcmp(target_name, LABEL_QUEUE) ||
265                 !strcmp(target_name, LABEL_RETURN))
266                 return TRUE;
267
268         return FALSE;
269 }
270
271 static gboolean is_jump(struct connman_iptables_entry *e)
272 {
273         struct xt_entry_target *target;
274
275         target = ipt_get_target(e->entry);
276
277         if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
278                 struct xt_standard_target *t;
279
280                 t = (struct xt_standard_target *)target;
281
282                 switch (t->verdict) {
283                 case XT_RETURN:
284                 case -NF_ACCEPT - 1:
285                 case -NF_DROP - 1:
286                 case -NF_QUEUE - 1:
287                 case -NF_STOP - 1:
288                         return false;
289
290                 default:
291                         return true;
292                 }
293         }
294
295         return false;
296 }
297
298 static gboolean is_chain(struct connman_iptables *table,
299                                 struct connman_iptables_entry *e)
300 {
301         struct ipt_entry *entry;
302         struct xt_entry_target *target;
303
304         entry = e->entry;
305         if (e->builtin >= 0)
306                 return TRUE;
307
308         target = ipt_get_target(entry);
309         if (!strcmp(target->u.user.name, IPT_ERROR_TARGET))
310                 return TRUE;
311
312         return FALSE;
313 }
314
315 static GList *find_chain_head(struct connman_iptables *table,
316                                 const char *chain_name)
317 {
318         GList *list;
319         struct connman_iptables_entry *head;
320         struct ipt_entry *entry;
321         struct xt_entry_target *target;
322         int builtin;
323
324         for (list = table->entries; list; list = list->next) {
325                 head = list->data;
326                 entry = head->entry;
327
328                 /* Buit-in chain */
329                 builtin = head->builtin;
330                 if (builtin >= 0 && !strcmp(hooknames[builtin], chain_name))
331                         break;
332
333                 /* User defined chain */
334                 target = ipt_get_target(entry);
335                 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET) &&
336                     !strcmp((char *)target->data, chain_name))
337                         break;
338         }
339
340         return list;
341 }
342
343 static GList *find_chain_tail(struct connman_iptables *table,
344                                 const char *chain_name)
345 {
346         struct connman_iptables_entry *tail;
347         GList *chain_head, *list;
348
349         chain_head = find_chain_head(table, chain_name);
350         if (chain_head == NULL)
351                 return NULL;
352
353         /* Then we look for the next chain */
354         for (list = chain_head->next; list; list = list->next) {
355                 tail = list->data;
356
357                 if (is_chain(table, tail))
358                         return list;
359         }
360
361         /* Nothing found, we return the table end */
362         return g_list_last(table->entries);
363 }
364
365
366 static void update_offsets(struct connman_iptables *table)
367 {
368         GList *list, *prev;
369         struct connman_iptables_entry *entry, *prev_entry;
370
371         for (list = table->entries; list; list = list->next) {
372                 entry = list->data;
373
374                 if (list == table->entries) {
375                         entry->offset = 0;
376
377                         continue;
378                 }
379
380                 prev = list->prev;
381                 prev_entry = prev->data;
382
383                 entry->offset = prev_entry->offset +
384                                         prev_entry->entry->next_offset;
385         }
386 }
387
388 static void update_targets_reference(struct connman_iptables *table,
389                                 struct connman_iptables_entry *entry_before,
390                                 struct connman_iptables_entry *modified_entry,
391                                 gboolean is_removing)
392 {
393         struct connman_iptables_entry *tmp;
394         struct xt_standard_target *t;
395         GList *list;
396         int offset;
397
398         offset = modified_entry->entry->next_offset;
399
400         for (list = table->entries; list; list = list->next) {
401                 tmp = list->data;
402
403                 if (!is_jump(tmp))
404                         continue;
405
406                 t = (struct xt_standard_target *)ipt_get_target(tmp->entry);
407
408                 if (is_removing == TRUE) {
409                         if (t->verdict >= entry_before->offset)
410                                 t->verdict -= offset;
411                 } else {
412                         if (t->verdict > entry_before->offset)
413                                 t->verdict += offset;
414                 }
415         }
416 }
417
418 static int iptables_add_entry(struct connman_iptables *table,
419                                 struct ipt_entry *entry, GList *before,
420                                         int builtin)
421 {
422         struct connman_iptables_entry *e, *entry_before;
423
424         if (table == NULL)
425                 return -1;
426
427         e = g_try_malloc0(sizeof(struct connman_iptables_entry));
428         if (e == NULL)
429                 return -1;
430
431         e->entry = entry;
432         e->builtin = builtin;
433
434         table->entries = g_list_insert_before(table->entries, before, e);
435         table->num_entries++;
436         table->size += entry->next_offset;
437
438         if (before == NULL) {
439                 e->offset = table->size - entry->next_offset;
440
441                 return 0;
442         }
443
444         entry_before = before->data;
445
446         /*
447          * We've just appended/insterted a new entry. All references
448          * should be bumped accordingly.
449          */
450         update_targets_reference(table, entry_before, e, FALSE);
451
452         update_offsets(table);
453
454         return 0;
455 }
456
457 static int remove_table_entry(struct connman_iptables *table,
458                                 struct connman_iptables_entry *entry)
459 {
460         int removed = 0;
461
462         table->num_entries--;
463         table->size -= entry->entry->next_offset;
464         removed = entry->entry->next_offset;
465
466         g_free(entry->entry);
467
468         table->entries = g_list_remove(table->entries, entry);
469
470         return removed;
471 }
472
473 static int iptables_flush_chain(struct connman_iptables *table,
474                                                 const char *name)
475 {
476         GList *chain_head, *chain_tail, *list, *next;
477         struct connman_iptables_entry *entry;
478         int builtin, removed = 0;
479
480         chain_head = find_chain_head(table, name);
481         if (chain_head == NULL)
482                 return -EINVAL;
483
484         chain_tail = find_chain_tail(table, name);
485         if (chain_tail == NULL)
486                 return -EINVAL;
487
488         entry = chain_head->data;
489         builtin = entry->builtin;
490
491         if (builtin >= 0)
492                 list = chain_head;
493         else
494                 list = chain_head->next;
495
496         if (list == chain_tail->prev)
497                 return 0;
498
499         while (list != chain_tail->prev) {
500                 entry = list->data;
501                 next = g_list_next(list);
502
503                 removed += remove_table_entry(table, entry);
504
505                 list = next;
506         }
507
508         if (builtin >= 0) {
509                 struct connman_iptables_entry *e;
510
511                 entry = list->data;
512
513                 entry->builtin = builtin;
514
515                 table->underflow[builtin] -= removed;
516
517                 for (list = chain_tail; list; list = list->next) {
518                         e = list->data;
519
520                         builtin = e->builtin;
521                         if (builtin < 0)
522                                 continue;
523
524                         table->hook_entry[builtin] -= removed;
525                         table->underflow[builtin] -= removed;
526                 }
527         }
528
529         update_offsets(table);
530
531         return 0;
532 }
533
534 static int iptables_add_chain(struct connman_iptables *table,
535                                 const char *name)
536 {
537         GList *last;
538         struct ipt_entry *entry_head;
539         struct ipt_entry *entry_return;
540         struct error_target *error;
541         struct ipt_standard_target *standard;
542         u_int16_t entry_head_size, entry_return_size;
543
544         last = g_list_last(table->entries);
545
546         /*
547          * An empty chain is composed of:
548          * - A head entry, with no match and an error target.
549          *   The error target data is the chain name.
550          * - A tail entry, with no match and a standard target.
551          *   The standard target verdict is XT_RETURN (return to the
552          *   caller).
553          */
554
555         /* head entry */
556         entry_head_size = sizeof(struct ipt_entry) +
557                                 sizeof(struct error_target);
558         entry_head = g_try_malloc0(entry_head_size);
559         if (entry_head == NULL)
560                 goto err_head;
561
562         memset(entry_head, 0, entry_head_size);
563
564         entry_head->target_offset = sizeof(struct ipt_entry);
565         entry_head->next_offset = entry_head_size;
566
567         error = (struct error_target *) entry_head->elems;
568         strcpy(error->t.u.user.name, IPT_ERROR_TARGET);
569         error->t.u.user.target_size = ALIGN(sizeof(struct error_target));
570         strcpy(error->error, name);
571
572         if (iptables_add_entry(table, entry_head, last, -1) < 0)
573                 goto err_head;
574
575         /* tail entry */
576         entry_return_size = sizeof(struct ipt_entry) +
577                                 sizeof(struct ipt_standard_target);
578         entry_return = g_try_malloc0(entry_return_size);
579         if (entry_return == NULL)
580                 goto err;
581
582         memset(entry_return, 0, entry_return_size);
583
584         entry_return->target_offset = sizeof(struct ipt_entry);
585         entry_return->next_offset = entry_return_size;
586
587         standard = (struct ipt_standard_target *) entry_return->elems;
588         standard->target.u.user.target_size =
589                                 ALIGN(sizeof(struct ipt_standard_target));
590         standard->verdict = XT_RETURN;
591
592         if (iptables_add_entry(table, entry_return, last, -1) < 0)
593                 goto err;
594
595         return 0;
596
597 err:
598         g_free(entry_return);
599 err_head:
600         g_free(entry_head);
601
602         return -ENOMEM;
603 }
604
605 static int iptables_delete_chain(struct connman_iptables *table,
606                                         const char *name)
607 {
608         struct connman_iptables_entry *entry;
609         GList *chain_head, *chain_tail;
610
611         chain_head = find_chain_head(table, name);
612         if (chain_head == NULL)
613                 return -EINVAL;
614
615         entry = chain_head->data;
616
617         /* We cannot remove builtin chain */
618         if (entry->builtin >= 0)
619                 return -EINVAL;
620
621         chain_tail = find_chain_tail(table, name);
622         if (chain_tail == NULL)
623                 return -EINVAL;
624
625         /* Chain must be flushed */
626         if (chain_head->next != chain_tail->prev)
627                 return -EINVAL;
628
629         remove_table_entry(table, entry);
630
631         entry = chain_tail->prev->data;
632         remove_table_entry(table, entry);
633
634         update_offsets(table);
635
636         return 0;
637 }
638
639 static struct ipt_entry *new_rule(struct ipt_ip *ip,
640                 const char *target_name, struct xtables_target *xt_t,
641                 struct xtables_rule_match *xt_rm)
642 {
643         struct xtables_rule_match *tmp_xt_rm;
644         struct ipt_entry *new_entry;
645         size_t match_size, target_size;
646
647         match_size = 0;
648         for (tmp_xt_rm = xt_rm; tmp_xt_rm != NULL; tmp_xt_rm = tmp_xt_rm->next)
649                 match_size += tmp_xt_rm->match->m->u.match_size;
650
651         if (xt_t)
652                 target_size = ALIGN(xt_t->t->u.target_size);
653         else
654                 target_size = ALIGN(sizeof(struct xt_standard_target));
655
656         new_entry = g_try_malloc0(sizeof(struct ipt_entry) + target_size +
657                                                                 match_size);
658         if (new_entry == NULL)
659                 return NULL;
660
661         memcpy(&new_entry->ip, ip, sizeof(struct ipt_ip));
662
663         new_entry->target_offset = sizeof(struct ipt_entry) + match_size;
664         new_entry->next_offset = sizeof(struct ipt_entry) + target_size +
665                                                                 match_size;
666
667         match_size = 0;
668         for (tmp_xt_rm = xt_rm; tmp_xt_rm != NULL;
669                                 tmp_xt_rm = tmp_xt_rm->next) {
670                 memcpy(new_entry->elems + match_size, tmp_xt_rm->match->m,
671                                         tmp_xt_rm->match->m->u.match_size);
672                 match_size += tmp_xt_rm->match->m->u.match_size;
673         }
674
675         if (xt_t) {
676                 struct xt_entry_target *entry_target;
677
678                 entry_target = ipt_get_target(new_entry);
679                 memcpy(entry_target, xt_t->t, target_size);
680         }
681
682         return new_entry;
683 }
684
685 static void update_hooks(struct connman_iptables *table, GList *chain_head,
686                                 struct ipt_entry *entry)
687 {
688         GList *list;
689         struct connman_iptables_entry *head, *e;
690         int builtin;
691
692         if (chain_head == NULL)
693                 return;
694
695         head = chain_head->data;
696
697         builtin = head->builtin;
698         if (builtin < 0)
699                 return;
700
701         table->underflow[builtin] += entry->next_offset;
702
703         for (list = chain_head->next; list; list = list->next) {
704                 e = list->data;
705
706                 builtin = e->builtin;
707                 if (builtin < 0)
708                         continue;
709
710                 table->hook_entry[builtin] += entry->next_offset;
711                 table->underflow[builtin] += entry->next_offset;
712         }
713 }
714
715 static struct ipt_entry *prepare_rule_inclusion(struct connman_iptables *table,
716                                 struct ipt_ip *ip, const char *chain_name,
717                                 const char *target_name,
718                                 struct xtables_target *xt_t,
719                                 int *builtin, struct xtables_rule_match *xt_rm)
720 {
721         GList *chain_tail, *chain_head;
722         struct ipt_entry *new_entry;
723         struct connman_iptables_entry *head;
724
725         chain_head = find_chain_head(table, chain_name);
726         if (chain_head == NULL)
727                 return NULL;
728
729         chain_tail = find_chain_tail(table, chain_name);
730         if (chain_tail == NULL)
731                 return NULL;
732
733         new_entry = new_rule(ip, target_name, xt_t, xt_rm);
734         if (new_entry == NULL)
735                 return NULL;
736
737         update_hooks(table, chain_head, new_entry);
738
739         /*
740          * If the chain is builtin, and does not have any rule,
741          * then the one that we're inserting is becoming the head
742          * and thus needs the builtin flag.
743          */
744         head = chain_head->data;
745         if (head->builtin < 0)
746                 *builtin = -1;
747         else if (chain_head == chain_tail->prev) {
748                 *builtin = head->builtin;
749                 head->builtin = -1;
750         }
751
752         return new_entry;
753 }
754
755 static int iptables_insert_rule(struct connman_iptables *table,
756                                 struct ipt_ip *ip, const char *chain_name,
757                                 const char *target_name,
758                                 struct xtables_target *xt_t,
759                                 struct xtables_rule_match *xt_rm)
760 {
761         struct ipt_entry *new_entry;
762         int builtin = -1, ret;
763         GList *chain_head;
764
765         chain_head = find_chain_head(table, chain_name);
766         if (chain_head == NULL)
767                 return -EINVAL;
768
769         new_entry = prepare_rule_inclusion(table, ip, chain_name,
770                                         target_name, xt_t, &builtin, xt_rm);
771         if (new_entry == NULL)
772                 return -EINVAL;
773
774         if (builtin == -1)
775                 chain_head = chain_head->next;
776
777         ret = iptables_add_entry(table, new_entry, chain_head, builtin);
778         if (ret < 0)
779                 g_free(new_entry);
780
781         return ret;
782 }
783
784 static gboolean is_same_ipt_entry(struct ipt_entry *i_e1,
785                                         struct ipt_entry *i_e2)
786 {
787         if (memcmp(&i_e1->ip, &i_e2->ip, sizeof(struct ipt_ip)) != 0)
788                 return FALSE;
789
790         if (i_e1->target_offset != i_e2->target_offset)
791                 return FALSE;
792
793         if (i_e1->next_offset != i_e2->next_offset)
794                 return FALSE;
795
796         return TRUE;
797 }
798
799 static gboolean is_same_target(struct xt_entry_target *xt_e_t1,
800                                         struct xt_entry_target *xt_e_t2)
801 {
802         if (xt_e_t1 == NULL || xt_e_t2 == NULL)
803                 return FALSE;
804
805         if (strcmp(xt_e_t1->u.user.name, IPT_STANDARD_TARGET) == 0) {
806                 struct xt_standard_target *xt_s_t1;
807                 struct xt_standard_target *xt_s_t2;
808
809                 xt_s_t1 = (struct xt_standard_target *) xt_e_t1;
810                 xt_s_t2 = (struct xt_standard_target *) xt_e_t2;
811
812                 if (xt_s_t1->verdict != xt_s_t2->verdict)
813                         return FALSE;
814         } else {
815                 if (xt_e_t1->u.target_size != xt_e_t2->u.target_size)
816                         return FALSE;
817
818                 if (strcmp(xt_e_t1->u.user.name, xt_e_t2->u.user.name) != 0)
819                         return FALSE;
820         }
821
822         return TRUE;
823 }
824
825 static gboolean is_same_match(struct xt_entry_match *xt_e_m1,
826                                 struct xt_entry_match *xt_e_m2)
827 {
828         if (xt_e_m1 == NULL || xt_e_m2 == NULL)
829                 return FALSE;
830
831         if (xt_e_m1->u.match_size != xt_e_m2->u.match_size)
832                 return FALSE;
833
834         if (xt_e_m1->u.user.revision != xt_e_m2->u.user.revision)
835                 return FALSE;
836
837         if (strcmp(xt_e_m1->u.user.name, xt_e_m2->u.user.name) != 0)
838                 return FALSE;
839
840         return TRUE;
841 }
842
843 static GList *find_existing_rule(struct connman_iptables *table,
844                                 struct ipt_ip *ip, const char *chain_name,
845                                 const char *target_name,
846                                 struct xtables_target *xt_t,
847                                 struct xtables_match *xt_m,
848                                 struct xtables_rule_match *xt_rm)
849 {
850         GList *chain_tail, *chain_head, *list;
851         struct xt_entry_target *xt_e_t = NULL;
852         struct xt_entry_match *xt_e_m = NULL;
853         struct connman_iptables_entry *entry;
854         struct ipt_entry *entry_test;
855         int builtin;
856
857         chain_head = find_chain_head(table, chain_name);
858         if (chain_head == NULL)
859                 return NULL;
860
861         chain_tail = find_chain_tail(table, chain_name);
862         if (chain_tail == NULL)
863                 return NULL;
864
865         if (!xt_t && !xt_m)
866                 return NULL;
867
868         entry_test = new_rule(ip, target_name, xt_t, xt_rm);
869         if (entry_test == NULL)
870                 return NULL;
871
872         if (xt_t != NULL)
873                 xt_e_t = ipt_get_target(entry_test);
874         if (xt_m != NULL)
875                 xt_e_m = (struct xt_entry_match *)entry_test->elems;
876
877         entry = chain_head->data;
878         builtin = entry->builtin;
879
880         if (builtin >= 0)
881                 list = chain_head;
882         else
883                 list = chain_head->next;
884
885         for (; list != chain_tail->prev; list = list->next) {
886                 struct connman_iptables_entry *tmp;
887                 struct ipt_entry *tmp_e;
888
889                 tmp = list->data;
890                 tmp_e = tmp->entry;
891
892                 if (is_same_ipt_entry(entry_test, tmp_e) == FALSE)
893                         continue;
894
895                 if (xt_t != NULL) {
896                         struct xt_entry_target *tmp_xt_e_t;
897
898                         tmp_xt_e_t = ipt_get_target(tmp_e);
899
900                         if (!is_same_target(tmp_xt_e_t, xt_e_t))
901                                 continue;
902                 }
903
904                 if (xt_m != NULL) {
905                         struct xt_entry_match *tmp_xt_e_m;
906
907                         tmp_xt_e_m = (struct xt_entry_match *)tmp_e->elems;
908
909                         if (!is_same_match(tmp_xt_e_m, xt_e_m))
910                                 continue;
911                 }
912
913                 break;
914         }
915
916         g_free(entry_test);
917
918         if (list != chain_tail->prev)
919                 return list;
920
921         return NULL;
922 }
923
924 static int iptables_delete_rule(struct connman_iptables *table,
925                                 struct ipt_ip *ip, const char *chain_name,
926                                 const char *target_name,
927                                 struct xtables_target *xt_t,
928                                 struct xtables_match *xt_m,
929                                 struct xtables_rule_match *xt_rm)
930 {
931         struct connman_iptables_entry *entry;
932         GList *chain_tail, *list;
933         int builtin, removed;
934
935         removed = 0;
936
937         chain_tail = find_chain_tail(table, chain_name);
938         if (chain_tail == NULL)
939                 return -EINVAL;
940
941         list = find_existing_rule(table, ip, chain_name, target_name,
942                                                         xt_t, xt_m, xt_rm);
943         if (list == NULL)
944                 return -EINVAL;
945
946         entry = list->data;
947         if (entry == NULL)
948                 return -EINVAL;
949
950         builtin = entry->builtin;
951
952         /* We have deleted a rule,
953          * all references should be bumped accordingly */
954         if (list->next != NULL)
955                 update_targets_reference(table, list->next->data,
956                                                 list->data, TRUE);
957
958         removed += remove_table_entry(table, entry);
959
960         if (builtin >= 0) {
961                 list = list->next;
962                 if (list) {
963                         entry = list->data;
964                         entry->builtin = builtin;
965                 }
966
967                 table->underflow[builtin] -= removed;
968                 for (list = chain_tail; list; list = list->next) {
969                         entry = list->data;
970
971                         builtin = entry->builtin;
972                         if (builtin < 0)
973                                 continue;
974
975                         table->hook_entry[builtin] -= removed;
976                         table->underflow[builtin] -= removed;
977                 }
978         }
979
980         update_offsets(table);
981
982         return 0;
983 }
984
985 static int iptables_change_policy(struct connman_iptables *table,
986                                 const char *chain_name, const char *policy)
987 {
988         GList *chain_head;
989         struct connman_iptables_entry *entry;
990         struct xt_entry_target *target;
991         struct xt_standard_target *t;
992         int verdict;
993
994         verdict = target_to_verdict(policy);
995         if (verdict == 0)
996                 return -EINVAL;
997
998         chain_head = find_chain_head(table, chain_name);
999         if (chain_head == NULL)
1000                 return -EINVAL;
1001
1002         entry = chain_head->data;
1003         if (entry->builtin < 0)
1004                 return -EINVAL;
1005
1006         target = ipt_get_target(entry->entry);
1007
1008         t = (struct xt_standard_target *)target;
1009         t->verdict = verdict;
1010
1011         return 0;
1012 }
1013
1014 static struct ipt_replace *iptables_blob(struct connman_iptables *table)
1015 {
1016         struct ipt_replace *r;
1017         GList *list;
1018         struct connman_iptables_entry *e;
1019         unsigned char *entry_index;
1020
1021         r = g_try_malloc0(sizeof(struct ipt_replace) + table->size);
1022         if (r == NULL)
1023                 return NULL;
1024
1025         memset(r, 0, sizeof(*r) + table->size);
1026
1027         r->counters = g_try_malloc0(sizeof(struct xt_counters)
1028                                 * table->old_entries);
1029         if (r->counters == NULL) {
1030                 g_free(r);
1031                 return NULL;
1032         }
1033
1034         strcpy(r->name, table->info->name);
1035         r->num_entries = table->num_entries;
1036         r->size = table->size;
1037
1038         r->num_counters = table->old_entries;
1039         r->valid_hooks  = table->info->valid_hooks;
1040
1041         memcpy(r->hook_entry, table->hook_entry, sizeof(table->hook_entry));
1042         memcpy(r->underflow, table->underflow, sizeof(table->underflow));
1043
1044         entry_index = (unsigned char *)r->entries;
1045         for (list = table->entries; list; list = list->next) {
1046                 e = list->data;
1047
1048                 memcpy(entry_index, e->entry, e->entry->next_offset);
1049                 entry_index += e->entry->next_offset;
1050         }
1051
1052         return r;
1053 }
1054
1055 static void dump_ip(struct ipt_entry *entry)
1056 {
1057         struct ipt_ip *ip = &entry->ip;
1058         char ip_string[INET6_ADDRSTRLEN];
1059         char ip_mask[INET6_ADDRSTRLEN];
1060
1061         if (strlen(ip->iniface))
1062                 DBG("\tin %s", ip->iniface);
1063
1064         if (strlen(ip->outiface))
1065                 DBG("\tout %s", ip->outiface);
1066
1067         if (inet_ntop(AF_INET, &ip->src, ip_string, INET6_ADDRSTRLEN) != NULL &&
1068                         inet_ntop(AF_INET, &ip->smsk,
1069                                         ip_mask, INET6_ADDRSTRLEN) != NULL)
1070                 DBG("\tsrc %s/%s", ip_string, ip_mask);
1071
1072         if (inet_ntop(AF_INET, &ip->dst, ip_string, INET6_ADDRSTRLEN) != NULL &&
1073                         inet_ntop(AF_INET, &ip->dmsk,
1074                                         ip_mask, INET6_ADDRSTRLEN) != NULL)
1075                 DBG("\tdst %s/%s", ip_string, ip_mask);
1076 }
1077
1078 static void dump_target(struct ipt_entry *entry)
1079
1080 {
1081         struct xtables_target *xt_t;
1082         struct xt_entry_target *target;
1083
1084         target = ipt_get_target(entry);
1085
1086         if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
1087                 struct xt_standard_target *t;
1088
1089                 t = (struct xt_standard_target *)target;
1090
1091                 switch (t->verdict) {
1092                 case XT_RETURN:
1093                         DBG("\ttarget RETURN");
1094                         break;
1095
1096                 case -NF_ACCEPT - 1:
1097                         DBG("\ttarget ACCEPT");
1098                         break;
1099
1100                 case -NF_DROP - 1:
1101                         DBG("\ttarget DROP");
1102                         break;
1103
1104                 case -NF_QUEUE - 1:
1105                         DBG("\ttarget QUEUE");
1106                         break;
1107
1108                 case -NF_STOP - 1:
1109                         DBG("\ttarget STOP");
1110                         break;
1111
1112                 default:
1113                         DBG("\tJUMP %u", t->verdict);
1114                         break;
1115                 }
1116
1117                 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
1118                                                 XTF_LOAD_MUST_SUCCEED);
1119
1120                 if(xt_t->print != NULL)
1121                         xt_t->print(NULL, target, 1);
1122         } else {
1123                 xt_t = xtables_find_target(target->u.user.name, XTF_TRY_LOAD);
1124                 if (xt_t == NULL) {
1125                         DBG("\ttarget %s", target->u.user.name);
1126                         return;
1127                 }
1128
1129                 if(xt_t->print != NULL) {
1130                         DBG("\ttarget ");
1131                         xt_t->print(NULL, target, 1);
1132                 }
1133         }
1134 }
1135
1136 static void dump_match(struct ipt_entry *entry)
1137 {
1138         struct xtables_match *xt_m;
1139         struct xt_entry_match *match;
1140
1141         if (entry->elems == (unsigned char *)entry + entry->target_offset)
1142                 return;
1143
1144         match = (struct xt_entry_match *) entry->elems;
1145
1146         if (!strlen(match->u.user.name))
1147                 return;
1148
1149         xt_m = xtables_find_match(match->u.user.name, XTF_TRY_LOAD, NULL);
1150         if (xt_m == NULL)
1151                 goto out;
1152
1153         if(xt_m->print != NULL) {
1154                 DBG("\tmatch ");
1155                 xt_m->print(NULL, match, 1);
1156
1157                 return;
1158         }
1159
1160 out:
1161         DBG("\tmatch %s", match->u.user.name);
1162
1163 }
1164
1165 static int dump_entry(struct ipt_entry *entry, int builtin,
1166                         unsigned int hook, size_t size, unsigned int offset,
1167                         void *user_data)
1168 {
1169         struct xt_entry_target *target;
1170
1171         target = ipt_get_target(entry);
1172
1173         if (offset + entry->next_offset == size) {
1174                 DBG("\tEnd of CHAIN");
1175                 return 0;
1176         }
1177
1178         if (!strcmp(target->u.user.name, IPT_ERROR_TARGET)) {
1179                 DBG("\tUSER CHAIN (%s) match %p  target %p",
1180                         target->data, entry->elems,
1181                         (char *)entry + entry->target_offset);
1182
1183                 return 0;
1184         } else if (builtin >= 0) {
1185                 DBG("\tCHAIN (%s) match %p  target %p",
1186                         hooknames[builtin], entry->elems,
1187                         (char *)entry + entry->target_offset);
1188         } else {
1189                 DBG("\tRULE  match %p  target %p",
1190                         entry->elems,
1191                         (char *)entry + entry->target_offset);
1192         }
1193
1194         dump_match(entry);
1195         dump_target(entry);
1196         dump_ip(entry);
1197
1198         return 0;
1199 }
1200
1201 static void dump_table(struct connman_iptables *table)
1202 {
1203         DBG("%s valid_hooks=0x%08x, num_entries=%u, size=%u",
1204                         table->info->name,
1205                         table->info->valid_hooks, table->info->num_entries,
1206                                 table->info->size);
1207
1208         DBG("entry hook: pre/in/fwd/out/post %d/%d/%d/%d/%d",
1209                 table->info->hook_entry[NF_IP_PRE_ROUTING],
1210                 table->info->hook_entry[NF_IP_LOCAL_IN],
1211                 table->info->hook_entry[NF_IP_FORWARD],
1212                 table->info->hook_entry[NF_IP_LOCAL_OUT],
1213                 table->info->hook_entry[NF_IP_POST_ROUTING]);
1214         DBG("underflow:  pre/in/fwd/out/post %d/%d/%d/%d/%d",
1215                 table->info->underflow[NF_IP_PRE_ROUTING],
1216                 table->info->underflow[NF_IP_LOCAL_IN],
1217                 table->info->underflow[NF_IP_FORWARD],
1218                 table->info->underflow[NF_IP_LOCAL_OUT],
1219                 table->info->underflow[NF_IP_POST_ROUTING]);
1220
1221         iterate_entries(table->blob_entries->entrytable,
1222                         table->info->valid_hooks,
1223                         table->info->hook_entry,
1224                         table->blob_entries->size,
1225                         print_entry, dump_entry);
1226 }
1227
1228 static void dump_ipt_replace(struct ipt_replace *repl)
1229 {
1230         DBG("%s valid_hooks 0x%08x  num_entries %u  size %u",
1231                         repl->name, repl->valid_hooks, repl->num_entries,
1232                         repl->size);
1233
1234         DBG("entry hook: pre/in/fwd/out/post %d/%d/%d/%d/%d",
1235                 repl->hook_entry[NF_IP_PRE_ROUTING],
1236                 repl->hook_entry[NF_IP_LOCAL_IN],
1237                 repl->hook_entry[NF_IP_FORWARD],
1238                 repl->hook_entry[NF_IP_LOCAL_OUT],
1239                 repl->hook_entry[NF_IP_POST_ROUTING]);
1240         DBG("underflow:  pre/in/fwd/out/post %d/%d/%d/%d/%d",
1241                 repl->underflow[NF_IP_PRE_ROUTING],
1242                 repl->underflow[NF_IP_LOCAL_IN],
1243                 repl->underflow[NF_IP_FORWARD],
1244                 repl->underflow[NF_IP_LOCAL_OUT],
1245                 repl->underflow[NF_IP_POST_ROUTING]);
1246
1247         iterate_entries(repl->entries, repl->valid_hooks,
1248                         repl->hook_entry, repl->size, print_entry, dump_entry);
1249 }
1250
1251 static int iptables_get_entries(struct connman_iptables *table)
1252 {
1253         socklen_t entry_size;
1254
1255         entry_size = sizeof(struct ipt_get_entries) + table->info->size;
1256
1257         return getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_ENTRIES,
1258                                 table->blob_entries, &entry_size);
1259 }
1260
1261 static int iptables_replace(struct connman_iptables *table,
1262                                         struct ipt_replace *r)
1263 {
1264         return setsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_SET_REPLACE, r,
1265                          sizeof(*r) + r->size);
1266 }
1267
1268 static int add_entry(struct ipt_entry *entry, int builtin, unsigned int hook,
1269                         size_t size, unsigned offset, void *user_data)
1270 {
1271         struct connman_iptables *table = user_data;
1272         struct ipt_entry *new_entry;
1273
1274         new_entry = g_try_malloc0(entry->next_offset);
1275         if (new_entry == NULL)
1276                 return -ENOMEM;
1277
1278         memcpy(new_entry, entry, entry->next_offset);
1279
1280         return iptables_add_entry(table, new_entry, NULL, builtin);
1281 }
1282
1283 static void table_cleanup(struct connman_iptables *table)
1284 {
1285         GList *list;
1286         struct connman_iptables_entry *entry;
1287
1288         if (table == NULL)
1289                 return;
1290
1291         if (table->ipt_sock >= 0)
1292                 close(table->ipt_sock);
1293
1294         for (list = table->entries; list; list = list->next) {
1295                 entry = list->data;
1296
1297                 g_free(entry->entry);
1298                 g_free(entry);
1299         }
1300
1301         g_list_free(table->entries);
1302         g_free(table->info);
1303         g_free(table->blob_entries);
1304         g_free(table);
1305 }
1306
1307 static struct connman_iptables *iptables_init(const char *table_name)
1308 {
1309         struct connman_iptables *table = NULL;
1310         char *module = NULL;
1311         socklen_t s;
1312
1313         if (table_name == NULL)
1314                 table_name = "filter";
1315
1316         DBG("%s", table_name);
1317
1318         if (xtables_insmod("ip_tables", NULL, TRUE) != 0)
1319                 DBG("ip_tables module loading gives error but trying anyway");
1320
1321         module = g_strconcat("iptable_", table_name, NULL);
1322         if (module == NULL)
1323                 return NULL;
1324
1325         if (xtables_insmod(module, NULL, TRUE) != 0)
1326                 DBG("%s module loading gives error but trying anyway", module);
1327
1328         g_free(module);
1329
1330         table = g_hash_table_lookup(table_hash, table_name);
1331         if (table != NULL)
1332                 return table;
1333
1334         table = g_try_new0(struct connman_iptables, 1);
1335         if (table == NULL)
1336                 return NULL;
1337
1338         table->info = g_try_new0(struct ipt_getinfo, 1);
1339         if (table->info == NULL)
1340                 goto err;
1341
1342         table->ipt_sock = socket(AF_INET, SOCK_RAW | SOCK_CLOEXEC, IPPROTO_RAW);
1343         if (table->ipt_sock < 0)
1344                 goto err;
1345
1346         s = sizeof(*table->info);
1347         strcpy(table->info->name, table_name);
1348         if (getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_INFO,
1349                                                 table->info, &s) < 0) {
1350                 connman_error("iptables support missing error %d (%s)", errno,
1351                         strerror(errno));
1352                 goto err;
1353         }
1354
1355         table->blob_entries = g_try_malloc0(sizeof(struct ipt_get_entries) +
1356                                                 table->info->size);
1357         if (table->blob_entries == NULL)
1358                 goto err;
1359
1360         strcpy(table->blob_entries->name, table_name);
1361         table->blob_entries->size = table->info->size;
1362
1363         if (iptables_get_entries(table) < 0)
1364                 goto err;
1365
1366         table->num_entries = 0;
1367         table->old_entries = table->info->num_entries;
1368         table->size = 0;
1369
1370         memcpy(table->underflow, table->info->underflow,
1371                                 sizeof(table->info->underflow));
1372         memcpy(table->hook_entry, table->info->hook_entry,
1373                                 sizeof(table->info->hook_entry));
1374
1375         iterate_entries(table->blob_entries->entrytable,
1376                         table->info->valid_hooks, table->info->hook_entry,
1377                         table->blob_entries->size, add_entry, table);
1378
1379         g_hash_table_insert(table_hash, g_strdup(table_name), table);
1380
1381         if (debug_enabled == TRUE)
1382                 dump_table(table);
1383
1384         return table;
1385
1386 err:
1387         table_cleanup(table);
1388
1389         return NULL;
1390 }
1391
1392 static struct option iptables_opts[] = {
1393         {.name = "append",        .has_arg = 1, .val = 'A'},
1394         {.name = "compare",       .has_arg = 1, .val = 'C'},
1395         {.name = "delete",        .has_arg = 1, .val = 'D'},
1396         {.name = "flush-chain",   .has_arg = 1, .val = 'F'},
1397         {.name = "insert",        .has_arg = 1, .val = 'I'},
1398         {.name = "list",          .has_arg = 2, .val = 'L'},
1399         {.name = "new-chain",     .has_arg = 1, .val = 'N'},
1400         {.name = "policy",        .has_arg = 1, .val = 'P'},
1401         {.name = "delete-chain",  .has_arg = 1, .val = 'X'},
1402         {.name = "destination",   .has_arg = 1, .val = 'd'},
1403         {.name = "in-interface",  .has_arg = 1, .val = 'i'},
1404         {.name = "jump",          .has_arg = 1, .val = 'j'},
1405         {.name = "match",         .has_arg = 1, .val = 'm'},
1406         {.name = "out-interface", .has_arg = 1, .val = 'o'},
1407         {.name = "source",        .has_arg = 1, .val = 's'},
1408         {.name = "table",         .has_arg = 1, .val = 't'},
1409         {NULL},
1410 };
1411
1412 struct xtables_globals iptables_globals = {
1413         .option_offset = 0,
1414         .opts = iptables_opts,
1415         .orig_opts = iptables_opts,
1416 };
1417
1418 static struct xtables_target *prepare_target(struct connman_iptables *table,
1419                                                         const char *target_name)
1420 {
1421         struct xtables_target *xt_t = NULL;
1422         gboolean is_builtin, is_user_defined;
1423         GList *chain_head = NULL;
1424         size_t target_size;
1425
1426         is_builtin = FALSE;
1427         is_user_defined = FALSE;
1428
1429         if (is_builtin_target(target_name))
1430                 is_builtin = TRUE;
1431         else {
1432                 chain_head = find_chain_head(table, target_name);
1433                 if (chain_head != NULL && chain_head->next != NULL)
1434                         is_user_defined = TRUE;
1435         }
1436
1437         if (is_builtin || is_user_defined)
1438                 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
1439                                                 XTF_LOAD_MUST_SUCCEED);
1440         else
1441                 xt_t = xtables_find_target(target_name, XTF_TRY_LOAD);
1442
1443         if (xt_t == NULL)
1444                 return NULL;
1445
1446         target_size = ALIGN(sizeof(struct ipt_entry_target)) + xt_t->size;
1447
1448         xt_t->t = g_try_malloc0(target_size);
1449         if (xt_t->t == NULL)
1450                 return NULL;
1451
1452         xt_t->t->u.target_size = target_size;
1453
1454         if (is_builtin || is_user_defined) {
1455                 struct xt_standard_target *target;
1456
1457                 target = (struct xt_standard_target *)(xt_t->t);
1458                 strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
1459
1460                 if (is_builtin == TRUE)
1461                         target->verdict = target_to_verdict(target_name);
1462                 else if (is_user_defined == TRUE) {
1463                         struct connman_iptables_entry *target_rule;
1464
1465                         if (chain_head == NULL) {
1466                                 g_free(xt_t->t);
1467                                 return NULL;
1468                         }
1469
1470                         target_rule = chain_head->next->data;
1471                         target->verdict = target_rule->offset;
1472                 }
1473         } else {
1474                 strcpy(xt_t->t->u.user.name, target_name);
1475                 xt_t->t->u.user.revision = xt_t->revision;
1476                 if (xt_t->init != NULL)
1477                         xt_t->init(xt_t->t);
1478         }
1479
1480 #if XTABLES_VERSION_CODE > 5
1481         if (xt_t->x6_options != NULL)
1482                 iptables_globals.opts =
1483                         xtables_options_xfrm(
1484                                 iptables_globals.orig_opts,
1485                                 iptables_globals.opts,
1486                                 xt_t->x6_options,
1487                                 &xt_t->option_offset);
1488         else
1489 #endif
1490                 iptables_globals.opts =
1491                         xtables_merge_options(
1492 #if XTABLES_VERSION_CODE > 5
1493                                 iptables_globals.orig_opts,
1494 #endif
1495                                 iptables_globals.opts,
1496                                 xt_t->extra_opts,
1497                                 &xt_t->option_offset);
1498
1499         if (iptables_globals.opts == NULL) {
1500                 g_free(xt_t->t);
1501                 xt_t = NULL;
1502         }
1503
1504         return xt_t;
1505 }
1506
1507 static struct xtables_match *prepare_matches(struct connman_iptables *table,
1508                                         struct xtables_rule_match **xt_rm,
1509                                         const char *match_name)
1510 {
1511         struct xtables_match *xt_m;
1512         size_t match_size;
1513
1514         if (match_name == NULL)
1515                 return NULL;
1516
1517         xt_m = xtables_find_match(match_name, XTF_LOAD_MUST_SUCCEED, xt_rm);
1518         match_size = ALIGN(sizeof(struct ipt_entry_match)) + xt_m->size;
1519
1520         xt_m->m = g_try_malloc0(match_size);
1521         if (xt_m->m == NULL)
1522                 return NULL;
1523
1524         xt_m->m->u.match_size = match_size;
1525         strcpy(xt_m->m->u.user.name, xt_m->name);
1526         xt_m->m->u.user.revision = xt_m->revision;
1527
1528         if (xt_m->init != NULL)
1529                 xt_m->init(xt_m->m);
1530
1531         if (xt_m == xt_m->next)
1532                 goto done;
1533
1534 #if XTABLES_VERSION_CODE > 5
1535         if (xt_m->x6_options != NULL)
1536                 iptables_globals.opts =
1537                         xtables_options_xfrm(
1538                                 iptables_globals.orig_opts,
1539                                 iptables_globals.opts,
1540                                 xt_m->x6_options,
1541                                 &xt_m->option_offset);
1542         else
1543 #endif
1544                         iptables_globals.opts =
1545                         xtables_merge_options(
1546 #if XTABLES_VERSION_CODE > 5
1547                                 iptables_globals.orig_opts,
1548 #endif
1549                                 iptables_globals.opts,
1550                                 xt_m->extra_opts,
1551                                 &xt_m->option_offset);
1552
1553         if (iptables_globals.opts == NULL) {
1554                 g_free(xt_m->m);
1555                 xt_m = NULL;
1556         }
1557
1558 done:
1559         return xt_m;
1560 }
1561
1562 static int parse_ip_and_mask(const char *str, struct in_addr *ip, struct in_addr *mask)
1563 {
1564         char **tokens;
1565         uint32_t prefixlength;
1566         uint32_t tmp;
1567         int err;
1568
1569         tokens = g_strsplit(str, "/", 2);
1570         if (tokens == NULL)
1571                 return -1;
1572
1573         if (!inet_pton(AF_INET, tokens[0], ip)) {
1574                 err = -1;
1575                 goto out;
1576         }
1577
1578         if (tokens[1] != NULL) {
1579                 prefixlength = strtol(tokens[1], NULL, 10);
1580                 if (prefixlength > 31) {
1581                         err = -1;
1582                         goto out;
1583                 }
1584
1585                 tmp = ~(0xffffffff >> prefixlength);
1586         } else {
1587                 tmp = 0xffffffff;
1588         }
1589
1590         mask->s_addr = htonl(tmp);
1591         ip->s_addr = ip->s_addr & mask->s_addr;
1592         err = 0;
1593 out:
1594         g_strfreev(tokens);
1595
1596         return err;
1597 }
1598
1599 static struct connman_iptables *pre_load_table(const char *table_name,
1600                                         struct connman_iptables *table)
1601 {
1602         if (table != NULL)
1603                 return table;
1604
1605         return iptables_init(table_name);
1606 }
1607
1608 static void clear_tables_flags(void)
1609 {
1610         struct xtables_match *xt_m;
1611         struct xtables_target *xt_t;
1612
1613         /*
1614          * Clear all flags because the flags are only valid
1615          * for one rule.
1616          */
1617         for (xt_m = xtables_matches; xt_m != NULL; xt_m = xt_m->next)
1618                 xt_m->mflags = 0;
1619
1620         for (xt_t = xtables_targets; xt_t != NULL; xt_t = xt_t->next) {
1621                 xt_t->tflags = 0;
1622                 xt_t->used = 0;
1623         }
1624 }
1625
1626 struct parse_context {
1627         int argc;
1628         char **argv;
1629         struct ipt_ip *ip;
1630         struct xtables_target *xt_t;
1631         struct xtables_match *xt_m;
1632         struct xtables_rule_match *xt_rm;
1633 };
1634
1635 static int prepare_getopt_args(const char *str, struct parse_context *ctx)
1636 {
1637         char **tokens;
1638         int i;
1639
1640         tokens = g_strsplit_set(str, " ", -1);
1641
1642         for (i = 0; tokens[i]; i++);
1643
1644         /* Add space for the argv[0] value */
1645         ctx->argc = i + 1;
1646
1647         /* Don't forget the last NULL entry */
1648         ctx->argv = g_try_malloc0((ctx->argc + 1) * sizeof(char *));
1649         if (ctx->argv == NULL) {
1650                 g_strfreev(tokens);
1651                 return -ENOMEM;
1652         }
1653
1654         /*
1655          * getopt_long() jumps over the first token; we need to add some
1656          * random argv[0] entry.
1657          */
1658         ctx->argv[0] = g_strdup("argh");
1659         for (i = 1; i < ctx->argc; i++)
1660                 ctx->argv[i] = tokens[i - 1];
1661
1662         return 0;
1663 }
1664
1665 #if XTABLES_VERSION_CODE > 5
1666
1667 static int parse_xt_modules(int c, connman_bool_t invert,
1668                                 struct parse_context *ctx)
1669 {
1670         struct xtables_match *m;
1671         struct xtables_rule_match *rm;
1672
1673         DBG("xtables version code > 5");
1674
1675         for (rm = ctx->xt_rm; rm != NULL; rm = rm->next) {
1676                 if (rm->completed != 0)
1677                         continue;
1678
1679                 m = rm->match;
1680
1681                 if (m->x6_parse == NULL && m->parse == NULL)
1682                         continue;
1683
1684                 if (c < (int) m->option_offset ||
1685                                 c >= (int) m->option_offset
1686                                         + XT_OPTION_OFFSET_SCALE)
1687                         continue;
1688
1689                 xtables_option_mpcall(c, ctx->argv, invert, m, NULL);
1690         }
1691
1692         if (ctx->xt_t == NULL)
1693                 return 0;
1694
1695         if (ctx->xt_t->x6_parse == NULL && ctx->xt_t->parse == NULL)
1696                 return 0;
1697
1698         if (c < (int) ctx->xt_t->option_offset ||
1699                         c >= (int) ctx->xt_t->option_offset
1700                                         + XT_OPTION_OFFSET_SCALE)
1701                 return 0;
1702
1703         xtables_option_tpcall(c, ctx->argv, invert, ctx->xt_t, NULL);
1704
1705         return 0;
1706 }
1707
1708 static int final_check_xt_modules(struct parse_context *ctx)
1709 {
1710         struct xtables_rule_match *rm;
1711
1712         DBG("xtables version code > 5");
1713
1714         for (rm = ctx->xt_rm; rm != NULL; rm = rm->next)
1715                 xtables_option_mfcall(rm->match);
1716
1717         if (ctx->xt_t != NULL)
1718                 xtables_option_tfcall(ctx->xt_t);
1719
1720         return 0;
1721 }
1722
1723 #else
1724
1725 static int parse_xt_modules(int c, connman_bool_t invert,
1726                                 struct parse_context *ctx)
1727 {
1728         struct xtables_match *m;
1729         struct xtables_rule_match *rm;
1730         int err;
1731
1732         DBG("xtables version code <= 5");
1733
1734         for (rm = ctx->xt_rm; rm != NULL; rm = rm->next) {
1735                 if (rm->completed == 1)
1736                         continue;
1737
1738                 m = rm->match;
1739
1740                 if (m->parse == NULL)
1741                         continue;
1742
1743                 err = m->parse(c - m->option_offset,
1744                                 argv, invert, &m->mflags,
1745                                 NULL, &m->m);
1746                 if (err > 0)
1747                         return -err;
1748         }
1749
1750         if (ctx->xt_t == NULL)
1751                 return 0;
1752
1753         if (ctx->xt_t->parse == NULL)
1754                 return 0;
1755
1756         err = ctx->xt_m->parse(c - ctx->xt_m->option_offset,
1757                                 ctx->argv, invert, &ctx->xt_m->mflags,
1758                                 NULL, &ctx->xt_m->m);
1759         return -err;
1760 }
1761
1762 static int final_check_xt_modules(struct parse_context *ctx)
1763 {
1764         struct xtables_rule_match *rm;
1765
1766         DBG("xtables version code <= 5");
1767
1768         for (rm = ctx->xt_rm; rm != NULL; rm = rm->next)
1769                 if (rm->match->final_check != NULL)
1770                         rm->match->final_check(rm->match->mflags);
1771
1772         if (ctx->xt_t != NULL && ctx->xt_t->final_check != NULL)
1773                 ctx->xt_t->final_check(ctx->xt_t->tflags);
1774
1775         return 0;
1776 }
1777
1778 #endif
1779
1780 static int parse_rule_spec(struct connman_iptables *table,
1781                                 struct parse_context *ctx)
1782 {
1783         /*
1784          * How the parser works:
1785          *
1786          *  - If getopt finds 's', 'd', 'i', 'o'.
1787          *    just extract the information.
1788          *  - if '!' is found, set the invert flag to true and
1789          *    removes the '!' from the optarg string and jumps
1790          *    back to getopt to reparse the current optarg string.
1791          *    After reparsing the invert flag is reseted to false.
1792          *  - If 'm' or 'j' is found then call either
1793          *    prepare_matches() or prepare_target(). Those function
1794          *    will modify (extend) the longopts for getopt_long.
1795          *    That means getopt will change its matching context according
1796          *    the loaded target.
1797          *
1798          *    Here an example with iptables-test
1799          *
1800          *    argv[0] = ./tools/iptables-test
1801          *    argv[1] = -t
1802          *    argv[2] = filter
1803          *    argv[3] = -A
1804          *    argv[4] = INPUT
1805          *    argv[5] = -m
1806          *    argv[6] = mark
1807          *    argv[7] = --mark
1808          *    argv[8] = 999
1809          *    argv[9] = -j
1810          *    argv[10] = LOG
1811          *
1812          *    getopt found 'm' then the optarg is "mark" and optind 7
1813          *    The longopts array containts before hitting the `case 'm'`
1814          *
1815          *    val A has_arg 1 name append
1816          *    val C has_arg 1 name compare
1817          *    val D has_arg 1 name delete
1818          *    val F has_arg 1 name flush-chain
1819          *    val I has_arg 1 name insert
1820          *    val L has_arg 2 name list
1821          *    val N has_arg 1 name new-chain
1822          *    val P has_arg 1 name policy
1823          *    val X has_arg 1 name delete-chain
1824          *    val d has_arg 1 name destination
1825          *    val i has_arg 1 name in-interface
1826          *    val j has_arg 1 name jump
1827          *    val m has_arg 1 name match
1828          *    val o has_arg 1 name out-interface
1829          *    val s has_arg 1 name source
1830          *    val t has_arg 1 name table
1831          *
1832          *    After executing the `case 'm'` block longopts is
1833          *
1834          *    val A has_arg 1 name append
1835          *    val C has_arg 1 name compare
1836          *    val D has_arg 1 name delete
1837          *    val F has_arg 1 name flush-chain
1838          *    val I has_arg 1 name insert
1839          *    val L has_arg 2 name list
1840          *    val N has_arg 1 name new-chain
1841          *    val P has_arg 1 name policy
1842          *    val X has_arg 1 name delete-chain
1843          *    val d has_arg 1 name destination
1844          *    val i has_arg 1 name in-interface
1845          *    val j has_arg 1 name jump
1846          *    val m has_arg 1 name match
1847          *    val o has_arg 1 name out-interface
1848          *    val s has_arg 1 name source
1849          *    val t has_arg 1 name table
1850          *    val   has_arg 1 name mark
1851          *
1852          *    So the 'mark' matcher has added the 'mark' options
1853          *    and getopt will then return c '256' optarg "999" optind 9
1854          *    And we will hit the 'default' statement which then
1855          *    will call the matchers parser (xt_m->parser() or
1856          *    xtables_option_mpcall() depending on which version
1857          *    of libxtables is found.
1858          */
1859         connman_bool_t invert = FALSE;
1860         int len, c, err;
1861
1862         DBG("");
1863
1864         ctx->ip = g_try_new0(struct ipt_ip, 1);
1865         if (ctx->ip == NULL)
1866                 return -ENOMEM;
1867
1868         /*
1869          * As side effect parsing a rule sets some global flags
1870          * which will be evaluated/verified. Let's reset them
1871          * to ensure we can parse more than one rule.
1872          */
1873         clear_tables_flags();
1874
1875         /*
1876          * Tell getopt_long not to generate error messages for unknown
1877          * options and also reset optind back to 0.
1878          */
1879         opterr = 0;
1880         optind = 0;
1881
1882         while ((c = getopt_long(ctx->argc, ctx->argv,
1883                                         "-:d:i:o:s:m:j:",
1884                                         iptables_globals.opts, NULL)) != -1) {
1885                 switch (c) {
1886                 case 's':
1887                         /* Source specification */
1888                         if (!parse_ip_and_mask(optarg,
1889                                                 &ctx->ip->src,
1890                                                 &ctx->ip->smsk))
1891                                 break;
1892
1893                         if (invert)
1894                                 ctx->ip->invflags |= IPT_INV_SRCIP;
1895
1896                         break;
1897                 case 'd':
1898                         /* Destination specification */
1899                         if (!parse_ip_and_mask(optarg,
1900                                                 &ctx->ip->dst,
1901                                                 &ctx->ip->dmsk))
1902                                 break;
1903
1904                         if (invert)
1905                                 ctx->ip->invflags |= IPT_INV_DSTIP;
1906                         break;
1907                 case 'i':
1908                         /* In interface specification */
1909                         len = strlen(optarg);
1910
1911                         if (len + 1 > IFNAMSIZ)
1912                                 break;
1913
1914                         strcpy(ctx->ip->iniface, optarg);
1915                         memset(ctx->ip->iniface_mask, 0xff, len + 1);
1916
1917                         if (invert)
1918                                 ctx->ip->invflags |= IPT_INV_VIA_IN;
1919
1920                         break;
1921                 case 'o':
1922                         /* Out interface specification */
1923                         len = strlen(optarg);
1924
1925                         if (len + 1 > IFNAMSIZ)
1926                                 break;
1927
1928                         strcpy(ctx->ip->outiface, optarg);
1929                         memset(ctx->ip->outiface_mask, 0xff, len + 1);
1930
1931                         if (invert)
1932                                 ctx->ip->invflags |= IPT_INV_VIA_OUT;
1933
1934                         break;
1935                 case 'm':
1936                         /* Matches */
1937                         ctx->xt_m = prepare_matches(table, &ctx->xt_rm, optarg);
1938                         if (ctx->xt_m == NULL) {
1939                                 err = -EINVAL;
1940                                 goto out;
1941                         }
1942
1943                         break;
1944                 case 'j':
1945                         /* Target */
1946                         ctx->xt_t = prepare_target(table, optarg);
1947                         if (ctx->xt_t == NULL) {
1948                                 err = -EINVAL;
1949                                 goto out;
1950                         }
1951
1952                         break;
1953                 case 1:
1954                         if (optarg[0] == '!' && optarg[1] == '\0') {
1955                                 invert = TRUE;
1956
1957                                 /* Remove the '!' from the optarg */
1958                                 optarg[0] = '\0';
1959
1960                                 /*
1961                                  * And recall getopt_long without reseting
1962                                  * invert.
1963                                  */
1964                                 continue;
1965                         }
1966
1967                         break;
1968                 default:
1969                         err = parse_xt_modules(c, invert, ctx);
1970                         if (err == 1)
1971                                 continue;
1972
1973                         break;
1974                 }
1975
1976                 invert = FALSE;
1977         }
1978
1979         err = final_check_xt_modules(ctx);
1980
1981 out:
1982         return err;
1983 }
1984
1985 static void cleanup_parse_context(struct parse_context *ctx)
1986 {
1987         g_strfreev(ctx->argv);
1988         g_free(ctx->ip);
1989         if (ctx->xt_t != NULL)
1990                 g_free(ctx->xt_t->t);
1991         if (ctx->xt_m != NULL)
1992                 g_free(ctx->xt_m->m);
1993         g_free(ctx);
1994 }
1995
1996 int __connman_iptables_new_chain(const char *table_name,
1997                                         const char *chain)
1998 {
1999         struct connman_iptables *table;
2000
2001         DBG("-t %s -N %s", table_name, chain);
2002
2003         table = pre_load_table(table_name, NULL);
2004         if (table == NULL)
2005                 return -EINVAL;
2006
2007         return iptables_add_chain(table, chain);
2008 }
2009
2010 int __connman_iptables_delete_chain(const char *table_name,
2011                                         const char *chain)
2012 {
2013         struct connman_iptables *table;
2014
2015         DBG("-t %s -X %s", table_name, chain);
2016
2017         table = pre_load_table(table_name, NULL);
2018         if (table == NULL)
2019                 return -EINVAL;
2020
2021         return iptables_delete_chain(table, chain);
2022 }
2023
2024 int __connman_iptables_flush_chain(const char *table_name,
2025                                         const char *chain)
2026 {
2027         struct connman_iptables *table;
2028
2029         DBG("-t %s -F %s", table_name, chain);
2030
2031         table = pre_load_table(table_name, NULL);
2032         if (table == NULL)
2033                 return -EINVAL;
2034
2035         return iptables_flush_chain(table, chain);
2036 }
2037
2038 int __connman_iptables_change_policy(const char *table_name,
2039                                         const char *chain,
2040                                         const char *policy)
2041 {
2042         struct connman_iptables *table;
2043
2044         DBG("-t %s -F %s", table_name, chain);
2045
2046         table = pre_load_table(table_name, NULL);
2047         if (table == NULL)
2048                 return -EINVAL;
2049
2050         return iptables_change_policy(table, chain, policy);
2051 }
2052
2053 int __connman_iptables_append(const char *table_name,
2054                                 const char *chain,
2055                                 const char *rule_spec)
2056 {
2057         struct connman_iptables *table;
2058         struct parse_context *ctx;
2059         const char *target_name;
2060         int err;
2061
2062         ctx = g_try_new0(struct parse_context, 1);
2063         if (ctx == NULL)
2064                 return -ENOMEM;
2065
2066         DBG("-t %s -A %s %s", table_name, chain, rule_spec);
2067
2068         err = prepare_getopt_args(rule_spec, ctx);
2069         if (err < 0)
2070                 goto out;
2071
2072         table = pre_load_table(table_name, NULL);
2073         if (table == NULL) {
2074                 err = -EINVAL;
2075                 goto out;
2076         }
2077
2078         err = parse_rule_spec(table, ctx);
2079         if (err < 0)
2080                 goto out;
2081
2082         if (ctx->xt_t == NULL)
2083                 target_name = NULL;
2084         else
2085                 target_name = ctx->xt_t->name;
2086
2087         err = iptables_insert_rule(table, ctx->ip, chain,
2088                                 target_name, ctx->xt_t, ctx->xt_rm);
2089 out:
2090         cleanup_parse_context(ctx);
2091
2092         return err;
2093 }
2094
2095 int __connman_iptables_delete(const char *table_name,
2096                                 const char *chain,
2097                                 const char *rule_spec)
2098 {
2099         struct connman_iptables *table;
2100         struct parse_context *ctx;
2101         const char *target_name;
2102         int err;
2103
2104         ctx = g_try_new0(struct parse_context, 1);
2105         if (ctx == NULL)
2106                 return -ENOMEM;
2107
2108         DBG("-t %s -D %s %s", table_name, chain, rule_spec);
2109
2110         err = prepare_getopt_args(rule_spec, ctx);
2111         if (err < 0)
2112                 goto out;
2113
2114         table = pre_load_table(table_name, NULL);
2115         if (table == NULL) {
2116                 err = -EINVAL;
2117                 goto out;
2118         }
2119
2120         err = parse_rule_spec(table, ctx);
2121         if (err < 0)
2122                 goto out;
2123
2124         if (ctx->xt_t == NULL)
2125                 target_name = NULL;
2126         else
2127                 target_name = ctx->xt_t->name;
2128
2129         err = iptables_delete_rule(table, ctx->ip, chain,
2130                                 target_name, ctx->xt_t, ctx->xt_m,
2131                                 ctx->xt_rm);
2132 out:
2133         cleanup_parse_context(ctx);
2134
2135         return err;
2136 }
2137
2138 int __connman_iptables_commit(const char *table_name)
2139 {
2140         struct connman_iptables *table;
2141         struct ipt_replace *repl;
2142         int err;
2143
2144         DBG("%s", table_name);
2145
2146         table = g_hash_table_lookup(table_hash, table_name);
2147         if (table == NULL)
2148                 return -EINVAL;
2149
2150         repl = iptables_blob(table);
2151
2152         if (debug_enabled == TRUE)
2153                 dump_ipt_replace(repl);
2154
2155         err = iptables_replace(table, repl);
2156
2157         g_free(repl->counters);
2158         g_free(repl);
2159
2160         if (err < 0)
2161             return err;
2162
2163         g_hash_table_remove(table_hash, table_name);
2164
2165         return 0;
2166 }
2167
2168 static void remove_table(gpointer user_data)
2169 {
2170         struct connman_iptables *table = user_data;
2171
2172         table_cleanup(table);
2173 }
2174
2175 static int flush_table_cb(struct ipt_entry *entry, int builtin,
2176                                 unsigned int hook, size_t size,
2177                                 unsigned int offset, void *user_data)
2178 {
2179         GSList **chains = user_data;
2180         struct xt_entry_target *target;
2181         char *name;
2182
2183         if (offset + entry->next_offset == size)
2184                 return 0;
2185
2186         target = ipt_get_target(entry);
2187
2188         if (!strcmp(target->u.user.name, IPT_ERROR_TARGET))
2189                 name = g_strdup((const char*)target->data);
2190         else if (builtin >= 0)
2191                   name = g_strdup(hooknames[builtin]);
2192         else
2193                 return 0;
2194
2195         *chains = g_slist_prepend(*chains, name);
2196
2197         return 0;
2198 }
2199
2200 static void flush_table(const char *name)
2201 {
2202         GSList *chains = NULL, *list;
2203         struct connman_iptables *table;
2204
2205         table = pre_load_table(name, NULL);
2206         if (table == NULL)
2207                 return;
2208
2209         iterate_entries(table->blob_entries->entrytable,
2210                         table->info->valid_hooks,
2211                         table->info->hook_entry,
2212                         table->blob_entries->size,
2213                         flush_table_cb, &chains);
2214
2215         for (list = chains; list != NULL; list = list->next) {
2216                 char *chain = list->data;
2217
2218                 DBG("chain %s", chain);
2219                 iptables_flush_chain(table, chain);
2220         }
2221
2222         __connman_iptables_commit(name);
2223         g_slist_free_full(chains, g_free);
2224 }
2225
2226 static void flush_all_chains(void)
2227 {
2228         flush_table("filter");
2229         flush_table("mangle");
2230         flush_table("nat");
2231 }
2232
2233 int __connman_iptables_init(void)
2234 {
2235         DBG("");
2236
2237         if (getenv("CONNMAN_IPTABLES_DEBUG"))
2238                 debug_enabled = TRUE;
2239
2240         table_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2241                                                 g_free, remove_table);
2242
2243         xtables_init_all(&iptables_globals, NFPROTO_IPV4);
2244
2245         flush_all_chains();
2246
2247         return 0;
2248 }
2249
2250 void __connman_iptables_cleanup(void)
2251 {
2252         DBG("");
2253
2254         g_hash_table_destroy(table_hash);
2255
2256         xtables_free_opts(1);
2257 }