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