2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <vconf-keys.h>
20 #include <bundle_internal.h>
21 #include <syspopup_caller.h>
24 #include "stc-plugin-monitor.h"
25 #include "stc-plugin-monitor-rstn.h"
26 #include "stc-plugin-monitor-ipt.h"
27 #include "stc-plugin-monitor-time.h"
28 #include "table-counters.h"
29 #include "table-restrictions.h"
30 #include "table-statistics.h"
31 #include "helper-net-cls.h"
32 #include "stc-manager-plugin-tether.h"
34 static void __print_rstn(stc_rstn_data_s *rstn_data)
36 if (STC_DEBUG_LOG && STC_RSTN_LOG) {
37 STC_LOGI("RstnID[%llu] AppID[%s] classid[%u] "
38 "iftype[%d] ifname[%s] rstn_state[%d] "
39 "rstn_type[%d] roaming[%d] subscriber_id[%s]",
40 rstn_data->restriction_id,
41 rstn_data->app_id, rstn_data->classid,
42 rstn_data->iftype, rstn_data->ifname,
43 rstn_data->rstn_state, rstn_data->rstn_type,
44 rstn_data->roaming, rstn_data->subscriber_id);
45 STC_LOGI("month_start_date[%d] limit[%lld] "
46 "warn_limit[%lld] monthly_limit[%lld] "
47 "weekly_limit[%lld] daily_limit[%lld] ",
48 rstn_data->month_start_date,
49 rstn_data->limit[STC_RSTN_LIMIT_TYPE_DATA],
50 rstn_data->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN],
51 rstn_data->limit[STC_RSTN_LIMIT_TYPE_MONTHLY],
52 rstn_data->limit[STC_RSTN_LIMIT_TYPE_WEEKLY],
53 rstn_data->limit[STC_RSTN_LIMIT_TYPE_DAILY]);
54 STC_LOGI("data_counter[%lld] warn_counter[%lld] "
55 "monthly_counter[%lld] weekly_counter[%lld] "
56 "daily_counter[%lld]",
57 rstn_data->counter[STC_RSTN_LIMIT_TYPE_DATA],
58 rstn_data->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN],
59 rstn_data->counter[STC_RSTN_LIMIT_TYPE_MONTHLY],
60 rstn_data->counter[STC_RSTN_LIMIT_TYPE_WEEKLY],
61 rstn_data->counter[STC_RSTN_LIMIT_TYPE_DAILY]);
65 static int __vconf_get_int(const char *key, int *value)
69 ret = vconf_get_int(key, value);
70 if (ret != VCONF_OK) {
71 STC_LOGE("Failed to get vconfkey [%s] value", key);
78 static int __vconf_set_int(const char *key, int value)
82 ret = vconf_set_int(key, value);
83 if (ret != VCONF_OK) {
84 STC_LOGE("Failed to set vconfkey [%s] value", key);
91 static stc_cb_ret_e __statistics_info_cb(const table_statistics_info *info,
94 stc_rstn_cumulative_data_s *stat = (stc_rstn_cumulative_data_s *)user_data;
95 long long int counters = 0;
97 counters = info->cnt.in_bytes + info->cnt.out_bytes;
99 stat->monthly_stat += counters;
100 if (stat->week_start_ts <= info->interval->from)
101 stat->weekly_stat += counters;
102 if (stat->day_start_ts <= info->interval->from)
103 stat->daily_stat += counters;
108 static void __rstn_add_tether_rule(long long int classid, gchar *mac,
109 nfacct_rule_intend intend, stc_iface_type_e iftype)
111 GSList *conn_list = stc_get_connection_list();
112 struct nfacct_rule counter;
113 stc_s *stc = stc_get_manager();
120 for (; conn_list != NULL; conn_list = conn_list->next) {
121 stc_connection_s *conn = conn_list->data;
124 stc->carg = MALLOC0(counter_arg_s, 1);
125 if (stc->carg == NULL)
128 stc->carg->sock = stc_monitor_get_contr_sock();
131 memset(&counter, 0, sizeof(struct nfacct_rule));
133 counter.carg = stc->carg;
134 counter.classid = classid;
135 counter.intend = intend;
137 if (conn->tether_state != TRUE ||
138 conn->tether_iface.ifname == NULL)
141 counter.iftype = conn->tether_iface.type;
142 g_strlcpy(counter.ifname, conn->tether_iface.ifname, MAX_IFACE_LENGTH);
144 /* get connected station ip based on its mac */
145 ret = stc_plugin_tether_get_station_ip(mac, &ipaddr);
146 if (ret != STC_ERROR_NONE)
149 /* tethering iptables rule */
150 stc_monitor_tether_add_in(&counter, ipaddr);
151 stc_monitor_tether_add_out(&counter, ipaddr);
156 static void __rstn_del_tether_rule(long long int classid, gchar *mac,
157 nfacct_rule_intend intend, stc_iface_type_e iftype)
159 GSList *conn_list = stc_get_connection_list();
160 struct nfacct_rule counter;
161 stc_s *stc = stc_get_manager();
168 for (; conn_list != NULL; conn_list = conn_list->next) {
169 stc_connection_s *conn = conn_list->data;
172 stc->carg = MALLOC0(counter_arg_s, 1);
173 if (stc->carg == NULL)
176 stc->carg->sock = stc_monitor_get_contr_sock();
179 memset(&counter, 0, sizeof(struct nfacct_rule));
181 counter.carg = stc->carg;
182 counter.classid = classid;
183 counter.intend = intend;
185 if (conn->tether_state != TRUE ||
186 conn->tether_iface.ifname == NULL)
189 counter.iftype = conn->tether_iface.type;
190 g_strlcpy(counter.ifname, conn->tether_iface.ifname, MAX_IFACE_LENGTH);
192 /* get connected station ip based on its mac */
193 ret = stc_plugin_tether_get_station_ip(mac, &ipaddr);
194 if (ret != STC_ERROR_NONE) {
195 STC_LOGE("Error: no IP found for station mac(%s)", mac);
199 /* tethering iptables rule */
200 stc_monitor_tether_del_in(&counter, ipaddr);
201 stc_monitor_tether_del_out(&counter, ipaddr);
206 static void __rstn_add_ipt_rule(long long int classid, nfacct_rule_intend intend,
207 stc_iface_type_e iftype)
209 GSList *conn_list = stc_get_connection_list();
210 struct nfacct_rule counter;
211 stc_s *stc = stc_get_manager();
216 for (; conn_list != NULL; conn_list = conn_list->next) {
217 stc_connection_s *conn = conn_list->data;
223 stc->carg = MALLOC0(counter_arg_s, 1);
224 if (stc->carg == NULL)
227 stc->carg->sock = stc_monitor_get_contr_sock();
230 memset(&counter, 0, sizeof(struct nfacct_rule));
232 counter.carg = stc->carg;
233 counter.classid = classid;
234 counter.intend = intend;
236 if (conn && conn->tether_iface.ifname != NULL &&
237 classid == STC_TETHERING_APP_CLASSID) {
238 counter.iftype = conn->tether_iface.type;
239 g_strlcpy(counter.ifname, conn->tether_iface.ifname, MAX_IFACE_LENGTH);
241 counter.iftype = iftype;
242 g_strlcpy(counter.ifname, conn->ifname, MAX_IFACE_LENGTH);
246 stc_monitor_ipt_add_in(&counter);
247 stc_monitor_ipt_add_out(&counter);
250 stc_monitor_ip6t_add_in(&counter);
251 stc_monitor_ip6t_add_out(&counter);
255 static void __rstn_del_ipt_rule(long long int classid, nfacct_rule_intend intend,
256 stc_iface_type_e iftype)
258 GSList *conn_list = stc_get_connection_list();
259 struct nfacct_rule counter;
260 stc_s *stc = stc_get_manager();
265 for (; conn_list != NULL; conn_list = conn_list->next) {
266 stc_connection_s *conn = conn_list->data;
272 stc->carg = MALLOC0(counter_arg_s, 1);
273 if (stc->carg == NULL)
276 stc->carg->sock = stc_monitor_get_contr_sock();
279 memset(&counter, 0, sizeof(struct nfacct_rule));
281 counter.carg = stc->carg;
282 counter.classid = classid;
283 counter.intend = intend;
285 if (conn && conn->tether_iface.ifname != NULL &&
286 classid == STC_TETHERING_APP_CLASSID) {
287 counter.iftype = conn->tether_iface.type;
288 g_strlcpy(counter.ifname, conn->tether_iface.ifname, MAX_IFACE_LENGTH);
290 counter.iftype = iftype;
291 g_strlcpy(counter.ifname, conn->ifname, MAX_IFACE_LENGTH);
295 stc_monitor_ipt_del_in(&counter);
296 stc_monitor_ipt_del_out(&counter);
299 stc_monitor_ip6t_del_in(&counter);
300 stc_monitor_ip6t_del_out(&counter);
304 static void __rstn_set_noti_state(int value)
306 int state = STC_RSTN_STATE_INIT;
308 if (__vconf_get_int(VCONFKEY_SETAPPL_DATA_RESTRICTION_INT, &state))
311 if (state == value) {
312 STC_LOGI("No need to change a restriction status: %d", state);
316 __vconf_set_int(VCONFKEY_SETAPPL_DATA_RESTRICTION_INT, value);
319 static void __rstn_tethering_process(enum traffic_restriction_type rstn_type,
320 char *app_id, stc_rstn_data_s *rstn_data, void *data)
322 stc_connection_s *old_connection = (stc_connection_s *)data;
323 stc_connection_s *connection = NULL;
324 char *mac_str = NULL;
326 if (old_connection != NULL) {
327 connection = old_connection;
328 if (connection->tether_state == FALSE)
331 /* rstn not applicable for this interface */
332 if (rstn_data->ifname != NULL &&
333 g_strcmp0("", rstn_data->ifname) != 0 &&
334 g_strcmp0(connection->tether_iface.ifname, rstn_data->ifname) != 0)
337 GSList *conn_list = stc_get_connection_list();
338 for (; conn_list != NULL; conn_list = conn_list->next) {
339 stc_connection_s *conn = conn_list->data;
340 if (conn->tether_state == FALSE)
343 if (rstn_data->ifname != NULL &&
344 g_strcmp0(rstn_data->ifname, "") != 0 &&
345 g_strcmp0(conn->tether_iface.ifname, rstn_data->ifname) == 0)
349 if (connection == NULL)
353 /* in case appid not a tethering app */
354 if (!g_str_has_suffix(app_id, STC_TETHERING_APP_SUFFIX))
357 /* Ignore TOTAL_TETHERING,
358 * Process only station appids */
359 if (rstn_data->classid == STC_TETHERING_APP_CLASSID)
362 /* get the station mac based on classid */
363 stc_plugin_tether_get_station_by_classid(rstn_data->classid, &mac_str);
365 STC_LOGE("Station not found for classid(%d)", rstn_data->classid);
373 table_counters_info info;
374 long long int effective_limit[STC_RSTN_LIMIT_TYPE_MAX] = { 0, };
376 memset(&info, 0, sizeof(table_counters_info));
377 rstn_data->limit_exceeded = 0;
379 if ((rstn_data->counter[STC_RSTN_LIMIT_TYPE_DATA] == 0 &&
380 rstn_data->limit[STC_RSTN_LIMIT_TYPE_DATA] >= 0) ||
381 (rstn_data->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] == 0 &&
382 rstn_data->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] >= 0) ||
383 (rstn_data->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] == 0 &&
384 rstn_data->limit[STC_RSTN_LIMIT_TYPE_MONTHLY] >= 0) ||
385 (rstn_data->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] == 0 &&
386 rstn_data->limit[STC_RSTN_LIMIT_TYPE_WEEKLY] >= 0) ||
387 (rstn_data->counter[STC_RSTN_LIMIT_TYPE_DAILY] == 0 &&
388 rstn_data->limit[STC_RSTN_LIMIT_TYPE_DAILY] >= 0)) {
389 table_counters_get(rstn_data->restriction_id, &info);
391 time_t current_time = 0;
392 stc_rstn_cumulative_data_s stat;
393 table_statistics_select_rule rule;
394 time_t last_week_ts = stc_monitor_get_last_week_ts();
395 time_t last_day_ts = stc_monitor_get_last_day_ts();
397 memset(&stat, 0, sizeof(stc_rstn_cumulative_data_s));
398 stat.month_start_ts = rstn_data->month_start_ts;
399 stat.week_start_ts = last_week_ts;
400 stat.day_start_ts = last_day_ts;
402 memset(&rule, 0, sizeof(table_statistics_select_rule));
403 rule.from = rstn_data->month_start_ts;
405 rule.to = current_time;
406 rule.iftype = rstn_data->iftype;
407 rule.granularity = GRANULARITY;
409 table_statistics_per_app(app_id, &rule, __statistics_info_cb, &stat);
411 rstn_data->counter[STC_RSTN_LIMIT_TYPE_DATA] = info.data_counter;
412 rstn_data->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info.warn_counter;
413 rstn_data->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] = info.monthly_counter + stat.monthly_stat;
414 rstn_data->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] = info.weekly_counter + stat.weekly_stat;
415 rstn_data->counter[STC_RSTN_LIMIT_TYPE_DAILY] = info.daily_counter + stat.daily_stat;
418 for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
419 if (rstn_data->limit[i] >= 0) {
420 effective_limit[i] = rstn_data->limit[i] - rstn_data->counter[i];
422 if (effective_limit[i] < 0)
423 rstn_data->limit_exceeded |= (1 << i);
427 STC_LOGD("Rstn_id[%llu] datausage[%lld]bytes",
428 rstn_data->restriction_id, info.data_counter);
430 if (rstn_data->limit_exceeded != 0 &&
431 rstn_data->limit_exceeded != (1 << STC_RSTN_LIMIT_TYPE_DATA_WARN)) {
432 __rstn_add_tether_rule(rstn_data->classid, mac_str,
433 NFACCT_TETH_BLOCK, rstn_data->iftype);
436 rstn_data->rstn_state = STC_RSTN_STATE_ACTIVATED;
441 __rstn_add_tether_rule(rstn_data->classid, mac_str,
442 NFACCT_TETH_ALLOW, rstn_data->iftype);
444 rstn_data->rstn_state = STC_RSTN_STATE_ACTIVATED;
445 rstn_data->limit_exceeded = 0;
446 rstn_data->limit_notified = 0;
452 __rstn_del_tether_rule(rstn_data->classid, mac_str,
453 NFACCT_TETH_BLOCK, rstn_data->iftype);
455 rstn_data->rstn_state = STC_RSTN_STATE_DEACTIVATED;
456 rstn_data->limit_exceeded = 0;
457 rstn_data->limit_notified = 0;
459 for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++)
460 if (rstn_data->limit[i] >= 0)
461 rstn_data->counter[i] = 0;
470 static void __rstn_process(enum traffic_restriction_type rstn_type,
471 char *app_id, stc_rstn_data_s *rstn_data, void *data)
473 stc_connection_s *old_connection = (stc_connection_s *)data;
474 stc_connection_s *connection = NULL;
476 if (old_connection != NULL) {
477 connection = old_connection;
478 if (connection->ifname == NULL)
481 /* rstn not applicable for this interface */
482 if (rstn_data->ifname != NULL &&
483 g_strcmp0(rstn_data->ifname, "") != 0 &&
484 g_strcmp0(connection->ifname, rstn_data->ifname) != 0 &&
485 g_strcmp0(connection->tether_iface.ifname, rstn_data->ifname) != 0)
488 GSList *conn_list = stc_get_connection_list();
489 for (; conn_list != NULL; conn_list = conn_list->next) {
490 stc_connection_s *conn = conn_list->data;
491 if (conn == NULL || conn->ifname == NULL)
496 if (rstn_data->ifname != NULL &&
497 g_strcmp0(rstn_data->ifname, "") != 0 &&
498 g_strcmp0(conn->ifname, rstn_data->ifname) != 0)
502 if (connection == NULL)
506 /* classid is invalid */
507 if (rstn_data->classid <= STC_UNKNOWN_CLASSID)
510 /* Do not proceed for tethering station appid if found here,
511 * for tethering station apps __rstn_tethering_process() call
513 if (g_str_has_suffix(app_id, STC_TETHERING_APP_SUFFIX) &&
514 rstn_data->classid != STC_TETHERING_APP_CLASSID)
521 table_counters_info info;
522 long long int effective_limit[STC_RSTN_LIMIT_TYPE_MAX] = { 0, };
524 memset(&info, 0, sizeof(table_counters_info));
525 rstn_data->limit_exceeded = 0;
527 if ((rstn_data->counter[STC_RSTN_LIMIT_TYPE_DATA] == 0 &&
528 rstn_data->limit[STC_RSTN_LIMIT_TYPE_DATA] >= 0) ||
529 (rstn_data->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] == 0 &&
530 rstn_data->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] >= 0) ||
531 (rstn_data->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] == 0 &&
532 rstn_data->limit[STC_RSTN_LIMIT_TYPE_MONTHLY] >= 0) ||
533 (rstn_data->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] == 0 &&
534 rstn_data->limit[STC_RSTN_LIMIT_TYPE_WEEKLY] >= 0) ||
535 (rstn_data->counter[STC_RSTN_LIMIT_TYPE_DAILY] == 0 &&
536 rstn_data->limit[STC_RSTN_LIMIT_TYPE_DAILY] >= 0)) {
537 table_counters_get(rstn_data->restriction_id, &info);
539 time_t current_time = 0;
540 stc_rstn_cumulative_data_s stat;
541 table_statistics_select_rule rule;
542 time_t last_week_ts = stc_monitor_get_last_week_ts();
543 time_t last_day_ts = stc_monitor_get_last_day_ts();
545 memset(&stat, 0, sizeof(stc_rstn_cumulative_data_s));
546 stat.month_start_ts = rstn_data->month_start_ts;
547 stat.week_start_ts = last_week_ts;
548 stat.day_start_ts = last_day_ts;
550 memset(&rule, 0, sizeof(table_statistics_select_rule));
551 rule.from = rstn_data->month_start_ts;
553 rule.to = current_time;
554 rule.iftype = rstn_data->iftype;
555 rule.granularity = GRANULARITY;
557 table_statistics_per_app(rstn_data->app_id, &rule, __statistics_info_cb, &stat);
559 rstn_data->counter[STC_RSTN_LIMIT_TYPE_DATA] = info.data_counter;
560 rstn_data->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info.warn_counter;
561 rstn_data->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] = info.monthly_counter + stat.monthly_stat;
562 rstn_data->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] = info.weekly_counter + stat.weekly_stat;
563 rstn_data->counter[STC_RSTN_LIMIT_TYPE_DAILY] = info.daily_counter + stat.daily_stat;
565 if (STC_DEBUG_LOG && STC_RSTN_LOG)
566 STC_LOGD("Rstn counter data[%lld] warn[%lld] "
567 "monthly[%lld] weekly[%lld] daily[%lld]",
568 rstn_data->counter[STC_RSTN_LIMIT_TYPE_DATA],
569 rstn_data->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN],
570 rstn_data->counter[STC_RSTN_LIMIT_TYPE_MONTHLY],
571 rstn_data->counter[STC_RSTN_LIMIT_TYPE_WEEKLY],
572 rstn_data->counter[STC_RSTN_LIMIT_TYPE_DAILY]);
575 for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
576 if (rstn_data->limit[i] >= 0) {
577 effective_limit[i] = rstn_data->limit[i] - rstn_data->counter[i];
579 if (effective_limit[i] < 0)
580 rstn_data->limit_exceeded |= (1 << i);
584 STC_LOGD("Rstn_id[%llu] limit[%lld] datausage[%lld]",
585 rstn_data->restriction_id,
586 rstn_data->limit[STC_RSTN_LIMIT_TYPE_DATA],
589 if (rstn_data->limit_exceeded != 0 &&
590 rstn_data->limit_exceeded != (1 << STC_RSTN_LIMIT_TYPE_DATA_WARN)) {
591 __rstn_add_ipt_rule(rstn_data->classid, NFACCT_BLOCK, rstn_data->iftype);
594 if (rstn_data->classid == STC_BACKGROUND_APP_CLASSID)
595 __rstn_add_ipt_rule(rstn_data->classid, NFACCT_BLOCK, rstn_data->iftype);
597 rstn_data->rstn_state = STC_RSTN_STATE_ACTIVATED;
599 if (STC_DEBUG_LOG && STC_RSTN_LOG) {
600 STC_LOGD("Restriction activated "
601 "[\033[1;36m%d\033[0;m:\033[1;35m%lld\033[0;m]",
602 rstn_data->classid, rstn_data->restriction_id);
607 __rstn_add_ipt_rule(rstn_data->classid, NFACCT_ALLOW,
610 rstn_data->rstn_state = STC_RSTN_STATE_ACTIVATED;
611 rstn_data->limit_exceeded = 0;
612 rstn_data->limit_notified = 0;
614 if (STC_DEBUG_LOG && STC_RSTN_LOG) {
615 STC_LOGD("Restriction activated "
616 "[\033[1;36m%d\033[0;m:\033[1;35m%lld\033[0;m]",
617 rstn_data->classid, rstn_data->restriction_id);
624 if (rstn_data->classid == STC_TETHERING_APP_CLASSID)
625 __rstn_del_ipt_rule(rstn_data->classid, NFACCT_BLOCK,
628 __rstn_del_ipt_rule(rstn_data->classid, rstn_data->rstn_type,
631 rstn_data->rstn_state = STC_RSTN_STATE_DEACTIVATED;
632 rstn_data->limit_exceeded = 0;
633 rstn_data->limit_notified = 0;
635 for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++)
636 if (rstn_data->limit[i] >= 0)
637 rstn_data->counter[i] = 0;
639 __rstn_set_noti_state(STC_RSTN_STATE_UNSET);
641 if (STC_DEBUG_LOG && STC_RSTN_LOG) {
642 STC_LOGD("Restriction deactivated "
643 "[\033[1;36m%d\033[0;m:\033[1;35m%lld\033[0;m]",
644 rstn_data->classid, rstn_data->restriction_id);
653 static void __rstn_add(gpointer data, gpointer user_data)
655 stc_rstn_data_s *rstn_data = (stc_rstn_data_s *)data;
657 /* rstn rule is activated */
658 if (rstn_data->rstn_state == STC_RSTN_STATE_ACTIVATED) {
659 if (STC_DEBUG_LOG && STC_RSTN_LOG) {
660 STC_LOGD("Restriction already activated "
661 "[\033[1;36m%d\033[0;m:\033[1;35m%lld\033[0;m]",
662 rstn_data->classid, rstn_data->restriction_id);
667 if (rstn_data->rstn_type == STC_RSTN_TYPE_ACCEPT) {
668 __rstn_process(RST_EXCLUDE,
669 rstn_data->app_id, rstn_data, user_data);
670 __rstn_tethering_process(RST_EXCLUDE,
671 rstn_data->app_id, rstn_data, user_data);
673 __rstn_process(RST_SET,
674 rstn_data->app_id, rstn_data, user_data);
675 __rstn_tethering_process(RST_SET,
676 rstn_data->app_id, rstn_data, user_data);
679 __print_rstn(rstn_data);
681 if (STC_DEBUG_LOG && STC_RSTN_LOG)
682 STC_LOGD("\033[1;32mRestriction added\033[0;m "
683 "[\033[1;36m%d\033[0;m:\033[1;35m%lld\033[0;m]",
684 rstn_data->classid, rstn_data->restriction_id);
687 static void __rstn_add_by_connection(gpointer key,
688 gpointer value, gpointer data)
690 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
692 g_slist_foreach(rstn_value->rules, __rstn_add, data);
695 static void __rstn_remove(gpointer data, gpointer user_data)
697 stc_rstn_data_s *rstn_data = (stc_rstn_data_s *)data;
699 if (rstn_data->rstn_state == STC_RSTN_STATE_DEACTIVATED) {
700 STC_LOGD("\033[1;31mRestriction already deactivated\033[0;m "
701 "[\033[1;36m%d\033[0;m:\033[1;35m%lld\033[0;m]",
702 rstn_data->classid, rstn_data->restriction_id);
706 __rstn_process(RST_UNSET,
707 rstn_data->app_id, rstn_data, user_data);
708 __rstn_tethering_process(RST_UNSET,
709 rstn_data->app_id, rstn_data, user_data);
711 __print_rstn(rstn_data);
713 if (STC_DEBUG_LOG && STC_RSTN_LOG)
714 STC_LOGD("\033[1;31mRestriction removed\033[0;m "
715 "[\033[1;36m%d\033[0;m:\033[1;35m%lld\033[0;m]",
716 rstn_data->classid, rstn_data->restriction_id);
719 static void __rstn_remove_by_connection(gpointer key,
720 gpointer value, gpointer data)
722 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
724 g_slist_foreach(rstn_value->rules, __rstn_remove, data);
727 static void __rstn_update_counter_data(gpointer data,
730 stc_rstn_data_s *rstn_data = (stc_rstn_data_s *)data;
732 table_counters_info info = {
733 .restriction_id = rstn_data->restriction_id,
734 .data_counter = rstn_data->counter[STC_RSTN_LIMIT_TYPE_DATA],
735 .warn_counter = rstn_data->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN],
736 .monthly_counter = rstn_data->counter[STC_RSTN_LIMIT_TYPE_MONTHLY],
737 .weekly_counter = rstn_data->counter[STC_RSTN_LIMIT_TYPE_WEEKLY],
738 .daily_counter = rstn_data->counter[STC_RSTN_LIMIT_TYPE_DAILY]
741 table_counters_update_counters(&info);
744 static void __rstn_update_counter_value(gpointer key,
745 gpointer value, gpointer data)
747 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
749 g_slist_foreach(rstn_value->rules, __rstn_update_counter_data, NULL);
752 static void __rstn_data_destroy(gpointer data)
754 stc_rstn_data_s *rstn_data = (stc_rstn_data_s *)data;
756 FREE(rstn_data->app_id);
757 FREE(rstn_data->ifname);
758 FREE(rstn_data->subscriber_id);
759 FREE(rstn_data->mac);
764 static gint __rstn_data_comp(gconstpointer a, gconstpointer b)
766 stc_rstn_data_s *da = (stc_rstn_data_s *)a;
767 stc_rstn_data_s *db = (stc_rstn_data_s *)b;
769 if ((da->iftype == db->iftype) &&
770 (g_strcmp0(da->ifname, db->ifname) == 0) &&
771 (g_strcmp0(da->subscriber_id, db->subscriber_id) == 0) &&
772 (da->roaming == db->roaming))
778 static void __rstn_foreach_print(gpointer data, gpointer user_data)
780 stc_rstn_data_s *rstn_data = (stc_rstn_data_s *)data;
781 __print_rstn(rstn_data);
784 static stc_error_e __rstn_data_remove(stc_rstn_data_s *data)
786 stc_rstn_value_s *lookup_value;
788 stc_rstn_data_s *lookup_data;
789 GHashTable *rstns = stc_monitor_get_system_rstns();
792 return STC_ERROR_NO_DATA;
794 lookup_value = g_hash_table_lookup(rstns, GUINT_TO_POINTER(data->classid));
796 if (STC_DEBUG_LOG && STC_RSTN_LOG)
797 STC_LOGE("Restriction not found [\033[1;36m%d\033[0;m]",
799 return STC_ERROR_NO_DATA;
802 lookup_list = g_slist_find_custom(lookup_value->rules,
803 data, __rstn_data_comp);
805 if (STC_DEBUG_LOG && STC_RSTN_LOG)
806 STC_LOGE("Restriction not found [%d:%s:%s:%d]",
807 data->iftype, data->ifname,
808 data->subscriber_id, data->roaming);
809 return STC_ERROR_NO_DATA;
812 lookup_data = lookup_list->data;
814 /* remove counter also */
815 table_counters_delete(lookup_data->restriction_id);
816 __rstn_remove(lookup_data, NULL);
818 lookup_value->rules = g_slist_remove(lookup_value->rules,
820 __rstn_data_destroy(lookup_data);
822 g_slist_foreach(lookup_value->rules, __rstn_foreach_print, NULL);
824 if (!lookup_value->rules)
825 g_hash_table_remove(rstns, GUINT_TO_POINTER(data->classid));
827 return STC_ERROR_NONE;
830 static stc_error_e __rstn_data_add(stc_rstn_data_s *data)
833 stc_rstn_value_s *lookup_value;
834 stc_rstn_value_s *rstn_value;
835 stc_rstn_data_s *rstn_data;
837 GHashTable *rstns = stc_monitor_get_system_rstns();
840 return STC_ERROR_NO_DATA;
842 lookup_value = g_hash_table_lookup(rstns, GUINT_TO_POINTER(data->classid));
844 rstn_value = MALLOC0(stc_rstn_value_s, 1);
846 if (STC_DEBUG_LOG && STC_RSTN_LOG)
847 STC_LOGE("Rstn_value allocation failed");
848 return STC_ERROR_OUT_OF_MEMORY;
851 g_hash_table_insert(rstns, GUINT_TO_POINTER(data->classid),
854 rstn_value = lookup_value;
857 lookup_list = g_slist_find_custom(rstn_value->rules,
858 data, __rstn_data_comp);
860 rstn_data = MALLOC0(stc_rstn_data_s, 1);
862 if (STC_DEBUG_LOG && STC_RSTN_LOG)
863 STC_LOGE("Rstn_data allocation failed");
864 return STC_ERROR_OUT_OF_MEMORY;
867 rstn_value->rules = g_slist_append(rstn_value->rules, rstn_data);
869 rstn_data = lookup_list->data;
872 rstn_data->classid = data->classid;
873 FREE(rstn_data->app_id);
874 rstn_data->app_id = g_strdup(data->app_id);
875 rstn_data->iftype = data->iftype;
876 FREE(rstn_data->ifname);
877 rstn_data->ifname = g_strdup(data->ifname);
878 FREE(rstn_data->subscriber_id);
879 rstn_data->subscriber_id = g_strdup(data->subscriber_id);
880 rstn_data->roaming = data->roaming;
881 FREE(rstn_data->mac);
882 rstn_data->mac = g_strdup(data->mac);
884 rstn_data->restriction_id = data->restriction_id;
885 rstn_data->rstn_state = data->rstn_state;
886 rstn_data->rstn_type = data->rstn_type;
888 for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
889 rstn_data->limit[i] = data->limit[i];
890 rstn_data->counter[i] = 0;
893 rstn_data->limit_exceeded = 0;
894 rstn_data->limit_notified = 0;
895 rstn_data->month_start_date = data->month_start_date;
896 rstn_data->month_start_ts = data->month_start_ts;
898 __rstn_add(rstn_data, NULL);
900 g_slist_foreach(rstn_value->rules, __rstn_foreach_print, NULL);
902 return STC_ERROR_NONE;
905 static void __rstn_value_destroy(gpointer data)
907 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)data;
909 g_slist_free_full(rstn_value->rules, __rstn_data_destroy);
914 static stc_cb_ret_e __rstn_insert_cb(const table_restrictions_info *info,
917 stc_cb_ret_e ret = STC_CONTINUE;
918 stc_rstn_data_s data;
920 memset(&data, 0, sizeof(stc_rstn_data_s));
923 data.classid = get_classid_by_app_id(info->app_id, TRUE);
924 data.app_id = info->app_id;
926 data.classid = STC_UNKNOWN_CLASSID;
928 data.iftype = info->iftype;
929 data.ifname = info->ifname;
930 data.subscriber_id = info->subscriber_id;
931 data.roaming = info->roaming;
933 data.rstn_type = info->rstn_type;
934 data.rstn_state = STC_RSTN_STATE_UNKNOWN;
935 data.restriction_id = info->restriction_id;
937 data.limit[STC_RSTN_LIMIT_TYPE_DATA] = info->data_limit;
938 data.limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info->data_warn_limit;
939 data.limit[STC_RSTN_LIMIT_TYPE_MONTHLY] = info->monthly_limit;
940 data.limit[STC_RSTN_LIMIT_TYPE_WEEKLY] = info->weekly_limit;
941 data.limit[STC_RSTN_LIMIT_TYPE_DAILY] = info->daily_limit;
943 if (__rstn_data_add(&data) != STC_ERROR_NONE)
949 static void __rstn_update_counter(classid_bytes_context_s *context,
952 stc_rstn_value_s *lookup;
953 GHashTable *rstns = stc_monitor_get_system_rstns();
958 lookup = g_hash_table_lookup(rstns, GUINT_TO_POINTER(classid));
960 context->counter->classid = classid;
961 g_slist_foreach(lookup->rules,
962 stc_monitor_rstn_update_counter,
967 static void __rstn_action_when_limit_exceeded_tethering(stc_rstn_data_s *rstn_data,
968 classid_bytes_context_s *context)
970 char *mac_str = NULL;
971 struct nfacct_rule *counter = context->counter;
973 /* get the station mac based on classid */
974 stc_plugin_tether_get_station_by_classid(counter->classid, &mac_str);
976 STC_LOGE("Station not found for classid(%d)", counter->classid);
980 STC_LOGI("Station mac[%s] classid[%u] iftype[%u] iotype[%d] "
981 "intend[%d] ifname[%s] bytes[%lld]", mac_str,
982 counter->classid, counter->iftype, counter->iotype,
983 counter->intend, counter->ifname, context->bytes);
985 /* Block tethering station immediately */
986 counter->intend = NFACCT_TETH_BLOCK;
987 __rstn_del_tether_rule(counter->classid, mac_str,
988 NFACCT_TETH_BLOCK, rstn_data->iftype);
990 __rstn_add_tether_rule(counter->classid, mac_str,
991 NFACCT_TETH_BLOCK, rstn_data->iftype);
992 counter->intend = NFACCT_TETH_COUNTER;
997 static void __reset_time_counter_foreach_rstn_data(gpointer data,
1000 stc_rstn_data_s *rstn_data = (stc_rstn_data_s *)data;
1001 reset_time_limits_context_s *context =
1002 (reset_time_limits_context_s *)user_data;
1004 time_t now_month_start_ts;
1006 if (rstn_data->month_start_date == 0) {
1007 table_counters_info info;
1008 memset(&info, 0, sizeof(table_counters_info));
1009 table_counters_get_timestamps(rstn_data->restriction_id, &info);
1011 if (info.month_start_date == 0)
1012 rstn_data->month_start_date = 1;
1014 rstn_data->month_start_date = info.month_start_date;
1015 rstn_data->month_start_ts = info.month_start_ts;
1018 now_month_start_ts =
1019 stc_time_get_month_start(context->now,
1020 rstn_data->month_start_date);
1022 if (rstn_data->month_start_ts != now_month_start_ts) {
1023 rstn_data->month_start_ts = now_month_start_ts;
1024 context->month_start_ts = now_month_start_ts;
1025 context->is_updated |= (1 << STC_RSTN_LIMIT_TYPE_MONTHLY);
1028 if (context->is_updated) {
1029 table_counters_info info;
1030 memset(&info, 0, sizeof(table_counters_info));
1032 info.restriction_id = rstn_data->restriction_id;
1033 info.month_start_date = rstn_data->month_start_date;
1034 info.month_start_ts = rstn_data->month_start_ts;
1035 info.week_start_ts = context->week_start_ts;
1036 info.day_start_ts = context->day_start_ts;
1038 table_counters_update_timestamps(&info);
1041 for (i = STC_RSTN_LIMIT_TYPE_MONTHLY; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
1043 if ((context->is_updated) & (1 << i)) {
1045 rstn_data->counter[i] = 0;
1047 if (rstn_data->limit_exceeded & (1 << i)) {
1048 /* remove iptables rule */
1049 GSList *conn_list = stc_get_connection_list();
1050 struct nfacct_rule counter;
1051 stc_s *stc = stc_get_manager();
1053 STC_LOGE("Can't get stc data");
1054 goto try_next_callback;
1058 stc->carg = MALLOC0(counter_arg_s, 1);
1059 if (stc->carg == NULL) {
1060 goto try_next_callback;
1063 stc->carg->sock = stc_monitor_get_contr_sock();
1066 for (; conn_list != NULL; conn_list = conn_list->next) {
1067 stc_connection_s *conn = conn_list->data;
1069 memset(&counter, 0, sizeof(struct nfacct_rule));
1071 counter.carg = stc->carg;
1072 counter.classid = rstn_data->classid;
1073 counter.intend = NFACCT_BLOCK;
1074 counter.iftype = rstn_data->iftype;
1075 g_strlcpy(counter.ifname, conn->ifname, MAX_IFACE_LENGTH);
1078 stc_monitor_ipt_del_in(&counter);
1079 stc_monitor_ipt_del_out(&counter);
1081 /* ip6tables rule */
1082 stc_monitor_ip6t_del_in(&counter);
1083 stc_monitor_ip6t_del_out(&counter);
1085 rstn_data->rstn_state = STC_RSTN_STATE_DEACTIVATED;
1086 rstn_data->limit_exceeded &= ~(1 << i);
1087 rstn_data->limit_notified &= ~(1 << i);
1097 static void __reset_time_counter_foreach_rstn_value(gpointer key,
1101 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1102 g_slist_foreach(rstn_value->rules,
1103 __reset_time_counter_foreach_rstn_data, data);
1106 API stc_error_e stc_plugin_monitor_rstn_add(const table_restrictions_info *info)
1108 stc_rstn_data_s data;
1110 memset(&data, 0, sizeof(stc_rstn_data_s));
1113 data.classid = get_classid_by_app_id(info->app_id, TRUE);
1114 data.app_id = info->app_id;
1116 data.classid = STC_UNKNOWN_CLASSID;
1118 if (data.classid == STC_BACKGROUND_APP_CLASSID) {
1119 stc_monitor_set_background_state(TRUE);
1120 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, TRUE);
1123 data.iftype = info->iftype;
1124 data.ifname = info->ifname;
1125 data.subscriber_id = info->subscriber_id;
1126 data.roaming = info->roaming;
1127 data.mac = info->mac;
1129 data.rstn_type = info->rstn_type;
1130 data.rstn_state = STC_RSTN_STATE_UNKNOWN;
1131 data.restriction_id = info->restriction_id;
1133 data.limit[STC_RSTN_LIMIT_TYPE_DATA] = info->data_limit;
1134 data.limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info->data_warn_limit;
1135 data.limit[STC_RSTN_LIMIT_TYPE_MONTHLY] = info->monthly_limit;
1136 data.limit[STC_RSTN_LIMIT_TYPE_WEEKLY] = info->weekly_limit;
1137 data.limit[STC_RSTN_LIMIT_TYPE_DAILY] = info->daily_limit;
1138 data.month_start_date = info->month_start_date;
1139 data.month_start_ts = stc_time_get_month_start(time(NULL),
1140 info->month_start_date);
1142 return __rstn_data_add(&data);
1145 API stc_error_e stc_plugin_monitor_rstn_remove(const table_restrictions_info *info)
1147 stc_rstn_data_s data;
1149 memset(&data, 0, sizeof(stc_rstn_data_s));
1151 data.classid = get_classid_by_app_id(info->app_id, TRUE);
1152 data.app_id = info->app_id;
1154 data.iftype = info->iftype;
1155 data.ifname = info->ifname;
1156 data.subscriber_id = info->subscriber_id;
1157 data.roaming = info->roaming;
1159 if (g_strcmp0(info->app_id, STC_TOTAL_BACKGROUND) == 0) {
1160 stc_monitor_set_background_state(FALSE);
1161 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, FALSE);
1164 return __rstn_data_remove(&data);
1167 void stc_monitor_rstn_reset_time_counters_if_required(void)
1169 reset_time_limits_context_s context;
1170 GHashTable *rstns = stc_monitor_get_system_rstns();
1171 time_t last_week_ts = stc_monitor_get_last_week_ts();
1172 time_t last_day_ts = stc_monitor_get_last_day_ts();
1174 context.now = time(NULL);
1175 context.week_start_ts = stc_time_get_week_start(context.now);
1176 context.day_start_ts = stc_time_get_day_start(context.now);
1177 context.is_updated = 0;
1179 if (last_week_ts != context.week_start_ts) {
1180 stc_monitor_set_last_week_ts(context.week_start_ts);
1181 context.is_updated |= (1 << STC_RSTN_LIMIT_TYPE_WEEKLY);
1184 if (last_day_ts != context.day_start_ts) {
1185 stc_monitor_set_last_day_ts(context.day_start_ts);
1186 context.is_updated |= (1 << STC_RSTN_LIMIT_TYPE_DAILY);
1190 g_hash_table_foreach(rstns,
1191 __reset_time_counter_foreach_rstn_value,
1194 if (context.is_updated)
1195 STC_LOGD("Counter reset completed month_start[%ld] "
1196 "week_start[%ld] day_start[%ld]",
1197 context.month_start_ts, last_week_ts, last_day_ts);
1201 void stc_monitor_rstn_update_counter(gpointer data,
1206 stc_rstn_data_s *rstn_data = (stc_rstn_data_s *)data;
1207 classid_bytes_context_s *context = (classid_bytes_context_s *)user_data;
1208 GSList *conn_list = stc_get_connection_list();
1210 for (; conn_list != NULL; conn_list = conn_list->next) {
1211 stc_connection_s *conn = conn_list->data;
1213 if (rstn_data->iftype != context->counter->iftype)
1216 if (rstn_data->ifname != NULL &&
1217 g_strcmp0(rstn_data->ifname, "") &&
1218 g_strcmp0(rstn_data->ifname, context->counter->ifname) != 0)
1221 if (rstn_data->subscriber_id != NULL &&
1222 g_strcmp0(rstn_data->subscriber_id, "") &&
1223 g_strcmp0(rstn_data->subscriber_id, conn->subscriber_id) != 0)
1226 is_roaming = (rstn_data->roaming == STC_ROAMING_ENABLE) ? 1 : 0;
1227 if (is_roaming != conn->roaming)
1230 if (rstn_data->limit_exceeded != 0) {
1231 context->data_limit_exceeded = TRUE;
1235 switch (context->counter->iotype) {
1236 case NFACCT_COUNTER_IN:
1237 case NFACCT_COUNTER_OUT:
1238 if ((rstn_data->counter[STC_RSTN_LIMIT_TYPE_DATA] == 0 &&
1239 rstn_data->limit[STC_RSTN_LIMIT_TYPE_DATA] >= 0) ||
1240 (rstn_data->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] == 0 &&
1241 rstn_data->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] >= 0) ||
1242 (rstn_data->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] == 0 &&
1243 rstn_data->limit[STC_RSTN_LIMIT_TYPE_MONTHLY] >= 0) ||
1244 (rstn_data->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] == 0 &&
1245 rstn_data->limit[STC_RSTN_LIMIT_TYPE_WEEKLY] >= 0) ||
1246 (rstn_data->counter[STC_RSTN_LIMIT_TYPE_DAILY] == 0 &&
1247 rstn_data->limit[STC_RSTN_LIMIT_TYPE_DAILY] >= 0)) {
1248 table_counters_info info;
1249 memset(&info, 0, sizeof(table_counters_info));
1250 table_counters_get(rstn_data->restriction_id, &info);
1252 rstn_data->counter[STC_RSTN_LIMIT_TYPE_DATA] = info.data_counter;
1253 rstn_data->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info.warn_counter;
1254 rstn_data->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] = info.monthly_counter;
1255 rstn_data->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] = info.weekly_counter;
1256 rstn_data->counter[STC_RSTN_LIMIT_TYPE_DAILY] = info.daily_counter;
1259 for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
1260 if (rstn_data->limit[i] >= 0 &&
1261 !(rstn_data->limit_notified & (1 << i))) {
1262 rstn_data->counter[i] += context->bytes;
1263 if (rstn_data->limit[i] <= rstn_data->counter[i])
1264 stc_monitor_rstn_action_when_limit_exceeded(i,
1270 stc_monitor_set_rstns_updated(TRUE);
1271 __print_rstn(rstn_data);
1274 STC_LOGE("Unknown iotype");
1279 void stc_monitor_rstn_update_iface_counter(classid_bytes_context_s *context)
1281 switch (context->counter->iftype) {
1282 case STC_IFACE_DATACALL:
1283 __rstn_update_counter(context, STC_TOTAL_DATACALL_CLASSID);
1285 case STC_IFACE_WIFI:
1286 __rstn_update_counter(context, STC_TOTAL_WIFI_CLASSID);
1287 __rstn_update_counter(context, STC_TETHERING_APP_CLASSID);
1289 case STC_IFACE_BLUETOOTH:
1290 __rstn_update_counter(context, STC_TOTAL_BLUETOOTH_CLASSID);
1291 __rstn_update_counter(context, STC_TETHERING_APP_CLASSID);
1294 __rstn_update_counter(context, STC_TETHERING_APP_CLASSID);
1297 __rstn_update_counter(context, STC_TETHERING_APP_CLASSID);
1304 static int stc_plugin_monitor_launch_rstn_popup(const char *content,
1305 const char *type, const char *app_id,
1306 const char *iftype, const char *limit)
1309 bundle *b = bundle_create();
1311 bundle_add(b, "_SYSPOPUP_CONTENT_", content);
1312 bundle_add(b, "_SYSPOPUP_TYPE_", type);
1313 bundle_add(b, "_APP_ID_", app_id);
1314 bundle_add(b, "_IF_TYPE_", iftype);
1316 if (g_strcmp0(type, "warning_noti") == 0) {
1317 bundle_add(b, "_WARN_LIMIT_", limit);
1318 STC_LOGD("Warn message : content[%s] type[%s] app_id[%s] limit[%s]",
1319 content, type, app_id, limit);
1321 bundle_add(b, "_RESTRICTION_LIMIT_", limit);
1322 STC_LOGD("Restriction message : content[%s] type[%s] app_id[%s] limit[%s]",
1323 content, type, app_id, limit);
1326 ret = syspopup_launch("net-popup", b);
1333 void stc_monitor_rstn_action_when_limit_exceeded(stc_rstn_limit_type_e limit_type,
1334 stc_rstn_data_s *rstn_data,
1335 classid_bytes_context_s *context)
1338 char iftype[MAX_INT_LENGTH] = { 0, };
1339 char byte[MAX_INT_LENGTH] = { 0, };
1340 const char *signal_name = NULL;
1341 const char *net_popup_content = NULL;
1342 const char *net_popup_type = NULL;
1343 stc_s *stc = (stc_s *)stc_get_manager();
1346 STC_LOGE("Failed to get stc data");
1350 switch (limit_type) {
1351 case STC_RSTN_LIMIT_TYPE_DATA_WARN:
1353 signal_name = "WarnThresholdCrossed";
1354 net_popup_content = "warn threshold crossed";
1355 net_popup_type = "warning_noti";
1358 case STC_RSTN_LIMIT_TYPE_DATA:
1359 case STC_RSTN_LIMIT_TYPE_MONTHLY:
1360 case STC_RSTN_LIMIT_TYPE_WEEKLY:
1361 case STC_RSTN_LIMIT_TYPE_DAILY:
1363 signal_name = "RestrictionThresholdCrossed";
1364 net_popup_content = "restriction threshold crossed";
1365 net_popup_type = "restriction_noti";
1367 /* Apply restriction for tethering apps if app_id is of tethering client
1368 * otherwise do the normal iptables rule */
1369 if (context->counter->intend == NFACCT_TETH_COUNTER) {
1371 if (g_str_has_suffix(rstn_data->app_id, STC_TETHERING_APP_SUFFIX) &&
1372 rstn_data->classid != STC_TETHERING_APP_CLASSID) {
1373 __rstn_action_when_limit_exceeded_tethering(rstn_data,
1378 /* block immediately */
1379 context->counter->intend = NFACCT_BLOCK;
1380 stc_monitor_ipt_del_in(context->counter);
1381 stc_monitor_ipt_del_out(context->counter);
1382 stc_monitor_ipt_add_in(context->counter);
1383 stc_monitor_ipt_add_out(context->counter);
1385 stc_monitor_ip6t_del_in(context->counter);
1386 stc_monitor_ip6t_del_out(context->counter);
1387 stc_monitor_ip6t_add_in(context->counter);
1388 stc_monitor_ip6t_add_out(context->counter);
1389 context->counter->intend = NFACCT_COUNTER;
1392 rstn_data->limit_exceeded |= (1 << limit_type);
1393 __rstn_set_noti_state(STC_RSTN_STATE_SET);
1400 if (signal_name == NULL) {
1401 STC_LOGE("Invalid parameter: limit_type");
1406 rv = stc_manager_dbus_emit_signal(stc->connection,
1407 STC_DBUS_SERVICE_RESTRICTION_PATH,
1408 STC_DBUS_INTERFACE_RESTRICTION,
1410 g_variant_new("(si)",
1412 rstn_data->iftype));
1415 rstn_data->limit_notified |= (1 << limit_type);
1417 snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_data->iftype);
1418 snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_data->limit[limit_type]);
1419 stc_plugin_monitor_launch_rstn_popup(net_popup_content,
1420 net_popup_type, rstn_data->app_id, iftype, byte);
1422 if (STC_DEBUG_LOG && STC_RSTN_LOG)
1423 STC_LOGD("Limit exceeded [\033[0;31m%s\033[0;m:%d]",
1424 net_popup_content, limit_type);
1427 gboolean stc_monitor_rstn_flush_contr_to_db(gpointer user_data)
1429 time_t current_time = 0;
1430 stc_s *stc = stc_get_manager();
1431 GHashTable *rstns = stc_monitor_get_system_rstns();
1432 gboolean rstns_updated = stc_monitor_get_rstns_updated();
1434 if (stc && stc->carg)
1435 current_time = stc->carg->last_run_time;
1437 if (rstns_updated == FALSE)
1438 return G_SOURCE_REMOVE;
1440 stc_monitor_set_rstns_updated(FALSE);
1443 g_hash_table_foreach(rstns,
1444 __rstn_update_counter_value,
1447 STC_LOGI("Flushed rstns counters to database");
1448 return G_SOURCE_REMOVE;
1451 void stc_monitor_rstn_add_for_app(uint32_t classid)
1453 stc_rstn_value_s *lookup_value;
1454 GHashTable *rstns = stc_monitor_get_system_rstns();
1459 lookup_value = g_hash_table_lookup(rstns, GUINT_TO_POINTER(classid));
1460 if (!lookup_value) {
1461 if (STC_DEBUG_LOG && STC_RSTN_LOG)
1462 STC_LOGD("Restriction not found [\033[1;36m%d\033[0;m]",
1467 g_slist_foreach(lookup_value->rules, __rstn_add, NULL);
1470 void stc_monitor_rstn_add_by_connection(stc_connection_s *conn)
1472 GHashTable *rstns = stc_monitor_get_system_rstns();
1477 g_hash_table_foreach(rstns, __rstn_add_by_connection, conn);
1480 void stc_monitor_rstn_remove_for_app(uint32_t classid)
1482 stc_rstn_value_s *lookup_value;
1483 GHashTable *rstns = stc_monitor_get_system_rstns();
1488 lookup_value = g_hash_table_lookup(rstns, GUINT_TO_POINTER(classid));
1489 if (!lookup_value) {
1490 if (STC_DEBUG_LOG && STC_RSTN_LOG)
1491 STC_LOGD("Restriction not found [\033[1;36m%d\033[0;m]",
1496 g_slist_foreach(lookup_value->rules, __rstn_remove, NULL);
1499 void stc_monitor_rstn_remove_by_connection(stc_connection_s *conn)
1501 GHashTable *rstns = stc_monitor_get_system_rstns();
1506 g_hash_table_foreach(rstns, __rstn_remove_by_connection, conn);
1509 void stc_monitor_rstns_load(void)
1511 table_restrictions_foreach(__rstn_insert_cb, NULL);
1513 /* __rstn_tree_printall(); */
1516 GHashTable *stc_monitor_rstns_init(void)
1518 return g_hash_table_new_full(g_direct_hash, g_direct_equal,
1519 NULL, __rstn_value_destroy);