Adding connman-test subpackage
[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                         struct xtables_rule_match *xt_rm)
537 {
538         struct xtables_rule_match *tmp_xt_rm;
539         struct ipt_entry *new_entry;
540         size_t match_size, target_size;
541
542         match_size = 0;
543         for (tmp_xt_rm = xt_rm; tmp_xt_rm != NULL; tmp_xt_rm = tmp_xt_rm->next)
544                 match_size += tmp_xt_rm->match->m->u.match_size;
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
562         match_size = 0;
563         for (tmp_xt_rm = xt_rm; tmp_xt_rm != NULL;
564                                 tmp_xt_rm = tmp_xt_rm->next) {
565                 memcpy(new_entry->elems + match_size, tmp_xt_rm->match->m,
566                                         tmp_xt_rm->match->m->u.match_size);
567                 match_size += tmp_xt_rm->match->m->u.match_size;
568         }
569
570         if (xt_t) {
571                 struct xt_entry_target *entry_target;
572
573                 entry_target = ipt_get_target(new_entry);
574                 memcpy(entry_target, xt_t->t, target_size);
575         }
576
577         return new_entry;
578 }
579
580 static void update_hooks(struct connman_iptables *table, GList *chain_head,
581                                 struct ipt_entry *entry)
582 {
583         GList *list;
584         struct connman_iptables_entry *head, *e;
585         int builtin;
586
587         if (chain_head == NULL)
588                 return;
589
590         head = chain_head->data;
591
592         builtin = head->builtin;
593         if (builtin < 0)
594                 return;
595
596         table->underflow[builtin] += entry->next_offset;
597
598         for (list = chain_head->next; list; list = list->next) {
599                 e = list->data;
600
601                 builtin = e->builtin;
602                 if (builtin < 0)
603                         continue;
604
605                 table->hook_entry[builtin] += entry->next_offset;
606                 table->underflow[builtin] += entry->next_offset;
607         }
608 }
609
610 static struct ipt_entry *prepare_rule_inclusion(struct connman_iptables *table,
611                                 struct ipt_ip *ip, char *chain_name,
612                                 char *target_name, struct xtables_target *xt_t,
613                                 int *builtin, struct xtables_rule_match *xt_rm)
614 {
615         GList *chain_tail, *chain_head;
616         struct ipt_entry *new_entry;
617         struct connman_iptables_entry *head;
618
619         chain_head = find_chain_head(table, chain_name);
620         if (chain_head == NULL)
621                 return NULL;
622
623         chain_tail = find_chain_tail(table, chain_name);
624         if (chain_tail == NULL)
625                 return NULL;
626
627         new_entry = new_rule(ip, target_name, xt_t, xt_rm);
628         if (new_entry == NULL)
629                 return NULL;
630
631         update_hooks(table, chain_head, new_entry);
632
633         /*
634          * If the chain is builtin, and does not have any rule,
635          * then the one that we're inserting is becoming the head
636          * and thus needs the builtin flag.
637          */
638         head = chain_head->data;
639         if (head->builtin < 0)
640                 *builtin = -1;
641         else if (chain_head == chain_tail->prev) {
642                 *builtin = head->builtin;
643                 head->builtin = -1;
644         }
645
646         return new_entry;
647 }
648
649 static int connman_iptables_append_rule(struct connman_iptables *table,
650                                 struct ipt_ip *ip, char *chain_name,
651                                 char *target_name, struct xtables_target *xt_t,
652                                 struct xtables_rule_match *xt_rm)
653 {
654         GList *chain_tail;
655         struct ipt_entry *new_entry;
656         int builtin = -1, ret;
657
658         chain_tail = find_chain_tail(table, chain_name);
659         if (chain_tail == NULL)
660                 return -EINVAL;
661
662         new_entry = prepare_rule_inclusion(table, ip, chain_name,
663                                         target_name, xt_t, &builtin, xt_rm);
664         if (new_entry == NULL)
665                 return -EINVAL;
666
667         ret = connman_add_entry(table, new_entry, chain_tail->prev, builtin);
668         if (ret < 0)
669                 g_free(new_entry);
670
671         return ret;
672 }
673
674 static int connman_iptables_insert_rule(struct connman_iptables *table,
675                                 struct ipt_ip *ip, char *chain_name,
676                                 char *target_name, struct xtables_target *xt_t,
677                                 struct xtables_rule_match *xt_rm)
678 {
679         GList *chain_head;
680         struct ipt_entry *new_entry;
681         int builtin = -1, ret;
682
683         chain_head = find_chain_head(table, chain_name);
684         if (chain_head == NULL)
685                 return -EINVAL;
686
687         new_entry = prepare_rule_inclusion(table, ip, chain_name,
688                                         target_name, xt_t, &builtin, xt_rm);
689         if (new_entry == NULL)
690                 return -EINVAL;
691
692         ret = connman_add_entry(table, new_entry, chain_head->next, builtin);
693         if (ret < 0)
694                 g_free(new_entry);
695
696         return ret;
697 }
698
699 static gboolean is_same_ipt_entry(struct ipt_entry *i_e1,
700                                         struct ipt_entry *i_e2)
701 {
702         if (memcmp(&i_e1->ip, &i_e2->ip, sizeof(struct ipt_ip)) != 0)
703                 return FALSE;
704
705         if (i_e1->target_offset != i_e2->target_offset)
706                 return FALSE;
707
708         if (i_e1->next_offset != i_e2->next_offset)
709                 return FALSE;
710
711         return TRUE;
712 }
713
714 static gboolean is_same_target(struct xt_entry_target *xt_e_t1,
715                                         struct xt_entry_target *xt_e_t2)
716 {
717         if (xt_e_t1 == NULL || xt_e_t2 == NULL)
718                 return FALSE;
719
720         if (strcmp(xt_e_t1->u.user.name, IPT_STANDARD_TARGET) == 0) {
721                 struct xt_standard_target *xt_s_t1;
722                 struct xt_standard_target *xt_s_t2;
723
724                 xt_s_t1 = (struct xt_standard_target *) xt_e_t1;
725                 xt_s_t2 = (struct xt_standard_target *) xt_e_t2;
726
727                 if (xt_s_t1->verdict != xt_s_t2->verdict)
728                         return FALSE;
729         } else {
730                 if (xt_e_t1->u.target_size != xt_e_t2->u.target_size)
731                         return FALSE;
732
733                 if (strcmp(xt_e_t1->u.user.name, xt_e_t2->u.user.name) != 0)
734                         return FALSE;
735         }
736
737         return TRUE;
738 }
739
740 static gboolean is_same_match(struct xt_entry_match *xt_e_m1,
741                                 struct xt_entry_match *xt_e_m2)
742 {
743         if (xt_e_m1 == NULL || xt_e_m2 == NULL)
744                 return FALSE;
745
746         if (xt_e_m1->u.match_size != xt_e_m2->u.match_size)
747                 return FALSE;
748
749         if (xt_e_m1->u.user.revision != xt_e_m2->u.user.revision)
750                 return FALSE;
751
752         if (strcmp(xt_e_m1->u.user.name, xt_e_m2->u.user.name) != 0)
753                 return FALSE;
754
755         return TRUE;
756 }
757
758 static int connman_iptables_delete_rule(struct connman_iptables *table,
759                                 struct ipt_ip *ip, char *chain_name,
760                                 char *target_name, struct xtables_target *xt_t,
761                                 struct xtables_match *xt_m,
762                                 struct xtables_rule_match *xt_rm)
763 {
764         GList *chain_tail, *chain_head, *list;
765         struct xt_entry_target *xt_e_t = NULL;
766         struct xt_entry_match *xt_e_m = NULL;
767         struct connman_iptables_entry *entry;
768         struct ipt_entry *entry_test;
769         int builtin, removed;
770
771         removed = 0;
772
773         chain_head = find_chain_head(table, chain_name);
774         if (chain_head == NULL)
775                 return -EINVAL;
776
777         chain_tail = find_chain_tail(table, chain_name);
778         if (chain_tail == NULL)
779                 return -EINVAL;
780
781         if (!xt_t && !xt_m)
782                 return -EINVAL;
783
784         entry_test = new_rule(ip, target_name, xt_t, xt_rm);
785         if (entry_test == NULL)
786                 return -EINVAL;
787
788         if (xt_t != NULL)
789                 xt_e_t = ipt_get_target(entry_test);
790         if (xt_m != NULL)
791                 xt_e_m = (struct xt_entry_match *)entry_test->elems;
792
793         entry = chain_head->data;
794         builtin = entry->builtin;
795
796         if (builtin >= 0)
797                 list = chain_head;
798         else
799                 list = chain_head->next;
800
801         for (entry = NULL; list != chain_tail->prev; list = list->next) {
802                 struct connman_iptables_entry *tmp;
803                 struct ipt_entry *tmp_e;
804
805                 tmp = list->data;
806                 tmp_e = tmp->entry;
807
808                 if (is_same_ipt_entry(entry_test, tmp_e) == FALSE)
809                         continue;
810
811                 if (xt_t != NULL) {
812                         struct xt_entry_target *tmp_xt_e_t;
813
814                         tmp_xt_e_t = ipt_get_target(tmp_e);
815
816                         if (!is_same_target(tmp_xt_e_t, xt_e_t))
817                                 continue;
818                 }
819
820                 if (xt_m != NULL) {
821                         struct xt_entry_match *tmp_xt_e_m;
822
823                         tmp_xt_e_m = (struct xt_entry_match *)tmp_e->elems;
824
825                         if (!is_same_match(tmp_xt_e_m, xt_e_m))
826                                 continue;
827                 }
828
829                 entry = tmp;
830                 break;
831         }
832
833         if (entry == NULL) {
834                 g_free(entry_test);
835                 return -EINVAL;
836         }
837
838         /* We have deleted a rule,
839          * all references should be bumped accordingly */
840         if (list->next != NULL)
841                 update_targets_reference(table, list->next->data,
842                                                 list->data, TRUE);
843
844         removed += remove_table_entry(table, entry);
845
846         if (builtin >= 0) {
847                 list = list->next;
848                 if (list) {
849                         entry = list->data;
850                         entry->builtin = builtin;
851                 }
852
853                 table->underflow[builtin] -= removed;
854                 for (list = chain_tail; list; list = list->next) {
855                         entry = list->data;
856
857                         builtin = entry->builtin;
858                         if (builtin < 0)
859                                 continue;
860
861                         table->hook_entry[builtin] -= removed;
862                         table->underflow[builtin] -= removed;
863                 }
864         }
865
866         update_offsets(table);
867
868         return 0;
869 }
870
871 static int connman_iptables_change_policy(struct connman_iptables *table,
872                                                 char *chain_name, char *policy)
873 {
874         GList *chain_head;
875         struct connman_iptables_entry *entry;
876         struct xt_entry_target *target;
877         struct xt_standard_target *t;
878         int verdict;
879
880         verdict = target_to_verdict(policy);
881         if (verdict == 0)
882                 return -EINVAL;
883
884         chain_head = find_chain_head(table, chain_name);
885         if (chain_head == NULL)
886                 return -EINVAL;
887
888         entry = chain_head->data;
889         if (entry->builtin < 0)
890                 return -EINVAL;
891
892         target = ipt_get_target(entry->entry);
893
894         t = (struct xt_standard_target *)target;
895         t->verdict = verdict;
896
897         return 0;
898 }
899
900 static struct ipt_replace *connman_iptables_blob(struct connman_iptables *table)
901 {
902         struct ipt_replace *r;
903         GList *list;
904         struct connman_iptables_entry *e;
905         unsigned char *entry_index;
906
907         r = g_try_malloc0(sizeof(struct ipt_replace) + table->size);
908         if (r == NULL)
909                 return NULL;
910
911         memset(r, 0, sizeof(*r) + table->size);
912
913         r->counters = g_try_malloc0(sizeof(struct xt_counters)
914                                 * table->old_entries);
915         if (r->counters == NULL) {
916                 g_free(r);
917                 return NULL;
918         }
919
920         strcpy(r->name, table->info->name);
921         r->num_entries = table->num_entries;
922         r->size = table->size;
923
924         r->num_counters = table->old_entries;
925         r->valid_hooks  = table->info->valid_hooks;
926
927         memcpy(r->hook_entry, table->hook_entry, sizeof(table->hook_entry));
928         memcpy(r->underflow, table->underflow, sizeof(table->underflow));
929
930         entry_index = (unsigned char *)r->entries;
931         for (list = table->entries; list; list = list->next) {
932                 e = list->data;
933
934                 memcpy(entry_index, e->entry, e->entry->next_offset);
935                 entry_index += e->entry->next_offset;
936         }
937
938         return r;
939 }
940
941 static void dump_target(struct connman_iptables *table,
942                                 struct ipt_entry *entry)
943
944 {
945         struct xtables_target *xt_t;
946         struct xt_entry_target *target;
947
948         target = ipt_get_target(entry);
949
950         if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
951                 struct xt_standard_target *t;
952
953                 t = (struct xt_standard_target *)target;
954
955                 switch (t->verdict) {
956                 case XT_RETURN:
957                         printf("\ttarget RETURN\n");
958                         break;
959
960                 case -NF_ACCEPT - 1:
961                         printf("\ttarget ACCEPT\n");
962                         break;
963
964                 case -NF_DROP - 1:
965                         printf("\ttarget DROP\n");
966                         break;
967
968                 case -NF_QUEUE - 1:
969                         printf("\ttarget QUEUE\n");
970                         break;
971
972                 case -NF_STOP - 1:
973                         printf("\ttarget STOP\n");
974                         break;
975
976                 default:
977                         printf("\tJUMP @%p (0x%x)\n",
978                                 (char*)table->blob_entries->entrytable +
979                                 t->verdict, t->verdict);
980                         break;
981                 }
982
983                 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
984                                                 XTF_LOAD_MUST_SUCCEED);
985
986                 if(xt_t->print != NULL)
987                         xt_t->print(NULL, target, 1);
988         } else {
989                 xt_t = xtables_find_target(target->u.user.name, XTF_TRY_LOAD);
990                 if (xt_t == NULL) {
991                         printf("\ttarget %s\n", target->u.user.name);
992                         return;
993                 }
994
995                 if(xt_t->print != NULL) {
996                         printf("\ttarget ");
997                         xt_t->print(NULL, target, 1);
998                         printf("\n");
999                 }
1000         }
1001 }
1002
1003 static void dump_match(struct connman_iptables *table, struct ipt_entry *entry)
1004 {
1005         struct xtables_match *xt_m;
1006         struct xt_entry_match *match;
1007
1008         if (entry->elems == (unsigned char *)entry + entry->target_offset)
1009                 return;
1010
1011         match = (struct xt_entry_match *) entry->elems;
1012
1013         if (!strlen(match->u.user.name))
1014                 return;
1015
1016         xt_m = xtables_find_match(match->u.user.name, XTF_TRY_LOAD, NULL);
1017         if (xt_m == NULL)
1018                 goto out;
1019
1020         if(xt_m->print != NULL) {
1021                 printf("\tmatch ");
1022                 xt_m->print(NULL, match, 1);
1023                 printf("\n");
1024
1025                 return;
1026         }
1027
1028 out:
1029         printf("\tmatch %s\n", match->u.user.name);
1030
1031 }
1032
1033 static int connman_iptables_dump_entry(struct ipt_entry *entry,
1034                                         struct connman_iptables *table)
1035 {
1036         struct xt_entry_target *target;
1037         unsigned int offset;
1038         int builtin;
1039
1040         offset = (char *)entry - (char *)table->blob_entries->entrytable;
1041         target = ipt_get_target(entry);
1042         builtin = is_hook_entry(table, entry);
1043
1044         if (entry_to_offset(table, entry) + entry->next_offset ==
1045                                         table->blob_entries->size) {
1046                 printf("End of CHAIN 0x%x\n", offset);
1047                 return 0;
1048         }
1049
1050         if (!strcmp(target->u.user.name, IPT_ERROR_TARGET)) {
1051                 printf("USER CHAIN (%s) %p  match %p  target %p  size %d\n",
1052                         target->data, entry, entry->elems,
1053                         (char *)entry + entry->target_offset,
1054                                 entry->next_offset);
1055
1056                 return 0;
1057         } else if (builtin >= 0) {
1058                 printf("CHAIN (%s) %p  match %p  target %p  size %d\n",
1059                         hooknames[builtin], entry, entry->elems,
1060                         (char *)entry + entry->target_offset,
1061                                 entry->next_offset);
1062         } else {
1063                 printf("RULE %p  match %p  target %p  size %d\n", entry,
1064                         entry->elems,
1065                         (char *)entry + entry->target_offset,
1066                                 entry->next_offset);
1067         }
1068
1069         dump_match(table, entry);
1070         dump_target(table, entry);
1071
1072         return 0;
1073 }
1074
1075 static void connman_iptables_dump_hook(struct connman_iptables *table)
1076 {
1077         int i;
1078         printf("hooks: \n");
1079         for (i = 0; i < NF_INET_NUMHOOKS; i++) {
1080                 if ((table->info->valid_hooks & (1 << i)))
1081                         printf("%s entry %p underflow %p (%#x)\n",
1082                                 hooknames[i],
1083                                 table->blob_entries->entrytable +
1084                                                 table->info->hook_entry[i],
1085                                 table->blob_entries->entrytable +
1086                                                 table->info->underflow[i],
1087                                         table->info->underflow[i]);
1088         }
1089 }
1090
1091 static void connman_iptables_dump(struct connman_iptables *table)
1092 {
1093         printf("%s valid_hooks=0x%08x, num_entries=%u, size=%u\n",
1094                 table->info->name,
1095                 table->info->valid_hooks, table->info->num_entries,
1096                 table->info->size);
1097
1098         connman_iptables_dump_hook(table);
1099
1100         ENTRY_ITERATE(table->blob_entries->entrytable,
1101                         table->blob_entries->size,
1102                         connman_iptables_dump_entry, table);
1103
1104 }
1105
1106 static int connman_iptables_get_entries(struct connman_iptables *table)
1107 {
1108         socklen_t entry_size;
1109
1110         entry_size = sizeof(struct ipt_get_entries) + table->info->size;
1111
1112         return getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_ENTRIES,
1113                                 table->blob_entries, &entry_size);
1114 }
1115
1116 static int connman_iptables_replace(struct connman_iptables *table,
1117                                         struct ipt_replace *r)
1118 {
1119         return setsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_SET_REPLACE, r,
1120                          sizeof(*r) + r->size);
1121 }
1122
1123 static void connman_iptables_cleanup(struct connman_iptables *table)
1124 {
1125         GList *list;
1126         struct connman_iptables_entry *entry;
1127
1128         close(table->ipt_sock);
1129
1130         for (list = table->entries; list; list = list->next) {
1131                 entry = list->data;
1132
1133                 g_free(entry->entry);
1134         }
1135
1136         g_free(table->info);
1137         g_free(table->blob_entries);
1138         g_free(table);
1139
1140         xtables_free_opts(1);
1141 }
1142
1143 static int connman_iptables_commit(struct connman_iptables *table)
1144 {
1145         struct ipt_replace *repl;
1146
1147         repl = connman_iptables_blob(table);
1148
1149         return connman_iptables_replace(table, repl);
1150 }
1151
1152 static int add_entry(struct ipt_entry *entry, struct connman_iptables *table)
1153 {
1154         struct ipt_entry *new_entry;
1155         int builtin;
1156
1157         new_entry = g_try_malloc0(entry->next_offset);
1158         if (new_entry == NULL)
1159                 return -ENOMEM;
1160
1161         memcpy(new_entry, entry, entry->next_offset);
1162
1163         builtin = is_hook_entry(table, entry);
1164
1165         return connman_add_entry(table, new_entry, NULL, builtin);
1166 }
1167
1168 static struct connman_iptables *connman_iptables_init(const char *table_name)
1169 {
1170         struct connman_iptables *table = NULL;
1171         char *module = NULL;
1172         socklen_t s;
1173
1174         if (xtables_insmod("ip_tables", NULL, TRUE) != 0)
1175                 goto err;
1176
1177         module = g_strconcat("iptable_", table_name, NULL);
1178         if (module == NULL)
1179                 goto err;
1180
1181         if (xtables_insmod(module, NULL, TRUE) != 0)
1182                 goto err;
1183
1184         g_free(module);
1185         module = NULL;
1186
1187         table =  g_try_new0(struct connman_iptables, 1);
1188         if (table == NULL)
1189                 return NULL;
1190
1191         table->info =  g_try_new0(struct ipt_getinfo, 1);
1192         if (table->info == NULL)
1193                 goto err;
1194
1195         table->ipt_sock = socket(AF_INET, SOCK_RAW | SOCK_CLOEXEC, IPPROTO_RAW);
1196         if (table->ipt_sock < 0)
1197                 goto err;
1198
1199         s = sizeof(*table->info);
1200         strcpy(table->info->name, table_name);
1201         if (getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_INFO,
1202                                                 table->info, &s) < 0)
1203                 goto err;
1204
1205         table->blob_entries = g_try_malloc0(sizeof(struct ipt_get_entries) +
1206                                                 table->info->size);
1207         if (table->blob_entries == NULL)
1208                 goto err;
1209
1210         strcpy(table->blob_entries->name, table_name);
1211         table->blob_entries->size = table->info->size;
1212
1213         if (connman_iptables_get_entries(table) < 0)
1214                 goto err;
1215
1216         table->num_entries = 0;
1217         table->old_entries = table->info->num_entries;
1218         table->size = 0;
1219
1220         memcpy(table->underflow, table->info->underflow,
1221                                 sizeof(table->info->underflow));
1222         memcpy(table->hook_entry, table->info->hook_entry,
1223                                 sizeof(table->info->hook_entry));
1224
1225         ENTRY_ITERATE(table->blob_entries->entrytable,
1226                         table->blob_entries->size,
1227                                 add_entry, table);
1228
1229         return table;
1230
1231 err:
1232         g_free(module);
1233
1234         connman_iptables_cleanup(table);
1235
1236         return NULL;
1237 }
1238
1239 static struct option connman_iptables_opts[] = {
1240         {.name = "append",        .has_arg = 1, .val = 'A'},
1241         {.name = "delete",        .has_arg = 1, .val = 'D'},
1242         {.name = "flush-chain",   .has_arg = 1, .val = 'F'},
1243         {.name = "insert",        .has_arg = 1, .val = 'I'},
1244         {.name = "list",          .has_arg = 2, .val = 'L'},
1245         {.name = "new-chain",     .has_arg = 1, .val = 'N'},
1246         {.name = "policy",        .has_arg = 1, .val = 'P'},
1247         {.name = "delete-chain",  .has_arg = 1, .val = 'X'},
1248         {.name = "destination",   .has_arg = 1, .val = 'd'},
1249         {.name = "in-interface",  .has_arg = 1, .val = 'i'},
1250         {.name = "jump",          .has_arg = 1, .val = 'j'},
1251         {.name = "match",         .has_arg = 1, .val = 'm'},
1252         {.name = "out-interface", .has_arg = 1, .val = 'o'},
1253         {.name = "source",        .has_arg = 1, .val = 's'},
1254         {.name = "table",         .has_arg = 1, .val = 't'},
1255         {NULL},
1256 };
1257
1258 struct xtables_globals connman_iptables_globals = {
1259         .option_offset = 0,
1260         .opts = connman_iptables_opts,
1261         .orig_opts = connman_iptables_opts,
1262 };
1263
1264 static struct xtables_target *prepare_target(struct connman_iptables *table,
1265                                                         char *target_name)
1266 {
1267         struct xtables_target *xt_t = NULL;
1268         gboolean is_builtin, is_user_defined;
1269         GList *chain_head = NULL;
1270         size_t target_size;
1271
1272         is_builtin = FALSE;
1273         is_user_defined = FALSE;
1274
1275         if (is_builtin_target(target_name))
1276                 is_builtin = TRUE;
1277         else {
1278                 chain_head = find_chain_head(table, target_name);
1279                 if (chain_head != NULL && chain_head->next != NULL)
1280                         is_user_defined = TRUE;
1281         }
1282
1283         if (is_builtin || is_user_defined)
1284                 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
1285                                                 XTF_LOAD_MUST_SUCCEED);
1286         else
1287                 xt_t = xtables_find_target(target_name, XTF_TRY_LOAD);
1288
1289         if (xt_t == NULL)
1290                 return NULL;
1291
1292         target_size = ALIGN(sizeof(struct ipt_entry_target)) + xt_t->size;
1293
1294         xt_t->t = g_try_malloc0(target_size);
1295         if (xt_t->t == NULL)
1296                 return NULL;
1297
1298         xt_t->t->u.target_size = target_size;
1299
1300         if (is_builtin || is_user_defined) {
1301                 struct xt_standard_target *target;
1302
1303                 target = (struct xt_standard_target *)(xt_t->t);
1304                 strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
1305
1306                 if (is_builtin == TRUE)
1307                         target->verdict = target_to_verdict(target_name);
1308                 else if (is_user_defined == TRUE) {
1309                         struct connman_iptables_entry *target_rule;
1310
1311                         if (chain_head == NULL) {
1312                                 g_free(xt_t->t);
1313                                 return NULL;
1314                         }
1315
1316                         target_rule = chain_head->next->data;
1317                         target->verdict = target_rule->offset;
1318                 }
1319         } else {
1320                 strcpy(xt_t->t->u.user.name, target_name);
1321                 xt_t->t->u.user.revision = xt_t->revision;
1322                 if (xt_t->init != NULL)
1323                         xt_t->init(xt_t->t);
1324         }
1325
1326 #if XTABLES_VERSION_CODE > 5
1327         if (xt_t->x6_options != NULL)
1328                 connman_iptables_globals.opts =
1329                         xtables_options_xfrm(
1330                                 connman_iptables_globals.orig_opts,
1331
1332                                 connman_iptables_globals.opts,
1333                                 xt_t->x6_options,
1334                                 &xt_t->option_offset);
1335         else
1336 #endif
1337                 connman_iptables_globals.opts =
1338                         xtables_merge_options(
1339 #if XTABLES_VERSION_CODE > 5
1340                                 connman_iptables_globals.orig_opts,
1341 #endif
1342                                 connman_iptables_globals.opts,
1343                                 xt_t->extra_opts,
1344                                 &xt_t->option_offset);
1345
1346         if (connman_iptables_globals.opts == NULL) {
1347                 g_free(xt_t->t);
1348                 xt_t = NULL;
1349         }
1350
1351         return xt_t;
1352 }
1353
1354 static struct xtables_match *prepare_matches(struct connman_iptables *table,
1355                         struct xtables_rule_match **xt_rm, char *match_name)
1356 {
1357         struct xtables_match *xt_m;
1358         size_t match_size;
1359
1360         if (match_name == NULL)
1361                 return NULL;
1362
1363         xt_m = xtables_find_match(match_name, XTF_LOAD_MUST_SUCCEED, xt_rm);
1364         match_size = ALIGN(sizeof(struct ipt_entry_match)) + xt_m->size;
1365
1366         xt_m->m = g_try_malloc0(match_size);
1367         if (xt_m->m == NULL)
1368                 return NULL;
1369
1370         xt_m->m->u.match_size = match_size;
1371         strcpy(xt_m->m->u.user.name, xt_m->name);
1372         xt_m->m->u.user.revision = xt_m->revision;
1373
1374         if (xt_m->init != NULL)
1375                 xt_m->init(xt_m->m);
1376
1377         if (xt_m == xt_m->next)
1378                 goto done;
1379
1380 #if XTABLES_VERSION_CODE > 5
1381         if (xt_m->x6_options != NULL)
1382                 connman_iptables_globals.opts =
1383                         xtables_options_xfrm(
1384                                 connman_iptables_globals.orig_opts,
1385                                 connman_iptables_globals.opts,
1386                                 xt_m->x6_options,
1387                                 &xt_m->option_offset);
1388         else
1389 #endif
1390                 connman_iptables_globals.opts =
1391                         xtables_merge_options(
1392 #if XTABLES_VERSION_CODE > 5
1393                                 connman_iptables_globals.orig_opts,
1394 #endif
1395                                 connman_iptables_globals.opts,
1396                                 xt_m->extra_opts,
1397                                 &xt_m->option_offset);
1398
1399         if (connman_iptables_globals.opts == NULL) {
1400                 g_free(xt_m->m);
1401                 xt_m = NULL;
1402         }
1403
1404 done:
1405         return xt_m;
1406 }
1407
1408 int main(int argc, char *argv[])
1409 {
1410         struct connman_iptables *table;
1411         struct xtables_rule_match *xt_rm, *tmp_xt_rm;
1412         struct xtables_match *xt_m, *xt_m_t;
1413         struct xtables_target *xt_t;
1414         struct ipt_ip ip;
1415         char *table_name, *chain, *new_chain, *match_name, *target_name;
1416         char *delete_chain, *flush_chain, *policy;
1417         int c, in_len, out_len;
1418         gboolean dump, invert, delete, insert, delete_rule;
1419         struct in_addr src, dst;
1420
1421         xtables_init_all(&connman_iptables_globals, NFPROTO_IPV4);
1422
1423         dump = FALSE;
1424         invert = FALSE;
1425         delete = FALSE;
1426         insert = FALSE;
1427         delete_rule = FALSE;
1428         table_name = chain = new_chain = match_name = target_name = NULL;
1429         delete_chain = flush_chain = policy = NULL;
1430         memset(&ip, 0, sizeof(struct ipt_ip));
1431         table = NULL;
1432         xt_rm = NULL;
1433         xt_m = NULL;
1434         xt_t = NULL;
1435
1436         /* extension's options will generate false-positives errors */
1437         opterr = 0;
1438
1439         while ((c = getopt_long(argc, argv, "-A:D:F:I:L::N:P:X:d:i:j:m:o:s:t:",
1440                                 connman_iptables_globals.opts, NULL)) != -1) {
1441                 switch (c) {
1442                 case 'A':
1443                         /* It is either -A, -D or -I at once */
1444                         if (chain)
1445                                 goto out;
1446
1447                         chain = optarg;
1448                         break;
1449
1450                 case 'D':
1451                         /* It is either -A, -D or -I at once */
1452                         if (chain)
1453                                 goto out;
1454
1455                         chain = optarg;
1456                         delete_rule = TRUE;
1457                         break;
1458
1459                 case 'F':
1460                         flush_chain = optarg;
1461                         break;
1462
1463                 case 'I':
1464                         /* It is either -A, -D or -I at once */
1465                         if (chain)
1466                                 goto out;
1467
1468                         chain = optarg;
1469                         insert = TRUE;
1470                         break;
1471
1472                 case 'L':
1473                         dump = true;
1474                         break;
1475
1476                 case 'N':
1477                         new_chain = optarg;
1478                         break;
1479
1480                 case 'P':
1481                         chain = optarg;
1482                         if (optind < argc)
1483                                 policy = argv[optind++];
1484                         else
1485                                 goto out;
1486
1487                         break;
1488
1489                 case 'X':
1490                         delete = true;
1491                         delete_chain = optarg;
1492                         break;
1493
1494                 case 'd':
1495                         if (!inet_pton(AF_INET, optarg, &dst))
1496                                 break;
1497
1498                         ip.dst = dst;
1499                         inet_pton(AF_INET, "255.255.255.255", &ip.dmsk);
1500
1501                         if (invert)
1502                                 ip.invflags |= IPT_INV_DSTIP;
1503
1504                         break;
1505
1506                 case 'i':
1507                         in_len = strlen(optarg);
1508
1509                         if (in_len + 1 > IFNAMSIZ)
1510                                 break;
1511
1512                         strcpy(ip.iniface, optarg);
1513                         memset(ip.iniface_mask, 0xff, in_len + 1);
1514
1515                         if (invert)
1516                                 ip.invflags |= IPT_INV_VIA_IN;
1517
1518                         break;
1519
1520                 case 'j':
1521                         target_name = optarg;
1522                         xt_t = prepare_target(table, target_name);
1523                         if (xt_t == NULL)
1524                                 goto out;
1525
1526                         break;
1527
1528                 case 'm':
1529                         match_name = optarg;
1530                         xt_m = prepare_matches(table, &xt_rm, match_name);
1531                         if (xt_m == NULL)
1532                                 goto out;
1533
1534                         break;
1535
1536                 case 'o':
1537                         out_len = strlen(optarg);
1538
1539                         if (out_len + 1 > IFNAMSIZ)
1540                                 break;
1541
1542                         strcpy(ip.outiface, optarg);
1543                         memset(ip.outiface_mask, 0xff, out_len + 1);
1544
1545                         if (invert)
1546                                 ip.invflags |= IPT_INV_VIA_OUT;
1547
1548                         break;
1549
1550                 case 's':
1551                         if (!inet_pton(AF_INET, optarg, &src))
1552                                 break;
1553
1554                         ip.src = src;
1555                         inet_pton(AF_INET, "255.255.255.255", &ip.smsk);
1556
1557                         if (invert)
1558                                 ip.invflags |= IPT_INV_SRCIP;
1559
1560                         break;
1561
1562                 case 't':
1563                         table_name = optarg;
1564
1565                         table = connman_iptables_init(table_name);
1566                         if (table == NULL)
1567                                 return -1;
1568
1569                         break;
1570
1571                 case 1:
1572                         if (optarg[0] == '!' && optarg[1] == '\0') {
1573                                 if (invert)
1574                                         printf("Consecutive ! not allowed\n");
1575
1576                                 invert = TRUE;
1577                                 optarg[0] = '\0';
1578                                 continue;
1579                         }
1580
1581                         printf("Invalid option\n");
1582
1583                         return -1;
1584
1585                 default:
1586 #if XTABLES_VERSION_CODE > 5
1587                         if (xt_t != NULL && (xt_t->x6_parse != NULL ||
1588                                                 xt_t->parse != NULL) &&
1589                                         (c >= (int) xt_t->option_offset &&
1590                                         c < (int) xt_t->option_offset +
1591                                         XT_OPTION_OFFSET_SCALE)) {
1592                                 xtables_option_tpcall(c, argv,
1593                                                         invert, xt_t, NULL);
1594
1595                                 break;
1596                         }
1597
1598                         for (tmp_xt_rm = xt_rm; tmp_xt_rm != NULL;
1599                                                 tmp_xt_rm = tmp_xt_rm->next) {
1600                                 xt_m_t = tmp_xt_rm->match;
1601
1602                                 if (tmp_xt_rm->completed ||
1603                                                 (xt_m_t->x6_parse == NULL &&
1604                                                  xt_m_t->parse == NULL))
1605                                         continue;
1606
1607                                 if (c < (int) xt_m_t->option_offset ||
1608                                         c >= (int) xt_m_t->option_offset
1609                                         + XT_OPTION_OFFSET_SCALE)
1610                                         continue;
1611
1612                                 xtables_option_mpcall(c, argv,
1613                                                         invert, xt_m_t, NULL);
1614
1615                                 break;
1616                         }
1617 #else
1618                         if (xt_t == NULL || xt_t->parse == NULL ||
1619                                 !xt_t->parse(c - xt_t->option_offset,
1620                                 argv, invert, &xt_t->tflags, NULL, &xt_t->t)) {
1621
1622                                 for (tmp_xt_rm = xt_rm; tmp_xt_rm != NULL;
1623                                                 tmp_xt_rm = tmp_xt_rm->next) {
1624                                         xt_m_t = tmp_xt_rm->match;
1625
1626                                         if (tmp_xt_rm->completed ||
1627                                                         xt_m_t->parse == NULL)
1628                                                 continue;
1629
1630                                         if (xt_m->parse(c - xt_m->option_offset,
1631                                                 argv, invert, &xt_m->mflags,
1632                                                 NULL, &xt_m->m))
1633                                                 break;
1634                                 }
1635                         }
1636 #endif
1637                         break;
1638                 }
1639
1640                 invert = FALSE;
1641         }
1642
1643 #if XTABLES_VERSION_CODE > 5
1644         for (tmp_xt_rm = xt_rm; tmp_xt_rm != NULL;
1645                                 tmp_xt_rm = tmp_xt_rm->next)
1646                 xtables_option_mfcall(tmp_xt_rm->match);
1647
1648         if (xt_t != NULL)
1649                 xtables_option_tfcall(xt_t);
1650 #else
1651         for (tmp_xt_rm = xt_rm; tmp_xt_rm != NULL;
1652                                 tmp_xt_rm = tmp_xt_rm->next)
1653                 if (tmp_xt_rm->match->final_check != NULL)
1654                         tmp_xt_rm->match->final_check(
1655                                         tmp_xt_rm->match->mflags);
1656
1657         if (xt_t != NULL && xt_t->final_check != NULL)
1658                 xt_t->final_check(xt_t->tflags);
1659 #endif
1660
1661         if (table == NULL) {
1662                 table_name = "filter";
1663
1664                 table = connman_iptables_init(table_name);
1665                 if (table == NULL)
1666                         return -1;
1667         }
1668
1669         if (delete) {
1670                 if (delete_chain == NULL)
1671                         goto out;
1672
1673                 printf("Delete chain %s\n", delete_chain);
1674
1675                 connman_iptables_delete_chain(table, delete_chain);
1676
1677                 goto commit;
1678         }
1679
1680         if (flush_chain) {
1681                 printf("Flush chain %s\n", flush_chain);
1682
1683                 connman_iptables_flush_chain(table, flush_chain);
1684
1685                 goto commit;
1686         }
1687
1688         if (dump) {
1689                 connman_iptables_dump(table);
1690
1691                 return 0;
1692         }
1693
1694         if (chain && new_chain)
1695                 return -1;
1696
1697         if (new_chain) {
1698                 printf("New chain %s\n", new_chain);
1699
1700                 connman_iptables_add_chain(table, new_chain);
1701
1702                 goto commit;
1703         }
1704
1705         if (chain) {
1706                 if (policy != NULL) {
1707                         printf("Changing policy of %s to %s\n", chain, policy);
1708
1709                         connman_iptables_change_policy(table, chain, policy);
1710
1711                         goto commit;
1712                 }
1713
1714                 if (delete_rule == TRUE) {
1715                         printf("Deleting %s to %s (match %s)\n", target_name,
1716                                         chain, match_name);
1717
1718                         connman_iptables_delete_rule(table, &ip, chain,
1719                                         target_name, xt_t, xt_m, xt_rm);
1720
1721                         goto commit;
1722                 }
1723
1724                 if (insert == TRUE) {
1725                         printf("Inserting %s to %s (match %s)\n", target_name,
1726                                         chain, match_name);
1727
1728                         connman_iptables_insert_rule(table, &ip, chain,
1729                                                 target_name, xt_t, xt_rm);
1730                 } else {
1731                         printf("Appending %s to %s (match %s)\n", target_name,
1732                                         chain, match_name);
1733
1734                         connman_iptables_append_rule(table, &ip, chain,
1735                                                 target_name, xt_t, xt_rm);
1736                 }
1737         }
1738
1739 commit:
1740
1741         connman_iptables_commit(table);
1742
1743 out:
1744         connman_iptables_cleanup(table);
1745
1746         if (xt_t)
1747                 g_free(xt_t->t);
1748
1749         if (xt_m)
1750                 g_free(xt_m->m);
1751
1752         return 0;
1753 }