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 "stc-default-connection.h"
20 #include "helper-nl.h"
21 #include "helper-nfacct-rule.h"
22 #include "helper-net-cls.h"
24 #include "table-statistics.h"
25 #include "table-counters.h"
26 #include "stc-monitor.h"
29 stc_app_key_s *app_key;
30 stc_process_key_s *proc_key;
31 gboolean entry_removed;
32 } remove_pid_context_s;
35 struct nfacct_rule *counter;
37 gboolean in_limit_reached;
38 gboolean out_limit_reached;
39 } classid_bytes_context_s;
41 static stc_system_s *g_system = NULL;
43 static nfacct_rule_jump __get_jump_by_intend(struct nfacct_rule *counter)
45 if (counter->intend == NFACCT_WARN)
46 return NFACCT_JUMP_ACCEPT;
47 else if (counter->intend == NFACCT_BLOCK)
48 return NFACCT_JUMP_REJECT;
50 return NFACCT_JUMP_UNKNOWN;
53 static stc_error_e __add_iptables_in(struct nfacct_rule *counter)
55 return produce_net_rule(counter, 0, 0,
56 NFACCT_ACTION_INSERT, __get_jump_by_intend(counter),
60 static stc_error_e __add_iptables_out(struct nfacct_rule *counter)
62 return produce_net_rule(counter, 0, 0,
63 NFACCT_ACTION_INSERT, __get_jump_by_intend(counter),
67 static stc_error_e __del_iptables_in(struct nfacct_rule *counter)
69 return produce_net_rule(counter, 0, 0,
70 NFACCT_ACTION_DELETE, __get_jump_by_intend(counter),
74 static stc_error_e __del_iptables_out(struct nfacct_rule *counter)
76 return produce_net_rule(counter, 0, 0,
77 NFACCT_ACTION_DELETE, __get_jump_by_intend(counter),
81 static int __processes_tree_key_compare(gconstpointer a, gconstpointer b,
82 gpointer UNUSED user_data)
84 stc_process_key_s *key_a = (stc_process_key_s *)a;
85 stc_process_key_s *key_b = (stc_process_key_s *)b;
87 return key_a->pid - key_b->pid;
90 static void __processes_tree_value_free(gpointer data)
92 stc_process_value_s *value = (stc_process_value_s *)data;
97 static void __processes_tree_key_free(gpointer data)
99 stc_process_key_s *key = (stc_process_key_s *)data;
104 static int __apps_tree_key_compare(gconstpointer a, gconstpointer b,
105 gpointer UNUSED user_data)
107 stc_app_key_s *key_a = (stc_app_key_s *)a;
108 stc_app_key_s *key_b = (stc_app_key_s *)b;
111 ret = g_strcmp0(key_a->pkg_id, key_b->pkg_id);
115 return g_strcmp0(key_a->app_id, key_b->app_id);
118 static void __apps_tree_value_free(gpointer data)
120 stc_app_value_s *value = (stc_app_value_s *)data;
122 g_tree_destroy(value->processes);
123 value->processes = NULL;
128 static void __apps_tree_key_free(gpointer data)
130 stc_app_key_s *key = (stc_app_key_s *)data;
137 static int __rstns_tree_key_compare(gconstpointer a, gconstpointer b,
138 gpointer UNUSED user_data)
140 stc_rstn_key_s *key_a = (stc_rstn_key_s *)a;
141 stc_rstn_key_s *key_b = (stc_rstn_key_s *)b;
144 ret = g_strcmp0(key_a->app_id, key_b->app_id);
148 ret = g_strcmp0(key_a->ifname, key_b->ifname);
152 ret = g_strcmp0(key_a->imsi, key_b->imsi);
156 ret = key_a->iftype - key_b->iftype;
163 static void __rstns_tree_value_free(gpointer data)
165 stc_rstn_value_s *value = (stc_rstn_value_s *)data;
170 static void __rstns_tree_key_free(gpointer data)
172 stc_rstn_key_s *key = (stc_rstn_key_s *)data;
180 static gboolean __processes_tree_foreach_print(gpointer key, gpointer value,
183 stc_process_key_s *proc_key = (stc_process_key_s *)key;
184 stc_process_value_s *proc_value = (stc_process_value_s *)value;
186 STC_LOGD("Process entry => PID [%d], Ground state [%d]",
187 proc_key->pid, proc_value->ground);
191 static void __processes_tree_printall(GTree *processes)
193 g_tree_foreach(processes, __processes_tree_foreach_print, NULL);
196 static gboolean __apps_tree_foreach_print(gpointer key, gpointer value,
199 stc_app_key_s *app_key = (stc_app_key_s *)key;
200 stc_app_value_s *app_value = (stc_app_value_s *)value;
202 STC_LOGD("Application info => Pkg ID [%s], App ID [%s],"
203 " Type [%d], classid [%d],"
204 " counter [ in (%lld), out (%lld)]",
205 app_key->pkg_id, app_key->app_id,
206 app_value->type, app_value->classid,
207 app_value->data_usage.in_bytes, app_value->data_usage.out_bytes);
209 __processes_tree_printall(app_value->processes);
213 static void __apps_tree_printall(void)
215 g_tree_foreach(g_system->apps, __apps_tree_foreach_print, NULL);
218 static gboolean __apps_tree_foreach_remove_pid(gpointer key, gpointer value,
221 remove_pid_context_s *context = (remove_pid_context_s *)data;
222 stc_app_value_s *app_value = (stc_app_value_s *)value;
224 if (!g_tree_remove(app_value->processes, context->proc_key)) {
225 STC_LOGD("key not found");
229 context->entry_removed = TRUE;
230 context->app_key = (stc_app_key_s *)key;
235 static stc_app_value_s * __application_lookup(GTree *apps,
236 const stc_app_key_s *key)
238 stc_app_value_s *lookup;
240 ret_value_msg_if(apps == NULL, NULL, "apps is null!");
242 lookup = g_tree_lookup(apps, key);
247 static stc_process_value_s * __process_lookup(GTree *processes,
248 const stc_process_key_s *key)
250 stc_process_value_s *lookup;
252 ret_value_msg_if(processes == NULL, NULL, "processes is null!");
254 lookup = g_tree_lookup(processes, key);
259 static gboolean __processes_tree_check_empty(gpointer key, gpointer value,
262 guint *pid_count = (guint *)data;
267 static gboolean __add_application_monitor(gpointer key, gpointer value,
270 stc_app_value_s *app_value = (stc_app_value_s *)value;
271 default_connection_s *connection = (default_connection_s *)data;
272 stc_s *stc = stc_get_manager();
274 if (stc && connection && connection->ifname) {
275 struct nfacct_rule counter;
278 stc->carg = MALLOC0(counter_arg_s, 1);
279 stc->carg->sock = stc_monitor_get_counter_socket();
282 memset(&counter, 0, sizeof(struct nfacct_rule));
284 counter.carg = stc->carg;
285 counter.classid = app_value->classid;
286 counter.intend = NFACCT_COUNTER;
287 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
289 __add_iptables_in(&counter);
290 __add_iptables_out(&counter);
296 static gboolean __remove_application_monitor(gpointer key, gpointer value,
299 stc_app_value_s *app_value = (stc_app_value_s *)value;
300 default_connection_s *connection = (default_connection_s *)data;
301 stc_s *stc = stc_get_manager();
303 if (stc && connection && connection->ifname) {
304 struct nfacct_rule counter;
307 stc->carg = MALLOC0(counter_arg_s, 1);
308 stc->carg->sock = stc_monitor_get_counter_socket();
311 memset(&counter, 0, sizeof(struct nfacct_rule));
313 counter.carg = stc->carg;
314 counter.classid = app_value->classid;
315 counter.intend = NFACCT_COUNTER;
316 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
318 __del_iptables_in(&counter);
319 __del_iptables_out(&counter);
325 static void __print_rstn(stc_rstn_key_s *rstn_key, stc_rstn_value_s *rstn_value)
327 STC_LOGI("rstn info => rstn_id [%llu], "
328 "app_id [%s], classid [%lu], ifname [%s], "
329 "iftype [%d], rst_state [%d], "
330 "limit [ in (%lld), out (%lld)], "
331 "warn_limit [ in (%lld), out (%lld)], "
332 "counter [ in (%lld), out (%lld)], "
333 "roaming [%d], imsi [%s]",
334 rstn_value->restriction_id,
335 rstn_key->app_id, rstn_value->classid , rstn_key->ifname,
336 rstn_key->iftype, rstn_value->rst_state,
337 rstn_value->limit.in_bytes, rstn_value->limit.out_bytes,
338 rstn_value->warn_limit.in_bytes,
339 rstn_value->warn_limit.out_bytes,
340 rstn_value->counter.in_bytes, rstn_value->counter.out_bytes,
341 rstn_key->roaming, rstn_key->imsi);
344 static void __process_restriction(enum traffic_restriction_type rst_type,
345 stc_rstn_key_s *rstn_key,
346 stc_rstn_value_s *rstn_value, void *data)
348 stc_data_counter_s effective_limit, effective_warn_limit;
349 default_connection_s *old_connection = (default_connection_s *)data;
350 default_connection_s *connection = NULL;
352 if (old_connection != NULL)
353 connection = old_connection;
355 connection = stc_get_default_connection();
357 /* no default ifname */
358 if (connection->ifname == NULL)
361 /* rstn not applicable for this interface */
362 if (rstn_key->ifname != NULL && g_strcmp0("", rstn_key->ifname) != 0 &&
363 g_strcmp0(connection->ifname, rstn_key->ifname) != 0)
366 /* classid is invalid */
367 if (rstn_value->classid == STC_UNKNOWN_CLASSID)
370 effective_limit.out_bytes = rstn_value->limit.out_bytes;
371 effective_limit.in_bytes = rstn_value->limit.in_bytes;
372 effective_warn_limit.out_bytes = rstn_value->warn_limit.out_bytes;
373 effective_warn_limit.in_bytes = rstn_value->warn_limit.in_bytes;
375 if (rst_type == RST_SET) {
376 /* TODO: Change this to runtime memory */
377 table_counters_info info;
379 memset(&info, 0, sizeof(table_counters_info));
380 table_counters_get(rstn_value->restriction_id, &info);
382 effective_limit.out_bytes -= info.sent_bytes;
383 effective_limit.in_bytes -= info.rcv_bytes;
384 effective_warn_limit.out_bytes -= info.sent_bytes;
385 effective_warn_limit.in_bytes -= info.rcv_bytes;
387 if (effective_limit.in_bytes < 0) {
388 effective_limit.in_bytes = 0;
389 rstn_value->in_limit_reached = TRUE;
392 if (effective_limit.out_bytes < 0) {
393 effective_limit.out_bytes = 0;
394 rstn_value->out_limit_reached = TRUE;
397 if (effective_warn_limit.in_bytes < 0)
398 effective_warn_limit.in_bytes = 0;
400 if (effective_warn_limit.out_bytes < 0)
401 effective_warn_limit.out_bytes = 0;
402 STC_LOGD("datausage [in: %lld, out: %lld]",
403 info.rcv_bytes, info.sent_bytes);
406 STC_LOGD("rstn_id [%llu], effective limit [in: %lld, out: %lld], "
407 "effective warn limit [in: %lld, out: %lld], "
408 "datausage [in: %lld, out: %lld]",
409 rstn_value->restriction_id,
410 effective_limit.in_bytes, effective_limit.out_bytes,
411 effective_warn_limit.in_bytes, effective_warn_limit.out_bytes);
413 send_net_restriction(rst_type,
417 effective_limit.out_bytes,
418 effective_limit.in_bytes,
419 effective_warn_limit.out_bytes,
420 effective_warn_limit.in_bytes,
424 rstn_value->rst_state = STC_RESTRICTION_ACTIVATED;
425 rstn_value->in_limit_reached = FALSE;
426 rstn_value->out_limit_reached = FALSE;
432 rstn_value->rst_state = STC_RESTRICTION_REMOVED;
433 rstn_value->in_limit_reached = FALSE;
434 rstn_value->out_limit_reached = FALSE;
441 static gboolean __remove_rstns_foreach_application(gpointer key,
445 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
446 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
447 gchar *app_id = (gchar *)data;
449 /* rstn rule is not for applications */
450 if (rstn_key->app_id == NULL)
453 /* rstn rule is not for this application */
454 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
457 /* rstn rule is already removed */
458 if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
461 /* remove restriction from system */
462 __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
464 __print_rstn(rstn_key, rstn_value);
469 static void __remove_rstns_for_application(gchar *app_id)
471 g_tree_foreach(g_system->rstns, __remove_rstns_foreach_application,
475 static stc_error_e __application_remove_if_empty(const stc_app_key_s *app_key)
477 stc_error_e ret = STC_ERROR_NONE;
479 stc_app_value_s *lookup;
481 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
483 lookup = __application_lookup(g_system->apps, app_key);
485 STC_LOGE("app_key not found");
486 return STC_ERROR_NO_DATA;
489 g_tree_foreach(lookup->processes, __processes_tree_check_empty,
493 /* remove nfacct rule for this classid */
494 __remove_application_monitor((gpointer) app_key, lookup,
495 stc_get_default_connection());
496 __remove_rstns_for_application(app_key->app_id);
499 if (!g_tree_remove(g_system->apps, app_key)) {
500 ret = STC_ERROR_NO_DATA;
501 STC_LOGE("key not found");
507 static stc_error_e __close_contr_sock(stc_system_s *system)
509 ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
511 /* close netlink socket for updating kernel counters */
512 if (g_system->contr_sock != -1) {
513 close(g_system->contr_sock);
514 g_system->contr_sock = -1;
517 if (g_system->contr_gsource_id != 0) {
518 g_source_remove(g_system->contr_gsource_id);
519 g_system->contr_gsource_id = 0;
522 return STC_ERROR_NONE;
525 static gboolean __rstn_counter_update_foreach_classid(gpointer key,
529 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
530 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
531 classid_bytes_context_s *context = (classid_bytes_context_s *)data;
533 if (context->counter->intend != NFACCT_COUNTER)
534 goto try_next_callback;
536 if (rstn_value->classid != context->counter->classid)
537 goto try_next_callback;
539 if (rstn_value->in_limit_reached == TRUE) {
540 context->in_limit_reached = TRUE;
541 goto try_next_callback;
544 if (rstn_value->out_limit_reached == TRUE) {
545 context->out_limit_reached = TRUE;
546 goto try_next_callback;
549 switch (context->counter->iotype) {
550 case NFACCT_COUNTER_IN:
551 rstn_value->counter.in_bytes += context->bytes;
553 if (rstn_value->counter.in_bytes >= rstn_value->warn_limit.in_bytes
554 && rstn_value->warn_limit_crossed_notified == FALSE) {
556 stc_s *stc = (stc_s *)stc_get_manager();
557 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
559 rv = stc_manager_dbus_emit_signal(stc->connection,
560 STC_DBUS_SERVICE_RESTRICTION_PATH,
561 STC_DBUS_INTERFACE_RESTRICTION,
562 "WarnThresholdCrossed",
563 g_variant_new("(s)", rstn_key->app_id));
565 rstn_value->warn_limit_crossed_notified = TRUE;
568 /* block immediately */
569 if (rstn_value->counter.in_bytes >= rstn_value->limit.in_bytes) {
570 __del_iptables_in(context->counter);
571 __add_iptables_in(context->counter);
572 rstn_value->in_limit_reached = TRUE;
573 //context->in_limit_reached = TRUE;
576 g_system->rstns_tree_updated = TRUE;
577 __print_rstn(rstn_key, rstn_value);
579 case NFACCT_COUNTER_OUT:
580 rstn_value->counter.out_bytes += context->bytes;
582 if (rstn_value->counter.out_bytes >= rstn_value->limit.out_bytes
583 && rstn_value->warn_limit_crossed_notified == FALSE) {
585 stc_s *stc = (stc_s *)stc_get_manager();
586 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
588 rv = stc_manager_dbus_emit_signal(stc->connection,
589 STC_DBUS_SERVICE_RESTRICTION_PATH,
590 STC_DBUS_INTERFACE_RESTRICTION,
591 "WarnThresholdCrossed",
592 g_variant_new("(s)", rstn_key->app_id));
594 rstn_value->warn_limit_crossed_notified = TRUE;
597 /* block immediately */
598 if (rstn_value->counter.out_bytes >= rstn_value->limit.out_bytes) {
599 __del_iptables_out(context->counter);
600 __add_iptables_out(context->counter);
601 rstn_value->out_limit_reached = TRUE;
602 //context->out_limit_reached = TRUE;
605 g_system->rstns_tree_updated = TRUE;
606 __print_rstn(rstn_key, rstn_value);
609 STC_LOGE("unknown iotype");
617 static gboolean __update_app_statistics(gpointer key, gpointer value,
620 stc_app_key_s *app_key = (stc_app_key_s *)key;
621 stc_app_value_s *app_value = (stc_app_value_s *)value;
622 time_t *touch_time = (time_t *)data;
623 stc_db_classid_iftype_key stat_key;
624 stc_db_app_stats stat;
625 char *default_ifname = stc_default_connection_get_ifname();
627 memset(&stat_key, 0, sizeof(stc_db_classid_iftype_key));
628 memset(&stat, 0 , sizeof(stc_db_app_stats));
630 stat_key.classid = app_value->classid;
631 stat_key.iftype = stc_default_connection_get_type();
632 if (STC_IFACE_DATACALL == stat_key.iftype)
633 stat_key.imsi = g_strdup("unknown");
635 stat_key.imsi = g_strdup("noneimsi");
636 g_strlcpy(stat_key.ifname, default_ifname, MAX_IFACE_LENGTH);
638 stat.app_id = g_strdup(app_key->app_id);
639 stat.snd_count = app_value->counter.out_bytes;
640 stat.rcv_count = app_value->counter.in_bytes;
643 stat.is_roaming = stc_default_connection_get_roaming();
644 stat.ground = STC_APP_STATE_UNKNOWN;
646 table_statistics_insert(&stat_key, &stat, *touch_time);
648 app_value->counter.out_bytes = 0;
649 app_value->counter.in_bytes = 0;
653 FREE(default_ifname);
658 static gboolean __flush_apps_stats_to_database(gpointer user_data)
660 time_t current_time = time(0);
662 if (g_system->apps_tree_updated == FALSE)
663 return G_SOURCE_REMOVE;
665 g_system->apps_tree_updated = FALSE;
668 g_tree_foreach(g_system->apps,
669 __update_app_statistics,
672 STC_LOGI("Flushed app stats to database");
673 return G_SOURCE_REMOVE;
676 static gboolean __update_counter_statistics(gpointer key, gpointer value,
679 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
680 table_counters_info info = {
681 .restriction_id = rstn_value->restriction_id,
682 .sent_bytes = rstn_value->counter.out_bytes,
683 .rcv_bytes = rstn_value->counter.in_bytes
686 table_counters_update_counters(&info);
691 static gboolean __flush_rstns_counter_to_database(gpointer user_data)
693 time_t current_time = time(0);
695 if (g_system->rstns_tree_updated == FALSE)
696 return G_SOURCE_REMOVE;
698 g_system->rstns_tree_updated = FALSE;
701 g_tree_foreach(g_system->rstns,
702 __update_counter_statistics,
705 STC_LOGI("Flushed rstns counters to database");
706 return G_SOURCE_REMOVE;
709 static gboolean __apps_counter_update_foreach_classid(gpointer key,
713 stc_app_key_s *app_key = (stc_app_key_s *)key;
714 stc_app_value_s *app_value = (stc_app_value_s *)value;
715 classid_bytes_context_s *context = (classid_bytes_context_s *)data;
717 if (context->counter->intend != NFACCT_COUNTER)
718 goto try_next_callback;
720 if (app_value->classid != context->counter->classid)
721 goto try_next_callback;
723 switch (context->counter->iotype) {
724 case NFACCT_COUNTER_IN:
725 app_value->data_usage.in_bytes += context->bytes;
726 app_value->counter.in_bytes = context->bytes;
727 g_system->apps_tree_updated = TRUE;
729 __apps_tree_foreach_print(app_key, app_value, NULL);
731 case NFACCT_COUNTER_OUT:
732 app_value->data_usage.out_bytes += context->bytes;
733 app_value->counter.out_bytes = context->bytes;
734 g_system->apps_tree_updated = TRUE;
736 __apps_tree_foreach_print(app_key, app_value, NULL);
739 STC_LOGE("unknown iotype");
746 static void __fill_nfacct_result(char *cnt_name, int64_t bytes,
747 struct counter_arg *carg)
749 struct nfacct_rule counter = {
756 classid_bytes_context_s context = {
759 .in_limit_reached = FALSE,
760 .out_limit_reached = FALSE
763 STC_LOGD("cnt_name %s", cnt_name);
765 if (!recreate_counter_by_name(cnt_name, &counter)) {
766 STC_LOGE("Can't parse counter name %s", cnt_name);
770 STC_LOGI("classid %lu, iftype %u, iotype %d, intend %d, ifname %s, bytes %lld",
771 context.counter->classid, context.counter->iftype,
772 context.counter->iotype, context.counter->intend,
773 context.counter->ifname, context.bytes);
776 g_tree_foreach(g_system->rstns,
777 __rstn_counter_update_foreach_classid,
781 g_tree_foreach(g_system->apps,
782 __apps_counter_update_foreach_classid,
786 static int __fill_counters(struct rtattr *attr_list[__NFACCT_MAX],
789 struct counter_arg *carg = user_data;
790 char *cnt_name = (char *)RTA_DATA(attr_list[NFACCT_NAME]);
791 if (carg->initiate) {
793 * TODO: this will be used when daemon starts to update existing
794 * counter data if present.
796 populate_counters(cnt_name, carg);
800 (int64_t *)RTA_DATA(attr_list[NFACCT_BYTES]);
801 int bytes = be64toh(*bytes_p);
803 ++carg->serialized_counters;
804 __fill_nfacct_result(cnt_name, bytes, carg);
811 static int __post_fill_counters(void *user_data)
813 struct counter_arg *carg = user_data;
821 static void __process_network_counter(struct genl *ans,
822 struct counter_arg *carg)
824 struct netlink_serialization_params ser_params = {
827 .eval_attr = __fill_counters,
828 .post_eval_attr = __post_fill_counters,
831 netlink_serialization_command *netlink =
832 netlink_create_command(&ser_params);
834 STC_LOGE("Can not create command");
838 netlink->deserialize_answer(&(netlink->params));
841 static gboolean __process_contr_reply(GIOChannel *source,
842 GIOCondition condition,
845 int sock = g_io_channel_unix_get_fd(source);
848 stc_s *stc = stc_get_manager();
850 if ((condition & G_IO_ERR) ||
851 (condition & G_IO_HUP) ||
852 (condition & G_IO_NVAL)) {
853 /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */
855 __close_contr_sock(g_system);
860 STC_LOGE("Can't get stc data");
864 ret = read_netlink(sock,
865 &ans, sizeof(struct genl));
866 STC_LOGD("Counter data received ret [%d]", ret);
870 stc->carg->ans_len = ret;
871 __process_network_counter(&ans, stc->carg);
873 g_idle_add(__flush_apps_stats_to_database, NULL);
874 g_idle_add(__flush_rstns_counter_to_database, NULL);
879 static gboolean __update_contr_cb(void *user_data)
881 /* Here we just sent command, answer we receive in another callback */
882 stc_s *stc = stc_get_manager();
883 ret_value_msg_if(stc == NULL, STC_ERROR_FAIL, "Can't get stc data");
885 stc->carg = MALLOC0(counter_arg_s, 1);
886 stc->carg->sock = stc_monitor_get_counter_socket();
889 STC_LOGD("Get all counters");
890 nfacct_send_get_all(stc->carg);
892 /* we need to continue the timer */
896 static gboolean __rstn_tree_foreach_print(gpointer key, gpointer value,
899 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
900 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
902 __print_rstn(rstn_key, rstn_value);
906 static void __rstn_tree_printall(void)
908 g_tree_foreach(g_system->rstns, __rstn_tree_foreach_print, NULL);
911 static stc_rstn_value_s * __rstn_lookup(GTree *rstns_tree,
912 const stc_rstn_key_s *key)
914 stc_rstn_value_s *lookup;
916 ret_value_msg_if(rstns_tree == NULL, NULL, "rstns_tree is null!");
918 lookup = g_tree_lookup(rstns_tree, key);
923 static gboolean __remove_restriction(gpointer key, gpointer value,
926 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
927 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
929 /* rstn rule is already removed */
930 if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
933 __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
934 __print_rstn(rstn_key, rstn_value);
938 static gboolean __add_restriction_debug(gpointer key, gpointer value,
941 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
942 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
944 /* rstn rule is activated */
945 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
948 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
949 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
951 __process_restriction(RST_SET, rstn_key, rstn_value, data);
953 __print_rstn(rstn_key, rstn_value);
958 static gboolean __add_restriction(gpointer key, gpointer value, gpointer data)
960 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
961 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
963 /* rstn rule is activated */
964 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
967 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
968 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
970 __process_restriction(RST_SET, rstn_key, rstn_value, data);
975 static stc_error_e __rstn_tree_remove(stc_rstn_key_s *key)
977 stc_rstn_value_s *lookup_value;
979 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
981 lookup_value = __rstn_lookup(g_system->rstns, key);
983 STC_LOGE("key not found");
984 return STC_ERROR_NO_DATA;
987 __remove_restriction(key, lookup_value, NULL);
989 /* remove counter also */
990 table_counters_delete(lookup_value->restriction_id);
992 if (!g_tree_remove(g_system->rstns, key)) {
993 STC_LOGD("key not found");
994 return STC_ERROR_NO_DATA;
997 return STC_ERROR_NONE;
1000 static stc_error_e __rstn_tree_add(stc_rstn_key_s *key,
1001 stc_rstn_value_s *value, gboolean debug)
1003 stc_rstn_value_s *rstn_value;
1005 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1007 rstn_value = __rstn_lookup(g_system->rstns, key);
1009 stc_rstn_key_s *rstn_key = MALLOC0(stc_rstn_key_s, 1);
1011 STC_LOGE("rstn_key allocation failed");
1012 return STC_ERROR_OUT_OF_MEMORY;
1015 rstn_value = MALLOC0(stc_rstn_value_s, 1);
1017 STC_LOGE("rstn_value allocation failed");
1019 return STC_ERROR_OUT_OF_MEMORY;
1022 rstn_key->app_id = g_strdup(key->app_id);
1023 rstn_key->ifname = g_strdup(key->ifname);
1024 rstn_key->imsi = g_strdup(key->imsi);
1025 rstn_key->iftype = key->iftype;
1026 rstn_key->roaming = key->roaming;
1028 g_tree_insert(g_system->rstns, rstn_key, rstn_value);
1031 rstn_value->restriction_id = value->restriction_id;
1032 rstn_value->rst_state = value->rst_state;
1033 rstn_value->classid = value->classid;
1034 rstn_value->limit.in_bytes = value->limit.in_bytes;
1035 rstn_value->limit.out_bytes = value->limit.out_bytes;
1036 rstn_value->warn_limit.in_bytes = value->warn_limit.in_bytes;
1037 rstn_value->warn_limit.out_bytes = value->warn_limit.out_bytes;
1038 rstn_value->counter.in_bytes = 0;
1039 rstn_value->counter.out_bytes = 0;
1040 rstn_value->warn_limit_crossed_notified = FALSE;
1043 __add_restriction_debug(key, rstn_value, NULL);
1045 __add_restriction(key, rstn_value, NULL);
1047 return STC_ERROR_NONE;
1050 static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info,
1053 stc_cb_ret_e ret = STC_CONTINUE;
1056 stc_rstn_value_s value;
1058 memset(&key, 0, sizeof(stc_rstn_key_s));
1059 memset(&value, 0, sizeof(stc_rstn_value_s));
1061 key.app_id = g_strdup(info->app_id);
1062 key.ifname = g_strdup(info->ifname);
1063 key.imsi = g_strdup(info->imsi);
1064 key.iftype = info->iftype;
1065 key.roaming = info->roaming;
1067 value.rst_state = info->rst_state;
1068 value.restriction_id = info->restriction_id;
1070 if (value.rst_state != STC_RESTRICTION_EXCLUDED)
1071 value.classid = get_classid_by_app_id(info->app_id ?
1075 value.classid = STC_UNKNOWN_CLASSID;
1077 value.limit.in_bytes = info->rcv_limit;
1078 value.limit.out_bytes = info->send_limit;
1079 value.warn_limit.in_bytes = info->rcv_warn_limit;
1080 value.warn_limit.out_bytes = info->send_warn_limit;
1082 if (__rstn_tree_add(&key, &value, FALSE) != STC_ERROR_NONE)
1091 static void __fill_restritions_list(void)
1093 table_restrictions_foreach(__insert_restriction_cb, NULL);
1094 //__rstn_tree_printall();
1097 static gboolean __add_rstn_foreach_application(gpointer key,
1101 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1102 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1103 gchar *app_id = (gchar *)data;
1105 /* rstn rule is not for applications */
1106 if (rstn_key->app_id == NULL)
1109 /* rstn rule is not for this application */
1110 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
1113 /* rstn rule is already applied */
1114 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1117 /* add restriction to system */
1118 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1119 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1121 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1123 __print_rstn(rstn_key, rstn_value);
1128 static void __add_rstns_for_application(gchar *app_id)
1130 g_tree_foreach(g_system->rstns, __add_rstn_foreach_application,
1134 /* used only for (STC_ALL_APP/STC_BACKGROUND_APP_NAME) */
1135 static void __stc_monitor_add_application_by_app_id(const char *app_id)
1137 stc_app_key_s app_key;
1138 stc_app_value_s app_value;
1143 memset(&app_key, 0, sizeof(stc_app_key_s));
1144 memset(&app_value, 0, sizeof(stc_app_value_s));
1146 app_key.pkg_id = g_strdup(app_id);
1147 app_key.app_id = g_strdup(app_id);
1149 app_value.type = STC_APP_TYPE_NONE;
1150 app_value.processes = NULL;
1151 app_value.counter.in_bytes = 0;
1152 app_value.counter.out_bytes = 0;
1154 stc_monitor_application_add(app_key, app_value);
1156 FREE(app_key.pkg_id);
1157 FREE(app_key.app_id);
1159 __apps_tree_printall();
1162 stc_error_e stc_monitor_init(void)
1164 stc_system_s *system = MALLOC0(stc_system_s, 1);
1165 GIOChannel *gio = NULL;
1167 ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY, "stc_system_s malloc fail!");
1169 /* creating monitored application tree */
1170 system->apps = g_tree_new_full(__apps_tree_key_compare, NULL,
1171 __apps_tree_key_free,
1172 __apps_tree_value_free);
1174 system->rstns = g_tree_new_full(__rstns_tree_key_compare, NULL,
1175 __rstns_tree_key_free,
1176 __rstns_tree_value_free);
1178 /* create netlink socket for updating kernel counters */
1179 system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
1180 if (!(system->contr_sock)) {
1181 STC_LOGE("failed to open socket");
1183 return STC_ERROR_FAIL;
1186 gio = g_io_channel_unix_new(system->contr_sock);
1187 system->contr_gsource_id =
1188 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
1189 (GIOFunc) __process_contr_reply,
1191 g_io_channel_unref(gio);
1195 /* create entry for STC_ALL_APP */
1196 __stc_monitor_add_application_by_app_id(STC_ALL_APP);
1198 /* create entry for STC_BACKGROUND_APP_NAME */
1199 __stc_monitor_add_application_by_app_id(STC_BACKGROUND_APP_NAME);
1201 /* create background cgroup */
1202 g_system->background_pids = NULL;
1203 create_net_background_cgroup(g_system->background_pids);
1205 /* creating restriction rules tree */
1206 __update_contr_cb(NULL);
1208 /* registering periodic kernel counters update callback */
1209 g_system->contr_timer_id = g_timeout_add_seconds(CONTR_TIMER_INTERVAL,
1212 if (g_system->contr_timer_id == 0) {
1213 STC_LOGE("Failed to register kernel counters update timer");
1214 __close_contr_sock(g_system);
1215 return STC_ERROR_FAIL;
1218 __fill_restritions_list();
1220 return STC_ERROR_NONE;
1223 stc_error_e stc_monitor_deinit(void)
1225 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1227 /* close netlink socket for updating kernel counters */
1228 __close_contr_sock(g_system);
1230 /* remove kernel counters update timer */
1231 if (g_system->contr_timer_id > 0) {
1232 g_source_remove(g_system->contr_timer_id);
1233 g_system->contr_timer_id = 0;
1236 /* free background pid list */
1237 g_slist_free(g_system->background_pids);
1238 g_system->background_pids = NULL;
1240 /* destroy monitored application tree */
1241 g_tree_destroy(g_system->apps);
1242 g_system->apps = NULL;
1244 /* destroy restriction rules tree */
1245 g_tree_destroy(g_system->rstns);
1246 g_system->rstns = NULL;
1250 return STC_ERROR_NONE;
1253 stc_error_e stc_monitor_application_add(const stc_app_key_s app_key,
1254 const stc_app_value_s app_value)
1256 stc_error_e ret = STC_ERROR_NONE;
1258 stc_app_value_s *value;
1259 stc_app_value_s *lookup;
1261 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1263 lookup = __application_lookup(g_system->apps, &app_key);
1265 STC_LOGD("app_key already present");
1266 return STC_ERROR_NONE;
1269 key = MALLOC0(stc_app_key_s, 1);
1271 STC_LOGE("key allocation failed");
1272 return STC_ERROR_OUT_OF_MEMORY;
1275 value = MALLOC0(stc_app_value_s, 1);
1277 STC_LOGE("value allocation failed");
1279 return STC_ERROR_OUT_OF_MEMORY;
1282 key->app_id = g_strdup(app_key.app_id);
1283 key->pkg_id = g_strdup(app_key.pkg_id);
1285 value->type = app_value.type;
1286 value->counter.in_bytes = app_value.counter.in_bytes;
1287 value->counter.out_bytes = app_value.counter.out_bytes;
1289 value->processes = g_tree_new_full(__processes_tree_key_compare, NULL,
1290 __processes_tree_key_free,
1291 __processes_tree_value_free);
1293 /* create cgroup and update classid */
1294 value->classid = get_classid_by_app_id(app_key.app_id, TRUE);
1296 g_tree_insert(g_system->apps, key, value);
1298 /* add nfacct rule for this classid */
1299 __add_application_monitor(key, value, stc_get_default_connection());
1300 __add_rstns_for_application(app_key.app_id);
1305 stc_error_e stc_monitor_process_add(const stc_app_key_s app_key,
1306 const stc_process_key_s proc_key,
1307 const stc_process_value_s proc_value)
1309 stc_error_e ret = STC_ERROR_NONE;
1310 stc_app_value_s *app_lookup;
1311 stc_process_key_s *key;
1312 stc_process_value_s *value;
1313 stc_process_value_s *proc_lookup;
1315 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1317 app_lookup = __application_lookup(g_system->apps, &app_key);
1319 STC_LOGD("app_key not found");
1320 return STC_ERROR_FAIL;
1323 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1325 STC_LOGD("proc_key already present");
1326 return STC_ERROR_NONE;
1329 key = MALLOC0(stc_process_key_s, 1);
1331 STC_LOGE("key allocation failed");
1332 return STC_ERROR_OUT_OF_MEMORY;
1335 value = MALLOC0(stc_process_value_s, 1);
1337 STC_LOGE("value allocation failed");
1339 return STC_ERROR_OUT_OF_MEMORY;
1342 key->pid = proc_key.pid;
1344 value->ground = proc_value.ground;
1346 g_tree_insert(app_lookup->processes, key, value);
1348 /* add pid to application cgroup */
1349 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1351 /* add pid to background cgroup if gound state is background */
1352 if (proc_value.ground == STC_APP_STATE_BACKGROUND)
1353 add_pid_to_background_cgroup(proc_key.pid);
1358 stc_error_e stc_monitor_process_remove(pid_t pid)
1360 stc_error_e ret = STC_ERROR_NONE;
1361 stc_process_key_s proc_key = {
1365 remove_pid_context_s context = {
1367 .proc_key = &proc_key,
1368 .entry_removed = FALSE,
1371 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1373 /* remove pid from background cgroup */
1374 remove_pid_from_background_cgroup(pid);
1376 g_tree_foreach(g_system->apps, __apps_tree_foreach_remove_pid,
1379 if (context.entry_removed)
1380 __application_remove_if_empty(context.app_key);
1385 stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key,
1386 const stc_process_key_s proc_key,
1387 stc_app_state_e ground)
1389 stc_error_e ret = STC_ERROR_NONE;
1390 stc_app_value_s *app_lookup;
1391 stc_process_value_s *proc_lookup;
1393 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1395 app_lookup = __application_lookup(g_system->apps, &app_key);
1397 STC_LOGD("app_key not found");
1398 return STC_ERROR_FAIL;
1401 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1403 STC_LOGD("proc_key not found");
1404 return STC_ERROR_FAIL;
1407 if (proc_lookup->ground != ground)
1408 proc_lookup->ground = ground;
1410 if (ground == STC_APP_STATE_BACKGROUND)
1411 add_pid_to_background_cgroup(proc_key.pid);
1413 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1418 void stc_monitor_update_rstn_by_default_connection(void *data)
1420 static default_connection_s old_connection;
1421 default_connection_s *new_connection = (default_connection_s *)data;
1423 if (old_connection.path != NULL) {
1425 g_tree_foreach(g_system->apps,
1426 __remove_application_monitor,
1427 (gpointer)&old_connection);
1429 if (g_system->rstns)
1430 g_tree_foreach(g_system->rstns,
1431 __remove_restriction,
1432 (gpointer)&old_connection);
1435 FREE(old_connection.path);
1436 FREE(old_connection.ifname);
1437 old_connection.type = 0;
1438 old_connection.roaming = 0;
1440 if (new_connection != NULL && new_connection->path != NULL) {
1442 g_tree_foreach(g_system->apps,
1443 __add_application_monitor,
1444 (gpointer)new_connection);
1446 if (g_system->rstns)
1447 g_tree_foreach(g_system->rstns, __add_restriction,
1450 old_connection.path = g_strdup(new_connection->path);
1451 old_connection.ifname = g_strdup(new_connection->ifname);
1452 old_connection.type = new_connection->type;
1453 old_connection.roaming = new_connection->roaming;
1457 stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info)
1462 stc_rstn_value_s value;
1464 memset(&key, 0, sizeof(stc_rstn_key_s));
1465 memset(&value, 0, sizeof(stc_rstn_value_s));
1467 key.app_id = g_strdup(info->app_id);
1468 key.ifname = g_strdup(info->ifname);
1469 key.imsi = g_strdup(info->imsi);
1470 key.iftype = info->iftype;
1471 key.roaming = info->roaming;
1473 value.rst_state = info->rst_state;
1474 value.restriction_id = info->restriction_id;
1476 if (value.rst_state != STC_RESTRICTION_EXCLUDED)
1477 value.classid = get_classid_by_app_id(info->app_id ?
1481 value.classid = STC_UNKNOWN_CLASSID;
1483 value.limit.in_bytes = info->rcv_limit;
1484 value.limit.out_bytes = info->send_limit;
1485 value.warn_limit.in_bytes = info->rcv_warn_limit;
1486 value.warn_limit.out_bytes = info->send_warn_limit;
1488 ret = __rstn_tree_add(&key, &value, TRUE);
1496 stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info)
1500 stc_rstn_key_s key = {
1501 .app_id = g_strdup(info->app_id),
1502 .ifname = g_strdup(info->ifname),
1503 .imsi = g_strdup(info->imsi),
1504 .iftype = info->iftype,
1505 .roaming = info->roaming,
1508 ret = __rstn_tree_remove(&key);
1516 int stc_monitor_get_counter_socket(void)
1518 return g_system->contr_sock;