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 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 *
538 new_rule(struct ipt_ip *ip, char *target_name,
539 struct xtables_target *xt_t,
540 char *match_name, struct xtables_match *xt_m)
542 struct ipt_entry *new_entry;
543 size_t match_size, target_size;
546 match_size = xt_m->m->u.match_size;
551 target_size = ALIGN(xt_t->t->u.target_size);
553 target_size = ALIGN(sizeof(struct xt_standard_target));
555 new_entry = g_try_malloc0(sizeof(struct ipt_entry) + target_size +
557 if (new_entry == NULL)
560 memcpy(&new_entry->ip, ip, sizeof(struct ipt_ip));
562 new_entry->target_offset = sizeof(struct ipt_entry) + match_size;
563 new_entry->next_offset = sizeof(struct ipt_entry) + target_size +
566 struct xt_entry_match *entry_match;
568 entry_match = (struct xt_entry_match *)new_entry->elems;
569 memcpy(entry_match, xt_m->m, match_size);
573 struct xt_entry_target *entry_target;
575 entry_target = ipt_get_target(new_entry);
576 memcpy(entry_target, xt_t->t, target_size);
582 static void update_hooks(struct connman_iptables *table, GList *chain_head,
583 struct ipt_entry *entry)
586 struct connman_iptables_entry *head, *e;
589 if (chain_head == NULL)
592 head = chain_head->data;
594 builtin = head->builtin;
598 table->underflow[builtin] += entry->next_offset;
600 for (list = chain_head->next; list; list = list->next) {
603 builtin = e->builtin;
607 table->hook_entry[builtin] += entry->next_offset;
608 table->underflow[builtin] += entry->next_offset;
612 static struct ipt_entry *prepare_rule_inclusion(struct connman_iptables *table,
613 struct ipt_ip *ip, char *chain_name,
614 char *target_name, struct xtables_target *xt_t,
615 char *match_name, struct xtables_match *xt_m,
618 GList *chain_tail, *chain_head;
619 struct ipt_entry *new_entry;
620 struct connman_iptables_entry *head;
622 chain_head = find_chain_head(table, chain_name);
623 if (chain_head == NULL)
626 chain_tail = find_chain_tail(table, chain_name);
627 if (chain_tail == NULL)
630 new_entry = new_rule(ip, target_name, xt_t, match_name, xt_m);
631 if (new_entry == NULL)
634 update_hooks(table, chain_head, new_entry);
637 * If the chain is builtin, and does not have any rule,
638 * then the one that we're inserting is becoming the head
639 * and thus needs the builtin flag.
641 head = chain_head->data;
642 if (head->builtin < 0)
644 else if (chain_head == chain_tail->prev) {
645 *builtin = head->builtin;
652 static int iptables_append_rule(struct connman_iptables *table,
653 struct ipt_ip *ip, char *chain_name,
654 char *target_name, struct xtables_target *xt_t,
655 char *match_name, struct xtables_match *xt_m)
658 struct ipt_entry *new_entry;
659 int builtin = -1, ret;
663 chain_tail = find_chain_tail(table, chain_name);
664 if (chain_tail == NULL)
667 new_entry = prepare_rule_inclusion(table, ip, chain_name,
668 target_name, xt_t, match_name, xt_m, &builtin);
669 if (new_entry == NULL)
672 ret = iptables_add_entry(table, new_entry, chain_tail->prev, builtin);
679 static int iptables_insert_rule(struct connman_iptables *table,
680 struct ipt_ip *ip, char *chain_name,
681 char *target_name, struct xtables_target *xt_t,
682 char *match_name, struct xtables_match *xt_m)
684 struct ipt_entry *new_entry;
685 int builtin = -1, ret;
688 chain_head = find_chain_head(table, chain_name);
689 if (chain_head == NULL)
692 new_entry = prepare_rule_inclusion(table, ip, chain_name,
693 target_name, xt_t, match_name, xt_m, &builtin);
694 if (new_entry == NULL)
697 ret = iptables_add_entry(table, new_entry, chain_head->next, builtin);
704 static gboolean is_same_ipt_entry(struct ipt_entry *i_e1,
705 struct ipt_entry *i_e2)
707 if (memcmp(&i_e1->ip, &i_e2->ip, sizeof(struct ipt_ip)) != 0)
710 if (i_e1->target_offset != i_e2->target_offset)
713 if (i_e1->next_offset != i_e2->next_offset)
719 static gboolean is_same_target(struct xt_entry_target *xt_e_t1,
720 struct xt_entry_target *xt_e_t2)
722 if (xt_e_t1 == NULL || xt_e_t2 == NULL)
725 if (strcmp(xt_e_t1->u.user.name, IPT_STANDARD_TARGET) == 0) {
726 struct xt_standard_target *xt_s_t1;
727 struct xt_standard_target *xt_s_t2;
729 xt_s_t1 = (struct xt_standard_target *) xt_e_t1;
730 xt_s_t2 = (struct xt_standard_target *) xt_e_t2;
732 if (xt_s_t1->verdict != xt_s_t2->verdict)
735 if (xt_e_t1->u.target_size != xt_e_t2->u.target_size)
738 if (strcmp(xt_e_t1->u.user.name, xt_e_t2->u.user.name) != 0)
745 static gboolean is_same_match(struct xt_entry_match *xt_e_m1,
746 struct xt_entry_match *xt_e_m2)
748 if (xt_e_m1 == NULL || xt_e_m2 == NULL)
751 if (xt_e_m1->u.match_size != xt_e_m2->u.match_size)
754 if (xt_e_m1->u.user.revision != xt_e_m2->u.user.revision)
757 if (strcmp(xt_e_m1->u.user.name, xt_e_m2->u.user.name) != 0)
763 static int iptables_delete_rule(struct connman_iptables *table,
764 struct ipt_ip *ip, char *chain_name,
765 char *target_name, struct xtables_target *xt_t,
766 char *match_name, struct xtables_match *xt_m)
768 GList *chain_tail, *chain_head, *list;
769 struct xt_entry_target *xt_e_t = NULL;
770 struct xt_entry_match *xt_e_m = NULL;
771 struct connman_iptables_entry *entry;
772 struct ipt_entry *entry_test;
773 int builtin, removed;
777 chain_head = find_chain_head(table, chain_name);
778 if (chain_head == NULL)
781 chain_tail = find_chain_tail(table, chain_name);
782 if (chain_tail == NULL)
788 entry_test = new_rule(ip, target_name, xt_t, match_name, xt_m);
789 if (entry_test == NULL)
793 xt_e_t = ipt_get_target(entry_test);
795 xt_e_m = (struct xt_entry_match *)entry_test->elems;
797 entry = chain_head->data;
798 builtin = entry->builtin;
803 list = chain_head->next;
805 for (entry = NULL; list != chain_tail->prev; list = list->next) {
806 struct connman_iptables_entry *tmp;
807 struct ipt_entry *tmp_e;
812 if (is_same_ipt_entry(entry_test, tmp_e) == FALSE)
816 struct xt_entry_target *tmp_xt_e_t;
818 tmp_xt_e_t = ipt_get_target(tmp_e);
820 if (!is_same_target(tmp_xt_e_t, xt_e_t))
825 struct xt_entry_match *tmp_xt_e_m;
827 tmp_xt_e_m = (struct xt_entry_match *)tmp_e->elems;
829 if (!is_same_match(tmp_xt_e_m, xt_e_m))
842 /* We have deleted a rule,
843 * all references should be bumped accordingly */
844 if (list->next != NULL)
845 update_targets_reference(table, list->next->data,
848 removed += remove_table_entry(table, entry);
854 entry->builtin = builtin;
857 table->underflow[builtin] -= removed;
858 for (list = chain_tail; list; list = list->next) {
861 builtin = entry->builtin;
865 table->hook_entry[builtin] -= removed;
866 table->underflow[builtin] -= removed;
870 update_offsets(table);
875 static struct ipt_replace *
876 iptables_blob(struct connman_iptables *table)
878 struct ipt_replace *r;
880 struct connman_iptables_entry *e;
881 unsigned char *entry_index;
883 r = g_try_malloc0(sizeof(struct ipt_replace) + table->size);
887 memset(r, 0, sizeof(*r) + table->size);
889 r->counters = g_try_malloc0(sizeof(struct xt_counters)
890 * table->old_entries);
891 if (r->counters == NULL) {
896 strcpy(r->name, table->info->name);
897 r->num_entries = table->num_entries;
898 r->size = table->size;
900 r->num_counters = table->old_entries;
901 r->valid_hooks = table->info->valid_hooks;
903 memcpy(r->hook_entry, table->hook_entry, sizeof(table->hook_entry));
904 memcpy(r->underflow, table->underflow, sizeof(table->underflow));
906 entry_index = (unsigned char *)r->entries;
907 for (list = table->entries; list; list = list->next) {
910 memcpy(entry_index, e->entry, e->entry->next_offset);
911 entry_index += e->entry->next_offset;
917 static void dump_ip(struct ipt_entry *entry)
919 struct ipt_ip *ip = &entry->ip;
920 char ip_string[INET6_ADDRSTRLEN];
921 char ip_mask[INET6_ADDRSTRLEN];
923 if (strlen(ip->iniface))
924 connman_info("\tin %s", ip->iniface);
926 if (strlen(ip->outiface))
927 connman_info("\tout %s", ip->outiface);
929 if (inet_ntop(AF_INET, &ip->src, ip_string, INET6_ADDRSTRLEN) != NULL &&
930 inet_ntop(AF_INET, &ip->smsk,
931 ip_mask, INET6_ADDRSTRLEN) != NULL)
932 connman_info("\tsrc %s/%s", ip_string, ip_mask);
934 if (inet_ntop(AF_INET, &ip->dst, ip_string, INET6_ADDRSTRLEN) != NULL &&
935 inet_ntop(AF_INET, &ip->dmsk,
936 ip_mask, INET6_ADDRSTRLEN) != NULL)
937 connman_info("\tdst %s/%s", ip_string, ip_mask);
940 static void dump_target(struct connman_iptables *table,
941 struct ipt_entry *entry)
944 struct xtables_target *xt_t;
945 struct xt_entry_target *target;
947 target = ipt_get_target(entry);
949 if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
950 struct xt_standard_target *t;
952 t = (struct xt_standard_target *)target;
954 switch (t->verdict) {
956 connman_info("\ttarget RETURN");
960 connman_info("\ttarget ACCEPT");
964 connman_info("\ttarget DROP");
968 connman_info("\ttarget QUEUE");
972 connman_info("\ttarget STOP");
976 connman_info("\tJUMP @%p (0x%x)",
977 (char*)table->blob_entries->entrytable +
978 t->verdict, t->verdict);
982 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
983 XTF_LOAD_MUST_SUCCEED);
985 if(xt_t->print != NULL)
986 xt_t->print(NULL, target, 1);
988 xt_t = xtables_find_target(target->u.user.name, XTF_TRY_LOAD);
990 connman_info("\ttarget %s", target->u.user.name);
994 if(xt_t->print != NULL) {
995 connman_info("\ttarget ");
996 xt_t->print(NULL, target, 1);
1001 static void dump_match(struct connman_iptables *table, struct ipt_entry *entry)
1003 struct xtables_match *xt_m;
1004 struct xt_entry_match *match;
1006 if (entry->elems == (unsigned char *)entry + entry->target_offset)
1009 match = (struct xt_entry_match *) entry->elems;
1011 if (!strlen(match->u.user.name))
1014 xt_m = xtables_find_match(match->u.user.name, XTF_TRY_LOAD, NULL);
1018 if(xt_m->print != NULL) {
1019 connman_info("\tmatch ");
1020 xt_m->print(NULL, match, 1);
1026 connman_info("\tmatch %s", match->u.user.name);
1030 static int dump_entry(struct ipt_entry *entry,
1031 struct connman_iptables *table)
1033 struct xt_entry_target *target;
1034 unsigned int offset;
1037 offset = (char *)entry - (char *)table->blob_entries->entrytable;
1038 target = ipt_get_target(entry);
1039 builtin = is_hook_entry(table, entry);
1041 if (entry_to_offset(table, entry) + entry->next_offset ==
1042 table->blob_entries->size) {
1043 connman_info("End of CHAIN 0x%x", offset);
1047 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET)) {
1048 connman_info("USER CHAIN (%s) %p match %p target %p size %d",
1049 target->data, entry, entry->elems,
1050 (char *)entry + entry->target_offset,
1051 entry->next_offset);
1054 } else if (builtin >= 0) {
1055 connman_info("CHAIN (%s) %p match %p target %p size %d",
1056 hooknames[builtin], entry, entry->elems,
1057 (char *)entry + entry->target_offset,
1058 entry->next_offset);
1060 connman_info("RULE %p match %p target %p size %d", entry,
1062 (char *)entry + entry->target_offset,
1063 entry->next_offset);
1066 dump_match(table, entry);
1067 dump_target(table, entry);
1073 static void iptables_dump(struct connman_iptables *table)
1075 connman_info("%s valid_hooks=0x%08x, num_entries=%u, size=%u",
1077 table->info->valid_hooks, table->info->num_entries,
1080 ENTRY_ITERATE(table->blob_entries->entrytable,
1081 table->blob_entries->size,
1086 static int iptables_get_entries(struct connman_iptables *table)
1088 socklen_t entry_size;
1090 entry_size = sizeof(struct ipt_get_entries) + table->info->size;
1092 return getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_ENTRIES,
1093 table->blob_entries, &entry_size);
1096 static int iptables_replace(struct connman_iptables *table,
1097 struct ipt_replace *r)
1099 return setsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_SET_REPLACE, r,
1100 sizeof(*r) + r->size);
1103 static int add_entry(struct ipt_entry *entry, struct connman_iptables *table)
1105 struct ipt_entry *new_entry;
1108 new_entry = g_try_malloc0(entry->next_offset);
1109 if (new_entry == NULL)
1112 memcpy(new_entry, entry, entry->next_offset);
1114 builtin = is_hook_entry(table, entry);
1116 return iptables_add_entry(table, new_entry, NULL, builtin);
1119 static void table_cleanup(struct connman_iptables *table)
1122 struct connman_iptables_entry *entry;
1124 close(table->ipt_sock);
1126 for (list = table->entries; list; list = list->next) {
1129 g_free(entry->entry);
1133 g_list_free(table->entries);
1134 g_free(table->info);
1135 g_free(table->blob_entries);
1139 static struct connman_iptables *iptables_init(char *table_name)
1141 struct connman_iptables *table;
1144 DBG("%s", table_name);
1146 table = g_hash_table_lookup(table_hash, table_name);
1150 table = g_try_new0(struct connman_iptables, 1);
1154 table->info = g_try_new0(struct ipt_getinfo, 1);
1155 if (table->info == NULL)
1158 table->ipt_sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
1159 if (table->ipt_sock < 0)
1162 s = sizeof(*table->info);
1163 strcpy(table->info->name, table_name);
1164 if (getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_INFO,
1165 table->info, &s) < 0)
1168 table->blob_entries = g_try_malloc0(sizeof(struct ipt_get_entries) +
1170 if (table->blob_entries == NULL)
1173 strcpy(table->blob_entries->name, table_name);
1174 table->blob_entries->size = table->info->size;
1176 if (iptables_get_entries(table) < 0)
1179 table->num_entries = 0;
1180 table->old_entries = table->info->num_entries;
1183 memcpy(table->underflow, table->info->underflow,
1184 sizeof(table->info->underflow));
1185 memcpy(table->hook_entry, table->info->hook_entry,
1186 sizeof(table->info->hook_entry));
1188 ENTRY_ITERATE(table->blob_entries->entrytable,
1189 table->blob_entries->size,
1192 g_hash_table_insert(table_hash, g_strdup(table_name), table);
1198 table_cleanup(table);
1203 static struct xtables_target *prepare_target(struct connman_iptables *table,
1206 struct xtables_target *xt_t = NULL;
1207 gboolean is_builtin, is_user_defined;
1208 GList *chain_head = NULL;
1212 is_user_defined = FALSE;
1214 if (is_builtin_target(target_name))
1217 chain_head = find_chain_head(table, target_name);
1218 if (chain_head != NULL && chain_head->next != NULL)
1219 is_user_defined = TRUE;
1222 if (is_builtin || is_user_defined)
1223 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
1224 XTF_LOAD_MUST_SUCCEED);
1226 xt_t = xtables_find_target(target_name, XTF_TRY_LOAD);
1231 target_size = ALIGN(sizeof(struct ipt_entry_target)) + xt_t->size;
1233 xt_t->t = g_try_malloc0(target_size);
1234 if (xt_t->t == NULL)
1237 xt_t->t->u.target_size = target_size;
1239 if (is_builtin || is_user_defined) {
1240 struct xt_standard_target *target;
1242 target = (struct xt_standard_target *)(xt_t->t);
1243 strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
1245 if (is_builtin == TRUE)
1246 target->verdict = target_to_verdict(target_name);
1247 else if (is_user_defined == TRUE) {
1248 struct connman_iptables_entry *target_rule;
1250 if (chain_head == NULL) {
1255 target_rule = chain_head->next->data;
1256 target->verdict = target_rule->offset;
1259 strcpy(xt_t->t->u.user.name, target_name);
1260 xt_t->t->u.user.revision = xt_t->revision;
1261 if (xt_t->init != NULL)
1262 xt_t->init(xt_t->t);
1268 static struct option iptables_opts[] = {
1269 {.name = "append", .has_arg = 1, .val = 'A'},
1270 {.name = "delete", .has_arg = 1, .val = 'D'},
1271 {.name = "flush-chain", .has_arg = 1, .val = 'F'},
1272 {.name = "insert", .has_arg = 1, .val = 'I'},
1273 {.name = "list", .has_arg = 2, .val = 'L'},
1274 {.name = "new-chain", .has_arg = 1, .val = 'N'},
1275 {.name = "delete-chain", .has_arg = 1, .val = 'X'},
1276 {.name = "destination", .has_arg = 1, .val = 'd'},
1277 {.name = "in-interface", .has_arg = 1, .val = 'i'},
1278 {.name = "jump", .has_arg = 1, .val = 'j'},
1279 {.name = "match", .has_arg = 1, .val = 'm'},
1280 {.name = "out-interface", .has_arg = 1, .val = 'o'},
1281 {.name = "source", .has_arg = 1, .val = 's'},
1282 {.name = "table", .has_arg = 1, .val = 't'},
1286 struct xtables_globals iptables_globals = {
1288 .opts = iptables_opts,
1289 .orig_opts = iptables_opts,
1292 static int iptables_command(int argc, char *argv[])
1294 struct connman_iptables *table;
1295 struct xtables_match *xt_m;
1296 struct xtables_target *xt_t;
1298 char *table_name, *chain, *new_chain, *match_name, *target_name;
1299 char *flush_chain, *delete_chain;
1300 int c, ret, in_len, out_len;
1302 gboolean dump, invert, insert, delete;
1303 struct in_addr src, dst;
1312 table_name = chain = new_chain = match_name = target_name = NULL;
1313 flush_chain = delete_chain = NULL;
1314 memset(&ip, 0, sizeof(struct ipt_ip));
1322 while ((c = getopt_long(argc, argv, "-A:F:I:L::N:X:d:j:i:m:o:s:t:",
1323 iptables_globals.opts, NULL)) != -1) {
1326 /* It is either -A, -D or -I at once */
1334 /* It is either -A, -D or -I at once */
1343 flush_chain = optarg;
1347 /* It is either -A, -D or -I at once */
1364 delete_chain = optarg;
1368 if (!inet_pton(AF_INET, optarg, &dst))
1372 inet_pton(AF_INET, "255.255.255.255", &ip.dmsk);
1375 ip.invflags |= IPT_INV_DSTIP;
1380 in_len = strlen(optarg);
1382 if (in_len + 1 > IFNAMSIZ)
1385 strcpy(ip.iniface, optarg);
1386 memset(ip.iniface_mask, 0xff, in_len + 1);
1389 ip.invflags |= IPT_INV_VIA_IN;
1394 target_name = optarg;
1398 match_name = optarg;
1400 xt_m = xtables_find_match(optarg, XTF_LOAD_MUST_SUCCEED, NULL);
1401 size = ALIGN(sizeof(struct ipt_entry_match)) +
1403 xt_m->m = g_try_malloc0(size);
1406 xt_m->m->u.match_size = size;
1407 strcpy(xt_m->m->u.user.name, xt_m->name);
1408 xt_m->m->u.user.revision = xt_m->revision;
1409 if (xt_m->init != NULL)
1410 xt_m->init(xt_m->m);
1411 if (xt_m != xt_m->next) {
1412 iptables_globals.opts =
1413 xtables_merge_options(
1414 #if XTABLES_VERSION_CODE > 5
1415 iptables_globals.orig_opts,
1417 iptables_globals.opts,
1419 &xt_m->option_offset);
1420 if (iptables_globals.opts == NULL)
1427 out_len = strlen(optarg);
1429 if (out_len + 1 > IFNAMSIZ)
1432 strcpy(ip.outiface, optarg);
1433 memset(ip.outiface_mask, 0xff, out_len + 1);
1436 ip.invflags |= IPT_INV_VIA_OUT;
1441 if (!inet_pton(AF_INET, optarg, &src))
1445 inet_pton(AF_INET, "255.255.255.255", &ip.smsk);
1448 ip.invflags |= IPT_INV_SRCIP;
1453 table_name = optarg;
1457 if (optarg[0] == '!' && optarg[1] == '\0') {
1463 connman_error("Invalid option");
1469 if (xt_t == NULL || xt_t->parse == NULL ||
1470 !xt_t->parse(c - xt_t->option_offset, argv, invert,
1471 &xt_t->tflags, NULL, &xt_t->t)) {
1472 if (xt_m == NULL || xt_m->parse == NULL)
1475 xt_m->parse(c - xt_m->option_offset, argv,
1476 invert, &xt_m->mflags, NULL, &xt_m->m);
1485 if (table_name == NULL)
1486 table_name = "filter";
1488 table = iptables_init(table_name);
1489 if (table == NULL) {
1494 if (delete_chain != NULL) {
1495 printf("Delete chain %s\n", delete_chain);
1497 iptables_delete_chain(table, delete_chain);
1503 iptables_dump(table);
1510 DBG("Flush chain %s", flush_chain);
1512 iptables_flush_chain(table, flush_chain);
1517 if (chain && new_chain) {
1523 DBG("New chain %s", new_chain);
1525 ret = iptables_add_chain(table, new_chain);
1530 xt_t = prepare_target(table, target_name);
1534 iptables_globals.opts =
1535 xtables_merge_options(
1536 #if XTABLES_VERSION_CODE > 5
1537 iptables_globals.orig_opts,
1539 iptables_globals.opts,
1541 &xt_t->option_offset);
1542 if (iptables_globals.opts == NULL)
1545 if (delete == TRUE) {
1546 DBG("Deleting %s to %s (match %s)\n",
1547 target_name, chain, match_name);
1549 ret = iptables_delete_rule(table, &ip, chain,
1550 target_name, xt_t, match_name, xt_m);
1555 if (insert == TRUE) {
1556 DBG("Inserting %s to %s (match %s)",
1557 target_name, chain, match_name);
1559 ret = iptables_insert_rule(table, &ip, chain,
1560 target_name, xt_t, match_name, xt_m);
1564 DBG("Adding %s to %s (match %s)",
1565 target_name, chain, match_name);
1567 ret = iptables_append_rule(table, &ip, chain,
1568 target_name, xt_t, match_name, xt_m);
1584 int __connman_iptables_command(const char *format, ...)
1586 char **argv, **arguments, *command;
1593 va_start(args, format);
1595 command = g_strdup_vprintf(format, args);
1599 if (command == NULL)
1602 arguments = g_strsplit_set(command, " ", -1);
1604 for (argc = 0; arguments[argc]; argc++);
1607 DBG("command %s argc %d", command, argc);
1609 argv = g_try_malloc0(argc * sizeof(char *));
1612 g_strfreev(arguments);
1616 argv[0] = "iptables";
1617 for (i = 1; i < argc; i++)
1618 argv[i] = arguments[i - 1];
1620 ret = iptables_command(argc, argv);
1623 g_strfreev(arguments);
1630 int __connman_iptables_commit(const char *table_name)
1632 struct connman_iptables *table;
1633 struct ipt_replace *repl;
1636 DBG("%s", table_name);
1638 table = g_hash_table_lookup(table_hash, table_name);
1642 repl = iptables_blob(table);
1644 err = iptables_replace(table, repl);
1646 g_free(repl->counters);
1652 g_hash_table_remove(table_hash, table_name);
1657 static void remove_table(gpointer user_data)
1659 struct connman_iptables *table = user_data;
1661 table_cleanup(table);
1664 int __connman_iptables_init(void)
1668 table_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1669 g_free, remove_table);
1671 xtables_init_all(&iptables_globals, NFPROTO_IPV4);
1677 void __connman_iptables_cleanup(void)
1681 g_hash_table_destroy(table_hash);
1683 xtables_free_opts(1);