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);
421 rstn_value->rst_state = STC_RESTRICTION_ACTIVATED;
422 rstn_value->in_limit_reached = FALSE;
423 rstn_value->out_limit_reached = FALSE;
429 rstn_value->rst_state = STC_RESTRICTION_REMOVED;
430 rstn_value->in_limit_reached = FALSE;
431 rstn_value->out_limit_reached = FALSE;
438 static gboolean __remove_rstns_foreach_application(gpointer key,
442 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
443 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
444 gchar *app_id = (gchar *)data;
446 /* rstn rule is not for applications */
447 if (rstn_key->app_id == NULL)
450 /* rstn rule is not for this application */
451 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
454 /* rstn rule is already removed */
455 if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
458 /* remove restriction from system */
459 __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
461 __print_rstn(rstn_key, rstn_value);
466 static void __remove_rstns_for_application(gchar *app_id)
468 g_tree_foreach(g_system->rstns, __remove_rstns_foreach_application,
472 static stc_error_e __application_remove_if_empty(const stc_app_key_s *app_key)
474 stc_error_e ret = STC_ERROR_NONE;
476 stc_app_value_s *lookup;
478 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
480 lookup = __application_lookup(g_system->apps, app_key);
482 STC_LOGE("app_key not found");
483 return STC_ERROR_NO_DATA;
486 g_tree_foreach(lookup->processes, __processes_tree_check_empty,
490 /* remove nfacct rule for this classid */
491 __remove_application_monitor((gpointer) app_key, lookup,
492 stc_get_default_connection());
493 __remove_rstns_for_application(app_key->app_id);
496 if (!g_tree_remove(g_system->apps, app_key)) {
497 ret = STC_ERROR_NO_DATA;
498 STC_LOGE("key not found");
504 static stc_error_e __close_contr_sock(stc_system_s *system)
506 ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
508 /* close netlink socket for updating kernel counters */
509 if (g_system->contr_sock != -1) {
510 close(g_system->contr_sock);
511 g_system->contr_sock = -1;
514 if (g_system->contr_gsource_id != 0) {
515 g_source_remove(g_system->contr_gsource_id);
516 g_system->contr_gsource_id = 0;
519 return STC_ERROR_NONE;
522 static gboolean __rstn_counter_update_foreach_classid(gpointer key,
526 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
527 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
528 classid_bytes_context_s *context = (classid_bytes_context_s *)data;
530 if (context->counter->intend != NFACCT_COUNTER)
531 goto try_next_callback;
533 if (rstn_value->classid != context->counter->classid)
534 goto try_next_callback;
536 if (rstn_value->in_limit_reached == TRUE) {
537 context->in_limit_reached = TRUE;
538 goto try_next_callback;
541 if (rstn_value->out_limit_reached == TRUE) {
542 context->out_limit_reached = TRUE;
543 goto try_next_callback;
546 switch (context->counter->iotype) {
547 case NFACCT_COUNTER_IN:
548 rstn_value->counter.in_bytes += context->bytes;
550 if (rstn_value->counter.in_bytes >= rstn_value->warn_limit.in_bytes
551 && rstn_value->warn_limit_crossed_notified == FALSE) {
553 char iftype[MAX_INT_LENGTH];
554 char byte[MAX_INT_LENGTH];
555 stc_s *stc = (stc_s *)stc_get_manager();
556 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
558 rv = stc_manager_dbus_emit_signal(stc->connection,
559 STC_DBUS_SERVICE_RESTRICTION_PATH,
560 STC_DBUS_INTERFACE_RESTRICTION,
561 "WarnThresholdCrossed",
562 g_variant_new("(s)", rstn_key->app_id));
564 rstn_value->warn_limit_crossed_notified = TRUE;
566 snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
567 snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->limit.in_bytes);
568 stc_send_warn_message_to_net_popup("warn threshold crossed",
569 "warning_noti", rstn_key->app_id, iftype, byte);
572 /* block immediately */
573 if (rstn_value->counter.in_bytes >= rstn_value->limit.in_bytes) {
574 context->counter->intend = NFACCT_BLOCK;
575 __del_iptables_in(context->counter);
576 __add_iptables_in(context->counter);
577 context->counter->intend = NFACCT_COUNTER;
578 rstn_value->in_limit_reached = TRUE;
580 if (rstn_value->rstn_limit_crossed_notified == FALSE) {
583 char iftype[MAX_INT_LENGTH];
584 char byte[MAX_INT_LENGTH];
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 "RestrictionThresholdCrossed",
592 g_variant_new("(s)", rstn_key->app_id));
594 rstn_value->rstn_limit_crossed_notified = TRUE;
596 snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
597 snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->limit.in_bytes);
598 stc_send_restriction_message_to_net_popup("restriction threshold crossed",
599 "restriction_noti", rstn_key->app_id, iftype, byte);
603 g_system->rstns_tree_updated = TRUE;
604 __print_rstn(rstn_key, rstn_value);
606 case NFACCT_COUNTER_OUT:
607 rstn_value->counter.out_bytes += context->bytes;
609 if (rstn_value->counter.out_bytes >= rstn_value->limit.out_bytes
610 && rstn_value->warn_limit_crossed_notified == FALSE) {
612 char iftype[MAX_INT_LENGTH];
613 char byte[MAX_INT_LENGTH];
614 stc_s *stc = (stc_s *)stc_get_manager();
615 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
617 rv = stc_manager_dbus_emit_signal(stc->connection,
618 STC_DBUS_SERVICE_RESTRICTION_PATH,
619 STC_DBUS_INTERFACE_RESTRICTION,
620 "WarnThresholdCrossed",
621 g_variant_new("(s)", rstn_key->app_id));
623 rstn_value->warn_limit_crossed_notified = TRUE;
625 snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
626 snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->limit.out_bytes);
627 stc_send_warn_message_to_net_popup("warn threshold crossed",
628 "warning_noti", rstn_key->app_id, iftype, byte);
631 /* block immediately */
632 if (rstn_value->counter.out_bytes >= rstn_value->limit.out_bytes) {
633 context->counter->intend = NFACCT_BLOCK;
634 __del_iptables_out(context->counter);
635 __add_iptables_out(context->counter);
636 context->counter->intend = NFACCT_COUNTER;
637 rstn_value->out_limit_reached = TRUE;
639 if (rstn_value->rstn_limit_crossed_notified == FALSE) {
641 char iftype[MAX_INT_LENGTH];
642 char byte[MAX_INT_LENGTH];
643 stc_s *stc = (stc_s *)stc_get_manager();
644 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
646 rv = stc_manager_dbus_emit_signal(stc->connection,
647 STC_DBUS_SERVICE_RESTRICTION_PATH,
648 STC_DBUS_INTERFACE_RESTRICTION,
649 "RestrictionThresholdCrossed",
650 g_variant_new("(s)", rstn_key->app_id));
652 rstn_value->rstn_limit_crossed_notified = TRUE;
654 snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
655 snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->limit.out_bytes);
656 stc_send_restriction_message_to_net_popup("restriction threshold crossed",
657 "restriction_noti", rstn_key->app_id, iftype, byte);
661 g_system->rstns_tree_updated = TRUE;
662 __print_rstn(rstn_key, rstn_value);
665 STC_LOGE("unknown iotype");
673 static gboolean __update_app_statistics(gpointer key, gpointer value,
676 stc_app_key_s *app_key = (stc_app_key_s *)key;
677 stc_app_value_s *app_value = (stc_app_value_s *)value;
678 time_t *touch_time = (time_t *)data;
679 stc_db_classid_iftype_key stat_key;
680 stc_db_app_stats stat;
681 char *default_ifname = stc_default_connection_get_ifname();
683 memset(&stat_key, 0, sizeof(stc_db_classid_iftype_key));
684 memset(&stat, 0 , sizeof(stc_db_app_stats));
686 stat_key.classid = app_value->classid;
687 stat_key.iftype = stc_default_connection_get_type();
688 if (STC_IFACE_DATACALL == stat_key.iftype)
689 stat_key.imsi = g_strdup("unknown");
691 stat_key.imsi = g_strdup("noneimsi");
692 g_strlcpy(stat_key.ifname, default_ifname, MAX_IFACE_LENGTH);
694 stat.app_id = g_strdup(app_key->app_id);
695 stat.snd_count = app_value->counter.out_bytes;
696 stat.rcv_count = app_value->counter.in_bytes;
699 stat.is_roaming = stc_default_connection_get_roaming();
700 stat.ground = STC_APP_STATE_UNKNOWN;
702 table_statistics_insert(&stat_key, &stat, *touch_time);
704 app_value->counter.out_bytes = 0;
705 app_value->counter.in_bytes = 0;
709 FREE(default_ifname);
714 static gboolean __flush_apps_stats_to_database(gpointer user_data)
716 time_t current_time = time(0);
718 if (g_system->apps_tree_updated == FALSE)
719 return G_SOURCE_REMOVE;
721 g_system->apps_tree_updated = FALSE;
724 g_tree_foreach(g_system->apps,
725 __update_app_statistics,
728 STC_LOGI("Flushed app stats to database");
729 return G_SOURCE_REMOVE;
732 static gboolean __update_counter_statistics(gpointer key, gpointer value,
735 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
736 table_counters_info info = {
737 .restriction_id = rstn_value->restriction_id,
738 .sent_bytes = rstn_value->counter.out_bytes,
739 .rcv_bytes = rstn_value->counter.in_bytes
742 table_counters_update_counters(&info);
747 static gboolean __flush_rstns_counter_to_database(gpointer user_data)
749 time_t current_time = time(0);
751 if (g_system->rstns_tree_updated == FALSE)
752 return G_SOURCE_REMOVE;
754 g_system->rstns_tree_updated = FALSE;
757 g_tree_foreach(g_system->rstns,
758 __update_counter_statistics,
761 STC_LOGI("Flushed rstns counters to database");
762 return G_SOURCE_REMOVE;
765 static gboolean __apps_counter_update_foreach_classid(gpointer key,
769 stc_app_key_s *app_key = (stc_app_key_s *)key;
770 stc_app_value_s *app_value = (stc_app_value_s *)value;
771 classid_bytes_context_s *context = (classid_bytes_context_s *)data;
773 if (context->counter->intend != NFACCT_COUNTER)
774 goto try_next_callback;
776 if (app_value->classid != context->counter->classid)
777 goto try_next_callback;
779 switch (context->counter->iotype) {
780 case NFACCT_COUNTER_IN:
781 app_value->data_usage.in_bytes += context->bytes;
782 app_value->counter.in_bytes = context->bytes;
783 g_system->apps_tree_updated = TRUE;
785 __apps_tree_foreach_print(app_key, app_value, NULL);
787 case NFACCT_COUNTER_OUT:
788 app_value->data_usage.out_bytes += context->bytes;
789 app_value->counter.out_bytes = context->bytes;
790 g_system->apps_tree_updated = TRUE;
792 __apps_tree_foreach_print(app_key, app_value, NULL);
795 STC_LOGE("unknown iotype");
802 static void __fill_nfacct_result(char *cnt_name, int64_t bytes,
803 struct counter_arg *carg)
805 struct nfacct_rule counter = {
812 classid_bytes_context_s context = {
815 .in_limit_reached = FALSE,
816 .out_limit_reached = FALSE
819 STC_LOGD("cnt_name %s", cnt_name);
821 if (!recreate_counter_by_name(cnt_name, &counter)) {
822 STC_LOGE("Can't parse counter name %s", cnt_name);
826 STC_LOGI("classid %lu, iftype %u, iotype %d, intend %d, ifname %s, bytes %lld",
827 context.counter->classid, context.counter->iftype,
828 context.counter->iotype, context.counter->intend,
829 context.counter->ifname, context.bytes);
832 g_tree_foreach(g_system->rstns,
833 __rstn_counter_update_foreach_classid,
837 g_tree_foreach(g_system->apps,
838 __apps_counter_update_foreach_classid,
842 static int __fill_counters(struct rtattr *attr_list[__NFACCT_MAX],
845 struct counter_arg *carg = user_data;
846 char *cnt_name = (char *)RTA_DATA(attr_list[NFACCT_NAME]);
847 if (carg->initiate) {
849 * TODO: this will be used when daemon starts to update existing
850 * counter data if present.
852 populate_counters(cnt_name, carg);
856 (int64_t *)RTA_DATA(attr_list[NFACCT_BYTES]);
857 int bytes = be64toh(*bytes_p);
859 ++carg->serialized_counters;
860 __fill_nfacct_result(cnt_name, bytes, carg);
867 static int __post_fill_counters(void *user_data)
869 struct counter_arg *carg = user_data;
877 static void __process_network_counter(struct genl *ans,
878 struct counter_arg *carg)
880 struct netlink_serialization_params ser_params = {
883 .eval_attr = __fill_counters,
884 .post_eval_attr = __post_fill_counters,
887 netlink_serialization_command *netlink =
888 netlink_create_command(&ser_params);
890 STC_LOGE("Can not create command");
894 netlink->deserialize_answer(&(netlink->params));
897 static gboolean __process_contr_reply(GIOChannel *source,
898 GIOCondition condition,
901 int sock = g_io_channel_unix_get_fd(source);
904 stc_s *stc = stc_get_manager();
906 if ((condition & G_IO_ERR) ||
907 (condition & G_IO_HUP) ||
908 (condition & G_IO_NVAL)) {
909 /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */
911 __close_contr_sock(g_system);
916 STC_LOGE("Can't get stc data");
920 ret = read_netlink(sock,
921 &ans, sizeof(struct genl));
922 STC_LOGD("Counter data received ret [%d]", ret);
926 stc->carg->ans_len = ret;
927 __process_network_counter(&ans, stc->carg);
929 g_idle_add(__flush_apps_stats_to_database, NULL);
930 g_idle_add(__flush_rstns_counter_to_database, NULL);
935 static gboolean __update_contr_cb(void *user_data)
937 /* Here we just sent command, answer we receive in another callback */
938 stc_s *stc = stc_get_manager();
939 ret_value_msg_if(stc == NULL, STC_ERROR_FAIL, "Can't get stc data");
941 stc->carg = MALLOC0(counter_arg_s, 1);
942 stc->carg->sock = stc_monitor_get_counter_socket();
945 STC_LOGD("Get all counters");
946 nfacct_send_get_all(stc->carg);
948 /* we need to continue the timer */
952 static gboolean __rstn_tree_foreach_print(gpointer key, gpointer value,
955 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
956 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
958 __print_rstn(rstn_key, rstn_value);
962 static void __rstn_tree_printall(void)
964 g_tree_foreach(g_system->rstns, __rstn_tree_foreach_print, NULL);
967 static stc_rstn_value_s * __rstn_lookup(GTree *rstns_tree,
968 const stc_rstn_key_s *key)
970 stc_rstn_value_s *lookup;
972 ret_value_msg_if(rstns_tree == NULL, NULL, "rstns_tree is null!");
974 lookup = g_tree_lookup(rstns_tree, key);
979 static gboolean __remove_restriction(gpointer key, gpointer value,
982 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
983 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
985 /* rstn rule is already removed */
986 if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
989 __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
990 __print_rstn(rstn_key, rstn_value);
994 static gboolean __add_restriction_debug(gpointer key, gpointer value,
997 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
998 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1000 /* rstn rule is activated */
1001 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1004 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1005 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1007 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1009 __print_rstn(rstn_key, rstn_value);
1014 static gboolean __add_restriction(gpointer key, gpointer value, gpointer data)
1016 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1017 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1019 /* rstn rule is activated */
1020 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1023 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1024 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1026 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1031 static stc_error_e __rstn_tree_remove(stc_rstn_key_s *key)
1033 stc_rstn_value_s *lookup_value;
1035 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1037 lookup_value = __rstn_lookup(g_system->rstns, key);
1038 if (!lookup_value) {
1039 STC_LOGE("key not found");
1040 return STC_ERROR_NO_DATA;
1043 __remove_restriction(key, lookup_value, NULL);
1045 /* remove counter also */
1046 table_counters_delete(lookup_value->restriction_id);
1048 if (!g_tree_remove(g_system->rstns, key)) {
1049 STC_LOGD("key not found");
1050 return STC_ERROR_NO_DATA;
1053 return STC_ERROR_NONE;
1056 static stc_error_e __rstn_tree_add(stc_rstn_key_s *key,
1057 stc_rstn_value_s *value, gboolean debug)
1059 stc_rstn_value_s *rstn_value;
1061 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1063 rstn_value = __rstn_lookup(g_system->rstns, key);
1065 stc_rstn_key_s *rstn_key = MALLOC0(stc_rstn_key_s, 1);
1067 STC_LOGE("rstn_key allocation failed");
1068 return STC_ERROR_OUT_OF_MEMORY;
1071 rstn_value = MALLOC0(stc_rstn_value_s, 1);
1073 STC_LOGE("rstn_value allocation failed");
1075 return STC_ERROR_OUT_OF_MEMORY;
1078 rstn_key->app_id = g_strdup(key->app_id);
1079 rstn_key->ifname = g_strdup(key->ifname);
1080 rstn_key->imsi = g_strdup(key->imsi);
1081 rstn_key->iftype = key->iftype;
1082 rstn_key->roaming = key->roaming;
1084 g_tree_insert(g_system->rstns, rstn_key, rstn_value);
1087 rstn_value->restriction_id = value->restriction_id;
1088 rstn_value->rst_state = value->rst_state;
1089 rstn_value->classid = value->classid;
1090 rstn_value->limit.in_bytes = value->limit.in_bytes;
1091 rstn_value->limit.out_bytes = value->limit.out_bytes;
1092 rstn_value->warn_limit.in_bytes = value->warn_limit.in_bytes;
1093 rstn_value->warn_limit.out_bytes = value->warn_limit.out_bytes;
1094 rstn_value->counter.in_bytes = 0;
1095 rstn_value->counter.out_bytes = 0;
1096 rstn_value->warn_limit_crossed_notified = FALSE;
1097 rstn_value->rstn_limit_crossed_notified = FALSE;
1100 __add_restriction_debug(key, rstn_value, NULL);
1102 __add_restriction(key, rstn_value, NULL);
1104 return STC_ERROR_NONE;
1107 static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info,
1110 stc_cb_ret_e ret = STC_CONTINUE;
1113 stc_rstn_value_s value;
1115 memset(&key, 0, sizeof(stc_rstn_key_s));
1116 memset(&value, 0, sizeof(stc_rstn_value_s));
1118 key.app_id = g_strdup(info->app_id);
1119 key.ifname = g_strdup(info->ifname);
1120 key.imsi = g_strdup(info->imsi);
1121 key.iftype = info->iftype;
1122 key.roaming = info->roaming;
1124 value.rst_state = info->rst_state;
1125 value.restriction_id = info->restriction_id;
1127 if (value.rst_state != STC_RESTRICTION_EXCLUDED && info->app_id)
1128 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1130 value.classid = STC_UNKNOWN_CLASSID;
1132 value.limit.in_bytes = info->rcv_limit;
1133 value.limit.out_bytes = info->send_limit;
1134 value.warn_limit.in_bytes = info->rcv_warn_limit;
1135 value.warn_limit.out_bytes = info->send_warn_limit;
1137 if (__rstn_tree_add(&key, &value, FALSE) != STC_ERROR_NONE)
1146 static void __fill_restritions_list(void)
1148 table_restrictions_foreach(__insert_restriction_cb, NULL);
1149 //__rstn_tree_printall();
1152 static gboolean __add_rstn_foreach_application(gpointer key,
1156 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1157 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1158 gchar *app_id = (gchar *)data;
1160 /* rstn rule is not for applications */
1161 if (rstn_key->app_id == NULL)
1164 /* rstn rule is not for this application */
1165 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
1168 /* rstn rule is already applied */
1169 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1172 /* add restriction to system */
1173 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1174 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1176 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1178 __print_rstn(rstn_key, rstn_value);
1183 static void __add_rstns_for_application(gchar *app_id)
1185 g_tree_foreach(g_system->rstns, __add_rstn_foreach_application,
1189 stc_error_e stc_monitor_init(void)
1191 stc_system_s *system = MALLOC0(stc_system_s, 1);
1192 GIOChannel *gio = NULL;
1194 ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY, "stc_system_s malloc fail!");
1196 /* initializing cgroups */
1199 /* creating monitored application tree */
1200 system->apps = g_tree_new_full(__apps_tree_key_compare, NULL,
1201 __apps_tree_key_free,
1202 __apps_tree_value_free);
1204 system->rstns = g_tree_new_full(__rstns_tree_key_compare, NULL,
1205 __rstns_tree_key_free,
1206 __rstns_tree_value_free);
1208 /* create netlink socket for updating kernel counters */
1209 system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
1210 if (!(system->contr_sock)) {
1211 STC_LOGE("failed to open socket");
1213 return STC_ERROR_FAIL;
1216 gio = g_io_channel_unix_new(system->contr_sock);
1217 system->contr_gsource_id =
1218 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
1219 (GIOFunc) __process_contr_reply,
1221 g_io_channel_unref(gio);
1225 /* creating restriction rules tree */
1226 __update_contr_cb(NULL);
1228 /* registering periodic kernel counters update callback */
1229 g_system->contr_timer_id = g_timeout_add_seconds(CONTR_TIMER_INTERVAL,
1232 if (g_system->contr_timer_id == 0) {
1233 STC_LOGE("Failed to register kernel counters update timer");
1234 __close_contr_sock(g_system);
1235 return STC_ERROR_FAIL;
1238 __fill_restritions_list();
1240 return STC_ERROR_NONE;
1243 stc_error_e stc_monitor_deinit(void)
1245 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1247 /* close netlink socket for updating kernel counters */
1248 __close_contr_sock(g_system);
1250 /* remove kernel counters update timer */
1251 if (g_system->contr_timer_id > 0) {
1252 g_source_remove(g_system->contr_timer_id);
1253 g_system->contr_timer_id = 0;
1256 /* destroy monitored application tree */
1257 g_tree_destroy(g_system->apps);
1258 g_system->apps = NULL;
1260 /* destroy restriction rules tree */
1261 g_tree_destroy(g_system->rstns);
1262 g_system->rstns = NULL;
1266 return STC_ERROR_NONE;
1269 stc_error_e stc_monitor_application_add(const stc_app_key_s app_key,
1270 const stc_app_value_s app_value)
1272 stc_error_e ret = STC_ERROR_NONE;
1274 stc_app_value_s *value;
1275 stc_app_value_s *lookup;
1277 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1279 lookup = __application_lookup(g_system->apps, &app_key);
1281 STC_LOGD("app_key already present");
1282 return STC_ERROR_NONE;
1285 key = MALLOC0(stc_app_key_s, 1);
1287 STC_LOGE("key allocation failed");
1288 return STC_ERROR_OUT_OF_MEMORY;
1291 value = MALLOC0(stc_app_value_s, 1);
1293 STC_LOGE("value allocation failed");
1295 return STC_ERROR_OUT_OF_MEMORY;
1298 key->app_id = g_strdup(app_key.app_id);
1299 key->pkg_id = g_strdup(app_key.pkg_id);
1301 value->type = app_value.type;
1302 value->counter.in_bytes = app_value.counter.in_bytes;
1303 value->counter.out_bytes = app_value.counter.out_bytes;
1305 value->processes = g_tree_new_full(__processes_tree_key_compare, NULL,
1306 __processes_tree_key_free,
1307 __processes_tree_value_free);
1309 /* create cgroup and update classid */
1310 value->classid = get_classid_by_app_id(app_key.app_id, TRUE);
1312 g_tree_insert(g_system->apps, key, value);
1314 /* add nfacct rule for this classid */
1315 __add_application_monitor(key, value, stc_get_default_connection());
1316 __add_rstns_for_application(app_key.app_id);
1321 stc_error_e stc_monitor_process_add(const stc_app_key_s app_key,
1322 const stc_process_key_s proc_key,
1323 const stc_process_value_s proc_value)
1325 stc_error_e ret = STC_ERROR_NONE;
1326 stc_app_value_s *app_lookup;
1327 stc_process_key_s *key;
1328 stc_process_value_s *value;
1329 stc_process_value_s *proc_lookup;
1331 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1333 app_lookup = __application_lookup(g_system->apps, &app_key);
1335 STC_LOGD("app_key not found");
1336 return STC_ERROR_FAIL;
1339 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1341 STC_LOGD("proc_key already present");
1342 return STC_ERROR_NONE;
1345 key = MALLOC0(stc_process_key_s, 1);
1347 STC_LOGE("key allocation failed");
1348 return STC_ERROR_OUT_OF_MEMORY;
1351 value = MALLOC0(stc_process_value_s, 1);
1353 STC_LOGE("value allocation failed");
1355 return STC_ERROR_OUT_OF_MEMORY;
1358 key->pid = proc_key.pid;
1360 value->ground = proc_value.ground;
1362 g_tree_insert(app_lookup->processes, key, value);
1364 /* add pid to application cgroup */
1365 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1370 stc_error_e stc_monitor_process_remove(pid_t pid)
1372 stc_error_e ret = STC_ERROR_NONE;
1373 stc_process_key_s proc_key = {
1377 remove_pid_context_s context = {
1379 .proc_key = &proc_key,
1380 .entry_removed = FALSE,
1383 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1385 g_tree_foreach(g_system->apps, __apps_tree_foreach_remove_pid,
1388 if (context.entry_removed)
1389 __application_remove_if_empty(context.app_key);
1394 stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key,
1395 const stc_process_key_s proc_key,
1396 stc_app_state_e ground)
1398 stc_error_e ret = STC_ERROR_NONE;
1399 stc_app_value_s *app_lookup;
1400 stc_process_value_s *proc_lookup;
1402 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1404 app_lookup = __application_lookup(g_system->apps, &app_key);
1406 STC_LOGD("app_key not found");
1407 return STC_ERROR_FAIL;
1410 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1412 STC_LOGD("proc_key not found");
1413 return STC_ERROR_FAIL;
1416 if (proc_lookup->ground != ground)
1417 proc_lookup->ground = ground;
1419 if (ground == STC_APP_STATE_BACKGROUND) {
1420 char *background_app_id = g_strconcat(app_key.app_id,
1421 STC_BACKGROUND_APP_SUFFIX,
1423 place_pids_to_net_cgroup(proc_key.pid, background_app_id);
1424 g_free(background_app_id);
1426 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1432 void stc_monitor_update_rstn_by_default_connection(void *data)
1434 static default_connection_s old_connection;
1435 default_connection_s *new_connection = (default_connection_s *)data;
1437 if (old_connection.path != NULL) {
1439 g_tree_foreach(g_system->apps,
1440 __remove_application_monitor,
1441 (gpointer)&old_connection);
1443 if (g_system->rstns)
1444 g_tree_foreach(g_system->rstns,
1445 __remove_restriction,
1446 (gpointer)&old_connection);
1449 FREE(old_connection.path);
1450 FREE(old_connection.ifname);
1451 old_connection.type = 0;
1452 old_connection.roaming = 0;
1454 if (new_connection != NULL && new_connection->path != NULL) {
1456 g_tree_foreach(g_system->apps,
1457 __add_application_monitor,
1458 (gpointer)new_connection);
1460 if (g_system->rstns)
1461 g_tree_foreach(g_system->rstns, __add_restriction,
1464 old_connection.path = g_strdup(new_connection->path);
1465 old_connection.ifname = g_strdup(new_connection->ifname);
1466 old_connection.type = new_connection->type;
1467 old_connection.roaming = new_connection->roaming;
1471 stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info)
1476 stc_rstn_value_s value;
1478 memset(&key, 0, sizeof(stc_rstn_key_s));
1479 memset(&value, 0, sizeof(stc_rstn_value_s));
1481 key.app_id = g_strdup(info->app_id);
1482 key.ifname = g_strdup(info->ifname);
1483 key.imsi = g_strdup(info->imsi);
1484 key.iftype = info->iftype;
1485 key.roaming = info->roaming;
1487 value.rst_state = info->rst_state;
1488 value.restriction_id = info->restriction_id;
1490 if (value.rst_state != STC_RESTRICTION_EXCLUDED && info->app_id)
1491 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1493 value.classid = STC_UNKNOWN_CLASSID;
1495 value.limit.in_bytes = info->rcv_limit;
1496 value.limit.out_bytes = info->send_limit;
1497 value.warn_limit.in_bytes = info->rcv_warn_limit;
1498 value.warn_limit.out_bytes = info->send_warn_limit;
1500 ret = __rstn_tree_add(&key, &value, TRUE);
1508 stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info)
1512 stc_rstn_key_s key = {
1513 .app_id = g_strdup(info->app_id),
1514 .ifname = g_strdup(info->ifname),
1515 .imsi = g_strdup(info->imsi),
1516 .iftype = info->iftype,
1517 .roaming = info->roaming,
1520 ret = __rstn_tree_remove(&key);
1528 int stc_monitor_get_counter_socket(void)
1530 return g_system->contr_sock;