*
* Connection Manager
*
- * Copyright (C) 2007-2012 Intel Corporation. All rights reserved.
+ * Copyright (C) 2007-2013 Intel Corporation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
#include <linux/netfilter_ipv4/ip_tables.h>
#include "connman.h"
+#include "src/shared/util.h"
/*
* Some comments on how the iptables API works (some of them from the
};
static GHashTable *table_hash = NULL;
-static gboolean debug_enabled = FALSE;
+static bool debug_enabled = false;
typedef int (*iterate_entries_cb_t)(struct ipt_entry *entry, int builtin,
- unsigned int hook,size_t size,
+ unsigned int hook, size_t size,
unsigned int offset, void *user_data);
static unsigned int next_hook_entry_index(unsigned int *valid_hooks)
if (h == NF_INET_NUMHOOKS)
hook = h;
- if (h < NF_INET_NUMHOOKS && underflow[h] <= offset) {
+ if (h < NF_INET_NUMHOOKS && underflow[h] <= offset)
h = next_hook_entry_index(&valid_hooks);
- }
err = cb(entry, builtin, hook, size, offset, user_data);
if (err < 0)
return 0;
}
-static gboolean is_builtin_target(const char *target_name)
+static bool is_builtin_target(const char *target_name)
{
if (!g_strcmp0(target_name, LABEL_ACCEPT) ||
!g_strcmp0(target_name, LABEL_DROP) ||
!g_strcmp0(target_name, LABEL_QUEUE) ||
!g_strcmp0(target_name, LABEL_RETURN))
- return TRUE;
+ return true;
- return FALSE;
+ return false;
}
-static gboolean is_jump(struct connman_iptables_entry *e)
+static bool is_jump(struct connman_iptables_entry *e)
{
struct xt_entry_target *target;
return false;
}
-static gboolean is_fallthrough(struct connman_iptables_entry *e)
+static bool is_fallthrough(struct connman_iptables_entry *e)
{
struct xt_entry_target *target;
return false;
}
-static gboolean is_chain(struct connman_iptables *table,
+static bool is_chain(struct connman_iptables *table,
struct connman_iptables_entry *e)
{
struct ipt_entry *entry;
entry = e->entry;
if (e->builtin >= 0)
- return TRUE;
+ return true;
target = ipt_get_target(entry);
if (!g_strcmp0(target->u.user.name, IPT_ERROR_TARGET))
- return TRUE;
+ return true;
- return FALSE;
+ return false;
}
static GList *find_chain_head(struct connman_iptables *table,
GList *chain_head, *list;
chain_head = find_chain_head(table, chain_name);
- if (chain_head == NULL)
+ if (!chain_head)
return NULL;
/* Then we look for the next chain */
static void update_targets_reference(struct connman_iptables *table,
struct connman_iptables_entry *entry_before,
struct connman_iptables_entry *modified_entry,
- gboolean is_removing)
+ bool is_removing)
{
struct connman_iptables_entry *tmp;
struct xt_standard_target *t;
t = (struct xt_standard_target *)ipt_get_target(tmp->entry);
- if (is_removing == TRUE) {
+ if (is_removing) {
if (t->verdict >= entry_before->offset)
t->verdict -= offset;
} else {
}
if (is_fallthrough(modified_entry)) {
- t = (struct xt_standard_target *) ipt_get_target(modified_entry->entry);
+ t = (struct xt_standard_target *)
+ ipt_get_target(modified_entry->entry);
t->verdict = entry_before->offset +
modified_entry->entry->target_offset +
{
struct connman_iptables_entry *e, *entry_before;
- if (table == NULL)
+ if (!table)
return -1;
e = g_try_malloc0(sizeof(struct connman_iptables_entry));
- if (e == NULL)
+ if (!e)
return -1;
e->entry = entry;
table->num_entries++;
table->size += entry->next_offset;
- if (before == NULL) {
+ if (!before) {
e->offset = table->size - entry->next_offset;
return 0;
* We've just appended/insterted a new entry. All references
* should be bumped accordingly.
*/
- update_targets_reference(table, entry_before, e, FALSE);
+ update_targets_reference(table, entry_before, e, false);
update_offsets(table);
DBG("table %s chain %s", table->name, name);
chain_head = find_chain_head(table, name);
- if (chain_head == NULL)
+ if (!chain_head)
return -EINVAL;
chain_tail = find_chain_tail(table, name);
- if (chain_tail == NULL)
+ if (!chain_tail)
return -EINVAL;
entry = chain_head->data;
*/
/* head entry */
- entry_head_size = sizeof(struct ipt_entry) +
- sizeof(struct error_target);
+ entry_head_size = ALIGN(sizeof(struct ipt_entry)) +
+ ALIGN(sizeof(struct error_target));
entry_head = g_try_malloc0(entry_head_size);
- if (entry_head == NULL)
+ if (!entry_head)
goto err_head;
- entry_head->target_offset = sizeof(struct ipt_entry);
+ entry_head->target_offset = ALIGN(sizeof(struct ipt_entry));
entry_head->next_offset = entry_head_size;
error = (struct error_target *) entry_head->elems;
goto err_head;
/* tail entry */
- entry_return_size = sizeof(struct ipt_entry) +
- sizeof(struct ipt_standard_target);
+ entry_return_size = ALIGN(sizeof(struct ipt_entry))+
+ ALIGN(sizeof(struct ipt_standard_target));
entry_return = g_try_malloc0(entry_return_size);
- if (entry_return == NULL)
+ if (!entry_return)
goto err;
- entry_return->target_offset = sizeof(struct ipt_entry);
+ entry_return->target_offset = ALIGN(sizeof(struct ipt_entry));
entry_return->next_offset = entry_return_size;
standard = (struct ipt_standard_target *) entry_return->elems;
DBG("table %s chain %s", table->name, name);
chain_head = find_chain_head(table, name);
- if (chain_head == NULL)
+ if (!chain_head)
return -EINVAL;
entry = chain_head->data;
return -EINVAL;
chain_tail = find_chain_tail(table, name);
- if (chain_tail == NULL)
+ if (!chain_tail)
return -EINVAL;
/* Chain must be flushed */
size_t match_size, target_size;
match_size = 0;
- for (tmp_xt_rm = xt_rm; tmp_xt_rm != NULL; tmp_xt_rm = tmp_xt_rm->next)
+ for (tmp_xt_rm = xt_rm; tmp_xt_rm; tmp_xt_rm = tmp_xt_rm->next)
match_size += tmp_xt_rm->match->m->u.match_size;
if (xt_t)
- target_size = ALIGN(xt_t->t->u.target_size);
+ target_size = xt_t->t->u.target_size;
else
target_size = ALIGN(sizeof(struct xt_standard_target));
- new_entry = g_try_malloc0(sizeof(struct ipt_entry) + target_size +
- match_size);
- if (new_entry == NULL)
+ new_entry = g_try_malloc0(ALIGN(sizeof(struct ipt_entry)) +
+ target_size + match_size);
+ if (!new_entry)
return NULL;
memcpy(&new_entry->ip, ip, sizeof(struct ipt_ip));
- new_entry->target_offset = sizeof(struct ipt_entry) + match_size;
- new_entry->next_offset = sizeof(struct ipt_entry) + target_size +
- match_size;
+ new_entry->target_offset = ALIGN(sizeof(struct ipt_entry)) +
+ match_size;
+ new_entry->next_offset = ALIGN(sizeof(struct ipt_entry)) +
+ target_size + match_size;
match_size = 0;
- for (tmp_xt_rm = xt_rm; tmp_xt_rm != NULL;
+ for (tmp_xt_rm = xt_rm; tmp_xt_rm;
tmp_xt_rm = tmp_xt_rm->next) {
memcpy(new_entry->elems + match_size, tmp_xt_rm->match->m,
tmp_xt_rm->match->m->u.match_size);
struct connman_iptables_entry *head, *e;
int builtin;
- if (chain_head == NULL)
+ if (!chain_head)
return;
head = chain_head->data;
const char *target_name,
struct xtables_target *xt_t,
int *builtin, struct xtables_rule_match *xt_rm,
- connman_bool_t insert)
+ bool insert)
{
GList *chain_tail, *chain_head;
struct ipt_entry *new_entry;
struct connman_iptables_entry *head;
chain_head = find_chain_head(table, chain_name);
- if (chain_head == NULL)
+ if (!chain_head)
return NULL;
chain_tail = find_chain_tail(table, chain_name);
- if (chain_tail == NULL)
+ if (!chain_tail)
return NULL;
new_entry = new_rule(ip, target_name, xt_t, xt_rm);
- if (new_entry == NULL)
+ if (!new_entry)
return NULL;
update_hooks(table, chain_head, new_entry);
head = chain_head->data;
if (head->builtin < 0)
*builtin = -1;
- else if (insert == TRUE || chain_head == chain_tail->prev) {
+ else if (insert || chain_head == chain_tail->prev) {
*builtin = head->builtin;
head->builtin = -1;
}
DBG("table %s chain %s", table->name, chain_name);
chain_tail = find_chain_tail(table, chain_name);
- if (chain_tail == NULL)
+ if (!chain_tail)
return -EINVAL;
new_entry = prepare_rule_inclusion(table, ip, chain_name,
- target_name, xt_t, &builtin, xt_rm, FALSE);
- if (new_entry == NULL)
+ target_name, xt_t, &builtin, xt_rm, false);
+ if (!new_entry)
return -EINVAL;
ret = iptables_add_entry(table, new_entry, chain_tail->prev, builtin);
DBG("table %s chain %s", table->name, chain_name);
chain_head = find_chain_head(table, chain_name);
- if (chain_head == NULL)
+ if (!chain_head)
return -EINVAL;
new_entry = prepare_rule_inclusion(table, ip, chain_name,
- target_name, xt_t, &builtin, xt_rm, TRUE);
- if (new_entry == NULL)
+ target_name, xt_t, &builtin, xt_rm, true);
+ if (!new_entry)
return -EINVAL;
if (builtin == -1)
return ret;
}
-static gboolean is_same_ipt_entry(struct ipt_entry *i_e1,
+static bool is_same_ipt_entry(struct ipt_entry *i_e1,
struct ipt_entry *i_e2)
{
if (memcmp(&i_e1->ip, &i_e2->ip, sizeof(struct ipt_ip)) != 0)
- return FALSE;
+ return false;
if (i_e1->target_offset != i_e2->target_offset)
- return FALSE;
+ return false;
if (i_e1->next_offset != i_e2->next_offset)
- return FALSE;
+ return false;
- return TRUE;
+ return true;
}
-static gboolean is_same_target(struct xt_entry_target *xt_e_t1,
+static bool is_same_target(struct xt_entry_target *xt_e_t1,
struct xt_entry_target *xt_e_t2)
{
unsigned int i;
- if (xt_e_t1 == NULL || xt_e_t2 == NULL)
- return FALSE;
+ if (!xt_e_t1 || !xt_e_t2)
+ return false;
if (g_strcmp0(xt_e_t1->u.user.name, "") == 0 &&
g_strcmp0(xt_e_t2->u.user.name, "") == 0) {
/* fallthrough */
- return TRUE;
+ return true;
} else if (g_strcmp0(xt_e_t1->u.user.name, IPT_STANDARD_TARGET) == 0) {
struct xt_standard_target *xt_s_t1;
struct xt_standard_target *xt_s_t2;
xt_s_t2 = (struct xt_standard_target *) xt_e_t2;
if (xt_s_t1->verdict != xt_s_t2->verdict)
- return FALSE;
+ return false;
} else {
if (xt_e_t1->u.target_size != xt_e_t2->u.target_size)
- return FALSE;
+ return false;
if (g_strcmp0(xt_e_t1->u.user.name, xt_e_t2->u.user.name) != 0)
- return FALSE;
+ return false;
for (i = 0; i < xt_e_t1->u.target_size -
sizeof(struct xt_standard_target); i++) {
if ((xt_e_t1->data[i] ^ xt_e_t2->data[i]) != 0)
- return FALSE;
+ return false;
}
}
- return TRUE;
+ return true;
}
-static gboolean is_same_match(struct xt_entry_match *xt_e_m1,
+static bool is_same_match(struct xt_entry_match *xt_e_m1,
struct xt_entry_match *xt_e_m2)
{
unsigned int i;
- if (xt_e_m1 == NULL || xt_e_m2 == NULL)
- return FALSE;
+ if (!xt_e_m1 || !xt_e_m2)
+ return false;
if (xt_e_m1->u.match_size != xt_e_m2->u.match_size)
- return FALSE;
+ return false;
if (xt_e_m1->u.user.revision != xt_e_m2->u.user.revision)
- return FALSE;
+ return false;
if (g_strcmp0(xt_e_m1->u.user.name, xt_e_m2->u.user.name) != 0)
- return FALSE;
+ return false;
for (i = 0; i < xt_e_m1->u.match_size - sizeof(struct xt_entry_match);
i++) {
if ((xt_e_m1->data[i] ^ xt_e_m2->data[i]) != 0)
- return FALSE;
+ return false;
}
- return TRUE;
+ return true;
}
static GList *find_existing_rule(struct connman_iptables *table,
int builtin;
chain_head = find_chain_head(table, chain_name);
- if (chain_head == NULL)
+ if (!chain_head)
return NULL;
chain_tail = find_chain_tail(table, chain_name);
- if (chain_tail == NULL)
+ if (!chain_tail)
return NULL;
if (!xt_t && !matches)
return NULL;
entry_test = new_rule(ip, target_name, xt_t, xt_rm);
- if (entry_test == NULL)
+ if (!entry_test)
return NULL;
- if (xt_t != NULL)
+ if (xt_t)
xt_e_t = ipt_get_target(entry_test);
- if (matches != NULL)
+ if (matches)
xt_e_m = (struct xt_entry_match *)entry_test->elems;
entry = chain_head->data;
tmp = list->data;
tmp_e = tmp->entry;
- if (is_same_ipt_entry(entry_test, tmp_e) == FALSE)
+ if (!is_same_ipt_entry(entry_test, tmp_e))
continue;
- if (xt_t != NULL) {
+ if (xt_t) {
struct xt_entry_target *tmp_xt_e_t;
tmp_xt_e_t = ipt_get_target(tmp_e);
continue;
}
- if (matches != NULL) {
+ if (matches) {
struct xt_entry_match *tmp_xt_e_m;
tmp_xt_e_m = (struct xt_entry_match *)tmp_e->elems;
removed = 0;
chain_head = find_chain_head(table, chain_name);
- if (chain_head == NULL)
+ if (!chain_head)
return -EINVAL;
chain_tail = find_chain_tail(table, chain_name);
- if (chain_tail == NULL)
+ if (!chain_tail)
return -EINVAL;
list = find_existing_rule(table, ip, chain_name, target_name,
xt_t, matches, xt_rm);
- if (list == NULL)
+ if (!list)
return -EINVAL;
entry = chain_head->data;
}
entry = list->data;
- if (entry == NULL)
+ if (!entry)
return -EINVAL;
/* We have deleted a rule,
* all references should be bumped accordingly */
- if (list->next != NULL)
+ if (list->next)
update_targets_reference(table, list->next->data,
- list->data, TRUE);
+ list->data, true);
removed += remove_table_entry(table, entry);
}
chain_head = find_chain_head(table, chain_name);
- if (chain_head == NULL)
+ if (!chain_head)
return -EINVAL;
entry = chain_head->data;
return -EINVAL;
chain_tail = find_chain_tail(table, chain_name);
- if (chain_tail == NULL)
+ if (!chain_tail)
return -EINVAL;
entry = chain_tail->prev->data;
unsigned char *entry_index;
r = g_try_malloc0(sizeof(struct ipt_replace) + table->size);
- if (r == NULL)
+ if (!r)
return NULL;
memset(r, 0, sizeof(*r) + table->size);
r->counters = g_try_malloc0(sizeof(struct xt_counters)
* table->old_entries);
- if (r->counters == NULL) {
+ if (!r->counters) {
g_free(r);
return NULL;
}
if (strlen(ip->outiface))
DBG("\tout %s", ip->outiface);
- if (inet_ntop(AF_INET, &ip->src, ip_string, INET6_ADDRSTRLEN) != NULL &&
- inet_ntop(AF_INET, &ip->smsk,
- ip_mask, INET6_ADDRSTRLEN) != NULL)
+ if (inet_ntop(AF_INET, &ip->src, ip_string, INET6_ADDRSTRLEN) &&
+ inet_ntop(AF_INET, &ip->smsk, ip_mask,
+ INET6_ADDRSTRLEN))
DBG("\tsrc %s/%s", ip_string, ip_mask);
- if (inet_ntop(AF_INET, &ip->dst, ip_string, INET6_ADDRSTRLEN) != NULL &&
- inet_ntop(AF_INET, &ip->dmsk,
- ip_mask, INET6_ADDRSTRLEN) != NULL)
+ if (inet_ntop(AF_INET, &ip->dst, ip_string, INET6_ADDRSTRLEN) &&
+ inet_ntop(AF_INET, &ip->dmsk, ip_mask,
+ INET6_ADDRSTRLEN))
DBG("\tdst %s/%s", ip_string, ip_mask);
}
xt_t = xtables_find_target(IPT_STANDARD_TARGET,
XTF_LOAD_MUST_SUCCEED);
- if(xt_t->print != NULL)
+ if (xt_t->print)
xt_t->print(NULL, target, 1);
} else {
xt_t = xtables_find_target(target->u.user.name, XTF_TRY_LOAD);
- if (xt_t == NULL) {
+ if (!xt_t) {
DBG("\ttarget %s", target->u.user.name);
return;
}
- if(xt_t->print != NULL) {
+ if (xt_t->print) {
DBG("\ttarget ");
xt_t->print(NULL, target, 1);
}
return;
xt_m = xtables_find_match(match->u.user.name, XTF_TRY_LOAD, NULL);
- if (xt_m == NULL)
+ if (!xt_m)
goto out;
- if(xt_m->print != NULL) {
+ if (xt_m->print) {
DBG("\tmatch ");
xt_m->print(NULL, match, 1);
static int iptables_get_entries(struct connman_iptables *table)
{
socklen_t entry_size;
+ int err;
entry_size = sizeof(struct ipt_get_entries) + table->info->size;
- return getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_ENTRIES,
+ err = getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_ENTRIES,
table->blob_entries, &entry_size);
+ if (err < 0)
+ return -errno;
+
+ return 0;
}
static int iptables_replace(struct connman_iptables *table,
struct ipt_replace *r)
{
- return setsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_SET_REPLACE, r,
- sizeof(*r) + r->size);
+ int err;
+
+ err = setsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_SET_REPLACE, r,
+ sizeof(*r) + r->size);
+ if (err < 0)
+ return -errno;
+
+ return 0;
}
static int add_entry(struct ipt_entry *entry, int builtin, unsigned int hook,
struct ipt_entry *new_entry;
new_entry = g_try_malloc0(entry->next_offset);
- if (new_entry == NULL)
+ if (!new_entry)
return -ENOMEM;
memcpy(new_entry, entry, entry->next_offset);
GList *list;
struct connman_iptables_entry *entry;
- if (table == NULL)
+ if (!table)
return;
if (table->ipt_sock >= 0)
DBG("ip_tables module loading gives error but trying anyway");
module = g_strconcat("iptable_", table_name, NULL);
- if (module == NULL)
+ if (!module)
return NULL;
if (xtables_insmod(module, NULL, TRUE) != 0)
g_free(module);
table = g_try_new0(struct connman_iptables, 1);
- if (table == NULL)
+ if (!table)
return NULL;
table->info = g_try_new0(struct ipt_getinfo, 1);
- if (table->info == NULL)
+ if (!table->info)
goto err;
table->ipt_sock = socket(AF_INET, SOCK_RAW | SOCK_CLOEXEC, IPPROTO_RAW);
table->blob_entries = g_try_malloc0(sizeof(struct ipt_get_entries) +
table->info->size);
- if (table->blob_entries == NULL)
+ if (!table->blob_entries)
goto err;
g_stpcpy(table->blob_entries->name, table_name);
table->info->underflow, table->blob_entries->size,
add_entry, table);
- if (debug_enabled == TRUE)
+ if (debug_enabled)
dump_table(table);
return table;
const char *target_name)
{
struct xtables_target *xt_t = NULL;
- gboolean is_builtin, is_user_defined;
+ bool is_builtin, is_user_defined;
GList *chain_head = NULL;
size_t target_size;
- is_builtin = FALSE;
- is_user_defined = FALSE;
+ is_builtin = false;
+ is_user_defined = false;
if (is_builtin_target(target_name))
- is_builtin = TRUE;
+ is_builtin = true;
else {
chain_head = find_chain_head(table, target_name);
- if (chain_head != NULL && chain_head->next != NULL)
- is_user_defined = TRUE;
+ if (chain_head && chain_head->next)
+ is_user_defined = true;
}
if (is_builtin || is_user_defined)
else
xt_t = xtables_find_target(target_name, XTF_TRY_LOAD);
- if (xt_t == NULL)
+ if (!xt_t)
return NULL;
target_size = ALIGN(sizeof(struct ipt_entry_target)) + xt_t->size;
xt_t->t = g_try_malloc0(target_size);
- if (xt_t->t == NULL)
+ if (!xt_t->t)
return NULL;
xt_t->t->u.target_size = target_size;
target = (struct xt_standard_target *)(xt_t->t);
g_stpcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
- if (is_builtin == TRUE)
+ if (is_builtin)
target->verdict = target_to_verdict(target_name);
- else if (is_user_defined == TRUE) {
+ else if (is_user_defined) {
struct connman_iptables_entry *target_rule;
- if (chain_head == NULL) {
- g_free(xt_t->t);
- return NULL;
- }
-
target_rule = chain_head->next->data;
target->verdict = target_rule->offset;
}
} else {
g_stpcpy(xt_t->t->u.user.name, target_name);
xt_t->t->u.user.revision = xt_t->revision;
- if (xt_t->init != NULL)
+ if (xt_t->init)
xt_t->init(xt_t->t);
}
- if (xt_t->x6_options != NULL)
+ if (xt_t->x6_options)
iptables_globals.opts =
xtables_options_xfrm(
iptables_globals.orig_opts,
xt_t->extra_opts,
&xt_t->option_offset);
- if (iptables_globals.opts == NULL) {
+ if (!iptables_globals.opts) {
g_free(xt_t->t);
xt_t = NULL;
}
struct xtables_match *xt_m;
size_t match_size;
- if (match_name == NULL)
+ if (!match_name)
return NULL;
xt_m = xtables_find_match(match_name, XTF_LOAD_MUST_SUCCEED, xt_rm);
match_size = ALIGN(sizeof(struct ipt_entry_match)) + xt_m->size;
xt_m->m = g_try_malloc0(match_size);
- if (xt_m->m == NULL)
+ if (!xt_m->m)
return NULL;
xt_m->m->u.match_size = match_size;
g_stpcpy(xt_m->m->u.user.name, xt_m->name);
xt_m->m->u.user.revision = xt_m->revision;
- if (xt_m->init != NULL)
+ if (xt_m->init)
xt_m->init(xt_m->m);
- if (xt_m->x6_options != NULL)
+ if (xt_m->x6_options)
iptables_globals.opts =
xtables_options_xfrm(
iptables_globals.orig_opts,
xt_m->extra_opts,
&xt_m->option_offset);
- if (iptables_globals.opts == NULL) {
+ if (!iptables_globals.opts) {
g_free(xt_m->m);
if (xt_m == xt_m->next)
return xt_m;
}
-static int parse_ip_and_mask(const char *str, struct in_addr *ip, struct in_addr *mask)
+static int parse_ip_and_mask(const char *str, struct in_addr *ip,
+ struct in_addr *mask)
{
char **tokens;
uint32_t prefixlength;
int err;
tokens = g_strsplit(str, "/", 2);
- if (tokens == NULL)
+ if (!tokens)
return -1;
if (!inet_pton(AF_INET, tokens[0], ip)) {
goto out;
}
- if (tokens[1] != NULL) {
+ if (tokens[1]) {
prefixlength = strtol(tokens[1], NULL, 10);
if (prefixlength > 31) {
err = -1;
{
struct connman_iptables *table;
- if (table_name == NULL)
+ if (!table_name)
table_name = "filter";
table = g_hash_table_lookup(table_hash, table_name);
- if (table != NULL)
+ if (table)
return table;
table = iptables_init(table_name);
- if (table == NULL)
+ if (!table)
return NULL;
table->name = g_strdup(table_name);
/* Don't forget the last NULL entry */
ctx->argv = g_try_malloc0((ctx->argc + 1) * sizeof(char *));
- if (ctx->argv == NULL) {
+ if (!ctx->argv) {
g_strfreev(tokens);
return -ENOMEM;
}
return 0;
}
-static int parse_xt_modules(int c, connman_bool_t invert,
+static int parse_xt_modules(int c, bool invert,
struct parse_context *ctx)
{
struct xtables_match *m;
struct xtables_rule_match *rm;
- for (rm = ctx->xt_rm; rm != NULL; rm = rm->next) {
+ for (rm = ctx->xt_rm; rm; rm = rm->next) {
if (rm->completed != 0)
continue;
m = rm->match;
- if (m->x6_parse == NULL && m->parse == NULL)
+ if (!m->x6_parse && !m->parse)
continue;
if (c < (int) m->option_offset ||
xtables_option_mpcall(c, ctx->argv, invert, m, NULL);
}
- if (ctx->xt_t == NULL)
+ if (!ctx->xt_t)
return 0;
- if (ctx->xt_t->x6_parse == NULL && ctx->xt_t->parse == NULL)
+ if (!ctx->xt_t->x6_parse && !ctx->xt_t->parse)
return 0;
if (c < (int) ctx->xt_t->option_offset ||
{
struct xtables_rule_match *rm;
- for (rm = ctx->xt_rm; rm != NULL; rm = rm->next)
+ for (rm = ctx->xt_rm; rm; rm = rm->next)
xtables_option_mfcall(rm->match);
- if (ctx->xt_t != NULL)
+ if (ctx->xt_t)
xtables_option_tfcall(ctx->xt_t);
return 0;
* of libxtables is found.
*/
struct xtables_match *xt_m;
- connman_bool_t invert = FALSE;
+ bool invert = false;
int len, c, err;
ctx->ip = g_try_new0(struct ipt_ip, 1);
- if (ctx->ip == NULL)
+ if (!ctx->ip)
return -ENOMEM;
/*
case 'm':
/* Matches */
xt_m = prepare_matches(table, &ctx->xt_rm, optarg);
- if (xt_m == NULL) {
+ if (!xt_m) {
err = -EINVAL;
goto out;
}
case 'j':
/* Target */
ctx->xt_t = prepare_target(table, optarg);
- if (ctx->xt_t == NULL) {
+ if (!ctx->xt_t) {
err = -EINVAL;
goto out;
}
break;
case 1:
if (optarg[0] == '!' && optarg[1] == '\0') {
- invert = TRUE;
+ invert = true;
/* Remove the '!' from the optarg */
optarg[0] = '\0';
break;
}
- invert = FALSE;
+ invert = false;
}
err = final_check_xt_modules(ctx);
* Clear all flags because the flags are only valid
* for one rule.
*/
- for (xt_m = xtables_matches; xt_m != NULL; xt_m = xt_m->next)
+ for (xt_m = xtables_matches; xt_m; xt_m = xt_m->next)
xt_m->mflags = 0;
- for (xt_t = xtables_targets; xt_t != NULL; xt_t = xt_t->next) {
+ for (xt_t = xtables_targets; xt_t; xt_t = xt_t->next) {
xt_t->tflags = 0;
xt_t->used = 0;
}
g_strfreev(ctx->argv);
g_free(ctx->ip);
- if (ctx->xt_t != NULL) {
+ if (ctx->xt_t) {
g_free(ctx->xt_t->t);
ctx->xt_t->t = NULL;
}
- for (list = ctx->xt_m; list != NULL; list = list->next) {
+ for (list = ctx->xt_m; list; list = list->next) {
struct xtables_match *xt_m = list->data;
g_free(xt_m->m);
}
g_list_free(ctx->xt_m);
- for (tmp = NULL, rm = ctx->xt_rm; rm != NULL; rm = rm->next) {
- if (tmp != NULL)
+ for (tmp = NULL, rm = ctx->xt_rm; rm; rm = rm->next) {
+ if (tmp)
g_free(tmp);
tmp = rm;
}
DBG("-t %s -L", table_name);
table = get_table(table_name);
- if (table == NULL)
+ if (!table)
return -EINVAL;
dump_table(table);
DBG("-t %s -N %s", table_name, chain);
table = get_table(table_name);
- if (table == NULL)
+ if (!table)
return -EINVAL;
return iptables_add_chain(table, chain);
DBG("-t %s -X %s", table_name, chain);
table = get_table(table_name);
- if (table == NULL)
+ if (!table)
return -EINVAL;
return iptables_delete_chain(table, chain);
DBG("-t %s -F %s", table_name, chain);
table = get_table(table_name);
- if (table == NULL)
+ if (!table)
return -EINVAL;
return iptables_flush_chain(table, chain);
DBG("-t %s -F %s", table_name, chain);
table = get_table(table_name);
- if (table == NULL)
+ if (!table)
return -EINVAL;
return iptables_change_policy(table, chain, policy);
int err;
ctx = g_try_new0(struct parse_context, 1);
- if (ctx == NULL)
+ if (!ctx)
return -ENOMEM;
DBG("-t %s -A %s %s", table_name, chain, rule_spec);
goto out;
table = get_table(table_name);
- if (table == NULL) {
+ if (!table) {
err = -EINVAL;
goto out;
}
if (err < 0)
goto out;
- if (ctx->xt_t == NULL)
+ if (!ctx->xt_t)
target_name = NULL;
else
target_name = ctx->xt_t->name;
int err;
ctx = g_try_new0(struct parse_context, 1);
- if (ctx == NULL)
+ if (!ctx)
return -ENOMEM;
DBG("-t %s -I %s %s", table_name, chain, rule_spec);
goto out;
table = get_table(table_name);
- if (table == NULL) {
+ if (!table) {
err = -EINVAL;
goto out;
}
if (err < 0)
goto out;
- if (ctx->xt_t == NULL)
+ if (!ctx->xt_t)
target_name = NULL;
else
target_name = ctx->xt_t->name;
int err;
ctx = g_try_new0(struct parse_context, 1);
- if (ctx == NULL)
+ if (!ctx)
return -ENOMEM;
DBG("-t %s -D %s %s", table_name, chain, rule_spec);
goto out;
table = get_table(table_name);
- if (table == NULL) {
+ if (!table) {
err = -EINVAL;
goto out;
}
if (err < 0)
goto out;
- if (ctx->xt_t == NULL)
+ if (!ctx->xt_t)
target_name = NULL;
else
target_name = ctx->xt_t->name;
DBG("%s", table_name);
table = g_hash_table_lookup(table_hash, table_name);
- if (table == NULL)
+ if (!table)
return -EINVAL;
repl = iptables_blob(table);
- if (debug_enabled == TRUE)
+ if (debug_enabled)
dump_ipt_replace(repl);
err = iptables_replace(table, repl);
g_free(repl);
if (err < 0)
- return err;
+ return err;
g_hash_table_remove(table_hash, table_name);
target = ipt_get_target(entry);
if (!g_strcmp0(target->u.user.name, IPT_ERROR_TARGET))
- (*cb)((const char*)target->data, cbd->user_data);
+ (*cb)((const char *)target->data, cbd->user_data);
else if (builtin >= 0)
(*cb)(hooknames[builtin], cbd->user_data);
struct connman_iptables *table;
table = get_table(table_name);
- if (table == NULL)
+ if (!table)
return -EINVAL;
iterate_entries(table->blob_entries->entrytable,
DBG("");
if (getenv("CONNMAN_IPTABLES_DEBUG"))
- debug_enabled = TRUE;
+ debug_enabled = true;
table_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
NULL, remove_table);