tools: Target and matches preparation function moves in iptables_test
[framework/connectivity/connman.git] / tools / iptables-test.c
1 /*
2  *  Connection Manager
3  *
4  *  Copyright (C) 2007-2011  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         module = NULL;
1153
1154         table =  g_try_new0(struct connman_iptables, 1);
1155         if (table == NULL)
1156                 return NULL;
1157
1158         table->info =  g_try_new0(struct ipt_getinfo, 1);
1159         if (table->info == NULL)
1160                 goto err;
1161
1162         table->ipt_sock = socket(AF_INET, SOCK_RAW | SOCK_CLOEXEC, IPPROTO_RAW);
1163         if (table->ipt_sock < 0)
1164                 goto err;
1165
1166         s = sizeof(*table->info);
1167         strcpy(table->info->name, table_name);
1168         if (getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_INFO,
1169                                                 table->info, &s) < 0)
1170                 goto err;
1171
1172         table->blob_entries = g_try_malloc0(sizeof(struct ipt_get_entries) +
1173                                                 table->info->size);
1174         if (table->blob_entries == NULL)
1175                 goto err;
1176
1177         strcpy(table->blob_entries->name, table_name);
1178         table->blob_entries->size = table->info->size;
1179
1180         if (connman_iptables_get_entries(table) < 0)
1181                 goto err;
1182
1183         table->num_entries = 0;
1184         table->old_entries = table->info->num_entries;
1185         table->size = 0;
1186
1187         memcpy(table->underflow, table->info->underflow,
1188                                 sizeof(table->info->underflow));
1189         memcpy(table->hook_entry, table->info->hook_entry,
1190                                 sizeof(table->info->hook_entry));
1191
1192         ENTRY_ITERATE(table->blob_entries->entrytable,
1193                         table->blob_entries->size,
1194                                 add_entry, table);
1195
1196         return table;
1197
1198 err:
1199         g_free(module);
1200
1201         connman_iptables_cleanup(table);
1202
1203         return NULL;
1204 }
1205
1206 static struct option connman_iptables_opts[] = {
1207         {.name = "append",        .has_arg = 1, .val = 'A'},
1208         {.name = "delete",        .has_arg = 1, .val = 'D'},
1209         {.name = "flush-chain",   .has_arg = 1, .val = 'F'},
1210         {.name = "insert",        .has_arg = 1, .val = 'I'},
1211         {.name = "list",          .has_arg = 2, .val = 'L'},
1212         {.name = "new-chain",     .has_arg = 1, .val = 'N'},
1213         {.name = "delete-chain",  .has_arg = 1, .val = 'X'},
1214         {.name = "destination",   .has_arg = 1, .val = 'd'},
1215         {.name = "in-interface",  .has_arg = 1, .val = 'i'},
1216         {.name = "jump",          .has_arg = 1, .val = 'j'},
1217         {.name = "match",         .has_arg = 1, .val = 'm'},
1218         {.name = "out-interface", .has_arg = 1, .val = 'o'},
1219         {.name = "source",        .has_arg = 1, .val = 's'},
1220         {.name = "table",         .has_arg = 1, .val = 't'},
1221         {NULL},
1222 };
1223
1224 struct xtables_globals connman_iptables_globals = {
1225         .option_offset = 0,
1226         .opts = connman_iptables_opts,
1227         .orig_opts = connman_iptables_opts,
1228 };
1229
1230 static struct xtables_target *prepare_target(struct connman_iptables *table,
1231                                                         char *target_name)
1232 {
1233         struct xtables_target *xt_t = NULL;
1234         gboolean is_builtin, is_user_defined;
1235         GList *chain_head = NULL;
1236         size_t target_size;
1237
1238         is_builtin = FALSE;
1239         is_user_defined = FALSE;
1240
1241         if (is_builtin_target(target_name))
1242                 is_builtin = TRUE;
1243         else {
1244                 chain_head = find_chain_head(table, target_name);
1245                 if (chain_head != NULL && chain_head->next != NULL)
1246                         is_user_defined = TRUE;
1247         }
1248
1249         if (is_builtin || is_user_defined)
1250                 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
1251                                                 XTF_LOAD_MUST_SUCCEED);
1252         else
1253                 xt_t = xtables_find_target(target_name, XTF_TRY_LOAD);
1254
1255         if (xt_t == NULL)
1256                 return NULL;
1257
1258         target_size = ALIGN(sizeof(struct ipt_entry_target)) + xt_t->size;
1259
1260         xt_t->t = g_try_malloc0(target_size);
1261         if (xt_t->t == NULL)
1262                 return NULL;
1263
1264         xt_t->t->u.target_size = target_size;
1265
1266         if (is_builtin || is_user_defined) {
1267                 struct xt_standard_target *target;
1268
1269                 target = (struct xt_standard_target *)(xt_t->t);
1270                 strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
1271
1272                 if (is_builtin == TRUE)
1273                         target->verdict = target_to_verdict(target_name);
1274                 else if (is_user_defined == TRUE) {
1275                         struct connman_iptables_entry *target_rule;
1276
1277                         if (chain_head == NULL) {
1278                                 g_free(xt_t->t);
1279                                 return NULL;
1280                         }
1281
1282                         target_rule = chain_head->next->data;
1283                         target->verdict = target_rule->offset;
1284                 }
1285         } else {
1286                 strcpy(xt_t->t->u.user.name, target_name);
1287                 xt_t->t->u.user.revision = xt_t->revision;
1288                 if (xt_t->init != NULL)
1289                         xt_t->init(xt_t->t);
1290         }
1291
1292         connman_iptables_globals.opts =
1293                 xtables_merge_options(
1294 #if XTABLES_VERSION_CODE > 5
1295                                 connman_iptables_globals.orig_opts,
1296 #endif
1297                                 connman_iptables_globals.opts,
1298                                 xt_t->extra_opts,
1299                                 &xt_t->option_offset);
1300
1301         if (connman_iptables_globals.opts == NULL) {
1302                 g_free(xt_t->t);
1303                 xt_t = NULL;
1304         }
1305
1306         return xt_t;
1307 }
1308
1309 static struct xtables_match *prepare_matches(struct connman_iptables *table,
1310                         struct xtables_rule_match **xt_rm, char *match_name)
1311 {
1312         struct xtables_match *xt_m;
1313         size_t match_size;
1314
1315         if (match_name == NULL)
1316                 return NULL;
1317
1318         xt_m = xtables_find_match(match_name, XTF_LOAD_MUST_SUCCEED, xt_rm);
1319         match_size = ALIGN(sizeof(struct ipt_entry_match)) + xt_m->size;
1320
1321         xt_m->m = g_try_malloc0(match_size);
1322         if (xt_m->m == NULL)
1323                 return NULL;
1324
1325         xt_m->m->u.match_size = match_size;
1326         strcpy(xt_m->m->u.user.name, xt_m->name);
1327         xt_m->m->u.user.revision = xt_m->revision;
1328
1329         if (xt_m->init != NULL)
1330                 xt_m->init(xt_m->m);
1331
1332         if (xt_m != xt_m->next) {
1333                 connman_iptables_globals.opts =
1334                         xtables_merge_options(
1335 #if XTABLES_VERSION_CODE > 5
1336                                 connman_iptables_globals.orig_opts,
1337 #endif
1338                                 connman_iptables_globals.opts,
1339                                 xt_m->extra_opts,
1340                                 &xt_m->option_offset);
1341
1342                 if (connman_iptables_globals.opts == NULL) {
1343                         g_free(xt_m->m);
1344                         xt_m = NULL;
1345                 }
1346         }
1347
1348         return xt_m;
1349 }
1350
1351 int main(int argc, char *argv[])
1352 {
1353         struct connman_iptables *table;
1354         struct xtables_rule_match *xt_rm;
1355         struct xtables_match *xt_m;
1356         struct xtables_target *xt_t;
1357         struct ipt_ip ip;
1358         char *table_name, *chain, *new_chain, *match_name, *target_name;
1359         char *delete_chain, *flush_chain;
1360         int c, in_len, out_len;
1361         gboolean dump, invert, delete, insert, delete_rule;
1362         struct in_addr src, dst;
1363
1364         xtables_init_all(&connman_iptables_globals, NFPROTO_IPV4);
1365
1366         dump = FALSE;
1367         invert = FALSE;
1368         delete = FALSE;
1369         insert = FALSE;
1370         delete_rule = FALSE;
1371         table_name = chain = new_chain = match_name = target_name = NULL;
1372         delete_chain = flush_chain = NULL;
1373         memset(&ip, 0, sizeof(struct ipt_ip));
1374         table = NULL;
1375         xt_rm = NULL;
1376         xt_m = NULL;
1377         xt_t = NULL;
1378
1379         while ((c = getopt_long(argc, argv, "-A:D:F:I:L::N:X:d:i:j:m:o:s:t:",
1380                                 connman_iptables_globals.opts, NULL)) != -1) {
1381                 switch (c) {
1382                 case 'A':
1383                         /* It is either -A, -D or -I at once */
1384                         if (chain)
1385                                 goto out;
1386
1387                         chain = optarg;
1388                         break;
1389
1390                 case 'D':
1391                         /* It is either -A, -D or -I at once */
1392                         if (chain)
1393                                 goto out;
1394
1395                         chain = optarg;
1396                         delete_rule = TRUE;
1397                         break;
1398
1399                 case 'F':
1400                         flush_chain = optarg;
1401                         break;
1402
1403                 case 'I':
1404                         /* It is either -A, -D or -I at once */
1405                         if (chain)
1406                                 goto out;
1407
1408                         chain = optarg;
1409                         insert = TRUE;
1410                         break;
1411
1412                 case 'L':
1413                         dump = true;
1414                         break;
1415
1416                 case 'N':
1417                         new_chain = optarg;
1418                         break;
1419
1420                 case 'X':
1421                         delete = true;
1422                         delete_chain = optarg;
1423                         break;
1424
1425                 case 'd':
1426                         if (!inet_pton(AF_INET, optarg, &dst))
1427                                 break;
1428
1429                         ip.dst = dst;
1430                         inet_pton(AF_INET, "255.255.255.255", &ip.dmsk);
1431
1432                         if (invert)
1433                                 ip.invflags |= IPT_INV_DSTIP;
1434
1435                         break;
1436
1437                 case 'i':
1438                         in_len = strlen(optarg);
1439
1440                         if (in_len + 1 > IFNAMSIZ)
1441                                 break;
1442
1443                         strcpy(ip.iniface, optarg);
1444                         memset(ip.iniface_mask, 0xff, in_len + 1);
1445
1446                         if (invert)
1447                                 ip.invflags |= IPT_INV_VIA_IN;
1448
1449                         break;
1450
1451                 case 'j':
1452                         target_name = optarg;
1453                         xt_t = prepare_target(table, target_name);
1454                         if (xt_t == NULL)
1455                                 goto out;
1456
1457                         break;
1458
1459                 case 'm':
1460                         match_name = optarg;
1461                         xt_m = prepare_matches(table, &xt_rm, match_name);
1462                         if (xt_m == NULL)
1463                                 goto out;
1464
1465                         break;
1466
1467                 case 'o':
1468                         out_len = strlen(optarg);
1469
1470                         if (out_len + 1 > IFNAMSIZ)
1471                                 break;
1472
1473                         strcpy(ip.outiface, optarg);
1474                         memset(ip.outiface_mask, 0xff, out_len + 1);
1475
1476                         if (invert)
1477                                 ip.invflags |= IPT_INV_VIA_OUT;
1478
1479                         break;
1480
1481                 case 's':
1482                         if (!inet_pton(AF_INET, optarg, &src))
1483                                 break;
1484
1485                         ip.src = src;
1486                         inet_pton(AF_INET, "255.255.255.255", &ip.smsk);
1487
1488                         if (invert)
1489                                 ip.invflags |= IPT_INV_SRCIP;
1490
1491                         break;
1492
1493                 case 't':
1494                         table_name = optarg;
1495                         break;
1496
1497                 case 1:
1498                         if (optarg[0] == '!' && optarg[1] == '\0') {
1499                                 if (invert)
1500                                         printf("Consecutive ! not allowed\n");
1501
1502                                 invert = TRUE;
1503                                 optarg[0] = '\0';
1504                                 continue;
1505                         }
1506
1507                         printf("Invalid option\n");
1508
1509                         return -1;
1510
1511                 default:
1512                         if (xt_t == NULL || xt_t->parse == NULL ||
1513                             !xt_t->parse(c - xt_t->option_offset, argv, invert,
1514                                         &xt_t->tflags, NULL, &xt_t->t)) {
1515                                 if (xt_m == NULL || xt_m->parse == NULL)
1516                                         break;
1517
1518                                 xt_m->parse(c - xt_m->option_offset, argv,
1519                                         invert, &xt_m->mflags, NULL, &xt_m->m);
1520                         }
1521
1522                         break;
1523                 }
1524         }
1525
1526         if (table_name == NULL)
1527                 table_name = "filter";
1528
1529         table = connman_iptables_init(table_name);
1530         if (table == NULL)
1531                 return -1;
1532
1533         if (delete) {
1534                 if (delete_chain == NULL)
1535                         goto out;
1536
1537                 printf("Delete chain %s\n", delete_chain);
1538
1539                 connman_iptables_delete_chain(table, delete_chain);
1540
1541                 goto commit;
1542         }
1543
1544         if (flush_chain) {
1545                 printf("Flush chain %s\n", flush_chain);
1546
1547                 connman_iptables_flush_chain(table, flush_chain);
1548
1549                 goto commit;
1550         }
1551
1552         if (dump) {
1553                 connman_iptables_dump(table);
1554
1555                 return 0;
1556         }
1557
1558         if (chain && new_chain)
1559                 return -1;
1560
1561         if (new_chain) {
1562                 printf("New chain %s\n", new_chain);
1563
1564                 connman_iptables_add_chain(table, new_chain);
1565
1566                 goto commit;
1567         }
1568
1569         if (chain) {
1570                 if (delete_rule == TRUE) {
1571                         printf("Deleting %s to %s (match %s)\n", target_name,
1572                                         chain, match_name);
1573
1574                         connman_iptables_delete_rule(table, &ip, chain,
1575                                         target_name, xt_t, match_name, xt_m);
1576
1577                         goto commit;
1578                 }
1579
1580                 if (insert == TRUE) {
1581                         printf("Inserting %s to %s (match %s)\n", target_name,
1582                                         chain, match_name);
1583
1584                         connman_iptables_insert_rule(table, &ip, chain,
1585                                         target_name, xt_t, match_name, xt_m);
1586                 } else {
1587                         printf("Appending %s to %s (match %s)\n", target_name,
1588                                         chain, match_name);
1589
1590                         connman_iptables_append_rule(table, &ip, chain,
1591                                 target_name, xt_t, match_name, xt_m);
1592                 }
1593         }
1594
1595 commit:
1596
1597         connman_iptables_commit(table);
1598
1599 out:
1600         connman_iptables_cleanup(table);
1601
1602         if (xt_t)
1603                 g_free(xt_t->t);
1604
1605         if (xt_m)
1606                 g_free(xt_m->m);
1607
1608         return 0;
1609 }