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"
31 #include "stc-manager-plugin-appstatus.h"
32 #include "stc-manager-plugin-exception.h"
34 #define MAX_INT_LENGTH 128
35 #define VCONFKEY_STC_BACKGROUND_STATE "db/stc/background_state"
38 stc_app_key_s *app_key;
39 stc_process_key_s *proc_key;
40 gboolean entry_removed;
41 } remove_pid_context_s;
44 struct nfacct_rule *counter;
46 gboolean data_limit_reached;
47 } classid_bytes_context_s;
49 static stc_system_s *g_system = NULL;
51 static nfacct_rule_jump __get_jump_by_intend(struct nfacct_rule *counter)
53 if (counter->intend == NFACCT_WARN)
54 return NFACCT_JUMP_ACCEPT;
55 else if (counter->intend == NFACCT_BLOCK)
56 return NFACCT_JUMP_REJECT;
57 else if (counter->intend == NFACCT_ALLOW)
58 return NFACCT_JUMP_ACCEPT;
60 return NFACCT_JUMP_UNKNOWN;
63 static stc_error_e __add_iptables_in(struct nfacct_rule *counter)
66 return STC_ERROR_INVALID_PARAMETER;
68 counter->action = NFACCT_ACTION_INSERT;
69 counter->iotype = NFACCT_COUNTER_IN;
70 counter->jump = __get_jump_by_intend(counter);
71 counter->iptype = NFACCT_TYPE_IPV4;
72 counter->send_limit = 0;
73 counter->rcv_limit = 0;
75 return produce_net_rule(counter);
78 static stc_error_e __add_iptables_out(struct nfacct_rule *counter)
81 return STC_ERROR_INVALID_PARAMETER;
83 counter->action = NFACCT_ACTION_INSERT;
84 counter->iotype = NFACCT_COUNTER_OUT;
85 counter->jump = __get_jump_by_intend(counter);
86 counter->iptype = NFACCT_TYPE_IPV4;
87 counter->send_limit = 0;
88 counter->rcv_limit = 0;
90 return produce_net_rule(counter);
93 static stc_error_e __del_iptables_in(struct nfacct_rule *counter)
96 return STC_ERROR_INVALID_PARAMETER;
98 counter->action = NFACCT_ACTION_DELETE;
99 counter->iotype = NFACCT_COUNTER_IN;
100 counter->jump = __get_jump_by_intend(counter);
101 counter->iptype = NFACCT_TYPE_IPV4;
102 counter->send_limit = 0;
103 counter->rcv_limit = 0;
105 return produce_net_rule(counter);
108 static stc_error_e __del_iptables_out(struct nfacct_rule *counter)
111 return STC_ERROR_INVALID_PARAMETER;
113 counter->action = NFACCT_ACTION_DELETE;
114 counter->iotype = NFACCT_COUNTER_OUT;
115 counter->jump = __get_jump_by_intend(counter);
116 counter->iptype = NFACCT_TYPE_IPV4;
117 counter->send_limit = 0;
118 counter->rcv_limit = 0;
120 return produce_net_rule(counter);
123 static stc_error_e __add_ip6tables_in(struct nfacct_rule *counter)
126 return STC_ERROR_INVALID_PARAMETER;
128 counter->action = NFACCT_ACTION_INSERT;
129 counter->iotype = NFACCT_COUNTER_IN;
130 counter->jump = __get_jump_by_intend(counter);
131 counter->iptype = NFACCT_TYPE_IPV6;
132 counter->send_limit = 0;
133 counter->rcv_limit = 0;
135 return produce_net_rule(counter);
138 static stc_error_e __add_ip6tables_out(struct nfacct_rule *counter)
141 return STC_ERROR_INVALID_PARAMETER;
143 counter->action = NFACCT_ACTION_INSERT;
144 counter->iotype = NFACCT_COUNTER_OUT;
145 counter->jump = __get_jump_by_intend(counter);
146 counter->iptype = NFACCT_TYPE_IPV6;
147 counter->send_limit = 0;
148 counter->rcv_limit = 0;
150 return produce_net_rule(counter);
153 static stc_error_e __del_ip6tables_in(struct nfacct_rule *counter)
156 return STC_ERROR_INVALID_PARAMETER;
158 counter->action = NFACCT_ACTION_DELETE;
159 counter->iotype = NFACCT_COUNTER_IN;
160 counter->jump = __get_jump_by_intend(counter);
161 counter->iptype = NFACCT_TYPE_IPV6;
162 counter->send_limit = 0;
163 counter->rcv_limit = 0;
165 return produce_net_rule(counter);
168 static stc_error_e __del_ip6tables_out(struct nfacct_rule *counter)
171 return STC_ERROR_INVALID_PARAMETER;
173 counter->action = NFACCT_ACTION_DELETE;
174 counter->iotype = NFACCT_COUNTER_OUT;
175 counter->jump = __get_jump_by_intend(counter);
176 counter->iptype = NFACCT_TYPE_IPV6;
177 counter->send_limit = 0;
178 counter->rcv_limit = 0;
180 return produce_net_rule(counter);
183 static int __processes_tree_key_compare(gconstpointer a, gconstpointer b,
184 gpointer UNUSED user_data)
186 stc_process_key_s *key_a = (stc_process_key_s *)a;
187 stc_process_key_s *key_b = (stc_process_key_s *)b;
189 return key_a->pid - key_b->pid;
192 static void __processes_tree_value_free(gpointer data)
194 stc_process_value_s *value = (stc_process_value_s *)data;
199 static void __processes_tree_key_free(gpointer data)
201 stc_process_key_s *key = (stc_process_key_s *)data;
206 static int __apps_tree_key_compare(gconstpointer a, gconstpointer b,
207 gpointer UNUSED user_data)
209 stc_app_key_s *key_a = (stc_app_key_s *)a;
210 stc_app_key_s *key_b = (stc_app_key_s *)b;
213 ret = g_strcmp0(key_a->pkg_id, key_b->pkg_id);
217 return g_strcmp0(key_a->app_id, key_b->app_id);
220 static void __apps_tree_value_free(gpointer data)
222 stc_app_value_s *value = (stc_app_value_s *)data;
224 g_tree_destroy(value->processes);
225 value->processes = NULL;
230 static void __apps_tree_key_free(gpointer data)
232 stc_app_key_s *key = (stc_app_key_s *)data;
239 static int __rstns_tree_key_compare(gconstpointer a, gconstpointer b,
240 gpointer UNUSED user_data)
242 stc_rstn_key_s *key_a = (stc_rstn_key_s *)a;
243 stc_rstn_key_s *key_b = (stc_rstn_key_s *)b;
246 ret = g_strcmp0(key_a->app_id, key_b->app_id);
250 ret = g_strcmp0(key_a->ifname, key_b->ifname);
254 ret = g_strcmp0(key_a->subscriber_id, key_b->subscriber_id);
258 ret = key_a->iftype - key_b->iftype;
262 ret = key_a->roaming - key_b->roaming;
269 static void __rstns_tree_value_free(gpointer data)
271 stc_rstn_value_s *value = (stc_rstn_value_s *)data;
276 static void __rstns_tree_key_free(gpointer data)
278 stc_rstn_key_s *key = (stc_rstn_key_s *)data;
282 FREE(key->subscriber_id);
287 static gboolean __processes_tree_foreach_print(gpointer key, gpointer value,
290 stc_process_key_s *proc_key = (stc_process_key_s *)key;
291 stc_process_value_s *proc_value = (stc_process_value_s *)value;
293 STC_LOGD("Process entry => PID [\033[1;33m%d\033[0;m], Ground state [%d]",
294 proc_key->pid, proc_value->ground);
298 static void __processes_tree_printall(GTree *processes)
300 g_tree_foreach(processes, __processes_tree_foreach_print, NULL);
303 static gboolean __apps_tree_foreach_print(gpointer key, gpointer value,
306 stc_app_key_s *app_key = (stc_app_key_s *)key;
307 stc_app_value_s *app_value = (stc_app_value_s *)value;
309 STC_LOGD("Application info => Pkg ID [\033[0;34m%s\033[0;m], "
310 "App ID [\033[0;32m%s\033[0;m], Type [%d], classid [%d],"
311 " counter [ in (%lld), out (%lld)]",
312 app_key->pkg_id, app_key->app_id,
313 app_value->type, app_value->classid,
314 app_value->data_usage.in_bytes, app_value->data_usage.out_bytes);
316 __processes_tree_printall(app_value->processes);
320 static void __apps_tree_printall(void)
322 g_tree_foreach(g_system->apps, __apps_tree_foreach_print, NULL);
326 static gboolean __apps_tree_foreach_remove_pid(gpointer key, gpointer value,
329 remove_pid_context_s *context = (remove_pid_context_s *)data;
330 stc_app_value_s *app_value = (stc_app_value_s *)value;
332 if (!g_tree_remove(app_value->processes, context->proc_key)) {
334 STC_LOGD("key not found"); //LCOV_EXCL_LINE
338 context->entry_removed = TRUE;
339 context->app_key = (stc_app_key_s *)key;
344 static stc_app_value_s * __application_lookup(GTree *apps,
345 const stc_app_key_s *key)
347 stc_app_value_s *lookup;
349 ret_value_msg_if(apps == NULL, NULL, "apps is null!");
351 lookup = g_tree_lookup(apps, key);
356 static stc_process_value_s * __process_lookup(GTree *processes,
357 const stc_process_key_s *key)
359 stc_process_value_s *lookup;
361 ret_value_msg_if(processes == NULL, NULL, "processes is null!");
363 lookup = g_tree_lookup(processes, key);
369 static gboolean __processes_tree_check_empty(gpointer key, gpointer value,
372 guint *pid_count = (guint *)data;
378 static gboolean __add_application_monitor(gpointer key, gpointer value,
381 stc_app_value_s *app_value = (stc_app_value_s *)value;
382 default_connection_s *connection = (default_connection_s *)data;
383 stc_s *stc = stc_get_manager();
385 if (app_value->classid == STC_TOTAL_DATACALL_CLASSID ||
386 app_value->classid == STC_TOTAL_WIFI_CLASSID ||
387 app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID)
390 if (stc && connection && connection->ifname) {
391 struct nfacct_rule counter;
394 stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
395 if (stc->carg == NULL) //LCOV_EXCL_LINE
396 return FALSE; //LCOV_EXCL_LINE
398 stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
401 memset(&counter, 0, sizeof(struct nfacct_rule));
403 counter.carg = stc->carg;
404 counter.classid = app_value->classid;
405 counter.intend = NFACCT_COUNTER;
407 if (connection->tether_state == TRUE &&
408 connection->tether_iface.ifname != NULL &&
409 app_value->classid == STC_TETHERING_APP_CLASSID) {
410 counter.iftype = connection->tether_iface.type;
411 g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
413 counter.iftype = connection->type;
414 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
417 if (app_value->classid == STC_TOTAL_IPV4_CLASSID) {
418 __add_iptables_in(&counter);
419 __add_iptables_out(&counter);
420 } else if (app_value->classid == STC_TOTAL_IPV6_CLASSID) {
421 __add_ip6tables_in(&counter);
422 __add_ip6tables_out(&counter);
424 __add_iptables_in(&counter);
425 __add_iptables_out(&counter);
426 __add_ip6tables_in(&counter);
427 __add_ip6tables_out(&counter);
434 static gboolean __remove_application_monitor(gpointer key, gpointer value,
437 stc_app_value_s *app_value = (stc_app_value_s *)value;
438 default_connection_s *connection = (default_connection_s *)data;
439 stc_s *stc = stc_get_manager();
441 if (stc && connection && connection->ifname) {
442 struct nfacct_rule counter;
445 stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
446 if (stc->carg == NULL) //LCOV_EXCL_LINE
447 return FALSE; //LCOV_EXCL_LINE
449 stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
452 memset(&counter, 0, sizeof(struct nfacct_rule));
454 counter.carg = stc->carg;
455 counter.classid = app_value->classid;
456 counter.intend = NFACCT_COUNTER;
458 if (connection->tether_state == FALSE &&
459 connection->tether_iface.ifname != NULL &&
460 app_value->classid == STC_TETHERING_APP_CLASSID) {
461 counter.iftype = connection->tether_iface.type;
462 g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
464 counter.iftype = connection->type;
465 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
468 __del_iptables_in(&counter);
469 __del_iptables_out(&counter);
470 __del_ip6tables_in(&counter);
471 __del_ip6tables_out(&counter);
477 static void __print_rstn(stc_rstn_key_s *rstn_key, stc_rstn_value_s *rstn_value)
479 STC_LOGI("rstn info => rstn_id [%llu], "
480 "app_id [%s], classid [%u], ifname [%s], "
481 "iftype [%d], rstn_state [%d], rstn_type [%d], "
482 "limit [ (%lld) bytes], "
483 "warn_limit [ (%lld) bytes], "
484 "counter [ (%lld) bytes], "
485 "roaming [%d], subscriber_id [%s]",
486 rstn_value->restriction_id,
487 rstn_key->app_id, rstn_value->classid , rstn_key->ifname,
488 rstn_key->iftype, rstn_value->rstn_state, rstn_value->rstn_type,
489 rstn_value->data_limit,
490 rstn_value->data_warn_limit,
491 rstn_value->data_counter,
492 rstn_key->roaming, rstn_key->subscriber_id);
495 static void __add_iptables_rule(int64_t classid, nfacct_rule_intend intend,
496 stc_iface_type_e iftype)
498 char *default_ifname = stc_default_connection_get_ifname();
499 default_connection_s *connection = stc_get_default_connection();
500 struct nfacct_rule counter;
501 stc_s *stc = stc_get_manager();
503 g_free(default_ifname); //LCOV_EXCL_LINE
504 return; //LCOV_EXCL_LINE
508 stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
509 if (stc->carg == NULL) { //LCOV_EXCL_LINE
510 g_free(default_ifname); //LCOV_EXCL_LINE
511 return; //LCOV_EXCL_LINE
514 stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
517 counter.carg = stc->carg;
518 counter.classid = classid;
519 counter.intend = intend;
521 if (connection && connection->tether_iface.ifname != NULL &&
522 classid == STC_TETHERING_APP_CLASSID) {
523 counter.iftype = connection->tether_iface.type;
524 g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
526 counter.iftype = iftype;
527 g_strlcpy(counter.ifname, default_ifname, MAX_IFACE_LENGTH);
530 g_free(default_ifname);
533 __add_iptables_in(&counter);
534 __add_iptables_out(&counter);
537 __add_ip6tables_in(&counter);
538 __add_ip6tables_out(&counter);
541 static void __del_iptables_rule(int64_t classid, nfacct_rule_intend intend,
542 stc_iface_type_e iftype)
544 char *default_ifname = stc_default_connection_get_ifname();
545 default_connection_s *connection = stc_get_default_connection();
546 struct nfacct_rule counter;
547 stc_s *stc = stc_get_manager();
549 g_free(default_ifname); //LCOV_EXCL_LINE
550 return; //LCOV_EXCL_LINE
554 stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
555 if (stc->carg == NULL) { //LCOV_EXCL_LINE
556 g_free(default_ifname); //LCOV_EXCL_LINE
557 return; //LCOV_EXCL_LINE
560 stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
563 counter.carg = stc->carg;
564 counter.classid = classid;
565 counter.intend = intend;
567 if (connection && connection->tether_iface.ifname != NULL &&
568 classid == STC_TETHERING_APP_CLASSID) {
569 counter.iftype = connection->tether_iface.type;
570 g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
572 counter.iftype = iftype;
573 g_strlcpy(counter.ifname, default_ifname, MAX_IFACE_LENGTH);
576 g_free(default_ifname);
579 __del_iptables_in(&counter);
580 __del_iptables_out(&counter);
583 __del_ip6tables_in(&counter);
584 __del_ip6tables_out(&counter);
587 static void __process_restriction(enum traffic_restriction_type rstn_type,
588 stc_rstn_key_s *rstn_key,
589 stc_rstn_value_s *rstn_value, void *data)
591 int64_t effective_data_limit, effective_data_warn_limit;
592 default_connection_s *old_connection = (default_connection_s *)data;
593 default_connection_s *connection = NULL;
595 if (old_connection != NULL)
596 connection = old_connection;
598 connection = stc_get_default_connection();
600 /* no default ifname */
601 if (connection->ifname == NULL)
604 /* rstn not applicable for this interface */
605 if (rstn_key->ifname != NULL && g_strcmp0("", rstn_key->ifname) != 0 &&
606 (g_strcmp0(connection->ifname, rstn_key->ifname) != 0) &&
607 (g_strcmp0(connection->tether_iface.ifname, rstn_key->ifname) != 0))
610 /* classid is invalid */
611 if (rstn_value->classid <= STC_UNKNOWN_CLASSID)
614 effective_data_limit = rstn_value->data_limit;
615 effective_data_warn_limit = rstn_value->data_warn_limit;
617 if (rstn_type == RST_SET) {
618 /* TODO: Change this to runtime memory */
619 table_counters_info info;
621 memset(&info, 0, sizeof(table_counters_info));
622 table_counters_get(rstn_value->restriction_id, &info);
624 effective_data_limit -= info.data_counter;
625 effective_data_warn_limit -= info.data_counter;
627 if (effective_data_limit < 0) {
628 effective_data_limit = 0; //LCOV_EXCL_LINE
629 rstn_value->data_limit_reached = TRUE; //LCOV_EXCL_LINE
632 if (effective_data_warn_limit < 0)
633 effective_data_warn_limit = 0; //LCOV_EXCL_LINE
635 STC_LOGD("datausage [%lld] bytes", info.data_counter);
638 STC_LOGD("rstn_id [%llu], effective_data_limit [%lld] bytes, "
639 "effective_data_warn_limit [%lld] bytes",
640 rstn_value->restriction_id, effective_data_limit,
641 effective_data_warn_limit);
645 if (effective_data_limit <= 0)
646 __add_iptables_rule(rstn_value->classid, NFACCT_BLOCK,
649 rstn_value->rstn_state = STC_RSTN_STATE_ACTIVATED;
650 rstn_value->data_limit_reached = FALSE;
653 __add_iptables_rule(rstn_value->classid, NFACCT_ALLOW,
656 rstn_value->rstn_state = STC_RSTN_STATE_ACTIVATED;
657 rstn_value->data_limit_reached = TRUE;
660 if (rstn_value->classid == STC_TETHERING_APP_CLASSID)
661 __del_iptables_rule(rstn_value->classid, NFACCT_BLOCK,
664 __del_iptables_rule(rstn_value->classid, rstn_value->rstn_type,
667 rstn_value->rstn_state = STC_RSTN_STATE_DEACTIVATED;
668 rstn_value->data_limit_reached = FALSE;
676 static gboolean __remove_rstns_foreach_application(gpointer key,
680 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
681 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
682 gchar *app_id = (gchar *)data;
684 /* rstn rule is not for applications */
685 if (rstn_key->app_id == NULL)
688 /* rstn rule is not for this application */
689 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
692 /* rstn rule is already removed */
693 if (rstn_value->rstn_state == STC_RSTN_STATE_DEACTIVATED)
696 /* remove restriction from system */
697 __process_restriction(RST_UNSET, rstn_key, rstn_value, NULL);
699 __print_rstn(rstn_key, rstn_value);
705 static void __remove_rstns_for_application(gchar *app_id)
707 g_tree_foreach(g_system->rstns, __remove_rstns_foreach_application,
711 static stc_error_e __application_remove_if_empty(const stc_app_key_s *app_key)
713 stc_error_e ret = STC_ERROR_NONE;
715 stc_app_value_s *lookup;
717 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
719 lookup = __application_lookup(g_system->apps, app_key);
721 STC_LOGE("app_key not found"); //LCOV_EXCL_LINE
722 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
725 g_tree_foreach(lookup->processes, __processes_tree_check_empty,
729 /* remove nfacct rule for this classid */
730 __remove_application_monitor((gpointer) app_key, lookup,
731 stc_get_default_connection());
732 __remove_rstns_for_application(app_key->app_id);
735 if (!g_tree_remove(g_system->apps, app_key)) {
736 ret = STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
737 STC_LOGE("key not found"); //LCOV_EXCL_LINE
743 static stc_error_e __close_contr_sock(stc_system_s *system)
745 ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
747 /* close netlink socket for updating kernel counters */
748 if (system->contr_sock != -1) {
749 close(system->contr_sock);
750 system->contr_sock = -1;
753 if (system->contr_gsource_id != 0) {
754 g_source_remove(system->contr_gsource_id);
755 system->contr_gsource_id = 0;
758 return STC_ERROR_NONE;
761 static gboolean __process_contr_reply(GIOChannel *source,
762 GIOCondition condition,
766 static stc_error_e __close_and_reopen_contr_sock(stc_system_s *system)
768 GIOChannel *gio = NULL;
769 ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
771 /* close netlink socket for updating kernel counters */
772 if (system->contr_sock != -1) {
773 close(system->contr_sock);
774 system->contr_sock = -1;
777 if (system->contr_gsource_id != 0) {
778 g_source_remove(system->contr_gsource_id);
779 system->contr_gsource_id = 0;
782 /* create netlink socket for updating kernel counters */
783 system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
784 if (system->contr_sock < 0) {
785 STC_LOGE("failed to open socket");
787 return STC_ERROR_FAIL;
790 gio = g_io_channel_unix_new(system->contr_sock);
791 system->contr_gsource_id =
792 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
793 (GIOFunc) __process_contr_reply,
795 g_io_channel_unref(gio);
797 return STC_ERROR_NONE;
800 static gboolean __rstn_counter_update(stc_rstn_key_s *rstn_key,
801 stc_rstn_value_s *rstn_value,
802 classid_bytes_context_s *context)
804 switch (context->counter->iotype) {
805 case NFACCT_COUNTER_IN:
806 case NFACCT_COUNTER_OUT:
807 rstn_value->data_counter += context->bytes;
809 if (rstn_value->data_counter >= rstn_value->data_warn_limit &&
810 rstn_value->warn_limit_crossed_notified == FALSE) {
813 char iftype[MAX_INT_LENGTH];
814 char byte[MAX_INT_LENGTH];
815 stc_s *stc = (stc_s *)stc_get_manager();
816 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
819 rv = stc_manager_dbus_emit_signal(stc->connection,
820 STC_DBUS_SERVICE_RESTRICTION_PATH,
821 STC_DBUS_INTERFACE_RESTRICTION,
822 "WarnThresholdCrossed",
823 g_variant_new("(si)", rstn_key->app_id, rstn_key->iftype));
825 rstn_value->warn_limit_crossed_notified = TRUE;
827 snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
828 snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->data_warn_limit);
829 stc_plugin_appstatus_send_warn_message("warn threshold crossed",
835 if (rstn_value->data_counter >= rstn_value->data_limit &&
836 rstn_value->rstn_limit_crossed_notified == FALSE) {
839 char iftype[MAX_INT_LENGTH];
840 char byte[MAX_INT_LENGTH];
841 stc_s *stc = (stc_s *)stc_get_manager();
842 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
844 /* block immediately */
845 context->counter->intend = NFACCT_BLOCK;
846 __del_iptables_in(context->counter);
847 __del_iptables_out(context->counter);
848 __add_iptables_in(context->counter);
849 __add_iptables_out(context->counter);
851 __del_ip6tables_in(context->counter);
852 __del_ip6tables_out(context->counter);
853 __add_ip6tables_in(context->counter);
854 __add_ip6tables_out(context->counter);
855 context->counter->intend = NFACCT_COUNTER;
857 rstn_value->data_limit_reached = TRUE;
860 rv = stc_manager_dbus_emit_signal(stc->connection,
861 STC_DBUS_SERVICE_RESTRICTION_PATH,
862 STC_DBUS_INTERFACE_RESTRICTION,
863 "RestrictionThresholdCrossed",
864 g_variant_new("(si)", rstn_key->app_id, rstn_key->iftype));
866 rstn_value->rstn_limit_crossed_notified = TRUE;
868 snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
869 snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->data_limit);
870 stc_plugin_appstatus_send_restriction_message("restriction threshold crossed",
871 "restriction_noti", rstn_key->app_id,
875 g_system->rstns_tree_updated = TRUE;
876 __print_rstn(rstn_key, rstn_value);
879 STC_LOGE("unknown iotype");
885 static gboolean __interface_rstn_counter_update(stc_rstn_key_s *rstn_key,
886 stc_rstn_value_s *rstn_value,
887 classid_bytes_context_s *context)
889 if ((rstn_value->classid == STC_TOTAL_DATACALL_CLASSID &&
890 context->counter->iftype == STC_IFACE_DATACALL) ||
891 (rstn_value->classid == STC_TOTAL_WIFI_CLASSID &&
892 context->counter->iftype == STC_IFACE_WIFI) ||
893 (rstn_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
894 context->counter->iftype == STC_IFACE_BLUETOOTH) ||
895 (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
896 context->counter->iftype == STC_IFACE_WIFI) ||
897 (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
898 context->counter->iftype == STC_IFACE_BLUETOOTH) ||
899 (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
900 context->counter->iftype == STC_IFACE_USB) ||
901 (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
902 context->counter->iftype == STC_IFACE_P2P)) {
903 context->counter->classid = rstn_value->classid;
904 return __rstn_counter_update(rstn_key, rstn_value, context);
910 static gboolean __rstn_counter_update_foreach_classid(gpointer key,
915 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
916 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
917 classid_bytes_context_s *context = (classid_bytes_context_s *)data;
920 if (context->counter->intend != NFACCT_COUNTER)
921 goto try_next_callback;
923 if (rstn_value->data_limit_reached == TRUE) {
924 context->data_limit_reached = TRUE; //LCOV_EXCL_LINE
925 goto try_next_callback; //LCOV_EXCL_LINE
928 classid = context->counter->classid;
929 rv = __interface_rstn_counter_update(rstn_key, rstn_value, context);
931 context->counter->classid = classid;
932 if (rstn_value->classid != context->counter->classid)
933 goto try_next_callback;
935 rv = __rstn_counter_update(rstn_key, rstn_value, context);
942 static gboolean __update_app_statistics(gpointer key, gpointer value,
945 stc_app_key_s *app_key = (stc_app_key_s *)key;
946 stc_app_value_s *app_value = (stc_app_value_s *)value;
947 time_t *touch_time = (time_t *)data;
948 stc_db_classid_iftype_key stat_key;
949 stc_db_app_stats stat;
950 default_connection_s *default_connection = stc_get_default_connection();
952 memset(&stat_key, 0, sizeof(stc_db_classid_iftype_key));
953 memset(&stat, 0 , sizeof(stc_db_app_stats));
955 /* Do not update statistics for Tethering
956 * if tethering is in-active found */
957 if (default_connection &&
958 default_connection->tether_state == FALSE &&
959 !strcmp(app_key->app_id, STC_TOTAL_TETHERING))
962 /* Do not update statistics for Wi-Fi
963 * if tethering is active on wlan0 iface */
964 if (default_connection && default_connection->tether_state &&
965 default_connection->tether_iface.type == STC_IFACE_WIFI &&
966 !strcmp(app_key->app_id, STC_TOTAL_WIFI))
969 stat_key.classid = app_value->classid;
971 if (app_value->classid == STC_TETHERING_APP_CLASSID &&
972 default_connection->tether_state == TRUE)
973 stat_key.iftype = default_connection->tether_iface.type;
975 stat_key.iftype = default_connection->type;
977 if (STC_IFACE_DATACALL == stat_key.iftype)
978 stat_key.subscriber_id = g_strdup(default_connection->subscriber_id);
980 stat_key.subscriber_id = g_strdup("none_subid"); //LCOV_EXCL_LINE
982 if (app_value->classid == STC_TETHERING_APP_CLASSID &&
983 default_connection->tether_state == TRUE)
984 g_strlcpy(stat_key.ifname, default_connection->tether_iface.ifname,
987 g_strlcpy(stat_key.ifname, default_connection->ifname,
990 stat.app_id = g_strdup(app_key->app_id);
991 stat.snd_count = app_value->counter.out_bytes;
992 stat.rcv_count = app_value->counter.in_bytes;
993 stat.is_roaming = default_connection->roaming;
995 if (strstr(stat.app_id, "_BACKGROUND")) {
996 stat.ground = STC_APP_STATE_BACKGROUND;
998 if (strstr(stat.app_id, "TOTAL_"))
999 stat.ground = STC_APP_STATE_UNKNOWN;
1001 stat.ground = STC_APP_STATE_FOREGROUND;
1004 table_statistics_insert(&stat_key, &stat, *touch_time);
1006 app_value->counter.out_bytes = 0;
1007 app_value->counter.in_bytes = 0;
1010 FREE(stat_key.subscriber_id);
1015 static gboolean __flush_apps_stats_to_database(gpointer user_data)
1017 time_t current_time = time(0);
1019 if (g_system->apps_tree_updated == FALSE)
1020 return G_SOURCE_REMOVE;
1022 g_system->apps_tree_updated = FALSE;
1025 g_tree_foreach(g_system->apps,
1026 __update_app_statistics,
1029 STC_LOGI("Flushed app stats to database");
1030 return G_SOURCE_REMOVE;
1034 static gboolean __update_counter_statistics(gpointer key, gpointer value,
1037 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1038 table_counters_info info = {
1039 .restriction_id = rstn_value->restriction_id,
1040 .data_counter = rstn_value->data_counter
1043 table_counters_update_counters(&info);
1048 static gboolean __flush_rstns_counter_to_database(gpointer user_data)
1050 time_t current_time = time(0);
1052 if (g_system->rstns_tree_updated == FALSE)
1053 return G_SOURCE_REMOVE;
1055 g_system->rstns_tree_updated = FALSE;
1057 if (g_system->rstns)
1058 g_tree_foreach(g_system->rstns,
1059 __update_counter_statistics,
1062 STC_LOGI("Flushed rstns counters to database");
1063 return G_SOURCE_REMOVE;
1067 static void __app_counter_update(stc_app_key_s *app_key,
1068 stc_app_value_s *app_value,
1069 classid_bytes_context_s *context)
1071 switch (context->counter->iotype) {
1072 case NFACCT_COUNTER_IN:
1073 app_value->data_usage.in_bytes += context->bytes;
1074 app_value->counter.in_bytes = context->bytes;
1075 g_system->apps_tree_updated = TRUE;
1078 __apps_tree_foreach_print(app_key, app_value, NULL); //LCOV_EXCL_LINE
1080 case NFACCT_COUNTER_OUT:
1081 app_value->data_usage.out_bytes += context->bytes;
1082 app_value->counter.out_bytes = context->bytes;
1083 g_system->apps_tree_updated = TRUE;
1086 __apps_tree_foreach_print(app_key, app_value, NULL); //LCOV_EXCL_LINE
1089 STC_LOGE("unknown iotype"); //LCOV_EXCL_LINE
1093 static void __interface_counter_update(stc_app_key_s *app_key,
1094 stc_app_value_s *app_value,
1095 classid_bytes_context_s *context)
1097 if ((app_value->classid == STC_TOTAL_DATACALL_CLASSID &&
1098 context->counter->iftype == STC_IFACE_DATACALL) ||
1099 (app_value->classid == STC_TOTAL_WIFI_CLASSID &&
1100 context->counter->iftype == STC_IFACE_WIFI) ||
1101 (app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
1102 context->counter->iftype == STC_IFACE_BLUETOOTH) ||
1103 (app_value->classid == STC_TETHERING_APP_CLASSID &&
1104 context->counter->iftype == STC_IFACE_WIFI) ||
1105 (app_value->classid == STC_TETHERING_APP_CLASSID &&
1106 context->counter->iftype == STC_IFACE_BLUETOOTH) ||
1107 (app_value->classid == STC_TETHERING_APP_CLASSID &&
1108 context->counter->iftype == STC_IFACE_USB) ||
1109 (app_value->classid == STC_TETHERING_APP_CLASSID &&
1110 context->counter->iftype == STC_IFACE_P2P))
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 void __fill_nfacct_result(char *cnt_name, int64_t bytes,
1138 struct counter_arg *carg)
1140 struct nfacct_rule counter = {
1147 classid_bytes_context_s context = {
1148 .counter = &counter,
1150 .data_limit_reached = FALSE,
1154 STC_LOGD("cnt_name %s", cnt_name); //LCOV_EXCL_LINE
1156 if (!recreate_counter_by_name(cnt_name, &counter)) {
1157 STC_LOGE("Can't parse counter name %s", cnt_name); //LCOV_EXCL_LINE
1158 return; //LCOV_EXCL_LINE
1161 STC_LOGI("classid %u, iftype %u, iotype %d, intend %d, ifname %s, bytes %lld",
1162 context.counter->classid, context.counter->iftype,
1163 context.counter->iotype, context.counter->intend,
1164 context.counter->ifname, context.bytes);
1166 if (g_system->rstns)
1167 g_tree_foreach(g_system->rstns,
1168 __rstn_counter_update_foreach_classid,
1172 g_tree_foreach(g_system->apps,
1173 __apps_counter_update_foreach_classid,
1177 static int __fill_counters(struct rtattr *attr_list[__NFACCT_MAX],
1180 struct counter_arg *carg = user_data;
1181 char *cnt_name = (char *)RTA_DATA(attr_list[NFACCT_NAME]);
1182 if (carg->initiate) {
1184 * TODO: this will be used when daemon starts to update existing
1185 * counter data if present.
1187 populate_counters(cnt_name, carg);
1191 (int64_t *)RTA_DATA(attr_list[NFACCT_BYTES]);
1192 int bytes = be64toh(*bytes_p);
1194 ++carg->serialized_counters;
1195 __fill_nfacct_result(cnt_name, bytes, carg);
1202 static int __post_fill_counters(void *user_data)
1204 struct counter_arg *carg = user_data;
1212 static void __process_network_counter(struct genl *ans,
1213 struct counter_arg *carg)
1215 struct netlink_serialization_params ser_params = {
1218 .eval_attr = __fill_counters,
1219 .post_eval_attr = __post_fill_counters,
1222 netlink_serialization_command *netlink =
1223 netlink_create_command(&ser_params);
1225 STC_LOGE("Can not create command"); //LCOV_EXCL_LINE
1226 return; //LCOV_EXCL_LINE
1229 netlink->deserialize_answer(&(netlink->params));
1232 static gboolean __process_contr_reply(GIOChannel *source,
1233 GIOCondition condition,
1236 int sock = g_io_channel_unix_get_fd(source);
1239 stc_s *stc = stc_get_manager();
1242 void __gcov_flush(void);
1246 if ((condition & G_IO_ERR) || (condition & G_IO_HUP) ||
1247 (condition & G_IO_NVAL)) {
1248 /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */
1250 STC_LOGE("Counter socket received G_IO event, closing socket." //LCOV_EXCL_LINE
1251 "G_IO_ERR [%u], G_IO_HUP [%u], G_IO_NVAL [%u]",
1252 (condition & G_IO_ERR), (condition & G_IO_HUP),
1253 (condition & G_IO_NVAL));
1254 __close_and_reopen_contr_sock(g_system); //LCOV_EXCL_LINE
1255 return FALSE; //LCOV_EXCL_LINE
1258 ans = MALLOC0(struct genl, 1);
1260 STC_LOGE("Failed allocate memory to genl reply message"); //LCOV_EXCL_LINE
1261 return TRUE; //LCOV_EXCL_LINE
1265 STC_LOGE("Can't get stc data"); //LCOV_EXCL_LINE
1266 goto out; //LCOV_EXCL_LINE
1269 ret = read_netlink(sock, ans, sizeof(struct genl));
1270 /* STC_LOGD("Counter data received ret [%d]", ret); */
1274 stc->carg->ans_len = ret;
1275 __process_network_counter(ans, stc->carg);
1277 g_idle_add(__flush_apps_stats_to_database, NULL);
1278 g_idle_add(__flush_rstns_counter_to_database, NULL);
1285 static gboolean __update_contr_cb(void *user_data)
1287 /* Here we just sent command, answer we receive in another callback */
1288 stc_s *stc = stc_get_manager();
1289 ret_value_msg_if(stc == NULL, STC_ERROR_FAIL, "Can't get stc data");
1291 stc->carg = MALLOC0(counter_arg_s, 1);
1292 if (stc->carg == NULL)
1293 return TRUE; /* we need to continue the timer */
1295 stc->carg->sock = stc_monitor_get_counter_socket();
1299 void __gcov_flush(void);
1303 /* STC_LOGD("Get all counters"); */
1304 nfacct_send_get_all(stc->carg);
1306 /* we need to continue the timer */
1311 static gboolean __rstn_tree_foreach_print(gpointer key, gpointer value,
1314 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1315 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1317 __print_rstn(rstn_key, rstn_value);
1321 static void __rstn_tree_printall(void)
1323 g_tree_foreach(g_system->rstns, __rstn_tree_foreach_print, NULL);
1327 static stc_rstn_value_s * __rstn_lookup(GTree *rstns_tree,
1328 const stc_rstn_key_s *key)
1330 stc_rstn_value_s *lookup;
1332 ret_value_msg_if(rstns_tree == NULL, NULL, "rstns_tree is null!");
1334 lookup = g_tree_lookup(rstns_tree, key);
1339 static gboolean __remove_restriction(gpointer key, gpointer value,
1342 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1343 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1345 __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
1346 __print_rstn(rstn_key, rstn_value);
1350 static gboolean __add_restriction_debug(gpointer key, gpointer value,
1353 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1354 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1356 /* rstn rule is activated */
1357 if (rstn_value->rstn_state == STC_RSTN_STATE_ACTIVATED)
1360 if (rstn_value->rstn_type == STC_RSTN_TYPE_ACCEPT)
1361 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1363 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1365 __print_rstn(rstn_key, rstn_value);
1371 static gboolean __add_restriction(gpointer key, gpointer value, gpointer data)
1373 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1374 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1376 /* rstn rule is activated */
1377 if (rstn_value->rstn_state == STC_RSTN_STATE_ACTIVATED)
1380 if (rstn_value->rstn_type == STC_RSTN_TYPE_ACCEPT)
1381 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1383 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1389 static stc_error_e __rstn_tree_remove(stc_rstn_key_s *key)
1391 stc_rstn_value_s *lookup_value;
1393 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1395 lookup_value = __rstn_lookup(g_system->rstns, key);
1396 if (!lookup_value) {
1397 STC_LOGE("key not found"); //LCOV_EXCL_LINE
1398 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
1401 __remove_restriction(key, lookup_value, NULL);
1403 /* remove counter also */
1404 table_counters_delete(lookup_value->restriction_id);
1406 if (!g_tree_remove(g_system->rstns, key)) {
1407 STC_LOGD("key not found"); //LCOV_EXCL_LINE
1408 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
1411 return STC_ERROR_NONE;
1414 static stc_error_e __rstn_tree_add(stc_rstn_key_s *key,
1415 stc_rstn_value_s *value, gboolean debug)
1417 stc_rstn_key_s *rstn_key;
1418 stc_rstn_value_s *rstn_value;
1420 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1422 rstn_value = __rstn_lookup(g_system->rstns, key);
1424 __rstn_tree_remove(key);
1426 rstn_key = MALLOC0(stc_rstn_key_s, 1);
1428 STC_LOGE("rstn_key allocation failed"); //LCOV_EXCL_LINE
1429 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1432 rstn_value = MALLOC0(stc_rstn_value_s, 1);
1434 STC_LOGE("rstn_value allocation failed"); //LCOV_EXCL_LINE
1435 FREE(rstn_key); //LCOV_EXCL_LINE
1436 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1439 rstn_key->app_id = g_strdup(key->app_id);
1440 rstn_key->ifname = g_strdup(key->ifname);
1441 rstn_key->subscriber_id = g_strdup(key->subscriber_id);
1442 rstn_key->iftype = key->iftype;
1443 rstn_key->roaming = key->roaming;
1445 g_tree_insert(g_system->rstns, rstn_key, rstn_value);
1447 rstn_value->restriction_id = value->restriction_id;
1448 rstn_value->rstn_state = value->rstn_state;
1449 rstn_value->rstn_type = value->rstn_type;
1450 rstn_value->classid = value->classid;
1451 rstn_value->data_limit = value->data_limit;
1452 rstn_value->data_warn_limit = value->data_warn_limit;
1453 rstn_value->data_counter = 0;
1454 rstn_value->warn_limit_crossed_notified = FALSE;
1455 rstn_value->rstn_limit_crossed_notified = FALSE;
1458 __add_restriction_debug(key, rstn_value, NULL);
1460 __add_restriction(key, rstn_value, NULL);
1462 return STC_ERROR_NONE;
1466 static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info,
1469 stc_cb_ret_e ret = STC_CONTINUE;
1472 stc_rstn_value_s value;
1474 memset(&key, 0, sizeof(stc_rstn_key_s));
1475 memset(&value, 0, sizeof(stc_rstn_value_s));
1477 key.app_id = g_strdup(info->app_id);
1478 key.ifname = g_strdup(info->ifname);
1479 key.subscriber_id = g_strdup(info->subscriber_id);
1480 key.iftype = info->iftype;
1481 key.roaming = info->roaming;
1483 value.rstn_type = info->rstn_type;
1484 value.rstn_state = STC_RSTN_STATE_UNKNOWN;
1485 value.restriction_id = info->restriction_id;
1488 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1490 value.classid = STC_UNKNOWN_CLASSID;
1492 value.data_limit = info->data_limit;
1493 value.data_warn_limit = info->data_warn_limit;
1495 if (__rstn_tree_add(&key, &value, FALSE) != STC_ERROR_NONE)
1500 FREE(key.subscriber_id);
1504 static void __fill_restritions_list(void)
1506 table_restrictions_foreach(__insert_restriction_cb, NULL);
1509 __rstn_tree_printall();
1512 static gboolean __add_rstn_foreach_application(gpointer key,
1516 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1517 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1518 gchar *app_id = (gchar *)data;
1520 /* rstn rule is not for applications */
1521 if (rstn_key->app_id == NULL)
1524 /* rstn rule is not for this application */
1525 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
1528 /* rstn rule is already applied */
1529 if (rstn_value->rstn_state == STC_RSTN_STATE_ACTIVATED)
1532 /* add restriction to system */
1533 if (rstn_value->rstn_type == STC_RSTN_TYPE_ACCEPT)
1534 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, NULL);
1536 __process_restriction(RST_SET, rstn_key, rstn_value, NULL);
1538 __print_rstn(rstn_key, rstn_value);
1544 static void __add_rstns_for_application(gchar *app_id)
1546 g_tree_foreach(g_system->rstns, __add_rstn_foreach_application,
1550 static void __add_application_by_interface(const char *app_id)
1552 stc_app_key_s app_key;
1553 stc_app_value_s app_value;
1556 return; //LCOV_EXCL_LINE
1558 memset(&app_key, 0, sizeof(stc_app_key_s));
1559 memset(&app_value, 0, sizeof(stc_app_value_s));
1561 app_key.pkg_id = g_strdup(app_id);
1562 app_key.app_id = g_strdup(app_id);
1564 app_value.type = STC_APP_TYPE_NONE;
1565 app_value.processes = NULL;
1566 app_value.counter.in_bytes = 0;
1567 app_value.counter.out_bytes = 0;
1569 stc_monitor_application_add(app_key, app_value);
1571 FREE(app_key.pkg_id);
1572 FREE(app_key.app_id);
1575 static int __vconf_get_int(const char *key, int *value)
1579 ret = vconf_get_int(key, value);
1580 if (ret != VCONF_OK) {
1581 STC_LOGE("Failed to get vconfkey [%s] value", key); //LCOV_EXCL_LINE
1582 return -1; //LCOV_EXCL_LINE
1589 static int __vconf_set_int(const char *key, int value)
1593 ret = vconf_set_int(key, value);
1594 if (ret != VCONF_OK) {
1595 STC_LOGE("Failed to set vconfkey [%s] value", key); //LCOV_EXCL_LINE
1596 return -1; //LCOV_EXCL_LINE
1602 static guint __get_background_state(void)
1604 return g_system->background_state;;
1607 static void __set_background_state(guint state)
1609 g_system->background_state = state;
1612 static gboolean __processes_tree_foreach_background(gpointer key,
1616 stc_process_key_s *proc_key = (stc_process_key_s *)key;
1617 stc_app_key_s *app_key = (stc_app_key_s *)data;
1619 if (g_system->background_state)
1620 place_pids_to_net_cgroup(proc_key->pid, STC_BACKGROUND_APP_ID);
1622 place_pids_to_net_cgroup(proc_key->pid, app_key->app_id);
1627 static gboolean __apps_tree_foreach_background(gpointer key, gpointer value,
1630 stc_app_key_s *app_key = (stc_app_key_s *)key;
1631 stc_app_value_s *app_value = (stc_app_value_s *)value;
1633 if (strstr(app_key->app_id, STC_BACKGROUND_APP_SUFFIX))
1634 g_tree_foreach(app_value->processes,
1635 __processes_tree_foreach_background, app_key);
1640 static stc_error_e __process_update_background(void)
1642 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1644 g_tree_foreach(g_system->apps, __apps_tree_foreach_background, NULL);
1646 return STC_ERROR_NONE;
1650 static void __fill_exceptions_list(void)
1652 stc_plugin_fill_exception_list();
1655 stc_error_e stc_monitor_init(void)
1657 stc_system_s *system = MALLOC0(stc_system_s, 1);
1658 GIOChannel *gio = NULL;
1660 ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY, "stc_system_s malloc fail!");
1662 /* initializing current classid */
1663 init_current_classid();
1665 /* initializing cgroups */
1668 /* creating monitored application tree */
1669 system->apps = g_tree_new_full(__apps_tree_key_compare, NULL,
1670 __apps_tree_key_free,
1671 __apps_tree_value_free);
1673 system->rstns = g_tree_new_full(__rstns_tree_key_compare, NULL,
1674 __rstns_tree_key_free,
1675 __rstns_tree_value_free);
1677 /* create netlink socket for updating kernel counters */
1678 system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
1679 if (system->contr_sock < 0) {
1680 STC_LOGE("failed to open socket"); //LCOV_EXCL_LINE
1681 FREE(system); //LCOV_EXCL_LINE
1682 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
1685 gio = g_io_channel_unix_new(system->contr_sock);
1686 system->contr_gsource_id =
1687 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
1688 (GIOFunc) __process_contr_reply,
1690 g_io_channel_unref(gio);
1694 __add_application_by_interface(STC_TOTAL_DATACALL);
1695 __add_application_by_interface(STC_TOTAL_WIFI);
1696 __add_application_by_interface(STC_TOTAL_BLUETOOTH);
1697 __add_application_by_interface(STC_TOTAL_IPV4);
1698 __add_application_by_interface(STC_TOTAL_IPV6);
1699 __add_application_by_interface(STC_TOTAL_TETHERING);
1701 /* creating restriction rules tree */
1702 __update_contr_cb(NULL);
1704 /* registering periodic kernel counters update callback */
1705 g_system->contr_timer_id = g_timeout_add_seconds(CONTR_TIMER_INTERVAL,
1708 if (g_system->contr_timer_id == 0) {
1709 STC_LOGE("Failed to register kernel counters update timer"); //LCOV_EXCL_LINE
1710 __close_contr_sock(g_system); //LCOV_EXCL_LINE
1711 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
1714 __vconf_get_int(VCONFKEY_STC_BACKGROUND_STATE,
1715 (int *)&g_system->background_state);
1717 __fill_exceptions_list();
1718 __fill_restritions_list();
1720 return STC_ERROR_NONE;
1723 stc_error_e stc_monitor_deinit(void)
1725 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1727 /* close netlink socket for updating kernel counters */
1728 __close_contr_sock(g_system);
1730 /* remove kernel counters update timer */
1731 if (g_system->contr_timer_id > 0) {
1732 g_source_remove(g_system->contr_timer_id);
1733 g_system->contr_timer_id = 0;
1736 /* destroy monitored application tree */
1737 g_tree_destroy(g_system->apps);
1738 g_system->apps = NULL;
1740 /* destroy restriction rules tree */
1741 g_tree_destroy(g_system->rstns);
1742 g_system->rstns = NULL;
1746 return STC_ERROR_NONE;
1749 API stc_error_e stc_monitor_application_add(const stc_app_key_s app_key,
1750 const stc_app_value_s app_value)
1752 stc_error_e ret = STC_ERROR_NONE;
1754 stc_app_value_s *value;
1755 stc_app_value_s *lookup;
1757 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1759 lookup = __application_lookup(g_system->apps, &app_key);
1761 STC_LOGD("app_key already present"); //LCOV_EXCL_LINE
1762 return STC_ERROR_NONE; //LCOV_EXCL_LINE
1765 key = MALLOC0(stc_app_key_s, 1);
1767 STC_LOGE("key allocation failed"); //LCOV_EXCL_LINE
1768 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1771 value = MALLOC0(stc_app_value_s, 1);
1773 STC_LOGE("value allocation failed"); //LCOV_EXCL_LINE
1774 FREE(key); //LCOV_EXCL_LINE
1775 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1778 key->app_id = g_strdup(app_key.app_id);
1779 key->pkg_id = g_strdup(app_key.pkg_id);
1781 value->type = app_value.type;
1782 value->data_usage.in_bytes = app_value.data_usage.in_bytes;
1783 value->data_usage.out_bytes = app_value.data_usage.out_bytes;
1785 value->processes = g_tree_new_full(__processes_tree_key_compare, NULL,
1786 __processes_tree_key_free,
1787 __processes_tree_value_free);
1789 /* create cgroup and update classid */
1790 value->classid = get_classid_by_app_id(app_key.app_id, TRUE);
1792 g_tree_insert(g_system->apps, key, value);
1794 /* add nfacct rule for this classid */
1795 __add_application_monitor(key, value, stc_get_default_connection());
1796 __add_rstns_for_application(app_key.app_id);
1801 API stc_error_e stc_monitor_process_add(const stc_app_key_s app_key,
1802 const stc_process_key_s proc_key,
1803 const stc_process_value_s proc_value)
1805 stc_error_e ret = STC_ERROR_NONE;
1806 stc_app_value_s *app_lookup;
1807 stc_process_key_s *key;
1808 stc_process_value_s *value;
1809 stc_process_value_s *proc_lookup;
1811 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1813 app_lookup = __application_lookup(g_system->apps, &app_key);
1815 STC_LOGD("app_key not found"); //LCOV_EXCL_LINE
1816 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
1819 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1821 STC_LOGD("proc_key already present"); //LCOV_EXCL_LINE
1822 return STC_ERROR_NONE; //LCOV_EXCL_LINE
1825 key = MALLOC0(stc_process_key_s, 1);
1827 STC_LOGE("key allocation failed"); //LCOV_EXCL_LINE
1828 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1831 value = MALLOC0(stc_process_value_s, 1);
1833 STC_LOGE("value allocation failed"); //LCOV_EXCL_LINE
1834 FREE(key); //LCOV_EXCL_LINE
1835 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1838 key->pid = proc_key.pid;
1840 value->ground = proc_value.ground;
1842 g_tree_insert(app_lookup->processes, key, value);
1844 /* add pid to application cgroup */
1845 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1848 __apps_tree_printall(); //LCOV_EXCL_LINE
1853 API stc_error_e stc_monitor_process_remove(pid_t pid)
1855 stc_error_e ret = STC_ERROR_NONE;
1856 stc_process_key_s proc_key = {
1860 remove_pid_context_s context = {
1862 .proc_key = &proc_key,
1863 .entry_removed = FALSE,
1866 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1868 g_tree_foreach(g_system->apps, __apps_tree_foreach_remove_pid,
1871 if (context.entry_removed)
1872 __application_remove_if_empty(context.app_key);
1875 __apps_tree_printall(); //LCOV_EXCL_LINE
1881 API stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key,
1882 const stc_process_key_s proc_key,
1883 stc_app_state_e ground)
1885 stc_error_e ret = STC_ERROR_NONE;
1886 stc_app_value_s *app_lookup;
1887 stc_process_value_s *proc_lookup;
1889 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1891 app_lookup = __application_lookup(g_system->apps, &app_key);
1893 STC_LOGD("app_key not found");
1894 return STC_ERROR_FAIL;
1897 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1899 STC_LOGD("proc_key not found");
1900 return STC_ERROR_FAIL;
1903 if (proc_lookup->ground != ground)
1904 proc_lookup->ground = ground;
1906 if (ground == STC_APP_STATE_BACKGROUND && __get_background_state())
1907 place_pids_to_net_cgroup(proc_key.pid, STC_BACKGROUND_APP_ID);
1909 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1915 void stc_monitor_update_rstn_by_default_connection(void *data)
1917 static default_connection_s old_connection;
1918 default_connection_s *new_connection = (default_connection_s *)data;
1920 if (old_connection.path != NULL) {
1923 g_tree_foreach(g_system->apps,
1924 __remove_application_monitor,
1925 (gpointer)&old_connection);
1927 if (g_system->rstns)
1928 g_tree_foreach(g_system->rstns,
1929 __remove_restriction,
1930 (gpointer)&old_connection);
1932 iptables_flush_chains();
1936 FREE(old_connection.path);
1937 FREE(old_connection.ifname);
1938 FREE(old_connection.tether_iface.ifname);
1939 old_connection.type = 0;
1940 old_connection.roaming = 0;
1941 old_connection.tether_state = FALSE;
1942 old_connection.tether_iface.type = 0;
1944 if (new_connection != NULL && new_connection->path != NULL) {
1946 g_tree_foreach(g_system->apps,
1947 __add_application_monitor,
1948 (gpointer)new_connection);
1950 if (g_system->rstns)
1951 g_tree_foreach(g_system->rstns, __add_restriction,
1954 old_connection.path = g_strdup(new_connection->path);
1955 old_connection.ifname = g_strdup(new_connection->ifname);
1956 old_connection.tether_iface.ifname = g_strdup(new_connection->tether_iface.ifname);
1957 old_connection.type = new_connection->type;
1958 old_connection.roaming = new_connection->roaming;
1959 old_connection.tether_state = new_connection->tether_state;
1960 old_connection.tether_iface.type = new_connection->tether_iface.type;
1964 stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info)
1969 stc_rstn_value_s value;
1971 memset(&key, 0, sizeof(stc_rstn_key_s));
1972 memset(&value, 0, sizeof(stc_rstn_value_s));
1974 key.app_id = g_strdup(info->app_id);
1975 key.ifname = g_strdup(info->ifname);
1976 key.subscriber_id = g_strdup(info->subscriber_id);
1977 key.iftype = info->iftype;
1978 key.roaming = info->roaming;
1980 value.rstn_type = info->rstn_type;
1981 value.rstn_state = STC_RSTN_STATE_UNKNOWN;
1982 value.restriction_id = info->restriction_id;
1985 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1987 value.classid = STC_UNKNOWN_CLASSID;
1989 if (value.classid == STC_BACKGROUND_APP_CLASSID) {
1990 __set_background_state(TRUE); //LCOV_EXCL_LINE
1991 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state); //LCOV_EXCL_LINE
1992 __process_update_background(); //LCOV_EXCL_LINE
1995 value.data_limit = info->data_limit;
1996 value.data_warn_limit = info->data_warn_limit;
1998 ret = __rstn_tree_add(&key, &value, TRUE);
2002 FREE(key.subscriber_id);
2006 stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info)
2010 stc_rstn_key_s key = {
2011 .app_id = g_strdup(info->app_id),
2012 .ifname = g_strdup(info->ifname),
2013 .subscriber_id = g_strdup(info->subscriber_id),
2014 .iftype = info->iftype,
2015 .roaming = info->roaming,
2018 if (!strcmp(key.app_id, STC_BACKGROUND_APP_ID)) {
2019 __set_background_state(FALSE); //LCOV_EXCL_LINE
2020 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state); //LCOV_EXCL_LINE
2021 __process_update_background(); //LCOV_EXCL_LINE
2024 ret = __rstn_tree_remove(&key);
2028 FREE(key.subscriber_id);
2032 API stc_error_e stc_monitor_check_excn_by_cmdline(char *cmdline)
2034 return stc_plugin_check_exception_by_cmdline(cmdline);
2037 int stc_monitor_get_counter_socket(void)
2039 return g_system->contr_sock;