Separate monitoring function plugin
[platform/core/connectivity/stc-manager.git] / src / stc-firewall.c
old mode 100644 (file)
new mode 100755 (executable)
index c18f5da..64e7abb
@@ -207,8 +207,10 @@ static void __fw_rule_make_key(firewall_rule_s *rule,
        info->identifier = g_strdup(rule->identifier);
        info->key = rule->key;
 
-       STC_LOGD("Identifier [%s]", rule->identifier);
-       STC_LOGD("Key [%u]", rule->key);
+       if (STC_DEBUG_LOG && STC_FW_LOG) {
+               STC_LOGD("Identifier [%s]", rule->identifier);
+               STC_LOGD("Key [%u]", rule->key);
+       }
 }
 
 static void __fw_rule_free(void *data)
@@ -257,13 +259,15 @@ static stc_error_e __fw_chain_add(const char *chain)
 
        lookup = g_hash_table_lookup(g_firewalls, chain);
        if (lookup) {
-               STC_LOGD("chain already present");
+               if (STC_DEBUG_LOG && STC_FW_LOG)
+                       STC_LOGD("chain already present");
                return STC_ERROR_ALREADY_DATA;
        }
 
        data = MALLOC0(stc_fw_data_s, 1);
        if (!data) {
-               STC_LOGE("data allocation failed");
+               if (STC_DEBUG_LOG && STC_FW_LOG)
+                       STC_LOGE("data allocation failed");
                return STC_ERROR_OUT_OF_MEMORY;
        }
 
@@ -286,12 +290,14 @@ static stc_error_e __fw_chain_remove(const char *chain)
 
        lookup = g_hash_table_lookup(g_firewalls, chain);
        if (!lookup) {
-               STC_LOGE("chain not found");
+               if (STC_DEBUG_LOG && STC_FW_LOG)
+                       STC_LOGE("chain not found");
                return STC_ERROR_NO_DATA;
        }
 
        if (lookup->target != STC_FW_CHAIN_TARGET_NONE) {
-               STC_LOGE("can't be applied bcz chain is set");
+               if (STC_DEBUG_LOG && STC_FW_LOG)
+                       STC_LOGE("can't be applied bcz chain is set");
                return STC_ERROR_INVALID_PARAMETER;
        }
 
@@ -313,12 +319,14 @@ static stc_error_e __fw_chain_flush(const char *chain)
 
        lookup = g_hash_table_lookup(g_firewalls, chain);
        if (!lookup) {
-               STC_LOGE("chain not found");
+               if (STC_DEBUG_LOG && STC_FW_LOG)
+                       STC_LOGE("chain not found");
                return STC_ERROR_NO_DATA;
        }
 
        if (lookup->target != STC_FW_CHAIN_TARGET_NONE) {
-               STC_LOGE("can't be applied bcz chain is set");
+               if (STC_DEBUG_LOG && STC_FW_LOG)
+                       STC_LOGE("can't be applied bcz chain is set");
                return STC_ERROR_INVALID_PARAMETER;
        }
 
@@ -338,7 +346,8 @@ static stc_error_e __fw_chain_set(const char *chain, stc_fw_data_s value)
 
        lookup = g_hash_table_lookup(g_firewalls, chain);
        if (!lookup) {
-               STC_LOGE("chain not found");
+               if (STC_DEBUG_LOG && STC_FW_LOG)
+                       STC_LOGE("chain not found");
                return STC_ERROR_NO_DATA;
        }
 
@@ -357,7 +366,8 @@ static stc_fw_data_s *__fw_chain_get(const char *chain)
 
        lookup = g_hash_table_lookup(g_firewalls, chain);
        if (!lookup) {
-               STC_LOGE("chain not found");
+               if (STC_DEBUG_LOG && STC_FW_LOG)
+                       STC_LOGE("chain not found");
                return NULL;
        }
 
@@ -375,7 +385,8 @@ static stc_error_e __fw_chain_unset(const char *chain)
 
        lookup = g_hash_table_lookup(g_firewalls, chain);
        if (!lookup) {
-               STC_LOGE("chain not found");
+               if (STC_DEBUG_LOG && STC_FW_LOG)
+                       STC_LOGE("chain not found");
                return STC_ERROR_NO_DATA;
        }
 
@@ -451,6 +462,7 @@ static void __fw_rule_make_params(gpointer data, gpointer user_data)
 
                switch (rule->s_ip_type) {
                case STC_FW_IP_RANGE:
+                       /* fall through */
                case STC_FW_IP_MASK:
                        addr = g_try_malloc0(INET_ADDRSTRLEN);
                        if (addr) {
@@ -459,6 +471,7 @@ static void __fw_rule_make_params(gpointer data, gpointer user_data)
                                                        g_variant_new_string(addr));
                                FREE(addr);
                        }
+                       /* fall through */
                case STC_FW_IP_SINGLE:
                        addr = g_try_malloc0(INET_ADDRSTRLEN);
                        if (addr) {
@@ -474,6 +487,7 @@ static void __fw_rule_make_params(gpointer data, gpointer user_data)
 
                switch (rule->d_ip_type) {
                case STC_FW_IP_RANGE:
+                       /* fall through */
                case STC_FW_IP_MASK:
                        addr = g_try_malloc0(INET_ADDRSTRLEN);
                        if (addr) {
@@ -482,6 +496,7 @@ static void __fw_rule_make_params(gpointer data, gpointer user_data)
                                                        g_variant_new_string(addr));
                                FREE(addr);
                        }
+                       /* fall through */
                case STC_FW_IP_SINGLE:
                        addr = g_try_malloc0(INET_ADDRSTRLEN);
                        if (addr) {
@@ -499,6 +514,7 @@ static void __fw_rule_make_params(gpointer data, gpointer user_data)
 
                switch (rule->s_ip_type) {
                case STC_FW_IP_RANGE:
+                       /* fall through */
                case STC_FW_IP_MASK:
                        addr = g_try_malloc0(INET6_ADDRSTRLEN);
                        if (addr) {
@@ -507,6 +523,7 @@ static void __fw_rule_make_params(gpointer data, gpointer user_data)
                                                        g_variant_new_string(addr));
                                FREE(addr);
                        }
+                       /* fall through */
                case STC_FW_IP_SINGLE:
                        addr = g_try_malloc0(INET6_ADDRSTRLEN);
                        if (addr) {
@@ -522,6 +539,7 @@ static void __fw_rule_make_params(gpointer data, gpointer user_data)
 
                switch (rule->d_ip_type) {
                case STC_FW_IP_RANGE:
+                       /* fall through */
                case STC_FW_IP_MASK:
                        addr = g_try_malloc0(INET6_ADDRSTRLEN);
                        if (addr) {
@@ -530,6 +548,7 @@ static void __fw_rule_make_params(gpointer data, gpointer user_data)
                                                        g_variant_new_string(addr));
                                FREE(addr);
                        }
+                       /* fall through */
                case STC_FW_IP_SINGLE:
                        addr = g_try_malloc0(INET6_ADDRSTRLEN);
                        if (addr) {
@@ -629,7 +648,7 @@ static void __fw_rule_set_to_chain(gpointer data, gpointer user_data)
                break;
        }
 
-       firewall_rule_add(rule);
+       firewall_rule_append(rule);
 }
 
 static void __fw_rule_print_rules(gpointer data, gpointer user_data)
@@ -654,8 +673,8 @@ static void __fw_rule_print_rules(gpointer data, gpointer user_data)
                        rule->d_ip_type, rule->d_ip1.Ipv4.s_addr, rule->d_ip2.Ipv4.s_addr);
                break;
        case STC_FW_FAMILY_V6:
-               STC_LOGD("[%d][%d][%08x:%08x:%08x:%08x][%08x:%08x:%08x:%08x]"
-                       "[%d][%08x:%08x:%08x:%08x][%08x:%08x:%08x:%08x]",
+               STC_LOGD("[%d][%d][%08x:%08x:%08x:%08x]"
+                       "[%d][%08x:%08x:%08x:%08x]",
                        rule->family,
                        rule->s_ip_type,
                        rule->s_ip1.Ipv6.s6_addr32[0], rule->s_ip1.Ipv6.s6_addr32[1],
@@ -739,7 +758,8 @@ stc_cb_ret_e __fw_table_chain_info_cb(const firewall_chain_s *info,
 
        data = MALLOC0(stc_fw_data_s, 1);
        if (!data) {
-               STC_LOGE("data allocation failed");
+               if (STC_DEBUG_LOG && STC_FW_LOG)
+                       STC_LOGE("data allocation failed");
                return STC_CONTINUE;
        }
 
@@ -760,13 +780,15 @@ stc_cb_ret_e __fw_table_rule_info_cb(const firewall_rule_s *info,
 
        lookup = g_hash_table_lookup(g_firewalls, info->chain);
        if (!lookup) {
-               STC_LOGE("chain not found");
+               if (STC_DEBUG_LOG && STC_FW_LOG)
+                       STC_LOGE("chain not found");
                return STC_CONTINUE;
        }
 
        rule = MALLOC0(firewall_rule_s, 1);
        if (!rule) {
-               STC_LOGE("rule allocation failed");
+               if (STC_DEBUG_LOG && STC_FW_LOG)
+                       STC_LOGE("rule allocation failed");
                return STC_CONTINUE;
        }
 
@@ -790,18 +812,21 @@ static stc_error_e __fw_rule_add(firewall_rule_s *info)
 
        lookup = g_hash_table_lookup(g_firewalls, info->chain);
        if (!lookup) {
-               STC_LOGE("chain not found");
+               if (STC_DEBUG_LOG && STC_FW_LOG)
+                       STC_LOGE("chain not found");
                return STC_ERROR_NO_DATA;
        }
 
        if (lookup->target != STC_FW_CHAIN_TARGET_NONE) {
-               STC_LOGE("can't be applied bcz chain is set");
+               if (STC_DEBUG_LOG && STC_FW_LOG)
+                       STC_LOGE("can't be applied bcz chain is set");
                return STC_ERROR_INVALID_PARAMETER;
        }
 
        rule = MALLOC0(firewall_rule_s, 1);
        if (!rule) {
-               STC_LOGE("rule allocation failed");
+               if (STC_DEBUG_LOG && STC_FW_LOG)
+                       STC_LOGE("rule allocation failed");
                return STC_ERROR_OUT_OF_MEMORY;
        }
 
@@ -811,7 +836,8 @@ static stc_error_e __fw_rule_add(firewall_rule_s *info)
 
        comp = g_slist_find_custom(lookup->rules, rule, __fw_rule_comp);
        if (comp) {
-               STC_LOGD("rule already present");
+               if (STC_DEBUG_LOG && STC_FW_LOG)
+                       STC_LOGD("rule already present");
                __fw_rule_free(rule);
                return STC_ERROR_ALREADY_DATA;
        }
@@ -834,19 +860,22 @@ static stc_error_e __fw_rule_remove(const firewall_rule_s *info)
 
        lookup = g_hash_table_lookup(g_firewalls, info->chain);
        if (!lookup) {
-               STC_LOGE("chain not found");
+               if (STC_DEBUG_LOG && STC_FW_LOG)
+                       STC_LOGE("chain not found");
                return STC_ERROR_NO_DATA;
        }
 
        if (lookup->target != STC_FW_CHAIN_TARGET_NONE) {
-               STC_LOGE("can't be applied bcz chain is set");
+               if (STC_DEBUG_LOG && STC_FW_LOG)
+                       STC_LOGE("can't be applied bcz chain is set");
                return STC_ERROR_INVALID_PARAMETER;
        }
 
        rule_list = lookup->rules;
        comp = g_slist_find_custom(rule_list, info, __fw_rule_comp);
        if (!comp) {
-               STC_LOGD("rule not found");
+               if (STC_DEBUG_LOG && STC_FW_LOG)
+                       STC_LOGD("rule not found");
                return STC_ERROR_NO_DATA;
        }
 
@@ -871,19 +900,22 @@ static stc_error_e __fw_rule_update(firewall_rule_s *info)
 
        lookup = g_hash_table_lookup(g_firewalls, info->chain);
        if (!lookup) {
-               STC_LOGE("chain not found");
+               if (STC_DEBUG_LOG && STC_FW_LOG)
+                       STC_LOGE("chain not found");
                return STC_ERROR_NO_DATA;
        }
 
        if (lookup->target != STC_FW_CHAIN_TARGET_NONE) {
-               STC_LOGE("can't be applied bcz chain is set");
+               if (STC_DEBUG_LOG && STC_FW_LOG)
+                       STC_LOGE("can't be applied bcz chain is set");
                return STC_ERROR_INVALID_PARAMETER;
        }
 
        rule_list = lookup->rules;
        comp = g_slist_find_custom(rule_list, info, __fw_rule_comp);
        if (!comp) {
-               STC_LOGD("rule not found");
+               if (STC_DEBUG_LOG && STC_FW_LOG)
+                       STC_LOGD("rule not found");
                return STC_ERROR_NO_DATA;
        }
 
@@ -891,7 +923,8 @@ static stc_error_e __fw_rule_update(firewall_rule_s *info)
 
        update_rule = MALLOC0(firewall_rule_s, 1);
        if (!update_rule) {
-               STC_LOGE("rule allocation failed");
+               if (STC_DEBUG_LOG && STC_FW_LOG)
+                       STC_LOGE("rule allocation failed");
                return STC_ERROR_OUT_OF_MEMORY;
        }
 
@@ -901,7 +934,8 @@ static stc_error_e __fw_rule_update(firewall_rule_s *info)
 
        comp = g_slist_find_custom(lookup->rules, update_rule, __fw_rule_comp);
        if (comp) {
-               STC_LOGD("rule already present");
+               if (STC_DEBUG_LOG && STC_FW_LOG)
+                       STC_LOGD("rule already present");
                __fw_rule_free(update_rule);
                return STC_ERROR_ALREADY_DATA;
        }
@@ -1030,7 +1064,7 @@ static void __fw_rule_extract(const char *key, GVariant *value,
                }
 
        } else if (g_strcmp0(key, RULE_DPORT1) == 0) {
-               if (rule->s_port_type != STC_FW_PORT_NONE) {
+               if (rule->d_port_type != STC_FW_PORT_NONE) {
                        rule->d_port1 = g_variant_get_uint32(value);
                        STC_LOGD("%s: [%04x]", RULE_DPORT1, rule->d_port1);
                }
@@ -1157,6 +1191,11 @@ gboolean __validate_fw_rule(firewall_rule_s *rule)
                return FALSE;
        }
 
+       if (rule->target >= STC_FW_RULE_TARGET_MAX) {
+               __STC_LOG_FUNC_EXIT__;
+               return FALSE;
+       }
+
        if (rule->target == STC_FW_RULE_TARGET_LOG &&
                (rule->log_prefix == NULL ||
                rule->log_prefix[0] == '\0')) {
@@ -1177,6 +1216,8 @@ gboolean __validate_fw_rule(firewall_rule_s *rule)
 
 void stc_firewall_init(void)
 {
+       __STC_LOG_FUNC_ENTER__;
+
        int ret = STC_ERROR_NONE;
 
        g_firewalls = g_hash_table_new_full(g_str_hash,
@@ -1191,21 +1232,31 @@ void stc_firewall_init(void)
 
        table_firewall_foreach_chain(__fw_table_chain_info_cb, NULL);
        table_firewall_foreach_rule(__fw_table_rule_info_cb, NULL);
+
+       __STC_LOG_FUNC_EXIT__;
 }
 
-void stc_firewall_update(void)
+API void stc_firewall_update(void)
 {
+       __STC_LOG_FUNC_ENTER__;
+
        __fw_chain_foreach(__fw_foreach_to_add_chain, NULL);
        __fw_chain_foreach(__fw_foreach_to_set_rule_to_chain, NULL);
        __fw_chain_foreach(__fw_foreach_to_set_chain, NULL);
+
+       __STC_LOG_FUNC_EXIT__;
 }
 
 void stc_firewall_deinit(void)
 {
+       __STC_LOG_FUNC_ENTER__;
+
        if (g_firewalls) {
                g_hash_table_destroy(g_firewalls);
                g_firewalls = NULL;
        }
+
+       __STC_LOG_FUNC_EXIT__;
 }
 
 gboolean handle_firewall_lock(StcFirewall *object,
@@ -1505,7 +1556,8 @@ gboolean handle_firewall_unset_chain(StcFirewall *object,
        }
 
        if (lookup->target == STC_FW_CHAIN_TARGET_NONE) {
-               STC_LOGE("chain is not set");
+               if (STC_DEBUG_LOG && STC_FW_LOG)
+                       STC_LOGE("chain is not set");
                STC_FIREWALL_DBUS_REPLY_ERROR(invocation,
                                                STC_ERROR_INVALID_PARAMETER);
                __STC_LOG_FUNC_EXIT__;
@@ -1562,7 +1614,8 @@ gboolean handle_firewall_add_rule(StcFirewall *object,
 
        rule = MALLOC0(firewall_rule_s, 1);
        if (!rule) {
-               STC_LOGE("rule allocation failed");
+               if (STC_DEBUG_LOG && STC_FW_LOG)
+                       STC_LOGE("rule allocation failed");
                return STC_ERROR_OUT_OF_MEMORY;
        }
 
@@ -1614,7 +1667,8 @@ gboolean handle_firewall_remove_rule(StcFirewall *object,
 
        rule = MALLOC0(firewall_rule_s, 1);
        if (!rule) {
-               STC_LOGE("rule allocation failed");
+               if (STC_DEBUG_LOG && STC_FW_LOG)
+                       STC_LOGE("rule allocation failed");
                return STC_ERROR_OUT_OF_MEMORY;
        }
 
@@ -1667,7 +1721,8 @@ gboolean handle_firewall_update_rule(StcFirewall *object,
 
        rule = MALLOC0(firewall_rule_s, 1);
        if (!rule) {
-               STC_LOGE("rule allocation failed");
+               if (STC_DEBUG_LOG && STC_FW_LOG)
+                       STC_LOGE("rule allocation failed");
                return STC_ERROR_OUT_OF_MEMORY;
        }