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"
23 #include "helper-cgroup.h"
25 #include "table-statistics.h"
26 #include "table-counters.h"
27 #include "stc-monitor.h"
30 stc_app_key_s *app_key;
31 stc_process_key_s *proc_key;
32 gboolean entry_removed;
33 } remove_pid_context_s;
36 struct nfacct_rule *counter;
38 gboolean in_limit_reached;
39 gboolean out_limit_reached;
40 } classid_bytes_context_s;
42 static stc_system_s *g_system = NULL;
44 static nfacct_rule_jump __get_jump_by_intend(struct nfacct_rule *counter)
46 if (counter->intend == NFACCT_WARN)
47 return NFACCT_JUMP_ACCEPT;
48 else if (counter->intend == NFACCT_BLOCK)
49 return NFACCT_JUMP_REJECT;
51 return NFACCT_JUMP_UNKNOWN;
54 static stc_error_e __add_iptables_in(struct nfacct_rule *counter)
56 return produce_net_rule(counter, 0, 0,
57 NFACCT_ACTION_INSERT, __get_jump_by_intend(counter),
61 static stc_error_e __add_iptables_out(struct nfacct_rule *counter)
63 return produce_net_rule(counter, 0, 0,
64 NFACCT_ACTION_INSERT, __get_jump_by_intend(counter),
68 static stc_error_e __del_iptables_in(struct nfacct_rule *counter)
70 return produce_net_rule(counter, 0, 0,
71 NFACCT_ACTION_DELETE, __get_jump_by_intend(counter),
75 static stc_error_e __del_iptables_out(struct nfacct_rule *counter)
77 return produce_net_rule(counter, 0, 0,
78 NFACCT_ACTION_DELETE, __get_jump_by_intend(counter),
82 static int __processes_tree_key_compare(gconstpointer a, gconstpointer b,
83 gpointer UNUSED user_data)
85 stc_process_key_s *key_a = (stc_process_key_s *)a;
86 stc_process_key_s *key_b = (stc_process_key_s *)b;
88 return key_a->pid - key_b->pid;
91 static void __processes_tree_value_free(gpointer data)
93 stc_process_value_s *value = (stc_process_value_s *)data;
98 static void __processes_tree_key_free(gpointer data)
100 stc_process_key_s *key = (stc_process_key_s *)data;
105 static int __apps_tree_key_compare(gconstpointer a, gconstpointer b,
106 gpointer UNUSED user_data)
108 stc_app_key_s *key_a = (stc_app_key_s *)a;
109 stc_app_key_s *key_b = (stc_app_key_s *)b;
112 ret = g_strcmp0(key_a->pkg_id, key_b->pkg_id);
116 return g_strcmp0(key_a->app_id, key_b->app_id);
119 static void __apps_tree_value_free(gpointer data)
121 stc_app_value_s *value = (stc_app_value_s *)data;
123 g_tree_destroy(value->processes);
124 value->processes = NULL;
129 static void __apps_tree_key_free(gpointer data)
131 stc_app_key_s *key = (stc_app_key_s *)data;
138 static int __rstns_tree_key_compare(gconstpointer a, gconstpointer b,
139 gpointer UNUSED user_data)
141 stc_rstn_key_s *key_a = (stc_rstn_key_s *)a;
142 stc_rstn_key_s *key_b = (stc_rstn_key_s *)b;
145 ret = g_strcmp0(key_a->app_id, key_b->app_id);
149 ret = g_strcmp0(key_a->ifname, key_b->ifname);
153 ret = g_strcmp0(key_a->imsi, key_b->imsi);
157 ret = key_a->iftype - key_b->iftype;
164 static void __rstns_tree_value_free(gpointer data)
166 stc_rstn_value_s *value = (stc_rstn_value_s *)data;
171 static void __rstns_tree_key_free(gpointer data)
173 stc_rstn_key_s *key = (stc_rstn_key_s *)data;
181 static gboolean __processes_tree_foreach_print(gpointer key, gpointer value,
184 stc_process_key_s *proc_key = (stc_process_key_s *)key;
185 stc_process_value_s *proc_value = (stc_process_value_s *)value;
187 STC_LOGD("Process entry => PID [%d], Ground state [%d]",
188 proc_key->pid, proc_value->ground);
192 static void __processes_tree_printall(GTree *processes)
194 g_tree_foreach(processes, __processes_tree_foreach_print, NULL);
197 static gboolean __apps_tree_foreach_print(gpointer key, gpointer value,
200 stc_app_key_s *app_key = (stc_app_key_s *)key;
201 stc_app_value_s *app_value = (stc_app_value_s *)value;
203 STC_LOGD("Application info => Pkg ID [%s], App ID [%s],"
204 " Type [%d], classid [%d],"
205 " counter [ in (%lld), out (%lld)]",
206 app_key->pkg_id, app_key->app_id,
207 app_value->type, app_value->classid,
208 app_value->data_usage.in_bytes, app_value->data_usage.out_bytes);
210 __processes_tree_printall(app_value->processes);
215 static void __apps_tree_printall(void)
217 g_tree_foreach(g_system->apps, __apps_tree_foreach_print, NULL);
221 static gboolean __apps_tree_foreach_remove_pid(gpointer key, gpointer value,
224 remove_pid_context_s *context = (remove_pid_context_s *)data;
225 stc_app_value_s *app_value = (stc_app_value_s *)value;
227 if (!g_tree_remove(app_value->processes, context->proc_key)) {
228 STC_LOGD("key not found");
232 context->entry_removed = TRUE;
233 context->app_key = (stc_app_key_s *)key;
238 static stc_app_value_s * __application_lookup(GTree *apps,
239 const stc_app_key_s *key)
241 stc_app_value_s *lookup;
243 ret_value_msg_if(apps == NULL, NULL, "apps is null!");
245 lookup = g_tree_lookup(apps, key);
250 static stc_process_value_s * __process_lookup(GTree *processes,
251 const stc_process_key_s *key)
253 stc_process_value_s *lookup;
255 ret_value_msg_if(processes == NULL, NULL, "processes is null!");
257 lookup = g_tree_lookup(processes, key);
262 static gboolean __processes_tree_check_empty(gpointer key, gpointer value,
265 guint *pid_count = (guint *)data;
270 static gboolean __add_application_monitor(gpointer key, gpointer value,
273 stc_app_value_s *app_value = (stc_app_value_s *)value;
274 default_connection_s *connection = (default_connection_s *)data;
275 stc_s *stc = stc_get_manager();
277 if (stc && connection && connection->ifname) {
278 struct nfacct_rule counter;
281 stc->carg = MALLOC0(counter_arg_s, 1);
282 stc->carg->sock = stc_monitor_get_counter_socket();
285 memset(&counter, 0, sizeof(struct nfacct_rule));
287 counter.carg = stc->carg;
288 counter.classid = app_value->classid;
289 counter.intend = NFACCT_COUNTER;
290 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
292 __add_iptables_in(&counter);
293 __add_iptables_out(&counter);
299 static gboolean __remove_application_monitor(gpointer key, gpointer value,
302 stc_app_value_s *app_value = (stc_app_value_s *)value;
303 default_connection_s *connection = (default_connection_s *)data;
304 stc_s *stc = stc_get_manager();
306 if (stc && connection && connection->ifname) {
307 struct nfacct_rule counter;
310 stc->carg = MALLOC0(counter_arg_s, 1);
311 stc->carg->sock = stc_monitor_get_counter_socket();
314 memset(&counter, 0, sizeof(struct nfacct_rule));
316 counter.carg = stc->carg;
317 counter.classid = app_value->classid;
318 counter.intend = NFACCT_COUNTER;
319 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
321 __del_iptables_in(&counter);
322 __del_iptables_out(&counter);
328 static void __print_rstn(stc_rstn_key_s *rstn_key, stc_rstn_value_s *rstn_value)
330 STC_LOGI("rstn info => rstn_id [%llu], "
331 "app_id [%s], classid [%lu], ifname [%s], "
332 "iftype [%d], rst_state [%d], "
333 "limit [ in (%lld), out (%lld)], "
334 "warn_limit [ in (%lld), out (%lld)], "
335 "counter [ in (%lld), out (%lld)], "
336 "roaming [%d], imsi [%s]",
337 rstn_value->restriction_id,
338 rstn_key->app_id, rstn_value->classid , rstn_key->ifname,
339 rstn_key->iftype, rstn_value->rst_state,
340 rstn_value->limit.in_bytes, rstn_value->limit.out_bytes,
341 rstn_value->warn_limit.in_bytes,
342 rstn_value->warn_limit.out_bytes,
343 rstn_value->counter.in_bytes, rstn_value->counter.out_bytes,
344 rstn_key->roaming, rstn_key->imsi);
347 static void __process_restriction(enum traffic_restriction_type rst_type,
348 stc_rstn_key_s *rstn_key,
349 stc_rstn_value_s *rstn_value, void *data)
351 stc_data_counter_s effective_limit, effective_warn_limit;
352 default_connection_s *old_connection = (default_connection_s *)data;
353 default_connection_s *connection = NULL;
355 if (old_connection != NULL)
356 connection = old_connection;
358 connection = stc_get_default_connection();
360 /* no default ifname */
361 if (connection->ifname == NULL)
364 /* rstn not applicable for this interface */
365 if (rstn_key->ifname != NULL && g_strcmp0("", rstn_key->ifname) != 0 &&
366 g_strcmp0(connection->ifname, rstn_key->ifname) != 0)
369 /* classid is invalid */
370 if (rstn_value->classid == STC_UNKNOWN_CLASSID)
373 effective_limit.out_bytes = rstn_value->limit.out_bytes;
374 effective_limit.in_bytes = rstn_value->limit.in_bytes;
375 effective_warn_limit.out_bytes = rstn_value->warn_limit.out_bytes;
376 effective_warn_limit.in_bytes = rstn_value->warn_limit.in_bytes;
378 if (rst_type == RST_SET) {
379 /* TODO: Change this to runtime memory */
380 table_counters_info info;
382 memset(&info, 0, sizeof(table_counters_info));
383 table_counters_get(rstn_value->restriction_id, &info);
385 effective_limit.out_bytes -= info.sent_bytes;
386 effective_limit.in_bytes -= info.rcv_bytes;
387 effective_warn_limit.out_bytes -= info.sent_bytes;
388 effective_warn_limit.in_bytes -= info.rcv_bytes;
390 if (effective_limit.in_bytes < 0) {
391 effective_limit.in_bytes = 0;
392 rstn_value->in_limit_reached = TRUE;
395 if (effective_limit.out_bytes < 0) {
396 effective_limit.out_bytes = 0;
397 rstn_value->out_limit_reached = TRUE;
400 if (effective_warn_limit.in_bytes < 0)
401 effective_warn_limit.in_bytes = 0;
403 if (effective_warn_limit.out_bytes < 0)
404 effective_warn_limit.out_bytes = 0;
405 STC_LOGD("datausage [in: %lld, out: %lld]",
406 info.rcv_bytes, info.sent_bytes);
409 STC_LOGD("rstn_id [%llu], effective limit [in: %lld, out: %lld], "
410 "effective warn limit [in: %lld, out: %lld], "
411 "datausage [in: %lld, out: %lld]",
412 rstn_value->restriction_id,
413 effective_limit.in_bytes, effective_limit.out_bytes,
414 effective_warn_limit.in_bytes, effective_warn_limit.out_bytes);
416 send_net_restriction(rst_type,
420 effective_limit.out_bytes,
421 effective_limit.in_bytes,
422 effective_warn_limit.out_bytes,
423 effective_warn_limit.in_bytes,
427 rstn_value->rst_state = STC_RESTRICTION_ACTIVATED;
428 rstn_value->in_limit_reached = FALSE;
429 rstn_value->out_limit_reached = FALSE;
435 rstn_value->rst_state = STC_RESTRICTION_REMOVED;
436 rstn_value->in_limit_reached = FALSE;
437 rstn_value->out_limit_reached = FALSE;
444 static gboolean __remove_rstns_foreach_application(gpointer key,
448 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
449 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
450 gchar *app_id = (gchar *)data;
452 /* rstn rule is not for applications */
453 if (rstn_key->app_id == NULL)
456 /* rstn rule is not for this application */
457 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
460 /* rstn rule is already removed */
461 if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
464 /* remove restriction from system */
465 __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
467 __print_rstn(rstn_key, rstn_value);
472 static void __remove_rstns_for_application(gchar *app_id)
474 g_tree_foreach(g_system->rstns, __remove_rstns_foreach_application,
478 static stc_error_e __application_remove_if_empty(const stc_app_key_s *app_key)
480 stc_error_e ret = STC_ERROR_NONE;
482 stc_app_value_s *lookup;
484 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
486 lookup = __application_lookup(g_system->apps, app_key);
488 STC_LOGE("app_key not found");
489 return STC_ERROR_NO_DATA;
492 g_tree_foreach(lookup->processes, __processes_tree_check_empty,
496 /* remove nfacct rule for this classid */
497 __remove_application_monitor((gpointer) app_key, lookup,
498 stc_get_default_connection());
499 __remove_rstns_for_application(app_key->app_id);
502 if (!g_tree_remove(g_system->apps, app_key)) {
503 ret = STC_ERROR_NO_DATA;
504 STC_LOGE("key not found");
510 static stc_error_e __close_contr_sock(stc_system_s *system)
512 ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
514 /* close netlink socket for updating kernel counters */
515 if (g_system->contr_sock != -1) {
516 close(g_system->contr_sock);
517 g_system->contr_sock = -1;
520 if (g_system->contr_gsource_id != 0) {
521 g_source_remove(g_system->contr_gsource_id);
522 g_system->contr_gsource_id = 0;
525 return STC_ERROR_NONE;
528 static gboolean __rstn_counter_update_foreach_classid(gpointer key,
532 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
533 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
534 classid_bytes_context_s *context = (classid_bytes_context_s *)data;
536 if (context->counter->intend != NFACCT_COUNTER)
537 goto try_next_callback;
539 if (rstn_value->classid != context->counter->classid)
540 goto try_next_callback;
542 if (rstn_value->in_limit_reached == TRUE) {
543 context->in_limit_reached = TRUE;
544 goto try_next_callback;
547 if (rstn_value->out_limit_reached == TRUE) {
548 context->out_limit_reached = TRUE;
549 goto try_next_callback;
552 switch (context->counter->iotype) {
553 case NFACCT_COUNTER_IN:
554 rstn_value->counter.in_bytes += context->bytes;
556 if (rstn_value->counter.in_bytes >= rstn_value->warn_limit.in_bytes
557 && rstn_value->warn_limit_crossed_notified == FALSE) {
559 stc_s *stc = (stc_s *)stc_get_manager();
560 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
562 rv = stc_manager_dbus_emit_signal(stc->connection,
563 STC_DBUS_SERVICE_RESTRICTION_PATH,
564 STC_DBUS_INTERFACE_RESTRICTION,
565 "WarnThresholdCrossed",
566 g_variant_new("(s)", rstn_key->app_id));
568 rstn_value->warn_limit_crossed_notified = TRUE;
571 /* block immediately */
572 if (rstn_value->counter.in_bytes >= rstn_value->limit.in_bytes) {
573 context->counter->intend = NFACCT_BLOCK;
574 __del_iptables_in(context->counter);
575 __add_iptables_in(context->counter);
576 context->counter->intend = NFACCT_COUNTER;
577 rstn_value->in_limit_reached = TRUE;
579 if (rstn_value->rstn_limit_crossed_notified == FALSE) {
581 stc_s *stc = (stc_s *)stc_get_manager();
582 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
584 rv = stc_manager_dbus_emit_signal(stc->connection,
585 STC_DBUS_SERVICE_RESTRICTION_PATH,
586 STC_DBUS_INTERFACE_RESTRICTION,
587 "RestrictionThresholdCrossed",
588 g_variant_new("(s)", rstn_key->app_id));
590 rstn_value->rstn_limit_crossed_notified = TRUE;
594 g_system->rstns_tree_updated = TRUE;
595 __print_rstn(rstn_key, rstn_value);
597 case NFACCT_COUNTER_OUT:
598 rstn_value->counter.out_bytes += context->bytes;
600 if (rstn_value->counter.out_bytes >= rstn_value->limit.out_bytes
601 && rstn_value->warn_limit_crossed_notified == FALSE) {
603 stc_s *stc = (stc_s *)stc_get_manager();
604 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
606 rv = stc_manager_dbus_emit_signal(stc->connection,
607 STC_DBUS_SERVICE_RESTRICTION_PATH,
608 STC_DBUS_INTERFACE_RESTRICTION,
609 "WarnThresholdCrossed",
610 g_variant_new("(s)", rstn_key->app_id));
612 rstn_value->warn_limit_crossed_notified = TRUE;
615 /* block immediately */
616 if (rstn_value->counter.out_bytes >= rstn_value->limit.out_bytes) {
617 context->counter->intend = NFACCT_BLOCK;
618 __del_iptables_out(context->counter);
619 __add_iptables_out(context->counter);
620 context->counter->intend = NFACCT_COUNTER;
621 rstn_value->out_limit_reached = TRUE;
623 if (rstn_value->rstn_limit_crossed_notified == FALSE) {
625 stc_s *stc = (stc_s *)stc_get_manager();
626 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
628 rv = stc_manager_dbus_emit_signal(stc->connection,
629 STC_DBUS_SERVICE_RESTRICTION_PATH,
630 STC_DBUS_INTERFACE_RESTRICTION,
631 "RestrictionThresholdCrossed",
632 g_variant_new("(s)", rstn_key->app_id));
634 rstn_value->rstn_limit_crossed_notified = TRUE;
638 g_system->rstns_tree_updated = TRUE;
639 __print_rstn(rstn_key, rstn_value);
642 STC_LOGE("unknown iotype");
650 static gboolean __update_app_statistics(gpointer key, gpointer value,
653 stc_app_key_s *app_key = (stc_app_key_s *)key;
654 stc_app_value_s *app_value = (stc_app_value_s *)value;
655 time_t *touch_time = (time_t *)data;
656 stc_db_classid_iftype_key stat_key;
657 stc_db_app_stats stat;
658 char *default_ifname = stc_default_connection_get_ifname();
660 memset(&stat_key, 0, sizeof(stc_db_classid_iftype_key));
661 memset(&stat, 0 , sizeof(stc_db_app_stats));
663 stat_key.classid = app_value->classid;
664 stat_key.iftype = stc_default_connection_get_type();
665 if (STC_IFACE_DATACALL == stat_key.iftype)
666 stat_key.imsi = g_strdup("unknown");
668 stat_key.imsi = g_strdup("noneimsi");
669 g_strlcpy(stat_key.ifname, default_ifname, MAX_IFACE_LENGTH);
671 stat.app_id = g_strdup(app_key->app_id);
672 stat.snd_count = app_value->counter.out_bytes;
673 stat.rcv_count = app_value->counter.in_bytes;
676 stat.is_roaming = stc_default_connection_get_roaming();
677 stat.ground = STC_APP_STATE_UNKNOWN;
679 table_statistics_insert(&stat_key, &stat, *touch_time);
681 app_value->counter.out_bytes = 0;
682 app_value->counter.in_bytes = 0;
686 FREE(default_ifname);
691 static gboolean __flush_apps_stats_to_database(gpointer user_data)
693 time_t current_time = time(0);
695 if (g_system->apps_tree_updated == FALSE)
696 return G_SOURCE_REMOVE;
698 g_system->apps_tree_updated = FALSE;
701 g_tree_foreach(g_system->apps,
702 __update_app_statistics,
705 STC_LOGI("Flushed app stats to database");
706 return G_SOURCE_REMOVE;
709 static gboolean __update_counter_statistics(gpointer key, gpointer value,
712 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
713 table_counters_info info = {
714 .restriction_id = rstn_value->restriction_id,
715 .sent_bytes = rstn_value->counter.out_bytes,
716 .rcv_bytes = rstn_value->counter.in_bytes
719 table_counters_update_counters(&info);
724 static gboolean __flush_rstns_counter_to_database(gpointer user_data)
726 time_t current_time = time(0);
728 if (g_system->rstns_tree_updated == FALSE)
729 return G_SOURCE_REMOVE;
731 g_system->rstns_tree_updated = FALSE;
734 g_tree_foreach(g_system->rstns,
735 __update_counter_statistics,
738 STC_LOGI("Flushed rstns counters to database");
739 return G_SOURCE_REMOVE;
742 static gboolean __apps_counter_update_foreach_classid(gpointer key,
746 stc_app_key_s *app_key = (stc_app_key_s *)key;
747 stc_app_value_s *app_value = (stc_app_value_s *)value;
748 classid_bytes_context_s *context = (classid_bytes_context_s *)data;
750 if (context->counter->intend != NFACCT_COUNTER)
751 goto try_next_callback;
753 if (app_value->classid != context->counter->classid)
754 goto try_next_callback;
756 switch (context->counter->iotype) {
757 case NFACCT_COUNTER_IN:
758 app_value->data_usage.in_bytes += context->bytes;
759 app_value->counter.in_bytes = context->bytes;
760 g_system->apps_tree_updated = TRUE;
762 __apps_tree_foreach_print(app_key, app_value, NULL);
764 case NFACCT_COUNTER_OUT:
765 app_value->data_usage.out_bytes += context->bytes;
766 app_value->counter.out_bytes = context->bytes;
767 g_system->apps_tree_updated = TRUE;
769 __apps_tree_foreach_print(app_key, app_value, NULL);
772 STC_LOGE("unknown iotype");
779 static void __fill_nfacct_result(char *cnt_name, int64_t bytes,
780 struct counter_arg *carg)
782 struct nfacct_rule counter = {
789 classid_bytes_context_s context = {
792 .in_limit_reached = FALSE,
793 .out_limit_reached = FALSE
796 STC_LOGD("cnt_name %s", cnt_name);
798 if (!recreate_counter_by_name(cnt_name, &counter)) {
799 STC_LOGE("Can't parse counter name %s", cnt_name);
803 STC_LOGI("classid %lu, iftype %u, iotype %d, intend %d, ifname %s, bytes %lld",
804 context.counter->classid, context.counter->iftype,
805 context.counter->iotype, context.counter->intend,
806 context.counter->ifname, context.bytes);
809 g_tree_foreach(g_system->rstns,
810 __rstn_counter_update_foreach_classid,
814 g_tree_foreach(g_system->apps,
815 __apps_counter_update_foreach_classid,
819 static int __fill_counters(struct rtattr *attr_list[__NFACCT_MAX],
822 struct counter_arg *carg = user_data;
823 char *cnt_name = (char *)RTA_DATA(attr_list[NFACCT_NAME]);
824 if (carg->initiate) {
826 * TODO: this will be used when daemon starts to update existing
827 * counter data if present.
829 populate_counters(cnt_name, carg);
833 (int64_t *)RTA_DATA(attr_list[NFACCT_BYTES]);
834 int bytes = be64toh(*bytes_p);
836 ++carg->serialized_counters;
837 __fill_nfacct_result(cnt_name, bytes, carg);
844 static int __post_fill_counters(void *user_data)
846 struct counter_arg *carg = user_data;
854 static void __process_network_counter(struct genl *ans,
855 struct counter_arg *carg)
857 struct netlink_serialization_params ser_params = {
860 .eval_attr = __fill_counters,
861 .post_eval_attr = __post_fill_counters,
864 netlink_serialization_command *netlink =
865 netlink_create_command(&ser_params);
867 STC_LOGE("Can not create command");
871 netlink->deserialize_answer(&(netlink->params));
874 static gboolean __process_contr_reply(GIOChannel *source,
875 GIOCondition condition,
878 int sock = g_io_channel_unix_get_fd(source);
881 stc_s *stc = stc_get_manager();
883 if ((condition & G_IO_ERR) ||
884 (condition & G_IO_HUP) ||
885 (condition & G_IO_NVAL)) {
886 /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */
888 __close_contr_sock(g_system);
893 STC_LOGE("Can't get stc data");
897 ret = read_netlink(sock,
898 &ans, sizeof(struct genl));
899 STC_LOGD("Counter data received ret [%d]", ret);
903 stc->carg->ans_len = ret;
904 __process_network_counter(&ans, stc->carg);
906 g_idle_add(__flush_apps_stats_to_database, NULL);
907 g_idle_add(__flush_rstns_counter_to_database, NULL);
912 static gboolean __update_contr_cb(void *user_data)
914 /* Here we just sent command, answer we receive in another callback */
915 stc_s *stc = stc_get_manager();
916 ret_value_msg_if(stc == NULL, STC_ERROR_FAIL, "Can't get stc data");
918 stc->carg = MALLOC0(counter_arg_s, 1);
919 stc->carg->sock = stc_monitor_get_counter_socket();
922 STC_LOGD("Get all counters");
923 nfacct_send_get_all(stc->carg);
925 /* we need to continue the timer */
929 static gboolean __rstn_tree_foreach_print(gpointer key, gpointer value,
932 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
933 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
935 __print_rstn(rstn_key, rstn_value);
939 static void __rstn_tree_printall(void)
941 g_tree_foreach(g_system->rstns, __rstn_tree_foreach_print, NULL);
944 static stc_rstn_value_s * __rstn_lookup(GTree *rstns_tree,
945 const stc_rstn_key_s *key)
947 stc_rstn_value_s *lookup;
949 ret_value_msg_if(rstns_tree == NULL, NULL, "rstns_tree is null!");
951 lookup = g_tree_lookup(rstns_tree, key);
956 static gboolean __remove_restriction(gpointer key, gpointer value,
959 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
960 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
962 /* rstn rule is already removed */
963 if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
966 __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
967 __print_rstn(rstn_key, rstn_value);
971 static gboolean __add_restriction_debug(gpointer key, gpointer value,
974 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
975 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
977 /* rstn rule is activated */
978 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
981 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
982 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
984 __process_restriction(RST_SET, rstn_key, rstn_value, data);
986 __print_rstn(rstn_key, rstn_value);
991 static gboolean __add_restriction(gpointer key, gpointer value, gpointer data)
993 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
994 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
996 /* rstn rule is activated */
997 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1000 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1001 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1003 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1008 static stc_error_e __rstn_tree_remove(stc_rstn_key_s *key)
1010 stc_rstn_value_s *lookup_value;
1012 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1014 lookup_value = __rstn_lookup(g_system->rstns, key);
1015 if (!lookup_value) {
1016 STC_LOGE("key not found");
1017 return STC_ERROR_NO_DATA;
1020 __remove_restriction(key, lookup_value, NULL);
1022 /* remove counter also */
1023 table_counters_delete(lookup_value->restriction_id);
1025 if (!g_tree_remove(g_system->rstns, key)) {
1026 STC_LOGD("key not found");
1027 return STC_ERROR_NO_DATA;
1030 return STC_ERROR_NONE;
1033 static stc_error_e __rstn_tree_add(stc_rstn_key_s *key,
1034 stc_rstn_value_s *value, gboolean debug)
1036 stc_rstn_value_s *rstn_value;
1038 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1040 rstn_value = __rstn_lookup(g_system->rstns, key);
1042 stc_rstn_key_s *rstn_key = MALLOC0(stc_rstn_key_s, 1);
1044 STC_LOGE("rstn_key allocation failed");
1045 return STC_ERROR_OUT_OF_MEMORY;
1048 rstn_value = MALLOC0(stc_rstn_value_s, 1);
1050 STC_LOGE("rstn_value allocation failed");
1052 return STC_ERROR_OUT_OF_MEMORY;
1055 rstn_key->app_id = g_strdup(key->app_id);
1056 rstn_key->ifname = g_strdup(key->ifname);
1057 rstn_key->imsi = g_strdup(key->imsi);
1058 rstn_key->iftype = key->iftype;
1059 rstn_key->roaming = key->roaming;
1061 g_tree_insert(g_system->rstns, rstn_key, rstn_value);
1064 rstn_value->restriction_id = value->restriction_id;
1065 rstn_value->rst_state = value->rst_state;
1066 rstn_value->classid = value->classid;
1067 rstn_value->limit.in_bytes = value->limit.in_bytes;
1068 rstn_value->limit.out_bytes = value->limit.out_bytes;
1069 rstn_value->warn_limit.in_bytes = value->warn_limit.in_bytes;
1070 rstn_value->warn_limit.out_bytes = value->warn_limit.out_bytes;
1071 rstn_value->counter.in_bytes = 0;
1072 rstn_value->counter.out_bytes = 0;
1073 rstn_value->warn_limit_crossed_notified = FALSE;
1074 rstn_value->rstn_limit_crossed_notified = FALSE;
1077 __add_restriction_debug(key, rstn_value, NULL);
1079 __add_restriction(key, rstn_value, NULL);
1081 return STC_ERROR_NONE;
1084 static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info,
1087 stc_cb_ret_e ret = STC_CONTINUE;
1090 stc_rstn_value_s value;
1092 memset(&key, 0, sizeof(stc_rstn_key_s));
1093 memset(&value, 0, sizeof(stc_rstn_value_s));
1095 key.app_id = g_strdup(info->app_id);
1096 key.ifname = g_strdup(info->ifname);
1097 key.imsi = g_strdup(info->imsi);
1098 key.iftype = info->iftype;
1099 key.roaming = info->roaming;
1101 value.rst_state = info->rst_state;
1102 value.restriction_id = info->restriction_id;
1104 if (value.rst_state != STC_RESTRICTION_EXCLUDED && info->app_id)
1105 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1107 value.classid = STC_UNKNOWN_CLASSID;
1109 value.limit.in_bytes = info->rcv_limit;
1110 value.limit.out_bytes = info->send_limit;
1111 value.warn_limit.in_bytes = info->rcv_warn_limit;
1112 value.warn_limit.out_bytes = info->send_warn_limit;
1114 if (__rstn_tree_add(&key, &value, FALSE) != STC_ERROR_NONE)
1123 static void __fill_restritions_list(void)
1125 table_restrictions_foreach(__insert_restriction_cb, NULL);
1126 //__rstn_tree_printall();
1129 static gboolean __add_rstn_foreach_application(gpointer key,
1133 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1134 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1135 gchar *app_id = (gchar *)data;
1137 /* rstn rule is not for applications */
1138 if (rstn_key->app_id == NULL)
1141 /* rstn rule is not for this application */
1142 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
1145 /* rstn rule is already applied */
1146 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1149 /* add restriction to system */
1150 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1151 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1153 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1155 __print_rstn(rstn_key, rstn_value);
1160 static void __add_rstns_for_application(gchar *app_id)
1162 g_tree_foreach(g_system->rstns, __add_rstn_foreach_application,
1166 stc_error_e stc_monitor_init(void)
1168 stc_system_s *system = MALLOC0(stc_system_s, 1);
1169 GIOChannel *gio = NULL;
1171 ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY, "stc_system_s malloc fail!");
1173 /* initializing cgroups */
1176 /* creating monitored application tree */
1177 system->apps = g_tree_new_full(__apps_tree_key_compare, NULL,
1178 __apps_tree_key_free,
1179 __apps_tree_value_free);
1181 system->rstns = g_tree_new_full(__rstns_tree_key_compare, NULL,
1182 __rstns_tree_key_free,
1183 __rstns_tree_value_free);
1185 /* create netlink socket for updating kernel counters */
1186 system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
1187 if (!(system->contr_sock)) {
1188 STC_LOGE("failed to open socket");
1190 return STC_ERROR_FAIL;
1193 gio = g_io_channel_unix_new(system->contr_sock);
1194 system->contr_gsource_id =
1195 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
1196 (GIOFunc) __process_contr_reply,
1198 g_io_channel_unref(gio);
1202 /* creating restriction rules tree */
1203 __update_contr_cb(NULL);
1205 /* registering periodic kernel counters update callback */
1206 g_system->contr_timer_id = g_timeout_add_seconds(CONTR_TIMER_INTERVAL,
1209 if (g_system->contr_timer_id == 0) {
1210 STC_LOGE("Failed to register kernel counters update timer");
1211 __close_contr_sock(g_system);
1212 return STC_ERROR_FAIL;
1215 __fill_restritions_list();
1217 return STC_ERROR_NONE;
1220 stc_error_e stc_monitor_deinit(void)
1222 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1224 /* close netlink socket for updating kernel counters */
1225 __close_contr_sock(g_system);
1227 /* remove kernel counters update timer */
1228 if (g_system->contr_timer_id > 0) {
1229 g_source_remove(g_system->contr_timer_id);
1230 g_system->contr_timer_id = 0;
1233 /* destroy monitored application tree */
1234 g_tree_destroy(g_system->apps);
1235 g_system->apps = NULL;
1237 /* destroy restriction rules tree */
1238 g_tree_destroy(g_system->rstns);
1239 g_system->rstns = NULL;
1243 return STC_ERROR_NONE;
1246 stc_error_e stc_monitor_application_add(const stc_app_key_s app_key,
1247 const stc_app_value_s app_value)
1249 stc_error_e ret = STC_ERROR_NONE;
1251 stc_app_value_s *value;
1252 stc_app_value_s *lookup;
1254 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1256 lookup = __application_lookup(g_system->apps, &app_key);
1258 STC_LOGD("app_key already present");
1259 return STC_ERROR_NONE;
1262 key = MALLOC0(stc_app_key_s, 1);
1264 STC_LOGE("key allocation failed");
1265 return STC_ERROR_OUT_OF_MEMORY;
1268 value = MALLOC0(stc_app_value_s, 1);
1270 STC_LOGE("value allocation failed");
1272 return STC_ERROR_OUT_OF_MEMORY;
1275 key->app_id = g_strdup(app_key.app_id);
1276 key->pkg_id = g_strdup(app_key.pkg_id);
1278 value->type = app_value.type;
1279 value->counter.in_bytes = app_value.counter.in_bytes;
1280 value->counter.out_bytes = app_value.counter.out_bytes;
1282 value->processes = g_tree_new_full(__processes_tree_key_compare, NULL,
1283 __processes_tree_key_free,
1284 __processes_tree_value_free);
1286 /* create cgroup and update classid */
1287 value->classid = get_classid_by_app_id(app_key.app_id, TRUE);
1289 g_tree_insert(g_system->apps, key, value);
1291 /* add nfacct rule for this classid */
1292 __add_application_monitor(key, value, stc_get_default_connection());
1293 __add_rstns_for_application(app_key.app_id);
1298 stc_error_e stc_monitor_process_add(const stc_app_key_s app_key,
1299 const stc_process_key_s proc_key,
1300 const stc_process_value_s proc_value)
1302 stc_error_e ret = STC_ERROR_NONE;
1303 stc_app_value_s *app_lookup;
1304 stc_process_key_s *key;
1305 stc_process_value_s *value;
1306 stc_process_value_s *proc_lookup;
1308 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1310 app_lookup = __application_lookup(g_system->apps, &app_key);
1312 STC_LOGD("app_key not found");
1313 return STC_ERROR_FAIL;
1316 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1318 STC_LOGD("proc_key already present");
1319 return STC_ERROR_NONE;
1322 key = MALLOC0(stc_process_key_s, 1);
1324 STC_LOGE("key allocation failed");
1325 return STC_ERROR_OUT_OF_MEMORY;
1328 value = MALLOC0(stc_process_value_s, 1);
1330 STC_LOGE("value allocation failed");
1332 return STC_ERROR_OUT_OF_MEMORY;
1335 key->pid = proc_key.pid;
1337 value->ground = proc_value.ground;
1339 g_tree_insert(app_lookup->processes, key, value);
1341 /* add pid to application cgroup */
1342 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1347 stc_error_e stc_monitor_process_remove(pid_t pid)
1349 stc_error_e ret = STC_ERROR_NONE;
1350 stc_process_key_s proc_key = {
1354 remove_pid_context_s context = {
1356 .proc_key = &proc_key,
1357 .entry_removed = FALSE,
1360 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1362 g_tree_foreach(g_system->apps, __apps_tree_foreach_remove_pid,
1365 if (context.entry_removed)
1366 __application_remove_if_empty(context.app_key);
1371 stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key,
1372 const stc_process_key_s proc_key,
1373 stc_app_state_e ground)
1375 stc_error_e ret = STC_ERROR_NONE;
1376 stc_app_value_s *app_lookup;
1377 stc_process_value_s *proc_lookup;
1379 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1381 app_lookup = __application_lookup(g_system->apps, &app_key);
1383 STC_LOGD("app_key not found");
1384 return STC_ERROR_FAIL;
1387 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1389 STC_LOGD("proc_key not found");
1390 return STC_ERROR_FAIL;
1393 if (proc_lookup->ground != ground)
1394 proc_lookup->ground = ground;
1396 if (ground == STC_APP_STATE_BACKGROUND) {
1397 char *background_app_id = g_strconcat(app_key.app_id,
1398 STC_BACKGROUND_APP_SUFFIX,
1400 place_pids_to_net_cgroup(proc_key.pid, background_app_id);
1401 g_free(background_app_id);
1403 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1409 void stc_monitor_update_rstn_by_default_connection(void *data)
1411 static default_connection_s old_connection;
1412 default_connection_s *new_connection = (default_connection_s *)data;
1414 if (old_connection.path != NULL) {
1416 g_tree_foreach(g_system->apps,
1417 __remove_application_monitor,
1418 (gpointer)&old_connection);
1420 if (g_system->rstns)
1421 g_tree_foreach(g_system->rstns,
1422 __remove_restriction,
1423 (gpointer)&old_connection);
1426 FREE(old_connection.path);
1427 FREE(old_connection.ifname);
1428 old_connection.type = 0;
1429 old_connection.roaming = 0;
1431 if (new_connection != NULL && new_connection->path != NULL) {
1433 g_tree_foreach(g_system->apps,
1434 __add_application_monitor,
1435 (gpointer)new_connection);
1437 if (g_system->rstns)
1438 g_tree_foreach(g_system->rstns, __add_restriction,
1441 old_connection.path = g_strdup(new_connection->path);
1442 old_connection.ifname = g_strdup(new_connection->ifname);
1443 old_connection.type = new_connection->type;
1444 old_connection.roaming = new_connection->roaming;
1448 stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info)
1453 stc_rstn_value_s value;
1455 memset(&key, 0, sizeof(stc_rstn_key_s));
1456 memset(&value, 0, sizeof(stc_rstn_value_s));
1458 key.app_id = g_strdup(info->app_id);
1459 key.ifname = g_strdup(info->ifname);
1460 key.imsi = g_strdup(info->imsi);
1461 key.iftype = info->iftype;
1462 key.roaming = info->roaming;
1464 value.rst_state = info->rst_state;
1465 value.restriction_id = info->restriction_id;
1467 if (value.rst_state != STC_RESTRICTION_EXCLUDED && info->app_id)
1468 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1470 value.classid = STC_UNKNOWN_CLASSID;
1472 value.limit.in_bytes = info->rcv_limit;
1473 value.limit.out_bytes = info->send_limit;
1474 value.warn_limit.in_bytes = info->rcv_warn_limit;
1475 value.warn_limit.out_bytes = info->send_warn_limit;
1477 ret = __rstn_tree_add(&key, &value, TRUE);
1485 stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info)
1489 stc_rstn_key_s key = {
1490 .app_id = g_strdup(info->app_id),
1491 .ifname = g_strdup(info->ifname),
1492 .imsi = g_strdup(info->imsi),
1493 .iftype = info->iftype,
1494 .roaming = info->roaming,
1497 ret = __rstn_tree_remove(&key);
1505 int stc_monitor_get_counter_socket(void)
1507 return g_system->contr_sock;