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