5 * Copyright (C) 2013 BMW Car IT GmbH.
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
29 #include <linux/netfilter_ipv4/ip_tables.h>
33 #define CHAIN_PREFIX "connman-"
35 static const char *builtin_chains[] = {
36 [NF_IP_PRE_ROUTING] = "PREROUTING",
37 [NF_IP_LOCAL_IN] = "INPUT",
38 [NF_IP_FORWARD] = "FORWARD",
39 [NF_IP_LOCAL_OUT] = "OUTPUT",
40 [NF_IP_POST_ROUTING] = "POSTROUTING",
43 struct connman_managed_table {
45 unsigned int chains[NF_INET_NUMHOOKS];
54 struct firewall_context {
58 static GSList *managed_tables;
60 static int chain_to_index(const char *chain_name)
62 if (!g_strcmp0(builtin_chains[NF_IP_PRE_ROUTING], chain_name))
63 return NF_IP_PRE_ROUTING;
64 if (!g_strcmp0(builtin_chains[NF_IP_LOCAL_IN], chain_name))
65 return NF_IP_LOCAL_IN;
66 if (!g_strcmp0(builtin_chains[NF_IP_FORWARD], chain_name))
68 if (!g_strcmp0(builtin_chains[NF_IP_LOCAL_OUT], chain_name))
69 return NF_IP_LOCAL_OUT;
70 if (!g_strcmp0(builtin_chains[NF_IP_POST_ROUTING], chain_name))
71 return NF_IP_POST_ROUTING;
76 static int managed_chain_to_index(const char *chain_name)
78 if (g_str_has_prefix(chain_name, CHAIN_PREFIX) == FALSE)
81 return chain_to_index(chain_name + strlen(CHAIN_PREFIX));
84 static int insert_managed_chain(const char *table_name, int id)
86 char *rule, *managed_chain;
89 managed_chain = g_strdup_printf("%s%s", CHAIN_PREFIX,
92 err = __connman_iptables_new_chain(table_name, managed_chain);
96 rule = g_strdup_printf("-j %s", managed_chain);
97 err = __connman_iptables_insert(table_name, builtin_chains[id], rule);
100 __connman_iptables_delete_chain(table_name, managed_chain);
105 g_free(managed_chain);
110 static int delete_managed_chain(const char *table_name, int id)
112 char *rule, *managed_chain;
115 managed_chain = g_strdup_printf("%s%s", CHAIN_PREFIX,
118 rule = g_strdup_printf("-j %s", managed_chain);
119 err = __connman_iptables_delete(table_name, builtin_chains[id], rule);
125 err = __connman_iptables_delete_chain(table_name, managed_chain);
128 g_free(managed_chain);
133 static int insert_managed_rule(const char *table_name,
134 const char *chain_name,
135 const char *rule_spec)
137 struct connman_managed_table *mtable = NULL;
142 id = chain_to_index(chain_name);
144 /* This chain is not managed */
145 chain = g_strdup(chain_name);
149 for (list = managed_tables; list != NULL; list = list->next) {
152 if (g_strcmp0(mtable->name, table_name) == 0)
158 if (mtable == NULL) {
159 mtable = g_new0(struct connman_managed_table, 1);
160 mtable->name = g_strdup(table_name);
162 managed_tables = g_slist_prepend(managed_tables, mtable);
165 if (mtable->chains[id] == 0) {
166 DBG("table %s add managed chain for %s",
167 table_name, chain_name);
169 err = insert_managed_chain(table_name, id);
174 mtable->chains[id]++;
175 chain = g_strdup_printf("%s%s", CHAIN_PREFIX, chain_name);
178 err = __connman_iptables_append(table_name, chain, rule_spec);
185 static int delete_managed_rule(const char *table_name,
186 const char *chain_name,
187 const char *rule_spec)
189 struct connman_managed_table *mtable = NULL;
194 id = chain_to_index(chain_name);
196 /* This chain is not managed */
197 return __connman_iptables_delete(table_name, chain_name,
201 managed_chain = g_strdup_printf("%s%s", CHAIN_PREFIX, chain_name);
203 err = __connman_iptables_delete(table_name, managed_chain,
206 for (list = managed_tables; list != NULL; list = list->next) {
209 if (g_strcmp0(mtable->name, table_name) == 0)
215 if (mtable == NULL) {
220 mtable->chains[id]--;
221 if (mtable->chains[id] > 0)
224 DBG("table %s remove managed chain for %s",
225 table_name, chain_name);
227 err = delete_managed_chain(table_name, id);
230 g_free(managed_chain);
235 static void cleanup_managed_table(gpointer user_data)
237 struct connman_managed_table *table = user_data;
243 static void cleanup_fw_rule(gpointer user_data)
245 struct fw_rule *rule = user_data;
247 g_free(rule->rule_spec);
253 struct firewall_context *__connman_firewall_create(void)
255 struct firewall_context *ctx;
257 ctx = g_new0(struct firewall_context, 1);
262 void __connman_firewall_destroy(struct firewall_context *ctx)
264 g_list_free_full(ctx->rules, cleanup_fw_rule);
268 int __connman_firewall_add_rule(struct firewall_context *ctx,
271 const char *rule_fmt, ...)
275 struct fw_rule *rule;
277 va_start(args, rule_fmt);
279 rule_spec = g_strdup_vprintf(rule_fmt, args);
283 rule = g_new0(struct fw_rule, 1);
285 rule->table = g_strdup(table);
286 rule->chain = g_strdup(chain);
287 rule->rule_spec = rule_spec;
289 ctx->rules = g_list_append(ctx->rules, rule);
294 static int firewall_disable(GList *rules)
296 struct fw_rule *rule;
300 for (list = rules; list != NULL; list = g_list_previous(list)) {
303 err = delete_managed_rule(rule->table,
304 rule->chain, rule->rule_spec);
306 connman_error("Cannot remove previously installed "
307 "iptables rules: %s", strerror(-err));
311 err = __connman_iptables_commit(rule->table);
313 connman_error("Cannot remove previously installed "
314 "iptables rules: %s", strerror(-err));
322 int __connman_firewall_enable(struct firewall_context *ctx)
324 struct fw_rule *rule;
328 for (list = g_list_first(ctx->rules); list != NULL;
329 list = g_list_next(list)) {
332 DBG("%s %s %s", rule->table, rule->chain, rule->rule_spec);
334 err = insert_managed_rule(rule->table,
335 rule->chain, rule->rule_spec);
339 err = __connman_iptables_commit(rule->table);
347 connman_warn("Failed to install iptables rules: %s", strerror(-err));
349 firewall_disable(g_list_previous(list));
354 int __connman_firewall_disable(struct firewall_context *ctx)
356 return firewall_disable(g_list_last(ctx->rules));
359 static void iterate_chains_cb(const char *chain_name, void *user_data)
361 GSList **chains = user_data;
364 id = managed_chain_to_index(chain_name);
368 *chains = g_slist_prepend(*chains, GINT_TO_POINTER(id));
371 static void flush_table(const char *table_name)
373 GSList *chains = NULL, *list;
374 char *rule, *managed_chain;
377 __connman_iptables_iterate_chains(table_name, iterate_chains_cb,
380 for (list = chains; list != NULL; list = list->next) {
381 id = GPOINTER_TO_INT(list->data);
383 managed_chain = g_strdup_printf("%s%s", CHAIN_PREFIX,
386 rule = g_strdup_printf("-j %s", managed_chain);
387 err = __connman_iptables_delete(table_name,
388 builtin_chains[id], rule);
390 connman_warn("Failed to delete jump rule '%s': %s",
391 rule, strerror(-err));
395 err = __connman_iptables_flush_chain(table_name, managed_chain);
397 connman_warn("Failed to flush chain '%s': %s",
398 managed_chain, strerror(-err));
400 err = __connman_iptables_delete_chain(table_name, managed_chain);
402 connman_warn("Failed to delete chain '%s': %s",
403 managed_chain, strerror(-err));
406 g_free(managed_chain);
409 err = __connman_iptables_commit(table_name);
411 connman_warn("Failed to flush table '%s': %s",
412 table_name, strerror(-err));
415 g_slist_free(chains);
418 static void flush_all_tables(void)
420 /* Flush the tables ConnMan might have modified */
422 flush_table("filter");
423 flush_table("mangle");
427 int __connman_firewall_init(void)
436 void __connman_firewall_cleanup(void)
440 g_slist_free_full(managed_tables, cleanup_managed_table);