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"
34 #include "stc-manager-plugin-tether.h"
36 #define GRANULARITY 10
37 #define MAX_INT_LENGTH 128
39 #ifndef VCONFKEY_STC_BACKGROUND_STATE
40 #define VCONFKEY_STC_BACKGROUND_STATE "db/stc/background_state"
43 #ifndef VCONFKEY_SETAPPL_DATA_RESTRICTION_INT
44 #define VCONFKEY_SETAPPL_DATA_RESTRICTION_INT "db/setting/data_restriction"
49 time_t month_start_ts;
53 } reset_time_limits_context_s;
56 stc_app_key_s *app_key;
57 stc_process_key_s *proc_key;
58 gboolean entry_removed;
59 } remove_pid_context_s;
62 struct nfacct_rule *counter;
64 gboolean data_limit_exceeded;
65 } classid_bytes_context_s;
67 static stc_system_s *g_system = NULL;
70 static int __vconf_get_int(const char *key, int *value)
74 ret = vconf_get_int(key, value);
75 if (ret != VCONF_OK) {
76 STC_LOGE("Failed to get vconfkey [%s] value", key); //LCOV_EXCL_LINE
77 return -1; //LCOV_EXCL_LINE
83 static int __vconf_set_int(const char *key, int value)
87 ret = vconf_set_int(key, value);
88 if (ret != VCONF_OK) {
89 STC_LOGE("Failed to set vconfkey [%s] value", key); //LCOV_EXCL_LINE
90 return -1; //LCOV_EXCL_LINE
97 static nfacct_rule_jump __get_jump_by_intend(struct nfacct_rule *counter)
99 if (counter->intend == NFACCT_WARN)
100 return NFACCT_JUMP_ACCEPT;
101 else if (counter->intend == NFACCT_BLOCK)
102 return NFACCT_JUMP_REJECT;
103 else if (counter->intend == NFACCT_ALLOW)
104 return NFACCT_JUMP_ACCEPT;
105 else if (counter->intend == NFACCT_TETH_BLOCK)
106 return NFACCT_JUMP_REJECT;
107 else if (counter->intend == NFACCT_TETH_ALLOW)
108 return NFACCT_JUMP_ACCEPT;
110 return NFACCT_JUMP_UNKNOWN;
113 static stc_error_e __add_iptables_tether_in(struct nfacct_rule *counter,
118 if (counter == NULL || ipaddr == NULL)
119 return STC_ERROR_INVALID_PARAMETER;
121 counter->action = NFACCT_ACTION_INSERT;
122 counter->iotype = NFACCT_COUNTER_IN;
123 counter->jump = __get_jump_by_intend(counter);
124 counter->iptype = NFACCT_TYPE_IPV4;
125 counter->send_limit = 0;
126 counter->rcv_limit = 0;
127 counter->src_iprange_type = NFACCT_IPRANGE_TYPE_SINGLE;
128 counter->src_ip1 = g_strdup(ipaddr);
130 ret = produce_net_rule(counter);
132 FREE(counter->src_ip1);
133 counter->src_iprange_type = NFACCT_IPRANGE_TYPE_NONE;
137 static stc_error_e __add_iptables_tether_out(struct nfacct_rule *counter,
142 if (counter == NULL || ipaddr == NULL)
143 return STC_ERROR_INVALID_PARAMETER;
145 counter->action = NFACCT_ACTION_INSERT;
146 counter->iotype = NFACCT_COUNTER_OUT;
147 counter->jump = __get_jump_by_intend(counter);
148 counter->iptype = NFACCT_TYPE_IPV4;
149 counter->send_limit = 0;
150 counter->rcv_limit = 0;
151 counter->dst_iprange_type = NFACCT_IPRANGE_TYPE_SINGLE;
152 counter->dst_ip1 = g_strdup(ipaddr);
154 ret = produce_net_rule(counter);
156 FREE(counter->dst_ip1);
157 counter->dst_iprange_type = NFACCT_IPRANGE_TYPE_NONE;
161 static stc_error_e __del_iptables_tether_in(struct nfacct_rule *counter,
166 if (counter == NULL || ipaddr == NULL)
167 return STC_ERROR_INVALID_PARAMETER;
169 counter->action = NFACCT_ACTION_DELETE;
170 counter->iotype = NFACCT_COUNTER_IN;
171 counter->jump = __get_jump_by_intend(counter);
172 counter->iptype = NFACCT_TYPE_IPV4;
173 counter->send_limit = 0;
174 counter->rcv_limit = 0;
175 counter->src_iprange_type = NFACCT_IPRANGE_TYPE_SINGLE;
176 counter->src_ip1 = g_strdup(ipaddr);
178 ret = produce_net_rule(counter);
180 FREE(counter->src_ip1);
181 counter->src_iprange_type = NFACCT_IPRANGE_TYPE_NONE;
185 static stc_error_e __del_iptables_tether_out(struct nfacct_rule *counter,
190 if (counter == NULL || ipaddr == NULL)
191 return STC_ERROR_INVALID_PARAMETER;
193 counter->action = NFACCT_ACTION_DELETE;
194 counter->iotype = NFACCT_COUNTER_OUT;
195 counter->jump = __get_jump_by_intend(counter);
196 counter->iptype = NFACCT_TYPE_IPV4;
197 counter->send_limit = 0;
198 counter->rcv_limit = 0;
199 counter->dst_iprange_type = NFACCT_IPRANGE_TYPE_SINGLE;
200 counter->dst_ip1 = g_strdup(ipaddr);
202 ret = produce_net_rule(counter);
204 FREE(counter->dst_ip1);
205 counter->dst_iprange_type = NFACCT_IPRANGE_TYPE_NONE;
209 static stc_error_e __add_iptables_in(struct nfacct_rule *counter)
212 return STC_ERROR_INVALID_PARAMETER;
214 counter->action = NFACCT_ACTION_INSERT;
215 counter->iotype = NFACCT_COUNTER_IN;
216 counter->jump = __get_jump_by_intend(counter);
217 counter->iptype = NFACCT_TYPE_IPV4;
218 counter->send_limit = 0;
219 counter->rcv_limit = 0;
221 return produce_net_rule(counter);
224 static stc_error_e __add_iptables_out(struct nfacct_rule *counter)
227 return STC_ERROR_INVALID_PARAMETER;
229 counter->action = NFACCT_ACTION_INSERT;
230 counter->iotype = NFACCT_COUNTER_OUT;
231 counter->jump = __get_jump_by_intend(counter);
232 counter->iptype = NFACCT_TYPE_IPV4;
233 counter->send_limit = 0;
234 counter->rcv_limit = 0;
236 return produce_net_rule(counter);
239 static stc_error_e __del_iptables_in(struct nfacct_rule *counter)
242 return STC_ERROR_INVALID_PARAMETER;
244 counter->action = NFACCT_ACTION_DELETE;
245 counter->iotype = NFACCT_COUNTER_IN;
246 counter->jump = __get_jump_by_intend(counter);
247 counter->iptype = NFACCT_TYPE_IPV4;
248 counter->send_limit = 0;
249 counter->rcv_limit = 0;
251 return produce_net_rule(counter);
254 static stc_error_e __del_iptables_out(struct nfacct_rule *counter)
257 return STC_ERROR_INVALID_PARAMETER;
259 counter->action = NFACCT_ACTION_DELETE;
260 counter->iotype = NFACCT_COUNTER_OUT;
261 counter->jump = __get_jump_by_intend(counter);
262 counter->iptype = NFACCT_TYPE_IPV4;
263 counter->send_limit = 0;
264 counter->rcv_limit = 0;
266 return produce_net_rule(counter);
269 static stc_error_e __add_ip6tables_in(struct nfacct_rule *counter)
272 return STC_ERROR_INVALID_PARAMETER;
274 counter->action = NFACCT_ACTION_INSERT;
275 counter->iotype = NFACCT_COUNTER_IN;
276 counter->jump = __get_jump_by_intend(counter);
277 counter->iptype = NFACCT_TYPE_IPV6;
278 counter->send_limit = 0;
279 counter->rcv_limit = 0;
281 return produce_net_rule(counter);
284 static stc_error_e __add_ip6tables_out(struct nfacct_rule *counter)
287 return STC_ERROR_INVALID_PARAMETER;
289 counter->action = NFACCT_ACTION_INSERT;
290 counter->iotype = NFACCT_COUNTER_OUT;
291 counter->jump = __get_jump_by_intend(counter);
292 counter->iptype = NFACCT_TYPE_IPV6;
293 counter->send_limit = 0;
294 counter->rcv_limit = 0;
296 return produce_net_rule(counter);
299 static stc_error_e __del_ip6tables_in(struct nfacct_rule *counter)
302 return STC_ERROR_INVALID_PARAMETER;
304 counter->action = NFACCT_ACTION_DELETE;
305 counter->iotype = NFACCT_COUNTER_IN;
306 counter->jump = __get_jump_by_intend(counter);
307 counter->iptype = NFACCT_TYPE_IPV6;
308 counter->send_limit = 0;
309 counter->rcv_limit = 0;
311 return produce_net_rule(counter);
314 static stc_error_e __del_ip6tables_out(struct nfacct_rule *counter)
317 return STC_ERROR_INVALID_PARAMETER;
319 counter->action = NFACCT_ACTION_DELETE;
320 counter->iotype = NFACCT_COUNTER_OUT;
321 counter->jump = __get_jump_by_intend(counter);
322 counter->iptype = NFACCT_TYPE_IPV6;
323 counter->send_limit = 0;
324 counter->rcv_limit = 0;
326 return produce_net_rule(counter);
329 static int __processes_tree_key_compare(gconstpointer a, gconstpointer b,
330 gpointer UNUSED user_data)
332 stc_process_key_s *key_a = (stc_process_key_s *)a;
333 stc_process_key_s *key_b = (stc_process_key_s *)b;
335 return key_a->pid - key_b->pid;
338 static void __processes_tree_value_free(gpointer data)
340 stc_process_value_s *value = (stc_process_value_s *)data;
345 static void __processes_tree_key_free(gpointer data)
347 stc_process_key_s *key = (stc_process_key_s *)data;
352 static int __apps_tree_key_compare(gconstpointer a, gconstpointer b,
353 gpointer UNUSED user_data)
355 stc_app_key_s *key_a = (stc_app_key_s *)a;
356 stc_app_key_s *key_b = (stc_app_key_s *)b;
359 ret = g_strcmp0(key_a->pkg_id, key_b->pkg_id);
363 return g_strcmp0(key_a->app_id, key_b->app_id);
366 static void __apps_tree_value_free(gpointer data)
368 stc_app_value_s *value = (stc_app_value_s *)data;
370 g_tree_destroy(value->processes);
371 value->processes = NULL;
376 static void __apps_tree_key_free(gpointer data)
378 stc_app_key_s *key = (stc_app_key_s *)data;
385 static int __rstns_tree_key_compare(gconstpointer a, gconstpointer b,
386 gpointer UNUSED user_data)
388 stc_rstn_key_s *key_a = (stc_rstn_key_s *)a;
389 stc_rstn_key_s *key_b = (stc_rstn_key_s *)b;
392 ret = g_strcmp0(key_a->app_id, key_b->app_id);
396 ret = g_strcmp0(key_a->ifname, key_b->ifname);
400 ret = g_strcmp0(key_a->subscriber_id, key_b->subscriber_id);
404 ret = key_a->iftype - key_b->iftype;
408 ret = key_a->roaming - key_b->roaming;
415 static void __rstns_tree_value_free(gpointer data)
417 stc_rstn_value_s *value = (stc_rstn_value_s *)data;
422 static void __rstns_tree_key_free(gpointer data)
424 stc_rstn_key_s *key = (stc_rstn_key_s *)data;
428 FREE(key->subscriber_id);
434 static gboolean __processes_tree_foreach_print(gpointer key, gpointer value,
437 stc_process_key_s *proc_key = (stc_process_key_s *)key;
438 stc_process_value_s *proc_value = (stc_process_value_s *)value;
440 STC_LOGD("Process entry => PID [\033[1;33m%d\033[0;m], Ground state [%d]",
441 proc_key->pid, proc_value->ground);
445 static void __processes_tree_printall(GTree *processes)
447 g_tree_foreach(processes, __processes_tree_foreach_print, NULL);
450 static gboolean __apps_tree_foreach_print(gpointer key, gpointer value,
453 stc_app_key_s *app_key = (stc_app_key_s *)key;
454 stc_app_value_s *app_value = (stc_app_value_s *)value;
456 STC_LOGD("Application info => Pkg ID [\033[0;34m%s\033[0;m], "
457 "App ID [\033[0;32m%s\033[0;m], Type [%d], classid [%d],"
458 " counter [ in (%lld), out (%lld)]",
459 app_key->pkg_id, app_key->app_id,
460 app_value->type, app_value->classid,
461 app_value->data_usage.in_bytes, app_value->data_usage.out_bytes);
463 __processes_tree_printall(app_value->processes);
467 static void __apps_tree_printall(void)
469 g_tree_foreach(g_system->apps, __apps_tree_foreach_print, NULL);
474 static gboolean __apps_tree_foreach_remove_pid(gpointer key, gpointer value,
477 remove_pid_context_s *context = (remove_pid_context_s *)data;
478 stc_app_value_s *app_value = (stc_app_value_s *)value;
480 if (!g_tree_remove(app_value->processes, context->proc_key))
483 context->entry_removed = TRUE;
484 context->app_key = (stc_app_key_s *)key;
489 static stc_app_value_s * __application_lookup(GTree *apps,
490 const stc_app_key_s *key)
492 stc_app_value_s *lookup;
494 ret_value_msg_if(apps == NULL, NULL, "apps is null!");
496 lookup = g_tree_lookup(apps, key);
501 static stc_process_value_s * __process_lookup(GTree *processes,
502 const stc_process_key_s *key)
504 stc_process_value_s *lookup;
506 ret_value_msg_if(processes == NULL, NULL, "processes is null!");
508 lookup = g_tree_lookup(processes, key);
514 static gboolean __processes_tree_check_empty(gpointer key, gpointer value,
517 guint *pid_count = (guint *)data;
523 static gboolean __add_application_monitor_for_tethering(gpointer key,
524 gpointer value, gpointer data)
526 stc_app_value_s *app_value = (stc_app_value_s *)value;
527 stc_app_key_s *app_key = (stc_app_key_s *)key;
528 default_connection_s *connection = (default_connection_s *)data;
529 stc_s *stc = stc_get_manager();
530 struct nfacct_rule counter;
534 STC_LOGI("add appid(%s) classid(%d)", app_key->app_id,
537 if (stc == NULL || connection == NULL)
541 stc->carg = MALLOC0(counter_arg_s, 1);
542 if (stc->carg == NULL)
545 stc->carg->sock = stc_monitor_get_counter_socket();
548 memset(&counter, 0, sizeof(struct nfacct_rule));
550 counter.carg = stc->carg;
551 counter.classid = app_value->classid;
552 counter.intend = NFACCT_TETH_COUNTER;
554 if (connection->tether_state != TRUE ||
555 connection->tether_iface.ifname == NULL)
558 counter.iftype = connection->tether_iface.type;
559 g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
561 /* get the ip address of the station based on its mac address */
562 ret = stc_plugin_tether_get_station_ip(app_value->mac, &ipaddr);
563 if (ret != STC_ERROR_NONE)
566 /* tethering iptables rule */
567 __add_iptables_tether_in(&counter, ipaddr);
568 __add_iptables_tether_out(&counter, ipaddr);
574 static gboolean __remove_application_monitor_for_tethering(gpointer key, gpointer value,
577 stc_app_value_s *app_value = (stc_app_value_s *)value;
578 stc_app_key_s *app_key = (stc_app_key_s *)key;
579 default_connection_s *connection = (default_connection_s *)data;
580 stc_s *stc = stc_get_manager();
581 struct nfacct_rule counter;
585 STC_LOGI("remove appid(%s) classid(%d)", app_key->app_id,
588 if (stc == NULL || connection == NULL)
592 stc->carg = MALLOC0(counter_arg_s, 1);
593 if (stc->carg == NULL)
596 stc->carg->sock = stc_monitor_get_counter_socket();
599 memset(&counter, 0, sizeof(struct nfacct_rule));
601 counter.carg = stc->carg;
602 counter.classid = app_value->classid;
603 counter.intend = NFACCT_TETH_COUNTER;
605 if (connection->tether_state != TRUE ||
606 connection->tether_iface.ifname == NULL)
609 counter.iftype = connection->tether_iface.type;
610 g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
612 /* get the ip address of the station based on its mac address */
613 ret = stc_plugin_tether_get_station_ip(app_value->mac, &ipaddr);
614 if (ret != STC_ERROR_NONE)
617 __del_iptables_tether_in(&counter, ipaddr);
618 __del_iptables_tether_out(&counter, ipaddr);
624 static gboolean __add_application_monitor(gpointer key, gpointer value,
627 stc_app_value_s *app_value = (stc_app_value_s *)value;
628 stc_app_key_s *app_key = (stc_app_key_s *)key;
629 default_connection_s *connection = (default_connection_s *)data;
630 stc_s *stc = stc_get_manager();
632 if (app_value->classid == STC_TOTAL_DATACALL_CLASSID ||
633 app_value->classid == STC_TOTAL_WIFI_CLASSID ||
634 app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID)
637 if (stc && connection && connection->ifname) {
638 struct nfacct_rule counter;
641 stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
642 if (stc->carg == NULL) //LCOV_EXCL_LINE
643 return FALSE; //LCOV_EXCL_LINE
645 stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
648 memset(&counter, 0, sizeof(struct nfacct_rule));
650 counter.carg = stc->carg;
651 counter.classid = app_value->classid;
652 counter.intend = NFACCT_COUNTER;
654 if (connection->tether_state == TRUE &&
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 if (g_str_has_suffix(app_key->app_id, STC_TETHERING_APP_SUFFIX) &&
665 app_value->classid != STC_TETHERING_APP_CLASSID) {
666 __add_application_monitor_for_tethering(key, value, data);
667 } else if (app_value->classid == STC_TOTAL_IPV4_CLASSID) {
668 __add_iptables_in(&counter);
669 __add_iptables_out(&counter);
670 } else if (app_value->classid == STC_TOTAL_IPV6_CLASSID) {
671 __add_ip6tables_in(&counter);
672 __add_ip6tables_out(&counter);
674 __add_iptables_in(&counter);
675 __add_iptables_out(&counter);
676 __add_ip6tables_in(&counter);
677 __add_ip6tables_out(&counter);
684 static gboolean __remove_application_monitor(gpointer key, gpointer value,
687 stc_app_value_s *app_value = (stc_app_value_s *)value;
688 stc_app_key_s *app_key = (stc_app_key_s *)key;
689 default_connection_s *connection = (default_connection_s *)data;
690 stc_s *stc = stc_get_manager();
692 if (stc && connection && connection->ifname) {
693 struct nfacct_rule counter;
696 stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
697 if (stc->carg == NULL) //LCOV_EXCL_LINE
698 return FALSE; //LCOV_EXCL_LINE
700 stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
703 memset(&counter, 0, sizeof(struct nfacct_rule));
705 counter.carg = stc->carg;
706 counter.classid = app_value->classid;
707 counter.intend = NFACCT_COUNTER;
709 if (g_str_has_suffix(app_key->app_id, STC_TETHERING_APP_SUFFIX) &&
710 app_value->classid != STC_TETHERING_APP_CLASSID) {
711 __remove_application_monitor_for_tethering(key, value, data);
713 } else if (connection->tether_state == FALSE &&
714 connection->tether_iface.ifname != NULL &&
715 app_value->classid == STC_TETHERING_APP_CLASSID) {
716 counter.iftype = connection->tether_iface.type;
717 g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
719 counter.iftype = connection->type;
720 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
723 __del_iptables_in(&counter);
724 __del_iptables_out(&counter);
725 __del_ip6tables_in(&counter);
726 __del_ip6tables_out(&counter);
732 static void __print_rstn(stc_rstn_key_s *rstn_key, stc_rstn_value_s *rstn_value)
734 STC_LOGI("rstn info => rstn_id [%llu], "
735 "app_id [%s], classid [%u], ifname [%s], "
736 "iftype [%d], rstn_state [%d], rstn_type [%d], "
737 "month_start_date [%d], limit [ (%lld) bytes], "
738 "warn_limit [ (%lld) bytes], "
739 "monthly_limit [ (%lld) bytes], "
740 "weekly_limit [ (%lld) bytes], "
741 "daily_limit [ (%lld) bytes], "
742 "data_counter [ (%lld) bytes], "
743 "warn_counter [ (%lld) bytes], "
744 "monthly_counter [ (%lld) bytes], "
745 "weekly_counter [ (%lld) bytes], "
746 "daily_counter [ (%lld) bytes], "
747 "roaming [%d], subscriber_id [%s]",
748 rstn_value->restriction_id,
749 rstn_key->app_id, rstn_value->classid , rstn_key->ifname,
750 rstn_key->iftype, rstn_value->rstn_state, rstn_value->rstn_type,
751 rstn_value->month_start_date,
752 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA],
753 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN],
754 rstn_value->limit[STC_RSTN_LIMIT_TYPE_MONTHLY],
755 rstn_value->limit[STC_RSTN_LIMIT_TYPE_WEEKLY],
756 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DAILY],
757 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA],
758 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN],
759 rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY],
760 rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY],
761 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY],
762 rstn_key->roaming, rstn_key->subscriber_id);
765 static void __add_iptables_rule(int64_t classid, nfacct_rule_intend intend,
766 stc_iface_type_e iftype)
768 char *default_ifname = stc_default_connection_get_ifname();
769 default_connection_s *connection = stc_get_default_connection();
770 struct nfacct_rule counter;
771 stc_s *stc = stc_get_manager();
773 g_free(default_ifname); //LCOV_EXCL_LINE
774 return; //LCOV_EXCL_LINE
778 stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
779 if (stc->carg == NULL) { //LCOV_EXCL_LINE
780 g_free(default_ifname); //LCOV_EXCL_LINE
781 return; //LCOV_EXCL_LINE
784 stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
787 memset(&counter, 0, sizeof(struct nfacct_rule));
789 counter.carg = stc->carg;
790 counter.classid = classid;
791 counter.intend = intend;
793 if (connection && connection->tether_iface.ifname != NULL &&
794 classid == STC_TETHERING_APP_CLASSID) {
795 counter.iftype = connection->tether_iface.type;
796 g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
798 counter.iftype = iftype;
799 g_strlcpy(counter.ifname, default_ifname, MAX_IFACE_LENGTH);
802 g_free(default_ifname);
805 __add_iptables_in(&counter);
806 __add_iptables_out(&counter);
809 __add_ip6tables_in(&counter);
810 __add_ip6tables_out(&counter);
813 static void __add_tethering_iptables_rule(int64_t classid, gchar *mac,
814 nfacct_rule_intend intend, stc_iface_type_e iftype)
816 default_connection_s *connection = stc_get_default_connection();
817 struct nfacct_rule counter;
818 stc_s *stc = stc_get_manager();
826 stc->carg = MALLOC0(counter_arg_s, 1);
827 if (stc->carg == NULL)
830 stc->carg->sock = stc_monitor_get_counter_socket();
833 memset(&counter, 0, sizeof(struct nfacct_rule));
835 counter.carg = stc->carg;
836 counter.classid = classid;
837 counter.intend = intend;
839 if (connection->tether_state != TRUE ||
840 connection->tether_iface.ifname == NULL)
843 counter.iftype = connection->tether_iface.type;
844 g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
846 /* get connected station ip based on its mac */
847 ret = stc_plugin_tether_get_station_ip(mac, &ipaddr);
848 if (ret != STC_ERROR_NONE)
851 /* tethering iptables rule */
852 __add_iptables_tether_in(&counter, ipaddr);
853 __add_iptables_tether_out(&counter, ipaddr);
857 static void __del_tethering_iptables_rule(int64_t classid, gchar *mac,
858 nfacct_rule_intend intend, stc_iface_type_e iftype)
860 default_connection_s *connection = stc_get_default_connection();
861 struct nfacct_rule counter;
862 stc_s *stc = stc_get_manager();
870 stc->carg = MALLOC0(counter_arg_s, 1);
871 if (stc->carg == NULL)
874 stc->carg->sock = stc_monitor_get_counter_socket();
877 memset(&counter, 0, sizeof(struct nfacct_rule));
879 counter.carg = stc->carg;
880 counter.classid = classid;
881 counter.intend = intend;
883 if (connection->tether_state != TRUE ||
884 connection->tether_iface.ifname == NULL)
887 counter.iftype = connection->tether_iface.type;
888 g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
890 /* get connected station ip based on its mac */
891 ret = stc_plugin_tether_get_station_ip(mac, &ipaddr);
892 if (ret != STC_ERROR_NONE) {
893 STC_LOGE("Error: no IP found for station mac(%s)", mac);
897 /* tethering iptables rule */
898 __del_iptables_tether_in(&counter, ipaddr);
899 __del_iptables_tether_out(&counter, ipaddr);
903 static void __del_iptables_rule(int64_t classid, nfacct_rule_intend intend,
904 stc_iface_type_e iftype)
906 char *default_ifname = stc_default_connection_get_ifname();
907 default_connection_s *connection = stc_get_default_connection();
908 struct nfacct_rule counter;
909 stc_s *stc = stc_get_manager();
911 g_free(default_ifname); //LCOV_EXCL_LINE
912 return; //LCOV_EXCL_LINE
916 stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
917 if (stc->carg == NULL) { //LCOV_EXCL_LINE
918 g_free(default_ifname); //LCOV_EXCL_LINE
919 return; //LCOV_EXCL_LINE
922 stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
925 memset(&counter, 0, sizeof(struct nfacct_rule));
927 counter.carg = stc->carg;
928 counter.classid = classid;
929 counter.intend = intend;
931 if (connection && connection->tether_iface.ifname != NULL &&
932 classid == STC_TETHERING_APP_CLASSID) {
933 counter.iftype = connection->tether_iface.type;
934 g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
936 counter.iftype = iftype;
937 g_strlcpy(counter.ifname, default_ifname, MAX_IFACE_LENGTH);
940 g_free(default_ifname);
943 __del_iptables_in(&counter);
944 __del_iptables_out(&counter);
947 __del_ip6tables_in(&counter);
948 __del_ip6tables_out(&counter);
951 static void __set_rstn_noti_state(int value)
953 int state = STC_RSTN_STATE_INIT;
955 if (__vconf_get_int(VCONFKEY_SETAPPL_DATA_RESTRICTION_INT, &state))
958 if (state == value) {
959 STC_LOGI("No need to change a restriction status: %d", state);
963 vconf_set_int(VCONFKEY_SETAPPL_DATA_RESTRICTION_INT, value);
968 time_t month_start_ts;
969 time_t week_start_ts;
971 int64_t monthly_stat;
976 static stc_cb_ret_e __statistics_info_cb(const table_statistics_info *info,
979 cumulative_data_s *stat = (cumulative_data_s *)user_data;
980 int64_t counters = 0;
982 counters = info->cnt.in_bytes + info->cnt.out_bytes;
984 stat->monthly_stat += counters;
985 if (stat->week_start_ts <= info->interval->from)
986 stat->weekly_stat += counters;
987 if (stat->day_start_ts <= info->interval->from)
988 stat->daily_stat += counters;
993 static void __process_tethering_restriction(enum traffic_restriction_type rstn_type,
994 stc_rstn_key_s *rstn_key, stc_rstn_value_s *rstn_value, void *data)
996 default_connection_s *old_connection = (default_connection_s *)data;
997 default_connection_s *connection = NULL;
998 char *mac_str = NULL;
1000 if (old_connection != NULL)
1001 connection = old_connection;
1003 connection = stc_get_default_connection();
1005 /* in case tethering is not active */
1006 if (connection->tether_state == FALSE)
1009 /* rstn not applicable for this interface */
1010 if (rstn_key->ifname != NULL && g_strcmp0("", rstn_key->ifname) != 0 &&
1011 (g_strcmp0(connection->tether_iface.ifname, rstn_key->ifname) != 0))
1014 /* in case appid not a tethering app */
1015 if (!g_str_has_suffix(rstn_key->app_id, STC_TETHERING_APP_SUFFIX))
1018 /* Ignore TOTAL_TETHERING,
1019 * Process only station appids */
1020 if (rstn_value->classid == STC_TETHERING_APP_CLASSID)
1023 /* get the station mac based on classid */
1024 stc_plugin_tether_get_station_by_classid(rstn_value->classid, &mac_str);
1026 STC_LOGE("station not found for classid(%d)", rstn_value->classid);
1030 switch (rstn_type) {
1034 table_counters_info info;
1035 int64_t effective_limit[STC_RSTN_LIMIT_TYPE_MAX] = { 0, };
1037 memset(&info, 0, sizeof(table_counters_info));
1038 rstn_value->limit_exceeded = 0;
1040 if ((rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] == 0 &&
1041 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA] >= 0) ||
1042 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] == 0 &&
1043 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] >= 0) ||
1044 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] == 0 &&
1045 rstn_value->limit[STC_RSTN_LIMIT_TYPE_MONTHLY] >= 0) ||
1046 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] == 0 &&
1047 rstn_value->limit[STC_RSTN_LIMIT_TYPE_WEEKLY] >= 0) ||
1048 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] == 0 &&
1049 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DAILY] >= 0)) {
1050 table_counters_get(rstn_value->restriction_id, &info);
1052 time_t current_time = 0;
1053 cumulative_data_s stat;
1054 table_statistics_select_rule rule;
1056 memset(&stat, 0, sizeof(cumulative_data_s));
1057 stat.month_start_ts = rstn_value->month_start_ts;
1058 stat.week_start_ts = g_system->last_week_ts;
1059 stat.day_start_ts = g_system->last_day_ts;
1061 memset(&rule, 0, sizeof(table_statistics_select_rule));
1062 rule.from = rstn_value->month_start_ts;
1063 time(¤t_time);
1064 rule.to = current_time;
1065 rule.iftype = rstn_key->iftype;
1066 rule.granularity = GRANULARITY;
1068 table_statistics_per_app(rstn_key->app_id, &rule, __statistics_info_cb, &stat);
1070 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] = info.data_counter;
1071 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info.warn_counter;
1072 rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] = info.monthly_counter + stat.monthly_stat;
1073 rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] = info.weekly_counter + stat.weekly_stat;
1074 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] = info.daily_counter + stat.daily_stat;
1077 for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
1078 if (rstn_value->limit[i] >= 0) {
1079 effective_limit[i] = rstn_value->limit[i] - rstn_value->counter[i];
1081 if (effective_limit[i] < 0)
1082 rstn_value->limit_exceeded |= (1 << i);
1086 STC_LOGD("rstn_id [%llu], datausage [%llu] bytes",
1087 rstn_value->restriction_id, info.data_counter);
1089 if (rstn_value->limit_exceeded != 0 &&
1090 rstn_value->limit_exceeded != (1 << STC_RSTN_LIMIT_TYPE_DATA_WARN)) {
1091 __add_tethering_iptables_rule(rstn_value->classid, mac_str,
1092 NFACCT_TETH_BLOCK, rstn_key->iftype);
1095 rstn_value->rstn_state = STC_RSTN_STATE_ACTIVATED;
1100 __add_tethering_iptables_rule(rstn_value->classid, mac_str,
1101 NFACCT_TETH_ALLOW, rstn_key->iftype);
1103 rstn_value->rstn_state = STC_RSTN_STATE_ACTIVATED;
1104 rstn_value->limit_exceeded = 0;
1105 rstn_value->limit_notified = 0;
1111 __del_tethering_iptables_rule(rstn_value->classid, mac_str,
1112 NFACCT_TETH_BLOCK, rstn_key->iftype);
1114 rstn_value->rstn_state = STC_RSTN_STATE_DEACTIVATED;
1115 rstn_value->limit_exceeded = 0;
1116 rstn_value->limit_notified = 0;
1118 for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++)
1119 if (rstn_value->limit[i] >= 0)
1120 rstn_value->counter[i] = 0;
1129 static void __process_restriction(enum traffic_restriction_type rstn_type,
1130 stc_rstn_key_s *rstn_key,
1131 stc_rstn_value_s *rstn_value, void *data)
1133 default_connection_s *old_connection = (default_connection_s *)data;
1134 default_connection_s *connection = NULL;
1136 if (old_connection != NULL)
1137 connection = old_connection;
1139 connection = stc_get_default_connection();
1141 /* no default ifname */
1142 if (connection->ifname == NULL)
1145 /* rstn not applicable for this interface */
1146 if (rstn_key->ifname != NULL && g_strcmp0("", rstn_key->ifname) != 0 &&
1147 (g_strcmp0(connection->ifname, rstn_key->ifname) != 0) &&
1148 (g_strcmp0(connection->tether_iface.ifname, rstn_key->ifname) != 0))
1151 /* classid is invalid */
1152 if (rstn_value->classid <= STC_UNKNOWN_CLASSID)
1155 /* Do not proceed for tethering station appid if found here,
1156 * for tethering station apps __process_tethering_restriction() call
1158 if (g_str_has_suffix(rstn_key->app_id, STC_TETHERING_APP_SUFFIX) &&
1159 rstn_value->classid != STC_TETHERING_APP_CLASSID)
1162 switch (rstn_type) {
1166 table_counters_info info;
1167 int64_t effective_limit[STC_RSTN_LIMIT_TYPE_MAX] = { 0, };
1169 memset(&info, 0, sizeof(table_counters_info));
1170 rstn_value->limit_exceeded = 0;
1172 if ((rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] == 0 &&
1173 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA] >= 0) ||
1174 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] == 0 &&
1175 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] >= 0) ||
1176 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] == 0 &&
1177 rstn_value->limit[STC_RSTN_LIMIT_TYPE_MONTHLY] >= 0) ||
1178 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] == 0 &&
1179 rstn_value->limit[STC_RSTN_LIMIT_TYPE_WEEKLY] >= 0) ||
1180 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] == 0 &&
1181 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DAILY] >= 0)) {
1182 table_counters_get(rstn_value->restriction_id, &info);
1184 time_t current_time = 0;
1185 cumulative_data_s stat;
1186 table_statistics_select_rule rule;
1188 memset(&stat, 0, sizeof(cumulative_data_s));
1189 stat.month_start_ts = rstn_value->month_start_ts;
1190 stat.week_start_ts = g_system->last_week_ts;
1191 stat.day_start_ts = g_system->last_day_ts;
1193 memset(&rule, 0, sizeof(table_statistics_select_rule));
1194 rule.from = rstn_value->month_start_ts;
1195 time(¤t_time);
1196 rule.to = current_time;
1197 rule.iftype = rstn_key->iftype;
1198 rule.granularity = GRANULARITY;
1200 table_statistics_per_app(rstn_key->app_id, &rule, __statistics_info_cb, &stat);
1202 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] = info.data_counter;
1203 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info.warn_counter;
1204 rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] = info.monthly_counter + stat.monthly_stat;
1205 rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] = info.weekly_counter + stat.weekly_stat;
1206 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] = info.daily_counter + stat.daily_stat;
1209 for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
1210 if (rstn_value->limit[i] >= 0) {
1211 effective_limit[i] = rstn_value->limit[i] - rstn_value->counter[i];
1213 if (effective_limit[i] < 0)
1214 rstn_value->limit_exceeded |= (1 << i);
1218 STC_LOGD("rstn_id [%llu], datausage [%llu] bytes",
1219 rstn_value->restriction_id, info.data_counter);
1221 if (rstn_value->limit_exceeded != 0 &&
1222 rstn_value->limit_exceeded != (1 << STC_RSTN_LIMIT_TYPE_DATA_WARN)) {
1223 __add_iptables_rule(rstn_value->classid, NFACCT_BLOCK, rstn_key->iftype);
1226 rstn_value->rstn_state = STC_RSTN_STATE_ACTIVATED;
1230 __add_iptables_rule(rstn_value->classid, NFACCT_ALLOW,
1233 rstn_value->rstn_state = STC_RSTN_STATE_ACTIVATED;
1234 rstn_value->limit_exceeded = 0;
1235 rstn_value->limit_notified = 0;
1241 if (rstn_value->classid == STC_TETHERING_APP_CLASSID)
1242 __del_iptables_rule(rstn_value->classid, NFACCT_BLOCK,
1245 __del_iptables_rule(rstn_value->classid, rstn_value->rstn_type,
1248 rstn_value->rstn_state = STC_RSTN_STATE_DEACTIVATED;
1249 rstn_value->limit_exceeded = 0;
1250 rstn_value->limit_notified = 0;
1252 for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++)
1253 if (rstn_value->limit[i] >= 0)
1254 rstn_value->counter[i] = 0;
1256 __set_rstn_noti_state(STC_RSTN_STATE_UNSET);
1265 static gboolean __remove_rstns_foreach_application(gpointer key,
1269 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1270 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1271 gchar *app_id = (gchar *)data;
1273 /* rstn rule is not for applications */
1274 if (rstn_key->app_id == NULL)
1277 /* rstn rule is not for this application */
1278 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
1281 /* rstn rule is already removed */
1282 if (rstn_value->rstn_state == STC_RSTN_STATE_DEACTIVATED)
1285 /* remove restriction from system */
1286 __process_restriction(RST_UNSET, rstn_key, rstn_value, NULL);
1288 /* remove tethering restriction from system*/
1289 __process_tethering_restriction(RST_UNSET, rstn_key, rstn_value, NULL);
1291 __print_rstn(rstn_key, rstn_value);
1297 static void __remove_rstns_for_application(gchar *app_id)
1299 g_tree_foreach(g_system->rstns, __remove_rstns_foreach_application,
1303 static stc_error_e __application_remove_if_empty(const stc_app_key_s *app_key)
1305 stc_error_e ret = STC_ERROR_NONE;
1306 guint pid_count = 0;
1307 stc_app_value_s *lookup;
1309 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1311 lookup = __application_lookup(g_system->apps, app_key);
1313 STC_LOGE("app_key not found"); //LCOV_EXCL_LINE
1314 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
1317 g_tree_foreach(lookup->processes, __processes_tree_check_empty,
1321 /* remove nfacct rule for this classid */
1322 __remove_application_monitor((gpointer) app_key, lookup,
1323 stc_get_default_connection());
1324 __remove_rstns_for_application(app_key->app_id);
1327 if (!g_tree_remove(g_system->apps, app_key)) {
1328 ret = STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
1329 STC_LOGE("key not found"); //LCOV_EXCL_LINE
1335 static stc_error_e __close_contr_sock(stc_system_s *system)
1337 ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
1339 /* close netlink socket for updating kernel counters */
1340 if (system->contr_sock != -1) {
1341 close(system->contr_sock);
1342 system->contr_sock = -1;
1345 if (system->contr_gsource_id != 0) {
1346 g_source_remove(system->contr_gsource_id);
1347 system->contr_gsource_id = 0;
1350 return STC_ERROR_NONE;
1353 static gboolean __process_contr_reply(GIOChannel *source,
1354 GIOCondition condition,
1355 gpointer user_data);
1358 static stc_error_e __close_and_reopen_contr_sock(stc_system_s *system)
1360 GIOChannel *gio = NULL;
1361 ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
1363 /* close netlink socket for updating kernel counters */
1364 if (system->contr_sock != -1) {
1365 close(system->contr_sock);
1366 system->contr_sock = -1;
1369 if (system->contr_gsource_id != 0) {
1370 g_source_remove(system->contr_gsource_id);
1371 system->contr_gsource_id = 0;
1374 /* create netlink socket for updating kernel counters */
1375 system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
1376 if (system->contr_sock < 0) {
1377 STC_LOGE("failed to open socket");
1379 return STC_ERROR_FAIL;
1382 gio = g_io_channel_unix_new(system->contr_sock);
1383 system->contr_gsource_id =
1384 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
1385 (GIOFunc) __process_contr_reply,
1387 g_io_channel_unref(gio);
1389 return STC_ERROR_NONE;
1392 static void __action_when_rstn_limit_exceeded_tethering(stc_rstn_key_s *rstn_key,
1393 stc_rstn_value_s *rstn_value, classid_bytes_context_s *context)
1395 char *mac_str = NULL;
1396 struct nfacct_rule *counter = context->counter;
1398 /* get the station mac based on classid */
1399 stc_plugin_tether_get_station_by_classid(counter->classid, &mac_str);
1401 STC_LOGE("station not found for classid(%d)", counter->classid);
1405 STC_LOGI("station mac %s, classid %u, iftype %u, iotype %d, \
1406 intend %d, ifname %s, bytes %lld", mac_str,
1407 counter->classid, counter->iftype, counter->iotype,
1408 counter->intend, counter->ifname, context->bytes);
1410 /* Block tethering station immediately */
1411 counter->intend = NFACCT_TETH_BLOCK;
1412 __del_tethering_iptables_rule(counter->classid, mac_str,
1413 NFACCT_TETH_BLOCK, rstn_key->iftype);
1415 __add_tethering_iptables_rule(counter->classid, mac_str,
1416 NFACCT_TETH_BLOCK, rstn_key->iftype);
1417 counter->intend = NFACCT_TETH_COUNTER;
1422 static void __action_when_rstn_limit_exceeded(stc_rstn_limit_type_e limit_type,
1423 stc_rstn_key_s *rstn_key,
1424 stc_rstn_value_s *rstn_value,
1425 classid_bytes_context_s *context)
1428 char iftype[MAX_INT_LENGTH] = { 0, };
1429 char byte[MAX_INT_LENGTH] = { 0, };
1430 const char *signal_name = NULL;
1431 const char *net_popup_content = NULL;
1432 const char *net_popup_type = NULL;
1433 stc_s *stc = (stc_s *)stc_get_manager();
1436 STC_LOGE("Failed to get stc data");
1440 switch (limit_type) {
1441 case STC_RSTN_LIMIT_TYPE_DATA_WARN:
1443 signal_name = "WarnThresholdCrossed";
1444 net_popup_content = "warn threshold crossed";
1445 net_popup_type = "warning_noti";
1448 case STC_RSTN_LIMIT_TYPE_DATA:
1449 case STC_RSTN_LIMIT_TYPE_MONTHLY:
1450 case STC_RSTN_LIMIT_TYPE_WEEKLY:
1451 case STC_RSTN_LIMIT_TYPE_DAILY:
1453 signal_name = "RestrictionThresholdCrossed";
1454 net_popup_content = "restriction threshold crossed";
1455 net_popup_type = "restriction_noti";
1457 /* Apply restriction for tethering apps if app_id is of tethering client
1458 * otherwise do the normal iptables rule */
1459 if (context->counter->intend == NFACCT_TETH_COUNTER) {
1461 if (g_str_has_suffix(rstn_key->app_id, STC_TETHERING_APP_SUFFIX) &&
1462 rstn_value->classid != STC_TETHERING_APP_CLASSID) {
1463 __action_when_rstn_limit_exceeded_tethering(rstn_key, rstn_value,
1468 /* block immediately */
1469 context->counter->intend = NFACCT_BLOCK;
1470 __del_iptables_in(context->counter);
1471 __del_iptables_out(context->counter);
1472 __add_iptables_in(context->counter);
1473 __add_iptables_out(context->counter);
1475 __del_ip6tables_in(context->counter);
1476 __del_ip6tables_out(context->counter);
1477 __add_ip6tables_in(context->counter);
1478 __add_ip6tables_out(context->counter);
1479 context->counter->intend = NFACCT_COUNTER;
1482 rstn_value->limit_exceeded |= (1 << limit_type);
1484 __set_rstn_noti_state(STC_RSTN_STATE_SET);
1491 if (signal_name == NULL) {
1492 STC_LOGE("Invalid parameter: limit_type");
1497 rv = stc_manager_dbus_emit_signal(stc->connection,
1498 STC_DBUS_SERVICE_RESTRICTION_PATH,
1499 STC_DBUS_INTERFACE_RESTRICTION,
1501 g_variant_new("(si)",
1506 rstn_value->limit_notified |= (1 << limit_type);
1508 snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
1509 snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->limit[limit_type]);
1510 stc_plugin_appstatus_send_message(net_popup_content,
1511 net_popup_type, rstn_key->app_id, iftype, byte);
1514 static gboolean __rstn_counter_update(stc_rstn_key_s *rstn_key,
1515 stc_rstn_value_s *rstn_value,
1516 classid_bytes_context_s *context)
1519 switch (context->counter->iotype) {
1520 case NFACCT_COUNTER_IN:
1521 case NFACCT_COUNTER_OUT:
1522 if ((rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] == 0 &&
1523 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA] >= 0) ||
1524 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] == 0 &&
1525 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] >= 0) ||
1526 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] == 0 &&
1527 rstn_value->limit[STC_RSTN_LIMIT_TYPE_MONTHLY] >= 0) ||
1528 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] == 0 &&
1529 rstn_value->limit[STC_RSTN_LIMIT_TYPE_WEEKLY] >= 0) ||
1530 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] == 0 &&
1531 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DAILY] >= 0)) {
1532 table_counters_info info;
1533 memset(&info, 0, sizeof(table_counters_info));
1534 table_counters_get(rstn_value->restriction_id, &info);
1536 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] = info.data_counter;
1537 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info.warn_counter;
1538 rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] = info.monthly_counter;
1539 rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] = info.weekly_counter;
1540 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] = info.daily_counter;
1543 for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
1544 if (rstn_value->limit[i] >= 0 &&
1545 !(rstn_value->limit_notified & (1 << i))) {
1546 rstn_value->counter[i] += context->bytes;
1547 if (rstn_value->limit[i] <= rstn_value->counter[i])
1548 __action_when_rstn_limit_exceeded(i,
1555 g_system->rstns_tree_updated = TRUE;
1556 __print_rstn(rstn_key, rstn_value);
1559 STC_LOGE("unknown iotype");
1565 static gboolean __interface_rstn_counter_update(stc_rstn_key_s *rstn_key,
1566 stc_rstn_value_s *rstn_value,
1567 classid_bytes_context_s *context)
1569 if ((rstn_value->classid == STC_TOTAL_DATACALL_CLASSID &&
1570 context->counter->iftype == STC_IFACE_DATACALL) ||
1571 (rstn_value->classid == STC_TOTAL_WIFI_CLASSID &&
1572 context->counter->iftype == STC_IFACE_WIFI) ||
1573 (rstn_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
1574 context->counter->iftype == STC_IFACE_BLUETOOTH) ||
1575 (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
1576 context->counter->iftype == STC_IFACE_WIFI) ||
1577 (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
1578 context->counter->iftype == STC_IFACE_BLUETOOTH) ||
1579 (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
1580 context->counter->iftype == STC_IFACE_USB) ||
1581 (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
1582 context->counter->iftype == STC_IFACE_P2P)) {
1583 context->counter->classid = rstn_value->classid;
1584 return __rstn_counter_update(rstn_key, rstn_value, context);
1590 static gboolean __rstn_counter_update_foreach_classid(gpointer key,
1594 gboolean rv = FALSE;
1595 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1596 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1597 classid_bytes_context_s *context = (classid_bytes_context_s *)data;
1600 if (context->counter->intend != NFACCT_COUNTER &&
1601 context->counter->intend != NFACCT_TETH_COUNTER)
1602 goto try_next_callback;
1604 if (rstn_value->limit_exceeded == TRUE) {
1605 context->data_limit_exceeded = TRUE; //LCOV_EXCL_LINE
1606 goto try_next_callback; //LCOV_EXCL_LINE
1609 classid = context->counter->classid;
1610 rv = __interface_rstn_counter_update(rstn_key, rstn_value, context);
1612 context->counter->classid = classid;
1613 if (rstn_value->classid != context->counter->classid)
1614 goto try_next_callback;
1616 rv = __rstn_counter_update(rstn_key, rstn_value, context);
1623 static gboolean __update_app_statistics(gpointer key, gpointer value,
1626 stc_app_key_s *app_key = (stc_app_key_s *)key;
1627 stc_app_value_s *app_value = (stc_app_value_s *)value;
1628 time_t *touch_time = (time_t *)data;
1629 stc_db_classid_iftype_key stat_key;
1630 stc_db_app_stats stat;
1631 default_connection_s *default_connection = stc_get_default_connection();
1633 memset(&stat_key, 0, sizeof(stc_db_classid_iftype_key));
1634 memset(&stat, 0 , sizeof(stc_db_app_stats));
1636 /* Do not update statistics for Tethering
1637 * if tethering is in-active found */
1638 if (default_connection &&
1639 default_connection->tether_state == FALSE &&
1640 !strcmp(app_key->app_id, STC_TOTAL_TETHERING))
1643 /* Do not update statistics for Wi-Fi
1644 * if tethering is active on wlan0 iface */
1645 if (default_connection && default_connection->tether_state &&
1646 default_connection->tether_iface.type == STC_IFACE_WIFI &&
1647 !strcmp(app_key->app_id, STC_TOTAL_WIFI))
1650 stat_key.classid = app_value->classid;
1652 if (app_value->classid == STC_TETHERING_APP_CLASSID &&
1653 default_connection->tether_state == TRUE)
1654 stat_key.iftype = default_connection->tether_iface.type;
1655 else if (g_str_has_suffix(app_key->app_id, STC_TETHERING_APP_SUFFIX))
1656 stat_key.iftype = default_connection->tether_iface.type;
1658 stat_key.iftype = default_connection->type;
1660 if (STC_IFACE_DATACALL == stat_key.iftype)
1661 stat_key.subscriber_id = g_strdup(default_connection->subscriber_id);
1663 stat_key.subscriber_id = g_strdup("none_subid"); //LCOV_EXCL_LINE
1665 if (app_value->classid == STC_TETHERING_APP_CLASSID &&
1666 default_connection->tether_state == TRUE)
1667 g_strlcpy(stat_key.ifname, default_connection->tether_iface.ifname,
1669 else if (g_str_has_suffix(app_key->app_id, STC_TETHERING_APP_SUFFIX))
1670 g_strlcpy(stat_key.ifname, default_connection->tether_iface.ifname,
1673 g_strlcpy(stat_key.ifname, default_connection->ifname,
1676 stat.app_id = g_strdup(app_key->app_id);
1677 stat.snd_count = app_value->counter.out_bytes;
1678 stat.rcv_count = app_value->counter.in_bytes;
1679 stat.is_roaming = default_connection->roaming;
1681 if (strstr(stat.app_id, "_BACKGROUND")) {
1682 stat.ground = STC_APP_STATE_BACKGROUND;
1684 if (strstr(stat.app_id, "TOTAL_"))
1685 stat.ground = STC_APP_STATE_UNKNOWN;
1687 stat.ground = STC_APP_STATE_FOREGROUND;
1690 table_statistics_insert(&stat_key, &stat, *touch_time);
1692 app_value->counter.out_bytes = 0;
1693 app_value->counter.in_bytes = 0;
1696 FREE(stat_key.subscriber_id);
1701 static gboolean __flush_apps_stats_to_database(gpointer user_data)
1703 time_t current_time = 0;
1704 stc_s *stc = stc_get_manager();
1706 if (stc && stc->carg)
1707 current_time = stc->carg->last_run_time;
1709 if (g_system->apps_tree_updated == FALSE)
1710 return G_SOURCE_REMOVE;
1712 g_system->apps_tree_updated = FALSE;
1715 g_tree_foreach(g_system->apps,
1716 __update_app_statistics,
1719 STC_LOGI("Flushed app stats to database");
1720 return G_SOURCE_REMOVE;
1724 static gboolean __update_counter_statistics(gpointer key, gpointer value,
1727 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1728 table_counters_info info = {
1729 .restriction_id = rstn_value->restriction_id,
1730 .data_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA],
1731 .warn_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN],
1732 .monthly_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY],
1733 .weekly_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY],
1734 .daily_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY]
1737 table_counters_update_counters(&info);
1742 static gboolean __flush_rstns_counter_to_database(gpointer user_data)
1744 time_t current_time = 0;
1745 stc_s *stc = stc_get_manager();
1747 if (stc && stc->carg)
1748 current_time = stc->carg->last_run_time;
1750 if (g_system->rstns_tree_updated == FALSE)
1751 return G_SOURCE_REMOVE;
1753 g_system->rstns_tree_updated = FALSE;
1755 if (g_system->rstns)
1756 g_tree_foreach(g_system->rstns,
1757 __update_counter_statistics,
1760 STC_LOGI("Flushed rstns counters to database");
1761 return G_SOURCE_REMOVE;
1765 static void __app_counter_update(stc_app_key_s *app_key,
1766 stc_app_value_s *app_value,
1767 classid_bytes_context_s *context)
1769 switch (context->counter->iotype) {
1770 case NFACCT_COUNTER_IN:
1771 app_value->data_usage.in_bytes += context->bytes;
1772 app_value->counter.in_bytes = context->bytes;
1773 g_system->apps_tree_updated = TRUE;
1776 __apps_tree_foreach_print(app_key, app_value, NULL); //LCOV_EXCL_LINE
1779 case NFACCT_COUNTER_OUT:
1780 app_value->data_usage.out_bytes += context->bytes;
1781 app_value->counter.out_bytes = context->bytes;
1782 g_system->apps_tree_updated = TRUE;
1785 __apps_tree_foreach_print(app_key, app_value, NULL); //LCOV_EXCL_LINE
1789 STC_LOGE("unknown iotype"); //LCOV_EXCL_LINE
1793 static void __interface_counter_update(stc_app_key_s *app_key,
1794 stc_app_value_s *app_value,
1795 classid_bytes_context_s *context)
1797 if ((app_value->classid == STC_TOTAL_DATACALL_CLASSID &&
1798 context->counter->iftype == STC_IFACE_DATACALL) ||
1799 (app_value->classid == STC_TOTAL_WIFI_CLASSID &&
1800 context->counter->iftype == STC_IFACE_WIFI) ||
1801 (app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
1802 context->counter->iftype == STC_IFACE_BLUETOOTH) ||
1803 (app_value->classid == STC_TETHERING_APP_CLASSID &&
1804 context->counter->iftype == STC_IFACE_WIFI) ||
1805 (app_value->classid == STC_TETHERING_APP_CLASSID &&
1806 context->counter->iftype == STC_IFACE_BLUETOOTH) ||
1807 (app_value->classid == STC_TETHERING_APP_CLASSID &&
1808 context->counter->iftype == STC_IFACE_USB) ||
1809 (app_value->classid == STC_TETHERING_APP_CLASSID &&
1810 context->counter->iftype == STC_IFACE_P2P))
1811 __app_counter_update(app_key, app_value, context);
1815 static gboolean __apps_counter_update_foreach_classid(gpointer key,
1819 stc_app_key_s *app_key = (stc_app_key_s *)key;
1820 stc_app_value_s *app_value = (stc_app_value_s *)value;
1821 classid_bytes_context_s *context = (classid_bytes_context_s *)data;
1823 if (context->counter->intend != NFACCT_COUNTER &&
1824 context->counter->intend != NFACCT_TETH_COUNTER)
1825 goto try_next_callback;
1827 __interface_counter_update(app_key, app_value, context);
1829 if (app_value->classid != context->counter->classid)
1830 goto try_next_callback;
1832 __app_counter_update(app_key, app_value, context);
1838 static gboolean __reset_time_counter_foreach_rstn(gpointer key,
1842 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1843 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1844 reset_time_limits_context_s *context = (reset_time_limits_context_s *)data;
1846 time_t now_month_start_ts;
1848 if (rstn_value->month_start_date == 0) {
1849 table_counters_info info;
1850 memset(&info, 0, sizeof(table_counters_info));
1851 table_counters_get_timestamps(rstn_value->restriction_id, &info);
1853 if (info.month_start_date == 0)
1854 rstn_value->month_start_date = 1;
1856 rstn_value->month_start_date = info.month_start_date;
1857 rstn_value->month_start_ts = info.month_start_ts;
1860 now_month_start_ts =
1861 stc_time_get_month_start(context->now,
1862 rstn_value->month_start_date);
1864 if (rstn_value->month_start_ts != now_month_start_ts) {
1865 rstn_value->month_start_ts = now_month_start_ts;
1866 context->month_start_ts = now_month_start_ts;
1867 context->is_updated |= (1 << STC_RSTN_LIMIT_TYPE_MONTHLY);
1870 if (context->is_updated) {
1871 table_counters_info info;
1872 memset(&info, 0, sizeof(table_counters_info));
1874 info.restriction_id = rstn_value->restriction_id;
1875 info.month_start_date = rstn_value->month_start_date;
1876 info.month_start_ts = rstn_value->month_start_ts;
1877 info.week_start_ts = context->week_start_ts;
1878 info.day_start_ts = context->day_start_ts;
1880 table_counters_update_timestamps(&info);
1883 for (i = STC_RSTN_LIMIT_TYPE_MONTHLY; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
1885 if ((context->is_updated) & (1 << i)) {
1887 rstn_value->counter[i] = 0;
1889 if (rstn_value->limit_exceeded & (1 << i)) {
1890 /* remove iptables rule */
1891 char *default_ifname = stc_default_connection_get_ifname();
1892 struct nfacct_rule counter;
1893 stc_s *stc = stc_get_manager();
1895 STC_LOGE("Can't get stc data");
1896 g_free(default_ifname);
1897 goto try_next_callback;
1901 stc->carg = MALLOC0(counter_arg_s, 1);
1902 if (stc->carg == NULL) {
1903 g_free(default_ifname);
1904 goto try_next_callback;
1908 stc_monitor_get_counter_socket();
1911 memset(&counter, 0, sizeof(struct nfacct_rule));
1913 counter.carg = stc->carg;
1914 counter.classid = rstn_value->classid;
1915 counter.intend = NFACCT_BLOCK;
1916 counter.iftype = rstn_key->iftype;
1917 g_strlcpy(counter.ifname, default_ifname,
1920 g_free(default_ifname);
1923 __del_iptables_in(&counter);
1924 __del_iptables_out(&counter);
1926 /* ip6tables rule */
1927 __del_ip6tables_in(&counter);
1928 __del_ip6tables_out(&counter);
1930 rstn_value->rstn_state = STC_RSTN_STATE_DEACTIVATED;
1931 rstn_value->limit_exceeded &= ~(1 << i);
1932 rstn_value->limit_notified &= ~(1 << i);
1941 static void __reset_time_counters_if_required(void)
1943 reset_time_limits_context_s context;
1945 if (g_system == NULL) {
1946 STC_LOGE("stc monitor not initialized!");
1950 context.now = time(NULL);
1951 context.week_start_ts = stc_time_get_week_start(context.now);
1952 context.day_start_ts = stc_time_get_day_start(context.now);
1953 context.is_updated = 0;
1955 if (g_system->last_week_ts != context.week_start_ts) {
1956 g_system->last_week_ts = context.week_start_ts;
1957 context.is_updated |= (1 << STC_RSTN_LIMIT_TYPE_WEEKLY);
1960 if (g_system->last_day_ts != context.day_start_ts) {
1961 g_system->last_day_ts = context.day_start_ts;
1962 context.is_updated |= (1 << STC_RSTN_LIMIT_TYPE_DAILY);
1965 if (g_system->rstns) {
1966 g_tree_foreach(g_system->rstns,
1967 __reset_time_counter_foreach_rstn,
1969 if (context.is_updated)
1970 STC_LOGD("Counter reset completed month_start [%ld], week_start [%ld], day_start [%ld]",
1971 context.month_start_ts, g_system->last_week_ts, g_system->last_day_ts);
1975 static void __fill_nfacct_result(char *cnt_name, int64_t bytes,
1976 struct counter_arg *carg)
1978 __reset_time_counters_if_required();
1980 struct nfacct_rule counter = {
1987 classid_bytes_context_s context = {
1988 .counter = &counter,
1990 .data_limit_exceeded = FALSE,
1993 if (!recreate_counter_by_name(cnt_name, &counter)) {
1994 STC_LOGE("Can't parse counter name %s", cnt_name); //LCOV_EXCL_LINE
1995 return; //LCOV_EXCL_LINE
1999 STC_LOGI("classid %u, iftype %u, iotype %d, intend %d, ifname %s, bytes %lld",
2000 context.counter->classid, context.counter->iftype,
2001 context.counter->iotype, context.counter->intend,
2002 context.counter->ifname, context.bytes);
2004 if (g_system->rstns)
2005 g_tree_foreach(g_system->rstns,
2006 __rstn_counter_update_foreach_classid,
2010 g_tree_foreach(g_system->apps,
2011 __apps_counter_update_foreach_classid,
2015 static int __fill_counters(struct rtattr *attr_list[__NFACCT_MAX],
2018 struct counter_arg *carg = user_data;
2019 char *cnt_name = (char *)RTA_DATA(attr_list[NFACCT_NAME]);
2020 if (carg->initiate) {
2022 * TODO: this will be used when daemon starts to update existing
2023 * counter data if present.
2025 populate_counters(cnt_name, carg);
2029 (int64_t *)RTA_DATA(attr_list[NFACCT_BYTES]);
2030 int bytes = be64toh(*bytes_p);
2032 ++carg->serialized_counters;
2033 __fill_nfacct_result(cnt_name, bytes, carg);
2040 static int __post_fill_counters(void *user_data)
2042 struct counter_arg *carg = user_data;
2050 static void __process_network_counter(struct genl *ans,
2051 struct counter_arg *carg)
2053 struct netlink_serialization_params ser_params = {
2056 .eval_attr = __fill_counters,
2057 .post_eval_attr = __post_fill_counters,
2060 netlink_serialization_command *netlink =
2061 netlink_create_command(&ser_params);
2063 STC_LOGE("Can not create command"); //LCOV_EXCL_LINE
2064 return; //LCOV_EXCL_LINE
2067 netlink->deserialize_answer(&(netlink->params));
2070 static gboolean __process_contr_reply(GIOChannel *source,
2071 GIOCondition condition,
2074 int sock = g_io_channel_unix_get_fd(source);
2077 stc_s *stc = stc_get_manager();
2080 void __gcov_flush(void);
2084 if ((condition & G_IO_ERR) || (condition & G_IO_HUP) ||
2085 (condition & G_IO_NVAL)) {
2086 /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */
2088 STC_LOGE("Counter socket received G_IO event, closing socket." //LCOV_EXCL_LINE
2089 "G_IO_ERR [%u], G_IO_HUP [%u], G_IO_NVAL [%u]",
2090 (condition & G_IO_ERR), (condition & G_IO_HUP),
2091 (condition & G_IO_NVAL));
2092 __close_and_reopen_contr_sock(g_system); //LCOV_EXCL_LINE
2093 return FALSE; //LCOV_EXCL_LINE
2096 ans = MALLOC0(struct genl, 1);
2098 STC_LOGE("Failed allocate memory to genl reply message"); //LCOV_EXCL_LINE
2099 return TRUE; //LCOV_EXCL_LINE
2103 STC_LOGE("Can't get stc data"); //LCOV_EXCL_LINE
2104 goto out; //LCOV_EXCL_LINE
2107 ret = read_netlink(sock, ans, sizeof(struct genl));
2108 /* STC_LOGD("Counter data received ret [%d]", ret); */
2112 stc->carg->ans_len = ret;
2113 stc->carg->last_run_time = time(NULL);
2115 __process_network_counter(ans, stc->carg);
2117 g_idle_add(__flush_apps_stats_to_database, NULL);
2118 g_idle_add(__flush_rstns_counter_to_database, NULL);
2125 static gboolean __update_contr_cb(void *user_data)
2127 /* Here we just sent command, answer we receive in another callback */
2128 stc_s *stc = stc_get_manager();
2129 ret_value_msg_if(stc == NULL, STC_ERROR_FAIL, "Can't get stc data");
2131 stc->carg = MALLOC0(counter_arg_s, 1);
2132 if (stc->carg == NULL)
2133 return TRUE; /* we need to continue the timer */
2135 stc->carg->sock = stc_monitor_get_counter_socket();
2139 void __gcov_flush(void);
2143 /* STC_LOGD("Get all counters"); */
2144 nfacct_send_get_all(stc->carg);
2146 /* we need to continue the timer */
2152 static gboolean __rstn_tree_foreach_print(gpointer key, gpointer value,
2155 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
2156 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
2158 __print_rstn(rstn_key, rstn_value);
2162 static void __rstn_tree_printall(void)
2164 g_tree_foreach(g_system->rstns, __rstn_tree_foreach_print, NULL);
2169 static stc_rstn_value_s * __rstn_lookup(GTree *rstns_tree,
2170 const stc_rstn_key_s *key)
2172 stc_rstn_value_s *lookup;
2174 ret_value_msg_if(rstns_tree == NULL, NULL, "rstns_tree is null!");
2176 lookup = g_tree_lookup(rstns_tree, key);
2181 static gboolean __remove_restriction(gpointer key, gpointer value,
2184 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
2185 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
2187 __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
2188 __process_tethering_restriction(RST_UNSET, rstn_key, rstn_value, data);
2189 __print_rstn(rstn_key, rstn_value);
2193 static gboolean __add_restriction_debug(gpointer key, gpointer value,
2196 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
2197 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
2199 /* rstn rule is activated */
2200 if (rstn_value->rstn_state == STC_RSTN_STATE_ACTIVATED)
2203 if (rstn_value->rstn_type == STC_RSTN_TYPE_ACCEPT) {
2204 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
2205 __process_tethering_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
2207 __process_restriction(RST_SET, rstn_key, rstn_value, data);
2208 __process_tethering_restriction(RST_SET, rstn_key, rstn_value, data);
2211 __print_rstn(rstn_key, rstn_value);
2217 static gboolean __add_restriction(gpointer key, gpointer value, gpointer data)
2219 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
2220 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
2222 /* rstn rule is activated */
2223 if (rstn_value->rstn_state == STC_RSTN_STATE_ACTIVATED)
2226 if (rstn_value->rstn_type == STC_RSTN_TYPE_ACCEPT) {
2227 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
2228 __process_tethering_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
2230 __process_restriction(RST_SET, rstn_key, rstn_value, data);
2231 __process_tethering_restriction(RST_SET, rstn_key, rstn_value, data);
2238 static stc_error_e __rstn_tree_remove(stc_rstn_key_s *key)
2240 stc_rstn_value_s *lookup_value;
2242 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2244 lookup_value = __rstn_lookup(g_system->rstns, key);
2245 if (!lookup_value) {
2246 STC_LOGE("key not found"); //LCOV_EXCL_LINE
2247 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
2250 /* remove counter also */
2251 table_counters_delete(lookup_value->restriction_id);
2252 __remove_restriction(key, lookup_value, NULL);
2254 if (!g_tree_remove(g_system->rstns, key)) {
2255 STC_LOGD("key not found"); //LCOV_EXCL_LINE
2256 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
2259 return STC_ERROR_NONE;
2262 static stc_error_e __rstn_tree_add(stc_rstn_key_s *key,
2263 stc_rstn_value_s *value, gboolean debug)
2265 stc_rstn_key_s *rstn_key;
2266 stc_rstn_value_s *rstn_value;
2268 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2270 rstn_value = __rstn_lookup(g_system->rstns, key);
2272 __rstn_tree_remove(key);
2274 rstn_key = MALLOC0(stc_rstn_key_s, 1);
2276 STC_LOGE("rstn_key allocation failed"); //LCOV_EXCL_LINE
2277 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
2280 rstn_value = MALLOC0(stc_rstn_value_s, 1);
2282 STC_LOGE("rstn_value allocation failed"); //LCOV_EXCL_LINE
2283 FREE(rstn_key); //LCOV_EXCL_LINE
2284 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
2287 rstn_key->app_id = g_strdup(key->app_id);
2288 rstn_key->ifname = g_strdup(key->ifname);
2289 rstn_key->mac = g_strdup(key->mac);
2290 rstn_key->subscriber_id = g_strdup(key->subscriber_id);
2291 rstn_key->iftype = key->iftype;
2292 rstn_key->roaming = key->roaming;
2294 g_tree_insert(g_system->rstns, rstn_key, rstn_value);
2296 rstn_value->restriction_id = value->restriction_id;
2297 rstn_value->rstn_state = value->rstn_state;
2298 rstn_value->rstn_type = value->rstn_type;
2299 rstn_value->classid = value->classid;
2302 for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
2303 rstn_value->limit[i] = value->limit[i];
2304 rstn_value->counter[i] = 0;
2307 rstn_value->limit_exceeded = 0;
2308 rstn_value->limit_notified = 0;
2309 rstn_value->month_start_date = value->month_start_date;
2310 rstn_value->month_start_ts = value->month_start_ts;
2313 __add_restriction_debug(key, rstn_value, NULL);
2315 __add_restriction(key, rstn_value, NULL);
2317 return STC_ERROR_NONE;
2321 static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info,
2324 stc_cb_ret_e ret = STC_CONTINUE;
2327 stc_rstn_value_s value;
2329 memset(&key, 0, sizeof(stc_rstn_key_s));
2330 memset(&value, 0, sizeof(stc_rstn_value_s));
2332 key.app_id = g_strdup(info->app_id);
2333 key.ifname = g_strdup(info->ifname);
2334 key.subscriber_id = g_strdup(info->subscriber_id);
2335 key.iftype = info->iftype;
2336 key.roaming = info->roaming;
2338 value.rstn_type = info->rstn_type;
2339 value.rstn_state = STC_RSTN_STATE_UNKNOWN;
2340 value.restriction_id = info->restriction_id;
2343 value.classid = get_classid_by_app_id(info->app_id, TRUE);
2345 value.classid = STC_UNKNOWN_CLASSID;
2347 value.limit[STC_RSTN_LIMIT_TYPE_DATA] = info->data_limit;
2348 value.limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info->data_warn_limit;
2349 value.limit[STC_RSTN_LIMIT_TYPE_MONTHLY] = info->monthly_limit;
2350 value.limit[STC_RSTN_LIMIT_TYPE_WEEKLY] = info->weekly_limit;
2351 value.limit[STC_RSTN_LIMIT_TYPE_DAILY] = info->daily_limit;
2353 if (__rstn_tree_add(&key, &value, FALSE) != STC_ERROR_NONE)
2358 FREE(key.subscriber_id);
2362 static void __fill_restritions_list(void)
2364 table_restrictions_foreach(__insert_restriction_cb, NULL);
2366 /* __rstn_tree_printall(); */
2369 static gboolean __add_rstn_foreach_application(gpointer key,
2373 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
2374 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
2375 gchar *app_id = (gchar *)data;
2377 /* rstn rule is not for applications */
2378 if (rstn_key->app_id == NULL)
2381 /* rstn rule is not for this application */
2382 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
2385 /* rstn rule is already applied */
2386 if (rstn_value->rstn_state == STC_RSTN_STATE_ACTIVATED)
2389 /* add restriction to system */
2390 if (rstn_value->rstn_type == STC_RSTN_TYPE_ACCEPT) {
2391 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, NULL);
2392 __process_tethering_restriction(RST_EXCLUDE, rstn_key, rstn_value, NULL);
2394 __process_restriction(RST_SET, rstn_key, rstn_value, NULL);
2395 __process_tethering_restriction(RST_SET, rstn_key, rstn_value, NULL);
2398 __print_rstn(rstn_key, rstn_value);
2404 static void __add_rstns_for_application(gchar *app_id)
2406 g_tree_foreach(g_system->rstns, __add_rstn_foreach_application,
2410 static void __add_application_by_interface(const char *app_id)
2412 stc_app_key_s app_key;
2413 stc_app_value_s app_value;
2416 return; //LCOV_EXCL_LINE
2418 memset(&app_key, 0, sizeof(stc_app_key_s));
2419 memset(&app_value, 0, sizeof(stc_app_value_s));
2421 app_key.pkg_id = g_strdup(app_id);
2422 app_key.app_id = g_strdup(app_id);
2424 app_value.type = STC_APP_TYPE_NONE;
2425 app_value.processes = NULL;
2426 app_value.counter.in_bytes = 0;
2427 app_value.counter.out_bytes = 0;
2429 stc_monitor_application_add(app_key, app_value);
2431 FREE(app_key.pkg_id);
2432 FREE(app_key.app_id);
2435 static gboolean __processes_tree_foreach_background(gpointer key,
2439 stc_process_key_s *proc_key = (stc_process_key_s *)key;
2440 stc_app_key_s *app_key = (stc_app_key_s *)data;
2442 place_pids_to_net_cgroup(proc_key->pid, app_key->app_id);
2447 static gboolean __apps_tree_foreach_background(gpointer key, gpointer value,
2450 stc_app_key_s *app_key = (stc_app_key_s *)key;
2451 stc_app_value_s *app_value = (stc_app_value_s *)value;
2453 if (strstr(app_key->app_id, STC_BACKGROUND_APP_SUFFIX))
2454 g_tree_foreach(app_value->processes,
2455 __processes_tree_foreach_background, app_key);
2460 static stc_error_e __process_update_background(void)
2462 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2464 g_tree_foreach(g_system->apps, __apps_tree_foreach_background, NULL);
2466 return STC_ERROR_NONE;
2470 static void __fill_exceptions_list(void)
2472 stc_plugin_fill_exception_list();
2475 stc_error_e stc_monitor_init(void)
2477 stc_system_s *system = MALLOC0(stc_system_s, 1);
2478 GIOChannel *gio = NULL;
2480 ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY, "stc_system_s malloc fail!");
2482 /* initializing current classid */
2483 init_current_classid();
2485 /* initializing cgroups */
2488 /* creating monitored application tree */
2489 system->apps = g_tree_new_full(__apps_tree_key_compare, NULL,
2490 __apps_tree_key_free,
2491 __apps_tree_value_free);
2493 system->rstns = g_tree_new_full(__rstns_tree_key_compare, NULL,
2494 __rstns_tree_key_free,
2495 __rstns_tree_value_free);
2497 /* create netlink socket for updating kernel counters */
2498 system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
2499 if (system->contr_sock < 0) {
2500 STC_LOGE("failed to open socket"); //LCOV_EXCL_LINE
2501 FREE(system); //LCOV_EXCL_LINE
2502 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
2505 gio = g_io_channel_unix_new(system->contr_sock);
2506 system->contr_gsource_id =
2507 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
2508 (GIOFunc) __process_contr_reply,
2510 g_io_channel_unref(gio);
2514 __add_application_by_interface(STC_TOTAL_DATACALL);
2515 __add_application_by_interface(STC_TOTAL_WIFI);
2516 __add_application_by_interface(STC_TOTAL_BLUETOOTH);
2517 __add_application_by_interface(STC_TOTAL_IPV4);
2518 __add_application_by_interface(STC_TOTAL_IPV6);
2519 __add_application_by_interface(STC_TOTAL_TETHERING);
2521 /* creating restriction rules tree */
2522 __update_contr_cb(NULL);
2524 /* registering periodic kernel counters update callback */
2525 g_system->contr_timer_id = g_timeout_add_seconds(CONTR_TIMER_INTERVAL,
2528 if (g_system->contr_timer_id == 0) {
2529 STC_LOGE("Failed to register kernel counters update timer"); //LCOV_EXCL_LINE
2530 __close_contr_sock(g_system); //LCOV_EXCL_LINE
2531 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
2534 __vconf_get_int(VCONFKEY_STC_BACKGROUND_STATE,
2535 (int *)&g_system->background_state);
2537 __fill_exceptions_list();
2538 __fill_restritions_list();
2540 return STC_ERROR_NONE;
2543 stc_error_e stc_monitor_deinit(void)
2545 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2547 /* close netlink socket for updating kernel counters */
2548 __close_contr_sock(g_system);
2550 /* remove kernel counters update timer */
2551 if (g_system->contr_timer_id > 0) {
2552 g_source_remove(g_system->contr_timer_id);
2553 g_system->contr_timer_id = 0;
2556 /* destroy monitored application tree */
2557 g_tree_destroy(g_system->apps);
2558 g_system->apps = NULL;
2560 /* destroy restriction rules tree */
2561 g_tree_destroy(g_system->rstns);
2562 g_system->rstns = NULL;
2566 return STC_ERROR_NONE;
2569 API stc_error_e stc_monitor_application_add(const stc_app_key_s app_key,
2570 const stc_app_value_s app_value)
2572 stc_error_e ret = STC_ERROR_NONE;
2574 stc_app_value_s *value;
2575 stc_app_value_s *lookup;
2577 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2579 lookup = __application_lookup(g_system->apps, &app_key);
2581 return STC_ERROR_NONE; //LCOV_EXCL_LINE
2583 key = MALLOC0(stc_app_key_s, 1);
2585 STC_LOGE("key allocation failed"); //LCOV_EXCL_LINE
2586 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
2589 value = MALLOC0(stc_app_value_s, 1);
2591 STC_LOGE("value allocation failed"); //LCOV_EXCL_LINE
2592 FREE(key); //LCOV_EXCL_LINE
2593 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
2596 key->app_id = g_strdup(app_key.app_id);
2597 key->pkg_id = g_strdup(app_key.pkg_id);
2599 value->type = app_value.type;
2600 value->data_usage.in_bytes = app_value.data_usage.in_bytes;
2601 value->data_usage.out_bytes = app_value.data_usage.out_bytes;
2602 g_strlcpy(value->mac, app_value.mac, MAC_ADDRESS_LEN);
2604 value->processes = g_tree_new_full(__processes_tree_key_compare, NULL,
2605 __processes_tree_key_free,
2606 __processes_tree_value_free);
2608 /* create cgroup and update classid */
2609 value->classid = get_classid_by_app_id(app_key.app_id, TRUE);
2611 /* update classid for tethering station based on its mac address */
2612 if (g_str_has_suffix(app_key.app_id, STC_TETHERING_APP_SUFFIX) &&
2613 value->classid != STC_TETHERING_APP_CLASSID)
2614 stc_plugin_tether_set_station_classid(value->mac, value->classid);
2616 g_tree_insert(g_system->apps, key, value);
2618 /* add nfacct rule for this classid */
2619 __add_application_monitor(key, value, stc_get_default_connection());
2620 __add_rstns_for_application(app_key.app_id);
2625 API stc_error_e stc_monitor_application_remove(const stc_app_key_s app_key)
2627 stc_error_e ret = STC_ERROR_NONE;
2628 stc_app_value_s *app_lookup;
2630 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2632 app_lookup = __application_lookup(g_system->apps, &app_key);
2635 STC_LOGD("app_key not found"); //LCOV_EXCL_LINE
2636 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
2639 /* remove nfacct rule for this classid */
2640 __remove_application_monitor((gpointer) &app_key, app_lookup,
2641 stc_get_default_connection());
2643 /* remove ristrictions if any */
2644 __remove_rstns_for_application(app_key.app_id);
2646 /* remove app_key from the stc-manager */
2647 if (!g_tree_remove(g_system->apps, &app_key)) {
2648 ret = STC_ERROR_NO_DATA;
2649 STC_LOGE("key not found");
2655 API stc_error_e stc_monitor_process_add(const stc_app_key_s app_key,
2656 const stc_process_key_s proc_key,
2657 const stc_process_value_s proc_value)
2659 stc_error_e ret = STC_ERROR_NONE;
2660 stc_app_value_s *app_lookup;
2661 stc_process_key_s *key;
2662 stc_process_value_s *value;
2663 stc_process_value_s *proc_lookup;
2665 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2667 app_lookup = __application_lookup(g_system->apps, &app_key);
2670 STC_LOGD("app_key not found"); //LCOV_EXCL_LINE
2671 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
2674 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
2676 return STC_ERROR_NONE; //LCOV_EXCL_LINE
2678 key = MALLOC0(stc_process_key_s, 1);
2680 STC_LOGE("key allocation failed"); //LCOV_EXCL_LINE
2681 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
2684 value = MALLOC0(stc_process_value_s, 1);
2686 STC_LOGE("value allocation failed"); //LCOV_EXCL_LINE
2687 FREE(key); //LCOV_EXCL_LINE
2688 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
2691 key->pid = proc_key.pid;
2693 value->ground = proc_value.ground;
2695 g_tree_insert(app_lookup->processes, key, value);
2697 /* add pid to application cgroup */
2698 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
2701 __apps_tree_printall(); //LCOV_EXCL_LINE
2707 API stc_error_e stc_monitor_process_remove(pid_t pid)
2709 stc_error_e ret = STC_ERROR_NONE;
2710 stc_process_key_s proc_key = {
2714 remove_pid_context_s context = {
2716 .proc_key = &proc_key,
2717 .entry_removed = FALSE,
2720 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2722 g_tree_foreach(g_system->apps, __apps_tree_foreach_remove_pid,
2725 if (context.entry_removed)
2726 __application_remove_if_empty(context.app_key);
2729 __apps_tree_printall(); //LCOV_EXCL_LINE
2736 API stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key,
2737 const stc_process_key_s proc_key,
2738 stc_app_state_e ground)
2740 stc_error_e ret = STC_ERROR_NONE;
2741 stc_app_value_s *app_lookup;
2742 stc_process_value_s *proc_lookup;
2744 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2746 app_lookup = __application_lookup(g_system->apps, &app_key);
2748 STC_LOGD("app_key not found");
2749 return STC_ERROR_FAIL;
2752 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
2754 STC_LOGD("proc_key not found");
2755 return STC_ERROR_FAIL;
2758 if (proc_lookup->ground != ground)
2759 proc_lookup->ground = ground;
2761 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
2767 void stc_monitor_update_rstn_by_default_connection(void *data)
2769 static default_connection_s old_connection;
2770 default_connection_s *new_connection = (default_connection_s *)data;
2772 if (old_connection.path != NULL) {
2775 g_tree_foreach(g_system->apps,
2776 __remove_application_monitor,
2777 (gpointer)&old_connection);
2779 if (g_system->rstns)
2780 g_tree_foreach(g_system->rstns,
2781 __remove_restriction,
2782 (gpointer)&old_connection);
2784 iptables_flush_chains();
2788 FREE(old_connection.path);
2789 FREE(old_connection.ifname);
2790 FREE(old_connection.tether_iface.ifname);
2791 old_connection.type = 0;
2792 old_connection.roaming = 0;
2793 old_connection.tether_state = FALSE;
2794 old_connection.tether_iface.type = 0;
2796 if (new_connection != NULL && new_connection->path != NULL) {
2798 g_tree_foreach(g_system->apps,
2799 __add_application_monitor,
2800 (gpointer)new_connection);
2802 if (g_system->rstns)
2803 g_tree_foreach(g_system->rstns, __add_restriction,
2806 old_connection.path = g_strdup(new_connection->path);
2807 old_connection.ifname = g_strdup(new_connection->ifname);
2808 old_connection.tether_iface.ifname = g_strdup(new_connection->tether_iface.ifname);
2809 old_connection.type = new_connection->type;
2810 old_connection.roaming = new_connection->roaming;
2811 old_connection.tether_state = new_connection->tether_state;
2812 old_connection.tether_iface.type = new_connection->tether_iface.type;
2816 stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info)
2821 stc_rstn_value_s value;
2823 memset(&key, 0, sizeof(stc_rstn_key_s));
2824 memset(&value, 0, sizeof(stc_rstn_value_s));
2826 key.app_id = g_strdup(info->app_id);
2827 key.ifname = g_strdup(info->ifname);
2828 key.mac = g_strdup(info->mac);
2829 key.subscriber_id = g_strdup(info->subscriber_id);
2830 key.iftype = info->iftype;
2831 key.roaming = info->roaming;
2833 value.rstn_type = info->rstn_type;
2834 value.rstn_state = STC_RSTN_STATE_UNKNOWN;
2835 value.restriction_id = info->restriction_id;
2838 value.classid = get_classid_by_app_id(info->app_id, TRUE);
2840 value.classid = STC_UNKNOWN_CLASSID;
2842 if (value.classid == STC_BACKGROUND_APP_CLASSID) {
2843 g_system->background_state = TRUE; //LCOV_EXCL_LINE
2844 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, TRUE); //LCOV_EXCL_LINE
2845 __process_update_background(); //LCOV_EXCL_LINE
2848 value.limit[STC_RSTN_LIMIT_TYPE_DATA] = info->data_limit;
2849 value.limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info->data_warn_limit;
2850 value.limit[STC_RSTN_LIMIT_TYPE_MONTHLY] = info->monthly_limit;
2851 value.limit[STC_RSTN_LIMIT_TYPE_WEEKLY] = info->weekly_limit;
2852 value.limit[STC_RSTN_LIMIT_TYPE_DAILY] = info->daily_limit;
2853 value.month_start_date = info->month_start_date;
2854 value.month_start_ts = stc_time_get_month_start(time(NULL),
2855 info->month_start_date);
2857 ret = __rstn_tree_add(&key, &value, TRUE);
2862 FREE(key.subscriber_id);
2866 stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info)
2870 stc_rstn_key_s key = {
2871 .app_id = g_strdup(info->app_id),
2872 .ifname = g_strdup(info->ifname),
2873 .subscriber_id = g_strdup(info->subscriber_id),
2874 .iftype = info->iftype,
2875 .roaming = info->roaming,
2878 if (!strcmp(key.app_id, STC_BACKGROUND_APP_ID)) {
2879 g_system->background_state = FALSE; //LCOV_EXCL_LINE
2880 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, FALSE); //LCOV_EXCL_LINE
2881 __process_update_background(); //LCOV_EXCL_LINE
2884 ret = __rstn_tree_remove(&key);
2888 FREE(key.subscriber_id);
2892 API stc_error_e stc_monitor_check_excn_by_cmdline(char *cmdline)
2894 return stc_plugin_check_exception_by_cmdline(cmdline);
2897 int stc_monitor_get_counter_socket(void)
2899 return g_system->contr_sock;