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 remove_table_entry(struct connman_iptables *table,
339 struct connman_iptables_entry *entry)
343 table->num_entries--;
344 table->size -= entry->entry->next_offset;
345 removed = entry->entry->next_offset;
347 g_free(entry->entry);
349 table->entries = g_list_remove(table->entries, entry);
354 static int iptables_flush_chain(struct connman_iptables *table,
357 GList *chain_head, *chain_tail, *list, *next;
358 struct connman_iptables_entry *entry;
359 int builtin, removed = 0;
361 chain_head = find_chain_head(table, name);
362 if (chain_head == NULL)
365 chain_tail = find_chain_tail(table, name);
366 if (chain_tail == NULL)
369 entry = chain_head->data;
370 builtin = entry->builtin;
375 list = chain_head->next;
377 if (list == chain_tail->prev)
380 while (list != chain_tail->prev) {
382 next = g_list_next(list);
384 removed += remove_table_entry(table, entry);
390 struct connman_iptables_entry *e;
394 entry->builtin = builtin;
396 table->underflow[builtin] -= removed;
398 for (list = chain_tail; list; list = list->next) {
401 builtin = e->builtin;
405 table->hook_entry[builtin] -= removed;
406 table->underflow[builtin] -= removed;
410 update_offsets(table);
415 static int iptables_add_chain(struct connman_iptables *table,
419 struct ipt_entry *entry_head;
420 struct ipt_entry *entry_return;
421 struct error_target *error;
422 struct ipt_standard_target *standard;
423 u_int16_t entry_head_size, entry_return_size;
425 last = g_list_last(table->entries);
428 * An empty chain is composed of:
429 * - A head entry, with no match and an error target.
430 * The error target data is the chain name.
431 * - A tail entry, with no match and a standard target.
432 * The standard target verdict is XT_RETURN (return to the
437 entry_head_size = sizeof(struct ipt_entry) +
438 sizeof(struct error_target);
439 entry_head = g_try_malloc0(entry_head_size);
440 if (entry_head == NULL)
443 memset(entry_head, 0, entry_head_size);
445 entry_head->target_offset = sizeof(struct ipt_entry);
446 entry_head->next_offset = entry_head_size;
448 error = (struct error_target *) entry_head->elems;
449 strcpy(error->t.u.user.name, IPT_ERROR_TARGET);
450 error->t.u.user.target_size = ALIGN(sizeof(struct error_target));
451 strcpy(error->error, name);
453 if (iptables_add_entry(table, entry_head, last, -1) < 0)
457 entry_return_size = sizeof(struct ipt_entry) +
458 sizeof(struct ipt_standard_target);
459 entry_return = g_try_malloc0(entry_return_size);
460 if (entry_return == NULL)
463 memset(entry_return, 0, entry_return_size);
465 entry_return->target_offset = sizeof(struct ipt_entry);
466 entry_return->next_offset = entry_return_size;
468 standard = (struct ipt_standard_target *) entry_return->elems;
469 standard->target.u.user.target_size =
470 ALIGN(sizeof(struct ipt_standard_target));
471 standard->verdict = XT_RETURN;
473 if (iptables_add_entry(table, entry_return, last, -1) < 0)
479 g_free(entry_return);
486 static int iptables_delete_chain(struct connman_iptables *table, char *name)
488 struct connman_iptables_entry *entry;
489 GList *chain_head, *chain_tail;
491 chain_head = find_chain_head(table, name);
492 if (chain_head == NULL)
495 entry = chain_head->data;
497 /* We cannot remove builtin chain */
498 if (entry->builtin >= 0)
501 chain_tail = find_chain_tail(table, name);
502 if (chain_tail == NULL)
505 /* Chain must be flushed */
506 if (chain_head->next != chain_tail->prev)
509 remove_table_entry(table, entry);
511 entry = chain_tail->prev->data;
512 remove_table_entry(table, entry);
514 update_offsets(table);
519 static struct ipt_entry *
520 new_rule(struct connman_iptables *table, struct ipt_ip *ip,
521 char *target_name, struct xtables_target *xt_t,
522 char *match_name, struct xtables_match *xt_m)
524 struct ipt_entry *new_entry;
525 size_t match_size, target_size;
526 int is_builtin = is_builtin_target(target_name);
529 match_size = xt_m->m->u.match_size;
534 target_size = ALIGN(xt_t->t->u.target_size);
536 target_size = ALIGN(sizeof(struct xt_standard_target));
538 new_entry = g_try_malloc0(sizeof(struct ipt_entry) + target_size +
540 if (new_entry == NULL)
543 memcpy(&new_entry->ip, ip, sizeof(struct ipt_ip));
545 new_entry->target_offset = sizeof(struct ipt_entry) + match_size;
546 new_entry->next_offset = sizeof(struct ipt_entry) + target_size +
549 struct xt_entry_match *entry_match;
551 entry_match = (struct xt_entry_match *)new_entry->elems;
552 memcpy(entry_match, xt_m->m, match_size);
556 struct xt_entry_target *entry_target;
559 struct xt_standard_target *target;
561 target = (struct xt_standard_target *)(xt_t->t);
562 strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
563 target->verdict = target_to_verdict(target_name);
566 entry_target = ipt_get_target(new_entry);
567 memcpy(entry_target, xt_t->t, target_size);
569 struct connman_iptables_entry *target_rule;
570 struct xt_standard_target *target;
574 * This is a user defined target, i.e. a chain jump.
575 * We search for the chain head, and the target verdict
576 * is the first rule's offset on this chain.
577 * The offset is from the beginning of the table.
580 chain_head = find_chain_head(table, target_name);
581 if (chain_head == NULL || chain_head->next == NULL) {
586 target_rule = chain_head->next->data;
588 target = (struct xt_standard_target *)ipt_get_target(new_entry);
589 strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
590 target->target.u.user.target_size = target_size;
591 target->verdict = target_rule->offset;
597 static void update_hooks(struct connman_iptables *table, GList *chain_head,
598 struct ipt_entry *entry)
601 struct connman_iptables_entry *head, *e;
604 if (chain_head == NULL)
607 head = chain_head->data;
609 builtin = head->builtin;
613 table->underflow[builtin] += entry->next_offset;
615 for (list = chain_head->next; list; list = list->next) {
618 builtin = e->builtin;
622 table->hook_entry[builtin] += entry->next_offset;
623 table->underflow[builtin] += entry->next_offset;
627 static struct ipt_entry *prepare_rule_inclusion(struct connman_iptables *table,
628 struct ipt_ip *ip, char *chain_name,
629 char *target_name, struct xtables_target *xt_t,
630 char *match_name, struct xtables_match *xt_m,
633 GList *chain_tail, *chain_head;
634 struct ipt_entry *new_entry;
635 struct connman_iptables_entry *head;
637 chain_head = find_chain_head(table, chain_name);
638 if (chain_head == NULL)
641 chain_tail = find_chain_tail(table, chain_name);
642 if (chain_tail == NULL)
645 new_entry = new_rule(table, ip, target_name, xt_t, match_name, xt_m);
646 if (new_entry == NULL)
649 update_hooks(table, chain_head, new_entry);
652 * If the chain is builtin, and does not have any rule,
653 * then the one that we're inserting is becoming the head
654 * and thus needs the builtin flag.
656 head = chain_head->data;
657 if (head->builtin < 0)
659 else if (chain_head == chain_tail->prev) {
660 *builtin = head->builtin;
668 iptables_add_rule(struct connman_iptables *table,
669 struct ipt_ip *ip, char *chain_name,
670 char *target_name, struct xtables_target *xt_t,
671 char *match_name, struct xtables_match *xt_m)
674 struct ipt_entry *new_entry;
675 int builtin = -1, ret;
679 chain_tail = find_chain_tail(table, chain_name);
680 if (chain_tail == NULL)
683 new_entry = prepare_rule_inclusion(table, ip, chain_name,
684 target_name, xt_t, match_name, xt_m, &builtin);
685 if (new_entry == NULL)
688 ret = iptables_add_entry(table, new_entry, chain_tail->prev, builtin);
695 static struct ipt_replace *
696 iptables_blob(struct connman_iptables *table)
698 struct ipt_replace *r;
700 struct connman_iptables_entry *e;
701 unsigned char *entry_index;
703 r = g_try_malloc0(sizeof(struct ipt_replace) + table->size);
707 memset(r, 0, sizeof(*r) + table->size);
709 r->counters = g_try_malloc0(sizeof(struct xt_counters)
710 * table->old_entries);
711 if (r->counters == NULL) {
716 strcpy(r->name, table->info->name);
717 r->num_entries = table->num_entries;
718 r->size = table->size;
720 r->num_counters = table->old_entries;
721 r->valid_hooks = table->info->valid_hooks;
723 memcpy(r->hook_entry, table->hook_entry, sizeof(table->hook_entry));
724 memcpy(r->underflow, table->underflow, sizeof(table->underflow));
726 entry_index = (unsigned char *)r->entries;
727 for (list = table->entries; list; list = list->next) {
730 memcpy(entry_index, e->entry, e->entry->next_offset);
731 entry_index += e->entry->next_offset;
737 static void dump_ip(struct ipt_entry *entry)
739 struct ipt_ip *ip = &entry->ip;
740 char ip_string[INET6_ADDRSTRLEN];
741 char ip_mask[INET6_ADDRSTRLEN];
743 if (strlen(ip->iniface))
744 connman_info("\tin %s", ip->iniface);
746 if (strlen(ip->outiface))
747 connman_info("\tout %s", ip->outiface);
749 if (inet_ntop(AF_INET, &ip->src, ip_string, INET6_ADDRSTRLEN) != NULL &&
750 inet_ntop(AF_INET, &ip->smsk,
751 ip_mask, INET6_ADDRSTRLEN) != NULL)
752 connman_info("\tsrc %s/%s", ip_string, ip_mask);
754 if (inet_ntop(AF_INET, &ip->dst, ip_string, INET6_ADDRSTRLEN) != NULL &&
755 inet_ntop(AF_INET, &ip->dmsk,
756 ip_mask, INET6_ADDRSTRLEN) != NULL)
757 connman_info("\tdst %s/%s", ip_string, ip_mask);
760 static void dump_target(struct connman_iptables *table,
761 struct ipt_entry *entry)
764 struct xtables_target *xt_t;
765 struct xt_entry_target *target;
767 target = ipt_get_target(entry);
769 if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
770 struct xt_standard_target *t;
772 t = (struct xt_standard_target *)target;
774 switch (t->verdict) {
776 connman_info("\ttarget RETURN");
780 connman_info("\ttarget ACCEPT");
784 connman_info("\ttarget DROP");
788 connman_info("\ttarget QUEUE");
792 connman_info("\ttarget STOP");
796 connman_info("\tJUMP @%p (0x%x)",
797 (char*)table->blob_entries->entrytable +
798 t->verdict, t->verdict);
802 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
803 XTF_LOAD_MUST_SUCCEED);
805 if(xt_t->print != NULL)
806 xt_t->print(NULL, target, 1);
808 xt_t = xtables_find_target(target->u.user.name, XTF_TRY_LOAD);
810 connman_info("\ttarget %s", target->u.user.name);
814 if(xt_t->print != NULL) {
815 connman_info("\ttarget ");
816 xt_t->print(NULL, target, 1);
821 static void dump_match(struct connman_iptables *table, struct ipt_entry *entry)
823 struct xtables_match *xt_m;
824 struct xt_entry_match *match;
826 if (entry->elems == (unsigned char *)entry + entry->target_offset)
829 match = (struct xt_entry_match *) entry->elems;
831 if (!strlen(match->u.user.name))
834 xt_m = xtables_find_match(match->u.user.name, XTF_TRY_LOAD, NULL);
838 if(xt_m->print != NULL) {
839 connman_info("\tmatch ");
840 xt_m->print(NULL, match, 1);
846 connman_info("\tmatch %s", match->u.user.name);
850 static int dump_entry(struct ipt_entry *entry,
851 struct connman_iptables *table)
853 struct xt_entry_target *target;
857 offset = (char *)entry - (char *)table->blob_entries->entrytable;
858 target = ipt_get_target(entry);
859 builtin = is_hook_entry(table, entry);
861 if (entry_to_offset(table, entry) + entry->next_offset ==
862 table->blob_entries->size) {
863 connman_info("End of CHAIN 0x%x", offset);
867 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET)) {
868 connman_info("USER CHAIN (%s) %p match %p target %p size %d",
869 target->data, entry, entry->elems,
870 (char *)entry + entry->target_offset,
874 } else if (builtin >= 0) {
875 connman_info("CHAIN (%s) %p match %p target %p size %d",
876 hooknames[builtin], entry, entry->elems,
877 (char *)entry + entry->target_offset,
880 connman_info("RULE %p match %p target %p size %d", entry,
882 (char *)entry + entry->target_offset,
886 dump_match(table, entry);
887 dump_target(table, entry);
893 static void iptables_dump(struct connman_iptables *table)
895 connman_info("%s valid_hooks=0x%08x, num_entries=%u, size=%u",
897 table->info->valid_hooks, table->info->num_entries,
900 ENTRY_ITERATE(table->blob_entries->entrytable,
901 table->blob_entries->size,
906 static int iptables_get_entries(struct connman_iptables *table)
908 socklen_t entry_size;
910 entry_size = sizeof(struct ipt_get_entries) + table->info->size;
912 return getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_ENTRIES,
913 table->blob_entries, &entry_size);
916 static int iptables_replace(struct connman_iptables *table,
917 struct ipt_replace *r)
919 return setsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_SET_REPLACE, r,
920 sizeof(*r) + r->size);
923 static int add_entry(struct ipt_entry *entry, struct connman_iptables *table)
925 struct ipt_entry *new_entry;
928 new_entry = g_try_malloc0(entry->next_offset);
929 if (new_entry == NULL)
932 memcpy(new_entry, entry, entry->next_offset);
934 builtin = is_hook_entry(table, entry);
936 return iptables_add_entry(table, new_entry, NULL, builtin);
939 static void table_cleanup(struct connman_iptables *table)
942 struct connman_iptables_entry *entry;
944 close(table->ipt_sock);
946 for (list = table->entries; list; list = list->next) {
949 g_free(entry->entry);
953 g_list_free(table->entries);
955 g_free(table->blob_entries);
959 static struct connman_iptables *iptables_init(char *table_name)
961 struct connman_iptables *table;
964 DBG("%s", table_name);
966 table = g_hash_table_lookup(table_hash, table_name);
970 table = g_try_new0(struct connman_iptables, 1);
974 table->info = g_try_new0(struct ipt_getinfo, 1);
975 if (table->info == NULL)
978 table->ipt_sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
979 if (table->ipt_sock < 0)
982 s = sizeof(*table->info);
983 strcpy(table->info->name, table_name);
984 if (getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_INFO,
985 table->info, &s) < 0)
988 table->blob_entries = g_try_malloc0(sizeof(struct ipt_get_entries) +
990 if (table->blob_entries == NULL)
993 strcpy(table->blob_entries->name, table_name);
994 table->blob_entries->size = table->info->size;
996 if (iptables_get_entries(table) < 0)
999 table->num_entries = 0;
1000 table->old_entries = table->info->num_entries;
1003 memcpy(table->underflow, table->info->underflow,
1004 sizeof(table->info->underflow));
1005 memcpy(table->hook_entry, table->info->hook_entry,
1006 sizeof(table->info->hook_entry));
1008 ENTRY_ITERATE(table->blob_entries->entrytable,
1009 table->blob_entries->size,
1012 g_hash_table_insert(table_hash, g_strdup(table_name), table);
1018 table_cleanup(table);
1023 static struct option iptables_opts[] = {
1024 {.name = "append", .has_arg = 1, .val = 'A'},
1025 {.name = "flush-chain", .has_arg = 1, .val = 'F'},
1026 {.name = "list", .has_arg = 2, .val = 'L'},
1027 {.name = "new-chain", .has_arg = 1, .val = 'N'},
1028 {.name = "delete-chain", .has_arg = 1, .val = 'X'},
1029 {.name = "destination", .has_arg = 1, .val = 'd'},
1030 {.name = "in-interface", .has_arg = 1, .val = 'i'},
1031 {.name = "jump", .has_arg = 1, .val = 'j'},
1032 {.name = "match", .has_arg = 1, .val = 'm'},
1033 {.name = "out-interface", .has_arg = 1, .val = 'o'},
1034 {.name = "source", .has_arg = 1, .val = 's'},
1035 {.name = "table", .has_arg = 1, .val = 't'},
1039 struct xtables_globals iptables_globals = {
1041 .opts = iptables_opts,
1042 .orig_opts = iptables_opts,
1045 static int iptables_command(int argc, char *argv[])
1047 struct connman_iptables *table;
1048 struct xtables_match *xt_m;
1049 struct xtables_target *xt_t;
1051 char *table_name, *chain, *new_chain, *match_name, *target_name;
1052 char *flush_chain, *delete_chain;
1053 int c, ret, in_len, out_len;
1055 gboolean dump, invert;
1056 struct in_addr src, dst;
1063 table_name = chain = new_chain = match_name = target_name = NULL;
1064 flush_chain = delete_chain = NULL;
1065 memset(&ip, 0, sizeof(struct ipt_ip));
1073 while ((c = getopt_long(argc, argv, "-A:F:L::N:X:d:j:i:m:o:s:t:",
1074 iptables_globals.opts, NULL)) != -1) {
1081 flush_chain = optarg;
1093 delete_chain = optarg;
1097 if (!inet_pton(AF_INET, optarg, &dst))
1101 inet_pton(AF_INET, "255.255.255.255", &ip.dmsk);
1104 ip.invflags |= IPT_INV_DSTIP;
1109 in_len = strlen(optarg);
1111 if (in_len + 1 > IFNAMSIZ)
1114 strcpy(ip.iniface, optarg);
1115 memset(ip.iniface_mask, 0xff, in_len + 1);
1118 ip.invflags |= IPT_INV_VIA_IN;
1123 target_name = optarg;
1124 xt_t = xtables_find_target(target_name, XTF_TRY_LOAD);
1129 size = ALIGN(sizeof(struct ipt_entry_target)) +
1132 xt_t->t = g_try_malloc0(size);
1133 if (xt_t->t == NULL)
1135 xt_t->t->u.target_size = size;
1136 strcpy(xt_t->t->u.user.name, target_name);
1137 xt_t->t->u.user.revision = xt_t->revision;
1138 if (xt_t->init != NULL)
1139 xt_t->init(xt_t->t);
1140 iptables_globals.opts =
1141 xtables_merge_options(
1142 #if XTABLES_VERSION_CODE > 5
1143 iptables_globals.orig_opts,
1145 iptables_globals.opts,
1147 &xt_t->option_offset);
1148 if (iptables_globals.opts == NULL)
1154 match_name = optarg;
1156 xt_m = xtables_find_match(optarg, XTF_LOAD_MUST_SUCCEED, NULL);
1157 size = ALIGN(sizeof(struct ipt_entry_match)) +
1159 xt_m->m = g_try_malloc0(size);
1162 xt_m->m->u.match_size = size;
1163 strcpy(xt_m->m->u.user.name, xt_m->name);
1164 xt_m->m->u.user.revision = xt_m->revision;
1165 if (xt_m->init != NULL)
1166 xt_m->init(xt_m->m);
1167 if (xt_m != xt_m->next) {
1168 iptables_globals.opts =
1169 xtables_merge_options(
1170 #if XTABLES_VERSION_CODE > 5
1171 iptables_globals.orig_opts,
1173 iptables_globals.opts,
1175 &xt_m->option_offset);
1176 if (iptables_globals.opts == NULL)
1183 out_len = strlen(optarg);
1185 if (out_len + 1 > IFNAMSIZ)
1188 strcpy(ip.outiface, optarg);
1189 memset(ip.outiface_mask, 0xff, out_len + 1);
1192 ip.invflags |= IPT_INV_VIA_OUT;
1197 if (!inet_pton(AF_INET, optarg, &src))
1201 inet_pton(AF_INET, "255.255.255.255", &ip.smsk);
1204 ip.invflags |= IPT_INV_SRCIP;
1209 table_name = optarg;
1213 if (optarg[0] == '!' && optarg[1] == '\0') {
1219 connman_error("Invalid option");
1225 if (xt_t == NULL || xt_t->parse == NULL ||
1226 !xt_t->parse(c - xt_t->option_offset, argv, invert,
1227 &xt_t->tflags, NULL, &xt_t->t)) {
1228 if (xt_m == NULL || xt_m->parse == NULL)
1231 xt_m->parse(c - xt_m->option_offset, argv,
1232 invert, &xt_m->mflags, NULL, &xt_m->m);
1241 if (table_name == NULL)
1242 table_name = "filter";
1244 table = iptables_init(table_name);
1245 if (table == NULL) {
1250 if (delete_chain != NULL) {
1251 printf("Delete chain %s\n", delete_chain);
1253 iptables_delete_chain(table, delete_chain);
1259 iptables_dump(table);
1266 DBG("Flush chain %s", flush_chain);
1268 iptables_flush_chain(table, flush_chain);
1273 if (chain && new_chain) {
1279 DBG("New chain %s", new_chain);
1281 ret = iptables_add_chain(table, new_chain);
1286 if (target_name == NULL)
1289 DBG("Adding %s to %s (match %s)",
1290 target_name, chain, match_name);
1292 ret = iptables_add_rule(table, &ip, chain, target_name, xt_t,
1308 int __connman_iptables_command(const char *format, ...)
1310 char **argv, **arguments, *command;
1317 va_start(args, format);
1319 command = g_strdup_vprintf(format, args);
1323 if (command == NULL)
1326 arguments = g_strsplit_set(command, " ", -1);
1328 for (argc = 0; arguments[argc]; argc++);
1331 DBG("command %s argc %d", command, argc);
1333 argv = g_try_malloc0(argc * sizeof(char *));
1336 g_strfreev(arguments);
1340 argv[0] = "iptables";
1341 for (i = 1; i < argc; i++)
1342 argv[i] = arguments[i - 1];
1344 ret = iptables_command(argc, argv);
1347 g_strfreev(arguments);
1354 int __connman_iptables_commit(const char *table_name)
1356 struct connman_iptables *table;
1357 struct ipt_replace *repl;
1360 DBG("%s", table_name);
1362 table = g_hash_table_lookup(table_hash, table_name);
1366 repl = iptables_blob(table);
1368 err = iptables_replace(table, repl);
1370 g_free(repl->counters);
1376 g_hash_table_remove(table_hash, table_name);
1381 static void remove_table(gpointer user_data)
1383 struct connman_iptables *table = user_data;
1385 table_cleanup(table);
1388 int __connman_iptables_init(void)
1392 table_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1393 g_free, remove_table);
1395 xtables_init_all(&iptables_globals, NFPROTO_IPV4);
1401 void __connman_iptables_cleanup(void)
1405 g_hash_table_destroy(table_hash);
1407 xtables_free_opts(1);