X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=src%2Fmonitor%2Fstc-monitor.c;h=8037c53445cbd1ab91002d59368d4119c79632dd;hb=a72b6122101219d7acd0ab83b7c74a90d3599596;hp=a0a58ffbb1996b22224c8d48980b15fd25b86334;hpb=d7d0c6b61fc56bd65a385606c92d1b16e067f4f8;p=platform%2Fcore%2Fconnectivity%2Fstc-manager.git diff --git a/src/monitor/stc-monitor.c b/src/monitor/stc-monitor.c index a0a58ff..8037c53 100755 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -15,16 +15,42 @@ */ #include +#include +#include #include "stc-default-connection.h" #include "helper-nl.h" #include "helper-nfacct-rule.h" #include "helper-net-cls.h" #include "helper-cgroup.h" +#include "helper-iptables.h" #include "counter.h" #include "table-statistics.h" #include "table-counters.h" #include "stc-monitor.h" +#include "stc-time.h" +#include "stc-manager-plugin-appstatus.h" +#include "stc-manager-plugin-exception.h" +#include "stc-manager-plugin-tether.h" + +#define GRANULARITY 10 +#define MAX_INT_LENGTH 128 + +#ifndef VCONFKEY_STC_BACKGROUND_STATE +#define VCONFKEY_STC_BACKGROUND_STATE "db/stc/background_state" +#endif + +#ifndef VCONFKEY_SETAPPL_DATA_RESTRICTION_INT +#define VCONFKEY_SETAPPL_DATA_RESTRICTION_INT "db/setting/data_restriction" +#endif + +typedef struct { + time_t now; + time_t month_start_ts; + time_t week_start_ts; + time_t day_start_ts; + int is_updated; +} reset_time_limits_context_s; typedef struct { stc_app_key_s *app_key; @@ -35,48 +61,269 @@ typedef struct { typedef struct { struct nfacct_rule *counter; int64_t bytes; - gboolean in_limit_reached; - gboolean out_limit_reached; + gboolean data_limit_exceeded; } classid_bytes_context_s; static stc_system_s *g_system = NULL; +//LCOV_EXCL_START +static int __vconf_get_int(const char *key, int *value) +{ + int ret = 0; + + ret = vconf_get_int(key, value); + if (ret != VCONF_OK) { + STC_LOGE("Failed to get vconfkey [%s] value", key); //LCOV_EXCL_LINE + return -1; //LCOV_EXCL_LINE + } + + return 0; +} + +static int __vconf_set_int(const char *key, int value) +{ + int ret = 0; + + ret = vconf_set_int(key, value); + if (ret != VCONF_OK) { + STC_LOGE("Failed to set vconfkey [%s] value", key); //LCOV_EXCL_LINE + return -1; //LCOV_EXCL_LINE + } + + return 0; +} +//LCOV_EXCL_STOP + static nfacct_rule_jump __get_jump_by_intend(struct nfacct_rule *counter) { if (counter->intend == NFACCT_WARN) return NFACCT_JUMP_ACCEPT; else if (counter->intend == NFACCT_BLOCK) return NFACCT_JUMP_REJECT; + else if (counter->intend == NFACCT_ALLOW) + return NFACCT_JUMP_ACCEPT; + else if (counter->intend == NFACCT_TETH_BLOCK) + return NFACCT_JUMP_REJECT; + else if (counter->intend == NFACCT_TETH_ALLOW) + return NFACCT_JUMP_ACCEPT; return NFACCT_JUMP_UNKNOWN; } +static stc_error_e __add_iptables_tether_in(struct nfacct_rule *counter, + const gchar *ipaddr) +{ + int ret; + + if (counter == NULL || ipaddr == NULL) + return STC_ERROR_INVALID_PARAMETER; + + counter->action = NFACCT_ACTION_INSERT; + counter->iotype = NFACCT_COUNTER_IN; + counter->jump = __get_jump_by_intend(counter); + counter->iptype = NFACCT_TYPE_IPV4; + counter->send_limit = 0; + counter->rcv_limit = 0; + counter->src_iprange_type = NFACCT_IPRANGE_TYPE_SINGLE; + counter->src_ip1 = g_strdup(ipaddr); + + ret = produce_net_rule(counter); + + FREE(counter->src_ip1); + counter->src_iprange_type = NFACCT_IPRANGE_TYPE_NONE; + return ret; +} + +static stc_error_e __add_iptables_tether_out(struct nfacct_rule *counter, + const gchar *ipaddr) +{ + int ret; + + if (counter == NULL || ipaddr == NULL) + return STC_ERROR_INVALID_PARAMETER; + + counter->action = NFACCT_ACTION_INSERT; + counter->iotype = NFACCT_COUNTER_OUT; + counter->jump = __get_jump_by_intend(counter); + counter->iptype = NFACCT_TYPE_IPV4; + counter->send_limit = 0; + counter->rcv_limit = 0; + counter->dst_iprange_type = NFACCT_IPRANGE_TYPE_SINGLE; + counter->dst_ip1 = g_strdup(ipaddr); + + ret = produce_net_rule(counter); + + FREE(counter->dst_ip1); + counter->dst_iprange_type = NFACCT_IPRANGE_TYPE_NONE; + return ret; +} + +static stc_error_e __del_iptables_tether_in(struct nfacct_rule *counter, + const gchar *ipaddr) +{ + int ret; + + if (counter == NULL || ipaddr == NULL) + return STC_ERROR_INVALID_PARAMETER; + + counter->action = NFACCT_ACTION_DELETE; + counter->iotype = NFACCT_COUNTER_IN; + counter->jump = __get_jump_by_intend(counter); + counter->iptype = NFACCT_TYPE_IPV4; + counter->send_limit = 0; + counter->rcv_limit = 0; + counter->src_iprange_type = NFACCT_IPRANGE_TYPE_SINGLE; + counter->src_ip1 = g_strdup(ipaddr); + + ret = produce_net_rule(counter); + + FREE(counter->src_ip1); + counter->src_iprange_type = NFACCT_IPRANGE_TYPE_NONE; + return ret; +} + +static stc_error_e __del_iptables_tether_out(struct nfacct_rule *counter, + const gchar *ipaddr) +{ + int ret; + + if (counter == NULL || ipaddr == NULL) + return STC_ERROR_INVALID_PARAMETER; + + counter->action = NFACCT_ACTION_DELETE; + counter->iotype = NFACCT_COUNTER_OUT; + counter->jump = __get_jump_by_intend(counter); + counter->iptype = NFACCT_TYPE_IPV4; + counter->send_limit = 0; + counter->rcv_limit = 0; + counter->dst_iprange_type = NFACCT_IPRANGE_TYPE_SINGLE; + counter->dst_ip1 = g_strdup(ipaddr); + + ret = produce_net_rule(counter); + + FREE(counter->dst_ip1); + counter->dst_iprange_type = NFACCT_IPRANGE_TYPE_NONE; + return ret; +} + static stc_error_e __add_iptables_in(struct nfacct_rule *counter) { - return produce_net_rule(counter, 0, 0, - NFACCT_ACTION_INSERT, __get_jump_by_intend(counter), - NFACCT_COUNTER_IN); + if (counter == NULL) + return STC_ERROR_INVALID_PARAMETER; + + counter->action = NFACCT_ACTION_INSERT; + counter->iotype = NFACCT_COUNTER_IN; + counter->jump = __get_jump_by_intend(counter); + counter->iptype = NFACCT_TYPE_IPV4; + counter->send_limit = 0; + counter->rcv_limit = 0; + + return produce_net_rule(counter); } static stc_error_e __add_iptables_out(struct nfacct_rule *counter) { - return produce_net_rule(counter, 0, 0, - NFACCT_ACTION_INSERT, __get_jump_by_intend(counter), - NFACCT_COUNTER_OUT); + if (counter == NULL) + return STC_ERROR_INVALID_PARAMETER; + + counter->action = NFACCT_ACTION_INSERT; + counter->iotype = NFACCT_COUNTER_OUT; + counter->jump = __get_jump_by_intend(counter); + counter->iptype = NFACCT_TYPE_IPV4; + counter->send_limit = 0; + counter->rcv_limit = 0; + + return produce_net_rule(counter); } static stc_error_e __del_iptables_in(struct nfacct_rule *counter) { - return produce_net_rule(counter, 0, 0, - NFACCT_ACTION_DELETE, __get_jump_by_intend(counter), - NFACCT_COUNTER_IN); + if (counter == NULL) + return STC_ERROR_INVALID_PARAMETER; + + counter->action = NFACCT_ACTION_DELETE; + counter->iotype = NFACCT_COUNTER_IN; + counter->jump = __get_jump_by_intend(counter); + counter->iptype = NFACCT_TYPE_IPV4; + counter->send_limit = 0; + counter->rcv_limit = 0; + + return produce_net_rule(counter); } static stc_error_e __del_iptables_out(struct nfacct_rule *counter) { - return produce_net_rule(counter, 0, 0, - NFACCT_ACTION_DELETE, __get_jump_by_intend(counter), - NFACCT_COUNTER_OUT); + if (counter == NULL) + return STC_ERROR_INVALID_PARAMETER; + + counter->action = NFACCT_ACTION_DELETE; + counter->iotype = NFACCT_COUNTER_OUT; + counter->jump = __get_jump_by_intend(counter); + counter->iptype = NFACCT_TYPE_IPV4; + counter->send_limit = 0; + counter->rcv_limit = 0; + + return produce_net_rule(counter); +} + +static stc_error_e __add_ip6tables_in(struct nfacct_rule *counter) +{ + if (counter == NULL) + return STC_ERROR_INVALID_PARAMETER; + + counter->action = NFACCT_ACTION_INSERT; + counter->iotype = NFACCT_COUNTER_IN; + counter->jump = __get_jump_by_intend(counter); + counter->iptype = NFACCT_TYPE_IPV6; + counter->send_limit = 0; + counter->rcv_limit = 0; + + return produce_net_rule(counter); +} + +static stc_error_e __add_ip6tables_out(struct nfacct_rule *counter) +{ + if (counter == NULL) + return STC_ERROR_INVALID_PARAMETER; + + counter->action = NFACCT_ACTION_INSERT; + counter->iotype = NFACCT_COUNTER_OUT; + counter->jump = __get_jump_by_intend(counter); + counter->iptype = NFACCT_TYPE_IPV6; + counter->send_limit = 0; + counter->rcv_limit = 0; + + return produce_net_rule(counter); +} + +static stc_error_e __del_ip6tables_in(struct nfacct_rule *counter) +{ + if (counter == NULL) + return STC_ERROR_INVALID_PARAMETER; + + counter->action = NFACCT_ACTION_DELETE; + counter->iotype = NFACCT_COUNTER_IN; + counter->jump = __get_jump_by_intend(counter); + counter->iptype = NFACCT_TYPE_IPV6; + counter->send_limit = 0; + counter->rcv_limit = 0; + + return produce_net_rule(counter); +} + +static stc_error_e __del_ip6tables_out(struct nfacct_rule *counter) +{ + if (counter == NULL) + return STC_ERROR_INVALID_PARAMETER; + + counter->action = NFACCT_ACTION_DELETE; + counter->iotype = NFACCT_COUNTER_OUT; + counter->jump = __get_jump_by_intend(counter); + counter->iptype = NFACCT_TYPE_IPV6; + counter->send_limit = 0; + counter->rcv_limit = 0; + + return produce_net_rule(counter); } static int __processes_tree_key_compare(gconstpointer a, gconstpointer b, @@ -150,7 +397,7 @@ static int __rstns_tree_key_compare(gconstpointer a, gconstpointer b, if (ret != 0) return ret; - ret = g_strcmp0(key_a->imsi, key_b->imsi); + ret = g_strcmp0(key_a->subscriber_id, key_b->subscriber_id); if (ret != 0) return ret; @@ -158,6 +405,10 @@ static int __rstns_tree_key_compare(gconstpointer a, gconstpointer b, if (ret != 0) return ret; + ret = key_a->roaming - key_b->roaming; + if (ret != 0) + return ret; + return 0; } @@ -174,17 +425,19 @@ static void __rstns_tree_key_free(gpointer data) FREE(key->app_id); FREE(key->ifname); - FREE(key->imsi); + FREE(key->subscriber_id); FREE(key); } +/* +//LCOV_EXCL_START static gboolean __processes_tree_foreach_print(gpointer key, gpointer value, gpointer data) { stc_process_key_s *proc_key = (stc_process_key_s *)key; stc_process_value_s *proc_value = (stc_process_value_s *)value; - STC_LOGD("Process entry => PID [%d], Ground state [%d]", + STC_LOGD("Process entry => PID [\033[1;33m%d\033[0;m], Ground state [%d]", proc_key->pid, proc_value->ground); return FALSE; } @@ -200,8 +453,8 @@ static gboolean __apps_tree_foreach_print(gpointer key, gpointer value, stc_app_key_s *app_key = (stc_app_key_s *)key; stc_app_value_s *app_value = (stc_app_value_s *)value; - STC_LOGD("Application info => Pkg ID [%s], App ID [%s]," - " Type [%d], classid [%d]," + STC_LOGD("Application info => Pkg ID [\033[0;34m%s\033[0;m], " + "App ID [\033[0;32m%s\033[0;m], Type [%d], classid [%d]," " counter [ in (%lld), out (%lld)]", app_key->pkg_id, app_key->app_id, app_value->type, app_value->classid, @@ -211,12 +464,12 @@ static gboolean __apps_tree_foreach_print(gpointer key, gpointer value, return FALSE; } -#if 0 static void __apps_tree_printall(void) { g_tree_foreach(g_system->apps, __apps_tree_foreach_print, NULL); } -#endif +//LCOV_EXCL_STOP +*/ static gboolean __apps_tree_foreach_remove_pid(gpointer key, gpointer value, gpointer data) @@ -224,10 +477,8 @@ static gboolean __apps_tree_foreach_remove_pid(gpointer key, gpointer value, remove_pid_context_s *context = (remove_pid_context_s *)data; stc_app_value_s *app_value = (stc_app_value_s *)value; - if (!g_tree_remove(app_value->processes, context->proc_key)) { - STC_LOGD("key not found"); + if (!g_tree_remove(app_value->processes, context->proc_key)) return FALSE; - } context->entry_removed = TRUE; context->app_key = (stc_app_key_s *)key; @@ -259,6 +510,7 @@ static stc_process_value_s * __process_lookup(GTree *processes, return lookup; } +//LCOV_EXCL_START static gboolean __processes_tree_check_empty(gpointer key, gpointer value, gpointer data) { @@ -266,20 +518,131 @@ static gboolean __processes_tree_check_empty(gpointer key, gpointer value, (*pid_count)++; return TRUE; } +//LCOV_EXCL_STOP + +static gboolean __add_application_monitor_for_tethering(gpointer key, + gpointer value, gpointer data) +{ + stc_app_value_s *app_value = (stc_app_value_s *)value; + stc_app_key_s *app_key = (stc_app_key_s *)key; + default_connection_s *connection = (default_connection_s *)data; + stc_s *stc = stc_get_manager(); + struct nfacct_rule counter; + char *ipaddr = NULL; + int ret; + + STC_LOGI("add appid(%s) classid(%d)", app_key->app_id, + app_value->classid); + + if (stc == NULL || connection == NULL) + return FALSE; + + if (!stc->carg) { + stc->carg = MALLOC0(counter_arg_s, 1); + if (stc->carg == NULL) + return FALSE; + + stc->carg->sock = stc_monitor_get_counter_socket(); + } + + memset(&counter, 0, sizeof(struct nfacct_rule)); + + counter.carg = stc->carg; + counter.classid = app_value->classid; + counter.intend = NFACCT_TETH_COUNTER; + + if (connection->tether_state != TRUE || + connection->tether_iface.ifname == NULL) + return FALSE; + + counter.iftype = connection->tether_iface.type; + g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH); + + /* get the ip address of the station based on its mac address */ + ret = stc_plugin_tether_get_station_ip(app_value->mac, &ipaddr); + if (ret != STC_ERROR_NONE) + return FALSE; + + /* tethering iptables rule */ + __add_iptables_tether_in(&counter, ipaddr); + __add_iptables_tether_out(&counter, ipaddr); + + g_free(ipaddr); + return FALSE; +} + +static gboolean __remove_application_monitor_for_tethering(gpointer key, gpointer value, + gpointer data) +{ + stc_app_value_s *app_value = (stc_app_value_s *)value; + stc_app_key_s *app_key = (stc_app_key_s *)key; + default_connection_s *connection = (default_connection_s *)data; + stc_s *stc = stc_get_manager(); + struct nfacct_rule counter; + char *ipaddr = NULL; + int ret; + + STC_LOGI("remove appid(%s) classid(%d)", app_key->app_id, + app_value->classid); + + if (stc == NULL || connection == NULL) + return FALSE; + + if (!stc->carg) { + stc->carg = MALLOC0(counter_arg_s, 1); + if (stc->carg == NULL) + return FALSE; + + stc->carg->sock = stc_monitor_get_counter_socket(); + } + + memset(&counter, 0, sizeof(struct nfacct_rule)); + + counter.carg = stc->carg; + counter.classid = app_value->classid; + counter.intend = NFACCT_TETH_COUNTER; + + if (connection->tether_state != TRUE || + connection->tether_iface.ifname == NULL) + return FALSE; + + counter.iftype = connection->tether_iface.type; + g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH); + + /* get the ip address of the station based on its mac address */ + ret = stc_plugin_tether_get_station_ip(app_value->mac, &ipaddr); + if (ret != STC_ERROR_NONE) + return FALSE; + + __del_iptables_tether_in(&counter, ipaddr); + __del_iptables_tether_out(&counter, ipaddr); + + g_free(ipaddr); + return FALSE; +} static gboolean __add_application_monitor(gpointer key, gpointer value, gpointer data) { stc_app_value_s *app_value = (stc_app_value_s *)value; + stc_app_key_s *app_key = (stc_app_key_s *)key; default_connection_s *connection = (default_connection_s *)data; stc_s *stc = stc_get_manager(); + if (app_value->classid == STC_TOTAL_DATACALL_CLASSID || + app_value->classid == STC_TOTAL_WIFI_CLASSID || + app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID) + return FALSE; + if (stc && connection && connection->ifname) { struct nfacct_rule counter; if (!stc->carg) { - stc->carg = MALLOC0(counter_arg_s, 1); - stc->carg->sock = stc_monitor_get_counter_socket(); + stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE + if (stc->carg == NULL) //LCOV_EXCL_LINE + return FALSE; //LCOV_EXCL_LINE + + stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE } memset(&counter, 0, sizeof(struct nfacct_rule)); @@ -287,10 +650,32 @@ static gboolean __add_application_monitor(gpointer key, gpointer value, counter.carg = stc->carg; counter.classid = app_value->classid; counter.intend = NFACCT_COUNTER; - g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH); - __add_iptables_in(&counter); - __add_iptables_out(&counter); + if (connection->tether_state == TRUE && + connection->tether_iface.ifname != NULL && + app_value->classid == STC_TETHERING_APP_CLASSID) { + counter.iftype = connection->tether_iface.type; + g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH); + } else { + counter.iftype = connection->type; + g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH); + } + + if (g_str_has_suffix(app_key->app_id, STC_TETHERING_APP_SUFFIX) && + app_value->classid != STC_TETHERING_APP_CLASSID) { + __add_application_monitor_for_tethering(key, value, data); + } else if (app_value->classid == STC_TOTAL_IPV4_CLASSID) { + __add_iptables_in(&counter); + __add_iptables_out(&counter); + } else if (app_value->classid == STC_TOTAL_IPV6_CLASSID) { + __add_ip6tables_in(&counter); + __add_ip6tables_out(&counter); + } else { + __add_iptables_in(&counter); + __add_iptables_out(&counter); + __add_ip6tables_in(&counter); + __add_ip6tables_out(&counter); + } } return FALSE; @@ -300,6 +685,7 @@ static gboolean __remove_application_monitor(gpointer key, gpointer value, gpointer data) { stc_app_value_s *app_value = (stc_app_value_s *)value; + stc_app_key_s *app_key = (stc_app_key_s *)key; default_connection_s *connection = (default_connection_s *)data; stc_s *stc = stc_get_manager(); @@ -307,8 +693,11 @@ static gboolean __remove_application_monitor(gpointer key, gpointer value, struct nfacct_rule counter; if (!stc->carg) { - stc->carg = MALLOC0(counter_arg_s, 1); - stc->carg->sock = stc_monitor_get_counter_socket(); + stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE + if (stc->carg == NULL) //LCOV_EXCL_LINE + return FALSE; //LCOV_EXCL_LINE + + stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE } memset(&counter, 0, sizeof(struct nfacct_rule)); @@ -316,10 +705,25 @@ static gboolean __remove_application_monitor(gpointer key, gpointer value, counter.carg = stc->carg; counter.classid = app_value->classid; counter.intend = NFACCT_COUNTER; - g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH); + + if (g_str_has_suffix(app_key->app_id, STC_TETHERING_APP_SUFFIX) && + app_value->classid != STC_TETHERING_APP_CLASSID) { + __remove_application_monitor_for_tethering(key, value, data); + return FALSE; + } else if (connection->tether_state == FALSE && + connection->tether_iface.ifname != NULL && + app_value->classid == STC_TETHERING_APP_CLASSID) { + counter.iftype = connection->tether_iface.type; + g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH); + } else { + counter.iftype = connection->type; + g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH); + } __del_iptables_in(&counter); __del_iptables_out(&counter); + __del_ip6tables_in(&counter); + __del_ip6tables_out(&counter); } return FALSE; @@ -328,27 +732,400 @@ static gboolean __remove_application_monitor(gpointer key, gpointer value, static void __print_rstn(stc_rstn_key_s *rstn_key, stc_rstn_value_s *rstn_value) { STC_LOGI("rstn info => rstn_id [%llu], " - "app_id [%s], classid [%lu], ifname [%s], " - "iftype [%d], rst_state [%d], " - "limit [ in (%lld), out (%lld)], " - "warn_limit [ in (%lld), out (%lld)], " - "counter [ in (%lld), out (%lld)], " - "roaming [%d], imsi [%s]", + "app_id [%s], classid [%u], ifname [%s], " + "iftype [%d], rstn_state [%d], rstn_type [%d], " + "month_start_date [%d], limit [ (%lld) bytes], " + "warn_limit [ (%lld) bytes], " + "monthly_limit [ (%lld) bytes], " + "weekly_limit [ (%lld) bytes], " + "daily_limit [ (%lld) bytes], " + "data_counter [ (%lld) bytes], " + "warn_counter [ (%lld) bytes], " + "monthly_counter [ (%lld) bytes], " + "weekly_counter [ (%lld) bytes], " + "daily_counter [ (%lld) bytes], " + "roaming [%d], subscriber_id [%s]", rstn_value->restriction_id, rstn_key->app_id, rstn_value->classid , rstn_key->ifname, - rstn_key->iftype, rstn_value->rst_state, - rstn_value->limit.in_bytes, rstn_value->limit.out_bytes, - rstn_value->warn_limit.in_bytes, - rstn_value->warn_limit.out_bytes, - rstn_value->counter.in_bytes, rstn_value->counter.out_bytes, - rstn_key->roaming, rstn_key->imsi); + rstn_key->iftype, rstn_value->rstn_state, rstn_value->rstn_type, + rstn_value->month_start_date, + rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA], + rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN], + rstn_value->limit[STC_RSTN_LIMIT_TYPE_MONTHLY], + rstn_value->limit[STC_RSTN_LIMIT_TYPE_WEEKLY], + rstn_value->limit[STC_RSTN_LIMIT_TYPE_DAILY], + rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA], + rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN], + rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY], + rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY], + rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY], + rstn_key->roaming, rstn_key->subscriber_id); +} + +static void __add_iptables_rule(int64_t classid, nfacct_rule_intend intend, + stc_iface_type_e iftype) +{ + char *default_ifname = stc_default_connection_get_ifname(); + default_connection_s *connection = stc_get_default_connection(); + struct nfacct_rule counter; + stc_s *stc = stc_get_manager(); + if (!stc) { + g_free(default_ifname); //LCOV_EXCL_LINE + return; //LCOV_EXCL_LINE + } + + if (!stc->carg) { + stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE + if (stc->carg == NULL) { //LCOV_EXCL_LINE + g_free(default_ifname); //LCOV_EXCL_LINE + return; //LCOV_EXCL_LINE + } + + stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE + } + + counter.carg = stc->carg; + counter.classid = classid; + counter.intend = intend; + + if (connection && connection->tether_iface.ifname != NULL && + classid == STC_TETHERING_APP_CLASSID) { + counter.iftype = connection->tether_iface.type; + g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH); + } else { + counter.iftype = iftype; + g_strlcpy(counter.ifname, default_ifname, MAX_IFACE_LENGTH); + } + + g_free(default_ifname); + + /* iptables rule */ + __add_iptables_in(&counter); + __add_iptables_out(&counter); + + /* ip6tables rule */ + __add_ip6tables_in(&counter); + __add_ip6tables_out(&counter); } -static void __process_restriction(enum traffic_restriction_type rst_type, +static void __add_tethering_iptables_rule(int64_t classid, gchar *mac, + nfacct_rule_intend intend, stc_iface_type_e iftype) +{ + default_connection_s *connection = stc_get_default_connection(); + struct nfacct_rule counter; + stc_s *stc = stc_get_manager(); + char *ipaddr = NULL; + int ret; + + if (!stc || !mac) + return; + + if (!stc->carg) { + stc->carg = MALLOC0(counter_arg_s, 1); + if (stc->carg == NULL) + return; + + stc->carg->sock = stc_monitor_get_counter_socket(); + } + + memset(&counter, 0, sizeof(struct nfacct_rule)); + + counter.carg = stc->carg; + counter.classid = classid; + counter.intend = intend; + + if (connection->tether_state != TRUE || + connection->tether_iface.ifname == NULL) + return; + + counter.iftype = connection->tether_iface.type; + g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH); + + /* get connected station ip based on its mac */ + ret = stc_plugin_tether_get_station_ip(mac, &ipaddr); + if (ret != STC_ERROR_NONE) + return; + + /* tethering iptables rule */ + __add_iptables_tether_in(&counter, ipaddr); + __add_iptables_tether_out(&counter, ipaddr); + g_free(ipaddr); +} + +static void __del_tethering_iptables_rule(int64_t classid, gchar *mac, + nfacct_rule_intend intend, stc_iface_type_e iftype) +{ + default_connection_s *connection = stc_get_default_connection(); + struct nfacct_rule counter; + stc_s *stc = stc_get_manager(); + char *ipaddr = NULL; + int ret; + + if (!stc || !mac) + return; + + if (!stc->carg) { + stc->carg = MALLOC0(counter_arg_s, 1); + if (stc->carg == NULL) + return; + + stc->carg->sock = stc_monitor_get_counter_socket(); + } + + memset(&counter, 0, sizeof(struct nfacct_rule)); + + counter.carg = stc->carg; + counter.classid = classid; + counter.intend = intend; + + if (connection->tether_state != TRUE || + connection->tether_iface.ifname == NULL) + return; + + counter.iftype = connection->tether_iface.type; + g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH); + + /* get connected station ip based on its mac */ + ret = stc_plugin_tether_get_station_ip(mac, &ipaddr); + if (ret != STC_ERROR_NONE) { + STC_LOGE("Error: no IP found for station mac(%s)", mac); + return; + } + + /* tethering iptables rule */ + __del_iptables_tether_in(&counter, ipaddr); + __del_iptables_tether_out(&counter, ipaddr); + g_free(ipaddr); +} + +static void __del_iptables_rule(int64_t classid, nfacct_rule_intend intend, + stc_iface_type_e iftype) +{ + char *default_ifname = stc_default_connection_get_ifname(); + default_connection_s *connection = stc_get_default_connection(); + struct nfacct_rule counter; + stc_s *stc = stc_get_manager(); + if (!stc) { + g_free(default_ifname); //LCOV_EXCL_LINE + return; //LCOV_EXCL_LINE + } + + if (!stc->carg) { + stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE + if (stc->carg == NULL) { //LCOV_EXCL_LINE + g_free(default_ifname); //LCOV_EXCL_LINE + return; //LCOV_EXCL_LINE + } + + stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE + } + + counter.carg = stc->carg; + counter.classid = classid; + counter.intend = intend; + + if (connection && connection->tether_iface.ifname != NULL && + classid == STC_TETHERING_APP_CLASSID) { + counter.iftype = connection->tether_iface.type; + g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH); + } else { + counter.iftype = iftype; + g_strlcpy(counter.ifname, default_ifname, MAX_IFACE_LENGTH); + } + + g_free(default_ifname); + + /* iptables rule */ + __del_iptables_in(&counter); + __del_iptables_out(&counter); + + /* ip6tables rule */ + __del_ip6tables_in(&counter); + __del_ip6tables_out(&counter); +} + +static void __set_rstn_noti_state(int value) +{ + int state = STC_RSTN_STATE_INIT; + + if (__vconf_get_int(VCONFKEY_SETAPPL_DATA_RESTRICTION_INT, &state)) + return; + + if (state == value) { + STC_LOGI("No need to change a restriction status: %d", state); + return; + } + + vconf_set_int(VCONFKEY_SETAPPL_DATA_RESTRICTION_INT, value); + return; +} + +typedef struct { + time_t month_start_ts; + time_t week_start_ts; + time_t day_start_ts; + int64_t monthly_stat; + int64_t weekly_stat; + int64_t daily_stat; +} cumulative_data_s; + +static stc_cb_ret_e __statistics_info_cb(const table_statistics_info *info, + void *user_data) +{ + cumulative_data_s *stat = (cumulative_data_s *)user_data; + int64_t counters = 0; + + counters = info->cnt.in_bytes + info->cnt.out_bytes; + + stat->monthly_stat += counters; + if (stat->week_start_ts <= info->interval->from) + stat->weekly_stat += counters; + if (stat->day_start_ts <= info->interval->from) + stat->daily_stat += counters; + + return STC_CONTINUE; +} + +static void __process_tethering_restriction(enum traffic_restriction_type rstn_type, + stc_rstn_key_s *rstn_key, stc_rstn_value_s *rstn_value, void *data) +{ + default_connection_s *old_connection = (default_connection_s *)data; + default_connection_s *connection = NULL; + char *mac_str = NULL; + + if (old_connection != NULL) + connection = old_connection; + else + connection = stc_get_default_connection(); + + /* in case tethering is not active */ + if (connection->tether_state == FALSE) + return; + + /* rstn not applicable for this interface */ + if (rstn_key->ifname != NULL && g_strcmp0("", rstn_key->ifname) != 0 && + (g_strcmp0(connection->tether_iface.ifname, rstn_key->ifname) != 0)) + return; + + /* in case appid not a tethering app */ + if (!g_str_has_suffix(rstn_key->app_id, STC_TETHERING_APP_SUFFIX)) + return; + + /* Ignore TOTAL_TETHERING, + * Process only station appids */ + if (rstn_value->classid == STC_TETHERING_APP_CLASSID) + return; + + /* get the station mac based on classid */ + stc_plugin_tether_get_station_by_classid(rstn_value->classid, &mac_str); + if (!mac_str) { + STC_LOGE("station not found for classid(%d)", rstn_value->classid); + return; + } + + switch (rstn_type) { + case RST_SET: + { + int i; + table_counters_info info; + int64_t effective_limit[STC_RSTN_LIMIT_TYPE_MAX] = { 0, }; + + memset(&info, 0, sizeof(table_counters_info)); + rstn_value->limit_exceeded = 0; + + if ((rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] == 0 && + rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA] >= 0) || + (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] == 0 && + rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] >= 0) || + (rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] == 0 && + rstn_value->limit[STC_RSTN_LIMIT_TYPE_MONTHLY] >= 0) || + (rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] == 0 && + rstn_value->limit[STC_RSTN_LIMIT_TYPE_WEEKLY] >= 0) || + (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] == 0 && + rstn_value->limit[STC_RSTN_LIMIT_TYPE_DAILY] >= 0)) { + table_counters_get(rstn_value->restriction_id, &info); + + time_t current_time = 0; + cumulative_data_s stat; + table_statistics_select_rule rule; + + memset(&stat, 0, sizeof(cumulative_data_s)); + stat.month_start_ts = rstn_value->month_start_ts; + stat.week_start_ts = g_system->last_week_ts; + stat.day_start_ts = g_system->last_day_ts; + + memset(&rule, 0, sizeof(table_statistics_select_rule)); + rule.from = rstn_value->month_start_ts; + time(¤t_time); + rule.to = current_time; + rule.iftype = rstn_key->iftype; + rule.granularity = GRANULARITY; + + table_statistics_per_app(rstn_key->app_id, &rule, __statistics_info_cb, &stat); + + rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] = info.data_counter; + rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info.warn_counter; + rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] = info.monthly_counter + stat.monthly_stat; + rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] = info.weekly_counter + stat.weekly_stat; + rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] = info.daily_counter + stat.daily_stat; + } + + for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) { + if (rstn_value->limit[i] >= 0) { + effective_limit[i] = rstn_value->limit[i] - rstn_value->counter[i]; + + if (effective_limit[i] < 0) + rstn_value->limit_exceeded |= (1 << i); + } + } + + STC_LOGD("rstn_id [%llu], datausage [%llu] bytes", + rstn_value->restriction_id, info.data_counter); + + if (rstn_value->limit_exceeded != 0 && + rstn_value->limit_exceeded != (1 << STC_RSTN_LIMIT_TYPE_DATA_WARN)) { + __add_tethering_iptables_rule(rstn_value->classid, mac_str, + NFACCT_TETH_BLOCK, rstn_key->iftype); + } + + rstn_value->rstn_state = STC_RSTN_STATE_ACTIVATED; + } + break; + case RST_EXCLUDE: + { + __add_tethering_iptables_rule(rstn_value->classid, mac_str, + NFACCT_TETH_ALLOW, rstn_key->iftype); + + rstn_value->rstn_state = STC_RSTN_STATE_ACTIVATED; + rstn_value->limit_exceeded = 0; + rstn_value->limit_notified = 0; + } + break; + case RST_UNSET: + { + int i; + __del_tethering_iptables_rule(rstn_value->classid, mac_str, + NFACCT_TETH_BLOCK, rstn_key->iftype); + + rstn_value->rstn_state = STC_RSTN_STATE_DEACTIVATED; + rstn_value->limit_exceeded = 0; + rstn_value->limit_notified = 0; + + for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) + if (rstn_value->limit[i] >= 0) + rstn_value->counter[i] = 0; + } + break; + default: + ;//Do Nothing + } + FREE(mac_str); +} + +static void __process_restriction(enum traffic_restriction_type rstn_type, stc_rstn_key_s *rstn_key, stc_rstn_value_s *rstn_value, void *data) { - stc_data_counter_s effective_limit, effective_warn_limit; default_connection_s *old_connection = (default_connection_s *)data; default_connection_s *connection = NULL; @@ -363,84 +1140,124 @@ static void __process_restriction(enum traffic_restriction_type rst_type, /* rstn not applicable for this interface */ if (rstn_key->ifname != NULL && g_strcmp0("", rstn_key->ifname) != 0 && - g_strcmp0(connection->ifname, rstn_key->ifname) != 0) + (g_strcmp0(connection->ifname, rstn_key->ifname) != 0) && + (g_strcmp0(connection->tether_iface.ifname, rstn_key->ifname) != 0)) return; /* classid is invalid */ - if (rstn_value->classid == STC_UNKNOWN_CLASSID) + if (rstn_value->classid <= STC_UNKNOWN_CLASSID) return; - effective_limit.out_bytes = rstn_value->limit.out_bytes; - effective_limit.in_bytes = rstn_value->limit.in_bytes; - effective_warn_limit.out_bytes = rstn_value->warn_limit.out_bytes; - effective_warn_limit.in_bytes = rstn_value->warn_limit.in_bytes; + /* Do not proceed for tethering station appid if found here, + * for tethering station apps __process_tethering_restriction() call + * will handle it */ + if (g_str_has_suffix(rstn_key->app_id, STC_TETHERING_APP_SUFFIX) && + rstn_value->classid != STC_TETHERING_APP_CLASSID) + return; - if (rst_type == RST_SET) { - /* TODO: Change this to runtime memory */ + switch (rstn_type) { + case RST_SET: + { + int i; table_counters_info info; + int64_t effective_limit[STC_RSTN_LIMIT_TYPE_MAX] = { 0, }; memset(&info, 0, sizeof(table_counters_info)); - table_counters_get(rstn_value->restriction_id, &info); + rstn_value->limit_exceeded = 0; + + if ((rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] == 0 && + rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA] >= 0) || + (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] == 0 && + rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] >= 0) || + (rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] == 0 && + rstn_value->limit[STC_RSTN_LIMIT_TYPE_MONTHLY] >= 0) || + (rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] == 0 && + rstn_value->limit[STC_RSTN_LIMIT_TYPE_WEEKLY] >= 0) || + (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] == 0 && + rstn_value->limit[STC_RSTN_LIMIT_TYPE_DAILY] >= 0)) { + table_counters_get(rstn_value->restriction_id, &info); + + time_t current_time = 0; + cumulative_data_s stat; + table_statistics_select_rule rule; + + memset(&stat, 0, sizeof(cumulative_data_s)); + stat.month_start_ts = rstn_value->month_start_ts; + stat.week_start_ts = g_system->last_week_ts; + stat.day_start_ts = g_system->last_day_ts; + + memset(&rule, 0, sizeof(table_statistics_select_rule)); + rule.from = rstn_value->month_start_ts; + time(¤t_time); + rule.to = current_time; + rule.iftype = rstn_key->iftype; + rule.granularity = GRANULARITY; + + table_statistics_per_app(rstn_key->app_id, &rule, __statistics_info_cb, &stat); + + rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] = info.data_counter; + rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info.warn_counter; + rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] = info.monthly_counter + stat.monthly_stat; + rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] = info.weekly_counter + stat.weekly_stat; + rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] = info.daily_counter + stat.daily_stat; + } - effective_limit.out_bytes -= info.sent_bytes; - effective_limit.in_bytes -= info.rcv_bytes; - effective_warn_limit.out_bytes -= info.sent_bytes; - effective_warn_limit.in_bytes -= info.rcv_bytes; + for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) { + if (rstn_value->limit[i] >= 0) { + effective_limit[i] = rstn_value->limit[i] - rstn_value->counter[i]; - if (effective_limit.in_bytes < 0) { - effective_limit.in_bytes = 0; - rstn_value->in_limit_reached = TRUE; + if (effective_limit[i] < 0) + rstn_value->limit_exceeded |= (1 << i); + } } - if (effective_limit.out_bytes < 0) { - effective_limit.out_bytes = 0; - rstn_value->out_limit_reached = TRUE; - } + STC_LOGD("rstn_id [%llu], datausage [%llu] bytes", + rstn_value->restriction_id, info.data_counter); - if (effective_warn_limit.in_bytes < 0) - effective_warn_limit.in_bytes = 0; + if (rstn_value->limit_exceeded != 0 && + rstn_value->limit_exceeded != (1 << STC_RSTN_LIMIT_TYPE_DATA_WARN)) { + __add_iptables_rule(rstn_value->classid, NFACCT_BLOCK, rstn_key->iftype); + } - if (effective_warn_limit.out_bytes < 0) - effective_warn_limit.out_bytes = 0; - STC_LOGD("datausage [in: %lld, out: %lld]", - info.rcv_bytes, info.sent_bytes); + rstn_value->rstn_state = STC_RSTN_STATE_ACTIVATED; } - - STC_LOGD("rstn_id [%llu], effective limit [in: %lld, out: %lld], " - "effective warn limit [in: %lld, out: %lld], " - "datausage [in: %lld, out: %lld]", - rstn_value->restriction_id, - effective_limit.in_bytes, effective_limit.out_bytes, - effective_warn_limit.in_bytes, effective_warn_limit.out_bytes); - - send_net_restriction(rst_type, - rstn_value->classid, - 0 /*quota_id*/, - connection->type, - effective_limit.out_bytes, - effective_limit.in_bytes, - effective_warn_limit.out_bytes, - effective_warn_limit.in_bytes, - connection->ifname); - switch (rst_type) { - case RST_SET: - rstn_value->rst_state = STC_RESTRICTION_ACTIVATED; - rstn_value->in_limit_reached = FALSE; - rstn_value->out_limit_reached = FALSE; - break; + break; case RST_EXCLUDE: - ;//Do Nothing + __add_iptables_rule(rstn_value->classid, NFACCT_ALLOW, + rstn_key->iftype); + + rstn_value->rstn_state = STC_RSTN_STATE_ACTIVATED; + rstn_value->limit_exceeded = 0; + rstn_value->limit_notified = 0; break; case RST_UNSET: - rstn_value->rst_state = STC_RESTRICTION_REMOVED; - rstn_value->in_limit_reached = FALSE; - rstn_value->out_limit_reached = FALSE; - break; + { + int i; + + if (rstn_value->classid == STC_TETHERING_APP_CLASSID) + __del_iptables_rule(rstn_value->classid, NFACCT_BLOCK, + rstn_key->iftype); + else + __del_iptables_rule(rstn_value->classid, rstn_value->rstn_type, + rstn_key->iftype); + + rstn_value->rstn_state = STC_RSTN_STATE_DEACTIVATED; + rstn_value->limit_exceeded = 0; + rstn_value->limit_notified = 0; + + for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) + if (rstn_value->limit[i] >= 0) + rstn_value->counter[i] = 0; + + __set_rstn_noti_state(STC_RSTN_STATE_UNSET); + } + break; default: ;//Do Nothing } } +//LCOV_EXCL_START static gboolean __remove_rstns_foreach_application(gpointer key, gpointer value, gpointer data) @@ -458,16 +1275,20 @@ static gboolean __remove_rstns_foreach_application(gpointer key, goto out; /* rstn rule is already removed */ - if (rstn_value->rst_state == STC_RESTRICTION_REMOVED) + if (rstn_value->rstn_state == STC_RSTN_STATE_DEACTIVATED) goto out; /* remove restriction from system */ - __process_restriction(RST_UNSET, rstn_key, rstn_value, data); + __process_restriction(RST_UNSET, rstn_key, rstn_value, NULL); + + /* remove tethering restriction from system*/ + __process_tethering_restriction(RST_UNSET, rstn_key, rstn_value, NULL); __print_rstn(rstn_key, rstn_value); out: return FALSE; } +//LCOV_EXCL_STOP static void __remove_rstns_for_application(gchar *app_id) { @@ -485,8 +1306,8 @@ static stc_error_e __application_remove_if_empty(const stc_app_key_s *app_key) lookup = __application_lookup(g_system->apps, app_key); if (!lookup) { - STC_LOGE("app_key not found"); - return STC_ERROR_NO_DATA; + STC_LOGE("app_key not found"); //LCOV_EXCL_LINE + return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE } g_tree_foreach(lookup->processes, __processes_tree_check_empty, @@ -500,8 +1321,8 @@ static stc_error_e __application_remove_if_empty(const stc_app_key_s *app_key) } if (!g_tree_remove(g_system->apps, app_key)) { - ret = STC_ERROR_NO_DATA; - STC_LOGE("key not found"); + ret = STC_ERROR_NO_DATA; //LCOV_EXCL_LINE + STC_LOGE("key not found"); //LCOV_EXCL_LINE } return ret; @@ -512,126 +1333,218 @@ static stc_error_e __close_contr_sock(stc_system_s *system) ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter"); /* close netlink socket for updating kernel counters */ - if (g_system->contr_sock != -1) { - close(g_system->contr_sock); - g_system->contr_sock = -1; + if (system->contr_sock != -1) { + close(system->contr_sock); + system->contr_sock = -1; } - if (g_system->contr_gsource_id != 0) { - g_source_remove(g_system->contr_gsource_id); - g_system->contr_gsource_id = 0; + if (system->contr_gsource_id != 0) { + g_source_remove(system->contr_gsource_id); + system->contr_gsource_id = 0; } return STC_ERROR_NONE; } -static gboolean __rstn_counter_update_foreach_classid(gpointer key, - gpointer value, - gpointer data) +static gboolean __process_contr_reply(GIOChannel *source, + GIOCondition condition, + gpointer user_data); + +//LCOV_EXCL_START +static stc_error_e __close_and_reopen_contr_sock(stc_system_s *system) { - stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key; - stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value; - classid_bytes_context_s *context = (classid_bytes_context_s *)data; + GIOChannel *gio = NULL; + ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter"); - if (context->counter->intend != NFACCT_COUNTER) - goto try_next_callback; + /* close netlink socket for updating kernel counters */ + if (system->contr_sock != -1) { + close(system->contr_sock); + system->contr_sock = -1; + } - if (rstn_value->classid != context->counter->classid) - goto try_next_callback; + if (system->contr_gsource_id != 0) { + g_source_remove(system->contr_gsource_id); + system->contr_gsource_id = 0; + } - if (rstn_value->in_limit_reached == TRUE) { - context->in_limit_reached = TRUE; - goto try_next_callback; + /* create netlink socket for updating kernel counters */ + system->contr_sock = create_netlink(NETLINK_NETFILTER, 0); + if (system->contr_sock < 0) { + STC_LOGE("failed to open socket"); + FREE(system); + return STC_ERROR_FAIL; } - if (rstn_value->out_limit_reached == TRUE) { - context->out_limit_reached = TRUE; - goto try_next_callback; + gio = g_io_channel_unix_new(system->contr_sock); + system->contr_gsource_id = + g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP, + (GIOFunc) __process_contr_reply, + NULL); + g_io_channel_unref(gio); + + return STC_ERROR_NONE; +} + +static void __action_when_rstn_limit_exceeded_tethering(stc_rstn_key_s *rstn_key, + stc_rstn_value_s *rstn_value, classid_bytes_context_s *context) +{ + char *mac_str = NULL; + struct nfacct_rule *counter = context->counter; + + /* get the station mac based on classid */ + stc_plugin_tether_get_station_by_classid(counter->classid, &mac_str); + if (!mac_str) { + STC_LOGE("station not found for classid(%d)", counter->classid); + return; } - switch (context->counter->iotype) { - case NFACCT_COUNTER_IN: - rstn_value->counter.in_bytes += context->bytes; - - if (rstn_value->counter.in_bytes >= rstn_value->warn_limit.in_bytes - && rstn_value->warn_limit_crossed_notified == FALSE) { - gboolean rv = FALSE; - stc_s *stc = (stc_s *)stc_get_manager(); - ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data"); - - rv = stc_manager_dbus_emit_signal(stc->connection, - STC_DBUS_SERVICE_RESTRICTION_PATH, - STC_DBUS_INTERFACE_RESTRICTION, - "WarnThresholdCrossed", - g_variant_new("(s)", rstn_key->app_id)); - if (rv == TRUE) - rstn_value->warn_limit_crossed_notified = TRUE; - } + STC_LOGI("station mac %s, classid %u, iftype %u, iotype %d, \ + intend %d, ifname %s, bytes %lld", mac_str, + counter->classid, counter->iftype, counter->iotype, + counter->intend, counter->ifname, context->bytes); + + /* Block tethering station immediately */ + counter->intend = NFACCT_TETH_BLOCK; + __del_tethering_iptables_rule(counter->classid, mac_str, + NFACCT_TETH_BLOCK, rstn_key->iftype); + + __add_tethering_iptables_rule(counter->classid, mac_str, + NFACCT_TETH_BLOCK, rstn_key->iftype); + counter->intend = NFACCT_TETH_COUNTER; + + g_free(mac_str); +} + +static void __action_when_rstn_limit_exceeded(stc_rstn_limit_type_e limit_type, + stc_rstn_key_s *rstn_key, + stc_rstn_value_s *rstn_value, + classid_bytes_context_s *context) +{ + gboolean rv; + char iftype[MAX_INT_LENGTH] = { 0, }; + char byte[MAX_INT_LENGTH] = { 0, }; + const char *signal_name = NULL; + const char *net_popup_content = NULL; + const char *net_popup_type = NULL; + stc_s *stc = (stc_s *)stc_get_manager(); + + if (stc == NULL) { + STC_LOGE("Failed to get stc data"); + return; + } + + switch (limit_type) { + case STC_RSTN_LIMIT_TYPE_DATA_WARN: + { + signal_name = "WarnThresholdCrossed"; + net_popup_content = "warn threshold crossed"; + net_popup_type = "warning_noti"; + } + break; + case STC_RSTN_LIMIT_TYPE_DATA: + case STC_RSTN_LIMIT_TYPE_MONTHLY: + case STC_RSTN_LIMIT_TYPE_WEEKLY: + case STC_RSTN_LIMIT_TYPE_DAILY: + { + signal_name = "RestrictionThresholdCrossed"; + net_popup_content = "restriction threshold crossed"; + net_popup_type = "restriction_noti"; + + /* Apply restriction for tethering apps if app_id is of tethering client + * otherwise do the normal iptables rule */ + if (context->counter->intend == NFACCT_TETH_COUNTER) { + + if (g_str_has_suffix(rstn_key->app_id, STC_TETHERING_APP_SUFFIX) && + rstn_value->classid != STC_TETHERING_APP_CLASSID) { + __action_when_rstn_limit_exceeded_tethering(rstn_key, rstn_value, + context); + } - /* block immediately */ - if (rstn_value->counter.in_bytes >= rstn_value->limit.in_bytes) { + } else { + /* block immediately */ context->counter->intend = NFACCT_BLOCK; __del_iptables_in(context->counter); + __del_iptables_out(context->counter); __add_iptables_in(context->counter); + __add_iptables_out(context->counter); + + __del_ip6tables_in(context->counter); + __del_ip6tables_out(context->counter); + __add_ip6tables_in(context->counter); + __add_ip6tables_out(context->counter); context->counter->intend = NFACCT_COUNTER; - rstn_value->in_limit_reached = TRUE; - - if (rstn_value->rstn_limit_crossed_notified == FALSE) { - gboolean rv = FALSE; - stc_s *stc = (stc_s *)stc_get_manager(); - ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data"); - - rv = stc_manager_dbus_emit_signal(stc->connection, - STC_DBUS_SERVICE_RESTRICTION_PATH, - STC_DBUS_INTERFACE_RESTRICTION, - "RestrictionThresholdCrossed", - g_variant_new("(s)", rstn_key->app_id)); - if (rv == TRUE) - rstn_value->rstn_limit_crossed_notified = TRUE; - } } - g_system->rstns_tree_updated = TRUE; - __print_rstn(rstn_key, rstn_value); + rstn_value->limit_exceeded |= (1 << limit_type); + + __set_rstn_noti_state(STC_RSTN_STATE_SET); + } + break; + default: break; + } + + if (signal_name == NULL) { + STC_LOGE("Invalid parameter: limit_type"); + return; + } + + /* emit signal */ + rv = stc_manager_dbus_emit_signal(stc->connection, + STC_DBUS_SERVICE_RESTRICTION_PATH, + STC_DBUS_INTERFACE_RESTRICTION, + signal_name, + g_variant_new("(si)", + rstn_key->app_id, + rstn_key->iftype)); + + if (rv == TRUE) + rstn_value->limit_notified |= (1 << limit_type); + + snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype); + snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->limit[limit_type]); + stc_plugin_appstatus_send_message(net_popup_content, + net_popup_type, rstn_key->app_id, iftype, byte); +} + +static gboolean __rstn_counter_update(stc_rstn_key_s *rstn_key, + stc_rstn_value_s *rstn_value, + classid_bytes_context_s *context) +{ + int i; + switch (context->counter->iotype) { + case NFACCT_COUNTER_IN: case NFACCT_COUNTER_OUT: - rstn_value->counter.out_bytes += context->bytes; - - if (rstn_value->counter.out_bytes >= rstn_value->limit.out_bytes - && rstn_value->warn_limit_crossed_notified == FALSE) { - gboolean rv = FALSE; - stc_s *stc = (stc_s *)stc_get_manager(); - ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data"); - - rv = stc_manager_dbus_emit_signal(stc->connection, - STC_DBUS_SERVICE_RESTRICTION_PATH, - STC_DBUS_INTERFACE_RESTRICTION, - "WarnThresholdCrossed", - g_variant_new("(s)", rstn_key->app_id)); - if (rv == TRUE) - rstn_value->warn_limit_crossed_notified = TRUE; + if ((rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] == 0 && + rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA] >= 0) || + (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] == 0 && + rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] >= 0) || + (rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] == 0 && + rstn_value->limit[STC_RSTN_LIMIT_TYPE_MONTHLY] >= 0) || + (rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] == 0 && + rstn_value->limit[STC_RSTN_LIMIT_TYPE_WEEKLY] >= 0) || + (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] == 0 && + rstn_value->limit[STC_RSTN_LIMIT_TYPE_DAILY] >= 0)) { + table_counters_info info; + memset(&info, 0, sizeof(table_counters_info)); + table_counters_get(rstn_value->restriction_id, &info); + + rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] = info.data_counter; + rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info.warn_counter; + rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] = info.monthly_counter; + rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] = info.weekly_counter; + rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] = info.daily_counter; } - /* block immediately */ - if (rstn_value->counter.out_bytes >= rstn_value->limit.out_bytes) { - context->counter->intend = NFACCT_BLOCK; - __del_iptables_out(context->counter); - __add_iptables_out(context->counter); - context->counter->intend = NFACCT_COUNTER; - rstn_value->out_limit_reached = TRUE; - - if (rstn_value->rstn_limit_crossed_notified == FALSE) { - gboolean rv = FALSE; - stc_s *stc = (stc_s *)stc_get_manager(); - ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data"); - - rv = stc_manager_dbus_emit_signal(stc->connection, - STC_DBUS_SERVICE_RESTRICTION_PATH, - STC_DBUS_INTERFACE_RESTRICTION, - "RestrictionThresholdCrossed", - g_variant_new("(s)", rstn_key->app_id)); - if (rv == TRUE) - rstn_value->rstn_limit_crossed_notified = TRUE; + for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) { + if (rstn_value->limit[i] >= 0 && + !(rstn_value->limit_notified & (1 << i))) { + rstn_value->counter[i] += context->bytes; + if (rstn_value->limit[i] <= rstn_value->counter[i]) + __action_when_rstn_limit_exceeded(i, + rstn_key, + rstn_value, + context); } } @@ -642,11 +1555,67 @@ static gboolean __rstn_counter_update_foreach_classid(gpointer key, STC_LOGE("unknown iotype"); } + return FALSE; +} + +static gboolean __interface_rstn_counter_update(stc_rstn_key_s *rstn_key, + stc_rstn_value_s *rstn_value, + classid_bytes_context_s *context) +{ + if ((rstn_value->classid == STC_TOTAL_DATACALL_CLASSID && + context->counter->iftype == STC_IFACE_DATACALL) || + (rstn_value->classid == STC_TOTAL_WIFI_CLASSID && + context->counter->iftype == STC_IFACE_WIFI) || + (rstn_value->classid == STC_TOTAL_BLUETOOTH_CLASSID && + context->counter->iftype == STC_IFACE_BLUETOOTH) || + (rstn_value->classid == STC_TETHERING_APP_CLASSID && + context->counter->iftype == STC_IFACE_WIFI) || + (rstn_value->classid == STC_TETHERING_APP_CLASSID && + context->counter->iftype == STC_IFACE_BLUETOOTH) || + (rstn_value->classid == STC_TETHERING_APP_CLASSID && + context->counter->iftype == STC_IFACE_USB) || + (rstn_value->classid == STC_TETHERING_APP_CLASSID && + context->counter->iftype == STC_IFACE_P2P)) { + context->counter->classid = rstn_value->classid; + return __rstn_counter_update(rstn_key, rstn_value, context); + } -try_next_callback: return FALSE; } +static gboolean __rstn_counter_update_foreach_classid(gpointer key, + gpointer value, + gpointer data) +{ + gboolean rv = FALSE; + stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key; + stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value; + classid_bytes_context_s *context = (classid_bytes_context_s *)data; + uint32_t classid; + + if (context->counter->intend != NFACCT_COUNTER && + context->counter->intend != NFACCT_TETH_COUNTER) + goto try_next_callback; + + if (rstn_value->limit_exceeded == TRUE) { + context->data_limit_exceeded = TRUE; //LCOV_EXCL_LINE + goto try_next_callback; //LCOV_EXCL_LINE + } + + classid = context->counter->classid; + rv = __interface_rstn_counter_update(rstn_key, rstn_value, context); + + context->counter->classid = classid; + if (rstn_value->classid != context->counter->classid) + goto try_next_callback; + + rv = __rstn_counter_update(rstn_key, rstn_value, context); + +try_next_callback: + return rv; +} +//LCOV_EXCL_STOP + static gboolean __update_app_statistics(gpointer key, gpointer value, gpointer data) { @@ -655,26 +1624,64 @@ static gboolean __update_app_statistics(gpointer key, gpointer value, time_t *touch_time = (time_t *)data; stc_db_classid_iftype_key stat_key; stc_db_app_stats stat; - char *default_ifname = stc_default_connection_get_ifname(); + default_connection_s *default_connection = stc_get_default_connection(); memset(&stat_key, 0, sizeof(stc_db_classid_iftype_key)); memset(&stat, 0 , sizeof(stc_db_app_stats)); + /* Do not update statistics for Tethering + * if tethering is in-active found */ + if (default_connection && + default_connection->tether_state == FALSE && + !strcmp(app_key->app_id, STC_TOTAL_TETHERING)) + return FALSE; + + /* Do not update statistics for Wi-Fi + * if tethering is active on wlan0 iface */ + if (default_connection && default_connection->tether_state && + default_connection->tether_iface.type == STC_IFACE_WIFI && + !strcmp(app_key->app_id, STC_TOTAL_WIFI)) + return FALSE; + stat_key.classid = app_value->classid; - stat_key.iftype = stc_default_connection_get_type(); + + if (app_value->classid == STC_TETHERING_APP_CLASSID && + default_connection->tether_state == TRUE) + stat_key.iftype = default_connection->tether_iface.type; + else if (g_str_has_suffix(app_key->app_id, STC_TETHERING_APP_SUFFIX)) + stat_key.iftype = default_connection->tether_iface.type; + else + stat_key.iftype = default_connection->type; + if (STC_IFACE_DATACALL == stat_key.iftype) - stat_key.imsi = g_strdup("unknown"); + stat_key.subscriber_id = g_strdup(default_connection->subscriber_id); + else + stat_key.subscriber_id = g_strdup("none_subid"); //LCOV_EXCL_LINE + + if (app_value->classid == STC_TETHERING_APP_CLASSID && + default_connection->tether_state == TRUE) + g_strlcpy(stat_key.ifname, default_connection->tether_iface.ifname, + MAX_IFACE_LENGTH); + else if (g_str_has_suffix(app_key->app_id, STC_TETHERING_APP_SUFFIX)) + g_strlcpy(stat_key.ifname, default_connection->tether_iface.ifname, + MAX_IFACE_LENGTH); else - stat_key.imsi = g_strdup("noneimsi"); - g_strlcpy(stat_key.ifname, default_ifname, MAX_IFACE_LENGTH); + g_strlcpy(stat_key.ifname, default_connection->ifname, + MAX_IFACE_LENGTH); stat.app_id = g_strdup(app_key->app_id); stat.snd_count = app_value->counter.out_bytes; stat.rcv_count = app_value->counter.in_bytes; - stat.delta_snd = 0; - stat.delta_rcv = 0; - stat.is_roaming = stc_default_connection_get_roaming(); - stat.ground = STC_APP_STATE_UNKNOWN; + stat.is_roaming = default_connection->roaming; + + if (strstr(stat.app_id, "_BACKGROUND")) { + stat.ground = STC_APP_STATE_BACKGROUND; + } else { + if (strstr(stat.app_id, "TOTAL_")) + stat.ground = STC_APP_STATE_UNKNOWN; + else + stat.ground = STC_APP_STATE_FOREGROUND; + } table_statistics_insert(&stat_key, &stat, *touch_time); @@ -682,15 +1689,18 @@ static gboolean __update_app_statistics(gpointer key, gpointer value, app_value->counter.in_bytes = 0; FREE(stat.app_id); - FREE(stat_key.imsi); - FREE(default_ifname); + FREE(stat_key.subscriber_id); return FALSE; } static gboolean __flush_apps_stats_to_database(gpointer user_data) { - time_t current_time = time(0); + time_t current_time = 0; + stc_s *stc = stc_get_manager(); + + if (stc && stc->carg) + current_time = stc->carg->last_run_time; if (g_system->apps_tree_updated == FALSE) return G_SOURCE_REMOVE; @@ -706,14 +1716,18 @@ static gboolean __flush_apps_stats_to_database(gpointer user_data) return G_SOURCE_REMOVE; } +//LCOV_EXCL_START static gboolean __update_counter_statistics(gpointer key, gpointer value, gpointer data) { stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value; table_counters_info info = { .restriction_id = rstn_value->restriction_id, - .sent_bytes = rstn_value->counter.out_bytes, - .rcv_bytes = rstn_value->counter.in_bytes + .data_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA], + .warn_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN], + .monthly_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY], + .weekly_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY], + .daily_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] }; table_counters_update_counters(&info); @@ -723,7 +1737,11 @@ static gboolean __update_counter_statistics(gpointer key, gpointer value, static gboolean __flush_rstns_counter_to_database(gpointer user_data) { - time_t current_time = time(0); + time_t current_time = 0; + stc_s *stc = stc_get_manager(); + + if (stc && stc->carg) + current_time = stc->carg->last_run_time; if (g_system->rstns_tree_updated == FALSE) return G_SOURCE_REMOVE; @@ -738,47 +1756,221 @@ static gboolean __flush_rstns_counter_to_database(gpointer user_data) STC_LOGI("Flushed rstns counters to database"); return G_SOURCE_REMOVE; } +//LCOV_EXCL_STOP -static gboolean __apps_counter_update_foreach_classid(gpointer key, - gpointer value, - gpointer data) +static void __app_counter_update(stc_app_key_s *app_key, + stc_app_value_s *app_value, + classid_bytes_context_s *context) { - stc_app_key_s *app_key = (stc_app_key_s *)key; - stc_app_value_s *app_value = (stc_app_value_s *)value; - classid_bytes_context_s *context = (classid_bytes_context_s *)data; - - if (context->counter->intend != NFACCT_COUNTER) - goto try_next_callback; - - if (app_value->classid != context->counter->classid) - goto try_next_callback; - switch (context->counter->iotype) { case NFACCT_COUNTER_IN: app_value->data_usage.in_bytes += context->bytes; app_value->counter.in_bytes = context->bytes; g_system->apps_tree_updated = TRUE; - __apps_tree_foreach_print(app_key, app_value, NULL); + /* + __apps_tree_foreach_print(app_key, app_value, NULL); //LCOV_EXCL_LINE + */ break; case NFACCT_COUNTER_OUT: app_value->data_usage.out_bytes += context->bytes; app_value->counter.out_bytes = context->bytes; g_system->apps_tree_updated = TRUE; - __apps_tree_foreach_print(app_key, app_value, NULL); + /* + __apps_tree_foreach_print(app_key, app_value, NULL); //LCOV_EXCL_LINE + */ break; default: - STC_LOGE("unknown iotype"); + STC_LOGE("unknown iotype"); //LCOV_EXCL_LINE + } +} + +static void __interface_counter_update(stc_app_key_s *app_key, + stc_app_value_s *app_value, + classid_bytes_context_s *context) +{ + if ((app_value->classid == STC_TOTAL_DATACALL_CLASSID && + context->counter->iftype == STC_IFACE_DATACALL) || + (app_value->classid == STC_TOTAL_WIFI_CLASSID && + context->counter->iftype == STC_IFACE_WIFI) || + (app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID && + context->counter->iftype == STC_IFACE_BLUETOOTH) || + (app_value->classid == STC_TETHERING_APP_CLASSID && + context->counter->iftype == STC_IFACE_WIFI) || + (app_value->classid == STC_TETHERING_APP_CLASSID && + context->counter->iftype == STC_IFACE_BLUETOOTH) || + (app_value->classid == STC_TETHERING_APP_CLASSID && + context->counter->iftype == STC_IFACE_USB) || + (app_value->classid == STC_TETHERING_APP_CLASSID && + context->counter->iftype == STC_IFACE_P2P)) + __app_counter_update(app_key, app_value, context); +} + + +static gboolean __apps_counter_update_foreach_classid(gpointer key, + gpointer value, + gpointer data) +{ + stc_app_key_s *app_key = (stc_app_key_s *)key; + stc_app_value_s *app_value = (stc_app_value_s *)value; + classid_bytes_context_s *context = (classid_bytes_context_s *)data; + + if (context->counter->intend != NFACCT_COUNTER && + context->counter->intend != NFACCT_TETH_COUNTER) + goto try_next_callback; + + __interface_counter_update(app_key, app_value, context); + + if (app_value->classid != context->counter->classid) + goto try_next_callback; + + __app_counter_update(app_key, app_value, context); + +try_next_callback: + return FALSE; +} + +static gboolean __reset_time_counter_foreach_rstn(gpointer key, + gpointer value, + gpointer data) +{ + stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key; + stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value; + reset_time_limits_context_s *context = (reset_time_limits_context_s *)data; + int i; + time_t now_month_start_ts; + + if (rstn_value->month_start_date == 0) { + table_counters_info info; + memset(&info, 0, sizeof(table_counters_info)); + table_counters_get_timestamps(rstn_value->restriction_id, &info); + + if (info.month_start_date == 0) + rstn_value->month_start_date = 1; + else + rstn_value->month_start_date = info.month_start_date; + rstn_value->month_start_ts = info.month_start_ts; + } + + now_month_start_ts = + stc_time_get_month_start(context->now, + rstn_value->month_start_date); + + if (rstn_value->month_start_ts != now_month_start_ts) { + rstn_value->month_start_ts = now_month_start_ts; + context->month_start_ts = now_month_start_ts; + context->is_updated |= (1 << STC_RSTN_LIMIT_TYPE_MONTHLY); + } + + if (context->is_updated) { + table_counters_info info; + memset(&info, 0, sizeof(table_counters_info)); + + info.restriction_id = rstn_value->restriction_id; + info.month_start_date = rstn_value->month_start_date; + info.month_start_ts = rstn_value->month_start_ts; + info.week_start_ts = context->week_start_ts; + info.day_start_ts = context->day_start_ts; + + table_counters_update_timestamps(&info); + } + + for (i = STC_RSTN_LIMIT_TYPE_MONTHLY; i < STC_RSTN_LIMIT_TYPE_MAX; i++) { + + if ((context->is_updated) & (1 << i)) { + /* reset limit */ + rstn_value->counter[i] = 0; + + if (rstn_value->limit_exceeded & (1 << i)) { + /* remove iptables rule */ + char *default_ifname = stc_default_connection_get_ifname(); + struct nfacct_rule counter; + stc_s *stc = stc_get_manager(); + if (stc == NULL) { + STC_LOGE("Can't get stc data"); + g_free(default_ifname); + goto try_next_callback; + } + + if (!stc->carg) { + stc->carg = MALLOC0(counter_arg_s, 1); + if (stc->carg == NULL) { + g_free(default_ifname); + goto try_next_callback; + } + + stc->carg->sock = + stc_monitor_get_counter_socket(); + } + + counter.carg = stc->carg; + counter.classid = rstn_value->classid; + counter.intend = NFACCT_BLOCK; + counter.iftype = rstn_key->iftype; + g_strlcpy(counter.ifname, default_ifname, + MAX_IFACE_LENGTH); + + g_free(default_ifname); + + /* iptables rule */ + __del_iptables_in(&counter); + __del_iptables_out(&counter); + + /* ip6tables rule */ + __del_ip6tables_in(&counter); + __del_ip6tables_out(&counter); + + rstn_value->rstn_state = STC_RSTN_STATE_DEACTIVATED; + rstn_value->limit_exceeded &= ~(1 << i); + rstn_value->limit_notified &= ~(1 << i); + } + } } try_next_callback: return FALSE; } +static void __reset_time_counters_if_required(void) +{ + reset_time_limits_context_s context; + + if (g_system == NULL) { + STC_LOGE("stc monitor not initialized!"); + return; + } + + context.now = time(NULL); + context.week_start_ts = stc_time_get_week_start(context.now); + context.day_start_ts = stc_time_get_day_start(context.now); + context.is_updated = 0; + + if (g_system->last_week_ts != context.week_start_ts) { + g_system->last_week_ts = context.week_start_ts; + context.is_updated |= (1 << STC_RSTN_LIMIT_TYPE_WEEKLY); + } + + if (g_system->last_day_ts != context.day_start_ts) { + g_system->last_day_ts = context.day_start_ts; + context.is_updated |= (1 << STC_RSTN_LIMIT_TYPE_DAILY); + } + + if (g_system->rstns) { + g_tree_foreach(g_system->rstns, + __reset_time_counter_foreach_rstn, + &context); + if (context.is_updated) + STC_LOGD("Counter reset completed month_start [%ld], week_start [%ld], day_start [%ld]", + context.month_start_ts, g_system->last_week_ts, g_system->last_day_ts); + } +} + static void __fill_nfacct_result(char *cnt_name, int64_t bytes, struct counter_arg *carg) { + __reset_time_counters_if_required(); + struct nfacct_rule counter = { .carg = carg, .name = {0}, @@ -789,21 +1981,19 @@ static void __fill_nfacct_result(char *cnt_name, int64_t bytes, classid_bytes_context_s context = { .counter = &counter, .bytes = bytes, - .in_limit_reached = FALSE, - .out_limit_reached = FALSE + .data_limit_exceeded = FALSE, }; - STC_LOGD("cnt_name %s", cnt_name); - if (!recreate_counter_by_name(cnt_name, &counter)) { - STC_LOGE("Can't parse counter name %s", cnt_name); - return; + STC_LOGE("Can't parse counter name %s", cnt_name); //LCOV_EXCL_LINE + return; //LCOV_EXCL_LINE } - STC_LOGI("classid %lu, iftype %u, iotype %d, intend %d, ifname %s, bytes %lld", - context.counter->classid, context.counter->iftype, - context.counter->iotype, context.counter->intend, - context.counter->ifname, context.bytes); + if (STC_DEBUG_LOG) + STC_LOGI("classid %u, iftype %u, iotype %d, intend %d, ifname %s, bytes %lld", + context.counter->classid, context.counter->iftype, + context.counter->iotype, context.counter->intend, + context.counter->ifname, context.bytes); if (g_system->rstns) g_tree_foreach(g_system->rstns, @@ -864,8 +2054,8 @@ static void __process_network_counter(struct genl *ans, netlink_serialization_command *netlink = netlink_create_command(&ser_params); if (!netlink) { - STC_LOGE("Can not create command"); - return; + STC_LOGE("Can not create command"); //LCOV_EXCL_LINE + return; //LCOV_EXCL_LINE } netlink->deserialize_answer(&(netlink->params)); @@ -876,36 +2066,53 @@ static gboolean __process_contr_reply(GIOChannel *source, gpointer user_data) { int sock = g_io_channel_unix_get_fd(source); - struct genl ans; + struct genl *ans; int ret; stc_s *stc = stc_get_manager(); - if ((condition & G_IO_ERR) || - (condition & G_IO_HUP) || +#ifdef TIZEN_GTESTS + void __gcov_flush(void); + __gcov_flush(); +#endif + + if ((condition & G_IO_ERR) || (condition & G_IO_HUP) || (condition & G_IO_NVAL)) { /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */ - __close_contr_sock(g_system); - return FALSE; + STC_LOGE("Counter socket received G_IO event, closing socket." //LCOV_EXCL_LINE + "G_IO_ERR [%u], G_IO_HUP [%u], G_IO_NVAL [%u]", + (condition & G_IO_ERR), (condition & G_IO_HUP), + (condition & G_IO_NVAL)); + __close_and_reopen_contr_sock(g_system); //LCOV_EXCL_LINE + return FALSE; //LCOV_EXCL_LINE + } + + ans = MALLOC0(struct genl, 1); + if (ans == NULL) { + STC_LOGE("Failed allocate memory to genl reply message"); //LCOV_EXCL_LINE + return TRUE; //LCOV_EXCL_LINE } if (stc == NULL) { - STC_LOGE("Can't get stc data"); - goto out; + STC_LOGE("Can't get stc data"); //LCOV_EXCL_LINE + goto out; //LCOV_EXCL_LINE } - ret = read_netlink(sock, - &ans, sizeof(struct genl)); - STC_LOGD("Counter data received ret [%d]", ret); + ret = read_netlink(sock, ans, sizeof(struct genl)); + /* STC_LOGD("Counter data received ret [%d]", ret); */ if (ret == 0) goto out; stc->carg->ans_len = ret; - __process_network_counter(&ans, stc->carg); + stc->carg->last_run_time = time(NULL); + + __process_network_counter(ans, stc->carg); g_idle_add(__flush_apps_stats_to_database, NULL); g_idle_add(__flush_rstns_counter_to_database, NULL); out: + + FREE(ans); return TRUE; } @@ -916,16 +2123,26 @@ static gboolean __update_contr_cb(void *user_data) ret_value_msg_if(stc == NULL, STC_ERROR_FAIL, "Can't get stc data"); if (!stc->carg) { stc->carg = MALLOC0(counter_arg_s, 1); + if (stc->carg == NULL) + return TRUE; /* we need to continue the timer */ + stc->carg->sock = stc_monitor_get_counter_socket(); } - STC_LOGD("Get all counters"); +#ifdef TIZEN_GTESTS + void __gcov_flush(void); + __gcov_flush(); +#endif + + /* STC_LOGD("Get all counters"); */ nfacct_send_get_all(stc->carg); /* we need to continue the timer */ return TRUE; } -#if 0 + +/* +//LCOV_EXCL_START static gboolean __rstn_tree_foreach_print(gpointer key, gpointer value, gpointer data) { @@ -940,7 +2157,9 @@ static void __rstn_tree_printall(void) { g_tree_foreach(g_system->rstns, __rstn_tree_foreach_print, NULL); } -#endif +//LCOV_EXCL_STOP +*/ + static stc_rstn_value_s * __rstn_lookup(GTree *rstns_tree, const stc_rstn_key_s *key) { @@ -959,11 +2178,8 @@ static gboolean __remove_restriction(gpointer key, gpointer value, stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key; stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value; - /* rstn rule is already removed */ - if (rstn_value->rst_state == STC_RESTRICTION_REMOVED) - return FALSE; - __process_restriction(RST_UNSET, rstn_key, rstn_value, data); + __process_tethering_restriction(RST_UNSET, rstn_key, rstn_value, data); __print_rstn(rstn_key, rstn_value); return FALSE; } @@ -975,35 +2191,43 @@ static gboolean __add_restriction_debug(gpointer key, gpointer value, stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value; /* rstn rule is activated */ - if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED) + if (rstn_value->rstn_state == STC_RSTN_STATE_ACTIVATED) return FALSE; - if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED) + if (rstn_value->rstn_type == STC_RSTN_TYPE_ACCEPT) { __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data); - else + __process_tethering_restriction(RST_EXCLUDE, rstn_key, rstn_value, data); + } else { __process_restriction(RST_SET, rstn_key, rstn_value, data); + __process_tethering_restriction(RST_SET, rstn_key, rstn_value, data); + } __print_rstn(rstn_key, rstn_value); return FALSE; } +//LCOV_EXCL_START static gboolean __add_restriction(gpointer key, gpointer value, gpointer data) { stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key; stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value; /* rstn rule is activated */ - if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED) + if (rstn_value->rstn_state == STC_RSTN_STATE_ACTIVATED) return FALSE; - if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED) + if (rstn_value->rstn_type == STC_RSTN_TYPE_ACCEPT) { __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data); - else + __process_tethering_restriction(RST_EXCLUDE, rstn_key, rstn_value, data); + } else { __process_restriction(RST_SET, rstn_key, rstn_value, data); + __process_tethering_restriction(RST_SET, rstn_key, rstn_value, data); + } return FALSE; } +//LCOV_EXCL_STOP static stc_error_e __rstn_tree_remove(stc_rstn_key_s *key) { @@ -1013,18 +2237,17 @@ static stc_error_e __rstn_tree_remove(stc_rstn_key_s *key) lookup_value = __rstn_lookup(g_system->rstns, key); if (!lookup_value) { - STC_LOGE("key not found"); - return STC_ERROR_NO_DATA; + STC_LOGE("key not found"); //LCOV_EXCL_LINE + return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE } - __remove_restriction(key, lookup_value, NULL); - /* remove counter also */ table_counters_delete(lookup_value->restriction_id); + __remove_restriction(key, lookup_value, NULL); if (!g_tree_remove(g_system->rstns, key)) { - STC_LOGD("key not found"); - return STC_ERROR_NO_DATA; + STC_LOGD("key not found"); //LCOV_EXCL_LINE + return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE } return STC_ERROR_NONE; @@ -1033,45 +2256,52 @@ static stc_error_e __rstn_tree_remove(stc_rstn_key_s *key) static stc_error_e __rstn_tree_add(stc_rstn_key_s *key, stc_rstn_value_s *value, gboolean debug) { + stc_rstn_key_s *rstn_key; stc_rstn_value_s *rstn_value; ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!"); rstn_value = __rstn_lookup(g_system->rstns, key); - if (!rstn_value) { - stc_rstn_key_s *rstn_key = MALLOC0(stc_rstn_key_s, 1); - if (!rstn_key) { - STC_LOGE("rstn_key allocation failed"); - return STC_ERROR_OUT_OF_MEMORY; - } - - rstn_value = MALLOC0(stc_rstn_value_s, 1); - if (!rstn_value) { - STC_LOGE("rstn_value allocation failed"); - FREE(rstn_key); - return STC_ERROR_OUT_OF_MEMORY; - } + if (rstn_value) + __rstn_tree_remove(key); - rstn_key->app_id = g_strdup(key->app_id); - rstn_key->ifname = g_strdup(key->ifname); - rstn_key->imsi = g_strdup(key->imsi); - rstn_key->iftype = key->iftype; - rstn_key->roaming = key->roaming; + rstn_key = MALLOC0(stc_rstn_key_s, 1); + if (!rstn_key) { + STC_LOGE("rstn_key allocation failed"); //LCOV_EXCL_LINE + return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE + } - g_tree_insert(g_system->rstns, rstn_key, rstn_value); + rstn_value = MALLOC0(stc_rstn_value_s, 1); + if (!rstn_value) { + STC_LOGE("rstn_value allocation failed"); //LCOV_EXCL_LINE + FREE(rstn_key); //LCOV_EXCL_LINE + return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE } + rstn_key->app_id = g_strdup(key->app_id); + rstn_key->ifname = g_strdup(key->ifname); + rstn_key->mac = g_strdup(key->mac); + rstn_key->subscriber_id = g_strdup(key->subscriber_id); + rstn_key->iftype = key->iftype; + rstn_key->roaming = key->roaming; + + g_tree_insert(g_system->rstns, rstn_key, rstn_value); + rstn_value->restriction_id = value->restriction_id; - rstn_value->rst_state = value->rst_state; + rstn_value->rstn_state = value->rstn_state; + rstn_value->rstn_type = value->rstn_type; rstn_value->classid = value->classid; - rstn_value->limit.in_bytes = value->limit.in_bytes; - rstn_value->limit.out_bytes = value->limit.out_bytes; - rstn_value->warn_limit.in_bytes = value->warn_limit.in_bytes; - rstn_value->warn_limit.out_bytes = value->warn_limit.out_bytes; - rstn_value->counter.in_bytes = 0; - rstn_value->counter.out_bytes = 0; - rstn_value->warn_limit_crossed_notified = FALSE; - rstn_value->rstn_limit_crossed_notified = FALSE; + + int i; + for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) { + rstn_value->limit[i] = value->limit[i]; + rstn_value->counter[i] = 0; + } + + rstn_value->limit_exceeded = 0; + rstn_value->limit_notified = 0; + rstn_value->month_start_date = value->month_start_date; + rstn_value->month_start_ts = value->month_start_ts; if (debug == TRUE) __add_restriction_debug(key, rstn_value, NULL); @@ -1081,6 +2311,7 @@ static stc_error_e __rstn_tree_add(stc_rstn_key_s *key, return STC_ERROR_NONE; } +//LCOV_EXCL_START static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info, void *user_data) { @@ -1094,36 +2325,39 @@ static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info, key.app_id = g_strdup(info->app_id); key.ifname = g_strdup(info->ifname); - key.imsi = g_strdup(info->imsi); + key.subscriber_id = g_strdup(info->subscriber_id); key.iftype = info->iftype; key.roaming = info->roaming; - value.rst_state = info->rst_state; + value.rstn_type = info->rstn_type; + value.rstn_state = STC_RSTN_STATE_UNKNOWN; value.restriction_id = info->restriction_id; - if (value.rst_state != STC_RESTRICTION_EXCLUDED && info->app_id) + if (info->app_id) value.classid = get_classid_by_app_id(info->app_id, TRUE); else value.classid = STC_UNKNOWN_CLASSID; - value.limit.in_bytes = info->rcv_limit; - value.limit.out_bytes = info->send_limit; - value.warn_limit.in_bytes = info->rcv_warn_limit; - value.warn_limit.out_bytes = info->send_warn_limit; + value.limit[STC_RSTN_LIMIT_TYPE_DATA] = info->data_limit; + value.limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info->data_warn_limit; + value.limit[STC_RSTN_LIMIT_TYPE_MONTHLY] = info->monthly_limit; + value.limit[STC_RSTN_LIMIT_TYPE_WEEKLY] = info->weekly_limit; + value.limit[STC_RSTN_LIMIT_TYPE_DAILY] = info->daily_limit; if (__rstn_tree_add(&key, &value, FALSE) != STC_ERROR_NONE) ret = STC_CANCEL; FREE(key.app_id); FREE(key.ifname); - FREE(key.imsi); + FREE(key.subscriber_id); return ret; } static void __fill_restritions_list(void) { table_restrictions_foreach(__insert_restriction_cb, NULL); - //__rstn_tree_printall(); + + /* __rstn_tree_printall(); */ } static gboolean __add_rstn_foreach_application(gpointer key, @@ -1143,19 +2377,23 @@ static gboolean __add_rstn_foreach_application(gpointer key, goto out; /* rstn rule is already applied */ - if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED) + if (rstn_value->rstn_state == STC_RSTN_STATE_ACTIVATED) goto out; /* add restriction to system */ - if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED) - __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data); - else - __process_restriction(RST_SET, rstn_key, rstn_value, data); + if (rstn_value->rstn_type == STC_RSTN_TYPE_ACCEPT) { + __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, NULL); + __process_tethering_restriction(RST_EXCLUDE, rstn_key, rstn_value, NULL); + } else { + __process_restriction(RST_SET, rstn_key, rstn_value, NULL); + __process_tethering_restriction(RST_SET, rstn_key, rstn_value, NULL); + } __print_rstn(rstn_key, rstn_value); out: return FALSE; } +//LCOV_EXCL_STOP static void __add_rstns_for_application(gchar *app_id) { @@ -1163,6 +2401,84 @@ static void __add_rstns_for_application(gchar *app_id) app_id); } +static void __add_application_by_interface(const char *app_id) +{ + stc_app_key_s app_key; + stc_app_value_s app_value; + + if (app_id == NULL) + return; //LCOV_EXCL_LINE + + memset(&app_key, 0, sizeof(stc_app_key_s)); + memset(&app_value, 0, sizeof(stc_app_value_s)); + + app_key.pkg_id = g_strdup(app_id); + app_key.app_id = g_strdup(app_id); + + app_value.type = STC_APP_TYPE_NONE; + app_value.processes = NULL; + app_value.counter.in_bytes = 0; + app_value.counter.out_bytes = 0; + + stc_monitor_application_add(app_key, app_value); + + FREE(app_key.pkg_id); + FREE(app_key.app_id); +} + +static guint __get_background_state(void) +{ + return g_system->background_state;; +} + +static void __set_background_state(guint state) +{ + g_system->background_state = state; +} + +static gboolean __processes_tree_foreach_background(gpointer key, + gpointer value, + gpointer data) +{ + stc_process_key_s *proc_key = (stc_process_key_s *)key; + stc_app_key_s *app_key = (stc_app_key_s *)data; + + if (g_system->background_state) + place_pids_to_net_cgroup(proc_key->pid, STC_BACKGROUND_APP_ID); + else + place_pids_to_net_cgroup(proc_key->pid, app_key->app_id); + + return FALSE; +} + +static gboolean __apps_tree_foreach_background(gpointer key, gpointer value, + gpointer data) +{ + stc_app_key_s *app_key = (stc_app_key_s *)key; + stc_app_value_s *app_value = (stc_app_value_s *)value; + + if (strstr(app_key->app_id, STC_BACKGROUND_APP_SUFFIX)) + g_tree_foreach(app_value->processes, + __processes_tree_foreach_background, app_key); + + return FALSE; +} + +static stc_error_e __process_update_background(void) +{ + ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!"); + + g_tree_foreach(g_system->apps, __apps_tree_foreach_background, NULL); + + return STC_ERROR_NONE; +} +//LCOV_EXCL_STOP + +static void __fill_exceptions_list(void) +{ + stc_plugin_fill_exception_list(); +} + stc_error_e stc_monitor_init(void) { stc_system_s *system = MALLOC0(stc_system_s, 1); @@ -1170,6 +2486,9 @@ stc_error_e stc_monitor_init(void) ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY, "stc_system_s malloc fail!"); + /* initializing current classid */ + init_current_classid(); + /* initializing cgroups */ cgroup_init(); @@ -1184,10 +2503,10 @@ stc_error_e stc_monitor_init(void) /* create netlink socket for updating kernel counters */ system->contr_sock = create_netlink(NETLINK_NETFILTER, 0); - if (!(system->contr_sock)) { - STC_LOGE("failed to open socket"); - FREE(system); - return STC_ERROR_FAIL; + if (system->contr_sock < 0) { + STC_LOGE("failed to open socket"); //LCOV_EXCL_LINE + FREE(system); //LCOV_EXCL_LINE + return STC_ERROR_FAIL; //LCOV_EXCL_LINE } gio = g_io_channel_unix_new(system->contr_sock); @@ -1199,6 +2518,13 @@ stc_error_e stc_monitor_init(void) g_system = system; + __add_application_by_interface(STC_TOTAL_DATACALL); + __add_application_by_interface(STC_TOTAL_WIFI); + __add_application_by_interface(STC_TOTAL_BLUETOOTH); + __add_application_by_interface(STC_TOTAL_IPV4); + __add_application_by_interface(STC_TOTAL_IPV6); + __add_application_by_interface(STC_TOTAL_TETHERING); + /* creating restriction rules tree */ __update_contr_cb(NULL); @@ -1207,11 +2533,15 @@ stc_error_e stc_monitor_init(void) __update_contr_cb, NULL); if (g_system->contr_timer_id == 0) { - STC_LOGE("Failed to register kernel counters update timer"); - __close_contr_sock(g_system); - return STC_ERROR_FAIL; + STC_LOGE("Failed to register kernel counters update timer"); //LCOV_EXCL_LINE + __close_contr_sock(g_system); //LCOV_EXCL_LINE + return STC_ERROR_FAIL; //LCOV_EXCL_LINE } + __vconf_get_int(VCONFKEY_STC_BACKGROUND_STATE, + (int *)&g_system->background_state); + + __fill_exceptions_list(); __fill_restritions_list(); return STC_ERROR_NONE; @@ -1243,7 +2573,7 @@ stc_error_e stc_monitor_deinit(void) return STC_ERROR_NONE; } -stc_error_e stc_monitor_application_add(const stc_app_key_s app_key, +API stc_error_e stc_monitor_application_add(const stc_app_key_s app_key, const stc_app_value_s app_value) { stc_error_e ret = STC_ERROR_NONE; @@ -1254,30 +2584,29 @@ stc_error_e stc_monitor_application_add(const stc_app_key_s app_key, ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!"); lookup = __application_lookup(g_system->apps, &app_key); - if (lookup) { - STC_LOGD("app_key already present"); - return STC_ERROR_NONE; - } + if (lookup) + return STC_ERROR_NONE; //LCOV_EXCL_LINE key = MALLOC0(stc_app_key_s, 1); if (!key) { - STC_LOGE("key allocation failed"); - return STC_ERROR_OUT_OF_MEMORY; + STC_LOGE("key allocation failed"); //LCOV_EXCL_LINE + return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE } value = MALLOC0(stc_app_value_s, 1); if (!value) { - STC_LOGE("value allocation failed"); - FREE(key); - return STC_ERROR_OUT_OF_MEMORY; + STC_LOGE("value allocation failed"); //LCOV_EXCL_LINE + FREE(key); //LCOV_EXCL_LINE + return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE } key->app_id = g_strdup(app_key.app_id); key->pkg_id = g_strdup(app_key.pkg_id); value->type = app_value.type; - value->counter.in_bytes = app_value.counter.in_bytes; - value->counter.out_bytes = app_value.counter.out_bytes; + value->data_usage.in_bytes = app_value.data_usage.in_bytes; + value->data_usage.out_bytes = app_value.data_usage.out_bytes; + g_strlcpy(value->mac, app_value.mac, MAC_ADDRESS_LEN); value->processes = g_tree_new_full(__processes_tree_key_compare, NULL, __processes_tree_key_free, @@ -1286,6 +2615,11 @@ stc_error_e stc_monitor_application_add(const stc_app_key_s app_key, /* create cgroup and update classid */ value->classid = get_classid_by_app_id(app_key.app_id, TRUE); + /* update classid for tethering station based on its mac address */ + if (g_str_has_suffix(app_key.app_id, STC_TETHERING_APP_SUFFIX) && + value->classid != STC_TETHERING_APP_CLASSID) + stc_plugin_tether_set_station_classid(value->mac, value->classid); + g_tree_insert(g_system->apps, key, value); /* add nfacct rule for this classid */ @@ -1295,7 +2629,37 @@ stc_error_e stc_monitor_application_add(const stc_app_key_s app_key, return ret; } -stc_error_e stc_monitor_process_add(const stc_app_key_s app_key, +API stc_error_e stc_monitor_application_remove(const stc_app_key_s app_key) +{ + stc_error_e ret = STC_ERROR_NONE; + stc_app_value_s *app_lookup; + + ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!"); + + app_lookup = __application_lookup(g_system->apps, &app_key); + if (!app_lookup) { + if (STC_DEBUG_LOG) + STC_LOGD("app_key not found"); //LCOV_EXCL_LINE + return STC_ERROR_FAIL; //LCOV_EXCL_LINE + } + + /* remove nfacct rule for this classid */ + __remove_application_monitor((gpointer) &app_key, app_lookup, + stc_get_default_connection()); + + /* remove ristrictions if any */ + __remove_rstns_for_application(app_key.app_id); + + /* remove app_key from the stc-manager */ + if (!g_tree_remove(g_system->apps, &app_key)) { + ret = STC_ERROR_NO_DATA; + STC_LOGE("key not found"); + } + + return ret; +} + +API stc_error_e stc_monitor_process_add(const stc_app_key_s app_key, const stc_process_key_s proc_key, const stc_process_value_s proc_value) { @@ -1309,27 +2673,26 @@ stc_error_e stc_monitor_process_add(const stc_app_key_s app_key, app_lookup = __application_lookup(g_system->apps, &app_key); if (!app_lookup) { - STC_LOGD("app_key not found"); - return STC_ERROR_FAIL; + if (STC_DEBUG_LOG) + STC_LOGD("app_key not found"); //LCOV_EXCL_LINE + return STC_ERROR_FAIL; //LCOV_EXCL_LINE } proc_lookup = __process_lookup(app_lookup->processes, &proc_key); - if (proc_lookup) { - STC_LOGD("proc_key already present"); - return STC_ERROR_NONE; - } + if (proc_lookup) + return STC_ERROR_NONE; //LCOV_EXCL_LINE key = MALLOC0(stc_process_key_s, 1); if (!key) { - STC_LOGE("key allocation failed"); - return STC_ERROR_OUT_OF_MEMORY; + STC_LOGE("key allocation failed"); //LCOV_EXCL_LINE + return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE } value = MALLOC0(stc_process_value_s, 1); if (!value) { - STC_LOGE("value allocation failed"); - FREE(key); - return STC_ERROR_OUT_OF_MEMORY; + STC_LOGE("value allocation failed"); //LCOV_EXCL_LINE + FREE(key); //LCOV_EXCL_LINE + return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE } key->pid = proc_key.pid; @@ -1341,10 +2704,14 @@ stc_error_e stc_monitor_process_add(const stc_app_key_s app_key, /* add pid to application cgroup */ place_pids_to_net_cgroup(proc_key.pid, app_key.app_id); + /* + __apps_tree_printall(); //LCOV_EXCL_LINE + */ + return ret; } -stc_error_e stc_monitor_process_remove(pid_t pid) +API stc_error_e stc_monitor_process_remove(pid_t pid) { stc_error_e ret = STC_ERROR_NONE; stc_process_key_s proc_key = { @@ -1365,10 +2732,15 @@ stc_error_e stc_monitor_process_remove(pid_t pid) if (context.entry_removed) __application_remove_if_empty(context.app_key); + /* + __apps_tree_printall(); //LCOV_EXCL_LINE + */ + return ret; } -stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key, +//LCOV_EXCL_START +API stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key, const stc_process_key_s proc_key, stc_app_state_e ground) { @@ -1393,18 +2765,14 @@ stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key, if (proc_lookup->ground != ground) proc_lookup->ground = ground; - if (ground == STC_APP_STATE_BACKGROUND) { - char *background_app_id = g_strconcat(app_key.app_id, - STC_BACKGROUND_APP_SUFFIX, - NULL); - place_pids_to_net_cgroup(proc_key.pid, background_app_id); - g_free(background_app_id); - } else { + if (ground == STC_APP_STATE_BACKGROUND && __get_background_state()) + place_pids_to_net_cgroup(proc_key.pid, STC_BACKGROUND_APP_ID); + else place_pids_to_net_cgroup(proc_key.pid, app_key.app_id); - } return ret; } +//LCOV_EXCL_STOP void stc_monitor_update_rstn_by_default_connection(void *data) { @@ -1412,6 +2780,7 @@ void stc_monitor_update_rstn_by_default_connection(void *data) default_connection_s *new_connection = (default_connection_s *)data; if (old_connection.path != NULL) { + //LCOV_EXCL_START if (g_system->apps) g_tree_foreach(g_system->apps, __remove_application_monitor, @@ -1421,12 +2790,18 @@ void stc_monitor_update_rstn_by_default_connection(void *data) g_tree_foreach(g_system->rstns, __remove_restriction, (gpointer)&old_connection); + + iptables_flush_chains(); + //LCOV_EXCL_STOP } FREE(old_connection.path); FREE(old_connection.ifname); + FREE(old_connection.tether_iface.ifname); old_connection.type = 0; old_connection.roaming = 0; + old_connection.tether_state = FALSE; + old_connection.tether_iface.type = 0; if (new_connection != NULL && new_connection->path != NULL) { if (g_system->apps) @@ -1440,8 +2815,11 @@ void stc_monitor_update_rstn_by_default_connection(void *data) old_connection.path = g_strdup(new_connection->path); old_connection.ifname = g_strdup(new_connection->ifname); + old_connection.tether_iface.ifname = g_strdup(new_connection->tether_iface.ifname); old_connection.type = new_connection->type; old_connection.roaming = new_connection->roaming; + old_connection.tether_state = new_connection->tether_state; + old_connection.tether_iface.type = new_connection->tether_iface.type; } } @@ -1457,28 +2835,41 @@ stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info) key.app_id = g_strdup(info->app_id); key.ifname = g_strdup(info->ifname); - key.imsi = g_strdup(info->imsi); + key.mac = g_strdup(info->mac); + key.subscriber_id = g_strdup(info->subscriber_id); key.iftype = info->iftype; key.roaming = info->roaming; - value.rst_state = info->rst_state; + value.rstn_type = info->rstn_type; + value.rstn_state = STC_RSTN_STATE_UNKNOWN; value.restriction_id = info->restriction_id; - if (value.rst_state != STC_RESTRICTION_EXCLUDED && info->app_id) + if (info->app_id) value.classid = get_classid_by_app_id(info->app_id, TRUE); else value.classid = STC_UNKNOWN_CLASSID; - value.limit.in_bytes = info->rcv_limit; - value.limit.out_bytes = info->send_limit; - value.warn_limit.in_bytes = info->rcv_warn_limit; - value.warn_limit.out_bytes = info->send_warn_limit; + if (value.classid == STC_BACKGROUND_APP_CLASSID) { + __set_background_state(TRUE); //LCOV_EXCL_LINE + __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state); //LCOV_EXCL_LINE + __process_update_background(); //LCOV_EXCL_LINE + } + + value.limit[STC_RSTN_LIMIT_TYPE_DATA] = info->data_limit; + value.limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info->data_warn_limit; + value.limit[STC_RSTN_LIMIT_TYPE_MONTHLY] = info->monthly_limit; + value.limit[STC_RSTN_LIMIT_TYPE_WEEKLY] = info->weekly_limit; + value.limit[STC_RSTN_LIMIT_TYPE_DAILY] = info->daily_limit; + value.month_start_date = info->month_start_date; + value.month_start_ts = stc_time_get_month_start(time(NULL), + info->month_start_date); ret = __rstn_tree_add(&key, &value, TRUE); FREE(key.app_id); FREE(key.ifname); - FREE(key.imsi); + FREE(key.mac); + FREE(key.subscriber_id); return ret; } @@ -1489,19 +2880,30 @@ stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info) stc_rstn_key_s key = { .app_id = g_strdup(info->app_id), .ifname = g_strdup(info->ifname), - .imsi = g_strdup(info->imsi), + .subscriber_id = g_strdup(info->subscriber_id), .iftype = info->iftype, .roaming = info->roaming, }; + if (!strcmp(key.app_id, STC_BACKGROUND_APP_ID)) { + __set_background_state(FALSE); //LCOV_EXCL_LINE + __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state); //LCOV_EXCL_LINE + __process_update_background(); //LCOV_EXCL_LINE + } + ret = __rstn_tree_remove(&key); FREE(key.app_id); FREE(key.ifname); - FREE(key.imsi); + FREE(key.subscriber_id); return ret; } +API stc_error_e stc_monitor_check_excn_by_cmdline(char *cmdline) +{ + return stc_plugin_check_exception_by_cmdline(cmdline); +} + int stc_monitor_get_counter_socket(void) { return g_system->contr_sock;