+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,