4 * Copyright (C) 2007-2011 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 *new_rule(struct ipt_ip *ip,
533 char *target_name, struct xtables_target *xt_t,
534 char *match_name, struct xtables_match *xt_m)
536 struct ipt_entry *new_entry;
537 size_t match_size, target_size;
540 match_size = xt_m->m->u.match_size;
545 target_size = ALIGN(xt_t->t->u.target_size);
547 target_size = ALIGN(sizeof(struct xt_standard_target));
549 new_entry = g_try_malloc0(sizeof(struct ipt_entry) + target_size +
551 if (new_entry == NULL)
554 memcpy(&new_entry->ip, ip, sizeof(struct ipt_ip));
556 new_entry->target_offset = sizeof(struct ipt_entry) + match_size;
557 new_entry->next_offset = sizeof(struct ipt_entry) + target_size +
560 struct xt_entry_match *entry_match;
562 entry_match = (struct xt_entry_match *)new_entry->elems;
563 memcpy(entry_match, xt_m->m, match_size);
567 struct xt_entry_target *entry_target;
569 entry_target = ipt_get_target(new_entry);
570 memcpy(entry_target, xt_t->t, target_size);
576 static void update_hooks(struct connman_iptables *table, GList *chain_head,
577 struct ipt_entry *entry)
580 struct connman_iptables_entry *head, *e;
583 if (chain_head == NULL)
586 head = chain_head->data;
588 builtin = head->builtin;
592 table->underflow[builtin] += entry->next_offset;
594 for (list = chain_head->next; list; list = list->next) {
597 builtin = e->builtin;
601 table->hook_entry[builtin] += entry->next_offset;
602 table->underflow[builtin] += entry->next_offset;
606 static struct ipt_entry *prepare_rule_inclusion(struct connman_iptables *table,
607 struct ipt_ip *ip, char *chain_name,
608 char *target_name, struct xtables_target *xt_t,
609 char *match_name, struct xtables_match *xt_m,
612 GList *chain_tail, *chain_head;
613 struct ipt_entry *new_entry;
614 struct connman_iptables_entry *head;
616 chain_head = find_chain_head(table, chain_name);
617 if (chain_head == NULL)
620 chain_tail = find_chain_tail(table, chain_name);
621 if (chain_tail == NULL)
624 new_entry = new_rule(ip, target_name, xt_t, match_name, xt_m);
625 if (new_entry == NULL)
628 update_hooks(table, chain_head, new_entry);
631 * If the chain is builtin, and does not have any rule,
632 * then the one that we're inserting is becoming the head
633 * and thus needs the builtin flag.
635 head = chain_head->data;
636 if (head->builtin < 0)
638 else if (chain_head == chain_tail->prev) {
639 *builtin = head->builtin;
646 static int connman_iptables_append_rule(struct connman_iptables *table,
647 struct ipt_ip *ip, char *chain_name,
648 char *target_name, struct xtables_target *xt_t,
649 char *match_name, struct xtables_match *xt_m)
652 struct ipt_entry *new_entry;
653 int builtin = -1, ret;
655 chain_tail = find_chain_tail(table, chain_name);
656 if (chain_tail == NULL)
659 new_entry = prepare_rule_inclusion(table, ip, chain_name,
660 target_name, xt_t, match_name, xt_m, &builtin);
661 if (new_entry == NULL)
664 ret = connman_add_entry(table, new_entry, chain_tail->prev, builtin);
671 static int connman_iptables_insert_rule(struct connman_iptables *table,
672 struct ipt_ip *ip, char *chain_name,
673 char *target_name, struct xtables_target *xt_t,
674 char *match_name, struct xtables_match *xt_m)
677 struct ipt_entry *new_entry;
678 int builtin = -1, ret;
680 chain_head = find_chain_head(table, chain_name);
681 if (chain_head == NULL)
684 new_entry = prepare_rule_inclusion(table, ip, chain_name,
685 target_name, xt_t, match_name, xt_m, &builtin);
686 if (new_entry == NULL)
689 ret = connman_add_entry(table, new_entry, chain_head->next, builtin);
696 static gboolean is_same_ipt_entry(struct ipt_entry *i_e1,
697 struct ipt_entry *i_e2)
699 if (memcmp(&i_e1->ip, &i_e2->ip, sizeof(struct ipt_ip)) != 0)
702 if (i_e1->target_offset != i_e2->target_offset)
705 if (i_e1->next_offset != i_e2->next_offset)
711 static gboolean is_same_target(struct xt_entry_target *xt_e_t1,
712 struct xt_entry_target *xt_e_t2)
714 if (xt_e_t1 == NULL || xt_e_t2 == NULL)
717 if (strcmp(xt_e_t1->u.user.name, IPT_STANDARD_TARGET) == 0) {
718 struct xt_standard_target *xt_s_t1;
719 struct xt_standard_target *xt_s_t2;
721 xt_s_t1 = (struct xt_standard_target *) xt_e_t1;
722 xt_s_t2 = (struct xt_standard_target *) xt_e_t2;
724 if (xt_s_t1->verdict != xt_s_t2->verdict)
727 if (xt_e_t1->u.target_size != xt_e_t2->u.target_size)
730 if (strcmp(xt_e_t1->u.user.name, xt_e_t2->u.user.name) != 0)
737 static gboolean is_same_match(struct xt_entry_match *xt_e_m1,
738 struct xt_entry_match *xt_e_m2)
740 if (xt_e_m1 == NULL || xt_e_m2 == NULL)
743 if (xt_e_m1->u.match_size != xt_e_m2->u.match_size)
746 if (xt_e_m1->u.user.revision != xt_e_m2->u.user.revision)
749 if (strcmp(xt_e_m1->u.user.name, xt_e_m2->u.user.name) != 0)
755 static int connman_iptables_delete_rule(struct connman_iptables *table,
756 struct ipt_ip *ip, char *chain_name,
757 char *target_name, struct xtables_target *xt_t,
758 char *match_name, struct xtables_match *xt_m)
760 GList *chain_tail, *chain_head, *list;
761 struct xt_entry_target *xt_e_t = NULL;
762 struct xt_entry_match *xt_e_m = NULL;
763 struct connman_iptables_entry *entry;
764 struct ipt_entry *entry_test;
765 int builtin, removed;
769 chain_head = find_chain_head(table, chain_name);
770 if (chain_head == NULL)
773 chain_tail = find_chain_tail(table, chain_name);
774 if (chain_tail == NULL)
780 entry_test = new_rule(ip, target_name, xt_t, match_name, xt_m);
781 if (entry_test == NULL)
785 xt_e_t = ipt_get_target(entry_test);
787 xt_e_m = (struct xt_entry_match *)entry_test->elems;
789 entry = chain_head->data;
790 builtin = entry->builtin;
795 list = chain_head->next;
797 for (entry = NULL; list != chain_tail->prev; list = list->next) {
798 struct connman_iptables_entry *tmp;
799 struct ipt_entry *tmp_e;
804 if (is_same_ipt_entry(entry_test, tmp_e) == FALSE)
808 struct xt_entry_target *tmp_xt_e_t;
810 tmp_xt_e_t = ipt_get_target(tmp_e);
812 if (!is_same_target(tmp_xt_e_t, xt_e_t))
817 struct xt_entry_match *tmp_xt_e_m;
819 tmp_xt_e_m = (struct xt_entry_match *)tmp_e->elems;
821 if (!is_same_match(tmp_xt_e_m, xt_e_m))
834 /* We have deleted a rule,
835 * all references should be bumped accordingly */
836 if (list->next != NULL)
837 update_targets_reference(table, list->next->data,
840 removed += remove_table_entry(table, entry);
846 entry->builtin = builtin;
849 table->underflow[builtin] -= removed;
850 for (list = chain_tail; list; list = list->next) {
853 builtin = entry->builtin;
857 table->hook_entry[builtin] -= removed;
858 table->underflow[builtin] -= removed;
862 update_offsets(table);
867 static struct ipt_replace *connman_iptables_blob(struct connman_iptables *table)
869 struct ipt_replace *r;
871 struct connman_iptables_entry *e;
872 unsigned char *entry_index;
874 r = g_try_malloc0(sizeof(struct ipt_replace) + table->size);
878 memset(r, 0, sizeof(*r) + table->size);
880 r->counters = g_try_malloc0(sizeof(struct xt_counters)
881 * table->old_entries);
882 if (r->counters == NULL) {
887 strcpy(r->name, table->info->name);
888 r->num_entries = table->num_entries;
889 r->size = table->size;
891 r->num_counters = table->old_entries;
892 r->valid_hooks = table->info->valid_hooks;
894 memcpy(r->hook_entry, table->hook_entry, sizeof(table->hook_entry));
895 memcpy(r->underflow, table->underflow, sizeof(table->underflow));
897 entry_index = (unsigned char *)r->entries;
898 for (list = table->entries; list; list = list->next) {
901 memcpy(entry_index, e->entry, e->entry->next_offset);
902 entry_index += e->entry->next_offset;
908 static void dump_target(struct connman_iptables *table,
909 struct ipt_entry *entry)
912 struct xtables_target *xt_t;
913 struct xt_entry_target *target;
915 target = ipt_get_target(entry);
917 if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
918 struct xt_standard_target *t;
920 t = (struct xt_standard_target *)target;
922 switch (t->verdict) {
924 printf("\ttarget RETURN\n");
928 printf("\ttarget ACCEPT\n");
932 printf("\ttarget DROP\n");
936 printf("\ttarget QUEUE\n");
940 printf("\ttarget STOP\n");
944 printf("\tJUMP @%p (0x%x)\n",
945 (char*)table->blob_entries->entrytable +
946 t->verdict, t->verdict);
950 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
951 XTF_LOAD_MUST_SUCCEED);
953 if(xt_t->print != NULL)
954 xt_t->print(NULL, target, 1);
956 xt_t = xtables_find_target(target->u.user.name, XTF_TRY_LOAD);
958 printf("\ttarget %s\n", target->u.user.name);
962 if(xt_t->print != NULL) {
964 xt_t->print(NULL, target, 1);
970 static void dump_match(struct connman_iptables *table, struct ipt_entry *entry)
972 struct xtables_match *xt_m;
973 struct xt_entry_match *match;
975 if (entry->elems == (unsigned char *)entry + entry->target_offset)
978 match = (struct xt_entry_match *) entry->elems;
980 if (!strlen(match->u.user.name))
983 xt_m = xtables_find_match(match->u.user.name, XTF_TRY_LOAD, NULL);
987 if(xt_m->print != NULL) {
989 xt_m->print(NULL, match, 1);
996 printf("\tmatch %s\n", match->u.user.name);
1000 static int connman_iptables_dump_entry(struct ipt_entry *entry,
1001 struct connman_iptables *table)
1003 struct xt_entry_target *target;
1004 unsigned int offset;
1007 offset = (char *)entry - (char *)table->blob_entries->entrytable;
1008 target = ipt_get_target(entry);
1009 builtin = is_hook_entry(table, entry);
1011 if (entry_to_offset(table, entry) + entry->next_offset ==
1012 table->blob_entries->size) {
1013 printf("End of CHAIN 0x%x\n", offset);
1017 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET)) {
1018 printf("USER CHAIN (%s) %p match %p target %p size %d\n",
1019 target->data, entry, entry->elems,
1020 (char *)entry + entry->target_offset,
1021 entry->next_offset);
1024 } else if (builtin >= 0) {
1025 printf("CHAIN (%s) %p match %p target %p size %d\n",
1026 hooknames[builtin], entry, entry->elems,
1027 (char *)entry + entry->target_offset,
1028 entry->next_offset);
1030 printf("RULE %p match %p target %p size %d\n", entry,
1032 (char *)entry + entry->target_offset,
1033 entry->next_offset);
1036 dump_match(table, entry);
1037 dump_target(table, entry);
1042 static void connman_iptables_dump_hook(struct connman_iptables *table)
1045 printf("hooks: \n");
1046 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
1047 if ((table->info->valid_hooks & (1 << i)))
1048 printf("%s entry %p underflow %p (%#x)\n",
1050 table->blob_entries->entrytable +
1051 table->info->hook_entry[i],
1052 table->blob_entries->entrytable +
1053 table->info->underflow[i],
1054 table->info->underflow[i]);
1058 static void connman_iptables_dump(struct connman_iptables *table)
1060 printf("%s valid_hooks=0x%08x, num_entries=%u, size=%u\n",
1062 table->info->valid_hooks, table->info->num_entries,
1065 connman_iptables_dump_hook(table);
1067 ENTRY_ITERATE(table->blob_entries->entrytable,
1068 table->blob_entries->size,
1069 connman_iptables_dump_entry, table);
1073 static int connman_iptables_get_entries(struct connman_iptables *table)
1075 socklen_t entry_size;
1077 entry_size = sizeof(struct ipt_get_entries) + table->info->size;
1079 return getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_ENTRIES,
1080 table->blob_entries, &entry_size);
1083 static int connman_iptables_replace(struct connman_iptables *table,
1084 struct ipt_replace *r)
1086 return setsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_SET_REPLACE, r,
1087 sizeof(*r) + r->size);
1090 static void connman_iptables_cleanup(struct connman_iptables *table)
1093 struct connman_iptables_entry *entry;
1095 close(table->ipt_sock);
1097 for (list = table->entries; list; list = list->next) {
1100 g_free(entry->entry);
1103 g_free(table->info);
1104 g_free(table->blob_entries);
1107 xtables_free_opts(1);
1110 static int connman_iptables_commit(struct connman_iptables *table)
1112 struct ipt_replace *repl;
1114 repl = connman_iptables_blob(table);
1116 return connman_iptables_replace(table, repl);
1119 static int add_entry(struct ipt_entry *entry, struct connman_iptables *table)
1121 struct ipt_entry *new_entry;
1124 new_entry = g_try_malloc0(entry->next_offset);
1125 if (new_entry == NULL)
1128 memcpy(new_entry, entry, entry->next_offset);
1130 builtin = is_hook_entry(table, entry);
1132 return connman_add_entry(table, new_entry, NULL, builtin);
1135 static struct connman_iptables *connman_iptables_init(const char *table_name)
1137 struct connman_iptables *table = NULL;
1138 char *module = NULL;
1141 if (xtables_insmod("ip_tables", NULL, TRUE) != 0)
1144 module = g_strconcat("iptable_", table_name, NULL);
1148 if (xtables_insmod(module, NULL, TRUE) != 0)
1153 table = g_try_new0(struct connman_iptables, 1);
1157 table->info = g_try_new0(struct ipt_getinfo, 1);
1158 if (table->info == NULL)
1161 table->ipt_sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
1162 if (table->ipt_sock < 0)
1165 s = sizeof(*table->info);
1166 strcpy(table->info->name, table_name);
1167 if (getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_INFO,
1168 table->info, &s) < 0)
1171 table->blob_entries = g_try_malloc0(sizeof(struct ipt_get_entries) +
1173 if (table->blob_entries == NULL)
1176 strcpy(table->blob_entries->name, table_name);
1177 table->blob_entries->size = table->info->size;
1179 if (connman_iptables_get_entries(table) < 0)
1182 table->num_entries = 0;
1183 table->old_entries = table->info->num_entries;
1186 memcpy(table->underflow, table->info->underflow,
1187 sizeof(table->info->underflow));
1188 memcpy(table->hook_entry, table->info->hook_entry,
1189 sizeof(table->info->hook_entry));
1191 ENTRY_ITERATE(table->blob_entries->entrytable,
1192 table->blob_entries->size,
1200 connman_iptables_cleanup(table);
1205 static struct xtables_target *prepare_target(struct connman_iptables *table,
1208 struct xtables_target *xt_t = NULL;
1209 gboolean is_builtin, is_user_defined;
1210 GList *chain_head = NULL;
1214 is_user_defined = FALSE;
1216 if (is_builtin_target(target_name))
1219 chain_head = find_chain_head(table, target_name);
1220 if (chain_head != NULL && chain_head->next != NULL)
1221 is_user_defined = TRUE;
1224 if (is_builtin || is_user_defined)
1225 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
1226 XTF_LOAD_MUST_SUCCEED);
1228 xt_t = xtables_find_target(target_name, XTF_TRY_LOAD);
1233 target_size = ALIGN(sizeof(struct ipt_entry_target)) + xt_t->size;
1235 xt_t->t = g_try_malloc0(target_size);
1236 if (xt_t->t == NULL)
1239 xt_t->t->u.target_size = target_size;
1241 if (is_builtin || is_user_defined) {
1242 struct xt_standard_target *target;
1244 target = (struct xt_standard_target *)(xt_t->t);
1245 strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
1247 if (is_builtin == TRUE)
1248 target->verdict = target_to_verdict(target_name);
1249 else if (is_user_defined == TRUE) {
1250 struct connman_iptables_entry *target_rule;
1252 if (chain_head == NULL) {
1257 target_rule = chain_head->next->data;
1258 target->verdict = target_rule->offset;
1261 strcpy(xt_t->t->u.user.name, target_name);
1262 xt_t->t->u.user.revision = xt_t->revision;
1263 if (xt_t->init != NULL)
1264 xt_t->init(xt_t->t);
1270 static struct option connman_iptables_opts[] = {
1271 {.name = "append", .has_arg = 1, .val = 'A'},
1272 {.name = "delete", .has_arg = 1, .val = 'D'},
1273 {.name = "flush-chain", .has_arg = 1, .val = 'F'},
1274 {.name = "insert", .has_arg = 1, .val = 'I'},
1275 {.name = "list", .has_arg = 2, .val = 'L'},
1276 {.name = "new-chain", .has_arg = 1, .val = 'N'},
1277 {.name = "delete-chain", .has_arg = 1, .val = 'X'},
1278 {.name = "destination", .has_arg = 1, .val = 'd'},
1279 {.name = "in-interface", .has_arg = 1, .val = 'i'},
1280 {.name = "jump", .has_arg = 1, .val = 'j'},
1281 {.name = "match", .has_arg = 1, .val = 'm'},
1282 {.name = "out-interface", .has_arg = 1, .val = 'o'},
1283 {.name = "source", .has_arg = 1, .val = 's'},
1284 {.name = "table", .has_arg = 1, .val = 't'},
1288 struct xtables_globals connman_iptables_globals = {
1290 .opts = connman_iptables_opts,
1291 .orig_opts = connman_iptables_opts,
1294 int main(int argc, char *argv[])
1296 struct connman_iptables *table;
1297 struct xtables_match *xt_m;
1298 struct xtables_target *xt_t;
1300 char *table_name, *chain, *new_chain, *match_name, *target_name;
1301 char *delete_chain, *flush_chain;
1302 int c, in_len, out_len;
1304 gboolean dump, invert, delete, insert, delete_rule;
1305 struct in_addr src, dst;
1307 xtables_init_all(&connman_iptables_globals, NFPROTO_IPV4);
1313 delete_rule = FALSE;
1314 table_name = chain = new_chain = match_name = target_name = NULL;
1315 delete_chain = flush_chain = NULL;
1316 memset(&ip, 0, sizeof(struct ipt_ip));
1321 while ((c = getopt_long(argc, argv, "-A:D:F:I:L::N:X:d:i:j:m:o:s:t:",
1322 connman_iptables_globals.opts, NULL)) != -1) {
1325 /* It is either -A, -D or -I at once */
1333 /* It is either -A, -D or -I at once */
1342 flush_chain = optarg;
1346 /* 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)) + xt_m->size;
1402 xt_m->m = g_try_malloc0(size);
1405 xt_m->m->u.match_size = size;
1406 strcpy(xt_m->m->u.user.name, xt_m->name);
1407 xt_m->m->u.user.revision = xt_m->revision;
1408 if (xt_m->init != NULL)
1409 xt_m->init(xt_m->m);
1410 if (xt_m != xt_m->next) {
1411 connman_iptables_globals.opts =
1412 xtables_merge_options(
1413 #if XTABLES_VERSION_CODE > 5
1414 connman_iptables_globals.orig_opts,
1416 connman_iptables_globals.opts,
1418 &xt_m->option_offset);
1419 if (connman_iptables_globals.opts == NULL)
1426 out_len = strlen(optarg);
1428 if (out_len + 1 > IFNAMSIZ)
1431 strcpy(ip.outiface, optarg);
1432 memset(ip.outiface_mask, 0xff, out_len + 1);
1435 ip.invflags |= IPT_INV_VIA_OUT;
1440 if (!inet_pton(AF_INET, optarg, &src))
1444 inet_pton(AF_INET, "255.255.255.255", &ip.smsk);
1447 ip.invflags |= IPT_INV_SRCIP;
1452 table_name = optarg;
1456 if (optarg[0] == '!' && optarg[1] == '\0') {
1458 printf("Consecutive ! not allowed\n");
1465 printf("Invalid option\n");
1470 if (xt_t == NULL || xt_t->parse == NULL ||
1471 !xt_t->parse(c - xt_t->option_offset, argv, invert,
1472 &xt_t->tflags, NULL, &xt_t->t)) {
1473 if (xt_m == NULL || xt_m->parse == NULL)
1476 xt_m->parse(c - xt_m->option_offset, argv,
1477 invert, &xt_m->mflags, NULL, &xt_m->m);
1484 if (table_name == NULL)
1485 table_name = "filter";
1487 table = connman_iptables_init(table_name);
1492 if (delete_chain == NULL)
1495 printf("Delete chain %s\n", delete_chain);
1497 connman_iptables_delete_chain(table, delete_chain);
1503 printf("Flush chain %s\n", flush_chain);
1505 connman_iptables_flush_chain(table, flush_chain);
1511 connman_iptables_dump(table);
1516 if (chain && new_chain)
1520 printf("New chain %s\n", new_chain);
1522 connman_iptables_add_chain(table, new_chain);
1528 xt_t = prepare_target(table, target_name);
1532 connman_iptables_globals.opts =
1533 xtables_merge_options(
1534 #if XTABLES_VERSION_CODE > 5
1535 connman_iptables_globals.orig_opts,
1537 connman_iptables_globals.opts,
1539 &xt_t->option_offset);
1540 if (connman_iptables_globals.opts == NULL)
1543 if (delete_rule == TRUE) {
1544 printf("Deleting %s to %s (match %s)\n", target_name,
1547 connman_iptables_delete_rule(table, &ip, chain,
1548 target_name, xt_t, match_name, xt_m);
1553 if (insert == TRUE) {
1554 printf("Inserting %s to %s (match %s)\n", target_name,
1557 connman_iptables_insert_rule(table, &ip, chain,
1558 target_name, xt_t, match_name, xt_m);
1560 printf("Appending %s to %s (match %s)\n", target_name,
1563 connman_iptables_append_rule(table, &ip, chain,
1564 target_name, xt_t, match_name, xt_m);
1570 connman_iptables_commit(table);
1573 connman_iptables_cleanup(table);