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)
491 g_free(entry_return);
498 static struct ipt_entry *
499 new_rule(struct connman_iptables *table, struct ipt_ip *ip,
500 char *target_name, struct xtables_target *xt_t,
501 char *match_name, struct xtables_match *xt_m)
503 struct ipt_entry *new_entry;
504 size_t match_size, target_size;
505 int is_builtin = is_builtin_target(target_name);
508 match_size = xt_m->m->u.match_size;
513 target_size = ALIGN(xt_t->t->u.target_size);
515 target_size = ALIGN(sizeof(struct xt_standard_target));
517 new_entry = g_try_malloc0(sizeof(struct ipt_entry) + target_size +
519 if (new_entry == NULL)
522 memcpy(&new_entry->ip, ip, sizeof(struct ipt_ip));
524 new_entry->target_offset = sizeof(struct ipt_entry) + match_size;
525 new_entry->next_offset = sizeof(struct ipt_entry) + target_size +
528 struct xt_entry_match *entry_match;
530 entry_match = (struct xt_entry_match *)new_entry->elems;
531 memcpy(entry_match, xt_m->m, match_size);
535 struct xt_entry_target *entry_target;
538 struct xt_standard_target *target;
540 target = (struct xt_standard_target *)(xt_t->t);
541 strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
542 target->verdict = target_to_verdict(target_name);
545 entry_target = ipt_get_target(new_entry);
546 memcpy(entry_target, xt_t->t, target_size);
548 struct connman_iptables_entry *target_rule;
549 struct xt_standard_target *target;
553 * This is a user defined target, i.e. a chain jump.
554 * We search for the chain head, and the target verdict
555 * is the first rule's offset on this chain.
556 * The offset is from the beginning of the table.
559 chain_head = find_chain_head(table, target_name);
560 if (chain_head == NULL || chain_head->next == NULL) {
565 target_rule = chain_head->next->data;
567 target = (struct xt_standard_target *)ipt_get_target(new_entry);
568 strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
569 target->target.u.user.target_size = target_size;
570 target->verdict = target_rule->offset;
576 static void update_hooks(struct connman_iptables *table, GList *chain_head,
577 struct ipt_entry *entry)
580 struct connman_iptables_entry *head, *e;
583 if (chain_head == NULL)
586 head = chain_head->data;
588 builtin = head->builtin;
592 table->underflow[builtin] += entry->next_offset;
594 for (list = chain_head->next; list; list = list->next) {
597 builtin = e->builtin;
601 table->hook_entry[builtin] += entry->next_offset;
602 table->underflow[builtin] += entry->next_offset;
607 iptables_add_rule(struct connman_iptables *table,
608 struct ipt_ip *ip, char *chain_name,
609 char *target_name, struct xtables_target *xt_t,
610 char *match_name, struct xtables_match *xt_m)
612 GList *chain_tail, *chain_head;
613 struct ipt_entry *new_entry;
614 struct connman_iptables_entry *head;
619 chain_head = find_chain_head(table, chain_name);
620 if (chain_head == NULL)
623 chain_tail = find_chain_tail(table, chain_name);
624 if (chain_tail == NULL)
627 new_entry = new_rule(table, ip,
630 if (new_entry == NULL)
633 update_hooks(table, chain_head, new_entry);
636 * If the chain is builtin, and does not have any rule,
637 * then the one that we're inserting is becoming the head
638 * and thus needs the builtin flag.
640 head = chain_head->data;
641 if (head->builtin < 0)
643 else if (chain_head == chain_tail->prev) {
644 builtin = head->builtin;
648 return iptables_add_entry(table, new_entry, chain_tail->prev, builtin);
651 static struct ipt_replace *
652 iptables_blob(struct connman_iptables *table)
654 struct ipt_replace *r;
656 struct connman_iptables_entry *e;
657 unsigned char *entry_index;
659 r = g_try_malloc0(sizeof(struct ipt_replace) + table->size);
663 memset(r, 0, sizeof(*r) + table->size);
665 r->counters = g_try_malloc0(sizeof(struct xt_counters)
666 * table->old_entries);
667 if (r->counters == NULL) {
672 strcpy(r->name, table->info->name);
673 r->num_entries = table->num_entries;
674 r->size = table->size;
676 r->num_counters = table->old_entries;
677 r->valid_hooks = table->info->valid_hooks;
679 memcpy(r->hook_entry, table->hook_entry, sizeof(table->hook_entry));
680 memcpy(r->underflow, table->underflow, sizeof(table->underflow));
682 entry_index = (unsigned char *)r->entries;
683 for (list = table->entries; list; list = list->next) {
686 memcpy(entry_index, e->entry, e->entry->next_offset);
687 entry_index += e->entry->next_offset;
693 static void dump_ip(struct ipt_entry *entry)
695 struct ipt_ip *ip = &entry->ip;
696 char ip_string[INET6_ADDRSTRLEN];
697 char ip_mask[INET6_ADDRSTRLEN];
699 if (strlen(ip->iniface))
700 connman_info("\tin %s", ip->iniface);
702 if (strlen(ip->outiface))
703 connman_info("\tout %s", ip->outiface);
705 if (inet_ntop(AF_INET, &ip->src, ip_string, INET6_ADDRSTRLEN) != NULL &&
706 inet_ntop(AF_INET, &ip->smsk,
707 ip_mask, INET6_ADDRSTRLEN) != NULL)
708 connman_info("\tsrc %s/%s", ip_string, ip_mask);
710 if (inet_ntop(AF_INET, &ip->dst, ip_string, INET6_ADDRSTRLEN) != NULL &&
711 inet_ntop(AF_INET, &ip->dmsk,
712 ip_mask, INET6_ADDRSTRLEN) != NULL)
713 connman_info("\tdst %s/%s", ip_string, ip_mask);
716 static void dump_target(struct connman_iptables *table,
717 struct ipt_entry *entry)
720 struct xtables_target *xt_t;
721 struct xt_entry_target *target;
723 target = ipt_get_target(entry);
725 if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
726 struct xt_standard_target *t;
728 t = (struct xt_standard_target *)target;
730 switch (t->verdict) {
732 connman_info("\ttarget RETURN");
736 connman_info("\ttarget ACCEPT");
740 connman_info("\ttarget DROP");
744 connman_info("\ttarget QUEUE");
748 connman_info("\ttarget STOP");
752 connman_info("\tJUMP @%p (0x%x)",
753 (char*)table->blob_entries->entrytable +
754 t->verdict, t->verdict);
758 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
759 XTF_LOAD_MUST_SUCCEED);
761 if(xt_t->print != NULL)
762 xt_t->print(NULL, target, 1);
764 xt_t = xtables_find_target(target->u.user.name, XTF_TRY_LOAD);
766 connman_info("\ttarget %s", target->u.user.name);
770 if(xt_t->print != NULL) {
771 connman_info("\ttarget ");
772 xt_t->print(NULL, target, 1);
777 static void dump_match(struct connman_iptables *table, struct ipt_entry *entry)
779 struct xtables_match *xt_m;
780 struct xt_entry_match *match;
782 if (entry->elems == (unsigned char *)entry + entry->target_offset)
785 match = (struct xt_entry_match *) entry->elems;
787 if (!strlen(match->u.user.name))
790 xt_m = xtables_find_match(match->u.user.name, XTF_TRY_LOAD, NULL);
794 if(xt_m->print != NULL) {
795 connman_info("\tmatch ");
796 xt_m->print(NULL, match, 1);
802 connman_info("\tmatch %s", match->u.user.name);
806 static int dump_entry(struct ipt_entry *entry,
807 struct connman_iptables *table)
809 struct xt_entry_target *target;
813 offset = (char *)entry - (char *)table->blob_entries->entrytable;
814 target = ipt_get_target(entry);
815 builtin = is_hook_entry(table, entry);
817 if (entry_to_offset(table, entry) + entry->next_offset ==
818 table->blob_entries->size) {
819 connman_info("End of CHAIN 0x%x", offset);
823 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET)) {
824 connman_info("USER CHAIN (%s) %p match %p target %p size %d",
825 target->data, entry, entry->elems,
826 (char *)entry + entry->target_offset,
830 } else if (builtin >= 0) {
831 connman_info("CHAIN (%s) %p match %p target %p size %d",
832 hooknames[builtin], entry, entry->elems,
833 (char *)entry + entry->target_offset,
836 connman_info("RULE %p match %p target %p size %d", entry,
838 (char *)entry + entry->target_offset,
842 dump_match(table, entry);
843 dump_target(table, entry);
849 static void iptables_dump(struct connman_iptables *table)
851 connman_info("%s valid_hooks=0x%08x, num_entries=%u, size=%u",
853 table->info->valid_hooks, table->info->num_entries,
856 ENTRY_ITERATE(table->blob_entries->entrytable,
857 table->blob_entries->size,
862 static int iptables_get_entries(struct connman_iptables *table)
864 socklen_t entry_size;
866 entry_size = sizeof(struct ipt_get_entries) + table->info->size;
868 return getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_ENTRIES,
869 table->blob_entries, &entry_size);
872 static int iptables_replace(struct connman_iptables *table,
873 struct ipt_replace *r)
875 return setsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_SET_REPLACE, r,
876 sizeof(*r) + r->size);
879 static int add_entry(struct ipt_entry *entry, struct connman_iptables *table)
881 struct ipt_entry *new_entry;
884 new_entry = g_try_malloc0(entry->next_offset);
885 if (new_entry == NULL)
888 memcpy(new_entry, entry, entry->next_offset);
890 builtin = is_hook_entry(table, entry);
892 return iptables_add_entry(table, new_entry, NULL, builtin);
895 static void table_cleanup(struct connman_iptables *table)
898 struct connman_iptables_entry *entry;
900 close(table->ipt_sock);
902 for (list = table->entries; list; list = list->next) {
905 g_free(entry->entry);
909 g_list_free(table->entries);
911 g_free(table->blob_entries);
915 static struct connman_iptables *iptables_init(char *table_name)
917 struct connman_iptables *table;
920 DBG("%s", table_name);
922 table = g_hash_table_lookup(table_hash, table_name);
926 table = g_try_new0(struct connman_iptables, 1);
930 table->info = g_try_new0(struct ipt_getinfo, 1);
931 if (table->info == NULL)
934 table->ipt_sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
935 if (table->ipt_sock < 0)
938 s = sizeof(*table->info);
939 strcpy(table->info->name, table_name);
940 if (getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_INFO,
941 table->info, &s) < 0)
944 table->blob_entries = g_try_malloc0(sizeof(struct ipt_get_entries) +
946 if (table->blob_entries == NULL)
949 strcpy(table->blob_entries->name, table_name);
950 table->blob_entries->size = table->info->size;
952 if (iptables_get_entries(table) < 0)
955 table->num_entries = 0;
956 table->old_entries = table->info->num_entries;
959 memcpy(table->underflow, table->info->underflow,
960 sizeof(table->info->underflow));
961 memcpy(table->hook_entry, table->info->hook_entry,
962 sizeof(table->info->hook_entry));
964 ENTRY_ITERATE(table->blob_entries->entrytable,
965 table->blob_entries->size,
968 g_hash_table_insert(table_hash, g_strdup(table_name), table);
974 table_cleanup(table);
979 static struct option iptables_opts[] = {
980 {.name = "append", .has_arg = 1, .val = 'A'},
981 {.name = "flush-chain", .has_arg = 1, .val = 'F'},
982 {.name = "list", .has_arg = 2, .val = 'L'},
983 {.name = "new-chain", .has_arg = 1, .val = 'N'},
984 {.name = "destination", .has_arg = 1, .val = 'd'},
985 {.name = "in-interface", .has_arg = 1, .val = 'i'},
986 {.name = "jump", .has_arg = 1, .val = 'j'},
987 {.name = "match", .has_arg = 1, .val = 'm'},
988 {.name = "out-interface", .has_arg = 1, .val = 'o'},
989 {.name = "source", .has_arg = 1, .val = 's'},
990 {.name = "table", .has_arg = 1, .val = 't'},
994 struct xtables_globals iptables_globals = {
996 .opts = iptables_opts,
997 .orig_opts = iptables_opts,
1000 static int iptables_command(int argc, char *argv[])
1002 struct connman_iptables *table;
1003 struct xtables_match *xt_m;
1004 struct xtables_target *xt_t;
1006 char *table_name, *chain, *new_chain, *match_name, *target_name;
1008 int c, ret, in_len, out_len;
1010 gboolean dump, invert;
1011 struct in_addr src, dst;
1018 table_name = chain = new_chain = match_name = target_name = NULL;
1020 memset(&ip, 0, sizeof(struct ipt_ip));
1028 while ((c = getopt_long(argc, argv,
1029 "-A:F:L::N:d:j:i:m:o:s:t:", iptables_globals.opts, NULL)) != -1) {
1036 flush_chain = optarg;
1048 if (!inet_pton(AF_INET, optarg, &dst))
1052 inet_pton(AF_INET, "255.255.255.255", &ip.dmsk);
1055 ip.invflags |= IPT_INV_DSTIP;
1060 in_len = strlen(optarg);
1062 if (in_len + 1 > IFNAMSIZ)
1065 strcpy(ip.iniface, optarg);
1066 memset(ip.iniface_mask, 0xff, in_len + 1);
1069 ip.invflags |= IPT_INV_VIA_IN;
1074 target_name = optarg;
1075 xt_t = xtables_find_target(target_name, XTF_TRY_LOAD);
1080 size = ALIGN(sizeof(struct ipt_entry_target)) +
1083 xt_t->t = g_try_malloc0(size);
1084 if (xt_t->t == NULL)
1086 xt_t->t->u.target_size = size;
1087 strcpy(xt_t->t->u.user.name, target_name);
1088 xt_t->t->u.user.revision = xt_t->revision;
1089 if (xt_t->init != NULL)
1090 xt_t->init(xt_t->t);
1091 iptables_globals.opts =
1092 xtables_merge_options(iptables_globals.opts,
1094 &xt_t->option_offset);
1095 if (iptables_globals.opts == NULL)
1101 match_name = optarg;
1103 xt_m = xtables_find_match(optarg, XTF_LOAD_MUST_SUCCEED, NULL);
1104 size = ALIGN(sizeof(struct ipt_entry_match)) +
1106 xt_m->m = g_try_malloc0(size);
1109 xt_m->m->u.match_size = size;
1110 strcpy(xt_m->m->u.user.name, xt_m->name);
1111 xt_m->m->u.user.revision = xt_m->revision;
1112 if (xt_m->init != NULL)
1113 xt_m->init(xt_m->m);
1114 if (xt_m != xt_m->next) {
1115 iptables_globals.opts =
1116 xtables_merge_options(iptables_globals.opts,
1118 &xt_m->option_offset);
1119 if (iptables_globals.opts == NULL)
1126 out_len = strlen(optarg);
1128 if (out_len + 1 > IFNAMSIZ)
1131 strcpy(ip.outiface, optarg);
1132 memset(ip.outiface_mask, 0xff, out_len + 1);
1135 ip.invflags |= IPT_INV_VIA_OUT;
1140 if (!inet_pton(AF_INET, optarg, &src))
1144 inet_pton(AF_INET, "255.255.255.255", &ip.smsk);
1147 ip.invflags |= IPT_INV_SRCIP;
1152 table_name = optarg;
1156 if (optarg[0] == '!' && optarg[1] == '\0') {
1162 connman_error("Invalid option");
1168 if (xt_t == NULL || xt_t->parse == NULL ||
1169 !xt_t->parse(c - xt_t->option_offset, argv, invert,
1170 &xt_t->tflags, NULL, &xt_t->t)) {
1171 if (xt_m == NULL || xt_m->parse == NULL)
1174 xt_m->parse(c - xt_m->option_offset, argv,
1175 invert, &xt_m->mflags, NULL, &xt_m->m);
1184 if (table_name == NULL)
1185 table_name = "filter";
1187 table = iptables_init(table_name);
1188 if (table == NULL) {
1194 iptables_dump(table);
1201 DBG("Flush chain %s", flush_chain);
1203 iptables_flush_chain(table, flush_chain);
1208 if (chain && new_chain) {
1214 DBG("New chain %s", new_chain);
1216 ret = iptables_add_chain(table, new_chain);
1221 if (target_name == NULL)
1224 DBG("Adding %s to %s (match %s)",
1225 target_name, chain, match_name);
1227 ret = iptables_add_rule(table, &ip, chain, target_name, xt_t,
1243 int __connman_iptables_command(const char *format, ...)
1245 char **argv, **arguments, *command;
1252 va_start(args, format);
1254 command = g_strdup_vprintf(format, args);
1258 if (command == NULL)
1261 arguments = g_strsplit_set(command, " ", -1);
1263 for (argc = 0; arguments[argc]; argc++);
1266 DBG("command %s argc %d", command, argc);
1268 argv = g_try_malloc0(argc * sizeof(char *));
1271 g_strfreev(arguments);
1275 argv[0] = "iptables";
1276 for (i = 1; i < argc; i++)
1277 argv[i] = arguments[i - 1];
1279 ret = iptables_command(argc, argv);
1282 g_strfreev(arguments);
1289 int __connman_iptables_commit(const char *table_name)
1291 struct connman_iptables *table;
1292 struct ipt_replace *repl;
1295 DBG("%s", table_name);
1297 table = g_hash_table_lookup(table_hash, table_name);
1301 repl = iptables_blob(table);
1303 err = iptables_replace(table, repl);
1305 g_free(repl->counters);
1311 g_hash_table_remove(table_hash, table_name);
1316 static void remove_table(gpointer user_data)
1318 struct connman_iptables *table = user_data;
1320 table_cleanup(table);
1323 int __connman_iptables_init(void)
1327 table_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1328 g_free, remove_table);
1330 xtables_init_all(&iptables_globals, NFPROTO_IPV4);
1336 void __connman_iptables_cleanup(void)
1340 g_hash_table_destroy(table_hash);
1342 xtables_free_opts(1);