5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
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.
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.
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
31 #include <sys/errno.h>
32 #include <sys/socket.h>
35 #include <linux/netfilter_ipv4/ip_tables.h>
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",
48 #define LABEL_ACCEPT "ACCEPT"
49 #define LABEL_DROP "DROP"
50 #define LABEL_QUEUE "QUEUE"
51 #define LABEL_RETURN "RETURN"
53 /* fn returns 0 to continue iteration */
54 #define _XT_ENTRY_ITERATE_CONTINUE(type, entries, size, n, fn, args...) \
61 for (__i = 0, __n = 0; __i < (size); \
62 __i += __entry->next_offset, __n++) { \
63 __entry = (void *)(entries) + __i; \
67 __ret = fn(__entry, ## args); \
74 /* fn returns 0 to continue iteration */
75 #define _XT_ENTRY_ITERATE(type, entries, size, fn, args...) \
76 _XT_ENTRY_ITERATE_CONTINUE(type, entries, size, 0, fn, args)
78 #define ENTRY_ITERATE(entries, size, fn, args...) \
79 _XT_ENTRY_ITERATE(struct ipt_entry, entries, size, fn, ## args)
81 #define MIN_ALIGN (__alignof__(struct ipt_entry))
83 #define ALIGN(s) (((s) + ((MIN_ALIGN)-1)) & ~((MIN_ALIGN)-1))
85 struct ipt_error_target {
86 struct xt_entry_target t;
87 char error[IPT_TABLE_MAXNAMELEN];
90 struct connman_iptables_entry {
94 struct ipt_entry *entry;
97 struct connman_iptables {
100 struct ipt_getinfo *info;
101 struct ipt_get_entries *blob_entries;
103 unsigned int num_entries;
104 unsigned int old_entries;
107 unsigned int underflow[NF_INET_NUMHOOKS];
108 unsigned int hook_entry[NF_INET_NUMHOOKS];
113 static GHashTable *table_hash = NULL;
115 static struct ipt_entry *get_entry(struct connman_iptables *table,
118 return (struct ipt_entry *)((char *)table->blob_entries->entrytable +
122 static int is_hook_entry(struct connman_iptables *table,
123 struct ipt_entry *entry)
127 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
128 if ((table->info->valid_hooks & (1 << i))
129 && get_entry(table, table->info->hook_entry[i]) == entry)
136 static unsigned long entry_to_offset(struct connman_iptables *table,
137 struct ipt_entry *entry)
139 return (void *)entry - (void *)table->blob_entries->entrytable;
142 static int target_to_verdict(char *target_name)
144 if (!strcmp(target_name, LABEL_ACCEPT))
145 return -NF_ACCEPT - 1;
147 if (!strcmp(target_name, LABEL_DROP))
150 if (!strcmp(target_name, LABEL_QUEUE))
151 return -NF_QUEUE - 1;
153 if (!strcmp(target_name, LABEL_RETURN))
159 static gboolean is_builtin_target(char *target_name)
161 if (!strcmp(target_name, LABEL_ACCEPT) ||
162 !strcmp(target_name, LABEL_DROP) ||
163 !strcmp(target_name, LABEL_QUEUE) ||
164 !strcmp(target_name, LABEL_RETURN))
170 static gboolean is_jump(struct connman_iptables_entry *e)
172 struct xt_entry_target *target;
174 target = ipt_get_target(e->entry);
176 if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
177 struct xt_standard_target *t;
179 t = (struct xt_standard_target *)target;
181 switch (t->verdict) {
197 static gboolean is_chain(struct connman_iptables *table,
198 struct connman_iptables_entry *e)
200 struct ipt_entry *entry;
201 struct xt_entry_target *target;
207 target = ipt_get_target(entry);
208 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET))
214 static GList *find_chain_head(struct connman_iptables *table,
218 struct connman_iptables_entry *head;
219 struct ipt_entry *entry;
220 struct xt_entry_target *target;
223 for (list = table->entries; list; list = list->next) {
228 builtin = head->builtin;
229 if (builtin >= 0 && !strcmp(hooknames[builtin], chain_name))
232 /* User defined chain */
233 target = ipt_get_target(entry);
234 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET) &&
235 !strcmp((char *)target->data, chain_name))
242 static GList *find_chain_tail(struct connman_iptables *table,
245 GList *chain_head, *list;
246 struct connman_iptables_entry *head, *tail;
247 struct ipt_entry *entry;
248 struct xt_entry_target *target;
251 /* First we look for the head */
252 for (list = table->entries; list; list = list->next) {
257 builtin = head->builtin;
258 if (builtin >= 0 && !strcmp(hooknames[builtin], chain_name))
261 /* User defined chain */
262 target = ipt_get_target(entry);
263 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET) &&
264 !strcmp((char *)target->data, chain_name))
273 /* Then we look for the next chain */
274 for (list = chain_head->next; list; list = list->next) {
278 if (is_chain(table, tail))
282 /* Nothing found, we return the table end */
283 return g_list_last(table->entries);
287 static void update_offsets(struct connman_iptables *table)
290 struct connman_iptables_entry *entry, *prev_entry;
292 for (list = table->entries; list; list = list->next) {
295 if (list == table->entries) {
302 prev_entry = prev->data;
304 entry->offset = prev_entry->offset +
305 prev_entry->entry->next_offset;
309 static int iptables_add_entry(struct connman_iptables *table,
310 struct ipt_entry *entry, GList *before,
314 struct connman_iptables_entry *e, *tmp, *entry_before;
315 struct xt_standard_target *t;
320 e = g_try_malloc0(sizeof(struct connman_iptables_entry));
325 e->builtin = builtin;
327 table->entries = g_list_insert_before(table->entries, before, e);
328 table->num_entries++;
329 table->size += entry->next_offset;
331 if (before == NULL) {
332 e->offset = table->size - entry->next_offset;
337 entry_before = before->data;
340 * We've just insterted a new entry. All references before it
341 * should be bumped accordingly.
343 for (list = table->entries; list != before; list = list->next) {
349 t = (struct xt_standard_target *)ipt_get_target(tmp->entry);
351 if (t->verdict >= entry_before->offset)
352 t->verdict += entry->next_offset;
355 update_offsets(table);
360 static int iptables_add_chain(struct connman_iptables *table,
364 struct ipt_entry *entry_head;
365 struct ipt_entry *entry_return;
366 struct ipt_error_target *error;
367 struct ipt_standard_target *standard;
368 u_int16_t entry_head_size, entry_return_size;
370 last = g_list_last(table->entries);
373 * An empty chain is composed of:
374 * - A head entry, with no match and an error target.
375 * The error target data is the chain name.
376 * - A tail entry, with no match and a standard target.
377 * The standard target verdict is XT_RETURN (return to the
382 entry_head_size = sizeof(struct ipt_entry) +
383 sizeof(struct ipt_error_target);
384 entry_head = g_try_malloc0(entry_head_size);
385 if (entry_head == NULL)
388 memset(entry_head, 0, entry_head_size);
390 entry_head->target_offset = sizeof(struct ipt_entry);
391 entry_head->next_offset = entry_head_size;
393 error = (struct ipt_error_target *) entry_head->elems;
394 strcpy(error->t.u.user.name, IPT_ERROR_TARGET);
395 error->t.u.user.target_size = ALIGN(sizeof(struct ipt_error_target));
396 strcpy(error->error, name);
398 if (iptables_add_entry(table, entry_head, last, -1) < 0)
402 entry_return_size = sizeof(struct ipt_entry) +
403 sizeof(struct ipt_standard_target);
404 entry_return = g_try_malloc0(entry_return_size);
405 if (entry_return == NULL)
408 memset(entry_return, 0, entry_return_size);
410 entry_return->target_offset = sizeof(struct ipt_entry);
411 entry_return->next_offset = entry_return_size;
413 standard = (struct ipt_standard_target *) entry_return->elems;
414 standard->target.u.user.target_size =
415 ALIGN(sizeof(struct ipt_standard_target));
416 standard->verdict = XT_RETURN;
418 if (iptables_add_entry(table, entry_return, last, -1) < 0)
425 g_free(entry_return);
430 static struct ipt_entry *
431 new_rule(struct connman_iptables *table, struct ipt_ip *ip,
432 char *target_name, struct xtables_target *xt_t,
433 char *match_name, struct xtables_match *xt_m)
435 struct ipt_entry *new_entry;
436 size_t match_size, target_size;
437 int is_builtin = is_builtin_target(target_name);
440 match_size = xt_m->m->u.match_size;
445 target_size = ALIGN(xt_t->t->u.target_size);
447 target_size = ALIGN(sizeof(struct xt_standard_target));
449 new_entry = g_try_malloc0(sizeof(struct ipt_entry) + target_size +
451 if (new_entry == NULL)
454 memcpy(&new_entry->ip, ip, sizeof(struct ipt_ip));
456 new_entry->target_offset = sizeof(struct ipt_entry) + match_size;
457 new_entry->next_offset = sizeof(struct ipt_entry) + target_size +
460 struct xt_entry_match *entry_match;
462 entry_match = (struct xt_entry_match *)new_entry->elems;
463 memcpy(entry_match, xt_m->m, match_size);
467 struct xt_entry_target *entry_target;
470 struct xt_standard_target *target;
472 target = (struct xt_standard_target *)(xt_t->t);
473 strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
474 target->verdict = target_to_verdict(target_name);
477 entry_target = ipt_get_target(new_entry);
478 memcpy(entry_target, xt_t->t, target_size);
480 struct connman_iptables_entry *target_rule;
481 struct xt_standard_target *target;
485 * This is a user defined target, i.e. a chain jump.
486 * We search for the chain head, and the target verdict
487 * is the first rule's offset on this chain.
488 * The offset is from the beginning of the table.
491 chain_head = find_chain_head(table, target_name);
492 if (chain_head == NULL || chain_head->next == NULL) {
497 target_rule = chain_head->next->data;
499 target = (struct xt_standard_target *)ipt_get_target(new_entry);
500 strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
501 target->target.u.user.target_size = target_size;
502 target->verdict = target_rule->offset;
508 static void update_hooks(struct connman_iptables *table, GList *chain_head,
509 struct ipt_entry *entry)
512 struct connman_iptables_entry *head, *e;
515 if (chain_head == NULL)
518 head = chain_head->data;
520 builtin = head->builtin;
524 table->underflow[builtin] += entry->next_offset;
526 for (list = chain_head->next; list; list = list->next) {
529 builtin = e->builtin;
533 table->hook_entry[builtin] += entry->next_offset;
534 table->underflow[builtin] += entry->next_offset;
539 iptables_add_rule(struct connman_iptables *table,
540 struct ipt_ip *ip, char *chain_name,
541 char *target_name, struct xtables_target *xt_t,
542 char *match_name, struct xtables_match *xt_m)
544 GList *chain_tail, *chain_head;
545 struct ipt_entry *new_entry;
546 struct connman_iptables_entry *head;
549 chain_tail = find_chain_tail(table, chain_name);
550 if (chain_tail == NULL)
553 chain_head = find_chain_head(table, chain_name);
554 if (chain_head == NULL)
557 new_entry = new_rule(table, ip,
560 if (new_entry == NULL)
563 update_hooks(table, chain_head, new_entry);
566 * If the chain is builtin, and does not have any rule,
567 * then the one that we're inserting is becoming the head
568 * and thus needs the builtin flag.
570 head = chain_head->data;
571 if (head->builtin < 0)
573 else if (chain_head == chain_tail->prev) {
575 builtin = head->builtin;
578 return iptables_add_entry(table, new_entry, chain_tail->prev, builtin);
581 static struct ipt_replace *
582 iptables_blob(struct connman_iptables *table)
584 struct ipt_replace *r;
586 struct connman_iptables_entry *e;
587 unsigned char *entry_index;
589 r = g_try_malloc0(sizeof(struct ipt_replace) + table->size);
593 memset(r, 0, sizeof(*r) + table->size);
595 r->counters = g_try_malloc0(sizeof(struct xt_counters)
596 * table->num_entries);
597 if (r->counters == NULL) {
602 strcpy(r->name, table->info->name);
603 r->num_entries = table->num_entries;
604 r->size = table->size;
606 r->num_counters = table->old_entries;
607 r->valid_hooks = table->info->valid_hooks;
609 memcpy(r->hook_entry, table->hook_entry, sizeof(table->hook_entry));
610 memcpy(r->underflow, table->underflow, sizeof(table->underflow));
612 entry_index = (unsigned char *)r->entries;
613 for (list = table->entries; list; list = list->next) {
616 memcpy(entry_index, e->entry, e->entry->next_offset);
617 entry_index += e->entry->next_offset;
623 static void dump_ip(struct ipt_entry *entry)
625 struct ipt_ip *ip = &entry->ip;
626 char ip_string[INET6_ADDRSTRLEN];
627 char ip_mask[INET6_ADDRSTRLEN];
629 if (strlen(ip->iniface))
630 connman_info("\tin %s", ip->iniface);
632 if (strlen(ip->outiface))
633 connman_info("\tout %s", ip->outiface);
635 if (inet_ntop(AF_INET, &ip->src, ip_string, INET6_ADDRSTRLEN) != NULL &&
636 inet_ntop(AF_INET, &ip->smsk,
637 ip_mask, INET6_ADDRSTRLEN) != NULL)
638 connman_info("\tsrc %s/%s", ip_string, ip_mask);
640 if (inet_ntop(AF_INET, &ip->dst, ip_string, INET6_ADDRSTRLEN) != NULL &&
641 inet_ntop(AF_INET, &ip->dmsk,
642 ip_mask, INET6_ADDRSTRLEN) != NULL)
643 connman_info("\tdst %s/%s", ip_string, ip_mask);
646 static void dump_target(struct connman_iptables *table,
647 struct ipt_entry *entry)
650 struct xtables_target *xt_t;
651 struct xt_entry_target *target;
653 target = ipt_get_target(entry);
655 if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
656 struct xt_standard_target *t;
658 t = (struct xt_standard_target *)target;
660 switch (t->verdict) {
662 connman_info("\ttarget RETURN");
666 connman_info("\ttarget ACCEPT");
670 connman_info("\ttarget DROP");
674 connman_info("\ttarget QUEUE");
678 connman_info("\ttarget STOP");
682 connman_info("\tJUMP @%p (0x%x)",
683 (char*)table->blob_entries->entrytable +
684 t->verdict, t->verdict);
688 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
689 XTF_LOAD_MUST_SUCCEED);
691 if(xt_t->print != NULL)
692 xt_t->print(NULL, target, 1);
694 xt_t = xtables_find_target(target->u.user.name, XTF_TRY_LOAD);
696 connman_info("\ttarget %s", target->u.user.name);
700 if(xt_t->print != NULL) {
701 connman_info("\ttarget ");
702 xt_t->print(NULL, target, 1);
707 static void dump_match(struct connman_iptables *table, struct ipt_entry *entry)
709 struct xtables_match *xt_m;
710 struct xt_entry_match *match;
712 if (entry->elems == (unsigned char *)entry + entry->target_offset)
715 match = (struct xt_entry_match *) entry->elems;
717 if (!strlen(match->u.user.name))
720 xt_m = xtables_find_match(match->u.user.name, XTF_TRY_LOAD, NULL);
724 if(xt_m->print != NULL) {
725 connman_info("\tmatch ");
726 xt_m->print(NULL, match, 1);
732 connman_info("\tmatch %s", match->u.user.name);
736 static int dump_entry(struct ipt_entry *entry,
737 struct connman_iptables *table)
739 struct xt_entry_target *target;
743 offset = (char *)entry - (char *)table->blob_entries->entrytable;
744 target = ipt_get_target(entry);
745 builtin = is_hook_entry(table, entry);
747 if (entry_to_offset(table, entry) + entry->next_offset ==
748 table->blob_entries->size) {
749 connman_info("End of CHAIN 0x%x", offset);
753 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET)) {
754 connman_info("USER CHAIN (%s) %p match %p target %p size %d",
755 target->data, entry, entry->elems,
756 (char *)entry + entry->target_offset,
760 } else if (builtin >= 0) {
761 connman_info("CHAIN (%s) %p match %p target %p size %d",
762 hooknames[builtin], entry, entry->elems,
763 (char *)entry + entry->target_offset,
766 connman_info("RULE %p match %p target %p size %d", entry,
768 (char *)entry + entry->target_offset,
772 dump_match(table, entry);
773 dump_target(table, entry);
779 static void iptables_dump(struct connman_iptables *table)
781 connman_info("%s valid_hooks=0x%08x, num_entries=%u, size=%u",
783 table->info->valid_hooks, table->info->num_entries,
786 ENTRY_ITERATE(table->blob_entries->entrytable,
787 table->blob_entries->size,
792 static int iptables_get_entries(struct connman_iptables *table)
794 socklen_t entry_size;
796 entry_size = sizeof(struct ipt_get_entries) + table->info->size;
798 return getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_ENTRIES,
799 table->blob_entries, &entry_size);
802 static int iptables_replace(struct connman_iptables *table,
803 struct ipt_replace *r)
805 return setsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_SET_REPLACE, r,
806 sizeof(*r) + r->size);
809 static int add_entry(struct ipt_entry *entry, struct connman_iptables *table)
811 struct ipt_entry *new_entry;
814 new_entry = g_try_malloc0(entry->next_offset);
815 if (new_entry == NULL)
818 memcpy(new_entry, entry, entry->next_offset);
820 builtin = is_hook_entry(table, entry);
822 return iptables_add_entry(table, new_entry, NULL, builtin);
825 static void table_cleanup(struct connman_iptables *table)
828 struct connman_iptables_entry *entry;
830 close(table->ipt_sock);
832 for (list = table->entries; list; list = list->next) {
835 g_free(entry->entry);
838 g_list_free(table->entries);
840 g_free(table->blob_entries);
844 static struct connman_iptables *iptables_init(char *table_name)
846 struct connman_iptables *table;
849 table = g_hash_table_lookup(table_hash, table_name);
853 table = g_try_new0(struct connman_iptables, 1);
857 table->info = g_try_new0(struct ipt_getinfo, 1);
858 if (table->info == NULL)
861 table->ipt_sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
862 if (table->ipt_sock < 0)
865 s = sizeof(*table->info);
866 strcpy(table->info->name, table_name);
867 if (getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_INFO,
868 table->info, &s) < 0)
871 table->blob_entries = g_try_malloc0(sizeof(struct ipt_get_entries) +
873 if (table->blob_entries == NULL)
876 strcpy(table->blob_entries->name, table_name);
877 table->blob_entries->size = table->info->size;
879 if (iptables_get_entries(table) < 0)
882 table->num_entries = 0;
883 table->old_entries = table->info->num_entries;
886 memcpy(table->underflow, table->info->underflow,
887 sizeof(table->info->underflow));
888 memcpy(table->hook_entry, table->info->hook_entry,
889 sizeof(table->info->hook_entry));
891 ENTRY_ITERATE(table->blob_entries->entrytable,
892 table->blob_entries->size,
895 g_hash_table_insert(table_hash, table_name, table);
901 table_cleanup(table);
906 static struct option iptables_opts[] = {
907 {.name = "append", .has_arg = 1, .val = 'A'},
908 {.name = "list", .has_arg = 2, .val = 'L'},
909 {.name = "new-chain", .has_arg = 1, .val = 'N'},
910 {.name = "destination", .has_arg = 1, .val = 'd'},
911 {.name = "in-interface", .has_arg = 1, .val = 'i'},
912 {.name = "jump", .has_arg = 1, .val = 'j'},
913 {.name = "match", .has_arg = 1, .val = 'm'},
914 {.name = "out-interface", .has_arg = 1, .val = 'o'},
915 {.name = "source", .has_arg = 1, .val = 's'},
916 {.name = "table", .has_arg = 1, .val = 't'},
920 struct xtables_globals iptables_globals = {
922 .opts = iptables_opts,
923 .orig_opts = iptables_opts,
926 static int iptables_command(int argc, char *argv[])
928 struct connman_iptables *table;
929 struct xtables_match *xt_m;
930 struct xtables_target *xt_t;
932 char *table_name, *chain, *new_chain, *match_name, *target_name;
933 int c, ret, in_len, out_len;
935 gboolean dump, invert;
936 struct in_addr src, dst;
943 table_name = chain = new_chain = match_name = target_name = NULL;
944 memset(&ip, 0, sizeof(struct ipt_ip));
952 while ((c = getopt_long(argc, argv,
953 "-A:L::N:d:j:i:m:o:s:t:", iptables_globals.opts, NULL)) != -1) {
968 if (!inet_pton(AF_INET, optarg, &dst))
972 inet_pton(AF_INET, "255.255.255.255", &ip.dmsk);
975 ip.invflags |= IPT_INV_DSTIP;
980 in_len = strlen(optarg);
982 if (in_len + 1 > IFNAMSIZ)
985 strcpy(ip.iniface, optarg);
986 memset(ip.iniface_mask, 0xff, in_len + 1);
989 ip.invflags |= IPT_INV_VIA_IN;
994 target_name = optarg;
995 xt_t = xtables_find_target(target_name, XTF_TRY_LOAD);
1000 size = ALIGN(sizeof(struct ipt_entry_target)) +
1003 xt_t->t = g_try_malloc0(size);
1004 if (xt_t->t == NULL)
1006 xt_t->t->u.target_size = size;
1007 strcpy(xt_t->t->u.user.name, target_name);
1008 xt_t->t->u.user.revision = xt_t->revision;
1009 if (xt_t->init != NULL)
1010 xt_t->init(xt_t->t);
1011 iptables_globals.opts =
1012 xtables_merge_options(iptables_globals.opts,
1014 &xt_t->option_offset);
1015 if (iptables_globals.opts == NULL)
1021 match_name = optarg;
1023 xt_m = xtables_find_match(optarg, XTF_LOAD_MUST_SUCCEED, NULL);
1024 size = ALIGN(sizeof(struct ipt_entry_match)) +
1026 xt_m->m = g_try_malloc0(size);
1029 xt_m->m->u.match_size = size;
1030 strcpy(xt_m->m->u.user.name, xt_m->name);
1031 xt_m->m->u.user.revision = xt_m->revision;
1032 if (xt_m->init != NULL)
1033 xt_m->init(xt_m->m);
1034 if (xt_m != xt_m->next) {
1035 iptables_globals.opts =
1036 xtables_merge_options(iptables_globals.opts,
1038 &xt_m->option_offset);
1039 if (iptables_globals.opts == NULL)
1046 out_len = strlen(optarg);
1048 if (out_len + 1 > IFNAMSIZ)
1051 strcpy(ip.outiface, optarg);
1052 memset(ip.outiface_mask, 0xff, out_len + 1);
1055 ip.invflags |= IPT_INV_VIA_OUT;
1060 if (!inet_pton(AF_INET, optarg, &src))
1064 inet_pton(AF_INET, "255.255.255.255", &ip.smsk);
1067 ip.invflags |= IPT_INV_SRCIP;
1072 table_name = optarg;
1076 if (optarg[0] == '!' && optarg[1] == '\0') {
1082 connman_error("Invalid option");
1088 if (xt_t == NULL || xt_t->parse == NULL ||
1089 !xt_t->parse(c - xt_t->option_offset, argv, invert,
1090 &xt_t->tflags, NULL, &xt_t->t)) {
1091 if (xt_m == NULL || xt_m->parse == NULL)
1094 xt_m->parse(c - xt_m->option_offset, argv,
1095 invert, &xt_m->mflags, NULL, &xt_m->m);
1104 if (table_name == NULL)
1105 table_name = "filter";
1107 table = iptables_init(table_name);
1108 if (table == NULL) {
1114 iptables_dump(table);
1120 if (chain && new_chain) {
1126 DBG("New chain %s", new_chain);
1128 ret = iptables_add_chain(table, new_chain);
1133 if (target_name == NULL)
1136 DBG("Adding %s to %s (match %s)",
1137 target_name, chain, match_name);
1139 ret = iptables_add_rule(table, &ip, chain, target_name, xt_t,
1155 int __connman_iptables_command(const char *format, ...)
1157 char **argv, **arguments, *command;
1164 va_start(args, format);
1166 command = g_strdup_vprintf(format, args);
1170 if (command == NULL)
1173 arguments = g_strsplit_set(command, " ", -1);
1175 for (argc = 0; arguments[argc]; argc++);
1177 DBG("command %s argc %d", command, ++argc);
1179 argv = g_try_malloc0(argc * sizeof(char *));
1182 g_strfreev(arguments);
1186 argv[0] = "iptables";
1187 for (i = 1; i < argc; i++)
1188 argv[i] = arguments[i - 1];
1190 ret = iptables_command(argc, argv);
1193 g_strfreev(arguments);
1200 int __connman_iptables_commit(const char *table_name)
1202 struct connman_iptables *table;
1203 struct ipt_replace *repl;
1205 table = g_hash_table_lookup(table_hash, table_name);
1209 repl = iptables_blob(table);
1211 return iptables_replace(table, repl);
1214 static void remove_table(gpointer user_data)
1216 struct connman_iptables *table = user_data;
1218 table_cleanup(table);
1221 int __connman_iptables_init(void)
1225 table_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1226 NULL, remove_table);
1228 xtables_init_all(&iptables_globals, NFPROTO_IPV4);
1234 void __connman_iptables_cleanup(void)
1236 g_hash_table_destroy(table_hash);
1238 xtables_free_opts(1);