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>
30 #include <linux/netfilter_ipv4/ip_tables.h>
34 static const char *hooknames[] = {
35 [NF_IP_PRE_ROUTING] = "PREROUTING",
36 [NF_IP_LOCAL_IN] = "INPUT",
37 [NF_IP_FORWARD] = "FORWARD",
38 [NF_IP_LOCAL_OUT] = "OUTPUT",
39 [NF_IP_POST_ROUTING] = "POSTROUTING",
42 #define LABEL_ACCEPT "ACCEPT"
43 #define LABEL_DROP "DROP"
44 #define LABEL_QUEUE "QUEUE"
45 #define LABEL_RETURN "RETURN"
47 /* fn returns 0 to continue iteration */
48 #define _XT_ENTRY_ITERATE_CONTINUE(type, entries, size, n, fn, args...) \
55 for (__i = 0, __n = 0; __i < (size); \
56 __i += __entry->next_offset, __n++) { \
57 __entry = (void *)(entries) + __i; \
61 __ret = fn(__entry, ## args); \
68 /* fn returns 0 to continue iteration */
69 #define _XT_ENTRY_ITERATE(type, entries, size, fn, args...) \
70 _XT_ENTRY_ITERATE_CONTINUE(type, entries, size, 0, fn, args)
72 #define ENTRY_ITERATE(entries, size, fn, args...) \
73 _XT_ENTRY_ITERATE(struct ipt_entry, entries, size, fn, ## args)
75 #define MIN_ALIGN (__alignof__(struct ipt_entry))
77 #define ALIGN(s) (((s) + ((MIN_ALIGN)-1)) & ~((MIN_ALIGN)-1))
79 struct ipt_error_target {
80 struct xt_entry_target t;
81 char error[IPT_TABLE_MAXNAMELEN];
84 struct connman_iptables_entry {
88 struct ipt_entry *entry;
91 struct connman_iptables {
94 struct ipt_getinfo *info;
95 struct ipt_get_entries *blob_entries;
97 unsigned int num_entries;
98 unsigned int old_entries;
101 unsigned int underflow[NF_INET_NUMHOOKS];
102 unsigned int hook_entry[NF_INET_NUMHOOKS];
108 static struct ipt_entry *get_entry(struct connman_iptables *table,
111 return (struct ipt_entry *)((char *)table->blob_entries->entrytable +
115 static int is_hook_entry(struct connman_iptables *table,
116 struct ipt_entry *entry)
120 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
121 if ((table->info->valid_hooks & (1 << i))
122 && get_entry(table, table->info->hook_entry[i]) == entry)
129 static unsigned long entry_to_offset(struct connman_iptables *table,
130 struct ipt_entry *entry)
132 return (void *)entry - (void *)table->blob_entries->entrytable;
135 static int target_to_verdict(char *target_name)
137 if (!strcmp(target_name, LABEL_ACCEPT))
138 return -NF_ACCEPT - 1;
140 if (!strcmp(target_name, LABEL_DROP))
143 if (!strcmp(target_name, LABEL_QUEUE))
144 return -NF_QUEUE - 1;
146 if (!strcmp(target_name, LABEL_RETURN))
152 static gboolean is_builtin_target(char *target_name)
154 if (!strcmp(target_name, LABEL_ACCEPT) ||
155 !strcmp(target_name, LABEL_DROP) ||
156 !strcmp(target_name, LABEL_QUEUE) ||
157 !strcmp(target_name, LABEL_RETURN))
163 static gboolean is_jump(struct connman_iptables_entry *e)
165 struct xt_entry_target *target;
167 target = ipt_get_target(e->entry);
169 if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
170 struct xt_standard_target *t;
172 t = (struct xt_standard_target *)target;
174 switch (t->verdict) {
190 static gboolean is_chain(struct connman_iptables *table,
191 struct connman_iptables_entry *e)
193 struct ipt_entry *entry;
194 struct xt_entry_target *target;
200 target = ipt_get_target(entry);
201 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET))
207 static GList *find_chain_head(struct connman_iptables *table,
211 struct connman_iptables_entry *head;
212 struct ipt_entry *entry;
213 struct xt_entry_target *target;
216 for (list = table->entries; list; list = list->next) {
221 builtin = head->builtin;
222 if (builtin >= 0 && !strcmp(hooknames[builtin], chain_name))
225 /* User defined chain */
226 target = ipt_get_target(entry);
227 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET) &&
228 !strcmp((char *)target->data, chain_name))
235 static GList *find_chain_tail(struct connman_iptables *table,
238 GList *chain_head, *list;
239 struct connman_iptables_entry *head, *tail;
240 struct ipt_entry *entry;
241 struct xt_entry_target *target;
244 /* First we look for the head */
245 for (list = table->entries; list; list = list->next) {
250 builtin = head->builtin;
251 if (builtin >= 0 && !strcmp(hooknames[builtin], chain_name))
254 /* User defined chain */
255 target = ipt_get_target(entry);
256 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET) &&
257 !strcmp((char *)target->data, chain_name))
266 /* Then we look for the next chain */
267 for (list = chain_head->next; list; list = list->next) {
271 if (is_chain(table, tail))
275 /* Nothing found, we return the table end */
276 return g_list_last(table->entries);
279 static void update_offsets(struct connman_iptables *table)
282 struct connman_iptables_entry *entry, *prev_entry;
284 for (list = table->entries; list; list = list->next) {
287 if (list == table->entries) {
294 prev_entry = prev->data;
296 entry->offset = prev_entry->offset +
297 prev_entry->entry->next_offset;
301 static int connman_add_entry(struct connman_iptables *table,
302 struct ipt_entry *entry, GList *before,
306 struct connman_iptables_entry *e, *tmp, *entry_before;
307 struct xt_standard_target *t;
312 e = g_try_malloc0(sizeof(struct connman_iptables_entry));
317 e->builtin = builtin;
319 table->entries = g_list_insert_before(table->entries, before, e);
320 table->num_entries++;
321 table->size += entry->next_offset;
323 if (before == NULL) {
324 e->offset = table->size - entry->next_offset;
329 entry_before = before->data;
332 * We've just insterted a new entry. All references before it
333 * should be bumped accordingly.
335 for (list = table->entries; list != before; list = list->next) {
341 t = (struct xt_standard_target *)ipt_get_target(tmp->entry);
343 if (t->verdict >= entry_before->offset)
344 t->verdict += entry->next_offset;
347 update_offsets(table);
352 static int connman_iptables_delete_chain(struct connman_iptables *table,
355 GList *chain_head, *chain_tail, *list, *next;
356 struct connman_iptables_entry *entry;
358 chain_head = find_chain_head(table, name);
359 if (chain_head == NULL)
362 chain_tail = find_chain_tail(table, name);
363 if (chain_tail == NULL)
368 while (list != chain_tail) {
370 next = g_list_next(list);
372 table->num_entries--;
373 table->size -= entry->entry->next_offset;
375 table->entries = g_list_remove(table->entries, list->data);
380 update_offsets(table);
385 static int connman_iptables_add_chain(struct connman_iptables *table,
389 struct ipt_entry *entry_head;
390 struct ipt_entry *entry_return;
391 struct ipt_error_target *error;
392 struct ipt_standard_target *standard;
393 u_int16_t entry_head_size, entry_return_size;
395 last = g_list_last(table->entries);
398 * An empty chain is composed of:
399 * - A head entry, with no match and an error target.
400 * The error target data is the chain name.
401 * - A tail entry, with no match and a standard target.
402 * The standard target verdict is XT_RETURN (return to the
407 entry_head_size = sizeof(struct ipt_entry) +
408 sizeof(struct ipt_error_target);
409 entry_head = g_try_malloc0(entry_head_size);
410 if (entry_head == NULL)
413 memset(entry_head, 0, entry_head_size);
415 entry_head->target_offset = sizeof(struct ipt_entry);
416 entry_head->next_offset = entry_head_size;
418 error = (struct ipt_error_target *) entry_head->elems;
419 strcpy(error->t.u.user.name, IPT_ERROR_TARGET);
420 error->t.u.user.target_size = ALIGN(sizeof(struct ipt_error_target));
421 strcpy(error->error, name);
423 if (connman_add_entry(table, entry_head, last, -1) < 0)
427 entry_return_size = sizeof(struct ipt_entry) +
428 sizeof(struct ipt_standard_target);
429 entry_return = g_try_malloc0(entry_return_size);
430 if (entry_return == NULL)
433 memset(entry_return, 0, entry_return_size);
435 entry_return->target_offset = sizeof(struct ipt_entry);
436 entry_return->next_offset = entry_return_size;
438 standard = (struct ipt_standard_target *) entry_return->elems;
439 standard->target.u.user.target_size =
440 ALIGN(sizeof(struct ipt_standard_target));
441 standard->verdict = XT_RETURN;
443 if (connman_add_entry(table, entry_return, last, -1) < 0)
450 g_free(entry_return);
455 static struct ipt_entry *
456 new_rule(struct connman_iptables *table,
457 char *target_name, struct xtables_target *xt_t,
458 char *match_name, struct xtables_match *xt_m)
460 struct ipt_entry *new_entry;
461 size_t match_size, target_size;
462 int is_builtin = is_builtin_target(target_name);
465 match_size = xt_m->m->u.match_size;
470 target_size = ALIGN(xt_t->t->u.target_size);
472 target_size = ALIGN(sizeof(struct xt_standard_target));
474 new_entry = g_try_malloc0(sizeof(struct ipt_entry) + target_size +
476 if (new_entry == NULL)
479 new_entry->target_offset = sizeof(struct ipt_entry) + match_size;
480 new_entry->next_offset = sizeof(struct ipt_entry) + target_size +
483 struct xt_entry_match *entry_match;
485 entry_match = (struct xt_entry_match *)new_entry->elems;
486 memcpy(entry_match, xt_m->m, match_size);
490 struct xt_entry_target *entry_target;
493 struct xt_standard_target *target;
495 target = (struct xt_standard_target *)(xt_t->t);
496 strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
497 target->verdict = target_to_verdict(target_name);
500 entry_target = ipt_get_target(new_entry);
501 memcpy(entry_target, xt_t->t, target_size);
503 struct connman_iptables_entry *target_rule;
504 struct xt_standard_target *target;
508 * This is a user defined target, i.e. a chain jump.
509 * We search for the chain head, and the target verdict
510 * is the first rule's offset on this chain.
511 * The offset is from the beginning of the table.
514 chain_head = find_chain_head(table, target_name);
515 if (chain_head == NULL || chain_head->next == NULL) {
520 target_rule = chain_head->next->data;
522 target = (struct xt_standard_target *)ipt_get_target(new_entry);
523 strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
524 target->target.u.user.target_size = target_size;
525 target->verdict = target_rule->offset;
531 static void update_hooks(struct connman_iptables *table, GList *chain_head, struct ipt_entry *entry)
534 struct connman_iptables_entry *head, *e;
537 if (chain_head == NULL)
540 head = chain_head->data;
542 builtin = head->builtin;
546 table->underflow[builtin] += entry->next_offset;
548 for (list = chain_head->next; list; list = list->next) {
551 builtin = e->builtin;
555 table->hook_entry[builtin] += entry->next_offset;
560 connman_iptables_add_rule(struct connman_iptables *table, char *chain_name,
561 char *target_name, struct xtables_target *xt_t,
562 char *match_name, struct xtables_match *xt_m)
564 GList *chain_tail, *chain_head;
565 struct ipt_entry *new_entry;
567 chain_head = find_chain_head(table, chain_name);
568 if (chain_head == NULL)
571 chain_tail = find_chain_tail(table, chain_name);
572 if (chain_tail == NULL)
575 new_entry = new_rule(table,
578 if (new_entry == NULL)
581 update_hooks(table, chain_head, new_entry);
583 return connman_add_entry(table, new_entry, chain_tail, -1);
586 static struct ipt_replace *
587 connman_iptables_blob(struct connman_iptables *table)
589 struct ipt_replace *r;
591 struct connman_iptables_entry *e;
592 unsigned char *entry_index;
594 r = g_try_malloc0(sizeof(struct ipt_replace) + table->size);
598 memset(r, 0, sizeof(*r) + table->size);
600 r->counters = g_try_malloc0(sizeof(struct xt_counters)
601 * table->num_entries);
602 if (r->counters == NULL) {
607 strcpy(r->name, table->info->name);
608 r->num_entries = table->num_entries;
609 r->size = table->size;
611 r->num_counters = table->old_entries;
612 r->valid_hooks = table->info->valid_hooks;
614 memcpy(r->hook_entry, table->hook_entry, sizeof(table->hook_entry));
615 memcpy(r->underflow, table->underflow, sizeof(table->underflow));
617 entry_index = (unsigned char *)r->entries;
618 for (list = table->entries; list; list = list->next) {
621 memcpy(entry_index, e->entry, e->entry->next_offset);
622 entry_index += e->entry->next_offset;
628 static void dump_target(struct connman_iptables *table,
629 struct ipt_entry *entry)
632 struct xtables_target *xt_t;
633 struct xt_entry_target *target;
635 target = ipt_get_target(entry);
637 if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
638 struct xt_standard_target *t;
640 t = (struct xt_standard_target *)target;
642 switch (t->verdict) {
644 printf("\ttarget RETURN\n");
648 printf("\ttarget ACCEPT\n");
652 printf("\ttarget DROP\n");
656 printf("\ttarget QUEUE\n");
660 printf("\ttarget STOP\n");
664 printf("\tJUMP @%p (0x%x)\n",
665 (char*)table->blob_entries->entrytable +
666 t->verdict, t->verdict);
670 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
671 XTF_LOAD_MUST_SUCCEED);
673 if(xt_t->print != NULL)
674 xt_t->print(NULL, target, 1);
676 xt_t = xtables_find_target(target->u.user.name, XTF_TRY_LOAD);
678 printf("\ttarget %s\n", target->u.user.name);
682 if(xt_t->print != NULL) {
684 xt_t->print(NULL, target, 1);
690 static void dump_match(struct connman_iptables *table, struct ipt_entry *entry)
692 struct xtables_match *xt_m;
693 struct xt_entry_match *match;
695 if (entry->elems == (unsigned char *)entry + entry->target_offset)
698 match = (struct xt_entry_match *) entry->elems;
700 if (!strlen(match->u.user.name))
703 xt_m = xtables_find_match(match->u.user.name, XTF_TRY_LOAD, NULL);
707 if(xt_m->print != NULL) {
709 xt_m->print(NULL, match, 1);
716 printf("\tmatch %s\n", match->u.user.name);
720 static int connman_iptables_dump_entry(struct ipt_entry *entry,
721 struct connman_iptables *table)
723 struct xt_entry_target *target;
727 offset = (char *)entry - (char *)table->blob_entries->entrytable;
728 target = ipt_get_target(entry);
729 builtin = is_hook_entry(table, entry);
731 if (entry_to_offset(table, entry) + entry->next_offset ==
732 table->blob_entries->size) {
733 printf("End of CHAIN 0x%x\n", offset);
737 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET)) {
738 printf("USER CHAIN (%s) %p match %p target %p size %d\n",
739 target->data, entry, entry->elems,
740 (char *)entry + entry->target_offset,
744 } else if (builtin >= 0) {
745 printf("CHAIN (%s) %p match %p target %p size %d\n",
746 hooknames[builtin], entry, entry->elems,
747 (char *)entry + entry->target_offset,
750 printf("RULE %p match %p target %p size %d\n", entry,
752 (char *)entry + entry->target_offset,
756 dump_match(table, entry);
757 dump_target(table, entry);
762 static void connman_iptables_dump_hook(struct connman_iptables *table)
766 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
767 if ((table->info->valid_hooks & (1 << i)))
768 printf("%s entry 0x%x underflow 0x%x (0x%x)\n",
770 (unsigned int)table->blob_entries->entrytable +
771 table->info->hook_entry[i],
772 (unsigned int)table->blob_entries->entrytable +
773 table->info->underflow[i],
774 table->info->underflow[i]);
778 static void connman_iptables_dump(struct connman_iptables *table)
780 printf("%s valid_hooks=0x%08x, num_entries=%u, size=%u\n",
782 table->info->valid_hooks, table->info->num_entries,
785 connman_iptables_dump_hook(table);
787 ENTRY_ITERATE(table->blob_entries->entrytable,
788 table->blob_entries->size,
789 connman_iptables_dump_entry, table);
793 static int connman_iptables_get_entries(struct connman_iptables *table)
795 socklen_t entry_size;
797 entry_size = sizeof(struct ipt_get_entries) + table->info->size;
799 return getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_ENTRIES,
800 table->blob_entries, &entry_size);
803 static int connman_iptables_replace(struct connman_iptables *table,
804 struct ipt_replace *r)
806 return setsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_SET_REPLACE, r,
807 sizeof(*r) + r->size);
810 static void connman_iptables_cleanup(struct connman_iptables *table)
813 struct connman_iptables_entry *entry;
815 close(table->ipt_sock);
817 for (list = table->entries; list; list = list->next) {
820 g_free(entry->entry);
824 g_free(table->blob_entries);
827 xtables_free_opts(1);
830 static int connman_iptables_commit(struct connman_iptables *table)
832 struct ipt_replace *repl;
834 repl = connman_iptables_blob(table);
836 return connman_iptables_replace(table, repl);
839 static int add_entry(struct ipt_entry *entry, struct connman_iptables *table)
841 struct ipt_entry *new_entry;
844 new_entry = g_try_malloc0(entry->next_offset);
845 if (new_entry == NULL)
848 memcpy(new_entry, entry, entry->next_offset);
850 builtin = is_hook_entry(table, entry);
852 return connman_add_entry(table, new_entry, NULL, builtin);
855 static struct connman_iptables *connman_iptables_init(const char *table_name)
857 struct connman_iptables *table;
860 table = g_try_new0(struct connman_iptables, 1);
864 table->info = g_try_new0(struct ipt_getinfo, 1);
865 if (table->info == NULL)
868 table->ipt_sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
869 if (table->ipt_sock < 0)
872 s = sizeof(*table->info);
873 strcpy(table->info->name, table_name);
874 if (getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_INFO,
875 table->info, &s) < 0)
878 table->blob_entries = g_try_malloc0(sizeof(struct ipt_get_entries) +
880 if (table->blob_entries == NULL)
883 strcpy(table->blob_entries->name, table_name);
884 table->blob_entries->size = table->info->size;
886 if (connman_iptables_get_entries(table) < 0)
889 table->num_entries = 0;
890 table->old_entries = table->info->num_entries;
893 memcpy(table->underflow, table->info->underflow,
894 sizeof(table->info->underflow));
895 memcpy(table->hook_entry, table->info->hook_entry,
896 sizeof(table->info->hook_entry));
898 ENTRY_ITERATE(table->blob_entries->entrytable,
899 table->blob_entries->size,
907 connman_iptables_cleanup(table);
913 static struct option connman_iptables_opts[] = {
914 {.name = "append", .has_arg = 1, .val = 'A'},
915 {.name = "list", .has_arg = 2, .val = 'L'},
916 {.name = "new-chain", .has_arg = 1, .val = 'N'},
917 {.name = "delete-chain", .has_arg = 1, .val = 'X'},
918 {.name = "in-interface", .has_arg = 1, .val = 'i'},
919 {.name = "jump", .has_arg = 1, .val = 'j'},
920 {.name = "match", .has_arg = 1, .val = 'm'},
921 {.name = "out-interface", .has_arg = 1, .val = 'o'},
922 {.name = "table", .has_arg = 1, .val = 't'},
926 struct xtables_globals connman_iptables_globals = {
928 .opts = connman_iptables_opts,
929 .orig_opts = connman_iptables_opts,
932 int main(int argc, char *argv[])
934 struct connman_iptables *table;
935 struct xtables_match *xt_m;
936 struct xtables_target *xt_t;
937 char *table_name, *chain, *new_chain, *match_name, *target_name;
941 gboolean dump, invert, delete;
943 xtables_init_all(&connman_iptables_globals, NFPROTO_IPV4);
948 table_name = chain = new_chain = match_name = target_name = NULL;
954 while ((c = getopt_long(argc, argv,
955 "-A:L::N:X:j:i:m:o:t:", connman_iptables_globals.opts, NULL)) != -1) {
971 delete_chain = optarg;
975 target_name = optarg;
976 xt_t = xtables_find_target(target_name, XTF_TRY_LOAD);
981 size = ALIGN(sizeof(struct ipt_entry_target)) + xt_t->size;
983 xt_t->t = g_try_malloc0(size);
986 xt_t->t->u.target_size = size;
987 strcpy(xt_t->t->u.user.name, target_name);
988 xt_t->t->u.user.revision = xt_t->revision;
989 if (xt_t->init != NULL)
991 connman_iptables_globals.opts =
992 xtables_merge_options(connman_iptables_globals.opts,
994 &xt_t->option_offset);
995 if (connman_iptables_globals.opts == NULL)
1004 match_name = optarg;
1006 xt_m = xtables_find_match(optarg, XTF_LOAD_MUST_SUCCEED, NULL);
1007 size = ALIGN(sizeof(struct ipt_entry_match)) + xt_m->size;
1008 xt_m->m = g_try_malloc0(size);
1011 xt_m->m->u.match_size = size;
1012 strcpy(xt_m->m->u.user.name, xt_m->name);
1013 xt_m->m->u.user.revision = xt_m->revision;
1014 if (xt_m->init != NULL)
1015 xt_m->init(xt_m->m);
1016 if (xt_m != xt_m->next) {
1017 connman_iptables_globals.opts =
1018 xtables_merge_options(connman_iptables_globals.opts,
1020 &xt_m->option_offset);
1021 if (connman_iptables_globals.opts == NULL)
1031 table_name = optarg;
1035 if (optarg[0] == '!' && optarg[1] == '\0') {
1037 printf("Consecutive ! not allowed\n");
1044 printf("Invalid option\n");
1049 if (xt_t == NULL || xt_t->parse == NULL ||
1050 !xt_t->parse(c - xt_t->option_offset, argv, invert,
1051 &xt_t->tflags, NULL, &xt_t->t)) {
1052 if (xt_m == NULL || xt_m->parse == NULL)
1055 xt_m->parse(c - xt_m->option_offset, argv,
1056 invert, &xt_m->mflags, NULL, &xt_m->m);
1063 if (table_name == NULL)
1064 table_name = "filter";
1066 table = connman_iptables_init(table_name);
1071 if (delete_chain == NULL)
1074 printf("Delete chain %s\n", delete_chain);
1076 connman_iptables_delete_chain(table, delete_chain);
1082 connman_iptables_dump(table);
1087 if (chain && new_chain)
1091 printf("New chain %s\n", new_chain);
1093 connman_iptables_add_chain(table, new_chain);
1099 if (target_name == NULL)
1102 printf("Adding %s to %s (match %s)\n", target_name, chain, match_name);
1104 connman_iptables_add_rule(table, chain, target_name, xt_t,
1112 connman_iptables_commit(table);
1115 connman_iptables_cleanup(table);