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"
26 #include "helper-iptables.h"
28 #include "table-statistics.h"
29 #include "table-counters.h"
30 #include "stc-monitor.h"
32 #include "stc-manager-plugin-appstatus.h"
33 #include "stc-manager-plugin-exception.h"
35 #define GRANULARITY 10
36 #define MAX_INT_LENGTH 128
37 #define VCONFKEY_STC_BACKGROUND_STATE "db/stc/background_state"
41 time_t month_start_ts;
45 } reset_time_limits_context_s;
48 stc_app_key_s *app_key;
49 stc_process_key_s *proc_key;
50 gboolean entry_removed;
51 } remove_pid_context_s;
54 struct nfacct_rule *counter;
56 gboolean data_limit_exceeded;
57 } classid_bytes_context_s;
59 static stc_system_s *g_system = NULL;
61 static nfacct_rule_jump __get_jump_by_intend(struct nfacct_rule *counter)
63 if (counter->intend == NFACCT_WARN)
64 return NFACCT_JUMP_ACCEPT;
65 else if (counter->intend == NFACCT_BLOCK)
66 return NFACCT_JUMP_REJECT;
67 else if (counter->intend == NFACCT_ALLOW)
68 return NFACCT_JUMP_ACCEPT;
70 return NFACCT_JUMP_UNKNOWN;
73 static stc_error_e __add_iptables_tether_in(struct nfacct_rule *counter,
78 if (counter == NULL || ipaddr == NULL)
79 return STC_ERROR_INVALID_PARAMETER;
81 counter->action = NFACCT_ACTION_INSERT;
82 counter->iotype = NFACCT_COUNTER_IN;
83 counter->jump = __get_jump_by_intend(counter);
84 counter->iptype = NFACCT_TYPE_IPV4;
85 counter->send_limit = 0;
86 counter->rcv_limit = 0;
87 counter->src_iprange_type = NFACCT_IPRANGE_TYPE_SINGLE;
88 counter->src_ip1 = g_strdup(ipaddr);
90 ret = produce_net_rule(counter);
92 g_free(counter->src_ip1);
93 counter->src_iprange_type = NFACCT_IPRANGE_TYPE_NONE;
97 static stc_error_e __add_iptables_tether_out(struct nfacct_rule *counter,
102 if (counter == NULL || ipaddr == NULL)
103 return STC_ERROR_INVALID_PARAMETER;
105 counter->action = NFACCT_ACTION_INSERT;
106 counter->iotype = NFACCT_COUNTER_OUT;
107 counter->jump = __get_jump_by_intend(counter);
108 counter->iptype = NFACCT_TYPE_IPV4;
109 counter->send_limit = 0;
110 counter->rcv_limit = 0;
111 counter->dst_iprange_type = NFACCT_IPRANGE_TYPE_SINGLE;
112 counter->dst_ip1 = g_strdup(ipaddr);
114 ret = produce_net_rule(counter);
116 g_free(counter->dst_ip1);
117 counter->dst_iprange_type = NFACCT_IPRANGE_TYPE_NONE;
121 static stc_error_e __del_iptables_tether_in(struct nfacct_rule *counter,
126 if (counter == NULL || ipaddr == NULL)
127 return STC_ERROR_INVALID_PARAMETER;
129 counter->action = NFACCT_ACTION_DELETE;
130 counter->iotype = NFACCT_COUNTER_IN;
131 counter->jump = __get_jump_by_intend(counter);
132 counter->iptype = NFACCT_TYPE_IPV4;
133 counter->send_limit = 0;
134 counter->rcv_limit = 0;
135 counter->src_iprange_type = NFACCT_IPRANGE_TYPE_SINGLE;
136 counter->src_ip1 = g_strdup(ipaddr);
138 ret = produce_net_rule(counter);
140 g_free(counter->src_ip1);
141 counter->src_iprange_type = NFACCT_IPRANGE_TYPE_NONE;
145 static stc_error_e __del_iptables_tether_out(struct nfacct_rule *counter,
150 if (counter == NULL || ipaddr == NULL)
151 return STC_ERROR_INVALID_PARAMETER;
153 counter->action = NFACCT_ACTION_DELETE;
154 counter->iotype = NFACCT_COUNTER_OUT;
155 counter->jump = __get_jump_by_intend(counter);
156 counter->iptype = NFACCT_TYPE_IPV4;
157 counter->send_limit = 0;
158 counter->rcv_limit = 0;
159 counter->dst_iprange_type = NFACCT_IPRANGE_TYPE_SINGLE;
160 counter->dst_ip1 = g_strdup(ipaddr);
162 ret = produce_net_rule(counter);
164 g_free(counter->dst_ip1);
165 counter->dst_iprange_type = NFACCT_IPRANGE_TYPE_NONE;
169 static stc_error_e __add_iptables_in(struct nfacct_rule *counter)
172 return STC_ERROR_INVALID_PARAMETER;
174 counter->action = NFACCT_ACTION_INSERT;
175 counter->iotype = NFACCT_COUNTER_IN;
176 counter->jump = __get_jump_by_intend(counter);
177 counter->iptype = NFACCT_TYPE_IPV4;
178 counter->send_limit = 0;
179 counter->rcv_limit = 0;
181 return produce_net_rule(counter);
184 static stc_error_e __add_iptables_out(struct nfacct_rule *counter)
187 return STC_ERROR_INVALID_PARAMETER;
189 counter->action = NFACCT_ACTION_INSERT;
190 counter->iotype = NFACCT_COUNTER_OUT;
191 counter->jump = __get_jump_by_intend(counter);
192 counter->iptype = NFACCT_TYPE_IPV4;
193 counter->send_limit = 0;
194 counter->rcv_limit = 0;
196 return produce_net_rule(counter);
199 static stc_error_e __del_iptables_in(struct nfacct_rule *counter)
202 return STC_ERROR_INVALID_PARAMETER;
204 counter->action = NFACCT_ACTION_DELETE;
205 counter->iotype = NFACCT_COUNTER_IN;
206 counter->jump = __get_jump_by_intend(counter);
207 counter->iptype = NFACCT_TYPE_IPV4;
208 counter->send_limit = 0;
209 counter->rcv_limit = 0;
211 return produce_net_rule(counter);
214 static stc_error_e __del_iptables_out(struct nfacct_rule *counter)
217 return STC_ERROR_INVALID_PARAMETER;
219 counter->action = NFACCT_ACTION_DELETE;
220 counter->iotype = NFACCT_COUNTER_OUT;
221 counter->jump = __get_jump_by_intend(counter);
222 counter->iptype = NFACCT_TYPE_IPV4;
223 counter->send_limit = 0;
224 counter->rcv_limit = 0;
226 return produce_net_rule(counter);
229 static stc_error_e __add_ip6tables_in(struct nfacct_rule *counter)
232 return STC_ERROR_INVALID_PARAMETER;
234 counter->action = NFACCT_ACTION_INSERT;
235 counter->iotype = NFACCT_COUNTER_IN;
236 counter->jump = __get_jump_by_intend(counter);
237 counter->iptype = NFACCT_TYPE_IPV6;
238 counter->send_limit = 0;
239 counter->rcv_limit = 0;
241 return produce_net_rule(counter);
244 static stc_error_e __add_ip6tables_out(struct nfacct_rule *counter)
247 return STC_ERROR_INVALID_PARAMETER;
249 counter->action = NFACCT_ACTION_INSERT;
250 counter->iotype = NFACCT_COUNTER_OUT;
251 counter->jump = __get_jump_by_intend(counter);
252 counter->iptype = NFACCT_TYPE_IPV6;
253 counter->send_limit = 0;
254 counter->rcv_limit = 0;
256 return produce_net_rule(counter);
259 static stc_error_e __del_ip6tables_in(struct nfacct_rule *counter)
262 return STC_ERROR_INVALID_PARAMETER;
264 counter->action = NFACCT_ACTION_DELETE;
265 counter->iotype = NFACCT_COUNTER_IN;
266 counter->jump = __get_jump_by_intend(counter);
267 counter->iptype = NFACCT_TYPE_IPV6;
268 counter->send_limit = 0;
269 counter->rcv_limit = 0;
271 return produce_net_rule(counter);
274 static stc_error_e __del_ip6tables_out(struct nfacct_rule *counter)
277 return STC_ERROR_INVALID_PARAMETER;
279 counter->action = NFACCT_ACTION_DELETE;
280 counter->iotype = NFACCT_COUNTER_OUT;
281 counter->jump = __get_jump_by_intend(counter);
282 counter->iptype = NFACCT_TYPE_IPV6;
283 counter->send_limit = 0;
284 counter->rcv_limit = 0;
286 return produce_net_rule(counter);
289 static int __processes_tree_key_compare(gconstpointer a, gconstpointer b,
290 gpointer UNUSED user_data)
292 stc_process_key_s *key_a = (stc_process_key_s *)a;
293 stc_process_key_s *key_b = (stc_process_key_s *)b;
295 return key_a->pid - key_b->pid;
298 static void __processes_tree_value_free(gpointer data)
300 stc_process_value_s *value = (stc_process_value_s *)data;
305 static void __processes_tree_key_free(gpointer data)
307 stc_process_key_s *key = (stc_process_key_s *)data;
312 static int __apps_tree_key_compare(gconstpointer a, gconstpointer b,
313 gpointer UNUSED user_data)
315 stc_app_key_s *key_a = (stc_app_key_s *)a;
316 stc_app_key_s *key_b = (stc_app_key_s *)b;
319 ret = g_strcmp0(key_a->pkg_id, key_b->pkg_id);
323 return g_strcmp0(key_a->app_id, key_b->app_id);
326 static void __apps_tree_value_free(gpointer data)
328 stc_app_value_s *value = (stc_app_value_s *)data;
330 g_tree_destroy(value->processes);
331 value->processes = NULL;
336 static void __apps_tree_key_free(gpointer data)
338 stc_app_key_s *key = (stc_app_key_s *)data;
345 static int __rstns_tree_key_compare(gconstpointer a, gconstpointer b,
346 gpointer UNUSED user_data)
348 stc_rstn_key_s *key_a = (stc_rstn_key_s *)a;
349 stc_rstn_key_s *key_b = (stc_rstn_key_s *)b;
352 ret = g_strcmp0(key_a->app_id, key_b->app_id);
356 ret = g_strcmp0(key_a->ifname, key_b->ifname);
360 ret = g_strcmp0(key_a->subscriber_id, key_b->subscriber_id);
364 ret = key_a->iftype - key_b->iftype;
368 ret = key_a->roaming - key_b->roaming;
375 static void __rstns_tree_value_free(gpointer data)
377 stc_rstn_value_s *value = (stc_rstn_value_s *)data;
382 static void __rstns_tree_key_free(gpointer data)
384 stc_rstn_key_s *key = (stc_rstn_key_s *)data;
388 FREE(key->subscriber_id);
394 static gboolean __processes_tree_foreach_print(gpointer key, gpointer value,
397 stc_process_key_s *proc_key = (stc_process_key_s *)key;
398 stc_process_value_s *proc_value = (stc_process_value_s *)value;
400 STC_LOGD("Process entry => PID [\033[1;33m%d\033[0;m], Ground state [%d]",
401 proc_key->pid, proc_value->ground);
405 static void __processes_tree_printall(GTree *processes)
407 g_tree_foreach(processes, __processes_tree_foreach_print, NULL);
410 static gboolean __apps_tree_foreach_print(gpointer key, gpointer value,
413 stc_app_key_s *app_key = (stc_app_key_s *)key;
414 stc_app_value_s *app_value = (stc_app_value_s *)value;
416 STC_LOGD("Application info => Pkg ID [\033[0;34m%s\033[0;m], "
417 "App ID [\033[0;32m%s\033[0;m], Type [%d], classid [%d],"
418 " counter [ in (%lld), out (%lld)]",
419 app_key->pkg_id, app_key->app_id,
420 app_value->type, app_value->classid,
421 app_value->data_usage.in_bytes, app_value->data_usage.out_bytes);
423 __processes_tree_printall(app_value->processes);
427 static void __apps_tree_printall(void)
429 g_tree_foreach(g_system->apps, __apps_tree_foreach_print, NULL);
434 static gboolean __apps_tree_foreach_remove_pid(gpointer key, gpointer value,
437 remove_pid_context_s *context = (remove_pid_context_s *)data;
438 stc_app_value_s *app_value = (stc_app_value_s *)value;
440 if (!g_tree_remove(app_value->processes, context->proc_key))
443 context->entry_removed = TRUE;
444 context->app_key = (stc_app_key_s *)key;
449 static stc_app_value_s * __application_lookup(GTree *apps,
450 const stc_app_key_s *key)
452 stc_app_value_s *lookup;
454 ret_value_msg_if(apps == NULL, NULL, "apps is null!");
456 lookup = g_tree_lookup(apps, key);
461 static stc_process_value_s * __process_lookup(GTree *processes,
462 const stc_process_key_s *key)
464 stc_process_value_s *lookup;
466 ret_value_msg_if(processes == NULL, NULL, "processes is null!");
468 lookup = g_tree_lookup(processes, key);
474 static gboolean __processes_tree_check_empty(gpointer key, gpointer value,
477 guint *pid_count = (guint *)data;
483 static gboolean __add_application_monitor_for_tethering(gpointer key, gpointer value,
486 stc_app_value_s *app_value = (stc_app_value_s *)value;
487 stc_app_key_s *app_key = (stc_app_key_s *)key;
488 default_connection_s *connection = (default_connection_s *)data;
489 stc_s *stc = stc_get_manager();
490 struct nfacct_rule counter;
492 STC_LOGI("add tether app (%s)", app_key->app_id);
494 if (stc == NULL || connection == NULL)
498 stc->carg = MALLOC0(counter_arg_s, 1);
499 if (stc->carg == NULL)
502 stc->carg->sock = stc_monitor_get_counter_socket();
505 memset(&counter, 0, sizeof(struct nfacct_rule));
507 counter.carg = stc->carg;
508 counter.classid = app_value->classid;
509 counter.intend = NFACCT_TETH_COUNTER;
511 if (connection->tether_state != TRUE ||
512 connection->tether_iface.ifname == NULL)
515 counter.iftype = connection->tether_iface.type;
516 g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
518 __add_iptables_tether_in(&counter, app_value->ipaddr);
519 __add_iptables_tether_out(&counter, app_value->ipaddr);
524 static gboolean __remove_application_monitor_for_tethering(gpointer key, gpointer value,
527 stc_app_value_s *app_value = (stc_app_value_s *)value;
528 stc_app_key_s *app_key = (stc_app_key_s *)key;
529 default_connection_s *connection = (default_connection_s *)data;
530 stc_s *stc = stc_get_manager();
531 struct nfacct_rule counter;
533 STC_LOGI("remove tether app (%s)", app_key->app_id);
535 if (stc == NULL || connection == NULL)
539 stc->carg = MALLOC0(counter_arg_s, 1);
540 if (stc->carg == NULL)
543 stc->carg->sock = stc_monitor_get_counter_socket();
546 memset(&counter, 0, sizeof(struct nfacct_rule));
548 counter.carg = stc->carg;
549 counter.classid = app_value->classid;
550 counter.intend = NFACCT_TETH_COUNTER;
552 if (connection->tether_state != TRUE ||
553 connection->tether_iface.ifname == NULL)
556 counter.iftype = connection->tether_iface.type;
557 g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
559 __del_iptables_tether_in(&counter, app_value->ipaddr);
560 __del_iptables_tether_out(&counter, app_value->ipaddr);
565 static gboolean __add_application_monitor(gpointer key, gpointer value,
568 stc_app_value_s *app_value = (stc_app_value_s *)value;
569 stc_app_key_s *app_key = (stc_app_key_s *)key;
570 default_connection_s *connection = (default_connection_s *)data;
571 stc_s *stc = stc_get_manager();
573 if (app_value->classid == STC_TOTAL_DATACALL_CLASSID ||
574 app_value->classid == STC_TOTAL_WIFI_CLASSID ||
575 app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID)
578 if (stc && connection && connection->ifname) {
579 struct nfacct_rule counter;
582 stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
583 if (stc->carg == NULL) //LCOV_EXCL_LINE
584 return FALSE; //LCOV_EXCL_LINE
586 stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
589 memset(&counter, 0, sizeof(struct nfacct_rule));
591 counter.carg = stc->carg;
592 counter.classid = app_value->classid;
593 counter.intend = NFACCT_COUNTER;
595 if (connection->tether_state == TRUE &&
596 connection->tether_iface.ifname != NULL &&
597 app_value->classid == STC_TETHERING_APP_CLASSID) {
598 counter.iftype = connection->tether_iface.type;
599 g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
601 counter.iftype = connection->type;
602 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
605 if (g_str_has_suffix(app_key->app_id, STC_TETHERING_APP_SUFFIX) &&
606 app_value->classid != STC_TETHERING_APP_CLASSID) {
607 __add_application_monitor_for_tethering(key, value, data);
608 } else if (app_value->classid == STC_TOTAL_IPV4_CLASSID) {
609 __add_iptables_in(&counter);
610 __add_iptables_out(&counter);
611 } else if (app_value->classid == STC_TOTAL_IPV6_CLASSID) {
612 __add_ip6tables_in(&counter);
613 __add_ip6tables_out(&counter);
615 __add_iptables_in(&counter);
616 __add_iptables_out(&counter);
617 __add_ip6tables_in(&counter);
618 __add_ip6tables_out(&counter);
625 static gboolean __remove_application_monitor(gpointer key, gpointer value,
628 stc_app_value_s *app_value = (stc_app_value_s *)value;
629 stc_app_key_s *app_key = (stc_app_key_s *)key;
630 default_connection_s *connection = (default_connection_s *)data;
631 stc_s *stc = stc_get_manager();
633 if (stc && connection && connection->ifname) {
634 struct nfacct_rule counter;
637 stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
638 if (stc->carg == NULL) //LCOV_EXCL_LINE
639 return FALSE; //LCOV_EXCL_LINE
641 stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
644 memset(&counter, 0, sizeof(struct nfacct_rule));
646 counter.carg = stc->carg;
647 counter.classid = app_value->classid;
648 counter.intend = NFACCT_COUNTER;
650 if (g_str_has_suffix(app_key->app_id, STC_TETHERING_APP_SUFFIX) &&
651 app_value->classid != STC_TETHERING_APP_CLASSID) {
652 __remove_application_monitor_for_tethering(key, value, data);
654 } else if (connection->tether_state == FALSE &&
655 connection->tether_iface.ifname != NULL &&
656 app_value->classid == STC_TETHERING_APP_CLASSID) {
657 counter.iftype = connection->tether_iface.type;
658 g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
660 counter.iftype = connection->type;
661 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
664 __del_iptables_in(&counter);
665 __del_iptables_out(&counter);
666 __del_ip6tables_in(&counter);
667 __del_ip6tables_out(&counter);
673 static void __print_rstn(stc_rstn_key_s *rstn_key, stc_rstn_value_s *rstn_value)
675 STC_LOGI("rstn info => rstn_id [%llu], "
676 "app_id [%s], classid [%u], ifname [%s], "
677 "iftype [%d], rstn_state [%d], rstn_type [%d], "
678 "month_start_date [%d], limit [ (%lld) bytes], "
679 "warn_limit [ (%lld) bytes], "
680 "monthly_limit [ (%lld) bytes], "
681 "weekly_limit [ (%lld) bytes], "
682 "daily_limit [ (%lld) bytes], "
683 "data_counter [ (%lld) bytes], "
684 "warn_counter [ (%lld) bytes], "
685 "monthly_counter [ (%lld) bytes], "
686 "weekly_counter [ (%lld) bytes], "
687 "daily_counter [ (%lld) bytes], "
688 "roaming [%d], subscriber_id [%s]",
689 rstn_value->restriction_id,
690 rstn_key->app_id, rstn_value->classid , rstn_key->ifname,
691 rstn_key->iftype, rstn_value->rstn_state, rstn_value->rstn_type,
692 rstn_value->month_start_date,
693 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA],
694 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN],
695 rstn_value->limit[STC_RSTN_LIMIT_TYPE_MONTHLY],
696 rstn_value->limit[STC_RSTN_LIMIT_TYPE_WEEKLY],
697 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DAILY],
698 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA],
699 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN],
700 rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY],
701 rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY],
702 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY],
703 rstn_key->roaming, rstn_key->subscriber_id);
706 static void __add_iptables_rule(int64_t classid, nfacct_rule_intend intend,
707 stc_iface_type_e iftype)
709 char *default_ifname = stc_default_connection_get_ifname();
710 default_connection_s *connection = stc_get_default_connection();
711 struct nfacct_rule counter;
712 stc_s *stc = stc_get_manager();
714 g_free(default_ifname); //LCOV_EXCL_LINE
715 return; //LCOV_EXCL_LINE
719 stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
720 if (stc->carg == NULL) { //LCOV_EXCL_LINE
721 g_free(default_ifname); //LCOV_EXCL_LINE
722 return; //LCOV_EXCL_LINE
725 stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
728 counter.carg = stc->carg;
729 counter.classid = classid;
730 counter.intend = intend;
732 if (connection && connection->tether_iface.ifname != NULL &&
733 classid == STC_TETHERING_APP_CLASSID) {
734 counter.iftype = connection->tether_iface.type;
735 g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
737 counter.iftype = iftype;
738 g_strlcpy(counter.ifname, default_ifname, MAX_IFACE_LENGTH);
741 g_free(default_ifname);
744 __add_iptables_in(&counter);
745 __add_iptables_out(&counter);
748 __add_ip6tables_in(&counter);
749 __add_ip6tables_out(&counter);
752 static void __del_iptables_rule(int64_t classid, nfacct_rule_intend intend,
753 stc_iface_type_e iftype)
755 char *default_ifname = stc_default_connection_get_ifname();
756 default_connection_s *connection = stc_get_default_connection();
757 struct nfacct_rule counter;
758 stc_s *stc = stc_get_manager();
760 g_free(default_ifname); //LCOV_EXCL_LINE
761 return; //LCOV_EXCL_LINE
765 stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
766 if (stc->carg == NULL) { //LCOV_EXCL_LINE
767 g_free(default_ifname); //LCOV_EXCL_LINE
768 return; //LCOV_EXCL_LINE
771 stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
774 counter.carg = stc->carg;
775 counter.classid = classid;
776 counter.intend = intend;
778 if (connection && connection->tether_iface.ifname != NULL &&
779 classid == STC_TETHERING_APP_CLASSID) {
780 counter.iftype = connection->tether_iface.type;
781 g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
783 counter.iftype = iftype;
784 g_strlcpy(counter.ifname, default_ifname, MAX_IFACE_LENGTH);
787 g_free(default_ifname);
790 __del_iptables_in(&counter);
791 __del_iptables_out(&counter);
794 __del_ip6tables_in(&counter);
795 __del_ip6tables_out(&counter);
799 time_t month_start_ts;
800 time_t week_start_ts;
802 int64_t monthly_stat;
807 static stc_cb_ret_e __statistics_info_cb(const table_statistics_info *info,
810 cumulative_data_s *stat = (cumulative_data_s *)user_data;
811 int64_t counters = 0;
813 counters = info->cnt.in_bytes + info->cnt.out_bytes;
815 stat->monthly_stat += counters;
816 if (stat->week_start_ts <= info->interval->from)
817 stat->weekly_stat += counters;
818 if (stat->day_start_ts <= info->interval->from)
819 stat->daily_stat += counters;
824 static void __process_restriction(enum traffic_restriction_type rstn_type,
825 stc_rstn_key_s *rstn_key,
826 stc_rstn_value_s *rstn_value, void *data)
828 default_connection_s *old_connection = (default_connection_s *)data;
829 default_connection_s *connection = NULL;
831 if (old_connection != NULL)
832 connection = old_connection;
834 connection = stc_get_default_connection();
836 /* no default ifname */
837 if (connection->ifname == NULL)
840 /* rstn not applicable for this interface */
841 if (rstn_key->ifname != NULL && g_strcmp0("", rstn_key->ifname) != 0 &&
842 (g_strcmp0(connection->ifname, rstn_key->ifname) != 0) &&
843 (g_strcmp0(connection->tether_iface.ifname, rstn_key->ifname) != 0))
846 /* classid is invalid */
847 if (rstn_value->classid <= STC_UNKNOWN_CLASSID)
854 table_counters_info info;
855 int64_t effective_limit[STC_RSTN_LIMIT_TYPE_MAX] = { 0, };
857 memset(&info, 0, sizeof(table_counters_info));
858 rstn_value->limit_exceeded = 0;
860 if ((rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] == 0 &&
861 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA] >= 0) ||
862 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] == 0 &&
863 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] >= 0) ||
864 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] == 0 &&
865 rstn_value->limit[STC_RSTN_LIMIT_TYPE_MONTHLY] >= 0) ||
866 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] == 0 &&
867 rstn_value->limit[STC_RSTN_LIMIT_TYPE_WEEKLY] >= 0) ||
868 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] == 0 &&
869 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DAILY] >= 0)) {
870 table_counters_get(rstn_value->restriction_id, &info);
872 time_t current_time = 0;
873 cumulative_data_s stat;
874 table_statistics_select_rule rule;
876 memset(&stat, 0, sizeof(cumulative_data_s));
877 stat.month_start_ts = rstn_value->month_start_ts;
878 stat.week_start_ts = g_system->last_week_ts;
879 stat.day_start_ts = g_system->last_day_ts;
881 memset(&rule, 0, sizeof(table_statistics_select_rule));
882 rule.from = rstn_value->month_start_ts;
884 rule.to = current_time;
885 rule.iftype = rstn_key->iftype;
886 rule.granularity = GRANULARITY;
888 table_statistics_per_app(rstn_key->app_id, &rule, __statistics_info_cb, &stat);
890 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] = info.data_counter;
891 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info.warn_counter;
892 rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] = info.monthly_counter + stat.monthly_stat;
893 rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] = info.weekly_counter + stat.weekly_stat;
894 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] = info.daily_counter + stat.daily_stat;
897 for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
898 if (rstn_value->limit[i] >= 0) {
899 effective_limit[i] = rstn_value->limit[i] - rstn_value->counter[i];
901 if (effective_limit[i] < 0)
902 rstn_value->limit_exceeded |= (1 << i);
906 STC_LOGD("rstn_id [%llu], datausage [%llu] bytes",
907 rstn_value->restriction_id, info.data_counter);
909 if (rstn_value->limit_exceeded != 0 &&
910 rstn_value->limit_exceeded != (1 << STC_RSTN_LIMIT_TYPE_DATA_WARN)) {
911 __add_iptables_rule(rstn_value->classid, NFACCT_BLOCK, rstn_key->iftype);
914 rstn_value->rstn_state = STC_RSTN_STATE_ACTIVATED;
918 __add_iptables_rule(rstn_value->classid, NFACCT_ALLOW,
921 rstn_value->rstn_state = STC_RSTN_STATE_ACTIVATED;
922 rstn_value->limit_exceeded = 0;
923 rstn_value->limit_notified = 0;
929 if (rstn_value->classid == STC_TETHERING_APP_CLASSID)
930 __del_iptables_rule(rstn_value->classid, NFACCT_BLOCK,
933 __del_iptables_rule(rstn_value->classid, rstn_value->rstn_type,
936 rstn_value->rstn_state = STC_RSTN_STATE_DEACTIVATED;
937 rstn_value->limit_exceeded = 0;
938 rstn_value->limit_notified = 0;
940 for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++)
941 if (rstn_value->limit[i] >= 0)
942 rstn_value->counter[i] = 0;
951 static gboolean __remove_rstns_foreach_application(gpointer key,
955 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
956 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
957 gchar *app_id = (gchar *)data;
959 /* rstn rule is not for applications */
960 if (rstn_key->app_id == NULL)
963 /* rstn rule is not for this application */
964 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
967 /* rstn rule is already removed */
968 if (rstn_value->rstn_state == STC_RSTN_STATE_DEACTIVATED)
971 /* remove restriction from system */
972 __process_restriction(RST_UNSET, rstn_key, rstn_value, NULL);
974 __print_rstn(rstn_key, rstn_value);
980 static void __remove_rstns_for_application(gchar *app_id)
982 g_tree_foreach(g_system->rstns, __remove_rstns_foreach_application,
986 static stc_error_e __application_remove_if_empty(const stc_app_key_s *app_key)
988 stc_error_e ret = STC_ERROR_NONE;
990 stc_app_value_s *lookup;
992 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
994 lookup = __application_lookup(g_system->apps, app_key);
996 STC_LOGE("app_key not found"); //LCOV_EXCL_LINE
997 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
1000 g_tree_foreach(lookup->processes, __processes_tree_check_empty,
1004 /* remove nfacct rule for this classid */
1005 __remove_application_monitor((gpointer) app_key, lookup,
1006 stc_get_default_connection());
1007 __remove_rstns_for_application(app_key->app_id);
1010 if (!g_tree_remove(g_system->apps, app_key)) {
1011 ret = STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
1012 STC_LOGE("key not found"); //LCOV_EXCL_LINE
1018 static stc_error_e __close_contr_sock(stc_system_s *system)
1020 ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
1022 /* close netlink socket for updating kernel counters */
1023 if (system->contr_sock != -1) {
1024 close(system->contr_sock);
1025 system->contr_sock = -1;
1028 if (system->contr_gsource_id != 0) {
1029 g_source_remove(system->contr_gsource_id);
1030 system->contr_gsource_id = 0;
1033 return STC_ERROR_NONE;
1036 static gboolean __process_contr_reply(GIOChannel *source,
1037 GIOCondition condition,
1038 gpointer user_data);
1041 static stc_error_e __close_and_reopen_contr_sock(stc_system_s *system)
1043 GIOChannel *gio = NULL;
1044 ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
1046 /* close netlink socket for updating kernel counters */
1047 if (system->contr_sock != -1) {
1048 close(system->contr_sock);
1049 system->contr_sock = -1;
1052 if (system->contr_gsource_id != 0) {
1053 g_source_remove(system->contr_gsource_id);
1054 system->contr_gsource_id = 0;
1057 /* create netlink socket for updating kernel counters */
1058 system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
1059 if (system->contr_sock < 0) {
1060 STC_LOGE("failed to open socket");
1062 return STC_ERROR_FAIL;
1065 gio = g_io_channel_unix_new(system->contr_sock);
1066 system->contr_gsource_id =
1067 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
1068 (GIOFunc) __process_contr_reply,
1070 g_io_channel_unref(gio);
1072 return STC_ERROR_NONE;
1075 static void __action_when_rstn_limit_exceeded(stc_rstn_limit_type_e limit_type,
1076 stc_rstn_key_s *rstn_key,
1077 stc_rstn_value_s *rstn_value,
1078 classid_bytes_context_s *context)
1081 char iftype[MAX_INT_LENGTH] = { 0, };
1082 char byte[MAX_INT_LENGTH] = { 0, };
1083 const char *signal_name = NULL;
1084 const char *net_popup_content = NULL;
1085 const char *net_popup_type = NULL;
1086 stc_s *stc = (stc_s *)stc_get_manager();
1089 STC_LOGE("Failed to get stc data");
1093 switch (limit_type) {
1094 case STC_RSTN_LIMIT_TYPE_DATA_WARN:
1096 signal_name = "WarnThresholdCrossed";
1097 net_popup_content = "warn threshold crossed";
1098 net_popup_type = "warning_noti";
1101 case STC_RSTN_LIMIT_TYPE_DATA:
1102 case STC_RSTN_LIMIT_TYPE_MONTHLY:
1103 case STC_RSTN_LIMIT_TYPE_WEEKLY:
1104 case STC_RSTN_LIMIT_TYPE_DAILY:
1106 signal_name = "RestrictionThresholdCrossed";
1107 net_popup_content = "restriction threshold crossed";
1108 net_popup_type = "restriction_noti";
1110 /* block immediately */
1111 context->counter->intend = NFACCT_BLOCK;
1112 __del_iptables_in(context->counter);
1113 __del_iptables_out(context->counter);
1114 __add_iptables_in(context->counter);
1115 __add_iptables_out(context->counter);
1117 __del_ip6tables_in(context->counter);
1118 __del_ip6tables_out(context->counter);
1119 __add_ip6tables_in(context->counter);
1120 __add_ip6tables_out(context->counter);
1121 context->counter->intend = NFACCT_COUNTER;
1123 rstn_value->limit_exceeded |= (1 << limit_type);
1130 if (signal_name == NULL) {
1131 STC_LOGE("Invalid parameter: limit_type");
1136 rv = stc_manager_dbus_emit_signal(stc->connection,
1137 STC_DBUS_SERVICE_RESTRICTION_PATH,
1138 STC_DBUS_INTERFACE_RESTRICTION,
1140 g_variant_new("(si)",
1145 rstn_value->limit_notified |= (1 << limit_type);
1147 snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
1148 snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->limit[limit_type]);
1149 stc_plugin_appstatus_send_message(net_popup_content,
1150 net_popup_type, rstn_key->app_id, iftype, byte);
1153 static gboolean __rstn_counter_update(stc_rstn_key_s *rstn_key,
1154 stc_rstn_value_s *rstn_value,
1155 classid_bytes_context_s *context)
1158 switch (context->counter->iotype) {
1159 case NFACCT_COUNTER_IN:
1160 case NFACCT_COUNTER_OUT:
1161 if ((rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] == 0 &&
1162 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA] >= 0) ||
1163 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] == 0 &&
1164 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] >= 0) ||
1165 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] == 0 &&
1166 rstn_value->limit[STC_RSTN_LIMIT_TYPE_MONTHLY] >= 0) ||
1167 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] == 0 &&
1168 rstn_value->limit[STC_RSTN_LIMIT_TYPE_WEEKLY] >= 0) ||
1169 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] == 0 &&
1170 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DAILY] >= 0)) {
1171 table_counters_info info;
1172 memset(&info, 0, sizeof(table_counters_info));
1173 table_counters_get(rstn_value->restriction_id, &info);
1175 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] = info.data_counter;
1176 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info.warn_counter;
1177 rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] = info.monthly_counter;
1178 rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] = info.weekly_counter;
1179 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] = info.daily_counter;
1182 for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
1183 if (rstn_value->limit[i] >= 0 &&
1184 !(rstn_value->limit_notified & (1 << i))) {
1185 rstn_value->counter[i] += context->bytes;
1186 if (rstn_value->limit[i] <= rstn_value->counter[i])
1187 __action_when_rstn_limit_exceeded(i,
1194 g_system->rstns_tree_updated = TRUE;
1195 __print_rstn(rstn_key, rstn_value);
1198 STC_LOGE("unknown iotype");
1204 static gboolean __interface_rstn_counter_update(stc_rstn_key_s *rstn_key,
1205 stc_rstn_value_s *rstn_value,
1206 classid_bytes_context_s *context)
1208 if ((rstn_value->classid == STC_TOTAL_DATACALL_CLASSID &&
1209 context->counter->iftype == STC_IFACE_DATACALL) ||
1210 (rstn_value->classid == STC_TOTAL_WIFI_CLASSID &&
1211 context->counter->iftype == STC_IFACE_WIFI) ||
1212 (rstn_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
1213 context->counter->iftype == STC_IFACE_BLUETOOTH) ||
1214 (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
1215 context->counter->iftype == STC_IFACE_WIFI) ||
1216 (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
1217 context->counter->iftype == STC_IFACE_BLUETOOTH) ||
1218 (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
1219 context->counter->iftype == STC_IFACE_USB) ||
1220 (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
1221 context->counter->iftype == STC_IFACE_P2P)) {
1222 context->counter->classid = rstn_value->classid;
1223 return __rstn_counter_update(rstn_key, rstn_value, context);
1229 static gboolean __rstn_counter_update_foreach_classid(gpointer key,
1233 gboolean rv = FALSE;
1234 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1235 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1236 classid_bytes_context_s *context = (classid_bytes_context_s *)data;
1239 if (context->counter->intend != NFACCT_COUNTER &&
1240 context->counter->intend != NFACCT_TETH_COUNTER)
1241 goto try_next_callback;
1243 if (rstn_value->limit_exceeded == TRUE) {
1244 context->data_limit_exceeded = TRUE; //LCOV_EXCL_LINE
1245 goto try_next_callback; //LCOV_EXCL_LINE
1248 classid = context->counter->classid;
1249 rv = __interface_rstn_counter_update(rstn_key, rstn_value, context);
1251 context->counter->classid = classid;
1252 if (rstn_value->classid != context->counter->classid)
1253 goto try_next_callback;
1255 rv = __rstn_counter_update(rstn_key, rstn_value, context);
1262 static gboolean __update_app_statistics(gpointer key, gpointer value,
1265 stc_app_key_s *app_key = (stc_app_key_s *)key;
1266 stc_app_value_s *app_value = (stc_app_value_s *)value;
1267 time_t *touch_time = (time_t *)data;
1268 stc_db_classid_iftype_key stat_key;
1269 stc_db_app_stats stat;
1270 default_connection_s *default_connection = stc_get_default_connection();
1272 memset(&stat_key, 0, sizeof(stc_db_classid_iftype_key));
1273 memset(&stat, 0 , sizeof(stc_db_app_stats));
1275 /* Do not update statistics for Tethering
1276 * if tethering is in-active found */
1277 if (default_connection &&
1278 default_connection->tether_state == FALSE &&
1279 !strcmp(app_key->app_id, STC_TOTAL_TETHERING))
1282 /* Do not update statistics for Wi-Fi
1283 * if tethering is active on wlan0 iface */
1284 if (default_connection && default_connection->tether_state &&
1285 default_connection->tether_iface.type == STC_IFACE_WIFI &&
1286 !strcmp(app_key->app_id, STC_TOTAL_WIFI))
1289 stat_key.classid = app_value->classid;
1291 if (app_value->classid == STC_TETHERING_APP_CLASSID &&
1292 default_connection->tether_state == TRUE)
1293 stat_key.iftype = default_connection->tether_iface.type;
1294 else if (g_str_has_suffix(app_key->app_id, STC_TETHERING_APP_SUFFIX))
1295 stat_key.iftype = default_connection->tether_iface.type;
1297 stat_key.iftype = default_connection->type;
1299 if (STC_IFACE_DATACALL == stat_key.iftype)
1300 stat_key.subscriber_id = g_strdup(default_connection->subscriber_id);
1302 stat_key.subscriber_id = g_strdup("none_subid"); //LCOV_EXCL_LINE
1304 if (app_value->classid == STC_TETHERING_APP_CLASSID &&
1305 default_connection->tether_state == TRUE)
1306 g_strlcpy(stat_key.ifname, default_connection->tether_iface.ifname,
1308 else if (g_str_has_suffix(app_key->app_id, STC_TETHERING_APP_SUFFIX))
1309 g_strlcpy(stat_key.ifname, default_connection->tether_iface.ifname,
1312 g_strlcpy(stat_key.ifname, default_connection->ifname,
1315 stat.app_id = g_strdup(app_key->app_id);
1316 stat.snd_count = app_value->counter.out_bytes;
1317 stat.rcv_count = app_value->counter.in_bytes;
1318 stat.is_roaming = default_connection->roaming;
1320 if (strstr(stat.app_id, "_BACKGROUND")) {
1321 stat.ground = STC_APP_STATE_BACKGROUND;
1323 if (strstr(stat.app_id, "TOTAL_"))
1324 stat.ground = STC_APP_STATE_UNKNOWN;
1326 stat.ground = STC_APP_STATE_FOREGROUND;
1329 table_statistics_insert(&stat_key, &stat, *touch_time);
1331 app_value->counter.out_bytes = 0;
1332 app_value->counter.in_bytes = 0;
1335 FREE(stat_key.subscriber_id);
1340 static gboolean __flush_apps_stats_to_database(gpointer user_data)
1342 time_t current_time = 0;
1343 stc_s *stc = stc_get_manager();
1345 if (stc && stc->carg)
1346 current_time = stc->carg->last_run_time;
1348 if (g_system->apps_tree_updated == FALSE)
1349 return G_SOURCE_REMOVE;
1351 g_system->apps_tree_updated = FALSE;
1354 g_tree_foreach(g_system->apps,
1355 __update_app_statistics,
1358 STC_LOGI("Flushed app stats to database");
1359 return G_SOURCE_REMOVE;
1363 static gboolean __update_counter_statistics(gpointer key, gpointer value,
1366 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1367 table_counters_info info = {
1368 .restriction_id = rstn_value->restriction_id,
1369 .data_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA],
1370 .warn_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN],
1371 .monthly_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY],
1372 .weekly_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY],
1373 .daily_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY]
1376 table_counters_update_counters(&info);
1381 static gboolean __flush_rstns_counter_to_database(gpointer user_data)
1383 time_t current_time = 0;
1384 stc_s *stc = stc_get_manager();
1386 if (stc && stc->carg)
1387 current_time = stc->carg->last_run_time;
1389 if (g_system->rstns_tree_updated == FALSE)
1390 return G_SOURCE_REMOVE;
1392 g_system->rstns_tree_updated = FALSE;
1394 if (g_system->rstns)
1395 g_tree_foreach(g_system->rstns,
1396 __update_counter_statistics,
1399 STC_LOGI("Flushed rstns counters to database");
1400 return G_SOURCE_REMOVE;
1404 static void __app_counter_update(stc_app_key_s *app_key,
1405 stc_app_value_s *app_value,
1406 classid_bytes_context_s *context)
1408 switch (context->counter->iotype) {
1409 case NFACCT_COUNTER_IN:
1410 app_value->data_usage.in_bytes += context->bytes;
1411 app_value->counter.in_bytes = context->bytes;
1412 g_system->apps_tree_updated = TRUE;
1415 __apps_tree_foreach_print(app_key, app_value, NULL); //LCOV_EXCL_LINE
1418 case NFACCT_COUNTER_OUT:
1419 app_value->data_usage.out_bytes += context->bytes;
1420 app_value->counter.out_bytes = context->bytes;
1421 g_system->apps_tree_updated = TRUE;
1424 __apps_tree_foreach_print(app_key, app_value, NULL); //LCOV_EXCL_LINE
1428 STC_LOGE("unknown iotype"); //LCOV_EXCL_LINE
1432 static void __interface_counter_update(stc_app_key_s *app_key,
1433 stc_app_value_s *app_value,
1434 classid_bytes_context_s *context)
1436 if ((app_value->classid == STC_TOTAL_DATACALL_CLASSID &&
1437 context->counter->iftype == STC_IFACE_DATACALL) ||
1438 (app_value->classid == STC_TOTAL_WIFI_CLASSID &&
1439 context->counter->iftype == STC_IFACE_WIFI) ||
1440 (app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
1441 context->counter->iftype == STC_IFACE_BLUETOOTH) ||
1442 (app_value->classid == STC_TETHERING_APP_CLASSID &&
1443 context->counter->iftype == STC_IFACE_WIFI) ||
1444 (app_value->classid == STC_TETHERING_APP_CLASSID &&
1445 context->counter->iftype == STC_IFACE_BLUETOOTH) ||
1446 (app_value->classid == STC_TETHERING_APP_CLASSID &&
1447 context->counter->iftype == STC_IFACE_USB) ||
1448 (app_value->classid == STC_TETHERING_APP_CLASSID &&
1449 context->counter->iftype == STC_IFACE_P2P))
1450 __app_counter_update(app_key, app_value, context);
1454 static gboolean __apps_counter_update_foreach_classid(gpointer key,
1458 stc_app_key_s *app_key = (stc_app_key_s *)key;
1459 stc_app_value_s *app_value = (stc_app_value_s *)value;
1460 classid_bytes_context_s *context = (classid_bytes_context_s *)data;
1462 if (context->counter->intend != NFACCT_COUNTER &&
1463 context->counter->intend != NFACCT_TETH_COUNTER)
1464 goto try_next_callback;
1466 __interface_counter_update(app_key, app_value, context);
1468 if (app_value->classid != context->counter->classid)
1469 goto try_next_callback;
1471 __app_counter_update(app_key, app_value, context);
1477 static gboolean __reset_time_counter_foreach_rstn(gpointer key,
1481 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1482 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1483 reset_time_limits_context_s *context = (reset_time_limits_context_s *)data;
1485 time_t now_month_start_ts;
1487 if (rstn_value->month_start_date == 0) {
1488 table_counters_info info;
1489 memset(&info, 0, sizeof(table_counters_info));
1490 table_counters_get_timestamps(rstn_value->restriction_id, &info);
1492 if (info.month_start_date == 0)
1493 rstn_value->month_start_date = 1;
1495 rstn_value->month_start_date = info.month_start_date;
1496 rstn_value->month_start_ts = info.month_start_ts;
1499 now_month_start_ts =
1500 stc_time_get_month_start(context->now,
1501 rstn_value->month_start_date);
1503 if (rstn_value->month_start_ts != now_month_start_ts) {
1504 rstn_value->month_start_ts = now_month_start_ts;
1505 context->month_start_ts = now_month_start_ts;
1506 context->is_updated |= (1 << STC_RSTN_LIMIT_TYPE_MONTHLY);
1509 if (context->is_updated) {
1510 table_counters_info info;
1511 memset(&info, 0, sizeof(table_counters_info));
1513 info.restriction_id = rstn_value->restriction_id;
1514 info.month_start_date = rstn_value->month_start_date;
1515 info.month_start_ts = rstn_value->month_start_ts;
1516 info.week_start_ts = context->week_start_ts;
1517 info.day_start_ts = context->day_start_ts;
1519 table_counters_update_timestamps(&info);
1522 for (i = STC_RSTN_LIMIT_TYPE_MONTHLY; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
1524 if ((context->is_updated) & (1 << i)) {
1526 rstn_value->counter[i] = 0;
1528 if (rstn_value->limit_exceeded & (1 << i)) {
1529 /* remove iptables rule */
1530 char *default_ifname = stc_default_connection_get_ifname();
1531 struct nfacct_rule counter;
1532 stc_s *stc = stc_get_manager();
1534 STC_LOGE("Can't get stc data");
1535 g_free(default_ifname);
1536 goto try_next_callback;
1540 stc->carg = MALLOC0(counter_arg_s, 1);
1541 if (stc->carg == NULL) {
1542 g_free(default_ifname);
1543 goto try_next_callback;
1547 stc_monitor_get_counter_socket();
1550 counter.carg = stc->carg;
1551 counter.classid = rstn_value->classid;
1552 counter.intend = NFACCT_BLOCK;
1553 counter.iftype = rstn_key->iftype;
1554 g_strlcpy(counter.ifname, default_ifname,
1557 g_free(default_ifname);
1560 __del_iptables_in(&counter);
1561 __del_iptables_out(&counter);
1563 /* ip6tables rule */
1564 __del_ip6tables_in(&counter);
1565 __del_ip6tables_out(&counter);
1567 rstn_value->rstn_state = STC_RSTN_STATE_DEACTIVATED;
1568 rstn_value->limit_exceeded &= ~(1 << i);
1569 rstn_value->limit_notified &= ~(1 << i);
1578 static void __reset_time_counters_if_required(void)
1580 reset_time_limits_context_s context;
1582 if (g_system == NULL) {
1583 STC_LOGE("stc monitor not initialized!");
1587 context.now = time(NULL);
1588 context.week_start_ts = stc_time_get_week_start(context.now);
1589 context.day_start_ts = stc_time_get_day_start(context.now);
1590 context.is_updated = 0;
1592 if (g_system->last_week_ts != context.week_start_ts) {
1593 g_system->last_week_ts = context.week_start_ts;
1594 context.is_updated |= (1 << STC_RSTN_LIMIT_TYPE_WEEKLY);
1597 if (g_system->last_day_ts != context.day_start_ts) {
1598 g_system->last_day_ts = context.day_start_ts;
1599 context.is_updated |= (1 << STC_RSTN_LIMIT_TYPE_DAILY);
1602 if (g_system->rstns) {
1603 g_tree_foreach(g_system->rstns,
1604 __reset_time_counter_foreach_rstn,
1606 if (context.is_updated)
1607 STC_LOGD("Counter reset completed month_start [%ld], week_start [%ld], day_start [%ld]",
1608 context.month_start_ts, g_system->last_week_ts, g_system->last_day_ts);
1612 static void __fill_nfacct_result(char *cnt_name, int64_t bytes,
1613 struct counter_arg *carg)
1615 __reset_time_counters_if_required();
1617 struct nfacct_rule counter = {
1624 classid_bytes_context_s context = {
1625 .counter = &counter,
1627 .data_limit_exceeded = FALSE,
1630 if (!recreate_counter_by_name(cnt_name, &counter)) {
1631 STC_LOGE("Can't parse counter name %s", cnt_name); //LCOV_EXCL_LINE
1632 return; //LCOV_EXCL_LINE
1636 STC_LOGI("classid %u, iftype %u, iotype %d, intend %d, ifname %s, bytes %lld",
1637 context.counter->classid, context.counter->iftype,
1638 context.counter->iotype, context.counter->intend,
1639 context.counter->ifname, context.bytes);
1641 if (g_system->rstns)
1642 g_tree_foreach(g_system->rstns,
1643 __rstn_counter_update_foreach_classid,
1647 g_tree_foreach(g_system->apps,
1648 __apps_counter_update_foreach_classid,
1652 static int __fill_counters(struct rtattr *attr_list[__NFACCT_MAX],
1655 struct counter_arg *carg = user_data;
1656 char *cnt_name = (char *)RTA_DATA(attr_list[NFACCT_NAME]);
1657 if (carg->initiate) {
1659 * TODO: this will be used when daemon starts to update existing
1660 * counter data if present.
1662 populate_counters(cnt_name, carg);
1666 (int64_t *)RTA_DATA(attr_list[NFACCT_BYTES]);
1667 int bytes = be64toh(*bytes_p);
1669 ++carg->serialized_counters;
1670 __fill_nfacct_result(cnt_name, bytes, carg);
1677 static int __post_fill_counters(void *user_data)
1679 struct counter_arg *carg = user_data;
1687 static void __process_network_counter(struct genl *ans,
1688 struct counter_arg *carg)
1690 struct netlink_serialization_params ser_params = {
1693 .eval_attr = __fill_counters,
1694 .post_eval_attr = __post_fill_counters,
1697 netlink_serialization_command *netlink =
1698 netlink_create_command(&ser_params);
1700 STC_LOGE("Can not create command"); //LCOV_EXCL_LINE
1701 return; //LCOV_EXCL_LINE
1704 netlink->deserialize_answer(&(netlink->params));
1707 static gboolean __process_contr_reply(GIOChannel *source,
1708 GIOCondition condition,
1711 int sock = g_io_channel_unix_get_fd(source);
1714 stc_s *stc = stc_get_manager();
1717 void __gcov_flush(void);
1721 if ((condition & G_IO_ERR) || (condition & G_IO_HUP) ||
1722 (condition & G_IO_NVAL)) {
1723 /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */
1725 STC_LOGE("Counter socket received G_IO event, closing socket." //LCOV_EXCL_LINE
1726 "G_IO_ERR [%u], G_IO_HUP [%u], G_IO_NVAL [%u]",
1727 (condition & G_IO_ERR), (condition & G_IO_HUP),
1728 (condition & G_IO_NVAL));
1729 __close_and_reopen_contr_sock(g_system); //LCOV_EXCL_LINE
1730 return FALSE; //LCOV_EXCL_LINE
1733 ans = MALLOC0(struct genl, 1);
1735 STC_LOGE("Failed allocate memory to genl reply message"); //LCOV_EXCL_LINE
1736 return TRUE; //LCOV_EXCL_LINE
1740 STC_LOGE("Can't get stc data"); //LCOV_EXCL_LINE
1741 goto out; //LCOV_EXCL_LINE
1744 ret = read_netlink(sock, ans, sizeof(struct genl));
1745 /* STC_LOGD("Counter data received ret [%d]", ret); */
1749 stc->carg->ans_len = ret;
1750 stc->carg->last_run_time = time(NULL);
1752 __process_network_counter(ans, stc->carg);
1754 g_idle_add(__flush_apps_stats_to_database, NULL);
1755 g_idle_add(__flush_rstns_counter_to_database, NULL);
1762 static gboolean __update_contr_cb(void *user_data)
1764 /* Here we just sent command, answer we receive in another callback */
1765 stc_s *stc = stc_get_manager();
1766 ret_value_msg_if(stc == NULL, STC_ERROR_FAIL, "Can't get stc data");
1768 stc->carg = MALLOC0(counter_arg_s, 1);
1769 if (stc->carg == NULL)
1770 return TRUE; /* we need to continue the timer */
1772 stc->carg->sock = stc_monitor_get_counter_socket();
1776 void __gcov_flush(void);
1780 /* STC_LOGD("Get all counters"); */
1781 nfacct_send_get_all(stc->carg);
1783 /* we need to continue the timer */
1789 static gboolean __rstn_tree_foreach_print(gpointer key, gpointer value,
1792 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1793 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1795 __print_rstn(rstn_key, rstn_value);
1799 static void __rstn_tree_printall(void)
1801 g_tree_foreach(g_system->rstns, __rstn_tree_foreach_print, NULL);
1806 static stc_rstn_value_s * __rstn_lookup(GTree *rstns_tree,
1807 const stc_rstn_key_s *key)
1809 stc_rstn_value_s *lookup;
1811 ret_value_msg_if(rstns_tree == NULL, NULL, "rstns_tree is null!");
1813 lookup = g_tree_lookup(rstns_tree, key);
1818 static gboolean __remove_restriction(gpointer key, gpointer value,
1821 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1822 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1824 __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
1825 __print_rstn(rstn_key, rstn_value);
1829 static gboolean __add_restriction_debug(gpointer key, gpointer value,
1832 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1833 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1835 /* rstn rule is activated */
1836 if (rstn_value->rstn_state == STC_RSTN_STATE_ACTIVATED)
1839 if (rstn_value->rstn_type == STC_RSTN_TYPE_ACCEPT)
1840 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1842 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1844 __print_rstn(rstn_key, rstn_value);
1850 static gboolean __add_restriction(gpointer key, gpointer value, gpointer data)
1852 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1853 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1855 /* rstn rule is activated */
1856 if (rstn_value->rstn_state == STC_RSTN_STATE_ACTIVATED)
1859 if (rstn_value->rstn_type == STC_RSTN_TYPE_ACCEPT)
1860 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1862 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1868 static stc_error_e __rstn_tree_remove(stc_rstn_key_s *key)
1870 stc_rstn_value_s *lookup_value;
1872 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1874 lookup_value = __rstn_lookup(g_system->rstns, key);
1875 if (!lookup_value) {
1876 STC_LOGE("key not found"); //LCOV_EXCL_LINE
1877 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
1880 /* remove counter also */
1881 table_counters_delete(lookup_value->restriction_id);
1882 __remove_restriction(key, lookup_value, NULL);
1884 if (!g_tree_remove(g_system->rstns, key)) {
1885 STC_LOGD("key not found"); //LCOV_EXCL_LINE
1886 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
1889 return STC_ERROR_NONE;
1892 static stc_error_e __rstn_tree_add(stc_rstn_key_s *key,
1893 stc_rstn_value_s *value, gboolean debug)
1895 stc_rstn_key_s *rstn_key;
1896 stc_rstn_value_s *rstn_value;
1898 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1900 rstn_value = __rstn_lookup(g_system->rstns, key);
1902 __rstn_tree_remove(key);
1904 rstn_key = MALLOC0(stc_rstn_key_s, 1);
1906 STC_LOGE("rstn_key allocation failed"); //LCOV_EXCL_LINE
1907 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1910 rstn_value = MALLOC0(stc_rstn_value_s, 1);
1912 STC_LOGE("rstn_value allocation failed"); //LCOV_EXCL_LINE
1913 FREE(rstn_key); //LCOV_EXCL_LINE
1914 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1917 rstn_key->app_id = g_strdup(key->app_id);
1918 rstn_key->ifname = g_strdup(key->ifname);
1919 rstn_key->subscriber_id = g_strdup(key->subscriber_id);
1920 rstn_key->iftype = key->iftype;
1921 rstn_key->roaming = key->roaming;
1923 g_tree_insert(g_system->rstns, rstn_key, rstn_value);
1925 rstn_value->restriction_id = value->restriction_id;
1926 rstn_value->rstn_state = value->rstn_state;
1927 rstn_value->rstn_type = value->rstn_type;
1928 rstn_value->classid = value->classid;
1931 for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
1932 rstn_value->limit[i] = value->limit[i];
1933 rstn_value->counter[i] = 0;
1936 rstn_value->limit_exceeded = 0;
1937 rstn_value->limit_notified = 0;
1938 rstn_value->month_start_date = value->month_start_date;
1939 rstn_value->month_start_ts = value->month_start_ts;
1942 __add_restriction_debug(key, rstn_value, NULL);
1944 __add_restriction(key, rstn_value, NULL);
1946 return STC_ERROR_NONE;
1950 static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info,
1953 stc_cb_ret_e ret = STC_CONTINUE;
1956 stc_rstn_value_s value;
1958 memset(&key, 0, sizeof(stc_rstn_key_s));
1959 memset(&value, 0, sizeof(stc_rstn_value_s));
1961 key.app_id = g_strdup(info->app_id);
1962 key.ifname = g_strdup(info->ifname);
1963 key.subscriber_id = g_strdup(info->subscriber_id);
1964 key.iftype = info->iftype;
1965 key.roaming = info->roaming;
1967 value.rstn_type = info->rstn_type;
1968 value.rstn_state = STC_RSTN_STATE_UNKNOWN;
1969 value.restriction_id = info->restriction_id;
1972 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1974 value.classid = STC_UNKNOWN_CLASSID;
1976 value.limit[STC_RSTN_LIMIT_TYPE_DATA] = info->data_limit;
1977 value.limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info->data_warn_limit;
1978 value.limit[STC_RSTN_LIMIT_TYPE_MONTHLY] = info->monthly_limit;
1979 value.limit[STC_RSTN_LIMIT_TYPE_WEEKLY] = info->weekly_limit;
1980 value.limit[STC_RSTN_LIMIT_TYPE_DAILY] = info->daily_limit;
1982 if (__rstn_tree_add(&key, &value, FALSE) != STC_ERROR_NONE)
1987 FREE(key.subscriber_id);
1991 static void __fill_restritions_list(void)
1993 table_restrictions_foreach(__insert_restriction_cb, NULL);
1995 /* __rstn_tree_printall(); */
1998 static gboolean __add_rstn_foreach_application(gpointer key,
2002 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
2003 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
2004 gchar *app_id = (gchar *)data;
2006 /* rstn rule is not for applications */
2007 if (rstn_key->app_id == NULL)
2010 /* rstn rule is not for this application */
2011 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
2014 /* rstn rule is already applied */
2015 if (rstn_value->rstn_state == STC_RSTN_STATE_ACTIVATED)
2018 /* add restriction to system */
2019 if (rstn_value->rstn_type == STC_RSTN_TYPE_ACCEPT)
2020 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, NULL);
2022 __process_restriction(RST_SET, rstn_key, rstn_value, NULL);
2024 __print_rstn(rstn_key, rstn_value);
2030 static void __add_rstns_for_application(gchar *app_id)
2032 g_tree_foreach(g_system->rstns, __add_rstn_foreach_application,
2036 static void __add_application_by_interface(const char *app_id)
2038 stc_app_key_s app_key;
2039 stc_app_value_s app_value;
2042 return; //LCOV_EXCL_LINE
2044 memset(&app_key, 0, sizeof(stc_app_key_s));
2045 memset(&app_value, 0, sizeof(stc_app_value_s));
2047 app_key.pkg_id = g_strdup(app_id);
2048 app_key.app_id = g_strdup(app_id);
2050 app_value.type = STC_APP_TYPE_NONE;
2051 app_value.processes = NULL;
2052 app_value.counter.in_bytes = 0;
2053 app_value.counter.out_bytes = 0;
2055 stc_monitor_application_add(app_key, app_value);
2057 FREE(app_key.pkg_id);
2058 FREE(app_key.app_id);
2061 static int __vconf_get_int(const char *key, int *value)
2065 ret = vconf_get_int(key, value);
2066 if (ret != VCONF_OK) {
2067 STC_LOGE("Failed to get vconfkey [%s] value", key); //LCOV_EXCL_LINE
2068 return -1; //LCOV_EXCL_LINE
2075 static int __vconf_set_int(const char *key, int value)
2079 ret = vconf_set_int(key, value);
2080 if (ret != VCONF_OK) {
2081 STC_LOGE("Failed to set vconfkey [%s] value", key); //LCOV_EXCL_LINE
2082 return -1; //LCOV_EXCL_LINE
2088 static guint __get_background_state(void)
2090 return g_system->background_state;;
2093 static void __set_background_state(guint state)
2095 g_system->background_state = state;
2098 static gboolean __processes_tree_foreach_background(gpointer key,
2102 stc_process_key_s *proc_key = (stc_process_key_s *)key;
2103 stc_app_key_s *app_key = (stc_app_key_s *)data;
2105 if (g_system->background_state)
2106 place_pids_to_net_cgroup(proc_key->pid, STC_BACKGROUND_APP_ID);
2108 place_pids_to_net_cgroup(proc_key->pid, app_key->app_id);
2113 static gboolean __apps_tree_foreach_background(gpointer key, gpointer value,
2116 stc_app_key_s *app_key = (stc_app_key_s *)key;
2117 stc_app_value_s *app_value = (stc_app_value_s *)value;
2119 if (strstr(app_key->app_id, STC_BACKGROUND_APP_SUFFIX))
2120 g_tree_foreach(app_value->processes,
2121 __processes_tree_foreach_background, app_key);
2126 static stc_error_e __process_update_background(void)
2128 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2130 g_tree_foreach(g_system->apps, __apps_tree_foreach_background, NULL);
2132 return STC_ERROR_NONE;
2136 static void __fill_exceptions_list(void)
2138 stc_plugin_fill_exception_list();
2141 stc_error_e stc_monitor_init(void)
2143 stc_system_s *system = MALLOC0(stc_system_s, 1);
2144 GIOChannel *gio = NULL;
2146 ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY, "stc_system_s malloc fail!");
2148 /* initializing current classid */
2149 init_current_classid();
2151 /* initializing cgroups */
2154 /* creating monitored application tree */
2155 system->apps = g_tree_new_full(__apps_tree_key_compare, NULL,
2156 __apps_tree_key_free,
2157 __apps_tree_value_free);
2159 system->rstns = g_tree_new_full(__rstns_tree_key_compare, NULL,
2160 __rstns_tree_key_free,
2161 __rstns_tree_value_free);
2163 /* create netlink socket for updating kernel counters */
2164 system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
2165 if (system->contr_sock < 0) {
2166 STC_LOGE("failed to open socket"); //LCOV_EXCL_LINE
2167 FREE(system); //LCOV_EXCL_LINE
2168 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
2171 gio = g_io_channel_unix_new(system->contr_sock);
2172 system->contr_gsource_id =
2173 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
2174 (GIOFunc) __process_contr_reply,
2176 g_io_channel_unref(gio);
2180 __add_application_by_interface(STC_TOTAL_DATACALL);
2181 __add_application_by_interface(STC_TOTAL_WIFI);
2182 __add_application_by_interface(STC_TOTAL_BLUETOOTH);
2183 __add_application_by_interface(STC_TOTAL_IPV4);
2184 __add_application_by_interface(STC_TOTAL_IPV6);
2185 __add_application_by_interface(STC_TOTAL_TETHERING);
2187 /* creating restriction rules tree */
2188 __update_contr_cb(NULL);
2190 /* registering periodic kernel counters update callback */
2191 g_system->contr_timer_id = g_timeout_add_seconds(CONTR_TIMER_INTERVAL,
2194 if (g_system->contr_timer_id == 0) {
2195 STC_LOGE("Failed to register kernel counters update timer"); //LCOV_EXCL_LINE
2196 __close_contr_sock(g_system); //LCOV_EXCL_LINE
2197 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
2200 __vconf_get_int(VCONFKEY_STC_BACKGROUND_STATE,
2201 (int *)&g_system->background_state);
2203 __fill_exceptions_list();
2204 __fill_restritions_list();
2206 return STC_ERROR_NONE;
2209 stc_error_e stc_monitor_deinit(void)
2211 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2213 /* close netlink socket for updating kernel counters */
2214 __close_contr_sock(g_system);
2216 /* remove kernel counters update timer */
2217 if (g_system->contr_timer_id > 0) {
2218 g_source_remove(g_system->contr_timer_id);
2219 g_system->contr_timer_id = 0;
2222 /* destroy monitored application tree */
2223 g_tree_destroy(g_system->apps);
2224 g_system->apps = NULL;
2226 /* destroy restriction rules tree */
2227 g_tree_destroy(g_system->rstns);
2228 g_system->rstns = NULL;
2232 return STC_ERROR_NONE;
2235 API stc_error_e stc_monitor_application_add(const stc_app_key_s app_key,
2236 const stc_app_value_s app_value)
2238 stc_error_e ret = STC_ERROR_NONE;
2240 stc_app_value_s *value;
2241 stc_app_value_s *lookup;
2243 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2245 lookup = __application_lookup(g_system->apps, &app_key);
2247 return STC_ERROR_NONE; //LCOV_EXCL_LINE
2249 key = MALLOC0(stc_app_key_s, 1);
2251 STC_LOGE("key allocation failed"); //LCOV_EXCL_LINE
2252 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
2255 value = MALLOC0(stc_app_value_s, 1);
2257 STC_LOGE("value allocation failed"); //LCOV_EXCL_LINE
2258 FREE(key); //LCOV_EXCL_LINE
2259 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
2262 key->app_id = g_strdup(app_key.app_id);
2263 key->pkg_id = g_strdup(app_key.pkg_id);
2265 value->type = app_value.type;
2266 value->data_usage.in_bytes = app_value.data_usage.in_bytes;
2267 value->data_usage.out_bytes = app_value.data_usage.out_bytes;
2268 g_strlcpy(value->ipaddr, app_value.ipaddr, IPV4_IPADDRESS_LEN);
2270 value->processes = g_tree_new_full(__processes_tree_key_compare, NULL,
2271 __processes_tree_key_free,
2272 __processes_tree_value_free);
2274 /* create cgroup and update classid */
2275 value->classid = get_classid_by_app_id(app_key.app_id, TRUE);
2277 g_tree_insert(g_system->apps, key, value);
2279 /* add nfacct rule for this classid */
2280 __add_application_monitor(key, value, stc_get_default_connection());
2281 __add_rstns_for_application(app_key.app_id);
2286 API stc_error_e stc_monitor_application_remove(const stc_app_key_s app_key)
2288 stc_error_e ret = STC_ERROR_NONE;
2289 stc_app_value_s *app_lookup;
2291 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2293 app_lookup = __application_lookup(g_system->apps, &app_key);
2296 STC_LOGD("app_key not found"); //LCOV_EXCL_LINE
2297 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
2300 /* remove nfacct rule for this classid */
2301 __remove_application_monitor((gpointer) &app_key, app_lookup,
2302 stc_get_default_connection());
2304 /* remove ristrictions if any */
2305 __remove_rstns_for_application(app_key.app_id);
2307 /* remove app_key from the stc-manager */
2308 if (!g_tree_remove(g_system->apps, &app_key)) {
2309 ret = STC_ERROR_NO_DATA;
2310 STC_LOGE("key not found");
2316 API stc_error_e stc_monitor_process_add(const stc_app_key_s app_key,
2317 const stc_process_key_s proc_key,
2318 const stc_process_value_s proc_value)
2320 stc_error_e ret = STC_ERROR_NONE;
2321 stc_app_value_s *app_lookup;
2322 stc_process_key_s *key;
2323 stc_process_value_s *value;
2324 stc_process_value_s *proc_lookup;
2326 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2328 app_lookup = __application_lookup(g_system->apps, &app_key);
2331 STC_LOGD("app_key not found"); //LCOV_EXCL_LINE
2332 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
2335 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
2337 return STC_ERROR_NONE; //LCOV_EXCL_LINE
2339 key = MALLOC0(stc_process_key_s, 1);
2341 STC_LOGE("key allocation failed"); //LCOV_EXCL_LINE
2342 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
2345 value = MALLOC0(stc_process_value_s, 1);
2347 STC_LOGE("value allocation failed"); //LCOV_EXCL_LINE
2348 FREE(key); //LCOV_EXCL_LINE
2349 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
2352 key->pid = proc_key.pid;
2354 value->ground = proc_value.ground;
2356 g_tree_insert(app_lookup->processes, key, value);
2358 /* add pid to application cgroup */
2359 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
2362 __apps_tree_printall(); //LCOV_EXCL_LINE
2368 API stc_error_e stc_monitor_process_remove(pid_t pid)
2370 stc_error_e ret = STC_ERROR_NONE;
2371 stc_process_key_s proc_key = {
2375 remove_pid_context_s context = {
2377 .proc_key = &proc_key,
2378 .entry_removed = FALSE,
2381 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2383 g_tree_foreach(g_system->apps, __apps_tree_foreach_remove_pid,
2386 if (context.entry_removed)
2387 __application_remove_if_empty(context.app_key);
2390 __apps_tree_printall(); //LCOV_EXCL_LINE
2397 API stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key,
2398 const stc_process_key_s proc_key,
2399 stc_app_state_e ground)
2401 stc_error_e ret = STC_ERROR_NONE;
2402 stc_app_value_s *app_lookup;
2403 stc_process_value_s *proc_lookup;
2405 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2407 app_lookup = __application_lookup(g_system->apps, &app_key);
2409 STC_LOGD("app_key not found");
2410 return STC_ERROR_FAIL;
2413 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
2415 STC_LOGD("proc_key not found");
2416 return STC_ERROR_FAIL;
2419 if (proc_lookup->ground != ground)
2420 proc_lookup->ground = ground;
2422 if (ground == STC_APP_STATE_BACKGROUND && __get_background_state())
2423 place_pids_to_net_cgroup(proc_key.pid, STC_BACKGROUND_APP_ID);
2425 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
2431 void stc_monitor_update_rstn_by_default_connection(void *data)
2433 static default_connection_s old_connection;
2434 default_connection_s *new_connection = (default_connection_s *)data;
2436 if (old_connection.path != NULL) {
2439 g_tree_foreach(g_system->apps,
2440 __remove_application_monitor,
2441 (gpointer)&old_connection);
2443 if (g_system->rstns)
2444 g_tree_foreach(g_system->rstns,
2445 __remove_restriction,
2446 (gpointer)&old_connection);
2448 iptables_flush_chains();
2452 FREE(old_connection.path);
2453 FREE(old_connection.ifname);
2454 FREE(old_connection.tether_iface.ifname);
2455 old_connection.type = 0;
2456 old_connection.roaming = 0;
2457 old_connection.tether_state = FALSE;
2458 old_connection.tether_iface.type = 0;
2460 if (new_connection != NULL && new_connection->path != NULL) {
2462 g_tree_foreach(g_system->apps,
2463 __add_application_monitor,
2464 (gpointer)new_connection);
2466 if (g_system->rstns)
2467 g_tree_foreach(g_system->rstns, __add_restriction,
2470 old_connection.path = g_strdup(new_connection->path);
2471 old_connection.ifname = g_strdup(new_connection->ifname);
2472 old_connection.tether_iface.ifname = g_strdup(new_connection->tether_iface.ifname);
2473 old_connection.type = new_connection->type;
2474 old_connection.roaming = new_connection->roaming;
2475 old_connection.tether_state = new_connection->tether_state;
2476 old_connection.tether_iface.type = new_connection->tether_iface.type;
2480 stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info)
2485 stc_rstn_value_s value;
2487 memset(&key, 0, sizeof(stc_rstn_key_s));
2488 memset(&value, 0, sizeof(stc_rstn_value_s));
2490 key.app_id = g_strdup(info->app_id);
2491 key.ifname = g_strdup(info->ifname);
2492 key.subscriber_id = g_strdup(info->subscriber_id);
2493 key.iftype = info->iftype;
2494 key.roaming = info->roaming;
2496 value.rstn_type = info->rstn_type;
2497 value.rstn_state = STC_RSTN_STATE_UNKNOWN;
2498 value.restriction_id = info->restriction_id;
2501 value.classid = get_classid_by_app_id(info->app_id, TRUE);
2503 value.classid = STC_UNKNOWN_CLASSID;
2505 if (value.classid == STC_BACKGROUND_APP_CLASSID) {
2506 __set_background_state(TRUE); //LCOV_EXCL_LINE
2507 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state); //LCOV_EXCL_LINE
2508 __process_update_background(); //LCOV_EXCL_LINE
2511 value.limit[STC_RSTN_LIMIT_TYPE_DATA] = info->data_limit;
2512 value.limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info->data_warn_limit;
2513 value.limit[STC_RSTN_LIMIT_TYPE_MONTHLY] = info->monthly_limit;
2514 value.limit[STC_RSTN_LIMIT_TYPE_WEEKLY] = info->weekly_limit;
2515 value.limit[STC_RSTN_LIMIT_TYPE_DAILY] = info->daily_limit;
2516 value.month_start_date = info->month_start_date;
2517 value.month_start_ts = stc_time_get_month_start(time(NULL),
2518 info->month_start_date);
2520 ret = __rstn_tree_add(&key, &value, TRUE);
2524 FREE(key.subscriber_id);
2528 stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info)
2532 stc_rstn_key_s key = {
2533 .app_id = g_strdup(info->app_id),
2534 .ifname = g_strdup(info->ifname),
2535 .subscriber_id = g_strdup(info->subscriber_id),
2536 .iftype = info->iftype,
2537 .roaming = info->roaming,
2540 if (!strcmp(key.app_id, STC_BACKGROUND_APP_ID)) {
2541 __set_background_state(FALSE); //LCOV_EXCL_LINE
2542 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state); //LCOV_EXCL_LINE
2543 __process_update_background(); //LCOV_EXCL_LINE
2546 ret = __rstn_tree_remove(&key);
2550 FREE(key.subscriber_id);
2554 API stc_error_e stc_monitor_check_excn_by_cmdline(char *cmdline)
2556 return stc_plugin_check_exception_by_cmdline(cmdline);
2559 int stc_monitor_get_counter_socket(void)
2561 return g_system->contr_sock;