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 counter.carg = stc->carg;
788 counter.classid = classid;
789 counter.intend = intend;
791 if (connection && connection->tether_iface.ifname != NULL &&
792 classid == STC_TETHERING_APP_CLASSID) {
793 counter.iftype = connection->tether_iface.type;
794 g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
796 counter.iftype = iftype;
797 g_strlcpy(counter.ifname, default_ifname, MAX_IFACE_LENGTH);
800 g_free(default_ifname);
803 __add_iptables_in(&counter);
804 __add_iptables_out(&counter);
807 __add_ip6tables_in(&counter);
808 __add_ip6tables_out(&counter);
811 static void __add_tethering_iptables_rule(int64_t classid, gchar *mac,
812 nfacct_rule_intend intend, stc_iface_type_e iftype)
814 default_connection_s *connection = stc_get_default_connection();
815 struct nfacct_rule counter;
816 stc_s *stc = stc_get_manager();
824 stc->carg = MALLOC0(counter_arg_s, 1);
825 if (stc->carg == NULL)
828 stc->carg->sock = stc_monitor_get_counter_socket();
831 memset(&counter, 0, sizeof(struct nfacct_rule));
833 counter.carg = stc->carg;
834 counter.classid = classid;
835 counter.intend = intend;
837 if (connection->tether_state != TRUE ||
838 connection->tether_iface.ifname == NULL)
841 counter.iftype = connection->tether_iface.type;
842 g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
844 /* get connected station ip based on its mac */
845 ret = stc_plugin_tether_get_station_ip(mac, &ipaddr);
846 if (ret != STC_ERROR_NONE)
849 /* tethering iptables rule */
850 __add_iptables_tether_in(&counter, ipaddr);
851 __add_iptables_tether_out(&counter, ipaddr);
855 static void __del_tethering_iptables_rule(int64_t classid, gchar *mac,
856 nfacct_rule_intend intend, stc_iface_type_e iftype)
858 default_connection_s *connection = stc_get_default_connection();
859 struct nfacct_rule counter;
860 stc_s *stc = stc_get_manager();
868 stc->carg = MALLOC0(counter_arg_s, 1);
869 if (stc->carg == NULL)
872 stc->carg->sock = stc_monitor_get_counter_socket();
875 memset(&counter, 0, sizeof(struct nfacct_rule));
877 counter.carg = stc->carg;
878 counter.classid = classid;
879 counter.intend = intend;
881 if (connection->tether_state != TRUE ||
882 connection->tether_iface.ifname == NULL)
885 counter.iftype = connection->tether_iface.type;
886 g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
888 /* get connected station ip based on its mac */
889 ret = stc_plugin_tether_get_station_ip(mac, &ipaddr);
890 if (ret != STC_ERROR_NONE) {
891 STC_LOGE("Error: no IP found for station mac(%s)", mac);
895 /* tethering iptables rule */
896 __del_iptables_tether_in(&counter, ipaddr);
897 __del_iptables_tether_out(&counter, ipaddr);
901 static void __del_iptables_rule(int64_t classid, nfacct_rule_intend intend,
902 stc_iface_type_e iftype)
904 char *default_ifname = stc_default_connection_get_ifname();
905 default_connection_s *connection = stc_get_default_connection();
906 struct nfacct_rule counter;
907 stc_s *stc = stc_get_manager();
909 g_free(default_ifname); //LCOV_EXCL_LINE
910 return; //LCOV_EXCL_LINE
914 stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
915 if (stc->carg == NULL) { //LCOV_EXCL_LINE
916 g_free(default_ifname); //LCOV_EXCL_LINE
917 return; //LCOV_EXCL_LINE
920 stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
923 counter.carg = stc->carg;
924 counter.classid = classid;
925 counter.intend = intend;
927 if (connection && connection->tether_iface.ifname != NULL &&
928 classid == STC_TETHERING_APP_CLASSID) {
929 counter.iftype = connection->tether_iface.type;
930 g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
932 counter.iftype = iftype;
933 g_strlcpy(counter.ifname, default_ifname, MAX_IFACE_LENGTH);
936 g_free(default_ifname);
939 __del_iptables_in(&counter);
940 __del_iptables_out(&counter);
943 __del_ip6tables_in(&counter);
944 __del_ip6tables_out(&counter);
947 static void __set_rstn_noti_state(int value)
949 int state = STC_RSTN_STATE_INIT;
951 if (__vconf_get_int(VCONFKEY_SETAPPL_DATA_RESTRICTION_INT, &state))
954 if (state == value) {
955 STC_LOGI("No need to change a restriction status: %d", state);
959 vconf_set_int(VCONFKEY_SETAPPL_DATA_RESTRICTION_INT, value);
964 time_t month_start_ts;
965 time_t week_start_ts;
967 int64_t monthly_stat;
972 static stc_cb_ret_e __statistics_info_cb(const table_statistics_info *info,
975 cumulative_data_s *stat = (cumulative_data_s *)user_data;
976 int64_t counters = 0;
978 counters = info->cnt.in_bytes + info->cnt.out_bytes;
980 stat->monthly_stat += counters;
981 if (stat->week_start_ts <= info->interval->from)
982 stat->weekly_stat += counters;
983 if (stat->day_start_ts <= info->interval->from)
984 stat->daily_stat += counters;
989 static void __process_tethering_restriction(enum traffic_restriction_type rstn_type,
990 stc_rstn_key_s *rstn_key, stc_rstn_value_s *rstn_value, void *data)
992 default_connection_s *old_connection = (default_connection_s *)data;
993 default_connection_s *connection = NULL;
994 char *mac_str = NULL;
996 if (old_connection != NULL)
997 connection = old_connection;
999 connection = stc_get_default_connection();
1001 /* in case tethering is not active */
1002 if (connection->tether_state == FALSE)
1005 /* rstn not applicable for this interface */
1006 if (rstn_key->ifname != NULL && g_strcmp0("", rstn_key->ifname) != 0 &&
1007 (g_strcmp0(connection->tether_iface.ifname, rstn_key->ifname) != 0))
1010 /* in case appid not a tethering app */
1011 if (!g_str_has_suffix(rstn_key->app_id, STC_TETHERING_APP_SUFFIX))
1014 /* Ignore TOTAL_TETHERING,
1015 * Process only station appids */
1016 if (rstn_value->classid == STC_TETHERING_APP_CLASSID)
1019 /* get the station mac based on classid */
1020 stc_plugin_tether_get_station_by_classid(rstn_value->classid, &mac_str);
1022 STC_LOGE("station not found for classid(%d)", rstn_value->classid);
1026 switch (rstn_type) {
1030 table_counters_info info;
1031 int64_t effective_limit[STC_RSTN_LIMIT_TYPE_MAX] = { 0, };
1033 memset(&info, 0, sizeof(table_counters_info));
1034 rstn_value->limit_exceeded = 0;
1036 if ((rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] == 0 &&
1037 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA] >= 0) ||
1038 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] == 0 &&
1039 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] >= 0) ||
1040 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] == 0 &&
1041 rstn_value->limit[STC_RSTN_LIMIT_TYPE_MONTHLY] >= 0) ||
1042 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] == 0 &&
1043 rstn_value->limit[STC_RSTN_LIMIT_TYPE_WEEKLY] >= 0) ||
1044 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] == 0 &&
1045 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DAILY] >= 0)) {
1046 table_counters_get(rstn_value->restriction_id, &info);
1048 time_t current_time = 0;
1049 cumulative_data_s stat;
1050 table_statistics_select_rule rule;
1052 memset(&stat, 0, sizeof(cumulative_data_s));
1053 stat.month_start_ts = rstn_value->month_start_ts;
1054 stat.week_start_ts = g_system->last_week_ts;
1055 stat.day_start_ts = g_system->last_day_ts;
1057 memset(&rule, 0, sizeof(table_statistics_select_rule));
1058 rule.from = rstn_value->month_start_ts;
1059 time(¤t_time);
1060 rule.to = current_time;
1061 rule.iftype = rstn_key->iftype;
1062 rule.granularity = GRANULARITY;
1064 table_statistics_per_app(rstn_key->app_id, &rule, __statistics_info_cb, &stat);
1066 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] = info.data_counter;
1067 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info.warn_counter;
1068 rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] = info.monthly_counter + stat.monthly_stat;
1069 rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] = info.weekly_counter + stat.weekly_stat;
1070 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] = info.daily_counter + stat.daily_stat;
1073 for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
1074 if (rstn_value->limit[i] >= 0) {
1075 effective_limit[i] = rstn_value->limit[i] - rstn_value->counter[i];
1077 if (effective_limit[i] < 0)
1078 rstn_value->limit_exceeded |= (1 << i);
1082 STC_LOGD("rstn_id [%llu], datausage [%llu] bytes",
1083 rstn_value->restriction_id, info.data_counter);
1085 if (rstn_value->limit_exceeded != 0 &&
1086 rstn_value->limit_exceeded != (1 << STC_RSTN_LIMIT_TYPE_DATA_WARN)) {
1087 __add_tethering_iptables_rule(rstn_value->classid, mac_str,
1088 NFACCT_TETH_BLOCK, rstn_key->iftype);
1091 rstn_value->rstn_state = STC_RSTN_STATE_ACTIVATED;
1096 __add_tethering_iptables_rule(rstn_value->classid, mac_str,
1097 NFACCT_TETH_ALLOW, rstn_key->iftype);
1099 rstn_value->rstn_state = STC_RSTN_STATE_ACTIVATED;
1100 rstn_value->limit_exceeded = 0;
1101 rstn_value->limit_notified = 0;
1107 __del_tethering_iptables_rule(rstn_value->classid, mac_str,
1108 NFACCT_TETH_BLOCK, rstn_key->iftype);
1110 rstn_value->rstn_state = STC_RSTN_STATE_DEACTIVATED;
1111 rstn_value->limit_exceeded = 0;
1112 rstn_value->limit_notified = 0;
1114 for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++)
1115 if (rstn_value->limit[i] >= 0)
1116 rstn_value->counter[i] = 0;
1125 static void __process_restriction(enum traffic_restriction_type rstn_type,
1126 stc_rstn_key_s *rstn_key,
1127 stc_rstn_value_s *rstn_value, void *data)
1129 default_connection_s *old_connection = (default_connection_s *)data;
1130 default_connection_s *connection = NULL;
1132 if (old_connection != NULL)
1133 connection = old_connection;
1135 connection = stc_get_default_connection();
1137 /* no default ifname */
1138 if (connection->ifname == NULL)
1141 /* rstn not applicable for this interface */
1142 if (rstn_key->ifname != NULL && g_strcmp0("", rstn_key->ifname) != 0 &&
1143 (g_strcmp0(connection->ifname, rstn_key->ifname) != 0) &&
1144 (g_strcmp0(connection->tether_iface.ifname, rstn_key->ifname) != 0))
1147 /* classid is invalid */
1148 if (rstn_value->classid <= STC_UNKNOWN_CLASSID)
1151 /* Do not proceed for tethering station appid if found here,
1152 * for tethering station apps __process_tethering_restriction() call
1154 if (g_str_has_suffix(rstn_key->app_id, STC_TETHERING_APP_SUFFIX) &&
1155 rstn_value->classid != STC_TETHERING_APP_CLASSID)
1158 switch (rstn_type) {
1162 table_counters_info info;
1163 int64_t effective_limit[STC_RSTN_LIMIT_TYPE_MAX] = { 0, };
1165 memset(&info, 0, sizeof(table_counters_info));
1166 rstn_value->limit_exceeded = 0;
1168 if ((rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] == 0 &&
1169 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA] >= 0) ||
1170 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] == 0 &&
1171 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] >= 0) ||
1172 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] == 0 &&
1173 rstn_value->limit[STC_RSTN_LIMIT_TYPE_MONTHLY] >= 0) ||
1174 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] == 0 &&
1175 rstn_value->limit[STC_RSTN_LIMIT_TYPE_WEEKLY] >= 0) ||
1176 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] == 0 &&
1177 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DAILY] >= 0)) {
1178 table_counters_get(rstn_value->restriction_id, &info);
1180 time_t current_time = 0;
1181 cumulative_data_s stat;
1182 table_statistics_select_rule rule;
1184 memset(&stat, 0, sizeof(cumulative_data_s));
1185 stat.month_start_ts = rstn_value->month_start_ts;
1186 stat.week_start_ts = g_system->last_week_ts;
1187 stat.day_start_ts = g_system->last_day_ts;
1189 memset(&rule, 0, sizeof(table_statistics_select_rule));
1190 rule.from = rstn_value->month_start_ts;
1191 time(¤t_time);
1192 rule.to = current_time;
1193 rule.iftype = rstn_key->iftype;
1194 rule.granularity = GRANULARITY;
1196 table_statistics_per_app(rstn_key->app_id, &rule, __statistics_info_cb, &stat);
1198 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] = info.data_counter;
1199 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info.warn_counter;
1200 rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] = info.monthly_counter + stat.monthly_stat;
1201 rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] = info.weekly_counter + stat.weekly_stat;
1202 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] = info.daily_counter + stat.daily_stat;
1205 for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
1206 if (rstn_value->limit[i] >= 0) {
1207 effective_limit[i] = rstn_value->limit[i] - rstn_value->counter[i];
1209 if (effective_limit[i] < 0)
1210 rstn_value->limit_exceeded |= (1 << i);
1214 STC_LOGD("rstn_id [%llu], datausage [%llu] bytes",
1215 rstn_value->restriction_id, info.data_counter);
1217 if (rstn_value->limit_exceeded != 0 &&
1218 rstn_value->limit_exceeded != (1 << STC_RSTN_LIMIT_TYPE_DATA_WARN)) {
1219 __add_iptables_rule(rstn_value->classid, NFACCT_BLOCK, rstn_key->iftype);
1222 rstn_value->rstn_state = STC_RSTN_STATE_ACTIVATED;
1226 __add_iptables_rule(rstn_value->classid, NFACCT_ALLOW,
1229 rstn_value->rstn_state = STC_RSTN_STATE_ACTIVATED;
1230 rstn_value->limit_exceeded = 0;
1231 rstn_value->limit_notified = 0;
1237 if (rstn_value->classid == STC_TETHERING_APP_CLASSID)
1238 __del_iptables_rule(rstn_value->classid, NFACCT_BLOCK,
1241 __del_iptables_rule(rstn_value->classid, rstn_value->rstn_type,
1244 rstn_value->rstn_state = STC_RSTN_STATE_DEACTIVATED;
1245 rstn_value->limit_exceeded = 0;
1246 rstn_value->limit_notified = 0;
1248 for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++)
1249 if (rstn_value->limit[i] >= 0)
1250 rstn_value->counter[i] = 0;
1252 __set_rstn_noti_state(STC_RSTN_STATE_UNSET);
1261 static gboolean __remove_rstns_foreach_application(gpointer key,
1265 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1266 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1267 gchar *app_id = (gchar *)data;
1269 /* rstn rule is not for applications */
1270 if (rstn_key->app_id == NULL)
1273 /* rstn rule is not for this application */
1274 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
1277 /* rstn rule is already removed */
1278 if (rstn_value->rstn_state == STC_RSTN_STATE_DEACTIVATED)
1281 /* remove restriction from system */
1282 __process_restriction(RST_UNSET, rstn_key, rstn_value, NULL);
1284 /* remove tethering restriction from system*/
1285 __process_tethering_restriction(RST_UNSET, rstn_key, rstn_value, NULL);
1287 __print_rstn(rstn_key, rstn_value);
1293 static void __remove_rstns_for_application(gchar *app_id)
1295 g_tree_foreach(g_system->rstns, __remove_rstns_foreach_application,
1299 static stc_error_e __application_remove_if_empty(const stc_app_key_s *app_key)
1301 stc_error_e ret = STC_ERROR_NONE;
1302 guint pid_count = 0;
1303 stc_app_value_s *lookup;
1305 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1307 lookup = __application_lookup(g_system->apps, app_key);
1309 STC_LOGE("app_key not found"); //LCOV_EXCL_LINE
1310 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
1313 g_tree_foreach(lookup->processes, __processes_tree_check_empty,
1317 /* remove nfacct rule for this classid */
1318 __remove_application_monitor((gpointer) app_key, lookup,
1319 stc_get_default_connection());
1320 __remove_rstns_for_application(app_key->app_id);
1323 if (!g_tree_remove(g_system->apps, app_key)) {
1324 ret = STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
1325 STC_LOGE("key not found"); //LCOV_EXCL_LINE
1331 static stc_error_e __close_contr_sock(stc_system_s *system)
1333 ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
1335 /* close netlink socket for updating kernel counters */
1336 if (system->contr_sock != -1) {
1337 close(system->contr_sock);
1338 system->contr_sock = -1;
1341 if (system->contr_gsource_id != 0) {
1342 g_source_remove(system->contr_gsource_id);
1343 system->contr_gsource_id = 0;
1346 return STC_ERROR_NONE;
1349 static gboolean __process_contr_reply(GIOChannel *source,
1350 GIOCondition condition,
1351 gpointer user_data);
1354 static stc_error_e __close_and_reopen_contr_sock(stc_system_s *system)
1356 GIOChannel *gio = NULL;
1357 ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
1359 /* close netlink socket for updating kernel counters */
1360 if (system->contr_sock != -1) {
1361 close(system->contr_sock);
1362 system->contr_sock = -1;
1365 if (system->contr_gsource_id != 0) {
1366 g_source_remove(system->contr_gsource_id);
1367 system->contr_gsource_id = 0;
1370 /* create netlink socket for updating kernel counters */
1371 system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
1372 if (system->contr_sock < 0) {
1373 STC_LOGE("failed to open socket");
1375 return STC_ERROR_FAIL;
1378 gio = g_io_channel_unix_new(system->contr_sock);
1379 system->contr_gsource_id =
1380 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
1381 (GIOFunc) __process_contr_reply,
1383 g_io_channel_unref(gio);
1385 return STC_ERROR_NONE;
1388 static void __action_when_rstn_limit_exceeded_tethering(stc_rstn_key_s *rstn_key,
1389 stc_rstn_value_s *rstn_value, classid_bytes_context_s *context)
1391 char *mac_str = NULL;
1392 struct nfacct_rule *counter = context->counter;
1394 /* get the station mac based on classid */
1395 stc_plugin_tether_get_station_by_classid(counter->classid, &mac_str);
1397 STC_LOGE("station not found for classid(%d)", counter->classid);
1401 STC_LOGI("station mac %s, classid %u, iftype %u, iotype %d, \
1402 intend %d, ifname %s, bytes %lld", mac_str,
1403 counter->classid, counter->iftype, counter->iotype,
1404 counter->intend, counter->ifname, context->bytes);
1406 /* Block tethering station immediately */
1407 counter->intend = NFACCT_TETH_BLOCK;
1408 __del_tethering_iptables_rule(counter->classid, mac_str,
1409 NFACCT_TETH_BLOCK, rstn_key->iftype);
1411 __add_tethering_iptables_rule(counter->classid, mac_str,
1412 NFACCT_TETH_BLOCK, rstn_key->iftype);
1413 counter->intend = NFACCT_TETH_COUNTER;
1418 static void __action_when_rstn_limit_exceeded(stc_rstn_limit_type_e limit_type,
1419 stc_rstn_key_s *rstn_key,
1420 stc_rstn_value_s *rstn_value,
1421 classid_bytes_context_s *context)
1424 char iftype[MAX_INT_LENGTH] = { 0, };
1425 char byte[MAX_INT_LENGTH] = { 0, };
1426 const char *signal_name = NULL;
1427 const char *net_popup_content = NULL;
1428 const char *net_popup_type = NULL;
1429 stc_s *stc = (stc_s *)stc_get_manager();
1432 STC_LOGE("Failed to get stc data");
1436 switch (limit_type) {
1437 case STC_RSTN_LIMIT_TYPE_DATA_WARN:
1439 signal_name = "WarnThresholdCrossed";
1440 net_popup_content = "warn threshold crossed";
1441 net_popup_type = "warning_noti";
1444 case STC_RSTN_LIMIT_TYPE_DATA:
1445 case STC_RSTN_LIMIT_TYPE_MONTHLY:
1446 case STC_RSTN_LIMIT_TYPE_WEEKLY:
1447 case STC_RSTN_LIMIT_TYPE_DAILY:
1449 signal_name = "RestrictionThresholdCrossed";
1450 net_popup_content = "restriction threshold crossed";
1451 net_popup_type = "restriction_noti";
1453 /* Apply restriction for tethering apps if app_id is of tethering client
1454 * otherwise do the normal iptables rule */
1455 if (context->counter->intend == NFACCT_TETH_COUNTER) {
1457 if (g_str_has_suffix(rstn_key->app_id, STC_TETHERING_APP_SUFFIX) &&
1458 rstn_value->classid != STC_TETHERING_APP_CLASSID) {
1459 __action_when_rstn_limit_exceeded_tethering(rstn_key, rstn_value,
1464 /* block immediately */
1465 context->counter->intend = NFACCT_BLOCK;
1466 __del_iptables_in(context->counter);
1467 __del_iptables_out(context->counter);
1468 __add_iptables_in(context->counter);
1469 __add_iptables_out(context->counter);
1471 __del_ip6tables_in(context->counter);
1472 __del_ip6tables_out(context->counter);
1473 __add_ip6tables_in(context->counter);
1474 __add_ip6tables_out(context->counter);
1475 context->counter->intend = NFACCT_COUNTER;
1478 rstn_value->limit_exceeded |= (1 << limit_type);
1480 __set_rstn_noti_state(STC_RSTN_STATE_SET);
1487 if (signal_name == NULL) {
1488 STC_LOGE("Invalid parameter: limit_type");
1493 rv = stc_manager_dbus_emit_signal(stc->connection,
1494 STC_DBUS_SERVICE_RESTRICTION_PATH,
1495 STC_DBUS_INTERFACE_RESTRICTION,
1497 g_variant_new("(si)",
1502 rstn_value->limit_notified |= (1 << limit_type);
1504 snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
1505 snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->limit[limit_type]);
1506 stc_plugin_appstatus_send_message(net_popup_content,
1507 net_popup_type, rstn_key->app_id, iftype, byte);
1510 static gboolean __rstn_counter_update(stc_rstn_key_s *rstn_key,
1511 stc_rstn_value_s *rstn_value,
1512 classid_bytes_context_s *context)
1515 switch (context->counter->iotype) {
1516 case NFACCT_COUNTER_IN:
1517 case NFACCT_COUNTER_OUT:
1518 if ((rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] == 0 &&
1519 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA] >= 0) ||
1520 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] == 0 &&
1521 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] >= 0) ||
1522 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] == 0 &&
1523 rstn_value->limit[STC_RSTN_LIMIT_TYPE_MONTHLY] >= 0) ||
1524 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] == 0 &&
1525 rstn_value->limit[STC_RSTN_LIMIT_TYPE_WEEKLY] >= 0) ||
1526 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] == 0 &&
1527 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DAILY] >= 0)) {
1528 table_counters_info info;
1529 memset(&info, 0, sizeof(table_counters_info));
1530 table_counters_get(rstn_value->restriction_id, &info);
1532 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] = info.data_counter;
1533 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info.warn_counter;
1534 rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] = info.monthly_counter;
1535 rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] = info.weekly_counter;
1536 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] = info.daily_counter;
1539 for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
1540 if (rstn_value->limit[i] >= 0 &&
1541 !(rstn_value->limit_notified & (1 << i))) {
1542 rstn_value->counter[i] += context->bytes;
1543 if (rstn_value->limit[i] <= rstn_value->counter[i])
1544 __action_when_rstn_limit_exceeded(i,
1551 g_system->rstns_tree_updated = TRUE;
1552 __print_rstn(rstn_key, rstn_value);
1555 STC_LOGE("unknown iotype");
1561 static gboolean __interface_rstn_counter_update(stc_rstn_key_s *rstn_key,
1562 stc_rstn_value_s *rstn_value,
1563 classid_bytes_context_s *context)
1565 if ((rstn_value->classid == STC_TOTAL_DATACALL_CLASSID &&
1566 context->counter->iftype == STC_IFACE_DATACALL) ||
1567 (rstn_value->classid == STC_TOTAL_WIFI_CLASSID &&
1568 context->counter->iftype == STC_IFACE_WIFI) ||
1569 (rstn_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
1570 context->counter->iftype == STC_IFACE_BLUETOOTH) ||
1571 (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
1572 context->counter->iftype == STC_IFACE_WIFI) ||
1573 (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
1574 context->counter->iftype == STC_IFACE_BLUETOOTH) ||
1575 (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
1576 context->counter->iftype == STC_IFACE_USB) ||
1577 (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
1578 context->counter->iftype == STC_IFACE_P2P)) {
1579 context->counter->classid = rstn_value->classid;
1580 return __rstn_counter_update(rstn_key, rstn_value, context);
1586 static gboolean __rstn_counter_update_foreach_classid(gpointer key,
1590 gboolean rv = FALSE;
1591 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1592 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1593 classid_bytes_context_s *context = (classid_bytes_context_s *)data;
1596 if (context->counter->intend != NFACCT_COUNTER &&
1597 context->counter->intend != NFACCT_TETH_COUNTER)
1598 goto try_next_callback;
1600 if (rstn_value->limit_exceeded == TRUE) {
1601 context->data_limit_exceeded = TRUE; //LCOV_EXCL_LINE
1602 goto try_next_callback; //LCOV_EXCL_LINE
1605 classid = context->counter->classid;
1606 rv = __interface_rstn_counter_update(rstn_key, rstn_value, context);
1608 context->counter->classid = classid;
1609 if (rstn_value->classid != context->counter->classid)
1610 goto try_next_callback;
1612 rv = __rstn_counter_update(rstn_key, rstn_value, context);
1619 static gboolean __update_app_statistics(gpointer key, gpointer value,
1622 stc_app_key_s *app_key = (stc_app_key_s *)key;
1623 stc_app_value_s *app_value = (stc_app_value_s *)value;
1624 time_t *touch_time = (time_t *)data;
1625 stc_db_classid_iftype_key stat_key;
1626 stc_db_app_stats stat;
1627 default_connection_s *default_connection = stc_get_default_connection();
1629 memset(&stat_key, 0, sizeof(stc_db_classid_iftype_key));
1630 memset(&stat, 0 , sizeof(stc_db_app_stats));
1632 /* Do not update statistics for Tethering
1633 * if tethering is in-active found */
1634 if (default_connection &&
1635 default_connection->tether_state == FALSE &&
1636 !strcmp(app_key->app_id, STC_TOTAL_TETHERING))
1639 /* Do not update statistics for Wi-Fi
1640 * if tethering is active on wlan0 iface */
1641 if (default_connection && default_connection->tether_state &&
1642 default_connection->tether_iface.type == STC_IFACE_WIFI &&
1643 !strcmp(app_key->app_id, STC_TOTAL_WIFI))
1646 stat_key.classid = app_value->classid;
1648 if (app_value->classid == STC_TETHERING_APP_CLASSID &&
1649 default_connection->tether_state == TRUE)
1650 stat_key.iftype = default_connection->tether_iface.type;
1651 else if (g_str_has_suffix(app_key->app_id, STC_TETHERING_APP_SUFFIX))
1652 stat_key.iftype = default_connection->tether_iface.type;
1654 stat_key.iftype = default_connection->type;
1656 if (STC_IFACE_DATACALL == stat_key.iftype)
1657 stat_key.subscriber_id = g_strdup(default_connection->subscriber_id);
1659 stat_key.subscriber_id = g_strdup("none_subid"); //LCOV_EXCL_LINE
1661 if (app_value->classid == STC_TETHERING_APP_CLASSID &&
1662 default_connection->tether_state == TRUE)
1663 g_strlcpy(stat_key.ifname, default_connection->tether_iface.ifname,
1665 else if (g_str_has_suffix(app_key->app_id, STC_TETHERING_APP_SUFFIX))
1666 g_strlcpy(stat_key.ifname, default_connection->tether_iface.ifname,
1669 g_strlcpy(stat_key.ifname, default_connection->ifname,
1672 stat.app_id = g_strdup(app_key->app_id);
1673 stat.snd_count = app_value->counter.out_bytes;
1674 stat.rcv_count = app_value->counter.in_bytes;
1675 stat.is_roaming = default_connection->roaming;
1677 if (strstr(stat.app_id, "_BACKGROUND")) {
1678 stat.ground = STC_APP_STATE_BACKGROUND;
1680 if (strstr(stat.app_id, "TOTAL_"))
1681 stat.ground = STC_APP_STATE_UNKNOWN;
1683 stat.ground = STC_APP_STATE_FOREGROUND;
1686 table_statistics_insert(&stat_key, &stat, *touch_time);
1688 app_value->counter.out_bytes = 0;
1689 app_value->counter.in_bytes = 0;
1692 FREE(stat_key.subscriber_id);
1697 static gboolean __flush_apps_stats_to_database(gpointer user_data)
1699 time_t current_time = 0;
1700 stc_s *stc = stc_get_manager();
1702 if (stc && stc->carg)
1703 current_time = stc->carg->last_run_time;
1705 if (g_system->apps_tree_updated == FALSE)
1706 return G_SOURCE_REMOVE;
1708 g_system->apps_tree_updated = FALSE;
1711 g_tree_foreach(g_system->apps,
1712 __update_app_statistics,
1715 STC_LOGI("Flushed app stats to database");
1716 return G_SOURCE_REMOVE;
1720 static gboolean __update_counter_statistics(gpointer key, gpointer value,
1723 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1724 table_counters_info info = {
1725 .restriction_id = rstn_value->restriction_id,
1726 .data_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA],
1727 .warn_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN],
1728 .monthly_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY],
1729 .weekly_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY],
1730 .daily_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY]
1733 table_counters_update_counters(&info);
1738 static gboolean __flush_rstns_counter_to_database(gpointer user_data)
1740 time_t current_time = 0;
1741 stc_s *stc = stc_get_manager();
1743 if (stc && stc->carg)
1744 current_time = stc->carg->last_run_time;
1746 if (g_system->rstns_tree_updated == FALSE)
1747 return G_SOURCE_REMOVE;
1749 g_system->rstns_tree_updated = FALSE;
1751 if (g_system->rstns)
1752 g_tree_foreach(g_system->rstns,
1753 __update_counter_statistics,
1756 STC_LOGI("Flushed rstns counters to database");
1757 return G_SOURCE_REMOVE;
1761 static void __app_counter_update(stc_app_key_s *app_key,
1762 stc_app_value_s *app_value,
1763 classid_bytes_context_s *context)
1765 switch (context->counter->iotype) {
1766 case NFACCT_COUNTER_IN:
1767 app_value->data_usage.in_bytes += context->bytes;
1768 app_value->counter.in_bytes = context->bytes;
1769 g_system->apps_tree_updated = TRUE;
1772 __apps_tree_foreach_print(app_key, app_value, NULL); //LCOV_EXCL_LINE
1775 case NFACCT_COUNTER_OUT:
1776 app_value->data_usage.out_bytes += context->bytes;
1777 app_value->counter.out_bytes = context->bytes;
1778 g_system->apps_tree_updated = TRUE;
1781 __apps_tree_foreach_print(app_key, app_value, NULL); //LCOV_EXCL_LINE
1785 STC_LOGE("unknown iotype"); //LCOV_EXCL_LINE
1789 static void __interface_counter_update(stc_app_key_s *app_key,
1790 stc_app_value_s *app_value,
1791 classid_bytes_context_s *context)
1793 if ((app_value->classid == STC_TOTAL_DATACALL_CLASSID &&
1794 context->counter->iftype == STC_IFACE_DATACALL) ||
1795 (app_value->classid == STC_TOTAL_WIFI_CLASSID &&
1796 context->counter->iftype == STC_IFACE_WIFI) ||
1797 (app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
1798 context->counter->iftype == STC_IFACE_BLUETOOTH) ||
1799 (app_value->classid == STC_TETHERING_APP_CLASSID &&
1800 context->counter->iftype == STC_IFACE_WIFI) ||
1801 (app_value->classid == STC_TETHERING_APP_CLASSID &&
1802 context->counter->iftype == STC_IFACE_BLUETOOTH) ||
1803 (app_value->classid == STC_TETHERING_APP_CLASSID &&
1804 context->counter->iftype == STC_IFACE_USB) ||
1805 (app_value->classid == STC_TETHERING_APP_CLASSID &&
1806 context->counter->iftype == STC_IFACE_P2P))
1807 __app_counter_update(app_key, app_value, context);
1811 static gboolean __apps_counter_update_foreach_classid(gpointer key,
1815 stc_app_key_s *app_key = (stc_app_key_s *)key;
1816 stc_app_value_s *app_value = (stc_app_value_s *)value;
1817 classid_bytes_context_s *context = (classid_bytes_context_s *)data;
1819 if (context->counter->intend != NFACCT_COUNTER &&
1820 context->counter->intend != NFACCT_TETH_COUNTER)
1821 goto try_next_callback;
1823 __interface_counter_update(app_key, app_value, context);
1825 if (app_value->classid != context->counter->classid)
1826 goto try_next_callback;
1828 __app_counter_update(app_key, app_value, context);
1834 static gboolean __reset_time_counter_foreach_rstn(gpointer key,
1838 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1839 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1840 reset_time_limits_context_s *context = (reset_time_limits_context_s *)data;
1842 time_t now_month_start_ts;
1844 if (rstn_value->month_start_date == 0) {
1845 table_counters_info info;
1846 memset(&info, 0, sizeof(table_counters_info));
1847 table_counters_get_timestamps(rstn_value->restriction_id, &info);
1849 if (info.month_start_date == 0)
1850 rstn_value->month_start_date = 1;
1852 rstn_value->month_start_date = info.month_start_date;
1853 rstn_value->month_start_ts = info.month_start_ts;
1856 now_month_start_ts =
1857 stc_time_get_month_start(context->now,
1858 rstn_value->month_start_date);
1860 if (rstn_value->month_start_ts != now_month_start_ts) {
1861 rstn_value->month_start_ts = now_month_start_ts;
1862 context->month_start_ts = now_month_start_ts;
1863 context->is_updated |= (1 << STC_RSTN_LIMIT_TYPE_MONTHLY);
1866 if (context->is_updated) {
1867 table_counters_info info;
1868 memset(&info, 0, sizeof(table_counters_info));
1870 info.restriction_id = rstn_value->restriction_id;
1871 info.month_start_date = rstn_value->month_start_date;
1872 info.month_start_ts = rstn_value->month_start_ts;
1873 info.week_start_ts = context->week_start_ts;
1874 info.day_start_ts = context->day_start_ts;
1876 table_counters_update_timestamps(&info);
1879 for (i = STC_RSTN_LIMIT_TYPE_MONTHLY; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
1881 if ((context->is_updated) & (1 << i)) {
1883 rstn_value->counter[i] = 0;
1885 if (rstn_value->limit_exceeded & (1 << i)) {
1886 /* remove iptables rule */
1887 char *default_ifname = stc_default_connection_get_ifname();
1888 struct nfacct_rule counter;
1889 stc_s *stc = stc_get_manager();
1891 STC_LOGE("Can't get stc data");
1892 g_free(default_ifname);
1893 goto try_next_callback;
1897 stc->carg = MALLOC0(counter_arg_s, 1);
1898 if (stc->carg == NULL) {
1899 g_free(default_ifname);
1900 goto try_next_callback;
1904 stc_monitor_get_counter_socket();
1907 counter.carg = stc->carg;
1908 counter.classid = rstn_value->classid;
1909 counter.intend = NFACCT_BLOCK;
1910 counter.iftype = rstn_key->iftype;
1911 g_strlcpy(counter.ifname, default_ifname,
1914 g_free(default_ifname);
1917 __del_iptables_in(&counter);
1918 __del_iptables_out(&counter);
1920 /* ip6tables rule */
1921 __del_ip6tables_in(&counter);
1922 __del_ip6tables_out(&counter);
1924 rstn_value->rstn_state = STC_RSTN_STATE_DEACTIVATED;
1925 rstn_value->limit_exceeded &= ~(1 << i);
1926 rstn_value->limit_notified &= ~(1 << i);
1935 static void __reset_time_counters_if_required(void)
1937 reset_time_limits_context_s context;
1939 if (g_system == NULL) {
1940 STC_LOGE("stc monitor not initialized!");
1944 context.now = time(NULL);
1945 context.week_start_ts = stc_time_get_week_start(context.now);
1946 context.day_start_ts = stc_time_get_day_start(context.now);
1947 context.is_updated = 0;
1949 if (g_system->last_week_ts != context.week_start_ts) {
1950 g_system->last_week_ts = context.week_start_ts;
1951 context.is_updated |= (1 << STC_RSTN_LIMIT_TYPE_WEEKLY);
1954 if (g_system->last_day_ts != context.day_start_ts) {
1955 g_system->last_day_ts = context.day_start_ts;
1956 context.is_updated |= (1 << STC_RSTN_LIMIT_TYPE_DAILY);
1959 if (g_system->rstns) {
1960 g_tree_foreach(g_system->rstns,
1961 __reset_time_counter_foreach_rstn,
1963 if (context.is_updated)
1964 STC_LOGD("Counter reset completed month_start [%ld], week_start [%ld], day_start [%ld]",
1965 context.month_start_ts, g_system->last_week_ts, g_system->last_day_ts);
1969 static void __fill_nfacct_result(char *cnt_name, int64_t bytes,
1970 struct counter_arg *carg)
1972 __reset_time_counters_if_required();
1974 struct nfacct_rule counter = {
1981 classid_bytes_context_s context = {
1982 .counter = &counter,
1984 .data_limit_exceeded = FALSE,
1987 if (!recreate_counter_by_name(cnt_name, &counter)) {
1988 STC_LOGE("Can't parse counter name %s", cnt_name); //LCOV_EXCL_LINE
1989 return; //LCOV_EXCL_LINE
1993 STC_LOGI("classid %u, iftype %u, iotype %d, intend %d, ifname %s, bytes %lld",
1994 context.counter->classid, context.counter->iftype,
1995 context.counter->iotype, context.counter->intend,
1996 context.counter->ifname, context.bytes);
1998 if (g_system->rstns)
1999 g_tree_foreach(g_system->rstns,
2000 __rstn_counter_update_foreach_classid,
2004 g_tree_foreach(g_system->apps,
2005 __apps_counter_update_foreach_classid,
2009 static int __fill_counters(struct rtattr *attr_list[__NFACCT_MAX],
2012 struct counter_arg *carg = user_data;
2013 char *cnt_name = (char *)RTA_DATA(attr_list[NFACCT_NAME]);
2014 if (carg->initiate) {
2016 * TODO: this will be used when daemon starts to update existing
2017 * counter data if present.
2019 populate_counters(cnt_name, carg);
2023 (int64_t *)RTA_DATA(attr_list[NFACCT_BYTES]);
2024 int bytes = be64toh(*bytes_p);
2026 ++carg->serialized_counters;
2027 __fill_nfacct_result(cnt_name, bytes, carg);
2034 static int __post_fill_counters(void *user_data)
2036 struct counter_arg *carg = user_data;
2044 static void __process_network_counter(struct genl *ans,
2045 struct counter_arg *carg)
2047 struct netlink_serialization_params ser_params = {
2050 .eval_attr = __fill_counters,
2051 .post_eval_attr = __post_fill_counters,
2054 netlink_serialization_command *netlink =
2055 netlink_create_command(&ser_params);
2057 STC_LOGE("Can not create command"); //LCOV_EXCL_LINE
2058 return; //LCOV_EXCL_LINE
2061 netlink->deserialize_answer(&(netlink->params));
2064 static gboolean __process_contr_reply(GIOChannel *source,
2065 GIOCondition condition,
2068 int sock = g_io_channel_unix_get_fd(source);
2071 stc_s *stc = stc_get_manager();
2074 void __gcov_flush(void);
2078 if ((condition & G_IO_ERR) || (condition & G_IO_HUP) ||
2079 (condition & G_IO_NVAL)) {
2080 /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */
2082 STC_LOGE("Counter socket received G_IO event, closing socket." //LCOV_EXCL_LINE
2083 "G_IO_ERR [%u], G_IO_HUP [%u], G_IO_NVAL [%u]",
2084 (condition & G_IO_ERR), (condition & G_IO_HUP),
2085 (condition & G_IO_NVAL));
2086 __close_and_reopen_contr_sock(g_system); //LCOV_EXCL_LINE
2087 return FALSE; //LCOV_EXCL_LINE
2090 ans = MALLOC0(struct genl, 1);
2092 STC_LOGE("Failed allocate memory to genl reply message"); //LCOV_EXCL_LINE
2093 return TRUE; //LCOV_EXCL_LINE
2097 STC_LOGE("Can't get stc data"); //LCOV_EXCL_LINE
2098 goto out; //LCOV_EXCL_LINE
2101 ret = read_netlink(sock, ans, sizeof(struct genl));
2102 /* STC_LOGD("Counter data received ret [%d]", ret); */
2106 stc->carg->ans_len = ret;
2107 stc->carg->last_run_time = time(NULL);
2109 __process_network_counter(ans, stc->carg);
2111 g_idle_add(__flush_apps_stats_to_database, NULL);
2112 g_idle_add(__flush_rstns_counter_to_database, NULL);
2119 static gboolean __update_contr_cb(void *user_data)
2121 /* Here we just sent command, answer we receive in another callback */
2122 stc_s *stc = stc_get_manager();
2123 ret_value_msg_if(stc == NULL, STC_ERROR_FAIL, "Can't get stc data");
2125 stc->carg = MALLOC0(counter_arg_s, 1);
2126 if (stc->carg == NULL)
2127 return TRUE; /* we need to continue the timer */
2129 stc->carg->sock = stc_monitor_get_counter_socket();
2133 void __gcov_flush(void);
2137 /* STC_LOGD("Get all counters"); */
2138 nfacct_send_get_all(stc->carg);
2140 /* we need to continue the timer */
2146 static gboolean __rstn_tree_foreach_print(gpointer key, gpointer value,
2149 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
2150 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
2152 __print_rstn(rstn_key, rstn_value);
2156 static void __rstn_tree_printall(void)
2158 g_tree_foreach(g_system->rstns, __rstn_tree_foreach_print, NULL);
2163 static stc_rstn_value_s * __rstn_lookup(GTree *rstns_tree,
2164 const stc_rstn_key_s *key)
2166 stc_rstn_value_s *lookup;
2168 ret_value_msg_if(rstns_tree == NULL, NULL, "rstns_tree is null!");
2170 lookup = g_tree_lookup(rstns_tree, key);
2175 static gboolean __remove_restriction(gpointer key, gpointer value,
2178 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
2179 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
2181 __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
2182 __process_tethering_restriction(RST_UNSET, rstn_key, rstn_value, data);
2183 __print_rstn(rstn_key, rstn_value);
2187 static gboolean __add_restriction_debug(gpointer key, gpointer value,
2190 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
2191 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
2193 /* rstn rule is activated */
2194 if (rstn_value->rstn_state == STC_RSTN_STATE_ACTIVATED)
2197 if (rstn_value->rstn_type == STC_RSTN_TYPE_ACCEPT) {
2198 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
2199 __process_tethering_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
2201 __process_restriction(RST_SET, rstn_key, rstn_value, data);
2202 __process_tethering_restriction(RST_SET, rstn_key, rstn_value, data);
2205 __print_rstn(rstn_key, rstn_value);
2211 static gboolean __add_restriction(gpointer key, gpointer value, gpointer data)
2213 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
2214 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
2216 /* rstn rule is activated */
2217 if (rstn_value->rstn_state == STC_RSTN_STATE_ACTIVATED)
2220 if (rstn_value->rstn_type == STC_RSTN_TYPE_ACCEPT) {
2221 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
2222 __process_tethering_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
2224 __process_restriction(RST_SET, rstn_key, rstn_value, data);
2225 __process_tethering_restriction(RST_SET, rstn_key, rstn_value, data);
2232 static stc_error_e __rstn_tree_remove(stc_rstn_key_s *key)
2234 stc_rstn_value_s *lookup_value;
2236 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2238 lookup_value = __rstn_lookup(g_system->rstns, key);
2239 if (!lookup_value) {
2240 STC_LOGE("key not found"); //LCOV_EXCL_LINE
2241 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
2244 /* remove counter also */
2245 table_counters_delete(lookup_value->restriction_id);
2246 __remove_restriction(key, lookup_value, NULL);
2248 if (!g_tree_remove(g_system->rstns, key)) {
2249 STC_LOGD("key not found"); //LCOV_EXCL_LINE
2250 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
2253 return STC_ERROR_NONE;
2256 static stc_error_e __rstn_tree_add(stc_rstn_key_s *key,
2257 stc_rstn_value_s *value, gboolean debug)
2259 stc_rstn_key_s *rstn_key;
2260 stc_rstn_value_s *rstn_value;
2262 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2264 rstn_value = __rstn_lookup(g_system->rstns, key);
2266 __rstn_tree_remove(key);
2268 rstn_key = MALLOC0(stc_rstn_key_s, 1);
2270 STC_LOGE("rstn_key allocation failed"); //LCOV_EXCL_LINE
2271 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
2274 rstn_value = MALLOC0(stc_rstn_value_s, 1);
2276 STC_LOGE("rstn_value allocation failed"); //LCOV_EXCL_LINE
2277 FREE(rstn_key); //LCOV_EXCL_LINE
2278 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
2281 rstn_key->app_id = g_strdup(key->app_id);
2282 rstn_key->ifname = g_strdup(key->ifname);
2283 rstn_key->mac = g_strdup(key->mac);
2284 rstn_key->subscriber_id = g_strdup(key->subscriber_id);
2285 rstn_key->iftype = key->iftype;
2286 rstn_key->roaming = key->roaming;
2288 g_tree_insert(g_system->rstns, rstn_key, rstn_value);
2290 rstn_value->restriction_id = value->restriction_id;
2291 rstn_value->rstn_state = value->rstn_state;
2292 rstn_value->rstn_type = value->rstn_type;
2293 rstn_value->classid = value->classid;
2296 for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
2297 rstn_value->limit[i] = value->limit[i];
2298 rstn_value->counter[i] = 0;
2301 rstn_value->limit_exceeded = 0;
2302 rstn_value->limit_notified = 0;
2303 rstn_value->month_start_date = value->month_start_date;
2304 rstn_value->month_start_ts = value->month_start_ts;
2307 __add_restriction_debug(key, rstn_value, NULL);
2309 __add_restriction(key, rstn_value, NULL);
2311 return STC_ERROR_NONE;
2315 static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info,
2318 stc_cb_ret_e ret = STC_CONTINUE;
2321 stc_rstn_value_s value;
2323 memset(&key, 0, sizeof(stc_rstn_key_s));
2324 memset(&value, 0, sizeof(stc_rstn_value_s));
2326 key.app_id = g_strdup(info->app_id);
2327 key.ifname = g_strdup(info->ifname);
2328 key.subscriber_id = g_strdup(info->subscriber_id);
2329 key.iftype = info->iftype;
2330 key.roaming = info->roaming;
2332 value.rstn_type = info->rstn_type;
2333 value.rstn_state = STC_RSTN_STATE_UNKNOWN;
2334 value.restriction_id = info->restriction_id;
2337 value.classid = get_classid_by_app_id(info->app_id, TRUE);
2339 value.classid = STC_UNKNOWN_CLASSID;
2341 value.limit[STC_RSTN_LIMIT_TYPE_DATA] = info->data_limit;
2342 value.limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info->data_warn_limit;
2343 value.limit[STC_RSTN_LIMIT_TYPE_MONTHLY] = info->monthly_limit;
2344 value.limit[STC_RSTN_LIMIT_TYPE_WEEKLY] = info->weekly_limit;
2345 value.limit[STC_RSTN_LIMIT_TYPE_DAILY] = info->daily_limit;
2347 if (__rstn_tree_add(&key, &value, FALSE) != STC_ERROR_NONE)
2352 FREE(key.subscriber_id);
2356 static void __fill_restritions_list(void)
2358 table_restrictions_foreach(__insert_restriction_cb, NULL);
2360 /* __rstn_tree_printall(); */
2363 static gboolean __add_rstn_foreach_application(gpointer key,
2367 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
2368 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
2369 gchar *app_id = (gchar *)data;
2371 /* rstn rule is not for applications */
2372 if (rstn_key->app_id == NULL)
2375 /* rstn rule is not for this application */
2376 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
2379 /* rstn rule is already applied */
2380 if (rstn_value->rstn_state == STC_RSTN_STATE_ACTIVATED)
2383 /* add restriction to system */
2384 if (rstn_value->rstn_type == STC_RSTN_TYPE_ACCEPT) {
2385 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, NULL);
2386 __process_tethering_restriction(RST_EXCLUDE, rstn_key, rstn_value, NULL);
2388 __process_restriction(RST_SET, rstn_key, rstn_value, NULL);
2389 __process_tethering_restriction(RST_SET, rstn_key, rstn_value, NULL);
2392 __print_rstn(rstn_key, rstn_value);
2398 static void __add_rstns_for_application(gchar *app_id)
2400 g_tree_foreach(g_system->rstns, __add_rstn_foreach_application,
2404 static void __add_application_by_interface(const char *app_id)
2406 stc_app_key_s app_key;
2407 stc_app_value_s app_value;
2410 return; //LCOV_EXCL_LINE
2412 memset(&app_key, 0, sizeof(stc_app_key_s));
2413 memset(&app_value, 0, sizeof(stc_app_value_s));
2415 app_key.pkg_id = g_strdup(app_id);
2416 app_key.app_id = g_strdup(app_id);
2418 app_value.type = STC_APP_TYPE_NONE;
2419 app_value.processes = NULL;
2420 app_value.counter.in_bytes = 0;
2421 app_value.counter.out_bytes = 0;
2423 stc_monitor_application_add(app_key, app_value);
2425 FREE(app_key.pkg_id);
2426 FREE(app_key.app_id);
2429 static guint __get_background_state(void)
2431 return g_system->background_state;;
2434 static void __set_background_state(guint state)
2436 g_system->background_state = state;
2439 static gboolean __processes_tree_foreach_background(gpointer key,
2443 stc_process_key_s *proc_key = (stc_process_key_s *)key;
2444 stc_app_key_s *app_key = (stc_app_key_s *)data;
2446 if (g_system->background_state)
2447 place_pids_to_net_cgroup(proc_key->pid, STC_BACKGROUND_APP_ID);
2449 place_pids_to_net_cgroup(proc_key->pid, app_key->app_id);
2454 static gboolean __apps_tree_foreach_background(gpointer key, gpointer value,
2457 stc_app_key_s *app_key = (stc_app_key_s *)key;
2458 stc_app_value_s *app_value = (stc_app_value_s *)value;
2460 if (strstr(app_key->app_id, STC_BACKGROUND_APP_SUFFIX))
2461 g_tree_foreach(app_value->processes,
2462 __processes_tree_foreach_background, app_key);
2467 static stc_error_e __process_update_background(void)
2469 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2471 g_tree_foreach(g_system->apps, __apps_tree_foreach_background, NULL);
2473 return STC_ERROR_NONE;
2477 static void __fill_exceptions_list(void)
2479 stc_plugin_fill_exception_list();
2482 stc_error_e stc_monitor_init(void)
2484 stc_system_s *system = MALLOC0(stc_system_s, 1);
2485 GIOChannel *gio = NULL;
2487 ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY, "stc_system_s malloc fail!");
2489 /* initializing current classid */
2490 init_current_classid();
2492 /* initializing cgroups */
2495 /* creating monitored application tree */
2496 system->apps = g_tree_new_full(__apps_tree_key_compare, NULL,
2497 __apps_tree_key_free,
2498 __apps_tree_value_free);
2500 system->rstns = g_tree_new_full(__rstns_tree_key_compare, NULL,
2501 __rstns_tree_key_free,
2502 __rstns_tree_value_free);
2504 /* create netlink socket for updating kernel counters */
2505 system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
2506 if (system->contr_sock < 0) {
2507 STC_LOGE("failed to open socket"); //LCOV_EXCL_LINE
2508 FREE(system); //LCOV_EXCL_LINE
2509 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
2512 gio = g_io_channel_unix_new(system->contr_sock);
2513 system->contr_gsource_id =
2514 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
2515 (GIOFunc) __process_contr_reply,
2517 g_io_channel_unref(gio);
2521 __add_application_by_interface(STC_TOTAL_DATACALL);
2522 __add_application_by_interface(STC_TOTAL_WIFI);
2523 __add_application_by_interface(STC_TOTAL_BLUETOOTH);
2524 __add_application_by_interface(STC_TOTAL_IPV4);
2525 __add_application_by_interface(STC_TOTAL_IPV6);
2526 __add_application_by_interface(STC_TOTAL_TETHERING);
2528 /* creating restriction rules tree */
2529 __update_contr_cb(NULL);
2531 /* registering periodic kernel counters update callback */
2532 g_system->contr_timer_id = g_timeout_add_seconds(CONTR_TIMER_INTERVAL,
2535 if (g_system->contr_timer_id == 0) {
2536 STC_LOGE("Failed to register kernel counters update timer"); //LCOV_EXCL_LINE
2537 __close_contr_sock(g_system); //LCOV_EXCL_LINE
2538 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
2541 __vconf_get_int(VCONFKEY_STC_BACKGROUND_STATE,
2542 (int *)&g_system->background_state);
2544 __fill_exceptions_list();
2545 __fill_restritions_list();
2547 return STC_ERROR_NONE;
2550 stc_error_e stc_monitor_deinit(void)
2552 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2554 /* close netlink socket for updating kernel counters */
2555 __close_contr_sock(g_system);
2557 /* remove kernel counters update timer */
2558 if (g_system->contr_timer_id > 0) {
2559 g_source_remove(g_system->contr_timer_id);
2560 g_system->contr_timer_id = 0;
2563 /* destroy monitored application tree */
2564 g_tree_destroy(g_system->apps);
2565 g_system->apps = NULL;
2567 /* destroy restriction rules tree */
2568 g_tree_destroy(g_system->rstns);
2569 g_system->rstns = NULL;
2573 return STC_ERROR_NONE;
2576 API stc_error_e stc_monitor_application_add(const stc_app_key_s app_key,
2577 const stc_app_value_s app_value)
2579 stc_error_e ret = STC_ERROR_NONE;
2581 stc_app_value_s *value;
2582 stc_app_value_s *lookup;
2584 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2586 lookup = __application_lookup(g_system->apps, &app_key);
2588 return STC_ERROR_NONE; //LCOV_EXCL_LINE
2590 key = MALLOC0(stc_app_key_s, 1);
2592 STC_LOGE("key allocation failed"); //LCOV_EXCL_LINE
2593 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
2596 value = MALLOC0(stc_app_value_s, 1);
2598 STC_LOGE("value allocation failed"); //LCOV_EXCL_LINE
2599 FREE(key); //LCOV_EXCL_LINE
2600 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
2603 key->app_id = g_strdup(app_key.app_id);
2604 key->pkg_id = g_strdup(app_key.pkg_id);
2606 value->type = app_value.type;
2607 value->data_usage.in_bytes = app_value.data_usage.in_bytes;
2608 value->data_usage.out_bytes = app_value.data_usage.out_bytes;
2609 g_strlcpy(value->mac, app_value.mac, MAC_ADDRESS_LEN);
2611 value->processes = g_tree_new_full(__processes_tree_key_compare, NULL,
2612 __processes_tree_key_free,
2613 __processes_tree_value_free);
2615 /* create cgroup and update classid */
2616 value->classid = get_classid_by_app_id(app_key.app_id, TRUE);
2618 /* update classid for tethering station based on its mac address */
2619 if (g_str_has_suffix(app_key.app_id, STC_TETHERING_APP_SUFFIX) &&
2620 value->classid != STC_TETHERING_APP_CLASSID)
2621 stc_plugin_tether_set_station_classid(value->mac, value->classid);
2623 g_tree_insert(g_system->apps, key, value);
2625 /* add nfacct rule for this classid */
2626 __add_application_monitor(key, value, stc_get_default_connection());
2627 __add_rstns_for_application(app_key.app_id);
2632 API stc_error_e stc_monitor_application_remove(const stc_app_key_s app_key)
2634 stc_error_e ret = STC_ERROR_NONE;
2635 stc_app_value_s *app_lookup;
2637 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2639 app_lookup = __application_lookup(g_system->apps, &app_key);
2642 STC_LOGD("app_key not found"); //LCOV_EXCL_LINE
2643 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
2646 /* remove nfacct rule for this classid */
2647 __remove_application_monitor((gpointer) &app_key, app_lookup,
2648 stc_get_default_connection());
2650 /* remove ristrictions if any */
2651 __remove_rstns_for_application(app_key.app_id);
2653 /* remove app_key from the stc-manager */
2654 if (!g_tree_remove(g_system->apps, &app_key)) {
2655 ret = STC_ERROR_NO_DATA;
2656 STC_LOGE("key not found");
2662 API stc_error_e stc_monitor_process_add(const stc_app_key_s app_key,
2663 const stc_process_key_s proc_key,
2664 const stc_process_value_s proc_value)
2666 stc_error_e ret = STC_ERROR_NONE;
2667 stc_app_value_s *app_lookup;
2668 stc_process_key_s *key;
2669 stc_process_value_s *value;
2670 stc_process_value_s *proc_lookup;
2672 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2674 app_lookup = __application_lookup(g_system->apps, &app_key);
2677 STC_LOGD("app_key not found"); //LCOV_EXCL_LINE
2678 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
2681 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
2683 return STC_ERROR_NONE; //LCOV_EXCL_LINE
2685 key = MALLOC0(stc_process_key_s, 1);
2687 STC_LOGE("key allocation failed"); //LCOV_EXCL_LINE
2688 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
2691 value = MALLOC0(stc_process_value_s, 1);
2693 STC_LOGE("value allocation failed"); //LCOV_EXCL_LINE
2694 FREE(key); //LCOV_EXCL_LINE
2695 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
2698 key->pid = proc_key.pid;
2700 value->ground = proc_value.ground;
2702 g_tree_insert(app_lookup->processes, key, value);
2704 /* add pid to application cgroup */
2705 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
2708 __apps_tree_printall(); //LCOV_EXCL_LINE
2714 API stc_error_e stc_monitor_process_remove(pid_t pid)
2716 stc_error_e ret = STC_ERROR_NONE;
2717 stc_process_key_s proc_key = {
2721 remove_pid_context_s context = {
2723 .proc_key = &proc_key,
2724 .entry_removed = FALSE,
2727 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2729 g_tree_foreach(g_system->apps, __apps_tree_foreach_remove_pid,
2732 if (context.entry_removed)
2733 __application_remove_if_empty(context.app_key);
2736 __apps_tree_printall(); //LCOV_EXCL_LINE
2743 API stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key,
2744 const stc_process_key_s proc_key,
2745 stc_app_state_e ground)
2747 stc_error_e ret = STC_ERROR_NONE;
2748 stc_app_value_s *app_lookup;
2749 stc_process_value_s *proc_lookup;
2751 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2753 app_lookup = __application_lookup(g_system->apps, &app_key);
2755 STC_LOGD("app_key not found");
2756 return STC_ERROR_FAIL;
2759 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
2761 STC_LOGD("proc_key not found");
2762 return STC_ERROR_FAIL;
2765 if (proc_lookup->ground != ground)
2766 proc_lookup->ground = ground;
2768 if (ground == STC_APP_STATE_BACKGROUND && __get_background_state())
2769 place_pids_to_net_cgroup(proc_key.pid, STC_BACKGROUND_APP_ID);
2771 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
2777 void stc_monitor_update_rstn_by_default_connection(void *data)
2779 static default_connection_s old_connection;
2780 default_connection_s *new_connection = (default_connection_s *)data;
2782 if (old_connection.path != NULL) {
2785 g_tree_foreach(g_system->apps,
2786 __remove_application_monitor,
2787 (gpointer)&old_connection);
2789 if (g_system->rstns)
2790 g_tree_foreach(g_system->rstns,
2791 __remove_restriction,
2792 (gpointer)&old_connection);
2794 iptables_flush_chains();
2798 FREE(old_connection.path);
2799 FREE(old_connection.ifname);
2800 FREE(old_connection.tether_iface.ifname);
2801 old_connection.type = 0;
2802 old_connection.roaming = 0;
2803 old_connection.tether_state = FALSE;
2804 old_connection.tether_iface.type = 0;
2806 if (new_connection != NULL && new_connection->path != NULL) {
2808 g_tree_foreach(g_system->apps,
2809 __add_application_monitor,
2810 (gpointer)new_connection);
2812 if (g_system->rstns)
2813 g_tree_foreach(g_system->rstns, __add_restriction,
2816 old_connection.path = g_strdup(new_connection->path);
2817 old_connection.ifname = g_strdup(new_connection->ifname);
2818 old_connection.tether_iface.ifname = g_strdup(new_connection->tether_iface.ifname);
2819 old_connection.type = new_connection->type;
2820 old_connection.roaming = new_connection->roaming;
2821 old_connection.tether_state = new_connection->tether_state;
2822 old_connection.tether_iface.type = new_connection->tether_iface.type;
2826 stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info)
2831 stc_rstn_value_s value;
2833 memset(&key, 0, sizeof(stc_rstn_key_s));
2834 memset(&value, 0, sizeof(stc_rstn_value_s));
2836 key.app_id = g_strdup(info->app_id);
2837 key.ifname = g_strdup(info->ifname);
2838 key.mac = g_strdup(info->mac);
2839 key.subscriber_id = g_strdup(info->subscriber_id);
2840 key.iftype = info->iftype;
2841 key.roaming = info->roaming;
2843 value.rstn_type = info->rstn_type;
2844 value.rstn_state = STC_RSTN_STATE_UNKNOWN;
2845 value.restriction_id = info->restriction_id;
2848 value.classid = get_classid_by_app_id(info->app_id, TRUE);
2850 value.classid = STC_UNKNOWN_CLASSID;
2852 if (value.classid == STC_BACKGROUND_APP_CLASSID) {
2853 __set_background_state(TRUE); //LCOV_EXCL_LINE
2854 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state); //LCOV_EXCL_LINE
2855 __process_update_background(); //LCOV_EXCL_LINE
2858 value.limit[STC_RSTN_LIMIT_TYPE_DATA] = info->data_limit;
2859 value.limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info->data_warn_limit;
2860 value.limit[STC_RSTN_LIMIT_TYPE_MONTHLY] = info->monthly_limit;
2861 value.limit[STC_RSTN_LIMIT_TYPE_WEEKLY] = info->weekly_limit;
2862 value.limit[STC_RSTN_LIMIT_TYPE_DAILY] = info->daily_limit;
2863 value.month_start_date = info->month_start_date;
2864 value.month_start_ts = stc_time_get_month_start(time(NULL),
2865 info->month_start_date);
2867 ret = __rstn_tree_add(&key, &value, TRUE);
2872 FREE(key.subscriber_id);
2876 stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info)
2880 stc_rstn_key_s key = {
2881 .app_id = g_strdup(info->app_id),
2882 .ifname = g_strdup(info->ifname),
2883 .subscriber_id = g_strdup(info->subscriber_id),
2884 .iftype = info->iftype,
2885 .roaming = info->roaming,
2888 if (!strcmp(key.app_id, STC_BACKGROUND_APP_ID)) {
2889 __set_background_state(FALSE); //LCOV_EXCL_LINE
2890 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state); //LCOV_EXCL_LINE
2891 __process_update_background(); //LCOV_EXCL_LINE
2894 ret = __rstn_tree_remove(&key);
2898 FREE(key.subscriber_id);
2902 API stc_error_e stc_monitor_check_excn_by_cmdline(char *cmdline)
2904 return stc_plugin_check_exception_by_cmdline(cmdline);
2907 int stc_monitor_get_counter_socket(void)
2909 return g_system->contr_sock;