2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <linux/netlink.h>
19 #include "stc-default-connection.h"
20 #include "helper-nl.h"
21 #include "helper-nfacct-rule.h"
22 #include "helper-net-cls.h"
23 #include "helper-cgroup.h"
25 #include "table-statistics.h"
26 #include "table-counters.h"
27 #include "stc-monitor.h"
30 stc_app_key_s *app_key;
31 stc_process_key_s *proc_key;
32 gboolean entry_removed;
33 } remove_pid_context_s;
36 struct nfacct_rule *counter;
38 gboolean in_limit_reached;
39 gboolean out_limit_reached;
40 } classid_bytes_context_s;
42 static stc_system_s *g_system = NULL;
44 static nfacct_rule_jump __get_jump_by_intend(struct nfacct_rule *counter)
46 if (counter->intend == NFACCT_WARN)
47 return NFACCT_JUMP_ACCEPT;
48 else if (counter->intend == NFACCT_BLOCK)
49 return NFACCT_JUMP_REJECT;
51 return NFACCT_JUMP_UNKNOWN;
54 static stc_error_e __add_iptables_in(struct nfacct_rule *counter)
56 return produce_net_rule(counter, 0, 0,
57 NFACCT_ACTION_INSERT, __get_jump_by_intend(counter),
61 static stc_error_e __add_iptables_out(struct nfacct_rule *counter)
63 return produce_net_rule(counter, 0, 0,
64 NFACCT_ACTION_INSERT, __get_jump_by_intend(counter),
68 static stc_error_e __del_iptables_in(struct nfacct_rule *counter)
70 return produce_net_rule(counter, 0, 0,
71 NFACCT_ACTION_DELETE, __get_jump_by_intend(counter),
75 static stc_error_e __del_iptables_out(struct nfacct_rule *counter)
77 return produce_net_rule(counter, 0, 0,
78 NFACCT_ACTION_DELETE, __get_jump_by_intend(counter),
82 static int __processes_tree_key_compare(gconstpointer a, gconstpointer b,
83 gpointer UNUSED user_data)
85 stc_process_key_s *key_a = (stc_process_key_s *)a;
86 stc_process_key_s *key_b = (stc_process_key_s *)b;
88 return key_a->pid - key_b->pid;
91 static void __processes_tree_value_free(gpointer data)
93 stc_process_value_s *value = (stc_process_value_s *)data;
98 static void __processes_tree_key_free(gpointer data)
100 stc_process_key_s *key = (stc_process_key_s *)data;
105 static int __apps_tree_key_compare(gconstpointer a, gconstpointer b,
106 gpointer UNUSED user_data)
108 stc_app_key_s *key_a = (stc_app_key_s *)a;
109 stc_app_key_s *key_b = (stc_app_key_s *)b;
112 ret = g_strcmp0(key_a->pkg_id, key_b->pkg_id);
116 return g_strcmp0(key_a->app_id, key_b->app_id);
119 static void __apps_tree_value_free(gpointer data)
121 stc_app_value_s *value = (stc_app_value_s *)data;
123 g_tree_destroy(value->processes);
124 value->processes = NULL;
129 static void __apps_tree_key_free(gpointer data)
131 stc_app_key_s *key = (stc_app_key_s *)data;
138 static int __rstns_tree_key_compare(gconstpointer a, gconstpointer b,
139 gpointer UNUSED user_data)
141 stc_rstn_key_s *key_a = (stc_rstn_key_s *)a;
142 stc_rstn_key_s *key_b = (stc_rstn_key_s *)b;
145 ret = g_strcmp0(key_a->app_id, key_b->app_id);
149 ret = g_strcmp0(key_a->ifname, key_b->ifname);
153 ret = g_strcmp0(key_a->imsi, key_b->imsi);
157 ret = key_a->iftype - key_b->iftype;
164 static void __rstns_tree_value_free(gpointer data)
166 stc_rstn_value_s *value = (stc_rstn_value_s *)data;
171 static void __rstns_tree_key_free(gpointer data)
173 stc_rstn_key_s *key = (stc_rstn_key_s *)data;
181 static gboolean __processes_tree_foreach_print(gpointer key, gpointer value,
184 stc_process_key_s *proc_key = (stc_process_key_s *)key;
185 stc_process_value_s *proc_value = (stc_process_value_s *)value;
187 STC_LOGD("Process entry => PID [%d], Ground state [%d]",
188 proc_key->pid, proc_value->ground);
192 static void __processes_tree_printall(GTree *processes)
194 g_tree_foreach(processes, __processes_tree_foreach_print, NULL);
197 static gboolean __apps_tree_foreach_print(gpointer key, gpointer value,
200 stc_app_key_s *app_key = (stc_app_key_s *)key;
201 stc_app_value_s *app_value = (stc_app_value_s *)value;
203 STC_LOGD("Application info => Pkg ID [%s], App ID [%s],"
204 " Type [%d], classid [%d],"
205 " counter [ in (%lld), out (%lld)]",
206 app_key->pkg_id, app_key->app_id,
207 app_value->type, app_value->classid,
208 app_value->data_usage.in_bytes, app_value->data_usage.out_bytes);
210 __processes_tree_printall(app_value->processes);
215 static void __apps_tree_printall(void)
217 g_tree_foreach(g_system->apps, __apps_tree_foreach_print, NULL);
221 static gboolean __apps_tree_foreach_remove_pid(gpointer key, gpointer value,
224 remove_pid_context_s *context = (remove_pid_context_s *)data;
225 stc_app_value_s *app_value = (stc_app_value_s *)value;
227 if (!g_tree_remove(app_value->processes, context->proc_key)) {
228 STC_LOGD("key not found");
232 context->entry_removed = TRUE;
233 context->app_key = (stc_app_key_s *)key;
238 static stc_app_value_s * __application_lookup(GTree *apps,
239 const stc_app_key_s *key)
241 stc_app_value_s *lookup;
243 ret_value_msg_if(apps == NULL, NULL, "apps is null!");
245 lookup = g_tree_lookup(apps, key);
250 static stc_process_value_s * __process_lookup(GTree *processes,
251 const stc_process_key_s *key)
253 stc_process_value_s *lookup;
255 ret_value_msg_if(processes == NULL, NULL, "processes is null!");
257 lookup = g_tree_lookup(processes, key);
262 static gboolean __processes_tree_check_empty(gpointer key, gpointer value,
265 guint *pid_count = (guint *)data;
270 static gboolean __add_application_monitor(gpointer key, gpointer value,
273 stc_app_value_s *app_value = (stc_app_value_s *)value;
274 default_connection_s *connection = (default_connection_s *)data;
275 stc_s *stc = stc_get_manager();
277 if (stc && connection && connection->ifname) {
278 struct nfacct_rule counter;
281 stc->carg = MALLOC0(counter_arg_s, 1);
282 stc->carg->sock = stc_monitor_get_counter_socket();
285 memset(&counter, 0, sizeof(struct nfacct_rule));
287 counter.carg = stc->carg;
288 counter.classid = app_value->classid;
289 counter.intend = NFACCT_COUNTER;
290 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
292 __add_iptables_in(&counter);
293 __add_iptables_out(&counter);
299 static gboolean __remove_application_monitor(gpointer key, gpointer value,
302 stc_app_value_s *app_value = (stc_app_value_s *)value;
303 default_connection_s *connection = (default_connection_s *)data;
304 stc_s *stc = stc_get_manager();
306 if (stc && connection && connection->ifname) {
307 struct nfacct_rule counter;
310 stc->carg = MALLOC0(counter_arg_s, 1);
311 stc->carg->sock = stc_monitor_get_counter_socket();
314 memset(&counter, 0, sizeof(struct nfacct_rule));
316 counter.carg = stc->carg;
317 counter.classid = app_value->classid;
318 counter.intend = NFACCT_COUNTER;
319 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
321 __del_iptables_in(&counter);
322 __del_iptables_out(&counter);
328 static void __print_rstn(stc_rstn_key_s *rstn_key, stc_rstn_value_s *rstn_value)
330 STC_LOGI("rstn info => rstn_id [%llu], "
331 "app_id [%s], classid [%lu], ifname [%s], "
332 "iftype [%d], rst_state [%d], "
333 "limit [ in (%lld), out (%lld)], "
334 "warn_limit [ in (%lld), out (%lld)], "
335 "counter [ in (%lld), out (%lld)], "
336 "roaming [%d], imsi [%s]",
337 rstn_value->restriction_id,
338 rstn_key->app_id, rstn_value->classid , rstn_key->ifname,
339 rstn_key->iftype, rstn_value->rst_state,
340 rstn_value->limit.in_bytes, rstn_value->limit.out_bytes,
341 rstn_value->warn_limit.in_bytes,
342 rstn_value->warn_limit.out_bytes,
343 rstn_value->counter.in_bytes, rstn_value->counter.out_bytes,
344 rstn_key->roaming, rstn_key->imsi);
347 static void __process_restriction(enum traffic_restriction_type rst_type,
348 stc_rstn_key_s *rstn_key,
349 stc_rstn_value_s *rstn_value, void *data)
351 stc_data_counter_s effective_limit, effective_warn_limit;
352 default_connection_s *old_connection = (default_connection_s *)data;
353 default_connection_s *connection = NULL;
355 if (old_connection != NULL)
356 connection = old_connection;
358 connection = stc_get_default_connection();
360 /* no default ifname */
361 if (connection->ifname == NULL)
364 /* rstn not applicable for this interface */
365 if (rstn_key->ifname != NULL && g_strcmp0("", rstn_key->ifname) != 0 &&
366 g_strcmp0(connection->ifname, rstn_key->ifname) != 0)
369 /* classid is invalid */
370 if (rstn_value->classid == STC_UNKNOWN_CLASSID)
373 effective_limit.out_bytes = rstn_value->limit.out_bytes;
374 effective_limit.in_bytes = rstn_value->limit.in_bytes;
375 effective_warn_limit.out_bytes = rstn_value->warn_limit.out_bytes;
376 effective_warn_limit.in_bytes = rstn_value->warn_limit.in_bytes;
378 if (rst_type == RST_SET) {
379 /* TODO: Change this to runtime memory */
380 table_counters_info info;
382 memset(&info, 0, sizeof(table_counters_info));
383 table_counters_get(rstn_value->restriction_id, &info);
385 effective_limit.out_bytes -= info.sent_bytes;
386 effective_limit.in_bytes -= info.rcv_bytes;
387 effective_warn_limit.out_bytes -= info.sent_bytes;
388 effective_warn_limit.in_bytes -= info.rcv_bytes;
390 if (effective_limit.in_bytes < 0) {
391 effective_limit.in_bytes = 0;
392 rstn_value->in_limit_reached = TRUE;
395 if (effective_limit.out_bytes < 0) {
396 effective_limit.out_bytes = 0;
397 rstn_value->out_limit_reached = TRUE;
400 if (effective_warn_limit.in_bytes < 0)
401 effective_warn_limit.in_bytes = 0;
403 if (effective_warn_limit.out_bytes < 0)
404 effective_warn_limit.out_bytes = 0;
405 STC_LOGD("datausage [in: %lld, out: %lld]",
406 info.rcv_bytes, info.sent_bytes);
409 STC_LOGD("rstn_id [%llu], effective limit [in: %lld, out: %lld], "
410 "effective warn limit [in: %lld, out: %lld], "
411 "datausage [in: %lld, out: %lld]",
412 rstn_value->restriction_id,
413 effective_limit.in_bytes, effective_limit.out_bytes,
414 effective_warn_limit.in_bytes, effective_warn_limit.out_bytes);
416 send_net_restriction(rst_type,
420 effective_limit.out_bytes,
421 effective_limit.in_bytes,
422 effective_warn_limit.out_bytes,
423 effective_warn_limit.in_bytes,
427 rstn_value->rst_state = STC_RESTRICTION_ACTIVATED;
428 rstn_value->in_limit_reached = FALSE;
429 rstn_value->out_limit_reached = FALSE;
435 rstn_value->rst_state = STC_RESTRICTION_REMOVED;
436 rstn_value->in_limit_reached = FALSE;
437 rstn_value->out_limit_reached = FALSE;
444 static gboolean __remove_rstns_foreach_application(gpointer key,
448 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
449 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
450 gchar *app_id = (gchar *)data;
452 /* rstn rule is not for applications */
453 if (rstn_key->app_id == NULL)
456 /* rstn rule is not for this application */
457 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
460 /* rstn rule is already removed */
461 if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
464 /* remove restriction from system */
465 __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
467 __print_rstn(rstn_key, rstn_value);
472 static void __remove_rstns_for_application(gchar *app_id)
474 g_tree_foreach(g_system->rstns, __remove_rstns_foreach_application,
478 static stc_error_e __application_remove_if_empty(const stc_app_key_s *app_key)
480 stc_error_e ret = STC_ERROR_NONE;
482 stc_app_value_s *lookup;
484 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
486 lookup = __application_lookup(g_system->apps, app_key);
488 STC_LOGE("app_key not found");
489 return STC_ERROR_NO_DATA;
492 g_tree_foreach(lookup->processes, __processes_tree_check_empty,
496 /* remove nfacct rule for this classid */
497 __remove_application_monitor((gpointer) app_key, lookup,
498 stc_get_default_connection());
499 __remove_rstns_for_application(app_key->app_id);
502 if (!g_tree_remove(g_system->apps, app_key)) {
503 ret = STC_ERROR_NO_DATA;
504 STC_LOGE("key not found");
510 static stc_error_e __close_contr_sock(stc_system_s *system)
512 ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
514 /* close netlink socket for updating kernel counters */
515 if (g_system->contr_sock != -1) {
516 close(g_system->contr_sock);
517 g_system->contr_sock = -1;
520 if (g_system->contr_gsource_id != 0) {
521 g_source_remove(g_system->contr_gsource_id);
522 g_system->contr_gsource_id = 0;
525 return STC_ERROR_NONE;
528 static gboolean __rstn_counter_update_foreach_classid(gpointer key,
532 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
533 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
534 classid_bytes_context_s *context = (classid_bytes_context_s *)data;
536 if (context->counter->intend != NFACCT_COUNTER)
537 goto try_next_callback;
539 if (rstn_value->classid != context->counter->classid)
540 goto try_next_callback;
542 if (rstn_value->in_limit_reached == TRUE) {
543 context->in_limit_reached = TRUE;
544 goto try_next_callback;
547 if (rstn_value->out_limit_reached == TRUE) {
548 context->out_limit_reached = TRUE;
549 goto try_next_callback;
552 switch (context->counter->iotype) {
553 case NFACCT_COUNTER_IN:
554 rstn_value->counter.in_bytes += context->bytes;
556 if (rstn_value->counter.in_bytes >= rstn_value->warn_limit.in_bytes
557 && rstn_value->warn_limit_crossed_notified == FALSE) {
559 stc_s *stc = (stc_s *)stc_get_manager();
560 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
562 rv = stc_manager_dbus_emit_signal(stc->connection,
563 STC_DBUS_SERVICE_RESTRICTION_PATH,
564 STC_DBUS_INTERFACE_RESTRICTION,
565 "WarnThresholdCrossed",
566 g_variant_new("(s)", rstn_key->app_id));
568 rstn_value->warn_limit_crossed_notified = TRUE;
571 /* block immediately */
572 if (rstn_value->counter.in_bytes >= rstn_value->limit.in_bytes) {
573 __del_iptables_in(context->counter);
574 __add_iptables_in(context->counter);
575 rstn_value->in_limit_reached = TRUE;
577 if (rstn_value->rstn_limit_crossed_notified == FALSE) {
579 stc_s *stc = (stc_s *)stc_get_manager();
580 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
582 rv = stc_manager_dbus_emit_signal(stc->connection,
583 STC_DBUS_SERVICE_RESTRICTION_PATH,
584 STC_DBUS_INTERFACE_RESTRICTION,
585 "RestrictionThresholdCrossed",
586 g_variant_new("(s)", rstn_key->app_id));
588 rstn_value->rstn_limit_crossed_notified = TRUE;
592 g_system->rstns_tree_updated = TRUE;
593 __print_rstn(rstn_key, rstn_value);
595 case NFACCT_COUNTER_OUT:
596 rstn_value->counter.out_bytes += context->bytes;
598 if (rstn_value->counter.out_bytes >= rstn_value->limit.out_bytes
599 && rstn_value->warn_limit_crossed_notified == FALSE) {
601 stc_s *stc = (stc_s *)stc_get_manager();
602 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
604 rv = stc_manager_dbus_emit_signal(stc->connection,
605 STC_DBUS_SERVICE_RESTRICTION_PATH,
606 STC_DBUS_INTERFACE_RESTRICTION,
607 "WarnThresholdCrossed",
608 g_variant_new("(s)", rstn_key->app_id));
610 rstn_value->warn_limit_crossed_notified = TRUE;
613 /* block immediately */
614 if (rstn_value->counter.out_bytes >= rstn_value->limit.out_bytes) {
615 __del_iptables_out(context->counter);
616 __add_iptables_out(context->counter);
617 rstn_value->out_limit_reached = TRUE;
619 if (rstn_value->rstn_limit_crossed_notified == FALSE) {
621 stc_s *stc = (stc_s *)stc_get_manager();
622 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
624 rv = stc_manager_dbus_emit_signal(stc->connection,
625 STC_DBUS_SERVICE_RESTRICTION_PATH,
626 STC_DBUS_INTERFACE_RESTRICTION,
627 "RestrictionThresholdCrossed",
628 g_variant_new("(s)", rstn_key->app_id));
630 rstn_value->rstn_limit_crossed_notified = TRUE;
634 g_system->rstns_tree_updated = TRUE;
635 __print_rstn(rstn_key, rstn_value);
638 STC_LOGE("unknown iotype");
646 static gboolean __update_app_statistics(gpointer key, gpointer value,
649 stc_app_key_s *app_key = (stc_app_key_s *)key;
650 stc_app_value_s *app_value = (stc_app_value_s *)value;
651 time_t *touch_time = (time_t *)data;
652 stc_db_classid_iftype_key stat_key;
653 stc_db_app_stats stat;
654 char *default_ifname = stc_default_connection_get_ifname();
656 memset(&stat_key, 0, sizeof(stc_db_classid_iftype_key));
657 memset(&stat, 0 , sizeof(stc_db_app_stats));
659 stat_key.classid = app_value->classid;
660 stat_key.iftype = stc_default_connection_get_type();
661 if (STC_IFACE_DATACALL == stat_key.iftype)
662 stat_key.imsi = g_strdup("unknown");
664 stat_key.imsi = g_strdup("noneimsi");
665 g_strlcpy(stat_key.ifname, default_ifname, MAX_IFACE_LENGTH);
667 stat.app_id = g_strdup(app_key->app_id);
668 stat.snd_count = app_value->counter.out_bytes;
669 stat.rcv_count = app_value->counter.in_bytes;
672 stat.is_roaming = stc_default_connection_get_roaming();
673 stat.ground = STC_APP_STATE_UNKNOWN;
675 table_statistics_insert(&stat_key, &stat, *touch_time);
677 app_value->counter.out_bytes = 0;
678 app_value->counter.in_bytes = 0;
682 FREE(default_ifname);
687 static gboolean __flush_apps_stats_to_database(gpointer user_data)
689 time_t current_time = time(0);
691 if (g_system->apps_tree_updated == FALSE)
692 return G_SOURCE_REMOVE;
694 g_system->apps_tree_updated = FALSE;
697 g_tree_foreach(g_system->apps,
698 __update_app_statistics,
701 STC_LOGI("Flushed app stats to database");
702 return G_SOURCE_REMOVE;
705 static gboolean __update_counter_statistics(gpointer key, gpointer value,
708 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
709 table_counters_info info = {
710 .restriction_id = rstn_value->restriction_id,
711 .sent_bytes = rstn_value->counter.out_bytes,
712 .rcv_bytes = rstn_value->counter.in_bytes
715 table_counters_update_counters(&info);
720 static gboolean __flush_rstns_counter_to_database(gpointer user_data)
722 time_t current_time = time(0);
724 if (g_system->rstns_tree_updated == FALSE)
725 return G_SOURCE_REMOVE;
727 g_system->rstns_tree_updated = FALSE;
730 g_tree_foreach(g_system->rstns,
731 __update_counter_statistics,
734 STC_LOGI("Flushed rstns counters to database");
735 return G_SOURCE_REMOVE;
738 static gboolean __apps_counter_update_foreach_classid(gpointer key,
742 stc_app_key_s *app_key = (stc_app_key_s *)key;
743 stc_app_value_s *app_value = (stc_app_value_s *)value;
744 classid_bytes_context_s *context = (classid_bytes_context_s *)data;
746 if (context->counter->intend != NFACCT_COUNTER)
747 goto try_next_callback;
749 if (app_value->classid != context->counter->classid)
750 goto try_next_callback;
752 switch (context->counter->iotype) {
753 case NFACCT_COUNTER_IN:
754 app_value->data_usage.in_bytes += context->bytes;
755 app_value->counter.in_bytes = context->bytes;
756 g_system->apps_tree_updated = TRUE;
758 __apps_tree_foreach_print(app_key, app_value, NULL);
760 case NFACCT_COUNTER_OUT:
761 app_value->data_usage.out_bytes += context->bytes;
762 app_value->counter.out_bytes = context->bytes;
763 g_system->apps_tree_updated = TRUE;
765 __apps_tree_foreach_print(app_key, app_value, NULL);
768 STC_LOGE("unknown iotype");
775 static void __fill_nfacct_result(char *cnt_name, int64_t bytes,
776 struct counter_arg *carg)
778 struct nfacct_rule counter = {
785 classid_bytes_context_s context = {
788 .in_limit_reached = FALSE,
789 .out_limit_reached = FALSE
792 STC_LOGD("cnt_name %s", cnt_name);
794 if (!recreate_counter_by_name(cnt_name, &counter)) {
795 STC_LOGE("Can't parse counter name %s", cnt_name);
799 STC_LOGI("classid %lu, iftype %u, iotype %d, intend %d, ifname %s, bytes %lld",
800 context.counter->classid, context.counter->iftype,
801 context.counter->iotype, context.counter->intend,
802 context.counter->ifname, context.bytes);
805 g_tree_foreach(g_system->rstns,
806 __rstn_counter_update_foreach_classid,
810 g_tree_foreach(g_system->apps,
811 __apps_counter_update_foreach_classid,
815 static int __fill_counters(struct rtattr *attr_list[__NFACCT_MAX],
818 struct counter_arg *carg = user_data;
819 char *cnt_name = (char *)RTA_DATA(attr_list[NFACCT_NAME]);
820 if (carg->initiate) {
822 * TODO: this will be used when daemon starts to update existing
823 * counter data if present.
825 populate_counters(cnt_name, carg);
829 (int64_t *)RTA_DATA(attr_list[NFACCT_BYTES]);
830 int bytes = be64toh(*bytes_p);
832 ++carg->serialized_counters;
833 __fill_nfacct_result(cnt_name, bytes, carg);
840 static int __post_fill_counters(void *user_data)
842 struct counter_arg *carg = user_data;
850 static void __process_network_counter(struct genl *ans,
851 struct counter_arg *carg)
853 struct netlink_serialization_params ser_params = {
856 .eval_attr = __fill_counters,
857 .post_eval_attr = __post_fill_counters,
860 netlink_serialization_command *netlink =
861 netlink_create_command(&ser_params);
863 STC_LOGE("Can not create command");
867 netlink->deserialize_answer(&(netlink->params));
870 static gboolean __process_contr_reply(GIOChannel *source,
871 GIOCondition condition,
874 int sock = g_io_channel_unix_get_fd(source);
877 stc_s *stc = stc_get_manager();
879 if ((condition & G_IO_ERR) ||
880 (condition & G_IO_HUP) ||
881 (condition & G_IO_NVAL)) {
882 /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */
884 __close_contr_sock(g_system);
889 STC_LOGE("Can't get stc data");
893 ret = read_netlink(sock,
894 &ans, sizeof(struct genl));
895 STC_LOGD("Counter data received ret [%d]", ret);
899 stc->carg->ans_len = ret;
900 __process_network_counter(&ans, stc->carg);
902 g_idle_add(__flush_apps_stats_to_database, NULL);
903 g_idle_add(__flush_rstns_counter_to_database, NULL);
908 static gboolean __update_contr_cb(void *user_data)
910 /* Here we just sent command, answer we receive in another callback */
911 stc_s *stc = stc_get_manager();
912 ret_value_msg_if(stc == NULL, STC_ERROR_FAIL, "Can't get stc data");
914 stc->carg = MALLOC0(counter_arg_s, 1);
915 stc->carg->sock = stc_monitor_get_counter_socket();
918 STC_LOGD("Get all counters");
919 nfacct_send_get_all(stc->carg);
921 /* we need to continue the timer */
925 static gboolean __rstn_tree_foreach_print(gpointer key, gpointer value,
928 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
929 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
931 __print_rstn(rstn_key, rstn_value);
935 static void __rstn_tree_printall(void)
937 g_tree_foreach(g_system->rstns, __rstn_tree_foreach_print, NULL);
940 static stc_rstn_value_s * __rstn_lookup(GTree *rstns_tree,
941 const stc_rstn_key_s *key)
943 stc_rstn_value_s *lookup;
945 ret_value_msg_if(rstns_tree == NULL, NULL, "rstns_tree is null!");
947 lookup = g_tree_lookup(rstns_tree, key);
952 static gboolean __remove_restriction(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 /* rstn rule is already removed */
959 if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
962 __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
963 __print_rstn(rstn_key, rstn_value);
967 static gboolean __add_restriction_debug(gpointer key, gpointer value,
970 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
971 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
973 /* rstn rule is activated */
974 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
977 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
978 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
980 __process_restriction(RST_SET, rstn_key, rstn_value, data);
982 __print_rstn(rstn_key, rstn_value);
987 static gboolean __add_restriction(gpointer key, gpointer value, gpointer data)
989 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
990 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
992 /* rstn rule is activated */
993 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
996 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
997 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
999 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1004 static stc_error_e __rstn_tree_remove(stc_rstn_key_s *key)
1006 stc_rstn_value_s *lookup_value;
1008 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1010 lookup_value = __rstn_lookup(g_system->rstns, key);
1011 if (!lookup_value) {
1012 STC_LOGE("key not found");
1013 return STC_ERROR_NO_DATA;
1016 __remove_restriction(key, lookup_value, NULL);
1018 /* remove counter also */
1019 table_counters_delete(lookup_value->restriction_id);
1021 if (!g_tree_remove(g_system->rstns, key)) {
1022 STC_LOGD("key not found");
1023 return STC_ERROR_NO_DATA;
1026 return STC_ERROR_NONE;
1029 static stc_error_e __rstn_tree_add(stc_rstn_key_s *key,
1030 stc_rstn_value_s *value, gboolean debug)
1032 stc_rstn_value_s *rstn_value;
1034 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1036 rstn_value = __rstn_lookup(g_system->rstns, key);
1038 stc_rstn_key_s *rstn_key = MALLOC0(stc_rstn_key_s, 1);
1040 STC_LOGE("rstn_key allocation failed");
1041 return STC_ERROR_OUT_OF_MEMORY;
1044 rstn_value = MALLOC0(stc_rstn_value_s, 1);
1046 STC_LOGE("rstn_value allocation failed");
1048 return STC_ERROR_OUT_OF_MEMORY;
1051 rstn_key->app_id = g_strdup(key->app_id);
1052 rstn_key->ifname = g_strdup(key->ifname);
1053 rstn_key->imsi = g_strdup(key->imsi);
1054 rstn_key->iftype = key->iftype;
1055 rstn_key->roaming = key->roaming;
1057 g_tree_insert(g_system->rstns, rstn_key, rstn_value);
1060 rstn_value->restriction_id = value->restriction_id;
1061 rstn_value->rst_state = value->rst_state;
1062 rstn_value->classid = value->classid;
1063 rstn_value->limit.in_bytes = value->limit.in_bytes;
1064 rstn_value->limit.out_bytes = value->limit.out_bytes;
1065 rstn_value->warn_limit.in_bytes = value->warn_limit.in_bytes;
1066 rstn_value->warn_limit.out_bytes = value->warn_limit.out_bytes;
1067 rstn_value->counter.in_bytes = 0;
1068 rstn_value->counter.out_bytes = 0;
1069 rstn_value->warn_limit_crossed_notified = FALSE;
1070 rstn_value->rstn_limit_crossed_notified = FALSE;
1073 __add_restriction_debug(key, rstn_value, NULL);
1075 __add_restriction(key, rstn_value, NULL);
1077 return STC_ERROR_NONE;
1080 static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info,
1083 stc_cb_ret_e ret = STC_CONTINUE;
1086 stc_rstn_value_s value;
1088 memset(&key, 0, sizeof(stc_rstn_key_s));
1089 memset(&value, 0, sizeof(stc_rstn_value_s));
1091 key.app_id = g_strdup(info->app_id);
1092 key.ifname = g_strdup(info->ifname);
1093 key.imsi = g_strdup(info->imsi);
1094 key.iftype = info->iftype;
1095 key.roaming = info->roaming;
1097 value.rst_state = info->rst_state;
1098 value.restriction_id = info->restriction_id;
1100 if (value.rst_state != STC_RESTRICTION_EXCLUDED && info->app_id)
1101 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1103 value.classid = STC_UNKNOWN_CLASSID;
1105 value.limit.in_bytes = info->rcv_limit;
1106 value.limit.out_bytes = info->send_limit;
1107 value.warn_limit.in_bytes = info->rcv_warn_limit;
1108 value.warn_limit.out_bytes = info->send_warn_limit;
1110 if (__rstn_tree_add(&key, &value, FALSE) != STC_ERROR_NONE)
1119 static void __fill_restritions_list(void)
1121 table_restrictions_foreach(__insert_restriction_cb, NULL);
1122 //__rstn_tree_printall();
1125 static gboolean __add_rstn_foreach_application(gpointer key,
1129 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1130 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1131 gchar *app_id = (gchar *)data;
1133 /* rstn rule is not for applications */
1134 if (rstn_key->app_id == NULL)
1137 /* rstn rule is not for this application */
1138 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
1141 /* rstn rule is already applied */
1142 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1145 /* add restriction to system */
1146 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1147 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1149 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1151 __print_rstn(rstn_key, rstn_value);
1156 static void __add_rstns_for_application(gchar *app_id)
1158 g_tree_foreach(g_system->rstns, __add_rstn_foreach_application,
1162 stc_error_e stc_monitor_init(void)
1164 stc_system_s *system = MALLOC0(stc_system_s, 1);
1165 GIOChannel *gio = NULL;
1167 ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY, "stc_system_s malloc fail!");
1169 /* initializing cgroups */
1172 /* creating monitored application tree */
1173 system->apps = g_tree_new_full(__apps_tree_key_compare, NULL,
1174 __apps_tree_key_free,
1175 __apps_tree_value_free);
1177 system->rstns = g_tree_new_full(__rstns_tree_key_compare, NULL,
1178 __rstns_tree_key_free,
1179 __rstns_tree_value_free);
1181 /* create netlink socket for updating kernel counters */
1182 system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
1183 if (!(system->contr_sock)) {
1184 STC_LOGE("failed to open socket");
1186 return STC_ERROR_FAIL;
1189 gio = g_io_channel_unix_new(system->contr_sock);
1190 system->contr_gsource_id =
1191 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
1192 (GIOFunc) __process_contr_reply,
1194 g_io_channel_unref(gio);
1198 /* creating restriction rules tree */
1199 __update_contr_cb(NULL);
1201 /* registering periodic kernel counters update callback */
1202 g_system->contr_timer_id = g_timeout_add_seconds(CONTR_TIMER_INTERVAL,
1205 if (g_system->contr_timer_id == 0) {
1206 STC_LOGE("Failed to register kernel counters update timer");
1207 __close_contr_sock(g_system);
1208 return STC_ERROR_FAIL;
1211 __fill_restritions_list();
1213 return STC_ERROR_NONE;
1216 stc_error_e stc_monitor_deinit(void)
1218 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1220 /* close netlink socket for updating kernel counters */
1221 __close_contr_sock(g_system);
1223 /* remove kernel counters update timer */
1224 if (g_system->contr_timer_id > 0) {
1225 g_source_remove(g_system->contr_timer_id);
1226 g_system->contr_timer_id = 0;
1229 /* destroy monitored application tree */
1230 g_tree_destroy(g_system->apps);
1231 g_system->apps = NULL;
1233 /* destroy restriction rules tree */
1234 g_tree_destroy(g_system->rstns);
1235 g_system->rstns = NULL;
1239 return STC_ERROR_NONE;
1242 stc_error_e stc_monitor_application_add(const stc_app_key_s app_key,
1243 const stc_app_value_s app_value)
1245 stc_error_e ret = STC_ERROR_NONE;
1247 stc_app_value_s *value;
1248 stc_app_value_s *lookup;
1250 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1252 lookup = __application_lookup(g_system->apps, &app_key);
1254 STC_LOGD("app_key already present");
1255 return STC_ERROR_NONE;
1258 key = MALLOC0(stc_app_key_s, 1);
1260 STC_LOGE("key allocation failed");
1261 return STC_ERROR_OUT_OF_MEMORY;
1264 value = MALLOC0(stc_app_value_s, 1);
1266 STC_LOGE("value allocation failed");
1268 return STC_ERROR_OUT_OF_MEMORY;
1271 key->app_id = g_strdup(app_key.app_id);
1272 key->pkg_id = g_strdup(app_key.pkg_id);
1274 value->type = app_value.type;
1275 value->counter.in_bytes = app_value.counter.in_bytes;
1276 value->counter.out_bytes = app_value.counter.out_bytes;
1278 value->processes = g_tree_new_full(__processes_tree_key_compare, NULL,
1279 __processes_tree_key_free,
1280 __processes_tree_value_free);
1282 /* create cgroup and update classid */
1283 value->classid = get_classid_by_app_id(app_key.app_id, TRUE);
1285 g_tree_insert(g_system->apps, key, value);
1287 /* add nfacct rule for this classid */
1288 __add_application_monitor(key, value, stc_get_default_connection());
1289 __add_rstns_for_application(app_key.app_id);
1294 stc_error_e stc_monitor_process_add(const stc_app_key_s app_key,
1295 const stc_process_key_s proc_key,
1296 const stc_process_value_s proc_value)
1298 stc_error_e ret = STC_ERROR_NONE;
1299 stc_app_value_s *app_lookup;
1300 stc_process_key_s *key;
1301 stc_process_value_s *value;
1302 stc_process_value_s *proc_lookup;
1304 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1306 app_lookup = __application_lookup(g_system->apps, &app_key);
1308 STC_LOGD("app_key not found");
1309 return STC_ERROR_FAIL;
1312 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1314 STC_LOGD("proc_key already present");
1315 return STC_ERROR_NONE;
1318 key = MALLOC0(stc_process_key_s, 1);
1320 STC_LOGE("key allocation failed");
1321 return STC_ERROR_OUT_OF_MEMORY;
1324 value = MALLOC0(stc_process_value_s, 1);
1326 STC_LOGE("value allocation failed");
1328 return STC_ERROR_OUT_OF_MEMORY;
1331 key->pid = proc_key.pid;
1333 value->ground = proc_value.ground;
1335 g_tree_insert(app_lookup->processes, key, value);
1337 /* add pid to application cgroup */
1338 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1343 stc_error_e stc_monitor_process_remove(pid_t pid)
1345 stc_error_e ret = STC_ERROR_NONE;
1346 stc_process_key_s proc_key = {
1350 remove_pid_context_s context = {
1352 .proc_key = &proc_key,
1353 .entry_removed = FALSE,
1356 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1358 g_tree_foreach(g_system->apps, __apps_tree_foreach_remove_pid,
1361 if (context.entry_removed)
1362 __application_remove_if_empty(context.app_key);
1367 stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key,
1368 const stc_process_key_s proc_key,
1369 stc_app_state_e ground)
1371 stc_error_e ret = STC_ERROR_NONE;
1372 stc_app_value_s *app_lookup;
1373 stc_process_value_s *proc_lookup;
1375 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1377 app_lookup = __application_lookup(g_system->apps, &app_key);
1379 STC_LOGD("app_key not found");
1380 return STC_ERROR_FAIL;
1383 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1385 STC_LOGD("proc_key not found");
1386 return STC_ERROR_FAIL;
1389 if (proc_lookup->ground != ground)
1390 proc_lookup->ground = ground;
1392 if (ground == STC_APP_STATE_BACKGROUND) {
1393 char *background_app_id = g_strconcat(app_key.app_id,
1394 STC_BACKGROUND_APP_SUFFIX,
1396 place_pids_to_net_cgroup(proc_key.pid, background_app_id);
1397 g_free(background_app_id);
1399 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1405 void stc_monitor_update_rstn_by_default_connection(void *data)
1407 static default_connection_s old_connection;
1408 default_connection_s *new_connection = (default_connection_s *)data;
1410 if (old_connection.path != NULL) {
1412 g_tree_foreach(g_system->apps,
1413 __remove_application_monitor,
1414 (gpointer)&old_connection);
1416 if (g_system->rstns)
1417 g_tree_foreach(g_system->rstns,
1418 __remove_restriction,
1419 (gpointer)&old_connection);
1422 FREE(old_connection.path);
1423 FREE(old_connection.ifname);
1424 old_connection.type = 0;
1425 old_connection.roaming = 0;
1427 if (new_connection != NULL && new_connection->path != NULL) {
1429 g_tree_foreach(g_system->apps,
1430 __add_application_monitor,
1431 (gpointer)new_connection);
1433 if (g_system->rstns)
1434 g_tree_foreach(g_system->rstns, __add_restriction,
1437 old_connection.path = g_strdup(new_connection->path);
1438 old_connection.ifname = g_strdup(new_connection->ifname);
1439 old_connection.type = new_connection->type;
1440 old_connection.roaming = new_connection->roaming;
1444 stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info)
1449 stc_rstn_value_s value;
1451 memset(&key, 0, sizeof(stc_rstn_key_s));
1452 memset(&value, 0, sizeof(stc_rstn_value_s));
1454 key.app_id = g_strdup(info->app_id);
1455 key.ifname = g_strdup(info->ifname);
1456 key.imsi = g_strdup(info->imsi);
1457 key.iftype = info->iftype;
1458 key.roaming = info->roaming;
1460 value.rst_state = info->rst_state;
1461 value.restriction_id = info->restriction_id;
1463 if (value.rst_state != STC_RESTRICTION_EXCLUDED && info->app_id)
1464 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1466 value.classid = STC_UNKNOWN_CLASSID;
1468 value.limit.in_bytes = info->rcv_limit;
1469 value.limit.out_bytes = info->send_limit;
1470 value.warn_limit.in_bytes = info->rcv_warn_limit;
1471 value.warn_limit.out_bytes = info->send_warn_limit;
1473 ret = __rstn_tree_add(&key, &value, TRUE);
1481 stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info)
1485 stc_rstn_key_s key = {
1486 .app_id = g_strdup(info->app_id),
1487 .ifname = g_strdup(info->ifname),
1488 .imsi = g_strdup(info->imsi),
1489 .iftype = info->iftype,
1490 .roaming = info->roaming,
1493 ret = __rstn_tree_remove(&key);
1501 int stc_monitor_get_counter_socket(void)
1503 return g_system->contr_sock;