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