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