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"
28 #include "stc-manager-plugin.h"
30 #define MAX_INT_LENGTH 128
33 stc_app_key_s *app_key;
34 stc_process_key_s *proc_key;
35 gboolean entry_removed;
36 } remove_pid_context_s;
39 struct nfacct_rule *counter;
41 gboolean in_limit_reached;
42 gboolean out_limit_reached;
43 } classid_bytes_context_s;
45 static stc_system_s *g_system = NULL;
47 static nfacct_rule_jump __get_jump_by_intend(struct nfacct_rule *counter)
49 if (counter->intend == NFACCT_WARN)
50 return NFACCT_JUMP_ACCEPT;
51 else if (counter->intend == NFACCT_BLOCK)
52 return NFACCT_JUMP_REJECT;
54 return NFACCT_JUMP_UNKNOWN;
57 static stc_error_e __add_iptables_in(struct nfacct_rule *counter)
59 return produce_net_rule(counter, 0, 0,
60 NFACCT_ACTION_INSERT, __get_jump_by_intend(counter),
64 static stc_error_e __add_iptables_out(struct nfacct_rule *counter)
66 return produce_net_rule(counter, 0, 0,
67 NFACCT_ACTION_INSERT, __get_jump_by_intend(counter),
71 static stc_error_e __del_iptables_in(struct nfacct_rule *counter)
73 return produce_net_rule(counter, 0, 0,
74 NFACCT_ACTION_DELETE, __get_jump_by_intend(counter),
78 static stc_error_e __del_iptables_out(struct nfacct_rule *counter)
80 return produce_net_rule(counter, 0, 0,
81 NFACCT_ACTION_DELETE, __get_jump_by_intend(counter),
85 static int __processes_tree_key_compare(gconstpointer a, gconstpointer b,
86 gpointer UNUSED user_data)
88 stc_process_key_s *key_a = (stc_process_key_s *)a;
89 stc_process_key_s *key_b = (stc_process_key_s *)b;
91 return key_a->pid - key_b->pid;
94 static void __processes_tree_value_free(gpointer data)
96 stc_process_value_s *value = (stc_process_value_s *)data;
101 static void __processes_tree_key_free(gpointer data)
103 stc_process_key_s *key = (stc_process_key_s *)data;
108 static int __apps_tree_key_compare(gconstpointer a, gconstpointer b,
109 gpointer UNUSED user_data)
111 stc_app_key_s *key_a = (stc_app_key_s *)a;
112 stc_app_key_s *key_b = (stc_app_key_s *)b;
115 ret = g_strcmp0(key_a->pkg_id, key_b->pkg_id);
119 return g_strcmp0(key_a->app_id, key_b->app_id);
122 static void __apps_tree_value_free(gpointer data)
124 stc_app_value_s *value = (stc_app_value_s *)data;
126 g_tree_destroy(value->processes);
127 value->processes = NULL;
132 static void __apps_tree_key_free(gpointer data)
134 stc_app_key_s *key = (stc_app_key_s *)data;
141 static int __rstns_tree_key_compare(gconstpointer a, gconstpointer b,
142 gpointer UNUSED user_data)
144 stc_rstn_key_s *key_a = (stc_rstn_key_s *)a;
145 stc_rstn_key_s *key_b = (stc_rstn_key_s *)b;
148 ret = g_strcmp0(key_a->app_id, key_b->app_id);
152 ret = g_strcmp0(key_a->ifname, key_b->ifname);
156 ret = g_strcmp0(key_a->imsi, key_b->imsi);
160 ret = key_a->iftype - key_b->iftype;
167 static void __rstns_tree_value_free(gpointer data)
169 stc_rstn_value_s *value = (stc_rstn_value_s *)data;
174 static void __rstns_tree_key_free(gpointer data)
176 stc_rstn_key_s *key = (stc_rstn_key_s *)data;
184 static gboolean __processes_tree_foreach_print(gpointer key, gpointer value,
187 stc_process_key_s *proc_key = (stc_process_key_s *)key;
188 stc_process_value_s *proc_value = (stc_process_value_s *)value;
190 STC_LOGD("Process entry => PID [%d], Ground state [%d]",
191 proc_key->pid, proc_value->ground);
195 static void __processes_tree_printall(GTree *processes)
197 g_tree_foreach(processes, __processes_tree_foreach_print, NULL);
200 static gboolean __apps_tree_foreach_print(gpointer key, gpointer value,
203 stc_app_key_s *app_key = (stc_app_key_s *)key;
204 stc_app_value_s *app_value = (stc_app_value_s *)value;
206 STC_LOGD("Application info => Pkg ID [%s], App ID [%s],"
207 " Type [%d], classid [%d],"
208 " counter [ in (%lld), out (%lld)]",
209 app_key->pkg_id, app_key->app_id,
210 app_value->type, app_value->classid,
211 app_value->data_usage.in_bytes, app_value->data_usage.out_bytes);
213 __processes_tree_printall(app_value->processes);
218 static void __apps_tree_printall(void)
220 g_tree_foreach(g_system->apps, __apps_tree_foreach_print, NULL);
224 static gboolean __apps_tree_foreach_remove_pid(gpointer key, gpointer value,
227 remove_pid_context_s *context = (remove_pid_context_s *)data;
228 stc_app_value_s *app_value = (stc_app_value_s *)value;
230 if (!g_tree_remove(app_value->processes, context->proc_key)) {
231 STC_LOGD("key not found");
235 context->entry_removed = TRUE;
236 context->app_key = (stc_app_key_s *)key;
241 static stc_app_value_s * __application_lookup(GTree *apps,
242 const stc_app_key_s *key)
244 stc_app_value_s *lookup;
246 ret_value_msg_if(apps == NULL, NULL, "apps is null!");
248 lookup = g_tree_lookup(apps, key);
253 static stc_process_value_s * __process_lookup(GTree *processes,
254 const stc_process_key_s *key)
256 stc_process_value_s *lookup;
258 ret_value_msg_if(processes == NULL, NULL, "processes is null!");
260 lookup = g_tree_lookup(processes, key);
265 static gboolean __processes_tree_check_empty(gpointer key, gpointer value,
268 guint *pid_count = (guint *)data;
273 static gboolean __add_application_monitor(gpointer key, gpointer value,
276 stc_app_value_s *app_value = (stc_app_value_s *)value;
277 default_connection_s *connection = (default_connection_s *)data;
278 stc_s *stc = stc_get_manager();
280 if (stc && connection && connection->ifname) {
281 struct nfacct_rule counter;
284 stc->carg = MALLOC0(counter_arg_s, 1);
285 stc->carg->sock = stc_monitor_get_counter_socket();
288 memset(&counter, 0, sizeof(struct nfacct_rule));
290 counter.carg = stc->carg;
291 counter.classid = app_value->classid;
292 counter.intend = NFACCT_COUNTER;
293 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
295 __add_iptables_in(&counter);
296 __add_iptables_out(&counter);
302 static gboolean __remove_application_monitor(gpointer key, gpointer value,
305 stc_app_value_s *app_value = (stc_app_value_s *)value;
306 default_connection_s *connection = (default_connection_s *)data;
307 stc_s *stc = stc_get_manager();
309 if (stc && connection && connection->ifname) {
310 struct nfacct_rule counter;
313 stc->carg = MALLOC0(counter_arg_s, 1);
314 stc->carg->sock = stc_monitor_get_counter_socket();
317 memset(&counter, 0, sizeof(struct nfacct_rule));
319 counter.carg = stc->carg;
320 counter.classid = app_value->classid;
321 counter.intend = NFACCT_COUNTER;
322 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
324 __del_iptables_in(&counter);
325 __del_iptables_out(&counter);
331 static void __print_rstn(stc_rstn_key_s *rstn_key, stc_rstn_value_s *rstn_value)
333 STC_LOGI("rstn info => rstn_id [%llu], "
334 "app_id [%s], classid [%lu], ifname [%s], "
335 "iftype [%d], rst_state [%d], "
336 "limit [ in (%lld), out (%lld)], "
337 "warn_limit [ in (%lld), out (%lld)], "
338 "counter [ in (%lld), out (%lld)], "
339 "roaming [%d], imsi [%s]",
340 rstn_value->restriction_id,
341 rstn_key->app_id, rstn_value->classid , rstn_key->ifname,
342 rstn_key->iftype, rstn_value->rst_state,
343 rstn_value->limit.in_bytes, rstn_value->limit.out_bytes,
344 rstn_value->warn_limit.in_bytes,
345 rstn_value->warn_limit.out_bytes,
346 rstn_value->counter.in_bytes, rstn_value->counter.out_bytes,
347 rstn_key->roaming, rstn_key->imsi);
350 static void __process_restriction(enum traffic_restriction_type rst_type,
351 stc_rstn_key_s *rstn_key,
352 stc_rstn_value_s *rstn_value, void *data)
354 stc_data_counter_s effective_limit, effective_warn_limit;
355 default_connection_s *old_connection = (default_connection_s *)data;
356 default_connection_s *connection = NULL;
358 if (old_connection != NULL)
359 connection = old_connection;
361 connection = stc_get_default_connection();
363 /* no default ifname */
364 if (connection->ifname == NULL)
367 /* rstn not applicable for this interface */
368 if (rstn_key->ifname != NULL && g_strcmp0("", rstn_key->ifname) != 0 &&
369 g_strcmp0(connection->ifname, rstn_key->ifname) != 0)
372 /* classid is invalid */
373 if (rstn_value->classid == STC_UNKNOWN_CLASSID)
376 effective_limit.out_bytes = rstn_value->limit.out_bytes;
377 effective_limit.in_bytes = rstn_value->limit.in_bytes;
378 effective_warn_limit.out_bytes = rstn_value->warn_limit.out_bytes;
379 effective_warn_limit.in_bytes = rstn_value->warn_limit.in_bytes;
381 if (rst_type == RST_SET) {
382 /* TODO: Change this to runtime memory */
383 table_counters_info info;
385 memset(&info, 0, sizeof(table_counters_info));
386 table_counters_get(rstn_value->restriction_id, &info);
388 effective_limit.out_bytes -= info.sent_bytes;
389 effective_limit.in_bytes -= info.rcv_bytes;
390 effective_warn_limit.out_bytes -= info.sent_bytes;
391 effective_warn_limit.in_bytes -= info.rcv_bytes;
393 if (effective_limit.in_bytes < 0) {
394 effective_limit.in_bytes = 0;
395 rstn_value->in_limit_reached = TRUE;
398 if (effective_limit.out_bytes < 0) {
399 effective_limit.out_bytes = 0;
400 rstn_value->out_limit_reached = TRUE;
403 if (effective_warn_limit.in_bytes < 0)
404 effective_warn_limit.in_bytes = 0;
406 if (effective_warn_limit.out_bytes < 0)
407 effective_warn_limit.out_bytes = 0;
408 STC_LOGD("datausage [in: %lld, out: %lld]",
409 info.rcv_bytes, info.sent_bytes);
412 STC_LOGD("rstn_id [%llu], effective limit [in: %lld, out: %lld], "
413 "effective warn limit [in: %lld, out: %lld], "
414 "datausage [in: %lld, out: %lld]",
415 rstn_value->restriction_id,
416 effective_limit.in_bytes, effective_limit.out_bytes,
417 effective_warn_limit.in_bytes, effective_warn_limit.out_bytes);
419 send_net_restriction(rst_type,
423 effective_limit.out_bytes,
424 effective_limit.in_bytes,
425 effective_warn_limit.out_bytes,
426 effective_warn_limit.in_bytes,
430 rstn_value->rst_state = STC_RESTRICTION_ACTIVATED;
431 rstn_value->in_limit_reached = FALSE;
432 rstn_value->out_limit_reached = FALSE;
438 rstn_value->rst_state = STC_RESTRICTION_REMOVED;
439 rstn_value->in_limit_reached = FALSE;
440 rstn_value->out_limit_reached = FALSE;
447 static gboolean __remove_rstns_foreach_application(gpointer key,
451 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
452 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
453 gchar *app_id = (gchar *)data;
455 /* rstn rule is not for applications */
456 if (rstn_key->app_id == NULL)
459 /* rstn rule is not for this application */
460 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
463 /* rstn rule is already removed */
464 if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
467 /* remove restriction from system */
468 __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
470 __print_rstn(rstn_key, rstn_value);
475 static void __remove_rstns_for_application(gchar *app_id)
477 g_tree_foreach(g_system->rstns, __remove_rstns_foreach_application,
481 static stc_error_e __application_remove_if_empty(const stc_app_key_s *app_key)
483 stc_error_e ret = STC_ERROR_NONE;
485 stc_app_value_s *lookup;
487 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
489 lookup = __application_lookup(g_system->apps, app_key);
491 STC_LOGE("app_key not found");
492 return STC_ERROR_NO_DATA;
495 g_tree_foreach(lookup->processes, __processes_tree_check_empty,
499 /* remove nfacct rule for this classid */
500 __remove_application_monitor((gpointer) app_key, lookup,
501 stc_get_default_connection());
502 __remove_rstns_for_application(app_key->app_id);
505 if (!g_tree_remove(g_system->apps, app_key)) {
506 ret = STC_ERROR_NO_DATA;
507 STC_LOGE("key not found");
513 static stc_error_e __close_contr_sock(stc_system_s *system)
515 ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
517 /* close netlink socket for updating kernel counters */
518 if (g_system->contr_sock != -1) {
519 close(g_system->contr_sock);
520 g_system->contr_sock = -1;
523 if (g_system->contr_gsource_id != 0) {
524 g_source_remove(g_system->contr_gsource_id);
525 g_system->contr_gsource_id = 0;
528 return STC_ERROR_NONE;
531 static gboolean __rstn_counter_update_foreach_classid(gpointer key,
535 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
536 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
537 classid_bytes_context_s *context = (classid_bytes_context_s *)data;
539 if (context->counter->intend != NFACCT_COUNTER)
540 goto try_next_callback;
542 if (rstn_value->classid != context->counter->classid)
543 goto try_next_callback;
545 if (rstn_value->in_limit_reached == TRUE) {
546 context->in_limit_reached = TRUE;
547 goto try_next_callback;
550 if (rstn_value->out_limit_reached == TRUE) {
551 context->out_limit_reached = TRUE;
552 goto try_next_callback;
555 switch (context->counter->iotype) {
556 case NFACCT_COUNTER_IN:
557 rstn_value->counter.in_bytes += context->bytes;
559 if (rstn_value->counter.in_bytes >= rstn_value->warn_limit.in_bytes
560 && rstn_value->warn_limit_crossed_notified == FALSE) {
562 char iftype[MAX_INT_LENGTH];
563 char byte[MAX_INT_LENGTH];
564 stc_s *stc = (stc_s *)stc_get_manager();
565 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
567 rv = stc_manager_dbus_emit_signal(stc->connection,
568 STC_DBUS_SERVICE_RESTRICTION_PATH,
569 STC_DBUS_INTERFACE_RESTRICTION,
570 "WarnThresholdCrossed",
571 g_variant_new("(s)", rstn_key->app_id));
573 rstn_value->warn_limit_crossed_notified = TRUE;
575 snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
576 snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->limit.in_bytes);
577 stc_send_warn_message_to_net_popup("warn threshold crossed",
578 "warning_noti", rstn_key->app_id, iftype, byte);
581 /* block immediately */
582 if (rstn_value->counter.in_bytes >= rstn_value->limit.in_bytes) {
583 context->counter->intend = NFACCT_BLOCK;
584 __del_iptables_in(context->counter);
585 __add_iptables_in(context->counter);
586 context->counter->intend = NFACCT_COUNTER;
587 rstn_value->in_limit_reached = TRUE;
589 if (rstn_value->rstn_limit_crossed_notified == FALSE) {
592 char iftype[MAX_INT_LENGTH];
593 char byte[MAX_INT_LENGTH];
594 stc_s *stc = (stc_s *)stc_get_manager();
595 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
597 rv = stc_manager_dbus_emit_signal(stc->connection,
598 STC_DBUS_SERVICE_RESTRICTION_PATH,
599 STC_DBUS_INTERFACE_RESTRICTION,
600 "RestrictionThresholdCrossed",
601 g_variant_new("(s)", rstn_key->app_id));
603 rstn_value->rstn_limit_crossed_notified = TRUE;
605 snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
606 snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->limit.in_bytes);
607 stc_send_restriction_message_to_net_popup("restriction threshold crossed",
608 "restriction_noti", rstn_key->app_id, iftype, byte);
612 g_system->rstns_tree_updated = TRUE;
613 __print_rstn(rstn_key, rstn_value);
615 case NFACCT_COUNTER_OUT:
616 rstn_value->counter.out_bytes += context->bytes;
618 if (rstn_value->counter.out_bytes >= rstn_value->limit.out_bytes
619 && rstn_value->warn_limit_crossed_notified == FALSE) {
621 char iftype[MAX_INT_LENGTH];
622 char byte[MAX_INT_LENGTH];
623 stc_s *stc = (stc_s *)stc_get_manager();
624 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
626 rv = stc_manager_dbus_emit_signal(stc->connection,
627 STC_DBUS_SERVICE_RESTRICTION_PATH,
628 STC_DBUS_INTERFACE_RESTRICTION,
629 "WarnThresholdCrossed",
630 g_variant_new("(s)", rstn_key->app_id));
632 rstn_value->warn_limit_crossed_notified = TRUE;
634 snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
635 snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->limit.out_bytes);
636 stc_send_warn_message_to_net_popup("warn threshold crossed",
637 "warning_noti", rstn_key->app_id, iftype, byte);
640 /* block immediately */
641 if (rstn_value->counter.out_bytes >= rstn_value->limit.out_bytes) {
642 context->counter->intend = NFACCT_BLOCK;
643 __del_iptables_out(context->counter);
644 __add_iptables_out(context->counter);
645 context->counter->intend = NFACCT_COUNTER;
646 rstn_value->out_limit_reached = TRUE;
648 if (rstn_value->rstn_limit_crossed_notified == FALSE) {
650 char iftype[MAX_INT_LENGTH];
651 char byte[MAX_INT_LENGTH];
652 stc_s *stc = (stc_s *)stc_get_manager();
653 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
655 rv = stc_manager_dbus_emit_signal(stc->connection,
656 STC_DBUS_SERVICE_RESTRICTION_PATH,
657 STC_DBUS_INTERFACE_RESTRICTION,
658 "RestrictionThresholdCrossed",
659 g_variant_new("(s)", rstn_key->app_id));
661 rstn_value->rstn_limit_crossed_notified = TRUE;
663 snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
664 snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->limit.out_bytes);
665 stc_send_restriction_message_to_net_popup("restriction threshold crossed",
666 "restriction_noti", rstn_key->app_id, iftype, byte);
670 g_system->rstns_tree_updated = TRUE;
671 __print_rstn(rstn_key, rstn_value);
674 STC_LOGE("unknown iotype");
682 static gboolean __update_app_statistics(gpointer key, gpointer value,
685 stc_app_key_s *app_key = (stc_app_key_s *)key;
686 stc_app_value_s *app_value = (stc_app_value_s *)value;
687 time_t *touch_time = (time_t *)data;
688 stc_db_classid_iftype_key stat_key;
689 stc_db_app_stats stat;
690 char *default_ifname = stc_default_connection_get_ifname();
692 memset(&stat_key, 0, sizeof(stc_db_classid_iftype_key));
693 memset(&stat, 0 , sizeof(stc_db_app_stats));
695 stat_key.classid = app_value->classid;
696 stat_key.iftype = stc_default_connection_get_type();
697 if (STC_IFACE_DATACALL == stat_key.iftype)
698 stat_key.imsi = g_strdup("unknown");
700 stat_key.imsi = g_strdup("noneimsi");
701 g_strlcpy(stat_key.ifname, default_ifname, MAX_IFACE_LENGTH);
703 stat.app_id = g_strdup(app_key->app_id);
704 stat.snd_count = app_value->counter.out_bytes;
705 stat.rcv_count = app_value->counter.in_bytes;
708 stat.is_roaming = stc_default_connection_get_roaming();
709 stat.ground = STC_APP_STATE_UNKNOWN;
711 table_statistics_insert(&stat_key, &stat, *touch_time);
713 app_value->counter.out_bytes = 0;
714 app_value->counter.in_bytes = 0;
718 FREE(default_ifname);
723 static gboolean __flush_apps_stats_to_database(gpointer user_data)
725 time_t current_time = time(0);
727 if (g_system->apps_tree_updated == FALSE)
728 return G_SOURCE_REMOVE;
730 g_system->apps_tree_updated = FALSE;
733 g_tree_foreach(g_system->apps,
734 __update_app_statistics,
737 STC_LOGI("Flushed app stats to database");
738 return G_SOURCE_REMOVE;
741 static gboolean __update_counter_statistics(gpointer key, gpointer value,
744 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
745 table_counters_info info = {
746 .restriction_id = rstn_value->restriction_id,
747 .sent_bytes = rstn_value->counter.out_bytes,
748 .rcv_bytes = rstn_value->counter.in_bytes
751 table_counters_update_counters(&info);
756 static gboolean __flush_rstns_counter_to_database(gpointer user_data)
758 time_t current_time = time(0);
760 if (g_system->rstns_tree_updated == FALSE)
761 return G_SOURCE_REMOVE;
763 g_system->rstns_tree_updated = FALSE;
766 g_tree_foreach(g_system->rstns,
767 __update_counter_statistics,
770 STC_LOGI("Flushed rstns counters to database");
771 return G_SOURCE_REMOVE;
774 static gboolean __apps_counter_update_foreach_classid(gpointer key,
778 stc_app_key_s *app_key = (stc_app_key_s *)key;
779 stc_app_value_s *app_value = (stc_app_value_s *)value;
780 classid_bytes_context_s *context = (classid_bytes_context_s *)data;
782 if (context->counter->intend != NFACCT_COUNTER)
783 goto try_next_callback;
785 if (app_value->classid != context->counter->classid)
786 goto try_next_callback;
788 switch (context->counter->iotype) {
789 case NFACCT_COUNTER_IN:
790 app_value->data_usage.in_bytes += context->bytes;
791 app_value->counter.in_bytes = context->bytes;
792 g_system->apps_tree_updated = TRUE;
794 __apps_tree_foreach_print(app_key, app_value, NULL);
796 case NFACCT_COUNTER_OUT:
797 app_value->data_usage.out_bytes += context->bytes;
798 app_value->counter.out_bytes = context->bytes;
799 g_system->apps_tree_updated = TRUE;
801 __apps_tree_foreach_print(app_key, app_value, NULL);
804 STC_LOGE("unknown iotype");
811 static void __fill_nfacct_result(char *cnt_name, int64_t bytes,
812 struct counter_arg *carg)
814 struct nfacct_rule counter = {
821 classid_bytes_context_s context = {
824 .in_limit_reached = FALSE,
825 .out_limit_reached = FALSE
828 STC_LOGD("cnt_name %s", cnt_name);
830 if (!recreate_counter_by_name(cnt_name, &counter)) {
831 STC_LOGE("Can't parse counter name %s", cnt_name);
835 STC_LOGI("classid %lu, iftype %u, iotype %d, intend %d, ifname %s, bytes %lld",
836 context.counter->classid, context.counter->iftype,
837 context.counter->iotype, context.counter->intend,
838 context.counter->ifname, context.bytes);
841 g_tree_foreach(g_system->rstns,
842 __rstn_counter_update_foreach_classid,
846 g_tree_foreach(g_system->apps,
847 __apps_counter_update_foreach_classid,
851 static int __fill_counters(struct rtattr *attr_list[__NFACCT_MAX],
854 struct counter_arg *carg = user_data;
855 char *cnt_name = (char *)RTA_DATA(attr_list[NFACCT_NAME]);
856 if (carg->initiate) {
858 * TODO: this will be used when daemon starts to update existing
859 * counter data if present.
861 populate_counters(cnt_name, carg);
865 (int64_t *)RTA_DATA(attr_list[NFACCT_BYTES]);
866 int bytes = be64toh(*bytes_p);
868 ++carg->serialized_counters;
869 __fill_nfacct_result(cnt_name, bytes, carg);
876 static int __post_fill_counters(void *user_data)
878 struct counter_arg *carg = user_data;
886 static void __process_network_counter(struct genl *ans,
887 struct counter_arg *carg)
889 struct netlink_serialization_params ser_params = {
892 .eval_attr = __fill_counters,
893 .post_eval_attr = __post_fill_counters,
896 netlink_serialization_command *netlink =
897 netlink_create_command(&ser_params);
899 STC_LOGE("Can not create command");
903 netlink->deserialize_answer(&(netlink->params));
906 static gboolean __process_contr_reply(GIOChannel *source,
907 GIOCondition condition,
910 int sock = g_io_channel_unix_get_fd(source);
913 stc_s *stc = stc_get_manager();
915 if ((condition & G_IO_ERR) ||
916 (condition & G_IO_HUP) ||
917 (condition & G_IO_NVAL)) {
918 /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */
920 __close_contr_sock(g_system);
925 STC_LOGE("Can't get stc data");
929 ret = read_netlink(sock,
930 &ans, sizeof(struct genl));
931 STC_LOGD("Counter data received ret [%d]", ret);
935 stc->carg->ans_len = ret;
936 __process_network_counter(&ans, stc->carg);
938 g_idle_add(__flush_apps_stats_to_database, NULL);
939 g_idle_add(__flush_rstns_counter_to_database, NULL);
944 static gboolean __update_contr_cb(void *user_data)
946 /* Here we just sent command, answer we receive in another callback */
947 stc_s *stc = stc_get_manager();
948 ret_value_msg_if(stc == NULL, STC_ERROR_FAIL, "Can't get stc data");
950 stc->carg = MALLOC0(counter_arg_s, 1);
951 stc->carg->sock = stc_monitor_get_counter_socket();
954 STC_LOGD("Get all counters");
955 nfacct_send_get_all(stc->carg);
957 /* we need to continue the timer */
961 static gboolean __rstn_tree_foreach_print(gpointer key, gpointer value,
964 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
965 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
967 __print_rstn(rstn_key, rstn_value);
971 static void __rstn_tree_printall(void)
973 g_tree_foreach(g_system->rstns, __rstn_tree_foreach_print, NULL);
976 static stc_rstn_value_s * __rstn_lookup(GTree *rstns_tree,
977 const stc_rstn_key_s *key)
979 stc_rstn_value_s *lookup;
981 ret_value_msg_if(rstns_tree == NULL, NULL, "rstns_tree is null!");
983 lookup = g_tree_lookup(rstns_tree, key);
988 static gboolean __remove_restriction(gpointer key, gpointer value,
991 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
992 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
994 /* rstn rule is already removed */
995 if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
998 __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
999 __print_rstn(rstn_key, rstn_value);
1003 static gboolean __add_restriction_debug(gpointer key, gpointer value,
1006 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1007 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1009 /* rstn rule is activated */
1010 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1013 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1014 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1016 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1018 __print_rstn(rstn_key, rstn_value);
1023 static gboolean __add_restriction(gpointer key, gpointer value, gpointer data)
1025 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1026 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1028 /* rstn rule is activated */
1029 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1032 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1033 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1035 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1040 static stc_error_e __rstn_tree_remove(stc_rstn_key_s *key)
1042 stc_rstn_value_s *lookup_value;
1044 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1046 lookup_value = __rstn_lookup(g_system->rstns, key);
1047 if (!lookup_value) {
1048 STC_LOGE("key not found");
1049 return STC_ERROR_NO_DATA;
1052 __remove_restriction(key, lookup_value, NULL);
1054 /* remove counter also */
1055 table_counters_delete(lookup_value->restriction_id);
1057 if (!g_tree_remove(g_system->rstns, key)) {
1058 STC_LOGD("key not found");
1059 return STC_ERROR_NO_DATA;
1062 return STC_ERROR_NONE;
1065 static stc_error_e __rstn_tree_add(stc_rstn_key_s *key,
1066 stc_rstn_value_s *value, gboolean debug)
1068 stc_rstn_value_s *rstn_value;
1070 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1072 rstn_value = __rstn_lookup(g_system->rstns, key);
1074 stc_rstn_key_s *rstn_key = MALLOC0(stc_rstn_key_s, 1);
1076 STC_LOGE("rstn_key allocation failed");
1077 return STC_ERROR_OUT_OF_MEMORY;
1080 rstn_value = MALLOC0(stc_rstn_value_s, 1);
1082 STC_LOGE("rstn_value allocation failed");
1084 return STC_ERROR_OUT_OF_MEMORY;
1087 rstn_key->app_id = g_strdup(key->app_id);
1088 rstn_key->ifname = g_strdup(key->ifname);
1089 rstn_key->imsi = g_strdup(key->imsi);
1090 rstn_key->iftype = key->iftype;
1091 rstn_key->roaming = key->roaming;
1093 g_tree_insert(g_system->rstns, rstn_key, rstn_value);
1096 rstn_value->restriction_id = value->restriction_id;
1097 rstn_value->rst_state = value->rst_state;
1098 rstn_value->classid = value->classid;
1099 rstn_value->limit.in_bytes = value->limit.in_bytes;
1100 rstn_value->limit.out_bytes = value->limit.out_bytes;
1101 rstn_value->warn_limit.in_bytes = value->warn_limit.in_bytes;
1102 rstn_value->warn_limit.out_bytes = value->warn_limit.out_bytes;
1103 rstn_value->counter.in_bytes = 0;
1104 rstn_value->counter.out_bytes = 0;
1105 rstn_value->warn_limit_crossed_notified = FALSE;
1106 rstn_value->rstn_limit_crossed_notified = FALSE;
1109 __add_restriction_debug(key, rstn_value, NULL);
1111 __add_restriction(key, rstn_value, NULL);
1113 return STC_ERROR_NONE;
1116 static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info,
1119 stc_cb_ret_e ret = STC_CONTINUE;
1122 stc_rstn_value_s value;
1124 memset(&key, 0, sizeof(stc_rstn_key_s));
1125 memset(&value, 0, sizeof(stc_rstn_value_s));
1127 key.app_id = g_strdup(info->app_id);
1128 key.ifname = g_strdup(info->ifname);
1129 key.imsi = g_strdup(info->imsi);
1130 key.iftype = info->iftype;
1131 key.roaming = info->roaming;
1133 value.rst_state = info->rst_state;
1134 value.restriction_id = info->restriction_id;
1136 if (value.rst_state != STC_RESTRICTION_EXCLUDED && info->app_id)
1137 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1139 value.classid = STC_UNKNOWN_CLASSID;
1141 value.limit.in_bytes = info->rcv_limit;
1142 value.limit.out_bytes = info->send_limit;
1143 value.warn_limit.in_bytes = info->rcv_warn_limit;
1144 value.warn_limit.out_bytes = info->send_warn_limit;
1146 if (__rstn_tree_add(&key, &value, FALSE) != STC_ERROR_NONE)
1155 static void __fill_restritions_list(void)
1157 table_restrictions_foreach(__insert_restriction_cb, NULL);
1158 //__rstn_tree_printall();
1161 static gboolean __add_rstn_foreach_application(gpointer key,
1165 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1166 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1167 gchar *app_id = (gchar *)data;
1169 /* rstn rule is not for applications */
1170 if (rstn_key->app_id == NULL)
1173 /* rstn rule is not for this application */
1174 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
1177 /* rstn rule is already applied */
1178 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1181 /* add restriction to system */
1182 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1183 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1185 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1187 __print_rstn(rstn_key, rstn_value);
1192 static void __add_rstns_for_application(gchar *app_id)
1194 g_tree_foreach(g_system->rstns, __add_rstn_foreach_application,
1198 stc_error_e stc_monitor_init(void)
1200 stc_system_s *system = MALLOC0(stc_system_s, 1);
1201 GIOChannel *gio = NULL;
1203 ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY, "stc_system_s malloc fail!");
1205 /* initializing cgroups */
1208 /* creating monitored application tree */
1209 system->apps = g_tree_new_full(__apps_tree_key_compare, NULL,
1210 __apps_tree_key_free,
1211 __apps_tree_value_free);
1213 system->rstns = g_tree_new_full(__rstns_tree_key_compare, NULL,
1214 __rstns_tree_key_free,
1215 __rstns_tree_value_free);
1217 /* create netlink socket for updating kernel counters */
1218 system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
1219 if (!(system->contr_sock)) {
1220 STC_LOGE("failed to open socket");
1222 return STC_ERROR_FAIL;
1225 gio = g_io_channel_unix_new(system->contr_sock);
1226 system->contr_gsource_id =
1227 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
1228 (GIOFunc) __process_contr_reply,
1230 g_io_channel_unref(gio);
1234 /* creating restriction rules tree */
1235 __update_contr_cb(NULL);
1237 /* registering periodic kernel counters update callback */
1238 g_system->contr_timer_id = g_timeout_add_seconds(CONTR_TIMER_INTERVAL,
1241 if (g_system->contr_timer_id == 0) {
1242 STC_LOGE("Failed to register kernel counters update timer");
1243 __close_contr_sock(g_system);
1244 return STC_ERROR_FAIL;
1247 __fill_restritions_list();
1249 return STC_ERROR_NONE;
1252 stc_error_e stc_monitor_deinit(void)
1254 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1256 /* close netlink socket for updating kernel counters */
1257 __close_contr_sock(g_system);
1259 /* remove kernel counters update timer */
1260 if (g_system->contr_timer_id > 0) {
1261 g_source_remove(g_system->contr_timer_id);
1262 g_system->contr_timer_id = 0;
1265 /* destroy monitored application tree */
1266 g_tree_destroy(g_system->apps);
1267 g_system->apps = NULL;
1269 /* destroy restriction rules tree */
1270 g_tree_destroy(g_system->rstns);
1271 g_system->rstns = NULL;
1275 return STC_ERROR_NONE;
1278 stc_error_e stc_monitor_application_add(const stc_app_key_s app_key,
1279 const stc_app_value_s app_value)
1281 stc_error_e ret = STC_ERROR_NONE;
1283 stc_app_value_s *value;
1284 stc_app_value_s *lookup;
1286 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1288 lookup = __application_lookup(g_system->apps, &app_key);
1290 STC_LOGD("app_key already present");
1291 return STC_ERROR_NONE;
1294 key = MALLOC0(stc_app_key_s, 1);
1296 STC_LOGE("key allocation failed");
1297 return STC_ERROR_OUT_OF_MEMORY;
1300 value = MALLOC0(stc_app_value_s, 1);
1302 STC_LOGE("value allocation failed");
1304 return STC_ERROR_OUT_OF_MEMORY;
1307 key->app_id = g_strdup(app_key.app_id);
1308 key->pkg_id = g_strdup(app_key.pkg_id);
1310 value->type = app_value.type;
1311 value->counter.in_bytes = app_value.counter.in_bytes;
1312 value->counter.out_bytes = app_value.counter.out_bytes;
1314 value->processes = g_tree_new_full(__processes_tree_key_compare, NULL,
1315 __processes_tree_key_free,
1316 __processes_tree_value_free);
1318 /* create cgroup and update classid */
1319 value->classid = get_classid_by_app_id(app_key.app_id, TRUE);
1321 g_tree_insert(g_system->apps, key, value);
1323 /* add nfacct rule for this classid */
1324 __add_application_monitor(key, value, stc_get_default_connection());
1325 __add_rstns_for_application(app_key.app_id);
1330 stc_error_e stc_monitor_process_add(const stc_app_key_s app_key,
1331 const stc_process_key_s proc_key,
1332 const stc_process_value_s proc_value)
1334 stc_error_e ret = STC_ERROR_NONE;
1335 stc_app_value_s *app_lookup;
1336 stc_process_key_s *key;
1337 stc_process_value_s *value;
1338 stc_process_value_s *proc_lookup;
1340 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1342 app_lookup = __application_lookup(g_system->apps, &app_key);
1344 STC_LOGD("app_key not found");
1345 return STC_ERROR_FAIL;
1348 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1350 STC_LOGD("proc_key already present");
1351 return STC_ERROR_NONE;
1354 key = MALLOC0(stc_process_key_s, 1);
1356 STC_LOGE("key allocation failed");
1357 return STC_ERROR_OUT_OF_MEMORY;
1360 value = MALLOC0(stc_process_value_s, 1);
1362 STC_LOGE("value allocation failed");
1364 return STC_ERROR_OUT_OF_MEMORY;
1367 key->pid = proc_key.pid;
1369 value->ground = proc_value.ground;
1371 g_tree_insert(app_lookup->processes, key, value);
1373 /* add pid to application cgroup */
1374 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1379 stc_error_e stc_monitor_process_remove(pid_t pid)
1381 stc_error_e ret = STC_ERROR_NONE;
1382 stc_process_key_s proc_key = {
1386 remove_pid_context_s context = {
1388 .proc_key = &proc_key,
1389 .entry_removed = FALSE,
1392 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1394 g_tree_foreach(g_system->apps, __apps_tree_foreach_remove_pid,
1397 if (context.entry_removed)
1398 __application_remove_if_empty(context.app_key);
1403 stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key,
1404 const stc_process_key_s proc_key,
1405 stc_app_state_e ground)
1407 stc_error_e ret = STC_ERROR_NONE;
1408 stc_app_value_s *app_lookup;
1409 stc_process_value_s *proc_lookup;
1411 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1413 app_lookup = __application_lookup(g_system->apps, &app_key);
1415 STC_LOGD("app_key not found");
1416 return STC_ERROR_FAIL;
1419 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1421 STC_LOGD("proc_key not found");
1422 return STC_ERROR_FAIL;
1425 if (proc_lookup->ground != ground)
1426 proc_lookup->ground = ground;
1428 if (ground == STC_APP_STATE_BACKGROUND) {
1429 char *background_app_id = g_strconcat(app_key.app_id,
1430 STC_BACKGROUND_APP_SUFFIX,
1432 place_pids_to_net_cgroup(proc_key.pid, background_app_id);
1433 g_free(background_app_id);
1435 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1441 void stc_monitor_update_rstn_by_default_connection(void *data)
1443 static default_connection_s old_connection;
1444 default_connection_s *new_connection = (default_connection_s *)data;
1446 if (old_connection.path != NULL) {
1448 g_tree_foreach(g_system->apps,
1449 __remove_application_monitor,
1450 (gpointer)&old_connection);
1452 if (g_system->rstns)
1453 g_tree_foreach(g_system->rstns,
1454 __remove_restriction,
1455 (gpointer)&old_connection);
1458 FREE(old_connection.path);
1459 FREE(old_connection.ifname);
1460 old_connection.type = 0;
1461 old_connection.roaming = 0;
1463 if (new_connection != NULL && new_connection->path != NULL) {
1465 g_tree_foreach(g_system->apps,
1466 __add_application_monitor,
1467 (gpointer)new_connection);
1469 if (g_system->rstns)
1470 g_tree_foreach(g_system->rstns, __add_restriction,
1473 old_connection.path = g_strdup(new_connection->path);
1474 old_connection.ifname = g_strdup(new_connection->ifname);
1475 old_connection.type = new_connection->type;
1476 old_connection.roaming = new_connection->roaming;
1480 stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info)
1485 stc_rstn_value_s value;
1487 memset(&key, 0, sizeof(stc_rstn_key_s));
1488 memset(&value, 0, sizeof(stc_rstn_value_s));
1490 key.app_id = g_strdup(info->app_id);
1491 key.ifname = g_strdup(info->ifname);
1492 key.imsi = g_strdup(info->imsi);
1493 key.iftype = info->iftype;
1494 key.roaming = info->roaming;
1496 value.rst_state = info->rst_state;
1497 value.restriction_id = info->restriction_id;
1499 if (value.rst_state != STC_RESTRICTION_EXCLUDED && info->app_id)
1500 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1502 value.classid = STC_UNKNOWN_CLASSID;
1504 value.limit.in_bytes = info->rcv_limit;
1505 value.limit.out_bytes = info->send_limit;
1506 value.warn_limit.in_bytes = info->rcv_warn_limit;
1507 value.warn_limit.out_bytes = info->send_warn_limit;
1509 ret = __rstn_tree_add(&key, &value, TRUE);
1517 stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info)
1521 stc_rstn_key_s key = {
1522 .app_id = g_strdup(info->app_id),
1523 .ifname = g_strdup(info->ifname),
1524 .imsi = g_strdup(info->imsi),
1525 .iftype = info->iftype,
1526 .roaming = info->roaming,
1529 ret = __rstn_tree_remove(&key);
1537 int stc_monitor_get_counter_socket(void)
1539 return g_system->contr_sock;