tools: Tiny indentation fix in iptables-test
[framework/connectivity/connman.git] / tools / iptables-test.c
1 /*
2  *  Connection Manager
3  *
4  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License version 2 as
8  *  published by the Free Software Foundation.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  */
20
21 #include <getopt.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/errno.h>
27 #include <sys/socket.h>
28 #include <sys/types.h>
29 #include <arpa/inet.h>
30 #include <xtables.h>
31
32 #include <linux/netfilter_ipv4/ip_tables.h>
33
34 #include <glib.h>
35
36 static const char *hooknames[] = {
37         [NF_IP_PRE_ROUTING]     = "PREROUTING",
38         [NF_IP_LOCAL_IN]        = "INPUT",
39         [NF_IP_FORWARD]         = "FORWARD",
40         [NF_IP_LOCAL_OUT]       = "OUTPUT",
41         [NF_IP_POST_ROUTING]    = "POSTROUTING",
42 };
43
44 #define LABEL_ACCEPT  "ACCEPT"
45 #define LABEL_DROP    "DROP"
46 #define LABEL_QUEUE   "QUEUE"
47 #define LABEL_RETURN  "RETURN"
48
49 /* fn returns 0 to continue iteration */
50 #define _XT_ENTRY_ITERATE_CONTINUE(type, entries, size, n, fn, args...) \
51 ({                                                              \
52         unsigned int __i;                                       \
53         int __n;                                                \
54         int __ret = 0;                                          \
55         type *__entry;                                          \
56                                                                 \
57         for (__i = 0, __n = 0; __i < (size);                    \
58              __i += __entry->next_offset, __n++) {              \
59                 __entry = (void *)(entries) + __i;              \
60                 if (__n < n)                                    \
61                         continue;                               \
62                                                                 \
63                 __ret = fn(__entry,  ## args);                  \
64                 if (__ret != 0)                                 \
65                         break;                                  \
66         }                                                       \
67         __ret;                                                  \
68 })
69
70 /* fn returns 0 to continue iteration */
71 #define _XT_ENTRY_ITERATE(type, entries, size, fn, args...) \
72         _XT_ENTRY_ITERATE_CONTINUE(type, entries, size, 0, fn, args)
73
74 #define ENTRY_ITERATE(entries, size, fn, args...) \
75         _XT_ENTRY_ITERATE(struct ipt_entry, entries, size, fn, ## args)
76
77 #define MIN_ALIGN (__alignof__(struct ipt_entry))
78
79 #define ALIGN(s) (((s) + ((MIN_ALIGN)-1)) & ~((MIN_ALIGN)-1))
80
81 struct error_target {
82         struct xt_entry_target t;
83         char error[IPT_TABLE_MAXNAMELEN];
84 };
85
86 struct connman_iptables_entry {
87         int offset;
88         int builtin;
89
90         struct ipt_entry *entry;
91 };
92
93 struct connman_iptables {
94         int ipt_sock;
95
96         struct ipt_getinfo *info;
97         struct ipt_get_entries *blob_entries;
98
99         unsigned int num_entries;
100         unsigned int old_entries;
101         unsigned int size;
102
103         unsigned int underflow[NF_INET_NUMHOOKS];
104         unsigned int hook_entry[NF_INET_NUMHOOKS];
105
106         GList *entries;
107 };
108
109
110 static struct ipt_entry *get_entry(struct connman_iptables *table,
111                                         unsigned int offset)
112 {
113         return (struct ipt_entry *)((char *)table->blob_entries->entrytable +
114                                                                         offset);
115 }
116
117 static int is_hook_entry(struct connman_iptables *table,
118                                 struct ipt_entry *entry)
119 {
120         unsigned int i;
121
122         for (i = 0; i < NF_INET_NUMHOOKS; i++) {
123                 if ((table->info->valid_hooks & (1 << i))
124                 && get_entry(table, table->info->hook_entry[i]) == entry)
125                         return i;
126         }
127
128         return -1;
129 }
130
131 static unsigned long entry_to_offset(struct connman_iptables *table,
132                                         struct ipt_entry *entry)
133 {
134         return (void *)entry - (void *)table->blob_entries->entrytable;
135 }
136
137 static int target_to_verdict(char *target_name)
138 {
139         if (!strcmp(target_name, LABEL_ACCEPT))
140                 return -NF_ACCEPT - 1;
141
142         if (!strcmp(target_name, LABEL_DROP))
143                 return -NF_DROP - 1;
144
145         if (!strcmp(target_name, LABEL_QUEUE))
146                 return -NF_QUEUE - 1;
147
148         if (!strcmp(target_name, LABEL_RETURN))
149                 return XT_RETURN;
150
151         return 0;
152 }
153
154 static gboolean is_builtin_target(char *target_name)
155 {
156         if (!strcmp(target_name, LABEL_ACCEPT) ||
157                 !strcmp(target_name, LABEL_DROP) ||
158                 !strcmp(target_name, LABEL_QUEUE) ||
159                 !strcmp(target_name, LABEL_RETURN))
160                 return TRUE;
161
162         return FALSE;
163 }
164
165 static gboolean is_jump(struct connman_iptables_entry *e)
166 {
167         struct xt_entry_target *target;
168
169         target = ipt_get_target(e->entry);
170
171         if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
172                 struct xt_standard_target *t;
173
174                 t = (struct xt_standard_target *)target;
175
176                 switch (t->verdict) {
177                 case XT_RETURN:
178                 case -NF_ACCEPT - 1:
179                 case -NF_DROP - 1:
180                 case -NF_QUEUE - 1:
181                 case -NF_STOP - 1:
182                         return false;
183
184                 default:
185                         return true;
186                 }
187         }
188
189         return false;
190 }
191
192 static gboolean is_chain(struct connman_iptables *table,
193                                 struct connman_iptables_entry *e)
194 {
195         struct ipt_entry *entry;
196         struct xt_entry_target *target;
197
198         entry = e->entry;
199         if (e->builtin >= 0)
200                 return TRUE;
201
202         target = ipt_get_target(entry);
203         if (!strcmp(target->u.user.name, IPT_ERROR_TARGET))
204                 return TRUE;
205
206         return FALSE;
207 }
208
209 static GList *find_chain_head(struct connman_iptables *table,
210                                 char *chain_name)
211 {
212         GList *list;
213         struct connman_iptables_entry *head;
214         struct ipt_entry *entry;
215         struct xt_entry_target *target;
216         int builtin;
217
218         for (list = table->entries; list; list = list->next) {
219                 head = list->data;
220                 entry = head->entry;
221
222                 /* Buit-in chain */
223                 builtin = head->builtin;
224                 if (builtin >= 0 && !strcmp(hooknames[builtin], chain_name))
225                         break;
226
227                 /* User defined chain */
228                 target = ipt_get_target(entry);
229                 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET) &&
230                     !strcmp((char *)target->data, chain_name))
231                         break;
232         }
233
234         return list;
235 }
236
237 static GList *find_chain_tail(struct connman_iptables *table,
238                                 char *chain_name)
239 {
240         GList *chain_head, *list;
241         struct connman_iptables_entry *tail;
242
243         chain_head = find_chain_head(table, chain_name);
244         if (chain_head == NULL)
245                 return NULL;
246
247         /* Then we look for the next chain */
248         for (list = chain_head->next; list; list = list->next) {
249                 tail = list->data;
250
251                 if (is_chain(table, tail))
252                         return list;
253         }
254
255         /* Nothing found, we return the table end */
256         return g_list_last(table->entries);
257 }
258
259 static void update_offsets(struct connman_iptables *table)
260 {
261         GList *list, *prev;
262         struct connman_iptables_entry *entry, *prev_entry;
263
264         for (list = table->entries; list; list = list->next) {
265                 entry = list->data;
266
267                 if (list == table->entries) {
268                         entry->offset = 0;
269
270                         continue;
271                 }
272
273                 prev = list->prev;
274                 prev_entry = prev->data;
275
276                 entry->offset = prev_entry->offset +
277                                         prev_entry->entry->next_offset;
278         }
279 }
280
281 static void update_targets_reference(struct connman_iptables *table,
282                                 struct connman_iptables_entry *entry_before,
283                                 struct connman_iptables_entry *modified_entry,
284                                 gboolean is_removing)
285 {
286         struct connman_iptables_entry *tmp;
287         struct xt_standard_target *t;
288         GList *list;
289         int offset;
290
291         offset = modified_entry->entry->next_offset;
292
293         for (list = table->entries; list; list = list->next) {
294                 tmp = list->data;
295
296                 if (!is_jump(tmp))
297                         continue;
298
299                 t = (struct xt_standard_target *)ipt_get_target(tmp->entry);
300
301                 if (is_removing == TRUE) {
302                         if (t->verdict >= entry_before->offset)
303                                 t->verdict -= offset;
304                 } else {
305                         if (t->verdict > entry_before->offset)
306                                 t->verdict += offset;
307                 }
308         }
309 }
310
311 static int connman_add_entry(struct connman_iptables *table,
312                                 struct ipt_entry *entry, GList *before,
313                                         int builtin)
314 {
315         struct connman_iptables_entry *e, *entry_before;
316
317         if (table == NULL)
318                 return -1;
319
320         e = g_try_malloc0(sizeof(struct connman_iptables_entry));
321         if (e == NULL)
322                 return -1;
323
324         e->entry = entry;
325         e->builtin = builtin;
326
327         table->entries = g_list_insert_before(table->entries, before, e);
328         table->num_entries++;
329         table->size += entry->next_offset;
330
331         if (before == NULL) {
332                 e->offset = table->size - entry->next_offset;
333
334                 return 0;
335         }
336
337         entry_before = before->data;
338
339         /*
340          * We've just appended/insterted a new entry. All references
341          * should be bumped accordingly.
342          */
343         update_targets_reference(table, entry_before, e, FALSE);
344
345         update_offsets(table);
346
347         return 0;
348 }
349
350 static int remove_table_entry(struct connman_iptables *table,
351                                         struct connman_iptables_entry *entry)
352 {
353         int removed = 0;
354
355         table->num_entries--;
356         table->size -= entry->entry->next_offset;
357         removed = entry->entry->next_offset;
358
359         g_free(entry->entry);
360
361         table->entries = g_list_remove(table->entries, entry);
362
363         return removed;
364 }
365
366 static int connman_iptables_flush_chain(struct connman_iptables *table,
367                                                 char *name)
368 {
369         GList *chain_head, *chain_tail, *list, *next;
370         struct connman_iptables_entry *entry;
371         int builtin, removed = 0;
372
373         chain_head = find_chain_head(table, name);
374         if (chain_head == NULL)
375                 return -EINVAL;
376
377         chain_tail = find_chain_tail(table, name);
378         if (chain_tail == NULL)
379                 return -EINVAL;
380
381         entry = chain_head->data;
382         builtin = entry->builtin;
383
384         if (builtin >= 0)
385                 list = chain_head;
386         else
387                 list = chain_head->next;
388
389         if (list == chain_tail->prev)
390                 return 0;
391
392         while (list != chain_tail->prev) {
393                 entry = list->data;
394                 next = g_list_next(list);
395
396                 removed += remove_table_entry(table, entry);
397
398                 list = next;
399         }
400
401         if (builtin >= 0) {
402                 struct connman_iptables_entry *e;
403
404                 entry = list->data;
405
406                 entry->builtin = builtin;
407
408                 table->underflow[builtin] -= removed;
409
410                 for (list = chain_tail; list; list = list->next) {
411                         e = list->data;
412
413                         builtin = e->builtin;
414                         if (builtin < 0)
415                                 continue;
416
417                         table->hook_entry[builtin] -= removed;
418                         table->underflow[builtin] -= removed;
419                 }
420         }
421
422         update_offsets(table);
423
424         return 0;
425 }
426
427 static int connman_iptables_delete_chain(struct connman_iptables *table,
428                                                 char *name)
429 {
430         GList *chain_head, *chain_tail;
431         struct connman_iptables_entry *entry;
432
433         chain_head = find_chain_head(table, name);
434         if (chain_head == NULL)
435                 return -EINVAL;
436
437         entry = chain_head->data;
438
439         /* We cannot remove builtin chain */
440         if (entry->builtin >= 0)
441                 return -EINVAL;
442
443         chain_tail = find_chain_tail(table, name);
444         if (chain_tail == NULL)
445                 return -EINVAL;
446
447         /* Chain must be flushed */
448         if (chain_head->next != chain_tail->prev)
449                 return -EINVAL;
450
451         remove_table_entry(table, entry);
452
453         entry = chain_tail->prev->data;
454         remove_table_entry(table, entry);
455
456         update_offsets(table);
457
458         return 0;
459 }
460
461 static int connman_iptables_add_chain(struct connman_iptables *table,
462                                         char *name)
463 {
464         GList *last;
465         struct ipt_entry *entry_head;
466         struct ipt_entry *entry_return;
467         struct error_target *error;
468         struct ipt_standard_target *standard;
469         u_int16_t entry_head_size, entry_return_size;
470
471         last = g_list_last(table->entries);
472
473         /*
474          * An empty chain is composed of:
475          * - A head entry, with no match and an error target.
476          *   The error target data is the chain name.
477          * - A tail entry, with no match and a standard target.
478          *   The standard target verdict is XT_RETURN (return to the
479          *   caller).
480          */
481
482         /* head entry */
483         entry_head_size = sizeof(struct ipt_entry) +
484                                 sizeof(struct error_target);
485         entry_head = g_try_malloc0(entry_head_size);
486         if (entry_head == NULL)
487                 goto err_head;
488
489         memset(entry_head, 0, entry_head_size);
490
491         entry_head->target_offset = sizeof(struct ipt_entry);
492         entry_head->next_offset = entry_head_size;
493
494         error = (struct error_target *) entry_head->elems;
495         strcpy(error->t.u.user.name, IPT_ERROR_TARGET);
496         error->t.u.user.target_size = ALIGN(sizeof(struct error_target));
497         strcpy(error->error, name);
498
499         if (connman_add_entry(table, entry_head, last, -1) < 0)
500                 goto err_head;
501
502         /* tail entry */
503         entry_return_size = sizeof(struct ipt_entry) +
504                                 sizeof(struct ipt_standard_target);
505         entry_return = g_try_malloc0(entry_return_size);
506         if (entry_return == NULL)
507                 goto err;
508
509         memset(entry_return, 0, entry_return_size);
510
511         entry_return->target_offset = sizeof(struct ipt_entry);
512         entry_return->next_offset = entry_return_size;
513
514         standard = (struct ipt_standard_target *) entry_return->elems;
515         standard->target.u.user.target_size =
516                                 ALIGN(sizeof(struct ipt_standard_target));
517         standard->verdict = XT_RETURN;
518
519         if (connman_add_entry(table, entry_return, last, -1) < 0)
520                 goto err;
521
522         return 0;
523
524 err:
525         g_free(entry_return);
526 err_head:
527         g_free(entry_head);
528
529         return -ENOMEM;
530 }
531
532 static struct ipt_entry *new_rule(struct ipt_ip *ip,
533                         char *target_name, struct xtables_target *xt_t,
534                         char *match_name, struct xtables_match *xt_m)
535 {
536         struct ipt_entry *new_entry;
537         size_t match_size, target_size;
538
539         if (xt_m)
540                 match_size = xt_m->m->u.match_size;
541         else
542                 match_size = 0;
543
544         if (xt_t)
545                 target_size = ALIGN(xt_t->t->u.target_size);
546         else
547                 target_size = ALIGN(sizeof(struct xt_standard_target));
548
549         new_entry = g_try_malloc0(sizeof(struct ipt_entry) + target_size +
550                                                                 match_size);
551         if (new_entry == NULL)
552                 return NULL;
553
554         memcpy(&new_entry->ip, ip, sizeof(struct ipt_ip));
555
556         new_entry->target_offset = sizeof(struct ipt_entry) + match_size;
557         new_entry->next_offset = sizeof(struct ipt_entry) + target_size +
558                                                                 match_size;
559         if (xt_m) {
560                 struct xt_entry_match *entry_match;
561
562                 entry_match = (struct xt_entry_match *)new_entry->elems;
563                 memcpy(entry_match, xt_m->m, match_size);
564         }
565
566         if (xt_t) {
567                 struct xt_entry_target *entry_target;
568
569                 entry_target = ipt_get_target(new_entry);
570                 memcpy(entry_target, xt_t->t, target_size);
571         }
572
573         return new_entry;
574 }
575
576 static void update_hooks(struct connman_iptables *table, GList *chain_head,
577                                 struct ipt_entry *entry)
578 {
579         GList *list;
580         struct connman_iptables_entry *head, *e;
581         int builtin;
582
583         if (chain_head == NULL)
584                 return;
585
586         head = chain_head->data;
587
588         builtin = head->builtin;
589         if (builtin < 0)
590                 return;
591
592         table->underflow[builtin] += entry->next_offset;
593
594         for (list = chain_head->next; list; list = list->next) {
595                 e = list->data;
596
597                 builtin = e->builtin;
598                 if (builtin < 0)
599                         continue;
600
601                 table->hook_entry[builtin] += entry->next_offset;
602                 table->underflow[builtin] += entry->next_offset;
603         }
604 }
605
606 static struct ipt_entry *prepare_rule_inclusion(struct connman_iptables *table,
607                                 struct ipt_ip *ip, char *chain_name,
608                                 char *target_name, struct xtables_target *xt_t,
609                                 char *match_name, struct xtables_match *xt_m,
610                                 int *builtin)
611 {
612         GList *chain_tail, *chain_head;
613         struct ipt_entry *new_entry;
614         struct connman_iptables_entry *head;
615
616         chain_head = find_chain_head(table, chain_name);
617         if (chain_head == NULL)
618                 return NULL;
619
620         chain_tail = find_chain_tail(table, chain_name);
621         if (chain_tail == NULL)
622                 return NULL;
623
624         new_entry = new_rule(ip, target_name, xt_t, match_name, xt_m);
625         if (new_entry == NULL)
626                 return NULL;
627
628         update_hooks(table, chain_head, new_entry);
629
630         /*
631          * If the chain is builtin, and does not have any rule,
632          * then the one that we're inserting is becoming the head
633          * and thus needs the builtin flag.
634          */
635         head = chain_head->data;
636         if (head->builtin < 0)
637                 *builtin = -1;
638         else if (chain_head == chain_tail->prev) {
639                 *builtin = head->builtin;
640                 head->builtin = -1;
641         }
642
643         return new_entry;
644 }
645
646 static int connman_iptables_append_rule(struct connman_iptables *table,
647                                 struct ipt_ip *ip, char *chain_name,
648                                 char *target_name, struct xtables_target *xt_t,
649                                 char *match_name, struct xtables_match *xt_m)
650 {
651         GList *chain_tail;
652         struct ipt_entry *new_entry;
653         int builtin = -1, ret;
654
655         chain_tail = find_chain_tail(table, chain_name);
656         if (chain_tail == NULL)
657                 return -EINVAL;
658
659         new_entry = prepare_rule_inclusion(table, ip, chain_name,
660                         target_name, xt_t, match_name, xt_m, &builtin);
661         if (new_entry == NULL)
662                 return -EINVAL;
663
664         ret = connman_add_entry(table, new_entry, chain_tail->prev, builtin);
665         if (ret < 0)
666                 g_free(new_entry);
667
668         return ret;
669 }
670
671 static int connman_iptables_insert_rule(struct connman_iptables *table,
672                                 struct ipt_ip *ip, char *chain_name,
673                                 char *target_name, struct xtables_target *xt_t,
674                                 char *match_name, struct xtables_match *xt_m)
675 {
676         GList *chain_head;
677         struct ipt_entry *new_entry;
678         int builtin = -1, ret;
679
680         chain_head = find_chain_head(table, chain_name);
681         if (chain_head == NULL)
682                 return -EINVAL;
683
684         new_entry = prepare_rule_inclusion(table, ip, chain_name,
685                         target_name, xt_t, match_name, xt_m, &builtin);
686         if (new_entry == NULL)
687                 return -EINVAL;
688
689         ret = connman_add_entry(table, new_entry, chain_head->next, builtin);
690         if (ret < 0)
691                 g_free(new_entry);
692
693         return ret;
694 }
695
696 static gboolean is_same_ipt_entry(struct ipt_entry *i_e1,
697                                         struct ipt_entry *i_e2)
698 {
699         if (memcmp(&i_e1->ip, &i_e2->ip, sizeof(struct ipt_ip)) != 0)
700                 return FALSE;
701
702         if (i_e1->target_offset != i_e2->target_offset)
703                 return FALSE;
704
705         if (i_e1->next_offset != i_e2->next_offset)
706                 return FALSE;
707
708         return TRUE;
709 }
710
711 static gboolean is_same_target(struct xt_entry_target *xt_e_t1,
712                                         struct xt_entry_target *xt_e_t2)
713 {
714         if (xt_e_t1 == NULL || xt_e_t2 == NULL)
715                 return FALSE;
716
717         if (strcmp(xt_e_t1->u.user.name, IPT_STANDARD_TARGET) == 0) {
718                 struct xt_standard_target *xt_s_t1;
719                 struct xt_standard_target *xt_s_t2;
720
721                 xt_s_t1 = (struct xt_standard_target *) xt_e_t1;
722                 xt_s_t2 = (struct xt_standard_target *) xt_e_t2;
723
724                 if (xt_s_t1->verdict != xt_s_t2->verdict)
725                         return FALSE;
726         } else {
727                 if (xt_e_t1->u.target_size != xt_e_t2->u.target_size)
728                         return FALSE;
729
730                 if (strcmp(xt_e_t1->u.user.name, xt_e_t2->u.user.name) != 0)
731                         return FALSE;
732         }
733
734         return TRUE;
735 }
736
737 static gboolean is_same_match(struct xt_entry_match *xt_e_m1,
738                                 struct xt_entry_match *xt_e_m2)
739 {
740         if (xt_e_m1 == NULL || xt_e_m2 == NULL)
741                 return FALSE;
742
743         if (xt_e_m1->u.match_size != xt_e_m2->u.match_size)
744                 return FALSE;
745
746         if (xt_e_m1->u.user.revision != xt_e_m2->u.user.revision)
747                 return FALSE;
748
749         if (strcmp(xt_e_m1->u.user.name, xt_e_m2->u.user.name) != 0)
750                 return FALSE;
751
752         return TRUE;
753 }
754
755 static int connman_iptables_delete_rule(struct connman_iptables *table,
756                                 struct ipt_ip *ip, char *chain_name,
757                                 char *target_name, struct xtables_target *xt_t,
758                                 char *match_name, struct xtables_match *xt_m)
759 {
760         GList *chain_tail, *chain_head, *list;
761         struct xt_entry_target *xt_e_t = NULL;
762         struct xt_entry_match *xt_e_m = NULL;
763         struct connman_iptables_entry *entry;
764         struct ipt_entry *entry_test;
765         int builtin, removed;
766
767         removed = 0;
768
769         chain_head = find_chain_head(table, chain_name);
770         if (chain_head == NULL)
771                 return -EINVAL;
772
773         chain_tail = find_chain_tail(table, chain_name);
774         if (chain_tail == NULL)
775                 return -EINVAL;
776
777         if (!xt_t && !xt_m)
778                 return -EINVAL;
779
780         entry_test = new_rule(ip, target_name, xt_t, match_name, xt_m);
781         if (entry_test == NULL)
782                 return -EINVAL;
783
784         if (xt_t != NULL)
785                 xt_e_t = ipt_get_target(entry_test);
786         if (xt_m != NULL)
787                 xt_e_m = (struct xt_entry_match *)entry_test->elems;
788
789         entry = chain_head->data;
790         builtin = entry->builtin;
791
792         if (builtin >= 0)
793                 list = chain_head;
794         else
795                 list = chain_head->next;
796
797         for (entry = NULL; list != chain_tail->prev; list = list->next) {
798                 struct connman_iptables_entry *tmp;
799                 struct ipt_entry *tmp_e;
800
801                 tmp = list->data;
802                 tmp_e = tmp->entry;
803
804                 if (is_same_ipt_entry(entry_test, tmp_e) == FALSE)
805                         continue;
806
807                 if (xt_t != NULL) {
808                         struct xt_entry_target *tmp_xt_e_t;
809
810                         tmp_xt_e_t = ipt_get_target(tmp_e);
811
812                         if (!is_same_target(tmp_xt_e_t, xt_e_t))
813                                 continue;
814                 }
815
816                 if (xt_m != NULL) {
817                         struct xt_entry_match *tmp_xt_e_m;
818
819                         tmp_xt_e_m = (struct xt_entry_match *)tmp_e->elems;
820
821                         if (!is_same_match(tmp_xt_e_m, xt_e_m))
822                                 continue;
823                 }
824
825                 entry = tmp;
826                 break;
827         }
828
829         if (entry == NULL) {
830                 g_free(entry_test);
831                 return -EINVAL;
832         }
833
834         /* We have deleted a rule,
835          * all references should be bumped accordingly */
836         if (list->next != NULL)
837                 update_targets_reference(table, list->next->data,
838                                                 list->data, TRUE);
839
840         removed += remove_table_entry(table, entry);
841
842         if (builtin >= 0) {
843                 list = list->next;
844                 if (list) {
845                         entry = list->data;
846                         entry->builtin = builtin;
847                 }
848
849                 table->underflow[builtin] -= removed;
850                 for (list = chain_tail; list; list = list->next) {
851                         entry = list->data;
852
853                         builtin = entry->builtin;
854                         if (builtin < 0)
855                                 continue;
856
857                         table->hook_entry[builtin] -= removed;
858                         table->underflow[builtin] -= removed;
859                 }
860         }
861
862         update_offsets(table);
863
864         return 0;
865 }
866
867 static struct ipt_replace *connman_iptables_blob(struct connman_iptables *table)
868 {
869         struct ipt_replace *r;
870         GList *list;
871         struct connman_iptables_entry *e;
872         unsigned char *entry_index;
873
874         r = g_try_malloc0(sizeof(struct ipt_replace) + table->size);
875         if (r == NULL)
876                 return NULL;
877
878         memset(r, 0, sizeof(*r) + table->size);
879
880         r->counters = g_try_malloc0(sizeof(struct xt_counters)
881                                 * table->old_entries);
882         if (r->counters == NULL) {
883                 g_free(r);
884                 return NULL;
885         }
886
887         strcpy(r->name, table->info->name);
888         r->num_entries = table->num_entries;
889         r->size = table->size;
890
891         r->num_counters = table->old_entries;
892         r->valid_hooks  = table->info->valid_hooks;
893
894         memcpy(r->hook_entry, table->hook_entry, sizeof(table->hook_entry));
895         memcpy(r->underflow, table->underflow, sizeof(table->underflow));
896
897         entry_index = (unsigned char *)r->entries;
898         for (list = table->entries; list; list = list->next) {
899                 e = list->data;
900
901                 memcpy(entry_index, e->entry, e->entry->next_offset);
902                 entry_index += e->entry->next_offset;
903         }
904
905         return r;
906 }
907
908 static void dump_target(struct connman_iptables *table,
909                                 struct ipt_entry *entry)
910
911 {
912         struct xtables_target *xt_t;
913         struct xt_entry_target *target;
914
915         target = ipt_get_target(entry);
916
917         if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
918                 struct xt_standard_target *t;
919
920                 t = (struct xt_standard_target *)target;
921
922                 switch (t->verdict) {
923                 case XT_RETURN:
924                         printf("\ttarget RETURN\n");
925                         break;
926
927                 case -NF_ACCEPT - 1:
928                         printf("\ttarget ACCEPT\n");
929                         break;
930
931                 case -NF_DROP - 1:
932                         printf("\ttarget DROP\n");
933                         break;
934
935                 case -NF_QUEUE - 1:
936                         printf("\ttarget QUEUE\n");
937                         break;
938
939                 case -NF_STOP - 1:
940                         printf("\ttarget STOP\n");
941                         break;
942
943                 default:
944                         printf("\tJUMP @%p (0x%x)\n",
945                                 (char*)table->blob_entries->entrytable +
946                                 t->verdict, t->verdict);
947                         break;
948                 }
949
950                 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
951                                                 XTF_LOAD_MUST_SUCCEED);
952
953                 if(xt_t->print != NULL)
954                         xt_t->print(NULL, target, 1);
955         } else {
956                 xt_t = xtables_find_target(target->u.user.name, XTF_TRY_LOAD);
957                 if (xt_t == NULL) {
958                         printf("\ttarget %s\n", target->u.user.name);
959                         return;
960                 }
961
962                 if(xt_t->print != NULL) {
963                         printf("\ttarget ");
964                         xt_t->print(NULL, target, 1);
965                         printf("\n");
966                 }
967         }
968 }
969
970 static void dump_match(struct connman_iptables *table, struct ipt_entry *entry)
971 {
972         struct xtables_match *xt_m;
973         struct xt_entry_match *match;
974
975         if (entry->elems == (unsigned char *)entry + entry->target_offset)
976                 return;
977
978         match = (struct xt_entry_match *) entry->elems;
979
980         if (!strlen(match->u.user.name))
981                 return;
982
983         xt_m = xtables_find_match(match->u.user.name, XTF_TRY_LOAD, NULL);
984         if (xt_m == NULL)
985                 goto out;
986
987         if(xt_m->print != NULL) {
988                 printf("\tmatch ");
989                 xt_m->print(NULL, match, 1);
990                 printf("\n");
991
992                 return;
993         }
994
995 out:
996         printf("\tmatch %s\n", match->u.user.name);
997
998 }
999
1000 static int connman_iptables_dump_entry(struct ipt_entry *entry,
1001                                         struct connman_iptables *table)
1002 {
1003         struct xt_entry_target *target;
1004         unsigned int offset;
1005         int builtin;
1006
1007         offset = (char *)entry - (char *)table->blob_entries->entrytable;
1008         target = ipt_get_target(entry);
1009         builtin = is_hook_entry(table, entry);
1010
1011         if (entry_to_offset(table, entry) + entry->next_offset ==
1012                                         table->blob_entries->size) {
1013                 printf("End of CHAIN 0x%x\n", offset);
1014                 return 0;
1015         }
1016
1017         if (!strcmp(target->u.user.name, IPT_ERROR_TARGET)) {
1018                 printf("USER CHAIN (%s) %p  match %p  target %p  size %d\n",
1019                         target->data, entry, entry->elems,
1020                         (char *)entry + entry->target_offset,
1021                                 entry->next_offset);
1022
1023                 return 0;
1024         } else if (builtin >= 0) {
1025                 printf("CHAIN (%s) %p  match %p  target %p  size %d\n",
1026                         hooknames[builtin], entry, entry->elems,
1027                         (char *)entry + entry->target_offset,
1028                                 entry->next_offset);
1029         } else {
1030                 printf("RULE %p  match %p  target %p  size %d\n", entry,
1031                         entry->elems,
1032                         (char *)entry + entry->target_offset,
1033                                 entry->next_offset);
1034         }
1035
1036         dump_match(table, entry);
1037         dump_target(table, entry);
1038
1039         return 0;
1040 }
1041
1042 static void connman_iptables_dump_hook(struct connman_iptables *table)
1043 {
1044         int i;
1045         printf("hooks: \n");
1046         for (i = 0; i < NF_INET_NUMHOOKS; i++) {
1047                 if ((table->info->valid_hooks & (1 << i)))
1048                         printf("%s entry %p underflow %p (%#x)\n",
1049                                 hooknames[i],
1050                                 table->blob_entries->entrytable +
1051                                                 table->info->hook_entry[i],
1052                                 table->blob_entries->entrytable +
1053                                                 table->info->underflow[i],
1054                                         table->info->underflow[i]);
1055         }
1056 }
1057
1058 static void connman_iptables_dump(struct connman_iptables *table)
1059 {
1060         printf("%s valid_hooks=0x%08x, num_entries=%u, size=%u\n",
1061                 table->info->name,
1062                 table->info->valid_hooks, table->info->num_entries,
1063                 table->info->size);
1064
1065         connman_iptables_dump_hook(table);
1066
1067         ENTRY_ITERATE(table->blob_entries->entrytable,
1068                         table->blob_entries->size,
1069                         connman_iptables_dump_entry, table);
1070
1071 }
1072
1073 static int connman_iptables_get_entries(struct connman_iptables *table)
1074 {
1075         socklen_t entry_size;
1076
1077         entry_size = sizeof(struct ipt_get_entries) + table->info->size;
1078
1079         return getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_ENTRIES,
1080                                 table->blob_entries, &entry_size);
1081 }
1082
1083 static int connman_iptables_replace(struct connman_iptables *table,
1084                                         struct ipt_replace *r)
1085 {
1086         return setsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_SET_REPLACE, r,
1087                          sizeof(*r) + r->size);
1088 }
1089
1090 static void connman_iptables_cleanup(struct connman_iptables *table)
1091 {
1092         GList *list;
1093         struct connman_iptables_entry *entry;
1094
1095         close(table->ipt_sock);
1096
1097         for (list = table->entries; list; list = list->next) {
1098                 entry = list->data;
1099
1100                 g_free(entry->entry);
1101         }
1102
1103         g_free(table->info);
1104         g_free(table->blob_entries);
1105         g_free(table);
1106
1107         xtables_free_opts(1);
1108 }
1109
1110 static int connman_iptables_commit(struct connman_iptables *table)
1111 {
1112         struct ipt_replace *repl;
1113
1114         repl = connman_iptables_blob(table);
1115
1116         return connman_iptables_replace(table, repl);
1117 }
1118
1119 static int add_entry(struct ipt_entry *entry, struct connman_iptables *table)
1120 {
1121         struct ipt_entry *new_entry;
1122         int builtin;
1123
1124         new_entry = g_try_malloc0(entry->next_offset);
1125         if (new_entry == NULL)
1126                 return -ENOMEM;
1127
1128         memcpy(new_entry, entry, entry->next_offset);
1129
1130         builtin = is_hook_entry(table, entry);
1131
1132         return connman_add_entry(table, new_entry, NULL, builtin);
1133 }
1134
1135 static struct connman_iptables *connman_iptables_init(const char *table_name)
1136 {
1137         struct connman_iptables *table = NULL;
1138         char *module = NULL;
1139         socklen_t s;
1140
1141         if (xtables_insmod("ip_tables", NULL, TRUE) != 0)
1142                 goto err;
1143
1144         module = g_strconcat("iptable_", table_name, NULL);
1145         if (module == NULL)
1146                 goto err;
1147
1148         if (xtables_insmod(module, NULL, TRUE) != 0)
1149                 goto err;
1150
1151         g_free(module);
1152
1153         table =  g_try_new0(struct connman_iptables, 1);
1154         if (table == NULL)
1155                 return NULL;
1156
1157         table->info =  g_try_new0(struct ipt_getinfo, 1);
1158         if (table->info == NULL)
1159                 goto err;
1160
1161         table->ipt_sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
1162         if (table->ipt_sock < 0)
1163                 goto err;
1164
1165         s = sizeof(*table->info);
1166         strcpy(table->info->name, table_name);
1167         if (getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_INFO,
1168                                                 table->info, &s) < 0)
1169                 goto err;
1170
1171         table->blob_entries = g_try_malloc0(sizeof(struct ipt_get_entries) +
1172                                                 table->info->size);
1173         if (table->blob_entries == NULL)
1174                 goto err;
1175
1176         strcpy(table->blob_entries->name, table_name);
1177         table->blob_entries->size = table->info->size;
1178
1179         if (connman_iptables_get_entries(table) < 0)
1180                 goto err;
1181
1182         table->num_entries = 0;
1183         table->old_entries = table->info->num_entries;
1184         table->size = 0;
1185
1186         memcpy(table->underflow, table->info->underflow,
1187                                 sizeof(table->info->underflow));
1188         memcpy(table->hook_entry, table->info->hook_entry,
1189                                 sizeof(table->info->hook_entry));
1190
1191         ENTRY_ITERATE(table->blob_entries->entrytable,
1192                         table->blob_entries->size,
1193                                 add_entry, table);
1194
1195         return table;
1196
1197 err:
1198         g_free(module);
1199
1200         connman_iptables_cleanup(table);
1201
1202         return NULL;
1203 }
1204
1205 static struct xtables_target *prepare_target(struct connman_iptables *table,
1206                                                         char *target_name)
1207 {
1208         struct xtables_target *xt_t = NULL;
1209         gboolean is_builtin, is_user_defined;
1210         GList *chain_head = NULL;
1211         size_t target_size;
1212
1213         is_builtin = FALSE;
1214         is_user_defined = FALSE;
1215
1216         if (is_builtin_target(target_name))
1217                 is_builtin = TRUE;
1218         else {
1219                 chain_head = find_chain_head(table, target_name);
1220                 if (chain_head != NULL && chain_head->next != NULL)
1221                         is_user_defined = TRUE;
1222         }
1223
1224         if (is_builtin || is_user_defined)
1225                 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
1226                                                 XTF_LOAD_MUST_SUCCEED);
1227         else
1228                 xt_t = xtables_find_target(target_name, XTF_TRY_LOAD);
1229
1230         if (xt_t == NULL)
1231                 return NULL;
1232
1233         target_size = ALIGN(sizeof(struct ipt_entry_target)) + xt_t->size;
1234
1235         xt_t->t = g_try_malloc0(target_size);
1236         if (xt_t->t == NULL)
1237                 return NULL;
1238
1239         xt_t->t->u.target_size = target_size;
1240
1241         if (is_builtin || is_user_defined) {
1242                 struct xt_standard_target *target;
1243
1244                 target = (struct xt_standard_target *)(xt_t->t);
1245                 strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
1246
1247                 if (is_builtin == TRUE)
1248                         target->verdict = target_to_verdict(target_name);
1249                 else if (is_user_defined == TRUE) {
1250                         struct connman_iptables_entry *target_rule;
1251
1252                         if (chain_head == NULL) {
1253                                 g_free(xt_t->t);
1254                                 return NULL;
1255                         }
1256
1257                         target_rule = chain_head->next->data;
1258                         target->verdict = target_rule->offset;
1259                 }
1260         } else {
1261                 strcpy(xt_t->t->u.user.name, target_name);
1262                 xt_t->t->u.user.revision = xt_t->revision;
1263                 if (xt_t->init != NULL)
1264                         xt_t->init(xt_t->t);
1265         }
1266
1267         return xt_t;
1268 }
1269
1270 static struct option connman_iptables_opts[] = {
1271         {.name = "append",        .has_arg = 1, .val = 'A'},
1272         {.name = "delete",        .has_arg = 1, .val = 'D'},
1273         {.name = "flush-chain",   .has_arg = 1, .val = 'F'},
1274         {.name = "insert",        .has_arg = 1, .val = 'I'},
1275         {.name = "list",          .has_arg = 2, .val = 'L'},
1276         {.name = "new-chain",     .has_arg = 1, .val = 'N'},
1277         {.name = "delete-chain",  .has_arg = 1, .val = 'X'},
1278         {.name = "destination",   .has_arg = 1, .val = 'd'},
1279         {.name = "in-interface",  .has_arg = 1, .val = 'i'},
1280         {.name = "jump",          .has_arg = 1, .val = 'j'},
1281         {.name = "match",         .has_arg = 1, .val = 'm'},
1282         {.name = "out-interface", .has_arg = 1, .val = 'o'},
1283         {.name = "source",        .has_arg = 1, .val = 's'},
1284         {.name = "table",         .has_arg = 1, .val = 't'},
1285         {NULL},
1286 };
1287
1288 struct xtables_globals connman_iptables_globals = {
1289         .option_offset = 0,
1290         .opts = connman_iptables_opts,
1291         .orig_opts = connman_iptables_opts,
1292 };
1293
1294 int main(int argc, char *argv[])
1295 {
1296         struct connman_iptables *table;
1297         struct xtables_match *xt_m;
1298         struct xtables_target *xt_t;
1299         struct ipt_ip ip;
1300         char *table_name, *chain, *new_chain, *match_name, *target_name;
1301         char *delete_chain, *flush_chain;
1302         int c, in_len, out_len;
1303         size_t size;
1304         gboolean dump, invert, delete, insert, delete_rule;
1305         struct in_addr src, dst;
1306
1307         xtables_init_all(&connman_iptables_globals, NFPROTO_IPV4);
1308
1309         dump = FALSE;
1310         invert = FALSE;
1311         delete = FALSE;
1312         insert = FALSE;
1313         delete_rule = FALSE;
1314         table_name = chain = new_chain = match_name = target_name = NULL;
1315         delete_chain = flush_chain = NULL;
1316         memset(&ip, 0, sizeof(struct ipt_ip));
1317         table = NULL;
1318         xt_m = NULL;
1319         xt_t = NULL;
1320
1321         while ((c = getopt_long(argc, argv, "-A:D:F:I:L::N:X:d:i:j:m:o:s:t:",
1322                                 connman_iptables_globals.opts, NULL)) != -1) {
1323                 switch (c) {
1324                 case 'A':
1325                         /* It is either -A, -D or -I at once */
1326                         if (chain)
1327                                 goto out;
1328
1329                         chain = optarg;
1330                         break;
1331
1332                 case 'D':
1333                         /* It is either -A, -D or -I at once */
1334                         if (chain)
1335                                 goto out;
1336
1337                         chain = optarg;
1338                         delete_rule = TRUE;
1339                         break;
1340
1341                 case 'F':
1342                         flush_chain = optarg;
1343                         break;
1344
1345                 case 'I':
1346                         /* It is either -A, -D or -I at once */
1347                         if (chain)
1348                                 goto out;
1349
1350                         chain = optarg;
1351                         insert = TRUE;
1352                         break;
1353
1354                 case 'L':
1355                         dump = true;
1356                         break;
1357
1358                 case 'N':
1359                         new_chain = optarg;
1360                         break;
1361
1362                 case 'X':
1363                         delete = true;
1364                         delete_chain = optarg;
1365                         break;
1366
1367                 case 'd':
1368                         if (!inet_pton(AF_INET, optarg, &dst))
1369                                 break;
1370
1371                         ip.dst = dst;
1372                         inet_pton(AF_INET, "255.255.255.255", &ip.dmsk);
1373
1374                         if (invert)
1375                                 ip.invflags |= IPT_INV_DSTIP;
1376
1377                         break;
1378
1379                 case 'i':
1380                         in_len = strlen(optarg);
1381
1382                         if (in_len + 1 > IFNAMSIZ)
1383                                 break;
1384
1385                         strcpy(ip.iniface, optarg);
1386                         memset(ip.iniface_mask, 0xff, in_len + 1);
1387
1388                         if (invert)
1389                                 ip.invflags |= IPT_INV_VIA_IN;
1390
1391                         break;
1392
1393                 case 'j':
1394                         target_name = optarg;
1395                         break;
1396
1397                 case 'm':
1398                         match_name = optarg;
1399
1400                         xt_m = xtables_find_match(optarg, XTF_LOAD_MUST_SUCCEED, NULL);
1401                         size = ALIGN(sizeof(struct ipt_entry_match)) + xt_m->size;
1402                         xt_m->m = g_try_malloc0(size);
1403                         if (xt_m == NULL)
1404                                 goto out;
1405                         xt_m->m->u.match_size = size;
1406                         strcpy(xt_m->m->u.user.name, xt_m->name);
1407                         xt_m->m->u.user.revision = xt_m->revision;
1408                         if (xt_m->init != NULL)
1409                                 xt_m->init(xt_m->m);
1410                         if (xt_m != xt_m->next) {
1411                                 connman_iptables_globals.opts =
1412                                         xtables_merge_options(
1413 #if XTABLES_VERSION_CODE > 5
1414                                                 connman_iptables_globals.orig_opts,
1415 #endif
1416                                                 connman_iptables_globals.opts,
1417                                                 xt_m->extra_opts,
1418                                                 &xt_m->option_offset);
1419                                 if (connman_iptables_globals.opts == NULL)
1420                                         goto out;
1421                         }
1422
1423                         break;
1424
1425                 case 'o':
1426                         out_len = strlen(optarg);
1427
1428                         if (out_len + 1 > IFNAMSIZ)
1429                                 break;
1430
1431                         strcpy(ip.outiface, optarg);
1432                         memset(ip.outiface_mask, 0xff, out_len + 1);
1433
1434                         if (invert)
1435                                 ip.invflags |= IPT_INV_VIA_OUT;
1436
1437                         break;
1438
1439                 case 's':
1440                         if (!inet_pton(AF_INET, optarg, &src))
1441                                 break;
1442
1443                         ip.src = src;
1444                         inet_pton(AF_INET, "255.255.255.255", &ip.smsk);
1445
1446                         if (invert)
1447                                 ip.invflags |= IPT_INV_SRCIP;
1448
1449                         break;
1450
1451                 case 't':
1452                         table_name = optarg;
1453                         break;
1454
1455                 case 1:
1456                         if (optarg[0] == '!' && optarg[1] == '\0') {
1457                                 if (invert)
1458                                         printf("Consecutive ! not allowed\n");
1459
1460                                 invert = TRUE;
1461                                 optarg[0] = '\0';
1462                                 continue;
1463                         }
1464
1465                         printf("Invalid option\n");
1466
1467                         return -1;
1468
1469                 default:
1470                         if (xt_t == NULL || xt_t->parse == NULL ||
1471                             !xt_t->parse(c - xt_t->option_offset, argv, invert,
1472                                         &xt_t->tflags, NULL, &xt_t->t)) {
1473                                 if (xt_m == NULL || xt_m->parse == NULL)
1474                                         break;
1475
1476                                 xt_m->parse(c - xt_m->option_offset, argv,
1477                                         invert, &xt_m->mflags, NULL, &xt_m->m);
1478                         }
1479
1480                         break;
1481                 }
1482         }
1483
1484         if (table_name == NULL)
1485                 table_name = "filter";
1486
1487         table = connman_iptables_init(table_name);
1488         if (table == NULL)
1489                 return -1;
1490
1491         if (delete) {
1492                 if (delete_chain == NULL)
1493                         goto out;
1494
1495                 printf("Delete chain %s\n", delete_chain);
1496
1497                 connman_iptables_delete_chain(table, delete_chain);
1498
1499                 goto commit;
1500         }
1501
1502         if (flush_chain) {
1503                 printf("Flush chain %s\n", flush_chain);
1504
1505                 connman_iptables_flush_chain(table, flush_chain);
1506
1507                 goto commit;
1508         }
1509
1510         if (dump) {
1511                 connman_iptables_dump(table);
1512
1513                 return 0;
1514         }
1515
1516         if (chain && new_chain)
1517                 return -1;
1518
1519         if (new_chain) {
1520                 printf("New chain %s\n", new_chain);
1521
1522                 connman_iptables_add_chain(table, new_chain);
1523
1524                 goto commit;
1525         }
1526
1527         if (chain) {
1528                 xt_t = prepare_target(table, target_name);
1529                 if (xt_t == NULL)
1530                         goto out;
1531
1532                 connman_iptables_globals.opts =
1533                         xtables_merge_options(
1534 #if XTABLES_VERSION_CODE > 5
1535                                         connman_iptables_globals.orig_opts,
1536 #endif
1537                                         connman_iptables_globals.opts,
1538                                         xt_t->extra_opts,
1539                                         &xt_t->option_offset);
1540                 if (connman_iptables_globals.opts == NULL)
1541                         goto out;
1542
1543                 if (delete_rule == TRUE) {
1544                         printf("Deleting %s to %s (match %s)\n", target_name,
1545                                         chain, match_name);
1546
1547                         connman_iptables_delete_rule(table, &ip, chain,
1548                                         target_name, xt_t, match_name, xt_m);
1549
1550                         goto commit;
1551                 }
1552
1553                 if (insert == TRUE) {
1554                         printf("Inserting %s to %s (match %s)\n", target_name,
1555                                         chain, match_name);
1556
1557                         connman_iptables_insert_rule(table, &ip, chain,
1558                                         target_name, xt_t, match_name, xt_m);
1559                 } else {
1560                         printf("Appending %s to %s (match %s)\n", target_name,
1561                                         chain, match_name);
1562
1563                         connman_iptables_append_rule(table, &ip, chain,
1564                                 target_name, xt_t, match_name, xt_m);
1565                 }
1566         }
1567
1568 commit:
1569
1570         connman_iptables_commit(table);
1571
1572 out:
1573         connman_iptables_cleanup(table);
1574
1575         if (xt_t)
1576                 g_free(xt_t->t);
1577
1578         if (xt_m)
1579                 g_free(xt_m->m);
1580
1581         return 0;
1582 }