5 * Copyright (C) 2007-2011 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 void update_targets_reference(struct connman_iptables *table,
288 struct connman_iptables_entry *entry_before,
289 struct connman_iptables_entry *modified_entry,
290 gboolean is_removing)
292 struct connman_iptables_entry *tmp;
293 struct xt_standard_target *t;
297 offset = modified_entry->entry->next_offset;
299 for (list = table->entries; list; list = list->next) {
305 t = (struct xt_standard_target *)ipt_get_target(tmp->entry);
307 if (is_removing == TRUE) {
308 if (t->verdict >= entry_before->offset)
309 t->verdict -= offset;
311 if (t->verdict > entry_before->offset)
312 t->verdict += offset;
317 static int iptables_add_entry(struct connman_iptables *table,
318 struct ipt_entry *entry, GList *before,
321 struct connman_iptables_entry *e, *entry_before;
326 e = g_try_malloc0(sizeof(struct connman_iptables_entry));
331 e->builtin = builtin;
333 table->entries = g_list_insert_before(table->entries, before, e);
334 table->num_entries++;
335 table->size += entry->next_offset;
337 if (before == NULL) {
338 e->offset = table->size - entry->next_offset;
343 entry_before = before->data;
346 * We've just appended/insterted a new entry. All references
347 * should be bumped accordingly.
349 update_targets_reference(table, entry_before, e, FALSE);
351 update_offsets(table);
356 static int remove_table_entry(struct connman_iptables *table,
357 struct connman_iptables_entry *entry)
361 table->num_entries--;
362 table->size -= entry->entry->next_offset;
363 removed = entry->entry->next_offset;
365 g_free(entry->entry);
367 table->entries = g_list_remove(table->entries, entry);
372 static int iptables_flush_chain(struct connman_iptables *table,
375 GList *chain_head, *chain_tail, *list, *next;
376 struct connman_iptables_entry *entry;
377 int builtin, removed = 0;
379 chain_head = find_chain_head(table, name);
380 if (chain_head == NULL)
383 chain_tail = find_chain_tail(table, name);
384 if (chain_tail == NULL)
387 entry = chain_head->data;
388 builtin = entry->builtin;
393 list = chain_head->next;
395 if (list == chain_tail->prev)
398 while (list != chain_tail->prev) {
400 next = g_list_next(list);
402 removed += remove_table_entry(table, entry);
408 struct connman_iptables_entry *e;
412 entry->builtin = builtin;
414 table->underflow[builtin] -= removed;
416 for (list = chain_tail; list; list = list->next) {
419 builtin = e->builtin;
423 table->hook_entry[builtin] -= removed;
424 table->underflow[builtin] -= removed;
428 update_offsets(table);
433 static int iptables_add_chain(struct connman_iptables *table,
437 struct ipt_entry *entry_head;
438 struct ipt_entry *entry_return;
439 struct error_target *error;
440 struct ipt_standard_target *standard;
441 u_int16_t entry_head_size, entry_return_size;
443 last = g_list_last(table->entries);
446 * An empty chain is composed of:
447 * - A head entry, with no match and an error target.
448 * The error target data is the chain name.
449 * - A tail entry, with no match and a standard target.
450 * The standard target verdict is XT_RETURN (return to the
455 entry_head_size = sizeof(struct ipt_entry) +
456 sizeof(struct error_target);
457 entry_head = g_try_malloc0(entry_head_size);
458 if (entry_head == NULL)
461 memset(entry_head, 0, entry_head_size);
463 entry_head->target_offset = sizeof(struct ipt_entry);
464 entry_head->next_offset = entry_head_size;
466 error = (struct error_target *) entry_head->elems;
467 strcpy(error->t.u.user.name, IPT_ERROR_TARGET);
468 error->t.u.user.target_size = ALIGN(sizeof(struct error_target));
469 strcpy(error->error, name);
471 if (iptables_add_entry(table, entry_head, last, -1) < 0)
475 entry_return_size = sizeof(struct ipt_entry) +
476 sizeof(struct ipt_standard_target);
477 entry_return = g_try_malloc0(entry_return_size);
478 if (entry_return == NULL)
481 memset(entry_return, 0, entry_return_size);
483 entry_return->target_offset = sizeof(struct ipt_entry);
484 entry_return->next_offset = entry_return_size;
486 standard = (struct ipt_standard_target *) entry_return->elems;
487 standard->target.u.user.target_size =
488 ALIGN(sizeof(struct ipt_standard_target));
489 standard->verdict = XT_RETURN;
491 if (iptables_add_entry(table, entry_return, last, -1) < 0)
497 g_free(entry_return);
504 static int iptables_delete_chain(struct connman_iptables *table, char *name)
506 struct connman_iptables_entry *entry;
507 GList *chain_head, *chain_tail;
509 chain_head = find_chain_head(table, name);
510 if (chain_head == NULL)
513 entry = chain_head->data;
515 /* We cannot remove builtin chain */
516 if (entry->builtin >= 0)
519 chain_tail = find_chain_tail(table, name);
520 if (chain_tail == NULL)
523 /* Chain must be flushed */
524 if (chain_head->next != chain_tail->prev)
527 remove_table_entry(table, entry);
529 entry = chain_tail->prev->data;
530 remove_table_entry(table, entry);
532 update_offsets(table);
537 static struct ipt_entry *new_rule(struct ipt_ip *ip,
538 char *target_name, struct xtables_target *xt_t,
539 char *match_name, struct xtables_match *xt_m)
541 struct ipt_entry *new_entry;
542 size_t match_size, target_size;
545 match_size = xt_m->m->u.match_size;
550 target_size = ALIGN(xt_t->t->u.target_size);
552 target_size = ALIGN(sizeof(struct xt_standard_target));
554 new_entry = g_try_malloc0(sizeof(struct ipt_entry) + target_size +
556 if (new_entry == NULL)
559 memcpy(&new_entry->ip, ip, sizeof(struct ipt_ip));
561 new_entry->target_offset = sizeof(struct ipt_entry) + match_size;
562 new_entry->next_offset = sizeof(struct ipt_entry) + target_size +
565 struct xt_entry_match *entry_match;
567 entry_match = (struct xt_entry_match *)new_entry->elems;
568 memcpy(entry_match, xt_m->m, match_size);
572 struct xt_entry_target *entry_target;
574 entry_target = ipt_get_target(new_entry);
575 memcpy(entry_target, xt_t->t, target_size);
581 static void update_hooks(struct connman_iptables *table, GList *chain_head,
582 struct ipt_entry *entry)
585 struct connman_iptables_entry *head, *e;
588 if (chain_head == NULL)
591 head = chain_head->data;
593 builtin = head->builtin;
597 table->underflow[builtin] += entry->next_offset;
599 for (list = chain_head->next; list; list = list->next) {
602 builtin = e->builtin;
606 table->hook_entry[builtin] += entry->next_offset;
607 table->underflow[builtin] += entry->next_offset;
611 static struct ipt_entry *prepare_rule_inclusion(struct connman_iptables *table,
612 struct ipt_ip *ip, char *chain_name,
613 char *target_name, struct xtables_target *xt_t,
614 char *match_name, struct xtables_match *xt_m,
617 GList *chain_tail, *chain_head;
618 struct ipt_entry *new_entry;
619 struct connman_iptables_entry *head;
621 chain_head = find_chain_head(table, chain_name);
622 if (chain_head == NULL)
625 chain_tail = find_chain_tail(table, chain_name);
626 if (chain_tail == NULL)
629 new_entry = new_rule(ip, target_name, xt_t, match_name, xt_m);
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;
651 static int iptables_append_rule(struct connman_iptables *table,
652 struct ipt_ip *ip, char *chain_name,
653 char *target_name, struct xtables_target *xt_t,
654 char *match_name, struct xtables_match *xt_m)
657 struct ipt_entry *new_entry;
658 int builtin = -1, ret;
662 chain_tail = find_chain_tail(table, chain_name);
663 if (chain_tail == NULL)
666 new_entry = prepare_rule_inclusion(table, ip, chain_name,
667 target_name, xt_t, match_name, xt_m, &builtin);
668 if (new_entry == NULL)
671 ret = iptables_add_entry(table, new_entry, chain_tail->prev, builtin);
678 static int iptables_insert_rule(struct connman_iptables *table,
679 struct ipt_ip *ip, char *chain_name,
680 char *target_name, struct xtables_target *xt_t,
681 char *match_name, struct xtables_match *xt_m)
683 struct ipt_entry *new_entry;
684 int builtin = -1, ret;
687 chain_head = find_chain_head(table, chain_name);
688 if (chain_head == NULL)
691 new_entry = prepare_rule_inclusion(table, ip, chain_name,
692 target_name, xt_t, match_name, xt_m, &builtin);
693 if (new_entry == NULL)
696 ret = iptables_add_entry(table, new_entry, chain_head->next, builtin);
703 static gboolean is_same_ipt_entry(struct ipt_entry *i_e1,
704 struct ipt_entry *i_e2)
706 if (memcmp(&i_e1->ip, &i_e2->ip, sizeof(struct ipt_ip)) != 0)
709 if (i_e1->target_offset != i_e2->target_offset)
712 if (i_e1->next_offset != i_e2->next_offset)
718 static gboolean is_same_target(struct xt_entry_target *xt_e_t1,
719 struct xt_entry_target *xt_e_t2)
721 if (xt_e_t1 == NULL || xt_e_t2 == NULL)
724 if (strcmp(xt_e_t1->u.user.name, IPT_STANDARD_TARGET) == 0) {
725 struct xt_standard_target *xt_s_t1;
726 struct xt_standard_target *xt_s_t2;
728 xt_s_t1 = (struct xt_standard_target *) xt_e_t1;
729 xt_s_t2 = (struct xt_standard_target *) xt_e_t2;
731 if (xt_s_t1->verdict != xt_s_t2->verdict)
734 if (xt_e_t1->u.target_size != xt_e_t2->u.target_size)
737 if (strcmp(xt_e_t1->u.user.name, xt_e_t2->u.user.name) != 0)
744 static gboolean is_same_match(struct xt_entry_match *xt_e_m1,
745 struct xt_entry_match *xt_e_m2)
747 if (xt_e_m1 == NULL || xt_e_m2 == NULL)
750 if (xt_e_m1->u.match_size != xt_e_m2->u.match_size)
753 if (xt_e_m1->u.user.revision != xt_e_m2->u.user.revision)
756 if (strcmp(xt_e_m1->u.user.name, xt_e_m2->u.user.name) != 0)
762 static int iptables_delete_rule(struct connman_iptables *table,
763 struct ipt_ip *ip, char *chain_name,
764 char *target_name, struct xtables_target *xt_t,
765 char *match_name, struct xtables_match *xt_m)
767 GList *chain_tail, *chain_head, *list;
768 struct xt_entry_target *xt_e_t = NULL;
769 struct xt_entry_match *xt_e_m = NULL;
770 struct connman_iptables_entry *entry;
771 struct ipt_entry *entry_test;
772 int builtin, removed;
776 chain_head = find_chain_head(table, chain_name);
777 if (chain_head == NULL)
780 chain_tail = find_chain_tail(table, chain_name);
781 if (chain_tail == NULL)
787 entry_test = new_rule(ip, target_name, xt_t, match_name, xt_m);
788 if (entry_test == NULL)
792 xt_e_t = ipt_get_target(entry_test);
794 xt_e_m = (struct xt_entry_match *)entry_test->elems;
796 entry = chain_head->data;
797 builtin = entry->builtin;
802 list = chain_head->next;
804 for (entry = NULL; list != chain_tail->prev; list = list->next) {
805 struct connman_iptables_entry *tmp;
806 struct ipt_entry *tmp_e;
811 if (is_same_ipt_entry(entry_test, tmp_e) == FALSE)
815 struct xt_entry_target *tmp_xt_e_t;
817 tmp_xt_e_t = ipt_get_target(tmp_e);
819 if (!is_same_target(tmp_xt_e_t, xt_e_t))
824 struct xt_entry_match *tmp_xt_e_m;
826 tmp_xt_e_m = (struct xt_entry_match *)tmp_e->elems;
828 if (!is_same_match(tmp_xt_e_m, xt_e_m))
841 /* We have deleted a rule,
842 * all references should be bumped accordingly */
843 if (list->next != NULL)
844 update_targets_reference(table, list->next->data,
847 removed += remove_table_entry(table, entry);
853 entry->builtin = builtin;
856 table->underflow[builtin] -= removed;
857 for (list = chain_tail; list; list = list->next) {
860 builtin = entry->builtin;
864 table->hook_entry[builtin] -= removed;
865 table->underflow[builtin] -= removed;
869 update_offsets(table);
874 static struct ipt_replace *iptables_blob(struct connman_iptables *table)
876 struct ipt_replace *r;
878 struct connman_iptables_entry *e;
879 unsigned char *entry_index;
881 r = g_try_malloc0(sizeof(struct ipt_replace) + table->size);
885 memset(r, 0, sizeof(*r) + table->size);
887 r->counters = g_try_malloc0(sizeof(struct xt_counters)
888 * table->old_entries);
889 if (r->counters == NULL) {
894 strcpy(r->name, table->info->name);
895 r->num_entries = table->num_entries;
896 r->size = table->size;
898 r->num_counters = table->old_entries;
899 r->valid_hooks = table->info->valid_hooks;
901 memcpy(r->hook_entry, table->hook_entry, sizeof(table->hook_entry));
902 memcpy(r->underflow, table->underflow, sizeof(table->underflow));
904 entry_index = (unsigned char *)r->entries;
905 for (list = table->entries; list; list = list->next) {
908 memcpy(entry_index, e->entry, e->entry->next_offset);
909 entry_index += e->entry->next_offset;
915 static void dump_ip(struct ipt_entry *entry)
917 struct ipt_ip *ip = &entry->ip;
918 char ip_string[INET6_ADDRSTRLEN];
919 char ip_mask[INET6_ADDRSTRLEN];
921 if (strlen(ip->iniface))
922 connman_info("\tin %s", ip->iniface);
924 if (strlen(ip->outiface))
925 connman_info("\tout %s", ip->outiface);
927 if (inet_ntop(AF_INET, &ip->src, ip_string, INET6_ADDRSTRLEN) != NULL &&
928 inet_ntop(AF_INET, &ip->smsk,
929 ip_mask, INET6_ADDRSTRLEN) != NULL)
930 connman_info("\tsrc %s/%s", ip_string, ip_mask);
932 if (inet_ntop(AF_INET, &ip->dst, ip_string, INET6_ADDRSTRLEN) != NULL &&
933 inet_ntop(AF_INET, &ip->dmsk,
934 ip_mask, INET6_ADDRSTRLEN) != NULL)
935 connman_info("\tdst %s/%s", ip_string, ip_mask);
938 static void dump_target(struct connman_iptables *table,
939 struct ipt_entry *entry)
942 struct xtables_target *xt_t;
943 struct xt_entry_target *target;
945 target = ipt_get_target(entry);
947 if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
948 struct xt_standard_target *t;
950 t = (struct xt_standard_target *)target;
952 switch (t->verdict) {
954 connman_info("\ttarget RETURN");
958 connman_info("\ttarget ACCEPT");
962 connman_info("\ttarget DROP");
966 connman_info("\ttarget QUEUE");
970 connman_info("\ttarget STOP");
974 connman_info("\tJUMP @%p (0x%x)",
975 (char*)table->blob_entries->entrytable +
976 t->verdict, t->verdict);
980 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
981 XTF_LOAD_MUST_SUCCEED);
983 if(xt_t->print != NULL)
984 xt_t->print(NULL, target, 1);
986 xt_t = xtables_find_target(target->u.user.name, XTF_TRY_LOAD);
988 connman_info("\ttarget %s", target->u.user.name);
992 if(xt_t->print != NULL) {
993 connman_info("\ttarget ");
994 xt_t->print(NULL, target, 1);
999 static void dump_match(struct connman_iptables *table, struct ipt_entry *entry)
1001 struct xtables_match *xt_m;
1002 struct xt_entry_match *match;
1004 if (entry->elems == (unsigned char *)entry + entry->target_offset)
1007 match = (struct xt_entry_match *) entry->elems;
1009 if (!strlen(match->u.user.name))
1012 xt_m = xtables_find_match(match->u.user.name, XTF_TRY_LOAD, NULL);
1016 if(xt_m->print != NULL) {
1017 connman_info("\tmatch ");
1018 xt_m->print(NULL, match, 1);
1024 connman_info("\tmatch %s", match->u.user.name);
1028 static int dump_entry(struct ipt_entry *entry,
1029 struct connman_iptables *table)
1031 struct xt_entry_target *target;
1032 unsigned int offset;
1035 offset = (char *)entry - (char *)table->blob_entries->entrytable;
1036 target = ipt_get_target(entry);
1037 builtin = is_hook_entry(table, entry);
1039 if (entry_to_offset(table, entry) + entry->next_offset ==
1040 table->blob_entries->size) {
1041 connman_info("End of CHAIN 0x%x", offset);
1045 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET)) {
1046 connman_info("USER CHAIN (%s) %p match %p target %p size %d",
1047 target->data, entry, entry->elems,
1048 (char *)entry + entry->target_offset,
1049 entry->next_offset);
1052 } else if (builtin >= 0) {
1053 connman_info("CHAIN (%s) %p match %p target %p size %d",
1054 hooknames[builtin], entry, entry->elems,
1055 (char *)entry + entry->target_offset,
1056 entry->next_offset);
1058 connman_info("RULE %p match %p target %p size %d", entry,
1060 (char *)entry + entry->target_offset,
1061 entry->next_offset);
1064 dump_match(table, entry);
1065 dump_target(table, entry);
1071 static void iptables_dump(struct connman_iptables *table)
1073 connman_info("%s valid_hooks=0x%08x, num_entries=%u, size=%u",
1075 table->info->valid_hooks, table->info->num_entries,
1078 ENTRY_ITERATE(table->blob_entries->entrytable,
1079 table->blob_entries->size,
1084 static int iptables_get_entries(struct connman_iptables *table)
1086 socklen_t entry_size;
1088 entry_size = sizeof(struct ipt_get_entries) + table->info->size;
1090 return getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_ENTRIES,
1091 table->blob_entries, &entry_size);
1094 static int iptables_replace(struct connman_iptables *table,
1095 struct ipt_replace *r)
1097 return setsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_SET_REPLACE, r,
1098 sizeof(*r) + r->size);
1101 static int add_entry(struct ipt_entry *entry, struct connman_iptables *table)
1103 struct ipt_entry *new_entry;
1106 new_entry = g_try_malloc0(entry->next_offset);
1107 if (new_entry == NULL)
1110 memcpy(new_entry, entry, entry->next_offset);
1112 builtin = is_hook_entry(table, entry);
1114 return iptables_add_entry(table, new_entry, NULL, builtin);
1117 static void table_cleanup(struct connman_iptables *table)
1120 struct connman_iptables_entry *entry;
1122 close(table->ipt_sock);
1124 for (list = table->entries; list; list = list->next) {
1127 g_free(entry->entry);
1131 g_list_free(table->entries);
1132 g_free(table->info);
1133 g_free(table->blob_entries);
1137 static struct connman_iptables *iptables_init(char *table_name)
1139 struct connman_iptables *table = NULL;
1140 char *module = NULL;
1143 DBG("%s", table_name);
1145 if (xtables_insmod("ip_tables", NULL, TRUE) != 0)
1148 module = g_strconcat("iptable_", table_name, NULL);
1152 if (xtables_insmod(module, NULL, TRUE) != 0)
1158 table = g_hash_table_lookup(table_hash, table_name);
1162 table = g_try_new0(struct connman_iptables, 1);
1166 table->info = g_try_new0(struct ipt_getinfo, 1);
1167 if (table->info == NULL)
1170 table->ipt_sock = socket(AF_INET, SOCK_RAW | SOCK_CLOEXEC, IPPROTO_RAW);
1171 if (table->ipt_sock < 0)
1174 s = sizeof(*table->info);
1175 strcpy(table->info->name, table_name);
1176 if (getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_INFO,
1177 table->info, &s) < 0)
1180 table->blob_entries = g_try_malloc0(sizeof(struct ipt_get_entries) +
1182 if (table->blob_entries == NULL)
1185 strcpy(table->blob_entries->name, table_name);
1186 table->blob_entries->size = table->info->size;
1188 if (iptables_get_entries(table) < 0)
1191 table->num_entries = 0;
1192 table->old_entries = table->info->num_entries;
1195 memcpy(table->underflow, table->info->underflow,
1196 sizeof(table->info->underflow));
1197 memcpy(table->hook_entry, table->info->hook_entry,
1198 sizeof(table->info->hook_entry));
1200 ENTRY_ITERATE(table->blob_entries->entrytable,
1201 table->blob_entries->size,
1204 g_hash_table_insert(table_hash, g_strdup(table_name), table);
1211 table_cleanup(table);
1216 static struct xtables_target *prepare_target(struct connman_iptables *table,
1219 struct xtables_target *xt_t = NULL;
1220 gboolean is_builtin, is_user_defined;
1221 GList *chain_head = NULL;
1225 is_user_defined = FALSE;
1227 if (is_builtin_target(target_name))
1230 chain_head = find_chain_head(table, target_name);
1231 if (chain_head != NULL && chain_head->next != NULL)
1232 is_user_defined = TRUE;
1235 if (is_builtin || is_user_defined)
1236 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
1237 XTF_LOAD_MUST_SUCCEED);
1239 xt_t = xtables_find_target(target_name, XTF_TRY_LOAD);
1244 target_size = ALIGN(sizeof(struct ipt_entry_target)) + xt_t->size;
1246 xt_t->t = g_try_malloc0(target_size);
1247 if (xt_t->t == NULL)
1250 xt_t->t->u.target_size = target_size;
1252 if (is_builtin || is_user_defined) {
1253 struct xt_standard_target *target;
1255 target = (struct xt_standard_target *)(xt_t->t);
1256 strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
1258 if (is_builtin == TRUE)
1259 target->verdict = target_to_verdict(target_name);
1260 else if (is_user_defined == TRUE) {
1261 struct connman_iptables_entry *target_rule;
1263 if (chain_head == NULL) {
1268 target_rule = chain_head->next->data;
1269 target->verdict = target_rule->offset;
1272 strcpy(xt_t->t->u.user.name, target_name);
1273 xt_t->t->u.user.revision = xt_t->revision;
1274 if (xt_t->init != NULL)
1275 xt_t->init(xt_t->t);
1281 static struct option iptables_opts[] = {
1282 {.name = "append", .has_arg = 1, .val = 'A'},
1283 {.name = "delete", .has_arg = 1, .val = 'D'},
1284 {.name = "flush-chain", .has_arg = 1, .val = 'F'},
1285 {.name = "insert", .has_arg = 1, .val = 'I'},
1286 {.name = "list", .has_arg = 2, .val = 'L'},
1287 {.name = "new-chain", .has_arg = 1, .val = 'N'},
1288 {.name = "delete-chain", .has_arg = 1, .val = 'X'},
1289 {.name = "destination", .has_arg = 1, .val = 'd'},
1290 {.name = "in-interface", .has_arg = 1, .val = 'i'},
1291 {.name = "jump", .has_arg = 1, .val = 'j'},
1292 {.name = "match", .has_arg = 1, .val = 'm'},
1293 {.name = "out-interface", .has_arg = 1, .val = 'o'},
1294 {.name = "source", .has_arg = 1, .val = 's'},
1295 {.name = "table", .has_arg = 1, .val = 't'},
1299 struct xtables_globals iptables_globals = {
1301 .opts = iptables_opts,
1302 .orig_opts = iptables_opts,
1305 static int iptables_command(int argc, char *argv[])
1307 struct connman_iptables *table;
1308 struct xtables_match *xt_m;
1309 struct xtables_target *xt_t;
1311 char *table_name, *chain, *new_chain, *match_name, *target_name;
1312 char *flush_chain, *delete_chain;
1313 int c, ret, in_len, out_len;
1315 gboolean dump, invert, insert, delete;
1316 struct in_addr src, dst;
1325 table_name = chain = new_chain = match_name = target_name = NULL;
1326 flush_chain = delete_chain = NULL;
1327 memset(&ip, 0, sizeof(struct ipt_ip));
1335 while ((c = getopt_long(argc, argv, "-A:F:I:L::N:X:d:j:i:m:o:s:t:",
1336 iptables_globals.opts, NULL)) != -1) {
1339 /* It is either -A, -D or -I at once */
1347 /* It is either -A, -D or -I at once */
1356 flush_chain = optarg;
1360 /* It is either -A, -D or -I at once */
1377 delete_chain = optarg;
1381 if (!inet_pton(AF_INET, optarg, &dst))
1385 inet_pton(AF_INET, "255.255.255.255", &ip.dmsk);
1388 ip.invflags |= IPT_INV_DSTIP;
1393 in_len = strlen(optarg);
1395 if (in_len + 1 > IFNAMSIZ)
1398 strcpy(ip.iniface, optarg);
1399 memset(ip.iniface_mask, 0xff, in_len + 1);
1402 ip.invflags |= IPT_INV_VIA_IN;
1407 target_name = optarg;
1411 match_name = optarg;
1413 xt_m = xtables_find_match(optarg, XTF_LOAD_MUST_SUCCEED, NULL);
1414 size = ALIGN(sizeof(struct ipt_entry_match)) +
1416 xt_m->m = g_try_malloc0(size);
1419 xt_m->m->u.match_size = size;
1420 strcpy(xt_m->m->u.user.name, xt_m->name);
1421 xt_m->m->u.user.revision = xt_m->revision;
1422 if (xt_m->init != NULL)
1423 xt_m->init(xt_m->m);
1424 if (xt_m != xt_m->next) {
1425 iptables_globals.opts =
1426 xtables_merge_options(
1427 #if XTABLES_VERSION_CODE > 5
1428 iptables_globals.orig_opts,
1430 iptables_globals.opts,
1432 &xt_m->option_offset);
1433 if (iptables_globals.opts == NULL)
1440 out_len = strlen(optarg);
1442 if (out_len + 1 > IFNAMSIZ)
1445 strcpy(ip.outiface, optarg);
1446 memset(ip.outiface_mask, 0xff, out_len + 1);
1449 ip.invflags |= IPT_INV_VIA_OUT;
1454 if (!inet_pton(AF_INET, optarg, &src))
1458 inet_pton(AF_INET, "255.255.255.255", &ip.smsk);
1461 ip.invflags |= IPT_INV_SRCIP;
1466 table_name = optarg;
1470 if (optarg[0] == '!' && optarg[1] == '\0') {
1476 connman_error("Invalid option");
1482 if (xt_t == NULL || xt_t->parse == NULL ||
1483 !xt_t->parse(c - xt_t->option_offset, argv, invert,
1484 &xt_t->tflags, NULL, &xt_t->t)) {
1485 if (xt_m == NULL || xt_m->parse == NULL)
1488 xt_m->parse(c - xt_m->option_offset, argv,
1489 invert, &xt_m->mflags, NULL, &xt_m->m);
1498 if (table_name == NULL)
1499 table_name = "filter";
1501 table = iptables_init(table_name);
1502 if (table == NULL) {
1507 if (delete_chain != NULL) {
1508 printf("Delete chain %s\n", delete_chain);
1510 iptables_delete_chain(table, delete_chain);
1516 iptables_dump(table);
1523 DBG("Flush chain %s", flush_chain);
1525 iptables_flush_chain(table, flush_chain);
1530 if (chain && new_chain) {
1536 DBG("New chain %s", new_chain);
1538 ret = iptables_add_chain(table, new_chain);
1543 xt_t = prepare_target(table, target_name);
1547 iptables_globals.opts =
1548 xtables_merge_options(
1549 #if XTABLES_VERSION_CODE > 5
1550 iptables_globals.orig_opts,
1552 iptables_globals.opts,
1554 &xt_t->option_offset);
1555 if (iptables_globals.opts == NULL)
1558 if (delete == TRUE) {
1559 DBG("Deleting %s to %s (match %s)\n",
1560 target_name, chain, match_name);
1562 ret = iptables_delete_rule(table, &ip, chain,
1563 target_name, xt_t, match_name, xt_m);
1568 if (insert == TRUE) {
1569 DBG("Inserting %s to %s (match %s)",
1570 target_name, chain, match_name);
1572 ret = iptables_insert_rule(table, &ip, chain,
1573 target_name, xt_t, match_name, xt_m);
1577 DBG("Adding %s to %s (match %s)",
1578 target_name, chain, match_name);
1580 ret = iptables_append_rule(table, &ip, chain,
1581 target_name, xt_t, match_name, xt_m);
1597 int __connman_iptables_command(const char *format, ...)
1599 char **argv, **arguments, *command;
1606 va_start(args, format);
1608 command = g_strdup_vprintf(format, args);
1612 if (command == NULL)
1615 arguments = g_strsplit_set(command, " ", -1);
1617 for (argc = 0; arguments[argc]; argc++);
1620 DBG("command %s argc %d", command, argc);
1622 argv = g_try_malloc0(argc * sizeof(char *));
1625 g_strfreev(arguments);
1629 argv[0] = "iptables";
1630 for (i = 1; i < argc; i++)
1631 argv[i] = arguments[i - 1];
1633 ret = iptables_command(argc, argv);
1636 g_strfreev(arguments);
1643 int __connman_iptables_commit(const char *table_name)
1645 struct connman_iptables *table;
1646 struct ipt_replace *repl;
1649 DBG("%s", table_name);
1651 table = g_hash_table_lookup(table_hash, table_name);
1655 repl = iptables_blob(table);
1657 err = iptables_replace(table, repl);
1659 g_free(repl->counters);
1665 g_hash_table_remove(table_hash, table_name);
1670 static void remove_table(gpointer user_data)
1672 struct connman_iptables *table = user_data;
1674 table_cleanup(table);
1677 int __connman_iptables_init(void)
1681 table_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1682 g_free, remove_table);
1684 xtables_init_all(&iptables_globals, NFPROTO_IPV4);
1690 void __connman_iptables_cleanup(void)
1694 g_hash_table_destroy(table_hash);
1696 xtables_free_opts(1);