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.
17 #include <linux/netlink.h>
19 #include <vconf-keys.h>
21 #include "stc-default-connection.h"
22 #include "helper-nl.h"
23 #include "helper-nfacct-rule.h"
24 #include "helper-net-cls.h"
25 #include "helper-cgroup.h"
27 #include "table-statistics.h"
28 #include "table-counters.h"
29 #include "stc-monitor.h"
30 #include "stc-manager-plugin-appstatus.h"
31 #include "stc-manager-plugin-exception.h"
32 #include "stc-manager-plugin-popup.h"
35 #define MAX_INT_LENGTH 128
36 #define VCONFKEY_STC_BACKGROUND_STATE "db/stc/background_state"
40 time_t month_start_ts;
44 } reset_time_limits_context_s;
47 stc_app_key_s *app_key;
48 stc_process_key_s *proc_key;
49 gboolean entry_removed;
50 } remove_pid_context_s;
53 struct nfacct_rule *counter;
55 gboolean data_limit_exceeded;
56 } classid_bytes_context_s;
58 static stc_system_s *g_system = NULL;
60 static nfacct_rule_jump __get_jump_by_intend(struct nfacct_rule *counter)
62 if (counter->intend == NFACCT_WARN)
63 return NFACCT_JUMP_ACCEPT;
64 else if (counter->intend == NFACCT_BLOCK)
65 return NFACCT_JUMP_REJECT;
67 return NFACCT_JUMP_UNKNOWN;
70 static stc_error_e __add_iptables_in(struct nfacct_rule *counter)
73 return STC_ERROR_INVALID_PARAMETER;
75 counter->action = NFACCT_ACTION_INSERT;
76 counter->iotype = NFACCT_COUNTER_IN;
77 counter->jump = __get_jump_by_intend(counter);
78 counter->iptype = NFACCT_TYPE_IPV4;
79 counter->send_limit = 0;
80 counter->rcv_limit = 0;
82 return produce_net_rule(counter);
85 static stc_error_e __add_iptables_out(struct nfacct_rule *counter)
88 return STC_ERROR_INVALID_PARAMETER;
90 counter->action = NFACCT_ACTION_INSERT;
91 counter->iotype = NFACCT_COUNTER_OUT;
92 counter->jump = __get_jump_by_intend(counter);
93 counter->iptype = NFACCT_TYPE_IPV4;
94 counter->send_limit = 0;
95 counter->rcv_limit = 0;
97 return produce_net_rule(counter);
100 static stc_error_e __del_iptables_in(struct nfacct_rule *counter)
103 return STC_ERROR_INVALID_PARAMETER;
105 counter->action = NFACCT_ACTION_DELETE;
106 counter->iotype = NFACCT_COUNTER_IN;
107 counter->jump = __get_jump_by_intend(counter);
108 counter->iptype = NFACCT_TYPE_IPV4;
109 counter->send_limit = 0;
110 counter->rcv_limit = 0;
112 return produce_net_rule(counter);
115 static stc_error_e __del_iptables_out(struct nfacct_rule *counter)
118 return STC_ERROR_INVALID_PARAMETER;
120 counter->action = NFACCT_ACTION_DELETE;
121 counter->iotype = NFACCT_COUNTER_OUT;
122 counter->jump = __get_jump_by_intend(counter);
123 counter->iptype = NFACCT_TYPE_IPV4;
124 counter->send_limit = 0;
125 counter->rcv_limit = 0;
127 return produce_net_rule(counter);
130 static stc_error_e __add_ip6tables_in(struct nfacct_rule *counter)
133 return STC_ERROR_INVALID_PARAMETER;
135 counter->action = NFACCT_ACTION_INSERT;
136 counter->iotype = NFACCT_COUNTER_IN;
137 counter->jump = __get_jump_by_intend(counter);
138 counter->iptype = NFACCT_TYPE_IPV6;
139 counter->send_limit = 0;
140 counter->rcv_limit = 0;
142 return produce_net_rule(counter);
145 static stc_error_e __add_ip6tables_out(struct nfacct_rule *counter)
148 return STC_ERROR_INVALID_PARAMETER;
150 counter->action = NFACCT_ACTION_INSERT;
151 counter->iotype = NFACCT_COUNTER_OUT;
152 counter->jump = __get_jump_by_intend(counter);
153 counter->iptype = NFACCT_TYPE_IPV6;
154 counter->send_limit = 0;
155 counter->rcv_limit = 0;
157 return produce_net_rule(counter);
160 static stc_error_e __del_ip6tables_in(struct nfacct_rule *counter)
163 return STC_ERROR_INVALID_PARAMETER;
165 counter->action = NFACCT_ACTION_DELETE;
166 counter->iotype = NFACCT_COUNTER_IN;
167 counter->jump = __get_jump_by_intend(counter);
168 counter->iptype = NFACCT_TYPE_IPV6;
169 counter->send_limit = 0;
170 counter->rcv_limit = 0;
172 return produce_net_rule(counter);
175 static stc_error_e __del_ip6tables_out(struct nfacct_rule *counter)
178 return STC_ERROR_INVALID_PARAMETER;
180 counter->action = NFACCT_ACTION_DELETE;
181 counter->iotype = NFACCT_COUNTER_OUT;
182 counter->jump = __get_jump_by_intend(counter);
183 counter->iptype = NFACCT_TYPE_IPV6;
184 counter->send_limit = 0;
185 counter->rcv_limit = 0;
187 return produce_net_rule(counter);
190 static int __processes_tree_key_compare(gconstpointer a, gconstpointer b,
191 gpointer UNUSED user_data)
193 stc_process_key_s *key_a = (stc_process_key_s *)a;
194 stc_process_key_s *key_b = (stc_process_key_s *)b;
196 return key_a->pid - key_b->pid;
199 static void __processes_tree_value_free(gpointer data)
201 stc_process_value_s *value = (stc_process_value_s *)data;
206 static void __processes_tree_key_free(gpointer data)
208 stc_process_key_s *key = (stc_process_key_s *)data;
213 static int __apps_tree_key_compare(gconstpointer a, gconstpointer b,
214 gpointer UNUSED user_data)
216 stc_app_key_s *key_a = (stc_app_key_s *)a;
217 stc_app_key_s *key_b = (stc_app_key_s *)b;
220 ret = g_strcmp0(key_a->pkg_id, key_b->pkg_id);
224 return g_strcmp0(key_a->app_id, key_b->app_id);
227 static void __apps_tree_value_free(gpointer data)
229 stc_app_value_s *value = (stc_app_value_s *)data;
231 g_tree_destroy(value->processes);
232 value->processes = NULL;
237 static void __apps_tree_key_free(gpointer data)
239 stc_app_key_s *key = (stc_app_key_s *)data;
246 static int __rstns_tree_key_compare(gconstpointer a, gconstpointer b,
247 gpointer UNUSED user_data)
249 stc_rstn_key_s *key_a = (stc_rstn_key_s *)a;
250 stc_rstn_key_s *key_b = (stc_rstn_key_s *)b;
253 ret = g_strcmp0(key_a->app_id, key_b->app_id);
257 ret = g_strcmp0(key_a->ifname, key_b->ifname);
261 ret = g_strcmp0(key_a->subscriber_id, key_b->subscriber_id);
265 ret = key_a->iftype - key_b->iftype;
272 static void __rstns_tree_value_free(gpointer data)
274 stc_rstn_value_s *value = (stc_rstn_value_s *)data;
279 static void __rstns_tree_key_free(gpointer data)
281 stc_rstn_key_s *key = (stc_rstn_key_s *)data;
285 FREE(key->subscriber_id);
289 static gboolean __processes_tree_foreach_print(gpointer key, gpointer value,
292 stc_process_key_s *proc_key = (stc_process_key_s *)key;
293 stc_process_value_s *proc_value = (stc_process_value_s *)value;
295 STC_LOGD("Process entry => PID [\033[1;33m%d\033[0;m], Ground state [%d]",
296 proc_key->pid, proc_value->ground);
300 static void __processes_tree_printall(GTree *processes)
302 g_tree_foreach(processes, __processes_tree_foreach_print, NULL);
305 static gboolean __apps_tree_foreach_print(gpointer key, gpointer value,
308 stc_app_key_s *app_key = (stc_app_key_s *)key;
309 stc_app_value_s *app_value = (stc_app_value_s *)value;
311 STC_LOGD("Application info => Pkg ID [\033[0;34m%s\033[0;m], "
312 "App ID [\033[0;32m%s\033[0;m], Type [%d], classid [%d],"
313 " counter [ in (%lld), out (%lld)]",
314 app_key->pkg_id, app_key->app_id,
315 app_value->type, app_value->classid,
316 app_value->data_usage.in_bytes, app_value->data_usage.out_bytes);
318 __processes_tree_printall(app_value->processes);
322 static void __apps_tree_printall(void)
324 g_tree_foreach(g_system->apps, __apps_tree_foreach_print, NULL);
327 static gboolean __apps_tree_foreach_remove_pid(gpointer key, gpointer value,
330 remove_pid_context_s *context = (remove_pid_context_s *)data;
331 stc_app_value_s *app_value = (stc_app_value_s *)value;
333 if (!g_tree_remove(app_value->processes, context->proc_key)) {
335 STC_LOGD("key not found");
339 context->entry_removed = TRUE;
340 context->app_key = (stc_app_key_s *)key;
345 static stc_app_value_s * __application_lookup(GTree *apps,
346 const stc_app_key_s *key)
348 stc_app_value_s *lookup;
350 ret_value_msg_if(apps == NULL, NULL, "apps is null!");
352 lookup = g_tree_lookup(apps, key);
357 static stc_process_value_s * __process_lookup(GTree *processes,
358 const stc_process_key_s *key)
360 stc_process_value_s *lookup;
362 ret_value_msg_if(processes == NULL, NULL, "processes is null!");
364 lookup = g_tree_lookup(processes, key);
369 static gboolean __processes_tree_check_empty(gpointer key, gpointer value,
372 guint *pid_count = (guint *)data;
377 static gboolean __add_application_monitor(gpointer key, gpointer value,
380 stc_app_value_s *app_value = (stc_app_value_s *)value;
381 default_connection_s *connection = (default_connection_s *)data;
382 stc_s *stc = stc_get_manager();
384 if ((app_value->classid == STC_TOTAL_DATACALL_CLASSID &&
385 connection->type != STC_IFACE_DATACALL) ||
386 (app_value->classid == STC_TOTAL_WIFI_CLASSID &&
387 connection->type != STC_IFACE_WIFI) ||
388 (app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
389 connection->type != STC_IFACE_BLUETOOTH))
392 if (stc && connection && connection->ifname) {
393 struct nfacct_rule counter;
396 stc->carg = MALLOC0(counter_arg_s, 1);
397 if (stc->carg == NULL)
400 stc->carg->sock = stc_monitor_get_counter_socket();
403 memset(&counter, 0, sizeof(struct nfacct_rule));
405 counter.carg = stc->carg;
406 counter.classid = app_value->classid;
407 counter.intend = NFACCT_COUNTER;
408 counter.iftype = connection->type;
409 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
411 if (app_value->classid == STC_TOTAL_IPV4_CLASSID) {
412 __add_iptables_in(&counter);
413 __add_iptables_out(&counter);
414 } else if (app_value->classid == STC_TOTAL_IPV6_CLASSID) {
415 __add_ip6tables_in(&counter);
416 __add_ip6tables_out(&counter);
418 __add_iptables_in(&counter);
419 __add_iptables_out(&counter);
420 __add_ip6tables_in(&counter);
421 __add_ip6tables_out(&counter);
428 static gboolean __remove_application_monitor(gpointer key, gpointer value,
431 stc_app_value_s *app_value = (stc_app_value_s *)value;
432 default_connection_s *connection = (default_connection_s *)data;
433 stc_s *stc = stc_get_manager();
435 if (stc && connection && connection->ifname) {
436 struct nfacct_rule counter;
439 stc->carg = MALLOC0(counter_arg_s, 1);
440 if (stc->carg == NULL)
443 stc->carg->sock = stc_monitor_get_counter_socket();
446 memset(&counter, 0, sizeof(struct nfacct_rule));
448 counter.carg = stc->carg;
449 counter.classid = app_value->classid;
450 counter.intend = NFACCT_COUNTER;
451 counter.iftype = connection->type;
452 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
454 __del_iptables_in(&counter);
455 __del_iptables_out(&counter);
456 __del_ip6tables_in(&counter);
457 __del_ip6tables_out(&counter);
463 static void __print_rstn(stc_rstn_key_s *rstn_key, stc_rstn_value_s *rstn_value)
465 STC_LOGI("rstn info => rstn_id [%llu], "
466 "app_id [%s], classid [%lu], ifname [%s], "
467 "iftype [%d], rst_state [%d], "
468 "limit [ (%lld) bytes], "
469 "warn_limit [ (%lld) bytes], "
470 "monthly_limit [ (%lld) bytes], "
471 "weekly_limit [ (%lld) bytes], "
472 "daily_limit [ (%lld) bytes], "
473 "data_counter [ (%lld) bytes], "
474 "warn_counter [ (%lld) bytes], "
475 "monthly_counter [ (%lld) bytes], "
476 "weekly_counter [ (%lld) bytes], "
477 "daily_counter [ (%lld) bytes], "
478 "roaming [%d], subscriber_id [%s]",
479 rstn_value->restriction_id,
480 rstn_key->app_id, rstn_value->classid , rstn_key->ifname,
481 rstn_key->iftype, rstn_value->rst_state,
482 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA],
483 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN],
484 rstn_value->limit[STC_RSTN_LIMIT_TYPE_MONTHLY],
485 rstn_value->limit[STC_RSTN_LIMIT_TYPE_WEEKLY],
486 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DAILY],
487 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA],
488 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN],
489 rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY],
490 rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY],
491 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY],
492 rstn_key->roaming, rstn_key->subscriber_id);
495 static void __process_restriction(enum traffic_restriction_type rst_type,
496 stc_rstn_key_s *rstn_key,
497 stc_rstn_value_s *rstn_value, void *data)
499 default_connection_s *old_connection = (default_connection_s *)data;
500 default_connection_s *connection = NULL;
502 if (old_connection != NULL)
503 connection = old_connection;
505 connection = stc_get_default_connection();
507 /* no default ifname */
508 if (connection->ifname == NULL)
511 /* rstn not applicable for this interface */
512 if (rstn_key->ifname != NULL && g_strcmp0("", rstn_key->ifname) != 0 &&
513 g_strcmp0(connection->ifname, rstn_key->ifname) != 0)
516 /* classid is invalid */
517 if (rstn_value->classid == STC_UNKNOWN_CLASSID)
523 /* TODO: Change this to runtime memory */
525 table_counters_info info;
526 int64_t effective_limit[STC_RSTN_LIMIT_TYPE_MAX] = {0, };
528 memset(&info, 0, sizeof(table_counters_info));
529 rstn_value->limit_exceeded = 0;
531 if ((rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] == 0 &&
532 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA] >= 0) ||
533 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] == 0 &&
534 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] >= 0) ||
535 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] == 0 &&
536 rstn_value->limit[STC_RSTN_LIMIT_TYPE_MONTHLY] >= 0) ||
537 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] == 0 &&
538 rstn_value->limit[STC_RSTN_LIMIT_TYPE_WEEKLY] >= 0) ||
539 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] == 0 &&
540 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DAILY] >= 0)) {
542 table_counters_get(rstn_value->restriction_id, &info);
544 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] = info.data_counter;
545 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info.warn_counter;
546 rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] = info.monthly_counter;
547 rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] = info.weekly_counter;
548 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] = info.daily_counter;
551 for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
553 if (rstn_value->limit[i] >= 0) {
554 effective_limit[i] = rstn_value->limit[i] - rstn_value->counter[i];
556 if (effective_limit[i] < 0) {
557 rstn_value->limit_exceeded |= (1 << i);
558 rstn_value->limit_notified |= (1 << i);
563 STC_LOGD("rstn_id [%llu], datausage [%lld] bytes, ",
564 rstn_value->restriction_id, info.data_counter);
566 if (rstn_value->limit_exceeded != 0 &&
567 rstn_value->limit_exceeded != (1 << STC_RSTN_LIMIT_TYPE_DATA_WARN)) {
569 char *default_ifname = stc_default_connection_get_ifname();
570 struct nfacct_rule counter;
571 stc_s *stc = stc_get_manager();
573 STC_LOGE("Can't get stc data");
574 g_free(default_ifname);
579 stc->carg = MALLOC0(counter_arg_s, 1);
580 if (stc->carg == NULL) {
581 g_free(default_ifname);
586 stc_monitor_get_counter_socket();
589 counter.carg = stc->carg;
590 counter.classid = rstn_value->classid;
591 counter.intend = NFACCT_BLOCK;
592 counter.iftype = rstn_key->iftype;
593 g_strlcpy(counter.ifname, default_ifname,
596 g_free(default_ifname);
599 __add_iptables_in(&counter);
600 __add_iptables_out(&counter);
603 __add_ip6tables_in(&counter);
604 __add_ip6tables_out(&counter);
607 rstn_value->rst_state = STC_RESTRICTION_ACTIVATED;
615 char *default_ifname = stc_default_connection_get_ifname();
616 struct nfacct_rule counter;
617 stc_s *stc = stc_get_manager();
619 STC_LOGE("Can't get stc data");
620 g_free(default_ifname);
625 stc->carg = MALLOC0(counter_arg_s, 1);
626 if (stc->carg == NULL) {
627 g_free(default_ifname);
632 stc_monitor_get_counter_socket();
635 counter.carg = stc->carg;
636 counter.classid = rstn_value->classid;
637 counter.intend = NFACCT_BLOCK;
638 counter.iftype = rstn_key->iftype;
639 g_strlcpy(counter.ifname, default_ifname,
642 g_free(default_ifname);
645 __del_iptables_in(&counter);
646 __del_iptables_out(&counter);
649 __del_ip6tables_in(&counter);
650 __del_ip6tables_out(&counter);
652 rstn_value->rst_state = STC_RESTRICTION_REMOVED;
653 rstn_value->limit_exceeded = 0;
654 rstn_value->limit_notified = 0;
657 for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++)
658 if (rstn_value->limit[i] >= 0)
659 rstn_value->counter[i] = 0;
667 static gboolean __remove_rstns_foreach_application(gpointer key,
671 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
672 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
673 gchar *app_id = (gchar *)data;
675 /* rstn rule is not for applications */
676 if (rstn_key->app_id == NULL)
679 /* rstn rule is not for this application */
680 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
683 /* rstn rule is already removed */
684 if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
687 /* remove restriction from system */
688 __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
690 __print_rstn(rstn_key, rstn_value);
695 static void __remove_rstns_for_application(gchar *app_id)
697 g_tree_foreach(g_system->rstns, __remove_rstns_foreach_application,
701 static stc_error_e __application_remove_if_empty(const stc_app_key_s *app_key)
703 stc_error_e ret = STC_ERROR_NONE;
705 stc_app_value_s *lookup;
707 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
709 lookup = __application_lookup(g_system->apps, app_key);
711 STC_LOGE("app_key not found");
712 return STC_ERROR_NO_DATA;
715 g_tree_foreach(lookup->processes, __processes_tree_check_empty,
719 /* remove nfacct rule for this classid */
720 __remove_application_monitor((gpointer) app_key, lookup,
721 stc_get_default_connection());
722 __remove_rstns_for_application(app_key->app_id);
725 if (!g_tree_remove(g_system->apps, app_key)) {
726 ret = STC_ERROR_NO_DATA;
727 STC_LOGE("key not found");
733 static stc_error_e __close_contr_sock(stc_system_s *system)
735 ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
737 /* close netlink socket for updating kernel counters */
738 if (system->contr_sock != -1) {
739 close(system->contr_sock);
740 system->contr_sock = -1;
743 if (system->contr_gsource_id != 0) {
744 g_source_remove(system->contr_gsource_id);
745 system->contr_gsource_id = 0;
748 return STC_ERROR_NONE;
751 static gboolean __process_contr_reply(GIOChannel *source,
752 GIOCondition condition,
755 static stc_error_e __close_and_reopen_contr_sock(stc_system_s *system)
757 GIOChannel *gio = NULL;
758 ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
760 /* close netlink socket for updating kernel counters */
761 if (system->contr_sock != -1) {
762 close(system->contr_sock);
763 system->contr_sock = -1;
766 if (system->contr_gsource_id != 0) {
767 g_source_remove(system->contr_gsource_id);
768 system->contr_gsource_id = 0;
771 /* create netlink socket for updating kernel counters */
772 system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
773 if (system->contr_sock < 0) {
774 STC_LOGE("failed to open socket");
776 return STC_ERROR_FAIL;
779 gio = g_io_channel_unix_new(system->contr_sock);
780 system->contr_gsource_id =
781 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
782 (GIOFunc) __process_contr_reply,
784 g_io_channel_unref(gio);
786 return STC_ERROR_NONE;
789 static void __action_when_rstn_limit_exceeded(stc_rstn_limit_type_e limit_type,
790 stc_rstn_key_s *rstn_key,
791 stc_rstn_value_s *rstn_value,
792 classid_bytes_context_s *context)
795 char iftype[MAX_INT_LENGTH];
796 char byte[MAX_INT_LENGTH];
797 const char *signal_name = NULL;
798 const char *net_popup_content = NULL;
799 const char *net_popup_type = NULL;
800 stc_s *stc = (stc_s *)stc_get_manager();
803 STC_LOGE("failed to get stc data");
807 switch (limit_type) {
808 case STC_RSTN_LIMIT_TYPE_DATA_WARN:
810 signal_name = "WarnThresholdCrossed";
811 net_popup_content = "warn threshold crossed";
812 net_popup_type = "warning_noti";
815 case STC_RSTN_LIMIT_TYPE_DATA:
816 case STC_RSTN_LIMIT_TYPE_MONTHLY:
817 case STC_RSTN_LIMIT_TYPE_WEEKLY:
818 case STC_RSTN_LIMIT_TYPE_DAILY:
820 signal_name = "RestrictionThresholdCrossed";
821 net_popup_content = "restriction threshold crossed";
822 net_popup_type = "restriction_noti";
824 /* block immediately */
825 context->counter->intend = NFACCT_BLOCK;
826 __del_iptables_in(context->counter);
827 __del_iptables_out(context->counter);
828 __add_iptables_in(context->counter);
829 __add_iptables_out(context->counter);
831 __del_ip6tables_in(context->counter);
832 __del_ip6tables_out(context->counter);
833 __add_ip6tables_in(context->counter);
834 __add_ip6tables_out(context->counter);
835 context->counter->intend = NFACCT_COUNTER;
837 rstn_value->limit_exceeded |= (1 << limit_type);
845 rv = stc_manager_dbus_emit_signal(stc->connection,
846 STC_DBUS_SERVICE_RESTRICTION_PATH,
847 STC_DBUS_INTERFACE_RESTRICTION,
849 g_variant_new("(si)",
853 rstn_value->limit_notified |= (1 << limit_type);
855 snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
856 snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->limit[limit_type]);
858 stc_plugin_popup_show(net_popup_content, net_popup_type,
859 rstn_key->app_id, iftype, byte);
862 static gboolean __rstn_counter_update(stc_rstn_key_s *rstn_key,
863 stc_rstn_value_s *rstn_value,
864 classid_bytes_context_s *context)
867 switch (context->counter->iotype) {
868 case NFACCT_COUNTER_IN:
869 case NFACCT_COUNTER_OUT:
870 if ((rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] == 0 &&
871 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA] >= 0) ||
872 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] == 0 &&
873 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] >= 0) ||
874 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] == 0 &&
875 rstn_value->limit[STC_RSTN_LIMIT_TYPE_MONTHLY] >= 0) ||
876 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] == 0 &&
877 rstn_value->limit[STC_RSTN_LIMIT_TYPE_WEEKLY] >= 0) ||
878 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] == 0 &&
879 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DAILY] >= 0)) {
881 table_counters_info info;
882 memset(&info, 0, sizeof(table_counters_info));
883 table_counters_get(rstn_value->restriction_id, &info);
885 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] = info.data_counter;
886 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info.warn_counter;
887 rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] = info.monthly_counter;
888 rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] = info.weekly_counter;
889 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] = info.daily_counter;
892 for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
894 if (rstn_value->limit[i] >= 0 &&
895 !(rstn_value->limit_notified & (1 << i))) {
897 rstn_value->counter[i] += context->bytes;
899 if (rstn_value->limit[i] <= rstn_value->counter[i])
900 __action_when_rstn_limit_exceeded(i, rstn_key,
906 g_system->rstns_tree_updated = TRUE;
907 __print_rstn(rstn_key, rstn_value);
910 STC_LOGE("unknown iotype");
916 static gboolean __interface_rstn_counter_update(stc_rstn_key_s *rstn_key,
917 stc_rstn_value_s *rstn_value,
918 classid_bytes_context_s *context)
920 if ((rstn_value->classid == STC_TOTAL_DATACALL_CLASSID &&
921 context->counter->iftype == STC_IFACE_DATACALL) ||
922 (rstn_value->classid == STC_TOTAL_WIFI_CLASSID &&
923 context->counter->iftype == STC_IFACE_WIFI) ||
924 (rstn_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
925 context->counter->iftype == STC_IFACE_BLUETOOTH)) {
926 context->counter->classid = rstn_value->classid;
927 return __rstn_counter_update(rstn_key, rstn_value, context);
933 static gboolean __rstn_counter_update_foreach_classid(gpointer key,
938 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
939 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
940 classid_bytes_context_s *context = (classid_bytes_context_s *)data;
943 if (context->counter->intend != NFACCT_COUNTER)
944 goto try_next_callback;
946 if (rstn_value->limit_exceeded) {
947 context->data_limit_exceeded = TRUE;
948 goto try_next_callback;
951 classid = context->counter->classid;
952 rv = __interface_rstn_counter_update(rstn_key, rstn_value, context);
954 context->counter->classid = classid;
955 if (rstn_value->classid != context->counter->classid)
956 goto try_next_callback;
958 rv = __rstn_counter_update(rstn_key, rstn_value, context);
964 static gboolean __update_app_statistics(gpointer key, gpointer value,
967 stc_app_key_s *app_key = (stc_app_key_s *)key;
968 stc_app_value_s *app_value = (stc_app_value_s *)value;
969 time_t *touch_time = (time_t *)data;
970 stc_db_classid_iftype_key stat_key;
971 stc_db_app_stats stat;
972 default_connection_s *default_connection = stc_get_default_connection();
974 memset(&stat_key, 0, sizeof(stc_db_classid_iftype_key));
975 memset(&stat, 0 , sizeof(stc_db_app_stats));
977 stat_key.classid = app_value->classid;
978 stat_key.iftype = default_connection->type;
980 if (STC_IFACE_DATACALL == stat_key.iftype)
981 stat_key.subscriber_id = g_strdup(default_connection->subscriber_id);
983 stat_key.subscriber_id = g_strdup("none_subid");
985 g_strlcpy(stat_key.ifname, default_connection->ifname,
988 stat.app_id = g_strdup(app_key->app_id);
989 stat.snd_count = app_value->counter.out_bytes;
990 stat.rcv_count = app_value->counter.in_bytes;
991 stat.is_roaming = default_connection->roaming;
993 if (strstr(stat.app_id, "_BACKGROUND")) {
994 stat.ground = STC_APP_STATE_BACKGROUND;
996 if (strstr(stat.app_id, "TOTAL_"))
997 stat.ground = STC_APP_STATE_UNKNOWN;
999 stat.ground = STC_APP_STATE_FOREGROUND;
1002 table_statistics_insert(&stat_key, &stat, *touch_time);
1004 app_value->counter.out_bytes = 0;
1005 app_value->counter.in_bytes = 0;
1008 FREE(stat_key.subscriber_id);
1013 static gboolean __flush_apps_stats_to_database(gpointer user_data)
1015 time_t current_time = 0;
1016 stc_s *stc = stc_get_manager();
1018 if (stc && stc->carg)
1019 current_time = stc->carg->last_run_time;
1021 if (g_system->apps_tree_updated == FALSE)
1022 return G_SOURCE_REMOVE;
1024 g_system->apps_tree_updated = FALSE;
1027 g_tree_foreach(g_system->apps,
1028 __update_app_statistics,
1031 STC_LOGI("Flushed app stats to database");
1032 return G_SOURCE_REMOVE;
1035 static gboolean __update_counter_statistics(gpointer key, gpointer value,
1038 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1039 table_counters_info info = {
1040 .restriction_id = rstn_value->restriction_id,
1041 .data_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA],
1042 .warn_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN],
1043 .monthly_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY],
1044 .weekly_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY],
1045 .daily_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY]
1048 table_counters_update_counters(&info);
1053 static gboolean __flush_rstns_counter_to_database(gpointer user_data)
1055 time_t current_time = 0;
1056 stc_s *stc = stc_get_manager();
1058 if (stc && stc->carg)
1059 current_time = stc->carg->last_run_time;
1061 if (g_system->rstns_tree_updated == FALSE)
1062 return G_SOURCE_REMOVE;
1064 g_system->rstns_tree_updated = FALSE;
1066 if (g_system->rstns)
1067 g_tree_foreach(g_system->rstns,
1068 __update_counter_statistics,
1071 STC_LOGI("Flushed rstns counters to database");
1072 return G_SOURCE_REMOVE;
1075 static void __app_counter_update(stc_app_key_s *app_key,
1076 stc_app_value_s *app_value,
1077 classid_bytes_context_s *context)
1079 switch (context->counter->iotype) {
1080 case NFACCT_COUNTER_IN:
1081 app_value->data_usage.in_bytes += context->bytes;
1082 app_value->counter.in_bytes = context->bytes;
1083 g_system->apps_tree_updated = TRUE;
1086 __apps_tree_foreach_print(app_key, app_value, NULL);
1088 case NFACCT_COUNTER_OUT:
1089 app_value->data_usage.out_bytes += context->bytes;
1090 app_value->counter.out_bytes = context->bytes;
1091 g_system->apps_tree_updated = TRUE;
1094 __apps_tree_foreach_print(app_key, app_value, NULL);
1097 STC_LOGE("unknown iotype");
1101 static void __interface_counter_update(stc_app_key_s *app_key,
1102 stc_app_value_s *app_value,
1103 classid_bytes_context_s *context)
1105 if ((app_value->classid == STC_TOTAL_DATACALL_CLASSID &&
1106 context->counter->iftype == STC_IFACE_DATACALL) ||
1107 (app_value->classid == STC_TOTAL_WIFI_CLASSID &&
1108 context->counter->iftype == STC_IFACE_WIFI) ||
1109 (app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
1110 context->counter->iftype == STC_IFACE_BLUETOOTH))
1111 __app_counter_update(app_key, app_value, context);
1115 static gboolean __apps_counter_update_foreach_classid(gpointer key,
1119 stc_app_key_s *app_key = (stc_app_key_s *)key;
1120 stc_app_value_s *app_value = (stc_app_value_s *)value;
1121 classid_bytes_context_s *context = (classid_bytes_context_s *)data;
1123 if (context->counter->intend != NFACCT_COUNTER)
1124 goto try_next_callback;
1126 __interface_counter_update(app_key, app_value, context);
1128 if (app_value->classid != context->counter->classid)
1129 goto try_next_callback;
1131 __app_counter_update(app_key, app_value, context);
1137 static gboolean __reset_time_counter_foreach_rstn(gpointer key,
1141 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1142 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1143 reset_time_limits_context_s *context = (reset_time_limits_context_s *)data;
1145 time_t now_month_start_ts;
1147 if (rstn_value->month_start_date == 0) {
1148 table_counters_info info;
1149 memset(&info, 0, sizeof(table_counters_info));
1150 table_counters_get_timestamps(rstn_value->restriction_id, &info);
1152 if (info.month_start_date == 0)
1153 rstn_value->month_start_date = 1;
1155 rstn_value->month_start_date = info.month_start_date;
1156 rstn_value->month_start_ts = info.month_start_ts;
1159 now_month_start_ts =
1160 stc_time_get_month_start(context->now,
1161 rstn_value->month_start_date);
1163 if (rstn_value->month_start_ts != now_month_start_ts) {
1164 rstn_value->month_start_ts = now_month_start_ts;
1165 context->month_start_ts = now_month_start_ts;
1166 context->is_updated |= (1 << STC_RSTN_LIMIT_TYPE_MONTHLY);
1169 if (context->is_updated) {
1170 table_counters_info info;
1171 memset(&info, 0, sizeof(table_counters_info));
1173 info.restriction_id = rstn_value->restriction_id;
1174 info.month_start_date = rstn_value->month_start_date;
1175 info.month_start_ts = rstn_value->month_start_ts;
1176 info.week_start_ts = context->week_start_ts;
1177 info.day_start_ts = context->day_start_ts;
1179 table_counters_update_timestamps(&info);
1182 for (i = STC_RSTN_LIMIT_TYPE_MONTHLY; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
1184 if ((context->is_updated) & (1 << i)) {
1186 rstn_value->counter[i] = 0;
1188 if (rstn_value->limit_exceeded & (1 << i)) {
1189 /* remove iptables rule */
1190 char *default_ifname = stc_default_connection_get_ifname();
1191 struct nfacct_rule counter;
1192 stc_s *stc = stc_get_manager();
1194 STC_LOGE("Can't get stc data");
1195 g_free(default_ifname);
1196 goto try_next_callback;
1200 stc->carg = MALLOC0(counter_arg_s, 1);
1201 if (stc->carg == NULL) {
1202 g_free(default_ifname);
1203 goto try_next_callback;
1207 stc_monitor_get_counter_socket();
1210 counter.carg = stc->carg;
1211 counter.classid = rstn_value->classid;
1212 counter.intend = NFACCT_BLOCK;
1213 counter.iftype = rstn_key->iftype;
1214 g_strlcpy(counter.ifname, default_ifname,
1217 g_free(default_ifname);
1220 __del_iptables_in(&counter);
1221 __del_iptables_out(&counter);
1223 /* ip6tables rule */
1224 __del_ip6tables_in(&counter);
1225 __del_ip6tables_out(&counter);
1227 rstn_value->rst_state = STC_RESTRICTION_REMOVED;
1228 rstn_value->limit_exceeded &= ~(1 << i);
1229 rstn_value->limit_notified &= ~(1 << i);
1238 static void __reset_time_counters_if_required(void)
1240 reset_time_limits_context_s context;
1242 if (g_system == NULL) {
1243 STC_LOGE("stc monitor not initialized!");
1247 context.now = time(NULL);
1248 context.week_start_ts = stc_time_get_week_start(context.now);
1249 context.day_start_ts = stc_time_get_day_start(context.now);
1250 context.is_updated = 0;
1252 if (g_system->last_week_ts != context.week_start_ts) {
1253 g_system->last_week_ts = context.week_start_ts;
1254 context.is_updated |= (1 << STC_RSTN_LIMIT_TYPE_WEEKLY);
1257 if (g_system->last_day_ts != context.day_start_ts) {
1258 g_system->last_day_ts = context.day_start_ts;
1259 context.is_updated |= (1 << STC_RSTN_LIMIT_TYPE_DAILY);
1262 if (g_system->rstns) {
1263 g_tree_foreach(g_system->rstns,
1264 __reset_time_counter_foreach_rstn,
1266 if (context.is_updated)
1267 STC_LOGD("Counter reset completed month_start [%ld], week_start [%ld], day_start [%ld]",
1268 context.month_start_ts, g_system->last_week_ts, g_system->last_day_ts);
1272 static void __fill_nfacct_result(char *cnt_name, int64_t bytes,
1273 struct counter_arg *carg)
1275 __reset_time_counters_if_required();
1277 struct nfacct_rule counter = {
1284 classid_bytes_context_s context = {
1285 .counter = &counter,
1287 .data_limit_exceeded = FALSE,
1291 STC_LOGD("cnt_name %s", cnt_name);
1293 if (!recreate_counter_by_name(cnt_name, &counter)) {
1294 STC_LOGE("Can't parse counter name %s", cnt_name);
1298 STC_LOGI("classid %lu, iftype %u, iotype %d, intend %d, ifname %s, bytes %lld",
1299 context.counter->classid, context.counter->iftype,
1300 context.counter->iotype, context.counter->intend,
1301 context.counter->ifname, context.bytes);
1303 if (g_system->rstns)
1304 g_tree_foreach(g_system->rstns,
1305 __rstn_counter_update_foreach_classid,
1309 g_tree_foreach(g_system->apps,
1310 __apps_counter_update_foreach_classid,
1314 static int __fill_counters(struct rtattr *attr_list[__NFACCT_MAX],
1317 struct counter_arg *carg = user_data;
1318 char *cnt_name = (char *)RTA_DATA(attr_list[NFACCT_NAME]);
1319 if (carg->initiate) {
1321 * TODO: this will be used when daemon starts to update existing
1322 * counter data if present.
1324 populate_counters(cnt_name, carg);
1328 (int64_t *)RTA_DATA(attr_list[NFACCT_BYTES]);
1329 int bytes = be64toh(*bytes_p);
1331 ++carg->serialized_counters;
1332 __fill_nfacct_result(cnt_name, bytes, carg);
1339 static int __post_fill_counters(void *user_data)
1341 struct counter_arg *carg = user_data;
1349 static void __process_network_counter(struct genl *ans,
1350 struct counter_arg *carg)
1352 struct netlink_serialization_params ser_params = {
1355 .eval_attr = __fill_counters,
1356 .post_eval_attr = __post_fill_counters,
1359 netlink_serialization_command *netlink =
1360 netlink_create_command(&ser_params);
1362 STC_LOGE("Can not create command");
1366 netlink->deserialize_answer(&(netlink->params));
1369 static gboolean __process_contr_reply(GIOChannel *source,
1370 GIOCondition condition,
1373 int sock = g_io_channel_unix_get_fd(source);
1376 stc_s *stc = stc_get_manager();
1378 if ((condition & G_IO_ERR) || (condition & G_IO_HUP) ||
1379 (condition & G_IO_NVAL)) {
1380 /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */
1382 STC_LOGE("Counter socket received G_IO event, closing socket."
1383 "G_IO_ERR [%d], G_IO_HUP [%d], G_IO_NVAL [%s]",
1384 (condition & G_IO_ERR), (condition & G_IO_HUP),
1385 (condition & G_IO_NVAL));
1386 __close_and_reopen_contr_sock(g_system);
1390 ans = MALLOC0(struct genl, 1);
1392 STC_LOGE("Failed allocate memory to genl reply message");
1397 STC_LOGE("Can't get stc data");
1401 ret = read_netlink(sock, ans, sizeof(struct genl));
1402 /* STC_LOGD("Counter data received ret [%d]", ret); */
1406 stc->carg->ans_len = ret;
1407 stc->carg->last_run_time = time(NULL);
1409 __process_network_counter(ans, stc->carg);
1411 g_idle_add(__flush_apps_stats_to_database, NULL);
1412 g_idle_add(__flush_rstns_counter_to_database, NULL);
1418 static gboolean __update_contr_cb(void *user_data)
1420 /* Here we just sent command, answer we receive in another callback */
1421 stc_s *stc = stc_get_manager();
1422 ret_value_msg_if(stc == NULL, STC_ERROR_FAIL, "Can't get stc data");
1424 stc->carg = MALLOC0(counter_arg_s, 1);
1425 if (stc->carg == NULL)
1426 return TRUE; /* we need to continue the timer */
1428 stc->carg->sock = stc_monitor_get_counter_socket();
1431 /* STC_LOGD("Get all counters"); */
1432 nfacct_send_get_all(stc->carg);
1434 /* we need to continue the timer */
1438 static gboolean __rstn_tree_foreach_print(gpointer key, gpointer value,
1441 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1442 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1444 __print_rstn(rstn_key, rstn_value);
1448 static void __rstn_tree_printall(void)
1450 g_tree_foreach(g_system->rstns, __rstn_tree_foreach_print, NULL);
1453 static stc_rstn_value_s * __rstn_lookup(GTree *rstns_tree,
1454 const stc_rstn_key_s *key)
1456 stc_rstn_value_s *lookup;
1458 ret_value_msg_if(rstns_tree == NULL, NULL, "rstns_tree is null!");
1460 lookup = g_tree_lookup(rstns_tree, key);
1465 static gboolean __remove_restriction(gpointer key, gpointer value,
1468 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1469 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1471 /* rstn rule is already removed */
1472 if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
1475 __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
1476 __print_rstn(rstn_key, rstn_value);
1480 static gboolean __add_restriction_debug(gpointer key, gpointer value,
1483 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1484 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1486 /* rstn rule is activated */
1487 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1490 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1491 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1493 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1495 __print_rstn(rstn_key, rstn_value);
1500 static gboolean __add_restriction(gpointer key, gpointer value, gpointer data)
1502 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1503 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1505 /* rstn rule is activated */
1506 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1509 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1510 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1512 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1517 static stc_error_e __rstn_tree_remove(stc_rstn_key_s *key)
1519 stc_rstn_value_s *lookup_value;
1521 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1523 lookup_value = __rstn_lookup(g_system->rstns, key);
1524 if (!lookup_value) {
1525 STC_LOGE("key not found");
1526 return STC_ERROR_NO_DATA;
1529 /* remove counter also */
1530 table_counters_delete(lookup_value->restriction_id);
1531 __remove_restriction(key, lookup_value, NULL);
1533 if (!g_tree_remove(g_system->rstns, key)) {
1534 STC_LOGD("key not found");
1535 return STC_ERROR_NO_DATA;
1538 return STC_ERROR_NONE;
1541 static stc_error_e __rstn_tree_add(stc_rstn_key_s *key,
1542 stc_rstn_value_s *value, gboolean debug)
1544 stc_rstn_value_s *rstn_value;
1546 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1548 rstn_value = __rstn_lookup(g_system->rstns, key);
1550 stc_rstn_key_s *rstn_key = MALLOC0(stc_rstn_key_s, 1);
1552 STC_LOGE("rstn_key allocation failed");
1553 return STC_ERROR_OUT_OF_MEMORY;
1556 rstn_value = MALLOC0(stc_rstn_value_s, 1);
1558 STC_LOGE("rstn_value allocation failed");
1560 return STC_ERROR_OUT_OF_MEMORY;
1563 rstn_key->app_id = g_strdup(key->app_id);
1564 rstn_key->ifname = g_strdup(key->ifname);
1565 rstn_key->subscriber_id = g_strdup(key->subscriber_id);
1566 rstn_key->iftype = key->iftype;
1567 rstn_key->roaming = key->roaming;
1569 g_tree_insert(g_system->rstns, rstn_key, rstn_value);
1572 rstn_value->restriction_id = value->restriction_id;
1573 rstn_value->rst_state = value->rst_state;
1574 rstn_value->classid = value->classid;
1577 for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
1578 rstn_value->limit[i] = value->limit[i];
1579 rstn_value->counter[i] = 0;
1582 rstn_value->limit_exceeded = 0;
1583 rstn_value->limit_notified = 0;
1584 rstn_value->month_start_date = value->month_start_date;
1585 rstn_value->month_start_ts = value->month_start_ts;
1588 __add_restriction_debug(key, rstn_value, NULL);
1590 __add_restriction(key, rstn_value, NULL);
1592 return STC_ERROR_NONE;
1595 static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info,
1598 stc_cb_ret_e ret = STC_CONTINUE;
1601 stc_rstn_value_s value;
1603 memset(&key, 0, sizeof(stc_rstn_key_s));
1604 memset(&value, 0, sizeof(stc_rstn_value_s));
1606 key.app_id = g_strdup(info->app_id);
1607 key.ifname = g_strdup(info->ifname);
1608 key.subscriber_id = g_strdup(info->subscriber_id);
1609 key.iftype = info->iftype;
1610 key.roaming = info->roaming;
1612 value.rst_state = info->rst_state;
1613 value.restriction_id = info->restriction_id;
1615 if (value.rst_state != STC_RESTRICTION_EXCLUDED && info->app_id)
1616 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1618 value.classid = STC_UNKNOWN_CLASSID;
1620 value.limit[STC_RSTN_LIMIT_TYPE_DATA] = info->data_limit;
1621 value.limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info->data_warn_limit;
1622 value.limit[STC_RSTN_LIMIT_TYPE_MONTHLY] = info->monthly_limit;
1623 value.limit[STC_RSTN_LIMIT_TYPE_WEEKLY] = info->weekly_limit;
1624 value.limit[STC_RSTN_LIMIT_TYPE_DAILY] = info->daily_limit;
1626 if (__rstn_tree_add(&key, &value, FALSE) != STC_ERROR_NONE)
1631 FREE(key.subscriber_id);
1635 static void __fill_restritions_list(void)
1637 table_restrictions_foreach(__insert_restriction_cb, NULL);
1638 //__rstn_tree_printall();
1641 static gboolean __add_rstn_foreach_application(gpointer key,
1645 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1646 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1647 gchar *app_id = (gchar *)data;
1649 /* rstn rule is not for applications */
1650 if (rstn_key->app_id == NULL)
1653 /* rstn rule is not for this application */
1654 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
1657 /* rstn rule is already applied */
1658 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1661 /* add restriction to system */
1662 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1663 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1665 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1667 __print_rstn(rstn_key, rstn_value);
1672 static void __add_rstns_for_application(gchar *app_id)
1674 g_tree_foreach(g_system->rstns, __add_rstn_foreach_application,
1678 static void __add_application_by_interface(const char *app_id)
1680 stc_app_key_s app_key;
1681 stc_app_value_s app_value;
1686 memset(&app_key, 0, sizeof(stc_app_key_s));
1687 memset(&app_value, 0, sizeof(stc_app_value_s));
1689 app_key.pkg_id = g_strdup(app_id);
1690 app_key.app_id = g_strdup(app_id);
1692 app_value.type = STC_APP_TYPE_NONE;
1693 app_value.processes = NULL;
1694 app_value.counter.in_bytes = 0;
1695 app_value.counter.out_bytes = 0;
1697 stc_monitor_application_add(app_key, app_value);
1699 FREE(app_key.pkg_id);
1700 FREE(app_key.app_id);
1703 static int __vconf_get_int(const char *key, int *value)
1707 ret = vconf_get_int(key, value);
1708 if (ret != VCONF_OK) {
1709 STC_LOGE("Failed to get vconfkey [%s] value", key);
1716 static int __vconf_set_int(const char *key, int value)
1720 ret = vconf_set_int(key, value);
1721 if (ret != VCONF_OK) {
1722 STC_LOGE("Failed to set vconfkey [%s] value", key);
1729 static guint __get_background_state(void)
1731 return g_system->background_state;;
1734 static void __set_background_state(guint state)
1736 g_system->background_state = state;
1739 static gboolean __processes_tree_foreach_background(gpointer key,
1743 stc_process_key_s *proc_key = (stc_process_key_s *)key;
1744 stc_app_key_s *app_key = (stc_app_key_s *)data;
1746 if (g_system->background_state)
1747 place_pids_to_net_cgroup(proc_key->pid, STC_BACKGROUND_APP_ID);
1749 place_pids_to_net_cgroup(proc_key->pid, app_key->app_id);
1754 static gboolean __apps_tree_foreach_background(gpointer key, gpointer value,
1757 stc_app_key_s *app_key = (stc_app_key_s *)key;
1758 stc_app_value_s *app_value = (stc_app_value_s *)value;
1760 if (strstr(app_key->app_id, STC_BACKGROUND_APP_SUFFIX))
1761 g_tree_foreach(app_value->processes,
1762 __processes_tree_foreach_background, app_key);
1767 static stc_error_e __process_update_background(void)
1769 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1771 g_tree_foreach(g_system->apps, __apps_tree_foreach_background, NULL);
1773 return STC_ERROR_NONE;
1776 static void __fill_exceptions_list(void)
1778 stc_plugin_fill_exception_list();
1781 stc_error_e stc_monitor_init(void)
1783 stc_system_s *system = MALLOC0(stc_system_s, 1);
1784 GIOChannel *gio = NULL;
1786 ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY, "stc_system_s malloc fail!");
1788 /* initializing current classid */
1789 init_current_classid();
1791 /* initializing cgroups */
1794 /* creating monitored application tree */
1795 system->apps = g_tree_new_full(__apps_tree_key_compare, NULL,
1796 __apps_tree_key_free,
1797 __apps_tree_value_free);
1799 system->rstns = g_tree_new_full(__rstns_tree_key_compare, NULL,
1800 __rstns_tree_key_free,
1801 __rstns_tree_value_free);
1803 /* create netlink socket for updating kernel counters */
1804 system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
1805 if (system->contr_sock < 0) {
1806 STC_LOGE("failed to open socket");
1808 return STC_ERROR_FAIL;
1811 gio = g_io_channel_unix_new(system->contr_sock);
1812 system->contr_gsource_id =
1813 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
1814 (GIOFunc) __process_contr_reply,
1816 g_io_channel_unref(gio);
1820 __add_application_by_interface(STC_TOTAL_DATACALL);
1821 __add_application_by_interface(STC_TOTAL_WIFI);
1822 __add_application_by_interface(STC_TOTAL_BLUETOOTH);
1823 __add_application_by_interface(STC_TOTAL_IPV4);
1824 __add_application_by_interface(STC_TOTAL_IPV6);
1826 /* creating restriction rules tree */
1827 __update_contr_cb(NULL);
1829 /* registering periodic kernel counters update callback */
1830 g_system->contr_timer_id = g_timeout_add_seconds(CONTR_TIMER_INTERVAL,
1833 if (g_system->contr_timer_id == 0) {
1834 STC_LOGE("Failed to register kernel counters update timer");
1835 __close_contr_sock(g_system);
1836 return STC_ERROR_FAIL;
1839 __vconf_get_int(VCONFKEY_STC_BACKGROUND_STATE,
1840 (int *)&g_system->background_state);
1842 __fill_exceptions_list();
1843 __fill_restritions_list();
1845 return STC_ERROR_NONE;
1848 stc_error_e stc_monitor_deinit(void)
1850 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1852 /* close netlink socket for updating kernel counters */
1853 __close_contr_sock(g_system);
1855 /* remove kernel counters update timer */
1856 if (g_system->contr_timer_id > 0) {
1857 g_source_remove(g_system->contr_timer_id);
1858 g_system->contr_timer_id = 0;
1861 /* destroy monitored application tree */
1862 g_tree_destroy(g_system->apps);
1863 g_system->apps = NULL;
1865 /* destroy restriction rules tree */
1866 g_tree_destroy(g_system->rstns);
1867 g_system->rstns = NULL;
1871 return STC_ERROR_NONE;
1874 API stc_error_e stc_monitor_application_add(const stc_app_key_s app_key,
1875 const stc_app_value_s app_value)
1877 stc_error_e ret = STC_ERROR_NONE;
1879 stc_app_value_s *value;
1880 stc_app_value_s *lookup;
1882 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1884 lookup = __application_lookup(g_system->apps, &app_key);
1886 STC_LOGD("app_key already present");
1887 return STC_ERROR_NONE;
1890 key = MALLOC0(stc_app_key_s, 1);
1892 STC_LOGE("key allocation failed");
1893 return STC_ERROR_OUT_OF_MEMORY;
1896 value = MALLOC0(stc_app_value_s, 1);
1898 STC_LOGE("value allocation failed");
1900 return STC_ERROR_OUT_OF_MEMORY;
1903 key->app_id = g_strdup(app_key.app_id);
1904 key->pkg_id = g_strdup(app_key.pkg_id);
1906 value->type = app_value.type;
1907 value->data_usage.in_bytes = app_value.data_usage.in_bytes;
1908 value->data_usage.out_bytes = app_value.data_usage.out_bytes;
1910 value->processes = g_tree_new_full(__processes_tree_key_compare, NULL,
1911 __processes_tree_key_free,
1912 __processes_tree_value_free);
1914 /* create cgroup and update classid */
1915 value->classid = get_classid_by_app_id(app_key.app_id, TRUE);
1917 g_tree_insert(g_system->apps, key, value);
1919 /* add nfacct rule for this classid */
1920 __add_application_monitor(key, value, stc_get_default_connection());
1921 __add_rstns_for_application(app_key.app_id);
1926 API stc_error_e stc_monitor_process_add(const stc_app_key_s app_key,
1927 const stc_process_key_s proc_key,
1928 const stc_process_value_s proc_value)
1930 stc_error_e ret = STC_ERROR_NONE;
1931 stc_app_value_s *app_lookup;
1932 stc_process_key_s *key;
1933 stc_process_value_s *value;
1934 stc_process_value_s *proc_lookup;
1936 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1938 app_lookup = __application_lookup(g_system->apps, &app_key);
1940 STC_LOGD("app_key not found");
1941 return STC_ERROR_FAIL;
1944 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1946 STC_LOGD("proc_key already present");
1947 return STC_ERROR_NONE;
1950 key = MALLOC0(stc_process_key_s, 1);
1952 STC_LOGE("key allocation failed");
1953 return STC_ERROR_OUT_OF_MEMORY;
1956 value = MALLOC0(stc_process_value_s, 1);
1958 STC_LOGE("value allocation failed");
1960 return STC_ERROR_OUT_OF_MEMORY;
1963 key->pid = proc_key.pid;
1965 value->ground = proc_value.ground;
1967 g_tree_insert(app_lookup->processes, key, value);
1969 /* add pid to application cgroup */
1970 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1973 __apps_tree_printall();
1978 API stc_error_e stc_monitor_process_remove(pid_t pid)
1980 stc_error_e ret = STC_ERROR_NONE;
1981 stc_process_key_s proc_key = {
1985 remove_pid_context_s context = {
1987 .proc_key = &proc_key,
1988 .entry_removed = FALSE,
1991 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1993 g_tree_foreach(g_system->apps, __apps_tree_foreach_remove_pid,
1996 if (context.entry_removed)
1997 __application_remove_if_empty(context.app_key);
2000 __apps_tree_printall();
2005 API stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key,
2006 const stc_process_key_s proc_key,
2007 stc_app_state_e ground)
2009 stc_error_e ret = STC_ERROR_NONE;
2010 stc_app_value_s *app_lookup;
2011 stc_process_value_s *proc_lookup;
2013 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2015 app_lookup = __application_lookup(g_system->apps, &app_key);
2017 STC_LOGD("app_key not found");
2018 return STC_ERROR_FAIL;
2021 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
2023 STC_LOGD("proc_key not found");
2024 return STC_ERROR_FAIL;
2027 if (proc_lookup->ground != ground)
2028 proc_lookup->ground = ground;
2030 if (ground == STC_APP_STATE_BACKGROUND && __get_background_state())
2031 place_pids_to_net_cgroup(proc_key.pid, STC_BACKGROUND_APP_ID);
2033 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
2038 void stc_monitor_update_rstn_by_default_connection(void *data)
2040 static default_connection_s old_connection;
2041 default_connection_s *new_connection = (default_connection_s *)data;
2043 if (old_connection.path != NULL) {
2045 g_tree_foreach(g_system->apps,
2046 __remove_application_monitor,
2047 (gpointer)&old_connection);
2049 if (g_system->rstns)
2050 g_tree_foreach(g_system->rstns,
2051 __remove_restriction,
2052 (gpointer)&old_connection);
2055 FREE(old_connection.path);
2056 FREE(old_connection.ifname);
2057 old_connection.type = 0;
2058 old_connection.roaming = 0;
2060 if (new_connection != NULL && new_connection->path != NULL) {
2062 g_tree_foreach(g_system->apps,
2063 __add_application_monitor,
2064 (gpointer)new_connection);
2066 if (g_system->rstns)
2067 g_tree_foreach(g_system->rstns, __add_restriction,
2070 old_connection.path = g_strdup(new_connection->path);
2071 old_connection.ifname = g_strdup(new_connection->ifname);
2072 old_connection.type = new_connection->type;
2073 old_connection.roaming = new_connection->roaming;
2077 stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info)
2082 stc_rstn_value_s value;
2084 memset(&key, 0, sizeof(stc_rstn_key_s));
2085 memset(&value, 0, sizeof(stc_rstn_value_s));
2087 key.app_id = g_strdup(info->app_id);
2088 key.ifname = g_strdup(info->ifname);
2089 key.subscriber_id = g_strdup(info->subscriber_id);
2090 key.iftype = info->iftype;
2091 key.roaming = info->roaming;
2093 value.rst_state = info->rst_state;
2094 value.restriction_id = info->restriction_id;
2096 if (value.rst_state != STC_RESTRICTION_EXCLUDED && info->app_id)
2097 value.classid = get_classid_by_app_id(info->app_id, TRUE);
2099 value.classid = STC_UNKNOWN_CLASSID;
2101 if (value.classid == STC_BACKGROUND_APP_CLASSID) {
2102 __set_background_state(TRUE);
2103 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state);
2104 __process_update_background();
2107 value.limit[STC_RSTN_LIMIT_TYPE_DATA] = info->data_limit;
2108 value.limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info->data_warn_limit;
2109 value.limit[STC_RSTN_LIMIT_TYPE_MONTHLY] = info->monthly_limit;
2110 value.limit[STC_RSTN_LIMIT_TYPE_WEEKLY] = info->weekly_limit;
2111 value.limit[STC_RSTN_LIMIT_TYPE_DAILY] = info->daily_limit;
2112 value.month_start_date = info->month_start_date;
2113 value.month_start_ts = stc_time_get_month_start(time(NULL),
2114 info->month_start_date);
2116 ret = __rstn_tree_add(&key, &value, TRUE);
2120 FREE(key.subscriber_id);
2124 stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info)
2128 stc_rstn_key_s key = {
2129 .app_id = g_strdup(info->app_id),
2130 .ifname = g_strdup(info->ifname),
2131 .subscriber_id = g_strdup(info->subscriber_id),
2132 .iftype = info->iftype,
2133 .roaming = info->roaming,
2136 if (!strcmp(key.app_id, STC_BACKGROUND_APP_ID)) {
2137 __set_background_state(FALSE);
2138 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state);
2139 __process_update_background();
2142 ret = __rstn_tree_remove(&key);
2146 FREE(key.subscriber_id);
2150 API stc_error_e stc_monitor_check_excn_by_cmdline(char *cmdline)
2152 return stc_plugin_check_exception_by_cmdline(cmdline);
2155 int stc_monitor_get_counter_socket(void)
2157 return g_system->contr_sock;