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 #define XT_OPTION_OFFSET_SCALE 256
51 /* fn returns 0 to continue iteration */
52 #define _XT_ENTRY_ITERATE_CONTINUE(type, entries, size, n, fn, args...) \
59 for (__i = 0, __n = 0; __i < (size); \
60 __i += __entry->next_offset, __n++) { \
61 __entry = (void *)(entries) + __i; \
65 __ret = fn(__entry, ## args); \
72 /* fn returns 0 to continue iteration */
73 #define _XT_ENTRY_ITERATE(type, entries, size, fn, args...) \
74 _XT_ENTRY_ITERATE_CONTINUE(type, entries, size, 0, fn, args)
76 #define ENTRY_ITERATE(entries, size, fn, args...) \
77 _XT_ENTRY_ITERATE(struct ipt_entry, entries, size, fn, ## args)
79 #define MIN_ALIGN (__alignof__(struct ipt_entry))
81 #define ALIGN(s) (((s) + ((MIN_ALIGN)-1)) & ~((MIN_ALIGN)-1))
84 struct xt_entry_target t;
85 char error[IPT_TABLE_MAXNAMELEN];
88 struct connman_iptables_entry {
92 struct ipt_entry *entry;
95 struct connman_iptables {
98 struct ipt_getinfo *info;
99 struct ipt_get_entries *blob_entries;
101 unsigned int num_entries;
102 unsigned int old_entries;
105 unsigned int underflow[NF_INET_NUMHOOKS];
106 unsigned int hook_entry[NF_INET_NUMHOOKS];
112 static struct ipt_entry *get_entry(struct connman_iptables *table,
115 return (struct ipt_entry *)((char *)table->blob_entries->entrytable +
119 static int is_hook_entry(struct connman_iptables *table,
120 struct ipt_entry *entry)
124 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
125 if ((table->info->valid_hooks & (1 << i))
126 && get_entry(table, table->info->hook_entry[i]) == entry)
133 static unsigned long entry_to_offset(struct connman_iptables *table,
134 struct ipt_entry *entry)
136 return (void *)entry - (void *)table->blob_entries->entrytable;
139 static int target_to_verdict(char *target_name)
141 if (!strcmp(target_name, LABEL_ACCEPT))
142 return -NF_ACCEPT - 1;
144 if (!strcmp(target_name, LABEL_DROP))
147 if (!strcmp(target_name, LABEL_QUEUE))
148 return -NF_QUEUE - 1;
150 if (!strcmp(target_name, LABEL_RETURN))
156 static gboolean is_builtin_target(char *target_name)
158 if (!strcmp(target_name, LABEL_ACCEPT) ||
159 !strcmp(target_name, LABEL_DROP) ||
160 !strcmp(target_name, LABEL_QUEUE) ||
161 !strcmp(target_name, LABEL_RETURN))
167 static gboolean is_jump(struct connman_iptables_entry *e)
169 struct xt_entry_target *target;
171 target = ipt_get_target(e->entry);
173 if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
174 struct xt_standard_target *t;
176 t = (struct xt_standard_target *)target;
178 switch (t->verdict) {
194 static gboolean is_chain(struct connman_iptables *table,
195 struct connman_iptables_entry *e)
197 struct ipt_entry *entry;
198 struct xt_entry_target *target;
204 target = ipt_get_target(entry);
205 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET))
211 static GList *find_chain_head(struct connman_iptables *table,
215 struct connman_iptables_entry *head;
216 struct ipt_entry *entry;
217 struct xt_entry_target *target;
220 for (list = table->entries; list; list = list->next) {
225 builtin = head->builtin;
226 if (builtin >= 0 && !strcmp(hooknames[builtin], chain_name))
229 /* User defined chain */
230 target = ipt_get_target(entry);
231 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET) &&
232 !strcmp((char *)target->data, chain_name))
239 static GList *find_chain_tail(struct connman_iptables *table,
242 GList *chain_head, *list;
243 struct connman_iptables_entry *tail;
245 chain_head = find_chain_head(table, chain_name);
246 if (chain_head == NULL)
249 /* Then we look for the next chain */
250 for (list = chain_head->next; list; list = list->next) {
253 if (is_chain(table, tail))
257 /* Nothing found, we return the table end */
258 return g_list_last(table->entries);
261 static void update_offsets(struct connman_iptables *table)
264 struct connman_iptables_entry *entry, *prev_entry;
266 for (list = table->entries; list; list = list->next) {
269 if (list == table->entries) {
276 prev_entry = prev->data;
278 entry->offset = prev_entry->offset +
279 prev_entry->entry->next_offset;
283 static void update_targets_reference(struct connman_iptables *table,
284 struct connman_iptables_entry *entry_before,
285 struct connman_iptables_entry *modified_entry,
286 gboolean is_removing)
288 struct connman_iptables_entry *tmp;
289 struct xt_standard_target *t;
293 offset = modified_entry->entry->next_offset;
295 for (list = table->entries; list; list = list->next) {
301 t = (struct xt_standard_target *)ipt_get_target(tmp->entry);
303 if (is_removing == TRUE) {
304 if (t->verdict >= entry_before->offset)
305 t->verdict -= offset;
307 if (t->verdict > entry_before->offset)
308 t->verdict += offset;
313 static int connman_add_entry(struct connman_iptables *table,
314 struct ipt_entry *entry, GList *before,
317 struct connman_iptables_entry *e, *entry_before;
322 e = g_try_malloc0(sizeof(struct connman_iptables_entry));
327 e->builtin = builtin;
329 table->entries = g_list_insert_before(table->entries, before, e);
330 table->num_entries++;
331 table->size += entry->next_offset;
333 if (before == NULL) {
334 e->offset = table->size - entry->next_offset;
339 entry_before = before->data;
342 * We've just appended/insterted a new entry. All references
343 * should be bumped accordingly.
345 update_targets_reference(table, entry_before, e, FALSE);
347 update_offsets(table);
352 static int remove_table_entry(struct connman_iptables *table,
353 struct connman_iptables_entry *entry)
357 table->num_entries--;
358 table->size -= entry->entry->next_offset;
359 removed = entry->entry->next_offset;
361 g_free(entry->entry);
363 table->entries = g_list_remove(table->entries, entry);
368 static int connman_iptables_flush_chain(struct connman_iptables *table,
371 GList *chain_head, *chain_tail, *list, *next;
372 struct connman_iptables_entry *entry;
373 int builtin, removed = 0;
375 chain_head = find_chain_head(table, name);
376 if (chain_head == NULL)
379 chain_tail = find_chain_tail(table, name);
380 if (chain_tail == NULL)
383 entry = chain_head->data;
384 builtin = entry->builtin;
389 list = chain_head->next;
391 if (list == chain_tail->prev)
394 while (list != chain_tail->prev) {
396 next = g_list_next(list);
398 removed += remove_table_entry(table, entry);
404 struct connman_iptables_entry *e;
408 entry->builtin = builtin;
410 table->underflow[builtin] -= removed;
412 for (list = chain_tail; list; list = list->next) {
415 builtin = e->builtin;
419 table->hook_entry[builtin] -= removed;
420 table->underflow[builtin] -= removed;
424 update_offsets(table);
429 static int connman_iptables_delete_chain(struct connman_iptables *table,
432 GList *chain_head, *chain_tail;
433 struct connman_iptables_entry *entry;
435 chain_head = find_chain_head(table, name);
436 if (chain_head == NULL)
439 entry = chain_head->data;
441 /* We cannot remove builtin chain */
442 if (entry->builtin >= 0)
445 chain_tail = find_chain_tail(table, name);
446 if (chain_tail == NULL)
449 /* Chain must be flushed */
450 if (chain_head->next != chain_tail->prev)
453 remove_table_entry(table, entry);
455 entry = chain_tail->prev->data;
456 remove_table_entry(table, entry);
458 update_offsets(table);
463 static int connman_iptables_add_chain(struct connman_iptables *table,
467 struct ipt_entry *entry_head;
468 struct ipt_entry *entry_return;
469 struct error_target *error;
470 struct ipt_standard_target *standard;
471 u_int16_t entry_head_size, entry_return_size;
473 last = g_list_last(table->entries);
476 * An empty chain is composed of:
477 * - A head entry, with no match and an error target.
478 * The error target data is the chain name.
479 * - A tail entry, with no match and a standard target.
480 * The standard target verdict is XT_RETURN (return to the
485 entry_head_size = sizeof(struct ipt_entry) +
486 sizeof(struct error_target);
487 entry_head = g_try_malloc0(entry_head_size);
488 if (entry_head == NULL)
491 memset(entry_head, 0, entry_head_size);
493 entry_head->target_offset = sizeof(struct ipt_entry);
494 entry_head->next_offset = entry_head_size;
496 error = (struct error_target *) entry_head->elems;
497 strcpy(error->t.u.user.name, IPT_ERROR_TARGET);
498 error->t.u.user.target_size = ALIGN(sizeof(struct error_target));
499 strcpy(error->error, name);
501 if (connman_add_entry(table, entry_head, last, -1) < 0)
505 entry_return_size = sizeof(struct ipt_entry) +
506 sizeof(struct ipt_standard_target);
507 entry_return = g_try_malloc0(entry_return_size);
508 if (entry_return == NULL)
511 memset(entry_return, 0, entry_return_size);
513 entry_return->target_offset = sizeof(struct ipt_entry);
514 entry_return->next_offset = entry_return_size;
516 standard = (struct ipt_standard_target *) entry_return->elems;
517 standard->target.u.user.target_size =
518 ALIGN(sizeof(struct ipt_standard_target));
519 standard->verdict = XT_RETURN;
521 if (connman_add_entry(table, entry_return, last, -1) < 0)
527 g_free(entry_return);
534 static struct ipt_entry *new_rule(struct ipt_ip *ip,
535 char *target_name, struct xtables_target *xt_t,
536 char *match_name, struct xtables_match *xt_m)
538 struct ipt_entry *new_entry;
539 size_t match_size, target_size;
542 match_size = xt_m->m->u.match_size;
547 target_size = ALIGN(xt_t->t->u.target_size);
549 target_size = ALIGN(sizeof(struct xt_standard_target));
551 new_entry = g_try_malloc0(sizeof(struct ipt_entry) + target_size +
553 if (new_entry == NULL)
556 memcpy(&new_entry->ip, ip, sizeof(struct ipt_ip));
558 new_entry->target_offset = sizeof(struct ipt_entry) + match_size;
559 new_entry->next_offset = sizeof(struct ipt_entry) + target_size +
562 struct xt_entry_match *entry_match;
564 entry_match = (struct xt_entry_match *)new_entry->elems;
565 memcpy(entry_match, xt_m->m, match_size);
569 struct xt_entry_target *entry_target;
571 entry_target = ipt_get_target(new_entry);
572 memcpy(entry_target, xt_t->t, target_size);
578 static void update_hooks(struct connman_iptables *table, GList *chain_head,
579 struct ipt_entry *entry)
582 struct connman_iptables_entry *head, *e;
585 if (chain_head == NULL)
588 head = chain_head->data;
590 builtin = head->builtin;
594 table->underflow[builtin] += entry->next_offset;
596 for (list = chain_head->next; list; list = list->next) {
599 builtin = e->builtin;
603 table->hook_entry[builtin] += entry->next_offset;
604 table->underflow[builtin] += entry->next_offset;
608 static struct ipt_entry *prepare_rule_inclusion(struct connman_iptables *table,
609 struct ipt_ip *ip, char *chain_name,
610 char *target_name, struct xtables_target *xt_t,
611 char *match_name, struct xtables_match *xt_m,
614 GList *chain_tail, *chain_head;
615 struct ipt_entry *new_entry;
616 struct connman_iptables_entry *head;
618 chain_head = find_chain_head(table, chain_name);
619 if (chain_head == NULL)
622 chain_tail = find_chain_tail(table, chain_name);
623 if (chain_tail == NULL)
626 new_entry = new_rule(ip, target_name, xt_t, match_name, xt_m);
627 if (new_entry == NULL)
630 update_hooks(table, chain_head, new_entry);
633 * If the chain is builtin, and does not have any rule,
634 * then the one that we're inserting is becoming the head
635 * and thus needs the builtin flag.
637 head = chain_head->data;
638 if (head->builtin < 0)
640 else if (chain_head == chain_tail->prev) {
641 *builtin = head->builtin;
648 static int 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 *connman_iptables_blob(struct connman_iptables *table)
871 struct ipt_replace *r;
873 struct connman_iptables_entry *e;
874 unsigned char *entry_index;
876 r = g_try_malloc0(sizeof(struct ipt_replace) + table->size);
880 memset(r, 0, sizeof(*r) + table->size);
882 r->counters = g_try_malloc0(sizeof(struct xt_counters)
883 * table->old_entries);
884 if (r->counters == NULL) {
889 strcpy(r->name, table->info->name);
890 r->num_entries = table->num_entries;
891 r->size = table->size;
893 r->num_counters = table->old_entries;
894 r->valid_hooks = table->info->valid_hooks;
896 memcpy(r->hook_entry, table->hook_entry, sizeof(table->hook_entry));
897 memcpy(r->underflow, table->underflow, sizeof(table->underflow));
899 entry_index = (unsigned char *)r->entries;
900 for (list = table->entries; list; list = list->next) {
903 memcpy(entry_index, e->entry, e->entry->next_offset);
904 entry_index += e->entry->next_offset;
910 static void dump_target(struct connman_iptables *table,
911 struct ipt_entry *entry)
914 struct xtables_target *xt_t;
915 struct xt_entry_target *target;
917 target = ipt_get_target(entry);
919 if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
920 struct xt_standard_target *t;
922 t = (struct xt_standard_target *)target;
924 switch (t->verdict) {
926 printf("\ttarget RETURN\n");
930 printf("\ttarget ACCEPT\n");
934 printf("\ttarget DROP\n");
938 printf("\ttarget QUEUE\n");
942 printf("\ttarget STOP\n");
946 printf("\tJUMP @%p (0x%x)\n",
947 (char*)table->blob_entries->entrytable +
948 t->verdict, t->verdict);
952 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
953 XTF_LOAD_MUST_SUCCEED);
955 if(xt_t->print != NULL)
956 xt_t->print(NULL, target, 1);
958 xt_t = xtables_find_target(target->u.user.name, XTF_TRY_LOAD);
960 printf("\ttarget %s\n", target->u.user.name);
964 if(xt_t->print != NULL) {
966 xt_t->print(NULL, target, 1);
972 static void dump_match(struct connman_iptables *table, struct ipt_entry *entry)
974 struct xtables_match *xt_m;
975 struct xt_entry_match *match;
977 if (entry->elems == (unsigned char *)entry + entry->target_offset)
980 match = (struct xt_entry_match *) entry->elems;
982 if (!strlen(match->u.user.name))
985 xt_m = xtables_find_match(match->u.user.name, XTF_TRY_LOAD, NULL);
989 if(xt_m->print != NULL) {
991 xt_m->print(NULL, match, 1);
998 printf("\tmatch %s\n", match->u.user.name);
1002 static int connman_iptables_dump_entry(struct ipt_entry *entry,
1003 struct connman_iptables *table)
1005 struct xt_entry_target *target;
1006 unsigned int offset;
1009 offset = (char *)entry - (char *)table->blob_entries->entrytable;
1010 target = ipt_get_target(entry);
1011 builtin = is_hook_entry(table, entry);
1013 if (entry_to_offset(table, entry) + entry->next_offset ==
1014 table->blob_entries->size) {
1015 printf("End of CHAIN 0x%x\n", offset);
1019 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET)) {
1020 printf("USER CHAIN (%s) %p match %p target %p size %d\n",
1021 target->data, entry, entry->elems,
1022 (char *)entry + entry->target_offset,
1023 entry->next_offset);
1026 } else if (builtin >= 0) {
1027 printf("CHAIN (%s) %p match %p target %p size %d\n",
1028 hooknames[builtin], entry, entry->elems,
1029 (char *)entry + entry->target_offset,
1030 entry->next_offset);
1032 printf("RULE %p match %p target %p size %d\n", entry,
1034 (char *)entry + entry->target_offset,
1035 entry->next_offset);
1038 dump_match(table, entry);
1039 dump_target(table, entry);
1044 static void connman_iptables_dump_hook(struct connman_iptables *table)
1047 printf("hooks: \n");
1048 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
1049 if ((table->info->valid_hooks & (1 << i)))
1050 printf("%s entry %p underflow %p (%#x)\n",
1052 table->blob_entries->entrytable +
1053 table->info->hook_entry[i],
1054 table->blob_entries->entrytable +
1055 table->info->underflow[i],
1056 table->info->underflow[i]);
1060 static void connman_iptables_dump(struct connman_iptables *table)
1062 printf("%s valid_hooks=0x%08x, num_entries=%u, size=%u\n",
1064 table->info->valid_hooks, table->info->num_entries,
1067 connman_iptables_dump_hook(table);
1069 ENTRY_ITERATE(table->blob_entries->entrytable,
1070 table->blob_entries->size,
1071 connman_iptables_dump_entry, table);
1075 static int connman_iptables_get_entries(struct connman_iptables *table)
1077 socklen_t entry_size;
1079 entry_size = sizeof(struct ipt_get_entries) + table->info->size;
1081 return getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_ENTRIES,
1082 table->blob_entries, &entry_size);
1085 static int connman_iptables_replace(struct connman_iptables *table,
1086 struct ipt_replace *r)
1088 return setsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_SET_REPLACE, r,
1089 sizeof(*r) + r->size);
1092 static void connman_iptables_cleanup(struct connman_iptables *table)
1095 struct connman_iptables_entry *entry;
1097 close(table->ipt_sock);
1099 for (list = table->entries; list; list = list->next) {
1102 g_free(entry->entry);
1105 g_free(table->info);
1106 g_free(table->blob_entries);
1109 xtables_free_opts(1);
1112 static int connman_iptables_commit(struct connman_iptables *table)
1114 struct ipt_replace *repl;
1116 repl = connman_iptables_blob(table);
1118 return connman_iptables_replace(table, repl);
1121 static int add_entry(struct ipt_entry *entry, struct connman_iptables *table)
1123 struct ipt_entry *new_entry;
1126 new_entry = g_try_malloc0(entry->next_offset);
1127 if (new_entry == NULL)
1130 memcpy(new_entry, entry, entry->next_offset);
1132 builtin = is_hook_entry(table, entry);
1134 return connman_add_entry(table, new_entry, NULL, builtin);
1137 static struct connman_iptables *connman_iptables_init(const char *table_name)
1139 struct connman_iptables *table = NULL;
1140 char *module = NULL;
1143 if (xtables_insmod("ip_tables", NULL, TRUE) != 0)
1146 module = g_strconcat("iptable_", table_name, NULL);
1150 if (xtables_insmod(module, NULL, TRUE) != 0)
1156 table = g_try_new0(struct connman_iptables, 1);
1160 table->info = g_try_new0(struct ipt_getinfo, 1);
1161 if (table->info == NULL)
1164 table->ipt_sock = socket(AF_INET, SOCK_RAW | SOCK_CLOEXEC, IPPROTO_RAW);
1165 if (table->ipt_sock < 0)
1168 s = sizeof(*table->info);
1169 strcpy(table->info->name, table_name);
1170 if (getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_INFO,
1171 table->info, &s) < 0)
1174 table->blob_entries = g_try_malloc0(sizeof(struct ipt_get_entries) +
1176 if (table->blob_entries == NULL)
1179 strcpy(table->blob_entries->name, table_name);
1180 table->blob_entries->size = table->info->size;
1182 if (connman_iptables_get_entries(table) < 0)
1185 table->num_entries = 0;
1186 table->old_entries = table->info->num_entries;
1189 memcpy(table->underflow, table->info->underflow,
1190 sizeof(table->info->underflow));
1191 memcpy(table->hook_entry, table->info->hook_entry,
1192 sizeof(table->info->hook_entry));
1194 ENTRY_ITERATE(table->blob_entries->entrytable,
1195 table->blob_entries->size,
1203 connman_iptables_cleanup(table);
1208 static struct option connman_iptables_opts[] = {
1209 {.name = "append", .has_arg = 1, .val = 'A'},
1210 {.name = "delete", .has_arg = 1, .val = 'D'},
1211 {.name = "flush-chain", .has_arg = 1, .val = 'F'},
1212 {.name = "insert", .has_arg = 1, .val = 'I'},
1213 {.name = "list", .has_arg = 2, .val = 'L'},
1214 {.name = "new-chain", .has_arg = 1, .val = 'N'},
1215 {.name = "delete-chain", .has_arg = 1, .val = 'X'},
1216 {.name = "destination", .has_arg = 1, .val = 'd'},
1217 {.name = "in-interface", .has_arg = 1, .val = 'i'},
1218 {.name = "jump", .has_arg = 1, .val = 'j'},
1219 {.name = "match", .has_arg = 1, .val = 'm'},
1220 {.name = "out-interface", .has_arg = 1, .val = 'o'},
1221 {.name = "source", .has_arg = 1, .val = 's'},
1222 {.name = "table", .has_arg = 1, .val = 't'},
1226 struct xtables_globals connman_iptables_globals = {
1228 .opts = connman_iptables_opts,
1229 .orig_opts = connman_iptables_opts,
1232 static struct xtables_target *prepare_target(struct connman_iptables *table,
1235 struct xtables_target *xt_t = NULL;
1236 gboolean is_builtin, is_user_defined;
1237 GList *chain_head = NULL;
1241 is_user_defined = FALSE;
1243 if (is_builtin_target(target_name))
1246 chain_head = find_chain_head(table, target_name);
1247 if (chain_head != NULL && chain_head->next != NULL)
1248 is_user_defined = TRUE;
1251 if (is_builtin || is_user_defined)
1252 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
1253 XTF_LOAD_MUST_SUCCEED);
1255 xt_t = xtables_find_target(target_name, XTF_TRY_LOAD);
1260 target_size = ALIGN(sizeof(struct ipt_entry_target)) + xt_t->size;
1262 xt_t->t = g_try_malloc0(target_size);
1263 if (xt_t->t == NULL)
1266 xt_t->t->u.target_size = target_size;
1268 if (is_builtin || is_user_defined) {
1269 struct xt_standard_target *target;
1271 target = (struct xt_standard_target *)(xt_t->t);
1272 strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
1274 if (is_builtin == TRUE)
1275 target->verdict = target_to_verdict(target_name);
1276 else if (is_user_defined == TRUE) {
1277 struct connman_iptables_entry *target_rule;
1279 if (chain_head == NULL) {
1284 target_rule = chain_head->next->data;
1285 target->verdict = target_rule->offset;
1288 strcpy(xt_t->t->u.user.name, target_name);
1289 xt_t->t->u.user.revision = xt_t->revision;
1290 if (xt_t->init != NULL)
1291 xt_t->init(xt_t->t);
1294 if (xt_t->x6_options != NULL)
1295 connman_iptables_globals.opts =
1296 xtables_options_xfrm(
1297 #if XTABLES_VERSION_CODE > 5
1298 connman_iptables_globals.orig_opts,
1300 connman_iptables_globals.opts,
1302 &xt_t->option_offset);
1304 connman_iptables_globals.opts =
1305 xtables_merge_options(
1306 #if XTABLES_VERSION_CODE > 5
1307 connman_iptables_globals.orig_opts,
1309 connman_iptables_globals.opts,
1311 &xt_t->option_offset);
1313 if (connman_iptables_globals.opts == NULL) {
1321 static struct xtables_match *prepare_matches(struct connman_iptables *table,
1322 struct xtables_rule_match **xt_rm, char *match_name)
1324 struct xtables_match *xt_m;
1327 if (match_name == NULL)
1330 xt_m = xtables_find_match(match_name, XTF_LOAD_MUST_SUCCEED, xt_rm);
1331 match_size = ALIGN(sizeof(struct ipt_entry_match)) + xt_m->size;
1333 xt_m->m = g_try_malloc0(match_size);
1334 if (xt_m->m == NULL)
1337 xt_m->m->u.match_size = match_size;
1338 strcpy(xt_m->m->u.user.name, xt_m->name);
1339 xt_m->m->u.user.revision = xt_m->revision;
1341 if (xt_m->init != NULL)
1342 xt_m->init(xt_m->m);
1344 if (xt_m == xt_m->next)
1347 if (xt_m->x6_options != NULL)
1348 connman_iptables_globals.opts =
1349 xtables_options_xfrm(
1350 #if XTABLES_VERSION_CODE > 5
1351 connman_iptables_globals.orig_opts,
1353 connman_iptables_globals.opts,
1355 &xt_m->option_offset);
1357 connman_iptables_globals.opts =
1358 xtables_merge_options(
1359 #if XTABLES_VERSION_CODE > 5
1360 connman_iptables_globals.orig_opts,
1362 connman_iptables_globals.opts,
1364 &xt_m->option_offset);
1366 if (connman_iptables_globals.opts == NULL) {
1375 int main(int argc, char *argv[])
1377 struct connman_iptables *table;
1378 struct xtables_rule_match *xt_rm, *tmp_xt_rm;
1379 struct xtables_match *xt_m, *xt_m_t;
1380 struct xtables_target *xt_t;
1382 char *table_name, *chain, *new_chain, *match_name, *target_name;
1383 char *delete_chain, *flush_chain;
1384 int c, in_len, out_len;
1385 gboolean dump, invert, delete, insert, delete_rule;
1386 struct in_addr src, dst;
1388 xtables_init_all(&connman_iptables_globals, NFPROTO_IPV4);
1394 delete_rule = FALSE;
1395 table_name = chain = new_chain = match_name = target_name = NULL;
1396 delete_chain = flush_chain = NULL;
1397 memset(&ip, 0, sizeof(struct ipt_ip));
1403 while ((c = getopt_long(argc, argv, "-A:D:F:I:L::N:X:d:i:j:m:o:s:t:",
1404 connman_iptables_globals.opts, NULL)) != -1) {
1407 /* It is either -A, -D or -I at once */
1415 /* It is either -A, -D or -I at once */
1424 flush_chain = optarg;
1428 /* It is either -A, -D or -I at once */
1446 delete_chain = optarg;
1450 if (!inet_pton(AF_INET, optarg, &dst))
1454 inet_pton(AF_INET, "255.255.255.255", &ip.dmsk);
1457 ip.invflags |= IPT_INV_DSTIP;
1462 in_len = strlen(optarg);
1464 if (in_len + 1 > IFNAMSIZ)
1467 strcpy(ip.iniface, optarg);
1468 memset(ip.iniface_mask, 0xff, in_len + 1);
1471 ip.invflags |= IPT_INV_VIA_IN;
1476 target_name = optarg;
1477 xt_t = prepare_target(table, target_name);
1484 match_name = optarg;
1485 xt_m = prepare_matches(table, &xt_rm, match_name);
1492 out_len = strlen(optarg);
1494 if (out_len + 1 > IFNAMSIZ)
1497 strcpy(ip.outiface, optarg);
1498 memset(ip.outiface_mask, 0xff, out_len + 1);
1501 ip.invflags |= IPT_INV_VIA_OUT;
1506 if (!inet_pton(AF_INET, optarg, &src))
1510 inet_pton(AF_INET, "255.255.255.255", &ip.smsk);
1513 ip.invflags |= IPT_INV_SRCIP;
1518 table_name = optarg;
1522 if (optarg[0] == '!' && optarg[1] == '\0') {
1524 printf("Consecutive ! not allowed\n");
1531 printf("Invalid option\n");
1536 if (xt_t != NULL && (xt_t->x6_parse != NULL ||
1537 xt_t->parse != NULL) &&
1538 (c >= (int) xt_t->option_offset &&
1539 c < (int) xt_t->option_offset +
1540 XT_OPTION_OFFSET_SCALE)) {
1541 xtables_option_tpcall(c, argv,
1542 invert, xt_t, NULL);
1547 for (tmp_xt_rm = xt_rm; tmp_xt_rm != NULL;
1548 tmp_xt_rm = tmp_xt_rm->next) {
1549 xt_m_t = tmp_xt_rm->match;
1551 if (tmp_xt_rm->completed ||
1552 (xt_m_t->x6_parse == NULL &&
1553 xt_m_t->parse == NULL))
1556 if (c < (int) xt_m_t->option_offset ||
1557 c >= (int) xt_m_t->option_offset
1558 + XT_OPTION_OFFSET_SCALE)
1561 xtables_option_mpcall(c, argv,
1562 invert, xt_m_t, NULL);
1571 for (tmp_xt_rm = xt_rm; tmp_xt_rm != NULL;
1572 tmp_xt_rm = tmp_xt_rm->next)
1573 xtables_option_mfcall(tmp_xt_rm->match);
1576 xtables_option_tfcall(xt_t);
1578 if (table_name == NULL)
1579 table_name = "filter";
1581 table = connman_iptables_init(table_name);
1586 if (delete_chain == NULL)
1589 printf("Delete chain %s\n", delete_chain);
1591 connman_iptables_delete_chain(table, delete_chain);
1597 printf("Flush chain %s\n", flush_chain);
1599 connman_iptables_flush_chain(table, flush_chain);
1605 connman_iptables_dump(table);
1610 if (chain && new_chain)
1614 printf("New chain %s\n", new_chain);
1616 connman_iptables_add_chain(table, new_chain);
1622 if (delete_rule == TRUE) {
1623 printf("Deleting %s to %s (match %s)\n", target_name,
1626 connman_iptables_delete_rule(table, &ip, chain,
1627 target_name, xt_t, match_name, xt_m);
1632 if (insert == TRUE) {
1633 printf("Inserting %s to %s (match %s)\n", target_name,
1636 connman_iptables_insert_rule(table, &ip, chain,
1637 target_name, xt_t, match_name, xt_m);
1639 printf("Appending %s to %s (match %s)\n", target_name,
1642 connman_iptables_append_rule(table, &ip, chain,
1643 target_name, xt_t, match_name, xt_m);
1649 connman_iptables_commit(table);
1652 connman_iptables_cleanup(table);