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);
418 rstn_value->rst_state = STC_RESTRICTION_ACTIVATED;
419 rstn_value->in_limit_reached = FALSE;
420 rstn_value->out_limit_reached = FALSE;
426 rstn_value->rst_state = STC_RESTRICTION_REMOVED;
427 rstn_value->in_limit_reached = FALSE;
428 rstn_value->out_limit_reached = FALSE;
435 static gboolean __remove_rstns_foreach_application(gpointer key,
439 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
440 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
441 gchar *app_id = (gchar *)data;
443 /* rstn rule is not for applications */
444 if (rstn_key->app_id == NULL)
447 /* rstn rule is not for this application */
448 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
451 /* rstn rule is already removed */
452 if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
455 /* remove restriction from system */
456 __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
458 __print_rstn(rstn_key, rstn_value);
463 static void __remove_rstns_for_application(gchar *app_id)
465 g_tree_foreach(g_system->rstns, __remove_rstns_foreach_application,
469 static stc_error_e __application_remove_if_empty(const stc_app_key_s *app_key)
471 stc_error_e ret = STC_ERROR_NONE;
473 stc_app_value_s *lookup;
475 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
477 lookup = __application_lookup(g_system->apps, app_key);
479 STC_LOGE("app_key not found");
480 return STC_ERROR_NO_DATA;
483 g_tree_foreach(lookup->processes, __processes_tree_check_empty,
487 /* remove nfacct rule for this classid */
488 __remove_application_monitor((gpointer) app_key, lookup,
489 stc_get_default_connection());
490 __remove_rstns_for_application(app_key->app_id);
493 if (!g_tree_remove(g_system->apps, app_key)) {
494 ret = STC_ERROR_NO_DATA;
495 STC_LOGE("key not found");
501 static stc_error_e __close_contr_sock(stc_system_s *system)
503 ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
505 /* close netlink socket for updating kernel counters */
506 if (g_system->contr_sock != -1) {
507 close(g_system->contr_sock);
508 g_system->contr_sock = -1;
511 if (g_system->contr_gsource_id != 0) {
512 g_source_remove(g_system->contr_gsource_id);
513 g_system->contr_gsource_id = 0;
516 return STC_ERROR_NONE;
519 static gboolean __rstn_counter_update_foreach_classid(gpointer key,
523 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
524 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
525 classid_bytes_context_s *context = (classid_bytes_context_s *)data;
527 if (context->counter->intend != NFACCT_COUNTER)
528 goto try_next_callback;
530 if (rstn_value->classid != context->counter->classid)
531 goto try_next_callback;
533 if (rstn_value->in_limit_reached == TRUE) {
534 context->in_limit_reached = TRUE;
535 goto try_next_callback;
538 if (rstn_value->out_limit_reached == TRUE) {
539 context->out_limit_reached = TRUE;
540 goto try_next_callback;
543 switch (context->counter->iotype) {
544 case NFACCT_COUNTER_IN:
545 rstn_value->counter.in_bytes += context->bytes;
547 if (rstn_value->counter.in_bytes >= rstn_value->warn_limit.in_bytes
548 && rstn_value->warn_limit_crossed_notified == FALSE) {
550 stc_s *stc = (stc_s *)stc_get_manager();
551 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
553 rv = stc_manager_dbus_emit_signal(stc->connection,
554 STC_DBUS_SERVICE_RESTRICTION_PATH,
555 STC_DBUS_INTERFACE_RESTRICTION,
556 "WarnThresholdCrossed",
557 g_variant_new("(s)", rstn_key->app_id));
559 rstn_value->warn_limit_crossed_notified = TRUE;
562 /* block immediately */
563 if (rstn_value->counter.in_bytes >= rstn_value->limit.in_bytes) {
564 context->counter->intend = NFACCT_BLOCK;
565 __del_iptables_in(context->counter);
566 __add_iptables_in(context->counter);
567 context->counter->intend = NFACCT_COUNTER;
568 rstn_value->in_limit_reached = TRUE;
570 if (rstn_value->rstn_limit_crossed_notified == FALSE) {
572 stc_s *stc = (stc_s *)stc_get_manager();
573 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
575 rv = stc_manager_dbus_emit_signal(stc->connection,
576 STC_DBUS_SERVICE_RESTRICTION_PATH,
577 STC_DBUS_INTERFACE_RESTRICTION,
578 "RestrictionThresholdCrossed",
579 g_variant_new("(s)", rstn_key->app_id));
581 rstn_value->rstn_limit_crossed_notified = TRUE;
585 g_system->rstns_tree_updated = TRUE;
586 __print_rstn(rstn_key, rstn_value);
588 case NFACCT_COUNTER_OUT:
589 rstn_value->counter.out_bytes += context->bytes;
591 if (rstn_value->counter.out_bytes >= rstn_value->limit.out_bytes
592 && rstn_value->warn_limit_crossed_notified == FALSE) {
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 "WarnThresholdCrossed",
601 g_variant_new("(s)", rstn_key->app_id));
603 rstn_value->warn_limit_crossed_notified = TRUE;
606 /* block immediately */
607 if (rstn_value->counter.out_bytes >= rstn_value->limit.out_bytes) {
608 context->counter->intend = NFACCT_BLOCK;
609 __del_iptables_out(context->counter);
610 __add_iptables_out(context->counter);
611 context->counter->intend = NFACCT_COUNTER;
612 rstn_value->out_limit_reached = TRUE;
614 if (rstn_value->rstn_limit_crossed_notified == FALSE) {
616 stc_s *stc = (stc_s *)stc_get_manager();
617 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
619 rv = stc_manager_dbus_emit_signal(stc->connection,
620 STC_DBUS_SERVICE_RESTRICTION_PATH,
621 STC_DBUS_INTERFACE_RESTRICTION,
622 "RestrictionThresholdCrossed",
623 g_variant_new("(s)", rstn_key->app_id));
625 rstn_value->rstn_limit_crossed_notified = TRUE;
629 g_system->rstns_tree_updated = TRUE;
630 __print_rstn(rstn_key, rstn_value);
633 STC_LOGE("unknown iotype");
641 static gboolean __update_app_statistics(gpointer key, gpointer value,
644 stc_app_key_s *app_key = (stc_app_key_s *)key;
645 stc_app_value_s *app_value = (stc_app_value_s *)value;
646 time_t *touch_time = (time_t *)data;
647 stc_db_classid_iftype_key stat_key;
648 stc_db_app_stats stat;
649 char *default_ifname = stc_default_connection_get_ifname();
651 memset(&stat_key, 0, sizeof(stc_db_classid_iftype_key));
652 memset(&stat, 0 , sizeof(stc_db_app_stats));
654 stat_key.classid = app_value->classid;
655 stat_key.iftype = stc_default_connection_get_type();
656 if (STC_IFACE_DATACALL == stat_key.iftype)
657 stat_key.imsi = g_strdup("unknown");
659 stat_key.imsi = g_strdup("noneimsi");
660 g_strlcpy(stat_key.ifname, default_ifname, MAX_IFACE_LENGTH);
662 stat.app_id = g_strdup(app_key->app_id);
663 stat.snd_count = app_value->counter.out_bytes;
664 stat.rcv_count = app_value->counter.in_bytes;
667 stat.is_roaming = stc_default_connection_get_roaming();
668 stat.ground = STC_APP_STATE_UNKNOWN;
670 table_statistics_insert(&stat_key, &stat, *touch_time);
672 app_value->counter.out_bytes = 0;
673 app_value->counter.in_bytes = 0;
677 FREE(default_ifname);
682 static gboolean __flush_apps_stats_to_database(gpointer user_data)
684 time_t current_time = time(0);
686 if (g_system->apps_tree_updated == FALSE)
687 return G_SOURCE_REMOVE;
689 g_system->apps_tree_updated = FALSE;
692 g_tree_foreach(g_system->apps,
693 __update_app_statistics,
696 STC_LOGI("Flushed app stats to database");
697 return G_SOURCE_REMOVE;
700 static gboolean __update_counter_statistics(gpointer key, gpointer value,
703 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
704 table_counters_info info = {
705 .restriction_id = rstn_value->restriction_id,
706 .sent_bytes = rstn_value->counter.out_bytes,
707 .rcv_bytes = rstn_value->counter.in_bytes
710 table_counters_update_counters(&info);
715 static gboolean __flush_rstns_counter_to_database(gpointer user_data)
717 time_t current_time = time(0);
719 if (g_system->rstns_tree_updated == FALSE)
720 return G_SOURCE_REMOVE;
722 g_system->rstns_tree_updated = FALSE;
725 g_tree_foreach(g_system->rstns,
726 __update_counter_statistics,
729 STC_LOGI("Flushed rstns counters to database");
730 return G_SOURCE_REMOVE;
733 static gboolean __apps_counter_update_foreach_classid(gpointer key,
737 stc_app_key_s *app_key = (stc_app_key_s *)key;
738 stc_app_value_s *app_value = (stc_app_value_s *)value;
739 classid_bytes_context_s *context = (classid_bytes_context_s *)data;
741 if (context->counter->intend != NFACCT_COUNTER)
742 goto try_next_callback;
744 if (app_value->classid != context->counter->classid)
745 goto try_next_callback;
747 switch (context->counter->iotype) {
748 case NFACCT_COUNTER_IN:
749 app_value->data_usage.in_bytes += context->bytes;
750 app_value->counter.in_bytes = context->bytes;
751 g_system->apps_tree_updated = TRUE;
753 __apps_tree_foreach_print(app_key, app_value, NULL);
755 case NFACCT_COUNTER_OUT:
756 app_value->data_usage.out_bytes += context->bytes;
757 app_value->counter.out_bytes = context->bytes;
758 g_system->apps_tree_updated = TRUE;
760 __apps_tree_foreach_print(app_key, app_value, NULL);
763 STC_LOGE("unknown iotype");
770 static void __fill_nfacct_result(char *cnt_name, int64_t bytes,
771 struct counter_arg *carg)
773 struct nfacct_rule counter = {
780 classid_bytes_context_s context = {
783 .in_limit_reached = FALSE,
784 .out_limit_reached = FALSE
787 STC_LOGD("cnt_name %s", cnt_name);
789 if (!recreate_counter_by_name(cnt_name, &counter)) {
790 STC_LOGE("Can't parse counter name %s", cnt_name);
794 STC_LOGI("classid %lu, iftype %u, iotype %d, intend %d, ifname %s, bytes %lld",
795 context.counter->classid, context.counter->iftype,
796 context.counter->iotype, context.counter->intend,
797 context.counter->ifname, context.bytes);
800 g_tree_foreach(g_system->rstns,
801 __rstn_counter_update_foreach_classid,
805 g_tree_foreach(g_system->apps,
806 __apps_counter_update_foreach_classid,
810 static int __fill_counters(struct rtattr *attr_list[__NFACCT_MAX],
813 struct counter_arg *carg = user_data;
814 char *cnt_name = (char *)RTA_DATA(attr_list[NFACCT_NAME]);
815 if (carg->initiate) {
817 * TODO: this will be used when daemon starts to update existing
818 * counter data if present.
820 populate_counters(cnt_name, carg);
824 (int64_t *)RTA_DATA(attr_list[NFACCT_BYTES]);
825 int bytes = be64toh(*bytes_p);
827 ++carg->serialized_counters;
828 __fill_nfacct_result(cnt_name, bytes, carg);
835 static int __post_fill_counters(void *user_data)
837 struct counter_arg *carg = user_data;
845 static void __process_network_counter(struct genl *ans,
846 struct counter_arg *carg)
848 struct netlink_serialization_params ser_params = {
851 .eval_attr = __fill_counters,
852 .post_eval_attr = __post_fill_counters,
855 netlink_serialization_command *netlink =
856 netlink_create_command(&ser_params);
858 STC_LOGE("Can not create command");
862 netlink->deserialize_answer(&(netlink->params));
865 static gboolean __process_contr_reply(GIOChannel *source,
866 GIOCondition condition,
869 int sock = g_io_channel_unix_get_fd(source);
872 stc_s *stc = stc_get_manager();
874 if ((condition & G_IO_ERR) ||
875 (condition & G_IO_HUP) ||
876 (condition & G_IO_NVAL)) {
877 /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */
879 __close_contr_sock(g_system);
884 STC_LOGE("Can't get stc data");
888 ret = read_netlink(sock,
889 &ans, sizeof(struct genl));
890 STC_LOGD("Counter data received ret [%d]", ret);
894 stc->carg->ans_len = ret;
895 __process_network_counter(&ans, stc->carg);
897 g_idle_add(__flush_apps_stats_to_database, NULL);
898 g_idle_add(__flush_rstns_counter_to_database, NULL);
903 static gboolean __update_contr_cb(void *user_data)
905 /* Here we just sent command, answer we receive in another callback */
906 stc_s *stc = stc_get_manager();
907 ret_value_msg_if(stc == NULL, STC_ERROR_FAIL, "Can't get stc data");
909 stc->carg = MALLOC0(counter_arg_s, 1);
910 stc->carg->sock = stc_monitor_get_counter_socket();
913 STC_LOGD("Get all counters");
914 nfacct_send_get_all(stc->carg);
916 /* we need to continue the timer */
920 static gboolean __rstn_tree_foreach_print(gpointer key, gpointer value,
923 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
924 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
926 __print_rstn(rstn_key, rstn_value);
930 static void __rstn_tree_printall(void)
932 g_tree_foreach(g_system->rstns, __rstn_tree_foreach_print, NULL);
935 static stc_rstn_value_s * __rstn_lookup(GTree *rstns_tree,
936 const stc_rstn_key_s *key)
938 stc_rstn_value_s *lookup;
940 ret_value_msg_if(rstns_tree == NULL, NULL, "rstns_tree is null!");
942 lookup = g_tree_lookup(rstns_tree, key);
947 static gboolean __remove_restriction(gpointer key, gpointer value,
950 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
951 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
953 /* rstn rule is already removed */
954 if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
957 __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
958 __print_rstn(rstn_key, rstn_value);
962 static gboolean __add_restriction_debug(gpointer key, gpointer value,
965 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
966 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
968 /* rstn rule is activated */
969 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
972 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
973 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
975 __process_restriction(RST_SET, rstn_key, rstn_value, data);
977 __print_rstn(rstn_key, rstn_value);
982 static gboolean __add_restriction(gpointer key, gpointer value, gpointer data)
984 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
985 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
987 /* rstn rule is activated */
988 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
991 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
992 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
994 __process_restriction(RST_SET, rstn_key, rstn_value, data);
999 static stc_error_e __rstn_tree_remove(stc_rstn_key_s *key)
1001 stc_rstn_value_s *lookup_value;
1003 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1005 lookup_value = __rstn_lookup(g_system->rstns, key);
1006 if (!lookup_value) {
1007 STC_LOGE("key not found");
1008 return STC_ERROR_NO_DATA;
1011 __remove_restriction(key, lookup_value, NULL);
1013 /* remove counter also */
1014 table_counters_delete(lookup_value->restriction_id);
1016 if (!g_tree_remove(g_system->rstns, key)) {
1017 STC_LOGD("key not found");
1018 return STC_ERROR_NO_DATA;
1021 return STC_ERROR_NONE;
1024 static stc_error_e __rstn_tree_add(stc_rstn_key_s *key,
1025 stc_rstn_value_s *value, gboolean debug)
1027 stc_rstn_value_s *rstn_value;
1029 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1031 rstn_value = __rstn_lookup(g_system->rstns, key);
1033 stc_rstn_key_s *rstn_key = MALLOC0(stc_rstn_key_s, 1);
1035 STC_LOGE("rstn_key allocation failed");
1036 return STC_ERROR_OUT_OF_MEMORY;
1039 rstn_value = MALLOC0(stc_rstn_value_s, 1);
1041 STC_LOGE("rstn_value allocation failed");
1043 return STC_ERROR_OUT_OF_MEMORY;
1046 rstn_key->app_id = g_strdup(key->app_id);
1047 rstn_key->ifname = g_strdup(key->ifname);
1048 rstn_key->imsi = g_strdup(key->imsi);
1049 rstn_key->iftype = key->iftype;
1050 rstn_key->roaming = key->roaming;
1052 g_tree_insert(g_system->rstns, rstn_key, rstn_value);
1055 rstn_value->restriction_id = value->restriction_id;
1056 rstn_value->rst_state = value->rst_state;
1057 rstn_value->classid = value->classid;
1058 rstn_value->limit.in_bytes = value->limit.in_bytes;
1059 rstn_value->limit.out_bytes = value->limit.out_bytes;
1060 rstn_value->warn_limit.in_bytes = value->warn_limit.in_bytes;
1061 rstn_value->warn_limit.out_bytes = value->warn_limit.out_bytes;
1062 rstn_value->counter.in_bytes = 0;
1063 rstn_value->counter.out_bytes = 0;
1064 rstn_value->warn_limit_crossed_notified = FALSE;
1065 rstn_value->rstn_limit_crossed_notified = FALSE;
1068 __add_restriction_debug(key, rstn_value, NULL);
1070 __add_restriction(key, rstn_value, NULL);
1072 return STC_ERROR_NONE;
1075 static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info,
1078 stc_cb_ret_e ret = STC_CONTINUE;
1081 stc_rstn_value_s value;
1083 memset(&key, 0, sizeof(stc_rstn_key_s));
1084 memset(&value, 0, sizeof(stc_rstn_value_s));
1086 key.app_id = g_strdup(info->app_id);
1087 key.ifname = g_strdup(info->ifname);
1088 key.imsi = g_strdup(info->imsi);
1089 key.iftype = info->iftype;
1090 key.roaming = info->roaming;
1092 value.rst_state = info->rst_state;
1093 value.restriction_id = info->restriction_id;
1095 if (value.rst_state != STC_RESTRICTION_EXCLUDED && info->app_id)
1096 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1098 value.classid = STC_UNKNOWN_CLASSID;
1100 value.limit.in_bytes = info->rcv_limit;
1101 value.limit.out_bytes = info->send_limit;
1102 value.warn_limit.in_bytes = info->rcv_warn_limit;
1103 value.warn_limit.out_bytes = info->send_warn_limit;
1105 if (__rstn_tree_add(&key, &value, FALSE) != STC_ERROR_NONE)
1114 static void __fill_restritions_list(void)
1116 table_restrictions_foreach(__insert_restriction_cb, NULL);
1117 //__rstn_tree_printall();
1120 static gboolean __add_rstn_foreach_application(gpointer key,
1124 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1125 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1126 gchar *app_id = (gchar *)data;
1128 /* rstn rule is not for applications */
1129 if (rstn_key->app_id == NULL)
1132 /* rstn rule is not for this application */
1133 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
1136 /* rstn rule is already applied */
1137 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1140 /* add restriction to system */
1141 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1142 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1144 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1146 __print_rstn(rstn_key, rstn_value);
1151 static void __add_rstns_for_application(gchar *app_id)
1153 g_tree_foreach(g_system->rstns, __add_rstn_foreach_application,
1157 stc_error_e stc_monitor_init(void)
1159 stc_system_s *system = MALLOC0(stc_system_s, 1);
1160 GIOChannel *gio = NULL;
1162 ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY, "stc_system_s malloc fail!");
1164 /* initializing cgroups */
1167 /* creating monitored application tree */
1168 system->apps = g_tree_new_full(__apps_tree_key_compare, NULL,
1169 __apps_tree_key_free,
1170 __apps_tree_value_free);
1172 system->rstns = g_tree_new_full(__rstns_tree_key_compare, NULL,
1173 __rstns_tree_key_free,
1174 __rstns_tree_value_free);
1176 /* create netlink socket for updating kernel counters */
1177 system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
1178 if (!(system->contr_sock)) {
1179 STC_LOGE("failed to open socket");
1181 return STC_ERROR_FAIL;
1184 gio = g_io_channel_unix_new(system->contr_sock);
1185 system->contr_gsource_id =
1186 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
1187 (GIOFunc) __process_contr_reply,
1189 g_io_channel_unref(gio);
1193 /* creating restriction rules tree */
1194 __update_contr_cb(NULL);
1196 /* registering periodic kernel counters update callback */
1197 g_system->contr_timer_id = g_timeout_add_seconds(CONTR_TIMER_INTERVAL,
1200 if (g_system->contr_timer_id == 0) {
1201 STC_LOGE("Failed to register kernel counters update timer");
1202 __close_contr_sock(g_system);
1203 return STC_ERROR_FAIL;
1206 __fill_restritions_list();
1208 return STC_ERROR_NONE;
1211 stc_error_e stc_monitor_deinit(void)
1213 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1215 /* close netlink socket for updating kernel counters */
1216 __close_contr_sock(g_system);
1218 /* remove kernel counters update timer */
1219 if (g_system->contr_timer_id > 0) {
1220 g_source_remove(g_system->contr_timer_id);
1221 g_system->contr_timer_id = 0;
1224 /* destroy monitored application tree */
1225 g_tree_destroy(g_system->apps);
1226 g_system->apps = NULL;
1228 /* destroy restriction rules tree */
1229 g_tree_destroy(g_system->rstns);
1230 g_system->rstns = NULL;
1234 return STC_ERROR_NONE;
1237 stc_error_e stc_monitor_application_add(const stc_app_key_s app_key,
1238 const stc_app_value_s app_value)
1240 stc_error_e ret = STC_ERROR_NONE;
1242 stc_app_value_s *value;
1243 stc_app_value_s *lookup;
1245 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1247 lookup = __application_lookup(g_system->apps, &app_key);
1249 STC_LOGD("app_key already present");
1250 return STC_ERROR_NONE;
1253 key = MALLOC0(stc_app_key_s, 1);
1255 STC_LOGE("key allocation failed");
1256 return STC_ERROR_OUT_OF_MEMORY;
1259 value = MALLOC0(stc_app_value_s, 1);
1261 STC_LOGE("value allocation failed");
1263 return STC_ERROR_OUT_OF_MEMORY;
1266 key->app_id = g_strdup(app_key.app_id);
1267 key->pkg_id = g_strdup(app_key.pkg_id);
1269 value->type = app_value.type;
1270 value->counter.in_bytes = app_value.counter.in_bytes;
1271 value->counter.out_bytes = app_value.counter.out_bytes;
1273 value->processes = g_tree_new_full(__processes_tree_key_compare, NULL,
1274 __processes_tree_key_free,
1275 __processes_tree_value_free);
1277 /* create cgroup and update classid */
1278 value->classid = get_classid_by_app_id(app_key.app_id, TRUE);
1280 g_tree_insert(g_system->apps, key, value);
1282 /* add nfacct rule for this classid */
1283 __add_application_monitor(key, value, stc_get_default_connection());
1284 __add_rstns_for_application(app_key.app_id);
1289 stc_error_e stc_monitor_process_add(const stc_app_key_s app_key,
1290 const stc_process_key_s proc_key,
1291 const stc_process_value_s proc_value)
1293 stc_error_e ret = STC_ERROR_NONE;
1294 stc_app_value_s *app_lookup;
1295 stc_process_key_s *key;
1296 stc_process_value_s *value;
1297 stc_process_value_s *proc_lookup;
1299 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1301 app_lookup = __application_lookup(g_system->apps, &app_key);
1303 STC_LOGD("app_key not found");
1304 return STC_ERROR_FAIL;
1307 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1309 STC_LOGD("proc_key already present");
1310 return STC_ERROR_NONE;
1313 key = MALLOC0(stc_process_key_s, 1);
1315 STC_LOGE("key allocation failed");
1316 return STC_ERROR_OUT_OF_MEMORY;
1319 value = MALLOC0(stc_process_value_s, 1);
1321 STC_LOGE("value allocation failed");
1323 return STC_ERROR_OUT_OF_MEMORY;
1326 key->pid = proc_key.pid;
1328 value->ground = proc_value.ground;
1330 g_tree_insert(app_lookup->processes, key, value);
1332 /* add pid to application cgroup */
1333 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1338 stc_error_e stc_monitor_process_remove(pid_t pid)
1340 stc_error_e ret = STC_ERROR_NONE;
1341 stc_process_key_s proc_key = {
1345 remove_pid_context_s context = {
1347 .proc_key = &proc_key,
1348 .entry_removed = FALSE,
1351 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1353 g_tree_foreach(g_system->apps, __apps_tree_foreach_remove_pid,
1356 if (context.entry_removed)
1357 __application_remove_if_empty(context.app_key);
1362 stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key,
1363 const stc_process_key_s proc_key,
1364 stc_app_state_e ground)
1366 stc_error_e ret = STC_ERROR_NONE;
1367 stc_app_value_s *app_lookup;
1368 stc_process_value_s *proc_lookup;
1370 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1372 app_lookup = __application_lookup(g_system->apps, &app_key);
1374 STC_LOGD("app_key not found");
1375 return STC_ERROR_FAIL;
1378 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1380 STC_LOGD("proc_key not found");
1381 return STC_ERROR_FAIL;
1384 if (proc_lookup->ground != ground)
1385 proc_lookup->ground = ground;
1387 if (ground == STC_APP_STATE_BACKGROUND) {
1388 char *background_app_id = g_strconcat(app_key.app_id,
1389 STC_BACKGROUND_APP_SUFFIX,
1391 place_pids_to_net_cgroup(proc_key.pid, background_app_id);
1392 g_free(background_app_id);
1394 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1400 void stc_monitor_update_rstn_by_default_connection(void *data)
1402 static default_connection_s old_connection;
1403 default_connection_s *new_connection = (default_connection_s *)data;
1405 if (old_connection.path != NULL) {
1407 g_tree_foreach(g_system->apps,
1408 __remove_application_monitor,
1409 (gpointer)&old_connection);
1411 if (g_system->rstns)
1412 g_tree_foreach(g_system->rstns,
1413 __remove_restriction,
1414 (gpointer)&old_connection);
1417 FREE(old_connection.path);
1418 FREE(old_connection.ifname);
1419 old_connection.type = 0;
1420 old_connection.roaming = 0;
1422 if (new_connection != NULL && new_connection->path != NULL) {
1424 g_tree_foreach(g_system->apps,
1425 __add_application_monitor,
1426 (gpointer)new_connection);
1428 if (g_system->rstns)
1429 g_tree_foreach(g_system->rstns, __add_restriction,
1432 old_connection.path = g_strdup(new_connection->path);
1433 old_connection.ifname = g_strdup(new_connection->ifname);
1434 old_connection.type = new_connection->type;
1435 old_connection.roaming = new_connection->roaming;
1439 stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info)
1444 stc_rstn_value_s value;
1446 memset(&key, 0, sizeof(stc_rstn_key_s));
1447 memset(&value, 0, sizeof(stc_rstn_value_s));
1449 key.app_id = g_strdup(info->app_id);
1450 key.ifname = g_strdup(info->ifname);
1451 key.imsi = g_strdup(info->imsi);
1452 key.iftype = info->iftype;
1453 key.roaming = info->roaming;
1455 value.rst_state = info->rst_state;
1456 value.restriction_id = info->restriction_id;
1458 if (value.rst_state != STC_RESTRICTION_EXCLUDED && info->app_id)
1459 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1461 value.classid = STC_UNKNOWN_CLASSID;
1463 value.limit.in_bytes = info->rcv_limit;
1464 value.limit.out_bytes = info->send_limit;
1465 value.warn_limit.in_bytes = info->rcv_warn_limit;
1466 value.warn_limit.out_bytes = info->send_warn_limit;
1468 ret = __rstn_tree_add(&key, &value, TRUE);
1476 stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info)
1480 stc_rstn_key_s key = {
1481 .app_id = g_strdup(info->app_id),
1482 .ifname = g_strdup(info->ifname),
1483 .imsi = g_strdup(info->imsi),
1484 .iftype = info->iftype,
1485 .roaming = info->roaming,
1488 ret = __rstn_tree_remove(&key);
1496 int stc_monitor_get_counter_socket(void)
1498 return g_system->contr_sock;