iptables: Add extensions options parsing for target
[platform/upstream/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 #define XT_OPTION_OFFSET_SCALE 256
54
55 /* fn returns 0 to continue iteration */
56 #define _XT_ENTRY_ITERATE_CONTINUE(type, entries, size, n, fn, args...) \
57 ({                                                              \
58         unsigned int __i;                                       \
59         int __n;                                                \
60         int __ret = 0;                                          \
61         type *__entry;                                          \
62                                                                 \
63         for (__i = 0, __n = 0; __i < (size);                    \
64              __i += __entry->next_offset, __n++) {              \
65                 __entry = (void *)(entries) + __i;              \
66                 if (__n < n)                                    \
67                         continue;                               \
68                                                                 \
69                 __ret = fn(__entry,  ## args);                  \
70                 if (__ret != 0)                                 \
71                         break;                                  \
72         }                                                       \
73         __ret;                                                  \
74 })
75
76 /* fn returns 0 to continue iteration */
77 #define _XT_ENTRY_ITERATE(type, entries, size, fn, args...) \
78         _XT_ENTRY_ITERATE_CONTINUE(type, entries, size, 0, fn, args)
79
80 #define ENTRY_ITERATE(entries, size, fn, args...) \
81         _XT_ENTRY_ITERATE(struct ipt_entry, entries, size, fn, ## args)
82
83 #define MIN_ALIGN (__alignof__(struct ipt_entry))
84
85 #define ALIGN(s) (((s) + ((MIN_ALIGN)-1)) & ~((MIN_ALIGN)-1))
86
87 struct error_target {
88         struct xt_entry_target t;
89         char error[IPT_TABLE_MAXNAMELEN];
90 };
91
92 struct connman_iptables_entry {
93         int offset;
94         int builtin;
95
96         struct ipt_entry *entry;
97 };
98
99 struct connman_iptables {
100         int ipt_sock;
101
102         struct ipt_getinfo *info;
103         struct ipt_get_entries *blob_entries;
104
105         unsigned int num_entries;
106         unsigned int old_entries;
107         unsigned int size;
108
109         unsigned int underflow[NF_INET_NUMHOOKS];
110         unsigned int hook_entry[NF_INET_NUMHOOKS];
111
112         GList *entries;
113 };
114
115 static GHashTable *table_hash = NULL;
116
117 static struct ipt_entry *get_entry(struct connman_iptables *table,
118                                         unsigned int offset)
119 {
120         return (struct ipt_entry *)((char *)table->blob_entries->entrytable +
121                                                                         offset);
122 }
123
124 static int is_hook_entry(struct connman_iptables *table,
125                                 struct ipt_entry *entry)
126 {
127         unsigned int i;
128
129         for (i = 0; i < NF_INET_NUMHOOKS; i++) {
130                 if ((table->info->valid_hooks & (1 << i))
131                 && get_entry(table, table->info->hook_entry[i]) == entry)
132                         return i;
133         }
134
135         return -1;
136 }
137
138 static unsigned long entry_to_offset(struct connman_iptables *table,
139                                         struct ipt_entry *entry)
140 {
141         return (void *)entry - (void *)table->blob_entries->entrytable;
142 }
143
144 static int target_to_verdict(char *target_name)
145 {
146         if (!strcmp(target_name, LABEL_ACCEPT))
147                 return -NF_ACCEPT - 1;
148
149         if (!strcmp(target_name, LABEL_DROP))
150                 return -NF_DROP - 1;
151
152         if (!strcmp(target_name, LABEL_QUEUE))
153                 return -NF_QUEUE - 1;
154
155         if (!strcmp(target_name, LABEL_RETURN))
156                 return XT_RETURN;
157
158         return 0;
159 }
160
161 static gboolean is_builtin_target(char *target_name)
162 {
163         if (!strcmp(target_name, LABEL_ACCEPT) ||
164                 !strcmp(target_name, LABEL_DROP) ||
165                 !strcmp(target_name, LABEL_QUEUE) ||
166                 !strcmp(target_name, LABEL_RETURN))
167                 return TRUE;
168
169         return FALSE;
170 }
171
172 static gboolean is_jump(struct connman_iptables_entry *e)
173 {
174         struct xt_entry_target *target;
175
176         target = ipt_get_target(e->entry);
177
178         if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
179                 struct xt_standard_target *t;
180
181                 t = (struct xt_standard_target *)target;
182
183                 switch (t->verdict) {
184                 case XT_RETURN:
185                 case -NF_ACCEPT - 1:
186                 case -NF_DROP - 1:
187                 case -NF_QUEUE - 1:
188                 case -NF_STOP - 1:
189                         return false;
190
191                 default:
192                         return true;
193                 }
194         }
195
196         return false;
197 }
198
199 static gboolean is_chain(struct connman_iptables *table,
200                                 struct connman_iptables_entry *e)
201 {
202         struct ipt_entry *entry;
203         struct xt_entry_target *target;
204
205         entry = e->entry;
206         if (e->builtin >= 0)
207                 return TRUE;
208
209         target = ipt_get_target(entry);
210         if (!strcmp(target->u.user.name, IPT_ERROR_TARGET))
211                 return TRUE;
212
213         return FALSE;
214 }
215
216 static GList *find_chain_head(struct connman_iptables *table,
217                                 char *chain_name)
218 {
219         GList *list;
220         struct connman_iptables_entry *head;
221         struct ipt_entry *entry;
222         struct xt_entry_target *target;
223         int builtin;
224
225         for (list = table->entries; list; list = list->next) {
226                 head = list->data;
227                 entry = head->entry;
228
229                 /* Buit-in chain */
230                 builtin = head->builtin;
231                 if (builtin >= 0 && !strcmp(hooknames[builtin], chain_name))
232                         break;
233
234                 /* User defined chain */
235                 target = ipt_get_target(entry);
236                 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET) &&
237                     !strcmp((char *)target->data, chain_name))
238                         break;
239         }
240
241         return list;
242 }
243
244 static GList *find_chain_tail(struct connman_iptables *table,
245                                 char *chain_name)
246 {
247         struct connman_iptables_entry *tail;
248         GList *chain_head, *list;
249
250         chain_head = find_chain_head(table, chain_name);
251         if (chain_head == NULL)
252                 return NULL;
253
254         /* Then we look for the next chain */
255         for (list = chain_head->next; list; list = list->next) {
256                 tail = list->data;
257
258                 if (is_chain(table, tail))
259                         return list;
260         }
261
262         /* Nothing found, we return the table end */
263         return g_list_last(table->entries);
264 }
265
266
267 static void update_offsets(struct connman_iptables *table)
268 {
269         GList *list, *prev;
270         struct connman_iptables_entry *entry, *prev_entry;
271
272         for (list = table->entries; list; list = list->next) {
273                 entry = list->data;
274
275                 if (list == table->entries) {
276                         entry->offset = 0;
277
278                         continue;
279                 }
280
281                 prev = list->prev;
282                 prev_entry = prev->data;
283
284                 entry->offset = prev_entry->offset +
285                                         prev_entry->entry->next_offset;
286         }
287 }
288
289 static void update_targets_reference(struct connman_iptables *table,
290                                 struct connman_iptables_entry *entry_before,
291                                 struct connman_iptables_entry *modified_entry,
292                                 gboolean is_removing)
293 {
294         struct connman_iptables_entry *tmp;
295         struct xt_standard_target *t;
296         GList *list;
297         int offset;
298
299         offset = modified_entry->entry->next_offset;
300
301         for (list = table->entries; list; list = list->next) {
302                 tmp = list->data;
303
304                 if (!is_jump(tmp))
305                         continue;
306
307                 t = (struct xt_standard_target *)ipt_get_target(tmp->entry);
308
309                 if (is_removing == TRUE) {
310                         if (t->verdict >= entry_before->offset)
311                                 t->verdict -= offset;
312                 } else {
313                         if (t->verdict > entry_before->offset)
314                                 t->verdict += offset;
315                 }
316         }
317 }
318
319 static int iptables_add_entry(struct connman_iptables *table,
320                                 struct ipt_entry *entry, GList *before,
321                                         int builtin)
322 {
323         struct connman_iptables_entry *e, *entry_before;
324
325         if (table == NULL)
326                 return -1;
327
328         e = g_try_malloc0(sizeof(struct connman_iptables_entry));
329         if (e == NULL)
330                 return -1;
331
332         e->entry = entry;
333         e->builtin = builtin;
334
335         table->entries = g_list_insert_before(table->entries, before, e);
336         table->num_entries++;
337         table->size += entry->next_offset;
338
339         if (before == NULL) {
340                 e->offset = table->size - entry->next_offset;
341
342                 return 0;
343         }
344
345         entry_before = before->data;
346
347         /*
348          * We've just appended/insterted a new entry. All references
349          * should be bumped accordingly.
350          */
351         update_targets_reference(table, entry_before, e, FALSE);
352
353         update_offsets(table);
354
355         return 0;
356 }
357
358 static int remove_table_entry(struct connman_iptables *table,
359                                 struct connman_iptables_entry *entry)
360 {
361         int removed = 0;
362
363         table->num_entries--;
364         table->size -= entry->entry->next_offset;
365         removed = entry->entry->next_offset;
366
367         g_free(entry->entry);
368
369         table->entries = g_list_remove(table->entries, entry);
370
371         return removed;
372 }
373
374 static int iptables_flush_chain(struct connman_iptables *table,
375                                                 char *name)
376 {
377         GList *chain_head, *chain_tail, *list, *next;
378         struct connman_iptables_entry *entry;
379         int builtin, removed = 0;
380
381         chain_head = find_chain_head(table, name);
382         if (chain_head == NULL)
383                 return -EINVAL;
384
385         chain_tail = find_chain_tail(table, name);
386         if (chain_tail == NULL)
387                 return -EINVAL;
388
389         entry = chain_head->data;
390         builtin = entry->builtin;
391
392         if (builtin >= 0)
393                 list = chain_head;
394         else
395                 list = chain_head->next;
396
397         if (list == chain_tail->prev)
398                 return 0;
399
400         while (list != chain_tail->prev) {
401                 entry = list->data;
402                 next = g_list_next(list);
403
404                 removed += remove_table_entry(table, entry);
405
406                 list = next;
407         }
408
409         if (builtin >= 0) {
410                 struct connman_iptables_entry *e;
411
412                 entry = list->data;
413
414                 entry->builtin = builtin;
415
416                 table->underflow[builtin] -= removed;
417
418                 for (list = chain_tail; list; list = list->next) {
419                         e = list->data;
420
421                         builtin = e->builtin;
422                         if (builtin < 0)
423                                 continue;
424
425                         table->hook_entry[builtin] -= removed;
426                         table->underflow[builtin] -= removed;
427                 }
428         }
429
430         update_offsets(table);
431
432         return 0;
433 }
434
435 static int iptables_add_chain(struct connman_iptables *table,
436                                         char *name)
437 {
438         GList *last;
439         struct ipt_entry *entry_head;
440         struct ipt_entry *entry_return;
441         struct error_target *error;
442         struct ipt_standard_target *standard;
443         u_int16_t entry_head_size, entry_return_size;
444
445         last = g_list_last(table->entries);
446
447         /*
448          * An empty chain is composed of:
449          * - A head entry, with no match and an error target.
450          *   The error target data is the chain name.
451          * - A tail entry, with no match and a standard target.
452          *   The standard target verdict is XT_RETURN (return to the
453          *   caller).
454          */
455
456         /* head entry */
457         entry_head_size = sizeof(struct ipt_entry) +
458                                 sizeof(struct error_target);
459         entry_head = g_try_malloc0(entry_head_size);
460         if (entry_head == NULL)
461                 goto err_head;
462
463         memset(entry_head, 0, entry_head_size);
464
465         entry_head->target_offset = sizeof(struct ipt_entry);
466         entry_head->next_offset = entry_head_size;
467
468         error = (struct error_target *) entry_head->elems;
469         strcpy(error->t.u.user.name, IPT_ERROR_TARGET);
470         error->t.u.user.target_size = ALIGN(sizeof(struct error_target));
471         strcpy(error->error, name);
472
473         if (iptables_add_entry(table, entry_head, last, -1) < 0)
474                 goto err_head;
475
476         /* tail entry */
477         entry_return_size = sizeof(struct ipt_entry) +
478                                 sizeof(struct ipt_standard_target);
479         entry_return = g_try_malloc0(entry_return_size);
480         if (entry_return == NULL)
481                 goto err;
482
483         memset(entry_return, 0, entry_return_size);
484
485         entry_return->target_offset = sizeof(struct ipt_entry);
486         entry_return->next_offset = entry_return_size;
487
488         standard = (struct ipt_standard_target *) entry_return->elems;
489         standard->target.u.user.target_size =
490                                 ALIGN(sizeof(struct ipt_standard_target));
491         standard->verdict = XT_RETURN;
492
493         if (iptables_add_entry(table, entry_return, last, -1) < 0)
494                 goto err;
495
496         return 0;
497
498 err:
499         g_free(entry_return);
500 err_head:
501         g_free(entry_head);
502
503         return -ENOMEM;
504 }
505
506 static int iptables_delete_chain(struct connman_iptables *table, char *name)
507 {
508         struct connman_iptables_entry *entry;
509         GList *chain_head, *chain_tail;
510
511         chain_head = find_chain_head(table, name);
512         if (chain_head == NULL)
513                 return -EINVAL;
514
515         entry = chain_head->data;
516
517         /* We cannot remove builtin chain */
518         if (entry->builtin >= 0)
519                 return -EINVAL;
520
521         chain_tail = find_chain_tail(table, name);
522         if (chain_tail == NULL)
523                 return -EINVAL;
524
525         /* Chain must be flushed */
526         if (chain_head->next != chain_tail->prev)
527                 return -EINVAL;
528
529         remove_table_entry(table, entry);
530
531         entry = chain_tail->prev->data;
532         remove_table_entry(table, entry);
533
534         update_offsets(table);
535
536         return 0;
537 }
538
539 static struct ipt_entry *new_rule(struct ipt_ip *ip,
540                 char *target_name, struct xtables_target *xt_t,
541                 char *match_name, struct xtables_match *xt_m)
542 {
543         struct ipt_entry *new_entry;
544         size_t match_size, target_size;
545
546         if (xt_m)
547                 match_size = xt_m->m->u.match_size;
548         else
549                 match_size = 0;
550
551         if (xt_t)
552                 target_size = ALIGN(xt_t->t->u.target_size);
553         else
554                 target_size = ALIGN(sizeof(struct xt_standard_target));
555
556         new_entry = g_try_malloc0(sizeof(struct ipt_entry) + target_size +
557                                                                 match_size);
558         if (new_entry == NULL)
559                 return NULL;
560
561         memcpy(&new_entry->ip, ip, sizeof(struct ipt_ip));
562
563         new_entry->target_offset = sizeof(struct ipt_entry) + match_size;
564         new_entry->next_offset = sizeof(struct ipt_entry) + target_size +
565                                                                 match_size;
566         if (xt_m) {
567                 struct xt_entry_match *entry_match;
568
569                 entry_match = (struct xt_entry_match *)new_entry->elems;
570                 memcpy(entry_match, xt_m->m, match_size);
571         }
572
573         if (xt_t) {
574                 struct xt_entry_target *entry_target;
575
576                 entry_target = ipt_get_target(new_entry);
577                 memcpy(entry_target, xt_t->t, target_size);
578         }
579
580         return new_entry;
581 }
582
583 static void update_hooks(struct connman_iptables *table, GList *chain_head,
584                                 struct ipt_entry *entry)
585 {
586         GList *list;
587         struct connman_iptables_entry *head, *e;
588         int builtin;
589
590         if (chain_head == NULL)
591                 return;
592
593         head = chain_head->data;
594
595         builtin = head->builtin;
596         if (builtin < 0)
597                 return;
598
599         table->underflow[builtin] += entry->next_offset;
600
601         for (list = chain_head->next; list; list = list->next) {
602                 e = list->data;
603
604                 builtin = e->builtin;
605                 if (builtin < 0)
606                         continue;
607
608                 table->hook_entry[builtin] += entry->next_offset;
609                 table->underflow[builtin] += entry->next_offset;
610         }
611 }
612
613 static struct ipt_entry *prepare_rule_inclusion(struct connman_iptables *table,
614                                 struct ipt_ip *ip, char *chain_name,
615                                 char *target_name, struct xtables_target *xt_t,
616                                 char *match_name, struct xtables_match *xt_m,
617                                 int *builtin)
618 {
619         GList *chain_tail, *chain_head;
620         struct ipt_entry *new_entry;
621         struct connman_iptables_entry *head;
622
623         chain_head = find_chain_head(table, chain_name);
624         if (chain_head == NULL)
625                 return NULL;
626
627         chain_tail = find_chain_tail(table, chain_name);
628         if (chain_tail == NULL)
629                 return NULL;
630
631         new_entry = new_rule(ip, target_name, xt_t, match_name, xt_m);
632         if (new_entry == NULL)
633                 return NULL;
634
635         update_hooks(table, chain_head, new_entry);
636
637         /*
638          * If the chain is builtin, and does not have any rule,
639          * then the one that we're inserting is becoming the head
640          * and thus needs the builtin flag.
641          */
642         head = chain_head->data;
643         if (head->builtin < 0)
644                 *builtin = -1;
645         else if (chain_head == chain_tail->prev) {
646                 *builtin = head->builtin;
647                 head->builtin = -1;
648         }
649
650         return new_entry;
651 }
652
653 static int iptables_append_rule(struct connman_iptables *table,
654                                 struct ipt_ip *ip, char *chain_name,
655                                 char *target_name, struct xtables_target *xt_t,
656                                 char *match_name, struct xtables_match *xt_m)
657 {
658         GList *chain_tail;
659         struct ipt_entry *new_entry;
660         int builtin = -1, ret;
661
662         DBG("");
663
664         chain_tail = find_chain_tail(table, chain_name);
665         if (chain_tail == NULL)
666                 return -EINVAL;
667
668         new_entry = prepare_rule_inclusion(table, ip, chain_name,
669                         target_name, xt_t, match_name, xt_m, &builtin);
670         if (new_entry == NULL)
671                 return -EINVAL;
672
673         ret = iptables_add_entry(table, new_entry, chain_tail->prev, builtin);
674         if (ret < 0)
675                 g_free(new_entry);
676
677         return ret;
678 }
679
680 static int iptables_insert_rule(struct connman_iptables *table,
681                                 struct ipt_ip *ip, char *chain_name,
682                                 char *target_name, struct xtables_target *xt_t,
683                                 char *match_name, struct xtables_match *xt_m)
684 {
685         struct ipt_entry *new_entry;
686         int builtin = -1, ret;
687         GList *chain_head;
688
689         chain_head = find_chain_head(table, chain_name);
690         if (chain_head == NULL)
691                 return -EINVAL;
692
693         new_entry = prepare_rule_inclusion(table, ip, chain_name,
694                         target_name, xt_t, match_name, xt_m, &builtin);
695         if (new_entry == NULL)
696                 return -EINVAL;
697
698         ret = iptables_add_entry(table, new_entry, chain_head->next, builtin);
699         if (ret < 0)
700                 g_free(new_entry);
701
702         return ret;
703 }
704
705 static gboolean is_same_ipt_entry(struct ipt_entry *i_e1,
706                                         struct ipt_entry *i_e2)
707 {
708         if (memcmp(&i_e1->ip, &i_e2->ip, sizeof(struct ipt_ip)) != 0)
709                 return FALSE;
710
711         if (i_e1->target_offset != i_e2->target_offset)
712                 return FALSE;
713
714         if (i_e1->next_offset != i_e2->next_offset)
715                 return FALSE;
716
717         return TRUE;
718 }
719
720 static gboolean is_same_target(struct xt_entry_target *xt_e_t1,
721                                         struct xt_entry_target *xt_e_t2)
722 {
723         if (xt_e_t1 == NULL || xt_e_t2 == NULL)
724                 return FALSE;
725
726         if (strcmp(xt_e_t1->u.user.name, IPT_STANDARD_TARGET) == 0) {
727                 struct xt_standard_target *xt_s_t1;
728                 struct xt_standard_target *xt_s_t2;
729
730                 xt_s_t1 = (struct xt_standard_target *) xt_e_t1;
731                 xt_s_t2 = (struct xt_standard_target *) xt_e_t2;
732
733                 if (xt_s_t1->verdict != xt_s_t2->verdict)
734                         return FALSE;
735         } else {
736                 if (xt_e_t1->u.target_size != xt_e_t2->u.target_size)
737                         return FALSE;
738
739                 if (strcmp(xt_e_t1->u.user.name, xt_e_t2->u.user.name) != 0)
740                         return FALSE;
741         }
742
743         return TRUE;
744 }
745
746 static gboolean is_same_match(struct xt_entry_match *xt_e_m1,
747                                 struct xt_entry_match *xt_e_m2)
748 {
749         if (xt_e_m1 == NULL || xt_e_m2 == NULL)
750                 return FALSE;
751
752         if (xt_e_m1->u.match_size != xt_e_m2->u.match_size)
753                 return FALSE;
754
755         if (xt_e_m1->u.user.revision != xt_e_m2->u.user.revision)
756                 return FALSE;
757
758         if (strcmp(xt_e_m1->u.user.name, xt_e_m2->u.user.name) != 0)
759                 return FALSE;
760
761         return TRUE;
762 }
763
764 static int iptables_delete_rule(struct connman_iptables *table,
765                                 struct ipt_ip *ip, char *chain_name,
766                                 char *target_name, struct xtables_target *xt_t,
767                                 char *match_name, struct xtables_match *xt_m)
768 {
769         GList *chain_tail, *chain_head, *list;
770         struct xt_entry_target *xt_e_t = NULL;
771         struct xt_entry_match *xt_e_m = NULL;
772         struct connman_iptables_entry *entry;
773         struct ipt_entry *entry_test;
774         int builtin, removed;
775
776         removed = 0;
777
778         chain_head = find_chain_head(table, chain_name);
779         if (chain_head == NULL)
780                 return -EINVAL;
781
782         chain_tail = find_chain_tail(table, chain_name);
783         if (chain_tail == NULL)
784                 return -EINVAL;
785
786         if (!xt_t && !xt_m)
787                 return -EINVAL;
788
789         entry_test = new_rule(ip, target_name, xt_t, match_name, xt_m);
790         if (entry_test == NULL)
791                 return -EINVAL;
792
793         if (xt_t != NULL)
794                 xt_e_t = ipt_get_target(entry_test);
795         if (xt_m != NULL)
796                 xt_e_m = (struct xt_entry_match *)entry_test->elems;
797
798         entry = chain_head->data;
799         builtin = entry->builtin;
800
801         if (builtin >= 0)
802                 list = chain_head;
803         else
804                 list = chain_head->next;
805
806         for (entry = NULL; list != chain_tail->prev; list = list->next) {
807                 struct connman_iptables_entry *tmp;
808                 struct ipt_entry *tmp_e;
809
810                 tmp = list->data;
811                 tmp_e = tmp->entry;
812
813                 if (is_same_ipt_entry(entry_test, tmp_e) == FALSE)
814                         continue;
815
816                 if (xt_t != NULL) {
817                         struct xt_entry_target *tmp_xt_e_t;
818
819                         tmp_xt_e_t = ipt_get_target(tmp_e);
820
821                         if (!is_same_target(tmp_xt_e_t, xt_e_t))
822                                 continue;
823                 }
824
825                 if (xt_m != NULL) {
826                         struct xt_entry_match *tmp_xt_e_m;
827
828                         tmp_xt_e_m = (struct xt_entry_match *)tmp_e->elems;
829
830                         if (!is_same_match(tmp_xt_e_m, xt_e_m))
831                                 continue;
832                 }
833
834                 entry = tmp;
835                 break;
836         }
837
838         if (entry == NULL) {
839                 g_free(entry_test);
840                 return -EINVAL;
841         }
842
843         /* We have deleted a rule,
844          * all references should be bumped accordingly */
845         if (list->next != NULL)
846                 update_targets_reference(table, list->next->data,
847                                                 list->data, TRUE);
848
849         removed += remove_table_entry(table, entry);
850
851         if (builtin >= 0) {
852                 list = list->next;
853                 if (list) {
854                         entry = list->data;
855                         entry->builtin = builtin;
856                 }
857
858                 table->underflow[builtin] -= removed;
859                 for (list = chain_tail; list; list = list->next) {
860                         entry = list->data;
861
862                         builtin = entry->builtin;
863                         if (builtin < 0)
864                                 continue;
865
866                         table->hook_entry[builtin] -= removed;
867                         table->underflow[builtin] -= removed;
868                 }
869         }
870
871         update_offsets(table);
872
873         return 0;
874 }
875
876 static struct ipt_replace *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 | SOCK_CLOEXEC, 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 option iptables_opts[] = {
1219         {.name = "append",        .has_arg = 1, .val = 'A'},
1220         {.name = "delete",        .has_arg = 1, .val = 'D'},
1221         {.name = "flush-chain",   .has_arg = 1, .val = 'F'},
1222         {.name = "insert",        .has_arg = 1, .val = 'I'},
1223         {.name = "list",          .has_arg = 2, .val = 'L'},
1224         {.name = "new-chain",     .has_arg = 1, .val = 'N'},
1225         {.name = "delete-chain",  .has_arg = 1, .val = 'X'},
1226         {.name = "destination",   .has_arg = 1, .val = 'd'},
1227         {.name = "in-interface",  .has_arg = 1, .val = 'i'},
1228         {.name = "jump",          .has_arg = 1, .val = 'j'},
1229         {.name = "match",         .has_arg = 1, .val = 'm'},
1230         {.name = "out-interface", .has_arg = 1, .val = 'o'},
1231         {.name = "source",        .has_arg = 1, .val = 's'},
1232         {.name = "table",         .has_arg = 1, .val = 't'},
1233         {NULL},
1234 };
1235
1236 struct xtables_globals iptables_globals = {
1237         .option_offset = 0,
1238         .opts = iptables_opts,
1239         .orig_opts = iptables_opts,
1240 };
1241
1242 static struct xtables_target *prepare_target(struct connman_iptables *table,
1243                                                         char *target_name)
1244 {
1245         struct xtables_target *xt_t = NULL;
1246         gboolean is_builtin, is_user_defined;
1247         GList *chain_head = NULL;
1248         size_t target_size;
1249
1250         is_builtin = FALSE;
1251         is_user_defined = FALSE;
1252
1253         if (is_builtin_target(target_name))
1254                 is_builtin = TRUE;
1255         else {
1256                 chain_head = find_chain_head(table, target_name);
1257                 if (chain_head != NULL && chain_head->next != NULL)
1258                         is_user_defined = TRUE;
1259         }
1260
1261         if (is_builtin || is_user_defined)
1262                 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
1263                                                 XTF_LOAD_MUST_SUCCEED);
1264         else
1265                 xt_t = xtables_find_target(target_name, XTF_TRY_LOAD);
1266
1267         if (xt_t == NULL)
1268                 return NULL;
1269
1270         target_size = ALIGN(sizeof(struct ipt_entry_target)) + xt_t->size;
1271
1272         xt_t->t = g_try_malloc0(target_size);
1273         if (xt_t->t == NULL)
1274                 return NULL;
1275
1276         xt_t->t->u.target_size = target_size;
1277
1278         if (is_builtin || is_user_defined) {
1279                 struct xt_standard_target *target;
1280
1281                 target = (struct xt_standard_target *)(xt_t->t);
1282                 strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
1283
1284                 if (is_builtin == TRUE)
1285                         target->verdict = target_to_verdict(target_name);
1286                 else if (is_user_defined == TRUE) {
1287                         struct connman_iptables_entry *target_rule;
1288
1289                         if (chain_head == NULL) {
1290                                 g_free(xt_t->t);
1291                                 return NULL;
1292                         }
1293
1294                         target_rule = chain_head->next->data;
1295                         target->verdict = target_rule->offset;
1296                 }
1297         } else {
1298                 strcpy(xt_t->t->u.user.name, target_name);
1299                 xt_t->t->u.user.revision = xt_t->revision;
1300                 if (xt_t->init != NULL)
1301                         xt_t->init(xt_t->t);
1302         }
1303
1304         if (xt_t->x6_options != NULL)
1305                 iptables_globals.opts =
1306                         xtables_options_xfrm(
1307 #if XTABLES_VERSION_CODE > 5
1308                                 iptables_globals.orig_opts,
1309 #endif
1310                                 iptables_globals.opts,
1311                                 xt_t->x6_options,
1312                                 &xt_t->option_offset);
1313         else
1314                 iptables_globals.opts =
1315                         xtables_merge_options(
1316 #if XTABLES_VERSION_CODE > 5
1317                                 iptables_globals.orig_opts,
1318 #endif
1319                                 iptables_globals.opts,
1320                                 xt_t->extra_opts,
1321                                 &xt_t->option_offset);
1322
1323         if (iptables_globals.opts == NULL) {
1324                 g_free(xt_t->t);
1325                 xt_t = NULL;
1326         }
1327
1328         return xt_t;
1329 }
1330
1331 static struct xtables_match *prepare_matches(struct connman_iptables *table,
1332                         struct xtables_rule_match **xt_rm, char *match_name)
1333 {
1334         struct xtables_match *xt_m;
1335         size_t match_size;
1336
1337         if (match_name == NULL)
1338                 return NULL;
1339
1340         xt_m = xtables_find_match(match_name, XTF_LOAD_MUST_SUCCEED, xt_rm);
1341         match_size = ALIGN(sizeof(struct ipt_entry_match)) + xt_m->size;
1342
1343         xt_m->m = g_try_malloc0(match_size);
1344         if (xt_m->m == NULL)
1345                 return NULL;
1346
1347         xt_m->m->u.match_size = match_size;
1348         strcpy(xt_m->m->u.user.name, xt_m->name);
1349         xt_m->m->u.user.revision = xt_m->revision;
1350
1351         if (xt_m->init != NULL)
1352                 xt_m->init(xt_m->m);
1353
1354         if (xt_m == xt_m->next)
1355                 goto done;
1356
1357         if (xt_m->x6_options != NULL)
1358                 iptables_globals.opts =
1359                         xtables_options_xfrm(
1360 #if XTABLES_VERSION_CODE > 5
1361                                 iptables_globals.orig_opts,
1362 #endif
1363                                 iptables_globals.opts,
1364                                 xt_m->x6_options,
1365                                 &xt_m->option_offset);
1366         else
1367                         iptables_globals.opts =
1368                         xtables_merge_options(
1369 #if XTABLES_VERSION_CODE > 5
1370                                 iptables_globals.orig_opts,
1371 #endif
1372                                 iptables_globals.opts,
1373                                 xt_m->extra_opts,
1374                                 &xt_m->option_offset);
1375
1376         if (iptables_globals.opts == NULL) {
1377                 g_free(xt_m->m);
1378                 xt_m = NULL;
1379         }
1380
1381 done:
1382         return xt_m;
1383 }
1384
1385 static int iptables_command(int argc, char *argv[])
1386 {
1387         struct connman_iptables *table;
1388         struct xtables_rule_match *xt_rm;
1389         struct xtables_match *xt_m;
1390         struct xtables_target *xt_t;
1391         struct ipt_ip ip;
1392         char *table_name, *chain, *new_chain, *match_name, *target_name;
1393         char *flush_chain, *delete_chain;
1394         int c, ret, in_len, out_len;
1395         gboolean dump, invert, insert, delete;
1396         struct in_addr src, dst;
1397
1398         if (argc == 0)
1399                 return -EINVAL;
1400
1401         dump = FALSE;
1402         invert = FALSE;
1403         insert = FALSE;
1404         delete = FALSE;
1405         table_name = chain = new_chain = match_name = target_name = NULL;
1406         flush_chain = delete_chain = NULL;
1407         memset(&ip, 0, sizeof(struct ipt_ip));
1408         table = NULL;
1409         xt_rm = NULL;
1410         xt_m = NULL;
1411         xt_t = NULL;
1412         ret = 0;
1413
1414         optind = 0;
1415
1416         while ((c = getopt_long(argc, argv, "-A:F:I:L::N:X:d:j:i:m:o:s:t:",
1417                                         iptables_globals.opts, NULL)) != -1) {
1418                 switch (c) {
1419                 case 'A':
1420                         /* It is either -A, -D or -I at once */
1421                         if (chain)
1422                                 goto out;
1423
1424                         chain = optarg;
1425                         break;
1426
1427                 case 'D':
1428                         /* It is either -A, -D or -I at once */
1429                         if (chain)
1430                                 goto out;
1431
1432                         chain = optarg;
1433                         delete = TRUE;
1434                         break;
1435
1436                 case 'F':
1437                         flush_chain = optarg;
1438                         break;
1439
1440                 case 'I':
1441                         /* It is either -A, -D or -I at once */
1442                         if (chain)
1443                                 goto out;
1444
1445                         chain = optarg;
1446                         insert = TRUE;
1447                         break;
1448
1449                 case 'L':
1450                         dump = TRUE;
1451                         break;
1452
1453                 case 'N':
1454                         new_chain = optarg;
1455                         break;
1456
1457                 case 'X':
1458                         delete_chain = optarg;
1459                         break;
1460
1461                 case 'd':
1462                         if (!inet_pton(AF_INET, optarg, &dst))
1463                                 break;
1464
1465                         ip.dst = dst;
1466                         inet_pton(AF_INET, "255.255.255.255", &ip.dmsk);
1467
1468                         if (invert)
1469                                 ip.invflags |= IPT_INV_DSTIP;
1470
1471                         break;
1472
1473                 case 'i':
1474                         in_len = strlen(optarg);
1475
1476                         if (in_len + 1 > IFNAMSIZ)
1477                                 break;
1478
1479                         strcpy(ip.iniface, optarg);
1480                         memset(ip.iniface_mask, 0xff, in_len + 1);
1481
1482                         if (invert)
1483                                 ip.invflags |= IPT_INV_VIA_IN;
1484
1485                         break;
1486
1487                 case 'j':
1488                         target_name = optarg;
1489                         xt_t = prepare_target(table, target_name);
1490                         if (xt_t == NULL)
1491                                 goto out;
1492
1493                         break;
1494
1495                 case 'm':
1496                         match_name = optarg;
1497                         xt_m = prepare_matches(table, &xt_rm, match_name);
1498                         if (xt_m == NULL)
1499                                 goto out;
1500
1501                         break;
1502
1503                 case 'o':
1504                         out_len = strlen(optarg);
1505
1506                         if (out_len + 1 > IFNAMSIZ)
1507                                 break;
1508
1509                         strcpy(ip.outiface, optarg);
1510                         memset(ip.outiface_mask, 0xff, out_len + 1);
1511
1512                         if (invert)
1513                                 ip.invflags |= IPT_INV_VIA_OUT;
1514
1515                         break;
1516
1517                 case 's':
1518                         if (!inet_pton(AF_INET, optarg, &src))
1519                                 break;
1520
1521                         ip.src = src;
1522                         inet_pton(AF_INET, "255.255.255.255", &ip.smsk);
1523
1524                         if (invert)
1525                                 ip.invflags |= IPT_INV_SRCIP;
1526
1527                         break;
1528
1529                 case 't':
1530                         table_name = optarg;
1531                         break;
1532
1533                 case 1:
1534                         if (optarg[0] == '!' && optarg[1] == '\0') {
1535                                 invert = TRUE;
1536                                 optarg[0] = '\0';
1537                                 continue;
1538                         }
1539
1540                         connman_error("Invalid option");
1541
1542                         ret = -EINVAL;
1543                         goto out;
1544
1545                 default:
1546                         if (xt_t != NULL && (xt_t->x6_parse != NULL ||
1547                                                 xt_t->parse != NULL) &&
1548                                         (c >= (int) xt_t->option_offset &&
1549                                         c < (int) xt_t->option_offset +
1550                                         XT_OPTION_OFFSET_SCALE)) {
1551                                 xtables_option_tpcall(c, argv,
1552                                                         invert, xt_t, NULL);
1553                         }
1554
1555                         break;
1556                 }
1557
1558                 invert = FALSE;
1559         }
1560
1561         if (table_name == NULL)
1562                 table_name = "filter";
1563
1564         table = iptables_init(table_name);
1565         if (table == NULL) {
1566                 ret = -EINVAL;
1567                 goto out;
1568         }
1569
1570         if (delete_chain != NULL) {
1571                 printf("Delete chain %s\n", delete_chain);
1572
1573                 iptables_delete_chain(table, delete_chain);
1574
1575                 goto out;
1576         }
1577
1578         if (dump) {
1579                 iptables_dump(table);
1580
1581                 ret = 0;
1582                 goto out;
1583         }
1584
1585         if (flush_chain) {
1586                 DBG("Flush chain %s", flush_chain);
1587
1588                 iptables_flush_chain(table, flush_chain);
1589
1590                 goto out;
1591         }
1592
1593         if (chain && new_chain) {
1594                 ret = -EINVAL;
1595                 goto out;
1596         }
1597
1598         if (new_chain) {
1599                 DBG("New chain %s", new_chain);
1600
1601                 ret = iptables_add_chain(table, new_chain);
1602                 goto out;
1603         }
1604
1605         if (chain) {
1606                 if (xt_t == NULL)
1607                         goto out;
1608
1609                 if (delete == TRUE) {
1610                         DBG("Deleting %s to %s (match %s)\n",
1611                                         target_name, chain, match_name);
1612
1613                         ret = iptables_delete_rule(table, &ip, chain,
1614                                         target_name, xt_t, match_name, xt_m);
1615
1616                         goto out;
1617                 }
1618
1619                 if (insert == TRUE) {
1620                         DBG("Inserting %s to %s (match %s)",
1621                                         target_name, chain, match_name);
1622
1623                         ret = iptables_insert_rule(table, &ip, chain,
1624                                         target_name, xt_t, match_name, xt_m);
1625
1626                         goto out;
1627                 } else {
1628                         DBG("Adding %s to %s (match %s)",
1629                                         target_name, chain, match_name);
1630
1631                         ret = iptables_append_rule(table, &ip, chain,
1632                                         target_name, xt_t, match_name, xt_m);
1633
1634                         goto out;
1635                 }
1636         }
1637
1638 out:
1639         if (xt_t)
1640                 g_free(xt_t->t);
1641
1642         if (xt_m)
1643                 g_free(xt_m->m);
1644
1645         return ret;
1646 }
1647
1648 int __connman_iptables_command(const char *format, ...)
1649 {
1650         char **argv, **arguments, *command;
1651         int argc, i, ret;
1652         va_list args;
1653
1654         if (format == NULL)
1655                 return -EINVAL;
1656
1657         va_start(args, format);
1658
1659         command = g_strdup_vprintf(format, args);
1660
1661         va_end(args);
1662
1663         if (command == NULL)
1664                 return -ENOMEM;
1665
1666         arguments = g_strsplit_set(command, " ", -1);
1667
1668         for (argc = 0; arguments[argc]; argc++);
1669         ++argc;
1670
1671         DBG("command %s argc %d", command, argc);
1672
1673         argv = g_try_malloc0(argc * sizeof(char *));
1674         if (argv == NULL) {
1675                 g_free(command);
1676                 g_strfreev(arguments);
1677                 return -ENOMEM;
1678         }
1679
1680         argv[0] = "iptables";
1681         for (i = 1; i < argc; i++)
1682                 argv[i] = arguments[i - 1];
1683
1684         ret = iptables_command(argc, argv);
1685
1686         g_free(command);
1687         g_strfreev(arguments);
1688         g_free(argv);
1689
1690         return ret;
1691 }
1692
1693
1694 int __connman_iptables_commit(const char *table_name)
1695 {
1696         struct connman_iptables *table;
1697         struct ipt_replace *repl;
1698         int err;
1699
1700         DBG("%s", table_name);
1701
1702         table = g_hash_table_lookup(table_hash, table_name);
1703         if (table == NULL)
1704                 return -EINVAL;
1705
1706         repl = iptables_blob(table);
1707
1708         err = iptables_replace(table, repl);
1709
1710         g_free(repl->counters);
1711         g_free(repl);
1712
1713         if (err < 0)
1714             return err;
1715
1716         g_hash_table_remove(table_hash, table_name);
1717
1718         return 0;
1719 }
1720
1721 static void remove_table(gpointer user_data)
1722 {
1723         struct connman_iptables *table = user_data;
1724
1725         table_cleanup(table);
1726 }
1727
1728 int __connman_iptables_init(void)
1729 {
1730         DBG("");
1731
1732         table_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1733                                                 g_free, remove_table);
1734
1735         xtables_init_all(&iptables_globals, NFPROTO_IPV4);
1736
1737         return 0;
1738
1739 }
1740
1741 void __connman_iptables_cleanup(void)
1742 {
1743         DBG("");
1744
1745         g_hash_table_destroy(table_hash);
1746
1747         xtables_free_opts(1);
1748 }