4 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include <sys/errno.h>
27 #include <sys/socket.h>
28 #include <sys/types.h>
29 #include <arpa/inet.h>
32 #include <linux/netfilter_ipv4/ip_tables.h>
36 static const char *hooknames[] = {
37 [NF_IP_PRE_ROUTING] = "PREROUTING",
38 [NF_IP_LOCAL_IN] = "INPUT",
39 [NF_IP_FORWARD] = "FORWARD",
40 [NF_IP_LOCAL_OUT] = "OUTPUT",
41 [NF_IP_POST_ROUTING] = "POSTROUTING",
44 #define LABEL_ACCEPT "ACCEPT"
45 #define LABEL_DROP "DROP"
46 #define LABEL_QUEUE "QUEUE"
47 #define LABEL_RETURN "RETURN"
49 /* fn returns 0 to continue iteration */
50 #define _XT_ENTRY_ITERATE_CONTINUE(type, entries, size, n, fn, args...) \
57 for (__i = 0, __n = 0; __i < (size); \
58 __i += __entry->next_offset, __n++) { \
59 __entry = (void *)(entries) + __i; \
63 __ret = fn(__entry, ## args); \
70 /* fn returns 0 to continue iteration */
71 #define _XT_ENTRY_ITERATE(type, entries, size, fn, args...) \
72 _XT_ENTRY_ITERATE_CONTINUE(type, entries, size, 0, fn, args)
74 #define ENTRY_ITERATE(entries, size, fn, args...) \
75 _XT_ENTRY_ITERATE(struct ipt_entry, entries, size, fn, ## args)
77 #define MIN_ALIGN (__alignof__(struct ipt_entry))
79 #define ALIGN(s) (((s) + ((MIN_ALIGN)-1)) & ~((MIN_ALIGN)-1))
82 struct xt_entry_target t;
83 char error[IPT_TABLE_MAXNAMELEN];
86 struct connman_iptables_entry {
90 struct ipt_entry *entry;
93 struct connman_iptables {
96 struct ipt_getinfo *info;
97 struct ipt_get_entries *blob_entries;
99 unsigned int num_entries;
100 unsigned int old_entries;
103 unsigned int underflow[NF_INET_NUMHOOKS];
104 unsigned int hook_entry[NF_INET_NUMHOOKS];
110 static struct ipt_entry *get_entry(struct connman_iptables *table,
113 return (struct ipt_entry *)((char *)table->blob_entries->entrytable +
117 static int is_hook_entry(struct connman_iptables *table,
118 struct ipt_entry *entry)
122 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
123 if ((table->info->valid_hooks & (1 << i))
124 && get_entry(table, table->info->hook_entry[i]) == entry)
131 static unsigned long entry_to_offset(struct connman_iptables *table,
132 struct ipt_entry *entry)
134 return (void *)entry - (void *)table->blob_entries->entrytable;
137 static int target_to_verdict(char *target_name)
139 if (!strcmp(target_name, LABEL_ACCEPT))
140 return -NF_ACCEPT - 1;
142 if (!strcmp(target_name, LABEL_DROP))
145 if (!strcmp(target_name, LABEL_QUEUE))
146 return -NF_QUEUE - 1;
148 if (!strcmp(target_name, LABEL_RETURN))
154 static gboolean is_builtin_target(char *target_name)
156 if (!strcmp(target_name, LABEL_ACCEPT) ||
157 !strcmp(target_name, LABEL_DROP) ||
158 !strcmp(target_name, LABEL_QUEUE) ||
159 !strcmp(target_name, LABEL_RETURN))
165 static gboolean is_jump(struct connman_iptables_entry *e)
167 struct xt_entry_target *target;
169 target = ipt_get_target(e->entry);
171 if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
172 struct xt_standard_target *t;
174 t = (struct xt_standard_target *)target;
176 switch (t->verdict) {
192 static gboolean is_chain(struct connman_iptables *table,
193 struct connman_iptables_entry *e)
195 struct ipt_entry *entry;
196 struct xt_entry_target *target;
202 target = ipt_get_target(entry);
203 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET))
209 static GList *find_chain_head(struct connman_iptables *table,
213 struct connman_iptables_entry *head;
214 struct ipt_entry *entry;
215 struct xt_entry_target *target;
218 for (list = table->entries; list; list = list->next) {
223 builtin = head->builtin;
224 if (builtin >= 0 && !strcmp(hooknames[builtin], chain_name))
227 /* User defined chain */
228 target = ipt_get_target(entry);
229 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET) &&
230 !strcmp((char *)target->data, chain_name))
237 static GList *find_chain_tail(struct connman_iptables *table,
240 GList *chain_head, *list;
241 struct connman_iptables_entry *tail;
243 chain_head = find_chain_head(table, chain_name);
244 if (chain_head == NULL)
247 /* Then we look for the next chain */
248 for (list = chain_head->next; list; list = list->next) {
251 if (is_chain(table, tail))
255 /* Nothing found, we return the table end */
256 return g_list_last(table->entries);
259 static void update_offsets(struct connman_iptables *table)
262 struct connman_iptables_entry *entry, *prev_entry;
264 for (list = table->entries; list; list = list->next) {
267 if (list == table->entries) {
274 prev_entry = prev->data;
276 entry->offset = prev_entry->offset +
277 prev_entry->entry->next_offset;
281 static void update_targets_reference(struct connman_iptables *table,
282 struct connman_iptables_entry *entry_before,
283 struct connman_iptables_entry *modified_entry,
284 gboolean is_removing)
286 struct connman_iptables_entry *tmp;
287 struct xt_standard_target *t;
291 offset = modified_entry->entry->next_offset;
293 for (list = table->entries; list; list = list->next) {
299 t = (struct xt_standard_target *)ipt_get_target(tmp->entry);
301 if (is_removing == TRUE) {
302 if (t->verdict >= entry_before->offset)
303 t->verdict -= offset;
305 if (t->verdict > entry_before->offset)
306 t->verdict += offset;
311 static int connman_add_entry(struct connman_iptables *table,
312 struct ipt_entry *entry, GList *before,
315 struct connman_iptables_entry *e, *entry_before;
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 appended/insterted a new entry. All references
341 * should be bumped accordingly.
343 update_targets_reference(table, entry_before, e, FALSE);
345 update_offsets(table);
350 static int remove_table_entry(struct connman_iptables *table,
351 struct connman_iptables_entry *entry)
355 table->num_entries--;
356 table->size -= entry->entry->next_offset;
357 removed = entry->entry->next_offset;
359 g_free(entry->entry);
361 table->entries = g_list_remove(table->entries, entry);
366 static int connman_iptables_flush_chain(struct connman_iptables *table,
369 GList *chain_head, *chain_tail, *list, *next;
370 struct connman_iptables_entry *entry;
371 int builtin, removed = 0;
373 chain_head = find_chain_head(table, name);
374 if (chain_head == NULL)
377 chain_tail = find_chain_tail(table, name);
378 if (chain_tail == NULL)
381 entry = chain_head->data;
382 builtin = entry->builtin;
387 list = chain_head->next;
389 if (list == chain_tail->prev)
392 while (list != chain_tail->prev) {
394 next = g_list_next(list);
396 removed += remove_table_entry(table, entry);
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 connman_iptables_delete_chain(struct connman_iptables *table,
430 GList *chain_head, *chain_tail;
431 struct connman_iptables_entry *entry;
433 chain_head = find_chain_head(table, name);
434 if (chain_head == NULL)
437 entry = chain_head->data;
439 /* We cannot remove builtin chain */
440 if (entry->builtin >= 0)
443 chain_tail = find_chain_tail(table, name);
444 if (chain_tail == NULL)
447 /* Chain must be flushed */
448 if (chain_head->next != chain_tail->prev)
451 remove_table_entry(table, entry);
453 entry = chain_tail->prev->data;
454 remove_table_entry(table, entry);
456 update_offsets(table);
461 static int connman_iptables_add_chain(struct connman_iptables *table,
465 struct ipt_entry *entry_head;
466 struct ipt_entry *entry_return;
467 struct error_target *error;
468 struct ipt_standard_target *standard;
469 u_int16_t entry_head_size, entry_return_size;
471 last = g_list_last(table->entries);
474 * An empty chain is composed of:
475 * - A head entry, with no match and an error target.
476 * The error target data is the chain name.
477 * - A tail entry, with no match and a standard target.
478 * The standard target verdict is XT_RETURN (return to the
483 entry_head_size = sizeof(struct ipt_entry) +
484 sizeof(struct error_target);
485 entry_head = g_try_malloc0(entry_head_size);
486 if (entry_head == NULL)
489 memset(entry_head, 0, entry_head_size);
491 entry_head->target_offset = sizeof(struct ipt_entry);
492 entry_head->next_offset = entry_head_size;
494 error = (struct error_target *) entry_head->elems;
495 strcpy(error->t.u.user.name, IPT_ERROR_TARGET);
496 error->t.u.user.target_size = ALIGN(sizeof(struct error_target));
497 strcpy(error->error, name);
499 if (connman_add_entry(table, entry_head, last, -1) < 0)
503 entry_return_size = sizeof(struct ipt_entry) +
504 sizeof(struct ipt_standard_target);
505 entry_return = g_try_malloc0(entry_return_size);
506 if (entry_return == NULL)
509 memset(entry_return, 0, entry_return_size);
511 entry_return->target_offset = sizeof(struct ipt_entry);
512 entry_return->next_offset = entry_return_size;
514 standard = (struct ipt_standard_target *) entry_return->elems;
515 standard->target.u.user.target_size =
516 ALIGN(sizeof(struct ipt_standard_target));
517 standard->verdict = XT_RETURN;
519 if (connman_add_entry(table, entry_return, last, -1) < 0)
525 g_free(entry_return);
532 static struct ipt_entry *
533 new_rule(struct ipt_ip *ip, char *target_name,
534 struct xtables_target *xt_t,
535 char *match_name, struct xtables_match *xt_m)
537 struct ipt_entry *new_entry;
538 size_t match_size, target_size;
541 match_size = xt_m->m->u.match_size;
546 target_size = ALIGN(xt_t->t->u.target_size);
548 target_size = ALIGN(sizeof(struct xt_standard_target));
550 new_entry = g_try_malloc0(sizeof(struct ipt_entry) + target_size +
552 if (new_entry == NULL)
555 memcpy(&new_entry->ip, ip, sizeof(struct ipt_ip));
557 new_entry->target_offset = sizeof(struct ipt_entry) + match_size;
558 new_entry->next_offset = sizeof(struct ipt_entry) + target_size +
561 struct xt_entry_match *entry_match;
563 entry_match = (struct xt_entry_match *)new_entry->elems;
564 memcpy(entry_match, xt_m->m, match_size);
568 struct xt_entry_target *entry_target;
570 entry_target = ipt_get_target(new_entry);
571 memcpy(entry_target, xt_t->t, target_size);
577 static void update_hooks(struct connman_iptables *table, GList *chain_head,
578 struct ipt_entry *entry)
581 struct connman_iptables_entry *head, *e;
584 if (chain_head == NULL)
587 head = chain_head->data;
589 builtin = head->builtin;
593 table->underflow[builtin] += entry->next_offset;
595 for (list = chain_head->next; list; list = list->next) {
598 builtin = e->builtin;
602 table->hook_entry[builtin] += entry->next_offset;
603 table->underflow[builtin] += entry->next_offset;
607 static struct ipt_entry *prepare_rule_inclusion(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,
613 GList *chain_tail, *chain_head;
614 struct ipt_entry *new_entry;
615 struct connman_iptables_entry *head;
617 chain_head = find_chain_head(table, chain_name);
618 if (chain_head == NULL)
621 chain_tail = find_chain_tail(table, chain_name);
622 if (chain_tail == NULL)
625 new_entry = new_rule(ip, target_name, xt_t, match_name, xt_m);
626 if (new_entry == NULL)
629 update_hooks(table, chain_head, new_entry);
632 * If the chain is builtin, and does not have any rule,
633 * then the one that we're inserting is becoming the head
634 * and thus needs the builtin flag.
636 head = chain_head->data;
637 if (head->builtin < 0)
639 else if (chain_head == chain_tail->prev) {
640 *builtin = head->builtin;
648 connman_iptables_append_rule(struct connman_iptables *table,
649 struct ipt_ip *ip, char *chain_name,
650 char *target_name, struct xtables_target *xt_t,
651 char *match_name, struct xtables_match *xt_m)
654 struct ipt_entry *new_entry;
655 int builtin = -1, ret;
657 chain_tail = find_chain_tail(table, chain_name);
658 if (chain_tail == NULL)
661 new_entry = prepare_rule_inclusion(table, ip, chain_name,
662 target_name, xt_t, match_name, xt_m, &builtin);
663 if (new_entry == NULL)
666 ret = connman_add_entry(table, new_entry, chain_tail->prev, builtin);
673 static int connman_iptables_insert_rule(struct connman_iptables *table,
674 struct ipt_ip *ip, char *chain_name,
675 char *target_name, struct xtables_target *xt_t,
676 char *match_name, struct xtables_match *xt_m)
679 struct ipt_entry *new_entry;
680 int builtin = -1, ret;
682 chain_head = find_chain_head(table, chain_name);
683 if (chain_head == NULL)
686 new_entry = prepare_rule_inclusion(table, ip, chain_name,
687 target_name, xt_t, match_name, xt_m, &builtin);
688 if (new_entry == NULL)
691 ret = connman_add_entry(table, new_entry, chain_head->next, builtin);
698 static gboolean is_same_ipt_entry(struct ipt_entry *i_e1,
699 struct ipt_entry *i_e2)
701 if (memcmp(&i_e1->ip, &i_e2->ip, sizeof(struct ipt_ip)) != 0)
704 if (i_e1->target_offset != i_e2->target_offset)
707 if (i_e1->next_offset != i_e2->next_offset)
713 static gboolean is_same_target(struct xt_entry_target *xt_e_t1,
714 struct xt_entry_target *xt_e_t2)
716 if (xt_e_t1 == NULL || xt_e_t2 == NULL)
719 if (strcmp(xt_e_t1->u.user.name, IPT_STANDARD_TARGET) == 0) {
720 struct xt_standard_target *xt_s_t1;
721 struct xt_standard_target *xt_s_t2;
723 xt_s_t1 = (struct xt_standard_target *) xt_e_t1;
724 xt_s_t2 = (struct xt_standard_target *) xt_e_t2;
726 if (xt_s_t1->verdict != xt_s_t2->verdict)
729 if (xt_e_t1->u.target_size != xt_e_t2->u.target_size)
732 if (strcmp(xt_e_t1->u.user.name, xt_e_t2->u.user.name) != 0)
739 static gboolean is_same_match(struct xt_entry_match *xt_e_m1,
740 struct xt_entry_match *xt_e_m2)
742 if (xt_e_m1 == NULL || xt_e_m2 == NULL)
745 if (xt_e_m1->u.match_size != xt_e_m2->u.match_size)
748 if (xt_e_m1->u.user.revision != xt_e_m2->u.user.revision)
751 if (strcmp(xt_e_m1->u.user.name, xt_e_m2->u.user.name) != 0)
757 static int connman_iptables_delete_rule(struct connman_iptables *table,
758 struct ipt_ip *ip, char *chain_name,
759 char *target_name, struct xtables_target *xt_t,
760 char *match_name, struct xtables_match *xt_m)
762 GList *chain_tail, *chain_head, *list;
763 struct xt_entry_target *xt_e_t = NULL;
764 struct xt_entry_match *xt_e_m = NULL;
765 struct connman_iptables_entry *entry;
766 struct ipt_entry *entry_test;
767 int builtin, removed;
771 chain_head = find_chain_head(table, chain_name);
772 if (chain_head == NULL)
775 chain_tail = find_chain_tail(table, chain_name);
776 if (chain_tail == NULL)
782 entry_test = new_rule(ip, target_name, xt_t, match_name, xt_m);
783 if (entry_test == NULL)
787 xt_e_t = ipt_get_target(entry_test);
789 xt_e_m = (struct xt_entry_match *)entry_test->elems;
791 entry = chain_head->data;
792 builtin = entry->builtin;
797 list = chain_head->next;
799 for (entry = NULL; list != chain_tail->prev; list = list->next) {
800 struct connman_iptables_entry *tmp;
801 struct ipt_entry *tmp_e;
806 if (is_same_ipt_entry(entry_test, tmp_e) == FALSE)
810 struct xt_entry_target *tmp_xt_e_t;
812 tmp_xt_e_t = ipt_get_target(tmp_e);
814 if (!is_same_target(tmp_xt_e_t, xt_e_t))
819 struct xt_entry_match *tmp_xt_e_m;
821 tmp_xt_e_m = (struct xt_entry_match *)tmp_e->elems;
823 if (!is_same_match(tmp_xt_e_m, xt_e_m))
836 /* We have deleted a rule,
837 * all references should be bumped accordingly */
838 if (list->next != NULL)
839 update_targets_reference(table, list->next->data,
842 removed += remove_table_entry(table, entry);
848 entry->builtin = builtin;
851 table->underflow[builtin] -= removed;
852 for (list = chain_tail; list; list = list->next) {
855 builtin = entry->builtin;
859 table->hook_entry[builtin] -= removed;
860 table->underflow[builtin] -= removed;
864 update_offsets(table);
869 static struct ipt_replace *
870 connman_iptables_blob(struct connman_iptables *table)
872 struct ipt_replace *r;
874 struct connman_iptables_entry *e;
875 unsigned char *entry_index;
877 r = g_try_malloc0(sizeof(struct ipt_replace) + table->size);
881 memset(r, 0, sizeof(*r) + table->size);
883 r->counters = g_try_malloc0(sizeof(struct xt_counters)
884 * table->old_entries);
885 if (r->counters == NULL) {
890 strcpy(r->name, table->info->name);
891 r->num_entries = table->num_entries;
892 r->size = table->size;
894 r->num_counters = table->old_entries;
895 r->valid_hooks = table->info->valid_hooks;
897 memcpy(r->hook_entry, table->hook_entry, sizeof(table->hook_entry));
898 memcpy(r->underflow, table->underflow, sizeof(table->underflow));
900 entry_index = (unsigned char *)r->entries;
901 for (list = table->entries; list; list = list->next) {
904 memcpy(entry_index, e->entry, e->entry->next_offset);
905 entry_index += e->entry->next_offset;
911 static void dump_target(struct connman_iptables *table,
912 struct ipt_entry *entry)
915 struct xtables_target *xt_t;
916 struct xt_entry_target *target;
918 target = ipt_get_target(entry);
920 if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
921 struct xt_standard_target *t;
923 t = (struct xt_standard_target *)target;
925 switch (t->verdict) {
927 printf("\ttarget RETURN\n");
931 printf("\ttarget ACCEPT\n");
935 printf("\ttarget DROP\n");
939 printf("\ttarget QUEUE\n");
943 printf("\ttarget STOP\n");
947 printf("\tJUMP @%p (0x%x)\n",
948 (char*)table->blob_entries->entrytable +
949 t->verdict, t->verdict);
953 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
954 XTF_LOAD_MUST_SUCCEED);
956 if(xt_t->print != NULL)
957 xt_t->print(NULL, target, 1);
959 xt_t = xtables_find_target(target->u.user.name, XTF_TRY_LOAD);
961 printf("\ttarget %s\n", target->u.user.name);
965 if(xt_t->print != NULL) {
967 xt_t->print(NULL, target, 1);
973 static void dump_match(struct connman_iptables *table, struct ipt_entry *entry)
975 struct xtables_match *xt_m;
976 struct xt_entry_match *match;
978 if (entry->elems == (unsigned char *)entry + entry->target_offset)
981 match = (struct xt_entry_match *) entry->elems;
983 if (!strlen(match->u.user.name))
986 xt_m = xtables_find_match(match->u.user.name, XTF_TRY_LOAD, NULL);
990 if(xt_m->print != NULL) {
992 xt_m->print(NULL, match, 1);
999 printf("\tmatch %s\n", match->u.user.name);
1003 static int connman_iptables_dump_entry(struct ipt_entry *entry,
1004 struct connman_iptables *table)
1006 struct xt_entry_target *target;
1007 unsigned int offset;
1010 offset = (char *)entry - (char *)table->blob_entries->entrytable;
1011 target = ipt_get_target(entry);
1012 builtin = is_hook_entry(table, entry);
1014 if (entry_to_offset(table, entry) + entry->next_offset ==
1015 table->blob_entries->size) {
1016 printf("End of CHAIN 0x%x\n", offset);
1020 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET)) {
1021 printf("USER CHAIN (%s) %p match %p target %p size %d\n",
1022 target->data, entry, entry->elems,
1023 (char *)entry + entry->target_offset,
1024 entry->next_offset);
1027 } else if (builtin >= 0) {
1028 printf("CHAIN (%s) %p match %p target %p size %d\n",
1029 hooknames[builtin], entry, entry->elems,
1030 (char *)entry + entry->target_offset,
1031 entry->next_offset);
1033 printf("RULE %p match %p target %p size %d\n", entry,
1035 (char *)entry + entry->target_offset,
1036 entry->next_offset);
1039 dump_match(table, entry);
1040 dump_target(table, entry);
1045 static void connman_iptables_dump_hook(struct connman_iptables *table)
1048 printf("hooks: \n");
1049 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
1050 if ((table->info->valid_hooks & (1 << i)))
1051 printf("%s entry %p underflow %p (%#x)\n",
1053 table->blob_entries->entrytable +
1054 table->info->hook_entry[i],
1055 table->blob_entries->entrytable +
1056 table->info->underflow[i],
1057 table->info->underflow[i]);
1061 static void connman_iptables_dump(struct connman_iptables *table)
1063 printf("%s valid_hooks=0x%08x, num_entries=%u, size=%u\n",
1065 table->info->valid_hooks, table->info->num_entries,
1068 connman_iptables_dump_hook(table);
1070 ENTRY_ITERATE(table->blob_entries->entrytable,
1071 table->blob_entries->size,
1072 connman_iptables_dump_entry, table);
1076 static int connman_iptables_get_entries(struct connman_iptables *table)
1078 socklen_t entry_size;
1080 entry_size = sizeof(struct ipt_get_entries) + table->info->size;
1082 return getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_ENTRIES,
1083 table->blob_entries, &entry_size);
1086 static int connman_iptables_replace(struct connman_iptables *table,
1087 struct ipt_replace *r)
1089 return setsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_SET_REPLACE, r,
1090 sizeof(*r) + r->size);
1093 static void connman_iptables_cleanup(struct connman_iptables *table)
1096 struct connman_iptables_entry *entry;
1098 close(table->ipt_sock);
1100 for (list = table->entries; list; list = list->next) {
1103 g_free(entry->entry);
1106 g_free(table->info);
1107 g_free(table->blob_entries);
1110 xtables_free_opts(1);
1113 static int connman_iptables_commit(struct connman_iptables *table)
1115 struct ipt_replace *repl;
1117 repl = connman_iptables_blob(table);
1119 return connman_iptables_replace(table, repl);
1122 static int add_entry(struct ipt_entry *entry, struct connman_iptables *table)
1124 struct ipt_entry *new_entry;
1127 new_entry = g_try_malloc0(entry->next_offset);
1128 if (new_entry == NULL)
1131 memcpy(new_entry, entry, entry->next_offset);
1133 builtin = is_hook_entry(table, entry);
1135 return connman_add_entry(table, new_entry, NULL, builtin);
1138 static struct connman_iptables *connman_iptables_init(const char *table_name)
1140 struct connman_iptables *table;
1143 table = g_try_new0(struct connman_iptables, 1);
1147 table->info = g_try_new0(struct ipt_getinfo, 1);
1148 if (table->info == NULL)
1151 table->ipt_sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
1152 if (table->ipt_sock < 0)
1155 s = sizeof(*table->info);
1156 strcpy(table->info->name, table_name);
1157 if (getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_INFO,
1158 table->info, &s) < 0)
1161 table->blob_entries = g_try_malloc0(sizeof(struct ipt_get_entries) +
1163 if (table->blob_entries == NULL)
1166 strcpy(table->blob_entries->name, table_name);
1167 table->blob_entries->size = table->info->size;
1169 if (connman_iptables_get_entries(table) < 0)
1172 table->num_entries = 0;
1173 table->old_entries = table->info->num_entries;
1176 memcpy(table->underflow, table->info->underflow,
1177 sizeof(table->info->underflow));
1178 memcpy(table->hook_entry, table->info->hook_entry,
1179 sizeof(table->info->hook_entry));
1181 ENTRY_ITERATE(table->blob_entries->entrytable,
1182 table->blob_entries->size,
1190 connman_iptables_cleanup(table);
1195 static struct xtables_target *prepare_target(struct connman_iptables *table,
1198 struct xtables_target *xt_t = NULL;
1199 gboolean is_builtin, is_user_defined;
1200 GList *chain_head = NULL;
1204 is_user_defined = FALSE;
1206 if (is_builtin_target(target_name))
1209 chain_head = find_chain_head(table, target_name);
1210 if (chain_head != NULL && chain_head->next != NULL)
1211 is_user_defined = TRUE;
1214 if (is_builtin || is_user_defined)
1215 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
1216 XTF_LOAD_MUST_SUCCEED);
1218 xt_t = xtables_find_target(target_name, XTF_TRY_LOAD);
1223 target_size = ALIGN(sizeof(struct ipt_entry_target)) + xt_t->size;
1225 xt_t->t = g_try_malloc0(target_size);
1226 if (xt_t->t == NULL)
1229 xt_t->t->u.target_size = target_size;
1231 if (is_builtin || is_user_defined) {
1232 struct xt_standard_target *target;
1234 target = (struct xt_standard_target *)(xt_t->t);
1235 strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
1237 if (is_builtin == TRUE)
1238 target->verdict = target_to_verdict(target_name);
1239 else if (is_user_defined == TRUE) {
1240 struct connman_iptables_entry *target_rule;
1242 if (chain_head == NULL) {
1247 target_rule = chain_head->next->data;
1248 target->verdict = target_rule->offset;
1251 strcpy(xt_t->t->u.user.name, target_name);
1252 xt_t->t->u.user.revision = xt_t->revision;
1253 if (xt_t->init != NULL)
1254 xt_t->init(xt_t->t);
1260 static struct option connman_iptables_opts[] = {
1261 {.name = "append", .has_arg = 1, .val = 'A'},
1262 {.name = "delete", .has_arg = 1, .val = 'D'},
1263 {.name = "flush-chain", .has_arg = 1, .val = 'F'},
1264 {.name = "insert", .has_arg = 1, .val = 'I'},
1265 {.name = "list", .has_arg = 2, .val = 'L'},
1266 {.name = "new-chain", .has_arg = 1, .val = 'N'},
1267 {.name = "delete-chain", .has_arg = 1, .val = 'X'},
1268 {.name = "destination", .has_arg = 1, .val = 'd'},
1269 {.name = "in-interface", .has_arg = 1, .val = 'i'},
1270 {.name = "jump", .has_arg = 1, .val = 'j'},
1271 {.name = "match", .has_arg = 1, .val = 'm'},
1272 {.name = "out-interface", .has_arg = 1, .val = 'o'},
1273 {.name = "source", .has_arg = 1, .val = 's'},
1274 {.name = "table", .has_arg = 1, .val = 't'},
1278 struct xtables_globals connman_iptables_globals = {
1280 .opts = connman_iptables_opts,
1281 .orig_opts = connman_iptables_opts,
1284 int main(int argc, char *argv[])
1286 struct connman_iptables *table;
1287 struct xtables_match *xt_m;
1288 struct xtables_target *xt_t;
1290 char *table_name, *chain, *new_chain, *match_name, *target_name;
1291 char *delete_chain, *flush_chain;
1292 int c, in_len, out_len;
1294 gboolean dump, invert, delete, insert, delete_rule;
1295 struct in_addr src, dst;
1297 xtables_init_all(&connman_iptables_globals, NFPROTO_IPV4);
1303 delete_rule = FALSE;
1304 table_name = chain = new_chain = match_name = target_name = NULL;
1305 delete_chain = flush_chain = NULL;
1306 memset(&ip, 0, sizeof(struct ipt_ip));
1311 while ((c = getopt_long(argc, argv, "-A:D:F:I:L::N:X:d:i:j:m:o:s:t:",
1312 connman_iptables_globals.opts, NULL)) != -1) {
1315 /* It is either -A, -D or -I at once */
1323 /* It is either -A, -D or -I at once */
1332 flush_chain = optarg;
1336 /* It is either -A, -D or -I at once */
1354 delete_chain = optarg;
1358 if (!inet_pton(AF_INET, optarg, &dst))
1362 inet_pton(AF_INET, "255.255.255.255", &ip.dmsk);
1365 ip.invflags |= IPT_INV_DSTIP;
1370 in_len = strlen(optarg);
1372 if (in_len + 1 > IFNAMSIZ)
1375 strcpy(ip.iniface, optarg);
1376 memset(ip.iniface_mask, 0xff, in_len + 1);
1379 ip.invflags |= IPT_INV_VIA_IN;
1384 target_name = optarg;
1388 match_name = optarg;
1390 xt_m = xtables_find_match(optarg, XTF_LOAD_MUST_SUCCEED, NULL);
1391 size = ALIGN(sizeof(struct ipt_entry_match)) + xt_m->size;
1392 xt_m->m = g_try_malloc0(size);
1395 xt_m->m->u.match_size = size;
1396 strcpy(xt_m->m->u.user.name, xt_m->name);
1397 xt_m->m->u.user.revision = xt_m->revision;
1398 if (xt_m->init != NULL)
1399 xt_m->init(xt_m->m);
1400 if (xt_m != xt_m->next) {
1401 connman_iptables_globals.opts =
1402 xtables_merge_options(
1403 #if XTABLES_VERSION_CODE > 5
1404 connman_iptables_globals.orig_opts,
1406 connman_iptables_globals.opts,
1408 &xt_m->option_offset);
1409 if (connman_iptables_globals.opts == NULL)
1416 out_len = strlen(optarg);
1418 if (out_len + 1 > IFNAMSIZ)
1421 strcpy(ip.outiface, optarg);
1422 memset(ip.outiface_mask, 0xff, out_len + 1);
1425 ip.invflags |= IPT_INV_VIA_OUT;
1430 if (!inet_pton(AF_INET, optarg, &src))
1434 inet_pton(AF_INET, "255.255.255.255", &ip.smsk);
1437 ip.invflags |= IPT_INV_SRCIP;
1442 table_name = optarg;
1446 if (optarg[0] == '!' && optarg[1] == '\0') {
1448 printf("Consecutive ! not allowed\n");
1455 printf("Invalid option\n");
1460 if (xt_t == NULL || xt_t->parse == NULL ||
1461 !xt_t->parse(c - xt_t->option_offset, argv, invert,
1462 &xt_t->tflags, NULL, &xt_t->t)) {
1463 if (xt_m == NULL || xt_m->parse == NULL)
1466 xt_m->parse(c - xt_m->option_offset, argv,
1467 invert, &xt_m->mflags, NULL, &xt_m->m);
1474 if (table_name == NULL)
1475 table_name = "filter";
1477 table = connman_iptables_init(table_name);
1482 if (delete_chain == NULL)
1485 printf("Delete chain %s\n", delete_chain);
1487 connman_iptables_delete_chain(table, delete_chain);
1493 printf("Flush chain %s\n", flush_chain);
1495 connman_iptables_flush_chain(table, flush_chain);
1501 connman_iptables_dump(table);
1506 if (chain && new_chain)
1510 printf("New chain %s\n", new_chain);
1512 connman_iptables_add_chain(table, new_chain);
1518 xt_t = prepare_target(table, target_name);
1522 connman_iptables_globals.opts =
1523 xtables_merge_options(
1524 #if XTABLES_VERSION_CODE > 5
1525 connman_iptables_globals.orig_opts,
1527 connman_iptables_globals.opts,
1529 &xt_t->option_offset);
1530 if (connman_iptables_globals.opts == NULL)
1533 if (delete_rule == TRUE) {
1534 printf("Deleting %s to %s (match %s)\n", target_name,
1537 connman_iptables_delete_rule(table, &ip, chain,
1538 target_name, xt_t, match_name, xt_m);
1543 if (insert == TRUE) {
1544 printf("Inserting %s to %s (match %s)\n", target_name,
1547 connman_iptables_insert_rule(table, &ip, chain,
1548 target_name, xt_t, match_name, xt_m);
1550 printf("Appending %s to %s (match %s)\n", target_name,
1553 connman_iptables_append_rule(table, &ip, chain,
1554 target_name, xt_t, match_name, xt_m);
1560 connman_iptables_commit(table);
1563 connman_iptables_cleanup(table);