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))
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 struct connman_iptables_entry *tail;
246 GList *chain_head, *list;
248 chain_head = find_chain_head(table, chain_name);
249 if (chain_head == NULL)
252 /* Then we look for the next chain */
253 for (list = chain_head->next; list; list = list->next) {
256 if (is_chain(table, tail))
260 /* Nothing found, we return the table end */
261 return g_list_last(table->entries);
265 static void update_offsets(struct connman_iptables *table)
268 struct connman_iptables_entry *entry, *prev_entry;
270 for (list = table->entries; list; list = list->next) {
273 if (list == table->entries) {
280 prev_entry = prev->data;
282 entry->offset = prev_entry->offset +
283 prev_entry->entry->next_offset;
287 static int iptables_add_entry(struct connman_iptables *table,
288 struct ipt_entry *entry, GList *before,
292 struct connman_iptables_entry *e, *tmp, *entry_before;
293 struct xt_standard_target *t;
298 e = g_try_malloc0(sizeof(struct connman_iptables_entry));
303 e->builtin = builtin;
305 table->entries = g_list_insert_before(table->entries, before, e);
306 table->num_entries++;
307 table->size += entry->next_offset;
309 if (before == NULL) {
310 e->offset = table->size - entry->next_offset;
315 entry_before = before->data;
318 * We've just insterted a new entry. All references before it
319 * should be bumped accordingly.
321 for (list = table->entries; list != before; list = list->next) {
327 t = (struct xt_standard_target *)ipt_get_target(tmp->entry);
329 if (t->verdict >= entry_before->offset)
330 t->verdict += entry->next_offset;
333 update_offsets(table);
338 static int iptables_flush_chain(struct connman_iptables *table,
341 GList *chain_head, *chain_tail, *list, *next;
342 struct connman_iptables_entry *entry;
343 int builtin, removed = 0;
345 chain_head = find_chain_head(table, name);
346 if (chain_head == NULL)
349 chain_tail = find_chain_tail(table, name);
350 if (chain_tail == NULL)
353 entry = chain_head->data;
354 builtin = entry->builtin;
359 list = chain_head->next;
361 if (list == chain_tail->prev)
364 while (list != chain_tail->prev) {
366 next = g_list_next(list);
368 table->num_entries--;
369 table->size -= entry->entry->next_offset;
370 removed += entry->entry->next_offset;
372 g_free(entry->entry);
374 table->entries = g_list_remove(table->entries, list->data);
380 struct connman_iptables_entry *e;
384 entry->builtin = builtin;
386 table->underflow[builtin] -= removed;
388 for (list = chain_tail; list; list = list->next) {
391 builtin = e->builtin;
395 table->hook_entry[builtin] -= removed;
396 table->underflow[builtin] -= removed;
400 update_offsets(table);
405 static int iptables_add_chain(struct connman_iptables *table,
409 struct ipt_entry *entry_head;
410 struct ipt_entry *entry_return;
411 struct error_target *error;
412 struct ipt_standard_target *standard;
413 u_int16_t entry_head_size, entry_return_size;
415 last = g_list_last(table->entries);
418 * An empty chain is composed of:
419 * - A head entry, with no match and an error target.
420 * The error target data is the chain name.
421 * - A tail entry, with no match and a standard target.
422 * The standard target verdict is XT_RETURN (return to the
427 entry_head_size = sizeof(struct ipt_entry) +
428 sizeof(struct error_target);
429 entry_head = g_try_malloc0(entry_head_size);
430 if (entry_head == NULL)
433 memset(entry_head, 0, entry_head_size);
435 entry_head->target_offset = sizeof(struct ipt_entry);
436 entry_head->next_offset = entry_head_size;
438 error = (struct error_target *) entry_head->elems;
439 strcpy(error->t.u.user.name, IPT_ERROR_TARGET);
440 error->t.u.user.target_size = ALIGN(sizeof(struct error_target));
441 strcpy(error->error, name);
443 if (iptables_add_entry(table, entry_head, last, -1) < 0)
447 entry_return_size = sizeof(struct ipt_entry) +
448 sizeof(struct ipt_standard_target);
449 entry_return = g_try_malloc0(entry_return_size);
450 if (entry_return == NULL)
453 memset(entry_return, 0, entry_return_size);
455 entry_return->target_offset = sizeof(struct ipt_entry);
456 entry_return->next_offset = entry_return_size;
458 standard = (struct ipt_standard_target *) entry_return->elems;
459 standard->target.u.user.target_size =
460 ALIGN(sizeof(struct ipt_standard_target));
461 standard->verdict = XT_RETURN;
463 if (iptables_add_entry(table, entry_return, last, -1) < 0)
469 g_free(entry_return);
476 static struct ipt_entry *
477 new_rule(struct connman_iptables *table, struct ipt_ip *ip,
478 char *target_name, struct xtables_target *xt_t,
479 char *match_name, struct xtables_match *xt_m)
481 struct ipt_entry *new_entry;
482 size_t match_size, target_size;
483 int is_builtin = is_builtin_target(target_name);
486 match_size = xt_m->m->u.match_size;
491 target_size = ALIGN(xt_t->t->u.target_size);
493 target_size = ALIGN(sizeof(struct xt_standard_target));
495 new_entry = g_try_malloc0(sizeof(struct ipt_entry) + target_size +
497 if (new_entry == NULL)
500 memcpy(&new_entry->ip, ip, sizeof(struct ipt_ip));
502 new_entry->target_offset = sizeof(struct ipt_entry) + match_size;
503 new_entry->next_offset = sizeof(struct ipt_entry) + target_size +
506 struct xt_entry_match *entry_match;
508 entry_match = (struct xt_entry_match *)new_entry->elems;
509 memcpy(entry_match, xt_m->m, match_size);
513 struct xt_entry_target *entry_target;
516 struct xt_standard_target *target;
518 target = (struct xt_standard_target *)(xt_t->t);
519 strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
520 target->verdict = target_to_verdict(target_name);
523 entry_target = ipt_get_target(new_entry);
524 memcpy(entry_target, xt_t->t, target_size);
526 struct connman_iptables_entry *target_rule;
527 struct xt_standard_target *target;
531 * This is a user defined target, i.e. a chain jump.
532 * We search for the chain head, and the target verdict
533 * is the first rule's offset on this chain.
534 * The offset is from the beginning of the table.
537 chain_head = find_chain_head(table, target_name);
538 if (chain_head == NULL || chain_head->next == NULL) {
543 target_rule = chain_head->next->data;
545 target = (struct xt_standard_target *)ipt_get_target(new_entry);
546 strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
547 target->target.u.user.target_size = target_size;
548 target->verdict = target_rule->offset;
554 static void update_hooks(struct connman_iptables *table, GList *chain_head,
555 struct ipt_entry *entry)
558 struct connman_iptables_entry *head, *e;
561 if (chain_head == NULL)
564 head = chain_head->data;
566 builtin = head->builtin;
570 table->underflow[builtin] += entry->next_offset;
572 for (list = chain_head->next; list; list = list->next) {
575 builtin = e->builtin;
579 table->hook_entry[builtin] += entry->next_offset;
580 table->underflow[builtin] += entry->next_offset;
585 iptables_add_rule(struct connman_iptables *table,
586 struct ipt_ip *ip, char *chain_name,
587 char *target_name, struct xtables_target *xt_t,
588 char *match_name, struct xtables_match *xt_m)
590 GList *chain_tail, *chain_head;
591 struct ipt_entry *new_entry;
592 struct connman_iptables_entry *head;
597 chain_head = find_chain_head(table, chain_name);
598 if (chain_head == NULL)
601 chain_tail = find_chain_tail(table, chain_name);
602 if (chain_tail == NULL)
605 new_entry = new_rule(table, ip,
608 if (new_entry == NULL)
611 update_hooks(table, chain_head, new_entry);
614 * If the chain is builtin, and does not have any rule,
615 * then the one that we're inserting is becoming the head
616 * and thus needs the builtin flag.
618 head = chain_head->data;
619 if (head->builtin < 0)
621 else if (chain_head == chain_tail->prev) {
622 builtin = head->builtin;
626 return iptables_add_entry(table, new_entry, chain_tail->prev, builtin);
629 static struct ipt_replace *
630 iptables_blob(struct connman_iptables *table)
632 struct ipt_replace *r;
634 struct connman_iptables_entry *e;
635 unsigned char *entry_index;
637 r = g_try_malloc0(sizeof(struct ipt_replace) + table->size);
641 memset(r, 0, sizeof(*r) + table->size);
643 r->counters = g_try_malloc0(sizeof(struct xt_counters)
644 * table->old_entries);
645 if (r->counters == NULL) {
650 strcpy(r->name, table->info->name);
651 r->num_entries = table->num_entries;
652 r->size = table->size;
654 r->num_counters = table->old_entries;
655 r->valid_hooks = table->info->valid_hooks;
657 memcpy(r->hook_entry, table->hook_entry, sizeof(table->hook_entry));
658 memcpy(r->underflow, table->underflow, sizeof(table->underflow));
660 entry_index = (unsigned char *)r->entries;
661 for (list = table->entries; list; list = list->next) {
664 memcpy(entry_index, e->entry, e->entry->next_offset);
665 entry_index += e->entry->next_offset;
671 static void dump_ip(struct ipt_entry *entry)
673 struct ipt_ip *ip = &entry->ip;
674 char ip_string[INET6_ADDRSTRLEN];
675 char ip_mask[INET6_ADDRSTRLEN];
677 if (strlen(ip->iniface))
678 connman_info("\tin %s", ip->iniface);
680 if (strlen(ip->outiface))
681 connman_info("\tout %s", ip->outiface);
683 if (inet_ntop(AF_INET, &ip->src, ip_string, INET6_ADDRSTRLEN) != NULL &&
684 inet_ntop(AF_INET, &ip->smsk,
685 ip_mask, INET6_ADDRSTRLEN) != NULL)
686 connman_info("\tsrc %s/%s", ip_string, ip_mask);
688 if (inet_ntop(AF_INET, &ip->dst, ip_string, INET6_ADDRSTRLEN) != NULL &&
689 inet_ntop(AF_INET, &ip->dmsk,
690 ip_mask, INET6_ADDRSTRLEN) != NULL)
691 connman_info("\tdst %s/%s", ip_string, ip_mask);
694 static void dump_target(struct connman_iptables *table,
695 struct ipt_entry *entry)
698 struct xtables_target *xt_t;
699 struct xt_entry_target *target;
701 target = ipt_get_target(entry);
703 if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
704 struct xt_standard_target *t;
706 t = (struct xt_standard_target *)target;
708 switch (t->verdict) {
710 connman_info("\ttarget RETURN");
714 connman_info("\ttarget ACCEPT");
718 connman_info("\ttarget DROP");
722 connman_info("\ttarget QUEUE");
726 connman_info("\ttarget STOP");
730 connman_info("\tJUMP @%p (0x%x)",
731 (char*)table->blob_entries->entrytable +
732 t->verdict, t->verdict);
736 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
737 XTF_LOAD_MUST_SUCCEED);
739 if(xt_t->print != NULL)
740 xt_t->print(NULL, target, 1);
742 xt_t = xtables_find_target(target->u.user.name, XTF_TRY_LOAD);
744 connman_info("\ttarget %s", target->u.user.name);
748 if(xt_t->print != NULL) {
749 connman_info("\ttarget ");
750 xt_t->print(NULL, target, 1);
755 static void dump_match(struct connman_iptables *table, struct ipt_entry *entry)
757 struct xtables_match *xt_m;
758 struct xt_entry_match *match;
760 if (entry->elems == (unsigned char *)entry + entry->target_offset)
763 match = (struct xt_entry_match *) entry->elems;
765 if (!strlen(match->u.user.name))
768 xt_m = xtables_find_match(match->u.user.name, XTF_TRY_LOAD, NULL);
772 if(xt_m->print != NULL) {
773 connman_info("\tmatch ");
774 xt_m->print(NULL, match, 1);
780 connman_info("\tmatch %s", match->u.user.name);
784 static int dump_entry(struct ipt_entry *entry,
785 struct connman_iptables *table)
787 struct xt_entry_target *target;
791 offset = (char *)entry - (char *)table->blob_entries->entrytable;
792 target = ipt_get_target(entry);
793 builtin = is_hook_entry(table, entry);
795 if (entry_to_offset(table, entry) + entry->next_offset ==
796 table->blob_entries->size) {
797 connman_info("End of CHAIN 0x%x", offset);
801 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET)) {
802 connman_info("USER CHAIN (%s) %p match %p target %p size %d",
803 target->data, entry, entry->elems,
804 (char *)entry + entry->target_offset,
808 } else if (builtin >= 0) {
809 connman_info("CHAIN (%s) %p match %p target %p size %d",
810 hooknames[builtin], entry, entry->elems,
811 (char *)entry + entry->target_offset,
814 connman_info("RULE %p match %p target %p size %d", entry,
816 (char *)entry + entry->target_offset,
820 dump_match(table, entry);
821 dump_target(table, entry);
827 static void iptables_dump(struct connman_iptables *table)
829 connman_info("%s valid_hooks=0x%08x, num_entries=%u, size=%u",
831 table->info->valid_hooks, table->info->num_entries,
834 ENTRY_ITERATE(table->blob_entries->entrytable,
835 table->blob_entries->size,
840 static int iptables_get_entries(struct connman_iptables *table)
842 socklen_t entry_size;
844 entry_size = sizeof(struct ipt_get_entries) + table->info->size;
846 return getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_ENTRIES,
847 table->blob_entries, &entry_size);
850 static int iptables_replace(struct connman_iptables *table,
851 struct ipt_replace *r)
853 return setsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_SET_REPLACE, r,
854 sizeof(*r) + r->size);
857 static int add_entry(struct ipt_entry *entry, struct connman_iptables *table)
859 struct ipt_entry *new_entry;
862 new_entry = g_try_malloc0(entry->next_offset);
863 if (new_entry == NULL)
866 memcpy(new_entry, entry, entry->next_offset);
868 builtin = is_hook_entry(table, entry);
870 return iptables_add_entry(table, new_entry, NULL, builtin);
873 static void table_cleanup(struct connman_iptables *table)
876 struct connman_iptables_entry *entry;
878 close(table->ipt_sock);
880 for (list = table->entries; list; list = list->next) {
883 g_free(entry->entry);
887 g_list_free(table->entries);
889 g_free(table->blob_entries);
893 static struct connman_iptables *iptables_init(char *table_name)
895 struct connman_iptables *table;
898 DBG("%s", table_name);
900 table = g_hash_table_lookup(table_hash, table_name);
904 table = g_try_new0(struct connman_iptables, 1);
908 table->info = g_try_new0(struct ipt_getinfo, 1);
909 if (table->info == NULL)
912 table->ipt_sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
913 if (table->ipt_sock < 0)
916 s = sizeof(*table->info);
917 strcpy(table->info->name, table_name);
918 if (getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_INFO,
919 table->info, &s) < 0)
922 table->blob_entries = g_try_malloc0(sizeof(struct ipt_get_entries) +
924 if (table->blob_entries == NULL)
927 strcpy(table->blob_entries->name, table_name);
928 table->blob_entries->size = table->info->size;
930 if (iptables_get_entries(table) < 0)
933 table->num_entries = 0;
934 table->old_entries = table->info->num_entries;
937 memcpy(table->underflow, table->info->underflow,
938 sizeof(table->info->underflow));
939 memcpy(table->hook_entry, table->info->hook_entry,
940 sizeof(table->info->hook_entry));
942 ENTRY_ITERATE(table->blob_entries->entrytable,
943 table->blob_entries->size,
946 g_hash_table_insert(table_hash, g_strdup(table_name), table);
952 table_cleanup(table);
957 static struct option iptables_opts[] = {
958 {.name = "append", .has_arg = 1, .val = 'A'},
959 {.name = "flush-chain", .has_arg = 1, .val = 'F'},
960 {.name = "list", .has_arg = 2, .val = 'L'},
961 {.name = "new-chain", .has_arg = 1, .val = 'N'},
962 {.name = "destination", .has_arg = 1, .val = 'd'},
963 {.name = "in-interface", .has_arg = 1, .val = 'i'},
964 {.name = "jump", .has_arg = 1, .val = 'j'},
965 {.name = "match", .has_arg = 1, .val = 'm'},
966 {.name = "out-interface", .has_arg = 1, .val = 'o'},
967 {.name = "source", .has_arg = 1, .val = 's'},
968 {.name = "table", .has_arg = 1, .val = 't'},
972 struct xtables_globals iptables_globals = {
974 .opts = iptables_opts,
975 .orig_opts = iptables_opts,
978 static int iptables_command(int argc, char *argv[])
980 struct connman_iptables *table;
981 struct xtables_match *xt_m;
982 struct xtables_target *xt_t;
984 char *table_name, *chain, *new_chain, *match_name, *target_name;
986 int c, ret, in_len, out_len;
988 gboolean dump, invert;
989 struct in_addr src, dst;
996 table_name = chain = new_chain = match_name = target_name = NULL;
998 memset(&ip, 0, sizeof(struct ipt_ip));
1006 while ((c = getopt_long(argc, argv,
1007 "-A:F:L::N:d:j:i:m:o:s:t:", iptables_globals.opts, NULL)) != -1) {
1014 flush_chain = optarg;
1026 if (!inet_pton(AF_INET, optarg, &dst))
1030 inet_pton(AF_INET, "255.255.255.255", &ip.dmsk);
1033 ip.invflags |= IPT_INV_DSTIP;
1038 in_len = strlen(optarg);
1040 if (in_len + 1 > IFNAMSIZ)
1043 strcpy(ip.iniface, optarg);
1044 memset(ip.iniface_mask, 0xff, in_len + 1);
1047 ip.invflags |= IPT_INV_VIA_IN;
1052 target_name = optarg;
1053 xt_t = xtables_find_target(target_name, XTF_TRY_LOAD);
1058 size = ALIGN(sizeof(struct ipt_entry_target)) +
1061 xt_t->t = g_try_malloc0(size);
1062 if (xt_t->t == NULL)
1064 xt_t->t->u.target_size = size;
1065 strcpy(xt_t->t->u.user.name, target_name);
1066 xt_t->t->u.user.revision = xt_t->revision;
1067 if (xt_t->init != NULL)
1068 xt_t->init(xt_t->t);
1069 iptables_globals.opts =
1070 xtables_merge_options(
1071 #if XTABLES_VERSION_CODE > 5
1072 iptables_globals.orig_opts,
1074 iptables_globals.opts,
1076 &xt_t->option_offset);
1077 if (iptables_globals.opts == NULL)
1083 match_name = optarg;
1085 xt_m = xtables_find_match(optarg, XTF_LOAD_MUST_SUCCEED, NULL);
1086 size = ALIGN(sizeof(struct ipt_entry_match)) +
1088 xt_m->m = g_try_malloc0(size);
1091 xt_m->m->u.match_size = size;
1092 strcpy(xt_m->m->u.user.name, xt_m->name);
1093 xt_m->m->u.user.revision = xt_m->revision;
1094 if (xt_m->init != NULL)
1095 xt_m->init(xt_m->m);
1096 if (xt_m != xt_m->next) {
1097 iptables_globals.opts =
1098 xtables_merge_options(
1099 #if XTABLES_VERSION_CODE > 5
1100 iptables_globals.orig_opts,
1102 iptables_globals.opts,
1104 &xt_m->option_offset);
1105 if (iptables_globals.opts == NULL)
1112 out_len = strlen(optarg);
1114 if (out_len + 1 > IFNAMSIZ)
1117 strcpy(ip.outiface, optarg);
1118 memset(ip.outiface_mask, 0xff, out_len + 1);
1121 ip.invflags |= IPT_INV_VIA_OUT;
1126 if (!inet_pton(AF_INET, optarg, &src))
1130 inet_pton(AF_INET, "255.255.255.255", &ip.smsk);
1133 ip.invflags |= IPT_INV_SRCIP;
1138 table_name = optarg;
1142 if (optarg[0] == '!' && optarg[1] == '\0') {
1148 connman_error("Invalid option");
1154 if (xt_t == NULL || xt_t->parse == NULL ||
1155 !xt_t->parse(c - xt_t->option_offset, argv, invert,
1156 &xt_t->tflags, NULL, &xt_t->t)) {
1157 if (xt_m == NULL || xt_m->parse == NULL)
1160 xt_m->parse(c - xt_m->option_offset, argv,
1161 invert, &xt_m->mflags, NULL, &xt_m->m);
1170 if (table_name == NULL)
1171 table_name = "filter";
1173 table = iptables_init(table_name);
1174 if (table == NULL) {
1180 iptables_dump(table);
1187 DBG("Flush chain %s", flush_chain);
1189 iptables_flush_chain(table, flush_chain);
1194 if (chain && new_chain) {
1200 DBG("New chain %s", new_chain);
1202 ret = iptables_add_chain(table, new_chain);
1207 if (target_name == NULL)
1210 DBG("Adding %s to %s (match %s)",
1211 target_name, chain, match_name);
1213 ret = iptables_add_rule(table, &ip, chain, target_name, xt_t,
1229 int __connman_iptables_command(const char *format, ...)
1231 char **argv, **arguments, *command;
1238 va_start(args, format);
1240 command = g_strdup_vprintf(format, args);
1244 if (command == NULL)
1247 arguments = g_strsplit_set(command, " ", -1);
1249 for (argc = 0; arguments[argc]; argc++);
1252 DBG("command %s argc %d", command, argc);
1254 argv = g_try_malloc0(argc * sizeof(char *));
1257 g_strfreev(arguments);
1261 argv[0] = "iptables";
1262 for (i = 1; i < argc; i++)
1263 argv[i] = arguments[i - 1];
1265 ret = iptables_command(argc, argv);
1268 g_strfreev(arguments);
1275 int __connman_iptables_commit(const char *table_name)
1277 struct connman_iptables *table;
1278 struct ipt_replace *repl;
1281 DBG("%s", table_name);
1283 table = g_hash_table_lookup(table_hash, table_name);
1287 repl = iptables_blob(table);
1289 err = iptables_replace(table, repl);
1291 g_free(repl->counters);
1297 g_hash_table_remove(table_hash, table_name);
1302 static void remove_table(gpointer user_data)
1304 struct connman_iptables *table = user_data;
1306 table_cleanup(table);
1309 int __connman_iptables_init(void)
1313 table_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1314 g_free, remove_table);
1316 xtables_init_all(&iptables_globals, NFPROTO_IPV4);
1322 void __connman_iptables_cleanup(void)
1326 g_hash_table_destroy(table_hash);
1328 xtables_free_opts(1);