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 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_flush_chain(struct connman_iptables *table,
363 GList *chain_head, *chain_tail, *list, *next;
364 struct connman_iptables_entry *entry;
365 int builtin, removed = 0;
367 chain_head = find_chain_head(table, name);
368 if (chain_head == NULL)
371 chain_tail = find_chain_tail(table, name);
372 if (chain_tail == NULL)
375 entry = chain_head->data;
376 builtin = entry->builtin;
381 list = chain_head->next;
383 if (list == chain_tail->prev)
386 while (list != chain_tail->prev) {
388 next = g_list_next(list);
390 table->num_entries--;
391 table->size -= entry->entry->next_offset;
392 removed += entry->entry->next_offset;
394 g_free(entry->entry);
396 table->entries = g_list_remove(table->entries, list->data);
402 struct connman_iptables_entry *e;
406 entry->builtin = builtin;
408 table->underflow[builtin] -= removed;
410 for (list = chain_tail; list; list = list->next) {
413 builtin = e->builtin;
417 table->hook_entry[builtin] -= removed;
418 table->underflow[builtin] -= removed;
422 update_offsets(table);
427 static int iptables_add_chain(struct connman_iptables *table,
431 struct ipt_entry *entry_head;
432 struct ipt_entry *entry_return;
433 struct error_target *error;
434 struct ipt_standard_target *standard;
435 u_int16_t entry_head_size, entry_return_size;
437 last = g_list_last(table->entries);
440 * An empty chain is composed of:
441 * - A head entry, with no match and an error target.
442 * The error target data is the chain name.
443 * - A tail entry, with no match and a standard target.
444 * The standard target verdict is XT_RETURN (return to the
449 entry_head_size = sizeof(struct ipt_entry) +
450 sizeof(struct error_target);
451 entry_head = g_try_malloc0(entry_head_size);
452 if (entry_head == NULL)
455 memset(entry_head, 0, entry_head_size);
457 entry_head->target_offset = sizeof(struct ipt_entry);
458 entry_head->next_offset = entry_head_size;
460 error = (struct error_target *) entry_head->elems;
461 strcpy(error->t.u.user.name, IPT_ERROR_TARGET);
462 error->t.u.user.target_size = ALIGN(sizeof(struct error_target));
463 strcpy(error->error, name);
465 if (iptables_add_entry(table, entry_head, last, -1) < 0)
469 entry_return_size = sizeof(struct ipt_entry) +
470 sizeof(struct ipt_standard_target);
471 entry_return = g_try_malloc0(entry_return_size);
472 if (entry_return == NULL)
475 memset(entry_return, 0, entry_return_size);
477 entry_return->target_offset = sizeof(struct ipt_entry);
478 entry_return->next_offset = entry_return_size;
480 standard = (struct ipt_standard_target *) entry_return->elems;
481 standard->target.u.user.target_size =
482 ALIGN(sizeof(struct ipt_standard_target));
483 standard->verdict = XT_RETURN;
485 if (iptables_add_entry(table, entry_return, last, -1) < 0)
492 g_free(entry_return);
497 static struct ipt_entry *
498 new_rule(struct connman_iptables *table, struct ipt_ip *ip,
499 char *target_name, struct xtables_target *xt_t,
500 char *match_name, struct xtables_match *xt_m)
502 struct ipt_entry *new_entry;
503 size_t match_size, target_size;
504 int is_builtin = is_builtin_target(target_name);
507 match_size = xt_m->m->u.match_size;
512 target_size = ALIGN(xt_t->t->u.target_size);
514 target_size = ALIGN(sizeof(struct xt_standard_target));
516 new_entry = g_try_malloc0(sizeof(struct ipt_entry) + target_size +
518 if (new_entry == NULL)
521 memcpy(&new_entry->ip, ip, sizeof(struct ipt_ip));
523 new_entry->target_offset = sizeof(struct ipt_entry) + match_size;
524 new_entry->next_offset = sizeof(struct ipt_entry) + target_size +
527 struct xt_entry_match *entry_match;
529 entry_match = (struct xt_entry_match *)new_entry->elems;
530 memcpy(entry_match, xt_m->m, match_size);
534 struct xt_entry_target *entry_target;
537 struct xt_standard_target *target;
539 target = (struct xt_standard_target *)(xt_t->t);
540 strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
541 target->verdict = target_to_verdict(target_name);
544 entry_target = ipt_get_target(new_entry);
545 memcpy(entry_target, xt_t->t, target_size);
547 struct connman_iptables_entry *target_rule;
548 struct xt_standard_target *target;
552 * This is a user defined target, i.e. a chain jump.
553 * We search for the chain head, and the target verdict
554 * is the first rule's offset on this chain.
555 * The offset is from the beginning of the table.
558 chain_head = find_chain_head(table, target_name);
559 if (chain_head == NULL || chain_head->next == NULL) {
564 target_rule = chain_head->next->data;
566 target = (struct xt_standard_target *)ipt_get_target(new_entry);
567 strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
568 target->target.u.user.target_size = target_size;
569 target->verdict = target_rule->offset;
575 static void update_hooks(struct connman_iptables *table, GList *chain_head,
576 struct ipt_entry *entry)
579 struct connman_iptables_entry *head, *e;
582 if (chain_head == NULL)
585 head = chain_head->data;
587 builtin = head->builtin;
591 table->underflow[builtin] += entry->next_offset;
593 for (list = chain_head->next; list; list = list->next) {
596 builtin = e->builtin;
600 table->hook_entry[builtin] += entry->next_offset;
601 table->underflow[builtin] += entry->next_offset;
606 iptables_add_rule(struct connman_iptables *table,
607 struct ipt_ip *ip, char *chain_name,
608 char *target_name, struct xtables_target *xt_t,
609 char *match_name, struct xtables_match *xt_m)
611 GList *chain_tail, *chain_head;
612 struct ipt_entry *new_entry;
613 struct connman_iptables_entry *head;
618 chain_head = find_chain_head(table, chain_name);
619 if (chain_head == NULL)
622 chain_tail = find_chain_tail(table, chain_name);
623 if (chain_tail == NULL)
626 new_entry = new_rule(table, ip,
629 if (new_entry == NULL)
632 update_hooks(table, chain_head, new_entry);
635 * If the chain is builtin, and does not have any rule,
636 * then the one that we're inserting is becoming the head
637 * and thus needs the builtin flag.
639 head = chain_head->data;
640 if (head->builtin < 0)
642 else if (chain_head == chain_tail->prev) {
643 builtin = head->builtin;
647 return iptables_add_entry(table, new_entry, chain_tail->prev, builtin);
650 static struct ipt_replace *
651 iptables_blob(struct connman_iptables *table)
653 struct ipt_replace *r;
655 struct connman_iptables_entry *e;
656 unsigned char *entry_index;
658 r = g_try_malloc0(sizeof(struct ipt_replace) + table->size);
662 memset(r, 0, sizeof(*r) + table->size);
664 r->counters = g_try_malloc0(sizeof(struct xt_counters)
665 * table->old_entries);
666 if (r->counters == NULL) {
671 strcpy(r->name, table->info->name);
672 r->num_entries = table->num_entries;
673 r->size = table->size;
675 r->num_counters = table->old_entries;
676 r->valid_hooks = table->info->valid_hooks;
678 memcpy(r->hook_entry, table->hook_entry, sizeof(table->hook_entry));
679 memcpy(r->underflow, table->underflow, sizeof(table->underflow));
681 entry_index = (unsigned char *)r->entries;
682 for (list = table->entries; list; list = list->next) {
685 memcpy(entry_index, e->entry, e->entry->next_offset);
686 entry_index += e->entry->next_offset;
692 static void dump_ip(struct ipt_entry *entry)
694 struct ipt_ip *ip = &entry->ip;
695 char ip_string[INET6_ADDRSTRLEN];
696 char ip_mask[INET6_ADDRSTRLEN];
698 if (strlen(ip->iniface))
699 connman_info("\tin %s", ip->iniface);
701 if (strlen(ip->outiface))
702 connman_info("\tout %s", ip->outiface);
704 if (inet_ntop(AF_INET, &ip->src, ip_string, INET6_ADDRSTRLEN) != NULL &&
705 inet_ntop(AF_INET, &ip->smsk,
706 ip_mask, INET6_ADDRSTRLEN) != NULL)
707 connman_info("\tsrc %s/%s", ip_string, ip_mask);
709 if (inet_ntop(AF_INET, &ip->dst, ip_string, INET6_ADDRSTRLEN) != NULL &&
710 inet_ntop(AF_INET, &ip->dmsk,
711 ip_mask, INET6_ADDRSTRLEN) != NULL)
712 connman_info("\tdst %s/%s", ip_string, ip_mask);
715 static void dump_target(struct connman_iptables *table,
716 struct ipt_entry *entry)
719 struct xtables_target *xt_t;
720 struct xt_entry_target *target;
722 target = ipt_get_target(entry);
724 if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
725 struct xt_standard_target *t;
727 t = (struct xt_standard_target *)target;
729 switch (t->verdict) {
731 connman_info("\ttarget RETURN");
735 connman_info("\ttarget ACCEPT");
739 connman_info("\ttarget DROP");
743 connman_info("\ttarget QUEUE");
747 connman_info("\ttarget STOP");
751 connman_info("\tJUMP @%p (0x%x)",
752 (char*)table->blob_entries->entrytable +
753 t->verdict, t->verdict);
757 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
758 XTF_LOAD_MUST_SUCCEED);
760 if(xt_t->print != NULL)
761 xt_t->print(NULL, target, 1);
763 xt_t = xtables_find_target(target->u.user.name, XTF_TRY_LOAD);
765 connman_info("\ttarget %s", target->u.user.name);
769 if(xt_t->print != NULL) {
770 connman_info("\ttarget ");
771 xt_t->print(NULL, target, 1);
776 static void dump_match(struct connman_iptables *table, struct ipt_entry *entry)
778 struct xtables_match *xt_m;
779 struct xt_entry_match *match;
781 if (entry->elems == (unsigned char *)entry + entry->target_offset)
784 match = (struct xt_entry_match *) entry->elems;
786 if (!strlen(match->u.user.name))
789 xt_m = xtables_find_match(match->u.user.name, XTF_TRY_LOAD, NULL);
793 if(xt_m->print != NULL) {
794 connman_info("\tmatch ");
795 xt_m->print(NULL, match, 1);
801 connman_info("\tmatch %s", match->u.user.name);
805 static int dump_entry(struct ipt_entry *entry,
806 struct connman_iptables *table)
808 struct xt_entry_target *target;
812 offset = (char *)entry - (char *)table->blob_entries->entrytable;
813 target = ipt_get_target(entry);
814 builtin = is_hook_entry(table, entry);
816 if (entry_to_offset(table, entry) + entry->next_offset ==
817 table->blob_entries->size) {
818 connman_info("End of CHAIN 0x%x", offset);
822 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET)) {
823 connman_info("USER CHAIN (%s) %p match %p target %p size %d",
824 target->data, entry, entry->elems,
825 (char *)entry + entry->target_offset,
829 } else if (builtin >= 0) {
830 connman_info("CHAIN (%s) %p match %p target %p size %d",
831 hooknames[builtin], entry, entry->elems,
832 (char *)entry + entry->target_offset,
835 connman_info("RULE %p match %p target %p size %d", entry,
837 (char *)entry + entry->target_offset,
841 dump_match(table, entry);
842 dump_target(table, entry);
848 static void iptables_dump(struct connman_iptables *table)
850 connman_info("%s valid_hooks=0x%08x, num_entries=%u, size=%u",
852 table->info->valid_hooks, table->info->num_entries,
855 ENTRY_ITERATE(table->blob_entries->entrytable,
856 table->blob_entries->size,
861 static int iptables_get_entries(struct connman_iptables *table)
863 socklen_t entry_size;
865 entry_size = sizeof(struct ipt_get_entries) + table->info->size;
867 return getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_ENTRIES,
868 table->blob_entries, &entry_size);
871 static int iptables_replace(struct connman_iptables *table,
872 struct ipt_replace *r)
874 return setsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_SET_REPLACE, r,
875 sizeof(*r) + r->size);
878 static int add_entry(struct ipt_entry *entry, struct connman_iptables *table)
880 struct ipt_entry *new_entry;
883 new_entry = g_try_malloc0(entry->next_offset);
884 if (new_entry == NULL)
887 memcpy(new_entry, entry, entry->next_offset);
889 builtin = is_hook_entry(table, entry);
891 return iptables_add_entry(table, new_entry, NULL, builtin);
894 static void table_cleanup(struct connman_iptables *table)
897 struct connman_iptables_entry *entry;
899 close(table->ipt_sock);
901 for (list = table->entries; list; list = list->next) {
904 g_free(entry->entry);
908 g_list_free(table->entries);
910 g_free(table->blob_entries);
914 static struct connman_iptables *iptables_init(char *table_name)
916 struct connman_iptables *table;
919 DBG("%s", table_name);
921 table = g_hash_table_lookup(table_hash, table_name);
925 table = g_try_new0(struct connman_iptables, 1);
929 table->info = g_try_new0(struct ipt_getinfo, 1);
930 if (table->info == NULL)
933 table->ipt_sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
934 if (table->ipt_sock < 0)
937 s = sizeof(*table->info);
938 strcpy(table->info->name, table_name);
939 if (getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_INFO,
940 table->info, &s) < 0)
943 table->blob_entries = g_try_malloc0(sizeof(struct ipt_get_entries) +
945 if (table->blob_entries == NULL)
948 strcpy(table->blob_entries->name, table_name);
949 table->blob_entries->size = table->info->size;
951 if (iptables_get_entries(table) < 0)
954 table->num_entries = 0;
955 table->old_entries = table->info->num_entries;
958 memcpy(table->underflow, table->info->underflow,
959 sizeof(table->info->underflow));
960 memcpy(table->hook_entry, table->info->hook_entry,
961 sizeof(table->info->hook_entry));
963 ENTRY_ITERATE(table->blob_entries->entrytable,
964 table->blob_entries->size,
967 g_hash_table_insert(table_hash, g_strdup(table_name), table);
973 table_cleanup(table);
978 static struct option iptables_opts[] = {
979 {.name = "append", .has_arg = 1, .val = 'A'},
980 {.name = "flush-chain", .has_arg = 1, .val = 'F'},
981 {.name = "list", .has_arg = 2, .val = 'L'},
982 {.name = "new-chain", .has_arg = 1, .val = 'N'},
983 {.name = "destination", .has_arg = 1, .val = 'd'},
984 {.name = "in-interface", .has_arg = 1, .val = 'i'},
985 {.name = "jump", .has_arg = 1, .val = 'j'},
986 {.name = "match", .has_arg = 1, .val = 'm'},
987 {.name = "out-interface", .has_arg = 1, .val = 'o'},
988 {.name = "source", .has_arg = 1, .val = 's'},
989 {.name = "table", .has_arg = 1, .val = 't'},
993 struct xtables_globals iptables_globals = {
995 .opts = iptables_opts,
996 .orig_opts = iptables_opts,
999 static int iptables_command(int argc, char *argv[])
1001 struct connman_iptables *table;
1002 struct xtables_match *xt_m;
1003 struct xtables_target *xt_t;
1005 char *table_name, *chain, *new_chain, *match_name, *target_name;
1007 int c, ret, in_len, out_len;
1009 gboolean dump, invert;
1010 struct in_addr src, dst;
1017 table_name = chain = new_chain = match_name = target_name = NULL;
1019 memset(&ip, 0, sizeof(struct ipt_ip));
1027 while ((c = getopt_long(argc, argv,
1028 "-A:F:L::N:d:j:i:m:o:s:t:", iptables_globals.opts, NULL)) != -1) {
1035 flush_chain = optarg;
1047 if (!inet_pton(AF_INET, optarg, &dst))
1051 inet_pton(AF_INET, "255.255.255.255", &ip.dmsk);
1054 ip.invflags |= IPT_INV_DSTIP;
1059 in_len = strlen(optarg);
1061 if (in_len + 1 > IFNAMSIZ)
1064 strcpy(ip.iniface, optarg);
1065 memset(ip.iniface_mask, 0xff, in_len + 1);
1068 ip.invflags |= IPT_INV_VIA_IN;
1073 target_name = optarg;
1074 xt_t = xtables_find_target(target_name, XTF_TRY_LOAD);
1079 size = ALIGN(sizeof(struct ipt_entry_target)) +
1082 xt_t->t = g_try_malloc0(size);
1083 if (xt_t->t == NULL)
1085 xt_t->t->u.target_size = size;
1086 strcpy(xt_t->t->u.user.name, target_name);
1087 xt_t->t->u.user.revision = xt_t->revision;
1088 if (xt_t->init != NULL)
1089 xt_t->init(xt_t->t);
1090 iptables_globals.opts =
1091 xtables_merge_options(iptables_globals.opts,
1093 &xt_t->option_offset);
1094 if (iptables_globals.opts == NULL)
1100 match_name = optarg;
1102 xt_m = xtables_find_match(optarg, XTF_LOAD_MUST_SUCCEED, NULL);
1103 size = ALIGN(sizeof(struct ipt_entry_match)) +
1105 xt_m->m = g_try_malloc0(size);
1108 xt_m->m->u.match_size = size;
1109 strcpy(xt_m->m->u.user.name, xt_m->name);
1110 xt_m->m->u.user.revision = xt_m->revision;
1111 if (xt_m->init != NULL)
1112 xt_m->init(xt_m->m);
1113 if (xt_m != xt_m->next) {
1114 iptables_globals.opts =
1115 xtables_merge_options(iptables_globals.opts,
1117 &xt_m->option_offset);
1118 if (iptables_globals.opts == NULL)
1125 out_len = strlen(optarg);
1127 if (out_len + 1 > IFNAMSIZ)
1130 strcpy(ip.outiface, optarg);
1131 memset(ip.outiface_mask, 0xff, out_len + 1);
1134 ip.invflags |= IPT_INV_VIA_OUT;
1139 if (!inet_pton(AF_INET, optarg, &src))
1143 inet_pton(AF_INET, "255.255.255.255", &ip.smsk);
1146 ip.invflags |= IPT_INV_SRCIP;
1151 table_name = optarg;
1155 if (optarg[0] == '!' && optarg[1] == '\0') {
1161 connman_error("Invalid option");
1167 if (xt_t == NULL || xt_t->parse == NULL ||
1168 !xt_t->parse(c - xt_t->option_offset, argv, invert,
1169 &xt_t->tflags, NULL, &xt_t->t)) {
1170 if (xt_m == NULL || xt_m->parse == NULL)
1173 xt_m->parse(c - xt_m->option_offset, argv,
1174 invert, &xt_m->mflags, NULL, &xt_m->m);
1183 if (table_name == NULL)
1184 table_name = "filter";
1186 table = iptables_init(table_name);
1187 if (table == NULL) {
1193 iptables_dump(table);
1200 DBG("Flush chain %s", flush_chain);
1202 iptables_flush_chain(table, flush_chain);
1207 if (chain && new_chain) {
1213 DBG("New chain %s", new_chain);
1215 ret = iptables_add_chain(table, new_chain);
1220 if (target_name == NULL)
1223 DBG("Adding %s to %s (match %s)",
1224 target_name, chain, match_name);
1226 ret = iptables_add_rule(table, &ip, chain, target_name, xt_t,
1242 int __connman_iptables_command(const char *format, ...)
1244 char **argv, **arguments, *command;
1251 va_start(args, format);
1253 command = g_strdup_vprintf(format, args);
1257 if (command == NULL)
1260 arguments = g_strsplit_set(command, " ", -1);
1262 for (argc = 0; arguments[argc]; argc++);
1265 DBG("command %s argc %d", command, argc);
1267 argv = g_try_malloc0(argc * sizeof(char *));
1270 g_strfreev(arguments);
1274 argv[0] = "iptables";
1275 for (i = 1; i < argc; i++)
1276 argv[i] = arguments[i - 1];
1278 ret = iptables_command(argc, argv);
1281 g_strfreev(arguments);
1288 int __connman_iptables_commit(const char *table_name)
1290 struct connman_iptables *table;
1291 struct ipt_replace *repl;
1294 DBG("%s", table_name);
1296 table = g_hash_table_lookup(table_hash, table_name);
1300 repl = iptables_blob(table);
1302 err = iptables_replace(table, repl);
1304 g_free(repl->counters);
1310 g_hash_table_remove(table_hash, table_name);
1315 static void remove_table(gpointer user_data)
1317 struct connman_iptables *table = user_data;
1319 table_cleanup(table);
1322 int __connman_iptables_init(void)
1326 table_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1327 g_free, remove_table);
1329 xtables_init_all(&iptables_globals, NFPROTO_IPV4);
1335 void __connman_iptables_cleanup(void)
1337 g_hash_table_destroy(table_hash);
1339 xtables_free_opts(1);