5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 #include <sys/errno.h>
32 #include <sys/socket.h>
35 #include <linux/netfilter_ipv4/ip_tables.h>
40 static const char *hooknames[] = {
41 [NF_IP_PRE_ROUTING] = "PREROUTING",
42 [NF_IP_LOCAL_IN] = "INPUT",
43 [NF_IP_FORWARD] = "FORWARD",
44 [NF_IP_LOCAL_OUT] = "OUTPUT",
45 [NF_IP_POST_ROUTING] = "POSTROUTING",
48 #define LABEL_ACCEPT "ACCEPT"
49 #define LABEL_DROP "DROP"
50 #define LABEL_QUEUE "QUEUE"
51 #define LABEL_RETURN "RETURN"
53 /* fn returns 0 to continue iteration */
54 #define _XT_ENTRY_ITERATE_CONTINUE(type, entries, size, n, fn, args...) \
61 for (__i = 0, __n = 0; __i < (size); \
62 __i += __entry->next_offset, __n++) { \
63 __entry = (void *)(entries) + __i; \
67 __ret = fn(__entry, ## args); \
74 /* fn returns 0 to continue iteration */
75 #define _XT_ENTRY_ITERATE(type, entries, size, fn, args...) \
76 _XT_ENTRY_ITERATE_CONTINUE(type, entries, size, 0, fn, args)
78 #define ENTRY_ITERATE(entries, size, fn, args...) \
79 _XT_ENTRY_ITERATE(struct ipt_entry, entries, size, fn, ## args)
81 #define MIN_ALIGN (__alignof__(struct ipt_entry))
83 #define ALIGN(s) (((s) + ((MIN_ALIGN)-1)) & ~((MIN_ALIGN)-1))
85 struct ipt_error_target {
86 struct xt_entry_target t;
87 char error[IPT_TABLE_MAXNAMELEN];
90 struct connman_iptables_entry {
93 struct ipt_entry *entry;
96 struct connman_iptables {
99 struct ipt_getinfo *info;
100 struct ipt_get_entries *blob_entries;
102 unsigned int num_entries;
103 unsigned int old_entries;
109 static GHashTable *table_hash = NULL;
111 static struct ipt_entry *get_entry(struct connman_iptables *table,
114 return (struct ipt_entry *)((char *)table->blob_entries->entrytable +
118 static int is_hook_entry(struct connman_iptables *table,
119 struct ipt_entry *entry)
123 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
124 if ((table->info->valid_hooks & (1 << i))
125 && get_entry(table, table->info->hook_entry[i]) == entry)
132 static unsigned long entry_to_offset(struct connman_iptables *table,
133 struct ipt_entry *entry)
135 return (void *)entry - (void *)table->blob_entries->entrytable;
138 static int target_to_verdict(char *target_name)
140 if (!strcmp(target_name, LABEL_ACCEPT))
141 return -NF_ACCEPT - 1;
143 if (!strcmp(target_name, LABEL_DROP))
146 if (!strcmp(target_name, LABEL_QUEUE))
147 return -NF_QUEUE - 1;
149 if (!strcmp(target_name, LABEL_RETURN))
155 static gboolean is_builtin_target(char *target_name)
157 if (!strcmp(target_name, LABEL_ACCEPT) ||
158 !strcmp(target_name, LABEL_DROP) ||
159 !strcmp(target_name, LABEL_QUEUE) ||
160 !strcmp(target_name, LABEL_RETURN))
166 static gboolean is_jump(struct connman_iptables_entry *e)
168 struct xt_entry_target *target;
170 target = ipt_get_target(e->entry);
172 if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
173 struct xt_standard_target *t;
175 t = (struct xt_standard_target *)target;
177 switch (t->verdict) {
193 static gboolean is_chain(struct connman_iptables *table,
194 struct connman_iptables_entry *e)
197 struct ipt_entry *entry;
198 struct xt_entry_target *target;
201 builtin = is_hook_entry(table, entry);
205 target = ipt_get_target(entry);
206 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET))
212 static GList *find_chain_head(struct connman_iptables *table,
216 struct connman_iptables_entry *head;
217 struct ipt_entry *entry;
218 struct xt_entry_target *target;
221 for (list = table->entries; list; list = list->next) {
226 builtin = is_hook_entry(table, entry);
227 if (builtin >= 0 && !strcmp(hooknames[builtin], chain_name))
230 /* User defined chain */
231 target = ipt_get_target(entry);
232 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET) &&
233 !strcmp((char *)target->data, chain_name))
240 static GList *find_chain_tail(struct connman_iptables *table,
243 GList *chain_head, *list;
244 struct connman_iptables_entry *head, *tail;
245 struct ipt_entry *entry;
246 struct xt_entry_target *target;
249 /* First we look for the head */
250 for (list = table->entries; list; list = list->next) {
255 builtin = is_hook_entry(table, entry);
256 if (builtin >= 0 && !strcmp(hooknames[builtin], chain_name))
259 /* User defined chain */
260 target = ipt_get_target(entry);
261 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET) &&
262 !strcmp((char *)target->data, chain_name))
271 /* Then we look for the next chain */
272 for (list = chain_head->next; list; list = list->next) {
276 if (is_chain(table, tail))
280 /* Nothing found, we return the table end */
281 return g_list_last(table->entries);
285 static void update_offsets(struct connman_iptables *table)
288 struct connman_iptables_entry *entry, *prev_entry;
290 for (list = table->entries; list; list = list->next) {
293 if (list == table->entries) {
300 prev_entry = prev->data;
302 entry->offset = prev_entry->offset +
303 prev_entry->entry->next_offset;
307 static int iptables_add_entry(struct connman_iptables *table,
308 struct ipt_entry *entry, GList *before)
311 struct connman_iptables_entry *e, *tmp, *entry_before;
312 struct xt_standard_target *t;
317 e = g_try_malloc0(sizeof(struct connman_iptables_entry));
323 table->entries = g_list_insert_before(table->entries, before, e);
324 table->num_entries++;
325 table->size += entry->next_offset;
327 if (before == NULL) {
328 e->offset = table->size - entry->next_offset;
333 entry_before = before->data;
336 * We've just insterted a new entry. All references before it
337 * should be bumped accordingly.
339 for (list = table->entries; list != before; list = list->next) {
345 t = (struct xt_standard_target *)ipt_get_target(tmp->entry);
347 if (t->verdict >= entry_before->offset)
348 t->verdict += entry->next_offset;
351 update_offsets(table);
356 static int iptables_add_chain(struct connman_iptables *table,
360 struct ipt_entry *entry_head;
361 struct ipt_entry *entry_return;
362 struct ipt_error_target *error;
363 struct ipt_standard_target *standard;
364 u_int16_t entry_head_size, entry_return_size;
366 last = g_list_last(table->entries);
369 * An empty chain is composed of:
370 * - A head entry, with no match and an error target.
371 * The error target data is the chain name.
372 * - A tail entry, with no match and a standard target.
373 * The standard target verdict is XT_RETURN (return to the
378 entry_head_size = sizeof(struct ipt_entry) +
379 sizeof(struct ipt_error_target);
380 entry_head = g_try_malloc0(entry_head_size);
381 if (entry_head == NULL)
384 memset(entry_head, 0, entry_head_size);
386 entry_head->target_offset = sizeof(struct ipt_entry);
387 entry_head->next_offset = entry_head_size;
389 error = (struct ipt_error_target *) entry_head->elems;
390 strcpy(error->t.u.user.name, IPT_ERROR_TARGET);
391 error->t.u.user.target_size = ALIGN(sizeof(struct ipt_error_target));
392 strcpy(error->error, name);
394 if (iptables_add_entry(table, entry_head, last) < 0)
398 entry_return_size = sizeof(struct ipt_entry) +
399 sizeof(struct ipt_standard_target);
400 entry_return = g_try_malloc0(entry_return_size);
401 if (entry_return == NULL)
404 memset(entry_return, 0, entry_return_size);
406 entry_return->target_offset = sizeof(struct ipt_entry);
407 entry_return->next_offset = entry_return_size;
409 standard = (struct ipt_standard_target *) entry_return->elems;
410 standard->target.u.user.target_size =
411 ALIGN(sizeof(struct ipt_standard_target));
412 standard->verdict = XT_RETURN;
414 if (iptables_add_entry(table, entry_return, last) < 0)
421 g_free(entry_return);
426 static struct ipt_entry *
427 new_rule(struct connman_iptables *table, struct ipt_ip *ip,
428 char *target_name, struct xtables_target *xt_t,
429 char *match_name, struct xtables_match *xt_m)
431 struct ipt_entry *new_entry;
432 size_t match_size, target_size;
433 int is_builtin = is_builtin_target(target_name);
436 match_size = xt_m->m->u.match_size;
441 target_size = ALIGN(xt_t->t->u.target_size);
443 target_size = ALIGN(sizeof(struct xt_standard_target));
445 new_entry = g_try_malloc0(sizeof(struct ipt_entry) + target_size +
447 if (new_entry == NULL)
450 memcpy(&new_entry->ip, ip, sizeof(struct ipt_ip));
452 new_entry->target_offset = sizeof(struct ipt_entry) + match_size;
453 new_entry->next_offset = sizeof(struct ipt_entry) + target_size +
456 struct xt_entry_match *entry_match;
458 entry_match = (struct xt_entry_match *)new_entry->elems;
459 memcpy(entry_match, xt_m->m, match_size);
463 struct xt_entry_target *entry_target;
466 struct xt_standard_target *target;
468 target = (struct xt_standard_target *)(xt_t->t);
469 strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
470 target->verdict = target_to_verdict(target_name);
473 entry_target = ipt_get_target(new_entry);
474 memcpy(entry_target, xt_t->t, target_size);
476 struct connman_iptables_entry *target_rule;
477 struct xt_standard_target *target;
481 * This is a user defined target, i.e. a chain jump.
482 * We search for the chain head, and the target verdict
483 * is the first rule's offset on this chain.
484 * The offset is from the beginning of the table.
487 chain_head = find_chain_head(table, target_name);
488 if (chain_head == NULL || chain_head->next == NULL) {
493 target_rule = chain_head->next->data;
495 target = (struct xt_standard_target *)ipt_get_target(new_entry);
496 strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
497 target->target.u.user.target_size = target_size;
498 target->verdict = target_rule->offset;
505 iptables_add_rule(struct connman_iptables *table,
506 struct ipt_ip *ip, char *chain_name,
507 char *target_name, struct xtables_target *xt_t,
508 char *match_name, struct xtables_match *xt_m)
511 struct ipt_entry *new_entry;
513 chain_tail = find_chain_tail(table, chain_name);
514 if (chain_tail == NULL)
517 new_entry = new_rule(table, ip,
520 if (new_entry == NULL)
523 return iptables_add_entry(table, new_entry, chain_tail->prev);
526 static struct ipt_replace *
527 iptables_blob(struct connman_iptables *table)
529 struct ipt_replace *r;
531 struct connman_iptables_entry *e;
532 unsigned char *entry_index;
534 r = g_try_malloc0(sizeof(struct ipt_replace) + table->size);
538 memset(r, 0, sizeof(*r) + table->size);
540 r->counters = g_try_malloc0(sizeof(struct xt_counters)
541 * table->num_entries);
542 if (r->counters == NULL) {
547 strcpy(r->name, table->info->name);
548 r->num_entries = table->num_entries;
549 r->size = table->size;
551 r->num_counters = table->old_entries;
552 r->valid_hooks = table->info->valid_hooks;
554 memcpy(r->hook_entry, table->info->hook_entry,
555 sizeof(table->info->hook_entry));
556 memcpy(r->underflow, table->info->underflow,
557 sizeof(table->info->underflow));
559 entry_index = (unsigned char *)r->entries;
560 for (list = table->entries; list; list = list->next) {
563 memcpy(entry_index, e->entry, e->entry->next_offset);
564 entry_index += e->entry->next_offset;
570 static void dump_ip(struct ipt_entry *entry)
572 struct ipt_ip *ip = &entry->ip;
574 if (strlen(ip->iniface))
575 connman_info("\tin %s", ip->iniface);
577 if (strlen(ip->outiface))
578 connman_info("\tout %s", ip->outiface);
581 static void dump_target(struct connman_iptables *table,
582 struct ipt_entry *entry)
585 struct xtables_target *xt_t;
586 struct xt_entry_target *target;
588 target = ipt_get_target(entry);
590 if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
591 struct xt_standard_target *t;
593 t = (struct xt_standard_target *)target;
595 switch (t->verdict) {
597 connman_info("\ttarget RETURN");
601 connman_info("\ttarget ACCEPT");
605 connman_info("\ttarget DROP");
609 connman_info("\ttarget QUEUE");
613 connman_info("\ttarget STOP");
617 connman_info("\tJUMP @%p (0x%x)",
618 (char*)table->blob_entries->entrytable +
619 t->verdict, t->verdict);
623 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
624 XTF_LOAD_MUST_SUCCEED);
626 if(xt_t->print != NULL)
627 xt_t->print(NULL, target, 1);
629 xt_t = xtables_find_target(target->u.user.name, XTF_TRY_LOAD);
631 connman_info("\ttarget %s", target->u.user.name);
635 if(xt_t->print != NULL) {
636 connman_info("\ttarget ");
637 xt_t->print(NULL, target, 1);
642 static void dump_match(struct connman_iptables *table, struct ipt_entry *entry)
644 struct xtables_match *xt_m;
645 struct xt_entry_match *match;
647 if (entry->elems == (unsigned char *)entry + entry->target_offset)
650 match = (struct xt_entry_match *) entry->elems;
652 if (!strlen(match->u.user.name))
655 xt_m = xtables_find_match(match->u.user.name, XTF_TRY_LOAD, NULL);
659 if(xt_m->print != NULL) {
660 connman_info("\tmatch ");
661 xt_m->print(NULL, match, 1);
667 connman_info("\tmatch %s", match->u.user.name);
671 static int dump_entry(struct ipt_entry *entry,
672 struct connman_iptables *table)
674 struct xt_entry_target *target;
678 offset = (char *)entry - (char *)table->blob_entries->entrytable;
679 target = ipt_get_target(entry);
680 builtin = is_hook_entry(table, entry);
682 if (entry_to_offset(table, entry) + entry->next_offset ==
683 table->blob_entries->size) {
684 connman_info("End of CHAIN 0x%x", offset);
688 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET)) {
689 connman_info("USER CHAIN (%s) %p match %p target %p size %d",
690 target->data, entry, entry->elems,
691 (char *)entry + entry->target_offset,
695 } else if (builtin >= 0) {
696 connman_info("CHAIN (%s) %p match %p target %p size %d",
697 hooknames[builtin], entry, entry->elems,
698 (char *)entry + entry->target_offset,
701 connman_info("RULE %p match %p target %p size %d", entry,
703 (char *)entry + entry->target_offset,
707 dump_match(table, entry);
708 dump_target(table, entry);
714 static void iptables_dump(struct connman_iptables *table)
716 connman_info("%s valid_hooks=0x%08x, num_entries=%u, size=%u",
718 table->info->valid_hooks, table->info->num_entries,
721 ENTRY_ITERATE(table->blob_entries->entrytable,
722 table->blob_entries->size,
727 static int iptables_get_entries(struct connman_iptables *table)
729 socklen_t entry_size;
731 entry_size = sizeof(struct ipt_get_entries) + table->info->size;
733 return getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_ENTRIES,
734 table->blob_entries, &entry_size);
737 static int iptables_replace(struct connman_iptables *table,
738 struct ipt_replace *r)
740 return setsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_SET_REPLACE, r,
741 sizeof(*r) + r->size);
744 static int add_entry(struct ipt_entry *entry, struct connman_iptables *table)
746 struct ipt_entry *new_entry;
748 new_entry = g_try_malloc0(entry->next_offset);
749 if (new_entry == NULL)
752 memcpy(new_entry, entry, entry->next_offset);
754 return iptables_add_entry(table, new_entry, NULL);
757 static void table_cleanup(struct connman_iptables *table)
760 struct connman_iptables_entry *entry;
762 close(table->ipt_sock);
764 for (list = table->entries; list; list = list->next) {
767 g_free(entry->entry);
770 g_list_free(table->entries);
772 g_free(table->blob_entries);
776 static struct connman_iptables *iptables_init(char *table_name)
778 struct connman_iptables *table;
781 table = g_hash_table_lookup(table_hash, table_name);
785 table = g_try_new0(struct connman_iptables, 1);
789 table->info = g_try_new0(struct ipt_getinfo, 1);
790 if (table->info == NULL)
793 table->ipt_sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
794 if (table->ipt_sock < 0)
797 s = sizeof(*table->info);
798 strcpy(table->info->name, table_name);
799 if (getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_INFO,
800 table->info, &s) < 0)
803 table->blob_entries = g_try_malloc0(sizeof(struct ipt_get_entries) +
805 if (table->blob_entries == NULL)
808 strcpy(table->blob_entries->name, table_name);
809 table->blob_entries->size = table->info->size;
811 if (iptables_get_entries(table) < 0)
814 table->num_entries = 0;
815 table->old_entries = table->info->num_entries;
818 ENTRY_ITERATE(table->blob_entries->entrytable,
819 table->blob_entries->size,
822 g_hash_table_insert(table_hash, table_name, table);
828 table_cleanup(table);
833 static struct option iptables_opts[] = {
834 {.name = "append", .has_arg = 1, .val = 'A'},
835 {.name = "list", .has_arg = 2, .val = 'L'},
836 {.name = "new-chain", .has_arg = 1, .val = 'N'},
837 {.name = "in-interface", .has_arg = 1, .val = 'i'},
838 {.name = "jump", .has_arg = 1, .val = 'j'},
839 {.name = "match", .has_arg = 1, .val = 'm'},
840 {.name = "out-interface", .has_arg = 1, .val = 'o'},
841 {.name = "table", .has_arg = 1, .val = 't'},
845 struct xtables_globals iptables_globals = {
847 .opts = iptables_opts,
848 .orig_opts = iptables_opts,
851 static int iptables_command(int argc, char *argv[])
853 struct connman_iptables *table;
854 struct xtables_match *xt_m;
855 struct xtables_target *xt_t;
857 char *table_name, *chain, *new_chain, *match_name, *target_name;
858 int c, ret, in_len, out_len;
860 gboolean dump, invert;
867 table_name = chain = new_chain = match_name = target_name = NULL;
868 memset(&ip, 0, sizeof(struct ipt_ip));
876 while ((c = getopt_long(argc, argv,
877 "-A:L::N:j:i:m:o:t:", iptables_globals.opts, NULL)) != -1) {
892 in_len = strlen(optarg);
894 if (in_len + 1 > IFNAMSIZ)
897 strcpy(ip.iniface, optarg);
898 memset(ip.iniface_mask, 0xff, in_len + 1);
903 target_name = optarg;
904 xt_t = xtables_find_target(target_name, XTF_TRY_LOAD);
909 size = ALIGN(sizeof(struct ipt_entry_target)) +
912 xt_t->t = g_try_malloc0(size);
915 xt_t->t->u.target_size = size;
916 strcpy(xt_t->t->u.user.name, target_name);
917 xt_t->t->u.user.revision = xt_t->revision;
918 if (xt_t->init != NULL)
920 iptables_globals.opts =
921 xtables_merge_options(iptables_globals.opts,
923 &xt_t->option_offset);
924 if (iptables_globals.opts == NULL)
932 xt_m = xtables_find_match(optarg, XTF_LOAD_MUST_SUCCEED, NULL);
933 size = ALIGN(sizeof(struct ipt_entry_match)) +
935 xt_m->m = g_try_malloc0(size);
938 xt_m->m->u.match_size = size;
939 strcpy(xt_m->m->u.user.name, xt_m->name);
940 xt_m->m->u.user.revision = xt_m->revision;
941 if (xt_m->init != NULL)
943 if (xt_m != xt_m->next) {
944 iptables_globals.opts =
945 xtables_merge_options(iptables_globals.opts,
947 &xt_m->option_offset);
948 if (iptables_globals.opts == NULL)
955 out_len = strlen(optarg);
957 if (out_len + 1 > IFNAMSIZ)
960 strcpy(ip.outiface, optarg);
961 memset(ip.outiface_mask, 0xff, out_len + 1);
970 if (optarg[0] == '!' && optarg[1] == '\0') {
976 connman_error("Invalid option");
982 if (xt_t == NULL || xt_t->parse == NULL ||
983 !xt_t->parse(c - xt_t->option_offset, argv, invert,
984 &xt_t->tflags, NULL, &xt_t->t)) {
985 if (xt_m == NULL || xt_m->parse == NULL)
988 xt_m->parse(c - xt_m->option_offset, argv,
989 invert, &xt_m->mflags, NULL, &xt_m->m);
996 if (table_name == NULL)
997 table_name = "filter";
999 table = iptables_init(table_name);
1000 if (table == NULL) {
1006 iptables_dump(table);
1012 if (chain && new_chain) {
1018 DBG("New chain %s", new_chain);
1020 ret = iptables_add_chain(table, new_chain);
1025 if (target_name == NULL)
1028 DBG("Adding %s to %s (match %s)",
1029 target_name, chain, match_name);
1031 ret = iptables_add_rule(table, &ip, chain, target_name, xt_t,
1047 int __connman_iptables_command(const char *format, ...)
1049 char **argv, **arguments, *command;
1056 va_start(args, format);
1058 command = g_strdup_vprintf(format, args);
1062 if (command == NULL)
1065 arguments = g_strsplit_set(command, " ", -1);
1067 for (argc = 0; arguments[argc]; argc++);
1069 DBG("command %s argc %d", command, ++argc);
1071 argv = g_try_malloc0(argc * sizeof(char *));
1074 g_strfreev(arguments);
1078 argv[0] = "iptables";
1079 for (i = 1; i < argc; i++)
1080 argv[i] = arguments[i - 1];
1082 ret = iptables_command(argc, argv);
1085 g_strfreev(arguments);
1092 int __connman_iptables_commit(const char *table_name)
1094 struct connman_iptables *table;
1095 struct ipt_replace *repl;
1097 table = g_hash_table_lookup(table_hash, table_name);
1101 repl = iptables_blob(table);
1103 return iptables_replace(table, repl);
1106 static void remove_table(gpointer user_data)
1108 struct connman_iptables *table = user_data;
1110 table_cleanup(table);
1113 int __connman_iptables_init(void)
1117 table_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1118 NULL, remove_table);
1120 xtables_init_all(&iptables_globals, NFPROTO_IPV4);
1126 void __connman_iptables_cleanup(void)
1128 g_hash_table_destroy(table_hash);
1130 xtables_free_opts(1);